]> sourceware.org Git - systemtap.git/blame - tapsets.cxx
NEWS: added blurbs about PID-based process probes
[systemtap.git] / tapsets.cxx
CommitLineData
56e12059 1// tapset resolution
ef36f781 2// Copyright (C) 2005-2014 Red Hat Inc.
aa30ccd3 3// Copyright (C) 2005-2007 Intel Corporation.
0b8f6579 4// Copyright (C) 2008 James.Bottomley@HansenPartnership.com
56e12059
FCE
5//
6// This file is part of systemtap, and is free software. You can
7// redistribute it and/or modify it under the terms of the GNU General
8// Public License (GPL); either version 2, or (at your option) any
9// later version.
10
11#include "config.h"
12#include "staptree.h"
13#include "elaborate.h"
b55bc428 14#include "tapsets.h"
93646f4d 15#include "task_finder.h"
f31a77f5 16#include "tapset-dynprobe.h"
56e12059 17#include "translate.h"
dc38c0ae 18#include "session.h"
72dbc915 19#include "util.h"
0a6f5a3f 20#include "buildrun.h"
86bf665e 21#include "dwarf_wrappers.h"
2e67a43b 22#include "auto_free.h"
b278033a 23#include "hash.h"
440f755a 24#include "dwflpp.h"
5f8ca04f 25#include "setupdwfl.h"
6b51ee12 26#include <gelf.h>
22d737e8 27
4d0d9e8a 28#include "sdt_types.h"
bd2b1e68 29
3b579393
FCE
30#include <cstdlib>
31#include <algorithm>
bd2b1e68 32#include <deque>
56e12059 33#include <iostream>
d1bcbe71 34#include <fstream>
bd2b1e68 35#include <map>
ec4373ff 36#include <set>
56e12059 37#include <sstream>
bd2b1e68 38#include <stdexcept>
b55bc428 39#include <vector>
67959c62 40#include <stack>
e36387d7 41#include <cstdarg>
29e64872 42#include <cassert>
1969b5bc 43#include <iomanip>
f781f849 44#include <cerrno>
bd2b1e68
GH
45
46extern "C" {
df8fadee 47#include <fcntl.h>
bd2b1e68 48#include <elfutils/libdwfl.h>
7a053d3b 49#include <elfutils/libdw.h>
77de5e9e
GH
50#include <dwarf.h>
51#include <elf.h>
52#include <obstack.h>
b20febf3 53#include <glob.h>
30a279be 54#include <fnmatch.h>
5f0a03a6 55#include <stdio.h>
349dc70e 56#include <sys/types.h>
37001baa 57#include <sys/stat.h>
aaf7ffe8 58#include <math.h>
aff5d390 59#include <regex.h>
37001baa 60#include <unistd.h>
4b1ad75e
RM
61
62#define __STDC_FORMAT_MACROS
63#include <inttypes.h>
bd2b1e68 64}
77de5e9e 65
508059f0
MW
66// Old elf.h doesn't know about this machine type.
67#ifndef EM_AARCH64
68#define EM_AARCH64 183
69#endif
70
56e12059
FCE
71
72using namespace std;
2171f774 73using namespace __gnu_cxx;
56e12059 74
47dd066d 75
b20febf3
FCE
76
77// ------------------------------------------------------------------------
faea5e16
JS
78
79string
80common_probe_init (derived_probe* p)
81{
26e63673
JS
82 assert(p->session_index != (unsigned)-1);
83 return "(&stap_probes[" + lex_cast(p->session_index) + "])";
faea5e16
JS
84}
85
86
b20febf3 87void
71db462b
DS
88common_probe_entryfn_prologue (systemtap_session& s,
89 string statestr, string probe,
90 string probe_type, bool overload_processing)
b20febf3 91{
944c9a7a 92 if (s.runtime_usermode_p())
ac34f2aa
JS
93 {
94 // If session_state() is NULL, then we haven't even initialized shm yet,
95 // and there's *nothing* for the probe to do. (even alibi is in shm)
96 // So failure skips this whole block through the end of the epilogue.
97 s.op->newline() << "if (likely(session_state())) {";
e481ab2e 98 s.op->indent(1);
ac34f2aa
JS
99 }
100
71db462b 101 s.op->newline() << "#ifdef STP_ALIBI";
7c3e97f4 102 s.op->newline() << "atomic_inc(probe_alibi(" << probe << "->index));";
71db462b 103 s.op->newline() << "#else";
653e6a9a 104
e481ab2e
JS
105 if (s.runtime_usermode_p())
106 s.op->newline() << "int _stp_saved_errno = errno;";
107
fee09ad5 108 s.op->newline() << "struct context* __restrict__ c = NULL;";
71db462b
DS
109 s.op->newline() << "#if !INTERRUPTIBLE";
110 s.op->newline() << "unsigned long flags;";
111 s.op->newline() << "#endif";
b20febf3 112
f887a8c9 113 s.op->newline() << "#ifdef STP_TIMING";
7c3e97f4 114 s.op->newline() << "Stat stat = probe_timing(" << probe << "->index);";
f887a8c9
DS
115 s.op->newline() << "#endif";
116 if (overload_processing && !s.runtime_usermode_p())
71db462b 117 s.op->newline() << "#if defined(STP_TIMING) || defined(STP_OVERLOAD)";
a58d79d0 118 else
71db462b 119 s.op->newline() << "#ifdef STP_TIMING";
b20febf3 120
f887a8c9
DS
121 if (! s.runtime_usermode_p())
122 s.op->newline() << "cycles_t cycles_atstart = get_cycles ();";
123 else
124 {
125 s.op->newline() << "struct timespec timespec_atstart;";
45639d96 126 s.op->newline() << "(void)clock_gettime(CLOCK_MONOTONIC, &timespec_atstart);";
f887a8c9 127 }
71db462b 128 s.op->newline() << "#endif";
994aac0e 129
d2d39de6 130 s.op->newline() << "#if !INTERRUPTIBLE";
71db462b
DS
131 s.op->newline() << "local_irq_save (flags);";
132 s.op->newline() << "#endif";
b20febf3 133
ac3af990 134 if (! s.runtime_usermode_p())
71db462b
DS
135 {
136 // Check for enough free enough stack space
137 s.op->newline() << "if (unlikely ((((unsigned long) (& c)) & (THREAD_SIZE-1))"; // free space
138 s.op->newline(1) << "< (MINSTACKSPACE + sizeof (struct thread_info)))) {"; // needed space
139 // XXX: may need porting to platforms where task_struct is not
140 // at bottom of kernel stack NB: see also
141 // CONFIG_DEBUG_STACKOVERFLOW
065d5567 142 s.op->newline() << "atomic_inc (skipped_count());";
71db462b 143 s.op->newline() << "#ifdef STP_TIMING";
065d5567 144 s.op->newline() << "atomic_inc (skipped_count_lowstack());";
71db462b
DS
145 s.op->newline() << "#endif";
146 s.op->newline() << "goto probe_epilogue;";
147 s.op->newline(-1) << "}";
148 }
c931ec8a 149
065d5567 150 s.op->newline() << "if (atomic_read (session_state()) != " << statestr << ")";
71db462b
DS
151 s.op->newline(1) << "goto probe_epilogue;";
152 s.op->indent(-1);
9a604fac 153
1d0e697d 154 s.op->newline() << "c = _stp_runtime_entryfn_get_context();";
d2d39de6 155 s.op->newline() << "if (!c) {";
71db462b 156 s.op->newline(1) << "#if !INTERRUPTIBLE";
065d5567 157 s.op->newline() << "atomic_inc (skipped_count());";
71db462b
DS
158 s.op->newline() << "#endif";
159 s.op->newline() << "#ifdef STP_TIMING";
065d5567 160 s.op->newline() << "atomic_inc (skipped_count_reentrant());";
71db462b 161 s.op->newline() << "#endif";
71db462b
DS
162 s.op->newline() << "goto probe_epilogue;";
163 s.op->newline(-1) << "}";
d2d39de6 164
71db462b
DS
165 s.op->newline();
166 s.op->newline() << "c->last_stmt = 0;";
167 s.op->newline() << "c->last_error = 0;";
168 s.op->newline() << "c->nesting = -1;"; // NB: PR10516 packs locals[] tighter
169 s.op->newline() << "c->uregs = 0;";
170 s.op->newline() << "c->kregs = 0;";
171 s.op->newline() << "#if defined __ia64__";
172 s.op->newline() << "c->unwaddr = 0;";
173 s.op->newline() << "#endif";
f4d70a33
JS
174 if (s.runtime_usermode_p())
175 s.op->newline() << "c->probe_index = " << probe << "->index;";
71db462b
DS
176 s.op->newline() << "c->probe_point = " << probe << "->pp;";
177 s.op->newline() << "#ifdef STP_NEED_PROBE_NAME";
178 s.op->newline() << "c->probe_name = " << probe << "->pn;";
179 s.op->newline() << "#endif";
180 s.op->newline() << "c->probe_type = " << probe_type << ";";
6dceb5c9 181 // reset Individual Probe State union
71db462b 182 s.op->newline() << "memset(&c->ips, 0, sizeof(c->ips));";
e04b5d74 183 s.op->newline() << "c->user_mode_p = 0; c->full_uregs_p = 0;";
71db462b
DS
184 s.op->newline() << "#ifdef STAP_NEED_REGPARM"; // i386 or x86_64 register.stp
185 s.op->newline() << "c->regparm = 0;";
186 s.op->newline() << "#endif";
e0a17418 187
152fa051
LB
188 if(!s.suppress_time_limits){
189 s.op->newline() << "#if INTERRUPTIBLE";
190 s.op->newline() << "c->actionremaining = MAXACTION_INTERRUPTIBLE;";
191 s.op->newline() << "#else";
192 s.op->newline() << "c->actionremaining = MAXACTION;";
193 s.op->newline() << "#endif";
194 }
9915575b
FCE
195 // NB: The following would actually be incorrect.
196 // That's because cycles_sum/cycles_base values are supposed to survive
197 // between consecutive probes. Periodically (STP_OVERLOAD_INTERVAL
198 // cycles), the values will be reset.
199 /*
71db462b
DS
200 s.op->newline() << "#ifdef STP_OVERLOAD";
201 s.op->newline() << "c->cycles_sum = 0;";
202 s.op->newline() << "c->cycles_base = 0;";
203 s.op->newline() << "#endif";
9915575b 204 */
44cfbe25
SM
205
206 s.op->newline() << "#if defined(STP_NEED_UNWIND_DATA)";
207 s.op->newline() << "c->uwcache_user.state = uwcache_uninitialized;";
208 s.op->newline() << "c->uwcache_kernel.state = uwcache_uninitialized;";
209 s.op->newline() << "#endif";
b20febf3 210}
9a604fac 211
a44a0785 212
b20febf3 213void
f887a8c9
DS
214common_probe_entryfn_epilogue (systemtap_session& s,
215 bool overload_processing)
b20febf3 216{
f887a8c9
DS
217 if (overload_processing && !s.runtime_usermode_p())
218 s.op->newline() << "#if defined(STP_TIMING) || defined(STP_OVERLOAD)";
a58d79d0 219 else
f887a8c9
DS
220 s.op->newline() << "#ifdef STP_TIMING";
221 s.op->newline() << "{";
222 s.op->indent(1);
223 if (! s.runtime_usermode_p())
224 {
225 s.op->newline() << "cycles_t cycles_atend = get_cycles ();";
226 // NB: we truncate cycles counts to 32 bits. Perhaps it should be
227 // fewer, if the hardware counter rolls over really quickly. We
228 // handle 32-bit wraparound here.
229 s.op->newline() << "int32_t cycles_elapsed = ((int32_t)cycles_atend > (int32_t)cycles_atstart)";
230 s.op->newline(1) << "? ((int32_t)cycles_atend - (int32_t)cycles_atstart)";
231 s.op->newline() << ": (~(int32_t)0) - (int32_t)cycles_atstart + (int32_t)cycles_atend + 1;";
232 s.op->indent(-1);
233 }
234 else
235 {
236 s.op->newline() << "struct timespec timespec_atend, timespec_elapsed;";
237 s.op->newline() << "long cycles_elapsed;";
45639d96 238 s.op->newline() << "(void)clock_gettime(CLOCK_MONOTONIC, &timespec_atend);";
f887a8c9
DS
239 s.op->newline() << "_stp_timespec_sub(&timespec_atend, &timespec_atstart, &timespec_elapsed);";
240 // 'cycles_elapsed' is really elapsed nanoseconds
241 s.op->newline() << "cycles_elapsed = (timespec_elapsed.tv_sec * NSEC_PER_SEC) + timespec_elapsed.tv_nsec;";
242 }
243
244 s.op->newline() << "#ifdef STP_TIMING";
245 s.op->newline() << "if (likely (stat)) _stp_stat_add(stat, cycles_elapsed);";
246 s.op->newline() << "#endif";
247
248 if (overload_processing && !s.runtime_usermode_p())
249 {
250 s.op->newline() << "#ifdef STP_OVERLOAD";
251 s.op->newline() << "{";
a58d79d0
DS
252 // If the cycle count has wrapped (cycles_atend > cycles_base),
253 // let's go ahead and pretend the interval has been reached.
254 // This should reset cycles_base and cycles_sum.
f887a8c9
DS
255 s.op->newline(1) << "cycles_t interval = (cycles_atend > c->cycles_base)";
256 s.op->newline(1) << "? (cycles_atend - c->cycles_base)";
257 s.op->newline() << ": (STP_OVERLOAD_INTERVAL + 1);";
258 s.op->newline(-1) << "c->cycles_sum += cycles_elapsed;";
a58d79d0
DS
259
260 // If we've spent more than STP_OVERLOAD_THRESHOLD cycles in a
261 // probe during the last STP_OVERLOAD_INTERVAL cycles, the probe
262 // has overloaded the system and we need to quit.
7baf48e9
FCE
263 // NB: this is not suppressible via --suppress-runtime-errors,
264 // because this is a system safety metric that we cannot trust
265 // unprivileged users to override.
f887a8c9
DS
266 s.op->newline() << "if (interval > STP_OVERLOAD_INTERVAL) {";
267 s.op->newline(1) << "if (c->cycles_sum > STP_OVERLOAD_THRESHOLD) {";
268 s.op->newline(1) << "_stp_error (\"probe overhead exceeded threshold\");";
065d5567
JS
269 s.op->newline() << "atomic_set (session_state(), STAP_SESSION_ERROR);";
270 s.op->newline() << "atomic_inc (error_count());";
f887a8c9
DS
271 s.op->newline(-1) << "}";
272
273 s.op->newline() << "c->cycles_base = cycles_atend;";
274 s.op->newline() << "c->cycles_sum = 0;";
275 s.op->newline(-1) << "}";
276 s.op->newline(-1) << "}";
277 s.op->newline() << "#endif";
a58d79d0 278 }
e57b735a 279
f887a8c9
DS
280 s.op->newline(-1) << "}";
281 s.op->newline() << "#endif";
e57b735a 282
f887a8c9
DS
283 s.op->newline() << "c->probe_point = 0;"; // vacated
284 s.op->newline() << "#ifdef STP_NEED_PROBE_NAME";
285 s.op->newline() << "c->probe_name = 0;";
286 s.op->newline() << "#endif";
287 s.op->newline() << "c->probe_type = 0;";
7baf48e9
FCE
288
289
f887a8c9
DS
290 s.op->newline() << "if (unlikely (c->last_error && c->last_error[0])) {";
291 s.op->indent(1);
292 if (s.suppress_handler_errors) // PR 13306
b78a0fbb 293 {
065d5567 294 s.op->newline() << "atomic_inc (error_count());";
7baf48e9
FCE
295 }
296 else
297 {
f887a8c9
DS
298 s.op->newline() << "if (c->last_stmt != NULL)";
299 s.op->newline(1) << "_stp_softerror (\"%s near %s\", c->last_error, c->last_stmt);";
300 s.op->newline(-1) << "else";
301 s.op->newline(1) << "_stp_softerror (\"%s\", c->last_error);";
302 s.op->indent(-1);
065d5567
JS
303 s.op->newline() << "atomic_inc (error_count());";
304 s.op->newline() << "if (atomic_read (error_count()) > MAXERRORS) {";
305 s.op->newline(1) << "atomic_set (session_state(), STAP_SESSION_ERROR);";
f887a8c9
DS
306 s.op->newline() << "_stp_exit ();";
307 s.op->newline(-1) << "}";
7baf48e9
FCE
308 }
309
f887a8c9 310 s.op->newline(-1) << "}";
7baf48e9
FCE
311
312
f887a8c9
DS
313 s.op->newline(-1) << "probe_epilogue:"; // context is free
314 s.op->indent(1);
e57b735a 315
f887a8c9 316 if (! s.suppress_handler_errors) // PR 13306
7baf48e9
FCE
317 {
318 // Check for excessive skip counts.
065d5567
JS
319 s.op->newline() << "if (unlikely (atomic_read (skipped_count()) > MAXSKIPPED)) {";
320 s.op->newline(1) << "if (unlikely (pseudo_atomic_cmpxchg(session_state(), STAP_SESSION_RUNNING, STAP_SESSION_ERROR) == STAP_SESSION_RUNNING))";
f887a8c9
DS
321 s.op->newline() << "_stp_error (\"Skipped too many probes, check MAXSKIPPED or try again with stap -t for more details.\");";
322 s.op->newline(-1) << "}";
7baf48e9 323 }
e57b735a 324
d2d39de6
JS
325 // We mustn't release the context until after all _stp_error(), so dyninst
326 // mode can still access the log buffers stored therein.
327 s.op->newline() << "_stp_runtime_entryfn_put_context(c);";
328
329 s.op->newline() << "#if !INTERRUPTIBLE";
f887a8c9
DS
330 s.op->newline() << "local_irq_restore (flags);";
331 s.op->newline() << "#endif";
653e6a9a 332
944c9a7a 333 if (s.runtime_usermode_p())
ac34f2aa
JS
334 {
335 s.op->newline() << "errno = _stp_saved_errno;";
ac34f2aa 336 }
e481ab2e
JS
337
338 s.op->newline() << "#endif // STP_ALIBI";
339
340 if (s.runtime_usermode_p())
341 s.op->newline(-1) << "}";
440f755a 342}
e57b735a 343
e57b735a 344
440f755a 345// ------------------------------------------------------------------------
e57b735a 346
440f755a
JS
347// ------------------------------------------------------------------------
348// Dwarf derived probes. "We apologize for the inconvience."
349// ------------------------------------------------------------------------
e57b735a 350
4627ed58
JS
351static const string TOK_KERNEL("kernel");
352static const string TOK_MODULE("module");
353static const string TOK_FUNCTION("function");
354static const string TOK_INLINE("inline");
355static const string TOK_CALL("call");
4bda987e 356static const string TOK_EXPORTED("exported");
4627ed58
JS
357static const string TOK_RETURN("return");
358static const string TOK_MAXACTIVE("maxactive");
359static const string TOK_STATEMENT("statement");
360static const string TOK_ABSOLUTE("absolute");
361static const string TOK_PROCESS("process");
a794dbeb 362static const string TOK_PROVIDER("provider");
4627ed58
JS
363static const string TOK_MARK("mark");
364static const string TOK_TRACE("trace");
365static const string TOK_LABEL("label");
63b4fd14 366static const string TOK_LIBRARY("library");
576eaefe 367static const string TOK_PLT("plt");
e7d4410d
LB
368static const string TOK_METHOD("method");
369static const string TOK_CLASS("class");;
c31add50
JL
370static const string TOK_CALLEE("callee");;
371static const string TOK_CALLEES("callees");;
e57b735a 372
440f755a
JS
373// Can we handle this query with just symbol-table info?
374enum dbinfo_reqt
375{
376 dbr_unknown,
377 dbr_none, // kernel.statement(NUM).absolute
378 dbr_need_symtab, // can get by with symbol table if there's no dwarf
379 dbr_need_dwarf
380};
e57b735a 381
20e4a32c 382
673d0add 383struct dwarf_query; // forward decl
20e4a32c 384
45582cbc
JL
385static int query_cu (Dwarf_Die * cudie, dwarf_query *q);
386static void query_addr(Dwarf_Addr addr, dwarf_query *q);
387static void query_plt_statement(dwarf_query *q);
a781f401 388
440f755a
JS
389struct
390symbol_table
391{
392 module_info *mod_info; // associated module
393 map<string, func_info*> map_by_name;
1c6b77e5 394 multimap<Dwarf_Addr, func_info*> map_by_addr;
2a4acb09
JL
395 map<string, Dwarf_Addr> globals;
396 map<string, Dwarf_Addr> locals;
1c6b77e5 397 typedef multimap<Dwarf_Addr, func_info*>::iterator iterator_t;
440f755a
JS
398 typedef pair<iterator_t, iterator_t> range_t;
399#ifdef __powerpc__
400 GElf_Word opd_section;
401#endif
2867a2a1
JS
402 void add_symbol(const char *name, bool weak, bool descriptor,
403 Dwarf_Addr addr, Dwarf_Addr *high_addr);
440f755a
JS
404 enum info_status get_from_elf();
405 void prepare_section_rejection(Dwfl_Module *mod);
406 bool reject_section(GElf_Word section);
440f755a
JS
407 void purge_syscall_stubs();
408 func_info *lookup_symbol(const string& name);
409 Dwarf_Addr lookup_symbol_address(const string& name);
410 func_info *get_func_containing_address(Dwarf_Addr addr);
3d372d6b 411 func_info *get_first_func();
7a053d3b 412
440f755a
JS
413 symbol_table(module_info *mi) : mod_info(mi) {}
414 ~symbol_table();
415};
77de5e9e 416
440f755a
JS
417static bool null_die(Dwarf_Die *die)
418{
822a6a3d 419 static Dwarf_Die null;
440f755a
JS
420 return (!die || !memcmp(die, &null, sizeof(null)));
421}
c4ce66a1
JS
422
423
7a053d3b 424enum
bd2b1e68 425function_spec_type
7a053d3b 426 {
bd2b1e68
GH
427 function_alone,
428 function_and_file,
7a053d3b 429 function_file_and_line
bd2b1e68
GH
430 };
431
ec4373ff 432
bd2b1e68 433struct dwarf_builder;
f10534c6 434struct dwarf_var_expanding_visitor;
77de5e9e 435
2930abc7 436
b20febf3
FCE
437// XXX: This class is a candidate for subclassing to separate
438// the relocation vs non-relocation variants. Likewise for
439// kprobe vs kretprobe variants.
440
441struct dwarf_derived_probe: public derived_probe
b55bc428 442{
b20febf3
FCE
443 dwarf_derived_probe (const string& function,
444 const string& filename,
445 int line,
446 const string& module,
447 const string& section,
448 Dwarf_Addr dwfl_addr,
2930abc7 449 Dwarf_Addr addr,
b20febf3
FCE
450 dwarf_query & q,
451 Dwarf_Die* scope_die);
20e4a32c 452
b20febf3
FCE
453 string module;
454 string section;
455 Dwarf_Addr addr;
63b4fd14 456 string path;
27dc09b1 457 bool has_process;
2930abc7 458 bool has_return;
c9bad430 459 bool has_maxactive;
63b4fd14 460 bool has_library;
c9bad430 461 long maxactive_val;
4ad95bbc 462 // dwarf_derived_probe_group::emit_module_decls uses this to emit sdt kprobe definition
b642c901
SC
463 string user_path;
464 string user_lib;
b95e2b79 465 bool access_vars;
2930abc7 466
af234c40
JS
467 unsigned saved_longs, saved_strings;
468 dwarf_derived_probe* entry_handler;
469
b8da0ad1 470 void printsig (std::ostream &o) const;
6b66b9f7 471 virtual void join_group (systemtap_session& s);
3689db05 472 void emit_probe_local_init(systemtap_session& s, translator_output * o);
d0bfd2ac 473 void getargs(std::list<std::string> &arg_set) const;
0a98fd42 474
42e38653 475 void emit_privilege_assertion (translator_output*);
27dc09b1
DB
476 void print_dupe_stamp(ostream& o);
477
bd2b1e68 478 // Pattern registration helpers.
7a053d3b 479 static void register_statement_variants(match_node * root,
27dc09b1 480 dwarf_builder * dw,
42e38653 481 privilege_t privilege);
fd6602a0 482 static void register_function_variants(match_node * root,
27dc09b1 483 dwarf_builder * dw,
42e38653 484 privilege_t privilege);
440d9b00
DB
485 static void register_function_and_statement_variants(systemtap_session& s,
486 match_node * root,
27dc09b1 487 dwarf_builder * dw,
42e38653 488 privilege_t privilege);
b1615c74
JS
489 static void register_sdt_variants(systemtap_session& s,
490 match_node * root,
491 dwarf_builder * dw);
492 static void register_plt_variants(systemtap_session& s,
493 match_node * root,
494 dwarf_builder * dw);
c4ce66a1 495 static void register_patterns(systemtap_session& s);
6b66b9f7
JS
496
497protected:
498 dwarf_derived_probe(probe *base,
499 probe_point *location,
500 Dwarf_Addr addr,
501 bool has_return):
74fe61bc
LB
502 derived_probe(base, location), addr(addr), has_process(0),
503 has_return(has_return), has_maxactive(0), has_library(0),
504 maxactive_val(0), access_vars(false), saved_longs(0),
505 saved_strings(0), entry_handler(0)
6b66b9f7
JS
506 {}
507
508private:
d0bfd2ac 509 list<string> args;
8c67c337 510 void saveargs(dwarf_query& q, Dwarf_Die* scope_die, Dwarf_Addr dwfl_addr);
20c6c071
GH
511};
512
513// Helper struct to thread through the dwfl callbacks.
2c384610 514struct base_query
20c6c071 515{
c4ce66a1
JS
516 base_query(dwflpp & dw, literal_map_t const & params);
517 base_query(dwflpp & dw, const string & module_val);
2c384610 518 virtual ~base_query() {}
bd2b1e68 519
5227f1ea 520 systemtap_session & sess;
2c384610 521 dwflpp & dw;
5227f1ea 522
070764c0
JL
523 // Used to keep track of which modules were visited during
524 // iterate_over_modules()
525 set<string> visited_modules;
526
bd2b1e68 527 // Parameter extractors.
86bf665e 528 static bool has_null_param(literal_map_t const & params,
888af770 529 string const & k);
86bf665e 530 static bool get_string_param(literal_map_t const & params,
bd2b1e68 531 string const & k, string & v);
86bf665e 532 static bool get_number_param(literal_map_t const & params,
bd2b1e68 533 string const & k, long & v);
86bf665e 534 static bool get_number_param(literal_map_t const & params,
c239d28c 535 string const & k, Dwarf_Addr & v);
bbbc7241 536 static void query_library_callback (base_query *me, const char *data);
45cdb40e 537 static void query_plt_callback (base_query *me, const char *link, size_t addr);
f301a9ff 538 virtual void query_library (const char *data) = 0;
576eaefe 539 virtual void query_plt (const char *link, size_t addr) = 0;
84c84ac4 540
b55bc428 541
2c384610
DS
542 // Extracted parameters.
543 bool has_kernel;
91af0778
FCE
544 bool has_module;
545 bool has_process;
63b4fd14 546 bool has_library;
576eaefe
SC
547 bool has_plt;
548 bool has_statement;
2c384610 549 string module_val; // has_kernel => module_val = "kernel"
63b4fd14 550 string path; // executable path if module is a .so
576eaefe 551 string plt_val; // has_plt => plt wildcard
c88bcbe8 552 int64_t pid_val;
2c384610
DS
553
554 virtual void handle_query_module() = 0;
555};
556
2c384610
DS
557struct dwarf_query : public base_query
558{
e1278bd4 559 dwarf_query(probe * base_probe,
2c384610
DS
560 probe_point * base_loc,
561 dwflpp & dw,
86bf665e 562 literal_map_t const & params,
b642c901
SC
563 vector<derived_probe *> & results,
564 const string user_path,
565 const string user_lib);
2c384610 566
c4ce66a1 567 vector<derived_probe *> & results;
8f14e444 568 set<string> inlined_non_returnable; // function names
c4ce66a1
JS
569 probe * base_probe;
570 probe_point * base_loc;
b642c901
SC
571 string user_path;
572 string user_lib;
c4ce66a1 573
614f0fcf
JL
574 set<string> visited_libraries;
575 bool resolved_library;
576
2c384610 577 virtual void handle_query_module();
5f0a03a6
JK
578 void query_module_dwarf();
579 void query_module_symtab();
5d5bd369 580 void query_library (const char *data);
576eaefe 581 void query_plt (const char *entry, size_t addr);
2c384610 582
2930abc7
FCE
583 void add_probe_point(string const & funcname,
584 char const * filename,
585 int line,
586 Dwarf_Die *scope_die,
587 Dwarf_Addr addr);
36f9dd1d 588
613c8675
JL
589 void mount_well_formed_probe_point();
590 void unmount_well_formed_probe_point();
591 stack<pair<probe_point*, probe*> > previous_bases;
592
71dfce11
JL
593 void replace_probe_point_component_arg(const string& functor,
594 const string& new_functor,
595 int64_t new_arg,
596 bool hex = false);
597 void replace_probe_point_component_arg(const string& functor,
598 int64_t new_arg,
599 bool hex = false);
600 void replace_probe_point_component_arg(const string& functor,
601 const string& new_functor,
602 const string& new_arg);
603 void replace_probe_point_component_arg(const string& functor,
604 const string& new_arg);
605 void remove_probe_point_component(const string& functor);
606
857bdfd1
JS
607 // Track addresses we've already seen in a given module
608 set<Dwarf_Addr> alias_dupes;
609
7fdd3e2c
JS
610 // Track inlines we've already seen as well
611 // NB: this can't be compared just by entrypc, as inlines can overlap
612 set<inline_instance_info> inline_dupes;
613
e2941743
JL
614 // Used in .callee[s] probes, when calling iterate_over_callees() (which
615 // provides the actual stack). Retains the addrs of the callers unwind addr
616 // where the callee is found. Specifies multiple callers. E.g. when a callee
617 // at depth 2 is found, callers[1] has the addr of the caller, and callers[0]
618 // has the addr of the caller's caller.
619 stack<Dwarf_Addr> *callers;
620
20c6c071
GH
621 bool has_function_str;
622 bool has_statement_str;
623 bool has_function_num;
624 bool has_statement_num;
7a053d3b
RM
625 string statement_str_val;
626 string function_str_val;
c239d28c
GH
627 Dwarf_Addr statement_num_val;
628 Dwarf_Addr function_num_val;
20c6c071 629
b8da0ad1 630 bool has_call;
4bda987e 631 bool has_exported;
b8da0ad1 632 bool has_inline;
20c6c071
GH
633 bool has_return;
634
c9bad430
DS
635 bool has_maxactive;
636 long maxactive_val;
637
20c6c071
GH
638 bool has_label;
639 string label_val;
640
c31add50
JL
641 bool has_callee;
642 string callee_val;
643
644 bool has_callees_num;
645 long callees_num_val;
646
20c6c071
GH
647 bool has_relative;
648 long relative_val;
649
37ebca01
FCE
650 bool has_absolute;
651
467bea43
SC
652 bool has_mark;
653
5f0a03a6
JK
654 enum dbinfo_reqt dbinfo_reqt;
655 enum dbinfo_reqt assess_dbinfo_reqt();
656
7d6d0afc 657 void parse_function_spec(const string & spec);
20c6c071 658 function_spec_type spec_type;
7d6d0afc 659 vector<string> scopes;
20c6c071
GH
660 string function;
661 string file;
4a81c0d9 662 lineno_t lineno_type;
f01d4ffb 663 vector<int> linenos;
5f0a03a6 664 bool query_done; // Found exact match
20c6c071 665
5a617dc6
JL
666 // Holds the prologue end of the current function
667 Dwarf_Addr prologue_end;
668
bd25380d 669 set<string> filtered_srcfiles;
7e1279ea
FCE
670
671 // Map official entrypc -> func_info object
86bf665e
TM
672 inline_instance_map_t filtered_inlines;
673 func_info_map_t filtered_functions;
4df79aaf 674
959ccfed
JL
675 // Helper when we want to iterate over both
676 base_func_info_map_t filtered_all();
677
4df79aaf 678 void query_module_functions ();
e772a6e7
JL
679
680 string final_function_name(const string& final_func,
79954c0f 681 const char* final_file,
e772a6e7 682 int final_line);
b55bc428
FCE
683};
684
37635b45
AJ
685struct uprobe_derived_probe: public dwarf_derived_probe
686{
687 int pid; // 0 => unrestricted
688
689 uprobe_derived_probe (const string& function,
690 const string& filename,
691 int line,
692 const string& module,
693 const string& section,
694 Dwarf_Addr dwfl_addr,
695 Dwarf_Addr addr,
696 dwarf_query & q,
697 Dwarf_Die* scope_die):
698 dwarf_derived_probe(function, filename, line, module, section,
c88bcbe8 699 dwfl_addr, addr, q, scope_die), pid(q.pid_val)
37635b45
AJ
700 {}
701
702 // alternate constructor for process(PID).statement(ADDR).absolute
703 uprobe_derived_probe (probe *base,
704 probe_point *location,
705 int pid,
706 Dwarf_Addr addr,
707 bool has_return):
708 dwarf_derived_probe(base, location, addr, has_return), pid(pid)
709 {}
710
711 void join_group (systemtap_session& s);
712
713 void emit_privilege_assertion (translator_output*);
714 void print_dupe_stamp(ostream& o) { print_dupe_stamp_unprivileged_process_owner (o); }
715 void getargs(std::list<std::string> &arg_set) const;
716 void saveargs(int nargs);
717private:
718 list<string> args;
719};
720
721struct dwarf_derived_probe_group: public derived_probe_group
722{
723private:
724 multimap<string,dwarf_derived_probe*> probes_by_module;
725 typedef multimap<string,dwarf_derived_probe*>::iterator p_b_m_iterator;
726
727public:
728 dwarf_derived_probe_group() {}
729 void enroll (dwarf_derived_probe* probe);
730 void emit_module_decls (systemtap_session& s);
731 void emit_module_init (systemtap_session& s);
732 void emit_module_refresh (systemtap_session& s);
733 void emit_module_exit (systemtap_session& s);
734};
735
736base_query::base_query(dwflpp & dw, literal_map_t const & params):
c88bcbe8
AJ
737 sess(dw.sess), dw(dw), has_library(false), has_plt(false), has_statement(false),
738 pid_val(0)
37635b45
AJ
739{
740 has_kernel = has_null_param (params, TOK_KERNEL);
741 if (has_kernel)
742 module_val = "kernel";
743
744 has_module = get_string_param (params, TOK_MODULE, module_val);
745 if (has_module)
746 has_process = false;
747 else
748 {
749 string library_name;
750 long statement_num_val;
751 has_process = derived_probe_builder::has_param(params, TOK_PROCESS);
752 has_library = get_string_param (params, TOK_LIBRARY, library_name);
753 if ((has_plt = has_null_param (params, TOK_PLT)))
754 plt_val = "*";
755 else has_plt = get_string_param (params, TOK_PLT, plt_val);
756 has_statement = get_number_param(params, TOK_STATEMENT, statement_num_val);
757
758 if (has_process)
759 {
c88bcbe8 760 if (get_number_param(params, TOK_PROCESS, pid_val))
37635b45
AJ
761 {
762 // check that the pid given corresponds to a running process
c88bcbe8 763 if (pid_val < 1 || kill(pid_val, 0) == -1)
37635b45
AJ
764 switch (errno) // ignore EINVAL: invalid signal
765 {
766 case ESRCH:
767 throw SEMANTIC_ERROR(_("pid given does not correspond to a running process"));
768 case EPERM:
769 throw SEMANTIC_ERROR(_("invalid permissions for signalling given pid"));
770 default:
771 throw SEMANTIC_ERROR(_("invalid pid"));
772 }
c88bcbe8 773 string pid_path = string("/proc/") + lex_cast(pid_val) + "/exe";
37635b45
AJ
774 module_val = sess.sysroot + pid_path;
775 }
c88bcbe8
AJ
776 else
777 {
778 // reset the pid_val in case anything weird got written into it
779 pid_val = 0;
37635b45 780 get_string_param(params, TOK_PROCESS, module_val);
c88bcbe8 781 }
37635b45
AJ
782 module_val = find_executable (module_val, sess.sysroot, sess.sysenv);
783 if (!is_fully_resolved(module_val, sess.sysroot, sess.sysenv))
784 throw SEMANTIC_ERROR(_F("cannot find executable '%s'",
785 module_val.c_str()));
786 }
787
788 // Library probe? Let's target that instead if it is fully resolved (such
789 // as what query_one_library() would have done for us). Otherwise, we
790 // resort to iterate_over_libraries().
791 if (has_library && is_fully_resolved(library_name, sess.sysroot, sess.sysenv,
792 "LD_LIBRARY_PATH"))
793 {
794 path = path_remove_sysroot(sess, module_val);
795 module_val = library_name;
796 }
797 }
798
799 assert (has_kernel || has_process || has_module);
800}
801
802base_query::base_query(dwflpp & dw, const string & module_val)
803 : sess(dw.sess), dw(dw), has_library(false), has_plt(false), has_statement(false),
c88bcbe8 804 module_val(module_val), pid_val(0)
37635b45
AJ
805{
806 // NB: This uses '/' to distinguish between kernel modules and userspace,
807 // which means that userspace modules won't get any PATH searching.
808 if (module_val.find('/') == string::npos)
809 {
810 has_kernel = (module_val == TOK_KERNEL);
811 has_module = !has_kernel;
812 has_process = false;
813 }
814 else
815 {
816 has_kernel = has_module = false;
817 has_process = true;
818 }
819}
820
821bool
822base_query::has_null_param(literal_map_t const & params,
823 string const & k)
824{
825 return derived_probe_builder::has_null_param(params, k);
826}
827
828
829bool
830base_query::get_string_param(literal_map_t const & params,
831 string const & k, string & v)
832{
833 return derived_probe_builder::get_param (params, k, v);
834}
835
836
837bool
838base_query::get_number_param(literal_map_t const & params,
839 string const & k, long & v)
840{
841 int64_t value;
842 bool present = derived_probe_builder::get_param (params, k, value);
843 v = (long) value;
844 return present;
845}
846
847
848bool
849base_query::get_number_param(literal_map_t const & params,
850 string const & k, Dwarf_Addr & v)
851{
852 int64_t value;
853 bool present = derived_probe_builder::get_param (params, k, value);
854 v = (Dwarf_Addr) value;
855 return present;
856}
98afd80e 857
435f53a7
FCE
858static void delete_session_module_cache (systemtap_session& s); // forward decl
859
860
98afd80e 861struct dwarf_builder: public derived_probe_builder
b55bc428 862{
665e1256 863 map <string,dwflpp*> kern_dw; /* NB: key string could be a wildcard */
7a24d422 864 map <string,dwflpp*> user_dw;
b642c901
SC
865 string user_path;
866 string user_lib;
44ffe90c
JL
867
868 // Holds modules to suggest functions from. NB: aggregates over
869 // recursive calls to build() when deriving globby probes.
870 set <string> modules_seen;
871
ae2552da 872 dwarf_builder() {}
aa30ccd3 873
ae2552da 874 dwflpp *get_kern_dw(systemtap_session& sess, const string& module)
707bf35e 875 {
ea14cf67
FCE
876 if (kern_dw[module] == 0)
877 kern_dw[module] = new dwflpp(sess, module, true); // might throw
ae2552da 878 return kern_dw[module];
707bf35e
JS
879 }
880
881 dwflpp *get_user_dw(systemtap_session& sess, const string& module)
882 {
ea14cf67
FCE
883 if (user_dw[module] == 0)
884 user_dw[module] = new dwflpp(sess, module, false); // might throw
707bf35e
JS
885 return user_dw[module];
886 }
7a24d422
FCE
887
888 /* NB: not virtual, so can be called from dtor too: */
822a6a3d 889 void dwarf_build_no_more (bool)
aa30ccd3 890 {
435f53a7
FCE
891 delete_map(kern_dw);
892 delete_map(user_dw);
7a24d422
FCE
893 }
894
895 void build_no_more (systemtap_session &s)
896 {
897 dwarf_build_no_more (s.verbose > 3);
435f53a7 898 delete_session_module_cache (s);
aa30ccd3
FCE
899 }
900
e38d6504
RM
901 ~dwarf_builder()
902 {
7a24d422 903 dwarf_build_no_more (false);
c8959a29 904 }
aa30ccd3 905
5227f1ea 906 virtual void build(systemtap_session & sess,
7a053d3b 907 probe * base,
20c6c071 908 probe_point * location,
86bf665e 909 literal_map_t const & parameters,
20c6c071 910 vector<derived_probe *> & finished_results);
b55bc428
FCE
911};
912
5111fc3e 913
e1278bd4 914dwarf_query::dwarf_query(probe * base_probe,
20c6c071
GH
915 probe_point * base_loc,
916 dwflpp & dw,
86bf665e 917 literal_map_t const & params,
b642c901
SC
918 vector<derived_probe *> & results,
919 const string user_path,
920 const string user_lib)
e2941743
JL
921 : base_query(dw, params), results(results), base_probe(base_probe),
922 base_loc(base_loc), user_path(user_path), user_lib(user_lib),
614f0fcf
JL
923 resolved_library(false), callers(NULL), has_relative(false),
924 relative_val(0), prologue_end(0)
bd2b1e68
GH
925{
926 // Reduce the query to more reasonable semantic values (booleans,
927 // extracted strings, numbers, etc).
bd2b1e68
GH
928 has_function_str = get_string_param(params, TOK_FUNCTION, function_str_val);
929 has_function_num = get_number_param(params, TOK_FUNCTION, function_num_val);
930
931 has_statement_str = get_string_param(params, TOK_STATEMENT, statement_str_val);
932 has_statement_num = get_number_param(params, TOK_STATEMENT, statement_num_val);
933
0f336e95 934 has_label = get_string_param(params, TOK_LABEL, label_val);
c31add50
JL
935 has_callee = get_string_param(params, TOK_CALLEE, callee_val);
936 if (has_null_param(params, TOK_CALLEES))
937 { // .callees ==> .callees(1) (also equivalent to .callee("*"))
938 has_callees_num = true;
939 callees_num_val = 1;
940 }
941 else
942 {
943 has_callees_num = get_number_param(params, TOK_CALLEES, callees_num_val);
944 if (has_callees_num && callees_num_val < 1)
945 throw SEMANTIC_ERROR(_(".callees(N) only acceptable for N >= 1"),
946 base_probe->tok);
947 }
0f336e95 948
b8da0ad1 949 has_call = has_null_param(params, TOK_CALL);
4bda987e 950 has_exported = has_null_param(params, TOK_EXPORTED);
b8da0ad1 951 has_inline = has_null_param(params, TOK_INLINE);
bd2b1e68 952 has_return = has_null_param(params, TOK_RETURN);
c9bad430 953 has_maxactive = get_number_param(params, TOK_MAXACTIVE, maxactive_val);
37ebca01 954 has_absolute = has_null_param(params, TOK_ABSOLUTE);
467bea43 955 has_mark = false;
37ebca01 956
bd2b1e68 957 if (has_function_str)
7d6d0afc 958 parse_function_spec(function_str_val);
bd2b1e68 959 else if (has_statement_str)
7d6d0afc 960 parse_function_spec(statement_str_val);
0daad364 961
5f0a03a6
JK
962 dbinfo_reqt = assess_dbinfo_reqt();
963 query_done = false;
0daad364
JS
964}
965
966
2c384610 967void
5f0a03a6 968dwarf_query::query_module_dwarf()
2c384610
DS
969{
970 if (has_function_num || has_statement_num)
971 {
972 // If we have module("foo").function(0xbeef) or
973 // module("foo").statement(0xbeef), the address is relative
974 // to the start of the module, so we seek the function
975 // number plus the module's bias.
6b517475
JS
976 Dwarf_Addr addr = has_function_num ?
977 function_num_val : statement_num_val;
08d1d520
MW
978
979 // These are raw addresses, we need to know what the elf_bias
980 // is to feed it to libdwfl based functions.
981 Dwarf_Addr elf_bias;
982 Elf *elf = dwfl_module_getelf (dw.module, &elf_bias);
983 assert(elf);
984 addr += elf_bias;
6b517475 985 query_addr(addr, this);
2c384610
DS
986 }
987 else
988 {
989 // Otherwise if we have a function("foo") or statement("foo")
990 // specifier, we have to scan over all the CUs looking for
991 // the function(s) in question
992 assert(has_function_str || has_statement_str);
4df79aaf
JS
993
994 // For simple cases, no wildcard and no source:line, we can do a very
995 // quick function lookup in a module-wide cache.
1ffb8bd1
JS
996 if (spec_type == function_alone &&
997 !dw.name_has_wildcard(function) &&
998 !startswith(function, "_Z"))
4df79aaf
JS
999 query_module_functions();
1000 else
337b7c44 1001 dw.iterate_over_cus(&query_cu, this, false);
2c384610
DS
1002 }
1003}
1004
5f0a03a6
JK
1005static void query_func_info (Dwarf_Addr entrypc, func_info & fi,
1006 dwarf_query * q);
1007
91bb9081
LB
1008static void
1009query_symtab_func_info (func_info & fi, dwarf_query * q)
1010{
1011 assert(null_die(&fi.die));
1012
1013 Dwarf_Addr addr = fi.addr;
1014
1015 // Now compensate for the dw bias because the addresses come
1016 // from dwfl_module_symtab, so fi->addr is NOT a normal dw address.
1017 q->dw.get_module_dwarf(false, false);
1018 addr -= q->dw.module_bias;
1019
1020 // If there are already probes in this module, lets not duplicate.
1021 // This can come from other weak symbols/aliases or existing
a41f2ad7
JS
1022 // matches from Dwarf DIE functions. Try to add this addr to the
1023 // collection, and only continue if it was new.
1024 if (q->alias_dupes.insert(addr).second)
1025 query_func_info(addr, fi, q);
91bb9081
LB
1026}
1027
5f0a03a6
JK
1028void
1029dwarf_query::query_module_symtab()
1030{
1031 // Get the symbol table if it's necessary, sufficient, and not already got.
1032 if (dbinfo_reqt == dbr_need_dwarf)
1033 return;
1034
1035 module_info *mi = dw.mod_info;
1036 if (dbinfo_reqt == dbr_need_symtab)
1037 {
1038 if (mi->symtab_status == info_unknown)
5f52fafe 1039 mi->get_symtab();
5f0a03a6
JK
1040 if (mi->symtab_status == info_absent)
1041 return;
1042 }
1043
1044 func_info *fi = NULL;
1045 symbol_table *sym_table = mi->sym_table;
1046
1047 if (has_function_str)
1048 {
1049 // Per dwarf_query::assess_dbinfo_reqt()...
1050 assert(spec_type == function_alone);
1051 if (dw.name_has_wildcard(function_str_val))
1052 {
2e67a43b 1053 symbol_table::iterator_t iter;
1c6b77e5
JS
1054 for (iter = sym_table->map_by_addr.begin();
1055 iter != sym_table->map_by_addr.end();
2e67a43b 1056 ++iter)
5f0a03a6 1057 {
1c6b77e5 1058 fi = iter->second;
5f0a03a6
JK
1059 if (!null_die(&fi->die))
1060 continue; // already handled in query_module_dwarf()
1061 if (dw.function_name_matches_pattern(fi->name, function_str_val))
91bb9081 1062 query_symtab_func_info(*fi, this);
5f0a03a6
JK
1063 }
1064 }
1065 else
1066 {
1067 fi = sym_table->lookup_symbol(function_str_val);
2867a2a1 1068 if (fi && !fi->descriptor && null_die(&fi->die))
91bb9081 1069 query_symtab_func_info(*fi, this);
5f0a03a6 1070 }
5f0a03a6
JK
1071 }
1072}
1073
1074void
1075dwarf_query::handle_query_module()
1076{
f4faaf86
JS
1077 if (has_plt && has_statement_num)
1078 {
1079 query_plt_statement (this);
1080 return;
1081 }
1082
0035051e 1083 bool report = dbinfo_reqt == dbr_need_dwarf;
1c6b77e5
JS
1084 dw.get_module_dwarf(false, report);
1085
1086 // prebuild the symbol table to resolve aliases
5f52fafe 1087 dw.mod_info->get_symtab();
1c6b77e5 1088
857bdfd1
JS
1089 // reset the dupe-checking for each new module
1090 alias_dupes.clear();
7fdd3e2c 1091 inline_dupes.clear();
857bdfd1 1092
5f0a03a6
JK
1093 if (dw.mod_info->dwarf_status == info_present)
1094 query_module_dwarf();
1c6b77e5 1095
91bb9081
LB
1096 // Consult the symbol table, asm and weak functions can show up
1097 // in the symbol table but not in dwarf and minidebuginfo is
1098 // located in the gnu_debugdata section, alias_dupes checking
1099 // is done before adding any probe points
1100 if (!query_done && !pending_interrupts)
5f0a03a6
JK
1101 query_module_symtab();
1102}
1103
2c384610 1104
7d6d0afc
JS
1105void
1106dwarf_query::parse_function_spec(const string & spec)
bd2b1e68 1107{
4a81c0d9 1108 lineno_type = ABSOLUTE;
f01d4ffb 1109 size_t src_pos, line_pos, scope_pos;
bd2b1e68 1110
7d6d0afc 1111 // look for named scopes
91699a70
JS
1112 scope_pos = spec.rfind("::");
1113 if (scope_pos != string::npos)
bd2b1e68 1114 {
91699a70
JS
1115 tokenize_cxx(spec.substr(0, scope_pos), scopes);
1116 scope_pos += 2;
bd2b1e68 1117 }
91699a70
JS
1118 else
1119 scope_pos = 0;
bd2b1e68 1120
7d6d0afc
JS
1121 // look for a source separator
1122 src_pos = spec.find('@', scope_pos);
1123 if (src_pos == string::npos)
bd2b1e68 1124 {
7d6d0afc
JS
1125 function = spec.substr(scope_pos);
1126 spec_type = function_alone;
bd2b1e68 1127 }
7d6d0afc 1128 else
879eb9e9 1129 {
7d6d0afc 1130 function = spec.substr(scope_pos, src_pos - scope_pos);
7a053d3b 1131
7d6d0afc
JS
1132 // look for a line-number separator
1133 line_pos = spec.find_first_of(":+", src_pos);
1134 if (line_pos == string::npos)
1135 {
1136 file = spec.substr(src_pos + 1);
1137 spec_type = function_and_file;
1138 }
1139 else
1140 {
1141 file = spec.substr(src_pos + 1, line_pos - src_pos - 1);
1142
1143 // classify the line spec
1144 spec_type = function_file_and_line;
1145 if (spec[line_pos] == '+')
4a81c0d9 1146 lineno_type = RELATIVE;
7d6d0afc
JS
1147 else if (spec[line_pos + 1] == '*' &&
1148 spec.length() == line_pos + 2)
4a81c0d9 1149 lineno_type = WILDCARD;
7d6d0afc 1150 else
4a81c0d9 1151 lineno_type = ABSOLUTE;
7d6d0afc 1152
4a81c0d9 1153 if (lineno_type != WILDCARD)
7d6d0afc
JS
1154 try
1155 {
f01d4ffb
BC
1156 // try to parse N, N-M, or N,M,O,P, or combination thereof...
1157 if (spec.find_first_of(",-", line_pos + 1) != string::npos)
1158 {
1159 lineno_type = ENUMERATED;
1160 vector<string> sub_specs;
1161 tokenize(spec.substr(line_pos + 1), sub_specs, ",");
1162 vector<string>::const_iterator line_spec;
1163 for (line_spec = sub_specs.begin(); line_spec != sub_specs.end(); ++line_spec)
1164 {
1165 vector<string> ranges;
1166 tokenize(*line_spec, ranges, "-");
1167 if (ranges.size() > 1)
1168 for (int i = lex_cast<int>(ranges.front()); i <= lex_cast<int>(ranges.back()); i++)
1169 linenos.push_back(i);
1170 else
1171 linenos.push_back(lex_cast<int>(ranges.at(0)));
1172 }
1173 sort(linenos.begin(), linenos.end());
1174 }
7d6d0afc
JS
1175 else
1176 {
f01d4ffb
BC
1177 linenos.push_back(lex_cast<int>(spec.substr(line_pos + 1)));
1178 linenos.push_back(lex_cast<int>(spec.substr(line_pos + 1)));
7d6d0afc
JS
1179 }
1180 }
1181 catch (runtime_error & exn)
1182 {
1183 goto bad;
1184 }
1185 }
bd2b1e68
GH
1186 }
1187
7d6d0afc
JS
1188 if (function.empty() ||
1189 (spec_type != function_alone && file.empty()))
bd2b1e68
GH
1190 goto bad;
1191
7d6d0afc 1192 if (sess.verbose > 2)
bd2b1e68 1193 {
b530b5b3
LB
1194 //clog << "parsed '" << spec << "'";
1195 clog << _F("parse '%s'", spec.c_str());
41c262f3 1196
7d6d0afc
JS
1197 if (!scopes.empty())
1198 clog << ", scope '" << scopes[0] << "'";
1199 for (unsigned i = 1; i < scopes.size(); ++i)
1200 clog << "::'" << scopes[i] << "'";
41c262f3 1201
7d6d0afc
JS
1202 clog << ", func '" << function << "'";
1203
1204 if (spec_type != function_alone)
1205 clog << ", file '" << file << "'";
1206
1207 if (spec_type == function_file_and_line)
1208 {
1209 clog << ", line ";
4a81c0d9 1210 switch (lineno_type)
7d6d0afc
JS
1211 {
1212 case ABSOLUTE:
4a81c0d9 1213 clog << linenos[0];
7d6d0afc
JS
1214 break;
1215
1216 case RELATIVE:
4a81c0d9 1217 clog << "+" << linenos[0];
7d6d0afc
JS
1218 break;
1219
f01d4ffb
BC
1220 case ENUMERATED:
1221 {
10e6fe03 1222 vector<int>::const_iterator linenos_it;
f01d4ffb
BC
1223 for (linenos_it = linenos.begin(); linenos_it != linenos.end(); ++linenos_it)
1224 {
1225 vector<int>::const_iterator range_it(linenos_it);
1226 while ((range_it+1) != linenos.end() && *range_it + 1 == *(range_it+1))
1227 ++range_it;
1228 if (linenos_it == range_it)
1229 clog << *linenos_it;
1230 else
1231 clog << *linenos_it << "-" << *range_it;
1232 if (range_it + 1 != linenos.end())
1233 clog << ",";
1234 linenos_it = range_it;
1235 }
1236 }
7d6d0afc
JS
1237 break;
1238
1239 case WILDCARD:
1240 clog << "*";
1241 break;
1242 }
1243 }
1244
1245 clog << endl;
bd2b1e68
GH
1246 }
1247
7d6d0afc
JS
1248 return;
1249
1250bad:
dc09353a 1251 throw SEMANTIC_ERROR(_F("malformed specification '%s'", spec.c_str()),
7d6d0afc 1252 base_probe->tok);
bd2b1e68
GH
1253}
1254
05fb3e0c
WF
1255string path_remove_sysroot(const systemtap_session& sess, const string& path)
1256{
1257 size_t pos;
1258 string retval = path;
1259 if (!sess.sysroot.empty() &&
1260 (pos = retval.find(sess.sysroot)) != string::npos)
1261 retval.replace(pos, sess.sysroot.length(), "/");
1262 return retval;
1263}
bd2b1e68 1264
36f9dd1d 1265void
1ffb8bd1 1266dwarf_query::add_probe_point(const string& dw_funcname,
b20febf3 1267 const char* filename,
36f9dd1d 1268 int line,
b20febf3 1269 Dwarf_Die* scope_die,
36f9dd1d
FCE
1270 Dwarf_Addr addr)
1271{
b20febf3 1272 string reloc_section; // base section for relocation purposes
27646582 1273 Dwarf_Addr reloc_addr; // relocated
b20febf3 1274 const string& module = dw.module_name; // "kernel" or other
1ffb8bd1 1275 string funcname = dw_funcname;
36f9dd1d 1276
37ebca01
FCE
1277 assert (! has_absolute); // already handled in dwarf_builder::build()
1278
f4faaf86 1279 reloc_addr = dw.relocate_address(addr, reloc_section);
2930abc7 1280
1ffb8bd1 1281 // If we originally used the linkage name, then let's call it that way
1ffb8bd1 1282 const char* linkage_name;
91bb9081 1283 if (!null_die(scope_die) && startswith (this->function, "_Z")
f450a7e3 1284 && (linkage_name = dwarf_linkage_name (scope_die)))
1ffb8bd1
JS
1285 funcname = linkage_name;
1286
7f9f3386
FCE
1287 if (sess.verbose > 1)
1288 {
b530b5b3 1289 clog << _("probe ") << funcname << "@" << filename << ":" << line;
b20febf3 1290 if (string(module) == TOK_KERNEL)
b530b5b3 1291 clog << _(" kernel");
91af0778 1292 else if (has_module)
b530b5b3 1293 clog << _(" module=") << module;
91af0778 1294 else if (has_process)
b530b5b3 1295 clog << _(" process=") << module;
b20febf3 1296 if (reloc_section != "") clog << " reloc=" << reloc_section;
b20febf3 1297 clog << " pc=0x" << hex << addr << dec;
7f9f3386 1298 }
4baf0e53 1299
2f18e69b
JL
1300 dwflpp::blacklisted_type blacklisted = dw.blacklisted_p (funcname, filename,
1301 line, module, addr,
1302 has_return);
b20febf3
FCE
1303 if (sess.verbose > 1)
1304 clog << endl;
7f9f3386 1305
84048984
FCE
1306 if (module == TOK_KERNEL)
1307 {
1308 // PR 4224: adapt to relocatable kernel by subtracting the _stext address here.
1309 reloc_addr = addr - sess.sym_stext;
37ebca01 1310 reloc_section = "_stext"; // a message to runtime's _stp_module_relocate
84048984
FCE
1311 }
1312
2f18e69b 1313 if (!blacklisted)
b20febf3 1314 {
1a0dbc5a 1315 sess.unwindsym_modules.insert (module);
6d0f3f0c
FCE
1316
1317 if (has_process)
1318 {
05fb3e0c 1319 string module_tgt = path_remove_sysroot(sess, module);
6d0f3f0c 1320 results.push_back (new uprobe_derived_probe(funcname, filename, line,
05fb3e0c 1321 module_tgt, reloc_section, addr, reloc_addr,
6d0f3f0c
FCE
1322 *this, scope_die));
1323 }
1324 else
1325 {
1326 assert (has_kernel || has_module);
1327 results.push_back (new dwarf_derived_probe(funcname, filename, line,
06aca46a 1328 module, reloc_section, addr, reloc_addr,
6d0f3f0c
FCE
1329 *this, scope_die));
1330 }
b20febf3 1331 }
2f18e69b
JL
1332 else
1333 {
1334 switch (blacklisted)
1335 {
1336 case dwflpp::blacklisted_section:
1337 sess.print_warning(_F("function %s is in blacklisted section",
1338 funcname.c_str()), base_probe->tok);
1339 break;
1340 case dwflpp::blacklisted_kprobes:
1341 sess.print_warning(_F("kprobes function %s is blacklisted",
1342 funcname.c_str()), base_probe->tok);
1343 break;
1344 case dwflpp::blacklisted_function_return:
1345 sess.print_warning(_F("function %s return probe is blacklisted",
1346 funcname.c_str()), base_probe->tok);
1347 break;
1348 case dwflpp::blacklisted_file:
1349 sess.print_warning(_F("function %s is in blacklisted file",
1350 funcname.c_str()), base_probe->tok);
1351 break;
1352 case dwflpp::blacklisted_function:
1353 default:
1354 sess.print_warning(_F("function %s is blacklisted",
1355 funcname.c_str()), base_probe->tok);
1356 break;
1357 }
1358 }
2930abc7
FCE
1359}
1360
613c8675
JL
1361void
1362dwarf_query::mount_well_formed_probe_point()
1363{
1364 string module = dw.module_name;
1365 if (has_process)
1366 module = path_remove_sysroot(sess, module);
1367
1368 vector<probe_point::component*> comps;
1369 vector<probe_point::component*>::iterator it;
1370 for (it = base_loc->components.begin();
1371 it != base_loc->components.end(); ++it)
1372 {
1373 if ((*it)->functor == TOK_PROCESS || (*it)->functor == TOK_MODULE)
1374 comps.push_back(new probe_point::component((*it)->functor,
1375 new literal_string(has_library ? path : module)));
1376 else
1377 comps.push_back(*it);
1378 }
1379
1380 probe_point *pp = new probe_point(*base_loc);
1381 pp->well_formed = true;
1382 pp->components = comps;
1383
1384 previous_bases.push(make_pair(base_loc, base_probe));
1385
1386 base_loc = pp;
1387 base_probe = new probe(base_probe, pp);
1388}
1389
1390void
1391dwarf_query::unmount_well_formed_probe_point()
1392{
1393 assert(!previous_bases.empty());
1394
1395 base_loc = previous_bases.top().first;
1396 base_probe = previous_bases.top().second;
1397
1398 previous_bases.pop();
1399}
1400
71dfce11
JL
1401void
1402dwarf_query::replace_probe_point_component_arg(const string& functor,
1403 const string& new_functor,
1404 int64_t new_arg,
1405 bool hex)
1406{
1407 // only allow these operations if we're editing the well-formed loc
1408 assert(!previous_bases.empty());
1409
1410 vector<probe_point::component*>::iterator it;
1411 for (it = base_loc->components.begin();
1412 it != base_loc->components.end(); ++it)
1413 if ((*it)->functor == functor)
1414 *it = new probe_point::component(new_functor,
1415 new literal_number(new_arg, hex));
1416}
1417
1418void
1419dwarf_query::replace_probe_point_component_arg(const string& functor,
1420 int64_t new_arg,
1421 bool hex)
1422{
1423 replace_probe_point_component_arg(functor, functor, new_arg, hex);
1424}
1425
1426void
1427dwarf_query::replace_probe_point_component_arg(const string& functor,
1428 const string& new_functor,
1429 const string& new_arg)
1430{
1431 // only allow these operations if we're editing the well-formed loc
1432 assert(!previous_bases.empty());
1433
1434 vector<probe_point::component*>::iterator it;
1435 for (it = base_loc->components.begin();
1436 it != base_loc->components.end(); ++it)
1437 if ((*it)->functor == functor)
1438 *it = new probe_point::component(new_functor,
1439 new literal_string(new_arg));
1440}
1441
1442void
1443dwarf_query::replace_probe_point_component_arg(const string& functor,
1444 const string& new_arg)
1445{
1446 replace_probe_point_component_arg(functor, functor, new_arg);
1447}
1448
1449void
1450dwarf_query::remove_probe_point_component(const string& functor)
1451{
1452 // only allow these operations if we're editing the well-formed loc
1453 assert(!previous_bases.empty());
1454
1455 vector<probe_point::component*> new_comps;
1456 vector<probe_point::component*>::iterator it;
1457 for (it = base_loc->components.begin();
1458 it != base_loc->components.end(); ++it)
1459 if ((*it)->functor != functor)
1460 new_comps.push_back(*it);
1461
1462 base_loc->components = new_comps;
1463}
1464
5f0a03a6
JK
1465enum dbinfo_reqt
1466dwarf_query::assess_dbinfo_reqt()
1467{
1468 if (has_absolute)
1469 {
1470 // kernel.statement(NUM).absolute
1471 return dbr_none;
1472 }
c31add50 1473 if (has_inline || has_label || has_callee || has_callees_num)
5f0a03a6
JK
1474 {
1475 // kernel.function("f").inline or module("m").function("f").inline
1476 return dbr_need_dwarf;
1477 }
1478 if (has_function_str && spec_type == function_alone)
1479 {
1480 // kernel.function("f") or module("m").function("f")
1481 return dbr_need_symtab;
1482 }
1483 if (has_statement_num)
1484 {
1485 // kernel.statement(NUM) or module("m").statement(NUM)
1486 // Technically, all we need is the module offset (or _stext, for
1487 // the kernel). But for that we need either the ELF file or (for
1488 // _stext) the symbol table. In either case, the symbol table
1489 // is available, and that allows us to map the NUM (address)
1490 // to a function, which is goodness.
1491 return dbr_need_symtab;
1492 }
1493 if (has_function_num)
1494 {
1495 // kernel.function(NUM) or module("m").function(NUM)
1496 // Need the symbol table so we can back up from NUM to the
1497 // start of the function.
1498 return dbr_need_symtab;
1499 }
1500 // Symbol table tells us nothing about source files or line numbers.
1501 return dbr_need_dwarf;
1502}
2930abc7 1503
e772a6e7
JL
1504string
1505dwarf_query::final_function_name(const string& final_func,
79954c0f 1506 const char* final_file,
e772a6e7
JL
1507 int final_line)
1508{
1509 string final_name = final_func;
79954c0f 1510 if (final_file && *final_file != '\0')
e772a6e7 1511 {
79954c0f 1512 final_name += ("@" + string(final_file));
e772a6e7
JL
1513 if (final_line > 0)
1514 final_name += (":" + lex_cast(final_line));
1515 }
1516 return final_name;
1517}
2930abc7 1518
959ccfed
JL
1519base_func_info_map_t
1520dwarf_query::filtered_all(void)
1521{
1522 vector<base_func_info> r;
1523 func_info_map_t::const_iterator f;
1524 for (f = filtered_functions.begin();
1525 f != filtered_functions.end(); ++f)
1526 r.push_back(*f);
1527 inline_instance_map_t::const_iterator i;
1528 for (i = filtered_inlines.begin();
1529 i != filtered_inlines.end(); ++i)
1530 r.push_back(*i);
1531 return r;
1532}
1533
b8da0ad1
FCE
1534// The critical determining factor when interpreting a pattern
1535// string is, perhaps surprisingly: "presence of a lineno". The
1536// presence of a lineno changes the search strategy completely.
1537//
1538// Compare the two cases:
1539//
1540// 1. {statement,function}(foo@file.c:lineno)
1541// - find the files matching file.c
1542// - in each file, find the functions matching foo
1543// - query the file for line records matching lineno
1544// - iterate over the line records,
1545// - and iterate over the functions,
1546// - if(haspc(function.DIE, line.addr))
1547// - if looking for statements: probe(lineno.addr)
1548// - if looking for functions: probe(function.{entrypc,return,etc.})
1549//
1550// 2. {statement,function}(foo@file.c)
1551// - find the files matching file.c
1552// - in each file, find the functions matching foo
1553// - probe(function.{entrypc,return,etc.})
1554//
1555// Thus the first decision we make is based on the presence of a
1556// lineno, and we enter entirely different sets of callbacks
1557// depending on that decision.
1558//
1559// Note that the first case is a generalization fo the second, in that
1560// we could theoretically search through line records for matching
1561// file names (a "table scan" in rdbms lingo). Luckily, file names
1562// are already cached elsewhere, so we can do an "index scan" as an
1563// optimization.
7e1279ea 1564
bd2b1e68 1565static void
4cd232e4 1566query_statement (string const & func,
20e4a32c 1567 char const * file,
4cd232e4 1568 int line,
bcc12710 1569 Dwarf_Die *scope_die,
20e4a32c 1570 Dwarf_Addr stmt_addr,
4cd232e4 1571 dwarf_query * q)
bd2b1e68 1572{
39bcd429
FCE
1573 try
1574 {
cee35f73 1575 q->add_probe_point(func, file ? file : "",
a9b2f3a5 1576 line, scope_die, stmt_addr);
39bcd429
FCE
1577 }
1578 catch (const semantic_error& e)
1579 {
1580 q->sess.print_error (e);
1581 }
bd2b1e68
GH
1582}
1583
6b517475
JS
1584static void
1585query_addr(Dwarf_Addr addr, dwarf_query *q)
1586{
7a71819c
JL
1587 assert(q->has_function_num || q->has_statement_num);
1588
6b517475
JS
1589 dwflpp &dw = q->dw;
1590
08d1d520
MW
1591 if (q->sess.verbose > 2)
1592 clog << "query_addr 0x" << hex << addr << dec << endl;
6b517475
JS
1593
1594 // First pick which CU contains this address
1595 Dwarf_Die* cudie = dw.query_cu_containing_address(addr);
1596 if (!cudie) // address could be wildly out of range
1597 return;
1598 dw.focus_on_cu(cudie);
1599
1600 // Now compensate for the dw bias
1601 addr -= dw.module_bias;
1602
1603 // Per PR5787, we look up the scope die even for
1604 // statement_num's, for blacklist sensitivity and $var
1605 // resolution purposes.
1606
1607 // Find the scopes containing this address
1608 vector<Dwarf_Die> scopes = dw.getscopes(addr);
1609 if (scopes.empty())
1610 return;
1611
1612 // Look for the innermost containing function
1613 Dwarf_Die *fnscope = NULL;
1614 for (size_t i = 0; i < scopes.size(); ++i)
1615 {
1616 int tag = dwarf_tag(&scopes[i]);
1617 if ((tag == DW_TAG_subprogram && !q->has_inline) ||
1618 (tag == DW_TAG_inlined_subroutine &&
4bda987e 1619 !q->has_call && !q->has_return && !q->has_exported))
6b517475
JS
1620 {
1621 fnscope = &scopes[i];
1622 break;
1623 }
1624 }
1625 if (!fnscope)
1626 return;
1627 dw.focus_on_function(fnscope);
1628
1629 Dwarf_Die *scope = q->has_function_num ? fnscope : &scopes[0];
1630
1631 const char *file = dwarf_decl_file(fnscope);
1632 int line;
1633 dwarf_decl_line(fnscope, &line);
1634
1635 // Function probes should reset the addr to the function entry
1636 // and possibly perform prologue searching
1637 if (q->has_function_num)
1638 {
1bbf3f90
JS
1639 if (!dw.die_entrypc(fnscope, &addr))
1640 return;
6b517475 1641 if (dwarf_tag(fnscope) == DW_TAG_subprogram &&
b5b9879f
JL
1642 (q->sess.prologue_searching ||
1643 (q->has_process && !q->dw.has_valid_locs()))) // PR 6871 && PR 6941
6b517475
JS
1644 {
1645 func_info func;
1646 func.die = *fnscope;
1647 func.name = dw.function_name;
1648 func.decl_file = file;
1649 func.decl_line = line;
1650 func.entrypc = addr;
1651
1652 func_info_map_t funcs(1, func);
1653 dw.resolve_prologue_endings (funcs);
5a617dc6
JL
1654 q->prologue_end = funcs[0].prologue_end;
1655
1656 // PR13200: if it's a .return probe, we need to emit a *retprobe based
1657 // on the entrypc so here we only use prologue_end for non .return
1658 // probes (note however that .return probes still take advantage of
1659 // prologue_end: PR14436)
1660 if (!q->has_return)
1661 addr = funcs[0].prologue_end;
6b517475
JS
1662 }
1663 }
1664 else
1665 {
de797211
JL
1666 Dwarf_Line *address_line = dwarf_getsrc_die(cudie, addr);
1667 Dwarf_Addr address_line_addr = addr;
6b517475
JS
1668 if (address_line)
1669 {
bf5974d6
JL
1670 file = DWARF_LINESRC(address_line);
1671 line = DWARF_LINENO(address_line);
1672 address_line_addr = DWARF_LINEADDR(address_line);
6b517475
JS
1673 }
1674
1675 // Verify that a raw address matches the beginning of a
1676 // statement. This is a somewhat lame check that the address
1677 // is at the start of an assembly instruction. Mark probes are in the
1678 // middle of a macro and thus not strictly at a statement beginning.
1679 // Guru mode may override this check.
de797211 1680 if (!q->has_mark && (!address_line || address_line_addr != addr))
6b517475
JS
1681 {
1682 stringstream msg;
2a97f50b 1683 msg << _F("address %#" PRIx64 " does not match the beginning of a statement",
b530b5b3 1684 addr);
6b517475 1685 if (address_line)
de797211 1686 msg << _F(" (try %#" PRIx64 ")", address_line_addr);
6b517475 1687 else
b530b5b3
LB
1688 msg << _F(" (no line info found for '%s', in module '%s')",
1689 dw.cu_name().c_str(), dw.module_name.c_str());
6b517475 1690 if (! q->sess.guru_mode)
dc09353a 1691 throw SEMANTIC_ERROR(msg.str());
2713ea24 1692 else
6b517475
JS
1693 q->sess.print_warning(msg.str());
1694 }
1695 }
1696
7a71819c
JL
1697 // We're ready to build a probe, but before, we need to create the final,
1698 // well-formed version of this location with all the components filled in
1699 q->mount_well_formed_probe_point();
1700 q->replace_probe_point_component_arg(TOK_FUNCTION, addr, true /* hex */ );
1701 q->replace_probe_point_component_arg(TOK_STATEMENT, addr, true /* hex */ );
1702
6b517475
JS
1703 // Build a probe at this point
1704 query_statement(dw.function_name, file, line, scope, addr, q);
7a71819c
JL
1705
1706 q->unmount_well_formed_probe_point();
6b517475
JS
1707}
1708
f4faaf86
JS
1709static void
1710query_plt_statement(dwarf_query *q)
1711{
1712 assert (q->has_plt && q->has_statement_num);
1713
1714 Dwarf_Addr addr = q->statement_num_val;
1715 if (q->sess.verbose > 2)
1716 clog << "query_plt_statement 0x" << hex << addr << dec << endl;
1717
1718 // First adjust the raw address to dwfl's elf bias.
1719 Dwarf_Addr elf_bias;
1720 Elf *elf = dwfl_module_getelf (q->dw.module, &elf_bias);
1721 assert(elf);
1722 addr += elf_bias;
1723
1724 // Now compensate for the dw bias
1725 q->dw.get_module_dwarf(false, false);
1726 addr -= q->dw.module_bias;
1727
93b44c2a
JL
1728 // Create the final well-formed probe point
1729 q->mount_well_formed_probe_point();
1730 q->replace_probe_point_component_arg(TOK_STATEMENT, q->statement_num_val, true /* hex */ );
1731
1732 // We remove the .plt part here, since if the user provided a .plt probe, then
1733 // the higher-level probe point is already well-formed. On the other hand, if
1734 // the user provides a .plt(PATTERN).statement(0xABCD), the PATTERN is
1735 // irrelevant (we won't iterate over plts) so just take it out.
1736 q->remove_probe_point_component(TOK_PLT);
1737
f4faaf86
JS
1738 // Build a probe at this point
1739 query_statement(q->plt_val, NULL, -1, NULL, addr, q);
93b44c2a
JL
1740
1741 q->unmount_well_formed_probe_point();
f4faaf86
JS
1742}
1743
8096dd7d 1744static void
600d39f1 1745query_label (const base_func_info& func,
8096dd7d
JS
1746 char const * label,
1747 char const * file,
1748 int line,
1749 Dwarf_Die *scope_die,
1750 Dwarf_Addr stmt_addr,
1751 dwarf_query * q)
1752{
6b517475
JS
1753 assert (q->has_statement_str || q->has_function_str);
1754
8096dd7d
JS
1755 // weed out functions whose decl_file isn't one of
1756 // the source files that we actually care about
6b517475 1757 if (q->spec_type != function_alone &&
8096dd7d
JS
1758 q->filtered_srcfiles.count(file) == 0)
1759 return;
1760
cdabbe86 1761 // Create the final well-formed probe
600d39f1 1762 string canon_func = q->final_function_name(func.name, file, line);
9891c543 1763
cdabbe86
JL
1764 q->mount_well_formed_probe_point();
1765 q->replace_probe_point_component_arg(TOK_FUNCTION, canon_func);
1766 q->replace_probe_point_component_arg(TOK_LABEL, label);
8096dd7d 1767
600d39f1 1768 query_statement(func.name, file, line, scope_die, stmt_addr, q);
9891c543 1769
cdabbe86 1770 q->unmount_well_formed_probe_point();
8096dd7d
JS
1771}
1772
67959c62 1773static void
e772a6e7
JL
1774query_callee (base_func_info& callee,
1775 base_func_info& caller,
67959c62
JL
1776 stack<Dwarf_Addr> *callers,
1777 dwarf_query * q)
1778{
1779 assert (q->has_function_str);
e772a6e7
JL
1780 assert (q->has_callee || q->has_callees_num);
1781
1782 // OK, we found a callee for a targeted caller. To help users see the
04a89a2a 1783 // derivation, we add the well-formed form .function(caller).callee(callee).
e772a6e7 1784
e772a6e7
JL
1785 string canon_caller = q->final_function_name(caller.name, caller.decl_file,
1786 caller.decl_line);
1787 string canon_callee = q->final_function_name(callee.name, callee.decl_file,
1788 callee.decl_line);
1789
04a89a2a
JL
1790 q->mount_well_formed_probe_point();
1791 q->replace_probe_point_component_arg(TOK_FUNCTION, canon_caller);
1792 q->replace_probe_point_component_arg(TOK_CALLEES, TOK_CALLEE, canon_callee);
1793 q->replace_probe_point_component_arg(TOK_CALLEE, canon_callee);
e772a6e7
JL
1794
1795 // Pass on the callers we'll need to add checks for
04a89a2a 1796 q->callers = callers;
e772a6e7
JL
1797
1798 query_statement(callee.name, callee.decl_file, callee.decl_line,
04a89a2a
JL
1799 &callee.die, callee.entrypc, q);
1800
1801 q->unmount_well_formed_probe_point();
67959c62
JL
1802}
1803
7e1279ea 1804static void
3e961ba6 1805query_inline_instance_info (inline_instance_info & ii,
7e1279ea
FCE
1806 dwarf_query * q)
1807{
b6581717 1808 try
7e1279ea 1809 {
8f14e444 1810 assert (! q->has_return); // checked by caller already
8debd4e7
JL
1811 assert (q->has_function_str || q->has_statement_str);
1812
8f14e444 1813 if (q->sess.verbose>2)
b530b5b3
LB
1814 clog << _F("querying entrypc %#" PRIx64 " of instance of inline '%s'\n",
1815 ii.entrypc, ii.name.c_str());
8debd4e7
JL
1816
1817 string canon_func = q->final_function_name(ii.name, ii.decl_file,
1818 ii.decl_line);
1819
1820 q->mount_well_formed_probe_point();
1821 q->replace_probe_point_component_arg(TOK_FUNCTION, canon_func);
1822 q->replace_probe_point_component_arg(TOK_STATEMENT, canon_func);
1823
8f14e444
FCE
1824 query_statement (ii.name, ii.decl_file, ii.decl_line,
1825 &ii.die, ii.entrypc, q);
8debd4e7
JL
1826
1827 q->unmount_well_formed_probe_point();
7e1279ea 1828 }
b6581717 1829 catch (semantic_error &e)
7e1279ea 1830 {
b6581717 1831 q->sess.print_error (e);
7e1279ea
FCE
1832 }
1833}
1834
1835static void
1836query_func_info (Dwarf_Addr entrypc,
bcc12710 1837 func_info & fi,
7e1279ea
FCE
1838 dwarf_query * q)
1839{
e5745975
JL
1840 assert(q->has_function_str || q->has_statement_str);
1841
b6581717 1842 try
7e1279ea 1843 {
e5745975
JL
1844 string canon_func = q->final_function_name(fi.name, fi.decl_file,
1845 fi.decl_line);
1846
1847 q->mount_well_formed_probe_point();
1848 q->replace_probe_point_component_arg(TOK_FUNCTION, canon_func);
1849 q->replace_probe_point_component_arg(TOK_STATEMENT, canon_func);
1850
5a617dc6
JL
1851 // If it's a .return probe, we need to emit a *retprobe based on the
1852 // entrypc (PR13200). Note however that if prologue_end is valid,
1853 // dwarf_derived_probe will still take advantage of it by creating a new
1854 // probe there if necessary to pick up target vars (PR14436).
1855 if (fi.prologue_end == 0 || q->has_return)
1856 {
170ccf10 1857 q->prologue_end = fi.prologue_end;
5a617dc6
JL
1858 query_statement (fi.name, fi.decl_file, fi.decl_line,
1859 &fi.die, entrypc, q);
1860 }
b6581717 1861 else
5a617dc6 1862 {
5a617dc6
JL
1863 query_statement (fi.name, fi.decl_file, fi.decl_line,
1864 &fi.die, fi.prologue_end, q);
1865 }
e5745975
JL
1866
1867 q->unmount_well_formed_probe_point();
7e1279ea 1868 }
b6581717 1869 catch (semantic_error &e)
7e1279ea 1870 {
b6581717 1871 q->sess.print_error (e);
7e1279ea
FCE
1872 }
1873}
1874
7e1279ea 1875static void
de797211 1876query_srcfile_line (Dwarf_Addr addr, int lineno, dwarf_query * q)
7e1279ea 1877{
8fb810d7
JL
1878 assert (q->has_statement_str || q->has_function_str);
1879 assert (q->spec_type == function_file_and_line);
1880
959ccfed
JL
1881 base_func_info_map_t bfis = q->filtered_all();
1882 base_func_info_map_t::iterator i;
1883 for (i = bfis.begin(); i != bfis.end(); ++i)
897820ca 1884 {
3e961ba6 1885 if (q->dw.die_has_pc (i->die, addr))
959ccfed
JL
1886 {
1887 if (q->sess.verbose>3)
1888 clog << _("filtered DIE lands on srcfile\n");
1889 Dwarf_Die scope;
1890 q->dw.inner_die_containing_pc(i->die, addr, scope);
e5bc2e52
JL
1891
1892 string canon_func = q->final_function_name(i->name, i->decl_file,
1893 lineno /* NB: not i->decl_line */ );
1894
1895 q->mount_well_formed_probe_point();
1896 q->replace_probe_point_component_arg(TOK_FUNCTION, canon_func);
1897 q->replace_probe_point_component_arg(TOK_STATEMENT, canon_func);
1898
959ccfed
JL
1899 query_statement (i->name, i->decl_file,
1900 lineno, // NB: not q->line !
1901 &scope, addr, q);
e5bc2e52
JL
1902
1903 q->unmount_well_formed_probe_point();
959ccfed 1904 }
20e4a32c 1905 }
7e1279ea
FCE
1906}
1907
7fdd3e2c
JS
1908bool
1909inline_instance_info::operator<(const inline_instance_info& other) const
1910{
1911 if (entrypc != other.entrypc)
1912 return entrypc < other.entrypc;
1913
1914 if (decl_line != other.decl_line)
1915 return decl_line < other.decl_line;
1916
1917 int cmp = name.compare(other.name);
c60517ca
FCE
1918
1919 if (!cmp)
1920 {
1921 assert (decl_file);
1922 assert (other.decl_file);
1923 cmp = strcmp(decl_file, other.decl_file);
1924 }
1925
7fdd3e2c
JS
1926 return cmp < 0;
1927}
1928
1929
4fa7b22b 1930static int
3956fccb 1931query_dwarf_inline_instance (Dwarf_Die * die, dwarf_query * q)
4fa7b22b 1932{
6b517475 1933 assert (q->has_statement_str || q->has_function_str);
4bda987e 1934 assert (!q->has_call && !q->has_return && !q->has_exported);
bd2b1e68 1935
39bcd429 1936 try
7a053d3b 1937 {
b0ee93c4 1938 if (q->sess.verbose>2)
b530b5b3 1939 clog << _F("selected inline instance of %s\n", q->dw.function_name.c_str());
7e1279ea 1940
6b517475
JS
1941 Dwarf_Addr entrypc;
1942 if (q->dw.die_entrypc (die, &entrypc))
1943 {
1944 inline_instance_info inl;
1945 inl.die = *die;
1946 inl.name = q->dw.function_name;
1947 inl.entrypc = entrypc;
1948 q->dw.function_file (&inl.decl_file);
1949 q->dw.function_line (&inl.decl_line);
1950
1951 // make sure that this inline hasn't already
1952 // been matched from a different CU
1953 if (q->inline_dupes.insert(inl).second)
1954 q->filtered_inlines.push_back(inl);
1955 }
7e1279ea
FCE
1956 return DWARF_CB_OK;
1957 }
1958 catch (const semantic_error& e)
1959 {
1960 q->sess.print_error (e);
1961 return DWARF_CB_ABORT;
1962 }
1963}
bb788f9f 1964
7e1279ea 1965static int
7d007451 1966query_dwarf_func (Dwarf_Die * func, dwarf_query * q)
7e1279ea 1967{
6b517475 1968 assert (q->has_statement_str || q->has_function_str);
bb788f9f 1969
bd25380d
JS
1970 // weed out functions whose decl_file isn't one of
1971 // the source files that we actually care about
6b517475 1972 if (q->spec_type != function_alone &&
bd25380d 1973 q->filtered_srcfiles.count(dwarf_decl_file(func)?:"") == 0)
8096dd7d 1974 return DWARF_CB_OK;
bd25380d 1975
7e1279ea
FCE
1976 try
1977 {
7e1279ea
FCE
1978 q->dw.focus_on_function (func);
1979
7d6d0afc
JS
1980 if (!q->dw.function_scope_matches(q->scopes))
1981 return DWARF_CB_OK;
1982
857bdfd1
JS
1983 // make sure that this function address hasn't
1984 // already been matched under an aliased name
1985 Dwarf_Addr addr;
1986 if (!q->dw.func_is_inline() &&
1987 dwarf_entrypc(func, &addr) == 0 &&
1988 !q->alias_dupes.insert(addr).second)
1989 return DWARF_CB_OK;
1990
4bda987e 1991 if (q->dw.func_is_inline () && (! q->has_call) && (! q->has_return) && (! q->has_exported))
7e1279ea 1992 {
4bda987e 1993 if (q->sess.verbose>3)
b530b5b3 1994 clog << _F("checking instances of inline %s\n", q->dw.function_name.c_str());
4bda987e 1995 q->dw.iterate_over_inline_instances (query_dwarf_inline_instance, q);
7e1279ea 1996 }
8f14e444
FCE
1997 else if (q->dw.func_is_inline () && (q->has_return)) // PR 11553
1998 {
1999 q->inlined_non_returnable.insert (q->dw.function_name);
2000 }
396afcee 2001 else if (!q->dw.func_is_inline () && (! q->has_inline))
20e4a32c 2002 {
4bda987e
SC
2003 if (q->has_exported && !q->dw.func_is_exported ())
2004 return DWARF_CB_OK;
6b517475 2005 if (q->sess.verbose>2)
b530b5b3 2006 clog << _F("selected function %s\n", q->dw.function_name.c_str());
6b517475
JS
2007
2008 func_info func;
2009 q->dw.function_die (&func.die);
2010 func.name = q->dw.function_name;
2011 q->dw.function_file (&func.decl_file);
2012 q->dw.function_line (&func.decl_line);
2013
2014 Dwarf_Addr entrypc;
2015 if (q->dw.function_entrypc (&entrypc))
2016 {
2017 func.entrypc = entrypc;
2018 q->filtered_functions.push_back (func);
2019 }
2020 /* else this function is fully inlined, just ignore it */
7e1279ea 2021 }
39bcd429 2022 return DWARF_CB_OK;
bd2b1e68 2023 }
39bcd429 2024 catch (const semantic_error& e)
bd2b1e68 2025 {
39bcd429
FCE
2026 q->sess.print_error (e);
2027 return DWARF_CB_ABORT;
bd2b1e68 2028 }
bd2b1e68
GH
2029}
2030
2031static int
5c378838 2032query_cu (Dwarf_Die * cudie, dwarf_query * q)
bd2b1e68 2033{
6b517475
JS
2034 assert (q->has_statement_str || q->has_function_str);
2035
85007c04 2036 if (pending_interrupts) return DWARF_CB_ABORT;
7a053d3b 2037
39bcd429 2038 try
bd2b1e68 2039 {
7e1279ea 2040 q->dw.focus_on_cu (cudie);
b5d77020 2041
b0ee93c4 2042 if (false && q->sess.verbose>2)
b530b5b3
LB
2043 clog << _F("focused on CU '%s', in module '%s'\n",
2044 q->dw.cu_name().c_str(), q->dw.module_name.c_str());
d9b516ca 2045
6b517475
JS
2046 q->filtered_srcfiles.clear();
2047 q->filtered_functions.clear();
2048 q->filtered_inlines.clear();
2049
2050 // In this path, we find "abstract functions", record
2051 // information about them, and then (depending on lineno
2052 // matching) possibly emit one or more of the function's
2053 // associated addresses. Unfortunately the control of this
2054 // cannot easily be turned inside out.
2055
2056 if (q->spec_type != function_alone)
39bcd429 2057 {
6b517475
JS
2058 // If we have a pattern string with a filename, we need
2059 // to elaborate the srcfile mask in question first.
2060 q->dw.collect_srcfiles_matching (q->file, q->filtered_srcfiles);
2061
2062 // If we have a file pattern and *no* srcfile matches, there's
2063 // no need to look further into this CU, so skip.
2064 if (q->filtered_srcfiles.empty())
2065 return DWARF_CB_OK;
2066 }
e4c58386 2067
6b517475
JS
2068 // Pick up [entrypc, name, DIE] tuples for all the functions
2069 // matching the query, and fill in the prologue endings of them
2070 // all in a single pass.
5898b6e1 2071 int rc = q->dw.iterate_over_functions (query_dwarf_func, q, q->function);
6b517475
JS
2072 if (rc != DWARF_CB_OK)
2073 q->query_done = true;
2074
277a2b96
JL
2075 if (!q->filtered_functions.empty() &&
2076 !q->has_statement_str && // PR 2608
2077 (q->sess.prologue_searching ||
2078 (q->has_process && !q->dw.has_valid_locs()))) // PR 6871 && PR 6941
2079 q->dw.resolve_prologue_endings (q->filtered_functions);
6b517475 2080
600d39f1 2081 if (q->has_label)
6b517475 2082 {
600d39f1
JL
2083 enum lineno_t lineno_type = WILDCARD;
2084 if (q->spec_type == function_file_and_line)
2085 lineno_type = q->lineno_type;
959ccfed
JL
2086 base_func_info_map_t bfis = q->filtered_all();
2087 base_func_info_map_t::iterator i;
2088 for (i = bfis.begin(); i != bfis.end(); ++i)
600d39f1
JL
2089 q->dw.iterate_over_labels (&i->die, q->label_val, *i, q->linenos,
2090 lineno_type, q, query_label);
39bcd429 2091 }
67959c62
JL
2092 else if (q->has_callee || q->has_callees_num)
2093 {
2094 // .callee(str) --> str, .callees[(N)] --> "*"
2095 string callee_val = q->has_callee ? q->callee_val : "*";
2096 long callees_num_val = q->has_callees_num ? q->callees_num_val : 1;
2097
2098 // NB: We filter functions that do not match the file here rather than
2099 // in query_callee because we only want the filtering to apply to the
2100 // first level, not to callees that are recursed into if
2101 // callees_num_val > 1.
959ccfed
JL
2102 base_func_info_map_t bfis = q->filtered_all();
2103 base_func_info_map_t::iterator i;
2104 for (i = bfis.begin(); i != bfis.end(); ++i)
67959c62
JL
2105 {
2106 if (q->spec_type != function_alone &&
2107 q->filtered_srcfiles.count(i->decl_file) == 0)
2108 continue;
2109 q->dw.iterate_over_callees (&i->die, callee_val,
2110 callees_num_val,
e772a6e7 2111 q, query_callee, *i);
67959c62
JL
2112 }
2113 }
600d39f1
JL
2114 else if (q->spec_type == function_file_and_line)
2115 {
2116 // .statement(...:NN) often gets mixed up with .function(...:NN)
2117 if (q->has_function_str)
2118 q->sess.print_warning (_("For probing a particular line, use a "
2119 ".statement() probe, not .function()"),
2120 q->base_probe->tok);
2121
2122 base_func_info_map_t bfis = q->filtered_all();
2123
2124 set<string>::const_iterator srcfile;
2125 for (srcfile = q->filtered_srcfiles.begin();
2126 srcfile != q->filtered_srcfiles.end(); ++srcfile)
2127 q->dw.iterate_over_srcfile_lines(srcfile->c_str(), q->linenos,
2128 q->lineno_type, bfis,
2129 query_srcfile_line, q);
2130 }
6b517475
JS
2131 else
2132 {
2133 // Otherwise, simply probe all resolved functions.
2134 for (func_info_map_t::iterator i = q->filtered_functions.begin();
2135 i != q->filtered_functions.end(); ++i)
2136 query_func_info (i->entrypc, *i, q);
2137
2138 // And all inline instances (if we're not excluding inlines with ".call")
2139 if (! q->has_call)
2140 for (inline_instance_map_t::iterator i
2141 = q->filtered_inlines.begin(); i != q->filtered_inlines.end(); ++i)
2142 query_inline_instance_info (*i, q);
e772a6e7 2143 }
39bcd429 2144 return DWARF_CB_OK;
bd2b1e68 2145 }
39bcd429 2146 catch (const semantic_error& e)
bd2b1e68 2147 {
39bcd429
FCE
2148 q->sess.print_error (e);
2149 return DWARF_CB_ABORT;
bd2b1e68 2150 }
bd2b1e68
GH
2151}
2152
0ce64fb8 2153
4df79aaf
JS
2154void
2155dwarf_query::query_module_functions ()
2156{
2157 try
2158 {
2159 filtered_srcfiles.clear();
2160 filtered_functions.clear();
2161 filtered_inlines.clear();
2162
2163 // Collect all module functions so we know which CUs are interesting
2164 int rc = dw.iterate_single_function(query_dwarf_func, this, function);
2165 if (rc != DWARF_CB_OK)
2166 {
2167 query_done = true;
2168 return;
2169 }
2170
2171 set<void*> used_cus; // by cu->addr
2172 vector<Dwarf_Die> cus;
2173 Dwarf_Die cu_mem;
2174
959ccfed
JL
2175 base_func_info_map_t bfis = filtered_all();
2176 base_func_info_map_t::iterator i;
2177 for (i = bfis.begin(); i != bfis.end(); ++i)
4df79aaf
JS
2178 if (dwarf_diecu(&i->die, &cu_mem, NULL, NULL) &&
2179 used_cus.insert(cu_mem.addr).second)
2180 cus.push_back(cu_mem);
2181
2182 // Reset the dupes since we didn't actually collect them the first time
2183 alias_dupes.clear();
2184 inline_dupes.clear();
2185
2186 // Run the query again on the individual CUs
91bb9081
LB
2187 for (vector<Dwarf_Die>::iterator i = cus.begin(); i != cus.end(); ++i){
2188 rc = query_cu(&*i, this);
2189 if (rc != DWARF_CB_OK)
2190 {
2191 query_done = true;
2192 return;
2193 }
2194 }
4df79aaf
JS
2195 }
2196 catch (const semantic_error& e)
2197 {
2198 sess.print_error (e);
2199 }
2200}
2201
2202
5f0a03a6
JK
2203static void
2204validate_module_elf (Dwfl_Module *mod, const char *name, base_query *q)
2205{
2206 // Validate the machine code in this elf file against the
2207 // session machine. This is important, in case the wrong kind
2208 // of debuginfo is being automagically processed by elfutils.
2209 // While we can tell i686 apart from x86-64, unfortunately
2210 // we can't help confusing i586 vs i686 (both EM_386).
2211
2212 Dwarf_Addr bias;
2213 // We prefer dwfl_module_getdwarf to dwfl_module_getelf here,
2214 // because dwfl_module_getelf can force costly section relocations
2215 // we don't really need, while either will do for this purpose.
2216 Elf* elf = (dwarf_getelf (dwfl_module_getdwarf (mod, &bias))
2217 ?: dwfl_module_getelf (mod, &bias));
2218
2219 GElf_Ehdr ehdr_mem;
2220 GElf_Ehdr* em = gelf_getehdr (elf, &ehdr_mem);
e1c3b13a 2221 if (em == 0) { DWFL_ASSERT ("dwfl_getehdr", dwfl_errno()); }
d8f31d0a 2222 assert(em);
5f0a03a6
JK
2223 int elf_machine = em->e_machine;
2224 const char* debug_filename = "";
2225 const char* main_filename = "";
2226 (void) dwfl_module_info (mod, NULL, NULL,
2227 NULL, NULL, NULL,
2228 & main_filename,
2229 & debug_filename);
2230 const string& sess_machine = q->sess.architecture;
756c9462
FCE
2231
2232 string expect_machine; // to match sess.machine (i.e., kernel machine)
2233 string expect_machine2;
5f0a03a6 2234
d27e6fd5 2235 // NB: See also the 'uname -m' squashing done in main.cxx.
5f0a03a6
JK
2236 switch (elf_machine)
2237 {
756c9462
FCE
2238 // x86 and ppc are bi-architecture; a 64-bit kernel
2239 // can normally run either 32-bit or 64-bit *userspace*.
2240 case EM_386:
2241 expect_machine = "i?86";
2242 if (! q->has_process) break; // 32-bit kernel/module
2243 /* FALLSTHROUGH */
2244 case EM_X86_64:
2245 expect_machine2 = "x86_64";
2246 break;
2247 case EM_PPC:
756c9462 2248 case EM_PPC64:
5a1c472e 2249 expect_machine = "powerpc";
756c9462 2250 break;
3fe7d888 2251 case EM_S390: expect_machine = "s390"; break;
5f0a03a6 2252 case EM_IA_64: expect_machine = "ia64"; break;
d27e6fd5 2253 case EM_ARM: expect_machine = "arm*"; break;
86aae990 2254 case EM_AARCH64: expect_machine = "arm64"; break;
5f0a03a6
JK
2255 // XXX: fill in some more of these
2256 default: expect_machine = "?"; break;
2257 }
2258
2259 if (! debug_filename) debug_filename = main_filename;
2260 if (! debug_filename) debug_filename = name;
2261
756c9462
FCE
2262 if (fnmatch (expect_machine.c_str(), sess_machine.c_str(), 0) != 0 &&
2263 fnmatch (expect_machine2.c_str(), sess_machine.c_str(), 0) != 0)
5f0a03a6
JK
2264 {
2265 stringstream msg;
b530b5b3
LB
2266 msg << _F("ELF machine %s|%s (code %d) mismatch with target %s in '%s'",
2267 expect_machine.c_str(), expect_machine2.c_str(), elf_machine,
2268 sess_machine.c_str(), debug_filename);
dc09353a 2269 throw SEMANTIC_ERROR(msg.str ());
5f0a03a6
JK
2270 }
2271
b57082de 2272 if (q->sess.verbose>1)
b78a0fbb 2273 clog << _F("focused on module '%s' = [%#" PRIx64 "-%#" PRIx64 ", bias %#" PRIx64
b530b5b3
LB
2274 " file %s ELF machine %s|%s (code %d)\n",
2275 q->dw.module_name.c_str(), q->dw.module_start, q->dw.module_end,
2276 q->dw.module_bias, debug_filename, expect_machine.c_str(),
2277 expect_machine2.c_str(), elf_machine);
5f0a03a6 2278}
1d3a40b6 2279
91af0778
FCE
2280
2281
2282static Dwarf_Addr
2283lookup_symbol_address (Dwfl_Module *m, const char* wanted)
2284{
2285 int syments = dwfl_module_getsymtab(m);
2286 assert(syments);
2287 for (int i = 1; i < syments; ++i)
2288 {
2289 GElf_Sym sym;
2290 const char *name = dwfl_module_getsym(m, i, &sym, NULL);
2291 if (name != NULL && strcmp(name, wanted) == 0)
2292 return sym.st_value;
2293 }
2294
2295 return 0;
2296}
2297
2298
2299
bd2b1e68 2300static int
b8da0ad1 2301query_module (Dwfl_Module *mod,
91af0778 2302 void **,
b8da0ad1 2303 const char *name,
6f4c1275 2304 Dwarf_Addr addr,
06de3a04 2305 base_query *q)
bd2b1e68 2306{
39bcd429 2307 try
e38d6504 2308 {
91af0778
FCE
2309 module_info* mi = q->sess.module_cache->cache[name];
2310 if (mi == 0)
2311 {
2312 mi = q->sess.module_cache->cache[name] = new module_info(name);
2313
6f4c1275
FCE
2314 mi->mod = mod;
2315 mi->addr = addr;
91af0778 2316
6f4c1275
FCE
2317 const char* debug_filename = "";
2318 const char* main_filename = "";
2319 (void) dwfl_module_info (mod, NULL, NULL,
2320 NULL, NULL, NULL,
2321 & main_filename,
2322 & debug_filename);
2323
ab3ed72d 2324 if (debug_filename || main_filename)
91af0778 2325 {
6f4c1275
FCE
2326 mi->elf_path = debug_filename ?: main_filename;
2327 }
2328 else if (name == TOK_KERNEL)
2329 {
2330 mi->dwarf_status = info_absent;
91af0778 2331 }
91af0778
FCE
2332 }
2333 // OK, enough of that module_info caching business.
2334
5f0a03a6 2335 q->dw.focus_on_module(mod, mi);
d9b516ca 2336
39bcd429
FCE
2337 // If we have enough information in the pattern to skip a module and
2338 // the module does not match that information, return early.
b8da0ad1 2339 if (!q->dw.module_name_matches(q->module_val))
85007c04 2340 return pending_interrupts ? DWARF_CB_ABORT : DWARF_CB_OK;
0cbbf9d1
FCE
2341
2342 // Don't allow module("*kernel*") type expressions to match the
2343 // elfutils module "kernel", which we refer to in the probe
2344 // point syntax exclusively as "kernel.*".
2345 if (q->dw.module_name == TOK_KERNEL && ! q->has_kernel)
85007c04 2346 return pending_interrupts ? DWARF_CB_ABORT : DWARF_CB_OK;
b5d77020 2347
5f0a03a6
JK
2348 if (mod)
2349 validate_module_elf(mod, name, q);
2350 else
91af0778
FCE
2351 assert(q->has_kernel); // and no vmlinux to examine
2352
2353 if (q->sess.verbose>2)
b530b5b3 2354 cerr << _F("focused on module '%s'\n", q->dw.module_name.c_str());
91af0778
FCE
2355
2356
2357 // Collect a few kernel addresses. XXX: these belong better
2358 // to the sess.module_info["kernel"] struct.
2359 if (q->dw.module_name == TOK_KERNEL)
c931ec8a 2360 {
91af0778
FCE
2361 if (! q->sess.sym_kprobes_text_start)
2362 q->sess.sym_kprobes_text_start = lookup_symbol_address (mod, "__kprobes_text_start");
2363 if (! q->sess.sym_kprobes_text_end)
2364 q->sess.sym_kprobes_text_end = lookup_symbol_address (mod, "__kprobes_text_end");
2365 if (! q->sess.sym_stext)
2366 q->sess.sym_stext = lookup_symbol_address (mod, "_stext");
c931ec8a
FCE
2367 }
2368
dbe9d133
JL
2369 // If there is a .library component, then q->path will hold the path to
2370 // the executable if the library was fully resolved. If not (e.g. not
2371 // absolute, or globby), resort to iterate_over_libraries().
2372 if (q->has_library && q->path.empty())
84c84ac4 2373 q->dw.iterate_over_libraries (&q->query_library_callback, q);
576eaefe
SC
2374 // .plt is translated to .plt.statement(N). We only want to iterate for the
2375 // .plt case
2376 else if (q->has_plt && ! q->has_statement)
070764c0
JL
2377 {
2378 q->dw.iterate_over_plt (q, &q->query_plt_callback);
2379 q->visited_modules.insert(name);
2380 }
84c84ac4 2381 else
070764c0
JL
2382 {
2383 // search the module for matches of the probe point.
2384 q->handle_query_module();
2385 q->visited_modules.insert(name);
2386 }
bb788f9f 2387
b8da0ad1 2388 // If we know that there will be no more matches, abort early.
85007c04 2389 if (q->dw.module_name_final_match(q->module_val) || pending_interrupts)
b8da0ad1
FCE
2390 return DWARF_CB_ABORT;
2391 else
2392 return DWARF_CB_OK;
7a053d3b 2393 }
39bcd429 2394 catch (const semantic_error& e)
bd2b1e68 2395 {
39bcd429
FCE
2396 q->sess.print_error (e);
2397 return DWARF_CB_ABORT;
bd2b1e68 2398 }
bd2b1e68
GH
2399}
2400
35d4ab18 2401
84c84ac4 2402void
bbbc7241 2403base_query::query_library_callback (base_query *me, const char *data)
84c84ac4 2404{
5d5bd369 2405 me->query_library (data);
84c84ac4
SC
2406}
2407
2408
614f0fcf
JL
2409probe*
2410build_library_probe(dwflpp& dw,
2411 const string& library,
2412 probe *base_probe,
2413 probe_point *base_loc)
2414{
2415 probe_point* specific_loc = new probe_point(*base_loc);
614f0fcf
JL
2416 specific_loc->from_glob = true;
2417 vector<probe_point::component*> derived_comps;
2418
2419 // Create new probe point for the matching library. This is what will be
2420 // shown in listing mode. Also replace the process(str) with the real
2421 // absolute path rather than keeping what the user typed in.
2422 vector<probe_point::component*>::iterator it;
2423 for (it = specific_loc->components.begin();
2424 it != specific_loc->components.end(); ++it)
2425 if ((*it)->functor == TOK_PROCESS)
2426 derived_comps.push_back(new probe_point::component(TOK_PROCESS,
2427 new literal_string(path_remove_sysroot(dw.sess, dw.module_name))));
2428 else if ((*it)->functor == TOK_LIBRARY)
2429 derived_comps.push_back(new probe_point::component(TOK_LIBRARY,
2430 new literal_string(path_remove_sysroot(dw.sess, library))));
2431 else
2432 derived_comps.push_back(*it);
2433 probe_point* derived_loc = new probe_point(*specific_loc);
2434 derived_loc->components = derived_comps;
2435 return new probe (new probe (base_probe, specific_loc), derived_loc);
2436}
2437
2438bool
51d6bda3
SC
2439query_one_library (const char *library, dwflpp & dw,
2440 const string user_lib, probe * base_probe, probe_point *base_loc,
2441 vector<derived_probe *> & results)
84c84ac4 2442{
47e226ed 2443 if (dw.function_name_matches_pattern(library, "*" + user_lib))
84c84ac4 2444 {
05fb3e0c
WF
2445 string library_path = find_executable (library, "", dw.sess.sysenv,
2446 "LD_LIBRARY_PATH");
168e2ef5
JL
2447 probe *new_base = build_library_probe(dw, library_path,
2448 base_probe, base_loc);
2449
2450 // We pass true for the optional parameter of derive_probes() here to
2451 // indicate that we don't mind if the probe doesn't resolve. This is
2452 // because users expect wildcarded probe points to only apply to a subset
2453 // of matching libraries, in the sense of "any", rather than "all", just
2454 // like module("*") and process("*"). See also dwarf_builder::build().
2455 derive_probes(dw.sess, new_base, results, true /* optional */ );
2456
51d6bda3 2457 if (dw.sess.verbose > 2)
2cbcfa9c 2458 clog << _("module=") << library_path << endl;
614f0fcf 2459 return true;
84c84ac4 2460 }
614f0fcf 2461 return false;
84c84ac4
SC
2462}
2463
2464
51d6bda3
SC
2465void
2466dwarf_query::query_library (const char *library)
2467{
614f0fcf
JL
2468 visited_libraries.insert(library);
2469 if (query_one_library (library, dw, user_lib, base_probe, base_loc, results))
2470 resolved_library = true;
51d6bda3
SC
2471}
2472
576eaefe
SC
2473struct plt_expanding_visitor: public var_expanding_visitor
2474{
2475 plt_expanding_visitor(const string & entry):
2476 entry (entry)
2477 {
2478 }
2479 const string & entry;
2480
2481 void visit_target_symbol (target_symbol* e);
2482};
2483
2484
2485void
45cdb40e 2486base_query::query_plt_callback (base_query *me, const char *entry, size_t address)
576eaefe 2487{
576eaefe
SC
2488 if (me->dw.function_name_matches_pattern (entry, me->plt_val))
2489 me->query_plt (entry, address);
7f4964f1 2490 me->dw.mod_info->plt_funcs.insert(entry);
576eaefe
SC
2491}
2492
2493
2494void
2495query_one_plt (const char *entry, long addr, dwflpp & dw,
2496 probe * base_probe, probe_point *base_loc,
93b44c2a 2497 vector<derived_probe *> & results, base_query *q)
576eaefe 2498{
93b44c2a
JL
2499 string module = dw.module_name;
2500 if (q->has_process)
2501 module = path_remove_sysroot(dw.sess, module);
2502
576eaefe 2503 probe_point* specific_loc = new probe_point(*base_loc);
93b44c2a
JL
2504 specific_loc->well_formed = true;
2505
576eaefe
SC
2506 vector<probe_point::component*> derived_comps;
2507
2508 if (dw.sess.verbose > 2)
2509 clog << _F("plt entry=%s\n", entry);
2510
576eaefe
SC
2511 vector<probe_point::component*>::iterator it;
2512 for (it = specific_loc->components.begin();
2513 it != specific_loc->components.end(); ++it)
93b44c2a
JL
2514 if ((*it)->functor == TOK_PROCESS)
2515 {
2516 // Replace with fully resolved path
2517 *it = new probe_point::component(TOK_PROCESS,
2518 new literal_string(q->has_library ? q->path : module));
2519 derived_comps.push_back(*it);
2520 }
2521 else if ((*it)->functor == TOK_PLT)
3d372d6b 2522 {
93b44c2a 2523 // Replace possibly globby component
8159bf55
FCE
2524 *it = new probe_point::component(TOK_PLT,
2525 new literal_string(entry));
3d372d6b
SC
2526 derived_comps.push_back(*it);
2527 derived_comps.push_back(new probe_point::component(TOK_STATEMENT,
ac7c8669 2528 new literal_number(addr, true)));
3d372d6b 2529 }
576eaefe
SC
2530 else
2531 derived_comps.push_back(*it);
2532 probe_point* derived_loc = new probe_point(*specific_loc);
2533 derived_loc->components = derived_comps;
02c34e38
FCE
2534 probe *new_base = new probe (new probe (base_probe, specific_loc),
2535 derived_loc);
576eaefe
SC
2536 string e = string(entry);
2537 plt_expanding_visitor pltv (e);
2538 pltv.replace (new_base->body);
2539 derive_probes(dw.sess, new_base, results);
2540}
2541
2542
2543void
2544dwarf_query::query_plt (const char *entry, size_t address)
2545{
93b44c2a 2546 query_one_plt (entry, address, dw, base_probe, base_loc, results, this);
576eaefe 2547}
51d6bda3 2548
435f53a7
FCE
2549// This would more naturally fit into elaborate.cxx:semantic_pass_symbols,
2550// but the needed declaration for module_cache is not available there.
2551// Nor for that matter in session.cxx. Only in this CU is that field ever
2552// set (in query_module() above), so we clean it up here too.
2553static void
2554delete_session_module_cache (systemtap_session& s)
2555{
2556 if (s.module_cache) {
2557 if (s.verbose > 3)
b530b5b3 2558 clog << _("deleting module_cache") << endl;
435f53a7
FCE
2559 delete s.module_cache;
2560 s.module_cache = 0;
2561 }
2562}
2563
2564
de688825 2565struct dwarf_var_expanding_visitor: public var_expanding_visitor
35d4ab18 2566{
77de5e9e 2567 dwarf_query & q;
bcc12710 2568 Dwarf_Die *scope_die;
77de5e9e 2569 Dwarf_Addr addr;
8c819921 2570 block *add_block;
2260f4e3 2571 block *add_call_probe; // synthesized from .return probes with saved $vars
1630966a
JS
2572 // NB: tids are not always collected in add_block & add_call_probe, because
2573 // gen_kretprobe_saved_return doesn't need them. Thus we need these extra
2574 // *_tid bools for gen_mapped_saved_return to tell what's there.
8cc799a5 2575 bool add_block_tid, add_call_probe_tid;
af234c40
JS
2576 unsigned saved_longs, saved_strings; // data saved within kretprobes
2577 map<std::string, expression *> return_ts_map;
729455a7 2578 vector<Dwarf_Die> scopes;
3689db05 2579 // probe counter name -> pointer of associated probe
698de6cc 2580 std::set<derived_probe*> perf_counter_refs;
b95e2b79 2581 bool visited;
77de5e9e 2582
de688825 2583 dwarf_var_expanding_visitor(dwarf_query & q, Dwarf_Die *sd, Dwarf_Addr a):
af234c40 2584 q(q), scope_die(sd), addr(a), add_block(NULL), add_call_probe(NULL),
8cc799a5 2585 add_block_tid(false), add_call_probe_tid(false),
af234c40 2586 saved_longs(0), saved_strings(0), visited(false) {}
277c21bc 2587 expression* gen_mapped_saved_return(expression* e, const string& name);
140be17a 2588 expression* gen_kretprobe_saved_return(expression* e);
a7999c82
JS
2589 void visit_target_symbol_saved_return (target_symbol* e);
2590 void visit_target_symbol_context (target_symbol* e);
d7f3e0c5 2591 void visit_target_symbol (target_symbol* e);
bd1fcbad 2592 void visit_atvar_op (atvar_op* e);
c24447be 2593 void visit_cast_op (cast_op* e);
8cc799a5 2594 void visit_entry_op (entry_op* e);
3689db05 2595 void visit_perf_op (perf_op* e);
729455a7
JS
2596private:
2597 vector<Dwarf_Die>& getscopes(target_symbol *e);
77de5e9e
GH
2598};
2599
2600
de688825 2601unsigned var_expanding_visitor::tick = 0;
77de5e9e 2602
a50de939 2603
74fe61bc 2604var_expanding_visitor::var_expanding_visitor (): op()
a50de939
DS
2605{
2606 // FIXME: for the time being, by default we only support plain '$foo
2607 // = bar', not '+=' or any other op= variant. This is fixable, but a
2608 // bit ugly.
2609 //
2610 // If derived classes desire to add additional operator support, add
2611 // new operators to this list in the derived class constructor.
2612 valid_ops.insert ("=");
2613}
2614
2615
87214add
JS
2616bool
2617var_expanding_visitor::rewrite_lvalue(const token* tok, const std::string& eop,
2618 expression*& lvalue, expression*& rvalue)
77de5e9e 2619{
e57b735a
GH
2620 // Our job would normally be to require() the left and right sides
2621 // into a new assignment. What we're doing is slightly trickier:
2622 // we're pushing a functioncall** onto a stack, and if our left
2623 // child sets the functioncall* for that value, we're going to
2624 // assume our left child was a target symbol -- transformed into a
2625 // set_target_foo(value) call, and it wants to take our right child
2626 // as the argument "value".
2627 //
2628 // This is why some people claim that languages with
2629 // constructor-decomposing case expressions have a leg up on
2630 // visitors.
2631
2632 functioncall *fcall = NULL;
d9b516ca 2633
a50de939 2634 // Let visit_target_symbol know what operator it should handle.
87214add
JS
2635 const string* old_op = op;
2636 op = &eop;
a50de939 2637
e57b735a 2638 target_symbol_setter_functioncalls.push (&fcall);
87214add 2639 replace (lvalue);
e57b735a 2640 target_symbol_setter_functioncalls.pop ();
87214add
JS
2641 replace (rvalue);
2642
2643 op = old_op;
e57b735a
GH
2644
2645 if (fcall != NULL)
77de5e9e 2646 {
e57b735a
GH
2647 // Our left child is informing us that it was a target variable
2648 // and it has been replaced with a set_target_foo() function
2649 // call; we are going to provide that function call -- with the
2650 // right child spliced in as sole argument -- in place of
de688825 2651 // ourselves, in the var expansion we're in the middle of making.
e57b735a 2652
87214add 2653 if (valid_ops.find (eop) == valid_ops.end ())
a50de939
DS
2654 {
2655 // Build up a list of supported operators.
2656 string ops;
2657 std::set<string>::iterator i;
b530b5b3 2658 int valid_ops_size = 0;
a50de939 2659 for (i = valid_ops.begin(); i != valid_ops.end(); i++)
b530b5b3 2660 {
a50de939 2661 ops += " " + *i + ",";
b530b5b3
LB
2662 valid_ops_size++;
2663 }
a50de939
DS
2664 ops.resize(ops.size() - 1); // chop off the last ','
2665
2666 // Throw the error.
dc09353a 2667 throw SEMANTIC_ERROR (_NF("Only the following assign operator is implemented on target variables: %s",
1e41115c 2668 "Only the following assign operators are implemented on target variables: %s",
52c2652f 2669 valid_ops_size, ops.c_str()), tok);
b530b5b3 2670
a50de939 2671 }
e57b735a 2672
87214add
JS
2673 assert (lvalue == fcall);
2674 if (rvalue)
2675 fcall->args.push_back (rvalue);
4ed05b15 2676 provide (fcall);
87214add 2677 return true;
77de5e9e 2678 }
e57b735a 2679 else
87214add
JS
2680 return false;
2681}
2682
2683
2684void
2685var_expanding_visitor::visit_assignment (assignment* e)
2686{
2687 if (!rewrite_lvalue (e->tok, e->op, e->left, e->right))
2688 provide (e);
2689}
2690
2691
2692void
2693var_expanding_visitor::visit_pre_crement (pre_crement* e)
2694{
2695 expression *dummy = NULL;
2696 if (!rewrite_lvalue (e->tok, e->op, e->operand, dummy))
2697 provide (e);
2698}
2699
2700
2701void
2702var_expanding_visitor::visit_post_crement (post_crement* e)
2703{
2704 expression *dummy = NULL;
2705 if (!rewrite_lvalue (e->tok, e->op, e->operand, dummy))
2706 provide (e);
2707}
2708
2709
2710void
2711var_expanding_visitor::visit_delete_statement (delete_statement* s)
2712{
2713 string fakeop = "delete";
2714 expression *dummy = NULL;
2715 if (!rewrite_lvalue (s->tok, fakeop, s->value, dummy))
2716 provide (s);
e57b735a 2717}
d9b516ca 2718
d7f3e0c5 2719
30263a73
FCE
2720void
2721var_expanding_visitor::visit_defined_op (defined_op* e)
2722{
2723 bool resolved = true;
2724
2725 defined_ops.push (e);
2726 try {
2727 // NB: provide<>/require<> are NOT typesafe. So even though a defined_op is
2728 // defined with a target_symbol* operand, a subsidiary call may attempt to
2729 // rewrite it to a general expression* instead, and require<> happily
2730 // casts to/from void*, causing possible memory corruption. We use
2731 // expression* here, being the general case of rewritten $variable.
2732 expression *foo1 = e->operand;
2733 foo1 = require (foo1);
2734
c69a87e0 2735 // NB: Formerly, we had some curious cases to consider here, depending on what
30263a73 2736 // various visit_target_symbol() implementations do for successful or
c69a87e0
FCE
2737 // erroneous resolutions. Some would signal a visit_target_symbol failure
2738 // with an exception, with a set flag within the target_symbol, or nothing
2739 // at all.
30263a73 2740 //
c69a87e0
FCE
2741 // Now, failures always have to be signalled with a
2742 // saved_conversion_error being chained to the target_symbol.
2743 // Successes have to result in an attempted rewrite of the
850bfddd 2744 // target_symbol (via provide()).
780f11ff 2745 //
c69a87e0
FCE
2746 // Edna Mode: "no capes". fche: "no exceptions".
2747
30263a73
FCE
2748 // dwarf stuff: success: rewrites to a function; failure: retains target_symbol, sets saved_conversion_error
2749 //
2750 // sdt-kprobes sdt.h: success: string or functioncall; failure: semantic_error
2751 //
2752 // sdt-uprobes: success: string or no op; failure: no op; expect derived/synthetic
2753 // dwarf probe to take care of it.
2754 // But this is rather unhelpful. So we rig the sdt_var_expanding_visitor
2755 // to pass through @defined() to the synthetic dwarf probe.
780f11ff 2756 //
30263a73
FCE
2757 // utrace: success: rewrites to function; failure: semantic_error
2758 //
850bfddd 2759 // procfs: success: rewrites to function; failure: semantic_error
30263a73
FCE
2760
2761 target_symbol* foo2 = dynamic_cast<target_symbol*> (foo1);
c69a87e0 2762 if (foo2 && foo2->saved_conversion_error) // failing
30263a73 2763 resolved = false;
a45664f4 2764 else if (foo2) // unresolved but not marked failing
b7aedf26 2765 {
780f11ff
JS
2766 // There are some visitors that won't touch certain target_symbols,
2767 // e.g. dwarf_var_expanding_visitor won't resolve @cast. We should
2768 // leave it for now so some other visitor can have a chance.
b7aedf26
JS
2769 e->operand = foo2;
2770 provide (e);
2771 return;
2772 }
30263a73
FCE
2773 else // resolved, rewritten to some other expression type
2774 resolved = true;
780f11ff 2775 } catch (const semantic_error& e) {
c69a87e0 2776 assert (0); // should not happen
30263a73
FCE
2777 }
2778 defined_ops.pop ();
2779
2780 literal_number* ln = new literal_number (resolved ? 1 : 0);
2781 ln->tok = e->tok;
2782 provide (ln);
2783}
2784
2785
5f36109e
JS
2786struct dwarf_pretty_print
2787{
2788 dwarf_pretty_print (dwflpp& dw, vector<Dwarf_Die>& scopes, Dwarf_Addr pc,
2789 const string& local, bool userspace_p,
2790 const target_symbol& e):
d19a9a82
JS
2791 dw(dw), local(local), scopes(scopes), pc(pc), pointer(NULL),
2792 userspace_p(userspace_p), deref_p(true)
5f36109e
JS
2793 {
2794 init_ts (e);
2795 dw.type_die_for_local (scopes, pc, local, ts, &base_type);
2796 }
2797
2798 dwarf_pretty_print (dwflpp& dw, Dwarf_Die *scope_die, Dwarf_Addr pc,
2799 bool userspace_p, const target_symbol& e):
d19a9a82
JS
2800 dw(dw), scopes(1, *scope_die), pc(pc), pointer(NULL),
2801 userspace_p(userspace_p), deref_p(true)
5f36109e
JS
2802 {
2803 init_ts (e);
2804 dw.type_die_for_return (&scopes[0], pc, ts, &base_type);
2805 }
2806
2807 dwarf_pretty_print (dwflpp& dw, Dwarf_Die *type_die, expression* pointer,
d19a9a82 2808 bool deref_p, bool userspace_p, const target_symbol& e):
5f36109e 2809 dw(dw), pc(0), pointer(pointer), pointer_type(*type_die),
d19a9a82 2810 userspace_p(userspace_p), deref_p(deref_p)
5f36109e
JS
2811 {
2812 init_ts (e);
2813 dw.type_die_for_pointer (type_die, ts, &base_type);
2814 }
2815
2816 functioncall* expand ();
ce83ff57 2817 ~dwarf_pretty_print () { delete ts; }
5f36109e
JS
2818
2819private:
2820 dwflpp& dw;
2821 target_symbol* ts;
7d11d8c9 2822 bool print_full;
5f36109e
JS
2823 Dwarf_Die base_type;
2824
2825 string local;
2826 vector<Dwarf_Die> scopes;
2827 Dwarf_Addr pc;
2828
2829 expression* pointer;
2830 Dwarf_Die pointer_type;
2831
d19a9a82 2832 const bool userspace_p, deref_p;
5f36109e
JS
2833
2834 void recurse (Dwarf_Die* type, target_symbol* e,
7d11d8c9 2835 print_format* pf, bool top=false);
600551ca
JS
2836 void recurse_bitfield (Dwarf_Die* type, target_symbol* e,
2837 print_format* pf);
5f36109e 2838 void recurse_base (Dwarf_Die* type, target_symbol* e,
7d11d8c9 2839 print_format* pf);
5f36109e 2840 void recurse_array (Dwarf_Die* type, target_symbol* e,
7d11d8c9 2841 print_format* pf, bool top);
5f36109e 2842 void recurse_pointer (Dwarf_Die* type, target_symbol* e,
7d11d8c9 2843 print_format* pf, bool top);
5f36109e 2844 void recurse_struct (Dwarf_Die* type, target_symbol* e,
7d11d8c9 2845 print_format* pf, bool top);
5f36109e 2846 void recurse_struct_members (Dwarf_Die* type, target_symbol* e,
7d11d8c9 2847 print_format* pf, int& count);
bbee5bb8 2848 bool print_chars (Dwarf_Die* type, target_symbol* e, print_format* pf);
5f36109e
JS
2849
2850 void init_ts (const target_symbol& e);
2851 expression* deref (target_symbol* e);
c55ea10d 2852 bool push_deref (print_format* pf, const string& fmt, target_symbol* e);
5f36109e
JS
2853};
2854
2855
2856void
2857dwarf_pretty_print::init_ts (const target_symbol& e)
2858{
2859 // Work with a new target_symbol so we can modify arguments
2860 ts = new target_symbol (e);
2861
2862 if (ts->addressof)
dc09353a 2863 throw SEMANTIC_ERROR(_("cannot take address of pretty-printed variable"), ts->tok);
5f36109e
JS
2864
2865 if (ts->components.empty() ||
2866 ts->components.back().type != target_symbol::comp_pretty_print)
dc09353a 2867 throw SEMANTIC_ERROR(_("invalid target_symbol for pretty-print"), ts->tok);
7d11d8c9 2868 print_full = ts->components.back().member.length() > 1;
5f36109e
JS
2869 ts->components.pop_back();
2870}
2871
2872
2873functioncall*
2874dwarf_pretty_print::expand ()
2875{
2876 static unsigned tick = 0;
2877
2878 // function pretty_print_X([pointer], [arg1, arg2, ...]) {
7d11d8c9
JS
2879 // try {
2880 // return sprintf("{.foo=...}", (ts)->foo, ...)
2881 // } catch {
2882 // return "ERROR"
2883 // }
5f36109e
JS
2884 // }
2885
2886 // Create the function decl and call.
2887
2888 functiondecl *fdecl = new functiondecl;
2889 fdecl->tok = ts->tok;
2890 fdecl->synthetic = true;
2891 fdecl->name = "_dwarf_pretty_print_" + lex_cast(tick++);
2892 fdecl->type = pe_string;
2893
2894 functioncall* fcall = new functioncall;
2895 fcall->tok = ts->tok;
2896 fcall->function = fdecl->name;
140be17a 2897 fcall->type = pe_string;
5f36109e
JS
2898
2899 // If there's a <pointer>, replace it with a new var and make that
2900 // the first function argument.
2901 if (pointer)
2902 {
2903 vardecl *v = new vardecl;
2904 v->type = pe_long;
2905 v->name = "pointer";
2906 v->tok = ts->tok;
2907 fdecl->formal_args.push_back (v);
2908 fcall->args.push_back (pointer);
2909
2910 symbol* sym = new symbol;
2911 sym->tok = ts->tok;
2912 sym->name = v->name;
5f36109e
JS
2913 pointer = sym;
2914 }
2915
2916 // For each expression argument, replace it with a function argument.
2917 for (unsigned i = 0; i < ts->components.size(); ++i)
2918 if (ts->components[i].type == target_symbol::comp_expression_array_index)
2919 {
2920 vardecl *v = new vardecl;
2921 v->type = pe_long;
2922 v->name = "index" + lex_cast(i);
2923 v->tok = ts->tok;
2924 fdecl->formal_args.push_back (v);
2925 fcall->args.push_back (ts->components[i].expr_index);
2926
2927 symbol* sym = new symbol;
2928 sym->tok = ts->tok;
2929 sym->name = v->name;
5f36109e
JS
2930 ts->components[i].expr_index = sym;
2931 }
2932
2933 // Create the return sprintf.
1c922ad7 2934 print_format* pf = print_format::create(ts->tok, "sprintf");
5f36109e
JS
2935 return_statement* rs = new return_statement;
2936 rs->tok = ts->tok;
2937 rs->value = pf;
5f36109e
JS
2938
2939 // Recurse into the actual values.
7d11d8c9 2940 recurse (&base_type, ts, pf, true);
5f36109e
JS
2941 pf->components = print_format::string_to_components(pf->raw_components);
2942
7d11d8c9
JS
2943 // Create the try-catch net
2944 try_block* tb = new try_block;
2945 tb->tok = ts->tok;
2946 tb->try_block = rs;
2947 tb->catch_error_var = 0;
2948 return_statement* rs2 = new return_statement;
2949 rs2->tok = ts->tok;
2950 rs2->value = new literal_string ("ERROR");
2951 rs2->value->tok = ts->tok;
2952 tb->catch_block = rs2;
2953 fdecl->body = tb;
2954
f8809d54 2955 fdecl->join (dw.sess);
5f36109e
JS
2956 return fcall;
2957}
2958
2959
2960void
2961dwarf_pretty_print::recurse (Dwarf_Die* start_type, target_symbol* e,
7d11d8c9 2962 print_format* pf, bool top)
5f36109e
JS
2963{
2964 Dwarf_Die type;
2965 dw.resolve_unqualified_inner_typedie (start_type, &type, e);
2966
2967 switch (dwarf_tag(&type))
2968 {
2969 default:
2970 // XXX need a warning?
2971 // throw semantic_error ("unsupported type (tag " + lex_cast(dwarf_tag(&type))
2972 // + ") for " + dwarf_type_name(&type), e->tok);
2973 pf->raw_components.append("?");
2974 break;
2975
2976 case DW_TAG_enumeration_type:
2977 case DW_TAG_base_type:
7d11d8c9 2978 recurse_base (&type, e, pf);
5f36109e
JS
2979 break;
2980
2981 case DW_TAG_array_type:
7d11d8c9 2982 recurse_array (&type, e, pf, top);
5f36109e
JS
2983 break;
2984
2985 case DW_TAG_pointer_type:
2986 case DW_TAG_reference_type:
2987 case DW_TAG_rvalue_reference_type:
7d11d8c9 2988 recurse_pointer (&type, e, pf, top);
5f36109e
JS
2989 break;
2990
2991 case DW_TAG_subroutine_type:
c55ea10d 2992 push_deref (pf, "<function>:%p", e);
5f36109e
JS
2993 break;
2994
2995 case DW_TAG_union_type:
5f36109e
JS
2996 case DW_TAG_structure_type:
2997 case DW_TAG_class_type:
7d11d8c9 2998 recurse_struct (&type, e, pf, top);
5f36109e
JS
2999 break;
3000 }
3001}
3002
3003
600551ca
JS
3004// Bit fields are handled as a special-case combination of recurse() and
3005// recurse_base(), only called from recurse_struct_members(). The main
3006// difference is that the value is always printed numerically, even if the
3007// underlying type is a char.
3008void
3009dwarf_pretty_print::recurse_bitfield (Dwarf_Die* start_type, target_symbol* e,
3010 print_format* pf)
3011{
3012 Dwarf_Die type;
3013 dw.resolve_unqualified_inner_typedie (start_type, &type, e);
3014
3015 int tag = dwarf_tag(&type);
3016 if (tag != DW_TAG_base_type && tag != DW_TAG_enumeration_type)
3017 {
3018 // XXX need a warning?
3019 // throw semantic_error ("unsupported bitfield type (tag " + lex_cast(tag)
3020 // + ") for " + dwarf_type_name(&type), e->tok);
3021 pf->raw_components.append("?");
3022 return;
3023 }
3024
3025 Dwarf_Attribute attr;
3026 Dwarf_Word encoding = (Dwarf_Word) -1;
3027 dwarf_formudata (dwarf_attr_integrate (&type, DW_AT_encoding, &attr),
3028 &encoding);
3029 switch (encoding)
3030 {
3031 case DW_ATE_float:
3032 case DW_ATE_complex_float:
3033 // XXX need a warning?
3034 // throw semantic_error ("unsupported bitfield type (encoding " + lex_cast(encoding)
3035 // + ") for " + dwarf_type_name(&type), e->tok);
3036 pf->raw_components.append("?");
3037 break;
3038
3039 case DW_ATE_unsigned:
3040 case DW_ATE_unsigned_char:
3041 push_deref (pf, "%u", e);
3042 break;
3043
3044 case DW_ATE_signed:
3045 case DW_ATE_signed_char:
3046 default:
3047 push_deref (pf, "%i", e);
3048 break;
3049 }
3050}
3051
3052
5f36109e
JS
3053void
3054dwarf_pretty_print::recurse_base (Dwarf_Die* type, target_symbol* e,
7d11d8c9 3055 print_format* pf)
5f36109e
JS
3056{
3057 Dwarf_Attribute attr;
3058 Dwarf_Word encoding = (Dwarf_Word) -1;
3059 dwarf_formudata (dwarf_attr_integrate (type, DW_AT_encoding, &attr),
3060 &encoding);
5f36109e
JS
3061 switch (encoding)
3062 {
3063 case DW_ATE_float:
3064 case DW_ATE_complex_float:
3065 // XXX need a warning?
3066 // throw semantic_error ("unsupported type (encoding " + lex_cast(encoding)
3067 // + ") for " + dwarf_type_name(type), e->tok);
3068 pf->raw_components.append("?");
5f36109e
JS
3069 break;
3070
6561d8d1 3071 case DW_ATE_UTF: // XXX need to add unicode to _stp_vsprint_char
5f36109e
JS
3072 case DW_ATE_signed_char:
3073 case DW_ATE_unsigned_char:
941101c1
JS
3074 // Use escapes to make sure that non-printable characters
3075 // don't interrupt our stream (especially '\0' values).
3076 push_deref (pf, "'%#c'", e);
5f36109e
JS
3077 break;
3078
3079 case DW_ATE_unsigned:
c55ea10d 3080 push_deref (pf, "%u", e);
5f36109e
JS
3081 break;
3082
600551ca 3083 case DW_ATE_signed:
5f36109e 3084 default:
c55ea10d 3085 push_deref (pf, "%i", e);
5f36109e
JS
3086 break;
3087 }
5f36109e
JS
3088}
3089
3090
3091void
3092dwarf_pretty_print::recurse_array (Dwarf_Die* type, target_symbol* e,
7d11d8c9 3093 print_format* pf, bool top)
5f36109e 3094{
7d11d8c9
JS
3095 if (!top && !print_full)
3096 {
3097 pf->raw_components.append("[...]");
3098 return;
3099 }
3100
5f36109e
JS
3101 Dwarf_Die childtype;
3102 dwarf_attr_die (type, DW_AT_type, &childtype);
bbee5bb8
JS
3103
3104 if (print_chars (&childtype, e, pf))
3105 return;
3106
5f36109e
JS
3107 pf->raw_components.append("[");
3108
3109 // We print the array up to the first 5 elements.
3110 // XXX how can we determine the array size?
3111 // ... for now, just print the first element
64cddf39 3112 // NB: limit to 32 args; see PR10750 and c_unparser::visit_print_format.
5f36109e 3113 unsigned i, size = 1;
64cddf39 3114 for (i=0; i < size && i < 5 && pf->args.size() < 32; ++i)
5f36109e
JS
3115 {
3116 if (i > 0)
3117 pf->raw_components.append(", ");
3118 target_symbol* e2 = new target_symbol(*e);
3119 e2->components.push_back (target_symbol::component(e->tok, i));
7d11d8c9 3120 recurse (&childtype, e2, pf);
5f36109e
JS
3121 }
3122 if (i < size || 1/*XXX until real size is known */)
3123 pf->raw_components.append(", ...");
3124 pf->raw_components.append("]");
3125}
3126
3127
3128void
3129dwarf_pretty_print::recurse_pointer (Dwarf_Die* type, target_symbol* e,
7d11d8c9 3130 print_format* pf, bool top)
5f36109e 3131{
7d11d8c9 3132 // We chase to top-level pointers, but leave the rest alone
d19a9a82 3133 bool void_p = true;
7d11d8c9 3134 Dwarf_Die pointee;
bbee5bb8 3135 if (dwarf_attr_die (type, DW_AT_type, &pointee))
d19a9a82
JS
3136 {
3137 try
3138 {
3139 dw.resolve_unqualified_inner_typedie (&pointee, &pointee, e);
3140 void_p = false;
3141 }
3142 catch (const semantic_error&) {}
3143 }
3144
3145 if (!void_p)
5f36109e 3146 {
bbee5bb8
JS
3147 if (print_chars (&pointee, e, pf))
3148 return;
3149
3150 if (top)
3151 {
3152 recurse (&pointee, e, pf, top);
3153 return;
3154 }
5f36109e 3155 }
bbee5bb8 3156
c55ea10d 3157 push_deref (pf, "%p", e);
5f36109e
JS
3158}
3159
3160
3161void
3162dwarf_pretty_print::recurse_struct (Dwarf_Die* type, target_symbol* e,
7d11d8c9 3163 print_format* pf, bool top)
5f36109e 3164{
bdec0e18
JS
3165 if (dwarf_hasattr(type, DW_AT_declaration))
3166 {
a44a7cb5 3167 Dwarf_Die *resolved = dw.declaration_resolve(type);
bdec0e18
JS
3168 if (!resolved)
3169 {
3170 // could be an error, but for now just stub it
3171 // throw semantic_error ("unresolved " + dwarf_type_name(type), e->tok);
3172 pf->raw_components.append("{...}");
3173 return;
3174 }
3175 type = resolved;
3176 }
3177
5f36109e
JS
3178 int count = 0;
3179 pf->raw_components.append("{");
7d11d8c9
JS
3180 if (top || print_full)
3181 recurse_struct_members (type, e, pf, count);
3182 else
3183 pf->raw_components.append("...");
5f36109e
JS
3184 pf->raw_components.append("}");
3185}
3186
3187
3188void
3189dwarf_pretty_print::recurse_struct_members (Dwarf_Die* type, target_symbol* e,
7d11d8c9 3190 print_format* pf, int& count)
5f36109e 3191{
a80f28d8
JS
3192 /* With inheritance, a subclass may mask member names of parent classes, so
3193 * our search among the inheritance tree must be breadth-first rather than
3194 * depth-first (recursive). The type die is still our starting point. When
3195 * we encounter a masked name, just skip it. */
3196 set<string> dupes;
3197 deque<Dwarf_Die> inheritees(1, *type);
3198 for (; !inheritees.empty(); inheritees.pop_front())
3199 {
dee830d9 3200 Dwarf_Die child, childtype, import;
a80f28d8
JS
3201 if (dwarf_child (&inheritees.front(), &child) == 0)
3202 do
3203 {
3204 target_symbol* e2 = e;
5f36109e 3205
a80f28d8
JS
3206 // skip static members
3207 if (dwarf_hasattr(&child, DW_AT_declaration))
3208 continue;
5f36109e 3209
a80f28d8 3210 int tag = dwarf_tag (&child);
5f36109e 3211
dee830d9
MW
3212 /* Pretend imported units contain members by recursing into
3213 struct_member printing with the same count. */
3214 if (tag == DW_TAG_imported_unit
3215 && dwarf_attr_die (&child, DW_AT_import, &import))
3216 recurse_struct_members (&import, e2, pf, count);
3217
a80f28d8
JS
3218 if (tag != DW_TAG_member && tag != DW_TAG_inheritance)
3219 continue;
5f36109e 3220
a80f28d8 3221 dwarf_attr_die (&child, DW_AT_type, &childtype);
5f36109e 3222
a80f28d8
JS
3223 if (tag == DW_TAG_inheritance)
3224 {
3225 inheritees.push_back(childtype);
3226 continue;
3227 }
5f36109e 3228
a80f28d8
JS
3229 int childtag = dwarf_tag (&childtype);
3230 const char *member = dwarf_diename (&child);
3a147004 3231
a80f28d8
JS
3232 // "_vptr.foo" members are C++ virtual function tables,
3233 // which (generally?) aren't interesting for users.
3234 if (member && startswith(member, "_vptr."))
3235 continue;
3a147004 3236
a80f28d8
JS
3237 // skip inheritance-masked duplicates
3238 if (member && !dupes.insert(member).second)
3239 continue;
64cddf39 3240
a80f28d8
JS
3241 if (++count > 1)
3242 pf->raw_components.append(", ");
64cddf39 3243
a80f28d8
JS
3244 // NB: limit to 32 args; see PR10750 and c_unparser::visit_print_format.
3245 if (pf->args.size() >= 32)
3246 {
3247 pf->raw_components.append("...");
3248 break;
3249 }
3250
3251 if (member)
3252 {
3253 pf->raw_components.append(".");
3254 pf->raw_components.append(member);
5f36109e 3255
a80f28d8
JS
3256 e2 = new target_symbol(*e);
3257 e2->components.push_back (target_symbol::component(e->tok, member));
3258 }
3259 else if (childtag == DW_TAG_union_type)
3260 pf->raw_components.append("<union>");
3261 else if (childtag == DW_TAG_structure_type)
3262 pf->raw_components.append("<class>");
3263 else if (childtag == DW_TAG_class_type)
3264 pf->raw_components.append("<struct>");
3265 pf->raw_components.append("=");
600551ca
JS
3266
3267 if (dwarf_hasattr_integrate (&child, DW_AT_bit_offset))
3268 recurse_bitfield (&childtype, e2, pf);
3269 else
3270 recurse (&childtype, e2, pf);
5f36109e 3271 }
a80f28d8
JS
3272 while (dwarf_siblingof (&child, &child) == 0);
3273 }
5f36109e
JS
3274}
3275
3276
bbee5bb8
JS
3277bool
3278dwarf_pretty_print::print_chars (Dwarf_Die* start_type, target_symbol* e,
3279 print_format* pf)
3280{
3281 Dwarf_Die type;
3282 dw.resolve_unqualified_inner_typedie (start_type, &type, e);
6561d8d1
JS
3283
3284 Dwarf_Attribute attr;
3285 Dwarf_Word encoding = (Dwarf_Word) -1;
3286 dwarf_formudata (dwarf_attr_integrate (&type, DW_AT_encoding, &attr),
3287 &encoding);
3288 switch (encoding)
bbee5bb8 3289 {
6561d8d1
JS
3290 case DW_ATE_UTF:
3291 case DW_ATE_signed_char:
3292 case DW_ATE_unsigned_char:
3293 break;
3294 default:
3295 return false;
3296 }
3297
3298 string function = userspace_p ? "user_string2" : "kernel_string2";
3299 Dwarf_Word size = (Dwarf_Word) -1;
3300 dwarf_formudata (dwarf_attr_integrate (&type, DW_AT_byte_size, &attr), &size);
3301 switch (size)
3302 {
3303 case 1:
3304 break;
3305 case 2:
3306 function += "_utf16";
3307 break;
3308 case 4:
3309 function += "_utf32";
3310 break;
3311 default:
3312 return false;
3313 }
3314
3315 if (push_deref (pf, "\"%s\"", e))
3316 {
3317 // steal the last arg for a string access
3318 assert (!pf->args.empty());
3319 functioncall* fcall = new functioncall;
3320 fcall->tok = e->tok;
3321 fcall->function = function;
3322 fcall->args.push_back (pf->args.back());
3323 expression *err_msg = new literal_string ("<unknown>");
3324 err_msg->tok = e->tok;
3325 fcall->args.push_back (err_msg);
3326 pf->args.back() = fcall;
bbee5bb8 3327 }
6561d8d1 3328 return true;
bbee5bb8
JS
3329}
3330
a5ce5211
MW
3331// PR10601: adapt to kernel-vs-userspace loc2c-runtime
3332static const string EMBEDDED_FETCH_DEREF_KERNEL = string("\n")
f1e8e7e0
MW
3333 + "#define fetch_register k_fetch_register\n"
3334 + "#define store_register k_store_register\n"
3335 + "#define deref kderef\n"
3336 + "#define store_deref store_kderef\n";
a5ce5211
MW
3337
3338static const string EMBEDDED_FETCH_DEREF_USER = string("\n")
f1e8e7e0
MW
3339 + "#define fetch_register u_fetch_register\n"
3340 + "#define store_register u_store_register\n"
3341 + "#define deref uderef\n"
3342 + "#define store_deref store_uderef\n";
a5ce5211
MW
3343
3344#define EMBEDDED_FETCH_DEREF(U) \
f1e8e7e0 3345 (U ? EMBEDDED_FETCH_DEREF_USER : EMBEDDED_FETCH_DEREF_KERNEL)
a5ce5211
MW
3346
3347static const string EMBEDDED_FETCH_DEREF_DONE = string("\n")
f1e8e7e0
MW
3348 + "#undef fetch_register\n"
3349 + "#undef store_register\n"
3350 + "#undef deref\n"
3351 + "#undef store_deref\n";
bbee5bb8 3352
1c0be8c7
JS
3353static functioncall*
3354synthetic_embedded_deref_call(systemtap_session& session,
3355 const string& function_name,
3356 const string& function_code,
3357 exp_type function_type,
3358 bool userspace_p,
3359 bool lvalue_p,
3360 target_symbol* e,
3361 expression* pointer=NULL)
3362{
3363 // Synthesize a functiondecl for the given embedded code string.
5f36109e
JS
3364 functiondecl *fdecl = new functiondecl;
3365 fdecl->synthetic = true;
3366 fdecl->tok = e->tok;
1c0be8c7
JS
3367 fdecl->name = function_name;
3368 fdecl->type = function_type;
3369
5f36109e
JS
3370 embeddedcode *ec = new embeddedcode;
3371 ec->tok = e->tok;
1c0be8c7
JS
3372 ec->code += "/* unprivileged */";
3373 if (! lvalue_p)
3374 ec->code += "/* pure */";
3375 ec->code += EMBEDDED_FETCH_DEREF(userspace_p);
3376 ec->code += function_code;
3377 ec->code += EMBEDDED_FETCH_DEREF_DONE;
5f36109e
JS
3378 fdecl->body = ec;
3379
3380 // Synthesize a functioncall.
3381 functioncall* fcall = new functioncall;
3382 fcall->tok = e->tok;
3383 fcall->function = fdecl->name;
1c0be8c7 3384 fcall->type = fdecl->type;
5f36109e 3385
1c0be8c7
JS
3386 // If this code snippet uses a precomputed pointer,
3387 // pass that as the first argument.
5f36109e
JS
3388 if (pointer)
3389 {
5f36109e
JS
3390 vardecl *v = new vardecl;
3391 v->type = pe_long;
3392 v->name = "pointer";
3393 v->tok = e->tok;
3394 fdecl->formal_args.push_back(v);
3395 fcall->args.push_back(pointer);
3396 }
5f36109e 3397
1c0be8c7 3398 // Any non-literal indexes need to be passed as arguments too.
5f36109e
JS
3399 for (unsigned i = 0; i < e->components.size(); ++i)
3400 if (e->components[i].type == target_symbol::comp_expression_array_index)
3401 {
3402 vardecl *v = new vardecl;
3403 v->type = pe_long;
3404 v->name = "index" + lex_cast(i);
3405 v->tok = e->tok;
3406 fdecl->formal_args.push_back(v);
3407 fcall->args.push_back(e->components[i].expr_index);
3408 }
3409
1c0be8c7
JS
3410 // If this code snippet is assigning to an lvalue,
3411 // add a final argument for the rvalue.
3412 if (lvalue_p)
3413 {
3414 // Modify the fdecl so it carries a single pe_long formal
3415 // argument called "value".
5f36109e 3416
1c0be8c7
JS
3417 // FIXME: For the time being we only support setting target
3418 // variables which have base types; these are 'pe_long' in
3419 // stap's type vocabulary. Strings and pointers might be
3420 // reasonable, some day, but not today.
5f36109e 3421
1c0be8c7
JS
3422 vardecl *v = new vardecl;
3423 v->type = pe_long;
3424 v->name = "value";
3425 v->tok = e->tok;
3426 fdecl->formal_args.push_back(v);
3427 // NB: We don't know the value for fcall argument yet.
3428 // (see target_symbol_setter_functioncalls)
3429 }
3430
3431 // Add the synthesized decl to the session, and return the call.
3432 fdecl->join (session);
5f36109e
JS
3433 return fcall;
3434}
3435
1c0be8c7
JS
3436expression*
3437dwarf_pretty_print::deref (target_symbol* e)
3438{
3439 static unsigned tick = 0;
3440
3441 if (!deref_p)
3442 {
3443 assert (pointer && e->components.empty());
3444 return pointer;
3445 }
3446
3447 bool lvalue_p = false;
3448 string name = "_dwarf_pretty_print_deref_" + lex_cast(tick++);
3449
3450 string code;
3451 exp_type type = pe_long;
3452 if (pointer)
3453 code = dw.literal_stmt_for_pointer (&pointer_type, e, false, type);
3454 else if (!local.empty())
3455 code = dw.literal_stmt_for_local (scopes, pc, local, e, false, type);
3456 else
3457 code = dw.literal_stmt_for_return (&scopes[0], pc, e, false, type);
3458
3459 return synthetic_embedded_deref_call(dw.sess, name, code, type,
3460 userspace_p, lvalue_p, e, pointer);
3461}
3462
5f36109e 3463
c55ea10d
JS
3464bool
3465dwarf_pretty_print::push_deref (print_format* pf, const string& fmt,
3466 target_symbol* e)
3467{
3468 expression* e2 = NULL;
3469 try
3470 {
3471 e2 = deref (e);
3472 }
3473 catch (const semantic_error&)
3474 {
3475 pf->raw_components.append ("?");
3476 return false;
3477 }
3478 pf->raw_components.append (fmt);
3479 pf->args.push_back (e2);
3480 return true;
3481}
3482
3483
e57b735a 3484void
a7999c82 3485dwarf_var_expanding_visitor::visit_target_symbol_saved_return (target_symbol* e)
e57b735a 3486{
a7999c82
JS
3487 // Get the full name of the target symbol.
3488 stringstream ts_name_stream;
3489 e->print(ts_name_stream);
3490 string ts_name = ts_name_stream.str();
3491
3492 // Check and make sure we haven't already seen this target
3493 // variable in this return probe. If we have, just return our
3494 // last replacement.
af234c40 3495 map<string, expression *>::iterator i = return_ts_map.find(ts_name);
a7999c82 3496 if (i != return_ts_map.end())
85ecf79a 3497 {
a7999c82
JS
3498 provide (i->second);
3499 return;
3500 }
85ecf79a 3501
70208613
JS
3502 // Attempt the expansion directly first, so if there's a problem with the
3503 // variable we won't have a bogus entry probe lying around. Like in
3504 // saveargs(), we pretend for a moment that we're not in a .return.
3505 bool saved_has_return = q.has_return;
3506 q.has_return = false;
3507 expression *repl = e;
3508 replace (repl);
3509 q.has_return = saved_has_return;
3510 target_symbol* n = dynamic_cast<target_symbol*>(repl);
3511 if (n && n->saved_conversion_error)
3512 {
3513 provide (repl);
3514 return;
3515 }
3516
af234c40
JS
3517 expression *exp;
3518 if (!q.has_process &&
3519 strverscmp(q.sess.kernel_base_release.c_str(), "2.6.25") >= 0)
140be17a 3520 exp = gen_kretprobe_saved_return(repl);
af234c40 3521 else
cc9001af 3522 exp = gen_mapped_saved_return(repl, e->sym_name());
af234c40
JS
3523
3524 // Provide the variable to our parent so it can be used as a
3525 // substitute for the target symbol.
3526 provide (exp);
3527
3528 // Remember this replacement since we might be able to reuse
3529 // it later if the same return probe references this target
3530 // symbol again.
3531 return_ts_map[ts_name] = exp;
3532}
3533
4a2970a3 3534static expression*
23dc94f6
DS
3535gen_mapped_saved_return(systemtap_session &sess, expression* e,
3536 const string& name,
3537 block *& add_block, bool& add_block_tid,
3538 block *& add_call_probe, bool& add_call_probe_tid)
af234c40 3539{
23dc94f6
DS
3540 static unsigned tick = 0;
3541
a7999c82
JS
3542 // We've got to do several things here to handle target
3543 // variables in return probes.
85ecf79a 3544
a7999c82
JS
3545 // (1) Synthesize two global arrays. One is the cache of the
3546 // target variable and the other contains a thread specific
3547 // nesting level counter. The arrays will look like
3548 // this:
3549 //
23dc94f6
DS
3550 // _entry_tvar_{name}_{num}
3551 // _entry_tvar_{name}_{num}_ctr
a7999c82 3552
23dc94f6 3553 string aname = (string("_entry_tvar_")
cc9001af 3554 + name
aca66a36 3555 + "_" + lex_cast(tick++));
a7999c82
JS
3556 vardecl* vd = new vardecl;
3557 vd->name = aname;
3558 vd->tok = e->tok;
23dc94f6 3559 sess.globals.push_back (vd);
a7999c82
JS
3560
3561 string ctrname = aname + "_ctr";
3562 vd = new vardecl;
3563 vd->name = ctrname;
3564 vd->tok = e->tok;
23dc94f6 3565 sess.globals.push_back (vd);
a7999c82
JS
3566
3567 // (2) Create a new code block we're going to insert at the
3568 // beginning of this probe to get the cached value into a
3569 // temporary variable. We'll replace the target variable
3570 // reference with the temporary variable reference. The code
3571 // will look like this:
3572 //
23dc94f6
DS
3573 // _entry_tvar_tid = tid()
3574 // _entry_tvar_{name}_{num}_tmp
3575 // = _entry_tvar_{name}_{num}[_entry_tvar_tid,
3576 // _entry_tvar_{name}_{num}_ctr[_entry_tvar_tid]]
3577 // delete _entry_tvar_{name}_{num}[_entry_tvar_tid,
3578 // _entry_tvar_{name}_{num}_ctr[_entry_tvar_tid]--]
3579 // if (! _entry_tvar_{name}_{num}_ctr[_entry_tvar_tid])
3580 // delete _entry_tvar_{name}_{num}_ctr[_entry_tvar_tid]
a7999c82
JS
3581
3582 // (2a) Synthesize the tid temporary expression, which will look
3583 // like this:
3584 //
23dc94f6 3585 // _entry_tvar_tid = tid()
a7999c82 3586 symbol* tidsym = new symbol;
23dc94f6 3587 tidsym->name = string("_entry_tvar_tid");
a7999c82 3588 tidsym->tok = e->tok;
85ecf79a 3589
a7999c82
JS
3590 if (add_block == NULL)
3591 {
3592 add_block = new block;
3593 add_block->tok = e->tok;
8cc799a5 3594 }
8c819921 3595
8cc799a5
JS
3596 if (!add_block_tid)
3597 {
a7999c82
JS
3598 // Synthesize a functioncall to grab the thread id.
3599 functioncall* fc = new functioncall;
3600 fc->tok = e->tok;
3601 fc->function = string("tid");
8c819921 3602
23dc94f6 3603 // Assign the tid to '_entry_tvar_tid'.
8c819921
DS
3604 assignment* a = new assignment;
3605 a->tok = e->tok;
3606 a->op = "=";
a7999c82
JS
3607 a->left = tidsym;
3608 a->right = fc;
8c819921
DS
3609
3610 expr_statement* es = new expr_statement;
3611 es->tok = e->tok;
3612 es->value = a;
8c819921 3613 add_block->statements.push_back (es);
8cc799a5 3614 add_block_tid = true;
a7999c82 3615 }
8c819921 3616
a7999c82
JS
3617 // (2b) Synthesize an array reference and assign it to a
3618 // temporary variable (that we'll use as replacement for the
3619 // target variable reference). It will look like this:
3620 //
23dc94f6
DS
3621 // _entry_tvar_{name}_{num}_tmp
3622 // = _entry_tvar_{name}_{num}[_entry_tvar_tid,
3623 // _entry_tvar_{name}_{num}_ctr[_entry_tvar_tid]]
a7999c82
JS
3624
3625 arrayindex* ai_tvar_base = new arrayindex;
3626 ai_tvar_base->tok = e->tok;
3627
3628 symbol* sym = new symbol;
3629 sym->name = aname;
3630 sym->tok = e->tok;
3631 ai_tvar_base->base = sym;
3632
3633 ai_tvar_base->indexes.push_back(tidsym);
3634
3635 // We need to create a copy of the array index in its current
3636 // state so we can have 2 variants of it (the original and one
3637 // that post-decrements the second index).
3638 arrayindex* ai_tvar = new arrayindex;
3639 arrayindex* ai_tvar_postdec = new arrayindex;
3640 *ai_tvar = *ai_tvar_base;
3641 *ai_tvar_postdec = *ai_tvar_base;
3642
3643 // Synthesize the
23dc94f6 3644 // "_entry_tvar_{name}_{num}_ctr[_entry_tvar_tid]" used as the
a7999c82
JS
3645 // second index into the array.
3646 arrayindex* ai_ctr = new arrayindex;
3647 ai_ctr->tok = e->tok;
3648
3649 sym = new symbol;
3650 sym->name = ctrname;
3651 sym->tok = e->tok;
3652 ai_ctr->base = sym;
3653 ai_ctr->indexes.push_back(tidsym);
3654 ai_tvar->indexes.push_back(ai_ctr);
3655
3656 symbol* tmpsym = new symbol;
3657 tmpsym->name = aname + "_tmp";
3658 tmpsym->tok = e->tok;
3659
3660 assignment* a = new assignment;
3661 a->tok = e->tok;
3662 a->op = "=";
3663 a->left = tmpsym;
3664 a->right = ai_tvar;
3665
3666 expr_statement* es = new expr_statement;
3667 es->tok = e->tok;
3668 es->value = a;
3669
3670 add_block->statements.push_back (es);
3671
3672 // (2c) Add a post-decrement to the second array index and
3673 // delete the array value. It will look like this:
3674 //
23dc94f6
DS
3675 // delete _entry_tvar_{name}_{num}[_entry_tvar_tid,
3676 // _entry_tvar_{name}_{num}_ctr[_entry_tvar_tid]--]
a7999c82
JS
3677
3678 post_crement* pc = new post_crement;
3679 pc->tok = e->tok;
3680 pc->op = "--";
3681 pc->operand = ai_ctr;
3682 ai_tvar_postdec->indexes.push_back(pc);
3683
3684 delete_statement* ds = new delete_statement;
3685 ds->tok = e->tok;
3686 ds->value = ai_tvar_postdec;
3687
3688 add_block->statements.push_back (ds);
3689
3690 // (2d) Delete the counter value if it is 0. It will look like
3691 // this:
23dc94f6
DS
3692 // if (! _entry_tvar_{name}_{num}_ctr[_entry_tvar_tid])
3693 // delete _entry_tvar_{name}_{num}_ctr[_entry_tvar_tid]
a7999c82
JS
3694
3695 ds = new delete_statement;
3696 ds->tok = e->tok;
3697 ds->value = ai_ctr;
3698
3699 unary_expression *ue = new unary_expression;
3700 ue->tok = e->tok;
3701 ue->op = "!";
3702 ue->operand = ai_ctr;
3703
3704 if_statement *ifs = new if_statement;
3705 ifs->tok = e->tok;
3706 ifs->condition = ue;
3707 ifs->thenblock = ds;
3708 ifs->elseblock = NULL;
3709
3710 add_block->statements.push_back (ifs);
3711
3712 // (3) We need an entry probe that saves the value for us in the
3713 // global array we created. Create the entry probe, which will
3714 // look like this:
3715 //
2260f4e3 3716 // probe kernel.function("{function}").call {
23dc94f6
DS
3717 // _entry_tvar_tid = tid()
3718 // _entry_tvar_{name}_{num}[_entry_tvar_tid,
3719 // ++_entry_tvar_{name}_{num}_ctr[_entry_tvar_tid]]
a7999c82
JS
3720 // = ${param}
3721 // }
3722
2260f4e3 3723 if (add_call_probe == NULL)
a7999c82 3724 {
2260f4e3
FCE
3725 add_call_probe = new block;
3726 add_call_probe->tok = e->tok;
8cc799a5 3727 }
4baf0e53 3728
8cc799a5
JS
3729 if (!add_call_probe_tid)
3730 {
a7999c82
JS
3731 // Synthesize a functioncall to grab the thread id.
3732 functioncall* fc = new functioncall;
3733 fc->tok = e->tok;
3734 fc->function = string("tid");
4baf0e53 3735
23dc94f6 3736 // Assign the tid to '_entry_tvar_tid'.
a7999c82 3737 assignment* a = new assignment;
8fc05e57
DS
3738 a->tok = e->tok;
3739 a->op = "=";
a7999c82
JS
3740 a->left = tidsym;
3741 a->right = fc;
8fc05e57 3742
a7999c82 3743 expr_statement* es = new expr_statement;
8fc05e57
DS
3744 es->tok = e->tok;
3745 es->value = a;
2260f4e3 3746 add_call_probe = new block(add_call_probe, es);
8cc799a5 3747 add_call_probe_tid = true;
85ecf79a 3748 }
cf2a1f85 3749
a7999c82 3750 // Save the value, like this:
23dc94f6
DS
3751 // _entry_tvar_{name}_{num}[_entry_tvar_tid,
3752 // ++_entry_tvar_{name}_{num}_ctr[_entry_tvar_tid]]
a7999c82
JS
3753 // = ${param}
3754 arrayindex* ai_tvar_preinc = new arrayindex;
3755 *ai_tvar_preinc = *ai_tvar_base;
3756
3757 pre_crement* preinc = new pre_crement;
3758 preinc->tok = e->tok;
3759 preinc->op = "++";
3760 preinc->operand = ai_ctr;
3761 ai_tvar_preinc->indexes.push_back(preinc);
3762
3763 a = new assignment;
3764 a->tok = e->tok;
3765 a->op = "=";
3766 a->left = ai_tvar_preinc;
3767 a->right = e;
3768
3769 es = new expr_statement;
3770 es->tok = e->tok;
3771 es->value = a;
3772
2260f4e3 3773 add_call_probe = new block(add_call_probe, es);
a7999c82 3774
23dc94f6 3775 // (4) Provide the '_entry_tvar_{name}_{num}_tmp' variable to
a7999c82
JS
3776 // our parent so it can be used as a substitute for the target
3777 // symbol.
3f803f9e 3778 delete ai_tvar_base;
af234c40
JS
3779 return tmpsym;
3780}
a7999c82 3781
af234c40 3782
23dc94f6
DS
3783expression*
3784dwarf_var_expanding_visitor::gen_mapped_saved_return(expression* e,
3785 const string& name)
3786{
3787 return ::gen_mapped_saved_return(q.sess, e, name, add_block,
3788 add_block_tid, add_call_probe,
3789 add_call_probe_tid);
3790}
3791
3792
af234c40 3793expression*
140be17a 3794dwarf_var_expanding_visitor::gen_kretprobe_saved_return(expression* e)
af234c40
JS
3795{
3796 // The code for this is simple.
3797 //
3798 // .call:
3799 // _set_kretprobe_long(index, $value)
3800 //
3801 // .return:
3802 // _get_kretprobe_long(index)
3803 //
3804 // (or s/long/string/ for things like $$parms)
3805
3806 unsigned index;
3807 string setfn, getfn;
3808
140be17a
JS
3809 // We need the caller to predetermine the type of the expression!
3810 switch (e->type)
af234c40 3811 {
140be17a 3812 case pe_string:
af234c40
JS
3813 index = saved_strings++;
3814 setfn = "_set_kretprobe_string";
3815 getfn = "_get_kretprobe_string";
140be17a
JS
3816 break;
3817 case pe_long:
af234c40
JS
3818 index = saved_longs++;
3819 setfn = "_set_kretprobe_long";
3820 getfn = "_get_kretprobe_long";
140be17a
JS
3821 break;
3822 default:
dc09353a 3823 throw SEMANTIC_ERROR(_("unknown type to save in kretprobe"), e->tok);
af234c40
JS
3824 }
3825
3826 // Create the entry code
3827 // _set_kretprobe_{long|string}(index, $value)
3828
3829 if (add_call_probe == NULL)
3830 {
3831 add_call_probe = new block;
3832 add_call_probe->tok = e->tok;
3833 }
3834
3835 functioncall* set_fc = new functioncall;
3836 set_fc->tok = e->tok;
3837 set_fc->function = setfn;
3838 set_fc->args.push_back(new literal_number(index));
3839 set_fc->args.back()->tok = e->tok;
3840 set_fc->args.push_back(e);
3841
3842 expr_statement* set_es = new expr_statement;
3843 set_es->tok = e->tok;
3844 set_es->value = set_fc;
3845
3846 add_call_probe->statements.push_back(set_es);
3847
3848 // Create the return code
3849 // _get_kretprobe_{long|string}(index)
3850
3851 functioncall* get_fc = new functioncall;
3852 get_fc->tok = e->tok;
3853 get_fc->function = getfn;
3854 get_fc->args.push_back(new literal_number(index));
3855 get_fc->args.back()->tok = e->tok;
3856
3857 return get_fc;
a7999c82 3858}
a43ba433 3859
2cb3fe26 3860
a7999c82
JS
3861void
3862dwarf_var_expanding_visitor::visit_target_symbol_context (target_symbol* e)
3863{
b78a0fbb 3864 if (null_die(scope_die)) {
04c8e51b
JL
3865 literal_string *empty = new literal_string("");
3866 empty->tok = e->tok;
3867 provide(empty);
a7999c82 3868 return;
b78a0fbb 3869 }
2cb3fe26 3870
5f36109e
JS
3871 target_symbol *tsym = new target_symbol(*e);
3872
fde50242
JS
3873 bool pretty = (!e->components.empty() &&
3874 e->components[0].type == target_symbol::comp_pretty_print);
3875 string format = pretty ? "=%s" : "=%#x";
a43ba433 3876
a7999c82
JS
3877 // Convert $$parms to sprintf of a list of parms and active local vars
3878 // which we recursively evaluate
a43ba433 3879
1c922ad7 3880 print_format* pf = print_format::create(e->tok, "sprintf");
a7999c82 3881
277c21bc 3882 if (q.has_return && (e->name == "$$return"))
a7999c82 3883 {
277c21bc 3884 tsym->name = "$return";
a7999c82
JS
3885
3886 // Ignore any variable that isn't accessible.
3887 tsym->saved_conversion_error = 0;
3888 expression *texp = tsym;
8b095b45 3889 replace (texp); // NB: throws nothing ...
a7999c82 3890 if (tsym->saved_conversion_error) // ... but this is how we know it happened.
a43ba433 3891 {
2cb3fe26 3892
a43ba433
FCE
3893 }
3894 else
3895 {
a7999c82 3896 pf->raw_components += "return";
5f36109e 3897 pf->raw_components += format;
a7999c82
JS
3898 pf->args.push_back(texp);
3899 }
3900 }
3901 else
3902 {
3903 // non-.return probe: support $$parms, $$vars, $$locals
345bbb3d 3904 bool first = true;
a7999c82 3905 Dwarf_Die result;
d48bc7eb
JS
3906 vector<Dwarf_Die> scopes = q.dw.getscopes(scope_die);
3907 for (unsigned i = 0; i < scopes.size(); ++i)
3908 {
3909 if (dwarf_tag(&scopes[i]) == DW_TAG_compile_unit)
3910 break; // we don't want file-level variables
3911 if (dwarf_child (&scopes[i], &result) == 0)
3912 do
00cf3709 3913 {
d48bc7eb
JS
3914 switch (dwarf_tag (&result))
3915 {
3916 case DW_TAG_variable:
3917 if (e->name == "$$parms")
3918 continue;
3919 break;
3920 case DW_TAG_formal_parameter:
3921 if (e->name == "$$locals")
3922 continue;
3923 break;
3924
3925 default:
3926 continue;
3927 }
41c262f3 3928
d48bc7eb
JS
3929 const char *diename = dwarf_diename (&result);
3930 if (! diename) continue;
f76427a2 3931
d48bc7eb
JS
3932 if (! first)
3933 pf->raw_components += " ";
3934 pf->raw_components += diename;
fde50242
JS
3935 first = false;
3936
3937 // Write a placeholder for ugly aggregates
3938 Dwarf_Die type;
3939 if (!pretty && dwarf_attr_die(&result, DW_AT_type, &type))
3940 {
3941 q.dw.resolve_unqualified_inner_typedie(&type, &type, e);
3942 switch (dwarf_tag(&type))
3943 {
3944 case DW_TAG_union_type:
3945 case DW_TAG_structure_type:
3946 case DW_TAG_class_type:
3947 pf->raw_components += "={...}";
3948 continue;
3949
3950 case DW_TAG_array_type:
3951 pf->raw_components += "=[...]";
3952 continue;
3953 }
3954 }
345bbb3d 3955
d48bc7eb
JS
3956 tsym->name = "$";
3957 tsym->name += diename;
41c262f3 3958
d48bc7eb
JS
3959 // Ignore any variable that isn't accessible.
3960 tsym->saved_conversion_error = 0;
3961 expression *texp = tsym;
3962 replace (texp); // NB: throws nothing ...
3963 if (tsym->saved_conversion_error) // ... but this is how we know it happened.
a43ba433 3964 {
d48bc7eb
JS
3965 if (q.sess.verbose>2)
3966 {
e26c2f83 3967 for (const semantic_error *c = tsym->saved_conversion_error;
d48bc7eb 3968 c != 0;
c081af73 3969 c = c->get_chain()) {
4c5d9906 3970 clog << _("variable location problem [man error::dwarf]: ") << c->what() << endl;
d48bc7eb
JS
3971 }
3972 }
3973
3974 pf->raw_components += "=?";
a43ba433 3975 }
d48bc7eb
JS
3976 else
3977 {
3978 pf->raw_components += format;
3979 pf->args.push_back(texp);
3980 }
a7999c82 3981 }
d48bc7eb
JS
3982 while (dwarf_siblingof (&result, &result) == 0);
3983 }
a7999c82 3984 }
2cb3fe26 3985
a7999c82 3986 pf->components = print_format::string_to_components(pf->raw_components);
140be17a 3987 pf->type = pe_string;
a7999c82
JS
3988 provide (pf);
3989}
3990
2cb3fe26 3991
bd1fcbad
YZ
3992void
3993dwarf_var_expanding_visitor::visit_atvar_op (atvar_op *e)
3994{
3995 // Fill in our current module context if needed
3996 if (e->module.empty())
3997 e->module = q.dw.module_name;
3998
3999 if (e->module == q.dw.module_name && e->cu_name.empty())
4000 {
4001 // process like any other local
4002 // e->sym_name() will do the right thing
4003 visit_target_symbol(e);
4004 return;
4005 }
4006
4007 var_expanding_visitor::visit_atvar_op(e);
4008}
4009
4010
a7999c82
JS
4011void
4012dwarf_var_expanding_visitor::visit_target_symbol (target_symbol *e)
4013{
bd1fcbad 4014 assert(e->name.size() > 0 && (e->name[0] == '$' || e->name == "@var"));
a7999c82 4015 visited = true;
30263a73
FCE
4016 bool defined_being_checked = (defined_ops.size() > 0 && (defined_ops.top()->operand == e));
4017 // In this mode, we avoid hiding errors or generating extra code such as for .return saved $vars
a7999c82 4018
70208613 4019 try
a7999c82 4020 {
c69a87e0
FCE
4021 bool lvalue = is_active_lvalue(e);
4022 if (lvalue && !q.sess.guru_mode)
dc09353a 4023 throw SEMANTIC_ERROR(_("write to target variable not permitted; need stap -g"), e->tok);
2cb3fe26 4024
100a540e 4025 // XXX: process $context vars should be writable
70208613 4026
c69a87e0
FCE
4027 // See if we need to generate a new probe to save/access function
4028 // parameters from a return probe. PR 1382.
4029 if (q.has_return
4030 && !defined_being_checked
277c21bc
JS
4031 && e->name != "$return" // not the special return-value variable handled below
4032 && e->name != "$$return") // nor the other special variable handled below
c69a87e0
FCE
4033 {
4034 if (lvalue)
dc09353a 4035 throw SEMANTIC_ERROR(_("write to target variable not permitted in .return probes"), e->tok);
c69a87e0
FCE
4036 visit_target_symbol_saved_return(e);
4037 return;
4038 }
e57b735a 4039
277c21bc
JS
4040 if (e->name == "$$vars" || e->name == "$$parms" || e->name == "$$locals"
4041 || (q.has_return && (e->name == "$$return")))
c69a87e0
FCE
4042 {
4043 if (lvalue)
dc09353a 4044 throw SEMANTIC_ERROR(_("cannot write to context variable"), e->tok);
70208613 4045
c69a87e0 4046 if (e->addressof)
dc09353a 4047 throw SEMANTIC_ERROR(_("cannot take address of context variable"), e->tok);
70208613 4048
5f36109e
JS
4049 e->assert_no_components("dwarf", true);
4050
c69a87e0
FCE
4051 visit_target_symbol_context(e);
4052 return;
4053 }
70208613 4054
04c8e51b
JL
4055 // Everything else (pretty-printed vars, and context vars) require a
4056 // scope_die in which to search for them. If we don't have that, just
4057 // leave it unresolved; we'll produce an error later on.
4058 if (null_die(scope_die))
4059 {
4060 provide(e);
4061 return;
4062 }
4063
5f36109e
JS
4064 if (!e->components.empty() &&
4065 e->components.back().type == target_symbol::comp_pretty_print)
4066 {
4067 if (lvalue)
dc09353a 4068 throw SEMANTIC_ERROR(_("cannot write to pretty-printed variable"), e->tok);
5f36109e 4069
277c21bc 4070 if (q.has_return && (e->name == "$return"))
5f36109e
JS
4071 {
4072 dwarf_pretty_print dpp (q.dw, scope_die, addr,
4073 q.has_process, *e);
4074 dpp.expand()->visit(this);
4075 }
4076 else
4077 {
4078 dwarf_pretty_print dpp (q.dw, getscopes(e), addr,
cc9001af 4079 e->sym_name(),
5f36109e
JS
4080 q.has_process, *e);
4081 dpp.expand()->visit(this);
4082 }
4083 return;
4084 }
4085
1c0be8c7 4086 bool userspace_p = q.has_process;
c69a87e0 4087 string fname = (string(lvalue ? "_dwarf_tvar_set" : "_dwarf_tvar_get")
cc9001af 4088 + "_" + e->sym_name()
c69a87e0 4089 + "_" + lex_cast(tick++));
70208613 4090
70208613 4091
1c0be8c7
JS
4092 exp_type type = pe_long;
4093 string code;
277c21bc 4094 if (q.has_return && (e->name == "$return"))
1c0be8c7 4095 code = q.dw.literal_stmt_for_return (scope_die, addr, e, lvalue, type);
e19fda4e 4096 else
1c0be8c7
JS
4097 code = q.dw.literal_stmt_for_local (getscopes(e), addr, e->sym_name(),
4098 e, lvalue, type);
70208613 4099
1c0be8c7
JS
4100 functioncall* n = synthetic_embedded_deref_call(q.sess, fname, code, type,
4101 userspace_p, lvalue, e);
70208613 4102
c69a87e0
FCE
4103 if (lvalue)
4104 {
4105 // Provide the functioncall to our parent, so that it can be
4106 // used to substitute for the assignment node immediately above
4107 // us.
4108 assert(!target_symbol_setter_functioncalls.empty());
4109 *(target_symbol_setter_functioncalls.top()) = n;
4110 }
70208613 4111
1c0be8c7
JS
4112 // Revisit the functioncall so arguments can be expanded.
4113 n->visit (this);
66d284f4
FCE
4114 }
4115 catch (const semantic_error& er)
4116 {
9fab2262
JS
4117 // We suppress this error message, and pass the unresolved
4118 // target_symbol to the next pass. We hope that this value ends
4119 // up not being referenced after all, so it can be optimized out
4120 // quietly.
1af1e62d 4121 e->chain (er);
9fab2262 4122 provide (e);
66d284f4 4123 }
77de5e9e
GH
4124}
4125
4126
c24447be
JS
4127void
4128dwarf_var_expanding_visitor::visit_cast_op (cast_op *e)
4129{
4130 // Fill in our current module context if needed
4131 if (e->module.empty())
4132 e->module = q.dw.module_name;
4133
4134 var_expanding_visitor::visit_cast_op(e);
4135}
4136
4137
8cc799a5
JS
4138void
4139dwarf_var_expanding_visitor::visit_entry_op (entry_op *e)
4140{
4141 expression *repl = e;
4142 if (q.has_return)
4143 {
4144 // expand the operand as if it weren't a return probe
4145 q.has_return = false;
4146 replace (e->operand);
4147 q.has_return = true;
4148
4149 // XXX it would be nice to use gen_kretprobe_saved_return when available,
4150 // but it requires knowing the types already, which is problematic for
4151 // arbitrary expressons.
cc9001af 4152 repl = gen_mapped_saved_return (e->operand, "entry");
8cc799a5
JS
4153 }
4154 provide (repl);
4155}
4156
3689db05
SC
4157void
4158dwarf_var_expanding_visitor::visit_perf_op (perf_op *e)
4159{
ace7c23f 4160 string e_lit_val = e->operand->value;
b78a0fbb 4161
3689db05 4162 add_block = new block;
9eaaceaa 4163 add_block->tok = e->tok;
3689db05
SC
4164
4165 systemtap_session &s = this->q.sess;
4166 map<string, pair<string,derived_probe*> >::iterator it;
4167 // Find the associated perf.counter probe
4168 for (it=s.perf_counters.begin();
4169 it != s.perf_counters.end();
4170 it++)
4171 if ((*it).first == e_lit_val)
4172 {
4173 // if perf .function("name") omitted, then set it to this process name
4174 if ((*it).second.first.length() == 0)
4175 ((*it).second).first = this->q.user_path;
4176 if (((*it).second).first == this->q.user_path)
4177 break;
4178 }
4179
4180 if (it != s.perf_counters.end())
4181 {
698de6cc 4182 perf_counter_refs.insert((*it).second.second);
3689db05
SC
4183 // __perf_read_N is assigned in the probe prologue
4184 symbol* sym = new symbol;
4653caf1 4185 sym->tok = e->tok;
3689db05
SC
4186 sym->name = "__perf_read_" + (*it).first;
4187 provide (sym);
4188 }
4189 else
dc09353a 4190 throw SEMANTIC_ERROR(_F("perf counter '%s' not defined", e_lit_val.c_str()));
3689db05
SC
4191}
4192
8cc799a5 4193
729455a7
JS
4194vector<Dwarf_Die>&
4195dwarf_var_expanding_visitor::getscopes(target_symbol *e)
4196{
4197 if (scopes.empty())
4198 {
04c8e51b 4199 if(!null_die(scope_die))
f25a9197 4200 scopes = q.dw.getscopes(scope_die);
729455a7 4201 if (scopes.empty())
b530b5b3
LB
4202 //throw semantic_error (_F("unable to find any scopes containing %d", addr), e->tok);
4203 // ((scope_die == NULL) ? "" : (string (" in ") + (dwarf_diename(scope_die) ?: "<unknown>") + "(" + (dwarf_diename(q.dw.cu) ?: "<unknown>") ")" ))
dc09353a 4204 throw SEMANTIC_ERROR ("unable to find any scopes containing "
729455a7 4205 + lex_cast_hex(addr)
04c8e51b 4206 + (null_die(scope_die) ? ""
729455a7
JS
4207 : (string (" in ")
4208 + (dwarf_diename(scope_die) ?: "<unknown>")
4209 + "(" + (dwarf_diename(q.dw.cu) ?: "<unknown>")
4210 + ")"))
4211 + " while searching for local '"
cc9001af 4212 + e->sym_name() + "'",
729455a7
JS
4213 e->tok);
4214 }
4215 return scopes;
4216}
4217
4218
5f36109e
JS
4219struct dwarf_cast_expanding_visitor: public var_expanding_visitor
4220{
4221 systemtap_session& s;
4222 dwarf_builder& db;
4223
4224 dwarf_cast_expanding_visitor(systemtap_session& s, dwarf_builder& db):
4225 s(s), db(db) {}
4226 void visit_cast_op (cast_op* e);
4227 void filter_special_modules(string& module);
4228};
4229
4230
c4ce66a1
JS
4231struct dwarf_cast_query : public base_query
4232{
946e1a48 4233 cast_op& e;
c4ce66a1 4234 const bool lvalue;
5f36109e
JS
4235 const bool userspace_p;
4236 functioncall*& result;
c4ce66a1 4237
5f36109e
JS
4238 dwarf_cast_query(dwflpp& dw, const string& module, cast_op& e, bool lvalue,
4239 const bool userspace_p, functioncall*& result):
abb41d92 4240 base_query(dw, module), e(e), lvalue(lvalue),
5f36109e 4241 userspace_p(userspace_p), result(result) {}
c4ce66a1
JS
4242
4243 void handle_query_module();
822a6a3d 4244 void query_library (const char *) {}
576eaefe 4245 void query_plt (const char *entry, size_t addr) {}
c4ce66a1
JS
4246};
4247
4248
c4ce66a1
JS
4249void
4250dwarf_cast_query::handle_query_module()
4251{
5f36109e
JS
4252 static unsigned tick = 0;
4253
4254 if (result)
c4ce66a1
JS
4255 return;
4256
ea1e477a 4257 // look for the type in any CU
a44a7cb5
JS
4258 Dwarf_Die* type_die = NULL;
4259 if (startswith(e.type_name, "class "))
4260 {
4261 // normalize to match dwflpp::global_alias_caching_callback
4262 string struct_name = "struct " + e.type_name.substr(6);
4263 type_die = dw.declaration_resolve_other_cus(struct_name);
4264 }
4265 else
4266 type_die = dw.declaration_resolve_other_cus(e.type_name);
4267
4268 // NB: We now index the types as "struct name"/"union name"/etc. instead of
4269 // just "name". But since we didn't require users to be explicit before, and
4270 // actually sort of discouraged it, we must be flexible now. So if a lookup
4271 // fails with a bare name, try augmenting it.
4272 if (!type_die &&
4273 !startswith(e.type_name, "class ") &&
4274 !startswith(e.type_name, "struct ") &&
4275 !startswith(e.type_name, "union ") &&
4276 !startswith(e.type_name, "enum "))
4277 {
4278 type_die = dw.declaration_resolve_other_cus("struct " + e.type_name);
4279 if (!type_die)
4280 type_die = dw.declaration_resolve_other_cus("union " + e.type_name);
4281 if (!type_die)
4282 type_die = dw.declaration_resolve_other_cus("enum " + e.type_name);
4283 }
4284
ea1e477a
JS
4285 if (!type_die)
4286 return;
c4ce66a1 4287
5f36109e
JS
4288 string code;
4289 exp_type type = pe_long;
4290
ea1e477a 4291 try
c4ce66a1 4292 {
ea1e477a
JS
4293 Dwarf_Die cu_mem;
4294 dw.focus_on_cu(dwarf_diecu(type_die, &cu_mem, NULL, NULL));
5f36109e
JS
4295
4296 if (!e.components.empty() &&
4297 e.components.back().type == target_symbol::comp_pretty_print)
4298 {
4299 if (lvalue)
dc09353a 4300 throw SEMANTIC_ERROR(_("cannot write to pretty-printed variable"), e.tok);
5f36109e 4301
d19a9a82 4302 dwarf_pretty_print dpp(dw, type_die, e.operand, true, userspace_p, e);
5f36109e
JS
4303 result = dpp.expand();
4304 return;
4305 }
4306
4307 code = dw.literal_stmt_for_pointer (type_die, &e, lvalue, type);
ea1e477a
JS
4308 }
4309 catch (const semantic_error& er)
4310 {
4311 // NB: we can have multiple errors, since a @cast
1af1e62d
JS
4312 // may be attempted using several different modules:
4313 // @cast(ptr, "type", "module1:module2:...")
4314 e.chain (er);
c4ce66a1 4315 }
c4ce66a1 4316
5f36109e
JS
4317 if (code.empty())
4318 return;
c4ce66a1 4319
5f36109e 4320 string fname = (string(lvalue ? "_dwarf_cast_set" : "_dwarf_cast_get")
cc9001af 4321 + "_" + e.sym_name()
5f36109e 4322 + "_" + lex_cast(tick++));
c4ce66a1 4323
1c0be8c7
JS
4324 result = synthetic_embedded_deref_call(dw.sess, fname, code, type,
4325 userspace_p, lvalue, &e, e.operand);
5f36109e 4326}
c4ce66a1
JS
4327
4328
fb0274bc
JS
4329void dwarf_cast_expanding_visitor::filter_special_modules(string& module)
4330{
d90053e7 4331 // look for "<path/to/header>" or "kernel<path/to/header>"
fb0274bc 4332 // for those cases, build a module including that header
d90053e7 4333 if (module[module.size() - 1] == '>' &&
60d98537 4334 (module[0] == '<' || startswith(module, "kernel<")))
fb0274bc
JS
4335 {
4336 string cached_module;
4337 if (s.use_cache)
4338 {
4339 // see if the cached module exists
a2639cb7 4340 cached_module = find_typequery_hash(s, module);
d105f664 4341 if (!cached_module.empty() && !s.poison_cache)
fb0274bc
JS
4342 {
4343 int fd = open(cached_module.c_str(), O_RDONLY);
4344 if (fd != -1)
4345 {
4346 if (s.verbose > 2)
b530b5b3
LB
4347 //TRANSLATORS: Here we're using a cached module.
4348 clog << _("Pass 2: using cached ") << cached_module << endl;
fb0274bc
JS
4349 module = cached_module;
4350 close(fd);
4351 return;
4352 }
4353 }
4354 }
4355
4356 // no cached module, time to make it
d90053e7 4357 if (make_typequery(s, module) == 0)
fb0274bc 4358 {
e16dc041 4359 // try to save typequery in the cache
fb0274bc 4360 if (s.use_cache)
e16dc041 4361 copy_file(module, cached_module, s.verbose > 2);
fb0274bc
JS
4362 }
4363 }
4364}
4365
4366
c4ce66a1
JS
4367void dwarf_cast_expanding_visitor::visit_cast_op (cast_op* e)
4368{
4369 bool lvalue = is_active_lvalue(e);
4370 if (lvalue && !s.guru_mode)
dc09353a 4371 throw SEMANTIC_ERROR(_("write to @cast context variable not permitted; need stap -g"), e->tok);
c4ce66a1
JS
4372
4373 if (e->module.empty())
4374 e->module = "kernel"; // "*" may also be reasonable to search all kernel modules
4375
5f36109e 4376 functioncall* result = NULL;
8b31197b
JS
4377
4378 // split the module string by ':' for alternatives
4379 vector<string> modules;
4380 tokenize(e->module, modules, ":");
b5a0dd41 4381 bool userspace_p=false; // PR10601
5f36109e 4382 for (unsigned i = 0; !result && i < modules.size(); ++i)
c4ce66a1 4383 {
8b31197b 4384 string& module = modules[i];
fb0274bc 4385 filter_special_modules(module);
abb41d92 4386
c4ce66a1
JS
4387 // NB: This uses '/' to distinguish between kernel modules and userspace,
4388 // which means that userspace modules won't get any PATH searching.
4389 dwflpp* dw;
707bf35e
JS
4390 try
4391 {
b5a0dd41
FCE
4392 userspace_p=is_user_module (module);
4393 if (! userspace_p)
707bf35e
JS
4394 {
4395 // kernel or kernel module target
ae2552da 4396 dw = db.get_kern_dw(s, module);
707bf35e
JS
4397 }
4398 else
4399 {
05fb3e0c 4400 module = find_executable (module, "", s.sysenv); // canonicalize it
707bf35e
JS
4401 dw = db.get_user_dw(s, module);
4402 }
4403 }
4404 catch (const semantic_error& er)
4405 {
4406 /* ignore and go to the next module */
4407 continue;
4408 }
c4ce66a1 4409
5f36109e 4410 dwarf_cast_query q (*dw, module, *e, lvalue, userspace_p, result);
06de3a04 4411 dw->iterate_over_modules<base_query>(&query_module, &q);
c4ce66a1 4412 }
abb41d92 4413
5f36109e 4414 if (!result)
c4ce66a1 4415 {
946e1a48
JS
4416 // We pass the unresolved cast_op to the next pass, and hope
4417 // that this value ends up not being referenced after all, so
4418 // it can be optimized out quietly.
c4ce66a1
JS
4419 provide (e);
4420 return;
4421 }
4422
c4ce66a1
JS
4423 if (lvalue)
4424 {
4425 // Provide the functioncall to our parent, so that it can be
4426 // used to substitute for the assignment node immediately above
4427 // us.
4428 assert(!target_symbol_setter_functioncalls.empty());
5f36109e 4429 *(target_symbol_setter_functioncalls.top()) = result;
c4ce66a1
JS
4430 }
4431
5f36109e 4432 result->visit (this);
77de5e9e
GH
4433}
4434
4435
bd1fcbad
YZ
4436struct dwarf_atvar_expanding_visitor: public var_expanding_visitor
4437{
4438 systemtap_session& s;
4439 dwarf_builder& db;
4440
4441 dwarf_atvar_expanding_visitor(systemtap_session& s, dwarf_builder& db):
4442 s(s), db(db) {}
4443 void visit_atvar_op (atvar_op* e);
4444};
4445
4446
4447struct dwarf_atvar_query: public base_query
4448{
4449 atvar_op& e;
4450 const bool userspace_p, lvalue;
4451 functioncall*& result;
4452 unsigned& tick;
4453 const string cu_name_pattern;
4454
4455 dwarf_atvar_query(dwflpp& dw, const string& module, atvar_op& e,
4456 const bool userspace_p, const bool lvalue,
4457 functioncall*& result,
4458 unsigned& tick):
4459 base_query(dw, module), e(e),
4460 userspace_p(userspace_p), lvalue(lvalue), result(result),
4461 tick(tick), cu_name_pattern(string("*/") + e.cu_name) {}
4462
4463 void handle_query_module ();
4464 void query_library (const char *) {}
4465 void query_plt (const char *entry, size_t addr) {}
5c378838 4466 static int atvar_query_cu (Dwarf_Die *cudie, dwarf_atvar_query *q);
bd1fcbad
YZ
4467};
4468
4469
4470int
5c378838 4471dwarf_atvar_query::atvar_query_cu (Dwarf_Die * cudie, dwarf_atvar_query *q)
bd1fcbad 4472{
bd1fcbad
YZ
4473 if (! q->e.cu_name.empty())
4474 {
c60517ca 4475 const char *die_name = dwarf_diename(cudie) ?: "";
bd1fcbad
YZ
4476 if (strcmp(die_name, q->e.cu_name.c_str()) != 0 // Perfect match
4477 && fnmatch(q->cu_name_pattern.c_str(), die_name, 0) != 0)
4478 {
4479 return DWARF_CB_OK;
4480 }
4481 }
4482
4483 try
4484 {
4485 vector<Dwarf_Die> scopes(1, *cudie);
4486
4487 q->dw.focus_on_cu (cudie);
4488
4489 if (! q->e.components.empty() &&
4490 q->e.components.back().type == target_symbol::comp_pretty_print)
4491 {
4492 dwarf_pretty_print dpp (q->dw, scopes, 0, q->e.sym_name(),
4493 q->userspace_p, q->e);
4494 q->result = dpp.expand();
4495 return DWARF_CB_ABORT;
4496 }
4497
4498 exp_type type = pe_long;
4499 string code = q->dw.literal_stmt_for_local (scopes, 0, q->e.sym_name(),
4500 &q->e, q->lvalue, type);
4501
4502 if (code.empty())
4503 return DWARF_CB_OK;
4504
4505 string fname = (string(q->lvalue ? "_dwarf_tvar_set"
4506 : "_dwarf_tvar_get")
4507 + "_" + q->e.sym_name()
4508 + "_" + lex_cast(q->tick++));
4509
4510 q->result = synthetic_embedded_deref_call (q->sess, fname, code, type,
4511 q->userspace_p, q->lvalue,
4512 &q->e);
4513 }
4514 catch (const semantic_error& er)
4515 {
4516 // Here we suppress the error because we often just have too many
4517 // when scanning all the CUs.
4518 return DWARF_CB_OK;
4519 }
4520
4521 if (q->result) {
4522 return DWARF_CB_ABORT;
4523 }
4524
4525 return DWARF_CB_OK;
4526}
4527
4528
4529void
4530dwarf_atvar_query::handle_query_module ()
4531{
4532
4533 dw.iterate_over_cus(atvar_query_cu, this, false);
4534}
4535
4536
4537void
4538dwarf_atvar_expanding_visitor::visit_atvar_op (atvar_op* e)
4539{
4540 const bool lvalue = is_active_lvalue(e);
4541 if (lvalue && !s.guru_mode)
dc09353a 4542 throw SEMANTIC_ERROR(_("write to @var variable not permitted; "
bd1fcbad
YZ
4543 "need stap -g"), e->tok);
4544
4545 if (e->module.empty())
4546 e->module = "kernel";
4547
4548 functioncall* result = NULL;
4549
4550 // split the module string by ':' for alternatives
4551 vector<string> modules;
4552 tokenize(e->module, modules, ":");
4553 bool userspace_p = false;
4554 for (unsigned i = 0; !result && i < modules.size(); ++i)
4555 {
4556 string& module = modules[i];
4557
4558 dwflpp* dw;
4559 try
4560 {
4561 userspace_p = is_user_module(module);
4562 if (!userspace_p)
4563 {
4564 // kernel or kernel module target
4565 dw = db.get_kern_dw(s, module);
4566 }
4567 else
4568 {
4569 module = find_executable(module, "", s.sysenv);
4570 dw = db.get_user_dw(s, module);
4571 }
4572 }
4573 catch (const semantic_error& er)
4574 {
4575 /* ignore and go to the next module */
4576 continue;
4577 }
4578
4579 dwarf_atvar_query q (*dw, module, *e, userspace_p, lvalue, result, tick);
06de3a04 4580 dw->iterate_over_modules<base_query>(&query_module, &q);
bd1fcbad
YZ
4581
4582 if (result)
4583 {
4584 s.unwindsym_modules.insert(module);
bd1fcbad
YZ
4585
4586 if (lvalue)
4587 {
4588 // Provide the functioncall to our parent, so that it can be
4589 // used to substitute for the assignment node immediately above
4590 // us.
4591 assert(!target_symbol_setter_functioncalls.empty());
4592 *(target_symbol_setter_functioncalls.top()) = result;
4593 }
4594
4595 result->visit(this);
4596 return;
4597 }
4598
4599 /* Unable to find the variable in the current module, so we chain
4600 * an error in atvar_op */
dc09353a 4601 semantic_error er(ERR_SRC, _F("unable to find global '%s' in %s%s%s",
bd1fcbad
YZ
4602 e->sym_name().c_str(), module.c_str(),
4603 e->cu_name.empty() ? "" : _(", in "),
4604 e->cu_name.c_str()));
4605 e->chain (er);
4606 }
4607
4608 provide(e);
4609}
4610
4611
b8da0ad1
FCE
4612void
4613dwarf_derived_probe::printsig (ostream& o) const
4614{
4615 // Instead of just printing the plain locations, we add a PC value
4616 // as a comment as a way of telling e.g. apart multiple inlined
4617 // function instances. This is distinct from the verbose/clog
4618 // output, since this part goes into the cache hash calculations.
4619 sole_location()->print (o);
6d0f3f0c 4620 o << " /* pc=" << section << "+0x" << hex << addr << dec << " */";
b8da0ad1
FCE
4621 printsig_nested (o);
4622}
4623
4624
4625
dc38c0ae 4626void
b20febf3
FCE
4627dwarf_derived_probe::join_group (systemtap_session& s)
4628{
af234c40
JS
4629 // skip probes which are paired entry-handlers
4630 if (!has_return && (saved_longs || saved_strings))
4631 return;
4632
b20febf3
FCE
4633 if (! s.dwarf_derived_probes)
4634 s.dwarf_derived_probes = new dwarf_derived_probe_group ();
4635 s.dwarf_derived_probes->enroll (this);
4636}
4637
4638
2b69faaf
JS
4639static bool
4640kernel_supports_inode_uprobes(systemtap_session& s)
4641{
4642 // The arch-supports is new to the builtin inode-uprobes, so it makes a
4643 // reasonable indicator of the new API. Else we'll need an autoconf...
64e807c2 4644 // see also buildrun.cxx:kernel_built_uprobs()
2b69faaf
JS
4645 return (s.kernel_config["CONFIG_ARCH_SUPPORTS_UPROBES"] == "y"
4646 && s.kernel_config["CONFIG_UPROBES"] == "y");
4647}
4648
4649
3667d615
JS
4650static bool
4651kernel_supports_inode_uretprobes(systemtap_session& s)
4652{
766013af
JS
4653 // We need inode-uprobes first, then look for a sign of uretprobes. The only
4654 // non-static function at present is arch_uretprobe_hijack_return_addr.
3667d615 4655 return kernel_supports_inode_uprobes(s) &&
af9e147f 4656 (s.kernel_functions.count("arch_uretprobe_hijack_return_addr") > 0);
3667d615
JS
4657}
4658
4659
5261f7ab
DS
4660void
4661check_process_probe_kernel_support(systemtap_session& s)
4662{
4663 // If we've got utrace, we're good to go.
4664 if (s.kernel_config["CONFIG_UTRACE"] == "y")
4665 return;
4666
8c021542
DS
4667 // We don't have utrace. For process probes that aren't
4668 // uprobes-based, we just need the task_finder. The task_finder
b266d318
DS
4669 // needs CONFIG_TRACEPOINTS and specific tracepoints. There is a
4670 // specific autoconf test for its needs.
8c021542
DS
4671 //
4672 // We'll just require CONFIG_TRACEPOINTS here as a quick-and-dirty
4673 // approximation.
4674 if (! s.need_uprobes && s.kernel_config["CONFIG_TRACEPOINTS"] == "y")
4675 return;
4676
d3e959b0
DS
4677 // For uprobes-based process probes, we need the task_finder plus
4678 // the builtin inode-uprobes.
8c021542
DS
4679 if (s.need_uprobes
4680 && s.kernel_config["CONFIG_TRACEPOINTS"] == "y"
d3e959b0 4681 && kernel_supports_inode_uprobes(s))
8c021542
DS
4682 return;
4683
dc09353a 4684 throw SEMANTIC_ERROR (_("process probes not available without kernel CONFIG_UTRACE or CONFIG_TRACEPOINTS/CONFIG_ARCH_SUPPORTS_UPROBES/CONFIG_UPROBES"));
5261f7ab
DS
4685}
4686
4687
b20febf3
FCE
4688dwarf_derived_probe::dwarf_derived_probe(const string& funcname,
4689 const string& filename,
4690 int line,
91af0778 4691 // module & section specify a relocation
b20febf3
FCE
4692 // base for <addr>, unless section==""
4693 // (equivalently module=="kernel")
4694 const string& module,
4695 const string& section,
4696 // NB: dwfl_addr is the virtualized
4697 // address for this symbol.
4698 Dwarf_Addr dwfl_addr,
4699 // addr is the section-offset for
4700 // actual relocation.
4701 Dwarf_Addr addr,
4702 dwarf_query& q,
37ebca01 4703 Dwarf_Die* scope_die /* may be null */)
4c5d1300 4704 : derived_probe (q.base_probe, q.base_loc, true /* .components soon rewritten */ ),
b20febf3 4705 module (module), section (section), addr (addr),
63b4fd14 4706 path (q.path),
27dc09b1 4707 has_process (q.has_process),
c9bad430
DS
4708 has_return (q.has_return),
4709 has_maxactive (q.has_maxactive),
c57ea854 4710 has_library (q.has_library),
6b66b9f7 4711 maxactive_val (q.maxactive_val),
b642c901
SC
4712 user_path (q.user_path),
4713 user_lib (q.user_lib),
af234c40 4714 access_vars(false),
c57ea854 4715 saved_longs(0), saved_strings(0),
af234c40 4716 entry_handler(0)
bd2b1e68 4717{
b642c901
SC
4718 if (user_lib.size() != 0)
4719 has_library = true;
4720
6b66b9f7
JS
4721 if (q.has_process)
4722 {
4723 // We may receive probes on two types of ELF objects: ET_EXEC or ET_DYN.
4724 // ET_EXEC ones need no further relocation on the addr(==dwfl_addr), whereas
4725 // ET_DYN ones do (addr += run-time mmap base address). We tell these apart
4726 // by the incoming section value (".absolute" vs. ".dynamic").
4727 // XXX Assert invariants here too?
2b69faaf
JS
4728
4729 // inode-uprobes needs an offset rather than an absolute VM address.
4441e344 4730 // ditto for userspace runtimes (dyninst)
ac3af990 4731 if ((kernel_supports_inode_uprobes(q.dw.sess) || q.dw.sess.runtime_usermode_p()) &&
2b69faaf
JS
4732 section == ".absolute" && addr == dwfl_addr &&
4733 addr >= q.dw.module_start && addr < q.dw.module_end)
4734 this->addr = addr - q.dw.module_start;
6b66b9f7
JS
4735 }
4736 else
4737 {
4738 // Assert kernel relocation invariants
4739 if (section == "" && dwfl_addr != addr) // addr should be absolute
dc09353a 4740 throw SEMANTIC_ERROR (_("missing relocation basis"), tok);
6b66b9f7 4741 if (section != "" && dwfl_addr == addr) // addr should be an offset
dc09353a 4742 throw SEMANTIC_ERROR (_("inconsistent relocation address"), tok);
6b66b9f7 4743 }
2930abc7 4744
21beacc9
FCE
4745 // XXX: hack for strange g++/gcc's
4746#ifndef USHRT_MAX
4747#define USHRT_MAX 32767
4748#endif
4749
606fd9c8 4750 // Range limit maxactive() value
6b66b9f7 4751 if (has_maxactive && (maxactive_val < 0 || maxactive_val > USHRT_MAX))
dc09353a 4752 throw SEMANTIC_ERROR (_F("maxactive value out of range [0,%s]",
b530b5b3 4753 lex_cast(USHRT_MAX).c_str()), q.base_loc->components.front()->tok);
606fd9c8 4754
a7696882
JL
4755 // Expand target variables in the probe body. Even if the scope_die is
4756 // invalid, we still want to expand things such as $$vars/$$parms/etc...
4757 // (PR15999, PR16473). Access to specific context vars e.g. $argc will not be
4758 // expanded and will produce an error during the typeresolution_info pass.
8fc05e57 4759 {
6b66b9f7 4760 // XXX: user-space deref's for q.has_process!
5a617dc6
JL
4761
4762 // PR14436: if we're expanding target variables in the probe body of a
4763 // .return probe, we need to make the expansion at the postprologue addr
4764 // instead (if any), which is then also the spot where the entry handler
4765 // probe is placed. (Note that at this point, a nonzero prologue_end
4766 // implies that it should be used, i.e. code is unoptimized).
4767 Dwarf_Addr handler_dwfl_addr = dwfl_addr;
4768 if (q.prologue_end != 0 && q.has_return)
4769 {
4770 handler_dwfl_addr = q.prologue_end;
4771 if (q.sess.verbose > 2)
4772 clog << _F("expanding .return vars at prologue_end (0x%s) "
4773 "rather than entrypc (0x%s)\n",
4774 lex_cast_hex(handler_dwfl_addr).c_str(),
4775 lex_cast_hex(dwfl_addr).c_str());
4776 }
4777 dwarf_var_expanding_visitor v (q, scope_die, handler_dwfl_addr);
8b095b45 4778 v.replace (this->body);
3689db05
SC
4779
4780 // Propagate perf.counters so we can emit later
4781 this->perf_counter_refs = v.perf_counter_refs;
4782 // Emit local var used to save the perf counter read value
698de6cc 4783 std::set<derived_probe*>::iterator pcii;
3689db05
SC
4784 for (pcii = v.perf_counter_refs.begin();
4785 pcii != v.perf_counter_refs.end(); pcii++)
4786 {
4787 map<string, pair<string,derived_probe*> >::iterator it;
4788 // Find the associated perf counter probe
4789 for (it=q.sess.perf_counters.begin() ;
4790 it != q.sess.perf_counters.end();
4791 it++)
4792 if ((*it).second.second == (*pcii))
4793 break;
4794 vardecl* vd = new vardecl;
4795 vd->name = "__perf_read_" + (*it).first;
4796 vd->tok = this->tok;
4797 vd->set_arity(0, this->tok);
4798 vd->type = pe_long;
4799 vd->synthetic = true;
4800 this->locals.push_back (vd);
4801 }
4802
4803
6b66b9f7
JS
4804 if (!q.has_process)
4805 access_vars = v.visited;
37ebca01
FCE
4806
4807 // If during target-variable-expanding the probe, we added a new block
4808 // of code, add it to the start of the probe.
4809 if (v.add_block)
ba6f838d 4810 this->body = new block(v.add_block, this->body);
2260f4e3
FCE
4811
4812 // If when target-variable-expanding the probe, we need to synthesize a
4813 // sibling function-entry probe. We don't go through the whole probe derivation
4814 // business (PR10642) that could lead to wildcard/alias resolution, or for that
4815 // dwarf-induced duplication.
4816 if (v.add_call_probe)
37ebca01 4817 {
2260f4e3
FCE
4818 assert (q.has_return && !q.has_call);
4819
4820 // We temporarily replace q.base_probe.
4821 statement* old_body = q.base_probe->body;
4822 q.base_probe->body = v.add_call_probe;
4823 q.has_return = false;
4824 q.has_call = true;
af234c40 4825
da23eceb 4826 if (q.has_process)
5a617dc6
JL
4827 {
4828 // Place handler probe at the same addr as where the vars were
4829 // expanded (which may not be the same addr as the one for the
4830 // main retprobe, PR14436).
4831 Dwarf_Addr handler_addr = addr;
4832 if (handler_dwfl_addr != dwfl_addr)
4833 // adjust section offset by prologue_end-entrypc
4834 handler_addr += handler_dwfl_addr - dwfl_addr;
4835 entry_handler = new uprobe_derived_probe (funcname, filename,
4836 line, module, section,
4837 handler_dwfl_addr,
4838 handler_addr, q,
4839 scope_die);
4840 }
da23eceb 4841 else
af234c40
JS
4842 entry_handler = new dwarf_derived_probe (funcname, filename, line,
4843 module, section, dwfl_addr,
4844 addr, q, scope_die);
4845
4846 saved_longs = entry_handler->saved_longs = v.saved_longs;
4847 saved_strings = entry_handler->saved_strings = v.saved_strings;
4848
4849 q.results.push_back (entry_handler);
2260f4e3
FCE
4850
4851 q.has_return = true;
4852 q.has_call = false;
4853 q.base_probe->body = old_body;
37ebca01 4854 }
a7696882
JL
4855 // Save the local variables for listing mode. If the scope_die is null,
4856 // local vars aren't accessible, so no need to invoke saveargs (PR10820).
bba368c5
JL
4857 if (!null_die(scope_die) &&
4858 q.sess.dump_mode == systemtap_session::dump_matched_probes_vars)
4859 saveargs(q, scope_die, dwfl_addr);
8fc05e57 4860 }
0a98fd42 4861
5d23847d 4862 // Reset the sole element of the "locations" vector as a
b20febf3
FCE
4863 // "reverse-engineered" form of the incoming (q.base_loc) probe
4864 // point. This allows a user to see what function / file / line
4865 // number any particular match of the wildcards.
919debfc
JL
4866
4867 vector<probe_point::component*> comps;
4868 if (q.has_kernel)
4869 comps.push_back (new probe_point::component(TOK_KERNEL));
4870 else if(q.has_module)
4871 comps.push_back (new probe_point::component(TOK_MODULE, new literal_string(module)));
4872 else if(q.has_process)
4873 comps.push_back (new probe_point::component(TOK_PROCESS, new literal_string(module)));
4874 else
4875 assert (0);
4876
4877 string fn_or_stmt;
4878 if (q.has_function_str || q.has_function_num)
e772a6e7 4879 fn_or_stmt = TOK_FUNCTION;
919debfc 4880 else
e772a6e7 4881 fn_or_stmt = TOK_STATEMENT;
919debfc
JL
4882
4883 if (q.has_function_str || q.has_statement_str)
4884 {
79954c0f 4885 string retro_name = q.final_function_name(funcname, filename.c_str(), line);
919debfc
JL
4886 comps.push_back
4887 (new probe_point::component
4888 (fn_or_stmt, new literal_string (retro_name)));
4889 }
4890 else if (q.has_function_num || q.has_statement_num)
4891 {
4892 Dwarf_Addr retro_addr;
4893 if (q.has_function_num)
4894 retro_addr = q.function_num_val;
4895 else
4896 retro_addr = q.statement_num_val;
4897 comps.push_back (new probe_point::component
4898 (fn_or_stmt,
4899 new literal_number(retro_addr, true)));
4900
4901 if (q.has_absolute)
4902 comps.push_back (new probe_point::component (TOK_ABSOLUTE));
4903 }
4904
4905 if (q.has_call)
4906 comps.push_back (new probe_point::component(TOK_CALL));
4907 if (q.has_exported)
4908 comps.push_back (new probe_point::component(TOK_EXPORTED));
4909 if (q.has_inline)
4910 comps.push_back (new probe_point::component(TOK_INLINE));
4911 if (has_return)
4912 comps.push_back (new probe_point::component(TOK_RETURN));
4913 if (has_maxactive)
4914 comps.push_back (new probe_point::component
4915 (TOK_MAXACTIVE, new literal_number(maxactive_val)));
4916
4917 // Overwrite it.
4918 this->sole_location()->components = comps;
e2941743
JL
4919
4920 // if it's a .callee[s[(N)]] call, add checks to the probe body so that the
4921 // user body is only 'triggered' when called from q.callers[N-1], which
4922 // itself is called from q.callers[N-2], etc... I.E.
4923 // callees(N) --> N elements in q.callers --> N checks against [u]stack(0..N-1)
4924 if ((q.has_callee || q.has_callees_num) && q.callers && !q.callers->empty())
4925 {
4926 if (q.sess.verbose > 2)
7761da8b 4927 clog << _F("adding caller checks for callee %s\n", funcname.c_str());
e2941743
JL
4928
4929 // Copy the stack and empty it out
4930 stack<Dwarf_Addr> callers(*q.callers);
4931 for (unsigned level = 1; !callers.empty(); level++,
4932 callers.pop())
4933 {
4934 Dwarf_Addr caller = callers.top();
4935
4936 // We first need to make the caller addr relocatable
4937 string caller_section;
4938 Dwarf_Addr caller_reloc;
4939 if (module == TOK_KERNEL)
4940 { // allow for relocatable kernel (see also add_probe_point())
4941 caller_reloc = caller - q.sess.sym_stext;
4942 caller_section = "_stext";
4943 }
4944 else
4945 caller_reloc = q.dw.relocate_address(caller,
4946 caller_section);
4947
4948 if (q.sess.verbose > 2)
7761da8b
JL
4949 clog << _F("adding caller check [u]stack(%d) == reloc(0x%s)\n",
4950 level, lex_cast_hex(caller_reloc).c_str());
e2941743
JL
4951
4952 // We want to add a statement like this:
6211aed9 4953 // if (!_caller_match(user, mod, sec, addr)) next;
e2941743
JL
4954 // Something similar is done in semantic_pass_conditions()
4955
4956 functioncall* check = new functioncall();
4957 check->tok = this->tok;
6211aed9 4958 check->function = "_caller_match";
e2941743
JL
4959 check->args.push_back(new literal_number(q.has_process));
4960 check->args[0]->tok = this->tok;
4961 check->args.push_back(new literal_number(level));
4962 check->args[1]->tok = this->tok;
4963 check->args.push_back(new literal_string(module));
4964 check->args[2]->tok = this->tok;
4965 check->args.push_back(new literal_string(caller_section));
4966 check->args[3]->tok = this->tok;
4967 check->args.push_back(new literal_number(caller_reloc, true /* hex */));
4968 check->args[4]->tok = this->tok;
4969
4970 unary_expression* notexp = new unary_expression();
4971 notexp->tok = this->tok;
4972 notexp->op = "!";
4973 notexp->operand = check;
4974
4975 if_statement* ifs = new if_statement();
4976 ifs->tok = this->tok;
4977 ifs->thenblock = new next_statement();
4978 ifs->thenblock->tok = this->tok;
4979 ifs->elseblock = NULL;
4980 ifs->condition = notexp;
4981
4982 this->body = new block(ifs, this->body);
4983 }
4984 }
2930abc7
FCE
4985}
4986
bd2b1e68 4987
0a98fd42 4988void
8c67c337
JS
4989dwarf_derived_probe::saveargs(dwarf_query& q, Dwarf_Die* scope_die,
4990 Dwarf_Addr dwfl_addr)
0a98fd42 4991{
9aa8ffce 4992 if (null_die(scope_die))
0a98fd42 4993 return;
0a98fd42 4994
8c67c337 4995 bool verbose = q.sess.verbose > 2;
0a98fd42 4996
8c67c337 4997 if (verbose)
b530b5b3 4998 clog << _F("saveargs: examining '%s' (dieoffset: %#" PRIx64 ")\n", (dwarf_diename(scope_die)?: "unknown"), dwarf_dieoffset(scope_die));
0a98fd42 4999
8c67c337
JS
5000 if (has_return)
5001 {
5002 /* Only save the return value if it has a type. */
5003 string type_name;
5004 Dwarf_Die type_die;
5005 if (dwarf_attr_die (scope_die, DW_AT_type, &type_die) &&
5006 dwarf_type_name(&type_die, type_name))
5007 args.push_back("$return:"+type_name);
5008
5009 else if (verbose)
b530b5b3
LB
5010 clog << _F("saveargs: failed to retrieve type name for return value (dieoffset: %s)\n",
5011 lex_cast_hex(dwarf_dieoffset(scope_die)).c_str());
8c67c337 5012 }
d87623a1 5013
0a98fd42 5014 Dwarf_Die arg;
4ef35696
JS
5015 vector<Dwarf_Die> scopes = q.dw.getscopes(scope_die);
5016 for (unsigned i = 0; i < scopes.size(); ++i)
5017 {
5018 if (dwarf_tag(&scopes[i]) == DW_TAG_compile_unit)
5019 break; // we don't want file-level variables
5020 if (dwarf_child (&scopes[i], &arg) == 0)
5021 do
0a98fd42 5022 {
4ef35696
JS
5023 switch (dwarf_tag (&arg))
5024 {
5025 case DW_TAG_variable:
5026 case DW_TAG_formal_parameter:
5027 break;
0a98fd42 5028
4ef35696
JS
5029 default:
5030 continue;
5031 }
0a98fd42 5032
4ef35696
JS
5033 /* Ignore this local if it has no name. */
5034 const char *arg_name = dwarf_diename (&arg);
5035 if (!arg_name)
8c67c337
JS
5036 {
5037 if (verbose)
b530b5b3
LB
5038 clog << _F("saveargs: failed to retrieve name for local (dieoffset: %s)\n",
5039 lex_cast_hex(dwarf_dieoffset(&arg)).c_str());
8c67c337
JS
5040 continue;
5041 }
4ef35696
JS
5042
5043 if (verbose)
b530b5b3
LB
5044 clog << _F("saveargs: finding location for local '%s' (dieoffset: %s)\n",
5045 arg_name, lex_cast_hex(dwarf_dieoffset(&arg)).c_str());
4ef35696
JS
5046
5047 /* Ignore this local if it has no location (or not at this PC). */
5048 /* NB: It still may not be directly accessible, e.g. if it is an
5049 * aggregate type, implicit_pointer, etc., but the user can later
5050 * figure out how to access the interesting parts. */
45b02a36
FCE
5051
5052 /* XXX: Perhaps saveargs() / listings-mode should work by synthesizing
5053 * several synthetic
5054 * probe foo { $var }
5055 * probes, testing them for overall resolvability.
5056 */
5057
4ef35696
JS
5058 Dwarf_Attribute attr_mem;
5059 if (!dwarf_attr_integrate (&arg, DW_AT_const_value, &attr_mem))
5060 {
5061 Dwarf_Op *expr;
5062 size_t len;
5063 if (!dwarf_attr_integrate (&arg, DW_AT_location, &attr_mem))
5064 {
5065 if (verbose)
b530b5b3
LB
5066 clog << _F("saveargs: failed to resolve the location for local '%s' (dieoffset: %s)\n",
5067 arg_name, lex_cast_hex(dwarf_dieoffset(&arg)).c_str());
4ef35696
JS
5068 continue;
5069 }
5070 else if (!(dwarf_getlocation_addr(&attr_mem, dwfl_addr, &expr,
5071 &len, 1) == 1 && len > 0))
5072 {
45b02a36
FCE
5073 Dwarf_Addr dwfl_addr2 = q.dw.pr15123_retry_addr (dwfl_addr, & arg);
5074 if (!dwfl_addr2 || (!(dwarf_getlocation_addr(&attr_mem, dwfl_addr2, &expr,
5075 &len, 1) == 1 && len > 0))) {
5076 if (verbose)
5077 clog << _F("saveargs: local '%s' (dieoffset: %s) is not available at this address (%s)\n",
5078 arg_name, lex_cast_hex(dwarf_dieoffset(&arg)).c_str(), lex_cast_hex(dwfl_addr).c_str());
5079 continue;
5080 }
4ef35696
JS
5081 }
5082 }
5083
5084 /* Ignore this local if it has no type. */
5085 string type_name;
5086 Dwarf_Die type_die;
5087 if (!dwarf_attr_die (&arg, DW_AT_type, &type_die) ||
5088 !dwarf_type_name(&type_die, type_name))
8c67c337
JS
5089 {
5090 if (verbose)
b530b5b3
LB
5091 clog << _F("saveargs: failed to retrieve type name for local '%s' (dieoffset: %s)\n",
5092 arg_name, lex_cast_hex(dwarf_dieoffset(&arg)).c_str());
8c67c337
JS
5093 continue;
5094 }
8c67c337 5095
4ef35696
JS
5096 /* This local looks good -- save it! */
5097 args.push_back("$"+string(arg_name)+":"+type_name);
8c67c337 5098 }
4ef35696
JS
5099 while (dwarf_siblingof (&arg, &arg) == 0);
5100 }
0a98fd42
JS
5101}
5102
5103
5104void
d0bfd2ac 5105dwarf_derived_probe::getargs(std::list<std::string> &arg_set) const
0a98fd42 5106{
d0bfd2ac 5107 arg_set.insert(arg_set.end(), args.begin(), args.end());
0a98fd42
JS
5108}
5109
5110
27dc09b1 5111void
42e38653 5112dwarf_derived_probe::emit_privilege_assertion (translator_output* o)
27dc09b1
DB
5113{
5114 if (has_process)
5115 {
5116 // These probes are allowed for unprivileged users, but only in the
5117 // context of processes which they own.
5118 emit_process_owner_assertion (o);
5119 return;
5120 }
5121
5122 // Other probes must contain the default assertion which aborts
5123 // if executed by an unprivileged user.
42e38653 5124 derived_probe::emit_privilege_assertion (o);
27dc09b1
DB
5125}
5126
5127
5128void
5129dwarf_derived_probe::print_dupe_stamp(ostream& o)
5130{
5131 if (has_process)
5132 {
5133 // These probes are allowed for unprivileged users, but only in the
5134 // context of processes which they own.
5135 print_dupe_stamp_unprivileged_process_owner (o);
5136 return;
5137 }
5138
5139 // Other probes must contain the default dupe stamp
5140 derived_probe::print_dupe_stamp (o);
5141}
5142
64211010 5143
7a053d3b 5144void
20c6c071 5145dwarf_derived_probe::register_statement_variants(match_node * root,
27dc09b1 5146 dwarf_builder * dw,
42e38653 5147 privilege_t privilege)
bd2b1e68 5148{
27dc09b1 5149 root
42e38653 5150 ->bind_privilege(privilege)
27dc09b1 5151 ->bind(dw);
54efe513
GH
5152}
5153
7a053d3b 5154void
fd6602a0 5155dwarf_derived_probe::register_function_variants(match_node * root,
27dc09b1 5156 dwarf_builder * dw,
42e38653 5157 privilege_t privilege)
2865d17a 5158{
27dc09b1 5159 root
42e38653 5160 ->bind_privilege(privilege)
27dc09b1 5161 ->bind(dw);
27dc09b1 5162 root->bind(TOK_CALL)
42e38653 5163 ->bind_privilege(privilege)
27dc09b1 5164 ->bind(dw);
4bda987e
SC
5165 root->bind(TOK_EXPORTED)
5166 ->bind_privilege(privilege)
5167 ->bind(dw);
27dc09b1 5168 root->bind(TOK_RETURN)
42e38653 5169 ->bind_privilege(privilege)
27dc09b1 5170 ->bind(dw);
1e035395 5171
f6be7c06
DB
5172 // For process probes / uprobes, .maxactive() is unused.
5173 if (! pr_contains (privilege, pr_stapusr))
1e035395
FCE
5174 {
5175 root->bind(TOK_RETURN)
1e035395
FCE
5176 ->bind_num(TOK_MAXACTIVE)->bind(dw);
5177 }
bd2b1e68
GH
5178}
5179
7a053d3b 5180void
27dc09b1 5181dwarf_derived_probe::register_function_and_statement_variants(
440d9b00 5182 systemtap_session& s,
27dc09b1
DB
5183 match_node * root,
5184 dwarf_builder * dw,
42e38653 5185 privilege_t privilege
27dc09b1 5186)
bd2b1e68
GH
5187{
5188 // Here we match 4 forms:
5189 //
5190 // .function("foo")
5191 // .function(0xdeadbeef)
5192 // .statement("foo")
5193 // .statement(0xdeadbeef)
5194
440d9b00 5195 match_node *fv_root = root->bind_str(TOK_FUNCTION);
42e38653 5196 register_function_variants(fv_root, dw, privilege);
7f02ca94 5197 // ROOT.function("STRING") always gets the .inline and .label variants.
440d9b00 5198 fv_root->bind(TOK_INLINE)
42e38653 5199 ->bind_privilege(privilege)
440d9b00 5200 ->bind(dw);
7f02ca94
JS
5201 fv_root->bind_str(TOK_LABEL)
5202 ->bind_privilege(privilege)
440d9b00 5203 ->bind(dw);
c31add50
JL
5204 fv_root->bind_str(TOK_CALLEE)
5205 ->bind_privilege(privilege)
5206 ->bind(dw);
5207 fv_root->bind(TOK_CALLEES)
5208 ->bind_privilege(privilege)
5209 ->bind(dw);
5210 fv_root->bind_num(TOK_CALLEES)
5211 ->bind_privilege(privilege)
5212 ->bind(dw);
440d9b00
DB
5213
5214 fv_root = root->bind_num(TOK_FUNCTION);
42e38653 5215 register_function_variants(fv_root, dw, privilege);
440d9b00
DB
5216 // ROOT.function(NUMBER).inline is deprecated in release 1.7 and removed thereafter.
5217 if (strverscmp(s.compatible.c_str(), "1.7") <= 0)
5218 {
5219 fv_root->bind(TOK_INLINE)
42e38653 5220 ->bind_privilege(privilege)
440d9b00
DB
5221 ->bind(dw);
5222 }
5223
42e38653
DB
5224 register_statement_variants(root->bind_str(TOK_STATEMENT), dw, privilege);
5225 register_statement_variants(root->bind_num(TOK_STATEMENT), dw, privilege);
bd2b1e68
GH
5226}
5227
b1615c74
JS
5228void
5229dwarf_derived_probe::register_sdt_variants(systemtap_session& s,
5230 match_node * root,
5231 dwarf_builder * dw)
5232{
5233 root->bind_str(TOK_MARK)
f66bb29a 5234 ->bind_privilege(pr_all)
b1615c74
JS
5235 ->bind(dw);
5236 root->bind_str(TOK_PROVIDER)->bind_str(TOK_MARK)
f66bb29a 5237 ->bind_privilege(pr_all)
b1615c74
JS
5238 ->bind(dw);
5239}
5240
5241void
5242dwarf_derived_probe::register_plt_variants(systemtap_session& s,
5243 match_node * root,
5244 dwarf_builder * dw)
5245{
5246 root->bind(TOK_PLT)
f66bb29a 5247 ->bind_privilege(pr_all)
b1615c74
JS
5248 ->bind(dw);
5249 root->bind_str(TOK_PLT)
f66bb29a 5250 ->bind_privilege(pr_all)
b1615c74
JS
5251 ->bind(dw);
5252 root->bind(TOK_PLT)->bind_num(TOK_STATEMENT)
f66bb29a 5253 ->bind_privilege(pr_all)
b1615c74
JS
5254 ->bind(dw);
5255 root->bind_str(TOK_PLT)->bind_num(TOK_STATEMENT)
f66bb29a 5256 ->bind_privilege(pr_all)
b1615c74 5257 ->bind(dw);
bd2b1e68
GH
5258}
5259
5260void
c4ce66a1 5261dwarf_derived_probe::register_patterns(systemtap_session& s)
bd2b1e68 5262{
c4ce66a1 5263 match_node* root = s.pattern_root;
bd2b1e68
GH
5264 dwarf_builder *dw = new dwarf_builder();
5265
c4ce66a1
JS
5266 update_visitor *filter = new dwarf_cast_expanding_visitor(s, *dw);
5267 s.code_filters.push_back(filter);
5268
bd1fcbad
YZ
5269 filter = new dwarf_atvar_expanding_visitor(s, *dw);
5270 s.code_filters.push_back(filter);
5271
73f52eb4
DB
5272 register_function_and_statement_variants(s, root->bind(TOK_KERNEL), dw, pr_privileged);
5273 register_function_and_statement_variants(s, root->bind_str(TOK_MODULE), dw, pr_privileged);
27dc09b1
DB
5274 root->bind(TOK_KERNEL)->bind_num(TOK_STATEMENT)->bind(TOK_ABSOLUTE)
5275 ->bind(dw);
2cab6244 5276
7f02ca94
JS
5277 match_node* uprobes[] = {
5278 root->bind(TOK_PROCESS),
5279 root->bind_str(TOK_PROCESS),
5c6f9e92 5280 root->bind_num(TOK_PROCESS),
7f02ca94
JS
5281 root->bind(TOK_PROCESS)->bind_str(TOK_LIBRARY),
5282 root->bind_str(TOK_PROCESS)->bind_str(TOK_LIBRARY),
5283 };
5284 for (size_t i = 0; i < sizeof(uprobes) / sizeof(*uprobes); ++i)
5285 {
f66bb29a 5286 register_function_and_statement_variants(s, uprobes[i], dw, pr_all);
7f02ca94
JS
5287 register_sdt_variants(s, uprobes[i], dw);
5288 register_plt_variants(s, uprobes[i], dw);
5289 }
bd2b1e68
GH
5290}
5291
9020300d 5292void
3689db05
SC
5293dwarf_derived_probe::emit_probe_local_init(systemtap_session& s, translator_output * o)
5294{
698de6cc 5295 std::set<derived_probe*>::iterator pcii;
3689db05
SC
5296 for (pcii = perf_counter_refs.begin();
5297 pcii != perf_counter_refs.end();
5298 pcii++)
5299 {
5300 map<string, pair<string,derived_probe*> >::iterator it;
5301 // Find the associated perf.counter probe
5302 unsigned i = 0;
5303 for (it=s.perf_counters.begin() ;
5304 it != s.perf_counters.end();
5305 it++, i++)
5306 if ((*it).second.second == (*pcii))
5307 break;
5308 // place the perf counter read so it precedes stp_lock_probe
5309 o->newline() << "l->l___perf_read_" + (*it).first
5310 + " = (((int64_t) (_stp_perf_read(smp_processor_id(),"
5311 + lex_cast(i) + "))));";
5312 }
b78a0fbb 5313
b95e2b79
MH
5314 if (access_vars)
5315 {
5316 // if accessing $variables, emit bsp cache setup for speeding up
d4670309 5317 o->newline() << "#if defined __ia64__";
d9aed31e 5318 o->newline() << "bspcache(c->unwaddr, c->kregs);";
d4670309 5319 o->newline() << "#endif";
b95e2b79 5320 }
9020300d 5321}
2930abc7 5322
b20febf3 5323// ------------------------------------------------------------------------
46b84a80
DS
5324
5325void
b20febf3 5326dwarf_derived_probe_group::enroll (dwarf_derived_probe* p)
46b84a80 5327{
b20febf3 5328 probes_by_module.insert (make_pair (p->module, p));
b8da0ad1
FCE
5329
5330 // XXX: probes put at the same address should all share a
5331 // single kprobe/kretprobe, and have their handlers executed
5332 // sequentially.
b55bc428
FCE
5333}
5334
7a053d3b 5335void
775d51e5 5336dwarf_derived_probe_group::emit_module_decls (systemtap_session& s)
ec4373ff 5337{
b20febf3 5338 if (probes_by_module.empty()) return;
2930abc7 5339
775d51e5
DS
5340 s.op->newline() << "/* ---- dwarf probes ---- */";
5341
5342 // Warn of misconfigured kernels
f41595cc
FCE
5343 s.op->newline() << "#if ! defined(CONFIG_KPROBES)";
5344 s.op->newline() << "#error \"Need CONFIG_KPROBES!\"";
5345 s.op->newline() << "#endif";
775d51e5 5346 s.op->newline();
f41595cc 5347
f07c3b68 5348 s.op->newline() << "#ifndef KRETACTIVE";
1ee6b5fc 5349 s.op->newline() << "#define KRETACTIVE (max(15,6*(int)num_possible_cpus()))";
f07c3b68
FCE
5350 s.op->newline() << "#endif";
5351
14cf7e42 5352 // Forward decls
2ba1736a 5353 s.op->newline() << "#include \"linux/kprobes-common.h\"";
14cf7e42 5354
b20febf3
FCE
5355 // Forward declare the master entry functions
5356 s.op->newline() << "static int enter_kprobe_probe (struct kprobe *inst,";
5357 s.op->line() << " struct pt_regs *regs);";
5358 s.op->newline() << "static int enter_kretprobe_probe (struct kretprobe_instance *inst,";
5359 s.op->line() << " struct pt_regs *regs);";
5360
42cb22bd
MH
5361 // Emit an array of kprobe/kretprobe pointers
5362 s.op->newline() << "#if defined(STAPCONF_UNREGISTER_KPROBES)";
5363 s.op->newline() << "static void * stap_unreg_kprobes[" << probes_by_module.size() << "];";
5364 s.op->newline() << "#endif";
5365
b20febf3 5366 // Emit the actual probe list.
606fd9c8
FCE
5367
5368 // NB: we used to plop a union { struct kprobe; struct kretprobe } into
5369 // struct stap_dwarf_probe, but it being initialized data makes it add
5370 // hundreds of bytes of padding per stap_dwarf_probe. (PR5673)
14cf7e42 5371 s.op->newline() << "static struct stap_dwarf_kprobe stap_dwarf_kprobes[" << probes_by_module.size() << "];";
606fd9c8
FCE
5372 // NB: bss!
5373
4c2732a1 5374 s.op->newline() << "static struct stap_dwarf_probe {";
b0986e7a
DS
5375 s.op->newline(1) << "const unsigned return_p:1;";
5376 s.op->newline() << "const unsigned maxactive_p:1;";
b350f56b 5377 s.op->newline() << "const unsigned optional_p:1;";
b20febf3 5378 s.op->newline() << "unsigned registered_p:1;";
b0986e7a 5379 s.op->newline() << "const unsigned short maxactive_val;";
606fd9c8 5380
af234c40
JS
5381 // data saved in the kretprobe_instance packet
5382 s.op->newline() << "const unsigned short saved_longs;";
5383 s.op->newline() << "const unsigned short saved_strings;";
5384
faea5e16 5385 // Let's find some stats for the embedded strings. Maybe they
606fd9c8
FCE
5386 // are small and uniform enough to justify putting char[MAX]'s into
5387 // the array instead of relocated char*'s.
faea5e16
JS
5388 size_t module_name_max = 0, section_name_max = 0;
5389 size_t module_name_tot = 0, section_name_tot = 0;
606fd9c8
FCE
5390 size_t all_name_cnt = probes_by_module.size(); // for average
5391 for (p_b_m_iterator it = probes_by_module.begin(); it != probes_by_module.end(); it++)
5392 {
5393 dwarf_derived_probe* p = it->second;
5394#define DOIT(var,expr) do { \
5395 size_t var##_size = (expr) + 1; \
5396 var##_max = max (var##_max, var##_size); \
5397 var##_tot += var##_size; } while (0)
5398 DOIT(module_name, p->module.size());
5399 DOIT(section_name, p->section.size());
606fd9c8
FCE
5400#undef DOIT
5401 }
5402
5403 // Decide whether it's worthwhile to use char[] or char* by comparing
5404 // the amount of average waste (max - avg) to the relocation data size
5405 // (3 native long words).
5406#define CALCIT(var) \
5407 if ((var##_name_max-(var##_name_tot/all_name_cnt)) < (3 * sizeof(void*))) \
5408 { \
5409 s.op->newline() << "const char " << #var << "[" << var##_name_max << "];"; \
5410 if (s.verbose > 2) clog << "stap_dwarf_probe " << #var \
5411 << "[" << var##_name_max << "]" << endl; \
5412 } \
5413 else \
5414 { \
b0986e7a 5415 s.op->newline() << "const char * const " << #var << ";"; \
606fd9c8
FCE
5416 if (s.verbose > 2) clog << "stap_dwarf_probe *" << #var << endl; \
5417 }
5418
5419 CALCIT(module);
5420 CALCIT(section);
e6fe60e7 5421#undef CALCIT
606fd9c8 5422
b0986e7a 5423 s.op->newline() << "const unsigned long address;";
7c3e97f4
JS
5424 s.op->newline() << "const struct stap_probe * const probe;";
5425 s.op->newline() << "const struct stap_probe * const entry_probe;";
b20febf3
FCE
5426 s.op->newline(-1) << "} stap_dwarf_probes[] = {";
5427 s.op->indent(1);
5428
5429 for (p_b_m_iterator it = probes_by_module.begin(); it != probes_by_module.end(); it++)
2930abc7 5430 {
b20febf3
FCE
5431 dwarf_derived_probe* p = it->second;
5432 s.op->newline() << "{";
5433 if (p->has_return)
5434 s.op->line() << " .return_p=1,";
c9bad430 5435 if (p->has_maxactive)
606fd9c8
FCE
5436 {
5437 s.op->line() << " .maxactive_p=1,";
5438 assert (p->maxactive_val >= 0 && p->maxactive_val <= USHRT_MAX);
5439 s.op->line() << " .maxactive_val=" << p->maxactive_val << ",";
5440 }
af234c40
JS
5441 if (p->saved_longs || p->saved_strings)
5442 {
5443 if (p->saved_longs)
5444 s.op->line() << " .saved_longs=" << p->saved_longs << ",";
5445 if (p->saved_strings)
5446 s.op->line() << " .saved_strings=" << p->saved_strings << ",";
5447 if (p->entry_handler)
c87ae2c1 5448 s.op->line() << " .entry_probe=" << common_probe_init (p->entry_handler) << ",";
af234c40 5449 }
b350f56b
JS
5450 if (p->locations[0]->optional)
5451 s.op->line() << " .optional_p=1,";
dc38c256 5452 s.op->line() << " .address=(unsigned long)0x" << hex << p->addr << dec << "ULL,";
84048984
FCE
5453 s.op->line() << " .module=\"" << p->module << "\",";
5454 s.op->line() << " .section=\"" << p->section << "\",";
faea5e16 5455 s.op->line() << " .probe=" << common_probe_init (p) << ",";
b20febf3 5456 s.op->line() << " },";
2930abc7 5457 }
2930abc7 5458
b20febf3
FCE
5459 s.op->newline(-1) << "};";
5460
5461 // Emit the kprobes callback function
5462 s.op->newline();
5463 s.op->newline() << "static int enter_kprobe_probe (struct kprobe *inst,";
5464 s.op->line() << " struct pt_regs *regs) {";
606fd9c8
FCE
5465 // NB: as of PR5673, the kprobe|kretprobe union struct is in BSS
5466 s.op->newline(1) << "int kprobe_idx = ((uintptr_t)inst-(uintptr_t)stap_dwarf_kprobes)/sizeof(struct stap_dwarf_kprobe);";
5467 // Check that the index is plausible
5468 s.op->newline() << "struct stap_dwarf_probe *sdp = &stap_dwarf_probes[";
5469 s.op->line() << "((kprobe_idx >= 0 && kprobe_idx < " << probes_by_module.size() << ")?";
5470 s.op->line() << "kprobe_idx:0)"; // NB: at least we avoid memory corruption
5471 // XXX: it would be nice to give a more verbose error though; BUG_ON later?
5472 s.op->line() << "];";
71db462b 5473 common_probe_entryfn_prologue (s, "STAP_SESSION_RUNNING", "sdp->probe",
cda141c2 5474 "stp_probe_type_kprobe");
d9aed31e 5475 s.op->newline() << "c->kregs = regs;";
6415ddde
MW
5476
5477 // Make it look like the IP is set as it wouldn't have been replaced
5478 // by a breakpoint instruction when calling real probe handler. Reset
5479 // IP regs on return, so we don't confuse kprobes. PR10458
5480 s.op->newline() << "{";
5481 s.op->indent(1);
d9aed31e 5482 s.op->newline() << "unsigned long kprobes_ip = REG_IP(c->kregs);";
259d54c0 5483 s.op->newline() << "SET_REG_IP(regs, (unsigned long) inst->addr);";
26e63673 5484 s.op->newline() << "(*sdp->probe->ph) (c);";
259d54c0 5485 s.op->newline() << "SET_REG_IP(regs, kprobes_ip);";
6415ddde
MW
5486 s.op->newline(-1) << "}";
5487
f887a8c9 5488 common_probe_entryfn_epilogue (s, true);
b20febf3
FCE
5489 s.op->newline() << "return 0;";
5490 s.op->newline(-1) << "}";
5491
5492 // Same for kretprobes
5493 s.op->newline();
af234c40
JS
5494 s.op->newline() << "static int enter_kretprobe_common (struct kretprobe_instance *inst,";
5495 s.op->line() << " struct pt_regs *regs, int entry) {";
b20febf3 5496 s.op->newline(1) << "struct kretprobe *krp = inst->rp;";
606fd9c8
FCE
5497
5498 // NB: as of PR5673, the kprobe|kretprobe union struct is in BSS
a36378d7 5499 s.op->newline() << "int kprobe_idx = ((uintptr_t)krp-(uintptr_t)stap_dwarf_kprobes)/sizeof(struct stap_dwarf_kprobe);";
606fd9c8
FCE
5500 // Check that the index is plausible
5501 s.op->newline() << "struct stap_dwarf_probe *sdp = &stap_dwarf_probes[";
5502 s.op->line() << "((kprobe_idx >= 0 && kprobe_idx < " << probes_by_module.size() << ")?";
5503 s.op->line() << "kprobe_idx:0)"; // NB: at least we avoid memory corruption
5504 // XXX: it would be nice to give a more verbose error though; BUG_ON later?
5505 s.op->line() << "];";
5506
7c3e97f4 5507 s.op->newline() << "const struct stap_probe *sp = entry ? sdp->entry_probe : sdp->probe;";
c87ae2c1
JS
5508 s.op->newline() << "if (sp) {";
5509 s.op->indent(1);
71db462b 5510 common_probe_entryfn_prologue (s, "STAP_SESSION_RUNNING", "sp",
cda141c2 5511 "stp_probe_type_kretprobe");
d9aed31e 5512 s.op->newline() << "c->kregs = regs;";
af234c40
JS
5513
5514 // for assisting runtime's backtrace logic and accessing kretprobe data packets
6dceb5c9
MW
5515 s.op->newline() << "c->ips.krp.pi = inst;";
5516 s.op->newline() << "c->ips.krp.pi_longs = sdp->saved_longs;";
6415ddde
MW
5517
5518 // Make it look like the IP is set as it wouldn't have been replaced
5519 // by a breakpoint instruction when calling real probe handler. Reset
5520 // IP regs on return, so we don't confuse kprobes. PR10458
5521 s.op->newline() << "{";
d9aed31e 5522 s.op->newline(1) << "unsigned long kprobes_ip = REG_IP(c->kregs);";
c87ae2c1
JS
5523 s.op->newline() << "if (entry)";
5524 s.op->newline(1) << "SET_REG_IP(regs, (unsigned long) inst->rp->kp.addr);";
5525 s.op->newline(-1) << "else";
5526 s.op->newline(1) << "SET_REG_IP(regs, (unsigned long)inst->ret_addr);";
5527 s.op->newline(-1) << "(sp->ph) (c);";
259d54c0 5528 s.op->newline() << "SET_REG_IP(regs, kprobes_ip);";
6415ddde
MW
5529 s.op->newline(-1) << "}";
5530
f887a8c9 5531 common_probe_entryfn_epilogue (s, true);
c87ae2c1 5532 s.op->newline(-1) << "}";
b20febf3
FCE
5533 s.op->newline() << "return 0;";
5534 s.op->newline(-1) << "}";
af234c40
JS
5535
5536 s.op->newline();
5537 s.op->newline() << "static int enter_kretprobe_probe (struct kretprobe_instance *inst,";
5538 s.op->line() << " struct pt_regs *regs) {";
5539 s.op->newline(1) << "return enter_kretprobe_common(inst, regs, 0);";
5540 s.op->newline(-1) << "}";
5541
5542 s.op->newline();
5543 s.op->newline() << "static int enter_kretprobe_entry_probe (struct kretprobe_instance *inst,";
5544 s.op->line() << " struct pt_regs *regs) {";
5545 s.op->newline(1) << "return enter_kretprobe_common(inst, regs, 1);";
5546 s.op->newline(-1) << "}";
b642c901 5547
14cf7e42 5548 s.op->newline();
20c6c071 5549}
ec4373ff 5550
20c6c071 5551
dc38c0ae 5552void
b20febf3
FCE
5553dwarf_derived_probe_group::emit_module_init (systemtap_session& s)
5554{
5555 s.op->newline() << "for (i=0; i<" << probes_by_module.size() << "; i++) {";
5556 s.op->newline(1) << "struct stap_dwarf_probe *sdp = & stap_dwarf_probes[i];";
a36378d7 5557 s.op->newline() << "struct stap_dwarf_kprobe *kp = & stap_dwarf_kprobes[i];";
a049e342 5558 s.op->newline() << "unsigned long relocated_addr = _stp_kmodule_relocate (sdp->module, sdp->section, sdp->address);";
b20febf3 5559 s.op->newline() << "if (relocated_addr == 0) continue;"; // quietly; assume module is absent
26e63673 5560 s.op->newline() << "probe_point = sdp->probe->pp;"; // for error messages
b20febf3 5561 s.op->newline() << "if (sdp->return_p) {";
606fd9c8 5562 s.op->newline(1) << "kp->u.krp.kp.addr = (void *) relocated_addr;";
c9bad430 5563 s.op->newline() << "if (sdp->maxactive_p) {";
606fd9c8 5564 s.op->newline(1) << "kp->u.krp.maxactive = sdp->maxactive_val;";
c9bad430 5565 s.op->newline(-1) << "} else {";
f07c3b68 5566 s.op->newline(1) << "kp->u.krp.maxactive = KRETACTIVE;";
c9bad430 5567 s.op->newline(-1) << "}";
606fd9c8 5568 s.op->newline() << "kp->u.krp.handler = &enter_kretprobe_probe;";
af234c40 5569 s.op->newline() << "#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,25)";
c87ae2c1 5570 s.op->newline() << "if (sdp->entry_probe) {";
af234c40
JS
5571 s.op->newline(1) << "kp->u.krp.entry_handler = &enter_kretprobe_entry_probe;";
5572 s.op->newline() << "kp->u.krp.data_size = sdp->saved_longs * sizeof(int64_t) + ";
5573 s.op->newline() << " sdp->saved_strings * MAXSTRINGLEN;";
5574 s.op->newline(-1) << "}";
5575 s.op->newline() << "#endif";
e4cb375f
MH
5576 // to ensure safeness of bspcache, always use aggr_kprobe on ia64
5577 s.op->newline() << "#ifdef __ia64__";
5578 s.op->newline() << "kp->dummy.addr = kp->u.krp.kp.addr;";
5579 s.op->newline() << "kp->dummy.pre_handler = NULL;";
5580 s.op->newline() << "rc = register_kprobe (& kp->dummy);";
5581 s.op->newline() << "if (rc == 0) {";
5582 s.op->newline(1) << "rc = register_kretprobe (& kp->u.krp);";
5583 s.op->newline() << "if (rc != 0)";
5584 s.op->newline(1) << "unregister_kprobe (& kp->dummy);";
5585 s.op->newline(-2) << "}";
5586 s.op->newline() << "#else";
606fd9c8 5587 s.op->newline() << "rc = register_kretprobe (& kp->u.krp);";
e4cb375f 5588 s.op->newline() << "#endif";
b20febf3 5589 s.op->newline(-1) << "} else {";
e4cb375f 5590 // to ensure safeness of bspcache, always use aggr_kprobe on ia64
606fd9c8
FCE
5591 s.op->newline(1) << "kp->u.kp.addr = (void *) relocated_addr;";
5592 s.op->newline() << "kp->u.kp.pre_handler = &enter_kprobe_probe;";
e4cb375f
MH
5593 s.op->newline() << "#ifdef __ia64__";
5594 s.op->newline() << "kp->dummy.addr = kp->u.kp.addr;";
5595 s.op->newline() << "kp->dummy.pre_handler = NULL;";
5596 s.op->newline() << "rc = register_kprobe (& kp->dummy);";
5597 s.op->newline() << "if (rc == 0) {";
5598 s.op->newline(1) << "rc = register_kprobe (& kp->u.kp);";
5599 s.op->newline() << "if (rc != 0)";
5600 s.op->newline(1) << "unregister_kprobe (& kp->dummy);";
5601 s.op->newline(-2) << "}";
5602 s.op->newline() << "#else";
606fd9c8 5603 s.op->newline() << "rc = register_kprobe (& kp->u.kp);";
e4cb375f 5604 s.op->newline() << "#endif";
b20febf3 5605 s.op->newline(-1) << "}";
9063462a
FCE
5606 s.op->newline() << "if (rc) {"; // PR6749: tolerate a failed register_*probe.
5607 s.op->newline(1) << "sdp->registered_p = 0;";
b350f56b 5608 s.op->newline() << "if (!sdp->optional_p)";
50b6acf7 5609 s.op->newline(1) << "_stp_warn (\"probe %s (address 0x%lx) registration error (rc %d)\", probe_point, (unsigned long) relocated_addr, rc);";
b350f56b 5610 s.op->newline(-1) << "rc = 0;"; // continue with other probes
9063462a
FCE
5611 // XXX: shall we increment numskipped?
5612 s.op->newline(-1) << "}";
5613
5614#if 0 /* pre PR 6749; XXX consider making an option */
c48cb0cc 5615 s.op->newline(1) << "for (j=i-1; j>=0; j--) {"; // partial rollback
b20febf3 5616 s.op->newline(1) << "struct stap_dwarf_probe *sdp2 = & stap_dwarf_probes[j];";
606fd9c8
FCE
5617 s.op->newline() << "struct stap_dwarf_kprobe *kp2 = & stap_dwarf_kprobes[j];";
5618 s.op->newline() << "if (sdp2->return_p) unregister_kretprobe (&kp2->u.krp);";
5619 s.op->newline() << "else unregister_kprobe (&kp2->u.kp);";
e4cb375f
MH
5620 s.op->newline() << "#ifdef __ia64__";
5621 s.op->newline() << "unregister_kprobe (&kp2->dummy);";
5622 s.op->newline() << "#endif";
c48cb0cc
FCE
5623 // NB: we don't have to clear sdp2->registered_p, since the module_exit code is
5624 // not run for this early-abort case.
5625 s.op->newline(-1) << "}";
5626 s.op->newline() << "break;"; // don't attempt to register any more probes
b20febf3 5627 s.op->newline(-1) << "}";
9063462a
FCE
5628#endif
5629
b20febf3
FCE
5630 s.op->newline() << "else sdp->registered_p = 1;";
5631 s.op->newline(-1) << "}"; // for loop
dc38c0ae
DS
5632}
5633
5634
b4be7cbc
FCE
5635void
5636dwarf_derived_probe_group::emit_module_refresh (systemtap_session& s)
5637{
5638 s.op->newline() << "for (i=0; i<" << probes_by_module.size() << "; i++) {";
5639 s.op->newline(1) << "struct stap_dwarf_probe *sdp = & stap_dwarf_probes[i];";
5640 s.op->newline() << "struct stap_dwarf_kprobe *kp = & stap_dwarf_kprobes[i];";
5641 s.op->newline() << "unsigned long relocated_addr = _stp_kmodule_relocate (sdp->module, sdp->section, sdp->address);";
5642 s.op->newline() << "int rc;";
5643
5644 // new module arrived?
5645 s.op->newline() << "if (sdp->registered_p == 0 && relocated_addr != 0) {";
5646 s.op->newline(1) << "if (sdp->return_p) {";
5647 s.op->newline(1) << "kp->u.krp.kp.addr = (void *) relocated_addr;";
5648 s.op->newline() << "if (sdp->maxactive_p) {";
5649 s.op->newline(1) << "kp->u.krp.maxactive = sdp->maxactive_val;";
5650 s.op->newline(-1) << "} else {";
5651 s.op->newline(1) << "kp->u.krp.maxactive = KRETACTIVE;";
5652 s.op->newline(-1) << "}";
5653 s.op->newline() << "kp->u.krp.handler = &enter_kretprobe_probe;";
5654 s.op->newline() << "#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,25)";
5655 s.op->newline() << "if (sdp->entry_probe) {";
5656 s.op->newline(1) << "kp->u.krp.entry_handler = &enter_kretprobe_entry_probe;";
5657 s.op->newline() << "kp->u.krp.data_size = sdp->saved_longs * sizeof(int64_t) + ";
5658 s.op->newline() << " sdp->saved_strings * MAXSTRINGLEN;";
5659 s.op->newline(-1) << "}";
5660 s.op->newline() << "#endif";
5661 // to ensure safeness of bspcache, always use aggr_kprobe on ia64
5662 s.op->newline() << "#ifdef __ia64__";
5663 s.op->newline() << "kp->dummy.addr = kp->u.krp.kp.addr;";
5664 s.op->newline() << "kp->dummy.pre_handler = NULL;";
5665 s.op->newline() << "rc = register_kprobe (& kp->dummy);";
5666 s.op->newline() << "if (rc == 0) {";
5667 s.op->newline(1) << "rc = register_kretprobe (& kp->u.krp);";
5668 s.op->newline() << "if (rc != 0)";
5669 s.op->newline(1) << "unregister_kprobe (& kp->dummy);";
5670 s.op->newline(-2) << "}";
5671 s.op->newline() << "#else";
5672 s.op->newline() << "rc = register_kretprobe (& kp->u.krp);";
5673 s.op->newline() << "#endif";
5674 s.op->newline(-1) << "} else {";
5675 // to ensure safeness of bspcache, always use aggr_kprobe on ia64
5676 s.op->newline(1) << "kp->u.kp.addr = (void *) relocated_addr;";
5677 s.op->newline() << "kp->u.kp.pre_handler = &enter_kprobe_probe;";
5678 s.op->newline() << "#ifdef __ia64__";
5679 s.op->newline() << "kp->dummy.addr = kp->u.kp.addr;";
5680 s.op->newline() << "kp->dummy.pre_handler = NULL;";
5681 s.op->newline() << "rc = register_kprobe (& kp->dummy);";
5682 s.op->newline() << "if (rc == 0) {";
5683 s.op->newline(1) << "rc = register_kprobe (& kp->u.kp);";
5684 s.op->newline() << "if (rc != 0)";
5685 s.op->newline(1) << "unregister_kprobe (& kp->dummy);";
5686 s.op->newline(-2) << "}";
5687 s.op->newline() << "#else";
5688 s.op->newline() << "rc = register_kprobe (& kp->u.kp);";
5689 s.op->newline() << "#endif";
5690 s.op->newline(-1) << "}";
5691 s.op->newline() << "if (rc == 0) sdp->registered_p = 1;";
5692
5693 // old module disappeared?
5694 s.op->newline(-1) << "} else if (sdp->registered_p == 1 && relocated_addr == 0) {";
5695 s.op->newline(1) << "if (sdp->return_p) {";
5696 s.op->newline(1) << "unregister_kretprobe (&kp->u.krp);";
065d5567 5697 s.op->newline() << "atomic_add (kp->u.krp.nmissed, skipped_count());";
b4be7cbc
FCE
5698 s.op->newline() << "#ifdef STP_TIMING";
5699 s.op->newline() << "if (kp->u.krp.nmissed)";
5700 s.op->newline(1) << "_stp_warn (\"Skipped due to missed kretprobe/1 on '%s': %d\\n\", sdp->probe->pp, kp->u.krp.nmissed);";
5701 s.op->newline(-1) << "#endif";
065d5567 5702 s.op->newline() << "atomic_add (kp->u.krp.kp.nmissed, skipped_count());";
b4be7cbc
FCE
5703 s.op->newline() << "#ifdef STP_TIMING";
5704 s.op->newline() << "if (kp->u.krp.kp.nmissed)";
5705 s.op->newline(1) << "_stp_warn (\"Skipped due to missed kretprobe/2 on '%s': %lu\\n\", sdp->probe->pp, kp->u.krp.kp.nmissed);";
5706 s.op->newline(-1) << "#endif";
5707 s.op->newline(-1) << "} else {";
5708 s.op->newline(1) << "unregister_kprobe (&kp->u.kp);";
065d5567 5709 s.op->newline() << "atomic_add (kp->u.kp.nmissed, skipped_count());";
b4be7cbc
FCE
5710 s.op->newline() << "#ifdef STP_TIMING";
5711 s.op->newline() << "if (kp->u.kp.nmissed)";
5712 s.op->newline(1) << "_stp_warn (\"Skipped due to missed kprobe on '%s': %lu\\n\", sdp->probe->pp, kp->u.kp.nmissed);";
5713 s.op->newline(-1) << "#endif";
5714 s.op->newline(-1) << "}";
5715 s.op->newline() << "#if defined(__ia64__)";
5716 s.op->newline() << "unregister_kprobe (&kp->dummy);";
5717 s.op->newline() << "#endif";
5718 s.op->newline() << "sdp->registered_p = 0;";
5719 s.op->newline(-1) << "}";
5720
5721 s.op->newline(-1) << "}"; // for loop
5722}
5723
5724
5725
5726
46b84a80 5727void
b20febf3 5728dwarf_derived_probe_group::emit_module_exit (systemtap_session& s)
46b84a80 5729{
42cb22bd
MH
5730 //Unregister kprobes by batch interfaces.
5731 s.op->newline() << "#if defined(STAPCONF_UNREGISTER_KPROBES)";
5732 s.op->newline() << "j = 0;";
5733 s.op->newline() << "for (i=0; i<" << probes_by_module.size() << "; i++) {";
5734 s.op->newline(1) << "struct stap_dwarf_probe *sdp = & stap_dwarf_probes[i];";
5735 s.op->newline() << "struct stap_dwarf_kprobe *kp = & stap_dwarf_kprobes[i];";
5736 s.op->newline() << "if (! sdp->registered_p) continue;";
5737 s.op->newline() << "if (!sdp->return_p)";
5738 s.op->newline(1) << "stap_unreg_kprobes[j++] = &kp->u.kp;";
5739 s.op->newline(-2) << "}";
5740 s.op->newline() << "unregister_kprobes((struct kprobe **)stap_unreg_kprobes, j);";
5741 s.op->newline() << "j = 0;";
5742 s.op->newline() << "for (i=0; i<" << probes_by_module.size() << "; i++) {";
5743 s.op->newline(1) << "struct stap_dwarf_probe *sdp = & stap_dwarf_probes[i];";
5744 s.op->newline() << "struct stap_dwarf_kprobe *kp = & stap_dwarf_kprobes[i];";
5745 s.op->newline() << "if (! sdp->registered_p) continue;";
5746 s.op->newline() << "if (sdp->return_p)";
5747 s.op->newline(1) << "stap_unreg_kprobes[j++] = &kp->u.krp;";
5748 s.op->newline(-2) << "}";
5749 s.op->newline() << "unregister_kretprobes((struct kretprobe **)stap_unreg_kprobes, j);";
e4cb375f
MH
5750 s.op->newline() << "#ifdef __ia64__";
5751 s.op->newline() << "j = 0;";
5752 s.op->newline() << "for (i=0; i<" << probes_by_module.size() << "; i++) {";
5753 s.op->newline(1) << "struct stap_dwarf_probe *sdp = & stap_dwarf_probes[i];";
5754 s.op->newline() << "struct stap_dwarf_kprobe *kp = & stap_dwarf_kprobes[i];";
5755 s.op->newline() << "if (! sdp->registered_p) continue;";
5756 s.op->newline() << "stap_unreg_kprobes[j++] = &kp->dummy;";
5757 s.op->newline(-1) << "}";
5758 s.op->newline() << "unregister_kprobes((struct kprobe **)stap_unreg_kprobes, j);";
5759 s.op->newline() << "#endif";
42cb22bd
MH
5760 s.op->newline() << "#endif";
5761
b20febf3
FCE
5762 s.op->newline() << "for (i=0; i<" << probes_by_module.size() << "; i++) {";
5763 s.op->newline(1) << "struct stap_dwarf_probe *sdp = & stap_dwarf_probes[i];";
a36378d7 5764 s.op->newline() << "struct stap_dwarf_kprobe *kp = & stap_dwarf_kprobes[i];";
b20febf3
FCE
5765 s.op->newline() << "if (! sdp->registered_p) continue;";
5766 s.op->newline() << "if (sdp->return_p) {";
42cb22bd 5767 s.op->newline() << "#if !defined(STAPCONF_UNREGISTER_KPROBES)";
606fd9c8 5768 s.op->newline(1) << "unregister_kretprobe (&kp->u.krp);";
42cb22bd 5769 s.op->newline() << "#endif";
065d5567 5770 s.op->newline() << "atomic_add (kp->u.krp.nmissed, skipped_count());";
73209876
FCE
5771 s.op->newline() << "#ifdef STP_TIMING";
5772 s.op->newline() << "if (kp->u.krp.nmissed)";
26e63673 5773 s.op->newline(1) << "_stp_warn (\"Skipped due to missed kretprobe/1 on '%s': %d\\n\", sdp->probe->pp, kp->u.krp.nmissed);";
73209876 5774 s.op->newline(-1) << "#endif";
065d5567 5775 s.op->newline() << "atomic_add (kp->u.krp.kp.nmissed, skipped_count());";
73209876
FCE
5776 s.op->newline() << "#ifdef STP_TIMING";
5777 s.op->newline() << "if (kp->u.krp.kp.nmissed)";
26e63673 5778 s.op->newline(1) << "_stp_warn (\"Skipped due to missed kretprobe/2 on '%s': %lu\\n\", sdp->probe->pp, kp->u.krp.kp.nmissed);";
73209876 5779 s.op->newline(-1) << "#endif";
557fb7a8 5780 s.op->newline(-1) << "} else {";
42cb22bd 5781 s.op->newline() << "#if !defined(STAPCONF_UNREGISTER_KPROBES)";
606fd9c8 5782 s.op->newline(1) << "unregister_kprobe (&kp->u.kp);";
42cb22bd 5783 s.op->newline() << "#endif";
065d5567 5784 s.op->newline() << "atomic_add (kp->u.kp.nmissed, skipped_count());";
73209876
FCE
5785 s.op->newline() << "#ifdef STP_TIMING";
5786 s.op->newline() << "if (kp->u.kp.nmissed)";
26e63673 5787 s.op->newline(1) << "_stp_warn (\"Skipped due to missed kprobe on '%s': %lu\\n\", sdp->probe->pp, kp->u.kp.nmissed);";
73209876 5788 s.op->newline(-1) << "#endif";
b20febf3 5789 s.op->newline(-1) << "}";
e4cb375f
MH
5790 s.op->newline() << "#if !defined(STAPCONF_UNREGISTER_KPROBES) && defined(__ia64__)";
5791 s.op->newline() << "unregister_kprobe (&kp->dummy);";
5792 s.op->newline() << "#endif";
b20febf3
FCE
5793 s.op->newline() << "sdp->registered_p = 0;";
5794 s.op->newline(-1) << "}";
46b84a80
DS
5795}
5796
272c9036
WF
5797static void sdt_v3_tokenize(const string& str, vector<string>& tokens)
5798{
5799 string::size_type pos;
5800 string::size_type lastPos = str.find_first_not_of(" ", 0);
5801 string::size_type nextAt = str.find("@", lastPos);
e9a90eee
JS
5802
5803 if (nextAt == string::npos)
5804 {
5805 // PR13934: Assembly probes are not forced to use the N@OP form.
5806 // In this case, N is inferred to be the native word size. Since we
5807 // don't have a nice delimiter, just split it on spaces. SDT-asm authors
5808 // then must not put any spaces in arguments, to avoid ambiguity.
5809 tokenize(str, tokens, " ");
5810 return;
5811 }
5812
272c9036
WF
5813 while (lastPos != string::npos)
5814 {
5815 pos = nextAt + 1;
5816 nextAt = str.find("@", pos);
5817 if (nextAt == string::npos)
5818 pos = string::npos;
5819 else
5820 pos = str.rfind(" ", nextAt);
5821
5822 tokens.push_back(str.substr(lastPos, pos - lastPos));
5823 lastPos = str.find_first_not_of(" ", pos);
5824 }
5825}
8aabf152 5826
8aabf152 5827
aff5d390 5828struct sdt_uprobe_var_expanding_visitor: public var_expanding_visitor
7a05f484 5829{
ae1418f0 5830 enum regwidths {QI, QIh, HI, SI, DI};
f83336a5 5831 sdt_uprobe_var_expanding_visitor(systemtap_session& s,
b8688ee6 5832 dwflpp& dw,
f83336a5
FCE
5833 int elf_machine,
5834 const string & process_name,
a794dbeb 5835 const string & provider_name,
aff5d390 5836 const string & probe_name,
71e5e13d 5837 stap_sdt_probe_type probe_type,
aff5d390 5838 const string & arg_string,
8aabf152 5839 int ac):
b8688ee6
JL
5840 session (s), dw (dw), elf_machine (elf_machine),
5841 process_name (process_name), provider_name (provider_name),
5842 probe_name (probe_name), probe_type (probe_type), arg_count ((unsigned) ac)
a8ec7719 5843 {
a57b7534
JL
5844 // sanity check that we're not somehow here for a kernel probe
5845 assert(is_user_module(process_name));
5846
2e6d617a 5847 build_dwarf_registers();
f83336a5 5848
ebbd2b45 5849 need_debug_info = false;
88e39987 5850 if (probe_type == uprobe3_type)
272c9036
WF
5851 {
5852 sdt_v3_tokenize(arg_string, arg_tokens);
5853 assert(arg_count <= 12);
5854 }
88e39987 5855 else
272c9036
WF
5856 {
5857 tokenize(arg_string, arg_tokens, " ");
5858 assert(arg_count <= 10);
5859 }
a8ec7719 5860 }
8aabf152 5861
f83336a5 5862 systemtap_session& session;
b8688ee6 5863 dwflpp& dw;
332ba7e7 5864 int elf_machine;
aff5d390 5865 const string & process_name;
a794dbeb 5866 const string & provider_name;
aff5d390 5867 const string & probe_name;
71e5e13d 5868 stap_sdt_probe_type probe_type;
8aabf152 5869 unsigned arg_count;
aff5d390 5870 vector<string> arg_tokens;
2e6d617a 5871
46a94997 5872 map<string, pair<unsigned,int> > dwarf_regs;
2e6d617a
JL
5873 string regnames;
5874 string percent_regnames;
5875
ebbd2b45 5876 bool need_debug_info;
aff5d390 5877
2e6d617a 5878 void build_dwarf_registers();
aff5d390 5879 void visit_target_symbol (target_symbol* e);
03c45fcb 5880 unsigned get_target_symbol_argno_and_validate (target_symbol* e);
ac074daf 5881 long parse_out_arg_precision(string& asmarg);
0fbf6fb8
JL
5882 expression* try_parse_arg_literal (target_symbol *e,
5883 const string& asmarg,
5884 long precision);
95b78bc9
JL
5885 expression* try_parse_arg_register (target_symbol *e,
5886 const string& asmarg,
5887 long precision);
8da56b62
JL
5888 expression* try_parse_arg_offset_register (target_symbol *e,
5889 const string& asmarg,
5890 long precision);
10fcca15
JL
5891 expression* try_parse_arg_effective_addr (target_symbol *e,
5892 const string& asmarg,
5893 long precision);
451c0c25
JL
5894 expression* try_parse_arg_varname (target_symbol *e,
5895 const string& asmarg,
5896 long precision);
6ef331c8
SC
5897 void visit_target_symbol_arg (target_symbol* e);
5898 void visit_target_symbol_context (target_symbol* e);
bd1fcbad 5899 void visit_atvar_op (atvar_op* e);
40a0c64e 5900 void visit_cast_op (cast_op* e);
aff5d390
SC
5901};
5902
2e6d617a
JL
5903void
5904sdt_uprobe_var_expanding_visitor::build_dwarf_registers ()
5905{
5906 /* Register name mapping table depends on the elf machine of this particular
5907 probe target process/file, not upon the host. So we can't just
5908 #ifdef _i686_ etc. */
5909
5910#define DRI(name,num,width) dwarf_regs[name]=make_pair(num,width)
5911 if (elf_machine == EM_X86_64) {
5912 DRI ("%rax", 0, DI); DRI ("%eax", 0, SI); DRI ("%ax", 0, HI);
5913 DRI ("%al", 0, QI); DRI ("%ah", 0, QIh);
5914 DRI ("%rdx", 1, DI); DRI ("%edx", 1, SI); DRI ("%dx", 1, HI);
5915 DRI ("%dl", 1, QI); DRI ("%dh", 1, QIh);
5916 DRI ("%rcx", 2, DI); DRI ("%ecx", 2, SI); DRI ("%cx", 2, HI);
5917 DRI ("%cl", 2, QI); DRI ("%ch", 2, QIh);
5918 DRI ("%rbx", 3, DI); DRI ("%ebx", 3, SI); DRI ("%bx", 3, HI);
5919 DRI ("%bl", 3, QI); DRI ("%bh", 3, QIh);
5920 DRI ("%rsi", 4, DI); DRI ("%esi", 4, SI); DRI ("%si", 4, HI);
5921 DRI ("%sil", 4, QI);
5922 DRI ("%rdi", 5, DI); DRI ("%edi", 5, SI); DRI ("%di", 5, HI);
5923 DRI ("%dil", 5, QI);
5924 DRI ("%rbp", 6, DI); DRI ("%ebp", 6, SI); DRI ("%bp", 6, HI);
5925 DRI ("%bpl", 6, QI);
5926 DRI ("%rsp", 7, DI); DRI ("%esp", 7, SI); DRI ("%sp", 7, HI);
5927 DRI ("%spl", 7, QI);
5928 DRI ("%r8", 8, DI); DRI ("%r8d", 8, SI); DRI ("%r8w", 8, HI);
5929 DRI ("%r8b", 8, QI);
5930 DRI ("%r9", 9, DI); DRI ("%r9d", 9, SI); DRI ("%r9w", 9, HI);
5931 DRI ("%r9b", 9, QI);
5932 DRI ("%r10", 10, DI); DRI ("%r10d", 10, SI); DRI ("%r10w", 10, HI);
5933 DRI ("%r10b", 10, QI);
5934 DRI ("%r11", 11, DI); DRI ("%r11d", 11, SI); DRI ("%r11w", 11, HI);
5935 DRI ("%r11b", 11, QI);
5936 DRI ("%r12", 12, DI); DRI ("%r12d", 12, SI); DRI ("%r12w", 12, HI);
5937 DRI ("%r12b", 12, QI);
5938 DRI ("%r13", 13, DI); DRI ("%r13d", 13, SI); DRI ("%r13w", 13, HI);
5939 DRI ("%r13b", 13, QI);
5940 DRI ("%r14", 14, DI); DRI ("%r14d", 14, SI); DRI ("%r14w", 14, HI);
5941 DRI ("%r14b", 14, QI);
5942 DRI ("%r15", 15, DI); DRI ("%r15d", 15, SI); DRI ("%r15w", 15, HI);
5943 DRI ("%r15b", 15, QI);
5944 DRI ("%rip", 16, DI); DRI ("%eip", 16, SI); DRI ("%ip", 16, HI);
5945 } else if (elf_machine == EM_386) {
5946 DRI ("%eax", 0, SI); DRI ("%ax", 0, HI); DRI ("%al", 0, QI);
5947 DRI ("%ah", 0, QIh);
5948 DRI ("%ecx", 1, SI); DRI ("%cx", 1, HI); DRI ("%cl", 1, QI);
5949 DRI ("%ch", 1, QIh);
5950 DRI ("%edx", 2, SI); DRI ("%dx", 2, HI); DRI ("%dl", 2, QI);
5951 DRI ("%dh", 2, QIh);
5952 DRI ("%ebx", 3, SI); DRI ("%bx", 3, HI); DRI ("%bl", 3, QI);
5953 DRI ("%bh", 3, QIh);
5954 DRI ("%esp", 4, SI); DRI ("%sp", 4, HI);
5955 DRI ("%ebp", 5, SI); DRI ("%bp", 5, HI);
5956 DRI ("%esi", 6, SI); DRI ("%si", 6, HI); DRI ("%sil", 6, QI);
5957 DRI ("%edi", 7, SI); DRI ("%di", 7, HI); DRI ("%dil", 7, QI);
5958 } else if (elf_machine == EM_PPC || elf_machine == EM_PPC64) {
5959 DRI ("%r0", 0, DI);
5960 DRI ("%r1", 1, DI);
5961 DRI ("%r2", 2, DI);
5962 DRI ("%r3", 3, DI);
5963 DRI ("%r4", 4, DI);
5964 DRI ("%r5", 5, DI);
5965 DRI ("%r6", 6, DI);
5966 DRI ("%r7", 7, DI);
5967 DRI ("%r8", 8, DI);
5968 DRI ("%r9", 9, DI);
5969 DRI ("%r10", 10, DI);
5970 DRI ("%r11", 11, DI);
5971 DRI ("%r12", 12, DI);
5972 DRI ("%r13", 13, DI);
5973 DRI ("%r14", 14, DI);
5974 DRI ("%r15", 15, DI);
5975 DRI ("%r16", 16, DI);
5976 DRI ("%r17", 17, DI);
5977 DRI ("%r18", 18, DI);
5978 DRI ("%r19", 19, DI);
5979 DRI ("%r20", 20, DI);
5980 DRI ("%r21", 21, DI);
5981 DRI ("%r22", 22, DI);
5982 DRI ("%r23", 23, DI);
5983 DRI ("%r24", 24, DI);
5984 DRI ("%r25", 25, DI);
5985 DRI ("%r26", 26, DI);
5986 DRI ("%r27", 27, DI);
5987 DRI ("%r28", 28, DI);
5988 DRI ("%r29", 29, DI);
5989 DRI ("%r30", 30, DI);
5990 DRI ("%r31", 31, DI);
5991 // PR11821: unadorned register "names" without -mregnames
5992 DRI ("0", 0, DI);
5993 DRI ("1", 1, DI);
5994 DRI ("2", 2, DI);
5995 DRI ("3", 3, DI);
5996 DRI ("4", 4, DI);
5997 DRI ("5", 5, DI);
5998 DRI ("6", 6, DI);
5999 DRI ("7", 7, DI);
6000 DRI ("8", 8, DI);
6001 DRI ("9", 9, DI);
6002 DRI ("10", 10, DI);
6003 DRI ("11", 11, DI);
6004 DRI ("12", 12, DI);
6005 DRI ("13", 13, DI);
6006 DRI ("14", 14, DI);
6007 DRI ("15", 15, DI);
6008 DRI ("16", 16, DI);
6009 DRI ("17", 17, DI);
6010 DRI ("18", 18, DI);
6011 DRI ("19", 19, DI);
6012 DRI ("20", 20, DI);
6013 DRI ("21", 21, DI);
6014 DRI ("22", 22, DI);
6015 DRI ("23", 23, DI);
6016 DRI ("24", 24, DI);
6017 DRI ("25", 25, DI);
6018 DRI ("26", 26, DI);
6019 DRI ("27", 27, DI);
6020 DRI ("28", 28, DI);
6021 DRI ("29", 29, DI);
6022 DRI ("30", 30, DI);
6023 DRI ("31", 31, DI);
6024 } else if (elf_machine == EM_S390) {
6025 DRI ("%r0", 0, DI);
6026 DRI ("%r1", 1, DI);
6027 DRI ("%r2", 2, DI);
6028 DRI ("%r3", 3, DI);
6029 DRI ("%r4", 4, DI);
6030 DRI ("%r5", 5, DI);
6031 DRI ("%r6", 6, DI);
6032 DRI ("%r7", 7, DI);
6033 DRI ("%r8", 8, DI);
6034 DRI ("%r9", 9, DI);
6035 DRI ("%r10", 10, DI);
6036 DRI ("%r11", 11, DI);
6037 DRI ("%r12", 12, DI);
6038 DRI ("%r13", 13, DI);
6039 DRI ("%r14", 14, DI);
6040 DRI ("%r15", 15, DI);
6041 } else if (elf_machine == EM_ARM) {
6042 DRI ("r0", 0, SI);
6043 DRI ("r1", 1, SI);
6044 DRI ("r2", 2, SI);
6045 DRI ("r3", 3, SI);
6046 DRI ("r4", 4, SI);
6047 DRI ("r5", 5, SI);
6048 DRI ("r6", 6, SI);
6049 DRI ("r7", 7, SI);
6050 DRI ("r8", 8, SI);
6051 DRI ("r9", 9, SI);
6052 DRI ("r10", 10, SI); DRI ("sl", 10, SI);
6053 DRI ("fp", 11, SI);
6054 DRI ("ip", 12, SI);
6055 DRI ("sp", 13, SI);
6056 DRI ("lr", 14, SI);
6057 DRI ("pc", 15, SI);
6058 } else if (elf_machine == EM_AARCH64) {
6059 DRI ("x0", 0, DI); DRI ("w0", 0, SI);
6060 DRI ("x1", 1, DI); DRI ("w1", 1, SI);
6061 DRI ("x2", 2, DI); DRI ("w2", 2, SI);
6062 DRI ("x3", 3, DI); DRI ("w3", 3, SI);
6063 DRI ("x4", 4, DI); DRI ("w4", 4, SI);
6064 DRI ("x5", 5, DI); DRI ("w5", 5, SI);
6065 DRI ("x6", 6, DI); DRI ("w6", 6, SI);
6066 DRI ("x7", 7, DI); DRI ("w7", 7, SI);
6067 DRI ("x8", 8, DI); DRI ("w8", 8, SI);
6068 DRI ("x9", 9, DI); DRI ("w9", 9, SI);
6069 DRI ("x10", 10, DI); DRI ("w10", 10, SI);
6070 DRI ("x11", 11, DI); DRI ("w11", 11, SI);
6071 DRI ("x12", 12, DI); DRI ("w12", 12, SI);
6072 DRI ("x13", 13, DI); DRI ("w13", 13, SI);
6073 DRI ("x14", 14, DI); DRI ("w14", 14, SI);
6074 DRI ("x15", 15, DI); DRI ("w15", 15, SI);
6075 DRI ("x16", 16, DI); DRI ("w16", 16, SI);
6076 DRI ("x17", 17, DI); DRI ("w17", 17, SI);
6077 DRI ("x18", 18, DI); DRI ("w18", 18, SI);
6078 DRI ("x19", 19, DI); DRI ("w19", 19, SI);
6079 DRI ("x20", 20, DI); DRI ("w20", 20, SI);
6080 DRI ("x21", 21, DI); DRI ("w21", 21, SI);
6081 DRI ("x22", 22, DI); DRI ("w22", 22, SI);
6082 DRI ("x23", 23, DI); DRI ("w23", 23, SI);
6083 DRI ("x24", 24, DI); DRI ("w24", 24, SI);
6084 DRI ("x25", 25, DI); DRI ("w25", 25, SI);
6085 DRI ("x26", 26, DI); DRI ("w26", 26, SI);
6086 DRI ("x27", 27, DI); DRI ("w27", 27, SI);
6087 DRI ("x28", 28, DI); DRI ("w28", 28, SI);
6088 DRI ("x29", 29, DI); DRI ("w29", 29, SI);
6089 DRI ("x30", 30, DI); DRI ("w30", 30, SI);
6090 DRI ("sp", 31, DI);
6091 } else if (arg_count) {
6092 /* permit this case; just fall back to dwarf */
6093 }
6094#undef DRI
6095
6096 // Build regex pieces out of the known dwarf_regs. We keep two separate
6097 // lists: ones with the % prefix (and thus unambigiuous even despite PR11821),
6098 // and ones with no prefix (and thus only usable in unambiguous contexts).
6099 map<string,pair<unsigned,int> >::const_iterator ri;
6100 for (ri = dwarf_regs.begin(); ri != dwarf_regs.end(); ri++)
6101 {
6102 string regname = ri->first;
6103 assert (regname != "");
6104 regnames += string("|")+regname;
6105 if (regname[0]=='%')
6106 percent_regnames += string("|")+regname;
6107 }
6108
6109 // clip off leading |
6110 if (regnames != "")
6111 regnames = regnames.substr(1);
6112 if (percent_regnames != "")
6113 percent_regnames = percent_regnames.substr(1);
6114}
7a05f484
SC
6115
6116void
6ef331c8 6117sdt_uprobe_var_expanding_visitor::visit_target_symbol_context (target_symbol* e)
7a05f484 6118{
6ef331c8 6119 if (e->addressof)
dc09353a 6120 throw SEMANTIC_ERROR(_("cannot take address of context variable"), e->tok);
6ef331c8
SC
6121
6122 if (e->name == "$$name")
7a05f484 6123 {
6ef331c8
SC
6124 literal_string *myname = new literal_string (probe_name);
6125 myname->tok = e->tok;
6126 provide(myname);
6127 return;
6128 }
7a05f484 6129
6ef331c8
SC
6130 else if (e->name == "$$provider")
6131 {
6132 literal_string *myname = new literal_string (provider_name);
6133 myname->tok = e->tok;
6134 provide(myname);
6135 return;
6136 }
a794dbeb 6137
6ef331c8
SC
6138 else if (e->name == "$$vars" || e->name == "$$parms")
6139 {
6140 e->assert_no_components("sdt", true);
6ef331c8
SC
6141
6142 // Convert $$vars to sprintf of a list of vars which we recursively evaluate
63ea4244 6143
1c922ad7 6144 print_format* pf = print_format::create(e->tok, "sprintf");
6ef331c8
SC
6145
6146 for (unsigned i = 1; i <= arg_count; ++i)
6147 {
6148 if (i > 1)
6149 pf->raw_components += " ";
6150 target_symbol *tsym = new target_symbol;
6151 tsym->tok = e->tok;
6152 tsym->name = "$arg" + lex_cast(i);
6153 pf->raw_components += tsym->name;
6154 tsym->components = e->components;
6155
8c2f50c0 6156 expression *texp = require<expression> (tsym);
6ef331c8
SC
6157 if (!e->components.empty() &&
6158 e->components[0].type == target_symbol::comp_pretty_print)
6159 pf->raw_components += "=%s";
6160 else
6161 pf->raw_components += "=%#x";
6162 pf->args.push_back(texp);
6163 }
6164
6165 pf->components = print_format::string_to_components(pf->raw_components);
6166 provide (pf);
6167 }
6168 else
6169 assert(0); // shouldn't get here
6170}
6171
03c45fcb
JL
6172unsigned
6173sdt_uprobe_var_expanding_visitor::get_target_symbol_argno_and_validate (target_symbol *e)
6174{
6175 // parsing
6176 unsigned argno = 0;
6177 if (startswith(e->name, "$arg"))
6178 {
6179 try
6180 {
6181 argno = lex_cast<unsigned>(e->name.substr(4));
6182 }
6183 catch (const runtime_error& f)
6184 {
6185 // non-integral $arg suffix: e.g. $argKKKSDF
6186 argno = 0;
6187 }
6188 }
6189
6190 // validation
6191 if (arg_count == 0 || // a sdt.h variant without .probe-stored arg_count
6192 argno < 1 || argno > arg_count) // a $argN with out-of-range N
6193 {
6194 // NB: Either
6195 // 1) uprobe1_type $argN or $FOO (we don't know the arg_count)
6196 // 2) uprobe2_type $FOO (no probe args)
6197 // both of which get resolved later.
6198 // Throw it now, and it might be resolved by DWARF later.
6199 need_debug_info = true;
6200 throw SEMANTIC_ERROR(_("target-symbol requires debuginfo"), e->tok);
6201 }
6202 assert (arg_tokens.size() >= argno);
6203 return argno;
6204}
6ef331c8 6205
ac074daf
JL
6206long
6207sdt_uprobe_var_expanding_visitor::parse_out_arg_precision(string& asmarg)
6208{
6209 long precision;
6210 if (asmarg.find('@') != string::npos)
6211 {
6212 precision = lex_cast<int>(asmarg.substr(0, asmarg.find('@')));
6213 asmarg = asmarg.substr(asmarg.find('@')+1);
6214 }
6215 else
6216 {
6217 // V1/V2 do not have precision field so default to signed long
6218 // V3 asm does not have precision field so default to unsigned long
6219 if (probe_type == uprobe3_type)
6220 precision = sizeof(long); // this is an asm probe
6221 else
6222 precision = -sizeof(long);
6223 }
6224 return precision;
6225}
6226
0fbf6fb8
JL
6227expression*
6228sdt_uprobe_var_expanding_visitor::try_parse_arg_literal (target_symbol *e,
6229 const string& asmarg,
6230 long precision)
6231{
6232 expression *argexpr = NULL;
6233
6234 // Here, we test for a numeric literal.
6235 // Only accept (signed) decimals throughout. XXX
6236
6237 // PR11821. NB: on powerpc, literals are not prefixed with $,
6238 // so this regex does not match. But that's OK, since without
6239 // -mregnames, we can't tell them apart from register numbers
6240 // anyway. With -mregnames, we could, if gcc somehow
6241 // communicated to us the presence of that option, but alas it
6242 // doesn't. http://gcc.gnu.org/PR44995.
6243 vector<string> matches;
6244 if (!regexp_match (asmarg, "^[i\\$#][-]?[0-9][0-9]*$", matches))
6245 {
6246 string sn = matches[0].substr(1);
6247 int64_t n;
6248
6249 // We have to pay attention to the size & sign, as gcc sometimes
6250 // propagates constants that don't quite match, like a negative
6251 // value to fill an unsigned type.
6252 // NB: let it throw if something happens
6253 switch (precision)
6254 {
6255 case -1: n = lex_cast< int8_t>(sn); break;
6256 case 1: n = lex_cast< uint8_t>(sn); break;
6257 case -2: n = lex_cast< int16_t>(sn); break;
6258 case 2: n = lex_cast<uint16_t>(sn); break;
6259 case -4: n = lex_cast< int32_t>(sn); break;
6260 case 4: n = lex_cast<uint32_t>(sn); break;
6261 default:
6262 case -8: n = lex_cast< int64_t>(sn); break;
6263 case 8: n = lex_cast<uint64_t>(sn); break;
6264 }
6265
6266 literal_number* ln = new literal_number(n);
6267 ln->tok = e->tok;
6268 argexpr = ln;
6269 }
6270
6271 return argexpr;
6272}
6273
95b78bc9
JL
6274expression*
6275sdt_uprobe_var_expanding_visitor::try_parse_arg_register (target_symbol *e,
6276 const string& asmarg,
6277 long precision)
6278{
6279 expression *argexpr = NULL;
6280
6281 // test for REGISTER
6282 // NB: Because PR11821, we must use percent_regnames here.
6283 string regexp;
6284 if (elf_machine == EM_PPC || elf_machine == EM_PPC64 || elf_machine == EM_ARM)
6285 regexp = "^(" + regnames + ")$";
6286 else
6287 regexp = "^(" + percent_regnames + ")$";
6288
6289 vector<string> matches;
6290 if (!regexp_match(asmarg, regexp, matches))
6291 {
6292 string regname = matches[1];
6293 map<string,pair<unsigned,int> >::iterator ri = dwarf_regs.find (regname);
6294 if (ri != dwarf_regs.end()) // known register
6295 {
6296 embedded_expr *get_arg1 = new embedded_expr;
6297 string width_adjust;
6298 switch (ri->second.second)
6299 {
6300 case QI: width_adjust = ") & 0xff)"; break;
6301 case QIh: width_adjust = ">>8) & 0xff)"; break;
6302 case HI:
6303 // preserve 16 bit register signness
6304 width_adjust = ") & 0xffff)";
6305 if (precision < 0)
6306 width_adjust += " << 48 >> 48";
6307 break;
6308 case SI:
6309 // preserve 32 bit register signness
6310 width_adjust = ") & 0xffffffff)";
6311 if (precision < 0)
6312 width_adjust += " << 32 >> 32";
6313 break;
6314 default: width_adjust = "))";
6315 }
6316 string type = "";
6317 if (probe_type == uprobe3_type)
6318 type = (precision < 0
6319 ? "(int" : "(uint") + lex_cast(abs(precision) * 8) + "_t)";
6320 type = type + "((";
6321 get_arg1->tok = e->tok;
6322 get_arg1->code = string("/* unprivileged */ /* pure */")
6323 + string(" ((int64_t)") + type
a57b7534 6324 + string("u_fetch_register(")
95b78bc9
JL
6325 + lex_cast(dwarf_regs[regname].first) + string("))")
6326 + width_adjust;
6327 argexpr = get_arg1;
6328 }
6329 }
6330 return argexpr;
6331}
6332
b15f98b2
JL
6333static string
6334precision_to_function(long precision)
6335{
6336 switch (precision)
6337 {
6338 case 1: case -1:
6339 return "user_int8";
6340 case 2:
6341 return "user_uint16";
6342 case -2:
6343 return "user_int16";
6344 case 4:
6345 return "user_uint32";
6346 case -4:
6347 return "user_int32";
6348 case 8: case -8:
6349 return "user_int64";
6350 default:
6351 return "user_long";
6352 }
6353}
6354
8da56b62
JL
6355expression*
6356sdt_uprobe_var_expanding_visitor::try_parse_arg_offset_register (target_symbol *e,
6357 const string& asmarg,
6358 long precision)
6359{
6360 expression *argexpr = NULL;
6361
6362 // test for OFFSET(REGISTER) where OFFSET is +-N+-N+-N
6363 // NB: Despite PR11821, we can use regnames here, since the parentheses
6364 // make things unambiguous. (Note: gdb/stap-probe.c also parses this)
6365 // On ARM test for [REGISTER, OFFSET]
6366
6367 string regexp;
6368 int reg, offset1;
6369 if (elf_machine == EM_ARM)
6370 {
6371 regexp = "^\\[(" + regnames + ")(, #([+-]?[0-9]+)([+-][0-9]*)?([+-][0-9]*)?)?\\]$";
6372 reg = 1;
6373 offset1 = 3;
6374 }
6375 else
6376 {
6377 regexp = "^([+-]?[0-9]*)([+-][0-9]*)?([+-][0-9]*)?[(](" + regnames + ")[)]$";
6378 reg = 4;
6379 offset1 = 1;
6380 }
6381
6382 vector<string> matches;
6383 if (!regexp_match(asmarg, regexp, matches))
6384 {
6385 string regname;
6386 int64_t disp = 0;
6387 if (matches[reg].length())
6388 regname = matches[reg];
6389 if (dwarf_regs.find (regname) == dwarf_regs.end())
6390 throw SEMANTIC_ERROR(_F("unrecognized register '%s'", regname.c_str()));
6391
6392 for (int i=offset1; i <= (offset1 + 2); i++)
6393 if (matches[i].length())
6394 // should decode positive/negative hex/decimal
6395 // NB: let it throw if something happens
6396 disp += lex_cast<int64_t>(matches[i]);
6397
6398 // synthesize user_long(%{fetch_register(R)%} + D)
6399 embedded_expr *get_arg1 = new embedded_expr;
6400 get_arg1->tok = e->tok;
6401 get_arg1->code = string("/* unprivileged */ /* pure */")
a57b7534 6402 + string("u_fetch_register(")
8da56b62
JL
6403 + lex_cast(dwarf_regs[regname].first) + string(")");
6404 // XXX: may we ever need to cast that to a narrower type?
6405
6406 literal_number* inc = new literal_number(disp);
6407 inc->tok = e->tok;
6408
6409 binary_expression *be = new binary_expression;
6410 be->tok = e->tok;
6411 be->left = get_arg1;
6412 be->op = "+";
6413 be->right = inc;
6414
6415 functioncall *fc = new functioncall;
b15f98b2 6416 fc->function = precision_to_function(precision);
8da56b62
JL
6417 fc->tok = e->tok;
6418 fc->args.push_back(be);
6419
6420 argexpr = fc;
6421 }
6422
6423 return argexpr;
6424}
6425
10fcca15
JL
6426expression*
6427sdt_uprobe_var_expanding_visitor::try_parse_arg_effective_addr (target_symbol *e,
6428 const string& asmarg,
6429 long precision)
6430{
6431 expression *argexpr = NULL;
6432
6433 // test for OFFSET(BASE_REGISTER,INDEX_REGISTER[,SCALE]) where OFFSET is +-N+-N+-N
6434 // NB: Despite PR11821, we can use regnames here, since the parentheses
6435 // make things unambiguous. (Note: gdb/stap-probe.c also parses this)
6436 string regexp = "^([+-]?[0-9]*)([+-][0-9]*)?([+-][0-9]*)?[(](" + regnames + "),(" +
6437 regnames + ")(,[1248])?[)]$";
6438 vector<string> matches;
6439 if (!regexp_match(asmarg, regexp, matches))
6440 {
6441 string baseregname;
6442 string indexregname;
6443 int64_t disp = 0;
6444 short scale = 1;
6445
6446 if (matches[6].length())
6447 // NB: let it throw if we can't cast
6448 scale = lex_cast<short>(matches[6].substr(1)); // NB: skip the comma!
6449
6450 if (matches[4].length())
6451 baseregname = matches[4];
6452 if (dwarf_regs.find (baseregname) == dwarf_regs.end())
6453 throw SEMANTIC_ERROR(_F("unrecognized base register '%s'", baseregname.c_str()));
6454
6455 if (matches[5].length())
6456 indexregname = matches[5];
6457 if (dwarf_regs.find (indexregname) == dwarf_regs.end())
6458 throw SEMANTIC_ERROR(_F("unrecognized index register '%s'", indexregname.c_str()));
6459
6460 for (int i = 1; i <= 3; i++) // up to three OFFSET terms
6461 if (matches[i].length())
6462 // should decode positive/negative hex/decimal
6463 // NB: let it throw if something happens
6464 disp += lex_cast<int64_t>(matches[i]);
6465
6466 // synthesize user_long(%{fetch_register(R1)+fetch_register(R2)*N%} + D)
6467
6468 embedded_expr *get_arg1 = new embedded_expr;
a57b7534 6469 string regfn = "u_fetch_register";
10fcca15
JL
6470
6471 get_arg1->tok = e->tok;
6472 get_arg1->code = string("/* unprivileged */ /* pure */")
6473 + regfn + string("(")+lex_cast(dwarf_regs[baseregname].first)+string(")")
6474 + string("+(")
6475 + regfn + string("(")+lex_cast(dwarf_regs[indexregname].first)+string(")")
6476 + string("*")
6477 + lex_cast(scale)
6478 + string(")");
6479
6480 // NB: could plop this +DISPLACEMENT bit into the embedded-c expression too
6481 literal_number* inc = new literal_number(disp);
6482 inc->tok = e->tok;
6483
6484 binary_expression *be = new binary_expression;
6485 be->tok = e->tok;
6486 be->left = get_arg1;
6487 be->op = "+";
6488 be->right = inc;
6489
6490 functioncall *fc = new functioncall;
b15f98b2 6491 fc->function = precision_to_function(precision);
10fcca15
JL
6492 fc->tok = e->tok;
6493 fc->args.push_back(be);
6494
6495 argexpr = fc;
6496 }
6497
6498 return argexpr;
6499}
6500
451c0c25
JL
6501expression*
6502sdt_uprobe_var_expanding_visitor::try_parse_arg_varname (target_symbol *e,
6503 const string& asmarg,
6504 long precision)
6ef331c8 6505{
b8688ee6 6506 static unsigned tick = 0;
451c0c25
JL
6507 expression *argexpr = NULL;
6508
6509 // test for [OFF+]VARNAME[+OFF][(REGISTER)], where VARNAME is a variable
6510 // name. NB: Despite PR11821, we can use regnames here, since the parentheses
6511 // make things unambiguous.
6512 string regex = "^(([0-9]+)[+])?([a-zA-Z_][a-zA-Z0-9_]*)([+][0-9]+)?([(]("
6513 + regnames + ")[)])?$";
6514 vector<string> matches;
6515 if (!regexp_match(asmarg, regex, matches))
6516 {
6517 assert(matches.size() >= 4);
6518 string varname = matches[3];
6519
6520 // OFF can be before VARNAME (put in matches[2]) or after (put in
6521 // matches[4]) (or both?). Seems like in most cases it comes after,
6522 // unless the code was compiled with -fPIC.
6523 int64_t offset = 0;
6524 if (!matches[2].empty())
6525 offset += lex_cast<int64_t>(matches[2]);
6526 if (matches.size() >= 5 && !matches[4].empty())
6527 offset += lex_cast<int64_t>(matches[4]);
6528
6529 string regname;
6530 if (matches.size() >= 7)
6531 regname = matches[6];
6532
6533 // If it's just VARNAME, then proceed. If it's VARNAME(REGISTER), then
6534 // only proceed if it's RIP-relative addressing on x86_64.
6535 if (regname.empty() || (regname == "%rip" && elf_machine == EM_X86_64))
6536 {
6537 dw.mod_info->get_symtab();
6538 if (dw.mod_info->symtab_status != info_present)
6539 throw SEMANTIC_ERROR(_("can't retrieve symbol table"));
6540
6541 assert(dw.mod_info->sym_table);
6542 map<string, Dwarf_Addr>& globals = dw.mod_info->sym_table->globals;
6543 map<string, Dwarf_Addr>& locals = dw.mod_info->sym_table->locals;
6544 Dwarf_Addr addr = 0;
6545
6546 // check symtab locals then globals
6547 if (locals.count(varname))
6548 addr = locals[varname];
6549 if (globals.count(varname))
6550 addr = globals[varname];
6551
6552 if (addr)
6553 {
6554 // add whatever offset is in the operand
6555 addr += offset;
6556
6557 // adjust for dw bias because relocate_address() expects a
6558 // libdw address and this addr is from the symtab
6559 dw.get_module_dwarf(false, false);
6560 addr -= dw.module_bias;
6561
6562 string reloc_section;
6563 Dwarf_Addr reloc_addr = dw.relocate_address(addr, reloc_section);
6564
6565 // OK, we have an address for the variable. Let's create a
6566 // function that will just relocate it at runtime, and then
6567 // call user_[u]int*() on the address it returns.
6568
6569 functioncall *user_int_call = new functioncall;
b15f98b2 6570 user_int_call->function = precision_to_function(precision);
451c0c25
JL
6571 user_int_call->tok = e->tok;
6572
6573 functiondecl *get_addr_decl = new functiondecl;
6574 get_addr_decl->tok = e->tok;
6575 get_addr_decl->synthetic = true;
6576 get_addr_decl->name = "_sdt_arg_get_addr_" + lex_cast(tick++);
6577 get_addr_decl->type = pe_long;
6578
6bc53f02 6579 // build _stp_umodule_relocate(module, addr, current)
451c0c25
JL
6580 stringstream ss;
6581 ss << " /* unprivileged */ /* pure */ /* pragma:vma */" << endl;
a57b7534
JL
6582 ss << "STAP_RETURN(_stp_umodule_relocate(";
6583 ss << "\"" << path_remove_sysroot(session, process_name) << "\", ";
6584 ss << "0x" << hex << reloc_addr << dec << ", ";
6585 ss << "current";
6586 ss << "));" << endl;
451c0c25
JL
6587
6588 embeddedcode *ec = new embeddedcode;
6589 ec->tok = e->tok;
6590 ec->code = ss.str();
6591 get_addr_decl->body = ec;
6592 get_addr_decl->join(session);
6593
6594 functioncall *get_addr_call = new functioncall;
6595 get_addr_call->tok = e->tok;
6596 get_addr_call->function = get_addr_decl->name;
6597 user_int_call->args.push_back(get_addr_call);
b8688ee6 6598
451c0c25
JL
6599 argexpr = user_int_call;
6600 }
6601 }
6602 }
6603
6604 return argexpr;
6605}
6606
6607void
6608sdt_uprobe_var_expanding_visitor::visit_target_symbol_arg (target_symbol *e)
6609{
6ef331c8
SC
6610 try
6611 {
03c45fcb 6612 unsigned argno = get_target_symbol_argno_and_validate(e); // the N in $argN
8aabf152 6613 string asmarg = arg_tokens[argno-1]; // $arg1 => arg_tokens[0]
c57ea854 6614
8aabf152
FCE
6615 // Now we try to parse this thing, which is an assembler operand
6616 // expression. If we can't, we warn, back down to need_debug_info
b874bd52 6617 // and hope for the best. Here is the syntax for a few architectures.
9859b766 6618 // Note that the power iN syntax is only for V3 sdt.h; gcc emits the i.
8095a157 6619 //
b6aaaf21
JL
6620 // literal reg reg reg+ base+index*size+ VAR VAR+off RIP-relative
6621 // indirect offset offset VAR+off
6622 // x86 $N %rR (%rR) N(%rR) O(%bR,%iR,S) var var+off var+off(%rip)
6623 // x86_64 $N %rR (%rR) N(%rR) O(%bR,%iR,S) var var+off var+off(%rip)
6624 // power iN R (R) N(R)
6625 // ia64 N rR [r16]
6626 // s390 N %rR 0(rR) N(r15)
6627 // arm #N rR [rR] [rR, #N]
b874bd52 6628
8aabf152
FCE
6629 expression* argexpr = 0; // filled in in case of successful parse
6630
ac074daf
JL
6631 // Parse (and remove from asmarg) the leading length
6632 long precision = parse_out_arg_precision(asmarg);
40fe32e0 6633
0fbf6fb8 6634 try
8aabf152 6635 {
0fbf6fb8
JL
6636 if ((argexpr = try_parse_arg_literal(e, asmarg, precision)) != NULL)
6637 goto matched;
2e6d617a
JL
6638
6639 // all other matches require registers
6640 if (regnames == "")
6641 throw SEMANTIC_ERROR("no registers to use for parsing");
95b78bc9
JL
6642
6643 if ((argexpr = try_parse_arg_register(e, asmarg, precision)) != NULL)
6644 goto matched;
8da56b62
JL
6645 if ((argexpr = try_parse_arg_offset_register(e, asmarg, precision)) != NULL)
6646 goto matched;
10fcca15
JL
6647 if ((argexpr = try_parse_arg_effective_addr(e, asmarg, precision)) != NULL)
6648 goto matched;
451c0c25
JL
6649 if ((argexpr = try_parse_arg_varname(e, asmarg, precision)) != NULL)
6650 goto matched;
0fbf6fb8
JL
6651 }
6652 catch (const semantic_error& err)
6653 {
6654 e->chain(err);
b8688ee6 6655 }
8aabf152 6656
8aabf152
FCE
6657 // The asmarg operand was not recognized. Back down to dwarf.
6658 if (! session.suppress_warnings)
84fef8ee
FCE
6659 {
6660 if (probe_type == UPROBE3_TYPE)
451c0c25
JL
6661 session.print_warning (_F("Can't parse SDT_V3 operand '%s' "
6662 "[man error::sdt]", asmarg.c_str()),
6663 e->tok);
84fef8ee 6664 else // must be *PROBE2; others don't get asm operands
451c0c25
JL
6665 session.print_warning (_F("Downgrading SDT_V2 probe argument to "
6666 "dwarf, can't parse '%s' [man error::sdt]",
6667 asmarg.c_str()),
6668 e->tok);
84fef8ee 6669 }
451c0c25 6670
8aabf152 6671 need_debug_info = true;
451c0c25
JL
6672 throw SEMANTIC_ERROR(_("SDT asm not understood, requires debuginfo "
6673 "[man error::sdt]"), e->tok);
6674
6675 /* NOTREACHED */
366af4e7 6676
8aabf152
FCE
6677 matched:
6678 assert (argexpr != 0);
366af4e7
RM
6679
6680 if (session.verbose > 2)
1e41115c 6681 //TRANSLATORS: We're mapping the operand to a new expression*.
b530b5b3 6682 clog << _F("mapped asm operand %s to ", asmarg.c_str()) << *argexpr << endl;
366af4e7 6683
aff5d390 6684 if (e->components.empty()) // We have a scalar
8aabf152
FCE
6685 {
6686 if (e->addressof)
dc09353a 6687 throw SEMANTIC_ERROR(_("cannot take address of sdt variable"), e->tok);
8aabf152 6688 provide (argexpr);
8aabf152
FCE
6689 }
6690 else // $var->foo
6691 {
6692 cast_op *cast = new cast_op;
6693 cast->name = "@cast";
6694 cast->tok = e->tok;
6695 cast->operand = argexpr;
6696 cast->components = e->components;
6697 cast->type_name = probe_name + "_arg" + lex_cast(argno);
6698 cast->module = process_name;
6699 cast->visit(this);
8aabf152 6700 }
aff5d390
SC
6701 }
6702 catch (const semantic_error &er)
6703 {
6704 e->chain (er);
6705 provide (e);
6706 }
6707}
6708
6709
6ef331c8
SC
6710void
6711sdt_uprobe_var_expanding_visitor::visit_target_symbol (target_symbol* e)
6712{
6713 try
6714 {
49131a6d 6715 assert(e->name.size() > 0
bd1fcbad 6716 && (e->name[0] == '$' || e->name == "@var"));
6ef331c8
SC
6717
6718 if (e->name == "$$name" || e->name == "$$provider" || e->name == "$$parms" || e->name == "$$vars")
6719 visit_target_symbol_context (e);
6720 else
6721 visit_target_symbol_arg (e);
6722 }
6723 catch (const semantic_error &er)
6724 {
6725 e->chain (er);
6726 provide (e);
6727 }
6728}
6729
6730
bd1fcbad
YZ
6731void
6732sdt_uprobe_var_expanding_visitor::visit_atvar_op (atvar_op* e)
6733{
6734 need_debug_info = true;
6735
6736 // Fill in our current module context if needed
6737 if (e->module.empty())
6738 e->module = process_name;
6739
6740 var_expanding_visitor::visit_atvar_op(e);
6741}
6742
6743
40a0c64e
JS
6744void
6745sdt_uprobe_var_expanding_visitor::visit_cast_op (cast_op* e)
6746{
6747 // Fill in our current module context if needed
6748 if (e->module.empty())
6749 e->module = process_name;
6750
6751 var_expanding_visitor::visit_cast_op(e);
6752}
6753
6754
576eaefe
SC
6755void
6756plt_expanding_visitor::visit_target_symbol (target_symbol *e)
6757{
6758 try
6759 {
6760 if (e->name == "$$name")
6761 {
6762 literal_string *myname = new literal_string (entry);
6763 myname->tok = e->tok;
6764 provide(myname);
6765 return;
6766 }
3d69c03f
JS
6767
6768 // variable not found -> throw a semantic error
6769 // (only to be caught right away, but this may be more complex later...)
6770 string alternatives = "$$name";
dc09353a 6771 throw SEMANTIC_ERROR(_F("unable to find plt variable '%s' (alternatives: %s)",
3d69c03f 6772 e->name.c_str(), alternatives.c_str()), e->tok);
576eaefe
SC
6773 }
6774 catch (const semantic_error &er)
6775 {
6776 e->chain (er);
6777 provide (e);
6778 }
6779}
6780
6781
edce5b67
JS
6782struct sdt_query : public base_query
6783{
6784 sdt_query(probe * base_probe, probe_point * base_loc,
6785 dwflpp & dw, literal_map_t const & params,
51d6bda3 6786 vector<derived_probe *> & results, const string user_lib);
edce5b67 6787
51d6bda3 6788 void query_library (const char *data);
614f0fcf
JL
6789 set<string> visited_libraries;
6790 bool resolved_library;
6791
576eaefe 6792 void query_plt (const char *entry, size_t addr) {}
edce5b67
JS
6793 void handle_query_module();
6794
6795private:
15284963 6796 stap_sdt_probe_type probe_type;
d61ea602 6797 enum { probe_section=0, note_section=1, unknown_section=-1 } probe_loc;
edce5b67
JS
6798 probe * base_probe;
6799 probe_point * base_loc;
6846cfc8 6800 literal_map_t const & params;
edce5b67 6801 vector<derived_probe *> & results;
a794dbeb
FCE
6802 string pp_mark;
6803 string pp_provider;
51d6bda3 6804 string user_lib;
edce5b67
JS
6805
6806 set<string> probes_handled;
6807
6808 Elf_Data *pdata;
6809 size_t probe_scn_offset;
6810 size_t probe_scn_addr;
aff5d390 6811 uint64_t arg_count;
40fe32e0 6812 GElf_Addr base;
c57ea854 6813 GElf_Addr pc;
aff5d390 6814 string arg_string;
edce5b67 6815 string probe_name;
a794dbeb 6816 string provider_name;
909ab234 6817 GElf_Addr semaphore_load_offset;
79a0ca08 6818 Dwarf_Addr semaphore;
edce5b67
JS
6819
6820 bool init_probe_scn();
6b51ee12 6821 bool get_next_probe();
c57ea854
SC
6822 void iterate_over_probe_entries();
6823 void handle_probe_entry();
edce5b67 6824
488d3c1b 6825 static void setup_note_probe_entry_callback (sdt_query *me,
dbd24034
JL
6826 const string& scn_name,
6827 const string& note_name,
488d3c1b
JL
6828 int type,
6829 const char *data,
6830 size_t len);
dbd24034
JL
6831 void setup_note_probe_entry (const string& scn_name,
6832 const string& note_name, int type,
6833 const char *data, size_t len);
40fe32e0 6834
edce5b67 6835 void convert_probe(probe *base);
4ddb6dd0 6836 void record_semaphore(vector<derived_probe *> & results, unsigned start);
c72aa911 6837 probe* convert_location();
40fe32e0 6838 bool have_uprobe() {return probe_type == uprobe1_type || probe_type == uprobe2_type || probe_type == uprobe3_type;}
c57ea854
SC
6839 bool have_debuginfo_uprobe(bool need_debug_info)
6840 {return probe_type == uprobe1_type
40fe32e0 6841 || ((probe_type == uprobe2_type || probe_type == uprobe3_type)
c57ea854 6842 && need_debug_info);}
40fe32e0 6843 bool have_debuginfoless_uprobe() {return probe_type == uprobe2_type || probe_type == uprobe3_type;}
edce5b67
JS
6844};
6845
6846
6847sdt_query::sdt_query(probe * base_probe, probe_point * base_loc,
6848 dwflpp & dw, literal_map_t const & params,
51d6bda3 6849 vector<derived_probe *> & results, const string user_lib):
614f0fcf
JL
6850 base_query(dw, params), resolved_library(false),
6851 probe_type(unknown_probe_type), probe_loc(unknown_section),
6852 base_probe(base_probe), base_loc(base_loc), params(params), results(results),
6853 user_lib(user_lib), probe_scn_offset(0), probe_scn_addr(0), arg_count(0),
6854 base(0), pc(0), semaphore_load_offset(0), semaphore(0)
edce5b67 6855{
a794dbeb
FCE
6856 assert(get_string_param(params, TOK_MARK, pp_mark));
6857 get_string_param(params, TOK_PROVIDER, pp_provider); // pp_provider == "" -> unspecified
6858
ef428667
FCE
6859 // PR10245: permit usage of dtrace-y "-" separator in marker name;
6860 // map it to double-underscores.
6861 size_t pos = 0;
6862 while (1) // there may be more than one
6863 {
a794dbeb 6864 size_t i = pp_mark.find("-", pos);
ef428667 6865 if (i == string::npos) break;
a794dbeb 6866 pp_mark.replace (i, 1, "__");
ef428667
FCE
6867 pos = i+1; // resume searching after the inserted __
6868 }
a794dbeb
FCE
6869
6870 // XXX: same for pp_provider?
edce5b67
JS
6871}
6872
6873
6874void
c57ea854 6875sdt_query::handle_probe_entry()
edce5b67 6876{
c57ea854
SC
6877 if (! have_uprobe()
6878 && !probes_handled.insert(probe_name).second)
edce5b67
JS
6879 return;
6880
6881 if (sess.verbose > 3)
c57ea854 6882 {
b78a0fbb 6883 //TRANSLATORS: Describing what probe type (kprobe or uprobe) the probe
b530b5b3
LB
6884 //TRANSLATORS: is matched to.
6885 clog << _F("matched probe_name %s probe type ", probe_name.c_str());
c57ea854
SC
6886 switch (probe_type)
6887 {
6888 case uprobe1_type:
6889 clog << "uprobe1 at 0x" << hex << pc << dec << endl;
6890 break;
6891 case uprobe2_type:
6892 clog << "uprobe2 at 0x" << hex << pc << dec << endl;
6893 break;
40fe32e0
SC
6894 case uprobe3_type:
6895 clog << "uprobe3 at 0x" << hex << pc << dec << endl;
6896 break;
d61ea602
JS
6897 default:
6898 clog << "unknown!" << endl;
6899 break;
c57ea854
SC
6900 }
6901 }
edce5b67 6902
c57ea854
SC
6903 // Extend the derivation chain
6904 probe *new_base = convert_location();
6905 probe_point *new_location = new_base->locations[0];
6906
c57ea854
SC
6907 bool need_debug_info = false;
6908
7d395255
JS
6909 // We could get the Elf* from either dwarf_getelf(dwfl_module_getdwarf(...))
6910 // or dwfl_module_getelf(...). We only need it for the machine type, which
6911 // should be the same. The bias is used for relocating debuginfoless probes,
6912 // though, so that must come from the possibly-prelinked ELF file, not DWARF.
c57ea854 6913 Dwarf_Addr bias;
7d395255 6914 Elf* elf = dwfl_module_getelf (dw.mod_info->mod, &bias);
c57ea854 6915
1cc41cd6
DS
6916 /* Figure out the architecture of this particular ELF file. The
6917 dwarfless register-name mappings depend on it. */
6918 GElf_Ehdr ehdr_mem;
6919 GElf_Ehdr* em = gelf_getehdr (elf, &ehdr_mem);
e1c3b13a 6920 if (em == 0) { DWFL_ASSERT ("dwfl_getehdr", dwfl_errno()); }
d8f31d0a 6921 assert(em);
1cc41cd6 6922 int elf_machine = em->e_machine;
b8688ee6
JL
6923 sdt_uprobe_var_expanding_visitor svv (sess, dw, elf_machine, module_val,
6924 provider_name, probe_name, probe_type,
6925 arg_string, arg_count);
1cc41cd6
DS
6926 svv.replace (new_base->body);
6927 need_debug_info = svv.need_debug_info;
c57ea854 6928
1cc41cd6
DS
6929 // XXX: why not derive_probes() in the uprobes case too?
6930 literal_map_t params;
6931 for (unsigned i = 0; i < new_location->components.size(); ++i)
6932 {
6933 probe_point::component *c = new_location->components[i];
6934 params[c->functor] = c->arg;
6935 }
c57ea854 6936
73d53dd2 6937 unsigned prior_results_size = results.size();
1cc41cd6
DS
6938 dwarf_query q(new_base, new_location, dw, params, results, "", "");
6939 q.has_mark = true; // enables mid-statement probing
30263a73 6940
73d53dd2
JS
6941 // V1 probes always need dwarf info
6942 // V2+ probes need dwarf info in case of a variable reference
1cc41cd6 6943 if (have_debuginfo_uprobe(need_debug_info))
06de3a04 6944 dw.iterate_over_modules<base_query>(&query_module, &q);
73d53dd2
JS
6945
6946 // For V2+ probes, if variable references weren't used or failed (PR14369),
6947 // then try with the more direct approach. Unresolved $vars might still
6948 // cause their own error, but this gives them a chance to be optimized out.
6949 if (have_debuginfoless_uprobe() && results.size() == prior_results_size)
1cc41cd6
DS
6950 {
6951 string section;
6952 Dwarf_Addr reloc_addr = q.statement_num_val + bias;
6953 if (dwfl_module_relocations (q.dw.mod_info->mod) > 0)
6954 {
6955 dwfl_module_relocate_address (q.dw.mod_info->mod, &reloc_addr);
6956 section = ".dynamic";
6957 }
6958 else
6959 section = ".absolute";
edce5b67 6960
1cc41cd6
DS
6961 uprobe_derived_probe* p =
6962 new uprobe_derived_probe ("", "", 0,
6963 path_remove_sysroot(sess,q.module_val),
6964 section,
6965 q.statement_num_val, reloc_addr, q, 0);
6966 p->saveargs (arg_count);
6967 results.push_back (p);
c57ea854 6968 }
487bf4e2 6969 sess.unwindsym_modules.insert (dw.module_name);
73d53dd2 6970 record_semaphore(results, prior_results_size);
c57ea854 6971}
edce5b67 6972
4ddb6dd0 6973
c57ea854
SC
6974void
6975sdt_query::handle_query_module()
6976{
6977 if (!init_probe_scn())
6978 return;
edce5b67 6979
c57ea854
SC
6980 if (sess.verbose > 3)
6981 clog << "TOK_MARK: " << pp_mark << " TOK_PROVIDER: " << pp_provider << endl;
edce5b67 6982
40fe32e0
SC
6983 if (probe_loc == note_section)
6984 {
6985 GElf_Shdr shdr_mem;
6986 GElf_Shdr *shdr = dw.get_section (".stapsdt.base", &shdr_mem);
6987
909ab234
JS
6988 // The 'base' lets us adjust the hardcoded addresses in notes for prelink
6989 // effects. The 'semaphore_load_offset' accounts for the difference in
6990 // load addresses between text and data, so the semaphore can be
6991 // converted to a file offset if needed.
40fe32e0 6992 if (shdr)
909ab234
JS
6993 {
6994 base = shdr->sh_addr;
6995 GElf_Addr base_offset = shdr->sh_offset;
6996 shdr = dw.get_section (".probes", &shdr_mem);
6997 if (shdr)
6998 semaphore_load_offset =
6999 (shdr->sh_addr - shdr->sh_offset) - (base - base_offset);
7000 }
40fe32e0 7001 else
909ab234
JS
7002 base = semaphore_load_offset = 0;
7003
488d3c1b 7004 dw.iterate_over_notes (this, &sdt_query::setup_note_probe_entry_callback);
40fe32e0 7005 }
d61ea602 7006 else if (probe_loc == probe_section)
40fe32e0 7007 iterate_over_probe_entries ();
edce5b67
JS
7008}
7009
7010
7011bool
7012sdt_query::init_probe_scn()
7013{
448a86b7 7014 Elf* elf;
edce5b67 7015 GElf_Shdr shdr_mem;
40fe32e0
SC
7016
7017 GElf_Shdr *shdr = dw.get_section (".note.stapsdt", &shdr_mem);
7018 if (shdr)
7019 {
7020 probe_loc = note_section;
7021 return true;
7022 }
edce5b67 7023
448a86b7 7024 shdr = dw.get_section (".probes", &shdr_mem, &elf);
fea74777 7025 if (shdr)
edce5b67 7026 {
fea74777
SC
7027 pdata = elf_getdata_rawchunk (elf, shdr->sh_offset, shdr->sh_size, ELF_T_BYTE);
7028 probe_scn_offset = 0;
7029 probe_scn_addr = shdr->sh_addr;
7030 assert (pdata != NULL);
7031 if (sess.verbose > 4)
ce0f6648
LB
7032 clog << "got .probes elf scn_addr@0x" << probe_scn_addr << ", size: "
7033 << pdata->d_size << endl;
40fe32e0 7034 probe_loc = probe_section;
fea74777 7035 return true;
edce5b67 7036 }
fea74777 7037 else
edce5b67 7038 return false;
edce5b67
JS
7039}
7040
40fe32e0 7041void
dbd24034
JL
7042sdt_query::setup_note_probe_entry_callback (sdt_query *me,
7043 const string& scn_name,
7044 const string& note_name, int type,
7045 const char *data, size_t len)
40fe32e0 7046{
dbd24034 7047 me->setup_note_probe_entry (scn_name, note_name, type, data, len);
40fe32e0
SC
7048}
7049
7050
7051void
dbd24034
JL
7052sdt_query::setup_note_probe_entry (const string& scn_name,
7053 const string& note_name, int type,
7054 const char *data, size_t len)
40fe32e0 7055{
dbd24034
JL
7056 if (scn_name.compare(".note.stapsdt"))
7057 return;
7058#define _SDT_NOTE_NAME "stapsdt"
7059 if (note_name.compare(_SDT_NOTE_NAME))
7060 return;
40fe32e0
SC
7061#define _SDT_NOTE_TYPE 3
7062 if (type != _SDT_NOTE_TYPE)
7063 return;
7064
dbd24034 7065 // we found a probe entry
40fe32e0
SC
7066 union
7067 {
7068 Elf64_Addr a64[3];
7069 Elf32_Addr a32[3];
7070 } buf;
7071 Dwarf_Addr bias;
7072 Elf* elf = (dwfl_module_getelf (dw.mod_info->mod, &bias));
7073 Elf_Data dst =
7074 {
7075 &buf, ELF_T_ADDR, EV_CURRENT,
7076 gelf_fsize (elf, ELF_T_ADDR, 3, EV_CURRENT), 0, 0
7077 };
7078 assert (dst.d_size <= sizeof buf);
7079
7080 if (len < dst.d_size + 3)
7081 return;
7082
7083 Elf_Data src =
7084 {
7085 (void *) data, ELF_T_ADDR, EV_CURRENT,
7086 dst.d_size, 0, 0
7087 };
7088
7089 if (gelf_xlatetom (elf, &dst, &src,
7090 elf_getident (elf, NULL)[EI_DATA]) == NULL)
7091 printf ("gelf_xlatetom: %s", elf_errmsg (-1));
7092
7093 probe_type = uprobe3_type;
7094 const char * provider = data + dst.d_size;
3f803f9e 7095
40fe32e0 7096 const char *name = (const char*)memchr (provider, '\0', data + len - provider);
3f803f9e
CM
7097 if(name++ == NULL)
7098 return;
7099
7100 const char *args = (const char*)memchr (name, '\0', data + len - name);
7101 if (args++ == NULL || memchr (args, '\0', data + len - name) != data + len - 1)
7102 return;
7103
7104 provider_name = provider;
7105 probe_name = name;
7106 arg_string = args;
40fe32e0 7107
e90006d7
JL
7108 dw.mod_info->marks.insert(make_pair(provider, name));
7109
40fe32e0
SC
7110 // Did we find a matching probe?
7111 if (! (dw.function_name_matches_pattern (probe_name, pp_mark)
7112 && ((pp_provider == "")
7113 || dw.function_name_matches_pattern (provider_name, pp_provider))))
7114 return;
7115
e9a90eee
JS
7116 // PR13934: Assembly probes are not forced to use the N@OP form.
7117 // If we have '@' then great, else count based on space-delimiters.
7118 arg_count = count(arg_string.begin(), arg_string.end(), '@');
7119 if (!arg_count && !arg_string.empty())
7120 arg_count = 1 + count(arg_string.begin(), arg_string.end(), ' ');
7121
40fe32e0
SC
7122 GElf_Addr base_ref;
7123 if (gelf_getclass (elf) == ELFCLASS32)
7124 {
7125 pc = buf.a32[0];
7126 base_ref = buf.a32[1];
7127 semaphore = buf.a32[2];
7128 }
7129 else
7130 {
7131 pc = buf.a64[0];
7132 base_ref = buf.a64[1];
7133 semaphore = buf.a64[2];
7134 }
7135
7136 semaphore += base - base_ref;
7137 pc += base - base_ref;
7138
7d395255
JS
7139 // The semaphore also needs the ELF bias added now, so
7140 // record_semaphore can properly relocate it later.
7141 semaphore += bias;
7142
40fe32e0 7143 if (sess.verbose > 4)
b530b5b3 7144 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
7145
7146 handle_probe_entry();
7147}
7148
7149
c57ea854
SC
7150void
7151sdt_query::iterate_over_probe_entries()
edce5b67 7152{
c57ea854 7153 // probes are in the .probe section
edce5b67
JS
7154 while (probe_scn_offset < pdata->d_size)
7155 {
aff5d390
SC
7156 stap_sdt_probe_entry_v1 *pbe_v1 = (stap_sdt_probe_entry_v1 *) ((char*)pdata->d_buf + probe_scn_offset);
7157 stap_sdt_probe_entry_v2 *pbe_v2 = (stap_sdt_probe_entry_v2 *) ((char*)pdata->d_buf + probe_scn_offset);
15284963 7158 probe_type = (stap_sdt_probe_type)(pbe_v1->type_a);
1cc41cd6 7159 if (! have_uprobe())
edce5b67
JS
7160 {
7161 // Unless this is a mangled .probes section, this happens
7162 // because the name of the probe comes first, followed by
7163 // the sentinel.
7164 if (sess.verbose > 5)
b530b5b3 7165 clog << _F("got unknown probe_type : 0x%x", probe_type) << endl;
edce5b67
JS
7166 probe_scn_offset += sizeof(__uint32_t);
7167 continue;
7168 }
aff5d390
SC
7169 if ((long)pbe_v1 % sizeof(__uint64_t)) // we have stap_sdt_probe_entry_v1.type_b
7170 {
7171 pbe_v1 = (stap_sdt_probe_entry_v1*)((char*)pbe_v1 - sizeof(__uint32_t));
1cc41cd6 7172 if (pbe_v1->type_b != uprobe1_type)
aff5d390
SC
7173 continue;
7174 }
7175
1cc41cd6 7176 if (probe_type == uprobe1_type)
aff5d390 7177 {
79a0ca08 7178 if (pbe_v1->name == 0) // No name possibly means we have a .so with a relocation
c57ea854 7179 return;
79a0ca08 7180 semaphore = 0;
aff5d390 7181 probe_name = (char*)((char*)pdata->d_buf + pbe_v1->name - (char*)probe_scn_addr);
a794dbeb 7182 provider_name = ""; // unknown
1cc41cd6
DS
7183 pc = pbe_v1->arg;
7184 arg_count = 0;
aff5d390
SC
7185 probe_scn_offset += sizeof (stap_sdt_probe_entry_v1);
7186 }
08b5a50c 7187 else if (probe_type == uprobe2_type)
aff5d390 7188 {
79a0ca08 7189 if (pbe_v2->name == 0) // No name possibly means we have a .so with a relocation
c57ea854 7190 return;
79a0ca08 7191 semaphore = pbe_v2->semaphore;
aff5d390 7192 probe_name = (char*)((char*)pdata->d_buf + pbe_v2->name - (char*)probe_scn_addr);
a794dbeb 7193 provider_name = (char*)((char*)pdata->d_buf + pbe_v2->provider - (char*)probe_scn_addr);
aff5d390
SC
7194 arg_count = pbe_v2->arg_count;
7195 pc = pbe_v2->pc;
7196 if (pbe_v2->arg_string)
7197 arg_string = (char*)((char*)pdata->d_buf + pbe_v2->arg_string - (char*)probe_scn_addr);
79a0ca08
SC
7198 // skip over pbe_v2, probe_name text and provider text
7199 probe_scn_offset = ((long)(pbe_v2->name) - (long)(probe_scn_addr)) + probe_name.length();
7200 probe_scn_offset += sizeof (__uint32_t) - probe_scn_offset % sizeof (__uint32_t);
aff5d390 7201 }
edce5b67 7202 if (sess.verbose > 4)
b530b5b3 7203 clog << _("saw .probes ") << probe_name << (provider_name != "" ? _(" (provider ")+provider_name+") " : "")
aff5d390 7204 << "@0x" << hex << pc << dec << endl;
edce5b67 7205
e90006d7
JL
7206 dw.mod_info->marks.insert(make_pair(provider_name, probe_name));
7207
a794dbeb
FCE
7208 if (dw.function_name_matches_pattern (probe_name, pp_mark)
7209 && ((pp_provider == "") || dw.function_name_matches_pattern (provider_name, pp_provider)))
c57ea854 7210 handle_probe_entry ();
edce5b67 7211 }
edce5b67
JS
7212}
7213
7214
6846cfc8 7215void
4ddb6dd0 7216sdt_query::record_semaphore (vector<derived_probe *> & results, unsigned start)
6846cfc8 7217{
a794dbeb
FCE
7218 for (unsigned i=0; i<2; i++) {
7219 // prefer with-provider symbol; look without provider prefix for backward compatibility only
7220 string semaphore = (i==0 ? (provider_name+"_") : "") + probe_name + "_semaphore";
7221 // XXX: multiple addresses?
7222 if (sess.verbose > 2)
b530b5b3 7223 clog << _F("looking for semaphore symbol %s ", semaphore.c_str());
a794dbeb 7224
79a0ca08
SC
7225 Dwarf_Addr addr;
7226 if (this->semaphore)
7227 addr = this->semaphore;
7228 else
7229 addr = lookup_symbol_address(dw.module, semaphore.c_str());
a794dbeb
FCE
7230 if (addr)
7231 {
7d395255 7232 if (dwfl_module_relocations (dw.module) > 0)
a794dbeb
FCE
7233 dwfl_module_relocate_address (dw.module, &addr);
7234 // XXX: relocation basis?
909ab234
JS
7235
7236 // Dyninst needs the *file*-based offset for semaphores,
7237 // so subtract the difference in load addresses between .text and .probes
7238 if (dw.sess.runtime_usermode_p())
7239 addr -= semaphore_load_offset;
7240
a794dbeb
FCE
7241 for (unsigned i = start; i < results.size(); ++i)
7242 results[i]->sdt_semaphore_addr = addr;
7243 if (sess.verbose > 2)
b530b5b3 7244 clog << _(", found at 0x") << hex << addr << dec << endl;
a794dbeb
FCE
7245 return;
7246 }
7247 else
7248 if (sess.verbose > 2)
b530b5b3 7249 clog << _(", not found") << endl;
a794dbeb 7250 }
6846cfc8
SC
7251}
7252
7253
edce5b67
JS
7254void
7255sdt_query::convert_probe (probe *base)
7256{
7257 block *b = new block;
7258 b->tok = base->body->tok;
7259
edce5b67
JS
7260 // Generate: if (arg1 != mark("label")) next;
7261 functioncall *fc = new functioncall;
bbafcb1e 7262 fc->function = "ulong_arg";
edce5b67 7263 fc->tok = b->tok;
bbafcb1e 7264 literal_number* num = new literal_number(1);
edce5b67
JS
7265 num->tok = b->tok;
7266 fc->args.push_back(num);
7267
7268 functioncall *fcus = new functioncall;
7269 fcus->function = "user_string";
7270 fcus->type = pe_string;
7271 fcus->tok = b->tok;
7272 fcus->args.push_back(fc);
7273
7274 if_statement *is = new if_statement;
7275 is->thenblock = new next_statement;
7276 is->elseblock = NULL;
7277 is->tok = b->tok;
63ea4244 7278 is->thenblock->tok = b->tok;
edce5b67
JS
7279 comparison *be = new comparison;
7280 be->op = "!=";
7281 be->tok = b->tok;
7282 be->left = fcus;
7283 be->right = new literal_string(probe_name);
63ea4244 7284 be->right->tok = b->tok;
edce5b67
JS
7285 is->condition = be;
7286 b->statements.push_back(is);
7287
7288 // Now replace the body
7289 b->statements.push_back(base->body);
7290 base->body = b;
7291}
7292
7293
c72aa911
JS
7294probe*
7295sdt_query::convert_location ()
edce5b67 7296{
684b8718
JL
7297 string module = dw.module_name;
7298 if (has_process)
7299 module = path_remove_sysroot(sess, module);
7300
c72aa911 7301 probe_point* specific_loc = new probe_point(*base_loc);
684b8718
JL
7302 specific_loc->well_formed = true;
7303
662539d9 7304 vector<probe_point::component*> derived_comps;
edce5b67 7305
662539d9
JS
7306 vector<probe_point::component*>::iterator it;
7307 for (it = specific_loc->components.begin();
7308 it != specific_loc->components.end(); ++it)
7309 if ((*it)->functor == TOK_PROCESS)
7310 {
684b8718
JL
7311 // replace the possibly incomplete path to process
7312 *it = new probe_point::component(TOK_PROCESS,
7313 new literal_string(has_library ? path : module));
7314
1cc41cd6
DS
7315 // copy the process name
7316 derived_comps.push_back(*it);
662539d9
JS
7317 }
7318 else if ((*it)->functor == TOK_LIBRARY)
7319 {
1cc41cd6
DS
7320 // copy the library name for process probes
7321 derived_comps.push_back(*it);
662539d9
JS
7322 }
7323 else if ((*it)->functor == TOK_PROVIDER)
7324 {
7325 // replace the possibly wildcarded arg with the specific provider name
7326 *it = new probe_point::component(TOK_PROVIDER,
7327 new literal_string(provider_name));
7328 }
7329 else if ((*it)->functor == TOK_MARK)
c72aa911
JS
7330 {
7331 // replace the possibly wildcarded arg with the specific marker name
662539d9
JS
7332 *it = new probe_point::component(TOK_MARK,
7333 new literal_string(probe_name));
a794dbeb 7334
aff5d390
SC
7335 if (sess.verbose > 3)
7336 switch (probe_type)
7337 {
7338 case uprobe1_type:
b530b5b3 7339 clog << _("probe_type == uprobe1, use statement addr: 0x")
aff5d390
SC
7340 << hex << pc << dec << endl;
7341 break;
7342 case uprobe2_type:
b530b5b3 7343 clog << _("probe_type == uprobe2, use statement addr: 0x")
aff5d390
SC
7344 << hex << pc << dec << endl;
7345 break;
40fe32e0 7346 case uprobe3_type:
b530b5b3 7347 clog << _("probe_type == uprobe3, use statement addr: 0x")
40fe32e0
SC
7348 << hex << pc << dec << endl;
7349 break;
aff5d390 7350 default:
b530b5b3
LB
7351 clog << _F("probe_type == use_uprobe_no_dwarf, use label name: _stapprobe1_%s",
7352 pp_mark.c_str()) << endl;
aff5d390
SC
7353 }
7354
c72aa911
JS
7355 switch (probe_type)
7356 {
aff5d390
SC
7357 case uprobe1_type:
7358 case uprobe2_type:
40fe32e0 7359 case uprobe3_type:
c72aa911 7360 // process("executable").statement(probe_arg)
662539d9
JS
7361 derived_comps.push_back
7362 (new probe_point::component(TOK_STATEMENT,
7363 new literal_number(pc, true)));
c72aa911
JS
7364 break;
7365
a794dbeb 7366 default: // deprecated
c72aa911 7367 // process("executable").function("*").label("_stapprobe1_MARK_NAME")
662539d9
JS
7368 derived_comps.push_back
7369 (new probe_point::component(TOK_FUNCTION,
7370 new literal_string("*")));
7371 derived_comps.push_back
c72aa911 7372 (new probe_point::component(TOK_LABEL,
a794dbeb 7373 new literal_string("_stapprobe1_" + pp_mark)));
c72aa911
JS
7374 break;
7375 }
7376 }
edce5b67 7377
662539d9
JS
7378 probe_point* derived_loc = new probe_point(*specific_loc);
7379 derived_loc->components = derived_comps;
8159bf55 7380 return new probe (new probe (base_probe, specific_loc), derived_loc);
edce5b67
JS
7381}
7382
7383
51d6bda3
SC
7384void
7385sdt_query::query_library (const char *library)
7386{
614f0fcf
JL
7387 visited_libraries.insert(library);
7388 if (query_one_library (library, dw, user_lib, base_probe, base_loc, results))
7389 resolved_library = true;
51d6bda3
SC
7390}
7391
e90006d7
JL
7392string
7393suggest_marks(systemtap_session& sess,
7394 const set<string>& modules,
7395 const string& mark,
7396 const string& provider)
7397{
7398 if (mark.empty() || modules.empty() || sess.module_cache == NULL)
7399 return "";
7400
7401 set<string> marks;
7402 const map<string, module_info*> &cache = sess.module_cache->cache;
7403 bool dash_suggestions = (mark.find("-") != string::npos);
7404
7405 for (set<string>::iterator itmod = modules.begin();
7406 itmod != modules.end(); ++itmod)
7407 {
7408 map<string, module_info*>::const_iterator itcache;
7409 if ((itcache = cache.find(*itmod)) != cache.end())
7410 {
7411 set<pair<string,string> >::const_iterator itmarks;
7412 for (itmarks = itcache->second->marks.begin();
7413 itmarks != itcache->second->marks.end(); ++itmarks)
7414 {
7415 if (provider.empty()
7416 // simulating dw.function_name_matches_pattern()
7417 || (fnmatch(provider.c_str(), itmarks->first.c_str(), 0) == 0))
7418 {
7419 string marksug = itmarks->second;
7420 if (dash_suggestions)
7421 {
7422 size_t pos = 0;
7423 while (1) // there may be more than one
7424 {
7425 size_t i = marksug.find("__", pos);
7426 if (i == string::npos) break;
7427 marksug.replace (i, 2, "-");
7428 pos = i+1; // resume searching after the inserted -
7429 }
7430 }
7431 marks.insert(marksug);
7432 }
7433 }
7434 }
7435 }
7436
7437 if (sess.verbose > 2)
7438 {
7439 clog << "suggesting " << marks.size() << " marks "
7440 << "from modules:" << endl;
7441 for (set<string>::iterator itmod = modules.begin();
7442 itmod != modules.end(); ++itmod)
7443 clog << *itmod << endl;
7444 }
7445
7446 if (marks.empty())
7447 return "";
7448
7449 return levenshtein_suggest(mark, marks, 5); // print top 5 marks only
7450}
7451
40a70f52
JL
7452string
7453suggest_plt_functions(systemtap_session& sess,
7454 const set<string>& modules,
7455 const string& func)
7456{
7457 if (func.empty() || modules.empty() || sess.module_cache == NULL)
7458 return "";
7459
7460 set<string> funcs;
7461 const map<string, module_info*> &cache = sess.module_cache->cache;
7462
7463 for (set<string>::iterator itmod = modules.begin();
7464 itmod != modules.end(); ++itmod)
7465 {
7466 map<string, module_info*>::const_iterator itcache;
7467 if ((itcache = cache.find(*itmod)) != cache.end())
7468 funcs.insert(itcache->second->plt_funcs.begin(),
7469 itcache->second->plt_funcs.end());
7470 }
7471
7472 if (sess.verbose > 2)
d82dd71f
JL
7473 {
7474 clog << "suggesting " << funcs.size() << " plt functions "
7475 << "from modules:" << endl;
7476 for (set<string>::iterator itmod = modules.begin();
7477 itmod != modules.end(); ++itmod)
7478 clog << *itmod << endl;
7479 }
40a70f52
JL
7480
7481 if (funcs.empty())
7482 return "";
7483
7484 return levenshtein_suggest(func, funcs, 5); // print top 5 funcs only
7485}
51d6bda3 7486
0508c45f
JL
7487string
7488suggest_dwarf_functions(systemtap_session& sess,
7489 const set<string>& modules,
7490 string func)
44ffe90c
JL
7491{
7492 // Trim any @ component
7493 size_t pos = func.find('@');
7494 if (pos != string::npos)
7495 func.erase(pos);
7496
0508c45f 7497 if (func.empty() || modules.empty() || sess.module_cache == NULL)
44ffe90c
JL
7498 return "";
7499
7500 // We must first aggregate all the functions from the cache
7501 set<string> funcs;
7502 const map<string, module_info*> &cache = sess.module_cache->cache;
7503
0508c45f
JL
7504 for (set<string>::iterator itmod = modules.begin();
7505 itmod != modules.end(); ++itmod)
44ffe90c 7506 {
6e6d9a3b
JL
7507 module_info *module;
7508
7509 // retrieve module_info from cache
44ffe90c
JL
7510 map<string, module_info*>::const_iterator itcache;
7511 if ((itcache = cache.find(*itmod)) != cache.end())
6e6d9a3b
JL
7512 module = itcache->second;
7513 else // module not found
7514 continue;
7515
7516 // add inlines
7517 funcs.insert(module->inlined_funcs.begin(),
7518 module->inlined_funcs.end());
7519
7520 // add all function symbols in cache
7521 if (module->symtab_status != info_present || module->sym_table == NULL)
7522 continue;
7523 map<string, func_info*>& modfuncs = module->sym_table->map_by_name;
7524 for (map<string, func_info*>::const_iterator itfuncs = modfuncs.begin();
7525 itfuncs != modfuncs.end(); ++itfuncs)
7526 funcs.insert(itfuncs->first);
44ffe90c
JL
7527 }
7528
7529 if (sess.verbose > 2)
d82dd71f
JL
7530 {
7531 clog << "suggesting " << funcs.size() << " dwarf functions "
7532 << "from modules:" << endl;
7533 for (set<string>::iterator itmod = modules.begin();
7534 itmod != modules.end(); ++itmod)
7535 clog << *itmod << endl;
7536 }
44ffe90c
JL
7537
7538 if (funcs.empty())
7539 return "";
7540
593f09eb 7541 return levenshtein_suggest(func, funcs, 5); // print top 5 funcs only
44ffe90c
JL
7542}
7543
20c6c071 7544void
5227f1ea 7545dwarf_builder::build(systemtap_session & sess,
7a053d3b 7546 probe * base,
20c6c071 7547 probe_point * location,
86bf665e 7548 literal_map_t const & parameters,
20c6c071
GH
7549 vector<derived_probe *> & finished_results)
7550{
b20febf3
FCE
7551 // NB: the kernel/user dwlfpp objects are long-lived.
7552 // XXX: but they should be per-session, as this builder object
7553 // may be reused if we try to cross-instrument multiple targets.
84048984 7554
7a24d422 7555 dwflpp* dw = 0;
6d5d594e 7556 literal_map_t filled_parameters = parameters;
7a24d422 7557
7a24d422 7558 string module_name;
5c6f9e92 7559 int64_t proc_pid;
ae2552da
FCE
7560 if (has_null_param (parameters, TOK_KERNEL))
7561 {
7562 dw = get_kern_dw(sess, "kernel");
7563 }
7564 else if (get_param (parameters, TOK_MODULE, module_name))
b8da0ad1 7565 {
c523a015
LB
7566 size_t dash_pos = 0;
7567 while((dash_pos=module_name.find('-'))!=string::npos)
7568 module_name.replace(int(dash_pos),1,"_");
7569 filled_parameters[TOK_MODULE] = new literal_string(module_name);
37001baa
FCE
7570 // NB: glob patterns get expanded later, during the offline
7571 // elfutils module listing.
ae2552da 7572 dw = get_kern_dw(sess, module_name);
b8da0ad1 7573 }
5c6f9e92 7574 else if (has_param(filled_parameters, TOK_PROCESS))
84c84ac4 7575 {
0ce4c32f
FCE
7576 // NB: module_name is not yet set!
7577
6d5d594e
LB
7578 if(has_null_param(filled_parameters, TOK_PROCESS))
7579 {
fc18e6c4
JL
7580 string file;
7581 try
7582 {
7583 file = sess.cmd_file();
7584 }
7585 catch (const semantic_error& e)
7586 {
6d9c4e46 7587 if(sess.target_pid)
1b6d5734 7588 throw SEMANTIC_ERROR(_("invalid -x pid for unspecified process"
6d9c4e46
AJ
7589 " probe [man stapprobes]"), NULL, NULL, &e);
7590 else
7591 throw SEMANTIC_ERROR(_("invalid -c command for unspecified process"
fc18e6c4
JL
7592 " probe [man stapprobes]"), NULL, NULL, &e);
7593 }
48cd804a 7594 if(file.empty())
fc18e6c4 7595 throw SEMANTIC_ERROR(_("unspecified process probe is invalid without"
6d9c4e46 7596 " a -c COMMAND or -x PID [man stapprobes]"));
48cd804a 7597 module_name = sess.sysroot + file;
6d5d594e 7598 filled_parameters[TOK_PROCESS] = new literal_string(module_name);// this needs to be used in place of the blank map
0ce4c32f 7599 // in the case of TOK_MARK we need to modify locations as well // XXX why?
6d5d594e 7600 if(location->components[0]->functor==TOK_PROCESS &&
0ce4c32f 7601 location->components[0]->arg == 0)
6d5d594e 7602 location->components[0]->arg = new literal_string(module_name);
5c6f9e92
AJ
7603 }
7604
0ce4c32f
FCE
7605 // NB: must specifically handle the classical ("string") form here, to make sure
7606 // we get the module_name out.
7607 else if (get_param (parameters, TOK_PROCESS, module_name))
7608 {
7609 module_name = sess.sysroot + module_name;
7610 filled_parameters[TOK_PROCESS] = new literal_string(module_name);
7611 }
7612
5c6f9e92
AJ
7613 else if (get_param (parameters, TOK_PROCESS, proc_pid))
7614 {
6c484277
AJ
7615 // check that the pid given corresponds to a running process
7616 if (proc_pid < 1 || kill(proc_pid, 0) == -1)
7617 switch (errno) // ignore EINVAL: invalid signal
7618 {
7619 case ESRCH:
7620 throw SEMANTIC_ERROR(_("pid given does not correspond to a running process"));
7621 case EPERM:
7622 throw SEMANTIC_ERROR(_("invalid permissions for signalling given pid"));
7623 default:
7624 throw SEMANTIC_ERROR(_("invalid pid"));
7625 }
7626
5c6f9e92
AJ
7627 string pid_path = string("/proc/") + lex_cast(proc_pid) + "/exe";
7628 module_name = sess.sysroot + pid_path;
37635b45 7629
0ce4c32f 7630 // in the case of TOK_MARK we need to modify locations as well // XXX why?
5c6f9e92 7631 if(location->components[0]->functor==TOK_PROCESS &&
0ce4c32f 7632 location->components[0]->arg == 0)
5c6f9e92 7633 location->components[0]->arg = new literal_string(module_name);
0ce4c32f
FCE
7634 // XXX: the above probably interferes with passing proc_pid to the new
7635 // uprobe_derived_probe p->pid, so the task-finder can associate this
7636 // probe with only the given PID.
b78a0fbb 7637 }
5750ecc6 7638
37001baa
FCE
7639 // PR6456 process("/bin/*") glob handling
7640 if (contains_glob_chars (module_name))
7641 {
7642 // Expand glob via rewriting the probe-point process("....")
7643 // parameter, asserted to be the first one.
7644
7645 assert (location->components.size() > 0);
7646 assert (location->components[0]->functor == TOK_PROCESS);
7647 assert (location->components[0]->arg);
7648 literal_string* lit = dynamic_cast<literal_string*>(location->components[0]->arg);
7649 assert (lit);
7650
7651 // Evaluate glob here, and call derive_probes recursively with each match.
7652 glob_t the_blob;
88f8e2c0 7653 set<string> dupes;
37001baa 7654 int rc = glob (module_name.c_str(), 0, NULL, & the_blob);
b530b5b3 7655 if (rc)
dc09353a 7656 throw SEMANTIC_ERROR (_F("glob %s error (%s)", module_name.c_str(), lex_cast(rc).c_str() ));
44ffe90c 7657 unsigned results_pre = finished_results.size();
37001baa
FCE
7658 for (unsigned i = 0; i < the_blob.gl_pathc; ++i)
7659 {
e19ebcf7 7660 assert_no_interrupts();
37001baa
FCE
7661
7662 const char* globbed = the_blob.gl_pathv[i];
7663 struct stat st;
7664
7665 if (access (globbed, X_OK) == 0
7666 && stat (globbed, &st) == 0
7667 && S_ISREG (st.st_mode)) // see find_executable()
7668 {
7977a734
FCE
7669 // Need to call canonicalize here, in order to path-expand
7670 // patterns like process("stap*"). Otherwise it may go through
7671 // to the next round of expansion as ("stap"), leading to a $PATH
7672 // search that's not consistent with the glob search already done.
5bca76a8
JS
7673 string canononicalized = resolve_path (globbed);
7674 globbed = canononicalized.c_str();
7977a734 7675
88f8e2c0
JS
7676 // The canonical names can result in duplication, for example
7677 // having followed symlinks that are common with shared
7678 // libraries. Filter those out.
7679 if (!dupes.insert(canononicalized).second)
7680 continue;
7681
37001baa
FCE
7682 // synthesize a new probe_point, with the glob-expanded string
7683 probe_point *pp = new probe_point (*location);
ef0943df
JL
7684 pp->from_glob = true;
7685
5750ecc6
FCE
7686 // PR13338: quote results to prevent recursion
7687 string eglobbed = escape_glob_chars (globbed);
7688
7689 if (sess.verbose > 1)
7690 clog << _F("Expanded process(\"%s\") to process(\"%s\")",
7691 module_name.c_str(), eglobbed.c_str()) << endl;
05fb3e0c 7692 string eglobbed_tgt = path_remove_sysroot(sess, eglobbed);
5750ecc6 7693
37001baa 7694 probe_point::component* ppc = new probe_point::component (TOK_PROCESS,
05fb3e0c 7695 new literal_string (eglobbed_tgt));
37001baa
FCE
7696 ppc->tok = location->components[0]->tok; // overwrite [0] slot, pattern matched above
7697 pp->components[0] = ppc;
7698
d885563b 7699 probe* new_probe = new probe (base, pp);
7977a734
FCE
7700
7701 // We override "optional = true" here, as if the
7702 // wildcarded probe point was given a "?" suffix.
7703
7704 // This is because wildcard probes will be expected
7705 // by users to apply only to some subset of the
7706 // matching binaries, in the sense of "any", rather
7707 // than "all", sort of similarly how
7708 // module("*").function("...") patterns work.
7709
7710 derive_probes (sess, new_probe, finished_results,
7711 true /* NB: not location->optional */ );
37001baa
FCE
7712 }
7713 }
7714
7715 globfree (& the_blob);
44ffe90c
JL
7716
7717 unsigned results_post = finished_results.size();
7718
e90006d7 7719 // Did we fail to find a function/plt/mark by name? Let's suggest
44ffe90c
JL
7720 // something!
7721 string func;
7722 if (results_pre == results_post
7723 && get_param(filled_parameters, TOK_FUNCTION, func)
7724 && !func.empty())
7725 {
0508c45f 7726 string sugs = suggest_dwarf_functions(sess, modules_seen, func);
44ffe90c
JL
7727 modules_seen.clear();
7728 if (!sugs.empty())
dc09353a 7729 throw SEMANTIC_ERROR (_NF("no match (similar function: %s)",
44ffe90c
JL
7730 "no match (similar functions: %s)",
7731 sugs.find(',') == string::npos,
7732 sugs.c_str()));
7733 }
40a70f52
JL
7734 else if (results_pre == results_post
7735 && get_param(filled_parameters, TOK_PLT, func)
7736 && !func.empty())
7737 {
7738 string sugs = suggest_plt_functions(sess, modules_seen, func);
7739 modules_seen.clear();
7740 if (!sugs.empty())
7741 throw SEMANTIC_ERROR (_NF("no match (similar function: %s)",
7742 "no match (similar functions: %s)",
7743 sugs.find(',') == string::npos,
7744 sugs.c_str()));
7745 }
e90006d7
JL
7746 else if (results_pre == results_post
7747 && get_param(filled_parameters, TOK_MARK, func)
7748 && !func.empty())
7749 {
7750 string provider;
7751 get_param(filled_parameters, TOK_PROVIDER, provider);
7752
7753 string sugs = suggest_marks(sess, modules_seen, func, provider);
7754 modules_seen.clear();
7755 if (!sugs.empty())
7756 throw SEMANTIC_ERROR (_NF("no match (similar mark: %s)",
7757 "no match (similar marks: %s)",
7758 sugs.find(',') == string::npos,
7759 sugs.c_str()));
7760 }
44ffe90c 7761
37001baa
FCE
7762 return; // avoid falling through
7763 }
7764
5750ecc6
FCE
7765 // PR13338: unquote glob results
7766 module_name = unescape_glob_chars (module_name);
05fb3e0c 7767 user_path = find_executable (module_name, "", sess.sysenv); // canonicalize it
5fa0d811
JL
7768 if (!is_fully_resolved(user_path, "", sess.sysenv))
7769 throw SEMANTIC_ERROR(_F("cannot find executable '%s'",
7770 user_path.c_str()));
d1bcbe71
RH
7771
7772 // if the executable starts with "#!", we look for the interpreter of the script
7773 {
7774 ifstream script_file (user_path.c_str () );
7775
7776 if (script_file.good ())
7777 {
7778 string line;
7779
7780 getline (script_file, line);
7781
7782 if (line.compare (0, 2, "#!") == 0)
7783 {
7784 string path_head = line.substr(2);
7785
7786 // remove white spaces at the beginning of the string
7787 size_t p2 = path_head.find_first_not_of(" \t");
7788
7789 if (p2 != string::npos)
7790 {
7791 string path = path_head.substr(p2);
7792
7793 // remove white spaces at the end of the string
7794 p2 = path.find_last_not_of(" \t\n");
7795 if (string::npos != p2)
7796 path.erase(p2+1);
7797
8e13c1a1
RH
7798 // handle "#!/usr/bin/env" redirect
7799 size_t offset = 0;
7800 if (path.compare(0, sizeof("/bin/env")-1, "/bin/env") == 0)
7801 {
7802 offset = sizeof("/bin/env")-1;
7803 }
7804 else if (path.compare(0, sizeof("/usr/bin/env")-1, "/usr/bin/env") == 0)
7805 {
7806 offset = sizeof("/usr/bin/env")-1;
7807 }
7808
7809 if (offset != 0)
7810 {
7811 size_t p3 = path.find_first_not_of(" \t", offset);
7812
7813 if (p3 != string::npos)
7814 {
7815 string env_path = path.substr(p3);
05fb3e0c
WF
7816 user_path = find_executable (env_path, sess.sysroot,
7817 sess.sysenv);
8e13c1a1
RH
7818 }
7819 }
7820 else
7821 {
05fb3e0c 7822 user_path = find_executable (path, sess.sysroot, sess.sysenv);
8e13c1a1 7823 }
d1bcbe71
RH
7824
7825 struct stat st;
7826
7827 if (access (user_path.c_str(), X_OK) == 0
7828 && stat (user_path.c_str(), &st) == 0
7829 && S_ISREG (st.st_mode)) // see find_executable()
7830 {
7831 if (sess.verbose > 1)
b530b5b3
LB
7832 clog << _F("Expanded process(\"%s\") to process(\"%s\")",
7833 module_name.c_str(), user_path.c_str()) << endl;
d1bcbe71
RH
7834
7835 assert (location->components.size() > 0);
7836 assert (location->components[0]->functor == TOK_PROCESS);
7837 assert (location->components[0]->arg);
7838 literal_string* lit = dynamic_cast<literal_string*>(location->components[0]->arg);
7839 assert (lit);
7840
7841 // synthesize a new probe_point, with the expanded string
7842 probe_point *pp = new probe_point (*location);
05fb3e0c 7843 string user_path_tgt = path_remove_sysroot(sess, user_path);
d1bcbe71 7844 probe_point::component* ppc = new probe_point::component (TOK_PROCESS,
05fb3e0c 7845 new literal_string (user_path_tgt.c_str()));
d1bcbe71
RH
7846 ppc->tok = location->components[0]->tok; // overwrite [0] slot, pattern matched above
7847 pp->components[0] = ppc;
7848
d885563b 7849 probe* new_probe = new probe (base, pp);
d1bcbe71
RH
7850
7851 derive_probes (sess, new_probe, finished_results);
7852
7853 script_file.close();
7854 return;
7855 }
7856 }
7857 }
7858 }
7859 script_file.close();
7860 }
7861
dbe9d133
JL
7862 // If this is a library probe, then target the library module instead. We
7863 // do this only if the library path is already fully resolved (such as
7864 // what query_one_library() would have done for us). Otherwise, we resort
7865 // to iterate_over_libraries.
7866 if (get_param (parameters, TOK_LIBRARY, user_lib) && !user_lib.empty()
7867 && is_fully_resolved(user_lib, sess.sysroot, sess.sysenv, "LD_LIBRARY_PATH"))
7868 module_name = user_lib;
63b4fd14 7869 else
dbe9d133 7870 module_name = user_path; // canonicalize it
d0a7f5a9 7871
3667d615 7872 // uretprobes aren't available everywhere
536f1261 7873 if (has_null_param(parameters, TOK_RETURN) && !sess.runtime_usermode_p())
2b69faaf 7874 {
3667d615
JS
7875 if (kernel_supports_inode_uprobes(sess) &&
7876 !kernel_supports_inode_uretprobes(sess))
dc09353a 7877 throw SEMANTIC_ERROR
3ffeaf3c 7878 (_("process return probes not available [man error::inode-uprobes]"));
2b69faaf 7879 }
3667d615 7880
f4000852
MW
7881 // There is a similar check in pass 4 (buildrun), but it is
7882 // needed here too to make sure alternatives for optional
7883 // (? or !) process probes are disposed and/or alternatives
7884 // are selected.
3667d615
JS
7885 if (!sess.runtime_usermode_p())
7886 check_process_probe_kernel_support(sess);
e34d5d13 7887
7a24d422
FCE
7888 // user-space target; we use one dwflpp instance per module name
7889 // (= program or shared library)
707bf35e 7890 dw = get_user_dw(sess, module_name);
c8959a29 7891 }
20c6c071 7892
1acfc030
JS
7893 assert(dw);
7894
e90006d7
JL
7895 unsigned results_pre = finished_results.size();
7896
5896cd05 7897 if (sess.verbose > 3)
b530b5b3 7898 clog << _F("dwarf_builder::build for %s", module_name.c_str()) << endl;
5896cd05 7899
a794dbeb
FCE
7900 string dummy_mark_name; // NB: PR10245: dummy value, need not substitute - => __
7901 if (get_param(parameters, TOK_MARK, dummy_mark_name))
f28a8c28 7902 {
51d6bda3 7903 sdt_query sdtq(base, location, *dw, filled_parameters, finished_results, user_lib);
06de3a04 7904 dw->iterate_over_modules<base_query>(&query_module, &sdtq);
e90006d7
JL
7905
7906 // We need to update modules_seen with the modules we've visited
7907 modules_seen.insert(sdtq.visited_modules.begin(),
7908 sdtq.visited_modules.end());
7909
614f0fcf
JL
7910 string lib;
7911 if (results_pre == finished_results.size() && !sdtq.resolved_library
7912 && get_param(filled_parameters, TOK_LIBRARY, lib)
7913 && !lib.empty() && !sdtq.visited_libraries.empty())
7914 {
7915 // The library didn't fit any DT_NEEDED libraries. As a last effort,
7916 // let's try to look for the library directly.
7917 string resolved_lib = find_executable(lib, sess.sysroot, sess.sysenv,
7918 "LD_LIBRARY_PATH");
7919 if (resolved_lib.find('/') != string::npos)
7920 {
7921 probe *new_base = build_library_probe(*dw, resolved_lib,
7922 base, location);
7923 derive_probes(sess, new_base, finished_results);
7924 sess.print_warning(_F("'%s' is not a needed library of '%s'. "
7925 "Specify the full path to squelch this warning.",
7926 resolved_lib.c_str(), dw->module_name.c_str()));
7927 return;
7928 }
f23e4abe
JL
7929
7930 // Otherwise, let's suggest from the DT_NEEDED libraries
7931 string sugs = levenshtein_suggest(lib, sdtq.visited_libraries, 5);
7932 if (!sugs.empty())
7933 throw SEMANTIC_ERROR (_NF("no match (similar library: %s)",
7934 "no match (similar libraries: %s)",
7935 sugs.find(',') == string::npos,
7936 sugs.c_str()));
614f0fcf
JL
7937 }
7938
e90006d7
JL
7939 // Did we fail to find a mark?
7940 if (results_pre == finished_results.size() && !location->from_glob)
7941 {
7942 string provider;
7943 get_param(filled_parameters, TOK_PROVIDER, provider);
7944
7945 string sugs = suggest_marks(sess, modules_seen, dummy_mark_name, provider);
7946 modules_seen.clear();
7947 if (!sugs.empty())
7948 throw SEMANTIC_ERROR (_NF("no match (similar mark: %s)",
7949 "no match (similar marks: %s)",
7950 sugs.find(',') == string::npos,
7951 sugs.c_str()));
7952 }
7953
edce5b67 7954 return;
7a05f484 7955 }
20c6c071 7956
6d5d594e 7957 dwarf_query q(base, location, *dw, filled_parameters, finished_results, user_path, user_lib);
7a24d422
FCE
7958
7959 // XXX: kernel.statement.absolute is a special case that requires no
7960 // dwfl processing. This code should be in a separate builder.
7a24d422 7961 if (q.has_kernel && q.has_absolute)
37ebca01 7962 {
4baf0e53 7963 // assert guru mode for absolute probes
37ebca01
FCE
7964 if (! q.base_probe->privileged)
7965 {
dc09353a 7966 throw SEMANTIC_ERROR (_("absolute statement probe in unprivileged script; need stap -g"),
edce5b67 7967 q.base_probe->tok);
37ebca01
FCE
7968 }
7969
7970 // For kernel.statement(NUM).absolute probe points, we bypass
7971 // all the debuginfo stuff: We just wire up a
7972 // dwarf_derived_probe right here and now.
4baf0e53 7973 dwarf_derived_probe* p =
b8da0ad1
FCE
7974 new dwarf_derived_probe ("", "", 0, "kernel", "",
7975 q.statement_num_val, q.statement_num_val,
7976 q, 0);
37ebca01 7977 finished_results.push_back (p);
1a0dbc5a 7978 sess.unwindsym_modules.insert ("kernel");
37ebca01
FCE
7979 return;
7980 }
7981
06de3a04 7982 dw->iterate_over_modules<base_query>(&query_module, &q);
8f14e444 7983
44ffe90c
JL
7984 // We need to update modules_seen with the modules we've visited
7985 modules_seen.insert(q.visited_modules.begin(),
7986 q.visited_modules.end());
8f14e444
FCE
7987
7988 // PR11553 special processing: .return probes requested, but
7989 // some inlined function instances matched.
7990 unsigned i_n_r = q.inlined_non_returnable.size();
7991 unsigned results_post = finished_results.size();
7992 if (i_n_r > 0)
7993 {
7994 if ((results_pre == results_post) && (! sess.suppress_warnings)) // no matches; issue warning
7995 {
7996 string quicklist;
7997 for (set<string>::iterator it = q.inlined_non_returnable.begin();
7998 it != q.inlined_non_returnable.end();
7999 it++)
8000 {
8001 quicklist += " " + (*it);
8002 if (quicklist.size() > 80) // heuristic, don't make an overlong report line
8003 {
8004 quicklist += " ...";
8005 break;
8006 }
8007 }
c57ea854 8008
52c2652f 8009 sess.print_warning (_NF("cannot probe .return of %u inlined function %s",
b530b5b3 8010 "cannot probe .return of %u inlined functions %s",
52c2652f 8011 quicklist.size(), i_n_r, quicklist.c_str()));
8f14e444
FCE
8012 // There will be also a "no matches" semantic error generated.
8013 }
8014 if (sess.verbose > 1)
52c2652f
NMA
8015 clog << _NF("skipped .return probe of %u inlined function",
8016 "skipped .return probe of %u inlined functions", i_n_r, i_n_r) << endl;
8f14e444
FCE
8017 if ((sess.verbose > 3) || (sess.verbose > 2 && results_pre == results_post)) // issue details with high verbosity
8018 {
8019 for (set<string>::iterator it = q.inlined_non_returnable.begin();
8020 it != q.inlined_non_returnable.end();
8021 it++)
8022 clog << (*it) << " ";
8023 clog << endl;
8024 }
8025 } // i_n_r > 0
44ffe90c 8026
614f0fcf
JL
8027 string lib;
8028 if (results_pre == results_post && !q.resolved_library
8029 && get_param(filled_parameters, TOK_LIBRARY, lib)
8030 && !lib.empty() && !q.visited_libraries.empty())
8031 {
8032 // The library didn't fit any DT_NEEDED libraries. As a last effort,
8033 // let's try to look for the library directly.
8034 string resolved_lib = find_executable(lib, sess.sysroot, sess.sysenv,
8035 "LD_LIBRARY_PATH");
8036 if (resolved_lib.find('/') != string::npos)
8037 {
8038 probe *new_base = build_library_probe(*dw, resolved_lib,
8039 base, location);
8040 derive_probes(sess, new_base, finished_results);
8041 sess.print_warning(_F("'%s' is not a needed library of '%s'. "
8042 "Specify the full path to squelch this warning.",
8043 resolved_lib.c_str(), dw->module_name.c_str()));
8044 return;
8045 }
f23e4abe
JL
8046
8047 // Otherwise, let's suggest from the DT_NEEDED libraries
8048 string sugs = levenshtein_suggest(lib, q.visited_libraries, 5);
8049 if (!sugs.empty())
8050 throw SEMANTIC_ERROR (_NF("no match (similar library: %s)",
8051 "no match (similar libraries: %s)",
8052 sugs.find(',') == string::npos,
8053 sugs.c_str()));
614f0fcf
JL
8054 }
8055
e90006d7 8056 // If we just failed to resolve a function/plt by name, we can suggest
ef0943df
JL
8057 // something. We only suggest things for probe points that were not
8058 // synthesized from a glob, i.e. only for 'real' probes. This is also
8059 // required because modules_seen needs to accumulate across recursive
8060 // calls for process(glob)[.library(glob)] probes.
44ffe90c 8061 string func;
ef0943df 8062 if (results_pre == results_post && !location->from_glob
44ffe90c
JL
8063 && get_param(filled_parameters, TOK_FUNCTION, func)
8064 && !func.empty())
8065 {
0508c45f 8066 string sugs = suggest_dwarf_functions(sess, modules_seen, func);
44ffe90c
JL
8067 modules_seen.clear();
8068 if (!sugs.empty())
dc09353a 8069 throw SEMANTIC_ERROR (_NF("no match (similar function: %s)",
44ffe90c
JL
8070 "no match (similar functions: %s)",
8071 sugs.find(',') == string::npos,
8072 sugs.c_str()));
8073 }
ef0943df 8074 else if (results_pre == results_post && !location->from_glob
40a70f52
JL
8075 && get_param(filled_parameters, TOK_PLT, func)
8076 && !func.empty())
8077 {
8078 string sugs = suggest_plt_functions(sess, modules_seen, func);
8079 modules_seen.clear();
8080 if (!sugs.empty())
8081 throw SEMANTIC_ERROR (_NF("no match (similar function: %s)",
8082 "no match (similar functions: %s)",
8083 sugs.find(',') == string::npos,
8084 sugs.c_str()));
8085 }
44ffe90c
JL
8086 else if (results_pre != results_post)
8087 // Something was derived so we won't need to suggest something
8088 modules_seen.clear();
5f0a03a6
JK
8089}
8090
8091symbol_table::~symbol_table()
8092{
c9efa5c9 8093 delete_map(map_by_addr);
5f0a03a6
JK
8094}
8095
8096void
2867a2a1 8097symbol_table::add_symbol(const char *name, bool weak, bool descriptor,
44ffe90c 8098 Dwarf_Addr addr, Dwarf_Addr* /*high_addr*/)
5f0a03a6 8099{
ab91b232
JK
8100#ifdef __powerpc__
8101 // Map ".sys_foo" to "sys_foo".
8102 if (name[0] == '.')
8103 name++;
8104#endif
5f0a03a6
JK
8105 func_info *fi = new func_info();
8106 fi->addr = addr;
8107 fi->name = name;
ab91b232 8108 fi->weak = weak;
2867a2a1 8109 fi->descriptor = descriptor;
5f0a03a6
JK
8110 map_by_name[fi->name] = fi;
8111 // TODO: Use a multimap in case there are multiple static
8112 // functions with the same name?
1c6b77e5 8113 map_by_addr.insert(make_pair(addr, fi));
5f0a03a6
JK
8114}
8115
46f7b6be 8116void
f98c6346 8117symbol_table::prepare_section_rejection(Dwfl_Module *mod __attribute__ ((unused)))
46f7b6be
JK
8118{
8119#ifdef __powerpc__
8120 /*
8121 * The .opd section contains function descriptors that can look
8122 * just like function entry points. For example, there's a function
8123 * descriptor called "do_exit" that links to the entry point ".do_exit".
8124 * Reject all symbols in .opd.
8125 */
8126 opd_section = SHN_UNDEF;
8127 Dwarf_Addr bias;
8128 Elf* elf = (dwarf_getelf (dwfl_module_getdwarf (mod, &bias))
8129 ?: dwfl_module_getelf (mod, &bias));
8130 Elf_Scn* scn = 0;
8131 size_t shstrndx;
8132
8133 if (!elf)
8134 return;
fcc30d6d 8135 if (elf_getshdrstrndx (elf, &shstrndx) != 0)
46f7b6be
JK
8136 return;
8137 while ((scn = elf_nextscn(elf, scn)) != NULL)
8138 {
8139 GElf_Shdr shdr_mem;
8140 GElf_Shdr *shdr = gelf_getshdr(scn, &shdr_mem);
8141 if (!shdr)
8142 continue;
8143 const char *name = elf_strptr(elf, shstrndx, shdr->sh_name);
8144 if (!strcmp(name, ".opd"))
8145 {
8146 opd_section = elf_ndxscn(scn);
8147 return;
8148 }
8149 }
8150#endif
8151}
8152
8153bool
8154symbol_table::reject_section(GElf_Word section)
8155{
8156 if (section == SHN_UNDEF)
8157 return true;
8158#ifdef __powerpc__
8159 if (section == opd_section)
8160 return true;
8161#endif
8162 return false;
8163}
8164
5f0a03a6
JK
8165enum info_status
8166symbol_table::get_from_elf()
8167{
8168 Dwarf_Addr high_addr = 0;
8169 Dwfl_Module *mod = mod_info->mod;
8170 int syments = dwfl_module_getsymtab(mod);
8171 assert(syments);
46f7b6be 8172 prepare_section_rejection(mod);
5f0a03a6
JK
8173 for (int i = 1; i < syments; ++i)
8174 {
8175 GElf_Sym sym;
ab91b232 8176 GElf_Word section;
cba67d8f
MW
8177 const char *name;
8178 GElf_Addr addr;
8179 bool reject;
8180
8181/* Note that dwfl_module_getsym does adjust the sym.st_value but doesn't
8182 try to resolve it to a function address. dwfl_module_getsym_info leaves
8183 the st_value in tact (no adjustment applied) and returns the fully
8184 resolved address separately. In that case we can simply reject the
8185 symbol if it is SHN_UNDEF and don't need to call reject_section which
8186 does extra checks to see whether the address fall in an architecture
8187 specific descriptor table (which will never be the case when using the
8188 new dwfl_module_getsym_info). dwfl_module_getsym will only provide us
8189 with the (adjusted) st_value of the symbol, which might point into a
8190 function descriptor table. So in that case we still have to call
8191 reject_section. */
8192#if _ELFUTILS_PREREQ (0, 158)
8193 name = dwfl_module_getsym_info (mod, i, &sym, &addr, &section,
8194 NULL, NULL);
8195 reject = section == SHN_UNDEF;
8196#else
8197 name = dwfl_module_getsym (mod, i, &sym, &section);
b23098cd 8198 addr = sym.st_value;
cba67d8f
MW
8199 reject = reject_section(section);
8200#endif
8201
2867a2a1 8202 if (name && GELF_ST_TYPE(sym.st_info) == STT_FUNC)
ab91b232 8203 add_symbol(name, (GELF_ST_BIND(sym.st_info) == STB_WEAK),
cba67d8f 8204 reject, addr, &high_addr);
2a4acb09
JL
8205 if (name && GELF_ST_TYPE(sym.st_info) == STT_OBJECT
8206 && GELF_ST_BIND(sym.st_info) == STB_GLOBAL)
8207 globals[name] = addr;
8208 if (name && GELF_ST_TYPE(sym.st_info) == STT_OBJECT
8209 && GELF_ST_BIND(sym.st_info) == STB_LOCAL)
8210 locals[name] = addr;
5f0a03a6
JK
8211 }
8212 return info_present;
8213}
8214
5f0a03a6
JK
8215func_info *
8216symbol_table::get_func_containing_address(Dwarf_Addr addr)
8217{
1c6b77e5
JS
8218 iterator_t iter = map_by_addr.upper_bound(addr);
8219 if (iter == map_by_addr.begin())
5f0a03a6 8220 return NULL;
2e67a43b 8221 else
1c6b77e5 8222 return (--iter)->second;
5f0a03a6
JK
8223}
8224
3d372d6b
SC
8225func_info *
8226symbol_table::get_first_func()
8227{
8228 iterator_t iter = map_by_addr.begin();
8229 return (iter)->second;
8230}
8231
5f0a03a6
JK
8232func_info *
8233symbol_table::lookup_symbol(const string& name)
8234{
8235 map<string, func_info*>::iterator i = map_by_name.find(name);
8236 if (i == map_by_name.end())
8237 return NULL;
8238 return i->second;
8239}
8240
8241Dwarf_Addr
8242symbol_table::lookup_symbol_address(const string& name)
8243{
8244 func_info *fi = lookup_symbol(name);
8245 if (fi)
8246 return fi->addr;
8247 return 0;
8248}
8249
ab91b232
JK
8250// This is the kernel symbol table. The kernel macro cond_syscall creates
8251// a weak symbol for each system call and maps it to sys_ni_syscall.
8252// For system calls not implemented elsewhere, this weak symbol shows up
8253// in the kernel symbol table. Following the precedent of dwarfful stap,
8254// we refuse to consider such symbols. Here we delete them from our
8255// symbol table.
8256// TODO: Consider generalizing this and/or making it part of blacklist
8257// processing.
8258void
8259symbol_table::purge_syscall_stubs()
8260{
8261 Dwarf_Addr stub_addr = lookup_symbol_address("sys_ni_syscall");
8262 if (stub_addr == 0)
8263 return;
1c6b77e5 8264 range_t purge_range = map_by_addr.equal_range(stub_addr);
2e67a43b
TM
8265 for (iterator_t iter = purge_range.first;
8266 iter != purge_range.second;
1c6b77e5 8267 )
ab91b232 8268 {
1c6b77e5 8269 func_info *fi = iter->second;
2e67a43b 8270 if (fi->weak && fi->name != "sys_ni_syscall")
ab91b232 8271 {
2e67a43b 8272 map_by_name.erase(fi->name);
1c6b77e5 8273 map_by_addr.erase(iter++);
2e67a43b 8274 delete fi;
2e67a43b 8275 }
1c6b77e5
JS
8276 else
8277 iter++;
ab91b232
JK
8278 }
8279}
8280
5f0a03a6 8281void
5f52fafe 8282module_info::get_symtab()
5f0a03a6 8283{
1c6b77e5
JS
8284 if (symtab_status != info_unknown)
8285 return;
8286
5f0a03a6
JK
8287 sym_table = new symbol_table(this);
8288 if (!elf_path.empty())
8289 {
5f0a03a6
JK
8290 symtab_status = sym_table->get_from_elf();
8291 }
8292 else
8293 {
8294 assert(name == TOK_KERNEL);
ab3ed72d
DS
8295 symtab_status = info_absent;
8296 cerr << _("Error: Cannot find vmlinux.") << endl;;
5f0a03a6
JK
8297 }
8298 if (symtab_status == info_absent)
8299 {
8300 delete sym_table;
8301 sym_table = NULL;
8302 return;
8303 }
8304
ab91b232
JK
8305 if (name == TOK_KERNEL)
8306 sym_table->purge_syscall_stubs();
5f0a03a6
JK
8307}
8308
1c6b77e5
JS
8309// update_symtab reconciles data between the elf symbol table and the dwarf
8310// function enumeration. It updates the symbol table entries with the dwarf
8311// die that describes the function, which also signals to query_module_symtab
8312// that a statement probe isn't needed. In return, it also adds aliases to the
8313// function table for names that share the same addr/die.
8314void
8315module_info::update_symtab(cu_function_cache_t *funcs)
8316{
8317 if (!sym_table)
8318 return;
8319
8320 cu_function_cache_t new_funcs;
8321
8322 for (cu_function_cache_t::iterator func = funcs->begin();
8323 func != funcs->end(); func++)
8324 {
8325 // optimization: inlines will never be in the symbol table
8326 if (dwarf_func_inline(&func->second) != 0)
6e6d9a3b
JL
8327 {
8328 inlined_funcs.insert(func->first);
8329 continue;
8330 }
1c6b77e5 8331
1ffb8bd1
JS
8332 // XXX We may want to make additional efforts to match mangled elf names
8333 // to dwarf too. MIPS_linkage_name can help, but that's sometimes
8334 // missing, so we may also need to try matching by address. See also the
8335 // notes about _Z in dwflpp::iterate_over_functions().
8336
1c6b77e5
JS
8337 func_info *fi = sym_table->lookup_symbol(func->first);
8338 if (!fi)
8339 continue;
8340
8341 // iterate over all functions at the same address
8342 symbol_table::range_t er = sym_table->map_by_addr.equal_range(fi->addr);
8343 for (symbol_table::iterator_t it = er.first; it != er.second; ++it)
8344 {
8345 // update this function with the dwarf die
8346 it->second->die = func->second;
8347
8348 // if this function is a new alias, then
8349 // save it to merge into the function cache
8350 if (it->second != fi)
b7478964 8351 new_funcs.insert(make_pair(it->second->name, it->second->die));
1c6b77e5
JS
8352 }
8353 }
8354
8355 // add all discovered aliases back into the function cache
8356 // NB: this won't replace any names that dwarf may have already found
8357 funcs->insert(new_funcs.begin(), new_funcs.end());
8358}
8359
5f0a03a6
JK
8360module_info::~module_info()
8361{
8362 if (sym_table)
8363 delete sym_table;
b55bc428
FCE
8364}
8365
935447c8 8366// ------------------------------------------------------------------------
888af770 8367// user-space probes
935447c8
DS
8368// ------------------------------------------------------------------------
8369
935447c8 8370
888af770 8371struct uprobe_derived_probe_group: public generic_dpg<uprobe_derived_probe>
935447c8 8372{
89ba3085
FCE
8373private:
8374 string make_pbm_key (uprobe_derived_probe* p) {
cfcab6c7 8375 return p->path + "|" + p->module + "|" + p->section + "|" + lex_cast(p->pid);
89ba3085
FCE
8376 }
8377
cfcab6c7
JS
8378 void emit_module_maxuprobes (systemtap_session& s);
8379
2b69faaf
JS
8380 // Using our own utrace-based uprobes
8381 void emit_module_utrace_decls (systemtap_session& s);
8382 void emit_module_utrace_init (systemtap_session& s);
8383 void emit_module_utrace_exit (systemtap_session& s);
8384
8385 // Using the upstream inode-based uprobes
8386 void emit_module_inode_decls (systemtap_session& s);
8387 void emit_module_inode_init (systemtap_session& s);
8388 void emit_module_inode_exit (systemtap_session& s);
8389
3a894f7e
JS
8390 // Using the dyninst backend (via stapdyn)
8391 void emit_module_dyninst_decls (systemtap_session& s);
8392 void emit_module_dyninst_init (systemtap_session& s);
8393 void emit_module_dyninst_exit (systemtap_session& s);
8394
935447c8 8395public:
888af770 8396 void emit_module_decls (systemtap_session& s);
935447c8
DS
8397 void emit_module_init (systemtap_session& s);
8398 void emit_module_exit (systemtap_session& s);
8399};
8400
8401
888af770
FCE
8402void
8403uprobe_derived_probe::join_group (systemtap_session& s)
8404{
8405 if (! s.uprobe_derived_probes)
8406 s.uprobe_derived_probes = new uprobe_derived_probe_group ();
8407 s.uprobe_derived_probes->enroll (this);
f31a77f5
DS
8408 if (s.runtime_usermode_p())
8409 enable_dynprobes(s);
8410 else
4441e344 8411 enable_task_finder(s);
a96d1db0 8412
8a03658e 8413 // Ask buildrun.cxx to build extra module if needed, and
d3e959b0
DS
8414 // signal staprun to load that module. If we're using the builtin
8415 // inode-uprobes, we still need to know that it is required.
8a03658e 8416 s.need_uprobes = true;
a96d1db0
DN
8417}
8418
888af770 8419
c0f84e7b
SC
8420void
8421uprobe_derived_probe::getargs(std::list<std::string> &arg_set) const
8422{
8423 dwarf_derived_probe::getargs(arg_set);
8424 arg_set.insert(arg_set.end(), args.begin(), args.end());
8425}
8426
8427
8428void
8429uprobe_derived_probe::saveargs(int nargs)
8430{
8431 for (int i = 1; i <= nargs; i++)
8432 args.push_back("$arg" + lex_cast (i) + ":long");
8433}
8434
8435
2865d17a 8436void
42e38653 8437uprobe_derived_probe::emit_privilege_assertion (translator_output* o)
2865d17a
DB
8438{
8439 // These probes are allowed for unprivileged users, but only in the
8440 // context of processes which they own.
8441 emit_process_owner_assertion (o);
8442}
8443
8444
888af770 8445struct uprobe_builder: public derived_probe_builder
a96d1db0 8446{
888af770 8447 uprobe_builder() {}
2b69faaf 8448 virtual void build(systemtap_session & sess,
a96d1db0
DN
8449 probe * base,
8450 probe_point * location,
86bf665e 8451 literal_map_t const & parameters,
a96d1db0
DN
8452 vector<derived_probe *> & finished_results)
8453 {
888af770 8454 int64_t process, address;
a96d1db0 8455
2b69faaf 8456 if (kernel_supports_inode_uprobes(sess))
dc09353a 8457 throw SEMANTIC_ERROR (_("absolute process probes not available [man error::inode-uprobes]"));
2b69faaf 8458
888af770 8459 bool b1 = get_param (parameters, TOK_PROCESS, process);
ced347a9 8460 (void) b1;
888af770 8461 bool b2 = get_param (parameters, TOK_STATEMENT, address);
ced347a9 8462 (void) b2;
888af770
FCE
8463 bool rr = has_null_param (parameters, TOK_RETURN);
8464 assert (b1 && b2); // by pattern_root construction
a96d1db0 8465
0973d815 8466 finished_results.push_back(new uprobe_derived_probe(base, location, process, address, rr));
a96d1db0
DN
8467 }
8468};
8469
8470
8471void
cfcab6c7 8472uprobe_derived_probe_group::emit_module_maxuprobes (systemtap_session& s)
a96d1db0 8473{
43241c44
FCE
8474 // We'll probably need at least this many:
8475 unsigned minuprobes = probes.size();
8476 // .. but we don't want so many that .bss is inflated (PR10507):
8477 unsigned uprobesize = 64;
8478 unsigned maxuprobesmem = 10*1024*1024; // 10 MB
8479 unsigned maxuprobes = maxuprobesmem / uprobesize;
8480
aaf7ffe8
FCE
8481 // Let's choose a value on the geometric middle. This should end up
8482 // between minuprobes and maxuprobes. It's OK if this number turns
8483 // out to be < minuprobes or > maxuprobes. At worst, we get a
8484 // run-time error of one kind (too few: missed uprobe registrations)
8485 // or another (too many: vmalloc errors at module load time).
8486 unsigned default_maxuprobes = (unsigned)sqrt((double)minuprobes * (double)maxuprobes);
43241c44 8487
6d0f3f0c 8488 s.op->newline() << "#ifndef MAXUPROBES";
43241c44 8489 s.op->newline() << "#define MAXUPROBES " << default_maxuprobes;
6d0f3f0c 8490 s.op->newline() << "#endif";
cfcab6c7
JS
8491}
8492
8493
8494void
8495uprobe_derived_probe_group::emit_module_utrace_decls (systemtap_session& s)
8496{
8497 if (probes.empty()) return;
8498 s.op->newline() << "/* ---- utrace uprobes ---- */";
8499 // If uprobes isn't in the kernel, pull it in from the runtime.
8500
8501 s.op->newline() << "#if defined(CONFIG_UPROBES) || defined(CONFIG_UPROBES_MODULE)";
8502 s.op->newline() << "#include <linux/uprobes.h>";
8503 s.op->newline() << "#else";
2ba1736a 8504 s.op->newline() << "#include \"linux/uprobes/uprobes.h\"";
cfcab6c7
JS
8505 s.op->newline() << "#endif";
8506 s.op->newline() << "#ifndef UPROBES_API_VERSION";
8507 s.op->newline() << "#define UPROBES_API_VERSION 1";
8508 s.op->newline() << "#endif";
8509
8510 emit_module_maxuprobes (s);
a96d1db0 8511
cc52276b 8512 // Forward decls
2ba1736a 8513 s.op->newline() << "#include \"linux/uprobes-common.h\"";
cc52276b 8514
5e112f92
FCE
8515 // In .bss, the shared pool of uprobe/uretprobe structs. These are
8516 // too big to embed in the initialized .data stap_uprobe_spec array.
cc52276b
WC
8517 // XXX: consider a slab cache or somesuch for stap_uprobes
8518 s.op->newline() << "static struct stap_uprobe stap_uprobes [MAXUPROBES];";
6b378b7c 8519 s.op->newline() << "static DEFINE_MUTEX(stap_uprobes_lock);"; // protects against concurrent registration/unregistration
a96d1db0 8520
89ba3085
FCE
8521 s.op->assert_0_indent();
8522
89ba3085
FCE
8523 // Assign task-finder numbers as we build up the stap_uprobe_tf table.
8524 // This means we process probes[] in two passes.
8525 map <string,unsigned> module_index;
8526 unsigned module_index_ctr = 0;
8527
cc52276b
WC
8528 // not const since embedded task_finder_target struct changes
8529 s.op->newline() << "static struct stap_uprobe_tf stap_uprobe_finders[] = {";
89ba3085
FCE
8530 s.op->indent(1);
8531 for (unsigned i=0; i<probes.size(); i++)
8532 {
8533 uprobe_derived_probe *p = probes[i];
8534 string pbmkey = make_pbm_key (p);
8535 if (module_index.find (pbmkey) == module_index.end())
8536 {
8537 module_index[pbmkey] = module_index_ctr++;
8538
8539 s.op->newline() << "{";
8540 // NB: it's essential that make_pbm_key() use all of and
8541 // only the same fields as we're about to emit.
8542 s.op->line() << " .finder={";
1af100fc 8543 s.op->line() << " .purpose=\"uprobes\",";
89ba3085 8544 if (p->pid != 0)
68910c97
JK
8545 s.op->line() << " .pid=" << p->pid << ",";
8546
8547 if (p->section == "") // .statement(addr).absolute
8548 s.op->line() << " .callback=&stap_uprobe_process_found,";
89ba3085
FCE
8549 else if (p->section == ".absolute") // proxy for ET_EXEC -> exec()'d program
8550 {
8551 s.op->line() << " .procname=" << lex_cast_qstring(p->module) << ",";
8552 s.op->line() << " .callback=&stap_uprobe_process_found,";
8553 }
68910c97 8554 else if (p->section != ".absolute") // ET_DYN
89ba3085 8555 {
4ad95bbc
SC
8556 if (p->has_library)
8557 s.op->line() << " .procname=\"" << p->path << "\", ";
89ba3085
FCE
8558 s.op->line() << " .mmap_callback=&stap_uprobe_mmap_found, ";
8559 s.op->line() << " .munmap_callback=&stap_uprobe_munmap_found, ";
19d91f6c 8560 s.op->line() << " .callback=&stap_uprobe_process_munmap,";
89ba3085 8561 }
89ba3085 8562 s.op->line() << " },";
68910c97
JK
8563 if (p->module != "")
8564 s.op->line() << " .pathname=" << lex_cast_qstring(p->module) << ", ";
89ba3085
FCE
8565 s.op->line() << " },";
8566 }
c57ea854 8567 else
822a6a3d 8568 { } // skip it in this pass, already have a suitable stap_uprobe_tf slot for it.
89ba3085
FCE
8569 }
8570 s.op->newline(-1) << "};";
8571
8572 s.op->assert_0_indent();
8573
3689db05
SC
8574 unsigned pci;
8575 for (pci=0; pci<probes.size(); pci++)
8576 {
8577 // List of perf counters used by each probe
8578 // This list is an index into struct stap_perf_probe,
8579 uprobe_derived_probe *p = probes[pci];
698de6cc 8580 std::set<derived_probe*>::iterator pcii;
3689db05
SC
8581 s.op->newline() << "long perf_counters_" + lex_cast(pci) + "[] = {";
8582 for (pcii = p->perf_counter_refs.begin();
8583 pcii != p->perf_counter_refs.end(); pcii++)
8584 {
8585 map<string, pair<string,derived_probe*> >::iterator it;
8586 unsigned i = 0;
8587 // Find the associated perf.counter probe
8588 for (it=s.perf_counters.begin() ;
8589 it != s.perf_counters.end(); it++, i++)
8590 if ((*it).second.second == (*pcii))
8591 break;
8592 s.op->line() << lex_cast(i) << ", ";
8593 }
8594 s.op->newline() << "};";
8595 }
8596
cc52276b
WC
8597 // NB: read-only structure
8598 s.op->newline() << "static const struct stap_uprobe_spec stap_uprobe_specs [] = {";
a96d1db0 8599 s.op->indent(1);
888af770
FCE
8600 for (unsigned i =0; i<probes.size(); i++)
8601 {
8602 uprobe_derived_probe* p = probes[i];
8603 s.op->newline() << "{";
89ba3085
FCE
8604 string key = make_pbm_key (p);
8605 unsigned value = module_index[key];
759e1d76
FCE
8606 if (value != 0)
8607 s.op->line() << " .tfi=" << value << ",";
6b66b9f7 8608 s.op->line() << " .address=(unsigned long)0x" << hex << p->addr << dec << "ULL,";
faea5e16 8609 s.op->line() << " .probe=" << common_probe_init (p) << ",";
4ddb6dd0 8610
038c38c6 8611 if (p->sdt_semaphore_addr != 0)
63b4fd14 8612 s.op->line() << " .sdt_sem_offset=(unsigned long)0x"
038c38c6 8613 << hex << p->sdt_semaphore_addr << dec << "ULL,";
4ddb6dd0 8614
0d049a1d 8615 // XXX: don't bother emit if array is empty
3689db05
SC
8616 s.op->line() << " .perf_counters_dim=ARRAY_SIZE(perf_counters_" << lex_cast(i) << "),";
8617 // List of perf counters used by a probe from above
0d049a1d 8618 s.op->line() << " .perf_counters=perf_counters_" + lex_cast(i) + ",";
3689db05 8619
4ddb6dd0
JS
8620 if (p->has_return)
8621 s.op->line() << " .return_p=1,";
888af770
FCE
8622 s.op->line() << " },";
8623 }
8624 s.op->newline(-1) << "};";
a96d1db0 8625
89ba3085
FCE
8626 s.op->assert_0_indent();
8627
48e685da 8628 s.op->newline() << "static void enter_uprobe_probe (struct uprobe *inst, struct pt_regs *regs) {";
888af770 8629 s.op->newline(1) << "struct stap_uprobe *sup = container_of(inst, struct stap_uprobe, up);";
89ba3085 8630 s.op->newline() << "const struct stap_uprobe_spec *sups = &stap_uprobe_specs [sup->spec_index];";
71db462b 8631 common_probe_entryfn_prologue (s, "STAP_SESSION_RUNNING", "sups->probe",
cda141c2 8632 "stp_probe_type_uprobe");
0e090c74 8633 s.op->newline() << "if (sup->spec_index < 0 || "
6e895029
DS
8634 << "sup->spec_index >= " << probes.size() << ") {";
8635 s.op->newline(1) << "_stp_error (\"bad spec_index %d (max " << probes.size()
0e090c74 8636 << "): %s\", sup->spec_index, c->probe_point);";
6e895029
DS
8637 s.op->newline() << "goto probe_epilogue;";
8638 s.op->newline(-1) << "}";
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);";
259d54c0 8648 s.op->newline() << "SET_REG_IP(regs, inst->vaddr);";
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
f887a8c9 8653 common_probe_entryfn_epilogue (s, true);
888af770 8654 s.op->newline(-1) << "}";
a96d1db0 8655
48e685da 8656 s.op->newline() << "static void enter_uretprobe_probe (struct uretprobe_instance *inst, struct pt_regs *regs) {";
888af770 8657 s.op->newline(1) << "struct stap_uprobe *sup = container_of(inst->rp, struct stap_uprobe, urp);";
89ba3085 8658 s.op->newline() << "const struct stap_uprobe_spec *sups = &stap_uprobe_specs [sup->spec_index];";
71db462b 8659 common_probe_entryfn_prologue (s, "STAP_SESSION_RUNNING", "sups->probe",
cda141c2 8660 "stp_probe_type_uretprobe");
6dceb5c9 8661 s.op->newline() << "c->ips.ri = inst;";
0e090c74 8662 s.op->newline() << "if (sup->spec_index < 0 || "
0d5561a5
DS
8663 << "sup->spec_index >= " << probes.size() << ") {";
8664 s.op->newline(1) << "_stp_error (\"bad spec_index %d (max " << probes.size()
0e090c74 8665 << "): %s\", sup->spec_index, c->probe_point);";
0d5561a5
DS
8666 s.op->newline() << "goto probe_epilogue;";
8667 s.op->newline(-1) << "}";
8668
d9aed31e 8669 s.op->newline() << "c->uregs = regs;";
e04b5d74 8670 s.op->newline() << "c->user_mode_p = 1;";
6415ddde
MW
8671
8672 // Make it look like the IP is set as it would in the actual user
8673 // task when calling real probe handler. Reset IP regs on return, so
8674 // we don't confuse uprobes. PR10458
8675 s.op->newline() << "{";
8676 s.op->indent(1);
d9aed31e 8677 s.op->newline() << "unsigned long uprobes_ip = REG_IP(c->uregs);";
5e562a69 8678 s.op->newline() << "SET_REG_IP(regs, inst->ret_addr);";
26e63673 8679 s.op->newline() << "(*sups->probe->ph) (c);";
259d54c0 8680 s.op->newline() << "SET_REG_IP(regs, uprobes_ip);";
6415ddde
MW
8681 s.op->newline(-1) << "}";
8682
f887a8c9 8683 common_probe_entryfn_epilogue (s, true);
a96d1db0
DN
8684 s.op->newline(-1) << "}";
8685
89ba3085 8686 s.op->newline();
2ba1736a 8687 s.op->newline() << "#include \"linux/uprobes-common.c\"";
6d0f3f0c 8688 s.op->newline();
888af770 8689}
935447c8
DS
8690
8691
888af770 8692void
2b69faaf 8693uprobe_derived_probe_group::emit_module_utrace_init (systemtap_session& s)
935447c8 8694{
888af770 8695 if (probes.empty()) return;
935447c8 8696
2b69faaf 8697 s.op->newline() << "/* ---- utrace uprobes ---- */";
935447c8 8698
01b05e2e 8699 s.op->newline() << "for (j=0; j<MAXUPROBES; j++) {";
5e112f92
FCE
8700 s.op->newline(1) << "struct stap_uprobe *sup = & stap_uprobes[j];";
8701 s.op->newline() << "sup->spec_index = -1;"; // free slot
80b4ad8b
FCE
8702 // NB: we assume the rest of the struct (specificaly, sup->up) is
8703 // initialized to zero. This is so that we can use
8704 // sup->up->kdata = NULL for "really free!" PR 6829.
5e112f92
FCE
8705 s.op->newline(-1) << "}";
8706 s.op->newline() << "mutex_init (& stap_uprobes_lock);";
935447c8 8707
89ba3085
FCE
8708 // Set up the task_finders
8709 s.op->newline() << "for (i=0; i<sizeof(stap_uprobe_finders)/sizeof(stap_uprobe_finders[0]); i++) {";
8710 s.op->newline(1) << "struct stap_uprobe_tf *stf = & stap_uprobe_finders[i];";
c57ea854 8711 s.op->newline() << "probe_point = stf->pathname;"; // for error messages; XXX: would prefer pp() or something better
89ba3085 8712 s.op->newline() << "rc = stap_register_task_finder_target (& stf->finder);";
935447c8 8713
5e112f92
FCE
8714 // NB: if (rc), there is no need (XXX: nor any way) to clean up any
8715 // finders already registered, since mere registration does not
8716 // cause any utrace or memory allocation actions. That happens only
8717 // later, once the task finder engine starts running. So, for a
8718 // partial initialization requiring unwind, we need do nothing.
8719 s.op->newline() << "if (rc) break;";
a7a68293 8720
888af770
FCE
8721 s.op->newline(-1) << "}";
8722}
d0ea46ce 8723
d0a7f5a9 8724
888af770 8725void
2b69faaf 8726uprobe_derived_probe_group::emit_module_utrace_exit (systemtap_session& s)
888af770
FCE
8727{
8728 if (probes.empty()) return;
2b69faaf 8729 s.op->newline() << "/* ---- utrace uprobes ---- */";
e56e51c9 8730
6d0f3f0c
FCE
8731 // NB: there is no stap_unregister_task_finder_target call;
8732 // important stuff like utrace cleanups are done by
d41d451c
FCE
8733 // __stp_task_finder_cleanup() via stap_stop_task_finder().
8734 //
8735 // This function blocks until all callbacks are completed, so there
8736 // is supposed to be no possibility of any registration-related code starting
8737 // to run in parallel with our shutdown here. So we don't need to protect the
8738 // stap_uprobes[] array with the mutex.
d0a7f5a9 8739
01b05e2e 8740 s.op->newline() << "for (j=0; j<MAXUPROBES; j++) {";
5e112f92 8741 s.op->newline(1) << "struct stap_uprobe *sup = & stap_uprobes[j];";
89ba3085 8742 s.op->newline() << "const struct stap_uprobe_spec *sups = &stap_uprobe_specs [sup->spec_index];";
6d0f3f0c 8743 s.op->newline() << "if (sup->spec_index < 0) continue;"; // free slot
3568f1dd 8744
8faa1fc5 8745 // PR10655: decrement that ENABLED semaphore
c116c31b 8746 s.op->newline() << "if (sup->sdt_sem_address) {";
8faa1fc5
FCE
8747 s.op->newline(1) << "unsigned short sdt_semaphore;"; // NB: fixed size
8748 s.op->newline() << "pid_t pid = (sups->return_p ? sup->urp.u.pid : sup->up.pid);";
8749 s.op->newline() << "struct task_struct *tsk;";
8750 s.op->newline() << "rcu_read_lock();";
6846cfc8 8751
86229a55
DS
8752 // Do a pid->task_struct* lookup. For 2.6.24+, this code assumes
8753 // that the pid is always in the global namespace, not in any
8754 // private namespace.
8faa1fc5 8755 s.op->newline() << "#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,24)";
86229a55
DS
8756 // We'd like to call find_task_by_pid_ns() here, but it isn't
8757 // exported. So, we call what it calls...
8758 s.op->newline() << " tsk = pid_task(find_pid_ns(pid, &init_pid_ns), PIDTYPE_PID);";
8faa1fc5
FCE
8759 s.op->newline() << "#else";
8760 s.op->newline() << " tsk = find_task_by_pid (pid);";
8761 s.op->newline() << "#endif /* 2.6.24 */";
8faa1fc5
FCE
8762
8763 s.op->newline() << "if (tsk) {"; // just in case the thing exited while we weren't watching
3c5b8e2b 8764 s.op->newline(1) << "if (__access_process_vm_noflush(tsk, sup->sdt_sem_address, &sdt_semaphore, sizeof(sdt_semaphore), 0)) {";
63b4fd14 8765 s.op->newline(1) << "sdt_semaphore --;";
903b9fcd 8766 s.op->newline() << "#ifdef DEBUG_UPROBES";
c116c31b 8767 s.op->newline() << "_stp_dbug (__FUNCTION__,__LINE__, \"-semaphore %#x @ %#lx\\n\", sdt_semaphore, sup->sdt_sem_address);";
903b9fcd 8768 s.op->newline() << "#endif";
3c5b8e2b 8769 s.op->newline() << "__access_process_vm_noflush(tsk, sup->sdt_sem_address, &sdt_semaphore, sizeof(sdt_semaphore), 1);";
93c84191 8770 s.op->newline(-1) << "}";
8faa1fc5
FCE
8771 // XXX: need to analyze possibility of race condition
8772 s.op->newline(-1) << "}";
8773 s.op->newline() << "rcu_read_unlock();";
8774 s.op->newline(-1) << "}";
6846cfc8 8775
3568f1dd
FCE
8776 s.op->newline() << "if (sups->return_p) {";
8777 s.op->newline(1) << "#ifdef DEBUG_UPROBES";
89ba3085 8778 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 8779 s.op->newline() << "#endif";
80b4ad8b
FCE
8780 // NB: PR6829 does not change that we still need to unregister at
8781 // *this* time -- when the script as a whole exits.
3568f1dd
FCE
8782 s.op->newline() << "unregister_uretprobe (& sup->urp);";
8783 s.op->newline(-1) << "} else {";
8784 s.op->newline(1) << "#ifdef DEBUG_UPROBES";
8faa1fc5 8785 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
8786 s.op->newline() << "#endif";
8787 s.op->newline() << "unregister_uprobe (& sup->up);";
8788 s.op->newline(-1) << "}";
935447c8 8789
6d0f3f0c 8790 s.op->newline() << "sup->spec_index = -1;";
935447c8 8791
3568f1dd
FCE
8792 // XXX: uprobe missed counts?
8793
6d0f3f0c 8794 s.op->newline(-1) << "}";
935447c8 8795
5e112f92 8796 s.op->newline() << "mutex_destroy (& stap_uprobes_lock);";
935447c8
DS
8797}
8798
2b69faaf
JS
8799
8800void
8801uprobe_derived_probe_group::emit_module_inode_decls (systemtap_session& s)
8802{
8803 if (probes.empty()) return;
8804 s.op->newline() << "/* ---- inode uprobes ---- */";
cfcab6c7 8805 emit_module_maxuprobes (s);
2ba1736a 8806 s.op->newline() << "#include \"linux/uprobes-inode.c\"";
2b69faaf
JS
8807
8808 // Write the probe handler.
79af55c3
JS
8809 s.op->newline() << "static int stapiu_probe_handler "
8810 << "(struct stapiu_consumer *sup, struct pt_regs *regs) {";
8811 s.op->newline(1);
2dbbd473
JS
8812
8813 // Since we're sharing the entry function, we have to dynamically choose the probe_type
8814 string probe_type = "(sup->return_p ? stp_probe_type_uretprobe : stp_probe_type_uprobe)";
71db462b 8815 common_probe_entryfn_prologue (s, "STAP_SESSION_RUNNING", "sup->probe",
2dbbd473
JS
8816 probe_type);
8817
3bff6634 8818 s.op->newline() << "c->uregs = regs;";
e04b5d74 8819 s.op->newline() << "c->user_mode_p = 1;";
497cfcb2 8820 // NB: IP is already set by stapiu_probe_prehandler in uprobes-inode.c
2b69faaf 8821 s.op->newline() << "(*sup->probe->ph) (c);";
39e63bae 8822
f887a8c9 8823 common_probe_entryfn_epilogue (s, true);
2b69faaf
JS
8824 s.op->newline() << "return 0;";
8825 s.op->newline(-1) << "}";
8826 s.op->assert_0_indent();
8827
8828 // Index of all the modules for which we need inodes.
8829 map<string, unsigned> module_index;
8830 unsigned module_index_ctr = 0;
8831
8832 // Discover and declare targets for each unique path.
cfcab6c7 8833 s.op->newline() << "static struct stapiu_target "
2b69faaf
JS
8834 << "stap_inode_uprobe_targets[] = {";
8835 s.op->indent(1);
8836 for (unsigned i=0; i<probes.size(); i++)
8837 {
8838 uprobe_derived_probe *p = probes[i];
cfcab6c7
JS
8839 const string key = make_pbm_key(p);
8840 if (module_index.find (key) == module_index.end())
2b69faaf 8841 {
cfcab6c7
JS
8842 module_index[key] = module_index_ctr++;
8843 s.op->newline() << "{";
8844 s.op->line() << " .finder={";
b78a0fbb 8845 s.op->line() << " .purpose=\"inode-uprobes\",";
cfcab6c7
JS
8846 if (p->pid != 0)
8847 s.op->line() << " .pid=" << p->pid << ",";
8848
8849 if (p->section == "") // .statement(addr).absolute XXX?
8850 s.op->line() << " .callback=&stapiu_process_found,";
8851 else if (p->section == ".absolute") // proxy for ET_EXEC -> exec()'d program
8852 {
8853 s.op->line() << " .procname=" << lex_cast_qstring(p->module) << ",";
8854 s.op->line() << " .callback=&stapiu_process_found,";
8855 }
8856 else if (p->section != ".absolute") // ET_DYN
8857 {
8858 if (p->has_library)
8859 s.op->line() << " .procname=\"" << p->path << "\", ";
8860 s.op->line() << " .mmap_callback=&stapiu_mmap_found, ";
8861 s.op->line() << " .munmap_callback=&stapiu_munmap_found, ";
8862 s.op->line() << " .callback=&stapiu_process_munmap,";
8863 }
8864 s.op->line() << " },";
8865 s.op->line() << " .filename=" << lex_cast_qstring(p->module) << ",";
8866 s.op->line() << " },";
2b69faaf
JS
8867 }
8868 }
8869 s.op->newline(-1) << "};";
8870 s.op->assert_0_indent();
8871
8872 // Declare the actual probes.
3689db05
SC
8873 unsigned pci;
8874 for (pci=0; pci<probes.size(); pci++)
8875 {
8876 // List of perf counters used by each probe
8877 // This list is an index into struct stap_perf_probe,
8878 uprobe_derived_probe *p = probes[pci];
698de6cc 8879 std::set<derived_probe*>::iterator pcii;
3689db05
SC
8880 s.op->newline() << "long perf_counters_" + lex_cast(pci) + "[] = {";
8881 for (pcii = p->perf_counter_refs.begin();
8882 pcii != p->perf_counter_refs.end(); pcii++)
8883 {
8884 map<string, pair<string,derived_probe*> >::iterator it;
8885 unsigned i = 0;
8886 // Find the associated perf.counter probe
4fa83377
SC
8887 for (it=s.perf_counters.begin() ;
8888 it != s.perf_counters.end(); it++, i++)
3689db05
SC
8889 if ((*it).second.second == (*pcii))
8890 break;
8891 s.op->line() << lex_cast(i) << ", ";
8892 }
8893 s.op->newline() << "};";
8894 }
8895
cfcab6c7 8896 s.op->newline() << "static struct stapiu_consumer "
2b69faaf
JS
8897 << "stap_inode_uprobe_consumers[] = {";
8898 s.op->indent(1);
8899 for (unsigned i=0; i<probes.size(); i++)
8900 {
8901 uprobe_derived_probe *p = probes[i];
cfcab6c7
JS
8902 unsigned index = module_index[make_pbm_key(p)];
8903 s.op->newline() << "{";
79af55c3
JS
8904 if (p->has_return)
8905 s.op->line() << " .return_p=1,";
cfcab6c7
JS
8906 s.op->line() << " .target=&stap_inode_uprobe_targets[" << index << "],";
8907 s.op->line() << " .offset=(loff_t)0x" << hex << p->addr << dec << "ULL,";
8908 if (p->sdt_semaphore_addr)
8909 s.op->line() << " .sdt_sem_offset=(loff_t)0x"
8910 << hex << p->sdt_semaphore_addr << dec << "ULL,";
0d049a1d 8911 // XXX: don't bother emit if array is empty
3689db05
SC
8912 s.op->line() << " .perf_counters_dim=ARRAY_SIZE(perf_counters_" << lex_cast(i) << "),";
8913 // List of perf counters used by a probe from above
0d049a1d 8914 s.op->line() << " .perf_counters=perf_counters_" + lex_cast(i) + ",";
cfcab6c7
JS
8915 s.op->line() << " .probe=" << common_probe_init (p) << ",";
8916 s.op->line() << " },";
2b69faaf
JS
8917 }
8918 s.op->newline(-1) << "};";
8919 s.op->assert_0_indent();
8920}
8921
8922
8923void
8924uprobe_derived_probe_group::emit_module_inode_init (systemtap_session& s)
8925{
8926 if (probes.empty()) return;
8927 s.op->newline() << "/* ---- inode uprobes ---- */";
b0f614a0
DS
8928 // Let stapiu_init() handle reporting errors by setting probe_point
8929 // to NULL.
8930 s.op->newline() << "probe_point = NULL;";
cfcab6c7 8931 s.op->newline() << "rc = stapiu_init ("
2b69faaf
JS
8932 << "stap_inode_uprobe_targets, "
8933 << "ARRAY_SIZE(stap_inode_uprobe_targets), "
8934 << "stap_inode_uprobe_consumers, "
8935 << "ARRAY_SIZE(stap_inode_uprobe_consumers));";
8936}
8937
8938
8939void
8940uprobe_derived_probe_group::emit_module_inode_exit (systemtap_session& s)
8941{
8942 if (probes.empty()) return;
8943 s.op->newline() << "/* ---- inode uprobes ---- */";
cfcab6c7 8944 s.op->newline() << "stapiu_exit ("
2b69faaf
JS
8945 << "stap_inode_uprobe_targets, "
8946 << "ARRAY_SIZE(stap_inode_uprobe_targets), "
8947 << "stap_inode_uprobe_consumers, "
8948 << "ARRAY_SIZE(stap_inode_uprobe_consumers));";
8949}
8950
8951
3a894f7e
JS
8952void
8953uprobe_derived_probe_group::emit_module_dyninst_decls (systemtap_session& s)
8954{
8955 if (probes.empty()) return;
8956 s.op->newline() << "/* ---- dyninst uprobes ---- */";
8957 emit_module_maxuprobes (s);
e00f3fb7 8958 s.op->newline() << "#include \"dyninst/uprobes.h\"";
3a894f7e 8959
f31a77f5
DS
8960 // Let the dynprobe_derived_probe_group handle outputting targets
8961 // and probes. This allows us to merge different types of probes.
8962 s.op->newline() << "static struct stapdu_probe stapdu_probes[];";
8963 for (unsigned i = 0; i < probes.size(); i++)
3a894f7e
JS
8964 {
8965 uprobe_derived_probe *p = probes[i];
e00f3fb7 8966
f31a77f5
DS
8967 dynprobe_add_uprobe(s, p->module, p->addr, p->sdt_semaphore_addr,
8968 (p->has_return ? "STAPDYN_PROBE_FLAG_RETURN" : "0"),
8969 common_probe_init(p));
3a894f7e 8970 }
874d38bf
JS
8971 // loc2c-generated code assumes pt_regs are available, so use this to make
8972 // sure we always have *something* for it to dereference...
f31a77f5 8973 s.op->newline() << "static struct pt_regs stapdu_dummy_uregs;";
e00f3fb7 8974
3a894f7e
JS
8975 // Write the probe handler.
8976 // NB: not static, so dyninst can find it
8977 s.op->newline() << "int enter_dyninst_uprobe "
8978 << "(uint64_t index, struct pt_regs *regs) {";
e00f3fb7 8979 s.op->newline(1) << "struct stapdu_probe *sup = &stapdu_probes[index];";
92b97104
JS
8980
8981 // Since we're sharing the entry function, we have to dynamically choose the probe_type
8982 string probe_type = "((sup->flags & STAPDYN_PROBE_FLAG_RETURN) ?"
8983 " stp_probe_type_uretprobe : stp_probe_type_uprobe)";
71db462b 8984 common_probe_entryfn_prologue (s, "STAP_SESSION_RUNNING", "sup->probe",
92b97104
JS
8985 probe_type);
8986
874d38bf 8987 s.op->newline() << "c->uregs = regs ?: &stapdu_dummy_uregs;";
e04b5d74 8988 s.op->newline() << "c->user_mode_p = 1;";
3a894f7e
JS
8989 // XXX: once we have regs, check how dyninst sets the IP
8990 // XXX: the way that dyninst rewrites stuff is probably going to be
8991 // ... very confusing to our backtracer (at least if we stay in process)
8992 s.op->newline() << "(*sup->probe->ph) (c);";
f887a8c9 8993 common_probe_entryfn_epilogue (s, true);
3a894f7e
JS
8994 s.op->newline() << "return 0;";
8995 s.op->newline(-1) << "}";
3debb935 8996 s.op->newline() << "#include \"dyninst/uprobes-regs.c\"";
3a894f7e
JS
8997 s.op->assert_0_indent();
8998}
8999
9000
9001void
9002uprobe_derived_probe_group::emit_module_dyninst_init (systemtap_session& s)
9003{
9004 if (probes.empty()) return;
9005
9006 /* stapdyn handles the dirty work via dyninst */
9007 s.op->newline() << "/* ---- dyninst uprobes ---- */";
9008 s.op->newline() << "/* this section left intentionally blank */";
9009}
9010
9011
9012void
9013uprobe_derived_probe_group::emit_module_dyninst_exit (systemtap_session& s)
9014{
9015 if (probes.empty()) return;
9016
9017 /* stapdyn handles the dirty work via dyninst */
9018 s.op->newline() << "/* ---- dyninst uprobes ---- */";
9019 s.op->newline() << "/* this section left intentionally blank */";
9020}
9021
9022
2b69faaf
JS
9023void
9024uprobe_derived_probe_group::emit_module_decls (systemtap_session& s)
9025{
ac3af990 9026 if (s.runtime_usermode_p())
4441e344
JS
9027 emit_module_dyninst_decls (s);
9028 else if (kernel_supports_inode_uprobes (s))
2b69faaf
JS
9029 emit_module_inode_decls (s);
9030 else
9031 emit_module_utrace_decls (s);
9032}
9033
9034
9035void
9036uprobe_derived_probe_group::emit_module_init (systemtap_session& s)
9037{
ac3af990 9038 if (s.runtime_usermode_p())
4441e344
JS
9039 emit_module_dyninst_init (s);
9040 else if (kernel_supports_inode_uprobes (s))
2b69faaf
JS
9041 emit_module_inode_init (s);
9042 else
9043 emit_module_utrace_init (s);
9044}
9045
9046
9047void
9048uprobe_derived_probe_group::emit_module_exit (systemtap_session& s)
9049{
ac3af990 9050 if (s.runtime_usermode_p())
4441e344
JS
9051 emit_module_dyninst_exit (s);
9052 else if (kernel_supports_inode_uprobes (s))
2b69faaf
JS
9053 emit_module_inode_exit (s);
9054 else
9055 emit_module_utrace_exit (s);
9056}
9057
9058
e6fe60e7
AM
9059// ------------------------------------------------------------------------
9060// Kprobe derived probes
9061// ------------------------------------------------------------------------
9062
4627ed58 9063static const string TOK_KPROBE("kprobe");
935447c8 9064
bae55db9 9065struct kprobe_derived_probe: public derived_probe
d0ea46ce 9066{
23dc94f6
DS
9067 kprobe_derived_probe (systemtap_session& sess,
9068 vector<derived_probe *> & results,
9069 probe *base,
bae55db9
JS
9070 probe_point *location,
9071 const string& name,
9072 int64_t stmt_addr,
e6afb07b 9073 bool has_call,
bae55db9
JS
9074 bool has_return,
9075 bool has_statement,
9076 bool has_maxactive,
b642c901
SC
9077 bool has_path,
9078 bool has_library,
9079 long maxactive_val,
9080 const string& path,
9081 const string& library
bae55db9
JS
9082 );
9083 string symbol_name;
9084 Dwarf_Addr addr;
e6afb07b 9085 bool has_call;
bae55db9
JS
9086 bool has_return;
9087 bool has_statement;
9088 bool has_maxactive;
b642c901
SC
9089 bool has_path;
9090 bool has_library;
bae55db9 9091 long maxactive_val;
b642c901
SC
9092 string path;
9093 string library;
bae55db9
JS
9094 bool access_var;
9095 void printsig (std::ostream &o) const;
9096 void join_group (systemtap_session& s);
9097};
d0ea46ce 9098
bae55db9
JS
9099struct kprobe_derived_probe_group: public derived_probe_group
9100{
9101private:
9102 multimap<string,kprobe_derived_probe*> probes_by_module;
9103 typedef multimap<string,kprobe_derived_probe*>::iterator p_b_m_iterator;
d0ea46ce 9104
bae55db9
JS
9105public:
9106 void enroll (kprobe_derived_probe* probe);
9107 void emit_module_decls (systemtap_session& s);
9108 void emit_module_init (systemtap_session& s);
9109 void emit_module_exit (systemtap_session& s);
9110};
d0ea46ce 9111
23dc94f6
DS
9112struct kprobe_var_expanding_visitor: public var_expanding_visitor
9113{
9114 systemtap_session& sess;
9115 block *add_block;
9116 block *add_call_probe; // synthesized from .return probes with saved $vars
9117 bool add_block_tid, add_call_probe_tid;
bd5b25e1 9118 bool has_return;
23dc94f6 9119
bd5b25e1 9120 kprobe_var_expanding_visitor(systemtap_session& sess, bool has_return):
23dc94f6 9121 sess(sess), add_block(NULL), add_call_probe(NULL),
bd5b25e1
JS
9122 add_block_tid(false), add_call_probe_tid(false),
9123 has_return(has_return) {}
23dc94f6
DS
9124
9125 void visit_entry_op (entry_op* e);
9126};
9127
9128
9129kprobe_derived_probe::kprobe_derived_probe (systemtap_session& sess,
9130 vector<derived_probe *> & results,
9131 probe *base,
e6fe60e7 9132 probe_point *location,
b6371390 9133 const string& name,
e6fe60e7 9134 int64_t stmt_addr,
e6afb07b 9135 bool has_call,
b6371390
JS
9136 bool has_return,
9137 bool has_statement,
9138 bool has_maxactive,
b642c901
SC
9139 bool has_path,
9140 bool has_library,
9141 long maxactive_val,
9142 const string& path,
9143 const string& library
b6371390 9144 ):
4c5d1300 9145 derived_probe (base, location, true /* .components soon rewritten */ ),
e6afb07b 9146 symbol_name (name), addr (stmt_addr), has_call (has_call),
b6371390 9147 has_return (has_return), has_statement (has_statement),
b642c901
SC
9148 has_maxactive (has_maxactive), has_path (has_path),
9149 has_library (has_library),
9150 maxactive_val (maxactive_val),
9151 path (path), library (library)
e6fe60e7
AM
9152{
9153 this->tok = base->tok;
9154 this->access_var = false;
d0ea46ce 9155
e6fe60e7
AM
9156#ifndef USHRT_MAX
9157#define USHRT_MAX 32767
9158#endif
d0ea46ce 9159
46856d8d
JS
9160 // Expansion of $target variables in the probe body produces an error during
9161 // translate phase, since we're not using debuginfo
d0ea46ce 9162
e6fe60e7 9163 vector<probe_point::component*> comps;
46856d8d 9164 comps.push_back (new probe_point::component(TOK_KPROBE));
e6fe60e7 9165
46856d8d
JS
9166 if (has_statement)
9167 {
9ea68eb9
JS
9168 comps.push_back (new probe_point::component(TOK_STATEMENT,
9169 new literal_number(addr, true)));
46856d8d
JS
9170 comps.push_back (new probe_point::component(TOK_ABSOLUTE));
9171 }
9172 else
9173 {
9174 size_t pos = name.find(':');
9175 if (pos != string::npos)
d0ea46ce 9176 {
46856d8d
JS
9177 string module = name.substr(0, pos);
9178 string function = name.substr(pos + 1);
9179 comps.push_back (new probe_point::component(TOK_MODULE, new literal_string(module)));
9180 comps.push_back (new probe_point::component(TOK_FUNCTION, new literal_string(function)));
9181 }
9182 else
9183 comps.push_back (new probe_point::component(TOK_FUNCTION, new literal_string(name)));
46856d8d 9184 }
d0ea46ce 9185
e6afb07b
JL
9186 if (has_call)
9187 comps.push_back (new probe_point::component(TOK_CALL));
b6371390
JS
9188 if (has_return)
9189 comps.push_back (new probe_point::component(TOK_RETURN));
9190 if (has_maxactive)
9191 comps.push_back (new probe_point::component(TOK_MAXACTIVE, new literal_number(maxactive_val)));
d0ea46ce 9192
bd5b25e1 9193 kprobe_var_expanding_visitor v (sess, has_return);
23dc94f6
DS
9194 v.replace (this->body);
9195
9196 // If during target-variable-expanding the probe, we added a new block
9197 // of code, add it to the start of the probe.
9198 if (v.add_block)
9199 this->body = new block(v.add_block, this->body);
9200
9201 // If when target-variable-expanding the probe, we need to
9202 // synthesize a sibling function-entry probe. We don't go through
9203 // the whole probe derivation business (PR10642) that could lead to
9204 // wildcard/alias resolution, or for that dwarf-induced duplication.
9205 if (v.add_call_probe)
9206 {
9207 assert (has_return);
9208
9209 // We temporarily replace base.
9210 statement* old_body = base->body;
9211 base->body = v.add_call_probe;
9212
9213 derived_probe *entry_handler
9214 = new kprobe_derived_probe (sess, results, base, location, name, 0,
e6afb07b
JL
9215 true /* has_call */, false /* has_return */,
9216 has_statement, has_maxactive, has_path,
9217 has_library, maxactive_val, path, library);
23dc94f6
DS
9218 results.push_back (entry_handler);
9219
9220 base->body = old_body;
9221 }
9222
e6fe60e7
AM
9223 this->sole_location()->components = comps;
9224}
d0ea46ce 9225
e6fe60e7
AM
9226void kprobe_derived_probe::printsig (ostream& o) const
9227{
9228 sole_location()->print (o);
9229 o << " /* " << " name = " << symbol_name << "*/";
9230 printsig_nested (o);
9231}
d0ea46ce 9232
e6fe60e7
AM
9233void kprobe_derived_probe::join_group (systemtap_session& s)
9234{
d0ea46ce 9235
e6fe60e7
AM
9236 if (! s.kprobe_derived_probes)
9237 s.kprobe_derived_probes = new kprobe_derived_probe_group ();
9238 s.kprobe_derived_probes->enroll (this);
d0ea46ce 9239
e6fe60e7 9240}
d0ea46ce 9241
e6fe60e7
AM
9242void kprobe_derived_probe_group::enroll (kprobe_derived_probe* p)
9243{
9244 probes_by_module.insert (make_pair (p->symbol_name, p));
9245 // probes of same symbol should share single kprobe/kretprobe
9246}
d0ea46ce 9247
e6fe60e7
AM
9248void
9249kprobe_derived_probe_group::emit_module_decls (systemtap_session& s)
9250{
9251 if (probes_by_module.empty()) return;
d0ea46ce 9252
e6fe60e7 9253 s.op->newline() << "/* ---- kprobe-based probes ---- */";
d0ea46ce 9254
e6fe60e7
AM
9255 // Warn of misconfigured kernels
9256 s.op->newline() << "#if ! defined(CONFIG_KPROBES)";
9257 s.op->newline() << "#error \"Need CONFIG_KPROBES!\"";
9258 s.op->newline() << "#endif";
9259 s.op->newline();
d0ea46ce 9260
f07c3b68 9261 s.op->newline() << "#ifndef KRETACTIVE";
1ee6b5fc 9262 s.op->newline() << "#define KRETACTIVE (max(15,6*(int)num_possible_cpus()))";
f07c3b68
FCE
9263 s.op->newline() << "#endif";
9264
e6fe60e7 9265 // Forward declare the master entry functions
88747011 9266 s.op->newline() << "static int enter_kprobe2_probe (struct kprobe *inst,";
e6fe60e7 9267 s.op->line() << " struct pt_regs *regs);";
88747011 9268 s.op->newline() << "static int enter_kretprobe2_probe (struct kretprobe_instance *inst,";
e6fe60e7 9269 s.op->line() << " struct pt_regs *regs);";
d0ea46ce 9270
e6fe60e7
AM
9271 // Emit an array of kprobe/kretprobe pointers
9272 s.op->newline() << "#if defined(STAPCONF_UNREGISTER_KPROBES)";
c9116e99 9273 s.op->newline() << "static void * stap_unreg_kprobes2[" << probes_by_module.size() << "];";
e6fe60e7 9274 s.op->newline() << "#endif";
d0ea46ce 9275
e6fe60e7 9276 // Emit the actual probe list.
d0ea46ce 9277
e6fe60e7
AM
9278 s.op->newline() << "static struct stap_dwarfless_kprobe {";
9279 s.op->newline(1) << "union { struct kprobe kp; struct kretprobe krp; } u;";
9280 s.op->newline() << "#ifdef __ia64__";
9281 s.op->newline() << "struct kprobe dummy;";
9282 s.op->newline() << "#endif";
9283 s.op->newline(-1) << "} stap_dwarfless_kprobes[" << probes_by_module.size() << "];";
9284 // NB: bss!
d0ea46ce 9285
e6fe60e7
AM
9286 s.op->newline() << "static struct stap_dwarfless_probe {";
9287 s.op->newline(1) << "const unsigned return_p:1;";
9288 s.op->newline() << "const unsigned maxactive_p:1;";
b350f56b 9289 s.op->newline() << "const unsigned optional_p:1;";
e6fe60e7
AM
9290 s.op->newline() << "unsigned registered_p:1;";
9291 s.op->newline() << "const unsigned short maxactive_val;";
935447c8 9292
e6fe60e7
AM
9293 // Function Names are mostly small and uniform enough to justify putting
9294 // char[MAX]'s into the array instead of relocated char*'s.
935447c8 9295
faea5e16
JS
9296 size_t symbol_string_name_max = 0;
9297 size_t symbol_string_name_tot = 0;
e6fe60e7 9298 for (p_b_m_iterator it = probes_by_module.begin(); it != probes_by_module.end(); it++)
6270adc1 9299 {
e6fe60e7
AM
9300 kprobe_derived_probe* p = it->second;
9301#define DOIT(var,expr) do { \
9302 size_t var##_size = (expr) + 1; \
9303 var##_max = max (var##_max, var##_size); \
9304 var##_tot += var##_size; } while (0)
e6fe60e7
AM
9305 DOIT(symbol_string_name, p->symbol_name.size());
9306#undef DOIT
6270adc1
MH
9307 }
9308
e6fe60e7
AM
9309#define CALCIT(var) \
9310 s.op->newline() << "const char " << #var << "[" << var##_name_max << "] ;";
935447c8 9311
e6fe60e7
AM
9312 CALCIT(symbol_string);
9313#undef CALCIT
6270adc1 9314
bd659351 9315 s.op->newline() << "unsigned long address;";
7c3e97f4 9316 s.op->newline() << "const struct stap_probe * const probe;";
e6fe60e7
AM
9317 s.op->newline(-1) << "} stap_dwarfless_probes[] = {";
9318 s.op->indent(1);
6270adc1 9319
e6fe60e7
AM
9320 for (p_b_m_iterator it = probes_by_module.begin(); it != probes_by_module.end(); it++)
9321 {
9322 kprobe_derived_probe* p = it->second;
9323 s.op->newline() << "{";
9324 if (p->has_return)
9325 s.op->line() << " .return_p=1,";
6270adc1 9326
e6fe60e7
AM
9327 if (p->has_maxactive)
9328 {
9329 s.op->line() << " .maxactive_p=1,";
9330 assert (p->maxactive_val >= 0 && p->maxactive_val <= USHRT_MAX);
9331 s.op->line() << " .maxactive_val=" << p->maxactive_val << ",";
9332 }
6270adc1 9333
b350f56b
JS
9334 if (p->locations[0]->optional)
9335 s.op->line() << " .optional_p=1,";
9336
e6fe60e7 9337 if (p->has_statement)
c8d9d15e 9338 s.op->line() << " .address=(unsigned long)0x" << hex << p->addr << dec << "ULL,";
e6fe60e7 9339 else
c8d9d15e 9340 s.op->line() << " .symbol_string=\"" << p->symbol_name << "\",";
5d67b47c 9341
faea5e16 9342 s.op->line() << " .probe=" << common_probe_init (p) << ",";
e6fe60e7 9343 s.op->line() << " },";
935447c8
DS
9344 }
9345
e6fe60e7 9346 s.op->newline(-1) << "};";
5d67b47c 9347
e6fe60e7
AM
9348 // Emit the kprobes callback function
9349 s.op->newline();
88747011 9350 s.op->newline() << "static int enter_kprobe2_probe (struct kprobe *inst,";
e6fe60e7
AM
9351 s.op->line() << " struct pt_regs *regs) {";
9352 // NB: as of PR5673, the kprobe|kretprobe union struct is in BSS
9353 s.op->newline(1) << "int kprobe_idx = ((uintptr_t)inst-(uintptr_t)stap_dwarfless_kprobes)/sizeof(struct stap_dwarfless_kprobe);";
9354 // Check that the index is plausible
9355 s.op->newline() << "struct stap_dwarfless_probe *sdp = &stap_dwarfless_probes[";
9356 s.op->line() << "((kprobe_idx >= 0 && kprobe_idx < " << probes_by_module.size() << ")?";
9357 s.op->line() << "kprobe_idx:0)"; // NB: at least we avoid memory corruption
9358 // XXX: it would be nice to give a more verbose error though; BUG_ON later?
9359 s.op->line() << "];";
71db462b 9360 common_probe_entryfn_prologue (s, "STAP_SESSION_RUNNING", "sdp->probe",
cda141c2 9361 "stp_probe_type_kprobe");
d9aed31e 9362 s.op->newline() << "c->kregs = regs;";
6415ddde
MW
9363
9364 // Make it look like the IP is set as it wouldn't have been replaced
9365 // by a breakpoint instruction when calling real probe handler. Reset
9366 // IP regs on return, so we don't confuse kprobes. PR10458
9367 s.op->newline() << "{";
9368 s.op->indent(1);
d9aed31e 9369 s.op->newline() << "unsigned long kprobes_ip = REG_IP(c->kregs);";
259d54c0 9370 s.op->newline() << "SET_REG_IP(regs, (unsigned long) inst->addr);";
26e63673 9371 s.op->newline() << "(*sdp->probe->ph) (c);";
259d54c0 9372 s.op->newline() << "SET_REG_IP(regs, kprobes_ip);";
6415ddde
MW
9373 s.op->newline(-1) << "}";
9374
f887a8c9 9375 common_probe_entryfn_epilogue (s, true);
e6fe60e7
AM
9376 s.op->newline() << "return 0;";
9377 s.op->newline(-1) << "}";
935447c8 9378
e6fe60e7
AM
9379 // Same for kretprobes
9380 s.op->newline();
88747011 9381 s.op->newline() << "static int enter_kretprobe2_probe (struct kretprobe_instance *inst,";
e6fe60e7
AM
9382 s.op->line() << " struct pt_regs *regs) {";
9383 s.op->newline(1) << "struct kretprobe *krp = inst->rp;";
935447c8 9384
e6fe60e7
AM
9385 // NB: as of PR5673, the kprobe|kretprobe union struct is in BSS
9386 s.op->newline() << "int kprobe_idx = ((uintptr_t)krp-(uintptr_t)stap_dwarfless_kprobes)/sizeof(struct stap_dwarfless_kprobe);";
9387 // Check that the index is plausible
9388 s.op->newline() << "struct stap_dwarfless_probe *sdp = &stap_dwarfless_probes[";
9389 s.op->line() << "((kprobe_idx >= 0 && kprobe_idx < " << probes_by_module.size() << ")?";
9390 s.op->line() << "kprobe_idx:0)"; // NB: at least we avoid memory corruption
9391 // XXX: it would be nice to give a more verbose error though; BUG_ON later?
9392 s.op->line() << "];";
935447c8 9393
71db462b 9394 common_probe_entryfn_prologue (s, "STAP_SESSION_RUNNING", "sdp->probe",
cda141c2 9395 "stp_probe_type_kretprobe");
d9aed31e 9396 s.op->newline() << "c->kregs = regs;";
6dceb5c9 9397 s.op->newline() << "c->ips.krp.pi = inst;"; // for assisting runtime's backtrace logic
6415ddde
MW
9398
9399 // Make it look like the IP is set as it wouldn't have been replaced
9400 // by a breakpoint instruction when calling real probe handler. Reset
9401 // IP regs on return, so we don't confuse kprobes. PR10458
9402 s.op->newline() << "{";
9403 s.op->indent(1);
d9aed31e 9404 s.op->newline() << "unsigned long kprobes_ip = REG_IP(c->kregs);";
259d54c0 9405 s.op->newline() << "SET_REG_IP(regs, (unsigned long) inst->rp->kp.addr);";
26e63673 9406 s.op->newline() << "(*sdp->probe->ph) (c);";
259d54c0 9407 s.op->newline() << "SET_REG_IP(regs, kprobes_ip);";
6415ddde
MW
9408 s.op->newline(-1) << "}";
9409
f887a8c9 9410 common_probe_entryfn_epilogue (s, true);
e6fe60e7
AM
9411 s.op->newline() << "return 0;";
9412 s.op->newline(-1) << "}";
bd659351 9413
03a4ec63 9414 s.op->newline() << "#ifdef STAPCONF_KALLSYMS_ON_EACH_SYMBOL";
bd659351
MW
9415 s.op->newline() << "static int kprobe_resolve(void *data, const char *name,";
9416 s.op->newline() << " struct module *owner,";
9417 s.op->newline() << " unsigned long val) {";
9418 s.op->newline(1) << "int i;";
fc1d2aa2
MW
9419 s.op->newline() << "int *p = (int *) data;";
9420 s.op->newline() << "for (i=0; i<" << probes_by_module.size()
9421 << " && *p > 0; i++) {";
bd659351 9422 s.op->newline(1) << "struct stap_dwarfless_probe *sdp = & stap_dwarfless_probes[i];";
8c272819
YW
9423 s.op->newline() << "if (! sdp->address) {";
9424 s.op->indent(1);
9425 s.op->newline() << "const char *colon;";
9426 s.op->newline() << "if (owner && (colon = strchr(sdp->symbol_string, ':'))) {";
9427 s.op->indent(1);
9428 s.op->newline() << "if ((strlen(owner->name) == (colon - sdp->symbol_string))";
9429 s.op->newline() << " && (strncmp(sdp->symbol_string, owner->name, colon - sdp->symbol_string) == 0)";
9430 s.op->newline() << " && (strcmp(colon + 1, name) == 0)) {";
9431 s.op->newline(1) << "sdp->address = val;";
9432 s.op->newline() << "(*p)--;";
9433 s.op->newline(-1) << "}";
9434 s.op->newline(-1) << "}";
9435 s.op->newline() << "else {";
fc1d2aa2 9436 s.op->newline(1) << "if (strcmp(sdp->symbol_string, name) == 0) {";
bd659351 9437 s.op->newline(1) << "sdp->address = val;";
fc1d2aa2
MW
9438 s.op->newline() << "(*p)--;";
9439 s.op->newline(-1) << "}";
8c272819
YW
9440 s.op->newline(-1) << "}";
9441 s.op->newline(-1) << "}";
9442 s.op->newline(-1) << "}";
fc1d2aa2 9443 s.op->newline() << "return (p > 0) ? 0 : -1;";
bd659351 9444 s.op->newline(-1) << "}";
03a4ec63 9445 s.op->newline() << "#endif";
935447c8
DS
9446}
9447
e6fe60e7 9448
6270adc1 9449void
e6fe60e7 9450kprobe_derived_probe_group::emit_module_init (systemtap_session& s)
6270adc1 9451{
03a4ec63 9452 s.op->newline() << "#ifdef STAPCONF_KALLSYMS_ON_EACH_SYMBOL";
fc1d2aa2
MW
9453 s.op->newline() << "{";
9454 s.op->newline(1) << "int p = 0;";
9455 s.op->newline() << "for (i = 0; i < " << probes_by_module.size() << "; i++) {";
9456 s.op->newline(1) << "struct stap_dwarfless_probe *sdp = & stap_dwarfless_probes[i];";
9457 s.op->newline() << "if (! sdp->address)";
9458 s.op->newline(1) << "p++;";
9459 s.op->newline(-2) << "}";
9460 s.op->newline() << "kallsyms_on_each_symbol(kprobe_resolve, &p);";
9461 s.op->newline(-1) << "}";
03a4ec63 9462 s.op->newline() << "#endif";
bd659351 9463
e6fe60e7 9464 s.op->newline() << "for (i=0; i<" << probes_by_module.size() << "; i++) {";
c8d9d15e 9465 s.op->newline(1) << "struct stap_dwarfless_probe *sdp = & stap_dwarfless_probes[i];";
e6fe60e7 9466 s.op->newline() << "struct stap_dwarfless_kprobe *kp = & stap_dwarfless_kprobes[i];";
c8d9d15e 9467 s.op->newline() << "void *addr = (void *) sdp->address;";
03a4ec63
MW
9468 s.op->newline() << "const char *symbol_name = addr ? NULL : sdp->symbol_string;";
9469
9470 s.op->newline() << "#ifdef STAPCONF_KALLSYMS_ON_EACH_SYMBOL";
bd659351
MW
9471 s.op->newline() << "if (! addr) {";
9472 s.op->newline(1) << "sdp->registered_p = 0;";
9319b767
MW
9473 s.op->newline() << "if (!sdp->optional_p)";
9474 s.op->newline(1) << "_stp_warn (\"probe %s registration error (symbol not found)\", probe_point);";
9475 s.op->newline(-1) << "continue;";
bd659351 9476 s.op->newline(-1) << "}";
03a4ec63
MW
9477 s.op->newline() << "#endif";
9478
26e63673 9479 s.op->newline() << "probe_point = sdp->probe->pp;"; // for error messages
e6fe60e7 9480 s.op->newline() << "if (sdp->return_p) {";
c8d9d15e 9481 s.op->newline(1) << "kp->u.krp.kp.addr = addr;";
766cee5f 9482 s.op->newline() << "#ifdef STAPCONF_KPROBE_SYMBOL_NAME";
03a4ec63 9483 s.op->newline() << "kp->u.krp.kp.symbol_name = (char *) symbol_name;";
766cee5f 9484 s.op->newline() << "#endif";
e6fe60e7
AM
9485 s.op->newline() << "if (sdp->maxactive_p) {";
9486 s.op->newline(1) << "kp->u.krp.maxactive = sdp->maxactive_val;";
9487 s.op->newline(-1) << "} else {";
f07c3b68 9488 s.op->newline(1) << "kp->u.krp.maxactive = KRETACTIVE;";
e6fe60e7 9489 s.op->newline(-1) << "}";
88747011 9490 s.op->newline() << "kp->u.krp.handler = &enter_kretprobe2_probe;";
e6fe60e7
AM
9491 // to ensure safeness of bspcache, always use aggr_kprobe on ia64
9492 s.op->newline() << "#ifdef __ia64__";
e6fe60e7 9493 s.op->newline() << "kp->dummy.addr = kp->u.krp.kp.addr;";
766cee5f 9494 s.op->newline() << "#ifdef STAPCONF_KPROBE_SYMBOL_NAME";
c8d9d15e 9495 s.op->newline() << "kp->dummy.symbol_name = kp->u.krp.kp.symbol_name;";
766cee5f 9496 s.op->newline() << "#endif";
c8d9d15e 9497 s.op->newline() << "kp->dummy.pre_handler = NULL;";
e6fe60e7
AM
9498 s.op->newline() << "rc = register_kprobe (& kp->dummy);";
9499 s.op->newline() << "if (rc == 0) {";
9500 s.op->newline(1) << "rc = register_kretprobe (& kp->u.krp);";
9501 s.op->newline() << "if (rc != 0)";
9502 s.op->newline(1) << "unregister_kprobe (& kp->dummy);";
9503 s.op->newline(-2) << "}";
9504 s.op->newline() << "#else";
9505 s.op->newline() << "rc = register_kretprobe (& kp->u.krp);";
9506 s.op->newline() << "#endif";
9507 s.op->newline(-1) << "} else {";
9508 // to ensure safeness of bspcache, always use aggr_kprobe on ia64
c8d9d15e 9509 s.op->newline(1) << "kp->u.kp.addr = addr;";
766cee5f 9510 s.op->newline() << "#ifdef STAPCONF_KPROBE_SYMBOL_NAME";
03a4ec63 9511 s.op->newline() << "kp->u.kp.symbol_name = (char *) symbol_name;";
766cee5f 9512 s.op->newline() << "#endif";
88747011 9513 s.op->newline() << "kp->u.kp.pre_handler = &enter_kprobe2_probe;";
e6fe60e7 9514 s.op->newline() << "#ifdef __ia64__";
e6fe60e7 9515 s.op->newline() << "kp->dummy.pre_handler = NULL;";
c8d9d15e 9516 s.op->newline() << "kp->dummy.addr = kp->u.kp.addr;";
766cee5f 9517 s.op->newline() << "#ifdef STAPCONF_KPROBE_SYMBOL_NAME";
c8d9d15e 9518 s.op->newline() << "kp->dummy.symbol_name = kp->u.kp.symbol_name;";
766cee5f 9519 s.op->newline() << "#endif";
e6fe60e7
AM
9520 s.op->newline() << "rc = register_kprobe (& kp->dummy);";
9521 s.op->newline() << "if (rc == 0) {";
9522 s.op->newline(1) << "rc = register_kprobe (& kp->u.kp);";
9523 s.op->newline() << "if (rc != 0)";
9524 s.op->newline(1) << "unregister_kprobe (& kp->dummy);";
9525 s.op->newline(-2) << "}";
9526 s.op->newline() << "#else";
9527 s.op->newline() << "rc = register_kprobe (& kp->u.kp);";
9528 s.op->newline() << "#endif";
9529 s.op->newline(-1) << "}";
9530 s.op->newline() << "if (rc) {"; // PR6749: tolerate a failed register_*probe.
9531 s.op->newline(1) << "sdp->registered_p = 0;";
b350f56b 9532 s.op->newline() << "if (!sdp->optional_p)";
50b6acf7 9533 s.op->newline(1) << "_stp_warn (\"probe %s (address 0x%lx) registration error (rc %d)\", probe_point, (unsigned long) addr, rc);";
b350f56b 9534 s.op->newline(-1) << "rc = 0;"; // continue with other probes
e6fe60e7
AM
9535 // XXX: shall we increment numskipped?
9536 s.op->newline(-1) << "}";
6270adc1 9537
e6fe60e7
AM
9538 s.op->newline() << "else sdp->registered_p = 1;";
9539 s.op->newline(-1) << "}"; // for loop
6270adc1
MH
9540}
9541
b4be7cbc 9542
e6fe60e7
AM
9543void
9544kprobe_derived_probe_group::emit_module_exit (systemtap_session& s)
935447c8 9545{
e6fe60e7
AM
9546 //Unregister kprobes by batch interfaces.
9547 s.op->newline() << "#if defined(STAPCONF_UNREGISTER_KPROBES)";
9548 s.op->newline() << "j = 0;";
9549 s.op->newline() << "for (i=0; i<" << probes_by_module.size() << "; i++) {";
9550 s.op->newline(1) << "struct stap_dwarfless_probe *sdp = & stap_dwarfless_probes[i];";
9551 s.op->newline() << "struct stap_dwarfless_kprobe *kp = & stap_dwarfless_kprobes[i];";
9552 s.op->newline() << "if (! sdp->registered_p) continue;";
9553 s.op->newline() << "if (!sdp->return_p)";
c9116e99 9554 s.op->newline(1) << "stap_unreg_kprobes2[j++] = &kp->u.kp;";
e6fe60e7 9555 s.op->newline(-2) << "}";
c9116e99 9556 s.op->newline() << "unregister_kprobes((struct kprobe **)stap_unreg_kprobes2, j);";
e6fe60e7
AM
9557 s.op->newline() << "j = 0;";
9558 s.op->newline() << "for (i=0; i<" << probes_by_module.size() << "; i++) {";
9559 s.op->newline(1) << "struct stap_dwarfless_probe *sdp = & stap_dwarfless_probes[i];";
9560 s.op->newline() << "struct stap_dwarfless_kprobe *kp = & stap_dwarfless_kprobes[i];";
9561 s.op->newline() << "if (! sdp->registered_p) continue;";
9562 s.op->newline() << "if (sdp->return_p)";
c9116e99 9563 s.op->newline(1) << "stap_unreg_kprobes2[j++] = &kp->u.krp;";
e6fe60e7 9564 s.op->newline(-2) << "}";
c9116e99 9565 s.op->newline() << "unregister_kretprobes((struct kretprobe **)stap_unreg_kprobes2, j);";
e6fe60e7
AM
9566 s.op->newline() << "#ifdef __ia64__";
9567 s.op->newline() << "j = 0;";
9568 s.op->newline() << "for (i=0; i<" << probes_by_module.size() << "; i++) {";
9569 s.op->newline(1) << "struct stap_dwarfless_probe *sdp = & stap_dwarfless_probes[i];";
9570 s.op->newline() << "struct stap_dwarfless_kprobe *kp = & stap_dwarfless_kprobes[i];";
9571 s.op->newline() << "if (! sdp->registered_p) continue;";
c9116e99 9572 s.op->newline() << "stap_unreg_kprobes2[j++] = &kp->dummy;";
e6fe60e7 9573 s.op->newline(-1) << "}";
c9116e99 9574 s.op->newline() << "unregister_kprobes((struct kprobe **)stap_unreg_kprobes2, j);";
e6fe60e7
AM
9575 s.op->newline() << "#endif";
9576 s.op->newline() << "#endif";
3e3bd7b6 9577
e6fe60e7
AM
9578 s.op->newline() << "for (i=0; i<" << probes_by_module.size() << "; i++) {";
9579 s.op->newline(1) << "struct stap_dwarfless_probe *sdp = & stap_dwarfless_probes[i];";
9580 s.op->newline() << "struct stap_dwarfless_kprobe *kp = & stap_dwarfless_kprobes[i];";
9581 s.op->newline() << "if (! sdp->registered_p) continue;";
9582 s.op->newline() << "if (sdp->return_p) {";
9583 s.op->newline() << "#if !defined(STAPCONF_UNREGISTER_KPROBES)";
9584 s.op->newline(1) << "unregister_kretprobe (&kp->u.krp);";
9585 s.op->newline() << "#endif";
065d5567 9586 s.op->newline() << "atomic_add (kp->u.krp.nmissed, skipped_count());";
e6fe60e7
AM
9587 s.op->newline() << "#ifdef STP_TIMING";
9588 s.op->newline() << "if (kp->u.krp.nmissed)";
26e63673 9589 s.op->newline(1) << "_stp_warn (\"Skipped due to missed kretprobe/1 on '%s': %d\\n\", sdp->probe->pp, kp->u.krp.nmissed);";
e6fe60e7 9590 s.op->newline(-1) << "#endif";
065d5567 9591 s.op->newline() << "atomic_add (kp->u.krp.kp.nmissed, skipped_count());";
e6fe60e7
AM
9592 s.op->newline() << "#ifdef STP_TIMING";
9593 s.op->newline() << "if (kp->u.krp.kp.nmissed)";
26e63673 9594 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
9595 s.op->newline(-1) << "#endif";
9596 s.op->newline(-1) << "} else {";
9597 s.op->newline() << "#if !defined(STAPCONF_UNREGISTER_KPROBES)";
9598 s.op->newline(1) << "unregister_kprobe (&kp->u.kp);";
9599 s.op->newline() << "#endif";
065d5567 9600 s.op->newline() << "atomic_add (kp->u.kp.nmissed, skipped_count());";
e6fe60e7
AM
9601 s.op->newline() << "#ifdef STP_TIMING";
9602 s.op->newline() << "if (kp->u.kp.nmissed)";
26e63673 9603 s.op->newline(1) << "_stp_warn (\"Skipped due to missed kprobe on '%s': %lu\\n\", sdp->probe->pp, kp->u.kp.nmissed);";
e6fe60e7
AM
9604 s.op->newline(-1) << "#endif";
9605 s.op->newline(-1) << "}";
9606 s.op->newline() << "#if !defined(STAPCONF_UNREGISTER_KPROBES) && defined(__ia64__)";
9607 s.op->newline() << "unregister_kprobe (&kp->dummy);";
9608 s.op->newline() << "#endif";
9609 s.op->newline() << "sdp->registered_p = 0;";
9610 s.op->newline(-1) << "}";
f8a968bc
JS
9611}
9612
e6fe60e7 9613struct kprobe_builder: public derived_probe_builder
3c1b3d06 9614{
9fdf787d 9615public:
2a639817 9616 kprobe_builder() {}
9fdf787d 9617
2a639817 9618 void build_no_more (systemtap_session &s) {}
9fdf787d 9619
e6fe60e7
AM
9620 virtual void build(systemtap_session & sess,
9621 probe * base,
9622 probe_point * location,
9623 literal_map_t const & parameters,
9624 vector<derived_probe *> & finished_results);
9625};
3c1b3d06
FCE
9626
9627
79189b84 9628void
05fb3e0c 9629kprobe_builder::build(systemtap_session & sess,
e6fe60e7
AM
9630 probe * base,
9631 probe_point * location,
9632 literal_map_t const & parameters,
9633 vector<derived_probe *> & finished_results)
79189b84 9634{
e6fe60e7 9635 string function_string_val, module_string_val;
05fb3e0c 9636 string path, library, path_tgt, library_tgt;
b6371390
JS
9637 int64_t statement_num_val = 0, maxactive_val = 0;
9638 bool has_function_str, has_module_str, has_statement_num;
e6afb07b 9639 bool has_absolute, has_call, has_return, has_maxactive;
b642c901 9640 bool has_path, has_library;
79189b84 9641
b6371390
JS
9642 has_function_str = get_param(parameters, TOK_FUNCTION, function_string_val);
9643 has_module_str = get_param(parameters, TOK_MODULE, module_string_val);
e6afb07b 9644 has_call = has_null_param (parameters, TOK_CALL);
b6371390
JS
9645 has_return = has_null_param (parameters, TOK_RETURN);
9646 has_maxactive = get_param(parameters, TOK_MAXACTIVE, maxactive_val);
9647 has_statement_num = get_param(parameters, TOK_STATEMENT, statement_num_val);
9648 has_absolute = has_null_param (parameters, TOK_ABSOLUTE);
b642c901
SC
9649 has_path = get_param (parameters, TOK_PROCESS, path);
9650 has_library = get_param (parameters, TOK_LIBRARY, library);
9651
9652 if (has_path)
05fb3e0c
WF
9653 {
9654 path = find_executable (path, sess.sysroot, sess.sysenv);
9655 path_tgt = path_remove_sysroot(sess, path);
9656 }
b642c901 9657 if (has_library)
05fb3e0c
WF
9658 {
9659 library = find_executable (library, sess.sysroot, sess.sysenv,
9660 "LD_LIBRARY_PATH");
9661 library_tgt = path_remove_sysroot(sess, library);
9662 }
c57ea854 9663
b6371390 9664 if (has_function_str)
6fb70fb7 9665 {
2a639817 9666 if (has_module_str)
9fdf787d
DS
9667 {
9668 function_string_val = module_string_val + ":" + function_string_val;
9669 derived_probe *dp
23dc94f6
DS
9670 = new kprobe_derived_probe (sess, finished_results, base,
9671 location, function_string_val,
e6afb07b
JL
9672 0, has_call, has_return,
9673 has_statement_num, has_maxactive,
9674 has_path, has_library, maxactive_val,
9675 path_tgt, library_tgt);
9fdf787d
DS
9676 finished_results.push_back (dp);
9677 }
9678 else
9679 {
2a639817
JS
9680 vector<string> matches;
9681
9682 // Simple names can be found directly
9683 if (function_string_val.find_first_of("*?[") == string::npos)
9684 {
9685 if (sess.kernel_functions.count(function_string_val))
9686 matches.push_back(function_string_val);
9687 }
9688 else // Search function name list for matching names
9689 {
9690 for (set<string>::const_iterator it = sess.kernel_functions.begin();
9691 it != sess.kernel_functions.end(); it++)
9692 // fnmatch returns zero for matching.
9693 if (fnmatch(function_string_val.c_str(), it->c_str(), 0) == 0)
9694 matches.push_back(*it);
9695 }
86758d5f 9696
2a639817
JS
9697 for (vector<string>::const_iterator it = matches.begin();
9698 it != matches.end(); it++)
9fdf787d 9699 {
2a639817
JS
9700 derived_probe *dp
9701 = new kprobe_derived_probe (sess, finished_results, base,
e6afb07b
JL
9702 location, *it, 0, has_call,
9703 has_return, has_statement_num,
2a639817
JS
9704 has_maxactive, has_path,
9705 has_library, maxactive_val,
9706 path_tgt, library_tgt);
9707 finished_results.push_back (dp);
9fdf787d
DS
9708 }
9709 }
6fb70fb7 9710 }
e6fe60e7 9711 else
b6371390
JS
9712 {
9713 // assert guru mode for absolute probes
9714 if ( has_statement_num && has_absolute && !base->privileged )
dc09353a 9715 throw SEMANTIC_ERROR (_("absolute statement probe in unprivileged script; need stap -g"), base->tok);
b6371390 9716
23dc94f6
DS
9717 finished_results.push_back (new kprobe_derived_probe (sess,
9718 finished_results,
9719 base,
b6371390
JS
9720 location, "",
9721 statement_num_val,
e6afb07b 9722 has_call,
b6371390
JS
9723 has_return,
9724 has_statement_num,
9725 has_maxactive,
b642c901
SC
9726 has_path,
9727 has_library,
9728 maxactive_val,
05fb3e0c
WF
9729 path_tgt,
9730 library_tgt));
96b030fe 9731 }
79189b84
JS
9732}
9733
23dc94f6
DS
9734
9735void
9736kprobe_var_expanding_visitor::visit_entry_op (entry_op *e)
9737{
9738 expression *repl = e;
9739
bd5b25e1
JS
9740 if (has_return)
9741 {
9742 // expand the operand as if it weren't a return probe
9743 has_return = false;
9744 replace (e->operand);
9745 has_return = true;
23dc94f6 9746
bd5b25e1
JS
9747 // XXX it would be nice to use gen_kretprobe_saved_return when
9748 // available, but it requires knowing the types already, which is
9749 // problematic for arbitrary expressons.
9750 repl = gen_mapped_saved_return (sess, e->operand, "entry",
9751 add_block, add_block_tid,
9752 add_call_probe, add_call_probe_tid);
9753 }
23dc94f6
DS
9754 provide (repl);
9755}
9756
9757
dd225250
PS
9758// ------------------------------------------------------------------------
9759// Hardware breakpoint based probes.
9760// ------------------------------------------------------------------------
9761
9762static const string TOK_HWBKPT("data");
9763static const string TOK_HWBKPT_WRITE("write");
9764static const string TOK_HWBKPT_RW("rw");
9765static const string TOK_LENGTH("length");
9766
9767#define HWBKPT_READ 0
9768#define HWBKPT_WRITE 1
9769#define HWBKPT_RW 2
9770struct hwbkpt_derived_probe: public derived_probe
9771{
9772 hwbkpt_derived_probe (probe *base,
9773 probe_point *location,
9774 uint64_t addr,
9775 string symname,
9776 unsigned int len,
9777 bool has_only_read_access,
9778 bool has_only_write_access,
9779 bool has_rw_access
9780 );
9781 Dwarf_Addr hwbkpt_addr;
9782 string symbol_name;
9783 unsigned int hwbkpt_access,hwbkpt_len;
9784
9785 void printsig (std::ostream &o) const;
9786 void join_group (systemtap_session& s);
9787};
9788
9789struct hwbkpt_derived_probe_group: public derived_probe_group
9790{
dd225250 9791private:
dac77b80 9792 vector<hwbkpt_derived_probe*> hwbkpt_probes;
dd225250
PS
9793
9794public:
9795 void enroll (hwbkpt_derived_probe* probe, systemtap_session& s);
9796 void emit_module_decls (systemtap_session& s);
9797 void emit_module_init (systemtap_session& s);
9798 void emit_module_exit (systemtap_session& s);
9799};
9800
9801hwbkpt_derived_probe::hwbkpt_derived_probe (probe *base,
9ea68eb9
JS
9802 probe_point *location,
9803 uint64_t addr,
9804 string symname,
9805 unsigned int len,
9806 bool has_only_read_access,
9807 bool has_only_write_access,
822a6a3d 9808 bool):
4c5d1300 9809 derived_probe (base, location, true /* .components soon rewritten */ ),
dd225250
PS
9810 hwbkpt_addr (addr),
9811 symbol_name (symname),
9812 hwbkpt_len (len)
9813{
9814 this->tok = base->tok;
9815
9816 vector<probe_point::component*> comps;
9817 comps.push_back (new probe_point::component(TOK_KERNEL));
9818
9819 if (hwbkpt_addr)
9ea68eb9
JS
9820 comps.push_back (new probe_point::component (TOK_HWBKPT,
9821 new literal_number(hwbkpt_addr, true)));
9822 else if (symbol_name.size())
9823 comps.push_back (new probe_point::component (TOK_HWBKPT, new literal_string(symbol_name)));
dd225250
PS
9824
9825 comps.push_back (new probe_point::component (TOK_LENGTH, new literal_number(hwbkpt_len)));
9826
9827 if (has_only_read_access)
9ea68eb9 9828 this->hwbkpt_access = HWBKPT_READ ;
dd225250
PS
9829//TODO add code for comps.push_back for read, since this flag is not for x86
9830
9831 else
9ea68eb9
JS
9832 {
9833 if (has_only_write_access)
9834 {
9835 this->hwbkpt_access = HWBKPT_WRITE ;
9836 comps.push_back (new probe_point::component(TOK_HWBKPT_WRITE));
9837 }
9838 else
9839 {
9840 this->hwbkpt_access = HWBKPT_RW ;
9841 comps.push_back (new probe_point::component(TOK_HWBKPT_RW));
9842 }
9843 }
dd225250
PS
9844
9845 this->sole_location()->components = comps;
9846}
9847
9848void hwbkpt_derived_probe::printsig (ostream& o) const
9849{
9850 sole_location()->print (o);
9851 printsig_nested (o);
9852}
9853
9854void hwbkpt_derived_probe::join_group (systemtap_session& s)
9855{
dac77b80
FCE
9856 if (! s.hwbkpt_derived_probes)
9857 s.hwbkpt_derived_probes = new hwbkpt_derived_probe_group ();
dd225250
PS
9858 s.hwbkpt_derived_probes->enroll (this, s);
9859}
9860
9861void hwbkpt_derived_probe_group::enroll (hwbkpt_derived_probe* p, systemtap_session& s)
9862{
dac77b80
FCE
9863 hwbkpt_probes.push_back (p);
9864
9865 unsigned max_hwbkpt_probes_by_arch = 0;
9866 if (s.architecture == "i386" || s.architecture == "x86_64")
9867 max_hwbkpt_probes_by_arch = 4;
9868 else if (s.architecture == "s390")
9869 max_hwbkpt_probes_by_arch = 1;
9870
c57ea854 9871 if (hwbkpt_probes.size() >= max_hwbkpt_probes_by_arch)
2713ea24 9872 s.print_warning (_F("Too many hardware breakpoint probes requested for %s (%zu vs. %u)",
b530b5b3 9873 s.architecture.c_str(), hwbkpt_probes.size(), max_hwbkpt_probes_by_arch));
dd225250
PS
9874}
9875
9876void
9877hwbkpt_derived_probe_group::emit_module_decls (systemtap_session& s)
9878{
dac77b80 9879 if (hwbkpt_probes.empty()) return;
dd225250
PS
9880
9881 s.op->newline() << "/* ---- hwbkpt-based probes ---- */";
9882
9883 s.op->newline() << "#include <linux/perf_event.h>";
9884 s.op->newline() << "#include <linux/hw_breakpoint.h>";
9885 s.op->newline();
9886
9887 // Forward declare the master entry functions
23063de1 9888 s.op->newline() << "#ifdef STAPCONF_PERF_HANDLER_NMI";
dd225250
PS
9889 s.op->newline() << "static int enter_hwbkpt_probe (struct perf_event *bp,";
9890 s.op->line() << " int nmi,";
9891 s.op->line() << " struct perf_sample_data *data,";
9892 s.op->line() << " struct pt_regs *regs);";
23063de1
FCE
9893 s.op->newline() << "#else";
9894 s.op->newline() << "static int enter_hwbkpt_probe (struct perf_event *bp,";
9895 s.op->line() << " struct perf_sample_data *data,";
9896 s.op->line() << " struct pt_regs *regs);";
9897 s.op->newline() << "#endif";
79189b84 9898
dd225250
PS
9899 // Emit the actual probe list.
9900
9901 s.op->newline() << "static struct perf_event_attr ";
dac77b80 9902 s.op->newline() << "stap_hwbkpt_probe_array[" << hwbkpt_probes.size() << "];";
dd225250
PS
9903
9904 s.op->newline() << "static struct perf_event **";
dac77b80 9905 s.op->newline() << "stap_hwbkpt_ret_array[" << hwbkpt_probes.size() << "];";
dd225250
PS
9906 s.op->newline() << "static struct stap_hwbkpt_probe {";
9907 s.op->newline() << "int registered_p:1;";
43650b10 9908// registered_p = 0 signifies a probe that is unregistered (or failed)
dd225250
PS
9909// registered_p = 1 signifies a probe that got registered successfully
9910
faea5e16 9911 // Symbol Names are mostly small and uniform enough
dd225250 9912 // to justify putting const char*.
dac77b80 9913 s.op->newline() << "const char * const symbol;";
dd225250
PS
9914
9915 s.op->newline() << "const unsigned long address;";
9916 s.op->newline() << "uint8_t atype;";
bb0a4e12 9917 s.op->newline() << "unsigned int len;";
7c3e97f4 9918 s.op->newline() << "const struct stap_probe * const probe;";
dd225250
PS
9919 s.op->newline() << "} stap_hwbkpt_probes[] = {";
9920 s.op->indent(1);
9921
dac77b80 9922 for (unsigned int it = 0; it < hwbkpt_probes.size(); it++)
dd225250 9923 {
dac77b80 9924 hwbkpt_derived_probe* p = hwbkpt_probes.at(it);
dd225250 9925 s.op->newline() << "{";
dd225250
PS
9926 if (p->symbol_name.size())
9927 s.op->line() << " .address=(unsigned long)0x0" << "ULL,";
9928 else
9929 s.op->line() << " .address=(unsigned long)0x" << hex << p->hwbkpt_addr << dec << "ULL,";
9930 switch(p->hwbkpt_access){
9931 case HWBKPT_READ:
9932 s.op->line() << " .atype=HW_BREAKPOINT_R ,";
bb0a4e12 9933 break;
dd225250
PS
9934 case HWBKPT_WRITE:
9935 s.op->line() << " .atype=HW_BREAKPOINT_W ,";
bb0a4e12 9936 break;
dd225250
PS
9937 case HWBKPT_RW:
9938 s.op->line() << " .atype=HW_BREAKPOINT_R|HW_BREAKPOINT_W ,";
bb0a4e12 9939 break;
dd225250
PS
9940 };
9941 s.op->line() << " .len=" << p->hwbkpt_len << ",";
faea5e16 9942 s.op->line() << " .probe=" << common_probe_init (p) << ",";
dd225250 9943 s.op->line() << " .symbol=\"" << p->symbol_name << "\",";
dd225250
PS
9944 s.op->line() << " },";
9945 }
dac77b80 9946 s.op->newline(-1) << "};";
dd225250
PS
9947
9948 // Emit the hwbkpt callback function
9949 s.op->newline() ;
23063de1 9950 s.op->newline() << "#ifdef STAPCONF_PERF_HANDLER_NMI";
dd225250
PS
9951 s.op->newline() << "static int enter_hwbkpt_probe (struct perf_event *bp,";
9952 s.op->line() << " int nmi,";
9953 s.op->line() << " struct perf_sample_data *data,";
9954 s.op->line() << " struct pt_regs *regs) {";
23063de1
FCE
9955 s.op->newline() << "#else";
9956 s.op->newline() << "static int enter_hwbkpt_probe (struct perf_event *bp,";
9957 s.op->line() << " struct perf_sample_data *data,";
9958 s.op->line() << " struct pt_regs *regs) {";
9959 s.op->newline() << "#endif";
dac77b80
FCE
9960 s.op->newline(1) << "unsigned int i;";
9961 s.op->newline() << "if (bp->attr.type != PERF_TYPE_BREAKPOINT) return -1;";
9962 s.op->newline() << "for (i=0; i<" << hwbkpt_probes.size() << "; i++) {";
9963 s.op->newline(1) << "struct perf_event_attr *hp = & stap_hwbkpt_probe_array[i];";
9964 // XXX: why not match stap_hwbkpt_ret_array[i] against bp instead?
9965 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) {";
9966 s.op->newline(1) << "struct stap_hwbkpt_probe *sdp = &stap_hwbkpt_probes[i];";
71db462b 9967 common_probe_entryfn_prologue (s, "STAP_SESSION_RUNNING", "sdp->probe",
cda141c2 9968 "stp_probe_type_hwbkpt");
d9aed31e 9969 s.op->newline() << "if (user_mode(regs)) {";
e04b5d74 9970 s.op->newline(1)<< "c->user_mode_p = 1;";
d9aed31e
MW
9971 s.op->newline() << "c->uregs = regs;";
9972 s.op->newline(-1) << "} else {";
9973 s.op->newline(1) << "c->kregs = regs;";
9974 s.op->newline(-1) << "}";
26e63673 9975 s.op->newline() << "(*sdp->probe->ph) (c);";
f887a8c9 9976 common_probe_entryfn_epilogue (s, true);
dac77b80 9977 s.op->newline(-1) << "}";
dd225250
PS
9978 s.op->newline(-1) << "}";
9979 s.op->newline() << "return 0;";
dac77b80 9980 s.op->newline(-1) << "}";
dd225250
PS
9981}
9982
9983void
9984hwbkpt_derived_probe_group::emit_module_init (systemtap_session& s)
9985{
dac77b80 9986 s.op->newline() << "for (i=0; i<" << hwbkpt_probes.size() << "; i++) {";
dd225250
PS
9987 s.op->newline(1) << "struct stap_hwbkpt_probe *sdp = & stap_hwbkpt_probes[i];";
9988 s.op->newline() << "struct perf_event_attr *hp = & stap_hwbkpt_probe_array[i];";
9989 s.op->newline() << "void *addr = (void *) sdp->address;";
9990 s.op->newline() << "const char *hwbkpt_symbol_name = addr ? NULL : sdp->symbol;";
dac77b80
FCE
9991 s.op->newline() << "hw_breakpoint_init(hp);";
9992 s.op->newline() << "if (addr)";
9993 s.op->newline(1) << "hp->bp_addr = (unsigned long) addr;";
9994 s.op->newline(-1) << "else { ";
9995 s.op->newline(1) << "hp->bp_addr = kallsyms_lookup_name(hwbkpt_symbol_name);";
9996 s.op->newline() << "if (!hp->bp_addr) { ";
26e63673 9997 s.op->newline(1) << "_stp_warn(\"Probe %s registration skipped: invalid symbol %s \",sdp->probe->pp,hwbkpt_symbol_name);";
dac77b80
FCE
9998 s.op->newline() << "continue;";
9999 s.op->newline(-1) << "}";
10000 s.op->newline(-1) << "}";
10001 s.op->newline() << "hp->bp_type = sdp->atype;";
10002
10003 // On x86 & x86-64, hp->bp_len is not just a number but a macro/enum (!?!).
c57ea854 10004 if (s.architecture == "i386" || s.architecture == "x86_64" )
dac77b80
FCE
10005 {
10006 s.op->newline() << "switch(sdp->len) {";
10007 s.op->newline() << "case 1:";
10008 s.op->newline(1) << "hp->bp_len = HW_BREAKPOINT_LEN_1;";
10009 s.op->newline() << "break;";
10010 s.op->newline(-1) << "case 2:";
10011 s.op->newline(1) << "hp->bp_len = HW_BREAKPOINT_LEN_2;";
10012 s.op->newline() << "break;";
10013 s.op->newline(-1) << "case 3:";
10014 s.op->newline() << "case 4:";
10015 s.op->newline(1) << "hp->bp_len = HW_BREAKPOINT_LEN_4;";
10016 s.op->newline() << "break;";
10017 s.op->newline(-1) << "case 5:";
10018 s.op->newline() << "case 6:";
10019 s.op->newline() << "case 7:";
10020 s.op->newline() << "case 8:";
10021 s.op->newline() << "default:"; // XXX: could instead reject
10022 s.op->newline(1) << "hp->bp_len = HW_BREAKPOINT_LEN_8;";
10023 s.op->newline() << "break;";
10024 s.op->newline(-1) << "}";
10025 }
10026 else // other architectures presumed straightforward
10027 s.op->newline() << "hp->bp_len = sdp->len;";
10028
26e63673 10029 s.op->newline() << "probe_point = sdp->probe->pp;"; // for error messages
b273669e
MW
10030 s.op->newline() << "#ifdef STAPCONF_HW_BREAKPOINT_CONTEXT";
10031 s.op->newline() << "stap_hwbkpt_ret_array[i] = register_wide_hw_breakpoint(hp, (void *)&enter_hwbkpt_probe, NULL);";
10032 s.op->newline() << "#else";
dac77b80 10033 s.op->newline() << "stap_hwbkpt_ret_array[i] = register_wide_hw_breakpoint(hp, (void *)&enter_hwbkpt_probe);";
b273669e 10034 s.op->newline() << "#endif";
43650b10 10035 s.op->newline() << "rc = 0;";
dac77b80 10036 s.op->newline() << "if (IS_ERR(stap_hwbkpt_ret_array[i])) {";
43650b10
WC
10037 s.op->newline(1) << "rc = PTR_ERR(stap_hwbkpt_ret_array[i]);";
10038 s.op->newline() << "stap_hwbkpt_ret_array[i] = 0;";
10039 s.op->newline(-1) << "}";
217ef1f4
WC
10040 s.op->newline() << "if (rc) {";
10041 s.op->newline(1) << "_stp_warn(\"Hwbkpt probe %s: registration error %d, addr %p, name %s\", probe_point, rc, addr, hwbkpt_symbol_name);";
43650b10 10042 s.op->newline() << "sdp->registered_p = 0;";
dac77b80 10043 s.op->newline(-1) << "}";
dd225250 10044 s.op->newline() << " else sdp->registered_p = 1;";
dd225250
PS
10045 s.op->newline(-1) << "}"; // for loop
10046}
10047
10048void
10049hwbkpt_derived_probe_group::emit_module_exit (systemtap_session& s)
10050{
10051 //Unregister hwbkpt probes.
dac77b80 10052 s.op->newline() << "for (i=0; i<" << hwbkpt_probes.size() << "; i++) {";
dd225250 10053 s.op->newline(1) << "struct stap_hwbkpt_probe *sdp = & stap_hwbkpt_probes[i];";
dac77b80
FCE
10054 s.op->newline() << "if (sdp->registered_p == 0) continue;";
10055 s.op->newline() << "unregister_wide_hw_breakpoint(stap_hwbkpt_ret_array[i]);";
dd225250
PS
10056 s.op->newline() << "sdp->registered_p = 0;";
10057 s.op->newline(-1) << "}";
10058}
10059
10060struct hwbkpt_builder: public derived_probe_builder
10061{
10062 hwbkpt_builder() {}
10063 virtual void build(systemtap_session & sess,
10064 probe * base,
10065 probe_point * location,
10066 literal_map_t const & parameters,
10067 vector<derived_probe *> & finished_results);
10068};
10069
10070void
10071hwbkpt_builder::build(systemtap_session & sess,
10072 probe * base,
10073 probe_point * location,
10074 literal_map_t const & parameters,
10075 vector<derived_probe *> & finished_results)
10076{
10077 string symbol_str_val;
10078 int64_t hwbkpt_address, len;
10079 bool has_addr, has_symbol_str, has_write, has_rw, has_len;
10080
b47f3a55 10081 if (! (sess.kernel_config["CONFIG_PERF_EVENTS"] == string("y")))
dc09353a 10082 throw SEMANTIC_ERROR (_("CONFIG_PERF_EVENTS not available on this kernel"),
b47f3a55
FCE
10083 location->components[0]->tok);
10084 if (! (sess.kernel_config["CONFIG_HAVE_HW_BREAKPOINT"] == string("y")))
dc09353a 10085 throw SEMANTIC_ERROR (_("CONFIG_HAVE_HW_BREAKPOINT not available on this kernel"),
b47f3a55
FCE
10086 location->components[0]->tok);
10087
dd225250
PS
10088 has_addr = get_param (parameters, TOK_HWBKPT, hwbkpt_address);
10089 has_symbol_str = get_param (parameters, TOK_HWBKPT, symbol_str_val);
10090 has_len = get_param (parameters, TOK_LENGTH, len);
10091 has_write = (parameters.find(TOK_HWBKPT_WRITE) != parameters.end());
10092 has_rw = (parameters.find(TOK_HWBKPT_RW) != parameters.end());
10093
b31695eb
JL
10094 // Make an intermediate pp that is well-formed. It's pretty much the same as
10095 // the user-provided one, except that the addr literal is well-typed.
10096 probe_point* well_formed_loc = new probe_point(*location);
10097 well_formed_loc->well_formed = true;
10098
10099 vector<probe_point::component*> well_formed_comps;
10100 vector<probe_point::component*>::iterator it;
10101 for (it = location->components.begin();
10102 it != location->components.end(); ++it)
10103 if ((*it)->functor == TOK_HWBKPT && has_addr)
10104 well_formed_comps.push_back(new probe_point::component(TOK_HWBKPT,
10105 new literal_number(hwbkpt_address, true /* hex */ )));
10106 else
10107 well_formed_comps.push_back(*it);
10108 well_formed_loc->components = well_formed_comps;
10109 probe *new_base = new probe (base, well_formed_loc);
10110
dd225250
PS
10111 if (!has_len)
10112 len = 1;
10113
10114 if (has_addr)
b31695eb 10115 finished_results.push_back (new hwbkpt_derived_probe (new_base,
dd225250
PS
10116 location,
10117 hwbkpt_address,
10118 "",len,0,
10119 has_write,
10120 has_rw));
5d8a0aea 10121 else if (has_symbol_str)
b31695eb 10122 finished_results.push_back (new hwbkpt_derived_probe (new_base,
dd225250
PS
10123 location,
10124 0,
10125 symbol_str_val,len,0,
10126 has_write,
10127 has_rw));
5d8a0aea
FCE
10128 else
10129 assert (0);
dd225250 10130}
342d3f96 10131
0a6f5a3f
JS
10132// ------------------------------------------------------------------------
10133// statically inserted kernel-tracepoint derived probes
10134// ------------------------------------------------------------------------
10135
6fb70fb7 10136struct tracepoint_arg
79189b84 10137{
ad370dcc 10138 string name, c_type, typecast;
dcaa1a65 10139 bool usable, used, isptr;
f8a968bc 10140 Dwarf_Die type_die;
dcaa1a65 10141 tracepoint_arg(): usable(false), used(false), isptr(false) {}
6fb70fb7 10142};
79189b84 10143
0a6f5a3f
JS
10144struct tracepoint_derived_probe: public derived_probe
10145{
79189b84
JS
10146 tracepoint_derived_probe (systemtap_session& s,
10147 dwflpp& dw, Dwarf_Die& func_die,
10148 const string& tracepoint_name,
10149 probe* base_probe, probe_point* location);
bc9a523d 10150
79189b84 10151 systemtap_session& sess;
6fb70fb7
JS
10152 string tracepoint_name, header;
10153 vector <struct tracepoint_arg> args;
bc9a523d 10154
6fb70fb7 10155 void build_args(dwflpp& dw, Dwarf_Die& func_die);
d0bfd2ac 10156 void getargs (std::list<std::string> &arg_set) const;
79189b84 10157 void join_group (systemtap_session& s);
3e3bd7b6 10158 void print_dupe_stamp(ostream& o);
0a6f5a3f 10159};
79189b84
JS
10160
10161
0a6f5a3f 10162struct tracepoint_derived_probe_group: public generic_dpg<tracepoint_derived_probe>
79189b84 10163{
79189b84
JS
10164 void emit_module_decls (systemtap_session& s);
10165 void emit_module_init (systemtap_session& s);
10166 void emit_module_exit (systemtap_session& s);
0a6f5a3f 10167};
79189b84 10168
bc9a523d 10169
f8a968bc
JS
10170struct tracepoint_var_expanding_visitor: public var_expanding_visitor
10171{
10172 tracepoint_var_expanding_visitor(dwflpp& dw, const string& probe_name,
10173 vector <struct tracepoint_arg>& args):
10174 dw (dw), probe_name (probe_name), args (args) {}
10175 dwflpp& dw;
10176 const string& probe_name;
10177 vector <struct tracepoint_arg>& args;
bc9a523d 10178
f8a968bc
JS
10179 void visit_target_symbol (target_symbol* e);
10180 void visit_target_symbol_arg (target_symbol* e);
10181 void visit_target_symbol_context (target_symbol* e);
10182};
79189b84
JS
10183
10184
f8a968bc
JS
10185void
10186tracepoint_var_expanding_visitor::visit_target_symbol_arg (target_symbol* e)
75ead1f7 10187{
cc9001af 10188 string argname = e->sym_name();
75ead1f7 10189
f8a968bc
JS
10190 // search for a tracepoint parameter matching this name
10191 tracepoint_arg *arg = NULL;
10192 for (unsigned i = 0; i < args.size(); ++i)
dcaa1a65 10193 if (args[i].usable && args[i].name == argname)
f8a968bc
JS
10194 {
10195 arg = &args[i];
10196 arg->used = true;
10197 break;
10198 }
75ead1f7 10199
f8a968bc
JS
10200 if (arg == NULL)
10201 {
1d0499c2 10202 set<string> vars;
f8a968bc 10203 for (unsigned i = 0; i < args.size(); ++i)
1d0499c2
JL
10204 vars.insert("$" + args[i].name);
10205 vars.insert("$$name");
10206 vars.insert("$$parms");
10207 vars.insert("$$vars");
10208 string sugs = levenshtein_suggest(e->name, vars); // no need to limit, there's not that many
75ead1f7 10209
f8a968bc
JS
10210 // We hope that this value ends up not being referenced after all, so it
10211 // can be optimized out quietly.
1d0499c2
JL
10212 throw SEMANTIC_ERROR(_F("unable to find tracepoint variable '%s'%s",
10213 e->name.c_str(), sugs.empty() ? "" :
10214 (_(" (alternatives: ") + sugs + ")").c_str()), e->tok);
10215 // NB: we use 'alternatives' because we list all
f8a968bc
JS
10216 // NB: we can have multiple errors, since a target variable
10217 // may be expanded in several different contexts:
10218 // trace ("*") { $foo->bar }
f8a968bc 10219 }
75ead1f7 10220
f8a968bc 10221 // make sure we're not dereferencing base types
dc5a09fc 10222 if (!arg->isptr)
d19a9a82 10223 e->assert_no_components("tracepoint", true);
75ead1f7 10224
f8a968bc
JS
10225 // we can only write to dereferenced fields, and only if guru mode is on
10226 bool lvalue = is_active_lvalue(e);
10227 if (lvalue && (!dw.sess.guru_mode || e->components.empty()))
dc09353a 10228 throw SEMANTIC_ERROR(_F("write to tracepoint variable '%s' not permitted; need stap -g", e->name.c_str()), e->tok);
c69a87e0 10229
ad370dcc
JS
10230 // XXX: if a struct/union arg is passed by value, then writing to its fields
10231 // is also meaningless until you dereference past a pointer member. It's
10232 // harder to detect and prevent that though...
75ead1f7 10233
f8a968bc
JS
10234 if (e->components.empty())
10235 {
03c75a4a 10236 if (e->addressof)
dc09353a 10237 throw SEMANTIC_ERROR(_("cannot take address of tracepoint variable"), e->tok);
a45664f4 10238
3e3bd7b6 10239 // Just grab the value from the probe locals
a45664f4
JS
10240 symbol* sym = new symbol;
10241 sym->tok = e->tok;
10242 sym->name = "__tracepoint_arg_" + arg->name;
10243 provide (sym);
f8a968bc
JS
10244 }
10245 else
10246 {
5f36109e
JS
10247 // make a copy of the original as a bare target symbol for the tracepoint
10248 // value, which will be passed into the dwarf dereferencing code
10249 target_symbol* e2 = deep_copy_visitor::deep_copy(e);
10250 e2->components.clear();
10251
10252 if (e->components.back().type == target_symbol::comp_pretty_print)
10253 {
10254 if (lvalue)
dc09353a 10255 throw SEMANTIC_ERROR(_("cannot write to pretty-printed variable"), e->tok);
5f36109e 10256
d19a9a82 10257 dwarf_pretty_print dpp(dw, &arg->type_die, e2, arg->isptr, false, *e);
5f36109e
JS
10258 dpp.expand()->visit (this);
10259 return;
10260 }
10261
1c0be8c7 10262 bool userspace_p = false;
f8a968bc 10263 string fname = (string(lvalue ? "_tracepoint_tvar_set" : "_tracepoint_tvar_get")
cc9001af 10264 + "_" + e->sym_name()
aca66a36 10265 + "_" + lex_cast(tick++));
75ead1f7 10266
1c0be8c7
JS
10267 exp_type type = pe_long;
10268 string code = dw.literal_stmt_for_pointer (&arg->type_die, e, lvalue, type);
6fda2dff 10269
1c0be8c7
JS
10270 functioncall* n = synthetic_embedded_deref_call(dw.sess, fname, code, type,
10271 userspace_p, lvalue, e, e2);
75ead1f7 10272
f8a968bc
JS
10273 if (lvalue)
10274 {
10275 // Provide the functioncall to our parent, so that it can be
10276 // used to substitute for the assignment node immediately above
10277 // us.
10278 assert(!target_symbol_setter_functioncalls.empty());
10279 *(target_symbol_setter_functioncalls.top()) = n;
10280 }
75ead1f7 10281
1c0be8c7
JS
10282 // Revisit the functioncall so arguments can be expanded.
10283 n->visit (this);
f8a968bc 10284 }
75ead1f7
JS
10285}
10286
10287
f8a968bc
JS
10288void
10289tracepoint_var_expanding_visitor::visit_target_symbol_context (target_symbol* e)
0a6f5a3f 10290{
03c75a4a 10291 if (e->addressof)
dc09353a 10292 throw SEMANTIC_ERROR(_("cannot take address of context variable"), e->tok);
03c75a4a 10293
f8a968bc 10294 if (is_active_lvalue (e))
dc09353a 10295 throw SEMANTIC_ERROR(_F("write to tracepoint '%s' not permitted", e->name.c_str()), e->tok);
0a6f5a3f 10296
277c21bc 10297 if (e->name == "$$name")
f8a968bc 10298 {
5f36109e
JS
10299 e->assert_no_components("tracepoint");
10300
bfdaad1e
DS
10301 // Synthesize an embedded expression.
10302 embedded_expr *expr = new embedded_expr;
10303 expr->tok = e->tok;
10304 expr->code = string("/* string */ /* pure */ ")
6dceb5c9 10305 + string("c->ips.tracepoint_name ? c->ips.tracepoint_name : \"\"");
bfdaad1e 10306 provide (expr);
f8a968bc 10307 }
277c21bc 10308 else if (e->name == "$$vars" || e->name == "$$parms")
f8a968bc 10309 {
5f36109e
JS
10310 e->assert_no_components("tracepoint", true);
10311
1c922ad7 10312 print_format* pf = print_format::create(e->tok, "sprintf");
0a6f5a3f 10313
f8a968bc 10314 for (unsigned i = 0; i < args.size(); ++i)
b278033a 10315 {
dcaa1a65
JS
10316 if (!args[i].usable)
10317 continue;
f8a968bc
JS
10318 if (i > 0)
10319 pf->raw_components += " ";
10320 pf->raw_components += args[i].name;
3e3bd7b6 10321 target_symbol *tsym = new target_symbol;
f8a968bc 10322 tsym->tok = e->tok;
277c21bc 10323 tsym->name = "$" + args[i].name;
5f36109e 10324 tsym->components = e->components;
b278033a 10325
f8a968bc
JS
10326 // every variable should always be accessible!
10327 tsym->saved_conversion_error = 0;
8c2f50c0 10328 expression *texp = require<expression> (tsym); // NB: throws nothing ...
14a97852
JS
10329 if (tsym->saved_conversion_error) // ... but this is how we know it happened.
10330 {
10331 if (dw.sess.verbose>2)
e26c2f83 10332 for (const semantic_error *c = tsym->saved_conversion_error;
c081af73 10333 c != 0; c = c->get_chain())
4c5d9906 10334 clog << _("variable location problem [man error::dwarf]: ") << c->what() << endl;
14a97852
JS
10335 pf->raw_components += "=?";
10336 continue;
10337 }
b278033a 10338
5f36109e
JS
10339 if (!e->components.empty() &&
10340 e->components[0].type == target_symbol::comp_pretty_print)
10341 pf->raw_components += "=%s";
10342 else
10343 pf->raw_components += args[i].isptr ? "=%p" : "=%#x";
f8a968bc
JS
10344 pf->args.push_back(texp);
10345 }
0a6f5a3f 10346
f8a968bc
JS
10347 pf->components = print_format::string_to_components(pf->raw_components);
10348 provide (pf);
b278033a 10349 }
f8a968bc
JS
10350 else
10351 assert(0); // shouldn't get here
0a6f5a3f
JS
10352}
10353
0a6f5a3f 10354void
f8a968bc 10355tracepoint_var_expanding_visitor::visit_target_symbol (target_symbol* e)
0a6f5a3f 10356{
aff5d390 10357 try
c69a87e0 10358 {
bd1fcbad 10359 assert(e->name.size() > 0 && e->name[0] == '$');
aff5d390 10360
277c21bc 10361 if (e->name == "$$name" || e->name == "$$parms" || e->name == "$$vars")
c69a87e0 10362 visit_target_symbol_context (e);
bd1fcbad 10363
c69a87e0
FCE
10364 else
10365 visit_target_symbol_arg (e);
10366 }
10367 catch (const semantic_error &er)
10368 {
1af1e62d 10369 e->chain (er);
c69a87e0
FCE
10370 provide (e);
10371 }
0a6f5a3f
JS
10372}
10373
10374
79189b84
JS
10375tracepoint_derived_probe::tracepoint_derived_probe (systemtap_session& s,
10376 dwflpp& dw, Dwarf_Die& func_die,
10377 const string& tracepoint_name,
10378 probe* base, probe_point* loc):
4c5d1300 10379 derived_probe (base, loc, true /* .components soon rewritten */),
79189b84 10380 sess (s), tracepoint_name (tracepoint_name)
56894e91 10381{
79189b84
JS
10382 // create synthetic probe point name; preserve condition
10383 vector<probe_point::component*> comps;
10384 comps.push_back (new probe_point::component (TOK_KERNEL));
10385 comps.push_back (new probe_point::component (TOK_TRACE, new literal_string (tracepoint_name)));
10386 this->sole_location()->components = comps;
10387
6fb70fb7
JS
10388 // fill out the available arguments in this tracepoint
10389 build_args(dw, func_die);
56894e91 10390
6fb70fb7
JS
10391 // determine which header defined this tracepoint
10392 string decl_file = dwarf_decl_file(&func_die);
b78a0fbb 10393 header = decl_file;
d4393459
FCE
10394
10395#if 0 /* This convention is not enforced. */
6fb70fb7
JS
10396 size_t header_pos = decl_file.rfind("trace/");
10397 if (header_pos == string::npos)
dc09353a 10398 throw SEMANTIC_ERROR ("cannot parse header location for tracepoint '"
6fb70fb7
JS
10399 + tracepoint_name + "' in '"
10400 + decl_file + "'");
10401 header = decl_file.substr(header_pos);
d4393459 10402#endif
56894e91 10403
6fb70fb7
JS
10404 // tracepoints from FOO_event_types.h should really be included from FOO.h
10405 // XXX can dwarf tell us the include hierarchy? it would be better to
10406 // ... walk up to see which one was directly included by tracequery.c
3c1b3d06 10407 // XXX: see also PR9993.
d4393459 10408 size_t header_pos = header.find("_event_types");
6fb70fb7
JS
10409 if (header_pos != string::npos)
10410 header.erase(header_pos, 12);
56894e91 10411
f8a968bc
JS
10412 // Now expand the local variables in the probe body
10413 tracepoint_var_expanding_visitor v (dw, name, args);
8b095b45 10414 v.replace (this->body);
a45664f4
JS
10415 for (unsigned i = 0; i < args.size(); i++)
10416 if (args[i].used)
10417 {
10418 vardecl* v = new vardecl;
10419 v->name = "__tracepoint_arg_" + args[i].name;
10420 v->tok = this->tok;
58701b78 10421 v->set_arity(0, this->tok);
a45664f4 10422 v->type = pe_long;
69aa668e 10423 v->synthetic = true;
a45664f4
JS
10424 this->locals.push_back (v);
10425 }
56894e91 10426
79189b84 10427 if (sess.verbose > 2)
ce0f6648 10428 clog << "tracepoint-based " << name << " tracepoint='" << tracepoint_name << "'" << endl;
79189b84 10429}
dc38c0ae 10430
56894e91 10431
f8a968bc 10432static bool
dcaa1a65 10433resolve_tracepoint_arg_type(tracepoint_arg& arg)
46b84a80 10434{
d19a9a82 10435 Dwarf_Die type;
dcaa1a65 10436 switch (dwarf_tag(&arg.type_die))
b20febf3 10437 {
f8a968bc
JS
10438 case DW_TAG_typedef:
10439 case DW_TAG_const_type:
10440 case DW_TAG_volatile_type:
c69af88f 10441 case DW_TAG_restrict_type:
f8a968bc 10442 // iterate on the referent type
3d1ad340 10443 return (dwarf_attr_die(&arg.type_die, DW_AT_type, &arg.type_die)
dcaa1a65 10444 && resolve_tracepoint_arg_type(arg));
f8a968bc 10445 case DW_TAG_base_type:
a52d2ac0 10446 case DW_TAG_enumeration_type:
f8a968bc 10447 // base types will simply be treated as script longs
dcaa1a65 10448 arg.isptr = false;
f8a968bc
JS
10449 return true;
10450 case DW_TAG_pointer_type:
dcaa1a65
JS
10451 // pointers can be treated as script longs,
10452 // and if we know their type, they can also be dereferenced
d19a9a82
JS
10453 type = arg.type_die;
10454 while (dwarf_attr_die(&arg.type_die, DW_AT_type, &arg.type_die))
10455 {
10456 // It still might be a non-type, e.g. const void,
10457 // so we need to strip away all qualifiers.
10458 int tag = dwarf_tag(&arg.type_die);
10459 if (tag != DW_TAG_typedef &&
10460 tag != DW_TAG_const_type &&
c69af88f
MW
10461 tag != DW_TAG_volatile_type &&
10462 tag != DW_TAG_restrict_type)
d19a9a82
JS
10463 {
10464 arg.isptr = true;
10465 break;
10466 }
10467 }
10468 if (!arg.isptr)
10469 arg.type_die = type;
ad370dcc
JS
10470 arg.typecast = "(intptr_t)";
10471 return true;
10472 case DW_TAG_structure_type:
10473 case DW_TAG_union_type:
10474 // for structs/unions which are passed by value, we turn it into
10475 // a pointer that can be dereferenced.
10476 arg.isptr = true;
10477 arg.typecast = "(intptr_t)&";
dcaa1a65 10478 return true;
f8a968bc
JS
10479 default:
10480 // should we consider other types too?
10481 return false;
b20febf3 10482 }
56894e91
JS
10483}
10484
10485
10486void
822a6a3d 10487tracepoint_derived_probe::build_args(dwflpp&, Dwarf_Die& func_die)
56894e91 10488{
6fb70fb7
JS
10489 Dwarf_Die arg;
10490 if (dwarf_child(&func_die, &arg) == 0)
10491 do
10492 if (dwarf_tag(&arg) == DW_TAG_formal_parameter)
10493 {
10494 // build a tracepoint_arg for this parameter
10495 tracepoint_arg tparg;
c60517ca 10496 tparg.name = dwarf_diename(&arg) ?: "";
56894e91 10497
6fb70fb7 10498 // read the type of this parameter
3d1ad340 10499 if (!dwarf_attr_die (&arg, DW_AT_type, &tparg.type_die)
f1c8f8a5 10500 || !dwarf_type_name(&tparg.type_die, tparg.c_type))
dc09353a 10501 throw SEMANTIC_ERROR (_F("cannot get type of parameter '%s' of tracepoint '%s'",
ce0f6648 10502 tparg.name.c_str(), tracepoint_name.c_str()));
a68f81a2 10503
dcaa1a65 10504 tparg.usable = resolve_tracepoint_arg_type(tparg);
6fb70fb7
JS
10505 args.push_back(tparg);
10506 if (sess.verbose > 4)
a52d2ac0
JS
10507 clog << _F("found parameter for tracepoint '%s': type:'%s' name:'%s' %s",
10508 tracepoint_name.c_str(), tparg.c_type.c_str(), tparg.name.c_str(),
10509 tparg.usable ? "ok" : "unavailable") << endl;
6fb70fb7
JS
10510 }
10511 while (dwarf_siblingof(&arg, &arg) == 0);
56894e91
JS
10512}
10513
dc38c0ae 10514void
d0bfd2ac 10515tracepoint_derived_probe::getargs(std::list<std::string> &arg_set) const
dc38c0ae 10516{
dcaa1a65
JS
10517 for (unsigned i = 0; i < args.size(); ++i)
10518 if (args[i].usable)
d0bfd2ac 10519 arg_set.push_back("$"+args[i].name+":"+args[i].c_type);
dc38c0ae
DS
10520}
10521
79189b84
JS
10522void
10523tracepoint_derived_probe::join_group (systemtap_session& s)
197a4d62 10524{
79189b84
JS
10525 if (! s.tracepoint_derived_probes)
10526 s.tracepoint_derived_probes = new tracepoint_derived_probe_group ();
10527 s.tracepoint_derived_probes->enroll (this);
10528}
e38d6504 10529
56894e91 10530
197a4d62 10531void
3e3bd7b6 10532tracepoint_derived_probe::print_dupe_stamp(ostream& o)
56894e91 10533{
3e3bd7b6
JS
10534 for (unsigned i = 0; i < args.size(); i++)
10535 if (args[i].used)
10536 o << "__tracepoint_arg_" << args[i].name << endl;
197a4d62 10537}
56894e91 10538
3e3bd7b6 10539
c9ccb642 10540static vector<string> tracepoint_extra_decls (systemtap_session& s, const string& header)
47dd066d 10541{
3c1b3d06
FCE
10542 vector<string> they_live;
10543 // PR 9993
10544 // XXX: may need this to be configurable
d4393459 10545 they_live.push_back ("#include <linux/skbuff.h>");
9e0cd21a
FCE
10546
10547 // PR11649: conditional extra header
10548 // for kvm tracepoints in 2.6.33ish
10549 if (s.kernel_config["CONFIG_KVM"] != string("")) {
d4393459
FCE
10550 they_live.push_back ("#include <linux/kvm_host.h>");
10551 }
10552
50b72692 10553 if (header.find("xfs") != string::npos && s.kernel_config["CONFIG_XFS_FS"] != string("")) {
d4393459 10554 they_live.push_back ("#define XFS_BIG_BLKNOS 1");
88637c31
FCE
10555 if (s.kernel_source_tree != "")
10556 they_live.push_back ("#include \"fs/xfs/xfs_types.h\""); // in kernel-source tree
d4393459
FCE
10557 they_live.push_back ("struct xfs_mount;");
10558 they_live.push_back ("struct xfs_inode;");
10559 they_live.push_back ("struct xfs_buf;");
10560 they_live.push_back ("struct xfs_bmbt_irec;");
c2cf1b87 10561 they_live.push_back ("struct xfs_trans;");
9e0cd21a 10562 }
d4393459 10563
50b72692 10564 if (header.find("nfs") != string::npos && s.kernel_config["CONFIG_NFSD"] != string("")) {
d4393459
FCE
10565 they_live.push_back ("struct rpc_task;");
10566 }
b64d65e2
FCE
10567 // RHEL6.3
10568 if (header.find("rpc") != string::npos && s.kernel_config["CONFIG_NFSD"] != string("")) {
10569 they_live.push_back ("struct rpc_clnt;");
10570 they_live.push_back ("struct rpc_wait_queue;");
10571 }
d4393459
FCE
10572
10573 they_live.push_back ("#include <asm/cputime.h>");
10574
c2cf1b87
FCE
10575 // linux 3.0
10576 they_live.push_back ("struct cpu_workqueue_struct;");
10577
50b72692 10578 if (header.find("ext4") != string::npos && s.kernel_config["CONFIG_EXT4_FS"] != string(""))
c2cf1b87
FCE
10579 if (s.kernel_source_tree != "")
10580 they_live.push_back ("#include \"fs/ext4/ext4.h\""); // in kernel-source tree
10581
d97d428e 10582 if (header.find("ext3") != string::npos)
50b72692
HP
10583 they_live.push_back ("struct ext3_reserve_window_node;");
10584
d97d428e
FCE
10585 if (header.find("workqueue") != string::npos)
10586 {
10587 they_live.push_back ("struct pool_workqueue;");
10588 they_live.push_back ("struct work_struct;");
10589 }
10590
10591 if (header.find("asoc") != string::npos)
10592 they_live.push_back ("struct snd_soc_dapm_path;");
10593
10594 if (header.find("9p") != string::npos)
10595 {
10596 they_live.push_back ("struct p9_client;");
10597 they_live.push_back ("struct p9_fcall;");
10598 }
10599
10600 if (header.find("bcache") != string::npos)
10601 {
10602 they_live.push_back ("struct bkey;");
10603 they_live.push_back ("struct btree;");
10604 they_live.push_back ("struct cache_set;");
10605 they_live.push_back ("struct cache;");
10606 }
10607
10608 if (header.find("f2fs") != string::npos)
10609 {
10610 // cannot get fs/f2fs/f2fs.h #included
10611 they_live.push_back ("typedef u32 block_t;");
10612 they_live.push_back ("typedef u32 nid_t;");
10613 }
10614
10615 if (header.find("radeon") != string::npos)
10616 they_live.push_back ("struct radeon_bo;");
10617
10618 // argh, 3.11, i915_trace.h -> i915_drv.h -> i915_reg.h without -I
10619 // also brcms_trace_events.h -> ... -> "types.h"
10620 // XXX: need a way to add a temporary -I flag
10621
10622 if (header.find("/ath/") != string::npos)
10623 they_live.push_back ("struct ath5k_hw;");
10624
10625
3c1b3d06
FCE
10626 return they_live;
10627}
47dd066d
WC
10628
10629
10630void
79189b84 10631tracepoint_derived_probe_group::emit_module_decls (systemtap_session& s)
47dd066d 10632{
79189b84
JS
10633 if (probes.empty())
10634 return;
47dd066d 10635
96b030fe 10636 s.op->newline() << "/* ---- tracepoint probes ---- */";
3ef9830a 10637 s.op->newline() << "#include <linux/stp_tracepoint.h>" << endl;
96b030fe 10638 s.op->newline();
79189b84 10639
47dd066d 10640
a4b9c3b3
FCE
10641 // We create a MODULE_aux_N.c file for each tracepoint header, to allow them
10642 // to be separately compiled. That's because kernel tracepoint headers sometimes
10643 // conflict. PR13155.
10644
10645 map<string,translator_output*> per_header_aux;
10646 // GC NB: the translator_output* structs are owned/retained by the systemtap_session.
47dd066d 10647
6fb70fb7
JS
10648 for (unsigned i = 0; i < probes.size(); ++i)
10649 {
10650 tracepoint_derived_probe *p = probes[i];
75ae2ec9 10651 string header = p->header;
5f73a260 10652
a4b9c3b3
FCE
10653 // We cache the auxiliary output files on a per-header basis. We don't
10654 // need one aux file per tracepoint, only one per tracepoint-header.
10655 translator_output *tpop = per_header_aux[header];
10656 if (tpop == 0)
10657 {
10658 tpop = s.op_create_auxiliary();
10659 per_header_aux[header] = tpop;
10660
10661 // PR9993: Add extra headers to work around undeclared types in individual
10662 // include/trace/foo.h files
10663 const vector<string>& extra_decls = tracepoint_extra_decls (s, header);
10664 for (unsigned z=0; z<extra_decls.size(); z++)
10665 tpop->newline() << extra_decls[z] << "\n";
720c435f 10666
a4b9c3b3
FCE
10667 // strip include/ substring, the same way as done in get_tracequery_module()
10668 size_t root_pos = header.rfind("include/");
10669 header = ((root_pos != string::npos) ? header.substr(root_pos + 8) : header);
bfffa443 10670
3ef9830a 10671 tpop->newline() << "#include <linux/stp_tracepoint.h>" << endl;
a4b9c3b3 10672 tpop->newline() << "#include <" << header << ">";
a4b9c3b3
FCE
10673 }
10674
720c435f
JS
10675 // collect the args that are actually in use
10676 vector<const tracepoint_arg*> used_args;
6fb70fb7 10677 for (unsigned j = 0; j < p->args.size(); ++j)
720c435f
JS
10678 if (p->args[j].used)
10679 used_args.push_back(&p->args[j]);
10680
3ef9830a
JS
10681 // forward-declare the generated-side tracepoint callback, and define the
10682 // generated-side tracepoint callback in the main translator-output
10683 string enter_real_fn = "enter_real_tracepoint_probe_" + lex_cast(i);
720c435f 10684 if (used_args.empty())
6fb70fb7 10685 {
3ef9830a
JS
10686 tpop->newline() << "STP_TRACE_ENTER_REAL_NOARGS(" << enter_real_fn << ");";
10687 s.op->newline() << "STP_TRACE_ENTER_REAL_NOARGS(" << enter_real_fn << ")";
6fb70fb7 10688 }
3ef9830a 10689 else
a4b9c3b3 10690 {
3ef9830a
JS
10691 tpop->newline() << "STP_TRACE_ENTER_REAL(" << enter_real_fn;
10692 s.op->newline() << "STP_TRACE_ENTER_REAL(" << enter_real_fn;
10693 s.op->indent(2);
10694 for (unsigned j = 0; j < used_args.size(); ++j)
10695 {
10696 tpop->line() << ", int64_t";
10697 s.op->newline() << ", int64_t __tracepoint_arg_" << used_args[j]->name;
10698 }
10699 tpop->line() << ");";
10700 s.op->newline() << ")";
10701 s.op->indent(-2);
6fb70fb7 10702 }
3ef9830a 10703 s.op->newline() << "{";
7c3e97f4 10704 s.op->newline(1) << "const struct stap_probe * const probe = "
faea5e16 10705 << common_probe_init (p) << ";";
71db462b 10706 common_probe_entryfn_prologue (s, "STAP_SESSION_RUNNING", "probe",
cda141c2 10707 "stp_probe_type_tracepoint");
6dceb5c9 10708 s.op->newline() << "c->ips.tracepoint_name = "
c12d974f
FCE
10709 << lex_cast_qstring (p->tracepoint_name)
10710 << ";";
720c435f
JS
10711 for (unsigned j = 0; j < used_args.size(); ++j)
10712 {
10713 s.op->newline() << "c->probe_locals." << p->name
0386bbcf 10714 << "." + s.up->c_localname("__tracepoint_arg_" + used_args[j]->name)
720c435f
JS
10715 << " = __tracepoint_arg_" << used_args[j]->name << ";";
10716 }
26e63673 10717 s.op->newline() << "(*probe->ph) (c);";
f887a8c9 10718 common_probe_entryfn_epilogue (s, true);
6fb70fb7 10719 s.op->newline(-1) << "}";
47dd066d 10720
a4b9c3b3 10721 // define the real tracepoint callback function
3ef9830a
JS
10722 string enter_fn = "enter_tracepoint_probe_" + lex_cast(i);
10723 if (p->args.empty())
10724 tpop->newline() << "static STP_TRACE_ENTER_NOARGS(" << enter_fn << ")";
10725 else
a4b9c3b3 10726 {
3ef9830a
JS
10727 tpop->newline() << "static STP_TRACE_ENTER(" << enter_fn;
10728 s.op->indent(2);
10729 for (unsigned j = 0; j < p->args.size(); ++j)
10730 {
10731 tpop->newline() << ", " << p->args[j].c_type
10732 << " __tracepoint_arg_" << p->args[j].name;
10733 }
10734 tpop->newline() << ")";
10735 s.op->indent(-2);
a4b9c3b3 10736 }
3ef9830a
JS
10737 tpop->newline() << "{";
10738 tpop->newline(1) << enter_real_fn << "(";
720c435f
JS
10739 tpop->indent(2);
10740 for (unsigned j = 0; j < used_args.size(); ++j)
10741 {
10742 if (j > 0)
10743 tpop->line() << ", ";
10744 tpop->newline() << "(int64_t)" << used_args[j]->typecast
10745 << "__tracepoint_arg_" << used_args[j]->name;
10746 }
10747 tpop->newline() << ");";
10748 tpop->newline(-3) << "}";
a4b9c3b3
FCE
10749
10750
96b030fe 10751 // emit normalized registration functions
720c435f 10752 tpop->newline() << "int register_tracepoint_probe_" << i << "(void) {";
3ef9830a
JS
10753 tpop->newline(1) << "return STP_TRACE_REGISTER(" << p->tracepoint_name
10754 << ", " << enter_fn << ");";
a4b9c3b3 10755 tpop->newline(-1) << "}";
47dd066d 10756
86758d5f
JS
10757 // NB: we're not prepared to deal with unreg failures. However, failures
10758 // can only occur if the tracepoint doesn't exist (yet?), or if we
10759 // weren't even registered. The former should be OKed by the initial
10760 // registration call, and the latter is safe to ignore.
720c435f 10761 tpop->newline() << "void unregister_tracepoint_probe_" << i << "(void) {";
3ef9830a
JS
10762 tpop->newline(1) << "(void) STP_TRACE_UNREGISTER(" << p->tracepoint_name
10763 << ", " << enter_fn << ");";
a4b9c3b3
FCE
10764 tpop->newline(-1) << "}";
10765 tpop->newline();
5f73a260 10766
720c435f
JS
10767 // declare normalized registration functions
10768 s.op->newline() << "int register_tracepoint_probe_" << i << "(void);";
10769 s.op->newline() << "void unregister_tracepoint_probe_" << i << "(void);";
5f73a260 10770
a4b9c3b3 10771 tpop->assert_0_indent();
af304783
DS
10772 }
10773
96b030fe
JS
10774 // emit an array of registration functions for easy init/shutdown
10775 s.op->newline() << "static struct stap_tracepoint_probe {";
10776 s.op->newline(1) << "int (*reg)(void);";
86758d5f 10777 s.op->newline(0) << "void (*unreg)(void);";
96b030fe
JS
10778 s.op->newline(-1) << "} stap_tracepoint_probes[] = {";
10779 s.op->indent(1);
10780 for (unsigned i = 0; i < probes.size(); ++i)
10781 {
10782 s.op->newline () << "{";
10783 s.op->line() << " .reg=&register_tracepoint_probe_" << i << ",";
10784 s.op->line() << " .unreg=&unregister_tracepoint_probe_" << i;
10785 s.op->line() << " },";
10786 }
10787 s.op->newline(-1) << "};";
10788 s.op->newline();
47dd066d
WC
10789}
10790
10791
79189b84
JS
10792void
10793tracepoint_derived_probe_group::emit_module_init (systemtap_session &s)
47dd066d 10794{
79189b84
JS
10795 if (probes.size () == 0)
10796 return;
47dd066d 10797
79189b84 10798 s.op->newline() << "/* init tracepoint probes */";
96b030fe
JS
10799 s.op->newline() << "for (i=0; i<" << probes.size() << "; i++) {";
10800 s.op->newline(1) << "rc = stap_tracepoint_probes[i].reg();";
10801 s.op->newline() << "if (rc) {";
10802 s.op->newline(1) << "for (j=i-1; j>=0; j--)"; // partial rollback
10803 s.op->newline(1) << "stap_tracepoint_probes[j].unreg();";
10804 s.op->newline(-1) << "break;"; // don't attempt to register any more probes
10805 s.op->newline(-1) << "}";
10806 s.op->newline(-1) << "}";
47dd066d 10807
bc9a523d
FCE
10808 // This would be technically proper (on those autoconf-detectable
10809 // kernels that include this function in tracepoint.h), however we
10810 // already make several calls to synchronze_sched() during our
10811 // shutdown processes.
47dd066d 10812
bc9a523d
FCE
10813 // s.op->newline() << "if (rc)";
10814 // s.op->newline(1) << "tracepoint_synchronize_unregister();";
10815 // s.op->indent(-1);
79189b84 10816}
47dd066d
WC
10817
10818
79189b84
JS
10819void
10820tracepoint_derived_probe_group::emit_module_exit (systemtap_session& s)
47dd066d 10821{
79189b84
JS
10822 if (probes.empty())
10823 return;
47dd066d 10824
96b030fe
JS
10825 s.op->newline() << "/* deregister tracepoint probes */";
10826 s.op->newline() << "for (i=0; i<" << probes.size() << "; i++)";
10827 s.op->newline(1) << "stap_tracepoint_probes[i].unreg();";
10828 s.op->indent(-1);
47dd066d 10829
bc9a523d 10830 // Not necessary: see above.
47dd066d 10831
bc9a523d 10832 // s.op->newline() << "tracepoint_synchronize_unregister();";
79189b84 10833}
b20febf3 10834
47dd066d 10835
75ead1f7 10836struct tracepoint_query : public base_query
47dd066d 10837{
75ead1f7
JS
10838 tracepoint_query(dwflpp & dw, const string & tracepoint,
10839 probe * base_probe, probe_point * base_loc,
10840 vector<derived_probe *> & results):
10841 base_query(dw, "*"), tracepoint(tracepoint),
10842 base_probe(base_probe), base_loc(base_loc),
10843 results(results) {}
47dd066d 10844
75ead1f7 10845 const string& tracepoint;
47dd066d 10846
75ead1f7
JS
10847 probe * base_probe;
10848 probe_point * base_loc;
10849 vector<derived_probe *> & results;
f982c59b 10850 set<string> probed_names;
47dd066d 10851
75ead1f7
JS
10852 void handle_query_module();
10853 int handle_query_cu(Dwarf_Die * cudie);
10854 int handle_query_func(Dwarf_Die * func);
822a6a3d 10855 void query_library (const char *) {}
576eaefe 10856 void query_plt (const char *entry, size_t addr) {}
b20febf3 10857
5c378838 10858 static int tracepoint_query_cu (Dwarf_Die * cudie, tracepoint_query * q);
7d007451 10859 static int tracepoint_query_func (Dwarf_Die * func, tracepoint_query * q);
75ead1f7 10860};
47dd066d
WC
10861
10862
10863void
75ead1f7 10864tracepoint_query::handle_query_module()
47dd066d 10865{
75ead1f7 10866 // look for the tracepoints in each CU
337b7c44 10867 dw.iterate_over_cus(tracepoint_query_cu, this, false);
47dd066d
WC
10868}
10869
10870
75ead1f7
JS
10871int
10872tracepoint_query::handle_query_cu(Dwarf_Die * cudie)
47dd066d 10873{
75ead1f7 10874 dw.focus_on_cu (cudie);
5f52fafe 10875 dw.mod_info->get_symtab();
47dd066d 10876
75ead1f7
JS
10877 // look at each function to see if it's a tracepoint
10878 string function = "stapprobe_" + tracepoint;
10879 return dw.iterate_over_functions (tracepoint_query_func, this, function);
47dd066d
WC
10880}
10881
10882
75ead1f7
JS
10883int
10884tracepoint_query::handle_query_func(Dwarf_Die * func)
47dd066d 10885{
75ead1f7 10886 dw.focus_on_function (func);
47dd066d 10887
60d98537 10888 assert(startswith(dw.function_name, "stapprobe_"));
75ead1f7 10889 string tracepoint_instance = dw.function_name.substr(10);
f982c59b
JS
10890
10891 // check for duplicates -- sometimes tracepoint headers may be indirectly
10892 // included in more than one of our tracequery modules.
10893 if (!probed_names.insert(tracepoint_instance).second)
10894 return DWARF_CB_OK;
10895
79189b84
JS
10896 derived_probe *dp = new tracepoint_derived_probe (dw.sess, dw, *func,
10897 tracepoint_instance,
10898 base_probe, base_loc);
10899 results.push_back (dp);
75ead1f7 10900 return DWARF_CB_OK;
47dd066d
WC
10901}
10902
10903
75ead1f7 10904int
5c378838 10905tracepoint_query::tracepoint_query_cu (Dwarf_Die * cudie, tracepoint_query * q)
47dd066d 10906{
85007c04 10907 if (pending_interrupts) return DWARF_CB_ABORT;
75ead1f7 10908 return q->handle_query_cu(cudie);
47dd066d
WC
10909}
10910
10911
75ead1f7 10912int
7d007451 10913tracepoint_query::tracepoint_query_func (Dwarf_Die * func, tracepoint_query * q)
47dd066d 10914{
85007c04 10915 if (pending_interrupts) return DWARF_CB_ABORT;
75ead1f7 10916 return q->handle_query_func(func);
47dd066d
WC
10917}
10918
10919
0a6f5a3f 10920struct tracepoint_builder: public derived_probe_builder
47dd066d 10921{
0a6f5a3f
JS
10922private:
10923 dwflpp *dw;
10924 bool init_dw(systemtap_session& s);
c9ccb642
FCE
10925 void get_tracequery_modules(systemtap_session& s,
10926 const vector<string>& headers,
10927 vector<string>& modules);
47dd066d 10928
0a6f5a3f 10929public:
47dd066d 10930
0a6f5a3f
JS
10931 tracepoint_builder(): dw(0) {}
10932 ~tracepoint_builder() { delete dw; }
47dd066d 10933
0a6f5a3f
JS
10934 void build_no_more (systemtap_session& s)
10935 {
10936 if (dw && s.verbose > 3)
b530b5b3 10937 clog << _("tracepoint_builder releasing dwflpp") << endl;
0a6f5a3f
JS
10938 delete dw;
10939 dw = NULL;
435f53a7
FCE
10940
10941 delete_session_module_cache (s);
0a6f5a3f 10942 }
47dd066d 10943
0a6f5a3f
JS
10944 void build(systemtap_session& s,
10945 probe *base, probe_point *location,
10946 literal_map_t const& parameters,
10947 vector<derived_probe*>& finished_results);
10948};
47dd066d 10949
47dd066d 10950
c9ccb642 10951
2a0e62a8 10952// Create (or cache) one or more tracequery .o modules, based upon the
c9ccb642
FCE
10953// tracepoint-related header files given. Return the generated or cached
10954// modules[].
10955
10956void
10957tracepoint_builder::get_tracequery_modules(systemtap_session& s,
10958 const vector<string>& headers,
10959 vector<string>& modules)
0a6f5a3f 10960{
c95eddf7 10961 if (s.verbose > 2)
55e50c24 10962 {
ce0f6648 10963 clog << _F("Pass 2: getting a tracepoint query for %zu headers: ", headers.size()) << endl;
55e50c24
JS
10964 for (size_t i = 0; i < headers.size(); ++i)
10965 clog << " " << headers[i] << endl;
10966 }
c95eddf7 10967
2a0e62a8
JS
10968 map<string,string> headers_cache_obj; // header name -> cache/.../tracequery_hash.o file name
10969 // Map the headers to cache .o names. Note that this has side-effects of
c9ccb642
FCE
10970 // creating the $SYSTEMTAP_DIR/.cache/XX/... directory and the hash-log file,
10971 // so we prefer not to repeat this.
10972 vector<string> uncached_headers;
10973 for (size_t i=0; i<headers.size(); i++)
2a0e62a8 10974 headers_cache_obj[headers[i]] = find_tracequery_hash(s, headers[i]);
c9ccb642
FCE
10975
10976 // They may be in the cache already.
10977 if (s.use_cache && !s.poison_cache)
10978 for (size_t i=0; i<headers.size(); i++)
10979 {
10980 // see if the cached module exists
2a0e62a8 10981 const string& tracequery_path = headers_cache_obj[headers[i]];
c9ccb642
FCE
10982 if (!tracequery_path.empty() && file_exists(tracequery_path))
10983 {
10984 if (s.verbose > 2)
10985 clog << _F("Pass 2: using cached %s", tracequery_path.c_str()) << endl;
47dd066d 10986
c252fca2
JS
10987 // an empty file is a cached failure
10988 if (get_file_size(tracequery_path) > 0)
10989 modules.push_back (tracequery_path);
c9ccb642
FCE
10990 }
10991 else
10992 uncached_headers.push_back(headers[i]);
10993 }
10994 else
10995 uncached_headers = headers;
f982c59b 10996
c9ccb642
FCE
10997 // If we have nothing left to search for, quit
10998 if (uncached_headers.empty()) return;
55e50c24 10999
c9ccb642 11000 map<string,string> headers_tracequery_src; // header -> C-source code mapping
55e50c24 11001
c9ccb642
FCE
11002 // We could query several subsets of headers[] to make this go
11003 // faster, but let's KISS and do one at a time.
11004 for (size_t i=0; i<uncached_headers.size(); i++)
55e50c24 11005 {
c9ccb642
FCE
11006 const string& header = uncached_headers[i];
11007
11008 // create a tracequery source file
11009 ostringstream osrc;
11010
11011 // PR9993: Add extra headers to work around undeclared types in individual
11012 // include/trace/foo.h files
11013 vector<string> short_decls = tracepoint_extra_decls(s, header);
11014
11015 // add each requested tracepoint header
75ae2ec9 11016 size_t root_pos = header.rfind("include/");
832f100d 11017 short_decls.push_back(string("#include <") +
75ae2ec9 11018 ((root_pos != string::npos) ? header.substr(root_pos + 8) : header) +
d4393459 11019 string(">"));
f982c59b 11020
c9ccb642
FCE
11021 osrc << "#ifdef CONFIG_TRACEPOINTS" << endl;
11022 osrc << "#include <linux/tracepoint.h>" << endl;
832f100d
JS
11023
11024 // the kernel has changed this naming a few times, previously TPPROTO,
11025 // TP_PROTO, TPARGS, TP_ARGS, etc. so let's just dupe the latest.
11026 osrc << "#ifndef PARAMS" << endl;
11027 osrc << "#define PARAMS(args...) args" << endl;
11028 osrc << "#endif" << endl;
11029
c9ccb642
FCE
11030 // override DECLARE_TRACE to synthesize probe functions for us
11031 osrc << "#undef DECLARE_TRACE" << endl;
11032 osrc << "#define DECLARE_TRACE(name, proto, args) \\" << endl;
11033 osrc << " void stapprobe_##name(proto) {}" << endl;
832f100d 11034
c9ccb642
FCE
11035 // 2.6.35 added the NOARGS variant, but it's the same for us
11036 osrc << "#undef DECLARE_TRACE_NOARGS" << endl;
11037 osrc << "#define DECLARE_TRACE_NOARGS(name) \\" << endl;
11038 osrc << " DECLARE_TRACE(name, void, )" << endl;
832f100d
JS
11039
11040 // 2.6.38 added the CONDITION variant, which can also just redirect
11041 osrc << "#undef DECLARE_TRACE_CONDITION" << endl;
11042 osrc << "#define DECLARE_TRACE_CONDITION(name, proto, args, cond) \\" << endl;
11043 osrc << " DECLARE_TRACE(name, PARAMS(proto), PARAMS(args))" << endl;
11044
c9ccb642
FCE
11045 // older tracepoints used DEFINE_TRACE, so redirect that too
11046 osrc << "#undef DEFINE_TRACE" << endl;
11047 osrc << "#define DEFINE_TRACE(name, proto, args) \\" << endl;
832f100d
JS
11048 osrc << " DECLARE_TRACE(name, PARAMS(proto), PARAMS(args))" << endl;
11049
c9ccb642
FCE
11050 // add the specified decls/#includes
11051 for (unsigned z=0; z<short_decls.size(); z++)
11052 osrc << "#undef TRACE_INCLUDE_FILE\n"
11053 << "#undef TRACE_INCLUDE_PATH\n"
11054 << short_decls[z] << "\n";
11055
11056 // finish up the module source
11057 osrc << "#endif /* CONFIG_TRACEPOINTS */" << endl;
47dd066d 11058
c9ccb642
FCE
11059 // save the source file away
11060 headers_tracequery_src[header] = osrc.str();
55e50c24 11061 }
f982c59b 11062
c9ccb642 11063 // now build them all together
2a0e62a8 11064 map<string,string> tracequery_objs = make_tracequeries(s, headers_tracequery_src);
47dd066d 11065
c9ccb642 11066 // now plop them into the cache
b278033a 11067 if (s.use_cache)
c9ccb642
FCE
11068 for (size_t i=0; i<uncached_headers.size(); i++)
11069 {
11070 const string& header = uncached_headers[i];
2a0e62a8
JS
11071 const string& tracequery_obj = tracequery_objs[header];
11072 const string& tracequery_path = headers_cache_obj[header];
11073 if (tracequery_obj !="" && file_exists(tracequery_obj))
c9ccb642 11074 {
2a0e62a8 11075 copy_file(tracequery_obj, tracequery_path, s.verbose > 2);
c9ccb642
FCE
11076 modules.push_back (tracequery_path);
11077 }
c252fca2
JS
11078 else
11079 // cache an empty file for failures
11080 copy_file("/dev/null", tracequery_path, s.verbose > 2);
c9ccb642 11081 }
f982c59b
JS
11082}
11083
11084
d4393459 11085
f982c59b
JS
11086bool
11087tracepoint_builder::init_dw(systemtap_session& s)
11088{
11089 if (dw != NULL)
11090 return true;
11091
11092 vector<string> tracequery_modules;
55e50c24 11093 vector<string> system_headers;
f982c59b
JS
11094
11095 glob_t trace_glob;
d4393459
FCE
11096
11097 // find kernel_source_tree
11098 if (s.kernel_source_tree == "")
f982c59b 11099 {
d4393459 11100 unsigned found;
ccf2c922 11101 Dwfl *dwfl = setup_dwfl_kernel ("kernel", &found, s);
d4393459
FCE
11102 if (found)
11103 {
11104 Dwarf_Die *cudie = 0;
11105 Dwarf_Addr bias;
11106 while ((cudie = dwfl_nextcu (dwfl, cudie, &bias)) != NULL)
11107 {
e19ebcf7 11108 assert_no_interrupts();
d4393459
FCE
11109 Dwarf_Attribute attr;
11110 const char* name = dwarf_formstring (dwarf_attr (cudie, DW_AT_comp_dir, &attr));
36d65b45 11111 if (name)
d4393459 11112 {
36d65b45
JS
11113 // check that the path actually exists locally before we try to use it
11114 if (file_exists(name))
11115 {
11116 if (s.verbose > 2)
11117 clog << _F("Located kernel source tree (DW_AT_comp_dir) at '%s'", name) << endl;
11118 s.kernel_source_tree = name;
11119 }
11120 else
11121 {
11122 if (s.verbose > 2)
11123 clog << _F("Ignoring inaccessible kernel source tree (DW_AT_comp_dir) at '%s'", name) << endl;
11124 }
61f1a63b 11125
d4393459
FCE
11126 break; // skip others; modern Kbuild uses same comp_dir for them all
11127 }
11128 }
11129 }
44392d73 11130 dwfl_end (dwfl);
d4393459
FCE
11131 }
11132
11133 // prefixes
11134 vector<string> glob_prefixes;
11135 glob_prefixes.push_back (s.kernel_build_tree);
11136 if (s.kernel_source_tree != "")
11137 glob_prefixes.push_back (s.kernel_source_tree);
11138
11139 // suffixes
11140 vector<string> glob_suffixes;
11141 glob_suffixes.push_back("include/trace/events/*.h");
11142 glob_suffixes.push_back("include/trace/*.h");
d97d428e 11143 glob_suffixes.push_back("include/ras/*_event.h");
d4393459 11144 glob_suffixes.push_back("arch/x86/kvm/*trace.h");
d97d428e
FCE
11145 glob_suffixes.push_back("arch/x86/kernel/*trace.h");
11146 glob_suffixes.push_back("arch/*/include/asm/trace*.h");
11147 glob_suffixes.push_back("arch/*/include/asm/trace/*.h");
f89ff3e2 11148 glob_suffixes.push_back("fs/xfs/linux-*/xfs_tr*.h");
d97d428e
FCE
11149 glob_suffixes.push_back("fs/*/*trace*.h");
11150 glob_suffixes.push_back("net/*/*trace*.h");
11151 glob_suffixes.push_back("sound/pci/hda/*_trace.h");
11152 glob_suffixes.push_back("drivers/gpu/drm/*_trace.h");
11153 glob_suffixes.push_back("drivers/gpu/drm/*/*_trace.h");
11154 glob_suffixes.push_back("drivers/net/wireless/*/*/*trace*.h");
11155 glob_suffixes.push_back("drivers/usb/host/*trace*.h");
11156
11157 // see also tracepoint_extra_decls above
d4393459
FCE
11158
11159 // compute cartesian product
11160 vector<string> globs;
11161 for (unsigned i=0; i<glob_prefixes.size(); i++)
11162 for (unsigned j=0; j<glob_suffixes.size(); j++)
11163 globs.push_back (glob_prefixes[i]+string("/")+glob_suffixes[j]);
11164
8aa43b8d 11165 set<string> duped_headers;
d4393459
FCE
11166 for (unsigned z = 0; z < globs.size(); z++)
11167 {
11168 string glob_str = globs[z];
11169 if (s.verbose > 3)
b530b5b3 11170 clog << _("Checking tracepoint glob ") << glob_str << endl;
d4393459 11171
067cc66f
CM
11172 int r = glob(glob_str.c_str(), 0, NULL, &trace_glob);
11173 if (r == GLOB_NOSPACE || r == GLOB_ABORTED)
11174 throw runtime_error("Error globbing tracepoint");
11175
f982c59b
JS
11176 for (unsigned i = 0; i < trace_glob.gl_pathc; ++i)
11177 {
11178 string header(trace_glob.gl_pathv[i]);
11179
11180 // filter out a few known "internal-only" headers
60d98537
JS
11181 if (endswith(header, "/define_trace.h") ||
11182 endswith(header, "/ftrace.h") ||
11183 endswith(header, "/trace_events.h") ||
11184 endswith(header, "_event_types.h"))
f982c59b
JS
11185 continue;
11186
b1966849
FCE
11187 // With headers now plopped under arch/FOO/include/asm/*,
11188 // the following logic miss some tracepoints.
11189#if 0
8aa43b8d
JS
11190 // skip identical headers from the build and source trees.
11191 size_t root_pos = header.rfind("include/");
11192 if (root_pos != string::npos &&
11193 !duped_headers.insert(header.substr(root_pos + 8)).second)
11194 continue;
b1966849 11195#endif
8aa43b8d 11196
55e50c24 11197 system_headers.push_back(header);
f982c59b
JS
11198 }
11199 globfree(&trace_glob);
11200 }
11201
c9ccb642
FCE
11202 // Build tracequery modules
11203 get_tracequery_modules(s, system_headers, tracequery_modules);
55e50c24 11204
f982c59b
JS
11205 // TODO: consider other sources of tracepoint headers too, like from
11206 // a command-line parameter or some environment or .systemtaprc
47dd066d 11207
59c11f91 11208 dw = new dwflpp(s, tracequery_modules, true);
0a6f5a3f
JS
11209 return true;
11210}
47dd066d 11211
0a6f5a3f
JS
11212void
11213tracepoint_builder::build(systemtap_session& s,
11214 probe *base, probe_point *location,
11215 literal_map_t const& parameters,
11216 vector<derived_probe*>& finished_results)
11217{
11218 if (!init_dw(s))
11219 return;
47dd066d 11220
75ead1f7
JS
11221 string tracepoint;
11222 assert(get_param (parameters, TOK_TRACE, tracepoint));
47dd066d 11223
75ead1f7 11224 tracepoint_query q(*dw, tracepoint, base, location, finished_results);
d906ab9d 11225 unsigned results_pre = finished_results.size();
06de3a04 11226 dw->iterate_over_modules<base_query>(&query_module, &q);
d906ab9d
JL
11227 unsigned results_post = finished_results.size();
11228
11229 // Did we fail to find a match? Let's suggest something!
11230 if (results_pre == results_post)
11231 {
11232 size_t pos;
11233 string sugs = suggest_dwarf_functions(s, q.visited_modules, tracepoint);
11234 while ((pos = sugs.find("stapprobe_")) != string::npos)
11235 sugs.erase(pos, string("stapprobe_").size());
11236 if (!sugs.empty())
ece93f53
JL
11237 throw SEMANTIC_ERROR (_NF("no match (similar tracepoint: %s)",
11238 "no match (similar tracepoints: %s)",
d906ab9d
JL
11239 sugs.find(',') == string::npos,
11240 sugs.c_str()));
11241 }
47dd066d 11242}
47dd066d 11243
e6fe60e7 11244
b55bc428 11245// ------------------------------------------------------------------------
bd2b1e68 11246// Standard tapset registry.
b55bc428
FCE
11247// ------------------------------------------------------------------------
11248
7a053d3b 11249void
f8220a7b 11250register_standard_tapsets(systemtap_session & s)
b55bc428 11251{
47e0478e 11252 register_tapset_been(s);
93646f4d 11253 register_tapset_itrace(s);
dd0e4fa7 11254 register_tapset_mark(s);
7a212aa8 11255 register_tapset_procfs(s);
912e8c59 11256 register_tapset_timers(s);
8d9609f5 11257 register_tapset_netfilter(s);
b84779a5 11258 register_tapset_utrace(s);
b98a8d73 11259
7a24d422 11260 // dwarf-based kprobe/uprobe parts
c4ce66a1 11261 dwarf_derived_probe::register_patterns(s);
30a279be 11262
888af770
FCE
11263 // XXX: user-space starter set
11264 s.pattern_root->bind_num(TOK_PROCESS)
11265 ->bind_num(TOK_STATEMENT)->bind(TOK_ABSOLUTE)
f66bb29a 11266 ->bind_privilege(pr_all)
888af770
FCE
11267 ->bind(new uprobe_builder ());
11268 s.pattern_root->bind_num(TOK_PROCESS)
11269 ->bind_num(TOK_STATEMENT)->bind(TOK_ABSOLUTE)->bind(TOK_RETURN)
f66bb29a 11270 ->bind_privilege(pr_all)
888af770
FCE
11271 ->bind(new uprobe_builder ());
11272
0a6f5a3f
JS
11273 // kernel tracepoint probes
11274 s.pattern_root->bind(TOK_KERNEL)->bind_str(TOK_TRACE)
11275 ->bind(new tracepoint_builder());
11276
e6fe60e7
AM
11277 // Kprobe based probe
11278 s.pattern_root->bind(TOK_KPROBE)->bind_str(TOK_FUNCTION)
11279 ->bind(new kprobe_builder());
3c57fe1f
JS
11280 s.pattern_root->bind(TOK_KPROBE)->bind_str(TOK_FUNCTION)->bind(TOK_CALL)
11281 ->bind(new kprobe_builder());
e6fe60e7
AM
11282 s.pattern_root->bind(TOK_KPROBE)->bind_str(TOK_MODULE)
11283 ->bind_str(TOK_FUNCTION)->bind(new kprobe_builder());
3c57fe1f
JS
11284 s.pattern_root->bind(TOK_KPROBE)->bind_str(TOK_MODULE)
11285 ->bind_str(TOK_FUNCTION)->bind(TOK_CALL)->bind(new kprobe_builder());
e6fe60e7
AM
11286 s.pattern_root->bind(TOK_KPROBE)->bind_str(TOK_FUNCTION)->bind(TOK_RETURN)
11287 ->bind(new kprobe_builder());
b6371390
JS
11288 s.pattern_root->bind(TOK_KPROBE)->bind_str(TOK_FUNCTION)->bind(TOK_RETURN)
11289 ->bind_num(TOK_MAXACTIVE)->bind(new kprobe_builder());
e6fe60e7
AM
11290 s.pattern_root->bind(TOK_KPROBE)->bind_str(TOK_MODULE)
11291 ->bind_str(TOK_FUNCTION)->bind(TOK_RETURN)->bind(new kprobe_builder());
b6371390
JS
11292 s.pattern_root->bind(TOK_KPROBE)->bind_str(TOK_MODULE)
11293 ->bind_str(TOK_FUNCTION)->bind(TOK_RETURN)
11294 ->bind_num(TOK_MAXACTIVE)->bind(new kprobe_builder());
e6fe60e7
AM
11295 s.pattern_root->bind(TOK_KPROBE)->bind_num(TOK_STATEMENT)
11296 ->bind(TOK_ABSOLUTE)->bind(new kprobe_builder());
dd225250
PS
11297
11298 //Hwbkpt based probe
b47f3a55
FCE
11299 // NB: we formerly registered the probe point types only if the kernel configuration
11300 // allowed it. However, we get better error messages if we allow probes to resolve.
11301 s.pattern_root->bind(TOK_KERNEL)->bind_num(TOK_HWBKPT)
11302 ->bind(TOK_HWBKPT_WRITE)->bind(new hwbkpt_builder());
11303 s.pattern_root->bind(TOK_KERNEL)->bind_str(TOK_HWBKPT)
11304 ->bind(TOK_HWBKPT_WRITE)->bind(new hwbkpt_builder());
11305 s.pattern_root->bind(TOK_KERNEL)->bind_num(TOK_HWBKPT)
11306 ->bind(TOK_HWBKPT_RW)->bind(new hwbkpt_builder());
11307 s.pattern_root->bind(TOK_KERNEL)->bind_str(TOK_HWBKPT)
11308 ->bind(TOK_HWBKPT_RW)->bind(new hwbkpt_builder());
11309 s.pattern_root->bind(TOK_KERNEL)->bind_num(TOK_HWBKPT)
11310 ->bind_num(TOK_LENGTH)->bind(TOK_HWBKPT_WRITE)->bind(new hwbkpt_builder());
11311 s.pattern_root->bind(TOK_KERNEL)->bind_num(TOK_HWBKPT)
11312 ->bind_num(TOK_LENGTH)->bind(TOK_HWBKPT_RW)->bind(new hwbkpt_builder());
11313 // length supported with address only, not symbol names
83ea76b1
WC
11314
11315 //perf event based probe
4763f713 11316 register_tapset_perf(s);
a29858ef 11317 register_tapset_java(s);
b55bc428 11318}
dc38c0ae
DS
11319
11320
b20febf3
FCE
11321vector<derived_probe_group*>
11322all_session_groups(systemtap_session& s)
dc38c0ae 11323{
b20febf3 11324 vector<derived_probe_group*> g;
912e8c59
JS
11325
11326#define DOONE(x) \
11327 if (s. x##_derived_probes) \
11328 g.push_back ((derived_probe_group*)(s. x##_derived_probes))
ab655cf8
DS
11329
11330 // Note that order *is* important here. We want to make sure we
11331 // register (actually run) begin probes before any other probe type
11332 // is run. Similarly, when unregistering probes, we want to
11333 // unregister (actually run) end probes after every other probe type
11334 // has be unregistered. To do the latter,
11335 // c_unparser::emit_module_exit() will run this list backwards.
b20febf3
FCE
11336 DOONE(be);
11337 DOONE(dwarf);
888af770 11338 DOONE(uprobe);
b20febf3
FCE
11339 DOONE(timer);
11340 DOONE(profile);
11341 DOONE(mark);
0a6f5a3f 11342 DOONE(tracepoint);
e6fe60e7 11343 DOONE(kprobe);
dd225250 11344 DOONE(hwbkpt);
83ea76b1 11345 DOONE(perf);
b20febf3 11346 DOONE(hrtimer);
ce82316f 11347 DOONE(procfs);
8d9609f5 11348 DOONE(netfilter);
935447c8
DS
11349
11350 // Another "order is important" item. We want to make sure we
11351 // "register" the dummy task_finder probe group after all probe
11352 // groups that use the task_finder.
11353 DOONE(utrace);
a96d1db0 11354 DOONE(itrace);
f31a77f5 11355 DOONE(dynprobe);
e7d4410d 11356 DOONE(java);
935447c8 11357 DOONE(task_finder);
b20febf3
FCE
11358#undef DOONE
11359 return g;
46b84a80 11360}
73267b89
JS
11361
11362/* vim: set sw=2 ts=8 cino=>4,n-2,{2,^-2,t0,(0,u0,w1,M1 : */
This page took 3.111406 seconds and 5 git commands to generate.