]> sourceware.org Git - systemtap.git/blame - tapsets.cxx
Allow syscall tests to match the arm64 remapped syscalls
[systemtap.git] / tapsets.cxx
CommitLineData
56e12059 1// tapset resolution
ef36f781 2// Copyright (C) 2005-2014 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>
4b1ad75e
RM
61
62#define __STDC_FORMAT_MACROS
63#include <inttypes.h>
bd2b1e68 64}
77de5e9e 65
508059f0
MW
66// Old elf.h doesn't know about this machine type.
67#ifndef EM_AARCH64
68#define EM_AARCH64 183
69#endif
70
56e12059
FCE
71
72using namespace std;
2171f774 73using namespace __gnu_cxx;
56e12059 74
47dd066d 75
b20febf3
FCE
76
77// ------------------------------------------------------------------------
faea5e16
JS
78
79string
80common_probe_init (derived_probe* p)
81{
26e63673
JS
82 assert(p->session_index != (unsigned)-1);
83 return "(&stap_probes[" + lex_cast(p->session_index) + "])";
faea5e16
JS
84}
85
86
b20febf3 87void
71db462b
DS
88common_probe_entryfn_prologue (systemtap_session& s,
89 string statestr, string probe,
90 string probe_type, bool overload_processing)
b20febf3 91{
944c9a7a 92 if (s.runtime_usermode_p())
ac34f2aa
JS
93 {
94 // If session_state() is NULL, then we haven't even initialized shm yet,
95 // and there's *nothing* for the probe to do. (even alibi is in shm)
96 // So failure skips this whole block through the end of the epilogue.
97 s.op->newline() << "if (likely(session_state())) {";
e481ab2e 98 s.op->indent(1);
ac34f2aa
JS
99 }
100
71db462b 101 s.op->newline() << "#ifdef STP_ALIBI";
7c3e97f4 102 s.op->newline() << "atomic_inc(probe_alibi(" << probe << "->index));";
71db462b 103 s.op->newline() << "#else";
653e6a9a 104
e481ab2e
JS
105 if (s.runtime_usermode_p())
106 s.op->newline() << "int _stp_saved_errno = errno;";
107
fee09ad5 108 s.op->newline() << "struct context* __restrict__ c = NULL;";
71db462b
DS
109 s.op->newline() << "#if !INTERRUPTIBLE";
110 s.op->newline() << "unsigned long flags;";
111 s.op->newline() << "#endif";
b20febf3 112
f887a8c9 113 s.op->newline() << "#ifdef STP_TIMING";
7c3e97f4 114 s.op->newline() << "Stat stat = probe_timing(" << probe << "->index);";
f887a8c9
DS
115 s.op->newline() << "#endif";
116 if (overload_processing && !s.runtime_usermode_p())
71db462b 117 s.op->newline() << "#if defined(STP_TIMING) || defined(STP_OVERLOAD)";
a58d79d0 118 else
71db462b 119 s.op->newline() << "#ifdef STP_TIMING";
b20febf3 120
f887a8c9
DS
121 if (! s.runtime_usermode_p())
122 s.op->newline() << "cycles_t cycles_atstart = get_cycles ();";
123 else
124 {
125 s.op->newline() << "struct timespec timespec_atstart;";
45639d96 126 s.op->newline() << "(void)clock_gettime(CLOCK_MONOTONIC, &timespec_atstart);";
f887a8c9 127 }
71db462b 128 s.op->newline() << "#endif";
994aac0e 129
d2d39de6 130 s.op->newline() << "#if !INTERRUPTIBLE";
71db462b
DS
131 s.op->newline() << "local_irq_save (flags);";
132 s.op->newline() << "#endif";
b20febf3 133
ac3af990 134 if (! s.runtime_usermode_p())
71db462b
DS
135 {
136 // Check for enough free enough stack space
137 s.op->newline() << "if (unlikely ((((unsigned long) (& c)) & (THREAD_SIZE-1))"; // free space
138 s.op->newline(1) << "< (MINSTACKSPACE + sizeof (struct thread_info)))) {"; // needed space
139 // XXX: may need porting to platforms where task_struct is not
140 // at bottom of kernel stack NB: see also
141 // CONFIG_DEBUG_STACKOVERFLOW
065d5567 142 s.op->newline() << "atomic_inc (skipped_count());";
71db462b 143 s.op->newline() << "#ifdef STP_TIMING";
065d5567 144 s.op->newline() << "atomic_inc (skipped_count_lowstack());";
71db462b
DS
145 s.op->newline() << "#endif";
146 s.op->newline() << "goto probe_epilogue;";
147 s.op->newline(-1) << "}";
148 }
c931ec8a 149
065d5567 150 s.op->newline() << "if (atomic_read (session_state()) != " << statestr << ")";
71db462b
DS
151 s.op->newline(1) << "goto probe_epilogue;";
152 s.op->indent(-1);
9a604fac 153
1d0e697d 154 s.op->newline() << "c = _stp_runtime_entryfn_get_context();";
d2d39de6 155 s.op->newline() << "if (!c) {";
71db462b 156 s.op->newline(1) << "#if !INTERRUPTIBLE";
065d5567 157 s.op->newline() << "atomic_inc (skipped_count());";
71db462b
DS
158 s.op->newline() << "#endif";
159 s.op->newline() << "#ifdef STP_TIMING";
065d5567 160 s.op->newline() << "atomic_inc (skipped_count_reentrant());";
71db462b 161 s.op->newline() << "#endif";
71db462b
DS
162 s.op->newline() << "goto probe_epilogue;";
163 s.op->newline(-1) << "}";
d2d39de6 164
71db462b
DS
165 s.op->newline();
166 s.op->newline() << "c->last_stmt = 0;";
167 s.op->newline() << "c->last_error = 0;";
168 s.op->newline() << "c->nesting = -1;"; // NB: PR10516 packs locals[] tighter
169 s.op->newline() << "c->uregs = 0;";
170 s.op->newline() << "c->kregs = 0;";
171 s.op->newline() << "#if defined __ia64__";
172 s.op->newline() << "c->unwaddr = 0;";
173 s.op->newline() << "#endif";
f4d70a33
JS
174 if (s.runtime_usermode_p())
175 s.op->newline() << "c->probe_index = " << probe << "->index;";
71db462b
DS
176 s.op->newline() << "c->probe_point = " << probe << "->pp;";
177 s.op->newline() << "#ifdef STP_NEED_PROBE_NAME";
178 s.op->newline() << "c->probe_name = " << probe << "->pn;";
179 s.op->newline() << "#endif";
180 s.op->newline() << "c->probe_type = " << probe_type << ";";
6dceb5c9 181 // reset Individual Probe State union
71db462b 182 s.op->newline() << "memset(&c->ips, 0, sizeof(c->ips));";
e04b5d74 183 s.op->newline() << "c->user_mode_p = 0; c->full_uregs_p = 0;";
71db462b
DS
184 s.op->newline() << "#ifdef STAP_NEED_REGPARM"; // i386 or x86_64 register.stp
185 s.op->newline() << "c->regparm = 0;";
186 s.op->newline() << "#endif";
e0a17418 187
152fa051
LB
188 if(!s.suppress_time_limits){
189 s.op->newline() << "#if INTERRUPTIBLE";
190 s.op->newline() << "c->actionremaining = MAXACTION_INTERRUPTIBLE;";
191 s.op->newline() << "#else";
192 s.op->newline() << "c->actionremaining = MAXACTION;";
193 s.op->newline() << "#endif";
194 }
9915575b
FCE
195 // NB: The following would actually be incorrect.
196 // That's because cycles_sum/cycles_base values are supposed to survive
197 // between consecutive probes. Periodically (STP_OVERLOAD_INTERVAL
198 // cycles), the values will be reset.
199 /*
71db462b
DS
200 s.op->newline() << "#ifdef STP_OVERLOAD";
201 s.op->newline() << "c->cycles_sum = 0;";
202 s.op->newline() << "c->cycles_base = 0;";
203 s.op->newline() << "#endif";
9915575b 204 */
44cfbe25
SM
205
206 s.op->newline() << "#if defined(STP_NEED_UNWIND_DATA)";
207 s.op->newline() << "c->uwcache_user.state = uwcache_uninitialized;";
208 s.op->newline() << "c->uwcache_kernel.state = uwcache_uninitialized;";
209 s.op->newline() << "#endif";
b20febf3 210}
9a604fac 211
a44a0785 212
b20febf3 213void
f887a8c9 214common_probe_entryfn_epilogue (systemtap_session& s,
ef1337ee
JL
215 bool overload_processing,
216 bool schedule_work_safe)
b20febf3 217{
ef1337ee
JL
218 if (!s.runtime_usermode_p()
219 && schedule_work_safe)
220 {
221 // If a refresh is required, we can safely schedule_work() here
222 s.op->newline( 0) << "if (atomic_cmpxchg(&need_module_refresh, 1, 0) == 1)";
223 s.op->newline(+1) << "schedule_work(&module_refresher_work);";
224 s.op->indent(-1);
225 }
226
f887a8c9
DS
227 if (overload_processing && !s.runtime_usermode_p())
228 s.op->newline() << "#if defined(STP_TIMING) || defined(STP_OVERLOAD)";
a58d79d0 229 else
f887a8c9
DS
230 s.op->newline() << "#ifdef STP_TIMING";
231 s.op->newline() << "{";
232 s.op->indent(1);
233 if (! s.runtime_usermode_p())
234 {
235 s.op->newline() << "cycles_t cycles_atend = get_cycles ();";
236 // NB: we truncate cycles counts to 32 bits. Perhaps it should be
237 // fewer, if the hardware counter rolls over really quickly. We
238 // handle 32-bit wraparound here.
239 s.op->newline() << "int32_t cycles_elapsed = ((int32_t)cycles_atend > (int32_t)cycles_atstart)";
240 s.op->newline(1) << "? ((int32_t)cycles_atend - (int32_t)cycles_atstart)";
241 s.op->newline() << ": (~(int32_t)0) - (int32_t)cycles_atstart + (int32_t)cycles_atend + 1;";
242 s.op->indent(-1);
243 }
244 else
245 {
246 s.op->newline() << "struct timespec timespec_atend, timespec_elapsed;";
247 s.op->newline() << "long cycles_elapsed;";
45639d96 248 s.op->newline() << "(void)clock_gettime(CLOCK_MONOTONIC, &timespec_atend);";
f887a8c9
DS
249 s.op->newline() << "_stp_timespec_sub(&timespec_atend, &timespec_atstart, &timespec_elapsed);";
250 // 'cycles_elapsed' is really elapsed nanoseconds
251 s.op->newline() << "cycles_elapsed = (timespec_elapsed.tv_sec * NSEC_PER_SEC) + timespec_elapsed.tv_nsec;";
252 }
253
254 s.op->newline() << "#ifdef STP_TIMING";
255 s.op->newline() << "if (likely (stat)) _stp_stat_add(stat, cycles_elapsed);";
256 s.op->newline() << "#endif";
257
258 if (overload_processing && !s.runtime_usermode_p())
259 {
260 s.op->newline() << "#ifdef STP_OVERLOAD";
261 s.op->newline() << "{";
a58d79d0
DS
262 // If the cycle count has wrapped (cycles_atend > cycles_base),
263 // let's go ahead and pretend the interval has been reached.
264 // This should reset cycles_base and cycles_sum.
f887a8c9
DS
265 s.op->newline(1) << "cycles_t interval = (cycles_atend > c->cycles_base)";
266 s.op->newline(1) << "? (cycles_atend - c->cycles_base)";
267 s.op->newline() << ": (STP_OVERLOAD_INTERVAL + 1);";
268 s.op->newline(-1) << "c->cycles_sum += cycles_elapsed;";
a58d79d0
DS
269
270 // If we've spent more than STP_OVERLOAD_THRESHOLD cycles in a
271 // probe during the last STP_OVERLOAD_INTERVAL cycles, the probe
272 // has overloaded the system and we need to quit.
7baf48e9
FCE
273 // NB: this is not suppressible via --suppress-runtime-errors,
274 // because this is a system safety metric that we cannot trust
275 // unprivileged users to override.
f887a8c9
DS
276 s.op->newline() << "if (interval > STP_OVERLOAD_INTERVAL) {";
277 s.op->newline(1) << "if (c->cycles_sum > STP_OVERLOAD_THRESHOLD) {";
278 s.op->newline(1) << "_stp_error (\"probe overhead exceeded threshold\");";
065d5567
JS
279 s.op->newline() << "atomic_set (session_state(), STAP_SESSION_ERROR);";
280 s.op->newline() << "atomic_inc (error_count());";
f887a8c9
DS
281 s.op->newline(-1) << "}";
282
283 s.op->newline() << "c->cycles_base = cycles_atend;";
284 s.op->newline() << "c->cycles_sum = 0;";
285 s.op->newline(-1) << "}";
286 s.op->newline(-1) << "}";
287 s.op->newline() << "#endif";
a58d79d0 288 }
e57b735a 289
f887a8c9
DS
290 s.op->newline(-1) << "}";
291 s.op->newline() << "#endif";
e57b735a 292
f887a8c9
DS
293 s.op->newline() << "c->probe_point = 0;"; // vacated
294 s.op->newline() << "#ifdef STP_NEED_PROBE_NAME";
295 s.op->newline() << "c->probe_name = 0;";
296 s.op->newline() << "#endif";
297 s.op->newline() << "c->probe_type = 0;";
7baf48e9
FCE
298
299
f887a8c9
DS
300 s.op->newline() << "if (unlikely (c->last_error && c->last_error[0])) {";
301 s.op->indent(1);
302 if (s.suppress_handler_errors) // PR 13306
b78a0fbb 303 {
065d5567 304 s.op->newline() << "atomic_inc (error_count());";
7baf48e9
FCE
305 }
306 else
307 {
f887a8c9
DS
308 s.op->newline() << "if (c->last_stmt != NULL)";
309 s.op->newline(1) << "_stp_softerror (\"%s near %s\", c->last_error, c->last_stmt);";
310 s.op->newline(-1) << "else";
311 s.op->newline(1) << "_stp_softerror (\"%s\", c->last_error);";
312 s.op->indent(-1);
065d5567
JS
313 s.op->newline() << "atomic_inc (error_count());";
314 s.op->newline() << "if (atomic_read (error_count()) > MAXERRORS) {";
315 s.op->newline(1) << "atomic_set (session_state(), STAP_SESSION_ERROR);";
f887a8c9
DS
316 s.op->newline() << "_stp_exit ();";
317 s.op->newline(-1) << "}";
7baf48e9
FCE
318 }
319
f887a8c9 320 s.op->newline(-1) << "}";
7baf48e9
FCE
321
322
f887a8c9
DS
323 s.op->newline(-1) << "probe_epilogue:"; // context is free
324 s.op->indent(1);
e57b735a 325
f887a8c9 326 if (! s.suppress_handler_errors) // PR 13306
7baf48e9
FCE
327 {
328 // Check for excessive skip counts.
065d5567
JS
329 s.op->newline() << "if (unlikely (atomic_read (skipped_count()) > MAXSKIPPED)) {";
330 s.op->newline(1) << "if (unlikely (pseudo_atomic_cmpxchg(session_state(), STAP_SESSION_RUNNING, STAP_SESSION_ERROR) == STAP_SESSION_RUNNING))";
f887a8c9
DS
331 s.op->newline() << "_stp_error (\"Skipped too many probes, check MAXSKIPPED or try again with stap -t for more details.\");";
332 s.op->newline(-1) << "}";
7baf48e9 333 }
e57b735a 334
d2d39de6
JS
335 // We mustn't release the context until after all _stp_error(), so dyninst
336 // mode can still access the log buffers stored therein.
337 s.op->newline() << "_stp_runtime_entryfn_put_context(c);";
338
339 s.op->newline() << "#if !INTERRUPTIBLE";
f887a8c9
DS
340 s.op->newline() << "local_irq_restore (flags);";
341 s.op->newline() << "#endif";
653e6a9a 342
944c9a7a 343 if (s.runtime_usermode_p())
ac34f2aa
JS
344 {
345 s.op->newline() << "errno = _stp_saved_errno;";
ac34f2aa 346 }
e481ab2e
JS
347
348 s.op->newline() << "#endif // STP_ALIBI";
349
350 if (s.runtime_usermode_p())
351 s.op->newline(-1) << "}";
440f755a 352}
e57b735a 353
e57b735a 354
440f755a 355// ------------------------------------------------------------------------
e57b735a 356
440f755a
JS
357// ------------------------------------------------------------------------
358// Dwarf derived probes. "We apologize for the inconvience."
359// ------------------------------------------------------------------------
e57b735a 360
4627ed58
JS
361static const string TOK_KERNEL("kernel");
362static const string TOK_MODULE("module");
363static const string TOK_FUNCTION("function");
364static const string TOK_INLINE("inline");
365static const string TOK_CALL("call");
4bda987e 366static const string TOK_EXPORTED("exported");
4627ed58
JS
367static const string TOK_RETURN("return");
368static const string TOK_MAXACTIVE("maxactive");
369static const string TOK_STATEMENT("statement");
370static const string TOK_ABSOLUTE("absolute");
371static const string TOK_PROCESS("process");
a794dbeb 372static const string TOK_PROVIDER("provider");
4627ed58
JS
373static const string TOK_MARK("mark");
374static const string TOK_TRACE("trace");
375static const string TOK_LABEL("label");
63b4fd14 376static const string TOK_LIBRARY("library");
576eaefe 377static const string TOK_PLT("plt");
e7d4410d
LB
378static const string TOK_METHOD("method");
379static const string TOK_CLASS("class");;
c31add50
JL
380static const string TOK_CALLEE("callee");;
381static const string TOK_CALLEES("callees");;
83eeb3ac 382static const string TOK_NEAREST("nearest");;
e57b735a 383
440f755a
JS
384// Can we handle this query with just symbol-table info?
385enum dbinfo_reqt
386{
387 dbr_unknown,
388 dbr_none, // kernel.statement(NUM).absolute
389 dbr_need_symtab, // can get by with symbol table if there's no dwarf
390 dbr_need_dwarf
391};
e57b735a 392
20e4a32c 393
673d0add 394struct dwarf_query; // forward decl
20e4a32c 395
45582cbc
JL
396static int query_cu (Dwarf_Die * cudie, dwarf_query *q);
397static void query_addr(Dwarf_Addr addr, dwarf_query *q);
398static void query_plt_statement(dwarf_query *q);
a781f401 399
440f755a
JS
400struct
401symbol_table
402{
403 module_info *mod_info; // associated module
404 map<string, func_info*> map_by_name;
1c6b77e5 405 multimap<Dwarf_Addr, func_info*> map_by_addr;
2a4acb09
JL
406 map<string, Dwarf_Addr> globals;
407 map<string, Dwarf_Addr> locals;
1c6b77e5 408 typedef multimap<Dwarf_Addr, func_info*>::iterator iterator_t;
440f755a
JS
409 typedef pair<iterator_t, iterator_t> range_t;
410#ifdef __powerpc__
411 GElf_Word opd_section;
412#endif
2867a2a1
JS
413 void add_symbol(const char *name, bool weak, bool descriptor,
414 Dwarf_Addr addr, Dwarf_Addr *high_addr);
440f755a
JS
415 enum info_status get_from_elf();
416 void prepare_section_rejection(Dwfl_Module *mod);
417 bool reject_section(GElf_Word section);
440f755a
JS
418 void purge_syscall_stubs();
419 func_info *lookup_symbol(const string& name);
420 Dwarf_Addr lookup_symbol_address(const string& name);
421 func_info *get_func_containing_address(Dwarf_Addr addr);
3d372d6b 422 func_info *get_first_func();
7a053d3b 423
440f755a
JS
424 symbol_table(module_info *mi) : mod_info(mi) {}
425 ~symbol_table();
426};
77de5e9e 427
440f755a
JS
428static bool null_die(Dwarf_Die *die)
429{
822a6a3d 430 static Dwarf_Die null;
440f755a
JS
431 return (!die || !memcmp(die, &null, sizeof(null)));
432}
c4ce66a1 433
40a393cd
JS
434struct exp_type_dwarf : public exp_type_details
435{
436 // NB: We don't own this dwflpp, so don't use it after build_no_more!
437 // A shared_ptr might help, but expressions are currently so leaky
438 // that we'd probably never clear all references... :/
439 dwflpp* dw;
440 Dwarf_Die die;
66facfa3 441 bool userspace_p;
37efef73
JS
442 bool is_pointer;
443 exp_type_dwarf(dwflpp* dw, Dwarf_Die* die, bool userspace_p, bool addressof);
40a393cd 444 uintptr_t id () const { return reinterpret_cast<uintptr_t>(die.addr); }
eb79eb0a 445 bool expandable() const { return true; }
66facfa3 446 functioncall *expand(autocast_op* e, bool lvalue);
40a393cd
JS
447};
448
c4ce66a1 449
7a053d3b 450enum
bd2b1e68 451function_spec_type
7a053d3b 452 {
bd2b1e68
GH
453 function_alone,
454 function_and_file,
7a053d3b 455 function_file_and_line
bd2b1e68
GH
456 };
457
ec4373ff 458
bd2b1e68 459struct dwarf_builder;
f10534c6 460struct dwarf_var_expanding_visitor;
77de5e9e 461
2930abc7 462
b20febf3
FCE
463// XXX: This class is a candidate for subclassing to separate
464// the relocation vs non-relocation variants. Likewise for
465// kprobe vs kretprobe variants.
466
467struct dwarf_derived_probe: public derived_probe
b55bc428 468{
b20febf3
FCE
469 dwarf_derived_probe (const string& function,
470 const string& filename,
471 int line,
472 const string& module,
473 const string& section,
474 Dwarf_Addr dwfl_addr,
2930abc7 475 Dwarf_Addr addr,
b20febf3
FCE
476 dwarf_query & q,
477 Dwarf_Die* scope_die);
20e4a32c 478
b20febf3
FCE
479 string module;
480 string section;
481 Dwarf_Addr addr;
63b4fd14 482 string path;
27dc09b1 483 bool has_process;
2930abc7 484 bool has_return;
c9bad430 485 bool has_maxactive;
63b4fd14 486 bool has_library;
c9bad430 487 long maxactive_val;
4ad95bbc 488 // dwarf_derived_probe_group::emit_module_decls uses this to emit sdt kprobe definition
b642c901
SC
489 string user_path;
490 string user_lib;
b95e2b79 491 bool access_vars;
2930abc7 492
af234c40
JS
493 unsigned saved_longs, saved_strings;
494 dwarf_derived_probe* entry_handler;
495
b8da0ad1 496 void printsig (std::ostream &o) const;
6b66b9f7 497 virtual void join_group (systemtap_session& s);
3689db05 498 void emit_probe_local_init(systemtap_session& s, translator_output * o);
d0bfd2ac 499 void getargs(std::list<std::string> &arg_set) const;
0a98fd42 500
42e38653 501 void emit_privilege_assertion (translator_output*);
27dc09b1
DB
502 void print_dupe_stamp(ostream& o);
503
bd2b1e68 504 // Pattern registration helpers.
7a053d3b 505 static void register_statement_variants(match_node * root,
27dc09b1 506 dwarf_builder * dw,
42e38653 507 privilege_t privilege);
fd6602a0 508 static void register_function_variants(match_node * root,
27dc09b1 509 dwarf_builder * dw,
42e38653 510 privilege_t privilege);
440d9b00
DB
511 static void register_function_and_statement_variants(systemtap_session& s,
512 match_node * root,
27dc09b1 513 dwarf_builder * dw,
42e38653 514 privilege_t privilege);
b1615c74
JS
515 static void register_sdt_variants(systemtap_session& s,
516 match_node * root,
517 dwarf_builder * dw);
518 static void register_plt_variants(systemtap_session& s,
519 match_node * root,
520 dwarf_builder * dw);
c4ce66a1 521 static void register_patterns(systemtap_session& s);
6b66b9f7
JS
522
523protected:
524 dwarf_derived_probe(probe *base,
525 probe_point *location,
526 Dwarf_Addr addr,
527 bool has_return):
74fe61bc
LB
528 derived_probe(base, location), addr(addr), has_process(0),
529 has_return(has_return), has_maxactive(0), has_library(0),
530 maxactive_val(0), access_vars(false), saved_longs(0),
531 saved_strings(0), entry_handler(0)
6b66b9f7
JS
532 {}
533
534private:
d0bfd2ac 535 list<string> args;
8c67c337 536 void saveargs(dwarf_query& q, Dwarf_Die* scope_die, Dwarf_Addr dwfl_addr);
20c6c071
GH
537};
538
dc38c0ae 539
6b66b9f7 540struct uprobe_derived_probe: public dwarf_derived_probe
6d0f3f0c 541{
6d0f3f0c 542 int pid; // 0 => unrestricted
0973d815 543
6d0f3f0c
FCE
544 uprobe_derived_probe (const string& function,
545 const string& filename,
546 int line,
547 const string& module,
6d0f3f0c
FCE
548 const string& section,
549 Dwarf_Addr dwfl_addr,
550 Dwarf_Addr addr,
551 dwarf_query & q,
d11f62b7 552 Dwarf_Die* scope_die);
6d0f3f0c 553
0973d815
FCE
554 // alternate constructor for process(PID).statement(ADDR).absolute
555 uprobe_derived_probe (probe *base,
556 probe_point *location,
557 int pid,
558 Dwarf_Addr addr,
6b66b9f7
JS
559 bool has_return):
560 dwarf_derived_probe(base, location, addr, has_return), pid(pid)
561 {}
9ace370f 562
6d0f3f0c 563 void join_group (systemtap_session& s);
2865d17a 564
42e38653 565 void emit_privilege_assertion (translator_output*);
8f6d8c2b 566 void print_dupe_stamp(ostream& o) { print_dupe_stamp_unprivileged_process_owner (o); }
c0f84e7b
SC
567 void getargs(std::list<std::string> &arg_set) const;
568 void saveargs(int nargs);
569private:
570 list<string> args;
6d0f3f0c
FCE
571};
572
dc38c0ae
DS
573struct dwarf_derived_probe_group: public derived_probe_group
574{
575private:
b20febf3
FCE
576 multimap<string,dwarf_derived_probe*> probes_by_module;
577 typedef multimap<string,dwarf_derived_probe*>::iterator p_b_m_iterator;
dc38c0ae
DS
578
579public:
08b5a50c 580 dwarf_derived_probe_group() {}
b20febf3
FCE
581 void enroll (dwarf_derived_probe* probe);
582 void emit_module_decls (systemtap_session& s);
583 void emit_module_init (systemtap_session& s);
b4be7cbc 584 void emit_module_refresh (systemtap_session& s);
b20febf3 585 void emit_module_exit (systemtap_session& s);
ca6d3b0f
JL
586 bool otf_supported (systemtap_session& s) { return true; }
587
588 // workqueue handling not safe in kprobes context
589 bool otf_safe_context (systemtap_session& s) { return false; }
dc38c0ae
DS
590};
591
20c6c071 592// Helper struct to thread through the dwfl callbacks.
2c384610 593struct base_query
20c6c071 594{
c4ce66a1
JS
595 base_query(dwflpp & dw, literal_map_t const & params);
596 base_query(dwflpp & dw, const string & module_val);
2c384610 597 virtual ~base_query() {}
bd2b1e68 598
5227f1ea 599 systemtap_session & sess;
2c384610 600 dwflpp & dw;
5227f1ea 601
070764c0
JL
602 // Used to keep track of which modules were visited during
603 // iterate_over_modules()
604 set<string> visited_modules;
605
bd2b1e68 606 // Parameter extractors.
86bf665e 607 static bool has_null_param(literal_map_t const & params,
888af770 608 string const & k);
86bf665e 609 static bool get_string_param(literal_map_t const & params,
bd2b1e68 610 string const & k, string & v);
86bf665e 611 static bool get_number_param(literal_map_t const & params,
bd2b1e68 612 string const & k, long & v);
7e28cf76
WC
613 static bool get_number_param(literal_map_t const & params,
614 string const & k, long long & v);
86bf665e 615 static bool get_number_param(literal_map_t const & params,
c239d28c 616 string const & k, Dwarf_Addr & v);
bbbc7241 617 static void query_library_callback (base_query *me, const char *data);
45cdb40e 618 static void query_plt_callback (base_query *me, const char *link, size_t addr);
f301a9ff 619 virtual void query_library (const char *data) = 0;
576eaefe 620 virtual void query_plt (const char *link, size_t addr) = 0;
84c84ac4 621
b55bc428 622
2c384610
DS
623 // Extracted parameters.
624 bool has_kernel;
91af0778
FCE
625 bool has_module;
626 bool has_process;
63b4fd14 627 bool has_library;
576eaefe
SC
628 bool has_plt;
629 bool has_statement;
2c384610 630 string module_val; // has_kernel => module_val = "kernel"
63b4fd14 631 string path; // executable path if module is a .so
576eaefe 632 string plt_val; // has_plt => plt wildcard
ff8bd809 633 int64_t pid_val;
2c384610
DS
634
635 virtual void handle_query_module() = 0;
636};
637
c4ce66a1 638base_query::base_query(dwflpp & dw, literal_map_t const & params):
c88bcbe8
AJ
639 sess(dw.sess), dw(dw), has_library(false), has_plt(false), has_statement(false),
640 pid_val(0)
2c384610 641{
91af0778 642 has_kernel = has_null_param (params, TOK_KERNEL);
2c384610
DS
643 if (has_kernel)
644 module_val = "kernel";
91af0778
FCE
645
646 has_module = get_string_param (params, TOK_MODULE, module_val);
647 if (has_module)
648 has_process = false;
4baf0e53 649 else
d0a7f5a9 650 {
63b4fd14 651 string library_name;
576eaefe 652 long statement_num_val;
37635b45 653 has_process = derived_probe_builder::has_param(params, TOK_PROCESS);
63b4fd14 654 has_library = get_string_param (params, TOK_LIBRARY, library_name);
576eaefe
SC
655 if ((has_plt = has_null_param (params, TOK_PLT)))
656 plt_val = "*";
657 else has_plt = get_string_param (params, TOK_PLT, plt_val);
658 has_statement = get_number_param(params, TOK_STATEMENT, statement_num_val);
659
84c84ac4 660 if (has_process)
84c84ac4 661 {
c88bcbe8 662 if (get_number_param(params, TOK_PROCESS, pid_val))
37635b45
AJ
663 {
664 // check that the pid given corresponds to a running process
a03a3744
JS
665 string pid_err_msg;
666 if (!is_valid_pid(pid_val, pid_err_msg))
667 throw SEMANTIC_ERROR(pid_err_msg);
668
c88bcbe8 669 string pid_path = string("/proc/") + lex_cast(pid_val) + "/exe";
37635b45
AJ
670 module_val = sess.sysroot + pid_path;
671 }
c88bcbe8
AJ
672 else
673 {
674 // reset the pid_val in case anything weird got written into it
675 pid_val = 0;
37635b45 676 get_string_param(params, TOK_PROCESS, module_val);
c88bcbe8 677 }
5fa0d811
JL
678 module_val = find_executable (module_val, sess.sysroot, sess.sysenv);
679 if (!is_fully_resolved(module_val, sess.sysroot, sess.sysenv))
680 throw SEMANTIC_ERROR(_F("cannot find executable '%s'",
681 module_val.c_str()));
682 }
dbe9d133
JL
683
684 // Library probe? Let's target that instead if it is fully resolved (such
685 // as what query_one_library() would have done for us). Otherwise, we
686 // resort to iterate_over_libraries().
687 if (has_library && is_fully_resolved(library_name, sess.sysroot, sess.sysenv,
688 "LD_LIBRARY_PATH"))
84c84ac4 689 {
dbe9d133
JL
690 path = path_remove_sysroot(sess, module_val);
691 module_val = library_name;
84c84ac4 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 699 : sess(dw.sess), dw(dw), has_library(false), has_plt(false), has_statement(false),
c88bcbe8 700 module_val(module_val), pid_val(0)
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
7e28cf76
WC
744bool
745base_query::get_number_param(literal_map_t const & params,
746 string const & k, long long & v)
747{
748 int64_t value;
749 bool present = derived_probe_builder::get_param (params, k, value);
750 v = (long) value;
751 return present;
752}
753
754
2c384610 755bool
86bf665e 756base_query::get_number_param(literal_map_t const & params,
2c384610
DS
757 string const & k, Dwarf_Addr & v)
758{
759 int64_t value;
760 bool present = derived_probe_builder::get_param (params, k, value);
761 v = (Dwarf_Addr) value;
762 return present;
763}
764
2c384610
DS
765struct dwarf_query : public base_query
766{
e1278bd4 767 dwarf_query(probe * base_probe,
2c384610
DS
768 probe_point * base_loc,
769 dwflpp & dw,
86bf665e 770 literal_map_t const & params,
b642c901
SC
771 vector<derived_probe *> & results,
772 const string user_path,
773 const string user_lib);
2c384610 774
c4ce66a1 775 vector<derived_probe *> & results;
8f14e444 776 set<string> inlined_non_returnable; // function names
c4ce66a1
JS
777 probe * base_probe;
778 probe_point * base_loc;
b642c901
SC
779 string user_path;
780 string user_lib;
c4ce66a1 781
614f0fcf
JL
782 set<string> visited_libraries;
783 bool resolved_library;
784
2c384610 785 virtual void handle_query_module();
5f0a03a6
JK
786 void query_module_dwarf();
787 void query_module_symtab();
5d5bd369 788 void query_library (const char *data);
576eaefe 789 void query_plt (const char *entry, size_t addr);
2c384610 790
2930abc7
FCE
791 void add_probe_point(string const & funcname,
792 char const * filename,
793 int line,
794 Dwarf_Die *scope_die,
795 Dwarf_Addr addr);
36f9dd1d 796
613c8675
JL
797 void mount_well_formed_probe_point();
798 void unmount_well_formed_probe_point();
799 stack<pair<probe_point*, probe*> > previous_bases;
800
71dfce11
JL
801 void replace_probe_point_component_arg(const string& functor,
802 const string& new_functor,
803 int64_t new_arg,
804 bool hex = false);
805 void replace_probe_point_component_arg(const string& functor,
806 int64_t new_arg,
807 bool hex = false);
808 void replace_probe_point_component_arg(const string& functor,
809 const string& new_functor,
810 const string& new_arg);
811 void replace_probe_point_component_arg(const string& functor,
812 const string& new_arg);
813 void remove_probe_point_component(const string& functor);
814
857bdfd1
JS
815 // Track addresses we've already seen in a given module
816 set<Dwarf_Addr> alias_dupes;
817
7fdd3e2c
JS
818 // Track inlines we've already seen as well
819 // NB: this can't be compared just by entrypc, as inlines can overlap
820 set<inline_instance_info> inline_dupes;
821
e2941743
JL
822 // Used in .callee[s] probes, when calling iterate_over_callees() (which
823 // provides the actual stack). Retains the addrs of the callers unwind addr
824 // where the callee is found. Specifies multiple callers. E.g. when a callee
825 // at depth 2 is found, callers[1] has the addr of the caller, and callers[0]
826 // has the addr of the caller's caller.
827 stack<Dwarf_Addr> *callers;
828
20c6c071
GH
829 bool has_function_str;
830 bool has_statement_str;
831 bool has_function_num;
832 bool has_statement_num;
7a053d3b
RM
833 string statement_str_val;
834 string function_str_val;
c239d28c
GH
835 Dwarf_Addr statement_num_val;
836 Dwarf_Addr function_num_val;
20c6c071 837
b8da0ad1 838 bool has_call;
4bda987e 839 bool has_exported;
b8da0ad1 840 bool has_inline;
20c6c071
GH
841 bool has_return;
842
83eeb3ac
HK
843 bool has_nearest;
844
c9bad430
DS
845 bool has_maxactive;
846 long maxactive_val;
847
20c6c071
GH
848 bool has_label;
849 string label_val;
850
c31add50
JL
851 bool has_callee;
852 string callee_val;
853
854 bool has_callees_num;
855 long callees_num_val;
856
20c6c071
GH
857 bool has_relative;
858 long relative_val;
859
37ebca01
FCE
860 bool has_absolute;
861
467bea43
SC
862 bool has_mark;
863
5f0a03a6
JK
864 enum dbinfo_reqt dbinfo_reqt;
865 enum dbinfo_reqt assess_dbinfo_reqt();
866
7d6d0afc 867 void parse_function_spec(const string & spec);
20c6c071 868 function_spec_type spec_type;
7d6d0afc 869 vector<string> scopes;
20c6c071
GH
870 string function;
871 string file;
4a81c0d9 872 lineno_t lineno_type;
f01d4ffb 873 vector<int> linenos;
5f0a03a6 874 bool query_done; // Found exact match
20c6c071 875
5a617dc6
JL
876 // Holds the prologue end of the current function
877 Dwarf_Addr prologue_end;
878
bd25380d 879 set<string> filtered_srcfiles;
7e1279ea
FCE
880
881 // Map official entrypc -> func_info object
86bf665e
TM
882 inline_instance_map_t filtered_inlines;
883 func_info_map_t filtered_functions;
4df79aaf 884
959ccfed
JL
885 // Helper when we want to iterate over both
886 base_func_info_map_t filtered_all();
887
4df79aaf 888 void query_module_functions ();
e772a6e7
JL
889
890 string final_function_name(const string& final_func,
79954c0f 891 const char* final_file,
e772a6e7 892 int final_line);
c5142c66
JL
893
894 bool is_fully_specified_function();
b55bc428
FCE
895};
896
98afd80e 897
d11f62b7
AJ
898uprobe_derived_probe::uprobe_derived_probe (const string& function,
899 const string& filename,
900 int line,
901 const string& module,
902 const string& section,
903 Dwarf_Addr dwfl_addr,
904 Dwarf_Addr addr,
905 dwarf_query & q,
906 Dwarf_Die* scope_die):
907 dwarf_derived_probe(function, filename, line, module, section,
908 dwfl_addr, addr, q, scope_die), pid(q.pid_val)
909 {}
435f53a7
FCE
910
911
ff8bd809 912static void delete_session_module_cache (systemtap_session& s); // forward decl
435f53a7 913
98afd80e 914struct dwarf_builder: public derived_probe_builder
b55bc428 915{
665e1256 916 map <string,dwflpp*> kern_dw; /* NB: key string could be a wildcard */
7a24d422 917 map <string,dwflpp*> user_dw;
b642c901
SC
918 string user_path;
919 string user_lib;
44ffe90c
JL
920
921 // Holds modules to suggest functions from. NB: aggregates over
922 // recursive calls to build() when deriving globby probes.
923 set <string> modules_seen;
924
ae2552da 925 dwarf_builder() {}
aa30ccd3 926
ae2552da 927 dwflpp *get_kern_dw(systemtap_session& sess, const string& module)
707bf35e 928 {
ea14cf67
FCE
929 if (kern_dw[module] == 0)
930 kern_dw[module] = new dwflpp(sess, module, true); // might throw
ae2552da 931 return kern_dw[module];
707bf35e
JS
932 }
933
934 dwflpp *get_user_dw(systemtap_session& sess, const string& module)
935 {
ea14cf67
FCE
936 if (user_dw[module] == 0)
937 user_dw[module] = new dwflpp(sess, module, false); // might throw
707bf35e
JS
938 return user_dw[module];
939 }
7a24d422
FCE
940
941 /* NB: not virtual, so can be called from dtor too: */
822a6a3d 942 void dwarf_build_no_more (bool)
aa30ccd3 943 {
435f53a7
FCE
944 delete_map(kern_dw);
945 delete_map(user_dw);
7a24d422
FCE
946 }
947
948 void build_no_more (systemtap_session &s)
949 {
950 dwarf_build_no_more (s.verbose > 3);
435f53a7 951 delete_session_module_cache (s);
aa30ccd3
FCE
952 }
953
e38d6504
RM
954 ~dwarf_builder()
955 {
7a24d422 956 dwarf_build_no_more (false);
c8959a29 957 }
aa30ccd3 958
5227f1ea 959 virtual void build(systemtap_session & sess,
7a053d3b 960 probe * base,
20c6c071 961 probe_point * location,
86bf665e 962 literal_map_t const & parameters,
20c6c071 963 vector<derived_probe *> & finished_results);
b55bc428
FCE
964};
965
5111fc3e 966
e1278bd4 967dwarf_query::dwarf_query(probe * base_probe,
20c6c071
GH
968 probe_point * base_loc,
969 dwflpp & dw,
86bf665e 970 literal_map_t const & params,
b642c901
SC
971 vector<derived_probe *> & results,
972 const string user_path,
973 const string user_lib)
e2941743
JL
974 : base_query(dw, params), results(results), base_probe(base_probe),
975 base_loc(base_loc), user_path(user_path), user_lib(user_lib),
614f0fcf
JL
976 resolved_library(false), callers(NULL), has_relative(false),
977 relative_val(0), prologue_end(0)
bd2b1e68
GH
978{
979 // Reduce the query to more reasonable semantic values (booleans,
980 // extracted strings, numbers, etc).
bd2b1e68
GH
981 has_function_str = get_string_param(params, TOK_FUNCTION, function_str_val);
982 has_function_num = get_number_param(params, TOK_FUNCTION, function_num_val);
983
984 has_statement_str = get_string_param(params, TOK_STATEMENT, statement_str_val);
985 has_statement_num = get_number_param(params, TOK_STATEMENT, statement_num_val);
986
0f336e95 987 has_label = get_string_param(params, TOK_LABEL, label_val);
c31add50
JL
988 has_callee = get_string_param(params, TOK_CALLEE, callee_val);
989 if (has_null_param(params, TOK_CALLEES))
990 { // .callees ==> .callees(1) (also equivalent to .callee("*"))
991 has_callees_num = true;
992 callees_num_val = 1;
993 }
994 else
995 {
996 has_callees_num = get_number_param(params, TOK_CALLEES, callees_num_val);
997 if (has_callees_num && callees_num_val < 1)
998 throw SEMANTIC_ERROR(_(".callees(N) only acceptable for N >= 1"),
999 base_probe->tok);
1000 }
0f336e95 1001
b8da0ad1 1002 has_call = has_null_param(params, TOK_CALL);
4bda987e 1003 has_exported = has_null_param(params, TOK_EXPORTED);
b8da0ad1 1004 has_inline = has_null_param(params, TOK_INLINE);
bd2b1e68 1005 has_return = has_null_param(params, TOK_RETURN);
83eeb3ac 1006 has_nearest = has_null_param(params, TOK_NEAREST);
c9bad430 1007 has_maxactive = get_number_param(params, TOK_MAXACTIVE, maxactive_val);
37ebca01 1008 has_absolute = has_null_param(params, TOK_ABSOLUTE);
467bea43 1009 has_mark = false;
37ebca01 1010
bd2b1e68 1011 if (has_function_str)
7d6d0afc 1012 parse_function_spec(function_str_val);
bd2b1e68 1013 else if (has_statement_str)
7d6d0afc 1014 parse_function_spec(statement_str_val);
0daad364 1015
5f0a03a6
JK
1016 dbinfo_reqt = assess_dbinfo_reqt();
1017 query_done = false;
0daad364
JS
1018}
1019
1020
2c384610 1021void
5f0a03a6 1022dwarf_query::query_module_dwarf()
2c384610
DS
1023{
1024 if (has_function_num || has_statement_num)
1025 {
1026 // If we have module("foo").function(0xbeef) or
1027 // module("foo").statement(0xbeef), the address is relative
1028 // to the start of the module, so we seek the function
1029 // number plus the module's bias.
6b517475
JS
1030 Dwarf_Addr addr = has_function_num ?
1031 function_num_val : statement_num_val;
08d1d520
MW
1032
1033 // These are raw addresses, we need to know what the elf_bias
1034 // is to feed it to libdwfl based functions.
1035 Dwarf_Addr elf_bias;
1036 Elf *elf = dwfl_module_getelf (dw.module, &elf_bias);
1037 assert(elf);
1038 addr += elf_bias;
6b517475 1039 query_addr(addr, this);
2c384610
DS
1040 }
1041 else
1042 {
1043 // Otherwise if we have a function("foo") or statement("foo")
1044 // specifier, we have to scan over all the CUs looking for
1045 // the function(s) in question
1046 assert(has_function_str || has_statement_str);
4df79aaf
JS
1047
1048 // For simple cases, no wildcard and no source:line, we can do a very
1049 // quick function lookup in a module-wide cache.
1ffb8bd1
JS
1050 if (spec_type == function_alone &&
1051 !dw.name_has_wildcard(function) &&
1052 !startswith(function, "_Z"))
4df79aaf
JS
1053 query_module_functions();
1054 else
337b7c44 1055 dw.iterate_over_cus(&query_cu, this, false);
2c384610
DS
1056 }
1057}
1058
5f0a03a6
JK
1059static void query_func_info (Dwarf_Addr entrypc, func_info & fi,
1060 dwarf_query * q);
1061
91bb9081
LB
1062static void
1063query_symtab_func_info (func_info & fi, dwarf_query * q)
1064{
1065 assert(null_die(&fi.die));
1066
1067 Dwarf_Addr addr = fi.addr;
1068
1069 // Now compensate for the dw bias because the addresses come
1070 // from dwfl_module_symtab, so fi->addr is NOT a normal dw address.
1071 q->dw.get_module_dwarf(false, false);
1072 addr -= q->dw.module_bias;
1073
1074 // If there are already probes in this module, lets not duplicate.
1075 // This can come from other weak symbols/aliases or existing
a41f2ad7
JS
1076 // matches from Dwarf DIE functions. Try to add this addr to the
1077 // collection, and only continue if it was new.
1078 if (q->alias_dupes.insert(addr).second)
1079 query_func_info(addr, fi, q);
91bb9081
LB
1080}
1081
5f0a03a6
JK
1082void
1083dwarf_query::query_module_symtab()
1084{
1085 // Get the symbol table if it's necessary, sufficient, and not already got.
1086 if (dbinfo_reqt == dbr_need_dwarf)
1087 return;
1088
1089 module_info *mi = dw.mod_info;
1090 if (dbinfo_reqt == dbr_need_symtab)
1091 {
1092 if (mi->symtab_status == info_unknown)
5f52fafe 1093 mi->get_symtab();
5f0a03a6
JK
1094 if (mi->symtab_status == info_absent)
1095 return;
1096 }
1097
1098 func_info *fi = NULL;
1099 symbol_table *sym_table = mi->sym_table;
1100
1101 if (has_function_str)
1102 {
1103 // Per dwarf_query::assess_dbinfo_reqt()...
1104 assert(spec_type == function_alone);
1105 if (dw.name_has_wildcard(function_str_val))
1106 {
2e67a43b 1107 symbol_table::iterator_t iter;
1c6b77e5
JS
1108 for (iter = sym_table->map_by_addr.begin();
1109 iter != sym_table->map_by_addr.end();
2e67a43b 1110 ++iter)
5f0a03a6 1111 {
1c6b77e5 1112 fi = iter->second;
53625401
JS
1113 if (!null_die(&fi->die) // already handled in query_module_dwarf()
1114 || fi->descriptor) // ppc opd (and also undefined symbols)
1115 continue;
5f0a03a6 1116 if (dw.function_name_matches_pattern(fi->name, function_str_val))
91bb9081 1117 query_symtab_func_info(*fi, this);
5f0a03a6
JK
1118 }
1119 }
1120 else
1121 {
1122 fi = sym_table->lookup_symbol(function_str_val);
2867a2a1 1123 if (fi && !fi->descriptor && null_die(&fi->die))
91bb9081 1124 query_symtab_func_info(*fi, this);
5f0a03a6 1125 }
5f0a03a6
JK
1126 }
1127}
1128
1129void
1130dwarf_query::handle_query_module()
1131{
f4faaf86
JS
1132 if (has_plt && has_statement_num)
1133 {
1134 query_plt_statement (this);
1135 return;
1136 }
1137
0035051e 1138 bool report = dbinfo_reqt == dbr_need_dwarf;
1c6b77e5
JS
1139 dw.get_module_dwarf(false, report);
1140
1141 // prebuild the symbol table to resolve aliases
5f52fafe 1142 dw.mod_info->get_symtab();
1c6b77e5 1143
857bdfd1
JS
1144 // reset the dupe-checking for each new module
1145 alias_dupes.clear();
7fdd3e2c 1146 inline_dupes.clear();
857bdfd1 1147
5f0a03a6
JK
1148 if (dw.mod_info->dwarf_status == info_present)
1149 query_module_dwarf();
1c6b77e5 1150
91bb9081
LB
1151 // Consult the symbol table, asm and weak functions can show up
1152 // in the symbol table but not in dwarf and minidebuginfo is
1153 // located in the gnu_debugdata section, alias_dupes checking
1154 // is done before adding any probe points
1155 if (!query_done && !pending_interrupts)
5f0a03a6
JK
1156 query_module_symtab();
1157}
1158
2c384610 1159
7d6d0afc
JS
1160void
1161dwarf_query::parse_function_spec(const string & spec)
bd2b1e68 1162{
4a81c0d9 1163 lineno_type = ABSOLUTE;
f01d4ffb 1164 size_t src_pos, line_pos, scope_pos;
bd2b1e68 1165
7d6d0afc 1166 // look for named scopes
91699a70
JS
1167 scope_pos = spec.rfind("::");
1168 if (scope_pos != string::npos)
bd2b1e68 1169 {
91699a70
JS
1170 tokenize_cxx(spec.substr(0, scope_pos), scopes);
1171 scope_pos += 2;
bd2b1e68 1172 }
91699a70
JS
1173 else
1174 scope_pos = 0;
bd2b1e68 1175
7d6d0afc
JS
1176 // look for a source separator
1177 src_pos = spec.find('@', scope_pos);
1178 if (src_pos == string::npos)
bd2b1e68 1179 {
7d6d0afc
JS
1180 function = spec.substr(scope_pos);
1181 spec_type = function_alone;
bd2b1e68 1182 }
7d6d0afc 1183 else
879eb9e9 1184 {
7d6d0afc 1185 function = spec.substr(scope_pos, src_pos - scope_pos);
7a053d3b 1186
7d6d0afc
JS
1187 // look for a line-number separator
1188 line_pos = spec.find_first_of(":+", src_pos);
1189 if (line_pos == string::npos)
1190 {
1191 file = spec.substr(src_pos + 1);
1192 spec_type = function_and_file;
1193 }
1194 else
1195 {
1196 file = spec.substr(src_pos + 1, line_pos - src_pos - 1);
1197
1198 // classify the line spec
1199 spec_type = function_file_and_line;
1200 if (spec[line_pos] == '+')
4a81c0d9 1201 lineno_type = RELATIVE;
7d6d0afc
JS
1202 else if (spec[line_pos + 1] == '*' &&
1203 spec.length() == line_pos + 2)
4a81c0d9 1204 lineno_type = WILDCARD;
7d6d0afc 1205 else
4a81c0d9 1206 lineno_type = ABSOLUTE;
7d6d0afc 1207
4a81c0d9 1208 if (lineno_type != WILDCARD)
7d6d0afc
JS
1209 try
1210 {
f01d4ffb
BC
1211 // try to parse N, N-M, or N,M,O,P, or combination thereof...
1212 if (spec.find_first_of(",-", line_pos + 1) != string::npos)
1213 {
1214 lineno_type = ENUMERATED;
1215 vector<string> sub_specs;
1216 tokenize(spec.substr(line_pos + 1), sub_specs, ",");
1217 vector<string>::const_iterator line_spec;
1218 for (line_spec = sub_specs.begin(); line_spec != sub_specs.end(); ++line_spec)
1219 {
1220 vector<string> ranges;
1221 tokenize(*line_spec, ranges, "-");
1222 if (ranges.size() > 1)
1223 for (int i = lex_cast<int>(ranges.front()); i <= lex_cast<int>(ranges.back()); i++)
1224 linenos.push_back(i);
1225 else
1226 linenos.push_back(lex_cast<int>(ranges.at(0)));
1227 }
1228 sort(linenos.begin(), linenos.end());
1229 }
7d6d0afc
JS
1230 else
1231 {
f01d4ffb
BC
1232 linenos.push_back(lex_cast<int>(spec.substr(line_pos + 1)));
1233 linenos.push_back(lex_cast<int>(spec.substr(line_pos + 1)));
7d6d0afc
JS
1234 }
1235 }
1236 catch (runtime_error & exn)
1237 {
1238 goto bad;
1239 }
2ff28684
JL
1240
1241 // only allow .nearest with ABSOLUTE and RELATIVE linenos
1242 if (has_nearest && lineno_type != ABSOLUTE
1243 && lineno_type != RELATIVE)
1244 throw SEMANTIC_ERROR(_(".nearest is only valid with absolute or relative line numbers"));
7d6d0afc 1245 }
bd2b1e68
GH
1246 }
1247
7d6d0afc
JS
1248 if (function.empty() ||
1249 (spec_type != function_alone && file.empty()))
bd2b1e68
GH
1250 goto bad;
1251
7d6d0afc 1252 if (sess.verbose > 2)
bd2b1e68 1253 {
b530b5b3
LB
1254 //clog << "parsed '" << spec << "'";
1255 clog << _F("parse '%s'", spec.c_str());
41c262f3 1256
7d6d0afc
JS
1257 if (!scopes.empty())
1258 clog << ", scope '" << scopes[0] << "'";
1259 for (unsigned i = 1; i < scopes.size(); ++i)
1260 clog << "::'" << scopes[i] << "'";
41c262f3 1261
7d6d0afc
JS
1262 clog << ", func '" << function << "'";
1263
1264 if (spec_type != function_alone)
1265 clog << ", file '" << file << "'";
1266
1267 if (spec_type == function_file_and_line)
1268 {
1269 clog << ", line ";
4a81c0d9 1270 switch (lineno_type)
7d6d0afc
JS
1271 {
1272 case ABSOLUTE:
4a81c0d9 1273 clog << linenos[0];
7d6d0afc
JS
1274 break;
1275
1276 case RELATIVE:
4a81c0d9 1277 clog << "+" << linenos[0];
7d6d0afc
JS
1278 break;
1279
f01d4ffb
BC
1280 case ENUMERATED:
1281 {
10e6fe03 1282 vector<int>::const_iterator linenos_it;
f01d4ffb
BC
1283 for (linenos_it = linenos.begin(); linenos_it != linenos.end(); ++linenos_it)
1284 {
1285 vector<int>::const_iterator range_it(linenos_it);
1286 while ((range_it+1) != linenos.end() && *range_it + 1 == *(range_it+1))
1287 ++range_it;
1288 if (linenos_it == range_it)
1289 clog << *linenos_it;
1290 else
1291 clog << *linenos_it << "-" << *range_it;
1292 if (range_it + 1 != linenos.end())
1293 clog << ",";
1294 linenos_it = range_it;
1295 }
1296 }
7d6d0afc
JS
1297 break;
1298
1299 case WILDCARD:
1300 clog << "*";
1301 break;
1302 }
1303 }
1304
1305 clog << endl;
bd2b1e68
GH
1306 }
1307
7d6d0afc
JS
1308 return;
1309
1310bad:
dc09353a 1311 throw SEMANTIC_ERROR(_F("malformed specification '%s'", spec.c_str()),
7d6d0afc 1312 base_probe->tok);
bd2b1e68
GH
1313}
1314
05fb3e0c
WF
1315string path_remove_sysroot(const systemtap_session& sess, const string& path)
1316{
1317 size_t pos;
1318 string retval = path;
1319 if (!sess.sysroot.empty() &&
1320 (pos = retval.find(sess.sysroot)) != string::npos)
1321 retval.replace(pos, sess.sysroot.length(), "/");
1322 return retval;
1323}
bd2b1e68 1324
36f9dd1d 1325void
1ffb8bd1 1326dwarf_query::add_probe_point(const string& dw_funcname,
b20febf3 1327 const char* filename,
36f9dd1d 1328 int line,
b20febf3 1329 Dwarf_Die* scope_die,
36f9dd1d
FCE
1330 Dwarf_Addr addr)
1331{
b20febf3 1332 string reloc_section; // base section for relocation purposes
27646582 1333 Dwarf_Addr reloc_addr; // relocated
b20febf3 1334 const string& module = dw.module_name; // "kernel" or other
1ffb8bd1 1335 string funcname = dw_funcname;
36f9dd1d 1336
37ebca01
FCE
1337 assert (! has_absolute); // already handled in dwarf_builder::build()
1338
f4faaf86 1339 reloc_addr = dw.relocate_address(addr, reloc_section);
2930abc7 1340
1ffb8bd1 1341 // If we originally used the linkage name, then let's call it that way
1ffb8bd1 1342 const char* linkage_name;
91bb9081 1343 if (!null_die(scope_die) && startswith (this->function, "_Z")
f450a7e3 1344 && (linkage_name = dwarf_linkage_name (scope_die)))
1ffb8bd1
JS
1345 funcname = linkage_name;
1346
7f9f3386
FCE
1347 if (sess.verbose > 1)
1348 {
b530b5b3 1349 clog << _("probe ") << funcname << "@" << filename << ":" << line;
b20febf3 1350 if (string(module) == TOK_KERNEL)
b530b5b3 1351 clog << _(" kernel");
91af0778 1352 else if (has_module)
b530b5b3 1353 clog << _(" module=") << module;
91af0778 1354 else if (has_process)
b530b5b3 1355 clog << _(" process=") << module;
b20febf3 1356 if (reloc_section != "") clog << " reloc=" << reloc_section;
b20febf3 1357 clog << " pc=0x" << hex << addr << dec;
7f9f3386 1358 }
4baf0e53 1359
2f18e69b
JL
1360 dwflpp::blacklisted_type blacklisted = dw.blacklisted_p (funcname, filename,
1361 line, module, addr,
1362 has_return);
b20febf3
FCE
1363 if (sess.verbose > 1)
1364 clog << endl;
7f9f3386 1365
84048984
FCE
1366 if (module == TOK_KERNEL)
1367 {
1368 // PR 4224: adapt to relocatable kernel by subtracting the _stext address here.
1369 reloc_addr = addr - sess.sym_stext;
37ebca01 1370 reloc_section = "_stext"; // a message to runtime's _stp_module_relocate
84048984
FCE
1371 }
1372
2f18e69b 1373 if (!blacklisted)
b20febf3 1374 {
1a0dbc5a 1375 sess.unwindsym_modules.insert (module);
6d0f3f0c
FCE
1376
1377 if (has_process)
1378 {
05fb3e0c 1379 string module_tgt = path_remove_sysroot(sess, module);
6d0f3f0c 1380 results.push_back (new uprobe_derived_probe(funcname, filename, line,
05fb3e0c 1381 module_tgt, reloc_section, addr, reloc_addr,
6d0f3f0c
FCE
1382 *this, scope_die));
1383 }
1384 else
1385 {
1386 assert (has_kernel || has_module);
1387 results.push_back (new dwarf_derived_probe(funcname, filename, line,
06aca46a 1388 module, reloc_section, addr, reloc_addr,
6d0f3f0c
FCE
1389 *this, scope_die));
1390 }
b20febf3 1391 }
2f18e69b
JL
1392 else
1393 {
1394 switch (blacklisted)
1395 {
1396 case dwflpp::blacklisted_section:
1397 sess.print_warning(_F("function %s is in blacklisted section",
1398 funcname.c_str()), base_probe->tok);
1399 break;
1400 case dwflpp::blacklisted_kprobes:
1401 sess.print_warning(_F("kprobes function %s is blacklisted",
1402 funcname.c_str()), base_probe->tok);
1403 break;
1404 case dwflpp::blacklisted_function_return:
1405 sess.print_warning(_F("function %s return probe is blacklisted",
1406 funcname.c_str()), base_probe->tok);
1407 break;
1408 case dwflpp::blacklisted_file:
1409 sess.print_warning(_F("function %s is in blacklisted file",
1410 funcname.c_str()), base_probe->tok);
1411 break;
1412 case dwflpp::blacklisted_function:
1413 default:
1414 sess.print_warning(_F("function %s is blacklisted",
1415 funcname.c_str()), base_probe->tok);
1416 break;
1417 }
1418 }
2930abc7
FCE
1419}
1420
613c8675
JL
1421void
1422dwarf_query::mount_well_formed_probe_point()
1423{
1424 string module = dw.module_name;
1425 if (has_process)
1426 module = path_remove_sysroot(sess, module);
1427
1428 vector<probe_point::component*> comps;
1429 vector<probe_point::component*>::iterator it;
1430 for (it = base_loc->components.begin();
1431 it != base_loc->components.end(); ++it)
1432 {
1433 if ((*it)->functor == TOK_PROCESS || (*it)->functor == TOK_MODULE)
1434 comps.push_back(new probe_point::component((*it)->functor,
1435 new literal_string(has_library ? path : module)));
1436 else
1437 comps.push_back(*it);
1438 }
1439
1440 probe_point *pp = new probe_point(*base_loc);
1441 pp->well_formed = true;
1442 pp->components = comps;
1443
1444 previous_bases.push(make_pair(base_loc, base_probe));
1445
1446 base_loc = pp;
1447 base_probe = new probe(base_probe, pp);
1448}
1449
1450void
1451dwarf_query::unmount_well_formed_probe_point()
1452{
1453 assert(!previous_bases.empty());
1454
1455 base_loc = previous_bases.top().first;
1456 base_probe = previous_bases.top().second;
1457
1458 previous_bases.pop();
1459}
1460
71dfce11
JL
1461void
1462dwarf_query::replace_probe_point_component_arg(const string& functor,
1463 const string& new_functor,
1464 int64_t new_arg,
1465 bool hex)
1466{
1467 // only allow these operations if we're editing the well-formed loc
1468 assert(!previous_bases.empty());
1469
1470 vector<probe_point::component*>::iterator it;
1471 for (it = base_loc->components.begin();
1472 it != base_loc->components.end(); ++it)
1473 if ((*it)->functor == functor)
1474 *it = new probe_point::component(new_functor,
1475 new literal_number(new_arg, hex));
1476}
1477
1478void
1479dwarf_query::replace_probe_point_component_arg(const string& functor,
1480 int64_t new_arg,
1481 bool hex)
1482{
1483 replace_probe_point_component_arg(functor, functor, new_arg, hex);
1484}
1485
1486void
1487dwarf_query::replace_probe_point_component_arg(const string& functor,
1488 const string& new_functor,
1489 const string& new_arg)
1490{
1491 // only allow these operations if we're editing the well-formed loc
1492 assert(!previous_bases.empty());
1493
1494 vector<probe_point::component*>::iterator it;
1495 for (it = base_loc->components.begin();
1496 it != base_loc->components.end(); ++it)
1497 if ((*it)->functor == functor)
1498 *it = new probe_point::component(new_functor,
1499 new literal_string(new_arg));
1500}
1501
1502void
1503dwarf_query::replace_probe_point_component_arg(const string& functor,
1504 const string& new_arg)
1505{
1506 replace_probe_point_component_arg(functor, functor, new_arg);
1507}
1508
1509void
1510dwarf_query::remove_probe_point_component(const string& functor)
1511{
1512 // only allow these operations if we're editing the well-formed loc
1513 assert(!previous_bases.empty());
1514
1515 vector<probe_point::component*> new_comps;
1516 vector<probe_point::component*>::iterator it;
1517 for (it = base_loc->components.begin();
1518 it != base_loc->components.end(); ++it)
1519 if ((*it)->functor != functor)
1520 new_comps.push_back(*it);
1521
1522 base_loc->components = new_comps;
2930abc7
FCE
1523}
1524
5f0a03a6
JK
1525enum dbinfo_reqt
1526dwarf_query::assess_dbinfo_reqt()
1527{
1528 if (has_absolute)
1529 {
1530 // kernel.statement(NUM).absolute
1531 return dbr_none;
1532 }
c31add50 1533 if (has_inline || has_label || has_callee || has_callees_num)
5f0a03a6
JK
1534 {
1535 // kernel.function("f").inline or module("m").function("f").inline
1536 return dbr_need_dwarf;
1537 }
1538 if (has_function_str && spec_type == function_alone)
1539 {
1540 // kernel.function("f") or module("m").function("f")
1541 return dbr_need_symtab;
1542 }
1543 if (has_statement_num)
1544 {
1545 // kernel.statement(NUM) or module("m").statement(NUM)
1546 // Technically, all we need is the module offset (or _stext, for
1547 // the kernel). But for that we need either the ELF file or (for
1548 // _stext) the symbol table. In either case, the symbol table
1549 // is available, and that allows us to map the NUM (address)
1550 // to a function, which is goodness.
1551 return dbr_need_symtab;
1552 }
1553 if (has_function_num)
1554 {
1555 // kernel.function(NUM) or module("m").function(NUM)
1556 // Need the symbol table so we can back up from NUM to the
1557 // start of the function.
1558 return dbr_need_symtab;
1559 }
1560 // Symbol table tells us nothing about source files or line numbers.
1561 return dbr_need_dwarf;
1562}
2930abc7 1563
e772a6e7
JL
1564string
1565dwarf_query::final_function_name(const string& final_func,
79954c0f 1566 const char* final_file,
e772a6e7
JL
1567 int final_line)
1568{
1569 string final_name = final_func;
79954c0f 1570 if (final_file && *final_file != '\0')
e772a6e7 1571 {
79954c0f 1572 final_name += ("@" + string(final_file));
e772a6e7
JL
1573 if (final_line > 0)
1574 final_name += (":" + lex_cast(final_line));
1575 }
1576 return final_name;
1577}
2930abc7 1578
c5142c66
JL
1579bool
1580dwarf_query::is_fully_specified_function()
1581{
1582 // A fully specified function is one that was given using a .function() probe
1583 // by full name (no wildcards), and specific srcfile and decl_line.
1584 return (has_function_str
1585 && spec_type == function_file_and_line
1586 && !dw.name_has_wildcard(function)
1587 && filtered_srcfiles.size() == 1
1588 && !filtered_functions.empty()
1589 && lineno_type == ABSOLUTE
1590 && filtered_functions[0].decl_line == linenos[0]);
1591}
1592
959ccfed
JL
1593base_func_info_map_t
1594dwarf_query::filtered_all(void)
1595{
c5142c66 1596 base_func_info_map_t r;
959ccfed
JL
1597 func_info_map_t::const_iterator f;
1598 for (f = filtered_functions.begin();
1599 f != filtered_functions.end(); ++f)
1600 r.push_back(*f);
1601 inline_instance_map_t::const_iterator i;
1602 for (i = filtered_inlines.begin();
1603 i != filtered_inlines.end(); ++i)
1604 r.push_back(*i);
1605 return r;
1606}
1607
b8da0ad1
FCE
1608// The critical determining factor when interpreting a pattern
1609// string is, perhaps surprisingly: "presence of a lineno". The
1610// presence of a lineno changes the search strategy completely.
1611//
1612// Compare the two cases:
1613//
1614// 1. {statement,function}(foo@file.c:lineno)
1615// - find the files matching file.c
1616// - in each file, find the functions matching foo
1617// - query the file for line records matching lineno
1618// - iterate over the line records,
1619// - and iterate over the functions,
1620// - if(haspc(function.DIE, line.addr))
1621// - if looking for statements: probe(lineno.addr)
1622// - if looking for functions: probe(function.{entrypc,return,etc.})
1623//
1624// 2. {statement,function}(foo@file.c)
1625// - find the files matching file.c
1626// - in each file, find the functions matching foo
1627// - probe(function.{entrypc,return,etc.})
1628//
1629// Thus the first decision we make is based on the presence of a
1630// lineno, and we enter entirely different sets of callbacks
1631// depending on that decision.
1632//
1633// Note that the first case is a generalization fo the second, in that
1634// we could theoretically search through line records for matching
1635// file names (a "table scan" in rdbms lingo). Luckily, file names
1636// are already cached elsewhere, so we can do an "index scan" as an
1637// optimization.
7e1279ea 1638
bd2b1e68 1639static void
4cd232e4 1640query_statement (string const & func,
20e4a32c 1641 char const * file,
4cd232e4 1642 int line,
bcc12710 1643 Dwarf_Die *scope_die,
20e4a32c 1644 Dwarf_Addr stmt_addr,
4cd232e4 1645 dwarf_query * q)
bd2b1e68 1646{
39bcd429
FCE
1647 try
1648 {
cee35f73 1649 q->add_probe_point(func, file ? file : "",
a9b2f3a5 1650 line, scope_die, stmt_addr);
39bcd429
FCE
1651 }
1652 catch (const semantic_error& e)
1653 {
1654 q->sess.print_error (e);
1655 }
bd2b1e68
GH
1656}
1657
6b517475
JS
1658static void
1659query_addr(Dwarf_Addr addr, dwarf_query *q)
1660{
7a71819c
JL
1661 assert(q->has_function_num || q->has_statement_num);
1662
6b517475
JS
1663 dwflpp &dw = q->dw;
1664
08d1d520
MW
1665 if (q->sess.verbose > 2)
1666 clog << "query_addr 0x" << hex << addr << dec << endl;
6b517475
JS
1667
1668 // First pick which CU contains this address
1669 Dwarf_Die* cudie = dw.query_cu_containing_address(addr);
1670 if (!cudie) // address could be wildly out of range
1671 return;
1672 dw.focus_on_cu(cudie);
1673
1674 // Now compensate for the dw bias
1675 addr -= dw.module_bias;
1676
1677 // Per PR5787, we look up the scope die even for
1678 // statement_num's, for blacklist sensitivity and $var
1679 // resolution purposes.
1680
1681 // Find the scopes containing this address
1682 vector<Dwarf_Die> scopes = dw.getscopes(addr);
1683 if (scopes.empty())
1684 return;
1685
1686 // Look for the innermost containing function
1687 Dwarf_Die *fnscope = NULL;
1688 for (size_t i = 0; i < scopes.size(); ++i)
1689 {
1690 int tag = dwarf_tag(&scopes[i]);
1691 if ((tag == DW_TAG_subprogram && !q->has_inline) ||
1692 (tag == DW_TAG_inlined_subroutine &&
4bda987e 1693 !q->has_call && !q->has_return && !q->has_exported))
6b517475
JS
1694 {
1695 fnscope = &scopes[i];
1696 break;
1697 }
1698 }
1699 if (!fnscope)
1700 return;
1701 dw.focus_on_function(fnscope);
1702
1703 Dwarf_Die *scope = q->has_function_num ? fnscope : &scopes[0];
1704
1705 const char *file = dwarf_decl_file(fnscope);
1706 int line;
1707 dwarf_decl_line(fnscope, &line);
1708
1709 // Function probes should reset the addr to the function entry
1710 // and possibly perform prologue searching
1711 if (q->has_function_num)
1712 {
1bbf3f90
JS
1713 if (!dw.die_entrypc(fnscope, &addr))
1714 return;
6b517475 1715 if (dwarf_tag(fnscope) == DW_TAG_subprogram &&
b5b9879f
JL
1716 (q->sess.prologue_searching ||
1717 (q->has_process && !q->dw.has_valid_locs()))) // PR 6871 && PR 6941
6b517475
JS
1718 {
1719 func_info func;
1720 func.die = *fnscope;
1721 func.name = dw.function_name;
1722 func.decl_file = file;
1723 func.decl_line = line;
1724 func.entrypc = addr;
1725
1726 func_info_map_t funcs(1, func);
1727 dw.resolve_prologue_endings (funcs);
5a617dc6
JL
1728 q->prologue_end = funcs[0].prologue_end;
1729
1730 // PR13200: if it's a .return probe, we need to emit a *retprobe based
1731 // on the entrypc so here we only use prologue_end for non .return
1732 // probes (note however that .return probes still take advantage of
1733 // prologue_end: PR14436)
1734 if (!q->has_return)
1735 addr = funcs[0].prologue_end;
6b517475
JS
1736 }
1737 }
1738 else
1739 {
de797211
JL
1740 Dwarf_Line *address_line = dwarf_getsrc_die(cudie, addr);
1741 Dwarf_Addr address_line_addr = addr;
6b517475
JS
1742 if (address_line)
1743 {
bf5974d6
JL
1744 file = DWARF_LINESRC(address_line);
1745 line = DWARF_LINENO(address_line);
1746 address_line_addr = DWARF_LINEADDR(address_line);
6b517475
JS
1747 }
1748
1749 // Verify that a raw address matches the beginning of a
1750 // statement. This is a somewhat lame check that the address
1751 // is at the start of an assembly instruction. Mark probes are in the
1752 // middle of a macro and thus not strictly at a statement beginning.
1753 // Guru mode may override this check.
de797211 1754 if (!q->has_mark && (!address_line || address_line_addr != addr))
6b517475
JS
1755 {
1756 stringstream msg;
2a97f50b 1757 msg << _F("address %#" PRIx64 " does not match the beginning of a statement",
b530b5b3 1758 addr);
6b517475 1759 if (address_line)
de797211 1760 msg << _F(" (try %#" PRIx64 ")", address_line_addr);
6b517475 1761 else
b530b5b3
LB
1762 msg << _F(" (no line info found for '%s', in module '%s')",
1763 dw.cu_name().c_str(), dw.module_name.c_str());
6b517475 1764 if (! q->sess.guru_mode)
dc09353a 1765 throw SEMANTIC_ERROR(msg.str());
2713ea24 1766 else
6b517475
JS
1767 q->sess.print_warning(msg.str());
1768 }
1769 }
1770
7a71819c
JL
1771 // We're ready to build a probe, but before, we need to create the final,
1772 // well-formed version of this location with all the components filled in
1773 q->mount_well_formed_probe_point();
1774 q->replace_probe_point_component_arg(TOK_FUNCTION, addr, true /* hex */ );
1775 q->replace_probe_point_component_arg(TOK_STATEMENT, addr, true /* hex */ );
1776
6b517475
JS
1777 // Build a probe at this point
1778 query_statement(dw.function_name, file, line, scope, addr, q);
7a71819c
JL
1779
1780 q->unmount_well_formed_probe_point();
6b517475
JS
1781}
1782
f4faaf86
JS
1783static void
1784query_plt_statement(dwarf_query *q)
1785{
1786 assert (q->has_plt && q->has_statement_num);
1787
1788 Dwarf_Addr addr = q->statement_num_val;
1789 if (q->sess.verbose > 2)
1790 clog << "query_plt_statement 0x" << hex << addr << dec << endl;
1791
1792 // First adjust the raw address to dwfl's elf bias.
1793 Dwarf_Addr elf_bias;
1794 Elf *elf = dwfl_module_getelf (q->dw.module, &elf_bias);
1795 assert(elf);
1796 addr += elf_bias;
1797
1798 // Now compensate for the dw bias
1799 q->dw.get_module_dwarf(false, false);
1800 addr -= q->dw.module_bias;
1801
93b44c2a
JL
1802 // Create the final well-formed probe point
1803 q->mount_well_formed_probe_point();
1804 q->replace_probe_point_component_arg(TOK_STATEMENT, q->statement_num_val, true /* hex */ );
1805
1806 // We remove the .plt part here, since if the user provided a .plt probe, then
1807 // the higher-level probe point is already well-formed. On the other hand, if
1808 // the user provides a .plt(PATTERN).statement(0xABCD), the PATTERN is
1809 // irrelevant (we won't iterate over plts) so just take it out.
1810 q->remove_probe_point_component(TOK_PLT);
1811
f4faaf86
JS
1812 // Build a probe at this point
1813 query_statement(q->plt_val, NULL, -1, NULL, addr, q);
93b44c2a
JL
1814
1815 q->unmount_well_formed_probe_point();
f4faaf86
JS
1816}
1817
8096dd7d 1818static void
600d39f1 1819query_label (const base_func_info& func,
8096dd7d
JS
1820 char const * label,
1821 char const * file,
1822 int line,
1823 Dwarf_Die *scope_die,
1824 Dwarf_Addr stmt_addr,
1825 dwarf_query * q)
1826{
6b517475
JS
1827 assert (q->has_statement_str || q->has_function_str);
1828
8096dd7d
JS
1829 // weed out functions whose decl_file isn't one of
1830 // the source files that we actually care about
6b517475 1831 if (q->spec_type != function_alone &&
8096dd7d
JS
1832 q->filtered_srcfiles.count(file) == 0)
1833 return;
1834
cdabbe86 1835 // Create the final well-formed probe
600d39f1 1836 string canon_func = q->final_function_name(func.name, file, line);
9891c543 1837
cdabbe86
JL
1838 q->mount_well_formed_probe_point();
1839 q->replace_probe_point_component_arg(TOK_FUNCTION, canon_func);
1840 q->replace_probe_point_component_arg(TOK_LABEL, label);
8096dd7d 1841
600d39f1 1842 query_statement(func.name, file, line, scope_die, stmt_addr, q);
9891c543 1843
cdabbe86 1844 q->unmount_well_formed_probe_point();
8096dd7d
JS
1845}
1846
67959c62 1847static void
e772a6e7
JL
1848query_callee (base_func_info& callee,
1849 base_func_info& caller,
67959c62
JL
1850 stack<Dwarf_Addr> *callers,
1851 dwarf_query * q)
1852{
1853 assert (q->has_function_str);
e772a6e7
JL
1854 assert (q->has_callee || q->has_callees_num);
1855
1856 // OK, we found a callee for a targeted caller. To help users see the
04a89a2a 1857 // derivation, we add the well-formed form .function(caller).callee(callee).
e772a6e7 1858
e772a6e7
JL
1859 string canon_caller = q->final_function_name(caller.name, caller.decl_file,
1860 caller.decl_line);
1861 string canon_callee = q->final_function_name(callee.name, callee.decl_file,
1862 callee.decl_line);
1863
04a89a2a
JL
1864 q->mount_well_formed_probe_point();
1865 q->replace_probe_point_component_arg(TOK_FUNCTION, canon_caller);
1866 q->replace_probe_point_component_arg(TOK_CALLEES, TOK_CALLEE, canon_callee);
1867 q->replace_probe_point_component_arg(TOK_CALLEE, canon_callee);
e772a6e7
JL
1868
1869 // Pass on the callers we'll need to add checks for
04a89a2a 1870 q->callers = callers;
e772a6e7
JL
1871
1872 query_statement(callee.name, callee.decl_file, callee.decl_line,
04a89a2a
JL
1873 &callee.die, callee.entrypc, q);
1874
1875 q->unmount_well_formed_probe_point();
67959c62
JL
1876}
1877
7e1279ea 1878static void
3e961ba6 1879query_inline_instance_info (inline_instance_info & ii,
7e1279ea
FCE
1880 dwarf_query * q)
1881{
b6581717 1882 try
7e1279ea 1883 {
8f14e444 1884 assert (! q->has_return); // checked by caller already
8debd4e7
JL
1885 assert (q->has_function_str || q->has_statement_str);
1886
8f14e444 1887 if (q->sess.verbose>2)
b530b5b3
LB
1888 clog << _F("querying entrypc %#" PRIx64 " of instance of inline '%s'\n",
1889 ii.entrypc, ii.name.c_str());
8debd4e7
JL
1890
1891 string canon_func = q->final_function_name(ii.name, ii.decl_file,
1892 ii.decl_line);
1893
1894 q->mount_well_formed_probe_point();
1895 q->replace_probe_point_component_arg(TOK_FUNCTION, canon_func);
1896 q->replace_probe_point_component_arg(TOK_STATEMENT, canon_func);
1897
8f14e444
FCE
1898 query_statement (ii.name, ii.decl_file, ii.decl_line,
1899 &ii.die, ii.entrypc, q);
8debd4e7
JL
1900
1901 q->unmount_well_formed_probe_point();
7e1279ea 1902 }
b6581717 1903 catch (semantic_error &e)
7e1279ea 1904 {
b6581717 1905 q->sess.print_error (e);
7e1279ea
FCE
1906 }
1907}
1908
1909static void
1910query_func_info (Dwarf_Addr entrypc,
bcc12710 1911 func_info & fi,
7e1279ea
FCE
1912 dwarf_query * q)
1913{
e5745975
JL
1914 assert(q->has_function_str || q->has_statement_str);
1915
b6581717 1916 try
7e1279ea 1917 {
e5745975
JL
1918 string canon_func = q->final_function_name(fi.name, fi.decl_file,
1919 fi.decl_line);
1920
1921 q->mount_well_formed_probe_point();
1922 q->replace_probe_point_component_arg(TOK_FUNCTION, canon_func);
1923 q->replace_probe_point_component_arg(TOK_STATEMENT, canon_func);
1924
5a617dc6
JL
1925 // If it's a .return probe, we need to emit a *retprobe based on the
1926 // entrypc (PR13200). Note however that if prologue_end is valid,
1927 // dwarf_derived_probe will still take advantage of it by creating a new
1928 // probe there if necessary to pick up target vars (PR14436).
1929 if (fi.prologue_end == 0 || q->has_return)
1930 {
170ccf10 1931 q->prologue_end = fi.prologue_end;
5a617dc6
JL
1932 query_statement (fi.name, fi.decl_file, fi.decl_line,
1933 &fi.die, entrypc, q);
1934 }
b6581717 1935 else
5a617dc6 1936 {
5a617dc6
JL
1937 query_statement (fi.name, fi.decl_file, fi.decl_line,
1938 &fi.die, fi.prologue_end, q);
1939 }
e5745975
JL
1940
1941 q->unmount_well_formed_probe_point();
7e1279ea 1942 }
b6581717 1943 catch (semantic_error &e)
7e1279ea 1944 {
b6581717 1945 q->sess.print_error (e);
7e1279ea
FCE
1946 }
1947}
1948
7e1279ea 1949static void
de797211 1950query_srcfile_line (Dwarf_Addr addr, int lineno, dwarf_query * q)
7e1279ea 1951{
8fb810d7
JL
1952 assert (q->has_statement_str || q->has_function_str);
1953 assert (q->spec_type == function_file_and_line);
20e4a32c 1954
959ccfed
JL
1955 base_func_info_map_t bfis = q->filtered_all();
1956 base_func_info_map_t::iterator i;
1957 for (i = bfis.begin(); i != bfis.end(); ++i)
897820ca 1958 {
3e961ba6 1959 if (q->dw.die_has_pc (i->die, addr))
959ccfed
JL
1960 {
1961 if (q->sess.verbose>3)
1962 clog << _("filtered DIE lands on srcfile\n");
1963 Dwarf_Die scope;
1964 q->dw.inner_die_containing_pc(i->die, addr, scope);
e5bc2e52
JL
1965
1966 string canon_func = q->final_function_name(i->name, i->decl_file,
1967 lineno /* NB: not i->decl_line */ );
1968
5702442a
JL
1969 if (q->has_nearest)
1970 {
1971 int lineno_nearest = q->linenos[0];
1972 if (q->lineno_type == RELATIVE)
1973 lineno_nearest += i->decl_line;
1974 string canon_func_nearest = q->final_function_name(i->name, i->decl_file,
1975 lineno_nearest);
1976 q->mount_well_formed_probe_point();
1977 q->replace_probe_point_component_arg(TOK_STATEMENT, canon_func_nearest);
1978 }
1979
e5bc2e52
JL
1980 q->mount_well_formed_probe_point();
1981 q->replace_probe_point_component_arg(TOK_FUNCTION, canon_func);
1982 q->replace_probe_point_component_arg(TOK_STATEMENT, canon_func);
1983
959ccfed
JL
1984 query_statement (i->name, i->decl_file,
1985 lineno, // NB: not q->line !
1986 &scope, addr, q);
e5bc2e52
JL
1987
1988 q->unmount_well_formed_probe_point();
5702442a
JL
1989 if (q->has_nearest)
1990 q->unmount_well_formed_probe_point();
959ccfed 1991 }
20e4a32c 1992 }
7e1279ea
FCE
1993}
1994
7fdd3e2c
JS
1995bool
1996inline_instance_info::operator<(const inline_instance_info& other) const
1997{
1998 if (entrypc != other.entrypc)
1999 return entrypc < other.entrypc;
2000
2001 if (decl_line != other.decl_line)
2002 return decl_line < other.decl_line;
2003
2004 int cmp = name.compare(other.name);
c60517ca
FCE
2005
2006 if (!cmp)
2007 {
2008 assert (decl_file);
2009 assert (other.decl_file);
2010 cmp = strcmp(decl_file, other.decl_file);
2011 }
2012
7fdd3e2c
JS
2013 return cmp < 0;
2014}
2015
2016
4fa7b22b 2017static int
3956fccb 2018query_dwarf_inline_instance (Dwarf_Die * die, dwarf_query * q)
4fa7b22b 2019{
6b517475 2020 assert (q->has_statement_str || q->has_function_str);
4bda987e 2021 assert (!q->has_call && !q->has_return && !q->has_exported);
bd2b1e68 2022
39bcd429 2023 try
7a053d3b 2024 {
b0ee93c4 2025 if (q->sess.verbose>2)
b530b5b3 2026 clog << _F("selected inline instance of %s\n", q->dw.function_name.c_str());
7e1279ea 2027
6b517475
JS
2028 Dwarf_Addr entrypc;
2029 if (q->dw.die_entrypc (die, &entrypc))
2030 {
2031 inline_instance_info inl;
2032 inl.die = *die;
2033 inl.name = q->dw.function_name;
2034 inl.entrypc = entrypc;
2035 q->dw.function_file (&inl.decl_file);
2036 q->dw.function_line (&inl.decl_line);
2037
2038 // make sure that this inline hasn't already
2039 // been matched from a different CU
2040 if (q->inline_dupes.insert(inl).second)
2041 q->filtered_inlines.push_back(inl);
2042 }
7e1279ea
FCE
2043 return DWARF_CB_OK;
2044 }
2045 catch (const semantic_error& e)
2046 {
2047 q->sess.print_error (e);
2048 return DWARF_CB_ABORT;
2049 }
2050}
bb788f9f 2051
7e1279ea 2052static int
7d007451 2053query_dwarf_func (Dwarf_Die * func, dwarf_query * q)
7e1279ea 2054{
6b517475 2055 assert (q->has_statement_str || q->has_function_str);
bb788f9f 2056
bd25380d
JS
2057 // weed out functions whose decl_file isn't one of
2058 // the source files that we actually care about
6b517475 2059 if (q->spec_type != function_alone &&
bd25380d 2060 q->filtered_srcfiles.count(dwarf_decl_file(func)?:"") == 0)
8096dd7d 2061 return DWARF_CB_OK;
bd25380d 2062
7e1279ea
FCE
2063 try
2064 {
7e1279ea
FCE
2065 q->dw.focus_on_function (func);
2066
7d6d0afc
JS
2067 if (!q->dw.function_scope_matches(q->scopes))
2068 return DWARF_CB_OK;
2069
857bdfd1
JS
2070 // make sure that this function address hasn't
2071 // already been matched under an aliased name
2072 Dwarf_Addr addr;
2073 if (!q->dw.func_is_inline() &&
2074 dwarf_entrypc(func, &addr) == 0 &&
2075 !q->alias_dupes.insert(addr).second)
2076 return DWARF_CB_OK;
2077
4bda987e 2078 if (q->dw.func_is_inline () && (! q->has_call) && (! q->has_return) && (! q->has_exported))
7e1279ea 2079 {
4bda987e 2080 if (q->sess.verbose>3)
b530b5b3 2081 clog << _F("checking instances of inline %s\n", q->dw.function_name.c_str());
4bda987e 2082 q->dw.iterate_over_inline_instances (query_dwarf_inline_instance, q);
7e1279ea 2083 }
8f14e444
FCE
2084 else if (q->dw.func_is_inline () && (q->has_return)) // PR 11553
2085 {
2086 q->inlined_non_returnable.insert (q->dw.function_name);
2087 }
396afcee 2088 else if (!q->dw.func_is_inline () && (! q->has_inline))
20e4a32c 2089 {
4bda987e
SC
2090 if (q->has_exported && !q->dw.func_is_exported ())
2091 return DWARF_CB_OK;
6b517475 2092 if (q->sess.verbose>2)
b530b5b3 2093 clog << _F("selected function %s\n", q->dw.function_name.c_str());
6b517475
JS
2094
2095 func_info func;
2096 q->dw.function_die (&func.die);
2097 func.name = q->dw.function_name;
2098 q->dw.function_file (&func.decl_file);
2099 q->dw.function_line (&func.decl_line);
2100
2101 Dwarf_Addr entrypc;
2102 if (q->dw.function_entrypc (&entrypc))
2103 {
2104 func.entrypc = entrypc;
2105 q->filtered_functions.push_back (func);
2106 }
2107 /* else this function is fully inlined, just ignore it */
7e1279ea 2108 }
39bcd429 2109 return DWARF_CB_OK;
bd2b1e68 2110 }
39bcd429 2111 catch (const semantic_error& e)
bd2b1e68 2112 {
39bcd429
FCE
2113 q->sess.print_error (e);
2114 return DWARF_CB_ABORT;
bd2b1e68 2115 }
bd2b1e68
GH
2116}
2117
2118static int
5c378838 2119query_cu (Dwarf_Die * cudie, dwarf_query * q)
bd2b1e68 2120{
6b517475
JS
2121 assert (q->has_statement_str || q->has_function_str);
2122
85007c04 2123 if (pending_interrupts) return DWARF_CB_ABORT;
7a053d3b 2124
39bcd429 2125 try
bd2b1e68 2126 {
7e1279ea 2127 q->dw.focus_on_cu (cudie);
b5d77020 2128
b0ee93c4 2129 if (false && q->sess.verbose>2)
b530b5b3
LB
2130 clog << _F("focused on CU '%s', in module '%s'\n",
2131 q->dw.cu_name().c_str(), q->dw.module_name.c_str());
d9b516ca 2132
6b517475
JS
2133 q->filtered_srcfiles.clear();
2134 q->filtered_functions.clear();
2135 q->filtered_inlines.clear();
2136
2137 // In this path, we find "abstract functions", record
2138 // information about them, and then (depending on lineno
2139 // matching) possibly emit one or more of the function's
2140 // associated addresses. Unfortunately the control of this
2141 // cannot easily be turned inside out.
2142
2143 if (q->spec_type != function_alone)
39bcd429 2144 {
6b517475
JS
2145 // If we have a pattern string with a filename, we need
2146 // to elaborate the srcfile mask in question first.
2147 q->dw.collect_srcfiles_matching (q->file, q->filtered_srcfiles);
2148
2149 // If we have a file pattern and *no* srcfile matches, there's
2150 // no need to look further into this CU, so skip.
2151 if (q->filtered_srcfiles.empty())
2152 return DWARF_CB_OK;
2153 }
e4c58386 2154
6b517475
JS
2155 // Pick up [entrypc, name, DIE] tuples for all the functions
2156 // matching the query, and fill in the prologue endings of them
2157 // all in a single pass.
5898b6e1 2158 int rc = q->dw.iterate_over_functions (query_dwarf_func, q, q->function);
6b517475
JS
2159 if (rc != DWARF_CB_OK)
2160 q->query_done = true;
2161
277a2b96
JL
2162 if (!q->filtered_functions.empty() &&
2163 !q->has_statement_str && // PR 2608
2164 (q->sess.prologue_searching ||
2165 (q->has_process && !q->dw.has_valid_locs()))) // PR 6871 && PR 6941
2166 q->dw.resolve_prologue_endings (q->filtered_functions);
6b517475 2167
600d39f1 2168 if (q->has_label)
6b517475 2169 {
600d39f1
JL
2170 enum lineno_t lineno_type = WILDCARD;
2171 if (q->spec_type == function_file_and_line)
2172 lineno_type = q->lineno_type;
959ccfed
JL
2173 base_func_info_map_t bfis = q->filtered_all();
2174 base_func_info_map_t::iterator i;
2175 for (i = bfis.begin(); i != bfis.end(); ++i)
600d39f1
JL
2176 q->dw.iterate_over_labels (&i->die, q->label_val, *i, q->linenos,
2177 lineno_type, q, query_label);
39bcd429 2178 }
67959c62
JL
2179 else if (q->has_callee || q->has_callees_num)
2180 {
2181 // .callee(str) --> str, .callees[(N)] --> "*"
2182 string callee_val = q->has_callee ? q->callee_val : "*";
2183 long callees_num_val = q->has_callees_num ? q->callees_num_val : 1;
2184
2185 // NB: We filter functions that do not match the file here rather than
2186 // in query_callee because we only want the filtering to apply to the
2187 // first level, not to callees that are recursed into if
2188 // callees_num_val > 1.
959ccfed
JL
2189 base_func_info_map_t bfis = q->filtered_all();
2190 base_func_info_map_t::iterator i;
2191 for (i = bfis.begin(); i != bfis.end(); ++i)
67959c62
JL
2192 {
2193 if (q->spec_type != function_alone &&
2194 q->filtered_srcfiles.count(i->decl_file) == 0)
2195 continue;
2196 q->dw.iterate_over_callees (&i->die, callee_val,
2197 callees_num_val,
e772a6e7 2198 q, query_callee, *i);
67959c62
JL
2199 }
2200 }
c5142c66
JL
2201 else if (q->spec_type == function_file_and_line
2202 // User specified function, file and lineno, but if they match
2203 // exactly a specific function in a specific line at a specific
2204 // decl_line, the user doesn't actually want to probe a lineno,
2205 // but rather the function itself. So let fall through to
2206 // query_func_info/query_inline_instance_info in final else.
2207 && !q->is_fully_specified_function())
600d39f1
JL
2208 {
2209 // .statement(...:NN) often gets mixed up with .function(...:NN)
2210 if (q->has_function_str)
2211 q->sess.print_warning (_("For probing a particular line, use a "
2212 ".statement() probe, not .function()"),
2213 q->base_probe->tok);
2214
2215 base_func_info_map_t bfis = q->filtered_all();
2216
2217 set<string>::const_iterator srcfile;
2218 for (srcfile = q->filtered_srcfiles.begin();
2219 srcfile != q->filtered_srcfiles.end(); ++srcfile)
2220 q->dw.iterate_over_srcfile_lines(srcfile->c_str(), q->linenos,
2221 q->lineno_type, bfis,
83eeb3ac
HK
2222 query_srcfile_line,
2223 q->has_nearest, q);
600d39f1 2224 }
6b517475
JS
2225 else
2226 {
2227 // Otherwise, simply probe all resolved functions.
2228 for (func_info_map_t::iterator i = q->filtered_functions.begin();
2229 i != q->filtered_functions.end(); ++i)
2230 query_func_info (i->entrypc, *i, q);
2231
2232 // And all inline instances (if we're not excluding inlines with ".call")
2233 if (! q->has_call)
2234 for (inline_instance_map_t::iterator i
2235 = q->filtered_inlines.begin(); i != q->filtered_inlines.end(); ++i)
2236 query_inline_instance_info (*i, q);
e772a6e7 2237 }
39bcd429 2238 return DWARF_CB_OK;
bd2b1e68 2239 }
39bcd429 2240 catch (const semantic_error& e)
bd2b1e68 2241 {
39bcd429
FCE
2242 q->sess.print_error (e);
2243 return DWARF_CB_ABORT;
bd2b1e68 2244 }
bd2b1e68
GH
2245}
2246
0ce64fb8 2247
4df79aaf
JS
2248void
2249dwarf_query::query_module_functions ()
2250{
2251 try
2252 {
2253 filtered_srcfiles.clear();
2254 filtered_functions.clear();
2255 filtered_inlines.clear();
2256
2257 // Collect all module functions so we know which CUs are interesting
2258 int rc = dw.iterate_single_function(query_dwarf_func, this, function);
2259 if (rc != DWARF_CB_OK)
2260 {
2261 query_done = true;
2262 return;
2263 }
2264
2265 set<void*> used_cus; // by cu->addr
2266 vector<Dwarf_Die> cus;
2267 Dwarf_Die cu_mem;
2268
959ccfed
JL
2269 base_func_info_map_t bfis = filtered_all();
2270 base_func_info_map_t::iterator i;
2271 for (i = bfis.begin(); i != bfis.end(); ++i)
4df79aaf
JS
2272 if (dwarf_diecu(&i->die, &cu_mem, NULL, NULL) &&
2273 used_cus.insert(cu_mem.addr).second)
2274 cus.push_back(cu_mem);
2275
2276 // Reset the dupes since we didn't actually collect them the first time
2277 alias_dupes.clear();
2278 inline_dupes.clear();
2279
2280 // Run the query again on the individual CUs
91bb9081
LB
2281 for (vector<Dwarf_Die>::iterator i = cus.begin(); i != cus.end(); ++i){
2282 rc = query_cu(&*i, this);
2283 if (rc != DWARF_CB_OK)
2284 {
2285 query_done = true;
2286 return;
2287 }
2288 }
4df79aaf
JS
2289 }
2290 catch (const semantic_error& e)
2291 {
2292 sess.print_error (e);
2293 }
2294}
2295
2296
5f0a03a6
JK
2297static void
2298validate_module_elf (Dwfl_Module *mod, const char *name, base_query *q)
2299{
2300 // Validate the machine code in this elf file against the
2301 // session machine. This is important, in case the wrong kind
2302 // of debuginfo is being automagically processed by elfutils.
2303 // While we can tell i686 apart from x86-64, unfortunately
2304 // we can't help confusing i586 vs i686 (both EM_386).
2305
2306 Dwarf_Addr bias;
2307 // We prefer dwfl_module_getdwarf to dwfl_module_getelf here,
2308 // because dwfl_module_getelf can force costly section relocations
2309 // we don't really need, while either will do for this purpose.
2310 Elf* elf = (dwarf_getelf (dwfl_module_getdwarf (mod, &bias))
2311 ?: dwfl_module_getelf (mod, &bias));
2312
2313 GElf_Ehdr ehdr_mem;
2314 GElf_Ehdr* em = gelf_getehdr (elf, &ehdr_mem);
e1c3b13a 2315 if (em == 0) { DWFL_ASSERT ("dwfl_getehdr", dwfl_errno()); }
d8f31d0a 2316 assert(em);
5f0a03a6
JK
2317 int elf_machine = em->e_machine;
2318 const char* debug_filename = "";
2319 const char* main_filename = "";
2320 (void) dwfl_module_info (mod, NULL, NULL,
2321 NULL, NULL, NULL,
2322 & main_filename,
2323 & debug_filename);
2324 const string& sess_machine = q->sess.architecture;
756c9462
FCE
2325
2326 string expect_machine; // to match sess.machine (i.e., kernel machine)
2327 string expect_machine2;
5f0a03a6 2328
d27e6fd5 2329 // NB: See also the 'uname -m' squashing done in main.cxx.
5f0a03a6
JK
2330 switch (elf_machine)
2331 {
756c9462
FCE
2332 // x86 and ppc are bi-architecture; a 64-bit kernel
2333 // can normally run either 32-bit or 64-bit *userspace*.
2334 case EM_386:
2335 expect_machine = "i?86";
2336 if (! q->has_process) break; // 32-bit kernel/module
2337 /* FALLSTHROUGH */
2338 case EM_X86_64:
2339 expect_machine2 = "x86_64";
2340 break;
2341 case EM_PPC:
756c9462 2342 case EM_PPC64:
5a1c472e 2343 expect_machine = "powerpc";
756c9462 2344 break;
3fe7d888 2345 case EM_S390: expect_machine = "s390"; break;
5f0a03a6 2346 case EM_IA_64: expect_machine = "ia64"; break;
d27e6fd5 2347 case EM_ARM: expect_machine = "arm*"; break;
86aae990 2348 case EM_AARCH64: expect_machine = "arm64"; break;
5f0a03a6
JK
2349 // XXX: fill in some more of these
2350 default: expect_machine = "?"; break;
2351 }
2352
2353 if (! debug_filename) debug_filename = main_filename;
2354 if (! debug_filename) debug_filename = name;
2355
756c9462
FCE
2356 if (fnmatch (expect_machine.c_str(), sess_machine.c_str(), 0) != 0 &&
2357 fnmatch (expect_machine2.c_str(), sess_machine.c_str(), 0) != 0)
5f0a03a6
JK
2358 {
2359 stringstream msg;
b530b5b3
LB
2360 msg << _F("ELF machine %s|%s (code %d) mismatch with target %s in '%s'",
2361 expect_machine.c_str(), expect_machine2.c_str(), elf_machine,
2362 sess_machine.c_str(), debug_filename);
dc09353a 2363 throw SEMANTIC_ERROR(msg.str ());
5f0a03a6
JK
2364 }
2365
b57082de 2366 if (q->sess.verbose>1)
b78a0fbb 2367 clog << _F("focused on module '%s' = [%#" PRIx64 "-%#" PRIx64 ", bias %#" PRIx64
b530b5b3
LB
2368 " file %s ELF machine %s|%s (code %d)\n",
2369 q->dw.module_name.c_str(), q->dw.module_start, q->dw.module_end,
2370 q->dw.module_bias, debug_filename, expect_machine.c_str(),
2371 expect_machine2.c_str(), elf_machine);
5f0a03a6 2372}
1d3a40b6 2373
91af0778
FCE
2374
2375
2376static Dwarf_Addr
2377lookup_symbol_address (Dwfl_Module *m, const char* wanted)
2378{
2379 int syments = dwfl_module_getsymtab(m);
2380 assert(syments);
2381 for (int i = 1; i < syments; ++i)
2382 {
2383 GElf_Sym sym;
2384 const char *name = dwfl_module_getsym(m, i, &sym, NULL);
2385 if (name != NULL && strcmp(name, wanted) == 0)
2386 return sym.st_value;
2387 }
2388
2389 return 0;
2390}
2391
2392
2393
bd2b1e68 2394static int
b8da0ad1 2395query_module (Dwfl_Module *mod,
91af0778 2396 void **,
b8da0ad1 2397 const char *name,
6f4c1275 2398 Dwarf_Addr addr,
06de3a04 2399 base_query *q)
bd2b1e68 2400{
39bcd429 2401 try
e38d6504 2402 {
91af0778
FCE
2403 module_info* mi = q->sess.module_cache->cache[name];
2404 if (mi == 0)
2405 {
2406 mi = q->sess.module_cache->cache[name] = new module_info(name);
2407
6f4c1275
FCE
2408 mi->mod = mod;
2409 mi->addr = addr;
91af0778 2410
6f4c1275
FCE
2411 const char* debug_filename = "";
2412 const char* main_filename = "";
2413 (void) dwfl_module_info (mod, NULL, NULL,
2414 NULL, NULL, NULL,
2415 & main_filename,
2416 & debug_filename);
2417
ab3ed72d 2418 if (debug_filename || main_filename)
91af0778 2419 {
6f4c1275
FCE
2420 mi->elf_path = debug_filename ?: main_filename;
2421 }
2422 else if (name == TOK_KERNEL)
2423 {
2424 mi->dwarf_status = info_absent;
91af0778 2425 }
91af0778
FCE
2426 }
2427 // OK, enough of that module_info caching business.
2428
5f0a03a6 2429 q->dw.focus_on_module(mod, mi);
d9b516ca 2430
39bcd429
FCE
2431 // If we have enough information in the pattern to skip a module and
2432 // the module does not match that information, return early.
b8da0ad1 2433 if (!q->dw.module_name_matches(q->module_val))
85007c04 2434 return pending_interrupts ? DWARF_CB_ABORT : DWARF_CB_OK;
0cbbf9d1
FCE
2435
2436 // Don't allow module("*kernel*") type expressions to match the
2437 // elfutils module "kernel", which we refer to in the probe
2438 // point syntax exclusively as "kernel.*".
2439 if (q->dw.module_name == TOK_KERNEL && ! q->has_kernel)
85007c04 2440 return pending_interrupts ? DWARF_CB_ABORT : DWARF_CB_OK;
b5d77020 2441
5f0a03a6
JK
2442 if (mod)
2443 validate_module_elf(mod, name, q);
2444 else
91af0778
FCE
2445 assert(q->has_kernel); // and no vmlinux to examine
2446
2447 if (q->sess.verbose>2)
b530b5b3 2448 cerr << _F("focused on module '%s'\n", q->dw.module_name.c_str());
91af0778
FCE
2449
2450
2451 // Collect a few kernel addresses. XXX: these belong better
2452 // to the sess.module_info["kernel"] struct.
2453 if (q->dw.module_name == TOK_KERNEL)
c931ec8a 2454 {
91af0778
FCE
2455 if (! q->sess.sym_kprobes_text_start)
2456 q->sess.sym_kprobes_text_start = lookup_symbol_address (mod, "__kprobes_text_start");
2457 if (! q->sess.sym_kprobes_text_end)
2458 q->sess.sym_kprobes_text_end = lookup_symbol_address (mod, "__kprobes_text_end");
2459 if (! q->sess.sym_stext)
2460 q->sess.sym_stext = lookup_symbol_address (mod, "_stext");
c931ec8a
FCE
2461 }
2462
dbe9d133
JL
2463 // If there is a .library component, then q->path will hold the path to
2464 // the executable if the library was fully resolved. If not (e.g. not
2465 // absolute, or globby), resort to iterate_over_libraries().
2466 if (q->has_library && q->path.empty())
84c84ac4 2467 q->dw.iterate_over_libraries (&q->query_library_callback, q);
576eaefe
SC
2468 // .plt is translated to .plt.statement(N). We only want to iterate for the
2469 // .plt case
2470 else if (q->has_plt && ! q->has_statement)
070764c0
JL
2471 {
2472 q->dw.iterate_over_plt (q, &q->query_plt_callback);
2473 q->visited_modules.insert(name);
2474 }
84c84ac4 2475 else
070764c0
JL
2476 {
2477 // search the module for matches of the probe point.
2478 q->handle_query_module();
2479 q->visited_modules.insert(name);
2480 }
bb788f9f 2481
b8da0ad1 2482 // If we know that there will be no more matches, abort early.
85007c04 2483 if (q->dw.module_name_final_match(q->module_val) || pending_interrupts)
b8da0ad1
FCE
2484 return DWARF_CB_ABORT;
2485 else
2486 return DWARF_CB_OK;
7a053d3b 2487 }
39bcd429 2488 catch (const semantic_error& e)
bd2b1e68 2489 {
39bcd429
FCE
2490 q->sess.print_error (e);
2491 return DWARF_CB_ABORT;
bd2b1e68 2492 }
bd2b1e68
GH
2493}
2494
35d4ab18 2495
84c84ac4 2496void
bbbc7241 2497base_query::query_library_callback (base_query *me, const char *data)
84c84ac4 2498{
5d5bd369 2499 me->query_library (data);
84c84ac4
SC
2500}
2501
2502
614f0fcf
JL
2503probe*
2504build_library_probe(dwflpp& dw,
2505 const string& library,
2506 probe *base_probe,
2507 probe_point *base_loc)
2508{
2509 probe_point* specific_loc = new probe_point(*base_loc);
614f0fcf
JL
2510 specific_loc->from_glob = true;
2511 vector<probe_point::component*> derived_comps;
2512
2513 // Create new probe point for the matching library. This is what will be
2514 // shown in listing mode. Also replace the process(str) with the real
2515 // absolute path rather than keeping what the user typed in.
2516 vector<probe_point::component*>::iterator it;
2517 for (it = specific_loc->components.begin();
2518 it != specific_loc->components.end(); ++it)
2519 if ((*it)->functor == TOK_PROCESS)
2520 derived_comps.push_back(new probe_point::component(TOK_PROCESS,
2521 new literal_string(path_remove_sysroot(dw.sess, dw.module_name))));
2522 else if ((*it)->functor == TOK_LIBRARY)
2523 derived_comps.push_back(new probe_point::component(TOK_LIBRARY,
2524 new literal_string(path_remove_sysroot(dw.sess, library))));
2525 else
2526 derived_comps.push_back(*it);
2527 probe_point* derived_loc = new probe_point(*specific_loc);
2528 derived_loc->components = derived_comps;
2529 return new probe (new probe (base_probe, specific_loc), derived_loc);
2530}
2531
2532bool
51d6bda3
SC
2533query_one_library (const char *library, dwflpp & dw,
2534 const string user_lib, probe * base_probe, probe_point *base_loc,
2535 vector<derived_probe *> & results)
84c84ac4 2536{
47e226ed 2537 if (dw.function_name_matches_pattern(library, "*" + user_lib))
84c84ac4 2538 {
05fb3e0c
WF
2539 string library_path = find_executable (library, "", dw.sess.sysenv,
2540 "LD_LIBRARY_PATH");
168e2ef5
JL
2541 probe *new_base = build_library_probe(dw, library_path,
2542 base_probe, base_loc);
2543
2544 // We pass true for the optional parameter of derive_probes() here to
2545 // indicate that we don't mind if the probe doesn't resolve. This is
2546 // because users expect wildcarded probe points to only apply to a subset
2547 // of matching libraries, in the sense of "any", rather than "all", just
2548 // like module("*") and process("*"). See also dwarf_builder::build().
2549 derive_probes(dw.sess, new_base, results, true /* optional */ );
84c84ac4 2550
51d6bda3 2551 if (dw.sess.verbose > 2)
2cbcfa9c 2552 clog << _("module=") << library_path << endl;
614f0fcf 2553 return true;
84c84ac4 2554 }
614f0fcf 2555 return false;
84c84ac4
SC
2556}
2557
2558
51d6bda3
SC
2559void
2560dwarf_query::query_library (const char *library)
2561{
614f0fcf
JL
2562 visited_libraries.insert(library);
2563 if (query_one_library (library, dw, user_lib, base_probe, base_loc, results))
2564 resolved_library = true;
51d6bda3
SC
2565}
2566
576eaefe
SC
2567struct plt_expanding_visitor: public var_expanding_visitor
2568{
2569 plt_expanding_visitor(const string & entry):
2570 entry (entry)
2571 {
2572 }
2573 const string & entry;
2574
2575 void visit_target_symbol (target_symbol* e);
2576};
2577
2578
2579void
45cdb40e 2580base_query::query_plt_callback (base_query *me, const char *entry, size_t address)
576eaefe 2581{
576eaefe
SC
2582 if (me->dw.function_name_matches_pattern (entry, me->plt_val))
2583 me->query_plt (entry, address);
7f4964f1 2584 me->dw.mod_info->plt_funcs.insert(entry);
576eaefe
SC
2585}
2586
2587
2588void
2589query_one_plt (const char *entry, long addr, dwflpp & dw,
2590 probe * base_probe, probe_point *base_loc,
93b44c2a 2591 vector<derived_probe *> & results, base_query *q)
576eaefe 2592{
93b44c2a
JL
2593 string module = dw.module_name;
2594 if (q->has_process)
2595 module = path_remove_sysroot(dw.sess, module);
2596
576eaefe 2597 probe_point* specific_loc = new probe_point(*base_loc);
93b44c2a
JL
2598 specific_loc->well_formed = true;
2599
576eaefe
SC
2600 vector<probe_point::component*> derived_comps;
2601
2602 if (dw.sess.verbose > 2)
2603 clog << _F("plt entry=%s\n", entry);
2604
576eaefe
SC
2605 vector<probe_point::component*>::iterator it;
2606 for (it = specific_loc->components.begin();
2607 it != specific_loc->components.end(); ++it)
93b44c2a
JL
2608 if ((*it)->functor == TOK_PROCESS)
2609 {
2610 // Replace with fully resolved path
2611 *it = new probe_point::component(TOK_PROCESS,
2612 new literal_string(q->has_library ? q->path : module));
2613 derived_comps.push_back(*it);
2614 }
2615 else if ((*it)->functor == TOK_PLT)
3d372d6b 2616 {
93b44c2a 2617 // Replace possibly globby component
8159bf55
FCE
2618 *it = new probe_point::component(TOK_PLT,
2619 new literal_string(entry));
3d372d6b
SC
2620 derived_comps.push_back(*it);
2621 derived_comps.push_back(new probe_point::component(TOK_STATEMENT,
ac7c8669 2622 new literal_number(addr, true)));
3d372d6b 2623 }
576eaefe
SC
2624 else
2625 derived_comps.push_back(*it);
2626 probe_point* derived_loc = new probe_point(*specific_loc);
2627 derived_loc->components = derived_comps;
02c34e38
FCE
2628 probe *new_base = new probe (new probe (base_probe, specific_loc),
2629 derived_loc);
576eaefe
SC
2630 string e = string(entry);
2631 plt_expanding_visitor pltv (e);
2632 pltv.replace (new_base->body);
2e96714f
SC
2633
2634 literal_map_t params;
2635 for (unsigned i = 0; i < derived_loc->components.size(); ++i)
2636 {
2637 probe_point::component *c = derived_loc->components[i];
2638 params[c->functor] = c->arg;
2639 }
2640 dwarf_query derived_q(new_base, derived_loc, dw, params, results, "", "");
2641 dw.iterate_over_modules<base_query>(&query_module, &derived_q);
576eaefe
SC
2642}
2643
2644
2645void
2646dwarf_query::query_plt (const char *entry, size_t address)
2647{
93b44c2a 2648 query_one_plt (entry, address, dw, base_probe, base_loc, results, this);
576eaefe 2649}
51d6bda3 2650
435f53a7
FCE
2651// This would more naturally fit into elaborate.cxx:semantic_pass_symbols,
2652// but the needed declaration for module_cache is not available there.
2653// Nor for that matter in session.cxx. Only in this CU is that field ever
2654// set (in query_module() above), so we clean it up here too.
2655static void
2656delete_session_module_cache (systemtap_session& s)
2657{
2658 if (s.module_cache) {
2659 if (s.verbose > 3)
b530b5b3 2660 clog << _("deleting module_cache") << endl;
435f53a7
FCE
2661 delete s.module_cache;
2662 s.module_cache = 0;
2663 }
2664}
2665
2666
de688825 2667struct dwarf_var_expanding_visitor: public var_expanding_visitor
35d4ab18 2668{
77de5e9e 2669 dwarf_query & q;
bcc12710 2670 Dwarf_Die *scope_die;
77de5e9e 2671 Dwarf_Addr addr;
8c819921 2672 block *add_block;
2260f4e3 2673 block *add_call_probe; // synthesized from .return probes with saved $vars
1630966a
JS
2674 // NB: tids are not always collected in add_block & add_call_probe, because
2675 // gen_kretprobe_saved_return doesn't need them. Thus we need these extra
2676 // *_tid bools for gen_mapped_saved_return to tell what's there.
8cc799a5 2677 bool add_block_tid, add_call_probe_tid;
af234c40
JS
2678 unsigned saved_longs, saved_strings; // data saved within kretprobes
2679 map<std::string, expression *> return_ts_map;
729455a7 2680 vector<Dwarf_Die> scopes;
3689db05 2681 // probe counter name -> pointer of associated probe
aa2c662f 2682 std::set<std::string> perf_counter_refs;
b95e2b79 2683 bool visited;
77de5e9e 2684
de688825 2685 dwarf_var_expanding_visitor(dwarf_query & q, Dwarf_Die *sd, Dwarf_Addr a):
af234c40 2686 q(q), scope_die(sd), addr(a), add_block(NULL), add_call_probe(NULL),
8cc799a5 2687 add_block_tid(false), add_call_probe_tid(false),
af234c40 2688 saved_longs(0), saved_strings(0), visited(false) {}
277c21bc 2689 expression* gen_mapped_saved_return(expression* e, const string& name);
140be17a 2690 expression* gen_kretprobe_saved_return(expression* e);
a7999c82
JS
2691 void visit_target_symbol_saved_return (target_symbol* e);
2692 void visit_target_symbol_context (target_symbol* e);
d7f3e0c5 2693 void visit_target_symbol (target_symbol* e);
bd1fcbad 2694 void visit_atvar_op (atvar_op* e);
c24447be 2695 void visit_cast_op (cast_op* e);
8cc799a5 2696 void visit_entry_op (entry_op* e);
3689db05 2697 void visit_perf_op (perf_op* e);
729455a7
JS
2698private:
2699 vector<Dwarf_Die>& getscopes(target_symbol *e);
77de5e9e
GH
2700};
2701
2702
de688825 2703unsigned var_expanding_visitor::tick = 0;
77de5e9e 2704
a50de939 2705
74fe61bc 2706var_expanding_visitor::var_expanding_visitor (): op()
a50de939
DS
2707{
2708 // FIXME: for the time being, by default we only support plain '$foo
2709 // = bar', not '+=' or any other op= variant. This is fixable, but a
2710 // bit ugly.
2711 //
2712 // If derived classes desire to add additional operator support, add
2713 // new operators to this list in the derived class constructor.
2714 valid_ops.insert ("=");
2715}
2716
2717
f55efafe
JS
2718void
2719var_expanding_visitor::provide_lvalue_call(functioncall* fcall)
2720{
2721 // Provide the functioncall to our parent, so that it can be used to
2722 // substitute for the assignment node immediately above us.
2723 assert(!target_symbol_setter_functioncalls.empty());
2724 *(target_symbol_setter_functioncalls.top()) = fcall;
2725}
2726
2727
87214add
JS
2728bool
2729var_expanding_visitor::rewrite_lvalue(const token* tok, const std::string& eop,
2730 expression*& lvalue, expression*& rvalue)
77de5e9e 2731{
e57b735a
GH
2732 // Our job would normally be to require() the left and right sides
2733 // into a new assignment. What we're doing is slightly trickier:
2734 // we're pushing a functioncall** onto a stack, and if our left
2735 // child sets the functioncall* for that value, we're going to
2736 // assume our left child was a target symbol -- transformed into a
2737 // set_target_foo(value) call, and it wants to take our right child
2738 // as the argument "value".
2739 //
2740 // This is why some people claim that languages with
2741 // constructor-decomposing case expressions have a leg up on
2742 // visitors.
2743
2744 functioncall *fcall = NULL;
d9b516ca 2745
a50de939 2746 // Let visit_target_symbol know what operator it should handle.
87214add
JS
2747 const string* old_op = op;
2748 op = &eop;
a50de939 2749
e57b735a 2750 target_symbol_setter_functioncalls.push (&fcall);
87214add 2751 replace (lvalue);
e57b735a 2752 target_symbol_setter_functioncalls.pop ();
87214add
JS
2753 replace (rvalue);
2754
2755 op = old_op;
e57b735a
GH
2756
2757 if (fcall != NULL)
77de5e9e 2758 {
e57b735a
GH
2759 // Our left child is informing us that it was a target variable
2760 // and it has been replaced with a set_target_foo() function
2761 // call; we are going to provide that function call -- with the
2762 // right child spliced in as sole argument -- in place of
de688825 2763 // ourselves, in the var expansion we're in the middle of making.
e57b735a 2764
87214add 2765 if (valid_ops.find (eop) == valid_ops.end ())
a50de939
DS
2766 {
2767 // Build up a list of supported operators.
2768 string ops;
2769 std::set<string>::iterator i;
b530b5b3 2770 int valid_ops_size = 0;
a50de939 2771 for (i = valid_ops.begin(); i != valid_ops.end(); i++)
b530b5b3 2772 {
a50de939 2773 ops += " " + *i + ",";
b530b5b3
LB
2774 valid_ops_size++;
2775 }
a50de939
DS
2776 ops.resize(ops.size() - 1); // chop off the last ','
2777
2778 // Throw the error.
dc09353a 2779 throw SEMANTIC_ERROR (_NF("Only the following assign operator is implemented on target variables: %s",
1e41115c 2780 "Only the following assign operators are implemented on target variables: %s",
52c2652f 2781 valid_ops_size, ops.c_str()), tok);
b530b5b3 2782
a50de939 2783 }
e57b735a 2784
87214add
JS
2785 assert (lvalue == fcall);
2786 if (rvalue)
2787 fcall->args.push_back (rvalue);
4ed05b15 2788 provide (fcall);
87214add 2789 return true;
77de5e9e 2790 }
e57b735a 2791 else
87214add
JS
2792 return false;
2793}
2794
2795
2796void
2797var_expanding_visitor::visit_assignment (assignment* e)
2798{
2799 if (!rewrite_lvalue (e->tok, e->op, e->left, e->right))
2800 provide (e);
2801}
2802
2803
2804void
2805var_expanding_visitor::visit_pre_crement (pre_crement* e)
2806{
2807 expression *dummy = NULL;
2808 if (!rewrite_lvalue (e->tok, e->op, e->operand, dummy))
2809 provide (e);
2810}
2811
2812
2813void
2814var_expanding_visitor::visit_post_crement (post_crement* e)
2815{
2816 expression *dummy = NULL;
2817 if (!rewrite_lvalue (e->tok, e->op, e->operand, dummy))
2818 provide (e);
2819}
2820
2821
2822void
2823var_expanding_visitor::visit_delete_statement (delete_statement* s)
2824{
2825 string fakeop = "delete";
2826 expression *dummy = NULL;
2827 if (!rewrite_lvalue (s->tok, fakeop, s->value, dummy))
2828 provide (s);
e57b735a 2829}
d9b516ca 2830
d7f3e0c5 2831
30263a73
FCE
2832void
2833var_expanding_visitor::visit_defined_op (defined_op* e)
2834{
2835 bool resolved = true;
2836
2837 defined_ops.push (e);
2838 try {
0fb0cac9 2839 replace (e->operand);
30263a73 2840
c69a87e0 2841 // NB: Formerly, we had some curious cases to consider here, depending on what
30263a73 2842 // various visit_target_symbol() implementations do for successful or
c69a87e0
FCE
2843 // erroneous resolutions. Some would signal a visit_target_symbol failure
2844 // with an exception, with a set flag within the target_symbol, or nothing
2845 // at all.
30263a73 2846 //
c69a87e0
FCE
2847 // Now, failures always have to be signalled with a
2848 // saved_conversion_error being chained to the target_symbol.
2849 // Successes have to result in an attempted rewrite of the
850bfddd 2850 // target_symbol (via provide()).
780f11ff 2851 //
c69a87e0
FCE
2852 // Edna Mode: "no capes". fche: "no exceptions".
2853
30263a73
FCE
2854 // dwarf stuff: success: rewrites to a function; failure: retains target_symbol, sets saved_conversion_error
2855 //
2856 // sdt-kprobes sdt.h: success: string or functioncall; failure: semantic_error
2857 //
2858 // sdt-uprobes: success: string or no op; failure: no op; expect derived/synthetic
2859 // dwarf probe to take care of it.
2860 // But this is rather unhelpful. So we rig the sdt_var_expanding_visitor
2861 // to pass through @defined() to the synthetic dwarf probe.
780f11ff 2862 //
30263a73
FCE
2863 // utrace: success: rewrites to function; failure: semantic_error
2864 //
850bfddd 2865 // procfs: success: rewrites to function; failure: semantic_error
30263a73 2866
0fb0cac9
JS
2867 target_symbol* tsym = dynamic_cast<target_symbol*> (e->operand);
2868 if (tsym && tsym->saved_conversion_error) // failing
30263a73 2869 resolved = false;
0fb0cac9 2870 else if (tsym) // unresolved but not marked failing
b7aedf26 2871 {
780f11ff
JS
2872 // There are some visitors that won't touch certain target_symbols,
2873 // e.g. dwarf_var_expanding_visitor won't resolve @cast. We should
2874 // leave it for now so some other visitor can have a chance.
b7aedf26
JS
2875 provide (e);
2876 return;
2877 }
30263a73
FCE
2878 else // resolved, rewritten to some other expression type
2879 resolved = true;
780f11ff 2880 } catch (const semantic_error& e) {
c69a87e0 2881 assert (0); // should not happen
30263a73
FCE
2882 }
2883 defined_ops.pop ();
2884
2885 literal_number* ln = new literal_number (resolved ? 1 : 0);
2886 ln->tok = e->tok;
2887 provide (ln);
2888}
2889
2890
5f36109e
JS
2891struct dwarf_pretty_print
2892{
2893 dwarf_pretty_print (dwflpp& dw, vector<Dwarf_Die>& scopes, Dwarf_Addr pc,
2894 const string& local, bool userspace_p,
2895 const target_symbol& e):
d19a9a82
JS
2896 dw(dw), local(local), scopes(scopes), pc(pc), pointer(NULL),
2897 userspace_p(userspace_p), deref_p(true)
5f36109e
JS
2898 {
2899 init_ts (e);
2900 dw.type_die_for_local (scopes, pc, local, ts, &base_type);
2901 }
2902
2903 dwarf_pretty_print (dwflpp& dw, Dwarf_Die *scope_die, Dwarf_Addr pc,
2904 bool userspace_p, const target_symbol& e):
d19a9a82
JS
2905 dw(dw), scopes(1, *scope_die), pc(pc), pointer(NULL),
2906 userspace_p(userspace_p), deref_p(true)
5f36109e
JS
2907 {
2908 init_ts (e);
2909 dw.type_die_for_return (&scopes[0], pc, ts, &base_type);
2910 }
2911
2912 dwarf_pretty_print (dwflpp& dw, Dwarf_Die *type_die, expression* pointer,
d19a9a82 2913 bool deref_p, bool userspace_p, const target_symbol& e):
5f36109e 2914 dw(dw), pc(0), pointer(pointer), pointer_type(*type_die),
d19a9a82 2915 userspace_p(userspace_p), deref_p(deref_p)
5f36109e
JS
2916 {
2917 init_ts (e);
2918 dw.type_die_for_pointer (type_die, ts, &base_type);
2919 }
2920
2921 functioncall* expand ();
ce83ff57 2922 ~dwarf_pretty_print () { delete ts; }
5f36109e
JS
2923
2924private:
2925 dwflpp& dw;
2926 target_symbol* ts;
7d11d8c9 2927 bool print_full;
5f36109e
JS
2928 Dwarf_Die base_type;
2929
2930 string local;
2931 vector<Dwarf_Die> scopes;
2932 Dwarf_Addr pc;
2933
2934 expression* pointer;
2935 Dwarf_Die pointer_type;
2936
d19a9a82 2937 const bool userspace_p, deref_p;
5f36109e
JS
2938
2939 void recurse (Dwarf_Die* type, target_symbol* e,
7d11d8c9 2940 print_format* pf, bool top=false);
600551ca
JS
2941 void recurse_bitfield (Dwarf_Die* type, target_symbol* e,
2942 print_format* pf);
5f36109e 2943 void recurse_base (Dwarf_Die* type, target_symbol* e,
7d11d8c9 2944 print_format* pf);
5f36109e 2945 void recurse_array (Dwarf_Die* type, target_symbol* e,
7d11d8c9 2946 print_format* pf, bool top);
5f36109e 2947 void recurse_pointer (Dwarf_Die* type, target_symbol* e,
7d11d8c9 2948 print_format* pf, bool top);
5f36109e 2949 void recurse_struct (Dwarf_Die* type, target_symbol* e,
7d11d8c9 2950 print_format* pf, bool top);
5f36109e 2951 void recurse_struct_members (Dwarf_Die* type, target_symbol* e,
7d11d8c9 2952 print_format* pf, int& count);
bbee5bb8 2953 bool print_chars (Dwarf_Die* type, target_symbol* e, print_format* pf);
5f36109e
JS
2954
2955 void init_ts (const target_symbol& e);
2956 expression* deref (target_symbol* e);
c55ea10d 2957 bool push_deref (print_format* pf, const string& fmt, target_symbol* e);
5f36109e
JS
2958};
2959
2960
2961void
2962dwarf_pretty_print::init_ts (const target_symbol& e)
2963{
2964 // Work with a new target_symbol so we can modify arguments
2965 ts = new target_symbol (e);
2966
2967 if (ts->addressof)
dc09353a 2968 throw SEMANTIC_ERROR(_("cannot take address of pretty-printed variable"), ts->tok);
5f36109e 2969
c4965ad9
JS
2970 size_t depth = ts->pretty_print_depth ();
2971 if (depth == 0)
dc09353a 2972 throw SEMANTIC_ERROR(_("invalid target_symbol for pretty-print"), ts->tok);
c4965ad9 2973 print_full = depth > 1;
5f36109e
JS
2974 ts->components.pop_back();
2975}
2976
2977
2978functioncall*
2979dwarf_pretty_print::expand ()
2980{
2981 static unsigned tick = 0;
2982
2983 // function pretty_print_X([pointer], [arg1, arg2, ...]) {
7d11d8c9
JS
2984 // try {
2985 // return sprintf("{.foo=...}", (ts)->foo, ...)
2986 // } catch {
2987 // return "ERROR"
2988 // }
5f36109e
JS
2989 // }
2990
2991 // Create the function decl and call.
2992
2993 functiondecl *fdecl = new functiondecl;
2994 fdecl->tok = ts->tok;
2995 fdecl->synthetic = true;
2996 fdecl->name = "_dwarf_pretty_print_" + lex_cast(tick++);
2997 fdecl->type = pe_string;
2998
2999 functioncall* fcall = new functioncall;
eb79eb0a 3000 fcall->referent = fdecl;
5f36109e
JS
3001 fcall->tok = ts->tok;
3002 fcall->function = fdecl->name;
140be17a 3003 fcall->type = pe_string;
5f36109e
JS
3004
3005 // If there's a <pointer>, replace it with a new var and make that
3006 // the first function argument.
3007 if (pointer)
3008 {
3009 vardecl *v = new vardecl;
3010 v->type = pe_long;
3011 v->name = "pointer";
3012 v->tok = ts->tok;
3013 fdecl->formal_args.push_back (v);
3014 fcall->args.push_back (pointer);
3015
3016 symbol* sym = new symbol;
3017 sym->tok = ts->tok;
3018 sym->name = v->name;
5f36109e
JS
3019 pointer = sym;
3020 }
3021
3022 // For each expression argument, replace it with a function argument.
3023 for (unsigned i = 0; i < ts->components.size(); ++i)
3024 if (ts->components[i].type == target_symbol::comp_expression_array_index)
3025 {
3026 vardecl *v = new vardecl;
3027 v->type = pe_long;
3028 v->name = "index" + lex_cast(i);
3029 v->tok = ts->tok;
3030 fdecl->formal_args.push_back (v);
3031 fcall->args.push_back (ts->components[i].expr_index);
3032
3033 symbol* sym = new symbol;
3034 sym->tok = ts->tok;
3035 sym->name = v->name;
5f36109e
JS
3036 ts->components[i].expr_index = sym;
3037 }
3038
3039 // Create the return sprintf.
1c922ad7 3040 print_format* pf = print_format::create(ts->tok, "sprintf");
5f36109e
JS
3041 return_statement* rs = new return_statement;
3042 rs->tok = ts->tok;
3043 rs->value = pf;
5f36109e
JS
3044
3045 // Recurse into the actual values.
7d11d8c9 3046 recurse (&base_type, ts, pf, true);
5f36109e
JS
3047 pf->components = print_format::string_to_components(pf->raw_components);
3048
7d11d8c9
JS
3049 // Create the try-catch net
3050 try_block* tb = new try_block;
3051 tb->tok = ts->tok;
3052 tb->try_block = rs;
3053 tb->catch_error_var = 0;
3054 return_statement* rs2 = new return_statement;
3055 rs2->tok = ts->tok;
3056 rs2->value = new literal_string ("ERROR");
3057 rs2->value->tok = ts->tok;
3058 tb->catch_block = rs2;
3059 fdecl->body = tb;
3060
f8809d54 3061 fdecl->join (dw.sess);
5f36109e
JS
3062 return fcall;
3063}
3064
3065
3066void
3067dwarf_pretty_print::recurse (Dwarf_Die* start_type, target_symbol* e,
7d11d8c9 3068 print_format* pf, bool top)
5f36109e 3069{
611a848e
JS
3070 // deal with initial void* pointers
3071 if (!deref_p && null_die(start_type))
3072 {
3073 push_deref (pf, "%p", e);
3074 return;
3075 }
3076
5f36109e
JS
3077 Dwarf_Die type;
3078 dw.resolve_unqualified_inner_typedie (start_type, &type, e);
3079
3080 switch (dwarf_tag(&type))
3081 {
3082 default:
3083 // XXX need a warning?
3084 // throw semantic_error ("unsupported type (tag " + lex_cast(dwarf_tag(&type))
3085 // + ") for " + dwarf_type_name(&type), e->tok);
3086 pf->raw_components.append("?");
3087 break;
3088
3089 case DW_TAG_enumeration_type:
3090 case DW_TAG_base_type:
7d11d8c9 3091 recurse_base (&type, e, pf);
5f36109e
JS
3092 break;
3093
3094 case DW_TAG_array_type:
7d11d8c9 3095 recurse_array (&type, e, pf, top);
5f36109e
JS
3096 break;
3097
3098 case DW_TAG_pointer_type:
3099 case DW_TAG_reference_type:
3100 case DW_TAG_rvalue_reference_type:
7d11d8c9 3101 recurse_pointer (&type, e, pf, top);
5f36109e
JS
3102 break;
3103
3104 case DW_TAG_subroutine_type:
c55ea10d 3105 push_deref (pf, "<function>:%p", e);
5f36109e
JS
3106 break;
3107
3108 case DW_TAG_union_type:
5f36109e
JS
3109 case DW_TAG_structure_type:
3110 case DW_TAG_class_type:
7d11d8c9 3111 recurse_struct (&type, e, pf, top);
5f36109e
JS
3112 break;
3113 }
3114}
3115
3116
600551ca
JS
3117// Bit fields are handled as a special-case combination of recurse() and
3118// recurse_base(), only called from recurse_struct_members(). The main
3119// difference is that the value is always printed numerically, even if the
3120// underlying type is a char.
3121void
3122dwarf_pretty_print::recurse_bitfield (Dwarf_Die* start_type, target_symbol* e,
3123 print_format* pf)
3124{
3125 Dwarf_Die type;
3126 dw.resolve_unqualified_inner_typedie (start_type, &type, e);
3127
3128 int tag = dwarf_tag(&type);
3129 if (tag != DW_TAG_base_type && tag != DW_TAG_enumeration_type)
3130 {
3131 // XXX need a warning?
3132 // throw semantic_error ("unsupported bitfield type (tag " + lex_cast(tag)
3133 // + ") for " + dwarf_type_name(&type), e->tok);
3134 pf->raw_components.append("?");
3135 return;
3136 }
3137
3138 Dwarf_Attribute attr;
3139 Dwarf_Word encoding = (Dwarf_Word) -1;
3140 dwarf_formudata (dwarf_attr_integrate (&type, DW_AT_encoding, &attr),
3141 &encoding);
3142 switch (encoding)
3143 {
3144 case DW_ATE_float:
3145 case DW_ATE_complex_float:
3146 // XXX need a warning?
3147 // throw semantic_error ("unsupported bitfield type (encoding " + lex_cast(encoding)
3148 // + ") for " + dwarf_type_name(&type), e->tok);
3149 pf->raw_components.append("?");
3150 break;
3151
3152 case DW_ATE_unsigned:
3153 case DW_ATE_unsigned_char:
3154 push_deref (pf, "%u", e);
3155 break;
3156
3157 case DW_ATE_signed:
3158 case DW_ATE_signed_char:
3159 default:
3160 push_deref (pf, "%i", e);
3161 break;
3162 }
3163}
3164
3165
5f36109e
JS
3166void
3167dwarf_pretty_print::recurse_base (Dwarf_Die* type, target_symbol* e,
7d11d8c9 3168 print_format* pf)
5f36109e
JS
3169{
3170 Dwarf_Attribute attr;
3171 Dwarf_Word encoding = (Dwarf_Word) -1;
3172 dwarf_formudata (dwarf_attr_integrate (type, DW_AT_encoding, &attr),
3173 &encoding);
5f36109e
JS
3174 switch (encoding)
3175 {
3176 case DW_ATE_float:
3177 case DW_ATE_complex_float:
3178 // XXX need a warning?
3179 // throw semantic_error ("unsupported type (encoding " + lex_cast(encoding)
3180 // + ") for " + dwarf_type_name(type), e->tok);
3181 pf->raw_components.append("?");
5f36109e
JS
3182 break;
3183
6561d8d1 3184 case DW_ATE_UTF: // XXX need to add unicode to _stp_vsprint_char
5f36109e
JS
3185 case DW_ATE_signed_char:
3186 case DW_ATE_unsigned_char:
941101c1
JS
3187 // Use escapes to make sure that non-printable characters
3188 // don't interrupt our stream (especially '\0' values).
3189 push_deref (pf, "'%#c'", e);
5f36109e
JS
3190 break;
3191
3192 case DW_ATE_unsigned:
c55ea10d 3193 push_deref (pf, "%u", e);
5f36109e
JS
3194 break;
3195
600551ca 3196 case DW_ATE_signed:
5f36109e 3197 default:
c55ea10d 3198 push_deref (pf, "%i", e);
5f36109e
JS
3199 break;
3200 }
5f36109e
JS
3201}
3202
3203
3204void
3205dwarf_pretty_print::recurse_array (Dwarf_Die* type, target_symbol* e,
7d11d8c9 3206 print_format* pf, bool top)
5f36109e 3207{
7d11d8c9
JS
3208 if (!top && !print_full)
3209 {
3210 pf->raw_components.append("[...]");
3211 return;
3212 }
3213
5f36109e
JS
3214 Dwarf_Die childtype;
3215 dwarf_attr_die (type, DW_AT_type, &childtype);
bbee5bb8
JS
3216
3217 if (print_chars (&childtype, e, pf))
3218 return;
3219
5f36109e
JS
3220 pf->raw_components.append("[");
3221
3222 // We print the array up to the first 5 elements.
3223 // XXX how can we determine the array size?
3224 // ... for now, just print the first element
64cddf39 3225 // NB: limit to 32 args; see PR10750 and c_unparser::visit_print_format.
5f36109e 3226 unsigned i, size = 1;
64cddf39 3227 for (i=0; i < size && i < 5 && pf->args.size() < 32; ++i)
5f36109e
JS
3228 {
3229 if (i > 0)
3230 pf->raw_components.append(", ");
3231 target_symbol* e2 = new target_symbol(*e);
3232 e2->components.push_back (target_symbol::component(e->tok, i));
7d11d8c9 3233 recurse (&childtype, e2, pf);
5f36109e
JS
3234 }
3235 if (i < size || 1/*XXX until real size is known */)
3236 pf->raw_components.append(", ...");
3237 pf->raw_components.append("]");
3238}
3239
3240
3241void
3242dwarf_pretty_print::recurse_pointer (Dwarf_Die* type, target_symbol* e,
7d11d8c9 3243 print_format* pf, bool top)
5f36109e 3244{
7d11d8c9 3245 // We chase to top-level pointers, but leave the rest alone
d19a9a82 3246 bool void_p = true;
7d11d8c9 3247 Dwarf_Die pointee;
bbee5bb8 3248 if (dwarf_attr_die (type, DW_AT_type, &pointee))
d19a9a82
JS
3249 {
3250 try
3251 {
3252 dw.resolve_unqualified_inner_typedie (&pointee, &pointee, e);
3253 void_p = false;
3254 }
3255 catch (const semantic_error&) {}
3256 }
3257
3258 if (!void_p)
5f36109e 3259 {
bbee5bb8
JS
3260 if (print_chars (&pointee, e, pf))
3261 return;
3262
3263 if (top)
3264 {
3265 recurse (&pointee, e, pf, top);
3266 return;
3267 }
5f36109e 3268 }
bbee5bb8 3269
c55ea10d 3270 push_deref (pf, "%p", e);
5f36109e
JS
3271}
3272
3273
3274void
3275dwarf_pretty_print::recurse_struct (Dwarf_Die* type, target_symbol* e,
7d11d8c9 3276 print_format* pf, bool top)
5f36109e 3277{
bdec0e18
JS
3278 if (dwarf_hasattr(type, DW_AT_declaration))
3279 {
a44a7cb5 3280 Dwarf_Die *resolved = dw.declaration_resolve(type);
bdec0e18
JS
3281 if (!resolved)
3282 {
3283 // could be an error, but for now just stub it
3284 // throw semantic_error ("unresolved " + dwarf_type_name(type), e->tok);
3285 pf->raw_components.append("{...}");
3286 return;
3287 }
3288 type = resolved;
3289 }
3290
5f36109e
JS
3291 int count = 0;
3292 pf->raw_components.append("{");
7d11d8c9
JS
3293 if (top || print_full)
3294 recurse_struct_members (type, e, pf, count);
3295 else
3296 pf->raw_components.append("...");
5f36109e
JS
3297 pf->raw_components.append("}");
3298}
3299
3300
3301void
3302dwarf_pretty_print::recurse_struct_members (Dwarf_Die* type, target_symbol* e,
7d11d8c9 3303 print_format* pf, int& count)
5f36109e 3304{
a80f28d8
JS
3305 /* With inheritance, a subclass may mask member names of parent classes, so
3306 * our search among the inheritance tree must be breadth-first rather than
3307 * depth-first (recursive). The type die is still our starting point. When
3308 * we encounter a masked name, just skip it. */
3309 set<string> dupes;
3310 deque<Dwarf_Die> inheritees(1, *type);
3311 for (; !inheritees.empty(); inheritees.pop_front())
3312 {
dee830d9 3313 Dwarf_Die child, childtype, import;
a80f28d8
JS
3314 if (dwarf_child (&inheritees.front(), &child) == 0)
3315 do
3316 {
3317 target_symbol* e2 = e;
5f36109e 3318
a80f28d8
JS
3319 // skip static members
3320 if (dwarf_hasattr(&child, DW_AT_declaration))
3321 continue;
5f36109e 3322
a80f28d8 3323 int tag = dwarf_tag (&child);
5f36109e 3324
dee830d9
MW
3325 /* Pretend imported units contain members by recursing into
3326 struct_member printing with the same count. */
3327 if (tag == DW_TAG_imported_unit
3328 && dwarf_attr_die (&child, DW_AT_import, &import))
3329 recurse_struct_members (&import, e2, pf, count);
3330
a80f28d8
JS
3331 if (tag != DW_TAG_member && tag != DW_TAG_inheritance)
3332 continue;
5f36109e 3333
a80f28d8 3334 dwarf_attr_die (&child, DW_AT_type, &childtype);
5f36109e 3335
a80f28d8
JS
3336 if (tag == DW_TAG_inheritance)
3337 {
3338 inheritees.push_back(childtype);
3339 continue;
3340 }
5f36109e 3341
a80f28d8
JS
3342 int childtag = dwarf_tag (&childtype);
3343 const char *member = dwarf_diename (&child);
3a147004 3344
a80f28d8
JS
3345 // "_vptr.foo" members are C++ virtual function tables,
3346 // which (generally?) aren't interesting for users.
3347 if (member && startswith(member, "_vptr."))
3348 continue;
3a147004 3349
a80f28d8
JS
3350 // skip inheritance-masked duplicates
3351 if (member && !dupes.insert(member).second)
3352 continue;
64cddf39 3353
a80f28d8
JS
3354 if (++count > 1)
3355 pf->raw_components.append(", ");
64cddf39 3356
a80f28d8
JS
3357 // NB: limit to 32 args; see PR10750 and c_unparser::visit_print_format.
3358 if (pf->args.size() >= 32)
3359 {
3360 pf->raw_components.append("...");
3361 break;
3362 }
3363
3364 if (member)
3365 {
3366 pf->raw_components.append(".");
3367 pf->raw_components.append(member);
5f36109e 3368
a80f28d8
JS
3369 e2 = new target_symbol(*e);
3370 e2->components.push_back (target_symbol::component(e->tok, member));
3371 }
3372 else if (childtag == DW_TAG_union_type)
3373 pf->raw_components.append("<union>");
3374 else if (childtag == DW_TAG_structure_type)
3375 pf->raw_components.append("<class>");
3376 else if (childtag == DW_TAG_class_type)
3377 pf->raw_components.append("<struct>");
3378 pf->raw_components.append("=");
600551ca
JS
3379
3380 if (dwarf_hasattr_integrate (&child, DW_AT_bit_offset))
3381 recurse_bitfield (&childtype, e2, pf);
3382 else
3383 recurse (&childtype, e2, pf);
5f36109e 3384 }
a80f28d8
JS
3385 while (dwarf_siblingof (&child, &child) == 0);
3386 }
5f36109e
JS
3387}
3388
3389
bbee5bb8
JS
3390bool
3391dwarf_pretty_print::print_chars (Dwarf_Die* start_type, target_symbol* e,
3392 print_format* pf)
3393{
3394 Dwarf_Die type;
3395 dw.resolve_unqualified_inner_typedie (start_type, &type, e);
6561d8d1
JS
3396
3397 Dwarf_Attribute attr;
3398 Dwarf_Word encoding = (Dwarf_Word) -1;
3399 dwarf_formudata (dwarf_attr_integrate (&type, DW_AT_encoding, &attr),
3400 &encoding);
3401 switch (encoding)
bbee5bb8 3402 {
6561d8d1
JS
3403 case DW_ATE_UTF:
3404 case DW_ATE_signed_char:
3405 case DW_ATE_unsigned_char:
3406 break;
3407 default:
3408 return false;
3409 }
3410
3411 string function = userspace_p ? "user_string2" : "kernel_string2";
3412 Dwarf_Word size = (Dwarf_Word) -1;
3413 dwarf_formudata (dwarf_attr_integrate (&type, DW_AT_byte_size, &attr), &size);
3414 switch (size)
3415 {
3416 case 1:
3417 break;
3418 case 2:
3419 function += "_utf16";
3420 break;
3421 case 4:
3422 function += "_utf32";
3423 break;
3424 default:
3425 return false;
3426 }
3427
3428 if (push_deref (pf, "\"%s\"", e))
3429 {
3430 // steal the last arg for a string access
3431 assert (!pf->args.empty());
3432 functioncall* fcall = new functioncall;
3433 fcall->tok = e->tok;
3434 fcall->function = function;
3435 fcall->args.push_back (pf->args.back());
3436 expression *err_msg = new literal_string ("<unknown>");
3437 err_msg->tok = e->tok;
3438 fcall->args.push_back (err_msg);
3439 pf->args.back() = fcall;
bbee5bb8 3440 }
6561d8d1 3441 return true;
bbee5bb8
JS
3442}
3443
a5ce5211
MW
3444// PR10601: adapt to kernel-vs-userspace loc2c-runtime
3445static const string EMBEDDED_FETCH_DEREF_KERNEL = string("\n")
f1e8e7e0
MW
3446 + "#define fetch_register k_fetch_register\n"
3447 + "#define store_register k_store_register\n"
3448 + "#define deref kderef\n"
3449 + "#define store_deref store_kderef\n";
a5ce5211
MW
3450
3451static const string EMBEDDED_FETCH_DEREF_USER = string("\n")
f1e8e7e0
MW
3452 + "#define fetch_register u_fetch_register\n"
3453 + "#define store_register u_store_register\n"
3454 + "#define deref uderef\n"
3455 + "#define store_deref store_uderef\n";
a5ce5211
MW
3456
3457#define EMBEDDED_FETCH_DEREF(U) \
f1e8e7e0 3458 (U ? EMBEDDED_FETCH_DEREF_USER : EMBEDDED_FETCH_DEREF_KERNEL)
a5ce5211
MW
3459
3460static const string EMBEDDED_FETCH_DEREF_DONE = string("\n")
f1e8e7e0
MW
3461 + "#undef fetch_register\n"
3462 + "#undef store_register\n"
3463 + "#undef deref\n"
3464 + "#undef store_deref\n";
bbee5bb8 3465
1c0be8c7 3466static functioncall*
40a393cd
JS
3467synthetic_embedded_deref_call(dwflpp& dw,
3468 Dwarf_Die* function_type,
1c0be8c7
JS
3469 const string& function_name,
3470 const string& function_code,
1c0be8c7
JS
3471 bool userspace_p,
3472 bool lvalue_p,
3473 target_symbol* e,
3474 expression* pointer=NULL)
3475{
3476 // Synthesize a functiondecl for the given embedded code string.
5f36109e
JS
3477 functiondecl *fdecl = new functiondecl;
3478 fdecl->synthetic = true;
3479 fdecl->tok = e->tok;
1c0be8c7 3480 fdecl->name = function_name;
40a393cd
JS
3481 // The fdecl type is generic, but we'll be detailed on the fcall below.
3482 fdecl->type = pe_long;
c3ed664e
JS
3483 fdecl->type_details.reset(new exp_type_dwarf(&dw, function_type,
3484 userspace_p, e->addressof));
1c0be8c7 3485
5f36109e
JS
3486 embeddedcode *ec = new embeddedcode;
3487 ec->tok = e->tok;
1c0be8c7
JS
3488 ec->code += "/* unprivileged */";
3489 if (! lvalue_p)
3490 ec->code += "/* pure */";
3491 ec->code += EMBEDDED_FETCH_DEREF(userspace_p);
3492 ec->code += function_code;
3493 ec->code += EMBEDDED_FETCH_DEREF_DONE;
5f36109e
JS
3494 fdecl->body = ec;
3495
3496 // Synthesize a functioncall.
3497 functioncall* fcall = new functioncall;
3498 fcall->tok = e->tok;
40a393cd 3499 fcall->referent = fdecl;
5f36109e 3500 fcall->function = fdecl->name;
1c0be8c7 3501 fcall->type = fdecl->type;
c3ed664e 3502 fcall->type_details = fdecl->type_details;
5f36109e 3503
1c0be8c7
JS
3504 // If this code snippet uses a precomputed pointer,
3505 // pass that as the first argument.
5f36109e
JS
3506 if (pointer)
3507 {
5f36109e
JS
3508 vardecl *v = new vardecl;
3509 v->type = pe_long;
3510 v->name = "pointer";
3511 v->tok = e->tok;
3512 fdecl->formal_args.push_back(v);
3513 fcall->args.push_back(pointer);
3514 }
5f36109e 3515
1c0be8c7 3516 // Any non-literal indexes need to be passed as arguments too.
5f36109e
JS
3517 for (unsigned i = 0; i < e->components.size(); ++i)
3518 if (e->components[i].type == target_symbol::comp_expression_array_index)
3519 {
3520 vardecl *v = new vardecl;
3521 v->type = pe_long;
3522 v->name = "index" + lex_cast(i);
3523 v->tok = e->tok;
3524 fdecl->formal_args.push_back(v);
3525 fcall->args.push_back(e->components[i].expr_index);
3526 }
3527
1c0be8c7
JS
3528 // If this code snippet is assigning to an lvalue,
3529 // add a final argument for the rvalue.
3530 if (lvalue_p)
3531 {
3532 // Modify the fdecl so it carries a single pe_long formal
3533 // argument called "value".
5f36109e 3534
1c0be8c7
JS
3535 // FIXME: For the time being we only support setting target
3536 // variables which have base types; these are 'pe_long' in
3537 // stap's type vocabulary. Strings and pointers might be
3538 // reasonable, some day, but not today.
5f36109e 3539
1c0be8c7
JS
3540 vardecl *v = new vardecl;
3541 v->type = pe_long;
3542 v->name = "value";
3543 v->tok = e->tok;
3544 fdecl->formal_args.push_back(v);
3545 // NB: We don't know the value for fcall argument yet.
3546 // (see target_symbol_setter_functioncalls)
3547 }
3548
3549 // Add the synthesized decl to the session, and return the call.
40a393cd 3550 fdecl->join (dw.sess);
5f36109e
JS
3551 return fcall;
3552}
3553
1c0be8c7
JS
3554expression*
3555dwarf_pretty_print::deref (target_symbol* e)
3556{
3557 static unsigned tick = 0;
3558
3559 if (!deref_p)
3560 {
3561 assert (pointer && e->components.empty());
3562 return pointer;
3563 }
3564
3565 bool lvalue_p = false;
3566 string name = "_dwarf_pretty_print_deref_" + lex_cast(tick++);
3567
3568 string code;
40a393cd 3569 Dwarf_Die endtype;
1c0be8c7 3570 if (pointer)
40a393cd 3571 code = dw.literal_stmt_for_pointer (&pointer_type, e, false, &endtype);
1c0be8c7 3572 else if (!local.empty())
40a393cd 3573 code = dw.literal_stmt_for_local (scopes, pc, local, e, false, &endtype);
1c0be8c7 3574 else
40a393cd 3575 code = dw.literal_stmt_for_return (&scopes[0], pc, e, false, &endtype);
1c0be8c7 3576
40a393cd 3577 return synthetic_embedded_deref_call(dw, &endtype, name, code,
1c0be8c7
JS
3578 userspace_p, lvalue_p, e, pointer);
3579}
3580
5f36109e 3581
c55ea10d
JS
3582bool
3583dwarf_pretty_print::push_deref (print_format* pf, const string& fmt,
3584 target_symbol* e)
3585{
3586 expression* e2 = NULL;
3587 try
3588 {
3589 e2 = deref (e);
3590 }
3591 catch (const semantic_error&)
3592 {
3593 pf->raw_components.append ("?");
3594 return false;
3595 }
3596 pf->raw_components.append (fmt);
3597 pf->args.push_back (e2);
3598 return true;
3599}
3600
3601
e57b735a 3602void
a7999c82 3603dwarf_var_expanding_visitor::visit_target_symbol_saved_return (target_symbol* e)
e57b735a 3604{
a7999c82
JS
3605 // Get the full name of the target symbol.
3606 stringstream ts_name_stream;
3607 e->print(ts_name_stream);
3608 string ts_name = ts_name_stream.str();
3609
3610 // Check and make sure we haven't already seen this target
3611 // variable in this return probe. If we have, just return our
3612 // last replacement.
af234c40 3613 map<string, expression *>::iterator i = return_ts_map.find(ts_name);
a7999c82 3614 if (i != return_ts_map.end())
85ecf79a 3615 {
a7999c82
JS
3616 provide (i->second);
3617 return;
3618 }
85ecf79a 3619
70208613
JS
3620 // Attempt the expansion directly first, so if there's a problem with the
3621 // variable we won't have a bogus entry probe lying around. Like in
3622 // saveargs(), we pretend for a moment that we're not in a .return.
3623 bool saved_has_return = q.has_return;
3624 q.has_return = false;
3625 expression *repl = e;
3626 replace (repl);
3627 q.has_return = saved_has_return;
3628 target_symbol* n = dynamic_cast<target_symbol*>(repl);
3629 if (n && n->saved_conversion_error)
3630 {
3631 provide (repl);
3632 return;
3633 }
3634
af234c40
JS
3635 expression *exp;
3636 if (!q.has_process &&
3637 strverscmp(q.sess.kernel_base_release.c_str(), "2.6.25") >= 0)
140be17a 3638 exp = gen_kretprobe_saved_return(repl);
af234c40 3639 else
cc9001af 3640 exp = gen_mapped_saved_return(repl, e->sym_name());
af234c40 3641
6bed2a76
JS
3642 // Propagate the DWARF type to the expression in the return probe.
3643 if (repl->type_details && !exp->type_details)
3644 exp->type_details = repl->type_details;
3645
af234c40
JS
3646 // Provide the variable to our parent so it can be used as a
3647 // substitute for the target symbol.
3648 provide (exp);
3649
3650 // Remember this replacement since we might be able to reuse
3651 // it later if the same return probe references this target
3652 // symbol again.
3653 return_ts_map[ts_name] = exp;
3654}
3655
4a2970a3 3656static expression*
23dc94f6
DS
3657gen_mapped_saved_return(systemtap_session &sess, expression* e,
3658 const string& name,
3659 block *& add_block, bool& add_block_tid,
3660 block *& add_call_probe, bool& add_call_probe_tid)
af234c40 3661{
23dc94f6
DS
3662 static unsigned tick = 0;
3663
a7999c82
JS
3664 // We've got to do several things here to handle target
3665 // variables in return probes.
85ecf79a 3666
a7999c82
JS
3667 // (1) Synthesize two global arrays. One is the cache of the
3668 // target variable and the other contains a thread specific
3669 // nesting level counter. The arrays will look like
3670 // this:
3671 //
23dc94f6
DS
3672 // _entry_tvar_{name}_{num}
3673 // _entry_tvar_{name}_{num}_ctr
a7999c82 3674
23dc94f6 3675 string aname = (string("_entry_tvar_")
cc9001af 3676 + name
aca66a36 3677 + "_" + lex_cast(tick++));
a7999c82
JS
3678 vardecl* vd = new vardecl;
3679 vd->name = aname;
3680 vd->tok = e->tok;
23dc94f6 3681 sess.globals.push_back (vd);
a7999c82
JS
3682
3683 string ctrname = aname + "_ctr";
3684 vd = new vardecl;
3685 vd->name = ctrname;
3686 vd->tok = e->tok;
23dc94f6 3687 sess.globals.push_back (vd);
a7999c82
JS
3688
3689 // (2) Create a new code block we're going to insert at the
3690 // beginning of this probe to get the cached value into a
3691 // temporary variable. We'll replace the target variable
3692 // reference with the temporary variable reference. The code
3693 // will look like this:
3694 //
23dc94f6
DS
3695 // _entry_tvar_tid = tid()
3696 // _entry_tvar_{name}_{num}_tmp
3697 // = _entry_tvar_{name}_{num}[_entry_tvar_tid,
3698 // _entry_tvar_{name}_{num}_ctr[_entry_tvar_tid]]
3699 // delete _entry_tvar_{name}_{num}[_entry_tvar_tid,
3700 // _entry_tvar_{name}_{num}_ctr[_entry_tvar_tid]--]
3701 // if (! _entry_tvar_{name}_{num}_ctr[_entry_tvar_tid])
3702 // delete _entry_tvar_{name}_{num}_ctr[_entry_tvar_tid]
a7999c82
JS
3703
3704 // (2a) Synthesize the tid temporary expression, which will look
3705 // like this:
3706 //
23dc94f6 3707 // _entry_tvar_tid = tid()
a7999c82 3708 symbol* tidsym = new symbol;
23dc94f6 3709 tidsym->name = string("_entry_tvar_tid");
a7999c82 3710 tidsym->tok = e->tok;
85ecf79a 3711
a7999c82
JS
3712 if (add_block == NULL)
3713 {
3714 add_block = new block;
3715 add_block->tok = e->tok;
8cc799a5 3716 }
8c819921 3717
8cc799a5
JS
3718 if (!add_block_tid)
3719 {
a7999c82
JS
3720 // Synthesize a functioncall to grab the thread id.
3721 functioncall* fc = new functioncall;
3722 fc->tok = e->tok;
3723 fc->function = string("tid");
8c819921 3724
23dc94f6 3725 // Assign the tid to '_entry_tvar_tid'.
8c819921
DS
3726 assignment* a = new assignment;
3727 a->tok = e->tok;
3728 a->op = "=";
a7999c82
JS
3729 a->left = tidsym;
3730 a->right = fc;
8c819921
DS
3731
3732 expr_statement* es = new expr_statement;
3733 es->tok = e->tok;
3734 es->value = a;
8c819921 3735 add_block->statements.push_back (es);
8cc799a5 3736 add_block_tid = true;
a7999c82 3737 }
8c819921 3738
a7999c82
JS
3739 // (2b) Synthesize an array reference and assign it to a
3740 // temporary variable (that we'll use as replacement for the
3741 // target variable reference). It will look like this:
3742 //
23dc94f6
DS
3743 // _entry_tvar_{name}_{num}_tmp
3744 // = _entry_tvar_{name}_{num}[_entry_tvar_tid,
3745 // _entry_tvar_{name}_{num}_ctr[_entry_tvar_tid]]
a7999c82
JS
3746
3747 arrayindex* ai_tvar_base = new arrayindex;
3748 ai_tvar_base->tok = e->tok;
3749
3750 symbol* sym = new symbol;
3751 sym->name = aname;
3752 sym->tok = e->tok;
3753 ai_tvar_base->base = sym;
3754
3755 ai_tvar_base->indexes.push_back(tidsym);
3756
3757 // We need to create a copy of the array index in its current
3758 // state so we can have 2 variants of it (the original and one
3759 // that post-decrements the second index).
3760 arrayindex* ai_tvar = new arrayindex;
3761 arrayindex* ai_tvar_postdec = new arrayindex;
3762 *ai_tvar = *ai_tvar_base;
3763 *ai_tvar_postdec = *ai_tvar_base;
3764
3765 // Synthesize the
23dc94f6 3766 // "_entry_tvar_{name}_{num}_ctr[_entry_tvar_tid]" used as the
a7999c82
JS
3767 // second index into the array.
3768 arrayindex* ai_ctr = new arrayindex;
3769 ai_ctr->tok = e->tok;
3770
3771 sym = new symbol;
3772 sym->name = ctrname;
3773 sym->tok = e->tok;
3774 ai_ctr->base = sym;
3775 ai_ctr->indexes.push_back(tidsym);
3776 ai_tvar->indexes.push_back(ai_ctr);
3777
3778 symbol* tmpsym = new symbol;
3779 tmpsym->name = aname + "_tmp";
3780 tmpsym->tok = e->tok;
3781
3782 assignment* a = new assignment;
3783 a->tok = e->tok;
3784 a->op = "=";
3785 a->left = tmpsym;
3786 a->right = ai_tvar;
3787
3788 expr_statement* es = new expr_statement;
3789 es->tok = e->tok;
3790 es->value = a;
3791
3792 add_block->statements.push_back (es);
3793
3794 // (2c) Add a post-decrement to the second array index and
3795 // delete the array value. It will look like this:
3796 //
23dc94f6
DS
3797 // delete _entry_tvar_{name}_{num}[_entry_tvar_tid,
3798 // _entry_tvar_{name}_{num}_ctr[_entry_tvar_tid]--]
a7999c82
JS
3799
3800 post_crement* pc = new post_crement;
3801 pc->tok = e->tok;
3802 pc->op = "--";
3803 pc->operand = ai_ctr;
3804 ai_tvar_postdec->indexes.push_back(pc);
3805
3806 delete_statement* ds = new delete_statement;
3807 ds->tok = e->tok;
3808 ds->value = ai_tvar_postdec;
3809
3810 add_block->statements.push_back (ds);
3811
3812 // (2d) Delete the counter value if it is 0. It will look like
3813 // this:
23dc94f6
DS
3814 // if (! _entry_tvar_{name}_{num}_ctr[_entry_tvar_tid])
3815 // delete _entry_tvar_{name}_{num}_ctr[_entry_tvar_tid]
a7999c82
JS
3816
3817 ds = new delete_statement;
3818 ds->tok = e->tok;
3819 ds->value = ai_ctr;
3820
3821 unary_expression *ue = new unary_expression;
3822 ue->tok = e->tok;
3823 ue->op = "!";
3824 ue->operand = ai_ctr;
3825
3826 if_statement *ifs = new if_statement;
3827 ifs->tok = e->tok;
3828 ifs->condition = ue;
3829 ifs->thenblock = ds;
3830 ifs->elseblock = NULL;
3831
3832 add_block->statements.push_back (ifs);
3833
3834 // (3) We need an entry probe that saves the value for us in the
3835 // global array we created. Create the entry probe, which will
3836 // look like this:
3837 //
2260f4e3 3838 // probe kernel.function("{function}").call {
23dc94f6
DS
3839 // _entry_tvar_tid = tid()
3840 // _entry_tvar_{name}_{num}[_entry_tvar_tid,
3841 // ++_entry_tvar_{name}_{num}_ctr[_entry_tvar_tid]]
a7999c82
JS
3842 // = ${param}
3843 // }
3844
2260f4e3 3845 if (add_call_probe == NULL)
a7999c82 3846 {
2260f4e3
FCE
3847 add_call_probe = new block;
3848 add_call_probe->tok = e->tok;
8cc799a5 3849 }
4baf0e53 3850
8cc799a5
JS
3851 if (!add_call_probe_tid)
3852 {
a7999c82
JS
3853 // Synthesize a functioncall to grab the thread id.
3854 functioncall* fc = new functioncall;
3855 fc->tok = e->tok;
3856 fc->function = string("tid");
4baf0e53 3857
23dc94f6 3858 // Assign the tid to '_entry_tvar_tid'.
a7999c82 3859 assignment* a = new assignment;
8fc05e57
DS
3860 a->tok = e->tok;
3861 a->op = "=";
a7999c82
JS
3862 a->left = tidsym;
3863 a->right = fc;
8fc05e57 3864
a7999c82 3865 expr_statement* es = new expr_statement;
8fc05e57
DS
3866 es->tok = e->tok;
3867 es->value = a;
2260f4e3 3868 add_call_probe = new block(add_call_probe, es);
8cc799a5 3869 add_call_probe_tid = true;
85ecf79a 3870 }
cf2a1f85 3871
a7999c82 3872 // Save the value, like this:
23dc94f6
DS
3873 // _entry_tvar_{name}_{num}[_entry_tvar_tid,
3874 // ++_entry_tvar_{name}_{num}_ctr[_entry_tvar_tid]]
a7999c82
JS
3875 // = ${param}
3876 arrayindex* ai_tvar_preinc = new arrayindex;
3877 *ai_tvar_preinc = *ai_tvar_base;
3878
3879 pre_crement* preinc = new pre_crement;
3880 preinc->tok = e->tok;
3881 preinc->op = "++";
3882 preinc->operand = ai_ctr;
3883 ai_tvar_preinc->indexes.push_back(preinc);
3884
3885 a = new assignment;
3886 a->tok = e->tok;
3887 a->op = "=";
3888 a->left = ai_tvar_preinc;
3889 a->right = e;
3890
3891 es = new expr_statement;
3892 es->tok = e->tok;
3893 es->value = a;
3894
2260f4e3 3895 add_call_probe = new block(add_call_probe, es);
a7999c82 3896
23dc94f6 3897 // (4) Provide the '_entry_tvar_{name}_{num}_tmp' variable to
a7999c82
JS
3898 // our parent so it can be used as a substitute for the target
3899 // symbol.
3f803f9e 3900 delete ai_tvar_base;
af234c40
JS
3901 return tmpsym;
3902}
a7999c82 3903
af234c40 3904
23dc94f6
DS
3905expression*
3906dwarf_var_expanding_visitor::gen_mapped_saved_return(expression* e,
3907 const string& name)
3908{
3909 return ::gen_mapped_saved_return(q.sess, e, name, add_block,
3910 add_block_tid, add_call_probe,
3911 add_call_probe_tid);
3912}
3913
3914
af234c40 3915expression*
140be17a 3916dwarf_var_expanding_visitor::gen_kretprobe_saved_return(expression* e)
af234c40
JS
3917{
3918 // The code for this is simple.
3919 //
3920 // .call:
3921 // _set_kretprobe_long(index, $value)
3922 //
3923 // .return:
3924 // _get_kretprobe_long(index)
3925 //
3926 // (or s/long/string/ for things like $$parms)
3927
3928 unsigned index;
3929 string setfn, getfn;
3930
140be17a
JS
3931 // We need the caller to predetermine the type of the expression!
3932 switch (e->type)
af234c40 3933 {
140be17a 3934 case pe_string:
af234c40
JS
3935 index = saved_strings++;
3936 setfn = "_set_kretprobe_string";
3937 getfn = "_get_kretprobe_string";
140be17a
JS
3938 break;
3939 case pe_long:
af234c40
JS
3940 index = saved_longs++;
3941 setfn = "_set_kretprobe_long";
3942 getfn = "_get_kretprobe_long";
140be17a
JS
3943 break;
3944 default:
dc09353a 3945 throw SEMANTIC_ERROR(_("unknown type to save in kretprobe"), e->tok);
af234c40
JS
3946 }
3947
3948 // Create the entry code
3949 // _set_kretprobe_{long|string}(index, $value)
3950
3951 if (add_call_probe == NULL)
3952 {
3953 add_call_probe = new block;
3954 add_call_probe->tok = e->tok;
3955 }
3956
3957 functioncall* set_fc = new functioncall;
3958 set_fc->tok = e->tok;
3959 set_fc->function = setfn;
3960 set_fc->args.push_back(new literal_number(index));
3961 set_fc->args.back()->tok = e->tok;
3962 set_fc->args.push_back(e);
3963
3964 expr_statement* set_es = new expr_statement;
3965 set_es->tok = e->tok;
3966 set_es->value = set_fc;
3967
3968 add_call_probe->statements.push_back(set_es);
3969
3970 // Create the return code
3971 // _get_kretprobe_{long|string}(index)
3972
3973 functioncall* get_fc = new functioncall;
3974 get_fc->tok = e->tok;
3975 get_fc->function = getfn;
3976 get_fc->args.push_back(new literal_number(index));
3977 get_fc->args.back()->tok = e->tok;
3978
3979 return get_fc;
a7999c82 3980}
a43ba433 3981
2cb3fe26 3982
a7999c82
JS
3983void
3984dwarf_var_expanding_visitor::visit_target_symbol_context (target_symbol* e)
3985{
b78a0fbb 3986 if (null_die(scope_die)) {
04c8e51b
JL
3987 literal_string *empty = new literal_string("");
3988 empty->tok = e->tok;
3989 provide(empty);
a7999c82 3990 return;
b78a0fbb 3991 }
2cb3fe26 3992
5f36109e
JS
3993 target_symbol *tsym = new target_symbol(*e);
3994
c4965ad9 3995 bool pretty = e->check_pretty_print ();
fde50242 3996 string format = pretty ? "=%s" : "=%#x";
a43ba433 3997
a7999c82
JS
3998 // Convert $$parms to sprintf of a list of parms and active local vars
3999 // which we recursively evaluate
a43ba433 4000
1c922ad7 4001 print_format* pf = print_format::create(e->tok, "sprintf");
a7999c82 4002
277c21bc 4003 if (q.has_return && (e->name == "$$return"))
a7999c82 4004 {
277c21bc 4005 tsym->name = "$return";
a7999c82
JS
4006
4007 // Ignore any variable that isn't accessible.
4008 tsym->saved_conversion_error = 0;
4009 expression *texp = tsym;
8b095b45 4010 replace (texp); // NB: throws nothing ...
a7999c82 4011 if (tsym->saved_conversion_error) // ... but this is how we know it happened.
a43ba433 4012 {
2cb3fe26 4013
a43ba433
FCE
4014 }
4015 else
4016 {
a7999c82 4017 pf->raw_components += "return";
5f36109e 4018 pf->raw_components += format;
a7999c82
JS
4019 pf->args.push_back(texp);
4020 }
4021 }
4022 else
4023 {
4024 // non-.return probe: support $$parms, $$vars, $$locals
345bbb3d 4025 bool first = true;
a7999c82 4026 Dwarf_Die result;
d48bc7eb
JS
4027 vector<Dwarf_Die> scopes = q.dw.getscopes(scope_die);
4028 for (unsigned i = 0; i < scopes.size(); ++i)
4029 {
4030 if (dwarf_tag(&scopes[i]) == DW_TAG_compile_unit)
4031 break; // we don't want file-level variables
4032 if (dwarf_child (&scopes[i], &result) == 0)
4033 do
00cf3709 4034 {
d48bc7eb
JS
4035 switch (dwarf_tag (&result))
4036 {
4037 case DW_TAG_variable:
4038 if (e->name == "$$parms")
4039 continue;
4040 break;
4041 case DW_TAG_formal_parameter:
4042 if (e->name == "$$locals")
4043 continue;
4044 break;
4045
4046 default:
4047 continue;
4048 }
41c262f3 4049
d48bc7eb
JS
4050 const char *diename = dwarf_diename (&result);
4051 if (! diename) continue;
f76427a2 4052
d48bc7eb
JS
4053 if (! first)
4054 pf->raw_components += " ";
4055 pf->raw_components += diename;
fde50242
JS
4056 first = false;
4057
4058 // Write a placeholder for ugly aggregates
4059 Dwarf_Die type;
4060 if (!pretty && dwarf_attr_die(&result, DW_AT_type, &type))
4061 {
4062 q.dw.resolve_unqualified_inner_typedie(&type, &type, e);
4063 switch (dwarf_tag(&type))
4064 {
4065 case DW_TAG_union_type:
4066 case DW_TAG_structure_type:
4067 case DW_TAG_class_type:
4068 pf->raw_components += "={...}";
4069 continue;
4070
4071 case DW_TAG_array_type:
4072 pf->raw_components += "=[...]";
4073 continue;
4074 }
4075 }
345bbb3d 4076
d48bc7eb
JS
4077 tsym->name = "$";
4078 tsym->name += diename;
41c262f3 4079
d48bc7eb
JS
4080 // Ignore any variable that isn't accessible.
4081 tsym->saved_conversion_error = 0;
4082 expression *texp = tsym;
4083 replace (texp); // NB: throws nothing ...
4084 if (tsym->saved_conversion_error) // ... but this is how we know it happened.
a43ba433 4085 {
d48bc7eb
JS
4086 if (q.sess.verbose>2)
4087 {
e26c2f83 4088 for (const semantic_error *c = tsym->saved_conversion_error;
d48bc7eb 4089 c != 0;
c081af73 4090 c = c->get_chain()) {
4c5d9906 4091 clog << _("variable location problem [man error::dwarf]: ") << c->what() << endl;
d48bc7eb
JS
4092 }
4093 }
4094
4095 pf->raw_components += "=?";
a43ba433 4096 }
d48bc7eb
JS
4097 else
4098 {
4099 pf->raw_components += format;
4100 pf->args.push_back(texp);
4101 }
a7999c82 4102 }
d48bc7eb
JS
4103 while (dwarf_siblingof (&result, &result) == 0);
4104 }
a7999c82 4105 }
2cb3fe26 4106
a7999c82 4107 pf->components = print_format::string_to_components(pf->raw_components);
140be17a 4108 pf->type = pe_string;
a7999c82
JS
4109 provide (pf);
4110}
4111
2cb3fe26 4112
bd1fcbad
YZ
4113void
4114dwarf_var_expanding_visitor::visit_atvar_op (atvar_op *e)
4115{
4116 // Fill in our current module context if needed
4117 if (e->module.empty())
4118 e->module = q.dw.module_name;
4119
4120 if (e->module == q.dw.module_name && e->cu_name.empty())
4121 {
4122 // process like any other local
4123 // e->sym_name() will do the right thing
4124 visit_target_symbol(e);
4125 return;
4126 }
4127
4128 var_expanding_visitor::visit_atvar_op(e);
4129}
4130
4131
a7999c82
JS
4132void
4133dwarf_var_expanding_visitor::visit_target_symbol (target_symbol *e)
4134{
bd1fcbad 4135 assert(e->name.size() > 0 && (e->name[0] == '$' || e->name == "@var"));
a7999c82 4136 visited = true;
30263a73
FCE
4137 bool defined_being_checked = (defined_ops.size() > 0 && (defined_ops.top()->operand == e));
4138 // In this mode, we avoid hiding errors or generating extra code such as for .return saved $vars
a7999c82 4139
70208613 4140 try
a7999c82 4141 {
c69a87e0
FCE
4142 bool lvalue = is_active_lvalue(e);
4143 if (lvalue && !q.sess.guru_mode)
dc09353a 4144 throw SEMANTIC_ERROR(_("write to target variable not permitted; need stap -g"), e->tok);
2cb3fe26 4145
100a540e 4146 // XXX: process $context vars should be writable
70208613 4147
c69a87e0
FCE
4148 // See if we need to generate a new probe to save/access function
4149 // parameters from a return probe. PR 1382.
4150 if (q.has_return
4151 && !defined_being_checked
277c21bc
JS
4152 && e->name != "$return" // not the special return-value variable handled below
4153 && e->name != "$$return") // nor the other special variable handled below
c69a87e0
FCE
4154 {
4155 if (lvalue)
dc09353a 4156 throw SEMANTIC_ERROR(_("write to target variable not permitted in .return probes"), e->tok);
c69a87e0
FCE
4157 visit_target_symbol_saved_return(e);
4158 return;
4159 }
e57b735a 4160
277c21bc
JS
4161 if (e->name == "$$vars" || e->name == "$$parms" || e->name == "$$locals"
4162 || (q.has_return && (e->name == "$$return")))
c69a87e0
FCE
4163 {
4164 if (lvalue)
dc09353a 4165 throw SEMANTIC_ERROR(_("cannot write to context variable"), e->tok);
70208613 4166
c69a87e0 4167 if (e->addressof)
dc09353a 4168 throw SEMANTIC_ERROR(_("cannot take address of context variable"), e->tok);
70208613 4169
5f36109e
JS
4170 e->assert_no_components("dwarf", true);
4171
c69a87e0
FCE
4172 visit_target_symbol_context(e);
4173 return;
4174 }
70208613 4175
04c8e51b
JL
4176 // Everything else (pretty-printed vars, and context vars) require a
4177 // scope_die in which to search for them. If we don't have that, just
4178 // leave it unresolved; we'll produce an error later on.
4179 if (null_die(scope_die))
4180 {
4181 provide(e);
4182 return;
4183 }
4184
c4965ad9 4185 if (e->check_pretty_print (lvalue))
5f36109e 4186 {
277c21bc 4187 if (q.has_return && (e->name == "$return"))
5f36109e
JS
4188 {
4189 dwarf_pretty_print dpp (q.dw, scope_die, addr,
4190 q.has_process, *e);
4191 dpp.expand()->visit(this);
4192 }
4193 else
4194 {
4195 dwarf_pretty_print dpp (q.dw, getscopes(e), addr,
cc9001af 4196 e->sym_name(),
5f36109e
JS
4197 q.has_process, *e);
4198 dpp.expand()->visit(this);
4199 }
4200 return;
4201 }
4202
1c0be8c7 4203 bool userspace_p = q.has_process;
c69a87e0 4204 string fname = (string(lvalue ? "_dwarf_tvar_set" : "_dwarf_tvar_get")
cc9001af 4205 + "_" + e->sym_name()
c69a87e0 4206 + "_" + lex_cast(tick++));
70208613 4207
70208613 4208
1c0be8c7 4209 string code;
40a393cd 4210 Dwarf_Die endtype;
277c21bc 4211 if (q.has_return && (e->name == "$return"))
40a393cd 4212 code = q.dw.literal_stmt_for_return (scope_die, addr, e, lvalue, &endtype);
e19fda4e 4213 else
1c0be8c7 4214 code = q.dw.literal_stmt_for_local (getscopes(e), addr, e->sym_name(),
40a393cd 4215 e, lvalue, &endtype);
70208613 4216
40a393cd 4217 functioncall* n = synthetic_embedded_deref_call(q.dw, &endtype, fname, code,
1c0be8c7 4218 userspace_p, lvalue, e);
70208613 4219
c69a87e0 4220 if (lvalue)
f55efafe 4221 provide_lvalue_call (n);
70208613 4222
1c0be8c7
JS
4223 // Revisit the functioncall so arguments can be expanded.
4224 n->visit (this);
66d284f4
FCE
4225 }
4226 catch (const semantic_error& er)
4227 {
9fab2262
JS
4228 // We suppress this error message, and pass the unresolved
4229 // target_symbol to the next pass. We hope that this value ends
4230 // up not being referenced after all, so it can be optimized out
4231 // quietly.
1af1e62d 4232 e->chain (er);
9fab2262 4233 provide (e);
66d284f4 4234 }
77de5e9e
GH
4235}
4236
4237
c24447be
JS
4238void
4239dwarf_var_expanding_visitor::visit_cast_op (cast_op *e)
4240{
4241 // Fill in our current module context if needed
4242 if (e->module.empty())
4243 e->module = q.dw.module_name;
4244
4245 var_expanding_visitor::visit_cast_op(e);
4246}
4247
4248
8cc799a5
JS
4249void
4250dwarf_var_expanding_visitor::visit_entry_op (entry_op *e)
4251{
4252 expression *repl = e;
4253 if (q.has_return)
4254 {
4255 // expand the operand as if it weren't a return probe
4256 q.has_return = false;
4257 replace (e->operand);
4258 q.has_return = true;
4259
4260 // XXX it would be nice to use gen_kretprobe_saved_return when available,
4261 // but it requires knowing the types already, which is problematic for
4262 // arbitrary expressons.
cc9001af 4263 repl = gen_mapped_saved_return (e->operand, "entry");
8cc799a5
JS
4264 }
4265 provide (repl);
4266}
4267
3689db05
SC
4268void
4269dwarf_var_expanding_visitor::visit_perf_op (perf_op *e)
4270{
ace7c23f 4271 string e_lit_val = e->operand->value;
b78a0fbb 4272
3689db05 4273 add_block = new block;
9eaaceaa 4274 add_block->tok = e->tok;
3689db05
SC
4275
4276 systemtap_session &s = this->q.sess;
aa2c662f 4277 std::vector<std::pair<std::string,std::string> >::iterator it;
3689db05
SC
4278 // Find the associated perf.counter probe
4279 for (it=s.perf_counters.begin();
4280 it != s.perf_counters.end();
4281 it++)
4282 if ((*it).first == e_lit_val)
4283 {
aa2c662f
SC
4284 // if perf .process("name") omitted, then set it to this process name
4285 if ((*it).second.length() == 0)
4286 (*it).second = this->q.user_path;
4287 if ((*it).second == this->q.user_path)
3689db05
SC
4288 break;
4289 }
4290
4291 if (it != s.perf_counters.end())
4292 {
aa2c662f 4293 perf_counter_refs.insert((*it).first);
3689db05
SC
4294 // __perf_read_N is assigned in the probe prologue
4295 symbol* sym = new symbol;
4653caf1 4296 sym->tok = e->tok;
3689db05
SC
4297 sym->name = "__perf_read_" + (*it).first;
4298 provide (sym);
4299 }
4300 else
dc09353a 4301 throw SEMANTIC_ERROR(_F("perf counter '%s' not defined", e_lit_val.c_str()));
3689db05
SC
4302}
4303
8cc799a5 4304
729455a7
JS
4305vector<Dwarf_Die>&
4306dwarf_var_expanding_visitor::getscopes(target_symbol *e)
4307{
4308 if (scopes.empty())
4309 {
04c8e51b 4310 if(!null_die(scope_die))
f25a9197 4311 scopes = q.dw.getscopes(scope_die);
729455a7 4312 if (scopes.empty())
b530b5b3
LB
4313 //throw semantic_error (_F("unable to find any scopes containing %d", addr), e->tok);
4314 // ((scope_die == NULL) ? "" : (string (" in ") + (dwarf_diename(scope_die) ?: "<unknown>") + "(" + (dwarf_diename(q.dw.cu) ?: "<unknown>") ")" ))
dc09353a 4315 throw SEMANTIC_ERROR ("unable to find any scopes containing "
729455a7 4316 + lex_cast_hex(addr)
04c8e51b 4317 + (null_die(scope_die) ? ""
729455a7
JS
4318 : (string (" in ")
4319 + (dwarf_diename(scope_die) ?: "<unknown>")
4320 + "(" + (dwarf_diename(q.dw.cu) ?: "<unknown>")
4321 + ")"))
4322 + " while searching for local '"
cc9001af 4323 + e->sym_name() + "'",
729455a7
JS
4324 e->tok);
4325 }
4326 return scopes;
4327}
4328
4329
5f36109e
JS
4330struct dwarf_cast_expanding_visitor: public var_expanding_visitor
4331{
4332 systemtap_session& s;
4333 dwarf_builder& db;
d77be314 4334 map<string,string> compiled_headers;
5f36109e
JS
4335
4336 dwarf_cast_expanding_visitor(systemtap_session& s, dwarf_builder& db):
4337 s(s), db(db) {}
4338 void visit_cast_op (cast_op* e);
4339 void filter_special_modules(string& module);
4340};
4341
4342
c4ce66a1
JS
4343struct dwarf_cast_query : public base_query
4344{
946e1a48 4345 cast_op& e;
c4ce66a1 4346 const bool lvalue;
5f36109e
JS
4347 const bool userspace_p;
4348 functioncall*& result;
c4ce66a1 4349
5f36109e
JS
4350 dwarf_cast_query(dwflpp& dw, const string& module, cast_op& e, bool lvalue,
4351 const bool userspace_p, functioncall*& result):
abb41d92 4352 base_query(dw, module), e(e), lvalue(lvalue),
5f36109e 4353 userspace_p(userspace_p), result(result) {}
c4ce66a1
JS
4354
4355 void handle_query_module();
822a6a3d 4356 void query_library (const char *) {}
576eaefe 4357 void query_plt (const char *entry, size_t addr) {}
c4ce66a1
JS
4358};
4359
4360
c4ce66a1
JS
4361void
4362dwarf_cast_query::handle_query_module()
4363{
5f36109e
JS
4364 static unsigned tick = 0;
4365
4366 if (result)
c4ce66a1
JS
4367 return;
4368
ea1e477a 4369 // look for the type in any CU
a44a7cb5
JS
4370 Dwarf_Die* type_die = NULL;
4371 if (startswith(e.type_name, "class "))
4372 {
4373 // normalize to match dwflpp::global_alias_caching_callback
4374 string struct_name = "struct " + e.type_name.substr(6);
4375 type_die = dw.declaration_resolve_other_cus(struct_name);
4376 }
4377 else
4378 type_die = dw.declaration_resolve_other_cus(e.type_name);
4379
4380 // NB: We now index the types as "struct name"/"union name"/etc. instead of
4381 // just "name". But since we didn't require users to be explicit before, and
4382 // actually sort of discouraged it, we must be flexible now. So if a lookup
4383 // fails with a bare name, try augmenting it.
4384 if (!type_die &&
4385 !startswith(e.type_name, "class ") &&
4386 !startswith(e.type_name, "struct ") &&
4387 !startswith(e.type_name, "union ") &&
4388 !startswith(e.type_name, "enum "))
4389 {
4390 type_die = dw.declaration_resolve_other_cus("struct " + e.type_name);
4391 if (!type_die)
4392 type_die = dw.declaration_resolve_other_cus("union " + e.type_name);
4393 if (!type_die)
4394 type_die = dw.declaration_resolve_other_cus("enum " + e.type_name);
4395 }
4396
ea1e477a
JS
4397 if (!type_die)
4398 return;
c4ce66a1 4399
5f36109e 4400 string code;
40a393cd 4401 Dwarf_Die endtype;
5f36109e 4402
ea1e477a 4403 try
c4ce66a1 4404 {
ea1e477a
JS
4405 Dwarf_Die cu_mem;
4406 dw.focus_on_cu(dwarf_diecu(type_die, &cu_mem, NULL, NULL));
5f36109e 4407
c4965ad9 4408 if (e.check_pretty_print (lvalue))
5f36109e 4409 {
d19a9a82 4410 dwarf_pretty_print dpp(dw, type_die, e.operand, true, userspace_p, e);
5f36109e
JS
4411 result = dpp.expand();
4412 return;
4413 }
4414
40a393cd 4415 code = dw.literal_stmt_for_pointer (type_die, &e, lvalue, &endtype);
ea1e477a
JS
4416 }
4417 catch (const semantic_error& er)
4418 {
4419 // NB: we can have multiple errors, since a @cast
1af1e62d
JS
4420 // may be attempted using several different modules:
4421 // @cast(ptr, "type", "module1:module2:...")
4422 e.chain (er);
c4ce66a1 4423 }
c4ce66a1 4424
5f36109e
JS
4425 if (code.empty())
4426 return;
c4ce66a1 4427
5f36109e 4428 string fname = (string(lvalue ? "_dwarf_cast_set" : "_dwarf_cast_get")
cc9001af 4429 + "_" + e.sym_name()
5f36109e 4430 + "_" + lex_cast(tick++));
c4ce66a1 4431
40a393cd 4432 result = synthetic_embedded_deref_call(dw, &endtype, fname, code,
1c0be8c7 4433 userspace_p, lvalue, &e, e.operand);
5f36109e 4434}
c4ce66a1
JS
4435
4436
fb0274bc
JS
4437void dwarf_cast_expanding_visitor::filter_special_modules(string& module)
4438{
d90053e7 4439 // look for "<path/to/header>" or "kernel<path/to/header>"
fb0274bc 4440 // for those cases, build a module including that header
d90053e7 4441 if (module[module.size() - 1] == '>' &&
60d98537 4442 (module[0] == '<' || startswith(module, "kernel<")))
fb0274bc 4443 {
d77be314
JS
4444 string header = module;
4445 map<string,string>::const_iterator it = compiled_headers.find(header);
4446 if (it != compiled_headers.end())
4447 {
4448 module = it->second;
4449 return;
4450 }
4451
fb0274bc
JS
4452 string cached_module;
4453 if (s.use_cache)
4454 {
4455 // see if the cached module exists
a2639cb7 4456 cached_module = find_typequery_hash(s, module);
d105f664 4457 if (!cached_module.empty() && !s.poison_cache)
fb0274bc
JS
4458 {
4459 int fd = open(cached_module.c_str(), O_RDONLY);
4460 if (fd != -1)
4461 {
4462 if (s.verbose > 2)
b530b5b3
LB
4463 //TRANSLATORS: Here we're using a cached module.
4464 clog << _("Pass 2: using cached ") << cached_module << endl;
d77be314 4465 compiled_headers[header] = module = cached_module;
fb0274bc
JS
4466 close(fd);
4467 return;
4468 }
4469 }
4470 }
4471
4472 // no cached module, time to make it
d90053e7 4473 if (make_typequery(s, module) == 0)
fb0274bc 4474 {
e16dc041 4475 // try to save typequery in the cache
fb0274bc 4476 if (s.use_cache)
e16dc041 4477 copy_file(module, cached_module, s.verbose > 2);
d77be314 4478 compiled_headers[header] = module;
fb0274bc
JS
4479 }
4480 }
4481}
4482
4483
c4ce66a1
JS
4484void dwarf_cast_expanding_visitor::visit_cast_op (cast_op* e)
4485{
4486 bool lvalue = is_active_lvalue(e);
4487 if (lvalue && !s.guru_mode)
dc09353a 4488 throw SEMANTIC_ERROR(_("write to @cast context variable not permitted; need stap -g"), e->tok);
c4ce66a1
JS
4489
4490 if (e->module.empty())
4491 e->module = "kernel"; // "*" may also be reasonable to search all kernel modules
4492
5f36109e 4493 functioncall* result = NULL;
8b31197b
JS
4494
4495 // split the module string by ':' for alternatives
4496 vector<string> modules;
4497 tokenize(e->module, modules, ":");
b5a0dd41 4498 bool userspace_p=false; // PR10601
5f36109e 4499 for (unsigned i = 0; !result && i < modules.size(); ++i)
c4ce66a1 4500 {
8b31197b 4501 string& module = modules[i];
fb0274bc 4502 filter_special_modules(module);
abb41d92 4503
c4ce66a1
JS
4504 // NB: This uses '/' to distinguish between kernel modules and userspace,
4505 // which means that userspace modules won't get any PATH searching.
4506 dwflpp* dw;
707bf35e
JS
4507 try
4508 {
b5a0dd41
FCE
4509 userspace_p=is_user_module (module);
4510 if (! userspace_p)
707bf35e
JS
4511 {
4512 // kernel or kernel module target
ae2552da 4513 dw = db.get_kern_dw(s, module);
707bf35e
JS
4514 }
4515 else
4516 {
05fb3e0c 4517 module = find_executable (module, "", s.sysenv); // canonicalize it
707bf35e
JS
4518 dw = db.get_user_dw(s, module);
4519 }
4520 }
4521 catch (const semantic_error& er)
4522 {
4523 /* ignore and go to the next module */
4524 continue;
4525 }
c4ce66a1 4526
5f36109e 4527 dwarf_cast_query q (*dw, module, *e, lvalue, userspace_p, result);
06de3a04 4528 dw->iterate_over_modules<base_query>(&query_module, &q);
c4ce66a1 4529 }
abb41d92 4530
5f36109e 4531 if (!result)
c4ce66a1 4532 {
946e1a48
JS
4533 // We pass the unresolved cast_op to the next pass, and hope
4534 // that this value ends up not being referenced after all, so
4535 // it can be optimized out quietly.
c4ce66a1
JS
4536 provide (e);
4537 return;
4538 }
4539
c4ce66a1 4540 if (lvalue)
f55efafe 4541 provide_lvalue_call (result);
c4ce66a1 4542
5f36109e 4543 result->visit (this);
77de5e9e
GH
4544}
4545
4546
37efef73
JS
4547static bool resolve_pointer_type(Dwarf_Die& die, bool& isptr);
4548
4549exp_type_dwarf::exp_type_dwarf(dwflpp* dw, Dwarf_Die* die,
4550 bool userspace_p, bool addressof):
4551 dw(dw), die(*die), userspace_p(userspace_p), is_pointer(false)
4552{
4553 // is_pointer tells us whether a value is a pointer to the given type, so we
4554 // can dereference it; otherwise it will be treated as an end point.
4555 if (addressof)
4556 // we're already looking at the pointed-to type
4557 is_pointer = true;
4558 else
4559 // use the same test as tracepoints to see what we have
4560 resolve_pointer_type(this->die, is_pointer);
4561}
4562
4563
66facfa3
JS
4564functioncall *
4565exp_type_dwarf::expand(autocast_op* e, bool lvalue)
251707c8 4566{
66facfa3 4567 static unsigned tick = 0;
251707c8 4568
66facfa3
JS
4569 try
4570 {
37efef73
JS
4571 // make sure we're not dereferencing base types or void
4572 bool deref_p = is_pointer && !null_die(&die);
4573 if (!deref_p)
4574 e->assert_no_components("autocast", true);
4575
fb437f4e
JS
4576 if (lvalue && !dw->sess.guru_mode)
4577 throw SEMANTIC_ERROR(_("write not permitted; need stap -g"), e->tok);
4578
37efef73
JS
4579 if (e->components.empty())
4580 {
4581 if (e->addressof)
4582 throw SEMANTIC_ERROR(_("cannot take address of tracepoint variable"), e->tok);
4583
4584 // no components and no addressof? how did this autocast come to be?
4585 throw SEMANTIC_ERROR(_("internal error: no-op autocast encountered"), e->tok);
4586 }
4587
66facfa3 4588 Dwarf_Die cu_mem;
611a848e
JS
4589 if (!null_die(&die))
4590 dw->focus_on_cu(dwarf_diecu(&die, &cu_mem, NULL, NULL));
251707c8 4591
c4965ad9 4592 if (e->check_pretty_print (lvalue))
66facfa3 4593 {
37efef73 4594 dwarf_pretty_print dpp(*dw, &die, e->operand, deref_p, userspace_p, *e);
66facfa3
JS
4595 return dpp.expand();
4596 }
4597
4598 Dwarf_Die endtype;
4599 string code = dw->literal_stmt_for_pointer (&die, e, lvalue, &endtype);
251707c8 4600
66facfa3
JS
4601 string fname = (string(lvalue ? "_dwarf_autocast_set" : "_dwarf_autocast_get")
4602 + "_" + lex_cast(tick++));
4603
4604 return synthetic_embedded_deref_call(*dw, &endtype, fname, code,
4605 userspace_p, lvalue, e, e->operand);
4606 }
4607 catch (const semantic_error &er)
4608 {
4609 e->chain (er);
4610 return NULL;
4611 }
251707c8
JS
4612}
4613
4614
bd1fcbad
YZ
4615struct dwarf_atvar_expanding_visitor: public var_expanding_visitor
4616{
4617 systemtap_session& s;
4618 dwarf_builder& db;
4619
4620 dwarf_atvar_expanding_visitor(systemtap_session& s, dwarf_builder& db):
4621 s(s), db(db) {}
4622 void visit_atvar_op (atvar_op* e);
4623};
4624
4625
4626struct dwarf_atvar_query: public base_query
4627{
4628 atvar_op& e;
4629 const bool userspace_p, lvalue;
4630 functioncall*& result;
4631 unsigned& tick;
4632 const string cu_name_pattern;
4633
4634 dwarf_atvar_query(dwflpp& dw, const string& module, atvar_op& e,
4635 const bool userspace_p, const bool lvalue,
4636 functioncall*& result,
4637 unsigned& tick):
4638 base_query(dw, module), e(e),
4639 userspace_p(userspace_p), lvalue(lvalue), result(result),
4640 tick(tick), cu_name_pattern(string("*/") + e.cu_name) {}
4641
4642 void handle_query_module ();
4643 void query_library (const char *) {}
4644 void query_plt (const char *entry, size_t addr) {}
5c378838 4645 static int atvar_query_cu (Dwarf_Die *cudie, dwarf_atvar_query *q);
bd1fcbad
YZ
4646};
4647
4648
4649int
5c378838 4650dwarf_atvar_query::atvar_query_cu (Dwarf_Die * cudie, dwarf_atvar_query *q)
bd1fcbad 4651{
bd1fcbad
YZ
4652 if (! q->e.cu_name.empty())
4653 {
c60517ca 4654 const char *die_name = dwarf_diename(cudie) ?: "";
bd1fcbad
YZ
4655 if (strcmp(die_name, q->e.cu_name.c_str()) != 0 // Perfect match
4656 && fnmatch(q->cu_name_pattern.c_str(), die_name, 0) != 0)
4657 {
4658 return DWARF_CB_OK;
4659 }
4660 }
4661
4662 try
4663 {
4664 vector<Dwarf_Die> scopes(1, *cudie);
4665
4666 q->dw.focus_on_cu (cudie);
4667
c4965ad9 4668 if (q->e.check_pretty_print (q->lvalue))
bd1fcbad
YZ
4669 {
4670 dwarf_pretty_print dpp (q->dw, scopes, 0, q->e.sym_name(),
4671 q->userspace_p, q->e);
4672 q->result = dpp.expand();
4673 return DWARF_CB_ABORT;
4674 }
4675
40a393cd 4676 Dwarf_Die endtype;
bd1fcbad 4677 string code = q->dw.literal_stmt_for_local (scopes, 0, q->e.sym_name(),
40a393cd 4678 &q->e, q->lvalue, &endtype);
bd1fcbad
YZ
4679
4680 if (code.empty())
4681 return DWARF_CB_OK;
4682
4683 string fname = (string(q->lvalue ? "_dwarf_tvar_set"
4684 : "_dwarf_tvar_get")
4685 + "_" + q->e.sym_name()
4686 + "_" + lex_cast(q->tick++));
4687
40a393cd 4688 q->result = synthetic_embedded_deref_call (q->dw, &endtype, fname, code,
bd1fcbad
YZ
4689 q->userspace_p, q->lvalue,
4690 &q->e);
4691 }
4692 catch (const semantic_error& er)
4693 {
4694 // Here we suppress the error because we often just have too many
4695 // when scanning all the CUs.
4696 return DWARF_CB_OK;
4697 }
4698
4699 if (q->result) {
4700 return DWARF_CB_ABORT;
4701 }
4702
4703 return DWARF_CB_OK;
4704}
4705
4706
4707void
4708dwarf_atvar_query::handle_query_module ()
4709{
4710
4711 dw.iterate_over_cus(atvar_query_cu, this, false);
4712}
4713
4714
4715void
4716dwarf_atvar_expanding_visitor::visit_atvar_op (atvar_op* e)
4717{
4718 const bool lvalue = is_active_lvalue(e);
4719 if (lvalue && !s.guru_mode)
dc09353a 4720 throw SEMANTIC_ERROR(_("write to @var variable not permitted; "
bd1fcbad
YZ
4721 "need stap -g"), e->tok);
4722
4723 if (e->module.empty())
4724 e->module = "kernel";
4725
4726 functioncall* result = NULL;
4727
4728 // split the module string by ':' for alternatives
4729 vector<string> modules;
4730 tokenize(e->module, modules, ":");
4731 bool userspace_p = false;
4732 for (unsigned i = 0; !result && i < modules.size(); ++i)
4733 {
4734 string& module = modules[i];
4735
4736 dwflpp* dw;
4737 try
4738 {
4739 userspace_p = is_user_module(module);
4740 if (!userspace_p)
4741 {
4742 // kernel or kernel module target
4743 dw = db.get_kern_dw(s, module);
4744 }
4745 else
4746 {
4747 module = find_executable(module, "", s.sysenv);
4748 dw = db.get_user_dw(s, module);
4749 }
4750 }
4751 catch (const semantic_error& er)
4752 {
4753 /* ignore and go to the next module */
4754 continue;
4755 }
4756
4757 dwarf_atvar_query q (*dw, module, *e, userspace_p, lvalue, result, tick);
06de3a04 4758 dw->iterate_over_modules<base_query>(&query_module, &q);
bd1fcbad
YZ
4759
4760 if (result)
4761 {
4762 s.unwindsym_modules.insert(module);
bd1fcbad
YZ
4763
4764 if (lvalue)
f55efafe 4765 provide_lvalue_call (result);
bd1fcbad
YZ
4766
4767 result->visit(this);
4768 return;
4769 }
4770
4771 /* Unable to find the variable in the current module, so we chain
4772 * an error in atvar_op */
dc09353a 4773 semantic_error er(ERR_SRC, _F("unable to find global '%s' in %s%s%s",
bd1fcbad
YZ
4774 e->sym_name().c_str(), module.c_str(),
4775 e->cu_name.empty() ? "" : _(", in "),
4776 e->cu_name.c_str()));
4777 e->chain (er);
4778 }
4779
4780 provide(e);
4781}
4782
4783
b8da0ad1
FCE
4784void
4785dwarf_derived_probe::printsig (ostream& o) const
4786{
4787 // Instead of just printing the plain locations, we add a PC value
4788 // as a comment as a way of telling e.g. apart multiple inlined
4789 // function instances. This is distinct from the verbose/clog
4790 // output, since this part goes into the cache hash calculations.
4791 sole_location()->print (o);
6d0f3f0c 4792 o << " /* pc=" << section << "+0x" << hex << addr << dec << " */";
b8da0ad1
FCE
4793 printsig_nested (o);
4794}
4795
4796
4797
dc38c0ae 4798void
b20febf3
FCE
4799dwarf_derived_probe::join_group (systemtap_session& s)
4800{
af234c40
JS
4801 // skip probes which are paired entry-handlers
4802 if (!has_return && (saved_longs || saved_strings))
4803 return;
4804
b20febf3
FCE
4805 if (! s.dwarf_derived_probes)
4806 s.dwarf_derived_probes = new dwarf_derived_probe_group ();
4807 s.dwarf_derived_probes->enroll (this);
ca6d3b0f
JL
4808 this->group = s.dwarf_derived_probes;
4809 if (has_return && entry_handler)
4810 entry_handler->group = s.dwarf_derived_probes;
b20febf3
FCE
4811}
4812
4813
2b69faaf
JS
4814static bool
4815kernel_supports_inode_uprobes(systemtap_session& s)
4816{
4817 // The arch-supports is new to the builtin inode-uprobes, so it makes a
4818 // reasonable indicator of the new API. Else we'll need an autoconf...
64e807c2 4819 // see also buildrun.cxx:kernel_built_uprobs()
2b69faaf
JS
4820 return (s.kernel_config["CONFIG_ARCH_SUPPORTS_UPROBES"] == "y"
4821 && s.kernel_config["CONFIG_UPROBES"] == "y");
4822}
4823
4824
3667d615
JS
4825static bool
4826kernel_supports_inode_uretprobes(systemtap_session& s)
4827{
766013af
JS
4828 // We need inode-uprobes first, then look for a sign of uretprobes. The only
4829 // non-static function at present is arch_uretprobe_hijack_return_addr.
3667d615 4830 return kernel_supports_inode_uprobes(s) &&
af9e147f 4831 (s.kernel_functions.count("arch_uretprobe_hijack_return_addr") > 0);
3667d615
JS
4832}
4833
4834
5261f7ab
DS
4835void
4836check_process_probe_kernel_support(systemtap_session& s)
4837{
4838 // If we've got utrace, we're good to go.
4839 if (s.kernel_config["CONFIG_UTRACE"] == "y")
4840 return;
4841
8c021542
DS
4842 // We don't have utrace. For process probes that aren't
4843 // uprobes-based, we just need the task_finder. The task_finder
b266d318
DS
4844 // needs CONFIG_TRACEPOINTS and specific tracepoints. There is a
4845 // specific autoconf test for its needs.
8c021542
DS
4846 //
4847 // We'll just require CONFIG_TRACEPOINTS here as a quick-and-dirty
4848 // approximation.
4849 if (! s.need_uprobes && s.kernel_config["CONFIG_TRACEPOINTS"] == "y")
4850 return;
4851
d3e959b0
DS
4852 // For uprobes-based process probes, we need the task_finder plus
4853 // the builtin inode-uprobes.
8c021542
DS
4854 if (s.need_uprobes
4855 && s.kernel_config["CONFIG_TRACEPOINTS"] == "y"
d3e959b0 4856 && kernel_supports_inode_uprobes(s))
8c021542
DS
4857 return;
4858
dc09353a 4859 throw SEMANTIC_ERROR (_("process probes not available without kernel CONFIG_UTRACE or CONFIG_TRACEPOINTS/CONFIG_ARCH_SUPPORTS_UPROBES/CONFIG_UPROBES"));
5261f7ab
DS
4860}
4861
4862
b20febf3
FCE
4863dwarf_derived_probe::dwarf_derived_probe(const string& funcname,
4864 const string& filename,
4865 int line,
91af0778 4866 // module & section specify a relocation
b20febf3
FCE
4867 // base for <addr>, unless section==""
4868 // (equivalently module=="kernel")
4766b1e6
JL
4869 // for userspace, it's a full path, for
4870 // modules, it's either a full path, or
4871 // the basename (e.g. 'btrfs')
b20febf3
FCE
4872 const string& module,
4873 const string& section,
4874 // NB: dwfl_addr is the virtualized
4875 // address for this symbol.
4876 Dwarf_Addr dwfl_addr,
4877 // addr is the section-offset for
4878 // actual relocation.
4879 Dwarf_Addr addr,
4880 dwarf_query& q,
37ebca01 4881 Dwarf_Die* scope_die /* may be null */)
4c5d1300 4882 : derived_probe (q.base_probe, q.base_loc, true /* .components soon rewritten */ ),
b20febf3 4883 module (module), section (section), addr (addr),
63b4fd14 4884 path (q.path),
27dc09b1 4885 has_process (q.has_process),
c9bad430
DS
4886 has_return (q.has_return),
4887 has_maxactive (q.has_maxactive),
c57ea854 4888 has_library (q.has_library),
6b66b9f7 4889 maxactive_val (q.maxactive_val),
b642c901
SC
4890 user_path (q.user_path),
4891 user_lib (q.user_lib),
af234c40 4892 access_vars(false),
c57ea854 4893 saved_longs(0), saved_strings(0),
af234c40 4894 entry_handler(0)
bd2b1e68 4895{
4766b1e6
JL
4896 // If we were given a fullpath to a kernel module, then get the simple name
4897 if (q.has_module && is_fully_resolved(module, q.dw.sess.sysroot, q.dw.sess.sysenv))
4898 this->module = modname_from_path(module);
4899
b642c901
SC
4900 if (user_lib.size() != 0)
4901 has_library = true;
4902
6b66b9f7
JS
4903 if (q.has_process)
4904 {
4905 // We may receive probes on two types of ELF objects: ET_EXEC or ET_DYN.
4906 // ET_EXEC ones need no further relocation on the addr(==dwfl_addr), whereas
4907 // ET_DYN ones do (addr += run-time mmap base address). We tell these apart
4908 // by the incoming section value (".absolute" vs. ".dynamic").
4909 // XXX Assert invariants here too?
2b69faaf
JS
4910
4911 // inode-uprobes needs an offset rather than an absolute VM address.
4441e344 4912 // ditto for userspace runtimes (dyninst)
ac3af990 4913 if ((kernel_supports_inode_uprobes(q.dw.sess) || q.dw.sess.runtime_usermode_p()) &&
2b69faaf
JS
4914 section == ".absolute" && addr == dwfl_addr &&
4915 addr >= q.dw.module_start && addr < q.dw.module_end)
4916 this->addr = addr - q.dw.module_start;
6b66b9f7
JS
4917 }
4918 else
4919 {
4920 // Assert kernel relocation invariants
4921 if (section == "" && dwfl_addr != addr) // addr should be absolute
dc09353a 4922 throw SEMANTIC_ERROR (_("missing relocation basis"), tok);
6b66b9f7 4923 if (section != "" && dwfl_addr == addr) // addr should be an offset
dc09353a 4924 throw SEMANTIC_ERROR (_("inconsistent relocation address"), tok);
6b66b9f7 4925 }
2930abc7 4926
21beacc9
FCE
4927 // XXX: hack for strange g++/gcc's
4928#ifndef USHRT_MAX
4929#define USHRT_MAX 32767
4930#endif
4931
606fd9c8 4932 // Range limit maxactive() value
6b66b9f7 4933 if (has_maxactive && (maxactive_val < 0 || maxactive_val > USHRT_MAX))
dc09353a 4934 throw SEMANTIC_ERROR (_F("maxactive value out of range [0,%s]",
b530b5b3 4935 lex_cast(USHRT_MAX).c_str()), q.base_loc->components.front()->tok);
606fd9c8 4936
a7696882
JL
4937 // Expand target variables in the probe body. Even if the scope_die is
4938 // invalid, we still want to expand things such as $$vars/$$parms/etc...
4939 // (PR15999, PR16473). Access to specific context vars e.g. $argc will not be
4940 // expanded and will produce an error during the typeresolution_info pass.
8fc05e57 4941 {
6b66b9f7 4942 // XXX: user-space deref's for q.has_process!
5a617dc6
JL
4943
4944 // PR14436: if we're expanding target variables in the probe body of a
4945 // .return probe, we need to make the expansion at the postprologue addr
4946 // instead (if any), which is then also the spot where the entry handler
4947 // probe is placed. (Note that at this point, a nonzero prologue_end
4948 // implies that it should be used, i.e. code is unoptimized).
4949 Dwarf_Addr handler_dwfl_addr = dwfl_addr;
4950 if (q.prologue_end != 0 && q.has_return)
4951 {
4952 handler_dwfl_addr = q.prologue_end;
4953 if (q.sess.verbose > 2)
4954 clog << _F("expanding .return vars at prologue_end (0x%s) "
4955 "rather than entrypc (0x%s)\n",
4956 lex_cast_hex(handler_dwfl_addr).c_str(),
4957 lex_cast_hex(dwfl_addr).c_str());
4958 }
4959 dwarf_var_expanding_visitor v (q, scope_die, handler_dwfl_addr);
8b095b45 4960 v.replace (this->body);
3689db05
SC
4961
4962 // Propagate perf.counters so we can emit later
4963 this->perf_counter_refs = v.perf_counter_refs;
4964 // Emit local var used to save the perf counter read value
aa2c662f 4965 std::set<string>::iterator pcii;
3689db05
SC
4966 for (pcii = v.perf_counter_refs.begin();
4967 pcii != v.perf_counter_refs.end(); pcii++)
4968 {
aa2c662f 4969 std::vector<std::pair<std::string,std::string> >::iterator it;
3689db05
SC
4970 // Find the associated perf counter probe
4971 for (it=q.sess.perf_counters.begin() ;
4972 it != q.sess.perf_counters.end();
4973 it++)
aa2c662f 4974 if ((*it).first == (*pcii))
3689db05
SC
4975 break;
4976 vardecl* vd = new vardecl;
4977 vd->name = "__perf_read_" + (*it).first;
4978 vd->tok = this->tok;
4979 vd->set_arity(0, this->tok);
4980 vd->type = pe_long;
4981 vd->synthetic = true;
4982 this->locals.push_back (vd);
4983 }
4984
4985
6b66b9f7
JS
4986 if (!q.has_process)
4987 access_vars = v.visited;
37ebca01
FCE
4988
4989 // If during target-variable-expanding the probe, we added a new block
4990 // of code, add it to the start of the probe.
4991 if (v.add_block)
ba6f838d 4992 this->body = new block(v.add_block, this->body);
2260f4e3
FCE
4993
4994 // If when target-variable-expanding the probe, we need to synthesize a
4995 // sibling function-entry probe. We don't go through the whole probe derivation
4996 // business (PR10642) that could lead to wildcard/alias resolution, or for that
4997 // dwarf-induced duplication.
4998 if (v.add_call_probe)
37ebca01 4999 {
2260f4e3
FCE
5000 assert (q.has_return && !q.has_call);
5001
5002 // We temporarily replace q.base_probe.
5003 statement* old_body = q.base_probe->body;
5004 q.base_probe->body = v.add_call_probe;
5005 q.has_return = false;
5006 q.has_call = true;
af234c40 5007
da23eceb 5008 if (q.has_process)
5a617dc6
JL
5009 {
5010 // Place handler probe at the same addr as where the vars were
5011 // expanded (which may not be the same addr as the one for the
5012 // main retprobe, PR14436).
5013 Dwarf_Addr handler_addr = addr;
5014 if (handler_dwfl_addr != dwfl_addr)
5015 // adjust section offset by prologue_end-entrypc
5016 handler_addr += handler_dwfl_addr - dwfl_addr;
5017 entry_handler = new uprobe_derived_probe (funcname, filename,
5018 line, module, section,
5019 handler_dwfl_addr,
5020 handler_addr, q,
5021 scope_die);
5022 }
da23eceb 5023 else
af234c40
JS
5024 entry_handler = new dwarf_derived_probe (funcname, filename, line,
5025 module, section, dwfl_addr,
5026 addr, q, scope_die);
5027
5028 saved_longs = entry_handler->saved_longs = v.saved_longs;
5029 saved_strings = entry_handler->saved_strings = v.saved_strings;
5030
5031 q.results.push_back (entry_handler);
2260f4e3
FCE
5032
5033 q.has_return = true;
5034 q.has_call = false;
5035 q.base_probe->body = old_body;
37ebca01 5036 }
a7696882
JL
5037 // Save the local variables for listing mode. If the scope_die is null,
5038 // local vars aren't accessible, so no need to invoke saveargs (PR10820).
bba368c5
JL
5039 if (!null_die(scope_die) &&
5040 q.sess.dump_mode == systemtap_session::dump_matched_probes_vars)
5041 saveargs(q, scope_die, dwfl_addr);
8fc05e57 5042 }
0a98fd42 5043
5d23847d 5044 // Reset the sole element of the "locations" vector as a
b20febf3
FCE
5045 // "reverse-engineered" form of the incoming (q.base_loc) probe
5046 // point. This allows a user to see what function / file / line
5047 // number any particular match of the wildcards.
919debfc
JL
5048
5049 vector<probe_point::component*> comps;
5050 if (q.has_kernel)
5051 comps.push_back (new probe_point::component(TOK_KERNEL));
5052 else if(q.has_module)
5053 comps.push_back (new probe_point::component(TOK_MODULE, new literal_string(module)));
5054 else if(q.has_process)
5055 comps.push_back (new probe_point::component(TOK_PROCESS, new literal_string(module)));
5056 else
5057 assert (0);
5058
5059 string fn_or_stmt;
5060 if (q.has_function_str || q.has_function_num)
e772a6e7 5061 fn_or_stmt = TOK_FUNCTION;
919debfc 5062 else
e772a6e7 5063 fn_or_stmt = TOK_STATEMENT;
919debfc
JL
5064
5065 if (q.has_function_str || q.has_statement_str)
5066 {
79954c0f 5067 string retro_name = q.final_function_name(funcname, filename.c_str(), line);
919debfc
JL
5068 comps.push_back
5069 (new probe_point::component
5070 (fn_or_stmt, new literal_string (retro_name)));
5071 }
5072 else if (q.has_function_num || q.has_statement_num)
5073 {
5074 Dwarf_Addr retro_addr;
5075 if (q.has_function_num)
5076 retro_addr = q.function_num_val;
5077 else
5078 retro_addr = q.statement_num_val;
5079 comps.push_back (new probe_point::component
5080 (fn_or_stmt,
5081 new literal_number(retro_addr, true)));
5082
5083 if (q.has_absolute)
5084 comps.push_back (new probe_point::component (TOK_ABSOLUTE));
5085 }
5086
5087 if (q.has_call)
5088 comps.push_back (new probe_point::component(TOK_CALL));
5089 if (q.has_exported)
5090 comps.push_back (new probe_point::component(TOK_EXPORTED));
5091 if (q.has_inline)
5092 comps.push_back (new probe_point::component(TOK_INLINE));
5093 if (has_return)
5094 comps.push_back (new probe_point::component(TOK_RETURN));
5095 if (has_maxactive)
5096 comps.push_back (new probe_point::component
5097 (TOK_MAXACTIVE, new literal_number(maxactive_val)));
5098
5099 // Overwrite it.
5100 this->sole_location()->components = comps;
e2941743
JL
5101
5102 // if it's a .callee[s[(N)]] call, add checks to the probe body so that the
5103 // user body is only 'triggered' when called from q.callers[N-1], which
5104 // itself is called from q.callers[N-2], etc... I.E.
5105 // callees(N) --> N elements in q.callers --> N checks against [u]stack(0..N-1)
5106 if ((q.has_callee || q.has_callees_num) && q.callers && !q.callers->empty())
5107 {
5108 if (q.sess.verbose > 2)
7761da8b 5109 clog << _F("adding caller checks for callee %s\n", funcname.c_str());
e2941743
JL
5110
5111 // Copy the stack and empty it out
5112 stack<Dwarf_Addr> callers(*q.callers);
5113 for (unsigned level = 1; !callers.empty(); level++,
5114 callers.pop())
5115 {
5116 Dwarf_Addr caller = callers.top();
5117
5118 // We first need to make the caller addr relocatable
5119 string caller_section;
5120 Dwarf_Addr caller_reloc;
5121 if (module == TOK_KERNEL)
5122 { // allow for relocatable kernel (see also add_probe_point())
5123 caller_reloc = caller - q.sess.sym_stext;
5124 caller_section = "_stext";
5125 }
5126 else
5127 caller_reloc = q.dw.relocate_address(caller,
5128 caller_section);
5129
5130 if (q.sess.verbose > 2)
7761da8b
JL
5131 clog << _F("adding caller check [u]stack(%d) == reloc(0x%s)\n",
5132 level, lex_cast_hex(caller_reloc).c_str());
e2941743
JL
5133
5134 // We want to add a statement like this:
6211aed9 5135 // if (!_caller_match(user, mod, sec, addr)) next;
e2941743
JL
5136 // Something similar is done in semantic_pass_conditions()
5137
5138 functioncall* check = new functioncall();
5139 check->tok = this->tok;
6211aed9 5140 check->function = "_caller_match";
e2941743
JL
5141 check->args.push_back(new literal_number(q.has_process));
5142 check->args[0]->tok = this->tok;
5143 check->args.push_back(new literal_number(level));
5144 check->args[1]->tok = this->tok;
4766b1e6 5145 check->args.push_back(new literal_string(this->module));
e2941743
JL
5146 check->args[2]->tok = this->tok;
5147 check->args.push_back(new literal_string(caller_section));
5148 check->args[3]->tok = this->tok;
5149 check->args.push_back(new literal_number(caller_reloc, true /* hex */));
5150 check->args[4]->tok = this->tok;
5151
5152 unary_expression* notexp = new unary_expression();
5153 notexp->tok = this->tok;
5154 notexp->op = "!";
5155 notexp->operand = check;
5156
5157 if_statement* ifs = new if_statement();
5158 ifs->tok = this->tok;
5159 ifs->thenblock = new next_statement();
5160 ifs->thenblock->tok = this->tok;
5161 ifs->elseblock = NULL;
5162 ifs->condition = notexp;
5163
5164 this->body = new block(ifs, this->body);
5165 }
5166 }
2930abc7
FCE
5167}
5168
bd2b1e68 5169
0a98fd42 5170void
8c67c337
JS
5171dwarf_derived_probe::saveargs(dwarf_query& q, Dwarf_Die* scope_die,
5172 Dwarf_Addr dwfl_addr)
0a98fd42 5173{
9aa8ffce 5174 if (null_die(scope_die))
0a98fd42 5175 return;
0a98fd42 5176
8c67c337 5177 bool verbose = q.sess.verbose > 2;
0a98fd42 5178
8c67c337 5179 if (verbose)
b530b5b3 5180 clog << _F("saveargs: examining '%s' (dieoffset: %#" PRIx64 ")\n", (dwarf_diename(scope_die)?: "unknown"), dwarf_dieoffset(scope_die));
0a98fd42 5181
8c67c337
JS
5182 if (has_return)
5183 {
5184 /* Only save the return value if it has a type. */
5185 string type_name;
5186 Dwarf_Die type_die;
5187 if (dwarf_attr_die (scope_die, DW_AT_type, &type_die) &&
5188 dwarf_type_name(&type_die, type_name))
5189 args.push_back("$return:"+type_name);
5190
5191 else if (verbose)
b530b5b3
LB
5192 clog << _F("saveargs: failed to retrieve type name for return value (dieoffset: %s)\n",
5193 lex_cast_hex(dwarf_dieoffset(scope_die)).c_str());
8c67c337 5194 }
d87623a1 5195
0a98fd42 5196 Dwarf_Die arg;
4ef35696
JS
5197 vector<Dwarf_Die> scopes = q.dw.getscopes(scope_die);
5198 for (unsigned i = 0; i < scopes.size(); ++i)
5199 {
5200 if (dwarf_tag(&scopes[i]) == DW_TAG_compile_unit)
5201 break; // we don't want file-level variables
5202 if (dwarf_child (&scopes[i], &arg) == 0)
5203 do
0a98fd42 5204 {
4ef35696
JS
5205 switch (dwarf_tag (&arg))
5206 {
5207 case DW_TAG_variable:
5208 case DW_TAG_formal_parameter:
5209 break;
0a98fd42 5210
4ef35696
JS
5211 default:
5212 continue;
5213 }
0a98fd42 5214
4ef35696
JS
5215 /* Ignore this local if it has no name. */
5216 const char *arg_name = dwarf_diename (&arg);
5217 if (!arg_name)
8c67c337
JS
5218 {
5219 if (verbose)
b530b5b3
LB
5220 clog << _F("saveargs: failed to retrieve name for local (dieoffset: %s)\n",
5221 lex_cast_hex(dwarf_dieoffset(&arg)).c_str());
8c67c337
JS
5222 continue;
5223 }
4ef35696
JS
5224
5225 if (verbose)
b530b5b3
LB
5226 clog << _F("saveargs: finding location for local '%s' (dieoffset: %s)\n",
5227 arg_name, lex_cast_hex(dwarf_dieoffset(&arg)).c_str());
4ef35696
JS
5228
5229 /* Ignore this local if it has no location (or not at this PC). */
5230 /* NB: It still may not be directly accessible, e.g. if it is an
5231 * aggregate type, implicit_pointer, etc., but the user can later
5232 * figure out how to access the interesting parts. */
45b02a36
FCE
5233
5234 /* XXX: Perhaps saveargs() / listings-mode should work by synthesizing
5235 * several synthetic
5236 * probe foo { $var }
5237 * probes, testing them for overall resolvability.
5238 */
5239
4ef35696
JS
5240 Dwarf_Attribute attr_mem;
5241 if (!dwarf_attr_integrate (&arg, DW_AT_const_value, &attr_mem))
5242 {
5243 Dwarf_Op *expr;
5244 size_t len;
5245 if (!dwarf_attr_integrate (&arg, DW_AT_location, &attr_mem))
5246 {
5247 if (verbose)
b530b5b3
LB
5248 clog << _F("saveargs: failed to resolve the location for local '%s' (dieoffset: %s)\n",
5249 arg_name, lex_cast_hex(dwarf_dieoffset(&arg)).c_str());
4ef35696
JS
5250 continue;
5251 }
5252 else if (!(dwarf_getlocation_addr(&attr_mem, dwfl_addr, &expr,
5253 &len, 1) == 1 && len > 0))
5254 {
45b02a36
FCE
5255 Dwarf_Addr dwfl_addr2 = q.dw.pr15123_retry_addr (dwfl_addr, & arg);
5256 if (!dwfl_addr2 || (!(dwarf_getlocation_addr(&attr_mem, dwfl_addr2, &expr,
5257 &len, 1) == 1 && len > 0))) {
5258 if (verbose)
5259 clog << _F("saveargs: local '%s' (dieoffset: %s) is not available at this address (%s)\n",
5260 arg_name, lex_cast_hex(dwarf_dieoffset(&arg)).c_str(), lex_cast_hex(dwfl_addr).c_str());
5261 continue;
5262 }
4ef35696
JS
5263 }
5264 }
5265
5266 /* Ignore this local if it has no type. */
5267 string type_name;
5268 Dwarf_Die type_die;
5269 if (!dwarf_attr_die (&arg, DW_AT_type, &type_die) ||
5270 !dwarf_type_name(&type_die, type_name))
8c67c337
JS
5271 {
5272 if (verbose)
b530b5b3
LB
5273 clog << _F("saveargs: failed to retrieve type name for local '%s' (dieoffset: %s)\n",
5274 arg_name, lex_cast_hex(dwarf_dieoffset(&arg)).c_str());
8c67c337
JS
5275 continue;
5276 }
8c67c337 5277
4ef35696
JS
5278 /* This local looks good -- save it! */
5279 args.push_back("$"+string(arg_name)+":"+type_name);
8c67c337 5280 }
4ef35696
JS
5281 while (dwarf_siblingof (&arg, &arg) == 0);
5282 }
0a98fd42
JS
5283}
5284
5285
5286void
d0bfd2ac 5287dwarf_derived_probe::getargs(std::list<std::string> &arg_set) const
0a98fd42 5288{
d0bfd2ac 5289 arg_set.insert(arg_set.end(), args.begin(), args.end());
0a98fd42
JS
5290}
5291
5292
27dc09b1 5293void
42e38653 5294dwarf_derived_probe::emit_privilege_assertion (translator_output* o)
27dc09b1
DB
5295{
5296 if (has_process)
5297 {
5298 // These probes are allowed for unprivileged users, but only in the
5299 // context of processes which they own.
5300 emit_process_owner_assertion (o);
5301 return;
5302 }
5303
5304 // Other probes must contain the default assertion which aborts
5305 // if executed by an unprivileged user.
42e38653 5306 derived_probe::emit_privilege_assertion (o);
27dc09b1
DB
5307}
5308
5309
5310void
5311dwarf_derived_probe::print_dupe_stamp(ostream& o)
5312{
5313 if (has_process)
5314 {
5315 // These probes are allowed for unprivileged users, but only in the
5316 // context of processes which they own.
5317 print_dupe_stamp_unprivileged_process_owner (o);
5318 return;
5319 }
5320
5321 // Other probes must contain the default dupe stamp
5322 derived_probe::print_dupe_stamp (o);
5323}
5324
64211010 5325
7a053d3b 5326void
20c6c071 5327dwarf_derived_probe::register_statement_variants(match_node * root,
27dc09b1 5328 dwarf_builder * dw,
42e38653 5329 privilege_t privilege)
bd2b1e68 5330{
27dc09b1 5331 root
42e38653 5332 ->bind_privilege(privilege)
27dc09b1 5333 ->bind(dw);
83eeb3ac
HK
5334 root->bind(TOK_NEAREST)
5335 ->bind_privilege(privilege)
5336 ->bind(dw);
54efe513
GH
5337}
5338
7a053d3b 5339void
fd6602a0 5340dwarf_derived_probe::register_function_variants(match_node * root,
27dc09b1 5341 dwarf_builder * dw,
42e38653 5342 privilege_t privilege)
2865d17a 5343{
27dc09b1 5344 root
42e38653 5345 ->bind_privilege(privilege)
27dc09b1 5346 ->bind(dw);
27dc09b1 5347 root->bind(TOK_CALL)
42e38653 5348 ->bind_privilege(privilege)
27dc09b1 5349 ->bind(dw);
4bda987e
SC
5350 root->bind(TOK_EXPORTED)
5351 ->bind_privilege(privilege)
5352 ->bind(dw);
27dc09b1 5353 root->bind(TOK_RETURN)
42e38653 5354 ->bind_privilege(privilege)
27dc09b1 5355 ->bind(dw);
1e035395 5356
f6be7c06
DB
5357 // For process probes / uprobes, .maxactive() is unused.
5358 if (! pr_contains (privilege, pr_stapusr))
1e035395
FCE
5359 {
5360 root->bind(TOK_RETURN)
1e035395
FCE
5361 ->bind_num(TOK_MAXACTIVE)->bind(dw);
5362 }
bd2b1e68
GH
5363}
5364
7a053d3b 5365void
27dc09b1 5366dwarf_derived_probe::register_function_and_statement_variants(
440d9b00 5367 systemtap_session& s,
27dc09b1
DB
5368 match_node * root,
5369 dwarf_builder * dw,
42e38653 5370 privilege_t privilege
27dc09b1 5371)
bd2b1e68
GH
5372{
5373 // Here we match 4 forms:
5374 //
5375 // .function("foo")
5376 // .function(0xdeadbeef)
5377 // .statement("foo")
5378 // .statement(0xdeadbeef)
5379
440d9b00 5380 match_node *fv_root = root->bind_str(TOK_FUNCTION);
42e38653 5381 register_function_variants(fv_root, dw, privilege);
7f02ca94 5382 // ROOT.function("STRING") always gets the .inline and .label variants.
440d9b00 5383 fv_root->bind(TOK_INLINE)
42e38653 5384 ->bind_privilege(privilege)
440d9b00 5385 ->bind(dw);
7f02ca94
JS
5386 fv_root->bind_str(TOK_LABEL)
5387 ->bind_privilege(privilege)
440d9b00 5388 ->bind(dw);
c31add50
JL
5389 fv_root->bind_str(TOK_CALLEE)
5390 ->bind_privilege(privilege)
5391 ->bind(dw);
5392 fv_root->bind(TOK_CALLEES)
5393 ->bind_privilege(privilege)
5394 ->bind(dw);
5395 fv_root->bind_num(TOK_CALLEES)
5396 ->bind_privilege(privilege)
5397 ->bind(dw);
440d9b00
DB
5398
5399 fv_root = root->bind_num(TOK_FUNCTION);
42e38653 5400 register_function_variants(fv_root, dw, privilege);
440d9b00
DB
5401 // ROOT.function(NUMBER).inline is deprecated in release 1.7 and removed thereafter.
5402 if (strverscmp(s.compatible.c_str(), "1.7") <= 0)
5403 {
5404 fv_root->bind(TOK_INLINE)
42e38653 5405 ->bind_privilege(privilege)
440d9b00
DB
5406 ->bind(dw);
5407 }
5408
42e38653
DB
5409 register_statement_variants(root->bind_str(TOK_STATEMENT), dw, privilege);
5410 register_statement_variants(root->bind_num(TOK_STATEMENT), dw, privilege);
bd2b1e68
GH
5411}
5412
b1615c74
JS
5413void
5414dwarf_derived_probe::register_sdt_variants(systemtap_session& s,
5415 match_node * root,
5416 dwarf_builder * dw)
5417{
5418 root->bind_str(TOK_MARK)
f66bb29a 5419 ->bind_privilege(pr_all)
b1615c74
JS
5420 ->bind(dw);
5421 root->bind_str(TOK_PROVIDER)->bind_str(TOK_MARK)
f66bb29a 5422 ->bind_privilege(pr_all)
b1615c74
JS
5423 ->bind(dw);
5424}
5425
5426void
5427dwarf_derived_probe::register_plt_variants(systemtap_session& s,
5428 match_node * root,
5429 dwarf_builder * dw)
5430{
5431 root->bind(TOK_PLT)
f66bb29a 5432 ->bind_privilege(pr_all)
b1615c74
JS
5433 ->bind(dw);
5434 root->bind_str(TOK_PLT)
f66bb29a 5435 ->bind_privilege(pr_all)
b1615c74 5436 ->bind(dw);
2e96714f
SC
5437
5438 root->bind(TOK_PLT)
5439 ->bind(TOK_RETURN)
f66bb29a 5440 ->bind_privilege(pr_all)
b1615c74 5441 ->bind(dw);
2e96714f
SC
5442 root->bind_str(TOK_PLT)
5443 ->bind(TOK_RETURN)
f66bb29a 5444 ->bind_privilege(pr_all)
b1615c74 5445 ->bind(dw);
bd2b1e68
GH
5446}
5447
5448void
c4ce66a1 5449dwarf_derived_probe::register_patterns(systemtap_session& s)
bd2b1e68 5450{
c4ce66a1 5451 match_node* root = s.pattern_root;
bd2b1e68
GH
5452 dwarf_builder *dw = new dwarf_builder();
5453
c4ce66a1
JS
5454 update_visitor *filter = new dwarf_cast_expanding_visitor(s, *dw);
5455 s.code_filters.push_back(filter);
5456
bd1fcbad
YZ
5457 filter = new dwarf_atvar_expanding_visitor(s, *dw);
5458 s.code_filters.push_back(filter);
5459
73f52eb4
DB
5460 register_function_and_statement_variants(s, root->bind(TOK_KERNEL), dw, pr_privileged);
5461 register_function_and_statement_variants(s, root->bind_str(TOK_MODULE), dw, pr_privileged);
27dc09b1
DB
5462 root->bind(TOK_KERNEL)->bind_num(TOK_STATEMENT)->bind(TOK_ABSOLUTE)
5463 ->bind(dw);
2cab6244 5464
7f02ca94
JS
5465 match_node* uprobes[] = {
5466 root->bind(TOK_PROCESS),
5467 root->bind_str(TOK_PROCESS),
5c6f9e92 5468 root->bind_num(TOK_PROCESS),
7f02ca94
JS
5469 root->bind(TOK_PROCESS)->bind_str(TOK_LIBRARY),
5470 root->bind_str(TOK_PROCESS)->bind_str(TOK_LIBRARY),
5471 };
5472 for (size_t i = 0; i < sizeof(uprobes) / sizeof(*uprobes); ++i)
5473 {
f66bb29a 5474 register_function_and_statement_variants(s, uprobes[i], dw, pr_all);
7f02ca94
JS
5475 register_sdt_variants(s, uprobes[i], dw);
5476 register_plt_variants(s, uprobes[i], dw);
5477 }
bd2b1e68
GH
5478}
5479
9020300d 5480void
3689db05
SC
5481dwarf_derived_probe::emit_probe_local_init(systemtap_session& s, translator_output * o)
5482{
aa2c662f 5483 std::set<string>::iterator pcii;
3689db05
SC
5484 for (pcii = perf_counter_refs.begin();
5485 pcii != perf_counter_refs.end();
5486 pcii++)
5487 {
aa2c662f 5488 std::vector<std::pair<std::string,std::string> >::iterator it;
3689db05
SC
5489 // Find the associated perf.counter probe
5490 unsigned i = 0;
5491 for (it=s.perf_counters.begin() ;
5492 it != s.perf_counters.end();
5493 it++, i++)
aa2c662f 5494 if ((*it).first == (*pcii))
3689db05
SC
5495 break;
5496 // place the perf counter read so it precedes stp_lock_probe
5497 o->newline() << "l->l___perf_read_" + (*it).first
5498 + " = (((int64_t) (_stp_perf_read(smp_processor_id(),"
5499 + lex_cast(i) + "))));";
5500 }
b78a0fbb 5501
b95e2b79
MH
5502 if (access_vars)
5503 {
5504 // if accessing $variables, emit bsp cache setup for speeding up
d4670309 5505 o->newline() << "#if defined __ia64__";
d9aed31e 5506 o->newline() << "bspcache(c->unwaddr, c->kregs);";
d4670309 5507 o->newline() << "#endif";
b95e2b79 5508 }
9020300d 5509}
2930abc7 5510
b20febf3 5511// ------------------------------------------------------------------------
46b84a80
DS
5512
5513void
b20febf3 5514dwarf_derived_probe_group::enroll (dwarf_derived_probe* p)
46b84a80 5515{
b20febf3 5516 probes_by_module.insert (make_pair (p->module, p));
b8da0ad1
FCE
5517
5518 // XXX: probes put at the same address should all share a
5519 // single kprobe/kretprobe, and have their handlers executed
5520 // sequentially.
b55bc428
FCE
5521}
5522
7a053d3b 5523void
775d51e5 5524dwarf_derived_probe_group::emit_module_decls (systemtap_session& s)
ec4373ff 5525{
b20febf3 5526 if (probes_by_module.empty()) return;
2930abc7 5527
775d51e5
DS
5528 s.op->newline() << "/* ---- dwarf probes ---- */";
5529
faea5e16 5530 // Let's find some stats for the embedded strings. Maybe they
606fd9c8
FCE
5531 // are small and uniform enough to justify putting char[MAX]'s into
5532 // the array instead of relocated char*'s.
faea5e16
JS
5533 size_t module_name_max = 0, section_name_max = 0;
5534 size_t module_name_tot = 0, section_name_tot = 0;
606fd9c8
FCE
5535 size_t all_name_cnt = probes_by_module.size(); // for average
5536 for (p_b_m_iterator it = probes_by_module.begin(); it != probes_by_module.end(); it++)
5537 {
5538 dwarf_derived_probe* p = it->second;
5539#define DOIT(var,expr) do { \
5540 size_t var##_size = (expr) + 1; \
5541 var##_max = max (var##_max, var##_size); \
5542 var##_tot += var##_size; } while (0)
5543 DOIT(module_name, p->module.size());
5544 DOIT(section_name, p->section.size());
606fd9c8
FCE
5545#undef DOIT
5546 }
5547
5548 // Decide whether it's worthwhile to use char[] or char* by comparing
5549 // the amount of average waste (max - avg) to the relocation data size
5550 // (3 native long words).
5551#define CALCIT(var) \
5552 if ((var##_name_max-(var##_name_tot/all_name_cnt)) < (3 * sizeof(void*))) \
5553 { \
2d9ec661
JL
5554 s.op->newline() << "#define STAP_DWARF_PROBE_STR_" << #var << " " \
5555 << "const char " << #var \
5556 << "[" << var##_name_max << "]"; \
606fd9c8
FCE
5557 if (s.verbose > 2) clog << "stap_dwarf_probe " << #var \
5558 << "[" << var##_name_max << "]" << endl; \
5559 } \
5560 else \
5561 { \
2d9ec661
JL
5562 s.op->newline() << "#define STAP_DWARF_PROBE_STR_" << #var << " " \
5563 << "const char * const " << #var << ""; \
606fd9c8
FCE
5564 if (s.verbose > 2) clog << "stap_dwarf_probe *" << #var << endl; \
5565 }
5566
5567 CALCIT(module);
5568 CALCIT(section);
2d9ec661 5569
e6fe60e7 5570#undef CALCIT
606fd9c8 5571
2d9ec661
JL
5572 s.op->newline() << "#include \"linux/kprobes.c\"";
5573
5574#define UNDEFIT(var) s.op->newline() << "#undef STAP_DWARF_PROBE_STR_" << #var
5575 UNDEFIT(module);
5576 UNDEFIT(section);
5577#undef UNDEFIT
5578
5579 // Emit an array of kprobe/kretprobe pointers
5580 s.op->newline() << "#if defined(STAPCONF_UNREGISTER_KPROBES)";
5581 s.op->newline() << "static void * stap_unreg_kprobes[" << probes_by_module.size() << "];";
5582 s.op->newline() << "#endif";
5583
5584 // Emit the actual probe list.
5585
5586 // NB: we used to plop a union { struct kprobe; struct kretprobe } into
5587 // struct stap_dwarf_probe, but it being initialized data makes it add
5588 // hundreds of bytes of padding per stap_dwarf_probe. (PR5673)
5589 s.op->newline() << "static struct stap_dwarf_kprobe stap_dwarf_kprobes[" << probes_by_module.size() << "];";
5590 // NB: bss!
5591
5592 s.op->newline() << "static struct stap_dwarf_probe stap_dwarf_probes[] = {";
b20febf3
FCE
5593 s.op->indent(1);
5594
ddafd8ce 5595 size_t stap_dwarf_kprobe_idx = 0;
b20febf3 5596 for (p_b_m_iterator it = probes_by_module.begin(); it != probes_by_module.end(); it++)
2930abc7 5597 {
b20febf3
FCE
5598 dwarf_derived_probe* p = it->second;
5599 s.op->newline() << "{";
5600 if (p->has_return)
5601 s.op->line() << " .return_p=1,";
c9bad430 5602 if (p->has_maxactive)
606fd9c8
FCE
5603 {
5604 s.op->line() << " .maxactive_p=1,";
5605 assert (p->maxactive_val >= 0 && p->maxactive_val <= USHRT_MAX);
5606 s.op->line() << " .maxactive_val=" << p->maxactive_val << ",";
5607 }
af234c40
JS
5608 if (p->saved_longs || p->saved_strings)
5609 {
5610 if (p->saved_longs)
5611 s.op->line() << " .saved_longs=" << p->saved_longs << ",";
5612 if (p->saved_strings)
5613 s.op->line() << " .saved_strings=" << p->saved_strings << ",";
5614 if (p->entry_handler)
c87ae2c1 5615 s.op->line() << " .entry_probe=" << common_probe_init (p->entry_handler) << ",";
af234c40 5616 }
b350f56b
JS
5617 if (p->locations[0]->optional)
5618 s.op->line() << " .optional_p=1,";
dc38c256 5619 s.op->line() << " .address=(unsigned long)0x" << hex << p->addr << dec << "ULL,";
84048984
FCE
5620 s.op->line() << " .module=\"" << p->module << "\",";
5621 s.op->line() << " .section=\"" << p->section << "\",";
faea5e16 5622 s.op->line() << " .probe=" << common_probe_init (p) << ",";
ddafd8ce 5623 s.op->line() << " .kprobe=&stap_dwarf_kprobes[" << stap_dwarf_kprobe_idx++ << "],";
b20febf3 5624 s.op->line() << " },";
2930abc7 5625 }
2930abc7 5626
b20febf3
FCE
5627 s.op->newline(-1) << "};";
5628
5629 // Emit the kprobes callback function
5630 s.op->newline();
5631 s.op->newline() << "static int enter_kprobe_probe (struct kprobe *inst,";
5632 s.op->line() << " struct pt_regs *regs) {";
606fd9c8
FCE
5633 // NB: as of PR5673, the kprobe|kretprobe union struct is in BSS
5634 s.op->newline(1) << "int kprobe_idx = ((uintptr_t)inst-(uintptr_t)stap_dwarf_kprobes)/sizeof(struct stap_dwarf_kprobe);";
5635 // Check that the index is plausible
5636 s.op->newline() << "struct stap_dwarf_probe *sdp = &stap_dwarf_probes[";
5637 s.op->line() << "((kprobe_idx >= 0 && kprobe_idx < " << probes_by_module.size() << ")?";
5638 s.op->line() << "kprobe_idx:0)"; // NB: at least we avoid memory corruption
5639 // XXX: it would be nice to give a more verbose error though; BUG_ON later?
5640 s.op->line() << "];";
71db462b 5641 common_probe_entryfn_prologue (s, "STAP_SESSION_RUNNING", "sdp->probe",
cda141c2 5642 "stp_probe_type_kprobe");
d9aed31e 5643 s.op->newline() << "c->kregs = regs;";
6415ddde
MW
5644
5645 // Make it look like the IP is set as it wouldn't have been replaced
5646 // by a breakpoint instruction when calling real probe handler. Reset
5647 // IP regs on return, so we don't confuse kprobes. PR10458
5648 s.op->newline() << "{";
5649 s.op->indent(1);
d9aed31e 5650 s.op->newline() << "unsigned long kprobes_ip = REG_IP(c->kregs);";
259d54c0 5651 s.op->newline() << "SET_REG_IP(regs, (unsigned long) inst->addr);";
26e63673 5652 s.op->newline() << "(*sdp->probe->ph) (c);";
259d54c0 5653 s.op->newline() << "SET_REG_IP(regs, kprobes_ip);";
6415ddde
MW
5654 s.op->newline(-1) << "}";
5655
ef1337ee 5656 common_probe_entryfn_epilogue (s, true, otf_safe_context(s));
b20febf3
FCE
5657 s.op->newline() << "return 0;";
5658 s.op->newline(-1) << "}";
5659
5660 // Same for kretprobes
5661 s.op->newline();
af234c40
JS
5662 s.op->newline() << "static int enter_kretprobe_common (struct kretprobe_instance *inst,";
5663 s.op->line() << " struct pt_regs *regs, int entry) {";
b20febf3 5664 s.op->newline(1) << "struct kretprobe *krp = inst->rp;";
606fd9c8
FCE
5665
5666 // NB: as of PR5673, the kprobe|kretprobe union struct is in BSS
a36378d7 5667 s.op->newline() << "int kprobe_idx = ((uintptr_t)krp-(uintptr_t)stap_dwarf_kprobes)/sizeof(struct stap_dwarf_kprobe);";
606fd9c8
FCE
5668 // Check that the index is plausible
5669 s.op->newline() << "struct stap_dwarf_probe *sdp = &stap_dwarf_probes[";
5670 s.op->line() << "((kprobe_idx >= 0 && kprobe_idx < " << probes_by_module.size() << ")?";
5671 s.op->line() << "kprobe_idx:0)"; // NB: at least we avoid memory corruption
5672 // XXX: it would be nice to give a more verbose error though; BUG_ON later?
5673 s.op->line() << "];";
5674
7c3e97f4 5675 s.op->newline() << "const struct stap_probe *sp = entry ? sdp->entry_probe : sdp->probe;";
c87ae2c1
JS
5676 s.op->newline() << "if (sp) {";
5677 s.op->indent(1);
71db462b 5678 common_probe_entryfn_prologue (s, "STAP_SESSION_RUNNING", "sp",
cda141c2 5679 "stp_probe_type_kretprobe");
d9aed31e 5680 s.op->newline() << "c->kregs = regs;";
af234c40
JS
5681
5682 // for assisting runtime's backtrace logic and accessing kretprobe data packets
6dceb5c9
MW
5683 s.op->newline() << "c->ips.krp.pi = inst;";
5684 s.op->newline() << "c->ips.krp.pi_longs = sdp->saved_longs;";
6415ddde
MW
5685
5686 // Make it look like the IP is set as it wouldn't have been replaced
5687 // by a breakpoint instruction when calling real probe handler. Reset
5688 // IP regs on return, so we don't confuse kprobes. PR10458
5689 s.op->newline() << "{";
d9aed31e 5690 s.op->newline(1) << "unsigned long kprobes_ip = REG_IP(c->kregs);";
c87ae2c1
JS
5691 s.op->newline() << "if (entry)";
5692 s.op->newline(1) << "SET_REG_IP(regs, (unsigned long) inst->rp->kp.addr);";
5693 s.op->newline(-1) << "else";
5694 s.op->newline(1) << "SET_REG_IP(regs, (unsigned long)inst->ret_addr);";
5695 s.op->newline(-1) << "(sp->ph) (c);";
259d54c0 5696 s.op->newline() << "SET_REG_IP(regs, kprobes_ip);";
6415ddde
MW
5697 s.op->newline(-1) << "}";
5698
ef1337ee 5699 common_probe_entryfn_epilogue (s, true, otf_safe_context(s));
c87ae2c1 5700 s.op->newline(-1) << "}";
b20febf3
FCE
5701 s.op->newline() << "return 0;";
5702 s.op->newline(-1) << "}";
af234c40 5703
14cf7e42 5704 s.op->newline();
20c6c071 5705}
ec4373ff 5706
20c6c071 5707
dc38c0ae 5708void
b20febf3
FCE
5709dwarf_derived_probe_group::emit_module_init (systemtap_session& s)
5710{
66cab35f 5711 if (probes_by_module.empty()) return;
195dfadc 5712
66cab35f 5713 s.op->newline() << "/* ---- dwarf probes ---- */";
54ff4cd7 5714
66cab35f
JL
5715 // We'll let stapkp_init() handle reporting errors by setting probe_point to
5716 // NULL.
5717 s.op->newline() << "probe_point = NULL;";
54ff4cd7 5718
66cab35f
JL
5719 s.op->newline() << "rc = stapkp_init( "
5720 << "stap_dwarf_probes, "
66cab35f 5721 << "ARRAY_SIZE(stap_dwarf_probes));";
dc38c0ae
DS
5722}
5723
5724
b4be7cbc
FCE
5725void
5726dwarf_derived_probe_group::emit_module_refresh (systemtap_session& s)
5727{
97ab978d 5728 if (probes_by_module.empty()) return;
b4be7cbc 5729
97ab978d 5730 s.op->newline() << "/* ---- dwarf probes ---- */";
b4be7cbc 5731
97ab978d 5732 s.op->newline() << "stapkp_refresh( "
19d62b5b 5733 << "modname, "
97ab978d 5734 << "stap_dwarf_probes, "
97ab978d
JL
5735 << "ARRAY_SIZE(stap_dwarf_probes));";
5736}
b4be7cbc
FCE
5737
5738
46b84a80 5739void
b20febf3 5740dwarf_derived_probe_group::emit_module_exit (systemtap_session& s)
46b84a80 5741{
c29d98ec 5742 if (probes_by_module.empty()) return;
42cb22bd 5743
c29d98ec
JL
5744 s.op->newline() << "/* ---- dwarf probes ---- */";
5745
5746 s.op->newline() << "stapkp_exit( "
5747 << "stap_dwarf_probes, "
c29d98ec 5748 << "ARRAY_SIZE(stap_dwarf_probes));";
46b84a80
DS
5749}
5750
272c9036
WF
5751static void sdt_v3_tokenize(const string& str, vector<string>& tokens)
5752{
5753 string::size_type pos;
5754 string::size_type lastPos = str.find_first_not_of(" ", 0);
5755 string::size_type nextAt = str.find("@", lastPos);
e9a90eee
JS
5756
5757 if (nextAt == string::npos)
5758 {
5759 // PR13934: Assembly probes are not forced to use the N@OP form.
5760 // In this case, N is inferred to be the native word size. Since we
5761 // don't have a nice delimiter, just split it on spaces. SDT-asm authors
5762 // then must not put any spaces in arguments, to avoid ambiguity.
5763 tokenize(str, tokens, " ");
5764 return;
5765 }
5766
272c9036
WF
5767 while (lastPos != string::npos)
5768 {
5769 pos = nextAt + 1;
5770 nextAt = str.find("@", pos);
5771 if (nextAt == string::npos)
5772 pos = string::npos;
5773 else
5774 pos = str.rfind(" ", nextAt);
5775
5776 tokens.push_back(str.substr(lastPos, pos - lastPos));
5777 lastPos = str.find_first_not_of(" ", pos);
5778 }
5779}
8aabf152 5780
8aabf152 5781
aff5d390 5782struct sdt_uprobe_var_expanding_visitor: public var_expanding_visitor
7a05f484 5783{
ae1418f0 5784 enum regwidths {QI, QIh, HI, SI, DI};
f83336a5 5785 sdt_uprobe_var_expanding_visitor(systemtap_session& s,
b8688ee6 5786 dwflpp& dw,
f83336a5
FCE
5787 int elf_machine,
5788 const string & process_name,
a794dbeb 5789 const string & provider_name,
aff5d390 5790 const string & probe_name,
71e5e13d 5791 stap_sdt_probe_type probe_type,
aff5d390 5792 const string & arg_string,
8aabf152 5793 int ac):
b8688ee6
JL
5794 session (s), dw (dw), elf_machine (elf_machine),
5795 process_name (process_name), provider_name (provider_name),
5796 probe_name (probe_name), probe_type (probe_type), arg_count ((unsigned) ac)
a8ec7719 5797 {
a57b7534
JL
5798 // sanity check that we're not somehow here for a kernel probe
5799 assert(is_user_module(process_name));
ae1418f0 5800
2e6d617a 5801 build_dwarf_registers();
f83336a5 5802
ebbd2b45 5803 need_debug_info = false;
88e39987 5804 if (probe_type == uprobe3_type)
272c9036
WF
5805 {
5806 sdt_v3_tokenize(arg_string, arg_tokens);
5807 assert(arg_count <= 12);
5808 }
88e39987 5809 else
272c9036
WF
5810 {
5811 tokenize(arg_string, arg_tokens, " ");
5812 assert(arg_count <= 10);
5813 }
a8ec7719 5814 }
8aabf152 5815
f83336a5 5816 systemtap_session& session;
b8688ee6 5817 dwflpp& dw;
332ba7e7 5818 int elf_machine;
aff5d390 5819 const string & process_name;
a794dbeb 5820 const string & provider_name;
aff5d390 5821 const string & probe_name;
71e5e13d 5822 stap_sdt_probe_type probe_type;
8aabf152 5823 unsigned arg_count;
aff5d390 5824 vector<string> arg_tokens;
2e6d617a 5825
46a94997 5826 map<string, pair<unsigned,int> > dwarf_regs;
2e6d617a
JL
5827 string regnames;
5828 string percent_regnames;
5829
ebbd2b45 5830 bool need_debug_info;
aff5d390 5831
2e6d617a 5832 void build_dwarf_registers();
aff5d390 5833 void visit_target_symbol (target_symbol* e);
03c45fcb 5834 unsigned get_target_symbol_argno_and_validate (target_symbol* e);
ac074daf 5835 long parse_out_arg_precision(string& asmarg);
0fbf6fb8
JL
5836 expression* try_parse_arg_literal (target_symbol *e,
5837 const string& asmarg,
5838 long precision);
95b78bc9
JL
5839 expression* try_parse_arg_register (target_symbol *e,
5840 const string& asmarg,
5841 long precision);
8da56b62
JL
5842 expression* try_parse_arg_offset_register (target_symbol *e,
5843 const string& asmarg,
5844 long precision);
10fcca15
JL
5845 expression* try_parse_arg_effective_addr (target_symbol *e,
5846 const string& asmarg,
5847 long precision);
451c0c25
JL
5848 expression* try_parse_arg_varname (target_symbol *e,
5849 const string& asmarg,
5850 long precision);
6ef331c8
SC
5851 void visit_target_symbol_arg (target_symbol* e);
5852 void visit_target_symbol_context (target_symbol* e);
bd1fcbad 5853 void visit_atvar_op (atvar_op* e);
40a0c64e 5854 void visit_cast_op (cast_op* e);
aff5d390
SC
5855};
5856
2e6d617a
JL
5857void
5858sdt_uprobe_var_expanding_visitor::build_dwarf_registers ()
5859{
5860 /* Register name mapping table depends on the elf machine of this particular
5861 probe target process/file, not upon the host. So we can't just
5862 #ifdef _i686_ etc. */
5863
5864#define DRI(name,num,width) dwarf_regs[name]=make_pair(num,width)
5865 if (elf_machine == EM_X86_64) {
5866 DRI ("%rax", 0, DI); DRI ("%eax", 0, SI); DRI ("%ax", 0, HI);
5867 DRI ("%al", 0, QI); DRI ("%ah", 0, QIh);
5868 DRI ("%rdx", 1, DI); DRI ("%edx", 1, SI); DRI ("%dx", 1, HI);
5869 DRI ("%dl", 1, QI); DRI ("%dh", 1, QIh);
5870 DRI ("%rcx", 2, DI); DRI ("%ecx", 2, SI); DRI ("%cx", 2, HI);
5871 DRI ("%cl", 2, QI); DRI ("%ch", 2, QIh);
5872 DRI ("%rbx", 3, DI); DRI ("%ebx", 3, SI); DRI ("%bx", 3, HI);
5873 DRI ("%bl", 3, QI); DRI ("%bh", 3, QIh);
5874 DRI ("%rsi", 4, DI); DRI ("%esi", 4, SI); DRI ("%si", 4, HI);
5875 DRI ("%sil", 4, QI);
5876 DRI ("%rdi", 5, DI); DRI ("%edi", 5, SI); DRI ("%di", 5, HI);
5877 DRI ("%dil", 5, QI);
5878 DRI ("%rbp", 6, DI); DRI ("%ebp", 6, SI); DRI ("%bp", 6, HI);
5879 DRI ("%bpl", 6, QI);
5880 DRI ("%rsp", 7, DI); DRI ("%esp", 7, SI); DRI ("%sp", 7, HI);
5881 DRI ("%spl", 7, QI);
5882 DRI ("%r8", 8, DI); DRI ("%r8d", 8, SI); DRI ("%r8w", 8, HI);
5883 DRI ("%r8b", 8, QI);
5884 DRI ("%r9", 9, DI); DRI ("%r9d", 9, SI); DRI ("%r9w", 9, HI);
5885 DRI ("%r9b", 9, QI);
5886 DRI ("%r10", 10, DI); DRI ("%r10d", 10, SI); DRI ("%r10w", 10, HI);
5887 DRI ("%r10b", 10, QI);
5888 DRI ("%r11", 11, DI); DRI ("%r11d", 11, SI); DRI ("%r11w", 11, HI);
5889 DRI ("%r11b", 11, QI);
5890 DRI ("%r12", 12, DI); DRI ("%r12d", 12, SI); DRI ("%r12w", 12, HI);
5891 DRI ("%r12b", 12, QI);
5892 DRI ("%r13", 13, DI); DRI ("%r13d", 13, SI); DRI ("%r13w", 13, HI);
5893 DRI ("%r13b", 13, QI);
5894 DRI ("%r14", 14, DI); DRI ("%r14d", 14, SI); DRI ("%r14w", 14, HI);
5895 DRI ("%r14b", 14, QI);
5896 DRI ("%r15", 15, DI); DRI ("%r15d", 15, SI); DRI ("%r15w", 15, HI);
5897 DRI ("%r15b", 15, QI);
5898 DRI ("%rip", 16, DI); DRI ("%eip", 16, SI); DRI ("%ip", 16, HI);
5899 } else if (elf_machine == EM_386) {
5900 DRI ("%eax", 0, SI); DRI ("%ax", 0, HI); DRI ("%al", 0, QI);
5901 DRI ("%ah", 0, QIh);
5902 DRI ("%ecx", 1, SI); DRI ("%cx", 1, HI); DRI ("%cl", 1, QI);
5903 DRI ("%ch", 1, QIh);
5904 DRI ("%edx", 2, SI); DRI ("%dx", 2, HI); DRI ("%dl", 2, QI);
5905 DRI ("%dh", 2, QIh);
5906 DRI ("%ebx", 3, SI); DRI ("%bx", 3, HI); DRI ("%bl", 3, QI);
5907 DRI ("%bh", 3, QIh);
5908 DRI ("%esp", 4, SI); DRI ("%sp", 4, HI);
5909 DRI ("%ebp", 5, SI); DRI ("%bp", 5, HI);
5910 DRI ("%esi", 6, SI); DRI ("%si", 6, HI); DRI ("%sil", 6, QI);
5911 DRI ("%edi", 7, SI); DRI ("%di", 7, HI); DRI ("%dil", 7, QI);
5912 } else if (elf_machine == EM_PPC || elf_machine == EM_PPC64) {
5913 DRI ("%r0", 0, DI);
5914 DRI ("%r1", 1, DI);
5915 DRI ("%r2", 2, DI);
5916 DRI ("%r3", 3, DI);
5917 DRI ("%r4", 4, DI);
5918 DRI ("%r5", 5, DI);
5919 DRI ("%r6", 6, DI);
5920 DRI ("%r7", 7, DI);
5921 DRI ("%r8", 8, DI);
5922 DRI ("%r9", 9, DI);
5923 DRI ("%r10", 10, DI);
5924 DRI ("%r11", 11, DI);
5925 DRI ("%r12", 12, DI);
5926 DRI ("%r13", 13, DI);
5927 DRI ("%r14", 14, DI);
5928 DRI ("%r15", 15, DI);
5929 DRI ("%r16", 16, DI);
5930 DRI ("%r17", 17, DI);
5931 DRI ("%r18", 18, DI);
5932 DRI ("%r19", 19, DI);
5933 DRI ("%r20", 20, DI);
5934 DRI ("%r21", 21, DI);
5935 DRI ("%r22", 22, DI);
5936 DRI ("%r23", 23, DI);
5937 DRI ("%r24", 24, DI);
5938 DRI ("%r25", 25, DI);
5939 DRI ("%r26", 26, DI);
5940 DRI ("%r27", 27, DI);
5941 DRI ("%r28", 28, DI);
5942 DRI ("%r29", 29, DI);
5943 DRI ("%r30", 30, DI);
5944 DRI ("%r31", 31, DI);
5945 // PR11821: unadorned register "names" without -mregnames
5946 DRI ("0", 0, DI);
5947 DRI ("1", 1, DI);
5948 DRI ("2", 2, DI);
5949 DRI ("3", 3, DI);
5950 DRI ("4", 4, DI);
5951 DRI ("5", 5, DI);
5952 DRI ("6", 6, DI);
5953 DRI ("7", 7, DI);
5954 DRI ("8", 8, DI);
5955 DRI ("9", 9, DI);
5956 DRI ("10", 10, DI);
5957 DRI ("11", 11, DI);
5958 DRI ("12", 12, DI);
5959 DRI ("13", 13, DI);
5960 DRI ("14", 14, DI);
5961 DRI ("15", 15, DI);
5962 DRI ("16", 16, DI);
5963 DRI ("17", 17, DI);
5964 DRI ("18", 18, DI);
5965 DRI ("19", 19, DI);
5966 DRI ("20", 20, DI);
5967 DRI ("21", 21, DI);
5968 DRI ("22", 22, DI);
5969 DRI ("23", 23, DI);
5970 DRI ("24", 24, DI);
5971 DRI ("25", 25, DI);
5972 DRI ("26", 26, DI);
5973 DRI ("27", 27, DI);
5974 DRI ("28", 28, DI);
5975 DRI ("29", 29, DI);
5976 DRI ("30", 30, DI);
5977 DRI ("31", 31, DI);
5978 } else if (elf_machine == EM_S390) {
5979 DRI ("%r0", 0, DI);
5980 DRI ("%r1", 1, DI);
5981 DRI ("%r2", 2, DI);
5982 DRI ("%r3", 3, DI);
5983 DRI ("%r4", 4, DI);
5984 DRI ("%r5", 5, DI);
5985 DRI ("%r6", 6, DI);
5986 DRI ("%r7", 7, DI);
5987 DRI ("%r8", 8, DI);
5988 DRI ("%r9", 9, DI);
5989 DRI ("%r10", 10, DI);
5990 DRI ("%r11", 11, DI);
5991 DRI ("%r12", 12, DI);
5992 DRI ("%r13", 13, DI);
5993 DRI ("%r14", 14, DI);
5994 DRI ("%r15", 15, DI);
5995 } else if (elf_machine == EM_ARM) {
5996 DRI ("r0", 0, SI);
5997 DRI ("r1", 1, SI);
5998 DRI ("r2", 2, SI);
5999 DRI ("r3", 3, SI);
6000 DRI ("r4", 4, SI);
6001 DRI ("r5", 5, SI);
6002 DRI ("r6", 6, SI);
6003 DRI ("r7", 7, SI);
6004 DRI ("r8", 8, SI);
6005 DRI ("r9", 9, SI);
6006 DRI ("r10", 10, SI); DRI ("sl", 10, SI);
6007 DRI ("fp", 11, SI);
6008 DRI ("ip", 12, SI);
6009 DRI ("sp", 13, SI);
6010 DRI ("lr", 14, SI);
6011 DRI ("pc", 15, SI);
6012 } else if (elf_machine == EM_AARCH64) {
6013 DRI ("x0", 0, DI); DRI ("w0", 0, SI);
6014 DRI ("x1", 1, DI); DRI ("w1", 1, SI);
6015 DRI ("x2", 2, DI); DRI ("w2", 2, SI);
6016 DRI ("x3", 3, DI); DRI ("w3", 3, SI);
6017 DRI ("x4", 4, DI); DRI ("w4", 4, SI);
6018 DRI ("x5", 5, DI); DRI ("w5", 5, SI);
6019 DRI ("x6", 6, DI); DRI ("w6", 6, SI);
6020 DRI ("x7", 7, DI); DRI ("w7", 7, SI);
6021 DRI ("x8", 8, DI); DRI ("w8", 8, SI);
6022 DRI ("x9", 9, DI); DRI ("w9", 9, SI);
6023 DRI ("x10", 10, DI); DRI ("w10", 10, SI);
6024 DRI ("x11", 11, DI); DRI ("w11", 11, SI);
6025 DRI ("x12", 12, DI); DRI ("w12", 12, SI);
6026 DRI ("x13", 13, DI); DRI ("w13", 13, SI);
6027 DRI ("x14", 14, DI); DRI ("w14", 14, SI);
6028 DRI ("x15", 15, DI); DRI ("w15", 15, SI);
6029 DRI ("x16", 16, DI); DRI ("w16", 16, SI);
6030 DRI ("x17", 17, DI); DRI ("w17", 17, SI);
6031 DRI ("x18", 18, DI); DRI ("w18", 18, SI);
6032 DRI ("x19", 19, DI); DRI ("w19", 19, SI);
6033 DRI ("x20", 20, DI); DRI ("w20", 20, SI);
6034 DRI ("x21", 21, DI); DRI ("w21", 21, SI);
6035 DRI ("x22", 22, DI); DRI ("w22", 22, SI);
6036 DRI ("x23", 23, DI); DRI ("w23", 23, SI);
6037 DRI ("x24", 24, DI); DRI ("w24", 24, SI);
6038 DRI ("x25", 25, DI); DRI ("w25", 25, SI);
6039 DRI ("x26", 26, DI); DRI ("w26", 26, SI);
6040 DRI ("x27", 27, DI); DRI ("w27", 27, SI);
6041 DRI ("x28", 28, DI); DRI ("w28", 28, SI);
6042 DRI ("x29", 29, DI); DRI ("w29", 29, SI);
6043 DRI ("x30", 30, DI); DRI ("w30", 30, SI);
6044 DRI ("sp", 31, DI);
6045 } else if (arg_count) {
6046 /* permit this case; just fall back to dwarf */
6047 }
6048#undef DRI
6049
6050 // Build regex pieces out of the known dwarf_regs. We keep two separate
6051 // lists: ones with the % prefix (and thus unambigiuous even despite PR11821),
6052 // and ones with no prefix (and thus only usable in unambiguous contexts).
6053 map<string,pair<unsigned,int> >::const_iterator ri;
6054 for (ri = dwarf_regs.begin(); ri != dwarf_regs.end(); ri++)
6055 {
6056 string regname = ri->first;
6057 assert (regname != "");
6058 regnames += string("|")+regname;
6059 if (regname[0]=='%')
6060 percent_regnames += string("|")+regname;
6061 }
6062
6063 // clip off leading |
6064 if (regnames != "")
6065 regnames = regnames.substr(1);
6066 if (percent_regnames != "")
6067 percent_regnames = percent_regnames.substr(1);
6068}
7a05f484
SC
6069
6070void
6ef331c8 6071sdt_uprobe_var_expanding_visitor::visit_target_symbol_context (target_symbol* e)
7a05f484 6072{
6ef331c8 6073 if (e->addressof)
dc09353a 6074 throw SEMANTIC_ERROR(_("cannot take address of context variable"), e->tok);
6ef331c8
SC
6075
6076 if (e->name == "$$name")
7a05f484 6077 {
6ef331c8
SC
6078 literal_string *myname = new literal_string (probe_name);
6079 myname->tok = e->tok;
6080 provide(myname);
6081 return;
6082 }
7a05f484 6083
6ef331c8
SC
6084 else if (e->name == "$$provider")
6085 {
6086 literal_string *myname = new literal_string (provider_name);
6087 myname->tok = e->tok;
6088 provide(myname);
6089 return;
6090 }
a794dbeb 6091
6ef331c8
SC
6092 else if (e->name == "$$vars" || e->name == "$$parms")
6093 {
6094 e->assert_no_components("sdt", true);
6ef331c8
SC
6095
6096 // Convert $$vars to sprintf of a list of vars which we recursively evaluate
63ea4244 6097
1c922ad7 6098 print_format* pf = print_format::create(e->tok, "sprintf");
6ef331c8
SC
6099
6100 for (unsigned i = 1; i <= arg_count; ++i)
6101 {
6102 if (i > 1)
6103 pf->raw_components += " ";
6104 target_symbol *tsym = new target_symbol;
6105 tsym->tok = e->tok;
6106 tsym->name = "$arg" + lex_cast(i);
6107 pf->raw_components += tsym->name;
6108 tsym->components = e->components;
6109
8c2f50c0 6110 expression *texp = require<expression> (tsym);
c4965ad9 6111 if (e->check_pretty_print ())
6ef331c8
SC
6112 pf->raw_components += "=%s";
6113 else
6114 pf->raw_components += "=%#x";
6115 pf->args.push_back(texp);
6116 }
6117
6118 pf->components = print_format::string_to_components(pf->raw_components);
6119 provide (pf);
6120 }
6121 else
6122 assert(0); // shouldn't get here
6123}
6124
03c45fcb
JL
6125unsigned
6126sdt_uprobe_var_expanding_visitor::get_target_symbol_argno_and_validate (target_symbol *e)
6ef331c8 6127{
03c45fcb
JL
6128 // parsing
6129 unsigned argno = 0;
6130 if (startswith(e->name, "$arg"))
6ef331c8 6131 {
c69a87e0 6132 try
03c45fcb
JL
6133 {
6134 argno = lex_cast<unsigned>(e->name.substr(4));
6135 }
6136 catch (const runtime_error& f)
6137 {
6138 // non-integral $arg suffix: e.g. $argKKKSDF
8aabf152 6139 argno = 0;
03c45fcb
JL
6140 }
6141 }
8aabf152 6142
03c45fcb
JL
6143 // validation
6144 if (arg_count == 0 || // a sdt.h variant without .probe-stored arg_count
6145 argno < 1 || argno > arg_count) // a $argN with out-of-range N
6146 {
6147 // NB: Either
6148 // 1) uprobe1_type $argN or $FOO (we don't know the arg_count)
6149 // 2) uprobe2_type $FOO (no probe args)
6150 // both of which get resolved later.
6151 // Throw it now, and it might be resolved by DWARF later.
6152 need_debug_info = true;
6153 throw SEMANTIC_ERROR(_("target-symbol requires debuginfo"), e->tok);
6154 }
6155 assert (arg_tokens.size() >= argno);
6156 return argno;
6157}
40fe32e0 6158
ac074daf
JL
6159long
6160sdt_uprobe_var_expanding_visitor::parse_out_arg_precision(string& asmarg)
6161{
6162 long precision;
6163 if (asmarg.find('@') != string::npos)
6164 {
6165 precision = lex_cast<int>(asmarg.substr(0, asmarg.find('@')));
6166 asmarg = asmarg.substr(asmarg.find('@')+1);
6167 }
6168 else
6169 {
6170 // V1/V2 do not have precision field so default to signed long
6171 // V3 asm does not have precision field so default to unsigned long
6172 if (probe_type == uprobe3_type)
6173 precision = sizeof(long); // this is an asm probe
71e5e13d 6174 else
ac074daf
JL
6175 precision = -sizeof(long);
6176 }
6177 return precision;
6178}
40fe32e0 6179
0fbf6fb8
JL
6180expression*
6181sdt_uprobe_var_expanding_visitor::try_parse_arg_literal (target_symbol *e,
6182 const string& asmarg,
6183 long precision)
6184{
6185 expression *argexpr = NULL;
6186
6187 // Here, we test for a numeric literal.
6188 // Only accept (signed) decimals throughout. XXX
6189
6190 // PR11821. NB: on powerpc, literals are not prefixed with $,
6191 // so this regex does not match. But that's OK, since without
6192 // -mregnames, we can't tell them apart from register numbers
6193 // anyway. With -mregnames, we could, if gcc somehow
6194 // communicated to us the presence of that option, but alas it
6195 // doesn't. http://gcc.gnu.org/PR44995.
6196 vector<string> matches;
6197 if (!regexp_match (asmarg, "^[i\\$#][-]?[0-9][0-9]*$", matches))
6198 {
6199 string sn = matches[0].substr(1);
6200 int64_t n;
6201
6202 // We have to pay attention to the size & sign, as gcc sometimes
6203 // propagates constants that don't quite match, like a negative
6204 // value to fill an unsigned type.
6205 // NB: let it throw if something happens
6206 switch (precision)
8aabf152 6207 {
0fbf6fb8
JL
6208 case -1: n = lex_cast< int8_t>(sn); break;
6209 case 1: n = lex_cast< uint8_t>(sn); break;
6210 case -2: n = lex_cast< int16_t>(sn); break;
6211 case 2: n = lex_cast<uint16_t>(sn); break;
6212 case -4: n = lex_cast< int32_t>(sn); break;
6213 case 4: n = lex_cast<uint32_t>(sn); break;
6214 default:
6215 case -8: n = lex_cast< int64_t>(sn); break;
6216 case 8: n = lex_cast<uint64_t>(sn); break;
8aabf152
FCE
6217 }
6218
0fbf6fb8
JL
6219 literal_number* ln = new literal_number(n);
6220 ln->tok = e->tok;
6221 argexpr = ln;
6222 }
b78a0fbb 6223
0fbf6fb8
JL
6224 return argexpr;
6225}
6226
95b78bc9
JL
6227expression*
6228sdt_uprobe_var_expanding_visitor::try_parse_arg_register (target_symbol *e,
6229 const string& asmarg,
6230 long precision)
6231{
6232 expression *argexpr = NULL;
6233
6234 // test for REGISTER
6235 // NB: Because PR11821, we must use percent_regnames here.
6236 string regexp;
6237 if (elf_machine == EM_PPC || elf_machine == EM_PPC64 || elf_machine == EM_ARM)
6238 regexp = "^(" + regnames + ")$";
6239 else
6240 regexp = "^(" + percent_regnames + ")$";
6241
6242 vector<string> matches;
6243 if (!regexp_match(asmarg, regexp, matches))
6244 {
6245 string regname = matches[1];
6246 map<string,pair<unsigned,int> >::iterator ri = dwarf_regs.find (regname);
6247 if (ri != dwarf_regs.end()) // known register
8aabf152 6248 {
95b78bc9
JL
6249 embedded_expr *get_arg1 = new embedded_expr;
6250 string width_adjust;
6251 switch (ri->second.second)
8aabf152 6252 {
95b78bc9
JL
6253 case QI: width_adjust = ") & 0xff)"; break;
6254 case QIh: width_adjust = ">>8) & 0xff)"; break;
6255 case HI:
6256 // preserve 16 bit register signness
6257 width_adjust = ") & 0xffff)";
6258 if (precision < 0)
6259 width_adjust += " << 48 >> 48";
6260 break;
6261 case SI:
6262 // preserve 32 bit register signness
6263 width_adjust = ") & 0xffffffff)";
6264 if (precision < 0)
6265 width_adjust += " << 32 >> 32";
6266 break;
6267 default: width_adjust = "))";
8aabf152 6268 }
95b78bc9
JL
6269 string type = "";
6270 if (probe_type == uprobe3_type)
6271 type = (precision < 0
6272 ? "(int" : "(uint") + lex_cast(abs(precision) * 8) + "_t)";
6273 type = type + "((";
6274 get_arg1->tok = e->tok;
6275 get_arg1->code = string("/* unprivileged */ /* pure */")
6276 + string(" ((int64_t)") + type
a57b7534 6277 + string("u_fetch_register(")
95b78bc9
JL
6278 + lex_cast(dwarf_regs[regname].first) + string("))")
6279 + width_adjust;
6280 argexpr = get_arg1;
8aabf152 6281 }
95b78bc9
JL
6282 }
6283 return argexpr;
6284}
40fe32e0 6285
b15f98b2
JL
6286static string
6287precision_to_function(long precision)
6288{
6289 switch (precision)
6290 {
6291 case 1: case -1:
6292 return "user_int8";
6293 case 2:
6294 return "user_uint16";
6295 case -2:
6296 return "user_int16";
6297 case 4:
6298 return "user_uint32";
6299 case -4:
6300 return "user_int32";
6301 case 8: case -8:
6302 return "user_int64";
6303 default:
6304 return "user_long";
6305 }
6306}
8aabf152 6307
8da56b62
JL
6308expression*
6309sdt_uprobe_var_expanding_visitor::try_parse_arg_offset_register (target_symbol *e,
6310 const string& asmarg,
6311 long precision)
6312{
6313 expression *argexpr = NULL;
8095a157 6314
8da56b62
JL
6315 // test for OFFSET(REGISTER) where OFFSET is +-N+-N+-N
6316 // NB: Despite PR11821, we can use regnames here, since the parentheses
6317 // make things unambiguous. (Note: gdb/stap-probe.c also parses this)
6318 // On ARM test for [REGISTER, OFFSET]
8095a157 6319
8da56b62
JL
6320 string regexp;
6321 int reg, offset1;
6322 if (elf_machine == EM_ARM)
6323 {
6324 regexp = "^\\[(" + regnames + ")(, #([+-]?[0-9]+)([+-][0-9]*)?([+-][0-9]*)?)?\\]$";
6325 reg = 1;
6326 offset1 = 3;
6327 }
6328 else
6329 {
6330 regexp = "^([+-]?[0-9]*)([+-][0-9]*)?([+-][0-9]*)?[(](" + regnames + ")[)]$";
6331 reg = 4;
6332 offset1 = 1;
6333 }
6334
6335 vector<string> matches;
6336 if (!regexp_match(asmarg, regexp, matches))
6337 {
6338 string regname;
6339 int64_t disp = 0;
6340 if (matches[reg].length())
6341 regname = matches[reg];
6342 if (dwarf_regs.find (regname) == dwarf_regs.end())
6343 throw SEMANTIC_ERROR(_F("unrecognized register '%s'", regname.c_str()));
6344
6345 for (int i=offset1; i <= (offset1 + 2); i++)
6346 if (matches[i].length())
6347 // should decode positive/negative hex/decimal
6348 // NB: let it throw if something happens
6349 disp += lex_cast<int64_t>(matches[i]);
6350
6351 // synthesize user_long(%{fetch_register(R)%} + D)
6352 embedded_expr *get_arg1 = new embedded_expr;
6353 get_arg1->tok = e->tok;
6354 get_arg1->code = string("/* unprivileged */ /* pure */")
a57b7534 6355 + string("u_fetch_register(")
8da56b62
JL
6356 + lex_cast(dwarf_regs[regname].first) + string(")");
6357 // XXX: may we ever need to cast that to a narrower type?
6358
6359 literal_number* inc = new literal_number(disp);
6360 inc->tok = e->tok;
6361
6362 binary_expression *be = new binary_expression;
6363 be->tok = e->tok;
6364 be->left = get_arg1;
6365 be->op = "+";
6366 be->right = inc;
6367
6368 functioncall *fc = new functioncall;
b15f98b2 6369 fc->function = precision_to_function(precision);
8da56b62
JL
6370 fc->tok = e->tok;
6371 fc->args.push_back(be);
8095a157 6372
8da56b62
JL
6373 argexpr = fc;
6374 }
8095a157 6375
8da56b62
JL
6376 return argexpr;
6377}
8095a157 6378
10fcca15
JL
6379expression*
6380sdt_uprobe_var_expanding_visitor::try_parse_arg_effective_addr (target_symbol *e,
6381 const string& asmarg,
6382 long precision)
6383{
6384 expression *argexpr = NULL;
6385
6386 // test for OFFSET(BASE_REGISTER,INDEX_REGISTER[,SCALE]) where OFFSET is +-N+-N+-N
6387 // NB: Despite PR11821, we can use regnames here, since the parentheses
6388 // make things unambiguous. (Note: gdb/stap-probe.c also parses this)
6389 string regexp = "^([+-]?[0-9]*)([+-][0-9]*)?([+-][0-9]*)?[(](" + regnames + "),(" +
6390 regnames + ")(,[1248])?[)]$";
6391 vector<string> matches;
6392 if (!regexp_match(asmarg, regexp, matches))
6393 {
6394 string baseregname;
6395 string indexregname;
6396 int64_t disp = 0;
6397 short scale = 1;
6398
6399 if (matches[6].length())
6400 // NB: let it throw if we can't cast
6401 scale = lex_cast<short>(matches[6].substr(1)); // NB: skip the comma!
6402
6403 if (matches[4].length())
6404 baseregname = matches[4];
6405 if (dwarf_regs.find (baseregname) == dwarf_regs.end())
6406 throw SEMANTIC_ERROR(_F("unrecognized base register '%s'", baseregname.c_str()));
6407
6408 if (matches[5].length())
6409 indexregname = matches[5];
6410 if (dwarf_regs.find (indexregname) == dwarf_regs.end())
6411 throw SEMANTIC_ERROR(_F("unrecognized index register '%s'", indexregname.c_str()));
6412
6413 for (int i = 1; i <= 3; i++) // up to three OFFSET terms
6414 if (matches[i].length())
6415 // should decode positive/negative hex/decimal
6416 // NB: let it throw if something happens
6417 disp += lex_cast<int64_t>(matches[i]);
6418
6419 // synthesize user_long(%{fetch_register(R1)+fetch_register(R2)*N%} + D)
6420
6421 embedded_expr *get_arg1 = new embedded_expr;
a57b7534 6422 string regfn = "u_fetch_register";
10fcca15
JL
6423
6424 get_arg1->tok = e->tok;
6425 get_arg1->code = string("/* unprivileged */ /* pure */")
6426 + regfn + string("(")+lex_cast(dwarf_regs[baseregname].first)+string(")")
6427 + string("+(")
6428 + regfn + string("(")+lex_cast(dwarf_regs[indexregname].first)+string(")")
6429 + string("*")
6430 + lex_cast(scale)
6431 + string(")");
6432
6433 // NB: could plop this +DISPLACEMENT bit into the embedded-c expression too
6434 literal_number* inc = new literal_number(disp);
6435 inc->tok = e->tok;
6436
6437 binary_expression *be = new binary_expression;
6438 be->tok = e->tok;
6439 be->left = get_arg1;
6440 be->op = "+";
6441 be->right = inc;
6442
6443 functioncall *fc = new functioncall;
b15f98b2 6444 fc->function = precision_to_function(precision);
10fcca15
JL
6445 fc->tok = e->tok;
6446 fc->args.push_back(be);
8095a157 6447
10fcca15
JL
6448 argexpr = fc;
6449 }
8095a157 6450
10fcca15
JL
6451 return argexpr;
6452}
8095a157 6453
451c0c25
JL
6454expression*
6455sdt_uprobe_var_expanding_visitor::try_parse_arg_varname (target_symbol *e,
6456 const string& asmarg,
6457 long precision)
6ef331c8 6458{
b8688ee6 6459 static unsigned tick = 0;
451c0c25
JL
6460 expression *argexpr = NULL;
6461
6462 // test for [OFF+]VARNAME[+OFF][(REGISTER)], where VARNAME is a variable
6463 // name. NB: Despite PR11821, we can use regnames here, since the parentheses
6464 // make things unambiguous.
6465 string regex = "^(([0-9]+)[+])?([a-zA-Z_][a-zA-Z0-9_]*)([+][0-9]+)?([(]("
6466 + regnames + ")[)])?$";
6467 vector<string> matches;
6468 if (!regexp_match(asmarg, regex, matches))
6469 {
6470 assert(matches.size() >= 4);
6471 string varname = matches[3];
6472
6473 // OFF can be before VARNAME (put in matches[2]) or after (put in
6474 // matches[4]) (or both?). Seems like in most cases it comes after,
6475 // unless the code was compiled with -fPIC.
6476 int64_t offset = 0;
6477 if (!matches[2].empty())
6478 offset += lex_cast<int64_t>(matches[2]);
6479 if (matches.size() >= 5 && !matches[4].empty())
6480 offset += lex_cast<int64_t>(matches[4]);
6481
6482 string regname;
6483 if (matches.size() >= 7)
6484 regname = matches[6];
6485
6486 // If it's just VARNAME, then proceed. If it's VARNAME(REGISTER), then
6487 // only proceed if it's RIP-relative addressing on x86_64.
6488 if (regname.empty() || (regname == "%rip" && elf_machine == EM_X86_64))
6489 {
6490 dw.mod_info->get_symtab();
6491 if (dw.mod_info->symtab_status != info_present)
6492 throw SEMANTIC_ERROR(_("can't retrieve symbol table"));
6493
6494 assert(dw.mod_info->sym_table);
6495 map<string, Dwarf_Addr>& globals = dw.mod_info->sym_table->globals;
6496 map<string, Dwarf_Addr>& locals = dw.mod_info->sym_table->locals;
6497 Dwarf_Addr addr = 0;
6498
6499 // check symtab locals then globals
6500 if (locals.count(varname))
6501 addr = locals[varname];
6502 if (globals.count(varname))
6503 addr = globals[varname];
6504
6505 if (addr)
8095a157 6506 {
451c0c25
JL
6507 // add whatever offset is in the operand
6508 addr += offset;
6509
6510 // adjust for dw bias because relocate_address() expects a
6511 // libdw address and this addr is from the symtab
6512 dw.get_module_dwarf(false, false);
6513 addr -= dw.module_bias;
6514
6515 string reloc_section;
6516 Dwarf_Addr reloc_addr = dw.relocate_address(addr, reloc_section);
6517
6518 // OK, we have an address for the variable. Let's create a
6519 // function that will just relocate it at runtime, and then
6520 // call user_[u]int*() on the address it returns.
6521
6522 functioncall *user_int_call = new functioncall;
b15f98b2 6523 user_int_call->function = precision_to_function(precision);
451c0c25
JL
6524 user_int_call->tok = e->tok;
6525
6526 functiondecl *get_addr_decl = new functiondecl;
6527 get_addr_decl->tok = e->tok;
6528 get_addr_decl->synthetic = true;
6529 get_addr_decl->name = "_sdt_arg_get_addr_" + lex_cast(tick++);
6530 get_addr_decl->type = pe_long;
6531
6bc53f02 6532 // build _stp_umodule_relocate(module, addr, current)
451c0c25
JL
6533 stringstream ss;
6534 ss << " /* unprivileged */ /* pure */ /* pragma:vma */" << endl;
a57b7534
JL
6535 ss << "STAP_RETURN(_stp_umodule_relocate(";
6536 ss << "\"" << path_remove_sysroot(session, process_name) << "\", ";
6537 ss << "0x" << hex << reloc_addr << dec << ", ";
6538 ss << "current";
6539 ss << "));" << endl;
451c0c25
JL
6540
6541 embeddedcode *ec = new embeddedcode;
6542 ec->tok = e->tok;
6543 ec->code = ss.str();
6544 get_addr_decl->body = ec;
6545 get_addr_decl->join(session);
6546
6547 functioncall *get_addr_call = new functioncall;
6548 get_addr_call->tok = e->tok;
6549 get_addr_call->function = get_addr_decl->name;
6550 user_int_call->args.push_back(get_addr_call);
b8688ee6 6551
451c0c25 6552 argexpr = user_int_call;
8095a157 6553 }
8aabf152 6554 }
451c0c25
JL
6555 }
6556
6557 return argexpr;
6558}
6559
6560void
6561sdt_uprobe_var_expanding_visitor::visit_target_symbol_arg (target_symbol *e)
6562{
6ef331c8
SC
6563 try
6564 {
03c45fcb 6565 unsigned argno = get_target_symbol_argno_and_validate(e); // the N in $argN
8aabf152 6566 string asmarg = arg_tokens[argno-1]; // $arg1 => arg_tokens[0]
8aabf152 6567
8aabf152
FCE
6568 // Now we try to parse this thing, which is an assembler operand
6569 // expression. If we can't, we warn, back down to need_debug_info
b874bd52 6570 // and hope for the best. Here is the syntax for a few architectures.
9859b766 6571 // Note that the power iN syntax is only for V3 sdt.h; gcc emits the i.
8095a157 6572 //
b6aaaf21
JL
6573 // literal reg reg reg+ base+index*size+ VAR VAR+off RIP-relative
6574 // indirect offset offset VAR+off
6575 // x86 $N %rR (%rR) N(%rR) O(%bR,%iR,S) var var+off var+off(%rip)
6576 // x86_64 $N %rR (%rR) N(%rR) O(%bR,%iR,S) var var+off var+off(%rip)
6577 // power iN R (R) N(R)
6578 // ia64 N rR [r16]
6579 // s390 N %rR 0(rR) N(r15)
6580 // arm #N rR [rR] [rR, #N]
b874bd52 6581
8aabf152
FCE
6582 expression* argexpr = 0; // filled in in case of successful parse
6583
ac074daf
JL
6584 // Parse (and remove from asmarg) the leading length
6585 long precision = parse_out_arg_precision(asmarg);
40fe32e0 6586
0fbf6fb8 6587 try
8aabf152 6588 {
0fbf6fb8
JL
6589 if ((argexpr = try_parse_arg_literal(e, asmarg, precision)) != NULL)
6590 goto matched;
2e6d617a
JL
6591
6592 // all other matches require registers
6593 if (regnames == "")
6594 throw SEMANTIC_ERROR("no registers to use for parsing");
95b78bc9
JL
6595
6596 if ((argexpr = try_parse_arg_register(e, asmarg, precision)) != NULL)
6597 goto matched;
8da56b62
JL
6598 if ((argexpr = try_parse_arg_offset_register(e, asmarg, precision)) != NULL)
6599 goto matched;
10fcca15
JL
6600 if ((argexpr = try_parse_arg_effective_addr(e, asmarg, precision)) != NULL)
6601 goto matched;
451c0c25
JL
6602 if ((argexpr = try_parse_arg_varname(e, asmarg, precision)) != NULL)
6603 goto matched;
0fbf6fb8
JL
6604 }
6605 catch (const semantic_error& err)
6606 {
6607 e->chain(err);
b8688ee6 6608 }
8aabf152 6609
8aabf152
FCE
6610 // The asmarg operand was not recognized. Back down to dwarf.
6611 if (! session.suppress_warnings)
84fef8ee
FCE
6612 {
6613 if (probe_type == UPROBE3_TYPE)
451c0c25
JL
6614 session.print_warning (_F("Can't parse SDT_V3 operand '%s' "
6615 "[man error::sdt]", asmarg.c_str()),
6616 e->tok);
84fef8ee 6617 else // must be *PROBE2; others don't get asm operands
451c0c25
JL
6618 session.print_warning (_F("Downgrading SDT_V2 probe argument to "
6619 "dwarf, can't parse '%s' [man error::sdt]",
6620 asmarg.c_str()),
6621 e->tok);
84fef8ee 6622 }
451c0c25 6623
8aabf152 6624 need_debug_info = true;
451c0c25
JL
6625 throw SEMANTIC_ERROR(_("SDT asm not understood, requires debuginfo "
6626 "[man error::sdt]"), e->tok);
6627
6628 /* NOTREACHED */
366af4e7 6629
8aabf152
FCE
6630 matched:
6631 assert (argexpr != 0);
366af4e7
RM
6632
6633 if (session.verbose > 2)
1e41115c 6634 //TRANSLATORS: We're mapping the operand to a new expression*.
b530b5b3 6635 clog << _F("mapped asm operand %s to ", asmarg.c_str()) << *argexpr << endl;
366af4e7 6636
aff5d390 6637 if (e->components.empty()) // We have a scalar
8aabf152
FCE
6638 {
6639 if (e->addressof)
dc09353a 6640 throw SEMANTIC_ERROR(_("cannot take address of sdt variable"), e->tok);
8aabf152 6641 provide (argexpr);
8aabf152
FCE
6642 }
6643 else // $var->foo
6644 {
6645 cast_op *cast = new cast_op;
6646 cast->name = "@cast";
6647 cast->tok = e->tok;
6648 cast->operand = argexpr;
6649 cast->components = e->components;
6650 cast->type_name = probe_name + "_arg" + lex_cast(argno);
6651 cast->module = process_name;
6652 cast->visit(this);
8aabf152 6653 }
aff5d390
SC
6654 }
6655 catch (const semantic_error &er)
6656 {
6657 e->chain (er);
6658 provide (e);
6659 }
6660}
6661
6662
6ef331c8
SC
6663void
6664sdt_uprobe_var_expanding_visitor::visit_target_symbol (target_symbol* e)
6665{
6666 try
6667 {
49131a6d 6668 assert(e->name.size() > 0
bd1fcbad 6669 && (e->name[0] == '$' || e->name == "@var"));
6ef331c8
SC
6670
6671 if (e->name == "$$name" || e->name == "$$provider" || e->name == "$$parms" || e->name == "$$vars")
6672 visit_target_symbol_context (e);
6673 else
6674 visit_target_symbol_arg (e);
6675 }
6676 catch (const semantic_error &er)
6677 {
6678 e->chain (er);
6679 provide (e);
6680 }
6681}
6682
6683
bd1fcbad
YZ
6684void
6685sdt_uprobe_var_expanding_visitor::visit_atvar_op (atvar_op* e)
6686{
6687 need_debug_info = true;
6688
6689 // Fill in our current module context if needed
6690 if (e->module.empty())
6691 e->module = process_name;
6692
6693 var_expanding_visitor::visit_atvar_op(e);
6694}
6695
6696
40a0c64e
JS
6697void
6698sdt_uprobe_var_expanding_visitor::visit_cast_op (cast_op* e)
6699{
6700 // Fill in our current module context if needed
6701 if (e->module.empty())
6702 e->module = process_name;
6703
6704 var_expanding_visitor::visit_cast_op(e);
6705}
6706
6707
576eaefe
SC
6708void
6709plt_expanding_visitor::visit_target_symbol (target_symbol *e)
6710{
6711 try
6712 {
6713 if (e->name == "$$name")
6714 {
6715 literal_string *myname = new literal_string (entry);
6716 myname->tok = e->tok;
6717 provide(myname);
6718 return;
6719 }
3d69c03f
JS
6720
6721 // variable not found -> throw a semantic error
6722 // (only to be caught right away, but this may be more complex later...)
6723 string alternatives = "$$name";
dc09353a 6724 throw SEMANTIC_ERROR(_F("unable to find plt variable '%s' (alternatives: %s)",
3d69c03f 6725 e->name.c_str(), alternatives.c_str()), e->tok);
576eaefe
SC
6726 }
6727 catch (const semantic_error &er)
6728 {
6729 e->chain (er);
6730 provide (e);
6731 }
6732}
6733
6734
edce5b67
JS
6735struct sdt_query : public base_query
6736{
6737 sdt_query(probe * base_probe, probe_point * base_loc,
6738 dwflpp & dw, literal_map_t const & params,
51d6bda3 6739 vector<derived_probe *> & results, const string user_lib);
edce5b67 6740
51d6bda3 6741 void query_library (const char *data);
614f0fcf
JL
6742 set<string> visited_libraries;
6743 bool resolved_library;
6744
576eaefe 6745 void query_plt (const char *entry, size_t addr) {}
edce5b67
JS
6746 void handle_query_module();
6747
6748private:
15284963 6749 stap_sdt_probe_type probe_type;
d61ea602 6750 enum { probe_section=0, note_section=1, unknown_section=-1 } probe_loc;
edce5b67
JS
6751 probe * base_probe;
6752 probe_point * base_loc;
6846cfc8 6753 literal_map_t const & params;
edce5b67 6754 vector<derived_probe *> & results;
a794dbeb
FCE
6755 string pp_mark;
6756 string pp_provider;
51d6bda3 6757 string user_lib;
edce5b67
JS
6758
6759 set<string> probes_handled;
6760
6761 Elf_Data *pdata;
6762 size_t probe_scn_offset;
6763 size_t probe_scn_addr;
aff5d390 6764 uint64_t arg_count;
40fe32e0 6765 GElf_Addr base;
c57ea854 6766 GElf_Addr pc;
aff5d390 6767 string arg_string;
edce5b67 6768 string probe_name;
a794dbeb 6769 string provider_name;
909ab234 6770 GElf_Addr semaphore_load_offset;
79a0ca08 6771 Dwarf_Addr semaphore;
edce5b67
JS
6772
6773 bool init_probe_scn();
6b51ee12 6774 bool get_next_probe();
c57ea854
SC
6775 void iterate_over_probe_entries();
6776 void handle_probe_entry();
edce5b67 6777
488d3c1b 6778 static void setup_note_probe_entry_callback (sdt_query *me,
dbd24034
JL
6779 const string& scn_name,
6780 const string& note_name,
488d3c1b
JL
6781 int type,
6782 const char *data,
6783 size_t len);
dbd24034
JL
6784 void setup_note_probe_entry (const string& scn_name,
6785 const string& note_name, int type,
6786 const char *data, size_t len);
40fe32e0 6787
edce5b67 6788 void convert_probe(probe *base);
4ddb6dd0 6789 void record_semaphore(vector<derived_probe *> & results, unsigned start);
c72aa911 6790 probe* convert_location();
40fe32e0 6791 bool have_uprobe() {return probe_type == uprobe1_type || probe_type == uprobe2_type || probe_type == uprobe3_type;}
c57ea854
SC
6792 bool have_debuginfo_uprobe(bool need_debug_info)
6793 {return probe_type == uprobe1_type
40fe32e0 6794 || ((probe_type == uprobe2_type || probe_type == uprobe3_type)
c57ea854 6795 && need_debug_info);}
40fe32e0 6796 bool have_debuginfoless_uprobe() {return probe_type == uprobe2_type || probe_type == uprobe3_type;}
edce5b67
JS
6797};
6798
6799
6800sdt_query::sdt_query(probe * base_probe, probe_point * base_loc,
6801 dwflpp & dw, literal_map_t const & params,
51d6bda3 6802 vector<derived_probe *> & results, const string user_lib):
614f0fcf
JL
6803 base_query(dw, params), resolved_library(false),
6804 probe_type(unknown_probe_type), probe_loc(unknown_section),
6805 base_probe(base_probe), base_loc(base_loc), params(params), results(results),
6806 user_lib(user_lib), probe_scn_offset(0), probe_scn_addr(0), arg_count(0),
6807 base(0), pc(0), semaphore_load_offset(0), semaphore(0)
edce5b67 6808{
a794dbeb
FCE
6809 assert(get_string_param(params, TOK_MARK, pp_mark));
6810 get_string_param(params, TOK_PROVIDER, pp_provider); // pp_provider == "" -> unspecified
6811
ef428667
FCE
6812 // PR10245: permit usage of dtrace-y "-" separator in marker name;
6813 // map it to double-underscores.
6814 size_t pos = 0;
6815 while (1) // there may be more than one
6816 {
a794dbeb 6817 size_t i = pp_mark.find("-", pos);
ef428667 6818 if (i == string::npos) break;
a794dbeb 6819 pp_mark.replace (i, 1, "__");
ef428667
FCE
6820 pos = i+1; // resume searching after the inserted __
6821 }
a794dbeb
FCE
6822
6823 // XXX: same for pp_provider?
edce5b67
JS
6824}
6825
6826
6827void
c57ea854 6828sdt_query::handle_probe_entry()
edce5b67 6829{
c57ea854
SC
6830 if (! have_uprobe()
6831 && !probes_handled.insert(probe_name).second)
edce5b67
JS
6832 return;
6833
6834 if (sess.verbose > 3)
c57ea854 6835 {
b78a0fbb 6836 //TRANSLATORS: Describing what probe type (kprobe or uprobe) the probe
b530b5b3
LB
6837 //TRANSLATORS: is matched to.
6838 clog << _F("matched probe_name %s probe type ", probe_name.c_str());
c57ea854
SC
6839 switch (probe_type)
6840 {
6841 case uprobe1_type:
6842 clog << "uprobe1 at 0x" << hex << pc << dec << endl;
6843 break;
6844 case uprobe2_type:
6845 clog << "uprobe2 at 0x" << hex << pc << dec << endl;
6846 break;
40fe32e0
SC
6847 case uprobe3_type:
6848 clog << "uprobe3 at 0x" << hex << pc << dec << endl;
6849 break;
d61ea602
JS
6850 default:
6851 clog << "unknown!" << endl;
6852 break;
c57ea854
SC
6853 }
6854 }
edce5b67 6855
c57ea854
SC
6856 // Extend the derivation chain
6857 probe *new_base = convert_location();
6858 probe_point *new_location = new_base->locations[0];
6859
c57ea854
SC
6860 bool need_debug_info = false;
6861
7d395255
JS
6862 // We could get the Elf* from either dwarf_getelf(dwfl_module_getdwarf(...))
6863 // or dwfl_module_getelf(...). We only need it for the machine type, which
6864 // should be the same. The bias is used for relocating debuginfoless probes,
6865 // though, so that must come from the possibly-prelinked ELF file, not DWARF.
c57ea854 6866 Dwarf_Addr bias;
7d395255 6867 Elf* elf = dwfl_module_getelf (dw.mod_info->mod, &bias);
c57ea854 6868
1cc41cd6
DS
6869 /* Figure out the architecture of this particular ELF file. The
6870 dwarfless register-name mappings depend on it. */
6871 GElf_Ehdr ehdr_mem;
6872 GElf_Ehdr* em = gelf_getehdr (elf, &ehdr_mem);
e1c3b13a 6873 if (em == 0) { DWFL_ASSERT ("dwfl_getehdr", dwfl_errno()); }
d8f31d0a 6874 assert(em);
1cc41cd6 6875 int elf_machine = em->e_machine;
b8688ee6
JL
6876 sdt_uprobe_var_expanding_visitor svv (sess, dw, elf_machine, module_val,
6877 provider_name, probe_name, probe_type,
6878 arg_string, arg_count);
1cc41cd6
DS
6879 svv.replace (new_base->body);
6880 need_debug_info = svv.need_debug_info;
c57ea854 6881
1cc41cd6
DS
6882 // XXX: why not derive_probes() in the uprobes case too?
6883 literal_map_t params;
6884 for (unsigned i = 0; i < new_location->components.size(); ++i)
6885 {
6886 probe_point::component *c = new_location->components[i];
6887 params[c->functor] = c->arg;
6888 }
c57ea854 6889
73d53dd2 6890 unsigned prior_results_size = results.size();
1cc41cd6
DS
6891 dwarf_query q(new_base, new_location, dw, params, results, "", "");
6892 q.has_mark = true; // enables mid-statement probing
30263a73 6893
73d53dd2
JS
6894 // V1 probes always need dwarf info
6895 // V2+ probes need dwarf info in case of a variable reference
1cc41cd6 6896 if (have_debuginfo_uprobe(need_debug_info))
06de3a04 6897 dw.iterate_over_modules<base_query>(&query_module, &q);
73d53dd2
JS
6898
6899 // For V2+ probes, if variable references weren't used or failed (PR14369),
6900 // then try with the more direct approach. Unresolved $vars might still
6901 // cause their own error, but this gives them a chance to be optimized out.
6902 if (have_debuginfoless_uprobe() && results.size() == prior_results_size)
1cc41cd6
DS
6903 {
6904 string section;
6905 Dwarf_Addr reloc_addr = q.statement_num_val + bias;
6906 if (dwfl_module_relocations (q.dw.mod_info->mod) > 0)
6907 {
6908 dwfl_module_relocate_address (q.dw.mod_info->mod, &reloc_addr);
6909 section = ".dynamic";
6910 }
6911 else
6912 section = ".absolute";
edce5b67 6913
1cc41cd6
DS
6914 uprobe_derived_probe* p =
6915 new uprobe_derived_probe ("", "", 0,
6916 path_remove_sysroot(sess,q.module_val),
6917 section,
6918 q.statement_num_val, reloc_addr, q, 0);
6919 p->saveargs (arg_count);
6920 results.push_back (p);
c57ea854 6921 }
487bf4e2 6922 sess.unwindsym_modules.insert (dw.module_name);
73d53dd2 6923 record_semaphore(results, prior_results_size);
c57ea854 6924}
edce5b67 6925
4ddb6dd0 6926
c57ea854
SC
6927void
6928sdt_query::handle_query_module()
6929{
6930 if (!init_probe_scn())
6931 return;
edce5b67 6932
c57ea854
SC
6933 if (sess.verbose > 3)
6934 clog << "TOK_MARK: " << pp_mark << " TOK_PROVIDER: " << pp_provider << endl;
edce5b67 6935
40fe32e0
SC
6936 if (probe_loc == note_section)
6937 {
6938 GElf_Shdr shdr_mem;
6939 GElf_Shdr *shdr = dw.get_section (".stapsdt.base", &shdr_mem);
6940
909ab234
JS
6941 // The 'base' lets us adjust the hardcoded addresses in notes for prelink
6942 // effects. The 'semaphore_load_offset' accounts for the difference in
6943 // load addresses between text and data, so the semaphore can be
6944 // converted to a file offset if needed.
40fe32e0 6945 if (shdr)
909ab234
JS
6946 {
6947 base = shdr->sh_addr;
6948 GElf_Addr base_offset = shdr->sh_offset;
6949 shdr = dw.get_section (".probes", &shdr_mem);
6950 if (shdr)
6951 semaphore_load_offset =
6952 (shdr->sh_addr - shdr->sh_offset) - (base - base_offset);
6953 }
40fe32e0 6954 else
909ab234
JS
6955 base = semaphore_load_offset = 0;
6956
488d3c1b 6957 dw.iterate_over_notes (this, &sdt_query::setup_note_probe_entry_callback);
40fe32e0 6958 }
d61ea602 6959 else if (probe_loc == probe_section)
40fe32e0 6960 iterate_over_probe_entries ();
edce5b67
JS
6961}
6962
6963
6964bool
6965sdt_query::init_probe_scn()
6966{
448a86b7 6967 Elf* elf;
edce5b67 6968 GElf_Shdr shdr_mem;
40fe32e0
SC
6969
6970 GElf_Shdr *shdr = dw.get_section (".note.stapsdt", &shdr_mem);
6971 if (shdr)
6972 {
6973 probe_loc = note_section;
6974 return true;
6975 }
edce5b67 6976
448a86b7 6977 shdr = dw.get_section (".probes", &shdr_mem, &elf);
fea74777 6978 if (shdr)
edce5b67 6979 {
fea74777
SC
6980 pdata = elf_getdata_rawchunk (elf, shdr->sh_offset, shdr->sh_size, ELF_T_BYTE);
6981 probe_scn_offset = 0;
6982 probe_scn_addr = shdr->sh_addr;
6983 assert (pdata != NULL);
6984 if (sess.verbose > 4)
ce0f6648
LB
6985 clog << "got .probes elf scn_addr@0x" << probe_scn_addr << ", size: "
6986 << pdata->d_size << endl;
40fe32e0 6987 probe_loc = probe_section;
fea74777 6988 return true;
edce5b67 6989 }
fea74777 6990 else
edce5b67 6991 return false;
edce5b67
JS
6992}
6993
40fe32e0 6994void
dbd24034
JL
6995sdt_query::setup_note_probe_entry_callback (sdt_query *me,
6996 const string& scn_name,
6997 const string& note_name, int type,
6998 const char *data, size_t len)
40fe32e0 6999{
dbd24034 7000 me->setup_note_probe_entry (scn_name, note_name, type, data, len);
40fe32e0
SC
7001}
7002
7003
7004void
dbd24034
JL
7005sdt_query::setup_note_probe_entry (const string& scn_name,
7006 const string& note_name, int type,
7007 const char *data, size_t len)
40fe32e0 7008{
dbd24034
JL
7009 if (scn_name.compare(".note.stapsdt"))
7010 return;
7011#define _SDT_NOTE_NAME "stapsdt"
7012 if (note_name.compare(_SDT_NOTE_NAME))
7013 return;
40fe32e0
SC
7014#define _SDT_NOTE_TYPE 3
7015 if (type != _SDT_NOTE_TYPE)
7016 return;
7017
dbd24034 7018 // we found a probe entry
40fe32e0
SC
7019 union
7020 {
7021 Elf64_Addr a64[3];
7022 Elf32_Addr a32[3];
7023 } buf;
7024 Dwarf_Addr bias;
7025 Elf* elf = (dwfl_module_getelf (dw.mod_info->mod, &bias));
7026 Elf_Data dst =
7027 {
7028 &buf, ELF_T_ADDR, EV_CURRENT,
7029 gelf_fsize (elf, ELF_T_ADDR, 3, EV_CURRENT), 0, 0
7030 };
7031 assert (dst.d_size <= sizeof buf);
7032
7033 if (len < dst.d_size + 3)
7034 return;
7035
7036 Elf_Data src =
7037 {
7038 (void *) data, ELF_T_ADDR, EV_CURRENT,
7039 dst.d_size, 0, 0
7040 };
7041
7042 if (gelf_xlatetom (elf, &dst, &src,
7043 elf_getident (elf, NULL)[EI_DATA]) == NULL)
7044 printf ("gelf_xlatetom: %s", elf_errmsg (-1));
7045
7046 probe_type = uprobe3_type;
7047 const char * provider = data + dst.d_size;
3f803f9e 7048
40fe32e0 7049 const char *name = (const char*)memchr (provider, '\0', data + len - provider);
3f803f9e
CM
7050 if(name++ == NULL)
7051 return;
7052
7053 const char *args = (const char*)memchr (name, '\0', data + len - name);
7054 if (args++ == NULL || memchr (args, '\0', data + len - name) != data + len - 1)
7055 return;
7056
7057 provider_name = provider;
7058 probe_name = name;
7059 arg_string = args;
40fe32e0 7060
e90006d7
JL
7061 dw.mod_info->marks.insert(make_pair(provider, name));
7062
40fe32e0
SC
7063 // Did we find a matching probe?
7064 if (! (dw.function_name_matches_pattern (probe_name, pp_mark)
7065 && ((pp_provider == "")
7066 || dw.function_name_matches_pattern (provider_name, pp_provider))))
7067 return;
7068
e9a90eee
JS
7069 // PR13934: Assembly probes are not forced to use the N@OP form.
7070 // If we have '@' then great, else count based on space-delimiters.
7071 arg_count = count(arg_string.begin(), arg_string.end(), '@');
7072 if (!arg_count && !arg_string.empty())
7073 arg_count = 1 + count(arg_string.begin(), arg_string.end(), ' ');
7074
40fe32e0
SC
7075 GElf_Addr base_ref;
7076 if (gelf_getclass (elf) == ELFCLASS32)
7077 {
7078 pc = buf.a32[0];
7079 base_ref = buf.a32[1];
7080 semaphore = buf.a32[2];
7081 }
7082 else
7083 {
7084 pc = buf.a64[0];
7085 base_ref = buf.a64[1];
7086 semaphore = buf.a64[2];
7087 }
7088
7089 semaphore += base - base_ref;
7090 pc += base - base_ref;
7091
7d395255
JS
7092 // The semaphore also needs the ELF bias added now, so
7093 // record_semaphore can properly relocate it later.
7094 semaphore += bias;
7095
40fe32e0 7096 if (sess.verbose > 4)
b530b5b3 7097 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
7098
7099 handle_probe_entry();
7100}
7101
7102
c57ea854
SC
7103void
7104sdt_query::iterate_over_probe_entries()
edce5b67 7105{
c57ea854 7106 // probes are in the .probe section
edce5b67
JS
7107 while (probe_scn_offset < pdata->d_size)
7108 {
aff5d390
SC
7109 stap_sdt_probe_entry_v1 *pbe_v1 = (stap_sdt_probe_entry_v1 *) ((char*)pdata->d_buf + probe_scn_offset);
7110 stap_sdt_probe_entry_v2 *pbe_v2 = (stap_sdt_probe_entry_v2 *) ((char*)pdata->d_buf + probe_scn_offset);
15284963 7111 probe_type = (stap_sdt_probe_type)(pbe_v1->type_a);
1cc41cd6 7112 if (! have_uprobe())
edce5b67
JS
7113 {
7114 // Unless this is a mangled .probes section, this happens
7115 // because the name of the probe comes first, followed by
7116 // the sentinel.
7117 if (sess.verbose > 5)
b530b5b3 7118 clog << _F("got unknown probe_type : 0x%x", probe_type) << endl;
edce5b67
JS
7119 probe_scn_offset += sizeof(__uint32_t);
7120 continue;
7121 }
aff5d390
SC
7122 if ((long)pbe_v1 % sizeof(__uint64_t)) // we have stap_sdt_probe_entry_v1.type_b
7123 {
7124 pbe_v1 = (stap_sdt_probe_entry_v1*)((char*)pbe_v1 - sizeof(__uint32_t));
1cc41cd6 7125 if (pbe_v1->type_b != uprobe1_type)
aff5d390
SC
7126 continue;
7127 }
7128
1cc41cd6 7129 if (probe_type == uprobe1_type)
aff5d390 7130 {
79a0ca08 7131 if (pbe_v1->name == 0) // No name possibly means we have a .so with a relocation
c57ea854 7132 return;
79a0ca08 7133 semaphore = 0;
aff5d390 7134 probe_name = (char*)((char*)pdata->d_buf + pbe_v1->name - (char*)probe_scn_addr);
a794dbeb 7135 provider_name = ""; // unknown
1cc41cd6
DS
7136 pc = pbe_v1->arg;
7137 arg_count = 0;
aff5d390
SC
7138 probe_scn_offset += sizeof (stap_sdt_probe_entry_v1);
7139 }
08b5a50c 7140 else if (probe_type == uprobe2_type)
aff5d390 7141 {
79a0ca08 7142 if (pbe_v2->name == 0) // No name possibly means we have a .so with a relocation
c57ea854 7143 return;
79a0ca08 7144 semaphore = pbe_v2->semaphore;
aff5d390 7145 probe_name = (char*)((char*)pdata->d_buf + pbe_v2->name - (char*)probe_scn_addr);
a794dbeb 7146 provider_name = (char*)((char*)pdata->d_buf + pbe_v2->provider - (char*)probe_scn_addr);
aff5d390
SC
7147 arg_count = pbe_v2->arg_count;
7148 pc = pbe_v2->pc;
7149 if (pbe_v2->arg_string)
7150 arg_string = (char*)((char*)pdata->d_buf + pbe_v2->arg_string - (char*)probe_scn_addr);
79a0ca08
SC
7151 // skip over pbe_v2, probe_name text and provider text
7152 probe_scn_offset = ((long)(pbe_v2->name) - (long)(probe_scn_addr)) + probe_name.length();
7153 probe_scn_offset += sizeof (__uint32_t) - probe_scn_offset % sizeof (__uint32_t);
aff5d390 7154 }
edce5b67 7155 if (sess.verbose > 4)
b530b5b3 7156 clog << _("saw .probes ") << probe_name << (provider_name != "" ? _(" (provider ")+provider_name+") " : "")
aff5d390 7157 << "@0x" << hex << pc << dec << endl;
edce5b67 7158
e90006d7
JL
7159 dw.mod_info->marks.insert(make_pair(provider_name, probe_name));
7160
a794dbeb
FCE
7161 if (dw.function_name_matches_pattern (probe_name, pp_mark)
7162 && ((pp_provider == "") || dw.function_name_matches_pattern (provider_name, pp_provider)))
c57ea854 7163 handle_probe_entry ();
edce5b67 7164 }
edce5b67
JS
7165}
7166
7167
6846cfc8 7168void
4ddb6dd0 7169sdt_query::record_semaphore (vector<derived_probe *> & results, unsigned start)
6846cfc8 7170{
a794dbeb
FCE
7171 for (unsigned i=0; i<2; i++) {
7172 // prefer with-provider symbol; look without provider prefix for backward compatibility only
7173 string semaphore = (i==0 ? (provider_name+"_") : "") + probe_name + "_semaphore";
7174 // XXX: multiple addresses?
7175 if (sess.verbose > 2)
b530b5b3 7176 clog << _F("looking for semaphore symbol %s ", semaphore.c_str());
a794dbeb 7177
79a0ca08
SC
7178 Dwarf_Addr addr;
7179 if (this->semaphore)
7180 addr = this->semaphore;
7181 else
7182 addr = lookup_symbol_address(dw.module, semaphore.c_str());
a794dbeb
FCE
7183 if (addr)
7184 {
7d395255 7185 if (dwfl_module_relocations (dw.module) > 0)
a794dbeb
FCE
7186 dwfl_module_relocate_address (dw.module, &addr);
7187 // XXX: relocation basis?
909ab234
JS
7188
7189 // Dyninst needs the *file*-based offset for semaphores,
7190 // so subtract the difference in load addresses between .text and .probes
7191 if (dw.sess.runtime_usermode_p())
7192 addr -= semaphore_load_offset;
7193
a794dbeb
FCE
7194 for (unsigned i = start; i < results.size(); ++i)
7195 results[i]->sdt_semaphore_addr = addr;
7196 if (sess.verbose > 2)
b530b5b3 7197 clog << _(", found at 0x") << hex << addr << dec << endl;
a794dbeb
FCE
7198 return;
7199 }
7200 else
7201 if (sess.verbose > 2)
b530b5b3 7202 clog << _(", not found") << endl;
a794dbeb 7203 }
6846cfc8
SC
7204}
7205
7206
edce5b67
JS
7207void
7208sdt_query::convert_probe (probe *base)
7209{
7210 block *b = new block;
7211 b->tok = base->body->tok;
7212
edce5b67
JS
7213 // Generate: if (arg1 != mark("label")) next;
7214 functioncall *fc = new functioncall;
bbafcb1e 7215 fc->function = "ulong_arg";
edce5b67 7216 fc->tok = b->tok;
bbafcb1e 7217 literal_number* num = new literal_number(1);
edce5b67
JS
7218 num->tok = b->tok;
7219 fc->args.push_back(num);
7220
7221 functioncall *fcus = new functioncall;
7222 fcus->function = "user_string";
7223 fcus->type = pe_string;
7224 fcus->tok = b->tok;
7225 fcus->args.push_back(fc);
7226
7227 if_statement *is = new if_statement;
7228 is->thenblock = new next_statement;
7229 is->elseblock = NULL;
7230 is->tok = b->tok;
63ea4244 7231 is->thenblock->tok = b->tok;
edce5b67
JS
7232 comparison *be = new comparison;
7233 be->op = "!=";
7234 be->tok = b->tok;
7235 be->left = fcus;
7236 be->right = new literal_string(probe_name);
63ea4244 7237 be->right->tok = b->tok;
edce5b67
JS
7238 is->condition = be;
7239 b->statements.push_back(is);
7240
7241 // Now replace the body
7242 b->statements.push_back(base->body);
7243 base->body = b;
7244}
7245
7246
c72aa911
JS
7247probe*
7248sdt_query::convert_location ()
edce5b67 7249{
684b8718
JL
7250 string module = dw.module_name;
7251 if (has_process)
7252 module = path_remove_sysroot(sess, module);
7253
c72aa911 7254 probe_point* specific_loc = new probe_point(*base_loc);
684b8718
JL
7255 specific_loc->well_formed = true;
7256
662539d9 7257 vector<probe_point::component*> derived_comps;
edce5b67 7258
662539d9
JS
7259 vector<probe_point::component*>::iterator it;
7260 for (it = specific_loc->components.begin();
7261 it != specific_loc->components.end(); ++it)
7262 if ((*it)->functor == TOK_PROCESS)
7263 {
684b8718
JL
7264 // replace the possibly incomplete path to process
7265 *it = new probe_point::component(TOK_PROCESS,
7266 new literal_string(has_library ? path : module));
7267
1cc41cd6
DS
7268 // copy the process name
7269 derived_comps.push_back(*it);
662539d9
JS
7270 }
7271 else if ((*it)->functor == TOK_LIBRARY)
7272 {
1cc41cd6
DS
7273 // copy the library name for process probes
7274 derived_comps.push_back(*it);
662539d9
JS
7275 }
7276 else if ((*it)->functor == TOK_PROVIDER)
7277 {
7278 // replace the possibly wildcarded arg with the specific provider name
7279 *it = new probe_point::component(TOK_PROVIDER,
7280 new literal_string(provider_name));
7281 }
7282 else if ((*it)->functor == TOK_MARK)
c72aa911
JS
7283 {
7284 // replace the possibly wildcarded arg with the specific marker name
662539d9
JS
7285 *it = new probe_point::component(TOK_MARK,
7286 new literal_string(probe_name));
a794dbeb 7287
aff5d390
SC
7288 if (sess.verbose > 3)
7289 switch (probe_type)
7290 {
7291 case uprobe1_type:
b530b5b3 7292 clog << _("probe_type == uprobe1, use statement addr: 0x")
aff5d390
SC
7293 << hex << pc << dec << endl;
7294 break;
7295 case uprobe2_type:
b530b5b3 7296 clog << _("probe_type == uprobe2, use statement addr: 0x")
aff5d390
SC
7297 << hex << pc << dec << endl;
7298 break;
40fe32e0 7299 case uprobe3_type:
b530b5b3 7300 clog << _("probe_type == uprobe3, use statement addr: 0x")
40fe32e0
SC
7301 << hex << pc << dec << endl;
7302 break;
aff5d390 7303 default:
b530b5b3
LB
7304 clog << _F("probe_type == use_uprobe_no_dwarf, use label name: _stapprobe1_%s",
7305 pp_mark.c_str()) << endl;
aff5d390
SC
7306 }
7307
c72aa911
JS
7308 switch (probe_type)
7309 {
aff5d390
SC
7310 case uprobe1_type:
7311 case uprobe2_type:
40fe32e0 7312 case uprobe3_type:
c72aa911 7313 // process("executable").statement(probe_arg)
662539d9
JS
7314 derived_comps.push_back
7315 (new probe_point::component(TOK_STATEMENT,
7316 new literal_number(pc, true)));
c72aa911
JS
7317 break;
7318
a794dbeb 7319 default: // deprecated
c72aa911 7320 // process("executable").function("*").label("_stapprobe1_MARK_NAME")
662539d9
JS
7321 derived_comps.push_back
7322 (new probe_point::component(TOK_FUNCTION,
7323 new literal_string("*")));
7324 derived_comps.push_back
c72aa911 7325 (new probe_point::component(TOK_LABEL,
a794dbeb 7326 new literal_string("_stapprobe1_" + pp_mark)));
c72aa911
JS
7327 break;
7328 }
7329 }
edce5b67 7330
662539d9
JS
7331 probe_point* derived_loc = new probe_point(*specific_loc);
7332 derived_loc->components = derived_comps;
8159bf55 7333 return new probe (new probe (base_probe, specific_loc), derived_loc);
edce5b67
JS
7334}
7335
7336
51d6bda3
SC
7337void
7338sdt_query::query_library (const char *library)
7339{
614f0fcf
JL
7340 visited_libraries.insert(library);
7341 if (query_one_library (library, dw, user_lib, base_probe, base_loc, results))
7342 resolved_library = true;
51d6bda3
SC
7343}
7344
e90006d7
JL
7345string
7346suggest_marks(systemtap_session& sess,
7347 const set<string>& modules,
7348 const string& mark,
7349 const string& provider)
7350{
7351 if (mark.empty() || modules.empty() || sess.module_cache == NULL)
7352 return "";
7353
7354 set<string> marks;
7355 const map<string, module_info*> &cache = sess.module_cache->cache;
7356 bool dash_suggestions = (mark.find("-") != string::npos);
7357
7358 for (set<string>::iterator itmod = modules.begin();
7359 itmod != modules.end(); ++itmod)
7360 {
7361 map<string, module_info*>::const_iterator itcache;
7362 if ((itcache = cache.find(*itmod)) != cache.end())
7363 {
7364 set<pair<string,string> >::const_iterator itmarks;
7365 for (itmarks = itcache->second->marks.begin();
7366 itmarks != itcache->second->marks.end(); ++itmarks)
7367 {
7368 if (provider.empty()
7369 // simulating dw.function_name_matches_pattern()
7370 || (fnmatch(provider.c_str(), itmarks->first.c_str(), 0) == 0))
7371 {
7372 string marksug = itmarks->second;
7373 if (dash_suggestions)
7374 {
7375 size_t pos = 0;
7376 while (1) // there may be more than one
7377 {
7378 size_t i = marksug.find("__", pos);
7379 if (i == string::npos) break;
7380 marksug.replace (i, 2, "-");
7381 pos = i+1; // resume searching after the inserted -
7382 }
7383 }
7384 marks.insert(marksug);
7385 }
7386 }
7387 }
7388 }
7389
7390 if (sess.verbose > 2)
7391 {
7392 clog << "suggesting " << marks.size() << " marks "
7393 << "from modules:" << endl;
7394 for (set<string>::iterator itmod = modules.begin();
7395 itmod != modules.end(); ++itmod)
7396 clog << *itmod << endl;
7397 }
7398
7399 if (marks.empty())
7400 return "";
7401
7402 return levenshtein_suggest(mark, marks, 5); // print top 5 marks only
7403}
7404
40a70f52
JL
7405string
7406suggest_plt_functions(systemtap_session& sess,
7407 const set<string>& modules,
7408 const string& func)
7409{
7410 if (func.empty() || modules.empty() || sess.module_cache == NULL)
7411 return "";
7412
7413 set<string> funcs;
7414 const map<string, module_info*> &cache = sess.module_cache->cache;
7415
7416 for (set<string>::iterator itmod = modules.begin();
7417 itmod != modules.end(); ++itmod)
7418 {
7419 map<string, module_info*>::const_iterator itcache;
7420 if ((itcache = cache.find(*itmod)) != cache.end())
7421 funcs.insert(itcache->second->plt_funcs.begin(),
7422 itcache->second->plt_funcs.end());
7423 }
7424
7425 if (sess.verbose > 2)
d82dd71f
JL
7426 {
7427 clog << "suggesting " << funcs.size() << " plt functions "
7428 << "from modules:" << endl;
7429 for (set<string>::iterator itmod = modules.begin();
7430 itmod != modules.end(); ++itmod)
7431 clog << *itmod << endl;
7432 }
40a70f52
JL
7433
7434 if (funcs.empty())
7435 return "";
7436
7437 return levenshtein_suggest(func, funcs, 5); // print top 5 funcs only
7438}
51d6bda3 7439
0508c45f
JL
7440string
7441suggest_dwarf_functions(systemtap_session& sess,
7442 const set<string>& modules,
7443 string func)
44ffe90c
JL
7444{
7445 // Trim any @ component
7446 size_t pos = func.find('@');
7447 if (pos != string::npos)
7448 func.erase(pos);
7449
0508c45f 7450 if (func.empty() || modules.empty() || sess.module_cache == NULL)
44ffe90c
JL
7451 return "";
7452
7453 // We must first aggregate all the functions from the cache
7454 set<string> funcs;
7455 const map<string, module_info*> &cache = sess.module_cache->cache;
7456
0508c45f
JL
7457 for (set<string>::iterator itmod = modules.begin();
7458 itmod != modules.end(); ++itmod)
44ffe90c 7459 {
6e6d9a3b
JL
7460 module_info *module;
7461
7462 // retrieve module_info from cache
44ffe90c
JL
7463 map<string, module_info*>::const_iterator itcache;
7464 if ((itcache = cache.find(*itmod)) != cache.end())
6e6d9a3b
JL
7465 module = itcache->second;
7466 else // module not found
7467 continue;
7468
7469 // add inlines
7470 funcs.insert(module->inlined_funcs.begin(),
7471 module->inlined_funcs.end());
7472
7473 // add all function symbols in cache
7474 if (module->symtab_status != info_present || module->sym_table == NULL)
7475 continue;
7476 map<string, func_info*>& modfuncs = module->sym_table->map_by_name;
7477 for (map<string, func_info*>::const_iterator itfuncs = modfuncs.begin();
7478 itfuncs != modfuncs.end(); ++itfuncs)
7479 funcs.insert(itfuncs->first);
44ffe90c
JL
7480 }
7481
7482 if (sess.verbose > 2)
d82dd71f
JL
7483 {
7484 clog << "suggesting " << funcs.size() << " dwarf functions "
7485 << "from modules:" << endl;
7486 for (set<string>::iterator itmod = modules.begin();
7487 itmod != modules.end(); ++itmod)
7488 clog << *itmod << endl;
7489 }
44ffe90c
JL
7490
7491 if (funcs.empty())
7492 return "";
7493
593f09eb 7494 return levenshtein_suggest(func, funcs, 5); // print top 5 funcs only
44ffe90c
JL
7495}
7496
20c6c071 7497void
5227f1ea 7498dwarf_builder::build(systemtap_session & sess,
7a053d3b 7499 probe * base,
20c6c071 7500 probe_point * location,
86bf665e 7501 literal_map_t const & parameters,
20c6c071
GH
7502 vector<derived_probe *> & finished_results)
7503{
b20febf3
FCE
7504 // NB: the kernel/user dwlfpp objects are long-lived.
7505 // XXX: but they should be per-session, as this builder object
7506 // may be reused if we try to cross-instrument multiple targets.
84048984 7507
7a24d422 7508 dwflpp* dw = 0;
6d5d594e 7509 literal_map_t filled_parameters = parameters;
7a24d422 7510
7a24d422 7511 string module_name;
5c6f9e92 7512 int64_t proc_pid;
ae2552da
FCE
7513 if (has_null_param (parameters, TOK_KERNEL))
7514 {
7515 dw = get_kern_dw(sess, "kernel");
7516 }
7517 else if (get_param (parameters, TOK_MODULE, module_name))
b8da0ad1 7518 {
8e30940d
JL
7519 // If not a full path was given, then it's an in-tree module. Replace any
7520 // dashes with underscores.
7521 if (!is_fully_resolved(module_name, sess.sysroot, sess.sysenv))
7522 {
7523 size_t dash_pos = 0;
7524 while((dash_pos=module_name.find('-'))!=string::npos)
7525 module_name.replace(int(dash_pos),1,"_");
7526 filled_parameters[TOK_MODULE] = new literal_string(module_name);
7527 }
7528
37001baa
FCE
7529 // NB: glob patterns get expanded later, during the offline
7530 // elfutils module listing.
ae2552da 7531 dw = get_kern_dw(sess, module_name);
b8da0ad1 7532 }
5c6f9e92 7533 else if (has_param(filled_parameters, TOK_PROCESS))
84c84ac4 7534 {
0ce4c32f
FCE
7535 // NB: module_name is not yet set!
7536
6d5d594e
LB
7537 if(has_null_param(filled_parameters, TOK_PROCESS))
7538 {
fc18e6c4
JL
7539 string file;
7540 try
7541 {
7542 file = sess.cmd_file();
7543 }
7544 catch (const semantic_error& e)
7545 {
6d9c4e46 7546 if(sess.target_pid)
1b6d5734 7547 throw SEMANTIC_ERROR(_("invalid -x pid for unspecified process"
6d9c4e46
AJ
7548 " probe [man stapprobes]"), NULL, NULL, &e);
7549 else
7550 throw SEMANTIC_ERROR(_("invalid -c command for unspecified process"
fc18e6c4
JL
7551 " probe [man stapprobes]"), NULL, NULL, &e);
7552 }
48cd804a 7553 if(file.empty())
fc18e6c4 7554 throw SEMANTIC_ERROR(_("unspecified process probe is invalid without"
6d9c4e46 7555 " a -c COMMAND or -x PID [man stapprobes]"));
48cd804a 7556 module_name = sess.sysroot + file;
6d5d594e 7557 filled_parameters[TOK_PROCESS] = new literal_string(module_name);// this needs to be used in place of the blank map
0ce4c32f 7558 // in the case of TOK_MARK we need to modify locations as well // XXX why?
6d5d594e 7559 if(location->components[0]->functor==TOK_PROCESS &&
0ce4c32f 7560 location->components[0]->arg == 0)
30b865ce
AJ
7561 {
7562 if (sess.target_pid)
7563 location->components[0]->arg = new literal_number(sess.target_pid);
7564 else
7565 location->components[0]->arg = new literal_string(module_name);
7566 }
5c6f9e92
AJ
7567 }
7568
0ce4c32f
FCE
7569 // NB: must specifically handle the classical ("string") form here, to make sure
7570 // we get the module_name out.
7571 else if (get_param (parameters, TOK_PROCESS, module_name))
7572 {
7573 module_name = sess.sysroot + module_name;
7574 filled_parameters[TOK_PROCESS] = new literal_string(module_name);
7575 }
7576
5c6f9e92
AJ
7577 else if (get_param (parameters, TOK_PROCESS, proc_pid))
7578 {
6c484277 7579 // check that the pid given corresponds to a running process
30b865ce 7580 string pid_err_msg;
fb9ae721 7581 if (!is_valid_pid(proc_pid, pid_err_msg))
30b865ce 7582 throw SEMANTIC_ERROR(pid_err_msg);
6c484277 7583
5c6f9e92
AJ
7584 string pid_path = string("/proc/") + lex_cast(proc_pid) + "/exe";
7585 module_name = sess.sysroot + pid_path;
37635b45 7586
0ce4c32f 7587 // in the case of TOK_MARK we need to modify locations as well // XXX why?
6d5d594e 7588 if(location->components[0]->functor==TOK_PROCESS &&
0ce4c32f 7589 location->components[0]->arg == 0)
30b865ce 7590 location->components[0]->arg = new literal_number(sess.target_pid);
b78a0fbb 7591 }
5750ecc6 7592
37001baa
FCE
7593 // PR6456 process("/bin/*") glob handling
7594 if (contains_glob_chars (module_name))
7595 {
7596 // Expand glob via rewriting the probe-point process("....")
7597 // parameter, asserted to be the first one.
7598
7599 assert (location->components.size() > 0);
7600 assert (location->components[0]->functor == TOK_PROCESS);
7601 assert (location->components[0]->arg);
7602 literal_string* lit = dynamic_cast<literal_string*>(location->components[0]->arg);
7603 assert (lit);
7604
7605 // Evaluate glob here, and call derive_probes recursively with each match.
7606 glob_t the_blob;
88f8e2c0 7607 set<string> dupes;
37001baa 7608 int rc = glob (module_name.c_str(), 0, NULL, & the_blob);
b530b5b3 7609 if (rc)
dc09353a 7610 throw SEMANTIC_ERROR (_F("glob %s error (%s)", module_name.c_str(), lex_cast(rc).c_str() ));
44ffe90c 7611 unsigned results_pre = finished_results.size();
37001baa
FCE
7612 for (unsigned i = 0; i < the_blob.gl_pathc; ++i)
7613 {
e19ebcf7 7614 assert_no_interrupts();
37001baa
FCE
7615
7616 const char* globbed = the_blob.gl_pathv[i];
7617 struct stat st;
7618
7619 if (access (globbed, X_OK) == 0
7620 && stat (globbed, &st) == 0
7621 && S_ISREG (st.st_mode)) // see find_executable()
7622 {
7977a734
FCE
7623 // Need to call canonicalize here, in order to path-expand
7624 // patterns like process("stap*"). Otherwise it may go through
7625 // to the next round of expansion as ("stap"), leading to a $PATH
7626 // search that's not consistent with the glob search already done.
5bca76a8
JS
7627 string canononicalized = resolve_path (globbed);
7628 globbed = canononicalized.c_str();
7977a734 7629
88f8e2c0
JS
7630 // The canonical names can result in duplication, for example
7631 // having followed symlinks that are common with shared
7632 // libraries. Filter those out.
7633 if (!dupes.insert(canononicalized).second)
7634 continue;
7635
37001baa
FCE
7636 // synthesize a new probe_point, with the glob-expanded string
7637 probe_point *pp = new probe_point (*location);
ef0943df
JL
7638 pp->from_glob = true;
7639
5750ecc6
FCE
7640 // PR13338: quote results to prevent recursion
7641 string eglobbed = escape_glob_chars (globbed);
7642
7643 if (sess.verbose > 1)
7644 clog << _F("Expanded process(\"%s\") to process(\"%s\")",
7645 module_name.c_str(), eglobbed.c_str()) << endl;
05fb3e0c 7646 string eglobbed_tgt = path_remove_sysroot(sess, eglobbed);
5750ecc6 7647
37001baa 7648 probe_point::component* ppc = new probe_point::component (TOK_PROCESS,
05fb3e0c 7649 new literal_string (eglobbed_tgt));
37001baa
FCE
7650 ppc->tok = location->components[0]->tok; // overwrite [0] slot, pattern matched above
7651 pp->components[0] = ppc;
7652
d885563b 7653 probe* new_probe = new probe (base, pp);
7977a734
FCE
7654
7655 // We override "optional = true" here, as if the
7656 // wildcarded probe point was given a "?" suffix.
7657
7658 // This is because wildcard probes will be expected
7659 // by users to apply only to some subset of the
7660 // matching binaries, in the sense of "any", rather
7661 // than "all", sort of similarly how
7662 // module("*").function("...") patterns work.
7663
7664 derive_probes (sess, new_probe, finished_results,
7665 true /* NB: not location->optional */ );
37001baa
FCE
7666 }
7667 }
7668
7669 globfree (& the_blob);
44ffe90c
JL
7670
7671 unsigned results_post = finished_results.size();
7672
e90006d7 7673 // Did we fail to find a function/plt/mark by name? Let's suggest
44ffe90c
JL
7674 // something!
7675 string func;
7676 if (results_pre == results_post
7677 && get_param(filled_parameters, TOK_FUNCTION, func)
7678 && !func.empty())
7679 {
0508c45f 7680 string sugs = suggest_dwarf_functions(sess, modules_seen, func);
44ffe90c
JL
7681 modules_seen.clear();
7682 if (!sugs.empty())
dc09353a 7683 throw SEMANTIC_ERROR (_NF("no match (similar function: %s)",
44ffe90c
JL
7684 "no match (similar functions: %s)",
7685 sugs.find(',') == string::npos,
7686 sugs.c_str()));
7687 }
40a70f52
JL
7688 else if (results_pre == results_post
7689 && get_param(filled_parameters, TOK_PLT, func)
7690 && !func.empty())
7691 {
7692 string sugs = suggest_plt_functions(sess, modules_seen, func);
7693 modules_seen.clear();
7694 if (!sugs.empty())
7695 throw SEMANTIC_ERROR (_NF("no match (similar function: %s)",
7696 "no match (similar functions: %s)",
7697 sugs.find(',') == string::npos,
7698 sugs.c_str()));
7699 }
e90006d7
JL
7700 else if (results_pre == results_post
7701 && get_param(filled_parameters, TOK_MARK, func)
7702 && !func.empty())
7703 {
7704 string provider;
7705 get_param(filled_parameters, TOK_PROVIDER, provider);
7706
7707 string sugs = suggest_marks(sess, modules_seen, func, provider);
7708 modules_seen.clear();
7709 if (!sugs.empty())
7710 throw SEMANTIC_ERROR (_NF("no match (similar mark: %s)",
7711 "no match (similar marks: %s)",
7712 sugs.find(',') == string::npos,
7713 sugs.c_str()));
7714 }
44ffe90c 7715
37001baa
FCE
7716 return; // avoid falling through
7717 }
7718
5750ecc6
FCE
7719 // PR13338: unquote glob results
7720 module_name = unescape_glob_chars (module_name);
05fb3e0c 7721 user_path = find_executable (module_name, "", sess.sysenv); // canonicalize it
5fa0d811
JL
7722 if (!is_fully_resolved(user_path, "", sess.sysenv))
7723 throw SEMANTIC_ERROR(_F("cannot find executable '%s'",
7724 user_path.c_str()));
d1bcbe71
RH
7725
7726 // if the executable starts with "#!", we look for the interpreter of the script
7727 {
7728 ifstream script_file (user_path.c_str () );
7729
7730 if (script_file.good ())
7731 {
7732 string line;
7733
7734 getline (script_file, line);
7735
7736 if (line.compare (0, 2, "#!") == 0)
7737 {
7738 string path_head = line.substr(2);
7739
7740 // remove white spaces at the beginning of the string
7741 size_t p2 = path_head.find_first_not_of(" \t");
7742
7743 if (p2 != string::npos)
7744 {
7745 string path = path_head.substr(p2);
7746
7747 // remove white spaces at the end of the string
7748 p2 = path.find_last_not_of(" \t\n");
7749 if (string::npos != p2)
7750 path.erase(p2+1);
7751
8e13c1a1
RH
7752 // handle "#!/usr/bin/env" redirect
7753 size_t offset = 0;
7754 if (path.compare(0, sizeof("/bin/env")-1, "/bin/env") == 0)
7755 {
7756 offset = sizeof("/bin/env")-1;
7757 }
7758 else if (path.compare(0, sizeof("/usr/bin/env")-1, "/usr/bin/env") == 0)
7759 {
7760 offset = sizeof("/usr/bin/env")-1;
7761 }
7762
7763 if (offset != 0)
7764 {
7765 size_t p3 = path.find_first_not_of(" \t", offset);
7766
7767 if (p3 != string::npos)
7768 {
7769 string env_path = path.substr(p3);
05fb3e0c
WF
7770 user_path = find_executable (env_path, sess.sysroot,
7771 sess.sysenv);
8e13c1a1
RH
7772 }
7773 }
7774 else
7775 {
05fb3e0c 7776 user_path = find_executable (path, sess.sysroot, sess.sysenv);
8e13c1a1 7777 }
d1bcbe71
RH
7778
7779 struct stat st;
7780
7781 if (access (user_path.c_str(), X_OK) == 0
7782 && stat (user_path.c_str(), &st) == 0
7783 && S_ISREG (st.st_mode)) // see find_executable()
7784 {
7785 if (sess.verbose > 1)
b530b5b3
LB
7786 clog << _F("Expanded process(\"%s\") to process(\"%s\")",
7787 module_name.c_str(), user_path.c_str()) << endl;
d1bcbe71
RH
7788
7789 assert (location->components.size() > 0);
7790 assert (location->components[0]->functor == TOK_PROCESS);
7791 assert (location->components[0]->arg);
7792 literal_string* lit = dynamic_cast<literal_string*>(location->components[0]->arg);
7793 assert (lit);
7794
7795 // synthesize a new probe_point, with the expanded string
7796 probe_point *pp = new probe_point (*location);
05fb3e0c 7797 string user_path_tgt = path_remove_sysroot(sess, user_path);
d1bcbe71 7798 probe_point::component* ppc = new probe_point::component (TOK_PROCESS,
05fb3e0c 7799 new literal_string (user_path_tgt.c_str()));
d1bcbe71
RH
7800 ppc->tok = location->components[0]->tok; // overwrite [0] slot, pattern matched above
7801 pp->components[0] = ppc;
7802
d885563b 7803 probe* new_probe = new probe (base, pp);
d1bcbe71
RH
7804
7805 derive_probes (sess, new_probe, finished_results);
7806
7807 script_file.close();
7808 return;
7809 }
7810 }
7811 }
7812 }
7813 script_file.close();
7814 }
7815
dbe9d133
JL
7816 // If this is a library probe, then target the library module instead. We
7817 // do this only if the library path is already fully resolved (such as
7818 // what query_one_library() would have done for us). Otherwise, we resort
7819 // to iterate_over_libraries.
7820 if (get_param (parameters, TOK_LIBRARY, user_lib) && !user_lib.empty()
7821 && is_fully_resolved(user_lib, sess.sysroot, sess.sysenv, "LD_LIBRARY_PATH"))
7822 module_name = user_lib;
63b4fd14 7823 else
dbe9d133 7824 module_name = user_path; // canonicalize it
d0a7f5a9 7825
3667d615 7826 // uretprobes aren't available everywhere
536f1261 7827 if (has_null_param(parameters, TOK_RETURN) && !sess.runtime_usermode_p())
2b69faaf 7828 {
3667d615
JS
7829 if (kernel_supports_inode_uprobes(sess) &&
7830 !kernel_supports_inode_uretprobes(sess))
dc09353a 7831 throw SEMANTIC_ERROR
3ffeaf3c 7832 (_("process return probes not available [man error::inode-uprobes]"));
2b69faaf 7833 }
3667d615 7834
f4000852
MW
7835 // There is a similar check in pass 4 (buildrun), but it is
7836 // needed here too to make sure alternatives for optional
7837 // (? or !) process probes are disposed and/or alternatives
7838 // are selected.
3667d615
JS
7839 if (!sess.runtime_usermode_p())
7840 check_process_probe_kernel_support(sess);
e34d5d13 7841
7a24d422
FCE
7842 // user-space target; we use one dwflpp instance per module name
7843 // (= program or shared library)
707bf35e 7844 dw = get_user_dw(sess, module_name);
c8959a29 7845 }
20c6c071 7846
1acfc030
JS
7847 assert(dw);
7848
e90006d7
JL
7849 unsigned results_pre = finished_results.size();
7850
5896cd05 7851 if (sess.verbose > 3)
b530b5b3 7852 clog << _F("dwarf_builder::build for %s", module_name.c_str()) << endl;
5896cd05 7853
a794dbeb
FCE
7854 string dummy_mark_name; // NB: PR10245: dummy value, need not substitute - => __
7855 if (get_param(parameters, TOK_MARK, dummy_mark_name))
f28a8c28 7856 {
51d6bda3 7857 sdt_query sdtq(base, location, *dw, filled_parameters, finished_results, user_lib);
06de3a04 7858 dw->iterate_over_modules<base_query>(&query_module, &sdtq);
e90006d7
JL
7859
7860 // We need to update modules_seen with the modules we've visited
7861 modules_seen.insert(sdtq.visited_modules.begin(),
7862 sdtq.visited_modules.end());
7863
614f0fcf
JL
7864 string lib;
7865 if (results_pre == finished_results.size() && !sdtq.resolved_library
7866 && get_param(filled_parameters, TOK_LIBRARY, lib)
7867 && !lib.empty() && !sdtq.visited_libraries.empty())
7868 {
7869 // The library didn't fit any DT_NEEDED libraries. As a last effort,
7870 // let's try to look for the library directly.
7871 string resolved_lib = find_executable(lib, sess.sysroot, sess.sysenv,
7872 "LD_LIBRARY_PATH");
7873 if (resolved_lib.find('/') != string::npos)
7874 {
7875 probe *new_base = build_library_probe(*dw, resolved_lib,
7876 base, location);
7877 derive_probes(sess, new_base, finished_results);
7878 sess.print_warning(_F("'%s' is not a needed library of '%s'. "
7879 "Specify the full path to squelch this warning.",
7880 resolved_lib.c_str(), dw->module_name.c_str()));
7881 return;
7882 }
f23e4abe
JL
7883
7884 // Otherwise, let's suggest from the DT_NEEDED libraries
7885 string sugs = levenshtein_suggest(lib, sdtq.visited_libraries, 5);
7886 if (!sugs.empty())
7887 throw SEMANTIC_ERROR (_NF("no match (similar library: %s)",
7888 "no match (similar libraries: %s)",
7889 sugs.find(',') == string::npos,
7890 sugs.c_str()));
614f0fcf
JL
7891 }
7892
e90006d7
JL
7893 // Did we fail to find a mark?
7894 if (results_pre == finished_results.size() && !location->from_glob)
7895 {
7896 string provider;
7897 get_param(filled_parameters, TOK_PROVIDER, provider);
7898
7899 string sugs = suggest_marks(sess, modules_seen, dummy_mark_name, provider);
7900 modules_seen.clear();
7901 if (!sugs.empty())
7902 throw SEMANTIC_ERROR (_NF("no match (similar mark: %s)",
7903 "no match (similar marks: %s)",
7904 sugs.find(',') == string::npos,
7905 sugs.c_str()));
7906 }
7907
edce5b67 7908 return;
7a05f484 7909 }
20c6c071 7910
6d5d594e 7911 dwarf_query q(base, location, *dw, filled_parameters, finished_results, user_path, user_lib);
7a24d422
FCE
7912
7913 // XXX: kernel.statement.absolute is a special case that requires no
7914 // dwfl processing. This code should be in a separate builder.
7a24d422 7915 if (q.has_kernel && q.has_absolute)
37ebca01 7916 {
4baf0e53 7917 // assert guru mode for absolute probes
37ebca01
FCE
7918 if (! q.base_probe->privileged)
7919 {
dc09353a 7920 throw SEMANTIC_ERROR (_("absolute statement probe in unprivileged script; need stap -g"),
edce5b67 7921 q.base_probe->tok);
37ebca01
FCE
7922 }
7923
7924 // For kernel.statement(NUM).absolute probe points, we bypass
7925 // all the debuginfo stuff: We just wire up a
7926 // dwarf_derived_probe right here and now.
4baf0e53 7927 dwarf_derived_probe* p =
b8da0ad1
FCE
7928 new dwarf_derived_probe ("", "", 0, "kernel", "",
7929 q.statement_num_val, q.statement_num_val,
7930 q, 0);
37ebca01 7931 finished_results.push_back (p);
1a0dbc5a 7932 sess.unwindsym_modules.insert ("kernel");
37ebca01
FCE
7933 return;
7934 }
7935
06de3a04 7936 dw->iterate_over_modules<base_query>(&query_module, &q);
8f14e444 7937
44ffe90c
JL
7938 // We need to update modules_seen with the modules we've visited
7939 modules_seen.insert(q.visited_modules.begin(),
7940 q.visited_modules.end());
8f14e444
FCE
7941
7942 // PR11553 special processing: .return probes requested, but
7943 // some inlined function instances matched.
7944 unsigned i_n_r = q.inlined_non_returnable.size();
7945 unsigned results_post = finished_results.size();
7946 if (i_n_r > 0)
7947 {
7948 if ((results_pre == results_post) && (! sess.suppress_warnings)) // no matches; issue warning
7949 {
7950 string quicklist;
7951 for (set<string>::iterator it = q.inlined_non_returnable.begin();
7952 it != q.inlined_non_returnable.end();
7953 it++)
7954 {
7955 quicklist += " " + (*it);
7956 if (quicklist.size() > 80) // heuristic, don't make an overlong report line
7957 {
7958 quicklist += " ...";
7959 break;
7960 }
7961 }
c57ea854 7962
52c2652f 7963 sess.print_warning (_NF("cannot probe .return of %u inlined function %s",
b530b5b3 7964 "cannot probe .return of %u inlined functions %s",
52c2652f 7965 quicklist.size(), i_n_r, quicklist.c_str()));
8f14e444
FCE
7966 // There will be also a "no matches" semantic error generated.
7967 }
7968 if (sess.verbose > 1)
52c2652f
NMA
7969 clog << _NF("skipped .return probe of %u inlined function",
7970 "skipped .return probe of %u inlined functions", i_n_r, i_n_r) << endl;
8f14e444
FCE
7971 if ((sess.verbose > 3) || (sess.verbose > 2 && results_pre == results_post)) // issue details with high verbosity
7972 {
7973 for (set<string>::iterator it = q.inlined_non_returnable.begin();
7974 it != q.inlined_non_returnable.end();
7975 it++)
7976 clog << (*it) << " ";
7977 clog << endl;
7978 }
7979 } // i_n_r > 0
44ffe90c 7980
614f0fcf
JL
7981 string lib;
7982 if (results_pre == results_post && !q.resolved_library
7983 && get_param(filled_parameters, TOK_LIBRARY, lib)
7984 && !lib.empty() && !q.visited_libraries.empty())
7985 {
7986 // The library didn't fit any DT_NEEDED libraries. As a last effort,
7987 // let's try to look for the library directly.
7988 string resolved_lib = find_executable(lib, sess.sysroot, sess.sysenv,
7989 "LD_LIBRARY_PATH");
7990 if (resolved_lib.find('/') != string::npos)
7991 {
7992 probe *new_base = build_library_probe(*dw, resolved_lib,
7993 base, location);
7994 derive_probes(sess, new_base, finished_results);
7995 sess.print_warning(_F("'%s' is not a needed library of '%s'. "
7996 "Specify the full path to squelch this warning.",
7997 resolved_lib.c_str(), dw->module_name.c_str()));
7998 return;
7999 }
f23e4abe
JL
8000
8001 // Otherwise, let's suggest from the DT_NEEDED libraries
8002 string sugs = levenshtein_suggest(lib, q.visited_libraries, 5);
8003 if (!sugs.empty())
8004 throw SEMANTIC_ERROR (_NF("no match (similar library: %s)",
8005 "no match (similar libraries: %s)",
8006 sugs.find(',') == string::npos,
8007 sugs.c_str()));
614f0fcf
JL
8008 }
8009
e90006d7 8010 // If we just failed to resolve a function/plt by name, we can suggest
ef0943df
JL
8011 // something. We only suggest things for probe points that were not
8012 // synthesized from a glob, i.e. only for 'real' probes. This is also
8013 // required because modules_seen needs to accumulate across recursive
8014 // calls for process(glob)[.library(glob)] probes.
44ffe90c 8015 string func;
ef0943df 8016 if (results_pre == results_post && !location->from_glob
44ffe90c
JL
8017 && get_param(filled_parameters, TOK_FUNCTION, func)
8018 && !func.empty())
8019 {
0508c45f 8020 string sugs = suggest_dwarf_functions(sess, modules_seen, func);
44ffe90c
JL
8021 modules_seen.clear();
8022 if (!sugs.empty())
dc09353a 8023 throw SEMANTIC_ERROR (_NF("no match (similar function: %s)",
44ffe90c
JL
8024 "no match (similar functions: %s)",
8025 sugs.find(',') == string::npos,
8026 sugs.c_str()));
8027 }
ef0943df 8028 else if (results_pre == results_post && !location->from_glob
40a70f52
JL
8029 && get_param(filled_parameters, TOK_PLT, func)
8030 && !func.empty())
8031 {
8032 string sugs = suggest_plt_functions(sess, modules_seen, func);
8033 modules_seen.clear();
8034 if (!sugs.empty())
8035 throw SEMANTIC_ERROR (_NF("no match (similar function: %s)",
8036 "no match (similar functions: %s)",
8037 sugs.find(',') == string::npos,
8038 sugs.c_str()));
8039 }
44ffe90c
JL
8040 else if (results_pre != results_post)
8041 // Something was derived so we won't need to suggest something
8042 modules_seen.clear();
5f0a03a6
JK
8043}
8044
8045symbol_table::~symbol_table()
8046{
c9efa5c9 8047 delete_map(map_by_addr);
5f0a03a6
JK
8048}
8049
8050void
2867a2a1 8051symbol_table::add_symbol(const char *name, bool weak, bool descriptor,
44ffe90c 8052 Dwarf_Addr addr, Dwarf_Addr* /*high_addr*/)
5f0a03a6 8053{
ab91b232
JK
8054#ifdef __powerpc__
8055 // Map ".sys_foo" to "sys_foo".
8056 if (name[0] == '.')
8057 name++;
8058#endif
5f0a03a6
JK
8059 func_info *fi = new func_info();
8060 fi->addr = addr;
8061 fi->name = name;
ab91b232 8062 fi->weak = weak;
2867a2a1 8063 fi->descriptor = descriptor;
5f0a03a6
JK
8064 map_by_name[fi->name] = fi;
8065 // TODO: Use a multimap in case there are multiple static
8066 // functions with the same name?
1c6b77e5 8067 map_by_addr.insert(make_pair(addr, fi));
5f0a03a6
JK
8068}
8069
46f7b6be 8070void
f98c6346 8071symbol_table::prepare_section_rejection(Dwfl_Module *mod __attribute__ ((unused)))
46f7b6be
JK
8072{
8073#ifdef __powerpc__
8074 /*
8075 * The .opd section contains function descriptors that can look
8076 * just like function entry points. For example, there's a function
8077 * descriptor called "do_exit" that links to the entry point ".do_exit".
8078 * Reject all symbols in .opd.
8079 */
8080 opd_section = SHN_UNDEF;
8081 Dwarf_Addr bias;
8082 Elf* elf = (dwarf_getelf (dwfl_module_getdwarf (mod, &bias))
8083 ?: dwfl_module_getelf (mod, &bias));
8084 Elf_Scn* scn = 0;
8085 size_t shstrndx;
8086
8087 if (!elf)
8088 return;
fcc30d6d 8089 if (elf_getshdrstrndx (elf, &shstrndx) != 0)
46f7b6be
JK
8090 return;
8091 while ((scn = elf_nextscn(elf, scn)) != NULL)
8092 {
8093 GElf_Shdr shdr_mem;
8094 GElf_Shdr *shdr = gelf_getshdr(scn, &shdr_mem);
8095 if (!shdr)
8096 continue;
8097 const char *name = elf_strptr(elf, shstrndx, shdr->sh_name);
8098 if (!strcmp(name, ".opd"))
8099 {
8100 opd_section = elf_ndxscn(scn);
8101 return;
8102 }
8103 }
8104#endif
8105}
8106
8107bool
8108symbol_table::reject_section(GElf_Word section)
8109{
8110 if (section == SHN_UNDEF)
8111 return true;
8112#ifdef __powerpc__
8113 if (section == opd_section)
8114 return true;
8115#endif
8116 return false;
8117}
8118
5f0a03a6
JK
8119enum info_status
8120symbol_table::get_from_elf()
8121{
8122 Dwarf_Addr high_addr = 0;
8123 Dwfl_Module *mod = mod_info->mod;
8124 int syments = dwfl_module_getsymtab(mod);
8125 assert(syments);
46f7b6be 8126 prepare_section_rejection(mod);
5f0a03a6
JK
8127 for (int i = 1; i < syments; ++i)
8128 {
8129 GElf_Sym sym;
ab91b232 8130 GElf_Word section;
cba67d8f
MW
8131 const char *name;
8132 GElf_Addr addr;
8133 bool reject;
8134
8135/* Note that dwfl_module_getsym does adjust the sym.st_value but doesn't
8136 try to resolve it to a function address. dwfl_module_getsym_info leaves
8137 the st_value in tact (no adjustment applied) and returns the fully
8138 resolved address separately. In that case we can simply reject the
8139 symbol if it is SHN_UNDEF and don't need to call reject_section which
8140 does extra checks to see whether the address fall in an architecture
8141 specific descriptor table (which will never be the case when using the
8142 new dwfl_module_getsym_info). dwfl_module_getsym will only provide us
8143 with the (adjusted) st_value of the symbol, which might point into a
8144 function descriptor table. So in that case we still have to call
8145 reject_section. */
8146#if _ELFUTILS_PREREQ (0, 158)
8147 name = dwfl_module_getsym_info (mod, i, &sym, &addr, &section,
8148 NULL, NULL);
8149 reject = section == SHN_UNDEF;
8150#else
8151 name = dwfl_module_getsym (mod, i, &sym, &section);
b23098cd 8152 addr = sym.st_value;
cba67d8f
MW
8153 reject = reject_section(section);
8154#endif
8155
2867a2a1 8156 if (name && GELF_ST_TYPE(sym.st_info) == STT_FUNC)
ab91b232 8157 add_symbol(name, (GELF_ST_BIND(sym.st_info) == STB_WEAK),
cba67d8f 8158 reject, addr, &high_addr);
2a4acb09
JL
8159 if (name && GELF_ST_TYPE(sym.st_info) == STT_OBJECT
8160 && GELF_ST_BIND(sym.st_info) == STB_GLOBAL)
8161 globals[name] = addr;
8162 if (name && GELF_ST_TYPE(sym.st_info) == STT_OBJECT
8163 && GELF_ST_BIND(sym.st_info) == STB_LOCAL)
8164 locals[name] = addr;
5f0a03a6
JK
8165 }
8166 return info_present;
8167}
8168
5f0a03a6
JK
8169func_info *
8170symbol_table::get_func_containing_address(Dwarf_Addr addr)
8171{
1c6b77e5
JS
8172 iterator_t iter = map_by_addr.upper_bound(addr);
8173 if (iter == map_by_addr.begin())
5f0a03a6 8174 return NULL;
2e67a43b 8175 else
1c6b77e5 8176 return (--iter)->second;
5f0a03a6
JK
8177}
8178
3d372d6b
SC
8179func_info *
8180symbol_table::get_first_func()
8181{
8182 iterator_t iter = map_by_addr.begin();
8183 return (iter)->second;
8184}
8185
5f0a03a6
JK
8186func_info *
8187symbol_table::lookup_symbol(const string& name)
8188{
8189 map<string, func_info*>::iterator i = map_by_name.find(name);
8190 if (i == map_by_name.end())
8191 return NULL;
8192 return i->second;
8193}
8194
8195Dwarf_Addr
8196symbol_table::lookup_symbol_address(const string& name)
8197{
8198 func_info *fi = lookup_symbol(name);
8199 if (fi)
8200 return fi->addr;
8201 return 0;
8202}
8203
ab91b232
JK
8204// This is the kernel symbol table. The kernel macro cond_syscall creates
8205// a weak symbol for each system call and maps it to sys_ni_syscall.
8206// For system calls not implemented elsewhere, this weak symbol shows up
8207// in the kernel symbol table. Following the precedent of dwarfful stap,
8208// we refuse to consider such symbols. Here we delete them from our
8209// symbol table.
8210// TODO: Consider generalizing this and/or making it part of blacklist
8211// processing.
8212void
8213symbol_table::purge_syscall_stubs()
8214{
8215 Dwarf_Addr stub_addr = lookup_symbol_address("sys_ni_syscall");
8216 if (stub_addr == 0)
8217 return;
1c6b77e5 8218 range_t purge_range = map_by_addr.equal_range(stub_addr);
2e67a43b
TM
8219 for (iterator_t iter = purge_range.first;
8220 iter != purge_range.second;
1c6b77e5 8221 )
ab91b232 8222 {
1c6b77e5 8223 func_info *fi = iter->second;
2e67a43b 8224 if (fi->weak && fi->name != "sys_ni_syscall")
ab91b232 8225 {
2e67a43b 8226 map_by_name.erase(fi->name);
1c6b77e5 8227 map_by_addr.erase(iter++);
2e67a43b 8228 delete fi;
2e67a43b 8229 }
1c6b77e5
JS
8230 else
8231 iter++;
ab91b232
JK
8232 }
8233}
8234
5f0a03a6 8235void
5f52fafe 8236module_info::get_symtab()
5f0a03a6 8237{
1c6b77e5
JS
8238 if (symtab_status != info_unknown)
8239 return;
8240
5f0a03a6
JK
8241 sym_table = new symbol_table(this);
8242 if (!elf_path.empty())
8243 {
5f0a03a6
JK
8244 symtab_status = sym_table->get_from_elf();
8245 }
8246 else
8247 {
8248 assert(name == TOK_KERNEL);
ab3ed72d
DS
8249 symtab_status = info_absent;
8250 cerr << _("Error: Cannot find vmlinux.") << endl;;
5f0a03a6
JK
8251 }
8252 if (symtab_status == info_absent)
8253 {
8254 delete sym_table;
8255 sym_table = NULL;
8256 return;
8257 }
8258
ab91b232
JK
8259 if (name == TOK_KERNEL)
8260 sym_table->purge_syscall_stubs();
5f0a03a6
JK
8261}
8262
1c6b77e5
JS
8263// update_symtab reconciles data between the elf symbol table and the dwarf
8264// function enumeration. It updates the symbol table entries with the dwarf
8265// die that describes the function, which also signals to query_module_symtab
8266// that a statement probe isn't needed. In return, it also adds aliases to the
8267// function table for names that share the same addr/die.
8268void
8269module_info::update_symtab(cu_function_cache_t *funcs)
8270{
8271 if (!sym_table)
8272 return;
8273
8274 cu_function_cache_t new_funcs;
8275
8276 for (cu_function_cache_t::iterator func = funcs->begin();
8277 func != funcs->end(); func++)
8278 {
8279 // optimization: inlines will never be in the symbol table
8280 if (dwarf_func_inline(&func->second) != 0)
6e6d9a3b
JL
8281 {
8282 inlined_funcs.insert(func->first);
8283 continue;
8284 }
1c6b77e5 8285
1ffb8bd1
JS
8286 // XXX We may want to make additional efforts to match mangled elf names
8287 // to dwarf too. MIPS_linkage_name can help, but that's sometimes
8288 // missing, so we may also need to try matching by address. See also the
8289 // notes about _Z in dwflpp::iterate_over_functions().
8290
1c6b77e5
JS
8291 func_info *fi = sym_table->lookup_symbol(func->first);
8292 if (!fi)
8293 continue;
8294
8295 // iterate over all functions at the same address
8296 symbol_table::range_t er = sym_table->map_by_addr.equal_range(fi->addr);
8297 for (symbol_table::iterator_t it = er.first; it != er.second; ++it)
8298 {
8299 // update this function with the dwarf die
8300 it->second->die = func->second;
8301
8302 // if this function is a new alias, then
8303 // save it to merge into the function cache
8304 if (it->second != fi)
b7478964 8305 new_funcs.insert(make_pair(it->second->name, it->second->die));
1c6b77e5
JS
8306 }
8307 }
8308
8309 // add all discovered aliases back into the function cache
8310 // NB: this won't replace any names that dwarf may have already found
8311 funcs->insert(new_funcs.begin(), new_funcs.end());
8312}
8313
5f0a03a6
JK
8314module_info::~module_info()
8315{
8316 if (sym_table)
8317 delete sym_table;
b55bc428
FCE
8318}
8319
935447c8 8320// ------------------------------------------------------------------------
888af770 8321// user-space probes
935447c8
DS
8322// ------------------------------------------------------------------------
8323
935447c8 8324
888af770 8325struct uprobe_derived_probe_group: public generic_dpg<uprobe_derived_probe>
935447c8 8326{
89ba3085
FCE
8327private:
8328 string make_pbm_key (uprobe_derived_probe* p) {
cfcab6c7 8329 return p->path + "|" + p->module + "|" + p->section + "|" + lex_cast(p->pid);
89ba3085
FCE
8330 }
8331
cfcab6c7
JS
8332 void emit_module_maxuprobes (systemtap_session& s);
8333
2b69faaf
JS
8334 // Using our own utrace-based uprobes
8335 void emit_module_utrace_decls (systemtap_session& s);
8336 void emit_module_utrace_init (systemtap_session& s);
8337 void emit_module_utrace_exit (systemtap_session& s);
8338
8339 // Using the upstream inode-based uprobes
8340 void emit_module_inode_decls (systemtap_session& s);
8341 void emit_module_inode_init (systemtap_session& s);
3642eb17 8342 void emit_module_inode_refresh (systemtap_session& s);
2b69faaf
JS
8343 void emit_module_inode_exit (systemtap_session& s);
8344
3a894f7e
JS
8345 // Using the dyninst backend (via stapdyn)
8346 void emit_module_dyninst_decls (systemtap_session& s);
8347 void emit_module_dyninst_init (systemtap_session& s);
8348 void emit_module_dyninst_exit (systemtap_session& s);
8349
935447c8 8350public:
888af770 8351 void emit_module_decls (systemtap_session& s);
935447c8 8352 void emit_module_init (systemtap_session& s);
3642eb17 8353 void emit_module_refresh (systemtap_session& s);
935447c8 8354 void emit_module_exit (systemtap_session& s);
ca6d3b0f
JL
8355
8356 // on-the-fly only supported for inode-uprobes
8357 bool otf_supported (systemtap_session& s)
8358 { return !s.runtime_usermode_p()
8359 && kernel_supports_inode_uprobes(s); }
8360
8361 // workqueue manipulation is safe in uprobes
8362 bool otf_safe_context (systemtap_session& s)
8363 { return otf_supported(s); }
935447c8
DS
8364};
8365
8366
888af770
FCE
8367void
8368uprobe_derived_probe::join_group (systemtap_session& s)
8369{
8370 if (! s.uprobe_derived_probes)
8371 s.uprobe_derived_probes = new uprobe_derived_probe_group ();
8372 s.uprobe_derived_probes->enroll (this);
ca6d3b0f
JL
8373 this->group = s.uprobe_derived_probes;
8374
f31a77f5
DS
8375 if (s.runtime_usermode_p())
8376 enable_dynprobes(s);
8377 else
4441e344 8378 enable_task_finder(s);
a96d1db0 8379
8a03658e 8380 // Ask buildrun.cxx to build extra module if needed, and
d3e959b0
DS
8381 // signal staprun to load that module. If we're using the builtin
8382 // inode-uprobes, we still need to know that it is required.
8a03658e 8383 s.need_uprobes = true;
a96d1db0
DN
8384}
8385
888af770 8386
c0f84e7b
SC
8387void
8388uprobe_derived_probe::getargs(std::list<std::string> &arg_set) const
8389{
8390 dwarf_derived_probe::getargs(arg_set);
8391 arg_set.insert(arg_set.end(), args.begin(), args.end());
8392}
8393
8394
8395void
8396uprobe_derived_probe::saveargs(int nargs)
8397{
8398 for (int i = 1; i <= nargs; i++)
8399 args.push_back("$arg" + lex_cast (i) + ":long");
8400}
8401
8402
2865d17a 8403void
42e38653 8404uprobe_derived_probe::emit_privilege_assertion (translator_output* o)
2865d17a
DB
8405{
8406 // These probes are allowed for unprivileged users, but only in the
8407 // context of processes which they own.
8408 emit_process_owner_assertion (o);
8409}
8410
8411
888af770 8412struct uprobe_builder: public derived_probe_builder
a96d1db0 8413{
888af770 8414 uprobe_builder() {}
2b69faaf 8415 virtual void build(systemtap_session & sess,
a96d1db0
DN
8416 probe * base,
8417 probe_point * location,
86bf665e 8418 literal_map_t const & parameters,
a96d1db0
DN
8419 vector<derived_probe *> & finished_results)
8420 {
888af770 8421 int64_t process, address;
a96d1db0 8422
2b69faaf 8423 if (kernel_supports_inode_uprobes(sess))
dc09353a 8424 throw SEMANTIC_ERROR (_("absolute process probes not available [man error::inode-uprobes]"));
2b69faaf 8425
888af770 8426 bool b1 = get_param (parameters, TOK_PROCESS, process);
ced347a9 8427 (void) b1;
888af770 8428 bool b2 = get_param (parameters, TOK_STATEMENT, address);
ced347a9 8429 (void) b2;
888af770
FCE
8430 bool rr = has_null_param (parameters, TOK_RETURN);
8431 assert (b1 && b2); // by pattern_root construction
a96d1db0 8432
0973d815 8433 finished_results.push_back(new uprobe_derived_probe(base, location, process, address, rr));
a96d1db0
DN
8434 }
8435};
8436
8437
8438void
cfcab6c7 8439uprobe_derived_probe_group::emit_module_maxuprobes (systemtap_session& s)
a96d1db0 8440{
43241c44
FCE
8441 // We'll probably need at least this many:
8442 unsigned minuprobes = probes.size();
8443 // .. but we don't want so many that .bss is inflated (PR10507):
8444 unsigned uprobesize = 64;
8445 unsigned maxuprobesmem = 10*1024*1024; // 10 MB
8446 unsigned maxuprobes = maxuprobesmem / uprobesize;
8447
aaf7ffe8
FCE
8448 // Let's choose a value on the geometric middle. This should end up
8449 // between minuprobes and maxuprobes. It's OK if this number turns
8450 // out to be < minuprobes or > maxuprobes. At worst, we get a
8451 // run-time error of one kind (too few: missed uprobe registrations)
8452 // or another (too many: vmalloc errors at module load time).
8453 unsigned default_maxuprobes = (unsigned)sqrt((double)minuprobes * (double)maxuprobes);
43241c44 8454
6d0f3f0c 8455 s.op->newline() << "#ifndef MAXUPROBES";
43241c44 8456 s.op->newline() << "#define MAXUPROBES " << default_maxuprobes;
6d0f3f0c 8457 s.op->newline() << "#endif";
cfcab6c7
JS
8458}
8459
8460
8461void
8462uprobe_derived_probe_group::emit_module_utrace_decls (systemtap_session& s)
8463{
8464 if (probes.empty()) return;
8465 s.op->newline() << "/* ---- utrace uprobes ---- */";
8466 // If uprobes isn't in the kernel, pull it in from the runtime.
8467
8468 s.op->newline() << "#if defined(CONFIG_UPROBES) || defined(CONFIG_UPROBES_MODULE)";
8469 s.op->newline() << "#include <linux/uprobes.h>";
8470 s.op->newline() << "#else";
2ba1736a 8471 s.op->newline() << "#include \"linux/uprobes/uprobes.h\"";
cfcab6c7
JS
8472 s.op->newline() << "#endif";
8473 s.op->newline() << "#ifndef UPROBES_API_VERSION";
8474 s.op->newline() << "#define UPROBES_API_VERSION 1";
8475 s.op->newline() << "#endif";
8476
8477 emit_module_maxuprobes (s);
a96d1db0 8478
cc52276b 8479 // Forward decls
2ba1736a 8480 s.op->newline() << "#include \"linux/uprobes-common.h\"";
cc52276b 8481
5e112f92
FCE
8482 // In .bss, the shared pool of uprobe/uretprobe structs. These are
8483 // too big to embed in the initialized .data stap_uprobe_spec array.
cc52276b
WC
8484 // XXX: consider a slab cache or somesuch for stap_uprobes
8485 s.op->newline() << "static struct stap_uprobe stap_uprobes [MAXUPROBES];";
6b378b7c 8486 s.op->newline() << "static DEFINE_MUTEX(stap_uprobes_lock);"; // protects against concurrent registration/unregistration
a96d1db0 8487
89ba3085
FCE
8488 s.op->assert_0_indent();
8489
89ba3085
FCE
8490 // Assign task-finder numbers as we build up the stap_uprobe_tf table.
8491 // This means we process probes[] in two passes.
8492 map <string,unsigned> module_index;
8493 unsigned module_index_ctr = 0;
8494
cc52276b
WC
8495 // not const since embedded task_finder_target struct changes
8496 s.op->newline() << "static struct stap_uprobe_tf stap_uprobe_finders[] = {";
89ba3085
FCE
8497 s.op->indent(1);
8498 for (unsigned i=0; i<probes.size(); i++)
8499 {
8500 uprobe_derived_probe *p = probes[i];
8501 string pbmkey = make_pbm_key (p);
8502 if (module_index.find (pbmkey) == module_index.end())
8503 {
8504 module_index[pbmkey] = module_index_ctr++;
8505
8506 s.op->newline() << "{";
8507 // NB: it's essential that make_pbm_key() use all of and
8508 // only the same fields as we're about to emit.
8509 s.op->line() << " .finder={";
1af100fc 8510 s.op->line() << " .purpose=\"uprobes\",";
89ba3085 8511 if (p->pid != 0)
68910c97
JK
8512 s.op->line() << " .pid=" << p->pid << ",";
8513
8514 if (p->section == "") // .statement(addr).absolute
8515 s.op->line() << " .callback=&stap_uprobe_process_found,";
89ba3085
FCE
8516 else if (p->section == ".absolute") // proxy for ET_EXEC -> exec()'d program
8517 {
8518 s.op->line() << " .procname=" << lex_cast_qstring(p->module) << ",";
8519 s.op->line() << " .callback=&stap_uprobe_process_found,";
8520 }
68910c97 8521 else if (p->section != ".absolute") // ET_DYN
89ba3085 8522 {
4ad95bbc
SC
8523 if (p->has_library)
8524 s.op->line() << " .procname=\"" << p->path << "\", ";
89ba3085
FCE
8525 s.op->line() << " .mmap_callback=&stap_uprobe_mmap_found, ";
8526 s.op->line() << " .munmap_callback=&stap_uprobe_munmap_found, ";
19d91f6c 8527 s.op->line() << " .callback=&stap_uprobe_process_munmap,";
89ba3085 8528 }
89ba3085 8529 s.op->line() << " },";
68910c97
JK
8530 if (p->module != "")
8531 s.op->line() << " .pathname=" << lex_cast_qstring(p->module) << ", ";
89ba3085
FCE
8532 s.op->line() << " },";
8533 }
c57ea854 8534 else
822a6a3d 8535 { } // skip it in this pass, already have a suitable stap_uprobe_tf slot for it.
89ba3085
FCE
8536 }
8537 s.op->newline(-1) << "};";
8538
8539 s.op->assert_0_indent();
8540
3689db05
SC
8541 unsigned pci;
8542 for (pci=0; pci<probes.size(); pci++)
8543 {
8544 // List of perf counters used by each probe
8545 // This list is an index into struct stap_perf_probe,
8546 uprobe_derived_probe *p = probes[pci];
aa2c662f 8547 std::set<string>::iterator pcii;
3689db05
SC
8548 s.op->newline() << "long perf_counters_" + lex_cast(pci) + "[] = {";
8549 for (pcii = p->perf_counter_refs.begin();
8550 pcii != p->perf_counter_refs.end(); pcii++)
8551 {
aa2c662f 8552 std::vector<std::pair<std::string,std::string> >::iterator it;
3689db05
SC
8553 unsigned i = 0;
8554 // Find the associated perf.counter probe
8555 for (it=s.perf_counters.begin() ;
8556 it != s.perf_counters.end(); it++, i++)
aa2c662f 8557 if ((*it).first == (*pcii))
3689db05
SC
8558 break;
8559 s.op->line() << lex_cast(i) << ", ";
8560 }
8561 s.op->newline() << "};";
8562 }
8563
cc52276b
WC
8564 // NB: read-only structure
8565 s.op->newline() << "static const struct stap_uprobe_spec stap_uprobe_specs [] = {";
a96d1db0 8566 s.op->indent(1);
888af770
FCE
8567 for (unsigned i =0; i<probes.size(); i++)
8568 {
8569 uprobe_derived_probe* p = probes[i];
8570 s.op->newline() << "{";
89ba3085
FCE
8571 string key = make_pbm_key (p);
8572 unsigned value = module_index[key];
759e1d76
FCE
8573 if (value != 0)
8574 s.op->line() << " .tfi=" << value << ",";
6b66b9f7 8575 s.op->line() << " .address=(unsigned long)0x" << hex << p->addr << dec << "ULL,";
faea5e16 8576 s.op->line() << " .probe=" << common_probe_init (p) << ",";
4ddb6dd0 8577
038c38c6 8578 if (p->sdt_semaphore_addr != 0)
63b4fd14 8579 s.op->line() << " .sdt_sem_offset=(unsigned long)0x"
038c38c6 8580 << hex << p->sdt_semaphore_addr << dec << "ULL,";
4ddb6dd0 8581
0d049a1d 8582 // XXX: don't bother emit if array is empty
3689db05
SC
8583 s.op->line() << " .perf_counters_dim=ARRAY_SIZE(perf_counters_" << lex_cast(i) << "),";
8584 // List of perf counters used by a probe from above
0d049a1d 8585 s.op->line() << " .perf_counters=perf_counters_" + lex_cast(i) + ",";
3689db05 8586
4ddb6dd0
JS
8587 if (p->has_return)
8588 s.op->line() << " .return_p=1,";
888af770
FCE
8589 s.op->line() << " },";
8590 }
8591 s.op->newline(-1) << "};";
a96d1db0 8592
89ba3085
FCE
8593 s.op->assert_0_indent();
8594
48e685da 8595 s.op->newline() << "static void enter_uprobe_probe (struct uprobe *inst, struct pt_regs *regs) {";
888af770 8596 s.op->newline(1) << "struct stap_uprobe *sup = container_of(inst, struct stap_uprobe, up);";
89ba3085 8597 s.op->newline() << "const struct stap_uprobe_spec *sups = &stap_uprobe_specs [sup->spec_index];";
71db462b 8598 common_probe_entryfn_prologue (s, "STAP_SESSION_RUNNING", "sups->probe",
cda141c2 8599 "stp_probe_type_uprobe");
0e090c74 8600 s.op->newline() << "if (sup->spec_index < 0 || "
6e895029
DS
8601 << "sup->spec_index >= " << probes.size() << ") {";
8602 s.op->newline(1) << "_stp_error (\"bad spec_index %d (max " << probes.size()
0e090c74 8603 << "): %s\", sup->spec_index, c->probe_point);";
6e895029
DS
8604 s.op->newline() << "goto probe_epilogue;";
8605 s.op->newline(-1) << "}";
d9aed31e 8606 s.op->newline() << "c->uregs = regs;";
e04b5d74 8607 s.op->newline() << "c->user_mode_p = 1;";
6415ddde
MW
8608
8609 // Make it look like the IP is set as it would in the actual user
8610 // task when calling real probe handler. Reset IP regs on return, so
8611 // we don't confuse uprobes. PR10458
8612 s.op->newline() << "{";
8613 s.op->indent(1);
d9aed31e 8614 s.op->newline() << "unsigned long uprobes_ip = REG_IP(c->uregs);";
259d54c0 8615 s.op->newline() << "SET_REG_IP(regs, inst->vaddr);";
26e63673 8616 s.op->newline() << "(*sups->probe->ph) (c);";
259d54c0 8617 s.op->newline() << "SET_REG_IP(regs, uprobes_ip);";
6415ddde
MW
8618 s.op->newline(-1) << "}";
8619
ef1337ee 8620 common_probe_entryfn_epilogue (s, true, otf_safe_context(s));
888af770 8621 s.op->newline(-1) << "}";
a96d1db0 8622
48e685da 8623 s.op->newline() << "static void enter_uretprobe_probe (struct uretprobe_instance *inst, struct pt_regs *regs) {";
888af770 8624 s.op->newline(1) << "struct stap_uprobe *sup = container_of(inst->rp, struct stap_uprobe, urp);";
89ba3085 8625 s.op->newline() << "const struct stap_uprobe_spec *sups = &stap_uprobe_specs [sup->spec_index];";
71db462b 8626 common_probe_entryfn_prologue (s, "STAP_SESSION_RUNNING", "sups->probe",
cda141c2 8627 "stp_probe_type_uretprobe");
6dceb5c9 8628 s.op->newline() << "c->ips.ri = inst;";
0e090c74 8629 s.op->newline() << "if (sup->spec_index < 0 || "
0d5561a5
DS
8630 << "sup->spec_index >= " << probes.size() << ") {";
8631 s.op->newline(1) << "_stp_error (\"bad spec_index %d (max " << probes.size()
0e090c74 8632 << "): %s\", sup->spec_index, c->probe_point);";
0d5561a5
DS
8633 s.op->newline() << "goto probe_epilogue;";
8634 s.op->newline(-1) << "}";
8635
d9aed31e 8636 s.op->newline() << "c->uregs = regs;";
e04b5d74 8637 s.op->newline() << "c->user_mode_p = 1;";
6415ddde
MW
8638
8639 // Make it look like the IP is set as it would in the actual user
8640 // task when calling real probe handler. Reset IP regs on return, so
8641 // we don't confuse uprobes. PR10458
8642 s.op->newline() << "{";
8643 s.op->indent(1);
d9aed31e 8644 s.op->newline() << "unsigned long uprobes_ip = REG_IP(c->uregs);";
5e562a69 8645 s.op->newline() << "SET_REG_IP(regs, inst->ret_addr);";
26e63673 8646 s.op->newline() << "(*sups->probe->ph) (c);";
259d54c0 8647 s.op->newline() << "SET_REG_IP(regs, uprobes_ip);";
6415ddde
MW
8648 s.op->newline(-1) << "}";
8649
ef1337ee 8650 common_probe_entryfn_epilogue (s, true, otf_safe_context(s));
a96d1db0
DN
8651 s.op->newline(-1) << "}";
8652
89ba3085 8653 s.op->newline();
2ba1736a 8654 s.op->newline() << "#include \"linux/uprobes-common.c\"";
6d0f3f0c 8655 s.op->newline();
888af770 8656}
935447c8
DS
8657
8658
888af770 8659void
2b69faaf 8660uprobe_derived_probe_group::emit_module_utrace_init (systemtap_session& s)
935447c8 8661{
888af770 8662 if (probes.empty()) return;
935447c8 8663
2b69faaf 8664 s.op->newline() << "/* ---- utrace uprobes ---- */";
935447c8 8665
01b05e2e 8666 s.op->newline() << "for (j=0; j<MAXUPROBES; j++) {";
5e112f92
FCE
8667 s.op->newline(1) << "struct stap_uprobe *sup = & stap_uprobes[j];";
8668 s.op->newline() << "sup->spec_index = -1;"; // free slot
80b4ad8b
FCE
8669 // NB: we assume the rest of the struct (specificaly, sup->up) is
8670 // initialized to zero. This is so that we can use
8671 // sup->up->kdata = NULL for "really free!" PR 6829.
5e112f92
FCE
8672 s.op->newline(-1) << "}";
8673 s.op->newline() << "mutex_init (& stap_uprobes_lock);";
935447c8 8674
89ba3085
FCE
8675 // Set up the task_finders
8676 s.op->newline() << "for (i=0; i<sizeof(stap_uprobe_finders)/sizeof(stap_uprobe_finders[0]); i++) {";
8677 s.op->newline(1) << "struct stap_uprobe_tf *stf = & stap_uprobe_finders[i];";
c57ea854 8678 s.op->newline() << "probe_point = stf->pathname;"; // for error messages; XXX: would prefer pp() or something better
89ba3085 8679 s.op->newline() << "rc = stap_register_task_finder_target (& stf->finder);";
935447c8 8680
5e112f92
FCE
8681 // NB: if (rc), there is no need (XXX: nor any way) to clean up any
8682 // finders already registered, since mere registration does not
8683 // cause any utrace or memory allocation actions. That happens only
8684 // later, once the task finder engine starts running. So, for a
8685 // partial initialization requiring unwind, we need do nothing.
8686 s.op->newline() << "if (rc) break;";
a7a68293 8687
888af770
FCE
8688 s.op->newline(-1) << "}";
8689}
d0ea46ce 8690
d0a7f5a9 8691
888af770 8692void
2b69faaf 8693uprobe_derived_probe_group::emit_module_utrace_exit (systemtap_session& s)
888af770
FCE
8694{
8695 if (probes.empty()) return;
2b69faaf 8696 s.op->newline() << "/* ---- utrace uprobes ---- */";
e56e51c9 8697
6d0f3f0c
FCE
8698 // NB: there is no stap_unregister_task_finder_target call;
8699 // important stuff like utrace cleanups are done by
d41d451c
FCE
8700 // __stp_task_finder_cleanup() via stap_stop_task_finder().
8701 //
8702 // This function blocks until all callbacks are completed, so there
8703 // is supposed to be no possibility of any registration-related code starting
8704 // to run in parallel with our shutdown here. So we don't need to protect the
8705 // stap_uprobes[] array with the mutex.
d0a7f5a9 8706
01b05e2e 8707 s.op->newline() << "for (j=0; j<MAXUPROBES; j++) {";
5e112f92 8708 s.op->newline(1) << "struct stap_uprobe *sup = & stap_uprobes[j];";
89ba3085 8709 s.op->newline() << "const struct stap_uprobe_spec *sups = &stap_uprobe_specs [sup->spec_index];";
6d0f3f0c 8710 s.op->newline() << "if (sup->spec_index < 0) continue;"; // free slot
3568f1dd 8711
8faa1fc5 8712 // PR10655: decrement that ENABLED semaphore
c116c31b 8713 s.op->newline() << "if (sup->sdt_sem_address) {";
8faa1fc5
FCE
8714 s.op->newline(1) << "unsigned short sdt_semaphore;"; // NB: fixed size
8715 s.op->newline() << "pid_t pid = (sups->return_p ? sup->urp.u.pid : sup->up.pid);";
8716 s.op->newline() << "struct task_struct *tsk;";
8717 s.op->newline() << "rcu_read_lock();";
6846cfc8 8718
86229a55
DS
8719 // Do a pid->task_struct* lookup. For 2.6.24+, this code assumes
8720 // that the pid is always in the global namespace, not in any
8721 // private namespace.
8faa1fc5 8722 s.op->newline() << "#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,24)";
86229a55
DS
8723 // We'd like to call find_task_by_pid_ns() here, but it isn't
8724 // exported. So, we call what it calls...
8725 s.op->newline() << " tsk = pid_task(find_pid_ns(pid, &init_pid_ns), PIDTYPE_PID);";
8faa1fc5
FCE
8726 s.op->newline() << "#else";
8727 s.op->newline() << " tsk = find_task_by_pid (pid);";
8728 s.op->newline() << "#endif /* 2.6.24 */";
8faa1fc5
FCE
8729
8730 s.op->newline() << "if (tsk) {"; // just in case the thing exited while we weren't watching
3c5b8e2b 8731 s.op->newline(1) << "if (__access_process_vm_noflush(tsk, sup->sdt_sem_address, &sdt_semaphore, sizeof(sdt_semaphore), 0)) {";
63b4fd14 8732 s.op->newline(1) << "sdt_semaphore --;";
903b9fcd 8733 s.op->newline() << "#ifdef DEBUG_UPROBES";
c116c31b 8734 s.op->newline() << "_stp_dbug (__FUNCTION__,__LINE__, \"-semaphore %#x @ %#lx\\n\", sdt_semaphore, sup->sdt_sem_address);";
903b9fcd 8735 s.op->newline() << "#endif";
3c5b8e2b 8736 s.op->newline() << "__access_process_vm_noflush(tsk, sup->sdt_sem_address, &sdt_semaphore, sizeof(sdt_semaphore), 1);";
93c84191 8737 s.op->newline(-1) << "}";
8faa1fc5
FCE
8738 // XXX: need to analyze possibility of race condition
8739 s.op->newline(-1) << "}";
8740 s.op->newline() << "rcu_read_unlock();";
8741 s.op->newline(-1) << "}";
6846cfc8 8742
3568f1dd
FCE
8743 s.op->newline() << "if (sups->return_p) {";
8744 s.op->newline(1) << "#ifdef DEBUG_UPROBES";
89ba3085 8745 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 8746 s.op->newline() << "#endif";
80b4ad8b
FCE
8747 // NB: PR6829 does not change that we still need to unregister at
8748 // *this* time -- when the script as a whole exits.
3568f1dd
FCE
8749 s.op->newline() << "unregister_uretprobe (& sup->urp);";
8750 s.op->newline(-1) << "} else {";
8751 s.op->newline(1) << "#ifdef DEBUG_UPROBES";
8faa1fc5 8752 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
8753 s.op->newline() << "#endif";
8754 s.op->newline() << "unregister_uprobe (& sup->up);";
8755 s.op->newline(-1) << "}";
935447c8 8756
6d0f3f0c 8757 s.op->newline() << "sup->spec_index = -1;";
935447c8 8758
3568f1dd
FCE
8759 // XXX: uprobe missed counts?
8760
6d0f3f0c 8761 s.op->newline(-1) << "}";
935447c8 8762
5e112f92 8763 s.op->newline() << "mutex_destroy (& stap_uprobes_lock);";
935447c8
DS
8764}
8765
2b69faaf
JS
8766
8767void
8768uprobe_derived_probe_group::emit_module_inode_decls (systemtap_session& s)
8769{
8770 if (probes.empty()) return;
8771 s.op->newline() << "/* ---- inode uprobes ---- */";
cfcab6c7 8772 emit_module_maxuprobes (s);
2ba1736a 8773 s.op->newline() << "#include \"linux/uprobes-inode.c\"";
2b69faaf
JS
8774
8775 // Write the probe handler.
79af55c3
JS
8776 s.op->newline() << "static int stapiu_probe_handler "
8777 << "(struct stapiu_consumer *sup, struct pt_regs *regs) {";
8778 s.op->newline(1);
2dbbd473
JS
8779
8780 // Since we're sharing the entry function, we have to dynamically choose the probe_type
8781 string probe_type = "(sup->return_p ? stp_probe_type_uretprobe : stp_probe_type_uprobe)";
71db462b 8782 common_probe_entryfn_prologue (s, "STAP_SESSION_RUNNING", "sup->probe",
2dbbd473
JS
8783 probe_type);
8784
3bff6634 8785 s.op->newline() << "c->uregs = regs;";
e04b5d74 8786 s.op->newline() << "c->user_mode_p = 1;";
497cfcb2 8787 // NB: IP is already set by stapiu_probe_prehandler in uprobes-inode.c
2b69faaf 8788 s.op->newline() << "(*sup->probe->ph) (c);";
39e63bae 8789
ef1337ee 8790 common_probe_entryfn_epilogue (s, true, otf_safe_context(s));
2b69faaf
JS
8791 s.op->newline() << "return 0;";
8792 s.op->newline(-1) << "}";
8793 s.op->assert_0_indent();
8794
8795 // Index of all the modules for which we need inodes.
8796 map<string, unsigned> module_index;
8797 unsigned module_index_ctr = 0;
8798
8799 // Discover and declare targets for each unique path.
cfcab6c7 8800 s.op->newline() << "static struct stapiu_target "
2b69faaf
JS
8801 << "stap_inode_uprobe_targets[] = {";
8802 s.op->indent(1);
8803 for (unsigned i=0; i<probes.size(); i++)
8804 {
8805 uprobe_derived_probe *p = probes[i];
cfcab6c7
JS
8806 const string key = make_pbm_key(p);
8807 if (module_index.find (key) == module_index.end())
2b69faaf 8808 {
cfcab6c7
JS
8809 module_index[key] = module_index_ctr++;
8810 s.op->newline() << "{";
8811 s.op->line() << " .finder={";
b78a0fbb 8812 s.op->line() << " .purpose=\"inode-uprobes\",";
cfcab6c7
JS
8813 if (p->pid != 0)
8814 s.op->line() << " .pid=" << p->pid << ",";
8815
8816 if (p->section == "") // .statement(addr).absolute XXX?
8817 s.op->line() << " .callback=&stapiu_process_found,";
8818 else if (p->section == ".absolute") // proxy for ET_EXEC -> exec()'d program
8819 {
8820 s.op->line() << " .procname=" << lex_cast_qstring(p->module) << ",";
8821 s.op->line() << " .callback=&stapiu_process_found,";
8822 }
8823 else if (p->section != ".absolute") // ET_DYN
8824 {
8825 if (p->has_library)
8826 s.op->line() << " .procname=\"" << p->path << "\", ";
8827 s.op->line() << " .mmap_callback=&stapiu_mmap_found, ";
8828 s.op->line() << " .munmap_callback=&stapiu_munmap_found, ";
8829 s.op->line() << " .callback=&stapiu_process_munmap,";
8830 }
8831 s.op->line() << " },";
8832 s.op->line() << " .filename=" << lex_cast_qstring(p->module) << ",";
8833 s.op->line() << " },";
2b69faaf
JS
8834 }
8835 }
8836 s.op->newline(-1) << "};";
8837 s.op->assert_0_indent();
8838
8839 // Declare the actual probes.
3689db05
SC
8840 unsigned pci;
8841 for (pci=0; pci<probes.size(); pci++)
8842 {
8843 // List of perf counters used by each probe
8844 // This list is an index into struct stap_perf_probe,
8845 uprobe_derived_probe *p = probes[pci];
aa2c662f 8846 std::set<string>::iterator pcii;
3689db05
SC
8847 s.op->newline() << "long perf_counters_" + lex_cast(pci) + "[] = {";
8848 for (pcii = p->perf_counter_refs.begin();
8849 pcii != p->perf_counter_refs.end(); pcii++)
8850 {
aa2c662f 8851 vector<std::pair<string,string> >:: iterator it;
3689db05
SC
8852 unsigned i = 0;
8853 // Find the associated perf.counter probe
4fa83377
SC
8854 for (it=s.perf_counters.begin() ;
8855 it != s.perf_counters.end(); it++, i++)
aa2c662f 8856 if ((*it).first == (*pcii))
3689db05
SC
8857 break;
8858 s.op->line() << lex_cast(i) << ", ";
8859 }
8860 s.op->newline() << "};";
8861 }
8862
cfcab6c7 8863 s.op->newline() << "static struct stapiu_consumer "
2b69faaf
JS
8864 << "stap_inode_uprobe_consumers[] = {";
8865 s.op->indent(1);
8866 for (unsigned i=0; i<probes.size(); i++)
8867 {
8868 uprobe_derived_probe *p = probes[i];
cfcab6c7
JS
8869 unsigned index = module_index[make_pbm_key(p)];
8870 s.op->newline() << "{";
79af55c3
JS
8871 if (p->has_return)
8872 s.op->line() << " .return_p=1,";
cfcab6c7
JS
8873 s.op->line() << " .target=&stap_inode_uprobe_targets[" << index << "],";
8874 s.op->line() << " .offset=(loff_t)0x" << hex << p->addr << dec << "ULL,";
8875 if (p->sdt_semaphore_addr)
8876 s.op->line() << " .sdt_sem_offset=(loff_t)0x"
8877 << hex << p->sdt_semaphore_addr << dec << "ULL,";
0d049a1d 8878 // XXX: don't bother emit if array is empty
3689db05
SC
8879 s.op->line() << " .perf_counters_dim=ARRAY_SIZE(perf_counters_" << lex_cast(i) << "),";
8880 // List of perf counters used by a probe from above
0d049a1d 8881 s.op->line() << " .perf_counters=perf_counters_" + lex_cast(i) + ",";
cfcab6c7
JS
8882 s.op->line() << " .probe=" << common_probe_init (p) << ",";
8883 s.op->line() << " },";
2b69faaf
JS
8884 }
8885 s.op->newline(-1) << "};";
8886 s.op->assert_0_indent();
8887}
8888
8889
8890void
8891uprobe_derived_probe_group::emit_module_inode_init (systemtap_session& s)
8892{
8893 if (probes.empty()) return;
8894 s.op->newline() << "/* ---- inode uprobes ---- */";
b0f614a0
DS
8895 // Let stapiu_init() handle reporting errors by setting probe_point
8896 // to NULL.
8897 s.op->newline() << "probe_point = NULL;";
cfcab6c7 8898 s.op->newline() << "rc = stapiu_init ("
2b69faaf
JS
8899 << "stap_inode_uprobe_targets, "
8900 << "ARRAY_SIZE(stap_inode_uprobe_targets), "
8901 << "stap_inode_uprobe_consumers, "
8902 << "ARRAY_SIZE(stap_inode_uprobe_consumers));";
8903}
8904
8905
3642eb17
JL
8906void
8907uprobe_derived_probe_group::emit_module_inode_refresh (systemtap_session& s)
8908{
8909 if (probes.empty()) return;
8910 s.op->newline() << "/* ---- inode uprobes ---- */";
3642eb17
JL
8911 s.op->newline() << "stapiu_refresh ("
8912 << "stap_inode_uprobe_targets, "
8913 << "ARRAY_SIZE(stap_inode_uprobe_targets));";
3642eb17
JL
8914}
8915
8916
2b69faaf
JS
8917void
8918uprobe_derived_probe_group::emit_module_inode_exit (systemtap_session& s)
8919{
8920 if (probes.empty()) return;
8921 s.op->newline() << "/* ---- inode uprobes ---- */";
cfcab6c7 8922 s.op->newline() << "stapiu_exit ("
2b69faaf
JS
8923 << "stap_inode_uprobe_targets, "
8924 << "ARRAY_SIZE(stap_inode_uprobe_targets), "
8925 << "stap_inode_uprobe_consumers, "
8926 << "ARRAY_SIZE(stap_inode_uprobe_consumers));";
8927}
8928
8929
3a894f7e
JS
8930void
8931uprobe_derived_probe_group::emit_module_dyninst_decls (systemtap_session& s)
8932{
8933 if (probes.empty()) return;
8934 s.op->newline() << "/* ---- dyninst uprobes ---- */";
8935 emit_module_maxuprobes (s);
e00f3fb7 8936 s.op->newline() << "#include \"dyninst/uprobes.h\"";
3a894f7e 8937
f31a77f5
DS
8938 // Let the dynprobe_derived_probe_group handle outputting targets
8939 // and probes. This allows us to merge different types of probes.
8940 s.op->newline() << "static struct stapdu_probe stapdu_probes[];";
8941 for (unsigned i = 0; i < probes.size(); i++)
3a894f7e
JS
8942 {
8943 uprobe_derived_probe *p = probes[i];
e00f3fb7 8944
f31a77f5
DS
8945 dynprobe_add_uprobe(s, p->module, p->addr, p->sdt_semaphore_addr,
8946 (p->has_return ? "STAPDYN_PROBE_FLAG_RETURN" : "0"),
8947 common_probe_init(p));
3a894f7e 8948 }
874d38bf
JS
8949 // loc2c-generated code assumes pt_regs are available, so use this to make
8950 // sure we always have *something* for it to dereference...
f31a77f5 8951 s.op->newline() << "static struct pt_regs stapdu_dummy_uregs;";
e00f3fb7 8952
3a894f7e
JS
8953 // Write the probe handler.
8954 // NB: not static, so dyninst can find it
8955 s.op->newline() << "int enter_dyninst_uprobe "
8956 << "(uint64_t index, struct pt_regs *regs) {";
e00f3fb7 8957 s.op->newline(1) << "struct stapdu_probe *sup = &stapdu_probes[index];";
92b97104
JS
8958
8959 // Since we're sharing the entry function, we have to dynamically choose the probe_type
8960 string probe_type = "((sup->flags & STAPDYN_PROBE_FLAG_RETURN) ?"
8961 " stp_probe_type_uretprobe : stp_probe_type_uprobe)";
71db462b 8962 common_probe_entryfn_prologue (s, "STAP_SESSION_RUNNING", "sup->probe",
92b97104
JS
8963 probe_type);
8964
874d38bf 8965 s.op->newline() << "c->uregs = regs ?: &stapdu_dummy_uregs;";
e04b5d74 8966 s.op->newline() << "c->user_mode_p = 1;";
3a894f7e
JS
8967 // XXX: once we have regs, check how dyninst sets the IP
8968 // XXX: the way that dyninst rewrites stuff is probably going to be
8969 // ... very confusing to our backtracer (at least if we stay in process)
8970 s.op->newline() << "(*sup->probe->ph) (c);";
ef1337ee 8971 common_probe_entryfn_epilogue (s, true, otf_safe_context(s));
3a894f7e
JS
8972 s.op->newline() << "return 0;";
8973 s.op->newline(-1) << "}";
3debb935 8974 s.op->newline() << "#include \"dyninst/uprobes-regs.c\"";
3a894f7e
JS
8975 s.op->assert_0_indent();
8976}
8977
8978
8979void
8980uprobe_derived_probe_group::emit_module_dyninst_init (systemtap_session& s)
8981{
8982 if (probes.empty()) return;
8983
8984 /* stapdyn handles the dirty work via dyninst */
8985 s.op->newline() << "/* ---- dyninst uprobes ---- */";
8986 s.op->newline() << "/* this section left intentionally blank */";
8987}
8988
8989
8990void
8991uprobe_derived_probe_group::emit_module_dyninst_exit (systemtap_session& s)
8992{
8993 if (probes.empty()) return;
8994
8995 /* stapdyn handles the dirty work via dyninst */
8996 s.op->newline() << "/* ---- dyninst uprobes ---- */";
8997 s.op->newline() << "/* this section left intentionally blank */";
8998}
8999
9000
2b69faaf
JS
9001void
9002uprobe_derived_probe_group::emit_module_decls (systemtap_session& s)
9003{
ac3af990 9004 if (s.runtime_usermode_p())
4441e344
JS
9005 emit_module_dyninst_decls (s);
9006 else if (kernel_supports_inode_uprobes (s))
2b69faaf
JS
9007 emit_module_inode_decls (s);
9008 else
9009 emit_module_utrace_decls (s);
9010}
9011
9012
9013void
9014uprobe_derived_probe_group::emit_module_init (systemtap_session& s)
9015{
ac3af990 9016 if (s.runtime_usermode_p())
4441e344
JS
9017 emit_module_dyninst_init (s);
9018 else if (kernel_supports_inode_uprobes (s))
2b69faaf
JS
9019 emit_module_inode_init (s);
9020 else
9021 emit_module_utrace_init (s);
9022}
9023
9024
3642eb17
JL
9025void
9026uprobe_derived_probe_group::emit_module_refresh (systemtap_session& s)
9027{
9028 if (!s.runtime_usermode_p() && kernel_supports_inode_uprobes (s))
9029 emit_module_inode_refresh (s);
9030}
9031
9032
2b69faaf
JS
9033void
9034uprobe_derived_probe_group::emit_module_exit (systemtap_session& s)
9035{
ac3af990 9036 if (s.runtime_usermode_p())
4441e344
JS
9037 emit_module_dyninst_exit (s);
9038 else if (kernel_supports_inode_uprobes (s))
2b69faaf
JS
9039 emit_module_inode_exit (s);
9040 else
9041 emit_module_utrace_exit (s);
9042}
9043
9044
e6fe60e7
AM
9045// ------------------------------------------------------------------------
9046// Kprobe derived probes
9047// ------------------------------------------------------------------------
9048
4627ed58 9049static const string TOK_KPROBE("kprobe");
935447c8 9050
bae55db9 9051struct kprobe_derived_probe: public derived_probe
d0ea46ce 9052{
23dc94f6
DS
9053 kprobe_derived_probe (systemtap_session& sess,
9054 vector<derived_probe *> & results,
9055 probe *base,
bae55db9
JS
9056 probe_point *location,
9057 const string& name,
9058 int64_t stmt_addr,
e6afb07b 9059 bool has_call,
bae55db9
JS
9060 bool has_return,
9061 bool has_statement,
9062 bool has_maxactive,
b642c901
SC
9063 bool has_path,
9064 bool has_library,
9065 long maxactive_val,
9066 const string& path,
9067 const string& library
bae55db9
JS
9068 );
9069 string symbol_name;
9070 Dwarf_Addr addr;
e6afb07b 9071 bool has_call;
bae55db9
JS
9072 bool has_return;
9073 bool has_statement;
9074 bool has_maxactive;
b642c901
SC
9075 bool has_path;
9076 bool has_library;
bae55db9 9077 long maxactive_val;
b642c901
SC
9078 string path;
9079 string library;
bae55db9
JS
9080 bool access_var;
9081 void printsig (std::ostream &o) const;
9082 void join_group (systemtap_session& s);
9083};
d0ea46ce 9084
bae55db9
JS
9085struct kprobe_derived_probe_group: public derived_probe_group
9086{
9087private:
9088 multimap<string,kprobe_derived_probe*> probes_by_module;
9089 typedef multimap<string,kprobe_derived_probe*>::iterator p_b_m_iterator;
d0ea46ce 9090
bae55db9
JS
9091public:
9092 void enroll (kprobe_derived_probe* probe);
9093 void emit_module_decls (systemtap_session& s);
9094 void emit_module_init (systemtap_session& s);
9095 void emit_module_exit (systemtap_session& s);
9096};
d0ea46ce 9097
23dc94f6
DS
9098struct kprobe_var_expanding_visitor: public var_expanding_visitor
9099{
9100 systemtap_session& sess;
9101 block *add_block;
9102 block *add_call_probe; // synthesized from .return probes with saved $vars
9103 bool add_block_tid, add_call_probe_tid;
bd5b25e1 9104 bool has_return;
23dc94f6 9105
bd5b25e1 9106 kprobe_var_expanding_visitor(systemtap_session& sess, bool has_return):
23dc94f6 9107 sess(sess), add_block(NULL), add_call_probe(NULL),
bd5b25e1
JS
9108 add_block_tid(false), add_call_probe_tid(false),
9109 has_return(has_return) {}
23dc94f6
DS
9110
9111 void visit_entry_op (entry_op* e);
9112};
9113
9114
9115kprobe_derived_probe::kprobe_derived_probe (systemtap_session& sess,
9116 vector<derived_probe *> & results,
9117 probe *base,
e6fe60e7 9118 probe_point *location,
b6371390 9119 const string& name,
e6fe60e7 9120 int64_t stmt_addr,
e6afb07b 9121 bool has_call,
b6371390
JS
9122 bool has_return,
9123 bool has_statement,
9124 bool has_maxactive,
b642c901
SC
9125 bool has_path,
9126 bool has_library,
9127 long maxactive_val,
9128 const string& path,
9129 const string& library
b6371390 9130 ):
4c5d1300 9131 derived_probe (base, location, true /* .components soon rewritten */ ),
e6afb07b 9132 symbol_name (name), addr (stmt_addr), has_call (has_call),
b6371390 9133 has_return (has_return), has_statement (has_statement),
b642c901
SC
9134 has_maxactive (has_maxactive), has_path (has_path),
9135 has_library (has_library),
9136 maxactive_val (maxactive_val),
9137 path (path), library (library)
e6fe60e7
AM
9138{
9139 this->tok = base->tok;
9140 this->access_var = false;
d0ea46ce 9141
e6fe60e7
AM
9142#ifndef USHRT_MAX
9143#define USHRT_MAX 32767
9144#endif
d0ea46ce 9145
46856d8d
JS
9146 // Expansion of $target variables in the probe body produces an error during
9147 // translate phase, since we're not using debuginfo
d0ea46ce 9148
e6fe60e7 9149 vector<probe_point::component*> comps;
46856d8d 9150 comps.push_back (new probe_point::component(TOK_KPROBE));
e6fe60e7 9151
46856d8d
JS
9152 if (has_statement)
9153 {
9ea68eb9
JS
9154 comps.push_back (new probe_point::component(TOK_STATEMENT,
9155 new literal_number(addr, true)));
46856d8d
JS
9156 comps.push_back (new probe_point::component(TOK_ABSOLUTE));
9157 }
9158 else
9159 {
9160 size_t pos = name.find(':');
9161 if (pos != string::npos)
d0ea46ce 9162 {
46856d8d
JS
9163 string module = name.substr(0, pos);
9164 string function = name.substr(pos + 1);
9165 comps.push_back (new probe_point::component(TOK_MODULE, new literal_string(module)));
9166 comps.push_back (new probe_point::component(TOK_FUNCTION, new literal_string(function)));
9167 }
9168 else
9169 comps.push_back (new probe_point::component(TOK_FUNCTION, new literal_string(name)));
46856d8d 9170 }
d0ea46ce 9171
e6afb07b
JL
9172 if (has_call)
9173 comps.push_back (new probe_point::component(TOK_CALL));
b6371390
JS
9174 if (has_return)
9175 comps.push_back (new probe_point::component(TOK_RETURN));
9176 if (has_maxactive)
9177 comps.push_back (new probe_point::component(TOK_MAXACTIVE, new literal_number(maxactive_val)));
d0ea46ce 9178
bd5b25e1 9179 kprobe_var_expanding_visitor v (sess, has_return);
23dc94f6
DS
9180 v.replace (this->body);
9181
9182 // If during target-variable-expanding the probe, we added a new block
9183 // of code, add it to the start of the probe.
9184 if (v.add_block)
9185 this->body = new block(v.add_block, this->body);
9186
9187 // If when target-variable-expanding the probe, we need to
9188 // synthesize a sibling function-entry probe. We don't go through
9189 // the whole probe derivation business (PR10642) that could lead to
9190 // wildcard/alias resolution, or for that dwarf-induced duplication.
9191 if (v.add_call_probe)
9192 {
9193 assert (has_return);
9194
9195 // We temporarily replace base.
9196 statement* old_body = base->body;
9197 base->body = v.add_call_probe;
9198
9199 derived_probe *entry_handler
9200 = new kprobe_derived_probe (sess, results, base, location, name, 0,
e6afb07b
JL
9201 true /* has_call */, false /* has_return */,
9202 has_statement, has_maxactive, has_path,
9203 has_library, maxactive_val, path, library);
23dc94f6
DS
9204 results.push_back (entry_handler);
9205
9206 base->body = old_body;
9207 }
9208
e6fe60e7
AM
9209 this->sole_location()->components = comps;
9210}
d0ea46ce 9211
e6fe60e7
AM
9212void kprobe_derived_probe::printsig (ostream& o) const
9213{
9214 sole_location()->print (o);
9215 o << " /* " << " name = " << symbol_name << "*/";
9216 printsig_nested (o);
9217}
d0ea46ce 9218
e6fe60e7
AM
9219void kprobe_derived_probe::join_group (systemtap_session& s)
9220{
e6fe60e7
AM
9221 if (! s.kprobe_derived_probes)
9222 s.kprobe_derived_probes = new kprobe_derived_probe_group ();
9223 s.kprobe_derived_probes->enroll (this);
ca6d3b0f 9224 this->group = s.kprobe_derived_probes;
e6fe60e7 9225}
d0ea46ce 9226
e6fe60e7
AM
9227void kprobe_derived_probe_group::enroll (kprobe_derived_probe* p)
9228{
9229 probes_by_module.insert (make_pair (p->symbol_name, p));
9230 // probes of same symbol should share single kprobe/kretprobe
9231}
d0ea46ce 9232
e6fe60e7
AM
9233void
9234kprobe_derived_probe_group::emit_module_decls (systemtap_session& s)
9235{
9236 if (probes_by_module.empty()) return;
d0ea46ce 9237
e6fe60e7 9238 s.op->newline() << "/* ---- kprobe-based probes ---- */";
d0ea46ce 9239
e6fe60e7
AM
9240 // Warn of misconfigured kernels
9241 s.op->newline() << "#if ! defined(CONFIG_KPROBES)";
9242 s.op->newline() << "#error \"Need CONFIG_KPROBES!\"";
9243 s.op->newline() << "#endif";
9244 s.op->newline();
d0ea46ce 9245
f07c3b68 9246 s.op->newline() << "#ifndef KRETACTIVE";
1ee6b5fc 9247 s.op->newline() << "#define KRETACTIVE (max(15,6*(int)num_possible_cpus()))";
f07c3b68
FCE
9248 s.op->newline() << "#endif";
9249
e6fe60e7 9250 // Forward declare the master entry functions
88747011 9251 s.op->newline() << "static int enter_kprobe2_probe (struct kprobe *inst,";
e6fe60e7 9252 s.op->line() << " struct pt_regs *regs);";
88747011 9253 s.op->newline() << "static int enter_kretprobe2_probe (struct kretprobe_instance *inst,";
e6fe60e7 9254 s.op->line() << " struct pt_regs *regs);";
d0ea46ce 9255
e6fe60e7
AM
9256 // Emit an array of kprobe/kretprobe pointers
9257 s.op->newline() << "#if defined(STAPCONF_UNREGISTER_KPROBES)";
c9116e99 9258 s.op->newline() << "static void * stap_unreg_kprobes2[" << probes_by_module.size() << "];";
e6fe60e7 9259 s.op->newline() << "#endif";
d0ea46ce 9260
e6fe60e7 9261 // Emit the actual probe list.
d0ea46ce 9262
e6fe60e7
AM
9263 s.op->newline() << "static struct stap_dwarfless_kprobe {";
9264 s.op->newline(1) << "union { struct kprobe kp; struct kretprobe krp; } u;";
9265 s.op->newline() << "#ifdef __ia64__";
9266 s.op->newline() << "struct kprobe dummy;";
9267 s.op->newline() << "#endif";
9268 s.op->newline(-1) << "} stap_dwarfless_kprobes[" << probes_by_module.size() << "];";
9269 // NB: bss!
d0ea46ce 9270
e6fe60e7
AM
9271 s.op->newline() << "static struct stap_dwarfless_probe {";
9272 s.op->newline(1) << "const unsigned return_p:1;";
9273 s.op->newline() << "const unsigned maxactive_p:1;";
b350f56b 9274 s.op->newline() << "const unsigned optional_p:1;";
e6fe60e7
AM
9275 s.op->newline() << "unsigned registered_p:1;";
9276 s.op->newline() << "const unsigned short maxactive_val;";
935447c8 9277
e6fe60e7
AM
9278 // Function Names are mostly small and uniform enough to justify putting
9279 // char[MAX]'s into the array instead of relocated char*'s.
935447c8 9280
faea5e16
JS
9281 size_t symbol_string_name_max = 0;
9282 size_t symbol_string_name_tot = 0;
e6fe60e7 9283 for (p_b_m_iterator it = probes_by_module.begin(); it != probes_by_module.end(); it++)
6270adc1 9284 {
e6fe60e7
AM
9285 kprobe_derived_probe* p = it->second;
9286#define DOIT(var,expr) do { \
9287 size_t var##_size = (expr) + 1; \
9288 var##_max = max (var##_max, var##_size); \
9289 var##_tot += var##_size; } while (0)
e6fe60e7
AM
9290 DOIT(symbol_string_name, p->symbol_name.size());
9291#undef DOIT
6270adc1
MH
9292 }
9293
e6fe60e7
AM
9294#define CALCIT(var) \
9295 s.op->newline() << "const char " << #var << "[" << var##_name_max << "] ;";
935447c8 9296
e6fe60e7
AM
9297 CALCIT(symbol_string);
9298#undef CALCIT
6270adc1 9299
bd659351 9300 s.op->newline() << "unsigned long address;";
7c3e97f4 9301 s.op->newline() << "const struct stap_probe * const probe;";
e6fe60e7
AM
9302 s.op->newline(-1) << "} stap_dwarfless_probes[] = {";
9303 s.op->indent(1);
6270adc1 9304
e6fe60e7
AM
9305 for (p_b_m_iterator it = probes_by_module.begin(); it != probes_by_module.end(); it++)
9306 {
9307 kprobe_derived_probe* p = it->second;
9308 s.op->newline() << "{";
9309 if (p->has_return)
9310 s.op->line() << " .return_p=1,";
6270adc1 9311
e6fe60e7
AM
9312 if (p->has_maxactive)
9313 {
9314 s.op->line() << " .maxactive_p=1,";
9315 assert (p->maxactive_val >= 0 && p->maxactive_val <= USHRT_MAX);
9316 s.op->line() << " .maxactive_val=" << p->maxactive_val << ",";
9317 }
6270adc1 9318
b350f56b
JS
9319 if (p->locations[0]->optional)
9320 s.op->line() << " .optional_p=1,";
9321
e6fe60e7 9322 if (p->has_statement)
c8d9d15e 9323 s.op->line() << " .address=(unsigned long)0x" << hex << p->addr << dec << "ULL,";
e6fe60e7 9324 else
c8d9d15e 9325 s.op->line() << " .symbol_string=\"" << p->symbol_name << "\",";
5d67b47c 9326
faea5e16 9327 s.op->line() << " .probe=" << common_probe_init (p) << ",";
e6fe60e7 9328 s.op->line() << " },";
935447c8
DS
9329 }
9330
e6fe60e7 9331 s.op->newline(-1) << "};";
5d67b47c 9332
e6fe60e7
AM
9333 // Emit the kprobes callback function
9334 s.op->newline();
88747011 9335 s.op->newline() << "static int enter_kprobe2_probe (struct kprobe *inst,";
e6fe60e7
AM
9336 s.op->line() << " struct pt_regs *regs) {";
9337 // NB: as of PR5673, the kprobe|kretprobe union struct is in BSS
9338 s.op->newline(1) << "int kprobe_idx = ((uintptr_t)inst-(uintptr_t)stap_dwarfless_kprobes)/sizeof(struct stap_dwarfless_kprobe);";
9339 // Check that the index is plausible
9340 s.op->newline() << "struct stap_dwarfless_probe *sdp = &stap_dwarfless_probes[";
9341 s.op->line() << "((kprobe_idx >= 0 && kprobe_idx < " << probes_by_module.size() << ")?";
9342 s.op->line() << "kprobe_idx:0)"; // NB: at least we avoid memory corruption
9343 // XXX: it would be nice to give a more verbose error though; BUG_ON later?
9344 s.op->line() << "];";
71db462b 9345 common_probe_entryfn_prologue (s, "STAP_SESSION_RUNNING", "sdp->probe",
cda141c2 9346 "stp_probe_type_kprobe");
d9aed31e 9347 s.op->newline() << "c->kregs = regs;";
6415ddde
MW
9348
9349 // Make it look like the IP is set as it wouldn't have been replaced
9350 // by a breakpoint instruction when calling real probe handler. Reset
9351 // IP regs on return, so we don't confuse kprobes. PR10458
9352 s.op->newline() << "{";
9353 s.op->indent(1);
d9aed31e 9354 s.op->newline() << "unsigned long kprobes_ip = REG_IP(c->kregs);";
259d54c0 9355 s.op->newline() << "SET_REG_IP(regs, (unsigned long) inst->addr);";
26e63673 9356 s.op->newline() << "(*sdp->probe->ph) (c);";
259d54c0 9357 s.op->newline() << "SET_REG_IP(regs, kprobes_ip);";
6415ddde
MW
9358 s.op->newline(-1) << "}";
9359
ef1337ee 9360 common_probe_entryfn_epilogue (s, true, otf_safe_context(s));
e6fe60e7
AM
9361 s.op->newline() << "return 0;";
9362 s.op->newline(-1) << "}";
935447c8 9363
e6fe60e7
AM
9364 // Same for kretprobes
9365 s.op->newline();
88747011 9366 s.op->newline() << "static int enter_kretprobe2_probe (struct kretprobe_instance *inst,";
e6fe60e7
AM
9367 s.op->line() << " struct pt_regs *regs) {";
9368 s.op->newline(1) << "struct kretprobe *krp = inst->rp;";
935447c8 9369
e6fe60e7
AM
9370 // NB: as of PR5673, the kprobe|kretprobe union struct is in BSS
9371 s.op->newline() << "int kprobe_idx = ((uintptr_t)krp-(uintptr_t)stap_dwarfless_kprobes)/sizeof(struct stap_dwarfless_kprobe);";
9372 // Check that the index is plausible
9373 s.op->newline() << "struct stap_dwarfless_probe *sdp = &stap_dwarfless_probes[";
9374 s.op->line() << "((kprobe_idx >= 0 && kprobe_idx < " << probes_by_module.size() << ")?";
9375 s.op->line() << "kprobe_idx:0)"; // NB: at least we avoid memory corruption
9376 // XXX: it would be nice to give a more verbose error though; BUG_ON later?
9377 s.op->line() << "];";
935447c8 9378
71db462b 9379 common_probe_entryfn_prologue (s, "STAP_SESSION_RUNNING", "sdp->probe",
cda141c2 9380 "stp_probe_type_kretprobe");
d9aed31e 9381 s.op->newline() << "c->kregs = regs;";
6dceb5c9 9382 s.op->newline() << "c->ips.krp.pi = inst;"; // for assisting runtime's backtrace logic
6415ddde
MW
9383
9384 // Make it look like the IP is set as it wouldn't have been replaced
9385 // by a breakpoint instruction when calling real probe handler. Reset
9386 // IP regs on return, so we don't confuse kprobes. PR10458
9387 s.op->newline() << "{";
9388 s.op->indent(1);
d9aed31e 9389 s.op->newline() << "unsigned long kprobes_ip = REG_IP(c->kregs);";
259d54c0 9390 s.op->newline() << "SET_REG_IP(regs, (unsigned long) inst->rp->kp.addr);";
26e63673 9391 s.op->newline() << "(*sdp->probe->ph) (c);";
259d54c0 9392 s.op->newline() << "SET_REG_IP(regs, kprobes_ip);";
6415ddde
MW
9393 s.op->newline(-1) << "}";
9394
ef1337ee 9395 common_probe_entryfn_epilogue (s, true, otf_safe_context(s));
e6fe60e7
AM
9396 s.op->newline() << "return 0;";
9397 s.op->newline(-1) << "}";
bd659351 9398
03a4ec63 9399 s.op->newline() << "#ifdef STAPCONF_KALLSYMS_ON_EACH_SYMBOL";
bd659351
MW
9400 s.op->newline() << "static int kprobe_resolve(void *data, const char *name,";
9401 s.op->newline() << " struct module *owner,";
9402 s.op->newline() << " unsigned long val) {";
9403 s.op->newline(1) << "int i;";
fc1d2aa2
MW
9404 s.op->newline() << "int *p = (int *) data;";
9405 s.op->newline() << "for (i=0; i<" << probes_by_module.size()
9406 << " && *p > 0; i++) {";
bd659351 9407 s.op->newline(1) << "struct stap_dwarfless_probe *sdp = & stap_dwarfless_probes[i];";
8c272819
YW
9408 s.op->newline() << "if (! sdp->address) {";
9409 s.op->indent(1);
9410 s.op->newline() << "const char *colon;";
9411 s.op->newline() << "if (owner && (colon = strchr(sdp->symbol_string, ':'))) {";
9412 s.op->indent(1);
9413 s.op->newline() << "if ((strlen(owner->name) == (colon - sdp->symbol_string))";
9414 s.op->newline() << " && (strncmp(sdp->symbol_string, owner->name, colon - sdp->symbol_string) == 0)";
9415 s.op->newline() << " && (strcmp(colon + 1, name) == 0)) {";
9416 s.op->newline(1) << "sdp->address = val;";
9417 s.op->newline() << "(*p)--;";
9418 s.op->newline(-1) << "}";
9419 s.op->newline(-1) << "}";
9420 s.op->newline() << "else {";
fc1d2aa2 9421 s.op->newline(1) << "if (strcmp(sdp->symbol_string, name) == 0) {";
bd659351 9422 s.op->newline(1) << "sdp->address = val;";
fc1d2aa2
MW
9423 s.op->newline() << "(*p)--;";
9424 s.op->newline(-1) << "}";
8c272819
YW
9425 s.op->newline(-1) << "}";
9426 s.op->newline(-1) << "}";
9427 s.op->newline(-1) << "}";
fc1d2aa2 9428 s.op->newline() << "return (p > 0) ? 0 : -1;";
bd659351 9429 s.op->newline(-1) << "}";
03a4ec63 9430 s.op->newline() << "#endif";
935447c8
DS
9431}
9432
e6fe60e7 9433
6270adc1 9434void
e6fe60e7 9435kprobe_derived_probe_group::emit_module_init (systemtap_session& s)
6270adc1 9436{
03a4ec63 9437 s.op->newline() << "#ifdef STAPCONF_KALLSYMS_ON_EACH_SYMBOL";
fc1d2aa2
MW
9438 s.op->newline() << "{";
9439 s.op->newline(1) << "int p = 0;";
9440 s.op->newline() << "for (i = 0; i < " << probes_by_module.size() << "; i++) {";
9441 s.op->newline(1) << "struct stap_dwarfless_probe *sdp = & stap_dwarfless_probes[i];";
9442 s.op->newline() << "if (! sdp->address)";
9443 s.op->newline(1) << "p++;";
9444 s.op->newline(-2) << "}";
9445 s.op->newline() << "kallsyms_on_each_symbol(kprobe_resolve, &p);";
9446 s.op->newline(-1) << "}";
03a4ec63 9447 s.op->newline() << "#endif";
bd659351 9448
e6fe60e7 9449 s.op->newline() << "for (i=0; i<" << probes_by_module.size() << "; i++) {";
c8d9d15e 9450 s.op->newline(1) << "struct stap_dwarfless_probe *sdp = & stap_dwarfless_probes[i];";
e6fe60e7 9451 s.op->newline() << "struct stap_dwarfless_kprobe *kp = & stap_dwarfless_kprobes[i];";
c8d9d15e 9452 s.op->newline() << "void *addr = (void *) sdp->address;";
03a4ec63
MW
9453 s.op->newline() << "const char *symbol_name = addr ? NULL : sdp->symbol_string;";
9454
9455 s.op->newline() << "#ifdef STAPCONF_KALLSYMS_ON_EACH_SYMBOL";
bd659351
MW
9456 s.op->newline() << "if (! addr) {";
9457 s.op->newline(1) << "sdp->registered_p = 0;";
9319b767
MW
9458 s.op->newline() << "if (!sdp->optional_p)";
9459 s.op->newline(1) << "_stp_warn (\"probe %s registration error (symbol not found)\", probe_point);";
9460 s.op->newline(-1) << "continue;";
bd659351 9461 s.op->newline(-1) << "}";
03a4ec63
MW
9462 s.op->newline() << "#endif";
9463
26e63673 9464 s.op->newline() << "probe_point = sdp->probe->pp;"; // for error messages
e6fe60e7 9465 s.op->newline() << "if (sdp->return_p) {";
c8d9d15e 9466 s.op->newline(1) << "kp->u.krp.kp.addr = addr;";
766cee5f 9467 s.op->newline() << "#ifdef STAPCONF_KPROBE_SYMBOL_NAME";
03a4ec63 9468 s.op->newline() << "kp->u.krp.kp.symbol_name = (char *) symbol_name;";
766cee5f 9469 s.op->newline() << "#endif";
e6fe60e7
AM
9470 s.op->newline() << "if (sdp->maxactive_p) {";
9471 s.op->newline(1) << "kp->u.krp.maxactive = sdp->maxactive_val;";
9472 s.op->newline(-1) << "} else {";
f07c3b68 9473 s.op->newline(1) << "kp->u.krp.maxactive = KRETACTIVE;";
e6fe60e7 9474 s.op->newline(-1) << "}";
88747011 9475 s.op->newline() << "kp->u.krp.handler = &enter_kretprobe2_probe;";
e6fe60e7
AM
9476 // to ensure safeness of bspcache, always use aggr_kprobe on ia64
9477 s.op->newline() << "#ifdef __ia64__";
e6fe60e7 9478 s.op->newline() << "kp->dummy.addr = kp->u.krp.kp.addr;";
766cee5f 9479 s.op->newline() << "#ifdef STAPCONF_KPROBE_SYMBOL_NAME";
c8d9d15e 9480 s.op->newline() << "kp->dummy.symbol_name = kp->u.krp.kp.symbol_name;";
766cee5f 9481 s.op->newline() << "#endif";
c8d9d15e 9482 s.op->newline() << "kp->dummy.pre_handler = NULL;";
e6fe60e7
AM
9483 s.op->newline() << "rc = register_kprobe (& kp->dummy);";
9484 s.op->newline() << "if (rc == 0) {";
9485 s.op->newline(1) << "rc = register_kretprobe (& kp->u.krp);";
9486 s.op->newline() << "if (rc != 0)";
9487 s.op->newline(1) << "unregister_kprobe (& kp->dummy);";
9488 s.op->newline(-2) << "}";
9489 s.op->newline() << "#else";
9490 s.op->newline() << "rc = register_kretprobe (& kp->u.krp);";
9491 s.op->newline() << "#endif";
9492 s.op->newline(-1) << "} else {";
9493 // to ensure safeness of bspcache, always use aggr_kprobe on ia64
c8d9d15e 9494 s.op->newline(1) << "kp->u.kp.addr = addr;";
766cee5f 9495 s.op->newline() << "#ifdef STAPCONF_KPROBE_SYMBOL_NAME";
03a4ec63 9496 s.op->newline() << "kp->u.kp.symbol_name = (char *) symbol_name;";
766cee5f 9497 s.op->newline() << "#endif";
88747011 9498 s.op->newline() << "kp->u.kp.pre_handler = &enter_kprobe2_probe;";
e6fe60e7 9499 s.op->newline() << "#ifdef __ia64__";
e6fe60e7 9500 s.op->newline() << "kp->dummy.pre_handler = NULL;";
c8d9d15e 9501 s.op->newline() << "kp->dummy.addr = kp->u.kp.addr;";
766cee5f 9502 s.op->newline() << "#ifdef STAPCONF_KPROBE_SYMBOL_NAME";
c8d9d15e 9503 s.op->newline() << "kp->dummy.symbol_name = kp->u.kp.symbol_name;";
766cee5f 9504 s.op->newline() << "#endif";
e6fe60e7
AM
9505 s.op->newline() << "rc = register_kprobe (& kp->dummy);";
9506 s.op->newline() << "if (rc == 0) {";
9507 s.op->newline(1) << "rc = register_kprobe (& kp->u.kp);";
9508 s.op->newline() << "if (rc != 0)";
9509 s.op->newline(1) << "unregister_kprobe (& kp->dummy);";
9510 s.op->newline(-2) << "}";
9511 s.op->newline() << "#else";
9512 s.op->newline() << "rc = register_kprobe (& kp->u.kp);";
9513 s.op->newline() << "#endif";
9514 s.op->newline(-1) << "}";
9515 s.op->newline() << "if (rc) {"; // PR6749: tolerate a failed register_*probe.
9516 s.op->newline(1) << "sdp->registered_p = 0;";
b350f56b 9517 s.op->newline() << "if (!sdp->optional_p)";
50b6acf7 9518 s.op->newline(1) << "_stp_warn (\"probe %s (address 0x%lx) registration error (rc %d)\", probe_point, (unsigned long) addr, rc);";
b350f56b 9519 s.op->newline(-1) << "rc = 0;"; // continue with other probes
e6fe60e7
AM
9520 // XXX: shall we increment numskipped?
9521 s.op->newline(-1) << "}";
6270adc1 9522
e6fe60e7
AM
9523 s.op->newline() << "else sdp->registered_p = 1;";
9524 s.op->newline(-1) << "}"; // for loop
6270adc1
MH
9525}
9526
b4be7cbc 9527
e6fe60e7
AM
9528void
9529kprobe_derived_probe_group::emit_module_exit (systemtap_session& s)
935447c8 9530{
e6fe60e7
AM
9531 //Unregister kprobes by batch interfaces.
9532 s.op->newline() << "#if defined(STAPCONF_UNREGISTER_KPROBES)";
9533 s.op->newline() << "j = 0;";
9534 s.op->newline() << "for (i=0; i<" << probes_by_module.size() << "; i++) {";
9535 s.op->newline(1) << "struct stap_dwarfless_probe *sdp = & stap_dwarfless_probes[i];";
9536 s.op->newline() << "struct stap_dwarfless_kprobe *kp = & stap_dwarfless_kprobes[i];";
9537 s.op->newline() << "if (! sdp->registered_p) continue;";
9538 s.op->newline() << "if (!sdp->return_p)";
c9116e99 9539 s.op->newline(1) << "stap_unreg_kprobes2[j++] = &kp->u.kp;";
e6fe60e7 9540 s.op->newline(-2) << "}";
c9116e99 9541 s.op->newline() << "unregister_kprobes((struct kprobe **)stap_unreg_kprobes2, j);";
e6fe60e7
AM
9542 s.op->newline() << "j = 0;";
9543 s.op->newline() << "for (i=0; i<" << probes_by_module.size() << "; i++) {";
9544 s.op->newline(1) << "struct stap_dwarfless_probe *sdp = & stap_dwarfless_probes[i];";
9545 s.op->newline() << "struct stap_dwarfless_kprobe *kp = & stap_dwarfless_kprobes[i];";
9546 s.op->newline() << "if (! sdp->registered_p) continue;";
9547 s.op->newline() << "if (sdp->return_p)";
c9116e99 9548 s.op->newline(1) << "stap_unreg_kprobes2[j++] = &kp->u.krp;";
e6fe60e7 9549 s.op->newline(-2) << "}";
c9116e99 9550 s.op->newline() << "unregister_kretprobes((struct kretprobe **)stap_unreg_kprobes2, j);";
e6fe60e7
AM
9551 s.op->newline() << "#ifdef __ia64__";
9552 s.op->newline() << "j = 0;";
9553 s.op->newline() << "for (i=0; i<" << probes_by_module.size() << "; i++) {";
9554 s.op->newline(1) << "struct stap_dwarfless_probe *sdp = & stap_dwarfless_probes[i];";
9555 s.op->newline() << "struct stap_dwarfless_kprobe *kp = & stap_dwarfless_kprobes[i];";
9556 s.op->newline() << "if (! sdp->registered_p) continue;";
c9116e99 9557 s.op->newline() << "stap_unreg_kprobes2[j++] = &kp->dummy;";
e6fe60e7 9558 s.op->newline(-1) << "}";
c9116e99 9559 s.op->newline() << "unregister_kprobes((struct kprobe **)stap_unreg_kprobes2, j);";
e6fe60e7
AM
9560 s.op->newline() << "#endif";
9561 s.op->newline() << "#endif";
3e3bd7b6 9562
e6fe60e7
AM
9563 s.op->newline() << "for (i=0; i<" << probes_by_module.size() << "; i++) {";
9564 s.op->newline(1) << "struct stap_dwarfless_probe *sdp = & stap_dwarfless_probes[i];";
9565 s.op->newline() << "struct stap_dwarfless_kprobe *kp = & stap_dwarfless_kprobes[i];";
9566 s.op->newline() << "if (! sdp->registered_p) continue;";
9567 s.op->newline() << "if (sdp->return_p) {";
9568 s.op->newline() << "#if !defined(STAPCONF_UNREGISTER_KPROBES)";
9569 s.op->newline(1) << "unregister_kretprobe (&kp->u.krp);";
9570 s.op->newline() << "#endif";
065d5567 9571 s.op->newline() << "atomic_add (kp->u.krp.nmissed, skipped_count());";
e6fe60e7
AM
9572 s.op->newline() << "#ifdef STP_TIMING";
9573 s.op->newline() << "if (kp->u.krp.nmissed)";
26e63673 9574 s.op->newline(1) << "_stp_warn (\"Skipped due to missed kretprobe/1 on '%s': %d\\n\", sdp->probe->pp, kp->u.krp.nmissed);";
e6fe60e7 9575 s.op->newline(-1) << "#endif";
065d5567 9576 s.op->newline() << "atomic_add (kp->u.krp.kp.nmissed, skipped_count());";
e6fe60e7
AM
9577 s.op->newline() << "#ifdef STP_TIMING";
9578 s.op->newline() << "if (kp->u.krp.kp.nmissed)";
26e63673 9579 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
9580 s.op->newline(-1) << "#endif";
9581 s.op->newline(-1) << "} else {";
9582 s.op->newline() << "#if !defined(STAPCONF_UNREGISTER_KPROBES)";
9583 s.op->newline(1) << "unregister_kprobe (&kp->u.kp);";
9584 s.op->newline() << "#endif";
065d5567 9585 s.op->newline() << "atomic_add (kp->u.kp.nmissed, skipped_count());";
e6fe60e7
AM
9586 s.op->newline() << "#ifdef STP_TIMING";
9587 s.op->newline() << "if (kp->u.kp.nmissed)";
26e63673 9588 s.op->newline(1) << "_stp_warn (\"Skipped due to missed kprobe on '%s': %lu\\n\", sdp->probe->pp, kp->u.kp.nmissed);";
e6fe60e7
AM
9589 s.op->newline(-1) << "#endif";
9590 s.op->newline(-1) << "}";
9591 s.op->newline() << "#if !defined(STAPCONF_UNREGISTER_KPROBES) && defined(__ia64__)";
9592 s.op->newline() << "unregister_kprobe (&kp->dummy);";
9593 s.op->newline() << "#endif";
9594 s.op->newline() << "sdp->registered_p = 0;";
9595 s.op->newline(-1) << "}";
f8a968bc
JS
9596}
9597
e6fe60e7 9598struct kprobe_builder: public derived_probe_builder
3c1b3d06 9599{
9fdf787d 9600public:
2a639817 9601 kprobe_builder() {}
9fdf787d 9602
2a639817 9603 void build_no_more (systemtap_session &s) {}
9fdf787d 9604
e6fe60e7
AM
9605 virtual void build(systemtap_session & sess,
9606 probe * base,
9607 probe_point * location,
9608 literal_map_t const & parameters,
9609 vector<derived_probe *> & finished_results);
9610};
3c1b3d06
FCE
9611
9612
79189b84 9613void
05fb3e0c 9614kprobe_builder::build(systemtap_session & sess,
e6fe60e7
AM
9615 probe * base,
9616 probe_point * location,
9617 literal_map_t const & parameters,
9618 vector<derived_probe *> & finished_results)
79189b84 9619{
e6fe60e7 9620 string function_string_val, module_string_val;
05fb3e0c 9621 string path, library, path_tgt, library_tgt;
b6371390
JS
9622 int64_t statement_num_val = 0, maxactive_val = 0;
9623 bool has_function_str, has_module_str, has_statement_num;
e6afb07b 9624 bool has_absolute, has_call, has_return, has_maxactive;
b642c901 9625 bool has_path, has_library;
79189b84 9626
b6371390
JS
9627 has_function_str = get_param(parameters, TOK_FUNCTION, function_string_val);
9628 has_module_str = get_param(parameters, TOK_MODULE, module_string_val);
e6afb07b 9629 has_call = has_null_param (parameters, TOK_CALL);
b6371390
JS
9630 has_return = has_null_param (parameters, TOK_RETURN);
9631 has_maxactive = get_param(parameters, TOK_MAXACTIVE, maxactive_val);
9632 has_statement_num = get_param(parameters, TOK_STATEMENT, statement_num_val);
9633 has_absolute = has_null_param (parameters, TOK_ABSOLUTE);
b642c901
SC
9634 has_path = get_param (parameters, TOK_PROCESS, path);
9635 has_library = get_param (parameters, TOK_LIBRARY, library);
9636
9637 if (has_path)
05fb3e0c
WF
9638 {
9639 path = find_executable (path, sess.sysroot, sess.sysenv);
9640 path_tgt = path_remove_sysroot(sess, path);
9641 }
b642c901 9642 if (has_library)
05fb3e0c
WF
9643 {
9644 library = find_executable (library, sess.sysroot, sess.sysenv,
9645 "LD_LIBRARY_PATH");
9646 library_tgt = path_remove_sysroot(sess, library);
9647 }
c57ea854 9648
b6371390 9649 if (has_function_str)
6fb70fb7 9650 {
2a639817 9651 if (has_module_str)
9fdf787d
DS
9652 {
9653 function_string_val = module_string_val + ":" + function_string_val;
9654 derived_probe *dp
23dc94f6
DS
9655 = new kprobe_derived_probe (sess, finished_results, base,
9656 location, function_string_val,
e6afb07b
JL
9657 0, has_call, has_return,
9658 has_statement_num, has_maxactive,
9659 has_path, has_library, maxactive_val,
9660 path_tgt, library_tgt);
9fdf787d
DS
9661 finished_results.push_back (dp);
9662 }
9663 else
9664 {
2a639817
JS
9665 vector<string> matches;
9666
9667 // Simple names can be found directly
9668 if (function_string_val.find_first_of("*?[") == string::npos)
9669 {
9670 if (sess.kernel_functions.count(function_string_val))
9671 matches.push_back(function_string_val);
9672 }
9673 else // Search function name list for matching names
9674 {
9675 for (set<string>::const_iterator it = sess.kernel_functions.begin();
9676 it != sess.kernel_functions.end(); it++)
9677 // fnmatch returns zero for matching.
9678 if (fnmatch(function_string_val.c_str(), it->c_str(), 0) == 0)
9679 matches.push_back(*it);
9680 }
86758d5f 9681
2a639817
JS
9682 for (vector<string>::const_iterator it = matches.begin();
9683 it != matches.end(); it++)
9fdf787d 9684 {
2a639817
JS
9685 derived_probe *dp
9686 = new kprobe_derived_probe (sess, finished_results, base,
e6afb07b
JL
9687 location, *it, 0, has_call,
9688 has_return, has_statement_num,
2a639817
JS
9689 has_maxactive, has_path,
9690 has_library, maxactive_val,
9691 path_tgt, library_tgt);
9692 finished_results.push_back (dp);
9fdf787d
DS
9693 }
9694 }
6fb70fb7 9695 }
e6fe60e7 9696 else
b6371390
JS
9697 {
9698 // assert guru mode for absolute probes
9699 if ( has_statement_num && has_absolute && !base->privileged )
dc09353a 9700 throw SEMANTIC_ERROR (_("absolute statement probe in unprivileged script; need stap -g"), base->tok);
b6371390 9701
23dc94f6
DS
9702 finished_results.push_back (new kprobe_derived_probe (sess,
9703 finished_results,
9704 base,
b6371390
JS
9705 location, "",
9706 statement_num_val,
e6afb07b 9707 has_call,
b6371390
JS
9708 has_return,
9709 has_statement_num,
9710 has_maxactive,
b642c901
SC
9711 has_path,
9712 has_library,
9713 maxactive_val,
05fb3e0c
WF
9714 path_tgt,
9715 library_tgt));
96b030fe 9716 }
79189b84
JS
9717}
9718
23dc94f6
DS
9719
9720void
9721kprobe_var_expanding_visitor::visit_entry_op (entry_op *e)
9722{
9723 expression *repl = e;
9724
bd5b25e1
JS
9725 if (has_return)
9726 {
9727 // expand the operand as if it weren't a return probe
9728 has_return = false;
9729 replace (e->operand);
9730 has_return = true;
23dc94f6 9731
bd5b25e1
JS
9732 // XXX it would be nice to use gen_kretprobe_saved_return when
9733 // available, but it requires knowing the types already, which is
9734 // problematic for arbitrary expressons.
9735 repl = gen_mapped_saved_return (sess, e->operand, "entry",
9736 add_block, add_block_tid,
9737 add_call_probe, add_call_probe_tid);
9738 }
23dc94f6
DS
9739 provide (repl);
9740}
9741
9742
dd225250
PS
9743// ------------------------------------------------------------------------
9744// Hardware breakpoint based probes.
9745// ------------------------------------------------------------------------
9746
9747static const string TOK_HWBKPT("data");
9748static const string TOK_HWBKPT_WRITE("write");
9749static const string TOK_HWBKPT_RW("rw");
9750static const string TOK_LENGTH("length");
9751
9752#define HWBKPT_READ 0
9753#define HWBKPT_WRITE 1
9754#define HWBKPT_RW 2
9755struct hwbkpt_derived_probe: public derived_probe
9756{
9757 hwbkpt_derived_probe (probe *base,
9758 probe_point *location,
9759 uint64_t addr,
9760 string symname,
9761 unsigned int len,
9762 bool has_only_read_access,
9763 bool has_only_write_access,
9764 bool has_rw_access
9765 );
9766 Dwarf_Addr hwbkpt_addr;
9767 string symbol_name;
9768 unsigned int hwbkpt_access,hwbkpt_len;
9769
9770 void printsig (std::ostream &o) const;
9771 void join_group (systemtap_session& s);
9772};
9773
9774struct hwbkpt_derived_probe_group: public derived_probe_group
9775{
dd225250 9776private:
dac77b80 9777 vector<hwbkpt_derived_probe*> hwbkpt_probes;
dd225250
PS
9778
9779public:
9780 void enroll (hwbkpt_derived_probe* probe, systemtap_session& s);
9781 void emit_module_decls (systemtap_session& s);
9782 void emit_module_init (systemtap_session& s);
9783 void emit_module_exit (systemtap_session& s);
9784};
9785
9786hwbkpt_derived_probe::hwbkpt_derived_probe (probe *base,
9ea68eb9
JS
9787 probe_point *location,
9788 uint64_t addr,
9789 string symname,
9790 unsigned int len,
9791 bool has_only_read_access,
9792 bool has_only_write_access,
822a6a3d 9793 bool):
4c5d1300 9794 derived_probe (base, location, true /* .components soon rewritten */ ),
dd225250
PS
9795 hwbkpt_addr (addr),
9796 symbol_name (symname),
9797 hwbkpt_len (len)
9798{
9799 this->tok = base->tok;
9800
9801 vector<probe_point::component*> comps;
9802 comps.push_back (new probe_point::component(TOK_KERNEL));
9803
9804 if (hwbkpt_addr)
9ea68eb9
JS
9805 comps.push_back (new probe_point::component (TOK_HWBKPT,
9806 new literal_number(hwbkpt_addr, true)));
9807 else if (symbol_name.size())
9808 comps.push_back (new probe_point::component (TOK_HWBKPT, new literal_string(symbol_name)));
dd225250
PS
9809
9810 comps.push_back (new probe_point::component (TOK_LENGTH, new literal_number(hwbkpt_len)));
9811
9812 if (has_only_read_access)
9ea68eb9 9813 this->hwbkpt_access = HWBKPT_READ ;
dd225250
PS
9814//TODO add code for comps.push_back for read, since this flag is not for x86
9815
9816 else
9ea68eb9
JS
9817 {
9818 if (has_only_write_access)
9819 {
9820 this->hwbkpt_access = HWBKPT_WRITE ;
9821 comps.push_back (new probe_point::component(TOK_HWBKPT_WRITE));
9822 }
9823 else
9824 {
9825 this->hwbkpt_access = HWBKPT_RW ;
9826 comps.push_back (new probe_point::component(TOK_HWBKPT_RW));
9827 }
9828 }
dd225250
PS
9829
9830 this->sole_location()->components = comps;
9831}
9832
9833void hwbkpt_derived_probe::printsig (ostream& o) const
9834{
9835 sole_location()->print (o);
9836 printsig_nested (o);
9837}
9838
9839void hwbkpt_derived_probe::join_group (systemtap_session& s)
9840{
dac77b80
FCE
9841 if (! s.hwbkpt_derived_probes)
9842 s.hwbkpt_derived_probes = new hwbkpt_derived_probe_group ();
dd225250 9843 s.hwbkpt_derived_probes->enroll (this, s);
ca6d3b0f 9844 this->group = s.hwbkpt_derived_probes;
dd225250
PS
9845}
9846
9847void hwbkpt_derived_probe_group::enroll (hwbkpt_derived_probe* p, systemtap_session& s)
9848{
dac77b80
FCE
9849 hwbkpt_probes.push_back (p);
9850
9851 unsigned max_hwbkpt_probes_by_arch = 0;
9852 if (s.architecture == "i386" || s.architecture == "x86_64")
9853 max_hwbkpt_probes_by_arch = 4;
9854 else if (s.architecture == "s390")
9855 max_hwbkpt_probes_by_arch = 1;
9856
c57ea854 9857 if (hwbkpt_probes.size() >= max_hwbkpt_probes_by_arch)
2713ea24 9858 s.print_warning (_F("Too many hardware breakpoint probes requested for %s (%zu vs. %u)",
b530b5b3 9859 s.architecture.c_str(), hwbkpt_probes.size(), max_hwbkpt_probes_by_arch));
dd225250
PS
9860}
9861
9862void
9863hwbkpt_derived_probe_group::emit_module_decls (systemtap_session& s)
9864{
dac77b80 9865 if (hwbkpt_probes.empty()) return;
dd225250
PS
9866
9867 s.op->newline() << "/* ---- hwbkpt-based probes ---- */";
9868
9869 s.op->newline() << "#include <linux/perf_event.h>";
9870 s.op->newline() << "#include <linux/hw_breakpoint.h>";
9871 s.op->newline();
9872
9873 // Forward declare the master entry functions
23063de1 9874 s.op->newline() << "#ifdef STAPCONF_PERF_HANDLER_NMI";
dd225250
PS
9875 s.op->newline() << "static int enter_hwbkpt_probe (struct perf_event *bp,";
9876 s.op->line() << " int nmi,";
9877 s.op->line() << " struct perf_sample_data *data,";
9878 s.op->line() << " struct pt_regs *regs);";
23063de1
FCE
9879 s.op->newline() << "#else";
9880 s.op->newline() << "static int enter_hwbkpt_probe (struct perf_event *bp,";
9881 s.op->line() << " struct perf_sample_data *data,";
9882 s.op->line() << " struct pt_regs *regs);";
9883 s.op->newline() << "#endif";
79189b84 9884
dd225250
PS
9885 // Emit the actual probe list.
9886
9887 s.op->newline() << "static struct perf_event_attr ";
dac77b80 9888 s.op->newline() << "stap_hwbkpt_probe_array[" << hwbkpt_probes.size() << "];";
dd225250
PS
9889
9890 s.op->newline() << "static struct perf_event **";
dac77b80 9891 s.op->newline() << "stap_hwbkpt_ret_array[" << hwbkpt_probes.size() << "];";
dd225250
PS
9892 s.op->newline() << "static struct stap_hwbkpt_probe {";
9893 s.op->newline() << "int registered_p:1;";
43650b10 9894// registered_p = 0 signifies a probe that is unregistered (or failed)
dd225250
PS
9895// registered_p = 1 signifies a probe that got registered successfully
9896
faea5e16 9897 // Symbol Names are mostly small and uniform enough
dd225250 9898 // to justify putting const char*.
dac77b80 9899 s.op->newline() << "const char * const symbol;";
dd225250
PS
9900
9901 s.op->newline() << "const unsigned long address;";
9902 s.op->newline() << "uint8_t atype;";
bb0a4e12 9903 s.op->newline() << "unsigned int len;";
7c3e97f4 9904 s.op->newline() << "const struct stap_probe * const probe;";
dd225250
PS
9905 s.op->newline() << "} stap_hwbkpt_probes[] = {";
9906 s.op->indent(1);
9907
dac77b80 9908 for (unsigned int it = 0; it < hwbkpt_probes.size(); it++)
dd225250 9909 {
dac77b80 9910 hwbkpt_derived_probe* p = hwbkpt_probes.at(it);
dd225250 9911 s.op->newline() << "{";
dd225250
PS
9912 if (p->symbol_name.size())
9913 s.op->line() << " .address=(unsigned long)0x0" << "ULL,";
9914 else
9915 s.op->line() << " .address=(unsigned long)0x" << hex << p->hwbkpt_addr << dec << "ULL,";
9916 switch(p->hwbkpt_access){
9917 case HWBKPT_READ:
9918 s.op->line() << " .atype=HW_BREAKPOINT_R ,";
bb0a4e12 9919 break;
dd225250
PS
9920 case HWBKPT_WRITE:
9921 s.op->line() << " .atype=HW_BREAKPOINT_W ,";
bb0a4e12 9922 break;
dd225250
PS
9923 case HWBKPT_RW:
9924 s.op->line() << " .atype=HW_BREAKPOINT_R|HW_BREAKPOINT_W ,";
bb0a4e12 9925 break;
dd225250
PS
9926 };
9927 s.op->line() << " .len=" << p->hwbkpt_len << ",";
faea5e16 9928 s.op->line() << " .probe=" << common_probe_init (p) << ",";
dd225250 9929 s.op->line() << " .symbol=\"" << p->symbol_name << "\",";
dd225250
PS
9930 s.op->line() << " },";
9931 }
dac77b80 9932 s.op->newline(-1) << "};";
dd225250
PS
9933
9934 // Emit the hwbkpt callback function
9935 s.op->newline() ;
23063de1 9936 s.op->newline() << "#ifdef STAPCONF_PERF_HANDLER_NMI";
dd225250
PS
9937 s.op->newline() << "static int enter_hwbkpt_probe (struct perf_event *bp,";
9938 s.op->line() << " int nmi,";
9939 s.op->line() << " struct perf_sample_data *data,";
9940 s.op->line() << " struct pt_regs *regs) {";
23063de1
FCE
9941 s.op->newline() << "#else";
9942 s.op->newline() << "static int enter_hwbkpt_probe (struct perf_event *bp,";
9943 s.op->line() << " struct perf_sample_data *data,";
9944 s.op->line() << " struct pt_regs *regs) {";
9945 s.op->newline() << "#endif";
dac77b80
FCE
9946 s.op->newline(1) << "unsigned int i;";
9947 s.op->newline() << "if (bp->attr.type != PERF_TYPE_BREAKPOINT) return -1;";
9948 s.op->newline() << "for (i=0; i<" << hwbkpt_probes.size() << "; i++) {";
9949 s.op->newline(1) << "struct perf_event_attr *hp = & stap_hwbkpt_probe_array[i];";
9950 // XXX: why not match stap_hwbkpt_ret_array[i] against bp instead?
9951 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) {";
9952 s.op->newline(1) << "struct stap_hwbkpt_probe *sdp = &stap_hwbkpt_probes[i];";
71db462b 9953 common_probe_entryfn_prologue (s, "STAP_SESSION_RUNNING", "sdp->probe",
cda141c2 9954 "stp_probe_type_hwbkpt");
d9aed31e 9955 s.op->newline() << "if (user_mode(regs)) {";
e04b5d74 9956 s.op->newline(1)<< "c->user_mode_p = 1;";
d9aed31e
MW
9957 s.op->newline() << "c->uregs = regs;";
9958 s.op->newline(-1) << "} else {";
9959 s.op->newline(1) << "c->kregs = regs;";
9960 s.op->newline(-1) << "}";
26e63673 9961 s.op->newline() << "(*sdp->probe->ph) (c);";
ef1337ee 9962 common_probe_entryfn_epilogue (s, true, otf_safe_context(s));
dac77b80 9963 s.op->newline(-1) << "}";
dd225250
PS
9964 s.op->newline(-1) << "}";
9965 s.op->newline() << "return 0;";
dac77b80 9966 s.op->newline(-1) << "}";
dd225250
PS
9967}
9968
9969void
9970hwbkpt_derived_probe_group::emit_module_init (systemtap_session& s)
9971{
dac77b80 9972 s.op->newline() << "for (i=0; i<" << hwbkpt_probes.size() << "; i++) {";
dd225250
PS
9973 s.op->newline(1) << "struct stap_hwbkpt_probe *sdp = & stap_hwbkpt_probes[i];";
9974 s.op->newline() << "struct perf_event_attr *hp = & stap_hwbkpt_probe_array[i];";
9975 s.op->newline() << "void *addr = (void *) sdp->address;";
9976 s.op->newline() << "const char *hwbkpt_symbol_name = addr ? NULL : sdp->symbol;";
dac77b80
FCE
9977 s.op->newline() << "hw_breakpoint_init(hp);";
9978 s.op->newline() << "if (addr)";
9979 s.op->newline(1) << "hp->bp_addr = (unsigned long) addr;";
9980 s.op->newline(-1) << "else { ";
9981 s.op->newline(1) << "hp->bp_addr = kallsyms_lookup_name(hwbkpt_symbol_name);";
9982 s.op->newline() << "if (!hp->bp_addr) { ";
26e63673 9983 s.op->newline(1) << "_stp_warn(\"Probe %s registration skipped: invalid symbol %s \",sdp->probe->pp,hwbkpt_symbol_name);";
dac77b80
FCE
9984 s.op->newline() << "continue;";
9985 s.op->newline(-1) << "}";
9986 s.op->newline(-1) << "}";
9987 s.op->newline() << "hp->bp_type = sdp->atype;";
9988
9989 // On x86 & x86-64, hp->bp_len is not just a number but a macro/enum (!?!).
c57ea854 9990 if (s.architecture == "i386" || s.architecture == "x86_64" )
dac77b80
FCE
9991 {
9992 s.op->newline() << "switch(sdp->len) {";
9993 s.op->newline() << "case 1:";
9994 s.op->newline(1) << "hp->bp_len = HW_BREAKPOINT_LEN_1;";
9995 s.op->newline() << "break;";
9996 s.op->newline(-1) << "case 2:";
9997 s.op->newline(1) << "hp->bp_len = HW_BREAKPOINT_LEN_2;";
9998 s.op->newline() << "break;";
9999 s.op->newline(-1) << "case 3:";
10000 s.op->newline() << "case 4:";
10001 s.op->newline(1) << "hp->bp_len = HW_BREAKPOINT_LEN_4;";
10002 s.op->newline() << "break;";
10003 s.op->newline(-1) << "case 5:";
10004 s.op->newline() << "case 6:";
10005 s.op->newline() << "case 7:";
10006 s.op->newline() << "case 8:";
10007 s.op->newline() << "default:"; // XXX: could instead reject
10008 s.op->newline(1) << "hp->bp_len = HW_BREAKPOINT_LEN_8;";
10009 s.op->newline() << "break;";
10010 s.op->newline(-1) << "}";
10011 }
10012 else // other architectures presumed straightforward
10013 s.op->newline() << "hp->bp_len = sdp->len;";
10014
26e63673 10015 s.op->newline() << "probe_point = sdp->probe->pp;"; // for error messages
b273669e
MW
10016 s.op->newline() << "#ifdef STAPCONF_HW_BREAKPOINT_CONTEXT";
10017 s.op->newline() << "stap_hwbkpt_ret_array[i] = register_wide_hw_breakpoint(hp, (void *)&enter_hwbkpt_probe, NULL);";
10018 s.op->newline() << "#else";
dac77b80 10019 s.op->newline() << "stap_hwbkpt_ret_array[i] = register_wide_hw_breakpoint(hp, (void *)&enter_hwbkpt_probe);";
b273669e 10020 s.op->newline() << "#endif";
43650b10 10021 s.op->newline() << "rc = 0;";
dac77b80 10022 s.op->newline() << "if (IS_ERR(stap_hwbkpt_ret_array[i])) {";
43650b10
WC
10023 s.op->newline(1) << "rc = PTR_ERR(stap_hwbkpt_ret_array[i]);";
10024 s.op->newline() << "stap_hwbkpt_ret_array[i] = 0;";
10025 s.op->newline(-1) << "}";
217ef1f4
WC
10026 s.op->newline() << "if (rc) {";
10027 s.op->newline(1) << "_stp_warn(\"Hwbkpt probe %s: registration error %d, addr %p, name %s\", probe_point, rc, addr, hwbkpt_symbol_name);";
43650b10 10028 s.op->newline() << "sdp->registered_p = 0;";
dac77b80 10029 s.op->newline(-1) << "}";
dd225250 10030 s.op->newline() << " else sdp->registered_p = 1;";
dd225250
PS
10031 s.op->newline(-1) << "}"; // for loop
10032}
10033
10034void
10035hwbkpt_derived_probe_group::emit_module_exit (systemtap_session& s)
10036{
10037 //Unregister hwbkpt probes.
dac77b80 10038 s.op->newline() << "for (i=0; i<" << hwbkpt_probes.size() << "; i++) {";
dd225250 10039 s.op->newline(1) << "struct stap_hwbkpt_probe *sdp = & stap_hwbkpt_probes[i];";
dac77b80
FCE
10040 s.op->newline() << "if (sdp->registered_p == 0) continue;";
10041 s.op->newline() << "unregister_wide_hw_breakpoint(stap_hwbkpt_ret_array[i]);";
dd225250
PS
10042 s.op->newline() << "sdp->registered_p = 0;";
10043 s.op->newline(-1) << "}";
10044}
10045
10046struct hwbkpt_builder: public derived_probe_builder
10047{
10048 hwbkpt_builder() {}
10049 virtual void build(systemtap_session & sess,
10050 probe * base,
10051 probe_point * location,
10052 literal_map_t const & parameters,
10053 vector<derived_probe *> & finished_results);
10054};
10055
10056void
10057hwbkpt_builder::build(systemtap_session & sess,
10058 probe * base,
10059 probe_point * location,
10060 literal_map_t const & parameters,
10061 vector<derived_probe *> & finished_results)
10062{
10063 string symbol_str_val;
10064 int64_t hwbkpt_address, len;
10065 bool has_addr, has_symbol_str, has_write, has_rw, has_len;
10066
b47f3a55 10067 if (! (sess.kernel_config["CONFIG_PERF_EVENTS"] == string("y")))
dc09353a 10068 throw SEMANTIC_ERROR (_("CONFIG_PERF_EVENTS not available on this kernel"),
b47f3a55
FCE
10069 location->components[0]->tok);
10070 if (! (sess.kernel_config["CONFIG_HAVE_HW_BREAKPOINT"] == string("y")))
dc09353a 10071 throw SEMANTIC_ERROR (_("CONFIG_HAVE_HW_BREAKPOINT not available on this kernel"),
b47f3a55
FCE
10072 location->components[0]->tok);
10073
dd225250
PS
10074 has_addr = get_param (parameters, TOK_HWBKPT, hwbkpt_address);
10075 has_symbol_str = get_param (parameters, TOK_HWBKPT, symbol_str_val);
10076 has_len = get_param (parameters, TOK_LENGTH, len);
10077 has_write = (parameters.find(TOK_HWBKPT_WRITE) != parameters.end());
10078 has_rw = (parameters.find(TOK_HWBKPT_RW) != parameters.end());
10079
b31695eb
JL
10080 // Make an intermediate pp that is well-formed. It's pretty much the same as
10081 // the user-provided one, except that the addr literal is well-typed.
10082 probe_point* well_formed_loc = new probe_point(*location);
10083 well_formed_loc->well_formed = true;
10084
10085 vector<probe_point::component*> well_formed_comps;
10086 vector<probe_point::component*>::iterator it;
10087 for (it = location->components.begin();
10088 it != location->components.end(); ++it)
10089 if ((*it)->functor == TOK_HWBKPT && has_addr)
10090 well_formed_comps.push_back(new probe_point::component(TOK_HWBKPT,
10091 new literal_number(hwbkpt_address, true /* hex */ )));
10092 else
10093 well_formed_comps.push_back(*it);
10094 well_formed_loc->components = well_formed_comps;
10095 probe *new_base = new probe (base, well_formed_loc);
10096
dd225250
PS
10097 if (!has_len)
10098 len = 1;
10099
10100 if (has_addr)
b31695eb 10101 finished_results.push_back (new hwbkpt_derived_probe (new_base,
dd225250
PS
10102 location,
10103 hwbkpt_address,
10104 "",len,0,
10105 has_write,
10106 has_rw));
5d8a0aea 10107 else if (has_symbol_str)
b31695eb 10108 finished_results.push_back (new hwbkpt_derived_probe (new_base,
dd225250
PS
10109 location,
10110 0,
10111 symbol_str_val,len,0,
10112 has_write,
10113 has_rw));
5d8a0aea
FCE
10114 else
10115 assert (0);
dd225250 10116}
342d3f96 10117
0a6f5a3f
JS
10118// ------------------------------------------------------------------------
10119// statically inserted kernel-tracepoint derived probes
10120// ------------------------------------------------------------------------
10121
6fb70fb7 10122struct tracepoint_arg
79189b84 10123{
ad370dcc 10124 string name, c_type, typecast;
dcaa1a65 10125 bool usable, used, isptr;
f8a968bc 10126 Dwarf_Die type_die;
dcaa1a65 10127 tracepoint_arg(): usable(false), used(false), isptr(false) {}
6fb70fb7 10128};
79189b84 10129
0a6f5a3f
JS
10130struct tracepoint_derived_probe: public derived_probe
10131{
79189b84
JS
10132 tracepoint_derived_probe (systemtap_session& s,
10133 dwflpp& dw, Dwarf_Die& func_die,
10134 const string& tracepoint_name,
10135 probe* base_probe, probe_point* location);
bc9a523d 10136
79189b84 10137 systemtap_session& sess;
6fb70fb7
JS
10138 string tracepoint_name, header;
10139 vector <struct tracepoint_arg> args;
bc9a523d 10140
6fb70fb7 10141 void build_args(dwflpp& dw, Dwarf_Die& func_die);
d0bfd2ac 10142 void getargs (std::list<std::string> &arg_set) const;
79189b84 10143 void join_group (systemtap_session& s);
3e3bd7b6 10144 void print_dupe_stamp(ostream& o);
0a6f5a3f 10145};
79189b84
JS
10146
10147
0a6f5a3f 10148struct tracepoint_derived_probe_group: public generic_dpg<tracepoint_derived_probe>
79189b84 10149{
79189b84
JS
10150 void emit_module_decls (systemtap_session& s);
10151 void emit_module_init (systemtap_session& s);
10152 void emit_module_exit (systemtap_session& s);
0a6f5a3f 10153};
79189b84 10154
bc9a523d 10155
f8a968bc
JS
10156struct tracepoint_var_expanding_visitor: public var_expanding_visitor
10157{
10158 tracepoint_var_expanding_visitor(dwflpp& dw, const string& probe_name,
10159 vector <struct tracepoint_arg>& args):
10160 dw (dw), probe_name (probe_name), args (args) {}
10161 dwflpp& dw;
10162 const string& probe_name;
10163 vector <struct tracepoint_arg>& args;
bc9a523d 10164
f8a968bc
JS
10165 void visit_target_symbol (target_symbol* e);
10166 void visit_target_symbol_arg (target_symbol* e);
10167 void visit_target_symbol_context (target_symbol* e);
10168};
79189b84
JS
10169
10170
f8a968bc
JS
10171void
10172tracepoint_var_expanding_visitor::visit_target_symbol_arg (target_symbol* e)
75ead1f7 10173{
cc9001af 10174 string argname = e->sym_name();
75ead1f7 10175
f8a968bc
JS
10176 // search for a tracepoint parameter matching this name
10177 tracepoint_arg *arg = NULL;
10178 for (unsigned i = 0; i < args.size(); ++i)
dcaa1a65 10179 if (args[i].usable && args[i].name == argname)
f8a968bc
JS
10180 {
10181 arg = &args[i];
10182 arg->used = true;
10183 break;
10184 }
75ead1f7 10185
f8a968bc
JS
10186 if (arg == NULL)
10187 {
1d0499c2 10188 set<string> vars;
f8a968bc 10189 for (unsigned i = 0; i < args.size(); ++i)
1d0499c2
JL
10190 vars.insert("$" + args[i].name);
10191 vars.insert("$$name");
10192 vars.insert("$$parms");
10193 vars.insert("$$vars");
10194 string sugs = levenshtein_suggest(e->name, vars); // no need to limit, there's not that many
75ead1f7 10195
f8a968bc
JS
10196 // We hope that this value ends up not being referenced after all, so it
10197 // can be optimized out quietly.
1d0499c2
JL
10198 throw SEMANTIC_ERROR(_F("unable to find tracepoint variable '%s'%s",
10199 e->name.c_str(), sugs.empty() ? "" :
10200 (_(" (alternatives: ") + sugs + ")").c_str()), e->tok);
10201 // NB: we use 'alternatives' because we list all
f8a968bc
JS
10202 // NB: we can have multiple errors, since a target variable
10203 // may be expanded in several different contexts:
10204 // trace ("*") { $foo->bar }
f8a968bc 10205 }
75ead1f7 10206
37efef73
JS
10207 // make sure we're not dereferencing base types or void
10208 bool deref_p = arg->isptr && !null_die(&arg->type_die);
10209 if (!deref_p)
d19a9a82 10210 e->assert_no_components("tracepoint", true);
75ead1f7 10211
f8a968bc
JS
10212 // we can only write to dereferenced fields, and only if guru mode is on
10213 bool lvalue = is_active_lvalue(e);
10214 if (lvalue && (!dw.sess.guru_mode || e->components.empty()))
dc09353a 10215 throw SEMANTIC_ERROR(_F("write to tracepoint variable '%s' not permitted; need stap -g", e->name.c_str()), e->tok);
c69a87e0 10216
ad370dcc
JS
10217 // XXX: if a struct/union arg is passed by value, then writing to its fields
10218 // is also meaningless until you dereference past a pointer member. It's
10219 // harder to detect and prevent that though...
75ead1f7 10220
f8a968bc
JS
10221 if (e->components.empty())
10222 {
03c75a4a 10223 if (e->addressof)
dc09353a 10224 throw SEMANTIC_ERROR(_("cannot take address of tracepoint variable"), e->tok);
a45664f4 10225
3e3bd7b6 10226 // Just grab the value from the probe locals
a45664f4
JS
10227 symbol* sym = new symbol;
10228 sym->tok = e->tok;
10229 sym->name = "__tracepoint_arg_" + arg->name;
6ee19a25 10230 sym->type_details.reset(new exp_type_dwarf(&dw, &arg->type_die, false, false));
a45664f4 10231 provide (sym);
f8a968bc
JS
10232 }
10233 else
10234 {
5f36109e
JS
10235 // make a copy of the original as a bare target symbol for the tracepoint
10236 // value, which will be passed into the dwarf dereferencing code
10237 target_symbol* e2 = deep_copy_visitor::deep_copy(e);
10238 e2->components.clear();
10239
c4965ad9 10240 if (e->check_pretty_print (lvalue))
5f36109e 10241 {
37efef73 10242 dwarf_pretty_print dpp(dw, &arg->type_die, e2, deref_p, false, *e);
5f36109e
JS
10243 dpp.expand()->visit (this);
10244 return;
10245 }
10246
1c0be8c7 10247 bool userspace_p = false;
f8a968bc 10248 string fname = (string(lvalue ? "_tracepoint_tvar_set" : "_tracepoint_tvar_get")
cc9001af 10249 + "_" + e->sym_name()
aca66a36 10250 + "_" + lex_cast(tick++));
75ead1f7 10251
40a393cd
JS
10252 Dwarf_Die endtype;
10253 string code = dw.literal_stmt_for_pointer (&arg->type_die, e, lvalue, &endtype);
6fda2dff 10254
40a393cd 10255 functioncall* n = synthetic_embedded_deref_call(dw, &endtype, fname, code,
1c0be8c7 10256 userspace_p, lvalue, e, e2);
75ead1f7 10257
f8a968bc 10258 if (lvalue)
f55efafe 10259 provide_lvalue_call (n);
75ead1f7 10260
1c0be8c7
JS
10261 // Revisit the functioncall so arguments can be expanded.
10262 n->visit (this);
f8a968bc 10263 }
75ead1f7
JS
10264}
10265
10266
f8a968bc
JS
10267void
10268tracepoint_var_expanding_visitor::visit_target_symbol_context (target_symbol* e)
0a6f5a3f 10269{
03c75a4a 10270 if (e->addressof)
dc09353a 10271 throw SEMANTIC_ERROR(_("cannot take address of context variable"), e->tok);
03c75a4a 10272
f8a968bc 10273 if (is_active_lvalue (e))
dc09353a 10274 throw SEMANTIC_ERROR(_F("write to tracepoint '%s' not permitted", e->name.c_str()), e->tok);
0a6f5a3f 10275
277c21bc 10276 if (e->name == "$$name")
f8a968bc 10277 {
5f36109e
JS
10278 e->assert_no_components("tracepoint");
10279
bfdaad1e
DS
10280 // Synthesize an embedded expression.
10281 embedded_expr *expr = new embedded_expr;
10282 expr->tok = e->tok;
10283 expr->code = string("/* string */ /* pure */ ")
6dceb5c9 10284 + string("c->ips.tracepoint_name ? c->ips.tracepoint_name : \"\"");
bfdaad1e 10285 provide (expr);
f8a968bc 10286 }
277c21bc 10287 else if (e->name == "$$vars" || e->name == "$$parms")
f8a968bc 10288 {
5f36109e
JS
10289 e->assert_no_components("tracepoint", true);
10290
1c922ad7 10291 print_format* pf = print_format::create(e->tok, "sprintf");
0a6f5a3f 10292
f8a968bc 10293 for (unsigned i = 0; i < args.size(); ++i)
b278033a 10294 {
dcaa1a65
JS
10295 if (!args[i].usable)
10296 continue;
f8a968bc
JS
10297 if (i > 0)
10298 pf->raw_components += " ";
10299 pf->raw_components += args[i].name;
3e3bd7b6 10300 target_symbol *tsym = new target_symbol;
f8a968bc 10301 tsym->tok = e->tok;
277c21bc 10302 tsym->name = "$" + args[i].name;
5f36109e 10303 tsym->components = e->components;
b278033a 10304
f8a968bc
JS
10305 // every variable should always be accessible!
10306 tsym->saved_conversion_error = 0;
8c2f50c0 10307 expression *texp = require<expression> (tsym); // NB: throws nothing ...
14a97852
JS
10308 if (tsym->saved_conversion_error) // ... but this is how we know it happened.
10309 {
10310 if (dw.sess.verbose>2)
e26c2f83 10311 for (const semantic_error *c = tsym->saved_conversion_error;
c081af73 10312 c != 0; c = c->get_chain())
4c5d9906 10313 clog << _("variable location problem [man error::dwarf]: ") << c->what() << endl;
14a97852
JS
10314 pf->raw_components += "=?";
10315 continue;
10316 }
b278033a 10317
c4965ad9 10318 if (e->check_pretty_print ())
5f36109e
JS
10319 pf->raw_components += "=%s";
10320 else
10321 pf->raw_components += args[i].isptr ? "=%p" : "=%#x";
f8a968bc
JS
10322 pf->args.push_back(texp);
10323 }
0a6f5a3f 10324
f8a968bc
JS
10325 pf->components = print_format::string_to_components(pf->raw_components);
10326 provide (pf);
b278033a 10327 }
f8a968bc
JS
10328 else
10329 assert(0); // shouldn't get here
0a6f5a3f
JS
10330}
10331
0a6f5a3f 10332void
f8a968bc 10333tracepoint_var_expanding_visitor::visit_target_symbol (target_symbol* e)
0a6f5a3f 10334{
aff5d390 10335 try
c69a87e0 10336 {
bd1fcbad 10337 assert(e->name.size() > 0 && e->name[0] == '$');
aff5d390 10338
277c21bc 10339 if (e->name == "$$name" || e->name == "$$parms" || e->name == "$$vars")
c69a87e0 10340 visit_target_symbol_context (e);
bd1fcbad 10341
c69a87e0
FCE
10342 else
10343 visit_target_symbol_arg (e);
10344 }
10345 catch (const semantic_error &er)
10346 {
1af1e62d 10347 e->chain (er);
c69a87e0
FCE
10348 provide (e);
10349 }
0a6f5a3f
JS
10350}
10351
10352
79189b84
JS
10353tracepoint_derived_probe::tracepoint_derived_probe (systemtap_session& s,
10354 dwflpp& dw, Dwarf_Die& func_die,
10355 const string& tracepoint_name,
10356 probe* base, probe_point* loc):
4c5d1300 10357 derived_probe (base, loc, true /* .components soon rewritten */),
79189b84 10358 sess (s), tracepoint_name (tracepoint_name)
56894e91 10359{
79189b84
JS
10360 // create synthetic probe point name; preserve condition
10361 vector<probe_point::component*> comps;
10362 comps.push_back (new probe_point::component (TOK_KERNEL));
10363 comps.push_back (new probe_point::component (TOK_TRACE, new literal_string (tracepoint_name)));
10364 this->sole_location()->components = comps;
10365
6fb70fb7
JS
10366 // fill out the available arguments in this tracepoint
10367 build_args(dw, func_die);
56894e91 10368
6fb70fb7
JS
10369 // determine which header defined this tracepoint
10370 string decl_file = dwarf_decl_file(&func_die);
b78a0fbb 10371 header = decl_file;
d4393459
FCE
10372
10373#if 0 /* This convention is not enforced. */
6fb70fb7
JS
10374 size_t header_pos = decl_file.rfind("trace/");
10375 if (header_pos == string::npos)
dc09353a 10376 throw SEMANTIC_ERROR ("cannot parse header location for tracepoint '"
6fb70fb7
JS
10377 + tracepoint_name + "' in '"
10378 + decl_file + "'");
10379 header = decl_file.substr(header_pos);
d4393459 10380#endif
56894e91 10381
6fb70fb7
JS
10382 // tracepoints from FOO_event_types.h should really be included from FOO.h
10383 // XXX can dwarf tell us the include hierarchy? it would be better to
10384 // ... walk up to see which one was directly included by tracequery.c
3c1b3d06 10385 // XXX: see also PR9993.
d4393459 10386 size_t header_pos = header.find("_event_types");
6fb70fb7
JS
10387 if (header_pos != string::npos)
10388 header.erase(header_pos, 12);
56894e91 10389
f8a968bc
JS
10390 // Now expand the local variables in the probe body
10391 tracepoint_var_expanding_visitor v (dw, name, args);
8b095b45 10392 v.replace (this->body);
a45664f4
JS
10393 for (unsigned i = 0; i < args.size(); i++)
10394 if (args[i].used)
10395 {
10396 vardecl* v = new vardecl;
10397 v->name = "__tracepoint_arg_" + args[i].name;
10398 v->tok = this->tok;
58701b78 10399 v->set_arity(0, this->tok);
a45664f4 10400 v->type = pe_long;
69aa668e 10401 v->synthetic = true;
a45664f4
JS
10402 this->locals.push_back (v);
10403 }
56894e91 10404
79189b84 10405 if (sess.verbose > 2)
ce0f6648 10406 clog << "tracepoint-based " << name << " tracepoint='" << tracepoint_name << "'" << endl;
79189b84 10407}
dc38c0ae 10408
56894e91 10409
f8a968bc 10410static bool
37efef73 10411resolve_pointer_type(Dwarf_Die& die, bool& isptr)
46b84a80 10412{
611a848e
JS
10413 if (null_die(&die))
10414 {
10415 isptr = true;
10416 return true;
10417 }
10418
d19a9a82 10419 Dwarf_Die type;
37efef73 10420 switch (dwarf_tag(&die))
b20febf3 10421 {
f8a968bc
JS
10422 case DW_TAG_typedef:
10423 case DW_TAG_const_type:
10424 case DW_TAG_volatile_type:
c69af88f 10425 case DW_TAG_restrict_type:
f8a968bc 10426 // iterate on the referent type
37efef73
JS
10427 return (dwarf_attr_die(&die, DW_AT_type, &die)
10428 && resolve_pointer_type(die, isptr));
10429
f8a968bc 10430 case DW_TAG_base_type:
a52d2ac0 10431 case DW_TAG_enumeration_type:
37efef73
JS
10432 case DW_TAG_structure_type:
10433 case DW_TAG_union_type:
f8a968bc 10434 // base types will simply be treated as script longs
37efef73
JS
10435 // structs/unions must be referenced by pointer elsewhere
10436 isptr = false;
f8a968bc 10437 return true;
37efef73
JS
10438
10439 case DW_TAG_array_type:
f8a968bc 10440 case DW_TAG_pointer_type:
37efef73
JS
10441 case DW_TAG_reference_type:
10442 case DW_TAG_rvalue_reference_type:
10443 // pointer-like types can be treated as script longs,
dcaa1a65 10444 // and if we know their type, they can also be dereferenced
37efef73
JS
10445 isptr = true;
10446 type = die;
10447 while (dwarf_attr_die(&type, DW_AT_type, &type))
d19a9a82
JS
10448 {
10449 // It still might be a non-type, e.g. const void,
10450 // so we need to strip away all qualifiers.
37efef73 10451 int tag = dwarf_tag(&type);
d19a9a82
JS
10452 if (tag != DW_TAG_typedef &&
10453 tag != DW_TAG_const_type &&
c69af88f
MW
10454 tag != DW_TAG_volatile_type &&
10455 tag != DW_TAG_restrict_type)
d19a9a82 10456 {
37efef73
JS
10457 die = type;
10458 return true;
d19a9a82
JS
10459 }
10460 }
37efef73
JS
10461 // otherwise use a null_die to indicate void
10462 std::memset(&die, 0, sizeof(die));
ad370dcc 10463 return true;
37efef73
JS
10464
10465 default:
10466 // should we consider other types too?
10467 return false;
10468 }
10469}
10470
10471
10472static bool
10473resolve_tracepoint_arg_type(tracepoint_arg& arg)
10474{
10475 if (!resolve_pointer_type(arg.type_die, arg.isptr))
10476 return false;
10477
10478 if (arg.isptr)
10479 arg.typecast = "(intptr_t)";
10480 else if (dwarf_tag(&arg.type_die) == DW_TAG_structure_type ||
10481 dwarf_tag(&arg.type_die) == DW_TAG_union_type)
10482 {
ad370dcc
JS
10483 // for structs/unions which are passed by value, we turn it into
10484 // a pointer that can be dereferenced.
10485 arg.isptr = true;
10486 arg.typecast = "(intptr_t)&";
b20febf3 10487 }
37efef73 10488 return true;
56894e91
JS
10489}
10490
10491
10492void
822a6a3d 10493tracepoint_derived_probe::build_args(dwflpp&, Dwarf_Die& func_die)
56894e91 10494{
6fb70fb7
JS
10495 Dwarf_Die arg;
10496 if (dwarf_child(&func_die, &arg) == 0)
10497 do
10498 if (dwarf_tag(&arg) == DW_TAG_formal_parameter)
10499 {
10500 // build a tracepoint_arg for this parameter
10501 tracepoint_arg tparg;
c60517ca 10502 tparg.name = dwarf_diename(&arg) ?: "";
56894e91 10503
6fb70fb7 10504 // read the type of this parameter
3d1ad340 10505 if (!dwarf_attr_die (&arg, DW_AT_type, &tparg.type_die)
f1c8f8a5 10506 || !dwarf_type_name(&tparg.type_die, tparg.c_type))
dc09353a 10507 throw SEMANTIC_ERROR (_F("cannot get type of parameter '%s' of tracepoint '%s'",
ce0f6648 10508 tparg.name.c_str(), tracepoint_name.c_str()));
a68f81a2 10509
dcaa1a65 10510 tparg.usable = resolve_tracepoint_arg_type(tparg);
6fb70fb7
JS
10511 args.push_back(tparg);
10512 if (sess.verbose > 4)
a52d2ac0
JS
10513 clog << _F("found parameter for tracepoint '%s': type:'%s' name:'%s' %s",
10514 tracepoint_name.c_str(), tparg.c_type.c_str(), tparg.name.c_str(),
10515 tparg.usable ? "ok" : "unavailable") << endl;
6fb70fb7
JS
10516 }
10517 while (dwarf_siblingof(&arg, &arg) == 0);
56894e91
JS
10518}
10519
dc38c0ae 10520void
d0bfd2ac 10521tracepoint_derived_probe::getargs(std::list<std::string> &arg_set) const
dc38c0ae 10522{
dcaa1a65
JS
10523 for (unsigned i = 0; i < args.size(); ++i)
10524 if (args[i].usable)
d0bfd2ac 10525 arg_set.push_back("$"+args[i].name+":"+args[i].c_type);
dc38c0ae
DS
10526}
10527
79189b84
JS
10528void
10529tracepoint_derived_probe::join_group (systemtap_session& s)
197a4d62 10530{
79189b84
JS
10531 if (! s.tracepoint_derived_probes)
10532 s.tracepoint_derived_probes = new tracepoint_derived_probe_group ();
10533 s.tracepoint_derived_probes->enroll (this);
ca6d3b0f 10534 this->group = s.tracepoint_derived_probes;
79189b84 10535}
e38d6504 10536
56894e91 10537
197a4d62 10538void
3e3bd7b6 10539tracepoint_derived_probe::print_dupe_stamp(ostream& o)
56894e91 10540{
3e3bd7b6
JS
10541 for (unsigned i = 0; i < args.size(); i++)
10542 if (args[i].used)
10543 o << "__tracepoint_arg_" << args[i].name << endl;
197a4d62 10544}
56894e91 10545
3e3bd7b6 10546
c9ccb642 10547static vector<string> tracepoint_extra_decls (systemtap_session& s, const string& header)
47dd066d 10548{
3c1b3d06
FCE
10549 vector<string> they_live;
10550 // PR 9993
10551 // XXX: may need this to be configurable
d4393459 10552 they_live.push_back ("#include <linux/skbuff.h>");
9e0cd21a
FCE
10553
10554 // PR11649: conditional extra header
10555 // for kvm tracepoints in 2.6.33ish
10556 if (s.kernel_config["CONFIG_KVM"] != string("")) {
d4393459
FCE
10557 they_live.push_back ("#include <linux/kvm_host.h>");
10558 }
10559
50b72692 10560 if (header.find("xfs") != string::npos && s.kernel_config["CONFIG_XFS_FS"] != string("")) {
d4393459 10561 they_live.push_back ("#define XFS_BIG_BLKNOS 1");
88637c31
FCE
10562 if (s.kernel_source_tree != "")
10563 they_live.push_back ("#include \"fs/xfs/xfs_types.h\""); // in kernel-source tree
d4393459
FCE
10564 they_live.push_back ("struct xfs_mount;");
10565 they_live.push_back ("struct xfs_inode;");
10566 they_live.push_back ("struct xfs_buf;");
10567 they_live.push_back ("struct xfs_bmbt_irec;");
c2cf1b87 10568 they_live.push_back ("struct xfs_trans;");
9e0cd21a 10569 }
d4393459 10570
50b72692 10571 if (header.find("nfs") != string::npos && s.kernel_config["CONFIG_NFSD"] != string("")) {
d4393459
FCE
10572 they_live.push_back ("struct rpc_task;");
10573 }
b64d65e2
FCE
10574 // RHEL6.3
10575 if (header.find("rpc") != string::npos && s.kernel_config["CONFIG_NFSD"] != string("")) {
10576 they_live.push_back ("struct rpc_clnt;");
10577 they_live.push_back ("struct rpc_wait_queue;");
10578 }
d4393459
FCE
10579
10580 they_live.push_back ("#include <asm/cputime.h>");
10581
c2cf1b87
FCE
10582 // linux 3.0
10583 they_live.push_back ("struct cpu_workqueue_struct;");
10584
50b72692 10585 if (header.find("ext4") != string::npos && s.kernel_config["CONFIG_EXT4_FS"] != string(""))
c2cf1b87
FCE
10586 if (s.kernel_source_tree != "")
10587 they_live.push_back ("#include \"fs/ext4/ext4.h\""); // in kernel-source tree
10588
d97d428e 10589 if (header.find("ext3") != string::npos)
50b72692
HP
10590 they_live.push_back ("struct ext3_reserve_window_node;");
10591
d97d428e
FCE
10592 if (header.find("workqueue") != string::npos)
10593 {
10594 they_live.push_back ("struct pool_workqueue;");
10595 they_live.push_back ("struct work_struct;");
10596 }
10597
10598 if (header.find("asoc") != string::npos)
10599 they_live.push_back ("struct snd_soc_dapm_path;");
10600
10601 if (header.find("9p") != string::npos)
10602 {
10603 they_live.push_back ("struct p9_client;");
10604 they_live.push_back ("struct p9_fcall;");
10605 }
10606
10607 if (header.find("bcache") != string::npos)
10608 {
10609 they_live.push_back ("struct bkey;");
10610 they_live.push_back ("struct btree;");
10611 they_live.push_back ("struct cache_set;");
10612 they_live.push_back ("struct cache;");
10613 }
10614
10615 if (header.find("f2fs") != string::npos)
10616 {
10617 // cannot get fs/f2fs/f2fs.h #included
10618 they_live.push_back ("typedef u32 block_t;");
10619 they_live.push_back ("typedef u32 nid_t;");
10620 }
10621
10622 if (header.find("radeon") != string::npos)
10623 they_live.push_back ("struct radeon_bo;");
10624
10625 // argh, 3.11, i915_trace.h -> i915_drv.h -> i915_reg.h without -I
10626 // also brcms_trace_events.h -> ... -> "types.h"
10627 // XXX: need a way to add a temporary -I flag
10628
10629 if (header.find("/ath/") != string::npos)
10630 they_live.push_back ("struct ath5k_hw;");
10631
10632
3c1b3d06
FCE
10633 return they_live;
10634}
47dd066d
WC
10635
10636
10637void
79189b84 10638tracepoint_derived_probe_group::emit_module_decls (systemtap_session& s)
47dd066d 10639{
79189b84
JS
10640 if (probes.empty())
10641 return;
47dd066d 10642
96b030fe 10643 s.op->newline() << "/* ---- tracepoint probes ---- */";
3ef9830a 10644 s.op->newline() << "#include <linux/stp_tracepoint.h>" << endl;
96b030fe 10645 s.op->newline();
79189b84 10646
47dd066d 10647
a4b9c3b3
FCE
10648 // We create a MODULE_aux_N.c file for each tracepoint header, to allow them
10649 // to be separately compiled. That's because kernel tracepoint headers sometimes
10650 // conflict. PR13155.
10651
10652 map<string,translator_output*> per_header_aux;
10653 // GC NB: the translator_output* structs are owned/retained by the systemtap_session.
47dd066d 10654
6fb70fb7
JS
10655 for (unsigned i = 0; i < probes.size(); ++i)
10656 {
10657 tracepoint_derived_probe *p = probes[i];
75ae2ec9 10658 string header = p->header;
5f73a260 10659
a4b9c3b3
FCE
10660 // We cache the auxiliary output files on a per-header basis. We don't
10661 // need one aux file per tracepoint, only one per tracepoint-header.
10662 translator_output *tpop = per_header_aux[header];
10663 if (tpop == 0)
10664 {
10665 tpop = s.op_create_auxiliary();
10666 per_header_aux[header] = tpop;
10667
10668 // PR9993: Add extra headers to work around undeclared types in individual
10669 // include/trace/foo.h files
10670 const vector<string>& extra_decls = tracepoint_extra_decls (s, header);
10671 for (unsigned z=0; z<extra_decls.size(); z++)
10672 tpop->newline() << extra_decls[z] << "\n";
720c435f 10673
a4b9c3b3
FCE
10674 // strip include/ substring, the same way as done in get_tracequery_module()
10675 size_t root_pos = header.rfind("include/");
10676 header = ((root_pos != string::npos) ? header.substr(root_pos + 8) : header);
bfffa443 10677
3ef9830a 10678 tpop->newline() << "#include <linux/stp_tracepoint.h>" << endl;
a4b9c3b3 10679 tpop->newline() << "#include <" << header << ">";
a4b9c3b3
FCE
10680 }
10681
720c435f
JS
10682 // collect the args that are actually in use
10683 vector<const tracepoint_arg*> used_args;
6fb70fb7 10684 for (unsigned j = 0; j < p->args.size(); ++j)
720c435f
JS
10685 if (p->args[j].used)
10686 used_args.push_back(&p->args[j]);
10687
3ef9830a
JS
10688 // forward-declare the generated-side tracepoint callback, and define the
10689 // generated-side tracepoint callback in the main translator-output
10690 string enter_real_fn = "enter_real_tracepoint_probe_" + lex_cast(i);
720c435f 10691 if (used_args.empty())
6fb70fb7 10692 {
3ef9830a
JS
10693 tpop->newline() << "STP_TRACE_ENTER_REAL_NOARGS(" << enter_real_fn << ");";
10694 s.op->newline() << "STP_TRACE_ENTER_REAL_NOARGS(" << enter_real_fn << ")";
6fb70fb7 10695 }
3ef9830a 10696 else
a4b9c3b3 10697 {
3ef9830a
JS
10698 tpop->newline() << "STP_TRACE_ENTER_REAL(" << enter_real_fn;
10699 s.op->newline() << "STP_TRACE_ENTER_REAL(" << enter_real_fn;
10700 s.op->indent(2);
10701 for (unsigned j = 0; j < used_args.size(); ++j)
10702 {
10703 tpop->line() << ", int64_t";
10704 s.op->newline() << ", int64_t __tracepoint_arg_" << used_args[j]->name;
10705 }
10706 tpop->line() << ");";
10707 s.op->newline() << ")";
10708 s.op->indent(-2);
6fb70fb7 10709 }
3ef9830a 10710 s.op->newline() << "{";
7c3e97f4 10711 s.op->newline(1) << "const struct stap_probe * const probe = "
faea5e16 10712 << common_probe_init (p) << ";";
71db462b 10713 common_probe_entryfn_prologue (s, "STAP_SESSION_RUNNING", "probe",
cda141c2 10714 "stp_probe_type_tracepoint");
6dceb5c9 10715 s.op->newline() << "c->ips.tracepoint_name = "
c12d974f
FCE
10716 << lex_cast_qstring (p->tracepoint_name)
10717 << ";";
720c435f
JS
10718 for (unsigned j = 0; j < used_args.size(); ++j)
10719 {
10720 s.op->newline() << "c->probe_locals." << p->name
0386bbcf 10721 << "." + s.up->c_localname("__tracepoint_arg_" + used_args[j]->name)
720c435f
JS
10722 << " = __tracepoint_arg_" << used_args[j]->name << ";";
10723 }
26e63673 10724 s.op->newline() << "(*probe->ph) (c);";
ef1337ee 10725 common_probe_entryfn_epilogue (s, true, otf_safe_context(s));
6fb70fb7 10726 s.op->newline(-1) << "}";
47dd066d 10727
a4b9c3b3 10728 // define the real tracepoint callback function
3ef9830a
JS
10729 string enter_fn = "enter_tracepoint_probe_" + lex_cast(i);
10730 if (p->args.empty())
10731 tpop->newline() << "static STP_TRACE_ENTER_NOARGS(" << enter_fn << ")";
10732 else
a4b9c3b3 10733 {
3ef9830a
JS
10734 tpop->newline() << "static STP_TRACE_ENTER(" << enter_fn;
10735 s.op->indent(2);
10736 for (unsigned j = 0; j < p->args.size(); ++j)
10737 {
10738 tpop->newline() << ", " << p->args[j].c_type
10739 << " __tracepoint_arg_" << p->args[j].name;
10740 }
10741 tpop->newline() << ")";
10742 s.op->indent(-2);
a4b9c3b3 10743 }
3ef9830a
JS
10744 tpop->newline() << "{";
10745 tpop->newline(1) << enter_real_fn << "(";
720c435f
JS
10746 tpop->indent(2);
10747 for (unsigned j = 0; j < used_args.size(); ++j)
10748 {
10749 if (j > 0)
10750 tpop->line() << ", ";
10751 tpop->newline() << "(int64_t)" << used_args[j]->typecast
10752 << "__tracepoint_arg_" << used_args[j]->name;
10753 }
10754 tpop->newline() << ");";
10755 tpop->newline(-3) << "}";
a4b9c3b3
FCE
10756
10757
96b030fe 10758 // emit normalized registration functions
720c435f 10759 tpop->newline() << "int register_tracepoint_probe_" << i << "(void) {";
3ef9830a
JS
10760 tpop->newline(1) << "return STP_TRACE_REGISTER(" << p->tracepoint_name
10761 << ", " << enter_fn << ");";
a4b9c3b3 10762 tpop->newline(-1) << "}";
47dd066d 10763
86758d5f
JS
10764 // NB: we're not prepared to deal with unreg failures. However, failures
10765 // can only occur if the tracepoint doesn't exist (yet?), or if we
10766 // weren't even registered. The former should be OKed by the initial
10767 // registration call, and the latter is safe to ignore.
720c435f 10768 tpop->newline() << "void unregister_tracepoint_probe_" << i << "(void) {";
3ef9830a
JS
10769 tpop->newline(1) << "(void) STP_TRACE_UNREGISTER(" << p->tracepoint_name
10770 << ", " << enter_fn << ");";
a4b9c3b3
FCE
10771 tpop->newline(-1) << "}";
10772 tpop->newline();
5f73a260 10773
720c435f
JS
10774 // declare normalized registration functions
10775 s.op->newline() << "int register_tracepoint_probe_" << i << "(void);";
10776 s.op->newline() << "void unregister_tracepoint_probe_" << i << "(void);";
5f73a260 10777
a4b9c3b3 10778 tpop->assert_0_indent();
af304783
DS
10779 }
10780
96b030fe
JS
10781 // emit an array of registration functions for easy init/shutdown
10782 s.op->newline() << "static struct stap_tracepoint_probe {";
10783 s.op->newline(1) << "int (*reg)(void);";
86758d5f 10784 s.op->newline(0) << "void (*unreg)(void);";
96b030fe
JS
10785 s.op->newline(-1) << "} stap_tracepoint_probes[] = {";
10786 s.op->indent(1);
10787 for (unsigned i = 0; i < probes.size(); ++i)
10788 {
10789 s.op->newline () << "{";
10790 s.op->line() << " .reg=&register_tracepoint_probe_" << i << ",";
10791 s.op->line() << " .unreg=&unregister_tracepoint_probe_" << i;
10792 s.op->line() << " },";
10793 }
10794 s.op->newline(-1) << "};";
10795 s.op->newline();
47dd066d
WC
10796}
10797
10798
79189b84
JS
10799void
10800tracepoint_derived_probe_group::emit_module_init (systemtap_session &s)
47dd066d 10801{
79189b84
JS
10802 if (probes.size () == 0)
10803 return;
47dd066d 10804
79189b84 10805 s.op->newline() << "/* init tracepoint probes */";
96b030fe
JS
10806 s.op->newline() << "for (i=0; i<" << probes.size() << "; i++) {";
10807 s.op->newline(1) << "rc = stap_tracepoint_probes[i].reg();";
10808 s.op->newline() << "if (rc) {";
10809 s.op->newline(1) << "for (j=i-1; j>=0; j--)"; // partial rollback
10810 s.op->newline(1) << "stap_tracepoint_probes[j].unreg();";
10811 s.op->newline(-1) << "break;"; // don't attempt to register any more probes
10812 s.op->newline(-1) << "}";
10813 s.op->newline(-1) << "}";
47dd066d 10814
bc9a523d
FCE
10815 // This would be technically proper (on those autoconf-detectable
10816 // kernels that include this function in tracepoint.h), however we
10817 // already make several calls to synchronze_sched() during our
10818 // shutdown processes.
47dd066d 10819
bc9a523d
FCE
10820 // s.op->newline() << "if (rc)";
10821 // s.op->newline(1) << "tracepoint_synchronize_unregister();";
10822 // s.op->indent(-1);
79189b84 10823}
47dd066d
WC
10824
10825
79189b84
JS
10826void
10827tracepoint_derived_probe_group::emit_module_exit (systemtap_session& s)
47dd066d 10828{
79189b84
JS
10829 if (probes.empty())
10830 return;
47dd066d 10831
96b030fe
JS
10832 s.op->newline() << "/* deregister tracepoint probes */";
10833 s.op->newline() << "for (i=0; i<" << probes.size() << "; i++)";
10834 s.op->newline(1) << "stap_tracepoint_probes[i].unreg();";
10835 s.op->indent(-1);
47dd066d 10836
bc9a523d 10837 // Not necessary: see above.
47dd066d 10838
bc9a523d 10839 // s.op->newline() << "tracepoint_synchronize_unregister();";
79189b84 10840}
b20febf3 10841
47dd066d 10842
75ead1f7 10843struct tracepoint_query : public base_query
47dd066d 10844{
75ead1f7
JS
10845 tracepoint_query(dwflpp & dw, const string & tracepoint,
10846 probe * base_probe, probe_point * base_loc,
10847 vector<derived_probe *> & results):
10848 base_query(dw, "*"), tracepoint(tracepoint),
10849 base_probe(base_probe), base_loc(base_loc),
10850 results(results) {}
47dd066d 10851
75ead1f7 10852 const string& tracepoint;
47dd066d 10853
75ead1f7
JS
10854 probe * base_probe;
10855 probe_point * base_loc;
10856 vector<derived_probe *> & results;
f982c59b 10857 set<string> probed_names;
47dd066d 10858
75ead1f7
JS
10859 void handle_query_module();
10860 int handle_query_cu(Dwarf_Die * cudie);
10861 int handle_query_func(Dwarf_Die * func);
822a6a3d 10862 void query_library (const char *) {}
576eaefe 10863 void query_plt (const char *entry, size_t addr) {}
b20febf3 10864
5c378838 10865 static int tracepoint_query_cu (Dwarf_Die * cudie, tracepoint_query * q);
7d007451 10866 static int tracepoint_query_func (Dwarf_Die * func, tracepoint_query * q);
75ead1f7 10867};
47dd066d
WC
10868
10869
10870void
75ead1f7 10871tracepoint_query::handle_query_module()
47dd066d 10872{
75ead1f7 10873 // look for the tracepoints in each CU
337b7c44 10874 dw.iterate_over_cus(tracepoint_query_cu, this, false);
47dd066d
WC
10875}
10876
10877
75ead1f7
JS
10878int
10879tracepoint_query::handle_query_cu(Dwarf_Die * cudie)
47dd066d 10880{
75ead1f7 10881 dw.focus_on_cu (cudie);
5f52fafe 10882 dw.mod_info->get_symtab();
47dd066d 10883
75ead1f7
JS
10884 // look at each function to see if it's a tracepoint
10885 string function = "stapprobe_" + tracepoint;
10886 return dw.iterate_over_functions (tracepoint_query_func, this, function);
47dd066d
WC
10887}
10888
10889
75ead1f7
JS
10890int
10891tracepoint_query::handle_query_func(Dwarf_Die * func)
47dd066d 10892{
75ead1f7 10893 dw.focus_on_function (func);
47dd066d 10894
60d98537 10895 assert(startswith(dw.function_name, "stapprobe_"));
75ead1f7 10896 string tracepoint_instance = dw.function_name.substr(10);
f982c59b
JS
10897
10898 // check for duplicates -- sometimes tracepoint headers may be indirectly
10899 // included in more than one of our tracequery modules.
10900 if (!probed_names.insert(tracepoint_instance).second)
10901 return DWARF_CB_OK;
10902
c4a04833
FCE
10903 // PR17126: blacklist
10904 if (!sess.guru_mode)
10905 {
10906 if ((sess.architecture.substr(0,3) == "ppc" ||
10907 sess.architecture.substr(0,7) == "powerpc") &&
10908 (tracepoint_instance == "hcall_entry" ||
10909 tracepoint_instance == "hcall_exit"))
10910 {
10911 sess.print_warning(_F("tracepoint %s is blacklisted on architecture %s",
10912 tracepoint_instance.c_str(), sess.architecture.c_str()));
10913 return DWARF_CB_OK;
10914 }
10915 }
10916
79189b84
JS
10917 derived_probe *dp = new tracepoint_derived_probe (dw.sess, dw, *func,
10918 tracepoint_instance,
10919 base_probe, base_loc);
10920 results.push_back (dp);
75ead1f7 10921 return DWARF_CB_OK;
47dd066d
WC
10922}
10923
10924
75ead1f7 10925int
5c378838 10926tracepoint_query::tracepoint_query_cu (Dwarf_Die * cudie, tracepoint_query * q)
47dd066d 10927{
85007c04 10928 if (pending_interrupts) return DWARF_CB_ABORT;
75ead1f7 10929 return q->handle_query_cu(cudie);
47dd066d
WC
10930}
10931
10932
75ead1f7 10933int
7d007451 10934tracepoint_query::tracepoint_query_func (Dwarf_Die * func, tracepoint_query * q)
47dd066d 10935{
85007c04 10936 if (pending_interrupts) return DWARF_CB_ABORT;
75ead1f7 10937 return q->handle_query_func(func);
47dd066d
WC
10938}
10939
10940
0a6f5a3f 10941struct tracepoint_builder: public derived_probe_builder
47dd066d 10942{
0a6f5a3f
JS
10943private:
10944 dwflpp *dw;
10945 bool init_dw(systemtap_session& s);
c9ccb642
FCE
10946 void get_tracequery_modules(systemtap_session& s,
10947 const vector<string>& headers,
10948 vector<string>& modules);
47dd066d 10949
0a6f5a3f 10950public:
47dd066d 10951
0a6f5a3f
JS
10952 tracepoint_builder(): dw(0) {}
10953 ~tracepoint_builder() { delete dw; }
47dd066d 10954
0a6f5a3f
JS
10955 void build_no_more (systemtap_session& s)
10956 {
10957 if (dw && s.verbose > 3)
b530b5b3 10958 clog << _("tracepoint_builder releasing dwflpp") << endl;
0a6f5a3f
JS
10959 delete dw;
10960 dw = NULL;
435f53a7
FCE
10961
10962 delete_session_module_cache (s);
0a6f5a3f 10963 }
47dd066d 10964
0a6f5a3f
JS
10965 void build(systemtap_session& s,
10966 probe *base, probe_point *location,
10967 literal_map_t const& parameters,
10968 vector<derived_probe*>& finished_results);
10969};
47dd066d 10970
47dd066d 10971
c9ccb642 10972
2a0e62a8 10973// Create (or cache) one or more tracequery .o modules, based upon the
c9ccb642
FCE
10974// tracepoint-related header files given. Return the generated or cached
10975// modules[].
10976
10977void
10978tracepoint_builder::get_tracequery_modules(systemtap_session& s,
10979 const vector<string>& headers,
10980 vector<string>& modules)
0a6f5a3f 10981{
c95eddf7 10982 if (s.verbose > 2)
55e50c24 10983 {
ce0f6648 10984 clog << _F("Pass 2: getting a tracepoint query for %zu headers: ", headers.size()) << endl;
55e50c24
JS
10985 for (size_t i = 0; i < headers.size(); ++i)
10986 clog << " " << headers[i] << endl;
10987 }
c95eddf7 10988
2a0e62a8
JS
10989 map<string,string> headers_cache_obj; // header name -> cache/.../tracequery_hash.o file name
10990 // Map the headers to cache .o names. Note that this has side-effects of
c9ccb642
FCE
10991 // creating the $SYSTEMTAP_DIR/.cache/XX/... directory and the hash-log file,
10992 // so we prefer not to repeat this.
10993 vector<string> uncached_headers;
10994 for (size_t i=0; i<headers.size(); i++)
2a0e62a8 10995 headers_cache_obj[headers[i]] = find_tracequery_hash(s, headers[i]);
c9ccb642
FCE
10996
10997 // They may be in the cache already.
10998 if (s.use_cache && !s.poison_cache)
10999 for (size_t i=0; i<headers.size(); i++)
11000 {
11001 // see if the cached module exists
2a0e62a8 11002 const string& tracequery_path = headers_cache_obj[headers[i]];
c9ccb642
FCE
11003 if (!tracequery_path.empty() && file_exists(tracequery_path))
11004 {
11005 if (s.verbose > 2)
11006 clog << _F("Pass 2: using cached %s", tracequery_path.c_str()) << endl;
47dd066d 11007
c252fca2
JS
11008 // an empty file is a cached failure
11009 if (get_file_size(tracequery_path) > 0)
11010 modules.push_back (tracequery_path);
c9ccb642
FCE
11011 }
11012 else
11013 uncached_headers.push_back(headers[i]);
11014 }
11015 else
11016 uncached_headers = headers;
f982c59b 11017
c9ccb642
FCE
11018 // If we have nothing left to search for, quit
11019 if (uncached_headers.empty()) return;
55e50c24 11020
c9ccb642 11021 map<string,string> headers_tracequery_src; // header -> C-source code mapping
55e50c24 11022
c9ccb642
FCE
11023 // We could query several subsets of headers[] to make this go
11024 // faster, but let's KISS and do one at a time.
11025 for (size_t i=0; i<uncached_headers.size(); i++)
55e50c24 11026 {
c9ccb642
FCE
11027 const string& header = uncached_headers[i];
11028
11029 // create a tracequery source file
11030 ostringstream osrc;
11031
11032 // PR9993: Add extra headers to work around undeclared types in individual
11033 // include/trace/foo.h files
11034 vector<string> short_decls = tracepoint_extra_decls(s, header);
11035
11036 // add each requested tracepoint header
75ae2ec9 11037 size_t root_pos = header.rfind("include/");
832f100d 11038 short_decls.push_back(string("#include <") +
75ae2ec9 11039 ((root_pos != string::npos) ? header.substr(root_pos + 8) : header) +
d4393459 11040 string(">"));
f982c59b 11041
c9ccb642
FCE
11042 osrc << "#ifdef CONFIG_TRACEPOINTS" << endl;
11043 osrc << "#include <linux/tracepoint.h>" << endl;
832f100d
JS
11044
11045 // the kernel has changed this naming a few times, previously TPPROTO,
11046 // TP_PROTO, TPARGS, TP_ARGS, etc. so let's just dupe the latest.
11047 osrc << "#ifndef PARAMS" << endl;
11048 osrc << "#define PARAMS(args...) args" << endl;
11049 osrc << "#endif" << endl;
11050
c9ccb642
FCE
11051 // override DECLARE_TRACE to synthesize probe functions for us
11052 osrc << "#undef DECLARE_TRACE" << endl;
11053 osrc << "#define DECLARE_TRACE(name, proto, args) \\" << endl;
11054 osrc << " void stapprobe_##name(proto) {}" << endl;
832f100d 11055
c9ccb642
FCE
11056 // 2.6.35 added the NOARGS variant, but it's the same for us
11057 osrc << "#undef DECLARE_TRACE_NOARGS" << endl;
11058 osrc << "#define DECLARE_TRACE_NOARGS(name) \\" << endl;
11059 osrc << " DECLARE_TRACE(name, void, )" << endl;
832f100d
JS
11060
11061 // 2.6.38 added the CONDITION variant, which can also just redirect
11062 osrc << "#undef DECLARE_TRACE_CONDITION" << endl;
11063 osrc << "#define DECLARE_TRACE_CONDITION(name, proto, args, cond) \\" << endl;
11064 osrc << " DECLARE_TRACE(name, PARAMS(proto), PARAMS(args))" << endl;
11065
c9ccb642
FCE
11066 // older tracepoints used DEFINE_TRACE, so redirect that too
11067 osrc << "#undef DEFINE_TRACE" << endl;
11068 osrc << "#define DEFINE_TRACE(name, proto, args) \\" << endl;
832f100d
JS
11069 osrc << " DECLARE_TRACE(name, PARAMS(proto), PARAMS(args))" << endl;
11070
c9ccb642
FCE
11071 // add the specified decls/#includes
11072 for (unsigned z=0; z<short_decls.size(); z++)
11073 osrc << "#undef TRACE_INCLUDE_FILE\n"
11074 << "#undef TRACE_INCLUDE_PATH\n"
11075 << short_decls[z] << "\n";
11076
11077 // finish up the module source
11078 osrc << "#endif /* CONFIG_TRACEPOINTS */" << endl;
47dd066d 11079
c9ccb642
FCE
11080 // save the source file away
11081 headers_tracequery_src[header] = osrc.str();
55e50c24 11082 }
f982c59b 11083
c9ccb642 11084 // now build them all together
2a0e62a8 11085 map<string,string> tracequery_objs = make_tracequeries(s, headers_tracequery_src);
47dd066d 11086
c9ccb642 11087 // now plop them into the cache
b278033a 11088 if (s.use_cache)
c9ccb642
FCE
11089 for (size_t i=0; i<uncached_headers.size(); i++)
11090 {
11091 const string& header = uncached_headers[i];
2a0e62a8
JS
11092 const string& tracequery_obj = tracequery_objs[header];
11093 const string& tracequery_path = headers_cache_obj[header];
11094 if (tracequery_obj !="" && file_exists(tracequery_obj))
c9ccb642 11095 {
2a0e62a8 11096 copy_file(tracequery_obj, tracequery_path, s.verbose > 2);
c9ccb642
FCE
11097 modules.push_back (tracequery_path);
11098 }
c252fca2
JS
11099 else
11100 // cache an empty file for failures
11101 copy_file("/dev/null", tracequery_path, s.verbose > 2);
c9ccb642 11102 }
f982c59b
JS
11103}
11104
11105
d4393459 11106
f982c59b
JS
11107bool
11108tracepoint_builder::init_dw(systemtap_session& s)
11109{
11110 if (dw != NULL)
11111 return true;
11112
11113 vector<string> tracequery_modules;
55e50c24 11114 vector<string> system_headers;
f982c59b
JS
11115
11116 glob_t trace_glob;
d4393459
FCE
11117
11118 // find kernel_source_tree
11119 if (s.kernel_source_tree == "")
f982c59b 11120 {
d4393459 11121 unsigned found;
ccf2c922 11122 Dwfl *dwfl = setup_dwfl_kernel ("kernel", &found, s);
d4393459
FCE
11123 if (found)
11124 {
11125 Dwarf_Die *cudie = 0;
11126 Dwarf_Addr bias;
11127 while ((cudie = dwfl_nextcu (dwfl, cudie, &bias)) != NULL)
11128 {
e19ebcf7 11129 assert_no_interrupts();
d4393459
FCE
11130 Dwarf_Attribute attr;
11131 const char* name = dwarf_formstring (dwarf_attr (cudie, DW_AT_comp_dir, &attr));
36d65b45 11132 if (name)
d4393459 11133 {
36d65b45
JS
11134 // check that the path actually exists locally before we try to use it
11135 if (file_exists(name))
11136 {
11137 if (s.verbose > 2)
11138 clog << _F("Located kernel source tree (DW_AT_comp_dir) at '%s'", name) << endl;
11139 s.kernel_source_tree = name;
11140 }
11141 else
11142 {
11143 if (s.verbose > 2)
11144 clog << _F("Ignoring inaccessible kernel source tree (DW_AT_comp_dir) at '%s'", name) << endl;
11145 }
61f1a63b 11146
d4393459
FCE
11147 break; // skip others; modern Kbuild uses same comp_dir for them all
11148 }
11149 }
11150 }
44392d73 11151 dwfl_end (dwfl);
d4393459
FCE
11152 }
11153
11154 // prefixes
11155 vector<string> glob_prefixes;
11156 glob_prefixes.push_back (s.kernel_build_tree);
11157 if (s.kernel_source_tree != "")
11158 glob_prefixes.push_back (s.kernel_source_tree);
11159
11160 // suffixes
11161 vector<string> glob_suffixes;
11162 glob_suffixes.push_back("include/trace/events/*.h");
11163 glob_suffixes.push_back("include/trace/*.h");
d97d428e 11164 glob_suffixes.push_back("include/ras/*_event.h");
d4393459 11165 glob_suffixes.push_back("arch/x86/kvm/*trace.h");
d97d428e
FCE
11166 glob_suffixes.push_back("arch/x86/kernel/*trace.h");
11167 glob_suffixes.push_back("arch/*/include/asm/trace*.h");
11168 glob_suffixes.push_back("arch/*/include/asm/trace/*.h");
f89ff3e2 11169 glob_suffixes.push_back("fs/xfs/linux-*/xfs_tr*.h");
d97d428e
FCE
11170 glob_suffixes.push_back("fs/*/*trace*.h");
11171 glob_suffixes.push_back("net/*/*trace*.h");
11172 glob_suffixes.push_back("sound/pci/hda/*_trace.h");
11173 glob_suffixes.push_back("drivers/gpu/drm/*_trace.h");
11174 glob_suffixes.push_back("drivers/gpu/drm/*/*_trace.h");
11175 glob_suffixes.push_back("drivers/net/wireless/*/*/*trace*.h");
11176 glob_suffixes.push_back("drivers/usb/host/*trace*.h");
11177
11178 // see also tracepoint_extra_decls above
d4393459
FCE
11179
11180 // compute cartesian product
11181 vector<string> globs;
11182 for (unsigned i=0; i<glob_prefixes.size(); i++)
11183 for (unsigned j=0; j<glob_suffixes.size(); j++)
11184 globs.push_back (glob_prefixes[i]+string("/")+glob_suffixes[j]);
11185
8aa43b8d 11186 set<string> duped_headers;
d4393459
FCE
11187 for (unsigned z = 0; z < globs.size(); z++)
11188 {
11189 string glob_str = globs[z];
11190 if (s.verbose > 3)
b530b5b3 11191 clog << _("Checking tracepoint glob ") << glob_str << endl;
d4393459 11192
067cc66f
CM
11193 int r = glob(glob_str.c_str(), 0, NULL, &trace_glob);
11194 if (r == GLOB_NOSPACE || r == GLOB_ABORTED)
11195 throw runtime_error("Error globbing tracepoint");
11196
f982c59b
JS
11197 for (unsigned i = 0; i < trace_glob.gl_pathc; ++i)
11198 {
11199 string header(trace_glob.gl_pathv[i]);
11200
11201 // filter out a few known "internal-only" headers
60d98537
JS
11202 if (endswith(header, "/define_trace.h") ||
11203 endswith(header, "/ftrace.h") ||
11204 endswith(header, "/trace_events.h") ||
11205 endswith(header, "_event_types.h"))
f982c59b
JS
11206 continue;
11207
b1966849
FCE
11208 // With headers now plopped under arch/FOO/include/asm/*,
11209 // the following logic miss some tracepoints.
11210#if 0
8aa43b8d
JS
11211 // skip identical headers from the build and source trees.
11212 size_t root_pos = header.rfind("include/");
11213 if (root_pos != string::npos &&
11214 !duped_headers.insert(header.substr(root_pos + 8)).second)
11215 continue;
b1966849 11216#endif
8aa43b8d 11217
55e50c24 11218 system_headers.push_back(header);
f982c59b
JS
11219 }
11220 globfree(&trace_glob);
11221 }
11222
c9ccb642
FCE
11223 // Build tracequery modules
11224 get_tracequery_modules(s, system_headers, tracequery_modules);
55e50c24 11225
f982c59b
JS
11226 // TODO: consider other sources of tracepoint headers too, like from
11227 // a command-line parameter or some environment or .systemtaprc
47dd066d 11228
59c11f91 11229 dw = new dwflpp(s, tracequery_modules, true);
0a6f5a3f
JS
11230 return true;
11231}
47dd066d 11232
0a6f5a3f
JS
11233void
11234tracepoint_builder::build(systemtap_session& s,
11235 probe *base, probe_point *location,
11236 literal_map_t const& parameters,
11237 vector<derived_probe*>& finished_results)
11238{
11239 if (!init_dw(s))
11240 return;
47dd066d 11241
75ead1f7
JS
11242 string tracepoint;
11243 assert(get_param (parameters, TOK_TRACE, tracepoint));
47dd066d 11244
75ead1f7 11245 tracepoint_query q(*dw, tracepoint, base, location, finished_results);
d906ab9d 11246 unsigned results_pre = finished_results.size();
06de3a04 11247 dw->iterate_over_modules<base_query>(&query_module, &q);
d906ab9d
JL
11248 unsigned results_post = finished_results.size();
11249
11250 // Did we fail to find a match? Let's suggest something!
11251 if (results_pre == results_post)
11252 {
11253 size_t pos;
11254 string sugs = suggest_dwarf_functions(s, q.visited_modules, tracepoint);
11255 while ((pos = sugs.find("stapprobe_")) != string::npos)
11256 sugs.erase(pos, string("stapprobe_").size());
11257 if (!sugs.empty())
ece93f53
JL
11258 throw SEMANTIC_ERROR (_NF("no match (similar tracepoint: %s)",
11259 "no match (similar tracepoints: %s)",
d906ab9d
JL
11260 sugs.find(',') == string::npos,
11261 sugs.c_str()));
11262 }
47dd066d 11263}
47dd066d 11264
e6fe60e7 11265
b55bc428 11266// ------------------------------------------------------------------------
bd2b1e68 11267// Standard tapset registry.
b55bc428
FCE
11268// ------------------------------------------------------------------------
11269
7a053d3b 11270void
f8220a7b 11271register_standard_tapsets(systemtap_session & s)
b55bc428 11272{
47e0478e 11273 register_tapset_been(s);
93646f4d 11274 register_tapset_itrace(s);
dd0e4fa7 11275 register_tapset_mark(s);
7a212aa8 11276 register_tapset_procfs(s);
912e8c59 11277 register_tapset_timers(s);
8d9609f5 11278 register_tapset_netfilter(s);
b84779a5 11279 register_tapset_utrace(s);
b98a8d73 11280
7a24d422 11281 // dwarf-based kprobe/uprobe parts
c4ce66a1 11282 dwarf_derived_probe::register_patterns(s);
30a279be 11283
888af770
FCE
11284 // XXX: user-space starter set
11285 s.pattern_root->bind_num(TOK_PROCESS)
11286 ->bind_num(TOK_STATEMENT)->bind(TOK_ABSOLUTE)
f66bb29a 11287 ->bind_privilege(pr_all)
888af770
FCE
11288 ->bind(new uprobe_builder ());
11289 s.pattern_root->bind_num(TOK_PROCESS)
11290 ->bind_num(TOK_STATEMENT)->bind(TOK_ABSOLUTE)->bind(TOK_RETURN)
f66bb29a 11291 ->bind_privilege(pr_all)
888af770
FCE
11292 ->bind(new uprobe_builder ());
11293
0a6f5a3f
JS
11294 // kernel tracepoint probes
11295 s.pattern_root->bind(TOK_KERNEL)->bind_str(TOK_TRACE)
11296 ->bind(new tracepoint_builder());
11297
e6fe60e7
AM
11298 // Kprobe based probe
11299 s.pattern_root->bind(TOK_KPROBE)->bind_str(TOK_FUNCTION)
11300 ->bind(new kprobe_builder());
3c57fe1f
JS
11301 s.pattern_root->bind(TOK_KPROBE)->bind_str(TOK_FUNCTION)->bind(TOK_CALL)
11302 ->bind(new kprobe_builder());
e6fe60e7
AM
11303 s.pattern_root->bind(TOK_KPROBE)->bind_str(TOK_MODULE)
11304 ->bind_str(TOK_FUNCTION)->bind(new kprobe_builder());
3c57fe1f
JS
11305 s.pattern_root->bind(TOK_KPROBE)->bind_str(TOK_MODULE)
11306 ->bind_str(TOK_FUNCTION)->bind(TOK_CALL)->bind(new kprobe_builder());
e6fe60e7
AM
11307 s.pattern_root->bind(TOK_KPROBE)->bind_str(TOK_FUNCTION)->bind(TOK_RETURN)
11308 ->bind(new kprobe_builder());
b6371390
JS
11309 s.pattern_root->bind(TOK_KPROBE)->bind_str(TOK_FUNCTION)->bind(TOK_RETURN)
11310 ->bind_num(TOK_MAXACTIVE)->bind(new kprobe_builder());
e6fe60e7
AM
11311 s.pattern_root->bind(TOK_KPROBE)->bind_str(TOK_MODULE)
11312 ->bind_str(TOK_FUNCTION)->bind(TOK_RETURN)->bind(new kprobe_builder());
b6371390
JS
11313 s.pattern_root->bind(TOK_KPROBE)->bind_str(TOK_MODULE)
11314 ->bind_str(TOK_FUNCTION)->bind(TOK_RETURN)
11315 ->bind_num(TOK_MAXACTIVE)->bind(new kprobe_builder());
e6fe60e7
AM
11316 s.pattern_root->bind(TOK_KPROBE)->bind_num(TOK_STATEMENT)
11317 ->bind(TOK_ABSOLUTE)->bind(new kprobe_builder());
dd225250
PS
11318
11319 //Hwbkpt based probe
b47f3a55
FCE
11320 // NB: we formerly registered the probe point types only if the kernel configuration
11321 // allowed it. However, we get better error messages if we allow probes to resolve.
11322 s.pattern_root->bind(TOK_KERNEL)->bind_num(TOK_HWBKPT)
11323 ->bind(TOK_HWBKPT_WRITE)->bind(new hwbkpt_builder());
11324 s.pattern_root->bind(TOK_KERNEL)->bind_str(TOK_HWBKPT)
11325 ->bind(TOK_HWBKPT_WRITE)->bind(new hwbkpt_builder());
11326 s.pattern_root->bind(TOK_KERNEL)->bind_num(TOK_HWBKPT)
11327 ->bind(TOK_HWBKPT_RW)->bind(new hwbkpt_builder());
11328 s.pattern_root->bind(TOK_KERNEL)->bind_str(TOK_HWBKPT)
11329 ->bind(TOK_HWBKPT_RW)->bind(new hwbkpt_builder());
11330 s.pattern_root->bind(TOK_KERNEL)->bind_num(TOK_HWBKPT)
11331 ->bind_num(TOK_LENGTH)->bind(TOK_HWBKPT_WRITE)->bind(new hwbkpt_builder());
11332 s.pattern_root->bind(TOK_KERNEL)->bind_num(TOK_HWBKPT)
11333 ->bind_num(TOK_LENGTH)->bind(TOK_HWBKPT_RW)->bind(new hwbkpt_builder());
11334 // length supported with address only, not symbol names
83ea76b1
WC
11335
11336 //perf event based probe
4763f713 11337 register_tapset_perf(s);
a29858ef 11338 register_tapset_java(s);
b55bc428 11339}
dc38c0ae
DS
11340
11341
b20febf3
FCE
11342vector<derived_probe_group*>
11343all_session_groups(systemtap_session& s)
dc38c0ae 11344{
b20febf3 11345 vector<derived_probe_group*> g;
912e8c59
JS
11346
11347#define DOONE(x) \
11348 if (s. x##_derived_probes) \
11349 g.push_back ((derived_probe_group*)(s. x##_derived_probes))
ab655cf8
DS
11350
11351 // Note that order *is* important here. We want to make sure we
11352 // register (actually run) begin probes before any other probe type
11353 // is run. Similarly, when unregistering probes, we want to
11354 // unregister (actually run) end probes after every other probe type
11355 // has be unregistered. To do the latter,
11356 // c_unparser::emit_module_exit() will run this list backwards.
b20febf3
FCE
11357 DOONE(be);
11358 DOONE(dwarf);
888af770 11359 DOONE(uprobe);
b20febf3
FCE
11360 DOONE(timer);
11361 DOONE(profile);
11362 DOONE(mark);
0a6f5a3f 11363 DOONE(tracepoint);
e6fe60e7 11364 DOONE(kprobe);
dd225250 11365 DOONE(hwbkpt);
83ea76b1 11366 DOONE(perf);
b20febf3 11367 DOONE(hrtimer);
ce82316f 11368 DOONE(procfs);
8d9609f5 11369 DOONE(netfilter);
935447c8
DS
11370
11371 // Another "order is important" item. We want to make sure we
11372 // "register" the dummy task_finder probe group after all probe
11373 // groups that use the task_finder.
11374 DOONE(utrace);
a96d1db0 11375 DOONE(itrace);
f31a77f5 11376 DOONE(dynprobe);
935447c8 11377 DOONE(task_finder);
b20febf3
FCE
11378#undef DOONE
11379 return g;
46b84a80 11380}
73267b89
JS
11381
11382/* vim: set sw=2 ts=8 cino=>4,n-2,{2,^-2,t0,(0,u0,w1,M1 : */
This page took 2.980687 seconds and 5 git commands to generate.