]> sourceware.org Git - systemtap.git/blame - tapsets.cxx
Use POSIX '=' in run-stap and run-staprun
[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;
4380196b
WC
6237 if (elf_machine == EM_PPC || elf_machine == EM_PPC64
6238 || elf_machine == EM_ARM || elf_machine == EM_AARCH64)
95b78bc9
JL
6239 regexp = "^(" + regnames + ")$";
6240 else
6241 regexp = "^(" + percent_regnames + ")$";
6242
6243 vector<string> matches;
6244 if (!regexp_match(asmarg, regexp, matches))
6245 {
6246 string regname = matches[1];
6247 map<string,pair<unsigned,int> >::iterator ri = dwarf_regs.find (regname);
6248 if (ri != dwarf_regs.end()) // known register
8aabf152 6249 {
95b78bc9
JL
6250 embedded_expr *get_arg1 = new embedded_expr;
6251 string width_adjust;
6252 switch (ri->second.second)
8aabf152 6253 {
95b78bc9
JL
6254 case QI: width_adjust = ") & 0xff)"; break;
6255 case QIh: width_adjust = ">>8) & 0xff)"; break;
6256 case HI:
6257 // preserve 16 bit register signness
6258 width_adjust = ") & 0xffff)";
6259 if (precision < 0)
6260 width_adjust += " << 48 >> 48";
6261 break;
6262 case SI:
6263 // preserve 32 bit register signness
6264 width_adjust = ") & 0xffffffff)";
6265 if (precision < 0)
6266 width_adjust += " << 32 >> 32";
6267 break;
6268 default: width_adjust = "))";
8aabf152 6269 }
95b78bc9
JL
6270 string type = "";
6271 if (probe_type == uprobe3_type)
6272 type = (precision < 0
6273 ? "(int" : "(uint") + lex_cast(abs(precision) * 8) + "_t)";
6274 type = type + "((";
6275 get_arg1->tok = e->tok;
6276 get_arg1->code = string("/* unprivileged */ /* pure */")
6277 + string(" ((int64_t)") + type
a57b7534 6278 + string("u_fetch_register(")
95b78bc9
JL
6279 + lex_cast(dwarf_regs[regname].first) + string("))")
6280 + width_adjust;
6281 argexpr = get_arg1;
8aabf152 6282 }
95b78bc9
JL
6283 }
6284 return argexpr;
6285}
40fe32e0 6286
b15f98b2
JL
6287static string
6288precision_to_function(long precision)
6289{
6290 switch (precision)
6291 {
6292 case 1: case -1:
6293 return "user_int8";
6294 case 2:
6295 return "user_uint16";
6296 case -2:
6297 return "user_int16";
6298 case 4:
6299 return "user_uint32";
6300 case -4:
6301 return "user_int32";
6302 case 8: case -8:
6303 return "user_int64";
6304 default:
6305 return "user_long";
6306 }
6307}
8aabf152 6308
8da56b62
JL
6309expression*
6310sdt_uprobe_var_expanding_visitor::try_parse_arg_offset_register (target_symbol *e,
6311 const string& asmarg,
6312 long precision)
6313{
6314 expression *argexpr = NULL;
8095a157 6315
8da56b62
JL
6316 // test for OFFSET(REGISTER) where OFFSET is +-N+-N+-N
6317 // NB: Despite PR11821, we can use regnames here, since the parentheses
6318 // make things unambiguous. (Note: gdb/stap-probe.c also parses this)
6319 // On ARM test for [REGISTER, OFFSET]
8095a157 6320
8da56b62
JL
6321 string regexp;
6322 int reg, offset1;
4380196b 6323 if (elf_machine == EM_ARM || elf_machine == EM_AARCH64)
8da56b62 6324 {
297745ba 6325 regexp = "^\\[(" + regnames + ")(,[ ]*[#]?([+-]?[0-9]+)([+-][0-9]*)?([+-][0-9]*)?)?\\]$";
8da56b62
JL
6326 reg = 1;
6327 offset1 = 3;
6328 }
6329 else
6330 {
6331 regexp = "^([+-]?[0-9]*)([+-][0-9]*)?([+-][0-9]*)?[(](" + regnames + ")[)]$";
6332 reg = 4;
6333 offset1 = 1;
6334 }
6335
6336 vector<string> matches;
6337 if (!regexp_match(asmarg, regexp, matches))
6338 {
6339 string regname;
6340 int64_t disp = 0;
6341 if (matches[reg].length())
6342 regname = matches[reg];
6343 if (dwarf_regs.find (regname) == dwarf_regs.end())
6344 throw SEMANTIC_ERROR(_F("unrecognized register '%s'", regname.c_str()));
6345
6346 for (int i=offset1; i <= (offset1 + 2); i++)
6347 if (matches[i].length())
6348 // should decode positive/negative hex/decimal
6349 // NB: let it throw if something happens
6350 disp += lex_cast<int64_t>(matches[i]);
6351
6352 // synthesize user_long(%{fetch_register(R)%} + D)
6353 embedded_expr *get_arg1 = new embedded_expr;
6354 get_arg1->tok = e->tok;
6355 get_arg1->code = string("/* unprivileged */ /* pure */")
a57b7534 6356 + string("u_fetch_register(")
8da56b62
JL
6357 + lex_cast(dwarf_regs[regname].first) + string(")");
6358 // XXX: may we ever need to cast that to a narrower type?
6359
6360 literal_number* inc = new literal_number(disp);
6361 inc->tok = e->tok;
6362
6363 binary_expression *be = new binary_expression;
6364 be->tok = e->tok;
6365 be->left = get_arg1;
6366 be->op = "+";
6367 be->right = inc;
6368
6369 functioncall *fc = new functioncall;
b15f98b2 6370 fc->function = precision_to_function(precision);
8da56b62
JL
6371 fc->tok = e->tok;
6372 fc->args.push_back(be);
8095a157 6373
8da56b62
JL
6374 argexpr = fc;
6375 }
8095a157 6376
8da56b62
JL
6377 return argexpr;
6378}
8095a157 6379
10fcca15
JL
6380expression*
6381sdt_uprobe_var_expanding_visitor::try_parse_arg_effective_addr (target_symbol *e,
6382 const string& asmarg,
6383 long precision)
6384{
6385 expression *argexpr = NULL;
6386
6387 // test for OFFSET(BASE_REGISTER,INDEX_REGISTER[,SCALE]) where OFFSET is +-N+-N+-N
6388 // NB: Despite PR11821, we can use regnames here, since the parentheses
6389 // make things unambiguous. (Note: gdb/stap-probe.c also parses this)
6390 string regexp = "^([+-]?[0-9]*)([+-][0-9]*)?([+-][0-9]*)?[(](" + regnames + "),(" +
6391 regnames + ")(,[1248])?[)]$";
6392 vector<string> matches;
6393 if (!regexp_match(asmarg, regexp, matches))
6394 {
6395 string baseregname;
6396 string indexregname;
6397 int64_t disp = 0;
6398 short scale = 1;
6399
6400 if (matches[6].length())
6401 // NB: let it throw if we can't cast
6402 scale = lex_cast<short>(matches[6].substr(1)); // NB: skip the comma!
6403
6404 if (matches[4].length())
6405 baseregname = matches[4];
6406 if (dwarf_regs.find (baseregname) == dwarf_regs.end())
6407 throw SEMANTIC_ERROR(_F("unrecognized base register '%s'", baseregname.c_str()));
6408
6409 if (matches[5].length())
6410 indexregname = matches[5];
6411 if (dwarf_regs.find (indexregname) == dwarf_regs.end())
6412 throw SEMANTIC_ERROR(_F("unrecognized index register '%s'", indexregname.c_str()));
6413
6414 for (int i = 1; i <= 3; i++) // up to three OFFSET terms
6415 if (matches[i].length())
6416 // should decode positive/negative hex/decimal
6417 // NB: let it throw if something happens
6418 disp += lex_cast<int64_t>(matches[i]);
6419
6420 // synthesize user_long(%{fetch_register(R1)+fetch_register(R2)*N%} + D)
6421
6422 embedded_expr *get_arg1 = new embedded_expr;
a57b7534 6423 string regfn = "u_fetch_register";
10fcca15
JL
6424
6425 get_arg1->tok = e->tok;
6426 get_arg1->code = string("/* unprivileged */ /* pure */")
6427 + regfn + string("(")+lex_cast(dwarf_regs[baseregname].first)+string(")")
6428 + string("+(")
6429 + regfn + string("(")+lex_cast(dwarf_regs[indexregname].first)+string(")")
6430 + string("*")
6431 + lex_cast(scale)
6432 + string(")");
6433
6434 // NB: could plop this +DISPLACEMENT bit into the embedded-c expression too
6435 literal_number* inc = new literal_number(disp);
6436 inc->tok = e->tok;
6437
6438 binary_expression *be = new binary_expression;
6439 be->tok = e->tok;
6440 be->left = get_arg1;
6441 be->op = "+";
6442 be->right = inc;
6443
6444 functioncall *fc = new functioncall;
b15f98b2 6445 fc->function = precision_to_function(precision);
10fcca15
JL
6446 fc->tok = e->tok;
6447 fc->args.push_back(be);
8095a157 6448
10fcca15
JL
6449 argexpr = fc;
6450 }
8095a157 6451
10fcca15
JL
6452 return argexpr;
6453}
8095a157 6454
451c0c25
JL
6455expression*
6456sdt_uprobe_var_expanding_visitor::try_parse_arg_varname (target_symbol *e,
6457 const string& asmarg,
6458 long precision)
6ef331c8 6459{
b8688ee6 6460 static unsigned tick = 0;
451c0c25
JL
6461 expression *argexpr = NULL;
6462
6463 // test for [OFF+]VARNAME[+OFF][(REGISTER)], where VARNAME is a variable
6464 // name. NB: Despite PR11821, we can use regnames here, since the parentheses
6465 // make things unambiguous.
6466 string regex = "^(([0-9]+)[+])?([a-zA-Z_][a-zA-Z0-9_]*)([+][0-9]+)?([(]("
6467 + regnames + ")[)])?$";
6468 vector<string> matches;
6469 if (!regexp_match(asmarg, regex, matches))
6470 {
6471 assert(matches.size() >= 4);
6472 string varname = matches[3];
6473
6474 // OFF can be before VARNAME (put in matches[2]) or after (put in
6475 // matches[4]) (or both?). Seems like in most cases it comes after,
6476 // unless the code was compiled with -fPIC.
6477 int64_t offset = 0;
6478 if (!matches[2].empty())
6479 offset += lex_cast<int64_t>(matches[2]);
6480 if (matches.size() >= 5 && !matches[4].empty())
6481 offset += lex_cast<int64_t>(matches[4]);
6482
6483 string regname;
6484 if (matches.size() >= 7)
6485 regname = matches[6];
6486
6487 // If it's just VARNAME, then proceed. If it's VARNAME(REGISTER), then
6488 // only proceed if it's RIP-relative addressing on x86_64.
6489 if (regname.empty() || (regname == "%rip" && elf_machine == EM_X86_64))
6490 {
6491 dw.mod_info->get_symtab();
6492 if (dw.mod_info->symtab_status != info_present)
6493 throw SEMANTIC_ERROR(_("can't retrieve symbol table"));
6494
6495 assert(dw.mod_info->sym_table);
6496 map<string, Dwarf_Addr>& globals = dw.mod_info->sym_table->globals;
6497 map<string, Dwarf_Addr>& locals = dw.mod_info->sym_table->locals;
6498 Dwarf_Addr addr = 0;
6499
6500 // check symtab locals then globals
6501 if (locals.count(varname))
6502 addr = locals[varname];
6503 if (globals.count(varname))
6504 addr = globals[varname];
6505
6506 if (addr)
8095a157 6507 {
451c0c25
JL
6508 // add whatever offset is in the operand
6509 addr += offset;
6510
6511 // adjust for dw bias because relocate_address() expects a
6512 // libdw address and this addr is from the symtab
6513 dw.get_module_dwarf(false, false);
6514 addr -= dw.module_bias;
6515
6516 string reloc_section;
6517 Dwarf_Addr reloc_addr = dw.relocate_address(addr, reloc_section);
6518
6519 // OK, we have an address for the variable. Let's create a
6520 // function that will just relocate it at runtime, and then
6521 // call user_[u]int*() on the address it returns.
6522
6523 functioncall *user_int_call = new functioncall;
b15f98b2 6524 user_int_call->function = precision_to_function(precision);
451c0c25
JL
6525 user_int_call->tok = e->tok;
6526
6527 functiondecl *get_addr_decl = new functiondecl;
6528 get_addr_decl->tok = e->tok;
6529 get_addr_decl->synthetic = true;
6530 get_addr_decl->name = "_sdt_arg_get_addr_" + lex_cast(tick++);
6531 get_addr_decl->type = pe_long;
6532
6bc53f02 6533 // build _stp_umodule_relocate(module, addr, current)
451c0c25
JL
6534 stringstream ss;
6535 ss << " /* unprivileged */ /* pure */ /* pragma:vma */" << endl;
a57b7534
JL
6536 ss << "STAP_RETURN(_stp_umodule_relocate(";
6537 ss << "\"" << path_remove_sysroot(session, process_name) << "\", ";
6538 ss << "0x" << hex << reloc_addr << dec << ", ";
6539 ss << "current";
6540 ss << "));" << endl;
451c0c25
JL
6541
6542 embeddedcode *ec = new embeddedcode;
6543 ec->tok = e->tok;
6544 ec->code = ss.str();
6545 get_addr_decl->body = ec;
6546 get_addr_decl->join(session);
6547
6548 functioncall *get_addr_call = new functioncall;
6549 get_addr_call->tok = e->tok;
6550 get_addr_call->function = get_addr_decl->name;
6551 user_int_call->args.push_back(get_addr_call);
b8688ee6 6552
451c0c25 6553 argexpr = user_int_call;
8095a157 6554 }
8aabf152 6555 }
451c0c25
JL
6556 }
6557
6558 return argexpr;
6559}
6560
6561void
6562sdt_uprobe_var_expanding_visitor::visit_target_symbol_arg (target_symbol *e)
6563{
6ef331c8
SC
6564 try
6565 {
03c45fcb 6566 unsigned argno = get_target_symbol_argno_and_validate(e); // the N in $argN
8aabf152 6567 string asmarg = arg_tokens[argno-1]; // $arg1 => arg_tokens[0]
8aabf152 6568
8aabf152
FCE
6569 // Now we try to parse this thing, which is an assembler operand
6570 // expression. If we can't, we warn, back down to need_debug_info
b874bd52 6571 // and hope for the best. Here is the syntax for a few architectures.
9859b766 6572 // Note that the power iN syntax is only for V3 sdt.h; gcc emits the i.
8095a157 6573 //
b6aaaf21
JL
6574 // literal reg reg reg+ base+index*size+ VAR VAR+off RIP-relative
6575 // indirect offset offset VAR+off
6576 // x86 $N %rR (%rR) N(%rR) O(%bR,%iR,S) var var+off var+off(%rip)
6577 // x86_64 $N %rR (%rR) N(%rR) O(%bR,%iR,S) var var+off var+off(%rip)
6578 // power iN R (R) N(R)
6579 // ia64 N rR [r16]
6580 // s390 N %rR 0(rR) N(r15)
6581 // arm #N rR [rR] [rR, #N]
b874bd52 6582
8aabf152
FCE
6583 expression* argexpr = 0; // filled in in case of successful parse
6584
ac074daf
JL
6585 // Parse (and remove from asmarg) the leading length
6586 long precision = parse_out_arg_precision(asmarg);
40fe32e0 6587
0fbf6fb8 6588 try
8aabf152 6589 {
0fbf6fb8
JL
6590 if ((argexpr = try_parse_arg_literal(e, asmarg, precision)) != NULL)
6591 goto matched;
2e6d617a
JL
6592
6593 // all other matches require registers
6594 if (regnames == "")
6595 throw SEMANTIC_ERROR("no registers to use for parsing");
95b78bc9
JL
6596
6597 if ((argexpr = try_parse_arg_register(e, asmarg, precision)) != NULL)
6598 goto matched;
8da56b62
JL
6599 if ((argexpr = try_parse_arg_offset_register(e, asmarg, precision)) != NULL)
6600 goto matched;
10fcca15
JL
6601 if ((argexpr = try_parse_arg_effective_addr(e, asmarg, precision)) != NULL)
6602 goto matched;
451c0c25
JL
6603 if ((argexpr = try_parse_arg_varname(e, asmarg, precision)) != NULL)
6604 goto matched;
0fbf6fb8
JL
6605 }
6606 catch (const semantic_error& err)
6607 {
6608 e->chain(err);
b8688ee6 6609 }
8aabf152 6610
8aabf152
FCE
6611 // The asmarg operand was not recognized. Back down to dwarf.
6612 if (! session.suppress_warnings)
84fef8ee
FCE
6613 {
6614 if (probe_type == UPROBE3_TYPE)
451c0c25
JL
6615 session.print_warning (_F("Can't parse SDT_V3 operand '%s' "
6616 "[man error::sdt]", asmarg.c_str()),
6617 e->tok);
84fef8ee 6618 else // must be *PROBE2; others don't get asm operands
451c0c25
JL
6619 session.print_warning (_F("Downgrading SDT_V2 probe argument to "
6620 "dwarf, can't parse '%s' [man error::sdt]",
6621 asmarg.c_str()),
6622 e->tok);
84fef8ee 6623 }
451c0c25 6624
8aabf152 6625 need_debug_info = true;
451c0c25
JL
6626 throw SEMANTIC_ERROR(_("SDT asm not understood, requires debuginfo "
6627 "[man error::sdt]"), e->tok);
6628
6629 /* NOTREACHED */
366af4e7 6630
8aabf152
FCE
6631 matched:
6632 assert (argexpr != 0);
366af4e7
RM
6633
6634 if (session.verbose > 2)
1e41115c 6635 //TRANSLATORS: We're mapping the operand to a new expression*.
b530b5b3 6636 clog << _F("mapped asm operand %s to ", asmarg.c_str()) << *argexpr << endl;
366af4e7 6637
aff5d390 6638 if (e->components.empty()) // We have a scalar
8aabf152
FCE
6639 {
6640 if (e->addressof)
dc09353a 6641 throw SEMANTIC_ERROR(_("cannot take address of sdt variable"), e->tok);
8aabf152 6642 provide (argexpr);
8aabf152
FCE
6643 }
6644 else // $var->foo
6645 {
6646 cast_op *cast = new cast_op;
6647 cast->name = "@cast";
6648 cast->tok = e->tok;
6649 cast->operand = argexpr;
6650 cast->components = e->components;
6651 cast->type_name = probe_name + "_arg" + lex_cast(argno);
6652 cast->module = process_name;
6653 cast->visit(this);
8aabf152 6654 }
aff5d390
SC
6655 }
6656 catch (const semantic_error &er)
6657 {
6658 e->chain (er);
6659 provide (e);
6660 }
6661}
6662
6663
6ef331c8
SC
6664void
6665sdt_uprobe_var_expanding_visitor::visit_target_symbol (target_symbol* e)
6666{
6667 try
6668 {
49131a6d 6669 assert(e->name.size() > 0
bd1fcbad 6670 && (e->name[0] == '$' || e->name == "@var"));
6ef331c8
SC
6671
6672 if (e->name == "$$name" || e->name == "$$provider" || e->name == "$$parms" || e->name == "$$vars")
6673 visit_target_symbol_context (e);
6674 else
6675 visit_target_symbol_arg (e);
6676 }
6677 catch (const semantic_error &er)
6678 {
6679 e->chain (er);
6680 provide (e);
6681 }
6682}
6683
6684
bd1fcbad
YZ
6685void
6686sdt_uprobe_var_expanding_visitor::visit_atvar_op (atvar_op* e)
6687{
6688 need_debug_info = true;
6689
6690 // Fill in our current module context if needed
6691 if (e->module.empty())
6692 e->module = process_name;
6693
6694 var_expanding_visitor::visit_atvar_op(e);
6695}
6696
6697
40a0c64e
JS
6698void
6699sdt_uprobe_var_expanding_visitor::visit_cast_op (cast_op* e)
6700{
6701 // Fill in our current module context if needed
6702 if (e->module.empty())
6703 e->module = process_name;
6704
6705 var_expanding_visitor::visit_cast_op(e);
6706}
6707
6708
576eaefe
SC
6709void
6710plt_expanding_visitor::visit_target_symbol (target_symbol *e)
6711{
6712 try
6713 {
6714 if (e->name == "$$name")
6715 {
6716 literal_string *myname = new literal_string (entry);
6717 myname->tok = e->tok;
6718 provide(myname);
6719 return;
6720 }
3d69c03f
JS
6721
6722 // variable not found -> throw a semantic error
6723 // (only to be caught right away, but this may be more complex later...)
6724 string alternatives = "$$name";
dc09353a 6725 throw SEMANTIC_ERROR(_F("unable to find plt variable '%s' (alternatives: %s)",
3d69c03f 6726 e->name.c_str(), alternatives.c_str()), e->tok);
576eaefe
SC
6727 }
6728 catch (const semantic_error &er)
6729 {
6730 e->chain (er);
6731 provide (e);
6732 }
6733}
6734
6735
edce5b67
JS
6736struct sdt_query : public base_query
6737{
6738 sdt_query(probe * base_probe, probe_point * base_loc,
6739 dwflpp & dw, literal_map_t const & params,
51d6bda3 6740 vector<derived_probe *> & results, const string user_lib);
edce5b67 6741
51d6bda3 6742 void query_library (const char *data);
614f0fcf
JL
6743 set<string> visited_libraries;
6744 bool resolved_library;
6745
576eaefe 6746 void query_plt (const char *entry, size_t addr) {}
edce5b67
JS
6747 void handle_query_module();
6748
6749private:
15284963 6750 stap_sdt_probe_type probe_type;
d61ea602 6751 enum { probe_section=0, note_section=1, unknown_section=-1 } probe_loc;
edce5b67
JS
6752 probe * base_probe;
6753 probe_point * base_loc;
6846cfc8 6754 literal_map_t const & params;
edce5b67 6755 vector<derived_probe *> & results;
a794dbeb
FCE
6756 string pp_mark;
6757 string pp_provider;
51d6bda3 6758 string user_lib;
edce5b67
JS
6759
6760 set<string> probes_handled;
6761
6762 Elf_Data *pdata;
6763 size_t probe_scn_offset;
6764 size_t probe_scn_addr;
aff5d390 6765 uint64_t arg_count;
40fe32e0 6766 GElf_Addr base;
c57ea854 6767 GElf_Addr pc;
aff5d390 6768 string arg_string;
edce5b67 6769 string probe_name;
a794dbeb 6770 string provider_name;
909ab234 6771 GElf_Addr semaphore_load_offset;
79a0ca08 6772 Dwarf_Addr semaphore;
edce5b67
JS
6773
6774 bool init_probe_scn();
6b51ee12 6775 bool get_next_probe();
c57ea854
SC
6776 void iterate_over_probe_entries();
6777 void handle_probe_entry();
edce5b67 6778
488d3c1b 6779 static void setup_note_probe_entry_callback (sdt_query *me,
dbd24034
JL
6780 const string& scn_name,
6781 const string& note_name,
488d3c1b
JL
6782 int type,
6783 const char *data,
6784 size_t len);
dbd24034
JL
6785 void setup_note_probe_entry (const string& scn_name,
6786 const string& note_name, int type,
6787 const char *data, size_t len);
40fe32e0 6788
edce5b67 6789 void convert_probe(probe *base);
4ddb6dd0 6790 void record_semaphore(vector<derived_probe *> & results, unsigned start);
c72aa911 6791 probe* convert_location();
40fe32e0 6792 bool have_uprobe() {return probe_type == uprobe1_type || probe_type == uprobe2_type || probe_type == uprobe3_type;}
c57ea854
SC
6793 bool have_debuginfo_uprobe(bool need_debug_info)
6794 {return probe_type == uprobe1_type
40fe32e0 6795 || ((probe_type == uprobe2_type || probe_type == uprobe3_type)
c57ea854 6796 && need_debug_info);}
40fe32e0 6797 bool have_debuginfoless_uprobe() {return probe_type == uprobe2_type || probe_type == uprobe3_type;}
edce5b67
JS
6798};
6799
6800
6801sdt_query::sdt_query(probe * base_probe, probe_point * base_loc,
6802 dwflpp & dw, literal_map_t const & params,
51d6bda3 6803 vector<derived_probe *> & results, const string user_lib):
614f0fcf
JL
6804 base_query(dw, params), resolved_library(false),
6805 probe_type(unknown_probe_type), probe_loc(unknown_section),
6806 base_probe(base_probe), base_loc(base_loc), params(params), results(results),
6807 user_lib(user_lib), probe_scn_offset(0), probe_scn_addr(0), arg_count(0),
6808 base(0), pc(0), semaphore_load_offset(0), semaphore(0)
edce5b67 6809{
a794dbeb
FCE
6810 assert(get_string_param(params, TOK_MARK, pp_mark));
6811 get_string_param(params, TOK_PROVIDER, pp_provider); // pp_provider == "" -> unspecified
6812
ef428667
FCE
6813 // PR10245: permit usage of dtrace-y "-" separator in marker name;
6814 // map it to double-underscores.
6815 size_t pos = 0;
6816 while (1) // there may be more than one
6817 {
a794dbeb 6818 size_t i = pp_mark.find("-", pos);
ef428667 6819 if (i == string::npos) break;
a794dbeb 6820 pp_mark.replace (i, 1, "__");
ef428667
FCE
6821 pos = i+1; // resume searching after the inserted __
6822 }
a794dbeb
FCE
6823
6824 // XXX: same for pp_provider?
edce5b67
JS
6825}
6826
6827
6828void
c57ea854 6829sdt_query::handle_probe_entry()
edce5b67 6830{
c57ea854
SC
6831 if (! have_uprobe()
6832 && !probes_handled.insert(probe_name).second)
edce5b67
JS
6833 return;
6834
6835 if (sess.verbose > 3)
c57ea854 6836 {
b78a0fbb 6837 //TRANSLATORS: Describing what probe type (kprobe or uprobe) the probe
b530b5b3
LB
6838 //TRANSLATORS: is matched to.
6839 clog << _F("matched probe_name %s probe type ", probe_name.c_str());
c57ea854
SC
6840 switch (probe_type)
6841 {
6842 case uprobe1_type:
6843 clog << "uprobe1 at 0x" << hex << pc << dec << endl;
6844 break;
6845 case uprobe2_type:
6846 clog << "uprobe2 at 0x" << hex << pc << dec << endl;
6847 break;
40fe32e0
SC
6848 case uprobe3_type:
6849 clog << "uprobe3 at 0x" << hex << pc << dec << endl;
6850 break;
d61ea602
JS
6851 default:
6852 clog << "unknown!" << endl;
6853 break;
c57ea854
SC
6854 }
6855 }
edce5b67 6856
c57ea854
SC
6857 // Extend the derivation chain
6858 probe *new_base = convert_location();
6859 probe_point *new_location = new_base->locations[0];
6860
c57ea854
SC
6861 bool need_debug_info = false;
6862
7d395255
JS
6863 // We could get the Elf* from either dwarf_getelf(dwfl_module_getdwarf(...))
6864 // or dwfl_module_getelf(...). We only need it for the machine type, which
6865 // should be the same. The bias is used for relocating debuginfoless probes,
6866 // though, so that must come from the possibly-prelinked ELF file, not DWARF.
c57ea854 6867 Dwarf_Addr bias;
7d395255 6868 Elf* elf = dwfl_module_getelf (dw.mod_info->mod, &bias);
c57ea854 6869
1cc41cd6
DS
6870 /* Figure out the architecture of this particular ELF file. The
6871 dwarfless register-name mappings depend on it. */
6872 GElf_Ehdr ehdr_mem;
6873 GElf_Ehdr* em = gelf_getehdr (elf, &ehdr_mem);
e1c3b13a 6874 if (em == 0) { DWFL_ASSERT ("dwfl_getehdr", dwfl_errno()); }
d8f31d0a 6875 assert(em);
1cc41cd6 6876 int elf_machine = em->e_machine;
b8688ee6
JL
6877 sdt_uprobe_var_expanding_visitor svv (sess, dw, elf_machine, module_val,
6878 provider_name, probe_name, probe_type,
6879 arg_string, arg_count);
1cc41cd6
DS
6880 svv.replace (new_base->body);
6881 need_debug_info = svv.need_debug_info;
c57ea854 6882
1cc41cd6
DS
6883 // XXX: why not derive_probes() in the uprobes case too?
6884 literal_map_t params;
6885 for (unsigned i = 0; i < new_location->components.size(); ++i)
6886 {
6887 probe_point::component *c = new_location->components[i];
6888 params[c->functor] = c->arg;
6889 }
c57ea854 6890
73d53dd2 6891 unsigned prior_results_size = results.size();
1cc41cd6
DS
6892 dwarf_query q(new_base, new_location, dw, params, results, "", "");
6893 q.has_mark = true; // enables mid-statement probing
30263a73 6894
73d53dd2
JS
6895 // V1 probes always need dwarf info
6896 // V2+ probes need dwarf info in case of a variable reference
1cc41cd6 6897 if (have_debuginfo_uprobe(need_debug_info))
06de3a04 6898 dw.iterate_over_modules<base_query>(&query_module, &q);
73d53dd2
JS
6899
6900 // For V2+ probes, if variable references weren't used or failed (PR14369),
6901 // then try with the more direct approach. Unresolved $vars might still
6902 // cause their own error, but this gives them a chance to be optimized out.
6903 if (have_debuginfoless_uprobe() && results.size() == prior_results_size)
1cc41cd6
DS
6904 {
6905 string section;
6906 Dwarf_Addr reloc_addr = q.statement_num_val + bias;
6907 if (dwfl_module_relocations (q.dw.mod_info->mod) > 0)
6908 {
6909 dwfl_module_relocate_address (q.dw.mod_info->mod, &reloc_addr);
6910 section = ".dynamic";
6911 }
6912 else
6913 section = ".absolute";
edce5b67 6914
1cc41cd6
DS
6915 uprobe_derived_probe* p =
6916 new uprobe_derived_probe ("", "", 0,
6917 path_remove_sysroot(sess,q.module_val),
6918 section,
6919 q.statement_num_val, reloc_addr, q, 0);
6920 p->saveargs (arg_count);
6921 results.push_back (p);
c57ea854 6922 }
487bf4e2 6923 sess.unwindsym_modules.insert (dw.module_name);
73d53dd2 6924 record_semaphore(results, prior_results_size);
c57ea854 6925}
edce5b67 6926
4ddb6dd0 6927
c57ea854
SC
6928void
6929sdt_query::handle_query_module()
6930{
6931 if (!init_probe_scn())
6932 return;
edce5b67 6933
c57ea854
SC
6934 if (sess.verbose > 3)
6935 clog << "TOK_MARK: " << pp_mark << " TOK_PROVIDER: " << pp_provider << endl;
edce5b67 6936
40fe32e0
SC
6937 if (probe_loc == note_section)
6938 {
6939 GElf_Shdr shdr_mem;
6940 GElf_Shdr *shdr = dw.get_section (".stapsdt.base", &shdr_mem);
6941
909ab234
JS
6942 // The 'base' lets us adjust the hardcoded addresses in notes for prelink
6943 // effects. The 'semaphore_load_offset' accounts for the difference in
6944 // load addresses between text and data, so the semaphore can be
6945 // converted to a file offset if needed.
40fe32e0 6946 if (shdr)
909ab234
JS
6947 {
6948 base = shdr->sh_addr;
6949 GElf_Addr base_offset = shdr->sh_offset;
6950 shdr = dw.get_section (".probes", &shdr_mem);
6951 if (shdr)
6952 semaphore_load_offset =
6953 (shdr->sh_addr - shdr->sh_offset) - (base - base_offset);
6954 }
40fe32e0 6955 else
909ab234
JS
6956 base = semaphore_load_offset = 0;
6957
488d3c1b 6958 dw.iterate_over_notes (this, &sdt_query::setup_note_probe_entry_callback);
40fe32e0 6959 }
d61ea602 6960 else if (probe_loc == probe_section)
40fe32e0 6961 iterate_over_probe_entries ();
edce5b67
JS
6962}
6963
6964
6965bool
6966sdt_query::init_probe_scn()
6967{
448a86b7 6968 Elf* elf;
edce5b67 6969 GElf_Shdr shdr_mem;
40fe32e0
SC
6970
6971 GElf_Shdr *shdr = dw.get_section (".note.stapsdt", &shdr_mem);
6972 if (shdr)
6973 {
6974 probe_loc = note_section;
6975 return true;
6976 }
edce5b67 6977
448a86b7 6978 shdr = dw.get_section (".probes", &shdr_mem, &elf);
fea74777 6979 if (shdr)
edce5b67 6980 {
fea74777
SC
6981 pdata = elf_getdata_rawchunk (elf, shdr->sh_offset, shdr->sh_size, ELF_T_BYTE);
6982 probe_scn_offset = 0;
6983 probe_scn_addr = shdr->sh_addr;
6984 assert (pdata != NULL);
6985 if (sess.verbose > 4)
ce0f6648
LB
6986 clog << "got .probes elf scn_addr@0x" << probe_scn_addr << ", size: "
6987 << pdata->d_size << endl;
40fe32e0 6988 probe_loc = probe_section;
fea74777 6989 return true;
edce5b67 6990 }
fea74777 6991 else
edce5b67 6992 return false;
edce5b67
JS
6993}
6994
40fe32e0 6995void
dbd24034
JL
6996sdt_query::setup_note_probe_entry_callback (sdt_query *me,
6997 const string& scn_name,
6998 const string& note_name, int type,
6999 const char *data, size_t len)
40fe32e0 7000{
dbd24034 7001 me->setup_note_probe_entry (scn_name, note_name, type, data, len);
40fe32e0
SC
7002}
7003
7004
7005void
dbd24034
JL
7006sdt_query::setup_note_probe_entry (const string& scn_name,
7007 const string& note_name, int type,
7008 const char *data, size_t len)
40fe32e0 7009{
dbd24034
JL
7010 if (scn_name.compare(".note.stapsdt"))
7011 return;
7012#define _SDT_NOTE_NAME "stapsdt"
7013 if (note_name.compare(_SDT_NOTE_NAME))
7014 return;
40fe32e0
SC
7015#define _SDT_NOTE_TYPE 3
7016 if (type != _SDT_NOTE_TYPE)
7017 return;
7018
dbd24034 7019 // we found a probe entry
40fe32e0
SC
7020 union
7021 {
7022 Elf64_Addr a64[3];
7023 Elf32_Addr a32[3];
7024 } buf;
7025 Dwarf_Addr bias;
7026 Elf* elf = (dwfl_module_getelf (dw.mod_info->mod, &bias));
7027 Elf_Data dst =
7028 {
7029 &buf, ELF_T_ADDR, EV_CURRENT,
7030 gelf_fsize (elf, ELF_T_ADDR, 3, EV_CURRENT), 0, 0
7031 };
7032 assert (dst.d_size <= sizeof buf);
7033
7034 if (len < dst.d_size + 3)
7035 return;
7036
7037 Elf_Data src =
7038 {
7039 (void *) data, ELF_T_ADDR, EV_CURRENT,
7040 dst.d_size, 0, 0
7041 };
7042
7043 if (gelf_xlatetom (elf, &dst, &src,
7044 elf_getident (elf, NULL)[EI_DATA]) == NULL)
7045 printf ("gelf_xlatetom: %s", elf_errmsg (-1));
7046
7047 probe_type = uprobe3_type;
7048 const char * provider = data + dst.d_size;
3f803f9e 7049
40fe32e0 7050 const char *name = (const char*)memchr (provider, '\0', data + len - provider);
3f803f9e
CM
7051 if(name++ == NULL)
7052 return;
7053
7054 const char *args = (const char*)memchr (name, '\0', data + len - name);
7055 if (args++ == NULL || memchr (args, '\0', data + len - name) != data + len - 1)
7056 return;
7057
7058 provider_name = provider;
7059 probe_name = name;
7060 arg_string = args;
40fe32e0 7061
e90006d7
JL
7062 dw.mod_info->marks.insert(make_pair(provider, name));
7063
40fe32e0
SC
7064 // Did we find a matching probe?
7065 if (! (dw.function_name_matches_pattern (probe_name, pp_mark)
7066 && ((pp_provider == "")
7067 || dw.function_name_matches_pattern (provider_name, pp_provider))))
7068 return;
7069
e9a90eee
JS
7070 // PR13934: Assembly probes are not forced to use the N@OP form.
7071 // If we have '@' then great, else count based on space-delimiters.
7072 arg_count = count(arg_string.begin(), arg_string.end(), '@');
7073 if (!arg_count && !arg_string.empty())
7074 arg_count = 1 + count(arg_string.begin(), arg_string.end(), ' ');
7075
40fe32e0
SC
7076 GElf_Addr base_ref;
7077 if (gelf_getclass (elf) == ELFCLASS32)
7078 {
7079 pc = buf.a32[0];
7080 base_ref = buf.a32[1];
7081 semaphore = buf.a32[2];
7082 }
7083 else
7084 {
7085 pc = buf.a64[0];
7086 base_ref = buf.a64[1];
7087 semaphore = buf.a64[2];
7088 }
7089
7090 semaphore += base - base_ref;
7091 pc += base - base_ref;
7092
7d395255
JS
7093 // The semaphore also needs the ELF bias added now, so
7094 // record_semaphore can properly relocate it later.
7095 semaphore += bias;
7096
40fe32e0 7097 if (sess.verbose > 4)
b530b5b3 7098 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
7099
7100 handle_probe_entry();
7101}
7102
7103
c57ea854
SC
7104void
7105sdt_query::iterate_over_probe_entries()
edce5b67 7106{
c57ea854 7107 // probes are in the .probe section
edce5b67
JS
7108 while (probe_scn_offset < pdata->d_size)
7109 {
aff5d390
SC
7110 stap_sdt_probe_entry_v1 *pbe_v1 = (stap_sdt_probe_entry_v1 *) ((char*)pdata->d_buf + probe_scn_offset);
7111 stap_sdt_probe_entry_v2 *pbe_v2 = (stap_sdt_probe_entry_v2 *) ((char*)pdata->d_buf + probe_scn_offset);
15284963 7112 probe_type = (stap_sdt_probe_type)(pbe_v1->type_a);
1cc41cd6 7113 if (! have_uprobe())
edce5b67
JS
7114 {
7115 // Unless this is a mangled .probes section, this happens
7116 // because the name of the probe comes first, followed by
7117 // the sentinel.
7118 if (sess.verbose > 5)
b530b5b3 7119 clog << _F("got unknown probe_type : 0x%x", probe_type) << endl;
edce5b67
JS
7120 probe_scn_offset += sizeof(__uint32_t);
7121 continue;
7122 }
aff5d390
SC
7123 if ((long)pbe_v1 % sizeof(__uint64_t)) // we have stap_sdt_probe_entry_v1.type_b
7124 {
7125 pbe_v1 = (stap_sdt_probe_entry_v1*)((char*)pbe_v1 - sizeof(__uint32_t));
1cc41cd6 7126 if (pbe_v1->type_b != uprobe1_type)
aff5d390
SC
7127 continue;
7128 }
7129
1cc41cd6 7130 if (probe_type == uprobe1_type)
aff5d390 7131 {
79a0ca08 7132 if (pbe_v1->name == 0) // No name possibly means we have a .so with a relocation
c57ea854 7133 return;
79a0ca08 7134 semaphore = 0;
aff5d390 7135 probe_name = (char*)((char*)pdata->d_buf + pbe_v1->name - (char*)probe_scn_addr);
a794dbeb 7136 provider_name = ""; // unknown
1cc41cd6
DS
7137 pc = pbe_v1->arg;
7138 arg_count = 0;
aff5d390
SC
7139 probe_scn_offset += sizeof (stap_sdt_probe_entry_v1);
7140 }
08b5a50c 7141 else if (probe_type == uprobe2_type)
aff5d390 7142 {
79a0ca08 7143 if (pbe_v2->name == 0) // No name possibly means we have a .so with a relocation
c57ea854 7144 return;
79a0ca08 7145 semaphore = pbe_v2->semaphore;
aff5d390 7146 probe_name = (char*)((char*)pdata->d_buf + pbe_v2->name - (char*)probe_scn_addr);
a794dbeb 7147 provider_name = (char*)((char*)pdata->d_buf + pbe_v2->provider - (char*)probe_scn_addr);
aff5d390
SC
7148 arg_count = pbe_v2->arg_count;
7149 pc = pbe_v2->pc;
7150 if (pbe_v2->arg_string)
7151 arg_string = (char*)((char*)pdata->d_buf + pbe_v2->arg_string - (char*)probe_scn_addr);
79a0ca08
SC
7152 // skip over pbe_v2, probe_name text and provider text
7153 probe_scn_offset = ((long)(pbe_v2->name) - (long)(probe_scn_addr)) + probe_name.length();
7154 probe_scn_offset += sizeof (__uint32_t) - probe_scn_offset % sizeof (__uint32_t);
aff5d390 7155 }
edce5b67 7156 if (sess.verbose > 4)
b530b5b3 7157 clog << _("saw .probes ") << probe_name << (provider_name != "" ? _(" (provider ")+provider_name+") " : "")
aff5d390 7158 << "@0x" << hex << pc << dec << endl;
edce5b67 7159
e90006d7
JL
7160 dw.mod_info->marks.insert(make_pair(provider_name, probe_name));
7161
a794dbeb
FCE
7162 if (dw.function_name_matches_pattern (probe_name, pp_mark)
7163 && ((pp_provider == "") || dw.function_name_matches_pattern (provider_name, pp_provider)))
c57ea854 7164 handle_probe_entry ();
edce5b67 7165 }
edce5b67
JS
7166}
7167
7168
6846cfc8 7169void
4ddb6dd0 7170sdt_query::record_semaphore (vector<derived_probe *> & results, unsigned start)
6846cfc8 7171{
a794dbeb
FCE
7172 for (unsigned i=0; i<2; i++) {
7173 // prefer with-provider symbol; look without provider prefix for backward compatibility only
7174 string semaphore = (i==0 ? (provider_name+"_") : "") + probe_name + "_semaphore";
7175 // XXX: multiple addresses?
7176 if (sess.verbose > 2)
b530b5b3 7177 clog << _F("looking for semaphore symbol %s ", semaphore.c_str());
a794dbeb 7178
79a0ca08
SC
7179 Dwarf_Addr addr;
7180 if (this->semaphore)
7181 addr = this->semaphore;
7182 else
7183 addr = lookup_symbol_address(dw.module, semaphore.c_str());
a794dbeb
FCE
7184 if (addr)
7185 {
7d395255 7186 if (dwfl_module_relocations (dw.module) > 0)
a794dbeb
FCE
7187 dwfl_module_relocate_address (dw.module, &addr);
7188 // XXX: relocation basis?
909ab234
JS
7189
7190 // Dyninst needs the *file*-based offset for semaphores,
7191 // so subtract the difference in load addresses between .text and .probes
7192 if (dw.sess.runtime_usermode_p())
7193 addr -= semaphore_load_offset;
7194
a794dbeb
FCE
7195 for (unsigned i = start; i < results.size(); ++i)
7196 results[i]->sdt_semaphore_addr = addr;
7197 if (sess.verbose > 2)
b530b5b3 7198 clog << _(", found at 0x") << hex << addr << dec << endl;
a794dbeb
FCE
7199 return;
7200 }
7201 else
7202 if (sess.verbose > 2)
b530b5b3 7203 clog << _(", not found") << endl;
a794dbeb 7204 }
6846cfc8
SC
7205}
7206
7207
edce5b67
JS
7208void
7209sdt_query::convert_probe (probe *base)
7210{
7211 block *b = new block;
7212 b->tok = base->body->tok;
7213
edce5b67
JS
7214 // Generate: if (arg1 != mark("label")) next;
7215 functioncall *fc = new functioncall;
bbafcb1e 7216 fc->function = "ulong_arg";
edce5b67 7217 fc->tok = b->tok;
bbafcb1e 7218 literal_number* num = new literal_number(1);
edce5b67
JS
7219 num->tok = b->tok;
7220 fc->args.push_back(num);
7221
7222 functioncall *fcus = new functioncall;
7223 fcus->function = "user_string";
7224 fcus->type = pe_string;
7225 fcus->tok = b->tok;
7226 fcus->args.push_back(fc);
7227
7228 if_statement *is = new if_statement;
7229 is->thenblock = new next_statement;
7230 is->elseblock = NULL;
7231 is->tok = b->tok;
63ea4244 7232 is->thenblock->tok = b->tok;
edce5b67
JS
7233 comparison *be = new comparison;
7234 be->op = "!=";
7235 be->tok = b->tok;
7236 be->left = fcus;
7237 be->right = new literal_string(probe_name);
63ea4244 7238 be->right->tok = b->tok;
edce5b67
JS
7239 is->condition = be;
7240 b->statements.push_back(is);
7241
7242 // Now replace the body
7243 b->statements.push_back(base->body);
7244 base->body = b;
7245}
7246
7247
c72aa911
JS
7248probe*
7249sdt_query::convert_location ()
edce5b67 7250{
684b8718
JL
7251 string module = dw.module_name;
7252 if (has_process)
7253 module = path_remove_sysroot(sess, module);
7254
c72aa911 7255 probe_point* specific_loc = new probe_point(*base_loc);
684b8718
JL
7256 specific_loc->well_formed = true;
7257
662539d9 7258 vector<probe_point::component*> derived_comps;
edce5b67 7259
662539d9
JS
7260 vector<probe_point::component*>::iterator it;
7261 for (it = specific_loc->components.begin();
7262 it != specific_loc->components.end(); ++it)
7263 if ((*it)->functor == TOK_PROCESS)
7264 {
684b8718
JL
7265 // replace the possibly incomplete path to process
7266 *it = new probe_point::component(TOK_PROCESS,
7267 new literal_string(has_library ? path : module));
7268
1cc41cd6
DS
7269 // copy the process name
7270 derived_comps.push_back(*it);
662539d9
JS
7271 }
7272 else if ((*it)->functor == TOK_LIBRARY)
7273 {
1cc41cd6
DS
7274 // copy the library name for process probes
7275 derived_comps.push_back(*it);
662539d9
JS
7276 }
7277 else if ((*it)->functor == TOK_PROVIDER)
7278 {
7279 // replace the possibly wildcarded arg with the specific provider name
7280 *it = new probe_point::component(TOK_PROVIDER,
7281 new literal_string(provider_name));
7282 }
7283 else if ((*it)->functor == TOK_MARK)
c72aa911
JS
7284 {
7285 // replace the possibly wildcarded arg with the specific marker name
662539d9
JS
7286 *it = new probe_point::component(TOK_MARK,
7287 new literal_string(probe_name));
a794dbeb 7288
aff5d390
SC
7289 if (sess.verbose > 3)
7290 switch (probe_type)
7291 {
7292 case uprobe1_type:
b530b5b3 7293 clog << _("probe_type == uprobe1, use statement addr: 0x")
aff5d390
SC
7294 << hex << pc << dec << endl;
7295 break;
7296 case uprobe2_type:
b530b5b3 7297 clog << _("probe_type == uprobe2, use statement addr: 0x")
aff5d390
SC
7298 << hex << pc << dec << endl;
7299 break;
40fe32e0 7300 case uprobe3_type:
b530b5b3 7301 clog << _("probe_type == uprobe3, use statement addr: 0x")
40fe32e0
SC
7302 << hex << pc << dec << endl;
7303 break;
aff5d390 7304 default:
b530b5b3
LB
7305 clog << _F("probe_type == use_uprobe_no_dwarf, use label name: _stapprobe1_%s",
7306 pp_mark.c_str()) << endl;
aff5d390
SC
7307 }
7308
c72aa911
JS
7309 switch (probe_type)
7310 {
aff5d390
SC
7311 case uprobe1_type:
7312 case uprobe2_type:
40fe32e0 7313 case uprobe3_type:
c72aa911 7314 // process("executable").statement(probe_arg)
662539d9
JS
7315 derived_comps.push_back
7316 (new probe_point::component(TOK_STATEMENT,
7317 new literal_number(pc, true)));
c72aa911
JS
7318 break;
7319
a794dbeb 7320 default: // deprecated
c72aa911 7321 // process("executable").function("*").label("_stapprobe1_MARK_NAME")
662539d9
JS
7322 derived_comps.push_back
7323 (new probe_point::component(TOK_FUNCTION,
7324 new literal_string("*")));
7325 derived_comps.push_back
c72aa911 7326 (new probe_point::component(TOK_LABEL,
a794dbeb 7327 new literal_string("_stapprobe1_" + pp_mark)));
c72aa911
JS
7328 break;
7329 }
7330 }
edce5b67 7331
662539d9
JS
7332 probe_point* derived_loc = new probe_point(*specific_loc);
7333 derived_loc->components = derived_comps;
8159bf55 7334 return new probe (new probe (base_probe, specific_loc), derived_loc);
edce5b67
JS
7335}
7336
7337
51d6bda3
SC
7338void
7339sdt_query::query_library (const char *library)
7340{
614f0fcf
JL
7341 visited_libraries.insert(library);
7342 if (query_one_library (library, dw, user_lib, base_probe, base_loc, results))
7343 resolved_library = true;
51d6bda3
SC
7344}
7345
e90006d7
JL
7346string
7347suggest_marks(systemtap_session& sess,
7348 const set<string>& modules,
7349 const string& mark,
7350 const string& provider)
7351{
7352 if (mark.empty() || modules.empty() || sess.module_cache == NULL)
7353 return "";
7354
7355 set<string> marks;
7356 const map<string, module_info*> &cache = sess.module_cache->cache;
7357 bool dash_suggestions = (mark.find("-") != string::npos);
7358
7359 for (set<string>::iterator itmod = modules.begin();
7360 itmod != modules.end(); ++itmod)
7361 {
7362 map<string, module_info*>::const_iterator itcache;
7363 if ((itcache = cache.find(*itmod)) != cache.end())
7364 {
7365 set<pair<string,string> >::const_iterator itmarks;
7366 for (itmarks = itcache->second->marks.begin();
7367 itmarks != itcache->second->marks.end(); ++itmarks)
7368 {
7369 if (provider.empty()
7370 // simulating dw.function_name_matches_pattern()
7371 || (fnmatch(provider.c_str(), itmarks->first.c_str(), 0) == 0))
7372 {
7373 string marksug = itmarks->second;
7374 if (dash_suggestions)
7375 {
7376 size_t pos = 0;
7377 while (1) // there may be more than one
7378 {
7379 size_t i = marksug.find("__", pos);
7380 if (i == string::npos) break;
7381 marksug.replace (i, 2, "-");
7382 pos = i+1; // resume searching after the inserted -
7383 }
7384 }
7385 marks.insert(marksug);
7386 }
7387 }
7388 }
7389 }
7390
7391 if (sess.verbose > 2)
7392 {
7393 clog << "suggesting " << marks.size() << " marks "
7394 << "from modules:" << endl;
7395 for (set<string>::iterator itmod = modules.begin();
7396 itmod != modules.end(); ++itmod)
7397 clog << *itmod << endl;
7398 }
7399
7400 if (marks.empty())
7401 return "";
7402
7403 return levenshtein_suggest(mark, marks, 5); // print top 5 marks only
7404}
7405
40a70f52
JL
7406string
7407suggest_plt_functions(systemtap_session& sess,
7408 const set<string>& modules,
7409 const string& func)
7410{
7411 if (func.empty() || modules.empty() || sess.module_cache == NULL)
7412 return "";
7413
7414 set<string> funcs;
7415 const map<string, module_info*> &cache = sess.module_cache->cache;
7416
7417 for (set<string>::iterator itmod = modules.begin();
7418 itmod != modules.end(); ++itmod)
7419 {
7420 map<string, module_info*>::const_iterator itcache;
7421 if ((itcache = cache.find(*itmod)) != cache.end())
7422 funcs.insert(itcache->second->plt_funcs.begin(),
7423 itcache->second->plt_funcs.end());
7424 }
7425
7426 if (sess.verbose > 2)
d82dd71f
JL
7427 {
7428 clog << "suggesting " << funcs.size() << " plt functions "
7429 << "from modules:" << endl;
7430 for (set<string>::iterator itmod = modules.begin();
7431 itmod != modules.end(); ++itmod)
7432 clog << *itmod << endl;
7433 }
40a70f52
JL
7434
7435 if (funcs.empty())
7436 return "";
7437
7438 return levenshtein_suggest(func, funcs, 5); // print top 5 funcs only
7439}
51d6bda3 7440
0508c45f
JL
7441string
7442suggest_dwarf_functions(systemtap_session& sess,
7443 const set<string>& modules,
7444 string func)
44ffe90c
JL
7445{
7446 // Trim any @ component
7447 size_t pos = func.find('@');
7448 if (pos != string::npos)
7449 func.erase(pos);
7450
0508c45f 7451 if (func.empty() || modules.empty() || sess.module_cache == NULL)
44ffe90c
JL
7452 return "";
7453
7454 // We must first aggregate all the functions from the cache
7455 set<string> funcs;
7456 const map<string, module_info*> &cache = sess.module_cache->cache;
7457
0508c45f
JL
7458 for (set<string>::iterator itmod = modules.begin();
7459 itmod != modules.end(); ++itmod)
44ffe90c 7460 {
6e6d9a3b
JL
7461 module_info *module;
7462
7463 // retrieve module_info from cache
44ffe90c
JL
7464 map<string, module_info*>::const_iterator itcache;
7465 if ((itcache = cache.find(*itmod)) != cache.end())
6e6d9a3b
JL
7466 module = itcache->second;
7467 else // module not found
7468 continue;
7469
7470 // add inlines
7471 funcs.insert(module->inlined_funcs.begin(),
7472 module->inlined_funcs.end());
7473
7474 // add all function symbols in cache
7475 if (module->symtab_status != info_present || module->sym_table == NULL)
7476 continue;
7477 map<string, func_info*>& modfuncs = module->sym_table->map_by_name;
7478 for (map<string, func_info*>::const_iterator itfuncs = modfuncs.begin();
7479 itfuncs != modfuncs.end(); ++itfuncs)
7480 funcs.insert(itfuncs->first);
44ffe90c
JL
7481 }
7482
7483 if (sess.verbose > 2)
d82dd71f
JL
7484 {
7485 clog << "suggesting " << funcs.size() << " dwarf functions "
7486 << "from modules:" << endl;
7487 for (set<string>::iterator itmod = modules.begin();
7488 itmod != modules.end(); ++itmod)
7489 clog << *itmod << endl;
7490 }
44ffe90c
JL
7491
7492 if (funcs.empty())
7493 return "";
7494
593f09eb 7495 return levenshtein_suggest(func, funcs, 5); // print top 5 funcs only
44ffe90c
JL
7496}
7497
20c6c071 7498void
5227f1ea 7499dwarf_builder::build(systemtap_session & sess,
7a053d3b 7500 probe * base,
20c6c071 7501 probe_point * location,
86bf665e 7502 literal_map_t const & parameters,
20c6c071
GH
7503 vector<derived_probe *> & finished_results)
7504{
b20febf3
FCE
7505 // NB: the kernel/user dwlfpp objects are long-lived.
7506 // XXX: but they should be per-session, as this builder object
7507 // may be reused if we try to cross-instrument multiple targets.
84048984 7508
7a24d422 7509 dwflpp* dw = 0;
6d5d594e 7510 literal_map_t filled_parameters = parameters;
7a24d422 7511
7a24d422 7512 string module_name;
5c6f9e92 7513 int64_t proc_pid;
ae2552da
FCE
7514 if (has_null_param (parameters, TOK_KERNEL))
7515 {
7516 dw = get_kern_dw(sess, "kernel");
7517 }
7518 else if (get_param (parameters, TOK_MODULE, module_name))
b8da0ad1 7519 {
8e30940d
JL
7520 // If not a full path was given, then it's an in-tree module. Replace any
7521 // dashes with underscores.
7522 if (!is_fully_resolved(module_name, sess.sysroot, sess.sysenv))
7523 {
7524 size_t dash_pos = 0;
7525 while((dash_pos=module_name.find('-'))!=string::npos)
7526 module_name.replace(int(dash_pos),1,"_");
7527 filled_parameters[TOK_MODULE] = new literal_string(module_name);
7528 }
7529
37001baa
FCE
7530 // NB: glob patterns get expanded later, during the offline
7531 // elfutils module listing.
ae2552da 7532 dw = get_kern_dw(sess, module_name);
b8da0ad1 7533 }
5c6f9e92 7534 else if (has_param(filled_parameters, TOK_PROCESS))
84c84ac4 7535 {
0ce4c32f
FCE
7536 // NB: module_name is not yet set!
7537
6d5d594e
LB
7538 if(has_null_param(filled_parameters, TOK_PROCESS))
7539 {
fc18e6c4
JL
7540 string file;
7541 try
7542 {
7543 file = sess.cmd_file();
7544 }
7545 catch (const semantic_error& e)
7546 {
6d9c4e46 7547 if(sess.target_pid)
1b6d5734 7548 throw SEMANTIC_ERROR(_("invalid -x pid for unspecified process"
6d9c4e46
AJ
7549 " probe [man stapprobes]"), NULL, NULL, &e);
7550 else
7551 throw SEMANTIC_ERROR(_("invalid -c command for unspecified process"
fc18e6c4
JL
7552 " probe [man stapprobes]"), NULL, NULL, &e);
7553 }
48cd804a 7554 if(file.empty())
fc18e6c4 7555 throw SEMANTIC_ERROR(_("unspecified process probe is invalid without"
6d9c4e46 7556 " a -c COMMAND or -x PID [man stapprobes]"));
48cd804a 7557 module_name = sess.sysroot + file;
6d5d594e 7558 filled_parameters[TOK_PROCESS] = new literal_string(module_name);// this needs to be used in place of the blank map
0ce4c32f 7559 // in the case of TOK_MARK we need to modify locations as well // XXX why?
6d5d594e 7560 if(location->components[0]->functor==TOK_PROCESS &&
0ce4c32f 7561 location->components[0]->arg == 0)
30b865ce
AJ
7562 {
7563 if (sess.target_pid)
7564 location->components[0]->arg = new literal_number(sess.target_pid);
7565 else
7566 location->components[0]->arg = new literal_string(module_name);
7567 }
5c6f9e92
AJ
7568 }
7569
0ce4c32f
FCE
7570 // NB: must specifically handle the classical ("string") form here, to make sure
7571 // we get the module_name out.
7572 else if (get_param (parameters, TOK_PROCESS, module_name))
7573 {
7574 module_name = sess.sysroot + module_name;
7575 filled_parameters[TOK_PROCESS] = new literal_string(module_name);
7576 }
7577
5c6f9e92
AJ
7578 else if (get_param (parameters, TOK_PROCESS, proc_pid))
7579 {
6c484277 7580 // check that the pid given corresponds to a running process
30b865ce 7581 string pid_err_msg;
fb9ae721 7582 if (!is_valid_pid(proc_pid, pid_err_msg))
30b865ce 7583 throw SEMANTIC_ERROR(pid_err_msg);
6c484277 7584
5c6f9e92
AJ
7585 string pid_path = string("/proc/") + lex_cast(proc_pid) + "/exe";
7586 module_name = sess.sysroot + pid_path;
37635b45 7587
0ce4c32f 7588 // in the case of TOK_MARK we need to modify locations as well // XXX why?
6d5d594e 7589 if(location->components[0]->functor==TOK_PROCESS &&
0ce4c32f 7590 location->components[0]->arg == 0)
30b865ce 7591 location->components[0]->arg = new literal_number(sess.target_pid);
b78a0fbb 7592 }
5750ecc6 7593
37001baa
FCE
7594 // PR6456 process("/bin/*") glob handling
7595 if (contains_glob_chars (module_name))
7596 {
7597 // Expand glob via rewriting the probe-point process("....")
7598 // parameter, asserted to be the first one.
7599
7600 assert (location->components.size() > 0);
7601 assert (location->components[0]->functor == TOK_PROCESS);
7602 assert (location->components[0]->arg);
7603 literal_string* lit = dynamic_cast<literal_string*>(location->components[0]->arg);
7604 assert (lit);
7605
7606 // Evaluate glob here, and call derive_probes recursively with each match.
7607 glob_t the_blob;
88f8e2c0 7608 set<string> dupes;
37001baa 7609 int rc = glob (module_name.c_str(), 0, NULL, & the_blob);
b530b5b3 7610 if (rc)
dc09353a 7611 throw SEMANTIC_ERROR (_F("glob %s error (%s)", module_name.c_str(), lex_cast(rc).c_str() ));
44ffe90c 7612 unsigned results_pre = finished_results.size();
37001baa
FCE
7613 for (unsigned i = 0; i < the_blob.gl_pathc; ++i)
7614 {
e19ebcf7 7615 assert_no_interrupts();
37001baa
FCE
7616
7617 const char* globbed = the_blob.gl_pathv[i];
7618 struct stat st;
7619
7620 if (access (globbed, X_OK) == 0
7621 && stat (globbed, &st) == 0
7622 && S_ISREG (st.st_mode)) // see find_executable()
7623 {
7977a734
FCE
7624 // Need to call canonicalize here, in order to path-expand
7625 // patterns like process("stap*"). Otherwise it may go through
7626 // to the next round of expansion as ("stap"), leading to a $PATH
7627 // search that's not consistent with the glob search already done.
5bca76a8
JS
7628 string canononicalized = resolve_path (globbed);
7629 globbed = canononicalized.c_str();
7977a734 7630
88f8e2c0
JS
7631 // The canonical names can result in duplication, for example
7632 // having followed symlinks that are common with shared
7633 // libraries. Filter those out.
7634 if (!dupes.insert(canononicalized).second)
7635 continue;
7636
37001baa
FCE
7637 // synthesize a new probe_point, with the glob-expanded string
7638 probe_point *pp = new probe_point (*location);
ef0943df
JL
7639 pp->from_glob = true;
7640
5750ecc6
FCE
7641 // PR13338: quote results to prevent recursion
7642 string eglobbed = escape_glob_chars (globbed);
7643
7644 if (sess.verbose > 1)
7645 clog << _F("Expanded process(\"%s\") to process(\"%s\")",
7646 module_name.c_str(), eglobbed.c_str()) << endl;
05fb3e0c 7647 string eglobbed_tgt = path_remove_sysroot(sess, eglobbed);
5750ecc6 7648
37001baa 7649 probe_point::component* ppc = new probe_point::component (TOK_PROCESS,
05fb3e0c 7650 new literal_string (eglobbed_tgt));
37001baa
FCE
7651 ppc->tok = location->components[0]->tok; // overwrite [0] slot, pattern matched above
7652 pp->components[0] = ppc;
7653
d885563b 7654 probe* new_probe = new probe (base, pp);
7977a734
FCE
7655
7656 // We override "optional = true" here, as if the
7657 // wildcarded probe point was given a "?" suffix.
7658
7659 // This is because wildcard probes will be expected
7660 // by users to apply only to some subset of the
7661 // matching binaries, in the sense of "any", rather
7662 // than "all", sort of similarly how
7663 // module("*").function("...") patterns work.
7664
7665 derive_probes (sess, new_probe, finished_results,
7666 true /* NB: not location->optional */ );
37001baa
FCE
7667 }
7668 }
7669
7670 globfree (& the_blob);
44ffe90c
JL
7671
7672 unsigned results_post = finished_results.size();
7673
e90006d7 7674 // Did we fail to find a function/plt/mark by name? Let's suggest
44ffe90c
JL
7675 // something!
7676 string func;
7677 if (results_pre == results_post
7678 && get_param(filled_parameters, TOK_FUNCTION, func)
7679 && !func.empty())
7680 {
0508c45f 7681 string sugs = suggest_dwarf_functions(sess, modules_seen, func);
44ffe90c
JL
7682 modules_seen.clear();
7683 if (!sugs.empty())
dc09353a 7684 throw SEMANTIC_ERROR (_NF("no match (similar function: %s)",
44ffe90c
JL
7685 "no match (similar functions: %s)",
7686 sugs.find(',') == string::npos,
7687 sugs.c_str()));
7688 }
40a70f52
JL
7689 else if (results_pre == results_post
7690 && get_param(filled_parameters, TOK_PLT, func)
7691 && !func.empty())
7692 {
7693 string sugs = suggest_plt_functions(sess, modules_seen, func);
7694 modules_seen.clear();
7695 if (!sugs.empty())
7696 throw SEMANTIC_ERROR (_NF("no match (similar function: %s)",
7697 "no match (similar functions: %s)",
7698 sugs.find(',') == string::npos,
7699 sugs.c_str()));
7700 }
e90006d7
JL
7701 else if (results_pre == results_post
7702 && get_param(filled_parameters, TOK_MARK, func)
7703 && !func.empty())
7704 {
7705 string provider;
7706 get_param(filled_parameters, TOK_PROVIDER, provider);
7707
7708 string sugs = suggest_marks(sess, modules_seen, func, provider);
7709 modules_seen.clear();
7710 if (!sugs.empty())
7711 throw SEMANTIC_ERROR (_NF("no match (similar mark: %s)",
7712 "no match (similar marks: %s)",
7713 sugs.find(',') == string::npos,
7714 sugs.c_str()));
7715 }
44ffe90c 7716
37001baa
FCE
7717 return; // avoid falling through
7718 }
7719
5750ecc6
FCE
7720 // PR13338: unquote glob results
7721 module_name = unescape_glob_chars (module_name);
05fb3e0c 7722 user_path = find_executable (module_name, "", sess.sysenv); // canonicalize it
5fa0d811
JL
7723 if (!is_fully_resolved(user_path, "", sess.sysenv))
7724 throw SEMANTIC_ERROR(_F("cannot find executable '%s'",
7725 user_path.c_str()));
d1bcbe71
RH
7726
7727 // if the executable starts with "#!", we look for the interpreter of the script
7728 {
7729 ifstream script_file (user_path.c_str () );
7730
7731 if (script_file.good ())
7732 {
7733 string line;
7734
7735 getline (script_file, line);
7736
7737 if (line.compare (0, 2, "#!") == 0)
7738 {
7739 string path_head = line.substr(2);
7740
7741 // remove white spaces at the beginning of the string
7742 size_t p2 = path_head.find_first_not_of(" \t");
7743
7744 if (p2 != string::npos)
7745 {
7746 string path = path_head.substr(p2);
7747
7748 // remove white spaces at the end of the string
7749 p2 = path.find_last_not_of(" \t\n");
7750 if (string::npos != p2)
7751 path.erase(p2+1);
7752
8e13c1a1
RH
7753 // handle "#!/usr/bin/env" redirect
7754 size_t offset = 0;
7755 if (path.compare(0, sizeof("/bin/env")-1, "/bin/env") == 0)
7756 {
7757 offset = sizeof("/bin/env")-1;
7758 }
7759 else if (path.compare(0, sizeof("/usr/bin/env")-1, "/usr/bin/env") == 0)
7760 {
7761 offset = sizeof("/usr/bin/env")-1;
7762 }
7763
7764 if (offset != 0)
7765 {
7766 size_t p3 = path.find_first_not_of(" \t", offset);
7767
7768 if (p3 != string::npos)
7769 {
7770 string env_path = path.substr(p3);
05fb3e0c
WF
7771 user_path = find_executable (env_path, sess.sysroot,
7772 sess.sysenv);
8e13c1a1
RH
7773 }
7774 }
7775 else
7776 {
05fb3e0c 7777 user_path = find_executable (path, sess.sysroot, sess.sysenv);
8e13c1a1 7778 }
d1bcbe71
RH
7779
7780 struct stat st;
7781
7782 if (access (user_path.c_str(), X_OK) == 0
7783 && stat (user_path.c_str(), &st) == 0
7784 && S_ISREG (st.st_mode)) // see find_executable()
7785 {
7786 if (sess.verbose > 1)
b530b5b3
LB
7787 clog << _F("Expanded process(\"%s\") to process(\"%s\")",
7788 module_name.c_str(), user_path.c_str()) << endl;
d1bcbe71
RH
7789
7790 assert (location->components.size() > 0);
7791 assert (location->components[0]->functor == TOK_PROCESS);
7792 assert (location->components[0]->arg);
7793 literal_string* lit = dynamic_cast<literal_string*>(location->components[0]->arg);
7794 assert (lit);
7795
7796 // synthesize a new probe_point, with the expanded string
7797 probe_point *pp = new probe_point (*location);
05fb3e0c 7798 string user_path_tgt = path_remove_sysroot(sess, user_path);
d1bcbe71 7799 probe_point::component* ppc = new probe_point::component (TOK_PROCESS,
05fb3e0c 7800 new literal_string (user_path_tgt.c_str()));
d1bcbe71
RH
7801 ppc->tok = location->components[0]->tok; // overwrite [0] slot, pattern matched above
7802 pp->components[0] = ppc;
7803
d885563b 7804 probe* new_probe = new probe (base, pp);
d1bcbe71
RH
7805
7806 derive_probes (sess, new_probe, finished_results);
7807
7808 script_file.close();
7809 return;
7810 }
7811 }
7812 }
7813 }
7814 script_file.close();
7815 }
7816
dbe9d133
JL
7817 // If this is a library probe, then target the library module instead. We
7818 // do this only if the library path is already fully resolved (such as
7819 // what query_one_library() would have done for us). Otherwise, we resort
7820 // to iterate_over_libraries.
7821 if (get_param (parameters, TOK_LIBRARY, user_lib) && !user_lib.empty()
7822 && is_fully_resolved(user_lib, sess.sysroot, sess.sysenv, "LD_LIBRARY_PATH"))
7823 module_name = user_lib;
63b4fd14 7824 else
dbe9d133 7825 module_name = user_path; // canonicalize it
d0a7f5a9 7826
3667d615 7827 // uretprobes aren't available everywhere
536f1261 7828 if (has_null_param(parameters, TOK_RETURN) && !sess.runtime_usermode_p())
2b69faaf 7829 {
3667d615
JS
7830 if (kernel_supports_inode_uprobes(sess) &&
7831 !kernel_supports_inode_uretprobes(sess))
dc09353a 7832 throw SEMANTIC_ERROR
3ffeaf3c 7833 (_("process return probes not available [man error::inode-uprobes]"));
2b69faaf 7834 }
3667d615 7835
f4000852
MW
7836 // There is a similar check in pass 4 (buildrun), but it is
7837 // needed here too to make sure alternatives for optional
7838 // (? or !) process probes are disposed and/or alternatives
7839 // are selected.
3667d615
JS
7840 if (!sess.runtime_usermode_p())
7841 check_process_probe_kernel_support(sess);
e34d5d13 7842
7a24d422
FCE
7843 // user-space target; we use one dwflpp instance per module name
7844 // (= program or shared library)
707bf35e 7845 dw = get_user_dw(sess, module_name);
c8959a29 7846 }
20c6c071 7847
1acfc030
JS
7848 assert(dw);
7849
e90006d7
JL
7850 unsigned results_pre = finished_results.size();
7851
5896cd05 7852 if (sess.verbose > 3)
b530b5b3 7853 clog << _F("dwarf_builder::build for %s", module_name.c_str()) << endl;
5896cd05 7854
a794dbeb
FCE
7855 string dummy_mark_name; // NB: PR10245: dummy value, need not substitute - => __
7856 if (get_param(parameters, TOK_MARK, dummy_mark_name))
f28a8c28 7857 {
51d6bda3 7858 sdt_query sdtq(base, location, *dw, filled_parameters, finished_results, user_lib);
06de3a04 7859 dw->iterate_over_modules<base_query>(&query_module, &sdtq);
e90006d7
JL
7860
7861 // We need to update modules_seen with the modules we've visited
7862 modules_seen.insert(sdtq.visited_modules.begin(),
7863 sdtq.visited_modules.end());
7864
614f0fcf
JL
7865 string lib;
7866 if (results_pre == finished_results.size() && !sdtq.resolved_library
7867 && get_param(filled_parameters, TOK_LIBRARY, lib)
7868 && !lib.empty() && !sdtq.visited_libraries.empty())
7869 {
7870 // The library didn't fit any DT_NEEDED libraries. As a last effort,
7871 // let's try to look for the library directly.
7872 string resolved_lib = find_executable(lib, sess.sysroot, sess.sysenv,
7873 "LD_LIBRARY_PATH");
7874 if (resolved_lib.find('/') != string::npos)
7875 {
7876 probe *new_base = build_library_probe(*dw, resolved_lib,
7877 base, location);
7878 derive_probes(sess, new_base, finished_results);
7879 sess.print_warning(_F("'%s' is not a needed library of '%s'. "
7880 "Specify the full path to squelch this warning.",
7881 resolved_lib.c_str(), dw->module_name.c_str()));
7882 return;
7883 }
f23e4abe
JL
7884
7885 // Otherwise, let's suggest from the DT_NEEDED libraries
7886 string sugs = levenshtein_suggest(lib, sdtq.visited_libraries, 5);
7887 if (!sugs.empty())
7888 throw SEMANTIC_ERROR (_NF("no match (similar library: %s)",
7889 "no match (similar libraries: %s)",
7890 sugs.find(',') == string::npos,
7891 sugs.c_str()));
614f0fcf
JL
7892 }
7893
e90006d7
JL
7894 // Did we fail to find a mark?
7895 if (results_pre == finished_results.size() && !location->from_glob)
7896 {
7897 string provider;
7898 get_param(filled_parameters, TOK_PROVIDER, provider);
7899
7900 string sugs = suggest_marks(sess, modules_seen, dummy_mark_name, provider);
7901 modules_seen.clear();
7902 if (!sugs.empty())
7903 throw SEMANTIC_ERROR (_NF("no match (similar mark: %s)",
7904 "no match (similar marks: %s)",
7905 sugs.find(',') == string::npos,
7906 sugs.c_str()));
7907 }
7908
edce5b67 7909 return;
7a05f484 7910 }
20c6c071 7911
6d5d594e 7912 dwarf_query q(base, location, *dw, filled_parameters, finished_results, user_path, user_lib);
7a24d422
FCE
7913
7914 // XXX: kernel.statement.absolute is a special case that requires no
7915 // dwfl processing. This code should be in a separate builder.
7a24d422 7916 if (q.has_kernel && q.has_absolute)
37ebca01 7917 {
4baf0e53 7918 // assert guru mode for absolute probes
37ebca01
FCE
7919 if (! q.base_probe->privileged)
7920 {
dc09353a 7921 throw SEMANTIC_ERROR (_("absolute statement probe in unprivileged script; need stap -g"),
edce5b67 7922 q.base_probe->tok);
37ebca01
FCE
7923 }
7924
7925 // For kernel.statement(NUM).absolute probe points, we bypass
7926 // all the debuginfo stuff: We just wire up a
7927 // dwarf_derived_probe right here and now.
4baf0e53 7928 dwarf_derived_probe* p =
b8da0ad1
FCE
7929 new dwarf_derived_probe ("", "", 0, "kernel", "",
7930 q.statement_num_val, q.statement_num_val,
7931 q, 0);
37ebca01 7932 finished_results.push_back (p);
1a0dbc5a 7933 sess.unwindsym_modules.insert ("kernel");
37ebca01
FCE
7934 return;
7935 }
7936
06de3a04 7937 dw->iterate_over_modules<base_query>(&query_module, &q);
8f14e444 7938
44ffe90c
JL
7939 // We need to update modules_seen with the modules we've visited
7940 modules_seen.insert(q.visited_modules.begin(),
7941 q.visited_modules.end());
8f14e444
FCE
7942
7943 // PR11553 special processing: .return probes requested, but
7944 // some inlined function instances matched.
7945 unsigned i_n_r = q.inlined_non_returnable.size();
7946 unsigned results_post = finished_results.size();
7947 if (i_n_r > 0)
7948 {
7949 if ((results_pre == results_post) && (! sess.suppress_warnings)) // no matches; issue warning
7950 {
7951 string quicklist;
7952 for (set<string>::iterator it = q.inlined_non_returnable.begin();
7953 it != q.inlined_non_returnable.end();
7954 it++)
7955 {
7956 quicklist += " " + (*it);
7957 if (quicklist.size() > 80) // heuristic, don't make an overlong report line
7958 {
7959 quicklist += " ...";
7960 break;
7961 }
7962 }
c57ea854 7963
52c2652f 7964 sess.print_warning (_NF("cannot probe .return of %u inlined function %s",
b530b5b3 7965 "cannot probe .return of %u inlined functions %s",
52c2652f 7966 quicklist.size(), i_n_r, quicklist.c_str()));
8f14e444
FCE
7967 // There will be also a "no matches" semantic error generated.
7968 }
7969 if (sess.verbose > 1)
52c2652f
NMA
7970 clog << _NF("skipped .return probe of %u inlined function",
7971 "skipped .return probe of %u inlined functions", i_n_r, i_n_r) << endl;
8f14e444
FCE
7972 if ((sess.verbose > 3) || (sess.verbose > 2 && results_pre == results_post)) // issue details with high verbosity
7973 {
7974 for (set<string>::iterator it = q.inlined_non_returnable.begin();
7975 it != q.inlined_non_returnable.end();
7976 it++)
7977 clog << (*it) << " ";
7978 clog << endl;
7979 }
7980 } // i_n_r > 0
44ffe90c 7981
614f0fcf
JL
7982 string lib;
7983 if (results_pre == results_post && !q.resolved_library
7984 && get_param(filled_parameters, TOK_LIBRARY, lib)
7985 && !lib.empty() && !q.visited_libraries.empty())
7986 {
7987 // The library didn't fit any DT_NEEDED libraries. As a last effort,
7988 // let's try to look for the library directly.
7989 string resolved_lib = find_executable(lib, sess.sysroot, sess.sysenv,
7990 "LD_LIBRARY_PATH");
7991 if (resolved_lib.find('/') != string::npos)
7992 {
7993 probe *new_base = build_library_probe(*dw, resolved_lib,
7994 base, location);
7995 derive_probes(sess, new_base, finished_results);
7996 sess.print_warning(_F("'%s' is not a needed library of '%s'. "
7997 "Specify the full path to squelch this warning.",
7998 resolved_lib.c_str(), dw->module_name.c_str()));
7999 return;
8000 }
f23e4abe
JL
8001
8002 // Otherwise, let's suggest from the DT_NEEDED libraries
8003 string sugs = levenshtein_suggest(lib, q.visited_libraries, 5);
8004 if (!sugs.empty())
8005 throw SEMANTIC_ERROR (_NF("no match (similar library: %s)",
8006 "no match (similar libraries: %s)",
8007 sugs.find(',') == string::npos,
8008 sugs.c_str()));
614f0fcf
JL
8009 }
8010
e90006d7 8011 // If we just failed to resolve a function/plt by name, we can suggest
ef0943df
JL
8012 // something. We only suggest things for probe points that were not
8013 // synthesized from a glob, i.e. only for 'real' probes. This is also
8014 // required because modules_seen needs to accumulate across recursive
8015 // calls for process(glob)[.library(glob)] probes.
44ffe90c 8016 string func;
ef0943df 8017 if (results_pre == results_post && !location->from_glob
44ffe90c
JL
8018 && get_param(filled_parameters, TOK_FUNCTION, func)
8019 && !func.empty())
8020 {
0508c45f 8021 string sugs = suggest_dwarf_functions(sess, modules_seen, func);
44ffe90c
JL
8022 modules_seen.clear();
8023 if (!sugs.empty())
dc09353a 8024 throw SEMANTIC_ERROR (_NF("no match (similar function: %s)",
44ffe90c
JL
8025 "no match (similar functions: %s)",
8026 sugs.find(',') == string::npos,
8027 sugs.c_str()));
8028 }
ef0943df 8029 else if (results_pre == results_post && !location->from_glob
40a70f52
JL
8030 && get_param(filled_parameters, TOK_PLT, func)
8031 && !func.empty())
8032 {
8033 string sugs = suggest_plt_functions(sess, modules_seen, func);
8034 modules_seen.clear();
8035 if (!sugs.empty())
8036 throw SEMANTIC_ERROR (_NF("no match (similar function: %s)",
8037 "no match (similar functions: %s)",
8038 sugs.find(',') == string::npos,
8039 sugs.c_str()));
8040 }
44ffe90c
JL
8041 else if (results_pre != results_post)
8042 // Something was derived so we won't need to suggest something
8043 modules_seen.clear();
5f0a03a6
JK
8044}
8045
8046symbol_table::~symbol_table()
8047{
c9efa5c9 8048 delete_map(map_by_addr);
5f0a03a6
JK
8049}
8050
8051void
2867a2a1 8052symbol_table::add_symbol(const char *name, bool weak, bool descriptor,
44ffe90c 8053 Dwarf_Addr addr, Dwarf_Addr* /*high_addr*/)
5f0a03a6 8054{
ab91b232
JK
8055#ifdef __powerpc__
8056 // Map ".sys_foo" to "sys_foo".
8057 if (name[0] == '.')
8058 name++;
8059#endif
5f0a03a6
JK
8060 func_info *fi = new func_info();
8061 fi->addr = addr;
8062 fi->name = name;
ab91b232 8063 fi->weak = weak;
2867a2a1 8064 fi->descriptor = descriptor;
5f0a03a6
JK
8065 map_by_name[fi->name] = fi;
8066 // TODO: Use a multimap in case there are multiple static
8067 // functions with the same name?
1c6b77e5 8068 map_by_addr.insert(make_pair(addr, fi));
5f0a03a6
JK
8069}
8070
46f7b6be 8071void
f98c6346 8072symbol_table::prepare_section_rejection(Dwfl_Module *mod __attribute__ ((unused)))
46f7b6be
JK
8073{
8074#ifdef __powerpc__
8075 /*
8076 * The .opd section contains function descriptors that can look
8077 * just like function entry points. For example, there's a function
8078 * descriptor called "do_exit" that links to the entry point ".do_exit".
8079 * Reject all symbols in .opd.
8080 */
8081 opd_section = SHN_UNDEF;
8082 Dwarf_Addr bias;
8083 Elf* elf = (dwarf_getelf (dwfl_module_getdwarf (mod, &bias))
8084 ?: dwfl_module_getelf (mod, &bias));
8085 Elf_Scn* scn = 0;
8086 size_t shstrndx;
8087
8088 if (!elf)
8089 return;
fcc30d6d 8090 if (elf_getshdrstrndx (elf, &shstrndx) != 0)
46f7b6be
JK
8091 return;
8092 while ((scn = elf_nextscn(elf, scn)) != NULL)
8093 {
8094 GElf_Shdr shdr_mem;
8095 GElf_Shdr *shdr = gelf_getshdr(scn, &shdr_mem);
8096 if (!shdr)
8097 continue;
8098 const char *name = elf_strptr(elf, shstrndx, shdr->sh_name);
8099 if (!strcmp(name, ".opd"))
8100 {
8101 opd_section = elf_ndxscn(scn);
8102 return;
8103 }
8104 }
8105#endif
8106}
8107
8108bool
8109symbol_table::reject_section(GElf_Word section)
8110{
8111 if (section == SHN_UNDEF)
8112 return true;
8113#ifdef __powerpc__
8114 if (section == opd_section)
8115 return true;
8116#endif
8117 return false;
8118}
8119
5f0a03a6
JK
8120enum info_status
8121symbol_table::get_from_elf()
8122{
8123 Dwarf_Addr high_addr = 0;
8124 Dwfl_Module *mod = mod_info->mod;
8125 int syments = dwfl_module_getsymtab(mod);
8126 assert(syments);
46f7b6be 8127 prepare_section_rejection(mod);
5f0a03a6
JK
8128 for (int i = 1; i < syments; ++i)
8129 {
8130 GElf_Sym sym;
ab91b232 8131 GElf_Word section;
cba67d8f
MW
8132 const char *name;
8133 GElf_Addr addr;
8134 bool reject;
8135
8136/* Note that dwfl_module_getsym does adjust the sym.st_value but doesn't
8137 try to resolve it to a function address. dwfl_module_getsym_info leaves
8138 the st_value in tact (no adjustment applied) and returns the fully
8139 resolved address separately. In that case we can simply reject the
8140 symbol if it is SHN_UNDEF and don't need to call reject_section which
8141 does extra checks to see whether the address fall in an architecture
8142 specific descriptor table (which will never be the case when using the
8143 new dwfl_module_getsym_info). dwfl_module_getsym will only provide us
8144 with the (adjusted) st_value of the symbol, which might point into a
8145 function descriptor table. So in that case we still have to call
8146 reject_section. */
8147#if _ELFUTILS_PREREQ (0, 158)
8148 name = dwfl_module_getsym_info (mod, i, &sym, &addr, &section,
8149 NULL, NULL);
8150 reject = section == SHN_UNDEF;
8151#else
8152 name = dwfl_module_getsym (mod, i, &sym, &section);
b23098cd 8153 addr = sym.st_value;
cba67d8f
MW
8154 reject = reject_section(section);
8155#endif
8156
2867a2a1 8157 if (name && GELF_ST_TYPE(sym.st_info) == STT_FUNC)
ab91b232 8158 add_symbol(name, (GELF_ST_BIND(sym.st_info) == STB_WEAK),
cba67d8f 8159 reject, addr, &high_addr);
2a4acb09
JL
8160 if (name && GELF_ST_TYPE(sym.st_info) == STT_OBJECT
8161 && GELF_ST_BIND(sym.st_info) == STB_GLOBAL)
8162 globals[name] = addr;
8163 if (name && GELF_ST_TYPE(sym.st_info) == STT_OBJECT
8164 && GELF_ST_BIND(sym.st_info) == STB_LOCAL)
8165 locals[name] = addr;
5f0a03a6
JK
8166 }
8167 return info_present;
8168}
8169
5f0a03a6
JK
8170func_info *
8171symbol_table::get_func_containing_address(Dwarf_Addr addr)
8172{
1c6b77e5
JS
8173 iterator_t iter = map_by_addr.upper_bound(addr);
8174 if (iter == map_by_addr.begin())
5f0a03a6 8175 return NULL;
2e67a43b 8176 else
1c6b77e5 8177 return (--iter)->second;
5f0a03a6
JK
8178}
8179
3d372d6b
SC
8180func_info *
8181symbol_table::get_first_func()
8182{
8183 iterator_t iter = map_by_addr.begin();
8184 return (iter)->second;
8185}
8186
5f0a03a6
JK
8187func_info *
8188symbol_table::lookup_symbol(const string& name)
8189{
8190 map<string, func_info*>::iterator i = map_by_name.find(name);
8191 if (i == map_by_name.end())
8192 return NULL;
8193 return i->second;
8194}
8195
8196Dwarf_Addr
8197symbol_table::lookup_symbol_address(const string& name)
8198{
8199 func_info *fi = lookup_symbol(name);
8200 if (fi)
8201 return fi->addr;
8202 return 0;
8203}
8204
ab91b232
JK
8205// This is the kernel symbol table. The kernel macro cond_syscall creates
8206// a weak symbol for each system call and maps it to sys_ni_syscall.
8207// For system calls not implemented elsewhere, this weak symbol shows up
8208// in the kernel symbol table. Following the precedent of dwarfful stap,
8209// we refuse to consider such symbols. Here we delete them from our
8210// symbol table.
8211// TODO: Consider generalizing this and/or making it part of blacklist
8212// processing.
8213void
8214symbol_table::purge_syscall_stubs()
8215{
8216 Dwarf_Addr stub_addr = lookup_symbol_address("sys_ni_syscall");
8217 if (stub_addr == 0)
8218 return;
1c6b77e5 8219 range_t purge_range = map_by_addr.equal_range(stub_addr);
2e67a43b
TM
8220 for (iterator_t iter = purge_range.first;
8221 iter != purge_range.second;
1c6b77e5 8222 )
ab91b232 8223 {
1c6b77e5 8224 func_info *fi = iter->second;
2e67a43b 8225 if (fi->weak && fi->name != "sys_ni_syscall")
ab91b232 8226 {
2e67a43b 8227 map_by_name.erase(fi->name);
1c6b77e5 8228 map_by_addr.erase(iter++);
2e67a43b 8229 delete fi;
2e67a43b 8230 }
1c6b77e5
JS
8231 else
8232 iter++;
ab91b232
JK
8233 }
8234}
8235
5f0a03a6 8236void
5f52fafe 8237module_info::get_symtab()
5f0a03a6 8238{
1c6b77e5
JS
8239 if (symtab_status != info_unknown)
8240 return;
8241
5f0a03a6
JK
8242 sym_table = new symbol_table(this);
8243 if (!elf_path.empty())
8244 {
5f0a03a6
JK
8245 symtab_status = sym_table->get_from_elf();
8246 }
8247 else
8248 {
8249 assert(name == TOK_KERNEL);
ab3ed72d
DS
8250 symtab_status = info_absent;
8251 cerr << _("Error: Cannot find vmlinux.") << endl;;
5f0a03a6
JK
8252 }
8253 if (symtab_status == info_absent)
8254 {
8255 delete sym_table;
8256 sym_table = NULL;
8257 return;
8258 }
8259
ab91b232
JK
8260 if (name == TOK_KERNEL)
8261 sym_table->purge_syscall_stubs();
5f0a03a6
JK
8262}
8263
1c6b77e5
JS
8264// update_symtab reconciles data between the elf symbol table and the dwarf
8265// function enumeration. It updates the symbol table entries with the dwarf
8266// die that describes the function, which also signals to query_module_symtab
8267// that a statement probe isn't needed. In return, it also adds aliases to the
8268// function table for names that share the same addr/die.
8269void
8270module_info::update_symtab(cu_function_cache_t *funcs)
8271{
8272 if (!sym_table)
8273 return;
8274
8275 cu_function_cache_t new_funcs;
8276
8277 for (cu_function_cache_t::iterator func = funcs->begin();
8278 func != funcs->end(); func++)
8279 {
8280 // optimization: inlines will never be in the symbol table
8281 if (dwarf_func_inline(&func->second) != 0)
6e6d9a3b
JL
8282 {
8283 inlined_funcs.insert(func->first);
8284 continue;
8285 }
1c6b77e5 8286
1ffb8bd1
JS
8287 // XXX We may want to make additional efforts to match mangled elf names
8288 // to dwarf too. MIPS_linkage_name can help, but that's sometimes
8289 // missing, so we may also need to try matching by address. See also the
8290 // notes about _Z in dwflpp::iterate_over_functions().
8291
1c6b77e5
JS
8292 func_info *fi = sym_table->lookup_symbol(func->first);
8293 if (!fi)
8294 continue;
8295
8296 // iterate over all functions at the same address
8297 symbol_table::range_t er = sym_table->map_by_addr.equal_range(fi->addr);
8298 for (symbol_table::iterator_t it = er.first; it != er.second; ++it)
8299 {
8300 // update this function with the dwarf die
8301 it->second->die = func->second;
8302
8303 // if this function is a new alias, then
8304 // save it to merge into the function cache
8305 if (it->second != fi)
b7478964 8306 new_funcs.insert(make_pair(it->second->name, it->second->die));
1c6b77e5
JS
8307 }
8308 }
8309
8310 // add all discovered aliases back into the function cache
8311 // NB: this won't replace any names that dwarf may have already found
8312 funcs->insert(new_funcs.begin(), new_funcs.end());
8313}
8314
5f0a03a6
JK
8315module_info::~module_info()
8316{
8317 if (sym_table)
8318 delete sym_table;
b55bc428
FCE
8319}
8320
935447c8 8321// ------------------------------------------------------------------------
888af770 8322// user-space probes
935447c8
DS
8323// ------------------------------------------------------------------------
8324
935447c8 8325
888af770 8326struct uprobe_derived_probe_group: public generic_dpg<uprobe_derived_probe>
935447c8 8327{
89ba3085
FCE
8328private:
8329 string make_pbm_key (uprobe_derived_probe* p) {
cfcab6c7 8330 return p->path + "|" + p->module + "|" + p->section + "|" + lex_cast(p->pid);
89ba3085
FCE
8331 }
8332
cfcab6c7
JS
8333 void emit_module_maxuprobes (systemtap_session& s);
8334
2b69faaf
JS
8335 // Using our own utrace-based uprobes
8336 void emit_module_utrace_decls (systemtap_session& s);
8337 void emit_module_utrace_init (systemtap_session& s);
8338 void emit_module_utrace_exit (systemtap_session& s);
8339
8340 // Using the upstream inode-based uprobes
8341 void emit_module_inode_decls (systemtap_session& s);
8342 void emit_module_inode_init (systemtap_session& s);
3642eb17 8343 void emit_module_inode_refresh (systemtap_session& s);
2b69faaf
JS
8344 void emit_module_inode_exit (systemtap_session& s);
8345
3a894f7e
JS
8346 // Using the dyninst backend (via stapdyn)
8347 void emit_module_dyninst_decls (systemtap_session& s);
8348 void emit_module_dyninst_init (systemtap_session& s);
8349 void emit_module_dyninst_exit (systemtap_session& s);
8350
935447c8 8351public:
888af770 8352 void emit_module_decls (systemtap_session& s);
935447c8 8353 void emit_module_init (systemtap_session& s);
3642eb17 8354 void emit_module_refresh (systemtap_session& s);
935447c8 8355 void emit_module_exit (systemtap_session& s);
ca6d3b0f
JL
8356
8357 // on-the-fly only supported for inode-uprobes
8358 bool otf_supported (systemtap_session& s)
8359 { return !s.runtime_usermode_p()
8360 && kernel_supports_inode_uprobes(s); }
8361
8362 // workqueue manipulation is safe in uprobes
8363 bool otf_safe_context (systemtap_session& s)
8364 { return otf_supported(s); }
935447c8
DS
8365};
8366
8367
888af770
FCE
8368void
8369uprobe_derived_probe::join_group (systemtap_session& s)
8370{
8371 if (! s.uprobe_derived_probes)
8372 s.uprobe_derived_probes = new uprobe_derived_probe_group ();
8373 s.uprobe_derived_probes->enroll (this);
ca6d3b0f
JL
8374 this->group = s.uprobe_derived_probes;
8375
f31a77f5
DS
8376 if (s.runtime_usermode_p())
8377 enable_dynprobes(s);
8378 else
4441e344 8379 enable_task_finder(s);
a96d1db0 8380
8a03658e 8381 // Ask buildrun.cxx to build extra module if needed, and
d3e959b0
DS
8382 // signal staprun to load that module. If we're using the builtin
8383 // inode-uprobes, we still need to know that it is required.
8a03658e 8384 s.need_uprobes = true;
a96d1db0
DN
8385}
8386
888af770 8387
c0f84e7b
SC
8388void
8389uprobe_derived_probe::getargs(std::list<std::string> &arg_set) const
8390{
8391 dwarf_derived_probe::getargs(arg_set);
8392 arg_set.insert(arg_set.end(), args.begin(), args.end());
8393}
8394
8395
8396void
8397uprobe_derived_probe::saveargs(int nargs)
8398{
8399 for (int i = 1; i <= nargs; i++)
8400 args.push_back("$arg" + lex_cast (i) + ":long");
8401}
8402
8403
2865d17a 8404void
42e38653 8405uprobe_derived_probe::emit_privilege_assertion (translator_output* o)
2865d17a
DB
8406{
8407 // These probes are allowed for unprivileged users, but only in the
8408 // context of processes which they own.
8409 emit_process_owner_assertion (o);
8410}
8411
8412
888af770 8413struct uprobe_builder: public derived_probe_builder
a96d1db0 8414{
888af770 8415 uprobe_builder() {}
2b69faaf 8416 virtual void build(systemtap_session & sess,
a96d1db0
DN
8417 probe * base,
8418 probe_point * location,
86bf665e 8419 literal_map_t const & parameters,
a96d1db0
DN
8420 vector<derived_probe *> & finished_results)
8421 {
888af770 8422 int64_t process, address;
a96d1db0 8423
2b69faaf 8424 if (kernel_supports_inode_uprobes(sess))
dc09353a 8425 throw SEMANTIC_ERROR (_("absolute process probes not available [man error::inode-uprobes]"));
2b69faaf 8426
888af770 8427 bool b1 = get_param (parameters, TOK_PROCESS, process);
ced347a9 8428 (void) b1;
888af770 8429 bool b2 = get_param (parameters, TOK_STATEMENT, address);
ced347a9 8430 (void) b2;
888af770
FCE
8431 bool rr = has_null_param (parameters, TOK_RETURN);
8432 assert (b1 && b2); // by pattern_root construction
a96d1db0 8433
0973d815 8434 finished_results.push_back(new uprobe_derived_probe(base, location, process, address, rr));
a96d1db0
DN
8435 }
8436};
8437
8438
8439void
cfcab6c7 8440uprobe_derived_probe_group::emit_module_maxuprobes (systemtap_session& s)
a96d1db0 8441{
43241c44
FCE
8442 // We'll probably need at least this many:
8443 unsigned minuprobes = probes.size();
8444 // .. but we don't want so many that .bss is inflated (PR10507):
8445 unsigned uprobesize = 64;
8446 unsigned maxuprobesmem = 10*1024*1024; // 10 MB
8447 unsigned maxuprobes = maxuprobesmem / uprobesize;
8448
aaf7ffe8
FCE
8449 // Let's choose a value on the geometric middle. This should end up
8450 // between minuprobes and maxuprobes. It's OK if this number turns
8451 // out to be < minuprobes or > maxuprobes. At worst, we get a
8452 // run-time error of one kind (too few: missed uprobe registrations)
8453 // or another (too many: vmalloc errors at module load time).
8454 unsigned default_maxuprobes = (unsigned)sqrt((double)minuprobes * (double)maxuprobes);
43241c44 8455
6d0f3f0c 8456 s.op->newline() << "#ifndef MAXUPROBES";
43241c44 8457 s.op->newline() << "#define MAXUPROBES " << default_maxuprobes;
6d0f3f0c 8458 s.op->newline() << "#endif";
cfcab6c7
JS
8459}
8460
8461
8462void
8463uprobe_derived_probe_group::emit_module_utrace_decls (systemtap_session& s)
8464{
8465 if (probes.empty()) return;
8466 s.op->newline() << "/* ---- utrace uprobes ---- */";
8467 // If uprobes isn't in the kernel, pull it in from the runtime.
8468
8469 s.op->newline() << "#if defined(CONFIG_UPROBES) || defined(CONFIG_UPROBES_MODULE)";
8470 s.op->newline() << "#include <linux/uprobes.h>";
8471 s.op->newline() << "#else";
2ba1736a 8472 s.op->newline() << "#include \"linux/uprobes/uprobes.h\"";
cfcab6c7
JS
8473 s.op->newline() << "#endif";
8474 s.op->newline() << "#ifndef UPROBES_API_VERSION";
8475 s.op->newline() << "#define UPROBES_API_VERSION 1";
8476 s.op->newline() << "#endif";
8477
8478 emit_module_maxuprobes (s);
a96d1db0 8479
cc52276b 8480 // Forward decls
2ba1736a 8481 s.op->newline() << "#include \"linux/uprobes-common.h\"";
cc52276b 8482
5e112f92
FCE
8483 // In .bss, the shared pool of uprobe/uretprobe structs. These are
8484 // too big to embed in the initialized .data stap_uprobe_spec array.
cc52276b
WC
8485 // XXX: consider a slab cache or somesuch for stap_uprobes
8486 s.op->newline() << "static struct stap_uprobe stap_uprobes [MAXUPROBES];";
6b378b7c 8487 s.op->newline() << "static DEFINE_MUTEX(stap_uprobes_lock);"; // protects against concurrent registration/unregistration
a96d1db0 8488
89ba3085
FCE
8489 s.op->assert_0_indent();
8490
89ba3085
FCE
8491 // Assign task-finder numbers as we build up the stap_uprobe_tf table.
8492 // This means we process probes[] in two passes.
8493 map <string,unsigned> module_index;
8494 unsigned module_index_ctr = 0;
8495
cc52276b
WC
8496 // not const since embedded task_finder_target struct changes
8497 s.op->newline() << "static struct stap_uprobe_tf stap_uprobe_finders[] = {";
89ba3085
FCE
8498 s.op->indent(1);
8499 for (unsigned i=0; i<probes.size(); i++)
8500 {
8501 uprobe_derived_probe *p = probes[i];
8502 string pbmkey = make_pbm_key (p);
8503 if (module_index.find (pbmkey) == module_index.end())
8504 {
8505 module_index[pbmkey] = module_index_ctr++;
8506
8507 s.op->newline() << "{";
8508 // NB: it's essential that make_pbm_key() use all of and
8509 // only the same fields as we're about to emit.
8510 s.op->line() << " .finder={";
1af100fc 8511 s.op->line() << " .purpose=\"uprobes\",";
89ba3085 8512 if (p->pid != 0)
68910c97
JK
8513 s.op->line() << " .pid=" << p->pid << ",";
8514
8515 if (p->section == "") // .statement(addr).absolute
8516 s.op->line() << " .callback=&stap_uprobe_process_found,";
89ba3085
FCE
8517 else if (p->section == ".absolute") // proxy for ET_EXEC -> exec()'d program
8518 {
8519 s.op->line() << " .procname=" << lex_cast_qstring(p->module) << ",";
8520 s.op->line() << " .callback=&stap_uprobe_process_found,";
8521 }
68910c97 8522 else if (p->section != ".absolute") // ET_DYN
89ba3085 8523 {
4ad95bbc
SC
8524 if (p->has_library)
8525 s.op->line() << " .procname=\"" << p->path << "\", ";
89ba3085
FCE
8526 s.op->line() << " .mmap_callback=&stap_uprobe_mmap_found, ";
8527 s.op->line() << " .munmap_callback=&stap_uprobe_munmap_found, ";
19d91f6c 8528 s.op->line() << " .callback=&stap_uprobe_process_munmap,";
89ba3085 8529 }
89ba3085 8530 s.op->line() << " },";
68910c97
JK
8531 if (p->module != "")
8532 s.op->line() << " .pathname=" << lex_cast_qstring(p->module) << ", ";
89ba3085
FCE
8533 s.op->line() << " },";
8534 }
c57ea854 8535 else
822a6a3d 8536 { } // skip it in this pass, already have a suitable stap_uprobe_tf slot for it.
89ba3085
FCE
8537 }
8538 s.op->newline(-1) << "};";
8539
8540 s.op->assert_0_indent();
8541
3689db05
SC
8542 unsigned pci;
8543 for (pci=0; pci<probes.size(); pci++)
8544 {
8545 // List of perf counters used by each probe
8546 // This list is an index into struct stap_perf_probe,
8547 uprobe_derived_probe *p = probes[pci];
aa2c662f 8548 std::set<string>::iterator pcii;
3689db05
SC
8549 s.op->newline() << "long perf_counters_" + lex_cast(pci) + "[] = {";
8550 for (pcii = p->perf_counter_refs.begin();
8551 pcii != p->perf_counter_refs.end(); pcii++)
8552 {
aa2c662f 8553 std::vector<std::pair<std::string,std::string> >::iterator it;
3689db05
SC
8554 unsigned i = 0;
8555 // Find the associated perf.counter probe
8556 for (it=s.perf_counters.begin() ;
8557 it != s.perf_counters.end(); it++, i++)
aa2c662f 8558 if ((*it).first == (*pcii))
3689db05
SC
8559 break;
8560 s.op->line() << lex_cast(i) << ", ";
8561 }
8562 s.op->newline() << "};";
8563 }
8564
cc52276b
WC
8565 // NB: read-only structure
8566 s.op->newline() << "static const struct stap_uprobe_spec stap_uprobe_specs [] = {";
a96d1db0 8567 s.op->indent(1);
888af770
FCE
8568 for (unsigned i =0; i<probes.size(); i++)
8569 {
8570 uprobe_derived_probe* p = probes[i];
8571 s.op->newline() << "{";
89ba3085
FCE
8572 string key = make_pbm_key (p);
8573 unsigned value = module_index[key];
759e1d76
FCE
8574 if (value != 0)
8575 s.op->line() << " .tfi=" << value << ",";
6b66b9f7 8576 s.op->line() << " .address=(unsigned long)0x" << hex << p->addr << dec << "ULL,";
faea5e16 8577 s.op->line() << " .probe=" << common_probe_init (p) << ",";
4ddb6dd0 8578
038c38c6 8579 if (p->sdt_semaphore_addr != 0)
63b4fd14 8580 s.op->line() << " .sdt_sem_offset=(unsigned long)0x"
038c38c6 8581 << hex << p->sdt_semaphore_addr << dec << "ULL,";
4ddb6dd0 8582
0d049a1d 8583 // XXX: don't bother emit if array is empty
3689db05
SC
8584 s.op->line() << " .perf_counters_dim=ARRAY_SIZE(perf_counters_" << lex_cast(i) << "),";
8585 // List of perf counters used by a probe from above
0d049a1d 8586 s.op->line() << " .perf_counters=perf_counters_" + lex_cast(i) + ",";
3689db05 8587
4ddb6dd0
JS
8588 if (p->has_return)
8589 s.op->line() << " .return_p=1,";
888af770
FCE
8590 s.op->line() << " },";
8591 }
8592 s.op->newline(-1) << "};";
a96d1db0 8593
89ba3085
FCE
8594 s.op->assert_0_indent();
8595
48e685da 8596 s.op->newline() << "static void enter_uprobe_probe (struct uprobe *inst, struct pt_regs *regs) {";
888af770 8597 s.op->newline(1) << "struct stap_uprobe *sup = container_of(inst, struct stap_uprobe, up);";
89ba3085 8598 s.op->newline() << "const struct stap_uprobe_spec *sups = &stap_uprobe_specs [sup->spec_index];";
71db462b 8599 common_probe_entryfn_prologue (s, "STAP_SESSION_RUNNING", "sups->probe",
cda141c2 8600 "stp_probe_type_uprobe");
0e090c74 8601 s.op->newline() << "if (sup->spec_index < 0 || "
6e895029
DS
8602 << "sup->spec_index >= " << probes.size() << ") {";
8603 s.op->newline(1) << "_stp_error (\"bad spec_index %d (max " << probes.size()
0e090c74 8604 << "): %s\", sup->spec_index, c->probe_point);";
6e895029
DS
8605 s.op->newline() << "goto probe_epilogue;";
8606 s.op->newline(-1) << "}";
d9aed31e 8607 s.op->newline() << "c->uregs = regs;";
e04b5d74 8608 s.op->newline() << "c->user_mode_p = 1;";
6415ddde
MW
8609
8610 // Make it look like the IP is set as it would in the actual user
8611 // task when calling real probe handler. Reset IP regs on return, so
8612 // we don't confuse uprobes. PR10458
8613 s.op->newline() << "{";
8614 s.op->indent(1);
d9aed31e 8615 s.op->newline() << "unsigned long uprobes_ip = REG_IP(c->uregs);";
259d54c0 8616 s.op->newline() << "SET_REG_IP(regs, inst->vaddr);";
26e63673 8617 s.op->newline() << "(*sups->probe->ph) (c);";
259d54c0 8618 s.op->newline() << "SET_REG_IP(regs, uprobes_ip);";
6415ddde
MW
8619 s.op->newline(-1) << "}";
8620
ef1337ee 8621 common_probe_entryfn_epilogue (s, true, otf_safe_context(s));
888af770 8622 s.op->newline(-1) << "}";
a96d1db0 8623
48e685da 8624 s.op->newline() << "static void enter_uretprobe_probe (struct uretprobe_instance *inst, struct pt_regs *regs) {";
888af770 8625 s.op->newline(1) << "struct stap_uprobe *sup = container_of(inst->rp, struct stap_uprobe, urp);";
89ba3085 8626 s.op->newline() << "const struct stap_uprobe_spec *sups = &stap_uprobe_specs [sup->spec_index];";
71db462b 8627 common_probe_entryfn_prologue (s, "STAP_SESSION_RUNNING", "sups->probe",
cda141c2 8628 "stp_probe_type_uretprobe");
6dceb5c9 8629 s.op->newline() << "c->ips.ri = inst;";
0e090c74 8630 s.op->newline() << "if (sup->spec_index < 0 || "
0d5561a5
DS
8631 << "sup->spec_index >= " << probes.size() << ") {";
8632 s.op->newline(1) << "_stp_error (\"bad spec_index %d (max " << probes.size()
0e090c74 8633 << "): %s\", sup->spec_index, c->probe_point);";
0d5561a5
DS
8634 s.op->newline() << "goto probe_epilogue;";
8635 s.op->newline(-1) << "}";
8636
d9aed31e 8637 s.op->newline() << "c->uregs = regs;";
e04b5d74 8638 s.op->newline() << "c->user_mode_p = 1;";
6415ddde
MW
8639
8640 // Make it look like the IP is set as it would in the actual user
8641 // task when calling real probe handler. Reset IP regs on return, so
8642 // we don't confuse uprobes. PR10458
8643 s.op->newline() << "{";
8644 s.op->indent(1);
d9aed31e 8645 s.op->newline() << "unsigned long uprobes_ip = REG_IP(c->uregs);";
5e562a69 8646 s.op->newline() << "SET_REG_IP(regs, inst->ret_addr);";
26e63673 8647 s.op->newline() << "(*sups->probe->ph) (c);";
259d54c0 8648 s.op->newline() << "SET_REG_IP(regs, uprobes_ip);";
6415ddde
MW
8649 s.op->newline(-1) << "}";
8650
ef1337ee 8651 common_probe_entryfn_epilogue (s, true, otf_safe_context(s));
a96d1db0
DN
8652 s.op->newline(-1) << "}";
8653
89ba3085 8654 s.op->newline();
2ba1736a 8655 s.op->newline() << "#include \"linux/uprobes-common.c\"";
6d0f3f0c 8656 s.op->newline();
888af770 8657}
935447c8
DS
8658
8659
888af770 8660void
2b69faaf 8661uprobe_derived_probe_group::emit_module_utrace_init (systemtap_session& s)
935447c8 8662{
888af770 8663 if (probes.empty()) return;
935447c8 8664
2b69faaf 8665 s.op->newline() << "/* ---- utrace uprobes ---- */";
935447c8 8666
01b05e2e 8667 s.op->newline() << "for (j=0; j<MAXUPROBES; j++) {";
5e112f92
FCE
8668 s.op->newline(1) << "struct stap_uprobe *sup = & stap_uprobes[j];";
8669 s.op->newline() << "sup->spec_index = -1;"; // free slot
80b4ad8b
FCE
8670 // NB: we assume the rest of the struct (specificaly, sup->up) is
8671 // initialized to zero. This is so that we can use
8672 // sup->up->kdata = NULL for "really free!" PR 6829.
5e112f92
FCE
8673 s.op->newline(-1) << "}";
8674 s.op->newline() << "mutex_init (& stap_uprobes_lock);";
935447c8 8675
89ba3085
FCE
8676 // Set up the task_finders
8677 s.op->newline() << "for (i=0; i<sizeof(stap_uprobe_finders)/sizeof(stap_uprobe_finders[0]); i++) {";
8678 s.op->newline(1) << "struct stap_uprobe_tf *stf = & stap_uprobe_finders[i];";
c57ea854 8679 s.op->newline() << "probe_point = stf->pathname;"; // for error messages; XXX: would prefer pp() or something better
89ba3085 8680 s.op->newline() << "rc = stap_register_task_finder_target (& stf->finder);";
935447c8 8681
5e112f92
FCE
8682 // NB: if (rc), there is no need (XXX: nor any way) to clean up any
8683 // finders already registered, since mere registration does not
8684 // cause any utrace or memory allocation actions. That happens only
8685 // later, once the task finder engine starts running. So, for a
8686 // partial initialization requiring unwind, we need do nothing.
8687 s.op->newline() << "if (rc) break;";
a7a68293 8688
888af770
FCE
8689 s.op->newline(-1) << "}";
8690}
d0ea46ce 8691
d0a7f5a9 8692
888af770 8693void
2b69faaf 8694uprobe_derived_probe_group::emit_module_utrace_exit (systemtap_session& s)
888af770
FCE
8695{
8696 if (probes.empty()) return;
2b69faaf 8697 s.op->newline() << "/* ---- utrace uprobes ---- */";
e56e51c9 8698
6d0f3f0c
FCE
8699 // NB: there is no stap_unregister_task_finder_target call;
8700 // important stuff like utrace cleanups are done by
d41d451c
FCE
8701 // __stp_task_finder_cleanup() via stap_stop_task_finder().
8702 //
8703 // This function blocks until all callbacks are completed, so there
8704 // is supposed to be no possibility of any registration-related code starting
8705 // to run in parallel with our shutdown here. So we don't need to protect the
8706 // stap_uprobes[] array with the mutex.
d0a7f5a9 8707
01b05e2e 8708 s.op->newline() << "for (j=0; j<MAXUPROBES; j++) {";
5e112f92 8709 s.op->newline(1) << "struct stap_uprobe *sup = & stap_uprobes[j];";
89ba3085 8710 s.op->newline() << "const struct stap_uprobe_spec *sups = &stap_uprobe_specs [sup->spec_index];";
6d0f3f0c 8711 s.op->newline() << "if (sup->spec_index < 0) continue;"; // free slot
3568f1dd 8712
8faa1fc5 8713 // PR10655: decrement that ENABLED semaphore
c116c31b 8714 s.op->newline() << "if (sup->sdt_sem_address) {";
8faa1fc5
FCE
8715 s.op->newline(1) << "unsigned short sdt_semaphore;"; // NB: fixed size
8716 s.op->newline() << "pid_t pid = (sups->return_p ? sup->urp.u.pid : sup->up.pid);";
8717 s.op->newline() << "struct task_struct *tsk;";
8718 s.op->newline() << "rcu_read_lock();";
6846cfc8 8719
86229a55
DS
8720 // Do a pid->task_struct* lookup. For 2.6.24+, this code assumes
8721 // that the pid is always in the global namespace, not in any
8722 // private namespace.
8faa1fc5 8723 s.op->newline() << "#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,24)";
86229a55
DS
8724 // We'd like to call find_task_by_pid_ns() here, but it isn't
8725 // exported. So, we call what it calls...
8726 s.op->newline() << " tsk = pid_task(find_pid_ns(pid, &init_pid_ns), PIDTYPE_PID);";
8faa1fc5
FCE
8727 s.op->newline() << "#else";
8728 s.op->newline() << " tsk = find_task_by_pid (pid);";
8729 s.op->newline() << "#endif /* 2.6.24 */";
8faa1fc5
FCE
8730
8731 s.op->newline() << "if (tsk) {"; // just in case the thing exited while we weren't watching
3c5b8e2b 8732 s.op->newline(1) << "if (__access_process_vm_noflush(tsk, sup->sdt_sem_address, &sdt_semaphore, sizeof(sdt_semaphore), 0)) {";
63b4fd14 8733 s.op->newline(1) << "sdt_semaphore --;";
903b9fcd 8734 s.op->newline() << "#ifdef DEBUG_UPROBES";
c116c31b 8735 s.op->newline() << "_stp_dbug (__FUNCTION__,__LINE__, \"-semaphore %#x @ %#lx\\n\", sdt_semaphore, sup->sdt_sem_address);";
903b9fcd 8736 s.op->newline() << "#endif";
3c5b8e2b 8737 s.op->newline() << "__access_process_vm_noflush(tsk, sup->sdt_sem_address, &sdt_semaphore, sizeof(sdt_semaphore), 1);";
93c84191 8738 s.op->newline(-1) << "}";
8faa1fc5
FCE
8739 // XXX: need to analyze possibility of race condition
8740 s.op->newline(-1) << "}";
8741 s.op->newline() << "rcu_read_unlock();";
8742 s.op->newline(-1) << "}";
6846cfc8 8743
3568f1dd
FCE
8744 s.op->newline() << "if (sups->return_p) {";
8745 s.op->newline(1) << "#ifdef DEBUG_UPROBES";
89ba3085 8746 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 8747 s.op->newline() << "#endif";
80b4ad8b
FCE
8748 // NB: PR6829 does not change that we still need to unregister at
8749 // *this* time -- when the script as a whole exits.
3568f1dd
FCE
8750 s.op->newline() << "unregister_uretprobe (& sup->urp);";
8751 s.op->newline(-1) << "} else {";
8752 s.op->newline(1) << "#ifdef DEBUG_UPROBES";
8faa1fc5 8753 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
8754 s.op->newline() << "#endif";
8755 s.op->newline() << "unregister_uprobe (& sup->up);";
8756 s.op->newline(-1) << "}";
935447c8 8757
6d0f3f0c 8758 s.op->newline() << "sup->spec_index = -1;";
935447c8 8759
3568f1dd
FCE
8760 // XXX: uprobe missed counts?
8761
6d0f3f0c 8762 s.op->newline(-1) << "}";
935447c8 8763
5e112f92 8764 s.op->newline() << "mutex_destroy (& stap_uprobes_lock);";
935447c8
DS
8765}
8766
2b69faaf
JS
8767
8768void
8769uprobe_derived_probe_group::emit_module_inode_decls (systemtap_session& s)
8770{
8771 if (probes.empty()) return;
8772 s.op->newline() << "/* ---- inode uprobes ---- */";
cfcab6c7 8773 emit_module_maxuprobes (s);
2ba1736a 8774 s.op->newline() << "#include \"linux/uprobes-inode.c\"";
2b69faaf
JS
8775
8776 // Write the probe handler.
79af55c3
JS
8777 s.op->newline() << "static int stapiu_probe_handler "
8778 << "(struct stapiu_consumer *sup, struct pt_regs *regs) {";
8779 s.op->newline(1);
2dbbd473
JS
8780
8781 // Since we're sharing the entry function, we have to dynamically choose the probe_type
8782 string probe_type = "(sup->return_p ? stp_probe_type_uretprobe : stp_probe_type_uprobe)";
71db462b 8783 common_probe_entryfn_prologue (s, "STAP_SESSION_RUNNING", "sup->probe",
2dbbd473
JS
8784 probe_type);
8785
3bff6634 8786 s.op->newline() << "c->uregs = regs;";
e04b5d74 8787 s.op->newline() << "c->user_mode_p = 1;";
497cfcb2 8788 // NB: IP is already set by stapiu_probe_prehandler in uprobes-inode.c
2b69faaf 8789 s.op->newline() << "(*sup->probe->ph) (c);";
39e63bae 8790
ef1337ee 8791 common_probe_entryfn_epilogue (s, true, otf_safe_context(s));
2b69faaf
JS
8792 s.op->newline() << "return 0;";
8793 s.op->newline(-1) << "}";
8794 s.op->assert_0_indent();
8795
8796 // Index of all the modules for which we need inodes.
8797 map<string, unsigned> module_index;
8798 unsigned module_index_ctr = 0;
8799
8800 // Discover and declare targets for each unique path.
cfcab6c7 8801 s.op->newline() << "static struct stapiu_target "
2b69faaf
JS
8802 << "stap_inode_uprobe_targets[] = {";
8803 s.op->indent(1);
8804 for (unsigned i=0; i<probes.size(); i++)
8805 {
8806 uprobe_derived_probe *p = probes[i];
cfcab6c7
JS
8807 const string key = make_pbm_key(p);
8808 if (module_index.find (key) == module_index.end())
2b69faaf 8809 {
cfcab6c7
JS
8810 module_index[key] = module_index_ctr++;
8811 s.op->newline() << "{";
8812 s.op->line() << " .finder={";
b78a0fbb 8813 s.op->line() << " .purpose=\"inode-uprobes\",";
cfcab6c7
JS
8814 if (p->pid != 0)
8815 s.op->line() << " .pid=" << p->pid << ",";
8816
8817 if (p->section == "") // .statement(addr).absolute XXX?
8818 s.op->line() << " .callback=&stapiu_process_found,";
8819 else if (p->section == ".absolute") // proxy for ET_EXEC -> exec()'d program
8820 {
8821 s.op->line() << " .procname=" << lex_cast_qstring(p->module) << ",";
8822 s.op->line() << " .callback=&stapiu_process_found,";
8823 }
8824 else if (p->section != ".absolute") // ET_DYN
8825 {
8826 if (p->has_library)
8827 s.op->line() << " .procname=\"" << p->path << "\", ";
8828 s.op->line() << " .mmap_callback=&stapiu_mmap_found, ";
8829 s.op->line() << " .munmap_callback=&stapiu_munmap_found, ";
8830 s.op->line() << " .callback=&stapiu_process_munmap,";
8831 }
8832 s.op->line() << " },";
8833 s.op->line() << " .filename=" << lex_cast_qstring(p->module) << ",";
8834 s.op->line() << " },";
2b69faaf
JS
8835 }
8836 }
8837 s.op->newline(-1) << "};";
8838 s.op->assert_0_indent();
8839
8840 // Declare the actual probes.
3689db05
SC
8841 unsigned pci;
8842 for (pci=0; pci<probes.size(); pci++)
8843 {
8844 // List of perf counters used by each probe
8845 // This list is an index into struct stap_perf_probe,
8846 uprobe_derived_probe *p = probes[pci];
aa2c662f 8847 std::set<string>::iterator pcii;
3689db05
SC
8848 s.op->newline() << "long perf_counters_" + lex_cast(pci) + "[] = {";
8849 for (pcii = p->perf_counter_refs.begin();
8850 pcii != p->perf_counter_refs.end(); pcii++)
8851 {
aa2c662f 8852 vector<std::pair<string,string> >:: iterator it;
3689db05
SC
8853 unsigned i = 0;
8854 // Find the associated perf.counter probe
4fa83377
SC
8855 for (it=s.perf_counters.begin() ;
8856 it != s.perf_counters.end(); it++, i++)
aa2c662f 8857 if ((*it).first == (*pcii))
3689db05
SC
8858 break;
8859 s.op->line() << lex_cast(i) << ", ";
8860 }
8861 s.op->newline() << "};";
8862 }
8863
cfcab6c7 8864 s.op->newline() << "static struct stapiu_consumer "
2b69faaf
JS
8865 << "stap_inode_uprobe_consumers[] = {";
8866 s.op->indent(1);
8867 for (unsigned i=0; i<probes.size(); i++)
8868 {
8869 uprobe_derived_probe *p = probes[i];
cfcab6c7
JS
8870 unsigned index = module_index[make_pbm_key(p)];
8871 s.op->newline() << "{";
79af55c3
JS
8872 if (p->has_return)
8873 s.op->line() << " .return_p=1,";
cfcab6c7
JS
8874 s.op->line() << " .target=&stap_inode_uprobe_targets[" << index << "],";
8875 s.op->line() << " .offset=(loff_t)0x" << hex << p->addr << dec << "ULL,";
8876 if (p->sdt_semaphore_addr)
8877 s.op->line() << " .sdt_sem_offset=(loff_t)0x"
8878 << hex << p->sdt_semaphore_addr << dec << "ULL,";
0d049a1d 8879 // XXX: don't bother emit if array is empty
3689db05
SC
8880 s.op->line() << " .perf_counters_dim=ARRAY_SIZE(perf_counters_" << lex_cast(i) << "),";
8881 // List of perf counters used by a probe from above
0d049a1d 8882 s.op->line() << " .perf_counters=perf_counters_" + lex_cast(i) + ",";
cfcab6c7
JS
8883 s.op->line() << " .probe=" << common_probe_init (p) << ",";
8884 s.op->line() << " },";
2b69faaf
JS
8885 }
8886 s.op->newline(-1) << "};";
8887 s.op->assert_0_indent();
8888}
8889
8890
8891void
8892uprobe_derived_probe_group::emit_module_inode_init (systemtap_session& s)
8893{
8894 if (probes.empty()) return;
8895 s.op->newline() << "/* ---- inode uprobes ---- */";
b0f614a0
DS
8896 // Let stapiu_init() handle reporting errors by setting probe_point
8897 // to NULL.
8898 s.op->newline() << "probe_point = NULL;";
cfcab6c7 8899 s.op->newline() << "rc = stapiu_init ("
2b69faaf
JS
8900 << "stap_inode_uprobe_targets, "
8901 << "ARRAY_SIZE(stap_inode_uprobe_targets), "
8902 << "stap_inode_uprobe_consumers, "
8903 << "ARRAY_SIZE(stap_inode_uprobe_consumers));";
8904}
8905
8906
3642eb17
JL
8907void
8908uprobe_derived_probe_group::emit_module_inode_refresh (systemtap_session& s)
8909{
8910 if (probes.empty()) return;
8911 s.op->newline() << "/* ---- inode uprobes ---- */";
3642eb17
JL
8912 s.op->newline() << "stapiu_refresh ("
8913 << "stap_inode_uprobe_targets, "
8914 << "ARRAY_SIZE(stap_inode_uprobe_targets));";
3642eb17
JL
8915}
8916
8917
2b69faaf
JS
8918void
8919uprobe_derived_probe_group::emit_module_inode_exit (systemtap_session& s)
8920{
8921 if (probes.empty()) return;
8922 s.op->newline() << "/* ---- inode uprobes ---- */";
cfcab6c7 8923 s.op->newline() << "stapiu_exit ("
2b69faaf
JS
8924 << "stap_inode_uprobe_targets, "
8925 << "ARRAY_SIZE(stap_inode_uprobe_targets), "
8926 << "stap_inode_uprobe_consumers, "
8927 << "ARRAY_SIZE(stap_inode_uprobe_consumers));";
8928}
8929
8930
3a894f7e
JS
8931void
8932uprobe_derived_probe_group::emit_module_dyninst_decls (systemtap_session& s)
8933{
8934 if (probes.empty()) return;
8935 s.op->newline() << "/* ---- dyninst uprobes ---- */";
8936 emit_module_maxuprobes (s);
e00f3fb7 8937 s.op->newline() << "#include \"dyninst/uprobes.h\"";
3a894f7e 8938
f31a77f5
DS
8939 // Let the dynprobe_derived_probe_group handle outputting targets
8940 // and probes. This allows us to merge different types of probes.
8941 s.op->newline() << "static struct stapdu_probe stapdu_probes[];";
8942 for (unsigned i = 0; i < probes.size(); i++)
3a894f7e
JS
8943 {
8944 uprobe_derived_probe *p = probes[i];
e00f3fb7 8945
f31a77f5
DS
8946 dynprobe_add_uprobe(s, p->module, p->addr, p->sdt_semaphore_addr,
8947 (p->has_return ? "STAPDYN_PROBE_FLAG_RETURN" : "0"),
8948 common_probe_init(p));
3a894f7e 8949 }
874d38bf
JS
8950 // loc2c-generated code assumes pt_regs are available, so use this to make
8951 // sure we always have *something* for it to dereference...
f31a77f5 8952 s.op->newline() << "static struct pt_regs stapdu_dummy_uregs;";
e00f3fb7 8953
3a894f7e
JS
8954 // Write the probe handler.
8955 // NB: not static, so dyninst can find it
8956 s.op->newline() << "int enter_dyninst_uprobe "
8957 << "(uint64_t index, struct pt_regs *regs) {";
e00f3fb7 8958 s.op->newline(1) << "struct stapdu_probe *sup = &stapdu_probes[index];";
92b97104
JS
8959
8960 // Since we're sharing the entry function, we have to dynamically choose the probe_type
8961 string probe_type = "((sup->flags & STAPDYN_PROBE_FLAG_RETURN) ?"
8962 " stp_probe_type_uretprobe : stp_probe_type_uprobe)";
71db462b 8963 common_probe_entryfn_prologue (s, "STAP_SESSION_RUNNING", "sup->probe",
92b97104
JS
8964 probe_type);
8965
874d38bf 8966 s.op->newline() << "c->uregs = regs ?: &stapdu_dummy_uregs;";
e04b5d74 8967 s.op->newline() << "c->user_mode_p = 1;";
3a894f7e
JS
8968 // XXX: once we have regs, check how dyninst sets the IP
8969 // XXX: the way that dyninst rewrites stuff is probably going to be
8970 // ... very confusing to our backtracer (at least if we stay in process)
8971 s.op->newline() << "(*sup->probe->ph) (c);";
ef1337ee 8972 common_probe_entryfn_epilogue (s, true, otf_safe_context(s));
3a894f7e
JS
8973 s.op->newline() << "return 0;";
8974 s.op->newline(-1) << "}";
3debb935 8975 s.op->newline() << "#include \"dyninst/uprobes-regs.c\"";
3a894f7e
JS
8976 s.op->assert_0_indent();
8977}
8978
8979
8980void
8981uprobe_derived_probe_group::emit_module_dyninst_init (systemtap_session& s)
8982{
8983 if (probes.empty()) return;
8984
8985 /* stapdyn handles the dirty work via dyninst */
8986 s.op->newline() << "/* ---- dyninst uprobes ---- */";
8987 s.op->newline() << "/* this section left intentionally blank */";
8988}
8989
8990
8991void
8992uprobe_derived_probe_group::emit_module_dyninst_exit (systemtap_session& s)
8993{
8994 if (probes.empty()) return;
8995
8996 /* stapdyn handles the dirty work via dyninst */
8997 s.op->newline() << "/* ---- dyninst uprobes ---- */";
8998 s.op->newline() << "/* this section left intentionally blank */";
8999}
9000
9001
2b69faaf
JS
9002void
9003uprobe_derived_probe_group::emit_module_decls (systemtap_session& s)
9004{
ac3af990 9005 if (s.runtime_usermode_p())
4441e344
JS
9006 emit_module_dyninst_decls (s);
9007 else if (kernel_supports_inode_uprobes (s))
2b69faaf
JS
9008 emit_module_inode_decls (s);
9009 else
9010 emit_module_utrace_decls (s);
9011}
9012
9013
9014void
9015uprobe_derived_probe_group::emit_module_init (systemtap_session& s)
9016{
ac3af990 9017 if (s.runtime_usermode_p())
4441e344
JS
9018 emit_module_dyninst_init (s);
9019 else if (kernel_supports_inode_uprobes (s))
2b69faaf
JS
9020 emit_module_inode_init (s);
9021 else
9022 emit_module_utrace_init (s);
9023}
9024
9025
3642eb17
JL
9026void
9027uprobe_derived_probe_group::emit_module_refresh (systemtap_session& s)
9028{
9029 if (!s.runtime_usermode_p() && kernel_supports_inode_uprobes (s))
9030 emit_module_inode_refresh (s);
9031}
9032
9033
2b69faaf
JS
9034void
9035uprobe_derived_probe_group::emit_module_exit (systemtap_session& s)
9036{
ac3af990 9037 if (s.runtime_usermode_p())
4441e344
JS
9038 emit_module_dyninst_exit (s);
9039 else if (kernel_supports_inode_uprobes (s))
2b69faaf
JS
9040 emit_module_inode_exit (s);
9041 else
9042 emit_module_utrace_exit (s);
9043}
9044
9045
e6fe60e7
AM
9046// ------------------------------------------------------------------------
9047// Kprobe derived probes
9048// ------------------------------------------------------------------------
9049
4627ed58 9050static const string TOK_KPROBE("kprobe");
935447c8 9051
bae55db9 9052struct kprobe_derived_probe: public derived_probe
d0ea46ce 9053{
23dc94f6
DS
9054 kprobe_derived_probe (systemtap_session& sess,
9055 vector<derived_probe *> & results,
9056 probe *base,
bae55db9
JS
9057 probe_point *location,
9058 const string& name,
9059 int64_t stmt_addr,
e6afb07b 9060 bool has_call,
bae55db9
JS
9061 bool has_return,
9062 bool has_statement,
9063 bool has_maxactive,
b642c901
SC
9064 bool has_path,
9065 bool has_library,
9066 long maxactive_val,
9067 const string& path,
9068 const string& library
bae55db9
JS
9069 );
9070 string symbol_name;
9071 Dwarf_Addr addr;
e6afb07b 9072 bool has_call;
bae55db9
JS
9073 bool has_return;
9074 bool has_statement;
9075 bool has_maxactive;
b642c901
SC
9076 bool has_path;
9077 bool has_library;
bae55db9 9078 long maxactive_val;
b642c901
SC
9079 string path;
9080 string library;
bae55db9
JS
9081 bool access_var;
9082 void printsig (std::ostream &o) const;
9083 void join_group (systemtap_session& s);
9084};
d0ea46ce 9085
bae55db9
JS
9086struct kprobe_derived_probe_group: public derived_probe_group
9087{
9088private:
9089 multimap<string,kprobe_derived_probe*> probes_by_module;
9090 typedef multimap<string,kprobe_derived_probe*>::iterator p_b_m_iterator;
d0ea46ce 9091
bae55db9
JS
9092public:
9093 void enroll (kprobe_derived_probe* probe);
9094 void emit_module_decls (systemtap_session& s);
9095 void emit_module_init (systemtap_session& s);
9096 void emit_module_exit (systemtap_session& s);
9097};
d0ea46ce 9098
23dc94f6
DS
9099struct kprobe_var_expanding_visitor: public var_expanding_visitor
9100{
9101 systemtap_session& sess;
9102 block *add_block;
9103 block *add_call_probe; // synthesized from .return probes with saved $vars
9104 bool add_block_tid, add_call_probe_tid;
bd5b25e1 9105 bool has_return;
23dc94f6 9106
bd5b25e1 9107 kprobe_var_expanding_visitor(systemtap_session& sess, bool has_return):
23dc94f6 9108 sess(sess), add_block(NULL), add_call_probe(NULL),
bd5b25e1
JS
9109 add_block_tid(false), add_call_probe_tid(false),
9110 has_return(has_return) {}
23dc94f6
DS
9111
9112 void visit_entry_op (entry_op* e);
9113};
9114
9115
9116kprobe_derived_probe::kprobe_derived_probe (systemtap_session& sess,
9117 vector<derived_probe *> & results,
9118 probe *base,
e6fe60e7 9119 probe_point *location,
b6371390 9120 const string& name,
e6fe60e7 9121 int64_t stmt_addr,
e6afb07b 9122 bool has_call,
b6371390
JS
9123 bool has_return,
9124 bool has_statement,
9125 bool has_maxactive,
b642c901
SC
9126 bool has_path,
9127 bool has_library,
9128 long maxactive_val,
9129 const string& path,
9130 const string& library
b6371390 9131 ):
4c5d1300 9132 derived_probe (base, location, true /* .components soon rewritten */ ),
e6afb07b 9133 symbol_name (name), addr (stmt_addr), has_call (has_call),
b6371390 9134 has_return (has_return), has_statement (has_statement),
b642c901
SC
9135 has_maxactive (has_maxactive), has_path (has_path),
9136 has_library (has_library),
9137 maxactive_val (maxactive_val),
9138 path (path), library (library)
e6fe60e7
AM
9139{
9140 this->tok = base->tok;
9141 this->access_var = false;
d0ea46ce 9142
e6fe60e7
AM
9143#ifndef USHRT_MAX
9144#define USHRT_MAX 32767
9145#endif
d0ea46ce 9146
46856d8d
JS
9147 // Expansion of $target variables in the probe body produces an error during
9148 // translate phase, since we're not using debuginfo
d0ea46ce 9149
e6fe60e7 9150 vector<probe_point::component*> comps;
46856d8d 9151 comps.push_back (new probe_point::component(TOK_KPROBE));
e6fe60e7 9152
46856d8d
JS
9153 if (has_statement)
9154 {
9ea68eb9
JS
9155 comps.push_back (new probe_point::component(TOK_STATEMENT,
9156 new literal_number(addr, true)));
46856d8d
JS
9157 comps.push_back (new probe_point::component(TOK_ABSOLUTE));
9158 }
9159 else
9160 {
9161 size_t pos = name.find(':');
9162 if (pos != string::npos)
d0ea46ce 9163 {
46856d8d
JS
9164 string module = name.substr(0, pos);
9165 string function = name.substr(pos + 1);
9166 comps.push_back (new probe_point::component(TOK_MODULE, new literal_string(module)));
9167 comps.push_back (new probe_point::component(TOK_FUNCTION, new literal_string(function)));
9168 }
9169 else
9170 comps.push_back (new probe_point::component(TOK_FUNCTION, new literal_string(name)));
46856d8d 9171 }
d0ea46ce 9172
e6afb07b
JL
9173 if (has_call)
9174 comps.push_back (new probe_point::component(TOK_CALL));
b6371390
JS
9175 if (has_return)
9176 comps.push_back (new probe_point::component(TOK_RETURN));
9177 if (has_maxactive)
9178 comps.push_back (new probe_point::component(TOK_MAXACTIVE, new literal_number(maxactive_val)));
d0ea46ce 9179
bd5b25e1 9180 kprobe_var_expanding_visitor v (sess, has_return);
23dc94f6
DS
9181 v.replace (this->body);
9182
9183 // If during target-variable-expanding the probe, we added a new block
9184 // of code, add it to the start of the probe.
9185 if (v.add_block)
9186 this->body = new block(v.add_block, this->body);
9187
9188 // If when target-variable-expanding the probe, we need to
9189 // synthesize a sibling function-entry probe. We don't go through
9190 // the whole probe derivation business (PR10642) that could lead to
9191 // wildcard/alias resolution, or for that dwarf-induced duplication.
9192 if (v.add_call_probe)
9193 {
9194 assert (has_return);
9195
9196 // We temporarily replace base.
9197 statement* old_body = base->body;
9198 base->body = v.add_call_probe;
9199
9200 derived_probe *entry_handler
9201 = new kprobe_derived_probe (sess, results, base, location, name, 0,
e6afb07b
JL
9202 true /* has_call */, false /* has_return */,
9203 has_statement, has_maxactive, has_path,
9204 has_library, maxactive_val, path, library);
23dc94f6
DS
9205 results.push_back (entry_handler);
9206
9207 base->body = old_body;
9208 }
9209
e6fe60e7
AM
9210 this->sole_location()->components = comps;
9211}
d0ea46ce 9212
e6fe60e7
AM
9213void kprobe_derived_probe::printsig (ostream& o) const
9214{
9215 sole_location()->print (o);
9216 o << " /* " << " name = " << symbol_name << "*/";
9217 printsig_nested (o);
9218}
d0ea46ce 9219
e6fe60e7
AM
9220void kprobe_derived_probe::join_group (systemtap_session& s)
9221{
e6fe60e7
AM
9222 if (! s.kprobe_derived_probes)
9223 s.kprobe_derived_probes = new kprobe_derived_probe_group ();
9224 s.kprobe_derived_probes->enroll (this);
ca6d3b0f 9225 this->group = s.kprobe_derived_probes;
e6fe60e7 9226}
d0ea46ce 9227
e6fe60e7
AM
9228void kprobe_derived_probe_group::enroll (kprobe_derived_probe* p)
9229{
9230 probes_by_module.insert (make_pair (p->symbol_name, p));
9231 // probes of same symbol should share single kprobe/kretprobe
9232}
d0ea46ce 9233
e6fe60e7
AM
9234void
9235kprobe_derived_probe_group::emit_module_decls (systemtap_session& s)
9236{
9237 if (probes_by_module.empty()) return;
d0ea46ce 9238
e6fe60e7 9239 s.op->newline() << "/* ---- kprobe-based probes ---- */";
d0ea46ce 9240
e6fe60e7
AM
9241 // Warn of misconfigured kernels
9242 s.op->newline() << "#if ! defined(CONFIG_KPROBES)";
9243 s.op->newline() << "#error \"Need CONFIG_KPROBES!\"";
9244 s.op->newline() << "#endif";
9245 s.op->newline();
d0ea46ce 9246
f07c3b68 9247 s.op->newline() << "#ifndef KRETACTIVE";
1ee6b5fc 9248 s.op->newline() << "#define KRETACTIVE (max(15,6*(int)num_possible_cpus()))";
f07c3b68
FCE
9249 s.op->newline() << "#endif";
9250
e6fe60e7 9251 // Forward declare the master entry functions
88747011 9252 s.op->newline() << "static int enter_kprobe2_probe (struct kprobe *inst,";
e6fe60e7 9253 s.op->line() << " struct pt_regs *regs);";
88747011 9254 s.op->newline() << "static int enter_kretprobe2_probe (struct kretprobe_instance *inst,";
e6fe60e7 9255 s.op->line() << " struct pt_regs *regs);";
d0ea46ce 9256
e6fe60e7
AM
9257 // Emit an array of kprobe/kretprobe pointers
9258 s.op->newline() << "#if defined(STAPCONF_UNREGISTER_KPROBES)";
c9116e99 9259 s.op->newline() << "static void * stap_unreg_kprobes2[" << probes_by_module.size() << "];";
e6fe60e7 9260 s.op->newline() << "#endif";
d0ea46ce 9261
e6fe60e7 9262 // Emit the actual probe list.
d0ea46ce 9263
e6fe60e7
AM
9264 s.op->newline() << "static struct stap_dwarfless_kprobe {";
9265 s.op->newline(1) << "union { struct kprobe kp; struct kretprobe krp; } u;";
9266 s.op->newline() << "#ifdef __ia64__";
9267 s.op->newline() << "struct kprobe dummy;";
9268 s.op->newline() << "#endif";
9269 s.op->newline(-1) << "} stap_dwarfless_kprobes[" << probes_by_module.size() << "];";
9270 // NB: bss!
d0ea46ce 9271
e6fe60e7
AM
9272 s.op->newline() << "static struct stap_dwarfless_probe {";
9273 s.op->newline(1) << "const unsigned return_p:1;";
9274 s.op->newline() << "const unsigned maxactive_p:1;";
b350f56b 9275 s.op->newline() << "const unsigned optional_p:1;";
e6fe60e7
AM
9276 s.op->newline() << "unsigned registered_p:1;";
9277 s.op->newline() << "const unsigned short maxactive_val;";
935447c8 9278
e6fe60e7
AM
9279 // Function Names are mostly small and uniform enough to justify putting
9280 // char[MAX]'s into the array instead of relocated char*'s.
935447c8 9281
faea5e16
JS
9282 size_t symbol_string_name_max = 0;
9283 size_t symbol_string_name_tot = 0;
e6fe60e7 9284 for (p_b_m_iterator it = probes_by_module.begin(); it != probes_by_module.end(); it++)
6270adc1 9285 {
e6fe60e7
AM
9286 kprobe_derived_probe* p = it->second;
9287#define DOIT(var,expr) do { \
9288 size_t var##_size = (expr) + 1; \
9289 var##_max = max (var##_max, var##_size); \
9290 var##_tot += var##_size; } while (0)
e6fe60e7
AM
9291 DOIT(symbol_string_name, p->symbol_name.size());
9292#undef DOIT
6270adc1
MH
9293 }
9294
e6fe60e7
AM
9295#define CALCIT(var) \
9296 s.op->newline() << "const char " << #var << "[" << var##_name_max << "] ;";
935447c8 9297
e6fe60e7
AM
9298 CALCIT(symbol_string);
9299#undef CALCIT
6270adc1 9300
bd659351 9301 s.op->newline() << "unsigned long address;";
7c3e97f4 9302 s.op->newline() << "const struct stap_probe * const probe;";
e6fe60e7
AM
9303 s.op->newline(-1) << "} stap_dwarfless_probes[] = {";
9304 s.op->indent(1);
6270adc1 9305
e6fe60e7
AM
9306 for (p_b_m_iterator it = probes_by_module.begin(); it != probes_by_module.end(); it++)
9307 {
9308 kprobe_derived_probe* p = it->second;
9309 s.op->newline() << "{";
9310 if (p->has_return)
9311 s.op->line() << " .return_p=1,";
6270adc1 9312
e6fe60e7
AM
9313 if (p->has_maxactive)
9314 {
9315 s.op->line() << " .maxactive_p=1,";
9316 assert (p->maxactive_val >= 0 && p->maxactive_val <= USHRT_MAX);
9317 s.op->line() << " .maxactive_val=" << p->maxactive_val << ",";
9318 }
6270adc1 9319
b350f56b
JS
9320 if (p->locations[0]->optional)
9321 s.op->line() << " .optional_p=1,";
9322
e6fe60e7 9323 if (p->has_statement)
c8d9d15e 9324 s.op->line() << " .address=(unsigned long)0x" << hex << p->addr << dec << "ULL,";
e6fe60e7 9325 else
c8d9d15e 9326 s.op->line() << " .symbol_string=\"" << p->symbol_name << "\",";
5d67b47c 9327
faea5e16 9328 s.op->line() << " .probe=" << common_probe_init (p) << ",";
e6fe60e7 9329 s.op->line() << " },";
935447c8
DS
9330 }
9331
e6fe60e7 9332 s.op->newline(-1) << "};";
5d67b47c 9333
e6fe60e7
AM
9334 // Emit the kprobes callback function
9335 s.op->newline();
88747011 9336 s.op->newline() << "static int enter_kprobe2_probe (struct kprobe *inst,";
e6fe60e7
AM
9337 s.op->line() << " struct pt_regs *regs) {";
9338 // NB: as of PR5673, the kprobe|kretprobe union struct is in BSS
9339 s.op->newline(1) << "int kprobe_idx = ((uintptr_t)inst-(uintptr_t)stap_dwarfless_kprobes)/sizeof(struct stap_dwarfless_kprobe);";
9340 // Check that the index is plausible
9341 s.op->newline() << "struct stap_dwarfless_probe *sdp = &stap_dwarfless_probes[";
9342 s.op->line() << "((kprobe_idx >= 0 && kprobe_idx < " << probes_by_module.size() << ")?";
9343 s.op->line() << "kprobe_idx:0)"; // NB: at least we avoid memory corruption
9344 // XXX: it would be nice to give a more verbose error though; BUG_ON later?
9345 s.op->line() << "];";
71db462b 9346 common_probe_entryfn_prologue (s, "STAP_SESSION_RUNNING", "sdp->probe",
cda141c2 9347 "stp_probe_type_kprobe");
d9aed31e 9348 s.op->newline() << "c->kregs = regs;";
6415ddde
MW
9349
9350 // Make it look like the IP is set as it wouldn't have been replaced
9351 // by a breakpoint instruction when calling real probe handler. Reset
9352 // IP regs on return, so we don't confuse kprobes. PR10458
9353 s.op->newline() << "{";
9354 s.op->indent(1);
d9aed31e 9355 s.op->newline() << "unsigned long kprobes_ip = REG_IP(c->kregs);";
259d54c0 9356 s.op->newline() << "SET_REG_IP(regs, (unsigned long) inst->addr);";
26e63673 9357 s.op->newline() << "(*sdp->probe->ph) (c);";
259d54c0 9358 s.op->newline() << "SET_REG_IP(regs, kprobes_ip);";
6415ddde
MW
9359 s.op->newline(-1) << "}";
9360
ef1337ee 9361 common_probe_entryfn_epilogue (s, true, otf_safe_context(s));
e6fe60e7
AM
9362 s.op->newline() << "return 0;";
9363 s.op->newline(-1) << "}";
935447c8 9364
e6fe60e7
AM
9365 // Same for kretprobes
9366 s.op->newline();
88747011 9367 s.op->newline() << "static int enter_kretprobe2_probe (struct kretprobe_instance *inst,";
e6fe60e7
AM
9368 s.op->line() << " struct pt_regs *regs) {";
9369 s.op->newline(1) << "struct kretprobe *krp = inst->rp;";
935447c8 9370
e6fe60e7
AM
9371 // NB: as of PR5673, the kprobe|kretprobe union struct is in BSS
9372 s.op->newline() << "int kprobe_idx = ((uintptr_t)krp-(uintptr_t)stap_dwarfless_kprobes)/sizeof(struct stap_dwarfless_kprobe);";
9373 // Check that the index is plausible
9374 s.op->newline() << "struct stap_dwarfless_probe *sdp = &stap_dwarfless_probes[";
9375 s.op->line() << "((kprobe_idx >= 0 && kprobe_idx < " << probes_by_module.size() << ")?";
9376 s.op->line() << "kprobe_idx:0)"; // NB: at least we avoid memory corruption
9377 // XXX: it would be nice to give a more verbose error though; BUG_ON later?
9378 s.op->line() << "];";
935447c8 9379
71db462b 9380 common_probe_entryfn_prologue (s, "STAP_SESSION_RUNNING", "sdp->probe",
cda141c2 9381 "stp_probe_type_kretprobe");
d9aed31e 9382 s.op->newline() << "c->kregs = regs;";
6dceb5c9 9383 s.op->newline() << "c->ips.krp.pi = inst;"; // for assisting runtime's backtrace logic
6415ddde
MW
9384
9385 // Make it look like the IP is set as it wouldn't have been replaced
9386 // by a breakpoint instruction when calling real probe handler. Reset
9387 // IP regs on return, so we don't confuse kprobes. PR10458
9388 s.op->newline() << "{";
9389 s.op->indent(1);
d9aed31e 9390 s.op->newline() << "unsigned long kprobes_ip = REG_IP(c->kregs);";
259d54c0 9391 s.op->newline() << "SET_REG_IP(regs, (unsigned long) inst->rp->kp.addr);";
26e63673 9392 s.op->newline() << "(*sdp->probe->ph) (c);";
259d54c0 9393 s.op->newline() << "SET_REG_IP(regs, kprobes_ip);";
6415ddde
MW
9394 s.op->newline(-1) << "}";
9395
ef1337ee 9396 common_probe_entryfn_epilogue (s, true, otf_safe_context(s));
e6fe60e7
AM
9397 s.op->newline() << "return 0;";
9398 s.op->newline(-1) << "}";
bd659351 9399
03a4ec63 9400 s.op->newline() << "#ifdef STAPCONF_KALLSYMS_ON_EACH_SYMBOL";
bd659351
MW
9401 s.op->newline() << "static int kprobe_resolve(void *data, const char *name,";
9402 s.op->newline() << " struct module *owner,";
9403 s.op->newline() << " unsigned long val) {";
9404 s.op->newline(1) << "int i;";
fc1d2aa2
MW
9405 s.op->newline() << "int *p = (int *) data;";
9406 s.op->newline() << "for (i=0; i<" << probes_by_module.size()
9407 << " && *p > 0; i++) {";
bd659351 9408 s.op->newline(1) << "struct stap_dwarfless_probe *sdp = & stap_dwarfless_probes[i];";
8c272819
YW
9409 s.op->newline() << "if (! sdp->address) {";
9410 s.op->indent(1);
9411 s.op->newline() << "const char *colon;";
9412 s.op->newline() << "if (owner && (colon = strchr(sdp->symbol_string, ':'))) {";
9413 s.op->indent(1);
9414 s.op->newline() << "if ((strlen(owner->name) == (colon - sdp->symbol_string))";
9415 s.op->newline() << " && (strncmp(sdp->symbol_string, owner->name, colon - sdp->symbol_string) == 0)";
9416 s.op->newline() << " && (strcmp(colon + 1, name) == 0)) {";
9417 s.op->newline(1) << "sdp->address = val;";
9418 s.op->newline() << "(*p)--;";
9419 s.op->newline(-1) << "}";
9420 s.op->newline(-1) << "}";
9421 s.op->newline() << "else {";
fc1d2aa2 9422 s.op->newline(1) << "if (strcmp(sdp->symbol_string, name) == 0) {";
bd659351 9423 s.op->newline(1) << "sdp->address = val;";
fc1d2aa2
MW
9424 s.op->newline() << "(*p)--;";
9425 s.op->newline(-1) << "}";
8c272819
YW
9426 s.op->newline(-1) << "}";
9427 s.op->newline(-1) << "}";
9428 s.op->newline(-1) << "}";
fc1d2aa2 9429 s.op->newline() << "return (p > 0) ? 0 : -1;";
bd659351 9430 s.op->newline(-1) << "}";
03a4ec63 9431 s.op->newline() << "#endif";
935447c8
DS
9432}
9433
e6fe60e7 9434
6270adc1 9435void
e6fe60e7 9436kprobe_derived_probe_group::emit_module_init (systemtap_session& s)
6270adc1 9437{
03a4ec63 9438 s.op->newline() << "#ifdef STAPCONF_KALLSYMS_ON_EACH_SYMBOL";
fc1d2aa2
MW
9439 s.op->newline() << "{";
9440 s.op->newline(1) << "int p = 0;";
9441 s.op->newline() << "for (i = 0; i < " << probes_by_module.size() << "; i++) {";
9442 s.op->newline(1) << "struct stap_dwarfless_probe *sdp = & stap_dwarfless_probes[i];";
9443 s.op->newline() << "if (! sdp->address)";
9444 s.op->newline(1) << "p++;";
9445 s.op->newline(-2) << "}";
9446 s.op->newline() << "kallsyms_on_each_symbol(kprobe_resolve, &p);";
9447 s.op->newline(-1) << "}";
03a4ec63 9448 s.op->newline() << "#endif";
bd659351 9449
e6fe60e7 9450 s.op->newline() << "for (i=0; i<" << probes_by_module.size() << "; i++) {";
c8d9d15e 9451 s.op->newline(1) << "struct stap_dwarfless_probe *sdp = & stap_dwarfless_probes[i];";
e6fe60e7 9452 s.op->newline() << "struct stap_dwarfless_kprobe *kp = & stap_dwarfless_kprobes[i];";
c8d9d15e 9453 s.op->newline() << "void *addr = (void *) sdp->address;";
03a4ec63
MW
9454 s.op->newline() << "const char *symbol_name = addr ? NULL : sdp->symbol_string;";
9455
9456 s.op->newline() << "#ifdef STAPCONF_KALLSYMS_ON_EACH_SYMBOL";
bd659351
MW
9457 s.op->newline() << "if (! addr) {";
9458 s.op->newline(1) << "sdp->registered_p = 0;";
9319b767
MW
9459 s.op->newline() << "if (!sdp->optional_p)";
9460 s.op->newline(1) << "_stp_warn (\"probe %s registration error (symbol not found)\", probe_point);";
9461 s.op->newline(-1) << "continue;";
bd659351 9462 s.op->newline(-1) << "}";
03a4ec63
MW
9463 s.op->newline() << "#endif";
9464
26e63673 9465 s.op->newline() << "probe_point = sdp->probe->pp;"; // for error messages
e6fe60e7 9466 s.op->newline() << "if (sdp->return_p) {";
c8d9d15e 9467 s.op->newline(1) << "kp->u.krp.kp.addr = addr;";
766cee5f 9468 s.op->newline() << "#ifdef STAPCONF_KPROBE_SYMBOL_NAME";
03a4ec63 9469 s.op->newline() << "kp->u.krp.kp.symbol_name = (char *) symbol_name;";
766cee5f 9470 s.op->newline() << "#endif";
e6fe60e7
AM
9471 s.op->newline() << "if (sdp->maxactive_p) {";
9472 s.op->newline(1) << "kp->u.krp.maxactive = sdp->maxactive_val;";
9473 s.op->newline(-1) << "} else {";
f07c3b68 9474 s.op->newline(1) << "kp->u.krp.maxactive = KRETACTIVE;";
e6fe60e7 9475 s.op->newline(-1) << "}";
88747011 9476 s.op->newline() << "kp->u.krp.handler = &enter_kretprobe2_probe;";
e6fe60e7
AM
9477 // to ensure safeness of bspcache, always use aggr_kprobe on ia64
9478 s.op->newline() << "#ifdef __ia64__";
e6fe60e7 9479 s.op->newline() << "kp->dummy.addr = kp->u.krp.kp.addr;";
766cee5f 9480 s.op->newline() << "#ifdef STAPCONF_KPROBE_SYMBOL_NAME";
c8d9d15e 9481 s.op->newline() << "kp->dummy.symbol_name = kp->u.krp.kp.symbol_name;";
766cee5f 9482 s.op->newline() << "#endif";
c8d9d15e 9483 s.op->newline() << "kp->dummy.pre_handler = NULL;";
e6fe60e7
AM
9484 s.op->newline() << "rc = register_kprobe (& kp->dummy);";
9485 s.op->newline() << "if (rc == 0) {";
9486 s.op->newline(1) << "rc = register_kretprobe (& kp->u.krp);";
9487 s.op->newline() << "if (rc != 0)";
9488 s.op->newline(1) << "unregister_kprobe (& kp->dummy);";
9489 s.op->newline(-2) << "}";
9490 s.op->newline() << "#else";
9491 s.op->newline() << "rc = register_kretprobe (& kp->u.krp);";
9492 s.op->newline() << "#endif";
9493 s.op->newline(-1) << "} else {";
9494 // to ensure safeness of bspcache, always use aggr_kprobe on ia64
c8d9d15e 9495 s.op->newline(1) << "kp->u.kp.addr = addr;";
766cee5f 9496 s.op->newline() << "#ifdef STAPCONF_KPROBE_SYMBOL_NAME";
03a4ec63 9497 s.op->newline() << "kp->u.kp.symbol_name = (char *) symbol_name;";
766cee5f 9498 s.op->newline() << "#endif";
88747011 9499 s.op->newline() << "kp->u.kp.pre_handler = &enter_kprobe2_probe;";
e6fe60e7 9500 s.op->newline() << "#ifdef __ia64__";
e6fe60e7 9501 s.op->newline() << "kp->dummy.pre_handler = NULL;";
c8d9d15e 9502 s.op->newline() << "kp->dummy.addr = kp->u.kp.addr;";
766cee5f 9503 s.op->newline() << "#ifdef STAPCONF_KPROBE_SYMBOL_NAME";
c8d9d15e 9504 s.op->newline() << "kp->dummy.symbol_name = kp->u.kp.symbol_name;";
766cee5f 9505 s.op->newline() << "#endif";
e6fe60e7
AM
9506 s.op->newline() << "rc = register_kprobe (& kp->dummy);";
9507 s.op->newline() << "if (rc == 0) {";
9508 s.op->newline(1) << "rc = register_kprobe (& kp->u.kp);";
9509 s.op->newline() << "if (rc != 0)";
9510 s.op->newline(1) << "unregister_kprobe (& kp->dummy);";
9511 s.op->newline(-2) << "}";
9512 s.op->newline() << "#else";
9513 s.op->newline() << "rc = register_kprobe (& kp->u.kp);";
9514 s.op->newline() << "#endif";
9515 s.op->newline(-1) << "}";
9516 s.op->newline() << "if (rc) {"; // PR6749: tolerate a failed register_*probe.
9517 s.op->newline(1) << "sdp->registered_p = 0;";
b350f56b 9518 s.op->newline() << "if (!sdp->optional_p)";
50b6acf7 9519 s.op->newline(1) << "_stp_warn (\"probe %s (address 0x%lx) registration error (rc %d)\", probe_point, (unsigned long) addr, rc);";
b350f56b 9520 s.op->newline(-1) << "rc = 0;"; // continue with other probes
e6fe60e7
AM
9521 // XXX: shall we increment numskipped?
9522 s.op->newline(-1) << "}";
6270adc1 9523
e6fe60e7
AM
9524 s.op->newline() << "else sdp->registered_p = 1;";
9525 s.op->newline(-1) << "}"; // for loop
6270adc1
MH
9526}
9527
b4be7cbc 9528
e6fe60e7
AM
9529void
9530kprobe_derived_probe_group::emit_module_exit (systemtap_session& s)
935447c8 9531{
e6fe60e7
AM
9532 //Unregister kprobes by batch interfaces.
9533 s.op->newline() << "#if defined(STAPCONF_UNREGISTER_KPROBES)";
9534 s.op->newline() << "j = 0;";
9535 s.op->newline() << "for (i=0; i<" << probes_by_module.size() << "; i++) {";
9536 s.op->newline(1) << "struct stap_dwarfless_probe *sdp = & stap_dwarfless_probes[i];";
9537 s.op->newline() << "struct stap_dwarfless_kprobe *kp = & stap_dwarfless_kprobes[i];";
9538 s.op->newline() << "if (! sdp->registered_p) continue;";
9539 s.op->newline() << "if (!sdp->return_p)";
c9116e99 9540 s.op->newline(1) << "stap_unreg_kprobes2[j++] = &kp->u.kp;";
e6fe60e7 9541 s.op->newline(-2) << "}";
c9116e99 9542 s.op->newline() << "unregister_kprobes((struct kprobe **)stap_unreg_kprobes2, j);";
e6fe60e7
AM
9543 s.op->newline() << "j = 0;";
9544 s.op->newline() << "for (i=0; i<" << probes_by_module.size() << "; i++) {";
9545 s.op->newline(1) << "struct stap_dwarfless_probe *sdp = & stap_dwarfless_probes[i];";
9546 s.op->newline() << "struct stap_dwarfless_kprobe *kp = & stap_dwarfless_kprobes[i];";
9547 s.op->newline() << "if (! sdp->registered_p) continue;";
9548 s.op->newline() << "if (sdp->return_p)";
c9116e99 9549 s.op->newline(1) << "stap_unreg_kprobes2[j++] = &kp->u.krp;";
e6fe60e7 9550 s.op->newline(-2) << "}";
c9116e99 9551 s.op->newline() << "unregister_kretprobes((struct kretprobe **)stap_unreg_kprobes2, j);";
e6fe60e7
AM
9552 s.op->newline() << "#ifdef __ia64__";
9553 s.op->newline() << "j = 0;";
9554 s.op->newline() << "for (i=0; i<" << probes_by_module.size() << "; i++) {";
9555 s.op->newline(1) << "struct stap_dwarfless_probe *sdp = & stap_dwarfless_probes[i];";
9556 s.op->newline() << "struct stap_dwarfless_kprobe *kp = & stap_dwarfless_kprobes[i];";
9557 s.op->newline() << "if (! sdp->registered_p) continue;";
c9116e99 9558 s.op->newline() << "stap_unreg_kprobes2[j++] = &kp->dummy;";
e6fe60e7 9559 s.op->newline(-1) << "}";
c9116e99 9560 s.op->newline() << "unregister_kprobes((struct kprobe **)stap_unreg_kprobes2, j);";
e6fe60e7
AM
9561 s.op->newline() << "#endif";
9562 s.op->newline() << "#endif";
3e3bd7b6 9563
e6fe60e7
AM
9564 s.op->newline() << "for (i=0; i<" << probes_by_module.size() << "; i++) {";
9565 s.op->newline(1) << "struct stap_dwarfless_probe *sdp = & stap_dwarfless_probes[i];";
9566 s.op->newline() << "struct stap_dwarfless_kprobe *kp = & stap_dwarfless_kprobes[i];";
9567 s.op->newline() << "if (! sdp->registered_p) continue;";
9568 s.op->newline() << "if (sdp->return_p) {";
9569 s.op->newline() << "#if !defined(STAPCONF_UNREGISTER_KPROBES)";
9570 s.op->newline(1) << "unregister_kretprobe (&kp->u.krp);";
9571 s.op->newline() << "#endif";
065d5567 9572 s.op->newline() << "atomic_add (kp->u.krp.nmissed, skipped_count());";
e6fe60e7
AM
9573 s.op->newline() << "#ifdef STP_TIMING";
9574 s.op->newline() << "if (kp->u.krp.nmissed)";
26e63673 9575 s.op->newline(1) << "_stp_warn (\"Skipped due to missed kretprobe/1 on '%s': %d\\n\", sdp->probe->pp, kp->u.krp.nmissed);";
e6fe60e7 9576 s.op->newline(-1) << "#endif";
065d5567 9577 s.op->newline() << "atomic_add (kp->u.krp.kp.nmissed, skipped_count());";
e6fe60e7
AM
9578 s.op->newline() << "#ifdef STP_TIMING";
9579 s.op->newline() << "if (kp->u.krp.kp.nmissed)";
26e63673 9580 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
9581 s.op->newline(-1) << "#endif";
9582 s.op->newline(-1) << "} else {";
9583 s.op->newline() << "#if !defined(STAPCONF_UNREGISTER_KPROBES)";
9584 s.op->newline(1) << "unregister_kprobe (&kp->u.kp);";
9585 s.op->newline() << "#endif";
065d5567 9586 s.op->newline() << "atomic_add (kp->u.kp.nmissed, skipped_count());";
e6fe60e7
AM
9587 s.op->newline() << "#ifdef STP_TIMING";
9588 s.op->newline() << "if (kp->u.kp.nmissed)";
26e63673 9589 s.op->newline(1) << "_stp_warn (\"Skipped due to missed kprobe on '%s': %lu\\n\", sdp->probe->pp, kp->u.kp.nmissed);";
e6fe60e7
AM
9590 s.op->newline(-1) << "#endif";
9591 s.op->newline(-1) << "}";
9592 s.op->newline() << "#if !defined(STAPCONF_UNREGISTER_KPROBES) && defined(__ia64__)";
9593 s.op->newline() << "unregister_kprobe (&kp->dummy);";
9594 s.op->newline() << "#endif";
9595 s.op->newline() << "sdp->registered_p = 0;";
9596 s.op->newline(-1) << "}";
f8a968bc
JS
9597}
9598
e6fe60e7 9599struct kprobe_builder: public derived_probe_builder
3c1b3d06 9600{
9fdf787d 9601public:
2a639817 9602 kprobe_builder() {}
9fdf787d 9603
2a639817 9604 void build_no_more (systemtap_session &s) {}
9fdf787d 9605
e6fe60e7
AM
9606 virtual void build(systemtap_session & sess,
9607 probe * base,
9608 probe_point * location,
9609 literal_map_t const & parameters,
9610 vector<derived_probe *> & finished_results);
9611};
3c1b3d06
FCE
9612
9613
79189b84 9614void
05fb3e0c 9615kprobe_builder::build(systemtap_session & sess,
e6fe60e7
AM
9616 probe * base,
9617 probe_point * location,
9618 literal_map_t const & parameters,
9619 vector<derived_probe *> & finished_results)
79189b84 9620{
e6fe60e7 9621 string function_string_val, module_string_val;
05fb3e0c 9622 string path, library, path_tgt, library_tgt;
b6371390
JS
9623 int64_t statement_num_val = 0, maxactive_val = 0;
9624 bool has_function_str, has_module_str, has_statement_num;
e6afb07b 9625 bool has_absolute, has_call, has_return, has_maxactive;
b642c901 9626 bool has_path, has_library;
79189b84 9627
b6371390
JS
9628 has_function_str = get_param(parameters, TOK_FUNCTION, function_string_val);
9629 has_module_str = get_param(parameters, TOK_MODULE, module_string_val);
e6afb07b 9630 has_call = has_null_param (parameters, TOK_CALL);
b6371390
JS
9631 has_return = has_null_param (parameters, TOK_RETURN);
9632 has_maxactive = get_param(parameters, TOK_MAXACTIVE, maxactive_val);
9633 has_statement_num = get_param(parameters, TOK_STATEMENT, statement_num_val);
9634 has_absolute = has_null_param (parameters, TOK_ABSOLUTE);
b642c901
SC
9635 has_path = get_param (parameters, TOK_PROCESS, path);
9636 has_library = get_param (parameters, TOK_LIBRARY, library);
9637
9638 if (has_path)
05fb3e0c
WF
9639 {
9640 path = find_executable (path, sess.sysroot, sess.sysenv);
9641 path_tgt = path_remove_sysroot(sess, path);
9642 }
b642c901 9643 if (has_library)
05fb3e0c
WF
9644 {
9645 library = find_executable (library, sess.sysroot, sess.sysenv,
9646 "LD_LIBRARY_PATH");
9647 library_tgt = path_remove_sysroot(sess, library);
9648 }
c57ea854 9649
b6371390 9650 if (has_function_str)
6fb70fb7 9651 {
2a639817 9652 if (has_module_str)
9fdf787d
DS
9653 {
9654 function_string_val = module_string_val + ":" + function_string_val;
9655 derived_probe *dp
23dc94f6
DS
9656 = new kprobe_derived_probe (sess, finished_results, base,
9657 location, function_string_val,
e6afb07b
JL
9658 0, has_call, has_return,
9659 has_statement_num, has_maxactive,
9660 has_path, has_library, maxactive_val,
9661 path_tgt, library_tgt);
9fdf787d
DS
9662 finished_results.push_back (dp);
9663 }
9664 else
9665 {
2a639817
JS
9666 vector<string> matches;
9667
9668 // Simple names can be found directly
9669 if (function_string_val.find_first_of("*?[") == string::npos)
9670 {
9671 if (sess.kernel_functions.count(function_string_val))
9672 matches.push_back(function_string_val);
9673 }
9674 else // Search function name list for matching names
9675 {
9676 for (set<string>::const_iterator it = sess.kernel_functions.begin();
9677 it != sess.kernel_functions.end(); it++)
9678 // fnmatch returns zero for matching.
9679 if (fnmatch(function_string_val.c_str(), it->c_str(), 0) == 0)
9680 matches.push_back(*it);
9681 }
86758d5f 9682
2a639817
JS
9683 for (vector<string>::const_iterator it = matches.begin();
9684 it != matches.end(); it++)
9fdf787d 9685 {
2a639817
JS
9686 derived_probe *dp
9687 = new kprobe_derived_probe (sess, finished_results, base,
e6afb07b
JL
9688 location, *it, 0, has_call,
9689 has_return, has_statement_num,
2a639817
JS
9690 has_maxactive, has_path,
9691 has_library, maxactive_val,
9692 path_tgt, library_tgt);
9693 finished_results.push_back (dp);
9fdf787d
DS
9694 }
9695 }
6fb70fb7 9696 }
e6fe60e7 9697 else
b6371390
JS
9698 {
9699 // assert guru mode for absolute probes
9700 if ( has_statement_num && has_absolute && !base->privileged )
dc09353a 9701 throw SEMANTIC_ERROR (_("absolute statement probe in unprivileged script; need stap -g"), base->tok);
b6371390 9702
23dc94f6
DS
9703 finished_results.push_back (new kprobe_derived_probe (sess,
9704 finished_results,
9705 base,
b6371390
JS
9706 location, "",
9707 statement_num_val,
e6afb07b 9708 has_call,
b6371390
JS
9709 has_return,
9710 has_statement_num,
9711 has_maxactive,
b642c901
SC
9712 has_path,
9713 has_library,
9714 maxactive_val,
05fb3e0c
WF
9715 path_tgt,
9716 library_tgt));
96b030fe 9717 }
79189b84
JS
9718}
9719
23dc94f6
DS
9720
9721void
9722kprobe_var_expanding_visitor::visit_entry_op (entry_op *e)
9723{
9724 expression *repl = e;
9725
bd5b25e1
JS
9726 if (has_return)
9727 {
9728 // expand the operand as if it weren't a return probe
9729 has_return = false;
9730 replace (e->operand);
9731 has_return = true;
23dc94f6 9732
bd5b25e1
JS
9733 // XXX it would be nice to use gen_kretprobe_saved_return when
9734 // available, but it requires knowing the types already, which is
9735 // problematic for arbitrary expressons.
9736 repl = gen_mapped_saved_return (sess, e->operand, "entry",
9737 add_block, add_block_tid,
9738 add_call_probe, add_call_probe_tid);
9739 }
23dc94f6
DS
9740 provide (repl);
9741}
9742
9743
dd225250
PS
9744// ------------------------------------------------------------------------
9745// Hardware breakpoint based probes.
9746// ------------------------------------------------------------------------
9747
9748static const string TOK_HWBKPT("data");
9749static const string TOK_HWBKPT_WRITE("write");
9750static const string TOK_HWBKPT_RW("rw");
9751static const string TOK_LENGTH("length");
9752
9753#define HWBKPT_READ 0
9754#define HWBKPT_WRITE 1
9755#define HWBKPT_RW 2
9756struct hwbkpt_derived_probe: public derived_probe
9757{
9758 hwbkpt_derived_probe (probe *base,
9759 probe_point *location,
9760 uint64_t addr,
9761 string symname,
9762 unsigned int len,
9763 bool has_only_read_access,
9764 bool has_only_write_access,
9765 bool has_rw_access
9766 );
9767 Dwarf_Addr hwbkpt_addr;
9768 string symbol_name;
9769 unsigned int hwbkpt_access,hwbkpt_len;
9770
9771 void printsig (std::ostream &o) const;
9772 void join_group (systemtap_session& s);
9773};
9774
9775struct hwbkpt_derived_probe_group: public derived_probe_group
9776{
dd225250 9777private:
dac77b80 9778 vector<hwbkpt_derived_probe*> hwbkpt_probes;
dd225250
PS
9779
9780public:
9781 void enroll (hwbkpt_derived_probe* probe, systemtap_session& s);
9782 void emit_module_decls (systemtap_session& s);
9783 void emit_module_init (systemtap_session& s);
9784 void emit_module_exit (systemtap_session& s);
9785};
9786
9787hwbkpt_derived_probe::hwbkpt_derived_probe (probe *base,
9ea68eb9
JS
9788 probe_point *location,
9789 uint64_t addr,
9790 string symname,
9791 unsigned int len,
9792 bool has_only_read_access,
9793 bool has_only_write_access,
822a6a3d 9794 bool):
4c5d1300 9795 derived_probe (base, location, true /* .components soon rewritten */ ),
dd225250
PS
9796 hwbkpt_addr (addr),
9797 symbol_name (symname),
9798 hwbkpt_len (len)
9799{
9800 this->tok = base->tok;
9801
9802 vector<probe_point::component*> comps;
9803 comps.push_back (new probe_point::component(TOK_KERNEL));
9804
9805 if (hwbkpt_addr)
9ea68eb9
JS
9806 comps.push_back (new probe_point::component (TOK_HWBKPT,
9807 new literal_number(hwbkpt_addr, true)));
9808 else if (symbol_name.size())
9809 comps.push_back (new probe_point::component (TOK_HWBKPT, new literal_string(symbol_name)));
dd225250
PS
9810
9811 comps.push_back (new probe_point::component (TOK_LENGTH, new literal_number(hwbkpt_len)));
9812
9813 if (has_only_read_access)
9ea68eb9 9814 this->hwbkpt_access = HWBKPT_READ ;
dd225250
PS
9815//TODO add code for comps.push_back for read, since this flag is not for x86
9816
9817 else
9ea68eb9
JS
9818 {
9819 if (has_only_write_access)
9820 {
9821 this->hwbkpt_access = HWBKPT_WRITE ;
9822 comps.push_back (new probe_point::component(TOK_HWBKPT_WRITE));
9823 }
9824 else
9825 {
9826 this->hwbkpt_access = HWBKPT_RW ;
9827 comps.push_back (new probe_point::component(TOK_HWBKPT_RW));
9828 }
9829 }
dd225250
PS
9830
9831 this->sole_location()->components = comps;
9832}
9833
9834void hwbkpt_derived_probe::printsig (ostream& o) const
9835{
9836 sole_location()->print (o);
9837 printsig_nested (o);
9838}
9839
9840void hwbkpt_derived_probe::join_group (systemtap_session& s)
9841{
dac77b80
FCE
9842 if (! s.hwbkpt_derived_probes)
9843 s.hwbkpt_derived_probes = new hwbkpt_derived_probe_group ();
dd225250 9844 s.hwbkpt_derived_probes->enroll (this, s);
ca6d3b0f 9845 this->group = s.hwbkpt_derived_probes;
dd225250
PS
9846}
9847
9848void hwbkpt_derived_probe_group::enroll (hwbkpt_derived_probe* p, systemtap_session& s)
9849{
dac77b80
FCE
9850 hwbkpt_probes.push_back (p);
9851
9852 unsigned max_hwbkpt_probes_by_arch = 0;
9853 if (s.architecture == "i386" || s.architecture == "x86_64")
9854 max_hwbkpt_probes_by_arch = 4;
9855 else if (s.architecture == "s390")
9856 max_hwbkpt_probes_by_arch = 1;
9857
c57ea854 9858 if (hwbkpt_probes.size() >= max_hwbkpt_probes_by_arch)
2713ea24 9859 s.print_warning (_F("Too many hardware breakpoint probes requested for %s (%zu vs. %u)",
b530b5b3 9860 s.architecture.c_str(), hwbkpt_probes.size(), max_hwbkpt_probes_by_arch));
dd225250
PS
9861}
9862
9863void
9864hwbkpt_derived_probe_group::emit_module_decls (systemtap_session& s)
9865{
dac77b80 9866 if (hwbkpt_probes.empty()) return;
dd225250
PS
9867
9868 s.op->newline() << "/* ---- hwbkpt-based probes ---- */";
9869
9870 s.op->newline() << "#include <linux/perf_event.h>";
9871 s.op->newline() << "#include <linux/hw_breakpoint.h>";
9872 s.op->newline();
9873
9874 // Forward declare the master entry functions
23063de1 9875 s.op->newline() << "#ifdef STAPCONF_PERF_HANDLER_NMI";
dd225250
PS
9876 s.op->newline() << "static int enter_hwbkpt_probe (struct perf_event *bp,";
9877 s.op->line() << " int nmi,";
9878 s.op->line() << " struct perf_sample_data *data,";
9879 s.op->line() << " struct pt_regs *regs);";
23063de1
FCE
9880 s.op->newline() << "#else";
9881 s.op->newline() << "static int enter_hwbkpt_probe (struct perf_event *bp,";
9882 s.op->line() << " struct perf_sample_data *data,";
9883 s.op->line() << " struct pt_regs *regs);";
9884 s.op->newline() << "#endif";
79189b84 9885
dd225250
PS
9886 // Emit the actual probe list.
9887
9888 s.op->newline() << "static struct perf_event_attr ";
dac77b80 9889 s.op->newline() << "stap_hwbkpt_probe_array[" << hwbkpt_probes.size() << "];";
dd225250
PS
9890
9891 s.op->newline() << "static struct perf_event **";
dac77b80 9892 s.op->newline() << "stap_hwbkpt_ret_array[" << hwbkpt_probes.size() << "];";
dd225250
PS
9893 s.op->newline() << "static struct stap_hwbkpt_probe {";
9894 s.op->newline() << "int registered_p:1;";
43650b10 9895// registered_p = 0 signifies a probe that is unregistered (or failed)
dd225250
PS
9896// registered_p = 1 signifies a probe that got registered successfully
9897
faea5e16 9898 // Symbol Names are mostly small and uniform enough
dd225250 9899 // to justify putting const char*.
dac77b80 9900 s.op->newline() << "const char * const symbol;";
dd225250
PS
9901
9902 s.op->newline() << "const unsigned long address;";
9903 s.op->newline() << "uint8_t atype;";
bb0a4e12 9904 s.op->newline() << "unsigned int len;";
7c3e97f4 9905 s.op->newline() << "const struct stap_probe * const probe;";
dd225250
PS
9906 s.op->newline() << "} stap_hwbkpt_probes[] = {";
9907 s.op->indent(1);
9908
dac77b80 9909 for (unsigned int it = 0; it < hwbkpt_probes.size(); it++)
dd225250 9910 {
dac77b80 9911 hwbkpt_derived_probe* p = hwbkpt_probes.at(it);
dd225250 9912 s.op->newline() << "{";
dd225250
PS
9913 if (p->symbol_name.size())
9914 s.op->line() << " .address=(unsigned long)0x0" << "ULL,";
9915 else
9916 s.op->line() << " .address=(unsigned long)0x" << hex << p->hwbkpt_addr << dec << "ULL,";
9917 switch(p->hwbkpt_access){
9918 case HWBKPT_READ:
9919 s.op->line() << " .atype=HW_BREAKPOINT_R ,";
bb0a4e12 9920 break;
dd225250
PS
9921 case HWBKPT_WRITE:
9922 s.op->line() << " .atype=HW_BREAKPOINT_W ,";
bb0a4e12 9923 break;
dd225250
PS
9924 case HWBKPT_RW:
9925 s.op->line() << " .atype=HW_BREAKPOINT_R|HW_BREAKPOINT_W ,";
bb0a4e12 9926 break;
dd225250
PS
9927 };
9928 s.op->line() << " .len=" << p->hwbkpt_len << ",";
faea5e16 9929 s.op->line() << " .probe=" << common_probe_init (p) << ",";
dd225250 9930 s.op->line() << " .symbol=\"" << p->symbol_name << "\",";
dd225250
PS
9931 s.op->line() << " },";
9932 }
dac77b80 9933 s.op->newline(-1) << "};";
dd225250
PS
9934
9935 // Emit the hwbkpt callback function
9936 s.op->newline() ;
23063de1 9937 s.op->newline() << "#ifdef STAPCONF_PERF_HANDLER_NMI";
dd225250
PS
9938 s.op->newline() << "static int enter_hwbkpt_probe (struct perf_event *bp,";
9939 s.op->line() << " int nmi,";
9940 s.op->line() << " struct perf_sample_data *data,";
9941 s.op->line() << " struct pt_regs *regs) {";
23063de1
FCE
9942 s.op->newline() << "#else";
9943 s.op->newline() << "static int enter_hwbkpt_probe (struct perf_event *bp,";
9944 s.op->line() << " struct perf_sample_data *data,";
9945 s.op->line() << " struct pt_regs *regs) {";
9946 s.op->newline() << "#endif";
dac77b80
FCE
9947 s.op->newline(1) << "unsigned int i;";
9948 s.op->newline() << "if (bp->attr.type != PERF_TYPE_BREAKPOINT) return -1;";
9949 s.op->newline() << "for (i=0; i<" << hwbkpt_probes.size() << "; i++) {";
9950 s.op->newline(1) << "struct perf_event_attr *hp = & stap_hwbkpt_probe_array[i];";
9951 // XXX: why not match stap_hwbkpt_ret_array[i] against bp instead?
9952 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) {";
9953 s.op->newline(1) << "struct stap_hwbkpt_probe *sdp = &stap_hwbkpt_probes[i];";
71db462b 9954 common_probe_entryfn_prologue (s, "STAP_SESSION_RUNNING", "sdp->probe",
cda141c2 9955 "stp_probe_type_hwbkpt");
d9aed31e 9956 s.op->newline() << "if (user_mode(regs)) {";
e04b5d74 9957 s.op->newline(1)<< "c->user_mode_p = 1;";
d9aed31e
MW
9958 s.op->newline() << "c->uregs = regs;";
9959 s.op->newline(-1) << "} else {";
9960 s.op->newline(1) << "c->kregs = regs;";
9961 s.op->newline(-1) << "}";
26e63673 9962 s.op->newline() << "(*sdp->probe->ph) (c);";
ef1337ee 9963 common_probe_entryfn_epilogue (s, true, otf_safe_context(s));
dac77b80 9964 s.op->newline(-1) << "}";
dd225250
PS
9965 s.op->newline(-1) << "}";
9966 s.op->newline() << "return 0;";
dac77b80 9967 s.op->newline(-1) << "}";
dd225250
PS
9968}
9969
9970void
9971hwbkpt_derived_probe_group::emit_module_init (systemtap_session& s)
9972{
dac77b80 9973 s.op->newline() << "for (i=0; i<" << hwbkpt_probes.size() << "; i++) {";
dd225250
PS
9974 s.op->newline(1) << "struct stap_hwbkpt_probe *sdp = & stap_hwbkpt_probes[i];";
9975 s.op->newline() << "struct perf_event_attr *hp = & stap_hwbkpt_probe_array[i];";
9976 s.op->newline() << "void *addr = (void *) sdp->address;";
9977 s.op->newline() << "const char *hwbkpt_symbol_name = addr ? NULL : sdp->symbol;";
dac77b80
FCE
9978 s.op->newline() << "hw_breakpoint_init(hp);";
9979 s.op->newline() << "if (addr)";
9980 s.op->newline(1) << "hp->bp_addr = (unsigned long) addr;";
9981 s.op->newline(-1) << "else { ";
9982 s.op->newline(1) << "hp->bp_addr = kallsyms_lookup_name(hwbkpt_symbol_name);";
9983 s.op->newline() << "if (!hp->bp_addr) { ";
26e63673 9984 s.op->newline(1) << "_stp_warn(\"Probe %s registration skipped: invalid symbol %s \",sdp->probe->pp,hwbkpt_symbol_name);";
dac77b80
FCE
9985 s.op->newline() << "continue;";
9986 s.op->newline(-1) << "}";
9987 s.op->newline(-1) << "}";
9988 s.op->newline() << "hp->bp_type = sdp->atype;";
9989
9990 // On x86 & x86-64, hp->bp_len is not just a number but a macro/enum (!?!).
c57ea854 9991 if (s.architecture == "i386" || s.architecture == "x86_64" )
dac77b80
FCE
9992 {
9993 s.op->newline() << "switch(sdp->len) {";
9994 s.op->newline() << "case 1:";
9995 s.op->newline(1) << "hp->bp_len = HW_BREAKPOINT_LEN_1;";
9996 s.op->newline() << "break;";
9997 s.op->newline(-1) << "case 2:";
9998 s.op->newline(1) << "hp->bp_len = HW_BREAKPOINT_LEN_2;";
9999 s.op->newline() << "break;";
10000 s.op->newline(-1) << "case 3:";
10001 s.op->newline() << "case 4:";
10002 s.op->newline(1) << "hp->bp_len = HW_BREAKPOINT_LEN_4;";
10003 s.op->newline() << "break;";
10004 s.op->newline(-1) << "case 5:";
10005 s.op->newline() << "case 6:";
10006 s.op->newline() << "case 7:";
10007 s.op->newline() << "case 8:";
10008 s.op->newline() << "default:"; // XXX: could instead reject
10009 s.op->newline(1) << "hp->bp_len = HW_BREAKPOINT_LEN_8;";
10010 s.op->newline() << "break;";
10011 s.op->newline(-1) << "}";
10012 }
10013 else // other architectures presumed straightforward
10014 s.op->newline() << "hp->bp_len = sdp->len;";
10015
26e63673 10016 s.op->newline() << "probe_point = sdp->probe->pp;"; // for error messages
b273669e
MW
10017 s.op->newline() << "#ifdef STAPCONF_HW_BREAKPOINT_CONTEXT";
10018 s.op->newline() << "stap_hwbkpt_ret_array[i] = register_wide_hw_breakpoint(hp, (void *)&enter_hwbkpt_probe, NULL);";
10019 s.op->newline() << "#else";
dac77b80 10020 s.op->newline() << "stap_hwbkpt_ret_array[i] = register_wide_hw_breakpoint(hp, (void *)&enter_hwbkpt_probe);";
b273669e 10021 s.op->newline() << "#endif";
43650b10 10022 s.op->newline() << "rc = 0;";
dac77b80 10023 s.op->newline() << "if (IS_ERR(stap_hwbkpt_ret_array[i])) {";
43650b10
WC
10024 s.op->newline(1) << "rc = PTR_ERR(stap_hwbkpt_ret_array[i]);";
10025 s.op->newline() << "stap_hwbkpt_ret_array[i] = 0;";
10026 s.op->newline(-1) << "}";
217ef1f4
WC
10027 s.op->newline() << "if (rc) {";
10028 s.op->newline(1) << "_stp_warn(\"Hwbkpt probe %s: registration error %d, addr %p, name %s\", probe_point, rc, addr, hwbkpt_symbol_name);";
43650b10 10029 s.op->newline() << "sdp->registered_p = 0;";
dac77b80 10030 s.op->newline(-1) << "}";
dd225250 10031 s.op->newline() << " else sdp->registered_p = 1;";
dd225250
PS
10032 s.op->newline(-1) << "}"; // for loop
10033}
10034
10035void
10036hwbkpt_derived_probe_group::emit_module_exit (systemtap_session& s)
10037{
10038 //Unregister hwbkpt probes.
dac77b80 10039 s.op->newline() << "for (i=0; i<" << hwbkpt_probes.size() << "; i++) {";
dd225250 10040 s.op->newline(1) << "struct stap_hwbkpt_probe *sdp = & stap_hwbkpt_probes[i];";
dac77b80
FCE
10041 s.op->newline() << "if (sdp->registered_p == 0) continue;";
10042 s.op->newline() << "unregister_wide_hw_breakpoint(stap_hwbkpt_ret_array[i]);";
dd225250
PS
10043 s.op->newline() << "sdp->registered_p = 0;";
10044 s.op->newline(-1) << "}";
10045}
10046
10047struct hwbkpt_builder: public derived_probe_builder
10048{
10049 hwbkpt_builder() {}
10050 virtual void build(systemtap_session & sess,
10051 probe * base,
10052 probe_point * location,
10053 literal_map_t const & parameters,
10054 vector<derived_probe *> & finished_results);
10055};
10056
10057void
10058hwbkpt_builder::build(systemtap_session & sess,
10059 probe * base,
10060 probe_point * location,
10061 literal_map_t const & parameters,
10062 vector<derived_probe *> & finished_results)
10063{
10064 string symbol_str_val;
10065 int64_t hwbkpt_address, len;
10066 bool has_addr, has_symbol_str, has_write, has_rw, has_len;
10067
b47f3a55 10068 if (! (sess.kernel_config["CONFIG_PERF_EVENTS"] == string("y")))
dc09353a 10069 throw SEMANTIC_ERROR (_("CONFIG_PERF_EVENTS not available on this kernel"),
b47f3a55
FCE
10070 location->components[0]->tok);
10071 if (! (sess.kernel_config["CONFIG_HAVE_HW_BREAKPOINT"] == string("y")))
dc09353a 10072 throw SEMANTIC_ERROR (_("CONFIG_HAVE_HW_BREAKPOINT not available on this kernel"),
b47f3a55
FCE
10073 location->components[0]->tok);
10074
dd225250
PS
10075 has_addr = get_param (parameters, TOK_HWBKPT, hwbkpt_address);
10076 has_symbol_str = get_param (parameters, TOK_HWBKPT, symbol_str_val);
10077 has_len = get_param (parameters, TOK_LENGTH, len);
10078 has_write = (parameters.find(TOK_HWBKPT_WRITE) != parameters.end());
10079 has_rw = (parameters.find(TOK_HWBKPT_RW) != parameters.end());
10080
b31695eb
JL
10081 // Make an intermediate pp that is well-formed. It's pretty much the same as
10082 // the user-provided one, except that the addr literal is well-typed.
10083 probe_point* well_formed_loc = new probe_point(*location);
10084 well_formed_loc->well_formed = true;
10085
10086 vector<probe_point::component*> well_formed_comps;
10087 vector<probe_point::component*>::iterator it;
10088 for (it = location->components.begin();
10089 it != location->components.end(); ++it)
10090 if ((*it)->functor == TOK_HWBKPT && has_addr)
10091 well_formed_comps.push_back(new probe_point::component(TOK_HWBKPT,
10092 new literal_number(hwbkpt_address, true /* hex */ )));
10093 else
10094 well_formed_comps.push_back(*it);
10095 well_formed_loc->components = well_formed_comps;
10096 probe *new_base = new probe (base, well_formed_loc);
10097
dd225250
PS
10098 if (!has_len)
10099 len = 1;
10100
10101 if (has_addr)
b31695eb 10102 finished_results.push_back (new hwbkpt_derived_probe (new_base,
dd225250
PS
10103 location,
10104 hwbkpt_address,
10105 "",len,0,
10106 has_write,
10107 has_rw));
5d8a0aea 10108 else if (has_symbol_str)
b31695eb 10109 finished_results.push_back (new hwbkpt_derived_probe (new_base,
dd225250
PS
10110 location,
10111 0,
10112 symbol_str_val,len,0,
10113 has_write,
10114 has_rw));
5d8a0aea
FCE
10115 else
10116 assert (0);
dd225250 10117}
342d3f96 10118
0a6f5a3f
JS
10119// ------------------------------------------------------------------------
10120// statically inserted kernel-tracepoint derived probes
10121// ------------------------------------------------------------------------
10122
6fb70fb7 10123struct tracepoint_arg
79189b84 10124{
ad370dcc 10125 string name, c_type, typecast;
dcaa1a65 10126 bool usable, used, isptr;
f8a968bc 10127 Dwarf_Die type_die;
dcaa1a65 10128 tracepoint_arg(): usable(false), used(false), isptr(false) {}
6fb70fb7 10129};
79189b84 10130
0a6f5a3f
JS
10131struct tracepoint_derived_probe: public derived_probe
10132{
79189b84
JS
10133 tracepoint_derived_probe (systemtap_session& s,
10134 dwflpp& dw, Dwarf_Die& func_die,
10135 const string& tracepoint_name,
10136 probe* base_probe, probe_point* location);
bc9a523d 10137
79189b84 10138 systemtap_session& sess;
6fb70fb7
JS
10139 string tracepoint_name, header;
10140 vector <struct tracepoint_arg> args;
bc9a523d 10141
6fb70fb7 10142 void build_args(dwflpp& dw, Dwarf_Die& func_die);
d0bfd2ac 10143 void getargs (std::list<std::string> &arg_set) const;
79189b84 10144 void join_group (systemtap_session& s);
3e3bd7b6 10145 void print_dupe_stamp(ostream& o);
0a6f5a3f 10146};
79189b84
JS
10147
10148
0a6f5a3f 10149struct tracepoint_derived_probe_group: public generic_dpg<tracepoint_derived_probe>
79189b84 10150{
79189b84
JS
10151 void emit_module_decls (systemtap_session& s);
10152 void emit_module_init (systemtap_session& s);
10153 void emit_module_exit (systemtap_session& s);
0a6f5a3f 10154};
79189b84 10155
bc9a523d 10156
f8a968bc
JS
10157struct tracepoint_var_expanding_visitor: public var_expanding_visitor
10158{
10159 tracepoint_var_expanding_visitor(dwflpp& dw, const string& probe_name,
10160 vector <struct tracepoint_arg>& args):
10161 dw (dw), probe_name (probe_name), args (args) {}
10162 dwflpp& dw;
10163 const string& probe_name;
10164 vector <struct tracepoint_arg>& args;
bc9a523d 10165
f8a968bc
JS
10166 void visit_target_symbol (target_symbol* e);
10167 void visit_target_symbol_arg (target_symbol* e);
10168 void visit_target_symbol_context (target_symbol* e);
10169};
79189b84
JS
10170
10171
f8a968bc
JS
10172void
10173tracepoint_var_expanding_visitor::visit_target_symbol_arg (target_symbol* e)
75ead1f7 10174{
cc9001af 10175 string argname = e->sym_name();
75ead1f7 10176
f8a968bc
JS
10177 // search for a tracepoint parameter matching this name
10178 tracepoint_arg *arg = NULL;
10179 for (unsigned i = 0; i < args.size(); ++i)
dcaa1a65 10180 if (args[i].usable && args[i].name == argname)
f8a968bc
JS
10181 {
10182 arg = &args[i];
10183 arg->used = true;
10184 break;
10185 }
75ead1f7 10186
f8a968bc
JS
10187 if (arg == NULL)
10188 {
1d0499c2 10189 set<string> vars;
f8a968bc 10190 for (unsigned i = 0; i < args.size(); ++i)
1d0499c2
JL
10191 vars.insert("$" + args[i].name);
10192 vars.insert("$$name");
10193 vars.insert("$$parms");
10194 vars.insert("$$vars");
10195 string sugs = levenshtein_suggest(e->name, vars); // no need to limit, there's not that many
75ead1f7 10196
f8a968bc
JS
10197 // We hope that this value ends up not being referenced after all, so it
10198 // can be optimized out quietly.
1d0499c2
JL
10199 throw SEMANTIC_ERROR(_F("unable to find tracepoint variable '%s'%s",
10200 e->name.c_str(), sugs.empty() ? "" :
10201 (_(" (alternatives: ") + sugs + ")").c_str()), e->tok);
10202 // NB: we use 'alternatives' because we list all
f8a968bc
JS
10203 // NB: we can have multiple errors, since a target variable
10204 // may be expanded in several different contexts:
10205 // trace ("*") { $foo->bar }
f8a968bc 10206 }
75ead1f7 10207
37efef73
JS
10208 // make sure we're not dereferencing base types or void
10209 bool deref_p = arg->isptr && !null_die(&arg->type_die);
10210 if (!deref_p)
d19a9a82 10211 e->assert_no_components("tracepoint", true);
75ead1f7 10212
f8a968bc
JS
10213 // we can only write to dereferenced fields, and only if guru mode is on
10214 bool lvalue = is_active_lvalue(e);
10215 if (lvalue && (!dw.sess.guru_mode || e->components.empty()))
dc09353a 10216 throw SEMANTIC_ERROR(_F("write to tracepoint variable '%s' not permitted; need stap -g", e->name.c_str()), e->tok);
c69a87e0 10217
ad370dcc
JS
10218 // XXX: if a struct/union arg is passed by value, then writing to its fields
10219 // is also meaningless until you dereference past a pointer member. It's
10220 // harder to detect and prevent that though...
75ead1f7 10221
f8a968bc
JS
10222 if (e->components.empty())
10223 {
03c75a4a 10224 if (e->addressof)
dc09353a 10225 throw SEMANTIC_ERROR(_("cannot take address of tracepoint variable"), e->tok);
a45664f4 10226
3e3bd7b6 10227 // Just grab the value from the probe locals
a45664f4
JS
10228 symbol* sym = new symbol;
10229 sym->tok = e->tok;
10230 sym->name = "__tracepoint_arg_" + arg->name;
6ee19a25 10231 sym->type_details.reset(new exp_type_dwarf(&dw, &arg->type_die, false, false));
a45664f4 10232 provide (sym);
f8a968bc
JS
10233 }
10234 else
10235 {
5f36109e
JS
10236 // make a copy of the original as a bare target symbol for the tracepoint
10237 // value, which will be passed into the dwarf dereferencing code
10238 target_symbol* e2 = deep_copy_visitor::deep_copy(e);
10239 e2->components.clear();
10240
c4965ad9 10241 if (e->check_pretty_print (lvalue))
5f36109e 10242 {
37efef73 10243 dwarf_pretty_print dpp(dw, &arg->type_die, e2, deref_p, false, *e);
5f36109e
JS
10244 dpp.expand()->visit (this);
10245 return;
10246 }
10247
1c0be8c7 10248 bool userspace_p = false;
f8a968bc 10249 string fname = (string(lvalue ? "_tracepoint_tvar_set" : "_tracepoint_tvar_get")
cc9001af 10250 + "_" + e->sym_name()
aca66a36 10251 + "_" + lex_cast(tick++));
75ead1f7 10252
40a393cd
JS
10253 Dwarf_Die endtype;
10254 string code = dw.literal_stmt_for_pointer (&arg->type_die, e, lvalue, &endtype);
6fda2dff 10255
40a393cd 10256 functioncall* n = synthetic_embedded_deref_call(dw, &endtype, fname, code,
1c0be8c7 10257 userspace_p, lvalue, e, e2);
75ead1f7 10258
f8a968bc 10259 if (lvalue)
f55efafe 10260 provide_lvalue_call (n);
75ead1f7 10261
1c0be8c7
JS
10262 // Revisit the functioncall so arguments can be expanded.
10263 n->visit (this);
f8a968bc 10264 }
75ead1f7
JS
10265}
10266
10267
f8a968bc
JS
10268void
10269tracepoint_var_expanding_visitor::visit_target_symbol_context (target_symbol* e)
0a6f5a3f 10270{
03c75a4a 10271 if (e->addressof)
dc09353a 10272 throw SEMANTIC_ERROR(_("cannot take address of context variable"), e->tok);
03c75a4a 10273
f8a968bc 10274 if (is_active_lvalue (e))
dc09353a 10275 throw SEMANTIC_ERROR(_F("write to tracepoint '%s' not permitted", e->name.c_str()), e->tok);
0a6f5a3f 10276
277c21bc 10277 if (e->name == "$$name")
f8a968bc 10278 {
5f36109e
JS
10279 e->assert_no_components("tracepoint");
10280
bfdaad1e
DS
10281 // Synthesize an embedded expression.
10282 embedded_expr *expr = new embedded_expr;
10283 expr->tok = e->tok;
10284 expr->code = string("/* string */ /* pure */ ")
6dceb5c9 10285 + string("c->ips.tracepoint_name ? c->ips.tracepoint_name : \"\"");
bfdaad1e 10286 provide (expr);
f8a968bc 10287 }
277c21bc 10288 else if (e->name == "$$vars" || e->name == "$$parms")
f8a968bc 10289 {
5f36109e
JS
10290 e->assert_no_components("tracepoint", true);
10291
1c922ad7 10292 print_format* pf = print_format::create(e->tok, "sprintf");
0a6f5a3f 10293
f8a968bc 10294 for (unsigned i = 0; i < args.size(); ++i)
b278033a 10295 {
dcaa1a65
JS
10296 if (!args[i].usable)
10297 continue;
f8a968bc
JS
10298 if (i > 0)
10299 pf->raw_components += " ";
10300 pf->raw_components += args[i].name;
3e3bd7b6 10301 target_symbol *tsym = new target_symbol;
f8a968bc 10302 tsym->tok = e->tok;
277c21bc 10303 tsym->name = "$" + args[i].name;
5f36109e 10304 tsym->components = e->components;
b278033a 10305
f8a968bc
JS
10306 // every variable should always be accessible!
10307 tsym->saved_conversion_error = 0;
8c2f50c0 10308 expression *texp = require<expression> (tsym); // NB: throws nothing ...
14a97852
JS
10309 if (tsym->saved_conversion_error) // ... but this is how we know it happened.
10310 {
10311 if (dw.sess.verbose>2)
e26c2f83 10312 for (const semantic_error *c = tsym->saved_conversion_error;
c081af73 10313 c != 0; c = c->get_chain())
4c5d9906 10314 clog << _("variable location problem [man error::dwarf]: ") << c->what() << endl;
14a97852
JS
10315 pf->raw_components += "=?";
10316 continue;
10317 }
b278033a 10318
c4965ad9 10319 if (e->check_pretty_print ())
5f36109e
JS
10320 pf->raw_components += "=%s";
10321 else
10322 pf->raw_components += args[i].isptr ? "=%p" : "=%#x";
f8a968bc
JS
10323 pf->args.push_back(texp);
10324 }
0a6f5a3f 10325
f8a968bc
JS
10326 pf->components = print_format::string_to_components(pf->raw_components);
10327 provide (pf);
b278033a 10328 }
f8a968bc
JS
10329 else
10330 assert(0); // shouldn't get here
0a6f5a3f
JS
10331}
10332
0a6f5a3f 10333void
f8a968bc 10334tracepoint_var_expanding_visitor::visit_target_symbol (target_symbol* e)
0a6f5a3f 10335{
aff5d390 10336 try
c69a87e0 10337 {
bd1fcbad 10338 assert(e->name.size() > 0 && e->name[0] == '$');
aff5d390 10339
277c21bc 10340 if (e->name == "$$name" || e->name == "$$parms" || e->name == "$$vars")
c69a87e0 10341 visit_target_symbol_context (e);
bd1fcbad 10342
c69a87e0
FCE
10343 else
10344 visit_target_symbol_arg (e);
10345 }
10346 catch (const semantic_error &er)
10347 {
1af1e62d 10348 e->chain (er);
c69a87e0
FCE
10349 provide (e);
10350 }
0a6f5a3f
JS
10351}
10352
10353
79189b84
JS
10354tracepoint_derived_probe::tracepoint_derived_probe (systemtap_session& s,
10355 dwflpp& dw, Dwarf_Die& func_die,
10356 const string& tracepoint_name,
10357 probe* base, probe_point* loc):
4c5d1300 10358 derived_probe (base, loc, true /* .components soon rewritten */),
79189b84 10359 sess (s), tracepoint_name (tracepoint_name)
56894e91 10360{
79189b84
JS
10361 // create synthetic probe point name; preserve condition
10362 vector<probe_point::component*> comps;
10363 comps.push_back (new probe_point::component (TOK_KERNEL));
10364 comps.push_back (new probe_point::component (TOK_TRACE, new literal_string (tracepoint_name)));
10365 this->sole_location()->components = comps;
10366
6fb70fb7
JS
10367 // fill out the available arguments in this tracepoint
10368 build_args(dw, func_die);
56894e91 10369
6fb70fb7
JS
10370 // determine which header defined this tracepoint
10371 string decl_file = dwarf_decl_file(&func_die);
b78a0fbb 10372 header = decl_file;
d4393459
FCE
10373
10374#if 0 /* This convention is not enforced. */
6fb70fb7
JS
10375 size_t header_pos = decl_file.rfind("trace/");
10376 if (header_pos == string::npos)
dc09353a 10377 throw SEMANTIC_ERROR ("cannot parse header location for tracepoint '"
6fb70fb7
JS
10378 + tracepoint_name + "' in '"
10379 + decl_file + "'");
10380 header = decl_file.substr(header_pos);
d4393459 10381#endif
56894e91 10382
6fb70fb7
JS
10383 // tracepoints from FOO_event_types.h should really be included from FOO.h
10384 // XXX can dwarf tell us the include hierarchy? it would be better to
10385 // ... walk up to see which one was directly included by tracequery.c
3c1b3d06 10386 // XXX: see also PR9993.
d4393459 10387 size_t header_pos = header.find("_event_types");
6fb70fb7
JS
10388 if (header_pos != string::npos)
10389 header.erase(header_pos, 12);
56894e91 10390
f8a968bc
JS
10391 // Now expand the local variables in the probe body
10392 tracepoint_var_expanding_visitor v (dw, name, args);
8b095b45 10393 v.replace (this->body);
a45664f4
JS
10394 for (unsigned i = 0; i < args.size(); i++)
10395 if (args[i].used)
10396 {
10397 vardecl* v = new vardecl;
10398 v->name = "__tracepoint_arg_" + args[i].name;
10399 v->tok = this->tok;
58701b78 10400 v->set_arity(0, this->tok);
a45664f4 10401 v->type = pe_long;
69aa668e 10402 v->synthetic = true;
a45664f4
JS
10403 this->locals.push_back (v);
10404 }
56894e91 10405
79189b84 10406 if (sess.verbose > 2)
ce0f6648 10407 clog << "tracepoint-based " << name << " tracepoint='" << tracepoint_name << "'" << endl;
79189b84 10408}
dc38c0ae 10409
56894e91 10410
f8a968bc 10411static bool
37efef73 10412resolve_pointer_type(Dwarf_Die& die, bool& isptr)
46b84a80 10413{
611a848e
JS
10414 if (null_die(&die))
10415 {
10416 isptr = true;
10417 return true;
10418 }
10419
d19a9a82 10420 Dwarf_Die type;
37efef73 10421 switch (dwarf_tag(&die))
b20febf3 10422 {
f8a968bc
JS
10423 case DW_TAG_typedef:
10424 case DW_TAG_const_type:
10425 case DW_TAG_volatile_type:
c69af88f 10426 case DW_TAG_restrict_type:
f8a968bc 10427 // iterate on the referent type
37efef73
JS
10428 return (dwarf_attr_die(&die, DW_AT_type, &die)
10429 && resolve_pointer_type(die, isptr));
10430
f8a968bc 10431 case DW_TAG_base_type:
a52d2ac0 10432 case DW_TAG_enumeration_type:
37efef73
JS
10433 case DW_TAG_structure_type:
10434 case DW_TAG_union_type:
f8a968bc 10435 // base types will simply be treated as script longs
37efef73
JS
10436 // structs/unions must be referenced by pointer elsewhere
10437 isptr = false;
f8a968bc 10438 return true;
37efef73
JS
10439
10440 case DW_TAG_array_type:
f8a968bc 10441 case DW_TAG_pointer_type:
37efef73
JS
10442 case DW_TAG_reference_type:
10443 case DW_TAG_rvalue_reference_type:
10444 // pointer-like types can be treated as script longs,
dcaa1a65 10445 // and if we know their type, they can also be dereferenced
37efef73
JS
10446 isptr = true;
10447 type = die;
10448 while (dwarf_attr_die(&type, DW_AT_type, &type))
d19a9a82
JS
10449 {
10450 // It still might be a non-type, e.g. const void,
10451 // so we need to strip away all qualifiers.
37efef73 10452 int tag = dwarf_tag(&type);
d19a9a82
JS
10453 if (tag != DW_TAG_typedef &&
10454 tag != DW_TAG_const_type &&
c69af88f
MW
10455 tag != DW_TAG_volatile_type &&
10456 tag != DW_TAG_restrict_type)
d19a9a82 10457 {
37efef73
JS
10458 die = type;
10459 return true;
d19a9a82
JS
10460 }
10461 }
37efef73
JS
10462 // otherwise use a null_die to indicate void
10463 std::memset(&die, 0, sizeof(die));
ad370dcc 10464 return true;
37efef73
JS
10465
10466 default:
10467 // should we consider other types too?
10468 return false;
10469 }
10470}
10471
10472
10473static bool
10474resolve_tracepoint_arg_type(tracepoint_arg& arg)
10475{
10476 if (!resolve_pointer_type(arg.type_die, arg.isptr))
10477 return false;
10478
10479 if (arg.isptr)
10480 arg.typecast = "(intptr_t)";
10481 else if (dwarf_tag(&arg.type_die) == DW_TAG_structure_type ||
10482 dwarf_tag(&arg.type_die) == DW_TAG_union_type)
10483 {
ad370dcc
JS
10484 // for structs/unions which are passed by value, we turn it into
10485 // a pointer that can be dereferenced.
10486 arg.isptr = true;
10487 arg.typecast = "(intptr_t)&";
b20febf3 10488 }
37efef73 10489 return true;
56894e91
JS
10490}
10491
10492
10493void
822a6a3d 10494tracepoint_derived_probe::build_args(dwflpp&, Dwarf_Die& func_die)
56894e91 10495{
6fb70fb7
JS
10496 Dwarf_Die arg;
10497 if (dwarf_child(&func_die, &arg) == 0)
10498 do
10499 if (dwarf_tag(&arg) == DW_TAG_formal_parameter)
10500 {
10501 // build a tracepoint_arg for this parameter
10502 tracepoint_arg tparg;
c60517ca 10503 tparg.name = dwarf_diename(&arg) ?: "";
56894e91 10504
6fb70fb7 10505 // read the type of this parameter
3d1ad340 10506 if (!dwarf_attr_die (&arg, DW_AT_type, &tparg.type_die)
f1c8f8a5 10507 || !dwarf_type_name(&tparg.type_die, tparg.c_type))
dc09353a 10508 throw SEMANTIC_ERROR (_F("cannot get type of parameter '%s' of tracepoint '%s'",
ce0f6648 10509 tparg.name.c_str(), tracepoint_name.c_str()));
a68f81a2 10510
dcaa1a65 10511 tparg.usable = resolve_tracepoint_arg_type(tparg);
6fb70fb7
JS
10512 args.push_back(tparg);
10513 if (sess.verbose > 4)
a52d2ac0
JS
10514 clog << _F("found parameter for tracepoint '%s': type:'%s' name:'%s' %s",
10515 tracepoint_name.c_str(), tparg.c_type.c_str(), tparg.name.c_str(),
10516 tparg.usable ? "ok" : "unavailable") << endl;
6fb70fb7
JS
10517 }
10518 while (dwarf_siblingof(&arg, &arg) == 0);
56894e91
JS
10519}
10520
dc38c0ae 10521void
d0bfd2ac 10522tracepoint_derived_probe::getargs(std::list<std::string> &arg_set) const
dc38c0ae 10523{
dcaa1a65
JS
10524 for (unsigned i = 0; i < args.size(); ++i)
10525 if (args[i].usable)
d0bfd2ac 10526 arg_set.push_back("$"+args[i].name+":"+args[i].c_type);
dc38c0ae
DS
10527}
10528
79189b84
JS
10529void
10530tracepoint_derived_probe::join_group (systemtap_session& s)
197a4d62 10531{
79189b84
JS
10532 if (! s.tracepoint_derived_probes)
10533 s.tracepoint_derived_probes = new tracepoint_derived_probe_group ();
10534 s.tracepoint_derived_probes->enroll (this);
ca6d3b0f 10535 this->group = s.tracepoint_derived_probes;
79189b84 10536}
e38d6504 10537
56894e91 10538
197a4d62 10539void
3e3bd7b6 10540tracepoint_derived_probe::print_dupe_stamp(ostream& o)
56894e91 10541{
3e3bd7b6
JS
10542 for (unsigned i = 0; i < args.size(); i++)
10543 if (args[i].used)
10544 o << "__tracepoint_arg_" << args[i].name << endl;
197a4d62 10545}
56894e91 10546
3e3bd7b6 10547
c9ccb642 10548static vector<string> tracepoint_extra_decls (systemtap_session& s, const string& header)
47dd066d 10549{
3c1b3d06
FCE
10550 vector<string> they_live;
10551 // PR 9993
10552 // XXX: may need this to be configurable
d4393459 10553 they_live.push_back ("#include <linux/skbuff.h>");
9e0cd21a
FCE
10554
10555 // PR11649: conditional extra header
10556 // for kvm tracepoints in 2.6.33ish
10557 if (s.kernel_config["CONFIG_KVM"] != string("")) {
d4393459
FCE
10558 they_live.push_back ("#include <linux/kvm_host.h>");
10559 }
10560
50b72692 10561 if (header.find("xfs") != string::npos && s.kernel_config["CONFIG_XFS_FS"] != string("")) {
d4393459 10562 they_live.push_back ("#define XFS_BIG_BLKNOS 1");
88637c31
FCE
10563 if (s.kernel_source_tree != "")
10564 they_live.push_back ("#include \"fs/xfs/xfs_types.h\""); // in kernel-source tree
d4393459
FCE
10565 they_live.push_back ("struct xfs_mount;");
10566 they_live.push_back ("struct xfs_inode;");
10567 they_live.push_back ("struct xfs_buf;");
10568 they_live.push_back ("struct xfs_bmbt_irec;");
c2cf1b87 10569 they_live.push_back ("struct xfs_trans;");
9e0cd21a 10570 }
d4393459 10571
50b72692 10572 if (header.find("nfs") != string::npos && s.kernel_config["CONFIG_NFSD"] != string("")) {
d4393459
FCE
10573 they_live.push_back ("struct rpc_task;");
10574 }
b64d65e2
FCE
10575 // RHEL6.3
10576 if (header.find("rpc") != string::npos && s.kernel_config["CONFIG_NFSD"] != string("")) {
10577 they_live.push_back ("struct rpc_clnt;");
10578 they_live.push_back ("struct rpc_wait_queue;");
10579 }
d4393459
FCE
10580
10581 they_live.push_back ("#include <asm/cputime.h>");
10582
c2cf1b87
FCE
10583 // linux 3.0
10584 they_live.push_back ("struct cpu_workqueue_struct;");
10585
50b72692 10586 if (header.find("ext4") != string::npos && s.kernel_config["CONFIG_EXT4_FS"] != string(""))
c2cf1b87
FCE
10587 if (s.kernel_source_tree != "")
10588 they_live.push_back ("#include \"fs/ext4/ext4.h\""); // in kernel-source tree
10589
d97d428e 10590 if (header.find("ext3") != string::npos)
50b72692
HP
10591 they_live.push_back ("struct ext3_reserve_window_node;");
10592
d97d428e
FCE
10593 if (header.find("workqueue") != string::npos)
10594 {
10595 they_live.push_back ("struct pool_workqueue;");
10596 they_live.push_back ("struct work_struct;");
10597 }
10598
10599 if (header.find("asoc") != string::npos)
10600 they_live.push_back ("struct snd_soc_dapm_path;");
10601
10602 if (header.find("9p") != string::npos)
10603 {
10604 they_live.push_back ("struct p9_client;");
10605 they_live.push_back ("struct p9_fcall;");
10606 }
10607
10608 if (header.find("bcache") != string::npos)
10609 {
10610 they_live.push_back ("struct bkey;");
10611 they_live.push_back ("struct btree;");
10612 they_live.push_back ("struct cache_set;");
10613 they_live.push_back ("struct cache;");
10614 }
10615
10616 if (header.find("f2fs") != string::npos)
10617 {
10618 // cannot get fs/f2fs/f2fs.h #included
10619 they_live.push_back ("typedef u32 block_t;");
10620 they_live.push_back ("typedef u32 nid_t;");
10621 }
10622
10623 if (header.find("radeon") != string::npos)
10624 they_live.push_back ("struct radeon_bo;");
10625
10626 // argh, 3.11, i915_trace.h -> i915_drv.h -> i915_reg.h without -I
10627 // also brcms_trace_events.h -> ... -> "types.h"
10628 // XXX: need a way to add a temporary -I flag
10629
10630 if (header.find("/ath/") != string::npos)
10631 they_live.push_back ("struct ath5k_hw;");
10632
10633
3c1b3d06
FCE
10634 return they_live;
10635}
47dd066d
WC
10636
10637
10638void
79189b84 10639tracepoint_derived_probe_group::emit_module_decls (systemtap_session& s)
47dd066d 10640{
79189b84
JS
10641 if (probes.empty())
10642 return;
47dd066d 10643
96b030fe 10644 s.op->newline() << "/* ---- tracepoint probes ---- */";
3ef9830a 10645 s.op->newline() << "#include <linux/stp_tracepoint.h>" << endl;
96b030fe 10646 s.op->newline();
79189b84 10647
47dd066d 10648
a4b9c3b3
FCE
10649 // We create a MODULE_aux_N.c file for each tracepoint header, to allow them
10650 // to be separately compiled. That's because kernel tracepoint headers sometimes
10651 // conflict. PR13155.
10652
10653 map<string,translator_output*> per_header_aux;
10654 // GC NB: the translator_output* structs are owned/retained by the systemtap_session.
47dd066d 10655
6fb70fb7
JS
10656 for (unsigned i = 0; i < probes.size(); ++i)
10657 {
10658 tracepoint_derived_probe *p = probes[i];
75ae2ec9 10659 string header = p->header;
5f73a260 10660
a4b9c3b3
FCE
10661 // We cache the auxiliary output files on a per-header basis. We don't
10662 // need one aux file per tracepoint, only one per tracepoint-header.
10663 translator_output *tpop = per_header_aux[header];
10664 if (tpop == 0)
10665 {
10666 tpop = s.op_create_auxiliary();
10667 per_header_aux[header] = tpop;
10668
10669 // PR9993: Add extra headers to work around undeclared types in individual
10670 // include/trace/foo.h files
10671 const vector<string>& extra_decls = tracepoint_extra_decls (s, header);
10672 for (unsigned z=0; z<extra_decls.size(); z++)
10673 tpop->newline() << extra_decls[z] << "\n";
720c435f 10674
a4b9c3b3
FCE
10675 // strip include/ substring, the same way as done in get_tracequery_module()
10676 size_t root_pos = header.rfind("include/");
10677 header = ((root_pos != string::npos) ? header.substr(root_pos + 8) : header);
bfffa443 10678
3ef9830a 10679 tpop->newline() << "#include <linux/stp_tracepoint.h>" << endl;
a4b9c3b3 10680 tpop->newline() << "#include <" << header << ">";
a4b9c3b3
FCE
10681 }
10682
720c435f
JS
10683 // collect the args that are actually in use
10684 vector<const tracepoint_arg*> used_args;
6fb70fb7 10685 for (unsigned j = 0; j < p->args.size(); ++j)
720c435f
JS
10686 if (p->args[j].used)
10687 used_args.push_back(&p->args[j]);
10688
3ef9830a
JS
10689 // forward-declare the generated-side tracepoint callback, and define the
10690 // generated-side tracepoint callback in the main translator-output
10691 string enter_real_fn = "enter_real_tracepoint_probe_" + lex_cast(i);
720c435f 10692 if (used_args.empty())
6fb70fb7 10693 {
3ef9830a
JS
10694 tpop->newline() << "STP_TRACE_ENTER_REAL_NOARGS(" << enter_real_fn << ");";
10695 s.op->newline() << "STP_TRACE_ENTER_REAL_NOARGS(" << enter_real_fn << ")";
6fb70fb7 10696 }
3ef9830a 10697 else
a4b9c3b3 10698 {
3ef9830a
JS
10699 tpop->newline() << "STP_TRACE_ENTER_REAL(" << enter_real_fn;
10700 s.op->newline() << "STP_TRACE_ENTER_REAL(" << enter_real_fn;
10701 s.op->indent(2);
10702 for (unsigned j = 0; j < used_args.size(); ++j)
10703 {
10704 tpop->line() << ", int64_t";
10705 s.op->newline() << ", int64_t __tracepoint_arg_" << used_args[j]->name;
10706 }
10707 tpop->line() << ");";
10708 s.op->newline() << ")";
10709 s.op->indent(-2);
6fb70fb7 10710 }
3ef9830a 10711 s.op->newline() << "{";
7c3e97f4 10712 s.op->newline(1) << "const struct stap_probe * const probe = "
faea5e16 10713 << common_probe_init (p) << ";";
71db462b 10714 common_probe_entryfn_prologue (s, "STAP_SESSION_RUNNING", "probe",
cda141c2 10715 "stp_probe_type_tracepoint");
6dceb5c9 10716 s.op->newline() << "c->ips.tracepoint_name = "
c12d974f
FCE
10717 << lex_cast_qstring (p->tracepoint_name)
10718 << ";";
720c435f
JS
10719 for (unsigned j = 0; j < used_args.size(); ++j)
10720 {
10721 s.op->newline() << "c->probe_locals." << p->name
0386bbcf 10722 << "." + s.up->c_localname("__tracepoint_arg_" + used_args[j]->name)
720c435f
JS
10723 << " = __tracepoint_arg_" << used_args[j]->name << ";";
10724 }
26e63673 10725 s.op->newline() << "(*probe->ph) (c);";
ef1337ee 10726 common_probe_entryfn_epilogue (s, true, otf_safe_context(s));
6fb70fb7 10727 s.op->newline(-1) << "}";
47dd066d 10728
a4b9c3b3 10729 // define the real tracepoint callback function
3ef9830a
JS
10730 string enter_fn = "enter_tracepoint_probe_" + lex_cast(i);
10731 if (p->args.empty())
10732 tpop->newline() << "static STP_TRACE_ENTER_NOARGS(" << enter_fn << ")";
10733 else
a4b9c3b3 10734 {
3ef9830a
JS
10735 tpop->newline() << "static STP_TRACE_ENTER(" << enter_fn;
10736 s.op->indent(2);
10737 for (unsigned j = 0; j < p->args.size(); ++j)
10738 {
10739 tpop->newline() << ", " << p->args[j].c_type
10740 << " __tracepoint_arg_" << p->args[j].name;
10741 }
10742 tpop->newline() << ")";
10743 s.op->indent(-2);
a4b9c3b3 10744 }
3ef9830a
JS
10745 tpop->newline() << "{";
10746 tpop->newline(1) << enter_real_fn << "(";
720c435f
JS
10747 tpop->indent(2);
10748 for (unsigned j = 0; j < used_args.size(); ++j)
10749 {
10750 if (j > 0)
10751 tpop->line() << ", ";
10752 tpop->newline() << "(int64_t)" << used_args[j]->typecast
10753 << "__tracepoint_arg_" << used_args[j]->name;
10754 }
10755 tpop->newline() << ");";
10756 tpop->newline(-3) << "}";
a4b9c3b3
FCE
10757
10758
96b030fe 10759 // emit normalized registration functions
720c435f 10760 tpop->newline() << "int register_tracepoint_probe_" << i << "(void) {";
3ef9830a
JS
10761 tpop->newline(1) << "return STP_TRACE_REGISTER(" << p->tracepoint_name
10762 << ", " << enter_fn << ");";
a4b9c3b3 10763 tpop->newline(-1) << "}";
47dd066d 10764
86758d5f
JS
10765 // NB: we're not prepared to deal with unreg failures. However, failures
10766 // can only occur if the tracepoint doesn't exist (yet?), or if we
10767 // weren't even registered. The former should be OKed by the initial
10768 // registration call, and the latter is safe to ignore.
720c435f 10769 tpop->newline() << "void unregister_tracepoint_probe_" << i << "(void) {";
3ef9830a
JS
10770 tpop->newline(1) << "(void) STP_TRACE_UNREGISTER(" << p->tracepoint_name
10771 << ", " << enter_fn << ");";
a4b9c3b3
FCE
10772 tpop->newline(-1) << "}";
10773 tpop->newline();
5f73a260 10774
720c435f
JS
10775 // declare normalized registration functions
10776 s.op->newline() << "int register_tracepoint_probe_" << i << "(void);";
10777 s.op->newline() << "void unregister_tracepoint_probe_" << i << "(void);";
5f73a260 10778
a4b9c3b3 10779 tpop->assert_0_indent();
af304783
DS
10780 }
10781
96b030fe
JS
10782 // emit an array of registration functions for easy init/shutdown
10783 s.op->newline() << "static struct stap_tracepoint_probe {";
10784 s.op->newline(1) << "int (*reg)(void);";
86758d5f 10785 s.op->newline(0) << "void (*unreg)(void);";
96b030fe
JS
10786 s.op->newline(-1) << "} stap_tracepoint_probes[] = {";
10787 s.op->indent(1);
10788 for (unsigned i = 0; i < probes.size(); ++i)
10789 {
10790 s.op->newline () << "{";
10791 s.op->line() << " .reg=&register_tracepoint_probe_" << i << ",";
10792 s.op->line() << " .unreg=&unregister_tracepoint_probe_" << i;
10793 s.op->line() << " },";
10794 }
10795 s.op->newline(-1) << "};";
10796 s.op->newline();
47dd066d
WC
10797}
10798
10799
79189b84
JS
10800void
10801tracepoint_derived_probe_group::emit_module_init (systemtap_session &s)
47dd066d 10802{
79189b84
JS
10803 if (probes.size () == 0)
10804 return;
47dd066d 10805
79189b84 10806 s.op->newline() << "/* init tracepoint probes */";
96b030fe
JS
10807 s.op->newline() << "for (i=0; i<" << probes.size() << "; i++) {";
10808 s.op->newline(1) << "rc = stap_tracepoint_probes[i].reg();";
10809 s.op->newline() << "if (rc) {";
10810 s.op->newline(1) << "for (j=i-1; j>=0; j--)"; // partial rollback
10811 s.op->newline(1) << "stap_tracepoint_probes[j].unreg();";
10812 s.op->newline(-1) << "break;"; // don't attempt to register any more probes
10813 s.op->newline(-1) << "}";
10814 s.op->newline(-1) << "}";
47dd066d 10815
bc9a523d
FCE
10816 // This would be technically proper (on those autoconf-detectable
10817 // kernels that include this function in tracepoint.h), however we
10818 // already make several calls to synchronze_sched() during our
10819 // shutdown processes.
47dd066d 10820
bc9a523d
FCE
10821 // s.op->newline() << "if (rc)";
10822 // s.op->newline(1) << "tracepoint_synchronize_unregister();";
10823 // s.op->indent(-1);
79189b84 10824}
47dd066d
WC
10825
10826
79189b84
JS
10827void
10828tracepoint_derived_probe_group::emit_module_exit (systemtap_session& s)
47dd066d 10829{
79189b84
JS
10830 if (probes.empty())
10831 return;
47dd066d 10832
96b030fe
JS
10833 s.op->newline() << "/* deregister tracepoint probes */";
10834 s.op->newline() << "for (i=0; i<" << probes.size() << "; i++)";
10835 s.op->newline(1) << "stap_tracepoint_probes[i].unreg();";
10836 s.op->indent(-1);
47dd066d 10837
bc9a523d 10838 // Not necessary: see above.
47dd066d 10839
bc9a523d 10840 // s.op->newline() << "tracepoint_synchronize_unregister();";
79189b84 10841}
b20febf3 10842
47dd066d 10843
75ead1f7 10844struct tracepoint_query : public base_query
47dd066d 10845{
75ead1f7
JS
10846 tracepoint_query(dwflpp & dw, const string & tracepoint,
10847 probe * base_probe, probe_point * base_loc,
10848 vector<derived_probe *> & results):
10849 base_query(dw, "*"), tracepoint(tracepoint),
10850 base_probe(base_probe), base_loc(base_loc),
10851 results(results) {}
47dd066d 10852
75ead1f7 10853 const string& tracepoint;
47dd066d 10854
75ead1f7
JS
10855 probe * base_probe;
10856 probe_point * base_loc;
10857 vector<derived_probe *> & results;
f982c59b 10858 set<string> probed_names;
47dd066d 10859
75ead1f7
JS
10860 void handle_query_module();
10861 int handle_query_cu(Dwarf_Die * cudie);
10862 int handle_query_func(Dwarf_Die * func);
822a6a3d 10863 void query_library (const char *) {}
576eaefe 10864 void query_plt (const char *entry, size_t addr) {}
b20febf3 10865
5c378838 10866 static int tracepoint_query_cu (Dwarf_Die * cudie, tracepoint_query * q);
7d007451 10867 static int tracepoint_query_func (Dwarf_Die * func, tracepoint_query * q);
75ead1f7 10868};
47dd066d
WC
10869
10870
10871void
75ead1f7 10872tracepoint_query::handle_query_module()
47dd066d 10873{
75ead1f7 10874 // look for the tracepoints in each CU
337b7c44 10875 dw.iterate_over_cus(tracepoint_query_cu, this, false);
47dd066d
WC
10876}
10877
10878
75ead1f7
JS
10879int
10880tracepoint_query::handle_query_cu(Dwarf_Die * cudie)
47dd066d 10881{
75ead1f7 10882 dw.focus_on_cu (cudie);
5f52fafe 10883 dw.mod_info->get_symtab();
47dd066d 10884
75ead1f7
JS
10885 // look at each function to see if it's a tracepoint
10886 string function = "stapprobe_" + tracepoint;
10887 return dw.iterate_over_functions (tracepoint_query_func, this, function);
47dd066d
WC
10888}
10889
10890
75ead1f7
JS
10891int
10892tracepoint_query::handle_query_func(Dwarf_Die * func)
47dd066d 10893{
75ead1f7 10894 dw.focus_on_function (func);
47dd066d 10895
60d98537 10896 assert(startswith(dw.function_name, "stapprobe_"));
75ead1f7 10897 string tracepoint_instance = dw.function_name.substr(10);
f982c59b
JS
10898
10899 // check for duplicates -- sometimes tracepoint headers may be indirectly
10900 // included in more than one of our tracequery modules.
10901 if (!probed_names.insert(tracepoint_instance).second)
10902 return DWARF_CB_OK;
10903
c4a04833
FCE
10904 // PR17126: blacklist
10905 if (!sess.guru_mode)
10906 {
10907 if ((sess.architecture.substr(0,3) == "ppc" ||
10908 sess.architecture.substr(0,7) == "powerpc") &&
10909 (tracepoint_instance == "hcall_entry" ||
10910 tracepoint_instance == "hcall_exit"))
10911 {
10912 sess.print_warning(_F("tracepoint %s is blacklisted on architecture %s",
10913 tracepoint_instance.c_str(), sess.architecture.c_str()));
10914 return DWARF_CB_OK;
10915 }
10916 }
10917
79189b84
JS
10918 derived_probe *dp = new tracepoint_derived_probe (dw.sess, dw, *func,
10919 tracepoint_instance,
10920 base_probe, base_loc);
10921 results.push_back (dp);
75ead1f7 10922 return DWARF_CB_OK;
47dd066d
WC
10923}
10924
10925
75ead1f7 10926int
5c378838 10927tracepoint_query::tracepoint_query_cu (Dwarf_Die * cudie, tracepoint_query * q)
47dd066d 10928{
85007c04 10929 if (pending_interrupts) return DWARF_CB_ABORT;
75ead1f7 10930 return q->handle_query_cu(cudie);
47dd066d
WC
10931}
10932
10933
75ead1f7 10934int
7d007451 10935tracepoint_query::tracepoint_query_func (Dwarf_Die * func, tracepoint_query * q)
47dd066d 10936{
85007c04 10937 if (pending_interrupts) return DWARF_CB_ABORT;
75ead1f7 10938 return q->handle_query_func(func);
47dd066d
WC
10939}
10940
10941
0a6f5a3f 10942struct tracepoint_builder: public derived_probe_builder
47dd066d 10943{
0a6f5a3f
JS
10944private:
10945 dwflpp *dw;
10946 bool init_dw(systemtap_session& s);
c9ccb642
FCE
10947 void get_tracequery_modules(systemtap_session& s,
10948 const vector<string>& headers,
10949 vector<string>& modules);
47dd066d 10950
0a6f5a3f 10951public:
47dd066d 10952
0a6f5a3f
JS
10953 tracepoint_builder(): dw(0) {}
10954 ~tracepoint_builder() { delete dw; }
47dd066d 10955
0a6f5a3f
JS
10956 void build_no_more (systemtap_session& s)
10957 {
10958 if (dw && s.verbose > 3)
b530b5b3 10959 clog << _("tracepoint_builder releasing dwflpp") << endl;
0a6f5a3f
JS
10960 delete dw;
10961 dw = NULL;
435f53a7
FCE
10962
10963 delete_session_module_cache (s);
0a6f5a3f 10964 }
47dd066d 10965
0a6f5a3f
JS
10966 void build(systemtap_session& s,
10967 probe *base, probe_point *location,
10968 literal_map_t const& parameters,
10969 vector<derived_probe*>& finished_results);
10970};
47dd066d 10971
47dd066d 10972
c9ccb642 10973
2a0e62a8 10974// Create (or cache) one or more tracequery .o modules, based upon the
c9ccb642
FCE
10975// tracepoint-related header files given. Return the generated or cached
10976// modules[].
10977
10978void
10979tracepoint_builder::get_tracequery_modules(systemtap_session& s,
10980 const vector<string>& headers,
10981 vector<string>& modules)
0a6f5a3f 10982{
c95eddf7 10983 if (s.verbose > 2)
55e50c24 10984 {
ce0f6648 10985 clog << _F("Pass 2: getting a tracepoint query for %zu headers: ", headers.size()) << endl;
55e50c24
JS
10986 for (size_t i = 0; i < headers.size(); ++i)
10987 clog << " " << headers[i] << endl;
10988 }
c95eddf7 10989
2a0e62a8
JS
10990 map<string,string> headers_cache_obj; // header name -> cache/.../tracequery_hash.o file name
10991 // Map the headers to cache .o names. Note that this has side-effects of
c9ccb642
FCE
10992 // creating the $SYSTEMTAP_DIR/.cache/XX/... directory and the hash-log file,
10993 // so we prefer not to repeat this.
10994 vector<string> uncached_headers;
10995 for (size_t i=0; i<headers.size(); i++)
2a0e62a8 10996 headers_cache_obj[headers[i]] = find_tracequery_hash(s, headers[i]);
c9ccb642
FCE
10997
10998 // They may be in the cache already.
10999 if (s.use_cache && !s.poison_cache)
11000 for (size_t i=0; i<headers.size(); i++)
11001 {
11002 // see if the cached module exists
2a0e62a8 11003 const string& tracequery_path = headers_cache_obj[headers[i]];
c9ccb642
FCE
11004 if (!tracequery_path.empty() && file_exists(tracequery_path))
11005 {
11006 if (s.verbose > 2)
11007 clog << _F("Pass 2: using cached %s", tracequery_path.c_str()) << endl;
47dd066d 11008
c252fca2
JS
11009 // an empty file is a cached failure
11010 if (get_file_size(tracequery_path) > 0)
11011 modules.push_back (tracequery_path);
c9ccb642
FCE
11012 }
11013 else
11014 uncached_headers.push_back(headers[i]);
11015 }
11016 else
11017 uncached_headers = headers;
f982c59b 11018
c9ccb642
FCE
11019 // If we have nothing left to search for, quit
11020 if (uncached_headers.empty()) return;
55e50c24 11021
c9ccb642 11022 map<string,string> headers_tracequery_src; // header -> C-source code mapping
55e50c24 11023
c9ccb642
FCE
11024 // We could query several subsets of headers[] to make this go
11025 // faster, but let's KISS and do one at a time.
11026 for (size_t i=0; i<uncached_headers.size(); i++)
55e50c24 11027 {
c9ccb642
FCE
11028 const string& header = uncached_headers[i];
11029
11030 // create a tracequery source file
11031 ostringstream osrc;
11032
11033 // PR9993: Add extra headers to work around undeclared types in individual
11034 // include/trace/foo.h files
11035 vector<string> short_decls = tracepoint_extra_decls(s, header);
11036
11037 // add each requested tracepoint header
75ae2ec9 11038 size_t root_pos = header.rfind("include/");
832f100d 11039 short_decls.push_back(string("#include <") +
75ae2ec9 11040 ((root_pos != string::npos) ? header.substr(root_pos + 8) : header) +
d4393459 11041 string(">"));
f982c59b 11042
c9ccb642
FCE
11043 osrc << "#ifdef CONFIG_TRACEPOINTS" << endl;
11044 osrc << "#include <linux/tracepoint.h>" << endl;
832f100d
JS
11045
11046 // the kernel has changed this naming a few times, previously TPPROTO,
11047 // TP_PROTO, TPARGS, TP_ARGS, etc. so let's just dupe the latest.
11048 osrc << "#ifndef PARAMS" << endl;
11049 osrc << "#define PARAMS(args...) args" << endl;
11050 osrc << "#endif" << endl;
11051
c9ccb642
FCE
11052 // override DECLARE_TRACE to synthesize probe functions for us
11053 osrc << "#undef DECLARE_TRACE" << endl;
11054 osrc << "#define DECLARE_TRACE(name, proto, args) \\" << endl;
11055 osrc << " void stapprobe_##name(proto) {}" << endl;
832f100d 11056
c9ccb642
FCE
11057 // 2.6.35 added the NOARGS variant, but it's the same for us
11058 osrc << "#undef DECLARE_TRACE_NOARGS" << endl;
11059 osrc << "#define DECLARE_TRACE_NOARGS(name) \\" << endl;
11060 osrc << " DECLARE_TRACE(name, void, )" << endl;
832f100d
JS
11061
11062 // 2.6.38 added the CONDITION variant, which can also just redirect
11063 osrc << "#undef DECLARE_TRACE_CONDITION" << endl;
11064 osrc << "#define DECLARE_TRACE_CONDITION(name, proto, args, cond) \\" << endl;
11065 osrc << " DECLARE_TRACE(name, PARAMS(proto), PARAMS(args))" << endl;
11066
c9ccb642
FCE
11067 // older tracepoints used DEFINE_TRACE, so redirect that too
11068 osrc << "#undef DEFINE_TRACE" << endl;
11069 osrc << "#define DEFINE_TRACE(name, proto, args) \\" << endl;
832f100d
JS
11070 osrc << " DECLARE_TRACE(name, PARAMS(proto), PARAMS(args))" << endl;
11071
c9ccb642
FCE
11072 // add the specified decls/#includes
11073 for (unsigned z=0; z<short_decls.size(); z++)
11074 osrc << "#undef TRACE_INCLUDE_FILE\n"
11075 << "#undef TRACE_INCLUDE_PATH\n"
11076 << short_decls[z] << "\n";
11077
11078 // finish up the module source
11079 osrc << "#endif /* CONFIG_TRACEPOINTS */" << endl;
47dd066d 11080
c9ccb642
FCE
11081 // save the source file away
11082 headers_tracequery_src[header] = osrc.str();
55e50c24 11083 }
f982c59b 11084
c9ccb642 11085 // now build them all together
2a0e62a8 11086 map<string,string> tracequery_objs = make_tracequeries(s, headers_tracequery_src);
47dd066d 11087
c9ccb642 11088 // now plop them into the cache
b278033a 11089 if (s.use_cache)
c9ccb642
FCE
11090 for (size_t i=0; i<uncached_headers.size(); i++)
11091 {
11092 const string& header = uncached_headers[i];
2a0e62a8
JS
11093 const string& tracequery_obj = tracequery_objs[header];
11094 const string& tracequery_path = headers_cache_obj[header];
11095 if (tracequery_obj !="" && file_exists(tracequery_obj))
c9ccb642 11096 {
2a0e62a8 11097 copy_file(tracequery_obj, tracequery_path, s.verbose > 2);
c9ccb642
FCE
11098 modules.push_back (tracequery_path);
11099 }
c252fca2
JS
11100 else
11101 // cache an empty file for failures
11102 copy_file("/dev/null", tracequery_path, s.verbose > 2);
c9ccb642 11103 }
f982c59b
JS
11104}
11105
11106
d4393459 11107
f982c59b
JS
11108bool
11109tracepoint_builder::init_dw(systemtap_session& s)
11110{
11111 if (dw != NULL)
11112 return true;
11113
11114 vector<string> tracequery_modules;
55e50c24 11115 vector<string> system_headers;
f982c59b
JS
11116
11117 glob_t trace_glob;
d4393459
FCE
11118
11119 // find kernel_source_tree
11120 if (s.kernel_source_tree == "")
f982c59b 11121 {
d4393459 11122 unsigned found;
ccf2c922 11123 Dwfl *dwfl = setup_dwfl_kernel ("kernel", &found, s);
d4393459
FCE
11124 if (found)
11125 {
11126 Dwarf_Die *cudie = 0;
11127 Dwarf_Addr bias;
11128 while ((cudie = dwfl_nextcu (dwfl, cudie, &bias)) != NULL)
11129 {
e19ebcf7 11130 assert_no_interrupts();
d4393459
FCE
11131 Dwarf_Attribute attr;
11132 const char* name = dwarf_formstring (dwarf_attr (cudie, DW_AT_comp_dir, &attr));
36d65b45 11133 if (name)
d4393459 11134 {
36d65b45
JS
11135 // check that the path actually exists locally before we try to use it
11136 if (file_exists(name))
11137 {
11138 if (s.verbose > 2)
11139 clog << _F("Located kernel source tree (DW_AT_comp_dir) at '%s'", name) << endl;
11140 s.kernel_source_tree = name;
11141 }
11142 else
11143 {
11144 if (s.verbose > 2)
11145 clog << _F("Ignoring inaccessible kernel source tree (DW_AT_comp_dir) at '%s'", name) << endl;
11146 }
61f1a63b 11147
d4393459
FCE
11148 break; // skip others; modern Kbuild uses same comp_dir for them all
11149 }
11150 }
11151 }
44392d73 11152 dwfl_end (dwfl);
d4393459
FCE
11153 }
11154
11155 // prefixes
11156 vector<string> glob_prefixes;
11157 glob_prefixes.push_back (s.kernel_build_tree);
11158 if (s.kernel_source_tree != "")
11159 glob_prefixes.push_back (s.kernel_source_tree);
11160
11161 // suffixes
11162 vector<string> glob_suffixes;
11163 glob_suffixes.push_back("include/trace/events/*.h");
11164 glob_suffixes.push_back("include/trace/*.h");
d97d428e 11165 glob_suffixes.push_back("include/ras/*_event.h");
d4393459 11166 glob_suffixes.push_back("arch/x86/kvm/*trace.h");
d97d428e
FCE
11167 glob_suffixes.push_back("arch/x86/kernel/*trace.h");
11168 glob_suffixes.push_back("arch/*/include/asm/trace*.h");
11169 glob_suffixes.push_back("arch/*/include/asm/trace/*.h");
f89ff3e2 11170 glob_suffixes.push_back("fs/xfs/linux-*/xfs_tr*.h");
d97d428e
FCE
11171 glob_suffixes.push_back("fs/*/*trace*.h");
11172 glob_suffixes.push_back("net/*/*trace*.h");
11173 glob_suffixes.push_back("sound/pci/hda/*_trace.h");
11174 glob_suffixes.push_back("drivers/gpu/drm/*_trace.h");
11175 glob_suffixes.push_back("drivers/gpu/drm/*/*_trace.h");
11176 glob_suffixes.push_back("drivers/net/wireless/*/*/*trace*.h");
11177 glob_suffixes.push_back("drivers/usb/host/*trace*.h");
11178
11179 // see also tracepoint_extra_decls above
d4393459
FCE
11180
11181 // compute cartesian product
11182 vector<string> globs;
11183 for (unsigned i=0; i<glob_prefixes.size(); i++)
11184 for (unsigned j=0; j<glob_suffixes.size(); j++)
11185 globs.push_back (glob_prefixes[i]+string("/")+glob_suffixes[j]);
11186
8aa43b8d 11187 set<string> duped_headers;
d4393459
FCE
11188 for (unsigned z = 0; z < globs.size(); z++)
11189 {
11190 string glob_str = globs[z];
11191 if (s.verbose > 3)
b530b5b3 11192 clog << _("Checking tracepoint glob ") << glob_str << endl;
d4393459 11193
067cc66f
CM
11194 int r = glob(glob_str.c_str(), 0, NULL, &trace_glob);
11195 if (r == GLOB_NOSPACE || r == GLOB_ABORTED)
11196 throw runtime_error("Error globbing tracepoint");
11197
f982c59b
JS
11198 for (unsigned i = 0; i < trace_glob.gl_pathc; ++i)
11199 {
11200 string header(trace_glob.gl_pathv[i]);
11201
11202 // filter out a few known "internal-only" headers
60d98537
JS
11203 if (endswith(header, "/define_trace.h") ||
11204 endswith(header, "/ftrace.h") ||
11205 endswith(header, "/trace_events.h") ||
11206 endswith(header, "_event_types.h"))
f982c59b
JS
11207 continue;
11208
b1966849
FCE
11209 // With headers now plopped under arch/FOO/include/asm/*,
11210 // the following logic miss some tracepoints.
11211#if 0
8aa43b8d
JS
11212 // skip identical headers from the build and source trees.
11213 size_t root_pos = header.rfind("include/");
11214 if (root_pos != string::npos &&
11215 !duped_headers.insert(header.substr(root_pos + 8)).second)
11216 continue;
b1966849 11217#endif
8aa43b8d 11218
55e50c24 11219 system_headers.push_back(header);
f982c59b
JS
11220 }
11221 globfree(&trace_glob);
11222 }
11223
c9ccb642
FCE
11224 // Build tracequery modules
11225 get_tracequery_modules(s, system_headers, tracequery_modules);
55e50c24 11226
f982c59b
JS
11227 // TODO: consider other sources of tracepoint headers too, like from
11228 // a command-line parameter or some environment or .systemtaprc
47dd066d 11229
59c11f91 11230 dw = new dwflpp(s, tracequery_modules, true);
0a6f5a3f
JS
11231 return true;
11232}
47dd066d 11233
0a6f5a3f
JS
11234void
11235tracepoint_builder::build(systemtap_session& s,
11236 probe *base, probe_point *location,
11237 literal_map_t const& parameters,
11238 vector<derived_probe*>& finished_results)
11239{
11240 if (!init_dw(s))
11241 return;
47dd066d 11242
75ead1f7
JS
11243 string tracepoint;
11244 assert(get_param (parameters, TOK_TRACE, tracepoint));
47dd066d 11245
75ead1f7 11246 tracepoint_query q(*dw, tracepoint, base, location, finished_results);
d906ab9d 11247 unsigned results_pre = finished_results.size();
06de3a04 11248 dw->iterate_over_modules<base_query>(&query_module, &q);
d906ab9d
JL
11249 unsigned results_post = finished_results.size();
11250
11251 // Did we fail to find a match? Let's suggest something!
11252 if (results_pre == results_post)
11253 {
11254 size_t pos;
11255 string sugs = suggest_dwarf_functions(s, q.visited_modules, tracepoint);
11256 while ((pos = sugs.find("stapprobe_")) != string::npos)
11257 sugs.erase(pos, string("stapprobe_").size());
11258 if (!sugs.empty())
ece93f53
JL
11259 throw SEMANTIC_ERROR (_NF("no match (similar tracepoint: %s)",
11260 "no match (similar tracepoints: %s)",
d906ab9d
JL
11261 sugs.find(',') == string::npos,
11262 sugs.c_str()));
11263 }
47dd066d 11264}
47dd066d 11265
e6fe60e7 11266
b55bc428 11267// ------------------------------------------------------------------------
bd2b1e68 11268// Standard tapset registry.
b55bc428
FCE
11269// ------------------------------------------------------------------------
11270
7a053d3b 11271void
f8220a7b 11272register_standard_tapsets(systemtap_session & s)
b55bc428 11273{
47e0478e 11274 register_tapset_been(s);
93646f4d 11275 register_tapset_itrace(s);
dd0e4fa7 11276 register_tapset_mark(s);
7a212aa8 11277 register_tapset_procfs(s);
912e8c59 11278 register_tapset_timers(s);
8d9609f5 11279 register_tapset_netfilter(s);
b84779a5 11280 register_tapset_utrace(s);
b98a8d73 11281
7a24d422 11282 // dwarf-based kprobe/uprobe parts
c4ce66a1 11283 dwarf_derived_probe::register_patterns(s);
30a279be 11284
888af770
FCE
11285 // XXX: user-space starter set
11286 s.pattern_root->bind_num(TOK_PROCESS)
11287 ->bind_num(TOK_STATEMENT)->bind(TOK_ABSOLUTE)
f66bb29a 11288 ->bind_privilege(pr_all)
888af770
FCE
11289 ->bind(new uprobe_builder ());
11290 s.pattern_root->bind_num(TOK_PROCESS)
11291 ->bind_num(TOK_STATEMENT)->bind(TOK_ABSOLUTE)->bind(TOK_RETURN)
f66bb29a 11292 ->bind_privilege(pr_all)
888af770
FCE
11293 ->bind(new uprobe_builder ());
11294
0a6f5a3f
JS
11295 // kernel tracepoint probes
11296 s.pattern_root->bind(TOK_KERNEL)->bind_str(TOK_TRACE)
11297 ->bind(new tracepoint_builder());
11298
e6fe60e7
AM
11299 // Kprobe based probe
11300 s.pattern_root->bind(TOK_KPROBE)->bind_str(TOK_FUNCTION)
11301 ->bind(new kprobe_builder());
3c57fe1f
JS
11302 s.pattern_root->bind(TOK_KPROBE)->bind_str(TOK_FUNCTION)->bind(TOK_CALL)
11303 ->bind(new kprobe_builder());
e6fe60e7
AM
11304 s.pattern_root->bind(TOK_KPROBE)->bind_str(TOK_MODULE)
11305 ->bind_str(TOK_FUNCTION)->bind(new kprobe_builder());
3c57fe1f
JS
11306 s.pattern_root->bind(TOK_KPROBE)->bind_str(TOK_MODULE)
11307 ->bind_str(TOK_FUNCTION)->bind(TOK_CALL)->bind(new kprobe_builder());
e6fe60e7
AM
11308 s.pattern_root->bind(TOK_KPROBE)->bind_str(TOK_FUNCTION)->bind(TOK_RETURN)
11309 ->bind(new kprobe_builder());
b6371390
JS
11310 s.pattern_root->bind(TOK_KPROBE)->bind_str(TOK_FUNCTION)->bind(TOK_RETURN)
11311 ->bind_num(TOK_MAXACTIVE)->bind(new kprobe_builder());
e6fe60e7
AM
11312 s.pattern_root->bind(TOK_KPROBE)->bind_str(TOK_MODULE)
11313 ->bind_str(TOK_FUNCTION)->bind(TOK_RETURN)->bind(new kprobe_builder());
b6371390
JS
11314 s.pattern_root->bind(TOK_KPROBE)->bind_str(TOK_MODULE)
11315 ->bind_str(TOK_FUNCTION)->bind(TOK_RETURN)
11316 ->bind_num(TOK_MAXACTIVE)->bind(new kprobe_builder());
e6fe60e7
AM
11317 s.pattern_root->bind(TOK_KPROBE)->bind_num(TOK_STATEMENT)
11318 ->bind(TOK_ABSOLUTE)->bind(new kprobe_builder());
dd225250
PS
11319
11320 //Hwbkpt based probe
b47f3a55
FCE
11321 // NB: we formerly registered the probe point types only if the kernel configuration
11322 // allowed it. However, we get better error messages if we allow probes to resolve.
11323 s.pattern_root->bind(TOK_KERNEL)->bind_num(TOK_HWBKPT)
11324 ->bind(TOK_HWBKPT_WRITE)->bind(new hwbkpt_builder());
11325 s.pattern_root->bind(TOK_KERNEL)->bind_str(TOK_HWBKPT)
11326 ->bind(TOK_HWBKPT_WRITE)->bind(new hwbkpt_builder());
11327 s.pattern_root->bind(TOK_KERNEL)->bind_num(TOK_HWBKPT)
11328 ->bind(TOK_HWBKPT_RW)->bind(new hwbkpt_builder());
11329 s.pattern_root->bind(TOK_KERNEL)->bind_str(TOK_HWBKPT)
11330 ->bind(TOK_HWBKPT_RW)->bind(new hwbkpt_builder());
11331 s.pattern_root->bind(TOK_KERNEL)->bind_num(TOK_HWBKPT)
11332 ->bind_num(TOK_LENGTH)->bind(TOK_HWBKPT_WRITE)->bind(new hwbkpt_builder());
11333 s.pattern_root->bind(TOK_KERNEL)->bind_num(TOK_HWBKPT)
11334 ->bind_num(TOK_LENGTH)->bind(TOK_HWBKPT_RW)->bind(new hwbkpt_builder());
11335 // length supported with address only, not symbol names
83ea76b1
WC
11336
11337 //perf event based probe
4763f713 11338 register_tapset_perf(s);
a29858ef 11339 register_tapset_java(s);
b55bc428 11340}
dc38c0ae
DS
11341
11342
b20febf3
FCE
11343vector<derived_probe_group*>
11344all_session_groups(systemtap_session& s)
dc38c0ae 11345{
b20febf3 11346 vector<derived_probe_group*> g;
912e8c59
JS
11347
11348#define DOONE(x) \
11349 if (s. x##_derived_probes) \
11350 g.push_back ((derived_probe_group*)(s. x##_derived_probes))
ab655cf8
DS
11351
11352 // Note that order *is* important here. We want to make sure we
11353 // register (actually run) begin probes before any other probe type
11354 // is run. Similarly, when unregistering probes, we want to
11355 // unregister (actually run) end probes after every other probe type
11356 // has be unregistered. To do the latter,
11357 // c_unparser::emit_module_exit() will run this list backwards.
b20febf3
FCE
11358 DOONE(be);
11359 DOONE(dwarf);
888af770 11360 DOONE(uprobe);
b20febf3
FCE
11361 DOONE(timer);
11362 DOONE(profile);
11363 DOONE(mark);
0a6f5a3f 11364 DOONE(tracepoint);
e6fe60e7 11365 DOONE(kprobe);
dd225250 11366 DOONE(hwbkpt);
83ea76b1 11367 DOONE(perf);
b20febf3 11368 DOONE(hrtimer);
ce82316f 11369 DOONE(procfs);
8d9609f5 11370 DOONE(netfilter);
935447c8
DS
11371
11372 // Another "order is important" item. We want to make sure we
11373 // "register" the dummy task_finder probe group after all probe
11374 // groups that use the task_finder.
11375 DOONE(utrace);
a96d1db0 11376 DOONE(itrace);
f31a77f5 11377 DOONE(dynprobe);
935447c8 11378 DOONE(task_finder);
b20febf3
FCE
11379#undef DOONE
11380 return g;
46b84a80 11381}
73267b89
JS
11382
11383/* vim: set sw=2 ts=8 cino=>4,n-2,{2,^-2,t0,(0,u0,w1,M1 : */
This page took 3.084324 seconds and 5 git commands to generate.