]> sourceware.org Git - systemtap.git/blame - tapsets.cxx
Fixed systemtap-server.exp
[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
56e12059 66using namespace std;
2171f774 67using namespace __gnu_cxx;
56e12059 68
47dd066d 69
b20febf3
FCE
70
71// ------------------------------------------------------------------------
faea5e16
JS
72
73string
74common_probe_init (derived_probe* p)
75{
26e63673
JS
76 assert(p->session_index != (unsigned)-1);
77 return "(&stap_probes[" + lex_cast(p->session_index) + "])";
faea5e16
JS
78}
79
80
b20febf3 81void
71db462b
DS
82common_probe_entryfn_prologue (systemtap_session& s,
83 string statestr, string probe,
84 string probe_type, bool overload_processing)
b20febf3 85{
944c9a7a 86 if (s.runtime_usermode_p())
ac34f2aa
JS
87 {
88 // If session_state() is NULL, then we haven't even initialized shm yet,
89 // and there's *nothing* for the probe to do. (even alibi is in shm)
90 // So failure skips this whole block through the end of the epilogue.
91 s.op->newline() << "if (likely(session_state())) {";
e481ab2e 92 s.op->indent(1);
ac34f2aa
JS
93 }
94
71db462b 95 s.op->newline() << "#ifdef STP_ALIBI";
7c3e97f4 96 s.op->newline() << "atomic_inc(probe_alibi(" << probe << "->index));";
71db462b 97 s.op->newline() << "#else";
653e6a9a 98
e481ab2e
JS
99 if (s.runtime_usermode_p())
100 s.op->newline() << "int _stp_saved_errno = errno;";
101
fee09ad5 102 s.op->newline() << "struct context* __restrict__ c = NULL;";
71db462b
DS
103 s.op->newline() << "#if !INTERRUPTIBLE";
104 s.op->newline() << "unsigned long flags;";
105 s.op->newline() << "#endif";
b20febf3 106
f887a8c9 107 s.op->newline() << "#ifdef STP_TIMING";
7c3e97f4 108 s.op->newline() << "Stat stat = probe_timing(" << probe << "->index);";
f887a8c9
DS
109 s.op->newline() << "#endif";
110 if (overload_processing && !s.runtime_usermode_p())
71db462b 111 s.op->newline() << "#if defined(STP_TIMING) || defined(STP_OVERLOAD)";
a58d79d0 112 else
71db462b 113 s.op->newline() << "#ifdef STP_TIMING";
b20febf3 114
f887a8c9
DS
115 if (! s.runtime_usermode_p())
116 s.op->newline() << "cycles_t cycles_atstart = get_cycles ();";
117 else
118 {
119 s.op->newline() << "struct timespec timespec_atstart;";
45639d96 120 s.op->newline() << "(void)clock_gettime(CLOCK_MONOTONIC, &timespec_atstart);";
f887a8c9 121 }
71db462b 122 s.op->newline() << "#endif";
994aac0e 123
d2d39de6 124 s.op->newline() << "#if !INTERRUPTIBLE";
71db462b
DS
125 s.op->newline() << "local_irq_save (flags);";
126 s.op->newline() << "#endif";
b20febf3 127
ac3af990 128 if (! s.runtime_usermode_p())
71db462b
DS
129 {
130 // Check for enough free enough stack space
131 s.op->newline() << "if (unlikely ((((unsigned long) (& c)) & (THREAD_SIZE-1))"; // free space
132 s.op->newline(1) << "< (MINSTACKSPACE + sizeof (struct thread_info)))) {"; // needed space
133 // XXX: may need porting to platforms where task_struct is not
134 // at bottom of kernel stack NB: see also
135 // CONFIG_DEBUG_STACKOVERFLOW
065d5567 136 s.op->newline() << "atomic_inc (skipped_count());";
71db462b 137 s.op->newline() << "#ifdef STP_TIMING";
065d5567 138 s.op->newline() << "atomic_inc (skipped_count_lowstack());";
71db462b
DS
139 s.op->newline() << "#endif";
140 s.op->newline() << "goto probe_epilogue;";
141 s.op->newline(-1) << "}";
142 }
c931ec8a 143
065d5567 144 s.op->newline() << "if (atomic_read (session_state()) != " << statestr << ")";
71db462b
DS
145 s.op->newline(1) << "goto probe_epilogue;";
146 s.op->indent(-1);
9a604fac 147
1d0e697d 148 s.op->newline() << "c = _stp_runtime_entryfn_get_context();";
d2d39de6 149 s.op->newline() << "if (!c) {";
71db462b 150 s.op->newline(1) << "#if !INTERRUPTIBLE";
065d5567 151 s.op->newline() << "atomic_inc (skipped_count());";
71db462b
DS
152 s.op->newline() << "#endif";
153 s.op->newline() << "#ifdef STP_TIMING";
065d5567 154 s.op->newline() << "atomic_inc (skipped_count_reentrant());";
71db462b 155 s.op->newline() << "#endif";
71db462b
DS
156 s.op->newline() << "goto probe_epilogue;";
157 s.op->newline(-1) << "}";
d2d39de6 158
71db462b
DS
159 s.op->newline();
160 s.op->newline() << "c->last_stmt = 0;";
161 s.op->newline() << "c->last_error = 0;";
162 s.op->newline() << "c->nesting = -1;"; // NB: PR10516 packs locals[] tighter
163 s.op->newline() << "c->uregs = 0;";
164 s.op->newline() << "c->kregs = 0;";
165 s.op->newline() << "#if defined __ia64__";
166 s.op->newline() << "c->unwaddr = 0;";
167 s.op->newline() << "#endif";
f4d70a33
JS
168 if (s.runtime_usermode_p())
169 s.op->newline() << "c->probe_index = " << probe << "->index;";
71db462b
DS
170 s.op->newline() << "c->probe_point = " << probe << "->pp;";
171 s.op->newline() << "#ifdef STP_NEED_PROBE_NAME";
172 s.op->newline() << "c->probe_name = " << probe << "->pn;";
173 s.op->newline() << "#endif";
174 s.op->newline() << "c->probe_type = " << probe_type << ";";
6dceb5c9 175 // reset Individual Probe State union
71db462b 176 s.op->newline() << "memset(&c->ips, 0, sizeof(c->ips));";
e04b5d74 177 s.op->newline() << "c->user_mode_p = 0; c->full_uregs_p = 0;";
71db462b
DS
178 s.op->newline() << "#ifdef STAP_NEED_REGPARM"; // i386 or x86_64 register.stp
179 s.op->newline() << "c->regparm = 0;";
180 s.op->newline() << "#endif";
e0a17418 181
152fa051
LB
182 if(!s.suppress_time_limits){
183 s.op->newline() << "#if INTERRUPTIBLE";
184 s.op->newline() << "c->actionremaining = MAXACTION_INTERRUPTIBLE;";
185 s.op->newline() << "#else";
186 s.op->newline() << "c->actionremaining = MAXACTION;";
187 s.op->newline() << "#endif";
188 }
9915575b
FCE
189 // NB: The following would actually be incorrect.
190 // That's because cycles_sum/cycles_base values are supposed to survive
191 // between consecutive probes. Periodically (STP_OVERLOAD_INTERVAL
192 // cycles), the values will be reset.
193 /*
71db462b
DS
194 s.op->newline() << "#ifdef STP_OVERLOAD";
195 s.op->newline() << "c->cycles_sum = 0;";
196 s.op->newline() << "c->cycles_base = 0;";
197 s.op->newline() << "#endif";
9915575b 198 */
44cfbe25
SM
199
200 s.op->newline() << "#if defined(STP_NEED_UNWIND_DATA)";
201 s.op->newline() << "c->uwcache_user.state = uwcache_uninitialized;";
202 s.op->newline() << "c->uwcache_kernel.state = uwcache_uninitialized;";
203 s.op->newline() << "#endif";
b20febf3 204}
9a604fac 205
a44a0785 206
b20febf3 207void
f887a8c9 208common_probe_entryfn_epilogue (systemtap_session& s,
ef1337ee
JL
209 bool overload_processing,
210 bool schedule_work_safe)
b20febf3 211{
ef1337ee
JL
212 if (!s.runtime_usermode_p()
213 && schedule_work_safe)
214 {
215 // If a refresh is required, we can safely schedule_work() here
216 s.op->newline( 0) << "if (atomic_cmpxchg(&need_module_refresh, 1, 0) == 1)";
217 s.op->newline(+1) << "schedule_work(&module_refresher_work);";
218 s.op->indent(-1);
219 }
220
f887a8c9
DS
221 if (overload_processing && !s.runtime_usermode_p())
222 s.op->newline() << "#if defined(STP_TIMING) || defined(STP_OVERLOAD)";
a58d79d0 223 else
f887a8c9
DS
224 s.op->newline() << "#ifdef STP_TIMING";
225 s.op->newline() << "{";
226 s.op->indent(1);
227 if (! s.runtime_usermode_p())
228 {
229 s.op->newline() << "cycles_t cycles_atend = get_cycles ();";
230 // NB: we truncate cycles counts to 32 bits. Perhaps it should be
231 // fewer, if the hardware counter rolls over really quickly. We
232 // handle 32-bit wraparound here.
233 s.op->newline() << "int32_t cycles_elapsed = ((int32_t)cycles_atend > (int32_t)cycles_atstart)";
234 s.op->newline(1) << "? ((int32_t)cycles_atend - (int32_t)cycles_atstart)";
235 s.op->newline() << ": (~(int32_t)0) - (int32_t)cycles_atstart + (int32_t)cycles_atend + 1;";
236 s.op->indent(-1);
237 }
238 else
239 {
240 s.op->newline() << "struct timespec timespec_atend, timespec_elapsed;";
241 s.op->newline() << "long cycles_elapsed;";
45639d96 242 s.op->newline() << "(void)clock_gettime(CLOCK_MONOTONIC, &timespec_atend);";
f887a8c9
DS
243 s.op->newline() << "_stp_timespec_sub(&timespec_atend, &timespec_atstart, &timespec_elapsed);";
244 // 'cycles_elapsed' is really elapsed nanoseconds
245 s.op->newline() << "cycles_elapsed = (timespec_elapsed.tv_sec * NSEC_PER_SEC) + timespec_elapsed.tv_nsec;";
246 }
247
248 s.op->newline() << "#ifdef STP_TIMING";
249 s.op->newline() << "if (likely (stat)) _stp_stat_add(stat, cycles_elapsed);";
250 s.op->newline() << "#endif";
251
252 if (overload_processing && !s.runtime_usermode_p())
253 {
254 s.op->newline() << "#ifdef STP_OVERLOAD";
255 s.op->newline() << "{";
a58d79d0
DS
256 // If the cycle count has wrapped (cycles_atend > cycles_base),
257 // let's go ahead and pretend the interval has been reached.
258 // This should reset cycles_base and cycles_sum.
f887a8c9
DS
259 s.op->newline(1) << "cycles_t interval = (cycles_atend > c->cycles_base)";
260 s.op->newline(1) << "? (cycles_atend - c->cycles_base)";
261 s.op->newline() << ": (STP_OVERLOAD_INTERVAL + 1);";
262 s.op->newline(-1) << "c->cycles_sum += cycles_elapsed;";
a58d79d0
DS
263
264 // If we've spent more than STP_OVERLOAD_THRESHOLD cycles in a
265 // probe during the last STP_OVERLOAD_INTERVAL cycles, the probe
266 // has overloaded the system and we need to quit.
7baf48e9
FCE
267 // NB: this is not suppressible via --suppress-runtime-errors,
268 // because this is a system safety metric that we cannot trust
269 // unprivileged users to override.
f887a8c9
DS
270 s.op->newline() << "if (interval > STP_OVERLOAD_INTERVAL) {";
271 s.op->newline(1) << "if (c->cycles_sum > STP_OVERLOAD_THRESHOLD) {";
272 s.op->newline(1) << "_stp_error (\"probe overhead exceeded threshold\");";
065d5567
JS
273 s.op->newline() << "atomic_set (session_state(), STAP_SESSION_ERROR);";
274 s.op->newline() << "atomic_inc (error_count());";
f887a8c9
DS
275 s.op->newline(-1) << "}";
276
277 s.op->newline() << "c->cycles_base = cycles_atend;";
278 s.op->newline() << "c->cycles_sum = 0;";
279 s.op->newline(-1) << "}";
280 s.op->newline(-1) << "}";
281 s.op->newline() << "#endif";
a58d79d0 282 }
e57b735a 283
f887a8c9
DS
284 s.op->newline(-1) << "}";
285 s.op->newline() << "#endif";
e57b735a 286
f887a8c9
DS
287 s.op->newline() << "c->probe_point = 0;"; // vacated
288 s.op->newline() << "#ifdef STP_NEED_PROBE_NAME";
289 s.op->newline() << "c->probe_name = 0;";
290 s.op->newline() << "#endif";
291 s.op->newline() << "c->probe_type = 0;";
7baf48e9
FCE
292
293
f887a8c9
DS
294 s.op->newline() << "if (unlikely (c->last_error && c->last_error[0])) {";
295 s.op->indent(1);
296 if (s.suppress_handler_errors) // PR 13306
b78a0fbb 297 {
065d5567 298 s.op->newline() << "atomic_inc (error_count());";
7baf48e9
FCE
299 }
300 else
301 {
f887a8c9
DS
302 s.op->newline() << "if (c->last_stmt != NULL)";
303 s.op->newline(1) << "_stp_softerror (\"%s near %s\", c->last_error, c->last_stmt);";
304 s.op->newline(-1) << "else";
305 s.op->newline(1) << "_stp_softerror (\"%s\", c->last_error);";
306 s.op->indent(-1);
065d5567
JS
307 s.op->newline() << "atomic_inc (error_count());";
308 s.op->newline() << "if (atomic_read (error_count()) > MAXERRORS) {";
309 s.op->newline(1) << "atomic_set (session_state(), STAP_SESSION_ERROR);";
f887a8c9
DS
310 s.op->newline() << "_stp_exit ();";
311 s.op->newline(-1) << "}";
7baf48e9
FCE
312 }
313
f887a8c9 314 s.op->newline(-1) << "}";
7baf48e9
FCE
315
316
f887a8c9
DS
317 s.op->newline(-1) << "probe_epilogue:"; // context is free
318 s.op->indent(1);
e57b735a 319
f887a8c9 320 if (! s.suppress_handler_errors) // PR 13306
7baf48e9
FCE
321 {
322 // Check for excessive skip counts.
065d5567
JS
323 s.op->newline() << "if (unlikely (atomic_read (skipped_count()) > MAXSKIPPED)) {";
324 s.op->newline(1) << "if (unlikely (pseudo_atomic_cmpxchg(session_state(), STAP_SESSION_RUNNING, STAP_SESSION_ERROR) == STAP_SESSION_RUNNING))";
f887a8c9
DS
325 s.op->newline() << "_stp_error (\"Skipped too many probes, check MAXSKIPPED or try again with stap -t for more details.\");";
326 s.op->newline(-1) << "}";
7baf48e9 327 }
e57b735a 328
d2d39de6
JS
329 // We mustn't release the context until after all _stp_error(), so dyninst
330 // mode can still access the log buffers stored therein.
331 s.op->newline() << "_stp_runtime_entryfn_put_context(c);";
332
333 s.op->newline() << "#if !INTERRUPTIBLE";
f887a8c9
DS
334 s.op->newline() << "local_irq_restore (flags);";
335 s.op->newline() << "#endif";
653e6a9a 336
944c9a7a 337 if (s.runtime_usermode_p())
ac34f2aa
JS
338 {
339 s.op->newline() << "errno = _stp_saved_errno;";
ac34f2aa 340 }
e481ab2e
JS
341
342 s.op->newline() << "#endif // STP_ALIBI";
343
344 if (s.runtime_usermode_p())
345 s.op->newline(-1) << "}";
440f755a 346}
e57b735a 347
e57b735a 348
440f755a 349// ------------------------------------------------------------------------
e57b735a 350
440f755a
JS
351// ------------------------------------------------------------------------
352// Dwarf derived probes. "We apologize for the inconvience."
353// ------------------------------------------------------------------------
e57b735a 354
4627ed58
JS
355static const string TOK_KERNEL("kernel");
356static const string TOK_MODULE("module");
357static const string TOK_FUNCTION("function");
358static const string TOK_INLINE("inline");
359static const string TOK_CALL("call");
4bda987e 360static const string TOK_EXPORTED("exported");
4627ed58
JS
361static const string TOK_RETURN("return");
362static const string TOK_MAXACTIVE("maxactive");
363static const string TOK_STATEMENT("statement");
364static const string TOK_ABSOLUTE("absolute");
365static const string TOK_PROCESS("process");
a794dbeb 366static const string TOK_PROVIDER("provider");
4627ed58
JS
367static const string TOK_MARK("mark");
368static const string TOK_TRACE("trace");
369static const string TOK_LABEL("label");
63b4fd14 370static const string TOK_LIBRARY("library");
576eaefe 371static const string TOK_PLT("plt");
e7d4410d
LB
372static const string TOK_METHOD("method");
373static const string TOK_CLASS("class");;
c31add50
JL
374static const string TOK_CALLEE("callee");;
375static const string TOK_CALLEES("callees");;
83eeb3ac 376static const string TOK_NEAREST("nearest");;
e57b735a 377
440f755a
JS
378// Can we handle this query with just symbol-table info?
379enum dbinfo_reqt
380{
381 dbr_unknown,
382 dbr_none, // kernel.statement(NUM).absolute
383 dbr_need_symtab, // can get by with symbol table if there's no dwarf
384 dbr_need_dwarf
385};
e57b735a 386
20e4a32c 387
673d0add 388struct dwarf_query; // forward decl
20e4a32c 389
45582cbc
JL
390static int query_cu (Dwarf_Die * cudie, dwarf_query *q);
391static void query_addr(Dwarf_Addr addr, dwarf_query *q);
392static void query_plt_statement(dwarf_query *q);
a781f401 393
440f755a
JS
394struct
395symbol_table
396{
397 module_info *mod_info; // associated module
398 map<string, func_info*> map_by_name;
1c6b77e5 399 multimap<Dwarf_Addr, func_info*> map_by_addr;
2a4acb09
JL
400 map<string, Dwarf_Addr> globals;
401 map<string, Dwarf_Addr> locals;
1c6b77e5 402 typedef multimap<Dwarf_Addr, func_info*>::iterator iterator_t;
440f755a
JS
403 typedef pair<iterator_t, iterator_t> range_t;
404#ifdef __powerpc__
405 GElf_Word opd_section;
406#endif
2867a2a1
JS
407 void add_symbol(const char *name, bool weak, bool descriptor,
408 Dwarf_Addr addr, Dwarf_Addr *high_addr);
440f755a
JS
409 enum info_status get_from_elf();
410 void prepare_section_rejection(Dwfl_Module *mod);
411 bool reject_section(GElf_Word section);
440f755a
JS
412 void purge_syscall_stubs();
413 func_info *lookup_symbol(const string& name);
414 Dwarf_Addr lookup_symbol_address(const string& name);
415 func_info *get_func_containing_address(Dwarf_Addr addr);
3d372d6b 416 func_info *get_first_func();
7a053d3b 417
440f755a
JS
418 symbol_table(module_info *mi) : mod_info(mi) {}
419 ~symbol_table();
420};
77de5e9e 421
440f755a
JS
422static bool null_die(Dwarf_Die *die)
423{
822a6a3d 424 static Dwarf_Die null;
440f755a
JS
425 return (!die || !memcmp(die, &null, sizeof(null)));
426}
c4ce66a1 427
40a393cd
JS
428struct exp_type_dwarf : public exp_type_details
429{
430 // NB: We don't own this dwflpp, so don't use it after build_no_more!
431 // A shared_ptr might help, but expressions are currently so leaky
432 // that we'd probably never clear all references... :/
433 dwflpp* dw;
434 Dwarf_Die die;
66facfa3 435 bool userspace_p;
37efef73
JS
436 bool is_pointer;
437 exp_type_dwarf(dwflpp* dw, Dwarf_Die* die, bool userspace_p, bool addressof);
40a393cd 438 uintptr_t id () const { return reinterpret_cast<uintptr_t>(die.addr); }
eb79eb0a 439 bool expandable() const { return true; }
66facfa3 440 functioncall *expand(autocast_op* e, bool lvalue);
40a393cd
JS
441};
442
c4ce66a1 443
7a053d3b 444enum
bd2b1e68 445function_spec_type
7a053d3b 446 {
bd2b1e68
GH
447 function_alone,
448 function_and_file,
7a053d3b 449 function_file_and_line
bd2b1e68
GH
450 };
451
ec4373ff 452
bd2b1e68 453struct dwarf_builder;
f10534c6 454struct dwarf_var_expanding_visitor;
77de5e9e 455
2930abc7 456
b20febf3
FCE
457// XXX: This class is a candidate for subclassing to separate
458// the relocation vs non-relocation variants. Likewise for
459// kprobe vs kretprobe variants.
460
461struct dwarf_derived_probe: public derived_probe
b55bc428 462{
b20febf3
FCE
463 dwarf_derived_probe (const string& function,
464 const string& filename,
465 int line,
466 const string& module,
467 const string& section,
468 Dwarf_Addr dwfl_addr,
2930abc7 469 Dwarf_Addr addr,
b20febf3
FCE
470 dwarf_query & q,
471 Dwarf_Die* scope_die);
20e4a32c 472
b20febf3
FCE
473 string module;
474 string section;
475 Dwarf_Addr addr;
63b4fd14 476 string path;
27dc09b1 477 bool has_process;
2930abc7 478 bool has_return;
c9bad430 479 bool has_maxactive;
63b4fd14 480 bool has_library;
c9bad430 481 long maxactive_val;
4ad95bbc 482 // dwarf_derived_probe_group::emit_module_decls uses this to emit sdt kprobe definition
b642c901
SC
483 string user_path;
484 string user_lib;
b95e2b79 485 bool access_vars;
2930abc7 486
af234c40
JS
487 unsigned saved_longs, saved_strings;
488 dwarf_derived_probe* entry_handler;
489
b8da0ad1 490 void printsig (std::ostream &o) const;
6b66b9f7 491 virtual void join_group (systemtap_session& s);
3689db05 492 void emit_probe_local_init(systemtap_session& s, translator_output * o);
d0bfd2ac 493 void getargs(std::list<std::string> &arg_set) const;
0a98fd42 494
42e38653 495 void emit_privilege_assertion (translator_output*);
27dc09b1
DB
496 void print_dupe_stamp(ostream& o);
497
bd2b1e68 498 // Pattern registration helpers.
7a053d3b 499 static void register_statement_variants(match_node * root,
27dc09b1 500 dwarf_builder * dw,
42e38653 501 privilege_t privilege);
fd6602a0 502 static void register_function_variants(match_node * root,
27dc09b1 503 dwarf_builder * dw,
42e38653 504 privilege_t privilege);
440d9b00
DB
505 static void register_function_and_statement_variants(systemtap_session& s,
506 match_node * root,
27dc09b1 507 dwarf_builder * dw,
42e38653 508 privilege_t privilege);
b1615c74
JS
509 static void register_sdt_variants(systemtap_session& s,
510 match_node * root,
511 dwarf_builder * dw);
512 static void register_plt_variants(systemtap_session& s,
513 match_node * root,
514 dwarf_builder * dw);
c4ce66a1 515 static void register_patterns(systemtap_session& s);
6b66b9f7
JS
516
517protected:
518 dwarf_derived_probe(probe *base,
519 probe_point *location,
520 Dwarf_Addr addr,
521 bool has_return):
74fe61bc
LB
522 derived_probe(base, location), addr(addr), has_process(0),
523 has_return(has_return), has_maxactive(0), has_library(0),
524 maxactive_val(0), access_vars(false), saved_longs(0),
525 saved_strings(0), entry_handler(0)
6b66b9f7
JS
526 {}
527
528private:
d0bfd2ac 529 list<string> args;
8c67c337 530 void saveargs(dwarf_query& q, Dwarf_Die* scope_die, Dwarf_Addr dwfl_addr);
20c6c071
GH
531};
532
dc38c0ae 533
6b66b9f7 534struct uprobe_derived_probe: public dwarf_derived_probe
6d0f3f0c 535{
6d0f3f0c 536 int pid; // 0 => unrestricted
0973d815 537
6d0f3f0c
FCE
538 uprobe_derived_probe (const string& function,
539 const string& filename,
540 int line,
541 const string& module,
6d0f3f0c
FCE
542 const string& section,
543 Dwarf_Addr dwfl_addr,
544 Dwarf_Addr addr,
545 dwarf_query & q,
d11f62b7 546 Dwarf_Die* scope_die);
6d0f3f0c 547
0973d815
FCE
548 // alternate constructor for process(PID).statement(ADDR).absolute
549 uprobe_derived_probe (probe *base,
550 probe_point *location,
551 int pid,
552 Dwarf_Addr addr,
6b66b9f7
JS
553 bool has_return):
554 dwarf_derived_probe(base, location, addr, has_return), pid(pid)
555 {}
9ace370f 556
6d0f3f0c 557 void join_group (systemtap_session& s);
2865d17a 558
42e38653 559 void emit_privilege_assertion (translator_output*);
8f6d8c2b 560 void print_dupe_stamp(ostream& o) { print_dupe_stamp_unprivileged_process_owner (o); }
c0f84e7b
SC
561 void getargs(std::list<std::string> &arg_set) const;
562 void saveargs(int nargs);
563private:
564 list<string> args;
6d0f3f0c
FCE
565};
566
dc38c0ae
DS
567struct dwarf_derived_probe_group: public derived_probe_group
568{
569private:
b20febf3
FCE
570 multimap<string,dwarf_derived_probe*> probes_by_module;
571 typedef multimap<string,dwarf_derived_probe*>::iterator p_b_m_iterator;
dc38c0ae
DS
572
573public:
08b5a50c 574 dwarf_derived_probe_group() {}
b20febf3
FCE
575 void enroll (dwarf_derived_probe* probe);
576 void emit_module_decls (systemtap_session& s);
577 void emit_module_init (systemtap_session& s);
b4be7cbc 578 void emit_module_refresh (systemtap_session& s);
b20febf3 579 void emit_module_exit (systemtap_session& s);
ca6d3b0f
JL
580 bool otf_supported (systemtap_session& s) { return true; }
581
582 // workqueue handling not safe in kprobes context
583 bool otf_safe_context (systemtap_session& s) { return false; }
dc38c0ae
DS
584};
585
20c6c071 586// Helper struct to thread through the dwfl callbacks.
2c384610 587struct base_query
20c6c071 588{
c4ce66a1
JS
589 base_query(dwflpp & dw, literal_map_t const & params);
590 base_query(dwflpp & dw, const string & module_val);
2c384610 591 virtual ~base_query() {}
bd2b1e68 592
5227f1ea 593 systemtap_session & sess;
2c384610 594 dwflpp & dw;
5227f1ea 595
070764c0
JL
596 // Used to keep track of which modules were visited during
597 // iterate_over_modules()
598 set<string> visited_modules;
599
bd2b1e68 600 // Parameter extractors.
86bf665e 601 static bool has_null_param(literal_map_t const & params,
888af770 602 string const & k);
86bf665e 603 static bool get_string_param(literal_map_t const & params,
bd2b1e68 604 string const & k, string & v);
86bf665e 605 static bool get_number_param(literal_map_t const & params,
bd2b1e68 606 string const & k, long & v);
7e28cf76
WC
607 static bool get_number_param(literal_map_t const & params,
608 string const & k, long long & v);
86bf665e 609 static bool get_number_param(literal_map_t const & params,
c239d28c 610 string const & k, Dwarf_Addr & v);
bbbc7241 611 static void query_library_callback (base_query *me, const char *data);
45cdb40e 612 static void query_plt_callback (base_query *me, const char *link, size_t addr);
f301a9ff 613 virtual void query_library (const char *data) = 0;
576eaefe 614 virtual void query_plt (const char *link, size_t addr) = 0;
84c84ac4 615
b55bc428 616
2c384610
DS
617 // Extracted parameters.
618 bool has_kernel;
91af0778
FCE
619 bool has_module;
620 bool has_process;
63b4fd14 621 bool has_library;
576eaefe
SC
622 bool has_plt;
623 bool has_statement;
2c384610 624 string module_val; // has_kernel => module_val = "kernel"
63b4fd14 625 string path; // executable path if module is a .so
576eaefe 626 string plt_val; // has_plt => plt wildcard
ff8bd809 627 int64_t pid_val;
2c384610
DS
628
629 virtual void handle_query_module() = 0;
630};
631
c4ce66a1 632base_query::base_query(dwflpp & dw, literal_map_t const & params):
c88bcbe8
AJ
633 sess(dw.sess), dw(dw), has_library(false), has_plt(false), has_statement(false),
634 pid_val(0)
2c384610 635{
91af0778 636 has_kernel = has_null_param (params, TOK_KERNEL);
2c384610
DS
637 if (has_kernel)
638 module_val = "kernel";
91af0778
FCE
639
640 has_module = get_string_param (params, TOK_MODULE, module_val);
641 if (has_module)
642 has_process = false;
4baf0e53 643 else
d0a7f5a9 644 {
63b4fd14 645 string library_name;
576eaefe 646 long statement_num_val;
37635b45 647 has_process = derived_probe_builder::has_param(params, TOK_PROCESS);
63b4fd14 648 has_library = get_string_param (params, TOK_LIBRARY, library_name);
576eaefe
SC
649 if ((has_plt = has_null_param (params, TOK_PLT)))
650 plt_val = "*";
651 else has_plt = get_string_param (params, TOK_PLT, plt_val);
652 has_statement = get_number_param(params, TOK_STATEMENT, statement_num_val);
653
84c84ac4 654 if (has_process)
84c84ac4 655 {
c88bcbe8 656 if (get_number_param(params, TOK_PROCESS, pid_val))
37635b45
AJ
657 {
658 // check that the pid given corresponds to a running process
a03a3744
JS
659 string pid_err_msg;
660 if (!is_valid_pid(pid_val, pid_err_msg))
661 throw SEMANTIC_ERROR(pid_err_msg);
662
c88bcbe8 663 string pid_path = string("/proc/") + lex_cast(pid_val) + "/exe";
37635b45
AJ
664 module_val = sess.sysroot + pid_path;
665 }
c88bcbe8
AJ
666 else
667 {
668 // reset the pid_val in case anything weird got written into it
669 pid_val = 0;
37635b45 670 get_string_param(params, TOK_PROCESS, module_val);
c88bcbe8 671 }
5fa0d811
JL
672 module_val = find_executable (module_val, sess.sysroot, sess.sysenv);
673 if (!is_fully_resolved(module_val, sess.sysroot, sess.sysenv))
674 throw SEMANTIC_ERROR(_F("cannot find executable '%s'",
675 module_val.c_str()));
676 }
dbe9d133
JL
677
678 // Library probe? Let's target that instead if it is fully resolved (such
679 // as what query_one_library() would have done for us). Otherwise, we
680 // resort to iterate_over_libraries().
681 if (has_library && is_fully_resolved(library_name, sess.sysroot, sess.sysenv,
682 "LD_LIBRARY_PATH"))
84c84ac4 683 {
dbe9d133
JL
684 path = path_remove_sysroot(sess, module_val);
685 module_val = library_name;
84c84ac4 686 }
d0a7f5a9 687 }
91af0778
FCE
688
689 assert (has_kernel || has_process || has_module);
2c384610
DS
690}
691
c4ce66a1 692base_query::base_query(dwflpp & dw, const string & module_val)
576eaefe 693 : sess(dw.sess), dw(dw), has_library(false), has_plt(false), has_statement(false),
c88bcbe8 694 module_val(module_val), pid_val(0)
c4ce66a1
JS
695{
696 // NB: This uses '/' to distinguish between kernel modules and userspace,
697 // which means that userspace modules won't get any PATH searching.
698 if (module_val.find('/') == string::npos)
699 {
700 has_kernel = (module_val == TOK_KERNEL);
701 has_module = !has_kernel;
702 has_process = false;
703 }
704 else
705 {
706 has_kernel = has_module = false;
707 has_process = true;
708 }
709}
710
2c384610 711bool
86bf665e 712base_query::has_null_param(literal_map_t const & params,
2c384610
DS
713 string const & k)
714{
888af770 715 return derived_probe_builder::has_null_param(params, k);
2c384610
DS
716}
717
718
719bool
86bf665e 720base_query::get_string_param(literal_map_t const & params,
2c384610
DS
721 string const & k, string & v)
722{
723 return derived_probe_builder::get_param (params, k, v);
724}
725
726
727bool
86bf665e 728base_query::get_number_param(literal_map_t const & params,
2c384610
DS
729 string const & k, long & v)
730{
731 int64_t value;
732 bool present = derived_probe_builder::get_param (params, k, value);
733 v = (long) value;
734 return present;
735}
736
737
7e28cf76
WC
738bool
739base_query::get_number_param(literal_map_t const & params,
740 string const & k, long long & v)
741{
742 int64_t value;
743 bool present = derived_probe_builder::get_param (params, k, value);
744 v = (long) value;
745 return present;
746}
747
748
2c384610 749bool
86bf665e 750base_query::get_number_param(literal_map_t const & params,
2c384610
DS
751 string const & k, Dwarf_Addr & v)
752{
753 int64_t value;
754 bool present = derived_probe_builder::get_param (params, k, value);
755 v = (Dwarf_Addr) value;
756 return present;
757}
758
2c384610
DS
759struct dwarf_query : public base_query
760{
e1278bd4 761 dwarf_query(probe * base_probe,
2c384610
DS
762 probe_point * base_loc,
763 dwflpp & dw,
86bf665e 764 literal_map_t const & params,
b642c901
SC
765 vector<derived_probe *> & results,
766 const string user_path,
767 const string user_lib);
2c384610 768
c4ce66a1 769 vector<derived_probe *> & results;
8f14e444 770 set<string> inlined_non_returnable; // function names
c4ce66a1
JS
771 probe * base_probe;
772 probe_point * base_loc;
b642c901
SC
773 string user_path;
774 string user_lib;
c4ce66a1 775
614f0fcf
JL
776 set<string> visited_libraries;
777 bool resolved_library;
778
2c384610 779 virtual void handle_query_module();
5f0a03a6
JK
780 void query_module_dwarf();
781 void query_module_symtab();
5d5bd369 782 void query_library (const char *data);
576eaefe 783 void query_plt (const char *entry, size_t addr);
2c384610 784
2930abc7
FCE
785 void add_probe_point(string const & funcname,
786 char const * filename,
787 int line,
788 Dwarf_Die *scope_die,
789 Dwarf_Addr addr);
36f9dd1d 790
613c8675
JL
791 void mount_well_formed_probe_point();
792 void unmount_well_formed_probe_point();
793 stack<pair<probe_point*, probe*> > previous_bases;
794
71dfce11
JL
795 void replace_probe_point_component_arg(const string& functor,
796 const string& new_functor,
797 int64_t new_arg,
798 bool hex = false);
799 void replace_probe_point_component_arg(const string& functor,
800 int64_t new_arg,
801 bool hex = false);
802 void replace_probe_point_component_arg(const string& functor,
803 const string& new_functor,
804 const string& new_arg);
805 void replace_probe_point_component_arg(const string& functor,
806 const string& new_arg);
807 void remove_probe_point_component(const string& functor);
808
857bdfd1
JS
809 // Track addresses we've already seen in a given module
810 set<Dwarf_Addr> alias_dupes;
811
7fdd3e2c
JS
812 // Track inlines we've already seen as well
813 // NB: this can't be compared just by entrypc, as inlines can overlap
814 set<inline_instance_info> inline_dupes;
815
e2941743
JL
816 // Used in .callee[s] probes, when calling iterate_over_callees() (which
817 // provides the actual stack). Retains the addrs of the callers unwind addr
818 // where the callee is found. Specifies multiple callers. E.g. when a callee
819 // at depth 2 is found, callers[1] has the addr of the caller, and callers[0]
820 // has the addr of the caller's caller.
821 stack<Dwarf_Addr> *callers;
822
20c6c071
GH
823 bool has_function_str;
824 bool has_statement_str;
825 bool has_function_num;
826 bool has_statement_num;
7a053d3b
RM
827 string statement_str_val;
828 string function_str_val;
c239d28c
GH
829 Dwarf_Addr statement_num_val;
830 Dwarf_Addr function_num_val;
20c6c071 831
b8da0ad1 832 bool has_call;
4bda987e 833 bool has_exported;
b8da0ad1 834 bool has_inline;
20c6c071
GH
835 bool has_return;
836
83eeb3ac
HK
837 bool has_nearest;
838
c9bad430
DS
839 bool has_maxactive;
840 long maxactive_val;
841
20c6c071
GH
842 bool has_label;
843 string label_val;
844
c31add50
JL
845 bool has_callee;
846 string callee_val;
847
848 bool has_callees_num;
849 long callees_num_val;
850
20c6c071
GH
851 bool has_relative;
852 long relative_val;
853
37ebca01
FCE
854 bool has_absolute;
855
467bea43
SC
856 bool has_mark;
857
5f0a03a6
JK
858 enum dbinfo_reqt dbinfo_reqt;
859 enum dbinfo_reqt assess_dbinfo_reqt();
860
7d6d0afc 861 void parse_function_spec(const string & spec);
20c6c071 862 function_spec_type spec_type;
7d6d0afc 863 vector<string> scopes;
20c6c071
GH
864 string function;
865 string file;
4a81c0d9 866 lineno_t lineno_type;
f01d4ffb 867 vector<int> linenos;
5f0a03a6 868 bool query_done; // Found exact match
20c6c071 869
5a617dc6
JL
870 // Holds the prologue end of the current function
871 Dwarf_Addr prologue_end;
872
bd25380d 873 set<string> filtered_srcfiles;
7e1279ea
FCE
874
875 // Map official entrypc -> func_info object
86bf665e
TM
876 inline_instance_map_t filtered_inlines;
877 func_info_map_t filtered_functions;
4df79aaf 878
959ccfed
JL
879 // Helper when we want to iterate over both
880 base_func_info_map_t filtered_all();
881
4df79aaf 882 void query_module_functions ();
e772a6e7
JL
883
884 string final_function_name(const string& final_func,
79954c0f 885 const char* final_file,
e772a6e7 886 int final_line);
c5142c66
JL
887
888 bool is_fully_specified_function();
b55bc428
FCE
889};
890
98afd80e 891
d11f62b7
AJ
892uprobe_derived_probe::uprobe_derived_probe (const string& function,
893 const string& filename,
894 int line,
895 const string& module,
896 const string& section,
897 Dwarf_Addr dwfl_addr,
898 Dwarf_Addr addr,
899 dwarf_query & q,
900 Dwarf_Die* scope_die):
901 dwarf_derived_probe(function, filename, line, module, section,
902 dwfl_addr, addr, q, scope_die), pid(q.pid_val)
903 {}
435f53a7
FCE
904
905
ff8bd809 906static void delete_session_module_cache (systemtap_session& s); // forward decl
435f53a7 907
98afd80e 908struct dwarf_builder: public derived_probe_builder
b55bc428 909{
665e1256 910 map <string,dwflpp*> kern_dw; /* NB: key string could be a wildcard */
7a24d422 911 map <string,dwflpp*> user_dw;
b642c901
SC
912 string user_path;
913 string user_lib;
44ffe90c
JL
914
915 // Holds modules to suggest functions from. NB: aggregates over
916 // recursive calls to build() when deriving globby probes.
917 set <string> modules_seen;
918
ae2552da 919 dwarf_builder() {}
aa30ccd3 920
ae2552da 921 dwflpp *get_kern_dw(systemtap_session& sess, const string& module)
707bf35e 922 {
ea14cf67
FCE
923 if (kern_dw[module] == 0)
924 kern_dw[module] = new dwflpp(sess, module, true); // might throw
ae2552da 925 return kern_dw[module];
707bf35e
JS
926 }
927
928 dwflpp *get_user_dw(systemtap_session& sess, const string& module)
929 {
ea14cf67
FCE
930 if (user_dw[module] == 0)
931 user_dw[module] = new dwflpp(sess, module, false); // might throw
707bf35e
JS
932 return user_dw[module];
933 }
7a24d422
FCE
934
935 /* NB: not virtual, so can be called from dtor too: */
822a6a3d 936 void dwarf_build_no_more (bool)
aa30ccd3 937 {
435f53a7
FCE
938 delete_map(kern_dw);
939 delete_map(user_dw);
7a24d422
FCE
940 }
941
942 void build_no_more (systemtap_session &s)
943 {
944 dwarf_build_no_more (s.verbose > 3);
435f53a7 945 delete_session_module_cache (s);
aa30ccd3
FCE
946 }
947
e38d6504
RM
948 ~dwarf_builder()
949 {
7a24d422 950 dwarf_build_no_more (false);
c8959a29 951 }
aa30ccd3 952
5227f1ea 953 virtual void build(systemtap_session & sess,
7a053d3b 954 probe * base,
20c6c071 955 probe_point * location,
86bf665e 956 literal_map_t const & parameters,
20c6c071 957 vector<derived_probe *> & finished_results);
b55bc428
FCE
958};
959
5111fc3e 960
e1278bd4 961dwarf_query::dwarf_query(probe * base_probe,
20c6c071
GH
962 probe_point * base_loc,
963 dwflpp & dw,
86bf665e 964 literal_map_t const & params,
b642c901
SC
965 vector<derived_probe *> & results,
966 const string user_path,
967 const string user_lib)
e2941743
JL
968 : base_query(dw, params), results(results), base_probe(base_probe),
969 base_loc(base_loc), user_path(user_path), user_lib(user_lib),
614f0fcf
JL
970 resolved_library(false), callers(NULL), has_relative(false),
971 relative_val(0), prologue_end(0)
bd2b1e68
GH
972{
973 // Reduce the query to more reasonable semantic values (booleans,
974 // extracted strings, numbers, etc).
bd2b1e68
GH
975 has_function_str = get_string_param(params, TOK_FUNCTION, function_str_val);
976 has_function_num = get_number_param(params, TOK_FUNCTION, function_num_val);
977
978 has_statement_str = get_string_param(params, TOK_STATEMENT, statement_str_val);
979 has_statement_num = get_number_param(params, TOK_STATEMENT, statement_num_val);
980
0f336e95 981 has_label = get_string_param(params, TOK_LABEL, label_val);
c31add50
JL
982 has_callee = get_string_param(params, TOK_CALLEE, callee_val);
983 if (has_null_param(params, TOK_CALLEES))
984 { // .callees ==> .callees(1) (also equivalent to .callee("*"))
985 has_callees_num = true;
986 callees_num_val = 1;
987 }
988 else
989 {
990 has_callees_num = get_number_param(params, TOK_CALLEES, callees_num_val);
991 if (has_callees_num && callees_num_val < 1)
992 throw SEMANTIC_ERROR(_(".callees(N) only acceptable for N >= 1"),
993 base_probe->tok);
994 }
0f336e95 995
b8da0ad1 996 has_call = has_null_param(params, TOK_CALL);
4bda987e 997 has_exported = has_null_param(params, TOK_EXPORTED);
b8da0ad1 998 has_inline = has_null_param(params, TOK_INLINE);
bd2b1e68 999 has_return = has_null_param(params, TOK_RETURN);
83eeb3ac 1000 has_nearest = has_null_param(params, TOK_NEAREST);
c9bad430 1001 has_maxactive = get_number_param(params, TOK_MAXACTIVE, maxactive_val);
37ebca01 1002 has_absolute = has_null_param(params, TOK_ABSOLUTE);
467bea43 1003 has_mark = false;
37ebca01 1004
bd2b1e68 1005 if (has_function_str)
7d6d0afc 1006 parse_function_spec(function_str_val);
bd2b1e68 1007 else if (has_statement_str)
7d6d0afc 1008 parse_function_spec(statement_str_val);
0daad364 1009
5f0a03a6
JK
1010 dbinfo_reqt = assess_dbinfo_reqt();
1011 query_done = false;
0daad364
JS
1012}
1013
1014
2c384610 1015void
5f0a03a6 1016dwarf_query::query_module_dwarf()
2c384610
DS
1017{
1018 if (has_function_num || has_statement_num)
1019 {
1020 // If we have module("foo").function(0xbeef) or
1021 // module("foo").statement(0xbeef), the address is relative
1022 // to the start of the module, so we seek the function
1023 // number plus the module's bias.
6b517475
JS
1024 Dwarf_Addr addr = has_function_num ?
1025 function_num_val : statement_num_val;
08d1d520
MW
1026
1027 // These are raw addresses, we need to know what the elf_bias
1028 // is to feed it to libdwfl based functions.
1029 Dwarf_Addr elf_bias;
1030 Elf *elf = dwfl_module_getelf (dw.module, &elf_bias);
1031 assert(elf);
1032 addr += elf_bias;
6b517475 1033 query_addr(addr, this);
2c384610
DS
1034 }
1035 else
1036 {
1037 // Otherwise if we have a function("foo") or statement("foo")
1038 // specifier, we have to scan over all the CUs looking for
1039 // the function(s) in question
1040 assert(has_function_str || has_statement_str);
4df79aaf
JS
1041
1042 // For simple cases, no wildcard and no source:line, we can do a very
1043 // quick function lookup in a module-wide cache.
1ffb8bd1
JS
1044 if (spec_type == function_alone &&
1045 !dw.name_has_wildcard(function) &&
1046 !startswith(function, "_Z"))
4df79aaf
JS
1047 query_module_functions();
1048 else
337b7c44 1049 dw.iterate_over_cus(&query_cu, this, false);
2c384610
DS
1050 }
1051}
1052
5f0a03a6
JK
1053static void query_func_info (Dwarf_Addr entrypc, func_info & fi,
1054 dwarf_query * q);
1055
91bb9081
LB
1056static void
1057query_symtab_func_info (func_info & fi, dwarf_query * q)
1058{
1059 assert(null_die(&fi.die));
1060
1061 Dwarf_Addr addr = fi.addr;
1062
1063 // Now compensate for the dw bias because the addresses come
1064 // from dwfl_module_symtab, so fi->addr is NOT a normal dw address.
1065 q->dw.get_module_dwarf(false, false);
1066 addr -= q->dw.module_bias;
1067
1068 // If there are already probes in this module, lets not duplicate.
1069 // This can come from other weak symbols/aliases or existing
a41f2ad7
JS
1070 // matches from Dwarf DIE functions. Try to add this addr to the
1071 // collection, and only continue if it was new.
1072 if (q->alias_dupes.insert(addr).second)
1073 query_func_info(addr, fi, q);
91bb9081
LB
1074}
1075
5f0a03a6
JK
1076void
1077dwarf_query::query_module_symtab()
1078{
1079 // Get the symbol table if it's necessary, sufficient, and not already got.
1080 if (dbinfo_reqt == dbr_need_dwarf)
1081 return;
1082
1083 module_info *mi = dw.mod_info;
1084 if (dbinfo_reqt == dbr_need_symtab)
1085 {
1086 if (mi->symtab_status == info_unknown)
5f52fafe 1087 mi->get_symtab();
5f0a03a6
JK
1088 if (mi->symtab_status == info_absent)
1089 return;
1090 }
1091
1092 func_info *fi = NULL;
1093 symbol_table *sym_table = mi->sym_table;
1094
1095 if (has_function_str)
1096 {
1097 // Per dwarf_query::assess_dbinfo_reqt()...
1098 assert(spec_type == function_alone);
1099 if (dw.name_has_wildcard(function_str_val))
1100 {
2e67a43b 1101 symbol_table::iterator_t iter;
1c6b77e5
JS
1102 for (iter = sym_table->map_by_addr.begin();
1103 iter != sym_table->map_by_addr.end();
2e67a43b 1104 ++iter)
5f0a03a6 1105 {
1c6b77e5 1106 fi = iter->second;
53625401
JS
1107 if (!null_die(&fi->die) // already handled in query_module_dwarf()
1108 || fi->descriptor) // ppc opd (and also undefined symbols)
1109 continue;
5f0a03a6 1110 if (dw.function_name_matches_pattern(fi->name, function_str_val))
91bb9081 1111 query_symtab_func_info(*fi, this);
5f0a03a6
JK
1112 }
1113 }
1114 else
1115 {
1116 fi = sym_table->lookup_symbol(function_str_val);
2867a2a1 1117 if (fi && !fi->descriptor && null_die(&fi->die))
91bb9081 1118 query_symtab_func_info(*fi, this);
5f0a03a6 1119 }
5f0a03a6
JK
1120 }
1121}
1122
1123void
1124dwarf_query::handle_query_module()
1125{
f4faaf86
JS
1126 if (has_plt && has_statement_num)
1127 {
1128 query_plt_statement (this);
1129 return;
1130 }
1131
0035051e 1132 bool report = dbinfo_reqt == dbr_need_dwarf;
1c6b77e5
JS
1133 dw.get_module_dwarf(false, report);
1134
1135 // prebuild the symbol table to resolve aliases
5f52fafe 1136 dw.mod_info->get_symtab();
1c6b77e5 1137
857bdfd1
JS
1138 // reset the dupe-checking for each new module
1139 alias_dupes.clear();
7fdd3e2c 1140 inline_dupes.clear();
857bdfd1 1141
5f0a03a6
JK
1142 if (dw.mod_info->dwarf_status == info_present)
1143 query_module_dwarf();
1c6b77e5 1144
91bb9081
LB
1145 // Consult the symbol table, asm and weak functions can show up
1146 // in the symbol table but not in dwarf and minidebuginfo is
1147 // located in the gnu_debugdata section, alias_dupes checking
1148 // is done before adding any probe points
1149 if (!query_done && !pending_interrupts)
5f0a03a6
JK
1150 query_module_symtab();
1151}
1152
2c384610 1153
7d6d0afc
JS
1154void
1155dwarf_query::parse_function_spec(const string & spec)
bd2b1e68 1156{
4a81c0d9 1157 lineno_type = ABSOLUTE;
f01d4ffb 1158 size_t src_pos, line_pos, scope_pos;
bd2b1e68 1159
7d6d0afc 1160 // look for named scopes
91699a70
JS
1161 scope_pos = spec.rfind("::");
1162 if (scope_pos != string::npos)
bd2b1e68 1163 {
91699a70
JS
1164 tokenize_cxx(spec.substr(0, scope_pos), scopes);
1165 scope_pos += 2;
bd2b1e68 1166 }
91699a70
JS
1167 else
1168 scope_pos = 0;
bd2b1e68 1169
7d6d0afc
JS
1170 // look for a source separator
1171 src_pos = spec.find('@', scope_pos);
1172 if (src_pos == string::npos)
bd2b1e68 1173 {
7d6d0afc
JS
1174 function = spec.substr(scope_pos);
1175 spec_type = function_alone;
bd2b1e68 1176 }
7d6d0afc 1177 else
879eb9e9 1178 {
7d6d0afc 1179 function = spec.substr(scope_pos, src_pos - scope_pos);
7a053d3b 1180
7d6d0afc
JS
1181 // look for a line-number separator
1182 line_pos = spec.find_first_of(":+", src_pos);
1183 if (line_pos == string::npos)
1184 {
1185 file = spec.substr(src_pos + 1);
1186 spec_type = function_and_file;
1187 }
1188 else
1189 {
1190 file = spec.substr(src_pos + 1, line_pos - src_pos - 1);
1191
1192 // classify the line spec
1193 spec_type = function_file_and_line;
1194 if (spec[line_pos] == '+')
4a81c0d9 1195 lineno_type = RELATIVE;
7d6d0afc
JS
1196 else if (spec[line_pos + 1] == '*' &&
1197 spec.length() == line_pos + 2)
4a81c0d9 1198 lineno_type = WILDCARD;
7d6d0afc 1199 else
4a81c0d9 1200 lineno_type = ABSOLUTE;
7d6d0afc 1201
4a81c0d9 1202 if (lineno_type != WILDCARD)
7d6d0afc
JS
1203 try
1204 {
f01d4ffb
BC
1205 // try to parse N, N-M, or N,M,O,P, or combination thereof...
1206 if (spec.find_first_of(",-", line_pos + 1) != string::npos)
1207 {
1208 lineno_type = ENUMERATED;
1209 vector<string> sub_specs;
1210 tokenize(spec.substr(line_pos + 1), sub_specs, ",");
1211 vector<string>::const_iterator line_spec;
1212 for (line_spec = sub_specs.begin(); line_spec != sub_specs.end(); ++line_spec)
1213 {
1214 vector<string> ranges;
1215 tokenize(*line_spec, ranges, "-");
1216 if (ranges.size() > 1)
1217 for (int i = lex_cast<int>(ranges.front()); i <= lex_cast<int>(ranges.back()); i++)
1218 linenos.push_back(i);
1219 else
1220 linenos.push_back(lex_cast<int>(ranges.at(0)));
1221 }
1222 sort(linenos.begin(), linenos.end());
1223 }
7d6d0afc
JS
1224 else
1225 {
f01d4ffb
BC
1226 linenos.push_back(lex_cast<int>(spec.substr(line_pos + 1)));
1227 linenos.push_back(lex_cast<int>(spec.substr(line_pos + 1)));
7d6d0afc
JS
1228 }
1229 }
1230 catch (runtime_error & exn)
1231 {
1232 goto bad;
1233 }
2ff28684
JL
1234
1235 // only allow .nearest with ABSOLUTE and RELATIVE linenos
1236 if (has_nearest && lineno_type != ABSOLUTE
1237 && lineno_type != RELATIVE)
1238 throw SEMANTIC_ERROR(_(".nearest is only valid with absolute or relative line numbers"));
7d6d0afc 1239 }
bd2b1e68
GH
1240 }
1241
7d6d0afc
JS
1242 if (function.empty() ||
1243 (spec_type != function_alone && file.empty()))
bd2b1e68
GH
1244 goto bad;
1245
7d6d0afc 1246 if (sess.verbose > 2)
bd2b1e68 1247 {
b530b5b3
LB
1248 //clog << "parsed '" << spec << "'";
1249 clog << _F("parse '%s'", spec.c_str());
41c262f3 1250
7d6d0afc
JS
1251 if (!scopes.empty())
1252 clog << ", scope '" << scopes[0] << "'";
1253 for (unsigned i = 1; i < scopes.size(); ++i)
1254 clog << "::'" << scopes[i] << "'";
41c262f3 1255
7d6d0afc
JS
1256 clog << ", func '" << function << "'";
1257
1258 if (spec_type != function_alone)
1259 clog << ", file '" << file << "'";
1260
1261 if (spec_type == function_file_and_line)
1262 {
1263 clog << ", line ";
4a81c0d9 1264 switch (lineno_type)
7d6d0afc
JS
1265 {
1266 case ABSOLUTE:
4a81c0d9 1267 clog << linenos[0];
7d6d0afc
JS
1268 break;
1269
1270 case RELATIVE:
4a81c0d9 1271 clog << "+" << linenos[0];
7d6d0afc
JS
1272 break;
1273
f01d4ffb
BC
1274 case ENUMERATED:
1275 {
10e6fe03 1276 vector<int>::const_iterator linenos_it;
f01d4ffb
BC
1277 for (linenos_it = linenos.begin(); linenos_it != linenos.end(); ++linenos_it)
1278 {
1279 vector<int>::const_iterator range_it(linenos_it);
1280 while ((range_it+1) != linenos.end() && *range_it + 1 == *(range_it+1))
1281 ++range_it;
1282 if (linenos_it == range_it)
1283 clog << *linenos_it;
1284 else
1285 clog << *linenos_it << "-" << *range_it;
1286 if (range_it + 1 != linenos.end())
1287 clog << ",";
1288 linenos_it = range_it;
1289 }
1290 }
7d6d0afc
JS
1291 break;
1292
1293 case WILDCARD:
1294 clog << "*";
1295 break;
1296 }
1297 }
1298
1299 clog << endl;
bd2b1e68
GH
1300 }
1301
7d6d0afc
JS
1302 return;
1303
1304bad:
dc09353a 1305 throw SEMANTIC_ERROR(_F("malformed specification '%s'", spec.c_str()),
7d6d0afc 1306 base_probe->tok);
bd2b1e68
GH
1307}
1308
05fb3e0c
WF
1309string path_remove_sysroot(const systemtap_session& sess, const string& path)
1310{
1311 size_t pos;
1312 string retval = path;
1313 if (!sess.sysroot.empty() &&
1314 (pos = retval.find(sess.sysroot)) != string::npos)
1315 retval.replace(pos, sess.sysroot.length(), "/");
1316 return retval;
1317}
bd2b1e68 1318
36f9dd1d 1319void
1ffb8bd1 1320dwarf_query::add_probe_point(const string& dw_funcname,
b20febf3 1321 const char* filename,
36f9dd1d 1322 int line,
b20febf3 1323 Dwarf_Die* scope_die,
36f9dd1d
FCE
1324 Dwarf_Addr addr)
1325{
b20febf3 1326 string reloc_section; // base section for relocation purposes
27646582 1327 Dwarf_Addr reloc_addr; // relocated
b20febf3 1328 const string& module = dw.module_name; // "kernel" or other
1ffb8bd1 1329 string funcname = dw_funcname;
36f9dd1d 1330
37ebca01
FCE
1331 assert (! has_absolute); // already handled in dwarf_builder::build()
1332
f4faaf86 1333 reloc_addr = dw.relocate_address(addr, reloc_section);
2930abc7 1334
1ffb8bd1 1335 // If we originally used the linkage name, then let's call it that way
1ffb8bd1 1336 const char* linkage_name;
91bb9081 1337 if (!null_die(scope_die) && startswith (this->function, "_Z")
f450a7e3 1338 && (linkage_name = dwarf_linkage_name (scope_die)))
1ffb8bd1
JS
1339 funcname = linkage_name;
1340
7f9f3386
FCE
1341 if (sess.verbose > 1)
1342 {
b530b5b3 1343 clog << _("probe ") << funcname << "@" << filename << ":" << line;
b20febf3 1344 if (string(module) == TOK_KERNEL)
b530b5b3 1345 clog << _(" kernel");
91af0778 1346 else if (has_module)
b530b5b3 1347 clog << _(" module=") << module;
91af0778 1348 else if (has_process)
b530b5b3 1349 clog << _(" process=") << module;
b20febf3 1350 if (reloc_section != "") clog << " reloc=" << reloc_section;
b20febf3 1351 clog << " pc=0x" << hex << addr << dec;
7f9f3386 1352 }
4baf0e53 1353
2f18e69b
JL
1354 dwflpp::blacklisted_type blacklisted = dw.blacklisted_p (funcname, filename,
1355 line, module, addr,
1356 has_return);
b20febf3
FCE
1357 if (sess.verbose > 1)
1358 clog << endl;
7f9f3386 1359
84048984
FCE
1360 if (module == TOK_KERNEL)
1361 {
1362 // PR 4224: adapt to relocatable kernel by subtracting the _stext address here.
1363 reloc_addr = addr - sess.sym_stext;
37ebca01 1364 reloc_section = "_stext"; // a message to runtime's _stp_module_relocate
84048984
FCE
1365 }
1366
2f18e69b 1367 if (!blacklisted)
b20febf3 1368 {
1a0dbc5a 1369 sess.unwindsym_modules.insert (module);
6d0f3f0c
FCE
1370
1371 if (has_process)
1372 {
05fb3e0c 1373 string module_tgt = path_remove_sysroot(sess, module);
6d0f3f0c 1374 results.push_back (new uprobe_derived_probe(funcname, filename, line,
05fb3e0c 1375 module_tgt, reloc_section, addr, reloc_addr,
6d0f3f0c
FCE
1376 *this, scope_die));
1377 }
1378 else
1379 {
1380 assert (has_kernel || has_module);
1381 results.push_back (new dwarf_derived_probe(funcname, filename, line,
06aca46a 1382 module, reloc_section, addr, reloc_addr,
6d0f3f0c
FCE
1383 *this, scope_die));
1384 }
b20febf3 1385 }
2f18e69b
JL
1386 else
1387 {
1388 switch (blacklisted)
1389 {
1390 case dwflpp::blacklisted_section:
1391 sess.print_warning(_F("function %s is in blacklisted section",
1392 funcname.c_str()), base_probe->tok);
1393 break;
1394 case dwflpp::blacklisted_kprobes:
1395 sess.print_warning(_F("kprobes function %s is blacklisted",
1396 funcname.c_str()), base_probe->tok);
1397 break;
1398 case dwflpp::blacklisted_function_return:
1399 sess.print_warning(_F("function %s return probe is blacklisted",
1400 funcname.c_str()), base_probe->tok);
1401 break;
1402 case dwflpp::blacklisted_file:
1403 sess.print_warning(_F("function %s is in blacklisted file",
1404 funcname.c_str()), base_probe->tok);
1405 break;
1406 case dwflpp::blacklisted_function:
1407 default:
1408 sess.print_warning(_F("function %s is blacklisted",
1409 funcname.c_str()), base_probe->tok);
1410 break;
1411 }
1412 }
2930abc7
FCE
1413}
1414
613c8675
JL
1415void
1416dwarf_query::mount_well_formed_probe_point()
1417{
1418 string module = dw.module_name;
1419 if (has_process)
1420 module = path_remove_sysroot(sess, module);
1421
1422 vector<probe_point::component*> comps;
1423 vector<probe_point::component*>::iterator it;
1424 for (it = base_loc->components.begin();
1425 it != base_loc->components.end(); ++it)
1426 {
1427 if ((*it)->functor == TOK_PROCESS || (*it)->functor == TOK_MODULE)
1428 comps.push_back(new probe_point::component((*it)->functor,
1429 new literal_string(has_library ? path : module)));
1430 else
1431 comps.push_back(*it);
1432 }
1433
1434 probe_point *pp = new probe_point(*base_loc);
1435 pp->well_formed = true;
1436 pp->components = comps;
1437
1438 previous_bases.push(make_pair(base_loc, base_probe));
1439
1440 base_loc = pp;
1441 base_probe = new probe(base_probe, pp);
1442}
1443
1444void
1445dwarf_query::unmount_well_formed_probe_point()
1446{
1447 assert(!previous_bases.empty());
1448
1449 base_loc = previous_bases.top().first;
1450 base_probe = previous_bases.top().second;
1451
1452 previous_bases.pop();
1453}
1454
71dfce11
JL
1455void
1456dwarf_query::replace_probe_point_component_arg(const string& functor,
1457 const string& new_functor,
1458 int64_t new_arg,
1459 bool hex)
1460{
1461 // only allow these operations if we're editing the well-formed loc
1462 assert(!previous_bases.empty());
1463
1464 vector<probe_point::component*>::iterator it;
1465 for (it = base_loc->components.begin();
1466 it != base_loc->components.end(); ++it)
1467 if ((*it)->functor == functor)
1468 *it = new probe_point::component(new_functor,
1469 new literal_number(new_arg, hex));
1470}
1471
1472void
1473dwarf_query::replace_probe_point_component_arg(const string& functor,
1474 int64_t new_arg,
1475 bool hex)
1476{
1477 replace_probe_point_component_arg(functor, functor, new_arg, hex);
1478}
1479
1480void
1481dwarf_query::replace_probe_point_component_arg(const string& functor,
1482 const string& new_functor,
1483 const string& new_arg)
1484{
1485 // only allow these operations if we're editing the well-formed loc
1486 assert(!previous_bases.empty());
1487
1488 vector<probe_point::component*>::iterator it;
1489 for (it = base_loc->components.begin();
1490 it != base_loc->components.end(); ++it)
1491 if ((*it)->functor == functor)
1492 *it = new probe_point::component(new_functor,
1493 new literal_string(new_arg));
1494}
1495
1496void
1497dwarf_query::replace_probe_point_component_arg(const string& functor,
1498 const string& new_arg)
1499{
1500 replace_probe_point_component_arg(functor, functor, new_arg);
1501}
1502
1503void
1504dwarf_query::remove_probe_point_component(const string& functor)
1505{
1506 // only allow these operations if we're editing the well-formed loc
1507 assert(!previous_bases.empty());
1508
1509 vector<probe_point::component*> new_comps;
1510 vector<probe_point::component*>::iterator it;
1511 for (it = base_loc->components.begin();
1512 it != base_loc->components.end(); ++it)
1513 if ((*it)->functor != functor)
1514 new_comps.push_back(*it);
1515
1516 base_loc->components = new_comps;
2930abc7
FCE
1517}
1518
5f0a03a6
JK
1519enum dbinfo_reqt
1520dwarf_query::assess_dbinfo_reqt()
1521{
1522 if (has_absolute)
1523 {
1524 // kernel.statement(NUM).absolute
1525 return dbr_none;
1526 }
c31add50 1527 if (has_inline || has_label || has_callee || has_callees_num)
5f0a03a6
JK
1528 {
1529 // kernel.function("f").inline or module("m").function("f").inline
1530 return dbr_need_dwarf;
1531 }
1532 if (has_function_str && spec_type == function_alone)
1533 {
1534 // kernel.function("f") or module("m").function("f")
1535 return dbr_need_symtab;
1536 }
1537 if (has_statement_num)
1538 {
1539 // kernel.statement(NUM) or module("m").statement(NUM)
1540 // Technically, all we need is the module offset (or _stext, for
1541 // the kernel). But for that we need either the ELF file or (for
1542 // _stext) the symbol table. In either case, the symbol table
1543 // is available, and that allows us to map the NUM (address)
1544 // to a function, which is goodness.
1545 return dbr_need_symtab;
1546 }
1547 if (has_function_num)
1548 {
1549 // kernel.function(NUM) or module("m").function(NUM)
1550 // Need the symbol table so we can back up from NUM to the
1551 // start of the function.
1552 return dbr_need_symtab;
1553 }
1554 // Symbol table tells us nothing about source files or line numbers.
1555 return dbr_need_dwarf;
1556}
2930abc7 1557
e772a6e7
JL
1558string
1559dwarf_query::final_function_name(const string& final_func,
79954c0f 1560 const char* final_file,
e772a6e7
JL
1561 int final_line)
1562{
1563 string final_name = final_func;
79954c0f 1564 if (final_file && *final_file != '\0')
e772a6e7 1565 {
79954c0f 1566 final_name += ("@" + string(final_file));
e772a6e7
JL
1567 if (final_line > 0)
1568 final_name += (":" + lex_cast(final_line));
1569 }
1570 return final_name;
1571}
2930abc7 1572
c5142c66
JL
1573bool
1574dwarf_query::is_fully_specified_function()
1575{
1576 // A fully specified function is one that was given using a .function() probe
1577 // by full name (no wildcards), and specific srcfile and decl_line.
1578 return (has_function_str
1579 && spec_type == function_file_and_line
1580 && !dw.name_has_wildcard(function)
1581 && filtered_srcfiles.size() == 1
1582 && !filtered_functions.empty()
1583 && lineno_type == ABSOLUTE
1584 && filtered_functions[0].decl_line == linenos[0]);
1585}
1586
959ccfed
JL
1587base_func_info_map_t
1588dwarf_query::filtered_all(void)
1589{
c5142c66 1590 base_func_info_map_t r;
959ccfed
JL
1591 func_info_map_t::const_iterator f;
1592 for (f = filtered_functions.begin();
1593 f != filtered_functions.end(); ++f)
1594 r.push_back(*f);
1595 inline_instance_map_t::const_iterator i;
1596 for (i = filtered_inlines.begin();
1597 i != filtered_inlines.end(); ++i)
1598 r.push_back(*i);
1599 return r;
1600}
1601
b8da0ad1
FCE
1602// The critical determining factor when interpreting a pattern
1603// string is, perhaps surprisingly: "presence of a lineno". The
1604// presence of a lineno changes the search strategy completely.
1605//
1606// Compare the two cases:
1607//
1608// 1. {statement,function}(foo@file.c:lineno)
1609// - find the files matching file.c
1610// - in each file, find the functions matching foo
1611// - query the file for line records matching lineno
1612// - iterate over the line records,
1613// - and iterate over the functions,
1614// - if(haspc(function.DIE, line.addr))
1615// - if looking for statements: probe(lineno.addr)
1616// - if looking for functions: probe(function.{entrypc,return,etc.})
1617//
1618// 2. {statement,function}(foo@file.c)
1619// - find the files matching file.c
1620// - in each file, find the functions matching foo
1621// - probe(function.{entrypc,return,etc.})
1622//
1623// Thus the first decision we make is based on the presence of a
1624// lineno, and we enter entirely different sets of callbacks
1625// depending on that decision.
1626//
1627// Note that the first case is a generalization fo the second, in that
1628// we could theoretically search through line records for matching
1629// file names (a "table scan" in rdbms lingo). Luckily, file names
1630// are already cached elsewhere, so we can do an "index scan" as an
1631// optimization.
7e1279ea 1632
bd2b1e68 1633static void
4cd232e4 1634query_statement (string const & func,
20e4a32c 1635 char const * file,
4cd232e4 1636 int line,
bcc12710 1637 Dwarf_Die *scope_die,
20e4a32c 1638 Dwarf_Addr stmt_addr,
4cd232e4 1639 dwarf_query * q)
bd2b1e68 1640{
39bcd429
FCE
1641 try
1642 {
cee35f73 1643 q->add_probe_point(func, file ? file : "",
a9b2f3a5 1644 line, scope_die, stmt_addr);
39bcd429
FCE
1645 }
1646 catch (const semantic_error& e)
1647 {
1648 q->sess.print_error (e);
1649 }
bd2b1e68
GH
1650}
1651
6b517475
JS
1652static void
1653query_addr(Dwarf_Addr addr, dwarf_query *q)
1654{
7a71819c
JL
1655 assert(q->has_function_num || q->has_statement_num);
1656
6b517475
JS
1657 dwflpp &dw = q->dw;
1658
08d1d520
MW
1659 if (q->sess.verbose > 2)
1660 clog << "query_addr 0x" << hex << addr << dec << endl;
6b517475
JS
1661
1662 // First pick which CU contains this address
1663 Dwarf_Die* cudie = dw.query_cu_containing_address(addr);
1664 if (!cudie) // address could be wildly out of range
1665 return;
1666 dw.focus_on_cu(cudie);
1667
1668 // Now compensate for the dw bias
1669 addr -= dw.module_bias;
1670
1671 // Per PR5787, we look up the scope die even for
1672 // statement_num's, for blacklist sensitivity and $var
1673 // resolution purposes.
1674
1675 // Find the scopes containing this address
1676 vector<Dwarf_Die> scopes = dw.getscopes(addr);
1677 if (scopes.empty())
1678 return;
1679
1680 // Look for the innermost containing function
1681 Dwarf_Die *fnscope = NULL;
1682 for (size_t i = 0; i < scopes.size(); ++i)
1683 {
1684 int tag = dwarf_tag(&scopes[i]);
1685 if ((tag == DW_TAG_subprogram && !q->has_inline) ||
1686 (tag == DW_TAG_inlined_subroutine &&
4bda987e 1687 !q->has_call && !q->has_return && !q->has_exported))
6b517475
JS
1688 {
1689 fnscope = &scopes[i];
1690 break;
1691 }
1692 }
1693 if (!fnscope)
1694 return;
1695 dw.focus_on_function(fnscope);
1696
1697 Dwarf_Die *scope = q->has_function_num ? fnscope : &scopes[0];
1698
1699 const char *file = dwarf_decl_file(fnscope);
1700 int line;
1701 dwarf_decl_line(fnscope, &line);
1702
1703 // Function probes should reset the addr to the function entry
1704 // and possibly perform prologue searching
1705 if (q->has_function_num)
1706 {
1bbf3f90
JS
1707 if (!dw.die_entrypc(fnscope, &addr))
1708 return;
6b517475 1709 if (dwarf_tag(fnscope) == DW_TAG_subprogram &&
b5b9879f
JL
1710 (q->sess.prologue_searching ||
1711 (q->has_process && !q->dw.has_valid_locs()))) // PR 6871 && PR 6941
6b517475
JS
1712 {
1713 func_info func;
1714 func.die = *fnscope;
1715 func.name = dw.function_name;
1716 func.decl_file = file;
1717 func.decl_line = line;
1718 func.entrypc = addr;
1719
1720 func_info_map_t funcs(1, func);
1721 dw.resolve_prologue_endings (funcs);
5a617dc6
JL
1722 q->prologue_end = funcs[0].prologue_end;
1723
1724 // PR13200: if it's a .return probe, we need to emit a *retprobe based
1725 // on the entrypc so here we only use prologue_end for non .return
1726 // probes (note however that .return probes still take advantage of
1727 // prologue_end: PR14436)
1728 if (!q->has_return)
1729 addr = funcs[0].prologue_end;
6b517475
JS
1730 }
1731 }
1732 else
1733 {
de797211
JL
1734 Dwarf_Line *address_line = dwarf_getsrc_die(cudie, addr);
1735 Dwarf_Addr address_line_addr = addr;
6b517475
JS
1736 if (address_line)
1737 {
bf5974d6
JL
1738 file = DWARF_LINESRC(address_line);
1739 line = DWARF_LINENO(address_line);
1740 address_line_addr = DWARF_LINEADDR(address_line);
6b517475
JS
1741 }
1742
1743 // Verify that a raw address matches the beginning of a
1744 // statement. This is a somewhat lame check that the address
1745 // is at the start of an assembly instruction. Mark probes are in the
1746 // middle of a macro and thus not strictly at a statement beginning.
1747 // Guru mode may override this check.
de797211 1748 if (!q->has_mark && (!address_line || address_line_addr != addr))
6b517475
JS
1749 {
1750 stringstream msg;
2a97f50b 1751 msg << _F("address %#" PRIx64 " does not match the beginning of a statement",
b530b5b3 1752 addr);
6b517475 1753 if (address_line)
de797211 1754 msg << _F(" (try %#" PRIx64 ")", address_line_addr);
6b517475 1755 else
b530b5b3
LB
1756 msg << _F(" (no line info found for '%s', in module '%s')",
1757 dw.cu_name().c_str(), dw.module_name.c_str());
6b517475 1758 if (! q->sess.guru_mode)
dc09353a 1759 throw SEMANTIC_ERROR(msg.str());
2713ea24 1760 else
6b517475
JS
1761 q->sess.print_warning(msg.str());
1762 }
1763 }
1764
7a71819c
JL
1765 // We're ready to build a probe, but before, we need to create the final,
1766 // well-formed version of this location with all the components filled in
1767 q->mount_well_formed_probe_point();
1768 q->replace_probe_point_component_arg(TOK_FUNCTION, addr, true /* hex */ );
1769 q->replace_probe_point_component_arg(TOK_STATEMENT, addr, true /* hex */ );
1770
6b517475
JS
1771 // Build a probe at this point
1772 query_statement(dw.function_name, file, line, scope, addr, q);
7a71819c
JL
1773
1774 q->unmount_well_formed_probe_point();
6b517475
JS
1775}
1776
f4faaf86
JS
1777static void
1778query_plt_statement(dwarf_query *q)
1779{
1780 assert (q->has_plt && q->has_statement_num);
1781
1782 Dwarf_Addr addr = q->statement_num_val;
1783 if (q->sess.verbose > 2)
1784 clog << "query_plt_statement 0x" << hex << addr << dec << endl;
1785
1786 // First adjust the raw address to dwfl's elf bias.
1787 Dwarf_Addr elf_bias;
1788 Elf *elf = dwfl_module_getelf (q->dw.module, &elf_bias);
1789 assert(elf);
1790 addr += elf_bias;
1791
1792 // Now compensate for the dw bias
1793 q->dw.get_module_dwarf(false, false);
1794 addr -= q->dw.module_bias;
1795
93b44c2a
JL
1796 // Create the final well-formed probe point
1797 q->mount_well_formed_probe_point();
1798 q->replace_probe_point_component_arg(TOK_STATEMENT, q->statement_num_val, true /* hex */ );
1799
1800 // We remove the .plt part here, since if the user provided a .plt probe, then
1801 // the higher-level probe point is already well-formed. On the other hand, if
1802 // the user provides a .plt(PATTERN).statement(0xABCD), the PATTERN is
1803 // irrelevant (we won't iterate over plts) so just take it out.
1804 q->remove_probe_point_component(TOK_PLT);
1805
f4faaf86
JS
1806 // Build a probe at this point
1807 query_statement(q->plt_val, NULL, -1, NULL, addr, q);
93b44c2a
JL
1808
1809 q->unmount_well_formed_probe_point();
f4faaf86
JS
1810}
1811
8096dd7d 1812static void
600d39f1 1813query_label (const base_func_info& func,
8096dd7d
JS
1814 char const * label,
1815 char const * file,
1816 int line,
1817 Dwarf_Die *scope_die,
1818 Dwarf_Addr stmt_addr,
1819 dwarf_query * q)
1820{
6b517475
JS
1821 assert (q->has_statement_str || q->has_function_str);
1822
8096dd7d
JS
1823 // weed out functions whose decl_file isn't one of
1824 // the source files that we actually care about
6b517475 1825 if (q->spec_type != function_alone &&
8096dd7d
JS
1826 q->filtered_srcfiles.count(file) == 0)
1827 return;
1828
cdabbe86 1829 // Create the final well-formed probe
600d39f1 1830 string canon_func = q->final_function_name(func.name, file, line);
9891c543 1831
cdabbe86
JL
1832 q->mount_well_formed_probe_point();
1833 q->replace_probe_point_component_arg(TOK_FUNCTION, canon_func);
1834 q->replace_probe_point_component_arg(TOK_LABEL, label);
8096dd7d 1835
600d39f1 1836 query_statement(func.name, file, line, scope_die, stmt_addr, q);
9891c543 1837
cdabbe86 1838 q->unmount_well_formed_probe_point();
8096dd7d
JS
1839}
1840
67959c62 1841static void
e772a6e7
JL
1842query_callee (base_func_info& callee,
1843 base_func_info& caller,
67959c62
JL
1844 stack<Dwarf_Addr> *callers,
1845 dwarf_query * q)
1846{
1847 assert (q->has_function_str);
e772a6e7
JL
1848 assert (q->has_callee || q->has_callees_num);
1849
1850 // OK, we found a callee for a targeted caller. To help users see the
04a89a2a 1851 // derivation, we add the well-formed form .function(caller).callee(callee).
e772a6e7 1852
e772a6e7
JL
1853 string canon_caller = q->final_function_name(caller.name, caller.decl_file,
1854 caller.decl_line);
1855 string canon_callee = q->final_function_name(callee.name, callee.decl_file,
1856 callee.decl_line);
1857
04a89a2a
JL
1858 q->mount_well_formed_probe_point();
1859 q->replace_probe_point_component_arg(TOK_FUNCTION, canon_caller);
1860 q->replace_probe_point_component_arg(TOK_CALLEES, TOK_CALLEE, canon_callee);
1861 q->replace_probe_point_component_arg(TOK_CALLEE, canon_callee);
e772a6e7
JL
1862
1863 // Pass on the callers we'll need to add checks for
04a89a2a 1864 q->callers = callers;
e772a6e7
JL
1865
1866 query_statement(callee.name, callee.decl_file, callee.decl_line,
04a89a2a
JL
1867 &callee.die, callee.entrypc, q);
1868
1869 q->unmount_well_formed_probe_point();
67959c62
JL
1870}
1871
7e1279ea 1872static void
3e961ba6 1873query_inline_instance_info (inline_instance_info & ii,
7e1279ea
FCE
1874 dwarf_query * q)
1875{
b6581717 1876 try
7e1279ea 1877 {
8f14e444 1878 assert (! q->has_return); // checked by caller already
8debd4e7
JL
1879 assert (q->has_function_str || q->has_statement_str);
1880
8f14e444 1881 if (q->sess.verbose>2)
b530b5b3
LB
1882 clog << _F("querying entrypc %#" PRIx64 " of instance of inline '%s'\n",
1883 ii.entrypc, ii.name.c_str());
8debd4e7
JL
1884
1885 string canon_func = q->final_function_name(ii.name, ii.decl_file,
1886 ii.decl_line);
1887
1888 q->mount_well_formed_probe_point();
1889 q->replace_probe_point_component_arg(TOK_FUNCTION, canon_func);
1890 q->replace_probe_point_component_arg(TOK_STATEMENT, canon_func);
1891
8f14e444
FCE
1892 query_statement (ii.name, ii.decl_file, ii.decl_line,
1893 &ii.die, ii.entrypc, q);
8debd4e7
JL
1894
1895 q->unmount_well_formed_probe_point();
7e1279ea 1896 }
b6581717 1897 catch (semantic_error &e)
7e1279ea 1898 {
b6581717 1899 q->sess.print_error (e);
7e1279ea
FCE
1900 }
1901}
1902
1903static void
1904query_func_info (Dwarf_Addr entrypc,
bcc12710 1905 func_info & fi,
7e1279ea
FCE
1906 dwarf_query * q)
1907{
e5745975
JL
1908 assert(q->has_function_str || q->has_statement_str);
1909
b6581717 1910 try
7e1279ea 1911 {
e5745975
JL
1912 string canon_func = q->final_function_name(fi.name, fi.decl_file,
1913 fi.decl_line);
1914
1915 q->mount_well_formed_probe_point();
1916 q->replace_probe_point_component_arg(TOK_FUNCTION, canon_func);
1917 q->replace_probe_point_component_arg(TOK_STATEMENT, canon_func);
1918
5a617dc6
JL
1919 // If it's a .return probe, we need to emit a *retprobe based on the
1920 // entrypc (PR13200). Note however that if prologue_end is valid,
1921 // dwarf_derived_probe will still take advantage of it by creating a new
1922 // probe there if necessary to pick up target vars (PR14436).
1923 if (fi.prologue_end == 0 || q->has_return)
1924 {
170ccf10 1925 q->prologue_end = fi.prologue_end;
5a617dc6
JL
1926 query_statement (fi.name, fi.decl_file, fi.decl_line,
1927 &fi.die, entrypc, q);
1928 }
b6581717 1929 else
5a617dc6 1930 {
5a617dc6
JL
1931 query_statement (fi.name, fi.decl_file, fi.decl_line,
1932 &fi.die, fi.prologue_end, q);
1933 }
e5745975
JL
1934
1935 q->unmount_well_formed_probe_point();
7e1279ea 1936 }
b6581717 1937 catch (semantic_error &e)
7e1279ea 1938 {
b6581717 1939 q->sess.print_error (e);
7e1279ea
FCE
1940 }
1941}
1942
7e1279ea 1943static void
de797211 1944query_srcfile_line (Dwarf_Addr addr, int lineno, dwarf_query * q)
7e1279ea 1945{
8fb810d7
JL
1946 assert (q->has_statement_str || q->has_function_str);
1947 assert (q->spec_type == function_file_and_line);
20e4a32c 1948
959ccfed
JL
1949 base_func_info_map_t bfis = q->filtered_all();
1950 base_func_info_map_t::iterator i;
1951 for (i = bfis.begin(); i != bfis.end(); ++i)
897820ca 1952 {
3e961ba6 1953 if (q->dw.die_has_pc (i->die, addr))
959ccfed
JL
1954 {
1955 if (q->sess.verbose>3)
1956 clog << _("filtered DIE lands on srcfile\n");
1957 Dwarf_Die scope;
1958 q->dw.inner_die_containing_pc(i->die, addr, scope);
e5bc2e52
JL
1959
1960 string canon_func = q->final_function_name(i->name, i->decl_file,
1961 lineno /* NB: not i->decl_line */ );
1962
5702442a
JL
1963 if (q->has_nearest)
1964 {
1965 int lineno_nearest = q->linenos[0];
1966 if (q->lineno_type == RELATIVE)
1967 lineno_nearest += i->decl_line;
1968 string canon_func_nearest = q->final_function_name(i->name, i->decl_file,
1969 lineno_nearest);
1970 q->mount_well_formed_probe_point();
1971 q->replace_probe_point_component_arg(TOK_STATEMENT, canon_func_nearest);
1972 }
1973
e5bc2e52
JL
1974 q->mount_well_formed_probe_point();
1975 q->replace_probe_point_component_arg(TOK_FUNCTION, canon_func);
1976 q->replace_probe_point_component_arg(TOK_STATEMENT, canon_func);
1977
959ccfed
JL
1978 query_statement (i->name, i->decl_file,
1979 lineno, // NB: not q->line !
1980 &scope, addr, q);
e5bc2e52
JL
1981
1982 q->unmount_well_formed_probe_point();
5702442a
JL
1983 if (q->has_nearest)
1984 q->unmount_well_formed_probe_point();
959ccfed 1985 }
20e4a32c 1986 }
7e1279ea
FCE
1987}
1988
7fdd3e2c
JS
1989bool
1990inline_instance_info::operator<(const inline_instance_info& other) const
1991{
1992 if (entrypc != other.entrypc)
1993 return entrypc < other.entrypc;
1994
1995 if (decl_line != other.decl_line)
1996 return decl_line < other.decl_line;
1997
1998 int cmp = name.compare(other.name);
c60517ca
FCE
1999
2000 if (!cmp)
2001 {
2002 assert (decl_file);
2003 assert (other.decl_file);
2004 cmp = strcmp(decl_file, other.decl_file);
2005 }
2006
7fdd3e2c
JS
2007 return cmp < 0;
2008}
2009
2010
4fa7b22b 2011static int
3956fccb 2012query_dwarf_inline_instance (Dwarf_Die * die, dwarf_query * q)
4fa7b22b 2013{
6b517475 2014 assert (q->has_statement_str || q->has_function_str);
4bda987e 2015 assert (!q->has_call && !q->has_return && !q->has_exported);
bd2b1e68 2016
39bcd429 2017 try
7a053d3b 2018 {
b0ee93c4 2019 if (q->sess.verbose>2)
b530b5b3 2020 clog << _F("selected inline instance of %s\n", q->dw.function_name.c_str());
7e1279ea 2021
6b517475
JS
2022 Dwarf_Addr entrypc;
2023 if (q->dw.die_entrypc (die, &entrypc))
2024 {
2025 inline_instance_info inl;
2026 inl.die = *die;
2027 inl.name = q->dw.function_name;
2028 inl.entrypc = entrypc;
2029 q->dw.function_file (&inl.decl_file);
2030 q->dw.function_line (&inl.decl_line);
2031
2032 // make sure that this inline hasn't already
2033 // been matched from a different CU
2034 if (q->inline_dupes.insert(inl).second)
2035 q->filtered_inlines.push_back(inl);
2036 }
7e1279ea
FCE
2037 return DWARF_CB_OK;
2038 }
2039 catch (const semantic_error& e)
2040 {
2041 q->sess.print_error (e);
2042 return DWARF_CB_ABORT;
2043 }
2044}
bb788f9f 2045
7e1279ea 2046static int
7d007451 2047query_dwarf_func (Dwarf_Die * func, dwarf_query * q)
7e1279ea 2048{
6b517475 2049 assert (q->has_statement_str || q->has_function_str);
bb788f9f 2050
bd25380d
JS
2051 // weed out functions whose decl_file isn't one of
2052 // the source files that we actually care about
6b517475 2053 if (q->spec_type != function_alone &&
bd25380d 2054 q->filtered_srcfiles.count(dwarf_decl_file(func)?:"") == 0)
8096dd7d 2055 return DWARF_CB_OK;
bd25380d 2056
7e1279ea
FCE
2057 try
2058 {
7e1279ea
FCE
2059 q->dw.focus_on_function (func);
2060
7d6d0afc
JS
2061 if (!q->dw.function_scope_matches(q->scopes))
2062 return DWARF_CB_OK;
2063
857bdfd1
JS
2064 // make sure that this function address hasn't
2065 // already been matched under an aliased name
2066 Dwarf_Addr addr;
2067 if (!q->dw.func_is_inline() &&
2068 dwarf_entrypc(func, &addr) == 0 &&
2069 !q->alias_dupes.insert(addr).second)
2070 return DWARF_CB_OK;
2071
4bda987e 2072 if (q->dw.func_is_inline () && (! q->has_call) && (! q->has_return) && (! q->has_exported))
7e1279ea 2073 {
4bda987e 2074 if (q->sess.verbose>3)
b530b5b3 2075 clog << _F("checking instances of inline %s\n", q->dw.function_name.c_str());
4bda987e 2076 q->dw.iterate_over_inline_instances (query_dwarf_inline_instance, q);
7e1279ea 2077 }
8f14e444
FCE
2078 else if (q->dw.func_is_inline () && (q->has_return)) // PR 11553
2079 {
2080 q->inlined_non_returnable.insert (q->dw.function_name);
2081 }
396afcee 2082 else if (!q->dw.func_is_inline () && (! q->has_inline))
20e4a32c 2083 {
4bda987e
SC
2084 if (q->has_exported && !q->dw.func_is_exported ())
2085 return DWARF_CB_OK;
6b517475 2086 if (q->sess.verbose>2)
b530b5b3 2087 clog << _F("selected function %s\n", q->dw.function_name.c_str());
6b517475
JS
2088
2089 func_info func;
2090 q->dw.function_die (&func.die);
2091 func.name = q->dw.function_name;
2092 q->dw.function_file (&func.decl_file);
2093 q->dw.function_line (&func.decl_line);
2094
2095 Dwarf_Addr entrypc;
2096 if (q->dw.function_entrypc (&entrypc))
2097 {
2098 func.entrypc = entrypc;
2099 q->filtered_functions.push_back (func);
2100 }
2101 /* else this function is fully inlined, just ignore it */
7e1279ea 2102 }
39bcd429 2103 return DWARF_CB_OK;
bd2b1e68 2104 }
39bcd429 2105 catch (const semantic_error& e)
bd2b1e68 2106 {
39bcd429
FCE
2107 q->sess.print_error (e);
2108 return DWARF_CB_ABORT;
bd2b1e68 2109 }
bd2b1e68
GH
2110}
2111
2112static int
5c378838 2113query_cu (Dwarf_Die * cudie, dwarf_query * q)
bd2b1e68 2114{
6b517475
JS
2115 assert (q->has_statement_str || q->has_function_str);
2116
85007c04 2117 if (pending_interrupts) return DWARF_CB_ABORT;
7a053d3b 2118
39bcd429 2119 try
bd2b1e68 2120 {
7e1279ea 2121 q->dw.focus_on_cu (cudie);
b5d77020 2122
b0ee93c4 2123 if (false && q->sess.verbose>2)
b530b5b3
LB
2124 clog << _F("focused on CU '%s', in module '%s'\n",
2125 q->dw.cu_name().c_str(), q->dw.module_name.c_str());
d9b516ca 2126
6b517475
JS
2127 q->filtered_srcfiles.clear();
2128 q->filtered_functions.clear();
2129 q->filtered_inlines.clear();
2130
2131 // In this path, we find "abstract functions", record
2132 // information about them, and then (depending on lineno
2133 // matching) possibly emit one or more of the function's
2134 // associated addresses. Unfortunately the control of this
2135 // cannot easily be turned inside out.
2136
2137 if (q->spec_type != function_alone)
39bcd429 2138 {
6b517475
JS
2139 // If we have a pattern string with a filename, we need
2140 // to elaborate the srcfile mask in question first.
2141 q->dw.collect_srcfiles_matching (q->file, q->filtered_srcfiles);
2142
2143 // If we have a file pattern and *no* srcfile matches, there's
2144 // no need to look further into this CU, so skip.
2145 if (q->filtered_srcfiles.empty())
2146 return DWARF_CB_OK;
2147 }
e4c58386 2148
6b517475
JS
2149 // Pick up [entrypc, name, DIE] tuples for all the functions
2150 // matching the query, and fill in the prologue endings of them
2151 // all in a single pass.
5898b6e1 2152 int rc = q->dw.iterate_over_functions (query_dwarf_func, q, q->function);
6b517475
JS
2153 if (rc != DWARF_CB_OK)
2154 q->query_done = true;
2155
277a2b96
JL
2156 if (!q->filtered_functions.empty() &&
2157 !q->has_statement_str && // PR 2608
2158 (q->sess.prologue_searching ||
2159 (q->has_process && !q->dw.has_valid_locs()))) // PR 6871 && PR 6941
2160 q->dw.resolve_prologue_endings (q->filtered_functions);
6b517475 2161
600d39f1 2162 if (q->has_label)
6b517475 2163 {
600d39f1
JL
2164 enum lineno_t lineno_type = WILDCARD;
2165 if (q->spec_type == function_file_and_line)
2166 lineno_type = q->lineno_type;
959ccfed
JL
2167 base_func_info_map_t bfis = q->filtered_all();
2168 base_func_info_map_t::iterator i;
2169 for (i = bfis.begin(); i != bfis.end(); ++i)
600d39f1
JL
2170 q->dw.iterate_over_labels (&i->die, q->label_val, *i, q->linenos,
2171 lineno_type, q, query_label);
39bcd429 2172 }
67959c62
JL
2173 else if (q->has_callee || q->has_callees_num)
2174 {
2175 // .callee(str) --> str, .callees[(N)] --> "*"
2176 string callee_val = q->has_callee ? q->callee_val : "*";
2177 long callees_num_val = q->has_callees_num ? q->callees_num_val : 1;
2178
2179 // NB: We filter functions that do not match the file here rather than
2180 // in query_callee because we only want the filtering to apply to the
2181 // first level, not to callees that are recursed into if
2182 // callees_num_val > 1.
959ccfed
JL
2183 base_func_info_map_t bfis = q->filtered_all();
2184 base_func_info_map_t::iterator i;
2185 for (i = bfis.begin(); i != bfis.end(); ++i)
67959c62
JL
2186 {
2187 if (q->spec_type != function_alone &&
2188 q->filtered_srcfiles.count(i->decl_file) == 0)
2189 continue;
2190 q->dw.iterate_over_callees (&i->die, callee_val,
2191 callees_num_val,
e772a6e7 2192 q, query_callee, *i);
67959c62
JL
2193 }
2194 }
c5142c66
JL
2195 else if (q->spec_type == function_file_and_line
2196 // User specified function, file and lineno, but if they match
2197 // exactly a specific function in a specific line at a specific
2198 // decl_line, the user doesn't actually want to probe a lineno,
2199 // but rather the function itself. So let fall through to
2200 // query_func_info/query_inline_instance_info in final else.
2201 && !q->is_fully_specified_function())
600d39f1
JL
2202 {
2203 // .statement(...:NN) often gets mixed up with .function(...:NN)
2204 if (q->has_function_str)
2205 q->sess.print_warning (_("For probing a particular line, use a "
2206 ".statement() probe, not .function()"),
2207 q->base_probe->tok);
2208
2209 base_func_info_map_t bfis = q->filtered_all();
2210
2211 set<string>::const_iterator srcfile;
2212 for (srcfile = q->filtered_srcfiles.begin();
2213 srcfile != q->filtered_srcfiles.end(); ++srcfile)
2214 q->dw.iterate_over_srcfile_lines(srcfile->c_str(), q->linenos,
2215 q->lineno_type, bfis,
83eeb3ac
HK
2216 query_srcfile_line,
2217 q->has_nearest, q);
600d39f1 2218 }
6b517475
JS
2219 else
2220 {
2221 // Otherwise, simply probe all resolved functions.
2222 for (func_info_map_t::iterator i = q->filtered_functions.begin();
2223 i != q->filtered_functions.end(); ++i)
2224 query_func_info (i->entrypc, *i, q);
2225
2226 // And all inline instances (if we're not excluding inlines with ".call")
2227 if (! q->has_call)
2228 for (inline_instance_map_t::iterator i
2229 = q->filtered_inlines.begin(); i != q->filtered_inlines.end(); ++i)
2230 query_inline_instance_info (*i, q);
e772a6e7 2231 }
39bcd429 2232 return DWARF_CB_OK;
bd2b1e68 2233 }
39bcd429 2234 catch (const semantic_error& e)
bd2b1e68 2235 {
39bcd429
FCE
2236 q->sess.print_error (e);
2237 return DWARF_CB_ABORT;
bd2b1e68 2238 }
bd2b1e68
GH
2239}
2240
0ce64fb8 2241
4df79aaf
JS
2242void
2243dwarf_query::query_module_functions ()
2244{
2245 try
2246 {
2247 filtered_srcfiles.clear();
2248 filtered_functions.clear();
2249 filtered_inlines.clear();
2250
2251 // Collect all module functions so we know which CUs are interesting
2252 int rc = dw.iterate_single_function(query_dwarf_func, this, function);
2253 if (rc != DWARF_CB_OK)
2254 {
2255 query_done = true;
2256 return;
2257 }
2258
2259 set<void*> used_cus; // by cu->addr
2260 vector<Dwarf_Die> cus;
2261 Dwarf_Die cu_mem;
2262
959ccfed
JL
2263 base_func_info_map_t bfis = filtered_all();
2264 base_func_info_map_t::iterator i;
2265 for (i = bfis.begin(); i != bfis.end(); ++i)
4df79aaf
JS
2266 if (dwarf_diecu(&i->die, &cu_mem, NULL, NULL) &&
2267 used_cus.insert(cu_mem.addr).second)
2268 cus.push_back(cu_mem);
2269
2270 // Reset the dupes since we didn't actually collect them the first time
2271 alias_dupes.clear();
2272 inline_dupes.clear();
2273
2274 // Run the query again on the individual CUs
91bb9081
LB
2275 for (vector<Dwarf_Die>::iterator i = cus.begin(); i != cus.end(); ++i){
2276 rc = query_cu(&*i, this);
2277 if (rc != DWARF_CB_OK)
2278 {
2279 query_done = true;
2280 return;
2281 }
2282 }
4df79aaf
JS
2283 }
2284 catch (const semantic_error& e)
2285 {
2286 sess.print_error (e);
2287 }
2288}
2289
2290
5f0a03a6
JK
2291static void
2292validate_module_elf (Dwfl_Module *mod, const char *name, base_query *q)
2293{
2294 // Validate the machine code in this elf file against the
2295 // session machine. This is important, in case the wrong kind
2296 // of debuginfo is being automagically processed by elfutils.
2297 // While we can tell i686 apart from x86-64, unfortunately
2298 // we can't help confusing i586 vs i686 (both EM_386).
2299
2300 Dwarf_Addr bias;
2301 // We prefer dwfl_module_getdwarf to dwfl_module_getelf here,
2302 // because dwfl_module_getelf can force costly section relocations
2303 // we don't really need, while either will do for this purpose.
2304 Elf* elf = (dwarf_getelf (dwfl_module_getdwarf (mod, &bias))
2305 ?: dwfl_module_getelf (mod, &bias));
2306
2307 GElf_Ehdr ehdr_mem;
2308 GElf_Ehdr* em = gelf_getehdr (elf, &ehdr_mem);
e1c3b13a 2309 if (em == 0) { DWFL_ASSERT ("dwfl_getehdr", dwfl_errno()); }
d8f31d0a 2310 assert(em);
5f0a03a6
JK
2311 int elf_machine = em->e_machine;
2312 const char* debug_filename = "";
2313 const char* main_filename = "";
2314 (void) dwfl_module_info (mod, NULL, NULL,
2315 NULL, NULL, NULL,
2316 & main_filename,
2317 & debug_filename);
2318 const string& sess_machine = q->sess.architecture;
756c9462
FCE
2319
2320 string expect_machine; // to match sess.machine (i.e., kernel machine)
2321 string expect_machine2;
5f0a03a6 2322
d27e6fd5 2323 // NB: See also the 'uname -m' squashing done in main.cxx.
5f0a03a6
JK
2324 switch (elf_machine)
2325 {
756c9462
FCE
2326 // x86 and ppc are bi-architecture; a 64-bit kernel
2327 // can normally run either 32-bit or 64-bit *userspace*.
2328 case EM_386:
2329 expect_machine = "i?86";
2330 if (! q->has_process) break; // 32-bit kernel/module
2331 /* FALLSTHROUGH */
2332 case EM_X86_64:
2333 expect_machine2 = "x86_64";
2334 break;
2335 case EM_PPC:
756c9462 2336 case EM_PPC64:
5a1c472e 2337 expect_machine = "powerpc";
756c9462 2338 break;
3fe7d888 2339 case EM_S390: expect_machine = "s390"; break;
5f0a03a6 2340 case EM_IA_64: expect_machine = "ia64"; break;
d27e6fd5 2341 case EM_ARM: expect_machine = "arm*"; break;
86aae990 2342 case EM_AARCH64: expect_machine = "arm64"; break;
5f0a03a6
JK
2343 // XXX: fill in some more of these
2344 default: expect_machine = "?"; break;
2345 }
2346
2347 if (! debug_filename) debug_filename = main_filename;
2348 if (! debug_filename) debug_filename = name;
2349
756c9462
FCE
2350 if (fnmatch (expect_machine.c_str(), sess_machine.c_str(), 0) != 0 &&
2351 fnmatch (expect_machine2.c_str(), sess_machine.c_str(), 0) != 0)
5f0a03a6
JK
2352 {
2353 stringstream msg;
b530b5b3
LB
2354 msg << _F("ELF machine %s|%s (code %d) mismatch with target %s in '%s'",
2355 expect_machine.c_str(), expect_machine2.c_str(), elf_machine,
2356 sess_machine.c_str(), debug_filename);
dc09353a 2357 throw SEMANTIC_ERROR(msg.str ());
5f0a03a6
JK
2358 }
2359
b57082de 2360 if (q->sess.verbose>1)
b78a0fbb 2361 clog << _F("focused on module '%s' = [%#" PRIx64 "-%#" PRIx64 ", bias %#" PRIx64
b530b5b3
LB
2362 " file %s ELF machine %s|%s (code %d)\n",
2363 q->dw.module_name.c_str(), q->dw.module_start, q->dw.module_end,
2364 q->dw.module_bias, debug_filename, expect_machine.c_str(),
2365 expect_machine2.c_str(), elf_machine);
5f0a03a6 2366}
1d3a40b6 2367
91af0778
FCE
2368
2369
2370static Dwarf_Addr
2371lookup_symbol_address (Dwfl_Module *m, const char* wanted)
2372{
2373 int syments = dwfl_module_getsymtab(m);
2374 assert(syments);
2375 for (int i = 1; i < syments; ++i)
2376 {
2377 GElf_Sym sym;
2378 const char *name = dwfl_module_getsym(m, i, &sym, NULL);
2379 if (name != NULL && strcmp(name, wanted) == 0)
2380 return sym.st_value;
2381 }
2382
2383 return 0;
2384}
2385
2386
2387
bd2b1e68 2388static int
b8da0ad1 2389query_module (Dwfl_Module *mod,
91af0778 2390 void **,
b8da0ad1 2391 const char *name,
6f4c1275 2392 Dwarf_Addr addr,
06de3a04 2393 base_query *q)
bd2b1e68 2394{
39bcd429 2395 try
e38d6504 2396 {
91af0778
FCE
2397 module_info* mi = q->sess.module_cache->cache[name];
2398 if (mi == 0)
2399 {
2400 mi = q->sess.module_cache->cache[name] = new module_info(name);
2401
6f4c1275
FCE
2402 mi->mod = mod;
2403 mi->addr = addr;
91af0778 2404
6f4c1275
FCE
2405 const char* debug_filename = "";
2406 const char* main_filename = "";
2407 (void) dwfl_module_info (mod, NULL, NULL,
2408 NULL, NULL, NULL,
2409 & main_filename,
2410 & debug_filename);
2411
ab3ed72d 2412 if (debug_filename || main_filename)
91af0778 2413 {
6f4c1275
FCE
2414 mi->elf_path = debug_filename ?: main_filename;
2415 }
2416 else if (name == TOK_KERNEL)
2417 {
2418 mi->dwarf_status = info_absent;
91af0778 2419 }
91af0778
FCE
2420 }
2421 // OK, enough of that module_info caching business.
2422
5f0a03a6 2423 q->dw.focus_on_module(mod, mi);
d9b516ca 2424
39bcd429
FCE
2425 // If we have enough information in the pattern to skip a module and
2426 // the module does not match that information, return early.
b8da0ad1 2427 if (!q->dw.module_name_matches(q->module_val))
85007c04 2428 return pending_interrupts ? DWARF_CB_ABORT : DWARF_CB_OK;
0cbbf9d1
FCE
2429
2430 // Don't allow module("*kernel*") type expressions to match the
2431 // elfutils module "kernel", which we refer to in the probe
2432 // point syntax exclusively as "kernel.*".
2433 if (q->dw.module_name == TOK_KERNEL && ! q->has_kernel)
85007c04 2434 return pending_interrupts ? DWARF_CB_ABORT : DWARF_CB_OK;
b5d77020 2435
5f0a03a6
JK
2436 if (mod)
2437 validate_module_elf(mod, name, q);
2438 else
91af0778
FCE
2439 assert(q->has_kernel); // and no vmlinux to examine
2440
2441 if (q->sess.verbose>2)
b530b5b3 2442 cerr << _F("focused on module '%s'\n", q->dw.module_name.c_str());
91af0778
FCE
2443
2444
2445 // Collect a few kernel addresses. XXX: these belong better
2446 // to the sess.module_info["kernel"] struct.
2447 if (q->dw.module_name == TOK_KERNEL)
c931ec8a 2448 {
91af0778
FCE
2449 if (! q->sess.sym_kprobes_text_start)
2450 q->sess.sym_kprobes_text_start = lookup_symbol_address (mod, "__kprobes_text_start");
2451 if (! q->sess.sym_kprobes_text_end)
2452 q->sess.sym_kprobes_text_end = lookup_symbol_address (mod, "__kprobes_text_end");
2453 if (! q->sess.sym_stext)
2454 q->sess.sym_stext = lookup_symbol_address (mod, "_stext");
c931ec8a
FCE
2455 }
2456
dbe9d133
JL
2457 // If there is a .library component, then q->path will hold the path to
2458 // the executable if the library was fully resolved. If not (e.g. not
2459 // absolute, or globby), resort to iterate_over_libraries().
2460 if (q->has_library && q->path.empty())
84c84ac4 2461 q->dw.iterate_over_libraries (&q->query_library_callback, q);
576eaefe
SC
2462 // .plt is translated to .plt.statement(N). We only want to iterate for the
2463 // .plt case
2464 else if (q->has_plt && ! q->has_statement)
070764c0
JL
2465 {
2466 q->dw.iterate_over_plt (q, &q->query_plt_callback);
2467 q->visited_modules.insert(name);
2468 }
84c84ac4 2469 else
070764c0
JL
2470 {
2471 // search the module for matches of the probe point.
2472 q->handle_query_module();
2473 q->visited_modules.insert(name);
2474 }
bb788f9f 2475
b8da0ad1 2476 // If we know that there will be no more matches, abort early.
85007c04 2477 if (q->dw.module_name_final_match(q->module_val) || pending_interrupts)
b8da0ad1
FCE
2478 return DWARF_CB_ABORT;
2479 else
2480 return DWARF_CB_OK;
7a053d3b 2481 }
39bcd429 2482 catch (const semantic_error& e)
bd2b1e68 2483 {
39bcd429
FCE
2484 q->sess.print_error (e);
2485 return DWARF_CB_ABORT;
bd2b1e68 2486 }
bd2b1e68
GH
2487}
2488
35d4ab18 2489
84c84ac4 2490void
bbbc7241 2491base_query::query_library_callback (base_query *me, const char *data)
84c84ac4 2492{
5d5bd369 2493 me->query_library (data);
84c84ac4
SC
2494}
2495
2496
614f0fcf
JL
2497probe*
2498build_library_probe(dwflpp& dw,
2499 const string& library,
2500 probe *base_probe,
2501 probe_point *base_loc)
2502{
2503 probe_point* specific_loc = new probe_point(*base_loc);
614f0fcf
JL
2504 specific_loc->from_glob = true;
2505 vector<probe_point::component*> derived_comps;
2506
2507 // Create new probe point for the matching library. This is what will be
2508 // shown in listing mode. Also replace the process(str) with the real
2509 // absolute path rather than keeping what the user typed in.
2510 vector<probe_point::component*>::iterator it;
2511 for (it = specific_loc->components.begin();
2512 it != specific_loc->components.end(); ++it)
2513 if ((*it)->functor == TOK_PROCESS)
2514 derived_comps.push_back(new probe_point::component(TOK_PROCESS,
2515 new literal_string(path_remove_sysroot(dw.sess, dw.module_name))));
2516 else if ((*it)->functor == TOK_LIBRARY)
2517 derived_comps.push_back(new probe_point::component(TOK_LIBRARY,
2518 new literal_string(path_remove_sysroot(dw.sess, library))));
2519 else
2520 derived_comps.push_back(*it);
2521 probe_point* derived_loc = new probe_point(*specific_loc);
2522 derived_loc->components = derived_comps;
2523 return new probe (new probe (base_probe, specific_loc), derived_loc);
2524}
2525
2526bool
51d6bda3
SC
2527query_one_library (const char *library, dwflpp & dw,
2528 const string user_lib, probe * base_probe, probe_point *base_loc,
2529 vector<derived_probe *> & results)
84c84ac4 2530{
47e226ed 2531 if (dw.function_name_matches_pattern(library, "*" + user_lib))
84c84ac4 2532 {
05fb3e0c
WF
2533 string library_path = find_executable (library, "", dw.sess.sysenv,
2534 "LD_LIBRARY_PATH");
168e2ef5
JL
2535 probe *new_base = build_library_probe(dw, library_path,
2536 base_probe, base_loc);
2537
2538 // We pass true for the optional parameter of derive_probes() here to
2539 // indicate that we don't mind if the probe doesn't resolve. This is
2540 // because users expect wildcarded probe points to only apply to a subset
2541 // of matching libraries, in the sense of "any", rather than "all", just
2542 // like module("*") and process("*"). See also dwarf_builder::build().
2543 derive_probes(dw.sess, new_base, results, true /* optional */ );
84c84ac4 2544
51d6bda3 2545 if (dw.sess.verbose > 2)
2cbcfa9c 2546 clog << _("module=") << library_path << endl;
614f0fcf 2547 return true;
84c84ac4 2548 }
614f0fcf 2549 return false;
84c84ac4
SC
2550}
2551
2552
51d6bda3
SC
2553void
2554dwarf_query::query_library (const char *library)
2555{
614f0fcf
JL
2556 visited_libraries.insert(library);
2557 if (query_one_library (library, dw, user_lib, base_probe, base_loc, results))
2558 resolved_library = true;
51d6bda3
SC
2559}
2560
576eaefe
SC
2561struct plt_expanding_visitor: public var_expanding_visitor
2562{
2563 plt_expanding_visitor(const string & entry):
2564 entry (entry)
2565 {
2566 }
2567 const string & entry;
2568
2569 void visit_target_symbol (target_symbol* e);
2570};
2571
2572
2573void
45cdb40e 2574base_query::query_plt_callback (base_query *me, const char *entry, size_t address)
576eaefe 2575{
576eaefe
SC
2576 if (me->dw.function_name_matches_pattern (entry, me->plt_val))
2577 me->query_plt (entry, address);
7f4964f1 2578 me->dw.mod_info->plt_funcs.insert(entry);
576eaefe
SC
2579}
2580
2581
2582void
2583query_one_plt (const char *entry, long addr, dwflpp & dw,
2584 probe * base_probe, probe_point *base_loc,
93b44c2a 2585 vector<derived_probe *> & results, base_query *q)
576eaefe 2586{
93b44c2a
JL
2587 string module = dw.module_name;
2588 if (q->has_process)
2589 module = path_remove_sysroot(dw.sess, module);
2590
576eaefe 2591 probe_point* specific_loc = new probe_point(*base_loc);
93b44c2a
JL
2592 specific_loc->well_formed = true;
2593
576eaefe
SC
2594 vector<probe_point::component*> derived_comps;
2595
2596 if (dw.sess.verbose > 2)
2597 clog << _F("plt entry=%s\n", entry);
2598
576eaefe
SC
2599 vector<probe_point::component*>::iterator it;
2600 for (it = specific_loc->components.begin();
2601 it != specific_loc->components.end(); ++it)
93b44c2a
JL
2602 if ((*it)->functor == TOK_PROCESS)
2603 {
2604 // Replace with fully resolved path
2605 *it = new probe_point::component(TOK_PROCESS,
2606 new literal_string(q->has_library ? q->path : module));
2607 derived_comps.push_back(*it);
2608 }
2609 else if ((*it)->functor == TOK_PLT)
3d372d6b 2610 {
93b44c2a 2611 // Replace possibly globby component
8159bf55
FCE
2612 *it = new probe_point::component(TOK_PLT,
2613 new literal_string(entry));
3d372d6b
SC
2614 derived_comps.push_back(*it);
2615 derived_comps.push_back(new probe_point::component(TOK_STATEMENT,
ac7c8669 2616 new literal_number(addr, true)));
3d372d6b 2617 }
576eaefe
SC
2618 else
2619 derived_comps.push_back(*it);
2620 probe_point* derived_loc = new probe_point(*specific_loc);
2621 derived_loc->components = derived_comps;
02c34e38
FCE
2622 probe *new_base = new probe (new probe (base_probe, specific_loc),
2623 derived_loc);
576eaefe
SC
2624 string e = string(entry);
2625 plt_expanding_visitor pltv (e);
2626 pltv.replace (new_base->body);
2e96714f
SC
2627
2628 literal_map_t params;
2629 for (unsigned i = 0; i < derived_loc->components.size(); ++i)
2630 {
2631 probe_point::component *c = derived_loc->components[i];
2632 params[c->functor] = c->arg;
2633 }
2634 dwarf_query derived_q(new_base, derived_loc, dw, params, results, "", "");
2635 dw.iterate_over_modules<base_query>(&query_module, &derived_q);
576eaefe
SC
2636}
2637
2638
2639void
2640dwarf_query::query_plt (const char *entry, size_t address)
2641{
93b44c2a 2642 query_one_plt (entry, address, dw, base_probe, base_loc, results, this);
576eaefe 2643}
51d6bda3 2644
435f53a7
FCE
2645// This would more naturally fit into elaborate.cxx:semantic_pass_symbols,
2646// but the needed declaration for module_cache is not available there.
2647// Nor for that matter in session.cxx. Only in this CU is that field ever
2648// set (in query_module() above), so we clean it up here too.
2649static void
2650delete_session_module_cache (systemtap_session& s)
2651{
2652 if (s.module_cache) {
2653 if (s.verbose > 3)
b530b5b3 2654 clog << _("deleting module_cache") << endl;
435f53a7
FCE
2655 delete s.module_cache;
2656 s.module_cache = 0;
2657 }
2658}
2659
2660
de688825 2661struct dwarf_var_expanding_visitor: public var_expanding_visitor
35d4ab18 2662{
77de5e9e 2663 dwarf_query & q;
bcc12710 2664 Dwarf_Die *scope_die;
77de5e9e 2665 Dwarf_Addr addr;
8c819921 2666 block *add_block;
2260f4e3 2667 block *add_call_probe; // synthesized from .return probes with saved $vars
1630966a
JS
2668 // NB: tids are not always collected in add_block & add_call_probe, because
2669 // gen_kretprobe_saved_return doesn't need them. Thus we need these extra
2670 // *_tid bools for gen_mapped_saved_return to tell what's there.
8cc799a5 2671 bool add_block_tid, add_call_probe_tid;
af234c40
JS
2672 unsigned saved_longs, saved_strings; // data saved within kretprobes
2673 map<std::string, expression *> return_ts_map;
729455a7 2674 vector<Dwarf_Die> scopes;
3689db05 2675 // probe counter name -> pointer of associated probe
aa2c662f 2676 std::set<std::string> perf_counter_refs;
b95e2b79 2677 bool visited;
77de5e9e 2678
de688825 2679 dwarf_var_expanding_visitor(dwarf_query & q, Dwarf_Die *sd, Dwarf_Addr a):
af234c40 2680 q(q), scope_die(sd), addr(a), add_block(NULL), add_call_probe(NULL),
8cc799a5 2681 add_block_tid(false), add_call_probe_tid(false),
af234c40 2682 saved_longs(0), saved_strings(0), visited(false) {}
277c21bc 2683 expression* gen_mapped_saved_return(expression* e, const string& name);
140be17a 2684 expression* gen_kretprobe_saved_return(expression* e);
a7999c82
JS
2685 void visit_target_symbol_saved_return (target_symbol* e);
2686 void visit_target_symbol_context (target_symbol* e);
d7f3e0c5 2687 void visit_target_symbol (target_symbol* e);
bd1fcbad 2688 void visit_atvar_op (atvar_op* e);
c24447be 2689 void visit_cast_op (cast_op* e);
8cc799a5 2690 void visit_entry_op (entry_op* e);
3689db05 2691 void visit_perf_op (perf_op* e);
729455a7
JS
2692private:
2693 vector<Dwarf_Die>& getscopes(target_symbol *e);
77de5e9e
GH
2694};
2695
2696
de688825 2697unsigned var_expanding_visitor::tick = 0;
77de5e9e 2698
a50de939 2699
74fe61bc 2700var_expanding_visitor::var_expanding_visitor (): op()
a50de939
DS
2701{
2702 // FIXME: for the time being, by default we only support plain '$foo
2703 // = bar', not '+=' or any other op= variant. This is fixable, but a
2704 // bit ugly.
2705 //
2706 // If derived classes desire to add additional operator support, add
2707 // new operators to this list in the derived class constructor.
2708 valid_ops.insert ("=");
2709}
2710
2711
f55efafe
JS
2712void
2713var_expanding_visitor::provide_lvalue_call(functioncall* fcall)
2714{
2715 // Provide the functioncall to our parent, so that it can be used to
2716 // substitute for the assignment node immediately above us.
2717 assert(!target_symbol_setter_functioncalls.empty());
2718 *(target_symbol_setter_functioncalls.top()) = fcall;
2719}
2720
2721
87214add
JS
2722bool
2723var_expanding_visitor::rewrite_lvalue(const token* tok, const std::string& eop,
2724 expression*& lvalue, expression*& rvalue)
77de5e9e 2725{
e57b735a
GH
2726 // Our job would normally be to require() the left and right sides
2727 // into a new assignment. What we're doing is slightly trickier:
2728 // we're pushing a functioncall** onto a stack, and if our left
2729 // child sets the functioncall* for that value, we're going to
2730 // assume our left child was a target symbol -- transformed into a
2731 // set_target_foo(value) call, and it wants to take our right child
2732 // as the argument "value".
2733 //
2734 // This is why some people claim that languages with
2735 // constructor-decomposing case expressions have a leg up on
2736 // visitors.
2737
2738 functioncall *fcall = NULL;
d9b516ca 2739
a50de939 2740 // Let visit_target_symbol know what operator it should handle.
87214add
JS
2741 const string* old_op = op;
2742 op = &eop;
a50de939 2743
e57b735a 2744 target_symbol_setter_functioncalls.push (&fcall);
87214add 2745 replace (lvalue);
e57b735a 2746 target_symbol_setter_functioncalls.pop ();
87214add
JS
2747 replace (rvalue);
2748
2749 op = old_op;
e57b735a
GH
2750
2751 if (fcall != NULL)
77de5e9e 2752 {
e57b735a
GH
2753 // Our left child is informing us that it was a target variable
2754 // and it has been replaced with a set_target_foo() function
2755 // call; we are going to provide that function call -- with the
2756 // right child spliced in as sole argument -- in place of
de688825 2757 // ourselves, in the var expansion we're in the middle of making.
e57b735a 2758
87214add 2759 if (valid_ops.find (eop) == valid_ops.end ())
a50de939
DS
2760 {
2761 // Build up a list of supported operators.
2762 string ops;
2763 std::set<string>::iterator i;
b530b5b3 2764 int valid_ops_size = 0;
a50de939 2765 for (i = valid_ops.begin(); i != valid_ops.end(); i++)
b530b5b3 2766 {
a50de939 2767 ops += " " + *i + ",";
b530b5b3
LB
2768 valid_ops_size++;
2769 }
a50de939
DS
2770 ops.resize(ops.size() - 1); // chop off the last ','
2771
2772 // Throw the error.
dc09353a 2773 throw SEMANTIC_ERROR (_NF("Only the following assign operator is implemented on target variables: %s",
1e41115c 2774 "Only the following assign operators are implemented on target variables: %s",
52c2652f 2775 valid_ops_size, ops.c_str()), tok);
b530b5b3 2776
a50de939 2777 }
e57b735a 2778
87214add
JS
2779 assert (lvalue == fcall);
2780 if (rvalue)
2781 fcall->args.push_back (rvalue);
4ed05b15 2782 provide (fcall);
87214add 2783 return true;
77de5e9e 2784 }
e57b735a 2785 else
87214add
JS
2786 return false;
2787}
2788
2789
2790void
2791var_expanding_visitor::visit_assignment (assignment* e)
2792{
2793 if (!rewrite_lvalue (e->tok, e->op, e->left, e->right))
2794 provide (e);
2795}
2796
2797
2798void
2799var_expanding_visitor::visit_pre_crement (pre_crement* e)
2800{
2801 expression *dummy = NULL;
2802 if (!rewrite_lvalue (e->tok, e->op, e->operand, dummy))
2803 provide (e);
2804}
2805
2806
2807void
2808var_expanding_visitor::visit_post_crement (post_crement* e)
2809{
2810 expression *dummy = NULL;
2811 if (!rewrite_lvalue (e->tok, e->op, e->operand, dummy))
2812 provide (e);
2813}
2814
2815
2816void
2817var_expanding_visitor::visit_delete_statement (delete_statement* s)
2818{
2819 string fakeop = "delete";
2820 expression *dummy = NULL;
2821 if (!rewrite_lvalue (s->tok, fakeop, s->value, dummy))
2822 provide (s);
e57b735a 2823}
d9b516ca 2824
d7f3e0c5 2825
30263a73
FCE
2826void
2827var_expanding_visitor::visit_defined_op (defined_op* e)
2828{
2829 bool resolved = true;
2830
2831 defined_ops.push (e);
2832 try {
0fb0cac9 2833 replace (e->operand);
30263a73 2834
c69a87e0 2835 // NB: Formerly, we had some curious cases to consider here, depending on what
30263a73 2836 // various visit_target_symbol() implementations do for successful or
c69a87e0
FCE
2837 // erroneous resolutions. Some would signal a visit_target_symbol failure
2838 // with an exception, with a set flag within the target_symbol, or nothing
2839 // at all.
30263a73 2840 //
c69a87e0
FCE
2841 // Now, failures always have to be signalled with a
2842 // saved_conversion_error being chained to the target_symbol.
2843 // Successes have to result in an attempted rewrite of the
850bfddd 2844 // target_symbol (via provide()).
780f11ff 2845 //
c69a87e0
FCE
2846 // Edna Mode: "no capes". fche: "no exceptions".
2847
30263a73
FCE
2848 // dwarf stuff: success: rewrites to a function; failure: retains target_symbol, sets saved_conversion_error
2849 //
2850 // sdt-kprobes sdt.h: success: string or functioncall; failure: semantic_error
2851 //
2852 // sdt-uprobes: success: string or no op; failure: no op; expect derived/synthetic
2853 // dwarf probe to take care of it.
2854 // But this is rather unhelpful. So we rig the sdt_var_expanding_visitor
2855 // to pass through @defined() to the synthetic dwarf probe.
780f11ff 2856 //
30263a73
FCE
2857 // utrace: success: rewrites to function; failure: semantic_error
2858 //
850bfddd 2859 // procfs: success: rewrites to function; failure: semantic_error
30263a73 2860
0fb0cac9
JS
2861 target_symbol* tsym = dynamic_cast<target_symbol*> (e->operand);
2862 if (tsym && tsym->saved_conversion_error) // failing
30263a73 2863 resolved = false;
0fb0cac9 2864 else if (tsym) // unresolved but not marked failing
b7aedf26 2865 {
780f11ff
JS
2866 // There are some visitors that won't touch certain target_symbols,
2867 // e.g. dwarf_var_expanding_visitor won't resolve @cast. We should
2868 // leave it for now so some other visitor can have a chance.
b7aedf26
JS
2869 provide (e);
2870 return;
2871 }
30263a73
FCE
2872 else // resolved, rewritten to some other expression type
2873 resolved = true;
780f11ff 2874 } catch (const semantic_error& e) {
c69a87e0 2875 assert (0); // should not happen
30263a73
FCE
2876 }
2877 defined_ops.pop ();
2878
2879 literal_number* ln = new literal_number (resolved ? 1 : 0);
2880 ln->tok = e->tok;
2881 provide (ln);
2882}
2883
2884
5f36109e
JS
2885struct dwarf_pretty_print
2886{
2887 dwarf_pretty_print (dwflpp& dw, vector<Dwarf_Die>& scopes, Dwarf_Addr pc,
2888 const string& local, bool userspace_p,
2889 const target_symbol& e):
d19a9a82
JS
2890 dw(dw), local(local), scopes(scopes), pc(pc), pointer(NULL),
2891 userspace_p(userspace_p), deref_p(true)
5f36109e
JS
2892 {
2893 init_ts (e);
2894 dw.type_die_for_local (scopes, pc, local, ts, &base_type);
2895 }
2896
2897 dwarf_pretty_print (dwflpp& dw, Dwarf_Die *scope_die, Dwarf_Addr pc,
2898 bool userspace_p, const target_symbol& e):
d19a9a82
JS
2899 dw(dw), scopes(1, *scope_die), pc(pc), pointer(NULL),
2900 userspace_p(userspace_p), deref_p(true)
5f36109e
JS
2901 {
2902 init_ts (e);
2903 dw.type_die_for_return (&scopes[0], pc, ts, &base_type);
2904 }
2905
2906 dwarf_pretty_print (dwflpp& dw, Dwarf_Die *type_die, expression* pointer,
d19a9a82 2907 bool deref_p, bool userspace_p, const target_symbol& e):
5f36109e 2908 dw(dw), pc(0), pointer(pointer), pointer_type(*type_die),
d19a9a82 2909 userspace_p(userspace_p), deref_p(deref_p)
5f36109e
JS
2910 {
2911 init_ts (e);
2912 dw.type_die_for_pointer (type_die, ts, &base_type);
2913 }
2914
2915 functioncall* expand ();
ce83ff57 2916 ~dwarf_pretty_print () { delete ts; }
5f36109e
JS
2917
2918private:
2919 dwflpp& dw;
2920 target_symbol* ts;
7d11d8c9 2921 bool print_full;
5f36109e
JS
2922 Dwarf_Die base_type;
2923
2924 string local;
2925 vector<Dwarf_Die> scopes;
2926 Dwarf_Addr pc;
2927
2928 expression* pointer;
2929 Dwarf_Die pointer_type;
2930
d19a9a82 2931 const bool userspace_p, deref_p;
5f36109e
JS
2932
2933 void recurse (Dwarf_Die* type, target_symbol* e,
7d11d8c9 2934 print_format* pf, bool top=false);
600551ca
JS
2935 void recurse_bitfield (Dwarf_Die* type, target_symbol* e,
2936 print_format* pf);
5f36109e 2937 void recurse_base (Dwarf_Die* type, target_symbol* e,
7d11d8c9 2938 print_format* pf);
5f36109e 2939 void recurse_array (Dwarf_Die* type, target_symbol* e,
7d11d8c9 2940 print_format* pf, bool top);
5f36109e 2941 void recurse_pointer (Dwarf_Die* type, target_symbol* e,
7d11d8c9 2942 print_format* pf, bool top);
5f36109e 2943 void recurse_struct (Dwarf_Die* type, target_symbol* e,
7d11d8c9 2944 print_format* pf, bool top);
5f36109e 2945 void recurse_struct_members (Dwarf_Die* type, target_symbol* e,
7d11d8c9 2946 print_format* pf, int& count);
bbee5bb8 2947 bool print_chars (Dwarf_Die* type, target_symbol* e, print_format* pf);
5f36109e
JS
2948
2949 void init_ts (const target_symbol& e);
2950 expression* deref (target_symbol* e);
c55ea10d 2951 bool push_deref (print_format* pf, const string& fmt, target_symbol* e);
5f36109e
JS
2952};
2953
2954
2955void
2956dwarf_pretty_print::init_ts (const target_symbol& e)
2957{
2958 // Work with a new target_symbol so we can modify arguments
2959 ts = new target_symbol (e);
2960
2961 if (ts->addressof)
dc09353a 2962 throw SEMANTIC_ERROR(_("cannot take address of pretty-printed variable"), ts->tok);
5f36109e 2963
c4965ad9
JS
2964 size_t depth = ts->pretty_print_depth ();
2965 if (depth == 0)
dc09353a 2966 throw SEMANTIC_ERROR(_("invalid target_symbol for pretty-print"), ts->tok);
c4965ad9 2967 print_full = depth > 1;
5f36109e
JS
2968 ts->components.pop_back();
2969}
2970
2971
2972functioncall*
2973dwarf_pretty_print::expand ()
2974{
2975 static unsigned tick = 0;
2976
2977 // function pretty_print_X([pointer], [arg1, arg2, ...]) {
7d11d8c9
JS
2978 // try {
2979 // return sprintf("{.foo=...}", (ts)->foo, ...)
2980 // } catch {
2981 // return "ERROR"
2982 // }
5f36109e
JS
2983 // }
2984
2985 // Create the function decl and call.
2986
2987 functiondecl *fdecl = new functiondecl;
2988 fdecl->tok = ts->tok;
2989 fdecl->synthetic = true;
2990 fdecl->name = "_dwarf_pretty_print_" + lex_cast(tick++);
2991 fdecl->type = pe_string;
2992
2993 functioncall* fcall = new functioncall;
eb79eb0a 2994 fcall->referent = fdecl;
5f36109e
JS
2995 fcall->tok = ts->tok;
2996 fcall->function = fdecl->name;
140be17a 2997 fcall->type = pe_string;
5f36109e
JS
2998
2999 // If there's a <pointer>, replace it with a new var and make that
3000 // the first function argument.
3001 if (pointer)
3002 {
3003 vardecl *v = new vardecl;
3004 v->type = pe_long;
3005 v->name = "pointer";
3006 v->tok = ts->tok;
3007 fdecl->formal_args.push_back (v);
3008 fcall->args.push_back (pointer);
3009
3010 symbol* sym = new symbol;
3011 sym->tok = ts->tok;
3012 sym->name = v->name;
5f36109e
JS
3013 pointer = sym;
3014 }
3015
3016 // For each expression argument, replace it with a function argument.
3017 for (unsigned i = 0; i < ts->components.size(); ++i)
3018 if (ts->components[i].type == target_symbol::comp_expression_array_index)
3019 {
3020 vardecl *v = new vardecl;
3021 v->type = pe_long;
3022 v->name = "index" + lex_cast(i);
3023 v->tok = ts->tok;
3024 fdecl->formal_args.push_back (v);
3025 fcall->args.push_back (ts->components[i].expr_index);
3026
3027 symbol* sym = new symbol;
3028 sym->tok = ts->tok;
3029 sym->name = v->name;
5f36109e
JS
3030 ts->components[i].expr_index = sym;
3031 }
3032
3033 // Create the return sprintf.
1c922ad7 3034 print_format* pf = print_format::create(ts->tok, "sprintf");
5f36109e
JS
3035 return_statement* rs = new return_statement;
3036 rs->tok = ts->tok;
3037 rs->value = pf;
5f36109e
JS
3038
3039 // Recurse into the actual values.
7d11d8c9 3040 recurse (&base_type, ts, pf, true);
5f36109e
JS
3041 pf->components = print_format::string_to_components(pf->raw_components);
3042
7d11d8c9
JS
3043 // Create the try-catch net
3044 try_block* tb = new try_block;
3045 tb->tok = ts->tok;
3046 tb->try_block = rs;
3047 tb->catch_error_var = 0;
3048 return_statement* rs2 = new return_statement;
3049 rs2->tok = ts->tok;
3050 rs2->value = new literal_string ("ERROR");
3051 rs2->value->tok = ts->tok;
3052 tb->catch_block = rs2;
3053 fdecl->body = tb;
3054
f8809d54 3055 fdecl->join (dw.sess);
5f36109e
JS
3056 return fcall;
3057}
3058
3059
3060void
3061dwarf_pretty_print::recurse (Dwarf_Die* start_type, target_symbol* e,
7d11d8c9 3062 print_format* pf, bool top)
5f36109e 3063{
611a848e
JS
3064 // deal with initial void* pointers
3065 if (!deref_p && null_die(start_type))
3066 {
3067 push_deref (pf, "%p", e);
3068 return;
3069 }
3070
5f36109e
JS
3071 Dwarf_Die type;
3072 dw.resolve_unqualified_inner_typedie (start_type, &type, e);
3073
3074 switch (dwarf_tag(&type))
3075 {
3076 default:
3077 // XXX need a warning?
3078 // throw semantic_error ("unsupported type (tag " + lex_cast(dwarf_tag(&type))
3079 // + ") for " + dwarf_type_name(&type), e->tok);
3080 pf->raw_components.append("?");
3081 break;
3082
3083 case DW_TAG_enumeration_type:
3084 case DW_TAG_base_type:
7d11d8c9 3085 recurse_base (&type, e, pf);
5f36109e
JS
3086 break;
3087
3088 case DW_TAG_array_type:
7d11d8c9 3089 recurse_array (&type, e, pf, top);
5f36109e
JS
3090 break;
3091
3092 case DW_TAG_pointer_type:
3093 case DW_TAG_reference_type:
3094 case DW_TAG_rvalue_reference_type:
7d11d8c9 3095 recurse_pointer (&type, e, pf, top);
5f36109e
JS
3096 break;
3097
3098 case DW_TAG_subroutine_type:
c55ea10d 3099 push_deref (pf, "<function>:%p", e);
5f36109e
JS
3100 break;
3101
3102 case DW_TAG_union_type:
5f36109e
JS
3103 case DW_TAG_structure_type:
3104 case DW_TAG_class_type:
7d11d8c9 3105 recurse_struct (&type, e, pf, top);
5f36109e
JS
3106 break;
3107 }
3108}
3109
3110
600551ca
JS
3111// Bit fields are handled as a special-case combination of recurse() and
3112// recurse_base(), only called from recurse_struct_members(). The main
3113// difference is that the value is always printed numerically, even if the
3114// underlying type is a char.
3115void
3116dwarf_pretty_print::recurse_bitfield (Dwarf_Die* start_type, target_symbol* e,
3117 print_format* pf)
3118{
3119 Dwarf_Die type;
3120 dw.resolve_unqualified_inner_typedie (start_type, &type, e);
3121
3122 int tag = dwarf_tag(&type);
3123 if (tag != DW_TAG_base_type && tag != DW_TAG_enumeration_type)
3124 {
3125 // XXX need a warning?
3126 // throw semantic_error ("unsupported bitfield type (tag " + lex_cast(tag)
3127 // + ") for " + dwarf_type_name(&type), e->tok);
3128 pf->raw_components.append("?");
3129 return;
3130 }
3131
3132 Dwarf_Attribute attr;
3133 Dwarf_Word encoding = (Dwarf_Word) -1;
3134 dwarf_formudata (dwarf_attr_integrate (&type, DW_AT_encoding, &attr),
3135 &encoding);
3136 switch (encoding)
3137 {
3138 case DW_ATE_float:
3139 case DW_ATE_complex_float:
3140 // XXX need a warning?
3141 // throw semantic_error ("unsupported bitfield type (encoding " + lex_cast(encoding)
3142 // + ") for " + dwarf_type_name(&type), e->tok);
3143 pf->raw_components.append("?");
3144 break;
3145
3146 case DW_ATE_unsigned:
3147 case DW_ATE_unsigned_char:
3148 push_deref (pf, "%u", e);
3149 break;
3150
3151 case DW_ATE_signed:
3152 case DW_ATE_signed_char:
3153 default:
3154 push_deref (pf, "%i", e);
3155 break;
3156 }
3157}
3158
3159
5f36109e
JS
3160void
3161dwarf_pretty_print::recurse_base (Dwarf_Die* type, target_symbol* e,
7d11d8c9 3162 print_format* pf)
5f36109e
JS
3163{
3164 Dwarf_Attribute attr;
3165 Dwarf_Word encoding = (Dwarf_Word) -1;
3166 dwarf_formudata (dwarf_attr_integrate (type, DW_AT_encoding, &attr),
3167 &encoding);
5f36109e
JS
3168 switch (encoding)
3169 {
3170 case DW_ATE_float:
3171 case DW_ATE_complex_float:
3172 // XXX need a warning?
3173 // throw semantic_error ("unsupported type (encoding " + lex_cast(encoding)
3174 // + ") for " + dwarf_type_name(type), e->tok);
3175 pf->raw_components.append("?");
5f36109e
JS
3176 break;
3177
6561d8d1 3178 case DW_ATE_UTF: // XXX need to add unicode to _stp_vsprint_char
5f36109e
JS
3179 case DW_ATE_signed_char:
3180 case DW_ATE_unsigned_char:
941101c1
JS
3181 // Use escapes to make sure that non-printable characters
3182 // don't interrupt our stream (especially '\0' values).
3183 push_deref (pf, "'%#c'", e);
5f36109e
JS
3184 break;
3185
3186 case DW_ATE_unsigned:
c55ea10d 3187 push_deref (pf, "%u", e);
5f36109e
JS
3188 break;
3189
600551ca 3190 case DW_ATE_signed:
5f36109e 3191 default:
c55ea10d 3192 push_deref (pf, "%i", e);
5f36109e
JS
3193 break;
3194 }
5f36109e
JS
3195}
3196
3197
3198void
3199dwarf_pretty_print::recurse_array (Dwarf_Die* type, target_symbol* e,
7d11d8c9 3200 print_format* pf, bool top)
5f36109e 3201{
7d11d8c9
JS
3202 if (!top && !print_full)
3203 {
3204 pf->raw_components.append("[...]");
3205 return;
3206 }
3207
5f36109e
JS
3208 Dwarf_Die childtype;
3209 dwarf_attr_die (type, DW_AT_type, &childtype);
bbee5bb8
JS
3210
3211 if (print_chars (&childtype, e, pf))
3212 return;
3213
5f36109e
JS
3214 pf->raw_components.append("[");
3215
3216 // We print the array up to the first 5 elements.
3217 // XXX how can we determine the array size?
3218 // ... for now, just print the first element
64cddf39 3219 // NB: limit to 32 args; see PR10750 and c_unparser::visit_print_format.
5f36109e 3220 unsigned i, size = 1;
64cddf39 3221 for (i=0; i < size && i < 5 && pf->args.size() < 32; ++i)
5f36109e
JS
3222 {
3223 if (i > 0)
3224 pf->raw_components.append(", ");
3225 target_symbol* e2 = new target_symbol(*e);
3226 e2->components.push_back (target_symbol::component(e->tok, i));
7d11d8c9 3227 recurse (&childtype, e2, pf);
5f36109e
JS
3228 }
3229 if (i < size || 1/*XXX until real size is known */)
3230 pf->raw_components.append(", ...");
3231 pf->raw_components.append("]");
3232}
3233
3234
3235void
3236dwarf_pretty_print::recurse_pointer (Dwarf_Die* type, target_symbol* e,
7d11d8c9 3237 print_format* pf, bool top)
5f36109e 3238{
7d11d8c9 3239 // We chase to top-level pointers, but leave the rest alone
d19a9a82 3240 bool void_p = true;
7d11d8c9 3241 Dwarf_Die pointee;
bbee5bb8 3242 if (dwarf_attr_die (type, DW_AT_type, &pointee))
d19a9a82
JS
3243 {
3244 try
3245 {
3246 dw.resolve_unqualified_inner_typedie (&pointee, &pointee, e);
3247 void_p = false;
3248 }
3249 catch (const semantic_error&) {}
3250 }
3251
3252 if (!void_p)
5f36109e 3253 {
bbee5bb8
JS
3254 if (print_chars (&pointee, e, pf))
3255 return;
3256
3257 if (top)
3258 {
3259 recurse (&pointee, e, pf, top);
3260 return;
3261 }
5f36109e 3262 }
bbee5bb8 3263
c55ea10d 3264 push_deref (pf, "%p", e);
5f36109e
JS
3265}
3266
3267
3268void
3269dwarf_pretty_print::recurse_struct (Dwarf_Die* type, target_symbol* e,
7d11d8c9 3270 print_format* pf, bool top)
5f36109e 3271{
bdec0e18
JS
3272 if (dwarf_hasattr(type, DW_AT_declaration))
3273 {
a44a7cb5 3274 Dwarf_Die *resolved = dw.declaration_resolve(type);
bdec0e18
JS
3275 if (!resolved)
3276 {
3277 // could be an error, but for now just stub it
3278 // throw semantic_error ("unresolved " + dwarf_type_name(type), e->tok);
3279 pf->raw_components.append("{...}");
3280 return;
3281 }
3282 type = resolved;
3283 }
3284
5f36109e
JS
3285 int count = 0;
3286 pf->raw_components.append("{");
7d11d8c9
JS
3287 if (top || print_full)
3288 recurse_struct_members (type, e, pf, count);
3289 else
3290 pf->raw_components.append("...");
5f36109e
JS
3291 pf->raw_components.append("}");
3292}
3293
3294
3295void
3296dwarf_pretty_print::recurse_struct_members (Dwarf_Die* type, target_symbol* e,
7d11d8c9 3297 print_format* pf, int& count)
5f36109e 3298{
a80f28d8
JS
3299 /* With inheritance, a subclass may mask member names of parent classes, so
3300 * our search among the inheritance tree must be breadth-first rather than
3301 * depth-first (recursive). The type die is still our starting point. When
3302 * we encounter a masked name, just skip it. */
3303 set<string> dupes;
3304 deque<Dwarf_Die> inheritees(1, *type);
3305 for (; !inheritees.empty(); inheritees.pop_front())
3306 {
dee830d9 3307 Dwarf_Die child, childtype, import;
a80f28d8
JS
3308 if (dwarf_child (&inheritees.front(), &child) == 0)
3309 do
3310 {
3311 target_symbol* e2 = e;
5f36109e 3312
a80f28d8
JS
3313 // skip static members
3314 if (dwarf_hasattr(&child, DW_AT_declaration))
3315 continue;
5f36109e 3316
a80f28d8 3317 int tag = dwarf_tag (&child);
5f36109e 3318
dee830d9
MW
3319 /* Pretend imported units contain members by recursing into
3320 struct_member printing with the same count. */
3321 if (tag == DW_TAG_imported_unit
3322 && dwarf_attr_die (&child, DW_AT_import, &import))
3323 recurse_struct_members (&import, e2, pf, count);
3324
a80f28d8
JS
3325 if (tag != DW_TAG_member && tag != DW_TAG_inheritance)
3326 continue;
5f36109e 3327
a80f28d8 3328 dwarf_attr_die (&child, DW_AT_type, &childtype);
5f36109e 3329
a80f28d8
JS
3330 if (tag == DW_TAG_inheritance)
3331 {
3332 inheritees.push_back(childtype);
3333 continue;
3334 }
5f36109e 3335
a80f28d8
JS
3336 int childtag = dwarf_tag (&childtype);
3337 const char *member = dwarf_diename (&child);
3a147004 3338
a80f28d8
JS
3339 // "_vptr.foo" members are C++ virtual function tables,
3340 // which (generally?) aren't interesting for users.
3341 if (member && startswith(member, "_vptr."))
3342 continue;
3a147004 3343
a80f28d8
JS
3344 // skip inheritance-masked duplicates
3345 if (member && !dupes.insert(member).second)
3346 continue;
64cddf39 3347
a80f28d8
JS
3348 if (++count > 1)
3349 pf->raw_components.append(", ");
64cddf39 3350
a80f28d8
JS
3351 // NB: limit to 32 args; see PR10750 and c_unparser::visit_print_format.
3352 if (pf->args.size() >= 32)
3353 {
3354 pf->raw_components.append("...");
3355 break;
3356 }
3357
3358 if (member)
3359 {
3360 pf->raw_components.append(".");
3361 pf->raw_components.append(member);
5f36109e 3362
a80f28d8
JS
3363 e2 = new target_symbol(*e);
3364 e2->components.push_back (target_symbol::component(e->tok, member));
3365 }
3366 else if (childtag == DW_TAG_union_type)
3367 pf->raw_components.append("<union>");
3368 else if (childtag == DW_TAG_structure_type)
3369 pf->raw_components.append("<class>");
3370 else if (childtag == DW_TAG_class_type)
3371 pf->raw_components.append("<struct>");
3372 pf->raw_components.append("=");
600551ca
JS
3373
3374 if (dwarf_hasattr_integrate (&child, DW_AT_bit_offset))
3375 recurse_bitfield (&childtype, e2, pf);
3376 else
3377 recurse (&childtype, e2, pf);
5f36109e 3378 }
a80f28d8
JS
3379 while (dwarf_siblingof (&child, &child) == 0);
3380 }
5f36109e
JS
3381}
3382
3383
bbee5bb8
JS
3384bool
3385dwarf_pretty_print::print_chars (Dwarf_Die* start_type, target_symbol* e,
3386 print_format* pf)
3387{
3388 Dwarf_Die type;
3389 dw.resolve_unqualified_inner_typedie (start_type, &type, e);
6561d8d1
JS
3390
3391 Dwarf_Attribute attr;
3392 Dwarf_Word encoding = (Dwarf_Word) -1;
3393 dwarf_formudata (dwarf_attr_integrate (&type, DW_AT_encoding, &attr),
3394 &encoding);
3395 switch (encoding)
bbee5bb8 3396 {
6561d8d1
JS
3397 case DW_ATE_UTF:
3398 case DW_ATE_signed_char:
3399 case DW_ATE_unsigned_char:
3400 break;
3401 default:
3402 return false;
3403 }
3404
3405 string function = userspace_p ? "user_string2" : "kernel_string2";
3406 Dwarf_Word size = (Dwarf_Word) -1;
3407 dwarf_formudata (dwarf_attr_integrate (&type, DW_AT_byte_size, &attr), &size);
3408 switch (size)
3409 {
3410 case 1:
3411 break;
3412 case 2:
3413 function += "_utf16";
3414 break;
3415 case 4:
3416 function += "_utf32";
3417 break;
3418 default:
3419 return false;
3420 }
3421
3422 if (push_deref (pf, "\"%s\"", e))
3423 {
3424 // steal the last arg for a string access
3425 assert (!pf->args.empty());
3426 functioncall* fcall = new functioncall;
3427 fcall->tok = e->tok;
3428 fcall->function = function;
3429 fcall->args.push_back (pf->args.back());
3430 expression *err_msg = new literal_string ("<unknown>");
3431 err_msg->tok = e->tok;
3432 fcall->args.push_back (err_msg);
3433 pf->args.back() = fcall;
bbee5bb8 3434 }
6561d8d1 3435 return true;
bbee5bb8
JS
3436}
3437
a5ce5211
MW
3438// PR10601: adapt to kernel-vs-userspace loc2c-runtime
3439static const string EMBEDDED_FETCH_DEREF_KERNEL = string("\n")
f1e8e7e0
MW
3440 + "#define fetch_register k_fetch_register\n"
3441 + "#define store_register k_store_register\n"
3442 + "#define deref kderef\n"
3443 + "#define store_deref store_kderef\n";
a5ce5211
MW
3444
3445static const string EMBEDDED_FETCH_DEREF_USER = string("\n")
f1e8e7e0
MW
3446 + "#define fetch_register u_fetch_register\n"
3447 + "#define store_register u_store_register\n"
3448 + "#define deref uderef\n"
3449 + "#define store_deref store_uderef\n";
a5ce5211
MW
3450
3451#define EMBEDDED_FETCH_DEREF(U) \
f1e8e7e0 3452 (U ? EMBEDDED_FETCH_DEREF_USER : EMBEDDED_FETCH_DEREF_KERNEL)
a5ce5211
MW
3453
3454static const string EMBEDDED_FETCH_DEREF_DONE = string("\n")
f1e8e7e0
MW
3455 + "#undef fetch_register\n"
3456 + "#undef store_register\n"
3457 + "#undef deref\n"
3458 + "#undef store_deref\n";
bbee5bb8 3459
1c0be8c7 3460static functioncall*
40a393cd
JS
3461synthetic_embedded_deref_call(dwflpp& dw,
3462 Dwarf_Die* function_type,
1c0be8c7
JS
3463 const string& function_name,
3464 const string& function_code,
1c0be8c7
JS
3465 bool userspace_p,
3466 bool lvalue_p,
3467 target_symbol* e,
3468 expression* pointer=NULL)
3469{
3470 // Synthesize a functiondecl for the given embedded code string.
5f36109e
JS
3471 functiondecl *fdecl = new functiondecl;
3472 fdecl->synthetic = true;
3473 fdecl->tok = e->tok;
1c0be8c7 3474 fdecl->name = function_name;
40a393cd
JS
3475 // The fdecl type is generic, but we'll be detailed on the fcall below.
3476 fdecl->type = pe_long;
c3ed664e
JS
3477 fdecl->type_details.reset(new exp_type_dwarf(&dw, function_type,
3478 userspace_p, e->addressof));
1c0be8c7 3479
5f36109e
JS
3480 embeddedcode *ec = new embeddedcode;
3481 ec->tok = e->tok;
1c0be8c7
JS
3482 ec->code += "/* unprivileged */";
3483 if (! lvalue_p)
3484 ec->code += "/* pure */";
3485 ec->code += EMBEDDED_FETCH_DEREF(userspace_p);
3486 ec->code += function_code;
3487 ec->code += EMBEDDED_FETCH_DEREF_DONE;
5f36109e
JS
3488 fdecl->body = ec;
3489
3490 // Synthesize a functioncall.
3491 functioncall* fcall = new functioncall;
3492 fcall->tok = e->tok;
40a393cd 3493 fcall->referent = fdecl;
5f36109e 3494 fcall->function = fdecl->name;
1c0be8c7 3495 fcall->type = fdecl->type;
c3ed664e 3496 fcall->type_details = fdecl->type_details;
5f36109e 3497
1c0be8c7
JS
3498 // If this code snippet uses a precomputed pointer,
3499 // pass that as the first argument.
5f36109e
JS
3500 if (pointer)
3501 {
5f36109e
JS
3502 vardecl *v = new vardecl;
3503 v->type = pe_long;
3504 v->name = "pointer";
3505 v->tok = e->tok;
3506 fdecl->formal_args.push_back(v);
3507 fcall->args.push_back(pointer);
3508 }
5f36109e 3509
1c0be8c7 3510 // Any non-literal indexes need to be passed as arguments too.
5f36109e
JS
3511 for (unsigned i = 0; i < e->components.size(); ++i)
3512 if (e->components[i].type == target_symbol::comp_expression_array_index)
3513 {
3514 vardecl *v = new vardecl;
3515 v->type = pe_long;
3516 v->name = "index" + lex_cast(i);
3517 v->tok = e->tok;
3518 fdecl->formal_args.push_back(v);
3519 fcall->args.push_back(e->components[i].expr_index);
3520 }
3521
1c0be8c7
JS
3522 // If this code snippet is assigning to an lvalue,
3523 // add a final argument for the rvalue.
3524 if (lvalue_p)
3525 {
3526 // Modify the fdecl so it carries a single pe_long formal
3527 // argument called "value".
5f36109e 3528
1c0be8c7
JS
3529 // FIXME: For the time being we only support setting target
3530 // variables which have base types; these are 'pe_long' in
3531 // stap's type vocabulary. Strings and pointers might be
3532 // reasonable, some day, but not today.
5f36109e 3533
1c0be8c7
JS
3534 vardecl *v = new vardecl;
3535 v->type = pe_long;
3536 v->name = "value";
3537 v->tok = e->tok;
3538 fdecl->formal_args.push_back(v);
3539 // NB: We don't know the value for fcall argument yet.
3540 // (see target_symbol_setter_functioncalls)
3541 }
3542
3543 // Add the synthesized decl to the session, and return the call.
40a393cd 3544 fdecl->join (dw.sess);
5f36109e
JS
3545 return fcall;
3546}
3547
1c0be8c7
JS
3548expression*
3549dwarf_pretty_print::deref (target_symbol* e)
3550{
3551 static unsigned tick = 0;
3552
3553 if (!deref_p)
3554 {
3555 assert (pointer && e->components.empty());
3556 return pointer;
3557 }
3558
3559 bool lvalue_p = false;
3560 string name = "_dwarf_pretty_print_deref_" + lex_cast(tick++);
3561
3562 string code;
40a393cd 3563 Dwarf_Die endtype;
1c0be8c7 3564 if (pointer)
40a393cd 3565 code = dw.literal_stmt_for_pointer (&pointer_type, e, false, &endtype);
1c0be8c7 3566 else if (!local.empty())
40a393cd 3567 code = dw.literal_stmt_for_local (scopes, pc, local, e, false, &endtype);
1c0be8c7 3568 else
40a393cd 3569 code = dw.literal_stmt_for_return (&scopes[0], pc, e, false, &endtype);
1c0be8c7 3570
40a393cd 3571 return synthetic_embedded_deref_call(dw, &endtype, name, code,
1c0be8c7
JS
3572 userspace_p, lvalue_p, e, pointer);
3573}
3574
5f36109e 3575
c55ea10d
JS
3576bool
3577dwarf_pretty_print::push_deref (print_format* pf, const string& fmt,
3578 target_symbol* e)
3579{
3580 expression* e2 = NULL;
3581 try
3582 {
3583 e2 = deref (e);
3584 }
3585 catch (const semantic_error&)
3586 {
3587 pf->raw_components.append ("?");
3588 return false;
3589 }
3590 pf->raw_components.append (fmt);
3591 pf->args.push_back (e2);
3592 return true;
3593}
3594
3595
e57b735a 3596void
a7999c82 3597dwarf_var_expanding_visitor::visit_target_symbol_saved_return (target_symbol* e)
e57b735a 3598{
a7999c82
JS
3599 // Get the full name of the target symbol.
3600 stringstream ts_name_stream;
3601 e->print(ts_name_stream);
3602 string ts_name = ts_name_stream.str();
3603
3604 // Check and make sure we haven't already seen this target
3605 // variable in this return probe. If we have, just return our
3606 // last replacement.
af234c40 3607 map<string, expression *>::iterator i = return_ts_map.find(ts_name);
a7999c82 3608 if (i != return_ts_map.end())
85ecf79a 3609 {
a7999c82
JS
3610 provide (i->second);
3611 return;
3612 }
85ecf79a 3613
70208613
JS
3614 // Attempt the expansion directly first, so if there's a problem with the
3615 // variable we won't have a bogus entry probe lying around. Like in
3616 // saveargs(), we pretend for a moment that we're not in a .return.
3617 bool saved_has_return = q.has_return;
3618 q.has_return = false;
3619 expression *repl = e;
3620 replace (repl);
3621 q.has_return = saved_has_return;
3622 target_symbol* n = dynamic_cast<target_symbol*>(repl);
3623 if (n && n->saved_conversion_error)
3624 {
3625 provide (repl);
3626 return;
3627 }
3628
af234c40
JS
3629 expression *exp;
3630 if (!q.has_process &&
3631 strverscmp(q.sess.kernel_base_release.c_str(), "2.6.25") >= 0)
140be17a 3632 exp = gen_kretprobe_saved_return(repl);
af234c40 3633 else
cc9001af 3634 exp = gen_mapped_saved_return(repl, e->sym_name());
af234c40 3635
6bed2a76
JS
3636 // Propagate the DWARF type to the expression in the return probe.
3637 if (repl->type_details && !exp->type_details)
3638 exp->type_details = repl->type_details;
3639
af234c40
JS
3640 // Provide the variable to our parent so it can be used as a
3641 // substitute for the target symbol.
3642 provide (exp);
3643
3644 // Remember this replacement since we might be able to reuse
3645 // it later if the same return probe references this target
3646 // symbol again.
3647 return_ts_map[ts_name] = exp;
3648}
3649
4a2970a3 3650static expression*
23dc94f6
DS
3651gen_mapped_saved_return(systemtap_session &sess, expression* e,
3652 const string& name,
3653 block *& add_block, bool& add_block_tid,
3654 block *& add_call_probe, bool& add_call_probe_tid)
af234c40 3655{
23dc94f6
DS
3656 static unsigned tick = 0;
3657
a7999c82
JS
3658 // We've got to do several things here to handle target
3659 // variables in return probes.
85ecf79a 3660
a7999c82
JS
3661 // (1) Synthesize two global arrays. One is the cache of the
3662 // target variable and the other contains a thread specific
3663 // nesting level counter. The arrays will look like
3664 // this:
3665 //
23dc94f6
DS
3666 // _entry_tvar_{name}_{num}
3667 // _entry_tvar_{name}_{num}_ctr
a7999c82 3668
23dc94f6 3669 string aname = (string("_entry_tvar_")
cc9001af 3670 + name
aca66a36 3671 + "_" + lex_cast(tick++));
a7999c82
JS
3672 vardecl* vd = new vardecl;
3673 vd->name = aname;
3674 vd->tok = e->tok;
23dc94f6 3675 sess.globals.push_back (vd);
a7999c82
JS
3676
3677 string ctrname = aname + "_ctr";
3678 vd = new vardecl;
3679 vd->name = ctrname;
3680 vd->tok = e->tok;
23dc94f6 3681 sess.globals.push_back (vd);
a7999c82
JS
3682
3683 // (2) Create a new code block we're going to insert at the
3684 // beginning of this probe to get the cached value into a
3685 // temporary variable. We'll replace the target variable
3686 // reference with the temporary variable reference. The code
3687 // will look like this:
3688 //
23dc94f6
DS
3689 // _entry_tvar_tid = tid()
3690 // _entry_tvar_{name}_{num}_tmp
3691 // = _entry_tvar_{name}_{num}[_entry_tvar_tid,
3692 // _entry_tvar_{name}_{num}_ctr[_entry_tvar_tid]]
3693 // delete _entry_tvar_{name}_{num}[_entry_tvar_tid,
3694 // _entry_tvar_{name}_{num}_ctr[_entry_tvar_tid]--]
3695 // if (! _entry_tvar_{name}_{num}_ctr[_entry_tvar_tid])
3696 // delete _entry_tvar_{name}_{num}_ctr[_entry_tvar_tid]
a7999c82
JS
3697
3698 // (2a) Synthesize the tid temporary expression, which will look
3699 // like this:
3700 //
23dc94f6 3701 // _entry_tvar_tid = tid()
a7999c82 3702 symbol* tidsym = new symbol;
23dc94f6 3703 tidsym->name = string("_entry_tvar_tid");
a7999c82 3704 tidsym->tok = e->tok;
85ecf79a 3705
a7999c82
JS
3706 if (add_block == NULL)
3707 {
3708 add_block = new block;
3709 add_block->tok = e->tok;
8cc799a5 3710 }
8c819921 3711
8cc799a5
JS
3712 if (!add_block_tid)
3713 {
a7999c82
JS
3714 // Synthesize a functioncall to grab the thread id.
3715 functioncall* fc = new functioncall;
3716 fc->tok = e->tok;
3717 fc->function = string("tid");
8c819921 3718
23dc94f6 3719 // Assign the tid to '_entry_tvar_tid'.
8c819921
DS
3720 assignment* a = new assignment;
3721 a->tok = e->tok;
3722 a->op = "=";
a7999c82
JS
3723 a->left = tidsym;
3724 a->right = fc;
8c819921
DS
3725
3726 expr_statement* es = new expr_statement;
3727 es->tok = e->tok;
3728 es->value = a;
8c819921 3729 add_block->statements.push_back (es);
8cc799a5 3730 add_block_tid = true;
a7999c82 3731 }
8c819921 3732
a7999c82
JS
3733 // (2b) Synthesize an array reference and assign it to a
3734 // temporary variable (that we'll use as replacement for the
3735 // target variable reference). It will look like this:
3736 //
23dc94f6
DS
3737 // _entry_tvar_{name}_{num}_tmp
3738 // = _entry_tvar_{name}_{num}[_entry_tvar_tid,
3739 // _entry_tvar_{name}_{num}_ctr[_entry_tvar_tid]]
a7999c82
JS
3740
3741 arrayindex* ai_tvar_base = new arrayindex;
3742 ai_tvar_base->tok = e->tok;
3743
3744 symbol* sym = new symbol;
3745 sym->name = aname;
3746 sym->tok = e->tok;
3747 ai_tvar_base->base = sym;
3748
3749 ai_tvar_base->indexes.push_back(tidsym);
3750
3751 // We need to create a copy of the array index in its current
3752 // state so we can have 2 variants of it (the original and one
3753 // that post-decrements the second index).
3754 arrayindex* ai_tvar = new arrayindex;
3755 arrayindex* ai_tvar_postdec = new arrayindex;
3756 *ai_tvar = *ai_tvar_base;
3757 *ai_tvar_postdec = *ai_tvar_base;
3758
3759 // Synthesize the
23dc94f6 3760 // "_entry_tvar_{name}_{num}_ctr[_entry_tvar_tid]" used as the
a7999c82
JS
3761 // second index into the array.
3762 arrayindex* ai_ctr = new arrayindex;
3763 ai_ctr->tok = e->tok;
3764
3765 sym = new symbol;
3766 sym->name = ctrname;
3767 sym->tok = e->tok;
3768 ai_ctr->base = sym;
3769 ai_ctr->indexes.push_back(tidsym);
3770 ai_tvar->indexes.push_back(ai_ctr);
3771
3772 symbol* tmpsym = new symbol;
3773 tmpsym->name = aname + "_tmp";
3774 tmpsym->tok = e->tok;
3775
3776 assignment* a = new assignment;
3777 a->tok = e->tok;
3778 a->op = "=";
3779 a->left = tmpsym;
3780 a->right = ai_tvar;
3781
3782 expr_statement* es = new expr_statement;
3783 es->tok = e->tok;
3784 es->value = a;
3785
3786 add_block->statements.push_back (es);
3787
3788 // (2c) Add a post-decrement to the second array index and
3789 // delete the array value. It will look like this:
3790 //
23dc94f6
DS
3791 // delete _entry_tvar_{name}_{num}[_entry_tvar_tid,
3792 // _entry_tvar_{name}_{num}_ctr[_entry_tvar_tid]--]
a7999c82
JS
3793
3794 post_crement* pc = new post_crement;
3795 pc->tok = e->tok;
3796 pc->op = "--";
3797 pc->operand = ai_ctr;
3798 ai_tvar_postdec->indexes.push_back(pc);
3799
3800 delete_statement* ds = new delete_statement;
3801 ds->tok = e->tok;
3802 ds->value = ai_tvar_postdec;
3803
3804 add_block->statements.push_back (ds);
3805
3806 // (2d) Delete the counter value if it is 0. It will look like
3807 // this:
23dc94f6
DS
3808 // if (! _entry_tvar_{name}_{num}_ctr[_entry_tvar_tid])
3809 // delete _entry_tvar_{name}_{num}_ctr[_entry_tvar_tid]
a7999c82
JS
3810
3811 ds = new delete_statement;
3812 ds->tok = e->tok;
3813 ds->value = ai_ctr;
3814
3815 unary_expression *ue = new unary_expression;
3816 ue->tok = e->tok;
3817 ue->op = "!";
3818 ue->operand = ai_ctr;
3819
3820 if_statement *ifs = new if_statement;
3821 ifs->tok = e->tok;
3822 ifs->condition = ue;
3823 ifs->thenblock = ds;
3824 ifs->elseblock = NULL;
3825
3826 add_block->statements.push_back (ifs);
3827
3828 // (3) We need an entry probe that saves the value for us in the
3829 // global array we created. Create the entry probe, which will
3830 // look like this:
3831 //
2260f4e3 3832 // probe kernel.function("{function}").call {
23dc94f6
DS
3833 // _entry_tvar_tid = tid()
3834 // _entry_tvar_{name}_{num}[_entry_tvar_tid,
3835 // ++_entry_tvar_{name}_{num}_ctr[_entry_tvar_tid]]
a7999c82
JS
3836 // = ${param}
3837 // }
3838
2260f4e3 3839 if (add_call_probe == NULL)
a7999c82 3840 {
2260f4e3
FCE
3841 add_call_probe = new block;
3842 add_call_probe->tok = e->tok;
8cc799a5 3843 }
4baf0e53 3844
8cc799a5
JS
3845 if (!add_call_probe_tid)
3846 {
a7999c82
JS
3847 // Synthesize a functioncall to grab the thread id.
3848 functioncall* fc = new functioncall;
3849 fc->tok = e->tok;
3850 fc->function = string("tid");
4baf0e53 3851
23dc94f6 3852 // Assign the tid to '_entry_tvar_tid'.
a7999c82 3853 assignment* a = new assignment;
8fc05e57
DS
3854 a->tok = e->tok;
3855 a->op = "=";
a7999c82
JS
3856 a->left = tidsym;
3857 a->right = fc;
8fc05e57 3858
a7999c82 3859 expr_statement* es = new expr_statement;
8fc05e57
DS
3860 es->tok = e->tok;
3861 es->value = a;
2260f4e3 3862 add_call_probe = new block(add_call_probe, es);
8cc799a5 3863 add_call_probe_tid = true;
85ecf79a 3864 }
cf2a1f85 3865
a7999c82 3866 // Save the value, like this:
23dc94f6
DS
3867 // _entry_tvar_{name}_{num}[_entry_tvar_tid,
3868 // ++_entry_tvar_{name}_{num}_ctr[_entry_tvar_tid]]
a7999c82
JS
3869 // = ${param}
3870 arrayindex* ai_tvar_preinc = new arrayindex;
3871 *ai_tvar_preinc = *ai_tvar_base;
3872
3873 pre_crement* preinc = new pre_crement;
3874 preinc->tok = e->tok;
3875 preinc->op = "++";
3876 preinc->operand = ai_ctr;
3877 ai_tvar_preinc->indexes.push_back(preinc);
3878
3879 a = new assignment;
3880 a->tok = e->tok;
3881 a->op = "=";
3882 a->left = ai_tvar_preinc;
3883 a->right = e;
3884
3885 es = new expr_statement;
3886 es->tok = e->tok;
3887 es->value = a;
3888
2260f4e3 3889 add_call_probe = new block(add_call_probe, es);
a7999c82 3890
23dc94f6 3891 // (4) Provide the '_entry_tvar_{name}_{num}_tmp' variable to
a7999c82
JS
3892 // our parent so it can be used as a substitute for the target
3893 // symbol.
3f803f9e 3894 delete ai_tvar_base;
af234c40
JS
3895 return tmpsym;
3896}
a7999c82 3897
af234c40 3898
23dc94f6
DS
3899expression*
3900dwarf_var_expanding_visitor::gen_mapped_saved_return(expression* e,
3901 const string& name)
3902{
3903 return ::gen_mapped_saved_return(q.sess, e, name, add_block,
3904 add_block_tid, add_call_probe,
3905 add_call_probe_tid);
3906}
3907
3908
af234c40 3909expression*
140be17a 3910dwarf_var_expanding_visitor::gen_kretprobe_saved_return(expression* e)
af234c40
JS
3911{
3912 // The code for this is simple.
3913 //
3914 // .call:
3915 // _set_kretprobe_long(index, $value)
3916 //
3917 // .return:
3918 // _get_kretprobe_long(index)
3919 //
3920 // (or s/long/string/ for things like $$parms)
3921
3922 unsigned index;
3923 string setfn, getfn;
3924
140be17a
JS
3925 // We need the caller to predetermine the type of the expression!
3926 switch (e->type)
af234c40 3927 {
140be17a 3928 case pe_string:
af234c40
JS
3929 index = saved_strings++;
3930 setfn = "_set_kretprobe_string";
3931 getfn = "_get_kretprobe_string";
140be17a
JS
3932 break;
3933 case pe_long:
af234c40
JS
3934 index = saved_longs++;
3935 setfn = "_set_kretprobe_long";
3936 getfn = "_get_kretprobe_long";
140be17a
JS
3937 break;
3938 default:
dc09353a 3939 throw SEMANTIC_ERROR(_("unknown type to save in kretprobe"), e->tok);
af234c40
JS
3940 }
3941
3942 // Create the entry code
3943 // _set_kretprobe_{long|string}(index, $value)
3944
3945 if (add_call_probe == NULL)
3946 {
3947 add_call_probe = new block;
3948 add_call_probe->tok = e->tok;
3949 }
3950
3951 functioncall* set_fc = new functioncall;
3952 set_fc->tok = e->tok;
3953 set_fc->function = setfn;
3954 set_fc->args.push_back(new literal_number(index));
3955 set_fc->args.back()->tok = e->tok;
3956 set_fc->args.push_back(e);
3957
3958 expr_statement* set_es = new expr_statement;
3959 set_es->tok = e->tok;
3960 set_es->value = set_fc;
3961
3962 add_call_probe->statements.push_back(set_es);
3963
3964 // Create the return code
3965 // _get_kretprobe_{long|string}(index)
3966
3967 functioncall* get_fc = new functioncall;
3968 get_fc->tok = e->tok;
3969 get_fc->function = getfn;
3970 get_fc->args.push_back(new literal_number(index));
3971 get_fc->args.back()->tok = e->tok;
3972
3973 return get_fc;
a7999c82 3974}
a43ba433 3975
2cb3fe26 3976
a7999c82
JS
3977void
3978dwarf_var_expanding_visitor::visit_target_symbol_context (target_symbol* e)
3979{
b78a0fbb 3980 if (null_die(scope_die)) {
04c8e51b
JL
3981 literal_string *empty = new literal_string("");
3982 empty->tok = e->tok;
3983 provide(empty);
a7999c82 3984 return;
b78a0fbb 3985 }
2cb3fe26 3986
5f36109e
JS
3987 target_symbol *tsym = new target_symbol(*e);
3988
c4965ad9 3989 bool pretty = e->check_pretty_print ();
fde50242 3990 string format = pretty ? "=%s" : "=%#x";
a43ba433 3991
a7999c82
JS
3992 // Convert $$parms to sprintf of a list of parms and active local vars
3993 // which we recursively evaluate
a43ba433 3994
1c922ad7 3995 print_format* pf = print_format::create(e->tok, "sprintf");
a7999c82 3996
277c21bc 3997 if (q.has_return && (e->name == "$$return"))
a7999c82 3998 {
277c21bc 3999 tsym->name = "$return";
a7999c82
JS
4000
4001 // Ignore any variable that isn't accessible.
4002 tsym->saved_conversion_error = 0;
4003 expression *texp = tsym;
8b095b45 4004 replace (texp); // NB: throws nothing ...
a7999c82 4005 if (tsym->saved_conversion_error) // ... but this is how we know it happened.
a43ba433 4006 {
2cb3fe26 4007
a43ba433
FCE
4008 }
4009 else
4010 {
a7999c82 4011 pf->raw_components += "return";
5f36109e 4012 pf->raw_components += format;
a7999c82
JS
4013 pf->args.push_back(texp);
4014 }
4015 }
4016 else
4017 {
4018 // non-.return probe: support $$parms, $$vars, $$locals
345bbb3d 4019 bool first = true;
a7999c82 4020 Dwarf_Die result;
d48bc7eb
JS
4021 vector<Dwarf_Die> scopes = q.dw.getscopes(scope_die);
4022 for (unsigned i = 0; i < scopes.size(); ++i)
4023 {
4024 if (dwarf_tag(&scopes[i]) == DW_TAG_compile_unit)
4025 break; // we don't want file-level variables
4026 if (dwarf_child (&scopes[i], &result) == 0)
4027 do
00cf3709 4028 {
d48bc7eb
JS
4029 switch (dwarf_tag (&result))
4030 {
4031 case DW_TAG_variable:
4032 if (e->name == "$$parms")
4033 continue;
4034 break;
4035 case DW_TAG_formal_parameter:
4036 if (e->name == "$$locals")
4037 continue;
4038 break;
4039
4040 default:
4041 continue;
4042 }
41c262f3 4043
d48bc7eb
JS
4044 const char *diename = dwarf_diename (&result);
4045 if (! diename) continue;
f76427a2 4046
d48bc7eb
JS
4047 if (! first)
4048 pf->raw_components += " ";
4049 pf->raw_components += diename;
fde50242
JS
4050 first = false;
4051
4052 // Write a placeholder for ugly aggregates
4053 Dwarf_Die type;
4054 if (!pretty && dwarf_attr_die(&result, DW_AT_type, &type))
4055 {
4056 q.dw.resolve_unqualified_inner_typedie(&type, &type, e);
4057 switch (dwarf_tag(&type))
4058 {
4059 case DW_TAG_union_type:
4060 case DW_TAG_structure_type:
4061 case DW_TAG_class_type:
4062 pf->raw_components += "={...}";
4063 continue;
4064
4065 case DW_TAG_array_type:
4066 pf->raw_components += "=[...]";
4067 continue;
4068 }
4069 }
345bbb3d 4070
d48bc7eb
JS
4071 tsym->name = "$";
4072 tsym->name += diename;
41c262f3 4073
d48bc7eb
JS
4074 // Ignore any variable that isn't accessible.
4075 tsym->saved_conversion_error = 0;
4076 expression *texp = tsym;
4077 replace (texp); // NB: throws nothing ...
4078 if (tsym->saved_conversion_error) // ... but this is how we know it happened.
a43ba433 4079 {
d48bc7eb
JS
4080 if (q.sess.verbose>2)
4081 {
e26c2f83 4082 for (const semantic_error *c = tsym->saved_conversion_error;
d48bc7eb 4083 c != 0;
c081af73 4084 c = c->get_chain()) {
4c5d9906 4085 clog << _("variable location problem [man error::dwarf]: ") << c->what() << endl;
d48bc7eb
JS
4086 }
4087 }
4088
4089 pf->raw_components += "=?";
a43ba433 4090 }
d48bc7eb
JS
4091 else
4092 {
4093 pf->raw_components += format;
4094 pf->args.push_back(texp);
4095 }
a7999c82 4096 }
d48bc7eb
JS
4097 while (dwarf_siblingof (&result, &result) == 0);
4098 }
a7999c82 4099 }
2cb3fe26 4100
a7999c82 4101 pf->components = print_format::string_to_components(pf->raw_components);
140be17a 4102 pf->type = pe_string;
a7999c82
JS
4103 provide (pf);
4104}
4105
2cb3fe26 4106
bd1fcbad
YZ
4107void
4108dwarf_var_expanding_visitor::visit_atvar_op (atvar_op *e)
4109{
4110 // Fill in our current module context if needed
4111 if (e->module.empty())
4112 e->module = q.dw.module_name;
4113
4114 if (e->module == q.dw.module_name && e->cu_name.empty())
4115 {
4116 // process like any other local
4117 // e->sym_name() will do the right thing
4118 visit_target_symbol(e);
4119 return;
4120 }
4121
4122 var_expanding_visitor::visit_atvar_op(e);
4123}
4124
4125
a7999c82
JS
4126void
4127dwarf_var_expanding_visitor::visit_target_symbol (target_symbol *e)
4128{
bd1fcbad 4129 assert(e->name.size() > 0 && (e->name[0] == '$' || e->name == "@var"));
a7999c82 4130 visited = true;
30263a73
FCE
4131 bool defined_being_checked = (defined_ops.size() > 0 && (defined_ops.top()->operand == e));
4132 // In this mode, we avoid hiding errors or generating extra code such as for .return saved $vars
a7999c82 4133
70208613 4134 try
a7999c82 4135 {
c69a87e0
FCE
4136 bool lvalue = is_active_lvalue(e);
4137 if (lvalue && !q.sess.guru_mode)
dc09353a 4138 throw SEMANTIC_ERROR(_("write to target variable not permitted; need stap -g"), e->tok);
2cb3fe26 4139
100a540e 4140 // XXX: process $context vars should be writable
70208613 4141
c69a87e0
FCE
4142 // See if we need to generate a new probe to save/access function
4143 // parameters from a return probe. PR 1382.
4144 if (q.has_return
4145 && !defined_being_checked
277c21bc
JS
4146 && e->name != "$return" // not the special return-value variable handled below
4147 && e->name != "$$return") // nor the other special variable handled below
c69a87e0
FCE
4148 {
4149 if (lvalue)
dc09353a 4150 throw SEMANTIC_ERROR(_("write to target variable not permitted in .return probes"), e->tok);
c69a87e0
FCE
4151 visit_target_symbol_saved_return(e);
4152 return;
4153 }
e57b735a 4154
277c21bc
JS
4155 if (e->name == "$$vars" || e->name == "$$parms" || e->name == "$$locals"
4156 || (q.has_return && (e->name == "$$return")))
c69a87e0
FCE
4157 {
4158 if (lvalue)
dc09353a 4159 throw SEMANTIC_ERROR(_("cannot write to context variable"), e->tok);
70208613 4160
c69a87e0 4161 if (e->addressof)
dc09353a 4162 throw SEMANTIC_ERROR(_("cannot take address of context variable"), e->tok);
70208613 4163
5f36109e
JS
4164 e->assert_no_components("dwarf", true);
4165
c69a87e0
FCE
4166 visit_target_symbol_context(e);
4167 return;
4168 }
70208613 4169
04c8e51b
JL
4170 // Everything else (pretty-printed vars, and context vars) require a
4171 // scope_die in which to search for them. If we don't have that, just
4172 // leave it unresolved; we'll produce an error later on.
4173 if (null_die(scope_die))
4174 {
4175 provide(e);
4176 return;
4177 }
4178
c4965ad9 4179 if (e->check_pretty_print (lvalue))
5f36109e 4180 {
277c21bc 4181 if (q.has_return && (e->name == "$return"))
5f36109e
JS
4182 {
4183 dwarf_pretty_print dpp (q.dw, scope_die, addr,
4184 q.has_process, *e);
4185 dpp.expand()->visit(this);
4186 }
4187 else
4188 {
4189 dwarf_pretty_print dpp (q.dw, getscopes(e), addr,
cc9001af 4190 e->sym_name(),
5f36109e
JS
4191 q.has_process, *e);
4192 dpp.expand()->visit(this);
4193 }
4194 return;
4195 }
4196
1c0be8c7 4197 bool userspace_p = q.has_process;
c69a87e0 4198 string fname = (string(lvalue ? "_dwarf_tvar_set" : "_dwarf_tvar_get")
cc9001af 4199 + "_" + e->sym_name()
c69a87e0 4200 + "_" + lex_cast(tick++));
70208613 4201
70208613 4202
1c0be8c7 4203 string code;
40a393cd 4204 Dwarf_Die endtype;
277c21bc 4205 if (q.has_return && (e->name == "$return"))
40a393cd 4206 code = q.dw.literal_stmt_for_return (scope_die, addr, e, lvalue, &endtype);
e19fda4e 4207 else
1c0be8c7 4208 code = q.dw.literal_stmt_for_local (getscopes(e), addr, e->sym_name(),
40a393cd 4209 e, lvalue, &endtype);
70208613 4210
40a393cd 4211 functioncall* n = synthetic_embedded_deref_call(q.dw, &endtype, fname, code,
1c0be8c7 4212 userspace_p, lvalue, e);
70208613 4213
c69a87e0 4214 if (lvalue)
f55efafe 4215 provide_lvalue_call (n);
70208613 4216
1c0be8c7
JS
4217 // Revisit the functioncall so arguments can be expanded.
4218 n->visit (this);
66d284f4
FCE
4219 }
4220 catch (const semantic_error& er)
4221 {
9fab2262
JS
4222 // We suppress this error message, and pass the unresolved
4223 // target_symbol to the next pass. We hope that this value ends
4224 // up not being referenced after all, so it can be optimized out
4225 // quietly.
1af1e62d 4226 e->chain (er);
9fab2262 4227 provide (e);
66d284f4 4228 }
77de5e9e
GH
4229}
4230
4231
c24447be
JS
4232void
4233dwarf_var_expanding_visitor::visit_cast_op (cast_op *e)
4234{
4235 // Fill in our current module context if needed
4236 if (e->module.empty())
4237 e->module = q.dw.module_name;
4238
4239 var_expanding_visitor::visit_cast_op(e);
4240}
4241
4242
8cc799a5
JS
4243void
4244dwarf_var_expanding_visitor::visit_entry_op (entry_op *e)
4245{
4246 expression *repl = e;
4247 if (q.has_return)
4248 {
4249 // expand the operand as if it weren't a return probe
4250 q.has_return = false;
4251 replace (e->operand);
4252 q.has_return = true;
4253
4254 // XXX it would be nice to use gen_kretprobe_saved_return when available,
4255 // but it requires knowing the types already, which is problematic for
4256 // arbitrary expressons.
cc9001af 4257 repl = gen_mapped_saved_return (e->operand, "entry");
8cc799a5
JS
4258 }
4259 provide (repl);
4260}
4261
3689db05
SC
4262void
4263dwarf_var_expanding_visitor::visit_perf_op (perf_op *e)
4264{
ace7c23f 4265 string e_lit_val = e->operand->value;
b78a0fbb 4266
3689db05 4267 add_block = new block;
9eaaceaa 4268 add_block->tok = e->tok;
3689db05
SC
4269
4270 systemtap_session &s = this->q.sess;
aa2c662f 4271 std::vector<std::pair<std::string,std::string> >::iterator it;
3689db05
SC
4272 // Find the associated perf.counter probe
4273 for (it=s.perf_counters.begin();
4274 it != s.perf_counters.end();
4275 it++)
4276 if ((*it).first == e_lit_val)
4277 {
aa2c662f
SC
4278 // if perf .process("name") omitted, then set it to this process name
4279 if ((*it).second.length() == 0)
4280 (*it).second = this->q.user_path;
4281 if ((*it).second == this->q.user_path)
3689db05
SC
4282 break;
4283 }
4284
4285 if (it != s.perf_counters.end())
4286 {
aa2c662f 4287 perf_counter_refs.insert((*it).first);
3689db05
SC
4288 // __perf_read_N is assigned in the probe prologue
4289 symbol* sym = new symbol;
4653caf1 4290 sym->tok = e->tok;
3689db05
SC
4291 sym->name = "__perf_read_" + (*it).first;
4292 provide (sym);
4293 }
4294 else
dc09353a 4295 throw SEMANTIC_ERROR(_F("perf counter '%s' not defined", e_lit_val.c_str()));
3689db05
SC
4296}
4297
8cc799a5 4298
729455a7
JS
4299vector<Dwarf_Die>&
4300dwarf_var_expanding_visitor::getscopes(target_symbol *e)
4301{
4302 if (scopes.empty())
4303 {
04c8e51b 4304 if(!null_die(scope_die))
f25a9197 4305 scopes = q.dw.getscopes(scope_die);
729455a7 4306 if (scopes.empty())
b530b5b3
LB
4307 //throw semantic_error (_F("unable to find any scopes containing %d", addr), e->tok);
4308 // ((scope_die == NULL) ? "" : (string (" in ") + (dwarf_diename(scope_die) ?: "<unknown>") + "(" + (dwarf_diename(q.dw.cu) ?: "<unknown>") ")" ))
dc09353a 4309 throw SEMANTIC_ERROR ("unable to find any scopes containing "
729455a7 4310 + lex_cast_hex(addr)
04c8e51b 4311 + (null_die(scope_die) ? ""
729455a7
JS
4312 : (string (" in ")
4313 + (dwarf_diename(scope_die) ?: "<unknown>")
4314 + "(" + (dwarf_diename(q.dw.cu) ?: "<unknown>")
4315 + ")"))
4316 + " while searching for local '"
cc9001af 4317 + e->sym_name() + "'",
729455a7
JS
4318 e->tok);
4319 }
4320 return scopes;
4321}
4322
4323
5f36109e
JS
4324struct dwarf_cast_expanding_visitor: public var_expanding_visitor
4325{
4326 systemtap_session& s;
4327 dwarf_builder& db;
d77be314 4328 map<string,string> compiled_headers;
5f36109e
JS
4329
4330 dwarf_cast_expanding_visitor(systemtap_session& s, dwarf_builder& db):
4331 s(s), db(db) {}
4332 void visit_cast_op (cast_op* e);
4333 void filter_special_modules(string& module);
4334};
4335
4336
c4ce66a1
JS
4337struct dwarf_cast_query : public base_query
4338{
946e1a48 4339 cast_op& e;
c4ce66a1 4340 const bool lvalue;
5f36109e
JS
4341 const bool userspace_p;
4342 functioncall*& result;
c4ce66a1 4343
5f36109e
JS
4344 dwarf_cast_query(dwflpp& dw, const string& module, cast_op& e, bool lvalue,
4345 const bool userspace_p, functioncall*& result):
abb41d92 4346 base_query(dw, module), e(e), lvalue(lvalue),
5f36109e 4347 userspace_p(userspace_p), result(result) {}
c4ce66a1
JS
4348
4349 void handle_query_module();
822a6a3d 4350 void query_library (const char *) {}
576eaefe 4351 void query_plt (const char *entry, size_t addr) {}
c4ce66a1
JS
4352};
4353
4354
c4ce66a1
JS
4355void
4356dwarf_cast_query::handle_query_module()
4357{
5f36109e
JS
4358 static unsigned tick = 0;
4359
4360 if (result)
c4ce66a1
JS
4361 return;
4362
ea1e477a 4363 // look for the type in any CU
a44a7cb5
JS
4364 Dwarf_Die* type_die = NULL;
4365 if (startswith(e.type_name, "class "))
4366 {
4367 // normalize to match dwflpp::global_alias_caching_callback
4368 string struct_name = "struct " + e.type_name.substr(6);
4369 type_die = dw.declaration_resolve_other_cus(struct_name);
4370 }
4371 else
4372 type_die = dw.declaration_resolve_other_cus(e.type_name);
4373
4374 // NB: We now index the types as "struct name"/"union name"/etc. instead of
4375 // just "name". But since we didn't require users to be explicit before, and
4376 // actually sort of discouraged it, we must be flexible now. So if a lookup
4377 // fails with a bare name, try augmenting it.
4378 if (!type_die &&
4379 !startswith(e.type_name, "class ") &&
4380 !startswith(e.type_name, "struct ") &&
4381 !startswith(e.type_name, "union ") &&
4382 !startswith(e.type_name, "enum "))
4383 {
4384 type_die = dw.declaration_resolve_other_cus("struct " + e.type_name);
4385 if (!type_die)
4386 type_die = dw.declaration_resolve_other_cus("union " + e.type_name);
4387 if (!type_die)
4388 type_die = dw.declaration_resolve_other_cus("enum " + e.type_name);
4389 }
4390
ea1e477a
JS
4391 if (!type_die)
4392 return;
c4ce66a1 4393
5f36109e 4394 string code;
40a393cd 4395 Dwarf_Die endtype;
5f36109e 4396
ea1e477a 4397 try
c4ce66a1 4398 {
ea1e477a
JS
4399 Dwarf_Die cu_mem;
4400 dw.focus_on_cu(dwarf_diecu(type_die, &cu_mem, NULL, NULL));
5f36109e 4401
c4965ad9 4402 if (e.check_pretty_print (lvalue))
5f36109e 4403 {
d19a9a82 4404 dwarf_pretty_print dpp(dw, type_die, e.operand, true, userspace_p, e);
5f36109e
JS
4405 result = dpp.expand();
4406 return;
4407 }
4408
40a393cd 4409 code = dw.literal_stmt_for_pointer (type_die, &e, lvalue, &endtype);
ea1e477a
JS
4410 }
4411 catch (const semantic_error& er)
4412 {
4413 // NB: we can have multiple errors, since a @cast
1af1e62d
JS
4414 // may be attempted using several different modules:
4415 // @cast(ptr, "type", "module1:module2:...")
4416 e.chain (er);
c4ce66a1 4417 }
c4ce66a1 4418
5f36109e
JS
4419 if (code.empty())
4420 return;
c4ce66a1 4421
5f36109e 4422 string fname = (string(lvalue ? "_dwarf_cast_set" : "_dwarf_cast_get")
cc9001af 4423 + "_" + e.sym_name()
5f36109e 4424 + "_" + lex_cast(tick++));
c4ce66a1 4425
40a393cd 4426 result = synthetic_embedded_deref_call(dw, &endtype, fname, code,
1c0be8c7 4427 userspace_p, lvalue, &e, e.operand);
5f36109e 4428}
c4ce66a1
JS
4429
4430
fb0274bc
JS
4431void dwarf_cast_expanding_visitor::filter_special_modules(string& module)
4432{
d90053e7 4433 // look for "<path/to/header>" or "kernel<path/to/header>"
fb0274bc 4434 // for those cases, build a module including that header
d90053e7 4435 if (module[module.size() - 1] == '>' &&
60d98537 4436 (module[0] == '<' || startswith(module, "kernel<")))
fb0274bc 4437 {
d77be314
JS
4438 string header = module;
4439 map<string,string>::const_iterator it = compiled_headers.find(header);
4440 if (it != compiled_headers.end())
4441 {
4442 module = it->second;
4443 return;
4444 }
4445
fb0274bc
JS
4446 string cached_module;
4447 if (s.use_cache)
4448 {
4449 // see if the cached module exists
a2639cb7 4450 cached_module = find_typequery_hash(s, module);
d105f664 4451 if (!cached_module.empty() && !s.poison_cache)
fb0274bc
JS
4452 {
4453 int fd = open(cached_module.c_str(), O_RDONLY);
4454 if (fd != -1)
4455 {
4456 if (s.verbose > 2)
b530b5b3
LB
4457 //TRANSLATORS: Here we're using a cached module.
4458 clog << _("Pass 2: using cached ") << cached_module << endl;
d77be314 4459 compiled_headers[header] = module = cached_module;
fb0274bc
JS
4460 close(fd);
4461 return;
4462 }
4463 }
4464 }
4465
4466 // no cached module, time to make it
d90053e7 4467 if (make_typequery(s, module) == 0)
fb0274bc 4468 {
e16dc041 4469 // try to save typequery in the cache
fb0274bc 4470 if (s.use_cache)
e16dc041 4471 copy_file(module, cached_module, s.verbose > 2);
d77be314 4472 compiled_headers[header] = module;
fb0274bc
JS
4473 }
4474 }
4475}
4476
4477
c4ce66a1
JS
4478void dwarf_cast_expanding_visitor::visit_cast_op (cast_op* e)
4479{
4480 bool lvalue = is_active_lvalue(e);
4481 if (lvalue && !s.guru_mode)
dc09353a 4482 throw SEMANTIC_ERROR(_("write to @cast context variable not permitted; need stap -g"), e->tok);
c4ce66a1
JS
4483
4484 if (e->module.empty())
4485 e->module = "kernel"; // "*" may also be reasonable to search all kernel modules
4486
5f36109e 4487 functioncall* result = NULL;
8b31197b
JS
4488
4489 // split the module string by ':' for alternatives
4490 vector<string> modules;
4491 tokenize(e->module, modules, ":");
b5a0dd41 4492 bool userspace_p=false; // PR10601
5f36109e 4493 for (unsigned i = 0; !result && i < modules.size(); ++i)
c4ce66a1 4494 {
8b31197b 4495 string& module = modules[i];
fb0274bc 4496 filter_special_modules(module);
abb41d92 4497
c4ce66a1
JS
4498 // NB: This uses '/' to distinguish between kernel modules and userspace,
4499 // which means that userspace modules won't get any PATH searching.
4500 dwflpp* dw;
707bf35e
JS
4501 try
4502 {
b5a0dd41
FCE
4503 userspace_p=is_user_module (module);
4504 if (! userspace_p)
707bf35e
JS
4505 {
4506 // kernel or kernel module target
ae2552da 4507 dw = db.get_kern_dw(s, module);
707bf35e
JS
4508 }
4509 else
4510 {
05fb3e0c 4511 module = find_executable (module, "", s.sysenv); // canonicalize it
707bf35e
JS
4512 dw = db.get_user_dw(s, module);
4513 }
4514 }
4515 catch (const semantic_error& er)
4516 {
4517 /* ignore and go to the next module */
4518 continue;
4519 }
c4ce66a1 4520
5f36109e 4521 dwarf_cast_query q (*dw, module, *e, lvalue, userspace_p, result);
06de3a04 4522 dw->iterate_over_modules<base_query>(&query_module, &q);
c4ce66a1 4523 }
abb41d92 4524
5f36109e 4525 if (!result)
c4ce66a1 4526 {
946e1a48
JS
4527 // We pass the unresolved cast_op to the next pass, and hope
4528 // that this value ends up not being referenced after all, so
4529 // it can be optimized out quietly.
c4ce66a1
JS
4530 provide (e);
4531 return;
4532 }
4533
c4ce66a1 4534 if (lvalue)
f55efafe 4535 provide_lvalue_call (result);
c4ce66a1 4536
5f36109e 4537 result->visit (this);
77de5e9e
GH
4538}
4539
4540
37efef73
JS
4541static bool resolve_pointer_type(Dwarf_Die& die, bool& isptr);
4542
4543exp_type_dwarf::exp_type_dwarf(dwflpp* dw, Dwarf_Die* die,
4544 bool userspace_p, bool addressof):
4545 dw(dw), die(*die), userspace_p(userspace_p), is_pointer(false)
4546{
4547 // is_pointer tells us whether a value is a pointer to the given type, so we
4548 // can dereference it; otherwise it will be treated as an end point.
4549 if (addressof)
4550 // we're already looking at the pointed-to type
4551 is_pointer = true;
4552 else
4553 // use the same test as tracepoints to see what we have
4554 resolve_pointer_type(this->die, is_pointer);
4555}
4556
4557
66facfa3
JS
4558functioncall *
4559exp_type_dwarf::expand(autocast_op* e, bool lvalue)
251707c8 4560{
66facfa3 4561 static unsigned tick = 0;
251707c8 4562
66facfa3
JS
4563 try
4564 {
37efef73
JS
4565 // make sure we're not dereferencing base types or void
4566 bool deref_p = is_pointer && !null_die(&die);
4567 if (!deref_p)
4568 e->assert_no_components("autocast", true);
4569
fb437f4e
JS
4570 if (lvalue && !dw->sess.guru_mode)
4571 throw SEMANTIC_ERROR(_("write not permitted; need stap -g"), e->tok);
4572
37efef73
JS
4573 if (e->components.empty())
4574 {
4575 if (e->addressof)
4576 throw SEMANTIC_ERROR(_("cannot take address of tracepoint variable"), e->tok);
4577
4578 // no components and no addressof? how did this autocast come to be?
4579 throw SEMANTIC_ERROR(_("internal error: no-op autocast encountered"), e->tok);
4580 }
4581
66facfa3 4582 Dwarf_Die cu_mem;
611a848e
JS
4583 if (!null_die(&die))
4584 dw->focus_on_cu(dwarf_diecu(&die, &cu_mem, NULL, NULL));
251707c8 4585
c4965ad9 4586 if (e->check_pretty_print (lvalue))
66facfa3 4587 {
37efef73 4588 dwarf_pretty_print dpp(*dw, &die, e->operand, deref_p, userspace_p, *e);
66facfa3
JS
4589 return dpp.expand();
4590 }
4591
4592 Dwarf_Die endtype;
4593 string code = dw->literal_stmt_for_pointer (&die, e, lvalue, &endtype);
251707c8 4594
66facfa3
JS
4595 string fname = (string(lvalue ? "_dwarf_autocast_set" : "_dwarf_autocast_get")
4596 + "_" + lex_cast(tick++));
4597
4598 return synthetic_embedded_deref_call(*dw, &endtype, fname, code,
4599 userspace_p, lvalue, e, e->operand);
4600 }
4601 catch (const semantic_error &er)
4602 {
4603 e->chain (er);
4604 return NULL;
4605 }
251707c8
JS
4606}
4607
4608
bd1fcbad
YZ
4609struct dwarf_atvar_expanding_visitor: public var_expanding_visitor
4610{
4611 systemtap_session& s;
4612 dwarf_builder& db;
4613
4614 dwarf_atvar_expanding_visitor(systemtap_session& s, dwarf_builder& db):
4615 s(s), db(db) {}
4616 void visit_atvar_op (atvar_op* e);
4617};
4618
4619
4620struct dwarf_atvar_query: public base_query
4621{
4622 atvar_op& e;
4623 const bool userspace_p, lvalue;
4624 functioncall*& result;
4625 unsigned& tick;
4626 const string cu_name_pattern;
4627
4628 dwarf_atvar_query(dwflpp& dw, const string& module, atvar_op& e,
4629 const bool userspace_p, const bool lvalue,
4630 functioncall*& result,
4631 unsigned& tick):
4632 base_query(dw, module), e(e),
4633 userspace_p(userspace_p), lvalue(lvalue), result(result),
4634 tick(tick), cu_name_pattern(string("*/") + e.cu_name) {}
4635
4636 void handle_query_module ();
4637 void query_library (const char *) {}
4638 void query_plt (const char *entry, size_t addr) {}
5c378838 4639 static int atvar_query_cu (Dwarf_Die *cudie, dwarf_atvar_query *q);
bd1fcbad
YZ
4640};
4641
4642
4643int
5c378838 4644dwarf_atvar_query::atvar_query_cu (Dwarf_Die * cudie, dwarf_atvar_query *q)
bd1fcbad 4645{
bd1fcbad
YZ
4646 if (! q->e.cu_name.empty())
4647 {
c60517ca 4648 const char *die_name = dwarf_diename(cudie) ?: "";
bd1fcbad
YZ
4649 if (strcmp(die_name, q->e.cu_name.c_str()) != 0 // Perfect match
4650 && fnmatch(q->cu_name_pattern.c_str(), die_name, 0) != 0)
4651 {
4652 return DWARF_CB_OK;
4653 }
4654 }
4655
4656 try
4657 {
4658 vector<Dwarf_Die> scopes(1, *cudie);
4659
4660 q->dw.focus_on_cu (cudie);
4661
c4965ad9 4662 if (q->e.check_pretty_print (q->lvalue))
bd1fcbad
YZ
4663 {
4664 dwarf_pretty_print dpp (q->dw, scopes, 0, q->e.sym_name(),
4665 q->userspace_p, q->e);
4666 q->result = dpp.expand();
4667 return DWARF_CB_ABORT;
4668 }
4669
40a393cd 4670 Dwarf_Die endtype;
bd1fcbad 4671 string code = q->dw.literal_stmt_for_local (scopes, 0, q->e.sym_name(),
40a393cd 4672 &q->e, q->lvalue, &endtype);
bd1fcbad
YZ
4673
4674 if (code.empty())
4675 return DWARF_CB_OK;
4676
4677 string fname = (string(q->lvalue ? "_dwarf_tvar_set"
4678 : "_dwarf_tvar_get")
4679 + "_" + q->e.sym_name()
4680 + "_" + lex_cast(q->tick++));
4681
40a393cd 4682 q->result = synthetic_embedded_deref_call (q->dw, &endtype, fname, code,
bd1fcbad
YZ
4683 q->userspace_p, q->lvalue,
4684 &q->e);
4685 }
4686 catch (const semantic_error& er)
4687 {
4688 // Here we suppress the error because we often just have too many
4689 // when scanning all the CUs.
4690 return DWARF_CB_OK;
4691 }
4692
4693 if (q->result) {
4694 return DWARF_CB_ABORT;
4695 }
4696
4697 return DWARF_CB_OK;
4698}
4699
4700
4701void
4702dwarf_atvar_query::handle_query_module ()
4703{
4704
4705 dw.iterate_over_cus(atvar_query_cu, this, false);
4706}
4707
4708
4709void
4710dwarf_atvar_expanding_visitor::visit_atvar_op (atvar_op* e)
4711{
4712 const bool lvalue = is_active_lvalue(e);
4713 if (lvalue && !s.guru_mode)
dc09353a 4714 throw SEMANTIC_ERROR(_("write to @var variable not permitted; "
bd1fcbad
YZ
4715 "need stap -g"), e->tok);
4716
4717 if (e->module.empty())
4718 e->module = "kernel";
4719
4720 functioncall* result = NULL;
4721
4722 // split the module string by ':' for alternatives
4723 vector<string> modules;
4724 tokenize(e->module, modules, ":");
4725 bool userspace_p = false;
4726 for (unsigned i = 0; !result && i < modules.size(); ++i)
4727 {
4728 string& module = modules[i];
4729
4730 dwflpp* dw;
4731 try
4732 {
4733 userspace_p = is_user_module(module);
4734 if (!userspace_p)
4735 {
4736 // kernel or kernel module target
4737 dw = db.get_kern_dw(s, module);
4738 }
4739 else
4740 {
4741 module = find_executable(module, "", s.sysenv);
4742 dw = db.get_user_dw(s, module);
4743 }
4744 }
4745 catch (const semantic_error& er)
4746 {
4747 /* ignore and go to the next module */
4748 continue;
4749 }
4750
4751 dwarf_atvar_query q (*dw, module, *e, userspace_p, lvalue, result, tick);
06de3a04 4752 dw->iterate_over_modules<base_query>(&query_module, &q);
bd1fcbad
YZ
4753
4754 if (result)
4755 {
4756 s.unwindsym_modules.insert(module);
bd1fcbad
YZ
4757
4758 if (lvalue)
f55efafe 4759 provide_lvalue_call (result);
bd1fcbad
YZ
4760
4761 result->visit(this);
4762 return;
4763 }
4764
4765 /* Unable to find the variable in the current module, so we chain
4766 * an error in atvar_op */
dc09353a 4767 semantic_error er(ERR_SRC, _F("unable to find global '%s' in %s%s%s",
bd1fcbad
YZ
4768 e->sym_name().c_str(), module.c_str(),
4769 e->cu_name.empty() ? "" : _(", in "),
4770 e->cu_name.c_str()));
4771 e->chain (er);
4772 }
4773
4774 provide(e);
4775}
4776
4777
b8da0ad1
FCE
4778void
4779dwarf_derived_probe::printsig (ostream& o) const
4780{
4781 // Instead of just printing the plain locations, we add a PC value
4782 // as a comment as a way of telling e.g. apart multiple inlined
4783 // function instances. This is distinct from the verbose/clog
4784 // output, since this part goes into the cache hash calculations.
4785 sole_location()->print (o);
6d0f3f0c 4786 o << " /* pc=" << section << "+0x" << hex << addr << dec << " */";
b8da0ad1
FCE
4787 printsig_nested (o);
4788}
4789
4790
4791
dc38c0ae 4792void
b20febf3
FCE
4793dwarf_derived_probe::join_group (systemtap_session& s)
4794{
af234c40
JS
4795 // skip probes which are paired entry-handlers
4796 if (!has_return && (saved_longs || saved_strings))
4797 return;
4798
b20febf3
FCE
4799 if (! s.dwarf_derived_probes)
4800 s.dwarf_derived_probes = new dwarf_derived_probe_group ();
4801 s.dwarf_derived_probes->enroll (this);
ca6d3b0f
JL
4802 this->group = s.dwarf_derived_probes;
4803 if (has_return && entry_handler)
4804 entry_handler->group = s.dwarf_derived_probes;
b20febf3
FCE
4805}
4806
4807
2b69faaf
JS
4808static bool
4809kernel_supports_inode_uprobes(systemtap_session& s)
4810{
4811 // The arch-supports is new to the builtin inode-uprobes, so it makes a
4812 // reasonable indicator of the new API. Else we'll need an autoconf...
64e807c2 4813 // see also buildrun.cxx:kernel_built_uprobs()
2b69faaf
JS
4814 return (s.kernel_config["CONFIG_ARCH_SUPPORTS_UPROBES"] == "y"
4815 && s.kernel_config["CONFIG_UPROBES"] == "y");
4816}
4817
4818
3667d615
JS
4819static bool
4820kernel_supports_inode_uretprobes(systemtap_session& s)
4821{
766013af
JS
4822 // We need inode-uprobes first, then look for a sign of uretprobes. The only
4823 // non-static function at present is arch_uretprobe_hijack_return_addr.
3667d615 4824 return kernel_supports_inode_uprobes(s) &&
af9e147f 4825 (s.kernel_functions.count("arch_uretprobe_hijack_return_addr") > 0);
3667d615
JS
4826}
4827
4828
5261f7ab
DS
4829void
4830check_process_probe_kernel_support(systemtap_session& s)
4831{
4832 // If we've got utrace, we're good to go.
4833 if (s.kernel_config["CONFIG_UTRACE"] == "y")
4834 return;
4835
8c021542
DS
4836 // We don't have utrace. For process probes that aren't
4837 // uprobes-based, we just need the task_finder. The task_finder
b266d318
DS
4838 // needs CONFIG_TRACEPOINTS and specific tracepoints. There is a
4839 // specific autoconf test for its needs.
8c021542
DS
4840 //
4841 // We'll just require CONFIG_TRACEPOINTS here as a quick-and-dirty
4842 // approximation.
4843 if (! s.need_uprobes && s.kernel_config["CONFIG_TRACEPOINTS"] == "y")
4844 return;
4845
d3e959b0
DS
4846 // For uprobes-based process probes, we need the task_finder plus
4847 // the builtin inode-uprobes.
8c021542
DS
4848 if (s.need_uprobes
4849 && s.kernel_config["CONFIG_TRACEPOINTS"] == "y"
d3e959b0 4850 && kernel_supports_inode_uprobes(s))
8c021542
DS
4851 return;
4852
dc09353a 4853 throw SEMANTIC_ERROR (_("process probes not available without kernel CONFIG_UTRACE or CONFIG_TRACEPOINTS/CONFIG_ARCH_SUPPORTS_UPROBES/CONFIG_UPROBES"));
5261f7ab
DS
4854}
4855
4856
b20febf3
FCE
4857dwarf_derived_probe::dwarf_derived_probe(const string& funcname,
4858 const string& filename,
4859 int line,
91af0778 4860 // module & section specify a relocation
b20febf3
FCE
4861 // base for <addr>, unless section==""
4862 // (equivalently module=="kernel")
4766b1e6
JL
4863 // for userspace, it's a full path, for
4864 // modules, it's either a full path, or
4865 // the basename (e.g. 'btrfs')
b20febf3
FCE
4866 const string& module,
4867 const string& section,
4868 // NB: dwfl_addr is the virtualized
4869 // address for this symbol.
4870 Dwarf_Addr dwfl_addr,
4871 // addr is the section-offset for
4872 // actual relocation.
4873 Dwarf_Addr addr,
4874 dwarf_query& q,
37ebca01 4875 Dwarf_Die* scope_die /* may be null */)
4c5d1300 4876 : derived_probe (q.base_probe, q.base_loc, true /* .components soon rewritten */ ),
b20febf3 4877 module (module), section (section), addr (addr),
63b4fd14 4878 path (q.path),
27dc09b1 4879 has_process (q.has_process),
c9bad430
DS
4880 has_return (q.has_return),
4881 has_maxactive (q.has_maxactive),
c57ea854 4882 has_library (q.has_library),
6b66b9f7 4883 maxactive_val (q.maxactive_val),
b642c901
SC
4884 user_path (q.user_path),
4885 user_lib (q.user_lib),
af234c40 4886 access_vars(false),
c57ea854 4887 saved_longs(0), saved_strings(0),
af234c40 4888 entry_handler(0)
bd2b1e68 4889{
4766b1e6
JL
4890 // If we were given a fullpath to a kernel module, then get the simple name
4891 if (q.has_module && is_fully_resolved(module, q.dw.sess.sysroot, q.dw.sess.sysenv))
4892 this->module = modname_from_path(module);
4893
b642c901
SC
4894 if (user_lib.size() != 0)
4895 has_library = true;
4896
6b66b9f7
JS
4897 if (q.has_process)
4898 {
4899 // We may receive probes on two types of ELF objects: ET_EXEC or ET_DYN.
4900 // ET_EXEC ones need no further relocation on the addr(==dwfl_addr), whereas
4901 // ET_DYN ones do (addr += run-time mmap base address). We tell these apart
4902 // by the incoming section value (".absolute" vs. ".dynamic").
4903 // XXX Assert invariants here too?
2b69faaf
JS
4904
4905 // inode-uprobes needs an offset rather than an absolute VM address.
4441e344 4906 // ditto for userspace runtimes (dyninst)
ac3af990 4907 if ((kernel_supports_inode_uprobes(q.dw.sess) || q.dw.sess.runtime_usermode_p()) &&
2b69faaf
JS
4908 section == ".absolute" && addr == dwfl_addr &&
4909 addr >= q.dw.module_start && addr < q.dw.module_end)
4910 this->addr = addr - q.dw.module_start;
6b66b9f7
JS
4911 }
4912 else
4913 {
4914 // Assert kernel relocation invariants
4915 if (section == "" && dwfl_addr != addr) // addr should be absolute
dc09353a 4916 throw SEMANTIC_ERROR (_("missing relocation basis"), tok);
6b66b9f7 4917 if (section != "" && dwfl_addr == addr) // addr should be an offset
dc09353a 4918 throw SEMANTIC_ERROR (_("inconsistent relocation address"), tok);
6b66b9f7 4919 }
2930abc7 4920
21beacc9
FCE
4921 // XXX: hack for strange g++/gcc's
4922#ifndef USHRT_MAX
4923#define USHRT_MAX 32767
4924#endif
4925
606fd9c8 4926 // Range limit maxactive() value
6b66b9f7 4927 if (has_maxactive && (maxactive_val < 0 || maxactive_val > USHRT_MAX))
dc09353a 4928 throw SEMANTIC_ERROR (_F("maxactive value out of range [0,%s]",
b530b5b3 4929 lex_cast(USHRT_MAX).c_str()), q.base_loc->components.front()->tok);
606fd9c8 4930
a7696882
JL
4931 // Expand target variables in the probe body. Even if the scope_die is
4932 // invalid, we still want to expand things such as $$vars/$$parms/etc...
4933 // (PR15999, PR16473). Access to specific context vars e.g. $argc will not be
4934 // expanded and will produce an error during the typeresolution_info pass.
8fc05e57 4935 {
6b66b9f7 4936 // XXX: user-space deref's for q.has_process!
5a617dc6
JL
4937
4938 // PR14436: if we're expanding target variables in the probe body of a
4939 // .return probe, we need to make the expansion at the postprologue addr
4940 // instead (if any), which is then also the spot where the entry handler
4941 // probe is placed. (Note that at this point, a nonzero prologue_end
4942 // implies that it should be used, i.e. code is unoptimized).
4943 Dwarf_Addr handler_dwfl_addr = dwfl_addr;
4944 if (q.prologue_end != 0 && q.has_return)
4945 {
4946 handler_dwfl_addr = q.prologue_end;
4947 if (q.sess.verbose > 2)
4948 clog << _F("expanding .return vars at prologue_end (0x%s) "
4949 "rather than entrypc (0x%s)\n",
4950 lex_cast_hex(handler_dwfl_addr).c_str(),
4951 lex_cast_hex(dwfl_addr).c_str());
4952 }
4953 dwarf_var_expanding_visitor v (q, scope_die, handler_dwfl_addr);
8b095b45 4954 v.replace (this->body);
3689db05
SC
4955
4956 // Propagate perf.counters so we can emit later
4957 this->perf_counter_refs = v.perf_counter_refs;
4958 // Emit local var used to save the perf counter read value
aa2c662f 4959 std::set<string>::iterator pcii;
3689db05
SC
4960 for (pcii = v.perf_counter_refs.begin();
4961 pcii != v.perf_counter_refs.end(); pcii++)
4962 {
aa2c662f 4963 std::vector<std::pair<std::string,std::string> >::iterator it;
3689db05
SC
4964 // Find the associated perf counter probe
4965 for (it=q.sess.perf_counters.begin() ;
4966 it != q.sess.perf_counters.end();
4967 it++)
aa2c662f 4968 if ((*it).first == (*pcii))
3689db05
SC
4969 break;
4970 vardecl* vd = new vardecl;
4971 vd->name = "__perf_read_" + (*it).first;
4972 vd->tok = this->tok;
4973 vd->set_arity(0, this->tok);
4974 vd->type = pe_long;
4975 vd->synthetic = true;
4976 this->locals.push_back (vd);
4977 }
4978
4979
6b66b9f7
JS
4980 if (!q.has_process)
4981 access_vars = v.visited;
37ebca01
FCE
4982
4983 // If during target-variable-expanding the probe, we added a new block
4984 // of code, add it to the start of the probe.
4985 if (v.add_block)
ba6f838d 4986 this->body = new block(v.add_block, this->body);
2260f4e3
FCE
4987
4988 // If when target-variable-expanding the probe, we need to synthesize a
4989 // sibling function-entry probe. We don't go through the whole probe derivation
4990 // business (PR10642) that could lead to wildcard/alias resolution, or for that
4991 // dwarf-induced duplication.
4992 if (v.add_call_probe)
37ebca01 4993 {
2260f4e3
FCE
4994 assert (q.has_return && !q.has_call);
4995
4996 // We temporarily replace q.base_probe.
4997 statement* old_body = q.base_probe->body;
4998 q.base_probe->body = v.add_call_probe;
4999 q.has_return = false;
5000 q.has_call = true;
af234c40 5001
da23eceb 5002 if (q.has_process)
5a617dc6
JL
5003 {
5004 // Place handler probe at the same addr as where the vars were
5005 // expanded (which may not be the same addr as the one for the
5006 // main retprobe, PR14436).
5007 Dwarf_Addr handler_addr = addr;
5008 if (handler_dwfl_addr != dwfl_addr)
5009 // adjust section offset by prologue_end-entrypc
5010 handler_addr += handler_dwfl_addr - dwfl_addr;
5011 entry_handler = new uprobe_derived_probe (funcname, filename,
5012 line, module, section,
5013 handler_dwfl_addr,
5014 handler_addr, q,
5015 scope_die);
5016 }
da23eceb 5017 else
af234c40
JS
5018 entry_handler = new dwarf_derived_probe (funcname, filename, line,
5019 module, section, dwfl_addr,
5020 addr, q, scope_die);
5021
5022 saved_longs = entry_handler->saved_longs = v.saved_longs;
5023 saved_strings = entry_handler->saved_strings = v.saved_strings;
5024
5025 q.results.push_back (entry_handler);
2260f4e3
FCE
5026
5027 q.has_return = true;
5028 q.has_call = false;
5029 q.base_probe->body = old_body;
37ebca01 5030 }
a7696882
JL
5031 // Save the local variables for listing mode. If the scope_die is null,
5032 // local vars aren't accessible, so no need to invoke saveargs (PR10820).
bba368c5
JL
5033 if (!null_die(scope_die) &&
5034 q.sess.dump_mode == systemtap_session::dump_matched_probes_vars)
5035 saveargs(q, scope_die, dwfl_addr);
8fc05e57 5036 }
0a98fd42 5037
5d23847d 5038 // Reset the sole element of the "locations" vector as a
b20febf3
FCE
5039 // "reverse-engineered" form of the incoming (q.base_loc) probe
5040 // point. This allows a user to see what function / file / line
5041 // number any particular match of the wildcards.
919debfc
JL
5042
5043 vector<probe_point::component*> comps;
5044 if (q.has_kernel)
5045 comps.push_back (new probe_point::component(TOK_KERNEL));
5046 else if(q.has_module)
5047 comps.push_back (new probe_point::component(TOK_MODULE, new literal_string(module)));
5048 else if(q.has_process)
5049 comps.push_back (new probe_point::component(TOK_PROCESS, new literal_string(module)));
5050 else
5051 assert (0);
5052
5053 string fn_or_stmt;
5054 if (q.has_function_str || q.has_function_num)
e772a6e7 5055 fn_or_stmt = TOK_FUNCTION;
919debfc 5056 else
e772a6e7 5057 fn_or_stmt = TOK_STATEMENT;
919debfc
JL
5058
5059 if (q.has_function_str || q.has_statement_str)
5060 {
79954c0f 5061 string retro_name = q.final_function_name(funcname, filename.c_str(), line);
919debfc
JL
5062 comps.push_back
5063 (new probe_point::component
5064 (fn_or_stmt, new literal_string (retro_name)));
5065 }
5066 else if (q.has_function_num || q.has_statement_num)
5067 {
5068 Dwarf_Addr retro_addr;
5069 if (q.has_function_num)
5070 retro_addr = q.function_num_val;
5071 else
5072 retro_addr = q.statement_num_val;
5073 comps.push_back (new probe_point::component
5074 (fn_or_stmt,
5075 new literal_number(retro_addr, true)));
5076
5077 if (q.has_absolute)
5078 comps.push_back (new probe_point::component (TOK_ABSOLUTE));
5079 }
5080
5081 if (q.has_call)
5082 comps.push_back (new probe_point::component(TOK_CALL));
5083 if (q.has_exported)
5084 comps.push_back (new probe_point::component(TOK_EXPORTED));
5085 if (q.has_inline)
5086 comps.push_back (new probe_point::component(TOK_INLINE));
5087 if (has_return)
5088 comps.push_back (new probe_point::component(TOK_RETURN));
5089 if (has_maxactive)
5090 comps.push_back (new probe_point::component
5091 (TOK_MAXACTIVE, new literal_number(maxactive_val)));
5092
5093 // Overwrite it.
5094 this->sole_location()->components = comps;
e2941743
JL
5095
5096 // if it's a .callee[s[(N)]] call, add checks to the probe body so that the
5097 // user body is only 'triggered' when called from q.callers[N-1], which
5098 // itself is called from q.callers[N-2], etc... I.E.
5099 // callees(N) --> N elements in q.callers --> N checks against [u]stack(0..N-1)
5100 if ((q.has_callee || q.has_callees_num) && q.callers && !q.callers->empty())
5101 {
5102 if (q.sess.verbose > 2)
7761da8b 5103 clog << _F("adding caller checks for callee %s\n", funcname.c_str());
e2941743
JL
5104
5105 // Copy the stack and empty it out
5106 stack<Dwarf_Addr> callers(*q.callers);
5107 for (unsigned level = 1; !callers.empty(); level++,
5108 callers.pop())
5109 {
5110 Dwarf_Addr caller = callers.top();
5111
5112 // We first need to make the caller addr relocatable
5113 string caller_section;
5114 Dwarf_Addr caller_reloc;
5115 if (module == TOK_KERNEL)
5116 { // allow for relocatable kernel (see also add_probe_point())
5117 caller_reloc = caller - q.sess.sym_stext;
5118 caller_section = "_stext";
5119 }
5120 else
5121 caller_reloc = q.dw.relocate_address(caller,
5122 caller_section);
5123
5124 if (q.sess.verbose > 2)
7761da8b
JL
5125 clog << _F("adding caller check [u]stack(%d) == reloc(0x%s)\n",
5126 level, lex_cast_hex(caller_reloc).c_str());
e2941743
JL
5127
5128 // We want to add a statement like this:
6211aed9 5129 // if (!_caller_match(user, mod, sec, addr)) next;
e2941743
JL
5130 // Something similar is done in semantic_pass_conditions()
5131
5132 functioncall* check = new functioncall();
5133 check->tok = this->tok;
6211aed9 5134 check->function = "_caller_match";
e2941743
JL
5135 check->args.push_back(new literal_number(q.has_process));
5136 check->args[0]->tok = this->tok;
5137 check->args.push_back(new literal_number(level));
5138 check->args[1]->tok = this->tok;
4766b1e6 5139 check->args.push_back(new literal_string(this->module));
e2941743
JL
5140 check->args[2]->tok = this->tok;
5141 check->args.push_back(new literal_string(caller_section));
5142 check->args[3]->tok = this->tok;
5143 check->args.push_back(new literal_number(caller_reloc, true /* hex */));
5144 check->args[4]->tok = this->tok;
5145
5146 unary_expression* notexp = new unary_expression();
5147 notexp->tok = this->tok;
5148 notexp->op = "!";
5149 notexp->operand = check;
5150
5151 if_statement* ifs = new if_statement();
5152 ifs->tok = this->tok;
5153 ifs->thenblock = new next_statement();
5154 ifs->thenblock->tok = this->tok;
5155 ifs->elseblock = NULL;
5156 ifs->condition = notexp;
5157
5158 this->body = new block(ifs, this->body);
5159 }
5160 }
2930abc7
FCE
5161}
5162
bd2b1e68 5163
0a98fd42 5164void
8c67c337
JS
5165dwarf_derived_probe::saveargs(dwarf_query& q, Dwarf_Die* scope_die,
5166 Dwarf_Addr dwfl_addr)
0a98fd42 5167{
9aa8ffce 5168 if (null_die(scope_die))
0a98fd42 5169 return;
0a98fd42 5170
8c67c337 5171 bool verbose = q.sess.verbose > 2;
0a98fd42 5172
8c67c337 5173 if (verbose)
b530b5b3 5174 clog << _F("saveargs: examining '%s' (dieoffset: %#" PRIx64 ")\n", (dwarf_diename(scope_die)?: "unknown"), dwarf_dieoffset(scope_die));
0a98fd42 5175
8c67c337
JS
5176 if (has_return)
5177 {
5178 /* Only save the return value if it has a type. */
5179 string type_name;
5180 Dwarf_Die type_die;
5181 if (dwarf_attr_die (scope_die, DW_AT_type, &type_die) &&
5182 dwarf_type_name(&type_die, type_name))
5183 args.push_back("$return:"+type_name);
5184
5185 else if (verbose)
b530b5b3
LB
5186 clog << _F("saveargs: failed to retrieve type name for return value (dieoffset: %s)\n",
5187 lex_cast_hex(dwarf_dieoffset(scope_die)).c_str());
8c67c337 5188 }
d87623a1 5189
0a98fd42 5190 Dwarf_Die arg;
4ef35696
JS
5191 vector<Dwarf_Die> scopes = q.dw.getscopes(scope_die);
5192 for (unsigned i = 0; i < scopes.size(); ++i)
5193 {
5194 if (dwarf_tag(&scopes[i]) == DW_TAG_compile_unit)
5195 break; // we don't want file-level variables
5196 if (dwarf_child (&scopes[i], &arg) == 0)
5197 do
0a98fd42 5198 {
4ef35696
JS
5199 switch (dwarf_tag (&arg))
5200 {
5201 case DW_TAG_variable:
5202 case DW_TAG_formal_parameter:
5203 break;
0a98fd42 5204
4ef35696
JS
5205 default:
5206 continue;
5207 }
0a98fd42 5208
4ef35696
JS
5209 /* Ignore this local if it has no name. */
5210 const char *arg_name = dwarf_diename (&arg);
5211 if (!arg_name)
8c67c337
JS
5212 {
5213 if (verbose)
b530b5b3
LB
5214 clog << _F("saveargs: failed to retrieve name for local (dieoffset: %s)\n",
5215 lex_cast_hex(dwarf_dieoffset(&arg)).c_str());
8c67c337
JS
5216 continue;
5217 }
4ef35696
JS
5218
5219 if (verbose)
b530b5b3
LB
5220 clog << _F("saveargs: finding location for local '%s' (dieoffset: %s)\n",
5221 arg_name, lex_cast_hex(dwarf_dieoffset(&arg)).c_str());
4ef35696
JS
5222
5223 /* Ignore this local if it has no location (or not at this PC). */
5224 /* NB: It still may not be directly accessible, e.g. if it is an
5225 * aggregate type, implicit_pointer, etc., but the user can later
5226 * figure out how to access the interesting parts. */
45b02a36
FCE
5227
5228 /* XXX: Perhaps saveargs() / listings-mode should work by synthesizing
5229 * several synthetic
5230 * probe foo { $var }
5231 * probes, testing them for overall resolvability.
5232 */
5233
4ef35696
JS
5234 Dwarf_Attribute attr_mem;
5235 if (!dwarf_attr_integrate (&arg, DW_AT_const_value, &attr_mem))
5236 {
5237 Dwarf_Op *expr;
5238 size_t len;
5239 if (!dwarf_attr_integrate (&arg, DW_AT_location, &attr_mem))
5240 {
5241 if (verbose)
b530b5b3
LB
5242 clog << _F("saveargs: failed to resolve the location for local '%s' (dieoffset: %s)\n",
5243 arg_name, lex_cast_hex(dwarf_dieoffset(&arg)).c_str());
4ef35696
JS
5244 continue;
5245 }
5246 else if (!(dwarf_getlocation_addr(&attr_mem, dwfl_addr, &expr,
5247 &len, 1) == 1 && len > 0))
5248 {
45b02a36
FCE
5249 Dwarf_Addr dwfl_addr2 = q.dw.pr15123_retry_addr (dwfl_addr, & arg);
5250 if (!dwfl_addr2 || (!(dwarf_getlocation_addr(&attr_mem, dwfl_addr2, &expr,
5251 &len, 1) == 1 && len > 0))) {
5252 if (verbose)
5253 clog << _F("saveargs: local '%s' (dieoffset: %s) is not available at this address (%s)\n",
5254 arg_name, lex_cast_hex(dwarf_dieoffset(&arg)).c_str(), lex_cast_hex(dwfl_addr).c_str());
5255 continue;
5256 }
4ef35696
JS
5257 }
5258 }
5259
5260 /* Ignore this local if it has no type. */
5261 string type_name;
5262 Dwarf_Die type_die;
5263 if (!dwarf_attr_die (&arg, DW_AT_type, &type_die) ||
5264 !dwarf_type_name(&type_die, type_name))
8c67c337
JS
5265 {
5266 if (verbose)
b530b5b3
LB
5267 clog << _F("saveargs: failed to retrieve type name for local '%s' (dieoffset: %s)\n",
5268 arg_name, lex_cast_hex(dwarf_dieoffset(&arg)).c_str());
8c67c337
JS
5269 continue;
5270 }
8c67c337 5271
4ef35696
JS
5272 /* This local looks good -- save it! */
5273 args.push_back("$"+string(arg_name)+":"+type_name);
8c67c337 5274 }
4ef35696
JS
5275 while (dwarf_siblingof (&arg, &arg) == 0);
5276 }
0a98fd42
JS
5277}
5278
5279
5280void
d0bfd2ac 5281dwarf_derived_probe::getargs(std::list<std::string> &arg_set) const
0a98fd42 5282{
d0bfd2ac 5283 arg_set.insert(arg_set.end(), args.begin(), args.end());
0a98fd42
JS
5284}
5285
5286
27dc09b1 5287void
42e38653 5288dwarf_derived_probe::emit_privilege_assertion (translator_output* o)
27dc09b1
DB
5289{
5290 if (has_process)
5291 {
5292 // These probes are allowed for unprivileged users, but only in the
5293 // context of processes which they own.
5294 emit_process_owner_assertion (o);
5295 return;
5296 }
5297
5298 // Other probes must contain the default assertion which aborts
5299 // if executed by an unprivileged user.
42e38653 5300 derived_probe::emit_privilege_assertion (o);
27dc09b1
DB
5301}
5302
5303
5304void
5305dwarf_derived_probe::print_dupe_stamp(ostream& o)
5306{
5307 if (has_process)
5308 {
5309 // These probes are allowed for unprivileged users, but only in the
5310 // context of processes which they own.
5311 print_dupe_stamp_unprivileged_process_owner (o);
5312 return;
5313 }
5314
5315 // Other probes must contain the default dupe stamp
5316 derived_probe::print_dupe_stamp (o);
5317}
5318
64211010 5319
7a053d3b 5320void
20c6c071 5321dwarf_derived_probe::register_statement_variants(match_node * root,
27dc09b1 5322 dwarf_builder * dw,
42e38653 5323 privilege_t privilege)
bd2b1e68 5324{
27dc09b1 5325 root
42e38653 5326 ->bind_privilege(privilege)
27dc09b1 5327 ->bind(dw);
83eeb3ac
HK
5328 root->bind(TOK_NEAREST)
5329 ->bind_privilege(privilege)
5330 ->bind(dw);
54efe513
GH
5331}
5332
7a053d3b 5333void
fd6602a0 5334dwarf_derived_probe::register_function_variants(match_node * root,
27dc09b1 5335 dwarf_builder * dw,
42e38653 5336 privilege_t privilege)
2865d17a 5337{
27dc09b1 5338 root
42e38653 5339 ->bind_privilege(privilege)
27dc09b1 5340 ->bind(dw);
27dc09b1 5341 root->bind(TOK_CALL)
42e38653 5342 ->bind_privilege(privilege)
27dc09b1 5343 ->bind(dw);
4bda987e
SC
5344 root->bind(TOK_EXPORTED)
5345 ->bind_privilege(privilege)
5346 ->bind(dw);
27dc09b1 5347 root->bind(TOK_RETURN)
42e38653 5348 ->bind_privilege(privilege)
27dc09b1 5349 ->bind(dw);
1e035395 5350
f6be7c06
DB
5351 // For process probes / uprobes, .maxactive() is unused.
5352 if (! pr_contains (privilege, pr_stapusr))
1e035395
FCE
5353 {
5354 root->bind(TOK_RETURN)
1e035395
FCE
5355 ->bind_num(TOK_MAXACTIVE)->bind(dw);
5356 }
bd2b1e68
GH
5357}
5358
7a053d3b 5359void
27dc09b1 5360dwarf_derived_probe::register_function_and_statement_variants(
440d9b00 5361 systemtap_session& s,
27dc09b1
DB
5362 match_node * root,
5363 dwarf_builder * dw,
42e38653 5364 privilege_t privilege
27dc09b1 5365)
bd2b1e68
GH
5366{
5367 // Here we match 4 forms:
5368 //
5369 // .function("foo")
5370 // .function(0xdeadbeef)
5371 // .statement("foo")
5372 // .statement(0xdeadbeef)
5373
440d9b00 5374 match_node *fv_root = root->bind_str(TOK_FUNCTION);
42e38653 5375 register_function_variants(fv_root, dw, privilege);
7f02ca94 5376 // ROOT.function("STRING") always gets the .inline and .label variants.
440d9b00 5377 fv_root->bind(TOK_INLINE)
42e38653 5378 ->bind_privilege(privilege)
440d9b00 5379 ->bind(dw);
7f02ca94
JS
5380 fv_root->bind_str(TOK_LABEL)
5381 ->bind_privilege(privilege)
440d9b00 5382 ->bind(dw);
c31add50
JL
5383 fv_root->bind_str(TOK_CALLEE)
5384 ->bind_privilege(privilege)
5385 ->bind(dw);
5386 fv_root->bind(TOK_CALLEES)
5387 ->bind_privilege(privilege)
5388 ->bind(dw);
5389 fv_root->bind_num(TOK_CALLEES)
5390 ->bind_privilege(privilege)
5391 ->bind(dw);
440d9b00
DB
5392
5393 fv_root = root->bind_num(TOK_FUNCTION);
42e38653 5394 register_function_variants(fv_root, dw, privilege);
440d9b00
DB
5395 // ROOT.function(NUMBER).inline is deprecated in release 1.7 and removed thereafter.
5396 if (strverscmp(s.compatible.c_str(), "1.7") <= 0)
5397 {
5398 fv_root->bind(TOK_INLINE)
42e38653 5399 ->bind_privilege(privilege)
440d9b00
DB
5400 ->bind(dw);
5401 }
5402
42e38653
DB
5403 register_statement_variants(root->bind_str(TOK_STATEMENT), dw, privilege);
5404 register_statement_variants(root->bind_num(TOK_STATEMENT), dw, privilege);
bd2b1e68
GH
5405}
5406
b1615c74
JS
5407void
5408dwarf_derived_probe::register_sdt_variants(systemtap_session& s,
5409 match_node * root,
5410 dwarf_builder * dw)
5411{
5412 root->bind_str(TOK_MARK)
f66bb29a 5413 ->bind_privilege(pr_all)
b1615c74
JS
5414 ->bind(dw);
5415 root->bind_str(TOK_PROVIDER)->bind_str(TOK_MARK)
f66bb29a 5416 ->bind_privilege(pr_all)
b1615c74
JS
5417 ->bind(dw);
5418}
5419
5420void
5421dwarf_derived_probe::register_plt_variants(systemtap_session& s,
5422 match_node * root,
5423 dwarf_builder * dw)
5424{
5425 root->bind(TOK_PLT)
f66bb29a 5426 ->bind_privilege(pr_all)
b1615c74
JS
5427 ->bind(dw);
5428 root->bind_str(TOK_PLT)
f66bb29a 5429 ->bind_privilege(pr_all)
b1615c74 5430 ->bind(dw);
2e96714f
SC
5431
5432 root->bind(TOK_PLT)
5433 ->bind(TOK_RETURN)
f66bb29a 5434 ->bind_privilege(pr_all)
b1615c74 5435 ->bind(dw);
2e96714f
SC
5436 root->bind_str(TOK_PLT)
5437 ->bind(TOK_RETURN)
f66bb29a 5438 ->bind_privilege(pr_all)
b1615c74 5439 ->bind(dw);
bd2b1e68
GH
5440}
5441
5442void
c4ce66a1 5443dwarf_derived_probe::register_patterns(systemtap_session& s)
bd2b1e68 5444{
c4ce66a1 5445 match_node* root = s.pattern_root;
bd2b1e68
GH
5446 dwarf_builder *dw = new dwarf_builder();
5447
c4ce66a1
JS
5448 update_visitor *filter = new dwarf_cast_expanding_visitor(s, *dw);
5449 s.code_filters.push_back(filter);
5450
bd1fcbad
YZ
5451 filter = new dwarf_atvar_expanding_visitor(s, *dw);
5452 s.code_filters.push_back(filter);
5453
73f52eb4
DB
5454 register_function_and_statement_variants(s, root->bind(TOK_KERNEL), dw, pr_privileged);
5455 register_function_and_statement_variants(s, root->bind_str(TOK_MODULE), dw, pr_privileged);
27dc09b1
DB
5456 root->bind(TOK_KERNEL)->bind_num(TOK_STATEMENT)->bind(TOK_ABSOLUTE)
5457 ->bind(dw);
2cab6244 5458
7f02ca94
JS
5459 match_node* uprobes[] = {
5460 root->bind(TOK_PROCESS),
5461 root->bind_str(TOK_PROCESS),
5c6f9e92 5462 root->bind_num(TOK_PROCESS),
7f02ca94
JS
5463 root->bind(TOK_PROCESS)->bind_str(TOK_LIBRARY),
5464 root->bind_str(TOK_PROCESS)->bind_str(TOK_LIBRARY),
5465 };
5466 for (size_t i = 0; i < sizeof(uprobes) / sizeof(*uprobes); ++i)
5467 {
f66bb29a 5468 register_function_and_statement_variants(s, uprobes[i], dw, pr_all);
7f02ca94
JS
5469 register_sdt_variants(s, uprobes[i], dw);
5470 register_plt_variants(s, uprobes[i], dw);
5471 }
bd2b1e68
GH
5472}
5473
9020300d 5474void
3689db05
SC
5475dwarf_derived_probe::emit_probe_local_init(systemtap_session& s, translator_output * o)
5476{
aa2c662f 5477 std::set<string>::iterator pcii;
3689db05
SC
5478 for (pcii = perf_counter_refs.begin();
5479 pcii != perf_counter_refs.end();
5480 pcii++)
5481 {
aa2c662f 5482 std::vector<std::pair<std::string,std::string> >::iterator it;
3689db05
SC
5483 // Find the associated perf.counter probe
5484 unsigned i = 0;
5485 for (it=s.perf_counters.begin() ;
5486 it != s.perf_counters.end();
5487 it++, i++)
aa2c662f 5488 if ((*it).first == (*pcii))
3689db05
SC
5489 break;
5490 // place the perf counter read so it precedes stp_lock_probe
5491 o->newline() << "l->l___perf_read_" + (*it).first
5492 + " = (((int64_t) (_stp_perf_read(smp_processor_id(),"
5493 + lex_cast(i) + "))));";
5494 }
b78a0fbb 5495
b95e2b79
MH
5496 if (access_vars)
5497 {
5498 // if accessing $variables, emit bsp cache setup for speeding up
d4670309 5499 o->newline() << "#if defined __ia64__";
d9aed31e 5500 o->newline() << "bspcache(c->unwaddr, c->kregs);";
d4670309 5501 o->newline() << "#endif";
b95e2b79 5502 }
9020300d 5503}
2930abc7 5504
b20febf3 5505// ------------------------------------------------------------------------
46b84a80
DS
5506
5507void
b20febf3 5508dwarf_derived_probe_group::enroll (dwarf_derived_probe* p)
46b84a80 5509{
b20febf3 5510 probes_by_module.insert (make_pair (p->module, p));
b8da0ad1
FCE
5511
5512 // XXX: probes put at the same address should all share a
5513 // single kprobe/kretprobe, and have their handlers executed
5514 // sequentially.
b55bc428
FCE
5515}
5516
7a053d3b 5517void
775d51e5 5518dwarf_derived_probe_group::emit_module_decls (systemtap_session& s)
ec4373ff 5519{
b20febf3 5520 if (probes_by_module.empty()) return;
2930abc7 5521
775d51e5
DS
5522 s.op->newline() << "/* ---- dwarf probes ---- */";
5523
faea5e16 5524 // Let's find some stats for the embedded strings. Maybe they
606fd9c8
FCE
5525 // are small and uniform enough to justify putting char[MAX]'s into
5526 // the array instead of relocated char*'s.
faea5e16
JS
5527 size_t module_name_max = 0, section_name_max = 0;
5528 size_t module_name_tot = 0, section_name_tot = 0;
606fd9c8
FCE
5529 size_t all_name_cnt = probes_by_module.size(); // for average
5530 for (p_b_m_iterator it = probes_by_module.begin(); it != probes_by_module.end(); it++)
5531 {
5532 dwarf_derived_probe* p = it->second;
5533#define DOIT(var,expr) do { \
5534 size_t var##_size = (expr) + 1; \
5535 var##_max = max (var##_max, var##_size); \
5536 var##_tot += var##_size; } while (0)
5537 DOIT(module_name, p->module.size());
5538 DOIT(section_name, p->section.size());
606fd9c8
FCE
5539#undef DOIT
5540 }
5541
5542 // Decide whether it's worthwhile to use char[] or char* by comparing
5543 // the amount of average waste (max - avg) to the relocation data size
5544 // (3 native long words).
5545#define CALCIT(var) \
5546 if ((var##_name_max-(var##_name_tot/all_name_cnt)) < (3 * sizeof(void*))) \
5547 { \
2d9ec661
JL
5548 s.op->newline() << "#define STAP_DWARF_PROBE_STR_" << #var << " " \
5549 << "const char " << #var \
5550 << "[" << var##_name_max << "]"; \
606fd9c8
FCE
5551 if (s.verbose > 2) clog << "stap_dwarf_probe " << #var \
5552 << "[" << var##_name_max << "]" << endl; \
5553 } \
5554 else \
5555 { \
2d9ec661
JL
5556 s.op->newline() << "#define STAP_DWARF_PROBE_STR_" << #var << " " \
5557 << "const char * const " << #var << ""; \
606fd9c8
FCE
5558 if (s.verbose > 2) clog << "stap_dwarf_probe *" << #var << endl; \
5559 }
5560
5561 CALCIT(module);
5562 CALCIT(section);
2d9ec661 5563
e6fe60e7 5564#undef CALCIT
606fd9c8 5565
2d9ec661
JL
5566 s.op->newline() << "#include \"linux/kprobes.c\"";
5567
5568#define UNDEFIT(var) s.op->newline() << "#undef STAP_DWARF_PROBE_STR_" << #var
5569 UNDEFIT(module);
5570 UNDEFIT(section);
5571#undef UNDEFIT
5572
5573 // Emit an array of kprobe/kretprobe pointers
5574 s.op->newline() << "#if defined(STAPCONF_UNREGISTER_KPROBES)";
5575 s.op->newline() << "static void * stap_unreg_kprobes[" << probes_by_module.size() << "];";
5576 s.op->newline() << "#endif";
5577
5578 // Emit the actual probe list.
5579
5580 // NB: we used to plop a union { struct kprobe; struct kretprobe } into
5581 // struct stap_dwarf_probe, but it being initialized data makes it add
5582 // hundreds of bytes of padding per stap_dwarf_probe. (PR5673)
5583 s.op->newline() << "static struct stap_dwarf_kprobe stap_dwarf_kprobes[" << probes_by_module.size() << "];";
5584 // NB: bss!
5585
5586 s.op->newline() << "static struct stap_dwarf_probe stap_dwarf_probes[] = {";
b20febf3
FCE
5587 s.op->indent(1);
5588
ddafd8ce 5589 size_t stap_dwarf_kprobe_idx = 0;
b20febf3 5590 for (p_b_m_iterator it = probes_by_module.begin(); it != probes_by_module.end(); it++)
2930abc7 5591 {
b20febf3
FCE
5592 dwarf_derived_probe* p = it->second;
5593 s.op->newline() << "{";
5594 if (p->has_return)
5595 s.op->line() << " .return_p=1,";
c9bad430 5596 if (p->has_maxactive)
606fd9c8
FCE
5597 {
5598 s.op->line() << " .maxactive_p=1,";
5599 assert (p->maxactive_val >= 0 && p->maxactive_val <= USHRT_MAX);
5600 s.op->line() << " .maxactive_val=" << p->maxactive_val << ",";
5601 }
af234c40
JS
5602 if (p->saved_longs || p->saved_strings)
5603 {
5604 if (p->saved_longs)
5605 s.op->line() << " .saved_longs=" << p->saved_longs << ",";
5606 if (p->saved_strings)
5607 s.op->line() << " .saved_strings=" << p->saved_strings << ",";
5608 if (p->entry_handler)
c87ae2c1 5609 s.op->line() << " .entry_probe=" << common_probe_init (p->entry_handler) << ",";
af234c40 5610 }
b350f56b
JS
5611 if (p->locations[0]->optional)
5612 s.op->line() << " .optional_p=1,";
dc38c256 5613 s.op->line() << " .address=(unsigned long)0x" << hex << p->addr << dec << "ULL,";
84048984
FCE
5614 s.op->line() << " .module=\"" << p->module << "\",";
5615 s.op->line() << " .section=\"" << p->section << "\",";
faea5e16 5616 s.op->line() << " .probe=" << common_probe_init (p) << ",";
ddafd8ce 5617 s.op->line() << " .kprobe=&stap_dwarf_kprobes[" << stap_dwarf_kprobe_idx++ << "],";
b20febf3 5618 s.op->line() << " },";
2930abc7 5619 }
2930abc7 5620
b20febf3
FCE
5621 s.op->newline(-1) << "};";
5622
5623 // Emit the kprobes callback function
5624 s.op->newline();
5625 s.op->newline() << "static int enter_kprobe_probe (struct kprobe *inst,";
5626 s.op->line() << " struct pt_regs *regs) {";
606fd9c8
FCE
5627 // NB: as of PR5673, the kprobe|kretprobe union struct is in BSS
5628 s.op->newline(1) << "int kprobe_idx = ((uintptr_t)inst-(uintptr_t)stap_dwarf_kprobes)/sizeof(struct stap_dwarf_kprobe);";
5629 // Check that the index is plausible
5630 s.op->newline() << "struct stap_dwarf_probe *sdp = &stap_dwarf_probes[";
5631 s.op->line() << "((kprobe_idx >= 0 && kprobe_idx < " << probes_by_module.size() << ")?";
5632 s.op->line() << "kprobe_idx:0)"; // NB: at least we avoid memory corruption
5633 // XXX: it would be nice to give a more verbose error though; BUG_ON later?
5634 s.op->line() << "];";
71db462b 5635 common_probe_entryfn_prologue (s, "STAP_SESSION_RUNNING", "sdp->probe",
cda141c2 5636 "stp_probe_type_kprobe");
d9aed31e 5637 s.op->newline() << "c->kregs = regs;";
6415ddde
MW
5638
5639 // Make it look like the IP is set as it wouldn't have been replaced
5640 // by a breakpoint instruction when calling real probe handler. Reset
5641 // IP regs on return, so we don't confuse kprobes. PR10458
5642 s.op->newline() << "{";
5643 s.op->indent(1);
d9aed31e 5644 s.op->newline() << "unsigned long kprobes_ip = REG_IP(c->kregs);";
259d54c0 5645 s.op->newline() << "SET_REG_IP(regs, (unsigned long) inst->addr);";
26e63673 5646 s.op->newline() << "(*sdp->probe->ph) (c);";
259d54c0 5647 s.op->newline() << "SET_REG_IP(regs, kprobes_ip);";
6415ddde
MW
5648 s.op->newline(-1) << "}";
5649
ef1337ee 5650 common_probe_entryfn_epilogue (s, true, otf_safe_context(s));
b20febf3
FCE
5651 s.op->newline() << "return 0;";
5652 s.op->newline(-1) << "}";
5653
5654 // Same for kretprobes
5655 s.op->newline();
af234c40
JS
5656 s.op->newline() << "static int enter_kretprobe_common (struct kretprobe_instance *inst,";
5657 s.op->line() << " struct pt_regs *regs, int entry) {";
b20febf3 5658 s.op->newline(1) << "struct kretprobe *krp = inst->rp;";
606fd9c8
FCE
5659
5660 // NB: as of PR5673, the kprobe|kretprobe union struct is in BSS
a36378d7 5661 s.op->newline() << "int kprobe_idx = ((uintptr_t)krp-(uintptr_t)stap_dwarf_kprobes)/sizeof(struct stap_dwarf_kprobe);";
606fd9c8
FCE
5662 // Check that the index is plausible
5663 s.op->newline() << "struct stap_dwarf_probe *sdp = &stap_dwarf_probes[";
5664 s.op->line() << "((kprobe_idx >= 0 && kprobe_idx < " << probes_by_module.size() << ")?";
5665 s.op->line() << "kprobe_idx:0)"; // NB: at least we avoid memory corruption
5666 // XXX: it would be nice to give a more verbose error though; BUG_ON later?
5667 s.op->line() << "];";
5668
7c3e97f4 5669 s.op->newline() << "const struct stap_probe *sp = entry ? sdp->entry_probe : sdp->probe;";
c87ae2c1
JS
5670 s.op->newline() << "if (sp) {";
5671 s.op->indent(1);
71db462b 5672 common_probe_entryfn_prologue (s, "STAP_SESSION_RUNNING", "sp",
cda141c2 5673 "stp_probe_type_kretprobe");
d9aed31e 5674 s.op->newline() << "c->kregs = regs;";
af234c40
JS
5675
5676 // for assisting runtime's backtrace logic and accessing kretprobe data packets
6dceb5c9
MW
5677 s.op->newline() << "c->ips.krp.pi = inst;";
5678 s.op->newline() << "c->ips.krp.pi_longs = sdp->saved_longs;";
6415ddde
MW
5679
5680 // Make it look like the IP is set as it wouldn't have been replaced
5681 // by a breakpoint instruction when calling real probe handler. Reset
5682 // IP regs on return, so we don't confuse kprobes. PR10458
5683 s.op->newline() << "{";
d9aed31e 5684 s.op->newline(1) << "unsigned long kprobes_ip = REG_IP(c->kregs);";
c87ae2c1
JS
5685 s.op->newline() << "if (entry)";
5686 s.op->newline(1) << "SET_REG_IP(regs, (unsigned long) inst->rp->kp.addr);";
5687 s.op->newline(-1) << "else";
5688 s.op->newline(1) << "SET_REG_IP(regs, (unsigned long)inst->ret_addr);";
5689 s.op->newline(-1) << "(sp->ph) (c);";
259d54c0 5690 s.op->newline() << "SET_REG_IP(regs, kprobes_ip);";
6415ddde
MW
5691 s.op->newline(-1) << "}";
5692
ef1337ee 5693 common_probe_entryfn_epilogue (s, true, otf_safe_context(s));
c87ae2c1 5694 s.op->newline(-1) << "}";
b20febf3
FCE
5695 s.op->newline() << "return 0;";
5696 s.op->newline(-1) << "}";
af234c40 5697
14cf7e42 5698 s.op->newline();
20c6c071 5699}
ec4373ff 5700
20c6c071 5701
dc38c0ae 5702void
b20febf3
FCE
5703dwarf_derived_probe_group::emit_module_init (systemtap_session& s)
5704{
66cab35f 5705 if (probes_by_module.empty()) return;
195dfadc 5706
66cab35f 5707 s.op->newline() << "/* ---- dwarf probes ---- */";
54ff4cd7 5708
66cab35f
JL
5709 // We'll let stapkp_init() handle reporting errors by setting probe_point to
5710 // NULL.
5711 s.op->newline() << "probe_point = NULL;";
54ff4cd7 5712
66cab35f
JL
5713 s.op->newline() << "rc = stapkp_init( "
5714 << "stap_dwarf_probes, "
66cab35f 5715 << "ARRAY_SIZE(stap_dwarf_probes));";
dc38c0ae
DS
5716}
5717
5718
b4be7cbc
FCE
5719void
5720dwarf_derived_probe_group::emit_module_refresh (systemtap_session& s)
5721{
97ab978d 5722 if (probes_by_module.empty()) return;
b4be7cbc 5723
97ab978d 5724 s.op->newline() << "/* ---- dwarf probes ---- */";
b4be7cbc 5725
97ab978d 5726 s.op->newline() << "stapkp_refresh( "
19d62b5b 5727 << "modname, "
97ab978d 5728 << "stap_dwarf_probes, "
97ab978d
JL
5729 << "ARRAY_SIZE(stap_dwarf_probes));";
5730}
b4be7cbc
FCE
5731
5732
46b84a80 5733void
b20febf3 5734dwarf_derived_probe_group::emit_module_exit (systemtap_session& s)
46b84a80 5735{
c29d98ec 5736 if (probes_by_module.empty()) return;
42cb22bd 5737
c29d98ec
JL
5738 s.op->newline() << "/* ---- dwarf probes ---- */";
5739
5740 s.op->newline() << "stapkp_exit( "
5741 << "stap_dwarf_probes, "
c29d98ec 5742 << "ARRAY_SIZE(stap_dwarf_probes));";
46b84a80
DS
5743}
5744
272c9036
WF
5745static void sdt_v3_tokenize(const string& str, vector<string>& tokens)
5746{
5747 string::size_type pos;
5748 string::size_type lastPos = str.find_first_not_of(" ", 0);
5749 string::size_type nextAt = str.find("@", lastPos);
e9a90eee
JS
5750
5751 if (nextAt == string::npos)
5752 {
5753 // PR13934: Assembly probes are not forced to use the N@OP form.
5754 // In this case, N is inferred to be the native word size. Since we
5755 // don't have a nice delimiter, just split it on spaces. SDT-asm authors
5756 // then must not put any spaces in arguments, to avoid ambiguity.
5757 tokenize(str, tokens, " ");
5758 return;
5759 }
5760
272c9036
WF
5761 while (lastPos != string::npos)
5762 {
5763 pos = nextAt + 1;
5764 nextAt = str.find("@", pos);
5765 if (nextAt == string::npos)
5766 pos = string::npos;
5767 else
5768 pos = str.rfind(" ", nextAt);
5769
5770 tokens.push_back(str.substr(lastPos, pos - lastPos));
5771 lastPos = str.find_first_not_of(" ", pos);
5772 }
5773}
8aabf152 5774
8aabf152 5775
aff5d390 5776struct sdt_uprobe_var_expanding_visitor: public var_expanding_visitor
7a05f484 5777{
ae1418f0 5778 enum regwidths {QI, QIh, HI, SI, DI};
f83336a5 5779 sdt_uprobe_var_expanding_visitor(systemtap_session& s,
b8688ee6 5780 dwflpp& dw,
f83336a5
FCE
5781 int elf_machine,
5782 const string & process_name,
a794dbeb 5783 const string & provider_name,
aff5d390 5784 const string & probe_name,
71e5e13d 5785 stap_sdt_probe_type probe_type,
aff5d390 5786 const string & arg_string,
8aabf152 5787 int ac):
b8688ee6
JL
5788 session (s), dw (dw), elf_machine (elf_machine),
5789 process_name (process_name), provider_name (provider_name),
5790 probe_name (probe_name), probe_type (probe_type), arg_count ((unsigned) ac)
a8ec7719 5791 {
a57b7534
JL
5792 // sanity check that we're not somehow here for a kernel probe
5793 assert(is_user_module(process_name));
ae1418f0 5794
2e6d617a 5795 build_dwarf_registers();
f83336a5 5796
ebbd2b45 5797 need_debug_info = false;
88e39987 5798 if (probe_type == uprobe3_type)
272c9036
WF
5799 {
5800 sdt_v3_tokenize(arg_string, arg_tokens);
5801 assert(arg_count <= 12);
5802 }
88e39987 5803 else
272c9036
WF
5804 {
5805 tokenize(arg_string, arg_tokens, " ");
5806 assert(arg_count <= 10);
5807 }
a8ec7719 5808 }
8aabf152 5809
f83336a5 5810 systemtap_session& session;
b8688ee6 5811 dwflpp& dw;
332ba7e7 5812 int elf_machine;
aff5d390 5813 const string & process_name;
a794dbeb 5814 const string & provider_name;
aff5d390 5815 const string & probe_name;
71e5e13d 5816 stap_sdt_probe_type probe_type;
8aabf152 5817 unsigned arg_count;
aff5d390 5818 vector<string> arg_tokens;
2e6d617a 5819
46a94997 5820 map<string, pair<unsigned,int> > dwarf_regs;
2e6d617a
JL
5821 string regnames;
5822 string percent_regnames;
5823
ebbd2b45 5824 bool need_debug_info;
aff5d390 5825
2e6d617a 5826 void build_dwarf_registers();
aff5d390 5827 void visit_target_symbol (target_symbol* e);
03c45fcb 5828 unsigned get_target_symbol_argno_and_validate (target_symbol* e);
ac074daf 5829 long parse_out_arg_precision(string& asmarg);
0fbf6fb8
JL
5830 expression* try_parse_arg_literal (target_symbol *e,
5831 const string& asmarg,
5832 long precision);
95b78bc9
JL
5833 expression* try_parse_arg_register (target_symbol *e,
5834 const string& asmarg,
5835 long precision);
8da56b62
JL
5836 expression* try_parse_arg_offset_register (target_symbol *e,
5837 const string& asmarg,
5838 long precision);
10fcca15
JL
5839 expression* try_parse_arg_effective_addr (target_symbol *e,
5840 const string& asmarg,
5841 long precision);
451c0c25
JL
5842 expression* try_parse_arg_varname (target_symbol *e,
5843 const string& asmarg,
5844 long precision);
6ef331c8
SC
5845 void visit_target_symbol_arg (target_symbol* e);
5846 void visit_target_symbol_context (target_symbol* e);
bd1fcbad 5847 void visit_atvar_op (atvar_op* e);
40a0c64e 5848 void visit_cast_op (cast_op* e);
aff5d390
SC
5849};
5850
2e6d617a
JL
5851void
5852sdt_uprobe_var_expanding_visitor::build_dwarf_registers ()
5853{
5854 /* Register name mapping table depends on the elf machine of this particular
5855 probe target process/file, not upon the host. So we can't just
5856 #ifdef _i686_ etc. */
5857
5858#define DRI(name,num,width) dwarf_regs[name]=make_pair(num,width)
5859 if (elf_machine == EM_X86_64) {
5860 DRI ("%rax", 0, DI); DRI ("%eax", 0, SI); DRI ("%ax", 0, HI);
5861 DRI ("%al", 0, QI); DRI ("%ah", 0, QIh);
5862 DRI ("%rdx", 1, DI); DRI ("%edx", 1, SI); DRI ("%dx", 1, HI);
5863 DRI ("%dl", 1, QI); DRI ("%dh", 1, QIh);
5864 DRI ("%rcx", 2, DI); DRI ("%ecx", 2, SI); DRI ("%cx", 2, HI);
5865 DRI ("%cl", 2, QI); DRI ("%ch", 2, QIh);
5866 DRI ("%rbx", 3, DI); DRI ("%ebx", 3, SI); DRI ("%bx", 3, HI);
5867 DRI ("%bl", 3, QI); DRI ("%bh", 3, QIh);
5868 DRI ("%rsi", 4, DI); DRI ("%esi", 4, SI); DRI ("%si", 4, HI);
5869 DRI ("%sil", 4, QI);
5870 DRI ("%rdi", 5, DI); DRI ("%edi", 5, SI); DRI ("%di", 5, HI);
5871 DRI ("%dil", 5, QI);
5872 DRI ("%rbp", 6, DI); DRI ("%ebp", 6, SI); DRI ("%bp", 6, HI);
5873 DRI ("%bpl", 6, QI);
5874 DRI ("%rsp", 7, DI); DRI ("%esp", 7, SI); DRI ("%sp", 7, HI);
5875 DRI ("%spl", 7, QI);
5876 DRI ("%r8", 8, DI); DRI ("%r8d", 8, SI); DRI ("%r8w", 8, HI);
5877 DRI ("%r8b", 8, QI);
5878 DRI ("%r9", 9, DI); DRI ("%r9d", 9, SI); DRI ("%r9w", 9, HI);
5879 DRI ("%r9b", 9, QI);
5880 DRI ("%r10", 10, DI); DRI ("%r10d", 10, SI); DRI ("%r10w", 10, HI);
5881 DRI ("%r10b", 10, QI);
5882 DRI ("%r11", 11, DI); DRI ("%r11d", 11, SI); DRI ("%r11w", 11, HI);
5883 DRI ("%r11b", 11, QI);
5884 DRI ("%r12", 12, DI); DRI ("%r12d", 12, SI); DRI ("%r12w", 12, HI);
5885 DRI ("%r12b", 12, QI);
5886 DRI ("%r13", 13, DI); DRI ("%r13d", 13, SI); DRI ("%r13w", 13, HI);
5887 DRI ("%r13b", 13, QI);
5888 DRI ("%r14", 14, DI); DRI ("%r14d", 14, SI); DRI ("%r14w", 14, HI);
5889 DRI ("%r14b", 14, QI);
5890 DRI ("%r15", 15, DI); DRI ("%r15d", 15, SI); DRI ("%r15w", 15, HI);
5891 DRI ("%r15b", 15, QI);
5892 DRI ("%rip", 16, DI); DRI ("%eip", 16, SI); DRI ("%ip", 16, HI);
5893 } else if (elf_machine == EM_386) {
5894 DRI ("%eax", 0, SI); DRI ("%ax", 0, HI); DRI ("%al", 0, QI);
5895 DRI ("%ah", 0, QIh);
5896 DRI ("%ecx", 1, SI); DRI ("%cx", 1, HI); DRI ("%cl", 1, QI);
5897 DRI ("%ch", 1, QIh);
5898 DRI ("%edx", 2, SI); DRI ("%dx", 2, HI); DRI ("%dl", 2, QI);
5899 DRI ("%dh", 2, QIh);
5900 DRI ("%ebx", 3, SI); DRI ("%bx", 3, HI); DRI ("%bl", 3, QI);
5901 DRI ("%bh", 3, QIh);
5902 DRI ("%esp", 4, SI); DRI ("%sp", 4, HI);
5903 DRI ("%ebp", 5, SI); DRI ("%bp", 5, HI);
5904 DRI ("%esi", 6, SI); DRI ("%si", 6, HI); DRI ("%sil", 6, QI);
5905 DRI ("%edi", 7, SI); DRI ("%di", 7, HI); DRI ("%dil", 7, QI);
5906 } else if (elf_machine == EM_PPC || elf_machine == EM_PPC64) {
5907 DRI ("%r0", 0, DI);
5908 DRI ("%r1", 1, DI);
5909 DRI ("%r2", 2, DI);
5910 DRI ("%r3", 3, DI);
5911 DRI ("%r4", 4, DI);
5912 DRI ("%r5", 5, DI);
5913 DRI ("%r6", 6, DI);
5914 DRI ("%r7", 7, DI);
5915 DRI ("%r8", 8, DI);
5916 DRI ("%r9", 9, DI);
5917 DRI ("%r10", 10, DI);
5918 DRI ("%r11", 11, DI);
5919 DRI ("%r12", 12, DI);
5920 DRI ("%r13", 13, DI);
5921 DRI ("%r14", 14, DI);
5922 DRI ("%r15", 15, DI);
5923 DRI ("%r16", 16, DI);
5924 DRI ("%r17", 17, DI);
5925 DRI ("%r18", 18, DI);
5926 DRI ("%r19", 19, DI);
5927 DRI ("%r20", 20, DI);
5928 DRI ("%r21", 21, DI);
5929 DRI ("%r22", 22, DI);
5930 DRI ("%r23", 23, DI);
5931 DRI ("%r24", 24, DI);
5932 DRI ("%r25", 25, DI);
5933 DRI ("%r26", 26, DI);
5934 DRI ("%r27", 27, DI);
5935 DRI ("%r28", 28, DI);
5936 DRI ("%r29", 29, DI);
5937 DRI ("%r30", 30, DI);
5938 DRI ("%r31", 31, DI);
5939 // PR11821: unadorned register "names" without -mregnames
5940 DRI ("0", 0, DI);
5941 DRI ("1", 1, DI);
5942 DRI ("2", 2, DI);
5943 DRI ("3", 3, DI);
5944 DRI ("4", 4, DI);
5945 DRI ("5", 5, DI);
5946 DRI ("6", 6, DI);
5947 DRI ("7", 7, DI);
5948 DRI ("8", 8, DI);
5949 DRI ("9", 9, DI);
5950 DRI ("10", 10, DI);
5951 DRI ("11", 11, DI);
5952 DRI ("12", 12, DI);
5953 DRI ("13", 13, DI);
5954 DRI ("14", 14, DI);
5955 DRI ("15", 15, DI);
5956 DRI ("16", 16, DI);
5957 DRI ("17", 17, DI);
5958 DRI ("18", 18, DI);
5959 DRI ("19", 19, DI);
5960 DRI ("20", 20, DI);
5961 DRI ("21", 21, DI);
5962 DRI ("22", 22, DI);
5963 DRI ("23", 23, DI);
5964 DRI ("24", 24, DI);
5965 DRI ("25", 25, DI);
5966 DRI ("26", 26, DI);
5967 DRI ("27", 27, DI);
5968 DRI ("28", 28, DI);
5969 DRI ("29", 29, DI);
5970 DRI ("30", 30, DI);
5971 DRI ("31", 31, DI);
5972 } else if (elf_machine == EM_S390) {
5973 DRI ("%r0", 0, DI);
5974 DRI ("%r1", 1, DI);
5975 DRI ("%r2", 2, DI);
5976 DRI ("%r3", 3, DI);
5977 DRI ("%r4", 4, DI);
5978 DRI ("%r5", 5, DI);
5979 DRI ("%r6", 6, DI);
5980 DRI ("%r7", 7, DI);
5981 DRI ("%r8", 8, DI);
5982 DRI ("%r9", 9, DI);
5983 DRI ("%r10", 10, DI);
5984 DRI ("%r11", 11, DI);
5985 DRI ("%r12", 12, DI);
5986 DRI ("%r13", 13, DI);
5987 DRI ("%r14", 14, DI);
5988 DRI ("%r15", 15, DI);
5989 } else if (elf_machine == EM_ARM) {
5990 DRI ("r0", 0, SI);
5991 DRI ("r1", 1, SI);
5992 DRI ("r2", 2, SI);
5993 DRI ("r3", 3, SI);
5994 DRI ("r4", 4, SI);
5995 DRI ("r5", 5, SI);
5996 DRI ("r6", 6, SI);
5997 DRI ("r7", 7, SI);
5998 DRI ("r8", 8, SI);
5999 DRI ("r9", 9, SI);
6000 DRI ("r10", 10, SI); DRI ("sl", 10, SI);
6001 DRI ("fp", 11, SI);
6002 DRI ("ip", 12, SI);
6003 DRI ("sp", 13, SI);
6004 DRI ("lr", 14, SI);
6005 DRI ("pc", 15, SI);
6006 } else if (elf_machine == EM_AARCH64) {
6007 DRI ("x0", 0, DI); DRI ("w0", 0, SI);
6008 DRI ("x1", 1, DI); DRI ("w1", 1, SI);
6009 DRI ("x2", 2, DI); DRI ("w2", 2, SI);
6010 DRI ("x3", 3, DI); DRI ("w3", 3, SI);
6011 DRI ("x4", 4, DI); DRI ("w4", 4, SI);
6012 DRI ("x5", 5, DI); DRI ("w5", 5, SI);
6013 DRI ("x6", 6, DI); DRI ("w6", 6, SI);
6014 DRI ("x7", 7, DI); DRI ("w7", 7, SI);
6015 DRI ("x8", 8, DI); DRI ("w8", 8, SI);
6016 DRI ("x9", 9, DI); DRI ("w9", 9, SI);
6017 DRI ("x10", 10, DI); DRI ("w10", 10, SI);
6018 DRI ("x11", 11, DI); DRI ("w11", 11, SI);
6019 DRI ("x12", 12, DI); DRI ("w12", 12, SI);
6020 DRI ("x13", 13, DI); DRI ("w13", 13, SI);
6021 DRI ("x14", 14, DI); DRI ("w14", 14, SI);
6022 DRI ("x15", 15, DI); DRI ("w15", 15, SI);
6023 DRI ("x16", 16, DI); DRI ("w16", 16, SI);
6024 DRI ("x17", 17, DI); DRI ("w17", 17, SI);
6025 DRI ("x18", 18, DI); DRI ("w18", 18, SI);
6026 DRI ("x19", 19, DI); DRI ("w19", 19, SI);
6027 DRI ("x20", 20, DI); DRI ("w20", 20, SI);
6028 DRI ("x21", 21, DI); DRI ("w21", 21, SI);
6029 DRI ("x22", 22, DI); DRI ("w22", 22, SI);
6030 DRI ("x23", 23, DI); DRI ("w23", 23, SI);
6031 DRI ("x24", 24, DI); DRI ("w24", 24, SI);
6032 DRI ("x25", 25, DI); DRI ("w25", 25, SI);
6033 DRI ("x26", 26, DI); DRI ("w26", 26, SI);
6034 DRI ("x27", 27, DI); DRI ("w27", 27, SI);
6035 DRI ("x28", 28, DI); DRI ("w28", 28, SI);
6036 DRI ("x29", 29, DI); DRI ("w29", 29, SI);
6037 DRI ("x30", 30, DI); DRI ("w30", 30, SI);
6038 DRI ("sp", 31, DI);
6039 } else if (arg_count) {
6040 /* permit this case; just fall back to dwarf */
6041 }
6042#undef DRI
6043
6044 // Build regex pieces out of the known dwarf_regs. We keep two separate
6045 // lists: ones with the % prefix (and thus unambigiuous even despite PR11821),
6046 // and ones with no prefix (and thus only usable in unambiguous contexts).
6047 map<string,pair<unsigned,int> >::const_iterator ri;
6048 for (ri = dwarf_regs.begin(); ri != dwarf_regs.end(); ri++)
6049 {
6050 string regname = ri->first;
6051 assert (regname != "");
6052 regnames += string("|")+regname;
6053 if (regname[0]=='%')
6054 percent_regnames += string("|")+regname;
6055 }
6056
6057 // clip off leading |
6058 if (regnames != "")
6059 regnames = regnames.substr(1);
6060 if (percent_regnames != "")
6061 percent_regnames = percent_regnames.substr(1);
6062}
7a05f484
SC
6063
6064void
6ef331c8 6065sdt_uprobe_var_expanding_visitor::visit_target_symbol_context (target_symbol* e)
7a05f484 6066{
6ef331c8 6067 if (e->addressof)
dc09353a 6068 throw SEMANTIC_ERROR(_("cannot take address of context variable"), e->tok);
6ef331c8
SC
6069
6070 if (e->name == "$$name")
7a05f484 6071 {
6ef331c8
SC
6072 literal_string *myname = new literal_string (probe_name);
6073 myname->tok = e->tok;
6074 provide(myname);
6075 return;
6076 }
7a05f484 6077
6ef331c8
SC
6078 else if (e->name == "$$provider")
6079 {
6080 literal_string *myname = new literal_string (provider_name);
6081 myname->tok = e->tok;
6082 provide(myname);
6083 return;
6084 }
a794dbeb 6085
6ef331c8
SC
6086 else if (e->name == "$$vars" || e->name == "$$parms")
6087 {
6088 e->assert_no_components("sdt", true);
6ef331c8
SC
6089
6090 // Convert $$vars to sprintf of a list of vars which we recursively evaluate
63ea4244 6091
1c922ad7 6092 print_format* pf = print_format::create(e->tok, "sprintf");
6ef331c8
SC
6093
6094 for (unsigned i = 1; i <= arg_count; ++i)
6095 {
6096 if (i > 1)
6097 pf->raw_components += " ";
6098 target_symbol *tsym = new target_symbol;
6099 tsym->tok = e->tok;
6100 tsym->name = "$arg" + lex_cast(i);
6101 pf->raw_components += tsym->name;
6102 tsym->components = e->components;
6103
8c2f50c0 6104 expression *texp = require<expression> (tsym);
c4965ad9 6105 if (e->check_pretty_print ())
6ef331c8
SC
6106 pf->raw_components += "=%s";
6107 else
6108 pf->raw_components += "=%#x";
6109 pf->args.push_back(texp);
6110 }
6111
6112 pf->components = print_format::string_to_components(pf->raw_components);
6113 provide (pf);
6114 }
6115 else
6116 assert(0); // shouldn't get here
6117}
6118
03c45fcb
JL
6119unsigned
6120sdt_uprobe_var_expanding_visitor::get_target_symbol_argno_and_validate (target_symbol *e)
6ef331c8 6121{
03c45fcb
JL
6122 // parsing
6123 unsigned argno = 0;
6124 if (startswith(e->name, "$arg"))
6ef331c8 6125 {
c69a87e0 6126 try
03c45fcb
JL
6127 {
6128 argno = lex_cast<unsigned>(e->name.substr(4));
6129 }
6130 catch (const runtime_error& f)
6131 {
6132 // non-integral $arg suffix: e.g. $argKKKSDF
8aabf152 6133 argno = 0;
03c45fcb
JL
6134 }
6135 }
8aabf152 6136
03c45fcb
JL
6137 // validation
6138 if (arg_count == 0 || // a sdt.h variant without .probe-stored arg_count
6139 argno < 1 || argno > arg_count) // a $argN with out-of-range N
6140 {
6141 // NB: Either
6142 // 1) uprobe1_type $argN or $FOO (we don't know the arg_count)
6143 // 2) uprobe2_type $FOO (no probe args)
6144 // both of which get resolved later.
6145 // Throw it now, and it might be resolved by DWARF later.
6146 need_debug_info = true;
6147 throw SEMANTIC_ERROR(_("target-symbol requires debuginfo"), e->tok);
6148 }
6149 assert (arg_tokens.size() >= argno);
6150 return argno;
6151}
40fe32e0 6152
ac074daf
JL
6153long
6154sdt_uprobe_var_expanding_visitor::parse_out_arg_precision(string& asmarg)
6155{
6156 long precision;
6157 if (asmarg.find('@') != string::npos)
6158 {
6159 precision = lex_cast<int>(asmarg.substr(0, asmarg.find('@')));
6160 asmarg = asmarg.substr(asmarg.find('@')+1);
6161 }
6162 else
6163 {
6164 // V1/V2 do not have precision field so default to signed long
6165 // V3 asm does not have precision field so default to unsigned long
6166 if (probe_type == uprobe3_type)
6167 precision = sizeof(long); // this is an asm probe
71e5e13d 6168 else
ac074daf
JL
6169 precision = -sizeof(long);
6170 }
6171 return precision;
6172}
40fe32e0 6173
0fbf6fb8
JL
6174expression*
6175sdt_uprobe_var_expanding_visitor::try_parse_arg_literal (target_symbol *e,
6176 const string& asmarg,
6177 long precision)
6178{
6179 expression *argexpr = NULL;
6180
6181 // Here, we test for a numeric literal.
6182 // Only accept (signed) decimals throughout. XXX
6183
6184 // PR11821. NB: on powerpc, literals are not prefixed with $,
6185 // so this regex does not match. But that's OK, since without
6186 // -mregnames, we can't tell them apart from register numbers
6187 // anyway. With -mregnames, we could, if gcc somehow
6188 // communicated to us the presence of that option, but alas it
6189 // doesn't. http://gcc.gnu.org/PR44995.
6190 vector<string> matches;
7be1b19f
WC
6191 string regexp;
6192
6193 if (elf_machine == EM_AARCH64) {
6194 regexp = "^([-]?[0-9][0-9]*)$";
6195 } else {
6196 regexp = "^[i\\$#]([-]?[0-9][0-9]*)$";
6197 }
6198
6199 if (!regexp_match (asmarg, regexp, matches)) {
6200 string sn =matches[1];
0fbf6fb8
JL
6201 int64_t n;
6202
6203 // We have to pay attention to the size & sign, as gcc sometimes
6204 // propagates constants that don't quite match, like a negative
6205 // value to fill an unsigned type.
6206 // NB: let it throw if something happens
6207 switch (precision)
8aabf152 6208 {
0fbf6fb8
JL
6209 case -1: n = lex_cast< int8_t>(sn); break;
6210 case 1: n = lex_cast< uint8_t>(sn); break;
6211 case -2: n = lex_cast< int16_t>(sn); break;
6212 case 2: n = lex_cast<uint16_t>(sn); break;
6213 case -4: n = lex_cast< int32_t>(sn); break;
6214 case 4: n = lex_cast<uint32_t>(sn); break;
6215 default:
6216 case -8: n = lex_cast< int64_t>(sn); break;
6217 case 8: n = lex_cast<uint64_t>(sn); break;
8aabf152
FCE
6218 }
6219
0fbf6fb8
JL
6220 literal_number* ln = new literal_number(n);
6221 ln->tok = e->tok;
6222 argexpr = ln;
6223 }
b78a0fbb 6224
0fbf6fb8
JL
6225 return argexpr;
6226}
6227
95b78bc9
JL
6228expression*
6229sdt_uprobe_var_expanding_visitor::try_parse_arg_register (target_symbol *e,
6230 const string& asmarg,
6231 long precision)
6232{
6233 expression *argexpr = NULL;
6234
6235 // test for REGISTER
6236 // NB: Because PR11821, we must use percent_regnames here.
6237 string regexp;
4380196b
WC
6238 if (elf_machine == EM_PPC || elf_machine == EM_PPC64
6239 || elf_machine == EM_ARM || elf_machine == EM_AARCH64)
95b78bc9
JL
6240 regexp = "^(" + regnames + ")$";
6241 else
6242 regexp = "^(" + percent_regnames + ")$";
6243
6244 vector<string> matches;
6245 if (!regexp_match(asmarg, regexp, matches))
6246 {
6247 string regname = matches[1];
6248 map<string,pair<unsigned,int> >::iterator ri = dwarf_regs.find (regname);
6249 if (ri != dwarf_regs.end()) // known register
8aabf152 6250 {
95b78bc9
JL
6251 embedded_expr *get_arg1 = new embedded_expr;
6252 string width_adjust;
6253 switch (ri->second.second)
8aabf152 6254 {
95b78bc9
JL
6255 case QI: width_adjust = ") & 0xff)"; break;
6256 case QIh: width_adjust = ">>8) & 0xff)"; break;
6257 case HI:
6258 // preserve 16 bit register signness
6259 width_adjust = ") & 0xffff)";
6260 if (precision < 0)
6261 width_adjust += " << 48 >> 48";
6262 break;
6263 case SI:
6264 // preserve 32 bit register signness
6265 width_adjust = ") & 0xffffffff)";
6266 if (precision < 0)
6267 width_adjust += " << 32 >> 32";
6268 break;
6269 default: width_adjust = "))";
8aabf152 6270 }
95b78bc9
JL
6271 string type = "";
6272 if (probe_type == uprobe3_type)
6273 type = (precision < 0
6274 ? "(int" : "(uint") + lex_cast(abs(precision) * 8) + "_t)";
6275 type = type + "((";
6276 get_arg1->tok = e->tok;
6277 get_arg1->code = string("/* unprivileged */ /* pure */")
6278 + string(" ((int64_t)") + type
a57b7534 6279 + string("u_fetch_register(")
95b78bc9
JL
6280 + lex_cast(dwarf_regs[regname].first) + string("))")
6281 + width_adjust;
6282 argexpr = get_arg1;
8aabf152 6283 }
95b78bc9
JL
6284 }
6285 return argexpr;
6286}
40fe32e0 6287
b15f98b2
JL
6288static string
6289precision_to_function(long precision)
6290{
6291 switch (precision)
6292 {
6293 case 1: case -1:
6294 return "user_int8";
6295 case 2:
6296 return "user_uint16";
6297 case -2:
6298 return "user_int16";
6299 case 4:
6300 return "user_uint32";
6301 case -4:
6302 return "user_int32";
6303 case 8: case -8:
6304 return "user_int64";
6305 default:
6306 return "user_long";
6307 }
6308}
8aabf152 6309
8da56b62
JL
6310expression*
6311sdt_uprobe_var_expanding_visitor::try_parse_arg_offset_register (target_symbol *e,
6312 const string& asmarg,
6313 long precision)
6314{
6315 expression *argexpr = NULL;
8095a157 6316
8da56b62
JL
6317 // test for OFFSET(REGISTER) where OFFSET is +-N+-N+-N
6318 // NB: Despite PR11821, we can use regnames here, since the parentheses
6319 // make things unambiguous. (Note: gdb/stap-probe.c also parses this)
6320 // On ARM test for [REGISTER, OFFSET]
8095a157 6321
8da56b62
JL
6322 string regexp;
6323 int reg, offset1;
4380196b 6324 if (elf_machine == EM_ARM || elf_machine == EM_AARCH64)
8da56b62 6325 {
297745ba 6326 regexp = "^\\[(" + regnames + ")(,[ ]*[#]?([+-]?[0-9]+)([+-][0-9]*)?([+-][0-9]*)?)?\\]$";
8da56b62
JL
6327 reg = 1;
6328 offset1 = 3;
6329 }
6330 else
6331 {
6332 regexp = "^([+-]?[0-9]*)([+-][0-9]*)?([+-][0-9]*)?[(](" + regnames + ")[)]$";
6333 reg = 4;
6334 offset1 = 1;
6335 }
6336
6337 vector<string> matches;
6338 if (!regexp_match(asmarg, regexp, matches))
6339 {
6340 string regname;
6341 int64_t disp = 0;
6342 if (matches[reg].length())
6343 regname = matches[reg];
6344 if (dwarf_regs.find (regname) == dwarf_regs.end())
6345 throw SEMANTIC_ERROR(_F("unrecognized register '%s'", regname.c_str()));
6346
6347 for (int i=offset1; i <= (offset1 + 2); i++)
6348 if (matches[i].length())
6349 // should decode positive/negative hex/decimal
6350 // NB: let it throw if something happens
6351 disp += lex_cast<int64_t>(matches[i]);
6352
6353 // synthesize user_long(%{fetch_register(R)%} + D)
6354 embedded_expr *get_arg1 = new embedded_expr;
6355 get_arg1->tok = e->tok;
6356 get_arg1->code = string("/* unprivileged */ /* pure */")
a57b7534 6357 + string("u_fetch_register(")
8da56b62
JL
6358 + lex_cast(dwarf_regs[regname].first) + string(")");
6359 // XXX: may we ever need to cast that to a narrower type?
6360
6361 literal_number* inc = new literal_number(disp);
6362 inc->tok = e->tok;
6363
6364 binary_expression *be = new binary_expression;
6365 be->tok = e->tok;
6366 be->left = get_arg1;
6367 be->op = "+";
6368 be->right = inc;
6369
6370 functioncall *fc = new functioncall;
b15f98b2 6371 fc->function = precision_to_function(precision);
8da56b62
JL
6372 fc->tok = e->tok;
6373 fc->args.push_back(be);
8095a157 6374
8da56b62
JL
6375 argexpr = fc;
6376 }
8095a157 6377
8da56b62
JL
6378 return argexpr;
6379}
8095a157 6380
10fcca15
JL
6381expression*
6382sdt_uprobe_var_expanding_visitor::try_parse_arg_effective_addr (target_symbol *e,
6383 const string& asmarg,
6384 long precision)
6385{
6386 expression *argexpr = NULL;
6387
6388 // test for OFFSET(BASE_REGISTER,INDEX_REGISTER[,SCALE]) where OFFSET is +-N+-N+-N
6389 // NB: Despite PR11821, we can use regnames here, since the parentheses
6390 // make things unambiguous. (Note: gdb/stap-probe.c also parses this)
6391 string regexp = "^([+-]?[0-9]*)([+-][0-9]*)?([+-][0-9]*)?[(](" + regnames + "),(" +
6392 regnames + ")(,[1248])?[)]$";
6393 vector<string> matches;
6394 if (!regexp_match(asmarg, regexp, matches))
6395 {
6396 string baseregname;
6397 string indexregname;
6398 int64_t disp = 0;
6399 short scale = 1;
6400
6401 if (matches[6].length())
6402 // NB: let it throw if we can't cast
6403 scale = lex_cast<short>(matches[6].substr(1)); // NB: skip the comma!
6404
6405 if (matches[4].length())
6406 baseregname = matches[4];
6407 if (dwarf_regs.find (baseregname) == dwarf_regs.end())
6408 throw SEMANTIC_ERROR(_F("unrecognized base register '%s'", baseregname.c_str()));
6409
6410 if (matches[5].length())
6411 indexregname = matches[5];
6412 if (dwarf_regs.find (indexregname) == dwarf_regs.end())
6413 throw SEMANTIC_ERROR(_F("unrecognized index register '%s'", indexregname.c_str()));
6414
6415 for (int i = 1; i <= 3; i++) // up to three OFFSET terms
6416 if (matches[i].length())
6417 // should decode positive/negative hex/decimal
6418 // NB: let it throw if something happens
6419 disp += lex_cast<int64_t>(matches[i]);
6420
6421 // synthesize user_long(%{fetch_register(R1)+fetch_register(R2)*N%} + D)
6422
6423 embedded_expr *get_arg1 = new embedded_expr;
a57b7534 6424 string regfn = "u_fetch_register";
10fcca15
JL
6425
6426 get_arg1->tok = e->tok;
6427 get_arg1->code = string("/* unprivileged */ /* pure */")
6428 + regfn + string("(")+lex_cast(dwarf_regs[baseregname].first)+string(")")
6429 + string("+(")
6430 + regfn + string("(")+lex_cast(dwarf_regs[indexregname].first)+string(")")
6431 + string("*")
6432 + lex_cast(scale)
6433 + string(")");
6434
6435 // NB: could plop this +DISPLACEMENT bit into the embedded-c expression too
6436 literal_number* inc = new literal_number(disp);
6437 inc->tok = e->tok;
6438
6439 binary_expression *be = new binary_expression;
6440 be->tok = e->tok;
6441 be->left = get_arg1;
6442 be->op = "+";
6443 be->right = inc;
6444
6445 functioncall *fc = new functioncall;
b15f98b2 6446 fc->function = precision_to_function(precision);
10fcca15
JL
6447 fc->tok = e->tok;
6448 fc->args.push_back(be);
8095a157 6449
10fcca15
JL
6450 argexpr = fc;
6451 }
8095a157 6452
10fcca15
JL
6453 return argexpr;
6454}
8095a157 6455
451c0c25
JL
6456expression*
6457sdt_uprobe_var_expanding_visitor::try_parse_arg_varname (target_symbol *e,
6458 const string& asmarg,
6459 long precision)
6ef331c8 6460{
b8688ee6 6461 static unsigned tick = 0;
451c0c25
JL
6462 expression *argexpr = NULL;
6463
6464 // test for [OFF+]VARNAME[+OFF][(REGISTER)], where VARNAME is a variable
6465 // name. NB: Despite PR11821, we can use regnames here, since the parentheses
6466 // make things unambiguous.
6467 string regex = "^(([0-9]+)[+])?([a-zA-Z_][a-zA-Z0-9_]*)([+][0-9]+)?([(]("
6468 + regnames + ")[)])?$";
6469 vector<string> matches;
6470 if (!regexp_match(asmarg, regex, matches))
6471 {
6472 assert(matches.size() >= 4);
6473 string varname = matches[3];
6474
6475 // OFF can be before VARNAME (put in matches[2]) or after (put in
6476 // matches[4]) (or both?). Seems like in most cases it comes after,
6477 // unless the code was compiled with -fPIC.
6478 int64_t offset = 0;
6479 if (!matches[2].empty())
6480 offset += lex_cast<int64_t>(matches[2]);
6481 if (matches.size() >= 5 && !matches[4].empty())
6482 offset += lex_cast<int64_t>(matches[4]);
6483
6484 string regname;
6485 if (matches.size() >= 7)
6486 regname = matches[6];
6487
6488 // If it's just VARNAME, then proceed. If it's VARNAME(REGISTER), then
6489 // only proceed if it's RIP-relative addressing on x86_64.
6490 if (regname.empty() || (regname == "%rip" && elf_machine == EM_X86_64))
6491 {
6492 dw.mod_info->get_symtab();
6493 if (dw.mod_info->symtab_status != info_present)
6494 throw SEMANTIC_ERROR(_("can't retrieve symbol table"));
6495
6496 assert(dw.mod_info->sym_table);
6497 map<string, Dwarf_Addr>& globals = dw.mod_info->sym_table->globals;
6498 map<string, Dwarf_Addr>& locals = dw.mod_info->sym_table->locals;
6499 Dwarf_Addr addr = 0;
6500
6501 // check symtab locals then globals
6502 if (locals.count(varname))
6503 addr = locals[varname];
6504 if (globals.count(varname))
6505 addr = globals[varname];
6506
6507 if (addr)
8095a157 6508 {
451c0c25
JL
6509 // add whatever offset is in the operand
6510 addr += offset;
6511
6512 // adjust for dw bias because relocate_address() expects a
6513 // libdw address and this addr is from the symtab
6514 dw.get_module_dwarf(false, false);
6515 addr -= dw.module_bias;
6516
6517 string reloc_section;
6518 Dwarf_Addr reloc_addr = dw.relocate_address(addr, reloc_section);
6519
6520 // OK, we have an address for the variable. Let's create a
6521 // function that will just relocate it at runtime, and then
6522 // call user_[u]int*() on the address it returns.
6523
6524 functioncall *user_int_call = new functioncall;
b15f98b2 6525 user_int_call->function = precision_to_function(precision);
451c0c25
JL
6526 user_int_call->tok = e->tok;
6527
6528 functiondecl *get_addr_decl = new functiondecl;
6529 get_addr_decl->tok = e->tok;
6530 get_addr_decl->synthetic = true;
6531 get_addr_decl->name = "_sdt_arg_get_addr_" + lex_cast(tick++);
6532 get_addr_decl->type = pe_long;
6533
6bc53f02 6534 // build _stp_umodule_relocate(module, addr, current)
451c0c25
JL
6535 stringstream ss;
6536 ss << " /* unprivileged */ /* pure */ /* pragma:vma */" << endl;
a57b7534
JL
6537 ss << "STAP_RETURN(_stp_umodule_relocate(";
6538 ss << "\"" << path_remove_sysroot(session, process_name) << "\", ";
6539 ss << "0x" << hex << reloc_addr << dec << ", ";
6540 ss << "current";
6541 ss << "));" << endl;
451c0c25
JL
6542
6543 embeddedcode *ec = new embeddedcode;
6544 ec->tok = e->tok;
6545 ec->code = ss.str();
6546 get_addr_decl->body = ec;
6547 get_addr_decl->join(session);
6548
6549 functioncall *get_addr_call = new functioncall;
6550 get_addr_call->tok = e->tok;
6551 get_addr_call->function = get_addr_decl->name;
6552 user_int_call->args.push_back(get_addr_call);
b8688ee6 6553
451c0c25 6554 argexpr = user_int_call;
8095a157 6555 }
8aabf152 6556 }
451c0c25
JL
6557 }
6558
6559 return argexpr;
6560}
6561
6562void
6563sdt_uprobe_var_expanding_visitor::visit_target_symbol_arg (target_symbol *e)
6564{
6ef331c8
SC
6565 try
6566 {
03c45fcb 6567 unsigned argno = get_target_symbol_argno_and_validate(e); // the N in $argN
8aabf152 6568 string asmarg = arg_tokens[argno-1]; // $arg1 => arg_tokens[0]
8aabf152 6569
8aabf152
FCE
6570 // Now we try to parse this thing, which is an assembler operand
6571 // expression. If we can't, we warn, back down to need_debug_info
b874bd52 6572 // and hope for the best. Here is the syntax for a few architectures.
9859b766 6573 // Note that the power iN syntax is only for V3 sdt.h; gcc emits the i.
8095a157 6574 //
b6aaaf21
JL
6575 // literal reg reg reg+ base+index*size+ VAR VAR+off RIP-relative
6576 // indirect offset offset VAR+off
6577 // x86 $N %rR (%rR) N(%rR) O(%bR,%iR,S) var var+off var+off(%rip)
6578 // x86_64 $N %rR (%rR) N(%rR) O(%bR,%iR,S) var var+off var+off(%rip)
6579 // power iN R (R) N(R)
6580 // ia64 N rR [r16]
6581 // s390 N %rR 0(rR) N(r15)
6582 // arm #N rR [rR] [rR, #N]
7be1b19f 6583 // arm64 N rR [rR] [rR, N]
b874bd52 6584
8aabf152
FCE
6585 expression* argexpr = 0; // filled in in case of successful parse
6586
ac074daf
JL
6587 // Parse (and remove from asmarg) the leading length
6588 long precision = parse_out_arg_precision(asmarg);
40fe32e0 6589
0fbf6fb8 6590 try
8aabf152 6591 {
0fbf6fb8
JL
6592 if ((argexpr = try_parse_arg_literal(e, asmarg, precision)) != NULL)
6593 goto matched;
2e6d617a
JL
6594
6595 // all other matches require registers
6596 if (regnames == "")
6597 throw SEMANTIC_ERROR("no registers to use for parsing");
95b78bc9
JL
6598
6599 if ((argexpr = try_parse_arg_register(e, asmarg, precision)) != NULL)
6600 goto matched;
8da56b62
JL
6601 if ((argexpr = try_parse_arg_offset_register(e, asmarg, precision)) != NULL)
6602 goto matched;
10fcca15
JL
6603 if ((argexpr = try_parse_arg_effective_addr(e, asmarg, precision)) != NULL)
6604 goto matched;
451c0c25
JL
6605 if ((argexpr = try_parse_arg_varname(e, asmarg, precision)) != NULL)
6606 goto matched;
0fbf6fb8
JL
6607 }
6608 catch (const semantic_error& err)
6609 {
6610 e->chain(err);
b8688ee6 6611 }
8aabf152 6612
8aabf152
FCE
6613 // The asmarg operand was not recognized. Back down to dwarf.
6614 if (! session.suppress_warnings)
84fef8ee
FCE
6615 {
6616 if (probe_type == UPROBE3_TYPE)
451c0c25
JL
6617 session.print_warning (_F("Can't parse SDT_V3 operand '%s' "
6618 "[man error::sdt]", asmarg.c_str()),
6619 e->tok);
84fef8ee 6620 else // must be *PROBE2; others don't get asm operands
451c0c25
JL
6621 session.print_warning (_F("Downgrading SDT_V2 probe argument to "
6622 "dwarf, can't parse '%s' [man error::sdt]",
6623 asmarg.c_str()),
6624 e->tok);
84fef8ee 6625 }
451c0c25 6626
8aabf152 6627 need_debug_info = true;
451c0c25
JL
6628 throw SEMANTIC_ERROR(_("SDT asm not understood, requires debuginfo "
6629 "[man error::sdt]"), e->tok);
6630
6631 /* NOTREACHED */
366af4e7 6632
8aabf152
FCE
6633 matched:
6634 assert (argexpr != 0);
366af4e7
RM
6635
6636 if (session.verbose > 2)
1e41115c 6637 //TRANSLATORS: We're mapping the operand to a new expression*.
b530b5b3 6638 clog << _F("mapped asm operand %s to ", asmarg.c_str()) << *argexpr << endl;
366af4e7 6639
aff5d390 6640 if (e->components.empty()) // We have a scalar
8aabf152
FCE
6641 {
6642 if (e->addressof)
dc09353a 6643 throw SEMANTIC_ERROR(_("cannot take address of sdt variable"), e->tok);
8aabf152 6644 provide (argexpr);
8aabf152
FCE
6645 }
6646 else // $var->foo
6647 {
6648 cast_op *cast = new cast_op;
6649 cast->name = "@cast";
6650 cast->tok = e->tok;
6651 cast->operand = argexpr;
6652 cast->components = e->components;
6653 cast->type_name = probe_name + "_arg" + lex_cast(argno);
6654 cast->module = process_name;
6655 cast->visit(this);
8aabf152 6656 }
aff5d390
SC
6657 }
6658 catch (const semantic_error &er)
6659 {
6660 e->chain (er);
6661 provide (e);
6662 }
6663}
6664
6665
6ef331c8
SC
6666void
6667sdt_uprobe_var_expanding_visitor::visit_target_symbol (target_symbol* e)
6668{
6669 try
6670 {
49131a6d 6671 assert(e->name.size() > 0
bd1fcbad 6672 && (e->name[0] == '$' || e->name == "@var"));
6ef331c8
SC
6673
6674 if (e->name == "$$name" || e->name == "$$provider" || e->name == "$$parms" || e->name == "$$vars")
6675 visit_target_symbol_context (e);
6676 else
6677 visit_target_symbol_arg (e);
6678 }
6679 catch (const semantic_error &er)
6680 {
6681 e->chain (er);
6682 provide (e);
6683 }
6684}
6685
6686
bd1fcbad
YZ
6687void
6688sdt_uprobe_var_expanding_visitor::visit_atvar_op (atvar_op* e)
6689{
6690 need_debug_info = true;
6691
6692 // Fill in our current module context if needed
6693 if (e->module.empty())
6694 e->module = process_name;
6695
6696 var_expanding_visitor::visit_atvar_op(e);
6697}
6698
6699
40a0c64e
JS
6700void
6701sdt_uprobe_var_expanding_visitor::visit_cast_op (cast_op* e)
6702{
6703 // Fill in our current module context if needed
6704 if (e->module.empty())
6705 e->module = process_name;
6706
6707 var_expanding_visitor::visit_cast_op(e);
6708}
6709
6710
576eaefe
SC
6711void
6712plt_expanding_visitor::visit_target_symbol (target_symbol *e)
6713{
6714 try
6715 {
6716 if (e->name == "$$name")
6717 {
6718 literal_string *myname = new literal_string (entry);
6719 myname->tok = e->tok;
6720 provide(myname);
6721 return;
6722 }
3d69c03f
JS
6723
6724 // variable not found -> throw a semantic error
6725 // (only to be caught right away, but this may be more complex later...)
6726 string alternatives = "$$name";
dc09353a 6727 throw SEMANTIC_ERROR(_F("unable to find plt variable '%s' (alternatives: %s)",
3d69c03f 6728 e->name.c_str(), alternatives.c_str()), e->tok);
576eaefe
SC
6729 }
6730 catch (const semantic_error &er)
6731 {
6732 e->chain (er);
6733 provide (e);
6734 }
6735}
6736
6737
edce5b67
JS
6738struct sdt_query : public base_query
6739{
6740 sdt_query(probe * base_probe, probe_point * base_loc,
6741 dwflpp & dw, literal_map_t const & params,
51d6bda3 6742 vector<derived_probe *> & results, const string user_lib);
edce5b67 6743
51d6bda3 6744 void query_library (const char *data);
614f0fcf
JL
6745 set<string> visited_libraries;
6746 bool resolved_library;
6747
576eaefe 6748 void query_plt (const char *entry, size_t addr) {}
edce5b67
JS
6749 void handle_query_module();
6750
6751private:
15284963 6752 stap_sdt_probe_type probe_type;
d61ea602 6753 enum { probe_section=0, note_section=1, unknown_section=-1 } probe_loc;
edce5b67
JS
6754 probe * base_probe;
6755 probe_point * base_loc;
6846cfc8 6756 literal_map_t const & params;
edce5b67 6757 vector<derived_probe *> & results;
a794dbeb
FCE
6758 string pp_mark;
6759 string pp_provider;
51d6bda3 6760 string user_lib;
edce5b67
JS
6761
6762 set<string> probes_handled;
6763
6764 Elf_Data *pdata;
6765 size_t probe_scn_offset;
6766 size_t probe_scn_addr;
aff5d390 6767 uint64_t arg_count;
40fe32e0 6768 GElf_Addr base;
c57ea854 6769 GElf_Addr pc;
aff5d390 6770 string arg_string;
edce5b67 6771 string probe_name;
a794dbeb 6772 string provider_name;
909ab234 6773 GElf_Addr semaphore_load_offset;
79a0ca08 6774 Dwarf_Addr semaphore;
edce5b67
JS
6775
6776 bool init_probe_scn();
6b51ee12 6777 bool get_next_probe();
c57ea854
SC
6778 void iterate_over_probe_entries();
6779 void handle_probe_entry();
edce5b67 6780
488d3c1b 6781 static void setup_note_probe_entry_callback (sdt_query *me,
dbd24034
JL
6782 const string& scn_name,
6783 const string& note_name,
488d3c1b
JL
6784 int type,
6785 const char *data,
6786 size_t len);
dbd24034
JL
6787 void setup_note_probe_entry (const string& scn_name,
6788 const string& note_name, int type,
6789 const char *data, size_t len);
40fe32e0 6790
edce5b67 6791 void convert_probe(probe *base);
4ddb6dd0 6792 void record_semaphore(vector<derived_probe *> & results, unsigned start);
c72aa911 6793 probe* convert_location();
40fe32e0 6794 bool have_uprobe() {return probe_type == uprobe1_type || probe_type == uprobe2_type || probe_type == uprobe3_type;}
c57ea854
SC
6795 bool have_debuginfo_uprobe(bool need_debug_info)
6796 {return probe_type == uprobe1_type
40fe32e0 6797 || ((probe_type == uprobe2_type || probe_type == uprobe3_type)
c57ea854 6798 && need_debug_info);}
40fe32e0 6799 bool have_debuginfoless_uprobe() {return probe_type == uprobe2_type || probe_type == uprobe3_type;}
edce5b67
JS
6800};
6801
6802
6803sdt_query::sdt_query(probe * base_probe, probe_point * base_loc,
6804 dwflpp & dw, literal_map_t const & params,
51d6bda3 6805 vector<derived_probe *> & results, const string user_lib):
614f0fcf
JL
6806 base_query(dw, params), resolved_library(false),
6807 probe_type(unknown_probe_type), probe_loc(unknown_section),
6808 base_probe(base_probe), base_loc(base_loc), params(params), results(results),
6809 user_lib(user_lib), probe_scn_offset(0), probe_scn_addr(0), arg_count(0),
6810 base(0), pc(0), semaphore_load_offset(0), semaphore(0)
edce5b67 6811{
a794dbeb
FCE
6812 assert(get_string_param(params, TOK_MARK, pp_mark));
6813 get_string_param(params, TOK_PROVIDER, pp_provider); // pp_provider == "" -> unspecified
6814
ef428667
FCE
6815 // PR10245: permit usage of dtrace-y "-" separator in marker name;
6816 // map it to double-underscores.
6817 size_t pos = 0;
6818 while (1) // there may be more than one
6819 {
a794dbeb 6820 size_t i = pp_mark.find("-", pos);
ef428667 6821 if (i == string::npos) break;
a794dbeb 6822 pp_mark.replace (i, 1, "__");
ef428667
FCE
6823 pos = i+1; // resume searching after the inserted __
6824 }
a794dbeb
FCE
6825
6826 // XXX: same for pp_provider?
edce5b67
JS
6827}
6828
6829
6830void
c57ea854 6831sdt_query::handle_probe_entry()
edce5b67 6832{
c57ea854
SC
6833 if (! have_uprobe()
6834 && !probes_handled.insert(probe_name).second)
edce5b67
JS
6835 return;
6836
6837 if (sess.verbose > 3)
c57ea854 6838 {
b78a0fbb 6839 //TRANSLATORS: Describing what probe type (kprobe or uprobe) the probe
b530b5b3
LB
6840 //TRANSLATORS: is matched to.
6841 clog << _F("matched probe_name %s probe type ", probe_name.c_str());
c57ea854
SC
6842 switch (probe_type)
6843 {
6844 case uprobe1_type:
6845 clog << "uprobe1 at 0x" << hex << pc << dec << endl;
6846 break;
6847 case uprobe2_type:
6848 clog << "uprobe2 at 0x" << hex << pc << dec << endl;
6849 break;
40fe32e0
SC
6850 case uprobe3_type:
6851 clog << "uprobe3 at 0x" << hex << pc << dec << endl;
6852 break;
d61ea602
JS
6853 default:
6854 clog << "unknown!" << endl;
6855 break;
c57ea854
SC
6856 }
6857 }
edce5b67 6858
c57ea854
SC
6859 // Extend the derivation chain
6860 probe *new_base = convert_location();
6861 probe_point *new_location = new_base->locations[0];
6862
c57ea854
SC
6863 bool need_debug_info = false;
6864
7d395255
JS
6865 // We could get the Elf* from either dwarf_getelf(dwfl_module_getdwarf(...))
6866 // or dwfl_module_getelf(...). We only need it for the machine type, which
6867 // should be the same. The bias is used for relocating debuginfoless probes,
6868 // though, so that must come from the possibly-prelinked ELF file, not DWARF.
c57ea854 6869 Dwarf_Addr bias;
7d395255 6870 Elf* elf = dwfl_module_getelf (dw.mod_info->mod, &bias);
c57ea854 6871
1cc41cd6
DS
6872 /* Figure out the architecture of this particular ELF file. The
6873 dwarfless register-name mappings depend on it. */
6874 GElf_Ehdr ehdr_mem;
6875 GElf_Ehdr* em = gelf_getehdr (elf, &ehdr_mem);
e1c3b13a 6876 if (em == 0) { DWFL_ASSERT ("dwfl_getehdr", dwfl_errno()); }
d8f31d0a 6877 assert(em);
1cc41cd6 6878 int elf_machine = em->e_machine;
b8688ee6
JL
6879 sdt_uprobe_var_expanding_visitor svv (sess, dw, elf_machine, module_val,
6880 provider_name, probe_name, probe_type,
6881 arg_string, arg_count);
1cc41cd6
DS
6882 svv.replace (new_base->body);
6883 need_debug_info = svv.need_debug_info;
c57ea854 6884
1cc41cd6
DS
6885 // XXX: why not derive_probes() in the uprobes case too?
6886 literal_map_t params;
6887 for (unsigned i = 0; i < new_location->components.size(); ++i)
6888 {
6889 probe_point::component *c = new_location->components[i];
6890 params[c->functor] = c->arg;
6891 }
c57ea854 6892
73d53dd2 6893 unsigned prior_results_size = results.size();
1cc41cd6
DS
6894 dwarf_query q(new_base, new_location, dw, params, results, "", "");
6895 q.has_mark = true; // enables mid-statement probing
30263a73 6896
73d53dd2
JS
6897 // V1 probes always need dwarf info
6898 // V2+ probes need dwarf info in case of a variable reference
1cc41cd6 6899 if (have_debuginfo_uprobe(need_debug_info))
06de3a04 6900 dw.iterate_over_modules<base_query>(&query_module, &q);
73d53dd2
JS
6901
6902 // For V2+ probes, if variable references weren't used or failed (PR14369),
6903 // then try with the more direct approach. Unresolved $vars might still
6904 // cause their own error, but this gives them a chance to be optimized out.
6905 if (have_debuginfoless_uprobe() && results.size() == prior_results_size)
1cc41cd6
DS
6906 {
6907 string section;
6908 Dwarf_Addr reloc_addr = q.statement_num_val + bias;
6909 if (dwfl_module_relocations (q.dw.mod_info->mod) > 0)
6910 {
6911 dwfl_module_relocate_address (q.dw.mod_info->mod, &reloc_addr);
6912 section = ".dynamic";
6913 }
6914 else
6915 section = ".absolute";
edce5b67 6916
1cc41cd6
DS
6917 uprobe_derived_probe* p =
6918 new uprobe_derived_probe ("", "", 0,
6919 path_remove_sysroot(sess,q.module_val),
6920 section,
6921 q.statement_num_val, reloc_addr, q, 0);
6922 p->saveargs (arg_count);
6923 results.push_back (p);
c57ea854 6924 }
487bf4e2 6925 sess.unwindsym_modules.insert (dw.module_name);
73d53dd2 6926 record_semaphore(results, prior_results_size);
c57ea854 6927}
edce5b67 6928
4ddb6dd0 6929
c57ea854
SC
6930void
6931sdt_query::handle_query_module()
6932{
6933 if (!init_probe_scn())
6934 return;
edce5b67 6935
c57ea854
SC
6936 if (sess.verbose > 3)
6937 clog << "TOK_MARK: " << pp_mark << " TOK_PROVIDER: " << pp_provider << endl;
edce5b67 6938
40fe32e0
SC
6939 if (probe_loc == note_section)
6940 {
6941 GElf_Shdr shdr_mem;
6942 GElf_Shdr *shdr = dw.get_section (".stapsdt.base", &shdr_mem);
6943
909ab234
JS
6944 // The 'base' lets us adjust the hardcoded addresses in notes for prelink
6945 // effects. The 'semaphore_load_offset' accounts for the difference in
6946 // load addresses between text and data, so the semaphore can be
6947 // converted to a file offset if needed.
40fe32e0 6948 if (shdr)
909ab234
JS
6949 {
6950 base = shdr->sh_addr;
6951 GElf_Addr base_offset = shdr->sh_offset;
6952 shdr = dw.get_section (".probes", &shdr_mem);
6953 if (shdr)
6954 semaphore_load_offset =
6955 (shdr->sh_addr - shdr->sh_offset) - (base - base_offset);
6956 }
40fe32e0 6957 else
909ab234
JS
6958 base = semaphore_load_offset = 0;
6959
488d3c1b 6960 dw.iterate_over_notes (this, &sdt_query::setup_note_probe_entry_callback);
40fe32e0 6961 }
d61ea602 6962 else if (probe_loc == probe_section)
40fe32e0 6963 iterate_over_probe_entries ();
edce5b67
JS
6964}
6965
6966
6967bool
6968sdt_query::init_probe_scn()
6969{
448a86b7 6970 Elf* elf;
edce5b67 6971 GElf_Shdr shdr_mem;
40fe32e0
SC
6972
6973 GElf_Shdr *shdr = dw.get_section (".note.stapsdt", &shdr_mem);
6974 if (shdr)
6975 {
6976 probe_loc = note_section;
6977 return true;
6978 }
edce5b67 6979
448a86b7 6980 shdr = dw.get_section (".probes", &shdr_mem, &elf);
fea74777 6981 if (shdr)
edce5b67 6982 {
fea74777
SC
6983 pdata = elf_getdata_rawchunk (elf, shdr->sh_offset, shdr->sh_size, ELF_T_BYTE);
6984 probe_scn_offset = 0;
6985 probe_scn_addr = shdr->sh_addr;
6986 assert (pdata != NULL);
6987 if (sess.verbose > 4)
ce0f6648
LB
6988 clog << "got .probes elf scn_addr@0x" << probe_scn_addr << ", size: "
6989 << pdata->d_size << endl;
40fe32e0 6990 probe_loc = probe_section;
fea74777 6991 return true;
edce5b67 6992 }
fea74777 6993 else
edce5b67 6994 return false;
edce5b67
JS
6995}
6996
40fe32e0 6997void
dbd24034
JL
6998sdt_query::setup_note_probe_entry_callback (sdt_query *me,
6999 const string& scn_name,
7000 const string& note_name, int type,
7001 const char *data, size_t len)
40fe32e0 7002{
dbd24034 7003 me->setup_note_probe_entry (scn_name, note_name, type, data, len);
40fe32e0
SC
7004}
7005
7006
7007void
dbd24034
JL
7008sdt_query::setup_note_probe_entry (const string& scn_name,
7009 const string& note_name, int type,
7010 const char *data, size_t len)
40fe32e0 7011{
dbd24034
JL
7012 if (scn_name.compare(".note.stapsdt"))
7013 return;
7014#define _SDT_NOTE_NAME "stapsdt"
7015 if (note_name.compare(_SDT_NOTE_NAME))
7016 return;
40fe32e0
SC
7017#define _SDT_NOTE_TYPE 3
7018 if (type != _SDT_NOTE_TYPE)
7019 return;
7020
dbd24034 7021 // we found a probe entry
40fe32e0
SC
7022 union
7023 {
7024 Elf64_Addr a64[3];
7025 Elf32_Addr a32[3];
7026 } buf;
7027 Dwarf_Addr bias;
7028 Elf* elf = (dwfl_module_getelf (dw.mod_info->mod, &bias));
7029 Elf_Data dst =
7030 {
7031 &buf, ELF_T_ADDR, EV_CURRENT,
7032 gelf_fsize (elf, ELF_T_ADDR, 3, EV_CURRENT), 0, 0
7033 };
7034 assert (dst.d_size <= sizeof buf);
7035
7036 if (len < dst.d_size + 3)
7037 return;
7038
7039 Elf_Data src =
7040 {
7041 (void *) data, ELF_T_ADDR, EV_CURRENT,
7042 dst.d_size, 0, 0
7043 };
7044
7045 if (gelf_xlatetom (elf, &dst, &src,
7046 elf_getident (elf, NULL)[EI_DATA]) == NULL)
7047 printf ("gelf_xlatetom: %s", elf_errmsg (-1));
7048
7049 probe_type = uprobe3_type;
7050 const char * provider = data + dst.d_size;
3f803f9e 7051
40fe32e0 7052 const char *name = (const char*)memchr (provider, '\0', data + len - provider);
3f803f9e
CM
7053 if(name++ == NULL)
7054 return;
7055
7056 const char *args = (const char*)memchr (name, '\0', data + len - name);
7057 if (args++ == NULL || memchr (args, '\0', data + len - name) != data + len - 1)
7058 return;
7059
7060 provider_name = provider;
7061 probe_name = name;
7062 arg_string = args;
40fe32e0 7063
e90006d7
JL
7064 dw.mod_info->marks.insert(make_pair(provider, name));
7065
40fe32e0
SC
7066 // Did we find a matching probe?
7067 if (! (dw.function_name_matches_pattern (probe_name, pp_mark)
7068 && ((pp_provider == "")
7069 || dw.function_name_matches_pattern (provider_name, pp_provider))))
7070 return;
7071
e9a90eee
JS
7072 // PR13934: Assembly probes are not forced to use the N@OP form.
7073 // If we have '@' then great, else count based on space-delimiters.
7074 arg_count = count(arg_string.begin(), arg_string.end(), '@');
7075 if (!arg_count && !arg_string.empty())
7076 arg_count = 1 + count(arg_string.begin(), arg_string.end(), ' ');
7077
40fe32e0
SC
7078 GElf_Addr base_ref;
7079 if (gelf_getclass (elf) == ELFCLASS32)
7080 {
7081 pc = buf.a32[0];
7082 base_ref = buf.a32[1];
7083 semaphore = buf.a32[2];
7084 }
7085 else
7086 {
7087 pc = buf.a64[0];
7088 base_ref = buf.a64[1];
7089 semaphore = buf.a64[2];
7090 }
7091
7092 semaphore += base - base_ref;
7093 pc += base - base_ref;
7094
7d395255
JS
7095 // The semaphore also needs the ELF bias added now, so
7096 // record_semaphore can properly relocate it later.
7097 semaphore += bias;
7098
40fe32e0 7099 if (sess.verbose > 4)
b530b5b3 7100 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
7101
7102 handle_probe_entry();
7103}
7104
7105
c57ea854
SC
7106void
7107sdt_query::iterate_over_probe_entries()
edce5b67 7108{
c57ea854 7109 // probes are in the .probe section
edce5b67
JS
7110 while (probe_scn_offset < pdata->d_size)
7111 {
aff5d390
SC
7112 stap_sdt_probe_entry_v1 *pbe_v1 = (stap_sdt_probe_entry_v1 *) ((char*)pdata->d_buf + probe_scn_offset);
7113 stap_sdt_probe_entry_v2 *pbe_v2 = (stap_sdt_probe_entry_v2 *) ((char*)pdata->d_buf + probe_scn_offset);
15284963 7114 probe_type = (stap_sdt_probe_type)(pbe_v1->type_a);
1cc41cd6 7115 if (! have_uprobe())
edce5b67
JS
7116 {
7117 // Unless this is a mangled .probes section, this happens
7118 // because the name of the probe comes first, followed by
7119 // the sentinel.
7120 if (sess.verbose > 5)
b530b5b3 7121 clog << _F("got unknown probe_type : 0x%x", probe_type) << endl;
edce5b67
JS
7122 probe_scn_offset += sizeof(__uint32_t);
7123 continue;
7124 }
aff5d390
SC
7125 if ((long)pbe_v1 % sizeof(__uint64_t)) // we have stap_sdt_probe_entry_v1.type_b
7126 {
7127 pbe_v1 = (stap_sdt_probe_entry_v1*)((char*)pbe_v1 - sizeof(__uint32_t));
1cc41cd6 7128 if (pbe_v1->type_b != uprobe1_type)
aff5d390
SC
7129 continue;
7130 }
7131
1cc41cd6 7132 if (probe_type == uprobe1_type)
aff5d390 7133 {
79a0ca08 7134 if (pbe_v1->name == 0) // No name possibly means we have a .so with a relocation
c57ea854 7135 return;
79a0ca08 7136 semaphore = 0;
aff5d390 7137 probe_name = (char*)((char*)pdata->d_buf + pbe_v1->name - (char*)probe_scn_addr);
a794dbeb 7138 provider_name = ""; // unknown
1cc41cd6
DS
7139 pc = pbe_v1->arg;
7140 arg_count = 0;
aff5d390
SC
7141 probe_scn_offset += sizeof (stap_sdt_probe_entry_v1);
7142 }
08b5a50c 7143 else if (probe_type == uprobe2_type)
aff5d390 7144 {
79a0ca08 7145 if (pbe_v2->name == 0) // No name possibly means we have a .so with a relocation
c57ea854 7146 return;
79a0ca08 7147 semaphore = pbe_v2->semaphore;
aff5d390 7148 probe_name = (char*)((char*)pdata->d_buf + pbe_v2->name - (char*)probe_scn_addr);
a794dbeb 7149 provider_name = (char*)((char*)pdata->d_buf + pbe_v2->provider - (char*)probe_scn_addr);
aff5d390
SC
7150 arg_count = pbe_v2->arg_count;
7151 pc = pbe_v2->pc;
7152 if (pbe_v2->arg_string)
7153 arg_string = (char*)((char*)pdata->d_buf + pbe_v2->arg_string - (char*)probe_scn_addr);
79a0ca08
SC
7154 // skip over pbe_v2, probe_name text and provider text
7155 probe_scn_offset = ((long)(pbe_v2->name) - (long)(probe_scn_addr)) + probe_name.length();
7156 probe_scn_offset += sizeof (__uint32_t) - probe_scn_offset % sizeof (__uint32_t);
aff5d390 7157 }
edce5b67 7158 if (sess.verbose > 4)
b530b5b3 7159 clog << _("saw .probes ") << probe_name << (provider_name != "" ? _(" (provider ")+provider_name+") " : "")
aff5d390 7160 << "@0x" << hex << pc << dec << endl;
edce5b67 7161
e90006d7
JL
7162 dw.mod_info->marks.insert(make_pair(provider_name, probe_name));
7163
a794dbeb
FCE
7164 if (dw.function_name_matches_pattern (probe_name, pp_mark)
7165 && ((pp_provider == "") || dw.function_name_matches_pattern (provider_name, pp_provider)))
c57ea854 7166 handle_probe_entry ();
edce5b67 7167 }
edce5b67
JS
7168}
7169
7170
6846cfc8 7171void
4ddb6dd0 7172sdt_query::record_semaphore (vector<derived_probe *> & results, unsigned start)
6846cfc8 7173{
a794dbeb
FCE
7174 for (unsigned i=0; i<2; i++) {
7175 // prefer with-provider symbol; look without provider prefix for backward compatibility only
7176 string semaphore = (i==0 ? (provider_name+"_") : "") + probe_name + "_semaphore";
7177 // XXX: multiple addresses?
7178 if (sess.verbose > 2)
b530b5b3 7179 clog << _F("looking for semaphore symbol %s ", semaphore.c_str());
a794dbeb 7180
79a0ca08
SC
7181 Dwarf_Addr addr;
7182 if (this->semaphore)
7183 addr = this->semaphore;
7184 else
7185 addr = lookup_symbol_address(dw.module, semaphore.c_str());
a794dbeb
FCE
7186 if (addr)
7187 {
7d395255 7188 if (dwfl_module_relocations (dw.module) > 0)
a794dbeb
FCE
7189 dwfl_module_relocate_address (dw.module, &addr);
7190 // XXX: relocation basis?
909ab234
JS
7191
7192 // Dyninst needs the *file*-based offset for semaphores,
7193 // so subtract the difference in load addresses between .text and .probes
7194 if (dw.sess.runtime_usermode_p())
7195 addr -= semaphore_load_offset;
7196
a794dbeb
FCE
7197 for (unsigned i = start; i < results.size(); ++i)
7198 results[i]->sdt_semaphore_addr = addr;
7199 if (sess.verbose > 2)
b530b5b3 7200 clog << _(", found at 0x") << hex << addr << dec << endl;
a794dbeb
FCE
7201 return;
7202 }
7203 else
7204 if (sess.verbose > 2)
b530b5b3 7205 clog << _(", not found") << endl;
a794dbeb 7206 }
6846cfc8
SC
7207}
7208
7209
edce5b67
JS
7210void
7211sdt_query::convert_probe (probe *base)
7212{
7213 block *b = new block;
7214 b->tok = base->body->tok;
7215
edce5b67
JS
7216 // Generate: if (arg1 != mark("label")) next;
7217 functioncall *fc = new functioncall;
bbafcb1e 7218 fc->function = "ulong_arg";
edce5b67 7219 fc->tok = b->tok;
bbafcb1e 7220 literal_number* num = new literal_number(1);
edce5b67
JS
7221 num->tok = b->tok;
7222 fc->args.push_back(num);
7223
7224 functioncall *fcus = new functioncall;
7225 fcus->function = "user_string";
7226 fcus->type = pe_string;
7227 fcus->tok = b->tok;
7228 fcus->args.push_back(fc);
7229
7230 if_statement *is = new if_statement;
7231 is->thenblock = new next_statement;
7232 is->elseblock = NULL;
7233 is->tok = b->tok;
63ea4244 7234 is->thenblock->tok = b->tok;
edce5b67
JS
7235 comparison *be = new comparison;
7236 be->op = "!=";
7237 be->tok = b->tok;
7238 be->left = fcus;
7239 be->right = new literal_string(probe_name);
63ea4244 7240 be->right->tok = b->tok;
edce5b67
JS
7241 is->condition = be;
7242 b->statements.push_back(is);
7243
7244 // Now replace the body
7245 b->statements.push_back(base->body);
7246 base->body = b;
7247}
7248
7249
c72aa911
JS
7250probe*
7251sdt_query::convert_location ()
edce5b67 7252{
684b8718
JL
7253 string module = dw.module_name;
7254 if (has_process)
7255 module = path_remove_sysroot(sess, module);
7256
c72aa911 7257 probe_point* specific_loc = new probe_point(*base_loc);
684b8718
JL
7258 specific_loc->well_formed = true;
7259
662539d9 7260 vector<probe_point::component*> derived_comps;
edce5b67 7261
662539d9
JS
7262 vector<probe_point::component*>::iterator it;
7263 for (it = specific_loc->components.begin();
7264 it != specific_loc->components.end(); ++it)
7265 if ((*it)->functor == TOK_PROCESS)
7266 {
684b8718
JL
7267 // replace the possibly incomplete path to process
7268 *it = new probe_point::component(TOK_PROCESS,
7269 new literal_string(has_library ? path : module));
7270
1cc41cd6
DS
7271 // copy the process name
7272 derived_comps.push_back(*it);
662539d9
JS
7273 }
7274 else if ((*it)->functor == TOK_LIBRARY)
7275 {
1cc41cd6
DS
7276 // copy the library name for process probes
7277 derived_comps.push_back(*it);
662539d9
JS
7278 }
7279 else if ((*it)->functor == TOK_PROVIDER)
7280 {
7281 // replace the possibly wildcarded arg with the specific provider name
7282 *it = new probe_point::component(TOK_PROVIDER,
7283 new literal_string(provider_name));
7284 }
7285 else if ((*it)->functor == TOK_MARK)
c72aa911
JS
7286 {
7287 // replace the possibly wildcarded arg with the specific marker name
662539d9
JS
7288 *it = new probe_point::component(TOK_MARK,
7289 new literal_string(probe_name));
a794dbeb 7290
aff5d390
SC
7291 if (sess.verbose > 3)
7292 switch (probe_type)
7293 {
7294 case uprobe1_type:
b530b5b3 7295 clog << _("probe_type == uprobe1, use statement addr: 0x")
aff5d390
SC
7296 << hex << pc << dec << endl;
7297 break;
7298 case uprobe2_type:
b530b5b3 7299 clog << _("probe_type == uprobe2, use statement addr: 0x")
aff5d390
SC
7300 << hex << pc << dec << endl;
7301 break;
40fe32e0 7302 case uprobe3_type:
b530b5b3 7303 clog << _("probe_type == uprobe3, use statement addr: 0x")
40fe32e0
SC
7304 << hex << pc << dec << endl;
7305 break;
aff5d390 7306 default:
b530b5b3
LB
7307 clog << _F("probe_type == use_uprobe_no_dwarf, use label name: _stapprobe1_%s",
7308 pp_mark.c_str()) << endl;
aff5d390
SC
7309 }
7310
c72aa911
JS
7311 switch (probe_type)
7312 {
aff5d390
SC
7313 case uprobe1_type:
7314 case uprobe2_type:
40fe32e0 7315 case uprobe3_type:
c72aa911 7316 // process("executable").statement(probe_arg)
662539d9
JS
7317 derived_comps.push_back
7318 (new probe_point::component(TOK_STATEMENT,
7319 new literal_number(pc, true)));
c72aa911
JS
7320 break;
7321
a794dbeb 7322 default: // deprecated
c72aa911 7323 // process("executable").function("*").label("_stapprobe1_MARK_NAME")
662539d9
JS
7324 derived_comps.push_back
7325 (new probe_point::component(TOK_FUNCTION,
7326 new literal_string("*")));
7327 derived_comps.push_back
c72aa911 7328 (new probe_point::component(TOK_LABEL,
a794dbeb 7329 new literal_string("_stapprobe1_" + pp_mark)));
c72aa911
JS
7330 break;
7331 }
7332 }
edce5b67 7333
662539d9
JS
7334 probe_point* derived_loc = new probe_point(*specific_loc);
7335 derived_loc->components = derived_comps;
8159bf55 7336 return new probe (new probe (base_probe, specific_loc), derived_loc);
edce5b67
JS
7337}
7338
7339
51d6bda3
SC
7340void
7341sdt_query::query_library (const char *library)
7342{
614f0fcf
JL
7343 visited_libraries.insert(library);
7344 if (query_one_library (library, dw, user_lib, base_probe, base_loc, results))
7345 resolved_library = true;
51d6bda3
SC
7346}
7347
e90006d7
JL
7348string
7349suggest_marks(systemtap_session& sess,
7350 const set<string>& modules,
7351 const string& mark,
7352 const string& provider)
7353{
7354 if (mark.empty() || modules.empty() || sess.module_cache == NULL)
7355 return "";
7356
7357 set<string> marks;
7358 const map<string, module_info*> &cache = sess.module_cache->cache;
7359 bool dash_suggestions = (mark.find("-") != string::npos);
7360
7361 for (set<string>::iterator itmod = modules.begin();
7362 itmod != modules.end(); ++itmod)
7363 {
7364 map<string, module_info*>::const_iterator itcache;
7365 if ((itcache = cache.find(*itmod)) != cache.end())
7366 {
7367 set<pair<string,string> >::const_iterator itmarks;
7368 for (itmarks = itcache->second->marks.begin();
7369 itmarks != itcache->second->marks.end(); ++itmarks)
7370 {
7371 if (provider.empty()
7372 // simulating dw.function_name_matches_pattern()
7373 || (fnmatch(provider.c_str(), itmarks->first.c_str(), 0) == 0))
7374 {
7375 string marksug = itmarks->second;
7376 if (dash_suggestions)
7377 {
7378 size_t pos = 0;
7379 while (1) // there may be more than one
7380 {
7381 size_t i = marksug.find("__", pos);
7382 if (i == string::npos) break;
7383 marksug.replace (i, 2, "-");
7384 pos = i+1; // resume searching after the inserted -
7385 }
7386 }
7387 marks.insert(marksug);
7388 }
7389 }
7390 }
7391 }
7392
7393 if (sess.verbose > 2)
7394 {
7395 clog << "suggesting " << marks.size() << " marks "
7396 << "from modules:" << endl;
7397 for (set<string>::iterator itmod = modules.begin();
7398 itmod != modules.end(); ++itmod)
7399 clog << *itmod << endl;
7400 }
7401
7402 if (marks.empty())
7403 return "";
7404
7405 return levenshtein_suggest(mark, marks, 5); // print top 5 marks only
7406}
7407
40a70f52
JL
7408string
7409suggest_plt_functions(systemtap_session& sess,
7410 const set<string>& modules,
7411 const string& func)
7412{
7413 if (func.empty() || modules.empty() || sess.module_cache == NULL)
7414 return "";
7415
7416 set<string> funcs;
7417 const map<string, module_info*> &cache = sess.module_cache->cache;
7418
7419 for (set<string>::iterator itmod = modules.begin();
7420 itmod != modules.end(); ++itmod)
7421 {
7422 map<string, module_info*>::const_iterator itcache;
7423 if ((itcache = cache.find(*itmod)) != cache.end())
7424 funcs.insert(itcache->second->plt_funcs.begin(),
7425 itcache->second->plt_funcs.end());
7426 }
7427
7428 if (sess.verbose > 2)
d82dd71f
JL
7429 {
7430 clog << "suggesting " << funcs.size() << " plt functions "
7431 << "from modules:" << endl;
7432 for (set<string>::iterator itmod = modules.begin();
7433 itmod != modules.end(); ++itmod)
7434 clog << *itmod << endl;
7435 }
40a70f52
JL
7436
7437 if (funcs.empty())
7438 return "";
7439
7440 return levenshtein_suggest(func, funcs, 5); // print top 5 funcs only
7441}
51d6bda3 7442
0508c45f
JL
7443string
7444suggest_dwarf_functions(systemtap_session& sess,
7445 const set<string>& modules,
7446 string func)
44ffe90c
JL
7447{
7448 // Trim any @ component
7449 size_t pos = func.find('@');
7450 if (pos != string::npos)
7451 func.erase(pos);
7452
0508c45f 7453 if (func.empty() || modules.empty() || sess.module_cache == NULL)
44ffe90c
JL
7454 return "";
7455
7456 // We must first aggregate all the functions from the cache
7457 set<string> funcs;
7458 const map<string, module_info*> &cache = sess.module_cache->cache;
7459
0508c45f
JL
7460 for (set<string>::iterator itmod = modules.begin();
7461 itmod != modules.end(); ++itmod)
44ffe90c 7462 {
6e6d9a3b
JL
7463 module_info *module;
7464
7465 // retrieve module_info from cache
44ffe90c
JL
7466 map<string, module_info*>::const_iterator itcache;
7467 if ((itcache = cache.find(*itmod)) != cache.end())
6e6d9a3b
JL
7468 module = itcache->second;
7469 else // module not found
7470 continue;
7471
7472 // add inlines
7473 funcs.insert(module->inlined_funcs.begin(),
7474 module->inlined_funcs.end());
7475
7476 // add all function symbols in cache
7477 if (module->symtab_status != info_present || module->sym_table == NULL)
7478 continue;
7479 map<string, func_info*>& modfuncs = module->sym_table->map_by_name;
7480 for (map<string, func_info*>::const_iterator itfuncs = modfuncs.begin();
7481 itfuncs != modfuncs.end(); ++itfuncs)
7482 funcs.insert(itfuncs->first);
44ffe90c
JL
7483 }
7484
7485 if (sess.verbose > 2)
d82dd71f
JL
7486 {
7487 clog << "suggesting " << funcs.size() << " dwarf functions "
7488 << "from modules:" << endl;
7489 for (set<string>::iterator itmod = modules.begin();
7490 itmod != modules.end(); ++itmod)
7491 clog << *itmod << endl;
7492 }
44ffe90c
JL
7493
7494 if (funcs.empty())
7495 return "";
7496
593f09eb 7497 return levenshtein_suggest(func, funcs, 5); // print top 5 funcs only
44ffe90c
JL
7498}
7499
20c6c071 7500void
5227f1ea 7501dwarf_builder::build(systemtap_session & sess,
7a053d3b 7502 probe * base,
20c6c071 7503 probe_point * location,
86bf665e 7504 literal_map_t const & parameters,
20c6c071
GH
7505 vector<derived_probe *> & finished_results)
7506{
b20febf3
FCE
7507 // NB: the kernel/user dwlfpp objects are long-lived.
7508 // XXX: but they should be per-session, as this builder object
7509 // may be reused if we try to cross-instrument multiple targets.
84048984 7510
7a24d422 7511 dwflpp* dw = 0;
6d5d594e 7512 literal_map_t filled_parameters = parameters;
7a24d422 7513
7a24d422 7514 string module_name;
5c6f9e92 7515 int64_t proc_pid;
ae2552da
FCE
7516 if (has_null_param (parameters, TOK_KERNEL))
7517 {
7518 dw = get_kern_dw(sess, "kernel");
7519 }
7520 else if (get_param (parameters, TOK_MODULE, module_name))
b8da0ad1 7521 {
8e30940d
JL
7522 // If not a full path was given, then it's an in-tree module. Replace any
7523 // dashes with underscores.
7524 if (!is_fully_resolved(module_name, sess.sysroot, sess.sysenv))
7525 {
7526 size_t dash_pos = 0;
7527 while((dash_pos=module_name.find('-'))!=string::npos)
7528 module_name.replace(int(dash_pos),1,"_");
7529 filled_parameters[TOK_MODULE] = new literal_string(module_name);
7530 }
7531
37001baa
FCE
7532 // NB: glob patterns get expanded later, during the offline
7533 // elfutils module listing.
ae2552da 7534 dw = get_kern_dw(sess, module_name);
b8da0ad1 7535 }
5c6f9e92 7536 else if (has_param(filled_parameters, TOK_PROCESS))
84c84ac4 7537 {
0ce4c32f
FCE
7538 // NB: module_name is not yet set!
7539
6d5d594e
LB
7540 if(has_null_param(filled_parameters, TOK_PROCESS))
7541 {
fc18e6c4
JL
7542 string file;
7543 try
7544 {
7545 file = sess.cmd_file();
7546 }
7547 catch (const semantic_error& e)
7548 {
6d9c4e46 7549 if(sess.target_pid)
1b6d5734 7550 throw SEMANTIC_ERROR(_("invalid -x pid for unspecified process"
6d9c4e46
AJ
7551 " probe [man stapprobes]"), NULL, NULL, &e);
7552 else
7553 throw SEMANTIC_ERROR(_("invalid -c command for unspecified process"
fc18e6c4
JL
7554 " probe [man stapprobes]"), NULL, NULL, &e);
7555 }
48cd804a 7556 if(file.empty())
fc18e6c4 7557 throw SEMANTIC_ERROR(_("unspecified process probe is invalid without"
6d9c4e46 7558 " a -c COMMAND or -x PID [man stapprobes]"));
48cd804a 7559 module_name = sess.sysroot + file;
6d5d594e 7560 filled_parameters[TOK_PROCESS] = new literal_string(module_name);// this needs to be used in place of the blank map
0ce4c32f 7561 // in the case of TOK_MARK we need to modify locations as well // XXX why?
6d5d594e 7562 if(location->components[0]->functor==TOK_PROCESS &&
0ce4c32f 7563 location->components[0]->arg == 0)
30b865ce
AJ
7564 {
7565 if (sess.target_pid)
7566 location->components[0]->arg = new literal_number(sess.target_pid);
7567 else
7568 location->components[0]->arg = new literal_string(module_name);
7569 }
5c6f9e92
AJ
7570 }
7571
0ce4c32f
FCE
7572 // NB: must specifically handle the classical ("string") form here, to make sure
7573 // we get the module_name out.
7574 else if (get_param (parameters, TOK_PROCESS, module_name))
7575 {
7576 module_name = sess.sysroot + module_name;
7577 filled_parameters[TOK_PROCESS] = new literal_string(module_name);
7578 }
7579
5c6f9e92
AJ
7580 else if (get_param (parameters, TOK_PROCESS, proc_pid))
7581 {
6c484277 7582 // check that the pid given corresponds to a running process
30b865ce 7583 string pid_err_msg;
fb9ae721 7584 if (!is_valid_pid(proc_pid, pid_err_msg))
30b865ce 7585 throw SEMANTIC_ERROR(pid_err_msg);
6c484277 7586
5c6f9e92
AJ
7587 string pid_path = string("/proc/") + lex_cast(proc_pid) + "/exe";
7588 module_name = sess.sysroot + pid_path;
37635b45 7589
0ce4c32f 7590 // in the case of TOK_MARK we need to modify locations as well // XXX why?
6d5d594e 7591 if(location->components[0]->functor==TOK_PROCESS &&
0ce4c32f 7592 location->components[0]->arg == 0)
30b865ce 7593 location->components[0]->arg = new literal_number(sess.target_pid);
b78a0fbb 7594 }
5750ecc6 7595
37001baa
FCE
7596 // PR6456 process("/bin/*") glob handling
7597 if (contains_glob_chars (module_name))
7598 {
7599 // Expand glob via rewriting the probe-point process("....")
7600 // parameter, asserted to be the first one.
7601
7602 assert (location->components.size() > 0);
7603 assert (location->components[0]->functor == TOK_PROCESS);
7604 assert (location->components[0]->arg);
7605 literal_string* lit = dynamic_cast<literal_string*>(location->components[0]->arg);
7606 assert (lit);
7607
7608 // Evaluate glob here, and call derive_probes recursively with each match.
7609 glob_t the_blob;
88f8e2c0 7610 set<string> dupes;
37001baa 7611 int rc = glob (module_name.c_str(), 0, NULL, & the_blob);
b530b5b3 7612 if (rc)
dc09353a 7613 throw SEMANTIC_ERROR (_F("glob %s error (%s)", module_name.c_str(), lex_cast(rc).c_str() ));
44ffe90c 7614 unsigned results_pre = finished_results.size();
37001baa
FCE
7615 for (unsigned i = 0; i < the_blob.gl_pathc; ++i)
7616 {
e19ebcf7 7617 assert_no_interrupts();
37001baa
FCE
7618
7619 const char* globbed = the_blob.gl_pathv[i];
7620 struct stat st;
7621
7622 if (access (globbed, X_OK) == 0
7623 && stat (globbed, &st) == 0
7624 && S_ISREG (st.st_mode)) // see find_executable()
7625 {
7977a734
FCE
7626 // Need to call canonicalize here, in order to path-expand
7627 // patterns like process("stap*"). Otherwise it may go through
7628 // to the next round of expansion as ("stap"), leading to a $PATH
7629 // search that's not consistent with the glob search already done.
5bca76a8
JS
7630 string canononicalized = resolve_path (globbed);
7631 globbed = canononicalized.c_str();
7977a734 7632
88f8e2c0
JS
7633 // The canonical names can result in duplication, for example
7634 // having followed symlinks that are common with shared
7635 // libraries. Filter those out.
7636 if (!dupes.insert(canononicalized).second)
7637 continue;
7638
37001baa
FCE
7639 // synthesize a new probe_point, with the glob-expanded string
7640 probe_point *pp = new probe_point (*location);
ef0943df
JL
7641 pp->from_glob = true;
7642
5750ecc6
FCE
7643 // PR13338: quote results to prevent recursion
7644 string eglobbed = escape_glob_chars (globbed);
7645
7646 if (sess.verbose > 1)
7647 clog << _F("Expanded process(\"%s\") to process(\"%s\")",
7648 module_name.c_str(), eglobbed.c_str()) << endl;
05fb3e0c 7649 string eglobbed_tgt = path_remove_sysroot(sess, eglobbed);
5750ecc6 7650
37001baa 7651 probe_point::component* ppc = new probe_point::component (TOK_PROCESS,
05fb3e0c 7652 new literal_string (eglobbed_tgt));
37001baa
FCE
7653 ppc->tok = location->components[0]->tok; // overwrite [0] slot, pattern matched above
7654 pp->components[0] = ppc;
7655
d885563b 7656 probe* new_probe = new probe (base, pp);
7977a734
FCE
7657
7658 // We override "optional = true" here, as if the
7659 // wildcarded probe point was given a "?" suffix.
7660
7661 // This is because wildcard probes will be expected
7662 // by users to apply only to some subset of the
7663 // matching binaries, in the sense of "any", rather
7664 // than "all", sort of similarly how
7665 // module("*").function("...") patterns work.
7666
7667 derive_probes (sess, new_probe, finished_results,
7668 true /* NB: not location->optional */ );
37001baa
FCE
7669 }
7670 }
7671
7672 globfree (& the_blob);
44ffe90c
JL
7673
7674 unsigned results_post = finished_results.size();
7675
e90006d7 7676 // Did we fail to find a function/plt/mark by name? Let's suggest
44ffe90c
JL
7677 // something!
7678 string func;
7679 if (results_pre == results_post
7680 && get_param(filled_parameters, TOK_FUNCTION, func)
7681 && !func.empty())
7682 {
0508c45f 7683 string sugs = suggest_dwarf_functions(sess, modules_seen, func);
44ffe90c
JL
7684 modules_seen.clear();
7685 if (!sugs.empty())
dc09353a 7686 throw SEMANTIC_ERROR (_NF("no match (similar function: %s)",
44ffe90c
JL
7687 "no match (similar functions: %s)",
7688 sugs.find(',') == string::npos,
7689 sugs.c_str()));
7690 }
40a70f52
JL
7691 else if (results_pre == results_post
7692 && get_param(filled_parameters, TOK_PLT, func)
7693 && !func.empty())
7694 {
7695 string sugs = suggest_plt_functions(sess, modules_seen, func);
7696 modules_seen.clear();
7697 if (!sugs.empty())
7698 throw SEMANTIC_ERROR (_NF("no match (similar function: %s)",
7699 "no match (similar functions: %s)",
7700 sugs.find(',') == string::npos,
7701 sugs.c_str()));
7702 }
e90006d7
JL
7703 else if (results_pre == results_post
7704 && get_param(filled_parameters, TOK_MARK, func)
7705 && !func.empty())
7706 {
7707 string provider;
7708 get_param(filled_parameters, TOK_PROVIDER, provider);
7709
7710 string sugs = suggest_marks(sess, modules_seen, func, provider);
7711 modules_seen.clear();
7712 if (!sugs.empty())
7713 throw SEMANTIC_ERROR (_NF("no match (similar mark: %s)",
7714 "no match (similar marks: %s)",
7715 sugs.find(',') == string::npos,
7716 sugs.c_str()));
7717 }
44ffe90c 7718
37001baa
FCE
7719 return; // avoid falling through
7720 }
7721
5750ecc6
FCE
7722 // PR13338: unquote glob results
7723 module_name = unescape_glob_chars (module_name);
05fb3e0c 7724 user_path = find_executable (module_name, "", sess.sysenv); // canonicalize it
5fa0d811
JL
7725 if (!is_fully_resolved(user_path, "", sess.sysenv))
7726 throw SEMANTIC_ERROR(_F("cannot find executable '%s'",
7727 user_path.c_str()));
d1bcbe71
RH
7728
7729 // if the executable starts with "#!", we look for the interpreter of the script
7730 {
7731 ifstream script_file (user_path.c_str () );
7732
7733 if (script_file.good ())
7734 {
7735 string line;
7736
7737 getline (script_file, line);
7738
7739 if (line.compare (0, 2, "#!") == 0)
7740 {
7741 string path_head = line.substr(2);
7742
7743 // remove white spaces at the beginning of the string
7744 size_t p2 = path_head.find_first_not_of(" \t");
7745
7746 if (p2 != string::npos)
7747 {
7748 string path = path_head.substr(p2);
7749
7750 // remove white spaces at the end of the string
7751 p2 = path.find_last_not_of(" \t\n");
7752 if (string::npos != p2)
7753 path.erase(p2+1);
7754
8e13c1a1
RH
7755 // handle "#!/usr/bin/env" redirect
7756 size_t offset = 0;
7757 if (path.compare(0, sizeof("/bin/env")-1, "/bin/env") == 0)
7758 {
7759 offset = sizeof("/bin/env")-1;
7760 }
7761 else if (path.compare(0, sizeof("/usr/bin/env")-1, "/usr/bin/env") == 0)
7762 {
7763 offset = sizeof("/usr/bin/env")-1;
7764 }
7765
7766 if (offset != 0)
7767 {
7768 size_t p3 = path.find_first_not_of(" \t", offset);
7769
7770 if (p3 != string::npos)
7771 {
7772 string env_path = path.substr(p3);
05fb3e0c
WF
7773 user_path = find_executable (env_path, sess.sysroot,
7774 sess.sysenv);
8e13c1a1
RH
7775 }
7776 }
7777 else
7778 {
05fb3e0c 7779 user_path = find_executable (path, sess.sysroot, sess.sysenv);
8e13c1a1 7780 }
d1bcbe71
RH
7781
7782 struct stat st;
7783
7784 if (access (user_path.c_str(), X_OK) == 0
7785 && stat (user_path.c_str(), &st) == 0
7786 && S_ISREG (st.st_mode)) // see find_executable()
7787 {
7788 if (sess.verbose > 1)
b530b5b3
LB
7789 clog << _F("Expanded process(\"%s\") to process(\"%s\")",
7790 module_name.c_str(), user_path.c_str()) << endl;
d1bcbe71
RH
7791
7792 assert (location->components.size() > 0);
7793 assert (location->components[0]->functor == TOK_PROCESS);
7794 assert (location->components[0]->arg);
7795 literal_string* lit = dynamic_cast<literal_string*>(location->components[0]->arg);
7796 assert (lit);
7797
7798 // synthesize a new probe_point, with the expanded string
7799 probe_point *pp = new probe_point (*location);
05fb3e0c 7800 string user_path_tgt = path_remove_sysroot(sess, user_path);
d1bcbe71 7801 probe_point::component* ppc = new probe_point::component (TOK_PROCESS,
05fb3e0c 7802 new literal_string (user_path_tgt.c_str()));
d1bcbe71
RH
7803 ppc->tok = location->components[0]->tok; // overwrite [0] slot, pattern matched above
7804 pp->components[0] = ppc;
7805
d885563b 7806 probe* new_probe = new probe (base, pp);
d1bcbe71
RH
7807
7808 derive_probes (sess, new_probe, finished_results);
7809
7810 script_file.close();
7811 return;
7812 }
7813 }
7814 }
7815 }
7816 script_file.close();
7817 }
7818
dbe9d133
JL
7819 // If this is a library probe, then target the library module instead. We
7820 // do this only if the library path is already fully resolved (such as
7821 // what query_one_library() would have done for us). Otherwise, we resort
7822 // to iterate_over_libraries.
7823 if (get_param (parameters, TOK_LIBRARY, user_lib) && !user_lib.empty()
7824 && is_fully_resolved(user_lib, sess.sysroot, sess.sysenv, "LD_LIBRARY_PATH"))
7825 module_name = user_lib;
63b4fd14 7826 else
dbe9d133 7827 module_name = user_path; // canonicalize it
d0a7f5a9 7828
3667d615 7829 // uretprobes aren't available everywhere
536f1261 7830 if (has_null_param(parameters, TOK_RETURN) && !sess.runtime_usermode_p())
2b69faaf 7831 {
3667d615
JS
7832 if (kernel_supports_inode_uprobes(sess) &&
7833 !kernel_supports_inode_uretprobes(sess))
dc09353a 7834 throw SEMANTIC_ERROR
3ffeaf3c 7835 (_("process return probes not available [man error::inode-uprobes]"));
2b69faaf 7836 }
3667d615 7837
f4000852
MW
7838 // There is a similar check in pass 4 (buildrun), but it is
7839 // needed here too to make sure alternatives for optional
7840 // (? or !) process probes are disposed and/or alternatives
7841 // are selected.
3667d615
JS
7842 if (!sess.runtime_usermode_p())
7843 check_process_probe_kernel_support(sess);
e34d5d13 7844
7a24d422
FCE
7845 // user-space target; we use one dwflpp instance per module name
7846 // (= program or shared library)
707bf35e 7847 dw = get_user_dw(sess, module_name);
c8959a29 7848 }
20c6c071 7849
1acfc030
JS
7850 assert(dw);
7851
e90006d7
JL
7852 unsigned results_pre = finished_results.size();
7853
5896cd05 7854 if (sess.verbose > 3)
b530b5b3 7855 clog << _F("dwarf_builder::build for %s", module_name.c_str()) << endl;
5896cd05 7856
a794dbeb
FCE
7857 string dummy_mark_name; // NB: PR10245: dummy value, need not substitute - => __
7858 if (get_param(parameters, TOK_MARK, dummy_mark_name))
f28a8c28 7859 {
51d6bda3 7860 sdt_query sdtq(base, location, *dw, filled_parameters, finished_results, user_lib);
06de3a04 7861 dw->iterate_over_modules<base_query>(&query_module, &sdtq);
e90006d7
JL
7862
7863 // We need to update modules_seen with the modules we've visited
7864 modules_seen.insert(sdtq.visited_modules.begin(),
7865 sdtq.visited_modules.end());
7866
614f0fcf
JL
7867 string lib;
7868 if (results_pre == finished_results.size() && !sdtq.resolved_library
7869 && get_param(filled_parameters, TOK_LIBRARY, lib)
7870 && !lib.empty() && !sdtq.visited_libraries.empty())
7871 {
7872 // The library didn't fit any DT_NEEDED libraries. As a last effort,
7873 // let's try to look for the library directly.
7874 string resolved_lib = find_executable(lib, sess.sysroot, sess.sysenv,
7875 "LD_LIBRARY_PATH");
7876 if (resolved_lib.find('/') != string::npos)
7877 {
7878 probe *new_base = build_library_probe(*dw, resolved_lib,
7879 base, location);
7880 derive_probes(sess, new_base, finished_results);
7881 sess.print_warning(_F("'%s' is not a needed library of '%s'. "
7882 "Specify the full path to squelch this warning.",
7883 resolved_lib.c_str(), dw->module_name.c_str()));
7884 return;
7885 }
f23e4abe
JL
7886
7887 // Otherwise, let's suggest from the DT_NEEDED libraries
7888 string sugs = levenshtein_suggest(lib, sdtq.visited_libraries, 5);
7889 if (!sugs.empty())
7890 throw SEMANTIC_ERROR (_NF("no match (similar library: %s)",
7891 "no match (similar libraries: %s)",
7892 sugs.find(',') == string::npos,
7893 sugs.c_str()));
614f0fcf
JL
7894 }
7895
e90006d7
JL
7896 // Did we fail to find a mark?
7897 if (results_pre == finished_results.size() && !location->from_glob)
7898 {
7899 string provider;
7900 get_param(filled_parameters, TOK_PROVIDER, provider);
7901
7902 string sugs = suggest_marks(sess, modules_seen, dummy_mark_name, provider);
7903 modules_seen.clear();
7904 if (!sugs.empty())
7905 throw SEMANTIC_ERROR (_NF("no match (similar mark: %s)",
7906 "no match (similar marks: %s)",
7907 sugs.find(',') == string::npos,
7908 sugs.c_str()));
7909 }
7910
edce5b67 7911 return;
7a05f484 7912 }
20c6c071 7913
6d5d594e 7914 dwarf_query q(base, location, *dw, filled_parameters, finished_results, user_path, user_lib);
7a24d422
FCE
7915
7916 // XXX: kernel.statement.absolute is a special case that requires no
7917 // dwfl processing. This code should be in a separate builder.
7a24d422 7918 if (q.has_kernel && q.has_absolute)
37ebca01 7919 {
4baf0e53 7920 // assert guru mode for absolute probes
37ebca01
FCE
7921 if (! q.base_probe->privileged)
7922 {
dc09353a 7923 throw SEMANTIC_ERROR (_("absolute statement probe in unprivileged script; need stap -g"),
edce5b67 7924 q.base_probe->tok);
37ebca01
FCE
7925 }
7926
7927 // For kernel.statement(NUM).absolute probe points, we bypass
7928 // all the debuginfo stuff: We just wire up a
7929 // dwarf_derived_probe right here and now.
4baf0e53 7930 dwarf_derived_probe* p =
b8da0ad1
FCE
7931 new dwarf_derived_probe ("", "", 0, "kernel", "",
7932 q.statement_num_val, q.statement_num_val,
7933 q, 0);
37ebca01 7934 finished_results.push_back (p);
1a0dbc5a 7935 sess.unwindsym_modules.insert ("kernel");
37ebca01
FCE
7936 return;
7937 }
7938
06de3a04 7939 dw->iterate_over_modules<base_query>(&query_module, &q);
8f14e444 7940
44ffe90c
JL
7941 // We need to update modules_seen with the modules we've visited
7942 modules_seen.insert(q.visited_modules.begin(),
7943 q.visited_modules.end());
8f14e444
FCE
7944
7945 // PR11553 special processing: .return probes requested, but
7946 // some inlined function instances matched.
7947 unsigned i_n_r = q.inlined_non_returnable.size();
7948 unsigned results_post = finished_results.size();
7949 if (i_n_r > 0)
7950 {
7951 if ((results_pre == results_post) && (! sess.suppress_warnings)) // no matches; issue warning
7952 {
7953 string quicklist;
7954 for (set<string>::iterator it = q.inlined_non_returnable.begin();
7955 it != q.inlined_non_returnable.end();
7956 it++)
7957 {
7958 quicklist += " " + (*it);
7959 if (quicklist.size() > 80) // heuristic, don't make an overlong report line
7960 {
7961 quicklist += " ...";
7962 break;
7963 }
7964 }
c57ea854 7965
52c2652f 7966 sess.print_warning (_NF("cannot probe .return of %u inlined function %s",
b530b5b3 7967 "cannot probe .return of %u inlined functions %s",
52c2652f 7968 quicklist.size(), i_n_r, quicklist.c_str()));
8f14e444
FCE
7969 // There will be also a "no matches" semantic error generated.
7970 }
7971 if (sess.verbose > 1)
52c2652f
NMA
7972 clog << _NF("skipped .return probe of %u inlined function",
7973 "skipped .return probe of %u inlined functions", i_n_r, i_n_r) << endl;
8f14e444
FCE
7974 if ((sess.verbose > 3) || (sess.verbose > 2 && results_pre == results_post)) // issue details with high verbosity
7975 {
7976 for (set<string>::iterator it = q.inlined_non_returnable.begin();
7977 it != q.inlined_non_returnable.end();
7978 it++)
7979 clog << (*it) << " ";
7980 clog << endl;
7981 }
7982 } // i_n_r > 0
44ffe90c 7983
614f0fcf
JL
7984 string lib;
7985 if (results_pre == results_post && !q.resolved_library
7986 && get_param(filled_parameters, TOK_LIBRARY, lib)
7987 && !lib.empty() && !q.visited_libraries.empty())
7988 {
7989 // The library didn't fit any DT_NEEDED libraries. As a last effort,
7990 // let's try to look for the library directly.
7991 string resolved_lib = find_executable(lib, sess.sysroot, sess.sysenv,
7992 "LD_LIBRARY_PATH");
7993 if (resolved_lib.find('/') != string::npos)
7994 {
7995 probe *new_base = build_library_probe(*dw, resolved_lib,
7996 base, location);
7997 derive_probes(sess, new_base, finished_results);
7998 sess.print_warning(_F("'%s' is not a needed library of '%s'. "
7999 "Specify the full path to squelch this warning.",
8000 resolved_lib.c_str(), dw->module_name.c_str()));
8001 return;
8002 }
f23e4abe
JL
8003
8004 // Otherwise, let's suggest from the DT_NEEDED libraries
8005 string sugs = levenshtein_suggest(lib, q.visited_libraries, 5);
8006 if (!sugs.empty())
8007 throw SEMANTIC_ERROR (_NF("no match (similar library: %s)",
8008 "no match (similar libraries: %s)",
8009 sugs.find(',') == string::npos,
8010 sugs.c_str()));
614f0fcf
JL
8011 }
8012
e90006d7 8013 // If we just failed to resolve a function/plt by name, we can suggest
ef0943df
JL
8014 // something. We only suggest things for probe points that were not
8015 // synthesized from a glob, i.e. only for 'real' probes. This is also
8016 // required because modules_seen needs to accumulate across recursive
8017 // calls for process(glob)[.library(glob)] probes.
44ffe90c 8018 string func;
ef0943df 8019 if (results_pre == results_post && !location->from_glob
44ffe90c
JL
8020 && get_param(filled_parameters, TOK_FUNCTION, func)
8021 && !func.empty())
8022 {
0508c45f 8023 string sugs = suggest_dwarf_functions(sess, modules_seen, func);
44ffe90c
JL
8024 modules_seen.clear();
8025 if (!sugs.empty())
dc09353a 8026 throw SEMANTIC_ERROR (_NF("no match (similar function: %s)",
44ffe90c
JL
8027 "no match (similar functions: %s)",
8028 sugs.find(',') == string::npos,
8029 sugs.c_str()));
8030 }
ef0943df 8031 else if (results_pre == results_post && !location->from_glob
40a70f52
JL
8032 && get_param(filled_parameters, TOK_PLT, func)
8033 && !func.empty())
8034 {
8035 string sugs = suggest_plt_functions(sess, modules_seen, func);
8036 modules_seen.clear();
8037 if (!sugs.empty())
8038 throw SEMANTIC_ERROR (_NF("no match (similar function: %s)",
8039 "no match (similar functions: %s)",
8040 sugs.find(',') == string::npos,
8041 sugs.c_str()));
8042 }
44ffe90c
JL
8043 else if (results_pre != results_post)
8044 // Something was derived so we won't need to suggest something
8045 modules_seen.clear();
5f0a03a6
JK
8046}
8047
8048symbol_table::~symbol_table()
8049{
c9efa5c9 8050 delete_map(map_by_addr);
5f0a03a6
JK
8051}
8052
8053void
2867a2a1 8054symbol_table::add_symbol(const char *name, bool weak, bool descriptor,
44ffe90c 8055 Dwarf_Addr addr, Dwarf_Addr* /*high_addr*/)
5f0a03a6 8056{
ab91b232
JK
8057#ifdef __powerpc__
8058 // Map ".sys_foo" to "sys_foo".
8059 if (name[0] == '.')
8060 name++;
8061#endif
5f0a03a6
JK
8062 func_info *fi = new func_info();
8063 fi->addr = addr;
8064 fi->name = name;
ab91b232 8065 fi->weak = weak;
2867a2a1 8066 fi->descriptor = descriptor;
5f0a03a6
JK
8067 map_by_name[fi->name] = fi;
8068 // TODO: Use a multimap in case there are multiple static
8069 // functions with the same name?
1c6b77e5 8070 map_by_addr.insert(make_pair(addr, fi));
5f0a03a6
JK
8071}
8072
46f7b6be 8073void
f98c6346 8074symbol_table::prepare_section_rejection(Dwfl_Module *mod __attribute__ ((unused)))
46f7b6be
JK
8075{
8076#ifdef __powerpc__
8077 /*
8078 * The .opd section contains function descriptors that can look
8079 * just like function entry points. For example, there's a function
8080 * descriptor called "do_exit" that links to the entry point ".do_exit".
8081 * Reject all symbols in .opd.
8082 */
8083 opd_section = SHN_UNDEF;
8084 Dwarf_Addr bias;
8085 Elf* elf = (dwarf_getelf (dwfl_module_getdwarf (mod, &bias))
8086 ?: dwfl_module_getelf (mod, &bias));
8087 Elf_Scn* scn = 0;
8088 size_t shstrndx;
8089
8090 if (!elf)
8091 return;
fcc30d6d 8092 if (elf_getshdrstrndx (elf, &shstrndx) != 0)
46f7b6be
JK
8093 return;
8094 while ((scn = elf_nextscn(elf, scn)) != NULL)
8095 {
8096 GElf_Shdr shdr_mem;
8097 GElf_Shdr *shdr = gelf_getshdr(scn, &shdr_mem);
8098 if (!shdr)
8099 continue;
8100 const char *name = elf_strptr(elf, shstrndx, shdr->sh_name);
8101 if (!strcmp(name, ".opd"))
8102 {
8103 opd_section = elf_ndxscn(scn);
8104 return;
8105 }
8106 }
8107#endif
8108}
8109
8110bool
8111symbol_table::reject_section(GElf_Word section)
8112{
8113 if (section == SHN_UNDEF)
8114 return true;
8115#ifdef __powerpc__
8116 if (section == opd_section)
8117 return true;
8118#endif
8119 return false;
8120}
8121
5f0a03a6
JK
8122enum info_status
8123symbol_table::get_from_elf()
8124{
8125 Dwarf_Addr high_addr = 0;
8126 Dwfl_Module *mod = mod_info->mod;
8127 int syments = dwfl_module_getsymtab(mod);
8128 assert(syments);
46f7b6be 8129 prepare_section_rejection(mod);
5f0a03a6
JK
8130 for (int i = 1; i < syments; ++i)
8131 {
8132 GElf_Sym sym;
ab91b232 8133 GElf_Word section;
cba67d8f
MW
8134 const char *name;
8135 GElf_Addr addr;
8136 bool reject;
8137
8138/* Note that dwfl_module_getsym does adjust the sym.st_value but doesn't
8139 try to resolve it to a function address. dwfl_module_getsym_info leaves
8140 the st_value in tact (no adjustment applied) and returns the fully
8141 resolved address separately. In that case we can simply reject the
8142 symbol if it is SHN_UNDEF and don't need to call reject_section which
8143 does extra checks to see whether the address fall in an architecture
8144 specific descriptor table (which will never be the case when using the
8145 new dwfl_module_getsym_info). dwfl_module_getsym will only provide us
8146 with the (adjusted) st_value of the symbol, which might point into a
8147 function descriptor table. So in that case we still have to call
8148 reject_section. */
8149#if _ELFUTILS_PREREQ (0, 158)
8150 name = dwfl_module_getsym_info (mod, i, &sym, &addr, &section,
8151 NULL, NULL);
8152 reject = section == SHN_UNDEF;
8153#else
8154 name = dwfl_module_getsym (mod, i, &sym, &section);
b23098cd 8155 addr = sym.st_value;
cba67d8f
MW
8156 reject = reject_section(section);
8157#endif
8158
2867a2a1 8159 if (name && GELF_ST_TYPE(sym.st_info) == STT_FUNC)
ab91b232 8160 add_symbol(name, (GELF_ST_BIND(sym.st_info) == STB_WEAK),
cba67d8f 8161 reject, addr, &high_addr);
2a4acb09
JL
8162 if (name && GELF_ST_TYPE(sym.st_info) == STT_OBJECT
8163 && GELF_ST_BIND(sym.st_info) == STB_GLOBAL)
8164 globals[name] = addr;
8165 if (name && GELF_ST_TYPE(sym.st_info) == STT_OBJECT
8166 && GELF_ST_BIND(sym.st_info) == STB_LOCAL)
8167 locals[name] = addr;
5f0a03a6
JK
8168 }
8169 return info_present;
8170}
8171
5f0a03a6
JK
8172func_info *
8173symbol_table::get_func_containing_address(Dwarf_Addr addr)
8174{
1c6b77e5
JS
8175 iterator_t iter = map_by_addr.upper_bound(addr);
8176 if (iter == map_by_addr.begin())
5f0a03a6 8177 return NULL;
2e67a43b 8178 else
1c6b77e5 8179 return (--iter)->second;
5f0a03a6
JK
8180}
8181
3d372d6b
SC
8182func_info *
8183symbol_table::get_first_func()
8184{
8185 iterator_t iter = map_by_addr.begin();
8186 return (iter)->second;
8187}
8188
5f0a03a6
JK
8189func_info *
8190symbol_table::lookup_symbol(const string& name)
8191{
8192 map<string, func_info*>::iterator i = map_by_name.find(name);
8193 if (i == map_by_name.end())
8194 return NULL;
8195 return i->second;
8196}
8197
8198Dwarf_Addr
8199symbol_table::lookup_symbol_address(const string& name)
8200{
8201 func_info *fi = lookup_symbol(name);
8202 if (fi)
8203 return fi->addr;
8204 return 0;
8205}
8206
ab91b232
JK
8207// This is the kernel symbol table. The kernel macro cond_syscall creates
8208// a weak symbol for each system call and maps it to sys_ni_syscall.
8209// For system calls not implemented elsewhere, this weak symbol shows up
8210// in the kernel symbol table. Following the precedent of dwarfful stap,
8211// we refuse to consider such symbols. Here we delete them from our
8212// symbol table.
8213// TODO: Consider generalizing this and/or making it part of blacklist
8214// processing.
8215void
8216symbol_table::purge_syscall_stubs()
8217{
8218 Dwarf_Addr stub_addr = lookup_symbol_address("sys_ni_syscall");
8219 if (stub_addr == 0)
8220 return;
1c6b77e5 8221 range_t purge_range = map_by_addr.equal_range(stub_addr);
2e67a43b
TM
8222 for (iterator_t iter = purge_range.first;
8223 iter != purge_range.second;
1c6b77e5 8224 )
ab91b232 8225 {
1c6b77e5 8226 func_info *fi = iter->second;
2e67a43b 8227 if (fi->weak && fi->name != "sys_ni_syscall")
ab91b232 8228 {
2e67a43b 8229 map_by_name.erase(fi->name);
1c6b77e5 8230 map_by_addr.erase(iter++);
2e67a43b 8231 delete fi;
2e67a43b 8232 }
1c6b77e5
JS
8233 else
8234 iter++;
ab91b232
JK
8235 }
8236}
8237
5f0a03a6 8238void
5f52fafe 8239module_info::get_symtab()
5f0a03a6 8240{
1c6b77e5
JS
8241 if (symtab_status != info_unknown)
8242 return;
8243
5f0a03a6
JK
8244 sym_table = new symbol_table(this);
8245 if (!elf_path.empty())
8246 {
5f0a03a6
JK
8247 symtab_status = sym_table->get_from_elf();
8248 }
8249 else
8250 {
8251 assert(name == TOK_KERNEL);
ab3ed72d
DS
8252 symtab_status = info_absent;
8253 cerr << _("Error: Cannot find vmlinux.") << endl;;
5f0a03a6
JK
8254 }
8255 if (symtab_status == info_absent)
8256 {
8257 delete sym_table;
8258 sym_table = NULL;
8259 return;
8260 }
8261
ab91b232
JK
8262 if (name == TOK_KERNEL)
8263 sym_table->purge_syscall_stubs();
5f0a03a6
JK
8264}
8265
1c6b77e5
JS
8266// update_symtab reconciles data between the elf symbol table and the dwarf
8267// function enumeration. It updates the symbol table entries with the dwarf
8268// die that describes the function, which also signals to query_module_symtab
8269// that a statement probe isn't needed. In return, it also adds aliases to the
8270// function table for names that share the same addr/die.
8271void
8272module_info::update_symtab(cu_function_cache_t *funcs)
8273{
8274 if (!sym_table)
8275 return;
8276
8277 cu_function_cache_t new_funcs;
8278
8279 for (cu_function_cache_t::iterator func = funcs->begin();
8280 func != funcs->end(); func++)
8281 {
8282 // optimization: inlines will never be in the symbol table
8283 if (dwarf_func_inline(&func->second) != 0)
6e6d9a3b
JL
8284 {
8285 inlined_funcs.insert(func->first);
8286 continue;
8287 }
1c6b77e5 8288
1ffb8bd1
JS
8289 // XXX We may want to make additional efforts to match mangled elf names
8290 // to dwarf too. MIPS_linkage_name can help, but that's sometimes
8291 // missing, so we may also need to try matching by address. See also the
8292 // notes about _Z in dwflpp::iterate_over_functions().
8293
1c6b77e5
JS
8294 func_info *fi = sym_table->lookup_symbol(func->first);
8295 if (!fi)
8296 continue;
8297
8298 // iterate over all functions at the same address
8299 symbol_table::range_t er = sym_table->map_by_addr.equal_range(fi->addr);
8300 for (symbol_table::iterator_t it = er.first; it != er.second; ++it)
8301 {
8302 // update this function with the dwarf die
8303 it->second->die = func->second;
8304
8305 // if this function is a new alias, then
8306 // save it to merge into the function cache
8307 if (it->second != fi)
b7478964 8308 new_funcs.insert(make_pair(it->second->name, it->second->die));
1c6b77e5
JS
8309 }
8310 }
8311
8312 // add all discovered aliases back into the function cache
8313 // NB: this won't replace any names that dwarf may have already found
8314 funcs->insert(new_funcs.begin(), new_funcs.end());
8315}
8316
5f0a03a6
JK
8317module_info::~module_info()
8318{
8319 if (sym_table)
8320 delete sym_table;
b55bc428
FCE
8321}
8322
935447c8 8323// ------------------------------------------------------------------------
888af770 8324// user-space probes
935447c8
DS
8325// ------------------------------------------------------------------------
8326
935447c8 8327
888af770 8328struct uprobe_derived_probe_group: public generic_dpg<uprobe_derived_probe>
935447c8 8329{
89ba3085
FCE
8330private:
8331 string make_pbm_key (uprobe_derived_probe* p) {
cfcab6c7 8332 return p->path + "|" + p->module + "|" + p->section + "|" + lex_cast(p->pid);
89ba3085
FCE
8333 }
8334
cfcab6c7
JS
8335 void emit_module_maxuprobes (systemtap_session& s);
8336
2b69faaf
JS
8337 // Using our own utrace-based uprobes
8338 void emit_module_utrace_decls (systemtap_session& s);
8339 void emit_module_utrace_init (systemtap_session& s);
8340 void emit_module_utrace_exit (systemtap_session& s);
8341
8342 // Using the upstream inode-based uprobes
8343 void emit_module_inode_decls (systemtap_session& s);
8344 void emit_module_inode_init (systemtap_session& s);
3642eb17 8345 void emit_module_inode_refresh (systemtap_session& s);
2b69faaf
JS
8346 void emit_module_inode_exit (systemtap_session& s);
8347
3a894f7e
JS
8348 // Using the dyninst backend (via stapdyn)
8349 void emit_module_dyninst_decls (systemtap_session& s);
8350 void emit_module_dyninst_init (systemtap_session& s);
8351 void emit_module_dyninst_exit (systemtap_session& s);
8352
935447c8 8353public:
888af770 8354 void emit_module_decls (systemtap_session& s);
935447c8 8355 void emit_module_init (systemtap_session& s);
3642eb17 8356 void emit_module_refresh (systemtap_session& s);
935447c8 8357 void emit_module_exit (systemtap_session& s);
ca6d3b0f
JL
8358
8359 // on-the-fly only supported for inode-uprobes
8360 bool otf_supported (systemtap_session& s)
8361 { return !s.runtime_usermode_p()
8362 && kernel_supports_inode_uprobes(s); }
8363
8364 // workqueue manipulation is safe in uprobes
8365 bool otf_safe_context (systemtap_session& s)
8366 { return otf_supported(s); }
935447c8
DS
8367};
8368
8369
888af770
FCE
8370void
8371uprobe_derived_probe::join_group (systemtap_session& s)
8372{
8373 if (! s.uprobe_derived_probes)
8374 s.uprobe_derived_probes = new uprobe_derived_probe_group ();
8375 s.uprobe_derived_probes->enroll (this);
ca6d3b0f
JL
8376 this->group = s.uprobe_derived_probes;
8377
f31a77f5
DS
8378 if (s.runtime_usermode_p())
8379 enable_dynprobes(s);
8380 else
4441e344 8381 enable_task_finder(s);
a96d1db0 8382
8a03658e 8383 // Ask buildrun.cxx to build extra module if needed, and
d3e959b0
DS
8384 // signal staprun to load that module. If we're using the builtin
8385 // inode-uprobes, we still need to know that it is required.
8a03658e 8386 s.need_uprobes = true;
a96d1db0
DN
8387}
8388
888af770 8389
c0f84e7b
SC
8390void
8391uprobe_derived_probe::getargs(std::list<std::string> &arg_set) const
8392{
8393 dwarf_derived_probe::getargs(arg_set);
8394 arg_set.insert(arg_set.end(), args.begin(), args.end());
8395}
8396
8397
8398void
8399uprobe_derived_probe::saveargs(int nargs)
8400{
8401 for (int i = 1; i <= nargs; i++)
8402 args.push_back("$arg" + lex_cast (i) + ":long");
8403}
8404
8405
2865d17a 8406void
42e38653 8407uprobe_derived_probe::emit_privilege_assertion (translator_output* o)
2865d17a
DB
8408{
8409 // These probes are allowed for unprivileged users, but only in the
8410 // context of processes which they own.
8411 emit_process_owner_assertion (o);
8412}
8413
8414
888af770 8415struct uprobe_builder: public derived_probe_builder
a96d1db0 8416{
888af770 8417 uprobe_builder() {}
2b69faaf 8418 virtual void build(systemtap_session & sess,
a96d1db0
DN
8419 probe * base,
8420 probe_point * location,
86bf665e 8421 literal_map_t const & parameters,
a96d1db0
DN
8422 vector<derived_probe *> & finished_results)
8423 {
888af770 8424 int64_t process, address;
a96d1db0 8425
2b69faaf 8426 if (kernel_supports_inode_uprobes(sess))
dc09353a 8427 throw SEMANTIC_ERROR (_("absolute process probes not available [man error::inode-uprobes]"));
2b69faaf 8428
888af770 8429 bool b1 = get_param (parameters, TOK_PROCESS, process);
ced347a9 8430 (void) b1;
888af770 8431 bool b2 = get_param (parameters, TOK_STATEMENT, address);
ced347a9 8432 (void) b2;
888af770
FCE
8433 bool rr = has_null_param (parameters, TOK_RETURN);
8434 assert (b1 && b2); // by pattern_root construction
a96d1db0 8435
0973d815 8436 finished_results.push_back(new uprobe_derived_probe(base, location, process, address, rr));
a96d1db0
DN
8437 }
8438};
8439
8440
8441void
cfcab6c7 8442uprobe_derived_probe_group::emit_module_maxuprobes (systemtap_session& s)
a96d1db0 8443{
43241c44
FCE
8444 // We'll probably need at least this many:
8445 unsigned minuprobes = probes.size();
8446 // .. but we don't want so many that .bss is inflated (PR10507):
8447 unsigned uprobesize = 64;
8448 unsigned maxuprobesmem = 10*1024*1024; // 10 MB
8449 unsigned maxuprobes = maxuprobesmem / uprobesize;
8450
aaf7ffe8
FCE
8451 // Let's choose a value on the geometric middle. This should end up
8452 // between minuprobes and maxuprobes. It's OK if this number turns
8453 // out to be < minuprobes or > maxuprobes. At worst, we get a
8454 // run-time error of one kind (too few: missed uprobe registrations)
8455 // or another (too many: vmalloc errors at module load time).
8456 unsigned default_maxuprobes = (unsigned)sqrt((double)minuprobes * (double)maxuprobes);
43241c44 8457
6d0f3f0c 8458 s.op->newline() << "#ifndef MAXUPROBES";
43241c44 8459 s.op->newline() << "#define MAXUPROBES " << default_maxuprobes;
6d0f3f0c 8460 s.op->newline() << "#endif";
cfcab6c7
JS
8461}
8462
8463
8464void
8465uprobe_derived_probe_group::emit_module_utrace_decls (systemtap_session& s)
8466{
8467 if (probes.empty()) return;
8468 s.op->newline() << "/* ---- utrace uprobes ---- */";
8469 // If uprobes isn't in the kernel, pull it in from the runtime.
8470
8471 s.op->newline() << "#if defined(CONFIG_UPROBES) || defined(CONFIG_UPROBES_MODULE)";
8472 s.op->newline() << "#include <linux/uprobes.h>";
8473 s.op->newline() << "#else";
2ba1736a 8474 s.op->newline() << "#include \"linux/uprobes/uprobes.h\"";
cfcab6c7
JS
8475 s.op->newline() << "#endif";
8476 s.op->newline() << "#ifndef UPROBES_API_VERSION";
8477 s.op->newline() << "#define UPROBES_API_VERSION 1";
8478 s.op->newline() << "#endif";
8479
8480 emit_module_maxuprobes (s);
a96d1db0 8481
cc52276b 8482 // Forward decls
2ba1736a 8483 s.op->newline() << "#include \"linux/uprobes-common.h\"";
cc52276b 8484
5e112f92
FCE
8485 // In .bss, the shared pool of uprobe/uretprobe structs. These are
8486 // too big to embed in the initialized .data stap_uprobe_spec array.
cc52276b
WC
8487 // XXX: consider a slab cache or somesuch for stap_uprobes
8488 s.op->newline() << "static struct stap_uprobe stap_uprobes [MAXUPROBES];";
6b378b7c 8489 s.op->newline() << "static DEFINE_MUTEX(stap_uprobes_lock);"; // protects against concurrent registration/unregistration
a96d1db0 8490
89ba3085
FCE
8491 s.op->assert_0_indent();
8492
89ba3085
FCE
8493 // Assign task-finder numbers as we build up the stap_uprobe_tf table.
8494 // This means we process probes[] in two passes.
8495 map <string,unsigned> module_index;
8496 unsigned module_index_ctr = 0;
8497
cc52276b
WC
8498 // not const since embedded task_finder_target struct changes
8499 s.op->newline() << "static struct stap_uprobe_tf stap_uprobe_finders[] = {";
89ba3085
FCE
8500 s.op->indent(1);
8501 for (unsigned i=0; i<probes.size(); i++)
8502 {
8503 uprobe_derived_probe *p = probes[i];
8504 string pbmkey = make_pbm_key (p);
8505 if (module_index.find (pbmkey) == module_index.end())
8506 {
8507 module_index[pbmkey] = module_index_ctr++;
8508
8509 s.op->newline() << "{";
8510 // NB: it's essential that make_pbm_key() use all of and
8511 // only the same fields as we're about to emit.
8512 s.op->line() << " .finder={";
1af100fc 8513 s.op->line() << " .purpose=\"uprobes\",";
89ba3085 8514 if (p->pid != 0)
68910c97
JK
8515 s.op->line() << " .pid=" << p->pid << ",";
8516
8517 if (p->section == "") // .statement(addr).absolute
8518 s.op->line() << " .callback=&stap_uprobe_process_found,";
89ba3085
FCE
8519 else if (p->section == ".absolute") // proxy for ET_EXEC -> exec()'d program
8520 {
8521 s.op->line() << " .procname=" << lex_cast_qstring(p->module) << ",";
8522 s.op->line() << " .callback=&stap_uprobe_process_found,";
8523 }
68910c97 8524 else if (p->section != ".absolute") // ET_DYN
89ba3085 8525 {
4ad95bbc
SC
8526 if (p->has_library)
8527 s.op->line() << " .procname=\"" << p->path << "\", ";
89ba3085
FCE
8528 s.op->line() << " .mmap_callback=&stap_uprobe_mmap_found, ";
8529 s.op->line() << " .munmap_callback=&stap_uprobe_munmap_found, ";
19d91f6c 8530 s.op->line() << " .callback=&stap_uprobe_process_munmap,";
89ba3085 8531 }
89ba3085 8532 s.op->line() << " },";
68910c97
JK
8533 if (p->module != "")
8534 s.op->line() << " .pathname=" << lex_cast_qstring(p->module) << ", ";
89ba3085
FCE
8535 s.op->line() << " },";
8536 }
c57ea854 8537 else
822a6a3d 8538 { } // skip it in this pass, already have a suitable stap_uprobe_tf slot for it.
89ba3085
FCE
8539 }
8540 s.op->newline(-1) << "};";
8541
8542 s.op->assert_0_indent();
8543
3689db05
SC
8544 unsigned pci;
8545 for (pci=0; pci<probes.size(); pci++)
8546 {
8547 // List of perf counters used by each probe
8548 // This list is an index into struct stap_perf_probe,
8549 uprobe_derived_probe *p = probes[pci];
aa2c662f 8550 std::set<string>::iterator pcii;
3689db05
SC
8551 s.op->newline() << "long perf_counters_" + lex_cast(pci) + "[] = {";
8552 for (pcii = p->perf_counter_refs.begin();
8553 pcii != p->perf_counter_refs.end(); pcii++)
8554 {
aa2c662f 8555 std::vector<std::pair<std::string,std::string> >::iterator it;
3689db05
SC
8556 unsigned i = 0;
8557 // Find the associated perf.counter probe
8558 for (it=s.perf_counters.begin() ;
8559 it != s.perf_counters.end(); it++, i++)
aa2c662f 8560 if ((*it).first == (*pcii))
3689db05
SC
8561 break;
8562 s.op->line() << lex_cast(i) << ", ";
8563 }
8564 s.op->newline() << "};";
8565 }
8566
cc52276b
WC
8567 // NB: read-only structure
8568 s.op->newline() << "static const struct stap_uprobe_spec stap_uprobe_specs [] = {";
a96d1db0 8569 s.op->indent(1);
888af770
FCE
8570 for (unsigned i =0; i<probes.size(); i++)
8571 {
8572 uprobe_derived_probe* p = probes[i];
8573 s.op->newline() << "{";
89ba3085
FCE
8574 string key = make_pbm_key (p);
8575 unsigned value = module_index[key];
759e1d76
FCE
8576 if (value != 0)
8577 s.op->line() << " .tfi=" << value << ",";
6b66b9f7 8578 s.op->line() << " .address=(unsigned long)0x" << hex << p->addr << dec << "ULL,";
faea5e16 8579 s.op->line() << " .probe=" << common_probe_init (p) << ",";
4ddb6dd0 8580
038c38c6 8581 if (p->sdt_semaphore_addr != 0)
63b4fd14 8582 s.op->line() << " .sdt_sem_offset=(unsigned long)0x"
038c38c6 8583 << hex << p->sdt_semaphore_addr << dec << "ULL,";
4ddb6dd0 8584
0d049a1d 8585 // XXX: don't bother emit if array is empty
3689db05
SC
8586 s.op->line() << " .perf_counters_dim=ARRAY_SIZE(perf_counters_" << lex_cast(i) << "),";
8587 // List of perf counters used by a probe from above
0d049a1d 8588 s.op->line() << " .perf_counters=perf_counters_" + lex_cast(i) + ",";
3689db05 8589
4ddb6dd0
JS
8590 if (p->has_return)
8591 s.op->line() << " .return_p=1,";
888af770
FCE
8592 s.op->line() << " },";
8593 }
8594 s.op->newline(-1) << "};";
a96d1db0 8595
89ba3085
FCE
8596 s.op->assert_0_indent();
8597
48e685da 8598 s.op->newline() << "static void enter_uprobe_probe (struct uprobe *inst, struct pt_regs *regs) {";
888af770 8599 s.op->newline(1) << "struct stap_uprobe *sup = container_of(inst, struct stap_uprobe, up);";
89ba3085 8600 s.op->newline() << "const struct stap_uprobe_spec *sups = &stap_uprobe_specs [sup->spec_index];";
71db462b 8601 common_probe_entryfn_prologue (s, "STAP_SESSION_RUNNING", "sups->probe",
cda141c2 8602 "stp_probe_type_uprobe");
0e090c74 8603 s.op->newline() << "if (sup->spec_index < 0 || "
6e895029
DS
8604 << "sup->spec_index >= " << probes.size() << ") {";
8605 s.op->newline(1) << "_stp_error (\"bad spec_index %d (max " << probes.size()
0e090c74 8606 << "): %s\", sup->spec_index, c->probe_point);";
6e895029
DS
8607 s.op->newline() << "goto probe_epilogue;";
8608 s.op->newline(-1) << "}";
d9aed31e 8609 s.op->newline() << "c->uregs = regs;";
e04b5d74 8610 s.op->newline() << "c->user_mode_p = 1;";
6415ddde
MW
8611
8612 // Make it look like the IP is set as it would in the actual user
8613 // task when calling real probe handler. Reset IP regs on return, so
8614 // we don't confuse uprobes. PR10458
8615 s.op->newline() << "{";
8616 s.op->indent(1);
d9aed31e 8617 s.op->newline() << "unsigned long uprobes_ip = REG_IP(c->uregs);";
259d54c0 8618 s.op->newline() << "SET_REG_IP(regs, inst->vaddr);";
26e63673 8619 s.op->newline() << "(*sups->probe->ph) (c);";
259d54c0 8620 s.op->newline() << "SET_REG_IP(regs, uprobes_ip);";
6415ddde
MW
8621 s.op->newline(-1) << "}";
8622
ef1337ee 8623 common_probe_entryfn_epilogue (s, true, otf_safe_context(s));
888af770 8624 s.op->newline(-1) << "}";
a96d1db0 8625
48e685da 8626 s.op->newline() << "static void enter_uretprobe_probe (struct uretprobe_instance *inst, struct pt_regs *regs) {";
888af770 8627 s.op->newline(1) << "struct stap_uprobe *sup = container_of(inst->rp, struct stap_uprobe, urp);";
89ba3085 8628 s.op->newline() << "const struct stap_uprobe_spec *sups = &stap_uprobe_specs [sup->spec_index];";
71db462b 8629 common_probe_entryfn_prologue (s, "STAP_SESSION_RUNNING", "sups->probe",
cda141c2 8630 "stp_probe_type_uretprobe");
6dceb5c9 8631 s.op->newline() << "c->ips.ri = inst;";
0e090c74 8632 s.op->newline() << "if (sup->spec_index < 0 || "
0d5561a5
DS
8633 << "sup->spec_index >= " << probes.size() << ") {";
8634 s.op->newline(1) << "_stp_error (\"bad spec_index %d (max " << probes.size()
0e090c74 8635 << "): %s\", sup->spec_index, c->probe_point);";
0d5561a5
DS
8636 s.op->newline() << "goto probe_epilogue;";
8637 s.op->newline(-1) << "}";
8638
d9aed31e 8639 s.op->newline() << "c->uregs = regs;";
e04b5d74 8640 s.op->newline() << "c->user_mode_p = 1;";
6415ddde
MW
8641
8642 // Make it look like the IP is set as it would in the actual user
8643 // task when calling real probe handler. Reset IP regs on return, so
8644 // we don't confuse uprobes. PR10458
8645 s.op->newline() << "{";
8646 s.op->indent(1);
d9aed31e 8647 s.op->newline() << "unsigned long uprobes_ip = REG_IP(c->uregs);";
5e562a69 8648 s.op->newline() << "SET_REG_IP(regs, inst->ret_addr);";
26e63673 8649 s.op->newline() << "(*sups->probe->ph) (c);";
259d54c0 8650 s.op->newline() << "SET_REG_IP(regs, uprobes_ip);";
6415ddde
MW
8651 s.op->newline(-1) << "}";
8652
ef1337ee 8653 common_probe_entryfn_epilogue (s, true, otf_safe_context(s));
a96d1db0
DN
8654 s.op->newline(-1) << "}";
8655
89ba3085 8656 s.op->newline();
2ba1736a 8657 s.op->newline() << "#include \"linux/uprobes-common.c\"";
6d0f3f0c 8658 s.op->newline();
888af770 8659}
935447c8
DS
8660
8661
888af770 8662void
2b69faaf 8663uprobe_derived_probe_group::emit_module_utrace_init (systemtap_session& s)
935447c8 8664{
888af770 8665 if (probes.empty()) return;
935447c8 8666
2b69faaf 8667 s.op->newline() << "/* ---- utrace uprobes ---- */";
935447c8 8668
01b05e2e 8669 s.op->newline() << "for (j=0; j<MAXUPROBES; j++) {";
5e112f92
FCE
8670 s.op->newline(1) << "struct stap_uprobe *sup = & stap_uprobes[j];";
8671 s.op->newline() << "sup->spec_index = -1;"; // free slot
80b4ad8b
FCE
8672 // NB: we assume the rest of the struct (specificaly, sup->up) is
8673 // initialized to zero. This is so that we can use
8674 // sup->up->kdata = NULL for "really free!" PR 6829.
5e112f92
FCE
8675 s.op->newline(-1) << "}";
8676 s.op->newline() << "mutex_init (& stap_uprobes_lock);";
935447c8 8677
89ba3085
FCE
8678 // Set up the task_finders
8679 s.op->newline() << "for (i=0; i<sizeof(stap_uprobe_finders)/sizeof(stap_uprobe_finders[0]); i++) {";
8680 s.op->newline(1) << "struct stap_uprobe_tf *stf = & stap_uprobe_finders[i];";
c57ea854 8681 s.op->newline() << "probe_point = stf->pathname;"; // for error messages; XXX: would prefer pp() or something better
89ba3085 8682 s.op->newline() << "rc = stap_register_task_finder_target (& stf->finder);";
935447c8 8683
5e112f92
FCE
8684 // NB: if (rc), there is no need (XXX: nor any way) to clean up any
8685 // finders already registered, since mere registration does not
8686 // cause any utrace or memory allocation actions. That happens only
8687 // later, once the task finder engine starts running. So, for a
8688 // partial initialization requiring unwind, we need do nothing.
8689 s.op->newline() << "if (rc) break;";
a7a68293 8690
888af770
FCE
8691 s.op->newline(-1) << "}";
8692}
d0ea46ce 8693
d0a7f5a9 8694
888af770 8695void
2b69faaf 8696uprobe_derived_probe_group::emit_module_utrace_exit (systemtap_session& s)
888af770
FCE
8697{
8698 if (probes.empty()) return;
2b69faaf 8699 s.op->newline() << "/* ---- utrace uprobes ---- */";
e56e51c9 8700
6d0f3f0c
FCE
8701 // NB: there is no stap_unregister_task_finder_target call;
8702 // important stuff like utrace cleanups are done by
d41d451c
FCE
8703 // __stp_task_finder_cleanup() via stap_stop_task_finder().
8704 //
8705 // This function blocks until all callbacks are completed, so there
8706 // is supposed to be no possibility of any registration-related code starting
8707 // to run in parallel with our shutdown here. So we don't need to protect the
8708 // stap_uprobes[] array with the mutex.
d0a7f5a9 8709
01b05e2e 8710 s.op->newline() << "for (j=0; j<MAXUPROBES; j++) {";
5e112f92 8711 s.op->newline(1) << "struct stap_uprobe *sup = & stap_uprobes[j];";
89ba3085 8712 s.op->newline() << "const struct stap_uprobe_spec *sups = &stap_uprobe_specs [sup->spec_index];";
6d0f3f0c 8713 s.op->newline() << "if (sup->spec_index < 0) continue;"; // free slot
3568f1dd 8714
8faa1fc5 8715 // PR10655: decrement that ENABLED semaphore
c116c31b 8716 s.op->newline() << "if (sup->sdt_sem_address) {";
8faa1fc5
FCE
8717 s.op->newline(1) << "unsigned short sdt_semaphore;"; // NB: fixed size
8718 s.op->newline() << "pid_t pid = (sups->return_p ? sup->urp.u.pid : sup->up.pid);";
8719 s.op->newline() << "struct task_struct *tsk;";
8720 s.op->newline() << "rcu_read_lock();";
6846cfc8 8721
86229a55
DS
8722 // Do a pid->task_struct* lookup. For 2.6.24+, this code assumes
8723 // that the pid is always in the global namespace, not in any
8724 // private namespace.
8faa1fc5 8725 s.op->newline() << "#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,24)";
86229a55
DS
8726 // We'd like to call find_task_by_pid_ns() here, but it isn't
8727 // exported. So, we call what it calls...
8728 s.op->newline() << " tsk = pid_task(find_pid_ns(pid, &init_pid_ns), PIDTYPE_PID);";
8faa1fc5
FCE
8729 s.op->newline() << "#else";
8730 s.op->newline() << " tsk = find_task_by_pid (pid);";
8731 s.op->newline() << "#endif /* 2.6.24 */";
8faa1fc5
FCE
8732
8733 s.op->newline() << "if (tsk) {"; // just in case the thing exited while we weren't watching
3c5b8e2b 8734 s.op->newline(1) << "if (__access_process_vm_noflush(tsk, sup->sdt_sem_address, &sdt_semaphore, sizeof(sdt_semaphore), 0)) {";
63b4fd14 8735 s.op->newline(1) << "sdt_semaphore --;";
903b9fcd 8736 s.op->newline() << "#ifdef DEBUG_UPROBES";
c116c31b 8737 s.op->newline() << "_stp_dbug (__FUNCTION__,__LINE__, \"-semaphore %#x @ %#lx\\n\", sdt_semaphore, sup->sdt_sem_address);";
903b9fcd 8738 s.op->newline() << "#endif";
3c5b8e2b 8739 s.op->newline() << "__access_process_vm_noflush(tsk, sup->sdt_sem_address, &sdt_semaphore, sizeof(sdt_semaphore), 1);";
93c84191 8740 s.op->newline(-1) << "}";
8faa1fc5
FCE
8741 // XXX: need to analyze possibility of race condition
8742 s.op->newline(-1) << "}";
8743 s.op->newline() << "rcu_read_unlock();";
8744 s.op->newline(-1) << "}";
6846cfc8 8745
3568f1dd
FCE
8746 s.op->newline() << "if (sups->return_p) {";
8747 s.op->newline(1) << "#ifdef DEBUG_UPROBES";
89ba3085 8748 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 8749 s.op->newline() << "#endif";
80b4ad8b
FCE
8750 // NB: PR6829 does not change that we still need to unregister at
8751 // *this* time -- when the script as a whole exits.
3568f1dd
FCE
8752 s.op->newline() << "unregister_uretprobe (& sup->urp);";
8753 s.op->newline(-1) << "} else {";
8754 s.op->newline(1) << "#ifdef DEBUG_UPROBES";
8faa1fc5 8755 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
8756 s.op->newline() << "#endif";
8757 s.op->newline() << "unregister_uprobe (& sup->up);";
8758 s.op->newline(-1) << "}";
935447c8 8759
6d0f3f0c 8760 s.op->newline() << "sup->spec_index = -1;";
935447c8 8761
3568f1dd
FCE
8762 // XXX: uprobe missed counts?
8763
6d0f3f0c 8764 s.op->newline(-1) << "}";
935447c8 8765
5e112f92 8766 s.op->newline() << "mutex_destroy (& stap_uprobes_lock);";
935447c8
DS
8767}
8768
2b69faaf
JS
8769
8770void
8771uprobe_derived_probe_group::emit_module_inode_decls (systemtap_session& s)
8772{
8773 if (probes.empty()) return;
8774 s.op->newline() << "/* ---- inode uprobes ---- */";
cfcab6c7 8775 emit_module_maxuprobes (s);
2ba1736a 8776 s.op->newline() << "#include \"linux/uprobes-inode.c\"";
2b69faaf
JS
8777
8778 // Write the probe handler.
79af55c3
JS
8779 s.op->newline() << "static int stapiu_probe_handler "
8780 << "(struct stapiu_consumer *sup, struct pt_regs *regs) {";
8781 s.op->newline(1);
2dbbd473
JS
8782
8783 // Since we're sharing the entry function, we have to dynamically choose the probe_type
8784 string probe_type = "(sup->return_p ? stp_probe_type_uretprobe : stp_probe_type_uprobe)";
71db462b 8785 common_probe_entryfn_prologue (s, "STAP_SESSION_RUNNING", "sup->probe",
2dbbd473
JS
8786 probe_type);
8787
3bff6634 8788 s.op->newline() << "c->uregs = regs;";
e04b5d74 8789 s.op->newline() << "c->user_mode_p = 1;";
497cfcb2 8790 // NB: IP is already set by stapiu_probe_prehandler in uprobes-inode.c
2b69faaf 8791 s.op->newline() << "(*sup->probe->ph) (c);";
39e63bae 8792
ef1337ee 8793 common_probe_entryfn_epilogue (s, true, otf_safe_context(s));
2b69faaf
JS
8794 s.op->newline() << "return 0;";
8795 s.op->newline(-1) << "}";
8796 s.op->assert_0_indent();
8797
8798 // Index of all the modules for which we need inodes.
8799 map<string, unsigned> module_index;
8800 unsigned module_index_ctr = 0;
8801
8802 // Discover and declare targets for each unique path.
cfcab6c7 8803 s.op->newline() << "static struct stapiu_target "
2b69faaf
JS
8804 << "stap_inode_uprobe_targets[] = {";
8805 s.op->indent(1);
8806 for (unsigned i=0; i<probes.size(); i++)
8807 {
8808 uprobe_derived_probe *p = probes[i];
cfcab6c7
JS
8809 const string key = make_pbm_key(p);
8810 if (module_index.find (key) == module_index.end())
2b69faaf 8811 {
cfcab6c7
JS
8812 module_index[key] = module_index_ctr++;
8813 s.op->newline() << "{";
8814 s.op->line() << " .finder={";
b78a0fbb 8815 s.op->line() << " .purpose=\"inode-uprobes\",";
cfcab6c7
JS
8816 if (p->pid != 0)
8817 s.op->line() << " .pid=" << p->pid << ",";
8818
8819 if (p->section == "") // .statement(addr).absolute XXX?
8820 s.op->line() << " .callback=&stapiu_process_found,";
8821 else if (p->section == ".absolute") // proxy for ET_EXEC -> exec()'d program
8822 {
8823 s.op->line() << " .procname=" << lex_cast_qstring(p->module) << ",";
8824 s.op->line() << " .callback=&stapiu_process_found,";
8825 }
8826 else if (p->section != ".absolute") // ET_DYN
8827 {
8828 if (p->has_library)
8829 s.op->line() << " .procname=\"" << p->path << "\", ";
8830 s.op->line() << " .mmap_callback=&stapiu_mmap_found, ";
8831 s.op->line() << " .munmap_callback=&stapiu_munmap_found, ";
8832 s.op->line() << " .callback=&stapiu_process_munmap,";
8833 }
8834 s.op->line() << " },";
8835 s.op->line() << " .filename=" << lex_cast_qstring(p->module) << ",";
8836 s.op->line() << " },";
2b69faaf
JS
8837 }
8838 }
8839 s.op->newline(-1) << "};";
8840 s.op->assert_0_indent();
8841
8842 // Declare the actual probes.
3689db05
SC
8843 unsigned pci;
8844 for (pci=0; pci<probes.size(); pci++)
8845 {
8846 // List of perf counters used by each probe
8847 // This list is an index into struct stap_perf_probe,
8848 uprobe_derived_probe *p = probes[pci];
aa2c662f 8849 std::set<string>::iterator pcii;
3689db05
SC
8850 s.op->newline() << "long perf_counters_" + lex_cast(pci) + "[] = {";
8851 for (pcii = p->perf_counter_refs.begin();
8852 pcii != p->perf_counter_refs.end(); pcii++)
8853 {
aa2c662f 8854 vector<std::pair<string,string> >:: iterator it;
3689db05
SC
8855 unsigned i = 0;
8856 // Find the associated perf.counter probe
4fa83377
SC
8857 for (it=s.perf_counters.begin() ;
8858 it != s.perf_counters.end(); it++, i++)
aa2c662f 8859 if ((*it).first == (*pcii))
3689db05
SC
8860 break;
8861 s.op->line() << lex_cast(i) << ", ";
8862 }
8863 s.op->newline() << "};";
8864 }
8865
cfcab6c7 8866 s.op->newline() << "static struct stapiu_consumer "
2b69faaf
JS
8867 << "stap_inode_uprobe_consumers[] = {";
8868 s.op->indent(1);
8869 for (unsigned i=0; i<probes.size(); i++)
8870 {
8871 uprobe_derived_probe *p = probes[i];
cfcab6c7
JS
8872 unsigned index = module_index[make_pbm_key(p)];
8873 s.op->newline() << "{";
79af55c3
JS
8874 if (p->has_return)
8875 s.op->line() << " .return_p=1,";
cfcab6c7
JS
8876 s.op->line() << " .target=&stap_inode_uprobe_targets[" << index << "],";
8877 s.op->line() << " .offset=(loff_t)0x" << hex << p->addr << dec << "ULL,";
8878 if (p->sdt_semaphore_addr)
8879 s.op->line() << " .sdt_sem_offset=(loff_t)0x"
8880 << hex << p->sdt_semaphore_addr << dec << "ULL,";
0d049a1d 8881 // XXX: don't bother emit if array is empty
3689db05
SC
8882 s.op->line() << " .perf_counters_dim=ARRAY_SIZE(perf_counters_" << lex_cast(i) << "),";
8883 // List of perf counters used by a probe from above
0d049a1d 8884 s.op->line() << " .perf_counters=perf_counters_" + lex_cast(i) + ",";
cfcab6c7
JS
8885 s.op->line() << " .probe=" << common_probe_init (p) << ",";
8886 s.op->line() << " },";
2b69faaf
JS
8887 }
8888 s.op->newline(-1) << "};";
8889 s.op->assert_0_indent();
8890}
8891
8892
8893void
8894uprobe_derived_probe_group::emit_module_inode_init (systemtap_session& s)
8895{
8896 if (probes.empty()) return;
8897 s.op->newline() << "/* ---- inode uprobes ---- */";
b0f614a0
DS
8898 // Let stapiu_init() handle reporting errors by setting probe_point
8899 // to NULL.
8900 s.op->newline() << "probe_point = NULL;";
cfcab6c7 8901 s.op->newline() << "rc = stapiu_init ("
2b69faaf
JS
8902 << "stap_inode_uprobe_targets, "
8903 << "ARRAY_SIZE(stap_inode_uprobe_targets), "
8904 << "stap_inode_uprobe_consumers, "
8905 << "ARRAY_SIZE(stap_inode_uprobe_consumers));";
8906}
8907
8908
3642eb17
JL
8909void
8910uprobe_derived_probe_group::emit_module_inode_refresh (systemtap_session& s)
8911{
8912 if (probes.empty()) return;
8913 s.op->newline() << "/* ---- inode uprobes ---- */";
3642eb17
JL
8914 s.op->newline() << "stapiu_refresh ("
8915 << "stap_inode_uprobe_targets, "
8916 << "ARRAY_SIZE(stap_inode_uprobe_targets));";
3642eb17
JL
8917}
8918
8919
2b69faaf
JS
8920void
8921uprobe_derived_probe_group::emit_module_inode_exit (systemtap_session& s)
8922{
8923 if (probes.empty()) return;
8924 s.op->newline() << "/* ---- inode uprobes ---- */";
cfcab6c7 8925 s.op->newline() << "stapiu_exit ("
2b69faaf
JS
8926 << "stap_inode_uprobe_targets, "
8927 << "ARRAY_SIZE(stap_inode_uprobe_targets), "
8928 << "stap_inode_uprobe_consumers, "
8929 << "ARRAY_SIZE(stap_inode_uprobe_consumers));";
8930}
8931
8932
3a894f7e
JS
8933void
8934uprobe_derived_probe_group::emit_module_dyninst_decls (systemtap_session& s)
8935{
8936 if (probes.empty()) return;
8937 s.op->newline() << "/* ---- dyninst uprobes ---- */";
8938 emit_module_maxuprobes (s);
e00f3fb7 8939 s.op->newline() << "#include \"dyninst/uprobes.h\"";
3a894f7e 8940
f31a77f5
DS
8941 // Let the dynprobe_derived_probe_group handle outputting targets
8942 // and probes. This allows us to merge different types of probes.
8943 s.op->newline() << "static struct stapdu_probe stapdu_probes[];";
8944 for (unsigned i = 0; i < probes.size(); i++)
3a894f7e
JS
8945 {
8946 uprobe_derived_probe *p = probes[i];
e00f3fb7 8947
f31a77f5
DS
8948 dynprobe_add_uprobe(s, p->module, p->addr, p->sdt_semaphore_addr,
8949 (p->has_return ? "STAPDYN_PROBE_FLAG_RETURN" : "0"),
8950 common_probe_init(p));
3a894f7e 8951 }
874d38bf
JS
8952 // loc2c-generated code assumes pt_regs are available, so use this to make
8953 // sure we always have *something* for it to dereference...
f31a77f5 8954 s.op->newline() << "static struct pt_regs stapdu_dummy_uregs;";
e00f3fb7 8955
3a894f7e
JS
8956 // Write the probe handler.
8957 // NB: not static, so dyninst can find it
8958 s.op->newline() << "int enter_dyninst_uprobe "
8959 << "(uint64_t index, struct pt_regs *regs) {";
e00f3fb7 8960 s.op->newline(1) << "struct stapdu_probe *sup = &stapdu_probes[index];";
92b97104
JS
8961
8962 // Since we're sharing the entry function, we have to dynamically choose the probe_type
8963 string probe_type = "((sup->flags & STAPDYN_PROBE_FLAG_RETURN) ?"
8964 " stp_probe_type_uretprobe : stp_probe_type_uprobe)";
71db462b 8965 common_probe_entryfn_prologue (s, "STAP_SESSION_RUNNING", "sup->probe",
92b97104
JS
8966 probe_type);
8967
874d38bf 8968 s.op->newline() << "c->uregs = regs ?: &stapdu_dummy_uregs;";
e04b5d74 8969 s.op->newline() << "c->user_mode_p = 1;";
3a894f7e
JS
8970 // XXX: once we have regs, check how dyninst sets the IP
8971 // XXX: the way that dyninst rewrites stuff is probably going to be
8972 // ... very confusing to our backtracer (at least if we stay in process)
8973 s.op->newline() << "(*sup->probe->ph) (c);";
ef1337ee 8974 common_probe_entryfn_epilogue (s, true, otf_safe_context(s));
3a894f7e
JS
8975 s.op->newline() << "return 0;";
8976 s.op->newline(-1) << "}";
3debb935 8977 s.op->newline() << "#include \"dyninst/uprobes-regs.c\"";
3a894f7e
JS
8978 s.op->assert_0_indent();
8979}
8980
8981
8982void
8983uprobe_derived_probe_group::emit_module_dyninst_init (systemtap_session& s)
8984{
8985 if (probes.empty()) return;
8986
8987 /* stapdyn handles the dirty work via dyninst */
8988 s.op->newline() << "/* ---- dyninst uprobes ---- */";
8989 s.op->newline() << "/* this section left intentionally blank */";
8990}
8991
8992
8993void
8994uprobe_derived_probe_group::emit_module_dyninst_exit (systemtap_session& s)
8995{
8996 if (probes.empty()) return;
8997
8998 /* stapdyn handles the dirty work via dyninst */
8999 s.op->newline() << "/* ---- dyninst uprobes ---- */";
9000 s.op->newline() << "/* this section left intentionally blank */";
9001}
9002
9003
2b69faaf
JS
9004void
9005uprobe_derived_probe_group::emit_module_decls (systemtap_session& s)
9006{
ac3af990 9007 if (s.runtime_usermode_p())
4441e344
JS
9008 emit_module_dyninst_decls (s);
9009 else if (kernel_supports_inode_uprobes (s))
2b69faaf
JS
9010 emit_module_inode_decls (s);
9011 else
9012 emit_module_utrace_decls (s);
9013}
9014
9015
9016void
9017uprobe_derived_probe_group::emit_module_init (systemtap_session& s)
9018{
ac3af990 9019 if (s.runtime_usermode_p())
4441e344
JS
9020 emit_module_dyninst_init (s);
9021 else if (kernel_supports_inode_uprobes (s))
2b69faaf
JS
9022 emit_module_inode_init (s);
9023 else
9024 emit_module_utrace_init (s);
9025}
9026
9027
3642eb17
JL
9028void
9029uprobe_derived_probe_group::emit_module_refresh (systemtap_session& s)
9030{
9031 if (!s.runtime_usermode_p() && kernel_supports_inode_uprobes (s))
9032 emit_module_inode_refresh (s);
9033}
9034
9035
2b69faaf
JS
9036void
9037uprobe_derived_probe_group::emit_module_exit (systemtap_session& s)
9038{
ac3af990 9039 if (s.runtime_usermode_p())
4441e344
JS
9040 emit_module_dyninst_exit (s);
9041 else if (kernel_supports_inode_uprobes (s))
2b69faaf
JS
9042 emit_module_inode_exit (s);
9043 else
9044 emit_module_utrace_exit (s);
9045}
9046
9047
e6fe60e7
AM
9048// ------------------------------------------------------------------------
9049// Kprobe derived probes
9050// ------------------------------------------------------------------------
9051
4627ed58 9052static const string TOK_KPROBE("kprobe");
935447c8 9053
bae55db9 9054struct kprobe_derived_probe: public derived_probe
d0ea46ce 9055{
23dc94f6
DS
9056 kprobe_derived_probe (systemtap_session& sess,
9057 vector<derived_probe *> & results,
9058 probe *base,
bae55db9
JS
9059 probe_point *location,
9060 const string& name,
9061 int64_t stmt_addr,
e6afb07b 9062 bool has_call,
bae55db9
JS
9063 bool has_return,
9064 bool has_statement,
9065 bool has_maxactive,
b642c901
SC
9066 bool has_path,
9067 bool has_library,
9068 long maxactive_val,
9069 const string& path,
9070 const string& library
bae55db9
JS
9071 );
9072 string symbol_name;
9073 Dwarf_Addr addr;
e6afb07b 9074 bool has_call;
bae55db9
JS
9075 bool has_return;
9076 bool has_statement;
9077 bool has_maxactive;
b642c901
SC
9078 bool has_path;
9079 bool has_library;
bae55db9 9080 long maxactive_val;
b642c901
SC
9081 string path;
9082 string library;
bae55db9
JS
9083 bool access_var;
9084 void printsig (std::ostream &o) const;
9085 void join_group (systemtap_session& s);
9086};
d0ea46ce 9087
bae55db9
JS
9088struct kprobe_derived_probe_group: public derived_probe_group
9089{
9090private:
9091 multimap<string,kprobe_derived_probe*> probes_by_module;
9092 typedef multimap<string,kprobe_derived_probe*>::iterator p_b_m_iterator;
d0ea46ce 9093
bae55db9
JS
9094public:
9095 void enroll (kprobe_derived_probe* probe);
9096 void emit_module_decls (systemtap_session& s);
9097 void emit_module_init (systemtap_session& s);
9098 void emit_module_exit (systemtap_session& s);
9099};
d0ea46ce 9100
23dc94f6
DS
9101struct kprobe_var_expanding_visitor: public var_expanding_visitor
9102{
9103 systemtap_session& sess;
9104 block *add_block;
9105 block *add_call_probe; // synthesized from .return probes with saved $vars
9106 bool add_block_tid, add_call_probe_tid;
bd5b25e1 9107 bool has_return;
23dc94f6 9108
bd5b25e1 9109 kprobe_var_expanding_visitor(systemtap_session& sess, bool has_return):
23dc94f6 9110 sess(sess), add_block(NULL), add_call_probe(NULL),
bd5b25e1
JS
9111 add_block_tid(false), add_call_probe_tid(false),
9112 has_return(has_return) {}
23dc94f6
DS
9113
9114 void visit_entry_op (entry_op* e);
9115};
9116
9117
9118kprobe_derived_probe::kprobe_derived_probe (systemtap_session& sess,
9119 vector<derived_probe *> & results,
9120 probe *base,
e6fe60e7 9121 probe_point *location,
b6371390 9122 const string& name,
e6fe60e7 9123 int64_t stmt_addr,
e6afb07b 9124 bool has_call,
b6371390
JS
9125 bool has_return,
9126 bool has_statement,
9127 bool has_maxactive,
b642c901
SC
9128 bool has_path,
9129 bool has_library,
9130 long maxactive_val,
9131 const string& path,
9132 const string& library
b6371390 9133 ):
4c5d1300 9134 derived_probe (base, location, true /* .components soon rewritten */ ),
e6afb07b 9135 symbol_name (name), addr (stmt_addr), has_call (has_call),
b6371390 9136 has_return (has_return), has_statement (has_statement),
b642c901
SC
9137 has_maxactive (has_maxactive), has_path (has_path),
9138 has_library (has_library),
9139 maxactive_val (maxactive_val),
9140 path (path), library (library)
e6fe60e7
AM
9141{
9142 this->tok = base->tok;
9143 this->access_var = false;
d0ea46ce 9144
e6fe60e7
AM
9145#ifndef USHRT_MAX
9146#define USHRT_MAX 32767
9147#endif
d0ea46ce 9148
46856d8d
JS
9149 // Expansion of $target variables in the probe body produces an error during
9150 // translate phase, since we're not using debuginfo
d0ea46ce 9151
e6fe60e7 9152 vector<probe_point::component*> comps;
46856d8d 9153 comps.push_back (new probe_point::component(TOK_KPROBE));
e6fe60e7 9154
46856d8d
JS
9155 if (has_statement)
9156 {
9ea68eb9
JS
9157 comps.push_back (new probe_point::component(TOK_STATEMENT,
9158 new literal_number(addr, true)));
46856d8d
JS
9159 comps.push_back (new probe_point::component(TOK_ABSOLUTE));
9160 }
9161 else
9162 {
9163 size_t pos = name.find(':');
9164 if (pos != string::npos)
d0ea46ce 9165 {
46856d8d
JS
9166 string module = name.substr(0, pos);
9167 string function = name.substr(pos + 1);
9168 comps.push_back (new probe_point::component(TOK_MODULE, new literal_string(module)));
9169 comps.push_back (new probe_point::component(TOK_FUNCTION, new literal_string(function)));
9170 }
9171 else
9172 comps.push_back (new probe_point::component(TOK_FUNCTION, new literal_string(name)));
46856d8d 9173 }
d0ea46ce 9174
e6afb07b
JL
9175 if (has_call)
9176 comps.push_back (new probe_point::component(TOK_CALL));
b6371390
JS
9177 if (has_return)
9178 comps.push_back (new probe_point::component(TOK_RETURN));
9179 if (has_maxactive)
9180 comps.push_back (new probe_point::component(TOK_MAXACTIVE, new literal_number(maxactive_val)));
d0ea46ce 9181
bd5b25e1 9182 kprobe_var_expanding_visitor v (sess, has_return);
23dc94f6
DS
9183 v.replace (this->body);
9184
9185 // If during target-variable-expanding the probe, we added a new block
9186 // of code, add it to the start of the probe.
9187 if (v.add_block)
9188 this->body = new block(v.add_block, this->body);
9189
9190 // If when target-variable-expanding the probe, we need to
9191 // synthesize a sibling function-entry probe. We don't go through
9192 // the whole probe derivation business (PR10642) that could lead to
9193 // wildcard/alias resolution, or for that dwarf-induced duplication.
9194 if (v.add_call_probe)
9195 {
9196 assert (has_return);
9197
9198 // We temporarily replace base.
9199 statement* old_body = base->body;
9200 base->body = v.add_call_probe;
9201
9202 derived_probe *entry_handler
9203 = new kprobe_derived_probe (sess, results, base, location, name, 0,
e6afb07b
JL
9204 true /* has_call */, false /* has_return */,
9205 has_statement, has_maxactive, has_path,
9206 has_library, maxactive_val, path, library);
23dc94f6
DS
9207 results.push_back (entry_handler);
9208
9209 base->body = old_body;
9210 }
9211
e6fe60e7
AM
9212 this->sole_location()->components = comps;
9213}
d0ea46ce 9214
e6fe60e7
AM
9215void kprobe_derived_probe::printsig (ostream& o) const
9216{
9217 sole_location()->print (o);
9218 o << " /* " << " name = " << symbol_name << "*/";
9219 printsig_nested (o);
9220}
d0ea46ce 9221
e6fe60e7
AM
9222void kprobe_derived_probe::join_group (systemtap_session& s)
9223{
e6fe60e7
AM
9224 if (! s.kprobe_derived_probes)
9225 s.kprobe_derived_probes = new kprobe_derived_probe_group ();
9226 s.kprobe_derived_probes->enroll (this);
ca6d3b0f 9227 this->group = s.kprobe_derived_probes;
e6fe60e7 9228}
d0ea46ce 9229
e6fe60e7
AM
9230void kprobe_derived_probe_group::enroll (kprobe_derived_probe* p)
9231{
9232 probes_by_module.insert (make_pair (p->symbol_name, p));
9233 // probes of same symbol should share single kprobe/kretprobe
9234}
d0ea46ce 9235
e6fe60e7
AM
9236void
9237kprobe_derived_probe_group::emit_module_decls (systemtap_session& s)
9238{
9239 if (probes_by_module.empty()) return;
d0ea46ce 9240
e6fe60e7 9241 s.op->newline() << "/* ---- kprobe-based probes ---- */";
d0ea46ce 9242
e6fe60e7
AM
9243 // Warn of misconfigured kernels
9244 s.op->newline() << "#if ! defined(CONFIG_KPROBES)";
9245 s.op->newline() << "#error \"Need CONFIG_KPROBES!\"";
9246 s.op->newline() << "#endif";
9247 s.op->newline();
d0ea46ce 9248
f07c3b68 9249 s.op->newline() << "#ifndef KRETACTIVE";
1ee6b5fc 9250 s.op->newline() << "#define KRETACTIVE (max(15,6*(int)num_possible_cpus()))";
f07c3b68
FCE
9251 s.op->newline() << "#endif";
9252
e6fe60e7 9253 // Forward declare the master entry functions
88747011 9254 s.op->newline() << "static int enter_kprobe2_probe (struct kprobe *inst,";
e6fe60e7 9255 s.op->line() << " struct pt_regs *regs);";
88747011 9256 s.op->newline() << "static int enter_kretprobe2_probe (struct kretprobe_instance *inst,";
e6fe60e7 9257 s.op->line() << " struct pt_regs *regs);";
d0ea46ce 9258
e6fe60e7
AM
9259 // Emit an array of kprobe/kretprobe pointers
9260 s.op->newline() << "#if defined(STAPCONF_UNREGISTER_KPROBES)";
c9116e99 9261 s.op->newline() << "static void * stap_unreg_kprobes2[" << probes_by_module.size() << "];";
e6fe60e7 9262 s.op->newline() << "#endif";
d0ea46ce 9263
e6fe60e7 9264 // Emit the actual probe list.
d0ea46ce 9265
e6fe60e7
AM
9266 s.op->newline() << "static struct stap_dwarfless_kprobe {";
9267 s.op->newline(1) << "union { struct kprobe kp; struct kretprobe krp; } u;";
9268 s.op->newline() << "#ifdef __ia64__";
9269 s.op->newline() << "struct kprobe dummy;";
9270 s.op->newline() << "#endif";
9271 s.op->newline(-1) << "} stap_dwarfless_kprobes[" << probes_by_module.size() << "];";
9272 // NB: bss!
d0ea46ce 9273
e6fe60e7
AM
9274 s.op->newline() << "static struct stap_dwarfless_probe {";
9275 s.op->newline(1) << "const unsigned return_p:1;";
9276 s.op->newline() << "const unsigned maxactive_p:1;";
b350f56b 9277 s.op->newline() << "const unsigned optional_p:1;";
e6fe60e7
AM
9278 s.op->newline() << "unsigned registered_p:1;";
9279 s.op->newline() << "const unsigned short maxactive_val;";
935447c8 9280
e6fe60e7
AM
9281 // Function Names are mostly small and uniform enough to justify putting
9282 // char[MAX]'s into the array instead of relocated char*'s.
935447c8 9283
faea5e16
JS
9284 size_t symbol_string_name_max = 0;
9285 size_t symbol_string_name_tot = 0;
e6fe60e7 9286 for (p_b_m_iterator it = probes_by_module.begin(); it != probes_by_module.end(); it++)
6270adc1 9287 {
e6fe60e7
AM
9288 kprobe_derived_probe* p = it->second;
9289#define DOIT(var,expr) do { \
9290 size_t var##_size = (expr) + 1; \
9291 var##_max = max (var##_max, var##_size); \
9292 var##_tot += var##_size; } while (0)
e6fe60e7
AM
9293 DOIT(symbol_string_name, p->symbol_name.size());
9294#undef DOIT
6270adc1
MH
9295 }
9296
e6fe60e7
AM
9297#define CALCIT(var) \
9298 s.op->newline() << "const char " << #var << "[" << var##_name_max << "] ;";
935447c8 9299
e6fe60e7
AM
9300 CALCIT(symbol_string);
9301#undef CALCIT
6270adc1 9302
bd659351 9303 s.op->newline() << "unsigned long address;";
7c3e97f4 9304 s.op->newline() << "const struct stap_probe * const probe;";
e6fe60e7
AM
9305 s.op->newline(-1) << "} stap_dwarfless_probes[] = {";
9306 s.op->indent(1);
6270adc1 9307
e6fe60e7
AM
9308 for (p_b_m_iterator it = probes_by_module.begin(); it != probes_by_module.end(); it++)
9309 {
9310 kprobe_derived_probe* p = it->second;
9311 s.op->newline() << "{";
9312 if (p->has_return)
9313 s.op->line() << " .return_p=1,";
6270adc1 9314
e6fe60e7
AM
9315 if (p->has_maxactive)
9316 {
9317 s.op->line() << " .maxactive_p=1,";
9318 assert (p->maxactive_val >= 0 && p->maxactive_val <= USHRT_MAX);
9319 s.op->line() << " .maxactive_val=" << p->maxactive_val << ",";
9320 }
6270adc1 9321
b350f56b
JS
9322 if (p->locations[0]->optional)
9323 s.op->line() << " .optional_p=1,";
9324
e6fe60e7 9325 if (p->has_statement)
c8d9d15e 9326 s.op->line() << " .address=(unsigned long)0x" << hex << p->addr << dec << "ULL,";
e6fe60e7 9327 else
c8d9d15e 9328 s.op->line() << " .symbol_string=\"" << p->symbol_name << "\",";
5d67b47c 9329
faea5e16 9330 s.op->line() << " .probe=" << common_probe_init (p) << ",";
e6fe60e7 9331 s.op->line() << " },";
935447c8
DS
9332 }
9333
e6fe60e7 9334 s.op->newline(-1) << "};";
5d67b47c 9335
e6fe60e7
AM
9336 // Emit the kprobes callback function
9337 s.op->newline();
88747011 9338 s.op->newline() << "static int enter_kprobe2_probe (struct kprobe *inst,";
e6fe60e7
AM
9339 s.op->line() << " struct pt_regs *regs) {";
9340 // NB: as of PR5673, the kprobe|kretprobe union struct is in BSS
9341 s.op->newline(1) << "int kprobe_idx = ((uintptr_t)inst-(uintptr_t)stap_dwarfless_kprobes)/sizeof(struct stap_dwarfless_kprobe);";
9342 // Check that the index is plausible
9343 s.op->newline() << "struct stap_dwarfless_probe *sdp = &stap_dwarfless_probes[";
9344 s.op->line() << "((kprobe_idx >= 0 && kprobe_idx < " << probes_by_module.size() << ")?";
9345 s.op->line() << "kprobe_idx:0)"; // NB: at least we avoid memory corruption
9346 // XXX: it would be nice to give a more verbose error though; BUG_ON later?
9347 s.op->line() << "];";
71db462b 9348 common_probe_entryfn_prologue (s, "STAP_SESSION_RUNNING", "sdp->probe",
cda141c2 9349 "stp_probe_type_kprobe");
d9aed31e 9350 s.op->newline() << "c->kregs = regs;";
6415ddde
MW
9351
9352 // Make it look like the IP is set as it wouldn't have been replaced
9353 // by a breakpoint instruction when calling real probe handler. Reset
9354 // IP regs on return, so we don't confuse kprobes. PR10458
9355 s.op->newline() << "{";
9356 s.op->indent(1);
d9aed31e 9357 s.op->newline() << "unsigned long kprobes_ip = REG_IP(c->kregs);";
259d54c0 9358 s.op->newline() << "SET_REG_IP(regs, (unsigned long) inst->addr);";
26e63673 9359 s.op->newline() << "(*sdp->probe->ph) (c);";
259d54c0 9360 s.op->newline() << "SET_REG_IP(regs, kprobes_ip);";
6415ddde
MW
9361 s.op->newline(-1) << "}";
9362
ef1337ee 9363 common_probe_entryfn_epilogue (s, true, otf_safe_context(s));
e6fe60e7
AM
9364 s.op->newline() << "return 0;";
9365 s.op->newline(-1) << "}";
935447c8 9366
e6fe60e7
AM
9367 // Same for kretprobes
9368 s.op->newline();
88747011 9369 s.op->newline() << "static int enter_kretprobe2_probe (struct kretprobe_instance *inst,";
e6fe60e7
AM
9370 s.op->line() << " struct pt_regs *regs) {";
9371 s.op->newline(1) << "struct kretprobe *krp = inst->rp;";
935447c8 9372
e6fe60e7
AM
9373 // NB: as of PR5673, the kprobe|kretprobe union struct is in BSS
9374 s.op->newline() << "int kprobe_idx = ((uintptr_t)krp-(uintptr_t)stap_dwarfless_kprobes)/sizeof(struct stap_dwarfless_kprobe);";
9375 // Check that the index is plausible
9376 s.op->newline() << "struct stap_dwarfless_probe *sdp = &stap_dwarfless_probes[";
9377 s.op->line() << "((kprobe_idx >= 0 && kprobe_idx < " << probes_by_module.size() << ")?";
9378 s.op->line() << "kprobe_idx:0)"; // NB: at least we avoid memory corruption
9379 // XXX: it would be nice to give a more verbose error though; BUG_ON later?
9380 s.op->line() << "];";
935447c8 9381
71db462b 9382 common_probe_entryfn_prologue (s, "STAP_SESSION_RUNNING", "sdp->probe",
cda141c2 9383 "stp_probe_type_kretprobe");
d9aed31e 9384 s.op->newline() << "c->kregs = regs;";
6dceb5c9 9385 s.op->newline() << "c->ips.krp.pi = inst;"; // for assisting runtime's backtrace logic
6415ddde
MW
9386
9387 // Make it look like the IP is set as it wouldn't have been replaced
9388 // by a breakpoint instruction when calling real probe handler. Reset
9389 // IP regs on return, so we don't confuse kprobes. PR10458
9390 s.op->newline() << "{";
9391 s.op->indent(1);
d9aed31e 9392 s.op->newline() << "unsigned long kprobes_ip = REG_IP(c->kregs);";
259d54c0 9393 s.op->newline() << "SET_REG_IP(regs, (unsigned long) inst->rp->kp.addr);";
26e63673 9394 s.op->newline() << "(*sdp->probe->ph) (c);";
259d54c0 9395 s.op->newline() << "SET_REG_IP(regs, kprobes_ip);";
6415ddde
MW
9396 s.op->newline(-1) << "}";
9397
ef1337ee 9398 common_probe_entryfn_epilogue (s, true, otf_safe_context(s));
e6fe60e7
AM
9399 s.op->newline() << "return 0;";
9400 s.op->newline(-1) << "}";
bd659351 9401
03a4ec63 9402 s.op->newline() << "#ifdef STAPCONF_KALLSYMS_ON_EACH_SYMBOL";
bd659351
MW
9403 s.op->newline() << "static int kprobe_resolve(void *data, const char *name,";
9404 s.op->newline() << " struct module *owner,";
9405 s.op->newline() << " unsigned long val) {";
9406 s.op->newline(1) << "int i;";
fc1d2aa2
MW
9407 s.op->newline() << "int *p = (int *) data;";
9408 s.op->newline() << "for (i=0; i<" << probes_by_module.size()
9409 << " && *p > 0; i++) {";
bd659351 9410 s.op->newline(1) << "struct stap_dwarfless_probe *sdp = & stap_dwarfless_probes[i];";
8c272819
YW
9411 s.op->newline() << "if (! sdp->address) {";
9412 s.op->indent(1);
9413 s.op->newline() << "const char *colon;";
9414 s.op->newline() << "if (owner && (colon = strchr(sdp->symbol_string, ':'))) {";
9415 s.op->indent(1);
9416 s.op->newline() << "if ((strlen(owner->name) == (colon - sdp->symbol_string))";
9417 s.op->newline() << " && (strncmp(sdp->symbol_string, owner->name, colon - sdp->symbol_string) == 0)";
9418 s.op->newline() << " && (strcmp(colon + 1, name) == 0)) {";
9419 s.op->newline(1) << "sdp->address = val;";
9420 s.op->newline() << "(*p)--;";
9421 s.op->newline(-1) << "}";
9422 s.op->newline(-1) << "}";
9423 s.op->newline() << "else {";
fc1d2aa2 9424 s.op->newline(1) << "if (strcmp(sdp->symbol_string, name) == 0) {";
bd659351 9425 s.op->newline(1) << "sdp->address = val;";
fc1d2aa2
MW
9426 s.op->newline() << "(*p)--;";
9427 s.op->newline(-1) << "}";
8c272819
YW
9428 s.op->newline(-1) << "}";
9429 s.op->newline(-1) << "}";
9430 s.op->newline(-1) << "}";
fc1d2aa2 9431 s.op->newline() << "return (p > 0) ? 0 : -1;";
bd659351 9432 s.op->newline(-1) << "}";
03a4ec63 9433 s.op->newline() << "#endif";
935447c8
DS
9434}
9435
e6fe60e7 9436
6270adc1 9437void
e6fe60e7 9438kprobe_derived_probe_group::emit_module_init (systemtap_session& s)
6270adc1 9439{
03a4ec63 9440 s.op->newline() << "#ifdef STAPCONF_KALLSYMS_ON_EACH_SYMBOL";
fc1d2aa2
MW
9441 s.op->newline() << "{";
9442 s.op->newline(1) << "int p = 0;";
9443 s.op->newline() << "for (i = 0; i < " << probes_by_module.size() << "; i++) {";
9444 s.op->newline(1) << "struct stap_dwarfless_probe *sdp = & stap_dwarfless_probes[i];";
9445 s.op->newline() << "if (! sdp->address)";
9446 s.op->newline(1) << "p++;";
9447 s.op->newline(-2) << "}";
9448 s.op->newline() << "kallsyms_on_each_symbol(kprobe_resolve, &p);";
9449 s.op->newline(-1) << "}";
03a4ec63 9450 s.op->newline() << "#endif";
bd659351 9451
e6fe60e7 9452 s.op->newline() << "for (i=0; i<" << probes_by_module.size() << "; i++) {";
c8d9d15e 9453 s.op->newline(1) << "struct stap_dwarfless_probe *sdp = & stap_dwarfless_probes[i];";
e6fe60e7 9454 s.op->newline() << "struct stap_dwarfless_kprobe *kp = & stap_dwarfless_kprobes[i];";
c8d9d15e 9455 s.op->newline() << "void *addr = (void *) sdp->address;";
03a4ec63
MW
9456 s.op->newline() << "const char *symbol_name = addr ? NULL : sdp->symbol_string;";
9457
9458 s.op->newline() << "#ifdef STAPCONF_KALLSYMS_ON_EACH_SYMBOL";
bd659351
MW
9459 s.op->newline() << "if (! addr) {";
9460 s.op->newline(1) << "sdp->registered_p = 0;";
9319b767
MW
9461 s.op->newline() << "if (!sdp->optional_p)";
9462 s.op->newline(1) << "_stp_warn (\"probe %s registration error (symbol not found)\", probe_point);";
9463 s.op->newline(-1) << "continue;";
bd659351 9464 s.op->newline(-1) << "}";
03a4ec63
MW
9465 s.op->newline() << "#endif";
9466
26e63673 9467 s.op->newline() << "probe_point = sdp->probe->pp;"; // for error messages
e6fe60e7 9468 s.op->newline() << "if (sdp->return_p) {";
c8d9d15e 9469 s.op->newline(1) << "kp->u.krp.kp.addr = addr;";
766cee5f 9470 s.op->newline() << "#ifdef STAPCONF_KPROBE_SYMBOL_NAME";
03a4ec63 9471 s.op->newline() << "kp->u.krp.kp.symbol_name = (char *) symbol_name;";
766cee5f 9472 s.op->newline() << "#endif";
e6fe60e7
AM
9473 s.op->newline() << "if (sdp->maxactive_p) {";
9474 s.op->newline(1) << "kp->u.krp.maxactive = sdp->maxactive_val;";
9475 s.op->newline(-1) << "} else {";
f07c3b68 9476 s.op->newline(1) << "kp->u.krp.maxactive = KRETACTIVE;";
e6fe60e7 9477 s.op->newline(-1) << "}";
88747011 9478 s.op->newline() << "kp->u.krp.handler = &enter_kretprobe2_probe;";
e6fe60e7
AM
9479 // to ensure safeness of bspcache, always use aggr_kprobe on ia64
9480 s.op->newline() << "#ifdef __ia64__";
e6fe60e7 9481 s.op->newline() << "kp->dummy.addr = kp->u.krp.kp.addr;";
766cee5f 9482 s.op->newline() << "#ifdef STAPCONF_KPROBE_SYMBOL_NAME";
c8d9d15e 9483 s.op->newline() << "kp->dummy.symbol_name = kp->u.krp.kp.symbol_name;";
766cee5f 9484 s.op->newline() << "#endif";
c8d9d15e 9485 s.op->newline() << "kp->dummy.pre_handler = NULL;";
e6fe60e7
AM
9486 s.op->newline() << "rc = register_kprobe (& kp->dummy);";
9487 s.op->newline() << "if (rc == 0) {";
9488 s.op->newline(1) << "rc = register_kretprobe (& kp->u.krp);";
9489 s.op->newline() << "if (rc != 0)";
9490 s.op->newline(1) << "unregister_kprobe (& kp->dummy);";
9491 s.op->newline(-2) << "}";
9492 s.op->newline() << "#else";
9493 s.op->newline() << "rc = register_kretprobe (& kp->u.krp);";
9494 s.op->newline() << "#endif";
9495 s.op->newline(-1) << "} else {";
9496 // to ensure safeness of bspcache, always use aggr_kprobe on ia64
c8d9d15e 9497 s.op->newline(1) << "kp->u.kp.addr = addr;";
766cee5f 9498 s.op->newline() << "#ifdef STAPCONF_KPROBE_SYMBOL_NAME";
03a4ec63 9499 s.op->newline() << "kp->u.kp.symbol_name = (char *) symbol_name;";
766cee5f 9500 s.op->newline() << "#endif";
88747011 9501 s.op->newline() << "kp->u.kp.pre_handler = &enter_kprobe2_probe;";
e6fe60e7 9502 s.op->newline() << "#ifdef __ia64__";
e6fe60e7 9503 s.op->newline() << "kp->dummy.pre_handler = NULL;";
c8d9d15e 9504 s.op->newline() << "kp->dummy.addr = kp->u.kp.addr;";
766cee5f 9505 s.op->newline() << "#ifdef STAPCONF_KPROBE_SYMBOL_NAME";
c8d9d15e 9506 s.op->newline() << "kp->dummy.symbol_name = kp->u.kp.symbol_name;";
766cee5f 9507 s.op->newline() << "#endif";
e6fe60e7
AM
9508 s.op->newline() << "rc = register_kprobe (& kp->dummy);";
9509 s.op->newline() << "if (rc == 0) {";
9510 s.op->newline(1) << "rc = register_kprobe (& kp->u.kp);";
9511 s.op->newline() << "if (rc != 0)";
9512 s.op->newline(1) << "unregister_kprobe (& kp->dummy);";
9513 s.op->newline(-2) << "}";
9514 s.op->newline() << "#else";
9515 s.op->newline() << "rc = register_kprobe (& kp->u.kp);";
9516 s.op->newline() << "#endif";
9517 s.op->newline(-1) << "}";
9518 s.op->newline() << "if (rc) {"; // PR6749: tolerate a failed register_*probe.
9519 s.op->newline(1) << "sdp->registered_p = 0;";
b350f56b 9520 s.op->newline() << "if (!sdp->optional_p)";
50b6acf7 9521 s.op->newline(1) << "_stp_warn (\"probe %s (address 0x%lx) registration error (rc %d)\", probe_point, (unsigned long) addr, rc);";
b350f56b 9522 s.op->newline(-1) << "rc = 0;"; // continue with other probes
e6fe60e7
AM
9523 // XXX: shall we increment numskipped?
9524 s.op->newline(-1) << "}";
6270adc1 9525
e6fe60e7
AM
9526 s.op->newline() << "else sdp->registered_p = 1;";
9527 s.op->newline(-1) << "}"; // for loop
6270adc1
MH
9528}
9529
b4be7cbc 9530
e6fe60e7
AM
9531void
9532kprobe_derived_probe_group::emit_module_exit (systemtap_session& s)
935447c8 9533{
e6fe60e7
AM
9534 //Unregister kprobes by batch interfaces.
9535 s.op->newline() << "#if defined(STAPCONF_UNREGISTER_KPROBES)";
9536 s.op->newline() << "j = 0;";
9537 s.op->newline() << "for (i=0; i<" << probes_by_module.size() << "; i++) {";
9538 s.op->newline(1) << "struct stap_dwarfless_probe *sdp = & stap_dwarfless_probes[i];";
9539 s.op->newline() << "struct stap_dwarfless_kprobe *kp = & stap_dwarfless_kprobes[i];";
9540 s.op->newline() << "if (! sdp->registered_p) continue;";
9541 s.op->newline() << "if (!sdp->return_p)";
c9116e99 9542 s.op->newline(1) << "stap_unreg_kprobes2[j++] = &kp->u.kp;";
e6fe60e7 9543 s.op->newline(-2) << "}";
c9116e99 9544 s.op->newline() << "unregister_kprobes((struct kprobe **)stap_unreg_kprobes2, j);";
e6fe60e7
AM
9545 s.op->newline() << "j = 0;";
9546 s.op->newline() << "for (i=0; i<" << probes_by_module.size() << "; i++) {";
9547 s.op->newline(1) << "struct stap_dwarfless_probe *sdp = & stap_dwarfless_probes[i];";
9548 s.op->newline() << "struct stap_dwarfless_kprobe *kp = & stap_dwarfless_kprobes[i];";
9549 s.op->newline() << "if (! sdp->registered_p) continue;";
9550 s.op->newline() << "if (sdp->return_p)";
c9116e99 9551 s.op->newline(1) << "stap_unreg_kprobes2[j++] = &kp->u.krp;";
e6fe60e7 9552 s.op->newline(-2) << "}";
c9116e99 9553 s.op->newline() << "unregister_kretprobes((struct kretprobe **)stap_unreg_kprobes2, j);";
e6fe60e7
AM
9554 s.op->newline() << "#ifdef __ia64__";
9555 s.op->newline() << "j = 0;";
9556 s.op->newline() << "for (i=0; i<" << probes_by_module.size() << "; i++) {";
9557 s.op->newline(1) << "struct stap_dwarfless_probe *sdp = & stap_dwarfless_probes[i];";
9558 s.op->newline() << "struct stap_dwarfless_kprobe *kp = & stap_dwarfless_kprobes[i];";
9559 s.op->newline() << "if (! sdp->registered_p) continue;";
c9116e99 9560 s.op->newline() << "stap_unreg_kprobes2[j++] = &kp->dummy;";
e6fe60e7 9561 s.op->newline(-1) << "}";
c9116e99 9562 s.op->newline() << "unregister_kprobes((struct kprobe **)stap_unreg_kprobes2, j);";
e6fe60e7
AM
9563 s.op->newline() << "#endif";
9564 s.op->newline() << "#endif";
3e3bd7b6 9565
e6fe60e7
AM
9566 s.op->newline() << "for (i=0; i<" << probes_by_module.size() << "; i++) {";
9567 s.op->newline(1) << "struct stap_dwarfless_probe *sdp = & stap_dwarfless_probes[i];";
9568 s.op->newline() << "struct stap_dwarfless_kprobe *kp = & stap_dwarfless_kprobes[i];";
9569 s.op->newline() << "if (! sdp->registered_p) continue;";
9570 s.op->newline() << "if (sdp->return_p) {";
9571 s.op->newline() << "#if !defined(STAPCONF_UNREGISTER_KPROBES)";
9572 s.op->newline(1) << "unregister_kretprobe (&kp->u.krp);";
9573 s.op->newline() << "#endif";
065d5567 9574 s.op->newline() << "atomic_add (kp->u.krp.nmissed, skipped_count());";
e6fe60e7
AM
9575 s.op->newline() << "#ifdef STP_TIMING";
9576 s.op->newline() << "if (kp->u.krp.nmissed)";
26e63673 9577 s.op->newline(1) << "_stp_warn (\"Skipped due to missed kretprobe/1 on '%s': %d\\n\", sdp->probe->pp, kp->u.krp.nmissed);";
e6fe60e7 9578 s.op->newline(-1) << "#endif";
065d5567 9579 s.op->newline() << "atomic_add (kp->u.krp.kp.nmissed, skipped_count());";
e6fe60e7
AM
9580 s.op->newline() << "#ifdef STP_TIMING";
9581 s.op->newline() << "if (kp->u.krp.kp.nmissed)";
26e63673 9582 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
9583 s.op->newline(-1) << "#endif";
9584 s.op->newline(-1) << "} else {";
9585 s.op->newline() << "#if !defined(STAPCONF_UNREGISTER_KPROBES)";
9586 s.op->newline(1) << "unregister_kprobe (&kp->u.kp);";
9587 s.op->newline() << "#endif";
065d5567 9588 s.op->newline() << "atomic_add (kp->u.kp.nmissed, skipped_count());";
e6fe60e7
AM
9589 s.op->newline() << "#ifdef STP_TIMING";
9590 s.op->newline() << "if (kp->u.kp.nmissed)";
26e63673 9591 s.op->newline(1) << "_stp_warn (\"Skipped due to missed kprobe on '%s': %lu\\n\", sdp->probe->pp, kp->u.kp.nmissed);";
e6fe60e7
AM
9592 s.op->newline(-1) << "#endif";
9593 s.op->newline(-1) << "}";
9594 s.op->newline() << "#if !defined(STAPCONF_UNREGISTER_KPROBES) && defined(__ia64__)";
9595 s.op->newline() << "unregister_kprobe (&kp->dummy);";
9596 s.op->newline() << "#endif";
9597 s.op->newline() << "sdp->registered_p = 0;";
9598 s.op->newline(-1) << "}";
f8a968bc
JS
9599}
9600
e6fe60e7 9601struct kprobe_builder: public derived_probe_builder
3c1b3d06 9602{
9fdf787d 9603public:
2a639817 9604 kprobe_builder() {}
9fdf787d 9605
2a639817 9606 void build_no_more (systemtap_session &s) {}
9fdf787d 9607
e6fe60e7
AM
9608 virtual void build(systemtap_session & sess,
9609 probe * base,
9610 probe_point * location,
9611 literal_map_t const & parameters,
9612 vector<derived_probe *> & finished_results);
9613};
3c1b3d06
FCE
9614
9615
79189b84 9616void
05fb3e0c 9617kprobe_builder::build(systemtap_session & sess,
e6fe60e7
AM
9618 probe * base,
9619 probe_point * location,
9620 literal_map_t const & parameters,
9621 vector<derived_probe *> & finished_results)
79189b84 9622{
e6fe60e7 9623 string function_string_val, module_string_val;
05fb3e0c 9624 string path, library, path_tgt, library_tgt;
b6371390
JS
9625 int64_t statement_num_val = 0, maxactive_val = 0;
9626 bool has_function_str, has_module_str, has_statement_num;
e6afb07b 9627 bool has_absolute, has_call, has_return, has_maxactive;
b642c901 9628 bool has_path, has_library;
79189b84 9629
b6371390
JS
9630 has_function_str = get_param(parameters, TOK_FUNCTION, function_string_val);
9631 has_module_str = get_param(parameters, TOK_MODULE, module_string_val);
e6afb07b 9632 has_call = has_null_param (parameters, TOK_CALL);
b6371390
JS
9633 has_return = has_null_param (parameters, TOK_RETURN);
9634 has_maxactive = get_param(parameters, TOK_MAXACTIVE, maxactive_val);
9635 has_statement_num = get_param(parameters, TOK_STATEMENT, statement_num_val);
9636 has_absolute = has_null_param (parameters, TOK_ABSOLUTE);
b642c901
SC
9637 has_path = get_param (parameters, TOK_PROCESS, path);
9638 has_library = get_param (parameters, TOK_LIBRARY, library);
9639
9640 if (has_path)
05fb3e0c
WF
9641 {
9642 path = find_executable (path, sess.sysroot, sess.sysenv);
9643 path_tgt = path_remove_sysroot(sess, path);
9644 }
b642c901 9645 if (has_library)
05fb3e0c
WF
9646 {
9647 library = find_executable (library, sess.sysroot, sess.sysenv,
9648 "LD_LIBRARY_PATH");
9649 library_tgt = path_remove_sysroot(sess, library);
9650 }
c57ea854 9651
b6371390 9652 if (has_function_str)
6fb70fb7 9653 {
2a639817 9654 if (has_module_str)
9fdf787d
DS
9655 {
9656 function_string_val = module_string_val + ":" + function_string_val;
9657 derived_probe *dp
23dc94f6
DS
9658 = new kprobe_derived_probe (sess, finished_results, base,
9659 location, function_string_val,
e6afb07b
JL
9660 0, has_call, has_return,
9661 has_statement_num, has_maxactive,
9662 has_path, has_library, maxactive_val,
9663 path_tgt, library_tgt);
9fdf787d
DS
9664 finished_results.push_back (dp);
9665 }
9666 else
9667 {
2a639817
JS
9668 vector<string> matches;
9669
9670 // Simple names can be found directly
9671 if (function_string_val.find_first_of("*?[") == string::npos)
9672 {
9673 if (sess.kernel_functions.count(function_string_val))
9674 matches.push_back(function_string_val);
9675 }
9676 else // Search function name list for matching names
9677 {
9678 for (set<string>::const_iterator it = sess.kernel_functions.begin();
9679 it != sess.kernel_functions.end(); it++)
9680 // fnmatch returns zero for matching.
9681 if (fnmatch(function_string_val.c_str(), it->c_str(), 0) == 0)
9682 matches.push_back(*it);
9683 }
86758d5f 9684
2a639817
JS
9685 for (vector<string>::const_iterator it = matches.begin();
9686 it != matches.end(); it++)
9fdf787d 9687 {
2a639817
JS
9688 derived_probe *dp
9689 = new kprobe_derived_probe (sess, finished_results, base,
e6afb07b
JL
9690 location, *it, 0, has_call,
9691 has_return, has_statement_num,
2a639817
JS
9692 has_maxactive, has_path,
9693 has_library, maxactive_val,
9694 path_tgt, library_tgt);
9695 finished_results.push_back (dp);
9fdf787d
DS
9696 }
9697 }
6fb70fb7 9698 }
e6fe60e7 9699 else
b6371390
JS
9700 {
9701 // assert guru mode for absolute probes
9702 if ( has_statement_num && has_absolute && !base->privileged )
dc09353a 9703 throw SEMANTIC_ERROR (_("absolute statement probe in unprivileged script; need stap -g"), base->tok);
b6371390 9704
23dc94f6
DS
9705 finished_results.push_back (new kprobe_derived_probe (sess,
9706 finished_results,
9707 base,
b6371390
JS
9708 location, "",
9709 statement_num_val,
e6afb07b 9710 has_call,
b6371390
JS
9711 has_return,
9712 has_statement_num,
9713 has_maxactive,
b642c901
SC
9714 has_path,
9715 has_library,
9716 maxactive_val,
05fb3e0c
WF
9717 path_tgt,
9718 library_tgt));
96b030fe 9719 }
79189b84
JS
9720}
9721
23dc94f6
DS
9722
9723void
9724kprobe_var_expanding_visitor::visit_entry_op (entry_op *e)
9725{
9726 expression *repl = e;
9727
bd5b25e1
JS
9728 if (has_return)
9729 {
9730 // expand the operand as if it weren't a return probe
9731 has_return = false;
9732 replace (e->operand);
9733 has_return = true;
23dc94f6 9734
bd5b25e1
JS
9735 // XXX it would be nice to use gen_kretprobe_saved_return when
9736 // available, but it requires knowing the types already, which is
9737 // problematic for arbitrary expressons.
9738 repl = gen_mapped_saved_return (sess, e->operand, "entry",
9739 add_block, add_block_tid,
9740 add_call_probe, add_call_probe_tid);
9741 }
23dc94f6
DS
9742 provide (repl);
9743}
9744
9745
dd225250
PS
9746// ------------------------------------------------------------------------
9747// Hardware breakpoint based probes.
9748// ------------------------------------------------------------------------
9749
9750static const string TOK_HWBKPT("data");
9751static const string TOK_HWBKPT_WRITE("write");
9752static const string TOK_HWBKPT_RW("rw");
9753static const string TOK_LENGTH("length");
9754
9755#define HWBKPT_READ 0
9756#define HWBKPT_WRITE 1
9757#define HWBKPT_RW 2
9758struct hwbkpt_derived_probe: public derived_probe
9759{
9760 hwbkpt_derived_probe (probe *base,
9761 probe_point *location,
9762 uint64_t addr,
9763 string symname,
9764 unsigned int len,
9765 bool has_only_read_access,
9766 bool has_only_write_access,
9767 bool has_rw_access
9768 );
9769 Dwarf_Addr hwbkpt_addr;
9770 string symbol_name;
9771 unsigned int hwbkpt_access,hwbkpt_len;
9772
9773 void printsig (std::ostream &o) const;
9774 void join_group (systemtap_session& s);
9775};
9776
9777struct hwbkpt_derived_probe_group: public derived_probe_group
9778{
dd225250 9779private:
dac77b80 9780 vector<hwbkpt_derived_probe*> hwbkpt_probes;
dd225250
PS
9781
9782public:
9783 void enroll (hwbkpt_derived_probe* probe, systemtap_session& s);
9784 void emit_module_decls (systemtap_session& s);
9785 void emit_module_init (systemtap_session& s);
9786 void emit_module_exit (systemtap_session& s);
9787};
9788
9789hwbkpt_derived_probe::hwbkpt_derived_probe (probe *base,
9ea68eb9
JS
9790 probe_point *location,
9791 uint64_t addr,
9792 string symname,
9793 unsigned int len,
9794 bool has_only_read_access,
9795 bool has_only_write_access,
822a6a3d 9796 bool):
4c5d1300 9797 derived_probe (base, location, true /* .components soon rewritten */ ),
dd225250
PS
9798 hwbkpt_addr (addr),
9799 symbol_name (symname),
9800 hwbkpt_len (len)
9801{
9802 this->tok = base->tok;
9803
9804 vector<probe_point::component*> comps;
9805 comps.push_back (new probe_point::component(TOK_KERNEL));
9806
9807 if (hwbkpt_addr)
9ea68eb9
JS
9808 comps.push_back (new probe_point::component (TOK_HWBKPT,
9809 new literal_number(hwbkpt_addr, true)));
9810 else if (symbol_name.size())
9811 comps.push_back (new probe_point::component (TOK_HWBKPT, new literal_string(symbol_name)));
dd225250
PS
9812
9813 comps.push_back (new probe_point::component (TOK_LENGTH, new literal_number(hwbkpt_len)));
9814
9815 if (has_only_read_access)
9ea68eb9 9816 this->hwbkpt_access = HWBKPT_READ ;
dd225250
PS
9817//TODO add code for comps.push_back for read, since this flag is not for x86
9818
9819 else
9ea68eb9
JS
9820 {
9821 if (has_only_write_access)
9822 {
9823 this->hwbkpt_access = HWBKPT_WRITE ;
9824 comps.push_back (new probe_point::component(TOK_HWBKPT_WRITE));
9825 }
9826 else
9827 {
9828 this->hwbkpt_access = HWBKPT_RW ;
9829 comps.push_back (new probe_point::component(TOK_HWBKPT_RW));
9830 }
9831 }
dd225250
PS
9832
9833 this->sole_location()->components = comps;
9834}
9835
9836void hwbkpt_derived_probe::printsig (ostream& o) const
9837{
9838 sole_location()->print (o);
9839 printsig_nested (o);
9840}
9841
9842void hwbkpt_derived_probe::join_group (systemtap_session& s)
9843{
dac77b80
FCE
9844 if (! s.hwbkpt_derived_probes)
9845 s.hwbkpt_derived_probes = new hwbkpt_derived_probe_group ();
dd225250 9846 s.hwbkpt_derived_probes->enroll (this, s);
ca6d3b0f 9847 this->group = s.hwbkpt_derived_probes;
dd225250
PS
9848}
9849
9850void hwbkpt_derived_probe_group::enroll (hwbkpt_derived_probe* p, systemtap_session& s)
9851{
dac77b80
FCE
9852 hwbkpt_probes.push_back (p);
9853
9854 unsigned max_hwbkpt_probes_by_arch = 0;
9855 if (s.architecture == "i386" || s.architecture == "x86_64")
9856 max_hwbkpt_probes_by_arch = 4;
9857 else if (s.architecture == "s390")
9858 max_hwbkpt_probes_by_arch = 1;
9859
c57ea854 9860 if (hwbkpt_probes.size() >= max_hwbkpt_probes_by_arch)
2713ea24 9861 s.print_warning (_F("Too many hardware breakpoint probes requested for %s (%zu vs. %u)",
b530b5b3 9862 s.architecture.c_str(), hwbkpt_probes.size(), max_hwbkpt_probes_by_arch));
dd225250
PS
9863}
9864
9865void
9866hwbkpt_derived_probe_group::emit_module_decls (systemtap_session& s)
9867{
dac77b80 9868 if (hwbkpt_probes.empty()) return;
dd225250
PS
9869
9870 s.op->newline() << "/* ---- hwbkpt-based probes ---- */";
9871
9872 s.op->newline() << "#include <linux/perf_event.h>";
9873 s.op->newline() << "#include <linux/hw_breakpoint.h>";
9874 s.op->newline();
9875
9876 // Forward declare the master entry functions
23063de1 9877 s.op->newline() << "#ifdef STAPCONF_PERF_HANDLER_NMI";
dd225250
PS
9878 s.op->newline() << "static int enter_hwbkpt_probe (struct perf_event *bp,";
9879 s.op->line() << " int nmi,";
9880 s.op->line() << " struct perf_sample_data *data,";
9881 s.op->line() << " struct pt_regs *regs);";
23063de1
FCE
9882 s.op->newline() << "#else";
9883 s.op->newline() << "static int enter_hwbkpt_probe (struct perf_event *bp,";
9884 s.op->line() << " struct perf_sample_data *data,";
9885 s.op->line() << " struct pt_regs *regs);";
9886 s.op->newline() << "#endif";
79189b84 9887
dd225250
PS
9888 // Emit the actual probe list.
9889
9890 s.op->newline() << "static struct perf_event_attr ";
dac77b80 9891 s.op->newline() << "stap_hwbkpt_probe_array[" << hwbkpt_probes.size() << "];";
dd225250
PS
9892
9893 s.op->newline() << "static struct perf_event **";
dac77b80 9894 s.op->newline() << "stap_hwbkpt_ret_array[" << hwbkpt_probes.size() << "];";
dd225250
PS
9895 s.op->newline() << "static struct stap_hwbkpt_probe {";
9896 s.op->newline() << "int registered_p:1;";
43650b10 9897// registered_p = 0 signifies a probe that is unregistered (or failed)
dd225250
PS
9898// registered_p = 1 signifies a probe that got registered successfully
9899
faea5e16 9900 // Symbol Names are mostly small and uniform enough
dd225250 9901 // to justify putting const char*.
dac77b80 9902 s.op->newline() << "const char * const symbol;";
dd225250
PS
9903
9904 s.op->newline() << "const unsigned long address;";
9905 s.op->newline() << "uint8_t atype;";
bb0a4e12 9906 s.op->newline() << "unsigned int len;";
7c3e97f4 9907 s.op->newline() << "const struct stap_probe * const probe;";
dd225250
PS
9908 s.op->newline() << "} stap_hwbkpt_probes[] = {";
9909 s.op->indent(1);
9910
dac77b80 9911 for (unsigned int it = 0; it < hwbkpt_probes.size(); it++)
dd225250 9912 {
dac77b80 9913 hwbkpt_derived_probe* p = hwbkpt_probes.at(it);
dd225250 9914 s.op->newline() << "{";
dd225250
PS
9915 if (p->symbol_name.size())
9916 s.op->line() << " .address=(unsigned long)0x0" << "ULL,";
9917 else
9918 s.op->line() << " .address=(unsigned long)0x" << hex << p->hwbkpt_addr << dec << "ULL,";
9919 switch(p->hwbkpt_access){
9920 case HWBKPT_READ:
9921 s.op->line() << " .atype=HW_BREAKPOINT_R ,";
bb0a4e12 9922 break;
dd225250
PS
9923 case HWBKPT_WRITE:
9924 s.op->line() << " .atype=HW_BREAKPOINT_W ,";
bb0a4e12 9925 break;
dd225250
PS
9926 case HWBKPT_RW:
9927 s.op->line() << " .atype=HW_BREAKPOINT_R|HW_BREAKPOINT_W ,";
bb0a4e12 9928 break;
dd225250
PS
9929 };
9930 s.op->line() << " .len=" << p->hwbkpt_len << ",";
faea5e16 9931 s.op->line() << " .probe=" << common_probe_init (p) << ",";
dd225250 9932 s.op->line() << " .symbol=\"" << p->symbol_name << "\",";
dd225250
PS
9933 s.op->line() << " },";
9934 }
dac77b80 9935 s.op->newline(-1) << "};";
dd225250
PS
9936
9937 // Emit the hwbkpt callback function
9938 s.op->newline() ;
23063de1 9939 s.op->newline() << "#ifdef STAPCONF_PERF_HANDLER_NMI";
dd225250
PS
9940 s.op->newline() << "static int enter_hwbkpt_probe (struct perf_event *bp,";
9941 s.op->line() << " int nmi,";
9942 s.op->line() << " struct perf_sample_data *data,";
9943 s.op->line() << " struct pt_regs *regs) {";
23063de1
FCE
9944 s.op->newline() << "#else";
9945 s.op->newline() << "static int enter_hwbkpt_probe (struct perf_event *bp,";
9946 s.op->line() << " struct perf_sample_data *data,";
9947 s.op->line() << " struct pt_regs *regs) {";
9948 s.op->newline() << "#endif";
dac77b80
FCE
9949 s.op->newline(1) << "unsigned int i;";
9950 s.op->newline() << "if (bp->attr.type != PERF_TYPE_BREAKPOINT) return -1;";
9951 s.op->newline() << "for (i=0; i<" << hwbkpt_probes.size() << "; i++) {";
9952 s.op->newline(1) << "struct perf_event_attr *hp = & stap_hwbkpt_probe_array[i];";
9953 // XXX: why not match stap_hwbkpt_ret_array[i] against bp instead?
9954 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) {";
9955 s.op->newline(1) << "struct stap_hwbkpt_probe *sdp = &stap_hwbkpt_probes[i];";
71db462b 9956 common_probe_entryfn_prologue (s, "STAP_SESSION_RUNNING", "sdp->probe",
cda141c2 9957 "stp_probe_type_hwbkpt");
d9aed31e 9958 s.op->newline() << "if (user_mode(regs)) {";
e04b5d74 9959 s.op->newline(1)<< "c->user_mode_p = 1;";
d9aed31e
MW
9960 s.op->newline() << "c->uregs = regs;";
9961 s.op->newline(-1) << "} else {";
9962 s.op->newline(1) << "c->kregs = regs;";
9963 s.op->newline(-1) << "}";
26e63673 9964 s.op->newline() << "(*sdp->probe->ph) (c);";
ef1337ee 9965 common_probe_entryfn_epilogue (s, true, otf_safe_context(s));
dac77b80 9966 s.op->newline(-1) << "}";
dd225250
PS
9967 s.op->newline(-1) << "}";
9968 s.op->newline() << "return 0;";
dac77b80 9969 s.op->newline(-1) << "}";
dd225250
PS
9970}
9971
9972void
9973hwbkpt_derived_probe_group::emit_module_init (systemtap_session& s)
9974{
dac77b80 9975 s.op->newline() << "for (i=0; i<" << hwbkpt_probes.size() << "; i++) {";
dd225250
PS
9976 s.op->newline(1) << "struct stap_hwbkpt_probe *sdp = & stap_hwbkpt_probes[i];";
9977 s.op->newline() << "struct perf_event_attr *hp = & stap_hwbkpt_probe_array[i];";
9978 s.op->newline() << "void *addr = (void *) sdp->address;";
9979 s.op->newline() << "const char *hwbkpt_symbol_name = addr ? NULL : sdp->symbol;";
dac77b80
FCE
9980 s.op->newline() << "hw_breakpoint_init(hp);";
9981 s.op->newline() << "if (addr)";
9982 s.op->newline(1) << "hp->bp_addr = (unsigned long) addr;";
9983 s.op->newline(-1) << "else { ";
9984 s.op->newline(1) << "hp->bp_addr = kallsyms_lookup_name(hwbkpt_symbol_name);";
9985 s.op->newline() << "if (!hp->bp_addr) { ";
26e63673 9986 s.op->newline(1) << "_stp_warn(\"Probe %s registration skipped: invalid symbol %s \",sdp->probe->pp,hwbkpt_symbol_name);";
dac77b80
FCE
9987 s.op->newline() << "continue;";
9988 s.op->newline(-1) << "}";
9989 s.op->newline(-1) << "}";
9990 s.op->newline() << "hp->bp_type = sdp->atype;";
9991
9992 // On x86 & x86-64, hp->bp_len is not just a number but a macro/enum (!?!).
c57ea854 9993 if (s.architecture == "i386" || s.architecture == "x86_64" )
dac77b80
FCE
9994 {
9995 s.op->newline() << "switch(sdp->len) {";
9996 s.op->newline() << "case 1:";
9997 s.op->newline(1) << "hp->bp_len = HW_BREAKPOINT_LEN_1;";
9998 s.op->newline() << "break;";
9999 s.op->newline(-1) << "case 2:";
10000 s.op->newline(1) << "hp->bp_len = HW_BREAKPOINT_LEN_2;";
10001 s.op->newline() << "break;";
10002 s.op->newline(-1) << "case 3:";
10003 s.op->newline() << "case 4:";
10004 s.op->newline(1) << "hp->bp_len = HW_BREAKPOINT_LEN_4;";
10005 s.op->newline() << "break;";
10006 s.op->newline(-1) << "case 5:";
10007 s.op->newline() << "case 6:";
10008 s.op->newline() << "case 7:";
10009 s.op->newline() << "case 8:";
10010 s.op->newline() << "default:"; // XXX: could instead reject
10011 s.op->newline(1) << "hp->bp_len = HW_BREAKPOINT_LEN_8;";
10012 s.op->newline() << "break;";
10013 s.op->newline(-1) << "}";
10014 }
10015 else // other architectures presumed straightforward
10016 s.op->newline() << "hp->bp_len = sdp->len;";
10017
26e63673 10018 s.op->newline() << "probe_point = sdp->probe->pp;"; // for error messages
b273669e
MW
10019 s.op->newline() << "#ifdef STAPCONF_HW_BREAKPOINT_CONTEXT";
10020 s.op->newline() << "stap_hwbkpt_ret_array[i] = register_wide_hw_breakpoint(hp, (void *)&enter_hwbkpt_probe, NULL);";
10021 s.op->newline() << "#else";
dac77b80 10022 s.op->newline() << "stap_hwbkpt_ret_array[i] = register_wide_hw_breakpoint(hp, (void *)&enter_hwbkpt_probe);";
b273669e 10023 s.op->newline() << "#endif";
43650b10 10024 s.op->newline() << "rc = 0;";
dac77b80 10025 s.op->newline() << "if (IS_ERR(stap_hwbkpt_ret_array[i])) {";
43650b10
WC
10026 s.op->newline(1) << "rc = PTR_ERR(stap_hwbkpt_ret_array[i]);";
10027 s.op->newline() << "stap_hwbkpt_ret_array[i] = 0;";
10028 s.op->newline(-1) << "}";
217ef1f4
WC
10029 s.op->newline() << "if (rc) {";
10030 s.op->newline(1) << "_stp_warn(\"Hwbkpt probe %s: registration error %d, addr %p, name %s\", probe_point, rc, addr, hwbkpt_symbol_name);";
43650b10 10031 s.op->newline() << "sdp->registered_p = 0;";
dac77b80 10032 s.op->newline(-1) << "}";
dd225250 10033 s.op->newline() << " else sdp->registered_p = 1;";
dd225250
PS
10034 s.op->newline(-1) << "}"; // for loop
10035}
10036
10037void
10038hwbkpt_derived_probe_group::emit_module_exit (systemtap_session& s)
10039{
10040 //Unregister hwbkpt probes.
dac77b80 10041 s.op->newline() << "for (i=0; i<" << hwbkpt_probes.size() << "; i++) {";
dd225250 10042 s.op->newline(1) << "struct stap_hwbkpt_probe *sdp = & stap_hwbkpt_probes[i];";
dac77b80
FCE
10043 s.op->newline() << "if (sdp->registered_p == 0) continue;";
10044 s.op->newline() << "unregister_wide_hw_breakpoint(stap_hwbkpt_ret_array[i]);";
dd225250
PS
10045 s.op->newline() << "sdp->registered_p = 0;";
10046 s.op->newline(-1) << "}";
10047}
10048
10049struct hwbkpt_builder: public derived_probe_builder
10050{
10051 hwbkpt_builder() {}
10052 virtual void build(systemtap_session & sess,
10053 probe * base,
10054 probe_point * location,
10055 literal_map_t const & parameters,
10056 vector<derived_probe *> & finished_results);
10057};
10058
10059void
10060hwbkpt_builder::build(systemtap_session & sess,
10061 probe * base,
10062 probe_point * location,
10063 literal_map_t const & parameters,
10064 vector<derived_probe *> & finished_results)
10065{
10066 string symbol_str_val;
10067 int64_t hwbkpt_address, len;
10068 bool has_addr, has_symbol_str, has_write, has_rw, has_len;
10069
b47f3a55 10070 if (! (sess.kernel_config["CONFIG_PERF_EVENTS"] == string("y")))
dc09353a 10071 throw SEMANTIC_ERROR (_("CONFIG_PERF_EVENTS not available on this kernel"),
b47f3a55
FCE
10072 location->components[0]->tok);
10073 if (! (sess.kernel_config["CONFIG_HAVE_HW_BREAKPOINT"] == string("y")))
dc09353a 10074 throw SEMANTIC_ERROR (_("CONFIG_HAVE_HW_BREAKPOINT not available on this kernel"),
b47f3a55
FCE
10075 location->components[0]->tok);
10076
dd225250
PS
10077 has_addr = get_param (parameters, TOK_HWBKPT, hwbkpt_address);
10078 has_symbol_str = get_param (parameters, TOK_HWBKPT, symbol_str_val);
10079 has_len = get_param (parameters, TOK_LENGTH, len);
10080 has_write = (parameters.find(TOK_HWBKPT_WRITE) != parameters.end());
10081 has_rw = (parameters.find(TOK_HWBKPT_RW) != parameters.end());
10082
b31695eb
JL
10083 // Make an intermediate pp that is well-formed. It's pretty much the same as
10084 // the user-provided one, except that the addr literal is well-typed.
10085 probe_point* well_formed_loc = new probe_point(*location);
10086 well_formed_loc->well_formed = true;
10087
10088 vector<probe_point::component*> well_formed_comps;
10089 vector<probe_point::component*>::iterator it;
10090 for (it = location->components.begin();
10091 it != location->components.end(); ++it)
10092 if ((*it)->functor == TOK_HWBKPT && has_addr)
10093 well_formed_comps.push_back(new probe_point::component(TOK_HWBKPT,
10094 new literal_number(hwbkpt_address, true /* hex */ )));
10095 else
10096 well_formed_comps.push_back(*it);
10097 well_formed_loc->components = well_formed_comps;
10098 probe *new_base = new probe (base, well_formed_loc);
10099
dd225250
PS
10100 if (!has_len)
10101 len = 1;
10102
10103 if (has_addr)
b31695eb 10104 finished_results.push_back (new hwbkpt_derived_probe (new_base,
dd225250
PS
10105 location,
10106 hwbkpt_address,
10107 "",len,0,
10108 has_write,
10109 has_rw));
5d8a0aea 10110 else if (has_symbol_str)
b31695eb 10111 finished_results.push_back (new hwbkpt_derived_probe (new_base,
dd225250
PS
10112 location,
10113 0,
10114 symbol_str_val,len,0,
10115 has_write,
10116 has_rw));
5d8a0aea
FCE
10117 else
10118 assert (0);
dd225250 10119}
342d3f96 10120
0a6f5a3f
JS
10121// ------------------------------------------------------------------------
10122// statically inserted kernel-tracepoint derived probes
10123// ------------------------------------------------------------------------
10124
6fb70fb7 10125struct tracepoint_arg
79189b84 10126{
ad370dcc 10127 string name, c_type, typecast;
dcaa1a65 10128 bool usable, used, isptr;
f8a968bc 10129 Dwarf_Die type_die;
dcaa1a65 10130 tracepoint_arg(): usable(false), used(false), isptr(false) {}
6fb70fb7 10131};
79189b84 10132
0a6f5a3f
JS
10133struct tracepoint_derived_probe: public derived_probe
10134{
79189b84
JS
10135 tracepoint_derived_probe (systemtap_session& s,
10136 dwflpp& dw, Dwarf_Die& func_die,
968b8188 10137 const string& tracepoint_system,
79189b84
JS
10138 const string& tracepoint_name,
10139 probe* base_probe, probe_point* location);
bc9a523d 10140
79189b84 10141 systemtap_session& sess;
4f58db62 10142 string tracepoint_system, tracepoint_name, header;
6fb70fb7 10143 vector <struct tracepoint_arg> args;
bc9a523d 10144
6fb70fb7 10145 void build_args(dwflpp& dw, Dwarf_Die& func_die);
d0bfd2ac 10146 void getargs (std::list<std::string> &arg_set) const;
79189b84 10147 void join_group (systemtap_session& s);
3e3bd7b6 10148 void print_dupe_stamp(ostream& o);
0a6f5a3f 10149};
79189b84
JS
10150
10151
0a6f5a3f 10152struct tracepoint_derived_probe_group: public generic_dpg<tracepoint_derived_probe>
79189b84 10153{
79189b84
JS
10154 void emit_module_decls (systemtap_session& s);
10155 void emit_module_init (systemtap_session& s);
10156 void emit_module_exit (systemtap_session& s);
0a6f5a3f 10157};
79189b84 10158
bc9a523d 10159
f8a968bc
JS
10160struct tracepoint_var_expanding_visitor: public var_expanding_visitor
10161{
10162 tracepoint_var_expanding_visitor(dwflpp& dw, const string& probe_name,
10163 vector <struct tracepoint_arg>& args):
10164 dw (dw), probe_name (probe_name), args (args) {}
10165 dwflpp& dw;
10166 const string& probe_name;
10167 vector <struct tracepoint_arg>& args;
bc9a523d 10168
f8a968bc
JS
10169 void visit_target_symbol (target_symbol* e);
10170 void visit_target_symbol_arg (target_symbol* e);
10171 void visit_target_symbol_context (target_symbol* e);
10172};
79189b84
JS
10173
10174
f8a968bc
JS
10175void
10176tracepoint_var_expanding_visitor::visit_target_symbol_arg (target_symbol* e)
75ead1f7 10177{
cc9001af 10178 string argname = e->sym_name();
75ead1f7 10179
f8a968bc
JS
10180 // search for a tracepoint parameter matching this name
10181 tracepoint_arg *arg = NULL;
10182 for (unsigned i = 0; i < args.size(); ++i)
dcaa1a65 10183 if (args[i].usable && args[i].name == argname)
f8a968bc
JS
10184 {
10185 arg = &args[i];
10186 arg->used = true;
10187 break;
10188 }
75ead1f7 10189
f8a968bc
JS
10190 if (arg == NULL)
10191 {
1d0499c2 10192 set<string> vars;
f8a968bc 10193 for (unsigned i = 0; i < args.size(); ++i)
1d0499c2
JL
10194 vars.insert("$" + args[i].name);
10195 vars.insert("$$name");
10196 vars.insert("$$parms");
10197 vars.insert("$$vars");
10198 string sugs = levenshtein_suggest(e->name, vars); // no need to limit, there's not that many
75ead1f7 10199
f8a968bc
JS
10200 // We hope that this value ends up not being referenced after all, so it
10201 // can be optimized out quietly.
1d0499c2
JL
10202 throw SEMANTIC_ERROR(_F("unable to find tracepoint variable '%s'%s",
10203 e->name.c_str(), sugs.empty() ? "" :
10204 (_(" (alternatives: ") + sugs + ")").c_str()), e->tok);
10205 // NB: we use 'alternatives' because we list all
f8a968bc
JS
10206 // NB: we can have multiple errors, since a target variable
10207 // may be expanded in several different contexts:
10208 // trace ("*") { $foo->bar }
f8a968bc 10209 }
75ead1f7 10210
37efef73
JS
10211 // make sure we're not dereferencing base types or void
10212 bool deref_p = arg->isptr && !null_die(&arg->type_die);
10213 if (!deref_p)
d19a9a82 10214 e->assert_no_components("tracepoint", true);
75ead1f7 10215
f8a968bc
JS
10216 // we can only write to dereferenced fields, and only if guru mode is on
10217 bool lvalue = is_active_lvalue(e);
10218 if (lvalue && (!dw.sess.guru_mode || e->components.empty()))
dc09353a 10219 throw SEMANTIC_ERROR(_F("write to tracepoint variable '%s' not permitted; need stap -g", e->name.c_str()), e->tok);
c69a87e0 10220
ad370dcc
JS
10221 // XXX: if a struct/union arg is passed by value, then writing to its fields
10222 // is also meaningless until you dereference past a pointer member. It's
10223 // harder to detect and prevent that though...
75ead1f7 10224
f8a968bc
JS
10225 if (e->components.empty())
10226 {
03c75a4a 10227 if (e->addressof)
dc09353a 10228 throw SEMANTIC_ERROR(_("cannot take address of tracepoint variable"), e->tok);
a45664f4 10229
3e3bd7b6 10230 // Just grab the value from the probe locals
a45664f4
JS
10231 symbol* sym = new symbol;
10232 sym->tok = e->tok;
10233 sym->name = "__tracepoint_arg_" + arg->name;
6ee19a25 10234 sym->type_details.reset(new exp_type_dwarf(&dw, &arg->type_die, false, false));
a45664f4 10235 provide (sym);
f8a968bc
JS
10236 }
10237 else
10238 {
5f36109e
JS
10239 // make a copy of the original as a bare target symbol for the tracepoint
10240 // value, which will be passed into the dwarf dereferencing code
10241 target_symbol* e2 = deep_copy_visitor::deep_copy(e);
10242 e2->components.clear();
10243
c4965ad9 10244 if (e->check_pretty_print (lvalue))
5f36109e 10245 {
37efef73 10246 dwarf_pretty_print dpp(dw, &arg->type_die, e2, deref_p, false, *e);
5f36109e
JS
10247 dpp.expand()->visit (this);
10248 return;
10249 }
10250
1c0be8c7 10251 bool userspace_p = false;
f8a968bc 10252 string fname = (string(lvalue ? "_tracepoint_tvar_set" : "_tracepoint_tvar_get")
cc9001af 10253 + "_" + e->sym_name()
aca66a36 10254 + "_" + lex_cast(tick++));
75ead1f7 10255
40a393cd
JS
10256 Dwarf_Die endtype;
10257 string code = dw.literal_stmt_for_pointer (&arg->type_die, e, lvalue, &endtype);
6fda2dff 10258
40a393cd 10259 functioncall* n = synthetic_embedded_deref_call(dw, &endtype, fname, code,
1c0be8c7 10260 userspace_p, lvalue, e, e2);
75ead1f7 10261
f8a968bc 10262 if (lvalue)
f55efafe 10263 provide_lvalue_call (n);
75ead1f7 10264
1c0be8c7
JS
10265 // Revisit the functioncall so arguments can be expanded.
10266 n->visit (this);
f8a968bc 10267 }
75ead1f7
JS
10268}
10269
10270
f8a968bc
JS
10271void
10272tracepoint_var_expanding_visitor::visit_target_symbol_context (target_symbol* e)
0a6f5a3f 10273{
03c75a4a 10274 if (e->addressof)
dc09353a 10275 throw SEMANTIC_ERROR(_("cannot take address of context variable"), e->tok);
03c75a4a 10276
f8a968bc 10277 if (is_active_lvalue (e))
dc09353a 10278 throw SEMANTIC_ERROR(_F("write to tracepoint '%s' not permitted", e->name.c_str()), e->tok);
0a6f5a3f 10279
4f58db62 10280 if (e->name == "$$name" || e->name == "$$system")
f8a968bc 10281 {
5f36109e
JS
10282 e->assert_no_components("tracepoint");
10283
4f58db62
JL
10284 string member = (e->name == "$$name") ? "c->ips.tp.tracepoint_name"
10285 : "c->ips.tp.tracepoint_system";
10286
bfdaad1e
DS
10287 // Synthesize an embedded expression.
10288 embedded_expr *expr = new embedded_expr;
10289 expr->tok = e->tok;
4f58db62
JL
10290 expr->code = string("/* string */ /* pure */ " +
10291 member + " ? " + member + " : \"\"");
bfdaad1e 10292 provide (expr);
f8a968bc 10293 }
277c21bc 10294 else if (e->name == "$$vars" || e->name == "$$parms")
f8a968bc 10295 {
5f36109e
JS
10296 e->assert_no_components("tracepoint", true);
10297
1c922ad7 10298 print_format* pf = print_format::create(e->tok, "sprintf");
0a6f5a3f 10299
f8a968bc 10300 for (unsigned i = 0; i < args.size(); ++i)
b278033a 10301 {
dcaa1a65
JS
10302 if (!args[i].usable)
10303 continue;
f8a968bc
JS
10304 if (i > 0)
10305 pf->raw_components += " ";
10306 pf->raw_components += args[i].name;
3e3bd7b6 10307 target_symbol *tsym = new target_symbol;
f8a968bc 10308 tsym->tok = e->tok;
277c21bc 10309 tsym->name = "$" + args[i].name;
5f36109e 10310 tsym->components = e->components;
b278033a 10311
f8a968bc
JS
10312 // every variable should always be accessible!
10313 tsym->saved_conversion_error = 0;
8c2f50c0 10314 expression *texp = require<expression> (tsym); // NB: throws nothing ...
14a97852
JS
10315 if (tsym->saved_conversion_error) // ... but this is how we know it happened.
10316 {
10317 if (dw.sess.verbose>2)
e26c2f83 10318 for (const semantic_error *c = tsym->saved_conversion_error;
c081af73 10319 c != 0; c = c->get_chain())
4c5d9906 10320 clog << _("variable location problem [man error::dwarf]: ") << c->what() << endl;
14a97852
JS
10321 pf->raw_components += "=?";
10322 continue;
10323 }
b278033a 10324
c4965ad9 10325 if (e->check_pretty_print ())
5f36109e
JS
10326 pf->raw_components += "=%s";
10327 else
10328 pf->raw_components += args[i].isptr ? "=%p" : "=%#x";
f8a968bc
JS
10329 pf->args.push_back(texp);
10330 }
0a6f5a3f 10331
f8a968bc
JS
10332 pf->components = print_format::string_to_components(pf->raw_components);
10333 provide (pf);
b278033a 10334 }
f8a968bc
JS
10335 else
10336 assert(0); // shouldn't get here
0a6f5a3f
JS
10337}
10338
0a6f5a3f 10339void
f8a968bc 10340tracepoint_var_expanding_visitor::visit_target_symbol (target_symbol* e)
0a6f5a3f 10341{
aff5d390 10342 try
c69a87e0 10343 {
bd1fcbad 10344 assert(e->name.size() > 0 && e->name[0] == '$');
aff5d390 10345
4f58db62
JL
10346 if (e->name == "$$name" || e->name == "$$system"
10347 || e->name == "$$parms" || e->name == "$$vars")
c69a87e0
FCE
10348 visit_target_symbol_context (e);
10349 else
10350 visit_target_symbol_arg (e);
10351 }
10352 catch (const semantic_error &er)
10353 {
1af1e62d 10354 e->chain (er);
c69a87e0
FCE
10355 provide (e);
10356 }
0a6f5a3f
JS
10357}
10358
10359
79189b84
JS
10360tracepoint_derived_probe::tracepoint_derived_probe (systemtap_session& s,
10361 dwflpp& dw, Dwarf_Die& func_die,
968b8188 10362 const string& tracepoint_system,
79189b84
JS
10363 const string& tracepoint_name,
10364 probe* base, probe_point* loc):
4f58db62
JL
10365 derived_probe (base, loc, true /* .components soon rewritten */), sess (s),
10366 tracepoint_system (tracepoint_system), tracepoint_name (tracepoint_name)
56894e91 10367{
79189b84
JS
10368 // create synthetic probe point name; preserve condition
10369 vector<probe_point::component*> comps;
10370 comps.push_back (new probe_point::component (TOK_KERNEL));
81e0a46a
JL
10371
10372 // tag on system to the final name unless we're in compatibility mode so that
10373 // e.g. pn() returns just the name as before
968b8188 10374 string final_name = tracepoint_name;
81e0a46a
JL
10375 if (!tracepoint_system.empty()
10376 && strverscmp(s.compatible.c_str(), "2.6") > 0)
968b8188 10377 final_name = tracepoint_system + ":" + final_name;
81e0a46a 10378
968b8188
JL
10379 comps.push_back (new probe_point::component (TOK_TRACE,
10380 new literal_string(final_name)));
79189b84
JS
10381 this->sole_location()->components = comps;
10382
6fb70fb7
JS
10383 // fill out the available arguments in this tracepoint
10384 build_args(dw, func_die);
56894e91 10385
6fb70fb7
JS
10386 // determine which header defined this tracepoint
10387 string decl_file = dwarf_decl_file(&func_die);
b78a0fbb 10388 header = decl_file;
d4393459
FCE
10389
10390#if 0 /* This convention is not enforced. */
6fb70fb7
JS
10391 size_t header_pos = decl_file.rfind("trace/");
10392 if (header_pos == string::npos)
dc09353a 10393 throw SEMANTIC_ERROR ("cannot parse header location for tracepoint '"
6fb70fb7
JS
10394 + tracepoint_name + "' in '"
10395 + decl_file + "'");
10396 header = decl_file.substr(header_pos);
d4393459 10397#endif
56894e91 10398
6fb70fb7
JS
10399 // tracepoints from FOO_event_types.h should really be included from FOO.h
10400 // XXX can dwarf tell us the include hierarchy? it would be better to
10401 // ... walk up to see which one was directly included by tracequery.c
3c1b3d06 10402 // XXX: see also PR9993.
d4393459 10403 size_t header_pos = header.find("_event_types");
6fb70fb7
JS
10404 if (header_pos != string::npos)
10405 header.erase(header_pos, 12);
56894e91 10406
f8a968bc
JS
10407 // Now expand the local variables in the probe body
10408 tracepoint_var_expanding_visitor v (dw, name, args);
8b095b45 10409 v.replace (this->body);
a45664f4
JS
10410 for (unsigned i = 0; i < args.size(); i++)
10411 if (args[i].used)
10412 {
10413 vardecl* v = new vardecl;
10414 v->name = "__tracepoint_arg_" + args[i].name;
10415 v->tok = this->tok;
58701b78 10416 v->set_arity(0, this->tok);
a45664f4 10417 v->type = pe_long;
69aa668e 10418 v->synthetic = true;
a45664f4
JS
10419 this->locals.push_back (v);
10420 }
56894e91 10421
79189b84 10422 if (sess.verbose > 2)
ce0f6648 10423 clog << "tracepoint-based " << name << " tracepoint='" << tracepoint_name << "'" << endl;
79189b84 10424}
dc38c0ae 10425
56894e91 10426
f8a968bc 10427static bool
37efef73 10428resolve_pointer_type(Dwarf_Die& die, bool& isptr)
46b84a80 10429{
611a848e
JS
10430 if (null_die(&die))
10431 {
10432 isptr = true;
10433 return true;
10434 }
10435
d19a9a82 10436 Dwarf_Die type;
37efef73 10437 switch (dwarf_tag(&die))
b20febf3 10438 {
f8a968bc
JS
10439 case DW_TAG_typedef:
10440 case DW_TAG_const_type:
10441 case DW_TAG_volatile_type:
c69af88f 10442 case DW_TAG_restrict_type:
f8a968bc 10443 // iterate on the referent type
37efef73
JS
10444 return (dwarf_attr_die(&die, DW_AT_type, &die)
10445 && resolve_pointer_type(die, isptr));
10446
f8a968bc 10447 case DW_TAG_base_type:
a52d2ac0 10448 case DW_TAG_enumeration_type:
37efef73
JS
10449 case DW_TAG_structure_type:
10450 case DW_TAG_union_type:
f8a968bc 10451 // base types will simply be treated as script longs
37efef73
JS
10452 // structs/unions must be referenced by pointer elsewhere
10453 isptr = false;
f8a968bc 10454 return true;
37efef73
JS
10455
10456 case DW_TAG_array_type:
f8a968bc 10457 case DW_TAG_pointer_type:
37efef73
JS
10458 case DW_TAG_reference_type:
10459 case DW_TAG_rvalue_reference_type:
10460 // pointer-like types can be treated as script longs,
dcaa1a65 10461 // and if we know their type, they can also be dereferenced
37efef73
JS
10462 isptr = true;
10463 type = die;
10464 while (dwarf_attr_die(&type, DW_AT_type, &type))
d19a9a82
JS
10465 {
10466 // It still might be a non-type, e.g. const void,
10467 // so we need to strip away all qualifiers.
37efef73 10468 int tag = dwarf_tag(&type);
d19a9a82
JS
10469 if (tag != DW_TAG_typedef &&
10470 tag != DW_TAG_const_type &&
c69af88f
MW
10471 tag != DW_TAG_volatile_type &&
10472 tag != DW_TAG_restrict_type)
d19a9a82 10473 {
37efef73
JS
10474 die = type;
10475 return true;
d19a9a82
JS
10476 }
10477 }
37efef73
JS
10478 // otherwise use a null_die to indicate void
10479 std::memset(&die, 0, sizeof(die));
ad370dcc 10480 return true;
37efef73
JS
10481
10482 default:
10483 // should we consider other types too?
10484 return false;
10485 }
10486}
10487
10488
10489static bool
10490resolve_tracepoint_arg_type(tracepoint_arg& arg)
10491{
10492 if (!resolve_pointer_type(arg.type_die, arg.isptr))
10493 return false;
10494
10495 if (arg.isptr)
10496 arg.typecast = "(intptr_t)";
10497 else if (dwarf_tag(&arg.type_die) == DW_TAG_structure_type ||
10498 dwarf_tag(&arg.type_die) == DW_TAG_union_type)
10499 {
ad370dcc
JS
10500 // for structs/unions which are passed by value, we turn it into
10501 // a pointer that can be dereferenced.
10502 arg.isptr = true;
10503 arg.typecast = "(intptr_t)&";
b20febf3 10504 }
37efef73 10505 return true;
56894e91
JS
10506}
10507
10508
10509void
822a6a3d 10510tracepoint_derived_probe::build_args(dwflpp&, Dwarf_Die& func_die)
56894e91 10511{
6fb70fb7
JS
10512 Dwarf_Die arg;
10513 if (dwarf_child(&func_die, &arg) == 0)
10514 do
10515 if (dwarf_tag(&arg) == DW_TAG_formal_parameter)
10516 {
10517 // build a tracepoint_arg for this parameter
10518 tracepoint_arg tparg;
c60517ca 10519 tparg.name = dwarf_diename(&arg) ?: "";
56894e91 10520
6fb70fb7 10521 // read the type of this parameter
3d1ad340 10522 if (!dwarf_attr_die (&arg, DW_AT_type, &tparg.type_die)
f1c8f8a5 10523 || !dwarf_type_name(&tparg.type_die, tparg.c_type))
dc09353a 10524 throw SEMANTIC_ERROR (_F("cannot get type of parameter '%s' of tracepoint '%s'",
ce0f6648 10525 tparg.name.c_str(), tracepoint_name.c_str()));
a68f81a2 10526
dcaa1a65 10527 tparg.usable = resolve_tracepoint_arg_type(tparg);
6fb70fb7
JS
10528 args.push_back(tparg);
10529 if (sess.verbose > 4)
a52d2ac0
JS
10530 clog << _F("found parameter for tracepoint '%s': type:'%s' name:'%s' %s",
10531 tracepoint_name.c_str(), tparg.c_type.c_str(), tparg.name.c_str(),
10532 tparg.usable ? "ok" : "unavailable") << endl;
6fb70fb7
JS
10533 }
10534 while (dwarf_siblingof(&arg, &arg) == 0);
56894e91
JS
10535}
10536
dc38c0ae 10537void
d0bfd2ac 10538tracepoint_derived_probe::getargs(std::list<std::string> &arg_set) const
dc38c0ae 10539{
dcaa1a65
JS
10540 for (unsigned i = 0; i < args.size(); ++i)
10541 if (args[i].usable)
d0bfd2ac 10542 arg_set.push_back("$"+args[i].name+":"+args[i].c_type);
dc38c0ae
DS
10543}
10544
79189b84
JS
10545void
10546tracepoint_derived_probe::join_group (systemtap_session& s)
197a4d62 10547{
79189b84
JS
10548 if (! s.tracepoint_derived_probes)
10549 s.tracepoint_derived_probes = new tracepoint_derived_probe_group ();
10550 s.tracepoint_derived_probes->enroll (this);
ca6d3b0f 10551 this->group = s.tracepoint_derived_probes;
79189b84 10552}
e38d6504 10553
56894e91 10554
197a4d62 10555void
3e3bd7b6 10556tracepoint_derived_probe::print_dupe_stamp(ostream& o)
56894e91 10557{
3e3bd7b6
JS
10558 for (unsigned i = 0; i < args.size(); i++)
10559 if (args[i].used)
10560 o << "__tracepoint_arg_" << args[i].name << endl;
197a4d62 10561}
56894e91 10562
3e3bd7b6 10563
c9ccb642 10564static vector<string> tracepoint_extra_decls (systemtap_session& s, const string& header)
47dd066d 10565{
3c1b3d06
FCE
10566 vector<string> they_live;
10567 // PR 9993
10568 // XXX: may need this to be configurable
d4393459 10569 they_live.push_back ("#include <linux/skbuff.h>");
9e0cd21a
FCE
10570
10571 // PR11649: conditional extra header
10572 // for kvm tracepoints in 2.6.33ish
10573 if (s.kernel_config["CONFIG_KVM"] != string("")) {
d4393459
FCE
10574 they_live.push_back ("#include <linux/kvm_host.h>");
10575 }
10576
50b72692 10577 if (header.find("xfs") != string::npos && s.kernel_config["CONFIG_XFS_FS"] != string("")) {
d4393459 10578 they_live.push_back ("#define XFS_BIG_BLKNOS 1");
88637c31
FCE
10579 if (s.kernel_source_tree != "")
10580 they_live.push_back ("#include \"fs/xfs/xfs_types.h\""); // in kernel-source tree
d4393459
FCE
10581 they_live.push_back ("struct xfs_mount;");
10582 they_live.push_back ("struct xfs_inode;");
10583 they_live.push_back ("struct xfs_buf;");
10584 they_live.push_back ("struct xfs_bmbt_irec;");
c2cf1b87 10585 they_live.push_back ("struct xfs_trans;");
9e0cd21a 10586 }
d4393459 10587
50b72692 10588 if (header.find("nfs") != string::npos && s.kernel_config["CONFIG_NFSD"] != string("")) {
d4393459
FCE
10589 they_live.push_back ("struct rpc_task;");
10590 }
b64d65e2
FCE
10591 // RHEL6.3
10592 if (header.find("rpc") != string::npos && s.kernel_config["CONFIG_NFSD"] != string("")) {
10593 they_live.push_back ("struct rpc_clnt;");
10594 they_live.push_back ("struct rpc_wait_queue;");
10595 }
d4393459
FCE
10596
10597 they_live.push_back ("#include <asm/cputime.h>");
10598
c2cf1b87
FCE
10599 // linux 3.0
10600 they_live.push_back ("struct cpu_workqueue_struct;");
10601
50b72692 10602 if (header.find("ext4") != string::npos && s.kernel_config["CONFIG_EXT4_FS"] != string(""))
c2cf1b87
FCE
10603 if (s.kernel_source_tree != "")
10604 they_live.push_back ("#include \"fs/ext4/ext4.h\""); // in kernel-source tree
10605
d97d428e 10606 if (header.find("ext3") != string::npos)
50b72692
HP
10607 they_live.push_back ("struct ext3_reserve_window_node;");
10608
d97d428e
FCE
10609 if (header.find("workqueue") != string::npos)
10610 {
10611 they_live.push_back ("struct pool_workqueue;");
10612 they_live.push_back ("struct work_struct;");
10613 }
10614
10615 if (header.find("asoc") != string::npos)
10616 they_live.push_back ("struct snd_soc_dapm_path;");
10617
10618 if (header.find("9p") != string::npos)
10619 {
10620 they_live.push_back ("struct p9_client;");
10621 they_live.push_back ("struct p9_fcall;");
10622 }
10623
10624 if (header.find("bcache") != string::npos)
10625 {
10626 they_live.push_back ("struct bkey;");
10627 they_live.push_back ("struct btree;");
10628 they_live.push_back ("struct cache_set;");
10629 they_live.push_back ("struct cache;");
10630 }
10631
10632 if (header.find("f2fs") != string::npos)
10633 {
10634 // cannot get fs/f2fs/f2fs.h #included
10635 they_live.push_back ("typedef u32 block_t;");
10636 they_live.push_back ("typedef u32 nid_t;");
10637 }
10638
10639 if (header.find("radeon") != string::npos)
10640 they_live.push_back ("struct radeon_bo;");
10641
10642 // argh, 3.11, i915_trace.h -> i915_drv.h -> i915_reg.h without -I
10643 // also brcms_trace_events.h -> ... -> "types.h"
10644 // XXX: need a way to add a temporary -I flag
10645
10646 if (header.find("/ath/") != string::npos)
10647 they_live.push_back ("struct ath5k_hw;");
10648
10649
3c1b3d06
FCE
10650 return they_live;
10651}
47dd066d
WC
10652
10653
10654void
79189b84 10655tracepoint_derived_probe_group::emit_module_decls (systemtap_session& s)
47dd066d 10656{
79189b84
JS
10657 if (probes.empty())
10658 return;
47dd066d 10659
96b030fe 10660 s.op->newline() << "/* ---- tracepoint probes ---- */";
3ef9830a 10661 s.op->newline() << "#include <linux/stp_tracepoint.h>" << endl;
96b030fe 10662 s.op->newline();
79189b84 10663
47dd066d 10664
a4b9c3b3
FCE
10665 // We create a MODULE_aux_N.c file for each tracepoint header, to allow them
10666 // to be separately compiled. That's because kernel tracepoint headers sometimes
10667 // conflict. PR13155.
10668
10669 map<string,translator_output*> per_header_aux;
10670 // GC NB: the translator_output* structs are owned/retained by the systemtap_session.
47dd066d 10671
6fb70fb7
JS
10672 for (unsigned i = 0; i < probes.size(); ++i)
10673 {
10674 tracepoint_derived_probe *p = probes[i];
75ae2ec9 10675 string header = p->header;
5f73a260 10676
a4b9c3b3
FCE
10677 // We cache the auxiliary output files on a per-header basis. We don't
10678 // need one aux file per tracepoint, only one per tracepoint-header.
10679 translator_output *tpop = per_header_aux[header];
10680 if (tpop == 0)
10681 {
10682 tpop = s.op_create_auxiliary();
10683 per_header_aux[header] = tpop;
10684
10685 // PR9993: Add extra headers to work around undeclared types in individual
10686 // include/trace/foo.h files
10687 const vector<string>& extra_decls = tracepoint_extra_decls (s, header);
10688 for (unsigned z=0; z<extra_decls.size(); z++)
10689 tpop->newline() << extra_decls[z] << "\n";
720c435f 10690
a4b9c3b3
FCE
10691 // strip include/ substring, the same way as done in get_tracequery_module()
10692 size_t root_pos = header.rfind("include/");
10693 header = ((root_pos != string::npos) ? header.substr(root_pos + 8) : header);
bfffa443 10694
3ef9830a 10695 tpop->newline() << "#include <linux/stp_tracepoint.h>" << endl;
a4b9c3b3 10696 tpop->newline() << "#include <" << header << ">";
a4b9c3b3
FCE
10697 }
10698
720c435f
JS
10699 // collect the args that are actually in use
10700 vector<const tracepoint_arg*> used_args;
6fb70fb7 10701 for (unsigned j = 0; j < p->args.size(); ++j)
720c435f
JS
10702 if (p->args[j].used)
10703 used_args.push_back(&p->args[j]);
10704
3ef9830a
JS
10705 // forward-declare the generated-side tracepoint callback, and define the
10706 // generated-side tracepoint callback in the main translator-output
10707 string enter_real_fn = "enter_real_tracepoint_probe_" + lex_cast(i);
720c435f 10708 if (used_args.empty())
6fb70fb7 10709 {
3ef9830a
JS
10710 tpop->newline() << "STP_TRACE_ENTER_REAL_NOARGS(" << enter_real_fn << ");";
10711 s.op->newline() << "STP_TRACE_ENTER_REAL_NOARGS(" << enter_real_fn << ")";
6fb70fb7 10712 }
3ef9830a 10713 else
a4b9c3b3 10714 {
3ef9830a
JS
10715 tpop->newline() << "STP_TRACE_ENTER_REAL(" << enter_real_fn;
10716 s.op->newline() << "STP_TRACE_ENTER_REAL(" << enter_real_fn;
10717 s.op->indent(2);
10718 for (unsigned j = 0; j < used_args.size(); ++j)
10719 {
10720 tpop->line() << ", int64_t";
10721 s.op->newline() << ", int64_t __tracepoint_arg_" << used_args[j]->name;
10722 }
10723 tpop->line() << ");";
10724 s.op->newline() << ")";
10725 s.op->indent(-2);
6fb70fb7 10726 }
3ef9830a 10727 s.op->newline() << "{";
7c3e97f4 10728 s.op->newline(1) << "const struct stap_probe * const probe = "
faea5e16 10729 << common_probe_init (p) << ";";
71db462b 10730 common_probe_entryfn_prologue (s, "STAP_SESSION_RUNNING", "probe",
cda141c2 10731 "stp_probe_type_tracepoint");
4f58db62
JL
10732 s.op->newline() << "c->ips.tp.tracepoint_system = "
10733 << lex_cast_qstring (p->tracepoint_system)
10734 << ";";
10735 s.op->newline() << "c->ips.tp.tracepoint_name = "
c12d974f
FCE
10736 << lex_cast_qstring (p->tracepoint_name)
10737 << ";";
720c435f
JS
10738 for (unsigned j = 0; j < used_args.size(); ++j)
10739 {
10740 s.op->newline() << "c->probe_locals." << p->name
0386bbcf 10741 << "." + s.up->c_localname("__tracepoint_arg_" + used_args[j]->name)
720c435f
JS
10742 << " = __tracepoint_arg_" << used_args[j]->name << ";";
10743 }
26e63673 10744 s.op->newline() << "(*probe->ph) (c);";
ef1337ee 10745 common_probe_entryfn_epilogue (s, true, otf_safe_context(s));
6fb70fb7 10746 s.op->newline(-1) << "}";
47dd066d 10747
a4b9c3b3 10748 // define the real tracepoint callback function
3ef9830a
JS
10749 string enter_fn = "enter_tracepoint_probe_" + lex_cast(i);
10750 if (p->args.empty())
10751 tpop->newline() << "static STP_TRACE_ENTER_NOARGS(" << enter_fn << ")";
10752 else
a4b9c3b3 10753 {
3ef9830a
JS
10754 tpop->newline() << "static STP_TRACE_ENTER(" << enter_fn;
10755 s.op->indent(2);
10756 for (unsigned j = 0; j < p->args.size(); ++j)
10757 {
10758 tpop->newline() << ", " << p->args[j].c_type
10759 << " __tracepoint_arg_" << p->args[j].name;
10760 }
10761 tpop->newline() << ")";
10762 s.op->indent(-2);
a4b9c3b3 10763 }
3ef9830a
JS
10764 tpop->newline() << "{";
10765 tpop->newline(1) << enter_real_fn << "(";
720c435f
JS
10766 tpop->indent(2);
10767 for (unsigned j = 0; j < used_args.size(); ++j)
10768 {
10769 if (j > 0)
10770 tpop->line() << ", ";
10771 tpop->newline() << "(int64_t)" << used_args[j]->typecast
10772 << "__tracepoint_arg_" << used_args[j]->name;
10773 }
10774 tpop->newline() << ");";
10775 tpop->newline(-3) << "}";
a4b9c3b3
FCE
10776
10777
96b030fe 10778 // emit normalized registration functions
720c435f 10779 tpop->newline() << "int register_tracepoint_probe_" << i << "(void) {";
3ef9830a
JS
10780 tpop->newline(1) << "return STP_TRACE_REGISTER(" << p->tracepoint_name
10781 << ", " << enter_fn << ");";
a4b9c3b3 10782 tpop->newline(-1) << "}";
47dd066d 10783
86758d5f
JS
10784 // NB: we're not prepared to deal with unreg failures. However, failures
10785 // can only occur if the tracepoint doesn't exist (yet?), or if we
10786 // weren't even registered. The former should be OKed by the initial
10787 // registration call, and the latter is safe to ignore.
720c435f 10788 tpop->newline() << "void unregister_tracepoint_probe_" << i << "(void) {";
3ef9830a
JS
10789 tpop->newline(1) << "(void) STP_TRACE_UNREGISTER(" << p->tracepoint_name
10790 << ", " << enter_fn << ");";
a4b9c3b3
FCE
10791 tpop->newline(-1) << "}";
10792 tpop->newline();
5f73a260 10793
720c435f
JS
10794 // declare normalized registration functions
10795 s.op->newline() << "int register_tracepoint_probe_" << i << "(void);";
10796 s.op->newline() << "void unregister_tracepoint_probe_" << i << "(void);";
5f73a260 10797
a4b9c3b3 10798 tpop->assert_0_indent();
af304783
DS
10799 }
10800
96b030fe
JS
10801 // emit an array of registration functions for easy init/shutdown
10802 s.op->newline() << "static struct stap_tracepoint_probe {";
10803 s.op->newline(1) << "int (*reg)(void);";
86758d5f 10804 s.op->newline(0) << "void (*unreg)(void);";
96b030fe
JS
10805 s.op->newline(-1) << "} stap_tracepoint_probes[] = {";
10806 s.op->indent(1);
10807 for (unsigned i = 0; i < probes.size(); ++i)
10808 {
10809 s.op->newline () << "{";
10810 s.op->line() << " .reg=&register_tracepoint_probe_" << i << ",";
10811 s.op->line() << " .unreg=&unregister_tracepoint_probe_" << i;
10812 s.op->line() << " },";
10813 }
10814 s.op->newline(-1) << "};";
10815 s.op->newline();
47dd066d
WC
10816}
10817
10818
79189b84
JS
10819void
10820tracepoint_derived_probe_group::emit_module_init (systemtap_session &s)
47dd066d 10821{
79189b84
JS
10822 if (probes.size () == 0)
10823 return;
47dd066d 10824
79189b84 10825 s.op->newline() << "/* init tracepoint probes */";
96b030fe
JS
10826 s.op->newline() << "for (i=0; i<" << probes.size() << "; i++) {";
10827 s.op->newline(1) << "rc = stap_tracepoint_probes[i].reg();";
10828 s.op->newline() << "if (rc) {";
10829 s.op->newline(1) << "for (j=i-1; j>=0; j--)"; // partial rollback
10830 s.op->newline(1) << "stap_tracepoint_probes[j].unreg();";
10831 s.op->newline(-1) << "break;"; // don't attempt to register any more probes
10832 s.op->newline(-1) << "}";
10833 s.op->newline(-1) << "}";
47dd066d 10834
bc9a523d
FCE
10835 // This would be technically proper (on those autoconf-detectable
10836 // kernels that include this function in tracepoint.h), however we
10837 // already make several calls to synchronze_sched() during our
10838 // shutdown processes.
47dd066d 10839
bc9a523d
FCE
10840 // s.op->newline() << "if (rc)";
10841 // s.op->newline(1) << "tracepoint_synchronize_unregister();";
10842 // s.op->indent(-1);
79189b84 10843}
47dd066d
WC
10844
10845
79189b84
JS
10846void
10847tracepoint_derived_probe_group::emit_module_exit (systemtap_session& s)
47dd066d 10848{
79189b84
JS
10849 if (probes.empty())
10850 return;
47dd066d 10851
96b030fe
JS
10852 s.op->newline() << "/* deregister tracepoint probes */";
10853 s.op->newline() << "for (i=0; i<" << probes.size() << "; i++)";
10854 s.op->newline(1) << "stap_tracepoint_probes[i].unreg();";
10855 s.op->indent(-1);
47dd066d 10856
bc9a523d 10857 // Not necessary: see above.
47dd066d 10858
bc9a523d 10859 // s.op->newline() << "tracepoint_synchronize_unregister();";
79189b84 10860}
b20febf3 10861
47dd066d 10862
75ead1f7 10863struct tracepoint_query : public base_query
47dd066d 10864{
75ead1f7
JS
10865 probe * base_probe;
10866 probe_point * base_loc;
10867 vector<derived_probe *> & results;
f982c59b 10868 set<string> probed_names;
47dd066d 10869
75ead1f7
JS
10870 void handle_query_module();
10871 int handle_query_cu(Dwarf_Die * cudie);
10872 int handle_query_func(Dwarf_Die * func);
822a6a3d 10873 void query_library (const char *) {}
576eaefe 10874 void query_plt (const char *entry, size_t addr) {}
b20febf3 10875
5c378838 10876 static int tracepoint_query_cu (Dwarf_Die * cudie, tracepoint_query * q);
7d007451 10877 static int tracepoint_query_func (Dwarf_Die * func, tracepoint_query * q);
e6d3a5bd
JL
10878
10879 tracepoint_query(dwflpp & dw, const string & tracepoint,
10880 probe * base_probe, probe_point * base_loc,
10881 vector<derived_probe *> & results):
10882 base_query(dw, "*"), base_probe(base_probe),
10883 base_loc(base_loc), results(results)
10884 {
10885 // The user may have specified the system to probe, e.g. all of the
10886 // following are possible:
10887 //
10888 // sched_switch --> tracepoint named sched_switch
10889 // sched:sched_switch --> tracepoint named sched_switch in the sched system
10890 // sch*:sched_* --> system starts with sch and tracepoint starts with sched_
10891 // sched:* --> all tracepoints in system sched
10892 // *:sched_switch --> same as just sched_switch
10893
10894 size_t sys_pos = tracepoint.find(':');
10895 if (sys_pos == string::npos)
10896 {
10897 this->system = "";
10898 this->tracepoint = tracepoint;
10899 }
10900 else
10901 {
81e0a46a
JL
10902 if (strverscmp(sess.compatible.c_str(), "2.6") <= 0)
10903 throw SEMANTIC_ERROR (_("SYSTEM:TRACEPOINT syntax not supported "
10904 "with --compatible <= 2.6"));
10905
e6d3a5bd
JL
10906 this->system = tracepoint.substr(0, sys_pos);
10907 this->tracepoint = tracepoint.substr(sys_pos+1);
10908 }
7bed8bec
JL
10909
10910 // make sure we have something to query (filters out e.g. "" and ":")
10911 if (this->tracepoint.empty())
10912 throw SEMANTIC_ERROR (_("invalid tracepoint string provided"));
e6d3a5bd 10913 }
99c2b408
JL
10914
10915private:
10916 string system; // target subsystem(s) to query
10917 string tracepoint; // target tracepoint(s) to query
10918 string current_system; // subsystem of module currently being visited
10919
10920 string retrieve_trace_system();
75ead1f7 10921};
47dd066d 10922
befcb049
JL
10923// name of section where TRACE_SYSTEM is stored
10924// (see tracepoint_builder::get_tracequery_modules())
10925#define STAP_TRACE_SYSTEM ".stap_trace_system"
47dd066d 10926
968b8188
JL
10927string
10928tracepoint_query::retrieve_trace_system()
10929{
10930 Dwarf_Addr bias;
10931 Elf* elf = dwfl_module_getelf(dw.module, &bias);
10932 if (!elf)
10933 return "";
10934
10935 size_t shstrndx;
10936 if (elf_getshdrstrndx(elf, &shstrndx))
10937 return "";
10938
10939 Elf_Scn *scn = NULL;
10940 GElf_Shdr shdr_mem;
10941
10942 while ((scn = elf_nextscn(elf, scn)))
10943 {
10944 if (gelf_getshdr(scn, &shdr_mem) == NULL)
10945 return "";
10946
10947 const char *name = elf_strptr(elf, shstrndx, shdr_mem.sh_name);
10948 if (name == NULL)
10949 return "";
10950
befcb049 10951 if (strcmp(name, STAP_TRACE_SYSTEM) == 0)
968b8188
JL
10952 break;
10953 }
10954
10955 if (scn == NULL)
10956 return "";
10957
10958 // Extract saved TRACE_SYSTEM in section
10959 Elf_Data *data = elf_getdata(scn, NULL);
10960 if (!data)
10961 return "";
10962
10963 return string((char*)data->d_buf);
10964}
10965
10966
47dd066d 10967void
75ead1f7 10968tracepoint_query::handle_query_module()
47dd066d 10969{
99c2b408 10970 // Get the TRACE_SYSTEM for this module, if any. It will be found in the
befcb049 10971 // STAP_TRACE_SYSTEM section if it exists.
968b8188
JL
10972 current_system = retrieve_trace_system();
10973
10974 // check if user wants a specific system
10975 if (!system.empty())
10976 {
10977 // don't need to go further if module has no system or doesn't
10978 // match the one we want
10979 if (current_system.empty()
10980 || !dw.function_name_matches_pattern(current_system, system))
10981 return;
10982 }
10983
75ead1f7 10984 // look for the tracepoints in each CU
337b7c44 10985 dw.iterate_over_cus(tracepoint_query_cu, this, false);
47dd066d
WC
10986}
10987
10988
75ead1f7
JS
10989int
10990tracepoint_query::handle_query_cu(Dwarf_Die * cudie)
47dd066d 10991{
75ead1f7 10992 dw.focus_on_cu (cudie);
5f52fafe 10993 dw.mod_info->get_symtab();
47dd066d 10994
75ead1f7
JS
10995 // look at each function to see if it's a tracepoint
10996 string function = "stapprobe_" + tracepoint;
10997 return dw.iterate_over_functions (tracepoint_query_func, this, function);
47dd066d
WC
10998}
10999
11000
75ead1f7
JS
11001int
11002tracepoint_query::handle_query_func(Dwarf_Die * func)
47dd066d 11003{
75ead1f7 11004 dw.focus_on_function (func);
47dd066d 11005
60d98537 11006 assert(startswith(dw.function_name, "stapprobe_"));
75ead1f7 11007 string tracepoint_instance = dw.function_name.substr(10);
f982c59b
JS
11008
11009 // check for duplicates -- sometimes tracepoint headers may be indirectly
11010 // included in more than one of our tracequery modules.
11011 if (!probed_names.insert(tracepoint_instance).second)
11012 return DWARF_CB_OK;
11013
c4a04833
FCE
11014 // PR17126: blacklist
11015 if (!sess.guru_mode)
11016 {
11017 if ((sess.architecture.substr(0,3) == "ppc" ||
11018 sess.architecture.substr(0,7) == "powerpc") &&
11019 (tracepoint_instance == "hcall_entry" ||
11020 tracepoint_instance == "hcall_exit"))
11021 {
11022 sess.print_warning(_F("tracepoint %s is blacklisted on architecture %s",
11023 tracepoint_instance.c_str(), sess.architecture.c_str()));
11024 return DWARF_CB_OK;
11025 }
11026 }
11027
79189b84 11028 derived_probe *dp = new tracepoint_derived_probe (dw.sess, dw, *func,
968b8188 11029 current_system,
79189b84
JS
11030 tracepoint_instance,
11031 base_probe, base_loc);
11032 results.push_back (dp);
75ead1f7 11033 return DWARF_CB_OK;
47dd066d
WC
11034}
11035
11036
75ead1f7 11037int
5c378838 11038tracepoint_query::tracepoint_query_cu (Dwarf_Die * cudie, tracepoint_query * q)
47dd066d 11039{
85007c04 11040 if (pending_interrupts) return DWARF_CB_ABORT;
75ead1f7 11041 return q->handle_query_cu(cudie);
47dd066d
WC
11042}
11043
11044
75ead1f7 11045int
7d007451 11046tracepoint_query::tracepoint_query_func (Dwarf_Die * func, tracepoint_query * q)
47dd066d 11047{
85007c04 11048 if (pending_interrupts) return DWARF_CB_ABORT;
75ead1f7 11049 return q->handle_query_func(func);
47dd066d
WC
11050}
11051
11052
0a6f5a3f 11053struct tracepoint_builder: public derived_probe_builder
47dd066d 11054{
0a6f5a3f
JS
11055private:
11056 dwflpp *dw;
11057 bool init_dw(systemtap_session& s);
c9ccb642
FCE
11058 void get_tracequery_modules(systemtap_session& s,
11059 const vector<string>& headers,
11060 vector<string>& modules);
47dd066d 11061
0a6f5a3f 11062public:
47dd066d 11063
0a6f5a3f
JS
11064 tracepoint_builder(): dw(0) {}
11065 ~tracepoint_builder() { delete dw; }
47dd066d 11066
0a6f5a3f
JS
11067 void build_no_more (systemtap_session& s)
11068 {
11069 if (dw && s.verbose > 3)
b530b5b3 11070 clog << _("tracepoint_builder releasing dwflpp") << endl;
0a6f5a3f
JS
11071 delete dw;
11072 dw = NULL;
435f53a7
FCE
11073
11074 delete_session_module_cache (s);
0a6f5a3f 11075 }
47dd066d 11076
0a6f5a3f
JS
11077 void build(systemtap_session& s,
11078 probe *base, probe_point *location,
11079 literal_map_t const& parameters,
11080 vector<derived_probe*>& finished_results);
11081};
47dd066d 11082
47dd066d 11083
c9ccb642 11084
2a0e62a8 11085// Create (or cache) one or more tracequery .o modules, based upon the
c9ccb642
FCE
11086// tracepoint-related header files given. Return the generated or cached
11087// modules[].
11088
11089void
11090tracepoint_builder::get_tracequery_modules(systemtap_session& s,
11091 const vector<string>& headers,
11092 vector<string>& modules)
0a6f5a3f 11093{
c95eddf7 11094 if (s.verbose > 2)
55e50c24 11095 {
ce0f6648 11096 clog << _F("Pass 2: getting a tracepoint query for %zu headers: ", headers.size()) << endl;
55e50c24
JS
11097 for (size_t i = 0; i < headers.size(); ++i)
11098 clog << " " << headers[i] << endl;
11099 }
c95eddf7 11100
2a0e62a8
JS
11101 map<string,string> headers_cache_obj; // header name -> cache/.../tracequery_hash.o file name
11102 // Map the headers to cache .o names. Note that this has side-effects of
c9ccb642
FCE
11103 // creating the $SYSTEMTAP_DIR/.cache/XX/... directory and the hash-log file,
11104 // so we prefer not to repeat this.
11105 vector<string> uncached_headers;
11106 for (size_t i=0; i<headers.size(); i++)
2a0e62a8 11107 headers_cache_obj[headers[i]] = find_tracequery_hash(s, headers[i]);
c9ccb642
FCE
11108
11109 // They may be in the cache already.
11110 if (s.use_cache && !s.poison_cache)
11111 for (size_t i=0; i<headers.size(); i++)
11112 {
11113 // see if the cached module exists
2a0e62a8 11114 const string& tracequery_path = headers_cache_obj[headers[i]];
c9ccb642
FCE
11115 if (!tracequery_path.empty() && file_exists(tracequery_path))
11116 {
11117 if (s.verbose > 2)
11118 clog << _F("Pass 2: using cached %s", tracequery_path.c_str()) << endl;
47dd066d 11119
c252fca2
JS
11120 // an empty file is a cached failure
11121 if (get_file_size(tracequery_path) > 0)
11122 modules.push_back (tracequery_path);
c9ccb642
FCE
11123 }
11124 else
11125 uncached_headers.push_back(headers[i]);
11126 }
11127 else
11128 uncached_headers = headers;
f982c59b 11129
c9ccb642
FCE
11130 // If we have nothing left to search for, quit
11131 if (uncached_headers.empty()) return;
55e50c24 11132
c9ccb642 11133 map<string,string> headers_tracequery_src; // header -> C-source code mapping
55e50c24 11134
c9ccb642
FCE
11135 // We could query several subsets of headers[] to make this go
11136 // faster, but let's KISS and do one at a time.
11137 for (size_t i=0; i<uncached_headers.size(); i++)
55e50c24 11138 {
c9ccb642
FCE
11139 const string& header = uncached_headers[i];
11140
11141 // create a tracequery source file
11142 ostringstream osrc;
11143
11144 // PR9993: Add extra headers to work around undeclared types in individual
11145 // include/trace/foo.h files
11146 vector<string> short_decls = tracepoint_extra_decls(s, header);
11147
11148 // add each requested tracepoint header
75ae2ec9 11149 size_t root_pos = header.rfind("include/");
832f100d 11150 short_decls.push_back(string("#include <") +
75ae2ec9 11151 ((root_pos != string::npos) ? header.substr(root_pos + 8) : header) +
d4393459 11152 string(">"));
f982c59b 11153
c9ccb642
FCE
11154 osrc << "#ifdef CONFIG_TRACEPOINTS" << endl;
11155 osrc << "#include <linux/tracepoint.h>" << endl;
832f100d
JS
11156
11157 // the kernel has changed this naming a few times, previously TPPROTO,
11158 // TP_PROTO, TPARGS, TP_ARGS, etc. so let's just dupe the latest.
11159 osrc << "#ifndef PARAMS" << endl;
11160 osrc << "#define PARAMS(args...) args" << endl;
11161 osrc << "#endif" << endl;
11162
c9ccb642
FCE
11163 // override DECLARE_TRACE to synthesize probe functions for us
11164 osrc << "#undef DECLARE_TRACE" << endl;
11165 osrc << "#define DECLARE_TRACE(name, proto, args) \\" << endl;
11166 osrc << " void stapprobe_##name(proto) {}" << endl;
832f100d 11167
c9ccb642
FCE
11168 // 2.6.35 added the NOARGS variant, but it's the same for us
11169 osrc << "#undef DECLARE_TRACE_NOARGS" << endl;
11170 osrc << "#define DECLARE_TRACE_NOARGS(name) \\" << endl;
11171 osrc << " DECLARE_TRACE(name, void, )" << endl;
832f100d
JS
11172
11173 // 2.6.38 added the CONDITION variant, which can also just redirect
11174 osrc << "#undef DECLARE_TRACE_CONDITION" << endl;
11175 osrc << "#define DECLARE_TRACE_CONDITION(name, proto, args, cond) \\" << endl;
11176 osrc << " DECLARE_TRACE(name, PARAMS(proto), PARAMS(args))" << endl;
11177
c9ccb642
FCE
11178 // older tracepoints used DEFINE_TRACE, so redirect that too
11179 osrc << "#undef DEFINE_TRACE" << endl;
11180 osrc << "#define DEFINE_TRACE(name, proto, args) \\" << endl;
832f100d
JS
11181 osrc << " DECLARE_TRACE(name, PARAMS(proto), PARAMS(args))" << endl;
11182
c9ccb642
FCE
11183 // add the specified decls/#includes
11184 for (unsigned z=0; z<short_decls.size(); z++)
11185 osrc << "#undef TRACE_INCLUDE_FILE\n"
11186 << "#undef TRACE_INCLUDE_PATH\n"
11187 << short_decls[z] << "\n";
11188
7d46f26e
JL
11189 // create a section that will hold the TRACE_SYSTEM for this header
11190 osrc << "#ifdef TRACE_SYSTEM" << endl;
11191 osrc << "const char stap_trace_system[]" << endl;
befcb049 11192 osrc << " __attribute__((section(\"" STAP_TRACE_SYSTEM "\")))" << endl;
7d46f26e
JL
11193 osrc << " = __stringify(TRACE_SYSTEM);" << endl;
11194 osrc << "#endif" << endl;
11195
c9ccb642
FCE
11196 // finish up the module source
11197 osrc << "#endif /* CONFIG_TRACEPOINTS */" << endl;
47dd066d 11198
c9ccb642
FCE
11199 // save the source file away
11200 headers_tracequery_src[header] = osrc.str();
55e50c24 11201 }
f982c59b 11202
c9ccb642 11203 // now build them all together
2a0e62a8 11204 map<string,string> tracequery_objs = make_tracequeries(s, headers_tracequery_src);
47dd066d 11205
c9ccb642 11206 // now plop them into the cache
b278033a 11207 if (s.use_cache)
c9ccb642
FCE
11208 for (size_t i=0; i<uncached_headers.size(); i++)
11209 {
11210 const string& header = uncached_headers[i];
2a0e62a8
JS
11211 const string& tracequery_obj = tracequery_objs[header];
11212 const string& tracequery_path = headers_cache_obj[header];
11213 if (tracequery_obj !="" && file_exists(tracequery_obj))
c9ccb642 11214 {
2a0e62a8 11215 copy_file(tracequery_obj, tracequery_path, s.verbose > 2);
c9ccb642
FCE
11216 modules.push_back (tracequery_path);
11217 }
c252fca2
JS
11218 else
11219 // cache an empty file for failures
11220 copy_file("/dev/null", tracequery_path, s.verbose > 2);
c9ccb642 11221 }
f982c59b
JS
11222}
11223
11224
d4393459 11225
f982c59b
JS
11226bool
11227tracepoint_builder::init_dw(systemtap_session& s)
11228{
11229 if (dw != NULL)
11230 return true;
11231
11232 vector<string> tracequery_modules;
55e50c24 11233 vector<string> system_headers;
f982c59b
JS
11234
11235 glob_t trace_glob;
d4393459 11236
b19a4376 11237 // find kernel_source_tree from DW_AT_comp_dir
d4393459 11238 if (s.kernel_source_tree == "")
f982c59b 11239 {
d4393459 11240 unsigned found;
ccf2c922 11241 Dwfl *dwfl = setup_dwfl_kernel ("kernel", &found, s);
d4393459
FCE
11242 if (found)
11243 {
11244 Dwarf_Die *cudie = 0;
11245 Dwarf_Addr bias;
11246 while ((cudie = dwfl_nextcu (dwfl, cudie, &bias)) != NULL)
11247 {
e19ebcf7 11248 assert_no_interrupts();
d4393459
FCE
11249 Dwarf_Attribute attr;
11250 const char* name = dwarf_formstring (dwarf_attr (cudie, DW_AT_comp_dir, &attr));
36d65b45 11251 if (name)
d4393459 11252 {
f27496f3
JS
11253 // Before we try to use it, check that the path actually
11254 // exists locally and is distinct from the build tree.
11255 if (!file_exists(name))
36d65b45
JS
11256 {
11257 if (s.verbose > 2)
f27496f3
JS
11258 clog << _F("Ignoring inaccessible kernel source tree (DW_AT_comp_dir) at '%s'", name) << endl;
11259 }
11260 else if (resolve_path(name) == resolve_path(s.kernel_build_tree))
11261 {
11262 if (s.verbose > 2)
11263 clog << _F("Ignoring duplicate kernel source tree (DW_AT_comp_dir) at '%s'", name) << endl;
36d65b45
JS
11264 }
11265 else
11266 {
11267 if (s.verbose > 2)
f27496f3
JS
11268 clog << _F("Located kernel source tree (DW_AT_comp_dir) at '%s'", name) << endl;
11269 s.kernel_source_tree = name;
36d65b45 11270 }
61f1a63b 11271
d4393459
FCE
11272 break; // skip others; modern Kbuild uses same comp_dir for them all
11273 }
11274 }
11275 }
44392d73 11276 dwfl_end (dwfl);
d4393459
FCE
11277 }
11278
b19a4376
JS
11279 // find kernel_source_tree from a source link, when different from build
11280 if (s.kernel_source_tree == "" && endswith(s.kernel_build_tree, "/build"))
11281 {
11282 string source_tree = s.kernel_build_tree;
11283 source_tree.replace(source_tree.length() - 5, 5, "source");
11284 if (file_exists(source_tree) &&
11285 resolve_path(source_tree) != resolve_path(s.kernel_build_tree))
11286 {
11287 if (s.verbose > 2)
11288 clog << _F("Located kernel source tree at '%s'", source_tree.c_str()) << endl;
11289 s.kernel_source_tree = source_tree;
11290 }
11291 }
11292
d4393459
FCE
11293 // prefixes
11294 vector<string> glob_prefixes;
11295 glob_prefixes.push_back (s.kernel_build_tree);
11296 if (s.kernel_source_tree != "")
11297 glob_prefixes.push_back (s.kernel_source_tree);
11298
11299 // suffixes
11300 vector<string> glob_suffixes;
11301 glob_suffixes.push_back("include/trace/events/*.h");
11302 glob_suffixes.push_back("include/trace/*.h");
d97d428e 11303 glob_suffixes.push_back("include/ras/*_event.h");
d4393459 11304 glob_suffixes.push_back("arch/x86/kvm/*trace.h");
d97d428e
FCE
11305 glob_suffixes.push_back("arch/x86/kernel/*trace.h");
11306 glob_suffixes.push_back("arch/*/include/asm/trace*.h");
11307 glob_suffixes.push_back("arch/*/include/asm/trace/*.h");
f89ff3e2 11308 glob_suffixes.push_back("fs/xfs/linux-*/xfs_tr*.h");
d97d428e
FCE
11309 glob_suffixes.push_back("fs/*/*trace*.h");
11310 glob_suffixes.push_back("net/*/*trace*.h");
11311 glob_suffixes.push_back("sound/pci/hda/*_trace.h");
11312 glob_suffixes.push_back("drivers/gpu/drm/*_trace.h");
11313 glob_suffixes.push_back("drivers/gpu/drm/*/*_trace.h");
11314 glob_suffixes.push_back("drivers/net/wireless/*/*/*trace*.h");
11315 glob_suffixes.push_back("drivers/usb/host/*trace*.h");
11316
11317 // see also tracepoint_extra_decls above
d4393459
FCE
11318
11319 // compute cartesian product
11320 vector<string> globs;
11321 for (unsigned i=0; i<glob_prefixes.size(); i++)
11322 for (unsigned j=0; j<glob_suffixes.size(); j++)
11323 globs.push_back (glob_prefixes[i]+string("/")+glob_suffixes[j]);
11324
8aa43b8d 11325 set<string> duped_headers;
d4393459
FCE
11326 for (unsigned z = 0; z < globs.size(); z++)
11327 {
11328 string glob_str = globs[z];
11329 if (s.verbose > 3)
b530b5b3 11330 clog << _("Checking tracepoint glob ") << glob_str << endl;
d4393459 11331
067cc66f
CM
11332 int r = glob(glob_str.c_str(), 0, NULL, &trace_glob);
11333 if (r == GLOB_NOSPACE || r == GLOB_ABORTED)
11334 throw runtime_error("Error globbing tracepoint");
11335
f982c59b
JS
11336 for (unsigned i = 0; i < trace_glob.gl_pathc; ++i)
11337 {
11338 string header(trace_glob.gl_pathv[i]);
11339
11340 // filter out a few known "internal-only" headers
60d98537
JS
11341 if (endswith(header, "/define_trace.h") ||
11342 endswith(header, "/ftrace.h") ||
11343 endswith(header, "/trace_events.h") ||
11344 endswith(header, "_event_types.h"))
f982c59b
JS
11345 continue;
11346
b1966849
FCE
11347 // With headers now plopped under arch/FOO/include/asm/*,
11348 // the following logic miss some tracepoints.
11349#if 0
8aa43b8d
JS
11350 // skip identical headers from the build and source trees.
11351 size_t root_pos = header.rfind("include/");
11352 if (root_pos != string::npos &&
11353 !duped_headers.insert(header.substr(root_pos + 8)).second)
11354 continue;
b1966849 11355#endif
8aa43b8d 11356
55e50c24 11357 system_headers.push_back(header);
f982c59b
JS
11358 }
11359 globfree(&trace_glob);
11360 }
11361
c9ccb642
FCE
11362 // Build tracequery modules
11363 get_tracequery_modules(s, system_headers, tracequery_modules);
55e50c24 11364
f982c59b
JS
11365 // TODO: consider other sources of tracepoint headers too, like from
11366 // a command-line parameter or some environment or .systemtaprc
47dd066d 11367
59c11f91 11368 dw = new dwflpp(s, tracequery_modules, true);
0a6f5a3f
JS
11369 return true;
11370}
47dd066d 11371
0a6f5a3f
JS
11372void
11373tracepoint_builder::build(systemtap_session& s,
11374 probe *base, probe_point *location,
11375 literal_map_t const& parameters,
11376 vector<derived_probe*>& finished_results)
11377{
11378 if (!init_dw(s))
11379 return;
47dd066d 11380
75ead1f7
JS
11381 string tracepoint;
11382 assert(get_param (parameters, TOK_TRACE, tracepoint));
47dd066d 11383
75ead1f7 11384 tracepoint_query q(*dw, tracepoint, base, location, finished_results);
d906ab9d 11385 unsigned results_pre = finished_results.size();
06de3a04 11386 dw->iterate_over_modules<base_query>(&query_module, &q);
d906ab9d
JL
11387 unsigned results_post = finished_results.size();
11388
11389 // Did we fail to find a match? Let's suggest something!
11390 if (results_pre == results_post)
11391 {
11392 size_t pos;
11393 string sugs = suggest_dwarf_functions(s, q.visited_modules, tracepoint);
11394 while ((pos = sugs.find("stapprobe_")) != string::npos)
11395 sugs.erase(pos, string("stapprobe_").size());
11396 if (!sugs.empty())
ece93f53
JL
11397 throw SEMANTIC_ERROR (_NF("no match (similar tracepoint: %s)",
11398 "no match (similar tracepoints: %s)",
d906ab9d
JL
11399 sugs.find(',') == string::npos,
11400 sugs.c_str()));
11401 }
47dd066d 11402}
47dd066d 11403
e6fe60e7 11404
b55bc428 11405// ------------------------------------------------------------------------
bd2b1e68 11406// Standard tapset registry.
b55bc428
FCE
11407// ------------------------------------------------------------------------
11408
7a053d3b 11409void
f8220a7b 11410register_standard_tapsets(systemtap_session & s)
b55bc428 11411{
47e0478e 11412 register_tapset_been(s);
93646f4d 11413 register_tapset_itrace(s);
dd0e4fa7 11414 register_tapset_mark(s);
7a212aa8 11415 register_tapset_procfs(s);
912e8c59 11416 register_tapset_timers(s);
8d9609f5 11417 register_tapset_netfilter(s);
b84779a5 11418 register_tapset_utrace(s);
b98a8d73 11419
7a24d422 11420 // dwarf-based kprobe/uprobe parts
c4ce66a1 11421 dwarf_derived_probe::register_patterns(s);
30a279be 11422
888af770
FCE
11423 // XXX: user-space starter set
11424 s.pattern_root->bind_num(TOK_PROCESS)
11425 ->bind_num(TOK_STATEMENT)->bind(TOK_ABSOLUTE)
f66bb29a 11426 ->bind_privilege(pr_all)
888af770
FCE
11427 ->bind(new uprobe_builder ());
11428 s.pattern_root->bind_num(TOK_PROCESS)
11429 ->bind_num(TOK_STATEMENT)->bind(TOK_ABSOLUTE)->bind(TOK_RETURN)
f66bb29a 11430 ->bind_privilege(pr_all)
888af770
FCE
11431 ->bind(new uprobe_builder ());
11432
0a6f5a3f
JS
11433 // kernel tracepoint probes
11434 s.pattern_root->bind(TOK_KERNEL)->bind_str(TOK_TRACE)
11435 ->bind(new tracepoint_builder());
11436
e6fe60e7
AM
11437 // Kprobe based probe
11438 s.pattern_root->bind(TOK_KPROBE)->bind_str(TOK_FUNCTION)
11439 ->bind(new kprobe_builder());
3c57fe1f
JS
11440 s.pattern_root->bind(TOK_KPROBE)->bind_str(TOK_FUNCTION)->bind(TOK_CALL)
11441 ->bind(new kprobe_builder());
e6fe60e7
AM
11442 s.pattern_root->bind(TOK_KPROBE)->bind_str(TOK_MODULE)
11443 ->bind_str(TOK_FUNCTION)->bind(new kprobe_builder());
3c57fe1f
JS
11444 s.pattern_root->bind(TOK_KPROBE)->bind_str(TOK_MODULE)
11445 ->bind_str(TOK_FUNCTION)->bind(TOK_CALL)->bind(new kprobe_builder());
e6fe60e7
AM
11446 s.pattern_root->bind(TOK_KPROBE)->bind_str(TOK_FUNCTION)->bind(TOK_RETURN)
11447 ->bind(new kprobe_builder());
b6371390
JS
11448 s.pattern_root->bind(TOK_KPROBE)->bind_str(TOK_FUNCTION)->bind(TOK_RETURN)
11449 ->bind_num(TOK_MAXACTIVE)->bind(new kprobe_builder());
e6fe60e7
AM
11450 s.pattern_root->bind(TOK_KPROBE)->bind_str(TOK_MODULE)
11451 ->bind_str(TOK_FUNCTION)->bind(TOK_RETURN)->bind(new kprobe_builder());
b6371390
JS
11452 s.pattern_root->bind(TOK_KPROBE)->bind_str(TOK_MODULE)
11453 ->bind_str(TOK_FUNCTION)->bind(TOK_RETURN)
11454 ->bind_num(TOK_MAXACTIVE)->bind(new kprobe_builder());
e6fe60e7
AM
11455 s.pattern_root->bind(TOK_KPROBE)->bind_num(TOK_STATEMENT)
11456 ->bind(TOK_ABSOLUTE)->bind(new kprobe_builder());
dd225250
PS
11457
11458 //Hwbkpt based probe
b47f3a55
FCE
11459 // NB: we formerly registered the probe point types only if the kernel configuration
11460 // allowed it. However, we get better error messages if we allow probes to resolve.
11461 s.pattern_root->bind(TOK_KERNEL)->bind_num(TOK_HWBKPT)
11462 ->bind(TOK_HWBKPT_WRITE)->bind(new hwbkpt_builder());
11463 s.pattern_root->bind(TOK_KERNEL)->bind_str(TOK_HWBKPT)
11464 ->bind(TOK_HWBKPT_WRITE)->bind(new hwbkpt_builder());
11465 s.pattern_root->bind(TOK_KERNEL)->bind_num(TOK_HWBKPT)
11466 ->bind(TOK_HWBKPT_RW)->bind(new hwbkpt_builder());
11467 s.pattern_root->bind(TOK_KERNEL)->bind_str(TOK_HWBKPT)
11468 ->bind(TOK_HWBKPT_RW)->bind(new hwbkpt_builder());
11469 s.pattern_root->bind(TOK_KERNEL)->bind_num(TOK_HWBKPT)
11470 ->bind_num(TOK_LENGTH)->bind(TOK_HWBKPT_WRITE)->bind(new hwbkpt_builder());
11471 s.pattern_root->bind(TOK_KERNEL)->bind_num(TOK_HWBKPT)
11472 ->bind_num(TOK_LENGTH)->bind(TOK_HWBKPT_RW)->bind(new hwbkpt_builder());
11473 // length supported with address only, not symbol names
83ea76b1
WC
11474
11475 //perf event based probe
4763f713 11476 register_tapset_perf(s);
a29858ef 11477 register_tapset_java(s);
b55bc428 11478}
dc38c0ae
DS
11479
11480
b20febf3
FCE
11481vector<derived_probe_group*>
11482all_session_groups(systemtap_session& s)
dc38c0ae 11483{
b20febf3 11484 vector<derived_probe_group*> g;
912e8c59
JS
11485
11486#define DOONE(x) \
11487 if (s. x##_derived_probes) \
11488 g.push_back ((derived_probe_group*)(s. x##_derived_probes))
ab655cf8
DS
11489
11490 // Note that order *is* important here. We want to make sure we
11491 // register (actually run) begin probes before any other probe type
11492 // is run. Similarly, when unregistering probes, we want to
11493 // unregister (actually run) end probes after every other probe type
11494 // has be unregistered. To do the latter,
11495 // c_unparser::emit_module_exit() will run this list backwards.
b20febf3
FCE
11496 DOONE(be);
11497 DOONE(dwarf);
888af770 11498 DOONE(uprobe);
b20febf3
FCE
11499 DOONE(timer);
11500 DOONE(profile);
11501 DOONE(mark);
0a6f5a3f 11502 DOONE(tracepoint);
e6fe60e7 11503 DOONE(kprobe);
dd225250 11504 DOONE(hwbkpt);
83ea76b1 11505 DOONE(perf);
b20febf3 11506 DOONE(hrtimer);
ce82316f 11507 DOONE(procfs);
8d9609f5 11508 DOONE(netfilter);
935447c8
DS
11509
11510 // Another "order is important" item. We want to make sure we
11511 // "register" the dummy task_finder probe group after all probe
11512 // groups that use the task_finder.
11513 DOONE(utrace);
a96d1db0 11514 DOONE(itrace);
f31a77f5 11515 DOONE(dynprobe);
935447c8 11516 DOONE(task_finder);
b20febf3
FCE
11517#undef DOONE
11518 return g;
46b84a80 11519}
73267b89
JS
11520
11521/* vim: set sw=2 ts=8 cino=>4,n-2,{2,^-2,t0,(0,u0,w1,M1 : */
This page took 3.041371 seconds and 5 git commands to generate.