]> sourceware.org Git - systemtap.git/blame - tapsets.cxx
tapsets: argv extension, oneshot introduction
[systemtap.git] / tapsets.cxx
CommitLineData
56e12059 1// tapset resolution
1af100fc 2// Copyright (C) 2005-2013 Red Hat Inc.
aa30ccd3 3// Copyright (C) 2005-2007 Intel Corporation.
0b8f6579 4// Copyright (C) 2008 James.Bottomley@HansenPartnership.com
56e12059
FCE
5//
6// This file is part of systemtap, and is free software. You can
7// redistribute it and/or modify it under the terms of the GNU General
8// Public License (GPL); either version 2, or (at your option) any
9// later version.
10
11#include "config.h"
12#include "staptree.h"
13#include "elaborate.h"
b55bc428 14#include "tapsets.h"
93646f4d 15#include "task_finder.h"
f31a77f5 16#include "tapset-dynprobe.h"
56e12059 17#include "translate.h"
dc38c0ae 18#include "session.h"
72dbc915 19#include "util.h"
0a6f5a3f 20#include "buildrun.h"
86bf665e 21#include "dwarf_wrappers.h"
2e67a43b 22#include "auto_free.h"
b278033a 23#include "hash.h"
440f755a 24#include "dwflpp.h"
5f8ca04f 25#include "setupdwfl.h"
6b51ee12 26#include <gelf.h>
22d737e8 27
4d0d9e8a 28#include "sdt_types.h"
bd2b1e68 29
3b579393
FCE
30#include <cstdlib>
31#include <algorithm>
bd2b1e68 32#include <deque>
56e12059 33#include <iostream>
d1bcbe71 34#include <fstream>
bd2b1e68 35#include <map>
ec4373ff 36#include <set>
56e12059 37#include <sstream>
bd2b1e68 38#include <stdexcept>
b55bc428 39#include <vector>
67959c62 40#include <stack>
e36387d7 41#include <cstdarg>
29e64872 42#include <cassert>
1969b5bc 43#include <iomanip>
f781f849 44#include <cerrno>
bd2b1e68
GH
45
46extern "C" {
df8fadee 47#include <fcntl.h>
bd2b1e68 48#include <elfutils/libdwfl.h>
7a053d3b 49#include <elfutils/libdw.h>
77de5e9e
GH
50#include <dwarf.h>
51#include <elf.h>
52#include <obstack.h>
b20febf3 53#include <glob.h>
30a279be 54#include <fnmatch.h>
5f0a03a6 55#include <stdio.h>
349dc70e 56#include <sys/types.h>
37001baa 57#include <sys/stat.h>
aaf7ffe8 58#include <math.h>
aff5d390 59#include <regex.h>
37001baa 60#include <unistd.h>
6d5d594e 61#include <wordexp.h>
4b1ad75e
RM
62
63#define __STDC_FORMAT_MACROS
64#include <inttypes.h>
bd2b1e68 65}
77de5e9e 66
508059f0
MW
67// Old elf.h doesn't know about this machine type.
68#ifndef EM_AARCH64
69#define EM_AARCH64 183
70#endif
71
56e12059
FCE
72
73using namespace std;
2171f774 74using namespace __gnu_cxx;
56e12059 75
47dd066d 76
b20febf3
FCE
77
78// ------------------------------------------------------------------------
faea5e16
JS
79
80string
81common_probe_init (derived_probe* p)
82{
26e63673
JS
83 assert(p->session_index != (unsigned)-1);
84 return "(&stap_probes[" + lex_cast(p->session_index) + "])";
faea5e16
JS
85}
86
87
b20febf3 88void
71db462b
DS
89common_probe_entryfn_prologue (systemtap_session& s,
90 string statestr, string probe,
91 string probe_type, bool overload_processing)
b20febf3 92{
944c9a7a 93 if (s.runtime_usermode_p())
ac34f2aa
JS
94 {
95 // If session_state() is NULL, then we haven't even initialized shm yet,
96 // and there's *nothing* for the probe to do. (even alibi is in shm)
97 // So failure skips this whole block through the end of the epilogue.
98 s.op->newline() << "if (likely(session_state())) {";
e481ab2e 99 s.op->indent(1);
ac34f2aa
JS
100 }
101
71db462b 102 s.op->newline() << "#ifdef STP_ALIBI";
7c3e97f4 103 s.op->newline() << "atomic_inc(probe_alibi(" << probe << "->index));";
71db462b 104 s.op->newline() << "#else";
653e6a9a 105
e481ab2e
JS
106 if (s.runtime_usermode_p())
107 s.op->newline() << "int _stp_saved_errno = errno;";
108
fee09ad5 109 s.op->newline() << "struct context* __restrict__ c = NULL;";
71db462b
DS
110 s.op->newline() << "#if !INTERRUPTIBLE";
111 s.op->newline() << "unsigned long flags;";
112 s.op->newline() << "#endif";
b20febf3 113
f887a8c9 114 s.op->newline() << "#ifdef STP_TIMING";
7c3e97f4 115 s.op->newline() << "Stat stat = probe_timing(" << probe << "->index);";
f887a8c9
DS
116 s.op->newline() << "#endif";
117 if (overload_processing && !s.runtime_usermode_p())
71db462b 118 s.op->newline() << "#if defined(STP_TIMING) || defined(STP_OVERLOAD)";
a58d79d0 119 else
71db462b 120 s.op->newline() << "#ifdef STP_TIMING";
b20febf3 121
f887a8c9
DS
122 if (! s.runtime_usermode_p())
123 s.op->newline() << "cycles_t cycles_atstart = get_cycles ();";
124 else
125 {
126 s.op->newline() << "struct timespec timespec_atstart;";
45639d96 127 s.op->newline() << "(void)clock_gettime(CLOCK_MONOTONIC, &timespec_atstart);";
f887a8c9 128 }
71db462b 129 s.op->newline() << "#endif";
994aac0e 130
71db462b
DS
131 s.op->newline() << "#if INTERRUPTIBLE";
132 s.op->newline() << "preempt_disable ();";
133 s.op->newline() << "#else";
134 s.op->newline() << "local_irq_save (flags);";
135 s.op->newline() << "#endif";
b20febf3 136
ac3af990 137 if (! s.runtime_usermode_p())
71db462b
DS
138 {
139 // Check for enough free enough stack space
140 s.op->newline() << "if (unlikely ((((unsigned long) (& c)) & (THREAD_SIZE-1))"; // free space
141 s.op->newline(1) << "< (MINSTACKSPACE + sizeof (struct thread_info)))) {"; // needed space
142 // XXX: may need porting to platforms where task_struct is not
143 // at bottom of kernel stack NB: see also
144 // CONFIG_DEBUG_STACKOVERFLOW
065d5567 145 s.op->newline() << "atomic_inc (skipped_count());";
71db462b 146 s.op->newline() << "#ifdef STP_TIMING";
065d5567 147 s.op->newline() << "atomic_inc (skipped_count_lowstack());";
71db462b
DS
148 s.op->newline() << "#endif";
149 s.op->newline() << "goto probe_epilogue;";
150 s.op->newline(-1) << "}";
151 }
c931ec8a 152
065d5567 153 s.op->newline() << "if (atomic_read (session_state()) != " << statestr << ")";
71db462b
DS
154 s.op->newline(1) << "goto probe_epilogue;";
155 s.op->indent(-1);
9a604fac 156
1d0e697d
DS
157 s.op->newline() << "c = _stp_runtime_entryfn_get_context();";
158 if (s.runtime_usermode_p())
159 {
160 s.op->newline() << "if (!c) {";
161 s.op->newline(1) << "#if !INTERRUPTIBLE";
065d5567 162 s.op->newline() << "atomic_inc (skipped_count());";
1d0e697d
DS
163 s.op->newline() << "#endif";
164 s.op->newline() << "#ifdef STP_TIMING";
065d5567 165 s.op->newline() << "atomic_inc (skipped_count_reentrant());";
1d0e697d
DS
166 s.op->newline() << "#ifdef DEBUG_REENTRANCY";
167 s.op->newline() << "_stp_warn (\"Skipped %s\\n\", " << probe << "->pp);";
168 s.op->newline() << "#endif";
169 s.op->newline() << "#endif";
170 s.op->newline() << "goto probe_epilogue;";
171 s.op->newline(-1) << "}";
172 }
173
71db462b
DS
174 s.op->newline() << "if (atomic_inc_return (& c->busy) != 1) {";
175 s.op->newline(1) << "#if !INTERRUPTIBLE";
065d5567 176 s.op->newline() << "atomic_inc (skipped_count());";
71db462b
DS
177 s.op->newline() << "#endif";
178 s.op->newline() << "#ifdef STP_TIMING";
065d5567 179 s.op->newline() << "atomic_inc (skipped_count_reentrant());";
71db462b
DS
180 s.op->newline() << "#ifdef DEBUG_REENTRANCY";
181 s.op->newline() << "_stp_warn (\"Skipped %s due to %s residency on cpu %u\\n\", "
26e63673 182 << probe << "->pp, c->probe_point ?: \"?\", smp_processor_id());";
c12d974f
FCE
183 // NB: There is a conceivable race condition here with reading
184 // c->probe_point, knowing that this other probe is sort of running.
185 // However, in reality, it's interrupted. Plus even if it were able
186 // to somehow start again, and stop before we read c->probe_point,
187 // at least we have that ?: "?" bit in there to avoid a NULL deref.
71db462b
DS
188 s.op->newline() << "#endif";
189 s.op->newline() << "#endif";
190 s.op->newline() << "atomic_dec (& c->busy);";
191 s.op->newline() << "goto probe_epilogue;";
192 s.op->newline(-1) << "}";
193 s.op->newline();
194 s.op->newline() << "c->last_stmt = 0;";
195 s.op->newline() << "c->last_error = 0;";
196 s.op->newline() << "c->nesting = -1;"; // NB: PR10516 packs locals[] tighter
197 s.op->newline() << "c->uregs = 0;";
198 s.op->newline() << "c->kregs = 0;";
199 s.op->newline() << "#if defined __ia64__";
200 s.op->newline() << "c->unwaddr = 0;";
201 s.op->newline() << "#endif";
f4d70a33
JS
202 if (s.runtime_usermode_p())
203 s.op->newline() << "c->probe_index = " << probe << "->index;";
71db462b
DS
204 s.op->newline() << "c->probe_point = " << probe << "->pp;";
205 s.op->newline() << "#ifdef STP_NEED_PROBE_NAME";
206 s.op->newline() << "c->probe_name = " << probe << "->pn;";
207 s.op->newline() << "#endif";
208 s.op->newline() << "c->probe_type = " << probe_type << ";";
6dceb5c9 209 // reset Individual Probe State union
71db462b 210 s.op->newline() << "memset(&c->ips, 0, sizeof(c->ips));";
e04b5d74 211 s.op->newline() << "c->user_mode_p = 0; c->full_uregs_p = 0;";
71db462b
DS
212 s.op->newline() << "#ifdef STAP_NEED_REGPARM"; // i386 or x86_64 register.stp
213 s.op->newline() << "c->regparm = 0;";
214 s.op->newline() << "#endif";
e0a17418 215
152fa051
LB
216 if(!s.suppress_time_limits){
217 s.op->newline() << "#if INTERRUPTIBLE";
218 s.op->newline() << "c->actionremaining = MAXACTION_INTERRUPTIBLE;";
219 s.op->newline() << "#else";
220 s.op->newline() << "c->actionremaining = MAXACTION;";
221 s.op->newline() << "#endif";
222 }
9915575b
FCE
223 // NB: The following would actually be incorrect.
224 // That's because cycles_sum/cycles_base values are supposed to survive
225 // between consecutive probes. Periodically (STP_OVERLOAD_INTERVAL
226 // cycles), the values will be reset.
227 /*
71db462b
DS
228 s.op->newline() << "#ifdef STP_OVERLOAD";
229 s.op->newline() << "c->cycles_sum = 0;";
230 s.op->newline() << "c->cycles_base = 0;";
231 s.op->newline() << "#endif";
9915575b 232 */
44cfbe25
SM
233
234 s.op->newline() << "#if defined(STP_NEED_UNWIND_DATA)";
235 s.op->newline() << "c->uwcache_user.state = uwcache_uninitialized;";
236 s.op->newline() << "c->uwcache_kernel.state = uwcache_uninitialized;";
237 s.op->newline() << "#endif";
b20febf3 238}
9a604fac 239
a44a0785 240
b20febf3 241void
f887a8c9
DS
242common_probe_entryfn_epilogue (systemtap_session& s,
243 bool overload_processing)
b20febf3 244{
f887a8c9
DS
245 if (overload_processing && !s.runtime_usermode_p())
246 s.op->newline() << "#if defined(STP_TIMING) || defined(STP_OVERLOAD)";
a58d79d0 247 else
f887a8c9
DS
248 s.op->newline() << "#ifdef STP_TIMING";
249 s.op->newline() << "{";
250 s.op->indent(1);
251 if (! s.runtime_usermode_p())
252 {
253 s.op->newline() << "cycles_t cycles_atend = get_cycles ();";
254 // NB: we truncate cycles counts to 32 bits. Perhaps it should be
255 // fewer, if the hardware counter rolls over really quickly. We
256 // handle 32-bit wraparound here.
257 s.op->newline() << "int32_t cycles_elapsed = ((int32_t)cycles_atend > (int32_t)cycles_atstart)";
258 s.op->newline(1) << "? ((int32_t)cycles_atend - (int32_t)cycles_atstart)";
259 s.op->newline() << ": (~(int32_t)0) - (int32_t)cycles_atstart + (int32_t)cycles_atend + 1;";
260 s.op->indent(-1);
261 }
262 else
263 {
264 s.op->newline() << "struct timespec timespec_atend, timespec_elapsed;";
265 s.op->newline() << "long cycles_elapsed;";
45639d96 266 s.op->newline() << "(void)clock_gettime(CLOCK_MONOTONIC, &timespec_atend);";
f887a8c9
DS
267 s.op->newline() << "_stp_timespec_sub(&timespec_atend, &timespec_atstart, &timespec_elapsed);";
268 // 'cycles_elapsed' is really elapsed nanoseconds
269 s.op->newline() << "cycles_elapsed = (timespec_elapsed.tv_sec * NSEC_PER_SEC) + timespec_elapsed.tv_nsec;";
270 }
271
272 s.op->newline() << "#ifdef STP_TIMING";
273 s.op->newline() << "if (likely (stat)) _stp_stat_add(stat, cycles_elapsed);";
274 s.op->newline() << "#endif";
275
276 if (overload_processing && !s.runtime_usermode_p())
277 {
278 s.op->newline() << "#ifdef STP_OVERLOAD";
279 s.op->newline() << "{";
a58d79d0
DS
280 // If the cycle count has wrapped (cycles_atend > cycles_base),
281 // let's go ahead and pretend the interval has been reached.
282 // This should reset cycles_base and cycles_sum.
f887a8c9
DS
283 s.op->newline(1) << "cycles_t interval = (cycles_atend > c->cycles_base)";
284 s.op->newline(1) << "? (cycles_atend - c->cycles_base)";
285 s.op->newline() << ": (STP_OVERLOAD_INTERVAL + 1);";
286 s.op->newline(-1) << "c->cycles_sum += cycles_elapsed;";
a58d79d0
DS
287
288 // If we've spent more than STP_OVERLOAD_THRESHOLD cycles in a
289 // probe during the last STP_OVERLOAD_INTERVAL cycles, the probe
290 // has overloaded the system and we need to quit.
7baf48e9
FCE
291 // NB: this is not suppressible via --suppress-runtime-errors,
292 // because this is a system safety metric that we cannot trust
293 // unprivileged users to override.
f887a8c9
DS
294 s.op->newline() << "if (interval > STP_OVERLOAD_INTERVAL) {";
295 s.op->newline(1) << "if (c->cycles_sum > STP_OVERLOAD_THRESHOLD) {";
296 s.op->newline(1) << "_stp_error (\"probe overhead exceeded threshold\");";
065d5567
JS
297 s.op->newline() << "atomic_set (session_state(), STAP_SESSION_ERROR);";
298 s.op->newline() << "atomic_inc (error_count());";
f887a8c9
DS
299 s.op->newline(-1) << "}";
300
301 s.op->newline() << "c->cycles_base = cycles_atend;";
302 s.op->newline() << "c->cycles_sum = 0;";
303 s.op->newline(-1) << "}";
304 s.op->newline(-1) << "}";
305 s.op->newline() << "#endif";
a58d79d0 306 }
e57b735a 307
f887a8c9
DS
308 s.op->newline(-1) << "}";
309 s.op->newline() << "#endif";
e57b735a 310
f887a8c9
DS
311 s.op->newline() << "c->probe_point = 0;"; // vacated
312 s.op->newline() << "#ifdef STP_NEED_PROBE_NAME";
313 s.op->newline() << "c->probe_name = 0;";
314 s.op->newline() << "#endif";
315 s.op->newline() << "c->probe_type = 0;";
7baf48e9
FCE
316
317
f887a8c9
DS
318 s.op->newline() << "if (unlikely (c->last_error && c->last_error[0])) {";
319 s.op->indent(1);
320 if (s.suppress_handler_errors) // PR 13306
b78a0fbb 321 {
065d5567 322 s.op->newline() << "atomic_inc (error_count());";
7baf48e9
FCE
323 }
324 else
325 {
f887a8c9
DS
326 s.op->newline() << "if (c->last_stmt != NULL)";
327 s.op->newline(1) << "_stp_softerror (\"%s near %s\", c->last_error, c->last_stmt);";
328 s.op->newline(-1) << "else";
329 s.op->newline(1) << "_stp_softerror (\"%s\", c->last_error);";
330 s.op->indent(-1);
065d5567
JS
331 s.op->newline() << "atomic_inc (error_count());";
332 s.op->newline() << "if (atomic_read (error_count()) > MAXERRORS) {";
333 s.op->newline(1) << "atomic_set (session_state(), STAP_SESSION_ERROR);";
f887a8c9
DS
334 s.op->newline() << "_stp_exit ();";
335 s.op->newline(-1) << "}";
7baf48e9
FCE
336 }
337
f887a8c9 338 s.op->newline(-1) << "}";
7baf48e9
FCE
339
340
f887a8c9 341 s.op->newline() << "atomic_dec (&c->busy);";
e57b735a 342
f887a8c9
DS
343 s.op->newline(-1) << "probe_epilogue:"; // context is free
344 s.op->indent(1);
e57b735a 345
8ca891c4
DS
346 // In dyninst mode, we're not done with the context yet, since
347 // _stp_error() still needs a context structure (since the log
348 // buffers are stored there).
349 if (!s.runtime_usermode_p())
350 {
e481ab2e 351 s.op->newline() << "_stp_runtime_entryfn_put_context(c);";
8ca891c4 352 }
f887a8c9 353 if (! s.suppress_handler_errors) // PR 13306
7baf48e9
FCE
354 {
355 // Check for excessive skip counts.
065d5567
JS
356 s.op->newline() << "if (unlikely (atomic_read (skipped_count()) > MAXSKIPPED)) {";
357 s.op->newline(1) << "if (unlikely (pseudo_atomic_cmpxchg(session_state(), STAP_SESSION_RUNNING, STAP_SESSION_ERROR) == STAP_SESSION_RUNNING))";
f887a8c9
DS
358 s.op->newline() << "_stp_error (\"Skipped too many probes, check MAXSKIPPED or try again with stap -t for more details.\");";
359 s.op->newline(-1) << "}";
7baf48e9 360 }
e57b735a 361
f887a8c9
DS
362 s.op->newline() << "#if INTERRUPTIBLE";
363 s.op->newline() << "preempt_enable_no_resched ();";
364 s.op->newline() << "#else";
365 s.op->newline() << "local_irq_restore (flags);";
366 s.op->newline() << "#endif";
653e6a9a 367
944c9a7a 368 if (s.runtime_usermode_p())
ac34f2aa 369 {
e481ab2e 370 s.op->newline() << "_stp_runtime_entryfn_put_context(c);";
ac34f2aa 371 s.op->newline() << "errno = _stp_saved_errno;";
ac34f2aa 372 }
e481ab2e
JS
373
374 s.op->newline() << "#endif // STP_ALIBI";
375
376 if (s.runtime_usermode_p())
377 s.op->newline(-1) << "}";
440f755a 378}
e57b735a 379
e57b735a 380
440f755a 381// ------------------------------------------------------------------------
e57b735a 382
440f755a
JS
383// ------------------------------------------------------------------------
384// Dwarf derived probes. "We apologize for the inconvience."
385// ------------------------------------------------------------------------
e57b735a 386
4627ed58
JS
387static const string TOK_KERNEL("kernel");
388static const string TOK_MODULE("module");
389static const string TOK_FUNCTION("function");
390static const string TOK_INLINE("inline");
391static const string TOK_CALL("call");
4bda987e 392static const string TOK_EXPORTED("exported");
4627ed58
JS
393static const string TOK_RETURN("return");
394static const string TOK_MAXACTIVE("maxactive");
395static const string TOK_STATEMENT("statement");
396static const string TOK_ABSOLUTE("absolute");
397static const string TOK_PROCESS("process");
a794dbeb 398static const string TOK_PROVIDER("provider");
4627ed58
JS
399static const string TOK_MARK("mark");
400static const string TOK_TRACE("trace");
401static const string TOK_LABEL("label");
63b4fd14 402static const string TOK_LIBRARY("library");
576eaefe 403static const string TOK_PLT("plt");
e7d4410d
LB
404static const string TOK_METHOD("method");
405static const string TOK_CLASS("class");;
c31add50
JL
406static const string TOK_CALLEE("callee");;
407static const string TOK_CALLEES("callees");;
e57b735a 408
5c378838 409static int query_cu (Dwarf_Die * cudie, dwarf_query *q);
6b517475 410static void query_addr(Dwarf_Addr addr, dwarf_query *q);
f4faaf86 411static void query_plt_statement(dwarf_query *q);
e57b735a 412
440f755a
JS
413// Can we handle this query with just symbol-table info?
414enum dbinfo_reqt
415{
416 dbr_unknown,
417 dbr_none, // kernel.statement(NUM).absolute
418 dbr_need_symtab, // can get by with symbol table if there's no dwarf
419 dbr_need_dwarf
420};
e57b735a 421
20e4a32c 422
440f755a
JS
423struct base_query; // forward decls
424struct dwarf_query;
425struct dwflpp;
426struct symbol_table;
20e4a32c 427
a781f401 428
440f755a
JS
429struct
430symbol_table
431{
432 module_info *mod_info; // associated module
433 map<string, func_info*> map_by_name;
1c6b77e5
JS
434 multimap<Dwarf_Addr, func_info*> map_by_addr;
435 typedef multimap<Dwarf_Addr, func_info*>::iterator iterator_t;
440f755a
JS
436 typedef pair<iterator_t, iterator_t> range_t;
437#ifdef __powerpc__
438 GElf_Word opd_section;
439#endif
2867a2a1
JS
440 void add_symbol(const char *name, bool weak, bool descriptor,
441 Dwarf_Addr addr, Dwarf_Addr *high_addr);
440f755a 442 enum info_status read_symbols(FILE *f, const string& path);
83ca3872 443 enum info_status read_from_elf_file(const string& path,
2713ea24 444 systemtap_session &sess);
83ca3872 445 enum info_status read_from_text_file(const string& path,
2713ea24 446 systemtap_session &sess);
440f755a
JS
447 enum info_status get_from_elf();
448 void prepare_section_rejection(Dwfl_Module *mod);
449 bool reject_section(GElf_Word section);
440f755a
JS
450 void purge_syscall_stubs();
451 func_info *lookup_symbol(const string& name);
452 Dwarf_Addr lookup_symbol_address(const string& name);
453 func_info *get_func_containing_address(Dwarf_Addr addr);
3d372d6b 454 func_info *get_first_func();
7a053d3b 455
440f755a
JS
456 symbol_table(module_info *mi) : mod_info(mi) {}
457 ~symbol_table();
458};
77de5e9e 459
440f755a
JS
460static bool null_die(Dwarf_Die *die)
461{
822a6a3d 462 static Dwarf_Die null;
440f755a
JS
463 return (!die || !memcmp(die, &null, sizeof(null)));
464}
c4ce66a1
JS
465
466
7a053d3b 467enum
bd2b1e68 468function_spec_type
7a053d3b 469 {
bd2b1e68
GH
470 function_alone,
471 function_and_file,
7a053d3b 472 function_file_and_line
bd2b1e68
GH
473 };
474
ec4373ff 475
bd2b1e68 476struct dwarf_builder;
f10534c6 477struct dwarf_var_expanding_visitor;
77de5e9e 478
2930abc7 479
b20febf3
FCE
480// XXX: This class is a candidate for subclassing to separate
481// the relocation vs non-relocation variants. Likewise for
482// kprobe vs kretprobe variants.
483
484struct dwarf_derived_probe: public derived_probe
b55bc428 485{
b20febf3
FCE
486 dwarf_derived_probe (const string& function,
487 const string& filename,
488 int line,
489 const string& module,
490 const string& section,
491 Dwarf_Addr dwfl_addr,
2930abc7 492 Dwarf_Addr addr,
b20febf3
FCE
493 dwarf_query & q,
494 Dwarf_Die* scope_die);
20e4a32c 495
b20febf3
FCE
496 string module;
497 string section;
498 Dwarf_Addr addr;
63b4fd14 499 string path;
27dc09b1 500 bool has_process;
2930abc7 501 bool has_return;
c9bad430 502 bool has_maxactive;
63b4fd14 503 bool has_library;
c9bad430 504 long maxactive_val;
4ad95bbc 505 // dwarf_derived_probe_group::emit_module_decls uses this to emit sdt kprobe definition
b642c901
SC
506 string user_path;
507 string user_lib;
b95e2b79 508 bool access_vars;
2930abc7 509
af234c40
JS
510 unsigned saved_longs, saved_strings;
511 dwarf_derived_probe* entry_handler;
512
b8da0ad1 513 void printsig (std::ostream &o) const;
6b66b9f7 514 virtual void join_group (systemtap_session& s);
3689db05 515 void emit_probe_local_init(systemtap_session& s, translator_output * o);
d0bfd2ac 516 void getargs(std::list<std::string> &arg_set) const;
0a98fd42 517
42e38653 518 void emit_privilege_assertion (translator_output*);
27dc09b1
DB
519 void print_dupe_stamp(ostream& o);
520
bd2b1e68 521 // Pattern registration helpers.
7a053d3b 522 static void register_statement_variants(match_node * root,
27dc09b1 523 dwarf_builder * dw,
42e38653 524 privilege_t privilege);
fd6602a0 525 static void register_function_variants(match_node * root,
27dc09b1 526 dwarf_builder * dw,
42e38653 527 privilege_t privilege);
440d9b00
DB
528 static void register_function_and_statement_variants(systemtap_session& s,
529 match_node * root,
27dc09b1 530 dwarf_builder * dw,
42e38653 531 privilege_t privilege);
b1615c74
JS
532 static void register_sdt_variants(systemtap_session& s,
533 match_node * root,
534 dwarf_builder * dw);
535 static void register_plt_variants(systemtap_session& s,
536 match_node * root,
537 dwarf_builder * dw);
c4ce66a1 538 static void register_patterns(systemtap_session& s);
6b66b9f7
JS
539
540protected:
541 dwarf_derived_probe(probe *base,
542 probe_point *location,
543 Dwarf_Addr addr,
544 bool has_return):
74fe61bc
LB
545 derived_probe(base, location), addr(addr), has_process(0),
546 has_return(has_return), has_maxactive(0), has_library(0),
547 maxactive_val(0), access_vars(false), saved_longs(0),
548 saved_strings(0), entry_handler(0)
6b66b9f7
JS
549 {}
550
551private:
d0bfd2ac 552 list<string> args;
8c67c337 553 void saveargs(dwarf_query& q, Dwarf_Die* scope_die, Dwarf_Addr dwfl_addr);
20c6c071
GH
554};
555
dc38c0ae 556
6b66b9f7 557struct uprobe_derived_probe: public dwarf_derived_probe
6d0f3f0c 558{
6d0f3f0c 559 int pid; // 0 => unrestricted
0973d815 560
6d0f3f0c
FCE
561 uprobe_derived_probe (const string& function,
562 const string& filename,
563 int line,
564 const string& module,
6d0f3f0c
FCE
565 const string& section,
566 Dwarf_Addr dwfl_addr,
567 Dwarf_Addr addr,
568 dwarf_query & q,
6b66b9f7
JS
569 Dwarf_Die* scope_die):
570 dwarf_derived_probe(function, filename, line, module, section,
571 dwfl_addr, addr, q, scope_die), pid(0)
572 {}
6d0f3f0c 573
0973d815
FCE
574 // alternate constructor for process(PID).statement(ADDR).absolute
575 uprobe_derived_probe (probe *base,
576 probe_point *location,
577 int pid,
578 Dwarf_Addr addr,
6b66b9f7
JS
579 bool has_return):
580 dwarf_derived_probe(base, location, addr, has_return), pid(pid)
581 {}
9ace370f 582
6d0f3f0c 583 void join_group (systemtap_session& s);
2865d17a 584
42e38653 585 void emit_privilege_assertion (translator_output*);
8f6d8c2b 586 void print_dupe_stamp(ostream& o) { print_dupe_stamp_unprivileged_process_owner (o); }
c0f84e7b
SC
587 void getargs(std::list<std::string> &arg_set) const;
588 void saveargs(int nargs);
589private:
590 list<string> args;
6d0f3f0c
FCE
591};
592
dc38c0ae
DS
593struct dwarf_derived_probe_group: public derived_probe_group
594{
595private:
b20febf3
FCE
596 multimap<string,dwarf_derived_probe*> probes_by_module;
597 typedef multimap<string,dwarf_derived_probe*>::iterator p_b_m_iterator;
dc38c0ae
DS
598
599public:
08b5a50c 600 dwarf_derived_probe_group() {}
b20febf3
FCE
601 void enroll (dwarf_derived_probe* probe);
602 void emit_module_decls (systemtap_session& s);
603 void emit_module_init (systemtap_session& s);
b4be7cbc 604 void emit_module_refresh (systemtap_session& s);
b20febf3 605 void emit_module_exit (systemtap_session& s);
dc38c0ae
DS
606};
607
608
20c6c071 609// Helper struct to thread through the dwfl callbacks.
2c384610 610struct base_query
20c6c071 611{
c4ce66a1
JS
612 base_query(dwflpp & dw, literal_map_t const & params);
613 base_query(dwflpp & dw, const string & module_val);
2c384610 614 virtual ~base_query() {}
bd2b1e68 615
5227f1ea 616 systemtap_session & sess;
2c384610 617 dwflpp & dw;
5227f1ea 618
070764c0
JL
619 // Used to keep track of which modules were visited during
620 // iterate_over_modules()
621 set<string> visited_modules;
622
bd2b1e68 623 // Parameter extractors.
86bf665e 624 static bool has_null_param(literal_map_t const & params,
888af770 625 string const & k);
86bf665e 626 static bool get_string_param(literal_map_t const & params,
bd2b1e68 627 string const & k, string & v);
86bf665e 628 static bool get_number_param(literal_map_t const & params,
bd2b1e68 629 string const & k, long & v);
86bf665e 630 static bool get_number_param(literal_map_t const & params,
c239d28c 631 string const & k, Dwarf_Addr & v);
bbbc7241 632 static void query_library_callback (base_query *me, const char *data);
45cdb40e 633 static void query_plt_callback (base_query *me, const char *link, size_t addr);
f301a9ff 634 virtual void query_library (const char *data) = 0;
576eaefe 635 virtual void query_plt (const char *link, size_t addr) = 0;
84c84ac4 636
b55bc428 637
2c384610
DS
638 // Extracted parameters.
639 bool has_kernel;
91af0778
FCE
640 bool has_module;
641 bool has_process;
63b4fd14 642 bool has_library;
576eaefe
SC
643 bool has_plt;
644 bool has_statement;
2c384610 645 string module_val; // has_kernel => module_val = "kernel"
63b4fd14 646 string path; // executable path if module is a .so
576eaefe 647 string plt_val; // has_plt => plt wildcard
2c384610
DS
648
649 virtual void handle_query_module() = 0;
650};
651
652
c4ce66a1 653base_query::base_query(dwflpp & dw, literal_map_t const & params):
576eaefe 654 sess(dw.sess), dw(dw), has_library(false), has_plt(false), has_statement(false)
2c384610 655{
91af0778 656 has_kernel = has_null_param (params, TOK_KERNEL);
2c384610
DS
657 if (has_kernel)
658 module_val = "kernel";
91af0778
FCE
659
660 has_module = get_string_param (params, TOK_MODULE, module_val);
661 if (has_module)
662 has_process = false;
4baf0e53 663 else
d0a7f5a9 664 {
63b4fd14 665 string library_name;
576eaefe 666 long statement_num_val;
d0a7f5a9 667 has_process = get_string_param(params, TOK_PROCESS, module_val);
63b4fd14 668 has_library = get_string_param (params, TOK_LIBRARY, library_name);
576eaefe
SC
669 if ((has_plt = has_null_param (params, TOK_PLT)))
670 plt_val = "*";
671 else has_plt = get_string_param (params, TOK_PLT, plt_val);
672 has_statement = get_number_param(params, TOK_STATEMENT, statement_num_val);
673
84c84ac4 674 if (has_process)
05fb3e0c 675 module_val = find_executable (module_val, sess.sysroot, sess.sysenv);
84c84ac4
SC
676 if (has_library)
677 {
678 if (! contains_glob_chars (library_name))
679 {
05fb3e0c
WF
680 path = path_remove_sysroot(sess, module_val);
681 module_val = find_executable (library_name, sess.sysroot,
682 sess.sysenv, "LD_LIBRARY_PATH");
47e226ed
SC
683 if (module_val.find('/') == string::npos)
684 {
685 // We didn't find library_name so use iterate_over_libraries
686 module_val = path;
687 path = library_name;
688 }
84c84ac4
SC
689 }
690 else
691 path = library_name;
692 }
d0a7f5a9 693 }
91af0778
FCE
694
695 assert (has_kernel || has_process || has_module);
2c384610
DS
696}
697
c4ce66a1 698base_query::base_query(dwflpp & dw, const string & module_val)
576eaefe
SC
699 : sess(dw.sess), dw(dw), has_library(false), has_plt(false), has_statement(false),
700 module_val(module_val)
c4ce66a1
JS
701{
702 // NB: This uses '/' to distinguish between kernel modules and userspace,
703 // which means that userspace modules won't get any PATH searching.
704 if (module_val.find('/') == string::npos)
705 {
706 has_kernel = (module_val == TOK_KERNEL);
707 has_module = !has_kernel;
708 has_process = false;
709 }
710 else
711 {
712 has_kernel = has_module = false;
713 has_process = true;
714 }
715}
716
2c384610 717bool
86bf665e 718base_query::has_null_param(literal_map_t const & params,
2c384610
DS
719 string const & k)
720{
888af770 721 return derived_probe_builder::has_null_param(params, k);
2c384610
DS
722}
723
724
725bool
86bf665e 726base_query::get_string_param(literal_map_t const & params,
2c384610
DS
727 string const & k, string & v)
728{
729 return derived_probe_builder::get_param (params, k, v);
730}
731
732
733bool
86bf665e 734base_query::get_number_param(literal_map_t const & params,
2c384610
DS
735 string const & k, long & v)
736{
737 int64_t value;
738 bool present = derived_probe_builder::get_param (params, k, value);
739 v = (long) value;
740 return present;
741}
742
743
744bool
86bf665e 745base_query::get_number_param(literal_map_t const & params,
2c384610
DS
746 string const & k, Dwarf_Addr & v)
747{
748 int64_t value;
749 bool present = derived_probe_builder::get_param (params, k, value);
750 v = (Dwarf_Addr) value;
751 return present;
752}
753
2c384610
DS
754struct dwarf_query : public base_query
755{
e1278bd4 756 dwarf_query(probe * base_probe,
2c384610
DS
757 probe_point * base_loc,
758 dwflpp & dw,
86bf665e 759 literal_map_t const & params,
b642c901
SC
760 vector<derived_probe *> & results,
761 const string user_path,
762 const string user_lib);
2c384610 763
c4ce66a1 764 vector<derived_probe *> & results;
8f14e444 765 set<string> inlined_non_returnable; // function names
c4ce66a1
JS
766 probe * base_probe;
767 probe_point * base_loc;
b642c901
SC
768 string user_path;
769 string user_lib;
c4ce66a1 770
2c384610 771 virtual void handle_query_module();
5f0a03a6
JK
772 void query_module_dwarf();
773 void query_module_symtab();
5d5bd369 774 void query_library (const char *data);
576eaefe 775 void query_plt (const char *entry, size_t addr);
2c384610 776
2930abc7
FCE
777 void add_probe_point(string const & funcname,
778 char const * filename,
779 int line,
780 Dwarf_Die *scope_die,
781 Dwarf_Addr addr);
36f9dd1d 782
857bdfd1
JS
783 // Track addresses we've already seen in a given module
784 set<Dwarf_Addr> alias_dupes;
785
7fdd3e2c
JS
786 // Track inlines we've already seen as well
787 // NB: this can't be compared just by entrypc, as inlines can overlap
788 set<inline_instance_info> inline_dupes;
789
e2941743
JL
790 // Used in .callee[s] probes, when calling iterate_over_callees() (which
791 // provides the actual stack). Retains the addrs of the callers unwind addr
792 // where the callee is found. Specifies multiple callers. E.g. when a callee
793 // at depth 2 is found, callers[1] has the addr of the caller, and callers[0]
794 // has the addr of the caller's caller.
795 stack<Dwarf_Addr> *callers;
796
2930abc7 797 // Extracted parameters.
7a053d3b 798 string function_val;
20c6c071
GH
799
800 bool has_function_str;
801 bool has_statement_str;
802 bool has_function_num;
803 bool has_statement_num;
7a053d3b
RM
804 string statement_str_val;
805 string function_str_val;
c239d28c
GH
806 Dwarf_Addr statement_num_val;
807 Dwarf_Addr function_num_val;
20c6c071 808
b8da0ad1 809 bool has_call;
4bda987e 810 bool has_exported;
b8da0ad1 811 bool has_inline;
20c6c071
GH
812 bool has_return;
813
c9bad430
DS
814 bool has_maxactive;
815 long maxactive_val;
816
20c6c071
GH
817 bool has_label;
818 string label_val;
819
c31add50
JL
820 bool has_callee;
821 string callee_val;
822
823 bool has_callees_num;
824 long callees_num_val;
825
20c6c071
GH
826 bool has_relative;
827 long relative_val;
828
37ebca01
FCE
829 bool has_absolute;
830
467bea43
SC
831 bool has_mark;
832
5f0a03a6
JK
833 enum dbinfo_reqt dbinfo_reqt;
834 enum dbinfo_reqt assess_dbinfo_reqt();
835
7d6d0afc 836 void parse_function_spec(const string & spec);
20c6c071 837 function_spec_type spec_type;
7d6d0afc 838 vector<string> scopes;
20c6c071
GH
839 string function;
840 string file;
0c8b7d37 841 line_t line_type;
879eb9e9 842 int line[2];
5f0a03a6 843 bool query_done; // Found exact match
20c6c071 844
5a617dc6
JL
845 // Holds the prologue end of the current function
846 Dwarf_Addr prologue_end;
847
bd25380d 848 set<string> filtered_srcfiles;
7e1279ea
FCE
849
850 // Map official entrypc -> func_info object
86bf665e
TM
851 inline_instance_map_t filtered_inlines;
852 func_info_map_t filtered_functions;
4df79aaf
JS
853
854 void query_module_functions ();
cfa29998
JL
855
856 vector<probe_point::component*> canonicalize_comps(const string& module,
857 const string& function,
858 const string& file,
859 int line);
b55bc428
FCE
860};
861
98afd80e 862
435f53a7
FCE
863static void delete_session_module_cache (systemtap_session& s); // forward decl
864
865
98afd80e 866struct dwarf_builder: public derived_probe_builder
b55bc428 867{
665e1256 868 map <string,dwflpp*> kern_dw; /* NB: key string could be a wildcard */
7a24d422 869 map <string,dwflpp*> user_dw;
b642c901
SC
870 string user_path;
871 string user_lib;
44ffe90c
JL
872
873 // Holds modules to suggest functions from. NB: aggregates over
874 // recursive calls to build() when deriving globby probes.
875 set <string> modules_seen;
876
ae2552da 877 dwarf_builder() {}
aa30ccd3 878
ae2552da 879 dwflpp *get_kern_dw(systemtap_session& sess, const string& module)
707bf35e 880 {
ea14cf67
FCE
881 if (kern_dw[module] == 0)
882 kern_dw[module] = new dwflpp(sess, module, true); // might throw
ae2552da 883 return kern_dw[module];
707bf35e
JS
884 }
885
886 dwflpp *get_user_dw(systemtap_session& sess, const string& module)
887 {
ea14cf67
FCE
888 if (user_dw[module] == 0)
889 user_dw[module] = new dwflpp(sess, module, false); // might throw
707bf35e
JS
890 return user_dw[module];
891 }
7a24d422
FCE
892
893 /* NB: not virtual, so can be called from dtor too: */
822a6a3d 894 void dwarf_build_no_more (bool)
aa30ccd3 895 {
435f53a7
FCE
896 delete_map(kern_dw);
897 delete_map(user_dw);
7a24d422
FCE
898 }
899
900 void build_no_more (systemtap_session &s)
901 {
902 dwarf_build_no_more (s.verbose > 3);
435f53a7 903 delete_session_module_cache (s);
aa30ccd3
FCE
904 }
905
e38d6504
RM
906 ~dwarf_builder()
907 {
7a24d422 908 dwarf_build_no_more (false);
c8959a29 909 }
aa30ccd3 910
5227f1ea 911 virtual void build(systemtap_session & sess,
7a053d3b 912 probe * base,
20c6c071 913 probe_point * location,
86bf665e 914 literal_map_t const & parameters,
20c6c071 915 vector<derived_probe *> & finished_results);
b55bc428
FCE
916};
917
5111fc3e 918
e1278bd4 919dwarf_query::dwarf_query(probe * base_probe,
20c6c071
GH
920 probe_point * base_loc,
921 dwflpp & dw,
86bf665e 922 literal_map_t const & params,
b642c901
SC
923 vector<derived_probe *> & results,
924 const string user_path,
925 const string user_lib)
e2941743
JL
926 : base_query(dw, params), results(results), base_probe(base_probe),
927 base_loc(base_loc), user_path(user_path), user_lib(user_lib),
5a617dc6 928 callers(NULL), has_relative(false), relative_val(0), prologue_end(0)
bd2b1e68
GH
929{
930 // Reduce the query to more reasonable semantic values (booleans,
931 // extracted strings, numbers, etc).
bd2b1e68
GH
932 has_function_str = get_string_param(params, TOK_FUNCTION, function_str_val);
933 has_function_num = get_number_param(params, TOK_FUNCTION, function_num_val);
934
935 has_statement_str = get_string_param(params, TOK_STATEMENT, statement_str_val);
936 has_statement_num = get_number_param(params, TOK_STATEMENT, statement_num_val);
937
0f336e95 938 has_label = get_string_param(params, TOK_LABEL, label_val);
c31add50
JL
939 has_callee = get_string_param(params, TOK_CALLEE, callee_val);
940 if (has_null_param(params, TOK_CALLEES))
941 { // .callees ==> .callees(1) (also equivalent to .callee("*"))
942 has_callees_num = true;
943 callees_num_val = 1;
944 }
945 else
946 {
947 has_callees_num = get_number_param(params, TOK_CALLEES, callees_num_val);
948 if (has_callees_num && callees_num_val < 1)
949 throw SEMANTIC_ERROR(_(".callees(N) only acceptable for N >= 1"),
950 base_probe->tok);
951 }
0f336e95 952
b8da0ad1 953 has_call = has_null_param(params, TOK_CALL);
4bda987e 954 has_exported = has_null_param(params, TOK_EXPORTED);
b8da0ad1 955 has_inline = has_null_param(params, TOK_INLINE);
bd2b1e68 956 has_return = has_null_param(params, TOK_RETURN);
c9bad430 957 has_maxactive = get_number_param(params, TOK_MAXACTIVE, maxactive_val);
37ebca01 958 has_absolute = has_null_param(params, TOK_ABSOLUTE);
467bea43 959 has_mark = false;
37ebca01 960
bd2b1e68 961 if (has_function_str)
7d6d0afc 962 parse_function_spec(function_str_val);
bd2b1e68 963 else if (has_statement_str)
7d6d0afc 964 parse_function_spec(statement_str_val);
0daad364 965
5f0a03a6
JK
966 dbinfo_reqt = assess_dbinfo_reqt();
967 query_done = false;
0daad364
JS
968}
969
970
440f755a
JS
971func_info_map_t *
972get_filtered_functions(dwarf_query *q)
973{
974 return &q->filtered_functions;
975}
976
977
978inline_instance_map_t *
979get_filtered_inlines(dwarf_query *q)
980{
981 return &q->filtered_inlines;
982}
983
984
2c384610 985void
5f0a03a6 986dwarf_query::query_module_dwarf()
2c384610
DS
987{
988 if (has_function_num || has_statement_num)
989 {
990 // If we have module("foo").function(0xbeef) or
991 // module("foo").statement(0xbeef), the address is relative
992 // to the start of the module, so we seek the function
993 // number plus the module's bias.
6b517475
JS
994 Dwarf_Addr addr = has_function_num ?
995 function_num_val : statement_num_val;
08d1d520
MW
996
997 // These are raw addresses, we need to know what the elf_bias
998 // is to feed it to libdwfl based functions.
999 Dwarf_Addr elf_bias;
1000 Elf *elf = dwfl_module_getelf (dw.module, &elf_bias);
1001 assert(elf);
1002 addr += elf_bias;
6b517475 1003 query_addr(addr, this);
2c384610
DS
1004 }
1005 else
1006 {
1007 // Otherwise if we have a function("foo") or statement("foo")
1008 // specifier, we have to scan over all the CUs looking for
1009 // the function(s) in question
1010 assert(has_function_str || has_statement_str);
4df79aaf
JS
1011
1012 // For simple cases, no wildcard and no source:line, we can do a very
1013 // quick function lookup in a module-wide cache.
1ffb8bd1
JS
1014 if (spec_type == function_alone &&
1015 !dw.name_has_wildcard(function) &&
1016 !startswith(function, "_Z"))
4df79aaf
JS
1017 query_module_functions();
1018 else
337b7c44 1019 dw.iterate_over_cus(&query_cu, this, false);
2c384610
DS
1020 }
1021}
1022
5f0a03a6
JK
1023static void query_func_info (Dwarf_Addr entrypc, func_info & fi,
1024 dwarf_query * q);
1025
91bb9081
LB
1026static void
1027query_symtab_func_info (func_info & fi, dwarf_query * q)
1028{
1029 assert(null_die(&fi.die));
1030
1031 Dwarf_Addr addr = fi.addr;
1032
1033 // Now compensate for the dw bias because the addresses come
1034 // from dwfl_module_symtab, so fi->addr is NOT a normal dw address.
1035 q->dw.get_module_dwarf(false, false);
1036 addr -= q->dw.module_bias;
1037
1038 // If there are already probes in this module, lets not duplicate.
1039 // This can come from other weak symbols/aliases or existing
a41f2ad7
JS
1040 // matches from Dwarf DIE functions. Try to add this addr to the
1041 // collection, and only continue if it was new.
1042 if (q->alias_dupes.insert(addr).second)
1043 query_func_info(addr, fi, q);
91bb9081
LB
1044}
1045
5f0a03a6
JK
1046void
1047dwarf_query::query_module_symtab()
1048{
1049 // Get the symbol table if it's necessary, sufficient, and not already got.
1050 if (dbinfo_reqt == dbr_need_dwarf)
1051 return;
1052
1053 module_info *mi = dw.mod_info;
1054 if (dbinfo_reqt == dbr_need_symtab)
1055 {
1056 if (mi->symtab_status == info_unknown)
1057 mi->get_symtab(this);
1058 if (mi->symtab_status == info_absent)
1059 return;
1060 }
1061
1062 func_info *fi = NULL;
1063 symbol_table *sym_table = mi->sym_table;
1064
1065 if (has_function_str)
1066 {
1067 // Per dwarf_query::assess_dbinfo_reqt()...
1068 assert(spec_type == function_alone);
1069 if (dw.name_has_wildcard(function_str_val))
1070 {
2e67a43b 1071 symbol_table::iterator_t iter;
1c6b77e5
JS
1072 for (iter = sym_table->map_by_addr.begin();
1073 iter != sym_table->map_by_addr.end();
2e67a43b 1074 ++iter)
5f0a03a6 1075 {
1c6b77e5 1076 fi = iter->second;
5f0a03a6
JK
1077 if (!null_die(&fi->die))
1078 continue; // already handled in query_module_dwarf()
1079 if (dw.function_name_matches_pattern(fi->name, function_str_val))
91bb9081 1080 query_symtab_func_info(*fi, this);
5f0a03a6
JK
1081 }
1082 }
1083 else
1084 {
1085 fi = sym_table->lookup_symbol(function_str_val);
2867a2a1 1086 if (fi && !fi->descriptor && null_die(&fi->die))
91bb9081 1087 query_symtab_func_info(*fi, this);
5f0a03a6 1088 }
5f0a03a6
JK
1089 }
1090}
1091
1092void
1093dwarf_query::handle_query_module()
1094{
f4faaf86
JS
1095 if (has_plt && has_statement_num)
1096 {
1097 query_plt_statement (this);
1098 return;
1099 }
1100
0035051e 1101 bool report = dbinfo_reqt == dbr_need_dwarf;
1c6b77e5
JS
1102 dw.get_module_dwarf(false, report);
1103
1104 // prebuild the symbol table to resolve aliases
1105 dw.mod_info->get_symtab(this);
1106
857bdfd1
JS
1107 // reset the dupe-checking for each new module
1108 alias_dupes.clear();
7fdd3e2c 1109 inline_dupes.clear();
857bdfd1 1110
5f0a03a6
JK
1111 if (dw.mod_info->dwarf_status == info_present)
1112 query_module_dwarf();
1c6b77e5 1113
91bb9081
LB
1114 // Consult the symbol table, asm and weak functions can show up
1115 // in the symbol table but not in dwarf and minidebuginfo is
1116 // located in the gnu_debugdata section, alias_dupes checking
1117 // is done before adding any probe points
1118 if (!query_done && !pending_interrupts)
5f0a03a6
JK
1119 query_module_symtab();
1120}
1121
2c384610 1122
7d6d0afc
JS
1123void
1124dwarf_query::parse_function_spec(const string & spec)
bd2b1e68 1125{
1d12a9b2
JS
1126 line_type = ABSOLUTE;
1127 line[0] = line[1] = 0;
1128
91699a70 1129 size_t src_pos, line_pos, dash_pos, scope_pos;
bd2b1e68 1130
7d6d0afc 1131 // look for named scopes
91699a70
JS
1132 scope_pos = spec.rfind("::");
1133 if (scope_pos != string::npos)
bd2b1e68 1134 {
91699a70
JS
1135 tokenize_cxx(spec.substr(0, scope_pos), scopes);
1136 scope_pos += 2;
bd2b1e68 1137 }
91699a70
JS
1138 else
1139 scope_pos = 0;
bd2b1e68 1140
7d6d0afc
JS
1141 // look for a source separator
1142 src_pos = spec.find('@', scope_pos);
1143 if (src_pos == string::npos)
bd2b1e68 1144 {
7d6d0afc
JS
1145 function = spec.substr(scope_pos);
1146 spec_type = function_alone;
bd2b1e68 1147 }
7d6d0afc 1148 else
879eb9e9 1149 {
7d6d0afc 1150 function = spec.substr(scope_pos, src_pos - scope_pos);
7a053d3b 1151
7d6d0afc
JS
1152 // look for a line-number separator
1153 line_pos = spec.find_first_of(":+", src_pos);
1154 if (line_pos == string::npos)
1155 {
1156 file = spec.substr(src_pos + 1);
1157 spec_type = function_and_file;
1158 }
1159 else
1160 {
1161 file = spec.substr(src_pos + 1, line_pos - src_pos - 1);
1162
1163 // classify the line spec
1164 spec_type = function_file_and_line;
1165 if (spec[line_pos] == '+')
1166 line_type = RELATIVE;
1167 else if (spec[line_pos + 1] == '*' &&
1168 spec.length() == line_pos + 2)
1169 line_type = WILDCARD;
1170 else
1171 line_type = ABSOLUTE;
1172
1173 if (line_type != WILDCARD)
1174 try
1175 {
1176 // try to parse either N or N-M
1177 dash_pos = spec.find('-', line_pos + 1);
1178 if (dash_pos == string::npos)
1179 line[0] = line[1] = lex_cast<int>(spec.substr(line_pos + 1));
1180 else
1181 {
1182 line_type = RANGE;
1183 line[0] = lex_cast<int>(spec.substr(line_pos + 1,
1184 dash_pos - line_pos - 1));
1185 line[1] = lex_cast<int>(spec.substr(dash_pos + 1));
1186 }
1187 }
1188 catch (runtime_error & exn)
1189 {
1190 goto bad;
1191 }
1192 }
bd2b1e68
GH
1193 }
1194
7d6d0afc
JS
1195 if (function.empty() ||
1196 (spec_type != function_alone && file.empty()))
bd2b1e68
GH
1197 goto bad;
1198
7d6d0afc 1199 if (sess.verbose > 2)
bd2b1e68 1200 {
b530b5b3
LB
1201 //clog << "parsed '" << spec << "'";
1202 clog << _F("parse '%s'", spec.c_str());
41c262f3 1203
7d6d0afc
JS
1204 if (!scopes.empty())
1205 clog << ", scope '" << scopes[0] << "'";
1206 for (unsigned i = 1; i < scopes.size(); ++i)
1207 clog << "::'" << scopes[i] << "'";
41c262f3 1208
7d6d0afc
JS
1209 clog << ", func '" << function << "'";
1210
1211 if (spec_type != function_alone)
1212 clog << ", file '" << file << "'";
1213
1214 if (spec_type == function_file_and_line)
1215 {
1216 clog << ", line ";
1217 switch (line_type)
1218 {
1219 case ABSOLUTE:
1220 clog << line[0];
1221 break;
1222
1223 case RELATIVE:
1224 clog << "+" << line[0];
1225 break;
1226
1227 case RANGE:
1228 clog << line[0] << " - " << line[1];
1229 break;
1230
1231 case WILDCARD:
1232 clog << "*";
1233 break;
1234 }
1235 }
1236
1237 clog << endl;
bd2b1e68
GH
1238 }
1239
7d6d0afc
JS
1240 return;
1241
1242bad:
dc09353a 1243 throw SEMANTIC_ERROR(_F("malformed specification '%s'", spec.c_str()),
7d6d0afc 1244 base_probe->tok);
bd2b1e68
GH
1245}
1246
05fb3e0c
WF
1247string path_remove_sysroot(const systemtap_session& sess, const string& path)
1248{
1249 size_t pos;
1250 string retval = path;
1251 if (!sess.sysroot.empty() &&
1252 (pos = retval.find(sess.sysroot)) != string::npos)
1253 retval.replace(pos, sess.sysroot.length(), "/");
1254 return retval;
1255}
bd2b1e68 1256
36f9dd1d 1257void
1ffb8bd1 1258dwarf_query::add_probe_point(const string& dw_funcname,
b20febf3 1259 const char* filename,
36f9dd1d 1260 int line,
b20febf3 1261 Dwarf_Die* scope_die,
36f9dd1d
FCE
1262 Dwarf_Addr addr)
1263{
b20febf3 1264 string reloc_section; // base section for relocation purposes
27646582 1265 Dwarf_Addr reloc_addr; // relocated
b20febf3 1266 const string& module = dw.module_name; // "kernel" or other
1ffb8bd1 1267 string funcname = dw_funcname;
36f9dd1d 1268
37ebca01
FCE
1269 assert (! has_absolute); // already handled in dwarf_builder::build()
1270
f4faaf86 1271 reloc_addr = dw.relocate_address(addr, reloc_section);
2930abc7 1272
1ffb8bd1 1273 // If we originally used the linkage name, then let's call it that way
1ffb8bd1 1274 const char* linkage_name;
91bb9081 1275 if (!null_die(scope_die) && startswith (this->function, "_Z")
f450a7e3 1276 && (linkage_name = dwarf_linkage_name (scope_die)))
1ffb8bd1
JS
1277 funcname = linkage_name;
1278
7f9f3386
FCE
1279 if (sess.verbose > 1)
1280 {
b530b5b3 1281 clog << _("probe ") << funcname << "@" << filename << ":" << line;
b20febf3 1282 if (string(module) == TOK_KERNEL)
b530b5b3 1283 clog << _(" kernel");
91af0778 1284 else if (has_module)
b530b5b3 1285 clog << _(" module=") << module;
91af0778 1286 else if (has_process)
b530b5b3 1287 clog << _(" process=") << module;
b20febf3 1288 if (reloc_section != "") clog << " reloc=" << reloc_section;
b20febf3 1289 clog << " pc=0x" << hex << addr << dec;
7f9f3386 1290 }
4baf0e53 1291
27646582 1292 bool bad = dw.blacklisted_p (funcname, filename, line, module,
789448a3 1293 addr, has_return);
b20febf3
FCE
1294 if (sess.verbose > 1)
1295 clog << endl;
7f9f3386 1296
84048984
FCE
1297 if (module == TOK_KERNEL)
1298 {
1299 // PR 4224: adapt to relocatable kernel by subtracting the _stext address here.
1300 reloc_addr = addr - sess.sym_stext;
37ebca01 1301 reloc_section = "_stext"; // a message to runtime's _stp_module_relocate
84048984
FCE
1302 }
1303
b20febf3
FCE
1304 if (! bad)
1305 {
1a0dbc5a 1306 sess.unwindsym_modules.insert (module);
6d0f3f0c
FCE
1307
1308 if (has_process)
1309 {
05fb3e0c 1310 string module_tgt = path_remove_sysroot(sess, module);
6d0f3f0c 1311 results.push_back (new uprobe_derived_probe(funcname, filename, line,
05fb3e0c 1312 module_tgt, reloc_section, addr, reloc_addr,
6d0f3f0c
FCE
1313 *this, scope_die));
1314 }
1315 else
1316 {
1317 assert (has_kernel || has_module);
1318 results.push_back (new dwarf_derived_probe(funcname, filename, line,
06aca46a 1319 module, reloc_section, addr, reloc_addr,
6d0f3f0c
FCE
1320 *this, scope_die));
1321 }
b20febf3 1322 }
2930abc7
FCE
1323}
1324
5f0a03a6
JK
1325enum dbinfo_reqt
1326dwarf_query::assess_dbinfo_reqt()
1327{
1328 if (has_absolute)
1329 {
1330 // kernel.statement(NUM).absolute
1331 return dbr_none;
1332 }
c31add50 1333 if (has_inline || has_label || has_callee || has_callees_num)
5f0a03a6
JK
1334 {
1335 // kernel.function("f").inline or module("m").function("f").inline
1336 return dbr_need_dwarf;
1337 }
1338 if (has_function_str && spec_type == function_alone)
1339 {
1340 // kernel.function("f") or module("m").function("f")
1341 return dbr_need_symtab;
1342 }
1343 if (has_statement_num)
1344 {
1345 // kernel.statement(NUM) or module("m").statement(NUM)
1346 // Technically, all we need is the module offset (or _stext, for
1347 // the kernel). But for that we need either the ELF file or (for
1348 // _stext) the symbol table. In either case, the symbol table
1349 // is available, and that allows us to map the NUM (address)
1350 // to a function, which is goodness.
1351 return dbr_need_symtab;
1352 }
1353 if (has_function_num)
1354 {
1355 // kernel.function(NUM) or module("m").function(NUM)
1356 // Need the symbol table so we can back up from NUM to the
1357 // start of the function.
1358 return dbr_need_symtab;
1359 }
1360 // Symbol table tells us nothing about source files or line numbers.
1361 return dbr_need_dwarf;
1362}
2930abc7 1363
cfa29998
JL
1364vector<probe_point::component*>
1365dwarf_query::canonicalize_comps(const string& module,
1366 const string& function,
1367 const string& file, int line)
1368{
1369 vector<probe_point::component*> comps;
1370 if (has_kernel)
1371 comps.push_back (new probe_point::component(TOK_KERNEL));
1372 else if(has_module)
1373 comps.push_back (new probe_point::component(TOK_MODULE, new literal_string(module)));
1374 else if(has_process)
1375 comps.push_back (new probe_point::component(TOK_PROCESS, new literal_string(module)));
1376 else
1377 assert (0);
1378
1379 string fn_or_stmt;
1380 if (has_function_str || has_function_num)
1381 fn_or_stmt = "function";
1382 else
1383 fn_or_stmt = "statement";
1384
1385 if (has_function_str || has_statement_str)
1386 {
1387 string retro_name = function;
1388 if (file != "")
1389 {
1390 retro_name += ("@" + file);
1391 if (line > 0)
1392 retro_name += (":" + lex_cast (line));
1393 }
1394 comps.push_back
1395 (new probe_point::component
1396 (fn_or_stmt, new literal_string (retro_name)));
1397 }
1398 else if (has_function_num || has_statement_num)
1399 {
1400 Dwarf_Addr retro_addr;
1401 if (has_function_num)
1402 retro_addr = function_num_val;
1403 else
1404 retro_addr = statement_num_val;
1405 comps.push_back (new probe_point::component
1406 (fn_or_stmt,
1407 new literal_number(retro_addr, true)));
1408
1409 if (has_absolute)
1410 comps.push_back (new probe_point::component (TOK_ABSOLUTE));
1411 }
1412
1413 if (has_call)
1414 comps.push_back (new probe_point::component(TOK_CALL));
1415 if (has_exported)
1416 comps.push_back (new probe_point::component(TOK_EXPORTED));
1417 if (has_inline)
1418 comps.push_back (new probe_point::component(TOK_INLINE));
1419 if (has_return)
1420 comps.push_back (new probe_point::component(TOK_RETURN));
1421 if (has_maxactive)
1422 comps.push_back (new probe_point::component
1423 (TOK_MAXACTIVE, new literal_number(maxactive_val)));
1424
1425 return comps;
1426}
2930abc7 1427
b8da0ad1
FCE
1428// The critical determining factor when interpreting a pattern
1429// string is, perhaps surprisingly: "presence of a lineno". The
1430// presence of a lineno changes the search strategy completely.
1431//
1432// Compare the two cases:
1433//
1434// 1. {statement,function}(foo@file.c:lineno)
1435// - find the files matching file.c
1436// - in each file, find the functions matching foo
1437// - query the file for line records matching lineno
1438// - iterate over the line records,
1439// - and iterate over the functions,
1440// - if(haspc(function.DIE, line.addr))
1441// - if looking for statements: probe(lineno.addr)
1442// - if looking for functions: probe(function.{entrypc,return,etc.})
1443//
1444// 2. {statement,function}(foo@file.c)
1445// - find the files matching file.c
1446// - in each file, find the functions matching foo
1447// - probe(function.{entrypc,return,etc.})
1448//
1449// Thus the first decision we make is based on the presence of a
1450// lineno, and we enter entirely different sets of callbacks
1451// depending on that decision.
1452//
1453// Note that the first case is a generalization fo the second, in that
1454// we could theoretically search through line records for matching
1455// file names (a "table scan" in rdbms lingo). Luckily, file names
1456// are already cached elsewhere, so we can do an "index scan" as an
1457// optimization.
7e1279ea 1458
bd2b1e68 1459static void
4cd232e4 1460query_statement (string const & func,
20e4a32c 1461 char const * file,
4cd232e4 1462 int line,
bcc12710 1463 Dwarf_Die *scope_die,
20e4a32c 1464 Dwarf_Addr stmt_addr,
4cd232e4 1465 dwarf_query * q)
bd2b1e68 1466{
39bcd429
FCE
1467 try
1468 {
cee35f73 1469 q->add_probe_point(func, file ? file : "",
a9b2f3a5 1470 line, scope_die, stmt_addr);
39bcd429
FCE
1471 }
1472 catch (const semantic_error& e)
1473 {
1474 q->sess.print_error (e);
1475 }
bd2b1e68
GH
1476}
1477
6b517475
JS
1478static void
1479query_addr(Dwarf_Addr addr, dwarf_query *q)
1480{
1481 dwflpp &dw = q->dw;
1482
08d1d520
MW
1483 if (q->sess.verbose > 2)
1484 clog << "query_addr 0x" << hex << addr << dec << endl;
6b517475
JS
1485
1486 // First pick which CU contains this address
1487 Dwarf_Die* cudie = dw.query_cu_containing_address(addr);
1488 if (!cudie) // address could be wildly out of range
1489 return;
1490 dw.focus_on_cu(cudie);
1491
1492 // Now compensate for the dw bias
1493 addr -= dw.module_bias;
1494
1495 // Per PR5787, we look up the scope die even for
1496 // statement_num's, for blacklist sensitivity and $var
1497 // resolution purposes.
1498
1499 // Find the scopes containing this address
1500 vector<Dwarf_Die> scopes = dw.getscopes(addr);
1501 if (scopes.empty())
1502 return;
1503
1504 // Look for the innermost containing function
1505 Dwarf_Die *fnscope = NULL;
1506 for (size_t i = 0; i < scopes.size(); ++i)
1507 {
1508 int tag = dwarf_tag(&scopes[i]);
1509 if ((tag == DW_TAG_subprogram && !q->has_inline) ||
1510 (tag == DW_TAG_inlined_subroutine &&
4bda987e 1511 !q->has_call && !q->has_return && !q->has_exported))
6b517475
JS
1512 {
1513 fnscope = &scopes[i];
1514 break;
1515 }
1516 }
1517 if (!fnscope)
1518 return;
1519 dw.focus_on_function(fnscope);
1520
1521 Dwarf_Die *scope = q->has_function_num ? fnscope : &scopes[0];
1522
1523 const char *file = dwarf_decl_file(fnscope);
1524 int line;
1525 dwarf_decl_line(fnscope, &line);
1526
1527 // Function probes should reset the addr to the function entry
1528 // and possibly perform prologue searching
1529 if (q->has_function_num)
1530 {
1bbf3f90
JS
1531 if (!dw.die_entrypc(fnscope, &addr))
1532 return;
6b517475 1533 if (dwarf_tag(fnscope) == DW_TAG_subprogram &&
b5b9879f
JL
1534 (q->sess.prologue_searching ||
1535 (q->has_process && !q->dw.has_valid_locs()))) // PR 6871 && PR 6941
6b517475
JS
1536 {
1537 func_info func;
1538 func.die = *fnscope;
1539 func.name = dw.function_name;
1540 func.decl_file = file;
1541 func.decl_line = line;
1542 func.entrypc = addr;
1543
1544 func_info_map_t funcs(1, func);
1545 dw.resolve_prologue_endings (funcs);
5a617dc6
JL
1546 q->prologue_end = funcs[0].prologue_end;
1547
1548 // PR13200: if it's a .return probe, we need to emit a *retprobe based
1549 // on the entrypc so here we only use prologue_end for non .return
1550 // probes (note however that .return probes still take advantage of
1551 // prologue_end: PR14436)
1552 if (!q->has_return)
1553 addr = funcs[0].prologue_end;
6b517475
JS
1554 }
1555 }
1556 else
1557 {
1558 dwarf_line_t address_line(dwarf_getsrc_die(cudie, addr));
1559 if (address_line)
1560 {
1561 file = address_line.linesrc();
1562 line = address_line.lineno();
1563 }
1564
1565 // Verify that a raw address matches the beginning of a
1566 // statement. This is a somewhat lame check that the address
1567 // is at the start of an assembly instruction. Mark probes are in the
1568 // middle of a macro and thus not strictly at a statement beginning.
1569 // Guru mode may override this check.
1570 if (!q->has_mark && (!address_line || address_line.addr() != addr))
1571 {
1572 stringstream msg;
2a97f50b 1573 msg << _F("address %#" PRIx64 " does not match the beginning of a statement",
b530b5b3 1574 addr);
6b517475 1575 if (address_line)
2a97f50b 1576 msg << _F(" (try %#" PRIx64 ")", address_line.addr());
6b517475 1577 else
b530b5b3
LB
1578 msg << _F(" (no line info found for '%s', in module '%s')",
1579 dw.cu_name().c_str(), dw.module_name.c_str());
6b517475 1580 if (! q->sess.guru_mode)
dc09353a 1581 throw SEMANTIC_ERROR(msg.str());
2713ea24 1582 else
6b517475
JS
1583 q->sess.print_warning(msg.str());
1584 }
1585 }
1586
1587 // Build a probe at this point
1588 query_statement(dw.function_name, file, line, scope, addr, q);
1589}
1590
f4faaf86
JS
1591static void
1592query_plt_statement(dwarf_query *q)
1593{
1594 assert (q->has_plt && q->has_statement_num);
1595
1596 Dwarf_Addr addr = q->statement_num_val;
1597 if (q->sess.verbose > 2)
1598 clog << "query_plt_statement 0x" << hex << addr << dec << endl;
1599
1600 // First adjust the raw address to dwfl's elf bias.
1601 Dwarf_Addr elf_bias;
1602 Elf *elf = dwfl_module_getelf (q->dw.module, &elf_bias);
1603 assert(elf);
1604 addr += elf_bias;
1605
1606 // Now compensate for the dw bias
1607 q->dw.get_module_dwarf(false, false);
1608 addr -= q->dw.module_bias;
1609
1610 // Build a probe at this point
1611 query_statement(q->plt_val, NULL, -1, NULL, addr, q);
1612}
1613
8096dd7d
JS
1614static void
1615query_label (string const & func,
1616 char const * label,
1617 char const * file,
1618 int line,
1619 Dwarf_Die *scope_die,
1620 Dwarf_Addr stmt_addr,
1621 dwarf_query * q)
1622{
6b517475
JS
1623 assert (q->has_statement_str || q->has_function_str);
1624
8096dd7d
JS
1625 size_t i = q->results.size();
1626
1627 // weed out functions whose decl_file isn't one of
1628 // the source files that we actually care about
6b517475 1629 if (q->spec_type != function_alone &&
8096dd7d
JS
1630 q->filtered_srcfiles.count(file) == 0)
1631 return;
1632
1633 query_statement(func, file, line, scope_die, stmt_addr, q);
1634
c72aa911
JS
1635 // after the fact, insert the label back into the derivation chain
1636 probe_point::component* ppc =
1637 new probe_point::component(TOK_LABEL, new literal_string (label));
1638 for (; i < q->results.size(); ++i)
1639 {
1640 derived_probe* p = q->results[i];
1641 probe_point* pp = new probe_point(*p->locations[0]);
1642 pp->components.push_back (ppc);
1643 p->base = p->base->create_alias(p->locations[0], pp);
1644 }
8096dd7d
JS
1645}
1646
67959c62
JL
1647static void
1648query_callee (char const * callee,
1649 char const * file,
1650 int line,
1651 Dwarf_Die *callee_die,
1652 Dwarf_Addr callee_addr,
80a63be6 1653 Dwarf_Die *caller_addr,
67959c62
JL
1654 stack<Dwarf_Addr> *callers,
1655 dwarf_query * q)
1656{
1657 assert (q->has_function_str);
e2941743 1658 q->callers = callers;
80a63be6
JL
1659
1660 size_t i = q->results.size();
1661
67959c62 1662 query_statement(callee, file, line, callee_die, callee_addr, q);
80a63be6
JL
1663
1664 // We just added a new probe for this callee, e.g. process.function("callee").
1665 // To help show the derivation, let's add the intermediate step:
1666 // process.function("caller").callee("callee"). This is what will show up if
1667 // we're in listing mode.
1668
1669 // First we need to get info about the caller. It may or may not be in
1670 // filtered_functions (since filtered_functions is concerned with funcs that
1671 // matched function(pattern), nothing to do with callee(pattern)). So rather
1672 // than searching, let's just directly poll info.
1673 string caller_name;
1674 const char *caller_file;
1675 int caller_line;
1676
1677 // Let's avoid using focus_on_function() because the caller may be in a
1678 // completely different CU. Directly get the info from dwarf.
1679 caller_name = dwarf_diename(caller_addr) ?: "function";
1680 caller_file = dwarf_decl_file(caller_addr) ?: "<unknown source>";
1681 if (dwarf_decl_line(caller_addr, &caller_line))
1682 caller_line = 0;
1683
1684 // Now, we build the list of components that the intermediate probe_point will
1685 // have. It's pretty much the same as what q describes. We canonicalize it and
1686 // then add on our own canonicalized .callee(func@file:line).
1687
1688 // Since canonicalize_comps() does not treat has_callee, we'll get back a
1689 // 'pure' kernel/process/module.[library.].function probe with no other suffix
1690 // tacked on (since .callee can only follow .function)
1691 vector<probe_point::component*> caller_comps =
1692 q->canonicalize_comps(q->dw.module_name, caller_name,
1693 caller_file, caller_line);
1694
1695 // Mini-canonicalization of callee, to add on to caller_comps
1696 string callee_file = string(file);
1697 string canon_callee = callee;
1698 if (callee_file != "")
1699 {
1700 canon_callee += ("@" + callee_file);
1701 if (line > 0)
1702 canon_callee += (":" + lex_cast(line));
1703 }
1704 caller_comps.push_back(new probe_point::component
1705 (TOK_CALLEE, new literal_string(canon_callee)));
1706
1707 // There should only be a single result. Take that result and make its base
1708 // probe based on this function(str).callee(str) intermediate step.
1709 for (; i < q->results.size(); ++i)
1710 {
1711 derived_probe* p = q->results[i];
1712 probe_point* pp = new probe_point(*p->locations[0]);
1713 pp->components = caller_comps;
1714 p->base = new probe (p->base, pp);
1715 }
67959c62
JL
1716}
1717
7e1279ea 1718static void
3e961ba6 1719query_inline_instance_info (inline_instance_info & ii,
7e1279ea
FCE
1720 dwarf_query * q)
1721{
b6581717 1722 try
7e1279ea 1723 {
8f14e444
FCE
1724 assert (! q->has_return); // checked by caller already
1725 if (q->sess.verbose>2)
b530b5b3
LB
1726 clog << _F("querying entrypc %#" PRIx64 " of instance of inline '%s'\n",
1727 ii.entrypc, ii.name.c_str());
8f14e444
FCE
1728 query_statement (ii.name, ii.decl_file, ii.decl_line,
1729 &ii.die, ii.entrypc, q);
7e1279ea 1730 }
b6581717 1731 catch (semantic_error &e)
7e1279ea 1732 {
b6581717 1733 q->sess.print_error (e);
7e1279ea
FCE
1734 }
1735}
1736
1737static void
1738query_func_info (Dwarf_Addr entrypc,
bcc12710 1739 func_info & fi,
7e1279ea
FCE
1740 dwarf_query * q)
1741{
b6581717 1742 try
7e1279ea 1743 {
5a617dc6
JL
1744 // If it's a .return probe, we need to emit a *retprobe based on the
1745 // entrypc (PR13200). Note however that if prologue_end is valid,
1746 // dwarf_derived_probe will still take advantage of it by creating a new
1747 // probe there if necessary to pick up target vars (PR14436).
1748 if (fi.prologue_end == 0 || q->has_return)
1749 {
170ccf10 1750 q->prologue_end = fi.prologue_end;
5a617dc6
JL
1751 query_statement (fi.name, fi.decl_file, fi.decl_line,
1752 &fi.die, entrypc, q);
1753 }
b6581717 1754 else
5a617dc6 1755 {
5a617dc6
JL
1756 query_statement (fi.name, fi.decl_file, fi.decl_line,
1757 &fi.die, fi.prologue_end, q);
1758 }
7e1279ea 1759 }
b6581717 1760 catch (semantic_error &e)
7e1279ea 1761 {
b6581717 1762 q->sess.print_error (e);
7e1279ea
FCE
1763 }
1764}
1765
1766
bd4b874d 1767static void
0e2e28a1 1768query_srcfile_label (const dwarf_line_t& line, dwarf_query * q)
bd4b874d 1769{
bd4b874d
SC
1770 Dwarf_Addr addr = line.addr();
1771
1772 for (func_info_map_t::iterator i = q->filtered_functions.begin();
1773 i != q->filtered_functions.end(); ++i)
1774 if (q->dw.die_has_pc (i->die, addr))
f09d0d1e
JS
1775 q->dw.iterate_over_labels (&i->die, q->label_val, i->name,
1776 q, query_label);
1777
1778 for (inline_instance_map_t::iterator i = q->filtered_inlines.begin();
1779 i != q->filtered_inlines.end(); ++i)
1780 if (q->dw.die_has_pc (i->die, addr))
1781 q->dw.iterate_over_labels (&i->die, q->label_val, i->name,
1782 q, query_label);
bd4b874d
SC
1783}
1784
7e1279ea 1785static void
0e2e28a1 1786query_srcfile_line (const dwarf_line_t& line, dwarf_query * q)
7e1279ea 1787{
86bf665e 1788 Dwarf_Addr addr = line.addr();
4cd232e4 1789
86bf665e 1790 int lineno = line.lineno();
847bf07f 1791
86bf665e 1792 for (func_info_map_t::iterator i = q->filtered_functions.begin();
7e1279ea
FCE
1793 i != q->filtered_functions.end(); ++i)
1794 {
3e961ba6 1795 if (q->dw.die_has_pc (i->die, addr))
7e1279ea 1796 {
b0ee93c4 1797 if (q->sess.verbose>3)
b530b5b3 1798 clog << _("function DIE lands on srcfile\n");
4cd232e4 1799 if (q->has_statement_str)
f5958c8f
JS
1800 {
1801 Dwarf_Die scope;
1802 q->dw.inner_die_containing_pc(i->die, addr, scope);
1803 query_statement (i->name, i->decl_file,
1804 lineno, // NB: not q->line !
1805 &scope, addr, q);
1806 }
4cd232e4 1807 else
3e961ba6 1808 query_func_info (i->entrypc, *i, q);
7e1279ea 1809 }
20e4a32c
RM
1810 }
1811
86bf665e 1812 for (inline_instance_map_t::iterator i
897820ca
GH
1813 = q->filtered_inlines.begin();
1814 i != q->filtered_inlines.end(); ++i)
1815 {
3e961ba6 1816 if (q->dw.die_has_pc (i->die, addr))
7e1279ea 1817 {
b0ee93c4 1818 if (q->sess.verbose>3)
b530b5b3 1819 clog << _("inline instance DIE lands on srcfile\n");
897820ca 1820 if (q->has_statement_str)
f5958c8f
JS
1821 {
1822 Dwarf_Die scope;
1823 q->dw.inner_die_containing_pc(i->die, addr, scope);
1824 query_statement (i->name, i->decl_file,
d807e0ab
JS
1825 lineno, // NB: not q->line !
1826 &scope, addr, q);
f5958c8f 1827 }
897820ca 1828 else
3e961ba6 1829 query_inline_instance_info (*i, q);
897820ca 1830 }
20e4a32c 1831 }
7e1279ea
FCE
1832}
1833
1834
7fdd3e2c
JS
1835bool
1836inline_instance_info::operator<(const inline_instance_info& other) const
1837{
1838 if (entrypc != other.entrypc)
1839 return entrypc < other.entrypc;
1840
1841 if (decl_line != other.decl_line)
1842 return decl_line < other.decl_line;
1843
1844 int cmp = name.compare(other.name);
c60517ca
FCE
1845
1846 if (!cmp)
1847 {
1848 assert (decl_file);
1849 assert (other.decl_file);
1850 cmp = strcmp(decl_file, other.decl_file);
1851 }
1852
7fdd3e2c
JS
1853 return cmp < 0;
1854}
1855
1856
4fa7b22b 1857static int
3956fccb 1858query_dwarf_inline_instance (Dwarf_Die * die, dwarf_query * q)
4fa7b22b 1859{
6b517475 1860 assert (q->has_statement_str || q->has_function_str);
4bda987e 1861 assert (!q->has_call && !q->has_return && !q->has_exported);
bd2b1e68 1862
39bcd429 1863 try
7a053d3b 1864 {
b0ee93c4 1865 if (q->sess.verbose>2)
b530b5b3 1866 clog << _F("selected inline instance of %s\n", q->dw.function_name.c_str());
7e1279ea 1867
6b517475
JS
1868 Dwarf_Addr entrypc;
1869 if (q->dw.die_entrypc (die, &entrypc))
1870 {
1871 inline_instance_info inl;
1872 inl.die = *die;
1873 inl.name = q->dw.function_name;
1874 inl.entrypc = entrypc;
1875 q->dw.function_file (&inl.decl_file);
1876 q->dw.function_line (&inl.decl_line);
1877
1878 // make sure that this inline hasn't already
1879 // been matched from a different CU
1880 if (q->inline_dupes.insert(inl).second)
1881 q->filtered_inlines.push_back(inl);
1882 }
7e1279ea
FCE
1883 return DWARF_CB_OK;
1884 }
1885 catch (const semantic_error& e)
1886 {
1887 q->sess.print_error (e);
1888 return DWARF_CB_ABORT;
1889 }
1890}
bb788f9f 1891
7e1279ea 1892static int
7d007451 1893query_dwarf_func (Dwarf_Die * func, dwarf_query * q)
7e1279ea 1894{
6b517475 1895 assert (q->has_statement_str || q->has_function_str);
bb788f9f 1896
bd25380d
JS
1897 // weed out functions whose decl_file isn't one of
1898 // the source files that we actually care about
6b517475 1899 if (q->spec_type != function_alone &&
bd25380d 1900 q->filtered_srcfiles.count(dwarf_decl_file(func)?:"") == 0)
8096dd7d 1901 return DWARF_CB_OK;
bd25380d 1902
7e1279ea
FCE
1903 try
1904 {
7e1279ea
FCE
1905 q->dw.focus_on_function (func);
1906
7d6d0afc
JS
1907 if (!q->dw.function_scope_matches(q->scopes))
1908 return DWARF_CB_OK;
1909
857bdfd1
JS
1910 // make sure that this function address hasn't
1911 // already been matched under an aliased name
1912 Dwarf_Addr addr;
1913 if (!q->dw.func_is_inline() &&
1914 dwarf_entrypc(func, &addr) == 0 &&
1915 !q->alias_dupes.insert(addr).second)
1916 return DWARF_CB_OK;
1917
4bda987e 1918 if (q->dw.func_is_inline () && (! q->has_call) && (! q->has_return) && (! q->has_exported))
7e1279ea 1919 {
4bda987e 1920 if (q->sess.verbose>3)
b530b5b3 1921 clog << _F("checking instances of inline %s\n", q->dw.function_name.c_str());
4bda987e 1922 q->dw.iterate_over_inline_instances (query_dwarf_inline_instance, q);
7e1279ea 1923 }
8f14e444
FCE
1924 else if (q->dw.func_is_inline () && (q->has_return)) // PR 11553
1925 {
1926 q->inlined_non_returnable.insert (q->dw.function_name);
1927 }
396afcee 1928 else if (!q->dw.func_is_inline () && (! q->has_inline))
20e4a32c 1929 {
4bda987e
SC
1930 if (q->has_exported && !q->dw.func_is_exported ())
1931 return DWARF_CB_OK;
6b517475 1932 if (q->sess.verbose>2)
b530b5b3 1933 clog << _F("selected function %s\n", q->dw.function_name.c_str());
6b517475
JS
1934
1935 func_info func;
1936 q->dw.function_die (&func.die);
1937 func.name = q->dw.function_name;
1938 q->dw.function_file (&func.decl_file);
1939 q->dw.function_line (&func.decl_line);
1940
1941 Dwarf_Addr entrypc;
1942 if (q->dw.function_entrypc (&entrypc))
1943 {
1944 func.entrypc = entrypc;
1945 q->filtered_functions.push_back (func);
1946 }
1947 /* else this function is fully inlined, just ignore it */
7e1279ea 1948 }
39bcd429 1949 return DWARF_CB_OK;
bd2b1e68 1950 }
39bcd429 1951 catch (const semantic_error& e)
bd2b1e68 1952 {
39bcd429
FCE
1953 q->sess.print_error (e);
1954 return DWARF_CB_ABORT;
bd2b1e68 1955 }
bd2b1e68
GH
1956}
1957
1958static int
5c378838 1959query_cu (Dwarf_Die * cudie, dwarf_query * q)
bd2b1e68 1960{
6b517475
JS
1961 assert (q->has_statement_str || q->has_function_str);
1962
85007c04 1963 if (pending_interrupts) return DWARF_CB_ABORT;
7a053d3b 1964
39bcd429 1965 try
bd2b1e68 1966 {
7e1279ea 1967 q->dw.focus_on_cu (cudie);
b5d77020 1968
b0ee93c4 1969 if (false && q->sess.verbose>2)
b530b5b3
LB
1970 clog << _F("focused on CU '%s', in module '%s'\n",
1971 q->dw.cu_name().c_str(), q->dw.module_name.c_str());
d9b516ca 1972
6b517475
JS
1973 q->filtered_srcfiles.clear();
1974 q->filtered_functions.clear();
1975 q->filtered_inlines.clear();
1976
1977 // In this path, we find "abstract functions", record
1978 // information about them, and then (depending on lineno
1979 // matching) possibly emit one or more of the function's
1980 // associated addresses. Unfortunately the control of this
1981 // cannot easily be turned inside out.
1982
1983 if (q->spec_type != function_alone)
39bcd429 1984 {
6b517475
JS
1985 // If we have a pattern string with a filename, we need
1986 // to elaborate the srcfile mask in question first.
1987 q->dw.collect_srcfiles_matching (q->file, q->filtered_srcfiles);
1988
1989 // If we have a file pattern and *no* srcfile matches, there's
1990 // no need to look further into this CU, so skip.
1991 if (q->filtered_srcfiles.empty())
1992 return DWARF_CB_OK;
1993 }
e4c58386 1994
6b517475
JS
1995 // Pick up [entrypc, name, DIE] tuples for all the functions
1996 // matching the query, and fill in the prologue endings of them
1997 // all in a single pass.
5898b6e1 1998 int rc = q->dw.iterate_over_functions (query_dwarf_func, q, q->function);
6b517475
JS
1999 if (rc != DWARF_CB_OK)
2000 q->query_done = true;
2001
277a2b96
JL
2002 if (!q->filtered_functions.empty() &&
2003 !q->has_statement_str && // PR 2608
2004 (q->sess.prologue_searching ||
2005 (q->has_process && !q->dw.has_valid_locs()))) // PR 6871 && PR 6941
2006 q->dw.resolve_prologue_endings (q->filtered_functions);
6b517475
JS
2007
2008 if (q->spec_type == function_file_and_line)
2009 {
58b070fb 2010 // .statement(...:NN) often gets mixed up with .function(...:NN)
2713ea24 2011 if (q->has_function_str)
b530b5b3 2012 q->sess.print_warning (_("For probing a particular line, use a "
b78a0fbb 2013 ".statement() probe, not .function()"),
af2e341f 2014 q->base_probe->tok);
58b070fb 2015
6b517475
JS
2016 // If we have a pattern string with target *line*, we
2017 // have to look at lines in all the matched srcfiles.
0e2e28a1 2018 void (* callback) (const dwarf_line_t&, dwarf_query*) =
6b517475
JS
2019 q->has_label ? query_srcfile_label : query_srcfile_line;
2020 for (set<string>::const_iterator i = q->filtered_srcfiles.begin();
2021 i != q->filtered_srcfiles.end(); ++i)
2022 q->dw.iterate_over_srcfile_lines (i->c_str(), q->line, q->has_statement_str,
2023 q->line_type, callback, q->function, q);
2024 }
2025 else if (q->has_label)
2026 {
2027 for (func_info_map_t::iterator i = q->filtered_functions.begin();
2028 i != q->filtered_functions.end(); ++i)
2029 q->dw.iterate_over_labels (&i->die, q->label_val, i->name,
2030 q, query_label);
2031
2032 for (inline_instance_map_t::iterator i = q->filtered_inlines.begin();
2033 i != q->filtered_inlines.end(); ++i)
2034 q->dw.iterate_over_labels (&i->die, q->label_val, i->name,
2035 q, query_label);
39bcd429 2036 }
67959c62
JL
2037 else if (q->has_callee || q->has_callees_num)
2038 {
2039 // .callee(str) --> str, .callees[(N)] --> "*"
2040 string callee_val = q->has_callee ? q->callee_val : "*";
2041 long callees_num_val = q->has_callees_num ? q->callees_num_val : 1;
2042
2043 // NB: We filter functions that do not match the file here rather than
2044 // in query_callee because we only want the filtering to apply to the
2045 // first level, not to callees that are recursed into if
2046 // callees_num_val > 1.
2047 for (func_info_map_t::iterator i = q->filtered_functions.begin();
2048 i != q->filtered_functions.end(); ++i)
2049 {
2050 if (q->spec_type != function_alone &&
2051 q->filtered_srcfiles.count(i->decl_file) == 0)
2052 continue;
2053 q->dw.iterate_over_callees (&i->die, callee_val,
2054 callees_num_val,
2055 q, query_callee);
2056 }
2057
2058 for (inline_instance_map_t::iterator i = q->filtered_inlines.begin();
2059 i != q->filtered_inlines.end(); ++i)
2060 {
2061 if (q->spec_type != function_alone &&
2062 q->filtered_srcfiles.count(i->decl_file) == 0)
2063 continue;
2064 q->dw.iterate_over_callees (&i->die, callee_val,
2065 callees_num_val,
2066 q, query_callee);
2067 }
2068 }
6b517475
JS
2069 else
2070 {
2071 // Otherwise, simply probe all resolved functions.
2072 for (func_info_map_t::iterator i = q->filtered_functions.begin();
2073 i != q->filtered_functions.end(); ++i)
2074 query_func_info (i->entrypc, *i, q);
2075
2076 // And all inline instances (if we're not excluding inlines with ".call")
2077 if (! q->has_call)
2078 for (inline_instance_map_t::iterator i
2079 = q->filtered_inlines.begin(); i != q->filtered_inlines.end(); ++i)
2080 query_inline_instance_info (*i, q);
2081 }
39bcd429 2082 return DWARF_CB_OK;
bd2b1e68 2083 }
39bcd429 2084 catch (const semantic_error& e)
bd2b1e68 2085 {
39bcd429
FCE
2086 q->sess.print_error (e);
2087 return DWARF_CB_ABORT;
bd2b1e68 2088 }
bd2b1e68
GH
2089}
2090
0ce64fb8 2091
4df79aaf
JS
2092void
2093dwarf_query::query_module_functions ()
2094{
2095 try
2096 {
2097 filtered_srcfiles.clear();
2098 filtered_functions.clear();
2099 filtered_inlines.clear();
2100
2101 // Collect all module functions so we know which CUs are interesting
2102 int rc = dw.iterate_single_function(query_dwarf_func, this, function);
2103 if (rc != DWARF_CB_OK)
2104 {
2105 query_done = true;
2106 return;
2107 }
2108
2109 set<void*> used_cus; // by cu->addr
2110 vector<Dwarf_Die> cus;
2111 Dwarf_Die cu_mem;
2112
2113 for (func_info_map_t::iterator i = filtered_functions.begin();
2114 i != filtered_functions.end(); ++i)
2115 if (dwarf_diecu(&i->die, &cu_mem, NULL, NULL) &&
2116 used_cus.insert(cu_mem.addr).second)
2117 cus.push_back(cu_mem);
2118
2119 for (inline_instance_map_t::iterator i = filtered_inlines.begin();
2120 i != filtered_inlines.end(); ++i)
2121 if (dwarf_diecu(&i->die, &cu_mem, NULL, NULL) &&
2122 used_cus.insert(cu_mem.addr).second)
2123 cus.push_back(cu_mem);
2124
2125 // Reset the dupes since we didn't actually collect them the first time
2126 alias_dupes.clear();
2127 inline_dupes.clear();
2128
2129 // Run the query again on the individual CUs
91bb9081
LB
2130 for (vector<Dwarf_Die>::iterator i = cus.begin(); i != cus.end(); ++i){
2131 rc = query_cu(&*i, this);
2132 if (rc != DWARF_CB_OK)
2133 {
2134 query_done = true;
2135 return;
2136 }
2137 }
4df79aaf
JS
2138 }
2139 catch (const semantic_error& e)
2140 {
2141 sess.print_error (e);
2142 }
2143}
2144
2145
5f0a03a6
JK
2146static void
2147validate_module_elf (Dwfl_Module *mod, const char *name, base_query *q)
2148{
2149 // Validate the machine code in this elf file against the
2150 // session machine. This is important, in case the wrong kind
2151 // of debuginfo is being automagically processed by elfutils.
2152 // While we can tell i686 apart from x86-64, unfortunately
2153 // we can't help confusing i586 vs i686 (both EM_386).
2154
2155 Dwarf_Addr bias;
2156 // We prefer dwfl_module_getdwarf to dwfl_module_getelf here,
2157 // because dwfl_module_getelf can force costly section relocations
2158 // we don't really need, while either will do for this purpose.
2159 Elf* elf = (dwarf_getelf (dwfl_module_getdwarf (mod, &bias))
2160 ?: dwfl_module_getelf (mod, &bias));
2161
2162 GElf_Ehdr ehdr_mem;
2163 GElf_Ehdr* em = gelf_getehdr (elf, &ehdr_mem);
86bf665e 2164 if (em == 0) { dwfl_assert ("dwfl_getehdr", dwfl_errno()); }
d8f31d0a 2165 assert(em);
5f0a03a6
JK
2166 int elf_machine = em->e_machine;
2167 const char* debug_filename = "";
2168 const char* main_filename = "";
2169 (void) dwfl_module_info (mod, NULL, NULL,
2170 NULL, NULL, NULL,
2171 & main_filename,
2172 & debug_filename);
2173 const string& sess_machine = q->sess.architecture;
756c9462
FCE
2174
2175 string expect_machine; // to match sess.machine (i.e., kernel machine)
2176 string expect_machine2;
5f0a03a6 2177
d27e6fd5 2178 // NB: See also the 'uname -m' squashing done in main.cxx.
5f0a03a6
JK
2179 switch (elf_machine)
2180 {
756c9462
FCE
2181 // x86 and ppc are bi-architecture; a 64-bit kernel
2182 // can normally run either 32-bit or 64-bit *userspace*.
2183 case EM_386:
2184 expect_machine = "i?86";
2185 if (! q->has_process) break; // 32-bit kernel/module
2186 /* FALLSTHROUGH */
2187 case EM_X86_64:
2188 expect_machine2 = "x86_64";
2189 break;
2190 case EM_PPC:
756c9462 2191 case EM_PPC64:
5a1c472e 2192 expect_machine = "powerpc";
756c9462 2193 break;
3fe7d888 2194 case EM_S390: expect_machine = "s390"; break;
5f0a03a6 2195 case EM_IA_64: expect_machine = "ia64"; break;
d27e6fd5 2196 case EM_ARM: expect_machine = "arm*"; break;
86aae990 2197 case EM_AARCH64: expect_machine = "arm64"; break;
5f0a03a6
JK
2198 // XXX: fill in some more of these
2199 default: expect_machine = "?"; break;
2200 }
2201
2202 if (! debug_filename) debug_filename = main_filename;
2203 if (! debug_filename) debug_filename = name;
2204
756c9462
FCE
2205 if (fnmatch (expect_machine.c_str(), sess_machine.c_str(), 0) != 0 &&
2206 fnmatch (expect_machine2.c_str(), sess_machine.c_str(), 0) != 0)
5f0a03a6
JK
2207 {
2208 stringstream msg;
b530b5b3
LB
2209 msg << _F("ELF machine %s|%s (code %d) mismatch with target %s in '%s'",
2210 expect_machine.c_str(), expect_machine2.c_str(), elf_machine,
2211 sess_machine.c_str(), debug_filename);
dc09353a 2212 throw SEMANTIC_ERROR(msg.str ());
5f0a03a6
JK
2213 }
2214
b57082de 2215 if (q->sess.verbose>1)
b78a0fbb 2216 clog << _F("focused on module '%s' = [%#" PRIx64 "-%#" PRIx64 ", bias %#" PRIx64
b530b5b3
LB
2217 " file %s ELF machine %s|%s (code %d)\n",
2218 q->dw.module_name.c_str(), q->dw.module_start, q->dw.module_end,
2219 q->dw.module_bias, debug_filename, expect_machine.c_str(),
2220 expect_machine2.c_str(), elf_machine);
5f0a03a6 2221}
1d3a40b6 2222
91af0778
FCE
2223
2224
2225static Dwarf_Addr
2226lookup_symbol_address (Dwfl_Module *m, const char* wanted)
2227{
2228 int syments = dwfl_module_getsymtab(m);
2229 assert(syments);
2230 for (int i = 1; i < syments; ++i)
2231 {
2232 GElf_Sym sym;
2233 const char *name = dwfl_module_getsym(m, i, &sym, NULL);
2234 if (name != NULL && strcmp(name, wanted) == 0)
2235 return sym.st_value;
2236 }
2237
2238 return 0;
2239}
2240
2241
2242
bd2b1e68 2243static int
b8da0ad1 2244query_module (Dwfl_Module *mod,
91af0778 2245 void **,
b8da0ad1 2246 const char *name,
6f4c1275 2247 Dwarf_Addr addr,
06de3a04 2248 base_query *q)
bd2b1e68 2249{
39bcd429 2250 try
e38d6504 2251 {
91af0778
FCE
2252 module_info* mi = q->sess.module_cache->cache[name];
2253 if (mi == 0)
2254 {
2255 mi = q->sess.module_cache->cache[name] = new module_info(name);
2256
6f4c1275
FCE
2257 mi->mod = mod;
2258 mi->addr = addr;
91af0778 2259
6f4c1275
FCE
2260 const char* debug_filename = "";
2261 const char* main_filename = "";
2262 (void) dwfl_module_info (mod, NULL, NULL,
2263 NULL, NULL, NULL,
2264 & main_filename,
2265 & debug_filename);
2266
ab3ed72d 2267 if (debug_filename || main_filename)
91af0778 2268 {
6f4c1275
FCE
2269 mi->elf_path = debug_filename ?: main_filename;
2270 }
2271 else if (name == TOK_KERNEL)
2272 {
2273 mi->dwarf_status = info_absent;
91af0778 2274 }
91af0778
FCE
2275 }
2276 // OK, enough of that module_info caching business.
2277
5f0a03a6 2278 q->dw.focus_on_module(mod, mi);
d9b516ca 2279
39bcd429
FCE
2280 // If we have enough information in the pattern to skip a module and
2281 // the module does not match that information, return early.
b8da0ad1 2282 if (!q->dw.module_name_matches(q->module_val))
85007c04 2283 return pending_interrupts ? DWARF_CB_ABORT : DWARF_CB_OK;
0cbbf9d1
FCE
2284
2285 // Don't allow module("*kernel*") type expressions to match the
2286 // elfutils module "kernel", which we refer to in the probe
2287 // point syntax exclusively as "kernel.*".
2288 if (q->dw.module_name == TOK_KERNEL && ! q->has_kernel)
85007c04 2289 return pending_interrupts ? DWARF_CB_ABORT : DWARF_CB_OK;
b5d77020 2290
5f0a03a6
JK
2291 if (mod)
2292 validate_module_elf(mod, name, q);
2293 else
91af0778
FCE
2294 assert(q->has_kernel); // and no vmlinux to examine
2295
2296 if (q->sess.verbose>2)
b530b5b3 2297 cerr << _F("focused on module '%s'\n", q->dw.module_name.c_str());
91af0778
FCE
2298
2299
2300 // Collect a few kernel addresses. XXX: these belong better
2301 // to the sess.module_info["kernel"] struct.
2302 if (q->dw.module_name == TOK_KERNEL)
c931ec8a 2303 {
91af0778
FCE
2304 if (! q->sess.sym_kprobes_text_start)
2305 q->sess.sym_kprobes_text_start = lookup_symbol_address (mod, "__kprobes_text_start");
2306 if (! q->sess.sym_kprobes_text_end)
2307 q->sess.sym_kprobes_text_end = lookup_symbol_address (mod, "__kprobes_text_end");
2308 if (! q->sess.sym_stext)
2309 q->sess.sym_stext = lookup_symbol_address (mod, "_stext");
c931ec8a
FCE
2310 }
2311
47e226ed
SC
2312 // We either have a wildcard or an unresolved library
2313 if (q->has_library && (contains_glob_chars (q->path)
2314 || q->path.find('/') == string::npos))
84c84ac4
SC
2315 // handle .library(GLOB)
2316 q->dw.iterate_over_libraries (&q->query_library_callback, q);
576eaefe
SC
2317 // .plt is translated to .plt.statement(N). We only want to iterate for the
2318 // .plt case
2319 else if (q->has_plt && ! q->has_statement)
070764c0
JL
2320 {
2321 q->dw.iterate_over_plt (q, &q->query_plt_callback);
2322 q->visited_modules.insert(name);
2323 }
84c84ac4 2324 else
070764c0
JL
2325 {
2326 // search the module for matches of the probe point.
2327 q->handle_query_module();
2328 q->visited_modules.insert(name);
2329 }
bb788f9f 2330
b8da0ad1 2331 // If we know that there will be no more matches, abort early.
85007c04 2332 if (q->dw.module_name_final_match(q->module_val) || pending_interrupts)
b8da0ad1
FCE
2333 return DWARF_CB_ABORT;
2334 else
2335 return DWARF_CB_OK;
7a053d3b 2336 }
39bcd429 2337 catch (const semantic_error& e)
bd2b1e68 2338 {
39bcd429
FCE
2339 q->sess.print_error (e);
2340 return DWARF_CB_ABORT;
bd2b1e68 2341 }
bd2b1e68
GH
2342}
2343
35d4ab18 2344
84c84ac4 2345void
bbbc7241 2346base_query::query_library_callback (base_query *me, const char *data)
84c84ac4 2347{
5d5bd369 2348 me->query_library (data);
84c84ac4
SC
2349}
2350
2351
2cbcfa9c 2352string
51d6bda3
SC
2353query_one_library (const char *library, dwflpp & dw,
2354 const string user_lib, probe * base_probe, probe_point *base_loc,
2355 vector<derived_probe *> & results)
84c84ac4 2356{
47e226ed 2357 if (dw.function_name_matches_pattern(library, "*" + user_lib))
84c84ac4 2358 {
05fb3e0c
WF
2359 string library_path = find_executable (library, "", dw.sess.sysenv,
2360 "LD_LIBRARY_PATH");
84c84ac4
SC
2361 probe_point* specific_loc = new probe_point(*base_loc);
2362 specific_loc->optional = true;
ef0943df 2363 specific_loc->from_glob = true;
84c84ac4
SC
2364 vector<probe_point::component*> derived_comps;
2365
2366 vector<probe_point::component*>::iterator it;
2367 for (it = specific_loc->components.begin();
2368 it != specific_loc->components.end(); ++it)
2369 if ((*it)->functor == TOK_LIBRARY)
2370 derived_comps.push_back(new probe_point::component(TOK_LIBRARY,
2371 new literal_string(library_path)));
2372 else
2373 derived_comps.push_back(*it);
2374 probe_point* derived_loc = new probe_point(*specific_loc);
2375 derived_loc->components = derived_comps;
8159bf55 2376 probe *new_base = new probe (new probe (base_probe, specific_loc), derived_loc);
51d6bda3
SC
2377 derive_probes(dw.sess, new_base, results);
2378 if (dw.sess.verbose > 2)
2cbcfa9c
JL
2379 clog << _("module=") << library_path << endl;
2380 return library_path;
84c84ac4 2381 }
2cbcfa9c 2382 return "";
84c84ac4
SC
2383}
2384
2385
51d6bda3
SC
2386void
2387dwarf_query::query_library (const char *library)
2388{
2cbcfa9c
JL
2389 string library_path =
2390 query_one_library (library, dw, user_lib, base_probe, base_loc, results);
2391 if (!library_path.empty())
2392 visited_modules.insert(library_path);
51d6bda3
SC
2393}
2394
576eaefe
SC
2395struct plt_expanding_visitor: public var_expanding_visitor
2396{
2397 plt_expanding_visitor(const string & entry):
2398 entry (entry)
2399 {
2400 }
2401 const string & entry;
2402
2403 void visit_target_symbol (target_symbol* e);
2404};
2405
2406
2407void
45cdb40e 2408base_query::query_plt_callback (base_query *me, const char *entry, size_t address)
576eaefe 2409{
576eaefe
SC
2410 if (me->dw.function_name_matches_pattern (entry, me->plt_val))
2411 me->query_plt (entry, address);
7f4964f1 2412 me->dw.mod_info->plt_funcs.insert(entry);
576eaefe
SC
2413}
2414
2415
2416void
2417query_one_plt (const char *entry, long addr, dwflpp & dw,
2418 probe * base_probe, probe_point *base_loc,
2419 vector<derived_probe *> & results)
2420{
2421 probe_point* specific_loc = new probe_point(*base_loc);
576eaefe
SC
2422 vector<probe_point::component*> derived_comps;
2423
2424 if (dw.sess.verbose > 2)
2425 clog << _F("plt entry=%s\n", entry);
2426
576eaefe
SC
2427 vector<probe_point::component*>::iterator it;
2428 for (it = specific_loc->components.begin();
2429 it != specific_loc->components.end(); ++it)
2430 if ((*it)->functor == TOK_PLT)
3d372d6b 2431 {
8159bf55
FCE
2432 *it = new probe_point::component(TOK_PLT,
2433 new literal_string(entry));
3d372d6b
SC
2434 derived_comps.push_back(*it);
2435 derived_comps.push_back(new probe_point::component(TOK_STATEMENT,
ac7c8669 2436 new literal_number(addr, true)));
3d372d6b 2437 }
576eaefe
SC
2438 else
2439 derived_comps.push_back(*it);
2440 probe_point* derived_loc = new probe_point(*specific_loc);
2441 derived_loc->components = derived_comps;
02c34e38
FCE
2442 probe *new_base = new probe (new probe (base_probe, specific_loc),
2443 derived_loc);
576eaefe
SC
2444 string e = string(entry);
2445 plt_expanding_visitor pltv (e);
2446 pltv.replace (new_base->body);
2447 derive_probes(dw.sess, new_base, results);
2448}
2449
2450
2451void
2452dwarf_query::query_plt (const char *entry, size_t address)
2453{
2454 query_one_plt (entry, address, dw, base_probe, base_loc, results);
2455}
51d6bda3 2456
435f53a7
FCE
2457// This would more naturally fit into elaborate.cxx:semantic_pass_symbols,
2458// but the needed declaration for module_cache is not available there.
2459// Nor for that matter in session.cxx. Only in this CU is that field ever
2460// set (in query_module() above), so we clean it up here too.
2461static void
2462delete_session_module_cache (systemtap_session& s)
2463{
2464 if (s.module_cache) {
2465 if (s.verbose > 3)
b530b5b3 2466 clog << _("deleting module_cache") << endl;
435f53a7
FCE
2467 delete s.module_cache;
2468 s.module_cache = 0;
2469 }
2470}
2471
2472
de688825 2473struct dwarf_var_expanding_visitor: public var_expanding_visitor
35d4ab18 2474{
77de5e9e 2475 dwarf_query & q;
bcc12710 2476 Dwarf_Die *scope_die;
77de5e9e 2477 Dwarf_Addr addr;
8c819921 2478 block *add_block;
2260f4e3 2479 block *add_call_probe; // synthesized from .return probes with saved $vars
1630966a
JS
2480 // NB: tids are not always collected in add_block & add_call_probe, because
2481 // gen_kretprobe_saved_return doesn't need them. Thus we need these extra
2482 // *_tid bools for gen_mapped_saved_return to tell what's there.
8cc799a5 2483 bool add_block_tid, add_call_probe_tid;
af234c40
JS
2484 unsigned saved_longs, saved_strings; // data saved within kretprobes
2485 map<std::string, expression *> return_ts_map;
729455a7 2486 vector<Dwarf_Die> scopes;
3689db05 2487 // probe counter name -> pointer of associated probe
698de6cc 2488 std::set<derived_probe*> perf_counter_refs;
b95e2b79 2489 bool visited;
77de5e9e 2490
de688825 2491 dwarf_var_expanding_visitor(dwarf_query & q, Dwarf_Die *sd, Dwarf_Addr a):
af234c40 2492 q(q), scope_die(sd), addr(a), add_block(NULL), add_call_probe(NULL),
8cc799a5 2493 add_block_tid(false), add_call_probe_tid(false),
af234c40 2494 saved_longs(0), saved_strings(0), visited(false) {}
277c21bc 2495 expression* gen_mapped_saved_return(expression* e, const string& name);
140be17a 2496 expression* gen_kretprobe_saved_return(expression* e);
a7999c82
JS
2497 void visit_target_symbol_saved_return (target_symbol* e);
2498 void visit_target_symbol_context (target_symbol* e);
d7f3e0c5 2499 void visit_target_symbol (target_symbol* e);
bd1fcbad 2500 void visit_atvar_op (atvar_op* e);
c24447be 2501 void visit_cast_op (cast_op* e);
8cc799a5 2502 void visit_entry_op (entry_op* e);
3689db05 2503 void visit_perf_op (perf_op* e);
729455a7
JS
2504private:
2505 vector<Dwarf_Die>& getscopes(target_symbol *e);
77de5e9e
GH
2506};
2507
2508
de688825 2509unsigned var_expanding_visitor::tick = 0;
77de5e9e 2510
a50de939 2511
74fe61bc 2512var_expanding_visitor::var_expanding_visitor (): op()
a50de939
DS
2513{
2514 // FIXME: for the time being, by default we only support plain '$foo
2515 // = bar', not '+=' or any other op= variant. This is fixable, but a
2516 // bit ugly.
2517 //
2518 // If derived classes desire to add additional operator support, add
2519 // new operators to this list in the derived class constructor.
2520 valid_ops.insert ("=");
2521}
2522
2523
87214add
JS
2524bool
2525var_expanding_visitor::rewrite_lvalue(const token* tok, const std::string& eop,
2526 expression*& lvalue, expression*& rvalue)
77de5e9e 2527{
e57b735a
GH
2528 // Our job would normally be to require() the left and right sides
2529 // into a new assignment. What we're doing is slightly trickier:
2530 // we're pushing a functioncall** onto a stack, and if our left
2531 // child sets the functioncall* for that value, we're going to
2532 // assume our left child was a target symbol -- transformed into a
2533 // set_target_foo(value) call, and it wants to take our right child
2534 // as the argument "value".
2535 //
2536 // This is why some people claim that languages with
2537 // constructor-decomposing case expressions have a leg up on
2538 // visitors.
2539
2540 functioncall *fcall = NULL;
d9b516ca 2541
a50de939 2542 // Let visit_target_symbol know what operator it should handle.
87214add
JS
2543 const string* old_op = op;
2544 op = &eop;
a50de939 2545
e57b735a 2546 target_symbol_setter_functioncalls.push (&fcall);
87214add 2547 replace (lvalue);
e57b735a 2548 target_symbol_setter_functioncalls.pop ();
87214add
JS
2549 replace (rvalue);
2550
2551 op = old_op;
e57b735a
GH
2552
2553 if (fcall != NULL)
77de5e9e 2554 {
e57b735a
GH
2555 // Our left child is informing us that it was a target variable
2556 // and it has been replaced with a set_target_foo() function
2557 // call; we are going to provide that function call -- with the
2558 // right child spliced in as sole argument -- in place of
de688825 2559 // ourselves, in the var expansion we're in the middle of making.
e57b735a 2560
87214add 2561 if (valid_ops.find (eop) == valid_ops.end ())
a50de939
DS
2562 {
2563 // Build up a list of supported operators.
2564 string ops;
2565 std::set<string>::iterator i;
b530b5b3 2566 int valid_ops_size = 0;
a50de939 2567 for (i = valid_ops.begin(); i != valid_ops.end(); i++)
b530b5b3 2568 {
a50de939 2569 ops += " " + *i + ",";
b530b5b3
LB
2570 valid_ops_size++;
2571 }
a50de939
DS
2572 ops.resize(ops.size() - 1); // chop off the last ','
2573
2574 // Throw the error.
dc09353a 2575 throw SEMANTIC_ERROR (_NF("Only the following assign operator is implemented on target variables: %s",
1e41115c 2576 "Only the following assign operators are implemented on target variables: %s",
52c2652f 2577 valid_ops_size, ops.c_str()), tok);
b530b5b3 2578
a50de939 2579 }
e57b735a 2580
87214add
JS
2581 assert (lvalue == fcall);
2582 if (rvalue)
2583 fcall->args.push_back (rvalue);
4ed05b15 2584 provide (fcall);
87214add 2585 return true;
77de5e9e 2586 }
e57b735a 2587 else
87214add
JS
2588 return false;
2589}
2590
2591
2592void
2593var_expanding_visitor::visit_assignment (assignment* e)
2594{
2595 if (!rewrite_lvalue (e->tok, e->op, e->left, e->right))
2596 provide (e);
2597}
2598
2599
2600void
2601var_expanding_visitor::visit_pre_crement (pre_crement* e)
2602{
2603 expression *dummy = NULL;
2604 if (!rewrite_lvalue (e->tok, e->op, e->operand, dummy))
2605 provide (e);
2606}
2607
2608
2609void
2610var_expanding_visitor::visit_post_crement (post_crement* e)
2611{
2612 expression *dummy = NULL;
2613 if (!rewrite_lvalue (e->tok, e->op, e->operand, dummy))
2614 provide (e);
2615}
2616
2617
2618void
2619var_expanding_visitor::visit_delete_statement (delete_statement* s)
2620{
2621 string fakeop = "delete";
2622 expression *dummy = NULL;
2623 if (!rewrite_lvalue (s->tok, fakeop, s->value, dummy))
2624 provide (s);
e57b735a 2625}
d9b516ca 2626
d7f3e0c5 2627
30263a73
FCE
2628void
2629var_expanding_visitor::visit_defined_op (defined_op* e)
2630{
2631 bool resolved = true;
2632
2633 defined_ops.push (e);
2634 try {
2635 // NB: provide<>/require<> are NOT typesafe. So even though a defined_op is
2636 // defined with a target_symbol* operand, a subsidiary call may attempt to
2637 // rewrite it to a general expression* instead, and require<> happily
2638 // casts to/from void*, causing possible memory corruption. We use
2639 // expression* here, being the general case of rewritten $variable.
2640 expression *foo1 = e->operand;
2641 foo1 = require (foo1);
2642
c69a87e0 2643 // NB: Formerly, we had some curious cases to consider here, depending on what
30263a73 2644 // various visit_target_symbol() implementations do for successful or
c69a87e0
FCE
2645 // erroneous resolutions. Some would signal a visit_target_symbol failure
2646 // with an exception, with a set flag within the target_symbol, or nothing
2647 // at all.
30263a73 2648 //
c69a87e0
FCE
2649 // Now, failures always have to be signalled with a
2650 // saved_conversion_error being chained to the target_symbol.
2651 // Successes have to result in an attempted rewrite of the
850bfddd 2652 // target_symbol (via provide()).
780f11ff 2653 //
c69a87e0
FCE
2654 // Edna Mode: "no capes". fche: "no exceptions".
2655
30263a73
FCE
2656 // dwarf stuff: success: rewrites to a function; failure: retains target_symbol, sets saved_conversion_error
2657 //
2658 // sdt-kprobes sdt.h: success: string or functioncall; failure: semantic_error
2659 //
2660 // sdt-uprobes: success: string or no op; failure: no op; expect derived/synthetic
2661 // dwarf probe to take care of it.
2662 // But this is rather unhelpful. So we rig the sdt_var_expanding_visitor
2663 // to pass through @defined() to the synthetic dwarf probe.
780f11ff 2664 //
30263a73
FCE
2665 // utrace: success: rewrites to function; failure: semantic_error
2666 //
850bfddd 2667 // procfs: success: rewrites to function; failure: semantic_error
30263a73
FCE
2668
2669 target_symbol* foo2 = dynamic_cast<target_symbol*> (foo1);
c69a87e0 2670 if (foo2 && foo2->saved_conversion_error) // failing
30263a73 2671 resolved = false;
a45664f4 2672 else if (foo2) // unresolved but not marked failing
b7aedf26 2673 {
780f11ff
JS
2674 // There are some visitors that won't touch certain target_symbols,
2675 // e.g. dwarf_var_expanding_visitor won't resolve @cast. We should
2676 // leave it for now so some other visitor can have a chance.
b7aedf26
JS
2677 e->operand = foo2;
2678 provide (e);
2679 return;
2680 }
30263a73
FCE
2681 else // resolved, rewritten to some other expression type
2682 resolved = true;
780f11ff 2683 } catch (const semantic_error& e) {
c69a87e0 2684 assert (0); // should not happen
30263a73
FCE
2685 }
2686 defined_ops.pop ();
2687
2688 literal_number* ln = new literal_number (resolved ? 1 : 0);
2689 ln->tok = e->tok;
2690 provide (ln);
2691}
2692
2693
5f36109e
JS
2694struct dwarf_pretty_print
2695{
2696 dwarf_pretty_print (dwflpp& dw, vector<Dwarf_Die>& scopes, Dwarf_Addr pc,
2697 const string& local, bool userspace_p,
2698 const target_symbol& e):
d19a9a82
JS
2699 dw(dw), local(local), scopes(scopes), pc(pc), pointer(NULL),
2700 userspace_p(userspace_p), deref_p(true)
5f36109e
JS
2701 {
2702 init_ts (e);
2703 dw.type_die_for_local (scopes, pc, local, ts, &base_type);
2704 }
2705
2706 dwarf_pretty_print (dwflpp& dw, Dwarf_Die *scope_die, Dwarf_Addr pc,
2707 bool userspace_p, const target_symbol& e):
d19a9a82
JS
2708 dw(dw), scopes(1, *scope_die), pc(pc), pointer(NULL),
2709 userspace_p(userspace_p), deref_p(true)
5f36109e
JS
2710 {
2711 init_ts (e);
2712 dw.type_die_for_return (&scopes[0], pc, ts, &base_type);
2713 }
2714
2715 dwarf_pretty_print (dwflpp& dw, Dwarf_Die *type_die, expression* pointer,
d19a9a82 2716 bool deref_p, bool userspace_p, const target_symbol& e):
5f36109e 2717 dw(dw), pc(0), pointer(pointer), pointer_type(*type_die),
d19a9a82 2718 userspace_p(userspace_p), deref_p(deref_p)
5f36109e
JS
2719 {
2720 init_ts (e);
2721 dw.type_die_for_pointer (type_die, ts, &base_type);
2722 }
2723
2724 functioncall* expand ();
ce83ff57 2725 ~dwarf_pretty_print () { delete ts; }
5f36109e
JS
2726
2727private:
2728 dwflpp& dw;
2729 target_symbol* ts;
7d11d8c9 2730 bool print_full;
5f36109e
JS
2731 Dwarf_Die base_type;
2732
2733 string local;
2734 vector<Dwarf_Die> scopes;
2735 Dwarf_Addr pc;
2736
2737 expression* pointer;
2738 Dwarf_Die pointer_type;
2739
d19a9a82 2740 const bool userspace_p, deref_p;
5f36109e
JS
2741
2742 void recurse (Dwarf_Die* type, target_symbol* e,
7d11d8c9 2743 print_format* pf, bool top=false);
600551ca
JS
2744 void recurse_bitfield (Dwarf_Die* type, target_symbol* e,
2745 print_format* pf);
5f36109e 2746 void recurse_base (Dwarf_Die* type, target_symbol* e,
7d11d8c9 2747 print_format* pf);
5f36109e 2748 void recurse_array (Dwarf_Die* type, target_symbol* e,
7d11d8c9 2749 print_format* pf, bool top);
5f36109e 2750 void recurse_pointer (Dwarf_Die* type, target_symbol* e,
7d11d8c9 2751 print_format* pf, bool top);
5f36109e 2752 void recurse_struct (Dwarf_Die* type, target_symbol* e,
7d11d8c9 2753 print_format* pf, bool top);
5f36109e 2754 void recurse_struct_members (Dwarf_Die* type, target_symbol* e,
7d11d8c9 2755 print_format* pf, int& count);
bbee5bb8 2756 bool print_chars (Dwarf_Die* type, target_symbol* e, print_format* pf);
5f36109e
JS
2757
2758 void init_ts (const target_symbol& e);
2759 expression* deref (target_symbol* e);
c55ea10d 2760 bool push_deref (print_format* pf, const string& fmt, target_symbol* e);
5f36109e
JS
2761};
2762
2763
2764void
2765dwarf_pretty_print::init_ts (const target_symbol& e)
2766{
2767 // Work with a new target_symbol so we can modify arguments
2768 ts = new target_symbol (e);
2769
2770 if (ts->addressof)
dc09353a 2771 throw SEMANTIC_ERROR(_("cannot take address of pretty-printed variable"), ts->tok);
5f36109e
JS
2772
2773 if (ts->components.empty() ||
2774 ts->components.back().type != target_symbol::comp_pretty_print)
dc09353a 2775 throw SEMANTIC_ERROR(_("invalid target_symbol for pretty-print"), ts->tok);
7d11d8c9 2776 print_full = ts->components.back().member.length() > 1;
5f36109e
JS
2777 ts->components.pop_back();
2778}
2779
2780
2781functioncall*
2782dwarf_pretty_print::expand ()
2783{
2784 static unsigned tick = 0;
2785
2786 // function pretty_print_X([pointer], [arg1, arg2, ...]) {
7d11d8c9
JS
2787 // try {
2788 // return sprintf("{.foo=...}", (ts)->foo, ...)
2789 // } catch {
2790 // return "ERROR"
2791 // }
5f36109e
JS
2792 // }
2793
2794 // Create the function decl and call.
2795
2796 functiondecl *fdecl = new functiondecl;
2797 fdecl->tok = ts->tok;
2798 fdecl->synthetic = true;
2799 fdecl->name = "_dwarf_pretty_print_" + lex_cast(tick++);
2800 fdecl->type = pe_string;
2801
2802 functioncall* fcall = new functioncall;
2803 fcall->tok = ts->tok;
2804 fcall->function = fdecl->name;
140be17a 2805 fcall->type = pe_string;
5f36109e
JS
2806
2807 // If there's a <pointer>, replace it with a new var and make that
2808 // the first function argument.
2809 if (pointer)
2810 {
2811 vardecl *v = new vardecl;
2812 v->type = pe_long;
2813 v->name = "pointer";
2814 v->tok = ts->tok;
2815 fdecl->formal_args.push_back (v);
2816 fcall->args.push_back (pointer);
2817
2818 symbol* sym = new symbol;
2819 sym->tok = ts->tok;
2820 sym->name = v->name;
5f36109e
JS
2821 pointer = sym;
2822 }
2823
2824 // For each expression argument, replace it with a function argument.
2825 for (unsigned i = 0; i < ts->components.size(); ++i)
2826 if (ts->components[i].type == target_symbol::comp_expression_array_index)
2827 {
2828 vardecl *v = new vardecl;
2829 v->type = pe_long;
2830 v->name = "index" + lex_cast(i);
2831 v->tok = ts->tok;
2832 fdecl->formal_args.push_back (v);
2833 fcall->args.push_back (ts->components[i].expr_index);
2834
2835 symbol* sym = new symbol;
2836 sym->tok = ts->tok;
2837 sym->name = v->name;
5f36109e
JS
2838 ts->components[i].expr_index = sym;
2839 }
2840
2841 // Create the return sprintf.
1c922ad7 2842 print_format* pf = print_format::create(ts->tok, "sprintf");
5f36109e
JS
2843 return_statement* rs = new return_statement;
2844 rs->tok = ts->tok;
2845 rs->value = pf;
5f36109e
JS
2846
2847 // Recurse into the actual values.
7d11d8c9 2848 recurse (&base_type, ts, pf, true);
5f36109e
JS
2849 pf->components = print_format::string_to_components(pf->raw_components);
2850
7d11d8c9
JS
2851 // Create the try-catch net
2852 try_block* tb = new try_block;
2853 tb->tok = ts->tok;
2854 tb->try_block = rs;
2855 tb->catch_error_var = 0;
2856 return_statement* rs2 = new return_statement;
2857 rs2->tok = ts->tok;
2858 rs2->value = new literal_string ("ERROR");
2859 rs2->value->tok = ts->tok;
2860 tb->catch_block = rs2;
2861 fdecl->body = tb;
2862
f8809d54 2863 fdecl->join (dw.sess);
5f36109e
JS
2864 return fcall;
2865}
2866
2867
2868void
2869dwarf_pretty_print::recurse (Dwarf_Die* start_type, target_symbol* e,
7d11d8c9 2870 print_format* pf, bool top)
5f36109e
JS
2871{
2872 Dwarf_Die type;
2873 dw.resolve_unqualified_inner_typedie (start_type, &type, e);
2874
2875 switch (dwarf_tag(&type))
2876 {
2877 default:
2878 // XXX need a warning?
2879 // throw semantic_error ("unsupported type (tag " + lex_cast(dwarf_tag(&type))
2880 // + ") for " + dwarf_type_name(&type), e->tok);
2881 pf->raw_components.append("?");
2882 break;
2883
2884 case DW_TAG_enumeration_type:
2885 case DW_TAG_base_type:
7d11d8c9 2886 recurse_base (&type, e, pf);
5f36109e
JS
2887 break;
2888
2889 case DW_TAG_array_type:
7d11d8c9 2890 recurse_array (&type, e, pf, top);
5f36109e
JS
2891 break;
2892
2893 case DW_TAG_pointer_type:
2894 case DW_TAG_reference_type:
2895 case DW_TAG_rvalue_reference_type:
7d11d8c9 2896 recurse_pointer (&type, e, pf, top);
5f36109e
JS
2897 break;
2898
2899 case DW_TAG_subroutine_type:
c55ea10d 2900 push_deref (pf, "<function>:%p", e);
5f36109e
JS
2901 break;
2902
2903 case DW_TAG_union_type:
5f36109e
JS
2904 case DW_TAG_structure_type:
2905 case DW_TAG_class_type:
7d11d8c9 2906 recurse_struct (&type, e, pf, top);
5f36109e
JS
2907 break;
2908 }
2909}
2910
2911
600551ca
JS
2912// Bit fields are handled as a special-case combination of recurse() and
2913// recurse_base(), only called from recurse_struct_members(). The main
2914// difference is that the value is always printed numerically, even if the
2915// underlying type is a char.
2916void
2917dwarf_pretty_print::recurse_bitfield (Dwarf_Die* start_type, target_symbol* e,
2918 print_format* pf)
2919{
2920 Dwarf_Die type;
2921 dw.resolve_unqualified_inner_typedie (start_type, &type, e);
2922
2923 int tag = dwarf_tag(&type);
2924 if (tag != DW_TAG_base_type && tag != DW_TAG_enumeration_type)
2925 {
2926 // XXX need a warning?
2927 // throw semantic_error ("unsupported bitfield type (tag " + lex_cast(tag)
2928 // + ") for " + dwarf_type_name(&type), e->tok);
2929 pf->raw_components.append("?");
2930 return;
2931 }
2932
2933 Dwarf_Attribute attr;
2934 Dwarf_Word encoding = (Dwarf_Word) -1;
2935 dwarf_formudata (dwarf_attr_integrate (&type, DW_AT_encoding, &attr),
2936 &encoding);
2937 switch (encoding)
2938 {
2939 case DW_ATE_float:
2940 case DW_ATE_complex_float:
2941 // XXX need a warning?
2942 // throw semantic_error ("unsupported bitfield type (encoding " + lex_cast(encoding)
2943 // + ") for " + dwarf_type_name(&type), e->tok);
2944 pf->raw_components.append("?");
2945 break;
2946
2947 case DW_ATE_unsigned:
2948 case DW_ATE_unsigned_char:
2949 push_deref (pf, "%u", e);
2950 break;
2951
2952 case DW_ATE_signed:
2953 case DW_ATE_signed_char:
2954 default:
2955 push_deref (pf, "%i", e);
2956 break;
2957 }
2958}
2959
2960
5f36109e
JS
2961void
2962dwarf_pretty_print::recurse_base (Dwarf_Die* type, target_symbol* e,
7d11d8c9 2963 print_format* pf)
5f36109e
JS
2964{
2965 Dwarf_Attribute attr;
2966 Dwarf_Word encoding = (Dwarf_Word) -1;
2967 dwarf_formudata (dwarf_attr_integrate (type, DW_AT_encoding, &attr),
2968 &encoding);
5f36109e
JS
2969 switch (encoding)
2970 {
2971 case DW_ATE_float:
2972 case DW_ATE_complex_float:
2973 // XXX need a warning?
2974 // throw semantic_error ("unsupported type (encoding " + lex_cast(encoding)
2975 // + ") for " + dwarf_type_name(type), e->tok);
2976 pf->raw_components.append("?");
5f36109e
JS
2977 break;
2978
6561d8d1 2979 case DW_ATE_UTF: // XXX need to add unicode to _stp_vsprint_char
5f36109e
JS
2980 case DW_ATE_signed_char:
2981 case DW_ATE_unsigned_char:
941101c1
JS
2982 // Use escapes to make sure that non-printable characters
2983 // don't interrupt our stream (especially '\0' values).
2984 push_deref (pf, "'%#c'", e);
5f36109e
JS
2985 break;
2986
2987 case DW_ATE_unsigned:
c55ea10d 2988 push_deref (pf, "%u", e);
5f36109e
JS
2989 break;
2990
600551ca 2991 case DW_ATE_signed:
5f36109e 2992 default:
c55ea10d 2993 push_deref (pf, "%i", e);
5f36109e
JS
2994 break;
2995 }
5f36109e
JS
2996}
2997
2998
2999void
3000dwarf_pretty_print::recurse_array (Dwarf_Die* type, target_symbol* e,
7d11d8c9 3001 print_format* pf, bool top)
5f36109e 3002{
7d11d8c9
JS
3003 if (!top && !print_full)
3004 {
3005 pf->raw_components.append("[...]");
3006 return;
3007 }
3008
5f36109e
JS
3009 Dwarf_Die childtype;
3010 dwarf_attr_die (type, DW_AT_type, &childtype);
bbee5bb8
JS
3011
3012 if (print_chars (&childtype, e, pf))
3013 return;
3014
5f36109e
JS
3015 pf->raw_components.append("[");
3016
3017 // We print the array up to the first 5 elements.
3018 // XXX how can we determine the array size?
3019 // ... for now, just print the first element
64cddf39 3020 // NB: limit to 32 args; see PR10750 and c_unparser::visit_print_format.
5f36109e 3021 unsigned i, size = 1;
64cddf39 3022 for (i=0; i < size && i < 5 && pf->args.size() < 32; ++i)
5f36109e
JS
3023 {
3024 if (i > 0)
3025 pf->raw_components.append(", ");
3026 target_symbol* e2 = new target_symbol(*e);
3027 e2->components.push_back (target_symbol::component(e->tok, i));
7d11d8c9 3028 recurse (&childtype, e2, pf);
5f36109e
JS
3029 }
3030 if (i < size || 1/*XXX until real size is known */)
3031 pf->raw_components.append(", ...");
3032 pf->raw_components.append("]");
3033}
3034
3035
3036void
3037dwarf_pretty_print::recurse_pointer (Dwarf_Die* type, target_symbol* e,
7d11d8c9 3038 print_format* pf, bool top)
5f36109e 3039{
7d11d8c9 3040 // We chase to top-level pointers, but leave the rest alone
d19a9a82 3041 bool void_p = true;
7d11d8c9 3042 Dwarf_Die pointee;
bbee5bb8 3043 if (dwarf_attr_die (type, DW_AT_type, &pointee))
d19a9a82
JS
3044 {
3045 try
3046 {
3047 dw.resolve_unqualified_inner_typedie (&pointee, &pointee, e);
3048 void_p = false;
3049 }
3050 catch (const semantic_error&) {}
3051 }
3052
3053 if (!void_p)
5f36109e 3054 {
bbee5bb8
JS
3055 if (print_chars (&pointee, e, pf))
3056 return;
3057
3058 if (top)
3059 {
3060 recurse (&pointee, e, pf, top);
3061 return;
3062 }
5f36109e 3063 }
bbee5bb8 3064
c55ea10d 3065 push_deref (pf, "%p", e);
5f36109e
JS
3066}
3067
3068
3069void
3070dwarf_pretty_print::recurse_struct (Dwarf_Die* type, target_symbol* e,
7d11d8c9 3071 print_format* pf, bool top)
5f36109e 3072{
bdec0e18
JS
3073 if (dwarf_hasattr(type, DW_AT_declaration))
3074 {
a44a7cb5 3075 Dwarf_Die *resolved = dw.declaration_resolve(type);
bdec0e18
JS
3076 if (!resolved)
3077 {
3078 // could be an error, but for now just stub it
3079 // throw semantic_error ("unresolved " + dwarf_type_name(type), e->tok);
3080 pf->raw_components.append("{...}");
3081 return;
3082 }
3083 type = resolved;
3084 }
3085
5f36109e
JS
3086 int count = 0;
3087 pf->raw_components.append("{");
7d11d8c9
JS
3088 if (top || print_full)
3089 recurse_struct_members (type, e, pf, count);
3090 else
3091 pf->raw_components.append("...");
5f36109e
JS
3092 pf->raw_components.append("}");
3093}
3094
3095
3096void
3097dwarf_pretty_print::recurse_struct_members (Dwarf_Die* type, target_symbol* e,
7d11d8c9 3098 print_format* pf, int& count)
5f36109e 3099{
a80f28d8
JS
3100 /* With inheritance, a subclass may mask member names of parent classes, so
3101 * our search among the inheritance tree must be breadth-first rather than
3102 * depth-first (recursive). The type die is still our starting point. When
3103 * we encounter a masked name, just skip it. */
3104 set<string> dupes;
3105 deque<Dwarf_Die> inheritees(1, *type);
3106 for (; !inheritees.empty(); inheritees.pop_front())
3107 {
dee830d9 3108 Dwarf_Die child, childtype, import;
a80f28d8
JS
3109 if (dwarf_child (&inheritees.front(), &child) == 0)
3110 do
3111 {
3112 target_symbol* e2 = e;
5f36109e 3113
a80f28d8
JS
3114 // skip static members
3115 if (dwarf_hasattr(&child, DW_AT_declaration))
3116 continue;
5f36109e 3117
a80f28d8 3118 int tag = dwarf_tag (&child);
5f36109e 3119
dee830d9
MW
3120 /* Pretend imported units contain members by recursing into
3121 struct_member printing with the same count. */
3122 if (tag == DW_TAG_imported_unit
3123 && dwarf_attr_die (&child, DW_AT_import, &import))
3124 recurse_struct_members (&import, e2, pf, count);
3125
a80f28d8
JS
3126 if (tag != DW_TAG_member && tag != DW_TAG_inheritance)
3127 continue;
5f36109e 3128
a80f28d8 3129 dwarf_attr_die (&child, DW_AT_type, &childtype);
5f36109e 3130
a80f28d8
JS
3131 if (tag == DW_TAG_inheritance)
3132 {
3133 inheritees.push_back(childtype);
3134 continue;
3135 }
5f36109e 3136
a80f28d8
JS
3137 int childtag = dwarf_tag (&childtype);
3138 const char *member = dwarf_diename (&child);
3a147004 3139
a80f28d8
JS
3140 // "_vptr.foo" members are C++ virtual function tables,
3141 // which (generally?) aren't interesting for users.
3142 if (member && startswith(member, "_vptr."))
3143 continue;
3a147004 3144
a80f28d8
JS
3145 // skip inheritance-masked duplicates
3146 if (member && !dupes.insert(member).second)
3147 continue;
64cddf39 3148
a80f28d8
JS
3149 if (++count > 1)
3150 pf->raw_components.append(", ");
64cddf39 3151
a80f28d8
JS
3152 // NB: limit to 32 args; see PR10750 and c_unparser::visit_print_format.
3153 if (pf->args.size() >= 32)
3154 {
3155 pf->raw_components.append("...");
3156 break;
3157 }
3158
3159 if (member)
3160 {
3161 pf->raw_components.append(".");
3162 pf->raw_components.append(member);
5f36109e 3163
a80f28d8
JS
3164 e2 = new target_symbol(*e);
3165 e2->components.push_back (target_symbol::component(e->tok, member));
3166 }
3167 else if (childtag == DW_TAG_union_type)
3168 pf->raw_components.append("<union>");
3169 else if (childtag == DW_TAG_structure_type)
3170 pf->raw_components.append("<class>");
3171 else if (childtag == DW_TAG_class_type)
3172 pf->raw_components.append("<struct>");
3173 pf->raw_components.append("=");
600551ca
JS
3174
3175 if (dwarf_hasattr_integrate (&child, DW_AT_bit_offset))
3176 recurse_bitfield (&childtype, e2, pf);
3177 else
3178 recurse (&childtype, e2, pf);
5f36109e 3179 }
a80f28d8
JS
3180 while (dwarf_siblingof (&child, &child) == 0);
3181 }
5f36109e
JS
3182}
3183
3184
bbee5bb8
JS
3185bool
3186dwarf_pretty_print::print_chars (Dwarf_Die* start_type, target_symbol* e,
3187 print_format* pf)
3188{
3189 Dwarf_Die type;
3190 dw.resolve_unqualified_inner_typedie (start_type, &type, e);
6561d8d1
JS
3191
3192 Dwarf_Attribute attr;
3193 Dwarf_Word encoding = (Dwarf_Word) -1;
3194 dwarf_formudata (dwarf_attr_integrate (&type, DW_AT_encoding, &attr),
3195 &encoding);
3196 switch (encoding)
bbee5bb8 3197 {
6561d8d1
JS
3198 case DW_ATE_UTF:
3199 case DW_ATE_signed_char:
3200 case DW_ATE_unsigned_char:
3201 break;
3202 default:
3203 return false;
3204 }
3205
3206 string function = userspace_p ? "user_string2" : "kernel_string2";
3207 Dwarf_Word size = (Dwarf_Word) -1;
3208 dwarf_formudata (dwarf_attr_integrate (&type, DW_AT_byte_size, &attr), &size);
3209 switch (size)
3210 {
3211 case 1:
3212 break;
3213 case 2:
3214 function += "_utf16";
3215 break;
3216 case 4:
3217 function += "_utf32";
3218 break;
3219 default:
3220 return false;
3221 }
3222
3223 if (push_deref (pf, "\"%s\"", e))
3224 {
3225 // steal the last arg for a string access
3226 assert (!pf->args.empty());
3227 functioncall* fcall = new functioncall;
3228 fcall->tok = e->tok;
3229 fcall->function = function;
3230 fcall->args.push_back (pf->args.back());
3231 expression *err_msg = new literal_string ("<unknown>");
3232 err_msg->tok = e->tok;
3233 fcall->args.push_back (err_msg);
3234 pf->args.back() = fcall;
bbee5bb8 3235 }
6561d8d1 3236 return true;
bbee5bb8
JS
3237}
3238
a5ce5211
MW
3239// PR10601: adapt to kernel-vs-userspace loc2c-runtime
3240static const string EMBEDDED_FETCH_DEREF_KERNEL = string("\n")
f1e8e7e0
MW
3241 + "#define fetch_register k_fetch_register\n"
3242 + "#define store_register k_store_register\n"
3243 + "#define deref kderef\n"
3244 + "#define store_deref store_kderef\n";
a5ce5211
MW
3245
3246static const string EMBEDDED_FETCH_DEREF_USER = string("\n")
f1e8e7e0
MW
3247 + "#define fetch_register u_fetch_register\n"
3248 + "#define store_register u_store_register\n"
3249 + "#define deref uderef\n"
3250 + "#define store_deref store_uderef\n";
a5ce5211
MW
3251
3252#define EMBEDDED_FETCH_DEREF(U) \
f1e8e7e0 3253 (U ? EMBEDDED_FETCH_DEREF_USER : EMBEDDED_FETCH_DEREF_KERNEL)
a5ce5211
MW
3254
3255static const string EMBEDDED_FETCH_DEREF_DONE = string("\n")
f1e8e7e0
MW
3256 + "#undef fetch_register\n"
3257 + "#undef store_register\n"
3258 + "#undef deref\n"
3259 + "#undef store_deref\n";
bbee5bb8 3260
1c0be8c7
JS
3261static functioncall*
3262synthetic_embedded_deref_call(systemtap_session& session,
3263 const string& function_name,
3264 const string& function_code,
3265 exp_type function_type,
3266 bool userspace_p,
3267 bool lvalue_p,
3268 target_symbol* e,
3269 expression* pointer=NULL)
3270{
3271 // Synthesize a functiondecl for the given embedded code string.
5f36109e
JS
3272 functiondecl *fdecl = new functiondecl;
3273 fdecl->synthetic = true;
3274 fdecl->tok = e->tok;
1c0be8c7
JS
3275 fdecl->name = function_name;
3276 fdecl->type = function_type;
3277
5f36109e
JS
3278 embeddedcode *ec = new embeddedcode;
3279 ec->tok = e->tok;
1c0be8c7
JS
3280 ec->code += "/* unprivileged */";
3281 if (! lvalue_p)
3282 ec->code += "/* pure */";
3283 ec->code += EMBEDDED_FETCH_DEREF(userspace_p);
3284 ec->code += function_code;
3285 ec->code += EMBEDDED_FETCH_DEREF_DONE;
5f36109e
JS
3286 fdecl->body = ec;
3287
3288 // Synthesize a functioncall.
3289 functioncall* fcall = new functioncall;
3290 fcall->tok = e->tok;
3291 fcall->function = fdecl->name;
1c0be8c7 3292 fcall->type = fdecl->type;
5f36109e 3293
1c0be8c7
JS
3294 // If this code snippet uses a precomputed pointer,
3295 // pass that as the first argument.
5f36109e
JS
3296 if (pointer)
3297 {
5f36109e
JS
3298 vardecl *v = new vardecl;
3299 v->type = pe_long;
3300 v->name = "pointer";
3301 v->tok = e->tok;
3302 fdecl->formal_args.push_back(v);
3303 fcall->args.push_back(pointer);
3304 }
5f36109e 3305
1c0be8c7 3306 // Any non-literal indexes need to be passed as arguments too.
5f36109e
JS
3307 for (unsigned i = 0; i < e->components.size(); ++i)
3308 if (e->components[i].type == target_symbol::comp_expression_array_index)
3309 {
3310 vardecl *v = new vardecl;
3311 v->type = pe_long;
3312 v->name = "index" + lex_cast(i);
3313 v->tok = e->tok;
3314 fdecl->formal_args.push_back(v);
3315 fcall->args.push_back(e->components[i].expr_index);
3316 }
3317
1c0be8c7
JS
3318 // If this code snippet is assigning to an lvalue,
3319 // add a final argument for the rvalue.
3320 if (lvalue_p)
3321 {
3322 // Modify the fdecl so it carries a single pe_long formal
3323 // argument called "value".
5f36109e 3324
1c0be8c7
JS
3325 // FIXME: For the time being we only support setting target
3326 // variables which have base types; these are 'pe_long' in
3327 // stap's type vocabulary. Strings and pointers might be
3328 // reasonable, some day, but not today.
5f36109e 3329
1c0be8c7
JS
3330 vardecl *v = new vardecl;
3331 v->type = pe_long;
3332 v->name = "value";
3333 v->tok = e->tok;
3334 fdecl->formal_args.push_back(v);
3335 // NB: We don't know the value for fcall argument yet.
3336 // (see target_symbol_setter_functioncalls)
3337 }
3338
3339 // Add the synthesized decl to the session, and return the call.
3340 fdecl->join (session);
5f36109e
JS
3341 return fcall;
3342}
3343
1c0be8c7
JS
3344expression*
3345dwarf_pretty_print::deref (target_symbol* e)
3346{
3347 static unsigned tick = 0;
3348
3349 if (!deref_p)
3350 {
3351 assert (pointer && e->components.empty());
3352 return pointer;
3353 }
3354
3355 bool lvalue_p = false;
3356 string name = "_dwarf_pretty_print_deref_" + lex_cast(tick++);
3357
3358 string code;
3359 exp_type type = pe_long;
3360 if (pointer)
3361 code = dw.literal_stmt_for_pointer (&pointer_type, e, false, type);
3362 else if (!local.empty())
3363 code = dw.literal_stmt_for_local (scopes, pc, local, e, false, type);
3364 else
3365 code = dw.literal_stmt_for_return (&scopes[0], pc, e, false, type);
3366
3367 return synthetic_embedded_deref_call(dw.sess, name, code, type,
3368 userspace_p, lvalue_p, e, pointer);
3369}
3370
5f36109e 3371
c55ea10d
JS
3372bool
3373dwarf_pretty_print::push_deref (print_format* pf, const string& fmt,
3374 target_symbol* e)
3375{
3376 expression* e2 = NULL;
3377 try
3378 {
3379 e2 = deref (e);
3380 }
3381 catch (const semantic_error&)
3382 {
3383 pf->raw_components.append ("?");
3384 return false;
3385 }
3386 pf->raw_components.append (fmt);
3387 pf->args.push_back (e2);
3388 return true;
3389}
3390
3391
e57b735a 3392void
a7999c82 3393dwarf_var_expanding_visitor::visit_target_symbol_saved_return (target_symbol* e)
e57b735a 3394{
a7999c82
JS
3395 // Get the full name of the target symbol.
3396 stringstream ts_name_stream;
3397 e->print(ts_name_stream);
3398 string ts_name = ts_name_stream.str();
3399
3400 // Check and make sure we haven't already seen this target
3401 // variable in this return probe. If we have, just return our
3402 // last replacement.
af234c40 3403 map<string, expression *>::iterator i = return_ts_map.find(ts_name);
a7999c82 3404 if (i != return_ts_map.end())
85ecf79a 3405 {
a7999c82
JS
3406 provide (i->second);
3407 return;
3408 }
85ecf79a 3409
70208613
JS
3410 // Attempt the expansion directly first, so if there's a problem with the
3411 // variable we won't have a bogus entry probe lying around. Like in
3412 // saveargs(), we pretend for a moment that we're not in a .return.
3413 bool saved_has_return = q.has_return;
3414 q.has_return = false;
3415 expression *repl = e;
3416 replace (repl);
3417 q.has_return = saved_has_return;
3418 target_symbol* n = dynamic_cast<target_symbol*>(repl);
3419 if (n && n->saved_conversion_error)
3420 {
3421 provide (repl);
3422 return;
3423 }
3424
af234c40
JS
3425 expression *exp;
3426 if (!q.has_process &&
3427 strverscmp(q.sess.kernel_base_release.c_str(), "2.6.25") >= 0)
140be17a 3428 exp = gen_kretprobe_saved_return(repl);
af234c40 3429 else
cc9001af 3430 exp = gen_mapped_saved_return(repl, e->sym_name());
af234c40
JS
3431
3432 // Provide the variable to our parent so it can be used as a
3433 // substitute for the target symbol.
3434 provide (exp);
3435
3436 // Remember this replacement since we might be able to reuse
3437 // it later if the same return probe references this target
3438 // symbol again.
3439 return_ts_map[ts_name] = exp;
3440}
3441
4a2970a3 3442static expression*
23dc94f6
DS
3443gen_mapped_saved_return(systemtap_session &sess, expression* e,
3444 const string& name,
3445 block *& add_block, bool& add_block_tid,
3446 block *& add_call_probe, bool& add_call_probe_tid)
af234c40 3447{
23dc94f6
DS
3448 static unsigned tick = 0;
3449
a7999c82
JS
3450 // We've got to do several things here to handle target
3451 // variables in return probes.
85ecf79a 3452
a7999c82
JS
3453 // (1) Synthesize two global arrays. One is the cache of the
3454 // target variable and the other contains a thread specific
3455 // nesting level counter. The arrays will look like
3456 // this:
3457 //
23dc94f6
DS
3458 // _entry_tvar_{name}_{num}
3459 // _entry_tvar_{name}_{num}_ctr
a7999c82 3460
23dc94f6 3461 string aname = (string("_entry_tvar_")
cc9001af 3462 + name
aca66a36 3463 + "_" + lex_cast(tick++));
a7999c82
JS
3464 vardecl* vd = new vardecl;
3465 vd->name = aname;
3466 vd->tok = e->tok;
23dc94f6 3467 sess.globals.push_back (vd);
a7999c82
JS
3468
3469 string ctrname = aname + "_ctr";
3470 vd = new vardecl;
3471 vd->name = ctrname;
3472 vd->tok = e->tok;
23dc94f6 3473 sess.globals.push_back (vd);
a7999c82
JS
3474
3475 // (2) Create a new code block we're going to insert at the
3476 // beginning of this probe to get the cached value into a
3477 // temporary variable. We'll replace the target variable
3478 // reference with the temporary variable reference. The code
3479 // will look like this:
3480 //
23dc94f6
DS
3481 // _entry_tvar_tid = tid()
3482 // _entry_tvar_{name}_{num}_tmp
3483 // = _entry_tvar_{name}_{num}[_entry_tvar_tid,
3484 // _entry_tvar_{name}_{num}_ctr[_entry_tvar_tid]]
3485 // delete _entry_tvar_{name}_{num}[_entry_tvar_tid,
3486 // _entry_tvar_{name}_{num}_ctr[_entry_tvar_tid]--]
3487 // if (! _entry_tvar_{name}_{num}_ctr[_entry_tvar_tid])
3488 // delete _entry_tvar_{name}_{num}_ctr[_entry_tvar_tid]
a7999c82
JS
3489
3490 // (2a) Synthesize the tid temporary expression, which will look
3491 // like this:
3492 //
23dc94f6 3493 // _entry_tvar_tid = tid()
a7999c82 3494 symbol* tidsym = new symbol;
23dc94f6 3495 tidsym->name = string("_entry_tvar_tid");
a7999c82 3496 tidsym->tok = e->tok;
85ecf79a 3497
a7999c82
JS
3498 if (add_block == NULL)
3499 {
3500 add_block = new block;
3501 add_block->tok = e->tok;
8cc799a5 3502 }
8c819921 3503
8cc799a5
JS
3504 if (!add_block_tid)
3505 {
a7999c82
JS
3506 // Synthesize a functioncall to grab the thread id.
3507 functioncall* fc = new functioncall;
3508 fc->tok = e->tok;
3509 fc->function = string("tid");
8c819921 3510
23dc94f6 3511 // Assign the tid to '_entry_tvar_tid'.
8c819921
DS
3512 assignment* a = new assignment;
3513 a->tok = e->tok;
3514 a->op = "=";
a7999c82
JS
3515 a->left = tidsym;
3516 a->right = fc;
8c819921
DS
3517
3518 expr_statement* es = new expr_statement;
3519 es->tok = e->tok;
3520 es->value = a;
8c819921 3521 add_block->statements.push_back (es);
8cc799a5 3522 add_block_tid = true;
a7999c82 3523 }
8c819921 3524
a7999c82
JS
3525 // (2b) Synthesize an array reference and assign it to a
3526 // temporary variable (that we'll use as replacement for the
3527 // target variable reference). It will look like this:
3528 //
23dc94f6
DS
3529 // _entry_tvar_{name}_{num}_tmp
3530 // = _entry_tvar_{name}_{num}[_entry_tvar_tid,
3531 // _entry_tvar_{name}_{num}_ctr[_entry_tvar_tid]]
a7999c82
JS
3532
3533 arrayindex* ai_tvar_base = new arrayindex;
3534 ai_tvar_base->tok = e->tok;
3535
3536 symbol* sym = new symbol;
3537 sym->name = aname;
3538 sym->tok = e->tok;
3539 ai_tvar_base->base = sym;
3540
3541 ai_tvar_base->indexes.push_back(tidsym);
3542
3543 // We need to create a copy of the array index in its current
3544 // state so we can have 2 variants of it (the original and one
3545 // that post-decrements the second index).
3546 arrayindex* ai_tvar = new arrayindex;
3547 arrayindex* ai_tvar_postdec = new arrayindex;
3548 *ai_tvar = *ai_tvar_base;
3549 *ai_tvar_postdec = *ai_tvar_base;
3550
3551 // Synthesize the
23dc94f6 3552 // "_entry_tvar_{name}_{num}_ctr[_entry_tvar_tid]" used as the
a7999c82
JS
3553 // second index into the array.
3554 arrayindex* ai_ctr = new arrayindex;
3555 ai_ctr->tok = e->tok;
3556
3557 sym = new symbol;
3558 sym->name = ctrname;
3559 sym->tok = e->tok;
3560 ai_ctr->base = sym;
3561 ai_ctr->indexes.push_back(tidsym);
3562 ai_tvar->indexes.push_back(ai_ctr);
3563
3564 symbol* tmpsym = new symbol;
3565 tmpsym->name = aname + "_tmp";
3566 tmpsym->tok = e->tok;
3567
3568 assignment* a = new assignment;
3569 a->tok = e->tok;
3570 a->op = "=";
3571 a->left = tmpsym;
3572 a->right = ai_tvar;
3573
3574 expr_statement* es = new expr_statement;
3575 es->tok = e->tok;
3576 es->value = a;
3577
3578 add_block->statements.push_back (es);
3579
3580 // (2c) Add a post-decrement to the second array index and
3581 // delete the array value. It will look like this:
3582 //
23dc94f6
DS
3583 // delete _entry_tvar_{name}_{num}[_entry_tvar_tid,
3584 // _entry_tvar_{name}_{num}_ctr[_entry_tvar_tid]--]
a7999c82
JS
3585
3586 post_crement* pc = new post_crement;
3587 pc->tok = e->tok;
3588 pc->op = "--";
3589 pc->operand = ai_ctr;
3590 ai_tvar_postdec->indexes.push_back(pc);
3591
3592 delete_statement* ds = new delete_statement;
3593 ds->tok = e->tok;
3594 ds->value = ai_tvar_postdec;
3595
3596 add_block->statements.push_back (ds);
3597
3598 // (2d) Delete the counter value if it is 0. It will look like
3599 // this:
23dc94f6
DS
3600 // if (! _entry_tvar_{name}_{num}_ctr[_entry_tvar_tid])
3601 // delete _entry_tvar_{name}_{num}_ctr[_entry_tvar_tid]
a7999c82
JS
3602
3603 ds = new delete_statement;
3604 ds->tok = e->tok;
3605 ds->value = ai_ctr;
3606
3607 unary_expression *ue = new unary_expression;
3608 ue->tok = e->tok;
3609 ue->op = "!";
3610 ue->operand = ai_ctr;
3611
3612 if_statement *ifs = new if_statement;
3613 ifs->tok = e->tok;
3614 ifs->condition = ue;
3615 ifs->thenblock = ds;
3616 ifs->elseblock = NULL;
3617
3618 add_block->statements.push_back (ifs);
3619
3620 // (3) We need an entry probe that saves the value for us in the
3621 // global array we created. Create the entry probe, which will
3622 // look like this:
3623 //
2260f4e3 3624 // probe kernel.function("{function}").call {
23dc94f6
DS
3625 // _entry_tvar_tid = tid()
3626 // _entry_tvar_{name}_{num}[_entry_tvar_tid,
3627 // ++_entry_tvar_{name}_{num}_ctr[_entry_tvar_tid]]
a7999c82
JS
3628 // = ${param}
3629 // }
3630
2260f4e3 3631 if (add_call_probe == NULL)
a7999c82 3632 {
2260f4e3
FCE
3633 add_call_probe = new block;
3634 add_call_probe->tok = e->tok;
8cc799a5 3635 }
4baf0e53 3636
8cc799a5
JS
3637 if (!add_call_probe_tid)
3638 {
a7999c82
JS
3639 // Synthesize a functioncall to grab the thread id.
3640 functioncall* fc = new functioncall;
3641 fc->tok = e->tok;
3642 fc->function = string("tid");
4baf0e53 3643
23dc94f6 3644 // Assign the tid to '_entry_tvar_tid'.
a7999c82 3645 assignment* a = new assignment;
8fc05e57
DS
3646 a->tok = e->tok;
3647 a->op = "=";
a7999c82
JS
3648 a->left = tidsym;
3649 a->right = fc;
8fc05e57 3650
a7999c82 3651 expr_statement* es = new expr_statement;
8fc05e57
DS
3652 es->tok = e->tok;
3653 es->value = a;
2260f4e3 3654 add_call_probe = new block(add_call_probe, es);
8cc799a5 3655 add_call_probe_tid = true;
85ecf79a 3656 }
cf2a1f85 3657
a7999c82 3658 // Save the value, like this:
23dc94f6
DS
3659 // _entry_tvar_{name}_{num}[_entry_tvar_tid,
3660 // ++_entry_tvar_{name}_{num}_ctr[_entry_tvar_tid]]
a7999c82
JS
3661 // = ${param}
3662 arrayindex* ai_tvar_preinc = new arrayindex;
3663 *ai_tvar_preinc = *ai_tvar_base;
3664
3665 pre_crement* preinc = new pre_crement;
3666 preinc->tok = e->tok;
3667 preinc->op = "++";
3668 preinc->operand = ai_ctr;
3669 ai_tvar_preinc->indexes.push_back(preinc);
3670
3671 a = new assignment;
3672 a->tok = e->tok;
3673 a->op = "=";
3674 a->left = ai_tvar_preinc;
3675 a->right = e;
3676
3677 es = new expr_statement;
3678 es->tok = e->tok;
3679 es->value = a;
3680
2260f4e3 3681 add_call_probe = new block(add_call_probe, es);
a7999c82 3682
23dc94f6 3683 // (4) Provide the '_entry_tvar_{name}_{num}_tmp' variable to
a7999c82
JS
3684 // our parent so it can be used as a substitute for the target
3685 // symbol.
3f803f9e 3686 delete ai_tvar_base;
af234c40
JS
3687 return tmpsym;
3688}
a7999c82 3689
af234c40 3690
23dc94f6
DS
3691expression*
3692dwarf_var_expanding_visitor::gen_mapped_saved_return(expression* e,
3693 const string& name)
3694{
3695 return ::gen_mapped_saved_return(q.sess, e, name, add_block,
3696 add_block_tid, add_call_probe,
3697 add_call_probe_tid);
3698}
3699
3700
af234c40 3701expression*
140be17a 3702dwarf_var_expanding_visitor::gen_kretprobe_saved_return(expression* e)
af234c40
JS
3703{
3704 // The code for this is simple.
3705 //
3706 // .call:
3707 // _set_kretprobe_long(index, $value)
3708 //
3709 // .return:
3710 // _get_kretprobe_long(index)
3711 //
3712 // (or s/long/string/ for things like $$parms)
3713
3714 unsigned index;
3715 string setfn, getfn;
3716
140be17a
JS
3717 // We need the caller to predetermine the type of the expression!
3718 switch (e->type)
af234c40 3719 {
140be17a 3720 case pe_string:
af234c40
JS
3721 index = saved_strings++;
3722 setfn = "_set_kretprobe_string";
3723 getfn = "_get_kretprobe_string";
140be17a
JS
3724 break;
3725 case pe_long:
af234c40
JS
3726 index = saved_longs++;
3727 setfn = "_set_kretprobe_long";
3728 getfn = "_get_kretprobe_long";
140be17a
JS
3729 break;
3730 default:
dc09353a 3731 throw SEMANTIC_ERROR(_("unknown type to save in kretprobe"), e->tok);
af234c40
JS
3732 }
3733
3734 // Create the entry code
3735 // _set_kretprobe_{long|string}(index, $value)
3736
3737 if (add_call_probe == NULL)
3738 {
3739 add_call_probe = new block;
3740 add_call_probe->tok = e->tok;
3741 }
3742
3743 functioncall* set_fc = new functioncall;
3744 set_fc->tok = e->tok;
3745 set_fc->function = setfn;
3746 set_fc->args.push_back(new literal_number(index));
3747 set_fc->args.back()->tok = e->tok;
3748 set_fc->args.push_back(e);
3749
3750 expr_statement* set_es = new expr_statement;
3751 set_es->tok = e->tok;
3752 set_es->value = set_fc;
3753
3754 add_call_probe->statements.push_back(set_es);
3755
3756 // Create the return code
3757 // _get_kretprobe_{long|string}(index)
3758
3759 functioncall* get_fc = new functioncall;
3760 get_fc->tok = e->tok;
3761 get_fc->function = getfn;
3762 get_fc->args.push_back(new literal_number(index));
3763 get_fc->args.back()->tok = e->tok;
3764
3765 return get_fc;
a7999c82 3766}
a43ba433 3767
2cb3fe26 3768
a7999c82
JS
3769void
3770dwarf_var_expanding_visitor::visit_target_symbol_context (target_symbol* e)
3771{
b78a0fbb
LB
3772 if (null_die(scope_die)) {
3773 print_format* pf = print_format::create(e->tok, "sprintf");
3774 pf->raw_components = "";
3775 pf->components = print_format::string_to_components(pf->raw_components);
3776 pf->type = pe_string;
3777 provide (pf);
a7999c82 3778 return;
b78a0fbb 3779 }
2cb3fe26 3780
5f36109e
JS
3781 target_symbol *tsym = new target_symbol(*e);
3782
fde50242
JS
3783 bool pretty = (!e->components.empty() &&
3784 e->components[0].type == target_symbol::comp_pretty_print);
3785 string format = pretty ? "=%s" : "=%#x";
a43ba433 3786
a7999c82
JS
3787 // Convert $$parms to sprintf of a list of parms and active local vars
3788 // which we recursively evaluate
a43ba433 3789
1c922ad7 3790 print_format* pf = print_format::create(e->tok, "sprintf");
a7999c82 3791
277c21bc 3792 if (q.has_return && (e->name == "$$return"))
a7999c82 3793 {
277c21bc 3794 tsym->name = "$return";
a7999c82
JS
3795
3796 // Ignore any variable that isn't accessible.
3797 tsym->saved_conversion_error = 0;
3798 expression *texp = tsym;
8b095b45 3799 replace (texp); // NB: throws nothing ...
a7999c82 3800 if (tsym->saved_conversion_error) // ... but this is how we know it happened.
a43ba433 3801 {
2cb3fe26 3802
a43ba433
FCE
3803 }
3804 else
3805 {
a7999c82 3806 pf->raw_components += "return";
5f36109e 3807 pf->raw_components += format;
a7999c82
JS
3808 pf->args.push_back(texp);
3809 }
3810 }
3811 else
3812 {
3813 // non-.return probe: support $$parms, $$vars, $$locals
345bbb3d 3814 bool first = true;
a7999c82 3815 Dwarf_Die result;
d48bc7eb
JS
3816 vector<Dwarf_Die> scopes = q.dw.getscopes(scope_die);
3817 for (unsigned i = 0; i < scopes.size(); ++i)
3818 {
3819 if (dwarf_tag(&scopes[i]) == DW_TAG_compile_unit)
3820 break; // we don't want file-level variables
3821 if (dwarf_child (&scopes[i], &result) == 0)
3822 do
00cf3709 3823 {
d48bc7eb
JS
3824 switch (dwarf_tag (&result))
3825 {
3826 case DW_TAG_variable:
3827 if (e->name == "$$parms")
3828 continue;
3829 break;
3830 case DW_TAG_formal_parameter:
3831 if (e->name == "$$locals")
3832 continue;
3833 break;
3834
3835 default:
3836 continue;
3837 }
41c262f3 3838
d48bc7eb
JS
3839 const char *diename = dwarf_diename (&result);
3840 if (! diename) continue;
f76427a2 3841
d48bc7eb
JS
3842 if (! first)
3843 pf->raw_components += " ";
3844 pf->raw_components += diename;
fde50242
JS
3845 first = false;
3846
3847 // Write a placeholder for ugly aggregates
3848 Dwarf_Die type;
3849 if (!pretty && dwarf_attr_die(&result, DW_AT_type, &type))
3850 {
3851 q.dw.resolve_unqualified_inner_typedie(&type, &type, e);
3852 switch (dwarf_tag(&type))
3853 {
3854 case DW_TAG_union_type:
3855 case DW_TAG_structure_type:
3856 case DW_TAG_class_type:
3857 pf->raw_components += "={...}";
3858 continue;
3859
3860 case DW_TAG_array_type:
3861 pf->raw_components += "=[...]";
3862 continue;
3863 }
3864 }
345bbb3d 3865
d48bc7eb
JS
3866 tsym->name = "$";
3867 tsym->name += diename;
41c262f3 3868
d48bc7eb
JS
3869 // Ignore any variable that isn't accessible.
3870 tsym->saved_conversion_error = 0;
3871 expression *texp = tsym;
3872 replace (texp); // NB: throws nothing ...
3873 if (tsym->saved_conversion_error) // ... but this is how we know it happened.
a43ba433 3874 {
d48bc7eb
JS
3875 if (q.sess.verbose>2)
3876 {
e26c2f83 3877 for (const semantic_error *c = tsym->saved_conversion_error;
d48bc7eb 3878 c != 0;
c081af73 3879 c = c->get_chain()) {
4c5d9906 3880 clog << _("variable location problem [man error::dwarf]: ") << c->what() << endl;
d48bc7eb
JS
3881 }
3882 }
3883
3884 pf->raw_components += "=?";
a43ba433 3885 }
d48bc7eb
JS
3886 else
3887 {
3888 pf->raw_components += format;
3889 pf->args.push_back(texp);
3890 }
a7999c82 3891 }
d48bc7eb
JS
3892 while (dwarf_siblingof (&result, &result) == 0);
3893 }
a7999c82 3894 }
2cb3fe26 3895
a7999c82 3896 pf->components = print_format::string_to_components(pf->raw_components);
140be17a 3897 pf->type = pe_string;
a7999c82
JS
3898 provide (pf);
3899}
3900
2cb3fe26 3901
bd1fcbad
YZ
3902void
3903dwarf_var_expanding_visitor::visit_atvar_op (atvar_op *e)
3904{
3905 // Fill in our current module context if needed
3906 if (e->module.empty())
3907 e->module = q.dw.module_name;
3908
3909 if (e->module == q.dw.module_name && e->cu_name.empty())
3910 {
3911 // process like any other local
3912 // e->sym_name() will do the right thing
3913 visit_target_symbol(e);
3914 return;
3915 }
3916
3917 var_expanding_visitor::visit_atvar_op(e);
3918}
3919
3920
a7999c82
JS
3921void
3922dwarf_var_expanding_visitor::visit_target_symbol (target_symbol *e)
3923{
bd1fcbad 3924 assert(e->name.size() > 0 && (e->name[0] == '$' || e->name == "@var"));
a7999c82 3925 visited = true;
30263a73
FCE
3926 bool defined_being_checked = (defined_ops.size() > 0 && (defined_ops.top()->operand == e));
3927 // In this mode, we avoid hiding errors or generating extra code such as for .return saved $vars
a7999c82 3928
70208613 3929 try
a7999c82 3930 {
c69a87e0
FCE
3931 bool lvalue = is_active_lvalue(e);
3932 if (lvalue && !q.sess.guru_mode)
dc09353a 3933 throw SEMANTIC_ERROR(_("write to target variable not permitted; need stap -g"), e->tok);
2cb3fe26 3934
100a540e 3935 // XXX: process $context vars should be writable
70208613 3936
c69a87e0
FCE
3937 // See if we need to generate a new probe to save/access function
3938 // parameters from a return probe. PR 1382.
3939 if (q.has_return
3940 && !defined_being_checked
277c21bc
JS
3941 && e->name != "$return" // not the special return-value variable handled below
3942 && e->name != "$$return") // nor the other special variable handled below
c69a87e0
FCE
3943 {
3944 if (lvalue)
dc09353a 3945 throw SEMANTIC_ERROR(_("write to target variable not permitted in .return probes"), e->tok);
c69a87e0
FCE
3946 visit_target_symbol_saved_return(e);
3947 return;
3948 }
e57b735a 3949
277c21bc
JS
3950 if (e->name == "$$vars" || e->name == "$$parms" || e->name == "$$locals"
3951 || (q.has_return && (e->name == "$$return")))
c69a87e0
FCE
3952 {
3953 if (lvalue)
dc09353a 3954 throw SEMANTIC_ERROR(_("cannot write to context variable"), e->tok);
70208613 3955
c69a87e0 3956 if (e->addressof)
dc09353a 3957 throw SEMANTIC_ERROR(_("cannot take address of context variable"), e->tok);
70208613 3958
5f36109e
JS
3959 e->assert_no_components("dwarf", true);
3960
c69a87e0
FCE
3961 visit_target_symbol_context(e);
3962 return;
3963 }
70208613 3964
5f36109e
JS
3965 if (!e->components.empty() &&
3966 e->components.back().type == target_symbol::comp_pretty_print)
3967 {
3968 if (lvalue)
dc09353a 3969 throw SEMANTIC_ERROR(_("cannot write to pretty-printed variable"), e->tok);
5f36109e 3970
277c21bc 3971 if (q.has_return && (e->name == "$return"))
5f36109e
JS
3972 {
3973 dwarf_pretty_print dpp (q.dw, scope_die, addr,
3974 q.has_process, *e);
3975 dpp.expand()->visit(this);
3976 }
3977 else
3978 {
3979 dwarf_pretty_print dpp (q.dw, getscopes(e), addr,
cc9001af 3980 e->sym_name(),
5f36109e
JS
3981 q.has_process, *e);
3982 dpp.expand()->visit(this);
3983 }
3984 return;
3985 }
3986
1c0be8c7 3987 bool userspace_p = q.has_process;
c69a87e0 3988 string fname = (string(lvalue ? "_dwarf_tvar_set" : "_dwarf_tvar_get")
cc9001af 3989 + "_" + e->sym_name()
c69a87e0 3990 + "_" + lex_cast(tick++));
70208613 3991
70208613 3992
1c0be8c7
JS
3993 exp_type type = pe_long;
3994 string code;
277c21bc 3995 if (q.has_return && (e->name == "$return"))
1c0be8c7 3996 code = q.dw.literal_stmt_for_return (scope_die, addr, e, lvalue, type);
e19fda4e 3997 else
1c0be8c7
JS
3998 code = q.dw.literal_stmt_for_local (getscopes(e), addr, e->sym_name(),
3999 e, lvalue, type);
70208613 4000
1c0be8c7
JS
4001 functioncall* n = synthetic_embedded_deref_call(q.sess, fname, code, type,
4002 userspace_p, lvalue, e);
70208613 4003
c69a87e0
FCE
4004 if (lvalue)
4005 {
4006 // Provide the functioncall to our parent, so that it can be
4007 // used to substitute for the assignment node immediately above
4008 // us.
4009 assert(!target_symbol_setter_functioncalls.empty());
4010 *(target_symbol_setter_functioncalls.top()) = n;
4011 }
70208613 4012
1c0be8c7
JS
4013 // Revisit the functioncall so arguments can be expanded.
4014 n->visit (this);
66d284f4
FCE
4015 }
4016 catch (const semantic_error& er)
4017 {
9fab2262
JS
4018 // We suppress this error message, and pass the unresolved
4019 // target_symbol to the next pass. We hope that this value ends
4020 // up not being referenced after all, so it can be optimized out
4021 // quietly.
1af1e62d 4022 e->chain (er);
9fab2262 4023 provide (e);
66d284f4 4024 }
77de5e9e
GH
4025}
4026
4027
c24447be
JS
4028void
4029dwarf_var_expanding_visitor::visit_cast_op (cast_op *e)
4030{
4031 // Fill in our current module context if needed
4032 if (e->module.empty())
4033 e->module = q.dw.module_name;
4034
4035 var_expanding_visitor::visit_cast_op(e);
4036}
4037
4038
8cc799a5
JS
4039void
4040dwarf_var_expanding_visitor::visit_entry_op (entry_op *e)
4041{
4042 expression *repl = e;
4043 if (q.has_return)
4044 {
4045 // expand the operand as if it weren't a return probe
4046 q.has_return = false;
4047 replace (e->operand);
4048 q.has_return = true;
4049
4050 // XXX it would be nice to use gen_kretprobe_saved_return when available,
4051 // but it requires knowing the types already, which is problematic for
4052 // arbitrary expressons.
cc9001af 4053 repl = gen_mapped_saved_return (e->operand, "entry");
8cc799a5
JS
4054 }
4055 provide (repl);
4056}
4057
3689db05
SC
4058void
4059dwarf_var_expanding_visitor::visit_perf_op (perf_op *e)
4060{
ace7c23f 4061 string e_lit_val = e->operand->value;
b78a0fbb 4062
3689db05 4063 add_block = new block;
9eaaceaa 4064 add_block->tok = e->tok;
3689db05
SC
4065
4066 systemtap_session &s = this->q.sess;
4067 map<string, pair<string,derived_probe*> >::iterator it;
4068 // Find the associated perf.counter probe
4069 for (it=s.perf_counters.begin();
4070 it != s.perf_counters.end();
4071 it++)
4072 if ((*it).first == e_lit_val)
4073 {
4074 // if perf .function("name") omitted, then set it to this process name
4075 if ((*it).second.first.length() == 0)
4076 ((*it).second).first = this->q.user_path;
4077 if (((*it).second).first == this->q.user_path)
4078 break;
4079 }
4080
4081 if (it != s.perf_counters.end())
4082 {
698de6cc 4083 perf_counter_refs.insert((*it).second.second);
3689db05
SC
4084 // __perf_read_N is assigned in the probe prologue
4085 symbol* sym = new symbol;
4653caf1 4086 sym->tok = e->tok;
3689db05
SC
4087 sym->name = "__perf_read_" + (*it).first;
4088 provide (sym);
4089 }
4090 else
dc09353a 4091 throw SEMANTIC_ERROR(_F("perf counter '%s' not defined", e_lit_val.c_str()));
3689db05
SC
4092}
4093
8cc799a5 4094
729455a7
JS
4095vector<Dwarf_Die>&
4096dwarf_var_expanding_visitor::getscopes(target_symbol *e)
4097{
4098 if (scopes.empty())
4099 {
f25a9197
CM
4100 if(scope_die != NULL)
4101 scopes = q.dw.getscopes(scope_die);
729455a7 4102 if (scopes.empty())
b530b5b3
LB
4103 //throw semantic_error (_F("unable to find any scopes containing %d", addr), e->tok);
4104 // ((scope_die == NULL) ? "" : (string (" in ") + (dwarf_diename(scope_die) ?: "<unknown>") + "(" + (dwarf_diename(q.dw.cu) ?: "<unknown>") ")" ))
dc09353a 4105 throw SEMANTIC_ERROR ("unable to find any scopes containing "
729455a7
JS
4106 + lex_cast_hex(addr)
4107 + ((scope_die == NULL) ? ""
4108 : (string (" in ")
4109 + (dwarf_diename(scope_die) ?: "<unknown>")
4110 + "(" + (dwarf_diename(q.dw.cu) ?: "<unknown>")
4111 + ")"))
4112 + " while searching for local '"
cc9001af 4113 + e->sym_name() + "'",
729455a7
JS
4114 e->tok);
4115 }
4116 return scopes;
4117}
4118
4119
5f36109e
JS
4120struct dwarf_cast_expanding_visitor: public var_expanding_visitor
4121{
4122 systemtap_session& s;
4123 dwarf_builder& db;
4124
4125 dwarf_cast_expanding_visitor(systemtap_session& s, dwarf_builder& db):
4126 s(s), db(db) {}
4127 void visit_cast_op (cast_op* e);
4128 void filter_special_modules(string& module);
4129};
4130
4131
c4ce66a1
JS
4132struct dwarf_cast_query : public base_query
4133{
946e1a48 4134 cast_op& e;
c4ce66a1 4135 const bool lvalue;
5f36109e
JS
4136 const bool userspace_p;
4137 functioncall*& result;
c4ce66a1 4138
5f36109e
JS
4139 dwarf_cast_query(dwflpp& dw, const string& module, cast_op& e, bool lvalue,
4140 const bool userspace_p, functioncall*& result):
abb41d92 4141 base_query(dw, module), e(e), lvalue(lvalue),
5f36109e 4142 userspace_p(userspace_p), result(result) {}
c4ce66a1
JS
4143
4144 void handle_query_module();
822a6a3d 4145 void query_library (const char *) {}
576eaefe 4146 void query_plt (const char *entry, size_t addr) {}
c4ce66a1
JS
4147};
4148
4149
c4ce66a1
JS
4150void
4151dwarf_cast_query::handle_query_module()
4152{
5f36109e
JS
4153 static unsigned tick = 0;
4154
4155 if (result)
c4ce66a1
JS
4156 return;
4157
ea1e477a 4158 // look for the type in any CU
a44a7cb5
JS
4159 Dwarf_Die* type_die = NULL;
4160 if (startswith(e.type_name, "class "))
4161 {
4162 // normalize to match dwflpp::global_alias_caching_callback
4163 string struct_name = "struct " + e.type_name.substr(6);
4164 type_die = dw.declaration_resolve_other_cus(struct_name);
4165 }
4166 else
4167 type_die = dw.declaration_resolve_other_cus(e.type_name);
4168
4169 // NB: We now index the types as "struct name"/"union name"/etc. instead of
4170 // just "name". But since we didn't require users to be explicit before, and
4171 // actually sort of discouraged it, we must be flexible now. So if a lookup
4172 // fails with a bare name, try augmenting it.
4173 if (!type_die &&
4174 !startswith(e.type_name, "class ") &&
4175 !startswith(e.type_name, "struct ") &&
4176 !startswith(e.type_name, "union ") &&
4177 !startswith(e.type_name, "enum "))
4178 {
4179 type_die = dw.declaration_resolve_other_cus("struct " + e.type_name);
4180 if (!type_die)
4181 type_die = dw.declaration_resolve_other_cus("union " + e.type_name);
4182 if (!type_die)
4183 type_die = dw.declaration_resolve_other_cus("enum " + e.type_name);
4184 }
4185
ea1e477a
JS
4186 if (!type_die)
4187 return;
c4ce66a1 4188
5f36109e
JS
4189 string code;
4190 exp_type type = pe_long;
4191
ea1e477a 4192 try
c4ce66a1 4193 {
ea1e477a
JS
4194 Dwarf_Die cu_mem;
4195 dw.focus_on_cu(dwarf_diecu(type_die, &cu_mem, NULL, NULL));
5f36109e
JS
4196
4197 if (!e.components.empty() &&
4198 e.components.back().type == target_symbol::comp_pretty_print)
4199 {
4200 if (lvalue)
dc09353a 4201 throw SEMANTIC_ERROR(_("cannot write to pretty-printed variable"), e.tok);
5f36109e 4202
d19a9a82 4203 dwarf_pretty_print dpp(dw, type_die, e.operand, true, userspace_p, e);
5f36109e
JS
4204 result = dpp.expand();
4205 return;
4206 }
4207
4208 code = dw.literal_stmt_for_pointer (type_die, &e, lvalue, type);
ea1e477a
JS
4209 }
4210 catch (const semantic_error& er)
4211 {
4212 // NB: we can have multiple errors, since a @cast
1af1e62d
JS
4213 // may be attempted using several different modules:
4214 // @cast(ptr, "type", "module1:module2:...")
4215 e.chain (er);
c4ce66a1 4216 }
c4ce66a1 4217
5f36109e
JS
4218 if (code.empty())
4219 return;
c4ce66a1 4220
5f36109e 4221 string fname = (string(lvalue ? "_dwarf_cast_set" : "_dwarf_cast_get")
cc9001af 4222 + "_" + e.sym_name()
5f36109e 4223 + "_" + lex_cast(tick++));
c4ce66a1 4224
1c0be8c7
JS
4225 result = synthetic_embedded_deref_call(dw.sess, fname, code, type,
4226 userspace_p, lvalue, &e, e.operand);
5f36109e 4227}
c4ce66a1
JS
4228
4229
fb0274bc
JS
4230void dwarf_cast_expanding_visitor::filter_special_modules(string& module)
4231{
d90053e7 4232 // look for "<path/to/header>" or "kernel<path/to/header>"
fb0274bc 4233 // for those cases, build a module including that header
d90053e7 4234 if (module[module.size() - 1] == '>' &&
60d98537 4235 (module[0] == '<' || startswith(module, "kernel<")))
fb0274bc
JS
4236 {
4237 string cached_module;
4238 if (s.use_cache)
4239 {
4240 // see if the cached module exists
a2639cb7 4241 cached_module = find_typequery_hash(s, module);
d105f664 4242 if (!cached_module.empty() && !s.poison_cache)
fb0274bc
JS
4243 {
4244 int fd = open(cached_module.c_str(), O_RDONLY);
4245 if (fd != -1)
4246 {
4247 if (s.verbose > 2)
b530b5b3
LB
4248 //TRANSLATORS: Here we're using a cached module.
4249 clog << _("Pass 2: using cached ") << cached_module << endl;
fb0274bc
JS
4250 module = cached_module;
4251 close(fd);
4252 return;
4253 }
4254 }
4255 }
4256
4257 // no cached module, time to make it
d90053e7 4258 if (make_typequery(s, module) == 0)
fb0274bc 4259 {
e16dc041 4260 // try to save typequery in the cache
fb0274bc 4261 if (s.use_cache)
e16dc041 4262 copy_file(module, cached_module, s.verbose > 2);
fb0274bc
JS
4263 }
4264 }
4265}
4266
4267
c4ce66a1
JS
4268void dwarf_cast_expanding_visitor::visit_cast_op (cast_op* e)
4269{
4270 bool lvalue = is_active_lvalue(e);
4271 if (lvalue && !s.guru_mode)
dc09353a 4272 throw SEMANTIC_ERROR(_("write to @cast context variable not permitted; need stap -g"), e->tok);
c4ce66a1
JS
4273
4274 if (e->module.empty())
4275 e->module = "kernel"; // "*" may also be reasonable to search all kernel modules
4276
5f36109e 4277 functioncall* result = NULL;
8b31197b
JS
4278
4279 // split the module string by ':' for alternatives
4280 vector<string> modules;
4281 tokenize(e->module, modules, ":");
b5a0dd41 4282 bool userspace_p=false; // PR10601
5f36109e 4283 for (unsigned i = 0; !result && i < modules.size(); ++i)
c4ce66a1 4284 {
8b31197b 4285 string& module = modules[i];
fb0274bc 4286 filter_special_modules(module);
abb41d92 4287
c4ce66a1
JS
4288 // NB: This uses '/' to distinguish between kernel modules and userspace,
4289 // which means that userspace modules won't get any PATH searching.
4290 dwflpp* dw;
707bf35e
JS
4291 try
4292 {
b5a0dd41
FCE
4293 userspace_p=is_user_module (module);
4294 if (! userspace_p)
707bf35e
JS
4295 {
4296 // kernel or kernel module target
ae2552da 4297 dw = db.get_kern_dw(s, module);
707bf35e
JS
4298 }
4299 else
4300 {
05fb3e0c 4301 module = find_executable (module, "", s.sysenv); // canonicalize it
707bf35e
JS
4302 dw = db.get_user_dw(s, module);
4303 }
4304 }
4305 catch (const semantic_error& er)
4306 {
4307 /* ignore and go to the next module */
4308 continue;
4309 }
c4ce66a1 4310
5f36109e 4311 dwarf_cast_query q (*dw, module, *e, lvalue, userspace_p, result);
06de3a04 4312 dw->iterate_over_modules<base_query>(&query_module, &q);
c4ce66a1 4313 }
abb41d92 4314
5f36109e 4315 if (!result)
c4ce66a1 4316 {
946e1a48
JS
4317 // We pass the unresolved cast_op to the next pass, and hope
4318 // that this value ends up not being referenced after all, so
4319 // it can be optimized out quietly.
c4ce66a1
JS
4320 provide (e);
4321 return;
4322 }
4323
c4ce66a1
JS
4324 if (lvalue)
4325 {
4326 // Provide the functioncall to our parent, so that it can be
4327 // used to substitute for the assignment node immediately above
4328 // us.
4329 assert(!target_symbol_setter_functioncalls.empty());
5f36109e 4330 *(target_symbol_setter_functioncalls.top()) = result;
c4ce66a1
JS
4331 }
4332
5f36109e 4333 result->visit (this);
77de5e9e
GH
4334}
4335
4336
bd1fcbad
YZ
4337struct dwarf_atvar_expanding_visitor: public var_expanding_visitor
4338{
4339 systemtap_session& s;
4340 dwarf_builder& db;
4341
4342 dwarf_atvar_expanding_visitor(systemtap_session& s, dwarf_builder& db):
4343 s(s), db(db) {}
4344 void visit_atvar_op (atvar_op* e);
4345};
4346
4347
4348struct dwarf_atvar_query: public base_query
4349{
4350 atvar_op& e;
4351 const bool userspace_p, lvalue;
4352 functioncall*& result;
4353 unsigned& tick;
4354 const string cu_name_pattern;
4355
4356 dwarf_atvar_query(dwflpp& dw, const string& module, atvar_op& e,
4357 const bool userspace_p, const bool lvalue,
4358 functioncall*& result,
4359 unsigned& tick):
4360 base_query(dw, module), e(e),
4361 userspace_p(userspace_p), lvalue(lvalue), result(result),
4362 tick(tick), cu_name_pattern(string("*/") + e.cu_name) {}
4363
4364 void handle_query_module ();
4365 void query_library (const char *) {}
4366 void query_plt (const char *entry, size_t addr) {}
5c378838 4367 static int atvar_query_cu (Dwarf_Die *cudie, dwarf_atvar_query *q);
bd1fcbad
YZ
4368};
4369
4370
4371int
5c378838 4372dwarf_atvar_query::atvar_query_cu (Dwarf_Die * cudie, dwarf_atvar_query *q)
bd1fcbad 4373{
bd1fcbad
YZ
4374 if (! q->e.cu_name.empty())
4375 {
c60517ca 4376 const char *die_name = dwarf_diename(cudie) ?: "";
bd1fcbad
YZ
4377 if (strcmp(die_name, q->e.cu_name.c_str()) != 0 // Perfect match
4378 && fnmatch(q->cu_name_pattern.c_str(), die_name, 0) != 0)
4379 {
4380 return DWARF_CB_OK;
4381 }
4382 }
4383
4384 try
4385 {
4386 vector<Dwarf_Die> scopes(1, *cudie);
4387
4388 q->dw.focus_on_cu (cudie);
4389
4390 if (! q->e.components.empty() &&
4391 q->e.components.back().type == target_symbol::comp_pretty_print)
4392 {
4393 dwarf_pretty_print dpp (q->dw, scopes, 0, q->e.sym_name(),
4394 q->userspace_p, q->e);
4395 q->result = dpp.expand();
4396 return DWARF_CB_ABORT;
4397 }
4398
4399 exp_type type = pe_long;
4400 string code = q->dw.literal_stmt_for_local (scopes, 0, q->e.sym_name(),
4401 &q->e, q->lvalue, type);
4402
4403 if (code.empty())
4404 return DWARF_CB_OK;
4405
4406 string fname = (string(q->lvalue ? "_dwarf_tvar_set"
4407 : "_dwarf_tvar_get")
4408 + "_" + q->e.sym_name()
4409 + "_" + lex_cast(q->tick++));
4410
4411 q->result = synthetic_embedded_deref_call (q->sess, fname, code, type,
4412 q->userspace_p, q->lvalue,
4413 &q->e);
4414 }
4415 catch (const semantic_error& er)
4416 {
4417 // Here we suppress the error because we often just have too many
4418 // when scanning all the CUs.
4419 return DWARF_CB_OK;
4420 }
4421
4422 if (q->result) {
4423 return DWARF_CB_ABORT;
4424 }
4425
4426 return DWARF_CB_OK;
4427}
4428
4429
4430void
4431dwarf_atvar_query::handle_query_module ()
4432{
4433
4434 dw.iterate_over_cus(atvar_query_cu, this, false);
4435}
4436
4437
4438void
4439dwarf_atvar_expanding_visitor::visit_atvar_op (atvar_op* e)
4440{
4441 const bool lvalue = is_active_lvalue(e);
4442 if (lvalue && !s.guru_mode)
dc09353a 4443 throw SEMANTIC_ERROR(_("write to @var variable not permitted; "
bd1fcbad
YZ
4444 "need stap -g"), e->tok);
4445
4446 if (e->module.empty())
4447 e->module = "kernel";
4448
4449 functioncall* result = NULL;
4450
4451 // split the module string by ':' for alternatives
4452 vector<string> modules;
4453 tokenize(e->module, modules, ":");
4454 bool userspace_p = false;
4455 for (unsigned i = 0; !result && i < modules.size(); ++i)
4456 {
4457 string& module = modules[i];
4458
4459 dwflpp* dw;
4460 try
4461 {
4462 userspace_p = is_user_module(module);
4463 if (!userspace_p)
4464 {
4465 // kernel or kernel module target
4466 dw = db.get_kern_dw(s, module);
4467 }
4468 else
4469 {
4470 module = find_executable(module, "", s.sysenv);
4471 dw = db.get_user_dw(s, module);
4472 }
4473 }
4474 catch (const semantic_error& er)
4475 {
4476 /* ignore and go to the next module */
4477 continue;
4478 }
4479
4480 dwarf_atvar_query q (*dw, module, *e, userspace_p, lvalue, result, tick);
06de3a04 4481 dw->iterate_over_modules<base_query>(&query_module, &q);
bd1fcbad
YZ
4482
4483 if (result)
4484 {
4485 s.unwindsym_modules.insert(module);
bd1fcbad
YZ
4486
4487 if (lvalue)
4488 {
4489 // Provide the functioncall to our parent, so that it can be
4490 // used to substitute for the assignment node immediately above
4491 // us.
4492 assert(!target_symbol_setter_functioncalls.empty());
4493 *(target_symbol_setter_functioncalls.top()) = result;
4494 }
4495
4496 result->visit(this);
4497 return;
4498 }
4499
4500 /* Unable to find the variable in the current module, so we chain
4501 * an error in atvar_op */
dc09353a 4502 semantic_error er(ERR_SRC, _F("unable to find global '%s' in %s%s%s",
bd1fcbad
YZ
4503 e->sym_name().c_str(), module.c_str(),
4504 e->cu_name.empty() ? "" : _(", in "),
4505 e->cu_name.c_str()));
4506 e->chain (er);
4507 }
4508
4509 provide(e);
4510}
4511
4512
b8da0ad1
FCE
4513void
4514dwarf_derived_probe::printsig (ostream& o) const
4515{
4516 // Instead of just printing the plain locations, we add a PC value
4517 // as a comment as a way of telling e.g. apart multiple inlined
4518 // function instances. This is distinct from the verbose/clog
4519 // output, since this part goes into the cache hash calculations.
4520 sole_location()->print (o);
6d0f3f0c 4521 o << " /* pc=" << section << "+0x" << hex << addr << dec << " */";
b8da0ad1
FCE
4522 printsig_nested (o);
4523}
4524
4525
4526
dc38c0ae 4527void
b20febf3
FCE
4528dwarf_derived_probe::join_group (systemtap_session& s)
4529{
af234c40
JS
4530 // skip probes which are paired entry-handlers
4531 if (!has_return && (saved_longs || saved_strings))
4532 return;
4533
b20febf3
FCE
4534 if (! s.dwarf_derived_probes)
4535 s.dwarf_derived_probes = new dwarf_derived_probe_group ();
4536 s.dwarf_derived_probes->enroll (this);
4537}
4538
4539
2b69faaf
JS
4540static bool
4541kernel_supports_inode_uprobes(systemtap_session& s)
4542{
4543 // The arch-supports is new to the builtin inode-uprobes, so it makes a
4544 // reasonable indicator of the new API. Else we'll need an autoconf...
64e807c2 4545 // see also buildrun.cxx:kernel_built_uprobs()
2b69faaf
JS
4546 return (s.kernel_config["CONFIG_ARCH_SUPPORTS_UPROBES"] == "y"
4547 && s.kernel_config["CONFIG_UPROBES"] == "y");
4548}
4549
4550
3667d615
JS
4551static bool
4552kernel_supports_inode_uretprobes(systemtap_session& s)
4553{
766013af
JS
4554 // We need inode-uprobes first, then look for a sign of uretprobes. The only
4555 // non-static function at present is arch_uretprobe_hijack_return_addr.
3667d615 4556 return kernel_supports_inode_uprobes(s) &&
af9e147f 4557 (s.kernel_functions.count("arch_uretprobe_hijack_return_addr") > 0);
3667d615
JS
4558}
4559
4560
5261f7ab
DS
4561void
4562check_process_probe_kernel_support(systemtap_session& s)
4563{
4564 // If we've got utrace, we're good to go.
4565 if (s.kernel_config["CONFIG_UTRACE"] == "y")
4566 return;
4567
8c021542
DS
4568 // We don't have utrace. For process probes that aren't
4569 // uprobes-based, we just need the task_finder. The task_finder
b266d318
DS
4570 // needs CONFIG_TRACEPOINTS and specific tracepoints. There is a
4571 // specific autoconf test for its needs.
8c021542
DS
4572 //
4573 // We'll just require CONFIG_TRACEPOINTS here as a quick-and-dirty
4574 // approximation.
4575 if (! s.need_uprobes && s.kernel_config["CONFIG_TRACEPOINTS"] == "y")
4576 return;
4577
d3e959b0
DS
4578 // For uprobes-based process probes, we need the task_finder plus
4579 // the builtin inode-uprobes.
8c021542
DS
4580 if (s.need_uprobes
4581 && s.kernel_config["CONFIG_TRACEPOINTS"] == "y"
d3e959b0 4582 && kernel_supports_inode_uprobes(s))
8c021542
DS
4583 return;
4584
dc09353a 4585 throw SEMANTIC_ERROR (_("process probes not available without kernel CONFIG_UTRACE or CONFIG_TRACEPOINTS/CONFIG_ARCH_SUPPORTS_UPROBES/CONFIG_UPROBES"));
5261f7ab
DS
4586}
4587
4588
b20febf3
FCE
4589dwarf_derived_probe::dwarf_derived_probe(const string& funcname,
4590 const string& filename,
4591 int line,
91af0778 4592 // module & section specify a relocation
b20febf3
FCE
4593 // base for <addr>, unless section==""
4594 // (equivalently module=="kernel")
4595 const string& module,
4596 const string& section,
4597 // NB: dwfl_addr is the virtualized
4598 // address for this symbol.
4599 Dwarf_Addr dwfl_addr,
4600 // addr is the section-offset for
4601 // actual relocation.
4602 Dwarf_Addr addr,
4603 dwarf_query& q,
37ebca01 4604 Dwarf_Die* scope_die /* may be null */)
4c5d1300 4605 : derived_probe (q.base_probe, q.base_loc, true /* .components soon rewritten */ ),
b20febf3 4606 module (module), section (section), addr (addr),
63b4fd14 4607 path (q.path),
27dc09b1 4608 has_process (q.has_process),
c9bad430
DS
4609 has_return (q.has_return),
4610 has_maxactive (q.has_maxactive),
c57ea854 4611 has_library (q.has_library),
6b66b9f7 4612 maxactive_val (q.maxactive_val),
b642c901
SC
4613 user_path (q.user_path),
4614 user_lib (q.user_lib),
af234c40 4615 access_vars(false),
c57ea854 4616 saved_longs(0), saved_strings(0),
af234c40 4617 entry_handler(0)
bd2b1e68 4618{
b642c901
SC
4619 if (user_lib.size() != 0)
4620 has_library = true;
4621
6b66b9f7
JS
4622 if (q.has_process)
4623 {
4624 // We may receive probes on two types of ELF objects: ET_EXEC or ET_DYN.
4625 // ET_EXEC ones need no further relocation on the addr(==dwfl_addr), whereas
4626 // ET_DYN ones do (addr += run-time mmap base address). We tell these apart
4627 // by the incoming section value (".absolute" vs. ".dynamic").
4628 // XXX Assert invariants here too?
2b69faaf
JS
4629
4630 // inode-uprobes needs an offset rather than an absolute VM address.
4441e344 4631 // ditto for userspace runtimes (dyninst)
ac3af990 4632 if ((kernel_supports_inode_uprobes(q.dw.sess) || q.dw.sess.runtime_usermode_p()) &&
2b69faaf
JS
4633 section == ".absolute" && addr == dwfl_addr &&
4634 addr >= q.dw.module_start && addr < q.dw.module_end)
4635 this->addr = addr - q.dw.module_start;
6b66b9f7
JS
4636 }
4637 else
4638 {
4639 // Assert kernel relocation invariants
4640 if (section == "" && dwfl_addr != addr) // addr should be absolute
dc09353a 4641 throw SEMANTIC_ERROR (_("missing relocation basis"), tok);
6b66b9f7 4642 if (section != "" && dwfl_addr == addr) // addr should be an offset
dc09353a 4643 throw SEMANTIC_ERROR (_("inconsistent relocation address"), tok);
6b66b9f7 4644 }
2930abc7 4645
21beacc9
FCE
4646 // XXX: hack for strange g++/gcc's
4647#ifndef USHRT_MAX
4648#define USHRT_MAX 32767
4649#endif
4650
606fd9c8 4651 // Range limit maxactive() value
6b66b9f7 4652 if (has_maxactive && (maxactive_val < 0 || maxactive_val > USHRT_MAX))
dc09353a 4653 throw SEMANTIC_ERROR (_F("maxactive value out of range [0,%s]",
b530b5b3 4654 lex_cast(USHRT_MAX).c_str()), q.base_loc->components.front()->tok);
606fd9c8 4655
de688825 4656 // Expand target variables in the probe body
b78a0fbb 4657 if (!null_die(scope_die) || (!q.has_kernel && q.dw.has_gnu_debugdata()))
8fc05e57 4658 {
6b66b9f7 4659 // XXX: user-space deref's for q.has_process!
5a617dc6
JL
4660
4661 // PR14436: if we're expanding target variables in the probe body of a
4662 // .return probe, we need to make the expansion at the postprologue addr
4663 // instead (if any), which is then also the spot where the entry handler
4664 // probe is placed. (Note that at this point, a nonzero prologue_end
4665 // implies that it should be used, i.e. code is unoptimized).
4666 Dwarf_Addr handler_dwfl_addr = dwfl_addr;
4667 if (q.prologue_end != 0 && q.has_return)
4668 {
4669 handler_dwfl_addr = q.prologue_end;
4670 if (q.sess.verbose > 2)
4671 clog << _F("expanding .return vars at prologue_end (0x%s) "
4672 "rather than entrypc (0x%s)\n",
4673 lex_cast_hex(handler_dwfl_addr).c_str(),
4674 lex_cast_hex(dwfl_addr).c_str());
4675 }
4676 dwarf_var_expanding_visitor v (q, scope_die, handler_dwfl_addr);
8b095b45 4677 v.replace (this->body);
3689db05
SC
4678
4679 // Propagate perf.counters so we can emit later
4680 this->perf_counter_refs = v.perf_counter_refs;
4681 // Emit local var used to save the perf counter read value
698de6cc 4682 std::set<derived_probe*>::iterator pcii;
3689db05
SC
4683 for (pcii = v.perf_counter_refs.begin();
4684 pcii != v.perf_counter_refs.end(); pcii++)
4685 {
4686 map<string, pair<string,derived_probe*> >::iterator it;
4687 // Find the associated perf counter probe
4688 for (it=q.sess.perf_counters.begin() ;
4689 it != q.sess.perf_counters.end();
4690 it++)
4691 if ((*it).second.second == (*pcii))
4692 break;
4693 vardecl* vd = new vardecl;
4694 vd->name = "__perf_read_" + (*it).first;
4695 vd->tok = this->tok;
4696 vd->set_arity(0, this->tok);
4697 vd->type = pe_long;
4698 vd->synthetic = true;
4699 this->locals.push_back (vd);
4700 }
4701
4702
6b66b9f7
JS
4703 if (!q.has_process)
4704 access_vars = v.visited;
37ebca01
FCE
4705
4706 // If during target-variable-expanding the probe, we added a new block
4707 // of code, add it to the start of the probe.
4708 if (v.add_block)
ba6f838d 4709 this->body = new block(v.add_block, this->body);
2260f4e3
FCE
4710
4711 // If when target-variable-expanding the probe, we need to synthesize a
4712 // sibling function-entry probe. We don't go through the whole probe derivation
4713 // business (PR10642) that could lead to wildcard/alias resolution, or for that
4714 // dwarf-induced duplication.
4715 if (v.add_call_probe)
37ebca01 4716 {
2260f4e3
FCE
4717 assert (q.has_return && !q.has_call);
4718
4719 // We temporarily replace q.base_probe.
4720 statement* old_body = q.base_probe->body;
4721 q.base_probe->body = v.add_call_probe;
4722 q.has_return = false;
4723 q.has_call = true;
af234c40 4724
da23eceb 4725 if (q.has_process)
5a617dc6
JL
4726 {
4727 // Place handler probe at the same addr as where the vars were
4728 // expanded (which may not be the same addr as the one for the
4729 // main retprobe, PR14436).
4730 Dwarf_Addr handler_addr = addr;
4731 if (handler_dwfl_addr != dwfl_addr)
4732 // adjust section offset by prologue_end-entrypc
4733 handler_addr += handler_dwfl_addr - dwfl_addr;
4734 entry_handler = new uprobe_derived_probe (funcname, filename,
4735 line, module, section,
4736 handler_dwfl_addr,
4737 handler_addr, q,
4738 scope_die);
4739 }
da23eceb 4740 else
af234c40
JS
4741 entry_handler = new dwarf_derived_probe (funcname, filename, line,
4742 module, section, dwfl_addr,
4743 addr, q, scope_die);
4744
4745 saved_longs = entry_handler->saved_longs = v.saved_longs;
4746 saved_strings = entry_handler->saved_strings = v.saved_strings;
4747
4748 q.results.push_back (entry_handler);
2260f4e3
FCE
4749
4750 q.has_return = true;
4751 q.has_call = false;
4752 q.base_probe->body = old_body;
37ebca01 4753 }
f10534c6
WH
4754 // Save the local variables for listing mode
4755 if (q.sess.listing_mode_vars)
8c67c337 4756 saveargs(q, scope_die, dwfl_addr);
8fc05e57 4757 }
37ebca01 4758 // else - null scope_die - $target variables will produce an error during translate phase
8fc05e57 4759
f10534c6 4760 // PR10820: null scope die, local variables aren't accessible, not necessary to invoke saveargs
0a98fd42 4761
5d23847d 4762 // Reset the sole element of the "locations" vector as a
b20febf3
FCE
4763 // "reverse-engineered" form of the incoming (q.base_loc) probe
4764 // point. This allows a user to see what function / file / line
4765 // number any particular match of the wildcards.
cfa29998
JL
4766 this->sole_location()->components =
4767 q.canonicalize_comps(module, funcname, filename, line);
e2941743
JL
4768
4769 // if it's a .callee[s[(N)]] call, add checks to the probe body so that the
4770 // user body is only 'triggered' when called from q.callers[N-1], which
4771 // itself is called from q.callers[N-2], etc... I.E.
4772 // callees(N) --> N elements in q.callers --> N checks against [u]stack(0..N-1)
4773 if ((q.has_callee || q.has_callees_num) && q.callers && !q.callers->empty())
4774 {
4775 if (q.sess.verbose > 2)
7761da8b 4776 clog << _F("adding caller checks for callee %s\n", funcname.c_str());
e2941743
JL
4777
4778 // Copy the stack and empty it out
4779 stack<Dwarf_Addr> callers(*q.callers);
4780 for (unsigned level = 1; !callers.empty(); level++,
4781 callers.pop())
4782 {
4783 Dwarf_Addr caller = callers.top();
4784
4785 // We first need to make the caller addr relocatable
4786 string caller_section;
4787 Dwarf_Addr caller_reloc;
4788 if (module == TOK_KERNEL)
4789 { // allow for relocatable kernel (see also add_probe_point())
4790 caller_reloc = caller - q.sess.sym_stext;
4791 caller_section = "_stext";
4792 }
4793 else
4794 caller_reloc = q.dw.relocate_address(caller,
4795 caller_section);
4796
4797 if (q.sess.verbose > 2)
7761da8b
JL
4798 clog << _F("adding caller check [u]stack(%d) == reloc(0x%s)\n",
4799 level, lex_cast_hex(caller_reloc).c_str());
e2941743
JL
4800
4801 // We want to add a statement like this:
4802 // if (!caller_match(user, mod, sec, addr)) next;
4803 // Something similar is done in semantic_pass_conditions()
4804
4805 functioncall* check = new functioncall();
4806 check->tok = this->tok;
4807 check->function = "caller_match";
4808 check->args.push_back(new literal_number(q.has_process));
4809 check->args[0]->tok = this->tok;
4810 check->args.push_back(new literal_number(level));
4811 check->args[1]->tok = this->tok;
4812 check->args.push_back(new literal_string(module));
4813 check->args[2]->tok = this->tok;
4814 check->args.push_back(new literal_string(caller_section));
4815 check->args[3]->tok = this->tok;
4816 check->args.push_back(new literal_number(caller_reloc, true /* hex */));
4817 check->args[4]->tok = this->tok;
4818
4819 unary_expression* notexp = new unary_expression();
4820 notexp->tok = this->tok;
4821 notexp->op = "!";
4822 notexp->operand = check;
4823
4824 if_statement* ifs = new if_statement();
4825 ifs->tok = this->tok;
4826 ifs->thenblock = new next_statement();
4827 ifs->thenblock->tok = this->tok;
4828 ifs->elseblock = NULL;
4829 ifs->condition = notexp;
4830
4831 this->body = new block(ifs, this->body);
4832 }
4833 }
2930abc7
FCE
4834}
4835
bd2b1e68 4836
0a98fd42 4837void
8c67c337
JS
4838dwarf_derived_probe::saveargs(dwarf_query& q, Dwarf_Die* scope_die,
4839 Dwarf_Addr dwfl_addr)
0a98fd42 4840{
9aa8ffce 4841 if (null_die(scope_die))
0a98fd42 4842 return;
0a98fd42 4843
8c67c337 4844 bool verbose = q.sess.verbose > 2;
0a98fd42 4845
8c67c337 4846 if (verbose)
b530b5b3 4847 clog << _F("saveargs: examining '%s' (dieoffset: %#" PRIx64 ")\n", (dwarf_diename(scope_die)?: "unknown"), dwarf_dieoffset(scope_die));
0a98fd42 4848
8c67c337
JS
4849 if (has_return)
4850 {
4851 /* Only save the return value if it has a type. */
4852 string type_name;
4853 Dwarf_Die type_die;
4854 if (dwarf_attr_die (scope_die, DW_AT_type, &type_die) &&
4855 dwarf_type_name(&type_die, type_name))
4856 args.push_back("$return:"+type_name);
4857
4858 else if (verbose)
b530b5b3
LB
4859 clog << _F("saveargs: failed to retrieve type name for return value (dieoffset: %s)\n",
4860 lex_cast_hex(dwarf_dieoffset(scope_die)).c_str());
8c67c337 4861 }
d87623a1 4862
0a98fd42 4863 Dwarf_Die arg;
4ef35696
JS
4864 vector<Dwarf_Die> scopes = q.dw.getscopes(scope_die);
4865 for (unsigned i = 0; i < scopes.size(); ++i)
4866 {
4867 if (dwarf_tag(&scopes[i]) == DW_TAG_compile_unit)
4868 break; // we don't want file-level variables
4869 if (dwarf_child (&scopes[i], &arg) == 0)
4870 do
0a98fd42 4871 {
4ef35696
JS
4872 switch (dwarf_tag (&arg))
4873 {
4874 case DW_TAG_variable:
4875 case DW_TAG_formal_parameter:
4876 break;
0a98fd42 4877
4ef35696
JS
4878 default:
4879 continue;
4880 }
0a98fd42 4881
4ef35696
JS
4882 /* Ignore this local if it has no name. */
4883 const char *arg_name = dwarf_diename (&arg);
4884 if (!arg_name)
8c67c337
JS
4885 {
4886 if (verbose)
b530b5b3
LB
4887 clog << _F("saveargs: failed to retrieve name for local (dieoffset: %s)\n",
4888 lex_cast_hex(dwarf_dieoffset(&arg)).c_str());
8c67c337
JS
4889 continue;
4890 }
4ef35696
JS
4891
4892 if (verbose)
b530b5b3
LB
4893 clog << _F("saveargs: finding location for local '%s' (dieoffset: %s)\n",
4894 arg_name, lex_cast_hex(dwarf_dieoffset(&arg)).c_str());
4ef35696
JS
4895
4896 /* Ignore this local if it has no location (or not at this PC). */
4897 /* NB: It still may not be directly accessible, e.g. if it is an
4898 * aggregate type, implicit_pointer, etc., but the user can later
4899 * figure out how to access the interesting parts. */
45b02a36
FCE
4900
4901 /* XXX: Perhaps saveargs() / listings-mode should work by synthesizing
4902 * several synthetic
4903 * probe foo { $var }
4904 * probes, testing them for overall resolvability.
4905 */
4906
4ef35696
JS
4907 Dwarf_Attribute attr_mem;
4908 if (!dwarf_attr_integrate (&arg, DW_AT_const_value, &attr_mem))
4909 {
4910 Dwarf_Op *expr;
4911 size_t len;
4912 if (!dwarf_attr_integrate (&arg, DW_AT_location, &attr_mem))
4913 {
4914 if (verbose)
b530b5b3
LB
4915 clog << _F("saveargs: failed to resolve the location for local '%s' (dieoffset: %s)\n",
4916 arg_name, lex_cast_hex(dwarf_dieoffset(&arg)).c_str());
4ef35696
JS
4917 continue;
4918 }
4919 else if (!(dwarf_getlocation_addr(&attr_mem, dwfl_addr, &expr,
4920 &len, 1) == 1 && len > 0))
4921 {
45b02a36
FCE
4922 Dwarf_Addr dwfl_addr2 = q.dw.pr15123_retry_addr (dwfl_addr, & arg);
4923 if (!dwfl_addr2 || (!(dwarf_getlocation_addr(&attr_mem, dwfl_addr2, &expr,
4924 &len, 1) == 1 && len > 0))) {
4925 if (verbose)
4926 clog << _F("saveargs: local '%s' (dieoffset: %s) is not available at this address (%s)\n",
4927 arg_name, lex_cast_hex(dwarf_dieoffset(&arg)).c_str(), lex_cast_hex(dwfl_addr).c_str());
4928 continue;
4929 }
4ef35696
JS
4930 }
4931 }
4932
4933 /* Ignore this local if it has no type. */
4934 string type_name;
4935 Dwarf_Die type_die;
4936 if (!dwarf_attr_die (&arg, DW_AT_type, &type_die) ||
4937 !dwarf_type_name(&type_die, type_name))
8c67c337
JS
4938 {
4939 if (verbose)
b530b5b3
LB
4940 clog << _F("saveargs: failed to retrieve type name for local '%s' (dieoffset: %s)\n",
4941 arg_name, lex_cast_hex(dwarf_dieoffset(&arg)).c_str());
8c67c337
JS
4942 continue;
4943 }
8c67c337 4944
4ef35696
JS
4945 /* This local looks good -- save it! */
4946 args.push_back("$"+string(arg_name)+":"+type_name);
8c67c337 4947 }
4ef35696
JS
4948 while (dwarf_siblingof (&arg, &arg) == 0);
4949 }
0a98fd42
JS
4950}
4951
4952
4953void
d0bfd2ac 4954dwarf_derived_probe::getargs(std::list<std::string> &arg_set) const
0a98fd42 4955{
d0bfd2ac 4956 arg_set.insert(arg_set.end(), args.begin(), args.end());
0a98fd42
JS
4957}
4958
4959
27dc09b1 4960void
42e38653 4961dwarf_derived_probe::emit_privilege_assertion (translator_output* o)
27dc09b1
DB
4962{
4963 if (has_process)
4964 {
4965 // These probes are allowed for unprivileged users, but only in the
4966 // context of processes which they own.
4967 emit_process_owner_assertion (o);
4968 return;
4969 }
4970
4971 // Other probes must contain the default assertion which aborts
4972 // if executed by an unprivileged user.
42e38653 4973 derived_probe::emit_privilege_assertion (o);
27dc09b1
DB
4974}
4975
4976
4977void
4978dwarf_derived_probe::print_dupe_stamp(ostream& o)
4979{
4980 if (has_process)
4981 {
4982 // These probes are allowed for unprivileged users, but only in the
4983 // context of processes which they own.
4984 print_dupe_stamp_unprivileged_process_owner (o);
4985 return;
4986 }
4987
4988 // Other probes must contain the default dupe stamp
4989 derived_probe::print_dupe_stamp (o);
4990}
4991
64211010 4992
7a053d3b 4993void
20c6c071 4994dwarf_derived_probe::register_statement_variants(match_node * root,
27dc09b1 4995 dwarf_builder * dw,
42e38653 4996 privilege_t privilege)
bd2b1e68 4997{
27dc09b1 4998 root
42e38653 4999 ->bind_privilege(privilege)
27dc09b1 5000 ->bind(dw);
54efe513
GH
5001}
5002
7a053d3b 5003void
fd6602a0 5004dwarf_derived_probe::register_function_variants(match_node * root,
27dc09b1 5005 dwarf_builder * dw,
42e38653 5006 privilege_t privilege)
2865d17a 5007{
27dc09b1 5008 root
42e38653 5009 ->bind_privilege(privilege)
27dc09b1 5010 ->bind(dw);
27dc09b1 5011 root->bind(TOK_CALL)
42e38653 5012 ->bind_privilege(privilege)
27dc09b1 5013 ->bind(dw);
4bda987e
SC
5014 root->bind(TOK_EXPORTED)
5015 ->bind_privilege(privilege)
5016 ->bind(dw);
27dc09b1 5017 root->bind(TOK_RETURN)
42e38653 5018 ->bind_privilege(privilege)
27dc09b1 5019 ->bind(dw);
1e035395 5020
f6be7c06
DB
5021 // For process probes / uprobes, .maxactive() is unused.
5022 if (! pr_contains (privilege, pr_stapusr))
1e035395
FCE
5023 {
5024 root->bind(TOK_RETURN)
1e035395
FCE
5025 ->bind_num(TOK_MAXACTIVE)->bind(dw);
5026 }
bd2b1e68
GH
5027}
5028
7a053d3b 5029void
27dc09b1 5030dwarf_derived_probe::register_function_and_statement_variants(
440d9b00 5031 systemtap_session& s,
27dc09b1
DB
5032 match_node * root,
5033 dwarf_builder * dw,
42e38653 5034 privilege_t privilege
27dc09b1 5035)
bd2b1e68
GH
5036{
5037 // Here we match 4 forms:
5038 //
5039 // .function("foo")
5040 // .function(0xdeadbeef)
5041 // .statement("foo")
5042 // .statement(0xdeadbeef)
5043
440d9b00 5044 match_node *fv_root = root->bind_str(TOK_FUNCTION);
42e38653 5045 register_function_variants(fv_root, dw, privilege);
7f02ca94 5046 // ROOT.function("STRING") always gets the .inline and .label variants.
440d9b00 5047 fv_root->bind(TOK_INLINE)
42e38653 5048 ->bind_privilege(privilege)
440d9b00 5049 ->bind(dw);
7f02ca94
JS
5050 fv_root->bind_str(TOK_LABEL)
5051 ->bind_privilege(privilege)
440d9b00 5052 ->bind(dw);
c31add50
JL
5053 fv_root->bind_str(TOK_CALLEE)
5054 ->bind_privilege(privilege)
5055 ->bind(dw);
5056 fv_root->bind(TOK_CALLEES)
5057 ->bind_privilege(privilege)
5058 ->bind(dw);
5059 fv_root->bind_num(TOK_CALLEES)
5060 ->bind_privilege(privilege)
5061 ->bind(dw);
440d9b00
DB
5062
5063 fv_root = root->bind_num(TOK_FUNCTION);
42e38653 5064 register_function_variants(fv_root, dw, privilege);
440d9b00
DB
5065 // ROOT.function(NUMBER).inline is deprecated in release 1.7 and removed thereafter.
5066 if (strverscmp(s.compatible.c_str(), "1.7") <= 0)
5067 {
5068 fv_root->bind(TOK_INLINE)
42e38653 5069 ->bind_privilege(privilege)
440d9b00
DB
5070 ->bind(dw);
5071 }
5072
42e38653
DB
5073 register_statement_variants(root->bind_str(TOK_STATEMENT), dw, privilege);
5074 register_statement_variants(root->bind_num(TOK_STATEMENT), dw, privilege);
bd2b1e68
GH
5075}
5076
b1615c74
JS
5077void
5078dwarf_derived_probe::register_sdt_variants(systemtap_session& s,
5079 match_node * root,
5080 dwarf_builder * dw)
5081{
5082 root->bind_str(TOK_MARK)
f66bb29a 5083 ->bind_privilege(pr_all)
b1615c74
JS
5084 ->bind(dw);
5085 root->bind_str(TOK_PROVIDER)->bind_str(TOK_MARK)
f66bb29a 5086 ->bind_privilege(pr_all)
b1615c74
JS
5087 ->bind(dw);
5088}
5089
5090void
5091dwarf_derived_probe::register_plt_variants(systemtap_session& s,
5092 match_node * root,
5093 dwarf_builder * dw)
5094{
5095 root->bind(TOK_PLT)
f66bb29a 5096 ->bind_privilege(pr_all)
b1615c74
JS
5097 ->bind(dw);
5098 root->bind_str(TOK_PLT)
f66bb29a 5099 ->bind_privilege(pr_all)
b1615c74
JS
5100 ->bind(dw);
5101 root->bind(TOK_PLT)->bind_num(TOK_STATEMENT)
f66bb29a 5102 ->bind_privilege(pr_all)
b1615c74
JS
5103 ->bind(dw);
5104 root->bind_str(TOK_PLT)->bind_num(TOK_STATEMENT)
f66bb29a 5105 ->bind_privilege(pr_all)
b1615c74 5106 ->bind(dw);
bd2b1e68
GH
5107}
5108
5109void
c4ce66a1 5110dwarf_derived_probe::register_patterns(systemtap_session& s)
bd2b1e68 5111{
c4ce66a1 5112 match_node* root = s.pattern_root;
bd2b1e68
GH
5113 dwarf_builder *dw = new dwarf_builder();
5114
c4ce66a1
JS
5115 update_visitor *filter = new dwarf_cast_expanding_visitor(s, *dw);
5116 s.code_filters.push_back(filter);
5117
bd1fcbad
YZ
5118 filter = new dwarf_atvar_expanding_visitor(s, *dw);
5119 s.code_filters.push_back(filter);
5120
73f52eb4
DB
5121 register_function_and_statement_variants(s, root->bind(TOK_KERNEL), dw, pr_privileged);
5122 register_function_and_statement_variants(s, root->bind_str(TOK_MODULE), dw, pr_privileged);
27dc09b1
DB
5123 root->bind(TOK_KERNEL)->bind_num(TOK_STATEMENT)->bind(TOK_ABSOLUTE)
5124 ->bind(dw);
2cab6244 5125
7f02ca94
JS
5126 match_node* uprobes[] = {
5127 root->bind(TOK_PROCESS),
5128 root->bind_str(TOK_PROCESS),
5129 root->bind(TOK_PROCESS)->bind_str(TOK_LIBRARY),
5130 root->bind_str(TOK_PROCESS)->bind_str(TOK_LIBRARY),
5131 };
5132 for (size_t i = 0; i < sizeof(uprobes) / sizeof(*uprobes); ++i)
5133 {
f66bb29a 5134 register_function_and_statement_variants(s, uprobes[i], dw, pr_all);
7f02ca94
JS
5135 register_sdt_variants(s, uprobes[i], dw);
5136 register_plt_variants(s, uprobes[i], dw);
5137 }
bd2b1e68
GH
5138}
5139
9020300d 5140void
3689db05
SC
5141dwarf_derived_probe::emit_probe_local_init(systemtap_session& s, translator_output * o)
5142{
698de6cc 5143 std::set<derived_probe*>::iterator pcii;
3689db05
SC
5144 for (pcii = perf_counter_refs.begin();
5145 pcii != perf_counter_refs.end();
5146 pcii++)
5147 {
5148 map<string, pair<string,derived_probe*> >::iterator it;
5149 // Find the associated perf.counter probe
5150 unsigned i = 0;
5151 for (it=s.perf_counters.begin() ;
5152 it != s.perf_counters.end();
5153 it++, i++)
5154 if ((*it).second.second == (*pcii))
5155 break;
5156 // place the perf counter read so it precedes stp_lock_probe
5157 o->newline() << "l->l___perf_read_" + (*it).first
5158 + " = (((int64_t) (_stp_perf_read(smp_processor_id(),"
5159 + lex_cast(i) + "))));";
5160 }
b78a0fbb 5161
b95e2b79
MH
5162 if (access_vars)
5163 {
5164 // if accessing $variables, emit bsp cache setup for speeding up
d4670309 5165 o->newline() << "#if defined __ia64__";
d9aed31e 5166 o->newline() << "bspcache(c->unwaddr, c->kregs);";
d4670309 5167 o->newline() << "#endif";
b95e2b79 5168 }
9020300d 5169}
2930abc7 5170
b20febf3 5171// ------------------------------------------------------------------------
46b84a80
DS
5172
5173void
b20febf3 5174dwarf_derived_probe_group::enroll (dwarf_derived_probe* p)
46b84a80 5175{
b20febf3 5176 probes_by_module.insert (make_pair (p->module, p));
b8da0ad1
FCE
5177
5178 // XXX: probes put at the same address should all share a
5179 // single kprobe/kretprobe, and have their handlers executed
5180 // sequentially.
b55bc428
FCE
5181}
5182
7a053d3b 5183void
775d51e5 5184dwarf_derived_probe_group::emit_module_decls (systemtap_session& s)
ec4373ff 5185{
b20febf3 5186 if (probes_by_module.empty()) return;
2930abc7 5187
775d51e5
DS
5188 s.op->newline() << "/* ---- dwarf probes ---- */";
5189
5190 // Warn of misconfigured kernels
f41595cc
FCE
5191 s.op->newline() << "#if ! defined(CONFIG_KPROBES)";
5192 s.op->newline() << "#error \"Need CONFIG_KPROBES!\"";
5193 s.op->newline() << "#endif";
775d51e5 5194 s.op->newline();
f41595cc 5195
f07c3b68 5196 s.op->newline() << "#ifndef KRETACTIVE";
1ee6b5fc 5197 s.op->newline() << "#define KRETACTIVE (max(15,6*(int)num_possible_cpus()))";
f07c3b68
FCE
5198 s.op->newline() << "#endif";
5199
14cf7e42 5200 // Forward decls
2ba1736a 5201 s.op->newline() << "#include \"linux/kprobes-common.h\"";
14cf7e42 5202
b20febf3
FCE
5203 // Forward declare the master entry functions
5204 s.op->newline() << "static int enter_kprobe_probe (struct kprobe *inst,";
5205 s.op->line() << " struct pt_regs *regs);";
5206 s.op->newline() << "static int enter_kretprobe_probe (struct kretprobe_instance *inst,";
5207 s.op->line() << " struct pt_regs *regs);";
5208
42cb22bd
MH
5209 // Emit an array of kprobe/kretprobe pointers
5210 s.op->newline() << "#if defined(STAPCONF_UNREGISTER_KPROBES)";
5211 s.op->newline() << "static void * stap_unreg_kprobes[" << probes_by_module.size() << "];";
5212 s.op->newline() << "#endif";
5213
b20febf3 5214 // Emit the actual probe list.
606fd9c8
FCE
5215
5216 // NB: we used to plop a union { struct kprobe; struct kretprobe } into
5217 // struct stap_dwarf_probe, but it being initialized data makes it add
5218 // hundreds of bytes of padding per stap_dwarf_probe. (PR5673)
14cf7e42 5219 s.op->newline() << "static struct stap_dwarf_kprobe stap_dwarf_kprobes[" << probes_by_module.size() << "];";
606fd9c8
FCE
5220 // NB: bss!
5221
4c2732a1 5222 s.op->newline() << "static struct stap_dwarf_probe {";
b0986e7a
DS
5223 s.op->newline(1) << "const unsigned return_p:1;";
5224 s.op->newline() << "const unsigned maxactive_p:1;";
b350f56b 5225 s.op->newline() << "const unsigned optional_p:1;";
b20febf3 5226 s.op->newline() << "unsigned registered_p:1;";
b0986e7a 5227 s.op->newline() << "const unsigned short maxactive_val;";
606fd9c8 5228
af234c40
JS
5229 // data saved in the kretprobe_instance packet
5230 s.op->newline() << "const unsigned short saved_longs;";
5231 s.op->newline() << "const unsigned short saved_strings;";
5232
faea5e16 5233 // Let's find some stats for the embedded strings. Maybe they
606fd9c8
FCE
5234 // are small and uniform enough to justify putting char[MAX]'s into
5235 // the array instead of relocated char*'s.
faea5e16
JS
5236 size_t module_name_max = 0, section_name_max = 0;
5237 size_t module_name_tot = 0, section_name_tot = 0;
606fd9c8
FCE
5238 size_t all_name_cnt = probes_by_module.size(); // for average
5239 for (p_b_m_iterator it = probes_by_module.begin(); it != probes_by_module.end(); it++)
5240 {
5241 dwarf_derived_probe* p = it->second;
5242#define DOIT(var,expr) do { \
5243 size_t var##_size = (expr) + 1; \
5244 var##_max = max (var##_max, var##_size); \
5245 var##_tot += var##_size; } while (0)
5246 DOIT(module_name, p->module.size());
5247 DOIT(section_name, p->section.size());
606fd9c8
FCE
5248#undef DOIT
5249 }
5250
5251 // Decide whether it's worthwhile to use char[] or char* by comparing
5252 // the amount of average waste (max - avg) to the relocation data size
5253 // (3 native long words).
5254#define CALCIT(var) \
5255 if ((var##_name_max-(var##_name_tot/all_name_cnt)) < (3 * sizeof(void*))) \
5256 { \
5257 s.op->newline() << "const char " << #var << "[" << var##_name_max << "];"; \
5258 if (s.verbose > 2) clog << "stap_dwarf_probe " << #var \
5259 << "[" << var##_name_max << "]" << endl; \
5260 } \
5261 else \
5262 { \
b0986e7a 5263 s.op->newline() << "const char * const " << #var << ";"; \
606fd9c8
FCE
5264 if (s.verbose > 2) clog << "stap_dwarf_probe *" << #var << endl; \
5265 }
5266
5267 CALCIT(module);
5268 CALCIT(section);
e6fe60e7 5269#undef CALCIT
606fd9c8 5270
b0986e7a 5271 s.op->newline() << "const unsigned long address;";
7c3e97f4
JS
5272 s.op->newline() << "const struct stap_probe * const probe;";
5273 s.op->newline() << "const struct stap_probe * const entry_probe;";
b20febf3
FCE
5274 s.op->newline(-1) << "} stap_dwarf_probes[] = {";
5275 s.op->indent(1);
5276
5277 for (p_b_m_iterator it = probes_by_module.begin(); it != probes_by_module.end(); it++)
2930abc7 5278 {
b20febf3
FCE
5279 dwarf_derived_probe* p = it->second;
5280 s.op->newline() << "{";
5281 if (p->has_return)
5282 s.op->line() << " .return_p=1,";
c9bad430 5283 if (p->has_maxactive)
606fd9c8
FCE
5284 {
5285 s.op->line() << " .maxactive_p=1,";
5286 assert (p->maxactive_val >= 0 && p->maxactive_val <= USHRT_MAX);
5287 s.op->line() << " .maxactive_val=" << p->maxactive_val << ",";
5288 }
af234c40
JS
5289 if (p->saved_longs || p->saved_strings)
5290 {
5291 if (p->saved_longs)
5292 s.op->line() << " .saved_longs=" << p->saved_longs << ",";
5293 if (p->saved_strings)
5294 s.op->line() << " .saved_strings=" << p->saved_strings << ",";
5295 if (p->entry_handler)
c87ae2c1 5296 s.op->line() << " .entry_probe=" << common_probe_init (p->entry_handler) << ",";
af234c40 5297 }
b350f56b
JS
5298 if (p->locations[0]->optional)
5299 s.op->line() << " .optional_p=1,";
dc38c256 5300 s.op->line() << " .address=(unsigned long)0x" << hex << p->addr << dec << "ULL,";
84048984
FCE
5301 s.op->line() << " .module=\"" << p->module << "\",";
5302 s.op->line() << " .section=\"" << p->section << "\",";
faea5e16 5303 s.op->line() << " .probe=" << common_probe_init (p) << ",";
b20febf3 5304 s.op->line() << " },";
2930abc7 5305 }
2930abc7 5306
b20febf3
FCE
5307 s.op->newline(-1) << "};";
5308
5309 // Emit the kprobes callback function
5310 s.op->newline();
5311 s.op->newline() << "static int enter_kprobe_probe (struct kprobe *inst,";
5312 s.op->line() << " struct pt_regs *regs) {";
606fd9c8
FCE
5313 // NB: as of PR5673, the kprobe|kretprobe union struct is in BSS
5314 s.op->newline(1) << "int kprobe_idx = ((uintptr_t)inst-(uintptr_t)stap_dwarf_kprobes)/sizeof(struct stap_dwarf_kprobe);";
5315 // Check that the index is plausible
5316 s.op->newline() << "struct stap_dwarf_probe *sdp = &stap_dwarf_probes[";
5317 s.op->line() << "((kprobe_idx >= 0 && kprobe_idx < " << probes_by_module.size() << ")?";
5318 s.op->line() << "kprobe_idx:0)"; // NB: at least we avoid memory corruption
5319 // XXX: it would be nice to give a more verbose error though; BUG_ON later?
5320 s.op->line() << "];";
71db462b 5321 common_probe_entryfn_prologue (s, "STAP_SESSION_RUNNING", "sdp->probe",
cda141c2 5322 "stp_probe_type_kprobe");
d9aed31e 5323 s.op->newline() << "c->kregs = regs;";
6415ddde
MW
5324
5325 // Make it look like the IP is set as it wouldn't have been replaced
5326 // by a breakpoint instruction when calling real probe handler. Reset
5327 // IP regs on return, so we don't confuse kprobes. PR10458
5328 s.op->newline() << "{";
5329 s.op->indent(1);
d9aed31e 5330 s.op->newline() << "unsigned long kprobes_ip = REG_IP(c->kregs);";
259d54c0 5331 s.op->newline() << "SET_REG_IP(regs, (unsigned long) inst->addr);";
26e63673 5332 s.op->newline() << "(*sdp->probe->ph) (c);";
259d54c0 5333 s.op->newline() << "SET_REG_IP(regs, kprobes_ip);";
6415ddde
MW
5334 s.op->newline(-1) << "}";
5335
f887a8c9 5336 common_probe_entryfn_epilogue (s, true);
b20febf3
FCE
5337 s.op->newline() << "return 0;";
5338 s.op->newline(-1) << "}";
5339
5340 // Same for kretprobes
5341 s.op->newline();
af234c40
JS
5342 s.op->newline() << "static int enter_kretprobe_common (struct kretprobe_instance *inst,";
5343 s.op->line() << " struct pt_regs *regs, int entry) {";
b20febf3 5344 s.op->newline(1) << "struct kretprobe *krp = inst->rp;";
606fd9c8
FCE
5345
5346 // NB: as of PR5673, the kprobe|kretprobe union struct is in BSS
a36378d7 5347 s.op->newline() << "int kprobe_idx = ((uintptr_t)krp-(uintptr_t)stap_dwarf_kprobes)/sizeof(struct stap_dwarf_kprobe);";
606fd9c8
FCE
5348 // Check that the index is plausible
5349 s.op->newline() << "struct stap_dwarf_probe *sdp = &stap_dwarf_probes[";
5350 s.op->line() << "((kprobe_idx >= 0 && kprobe_idx < " << probes_by_module.size() << ")?";
5351 s.op->line() << "kprobe_idx:0)"; // NB: at least we avoid memory corruption
5352 // XXX: it would be nice to give a more verbose error though; BUG_ON later?
5353 s.op->line() << "];";
5354
7c3e97f4 5355 s.op->newline() << "const struct stap_probe *sp = entry ? sdp->entry_probe : sdp->probe;";
c87ae2c1
JS
5356 s.op->newline() << "if (sp) {";
5357 s.op->indent(1);
71db462b 5358 common_probe_entryfn_prologue (s, "STAP_SESSION_RUNNING", "sp",
cda141c2 5359 "stp_probe_type_kretprobe");
d9aed31e 5360 s.op->newline() << "c->kregs = regs;";
af234c40
JS
5361
5362 // for assisting runtime's backtrace logic and accessing kretprobe data packets
6dceb5c9
MW
5363 s.op->newline() << "c->ips.krp.pi = inst;";
5364 s.op->newline() << "c->ips.krp.pi_longs = sdp->saved_longs;";
6415ddde
MW
5365
5366 // Make it look like the IP is set as it wouldn't have been replaced
5367 // by a breakpoint instruction when calling real probe handler. Reset
5368 // IP regs on return, so we don't confuse kprobes. PR10458
5369 s.op->newline() << "{";
d9aed31e 5370 s.op->newline(1) << "unsigned long kprobes_ip = REG_IP(c->kregs);";
c87ae2c1
JS
5371 s.op->newline() << "if (entry)";
5372 s.op->newline(1) << "SET_REG_IP(regs, (unsigned long) inst->rp->kp.addr);";
5373 s.op->newline(-1) << "else";
5374 s.op->newline(1) << "SET_REG_IP(regs, (unsigned long)inst->ret_addr);";
5375 s.op->newline(-1) << "(sp->ph) (c);";
259d54c0 5376 s.op->newline() << "SET_REG_IP(regs, kprobes_ip);";
6415ddde
MW
5377 s.op->newline(-1) << "}";
5378
f887a8c9 5379 common_probe_entryfn_epilogue (s, true);
c87ae2c1 5380 s.op->newline(-1) << "}";
b20febf3
FCE
5381 s.op->newline() << "return 0;";
5382 s.op->newline(-1) << "}";
af234c40
JS
5383
5384 s.op->newline();
5385 s.op->newline() << "static int enter_kretprobe_probe (struct kretprobe_instance *inst,";
5386 s.op->line() << " struct pt_regs *regs) {";
5387 s.op->newline(1) << "return enter_kretprobe_common(inst, regs, 0);";
5388 s.op->newline(-1) << "}";
5389
5390 s.op->newline();
5391 s.op->newline() << "static int enter_kretprobe_entry_probe (struct kretprobe_instance *inst,";
5392 s.op->line() << " struct pt_regs *regs) {";
5393 s.op->newline(1) << "return enter_kretprobe_common(inst, regs, 1);";
5394 s.op->newline(-1) << "}";
b642c901 5395
14cf7e42 5396 s.op->newline();
20c6c071 5397}
ec4373ff 5398
20c6c071 5399
dc38c0ae 5400void
b20febf3
FCE
5401dwarf_derived_probe_group::emit_module_init (systemtap_session& s)
5402{
5403 s.op->newline() << "for (i=0; i<" << probes_by_module.size() << "; i++) {";
5404 s.op->newline(1) << "struct stap_dwarf_probe *sdp = & stap_dwarf_probes[i];";
a36378d7 5405 s.op->newline() << "struct stap_dwarf_kprobe *kp = & stap_dwarf_kprobes[i];";
a049e342 5406 s.op->newline() << "unsigned long relocated_addr = _stp_kmodule_relocate (sdp->module, sdp->section, sdp->address);";
b20febf3 5407 s.op->newline() << "if (relocated_addr == 0) continue;"; // quietly; assume module is absent
26e63673 5408 s.op->newline() << "probe_point = sdp->probe->pp;"; // for error messages
b20febf3 5409 s.op->newline() << "if (sdp->return_p) {";
606fd9c8 5410 s.op->newline(1) << "kp->u.krp.kp.addr = (void *) relocated_addr;";
c9bad430 5411 s.op->newline() << "if (sdp->maxactive_p) {";
606fd9c8 5412 s.op->newline(1) << "kp->u.krp.maxactive = sdp->maxactive_val;";
c9bad430 5413 s.op->newline(-1) << "} else {";
f07c3b68 5414 s.op->newline(1) << "kp->u.krp.maxactive = KRETACTIVE;";
c9bad430 5415 s.op->newline(-1) << "}";
606fd9c8 5416 s.op->newline() << "kp->u.krp.handler = &enter_kretprobe_probe;";
af234c40 5417 s.op->newline() << "#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,25)";
c87ae2c1 5418 s.op->newline() << "if (sdp->entry_probe) {";
af234c40
JS
5419 s.op->newline(1) << "kp->u.krp.entry_handler = &enter_kretprobe_entry_probe;";
5420 s.op->newline() << "kp->u.krp.data_size = sdp->saved_longs * sizeof(int64_t) + ";
5421 s.op->newline() << " sdp->saved_strings * MAXSTRINGLEN;";
5422 s.op->newline(-1) << "}";
5423 s.op->newline() << "#endif";
e4cb375f
MH
5424 // to ensure safeness of bspcache, always use aggr_kprobe on ia64
5425 s.op->newline() << "#ifdef __ia64__";
5426 s.op->newline() << "kp->dummy.addr = kp->u.krp.kp.addr;";
5427 s.op->newline() << "kp->dummy.pre_handler = NULL;";
5428 s.op->newline() << "rc = register_kprobe (& kp->dummy);";
5429 s.op->newline() << "if (rc == 0) {";
5430 s.op->newline(1) << "rc = register_kretprobe (& kp->u.krp);";
5431 s.op->newline() << "if (rc != 0)";
5432 s.op->newline(1) << "unregister_kprobe (& kp->dummy);";
5433 s.op->newline(-2) << "}";
5434 s.op->newline() << "#else";
606fd9c8 5435 s.op->newline() << "rc = register_kretprobe (& kp->u.krp);";
e4cb375f 5436 s.op->newline() << "#endif";
b20febf3 5437 s.op->newline(-1) << "} else {";
e4cb375f 5438 // to ensure safeness of bspcache, always use aggr_kprobe on ia64
606fd9c8
FCE
5439 s.op->newline(1) << "kp->u.kp.addr = (void *) relocated_addr;";
5440 s.op->newline() << "kp->u.kp.pre_handler = &enter_kprobe_probe;";
e4cb375f
MH
5441 s.op->newline() << "#ifdef __ia64__";
5442 s.op->newline() << "kp->dummy.addr = kp->u.kp.addr;";
5443 s.op->newline() << "kp->dummy.pre_handler = NULL;";
5444 s.op->newline() << "rc = register_kprobe (& kp->dummy);";
5445 s.op->newline() << "if (rc == 0) {";
5446 s.op->newline(1) << "rc = register_kprobe (& kp->u.kp);";
5447 s.op->newline() << "if (rc != 0)";
5448 s.op->newline(1) << "unregister_kprobe (& kp->dummy);";
5449 s.op->newline(-2) << "}";
5450 s.op->newline() << "#else";
606fd9c8 5451 s.op->newline() << "rc = register_kprobe (& kp->u.kp);";
e4cb375f 5452 s.op->newline() << "#endif";
b20febf3 5453 s.op->newline(-1) << "}";
9063462a
FCE
5454 s.op->newline() << "if (rc) {"; // PR6749: tolerate a failed register_*probe.
5455 s.op->newline(1) << "sdp->registered_p = 0;";
b350f56b 5456 s.op->newline() << "if (!sdp->optional_p)";
50b6acf7 5457 s.op->newline(1) << "_stp_warn (\"probe %s (address 0x%lx) registration error (rc %d)\", probe_point, (unsigned long) relocated_addr, rc);";
b350f56b 5458 s.op->newline(-1) << "rc = 0;"; // continue with other probes
9063462a
FCE
5459 // XXX: shall we increment numskipped?
5460 s.op->newline(-1) << "}";
5461
5462#if 0 /* pre PR 6749; XXX consider making an option */
c48cb0cc 5463 s.op->newline(1) << "for (j=i-1; j>=0; j--) {"; // partial rollback
b20febf3 5464 s.op->newline(1) << "struct stap_dwarf_probe *sdp2 = & stap_dwarf_probes[j];";
606fd9c8
FCE
5465 s.op->newline() << "struct stap_dwarf_kprobe *kp2 = & stap_dwarf_kprobes[j];";
5466 s.op->newline() << "if (sdp2->return_p) unregister_kretprobe (&kp2->u.krp);";
5467 s.op->newline() << "else unregister_kprobe (&kp2->u.kp);";
e4cb375f
MH
5468 s.op->newline() << "#ifdef __ia64__";
5469 s.op->newline() << "unregister_kprobe (&kp2->dummy);";
5470 s.op->newline() << "#endif";
c48cb0cc
FCE
5471 // NB: we don't have to clear sdp2->registered_p, since the module_exit code is
5472 // not run for this early-abort case.
5473 s.op->newline(-1) << "}";
5474 s.op->newline() << "break;"; // don't attempt to register any more probes
b20febf3 5475 s.op->newline(-1) << "}";
9063462a
FCE
5476#endif
5477
b20febf3
FCE
5478 s.op->newline() << "else sdp->registered_p = 1;";
5479 s.op->newline(-1) << "}"; // for loop
dc38c0ae
DS
5480}
5481
5482
b4be7cbc
FCE
5483void
5484dwarf_derived_probe_group::emit_module_refresh (systemtap_session& s)
5485{
5486 s.op->newline() << "for (i=0; i<" << probes_by_module.size() << "; i++) {";
5487 s.op->newline(1) << "struct stap_dwarf_probe *sdp = & stap_dwarf_probes[i];";
5488 s.op->newline() << "struct stap_dwarf_kprobe *kp = & stap_dwarf_kprobes[i];";
5489 s.op->newline() << "unsigned long relocated_addr = _stp_kmodule_relocate (sdp->module, sdp->section, sdp->address);";
5490 s.op->newline() << "int rc;";
5491
5492 // new module arrived?
5493 s.op->newline() << "if (sdp->registered_p == 0 && relocated_addr != 0) {";
5494 s.op->newline(1) << "if (sdp->return_p) {";
5495 s.op->newline(1) << "kp->u.krp.kp.addr = (void *) relocated_addr;";
5496 s.op->newline() << "if (sdp->maxactive_p) {";
5497 s.op->newline(1) << "kp->u.krp.maxactive = sdp->maxactive_val;";
5498 s.op->newline(-1) << "} else {";
5499 s.op->newline(1) << "kp->u.krp.maxactive = KRETACTIVE;";
5500 s.op->newline(-1) << "}";
5501 s.op->newline() << "kp->u.krp.handler = &enter_kretprobe_probe;";
5502 s.op->newline() << "#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,25)";
5503 s.op->newline() << "if (sdp->entry_probe) {";
5504 s.op->newline(1) << "kp->u.krp.entry_handler = &enter_kretprobe_entry_probe;";
5505 s.op->newline() << "kp->u.krp.data_size = sdp->saved_longs * sizeof(int64_t) + ";
5506 s.op->newline() << " sdp->saved_strings * MAXSTRINGLEN;";
5507 s.op->newline(-1) << "}";
5508 s.op->newline() << "#endif";
5509 // to ensure safeness of bspcache, always use aggr_kprobe on ia64
5510 s.op->newline() << "#ifdef __ia64__";
5511 s.op->newline() << "kp->dummy.addr = kp->u.krp.kp.addr;";
5512 s.op->newline() << "kp->dummy.pre_handler = NULL;";
5513 s.op->newline() << "rc = register_kprobe (& kp->dummy);";
5514 s.op->newline() << "if (rc == 0) {";
5515 s.op->newline(1) << "rc = register_kretprobe (& kp->u.krp);";
5516 s.op->newline() << "if (rc != 0)";
5517 s.op->newline(1) << "unregister_kprobe (& kp->dummy);";
5518 s.op->newline(-2) << "}";
5519 s.op->newline() << "#else";
5520 s.op->newline() << "rc = register_kretprobe (& kp->u.krp);";
5521 s.op->newline() << "#endif";
5522 s.op->newline(-1) << "} else {";
5523 // to ensure safeness of bspcache, always use aggr_kprobe on ia64
5524 s.op->newline(1) << "kp->u.kp.addr = (void *) relocated_addr;";
5525 s.op->newline() << "kp->u.kp.pre_handler = &enter_kprobe_probe;";
5526 s.op->newline() << "#ifdef __ia64__";
5527 s.op->newline() << "kp->dummy.addr = kp->u.kp.addr;";
5528 s.op->newline() << "kp->dummy.pre_handler = NULL;";
5529 s.op->newline() << "rc = register_kprobe (& kp->dummy);";
5530 s.op->newline() << "if (rc == 0) {";
5531 s.op->newline(1) << "rc = register_kprobe (& kp->u.kp);";
5532 s.op->newline() << "if (rc != 0)";
5533 s.op->newline(1) << "unregister_kprobe (& kp->dummy);";
5534 s.op->newline(-2) << "}";
5535 s.op->newline() << "#else";
5536 s.op->newline() << "rc = register_kprobe (& kp->u.kp);";
5537 s.op->newline() << "#endif";
5538 s.op->newline(-1) << "}";
5539 s.op->newline() << "if (rc == 0) sdp->registered_p = 1;";
5540
5541 // old module disappeared?
5542 s.op->newline(-1) << "} else if (sdp->registered_p == 1 && relocated_addr == 0) {";
5543 s.op->newline(1) << "if (sdp->return_p) {";
5544 s.op->newline(1) << "unregister_kretprobe (&kp->u.krp);";
065d5567 5545 s.op->newline() << "atomic_add (kp->u.krp.nmissed, skipped_count());";
b4be7cbc
FCE
5546 s.op->newline() << "#ifdef STP_TIMING";
5547 s.op->newline() << "if (kp->u.krp.nmissed)";
5548 s.op->newline(1) << "_stp_warn (\"Skipped due to missed kretprobe/1 on '%s': %d\\n\", sdp->probe->pp, kp->u.krp.nmissed);";
5549 s.op->newline(-1) << "#endif";
065d5567 5550 s.op->newline() << "atomic_add (kp->u.krp.kp.nmissed, skipped_count());";
b4be7cbc
FCE
5551 s.op->newline() << "#ifdef STP_TIMING";
5552 s.op->newline() << "if (kp->u.krp.kp.nmissed)";
5553 s.op->newline(1) << "_stp_warn (\"Skipped due to missed kretprobe/2 on '%s': %lu\\n\", sdp->probe->pp, kp->u.krp.kp.nmissed);";
5554 s.op->newline(-1) << "#endif";
5555 s.op->newline(-1) << "} else {";
5556 s.op->newline(1) << "unregister_kprobe (&kp->u.kp);";
065d5567 5557 s.op->newline() << "atomic_add (kp->u.kp.nmissed, skipped_count());";
b4be7cbc
FCE
5558 s.op->newline() << "#ifdef STP_TIMING";
5559 s.op->newline() << "if (kp->u.kp.nmissed)";
5560 s.op->newline(1) << "_stp_warn (\"Skipped due to missed kprobe on '%s': %lu\\n\", sdp->probe->pp, kp->u.kp.nmissed);";
5561 s.op->newline(-1) << "#endif";
5562 s.op->newline(-1) << "}";
5563 s.op->newline() << "#if defined(__ia64__)";
5564 s.op->newline() << "unregister_kprobe (&kp->dummy);";
5565 s.op->newline() << "#endif";
5566 s.op->newline() << "sdp->registered_p = 0;";
5567 s.op->newline(-1) << "}";
5568
5569 s.op->newline(-1) << "}"; // for loop
5570}
5571
5572
5573
5574
46b84a80 5575void
b20febf3 5576dwarf_derived_probe_group::emit_module_exit (systemtap_session& s)
46b84a80 5577{
42cb22bd
MH
5578 //Unregister kprobes by batch interfaces.
5579 s.op->newline() << "#if defined(STAPCONF_UNREGISTER_KPROBES)";
5580 s.op->newline() << "j = 0;";
5581 s.op->newline() << "for (i=0; i<" << probes_by_module.size() << "; i++) {";
5582 s.op->newline(1) << "struct stap_dwarf_probe *sdp = & stap_dwarf_probes[i];";
5583 s.op->newline() << "struct stap_dwarf_kprobe *kp = & stap_dwarf_kprobes[i];";
5584 s.op->newline() << "if (! sdp->registered_p) continue;";
5585 s.op->newline() << "if (!sdp->return_p)";
5586 s.op->newline(1) << "stap_unreg_kprobes[j++] = &kp->u.kp;";
5587 s.op->newline(-2) << "}";
5588 s.op->newline() << "unregister_kprobes((struct kprobe **)stap_unreg_kprobes, j);";
5589 s.op->newline() << "j = 0;";
5590 s.op->newline() << "for (i=0; i<" << probes_by_module.size() << "; i++) {";
5591 s.op->newline(1) << "struct stap_dwarf_probe *sdp = & stap_dwarf_probes[i];";
5592 s.op->newline() << "struct stap_dwarf_kprobe *kp = & stap_dwarf_kprobes[i];";
5593 s.op->newline() << "if (! sdp->registered_p) continue;";
5594 s.op->newline() << "if (sdp->return_p)";
5595 s.op->newline(1) << "stap_unreg_kprobes[j++] = &kp->u.krp;";
5596 s.op->newline(-2) << "}";
5597 s.op->newline() << "unregister_kretprobes((struct kretprobe **)stap_unreg_kprobes, j);";
e4cb375f
MH
5598 s.op->newline() << "#ifdef __ia64__";
5599 s.op->newline() << "j = 0;";
5600 s.op->newline() << "for (i=0; i<" << probes_by_module.size() << "; i++) {";
5601 s.op->newline(1) << "struct stap_dwarf_probe *sdp = & stap_dwarf_probes[i];";
5602 s.op->newline() << "struct stap_dwarf_kprobe *kp = & stap_dwarf_kprobes[i];";
5603 s.op->newline() << "if (! sdp->registered_p) continue;";
5604 s.op->newline() << "stap_unreg_kprobes[j++] = &kp->dummy;";
5605 s.op->newline(-1) << "}";
5606 s.op->newline() << "unregister_kprobes((struct kprobe **)stap_unreg_kprobes, j);";
5607 s.op->newline() << "#endif";
42cb22bd
MH
5608 s.op->newline() << "#endif";
5609
b20febf3
FCE
5610 s.op->newline() << "for (i=0; i<" << probes_by_module.size() << "; i++) {";
5611 s.op->newline(1) << "struct stap_dwarf_probe *sdp = & stap_dwarf_probes[i];";
a36378d7 5612 s.op->newline() << "struct stap_dwarf_kprobe *kp = & stap_dwarf_kprobes[i];";
b20febf3
FCE
5613 s.op->newline() << "if (! sdp->registered_p) continue;";
5614 s.op->newline() << "if (sdp->return_p) {";
42cb22bd 5615 s.op->newline() << "#if !defined(STAPCONF_UNREGISTER_KPROBES)";
606fd9c8 5616 s.op->newline(1) << "unregister_kretprobe (&kp->u.krp);";
42cb22bd 5617 s.op->newline() << "#endif";
065d5567 5618 s.op->newline() << "atomic_add (kp->u.krp.nmissed, skipped_count());";
73209876
FCE
5619 s.op->newline() << "#ifdef STP_TIMING";
5620 s.op->newline() << "if (kp->u.krp.nmissed)";
26e63673 5621 s.op->newline(1) << "_stp_warn (\"Skipped due to missed kretprobe/1 on '%s': %d\\n\", sdp->probe->pp, kp->u.krp.nmissed);";
73209876 5622 s.op->newline(-1) << "#endif";
065d5567 5623 s.op->newline() << "atomic_add (kp->u.krp.kp.nmissed, skipped_count());";
73209876
FCE
5624 s.op->newline() << "#ifdef STP_TIMING";
5625 s.op->newline() << "if (kp->u.krp.kp.nmissed)";
26e63673 5626 s.op->newline(1) << "_stp_warn (\"Skipped due to missed kretprobe/2 on '%s': %lu\\n\", sdp->probe->pp, kp->u.krp.kp.nmissed);";
73209876 5627 s.op->newline(-1) << "#endif";
557fb7a8 5628 s.op->newline(-1) << "} else {";
42cb22bd 5629 s.op->newline() << "#if !defined(STAPCONF_UNREGISTER_KPROBES)";
606fd9c8 5630 s.op->newline(1) << "unregister_kprobe (&kp->u.kp);";
42cb22bd 5631 s.op->newline() << "#endif";
065d5567 5632 s.op->newline() << "atomic_add (kp->u.kp.nmissed, skipped_count());";
73209876
FCE
5633 s.op->newline() << "#ifdef STP_TIMING";
5634 s.op->newline() << "if (kp->u.kp.nmissed)";
26e63673 5635 s.op->newline(1) << "_stp_warn (\"Skipped due to missed kprobe on '%s': %lu\\n\", sdp->probe->pp, kp->u.kp.nmissed);";
73209876 5636 s.op->newline(-1) << "#endif";
b20febf3 5637 s.op->newline(-1) << "}";
e4cb375f
MH
5638 s.op->newline() << "#if !defined(STAPCONF_UNREGISTER_KPROBES) && defined(__ia64__)";
5639 s.op->newline() << "unregister_kprobe (&kp->dummy);";
5640 s.op->newline() << "#endif";
b20febf3
FCE
5641 s.op->newline() << "sdp->registered_p = 0;";
5642 s.op->newline(-1) << "}";
46b84a80
DS
5643}
5644
272c9036
WF
5645static void sdt_v3_tokenize(const string& str, vector<string>& tokens)
5646{
5647 string::size_type pos;
5648 string::size_type lastPos = str.find_first_not_of(" ", 0);
5649 string::size_type nextAt = str.find("@", lastPos);
e9a90eee
JS
5650
5651 if (nextAt == string::npos)
5652 {
5653 // PR13934: Assembly probes are not forced to use the N@OP form.
5654 // In this case, N is inferred to be the native word size. Since we
5655 // don't have a nice delimiter, just split it on spaces. SDT-asm authors
5656 // then must not put any spaces in arguments, to avoid ambiguity.
5657 tokenize(str, tokens, " ");
5658 return;
5659 }
5660
272c9036
WF
5661 while (lastPos != string::npos)
5662 {
5663 pos = nextAt + 1;
5664 nextAt = str.find("@", pos);
5665 if (nextAt == string::npos)
5666 pos = string::npos;
5667 else
5668 pos = str.rfind(" ", nextAt);
5669
5670 tokens.push_back(str.substr(lastPos, pos - lastPos));
5671 lastPos = str.find_first_not_of(" ", pos);
5672 }
5673}
8aabf152 5674
8aabf152 5675
aff5d390 5676struct sdt_uprobe_var_expanding_visitor: public var_expanding_visitor
7a05f484 5677{
ae1418f0 5678 enum regwidths {QI, QIh, HI, SI, DI};
f83336a5
FCE
5679 sdt_uprobe_var_expanding_visitor(systemtap_session& s,
5680 int elf_machine,
5681 const string & process_name,
a794dbeb 5682 const string & provider_name,
aff5d390 5683 const string & probe_name,
71e5e13d 5684 stap_sdt_probe_type probe_type,
aff5d390 5685 const string & arg_string,
8aabf152 5686 int ac):
332ba7e7 5687 session (s), elf_machine (elf_machine), process_name (process_name),
71e5e13d
SC
5688 provider_name (provider_name), probe_name (probe_name),
5689 probe_type (probe_type), arg_count ((unsigned) ac)
a8ec7719 5690 {
f83336a5
FCE
5691 /* Register name mapping table depends on the elf machine of this particular
5692 probe target process/file, not upon the host. So we can't just
5693 #ifdef _i686_ etc. */
ae1418f0
FCE
5694
5695#define DRI(name,num,width) dwarf_regs[name]=make_pair(num,width)
f83336a5 5696 if (elf_machine == EM_X86_64) {
b78a0fbb
LB
5697 DRI ("%rax", 0, DI); DRI ("%eax", 0, SI); DRI ("%ax", 0, HI);
5698 DRI ("%al", 0, QI); DRI ("%ah", 0, QIh);
46a94997
SC
5699 DRI ("%rdx", 1, DI); DRI ("%edx", 1, SI); DRI ("%dx", 1, HI);
5700 DRI ("%dl", 1, QI); DRI ("%dh", 1, QIh);
5701 DRI ("%rcx", 2, DI); DRI ("%ecx", 2, SI); DRI ("%cx", 2, HI);
5702 DRI ("%cl", 2, QI); DRI ("%ch", 2, QIh);
5703 DRI ("%rbx", 3, DI); DRI ("%ebx", 3, SI); DRI ("%bx", 3, HI);
b78a0fbb
LB
5704 DRI ("%bl", 3, QI); DRI ("%bh", 3, QIh);
5705 DRI ("%rsi", 4, DI); DRI ("%esi", 4, SI); DRI ("%si", 4, HI);
46a94997
SC
5706 DRI ("%sil", 4, QI);
5707 DRI ("%rdi", 5, DI); DRI ("%edi", 5, SI); DRI ("%di", 5, HI);
5708 DRI ("%dil", 5, QI);
5709 DRI ("%rbp", 6, DI); DRI ("%ebp", 6, SI); DRI ("%bp", 6, HI);
2c7281e7 5710 DRI ("%bpl", 6, QI);
46a94997 5711 DRI ("%rsp", 7, DI); DRI ("%esp", 7, SI); DRI ("%sp", 7, HI);
2c7281e7 5712 DRI ("%spl", 7, QI);
46a94997
SC
5713 DRI ("%r8", 8, DI); DRI ("%r8d", 8, SI); DRI ("%r8w", 8, HI);
5714 DRI ("%r8b", 8, QI);
5715 DRI ("%r9", 9, DI); DRI ("%r9d", 9, SI); DRI ("%r9w", 9, HI);
5716 DRI ("%r9b", 9, QI);
5717 DRI ("%r10", 10, DI); DRI ("%r10d", 10, SI); DRI ("%r10w", 10, HI);
5718 DRI ("%r10b", 10, QI);
5719 DRI ("%r11", 11, DI); DRI ("%r11d", 11, SI); DRI ("%r11w", 11, HI);
5720 DRI ("%r11b", 11, QI);
5721 DRI ("%r12", 12, DI); DRI ("%r12d", 12, SI); DRI ("%r12w", 12, HI);
5722 DRI ("%r12b", 12, QI);
5723 DRI ("%r13", 13, DI); DRI ("%r13d", 13, SI); DRI ("%r13w", 13, HI);
5724 DRI ("%r13b", 13, QI);
5725 DRI ("%r14", 14, DI); DRI ("%r14d", 14, SI); DRI ("%r14w", 14, HI);
5726 DRI ("%r14b", 14, QI);
5727 DRI ("%r15", 15, DI); DRI ("%r15d", 15, SI); DRI ("%r15w", 15, HI);
5728 DRI ("%r15b", 15, QI);
f83336a5 5729 } else if (elf_machine == EM_386) {
46a94997
SC
5730 DRI ("%eax", 0, SI); DRI ("%ax", 0, HI); DRI ("%al", 0, QI);
5731 DRI ("%ah", 0, QIh);
5732 DRI ("%ecx", 1, SI); DRI ("%cx", 1, HI); DRI ("%cl", 1, QI);
5733 DRI ("%ch", 1, QIh);
5734 DRI ("%edx", 2, SI); DRI ("%dx", 2, HI); DRI ("%dl", 2, QI);
5735 DRI ("%dh", 2, QIh);
5736 DRI ("%ebx", 3, SI); DRI ("%bx", 3, HI); DRI ("%bl", 3, QI);
5737 DRI ("%bh", 3, QIh);
b78a0fbb 5738 DRI ("%esp", 4, SI); DRI ("%sp", 4, HI);
46a94997
SC
5739 DRI ("%ebp", 5, SI); DRI ("%bp", 5, HI);
5740 DRI ("%esi", 6, SI); DRI ("%si", 6, HI); DRI ("%sil", 6, QI);
5741 DRI ("%edi", 7, SI); DRI ("%di", 7, HI); DRI ("%dil", 7, QI);
0491c523 5742 } else if (elf_machine == EM_PPC || elf_machine == EM_PPC64) {
46a94997
SC
5743 DRI ("%r0", 0, DI);
5744 DRI ("%r1", 1, DI);
5745 DRI ("%r2", 2, DI);
5746 DRI ("%r3", 3, DI);
5747 DRI ("%r4", 4, DI);
5748 DRI ("%r5", 5, DI);
5749 DRI ("%r6", 6, DI);
5750 DRI ("%r7", 7, DI);
5751 DRI ("%r8", 8, DI);
5752 DRI ("%r9", 9, DI);
5753 DRI ("%r10", 10, DI);
5754 DRI ("%r11", 11, DI);
5755 DRI ("%r12", 12, DI);
5756 DRI ("%r13", 13, DI);
5757 DRI ("%r14", 14, DI);
5758 DRI ("%r15", 15, DI);
5759 DRI ("%r16", 16, DI);
5760 DRI ("%r17", 17, DI);
5761 DRI ("%r18", 18, DI);
5762 DRI ("%r19", 19, DI);
5763 DRI ("%r20", 20, DI);
5764 DRI ("%r21", 21, DI);
5765 DRI ("%r22", 22, DI);
5766 DRI ("%r23", 23, DI);
5767 DRI ("%r24", 24, DI);
5768 DRI ("%r25", 25, DI);
5769 DRI ("%r26", 26, DI);
5770 DRI ("%r27", 27, DI);
5771 DRI ("%r28", 28, DI);
5772 DRI ("%r29", 29, DI);
5773 DRI ("%r30", 30, DI);
5774 DRI ("%r31", 31, DI);
8aabf152 5775 // PR11821: unadorned register "names" without -mregnames
46a94997
SC
5776 DRI ("0", 0, DI);
5777 DRI ("1", 1, DI);
5778 DRI ("2", 2, DI);
5779 DRI ("3", 3, DI);
5780 DRI ("4", 4, DI);
5781 DRI ("5", 5, DI);
5782 DRI ("6", 6, DI);
5783 DRI ("7", 7, DI);
5784 DRI ("8", 8, DI);
5785 DRI ("9", 9, DI);
5786 DRI ("10", 10, DI);
5787 DRI ("11", 11, DI);
5788 DRI ("12", 12, DI);
5789 DRI ("13", 13, DI);
5790 DRI ("14", 14, DI);
5791 DRI ("15", 15, DI);
5792 DRI ("16", 16, DI);
5793 DRI ("17", 17, DI);
5794 DRI ("18", 18, DI);
5795 DRI ("19", 19, DI);
5796 DRI ("20", 20, DI);
5797 DRI ("21", 21, DI);
5798 DRI ("22", 22, DI);
5799 DRI ("23", 23, DI);
5800 DRI ("24", 24, DI);
5801 DRI ("25", 25, DI);
5802 DRI ("26", 26, DI);
5803 DRI ("27", 27, DI);
5804 DRI ("28", 28, DI);
5805 DRI ("29", 29, DI);
5806 DRI ("30", 30, DI);
5807 DRI ("31", 31, DI);
14900130 5808 } else if (elf_machine == EM_S390) {
46a94997
SC
5809 DRI ("%r0", 0, DI);
5810 DRI ("%r1", 1, DI);
5811 DRI ("%r2", 2, DI);
5812 DRI ("%r3", 3, DI);
5813 DRI ("%r4", 4, DI);
5814 DRI ("%r5", 5, DI);
5815 DRI ("%r6", 6, DI);
5816 DRI ("%r7", 7, DI);
5817 DRI ("%r8", 8, DI);
5818 DRI ("%r9", 9, DI);
5819 DRI ("%r10", 10, DI);
5820 DRI ("%r11", 11, DI);
5821 DRI ("%r12", 12, DI);
5822 DRI ("%r13", 13, DI);
5823 DRI ("%r14", 14, DI);
5824 DRI ("%r15", 15, DI);
272c9036
WF
5825 } else if (elf_machine == EM_ARM) {
5826 DRI ("r0", 0, SI);
5827 DRI ("r1", 1, SI);
5828 DRI ("r2", 2, SI);
5829 DRI ("r3", 3, SI);
5830 DRI ("r4", 4, SI);
5831 DRI ("r5", 5, SI);
5832 DRI ("r6", 6, SI);
5833 DRI ("r7", 7, SI);
5834 DRI ("r8", 8, SI);
5835 DRI ("r9", 9, SI);
669a2feb 5836 DRI ("r10", 10, SI); DRI ("sl", 10, SI);
272c9036
WF
5837 DRI ("fp", 11, SI);
5838 DRI ("ip", 12, SI);
5839 DRI ("sp", 13, SI);
5840 DRI ("lr", 14, SI);
5841 DRI ("pc", 15, SI);
86aae990
WC
5842 } else if (elf_machine == EM_AARCH64) {
5843 DRI ("x0", 0, DI); DRI ("w0", 0, SI);
5844 DRI ("x1", 1, DI); DRI ("w1", 1, SI);
5845 DRI ("x2", 2, DI); DRI ("w2", 2, SI);
5846 DRI ("x3", 3, DI); DRI ("w3", 3, SI);
5847 DRI ("x4", 4, DI); DRI ("w4", 4, SI);
5848 DRI ("x5", 5, DI); DRI ("w5", 5, SI);
5849 DRI ("x6", 6, DI); DRI ("w6", 6, SI);
5850 DRI ("x7", 7, DI); DRI ("w7", 7, SI);
5851 DRI ("x8", 8, DI); DRI ("w8", 8, SI);
5852 DRI ("x9", 9, DI); DRI ("w9", 9, SI);
5853 DRI ("x10", 10, DI); DRI ("w10", 10, SI);
5854 DRI ("x11", 11, DI); DRI ("w11", 11, SI);
5855 DRI ("x12", 12, DI); DRI ("w12", 12, SI);
5856 DRI ("x13", 13, DI); DRI ("w13", 13, SI);
5857 DRI ("x14", 14, DI); DRI ("w14", 14, SI);
5858 DRI ("x15", 15, DI); DRI ("w15", 15, SI);
5859 DRI ("x16", 16, DI); DRI ("w16", 16, SI);
5860 DRI ("x17", 17, DI); DRI ("w17", 17, SI);
5861 DRI ("x18", 18, DI); DRI ("w18", 18, SI);
5862 DRI ("x19", 19, DI); DRI ("w19", 19, SI);
5863 DRI ("x20", 20, DI); DRI ("w20", 20, SI);
5864 DRI ("x21", 21, DI); DRI ("w21", 21, SI);
5865 DRI ("x22", 22, DI); DRI ("w22", 22, SI);
5866 DRI ("x23", 23, DI); DRI ("w23", 23, SI);
5867 DRI ("x24", 24, DI); DRI ("w24", 24, SI);
5868 DRI ("x25", 25, DI); DRI ("w25", 25, SI);
5869 DRI ("x26", 26, DI); DRI ("w26", 26, SI);
5870 DRI ("x27", 27, DI); DRI ("w27", 27, SI);
5871 DRI ("x28", 28, DI); DRI ("w28", 28, SI);
5872 DRI ("x29", 29, DI); DRI ("w29", 29, SI);
5873 DRI ("x30", 30, DI); DRI ("w30", 30, SI);
5874 DRI ("sp", 31, DI);
14900130 5875 } else if (arg_count) {
8aabf152 5876 /* permit this case; just fall back to dwarf */
f83336a5 5877 }
ae1418f0 5878#undef DRI
f83336a5 5879
ebbd2b45 5880 need_debug_info = false;
88e39987 5881 if (probe_type == uprobe3_type)
272c9036
WF
5882 {
5883 sdt_v3_tokenize(arg_string, arg_tokens);
5884 assert(arg_count <= 12);
5885 }
88e39987 5886 else
272c9036
WF
5887 {
5888 tokenize(arg_string, arg_tokens, " ");
5889 assert(arg_count <= 10);
5890 }
a8ec7719 5891 }
8aabf152 5892
f83336a5 5893 systemtap_session& session;
332ba7e7 5894 int elf_machine;
aff5d390 5895 const string & process_name;
a794dbeb 5896 const string & provider_name;
aff5d390 5897 const string & probe_name;
71e5e13d 5898 stap_sdt_probe_type probe_type;
8aabf152 5899 unsigned arg_count;
aff5d390 5900 vector<string> arg_tokens;
46a94997 5901 map<string, pair<unsigned,int> > dwarf_regs;
ebbd2b45 5902 bool need_debug_info;
aff5d390
SC
5903
5904 void visit_target_symbol (target_symbol* e);
6ef331c8
SC
5905 void visit_target_symbol_arg (target_symbol* e);
5906 void visit_target_symbol_context (target_symbol* e);
bd1fcbad 5907 void visit_atvar_op (atvar_op* e);
40a0c64e 5908 void visit_cast_op (cast_op* e);
aff5d390
SC
5909};
5910
7a05f484
SC
5911
5912void
6ef331c8 5913sdt_uprobe_var_expanding_visitor::visit_target_symbol_context (target_symbol* e)
7a05f484 5914{
6ef331c8 5915 if (e->addressof)
dc09353a 5916 throw SEMANTIC_ERROR(_("cannot take address of context variable"), e->tok);
6ef331c8
SC
5917
5918 if (e->name == "$$name")
7a05f484 5919 {
6ef331c8
SC
5920 literal_string *myname = new literal_string (probe_name);
5921 myname->tok = e->tok;
5922 provide(myname);
5923 return;
5924 }
7a05f484 5925
6ef331c8
SC
5926 else if (e->name == "$$provider")
5927 {
5928 literal_string *myname = new literal_string (provider_name);
5929 myname->tok = e->tok;
5930 provide(myname);
5931 return;
5932 }
a794dbeb 5933
6ef331c8
SC
5934 else if (e->name == "$$vars" || e->name == "$$parms")
5935 {
5936 e->assert_no_components("sdt", true);
6ef331c8
SC
5937
5938 // Convert $$vars to sprintf of a list of vars which we recursively evaluate
63ea4244 5939
1c922ad7 5940 print_format* pf = print_format::create(e->tok, "sprintf");
6ef331c8
SC
5941
5942 for (unsigned i = 1; i <= arg_count; ++i)
5943 {
5944 if (i > 1)
5945 pf->raw_components += " ";
5946 target_symbol *tsym = new target_symbol;
5947 tsym->tok = e->tok;
5948 tsym->name = "$arg" + lex_cast(i);
5949 pf->raw_components += tsym->name;
5950 tsym->components = e->components;
5951
8c2f50c0 5952 expression *texp = require<expression> (tsym);
6ef331c8
SC
5953 if (!e->components.empty() &&
5954 e->components[0].type == target_symbol::comp_pretty_print)
5955 pf->raw_components += "=%s";
5956 else
5957 pf->raw_components += "=%#x";
5958 pf->args.push_back(texp);
5959 }
5960
5961 pf->components = print_format::string_to_components(pf->raw_components);
5962 provide (pf);
5963 }
5964 else
5965 assert(0); // shouldn't get here
5966}
5967
5968
5969void
5970sdt_uprobe_var_expanding_visitor::visit_target_symbol_arg (target_symbol *e)
5971{
5972 try
5973 {
8aabf152 5974 unsigned argno = 0; // the N in $argN
c69a87e0 5975 try
aff5d390 5976 {
5ecaa5a7 5977 if (startswith(e->name, "$arg"))
8aabf152 5978 argno = lex_cast<unsigned>(e->name.substr(4));
aff5d390 5979 }
c69a87e0 5980 catch (const runtime_error& f) // non-integral $arg suffix: e.g. $argKKKSDF
aff5d390 5981 {
8aabf152 5982 argno = 0;
aff5d390 5983 }
5ecaa5a7 5984
8aabf152
FCE
5985 if (arg_count == 0 || // a sdt.h variant without .probe-stored arg_count
5986 argno < 1 || argno > arg_count) // a $argN with out-of-range N
aff5d390 5987 {
8aabf152
FCE
5988 // NB: Either
5989 // 1) uprobe1_type $argN or $FOO (we don't know the arg_count)
5990 // 2) uprobe2_type $FOO (no probe args)
5991 // both of which get resolved later.
73d53dd2 5992 // Throw it now, and it might be resolved by DWARF later.
8aabf152 5993 need_debug_info = true;
dc09353a 5994 throw SEMANTIC_ERROR(_("target-symbol requires debuginfo"), e->tok);
aff5d390 5995 }
277c21bc 5996
8aabf152
FCE
5997 assert (arg_tokens.size() >= argno);
5998 string asmarg = arg_tokens[argno-1]; // $arg1 => arg_tokens[0]
c57ea854 5999
8aabf152
FCE
6000 // Now we try to parse this thing, which is an assembler operand
6001 // expression. If we can't, we warn, back down to need_debug_info
b874bd52 6002 // and hope for the best. Here is the syntax for a few architectures.
9859b766 6003 // Note that the power iN syntax is only for V3 sdt.h; gcc emits the i.
8095a157
FCE
6004 //
6005 // literal reg reg reg + base+index*size+offset
b874bd52 6006 // indirect offset
8095a157 6007 // x86 $N %rR (%rR) N(%rR) O(%bR,%iR,S)
b874bd52 6008 // power iN R (R) N(R)
b78a0fbb 6009 // ia64 N rR [r16]
b874bd52
SC
6010 // s390 N %rR 0(rR) N(r15)
6011 // arm #N rR [rR] [rR, #N]
6012
8aabf152
FCE
6013 expression* argexpr = 0; // filled in in case of successful parse
6014
6015 string percent_regnames;
6016 string regnames;
6017 vector<string> matches;
71e5e13d 6018 long precision;
8aabf152
FCE
6019 int rc;
6020
40fe32e0
SC
6021 // Parse the leading length
6022
6023 if (asmarg.find('@') != string::npos)
6024 {
6025 precision = lex_cast<int>(asmarg.substr(0, asmarg.find('@')));
6026 asmarg = asmarg.substr(asmarg.find('@')+1);
6027 }
71e5e13d
SC
6028 else
6029 {
6030 // V1/V2 do not have precision field so default to signed long
6031 // V3 asm does not have precision field so default to unsigned long
6032 if (probe_type == uprobe3_type)
6033 precision = sizeof(long); // this is an asm probe
6034 else
6035 precision = -sizeof(long);
6036 }
40fe32e0 6037
8aabf152
FCE
6038 // test for a numeric literal.
6039 // Only accept (signed) decimals throughout. XXX
6040
6041 // PR11821. NB: on powerpc, literals are not prefixed with $,
6042 // so this regex does not match. But that's OK, since without
6043 // -mregnames, we can't tell them apart from register numbers
6044 // anyway. With -mregnames, we could, if gcc somehow
6045 // communicated to us the presence of that option, but alas it
6046 // doesn't. http://gcc.gnu.org/PR44995.
272c9036 6047 rc = regexp_match (asmarg, "^[i\\$#][-]?[0-9][0-9]*$", matches);
8aabf152
FCE
6048 if (! rc)
6049 {
75a371ce
JS
6050 string sn = matches[0].substr(1);
6051 int64_t n;
6052 try
6053 {
6054 // We have to pay attention to the size & sign, as gcc sometimes
6055 // propagates constants that don't quite match, like a negative
6056 // value to fill an unsigned type.
6057 switch (precision)
6058 {
6059 case -1: n = lex_cast< int8_t>(sn); break;
6060 case 1: n = lex_cast< uint8_t>(sn); break;
6061 case -2: n = lex_cast< int16_t>(sn); break;
6062 case 2: n = lex_cast<uint16_t>(sn); break;
6063 case -4: n = lex_cast< int32_t>(sn); break;
6064 case 4: n = lex_cast<uint32_t>(sn); break;
6065 default:
6066 case -8: n = lex_cast< int64_t>(sn); break;
6067 case 8: n = lex_cast<uint64_t>(sn); break;
6068 }
6069 }
6070 catch (std::runtime_error&)
6071 {
6072 goto not_matched;
6073 }
6074 literal_number* ln = new literal_number(n);
8aabf152
FCE
6075 ln->tok = e->tok;
6076 argexpr = ln;
6077 goto matched;
6078 }
6079
14900130
SC
6080 if (dwarf_regs.empty())
6081 goto not_matched;
b78a0fbb 6082
8aabf152
FCE
6083 // Build regex pieces out of the known dwarf_regs. We keep two separate
6084 // lists: ones with the % prefix (and thus unambigiuous even despite PR11821),
6085 // and ones with no prefix (and thus only usable in unambiguous contexts).
46a94997 6086 for (map<string,pair<unsigned,int> >::iterator ri = dwarf_regs.begin(); ri != dwarf_regs.end(); ri++)
8aabf152
FCE
6087 {
6088 string regname = ri->first;
6089 assert (regname != "");
6090 regnames += string("|")+regname;
6091 if (regname[0]=='%')
6092 percent_regnames += string("|")+regname;
6093 }
6094 // clip off leading |
6095 regnames = regnames.substr(1);
272c9036
WF
6096 if (percent_regnames != "")
6097 percent_regnames = percent_regnames.substr(1);
8aabf152
FCE
6098
6099 // test for REGISTER
6100 // NB: Because PR11821, we must use percent_regnames here.
272c9036 6101 if (elf_machine == EM_PPC || elf_machine == EM_PPC64 || elf_machine == EM_ARM)
9109f487
SC
6102 rc = regexp_match (asmarg, string("^(")+regnames+string(")$"), matches);
6103 else
332ba7e7 6104 rc = regexp_match (asmarg, string("^(")+percent_regnames+string(")$"), matches);
8aabf152
FCE
6105 if (! rc)
6106 {
6107 string regname = matches[1];
46a94997
SC
6108 map<string,pair<unsigned,int> >::iterator ri = dwarf_regs.find (regname);
6109 if (ri != dwarf_regs.end()) // known register
8aabf152
FCE
6110 {
6111 embedded_expr *get_arg1 = new embedded_expr;
19c22e1f 6112 string width_adjust;
46a94997 6113 switch (ri->second.second)
19c22e1f 6114 {
892ec39a
SC
6115 case QI: width_adjust = ") & 0xff)"; break;
6116 case QIh: width_adjust = ">>8) & 0xff)"; break;
46a94997 6117 case HI:
71e5e13d 6118 // preserve 16 bit register signness
892ec39a
SC
6119 width_adjust = ") & 0xffff)";
6120 if (precision < 0)
55b377f4 6121 width_adjust += " << 48 >> 48";
ac8a78aa
SC
6122 break;
6123 case SI:
6124 // preserve 32 bit register signness
892ec39a
SC
6125 width_adjust = ") & 0xffffffff)";
6126 if (precision < 0)
55b377f4 6127 width_adjust += " << 32 >> 32";
19c22e1f 6128 break;
892ec39a 6129 default: width_adjust = "))";
19c22e1f 6130 }
55b377f4
SC
6131 string type = "";
6132 if (probe_type == uprobe3_type)
6133 type = (precision < 0
6134 ? "(int" : "(uint") + lex_cast(abs(precision) * 8) + "_t)";
6135 type = type + "((";
8aabf152
FCE
6136 get_arg1->tok = e->tok;
6137 get_arg1->code = string("/* unprivileged */ /* pure */")
892ec39a 6138 + string(" ((int64_t)") + type
8aabf152
FCE
6139 + (is_user_module (process_name)
6140 ? string("u_fetch_register(")
6141 : string("k_fetch_register("))
46a94997 6142 + lex_cast(dwarf_regs[regname].first) + string("))")
19c22e1f 6143 + width_adjust;
8aabf152
FCE
6144 argexpr = get_arg1;
6145 goto matched;
6146 }
6147 // invalid register name, fall through
6148 }
40fe32e0 6149
272c9036 6150 int reg, offset1;
e5b7b83f 6151 // test for OFFSET(REGISTER) where OFFSET is +-N+-N+-N
40fe32e0 6152 // NB: Despite PR11821, we can use regnames here, since the parentheses
e5b7b83f 6153 // make things unambiguous. (Note: gdb/stap-probe.c also parses this)
272c9036
WF
6154 // On ARM test for [REGISTER, OFFSET]
6155 if (elf_machine == EM_ARM)
6156 {
669a2feb 6157 rc = regexp_match (asmarg, string("^\\[(")+regnames+string(")(, #([+-]?[0-9]+)([+-][0-9]*)?([+-][0-9]*)?)?\\]$"), matches);
272c9036 6158 reg = 1;
669a2feb 6159 offset1 = 3;
272c9036
WF
6160 }
6161 else
6162 {
6163 rc = regexp_match (asmarg, string("^([+-]?[0-9]*)([+-][0-9]*)?([+-][0-9]*)?[(](")+regnames+string(")[)]$"), matches);
6164 reg = 4;
6165 offset1 = 1;
6166 }
8aabf152
FCE
6167 if (! rc)
6168 {
e5b7b83f 6169 string regname;
8aabf152 6170 int64_t disp = 0;
272c9036
WF
6171 if (matches[reg].length())
6172 regname = matches[reg];
8095a157
FCE
6173 if (dwarf_regs.find (regname) == dwarf_regs.end())
6174 goto not_matched;
6175
272c9036 6176 for (int i=offset1; i <= (offset1 + 2); i++)
e5b7b83f
SC
6177 if (matches[i].length())
6178 try
6179 {
6180 disp += lex_cast<int64_t>(matches[i]); // should decode positive/negative hex/decimal
6181 }
8aabf152
FCE
6182 catch (const runtime_error& f) // unparseable offset
6183 {
6184 goto not_matched; // can't just 'break' out of
6185 // this case or use a sentinel
6186 // value, unfortunately
6187 }
6188
8aabf152 6189 // synthesize user_long(%{fetch_register(R)%} + D)
8aabf152
FCE
6190 embedded_expr *get_arg1 = new embedded_expr;
6191 get_arg1->tok = e->tok;
6192 get_arg1->code = string("/* unprivileged */ /* pure */")
6193 + (is_user_module (process_name)
6194 ? string("u_fetch_register(")
6195 : string("k_fetch_register("))
46a94997 6196 + lex_cast(dwarf_regs[regname].first) + string(")");
8aabf152 6197 // XXX: may we ever need to cast that to a narrower type?
40fe32e0 6198
8aabf152
FCE
6199 literal_number* inc = new literal_number(disp);
6200 inc->tok = e->tok;
40fe32e0 6201
8aabf152
FCE
6202 binary_expression *be = new binary_expression;
6203 be->tok = e->tok;
6204 be->left = get_arg1;
6205 be->op = "+";
6206 be->right = inc;
40fe32e0 6207
8aabf152 6208 functioncall *fc = new functioncall;
40fe32e0
SC
6209 switch (precision)
6210 {
7f6ce9ab
SC
6211 case 1: case -1:
6212 fc->function = "user_int8"; break;
6213 case 2:
6214 fc->function = "user_uint16"; break;
6215 case -2:
6216 fc->function = "user_int16"; break;
6217 case 4:
6218 fc->function = "user_uint32"; break;
6219 case -4:
6220 fc->function = "user_int32"; break;
6221 case 8: case -8:
6222 fc->function = "user_int64"; break;
40fe32e0
SC
6223 default: fc->function = "user_long";
6224 }
8aabf152
FCE
6225 fc->tok = e->tok;
6226 fc->args.push_back(be);
366af4e7 6227
8aabf152
FCE
6228 argexpr = fc;
6229 goto matched;
6230 }
8095a157
FCE
6231
6232 // test for OFFSET(BASE_REGISTER,INDEX_REGISTER[,SCALE]) where OFFSET is +-N+-N+-N
6233 // NB: Despite PR11821, we can use regnames here, since the parentheses
6234 // make things unambiguous. (Note: gdb/stap-probe.c also parses this)
0f7b51d6 6235 rc = regexp_match (asmarg, string("^([+-]?[0-9]*)([+-][0-9]*)?([+-][0-9]*)?[(](")+regnames+string("),(")+regnames+string(")(,[1248])?[)]$"), matches);
8095a157
FCE
6236 if (! rc)
6237 {
6238 string baseregname;
6239 string indexregname;
6240 int64_t disp = 0;
6241 short scale = 1;
6242
6243 if (matches[6].length())
6244 try
6245 {
6246 scale = lex_cast<short>(matches[6].substr(1)); // NB: skip the comma!
6247 // We could verify that scale is one of 1,2,4,8,
6248 // but it doesn't really matter. An erroneous
6249 // address merely results in run-time errors.
8aabf152 6250 }
8095a157
FCE
6251 catch (const runtime_error &f) // unparseable scale
6252 {
6253 goto not_matched;
6254 }
6255
6256 if (matches[4].length())
6257 baseregname = matches[4];
6258 if (dwarf_regs.find (baseregname) == dwarf_regs.end())
6259 goto not_matched;
6260
6261 if (matches[5].length())
6262 indexregname = matches[5];
6263 if (dwarf_regs.find (indexregname) == dwarf_regs.end())
6264 goto not_matched;
6265
f7719b3d 6266 for (int i = 1; i <= 3; i++) // up to three OFFSET terms
8095a157
FCE
6267 if (matches[i].length())
6268 try
6269 {
6270 disp += lex_cast<int64_t>(matches[i]); // should decode positive/negative hex/decimal
6271 }
6272 catch (const runtime_error& f) // unparseable offset
6273 {
6274 goto not_matched; // can't just 'break' out of
6275 // this case or use a sentinel
6276 // value, unfortunately
6277 }
6278
6279 // synthesize user_long(%{fetch_register(R1)+fetch_register(R2)*N%} + D)
6280
6281 embedded_expr *get_arg1 = new embedded_expr;
6282 string regfn = is_user_module (process_name)
6283 ? string("u_fetch_register")
6284 : string("k_fetch_register"); // NB: in practice sdt.h probes are for userspace only
6285
6286 get_arg1->tok = e->tok;
6287 get_arg1->code = string("/* unprivileged */ /* pure */")
6288 + regfn + string("(")+lex_cast(dwarf_regs[baseregname].first)+string(")")
6289 + string("+(")
6290 + regfn + string("(")+lex_cast(dwarf_regs[indexregname].first)+string(")")
6291 + string("*")
6292 + lex_cast(scale)
6293 + string(")");
6294
6295 // NB: could plop this +DISPLACEMENT bit into the embedded-c expression too
6296 literal_number* inc = new literal_number(disp);
6297 inc->tok = e->tok;
6298
6299 binary_expression *be = new binary_expression;
6300 be->tok = e->tok;
6301 be->left = get_arg1;
6302 be->op = "+";
6303 be->right = inc;
6304
6305 functioncall *fc = new functioncall;
6306 switch (precision)
6307 {
6308 case 1: case -1:
6309 fc->function = "user_int8"; break;
6310 case 2:
6311 fc->function = "user_uint16"; break;
6312 case -2:
6313 fc->function = "user_int16"; break;
6314 case 4:
6315 fc->function = "user_uint32"; break;
6316 case -4:
6317 fc->function = "user_int32"; break;
6318 case 8: case -8:
6319 fc->function = "user_int64"; break;
6320 default: fc->function = "user_long";
6321 }
6322 fc->tok = e->tok;
6323 fc->args.push_back(be);
6324
6325 argexpr = fc;
6326 goto matched;
8aabf152
FCE
6327 }
6328
8aabf152
FCE
6329
6330 not_matched:
6331 // The asmarg operand was not recognized. Back down to dwarf.
6332 if (! session.suppress_warnings)
84fef8ee
FCE
6333 {
6334 if (probe_type == UPROBE3_TYPE)
6c9e1946 6335 session.print_warning (_F("Can't parse SDT_V3 operand '%s' [man error::sdt]", asmarg.c_str()), e->tok);
84fef8ee 6336 else // must be *PROBE2; others don't get asm operands
b78a0fbb 6337 session.print_warning (_F("Downgrading SDT_V2 probe argument to dwarf, can't parse '%s' [man error::sdt]",
84fef8ee
FCE
6338 asmarg.c_str()), e->tok);
6339 }
8aabf152
FCE
6340 assert (argexpr == 0);
6341 need_debug_info = true;
dc09353a 6342 throw SEMANTIC_ERROR(_("SDT asm not understood, requires debuginfo [man error::sdt]"), e->tok);
366af4e7 6343
8aabf152
FCE
6344 matched:
6345 assert (argexpr != 0);
366af4e7
RM
6346
6347 if (session.verbose > 2)
1e41115c 6348 //TRANSLATORS: We're mapping the operand to a new expression*.
b530b5b3 6349 clog << _F("mapped asm operand %s to ", asmarg.c_str()) << *argexpr << endl;
366af4e7 6350
aff5d390 6351 if (e->components.empty()) // We have a scalar
8aabf152
FCE
6352 {
6353 if (e->addressof)
dc09353a 6354 throw SEMANTIC_ERROR(_("cannot take address of sdt variable"), e->tok);
8aabf152
FCE
6355 provide (argexpr);
6356 return;
6357 }
6358 else // $var->foo
6359 {
6360 cast_op *cast = new cast_op;
6361 cast->name = "@cast";
6362 cast->tok = e->tok;
6363 cast->operand = argexpr;
6364 cast->components = e->components;
6365 cast->type_name = probe_name + "_arg" + lex_cast(argno);
6366 cast->module = process_name;
6367 cast->visit(this);
6368 return;
6369 }
366af4e7 6370
8aabf152 6371 /* NOTREACHED */
aff5d390
SC
6372 }
6373 catch (const semantic_error &er)
6374 {
6375 e->chain (er);
6376 provide (e);
6377 }
6378}
6379
6380
6ef331c8
SC
6381void
6382sdt_uprobe_var_expanding_visitor::visit_target_symbol (target_symbol* e)
6383{
6384 try
6385 {
49131a6d 6386 assert(e->name.size() > 0
bd1fcbad 6387 && (e->name[0] == '$' || e->name == "@var"));
6ef331c8
SC
6388
6389 if (e->name == "$$name" || e->name == "$$provider" || e->name == "$$parms" || e->name == "$$vars")
6390 visit_target_symbol_context (e);
6391 else
6392 visit_target_symbol_arg (e);
6393 }
6394 catch (const semantic_error &er)
6395 {
6396 e->chain (er);
6397 provide (e);
6398 }
6399}
6400
6401
bd1fcbad
YZ
6402void
6403sdt_uprobe_var_expanding_visitor::visit_atvar_op (atvar_op* e)
6404{
6405 need_debug_info = true;
6406
6407 // Fill in our current module context if needed
6408 if (e->module.empty())
6409 e->module = process_name;
6410
6411 var_expanding_visitor::visit_atvar_op(e);
6412}
6413
6414
40a0c64e
JS
6415void
6416sdt_uprobe_var_expanding_visitor::visit_cast_op (cast_op* e)
6417{
6418 // Fill in our current module context if needed
6419 if (e->module.empty())
6420 e->module = process_name;
6421
6422 var_expanding_visitor::visit_cast_op(e);
6423}
6424
6425
576eaefe
SC
6426void
6427plt_expanding_visitor::visit_target_symbol (target_symbol *e)
6428{
6429 try
6430 {
6431 if (e->name == "$$name")
6432 {
6433 literal_string *myname = new literal_string (entry);
6434 myname->tok = e->tok;
6435 provide(myname);
6436 return;
6437 }
3d69c03f
JS
6438
6439 // variable not found -> throw a semantic error
6440 // (only to be caught right away, but this may be more complex later...)
6441 string alternatives = "$$name";
dc09353a 6442 throw SEMANTIC_ERROR(_F("unable to find plt variable '%s' (alternatives: %s)",
3d69c03f 6443 e->name.c_str(), alternatives.c_str()), e->tok);
576eaefe
SC
6444 }
6445 catch (const semantic_error &er)
6446 {
6447 e->chain (er);
6448 provide (e);
6449 }
6450}
6451
6452
edce5b67
JS
6453struct sdt_query : public base_query
6454{
6455 sdt_query(probe * base_probe, probe_point * base_loc,
6456 dwflpp & dw, literal_map_t const & params,
51d6bda3 6457 vector<derived_probe *> & results, const string user_lib);
edce5b67 6458
51d6bda3 6459 void query_library (const char *data);
576eaefe 6460 void query_plt (const char *entry, size_t addr) {}
edce5b67
JS
6461 void handle_query_module();
6462
6463private:
15284963 6464 stap_sdt_probe_type probe_type;
d61ea602 6465 enum { probe_section=0, note_section=1, unknown_section=-1 } probe_loc;
edce5b67
JS
6466 probe * base_probe;
6467 probe_point * base_loc;
6846cfc8 6468 literal_map_t const & params;
edce5b67 6469 vector<derived_probe *> & results;
a794dbeb
FCE
6470 string pp_mark;
6471 string pp_provider;
51d6bda3 6472 string user_lib;
edce5b67
JS
6473
6474 set<string> probes_handled;
6475
6476 Elf_Data *pdata;
6477 size_t probe_scn_offset;
6478 size_t probe_scn_addr;
aff5d390 6479 uint64_t arg_count;
40fe32e0 6480 GElf_Addr base;
c57ea854 6481 GElf_Addr pc;
aff5d390 6482 string arg_string;
edce5b67 6483 string probe_name;
a794dbeb 6484 string provider_name;
909ab234 6485 GElf_Addr semaphore_load_offset;
79a0ca08 6486 Dwarf_Addr semaphore;
edce5b67
JS
6487
6488 bool init_probe_scn();
6b51ee12 6489 bool get_next_probe();
c57ea854
SC
6490 void iterate_over_probe_entries();
6491 void handle_probe_entry();
edce5b67 6492
488d3c1b
JL
6493 static void setup_note_probe_entry_callback (sdt_query *me,
6494 int type,
6495 const char *data,
6496 size_t len);
40fe32e0
SC
6497 void setup_note_probe_entry (int type, const char *data, size_t len);
6498
edce5b67 6499 void convert_probe(probe *base);
4ddb6dd0 6500 void record_semaphore(vector<derived_probe *> & results, unsigned start);
c72aa911 6501 probe* convert_location();
40fe32e0 6502 bool have_uprobe() {return probe_type == uprobe1_type || probe_type == uprobe2_type || probe_type == uprobe3_type;}
c57ea854
SC
6503 bool have_debuginfo_uprobe(bool need_debug_info)
6504 {return probe_type == uprobe1_type
40fe32e0 6505 || ((probe_type == uprobe2_type || probe_type == uprobe3_type)
c57ea854 6506 && need_debug_info);}
40fe32e0 6507 bool have_debuginfoless_uprobe() {return probe_type == uprobe2_type || probe_type == uprobe3_type;}
edce5b67
JS
6508};
6509
6510
6511sdt_query::sdt_query(probe * base_probe, probe_point * base_loc,
6512 dwflpp & dw, literal_map_t const & params,
51d6bda3 6513 vector<derived_probe *> & results, const string user_lib):
d61ea602
JS
6514 base_query(dw, params), probe_type(unknown_probe_type),
6515 probe_loc(unknown_section), base_probe(base_probe),
74fe61bc
LB
6516 base_loc(base_loc), params(params), results(results), user_lib(user_lib),
6517 probe_scn_offset(0), probe_scn_addr(0), arg_count(0), base(0), pc(0),
909ab234 6518 semaphore_load_offset(0), semaphore(0)
edce5b67 6519{
a794dbeb
FCE
6520 assert(get_string_param(params, TOK_MARK, pp_mark));
6521 get_string_param(params, TOK_PROVIDER, pp_provider); // pp_provider == "" -> unspecified
6522
ef428667
FCE
6523 // PR10245: permit usage of dtrace-y "-" separator in marker name;
6524 // map it to double-underscores.
6525 size_t pos = 0;
6526 while (1) // there may be more than one
6527 {
a794dbeb 6528 size_t i = pp_mark.find("-", pos);
ef428667 6529 if (i == string::npos) break;
a794dbeb 6530 pp_mark.replace (i, 1, "__");
ef428667
FCE
6531 pos = i+1; // resume searching after the inserted __
6532 }
a794dbeb
FCE
6533
6534 // XXX: same for pp_provider?
edce5b67
JS
6535}
6536
6537
6538void
c57ea854 6539sdt_query::handle_probe_entry()
edce5b67 6540{
c57ea854
SC
6541 if (! have_uprobe()
6542 && !probes_handled.insert(probe_name).second)
edce5b67
JS
6543 return;
6544
6545 if (sess.verbose > 3)
c57ea854 6546 {
b78a0fbb 6547 //TRANSLATORS: Describing what probe type (kprobe or uprobe) the probe
b530b5b3
LB
6548 //TRANSLATORS: is matched to.
6549 clog << _F("matched probe_name %s probe type ", probe_name.c_str());
c57ea854
SC
6550 switch (probe_type)
6551 {
6552 case uprobe1_type:
6553 clog << "uprobe1 at 0x" << hex << pc << dec << endl;
6554 break;
6555 case uprobe2_type:
6556 clog << "uprobe2 at 0x" << hex << pc << dec << endl;
6557 break;
40fe32e0
SC
6558 case uprobe3_type:
6559 clog << "uprobe3 at 0x" << hex << pc << dec << endl;
6560 break;
d61ea602
JS
6561 default:
6562 clog << "unknown!" << endl;
6563 break;
c57ea854
SC
6564 }
6565 }
edce5b67 6566
c57ea854
SC
6567 // Extend the derivation chain
6568 probe *new_base = convert_location();
6569 probe_point *new_location = new_base->locations[0];
6570
c57ea854
SC
6571 bool need_debug_info = false;
6572
7d395255
JS
6573 // We could get the Elf* from either dwarf_getelf(dwfl_module_getdwarf(...))
6574 // or dwfl_module_getelf(...). We only need it for the machine type, which
6575 // should be the same. The bias is used for relocating debuginfoless probes,
6576 // though, so that must come from the possibly-prelinked ELF file, not DWARF.
c57ea854 6577 Dwarf_Addr bias;
7d395255 6578 Elf* elf = dwfl_module_getelf (dw.mod_info->mod, &bias);
c57ea854 6579
1cc41cd6
DS
6580 /* Figure out the architecture of this particular ELF file. The
6581 dwarfless register-name mappings depend on it. */
6582 GElf_Ehdr ehdr_mem;
6583 GElf_Ehdr* em = gelf_getehdr (elf, &ehdr_mem);
6584 if (em == 0) { dwfl_assert ("dwfl_getehdr", dwfl_errno()); }
d8f31d0a 6585 assert(em);
1cc41cd6
DS
6586 int elf_machine = em->e_machine;
6587 sdt_uprobe_var_expanding_visitor svv (sess, elf_machine, module_val,
6588 provider_name, probe_name,
6589 probe_type, arg_string, arg_count);
6590 svv.replace (new_base->body);
6591 need_debug_info = svv.need_debug_info;
c57ea854 6592
1cc41cd6
DS
6593 // XXX: why not derive_probes() in the uprobes case too?
6594 literal_map_t params;
6595 for (unsigned i = 0; i < new_location->components.size(); ++i)
6596 {
6597 probe_point::component *c = new_location->components[i];
6598 params[c->functor] = c->arg;
6599 }
c57ea854 6600
73d53dd2 6601 unsigned prior_results_size = results.size();
1cc41cd6
DS
6602 dwarf_query q(new_base, new_location, dw, params, results, "", "");
6603 q.has_mark = true; // enables mid-statement probing
30263a73 6604
73d53dd2
JS
6605 // V1 probes always need dwarf info
6606 // V2+ probes need dwarf info in case of a variable reference
1cc41cd6 6607 if (have_debuginfo_uprobe(need_debug_info))
06de3a04 6608 dw.iterate_over_modules<base_query>(&query_module, &q);
73d53dd2
JS
6609
6610 // For V2+ probes, if variable references weren't used or failed (PR14369),
6611 // then try with the more direct approach. Unresolved $vars might still
6612 // cause their own error, but this gives them a chance to be optimized out.
6613 if (have_debuginfoless_uprobe() && results.size() == prior_results_size)
1cc41cd6
DS
6614 {
6615 string section;
6616 Dwarf_Addr reloc_addr = q.statement_num_val + bias;
6617 if (dwfl_module_relocations (q.dw.mod_info->mod) > 0)
6618 {
6619 dwfl_module_relocate_address (q.dw.mod_info->mod, &reloc_addr);
6620 section = ".dynamic";
6621 }
6622 else
6623 section = ".absolute";
edce5b67 6624
1cc41cd6
DS
6625 uprobe_derived_probe* p =
6626 new uprobe_derived_probe ("", "", 0,
6627 path_remove_sysroot(sess,q.module_val),
6628 section,
6629 q.statement_num_val, reloc_addr, q, 0);
6630 p->saveargs (arg_count);
6631 results.push_back (p);
c57ea854 6632 }
487bf4e2 6633 sess.unwindsym_modules.insert (dw.module_name);
73d53dd2 6634 record_semaphore(results, prior_results_size);
c57ea854 6635}
edce5b67 6636
4ddb6dd0 6637
c57ea854
SC
6638void
6639sdt_query::handle_query_module()
6640{
6641 if (!init_probe_scn())
6642 return;
edce5b67 6643
c57ea854
SC
6644 if (sess.verbose > 3)
6645 clog << "TOK_MARK: " << pp_mark << " TOK_PROVIDER: " << pp_provider << endl;
edce5b67 6646
40fe32e0
SC
6647 if (probe_loc == note_section)
6648 {
6649 GElf_Shdr shdr_mem;
6650 GElf_Shdr *shdr = dw.get_section (".stapsdt.base", &shdr_mem);
6651
909ab234
JS
6652 // The 'base' lets us adjust the hardcoded addresses in notes for prelink
6653 // effects. The 'semaphore_load_offset' accounts for the difference in
6654 // load addresses between text and data, so the semaphore can be
6655 // converted to a file offset if needed.
40fe32e0 6656 if (shdr)
909ab234
JS
6657 {
6658 base = shdr->sh_addr;
6659 GElf_Addr base_offset = shdr->sh_offset;
6660 shdr = dw.get_section (".probes", &shdr_mem);
6661 if (shdr)
6662 semaphore_load_offset =
6663 (shdr->sh_addr - shdr->sh_offset) - (base - base_offset);
6664 }
40fe32e0 6665 else
909ab234
JS
6666 base = semaphore_load_offset = 0;
6667
488d3c1b 6668 dw.iterate_over_notes (this, &sdt_query::setup_note_probe_entry_callback);
40fe32e0 6669 }
d61ea602 6670 else if (probe_loc == probe_section)
40fe32e0 6671 iterate_over_probe_entries ();
edce5b67
JS
6672}
6673
6674
6675bool
6676sdt_query::init_probe_scn()
6677{
448a86b7 6678 Elf* elf;
edce5b67 6679 GElf_Shdr shdr_mem;
40fe32e0
SC
6680
6681 GElf_Shdr *shdr = dw.get_section (".note.stapsdt", &shdr_mem);
6682 if (shdr)
6683 {
6684 probe_loc = note_section;
6685 return true;
6686 }
edce5b67 6687
448a86b7 6688 shdr = dw.get_section (".probes", &shdr_mem, &elf);
fea74777 6689 if (shdr)
edce5b67 6690 {
fea74777
SC
6691 pdata = elf_getdata_rawchunk (elf, shdr->sh_offset, shdr->sh_size, ELF_T_BYTE);
6692 probe_scn_offset = 0;
6693 probe_scn_addr = shdr->sh_addr;
6694 assert (pdata != NULL);
6695 if (sess.verbose > 4)
ce0f6648
LB
6696 clog << "got .probes elf scn_addr@0x" << probe_scn_addr << ", size: "
6697 << pdata->d_size << endl;
40fe32e0 6698 probe_loc = probe_section;
fea74777 6699 return true;
edce5b67 6700 }
fea74777 6701 else
edce5b67 6702 return false;
edce5b67
JS
6703}
6704
40fe32e0 6705void
488d3c1b 6706sdt_query::setup_note_probe_entry_callback (sdt_query *me, int type, const char *data, size_t len)
40fe32e0 6707{
40fe32e0
SC
6708 me->setup_note_probe_entry (type, data, len);
6709}
6710
6711
6712void
6713sdt_query::setup_note_probe_entry (int type, const char *data, size_t len)
6714{
6715 // if (nhdr.n_namesz == sizeof _SDT_NOTE_NAME
6716 // && !memcmp (data->d_buf + name_off,
6717 // _SDT_NOTE_NAME, sizeof _SDT_NOTE_NAME))
6718
6719 // probes are in the .note.stapsdt section
6720#define _SDT_NOTE_TYPE 3
6721 if (type != _SDT_NOTE_TYPE)
6722 return;
6723
6724 union
6725 {
6726 Elf64_Addr a64[3];
6727 Elf32_Addr a32[3];
6728 } buf;
6729 Dwarf_Addr bias;
6730 Elf* elf = (dwfl_module_getelf (dw.mod_info->mod, &bias));
6731 Elf_Data dst =
6732 {
6733 &buf, ELF_T_ADDR, EV_CURRENT,
6734 gelf_fsize (elf, ELF_T_ADDR, 3, EV_CURRENT), 0, 0
6735 };
6736 assert (dst.d_size <= sizeof buf);
6737
6738 if (len < dst.d_size + 3)
6739 return;
6740
6741 Elf_Data src =
6742 {
6743 (void *) data, ELF_T_ADDR, EV_CURRENT,
6744 dst.d_size, 0, 0
6745 };
6746
6747 if (gelf_xlatetom (elf, &dst, &src,
6748 elf_getident (elf, NULL)[EI_DATA]) == NULL)
6749 printf ("gelf_xlatetom: %s", elf_errmsg (-1));
6750
6751 probe_type = uprobe3_type;
6752 const char * provider = data + dst.d_size;
3f803f9e 6753
40fe32e0 6754 const char *name = (const char*)memchr (provider, '\0', data + len - provider);
3f803f9e
CM
6755 if(name++ == NULL)
6756 return;
6757
6758 const char *args = (const char*)memchr (name, '\0', data + len - name);
6759 if (args++ == NULL || memchr (args, '\0', data + len - name) != data + len - 1)
6760 return;
6761
6762 provider_name = provider;
6763 probe_name = name;
6764 arg_string = args;
40fe32e0 6765
e90006d7
JL
6766 dw.mod_info->marks.insert(make_pair(provider, name));
6767
40fe32e0
SC
6768 // Did we find a matching probe?
6769 if (! (dw.function_name_matches_pattern (probe_name, pp_mark)
6770 && ((pp_provider == "")
6771 || dw.function_name_matches_pattern (provider_name, pp_provider))))
6772 return;
6773
e9a90eee
JS
6774 // PR13934: Assembly probes are not forced to use the N@OP form.
6775 // If we have '@' then great, else count based on space-delimiters.
6776 arg_count = count(arg_string.begin(), arg_string.end(), '@');
6777 if (!arg_count && !arg_string.empty())
6778 arg_count = 1 + count(arg_string.begin(), arg_string.end(), ' ');
6779
40fe32e0
SC
6780 GElf_Addr base_ref;
6781 if (gelf_getclass (elf) == ELFCLASS32)
6782 {
6783 pc = buf.a32[0];
6784 base_ref = buf.a32[1];
6785 semaphore = buf.a32[2];
6786 }
6787 else
6788 {
6789 pc = buf.a64[0];
6790 base_ref = buf.a64[1];
6791 semaphore = buf.a64[2];
6792 }
6793
6794 semaphore += base - base_ref;
6795 pc += base - base_ref;
6796
7d395255
JS
6797 // The semaphore also needs the ELF bias added now, so
6798 // record_semaphore can properly relocate it later.
6799 semaphore += bias;
6800
40fe32e0 6801 if (sess.verbose > 4)
b530b5b3 6802 clog << _F(" saw .note.stapsdt %s%s ", probe_name.c_str(), (provider_name != "" ? _(" (provider ")+provider_name+") " : "").c_str()) << "@0x" << hex << pc << dec << endl;
40fe32e0
SC
6803
6804 handle_probe_entry();
6805}
6806
6807
c57ea854
SC
6808void
6809sdt_query::iterate_over_probe_entries()
edce5b67 6810{
c57ea854 6811 // probes are in the .probe section
edce5b67
JS
6812 while (probe_scn_offset < pdata->d_size)
6813 {
aff5d390
SC
6814 stap_sdt_probe_entry_v1 *pbe_v1 = (stap_sdt_probe_entry_v1 *) ((char*)pdata->d_buf + probe_scn_offset);
6815 stap_sdt_probe_entry_v2 *pbe_v2 = (stap_sdt_probe_entry_v2 *) ((char*)pdata->d_buf + probe_scn_offset);
15284963 6816 probe_type = (stap_sdt_probe_type)(pbe_v1->type_a);
1cc41cd6 6817 if (! have_uprobe())
edce5b67
JS
6818 {
6819 // Unless this is a mangled .probes section, this happens
6820 // because the name of the probe comes first, followed by
6821 // the sentinel.
6822 if (sess.verbose > 5)
b530b5b3 6823 clog << _F("got unknown probe_type : 0x%x", probe_type) << endl;
edce5b67
JS
6824 probe_scn_offset += sizeof(__uint32_t);
6825 continue;
6826 }
aff5d390
SC
6827 if ((long)pbe_v1 % sizeof(__uint64_t)) // we have stap_sdt_probe_entry_v1.type_b
6828 {
6829 pbe_v1 = (stap_sdt_probe_entry_v1*)((char*)pbe_v1 - sizeof(__uint32_t));
1cc41cd6 6830 if (pbe_v1->type_b != uprobe1_type)
aff5d390
SC
6831 continue;
6832 }
6833
1cc41cd6 6834 if (probe_type == uprobe1_type)
aff5d390 6835 {
79a0ca08 6836 if (pbe_v1->name == 0) // No name possibly means we have a .so with a relocation
c57ea854 6837 return;
79a0ca08 6838 semaphore = 0;
aff5d390 6839 probe_name = (char*)((char*)pdata->d_buf + pbe_v1->name - (char*)probe_scn_addr);
a794dbeb 6840 provider_name = ""; // unknown
1cc41cd6
DS
6841 pc = pbe_v1->arg;
6842 arg_count = 0;
aff5d390
SC
6843 probe_scn_offset += sizeof (stap_sdt_probe_entry_v1);
6844 }
08b5a50c 6845 else if (probe_type == uprobe2_type)
aff5d390 6846 {
79a0ca08 6847 if (pbe_v2->name == 0) // No name possibly means we have a .so with a relocation
c57ea854 6848 return;
79a0ca08 6849 semaphore = pbe_v2->semaphore;
aff5d390 6850 probe_name = (char*)((char*)pdata->d_buf + pbe_v2->name - (char*)probe_scn_addr);
a794dbeb 6851 provider_name = (char*)((char*)pdata->d_buf + pbe_v2->provider - (char*)probe_scn_addr);
aff5d390
SC
6852 arg_count = pbe_v2->arg_count;
6853 pc = pbe_v2->pc;
6854 if (pbe_v2->arg_string)
6855 arg_string = (char*)((char*)pdata->d_buf + pbe_v2->arg_string - (char*)probe_scn_addr);
79a0ca08
SC
6856 // skip over pbe_v2, probe_name text and provider text
6857 probe_scn_offset = ((long)(pbe_v2->name) - (long)(probe_scn_addr)) + probe_name.length();
6858 probe_scn_offset += sizeof (__uint32_t) - probe_scn_offset % sizeof (__uint32_t);
aff5d390 6859 }
edce5b67 6860 if (sess.verbose > 4)
b530b5b3 6861 clog << _("saw .probes ") << probe_name << (provider_name != "" ? _(" (provider ")+provider_name+") " : "")
aff5d390 6862 << "@0x" << hex << pc << dec << endl;
edce5b67 6863
e90006d7
JL
6864 dw.mod_info->marks.insert(make_pair(provider_name, probe_name));
6865
a794dbeb
FCE
6866 if (dw.function_name_matches_pattern (probe_name, pp_mark)
6867 && ((pp_provider == "") || dw.function_name_matches_pattern (provider_name, pp_provider)))
c57ea854 6868 handle_probe_entry ();
edce5b67 6869 }
edce5b67
JS
6870}
6871
6872
6846cfc8 6873void
4ddb6dd0 6874sdt_query::record_semaphore (vector<derived_probe *> & results, unsigned start)
6846cfc8 6875{
a794dbeb
FCE
6876 for (unsigned i=0; i<2; i++) {
6877 // prefer with-provider symbol; look without provider prefix for backward compatibility only
6878 string semaphore = (i==0 ? (provider_name+"_") : "") + probe_name + "_semaphore";
6879 // XXX: multiple addresses?
6880 if (sess.verbose > 2)
b530b5b3 6881 clog << _F("looking for semaphore symbol %s ", semaphore.c_str());
a794dbeb 6882
79a0ca08
SC
6883 Dwarf_Addr addr;
6884 if (this->semaphore)
6885 addr = this->semaphore;
6886 else
6887 addr = lookup_symbol_address(dw.module, semaphore.c_str());
a794dbeb
FCE
6888 if (addr)
6889 {
7d395255 6890 if (dwfl_module_relocations (dw.module) > 0)
a794dbeb
FCE
6891 dwfl_module_relocate_address (dw.module, &addr);
6892 // XXX: relocation basis?
909ab234
JS
6893
6894 // Dyninst needs the *file*-based offset for semaphores,
6895 // so subtract the difference in load addresses between .text and .probes
6896 if (dw.sess.runtime_usermode_p())
6897 addr -= semaphore_load_offset;
6898
a794dbeb
FCE
6899 for (unsigned i = start; i < results.size(); ++i)
6900 results[i]->sdt_semaphore_addr = addr;
6901 if (sess.verbose > 2)
b530b5b3 6902 clog << _(", found at 0x") << hex << addr << dec << endl;
a794dbeb
FCE
6903 return;
6904 }
6905 else
6906 if (sess.verbose > 2)
b530b5b3 6907 clog << _(", not found") << endl;
a794dbeb 6908 }
6846cfc8
SC
6909}
6910
6911
edce5b67
JS
6912void
6913sdt_query::convert_probe (probe *base)
6914{
6915 block *b = new block;
6916 b->tok = base->body->tok;
6917
edce5b67
JS
6918 // Generate: if (arg1 != mark("label")) next;
6919 functioncall *fc = new functioncall;
bbafcb1e 6920 fc->function = "ulong_arg";
edce5b67 6921 fc->tok = b->tok;
bbafcb1e 6922 literal_number* num = new literal_number(1);
edce5b67
JS
6923 num->tok = b->tok;
6924 fc->args.push_back(num);
6925
6926 functioncall *fcus = new functioncall;
6927 fcus->function = "user_string";
6928 fcus->type = pe_string;
6929 fcus->tok = b->tok;
6930 fcus->args.push_back(fc);
6931
6932 if_statement *is = new if_statement;
6933 is->thenblock = new next_statement;
6934 is->elseblock = NULL;
6935 is->tok = b->tok;
63ea4244 6936 is->thenblock->tok = b->tok;
edce5b67
JS
6937 comparison *be = new comparison;
6938 be->op = "!=";
6939 be->tok = b->tok;
6940 be->left = fcus;
6941 be->right = new literal_string(probe_name);
63ea4244 6942 be->right->tok = b->tok;
edce5b67
JS
6943 is->condition = be;
6944 b->statements.push_back(is);
6945
6946 // Now replace the body
6947 b->statements.push_back(base->body);
6948 base->body = b;
6949}
6950
6951
c72aa911
JS
6952probe*
6953sdt_query::convert_location ()
edce5b67 6954{
c72aa911 6955 probe_point* specific_loc = new probe_point(*base_loc);
662539d9 6956 vector<probe_point::component*> derived_comps;
edce5b67 6957
662539d9
JS
6958 vector<probe_point::component*>::iterator it;
6959 for (it = specific_loc->components.begin();
6960 it != specific_loc->components.end(); ++it)
6961 if ((*it)->functor == TOK_PROCESS)
6962 {
1cc41cd6
DS
6963 // copy the process name
6964 derived_comps.push_back(*it);
662539d9
JS
6965 }
6966 else if ((*it)->functor == TOK_LIBRARY)
6967 {
1cc41cd6
DS
6968 // copy the library name for process probes
6969 derived_comps.push_back(*it);
662539d9
JS
6970 }
6971 else if ((*it)->functor == TOK_PROVIDER)
6972 {
6973 // replace the possibly wildcarded arg with the specific provider name
6974 *it = new probe_point::component(TOK_PROVIDER,
6975 new literal_string(provider_name));
6976 }
6977 else if ((*it)->functor == TOK_MARK)
c72aa911
JS
6978 {
6979 // replace the possibly wildcarded arg with the specific marker name
662539d9
JS
6980 *it = new probe_point::component(TOK_MARK,
6981 new literal_string(probe_name));
a794dbeb 6982
aff5d390
SC
6983 if (sess.verbose > 3)
6984 switch (probe_type)
6985 {
6986 case uprobe1_type:
b530b5b3 6987 clog << _("probe_type == uprobe1, use statement addr: 0x")
aff5d390
SC
6988 << hex << pc << dec << endl;
6989 break;
6990 case uprobe2_type:
b530b5b3 6991 clog << _("probe_type == uprobe2, use statement addr: 0x")
aff5d390
SC
6992 << hex << pc << dec << endl;
6993 break;
40fe32e0 6994 case uprobe3_type:
b530b5b3 6995 clog << _("probe_type == uprobe3, use statement addr: 0x")
40fe32e0
SC
6996 << hex << pc << dec << endl;
6997 break;
aff5d390 6998 default:
b530b5b3
LB
6999 clog << _F("probe_type == use_uprobe_no_dwarf, use label name: _stapprobe1_%s",
7000 pp_mark.c_str()) << endl;
aff5d390
SC
7001 }
7002
c72aa911
JS
7003 switch (probe_type)
7004 {
aff5d390
SC
7005 case uprobe1_type:
7006 case uprobe2_type:
40fe32e0 7007 case uprobe3_type:
c72aa911 7008 // process("executable").statement(probe_arg)
662539d9
JS
7009 derived_comps.push_back
7010 (new probe_point::component(TOK_STATEMENT,
7011 new literal_number(pc, true)));
c72aa911
JS
7012 break;
7013
a794dbeb 7014 default: // deprecated
c72aa911 7015 // process("executable").function("*").label("_stapprobe1_MARK_NAME")
662539d9
JS
7016 derived_comps.push_back
7017 (new probe_point::component(TOK_FUNCTION,
7018 new literal_string("*")));
7019 derived_comps.push_back
c72aa911 7020 (new probe_point::component(TOK_LABEL,
a794dbeb 7021 new literal_string("_stapprobe1_" + pp_mark)));
c72aa911
JS
7022 break;
7023 }
7024 }
edce5b67 7025
662539d9
JS
7026 probe_point* derived_loc = new probe_point(*specific_loc);
7027 derived_loc->components = derived_comps;
8159bf55 7028 return new probe (new probe (base_probe, specific_loc), derived_loc);
edce5b67
JS
7029}
7030
7031
51d6bda3
SC
7032void
7033sdt_query::query_library (const char *library)
7034{
7035 query_one_library (library, dw, user_lib, base_probe, base_loc, results);
7036}
7037
e90006d7
JL
7038string
7039suggest_marks(systemtap_session& sess,
7040 const set<string>& modules,
7041 const string& mark,
7042 const string& provider)
7043{
7044 if (mark.empty() || modules.empty() || sess.module_cache == NULL)
7045 return "";
7046
7047 set<string> marks;
7048 const map<string, module_info*> &cache = sess.module_cache->cache;
7049 bool dash_suggestions = (mark.find("-") != string::npos);
7050
7051 for (set<string>::iterator itmod = modules.begin();
7052 itmod != modules.end(); ++itmod)
7053 {
7054 map<string, module_info*>::const_iterator itcache;
7055 if ((itcache = cache.find(*itmod)) != cache.end())
7056 {
7057 set<pair<string,string> >::const_iterator itmarks;
7058 for (itmarks = itcache->second->marks.begin();
7059 itmarks != itcache->second->marks.end(); ++itmarks)
7060 {
7061 if (provider.empty()
7062 // simulating dw.function_name_matches_pattern()
7063 || (fnmatch(provider.c_str(), itmarks->first.c_str(), 0) == 0))
7064 {
7065 string marksug = itmarks->second;
7066 if (dash_suggestions)
7067 {
7068 size_t pos = 0;
7069 while (1) // there may be more than one
7070 {
7071 size_t i = marksug.find("__", pos);
7072 if (i == string::npos) break;
7073 marksug.replace (i, 2, "-");
7074 pos = i+1; // resume searching after the inserted -
7075 }
7076 }
7077 marks.insert(marksug);
7078 }
7079 }
7080 }
7081 }
7082
7083 if (sess.verbose > 2)
7084 {
7085 clog << "suggesting " << marks.size() << " marks "
7086 << "from modules:" << endl;
7087 for (set<string>::iterator itmod = modules.begin();
7088 itmod != modules.end(); ++itmod)
7089 clog << *itmod << endl;
7090 }
7091
7092 if (marks.empty())
7093 return "";
7094
7095 return levenshtein_suggest(mark, marks, 5); // print top 5 marks only
7096}
7097
40a70f52
JL
7098string
7099suggest_plt_functions(systemtap_session& sess,
7100 const set<string>& modules,
7101 const string& func)
7102{
7103 if (func.empty() || modules.empty() || sess.module_cache == NULL)
7104 return "";
7105
7106 set<string> funcs;
7107 const map<string, module_info*> &cache = sess.module_cache->cache;
7108
7109 for (set<string>::iterator itmod = modules.begin();
7110 itmod != modules.end(); ++itmod)
7111 {
7112 map<string, module_info*>::const_iterator itcache;
7113 if ((itcache = cache.find(*itmod)) != cache.end())
7114 funcs.insert(itcache->second->plt_funcs.begin(),
7115 itcache->second->plt_funcs.end());
7116 }
7117
7118 if (sess.verbose > 2)
d82dd71f
JL
7119 {
7120 clog << "suggesting " << funcs.size() << " plt functions "
7121 << "from modules:" << endl;
7122 for (set<string>::iterator itmod = modules.begin();
7123 itmod != modules.end(); ++itmod)
7124 clog << *itmod << endl;
7125 }
40a70f52
JL
7126
7127 if (funcs.empty())
7128 return "";
7129
7130 return levenshtein_suggest(func, funcs, 5); // print top 5 funcs only
7131}
51d6bda3 7132
0508c45f
JL
7133string
7134suggest_dwarf_functions(systemtap_session& sess,
7135 const set<string>& modules,
7136 string func)
44ffe90c
JL
7137{
7138 // Trim any @ component
7139 size_t pos = func.find('@');
7140 if (pos != string::npos)
7141 func.erase(pos);
7142
0508c45f 7143 if (func.empty() || modules.empty() || sess.module_cache == NULL)
44ffe90c
JL
7144 return "";
7145
7146 // We must first aggregate all the functions from the cache
7147 set<string> funcs;
7148 const map<string, module_info*> &cache = sess.module_cache->cache;
7149
0508c45f
JL
7150 for (set<string>::iterator itmod = modules.begin();
7151 itmod != modules.end(); ++itmod)
44ffe90c 7152 {
6e6d9a3b
JL
7153 module_info *module;
7154
7155 // retrieve module_info from cache
44ffe90c
JL
7156 map<string, module_info*>::const_iterator itcache;
7157 if ((itcache = cache.find(*itmod)) != cache.end())
6e6d9a3b
JL
7158 module = itcache->second;
7159 else // module not found
7160 continue;
7161
7162 // add inlines
7163 funcs.insert(module->inlined_funcs.begin(),
7164 module->inlined_funcs.end());
7165
7166 // add all function symbols in cache
7167 if (module->symtab_status != info_present || module->sym_table == NULL)
7168 continue;
7169 map<string, func_info*>& modfuncs = module->sym_table->map_by_name;
7170 for (map<string, func_info*>::const_iterator itfuncs = modfuncs.begin();
7171 itfuncs != modfuncs.end(); ++itfuncs)
7172 funcs.insert(itfuncs->first);
44ffe90c
JL
7173 }
7174
7175 if (sess.verbose > 2)
d82dd71f
JL
7176 {
7177 clog << "suggesting " << funcs.size() << " dwarf functions "
7178 << "from modules:" << endl;
7179 for (set<string>::iterator itmod = modules.begin();
7180 itmod != modules.end(); ++itmod)
7181 clog << *itmod << endl;
7182 }
44ffe90c
JL
7183
7184 if (funcs.empty())
7185 return "";
7186
593f09eb 7187 return levenshtein_suggest(func, funcs, 5); // print top 5 funcs only
44ffe90c
JL
7188}
7189
20c6c071 7190void
5227f1ea 7191dwarf_builder::build(systemtap_session & sess,
7a053d3b 7192 probe * base,
20c6c071 7193 probe_point * location,
86bf665e 7194 literal_map_t const & parameters,
20c6c071
GH
7195 vector<derived_probe *> & finished_results)
7196{
b20febf3
FCE
7197 // NB: the kernel/user dwlfpp objects are long-lived.
7198 // XXX: but they should be per-session, as this builder object
7199 // may be reused if we try to cross-instrument multiple targets.
84048984 7200
7a24d422 7201 dwflpp* dw = 0;
6d5d594e 7202 literal_map_t filled_parameters = parameters;
7a24d422 7203
7a24d422 7204 string module_name;
ae2552da
FCE
7205 if (has_null_param (parameters, TOK_KERNEL))
7206 {
7207 dw = get_kern_dw(sess, "kernel");
7208 }
7209 else if (get_param (parameters, TOK_MODULE, module_name))
b8da0ad1 7210 {
c523a015
LB
7211 size_t dash_pos = 0;
7212 while((dash_pos=module_name.find('-'))!=string::npos)
7213 module_name.replace(int(dash_pos),1,"_");
7214 filled_parameters[TOK_MODULE] = new literal_string(module_name);
37001baa
FCE
7215 // NB: glob patterns get expanded later, during the offline
7216 // elfutils module listing.
ae2552da 7217 dw = get_kern_dw(sess, module_name);
b8da0ad1 7218 }
6d5d594e 7219 else if (get_param (parameters, TOK_PROCESS, module_name) || has_null_param(parameters, TOK_PROCESS))
84c84ac4 7220 {
05fb3e0c 7221 module_name = sess.sysroot + module_name;
6d5d594e
LB
7222 if(has_null_param(filled_parameters, TOK_PROCESS))
7223 {
7224 wordexp_t words;
7225 int rc = wordexp(sess.cmd.c_str(), &words, WRDE_NOCMD|WRDE_UNDEF);
7226 if(rc || words.we_wordc <= 0)
dc09353a 7227 throw SEMANTIC_ERROR(_("unspecified process probe is invalid without a -c COMMAND"));
05fb3e0c 7228 module_name = sess.sysroot + words.we_wordv[0];
6d5d594e
LB
7229 filled_parameters[TOK_PROCESS] = new literal_string(module_name);// this needs to be used in place of the blank map
7230 // in the case of TOK_MARK we need to modify locations as well
7231 if(location->components[0]->functor==TOK_PROCESS &&
7232 location->components[0]->arg == 0)
7233 location->components[0]->arg = new literal_string(module_name);
7234 wordfree (& words);
b78a0fbb 7235 }
5750ecc6 7236
37001baa
FCE
7237 // PR6456 process("/bin/*") glob handling
7238 if (contains_glob_chars (module_name))
7239 {
7240 // Expand glob via rewriting the probe-point process("....")
7241 // parameter, asserted to be the first one.
7242
7243 assert (location->components.size() > 0);
7244 assert (location->components[0]->functor == TOK_PROCESS);
7245 assert (location->components[0]->arg);
7246 literal_string* lit = dynamic_cast<literal_string*>(location->components[0]->arg);
7247 assert (lit);
7248
7249 // Evaluate glob here, and call derive_probes recursively with each match.
7250 glob_t the_blob;
88f8e2c0 7251 set<string> dupes;
37001baa 7252 int rc = glob (module_name.c_str(), 0, NULL, & the_blob);
b530b5b3 7253 if (rc)
dc09353a 7254 throw SEMANTIC_ERROR (_F("glob %s error (%s)", module_name.c_str(), lex_cast(rc).c_str() ));
44ffe90c 7255 unsigned results_pre = finished_results.size();
37001baa
FCE
7256 for (unsigned i = 0; i < the_blob.gl_pathc; ++i)
7257 {
e19ebcf7 7258 assert_no_interrupts();
37001baa
FCE
7259
7260 const char* globbed = the_blob.gl_pathv[i];
7261 struct stat st;
7262
7263 if (access (globbed, X_OK) == 0
7264 && stat (globbed, &st) == 0
7265 && S_ISREG (st.st_mode)) // see find_executable()
7266 {
7977a734
FCE
7267 // Need to call canonicalize here, in order to path-expand
7268 // patterns like process("stap*"). Otherwise it may go through
7269 // to the next round of expansion as ("stap"), leading to a $PATH
7270 // search that's not consistent with the glob search already done.
5bca76a8
JS
7271 string canononicalized = resolve_path (globbed);
7272 globbed = canononicalized.c_str();
7977a734 7273
88f8e2c0
JS
7274 // The canonical names can result in duplication, for example
7275 // having followed symlinks that are common with shared
7276 // libraries. Filter those out.
7277 if (!dupes.insert(canononicalized).second)
7278 continue;
7279
37001baa
FCE
7280 // synthesize a new probe_point, with the glob-expanded string
7281 probe_point *pp = new probe_point (*location);
ef0943df
JL
7282 pp->from_glob = true;
7283
5750ecc6
FCE
7284 // PR13338: quote results to prevent recursion
7285 string eglobbed = escape_glob_chars (globbed);
7286
7287 if (sess.verbose > 1)
7288 clog << _F("Expanded process(\"%s\") to process(\"%s\")",
7289 module_name.c_str(), eglobbed.c_str()) << endl;
05fb3e0c 7290 string eglobbed_tgt = path_remove_sysroot(sess, eglobbed);
5750ecc6 7291
37001baa 7292 probe_point::component* ppc = new probe_point::component (TOK_PROCESS,
05fb3e0c 7293 new literal_string (eglobbed_tgt));
37001baa
FCE
7294 ppc->tok = location->components[0]->tok; // overwrite [0] slot, pattern matched above
7295 pp->components[0] = ppc;
7296
d885563b 7297 probe* new_probe = new probe (base, pp);
7977a734
FCE
7298
7299 // We override "optional = true" here, as if the
7300 // wildcarded probe point was given a "?" suffix.
7301
7302 // This is because wildcard probes will be expected
7303 // by users to apply only to some subset of the
7304 // matching binaries, in the sense of "any", rather
7305 // than "all", sort of similarly how
7306 // module("*").function("...") patterns work.
7307
7308 derive_probes (sess, new_probe, finished_results,
7309 true /* NB: not location->optional */ );
37001baa
FCE
7310 }
7311 }
7312
7313 globfree (& the_blob);
44ffe90c
JL
7314
7315 unsigned results_post = finished_results.size();
7316
e90006d7 7317 // Did we fail to find a function/plt/mark by name? Let's suggest
44ffe90c
JL
7318 // something!
7319 string func;
7320 if (results_pre == results_post
7321 && get_param(filled_parameters, TOK_FUNCTION, func)
7322 && !func.empty())
7323 {
0508c45f 7324 string sugs = suggest_dwarf_functions(sess, modules_seen, func);
44ffe90c
JL
7325 modules_seen.clear();
7326 if (!sugs.empty())
dc09353a 7327 throw SEMANTIC_ERROR (_NF("no match (similar function: %s)",
44ffe90c
JL
7328 "no match (similar functions: %s)",
7329 sugs.find(',') == string::npos,
7330 sugs.c_str()));
7331 }
40a70f52
JL
7332 else if (results_pre == results_post
7333 && get_param(filled_parameters, TOK_PLT, func)
7334 && !func.empty())
7335 {
7336 string sugs = suggest_plt_functions(sess, modules_seen, func);
7337 modules_seen.clear();
7338 if (!sugs.empty())
7339 throw SEMANTIC_ERROR (_NF("no match (similar function: %s)",
7340 "no match (similar functions: %s)",
7341 sugs.find(',') == string::npos,
7342 sugs.c_str()));
7343 }
e90006d7
JL
7344 else if (results_pre == results_post
7345 && get_param(filled_parameters, TOK_MARK, func)
7346 && !func.empty())
7347 {
7348 string provider;
7349 get_param(filled_parameters, TOK_PROVIDER, provider);
7350
7351 string sugs = suggest_marks(sess, modules_seen, func, provider);
7352 modules_seen.clear();
7353 if (!sugs.empty())
7354 throw SEMANTIC_ERROR (_NF("no match (similar mark: %s)",
7355 "no match (similar marks: %s)",
7356 sugs.find(',') == string::npos,
7357 sugs.c_str()));
7358 }
44ffe90c 7359
37001baa
FCE
7360 return; // avoid falling through
7361 }
7362
5750ecc6
FCE
7363 // PR13338: unquote glob results
7364 module_name = unescape_glob_chars (module_name);
05fb3e0c 7365 user_path = find_executable (module_name, "", sess.sysenv); // canonicalize it
d1bcbe71
RH
7366
7367 // if the executable starts with "#!", we look for the interpreter of the script
7368 {
7369 ifstream script_file (user_path.c_str () );
7370
7371 if (script_file.good ())
7372 {
7373 string line;
7374
7375 getline (script_file, line);
7376
7377 if (line.compare (0, 2, "#!") == 0)
7378 {
7379 string path_head = line.substr(2);
7380
7381 // remove white spaces at the beginning of the string
7382 size_t p2 = path_head.find_first_not_of(" \t");
7383
7384 if (p2 != string::npos)
7385 {
7386 string path = path_head.substr(p2);
7387
7388 // remove white spaces at the end of the string
7389 p2 = path.find_last_not_of(" \t\n");
7390 if (string::npos != p2)
7391 path.erase(p2+1);
7392
8e13c1a1
RH
7393 // handle "#!/usr/bin/env" redirect
7394 size_t offset = 0;
7395 if (path.compare(0, sizeof("/bin/env")-1, "/bin/env") == 0)
7396 {
7397 offset = sizeof("/bin/env")-1;
7398 }
7399 else if (path.compare(0, sizeof("/usr/bin/env")-1, "/usr/bin/env") == 0)
7400 {
7401 offset = sizeof("/usr/bin/env")-1;
7402 }
7403
7404 if (offset != 0)
7405 {
7406 size_t p3 = path.find_first_not_of(" \t", offset);
7407
7408 if (p3 != string::npos)
7409 {
7410 string env_path = path.substr(p3);
05fb3e0c
WF
7411 user_path = find_executable (env_path, sess.sysroot,
7412 sess.sysenv);
8e13c1a1
RH
7413 }
7414 }
7415 else
7416 {
05fb3e0c 7417 user_path = find_executable (path, sess.sysroot, sess.sysenv);
8e13c1a1 7418 }
d1bcbe71
RH
7419
7420 struct stat st;
7421
7422 if (access (user_path.c_str(), X_OK) == 0
7423 && stat (user_path.c_str(), &st) == 0
7424 && S_ISREG (st.st_mode)) // see find_executable()
7425 {
7426 if (sess.verbose > 1)
b530b5b3
LB
7427 clog << _F("Expanded process(\"%s\") to process(\"%s\")",
7428 module_name.c_str(), user_path.c_str()) << endl;
d1bcbe71
RH
7429
7430 assert (location->components.size() > 0);
7431 assert (location->components[0]->functor == TOK_PROCESS);
7432 assert (location->components[0]->arg);
7433 literal_string* lit = dynamic_cast<literal_string*>(location->components[0]->arg);
7434 assert (lit);
7435
7436 // synthesize a new probe_point, with the expanded string
7437 probe_point *pp = new probe_point (*location);
05fb3e0c 7438 string user_path_tgt = path_remove_sysroot(sess, user_path);
d1bcbe71 7439 probe_point::component* ppc = new probe_point::component (TOK_PROCESS,
05fb3e0c 7440 new literal_string (user_path_tgt.c_str()));
d1bcbe71
RH
7441 ppc->tok = location->components[0]->tok; // overwrite [0] slot, pattern matched above
7442 pp->components[0] = ppc;
7443
d885563b 7444 probe* new_probe = new probe (base, pp);
d1bcbe71
RH
7445
7446 derive_probes (sess, new_probe, finished_results);
7447
7448 script_file.close();
7449 return;
7450 }
7451 }
7452 }
7453 }
7454 script_file.close();
7455 }
7456
47e226ed 7457 if (get_param (parameters, TOK_LIBRARY, user_lib)
378d78b5 7458 && user_lib.length() && ! contains_glob_chars (user_lib))
47e226ed
SC
7459 {
7460 module_name = find_executable (user_lib, sess.sysroot, sess.sysenv,
7461 "LD_LIBRARY_PATH");
7462 if (module_name.find('/') == string::npos)
7463 // We didn't find user_lib so use iterate_over_libraries
7464 module_name = user_path;
7465 }
63b4fd14 7466 else
b642c901 7467 module_name = user_path; // canonicalize it
d0a7f5a9 7468
3667d615 7469 // uretprobes aren't available everywhere
536f1261 7470 if (has_null_param(parameters, TOK_RETURN) && !sess.runtime_usermode_p())
2b69faaf 7471 {
3667d615
JS
7472 if (kernel_supports_inode_uprobes(sess) &&
7473 !kernel_supports_inode_uretprobes(sess))
dc09353a 7474 throw SEMANTIC_ERROR
3ffeaf3c 7475 (_("process return probes not available [man error::inode-uprobes]"));
2b69faaf 7476 }
3667d615 7477
f4000852
MW
7478 // There is a similar check in pass 4 (buildrun), but it is
7479 // needed here too to make sure alternatives for optional
7480 // (? or !) process probes are disposed and/or alternatives
7481 // are selected.
3667d615
JS
7482 if (!sess.runtime_usermode_p())
7483 check_process_probe_kernel_support(sess);
e34d5d13 7484
7a24d422
FCE
7485 // user-space target; we use one dwflpp instance per module name
7486 // (= program or shared library)
707bf35e 7487 dw = get_user_dw(sess, module_name);
c8959a29 7488 }
20c6c071 7489
1acfc030
JS
7490 assert(dw);
7491
e90006d7
JL
7492 unsigned results_pre = finished_results.size();
7493
5896cd05 7494 if (sess.verbose > 3)
b530b5b3 7495 clog << _F("dwarf_builder::build for %s", module_name.c_str()) << endl;
5896cd05 7496
a794dbeb
FCE
7497 string dummy_mark_name; // NB: PR10245: dummy value, need not substitute - => __
7498 if (get_param(parameters, TOK_MARK, dummy_mark_name))
f28a8c28 7499 {
51d6bda3 7500 sdt_query sdtq(base, location, *dw, filled_parameters, finished_results, user_lib);
06de3a04 7501 dw->iterate_over_modules<base_query>(&query_module, &sdtq);
e90006d7
JL
7502
7503 // We need to update modules_seen with the modules we've visited
7504 modules_seen.insert(sdtq.visited_modules.begin(),
7505 sdtq.visited_modules.end());
7506
7507 // Did we fail to find a mark?
7508 if (results_pre == finished_results.size() && !location->from_glob)
7509 {
7510 string provider;
7511 get_param(filled_parameters, TOK_PROVIDER, provider);
7512
7513 string sugs = suggest_marks(sess, modules_seen, dummy_mark_name, provider);
7514 modules_seen.clear();
7515 if (!sugs.empty())
7516 throw SEMANTIC_ERROR (_NF("no match (similar mark: %s)",
7517 "no match (similar marks: %s)",
7518 sugs.find(',') == string::npos,
7519 sugs.c_str()));
7520 }
7521
edce5b67 7522 return;
7a05f484 7523 }
20c6c071 7524
6d5d594e 7525 dwarf_query q(base, location, *dw, filled_parameters, finished_results, user_path, user_lib);
7a24d422
FCE
7526
7527 // XXX: kernel.statement.absolute is a special case that requires no
7528 // dwfl processing. This code should be in a separate builder.
7a24d422 7529 if (q.has_kernel && q.has_absolute)
37ebca01 7530 {
4baf0e53 7531 // assert guru mode for absolute probes
37ebca01
FCE
7532 if (! q.base_probe->privileged)
7533 {
dc09353a 7534 throw SEMANTIC_ERROR (_("absolute statement probe in unprivileged script; need stap -g"),
edce5b67 7535 q.base_probe->tok);
37ebca01
FCE
7536 }
7537
7538 // For kernel.statement(NUM).absolute probe points, we bypass
7539 // all the debuginfo stuff: We just wire up a
7540 // dwarf_derived_probe right here and now.
4baf0e53 7541 dwarf_derived_probe* p =
b8da0ad1
FCE
7542 new dwarf_derived_probe ("", "", 0, "kernel", "",
7543 q.statement_num_val, q.statement_num_val,
7544 q, 0);
37ebca01 7545 finished_results.push_back (p);
1a0dbc5a 7546 sess.unwindsym_modules.insert ("kernel");
37ebca01
FCE
7547 return;
7548 }
7549
06de3a04 7550 dw->iterate_over_modules<base_query>(&query_module, &q);
8f14e444 7551
44ffe90c
JL
7552 // We need to update modules_seen with the modules we've visited
7553 modules_seen.insert(q.visited_modules.begin(),
7554 q.visited_modules.end());
8f14e444
FCE
7555
7556 // PR11553 special processing: .return probes requested, but
7557 // some inlined function instances matched.
7558 unsigned i_n_r = q.inlined_non_returnable.size();
7559 unsigned results_post = finished_results.size();
7560 if (i_n_r > 0)
7561 {
7562 if ((results_pre == results_post) && (! sess.suppress_warnings)) // no matches; issue warning
7563 {
7564 string quicklist;
7565 for (set<string>::iterator it = q.inlined_non_returnable.begin();
7566 it != q.inlined_non_returnable.end();
7567 it++)
7568 {
7569 quicklist += " " + (*it);
7570 if (quicklist.size() > 80) // heuristic, don't make an overlong report line
7571 {
7572 quicklist += " ...";
7573 break;
7574 }
7575 }
c57ea854 7576
52c2652f 7577 sess.print_warning (_NF("cannot probe .return of %u inlined function %s",
b530b5b3 7578 "cannot probe .return of %u inlined functions %s",
52c2652f 7579 quicklist.size(), i_n_r, quicklist.c_str()));
8f14e444
FCE
7580 // There will be also a "no matches" semantic error generated.
7581 }
7582 if (sess.verbose > 1)
52c2652f
NMA
7583 clog << _NF("skipped .return probe of %u inlined function",
7584 "skipped .return probe of %u inlined functions", i_n_r, i_n_r) << endl;
8f14e444
FCE
7585 if ((sess.verbose > 3) || (sess.verbose > 2 && results_pre == results_post)) // issue details with high verbosity
7586 {
7587 for (set<string>::iterator it = q.inlined_non_returnable.begin();
7588 it != q.inlined_non_returnable.end();
7589 it++)
7590 clog << (*it) << " ";
7591 clog << endl;
7592 }
7593 } // i_n_r > 0
44ffe90c 7594
e90006d7 7595 // If we just failed to resolve a function/plt by name, we can suggest
ef0943df
JL
7596 // something. We only suggest things for probe points that were not
7597 // synthesized from a glob, i.e. only for 'real' probes. This is also
7598 // required because modules_seen needs to accumulate across recursive
7599 // calls for process(glob)[.library(glob)] probes.
44ffe90c 7600 string func;
ef0943df 7601 if (results_pre == results_post && !location->from_glob
44ffe90c
JL
7602 && get_param(filled_parameters, TOK_FUNCTION, func)
7603 && !func.empty())
7604 {
0508c45f 7605 string sugs = suggest_dwarf_functions(sess, modules_seen, func);
44ffe90c
JL
7606 modules_seen.clear();
7607 if (!sugs.empty())
dc09353a 7608 throw SEMANTIC_ERROR (_NF("no match (similar function: %s)",
44ffe90c
JL
7609 "no match (similar functions: %s)",
7610 sugs.find(',') == string::npos,
7611 sugs.c_str()));
7612 }
ef0943df 7613 else if (results_pre == results_post && !location->from_glob
40a70f52
JL
7614 && get_param(filled_parameters, TOK_PLT, func)
7615 && !func.empty())
7616 {
7617 string sugs = suggest_plt_functions(sess, modules_seen, func);
7618 modules_seen.clear();
7619 if (!sugs.empty())
7620 throw SEMANTIC_ERROR (_NF("no match (similar function: %s)",
7621 "no match (similar functions: %s)",
7622 sugs.find(',') == string::npos,
7623 sugs.c_str()));
7624 }
44ffe90c
JL
7625 else if (results_pre != results_post)
7626 // Something was derived so we won't need to suggest something
7627 modules_seen.clear();
5f0a03a6
JK
7628}
7629
7630symbol_table::~symbol_table()
7631{
c9efa5c9 7632 delete_map(map_by_addr);
5f0a03a6
JK
7633}
7634
7635void
2867a2a1 7636symbol_table::add_symbol(const char *name, bool weak, bool descriptor,
44ffe90c 7637 Dwarf_Addr addr, Dwarf_Addr* /*high_addr*/)
5f0a03a6 7638{
ab91b232
JK
7639#ifdef __powerpc__
7640 // Map ".sys_foo" to "sys_foo".
7641 if (name[0] == '.')
7642 name++;
7643#endif
5f0a03a6
JK
7644 func_info *fi = new func_info();
7645 fi->addr = addr;
7646 fi->name = name;
ab91b232 7647 fi->weak = weak;
2867a2a1 7648 fi->descriptor = descriptor;
5f0a03a6
JK
7649 map_by_name[fi->name] = fi;
7650 // TODO: Use a multimap in case there are multiple static
7651 // functions with the same name?
1c6b77e5 7652 map_by_addr.insert(make_pair(addr, fi));
5f0a03a6
JK
7653}
7654
7655enum info_status
7656symbol_table::read_symbols(FILE *f, const string& path)
7657{
7658 // Based on do_kernel_symbols() in runtime/staprun/symbols.c
7659 int ret;
2e67a43b
TM
7660 char *name = 0;
7661 char *mod = 0;
5f0a03a6
JK
7662 char type;
7663 unsigned long long addr;
7664 Dwarf_Addr high_addr = 0;
7665 int line = 0;
7666
62a4021d
FCE
7667#if __GLIBC__ >2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 7)
7668#define MS_FMT "%ms"
7669#else
7670#define MS_FMT "%as"
7671#endif
7672 // %ms (newer than %as) mallocs space for the string and stores its address.
7673 while ((ret = fscanf(f, "%llx %c " MS_FMT " [" MS_FMT, &addr, &type, &name, &mod)) > 0)
5f0a03a6 7674 {
2e67a43b
TM
7675 auto_free free_name(name);
7676 auto_free free_mod(mod);
5f0a03a6
JK
7677 line++;
7678 if (ret < 3)
7679 {
3d372d6b 7680 cerr << _F("Symbol table error: Line %d of symbol list from %s is not in correct format: address type name [module]\n",
b530b5b3 7681 line, path.c_str());
5f0a03a6
JK
7682 // Caller should delete symbol_table object.
7683 return info_absent;
7684 }
2e67a43b 7685 else if (ret > 3)
5f0a03a6
JK
7686 {
7687 // Modules are loaded above the kernel, so if we're getting
7688 // modules, we're done.
2e67a43b 7689 break;
5f0a03a6 7690 }
ab91b232 7691 if (type == 'T' || type == 't' || type == 'W')
2867a2a1 7692 add_symbol(name, (type == 'W'), false, (Dwarf_Addr) addr, &high_addr);
5f0a03a6
JK
7693 }
7694
1c6b77e5 7695 if (map_by_addr.size() < 1)
5f0a03a6 7696 {
3d372d6b 7697 cerr << _F("Symbol table error: %s contains no function symbols.\n",
b530b5b3 7698 path.c_str()) << endl;
5f0a03a6
JK
7699 return info_absent;
7700 }
7701 return info_present;
7702}
7703
7704// NB: This currently unused. We use get_from_elf() instead because
7705// that gives us raw addresses -- which we need for modules -- whereas
7706// nm provides the address relative to the beginning of the section.
7707enum info_status
83ca3872 7708symbol_table::read_from_elf_file(const string &path,
2713ea24 7709 systemtap_session &sess)
5f0a03a6 7710{
58502ae4
JS
7711 vector<string> cmd;
7712 cmd.push_back("/usr/bin/nm");
7713 cmd.push_back("-n");
7714 cmd.push_back("--defined-only");
7715 cmd.push_back("path");
7716
5f0a03a6 7717 FILE *f;
58502ae4
JS
7718 int child_fd;
7719 pid_t child = stap_spawn_piped(sess.verbose, cmd, NULL, &child_fd);
7720 if (child <= 0 || !(f = fdopen(child_fd, "r")))
5f0a03a6 7721 {
58502ae4 7722 // nm failures are detected by stap_waitpid
3d372d6b 7723 cerr << _F("Internal error reading symbol table from %s -- %s\n",
b530b5b3 7724 path.c_str(), strerror(errno));
5f0a03a6
JK
7725 return info_absent;
7726 }
7727 enum info_status status = read_symbols(f, path);
58502ae4 7728 if (fclose(f) || stap_waitpid(sess.verbose, child))
5f0a03a6 7729 {
2713ea24
CM
7730 if (status == info_present)
7731 sess.print_warning("nm cannot read symbol table from " + path);
5f0a03a6
JK
7732 return info_absent;
7733 }
7734 return status;
7735}
7736
7737enum info_status
83ca3872 7738symbol_table::read_from_text_file(const string& path,
2713ea24 7739 systemtap_session &sess)
5f0a03a6
JK
7740{
7741 FILE *f = fopen(path.c_str(), "r");
7742 if (!f)
7743 {
2713ea24 7744 sess.print_warning("cannot read symbol table from " + path + " -- " + strerror(errno));
5f0a03a6
JK
7745 return info_absent;
7746 }
7747 enum info_status status = read_symbols(f, path);
7748 (void) fclose(f);
7749 return status;
7750}
7751
46f7b6be 7752void
f98c6346 7753symbol_table::prepare_section_rejection(Dwfl_Module *mod __attribute__ ((unused)))
46f7b6be
JK
7754{
7755#ifdef __powerpc__
7756 /*
7757 * The .opd section contains function descriptors that can look
7758 * just like function entry points. For example, there's a function
7759 * descriptor called "do_exit" that links to the entry point ".do_exit".
7760 * Reject all symbols in .opd.
7761 */
7762 opd_section = SHN_UNDEF;
7763 Dwarf_Addr bias;
7764 Elf* elf = (dwarf_getelf (dwfl_module_getdwarf (mod, &bias))
7765 ?: dwfl_module_getelf (mod, &bias));
7766 Elf_Scn* scn = 0;
7767 size_t shstrndx;
7768
7769 if (!elf)
7770 return;
fcc30d6d 7771 if (elf_getshdrstrndx (elf, &shstrndx) != 0)
46f7b6be
JK
7772 return;
7773 while ((scn = elf_nextscn(elf, scn)) != NULL)
7774 {
7775 GElf_Shdr shdr_mem;
7776 GElf_Shdr *shdr = gelf_getshdr(scn, &shdr_mem);
7777 if (!shdr)
7778 continue;
7779 const char *name = elf_strptr(elf, shstrndx, shdr->sh_name);
7780 if (!strcmp(name, ".opd"))
7781 {
7782 opd_section = elf_ndxscn(scn);
7783 return;
7784 }
7785 }
7786#endif
7787}
7788
7789bool
7790symbol_table::reject_section(GElf_Word section)
7791{
7792 if (section == SHN_UNDEF)
7793 return true;
7794#ifdef __powerpc__
7795 if (section == opd_section)
7796 return true;
7797#endif
7798 return false;
7799}
7800
5f0a03a6
JK
7801enum info_status
7802symbol_table::get_from_elf()
7803{
7804 Dwarf_Addr high_addr = 0;
7805 Dwfl_Module *mod = mod_info->mod;
7806 int syments = dwfl_module_getsymtab(mod);
7807 assert(syments);
46f7b6be 7808 prepare_section_rejection(mod);
5f0a03a6
JK
7809 for (int i = 1; i < syments; ++i)
7810 {
7811 GElf_Sym sym;
ab91b232
JK
7812 GElf_Word section;
7813 const char *name = dwfl_module_getsym(mod, i, &sym, &section);
2867a2a1 7814 if (name && GELF_ST_TYPE(sym.st_info) == STT_FUNC)
ab91b232 7815 add_symbol(name, (GELF_ST_BIND(sym.st_info) == STB_WEAK),
2867a2a1 7816 reject_section(section), sym.st_value, &high_addr);
5f0a03a6
JK
7817 }
7818 return info_present;
7819}
7820
5f0a03a6
JK
7821func_info *
7822symbol_table::get_func_containing_address(Dwarf_Addr addr)
7823{
1c6b77e5
JS
7824 iterator_t iter = map_by_addr.upper_bound(addr);
7825 if (iter == map_by_addr.begin())
5f0a03a6 7826 return NULL;
2e67a43b 7827 else
1c6b77e5 7828 return (--iter)->second;
5f0a03a6
JK
7829}
7830
3d372d6b
SC
7831func_info *
7832symbol_table::get_first_func()
7833{
7834 iterator_t iter = map_by_addr.begin();
7835 return (iter)->second;
7836}
7837
5f0a03a6
JK
7838func_info *
7839symbol_table::lookup_symbol(const string& name)
7840{
7841 map<string, func_info*>::iterator i = map_by_name.find(name);
7842 if (i == map_by_name.end())
7843 return NULL;
7844 return i->second;
7845}
7846
7847Dwarf_Addr
7848symbol_table::lookup_symbol_address(const string& name)
7849{
7850 func_info *fi = lookup_symbol(name);
7851 if (fi)
7852 return fi->addr;
7853 return 0;
7854}
7855
ab91b232
JK
7856// This is the kernel symbol table. The kernel macro cond_syscall creates
7857// a weak symbol for each system call and maps it to sys_ni_syscall.
7858// For system calls not implemented elsewhere, this weak symbol shows up
7859// in the kernel symbol table. Following the precedent of dwarfful stap,
7860// we refuse to consider such symbols. Here we delete them from our
7861// symbol table.
7862// TODO: Consider generalizing this and/or making it part of blacklist
7863// processing.
7864void
7865symbol_table::purge_syscall_stubs()
7866{
7867 Dwarf_Addr stub_addr = lookup_symbol_address("sys_ni_syscall");
7868 if (stub_addr == 0)
7869 return;
1c6b77e5 7870 range_t purge_range = map_by_addr.equal_range(stub_addr);
2e67a43b
TM
7871 for (iterator_t iter = purge_range.first;
7872 iter != purge_range.second;
1c6b77e5 7873 )
ab91b232 7874 {
1c6b77e5 7875 func_info *fi = iter->second;
2e67a43b 7876 if (fi->weak && fi->name != "sys_ni_syscall")
ab91b232 7877 {
2e67a43b 7878 map_by_name.erase(fi->name);
1c6b77e5 7879 map_by_addr.erase(iter++);
2e67a43b 7880 delete fi;
2e67a43b 7881 }
1c6b77e5
JS
7882 else
7883 iter++;
ab91b232
JK
7884 }
7885}
7886
5f0a03a6 7887void
d906ab9d 7888module_info::get_symtab(base_query *q)
5f0a03a6 7889{
1c6b77e5
JS
7890 if (symtab_status != info_unknown)
7891 return;
7892
5f0a03a6
JK
7893 sym_table = new symbol_table(this);
7894 if (!elf_path.empty())
7895 {
5f0a03a6
JK
7896 symtab_status = sym_table->get_from_elf();
7897 }
7898 else
7899 {
7900 assert(name == TOK_KERNEL);
ab3ed72d
DS
7901 symtab_status = info_absent;
7902 cerr << _("Error: Cannot find vmlinux.") << endl;;
5f0a03a6
JK
7903 }
7904 if (symtab_status == info_absent)
7905 {
7906 delete sym_table;
7907 sym_table = NULL;
7908 return;
7909 }
7910
ab91b232
JK
7911 if (name == TOK_KERNEL)
7912 sym_table->purge_syscall_stubs();
5f0a03a6
JK
7913}
7914
1c6b77e5
JS
7915// update_symtab reconciles data between the elf symbol table and the dwarf
7916// function enumeration. It updates the symbol table entries with the dwarf
7917// die that describes the function, which also signals to query_module_symtab
7918// that a statement probe isn't needed. In return, it also adds aliases to the
7919// function table for names that share the same addr/die.
7920void
7921module_info::update_symtab(cu_function_cache_t *funcs)
7922{
7923 if (!sym_table)
7924 return;
7925
7926 cu_function_cache_t new_funcs;
7927
7928 for (cu_function_cache_t::iterator func = funcs->begin();
7929 func != funcs->end(); func++)
7930 {
7931 // optimization: inlines will never be in the symbol table
7932 if (dwarf_func_inline(&func->second) != 0)
6e6d9a3b
JL
7933 {
7934 inlined_funcs.insert(func->first);
7935 continue;
7936 }
1c6b77e5 7937
1ffb8bd1
JS
7938 // XXX We may want to make additional efforts to match mangled elf names
7939 // to dwarf too. MIPS_linkage_name can help, but that's sometimes
7940 // missing, so we may also need to try matching by address. See also the
7941 // notes about _Z in dwflpp::iterate_over_functions().
7942
1c6b77e5
JS
7943 func_info *fi = sym_table->lookup_symbol(func->first);
7944 if (!fi)
7945 continue;
7946
7947 // iterate over all functions at the same address
7948 symbol_table::range_t er = sym_table->map_by_addr.equal_range(fi->addr);
7949 for (symbol_table::iterator_t it = er.first; it != er.second; ++it)
7950 {
7951 // update this function with the dwarf die
7952 it->second->die = func->second;
7953
7954 // if this function is a new alias, then
7955 // save it to merge into the function cache
7956 if (it->second != fi)
b7478964 7957 new_funcs.insert(make_pair(it->second->name, it->second->die));
1c6b77e5
JS
7958 }
7959 }
7960
7961 // add all discovered aliases back into the function cache
7962 // NB: this won't replace any names that dwarf may have already found
7963 funcs->insert(new_funcs.begin(), new_funcs.end());
7964}
7965
5f0a03a6
JK
7966module_info::~module_info()
7967{
7968 if (sym_table)
7969 delete sym_table;
b55bc428
FCE
7970}
7971
935447c8 7972// ------------------------------------------------------------------------
888af770 7973// user-space probes
935447c8
DS
7974// ------------------------------------------------------------------------
7975
935447c8 7976
888af770 7977struct uprobe_derived_probe_group: public generic_dpg<uprobe_derived_probe>
935447c8 7978{
89ba3085
FCE
7979private:
7980 string make_pbm_key (uprobe_derived_probe* p) {
cfcab6c7 7981 return p->path + "|" + p->module + "|" + p->section + "|" + lex_cast(p->pid);
89ba3085
FCE
7982 }
7983
cfcab6c7
JS
7984 void emit_module_maxuprobes (systemtap_session& s);
7985
2b69faaf
JS
7986 // Using our own utrace-based uprobes
7987 void emit_module_utrace_decls (systemtap_session& s);
7988 void emit_module_utrace_init (systemtap_session& s);
7989 void emit_module_utrace_exit (systemtap_session& s);
7990
7991 // Using the upstream inode-based uprobes
7992 void emit_module_inode_decls (systemtap_session& s);
7993 void emit_module_inode_init (systemtap_session& s);
7994 void emit_module_inode_exit (systemtap_session& s);
7995
3a894f7e
JS
7996 // Using the dyninst backend (via stapdyn)
7997 void emit_module_dyninst_decls (systemtap_session& s);
7998 void emit_module_dyninst_init (systemtap_session& s);
7999 void emit_module_dyninst_exit (systemtap_session& s);
8000
935447c8 8001public:
888af770 8002 void emit_module_decls (systemtap_session& s);
935447c8
DS
8003 void emit_module_init (systemtap_session& s);
8004 void emit_module_exit (systemtap_session& s);
8005};
8006
8007
888af770
FCE
8008void
8009uprobe_derived_probe::join_group (systemtap_session& s)
8010{
8011 if (! s.uprobe_derived_probes)
8012 s.uprobe_derived_probes = new uprobe_derived_probe_group ();
8013 s.uprobe_derived_probes->enroll (this);
f31a77f5
DS
8014 if (s.runtime_usermode_p())
8015 enable_dynprobes(s);
8016 else
4441e344 8017 enable_task_finder(s);
a96d1db0 8018
8a03658e 8019 // Ask buildrun.cxx to build extra module if needed, and
d3e959b0
DS
8020 // signal staprun to load that module. If we're using the builtin
8021 // inode-uprobes, we still need to know that it is required.
8a03658e 8022 s.need_uprobes = true;
a96d1db0
DN
8023}
8024
888af770 8025
c0f84e7b
SC
8026void
8027uprobe_derived_probe::getargs(std::list<std::string> &arg_set) const
8028{
8029 dwarf_derived_probe::getargs(arg_set);
8030 arg_set.insert(arg_set.end(), args.begin(), args.end());
8031}
8032
8033
8034void
8035uprobe_derived_probe::saveargs(int nargs)
8036{
8037 for (int i = 1; i <= nargs; i++)
8038 args.push_back("$arg" + lex_cast (i) + ":long");
8039}
8040
8041
2865d17a 8042void
42e38653 8043uprobe_derived_probe::emit_privilege_assertion (translator_output* o)
2865d17a
DB
8044{
8045 // These probes are allowed for unprivileged users, but only in the
8046 // context of processes which they own.
8047 emit_process_owner_assertion (o);
8048}
8049
8050
888af770 8051struct uprobe_builder: public derived_probe_builder
a96d1db0 8052{
888af770 8053 uprobe_builder() {}
2b69faaf 8054 virtual void build(systemtap_session & sess,
a96d1db0
DN
8055 probe * base,
8056 probe_point * location,
86bf665e 8057 literal_map_t const & parameters,
a96d1db0
DN
8058 vector<derived_probe *> & finished_results)
8059 {
888af770 8060 int64_t process, address;
a96d1db0 8061
2b69faaf 8062 if (kernel_supports_inode_uprobes(sess))
dc09353a 8063 throw SEMANTIC_ERROR (_("absolute process probes not available [man error::inode-uprobes]"));
2b69faaf 8064
888af770 8065 bool b1 = get_param (parameters, TOK_PROCESS, process);
ced347a9 8066 (void) b1;
888af770 8067 bool b2 = get_param (parameters, TOK_STATEMENT, address);
ced347a9 8068 (void) b2;
888af770
FCE
8069 bool rr = has_null_param (parameters, TOK_RETURN);
8070 assert (b1 && b2); // by pattern_root construction
a96d1db0 8071
0973d815 8072 finished_results.push_back(new uprobe_derived_probe(base, location, process, address, rr));
a96d1db0
DN
8073 }
8074};
8075
8076
8077void
cfcab6c7 8078uprobe_derived_probe_group::emit_module_maxuprobes (systemtap_session& s)
a96d1db0 8079{
43241c44
FCE
8080 // We'll probably need at least this many:
8081 unsigned minuprobes = probes.size();
8082 // .. but we don't want so many that .bss is inflated (PR10507):
8083 unsigned uprobesize = 64;
8084 unsigned maxuprobesmem = 10*1024*1024; // 10 MB
8085 unsigned maxuprobes = maxuprobesmem / uprobesize;
8086
aaf7ffe8
FCE
8087 // Let's choose a value on the geometric middle. This should end up
8088 // between minuprobes and maxuprobes. It's OK if this number turns
8089 // out to be < minuprobes or > maxuprobes. At worst, we get a
8090 // run-time error of one kind (too few: missed uprobe registrations)
8091 // or another (too many: vmalloc errors at module load time).
8092 unsigned default_maxuprobes = (unsigned)sqrt((double)minuprobes * (double)maxuprobes);
43241c44 8093
6d0f3f0c 8094 s.op->newline() << "#ifndef MAXUPROBES";
43241c44 8095 s.op->newline() << "#define MAXUPROBES " << default_maxuprobes;
6d0f3f0c 8096 s.op->newline() << "#endif";
cfcab6c7
JS
8097}
8098
8099
8100void
8101uprobe_derived_probe_group::emit_module_utrace_decls (systemtap_session& s)
8102{
8103 if (probes.empty()) return;
8104 s.op->newline() << "/* ---- utrace uprobes ---- */";
8105 // If uprobes isn't in the kernel, pull it in from the runtime.
8106
8107 s.op->newline() << "#if defined(CONFIG_UPROBES) || defined(CONFIG_UPROBES_MODULE)";
8108 s.op->newline() << "#include <linux/uprobes.h>";
8109 s.op->newline() << "#else";
2ba1736a 8110 s.op->newline() << "#include \"linux/uprobes/uprobes.h\"";
cfcab6c7
JS
8111 s.op->newline() << "#endif";
8112 s.op->newline() << "#ifndef UPROBES_API_VERSION";
8113 s.op->newline() << "#define UPROBES_API_VERSION 1";
8114 s.op->newline() << "#endif";
8115
8116 emit_module_maxuprobes (s);
a96d1db0 8117
cc52276b 8118 // Forward decls
2ba1736a 8119 s.op->newline() << "#include \"linux/uprobes-common.h\"";
cc52276b 8120
5e112f92
FCE
8121 // In .bss, the shared pool of uprobe/uretprobe structs. These are
8122 // too big to embed in the initialized .data stap_uprobe_spec array.
cc52276b
WC
8123 // XXX: consider a slab cache or somesuch for stap_uprobes
8124 s.op->newline() << "static struct stap_uprobe stap_uprobes [MAXUPROBES];";
6b378b7c 8125 s.op->newline() << "static DEFINE_MUTEX(stap_uprobes_lock);"; // protects against concurrent registration/unregistration
a96d1db0 8126
89ba3085
FCE
8127 s.op->assert_0_indent();
8128
89ba3085
FCE
8129 // Assign task-finder numbers as we build up the stap_uprobe_tf table.
8130 // This means we process probes[] in two passes.
8131 map <string,unsigned> module_index;
8132 unsigned module_index_ctr = 0;
8133
cc52276b
WC
8134 // not const since embedded task_finder_target struct changes
8135 s.op->newline() << "static struct stap_uprobe_tf stap_uprobe_finders[] = {";
89ba3085
FCE
8136 s.op->indent(1);
8137 for (unsigned i=0; i<probes.size(); i++)
8138 {
8139 uprobe_derived_probe *p = probes[i];
8140 string pbmkey = make_pbm_key (p);
8141 if (module_index.find (pbmkey) == module_index.end())
8142 {
8143 module_index[pbmkey] = module_index_ctr++;
8144
8145 s.op->newline() << "{";
8146 // NB: it's essential that make_pbm_key() use all of and
8147 // only the same fields as we're about to emit.
8148 s.op->line() << " .finder={";
1af100fc 8149 s.op->line() << " .purpose=\"uprobes\",";
89ba3085 8150 if (p->pid != 0)
68910c97
JK
8151 s.op->line() << " .pid=" << p->pid << ",";
8152
8153 if (p->section == "") // .statement(addr).absolute
8154 s.op->line() << " .callback=&stap_uprobe_process_found,";
89ba3085
FCE
8155 else if (p->section == ".absolute") // proxy for ET_EXEC -> exec()'d program
8156 {
8157 s.op->line() << " .procname=" << lex_cast_qstring(p->module) << ",";
8158 s.op->line() << " .callback=&stap_uprobe_process_found,";
8159 }
68910c97 8160 else if (p->section != ".absolute") // ET_DYN
89ba3085 8161 {
4ad95bbc
SC
8162 if (p->has_library)
8163 s.op->line() << " .procname=\"" << p->path << "\", ";
89ba3085
FCE
8164 s.op->line() << " .mmap_callback=&stap_uprobe_mmap_found, ";
8165 s.op->line() << " .munmap_callback=&stap_uprobe_munmap_found, ";
19d91f6c 8166 s.op->line() << " .callback=&stap_uprobe_process_munmap,";
89ba3085 8167 }
89ba3085 8168 s.op->line() << " },";
68910c97
JK
8169 if (p->module != "")
8170 s.op->line() << " .pathname=" << lex_cast_qstring(p->module) << ", ";
89ba3085
FCE
8171 s.op->line() << " },";
8172 }
c57ea854 8173 else
822a6a3d 8174 { } // skip it in this pass, already have a suitable stap_uprobe_tf slot for it.
89ba3085
FCE
8175 }
8176 s.op->newline(-1) << "};";
8177
8178 s.op->assert_0_indent();
8179
3689db05
SC
8180 unsigned pci;
8181 for (pci=0; pci<probes.size(); pci++)
8182 {
8183 // List of perf counters used by each probe
8184 // This list is an index into struct stap_perf_probe,
8185 uprobe_derived_probe *p = probes[pci];
698de6cc 8186 std::set<derived_probe*>::iterator pcii;
3689db05
SC
8187 s.op->newline() << "long perf_counters_" + lex_cast(pci) + "[] = {";
8188 for (pcii = p->perf_counter_refs.begin();
8189 pcii != p->perf_counter_refs.end(); pcii++)
8190 {
8191 map<string, pair<string,derived_probe*> >::iterator it;
8192 unsigned i = 0;
8193 // Find the associated perf.counter probe
8194 for (it=s.perf_counters.begin() ;
8195 it != s.perf_counters.end(); it++, i++)
8196 if ((*it).second.second == (*pcii))
8197 break;
8198 s.op->line() << lex_cast(i) << ", ";
8199 }
8200 s.op->newline() << "};";
8201 }
8202
cc52276b
WC
8203 // NB: read-only structure
8204 s.op->newline() << "static const struct stap_uprobe_spec stap_uprobe_specs [] = {";
a96d1db0 8205 s.op->indent(1);
888af770
FCE
8206 for (unsigned i =0; i<probes.size(); i++)
8207 {
8208 uprobe_derived_probe* p = probes[i];
8209 s.op->newline() << "{";
89ba3085
FCE
8210 string key = make_pbm_key (p);
8211 unsigned value = module_index[key];
759e1d76
FCE
8212 if (value != 0)
8213 s.op->line() << " .tfi=" << value << ",";
6b66b9f7 8214 s.op->line() << " .address=(unsigned long)0x" << hex << p->addr << dec << "ULL,";
faea5e16 8215 s.op->line() << " .probe=" << common_probe_init (p) << ",";
4ddb6dd0 8216
038c38c6 8217 if (p->sdt_semaphore_addr != 0)
63b4fd14 8218 s.op->line() << " .sdt_sem_offset=(unsigned long)0x"
038c38c6 8219 << hex << p->sdt_semaphore_addr << dec << "ULL,";
4ddb6dd0 8220
0d049a1d 8221 // XXX: don't bother emit if array is empty
3689db05
SC
8222 s.op->line() << " .perf_counters_dim=ARRAY_SIZE(perf_counters_" << lex_cast(i) << "),";
8223 // List of perf counters used by a probe from above
0d049a1d 8224 s.op->line() << " .perf_counters=perf_counters_" + lex_cast(i) + ",";
3689db05 8225
4ddb6dd0
JS
8226 if (p->has_return)
8227 s.op->line() << " .return_p=1,";
888af770
FCE
8228 s.op->line() << " },";
8229 }
8230 s.op->newline(-1) << "};";
a96d1db0 8231
89ba3085
FCE
8232 s.op->assert_0_indent();
8233
48e685da 8234 s.op->newline() << "static void enter_uprobe_probe (struct uprobe *inst, struct pt_regs *regs) {";
888af770 8235 s.op->newline(1) << "struct stap_uprobe *sup = container_of(inst, struct stap_uprobe, up);";
89ba3085 8236 s.op->newline() << "const struct stap_uprobe_spec *sups = &stap_uprobe_specs [sup->spec_index];";
71db462b 8237 common_probe_entryfn_prologue (s, "STAP_SESSION_RUNNING", "sups->probe",
cda141c2 8238 "stp_probe_type_uprobe");
0e090c74 8239 s.op->newline() << "if (sup->spec_index < 0 || "
6e895029
DS
8240 << "sup->spec_index >= " << probes.size() << ") {";
8241 s.op->newline(1) << "_stp_error (\"bad spec_index %d (max " << probes.size()
0e090c74 8242 << "): %s\", sup->spec_index, c->probe_point);";
6e895029
DS
8243 s.op->newline() << "atomic_dec (&c->busy);";
8244 s.op->newline() << "goto probe_epilogue;";
8245 s.op->newline(-1) << "}";
d9aed31e 8246 s.op->newline() << "c->uregs = regs;";
e04b5d74 8247 s.op->newline() << "c->user_mode_p = 1;";
6415ddde
MW
8248
8249 // Make it look like the IP is set as it would in the actual user
8250 // task when calling real probe handler. Reset IP regs on return, so
8251 // we don't confuse uprobes. PR10458
8252 s.op->newline() << "{";
8253 s.op->indent(1);
d9aed31e 8254 s.op->newline() << "unsigned long uprobes_ip = REG_IP(c->uregs);";
259d54c0 8255 s.op->newline() << "SET_REG_IP(regs, inst->vaddr);";
26e63673 8256 s.op->newline() << "(*sups->probe->ph) (c);";
259d54c0 8257 s.op->newline() << "SET_REG_IP(regs, uprobes_ip);";
6415ddde
MW
8258 s.op->newline(-1) << "}";
8259
f887a8c9 8260 common_probe_entryfn_epilogue (s, true);
888af770 8261 s.op->newline(-1) << "}";
a96d1db0 8262
48e685da 8263 s.op->newline() << "static void enter_uretprobe_probe (struct uretprobe_instance *inst, struct pt_regs *regs) {";
888af770 8264 s.op->newline(1) << "struct stap_uprobe *sup = container_of(inst->rp, struct stap_uprobe, urp);";
89ba3085 8265 s.op->newline() << "const struct stap_uprobe_spec *sups = &stap_uprobe_specs [sup->spec_index];";
71db462b 8266 common_probe_entryfn_prologue (s, "STAP_SESSION_RUNNING", "sups->probe",
cda141c2 8267 "stp_probe_type_uretprobe");
6dceb5c9 8268 s.op->newline() << "c->ips.ri = inst;";
0e090c74 8269 s.op->newline() << "if (sup->spec_index < 0 || "
0d5561a5
DS
8270 << "sup->spec_index >= " << probes.size() << ") {";
8271 s.op->newline(1) << "_stp_error (\"bad spec_index %d (max " << probes.size()
0e090c74 8272 << "): %s\", sup->spec_index, c->probe_point);";
0d5561a5
DS
8273 s.op->newline() << "atomic_dec (&c->busy);";
8274 s.op->newline() << "goto probe_epilogue;";
8275 s.op->newline(-1) << "}";
8276
d9aed31e 8277 s.op->newline() << "c->uregs = regs;";
e04b5d74 8278 s.op->newline() << "c->user_mode_p = 1;";
6415ddde
MW
8279
8280 // Make it look like the IP is set as it would in the actual user
8281 // task when calling real probe handler. Reset IP regs on return, so
8282 // we don't confuse uprobes. PR10458
8283 s.op->newline() << "{";
8284 s.op->indent(1);
d9aed31e 8285 s.op->newline() << "unsigned long uprobes_ip = REG_IP(c->uregs);";
5e562a69 8286 s.op->newline() << "SET_REG_IP(regs, inst->ret_addr);";
26e63673 8287 s.op->newline() << "(*sups->probe->ph) (c);";
259d54c0 8288 s.op->newline() << "SET_REG_IP(regs, uprobes_ip);";
6415ddde
MW
8289 s.op->newline(-1) << "}";
8290
f887a8c9 8291 common_probe_entryfn_epilogue (s, true);
a96d1db0
DN
8292 s.op->newline(-1) << "}";
8293
89ba3085 8294 s.op->newline();
2ba1736a 8295 s.op->newline() << "#include \"linux/uprobes-common.c\"";
6d0f3f0c 8296 s.op->newline();
888af770 8297}
935447c8
DS
8298
8299
888af770 8300void
2b69faaf 8301uprobe_derived_probe_group::emit_module_utrace_init (systemtap_session& s)
935447c8 8302{
888af770 8303 if (probes.empty()) return;
935447c8 8304
2b69faaf 8305 s.op->newline() << "/* ---- utrace uprobes ---- */";
935447c8 8306
01b05e2e 8307 s.op->newline() << "for (j=0; j<MAXUPROBES; j++) {";
5e112f92
FCE
8308 s.op->newline(1) << "struct stap_uprobe *sup = & stap_uprobes[j];";
8309 s.op->newline() << "sup->spec_index = -1;"; // free slot
80b4ad8b
FCE
8310 // NB: we assume the rest of the struct (specificaly, sup->up) is
8311 // initialized to zero. This is so that we can use
8312 // sup->up->kdata = NULL for "really free!" PR 6829.
5e112f92
FCE
8313 s.op->newline(-1) << "}";
8314 s.op->newline() << "mutex_init (& stap_uprobes_lock);";
935447c8 8315
89ba3085
FCE
8316 // Set up the task_finders
8317 s.op->newline() << "for (i=0; i<sizeof(stap_uprobe_finders)/sizeof(stap_uprobe_finders[0]); i++) {";
8318 s.op->newline(1) << "struct stap_uprobe_tf *stf = & stap_uprobe_finders[i];";
c57ea854 8319 s.op->newline() << "probe_point = stf->pathname;"; // for error messages; XXX: would prefer pp() or something better
89ba3085 8320 s.op->newline() << "rc = stap_register_task_finder_target (& stf->finder);";
935447c8 8321
5e112f92
FCE
8322 // NB: if (rc), there is no need (XXX: nor any way) to clean up any
8323 // finders already registered, since mere registration does not
8324 // cause any utrace or memory allocation actions. That happens only
8325 // later, once the task finder engine starts running. So, for a
8326 // partial initialization requiring unwind, we need do nothing.
8327 s.op->newline() << "if (rc) break;";
a7a68293 8328
888af770
FCE
8329 s.op->newline(-1) << "}";
8330}
d0ea46ce 8331
d0a7f5a9 8332
888af770 8333void
2b69faaf 8334uprobe_derived_probe_group::emit_module_utrace_exit (systemtap_session& s)
888af770
FCE
8335{
8336 if (probes.empty()) return;
2b69faaf 8337 s.op->newline() << "/* ---- utrace uprobes ---- */";
e56e51c9 8338
6d0f3f0c
FCE
8339 // NB: there is no stap_unregister_task_finder_target call;
8340 // important stuff like utrace cleanups are done by
d41d451c
FCE
8341 // __stp_task_finder_cleanup() via stap_stop_task_finder().
8342 //
8343 // This function blocks until all callbacks are completed, so there
8344 // is supposed to be no possibility of any registration-related code starting
8345 // to run in parallel with our shutdown here. So we don't need to protect the
8346 // stap_uprobes[] array with the mutex.
d0a7f5a9 8347
01b05e2e 8348 s.op->newline() << "for (j=0; j<MAXUPROBES; j++) {";
5e112f92 8349 s.op->newline(1) << "struct stap_uprobe *sup = & stap_uprobes[j];";
89ba3085 8350 s.op->newline() << "const struct stap_uprobe_spec *sups = &stap_uprobe_specs [sup->spec_index];";
6d0f3f0c 8351 s.op->newline() << "if (sup->spec_index < 0) continue;"; // free slot
3568f1dd 8352
8faa1fc5 8353 // PR10655: decrement that ENABLED semaphore
c116c31b 8354 s.op->newline() << "if (sup->sdt_sem_address) {";
8faa1fc5
FCE
8355 s.op->newline(1) << "unsigned short sdt_semaphore;"; // NB: fixed size
8356 s.op->newline() << "pid_t pid = (sups->return_p ? sup->urp.u.pid : sup->up.pid);";
8357 s.op->newline() << "struct task_struct *tsk;";
8358 s.op->newline() << "rcu_read_lock();";
6846cfc8 8359
86229a55
DS
8360 // Do a pid->task_struct* lookup. For 2.6.24+, this code assumes
8361 // that the pid is always in the global namespace, not in any
8362 // private namespace.
8faa1fc5 8363 s.op->newline() << "#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,24)";
86229a55
DS
8364 // We'd like to call find_task_by_pid_ns() here, but it isn't
8365 // exported. So, we call what it calls...
8366 s.op->newline() << " tsk = pid_task(find_pid_ns(pid, &init_pid_ns), PIDTYPE_PID);";
8faa1fc5
FCE
8367 s.op->newline() << "#else";
8368 s.op->newline() << " tsk = find_task_by_pid (pid);";
8369 s.op->newline() << "#endif /* 2.6.24 */";
8faa1fc5
FCE
8370
8371 s.op->newline() << "if (tsk) {"; // just in case the thing exited while we weren't watching
3c5b8e2b 8372 s.op->newline(1) << "if (__access_process_vm_noflush(tsk, sup->sdt_sem_address, &sdt_semaphore, sizeof(sdt_semaphore), 0)) {";
63b4fd14 8373 s.op->newline(1) << "sdt_semaphore --;";
903b9fcd 8374 s.op->newline() << "#ifdef DEBUG_UPROBES";
c116c31b 8375 s.op->newline() << "_stp_dbug (__FUNCTION__,__LINE__, \"-semaphore %#x @ %#lx\\n\", sdt_semaphore, sup->sdt_sem_address);";
903b9fcd 8376 s.op->newline() << "#endif";
3c5b8e2b 8377 s.op->newline() << "__access_process_vm_noflush(tsk, sup->sdt_sem_address, &sdt_semaphore, sizeof(sdt_semaphore), 1);";
93c84191 8378 s.op->newline(-1) << "}";
8faa1fc5
FCE
8379 // XXX: need to analyze possibility of race condition
8380 s.op->newline(-1) << "}";
8381 s.op->newline() << "rcu_read_unlock();";
8382 s.op->newline(-1) << "}";
6846cfc8 8383
3568f1dd
FCE
8384 s.op->newline() << "if (sups->return_p) {";
8385 s.op->newline(1) << "#ifdef DEBUG_UPROBES";
89ba3085 8386 s.op->newline() << "_stp_dbug (__FUNCTION__,__LINE__, \"-uretprobe spec %d index %d pid %d addr %p\\n\", sup->spec_index, j, sup->up.pid, (void*) sup->up.vaddr);";
3568f1dd 8387 s.op->newline() << "#endif";
80b4ad8b
FCE
8388 // NB: PR6829 does not change that we still need to unregister at
8389 // *this* time -- when the script as a whole exits.
3568f1dd
FCE
8390 s.op->newline() << "unregister_uretprobe (& sup->urp);";
8391 s.op->newline(-1) << "} else {";
8392 s.op->newline(1) << "#ifdef DEBUG_UPROBES";
8faa1fc5 8393 s.op->newline() << "_stp_dbug (__FUNCTION__,__LINE__, \"-uprobe spec %d index %d pid %d addr %p\\n\", sup->spec_index, j, sup->up.pid, (void*) sup->up.vaddr);";
3568f1dd
FCE
8394 s.op->newline() << "#endif";
8395 s.op->newline() << "unregister_uprobe (& sup->up);";
8396 s.op->newline(-1) << "}";
935447c8 8397
6d0f3f0c 8398 s.op->newline() << "sup->spec_index = -1;";
935447c8 8399
3568f1dd
FCE
8400 // XXX: uprobe missed counts?
8401
6d0f3f0c 8402 s.op->newline(-1) << "}";
935447c8 8403
5e112f92 8404 s.op->newline() << "mutex_destroy (& stap_uprobes_lock);";
935447c8
DS
8405}
8406
2b69faaf
JS
8407
8408void
8409uprobe_derived_probe_group::emit_module_inode_decls (systemtap_session& s)
8410{
8411 if (probes.empty()) return;
8412 s.op->newline() << "/* ---- inode uprobes ---- */";
cfcab6c7 8413 emit_module_maxuprobes (s);
2ba1736a 8414 s.op->newline() << "#include \"linux/uprobes-inode.c\"";
2b69faaf
JS
8415
8416 // Write the probe handler.
79af55c3
JS
8417 s.op->newline() << "static int stapiu_probe_handler "
8418 << "(struct stapiu_consumer *sup, struct pt_regs *regs) {";
8419 s.op->newline(1);
2dbbd473
JS
8420
8421 // Since we're sharing the entry function, we have to dynamically choose the probe_type
8422 string probe_type = "(sup->return_p ? stp_probe_type_uretprobe : stp_probe_type_uprobe)";
71db462b 8423 common_probe_entryfn_prologue (s, "STAP_SESSION_RUNNING", "sup->probe",
2dbbd473
JS
8424 probe_type);
8425
3bff6634 8426 s.op->newline() << "c->uregs = regs;";
e04b5d74 8427 s.op->newline() << "c->user_mode_p = 1;";
497cfcb2 8428 // NB: IP is already set by stapiu_probe_prehandler in uprobes-inode.c
2b69faaf 8429 s.op->newline() << "(*sup->probe->ph) (c);";
39e63bae 8430
f887a8c9 8431 common_probe_entryfn_epilogue (s, true);
2b69faaf
JS
8432 s.op->newline() << "return 0;";
8433 s.op->newline(-1) << "}";
8434 s.op->assert_0_indent();
8435
8436 // Index of all the modules for which we need inodes.
8437 map<string, unsigned> module_index;
8438 unsigned module_index_ctr = 0;
8439
8440 // Discover and declare targets for each unique path.
cfcab6c7 8441 s.op->newline() << "static struct stapiu_target "
2b69faaf
JS
8442 << "stap_inode_uprobe_targets[] = {";
8443 s.op->indent(1);
8444 for (unsigned i=0; i<probes.size(); i++)
8445 {
8446 uprobe_derived_probe *p = probes[i];
cfcab6c7
JS
8447 const string key = make_pbm_key(p);
8448 if (module_index.find (key) == module_index.end())
2b69faaf 8449 {
cfcab6c7
JS
8450 module_index[key] = module_index_ctr++;
8451 s.op->newline() << "{";
8452 s.op->line() << " .finder={";
b78a0fbb 8453 s.op->line() << " .purpose=\"inode-uprobes\",";
cfcab6c7
JS
8454 if (p->pid != 0)
8455 s.op->line() << " .pid=" << p->pid << ",";
8456
8457 if (p->section == "") // .statement(addr).absolute XXX?
8458 s.op->line() << " .callback=&stapiu_process_found,";
8459 else if (p->section == ".absolute") // proxy for ET_EXEC -> exec()'d program
8460 {
8461 s.op->line() << " .procname=" << lex_cast_qstring(p->module) << ",";
8462 s.op->line() << " .callback=&stapiu_process_found,";
8463 }
8464 else if (p->section != ".absolute") // ET_DYN
8465 {
8466 if (p->has_library)
8467 s.op->line() << " .procname=\"" << p->path << "\", ";
8468 s.op->line() << " .mmap_callback=&stapiu_mmap_found, ";
8469 s.op->line() << " .munmap_callback=&stapiu_munmap_found, ";
8470 s.op->line() << " .callback=&stapiu_process_munmap,";
8471 }
8472 s.op->line() << " },";
8473 s.op->line() << " .filename=" << lex_cast_qstring(p->module) << ",";
8474 s.op->line() << " },";
2b69faaf
JS
8475 }
8476 }
8477 s.op->newline(-1) << "};";
8478 s.op->assert_0_indent();
8479
8480 // Declare the actual probes.
3689db05
SC
8481 unsigned pci;
8482 for (pci=0; pci<probes.size(); pci++)
8483 {
8484 // List of perf counters used by each probe
8485 // This list is an index into struct stap_perf_probe,
8486 uprobe_derived_probe *p = probes[pci];
698de6cc 8487 std::set<derived_probe*>::iterator pcii;
3689db05
SC
8488 s.op->newline() << "long perf_counters_" + lex_cast(pci) + "[] = {";
8489 for (pcii = p->perf_counter_refs.begin();
8490 pcii != p->perf_counter_refs.end(); pcii++)
8491 {
8492 map<string, pair<string,derived_probe*> >::iterator it;
8493 unsigned i = 0;
8494 // Find the associated perf.counter probe
4fa83377
SC
8495 for (it=s.perf_counters.begin() ;
8496 it != s.perf_counters.end(); it++, i++)
3689db05
SC
8497 if ((*it).second.second == (*pcii))
8498 break;
8499 s.op->line() << lex_cast(i) << ", ";
8500 }
8501 s.op->newline() << "};";
8502 }
8503
cfcab6c7 8504 s.op->newline() << "static struct stapiu_consumer "
2b69faaf
JS
8505 << "stap_inode_uprobe_consumers[] = {";
8506 s.op->indent(1);
8507 for (unsigned i=0; i<probes.size(); i++)
8508 {
8509 uprobe_derived_probe *p = probes[i];
cfcab6c7
JS
8510 unsigned index = module_index[make_pbm_key(p)];
8511 s.op->newline() << "{";
79af55c3
JS
8512 if (p->has_return)
8513 s.op->line() << " .return_p=1,";
cfcab6c7
JS
8514 s.op->line() << " .target=&stap_inode_uprobe_targets[" << index << "],";
8515 s.op->line() << " .offset=(loff_t)0x" << hex << p->addr << dec << "ULL,";
8516 if (p->sdt_semaphore_addr)
8517 s.op->line() << " .sdt_sem_offset=(loff_t)0x"
8518 << hex << p->sdt_semaphore_addr << dec << "ULL,";
0d049a1d 8519 // XXX: don't bother emit if array is empty
3689db05
SC
8520 s.op->line() << " .perf_counters_dim=ARRAY_SIZE(perf_counters_" << lex_cast(i) << "),";
8521 // List of perf counters used by a probe from above
0d049a1d 8522 s.op->line() << " .perf_counters=perf_counters_" + lex_cast(i) + ",";
cfcab6c7
JS
8523 s.op->line() << " .probe=" << common_probe_init (p) << ",";
8524 s.op->line() << " },";
2b69faaf
JS
8525 }
8526 s.op->newline(-1) << "};";
8527 s.op->assert_0_indent();
8528}
8529
8530
8531void
8532uprobe_derived_probe_group::emit_module_inode_init (systemtap_session& s)
8533{
8534 if (probes.empty()) return;
8535 s.op->newline() << "/* ---- inode uprobes ---- */";
b0f614a0
DS
8536 // Let stapiu_init() handle reporting errors by setting probe_point
8537 // to NULL.
8538 s.op->newline() << "probe_point = NULL;";
cfcab6c7 8539 s.op->newline() << "rc = stapiu_init ("
2b69faaf
JS
8540 << "stap_inode_uprobe_targets, "
8541 << "ARRAY_SIZE(stap_inode_uprobe_targets), "
8542 << "stap_inode_uprobe_consumers, "
8543 << "ARRAY_SIZE(stap_inode_uprobe_consumers));";
8544}
8545
8546
8547void
8548uprobe_derived_probe_group::emit_module_inode_exit (systemtap_session& s)
8549{
8550 if (probes.empty()) return;
8551 s.op->newline() << "/* ---- inode uprobes ---- */";
cfcab6c7 8552 s.op->newline() << "stapiu_exit ("
2b69faaf
JS
8553 << "stap_inode_uprobe_targets, "
8554 << "ARRAY_SIZE(stap_inode_uprobe_targets), "
8555 << "stap_inode_uprobe_consumers, "
8556 << "ARRAY_SIZE(stap_inode_uprobe_consumers));";
8557}
8558
8559
3a894f7e
JS
8560void
8561uprobe_derived_probe_group::emit_module_dyninst_decls (systemtap_session& s)
8562{
8563 if (probes.empty()) return;
8564 s.op->newline() << "/* ---- dyninst uprobes ---- */";
8565 emit_module_maxuprobes (s);
e00f3fb7 8566 s.op->newline() << "#include \"dyninst/uprobes.h\"";
3a894f7e 8567
f31a77f5
DS
8568 // Let the dynprobe_derived_probe_group handle outputting targets
8569 // and probes. This allows us to merge different types of probes.
8570 s.op->newline() << "static struct stapdu_probe stapdu_probes[];";
8571 for (unsigned i = 0; i < probes.size(); i++)
3a894f7e
JS
8572 {
8573 uprobe_derived_probe *p = probes[i];
e00f3fb7 8574
f31a77f5
DS
8575 dynprobe_add_uprobe(s, p->module, p->addr, p->sdt_semaphore_addr,
8576 (p->has_return ? "STAPDYN_PROBE_FLAG_RETURN" : "0"),
8577 common_probe_init(p));
3a894f7e 8578 }
874d38bf
JS
8579 // loc2c-generated code assumes pt_regs are available, so use this to make
8580 // sure we always have *something* for it to dereference...
f31a77f5 8581 s.op->newline() << "static struct pt_regs stapdu_dummy_uregs;";
e00f3fb7 8582
3a894f7e
JS
8583 // Write the probe handler.
8584 // NB: not static, so dyninst can find it
8585 s.op->newline() << "int enter_dyninst_uprobe "
8586 << "(uint64_t index, struct pt_regs *regs) {";
e00f3fb7 8587 s.op->newline(1) << "struct stapdu_probe *sup = &stapdu_probes[index];";
92b97104
JS
8588
8589 // Since we're sharing the entry function, we have to dynamically choose the probe_type
8590 string probe_type = "((sup->flags & STAPDYN_PROBE_FLAG_RETURN) ?"
8591 " stp_probe_type_uretprobe : stp_probe_type_uprobe)";
71db462b 8592 common_probe_entryfn_prologue (s, "STAP_SESSION_RUNNING", "sup->probe",
92b97104
JS
8593 probe_type);
8594
874d38bf 8595 s.op->newline() << "c->uregs = regs ?: &stapdu_dummy_uregs;";
e04b5d74 8596 s.op->newline() << "c->user_mode_p = 1;";
3a894f7e
JS
8597 // XXX: once we have regs, check how dyninst sets the IP
8598 // XXX: the way that dyninst rewrites stuff is probably going to be
8599 // ... very confusing to our backtracer (at least if we stay in process)
8600 s.op->newline() << "(*sup->probe->ph) (c);";
f887a8c9 8601 common_probe_entryfn_epilogue (s, true);
3a894f7e
JS
8602 s.op->newline() << "return 0;";
8603 s.op->newline(-1) << "}";
3debb935 8604 s.op->newline() << "#include \"dyninst/uprobes-regs.c\"";
3a894f7e
JS
8605 s.op->assert_0_indent();
8606}
8607
8608
8609void
8610uprobe_derived_probe_group::emit_module_dyninst_init (systemtap_session& s)
8611{
8612 if (probes.empty()) return;
8613
8614 /* stapdyn handles the dirty work via dyninst */
8615 s.op->newline() << "/* ---- dyninst uprobes ---- */";
8616 s.op->newline() << "/* this section left intentionally blank */";
8617}
8618
8619
8620void
8621uprobe_derived_probe_group::emit_module_dyninst_exit (systemtap_session& s)
8622{
8623 if (probes.empty()) return;
8624
8625 /* stapdyn handles the dirty work via dyninst */
8626 s.op->newline() << "/* ---- dyninst uprobes ---- */";
8627 s.op->newline() << "/* this section left intentionally blank */";
8628}
8629
8630
2b69faaf
JS
8631void
8632uprobe_derived_probe_group::emit_module_decls (systemtap_session& s)
8633{
ac3af990 8634 if (s.runtime_usermode_p())
4441e344
JS
8635 emit_module_dyninst_decls (s);
8636 else if (kernel_supports_inode_uprobes (s))
2b69faaf
JS
8637 emit_module_inode_decls (s);
8638 else
8639 emit_module_utrace_decls (s);
8640}
8641
8642
8643void
8644uprobe_derived_probe_group::emit_module_init (systemtap_session& s)
8645{
ac3af990 8646 if (s.runtime_usermode_p())
4441e344
JS
8647 emit_module_dyninst_init (s);
8648 else if (kernel_supports_inode_uprobes (s))
2b69faaf
JS
8649 emit_module_inode_init (s);
8650 else
8651 emit_module_utrace_init (s);
8652}
8653
8654
8655void
8656uprobe_derived_probe_group::emit_module_exit (systemtap_session& s)
8657{
ac3af990 8658 if (s.runtime_usermode_p())
4441e344
JS
8659 emit_module_dyninst_exit (s);
8660 else if (kernel_supports_inode_uprobes (s))
2b69faaf
JS
8661 emit_module_inode_exit (s);
8662 else
8663 emit_module_utrace_exit (s);
8664}
8665
8666
e6fe60e7
AM
8667// ------------------------------------------------------------------------
8668// Kprobe derived probes
8669// ------------------------------------------------------------------------
8670
4627ed58 8671static const string TOK_KPROBE("kprobe");
935447c8 8672
bae55db9 8673struct kprobe_derived_probe: public derived_probe
d0ea46ce 8674{
23dc94f6
DS
8675 kprobe_derived_probe (systemtap_session& sess,
8676 vector<derived_probe *> & results,
8677 probe *base,
bae55db9
JS
8678 probe_point *location,
8679 const string& name,
8680 int64_t stmt_addr,
8681 bool has_return,
8682 bool has_statement,
8683 bool has_maxactive,
b642c901
SC
8684 bool has_path,
8685 bool has_library,
8686 long maxactive_val,
8687 const string& path,
8688 const string& library
bae55db9
JS
8689 );
8690 string symbol_name;
8691 Dwarf_Addr addr;
8692 bool has_return;
8693 bool has_statement;
8694 bool has_maxactive;
b642c901
SC
8695 bool has_path;
8696 bool has_library;
bae55db9 8697 long maxactive_val;
b642c901
SC
8698 string path;
8699 string library;
bae55db9
JS
8700 bool access_var;
8701 void printsig (std::ostream &o) const;
8702 void join_group (systemtap_session& s);
8703};
d0ea46ce 8704
bae55db9
JS
8705struct kprobe_derived_probe_group: public derived_probe_group
8706{
8707private:
8708 multimap<string,kprobe_derived_probe*> probes_by_module;
8709 typedef multimap<string,kprobe_derived_probe*>::iterator p_b_m_iterator;
d0ea46ce 8710
bae55db9
JS
8711public:
8712 void enroll (kprobe_derived_probe* probe);
8713 void emit_module_decls (systemtap_session& s);
8714 void emit_module_init (systemtap_session& s);
8715 void emit_module_exit (systemtap_session& s);
8716};
d0ea46ce 8717
23dc94f6
DS
8718struct kprobe_var_expanding_visitor: public var_expanding_visitor
8719{
8720 systemtap_session& sess;
8721 block *add_block;
8722 block *add_call_probe; // synthesized from .return probes with saved $vars
8723 bool add_block_tid, add_call_probe_tid;
bd5b25e1 8724 bool has_return;
23dc94f6 8725
bd5b25e1 8726 kprobe_var_expanding_visitor(systemtap_session& sess, bool has_return):
23dc94f6 8727 sess(sess), add_block(NULL), add_call_probe(NULL),
bd5b25e1
JS
8728 add_block_tid(false), add_call_probe_tid(false),
8729 has_return(has_return) {}
23dc94f6
DS
8730
8731 void visit_entry_op (entry_op* e);
8732};
8733
8734
8735kprobe_derived_probe::kprobe_derived_probe (systemtap_session& sess,
8736 vector<derived_probe *> & results,
8737 probe *base,
e6fe60e7 8738 probe_point *location,
b6371390 8739 const string& name,
e6fe60e7 8740 int64_t stmt_addr,
b6371390
JS
8741 bool has_return,
8742 bool has_statement,
8743 bool has_maxactive,
b642c901
SC
8744 bool has_path,
8745 bool has_library,
8746 long maxactive_val,
8747 const string& path,
8748 const string& library
b6371390 8749 ):
4c5d1300 8750 derived_probe (base, location, true /* .components soon rewritten */ ),
e6fe60e7 8751 symbol_name (name), addr (stmt_addr),
b6371390 8752 has_return (has_return), has_statement (has_statement),
b642c901
SC
8753 has_maxactive (has_maxactive), has_path (has_path),
8754 has_library (has_library),
8755 maxactive_val (maxactive_val),
8756 path (path), library (library)
e6fe60e7
AM
8757{
8758 this->tok = base->tok;
8759 this->access_var = false;
d0ea46ce 8760
e6fe60e7
AM
8761#ifndef USHRT_MAX
8762#define USHRT_MAX 32767
8763#endif
d0ea46ce 8764
46856d8d
JS
8765 // Expansion of $target variables in the probe body produces an error during
8766 // translate phase, since we're not using debuginfo
d0ea46ce 8767
e6fe60e7 8768 vector<probe_point::component*> comps;
46856d8d 8769 comps.push_back (new probe_point::component(TOK_KPROBE));
e6fe60e7 8770
46856d8d
JS
8771 if (has_statement)
8772 {
9ea68eb9
JS
8773 comps.push_back (new probe_point::component(TOK_STATEMENT,
8774 new literal_number(addr, true)));
46856d8d
JS
8775 comps.push_back (new probe_point::component(TOK_ABSOLUTE));
8776 }
8777 else
8778 {
8779 size_t pos = name.find(':');
8780 if (pos != string::npos)
d0ea46ce 8781 {
46856d8d
JS
8782 string module = name.substr(0, pos);
8783 string function = name.substr(pos + 1);
8784 comps.push_back (new probe_point::component(TOK_MODULE, new literal_string(module)));
8785 comps.push_back (new probe_point::component(TOK_FUNCTION, new literal_string(function)));
8786 }
8787 else
8788 comps.push_back (new probe_point::component(TOK_FUNCTION, new literal_string(name)));
46856d8d 8789 }
d0ea46ce 8790
b6371390
JS
8791 if (has_return)
8792 comps.push_back (new probe_point::component(TOK_RETURN));
8793 if (has_maxactive)
8794 comps.push_back (new probe_point::component(TOK_MAXACTIVE, new literal_number(maxactive_val)));
d0ea46ce 8795
bd5b25e1 8796 kprobe_var_expanding_visitor v (sess, has_return);
23dc94f6
DS
8797 v.replace (this->body);
8798
8799 // If during target-variable-expanding the probe, we added a new block
8800 // of code, add it to the start of the probe.
8801 if (v.add_block)
8802 this->body = new block(v.add_block, this->body);
8803
8804 // If when target-variable-expanding the probe, we need to
8805 // synthesize a sibling function-entry probe. We don't go through
8806 // the whole probe derivation business (PR10642) that could lead to
8807 // wildcard/alias resolution, or for that dwarf-induced duplication.
8808 if (v.add_call_probe)
8809 {
8810 assert (has_return);
8811
8812 // We temporarily replace base.
8813 statement* old_body = base->body;
8814 base->body = v.add_call_probe;
8815
8816 derived_probe *entry_handler
8817 = new kprobe_derived_probe (sess, results, base, location, name, 0,
8818 false, has_statement, has_maxactive,
8819 has_path, has_library, maxactive_val,
8820 path, library);
8821 results.push_back (entry_handler);
8822
8823 base->body = old_body;
8824 }
8825
e6fe60e7
AM
8826 this->sole_location()->components = comps;
8827}
d0ea46ce 8828
e6fe60e7
AM
8829void kprobe_derived_probe::printsig (ostream& o) const
8830{
8831 sole_location()->print (o);
8832 o << " /* " << " name = " << symbol_name << "*/";
8833 printsig_nested (o);
8834}
d0ea46ce 8835
e6fe60e7
AM
8836void kprobe_derived_probe::join_group (systemtap_session& s)
8837{
d0ea46ce 8838
e6fe60e7
AM
8839 if (! s.kprobe_derived_probes)
8840 s.kprobe_derived_probes = new kprobe_derived_probe_group ();
8841 s.kprobe_derived_probes->enroll (this);
d0ea46ce 8842
e6fe60e7 8843}
d0ea46ce 8844
e6fe60e7
AM
8845void kprobe_derived_probe_group::enroll (kprobe_derived_probe* p)
8846{
8847 probes_by_module.insert (make_pair (p->symbol_name, p));
8848 // probes of same symbol should share single kprobe/kretprobe
8849}
d0ea46ce 8850
e6fe60e7
AM
8851void
8852kprobe_derived_probe_group::emit_module_decls (systemtap_session& s)
8853{
8854 if (probes_by_module.empty()) return;
d0ea46ce 8855
e6fe60e7 8856 s.op->newline() << "/* ---- kprobe-based probes ---- */";
d0ea46ce 8857
e6fe60e7
AM
8858 // Warn of misconfigured kernels
8859 s.op->newline() << "#if ! defined(CONFIG_KPROBES)";
8860 s.op->newline() << "#error \"Need CONFIG_KPROBES!\"";
8861 s.op->newline() << "#endif";
8862 s.op->newline();
d0ea46ce 8863
f07c3b68 8864 s.op->newline() << "#ifndef KRETACTIVE";
1ee6b5fc 8865 s.op->newline() << "#define KRETACTIVE (max(15,6*(int)num_possible_cpus()))";
f07c3b68
FCE
8866 s.op->newline() << "#endif";
8867
e6fe60e7 8868 // Forward declare the master entry functions
88747011 8869 s.op->newline() << "static int enter_kprobe2_probe (struct kprobe *inst,";
e6fe60e7 8870 s.op->line() << " struct pt_regs *regs);";
88747011 8871 s.op->newline() << "static int enter_kretprobe2_probe (struct kretprobe_instance *inst,";
e6fe60e7 8872 s.op->line() << " struct pt_regs *regs);";
d0ea46ce 8873
e6fe60e7
AM
8874 // Emit an array of kprobe/kretprobe pointers
8875 s.op->newline() << "#if defined(STAPCONF_UNREGISTER_KPROBES)";
c9116e99 8876 s.op->newline() << "static void * stap_unreg_kprobes2[" << probes_by_module.size() << "];";
e6fe60e7 8877 s.op->newline() << "#endif";
d0ea46ce 8878
e6fe60e7 8879 // Emit the actual probe list.
d0ea46ce 8880
e6fe60e7
AM
8881 s.op->newline() << "static struct stap_dwarfless_kprobe {";
8882 s.op->newline(1) << "union { struct kprobe kp; struct kretprobe krp; } u;";
8883 s.op->newline() << "#ifdef __ia64__";
8884 s.op->newline() << "struct kprobe dummy;";
8885 s.op->newline() << "#endif";
8886 s.op->newline(-1) << "} stap_dwarfless_kprobes[" << probes_by_module.size() << "];";
8887 // NB: bss!
d0ea46ce 8888
e6fe60e7
AM
8889 s.op->newline() << "static struct stap_dwarfless_probe {";
8890 s.op->newline(1) << "const unsigned return_p:1;";
8891 s.op->newline() << "const unsigned maxactive_p:1;";
b350f56b 8892 s.op->newline() << "const unsigned optional_p:1;";
e6fe60e7
AM
8893 s.op->newline() << "unsigned registered_p:1;";
8894 s.op->newline() << "const unsigned short maxactive_val;";
935447c8 8895
e6fe60e7
AM
8896 // Function Names are mostly small and uniform enough to justify putting
8897 // char[MAX]'s into the array instead of relocated char*'s.
935447c8 8898
faea5e16
JS
8899 size_t symbol_string_name_max = 0;
8900 size_t symbol_string_name_tot = 0;
e6fe60e7 8901 for (p_b_m_iterator it = probes_by_module.begin(); it != probes_by_module.end(); it++)
6270adc1 8902 {
e6fe60e7
AM
8903 kprobe_derived_probe* p = it->second;
8904#define DOIT(var,expr) do { \
8905 size_t var##_size = (expr) + 1; \
8906 var##_max = max (var##_max, var##_size); \
8907 var##_tot += var##_size; } while (0)
e6fe60e7
AM
8908 DOIT(symbol_string_name, p->symbol_name.size());
8909#undef DOIT
6270adc1
MH
8910 }
8911
e6fe60e7
AM
8912#define CALCIT(var) \
8913 s.op->newline() << "const char " << #var << "[" << var##_name_max << "] ;";
935447c8 8914
e6fe60e7
AM
8915 CALCIT(symbol_string);
8916#undef CALCIT
6270adc1 8917
bd659351 8918 s.op->newline() << "unsigned long address;";
7c3e97f4 8919 s.op->newline() << "const struct stap_probe * const probe;";
e6fe60e7
AM
8920 s.op->newline(-1) << "} stap_dwarfless_probes[] = {";
8921 s.op->indent(1);
6270adc1 8922
e6fe60e7
AM
8923 for (p_b_m_iterator it = probes_by_module.begin(); it != probes_by_module.end(); it++)
8924 {
8925 kprobe_derived_probe* p = it->second;
8926 s.op->newline() << "{";
8927 if (p->has_return)
8928 s.op->line() << " .return_p=1,";
6270adc1 8929
e6fe60e7
AM
8930 if (p->has_maxactive)
8931 {
8932 s.op->line() << " .maxactive_p=1,";
8933 assert (p->maxactive_val >= 0 && p->maxactive_val <= USHRT_MAX);
8934 s.op->line() << " .maxactive_val=" << p->maxactive_val << ",";
8935 }
6270adc1 8936
b350f56b
JS
8937 if (p->locations[0]->optional)
8938 s.op->line() << " .optional_p=1,";
8939
e6fe60e7 8940 if (p->has_statement)
c8d9d15e 8941 s.op->line() << " .address=(unsigned long)0x" << hex << p->addr << dec << "ULL,";
e6fe60e7 8942 else
c8d9d15e 8943 s.op->line() << " .symbol_string=\"" << p->symbol_name << "\",";
5d67b47c 8944
faea5e16 8945 s.op->line() << " .probe=" << common_probe_init (p) << ",";
e6fe60e7 8946 s.op->line() << " },";
935447c8
DS
8947 }
8948
e6fe60e7 8949 s.op->newline(-1) << "};";
5d67b47c 8950
e6fe60e7
AM
8951 // Emit the kprobes callback function
8952 s.op->newline();
88747011 8953 s.op->newline() << "static int enter_kprobe2_probe (struct kprobe *inst,";
e6fe60e7
AM
8954 s.op->line() << " struct pt_regs *regs) {";
8955 // NB: as of PR5673, the kprobe|kretprobe union struct is in BSS
8956 s.op->newline(1) << "int kprobe_idx = ((uintptr_t)inst-(uintptr_t)stap_dwarfless_kprobes)/sizeof(struct stap_dwarfless_kprobe);";
8957 // Check that the index is plausible
8958 s.op->newline() << "struct stap_dwarfless_probe *sdp = &stap_dwarfless_probes[";
8959 s.op->line() << "((kprobe_idx >= 0 && kprobe_idx < " << probes_by_module.size() << ")?";
8960 s.op->line() << "kprobe_idx:0)"; // NB: at least we avoid memory corruption
8961 // XXX: it would be nice to give a more verbose error though; BUG_ON later?
8962 s.op->line() << "];";
71db462b 8963 common_probe_entryfn_prologue (s, "STAP_SESSION_RUNNING", "sdp->probe",
cda141c2 8964 "stp_probe_type_kprobe");
d9aed31e 8965 s.op->newline() << "c->kregs = regs;";
6415ddde
MW
8966
8967 // Make it look like the IP is set as it wouldn't have been replaced
8968 // by a breakpoint instruction when calling real probe handler. Reset
8969 // IP regs on return, so we don't confuse kprobes. PR10458
8970 s.op->newline() << "{";
8971 s.op->indent(1);
d9aed31e 8972 s.op->newline() << "unsigned long kprobes_ip = REG_IP(c->kregs);";
259d54c0 8973 s.op->newline() << "SET_REG_IP(regs, (unsigned long) inst->addr);";
26e63673 8974 s.op->newline() << "(*sdp->probe->ph) (c);";
259d54c0 8975 s.op->newline() << "SET_REG_IP(regs, kprobes_ip);";
6415ddde
MW
8976 s.op->newline(-1) << "}";
8977
f887a8c9 8978 common_probe_entryfn_epilogue (s, true);
e6fe60e7
AM
8979 s.op->newline() << "return 0;";
8980 s.op->newline(-1) << "}";
935447c8 8981
e6fe60e7
AM
8982 // Same for kretprobes
8983 s.op->newline();
88747011 8984 s.op->newline() << "static int enter_kretprobe2_probe (struct kretprobe_instance *inst,";
e6fe60e7
AM
8985 s.op->line() << " struct pt_regs *regs) {";
8986 s.op->newline(1) << "struct kretprobe *krp = inst->rp;";
935447c8 8987
e6fe60e7
AM
8988 // NB: as of PR5673, the kprobe|kretprobe union struct is in BSS
8989 s.op->newline() << "int kprobe_idx = ((uintptr_t)krp-(uintptr_t)stap_dwarfless_kprobes)/sizeof(struct stap_dwarfless_kprobe);";
8990 // Check that the index is plausible
8991 s.op->newline() << "struct stap_dwarfless_probe *sdp = &stap_dwarfless_probes[";
8992 s.op->line() << "((kprobe_idx >= 0 && kprobe_idx < " << probes_by_module.size() << ")?";
8993 s.op->line() << "kprobe_idx:0)"; // NB: at least we avoid memory corruption
8994 // XXX: it would be nice to give a more verbose error though; BUG_ON later?
8995 s.op->line() << "];";
935447c8 8996
71db462b 8997 common_probe_entryfn_prologue (s, "STAP_SESSION_RUNNING", "sdp->probe",
cda141c2 8998 "stp_probe_type_kretprobe");
d9aed31e 8999 s.op->newline() << "c->kregs = regs;";
6dceb5c9 9000 s.op->newline() << "c->ips.krp.pi = inst;"; // for assisting runtime's backtrace logic
6415ddde
MW
9001
9002 // Make it look like the IP is set as it wouldn't have been replaced
9003 // by a breakpoint instruction when calling real probe handler. Reset
9004 // IP regs on return, so we don't confuse kprobes. PR10458
9005 s.op->newline() << "{";
9006 s.op->indent(1);
d9aed31e 9007 s.op->newline() << "unsigned long kprobes_ip = REG_IP(c->kregs);";
259d54c0 9008 s.op->newline() << "SET_REG_IP(regs, (unsigned long) inst->rp->kp.addr);";
26e63673 9009 s.op->newline() << "(*sdp->probe->ph) (c);";
259d54c0 9010 s.op->newline() << "SET_REG_IP(regs, kprobes_ip);";
6415ddde
MW
9011 s.op->newline(-1) << "}";
9012
f887a8c9 9013 common_probe_entryfn_epilogue (s, true);
e6fe60e7
AM
9014 s.op->newline() << "return 0;";
9015 s.op->newline(-1) << "}";
bd659351 9016
03a4ec63 9017 s.op->newline() << "#ifdef STAPCONF_KALLSYMS_ON_EACH_SYMBOL";
bd659351
MW
9018 s.op->newline() << "static int kprobe_resolve(void *data, const char *name,";
9019 s.op->newline() << " struct module *owner,";
9020 s.op->newline() << " unsigned long val) {";
9021 s.op->newline(1) << "int i;";
fc1d2aa2
MW
9022 s.op->newline() << "int *p = (int *) data;";
9023 s.op->newline() << "for (i=0; i<" << probes_by_module.size()
9024 << " && *p > 0; i++) {";
bd659351 9025 s.op->newline(1) << "struct stap_dwarfless_probe *sdp = & stap_dwarfless_probes[i];";
8c272819
YW
9026 s.op->newline() << "if (! sdp->address) {";
9027 s.op->indent(1);
9028 s.op->newline() << "const char *colon;";
9029 s.op->newline() << "if (owner && (colon = strchr(sdp->symbol_string, ':'))) {";
9030 s.op->indent(1);
9031 s.op->newline() << "if ((strlen(owner->name) == (colon - sdp->symbol_string))";
9032 s.op->newline() << " && (strncmp(sdp->symbol_string, owner->name, colon - sdp->symbol_string) == 0)";
9033 s.op->newline() << " && (strcmp(colon + 1, name) == 0)) {";
9034 s.op->newline(1) << "sdp->address = val;";
9035 s.op->newline() << "(*p)--;";
9036 s.op->newline(-1) << "}";
9037 s.op->newline(-1) << "}";
9038 s.op->newline() << "else {";
fc1d2aa2 9039 s.op->newline(1) << "if (strcmp(sdp->symbol_string, name) == 0) {";
bd659351 9040 s.op->newline(1) << "sdp->address = val;";
fc1d2aa2
MW
9041 s.op->newline() << "(*p)--;";
9042 s.op->newline(-1) << "}";
8c272819
YW
9043 s.op->newline(-1) << "}";
9044 s.op->newline(-1) << "}";
9045 s.op->newline(-1) << "}";
fc1d2aa2 9046 s.op->newline() << "return (p > 0) ? 0 : -1;";
bd659351 9047 s.op->newline(-1) << "}";
03a4ec63 9048 s.op->newline() << "#endif";
935447c8
DS
9049}
9050
e6fe60e7 9051
6270adc1 9052void
e6fe60e7 9053kprobe_derived_probe_group::emit_module_init (systemtap_session& s)
6270adc1 9054{
03a4ec63 9055 s.op->newline() << "#ifdef STAPCONF_KALLSYMS_ON_EACH_SYMBOL";
fc1d2aa2
MW
9056 s.op->newline() << "{";
9057 s.op->newline(1) << "int p = 0;";
9058 s.op->newline() << "for (i = 0; i < " << probes_by_module.size() << "; i++) {";
9059 s.op->newline(1) << "struct stap_dwarfless_probe *sdp = & stap_dwarfless_probes[i];";
9060 s.op->newline() << "if (! sdp->address)";
9061 s.op->newline(1) << "p++;";
9062 s.op->newline(-2) << "}";
9063 s.op->newline() << "kallsyms_on_each_symbol(kprobe_resolve, &p);";
9064 s.op->newline(-1) << "}";
03a4ec63 9065 s.op->newline() << "#endif";
bd659351 9066
e6fe60e7 9067 s.op->newline() << "for (i=0; i<" << probes_by_module.size() << "; i++) {";
c8d9d15e 9068 s.op->newline(1) << "struct stap_dwarfless_probe *sdp = & stap_dwarfless_probes[i];";
e6fe60e7 9069 s.op->newline() << "struct stap_dwarfless_kprobe *kp = & stap_dwarfless_kprobes[i];";
c8d9d15e 9070 s.op->newline() << "void *addr = (void *) sdp->address;";
03a4ec63
MW
9071 s.op->newline() << "const char *symbol_name = addr ? NULL : sdp->symbol_string;";
9072
9073 s.op->newline() << "#ifdef STAPCONF_KALLSYMS_ON_EACH_SYMBOL";
bd659351
MW
9074 s.op->newline() << "if (! addr) {";
9075 s.op->newline(1) << "sdp->registered_p = 0;";
9319b767
MW
9076 s.op->newline() << "if (!sdp->optional_p)";
9077 s.op->newline(1) << "_stp_warn (\"probe %s registration error (symbol not found)\", probe_point);";
9078 s.op->newline(-1) << "continue;";
bd659351 9079 s.op->newline(-1) << "}";
03a4ec63
MW
9080 s.op->newline() << "#endif";
9081
26e63673 9082 s.op->newline() << "probe_point = sdp->probe->pp;"; // for error messages
e6fe60e7 9083 s.op->newline() << "if (sdp->return_p) {";
c8d9d15e 9084 s.op->newline(1) << "kp->u.krp.kp.addr = addr;";
766cee5f 9085 s.op->newline() << "#ifdef STAPCONF_KPROBE_SYMBOL_NAME";
03a4ec63 9086 s.op->newline() << "kp->u.krp.kp.symbol_name = (char *) symbol_name;";
766cee5f 9087 s.op->newline() << "#endif";
e6fe60e7
AM
9088 s.op->newline() << "if (sdp->maxactive_p) {";
9089 s.op->newline(1) << "kp->u.krp.maxactive = sdp->maxactive_val;";
9090 s.op->newline(-1) << "} else {";
f07c3b68 9091 s.op->newline(1) << "kp->u.krp.maxactive = KRETACTIVE;";
e6fe60e7 9092 s.op->newline(-1) << "}";
88747011 9093 s.op->newline() << "kp->u.krp.handler = &enter_kretprobe2_probe;";
e6fe60e7
AM
9094 // to ensure safeness of bspcache, always use aggr_kprobe on ia64
9095 s.op->newline() << "#ifdef __ia64__";
e6fe60e7 9096 s.op->newline() << "kp->dummy.addr = kp->u.krp.kp.addr;";
766cee5f 9097 s.op->newline() << "#ifdef STAPCONF_KPROBE_SYMBOL_NAME";
c8d9d15e 9098 s.op->newline() << "kp->dummy.symbol_name = kp->u.krp.kp.symbol_name;";
766cee5f 9099 s.op->newline() << "#endif";
c8d9d15e 9100 s.op->newline() << "kp->dummy.pre_handler = NULL;";
e6fe60e7
AM
9101 s.op->newline() << "rc = register_kprobe (& kp->dummy);";
9102 s.op->newline() << "if (rc == 0) {";
9103 s.op->newline(1) << "rc = register_kretprobe (& kp->u.krp);";
9104 s.op->newline() << "if (rc != 0)";
9105 s.op->newline(1) << "unregister_kprobe (& kp->dummy);";
9106 s.op->newline(-2) << "}";
9107 s.op->newline() << "#else";
9108 s.op->newline() << "rc = register_kretprobe (& kp->u.krp);";
9109 s.op->newline() << "#endif";
9110 s.op->newline(-1) << "} else {";
9111 // to ensure safeness of bspcache, always use aggr_kprobe on ia64
c8d9d15e 9112 s.op->newline(1) << "kp->u.kp.addr = addr;";
766cee5f 9113 s.op->newline() << "#ifdef STAPCONF_KPROBE_SYMBOL_NAME";
03a4ec63 9114 s.op->newline() << "kp->u.kp.symbol_name = (char *) symbol_name;";
766cee5f 9115 s.op->newline() << "#endif";
88747011 9116 s.op->newline() << "kp->u.kp.pre_handler = &enter_kprobe2_probe;";
e6fe60e7 9117 s.op->newline() << "#ifdef __ia64__";
e6fe60e7 9118 s.op->newline() << "kp->dummy.pre_handler = NULL;";
c8d9d15e 9119 s.op->newline() << "kp->dummy.addr = kp->u.kp.addr;";
766cee5f 9120 s.op->newline() << "#ifdef STAPCONF_KPROBE_SYMBOL_NAME";
c8d9d15e 9121 s.op->newline() << "kp->dummy.symbol_name = kp->u.kp.symbol_name;";
766cee5f 9122 s.op->newline() << "#endif";
e6fe60e7
AM
9123 s.op->newline() << "rc = register_kprobe (& kp->dummy);";
9124 s.op->newline() << "if (rc == 0) {";
9125 s.op->newline(1) << "rc = register_kprobe (& kp->u.kp);";
9126 s.op->newline() << "if (rc != 0)";
9127 s.op->newline(1) << "unregister_kprobe (& kp->dummy);";
9128 s.op->newline(-2) << "}";
9129 s.op->newline() << "#else";
9130 s.op->newline() << "rc = register_kprobe (& kp->u.kp);";
9131 s.op->newline() << "#endif";
9132 s.op->newline(-1) << "}";
9133 s.op->newline() << "if (rc) {"; // PR6749: tolerate a failed register_*probe.
9134 s.op->newline(1) << "sdp->registered_p = 0;";
b350f56b 9135 s.op->newline() << "if (!sdp->optional_p)";
50b6acf7 9136 s.op->newline(1) << "_stp_warn (\"probe %s (address 0x%lx) registration error (rc %d)\", probe_point, (unsigned long) addr, rc);";
b350f56b 9137 s.op->newline(-1) << "rc = 0;"; // continue with other probes
e6fe60e7
AM
9138 // XXX: shall we increment numskipped?
9139 s.op->newline(-1) << "}";
6270adc1 9140
e6fe60e7
AM
9141 s.op->newline() << "else sdp->registered_p = 1;";
9142 s.op->newline(-1) << "}"; // for loop
6270adc1
MH
9143}
9144
b4be7cbc 9145
e6fe60e7
AM
9146void
9147kprobe_derived_probe_group::emit_module_exit (systemtap_session& s)
935447c8 9148{
e6fe60e7
AM
9149 //Unregister kprobes by batch interfaces.
9150 s.op->newline() << "#if defined(STAPCONF_UNREGISTER_KPROBES)";
9151 s.op->newline() << "j = 0;";
9152 s.op->newline() << "for (i=0; i<" << probes_by_module.size() << "; i++) {";
9153 s.op->newline(1) << "struct stap_dwarfless_probe *sdp = & stap_dwarfless_probes[i];";
9154 s.op->newline() << "struct stap_dwarfless_kprobe *kp = & stap_dwarfless_kprobes[i];";
9155 s.op->newline() << "if (! sdp->registered_p) continue;";
9156 s.op->newline() << "if (!sdp->return_p)";
c9116e99 9157 s.op->newline(1) << "stap_unreg_kprobes2[j++] = &kp->u.kp;";
e6fe60e7 9158 s.op->newline(-2) << "}";
c9116e99 9159 s.op->newline() << "unregister_kprobes((struct kprobe **)stap_unreg_kprobes2, j);";
e6fe60e7
AM
9160 s.op->newline() << "j = 0;";
9161 s.op->newline() << "for (i=0; i<" << probes_by_module.size() << "; i++) {";
9162 s.op->newline(1) << "struct stap_dwarfless_probe *sdp = & stap_dwarfless_probes[i];";
9163 s.op->newline() << "struct stap_dwarfless_kprobe *kp = & stap_dwarfless_kprobes[i];";
9164 s.op->newline() << "if (! sdp->registered_p) continue;";
9165 s.op->newline() << "if (sdp->return_p)";
c9116e99 9166 s.op->newline(1) << "stap_unreg_kprobes2[j++] = &kp->u.krp;";
e6fe60e7 9167 s.op->newline(-2) << "}";
c9116e99 9168 s.op->newline() << "unregister_kretprobes((struct kretprobe **)stap_unreg_kprobes2, j);";
e6fe60e7
AM
9169 s.op->newline() << "#ifdef __ia64__";
9170 s.op->newline() << "j = 0;";
9171 s.op->newline() << "for (i=0; i<" << probes_by_module.size() << "; i++) {";
9172 s.op->newline(1) << "struct stap_dwarfless_probe *sdp = & stap_dwarfless_probes[i];";
9173 s.op->newline() << "struct stap_dwarfless_kprobe *kp = & stap_dwarfless_kprobes[i];";
9174 s.op->newline() << "if (! sdp->registered_p) continue;";
c9116e99 9175 s.op->newline() << "stap_unreg_kprobes2[j++] = &kp->dummy;";
e6fe60e7 9176 s.op->newline(-1) << "}";
c9116e99 9177 s.op->newline() << "unregister_kprobes((struct kprobe **)stap_unreg_kprobes2, j);";
e6fe60e7
AM
9178 s.op->newline() << "#endif";
9179 s.op->newline() << "#endif";
3e3bd7b6 9180
e6fe60e7
AM
9181 s.op->newline() << "for (i=0; i<" << probes_by_module.size() << "; i++) {";
9182 s.op->newline(1) << "struct stap_dwarfless_probe *sdp = & stap_dwarfless_probes[i];";
9183 s.op->newline() << "struct stap_dwarfless_kprobe *kp = & stap_dwarfless_kprobes[i];";
9184 s.op->newline() << "if (! sdp->registered_p) continue;";
9185 s.op->newline() << "if (sdp->return_p) {";
9186 s.op->newline() << "#if !defined(STAPCONF_UNREGISTER_KPROBES)";
9187 s.op->newline(1) << "unregister_kretprobe (&kp->u.krp);";
9188 s.op->newline() << "#endif";
065d5567 9189 s.op->newline() << "atomic_add (kp->u.krp.nmissed, skipped_count());";
e6fe60e7
AM
9190 s.op->newline() << "#ifdef STP_TIMING";
9191 s.op->newline() << "if (kp->u.krp.nmissed)";
26e63673 9192 s.op->newline(1) << "_stp_warn (\"Skipped due to missed kretprobe/1 on '%s': %d\\n\", sdp->probe->pp, kp->u.krp.nmissed);";
e6fe60e7 9193 s.op->newline(-1) << "#endif";
065d5567 9194 s.op->newline() << "atomic_add (kp->u.krp.kp.nmissed, skipped_count());";
e6fe60e7
AM
9195 s.op->newline() << "#ifdef STP_TIMING";
9196 s.op->newline() << "if (kp->u.krp.kp.nmissed)";
26e63673 9197 s.op->newline(1) << "_stp_warn (\"Skipped due to missed kretprobe/2 on '%s': %lu\\n\", sdp->probe->pp, kp->u.krp.kp.nmissed);";
e6fe60e7
AM
9198 s.op->newline(-1) << "#endif";
9199 s.op->newline(-1) << "} else {";
9200 s.op->newline() << "#if !defined(STAPCONF_UNREGISTER_KPROBES)";
9201 s.op->newline(1) << "unregister_kprobe (&kp->u.kp);";
9202 s.op->newline() << "#endif";
065d5567 9203 s.op->newline() << "atomic_add (kp->u.kp.nmissed, skipped_count());";
e6fe60e7
AM
9204 s.op->newline() << "#ifdef STP_TIMING";
9205 s.op->newline() << "if (kp->u.kp.nmissed)";
26e63673 9206 s.op->newline(1) << "_stp_warn (\"Skipped due to missed kprobe on '%s': %lu\\n\", sdp->probe->pp, kp->u.kp.nmissed);";
e6fe60e7
AM
9207 s.op->newline(-1) << "#endif";
9208 s.op->newline(-1) << "}";
9209 s.op->newline() << "#if !defined(STAPCONF_UNREGISTER_KPROBES) && defined(__ia64__)";
9210 s.op->newline() << "unregister_kprobe (&kp->dummy);";
9211 s.op->newline() << "#endif";
9212 s.op->newline() << "sdp->registered_p = 0;";
9213 s.op->newline(-1) << "}";
f8a968bc
JS
9214}
9215
e6fe60e7 9216struct kprobe_builder: public derived_probe_builder
3c1b3d06 9217{
9fdf787d 9218public:
2a639817 9219 kprobe_builder() {}
9fdf787d 9220
2a639817 9221 void build_no_more (systemtap_session &s) {}
9fdf787d 9222
e6fe60e7
AM
9223 virtual void build(systemtap_session & sess,
9224 probe * base,
9225 probe_point * location,
9226 literal_map_t const & parameters,
9227 vector<derived_probe *> & finished_results);
9228};
3c1b3d06
FCE
9229
9230
79189b84 9231void
05fb3e0c 9232kprobe_builder::build(systemtap_session & sess,
e6fe60e7
AM
9233 probe * base,
9234 probe_point * location,
9235 literal_map_t const & parameters,
9236 vector<derived_probe *> & finished_results)
79189b84 9237{
e6fe60e7 9238 string function_string_val, module_string_val;
05fb3e0c 9239 string path, library, path_tgt, library_tgt;
b6371390
JS
9240 int64_t statement_num_val = 0, maxactive_val = 0;
9241 bool has_function_str, has_module_str, has_statement_num;
9242 bool has_absolute, has_return, has_maxactive;
b642c901 9243 bool has_path, has_library;
79189b84 9244
b6371390
JS
9245 has_function_str = get_param(parameters, TOK_FUNCTION, function_string_val);
9246 has_module_str = get_param(parameters, TOK_MODULE, module_string_val);
9247 has_return = has_null_param (parameters, TOK_RETURN);
9248 has_maxactive = get_param(parameters, TOK_MAXACTIVE, maxactive_val);
9249 has_statement_num = get_param(parameters, TOK_STATEMENT, statement_num_val);
9250 has_absolute = has_null_param (parameters, TOK_ABSOLUTE);
b642c901
SC
9251 has_path = get_param (parameters, TOK_PROCESS, path);
9252 has_library = get_param (parameters, TOK_LIBRARY, library);
9253
9254 if (has_path)
05fb3e0c
WF
9255 {
9256 path = find_executable (path, sess.sysroot, sess.sysenv);
9257 path_tgt = path_remove_sysroot(sess, path);
9258 }
b642c901 9259 if (has_library)
05fb3e0c
WF
9260 {
9261 library = find_executable (library, sess.sysroot, sess.sysenv,
9262 "LD_LIBRARY_PATH");
9263 library_tgt = path_remove_sysroot(sess, library);
9264 }
c57ea854 9265
b6371390 9266 if (has_function_str)
6fb70fb7 9267 {
2a639817 9268 if (has_module_str)
9fdf787d
DS
9269 {
9270 function_string_val = module_string_val + ":" + function_string_val;
9271 derived_probe *dp
23dc94f6
DS
9272 = new kprobe_derived_probe (sess, finished_results, base,
9273 location, function_string_val,
9fdf787d
DS
9274 0, has_return, has_statement_num,
9275 has_maxactive, has_path, has_library,
9276 maxactive_val, path_tgt, library_tgt);
9277 finished_results.push_back (dp);
9278 }
9279 else
9280 {
2a639817
JS
9281 vector<string> matches;
9282
9283 // Simple names can be found directly
9284 if (function_string_val.find_first_of("*?[") == string::npos)
9285 {
9286 if (sess.kernel_functions.count(function_string_val))
9287 matches.push_back(function_string_val);
9288 }
9289 else // Search function name list for matching names
9290 {
9291 for (set<string>::const_iterator it = sess.kernel_functions.begin();
9292 it != sess.kernel_functions.end(); it++)
9293 // fnmatch returns zero for matching.
9294 if (fnmatch(function_string_val.c_str(), it->c_str(), 0) == 0)
9295 matches.push_back(*it);
9296 }
86758d5f 9297
2a639817
JS
9298 for (vector<string>::const_iterator it = matches.begin();
9299 it != matches.end(); it++)
9fdf787d 9300 {
2a639817
JS
9301 derived_probe *dp
9302 = new kprobe_derived_probe (sess, finished_results, base,
9303 location, *it, 0, has_return,
9304 has_statement_num,
9305 has_maxactive, has_path,
9306 has_library, maxactive_val,
9307 path_tgt, library_tgt);
9308 finished_results.push_back (dp);
9fdf787d
DS
9309 }
9310 }
6fb70fb7 9311 }
e6fe60e7 9312 else
b6371390
JS
9313 {
9314 // assert guru mode for absolute probes
9315 if ( has_statement_num && has_absolute && !base->privileged )
dc09353a 9316 throw SEMANTIC_ERROR (_("absolute statement probe in unprivileged script; need stap -g"), base->tok);
b6371390 9317
23dc94f6
DS
9318 finished_results.push_back (new kprobe_derived_probe (sess,
9319 finished_results,
9320 base,
b6371390
JS
9321 location, "",
9322 statement_num_val,
9323 has_return,
9324 has_statement_num,
9325 has_maxactive,
b642c901
SC
9326 has_path,
9327 has_library,
9328 maxactive_val,
05fb3e0c
WF
9329 path_tgt,
9330 library_tgt));
96b030fe 9331 }
79189b84
JS
9332}
9333
23dc94f6
DS
9334
9335void
9336kprobe_var_expanding_visitor::visit_entry_op (entry_op *e)
9337{
9338 expression *repl = e;
9339
bd5b25e1
JS
9340 if (has_return)
9341 {
9342 // expand the operand as if it weren't a return probe
9343 has_return = false;
9344 replace (e->operand);
9345 has_return = true;
23dc94f6 9346
bd5b25e1
JS
9347 // XXX it would be nice to use gen_kretprobe_saved_return when
9348 // available, but it requires knowing the types already, which is
9349 // problematic for arbitrary expressons.
9350 repl = gen_mapped_saved_return (sess, e->operand, "entry",
9351 add_block, add_block_tid,
9352 add_call_probe, add_call_probe_tid);
9353 }
23dc94f6
DS
9354 provide (repl);
9355}
9356
9357
dd225250
PS
9358// ------------------------------------------------------------------------
9359// Hardware breakpoint based probes.
9360// ------------------------------------------------------------------------
9361
9362static const string TOK_HWBKPT("data");
9363static const string TOK_HWBKPT_WRITE("write");
9364static const string TOK_HWBKPT_RW("rw");
9365static const string TOK_LENGTH("length");
9366
9367#define HWBKPT_READ 0
9368#define HWBKPT_WRITE 1
9369#define HWBKPT_RW 2
9370struct hwbkpt_derived_probe: public derived_probe
9371{
9372 hwbkpt_derived_probe (probe *base,
9373 probe_point *location,
9374 uint64_t addr,
9375 string symname,
9376 unsigned int len,
9377 bool has_only_read_access,
9378 bool has_only_write_access,
9379 bool has_rw_access
9380 );
9381 Dwarf_Addr hwbkpt_addr;
9382 string symbol_name;
9383 unsigned int hwbkpt_access,hwbkpt_len;
9384
9385 void printsig (std::ostream &o) const;
9386 void join_group (systemtap_session& s);
9387};
9388
9389struct hwbkpt_derived_probe_group: public derived_probe_group
9390{
dd225250 9391private:
dac77b80 9392 vector<hwbkpt_derived_probe*> hwbkpt_probes;
dd225250
PS
9393
9394public:
9395 void enroll (hwbkpt_derived_probe* probe, systemtap_session& s);
9396 void emit_module_decls (systemtap_session& s);
9397 void emit_module_init (systemtap_session& s);
9398 void emit_module_exit (systemtap_session& s);
9399};
9400
9401hwbkpt_derived_probe::hwbkpt_derived_probe (probe *base,
9ea68eb9
JS
9402 probe_point *location,
9403 uint64_t addr,
9404 string symname,
9405 unsigned int len,
9406 bool has_only_read_access,
9407 bool has_only_write_access,
822a6a3d 9408 bool):
4c5d1300 9409 derived_probe (base, location, true /* .components soon rewritten */ ),
dd225250
PS
9410 hwbkpt_addr (addr),
9411 symbol_name (symname),
9412 hwbkpt_len (len)
9413{
9414 this->tok = base->tok;
9415
9416 vector<probe_point::component*> comps;
9417 comps.push_back (new probe_point::component(TOK_KERNEL));
9418
9419 if (hwbkpt_addr)
9ea68eb9
JS
9420 comps.push_back (new probe_point::component (TOK_HWBKPT,
9421 new literal_number(hwbkpt_addr, true)));
9422 else if (symbol_name.size())
9423 comps.push_back (new probe_point::component (TOK_HWBKPT, new literal_string(symbol_name)));
dd225250
PS
9424
9425 comps.push_back (new probe_point::component (TOK_LENGTH, new literal_number(hwbkpt_len)));
9426
9427 if (has_only_read_access)
9ea68eb9 9428 this->hwbkpt_access = HWBKPT_READ ;
dd225250
PS
9429//TODO add code for comps.push_back for read, since this flag is not for x86
9430
9431 else
9ea68eb9
JS
9432 {
9433 if (has_only_write_access)
9434 {
9435 this->hwbkpt_access = HWBKPT_WRITE ;
9436 comps.push_back (new probe_point::component(TOK_HWBKPT_WRITE));
9437 }
9438 else
9439 {
9440 this->hwbkpt_access = HWBKPT_RW ;
9441 comps.push_back (new probe_point::component(TOK_HWBKPT_RW));
9442 }
9443 }
dd225250
PS
9444
9445 this->sole_location()->components = comps;
9446}
9447
9448void hwbkpt_derived_probe::printsig (ostream& o) const
9449{
9450 sole_location()->print (o);
9451 printsig_nested (o);
9452}
9453
9454void hwbkpt_derived_probe::join_group (systemtap_session& s)
9455{
dac77b80
FCE
9456 if (! s.hwbkpt_derived_probes)
9457 s.hwbkpt_derived_probes = new hwbkpt_derived_probe_group ();
dd225250
PS
9458 s.hwbkpt_derived_probes->enroll (this, s);
9459}
9460
9461void hwbkpt_derived_probe_group::enroll (hwbkpt_derived_probe* p, systemtap_session& s)
9462{
dac77b80
FCE
9463 hwbkpt_probes.push_back (p);
9464
9465 unsigned max_hwbkpt_probes_by_arch = 0;
9466 if (s.architecture == "i386" || s.architecture == "x86_64")
9467 max_hwbkpt_probes_by_arch = 4;
9468 else if (s.architecture == "s390")
9469 max_hwbkpt_probes_by_arch = 1;
9470
c57ea854 9471 if (hwbkpt_probes.size() >= max_hwbkpt_probes_by_arch)
2713ea24 9472 s.print_warning (_F("Too many hardware breakpoint probes requested for %s (%zu vs. %u)",
b530b5b3 9473 s.architecture.c_str(), hwbkpt_probes.size(), max_hwbkpt_probes_by_arch));
dd225250
PS
9474}
9475
9476void
9477hwbkpt_derived_probe_group::emit_module_decls (systemtap_session& s)
9478{
dac77b80 9479 if (hwbkpt_probes.empty()) return;
dd225250
PS
9480
9481 s.op->newline() << "/* ---- hwbkpt-based probes ---- */";
9482
9483 s.op->newline() << "#include <linux/perf_event.h>";
9484 s.op->newline() << "#include <linux/hw_breakpoint.h>";
9485 s.op->newline();
9486
9487 // Forward declare the master entry functions
23063de1 9488 s.op->newline() << "#ifdef STAPCONF_PERF_HANDLER_NMI";
dd225250
PS
9489 s.op->newline() << "static int enter_hwbkpt_probe (struct perf_event *bp,";
9490 s.op->line() << " int nmi,";
9491 s.op->line() << " struct perf_sample_data *data,";
9492 s.op->line() << " struct pt_regs *regs);";
23063de1
FCE
9493 s.op->newline() << "#else";
9494 s.op->newline() << "static int enter_hwbkpt_probe (struct perf_event *bp,";
9495 s.op->line() << " struct perf_sample_data *data,";
9496 s.op->line() << " struct pt_regs *regs);";
9497 s.op->newline() << "#endif";
79189b84 9498
dd225250
PS
9499 // Emit the actual probe list.
9500
9501 s.op->newline() << "static struct perf_event_attr ";
dac77b80 9502 s.op->newline() << "stap_hwbkpt_probe_array[" << hwbkpt_probes.size() << "];";
dd225250
PS
9503
9504 s.op->newline() << "static struct perf_event **";
dac77b80 9505 s.op->newline() << "stap_hwbkpt_ret_array[" << hwbkpt_probes.size() << "];";
dd225250
PS
9506 s.op->newline() << "static struct stap_hwbkpt_probe {";
9507 s.op->newline() << "int registered_p:1;";
43650b10 9508// registered_p = 0 signifies a probe that is unregistered (or failed)
dd225250
PS
9509// registered_p = 1 signifies a probe that got registered successfully
9510
faea5e16 9511 // Symbol Names are mostly small and uniform enough
dd225250 9512 // to justify putting const char*.
dac77b80 9513 s.op->newline() << "const char * const symbol;";
dd225250
PS
9514
9515 s.op->newline() << "const unsigned long address;";
9516 s.op->newline() << "uint8_t atype;";
bb0a4e12 9517 s.op->newline() << "unsigned int len;";
7c3e97f4 9518 s.op->newline() << "const struct stap_probe * const probe;";
dd225250
PS
9519 s.op->newline() << "} stap_hwbkpt_probes[] = {";
9520 s.op->indent(1);
9521
dac77b80 9522 for (unsigned int it = 0; it < hwbkpt_probes.size(); it++)
dd225250 9523 {
dac77b80 9524 hwbkpt_derived_probe* p = hwbkpt_probes.at(it);
dd225250 9525 s.op->newline() << "{";
dd225250
PS
9526 if (p->symbol_name.size())
9527 s.op->line() << " .address=(unsigned long)0x0" << "ULL,";
9528 else
9529 s.op->line() << " .address=(unsigned long)0x" << hex << p->hwbkpt_addr << dec << "ULL,";
9530 switch(p->hwbkpt_access){
9531 case HWBKPT_READ:
9532 s.op->line() << " .atype=HW_BREAKPOINT_R ,";
bb0a4e12 9533 break;
dd225250
PS
9534 case HWBKPT_WRITE:
9535 s.op->line() << " .atype=HW_BREAKPOINT_W ,";
bb0a4e12 9536 break;
dd225250
PS
9537 case HWBKPT_RW:
9538 s.op->line() << " .atype=HW_BREAKPOINT_R|HW_BREAKPOINT_W ,";
bb0a4e12 9539 break;
dd225250
PS
9540 };
9541 s.op->line() << " .len=" << p->hwbkpt_len << ",";
faea5e16 9542 s.op->line() << " .probe=" << common_probe_init (p) << ",";
dd225250 9543 s.op->line() << " .symbol=\"" << p->symbol_name << "\",";
dd225250
PS
9544 s.op->line() << " },";
9545 }
dac77b80 9546 s.op->newline(-1) << "};";
dd225250
PS
9547
9548 // Emit the hwbkpt callback function
9549 s.op->newline() ;
23063de1 9550 s.op->newline() << "#ifdef STAPCONF_PERF_HANDLER_NMI";
dd225250
PS
9551 s.op->newline() << "static int enter_hwbkpt_probe (struct perf_event *bp,";
9552 s.op->line() << " int nmi,";
9553 s.op->line() << " struct perf_sample_data *data,";
9554 s.op->line() << " struct pt_regs *regs) {";
23063de1
FCE
9555 s.op->newline() << "#else";
9556 s.op->newline() << "static int enter_hwbkpt_probe (struct perf_event *bp,";
9557 s.op->line() << " struct perf_sample_data *data,";
9558 s.op->line() << " struct pt_regs *regs) {";
9559 s.op->newline() << "#endif";
dac77b80
FCE
9560 s.op->newline(1) << "unsigned int i;";
9561 s.op->newline() << "if (bp->attr.type != PERF_TYPE_BREAKPOINT) return -1;";
9562 s.op->newline() << "for (i=0; i<" << hwbkpt_probes.size() << "; i++) {";
9563 s.op->newline(1) << "struct perf_event_attr *hp = & stap_hwbkpt_probe_array[i];";
9564 // XXX: why not match stap_hwbkpt_ret_array[i] against bp instead?
9565 s.op->newline() << "if (bp->attr.bp_addr==hp->bp_addr && bp->attr.bp_type==hp->bp_type && bp->attr.bp_len==hp->bp_len) {";
9566 s.op->newline(1) << "struct stap_hwbkpt_probe *sdp = &stap_hwbkpt_probes[i];";
71db462b 9567 common_probe_entryfn_prologue (s, "STAP_SESSION_RUNNING", "sdp->probe",
cda141c2 9568 "stp_probe_type_hwbkpt");
d9aed31e 9569 s.op->newline() << "if (user_mode(regs)) {";
e04b5d74 9570 s.op->newline(1)<< "c->user_mode_p = 1;";
d9aed31e
MW
9571 s.op->newline() << "c->uregs = regs;";
9572 s.op->newline(-1) << "} else {";
9573 s.op->newline(1) << "c->kregs = regs;";
9574 s.op->newline(-1) << "}";
26e63673 9575 s.op->newline() << "(*sdp->probe->ph) (c);";
f887a8c9 9576 common_probe_entryfn_epilogue (s, true);
dac77b80 9577 s.op->newline(-1) << "}";
dd225250
PS
9578 s.op->newline(-1) << "}";
9579 s.op->newline() << "return 0;";
dac77b80 9580 s.op->newline(-1) << "}";
dd225250
PS
9581}
9582
9583void
9584hwbkpt_derived_probe_group::emit_module_init (systemtap_session& s)
9585{
dac77b80 9586 s.op->newline() << "for (i=0; i<" << hwbkpt_probes.size() << "; i++) {";
dd225250
PS
9587 s.op->newline(1) << "struct stap_hwbkpt_probe *sdp = & stap_hwbkpt_probes[i];";
9588 s.op->newline() << "struct perf_event_attr *hp = & stap_hwbkpt_probe_array[i];";
9589 s.op->newline() << "void *addr = (void *) sdp->address;";
9590 s.op->newline() << "const char *hwbkpt_symbol_name = addr ? NULL : sdp->symbol;";
dac77b80
FCE
9591 s.op->newline() << "hw_breakpoint_init(hp);";
9592 s.op->newline() << "if (addr)";
9593 s.op->newline(1) << "hp->bp_addr = (unsigned long) addr;";
9594 s.op->newline(-1) << "else { ";
9595 s.op->newline(1) << "hp->bp_addr = kallsyms_lookup_name(hwbkpt_symbol_name);";
9596 s.op->newline() << "if (!hp->bp_addr) { ";
26e63673 9597 s.op->newline(1) << "_stp_warn(\"Probe %s registration skipped: invalid symbol %s \",sdp->probe->pp,hwbkpt_symbol_name);";
dac77b80
FCE
9598 s.op->newline() << "continue;";
9599 s.op->newline(-1) << "}";
9600 s.op->newline(-1) << "}";
9601 s.op->newline() << "hp->bp_type = sdp->atype;";
9602
9603 // On x86 & x86-64, hp->bp_len is not just a number but a macro/enum (!?!).
c57ea854 9604 if (s.architecture == "i386" || s.architecture == "x86_64" )
dac77b80
FCE
9605 {
9606 s.op->newline() << "switch(sdp->len) {";
9607 s.op->newline() << "case 1:";
9608 s.op->newline(1) << "hp->bp_len = HW_BREAKPOINT_LEN_1;";
9609 s.op->newline() << "break;";
9610 s.op->newline(-1) << "case 2:";
9611 s.op->newline(1) << "hp->bp_len = HW_BREAKPOINT_LEN_2;";
9612 s.op->newline() << "break;";
9613 s.op->newline(-1) << "case 3:";
9614 s.op->newline() << "case 4:";
9615 s.op->newline(1) << "hp->bp_len = HW_BREAKPOINT_LEN_4;";
9616 s.op->newline() << "break;";
9617 s.op->newline(-1) << "case 5:";
9618 s.op->newline() << "case 6:";
9619 s.op->newline() << "case 7:";
9620 s.op->newline() << "case 8:";
9621 s.op->newline() << "default:"; // XXX: could instead reject
9622 s.op->newline(1) << "hp->bp_len = HW_BREAKPOINT_LEN_8;";
9623 s.op->newline() << "break;";
9624 s.op->newline(-1) << "}";
9625 }
9626 else // other architectures presumed straightforward
9627 s.op->newline() << "hp->bp_len = sdp->len;";
9628
26e63673 9629 s.op->newline() << "probe_point = sdp->probe->pp;"; // for error messages
b273669e
MW
9630 s.op->newline() << "#ifdef STAPCONF_HW_BREAKPOINT_CONTEXT";
9631 s.op->newline() << "stap_hwbkpt_ret_array[i] = register_wide_hw_breakpoint(hp, (void *)&enter_hwbkpt_probe, NULL);";
9632 s.op->newline() << "#else";
dac77b80 9633 s.op->newline() << "stap_hwbkpt_ret_array[i] = register_wide_hw_breakpoint(hp, (void *)&enter_hwbkpt_probe);";
b273669e 9634 s.op->newline() << "#endif";
43650b10 9635 s.op->newline() << "rc = 0;";
dac77b80 9636 s.op->newline() << "if (IS_ERR(stap_hwbkpt_ret_array[i])) {";
43650b10
WC
9637 s.op->newline(1) << "rc = PTR_ERR(stap_hwbkpt_ret_array[i]);";
9638 s.op->newline() << "stap_hwbkpt_ret_array[i] = 0;";
9639 s.op->newline(-1) << "}";
217ef1f4
WC
9640 s.op->newline() << "if (rc) {";
9641 s.op->newline(1) << "_stp_warn(\"Hwbkpt probe %s: registration error %d, addr %p, name %s\", probe_point, rc, addr, hwbkpt_symbol_name);";
43650b10 9642 s.op->newline() << "sdp->registered_p = 0;";
dac77b80 9643 s.op->newline(-1) << "}";
dd225250 9644 s.op->newline() << " else sdp->registered_p = 1;";
dd225250
PS
9645 s.op->newline(-1) << "}"; // for loop
9646}
9647
9648void
9649hwbkpt_derived_probe_group::emit_module_exit (systemtap_session& s)
9650{
9651 //Unregister hwbkpt probes.
dac77b80 9652 s.op->newline() << "for (i=0; i<" << hwbkpt_probes.size() << "; i++) {";
dd225250 9653 s.op->newline(1) << "struct stap_hwbkpt_probe *sdp = & stap_hwbkpt_probes[i];";
dac77b80
FCE
9654 s.op->newline() << "if (sdp->registered_p == 0) continue;";
9655 s.op->newline() << "unregister_wide_hw_breakpoint(stap_hwbkpt_ret_array[i]);";
dd225250
PS
9656 s.op->newline() << "sdp->registered_p = 0;";
9657 s.op->newline(-1) << "}";
9658}
9659
9660struct hwbkpt_builder: public derived_probe_builder
9661{
9662 hwbkpt_builder() {}
9663 virtual void build(systemtap_session & sess,
9664 probe * base,
9665 probe_point * location,
9666 literal_map_t const & parameters,
9667 vector<derived_probe *> & finished_results);
9668};
9669
9670void
9671hwbkpt_builder::build(systemtap_session & sess,
9672 probe * base,
9673 probe_point * location,
9674 literal_map_t const & parameters,
9675 vector<derived_probe *> & finished_results)
9676{
9677 string symbol_str_val;
9678 int64_t hwbkpt_address, len;
9679 bool has_addr, has_symbol_str, has_write, has_rw, has_len;
9680
b47f3a55 9681 if (! (sess.kernel_config["CONFIG_PERF_EVENTS"] == string("y")))
dc09353a 9682 throw SEMANTIC_ERROR (_("CONFIG_PERF_EVENTS not available on this kernel"),
b47f3a55
FCE
9683 location->components[0]->tok);
9684 if (! (sess.kernel_config["CONFIG_HAVE_HW_BREAKPOINT"] == string("y")))
dc09353a 9685 throw SEMANTIC_ERROR (_("CONFIG_HAVE_HW_BREAKPOINT not available on this kernel"),
b47f3a55
FCE
9686 location->components[0]->tok);
9687
dd225250
PS
9688 has_addr = get_param (parameters, TOK_HWBKPT, hwbkpt_address);
9689 has_symbol_str = get_param (parameters, TOK_HWBKPT, symbol_str_val);
9690 has_len = get_param (parameters, TOK_LENGTH, len);
9691 has_write = (parameters.find(TOK_HWBKPT_WRITE) != parameters.end());
9692 has_rw = (parameters.find(TOK_HWBKPT_RW) != parameters.end());
9693
9694 if (!has_len)
9695 len = 1;
9696
9697 if (has_addr)
9698 finished_results.push_back (new hwbkpt_derived_probe (base,
9699 location,
9700 hwbkpt_address,
9701 "",len,0,
9702 has_write,
9703 has_rw));
5d8a0aea 9704 else if (has_symbol_str)
dd225250
PS
9705 finished_results.push_back (new hwbkpt_derived_probe (base,
9706 location,
9707 0,
9708 symbol_str_val,len,0,
9709 has_write,
9710 has_rw));
5d8a0aea
FCE
9711 else
9712 assert (0);
dd225250 9713}
342d3f96 9714
0a6f5a3f
JS
9715// ------------------------------------------------------------------------
9716// statically inserted kernel-tracepoint derived probes
9717// ------------------------------------------------------------------------
9718
6fb70fb7 9719struct tracepoint_arg
79189b84 9720{
ad370dcc 9721 string name, c_type, typecast;
dcaa1a65 9722 bool usable, used, isptr;
f8a968bc 9723 Dwarf_Die type_die;
dcaa1a65 9724 tracepoint_arg(): usable(false), used(false), isptr(false) {}
6fb70fb7 9725};
79189b84 9726
0a6f5a3f
JS
9727struct tracepoint_derived_probe: public derived_probe
9728{
79189b84
JS
9729 tracepoint_derived_probe (systemtap_session& s,
9730 dwflpp& dw, Dwarf_Die& func_die,
9731 const string& tracepoint_name,
9732 probe* base_probe, probe_point* location);
bc9a523d 9733
79189b84 9734 systemtap_session& sess;
6fb70fb7
JS
9735 string tracepoint_name, header;
9736 vector <struct tracepoint_arg> args;
bc9a523d 9737
6fb70fb7 9738 void build_args(dwflpp& dw, Dwarf_Die& func_die);
d0bfd2ac 9739 void getargs (std::list<std::string> &arg_set) const;
79189b84 9740 void join_group (systemtap_session& s);
3e3bd7b6 9741 void print_dupe_stamp(ostream& o);
0a6f5a3f 9742};
79189b84
JS
9743
9744
0a6f5a3f 9745struct tracepoint_derived_probe_group: public generic_dpg<tracepoint_derived_probe>
79189b84 9746{
79189b84
JS
9747 void emit_module_decls (systemtap_session& s);
9748 void emit_module_init (systemtap_session& s);
9749 void emit_module_exit (systemtap_session& s);
0a6f5a3f 9750};
79189b84 9751
bc9a523d 9752
f8a968bc
JS
9753struct tracepoint_var_expanding_visitor: public var_expanding_visitor
9754{
9755 tracepoint_var_expanding_visitor(dwflpp& dw, const string& probe_name,
9756 vector <struct tracepoint_arg>& args):
9757 dw (dw), probe_name (probe_name), args (args) {}
9758 dwflpp& dw;
9759 const string& probe_name;
9760 vector <struct tracepoint_arg>& args;
bc9a523d 9761
f8a968bc
JS
9762 void visit_target_symbol (target_symbol* e);
9763 void visit_target_symbol_arg (target_symbol* e);
9764 void visit_target_symbol_context (target_symbol* e);
9765};
79189b84
JS
9766
9767
f8a968bc
JS
9768void
9769tracepoint_var_expanding_visitor::visit_target_symbol_arg (target_symbol* e)
75ead1f7 9770{
cc9001af 9771 string argname = e->sym_name();
75ead1f7 9772
f8a968bc
JS
9773 // search for a tracepoint parameter matching this name
9774 tracepoint_arg *arg = NULL;
9775 for (unsigned i = 0; i < args.size(); ++i)
dcaa1a65 9776 if (args[i].usable && args[i].name == argname)
f8a968bc
JS
9777 {
9778 arg = &args[i];
9779 arg->used = true;
9780 break;
9781 }
75ead1f7 9782
f8a968bc
JS
9783 if (arg == NULL)
9784 {
1d0499c2 9785 set<string> vars;
f8a968bc 9786 for (unsigned i = 0; i < args.size(); ++i)
1d0499c2
JL
9787 vars.insert("$" + args[i].name);
9788 vars.insert("$$name");
9789 vars.insert("$$parms");
9790 vars.insert("$$vars");
9791 string sugs = levenshtein_suggest(e->name, vars); // no need to limit, there's not that many
75ead1f7 9792
f8a968bc
JS
9793 // We hope that this value ends up not being referenced after all, so it
9794 // can be optimized out quietly.
1d0499c2
JL
9795 throw SEMANTIC_ERROR(_F("unable to find tracepoint variable '%s'%s",
9796 e->name.c_str(), sugs.empty() ? "" :
9797 (_(" (alternatives: ") + sugs + ")").c_str()), e->tok);
9798 // NB: we use 'alternatives' because we list all
f8a968bc
JS
9799 // NB: we can have multiple errors, since a target variable
9800 // may be expanded in several different contexts:
9801 // trace ("*") { $foo->bar }
f8a968bc 9802 }
75ead1f7 9803
f8a968bc 9804 // make sure we're not dereferencing base types
dc5a09fc 9805 if (!arg->isptr)
d19a9a82 9806 e->assert_no_components("tracepoint", true);
75ead1f7 9807
f8a968bc
JS
9808 // we can only write to dereferenced fields, and only if guru mode is on
9809 bool lvalue = is_active_lvalue(e);
9810 if (lvalue && (!dw.sess.guru_mode || e->components.empty()))
dc09353a 9811 throw SEMANTIC_ERROR(_F("write to tracepoint variable '%s' not permitted; need stap -g", e->name.c_str()), e->tok);
c69a87e0 9812
ad370dcc
JS
9813 // XXX: if a struct/union arg is passed by value, then writing to its fields
9814 // is also meaningless until you dereference past a pointer member. It's
9815 // harder to detect and prevent that though...
75ead1f7 9816
f8a968bc
JS
9817 if (e->components.empty())
9818 {
03c75a4a 9819 if (e->addressof)
dc09353a 9820 throw SEMANTIC_ERROR(_("cannot take address of tracepoint variable"), e->tok);
a45664f4 9821
3e3bd7b6 9822 // Just grab the value from the probe locals
a45664f4
JS
9823 symbol* sym = new symbol;
9824 sym->tok = e->tok;
9825 sym->name = "__tracepoint_arg_" + arg->name;
9826 provide (sym);
f8a968bc
JS
9827 }
9828 else
9829 {
5f36109e
JS
9830 // make a copy of the original as a bare target symbol for the tracepoint
9831 // value, which will be passed into the dwarf dereferencing code
9832 target_symbol* e2 = deep_copy_visitor::deep_copy(e);
9833 e2->components.clear();
9834
9835 if (e->components.back().type == target_symbol::comp_pretty_print)
9836 {
9837 if (lvalue)
dc09353a 9838 throw SEMANTIC_ERROR(_("cannot write to pretty-printed variable"), e->tok);
5f36109e 9839
d19a9a82 9840 dwarf_pretty_print dpp(dw, &arg->type_die, e2, arg->isptr, false, *e);
5f36109e
JS
9841 dpp.expand()->visit (this);
9842 return;
9843 }
9844
1c0be8c7 9845 bool userspace_p = false;
f8a968bc 9846 string fname = (string(lvalue ? "_tracepoint_tvar_set" : "_tracepoint_tvar_get")
cc9001af 9847 + "_" + e->sym_name()
aca66a36 9848 + "_" + lex_cast(tick++));
75ead1f7 9849
1c0be8c7
JS
9850 exp_type type = pe_long;
9851 string code = dw.literal_stmt_for_pointer (&arg->type_die, e, lvalue, type);
6fda2dff 9852
1c0be8c7
JS
9853 functioncall* n = synthetic_embedded_deref_call(dw.sess, fname, code, type,
9854 userspace_p, lvalue, e, e2);
75ead1f7 9855
f8a968bc
JS
9856 if (lvalue)
9857 {
9858 // Provide the functioncall to our parent, so that it can be
9859 // used to substitute for the assignment node immediately above
9860 // us.
9861 assert(!target_symbol_setter_functioncalls.empty());
9862 *(target_symbol_setter_functioncalls.top()) = n;
9863 }
75ead1f7 9864
1c0be8c7
JS
9865 // Revisit the functioncall so arguments can be expanded.
9866 n->visit (this);
f8a968bc 9867 }
75ead1f7
JS
9868}
9869
9870
f8a968bc
JS
9871void
9872tracepoint_var_expanding_visitor::visit_target_symbol_context (target_symbol* e)
0a6f5a3f 9873{
03c75a4a 9874 if (e->addressof)
dc09353a 9875 throw SEMANTIC_ERROR(_("cannot take address of context variable"), e->tok);
03c75a4a 9876
f8a968bc 9877 if (is_active_lvalue (e))
dc09353a 9878 throw SEMANTIC_ERROR(_F("write to tracepoint '%s' not permitted", e->name.c_str()), e->tok);
0a6f5a3f 9879
277c21bc 9880 if (e->name == "$$name")
f8a968bc 9881 {
5f36109e
JS
9882 e->assert_no_components("tracepoint");
9883
bfdaad1e
DS
9884 // Synthesize an embedded expression.
9885 embedded_expr *expr = new embedded_expr;
9886 expr->tok = e->tok;
9887 expr->code = string("/* string */ /* pure */ ")
6dceb5c9 9888 + string("c->ips.tracepoint_name ? c->ips.tracepoint_name : \"\"");
bfdaad1e 9889 provide (expr);
f8a968bc 9890 }
277c21bc 9891 else if (e->name == "$$vars" || e->name == "$$parms")
f8a968bc 9892 {
5f36109e
JS
9893 e->assert_no_components("tracepoint", true);
9894
1c922ad7 9895 print_format* pf = print_format::create(e->tok, "sprintf");
0a6f5a3f 9896
f8a968bc 9897 for (unsigned i = 0; i < args.size(); ++i)
b278033a 9898 {
dcaa1a65
JS
9899 if (!args[i].usable)
9900 continue;
f8a968bc
JS
9901 if (i > 0)
9902 pf->raw_components += " ";
9903 pf->raw_components += args[i].name;
3e3bd7b6 9904 target_symbol *tsym = new target_symbol;
f8a968bc 9905 tsym->tok = e->tok;
277c21bc 9906 tsym->name = "$" + args[i].name;
5f36109e 9907 tsym->components = e->components;
b278033a 9908
f8a968bc
JS
9909 // every variable should always be accessible!
9910 tsym->saved_conversion_error = 0;
8c2f50c0 9911 expression *texp = require<expression> (tsym); // NB: throws nothing ...
14a97852
JS
9912 if (tsym->saved_conversion_error) // ... but this is how we know it happened.
9913 {
9914 if (dw.sess.verbose>2)
e26c2f83 9915 for (const semantic_error *c = tsym->saved_conversion_error;
c081af73 9916 c != 0; c = c->get_chain())
4c5d9906 9917 clog << _("variable location problem [man error::dwarf]: ") << c->what() << endl;
14a97852
JS
9918 pf->raw_components += "=?";
9919 continue;
9920 }
b278033a 9921
5f36109e
JS
9922 if (!e->components.empty() &&
9923 e->components[0].type == target_symbol::comp_pretty_print)
9924 pf->raw_components += "=%s";
9925 else
9926 pf->raw_components += args[i].isptr ? "=%p" : "=%#x";
f8a968bc
JS
9927 pf->args.push_back(texp);
9928 }
0a6f5a3f 9929
f8a968bc
JS
9930 pf->components = print_format::string_to_components(pf->raw_components);
9931 provide (pf);
b278033a 9932 }
f8a968bc
JS
9933 else
9934 assert(0); // shouldn't get here
0a6f5a3f
JS
9935}
9936
0a6f5a3f 9937void
f8a968bc 9938tracepoint_var_expanding_visitor::visit_target_symbol (target_symbol* e)
0a6f5a3f 9939{
aff5d390 9940 try
c69a87e0 9941 {
bd1fcbad 9942 assert(e->name.size() > 0 && e->name[0] == '$');
aff5d390 9943
277c21bc 9944 if (e->name == "$$name" || e->name == "$$parms" || e->name == "$$vars")
c69a87e0 9945 visit_target_symbol_context (e);
bd1fcbad 9946
c69a87e0
FCE
9947 else
9948 visit_target_symbol_arg (e);
9949 }
9950 catch (const semantic_error &er)
9951 {
1af1e62d 9952 e->chain (er);
c69a87e0
FCE
9953 provide (e);
9954 }
0a6f5a3f
JS
9955}
9956
9957
79189b84
JS
9958tracepoint_derived_probe::tracepoint_derived_probe (systemtap_session& s,
9959 dwflpp& dw, Dwarf_Die& func_die,
9960 const string& tracepoint_name,
9961 probe* base, probe_point* loc):
4c5d1300 9962 derived_probe (base, loc, true /* .components soon rewritten */),
79189b84 9963 sess (s), tracepoint_name (tracepoint_name)
56894e91 9964{
79189b84
JS
9965 // create synthetic probe point name; preserve condition
9966 vector<probe_point::component*> comps;
9967 comps.push_back (new probe_point::component (TOK_KERNEL));
9968 comps.push_back (new probe_point::component (TOK_TRACE, new literal_string (tracepoint_name)));
9969 this->sole_location()->components = comps;
9970
6fb70fb7
JS
9971 // fill out the available arguments in this tracepoint
9972 build_args(dw, func_die);
56894e91 9973
6fb70fb7
JS
9974 // determine which header defined this tracepoint
9975 string decl_file = dwarf_decl_file(&func_die);
b78a0fbb 9976 header = decl_file;
d4393459
FCE
9977
9978#if 0 /* This convention is not enforced. */
6fb70fb7
JS
9979 size_t header_pos = decl_file.rfind("trace/");
9980 if (header_pos == string::npos)
dc09353a 9981 throw SEMANTIC_ERROR ("cannot parse header location for tracepoint '"
6fb70fb7
JS
9982 + tracepoint_name + "' in '"
9983 + decl_file + "'");
9984 header = decl_file.substr(header_pos);
d4393459 9985#endif
56894e91 9986
6fb70fb7
JS
9987 // tracepoints from FOO_event_types.h should really be included from FOO.h
9988 // XXX can dwarf tell us the include hierarchy? it would be better to
9989 // ... walk up to see which one was directly included by tracequery.c
3c1b3d06 9990 // XXX: see also PR9993.
d4393459 9991 size_t header_pos = header.find("_event_types");
6fb70fb7
JS
9992 if (header_pos != string::npos)
9993 header.erase(header_pos, 12);
56894e91 9994
f8a968bc
JS
9995 // Now expand the local variables in the probe body
9996 tracepoint_var_expanding_visitor v (dw, name, args);
8b095b45 9997 v.replace (this->body);
a45664f4
JS
9998 for (unsigned i = 0; i < args.size(); i++)
9999 if (args[i].used)
10000 {
10001 vardecl* v = new vardecl;
10002 v->name = "__tracepoint_arg_" + args[i].name;
10003 v->tok = this->tok;
58701b78 10004 v->set_arity(0, this->tok);
a45664f4 10005 v->type = pe_long;
69aa668e 10006 v->synthetic = true;
a45664f4
JS
10007 this->locals.push_back (v);
10008 }
56894e91 10009
79189b84 10010 if (sess.verbose > 2)
ce0f6648 10011 clog << "tracepoint-based " << name << " tracepoint='" << tracepoint_name << "'" << endl;
79189b84 10012}
dc38c0ae 10013
56894e91 10014
f8a968bc 10015static bool
dcaa1a65 10016resolve_tracepoint_arg_type(tracepoint_arg& arg)
46b84a80 10017{
d19a9a82 10018 Dwarf_Die type;
dcaa1a65 10019 switch (dwarf_tag(&arg.type_die))
b20febf3 10020 {
f8a968bc
JS
10021 case DW_TAG_typedef:
10022 case DW_TAG_const_type:
10023 case DW_TAG_volatile_type:
10024 // iterate on the referent type
3d1ad340 10025 return (dwarf_attr_die(&arg.type_die, DW_AT_type, &arg.type_die)
dcaa1a65 10026 && resolve_tracepoint_arg_type(arg));
f8a968bc 10027 case DW_TAG_base_type:
a52d2ac0 10028 case DW_TAG_enumeration_type:
f8a968bc 10029 // base types will simply be treated as script longs
dcaa1a65 10030 arg.isptr = false;
f8a968bc
JS
10031 return true;
10032 case DW_TAG_pointer_type:
dcaa1a65
JS
10033 // pointers can be treated as script longs,
10034 // and if we know their type, they can also be dereferenced
d19a9a82
JS
10035 type = arg.type_die;
10036 while (dwarf_attr_die(&arg.type_die, DW_AT_type, &arg.type_die))
10037 {
10038 // It still might be a non-type, e.g. const void,
10039 // so we need to strip away all qualifiers.
10040 int tag = dwarf_tag(&arg.type_die);
10041 if (tag != DW_TAG_typedef &&
10042 tag != DW_TAG_const_type &&
10043 tag != DW_TAG_volatile_type)
10044 {
10045 arg.isptr = true;
10046 break;
10047 }
10048 }
10049 if (!arg.isptr)
10050 arg.type_die = type;
ad370dcc
JS
10051 arg.typecast = "(intptr_t)";
10052 return true;
10053 case DW_TAG_structure_type:
10054 case DW_TAG_union_type:
10055 // for structs/unions which are passed by value, we turn it into
10056 // a pointer that can be dereferenced.
10057 arg.isptr = true;
10058 arg.typecast = "(intptr_t)&";
dcaa1a65 10059 return true;
f8a968bc
JS
10060 default:
10061 // should we consider other types too?
10062 return false;
b20febf3 10063 }
56894e91
JS
10064}
10065
10066
10067void
822a6a3d 10068tracepoint_derived_probe::build_args(dwflpp&, Dwarf_Die& func_die)
56894e91 10069{
6fb70fb7
JS
10070 Dwarf_Die arg;
10071 if (dwarf_child(&func_die, &arg) == 0)
10072 do
10073 if (dwarf_tag(&arg) == DW_TAG_formal_parameter)
10074 {
10075 // build a tracepoint_arg for this parameter
10076 tracepoint_arg tparg;
c60517ca 10077 tparg.name = dwarf_diename(&arg) ?: "";
56894e91 10078
6fb70fb7 10079 // read the type of this parameter
3d1ad340 10080 if (!dwarf_attr_die (&arg, DW_AT_type, &tparg.type_die)
f1c8f8a5 10081 || !dwarf_type_name(&tparg.type_die, tparg.c_type))
dc09353a 10082 throw SEMANTIC_ERROR (_F("cannot get type of parameter '%s' of tracepoint '%s'",
ce0f6648 10083 tparg.name.c_str(), tracepoint_name.c_str()));
a68f81a2 10084
dcaa1a65 10085 tparg.usable = resolve_tracepoint_arg_type(tparg);
6fb70fb7
JS
10086 args.push_back(tparg);
10087 if (sess.verbose > 4)
a52d2ac0
JS
10088 clog << _F("found parameter for tracepoint '%s': type:'%s' name:'%s' %s",
10089 tracepoint_name.c_str(), tparg.c_type.c_str(), tparg.name.c_str(),
10090 tparg.usable ? "ok" : "unavailable") << endl;
6fb70fb7
JS
10091 }
10092 while (dwarf_siblingof(&arg, &arg) == 0);
56894e91
JS
10093}
10094
dc38c0ae 10095void
d0bfd2ac 10096tracepoint_derived_probe::getargs(std::list<std::string> &arg_set) const
dc38c0ae 10097{
dcaa1a65
JS
10098 for (unsigned i = 0; i < args.size(); ++i)
10099 if (args[i].usable)
d0bfd2ac 10100 arg_set.push_back("$"+args[i].name+":"+args[i].c_type);
dc38c0ae
DS
10101}
10102
79189b84
JS
10103void
10104tracepoint_derived_probe::join_group (systemtap_session& s)
197a4d62 10105{
79189b84
JS
10106 if (! s.tracepoint_derived_probes)
10107 s.tracepoint_derived_probes = new tracepoint_derived_probe_group ();
10108 s.tracepoint_derived_probes->enroll (this);
10109}
e38d6504 10110
56894e91 10111
197a4d62 10112void
3e3bd7b6 10113tracepoint_derived_probe::print_dupe_stamp(ostream& o)
56894e91 10114{
3e3bd7b6
JS
10115 for (unsigned i = 0; i < args.size(); i++)
10116 if (args[i].used)
10117 o << "__tracepoint_arg_" << args[i].name << endl;
197a4d62 10118}
56894e91 10119
3e3bd7b6 10120
c9ccb642 10121static vector<string> tracepoint_extra_decls (systemtap_session& s, const string& header)
47dd066d 10122{
3c1b3d06
FCE
10123 vector<string> they_live;
10124 // PR 9993
10125 // XXX: may need this to be configurable
d4393459 10126 they_live.push_back ("#include <linux/skbuff.h>");
9e0cd21a
FCE
10127
10128 // PR11649: conditional extra header
10129 // for kvm tracepoints in 2.6.33ish
10130 if (s.kernel_config["CONFIG_KVM"] != string("")) {
d4393459
FCE
10131 they_live.push_back ("#include <linux/kvm_host.h>");
10132 }
10133
50b72692 10134 if (header.find("xfs") != string::npos && s.kernel_config["CONFIG_XFS_FS"] != string("")) {
d4393459 10135 they_live.push_back ("#define XFS_BIG_BLKNOS 1");
88637c31
FCE
10136 if (s.kernel_source_tree != "")
10137 they_live.push_back ("#include \"fs/xfs/xfs_types.h\""); // in kernel-source tree
d4393459
FCE
10138 they_live.push_back ("struct xfs_mount;");
10139 they_live.push_back ("struct xfs_inode;");
10140 they_live.push_back ("struct xfs_buf;");
10141 they_live.push_back ("struct xfs_bmbt_irec;");
c2cf1b87 10142 they_live.push_back ("struct xfs_trans;");
9e0cd21a 10143 }
d4393459 10144
50b72692 10145 if (header.find("nfs") != string::npos && s.kernel_config["CONFIG_NFSD"] != string("")) {
d4393459
FCE
10146 they_live.push_back ("struct rpc_task;");
10147 }
b64d65e2
FCE
10148 // RHEL6.3
10149 if (header.find("rpc") != string::npos && s.kernel_config["CONFIG_NFSD"] != string("")) {
10150 they_live.push_back ("struct rpc_clnt;");
10151 they_live.push_back ("struct rpc_wait_queue;");
10152 }
d4393459
FCE
10153
10154 they_live.push_back ("#include <asm/cputime.h>");
10155
c2cf1b87
FCE
10156 // linux 3.0
10157 they_live.push_back ("struct cpu_workqueue_struct;");
10158
50b72692 10159 if (header.find("ext4") != string::npos && s.kernel_config["CONFIG_EXT4_FS"] != string(""))
c2cf1b87
FCE
10160 if (s.kernel_source_tree != "")
10161 they_live.push_back ("#include \"fs/ext4/ext4.h\""); // in kernel-source tree
10162
d97d428e 10163 if (header.find("ext3") != string::npos)
50b72692
HP
10164 they_live.push_back ("struct ext3_reserve_window_node;");
10165
d97d428e
FCE
10166 if (header.find("workqueue") != string::npos)
10167 {
10168 they_live.push_back ("struct pool_workqueue;");
10169 they_live.push_back ("struct work_struct;");
10170 }
10171
10172 if (header.find("asoc") != string::npos)
10173 they_live.push_back ("struct snd_soc_dapm_path;");
10174
10175 if (header.find("9p") != string::npos)
10176 {
10177 they_live.push_back ("struct p9_client;");
10178 they_live.push_back ("struct p9_fcall;");
10179 }
10180
10181 if (header.find("bcache") != string::npos)
10182 {
10183 they_live.push_back ("struct bkey;");
10184 they_live.push_back ("struct btree;");
10185 they_live.push_back ("struct cache_set;");
10186 they_live.push_back ("struct cache;");
10187 }
10188
10189 if (header.find("f2fs") != string::npos)
10190 {
10191 // cannot get fs/f2fs/f2fs.h #included
10192 they_live.push_back ("typedef u32 block_t;");
10193 they_live.push_back ("typedef u32 nid_t;");
10194 }
10195
10196 if (header.find("radeon") != string::npos)
10197 they_live.push_back ("struct radeon_bo;");
10198
10199 // argh, 3.11, i915_trace.h -> i915_drv.h -> i915_reg.h without -I
10200 // also brcms_trace_events.h -> ... -> "types.h"
10201 // XXX: need a way to add a temporary -I flag
10202
10203 if (header.find("/ath/") != string::npos)
10204 they_live.push_back ("struct ath5k_hw;");
10205
10206
3c1b3d06
FCE
10207 return they_live;
10208}
47dd066d
WC
10209
10210
10211void
79189b84 10212tracepoint_derived_probe_group::emit_module_decls (systemtap_session& s)
47dd066d 10213{
79189b84
JS
10214 if (probes.empty())
10215 return;
47dd066d 10216
96b030fe
JS
10217 s.op->newline() << "/* ---- tracepoint probes ---- */";
10218 s.op->newline();
79189b84 10219
47dd066d 10220
a4b9c3b3
FCE
10221 // We create a MODULE_aux_N.c file for each tracepoint header, to allow them
10222 // to be separately compiled. That's because kernel tracepoint headers sometimes
10223 // conflict. PR13155.
10224
10225 map<string,translator_output*> per_header_aux;
10226 // GC NB: the translator_output* structs are owned/retained by the systemtap_session.
47dd066d 10227
6fb70fb7
JS
10228 for (unsigned i = 0; i < probes.size(); ++i)
10229 {
10230 tracepoint_derived_probe *p = probes[i];
75ae2ec9 10231 string header = p->header;
5f73a260 10232
a4b9c3b3
FCE
10233 // We cache the auxiliary output files on a per-header basis. We don't
10234 // need one aux file per tracepoint, only one per tracepoint-header.
10235 translator_output *tpop = per_header_aux[header];
10236 if (tpop == 0)
10237 {
10238 tpop = s.op_create_auxiliary();
10239 per_header_aux[header] = tpop;
10240
10241 // PR9993: Add extra headers to work around undeclared types in individual
10242 // include/trace/foo.h files
10243 const vector<string>& extra_decls = tracepoint_extra_decls (s, header);
10244 for (unsigned z=0; z<extra_decls.size(); z++)
10245 tpop->newline() << extra_decls[z] << "\n";
720c435f 10246
a4b9c3b3
FCE
10247 // strip include/ substring, the same way as done in get_tracequery_module()
10248 size_t root_pos = header.rfind("include/");
10249 header = ((root_pos != string::npos) ? header.substr(root_pos + 8) : header);
bfffa443
FCE
10250
10251 tpop->newline() << "#include <linux/tracepoint.h>" << endl;
a4b9c3b3 10252 tpop->newline() << "#include <" << header << ">";
720c435f 10253
a4b9c3b3
FCE
10254 // Starting in 2.6.35, at the same time NOARGS was added, the callback
10255 // always has a void* as the first parameter. PR11599
10256 tpop->newline() << "#ifdef DECLARE_TRACE_NOARGS";
10257 tpop->newline() << "#define STAP_TP_DATA , NULL";
10258 tpop->newline() << "#define STAP_TP_PROTO void *cb_data"
10259 << " __attribute__ ((unused))";
10260 if (!p->args.empty())
10261 tpop->line() << ",";
10262 tpop->newline() << "#else";
10263 tpop->newline() << "#define STAP_TP_DATA";
10264 tpop->newline() << "#define STAP_TP_PROTO";
10265 if (p->args.empty())
10266 tpop->line() << " void";
10267 tpop->newline() << "#endif";
720c435f
JS
10268
10269 tpop->newline() << "#define intptr_t long";
a4b9c3b3
FCE
10270 }
10271
720c435f
JS
10272 // collect the args that are actually in use
10273 vector<const tracepoint_arg*> used_args;
6fb70fb7 10274 for (unsigned j = 0; j < p->args.size(); ++j)
720c435f
JS
10275 if (p->args[j].used)
10276 used_args.push_back(&p->args[j]);
10277
a4b9c3b3
FCE
10278 // forward-declare the generated-side tracepoint callback
10279 tpop->newline() << "void enter_real_tracepoint_probe_" << i << "(";
720c435f
JS
10280 tpop->indent(2);
10281 if (used_args.empty())
10282 tpop->line() << "void";
10283 for (unsigned j = 0; j < used_args.size(); ++j)
6fb70fb7
JS
10284 {
10285 if (j > 0)
a4b9c3b3
FCE
10286 tpop->line() << ", ";
10287 tpop->line() << "int64_t";
6fb70fb7 10288 }
720c435f
JS
10289 tpop->line() << ");";
10290 tpop->indent(-2);
5f73a260 10291
a4b9c3b3
FCE
10292 // define the generated-side tracepoint callback - in the main translator-output
10293 s.op->newline() << "void enter_real_tracepoint_probe_" << i << "(";
720c435f
JS
10294 s.op->indent(2);
10295 if (used_args.empty())
10296 s.op->newline() << "void";
10297 for (unsigned j = 0; j < used_args.size(); ++j)
a4b9c3b3 10298 {
a4b9c3b3
FCE
10299 if (j > 0)
10300 s.op->line() << ", ";
720c435f 10301 s.op->newline() << "int64_t __tracepoint_arg_" << used_args[j]->name;
6fb70fb7 10302 }
5f73a260
JS
10303 s.op->newline() << ")";
10304 s.op->newline(-2) << "{";
7c3e97f4 10305 s.op->newline(1) << "const struct stap_probe * const probe = "
faea5e16 10306 << common_probe_init (p) << ";";
71db462b 10307 common_probe_entryfn_prologue (s, "STAP_SESSION_RUNNING", "probe",
cda141c2 10308 "stp_probe_type_tracepoint");
6dceb5c9 10309 s.op->newline() << "c->ips.tracepoint_name = "
c12d974f
FCE
10310 << lex_cast_qstring (p->tracepoint_name)
10311 << ";";
720c435f
JS
10312 for (unsigned j = 0; j < used_args.size(); ++j)
10313 {
10314 s.op->newline() << "c->probe_locals." << p->name
0386bbcf 10315 << "." + s.up->c_localname("__tracepoint_arg_" + used_args[j]->name)
720c435f
JS
10316 << " = __tracepoint_arg_" << used_args[j]->name << ";";
10317 }
26e63673 10318 s.op->newline() << "(*probe->ph) (c);";
f887a8c9 10319 common_probe_entryfn_epilogue (s, true);
6fb70fb7 10320 s.op->newline(-1) << "}";
47dd066d 10321
a4b9c3b3 10322 // define the real tracepoint callback function
720c435f 10323 tpop->newline() << "static void enter_tracepoint_probe_" << i << "(";
a4b9c3b3
FCE
10324 tpop->newline(2) << "STAP_TP_PROTO";
10325 for (unsigned j = 0; j < p->args.size(); ++j)
10326 {
10327 if (j > 0)
10328 tpop->line() << ", ";
10329 tpop->newline() << p->args[j].c_type << " __tracepoint_arg_" << p->args[j].name;
10330 }
10331 tpop->newline() << ")";
10332 tpop->newline(-2) << "{";
720c435f
JS
10333 tpop->newline(1) << "enter_real_tracepoint_probe_" << i << "(";
10334 tpop->indent(2);
10335 for (unsigned j = 0; j < used_args.size(); ++j)
10336 {
10337 if (j > 0)
10338 tpop->line() << ", ";
10339 tpop->newline() << "(int64_t)" << used_args[j]->typecast
10340 << "__tracepoint_arg_" << used_args[j]->name;
10341 }
10342 tpop->newline() << ");";
10343 tpop->newline(-3) << "}";
a4b9c3b3
FCE
10344
10345
96b030fe 10346 // emit normalized registration functions
720c435f 10347 tpop->newline() << "int register_tracepoint_probe_" << i << "(void) {";
a4b9c3b3 10348 tpop->newline(1) << "return register_trace_" << p->tracepoint_name
5f73a260 10349 << "(enter_tracepoint_probe_" << i << " STAP_TP_DATA);";
a4b9c3b3 10350 tpop->newline(-1) << "}";
47dd066d 10351
86758d5f
JS
10352 // NB: we're not prepared to deal with unreg failures. However, failures
10353 // can only occur if the tracepoint doesn't exist (yet?), or if we
10354 // weren't even registered. The former should be OKed by the initial
10355 // registration call, and the latter is safe to ignore.
720c435f 10356 tpop->newline() << "void unregister_tracepoint_probe_" << i << "(void) {";
a4b9c3b3 10357 tpop->newline(1) << "(void) unregister_trace_" << p->tracepoint_name
5f73a260 10358 << "(enter_tracepoint_probe_" << i << " STAP_TP_DATA);";
a4b9c3b3
FCE
10359 tpop->newline(-1) << "}";
10360 tpop->newline();
5f73a260 10361
720c435f
JS
10362 // declare normalized registration functions
10363 s.op->newline() << "int register_tracepoint_probe_" << i << "(void);";
10364 s.op->newline() << "void unregister_tracepoint_probe_" << i << "(void);";
5f73a260 10365
a4b9c3b3 10366 tpop->assert_0_indent();
af304783
DS
10367 }
10368
96b030fe
JS
10369 // emit an array of registration functions for easy init/shutdown
10370 s.op->newline() << "static struct stap_tracepoint_probe {";
10371 s.op->newline(1) << "int (*reg)(void);";
86758d5f 10372 s.op->newline(0) << "void (*unreg)(void);";
96b030fe
JS
10373 s.op->newline(-1) << "} stap_tracepoint_probes[] = {";
10374 s.op->indent(1);
10375 for (unsigned i = 0; i < probes.size(); ++i)
10376 {
10377 s.op->newline () << "{";
10378 s.op->line() << " .reg=&register_tracepoint_probe_" << i << ",";
10379 s.op->line() << " .unreg=&unregister_tracepoint_probe_" << i;
10380 s.op->line() << " },";
10381 }
10382 s.op->newline(-1) << "};";
10383 s.op->newline();
47dd066d
WC
10384}
10385
10386
79189b84
JS
10387void
10388tracepoint_derived_probe_group::emit_module_init (systemtap_session &s)
47dd066d 10389{
79189b84
JS
10390 if (probes.size () == 0)
10391 return;
47dd066d 10392
79189b84 10393 s.op->newline() << "/* init tracepoint probes */";
96b030fe
JS
10394 s.op->newline() << "for (i=0; i<" << probes.size() << "; i++) {";
10395 s.op->newline(1) << "rc = stap_tracepoint_probes[i].reg();";
10396 s.op->newline() << "if (rc) {";
10397 s.op->newline(1) << "for (j=i-1; j>=0; j--)"; // partial rollback
10398 s.op->newline(1) << "stap_tracepoint_probes[j].unreg();";
10399 s.op->newline(-1) << "break;"; // don't attempt to register any more probes
10400 s.op->newline(-1) << "}";
10401 s.op->newline(-1) << "}";
47dd066d 10402
bc9a523d
FCE
10403 // This would be technically proper (on those autoconf-detectable
10404 // kernels that include this function in tracepoint.h), however we
10405 // already make several calls to synchronze_sched() during our
10406 // shutdown processes.
47dd066d 10407
bc9a523d
FCE
10408 // s.op->newline() << "if (rc)";
10409 // s.op->newline(1) << "tracepoint_synchronize_unregister();";
10410 // s.op->indent(-1);
79189b84 10411}
47dd066d
WC
10412
10413
79189b84
JS
10414void
10415tracepoint_derived_probe_group::emit_module_exit (systemtap_session& s)
47dd066d 10416{
79189b84
JS
10417 if (probes.empty())
10418 return;
47dd066d 10419
96b030fe
JS
10420 s.op->newline() << "/* deregister tracepoint probes */";
10421 s.op->newline() << "for (i=0; i<" << probes.size() << "; i++)";
10422 s.op->newline(1) << "stap_tracepoint_probes[i].unreg();";
10423 s.op->indent(-1);
47dd066d 10424
bc9a523d 10425 // Not necessary: see above.
47dd066d 10426
bc9a523d 10427 // s.op->newline() << "tracepoint_synchronize_unregister();";
79189b84 10428}
b20febf3 10429
47dd066d 10430
75ead1f7 10431struct tracepoint_query : public base_query
47dd066d 10432{
75ead1f7
JS
10433 tracepoint_query(dwflpp & dw, const string & tracepoint,
10434 probe * base_probe, probe_point * base_loc,
10435 vector<derived_probe *> & results):
10436 base_query(dw, "*"), tracepoint(tracepoint),
10437 base_probe(base_probe), base_loc(base_loc),
10438 results(results) {}
47dd066d 10439
75ead1f7 10440 const string& tracepoint;
47dd066d 10441
75ead1f7
JS
10442 probe * base_probe;
10443 probe_point * base_loc;
10444 vector<derived_probe *> & results;
f982c59b 10445 set<string> probed_names;
47dd066d 10446
75ead1f7
JS
10447 void handle_query_module();
10448 int handle_query_cu(Dwarf_Die * cudie);
10449 int handle_query_func(Dwarf_Die * func);
822a6a3d 10450 void query_library (const char *) {}
576eaefe 10451 void query_plt (const char *entry, size_t addr) {}
b20febf3 10452
5c378838 10453 static int tracepoint_query_cu (Dwarf_Die * cudie, tracepoint_query * q);
7d007451 10454 static int tracepoint_query_func (Dwarf_Die * func, tracepoint_query * q);
75ead1f7 10455};
47dd066d
WC
10456
10457
10458void
75ead1f7 10459tracepoint_query::handle_query_module()
47dd066d 10460{
75ead1f7 10461 // look for the tracepoints in each CU
337b7c44 10462 dw.iterate_over_cus(tracepoint_query_cu, this, false);
47dd066d
WC
10463}
10464
10465
75ead1f7
JS
10466int
10467tracepoint_query::handle_query_cu(Dwarf_Die * cudie)
47dd066d 10468{
75ead1f7 10469 dw.focus_on_cu (cudie);
d906ab9d 10470 dw.mod_info->get_symtab(this);
47dd066d 10471
75ead1f7
JS
10472 // look at each function to see if it's a tracepoint
10473 string function = "stapprobe_" + tracepoint;
10474 return dw.iterate_over_functions (tracepoint_query_func, this, function);
47dd066d
WC
10475}
10476
10477
75ead1f7
JS
10478int
10479tracepoint_query::handle_query_func(Dwarf_Die * func)
47dd066d 10480{
75ead1f7 10481 dw.focus_on_function (func);
47dd066d 10482
60d98537 10483 assert(startswith(dw.function_name, "stapprobe_"));
75ead1f7 10484 string tracepoint_instance = dw.function_name.substr(10);
f982c59b
JS
10485
10486 // check for duplicates -- sometimes tracepoint headers may be indirectly
10487 // included in more than one of our tracequery modules.
10488 if (!probed_names.insert(tracepoint_instance).second)
10489 return DWARF_CB_OK;
10490
79189b84
JS
10491 derived_probe *dp = new tracepoint_derived_probe (dw.sess, dw, *func,
10492 tracepoint_instance,
10493 base_probe, base_loc);
10494 results.push_back (dp);
75ead1f7 10495 return DWARF_CB_OK;
47dd066d
WC
10496}
10497
10498
75ead1f7 10499int
5c378838 10500tracepoint_query::tracepoint_query_cu (Dwarf_Die * cudie, tracepoint_query * q)
47dd066d 10501{
85007c04 10502 if (pending_interrupts) return DWARF_CB_ABORT;
75ead1f7 10503 return q->handle_query_cu(cudie);
47dd066d
WC
10504}
10505
10506
75ead1f7 10507int
7d007451 10508tracepoint_query::tracepoint_query_func (Dwarf_Die * func, tracepoint_query * q)
47dd066d 10509{
85007c04 10510 if (pending_interrupts) return DWARF_CB_ABORT;
75ead1f7 10511 return q->handle_query_func(func);
47dd066d
WC
10512}
10513
10514
0a6f5a3f 10515struct tracepoint_builder: public derived_probe_builder
47dd066d 10516{
0a6f5a3f
JS
10517private:
10518 dwflpp *dw;
10519 bool init_dw(systemtap_session& s);
c9ccb642
FCE
10520 void get_tracequery_modules(systemtap_session& s,
10521 const vector<string>& headers,
10522 vector<string>& modules);
47dd066d 10523
0a6f5a3f 10524public:
47dd066d 10525
0a6f5a3f
JS
10526 tracepoint_builder(): dw(0) {}
10527 ~tracepoint_builder() { delete dw; }
47dd066d 10528
0a6f5a3f
JS
10529 void build_no_more (systemtap_session& s)
10530 {
10531 if (dw && s.verbose > 3)
b530b5b3 10532 clog << _("tracepoint_builder releasing dwflpp") << endl;
0a6f5a3f
JS
10533 delete dw;
10534 dw = NULL;
435f53a7
FCE
10535
10536 delete_session_module_cache (s);
0a6f5a3f 10537 }
47dd066d 10538
0a6f5a3f
JS
10539 void build(systemtap_session& s,
10540 probe *base, probe_point *location,
10541 literal_map_t const& parameters,
10542 vector<derived_probe*>& finished_results);
10543};
47dd066d 10544
47dd066d 10545
c9ccb642 10546
2a0e62a8 10547// Create (or cache) one or more tracequery .o modules, based upon the
c9ccb642
FCE
10548// tracepoint-related header files given. Return the generated or cached
10549// modules[].
10550
10551void
10552tracepoint_builder::get_tracequery_modules(systemtap_session& s,
10553 const vector<string>& headers,
10554 vector<string>& modules)
0a6f5a3f 10555{
c95eddf7 10556 if (s.verbose > 2)
55e50c24 10557 {
ce0f6648 10558 clog << _F("Pass 2: getting a tracepoint query for %zu headers: ", headers.size()) << endl;
55e50c24
JS
10559 for (size_t i = 0; i < headers.size(); ++i)
10560 clog << " " << headers[i] << endl;
10561 }
c95eddf7 10562
2a0e62a8
JS
10563 map<string,string> headers_cache_obj; // header name -> cache/.../tracequery_hash.o file name
10564 // Map the headers to cache .o names. Note that this has side-effects of
c9ccb642
FCE
10565 // creating the $SYSTEMTAP_DIR/.cache/XX/... directory and the hash-log file,
10566 // so we prefer not to repeat this.
10567 vector<string> uncached_headers;
10568 for (size_t i=0; i<headers.size(); i++)
2a0e62a8 10569 headers_cache_obj[headers[i]] = find_tracequery_hash(s, headers[i]);
c9ccb642
FCE
10570
10571 // They may be in the cache already.
10572 if (s.use_cache && !s.poison_cache)
10573 for (size_t i=0; i<headers.size(); i++)
10574 {
10575 // see if the cached module exists
2a0e62a8 10576 const string& tracequery_path = headers_cache_obj[headers[i]];
c9ccb642
FCE
10577 if (!tracequery_path.empty() && file_exists(tracequery_path))
10578 {
10579 if (s.verbose > 2)
10580 clog << _F("Pass 2: using cached %s", tracequery_path.c_str()) << endl;
47dd066d 10581
c252fca2
JS
10582 // an empty file is a cached failure
10583 if (get_file_size(tracequery_path) > 0)
10584 modules.push_back (tracequery_path);
c9ccb642
FCE
10585 }
10586 else
10587 uncached_headers.push_back(headers[i]);
10588 }
10589 else
10590 uncached_headers = headers;
f982c59b 10591
c9ccb642
FCE
10592 // If we have nothing left to search for, quit
10593 if (uncached_headers.empty()) return;
55e50c24 10594
c9ccb642 10595 map<string,string> headers_tracequery_src; // header -> C-source code mapping
55e50c24 10596
c9ccb642
FCE
10597 // We could query several subsets of headers[] to make this go
10598 // faster, but let's KISS and do one at a time.
10599 for (size_t i=0; i<uncached_headers.size(); i++)
55e50c24 10600 {
c9ccb642
FCE
10601 const string& header = uncached_headers[i];
10602
10603 // create a tracequery source file
10604 ostringstream osrc;
10605
10606 // PR9993: Add extra headers to work around undeclared types in individual
10607 // include/trace/foo.h files
10608 vector<string> short_decls = tracepoint_extra_decls(s, header);
10609
10610 // add each requested tracepoint header
75ae2ec9 10611 size_t root_pos = header.rfind("include/");
832f100d 10612 short_decls.push_back(string("#include <") +
75ae2ec9 10613 ((root_pos != string::npos) ? header.substr(root_pos + 8) : header) +
d4393459 10614 string(">"));
f982c59b 10615
c9ccb642
FCE
10616 osrc << "#ifdef CONFIG_TRACEPOINTS" << endl;
10617 osrc << "#include <linux/tracepoint.h>" << endl;
832f100d
JS
10618
10619 // the kernel has changed this naming a few times, previously TPPROTO,
10620 // TP_PROTO, TPARGS, TP_ARGS, etc. so let's just dupe the latest.
10621 osrc << "#ifndef PARAMS" << endl;
10622 osrc << "#define PARAMS(args...) args" << endl;
10623 osrc << "#endif" << endl;
10624
c9ccb642
FCE
10625 // override DECLARE_TRACE to synthesize probe functions for us
10626 osrc << "#undef DECLARE_TRACE" << endl;
10627 osrc << "#define DECLARE_TRACE(name, proto, args) \\" << endl;
10628 osrc << " void stapprobe_##name(proto) {}" << endl;
832f100d 10629
c9ccb642
FCE
10630 // 2.6.35 added the NOARGS variant, but it's the same for us
10631 osrc << "#undef DECLARE_TRACE_NOARGS" << endl;
10632 osrc << "#define DECLARE_TRACE_NOARGS(name) \\" << endl;
10633 osrc << " DECLARE_TRACE(name, void, )" << endl;
832f100d
JS
10634
10635 // 2.6.38 added the CONDITION variant, which can also just redirect
10636 osrc << "#undef DECLARE_TRACE_CONDITION" << endl;
10637 osrc << "#define DECLARE_TRACE_CONDITION(name, proto, args, cond) \\" << endl;
10638 osrc << " DECLARE_TRACE(name, PARAMS(proto), PARAMS(args))" << endl;
10639
c9ccb642
FCE
10640 // older tracepoints used DEFINE_TRACE, so redirect that too
10641 osrc << "#undef DEFINE_TRACE" << endl;
10642 osrc << "#define DEFINE_TRACE(name, proto, args) \\" << endl;
832f100d
JS
10643 osrc << " DECLARE_TRACE(name, PARAMS(proto), PARAMS(args))" << endl;
10644
c9ccb642
FCE
10645 // add the specified decls/#includes
10646 for (unsigned z=0; z<short_decls.size(); z++)
10647 osrc << "#undef TRACE_INCLUDE_FILE\n"
10648 << "#undef TRACE_INCLUDE_PATH\n"
10649 << short_decls[z] << "\n";
10650
10651 // finish up the module source
10652 osrc << "#endif /* CONFIG_TRACEPOINTS */" << endl;
47dd066d 10653
c9ccb642
FCE
10654 // save the source file away
10655 headers_tracequery_src[header] = osrc.str();
55e50c24 10656 }
f982c59b 10657
c9ccb642 10658 // now build them all together
2a0e62a8 10659 map<string,string> tracequery_objs = make_tracequeries(s, headers_tracequery_src);
47dd066d 10660
c9ccb642 10661 // now plop them into the cache
b278033a 10662 if (s.use_cache)
c9ccb642
FCE
10663 for (size_t i=0; i<uncached_headers.size(); i++)
10664 {
10665 const string& header = uncached_headers[i];
2a0e62a8
JS
10666 const string& tracequery_obj = tracequery_objs[header];
10667 const string& tracequery_path = headers_cache_obj[header];
10668 if (tracequery_obj !="" && file_exists(tracequery_obj))
c9ccb642 10669 {
2a0e62a8 10670 copy_file(tracequery_obj, tracequery_path, s.verbose > 2);
c9ccb642
FCE
10671 modules.push_back (tracequery_path);
10672 }
c252fca2
JS
10673 else
10674 // cache an empty file for failures
10675 copy_file("/dev/null", tracequery_path, s.verbose > 2);
c9ccb642 10676 }
f982c59b
JS
10677}
10678
10679
d4393459 10680
f982c59b
JS
10681bool
10682tracepoint_builder::init_dw(systemtap_session& s)
10683{
10684 if (dw != NULL)
10685 return true;
10686
10687 vector<string> tracequery_modules;
55e50c24 10688 vector<string> system_headers;
f982c59b
JS
10689
10690 glob_t trace_glob;
d4393459
FCE
10691
10692 // find kernel_source_tree
10693 if (s.kernel_source_tree == "")
f982c59b 10694 {
d4393459
FCE
10695 unsigned found;
10696 DwflPtr dwfl_ptr = setup_dwfl_kernel ("kernel", &found, s);
10697 Dwfl *dwfl = dwfl_ptr.get()->dwfl;
10698 if (found)
10699 {
10700 Dwarf_Die *cudie = 0;
10701 Dwarf_Addr bias;
10702 while ((cudie = dwfl_nextcu (dwfl, cudie, &bias)) != NULL)
10703 {
e19ebcf7 10704 assert_no_interrupts();
d4393459
FCE
10705 Dwarf_Attribute attr;
10706 const char* name = dwarf_formstring (dwarf_attr (cudie, DW_AT_comp_dir, &attr));
36d65b45 10707 if (name)
d4393459 10708 {
36d65b45
JS
10709 // check that the path actually exists locally before we try to use it
10710 if (file_exists(name))
10711 {
10712 if (s.verbose > 2)
10713 clog << _F("Located kernel source tree (DW_AT_comp_dir) at '%s'", name) << endl;
10714 s.kernel_source_tree = name;
10715 }
10716 else
10717 {
10718 if (s.verbose > 2)
10719 clog << _F("Ignoring inaccessible kernel source tree (DW_AT_comp_dir) at '%s'", name) << endl;
10720 }
61f1a63b 10721
d4393459
FCE
10722 break; // skip others; modern Kbuild uses same comp_dir for them all
10723 }
10724 }
10725 }
10726 }
10727
10728 // prefixes
10729 vector<string> glob_prefixes;
10730 glob_prefixes.push_back (s.kernel_build_tree);
10731 if (s.kernel_source_tree != "")
10732 glob_prefixes.push_back (s.kernel_source_tree);
10733
10734 // suffixes
10735 vector<string> glob_suffixes;
10736 glob_suffixes.push_back("include/trace/events/*.h");
10737 glob_suffixes.push_back("include/trace/*.h");
d97d428e 10738 glob_suffixes.push_back("include/ras/*_event.h");
d4393459 10739 glob_suffixes.push_back("arch/x86/kvm/*trace.h");
d97d428e
FCE
10740 glob_suffixes.push_back("arch/x86/kernel/*trace.h");
10741 glob_suffixes.push_back("arch/*/include/asm/trace*.h");
10742 glob_suffixes.push_back("arch/*/include/asm/trace/*.h");
f89ff3e2 10743 glob_suffixes.push_back("fs/xfs/linux-*/xfs_tr*.h");
d97d428e
FCE
10744 glob_suffixes.push_back("fs/*/*trace*.h");
10745 glob_suffixes.push_back("net/*/*trace*.h");
10746 glob_suffixes.push_back("sound/pci/hda/*_trace.h");
10747 glob_suffixes.push_back("drivers/gpu/drm/*_trace.h");
10748 glob_suffixes.push_back("drivers/gpu/drm/*/*_trace.h");
10749 glob_suffixes.push_back("drivers/net/wireless/*/*/*trace*.h");
10750 glob_suffixes.push_back("drivers/usb/host/*trace*.h");
10751
10752 // see also tracepoint_extra_decls above
d4393459
FCE
10753
10754 // compute cartesian product
10755 vector<string> globs;
10756 for (unsigned i=0; i<glob_prefixes.size(); i++)
10757 for (unsigned j=0; j<glob_suffixes.size(); j++)
10758 globs.push_back (glob_prefixes[i]+string("/")+glob_suffixes[j]);
10759
8aa43b8d 10760 set<string> duped_headers;
d4393459
FCE
10761 for (unsigned z = 0; z < globs.size(); z++)
10762 {
10763 string glob_str = globs[z];
10764 if (s.verbose > 3)
b530b5b3 10765 clog << _("Checking tracepoint glob ") << glob_str << endl;
d4393459 10766
067cc66f
CM
10767 int r = glob(glob_str.c_str(), 0, NULL, &trace_glob);
10768 if (r == GLOB_NOSPACE || r == GLOB_ABORTED)
10769 throw runtime_error("Error globbing tracepoint");
10770
f982c59b
JS
10771 for (unsigned i = 0; i < trace_glob.gl_pathc; ++i)
10772 {
10773 string header(trace_glob.gl_pathv[i]);
10774
10775 // filter out a few known "internal-only" headers
60d98537
JS
10776 if (endswith(header, "/define_trace.h") ||
10777 endswith(header, "/ftrace.h") ||
10778 endswith(header, "/trace_events.h") ||
10779 endswith(header, "_event_types.h"))
f982c59b
JS
10780 continue;
10781
b1966849
FCE
10782 // With headers now plopped under arch/FOO/include/asm/*,
10783 // the following logic miss some tracepoints.
10784#if 0
8aa43b8d
JS
10785 // skip identical headers from the build and source trees.
10786 size_t root_pos = header.rfind("include/");
10787 if (root_pos != string::npos &&
10788 !duped_headers.insert(header.substr(root_pos + 8)).second)
10789 continue;
b1966849 10790#endif
8aa43b8d 10791
55e50c24 10792 system_headers.push_back(header);
f982c59b
JS
10793 }
10794 globfree(&trace_glob);
10795 }
10796
c9ccb642
FCE
10797 // Build tracequery modules
10798 get_tracequery_modules(s, system_headers, tracequery_modules);
55e50c24 10799
f982c59b
JS
10800 // TODO: consider other sources of tracepoint headers too, like from
10801 // a command-line parameter or some environment or .systemtaprc
47dd066d 10802
59c11f91 10803 dw = new dwflpp(s, tracequery_modules, true);
0a6f5a3f
JS
10804 return true;
10805}
47dd066d 10806
0a6f5a3f
JS
10807void
10808tracepoint_builder::build(systemtap_session& s,
10809 probe *base, probe_point *location,
10810 literal_map_t const& parameters,
10811 vector<derived_probe*>& finished_results)
10812{
10813 if (!init_dw(s))
10814 return;
47dd066d 10815
75ead1f7
JS
10816 string tracepoint;
10817 assert(get_param (parameters, TOK_TRACE, tracepoint));
47dd066d 10818
75ead1f7 10819 tracepoint_query q(*dw, tracepoint, base, location, finished_results);
d906ab9d 10820 unsigned results_pre = finished_results.size();
06de3a04 10821 dw->iterate_over_modules<base_query>(&query_module, &q);
d906ab9d
JL
10822 unsigned results_post = finished_results.size();
10823
10824 // Did we fail to find a match? Let's suggest something!
10825 if (results_pre == results_post)
10826 {
10827 size_t pos;
10828 string sugs = suggest_dwarf_functions(s, q.visited_modules, tracepoint);
10829 while ((pos = sugs.find("stapprobe_")) != string::npos)
10830 sugs.erase(pos, string("stapprobe_").size());
10831 if (!sugs.empty())
ece93f53
JL
10832 throw SEMANTIC_ERROR (_NF("no match (similar tracepoint: %s)",
10833 "no match (similar tracepoints: %s)",
d906ab9d
JL
10834 sugs.find(',') == string::npos,
10835 sugs.c_str()));
10836 }
47dd066d 10837}
47dd066d 10838
e6fe60e7 10839
b55bc428 10840// ------------------------------------------------------------------------
bd2b1e68 10841// Standard tapset registry.
b55bc428
FCE
10842// ------------------------------------------------------------------------
10843
7a053d3b 10844void
f8220a7b 10845register_standard_tapsets(systemtap_session & s)
b55bc428 10846{
47e0478e 10847 register_tapset_been(s);
93646f4d 10848 register_tapset_itrace(s);
dd0e4fa7 10849 register_tapset_mark(s);
7a212aa8 10850 register_tapset_procfs(s);
912e8c59 10851 register_tapset_timers(s);
8d9609f5 10852 register_tapset_netfilter(s);
b84779a5 10853 register_tapset_utrace(s);
b98a8d73 10854
7a24d422 10855 // dwarf-based kprobe/uprobe parts
c4ce66a1 10856 dwarf_derived_probe::register_patterns(s);
30a279be 10857
888af770
FCE
10858 // XXX: user-space starter set
10859 s.pattern_root->bind_num(TOK_PROCESS)
10860 ->bind_num(TOK_STATEMENT)->bind(TOK_ABSOLUTE)
f66bb29a 10861 ->bind_privilege(pr_all)
888af770
FCE
10862 ->bind(new uprobe_builder ());
10863 s.pattern_root->bind_num(TOK_PROCESS)
10864 ->bind_num(TOK_STATEMENT)->bind(TOK_ABSOLUTE)->bind(TOK_RETURN)
f66bb29a 10865 ->bind_privilege(pr_all)
888af770
FCE
10866 ->bind(new uprobe_builder ());
10867
0a6f5a3f
JS
10868 // kernel tracepoint probes
10869 s.pattern_root->bind(TOK_KERNEL)->bind_str(TOK_TRACE)
10870 ->bind(new tracepoint_builder());
10871
e6fe60e7
AM
10872 // Kprobe based probe
10873 s.pattern_root->bind(TOK_KPROBE)->bind_str(TOK_FUNCTION)
10874 ->bind(new kprobe_builder());
3c57fe1f
JS
10875 s.pattern_root->bind(TOK_KPROBE)->bind_str(TOK_FUNCTION)->bind(TOK_CALL)
10876 ->bind(new kprobe_builder());
e6fe60e7
AM
10877 s.pattern_root->bind(TOK_KPROBE)->bind_str(TOK_MODULE)
10878 ->bind_str(TOK_FUNCTION)->bind(new kprobe_builder());
3c57fe1f
JS
10879 s.pattern_root->bind(TOK_KPROBE)->bind_str(TOK_MODULE)
10880 ->bind_str(TOK_FUNCTION)->bind(TOK_CALL)->bind(new kprobe_builder());
e6fe60e7
AM
10881 s.pattern_root->bind(TOK_KPROBE)->bind_str(TOK_FUNCTION)->bind(TOK_RETURN)
10882 ->bind(new kprobe_builder());
b6371390
JS
10883 s.pattern_root->bind(TOK_KPROBE)->bind_str(TOK_FUNCTION)->bind(TOK_RETURN)
10884 ->bind_num(TOK_MAXACTIVE)->bind(new kprobe_builder());
e6fe60e7
AM
10885 s.pattern_root->bind(TOK_KPROBE)->bind_str(TOK_MODULE)
10886 ->bind_str(TOK_FUNCTION)->bind(TOK_RETURN)->bind(new kprobe_builder());
b6371390
JS
10887 s.pattern_root->bind(TOK_KPROBE)->bind_str(TOK_MODULE)
10888 ->bind_str(TOK_FUNCTION)->bind(TOK_RETURN)
10889 ->bind_num(TOK_MAXACTIVE)->bind(new kprobe_builder());
e6fe60e7
AM
10890 s.pattern_root->bind(TOK_KPROBE)->bind_num(TOK_STATEMENT)
10891 ->bind(TOK_ABSOLUTE)->bind(new kprobe_builder());
dd225250
PS
10892
10893 //Hwbkpt based probe
b47f3a55
FCE
10894 // NB: we formerly registered the probe point types only if the kernel configuration
10895 // allowed it. However, we get better error messages if we allow probes to resolve.
10896 s.pattern_root->bind(TOK_KERNEL)->bind_num(TOK_HWBKPT)
10897 ->bind(TOK_HWBKPT_WRITE)->bind(new hwbkpt_builder());
10898 s.pattern_root->bind(TOK_KERNEL)->bind_str(TOK_HWBKPT)
10899 ->bind(TOK_HWBKPT_WRITE)->bind(new hwbkpt_builder());
10900 s.pattern_root->bind(TOK_KERNEL)->bind_num(TOK_HWBKPT)
10901 ->bind(TOK_HWBKPT_RW)->bind(new hwbkpt_builder());
10902 s.pattern_root->bind(TOK_KERNEL)->bind_str(TOK_HWBKPT)
10903 ->bind(TOK_HWBKPT_RW)->bind(new hwbkpt_builder());
10904 s.pattern_root->bind(TOK_KERNEL)->bind_num(TOK_HWBKPT)
10905 ->bind_num(TOK_LENGTH)->bind(TOK_HWBKPT_WRITE)->bind(new hwbkpt_builder());
10906 s.pattern_root->bind(TOK_KERNEL)->bind_num(TOK_HWBKPT)
10907 ->bind_num(TOK_LENGTH)->bind(TOK_HWBKPT_RW)->bind(new hwbkpt_builder());
10908 // length supported with address only, not symbol names
83ea76b1
WC
10909
10910 //perf event based probe
4763f713 10911 register_tapset_perf(s);
a29858ef 10912 register_tapset_java(s);
b55bc428 10913}
dc38c0ae
DS
10914
10915
b20febf3
FCE
10916vector<derived_probe_group*>
10917all_session_groups(systemtap_session& s)
dc38c0ae 10918{
b20febf3 10919 vector<derived_probe_group*> g;
912e8c59
JS
10920
10921#define DOONE(x) \
10922 if (s. x##_derived_probes) \
10923 g.push_back ((derived_probe_group*)(s. x##_derived_probes))
ab655cf8
DS
10924
10925 // Note that order *is* important here. We want to make sure we
10926 // register (actually run) begin probes before any other probe type
10927 // is run. Similarly, when unregistering probes, we want to
10928 // unregister (actually run) end probes after every other probe type
10929 // has be unregistered. To do the latter,
10930 // c_unparser::emit_module_exit() will run this list backwards.
b20febf3
FCE
10931 DOONE(be);
10932 DOONE(dwarf);
888af770 10933 DOONE(uprobe);
b20febf3
FCE
10934 DOONE(timer);
10935 DOONE(profile);
10936 DOONE(mark);
0a6f5a3f 10937 DOONE(tracepoint);
e6fe60e7 10938 DOONE(kprobe);
dd225250 10939 DOONE(hwbkpt);
83ea76b1 10940 DOONE(perf);
b20febf3 10941 DOONE(hrtimer);
ce82316f 10942 DOONE(procfs);
8d9609f5 10943 DOONE(netfilter);
935447c8
DS
10944
10945 // Another "order is important" item. We want to make sure we
10946 // "register" the dummy task_finder probe group after all probe
10947 // groups that use the task_finder.
10948 DOONE(utrace);
a96d1db0 10949 DOONE(itrace);
f31a77f5 10950 DOONE(dynprobe);
e7d4410d 10951 DOONE(java);
935447c8 10952 DOONE(task_finder);
b20febf3
FCE
10953#undef DOONE
10954 return g;
46b84a80 10955}
73267b89
JS
10956
10957/* vim: set sw=2 ts=8 cino=>4,n-2,{2,^-2,t0,(0,u0,w1,M1 : */
This page took 2.958552 seconds and 5 git commands to generate.