]> sourceware.org Git - systemtap.git/blame - tapsets.cxx
Flatten dwflpp in add_unwindsym_ldd
[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
440f755a
JS
383struct base_query; // forward decls
384struct dwarf_query;
385struct dwflpp;
386struct symbol_table;
20e4a32c 387
45582cbc
JL
388static int query_cu (Dwarf_Die * cudie, dwarf_query *q);
389static void query_addr(Dwarf_Addr addr, dwarf_query *q);
390static void query_plt_statement(dwarf_query *q);
a781f401 391
440f755a
JS
392struct
393symbol_table
394{
395 module_info *mod_info; // associated module
396 map<string, func_info*> map_by_name;
1c6b77e5
JS
397 multimap<Dwarf_Addr, func_info*> map_by_addr;
398 typedef multimap<Dwarf_Addr, func_info*>::iterator iterator_t;
440f755a
JS
399 typedef pair<iterator_t, iterator_t> range_t;
400#ifdef __powerpc__
401 GElf_Word opd_section;
402#endif
2867a2a1
JS
403 void add_symbol(const char *name, bool weak, bool descriptor,
404 Dwarf_Addr addr, Dwarf_Addr *high_addr);
440f755a 405 enum info_status read_symbols(FILE *f, const string& path);
83ca3872 406 enum info_status read_from_elf_file(const string& path,
2713ea24 407 systemtap_session &sess);
83ca3872 408 enum info_status read_from_text_file(const string& path,
2713ea24 409 systemtap_session &sess);
440f755a
JS
410 enum info_status get_from_elf();
411 void prepare_section_rejection(Dwfl_Module *mod);
412 bool reject_section(GElf_Word section);
440f755a
JS
413 void purge_syscall_stubs();
414 func_info *lookup_symbol(const string& name);
415 Dwarf_Addr lookup_symbol_address(const string& name);
416 func_info *get_func_containing_address(Dwarf_Addr addr);
3d372d6b 417 func_info *get_first_func();
7a053d3b 418
440f755a
JS
419 symbol_table(module_info *mi) : mod_info(mi) {}
420 ~symbol_table();
421};
77de5e9e 422
440f755a
JS
423static bool null_die(Dwarf_Die *die)
424{
822a6a3d 425 static Dwarf_Die null;
440f755a
JS
426 return (!die || !memcmp(die, &null, sizeof(null)));
427}
c4ce66a1
JS
428
429
7a053d3b 430enum
bd2b1e68 431function_spec_type
7a053d3b 432 {
bd2b1e68
GH
433 function_alone,
434 function_and_file,
7a053d3b 435 function_file_and_line
bd2b1e68
GH
436 };
437
ec4373ff 438
bd2b1e68 439struct dwarf_builder;
f10534c6 440struct dwarf_var_expanding_visitor;
77de5e9e 441
2930abc7 442
b20febf3
FCE
443// XXX: This class is a candidate for subclassing to separate
444// the relocation vs non-relocation variants. Likewise for
445// kprobe vs kretprobe variants.
446
447struct dwarf_derived_probe: public derived_probe
b55bc428 448{
b20febf3
FCE
449 dwarf_derived_probe (const string& function,
450 const string& filename,
451 int line,
452 const string& module,
453 const string& section,
454 Dwarf_Addr dwfl_addr,
2930abc7 455 Dwarf_Addr addr,
b20febf3
FCE
456 dwarf_query & q,
457 Dwarf_Die* scope_die);
20e4a32c 458
b20febf3
FCE
459 string module;
460 string section;
461 Dwarf_Addr addr;
63b4fd14 462 string path;
27dc09b1 463 bool has_process;
2930abc7 464 bool has_return;
c9bad430 465 bool has_maxactive;
63b4fd14 466 bool has_library;
c9bad430 467 long maxactive_val;
4ad95bbc 468 // dwarf_derived_probe_group::emit_module_decls uses this to emit sdt kprobe definition
b642c901
SC
469 string user_path;
470 string user_lib;
b95e2b79 471 bool access_vars;
2930abc7 472
af234c40
JS
473 unsigned saved_longs, saved_strings;
474 dwarf_derived_probe* entry_handler;
475
b8da0ad1 476 void printsig (std::ostream &o) const;
6b66b9f7 477 virtual void join_group (systemtap_session& s);
3689db05 478 void emit_probe_local_init(systemtap_session& s, translator_output * o);
d0bfd2ac 479 void getargs(std::list<std::string> &arg_set) const;
0a98fd42 480
42e38653 481 void emit_privilege_assertion (translator_output*);
27dc09b1
DB
482 void print_dupe_stamp(ostream& o);
483
bd2b1e68 484 // Pattern registration helpers.
7a053d3b 485 static void register_statement_variants(match_node * root,
27dc09b1 486 dwarf_builder * dw,
42e38653 487 privilege_t privilege);
fd6602a0 488 static void register_function_variants(match_node * root,
27dc09b1 489 dwarf_builder * dw,
42e38653 490 privilege_t privilege);
440d9b00
DB
491 static void register_function_and_statement_variants(systemtap_session& s,
492 match_node * root,
27dc09b1 493 dwarf_builder * dw,
42e38653 494 privilege_t privilege);
b1615c74
JS
495 static void register_sdt_variants(systemtap_session& s,
496 match_node * root,
497 dwarf_builder * dw);
498 static void register_plt_variants(systemtap_session& s,
499 match_node * root,
500 dwarf_builder * dw);
c4ce66a1 501 static void register_patterns(systemtap_session& s);
6b66b9f7
JS
502
503protected:
504 dwarf_derived_probe(probe *base,
505 probe_point *location,
506 Dwarf_Addr addr,
507 bool has_return):
74fe61bc
LB
508 derived_probe(base, location), addr(addr), has_process(0),
509 has_return(has_return), has_maxactive(0), has_library(0),
510 maxactive_val(0), access_vars(false), saved_longs(0),
511 saved_strings(0), entry_handler(0)
6b66b9f7
JS
512 {}
513
514private:
d0bfd2ac 515 list<string> args;
8c67c337 516 void saveargs(dwarf_query& q, Dwarf_Die* scope_die, Dwarf_Addr dwfl_addr);
20c6c071
GH
517};
518
dc38c0ae 519
6b66b9f7 520struct uprobe_derived_probe: public dwarf_derived_probe
6d0f3f0c 521{
6d0f3f0c 522 int pid; // 0 => unrestricted
0973d815 523
6d0f3f0c
FCE
524 uprobe_derived_probe (const string& function,
525 const string& filename,
526 int line,
527 const string& module,
6d0f3f0c
FCE
528 const string& section,
529 Dwarf_Addr dwfl_addr,
530 Dwarf_Addr addr,
531 dwarf_query & q,
6b66b9f7
JS
532 Dwarf_Die* scope_die):
533 dwarf_derived_probe(function, filename, line, module, section,
534 dwfl_addr, addr, q, scope_die), pid(0)
535 {}
6d0f3f0c 536
0973d815
FCE
537 // alternate constructor for process(PID).statement(ADDR).absolute
538 uprobe_derived_probe (probe *base,
539 probe_point *location,
540 int pid,
541 Dwarf_Addr addr,
6b66b9f7
JS
542 bool has_return):
543 dwarf_derived_probe(base, location, addr, has_return), pid(pid)
544 {}
9ace370f 545
6d0f3f0c 546 void join_group (systemtap_session& s);
2865d17a 547
42e38653 548 void emit_privilege_assertion (translator_output*);
8f6d8c2b 549 void print_dupe_stamp(ostream& o) { print_dupe_stamp_unprivileged_process_owner (o); }
c0f84e7b
SC
550 void getargs(std::list<std::string> &arg_set) const;
551 void saveargs(int nargs);
552private:
553 list<string> args;
6d0f3f0c
FCE
554};
555
dc38c0ae
DS
556struct dwarf_derived_probe_group: public derived_probe_group
557{
558private:
b20febf3
FCE
559 multimap<string,dwarf_derived_probe*> probes_by_module;
560 typedef multimap<string,dwarf_derived_probe*>::iterator p_b_m_iterator;
dc38c0ae
DS
561
562public:
08b5a50c 563 dwarf_derived_probe_group() {}
b20febf3
FCE
564 void enroll (dwarf_derived_probe* probe);
565 void emit_module_decls (systemtap_session& s);
566 void emit_module_init (systemtap_session& s);
b4be7cbc 567 void emit_module_refresh (systemtap_session& s);
b20febf3 568 void emit_module_exit (systemtap_session& s);
dc38c0ae
DS
569};
570
571
20c6c071 572// Helper struct to thread through the dwfl callbacks.
2c384610 573struct base_query
20c6c071 574{
c4ce66a1
JS
575 base_query(dwflpp & dw, literal_map_t const & params);
576 base_query(dwflpp & dw, const string & module_val);
2c384610 577 virtual ~base_query() {}
bd2b1e68 578
5227f1ea 579 systemtap_session & sess;
2c384610 580 dwflpp & dw;
5227f1ea 581
070764c0
JL
582 // Used to keep track of which modules were visited during
583 // iterate_over_modules()
584 set<string> visited_modules;
585
bd2b1e68 586 // Parameter extractors.
86bf665e 587 static bool has_null_param(literal_map_t const & params,
888af770 588 string const & k);
86bf665e 589 static bool get_string_param(literal_map_t const & params,
bd2b1e68 590 string const & k, string & v);
86bf665e 591 static bool get_number_param(literal_map_t const & params,
bd2b1e68 592 string const & k, long & v);
86bf665e 593 static bool get_number_param(literal_map_t const & params,
c239d28c 594 string const & k, Dwarf_Addr & v);
bbbc7241 595 static void query_library_callback (base_query *me, const char *data);
45cdb40e 596 static void query_plt_callback (base_query *me, const char *link, size_t addr);
f301a9ff 597 virtual void query_library (const char *data) = 0;
576eaefe 598 virtual void query_plt (const char *link, size_t addr) = 0;
84c84ac4 599
b55bc428 600
2c384610
DS
601 // Extracted parameters.
602 bool has_kernel;
91af0778
FCE
603 bool has_module;
604 bool has_process;
63b4fd14 605 bool has_library;
576eaefe
SC
606 bool has_plt;
607 bool has_statement;
2c384610 608 string module_val; // has_kernel => module_val = "kernel"
63b4fd14 609 string path; // executable path if module is a .so
576eaefe 610 string plt_val; // has_plt => plt wildcard
2c384610
DS
611
612 virtual void handle_query_module() = 0;
613};
614
615
c4ce66a1 616base_query::base_query(dwflpp & dw, literal_map_t const & params):
576eaefe 617 sess(dw.sess), dw(dw), has_library(false), has_plt(false), has_statement(false)
2c384610 618{
91af0778 619 has_kernel = has_null_param (params, TOK_KERNEL);
2c384610
DS
620 if (has_kernel)
621 module_val = "kernel";
91af0778
FCE
622
623 has_module = get_string_param (params, TOK_MODULE, module_val);
624 if (has_module)
625 has_process = false;
4baf0e53 626 else
d0a7f5a9 627 {
63b4fd14 628 string library_name;
576eaefe 629 long statement_num_val;
d0a7f5a9 630 has_process = get_string_param(params, TOK_PROCESS, module_val);
63b4fd14 631 has_library = get_string_param (params, TOK_LIBRARY, library_name);
576eaefe
SC
632 if ((has_plt = has_null_param (params, TOK_PLT)))
633 plt_val = "*";
634 else has_plt = get_string_param (params, TOK_PLT, plt_val);
635 has_statement = get_number_param(params, TOK_STATEMENT, statement_num_val);
636
84c84ac4 637 if (has_process)
05fb3e0c 638 module_val = find_executable (module_val, sess.sysroot, sess.sysenv);
84c84ac4
SC
639 if (has_library)
640 {
641 if (! contains_glob_chars (library_name))
642 {
05fb3e0c
WF
643 path = path_remove_sysroot(sess, module_val);
644 module_val = find_executable (library_name, sess.sysroot,
645 sess.sysenv, "LD_LIBRARY_PATH");
47e226ed
SC
646 if (module_val.find('/') == string::npos)
647 {
648 // We didn't find library_name so use iterate_over_libraries
649 module_val = path;
650 path = library_name;
651 }
84c84ac4
SC
652 }
653 else
654 path = library_name;
655 }
d0a7f5a9 656 }
91af0778
FCE
657
658 assert (has_kernel || has_process || has_module);
2c384610
DS
659}
660
c4ce66a1 661base_query::base_query(dwflpp & dw, const string & module_val)
576eaefe
SC
662 : sess(dw.sess), dw(dw), has_library(false), has_plt(false), has_statement(false),
663 module_val(module_val)
c4ce66a1
JS
664{
665 // NB: This uses '/' to distinguish between kernel modules and userspace,
666 // which means that userspace modules won't get any PATH searching.
667 if (module_val.find('/') == string::npos)
668 {
669 has_kernel = (module_val == TOK_KERNEL);
670 has_module = !has_kernel;
671 has_process = false;
672 }
673 else
674 {
675 has_kernel = has_module = false;
676 has_process = true;
677 }
678}
679
2c384610 680bool
86bf665e 681base_query::has_null_param(literal_map_t const & params,
2c384610
DS
682 string const & k)
683{
888af770 684 return derived_probe_builder::has_null_param(params, k);
2c384610
DS
685}
686
687
688bool
86bf665e 689base_query::get_string_param(literal_map_t const & params,
2c384610
DS
690 string const & k, string & v)
691{
692 return derived_probe_builder::get_param (params, k, v);
693}
694
695
696bool
86bf665e 697base_query::get_number_param(literal_map_t const & params,
2c384610
DS
698 string const & k, long & v)
699{
700 int64_t value;
701 bool present = derived_probe_builder::get_param (params, k, value);
702 v = (long) value;
703 return present;
704}
705
706
707bool
86bf665e 708base_query::get_number_param(literal_map_t const & params,
2c384610
DS
709 string const & k, Dwarf_Addr & v)
710{
711 int64_t value;
712 bool present = derived_probe_builder::get_param (params, k, value);
713 v = (Dwarf_Addr) value;
714 return present;
715}
716
2c384610
DS
717struct dwarf_query : public base_query
718{
e1278bd4 719 dwarf_query(probe * base_probe,
2c384610
DS
720 probe_point * base_loc,
721 dwflpp & dw,
86bf665e 722 literal_map_t const & params,
b642c901
SC
723 vector<derived_probe *> & results,
724 const string user_path,
725 const string user_lib);
2c384610 726
c4ce66a1 727 vector<derived_probe *> & results;
8f14e444 728 set<string> inlined_non_returnable; // function names
c4ce66a1
JS
729 probe * base_probe;
730 probe_point * base_loc;
b642c901
SC
731 string user_path;
732 string user_lib;
c4ce66a1 733
2c384610 734 virtual void handle_query_module();
5f0a03a6
JK
735 void query_module_dwarf();
736 void query_module_symtab();
5d5bd369 737 void query_library (const char *data);
576eaefe 738 void query_plt (const char *entry, size_t addr);
2c384610 739
2930abc7
FCE
740 void add_probe_point(string const & funcname,
741 char const * filename,
742 int line,
743 Dwarf_Die *scope_die,
744 Dwarf_Addr addr);
36f9dd1d 745
857bdfd1
JS
746 // Track addresses we've already seen in a given module
747 set<Dwarf_Addr> alias_dupes;
748
7fdd3e2c
JS
749 // Track inlines we've already seen as well
750 // NB: this can't be compared just by entrypc, as inlines can overlap
751 set<inline_instance_info> inline_dupes;
752
e2941743
JL
753 // Used in .callee[s] probes, when calling iterate_over_callees() (which
754 // provides the actual stack). Retains the addrs of the callers unwind addr
755 // where the callee is found. Specifies multiple callers. E.g. when a callee
756 // at depth 2 is found, callers[1] has the addr of the caller, and callers[0]
757 // has the addr of the caller's caller.
758 stack<Dwarf_Addr> *callers;
759
20c6c071
GH
760 bool has_function_str;
761 bool has_statement_str;
762 bool has_function_num;
763 bool has_statement_num;
7a053d3b
RM
764 string statement_str_val;
765 string function_str_val;
c239d28c
GH
766 Dwarf_Addr statement_num_val;
767 Dwarf_Addr function_num_val;
20c6c071 768
b8da0ad1 769 bool has_call;
4bda987e 770 bool has_exported;
b8da0ad1 771 bool has_inline;
20c6c071
GH
772 bool has_return;
773
c9bad430
DS
774 bool has_maxactive;
775 long maxactive_val;
776
20c6c071
GH
777 bool has_label;
778 string label_val;
779
c31add50
JL
780 bool has_callee;
781 string callee_val;
782
783 bool has_callees_num;
784 long callees_num_val;
785
20c6c071
GH
786 bool has_relative;
787 long relative_val;
788
37ebca01
FCE
789 bool has_absolute;
790
467bea43
SC
791 bool has_mark;
792
5f0a03a6
JK
793 enum dbinfo_reqt dbinfo_reqt;
794 enum dbinfo_reqt assess_dbinfo_reqt();
795
7d6d0afc 796 void parse_function_spec(const string & spec);
20c6c071 797 function_spec_type spec_type;
7d6d0afc 798 vector<string> scopes;
20c6c071
GH
799 string function;
800 string file;
4a81c0d9
JL
801 lineno_t lineno_type;
802 int linenos[2];
5f0a03a6 803 bool query_done; // Found exact match
20c6c071 804
5a617dc6
JL
805 // Holds the prologue end of the current function
806 Dwarf_Addr prologue_end;
807
bd25380d 808 set<string> filtered_srcfiles;
7e1279ea
FCE
809
810 // Map official entrypc -> func_info object
86bf665e
TM
811 inline_instance_map_t filtered_inlines;
812 func_info_map_t filtered_functions;
4df79aaf 813
959ccfed
JL
814 // Helper when we want to iterate over both
815 base_func_info_map_t filtered_all();
816
4df79aaf 817 void query_module_functions ();
e772a6e7
JL
818
819 string final_function_name(const string& final_func,
820 const string& final_file,
821 int final_line);
b55bc428
FCE
822};
823
98afd80e 824
435f53a7
FCE
825static void delete_session_module_cache (systemtap_session& s); // forward decl
826
827
98afd80e 828struct dwarf_builder: public derived_probe_builder
b55bc428 829{
665e1256 830 map <string,dwflpp*> kern_dw; /* NB: key string could be a wildcard */
7a24d422 831 map <string,dwflpp*> user_dw;
b642c901
SC
832 string user_path;
833 string user_lib;
44ffe90c
JL
834
835 // Holds modules to suggest functions from. NB: aggregates over
836 // recursive calls to build() when deriving globby probes.
837 set <string> modules_seen;
838
ae2552da 839 dwarf_builder() {}
aa30ccd3 840
ae2552da 841 dwflpp *get_kern_dw(systemtap_session& sess, const string& module)
707bf35e 842 {
ea14cf67
FCE
843 if (kern_dw[module] == 0)
844 kern_dw[module] = new dwflpp(sess, module, true); // might throw
ae2552da 845 return kern_dw[module];
707bf35e
JS
846 }
847
848 dwflpp *get_user_dw(systemtap_session& sess, const string& module)
849 {
ea14cf67
FCE
850 if (user_dw[module] == 0)
851 user_dw[module] = new dwflpp(sess, module, false); // might throw
707bf35e
JS
852 return user_dw[module];
853 }
7a24d422
FCE
854
855 /* NB: not virtual, so can be called from dtor too: */
822a6a3d 856 void dwarf_build_no_more (bool)
aa30ccd3 857 {
435f53a7
FCE
858 delete_map(kern_dw);
859 delete_map(user_dw);
7a24d422
FCE
860 }
861
862 void build_no_more (systemtap_session &s)
863 {
864 dwarf_build_no_more (s.verbose > 3);
435f53a7 865 delete_session_module_cache (s);
aa30ccd3
FCE
866 }
867
e38d6504
RM
868 ~dwarf_builder()
869 {
7a24d422 870 dwarf_build_no_more (false);
c8959a29 871 }
aa30ccd3 872
5227f1ea 873 virtual void build(systemtap_session & sess,
7a053d3b 874 probe * base,
20c6c071 875 probe_point * location,
86bf665e 876 literal_map_t const & parameters,
20c6c071 877 vector<derived_probe *> & finished_results);
b55bc428
FCE
878};
879
5111fc3e 880
e1278bd4 881dwarf_query::dwarf_query(probe * base_probe,
20c6c071
GH
882 probe_point * base_loc,
883 dwflpp & dw,
86bf665e 884 literal_map_t const & params,
b642c901
SC
885 vector<derived_probe *> & results,
886 const string user_path,
887 const string user_lib)
e2941743
JL
888 : base_query(dw, params), results(results), base_probe(base_probe),
889 base_loc(base_loc), user_path(user_path), user_lib(user_lib),
5a617dc6 890 callers(NULL), has_relative(false), relative_val(0), prologue_end(0)
bd2b1e68
GH
891{
892 // Reduce the query to more reasonable semantic values (booleans,
893 // extracted strings, numbers, etc).
bd2b1e68
GH
894 has_function_str = get_string_param(params, TOK_FUNCTION, function_str_val);
895 has_function_num = get_number_param(params, TOK_FUNCTION, function_num_val);
896
897 has_statement_str = get_string_param(params, TOK_STATEMENT, statement_str_val);
898 has_statement_num = get_number_param(params, TOK_STATEMENT, statement_num_val);
899
0f336e95 900 has_label = get_string_param(params, TOK_LABEL, label_val);
c31add50
JL
901 has_callee = get_string_param(params, TOK_CALLEE, callee_val);
902 if (has_null_param(params, TOK_CALLEES))
903 { // .callees ==> .callees(1) (also equivalent to .callee("*"))
904 has_callees_num = true;
905 callees_num_val = 1;
906 }
907 else
908 {
909 has_callees_num = get_number_param(params, TOK_CALLEES, callees_num_val);
910 if (has_callees_num && callees_num_val < 1)
911 throw SEMANTIC_ERROR(_(".callees(N) only acceptable for N >= 1"),
912 base_probe->tok);
913 }
0f336e95 914
b8da0ad1 915 has_call = has_null_param(params, TOK_CALL);
4bda987e 916 has_exported = has_null_param(params, TOK_EXPORTED);
b8da0ad1 917 has_inline = has_null_param(params, TOK_INLINE);
bd2b1e68 918 has_return = has_null_param(params, TOK_RETURN);
c9bad430 919 has_maxactive = get_number_param(params, TOK_MAXACTIVE, maxactive_val);
37ebca01 920 has_absolute = has_null_param(params, TOK_ABSOLUTE);
467bea43 921 has_mark = false;
37ebca01 922
bd2b1e68 923 if (has_function_str)
7d6d0afc 924 parse_function_spec(function_str_val);
bd2b1e68 925 else if (has_statement_str)
7d6d0afc 926 parse_function_spec(statement_str_val);
0daad364 927
5f0a03a6
JK
928 dbinfo_reqt = assess_dbinfo_reqt();
929 query_done = false;
0daad364
JS
930}
931
932
2c384610 933void
5f0a03a6 934dwarf_query::query_module_dwarf()
2c384610
DS
935{
936 if (has_function_num || has_statement_num)
937 {
938 // If we have module("foo").function(0xbeef) or
939 // module("foo").statement(0xbeef), the address is relative
940 // to the start of the module, so we seek the function
941 // number plus the module's bias.
6b517475
JS
942 Dwarf_Addr addr = has_function_num ?
943 function_num_val : statement_num_val;
08d1d520
MW
944
945 // These are raw addresses, we need to know what the elf_bias
946 // is to feed it to libdwfl based functions.
947 Dwarf_Addr elf_bias;
948 Elf *elf = dwfl_module_getelf (dw.module, &elf_bias);
949 assert(elf);
950 addr += elf_bias;
6b517475 951 query_addr(addr, this);
2c384610
DS
952 }
953 else
954 {
955 // Otherwise if we have a function("foo") or statement("foo")
956 // specifier, we have to scan over all the CUs looking for
957 // the function(s) in question
958 assert(has_function_str || has_statement_str);
4df79aaf
JS
959
960 // For simple cases, no wildcard and no source:line, we can do a very
961 // quick function lookup in a module-wide cache.
1ffb8bd1
JS
962 if (spec_type == function_alone &&
963 !dw.name_has_wildcard(function) &&
964 !startswith(function, "_Z"))
4df79aaf
JS
965 query_module_functions();
966 else
337b7c44 967 dw.iterate_over_cus(&query_cu, this, false);
2c384610
DS
968 }
969}
970
5f0a03a6
JK
971static void query_func_info (Dwarf_Addr entrypc, func_info & fi,
972 dwarf_query * q);
973
91bb9081
LB
974static void
975query_symtab_func_info (func_info & fi, dwarf_query * q)
976{
977 assert(null_die(&fi.die));
978
979 Dwarf_Addr addr = fi.addr;
980
981 // Now compensate for the dw bias because the addresses come
982 // from dwfl_module_symtab, so fi->addr is NOT a normal dw address.
983 q->dw.get_module_dwarf(false, false);
984 addr -= q->dw.module_bias;
985
986 // If there are already probes in this module, lets not duplicate.
987 // This can come from other weak symbols/aliases or existing
a41f2ad7
JS
988 // matches from Dwarf DIE functions. Try to add this addr to the
989 // collection, and only continue if it was new.
990 if (q->alias_dupes.insert(addr).second)
991 query_func_info(addr, fi, q);
91bb9081
LB
992}
993
5f0a03a6
JK
994void
995dwarf_query::query_module_symtab()
996{
997 // Get the symbol table if it's necessary, sufficient, and not already got.
998 if (dbinfo_reqt == dbr_need_dwarf)
999 return;
1000
1001 module_info *mi = dw.mod_info;
1002 if (dbinfo_reqt == dbr_need_symtab)
1003 {
1004 if (mi->symtab_status == info_unknown)
1005 mi->get_symtab(this);
1006 if (mi->symtab_status == info_absent)
1007 return;
1008 }
1009
1010 func_info *fi = NULL;
1011 symbol_table *sym_table = mi->sym_table;
1012
1013 if (has_function_str)
1014 {
1015 // Per dwarf_query::assess_dbinfo_reqt()...
1016 assert(spec_type == function_alone);
1017 if (dw.name_has_wildcard(function_str_val))
1018 {
2e67a43b 1019 symbol_table::iterator_t iter;
1c6b77e5
JS
1020 for (iter = sym_table->map_by_addr.begin();
1021 iter != sym_table->map_by_addr.end();
2e67a43b 1022 ++iter)
5f0a03a6 1023 {
1c6b77e5 1024 fi = iter->second;
5f0a03a6
JK
1025 if (!null_die(&fi->die))
1026 continue; // already handled in query_module_dwarf()
1027 if (dw.function_name_matches_pattern(fi->name, function_str_val))
91bb9081 1028 query_symtab_func_info(*fi, this);
5f0a03a6
JK
1029 }
1030 }
1031 else
1032 {
1033 fi = sym_table->lookup_symbol(function_str_val);
2867a2a1 1034 if (fi && !fi->descriptor && null_die(&fi->die))
91bb9081 1035 query_symtab_func_info(*fi, this);
5f0a03a6 1036 }
5f0a03a6
JK
1037 }
1038}
1039
1040void
1041dwarf_query::handle_query_module()
1042{
f4faaf86
JS
1043 if (has_plt && has_statement_num)
1044 {
1045 query_plt_statement (this);
1046 return;
1047 }
1048
0035051e 1049 bool report = dbinfo_reqt == dbr_need_dwarf;
1c6b77e5
JS
1050 dw.get_module_dwarf(false, report);
1051
1052 // prebuild the symbol table to resolve aliases
1053 dw.mod_info->get_symtab(this);
1054
857bdfd1
JS
1055 // reset the dupe-checking for each new module
1056 alias_dupes.clear();
7fdd3e2c 1057 inline_dupes.clear();
857bdfd1 1058
5f0a03a6
JK
1059 if (dw.mod_info->dwarf_status == info_present)
1060 query_module_dwarf();
1c6b77e5 1061
91bb9081
LB
1062 // Consult the symbol table, asm and weak functions can show up
1063 // in the symbol table but not in dwarf and minidebuginfo is
1064 // located in the gnu_debugdata section, alias_dupes checking
1065 // is done before adding any probe points
1066 if (!query_done && !pending_interrupts)
5f0a03a6
JK
1067 query_module_symtab();
1068}
1069
2c384610 1070
7d6d0afc
JS
1071void
1072dwarf_query::parse_function_spec(const string & spec)
bd2b1e68 1073{
4a81c0d9
JL
1074 lineno_type = ABSOLUTE;
1075 linenos[0] = linenos[1] = 0;
1d12a9b2 1076
91699a70 1077 size_t src_pos, line_pos, dash_pos, scope_pos;
bd2b1e68 1078
7d6d0afc 1079 // look for named scopes
91699a70
JS
1080 scope_pos = spec.rfind("::");
1081 if (scope_pos != string::npos)
bd2b1e68 1082 {
91699a70
JS
1083 tokenize_cxx(spec.substr(0, scope_pos), scopes);
1084 scope_pos += 2;
bd2b1e68 1085 }
91699a70
JS
1086 else
1087 scope_pos = 0;
bd2b1e68 1088
7d6d0afc
JS
1089 // look for a source separator
1090 src_pos = spec.find('@', scope_pos);
1091 if (src_pos == string::npos)
bd2b1e68 1092 {
7d6d0afc
JS
1093 function = spec.substr(scope_pos);
1094 spec_type = function_alone;
bd2b1e68 1095 }
7d6d0afc 1096 else
879eb9e9 1097 {
7d6d0afc 1098 function = spec.substr(scope_pos, src_pos - scope_pos);
7a053d3b 1099
7d6d0afc
JS
1100 // look for a line-number separator
1101 line_pos = spec.find_first_of(":+", src_pos);
1102 if (line_pos == string::npos)
1103 {
1104 file = spec.substr(src_pos + 1);
1105 spec_type = function_and_file;
1106 }
1107 else
1108 {
1109 file = spec.substr(src_pos + 1, line_pos - src_pos - 1);
1110
1111 // classify the line spec
1112 spec_type = function_file_and_line;
1113 if (spec[line_pos] == '+')
4a81c0d9 1114 lineno_type = RELATIVE;
7d6d0afc
JS
1115 else if (spec[line_pos + 1] == '*' &&
1116 spec.length() == line_pos + 2)
4a81c0d9 1117 lineno_type = WILDCARD;
7d6d0afc 1118 else
4a81c0d9 1119 lineno_type = ABSOLUTE;
7d6d0afc 1120
4a81c0d9 1121 if (lineno_type != WILDCARD)
7d6d0afc
JS
1122 try
1123 {
1124 // try to parse either N or N-M
1125 dash_pos = spec.find('-', line_pos + 1);
1126 if (dash_pos == string::npos)
4a81c0d9 1127 linenos[0] = linenos[1] = lex_cast<int>(spec.substr(line_pos + 1));
7d6d0afc
JS
1128 else
1129 {
4a81c0d9
JL
1130 lineno_type = RANGE;
1131 linenos[0] = lex_cast<int>(spec.substr(line_pos + 1,
7d6d0afc 1132 dash_pos - line_pos - 1));
4a81c0d9
JL
1133 linenos[1] = lex_cast<int>(spec.substr(dash_pos + 1));
1134 if (linenos[0] > linenos[1])
1135 throw runtime_error("bad range");
7d6d0afc
JS
1136 }
1137 }
1138 catch (runtime_error & exn)
1139 {
1140 goto bad;
1141 }
1142 }
bd2b1e68
GH
1143 }
1144
7d6d0afc
JS
1145 if (function.empty() ||
1146 (spec_type != function_alone && file.empty()))
bd2b1e68
GH
1147 goto bad;
1148
7d6d0afc 1149 if (sess.verbose > 2)
bd2b1e68 1150 {
b530b5b3
LB
1151 //clog << "parsed '" << spec << "'";
1152 clog << _F("parse '%s'", spec.c_str());
41c262f3 1153
7d6d0afc
JS
1154 if (!scopes.empty())
1155 clog << ", scope '" << scopes[0] << "'";
1156 for (unsigned i = 1; i < scopes.size(); ++i)
1157 clog << "::'" << scopes[i] << "'";
41c262f3 1158
7d6d0afc
JS
1159 clog << ", func '" << function << "'";
1160
1161 if (spec_type != function_alone)
1162 clog << ", file '" << file << "'";
1163
1164 if (spec_type == function_file_and_line)
1165 {
1166 clog << ", line ";
4a81c0d9 1167 switch (lineno_type)
7d6d0afc
JS
1168 {
1169 case ABSOLUTE:
4a81c0d9 1170 clog << linenos[0];
7d6d0afc
JS
1171 break;
1172
1173 case RELATIVE:
4a81c0d9 1174 clog << "+" << linenos[0];
7d6d0afc
JS
1175 break;
1176
1177 case RANGE:
4a81c0d9 1178 clog << linenos[0] << " - " << linenos[1];
7d6d0afc
JS
1179 break;
1180
1181 case WILDCARD:
1182 clog << "*";
1183 break;
1184 }
1185 }
1186
1187 clog << endl;
bd2b1e68
GH
1188 }
1189
7d6d0afc
JS
1190 return;
1191
1192bad:
dc09353a 1193 throw SEMANTIC_ERROR(_F("malformed specification '%s'", spec.c_str()),
7d6d0afc 1194 base_probe->tok);
bd2b1e68
GH
1195}
1196
05fb3e0c
WF
1197string path_remove_sysroot(const systemtap_session& sess, const string& path)
1198{
1199 size_t pos;
1200 string retval = path;
1201 if (!sess.sysroot.empty() &&
1202 (pos = retval.find(sess.sysroot)) != string::npos)
1203 retval.replace(pos, sess.sysroot.length(), "/");
1204 return retval;
1205}
bd2b1e68 1206
36f9dd1d 1207void
1ffb8bd1 1208dwarf_query::add_probe_point(const string& dw_funcname,
b20febf3 1209 const char* filename,
36f9dd1d 1210 int line,
b20febf3 1211 Dwarf_Die* scope_die,
36f9dd1d
FCE
1212 Dwarf_Addr addr)
1213{
b20febf3 1214 string reloc_section; // base section for relocation purposes
27646582 1215 Dwarf_Addr reloc_addr; // relocated
b20febf3 1216 const string& module = dw.module_name; // "kernel" or other
1ffb8bd1 1217 string funcname = dw_funcname;
36f9dd1d 1218
37ebca01
FCE
1219 assert (! has_absolute); // already handled in dwarf_builder::build()
1220
f4faaf86 1221 reloc_addr = dw.relocate_address(addr, reloc_section);
2930abc7 1222
1ffb8bd1 1223 // If we originally used the linkage name, then let's call it that way
1ffb8bd1 1224 const char* linkage_name;
91bb9081 1225 if (!null_die(scope_die) && startswith (this->function, "_Z")
f450a7e3 1226 && (linkage_name = dwarf_linkage_name (scope_die)))
1ffb8bd1
JS
1227 funcname = linkage_name;
1228
7f9f3386
FCE
1229 if (sess.verbose > 1)
1230 {
b530b5b3 1231 clog << _("probe ") << funcname << "@" << filename << ":" << line;
b20febf3 1232 if (string(module) == TOK_KERNEL)
b530b5b3 1233 clog << _(" kernel");
91af0778 1234 else if (has_module)
b530b5b3 1235 clog << _(" module=") << module;
91af0778 1236 else if (has_process)
b530b5b3 1237 clog << _(" process=") << module;
b20febf3 1238 if (reloc_section != "") clog << " reloc=" << reloc_section;
b20febf3 1239 clog << " pc=0x" << hex << addr << dec;
7f9f3386 1240 }
4baf0e53 1241
27646582 1242 bool bad = dw.blacklisted_p (funcname, filename, line, module,
789448a3 1243 addr, has_return);
b20febf3
FCE
1244 if (sess.verbose > 1)
1245 clog << endl;
7f9f3386 1246
84048984
FCE
1247 if (module == TOK_KERNEL)
1248 {
1249 // PR 4224: adapt to relocatable kernel by subtracting the _stext address here.
1250 reloc_addr = addr - sess.sym_stext;
37ebca01 1251 reloc_section = "_stext"; // a message to runtime's _stp_module_relocate
84048984
FCE
1252 }
1253
b20febf3
FCE
1254 if (! bad)
1255 {
1a0dbc5a 1256 sess.unwindsym_modules.insert (module);
6d0f3f0c
FCE
1257
1258 if (has_process)
1259 {
05fb3e0c 1260 string module_tgt = path_remove_sysroot(sess, module);
6d0f3f0c 1261 results.push_back (new uprobe_derived_probe(funcname, filename, line,
05fb3e0c 1262 module_tgt, reloc_section, addr, reloc_addr,
6d0f3f0c
FCE
1263 *this, scope_die));
1264 }
1265 else
1266 {
1267 assert (has_kernel || has_module);
1268 results.push_back (new dwarf_derived_probe(funcname, filename, line,
06aca46a 1269 module, reloc_section, addr, reloc_addr,
6d0f3f0c
FCE
1270 *this, scope_die));
1271 }
b20febf3 1272 }
2930abc7
FCE
1273}
1274
5f0a03a6
JK
1275enum dbinfo_reqt
1276dwarf_query::assess_dbinfo_reqt()
1277{
1278 if (has_absolute)
1279 {
1280 // kernel.statement(NUM).absolute
1281 return dbr_none;
1282 }
c31add50 1283 if (has_inline || has_label || has_callee || has_callees_num)
5f0a03a6
JK
1284 {
1285 // kernel.function("f").inline or module("m").function("f").inline
1286 return dbr_need_dwarf;
1287 }
1288 if (has_function_str && spec_type == function_alone)
1289 {
1290 // kernel.function("f") or module("m").function("f")
1291 return dbr_need_symtab;
1292 }
1293 if (has_statement_num)
1294 {
1295 // kernel.statement(NUM) or module("m").statement(NUM)
1296 // Technically, all we need is the module offset (or _stext, for
1297 // the kernel). But for that we need either the ELF file or (for
1298 // _stext) the symbol table. In either case, the symbol table
1299 // is available, and that allows us to map the NUM (address)
1300 // to a function, which is goodness.
1301 return dbr_need_symtab;
1302 }
1303 if (has_function_num)
1304 {
1305 // kernel.function(NUM) or module("m").function(NUM)
1306 // Need the symbol table so we can back up from NUM to the
1307 // start of the function.
1308 return dbr_need_symtab;
1309 }
1310 // Symbol table tells us nothing about source files or line numbers.
1311 return dbr_need_dwarf;
1312}
2930abc7 1313
e772a6e7
JL
1314string
1315dwarf_query::final_function_name(const string& final_func,
1316 const string& final_file,
1317 int final_line)
1318{
1319 string final_name = final_func;
1320 if (final_file != "")
1321 {
1322 final_name += ("@" + final_file);
1323 if (final_line > 0)
1324 final_name += (":" + lex_cast(final_line));
1325 }
1326 return final_name;
1327}
2930abc7 1328
959ccfed
JL
1329base_func_info_map_t
1330dwarf_query::filtered_all(void)
1331{
1332 vector<base_func_info> r;
1333 func_info_map_t::const_iterator f;
1334 for (f = filtered_functions.begin();
1335 f != filtered_functions.end(); ++f)
1336 r.push_back(*f);
1337 inline_instance_map_t::const_iterator i;
1338 for (i = filtered_inlines.begin();
1339 i != filtered_inlines.end(); ++i)
1340 r.push_back(*i);
1341 return r;
1342}
1343
b8da0ad1
FCE
1344// The critical determining factor when interpreting a pattern
1345// string is, perhaps surprisingly: "presence of a lineno". The
1346// presence of a lineno changes the search strategy completely.
1347//
1348// Compare the two cases:
1349//
1350// 1. {statement,function}(foo@file.c:lineno)
1351// - find the files matching file.c
1352// - in each file, find the functions matching foo
1353// - query the file for line records matching lineno
1354// - iterate over the line records,
1355// - and iterate over the functions,
1356// - if(haspc(function.DIE, line.addr))
1357// - if looking for statements: probe(lineno.addr)
1358// - if looking for functions: probe(function.{entrypc,return,etc.})
1359//
1360// 2. {statement,function}(foo@file.c)
1361// - find the files matching file.c
1362// - in each file, find the functions matching foo
1363// - probe(function.{entrypc,return,etc.})
1364//
1365// Thus the first decision we make is based on the presence of a
1366// lineno, and we enter entirely different sets of callbacks
1367// depending on that decision.
1368//
1369// Note that the first case is a generalization fo the second, in that
1370// we could theoretically search through line records for matching
1371// file names (a "table scan" in rdbms lingo). Luckily, file names
1372// are already cached elsewhere, so we can do an "index scan" as an
1373// optimization.
7e1279ea 1374
bd2b1e68 1375static void
4cd232e4 1376query_statement (string const & func,
20e4a32c 1377 char const * file,
4cd232e4 1378 int line,
bcc12710 1379 Dwarf_Die *scope_die,
20e4a32c 1380 Dwarf_Addr stmt_addr,
4cd232e4 1381 dwarf_query * q)
bd2b1e68 1382{
39bcd429
FCE
1383 try
1384 {
cee35f73 1385 q->add_probe_point(func, file ? file : "",
a9b2f3a5 1386 line, scope_die, stmt_addr);
39bcd429
FCE
1387 }
1388 catch (const semantic_error& e)
1389 {
1390 q->sess.print_error (e);
1391 }
bd2b1e68
GH
1392}
1393
6b517475
JS
1394static void
1395query_addr(Dwarf_Addr addr, dwarf_query *q)
1396{
1397 dwflpp &dw = q->dw;
1398
08d1d520
MW
1399 if (q->sess.verbose > 2)
1400 clog << "query_addr 0x" << hex << addr << dec << endl;
6b517475
JS
1401
1402 // First pick which CU contains this address
1403 Dwarf_Die* cudie = dw.query_cu_containing_address(addr);
1404 if (!cudie) // address could be wildly out of range
1405 return;
1406 dw.focus_on_cu(cudie);
1407
1408 // Now compensate for the dw bias
1409 addr -= dw.module_bias;
1410
1411 // Per PR5787, we look up the scope die even for
1412 // statement_num's, for blacklist sensitivity and $var
1413 // resolution purposes.
1414
1415 // Find the scopes containing this address
1416 vector<Dwarf_Die> scopes = dw.getscopes(addr);
1417 if (scopes.empty())
1418 return;
1419
1420 // Look for the innermost containing function
1421 Dwarf_Die *fnscope = NULL;
1422 for (size_t i = 0; i < scopes.size(); ++i)
1423 {
1424 int tag = dwarf_tag(&scopes[i]);
1425 if ((tag == DW_TAG_subprogram && !q->has_inline) ||
1426 (tag == DW_TAG_inlined_subroutine &&
4bda987e 1427 !q->has_call && !q->has_return && !q->has_exported))
6b517475
JS
1428 {
1429 fnscope = &scopes[i];
1430 break;
1431 }
1432 }
1433 if (!fnscope)
1434 return;
1435 dw.focus_on_function(fnscope);
1436
1437 Dwarf_Die *scope = q->has_function_num ? fnscope : &scopes[0];
1438
1439 const char *file = dwarf_decl_file(fnscope);
1440 int line;
1441 dwarf_decl_line(fnscope, &line);
1442
1443 // Function probes should reset the addr to the function entry
1444 // and possibly perform prologue searching
1445 if (q->has_function_num)
1446 {
1bbf3f90
JS
1447 if (!dw.die_entrypc(fnscope, &addr))
1448 return;
6b517475 1449 if (dwarf_tag(fnscope) == DW_TAG_subprogram &&
b5b9879f
JL
1450 (q->sess.prologue_searching ||
1451 (q->has_process && !q->dw.has_valid_locs()))) // PR 6871 && PR 6941
6b517475
JS
1452 {
1453 func_info func;
1454 func.die = *fnscope;
1455 func.name = dw.function_name;
1456 func.decl_file = file;
1457 func.decl_line = line;
1458 func.entrypc = addr;
1459
1460 func_info_map_t funcs(1, func);
1461 dw.resolve_prologue_endings (funcs);
5a617dc6
JL
1462 q->prologue_end = funcs[0].prologue_end;
1463
1464 // PR13200: if it's a .return probe, we need to emit a *retprobe based
1465 // on the entrypc so here we only use prologue_end for non .return
1466 // probes (note however that .return probes still take advantage of
1467 // prologue_end: PR14436)
1468 if (!q->has_return)
1469 addr = funcs[0].prologue_end;
6b517475
JS
1470 }
1471 }
1472 else
1473 {
de797211
JL
1474 Dwarf_Line *address_line = dwarf_getsrc_die(cudie, addr);
1475 Dwarf_Addr address_line_addr = addr;
6b517475
JS
1476 if (address_line)
1477 {
bf5974d6
JL
1478 file = DWARF_LINESRC(address_line);
1479 line = DWARF_LINENO(address_line);
1480 address_line_addr = DWARF_LINEADDR(address_line);
6b517475
JS
1481 }
1482
1483 // Verify that a raw address matches the beginning of a
1484 // statement. This is a somewhat lame check that the address
1485 // is at the start of an assembly instruction. Mark probes are in the
1486 // middle of a macro and thus not strictly at a statement beginning.
1487 // Guru mode may override this check.
de797211 1488 if (!q->has_mark && (!address_line || address_line_addr != addr))
6b517475
JS
1489 {
1490 stringstream msg;
2a97f50b 1491 msg << _F("address %#" PRIx64 " does not match the beginning of a statement",
b530b5b3 1492 addr);
6b517475 1493 if (address_line)
de797211 1494 msg << _F(" (try %#" PRIx64 ")", address_line_addr);
6b517475 1495 else
b530b5b3
LB
1496 msg << _F(" (no line info found for '%s', in module '%s')",
1497 dw.cu_name().c_str(), dw.module_name.c_str());
6b517475 1498 if (! q->sess.guru_mode)
dc09353a 1499 throw SEMANTIC_ERROR(msg.str());
2713ea24 1500 else
6b517475
JS
1501 q->sess.print_warning(msg.str());
1502 }
1503 }
1504
1505 // Build a probe at this point
1506 query_statement(dw.function_name, file, line, scope, addr, q);
1507}
1508
f4faaf86
JS
1509static void
1510query_plt_statement(dwarf_query *q)
1511{
1512 assert (q->has_plt && q->has_statement_num);
1513
1514 Dwarf_Addr addr = q->statement_num_val;
1515 if (q->sess.verbose > 2)
1516 clog << "query_plt_statement 0x" << hex << addr << dec << endl;
1517
1518 // First adjust the raw address to dwfl's elf bias.
1519 Dwarf_Addr elf_bias;
1520 Elf *elf = dwfl_module_getelf (q->dw.module, &elf_bias);
1521 assert(elf);
1522 addr += elf_bias;
1523
1524 // Now compensate for the dw bias
1525 q->dw.get_module_dwarf(false, false);
1526 addr -= q->dw.module_bias;
1527
1528 // Build a probe at this point
1529 query_statement(q->plt_val, NULL, -1, NULL, addr, q);
1530}
1531
8096dd7d
JS
1532static void
1533query_label (string const & func,
1534 char const * label,
1535 char const * file,
1536 int line,
1537 Dwarf_Die *scope_die,
1538 Dwarf_Addr stmt_addr,
1539 dwarf_query * q)
1540{
6b517475
JS
1541 assert (q->has_statement_str || q->has_function_str);
1542
8096dd7d
JS
1543 // weed out functions whose decl_file isn't one of
1544 // the source files that we actually care about
6b517475 1545 if (q->spec_type != function_alone &&
8096dd7d
JS
1546 q->filtered_srcfiles.count(file) == 0)
1547 return;
1548
9891c543
JL
1549 // To help users, we create an intermediate probe point containing the final
1550 // function name, as well as the matched label. See query_callee() for
1551 // something similar, but with more comments.
1552 string canon_func = q->final_function_name(func, file, line);
1553
1554 string module = q->dw.module_name;
1555 if (q->has_process)
1556 module = path_remove_sysroot(q->sess, module);
8096dd7d 1557
9891c543
JL
1558 probe_point *pp = new probe_point(*q->base_loc);
1559 vector<probe_point::component*> pp_comps;
1560 vector<probe_point::component*>::iterator it;
1561 for (it = pp->components.begin(); it != pp->components.end(); ++it)
c72aa911 1562 {
9891c543
JL
1563 if ((*it)->functor == TOK_PROCESS || (*it)->functor == TOK_MODULE)
1564 pp_comps.push_back(new probe_point::component((*it)->functor,
1565 new literal_string(module)));
1566 else if ((*it)->functor == TOK_FUNCTION)
1567 pp_comps.push_back(new probe_point::component(TOK_FUNCTION,
1568 new literal_string(canon_func)));
1569 else if ((*it)->functor == TOK_LABEL)
1570 pp_comps.push_back(new probe_point::component(TOK_LABEL,
1571 new literal_string(label)));
1572 else
1573 pp_comps.push_back(*it);
c72aa911 1574 }
9891c543
JL
1575
1576 pp->components = pp_comps;
1577 dwarf_query q_label(*q);
1578 q_label.base_loc = pp;
1579 q_label.base_probe = new probe(q->base_probe, pp);
1580
1581 query_statement(func, file, line, scope_die, stmt_addr, &q_label);
8096dd7d
JS
1582}
1583
67959c62 1584static void
e772a6e7
JL
1585query_callee (base_func_info& callee,
1586 base_func_info& caller,
67959c62
JL
1587 stack<Dwarf_Addr> *callers,
1588 dwarf_query * q)
1589{
1590 assert (q->has_function_str);
e772a6e7
JL
1591 assert (q->has_callee || q->has_callees_num);
1592
1593 // OK, we found a callee for a targeted caller. To help users see the
1594 // derivation, let's add an intermediate probe to the chain. E.g. right now we
1595 // have .function(pattern).callee(pattern) (or .callees) and we want to add as
1596 // the next step .function(caller).callee(callee). This is the step that will
1597 // show up as well when in listing mode.
1598
1599 // To add this intermediate probe, we first create it and then create a new
1600 // dwarf_query with that probe as base.
1601
1602 // Build final names for caller and callee
1603 string canon_caller = q->final_function_name(caller.name, caller.decl_file,
1604 caller.decl_line);
1605 string canon_callee = q->final_function_name(callee.name, callee.decl_file,
1606 callee.decl_line);
1607
1608 // Build the intermediate probe. We replace the function arg with the final
1609 // caller, and the callee arg with the final callee. Also, since the
1610 // process(str) or module(str) currently in the base_loc is still the one the
1611 // user typed in (not necessarily the same as the absolute path we get in the
1612 // sole location), we also replace that (otherwise, function(str).* will
1613 // return a mishmash of absolute and relative paths in listing mode).
1614
1615 string module = q->dw.module_name;
1616 if (q->has_process)
1617 module = path_remove_sysroot(q->sess, module);
1618
1619 probe_point *pp = new probe_point(*q->base_loc);
1620 vector<probe_point::component*> pp_comps;
1621 vector<probe_point::component*>::iterator it;
1622 for (it = pp->components.begin(); it != pp->components.end(); ++it)
1623 {
1624 if ((*it)->functor == TOK_PROCESS || (*it)->functor == TOK_MODULE)
1625 pp_comps.push_back(new probe_point::component((*it)->functor,
1626 new literal_string(module)));
1627 else if ((*it)->functor == TOK_FUNCTION)
1628 pp_comps.push_back(new probe_point::component(TOK_FUNCTION,
1629 new literal_string(canon_caller)));
1630 else if ((*it)->functor == TOK_CALLEE || (*it)->functor == TOK_CALLEES)
1631 pp_comps.push_back(new probe_point::component(TOK_CALLEE,
1632 new literal_string(canon_callee)));
1633 else
1634 pp_comps.push_back(*it);
1635 }
1636
1637 // Finally create the new probe and the new query (use copy-constructor
1638 // instead of creating from scratch so we don't have to rebuild param map)
1639 pp->components = pp_comps;
1640 dwarf_query q_callee(*q);
1641 q_callee.base_loc = pp;
1642 q_callee.base_probe = new probe(q->base_probe, pp);
1643
1644 // Pass on the callers we'll need to add checks for
1645 q_callee.callers = callers;
1646
1647 query_statement(callee.name, callee.decl_file, callee.decl_line,
1648 &callee.die, callee.entrypc, &q_callee);
67959c62
JL
1649}
1650
7e1279ea 1651static void
3e961ba6 1652query_inline_instance_info (inline_instance_info & ii,
7e1279ea
FCE
1653 dwarf_query * q)
1654{
b6581717 1655 try
7e1279ea 1656 {
8f14e444
FCE
1657 assert (! q->has_return); // checked by caller already
1658 if (q->sess.verbose>2)
b530b5b3
LB
1659 clog << _F("querying entrypc %#" PRIx64 " of instance of inline '%s'\n",
1660 ii.entrypc, ii.name.c_str());
8f14e444
FCE
1661 query_statement (ii.name, ii.decl_file, ii.decl_line,
1662 &ii.die, ii.entrypc, q);
7e1279ea 1663 }
b6581717 1664 catch (semantic_error &e)
7e1279ea 1665 {
b6581717 1666 q->sess.print_error (e);
7e1279ea
FCE
1667 }
1668}
1669
1670static void
1671query_func_info (Dwarf_Addr entrypc,
bcc12710 1672 func_info & fi,
7e1279ea
FCE
1673 dwarf_query * q)
1674{
b6581717 1675 try
7e1279ea 1676 {
5a617dc6
JL
1677 // If it's a .return probe, we need to emit a *retprobe based on the
1678 // entrypc (PR13200). Note however that if prologue_end is valid,
1679 // dwarf_derived_probe will still take advantage of it by creating a new
1680 // probe there if necessary to pick up target vars (PR14436).
1681 if (fi.prologue_end == 0 || q->has_return)
1682 {
170ccf10 1683 q->prologue_end = fi.prologue_end;
5a617dc6
JL
1684 query_statement (fi.name, fi.decl_file, fi.decl_line,
1685 &fi.die, entrypc, q);
1686 }
b6581717 1687 else
5a617dc6 1688 {
5a617dc6
JL
1689 query_statement (fi.name, fi.decl_file, fi.decl_line,
1690 &fi.die, fi.prologue_end, q);
1691 }
7e1279ea 1692 }
b6581717 1693 catch (semantic_error &e)
7e1279ea 1694 {
b6581717 1695 q->sess.print_error (e);
7e1279ea
FCE
1696 }
1697}
1698
1699
bd4b874d 1700static void
de797211 1701query_srcfile_label (Dwarf_Addr addr, int lineno, dwarf_query * q)
bd4b874d 1702{
959ccfed
JL
1703 base_func_info_map_t bfis = q->filtered_all();
1704 base_func_info_map_t::iterator i;
1705 for (i = bfis.begin(); i != bfis.end(); ++i)
f09d0d1e
JS
1706 if (q->dw.die_has_pc (i->die, addr))
1707 q->dw.iterate_over_labels (&i->die, q->label_val, i->name,
1708 q, query_label);
bd4b874d
SC
1709}
1710
7e1279ea 1711static void
de797211 1712query_srcfile_line (Dwarf_Addr addr, int lineno, dwarf_query * q)
7e1279ea 1713{
8fb810d7
JL
1714 assert (q->has_statement_str || q->has_function_str);
1715 assert (q->spec_type == function_file_and_line);
20e4a32c 1716
959ccfed
JL
1717 base_func_info_map_t bfis = q->filtered_all();
1718 base_func_info_map_t::iterator i;
1719 for (i = bfis.begin(); i != bfis.end(); ++i)
897820ca 1720 {
3e961ba6 1721 if (q->dw.die_has_pc (i->die, addr))
959ccfed
JL
1722 {
1723 if (q->sess.verbose>3)
1724 clog << _("filtered DIE lands on srcfile\n");
1725 Dwarf_Die scope;
1726 q->dw.inner_die_containing_pc(i->die, addr, scope);
1727 query_statement (i->name, i->decl_file,
1728 lineno, // NB: not q->line !
1729 &scope, addr, q);
1730 }
20e4a32c 1731 }
7e1279ea
FCE
1732}
1733
7fdd3e2c
JS
1734bool
1735inline_instance_info::operator<(const inline_instance_info& other) const
1736{
1737 if (entrypc != other.entrypc)
1738 return entrypc < other.entrypc;
1739
1740 if (decl_line != other.decl_line)
1741 return decl_line < other.decl_line;
1742
1743 int cmp = name.compare(other.name);
c60517ca
FCE
1744
1745 if (!cmp)
1746 {
1747 assert (decl_file);
1748 assert (other.decl_file);
1749 cmp = strcmp(decl_file, other.decl_file);
1750 }
1751
7fdd3e2c
JS
1752 return cmp < 0;
1753}
1754
1755
4fa7b22b 1756static int
3956fccb 1757query_dwarf_inline_instance (Dwarf_Die * die, dwarf_query * q)
4fa7b22b 1758{
6b517475 1759 assert (q->has_statement_str || q->has_function_str);
4bda987e 1760 assert (!q->has_call && !q->has_return && !q->has_exported);
bd2b1e68 1761
39bcd429 1762 try
7a053d3b 1763 {
b0ee93c4 1764 if (q->sess.verbose>2)
b530b5b3 1765 clog << _F("selected inline instance of %s\n", q->dw.function_name.c_str());
7e1279ea 1766
6b517475
JS
1767 Dwarf_Addr entrypc;
1768 if (q->dw.die_entrypc (die, &entrypc))
1769 {
1770 inline_instance_info inl;
1771 inl.die = *die;
1772 inl.name = q->dw.function_name;
1773 inl.entrypc = entrypc;
1774 q->dw.function_file (&inl.decl_file);
1775 q->dw.function_line (&inl.decl_line);
1776
1777 // make sure that this inline hasn't already
1778 // been matched from a different CU
1779 if (q->inline_dupes.insert(inl).second)
1780 q->filtered_inlines.push_back(inl);
1781 }
7e1279ea
FCE
1782 return DWARF_CB_OK;
1783 }
1784 catch (const semantic_error& e)
1785 {
1786 q->sess.print_error (e);
1787 return DWARF_CB_ABORT;
1788 }
1789}
bb788f9f 1790
7e1279ea 1791static int
7d007451 1792query_dwarf_func (Dwarf_Die * func, dwarf_query * q)
7e1279ea 1793{
6b517475 1794 assert (q->has_statement_str || q->has_function_str);
bb788f9f 1795
bd25380d
JS
1796 // weed out functions whose decl_file isn't one of
1797 // the source files that we actually care about
6b517475 1798 if (q->spec_type != function_alone &&
bd25380d 1799 q->filtered_srcfiles.count(dwarf_decl_file(func)?:"") == 0)
8096dd7d 1800 return DWARF_CB_OK;
bd25380d 1801
7e1279ea
FCE
1802 try
1803 {
7e1279ea
FCE
1804 q->dw.focus_on_function (func);
1805
7d6d0afc
JS
1806 if (!q->dw.function_scope_matches(q->scopes))
1807 return DWARF_CB_OK;
1808
857bdfd1
JS
1809 // make sure that this function address hasn't
1810 // already been matched under an aliased name
1811 Dwarf_Addr addr;
1812 if (!q->dw.func_is_inline() &&
1813 dwarf_entrypc(func, &addr) == 0 &&
1814 !q->alias_dupes.insert(addr).second)
1815 return DWARF_CB_OK;
1816
4bda987e 1817 if (q->dw.func_is_inline () && (! q->has_call) && (! q->has_return) && (! q->has_exported))
7e1279ea 1818 {
4bda987e 1819 if (q->sess.verbose>3)
b530b5b3 1820 clog << _F("checking instances of inline %s\n", q->dw.function_name.c_str());
4bda987e 1821 q->dw.iterate_over_inline_instances (query_dwarf_inline_instance, q);
7e1279ea 1822 }
8f14e444
FCE
1823 else if (q->dw.func_is_inline () && (q->has_return)) // PR 11553
1824 {
1825 q->inlined_non_returnable.insert (q->dw.function_name);
1826 }
396afcee 1827 else if (!q->dw.func_is_inline () && (! q->has_inline))
20e4a32c 1828 {
4bda987e
SC
1829 if (q->has_exported && !q->dw.func_is_exported ())
1830 return DWARF_CB_OK;
6b517475 1831 if (q->sess.verbose>2)
b530b5b3 1832 clog << _F("selected function %s\n", q->dw.function_name.c_str());
6b517475
JS
1833
1834 func_info func;
1835 q->dw.function_die (&func.die);
1836 func.name = q->dw.function_name;
1837 q->dw.function_file (&func.decl_file);
1838 q->dw.function_line (&func.decl_line);
1839
1840 Dwarf_Addr entrypc;
1841 if (q->dw.function_entrypc (&entrypc))
1842 {
1843 func.entrypc = entrypc;
1844 q->filtered_functions.push_back (func);
1845 }
1846 /* else this function is fully inlined, just ignore it */
7e1279ea 1847 }
39bcd429 1848 return DWARF_CB_OK;
bd2b1e68 1849 }
39bcd429 1850 catch (const semantic_error& e)
bd2b1e68 1851 {
39bcd429
FCE
1852 q->sess.print_error (e);
1853 return DWARF_CB_ABORT;
bd2b1e68 1854 }
bd2b1e68
GH
1855}
1856
1857static int
5c378838 1858query_cu (Dwarf_Die * cudie, dwarf_query * q)
bd2b1e68 1859{
6b517475
JS
1860 assert (q->has_statement_str || q->has_function_str);
1861
85007c04 1862 if (pending_interrupts) return DWARF_CB_ABORT;
7a053d3b 1863
39bcd429 1864 try
bd2b1e68 1865 {
7e1279ea 1866 q->dw.focus_on_cu (cudie);
b5d77020 1867
b0ee93c4 1868 if (false && q->sess.verbose>2)
b530b5b3
LB
1869 clog << _F("focused on CU '%s', in module '%s'\n",
1870 q->dw.cu_name().c_str(), q->dw.module_name.c_str());
d9b516ca 1871
6b517475
JS
1872 q->filtered_srcfiles.clear();
1873 q->filtered_functions.clear();
1874 q->filtered_inlines.clear();
1875
1876 // In this path, we find "abstract functions", record
1877 // information about them, and then (depending on lineno
1878 // matching) possibly emit one or more of the function's
1879 // associated addresses. Unfortunately the control of this
1880 // cannot easily be turned inside out.
1881
1882 if (q->spec_type != function_alone)
39bcd429 1883 {
6b517475
JS
1884 // If we have a pattern string with a filename, we need
1885 // to elaborate the srcfile mask in question first.
1886 q->dw.collect_srcfiles_matching (q->file, q->filtered_srcfiles);
1887
1888 // If we have a file pattern and *no* srcfile matches, there's
1889 // no need to look further into this CU, so skip.
1890 if (q->filtered_srcfiles.empty())
1891 return DWARF_CB_OK;
1892 }
e4c58386 1893
6b517475
JS
1894 // Pick up [entrypc, name, DIE] tuples for all the functions
1895 // matching the query, and fill in the prologue endings of them
1896 // all in a single pass.
5898b6e1 1897 int rc = q->dw.iterate_over_functions (query_dwarf_func, q, q->function);
6b517475
JS
1898 if (rc != DWARF_CB_OK)
1899 q->query_done = true;
1900
277a2b96
JL
1901 if (!q->filtered_functions.empty() &&
1902 !q->has_statement_str && // PR 2608
1903 (q->sess.prologue_searching ||
1904 (q->has_process && !q->dw.has_valid_locs()))) // PR 6871 && PR 6941
1905 q->dw.resolve_prologue_endings (q->filtered_functions);
6b517475
JS
1906
1907 if (q->spec_type == function_file_and_line)
1908 {
58b070fb 1909 // .statement(...:NN) often gets mixed up with .function(...:NN)
2713ea24 1910 if (q->has_function_str)
b530b5b3 1911 q->sess.print_warning (_("For probing a particular line, use a "
b78a0fbb 1912 ".statement() probe, not .function()"),
af2e341f 1913 q->base_probe->tok);
58b070fb 1914
de797211 1915 void (* callback) (Dwarf_Addr, int, dwarf_query*) =
6b517475 1916 q->has_label ? query_srcfile_label : query_srcfile_line;
516e1e2c
JL
1917
1918 base_func_info_map_t bfis = q->filtered_all();
1919
1920 set<string>::const_iterator srcfile;
1921 for (srcfile = q->filtered_srcfiles.begin();
1922 srcfile != q->filtered_srcfiles.end(); ++srcfile)
1923 q->dw.iterate_over_srcfile_lines(srcfile->c_str(), q->linenos,
1924 q->lineno_type, bfis, callback, q);
6b517475
JS
1925 }
1926 else if (q->has_label)
1927 {
959ccfed
JL
1928 base_func_info_map_t bfis = q->filtered_all();
1929 base_func_info_map_t::iterator i;
1930 for (i = bfis.begin(); i != bfis.end(); ++i)
6b517475
JS
1931 q->dw.iterate_over_labels (&i->die, q->label_val, i->name,
1932 q, query_label);
39bcd429 1933 }
67959c62
JL
1934 else if (q->has_callee || q->has_callees_num)
1935 {
1936 // .callee(str) --> str, .callees[(N)] --> "*"
1937 string callee_val = q->has_callee ? q->callee_val : "*";
1938 long callees_num_val = q->has_callees_num ? q->callees_num_val : 1;
1939
1940 // NB: We filter functions that do not match the file here rather than
1941 // in query_callee because we only want the filtering to apply to the
1942 // first level, not to callees that are recursed into if
1943 // callees_num_val > 1.
959ccfed
JL
1944 base_func_info_map_t bfis = q->filtered_all();
1945 base_func_info_map_t::iterator i;
1946 for (i = bfis.begin(); i != bfis.end(); ++i)
67959c62
JL
1947 {
1948 if (q->spec_type != function_alone &&
1949 q->filtered_srcfiles.count(i->decl_file) == 0)
1950 continue;
1951 q->dw.iterate_over_callees (&i->die, callee_val,
1952 callees_num_val,
e772a6e7 1953 q, query_callee, *i);
67959c62
JL
1954 }
1955 }
6b517475
JS
1956 else
1957 {
1958 // Otherwise, simply probe all resolved functions.
1959 for (func_info_map_t::iterator i = q->filtered_functions.begin();
1960 i != q->filtered_functions.end(); ++i)
1961 query_func_info (i->entrypc, *i, q);
1962
1963 // And all inline instances (if we're not excluding inlines with ".call")
1964 if (! q->has_call)
1965 for (inline_instance_map_t::iterator i
1966 = q->filtered_inlines.begin(); i != q->filtered_inlines.end(); ++i)
1967 query_inline_instance_info (*i, q);
e772a6e7 1968 }
39bcd429 1969 return DWARF_CB_OK;
bd2b1e68 1970 }
39bcd429 1971 catch (const semantic_error& e)
bd2b1e68 1972 {
39bcd429
FCE
1973 q->sess.print_error (e);
1974 return DWARF_CB_ABORT;
bd2b1e68 1975 }
bd2b1e68
GH
1976}
1977
0ce64fb8 1978
4df79aaf
JS
1979void
1980dwarf_query::query_module_functions ()
1981{
1982 try
1983 {
1984 filtered_srcfiles.clear();
1985 filtered_functions.clear();
1986 filtered_inlines.clear();
1987
1988 // Collect all module functions so we know which CUs are interesting
1989 int rc = dw.iterate_single_function(query_dwarf_func, this, function);
1990 if (rc != DWARF_CB_OK)
1991 {
1992 query_done = true;
1993 return;
1994 }
1995
1996 set<void*> used_cus; // by cu->addr
1997 vector<Dwarf_Die> cus;
1998 Dwarf_Die cu_mem;
1999
959ccfed
JL
2000 base_func_info_map_t bfis = filtered_all();
2001 base_func_info_map_t::iterator i;
2002 for (i = bfis.begin(); i != bfis.end(); ++i)
4df79aaf
JS
2003 if (dwarf_diecu(&i->die, &cu_mem, NULL, NULL) &&
2004 used_cus.insert(cu_mem.addr).second)
2005 cus.push_back(cu_mem);
2006
2007 // Reset the dupes since we didn't actually collect them the first time
2008 alias_dupes.clear();
2009 inline_dupes.clear();
2010
2011 // Run the query again on the individual CUs
91bb9081
LB
2012 for (vector<Dwarf_Die>::iterator i = cus.begin(); i != cus.end(); ++i){
2013 rc = query_cu(&*i, this);
2014 if (rc != DWARF_CB_OK)
2015 {
2016 query_done = true;
2017 return;
2018 }
2019 }
4df79aaf
JS
2020 }
2021 catch (const semantic_error& e)
2022 {
2023 sess.print_error (e);
2024 }
2025}
2026
2027
5f0a03a6
JK
2028static void
2029validate_module_elf (Dwfl_Module *mod, const char *name, base_query *q)
2030{
2031 // Validate the machine code in this elf file against the
2032 // session machine. This is important, in case the wrong kind
2033 // of debuginfo is being automagically processed by elfutils.
2034 // While we can tell i686 apart from x86-64, unfortunately
2035 // we can't help confusing i586 vs i686 (both EM_386).
2036
2037 Dwarf_Addr bias;
2038 // We prefer dwfl_module_getdwarf to dwfl_module_getelf here,
2039 // because dwfl_module_getelf can force costly section relocations
2040 // we don't really need, while either will do for this purpose.
2041 Elf* elf = (dwarf_getelf (dwfl_module_getdwarf (mod, &bias))
2042 ?: dwfl_module_getelf (mod, &bias));
2043
2044 GElf_Ehdr ehdr_mem;
2045 GElf_Ehdr* em = gelf_getehdr (elf, &ehdr_mem);
e1c3b13a 2046 if (em == 0) { DWFL_ASSERT ("dwfl_getehdr", dwfl_errno()); }
d8f31d0a 2047 assert(em);
5f0a03a6
JK
2048 int elf_machine = em->e_machine;
2049 const char* debug_filename = "";
2050 const char* main_filename = "";
2051 (void) dwfl_module_info (mod, NULL, NULL,
2052 NULL, NULL, NULL,
2053 & main_filename,
2054 & debug_filename);
2055 const string& sess_machine = q->sess.architecture;
756c9462
FCE
2056
2057 string expect_machine; // to match sess.machine (i.e., kernel machine)
2058 string expect_machine2;
5f0a03a6 2059
d27e6fd5 2060 // NB: See also the 'uname -m' squashing done in main.cxx.
5f0a03a6
JK
2061 switch (elf_machine)
2062 {
756c9462
FCE
2063 // x86 and ppc are bi-architecture; a 64-bit kernel
2064 // can normally run either 32-bit or 64-bit *userspace*.
2065 case EM_386:
2066 expect_machine = "i?86";
2067 if (! q->has_process) break; // 32-bit kernel/module
2068 /* FALLSTHROUGH */
2069 case EM_X86_64:
2070 expect_machine2 = "x86_64";
2071 break;
2072 case EM_PPC:
756c9462 2073 case EM_PPC64:
5a1c472e 2074 expect_machine = "powerpc";
756c9462 2075 break;
3fe7d888 2076 case EM_S390: expect_machine = "s390"; break;
5f0a03a6 2077 case EM_IA_64: expect_machine = "ia64"; break;
d27e6fd5 2078 case EM_ARM: expect_machine = "arm*"; break;
86aae990 2079 case EM_AARCH64: expect_machine = "arm64"; break;
5f0a03a6
JK
2080 // XXX: fill in some more of these
2081 default: expect_machine = "?"; break;
2082 }
2083
2084 if (! debug_filename) debug_filename = main_filename;
2085 if (! debug_filename) debug_filename = name;
2086
756c9462
FCE
2087 if (fnmatch (expect_machine.c_str(), sess_machine.c_str(), 0) != 0 &&
2088 fnmatch (expect_machine2.c_str(), sess_machine.c_str(), 0) != 0)
5f0a03a6
JK
2089 {
2090 stringstream msg;
b530b5b3
LB
2091 msg << _F("ELF machine %s|%s (code %d) mismatch with target %s in '%s'",
2092 expect_machine.c_str(), expect_machine2.c_str(), elf_machine,
2093 sess_machine.c_str(), debug_filename);
dc09353a 2094 throw SEMANTIC_ERROR(msg.str ());
5f0a03a6
JK
2095 }
2096
b57082de 2097 if (q->sess.verbose>1)
b78a0fbb 2098 clog << _F("focused on module '%s' = [%#" PRIx64 "-%#" PRIx64 ", bias %#" PRIx64
b530b5b3
LB
2099 " file %s ELF machine %s|%s (code %d)\n",
2100 q->dw.module_name.c_str(), q->dw.module_start, q->dw.module_end,
2101 q->dw.module_bias, debug_filename, expect_machine.c_str(),
2102 expect_machine2.c_str(), elf_machine);
5f0a03a6 2103}
1d3a40b6 2104
91af0778
FCE
2105
2106
2107static Dwarf_Addr
2108lookup_symbol_address (Dwfl_Module *m, const char* wanted)
2109{
2110 int syments = dwfl_module_getsymtab(m);
2111 assert(syments);
2112 for (int i = 1; i < syments; ++i)
2113 {
2114 GElf_Sym sym;
2115 const char *name = dwfl_module_getsym(m, i, &sym, NULL);
2116 if (name != NULL && strcmp(name, wanted) == 0)
2117 return sym.st_value;
2118 }
2119
2120 return 0;
2121}
2122
2123
2124
bd2b1e68 2125static int
b8da0ad1 2126query_module (Dwfl_Module *mod,
91af0778 2127 void **,
b8da0ad1 2128 const char *name,
6f4c1275 2129 Dwarf_Addr addr,
06de3a04 2130 base_query *q)
bd2b1e68 2131{
39bcd429 2132 try
e38d6504 2133 {
91af0778
FCE
2134 module_info* mi = q->sess.module_cache->cache[name];
2135 if (mi == 0)
2136 {
2137 mi = q->sess.module_cache->cache[name] = new module_info(name);
2138
6f4c1275
FCE
2139 mi->mod = mod;
2140 mi->addr = addr;
91af0778 2141
6f4c1275
FCE
2142 const char* debug_filename = "";
2143 const char* main_filename = "";
2144 (void) dwfl_module_info (mod, NULL, NULL,
2145 NULL, NULL, NULL,
2146 & main_filename,
2147 & debug_filename);
2148
ab3ed72d 2149 if (debug_filename || main_filename)
91af0778 2150 {
6f4c1275
FCE
2151 mi->elf_path = debug_filename ?: main_filename;
2152 }
2153 else if (name == TOK_KERNEL)
2154 {
2155 mi->dwarf_status = info_absent;
91af0778 2156 }
91af0778
FCE
2157 }
2158 // OK, enough of that module_info caching business.
2159
5f0a03a6 2160 q->dw.focus_on_module(mod, mi);
d9b516ca 2161
39bcd429
FCE
2162 // If we have enough information in the pattern to skip a module and
2163 // the module does not match that information, return early.
b8da0ad1 2164 if (!q->dw.module_name_matches(q->module_val))
85007c04 2165 return pending_interrupts ? DWARF_CB_ABORT : DWARF_CB_OK;
0cbbf9d1
FCE
2166
2167 // Don't allow module("*kernel*") type expressions to match the
2168 // elfutils module "kernel", which we refer to in the probe
2169 // point syntax exclusively as "kernel.*".
2170 if (q->dw.module_name == TOK_KERNEL && ! q->has_kernel)
85007c04 2171 return pending_interrupts ? DWARF_CB_ABORT : DWARF_CB_OK;
b5d77020 2172
5f0a03a6
JK
2173 if (mod)
2174 validate_module_elf(mod, name, q);
2175 else
91af0778
FCE
2176 assert(q->has_kernel); // and no vmlinux to examine
2177
2178 if (q->sess.verbose>2)
b530b5b3 2179 cerr << _F("focused on module '%s'\n", q->dw.module_name.c_str());
91af0778
FCE
2180
2181
2182 // Collect a few kernel addresses. XXX: these belong better
2183 // to the sess.module_info["kernel"] struct.
2184 if (q->dw.module_name == TOK_KERNEL)
c931ec8a 2185 {
91af0778
FCE
2186 if (! q->sess.sym_kprobes_text_start)
2187 q->sess.sym_kprobes_text_start = lookup_symbol_address (mod, "__kprobes_text_start");
2188 if (! q->sess.sym_kprobes_text_end)
2189 q->sess.sym_kprobes_text_end = lookup_symbol_address (mod, "__kprobes_text_end");
2190 if (! q->sess.sym_stext)
2191 q->sess.sym_stext = lookup_symbol_address (mod, "_stext");
c931ec8a
FCE
2192 }
2193
47e226ed
SC
2194 // We either have a wildcard or an unresolved library
2195 if (q->has_library && (contains_glob_chars (q->path)
2196 || q->path.find('/') == string::npos))
84c84ac4
SC
2197 // handle .library(GLOB)
2198 q->dw.iterate_over_libraries (&q->query_library_callback, q);
576eaefe
SC
2199 // .plt is translated to .plt.statement(N). We only want to iterate for the
2200 // .plt case
2201 else if (q->has_plt && ! q->has_statement)
070764c0
JL
2202 {
2203 q->dw.iterate_over_plt (q, &q->query_plt_callback);
2204 q->visited_modules.insert(name);
2205 }
84c84ac4 2206 else
070764c0
JL
2207 {
2208 // search the module for matches of the probe point.
2209 q->handle_query_module();
2210 q->visited_modules.insert(name);
2211 }
bb788f9f 2212
b8da0ad1 2213 // If we know that there will be no more matches, abort early.
85007c04 2214 if (q->dw.module_name_final_match(q->module_val) || pending_interrupts)
b8da0ad1
FCE
2215 return DWARF_CB_ABORT;
2216 else
2217 return DWARF_CB_OK;
7a053d3b 2218 }
39bcd429 2219 catch (const semantic_error& e)
bd2b1e68 2220 {
39bcd429
FCE
2221 q->sess.print_error (e);
2222 return DWARF_CB_ABORT;
bd2b1e68 2223 }
bd2b1e68
GH
2224}
2225
35d4ab18 2226
84c84ac4 2227void
bbbc7241 2228base_query::query_library_callback (base_query *me, const char *data)
84c84ac4 2229{
5d5bd369 2230 me->query_library (data);
84c84ac4
SC
2231}
2232
2233
2cbcfa9c 2234string
51d6bda3
SC
2235query_one_library (const char *library, dwflpp & dw,
2236 const string user_lib, probe * base_probe, probe_point *base_loc,
2237 vector<derived_probe *> & results)
84c84ac4 2238{
47e226ed 2239 if (dw.function_name_matches_pattern(library, "*" + user_lib))
84c84ac4 2240 {
05fb3e0c
WF
2241 string library_path = find_executable (library, "", dw.sess.sysenv,
2242 "LD_LIBRARY_PATH");
84c84ac4
SC
2243 probe_point* specific_loc = new probe_point(*base_loc);
2244 specific_loc->optional = true;
ef0943df 2245 specific_loc->from_glob = true;
84c84ac4
SC
2246 vector<probe_point::component*> derived_comps;
2247
5d25dfb6
JL
2248 // Create new probe point for the matching library. This is what will be
2249 // shown in listing mode. Also replace the process(str) with the real
2250 // absolute path rather than keeping what the user typed in.
84c84ac4
SC
2251 vector<probe_point::component*>::iterator it;
2252 for (it = specific_loc->components.begin();
2253 it != specific_loc->components.end(); ++it)
5d25dfb6
JL
2254 if ((*it)->functor == TOK_PROCESS)
2255 derived_comps.push_back(new probe_point::component(TOK_PROCESS,
2256 new literal_string(path_remove_sysroot(dw.sess, dw.module_name))));
2257 else if ((*it)->functor == TOK_LIBRARY)
84c84ac4
SC
2258 derived_comps.push_back(new probe_point::component(TOK_LIBRARY,
2259 new literal_string(library_path)));
2260 else
2261 derived_comps.push_back(*it);
2262 probe_point* derived_loc = new probe_point(*specific_loc);
2263 derived_loc->components = derived_comps;
8159bf55 2264 probe *new_base = new probe (new probe (base_probe, specific_loc), derived_loc);
51d6bda3
SC
2265 derive_probes(dw.sess, new_base, results);
2266 if (dw.sess.verbose > 2)
2cbcfa9c
JL
2267 clog << _("module=") << library_path << endl;
2268 return library_path;
84c84ac4 2269 }
2cbcfa9c 2270 return "";
84c84ac4
SC
2271}
2272
2273
51d6bda3
SC
2274void
2275dwarf_query::query_library (const char *library)
2276{
2cbcfa9c
JL
2277 string library_path =
2278 query_one_library (library, dw, user_lib, base_probe, base_loc, results);
2279 if (!library_path.empty())
2280 visited_modules.insert(library_path);
51d6bda3
SC
2281}
2282
576eaefe
SC
2283struct plt_expanding_visitor: public var_expanding_visitor
2284{
2285 plt_expanding_visitor(const string & entry):
2286 entry (entry)
2287 {
2288 }
2289 const string & entry;
2290
2291 void visit_target_symbol (target_symbol* e);
2292};
2293
2294
2295void
45cdb40e 2296base_query::query_plt_callback (base_query *me, const char *entry, size_t address)
576eaefe 2297{
576eaefe
SC
2298 if (me->dw.function_name_matches_pattern (entry, me->plt_val))
2299 me->query_plt (entry, address);
7f4964f1 2300 me->dw.mod_info->plt_funcs.insert(entry);
576eaefe
SC
2301}
2302
2303
2304void
2305query_one_plt (const char *entry, long addr, dwflpp & dw,
2306 probe * base_probe, probe_point *base_loc,
2307 vector<derived_probe *> & results)
2308{
2309 probe_point* specific_loc = new probe_point(*base_loc);
576eaefe
SC
2310 vector<probe_point::component*> derived_comps;
2311
2312 if (dw.sess.verbose > 2)
2313 clog << _F("plt entry=%s\n", entry);
2314
576eaefe
SC
2315 vector<probe_point::component*>::iterator it;
2316 for (it = specific_loc->components.begin();
2317 it != specific_loc->components.end(); ++it)
2318 if ((*it)->functor == TOK_PLT)
3d372d6b 2319 {
8159bf55
FCE
2320 *it = new probe_point::component(TOK_PLT,
2321 new literal_string(entry));
3d372d6b
SC
2322 derived_comps.push_back(*it);
2323 derived_comps.push_back(new probe_point::component(TOK_STATEMENT,
ac7c8669 2324 new literal_number(addr, true)));
3d372d6b 2325 }
576eaefe
SC
2326 else
2327 derived_comps.push_back(*it);
2328 probe_point* derived_loc = new probe_point(*specific_loc);
2329 derived_loc->components = derived_comps;
02c34e38
FCE
2330 probe *new_base = new probe (new probe (base_probe, specific_loc),
2331 derived_loc);
576eaefe
SC
2332 string e = string(entry);
2333 plt_expanding_visitor pltv (e);
2334 pltv.replace (new_base->body);
2335 derive_probes(dw.sess, new_base, results);
2336}
2337
2338
2339void
2340dwarf_query::query_plt (const char *entry, size_t address)
2341{
2342 query_one_plt (entry, address, dw, base_probe, base_loc, results);
2343}
51d6bda3 2344
435f53a7
FCE
2345// This would more naturally fit into elaborate.cxx:semantic_pass_symbols,
2346// but the needed declaration for module_cache is not available there.
2347// Nor for that matter in session.cxx. Only in this CU is that field ever
2348// set (in query_module() above), so we clean it up here too.
2349static void
2350delete_session_module_cache (systemtap_session& s)
2351{
2352 if (s.module_cache) {
2353 if (s.verbose > 3)
b530b5b3 2354 clog << _("deleting module_cache") << endl;
435f53a7
FCE
2355 delete s.module_cache;
2356 s.module_cache = 0;
2357 }
2358}
2359
2360
de688825 2361struct dwarf_var_expanding_visitor: public var_expanding_visitor
35d4ab18 2362{
77de5e9e 2363 dwarf_query & q;
bcc12710 2364 Dwarf_Die *scope_die;
77de5e9e 2365 Dwarf_Addr addr;
8c819921 2366 block *add_block;
2260f4e3 2367 block *add_call_probe; // synthesized from .return probes with saved $vars
1630966a
JS
2368 // NB: tids are not always collected in add_block & add_call_probe, because
2369 // gen_kretprobe_saved_return doesn't need them. Thus we need these extra
2370 // *_tid bools for gen_mapped_saved_return to tell what's there.
8cc799a5 2371 bool add_block_tid, add_call_probe_tid;
af234c40
JS
2372 unsigned saved_longs, saved_strings; // data saved within kretprobes
2373 map<std::string, expression *> return_ts_map;
729455a7 2374 vector<Dwarf_Die> scopes;
3689db05 2375 // probe counter name -> pointer of associated probe
698de6cc 2376 std::set<derived_probe*> perf_counter_refs;
b95e2b79 2377 bool visited;
77de5e9e 2378
de688825 2379 dwarf_var_expanding_visitor(dwarf_query & q, Dwarf_Die *sd, Dwarf_Addr a):
af234c40 2380 q(q), scope_die(sd), addr(a), add_block(NULL), add_call_probe(NULL),
8cc799a5 2381 add_block_tid(false), add_call_probe_tid(false),
af234c40 2382 saved_longs(0), saved_strings(0), visited(false) {}
277c21bc 2383 expression* gen_mapped_saved_return(expression* e, const string& name);
140be17a 2384 expression* gen_kretprobe_saved_return(expression* e);
a7999c82
JS
2385 void visit_target_symbol_saved_return (target_symbol* e);
2386 void visit_target_symbol_context (target_symbol* e);
d7f3e0c5 2387 void visit_target_symbol (target_symbol* e);
bd1fcbad 2388 void visit_atvar_op (atvar_op* e);
c24447be 2389 void visit_cast_op (cast_op* e);
8cc799a5 2390 void visit_entry_op (entry_op* e);
3689db05 2391 void visit_perf_op (perf_op* e);
729455a7
JS
2392private:
2393 vector<Dwarf_Die>& getscopes(target_symbol *e);
77de5e9e
GH
2394};
2395
2396
de688825 2397unsigned var_expanding_visitor::tick = 0;
77de5e9e 2398
a50de939 2399
74fe61bc 2400var_expanding_visitor::var_expanding_visitor (): op()
a50de939
DS
2401{
2402 // FIXME: for the time being, by default we only support plain '$foo
2403 // = bar', not '+=' or any other op= variant. This is fixable, but a
2404 // bit ugly.
2405 //
2406 // If derived classes desire to add additional operator support, add
2407 // new operators to this list in the derived class constructor.
2408 valid_ops.insert ("=");
2409}
2410
2411
87214add
JS
2412bool
2413var_expanding_visitor::rewrite_lvalue(const token* tok, const std::string& eop,
2414 expression*& lvalue, expression*& rvalue)
77de5e9e 2415{
e57b735a
GH
2416 // Our job would normally be to require() the left and right sides
2417 // into a new assignment. What we're doing is slightly trickier:
2418 // we're pushing a functioncall** onto a stack, and if our left
2419 // child sets the functioncall* for that value, we're going to
2420 // assume our left child was a target symbol -- transformed into a
2421 // set_target_foo(value) call, and it wants to take our right child
2422 // as the argument "value".
2423 //
2424 // This is why some people claim that languages with
2425 // constructor-decomposing case expressions have a leg up on
2426 // visitors.
2427
2428 functioncall *fcall = NULL;
d9b516ca 2429
a50de939 2430 // Let visit_target_symbol know what operator it should handle.
87214add
JS
2431 const string* old_op = op;
2432 op = &eop;
a50de939 2433
e57b735a 2434 target_symbol_setter_functioncalls.push (&fcall);
87214add 2435 replace (lvalue);
e57b735a 2436 target_symbol_setter_functioncalls.pop ();
87214add
JS
2437 replace (rvalue);
2438
2439 op = old_op;
e57b735a
GH
2440
2441 if (fcall != NULL)
77de5e9e 2442 {
e57b735a
GH
2443 // Our left child is informing us that it was a target variable
2444 // and it has been replaced with a set_target_foo() function
2445 // call; we are going to provide that function call -- with the
2446 // right child spliced in as sole argument -- in place of
de688825 2447 // ourselves, in the var expansion we're in the middle of making.
e57b735a 2448
87214add 2449 if (valid_ops.find (eop) == valid_ops.end ())
a50de939
DS
2450 {
2451 // Build up a list of supported operators.
2452 string ops;
2453 std::set<string>::iterator i;
b530b5b3 2454 int valid_ops_size = 0;
a50de939 2455 for (i = valid_ops.begin(); i != valid_ops.end(); i++)
b530b5b3 2456 {
a50de939 2457 ops += " " + *i + ",";
b530b5b3
LB
2458 valid_ops_size++;
2459 }
a50de939
DS
2460 ops.resize(ops.size() - 1); // chop off the last ','
2461
2462 // Throw the error.
dc09353a 2463 throw SEMANTIC_ERROR (_NF("Only the following assign operator is implemented on target variables: %s",
1e41115c 2464 "Only the following assign operators are implemented on target variables: %s",
52c2652f 2465 valid_ops_size, ops.c_str()), tok);
b530b5b3 2466
a50de939 2467 }
e57b735a 2468
87214add
JS
2469 assert (lvalue == fcall);
2470 if (rvalue)
2471 fcall->args.push_back (rvalue);
4ed05b15 2472 provide (fcall);
87214add 2473 return true;
77de5e9e 2474 }
e57b735a 2475 else
87214add
JS
2476 return false;
2477}
2478
2479
2480void
2481var_expanding_visitor::visit_assignment (assignment* e)
2482{
2483 if (!rewrite_lvalue (e->tok, e->op, e->left, e->right))
2484 provide (e);
2485}
2486
2487
2488void
2489var_expanding_visitor::visit_pre_crement (pre_crement* e)
2490{
2491 expression *dummy = NULL;
2492 if (!rewrite_lvalue (e->tok, e->op, e->operand, dummy))
2493 provide (e);
2494}
2495
2496
2497void
2498var_expanding_visitor::visit_post_crement (post_crement* e)
2499{
2500 expression *dummy = NULL;
2501 if (!rewrite_lvalue (e->tok, e->op, e->operand, dummy))
2502 provide (e);
2503}
2504
2505
2506void
2507var_expanding_visitor::visit_delete_statement (delete_statement* s)
2508{
2509 string fakeop = "delete";
2510 expression *dummy = NULL;
2511 if (!rewrite_lvalue (s->tok, fakeop, s->value, dummy))
2512 provide (s);
e57b735a 2513}
d9b516ca 2514
d7f3e0c5 2515
30263a73
FCE
2516void
2517var_expanding_visitor::visit_defined_op (defined_op* e)
2518{
2519 bool resolved = true;
2520
2521 defined_ops.push (e);
2522 try {
0fb0cac9 2523 replace (e->operand);
30263a73 2524
c69a87e0 2525 // NB: Formerly, we had some curious cases to consider here, depending on what
30263a73 2526 // various visit_target_symbol() implementations do for successful or
c69a87e0
FCE
2527 // erroneous resolutions. Some would signal a visit_target_symbol failure
2528 // with an exception, with a set flag within the target_symbol, or nothing
2529 // at all.
30263a73 2530 //
c69a87e0
FCE
2531 // Now, failures always have to be signalled with a
2532 // saved_conversion_error being chained to the target_symbol.
2533 // Successes have to result in an attempted rewrite of the
850bfddd 2534 // target_symbol (via provide()).
780f11ff 2535 //
c69a87e0
FCE
2536 // Edna Mode: "no capes". fche: "no exceptions".
2537
30263a73
FCE
2538 // dwarf stuff: success: rewrites to a function; failure: retains target_symbol, sets saved_conversion_error
2539 //
2540 // sdt-kprobes sdt.h: success: string or functioncall; failure: semantic_error
2541 //
2542 // sdt-uprobes: success: string or no op; failure: no op; expect derived/synthetic
2543 // dwarf probe to take care of it.
2544 // But this is rather unhelpful. So we rig the sdt_var_expanding_visitor
2545 // to pass through @defined() to the synthetic dwarf probe.
780f11ff 2546 //
30263a73
FCE
2547 // utrace: success: rewrites to function; failure: semantic_error
2548 //
850bfddd 2549 // procfs: success: rewrites to function; failure: semantic_error
30263a73 2550
0fb0cac9
JS
2551 target_symbol* tsym = dynamic_cast<target_symbol*> (e->operand);
2552 if (tsym && tsym->saved_conversion_error) // failing
30263a73 2553 resolved = false;
0fb0cac9 2554 else if (tsym) // unresolved but not marked failing
b7aedf26 2555 {
780f11ff
JS
2556 // There are some visitors that won't touch certain target_symbols,
2557 // e.g. dwarf_var_expanding_visitor won't resolve @cast. We should
2558 // leave it for now so some other visitor can have a chance.
b7aedf26
JS
2559 provide (e);
2560 return;
2561 }
30263a73
FCE
2562 else // resolved, rewritten to some other expression type
2563 resolved = true;
780f11ff 2564 } catch (const semantic_error& e) {
c69a87e0 2565 assert (0); // should not happen
30263a73
FCE
2566 }
2567 defined_ops.pop ();
2568
2569 literal_number* ln = new literal_number (resolved ? 1 : 0);
2570 ln->tok = e->tok;
2571 provide (ln);
2572}
2573
2574
5f36109e
JS
2575struct dwarf_pretty_print
2576{
2577 dwarf_pretty_print (dwflpp& dw, vector<Dwarf_Die>& scopes, Dwarf_Addr pc,
2578 const string& local, bool userspace_p,
2579 const target_symbol& e):
d19a9a82
JS
2580 dw(dw), local(local), scopes(scopes), pc(pc), pointer(NULL),
2581 userspace_p(userspace_p), deref_p(true)
5f36109e
JS
2582 {
2583 init_ts (e);
2584 dw.type_die_for_local (scopes, pc, local, ts, &base_type);
2585 }
2586
2587 dwarf_pretty_print (dwflpp& dw, Dwarf_Die *scope_die, Dwarf_Addr pc,
2588 bool userspace_p, const target_symbol& e):
d19a9a82
JS
2589 dw(dw), scopes(1, *scope_die), pc(pc), pointer(NULL),
2590 userspace_p(userspace_p), deref_p(true)
5f36109e
JS
2591 {
2592 init_ts (e);
2593 dw.type_die_for_return (&scopes[0], pc, ts, &base_type);
2594 }
2595
2596 dwarf_pretty_print (dwflpp& dw, Dwarf_Die *type_die, expression* pointer,
d19a9a82 2597 bool deref_p, bool userspace_p, const target_symbol& e):
5f36109e 2598 dw(dw), pc(0), pointer(pointer), pointer_type(*type_die),
d19a9a82 2599 userspace_p(userspace_p), deref_p(deref_p)
5f36109e
JS
2600 {
2601 init_ts (e);
2602 dw.type_die_for_pointer (type_die, ts, &base_type);
2603 }
2604
2605 functioncall* expand ();
ce83ff57 2606 ~dwarf_pretty_print () { delete ts; }
5f36109e
JS
2607
2608private:
2609 dwflpp& dw;
2610 target_symbol* ts;
7d11d8c9 2611 bool print_full;
5f36109e
JS
2612 Dwarf_Die base_type;
2613
2614 string local;
2615 vector<Dwarf_Die> scopes;
2616 Dwarf_Addr pc;
2617
2618 expression* pointer;
2619 Dwarf_Die pointer_type;
2620
d19a9a82 2621 const bool userspace_p, deref_p;
5f36109e
JS
2622
2623 void recurse (Dwarf_Die* type, target_symbol* e,
7d11d8c9 2624 print_format* pf, bool top=false);
600551ca
JS
2625 void recurse_bitfield (Dwarf_Die* type, target_symbol* e,
2626 print_format* pf);
5f36109e 2627 void recurse_base (Dwarf_Die* type, target_symbol* e,
7d11d8c9 2628 print_format* pf);
5f36109e 2629 void recurse_array (Dwarf_Die* type, target_symbol* e,
7d11d8c9 2630 print_format* pf, bool top);
5f36109e 2631 void recurse_pointer (Dwarf_Die* type, target_symbol* e,
7d11d8c9 2632 print_format* pf, bool top);
5f36109e 2633 void recurse_struct (Dwarf_Die* type, target_symbol* e,
7d11d8c9 2634 print_format* pf, bool top);
5f36109e 2635 void recurse_struct_members (Dwarf_Die* type, target_symbol* e,
7d11d8c9 2636 print_format* pf, int& count);
bbee5bb8 2637 bool print_chars (Dwarf_Die* type, target_symbol* e, print_format* pf);
5f36109e
JS
2638
2639 void init_ts (const target_symbol& e);
2640 expression* deref (target_symbol* e);
c55ea10d 2641 bool push_deref (print_format* pf, const string& fmt, target_symbol* e);
5f36109e
JS
2642};
2643
2644
2645void
2646dwarf_pretty_print::init_ts (const target_symbol& e)
2647{
2648 // Work with a new target_symbol so we can modify arguments
2649 ts = new target_symbol (e);
2650
2651 if (ts->addressof)
dc09353a 2652 throw SEMANTIC_ERROR(_("cannot take address of pretty-printed variable"), ts->tok);
5f36109e
JS
2653
2654 if (ts->components.empty() ||
2655 ts->components.back().type != target_symbol::comp_pretty_print)
dc09353a 2656 throw SEMANTIC_ERROR(_("invalid target_symbol for pretty-print"), ts->tok);
7d11d8c9 2657 print_full = ts->components.back().member.length() > 1;
5f36109e
JS
2658 ts->components.pop_back();
2659}
2660
2661
2662functioncall*
2663dwarf_pretty_print::expand ()
2664{
2665 static unsigned tick = 0;
2666
2667 // function pretty_print_X([pointer], [arg1, arg2, ...]) {
7d11d8c9
JS
2668 // try {
2669 // return sprintf("{.foo=...}", (ts)->foo, ...)
2670 // } catch {
2671 // return "ERROR"
2672 // }
5f36109e
JS
2673 // }
2674
2675 // Create the function decl and call.
2676
2677 functiondecl *fdecl = new functiondecl;
2678 fdecl->tok = ts->tok;
2679 fdecl->synthetic = true;
2680 fdecl->name = "_dwarf_pretty_print_" + lex_cast(tick++);
2681 fdecl->type = pe_string;
2682
2683 functioncall* fcall = new functioncall;
2684 fcall->tok = ts->tok;
2685 fcall->function = fdecl->name;
140be17a 2686 fcall->type = pe_string;
5f36109e
JS
2687
2688 // If there's a <pointer>, replace it with a new var and make that
2689 // the first function argument.
2690 if (pointer)
2691 {
2692 vardecl *v = new vardecl;
2693 v->type = pe_long;
2694 v->name = "pointer";
2695 v->tok = ts->tok;
2696 fdecl->formal_args.push_back (v);
2697 fcall->args.push_back (pointer);
2698
2699 symbol* sym = new symbol;
2700 sym->tok = ts->tok;
2701 sym->name = v->name;
5f36109e
JS
2702 pointer = sym;
2703 }
2704
2705 // For each expression argument, replace it with a function argument.
2706 for (unsigned i = 0; i < ts->components.size(); ++i)
2707 if (ts->components[i].type == target_symbol::comp_expression_array_index)
2708 {
2709 vardecl *v = new vardecl;
2710 v->type = pe_long;
2711 v->name = "index" + lex_cast(i);
2712 v->tok = ts->tok;
2713 fdecl->formal_args.push_back (v);
2714 fcall->args.push_back (ts->components[i].expr_index);
2715
2716 symbol* sym = new symbol;
2717 sym->tok = ts->tok;
2718 sym->name = v->name;
5f36109e
JS
2719 ts->components[i].expr_index = sym;
2720 }
2721
2722 // Create the return sprintf.
1c922ad7 2723 print_format* pf = print_format::create(ts->tok, "sprintf");
5f36109e
JS
2724 return_statement* rs = new return_statement;
2725 rs->tok = ts->tok;
2726 rs->value = pf;
5f36109e
JS
2727
2728 // Recurse into the actual values.
7d11d8c9 2729 recurse (&base_type, ts, pf, true);
5f36109e
JS
2730 pf->components = print_format::string_to_components(pf->raw_components);
2731
7d11d8c9
JS
2732 // Create the try-catch net
2733 try_block* tb = new try_block;
2734 tb->tok = ts->tok;
2735 tb->try_block = rs;
2736 tb->catch_error_var = 0;
2737 return_statement* rs2 = new return_statement;
2738 rs2->tok = ts->tok;
2739 rs2->value = new literal_string ("ERROR");
2740 rs2->value->tok = ts->tok;
2741 tb->catch_block = rs2;
2742 fdecl->body = tb;
2743
f8809d54 2744 fdecl->join (dw.sess);
5f36109e
JS
2745 return fcall;
2746}
2747
2748
2749void
2750dwarf_pretty_print::recurse (Dwarf_Die* start_type, target_symbol* e,
7d11d8c9 2751 print_format* pf, bool top)
5f36109e
JS
2752{
2753 Dwarf_Die type;
2754 dw.resolve_unqualified_inner_typedie (start_type, &type, e);
2755
2756 switch (dwarf_tag(&type))
2757 {
2758 default:
2759 // XXX need a warning?
2760 // throw semantic_error ("unsupported type (tag " + lex_cast(dwarf_tag(&type))
2761 // + ") for " + dwarf_type_name(&type), e->tok);
2762 pf->raw_components.append("?");
2763 break;
2764
2765 case DW_TAG_enumeration_type:
2766 case DW_TAG_base_type:
7d11d8c9 2767 recurse_base (&type, e, pf);
5f36109e
JS
2768 break;
2769
2770 case DW_TAG_array_type:
7d11d8c9 2771 recurse_array (&type, e, pf, top);
5f36109e
JS
2772 break;
2773
2774 case DW_TAG_pointer_type:
2775 case DW_TAG_reference_type:
2776 case DW_TAG_rvalue_reference_type:
7d11d8c9 2777 recurse_pointer (&type, e, pf, top);
5f36109e
JS
2778 break;
2779
2780 case DW_TAG_subroutine_type:
c55ea10d 2781 push_deref (pf, "<function>:%p", e);
5f36109e
JS
2782 break;
2783
2784 case DW_TAG_union_type:
5f36109e
JS
2785 case DW_TAG_structure_type:
2786 case DW_TAG_class_type:
7d11d8c9 2787 recurse_struct (&type, e, pf, top);
5f36109e
JS
2788 break;
2789 }
2790}
2791
2792
600551ca
JS
2793// Bit fields are handled as a special-case combination of recurse() and
2794// recurse_base(), only called from recurse_struct_members(). The main
2795// difference is that the value is always printed numerically, even if the
2796// underlying type is a char.
2797void
2798dwarf_pretty_print::recurse_bitfield (Dwarf_Die* start_type, target_symbol* e,
2799 print_format* pf)
2800{
2801 Dwarf_Die type;
2802 dw.resolve_unqualified_inner_typedie (start_type, &type, e);
2803
2804 int tag = dwarf_tag(&type);
2805 if (tag != DW_TAG_base_type && tag != DW_TAG_enumeration_type)
2806 {
2807 // XXX need a warning?
2808 // throw semantic_error ("unsupported bitfield type (tag " + lex_cast(tag)
2809 // + ") for " + dwarf_type_name(&type), e->tok);
2810 pf->raw_components.append("?");
2811 return;
2812 }
2813
2814 Dwarf_Attribute attr;
2815 Dwarf_Word encoding = (Dwarf_Word) -1;
2816 dwarf_formudata (dwarf_attr_integrate (&type, DW_AT_encoding, &attr),
2817 &encoding);
2818 switch (encoding)
2819 {
2820 case DW_ATE_float:
2821 case DW_ATE_complex_float:
2822 // XXX need a warning?
2823 // throw semantic_error ("unsupported bitfield type (encoding " + lex_cast(encoding)
2824 // + ") for " + dwarf_type_name(&type), e->tok);
2825 pf->raw_components.append("?");
2826 break;
2827
2828 case DW_ATE_unsigned:
2829 case DW_ATE_unsigned_char:
2830 push_deref (pf, "%u", e);
2831 break;
2832
2833 case DW_ATE_signed:
2834 case DW_ATE_signed_char:
2835 default:
2836 push_deref (pf, "%i", e);
2837 break;
2838 }
2839}
2840
2841
5f36109e
JS
2842void
2843dwarf_pretty_print::recurse_base (Dwarf_Die* type, target_symbol* e,
7d11d8c9 2844 print_format* pf)
5f36109e
JS
2845{
2846 Dwarf_Attribute attr;
2847 Dwarf_Word encoding = (Dwarf_Word) -1;
2848 dwarf_formudata (dwarf_attr_integrate (type, DW_AT_encoding, &attr),
2849 &encoding);
5f36109e
JS
2850 switch (encoding)
2851 {
2852 case DW_ATE_float:
2853 case DW_ATE_complex_float:
2854 // XXX need a warning?
2855 // throw semantic_error ("unsupported type (encoding " + lex_cast(encoding)
2856 // + ") for " + dwarf_type_name(type), e->tok);
2857 pf->raw_components.append("?");
5f36109e
JS
2858 break;
2859
6561d8d1 2860 case DW_ATE_UTF: // XXX need to add unicode to _stp_vsprint_char
5f36109e
JS
2861 case DW_ATE_signed_char:
2862 case DW_ATE_unsigned_char:
941101c1
JS
2863 // Use escapes to make sure that non-printable characters
2864 // don't interrupt our stream (especially '\0' values).
2865 push_deref (pf, "'%#c'", e);
5f36109e
JS
2866 break;
2867
2868 case DW_ATE_unsigned:
c55ea10d 2869 push_deref (pf, "%u", e);
5f36109e
JS
2870 break;
2871
600551ca 2872 case DW_ATE_signed:
5f36109e 2873 default:
c55ea10d 2874 push_deref (pf, "%i", e);
5f36109e
JS
2875 break;
2876 }
5f36109e
JS
2877}
2878
2879
2880void
2881dwarf_pretty_print::recurse_array (Dwarf_Die* type, target_symbol* e,
7d11d8c9 2882 print_format* pf, bool top)
5f36109e 2883{
7d11d8c9
JS
2884 if (!top && !print_full)
2885 {
2886 pf->raw_components.append("[...]");
2887 return;
2888 }
2889
5f36109e
JS
2890 Dwarf_Die childtype;
2891 dwarf_attr_die (type, DW_AT_type, &childtype);
bbee5bb8
JS
2892
2893 if (print_chars (&childtype, e, pf))
2894 return;
2895
5f36109e
JS
2896 pf->raw_components.append("[");
2897
2898 // We print the array up to the first 5 elements.
2899 // XXX how can we determine the array size?
2900 // ... for now, just print the first element
64cddf39 2901 // NB: limit to 32 args; see PR10750 and c_unparser::visit_print_format.
5f36109e 2902 unsigned i, size = 1;
64cddf39 2903 for (i=0; i < size && i < 5 && pf->args.size() < 32; ++i)
5f36109e
JS
2904 {
2905 if (i > 0)
2906 pf->raw_components.append(", ");
2907 target_symbol* e2 = new target_symbol(*e);
2908 e2->components.push_back (target_symbol::component(e->tok, i));
7d11d8c9 2909 recurse (&childtype, e2, pf);
5f36109e
JS
2910 }
2911 if (i < size || 1/*XXX until real size is known */)
2912 pf->raw_components.append(", ...");
2913 pf->raw_components.append("]");
2914}
2915
2916
2917void
2918dwarf_pretty_print::recurse_pointer (Dwarf_Die* type, target_symbol* e,
7d11d8c9 2919 print_format* pf, bool top)
5f36109e 2920{
7d11d8c9 2921 // We chase to top-level pointers, but leave the rest alone
d19a9a82 2922 bool void_p = true;
7d11d8c9 2923 Dwarf_Die pointee;
bbee5bb8 2924 if (dwarf_attr_die (type, DW_AT_type, &pointee))
d19a9a82
JS
2925 {
2926 try
2927 {
2928 dw.resolve_unqualified_inner_typedie (&pointee, &pointee, e);
2929 void_p = false;
2930 }
2931 catch (const semantic_error&) {}
2932 }
2933
2934 if (!void_p)
5f36109e 2935 {
bbee5bb8
JS
2936 if (print_chars (&pointee, e, pf))
2937 return;
2938
2939 if (top)
2940 {
2941 recurse (&pointee, e, pf, top);
2942 return;
2943 }
5f36109e 2944 }
bbee5bb8 2945
c55ea10d 2946 push_deref (pf, "%p", e);
5f36109e
JS
2947}
2948
2949
2950void
2951dwarf_pretty_print::recurse_struct (Dwarf_Die* type, target_symbol* e,
7d11d8c9 2952 print_format* pf, bool top)
5f36109e 2953{
bdec0e18
JS
2954 if (dwarf_hasattr(type, DW_AT_declaration))
2955 {
a44a7cb5 2956 Dwarf_Die *resolved = dw.declaration_resolve(type);
bdec0e18
JS
2957 if (!resolved)
2958 {
2959 // could be an error, but for now just stub it
2960 // throw semantic_error ("unresolved " + dwarf_type_name(type), e->tok);
2961 pf->raw_components.append("{...}");
2962 return;
2963 }
2964 type = resolved;
2965 }
2966
5f36109e
JS
2967 int count = 0;
2968 pf->raw_components.append("{");
7d11d8c9
JS
2969 if (top || print_full)
2970 recurse_struct_members (type, e, pf, count);
2971 else
2972 pf->raw_components.append("...");
5f36109e
JS
2973 pf->raw_components.append("}");
2974}
2975
2976
2977void
2978dwarf_pretty_print::recurse_struct_members (Dwarf_Die* type, target_symbol* e,
7d11d8c9 2979 print_format* pf, int& count)
5f36109e 2980{
a80f28d8
JS
2981 /* With inheritance, a subclass may mask member names of parent classes, so
2982 * our search among the inheritance tree must be breadth-first rather than
2983 * depth-first (recursive). The type die is still our starting point. When
2984 * we encounter a masked name, just skip it. */
2985 set<string> dupes;
2986 deque<Dwarf_Die> inheritees(1, *type);
2987 for (; !inheritees.empty(); inheritees.pop_front())
2988 {
dee830d9 2989 Dwarf_Die child, childtype, import;
a80f28d8
JS
2990 if (dwarf_child (&inheritees.front(), &child) == 0)
2991 do
2992 {
2993 target_symbol* e2 = e;
5f36109e 2994
a80f28d8
JS
2995 // skip static members
2996 if (dwarf_hasattr(&child, DW_AT_declaration))
2997 continue;
5f36109e 2998
a80f28d8 2999 int tag = dwarf_tag (&child);
5f36109e 3000
dee830d9
MW
3001 /* Pretend imported units contain members by recursing into
3002 struct_member printing with the same count. */
3003 if (tag == DW_TAG_imported_unit
3004 && dwarf_attr_die (&child, DW_AT_import, &import))
3005 recurse_struct_members (&import, e2, pf, count);
3006
a80f28d8
JS
3007 if (tag != DW_TAG_member && tag != DW_TAG_inheritance)
3008 continue;
5f36109e 3009
a80f28d8 3010 dwarf_attr_die (&child, DW_AT_type, &childtype);
5f36109e 3011
a80f28d8
JS
3012 if (tag == DW_TAG_inheritance)
3013 {
3014 inheritees.push_back(childtype);
3015 continue;
3016 }
5f36109e 3017
a80f28d8
JS
3018 int childtag = dwarf_tag (&childtype);
3019 const char *member = dwarf_diename (&child);
3a147004 3020
a80f28d8
JS
3021 // "_vptr.foo" members are C++ virtual function tables,
3022 // which (generally?) aren't interesting for users.
3023 if (member && startswith(member, "_vptr."))
3024 continue;
3a147004 3025
a80f28d8
JS
3026 // skip inheritance-masked duplicates
3027 if (member && !dupes.insert(member).second)
3028 continue;
64cddf39 3029
a80f28d8
JS
3030 if (++count > 1)
3031 pf->raw_components.append(", ");
64cddf39 3032
a80f28d8
JS
3033 // NB: limit to 32 args; see PR10750 and c_unparser::visit_print_format.
3034 if (pf->args.size() >= 32)
3035 {
3036 pf->raw_components.append("...");
3037 break;
3038 }
3039
3040 if (member)
3041 {
3042 pf->raw_components.append(".");
3043 pf->raw_components.append(member);
5f36109e 3044
a80f28d8
JS
3045 e2 = new target_symbol(*e);
3046 e2->components.push_back (target_symbol::component(e->tok, member));
3047 }
3048 else if (childtag == DW_TAG_union_type)
3049 pf->raw_components.append("<union>");
3050 else if (childtag == DW_TAG_structure_type)
3051 pf->raw_components.append("<class>");
3052 else if (childtag == DW_TAG_class_type)
3053 pf->raw_components.append("<struct>");
3054 pf->raw_components.append("=");
600551ca
JS
3055
3056 if (dwarf_hasattr_integrate (&child, DW_AT_bit_offset))
3057 recurse_bitfield (&childtype, e2, pf);
3058 else
3059 recurse (&childtype, e2, pf);
5f36109e 3060 }
a80f28d8
JS
3061 while (dwarf_siblingof (&child, &child) == 0);
3062 }
5f36109e
JS
3063}
3064
3065
bbee5bb8
JS
3066bool
3067dwarf_pretty_print::print_chars (Dwarf_Die* start_type, target_symbol* e,
3068 print_format* pf)
3069{
3070 Dwarf_Die type;
3071 dw.resolve_unqualified_inner_typedie (start_type, &type, e);
6561d8d1
JS
3072
3073 Dwarf_Attribute attr;
3074 Dwarf_Word encoding = (Dwarf_Word) -1;
3075 dwarf_formudata (dwarf_attr_integrate (&type, DW_AT_encoding, &attr),
3076 &encoding);
3077 switch (encoding)
bbee5bb8 3078 {
6561d8d1
JS
3079 case DW_ATE_UTF:
3080 case DW_ATE_signed_char:
3081 case DW_ATE_unsigned_char:
3082 break;
3083 default:
3084 return false;
3085 }
3086
3087 string function = userspace_p ? "user_string2" : "kernel_string2";
3088 Dwarf_Word size = (Dwarf_Word) -1;
3089 dwarf_formudata (dwarf_attr_integrate (&type, DW_AT_byte_size, &attr), &size);
3090 switch (size)
3091 {
3092 case 1:
3093 break;
3094 case 2:
3095 function += "_utf16";
3096 break;
3097 case 4:
3098 function += "_utf32";
3099 break;
3100 default:
3101 return false;
3102 }
3103
3104 if (push_deref (pf, "\"%s\"", e))
3105 {
3106 // steal the last arg for a string access
3107 assert (!pf->args.empty());
3108 functioncall* fcall = new functioncall;
3109 fcall->tok = e->tok;
3110 fcall->function = function;
3111 fcall->args.push_back (pf->args.back());
3112 expression *err_msg = new literal_string ("<unknown>");
3113 err_msg->tok = e->tok;
3114 fcall->args.push_back (err_msg);
3115 pf->args.back() = fcall;
bbee5bb8 3116 }
6561d8d1 3117 return true;
bbee5bb8
JS
3118}
3119
a5ce5211
MW
3120// PR10601: adapt to kernel-vs-userspace loc2c-runtime
3121static const string EMBEDDED_FETCH_DEREF_KERNEL = string("\n")
f1e8e7e0
MW
3122 + "#define fetch_register k_fetch_register\n"
3123 + "#define store_register k_store_register\n"
3124 + "#define deref kderef\n"
3125 + "#define store_deref store_kderef\n";
a5ce5211
MW
3126
3127static const string EMBEDDED_FETCH_DEREF_USER = string("\n")
f1e8e7e0
MW
3128 + "#define fetch_register u_fetch_register\n"
3129 + "#define store_register u_store_register\n"
3130 + "#define deref uderef\n"
3131 + "#define store_deref store_uderef\n";
a5ce5211
MW
3132
3133#define EMBEDDED_FETCH_DEREF(U) \
f1e8e7e0 3134 (U ? EMBEDDED_FETCH_DEREF_USER : EMBEDDED_FETCH_DEREF_KERNEL)
a5ce5211
MW
3135
3136static const string EMBEDDED_FETCH_DEREF_DONE = string("\n")
f1e8e7e0
MW
3137 + "#undef fetch_register\n"
3138 + "#undef store_register\n"
3139 + "#undef deref\n"
3140 + "#undef store_deref\n";
bbee5bb8 3141
1c0be8c7
JS
3142static functioncall*
3143synthetic_embedded_deref_call(systemtap_session& session,
3144 const string& function_name,
3145 const string& function_code,
3146 exp_type function_type,
3147 bool userspace_p,
3148 bool lvalue_p,
3149 target_symbol* e,
3150 expression* pointer=NULL)
3151{
3152 // Synthesize a functiondecl for the given embedded code string.
5f36109e
JS
3153 functiondecl *fdecl = new functiondecl;
3154 fdecl->synthetic = true;
3155 fdecl->tok = e->tok;
1c0be8c7
JS
3156 fdecl->name = function_name;
3157 fdecl->type = function_type;
3158
5f36109e
JS
3159 embeddedcode *ec = new embeddedcode;
3160 ec->tok = e->tok;
1c0be8c7
JS
3161 ec->code += "/* unprivileged */";
3162 if (! lvalue_p)
3163 ec->code += "/* pure */";
3164 ec->code += EMBEDDED_FETCH_DEREF(userspace_p);
3165 ec->code += function_code;
3166 ec->code += EMBEDDED_FETCH_DEREF_DONE;
5f36109e
JS
3167 fdecl->body = ec;
3168
3169 // Synthesize a functioncall.
3170 functioncall* fcall = new functioncall;
3171 fcall->tok = e->tok;
3172 fcall->function = fdecl->name;
1c0be8c7 3173 fcall->type = fdecl->type;
5f36109e 3174
1c0be8c7
JS
3175 // If this code snippet uses a precomputed pointer,
3176 // pass that as the first argument.
5f36109e
JS
3177 if (pointer)
3178 {
5f36109e
JS
3179 vardecl *v = new vardecl;
3180 v->type = pe_long;
3181 v->name = "pointer";
3182 v->tok = e->tok;
3183 fdecl->formal_args.push_back(v);
3184 fcall->args.push_back(pointer);
3185 }
5f36109e 3186
1c0be8c7 3187 // Any non-literal indexes need to be passed as arguments too.
5f36109e
JS
3188 for (unsigned i = 0; i < e->components.size(); ++i)
3189 if (e->components[i].type == target_symbol::comp_expression_array_index)
3190 {
3191 vardecl *v = new vardecl;
3192 v->type = pe_long;
3193 v->name = "index" + lex_cast(i);
3194 v->tok = e->tok;
3195 fdecl->formal_args.push_back(v);
3196 fcall->args.push_back(e->components[i].expr_index);
3197 }
3198
1c0be8c7
JS
3199 // If this code snippet is assigning to an lvalue,
3200 // add a final argument for the rvalue.
3201 if (lvalue_p)
3202 {
3203 // Modify the fdecl so it carries a single pe_long formal
3204 // argument called "value".
5f36109e 3205
1c0be8c7
JS
3206 // FIXME: For the time being we only support setting target
3207 // variables which have base types; these are 'pe_long' in
3208 // stap's type vocabulary. Strings and pointers might be
3209 // reasonable, some day, but not today.
5f36109e 3210
1c0be8c7
JS
3211 vardecl *v = new vardecl;
3212 v->type = pe_long;
3213 v->name = "value";
3214 v->tok = e->tok;
3215 fdecl->formal_args.push_back(v);
3216 // NB: We don't know the value for fcall argument yet.
3217 // (see target_symbol_setter_functioncalls)
3218 }
3219
3220 // Add the synthesized decl to the session, and return the call.
3221 fdecl->join (session);
5f36109e
JS
3222 return fcall;
3223}
3224
1c0be8c7
JS
3225expression*
3226dwarf_pretty_print::deref (target_symbol* e)
3227{
3228 static unsigned tick = 0;
3229
3230 if (!deref_p)
3231 {
3232 assert (pointer && e->components.empty());
3233 return pointer;
3234 }
3235
3236 bool lvalue_p = false;
3237 string name = "_dwarf_pretty_print_deref_" + lex_cast(tick++);
3238
3239 string code;
3240 exp_type type = pe_long;
3241 if (pointer)
3242 code = dw.literal_stmt_for_pointer (&pointer_type, e, false, type);
3243 else if (!local.empty())
3244 code = dw.literal_stmt_for_local (scopes, pc, local, e, false, type);
3245 else
3246 code = dw.literal_stmt_for_return (&scopes[0], pc, e, false, type);
3247
3248 return synthetic_embedded_deref_call(dw.sess, name, code, type,
3249 userspace_p, lvalue_p, e, pointer);
3250}
3251
5f36109e 3252
c55ea10d
JS
3253bool
3254dwarf_pretty_print::push_deref (print_format* pf, const string& fmt,
3255 target_symbol* e)
3256{
3257 expression* e2 = NULL;
3258 try
3259 {
3260 e2 = deref (e);
3261 }
3262 catch (const semantic_error&)
3263 {
3264 pf->raw_components.append ("?");
3265 return false;
3266 }
3267 pf->raw_components.append (fmt);
3268 pf->args.push_back (e2);
3269 return true;
3270}
3271
3272
e57b735a 3273void
a7999c82 3274dwarf_var_expanding_visitor::visit_target_symbol_saved_return (target_symbol* e)
e57b735a 3275{
a7999c82
JS
3276 // Get the full name of the target symbol.
3277 stringstream ts_name_stream;
3278 e->print(ts_name_stream);
3279 string ts_name = ts_name_stream.str();
3280
3281 // Check and make sure we haven't already seen this target
3282 // variable in this return probe. If we have, just return our
3283 // last replacement.
af234c40 3284 map<string, expression *>::iterator i = return_ts_map.find(ts_name);
a7999c82 3285 if (i != return_ts_map.end())
85ecf79a 3286 {
a7999c82
JS
3287 provide (i->second);
3288 return;
3289 }
85ecf79a 3290
70208613
JS
3291 // Attempt the expansion directly first, so if there's a problem with the
3292 // variable we won't have a bogus entry probe lying around. Like in
3293 // saveargs(), we pretend for a moment that we're not in a .return.
3294 bool saved_has_return = q.has_return;
3295 q.has_return = false;
3296 expression *repl = e;
3297 replace (repl);
3298 q.has_return = saved_has_return;
3299 target_symbol* n = dynamic_cast<target_symbol*>(repl);
3300 if (n && n->saved_conversion_error)
3301 {
3302 provide (repl);
3303 return;
3304 }
3305
af234c40
JS
3306 expression *exp;
3307 if (!q.has_process &&
3308 strverscmp(q.sess.kernel_base_release.c_str(), "2.6.25") >= 0)
140be17a 3309 exp = gen_kretprobe_saved_return(repl);
af234c40 3310 else
cc9001af 3311 exp = gen_mapped_saved_return(repl, e->sym_name());
af234c40
JS
3312
3313 // Provide the variable to our parent so it can be used as a
3314 // substitute for the target symbol.
3315 provide (exp);
3316
3317 // Remember this replacement since we might be able to reuse
3318 // it later if the same return probe references this target
3319 // symbol again.
3320 return_ts_map[ts_name] = exp;
3321}
3322
4a2970a3 3323static expression*
23dc94f6
DS
3324gen_mapped_saved_return(systemtap_session &sess, expression* e,
3325 const string& name,
3326 block *& add_block, bool& add_block_tid,
3327 block *& add_call_probe, bool& add_call_probe_tid)
af234c40 3328{
23dc94f6
DS
3329 static unsigned tick = 0;
3330
a7999c82
JS
3331 // We've got to do several things here to handle target
3332 // variables in return probes.
85ecf79a 3333
a7999c82
JS
3334 // (1) Synthesize two global arrays. One is the cache of the
3335 // target variable and the other contains a thread specific
3336 // nesting level counter. The arrays will look like
3337 // this:
3338 //
23dc94f6
DS
3339 // _entry_tvar_{name}_{num}
3340 // _entry_tvar_{name}_{num}_ctr
a7999c82 3341
23dc94f6 3342 string aname = (string("_entry_tvar_")
cc9001af 3343 + name
aca66a36 3344 + "_" + lex_cast(tick++));
a7999c82
JS
3345 vardecl* vd = new vardecl;
3346 vd->name = aname;
3347 vd->tok = e->tok;
23dc94f6 3348 sess.globals.push_back (vd);
a7999c82
JS
3349
3350 string ctrname = aname + "_ctr";
3351 vd = new vardecl;
3352 vd->name = ctrname;
3353 vd->tok = e->tok;
23dc94f6 3354 sess.globals.push_back (vd);
a7999c82
JS
3355
3356 // (2) Create a new code block we're going to insert at the
3357 // beginning of this probe to get the cached value into a
3358 // temporary variable. We'll replace the target variable
3359 // reference with the temporary variable reference. The code
3360 // will look like this:
3361 //
23dc94f6
DS
3362 // _entry_tvar_tid = tid()
3363 // _entry_tvar_{name}_{num}_tmp
3364 // = _entry_tvar_{name}_{num}[_entry_tvar_tid,
3365 // _entry_tvar_{name}_{num}_ctr[_entry_tvar_tid]]
3366 // delete _entry_tvar_{name}_{num}[_entry_tvar_tid,
3367 // _entry_tvar_{name}_{num}_ctr[_entry_tvar_tid]--]
3368 // if (! _entry_tvar_{name}_{num}_ctr[_entry_tvar_tid])
3369 // delete _entry_tvar_{name}_{num}_ctr[_entry_tvar_tid]
a7999c82
JS
3370
3371 // (2a) Synthesize the tid temporary expression, which will look
3372 // like this:
3373 //
23dc94f6 3374 // _entry_tvar_tid = tid()
a7999c82 3375 symbol* tidsym = new symbol;
23dc94f6 3376 tidsym->name = string("_entry_tvar_tid");
a7999c82 3377 tidsym->tok = e->tok;
85ecf79a 3378
a7999c82
JS
3379 if (add_block == NULL)
3380 {
3381 add_block = new block;
3382 add_block->tok = e->tok;
8cc799a5 3383 }
8c819921 3384
8cc799a5
JS
3385 if (!add_block_tid)
3386 {
a7999c82
JS
3387 // Synthesize a functioncall to grab the thread id.
3388 functioncall* fc = new functioncall;
3389 fc->tok = e->tok;
3390 fc->function = string("tid");
8c819921 3391
23dc94f6 3392 // Assign the tid to '_entry_tvar_tid'.
8c819921
DS
3393 assignment* a = new assignment;
3394 a->tok = e->tok;
3395 a->op = "=";
a7999c82
JS
3396 a->left = tidsym;
3397 a->right = fc;
8c819921
DS
3398
3399 expr_statement* es = new expr_statement;
3400 es->tok = e->tok;
3401 es->value = a;
8c819921 3402 add_block->statements.push_back (es);
8cc799a5 3403 add_block_tid = true;
a7999c82 3404 }
8c819921 3405
a7999c82
JS
3406 // (2b) Synthesize an array reference and assign it to a
3407 // temporary variable (that we'll use as replacement for the
3408 // target variable reference). It will look like this:
3409 //
23dc94f6
DS
3410 // _entry_tvar_{name}_{num}_tmp
3411 // = _entry_tvar_{name}_{num}[_entry_tvar_tid,
3412 // _entry_tvar_{name}_{num}_ctr[_entry_tvar_tid]]
a7999c82
JS
3413
3414 arrayindex* ai_tvar_base = new arrayindex;
3415 ai_tvar_base->tok = e->tok;
3416
3417 symbol* sym = new symbol;
3418 sym->name = aname;
3419 sym->tok = e->tok;
3420 ai_tvar_base->base = sym;
3421
3422 ai_tvar_base->indexes.push_back(tidsym);
3423
3424 // We need to create a copy of the array index in its current
3425 // state so we can have 2 variants of it (the original and one
3426 // that post-decrements the second index).
3427 arrayindex* ai_tvar = new arrayindex;
3428 arrayindex* ai_tvar_postdec = new arrayindex;
3429 *ai_tvar = *ai_tvar_base;
3430 *ai_tvar_postdec = *ai_tvar_base;
3431
3432 // Synthesize the
23dc94f6 3433 // "_entry_tvar_{name}_{num}_ctr[_entry_tvar_tid]" used as the
a7999c82
JS
3434 // second index into the array.
3435 arrayindex* ai_ctr = new arrayindex;
3436 ai_ctr->tok = e->tok;
3437
3438 sym = new symbol;
3439 sym->name = ctrname;
3440 sym->tok = e->tok;
3441 ai_ctr->base = sym;
3442 ai_ctr->indexes.push_back(tidsym);
3443 ai_tvar->indexes.push_back(ai_ctr);
3444
3445 symbol* tmpsym = new symbol;
3446 tmpsym->name = aname + "_tmp";
3447 tmpsym->tok = e->tok;
3448
3449 assignment* a = new assignment;
3450 a->tok = e->tok;
3451 a->op = "=";
3452 a->left = tmpsym;
3453 a->right = ai_tvar;
3454
3455 expr_statement* es = new expr_statement;
3456 es->tok = e->tok;
3457 es->value = a;
3458
3459 add_block->statements.push_back (es);
3460
3461 // (2c) Add a post-decrement to the second array index and
3462 // delete the array value. It will look like this:
3463 //
23dc94f6
DS
3464 // delete _entry_tvar_{name}_{num}[_entry_tvar_tid,
3465 // _entry_tvar_{name}_{num}_ctr[_entry_tvar_tid]--]
a7999c82
JS
3466
3467 post_crement* pc = new post_crement;
3468 pc->tok = e->tok;
3469 pc->op = "--";
3470 pc->operand = ai_ctr;
3471 ai_tvar_postdec->indexes.push_back(pc);
3472
3473 delete_statement* ds = new delete_statement;
3474 ds->tok = e->tok;
3475 ds->value = ai_tvar_postdec;
3476
3477 add_block->statements.push_back (ds);
3478
3479 // (2d) Delete the counter value if it is 0. It will look like
3480 // this:
23dc94f6
DS
3481 // if (! _entry_tvar_{name}_{num}_ctr[_entry_tvar_tid])
3482 // delete _entry_tvar_{name}_{num}_ctr[_entry_tvar_tid]
a7999c82
JS
3483
3484 ds = new delete_statement;
3485 ds->tok = e->tok;
3486 ds->value = ai_ctr;
3487
3488 unary_expression *ue = new unary_expression;
3489 ue->tok = e->tok;
3490 ue->op = "!";
3491 ue->operand = ai_ctr;
3492
3493 if_statement *ifs = new if_statement;
3494 ifs->tok = e->tok;
3495 ifs->condition = ue;
3496 ifs->thenblock = ds;
3497 ifs->elseblock = NULL;
3498
3499 add_block->statements.push_back (ifs);
3500
3501 // (3) We need an entry probe that saves the value for us in the
3502 // global array we created. Create the entry probe, which will
3503 // look like this:
3504 //
2260f4e3 3505 // probe kernel.function("{function}").call {
23dc94f6
DS
3506 // _entry_tvar_tid = tid()
3507 // _entry_tvar_{name}_{num}[_entry_tvar_tid,
3508 // ++_entry_tvar_{name}_{num}_ctr[_entry_tvar_tid]]
a7999c82
JS
3509 // = ${param}
3510 // }
3511
2260f4e3 3512 if (add_call_probe == NULL)
a7999c82 3513 {
2260f4e3
FCE
3514 add_call_probe = new block;
3515 add_call_probe->tok = e->tok;
8cc799a5 3516 }
4baf0e53 3517
8cc799a5
JS
3518 if (!add_call_probe_tid)
3519 {
a7999c82
JS
3520 // Synthesize a functioncall to grab the thread id.
3521 functioncall* fc = new functioncall;
3522 fc->tok = e->tok;
3523 fc->function = string("tid");
4baf0e53 3524
23dc94f6 3525 // Assign the tid to '_entry_tvar_tid'.
a7999c82 3526 assignment* a = new assignment;
8fc05e57
DS
3527 a->tok = e->tok;
3528 a->op = "=";
a7999c82
JS
3529 a->left = tidsym;
3530 a->right = fc;
8fc05e57 3531
a7999c82 3532 expr_statement* es = new expr_statement;
8fc05e57
DS
3533 es->tok = e->tok;
3534 es->value = a;
2260f4e3 3535 add_call_probe = new block(add_call_probe, es);
8cc799a5 3536 add_call_probe_tid = true;
85ecf79a 3537 }
cf2a1f85 3538
a7999c82 3539 // Save the value, like this:
23dc94f6
DS
3540 // _entry_tvar_{name}_{num}[_entry_tvar_tid,
3541 // ++_entry_tvar_{name}_{num}_ctr[_entry_tvar_tid]]
a7999c82
JS
3542 // = ${param}
3543 arrayindex* ai_tvar_preinc = new arrayindex;
3544 *ai_tvar_preinc = *ai_tvar_base;
3545
3546 pre_crement* preinc = new pre_crement;
3547 preinc->tok = e->tok;
3548 preinc->op = "++";
3549 preinc->operand = ai_ctr;
3550 ai_tvar_preinc->indexes.push_back(preinc);
3551
3552 a = new assignment;
3553 a->tok = e->tok;
3554 a->op = "=";
3555 a->left = ai_tvar_preinc;
3556 a->right = e;
3557
3558 es = new expr_statement;
3559 es->tok = e->tok;
3560 es->value = a;
3561
2260f4e3 3562 add_call_probe = new block(add_call_probe, es);
a7999c82 3563
23dc94f6 3564 // (4) Provide the '_entry_tvar_{name}_{num}_tmp' variable to
a7999c82
JS
3565 // our parent so it can be used as a substitute for the target
3566 // symbol.
3f803f9e 3567 delete ai_tvar_base;
af234c40
JS
3568 return tmpsym;
3569}
a7999c82 3570
af234c40 3571
23dc94f6
DS
3572expression*
3573dwarf_var_expanding_visitor::gen_mapped_saved_return(expression* e,
3574 const string& name)
3575{
3576 return ::gen_mapped_saved_return(q.sess, e, name, add_block,
3577 add_block_tid, add_call_probe,
3578 add_call_probe_tid);
3579}
3580
3581
af234c40 3582expression*
140be17a 3583dwarf_var_expanding_visitor::gen_kretprobe_saved_return(expression* e)
af234c40
JS
3584{
3585 // The code for this is simple.
3586 //
3587 // .call:
3588 // _set_kretprobe_long(index, $value)
3589 //
3590 // .return:
3591 // _get_kretprobe_long(index)
3592 //
3593 // (or s/long/string/ for things like $$parms)
3594
3595 unsigned index;
3596 string setfn, getfn;
3597
140be17a
JS
3598 // We need the caller to predetermine the type of the expression!
3599 switch (e->type)
af234c40 3600 {
140be17a 3601 case pe_string:
af234c40
JS
3602 index = saved_strings++;
3603 setfn = "_set_kretprobe_string";
3604 getfn = "_get_kretprobe_string";
140be17a
JS
3605 break;
3606 case pe_long:
af234c40
JS
3607 index = saved_longs++;
3608 setfn = "_set_kretprobe_long";
3609 getfn = "_get_kretprobe_long";
140be17a
JS
3610 break;
3611 default:
dc09353a 3612 throw SEMANTIC_ERROR(_("unknown type to save in kretprobe"), e->tok);
af234c40
JS
3613 }
3614
3615 // Create the entry code
3616 // _set_kretprobe_{long|string}(index, $value)
3617
3618 if (add_call_probe == NULL)
3619 {
3620 add_call_probe = new block;
3621 add_call_probe->tok = e->tok;
3622 }
3623
3624 functioncall* set_fc = new functioncall;
3625 set_fc->tok = e->tok;
3626 set_fc->function = setfn;
3627 set_fc->args.push_back(new literal_number(index));
3628 set_fc->args.back()->tok = e->tok;
3629 set_fc->args.push_back(e);
3630
3631 expr_statement* set_es = new expr_statement;
3632 set_es->tok = e->tok;
3633 set_es->value = set_fc;
3634
3635 add_call_probe->statements.push_back(set_es);
3636
3637 // Create the return code
3638 // _get_kretprobe_{long|string}(index)
3639
3640 functioncall* get_fc = new functioncall;
3641 get_fc->tok = e->tok;
3642 get_fc->function = getfn;
3643 get_fc->args.push_back(new literal_number(index));
3644 get_fc->args.back()->tok = e->tok;
3645
3646 return get_fc;
a7999c82 3647}
a43ba433 3648
2cb3fe26 3649
a7999c82
JS
3650void
3651dwarf_var_expanding_visitor::visit_target_symbol_context (target_symbol* e)
3652{
b78a0fbb 3653 if (null_die(scope_die)) {
04c8e51b
JL
3654 literal_string *empty = new literal_string("");
3655 empty->tok = e->tok;
3656 provide(empty);
a7999c82 3657 return;
b78a0fbb 3658 }
2cb3fe26 3659
5f36109e
JS
3660 target_symbol *tsym = new target_symbol(*e);
3661
fde50242
JS
3662 bool pretty = (!e->components.empty() &&
3663 e->components[0].type == target_symbol::comp_pretty_print);
3664 string format = pretty ? "=%s" : "=%#x";
a43ba433 3665
a7999c82
JS
3666 // Convert $$parms to sprintf of a list of parms and active local vars
3667 // which we recursively evaluate
a43ba433 3668
1c922ad7 3669 print_format* pf = print_format::create(e->tok, "sprintf");
a7999c82 3670
277c21bc 3671 if (q.has_return && (e->name == "$$return"))
a7999c82 3672 {
277c21bc 3673 tsym->name = "$return";
a7999c82
JS
3674
3675 // Ignore any variable that isn't accessible.
3676 tsym->saved_conversion_error = 0;
3677 expression *texp = tsym;
8b095b45 3678 replace (texp); // NB: throws nothing ...
a7999c82 3679 if (tsym->saved_conversion_error) // ... but this is how we know it happened.
a43ba433 3680 {
2cb3fe26 3681
a43ba433
FCE
3682 }
3683 else
3684 {
a7999c82 3685 pf->raw_components += "return";
5f36109e 3686 pf->raw_components += format;
a7999c82
JS
3687 pf->args.push_back(texp);
3688 }
3689 }
3690 else
3691 {
3692 // non-.return probe: support $$parms, $$vars, $$locals
345bbb3d 3693 bool first = true;
a7999c82 3694 Dwarf_Die result;
d48bc7eb
JS
3695 vector<Dwarf_Die> scopes = q.dw.getscopes(scope_die);
3696 for (unsigned i = 0; i < scopes.size(); ++i)
3697 {
3698 if (dwarf_tag(&scopes[i]) == DW_TAG_compile_unit)
3699 break; // we don't want file-level variables
3700 if (dwarf_child (&scopes[i], &result) == 0)
3701 do
00cf3709 3702 {
d48bc7eb
JS
3703 switch (dwarf_tag (&result))
3704 {
3705 case DW_TAG_variable:
3706 if (e->name == "$$parms")
3707 continue;
3708 break;
3709 case DW_TAG_formal_parameter:
3710 if (e->name == "$$locals")
3711 continue;
3712 break;
3713
3714 default:
3715 continue;
3716 }
41c262f3 3717
d48bc7eb
JS
3718 const char *diename = dwarf_diename (&result);
3719 if (! diename) continue;
f76427a2 3720
d48bc7eb
JS
3721 if (! first)
3722 pf->raw_components += " ";
3723 pf->raw_components += diename;
fde50242
JS
3724 first = false;
3725
3726 // Write a placeholder for ugly aggregates
3727 Dwarf_Die type;
3728 if (!pretty && dwarf_attr_die(&result, DW_AT_type, &type))
3729 {
3730 q.dw.resolve_unqualified_inner_typedie(&type, &type, e);
3731 switch (dwarf_tag(&type))
3732 {
3733 case DW_TAG_union_type:
3734 case DW_TAG_structure_type:
3735 case DW_TAG_class_type:
3736 pf->raw_components += "={...}";
3737 continue;
3738
3739 case DW_TAG_array_type:
3740 pf->raw_components += "=[...]";
3741 continue;
3742 }
3743 }
345bbb3d 3744
d48bc7eb
JS
3745 tsym->name = "$";
3746 tsym->name += diename;
41c262f3 3747
d48bc7eb
JS
3748 // Ignore any variable that isn't accessible.
3749 tsym->saved_conversion_error = 0;
3750 expression *texp = tsym;
3751 replace (texp); // NB: throws nothing ...
3752 if (tsym->saved_conversion_error) // ... but this is how we know it happened.
a43ba433 3753 {
d48bc7eb
JS
3754 if (q.sess.verbose>2)
3755 {
e26c2f83 3756 for (const semantic_error *c = tsym->saved_conversion_error;
d48bc7eb 3757 c != 0;
c081af73 3758 c = c->get_chain()) {
4c5d9906 3759 clog << _("variable location problem [man error::dwarf]: ") << c->what() << endl;
d48bc7eb
JS
3760 }
3761 }
3762
3763 pf->raw_components += "=?";
a43ba433 3764 }
d48bc7eb
JS
3765 else
3766 {
3767 pf->raw_components += format;
3768 pf->args.push_back(texp);
3769 }
a7999c82 3770 }
d48bc7eb
JS
3771 while (dwarf_siblingof (&result, &result) == 0);
3772 }
a7999c82 3773 }
2cb3fe26 3774
a7999c82 3775 pf->components = print_format::string_to_components(pf->raw_components);
140be17a 3776 pf->type = pe_string;
a7999c82
JS
3777 provide (pf);
3778}
3779
2cb3fe26 3780
bd1fcbad
YZ
3781void
3782dwarf_var_expanding_visitor::visit_atvar_op (atvar_op *e)
3783{
3784 // Fill in our current module context if needed
3785 if (e->module.empty())
3786 e->module = q.dw.module_name;
3787
3788 if (e->module == q.dw.module_name && e->cu_name.empty())
3789 {
3790 // process like any other local
3791 // e->sym_name() will do the right thing
3792 visit_target_symbol(e);
3793 return;
3794 }
3795
3796 var_expanding_visitor::visit_atvar_op(e);
3797}
3798
3799
a7999c82
JS
3800void
3801dwarf_var_expanding_visitor::visit_target_symbol (target_symbol *e)
3802{
bd1fcbad 3803 assert(e->name.size() > 0 && (e->name[0] == '$' || e->name == "@var"));
a7999c82 3804 visited = true;
30263a73
FCE
3805 bool defined_being_checked = (defined_ops.size() > 0 && (defined_ops.top()->operand == e));
3806 // In this mode, we avoid hiding errors or generating extra code such as for .return saved $vars
a7999c82 3807
70208613 3808 try
a7999c82 3809 {
c69a87e0
FCE
3810 bool lvalue = is_active_lvalue(e);
3811 if (lvalue && !q.sess.guru_mode)
dc09353a 3812 throw SEMANTIC_ERROR(_("write to target variable not permitted; need stap -g"), e->tok);
2cb3fe26 3813
100a540e 3814 // XXX: process $context vars should be writable
70208613 3815
c69a87e0
FCE
3816 // See if we need to generate a new probe to save/access function
3817 // parameters from a return probe. PR 1382.
3818 if (q.has_return
3819 && !defined_being_checked
277c21bc
JS
3820 && e->name != "$return" // not the special return-value variable handled below
3821 && e->name != "$$return") // nor the other special variable handled below
c69a87e0
FCE
3822 {
3823 if (lvalue)
dc09353a 3824 throw SEMANTIC_ERROR(_("write to target variable not permitted in .return probes"), e->tok);
c69a87e0
FCE
3825 visit_target_symbol_saved_return(e);
3826 return;
3827 }
e57b735a 3828
277c21bc
JS
3829 if (e->name == "$$vars" || e->name == "$$parms" || e->name == "$$locals"
3830 || (q.has_return && (e->name == "$$return")))
c69a87e0
FCE
3831 {
3832 if (lvalue)
dc09353a 3833 throw SEMANTIC_ERROR(_("cannot write to context variable"), e->tok);
70208613 3834
c69a87e0 3835 if (e->addressof)
dc09353a 3836 throw SEMANTIC_ERROR(_("cannot take address of context variable"), e->tok);
70208613 3837
5f36109e
JS
3838 e->assert_no_components("dwarf", true);
3839
c69a87e0
FCE
3840 visit_target_symbol_context(e);
3841 return;
3842 }
70208613 3843
04c8e51b
JL
3844 // Everything else (pretty-printed vars, and context vars) require a
3845 // scope_die in which to search for them. If we don't have that, just
3846 // leave it unresolved; we'll produce an error later on.
3847 if (null_die(scope_die))
3848 {
3849 provide(e);
3850 return;
3851 }
3852
5f36109e
JS
3853 if (!e->components.empty() &&
3854 e->components.back().type == target_symbol::comp_pretty_print)
3855 {
3856 if (lvalue)
dc09353a 3857 throw SEMANTIC_ERROR(_("cannot write to pretty-printed variable"), e->tok);
5f36109e 3858
277c21bc 3859 if (q.has_return && (e->name == "$return"))
5f36109e
JS
3860 {
3861 dwarf_pretty_print dpp (q.dw, scope_die, addr,
3862 q.has_process, *e);
3863 dpp.expand()->visit(this);
3864 }
3865 else
3866 {
3867 dwarf_pretty_print dpp (q.dw, getscopes(e), addr,
cc9001af 3868 e->sym_name(),
5f36109e
JS
3869 q.has_process, *e);
3870 dpp.expand()->visit(this);
3871 }
3872 return;
3873 }
3874
1c0be8c7 3875 bool userspace_p = q.has_process;
c69a87e0 3876 string fname = (string(lvalue ? "_dwarf_tvar_set" : "_dwarf_tvar_get")
cc9001af 3877 + "_" + e->sym_name()
c69a87e0 3878 + "_" + lex_cast(tick++));
70208613 3879
70208613 3880
1c0be8c7
JS
3881 exp_type type = pe_long;
3882 string code;
277c21bc 3883 if (q.has_return && (e->name == "$return"))
1c0be8c7 3884 code = q.dw.literal_stmt_for_return (scope_die, addr, e, lvalue, type);
e19fda4e 3885 else
1c0be8c7
JS
3886 code = q.dw.literal_stmt_for_local (getscopes(e), addr, e->sym_name(),
3887 e, lvalue, type);
70208613 3888
1c0be8c7
JS
3889 functioncall* n = synthetic_embedded_deref_call(q.sess, fname, code, type,
3890 userspace_p, lvalue, e);
70208613 3891
c69a87e0
FCE
3892 if (lvalue)
3893 {
3894 // Provide the functioncall to our parent, so that it can be
3895 // used to substitute for the assignment node immediately above
3896 // us.
3897 assert(!target_symbol_setter_functioncalls.empty());
3898 *(target_symbol_setter_functioncalls.top()) = n;
3899 }
70208613 3900
1c0be8c7
JS
3901 // Revisit the functioncall so arguments can be expanded.
3902 n->visit (this);
66d284f4
FCE
3903 }
3904 catch (const semantic_error& er)
3905 {
9fab2262
JS
3906 // We suppress this error message, and pass the unresolved
3907 // target_symbol to the next pass. We hope that this value ends
3908 // up not being referenced after all, so it can be optimized out
3909 // quietly.
1af1e62d 3910 e->chain (er);
9fab2262 3911 provide (e);
66d284f4 3912 }
77de5e9e
GH
3913}
3914
3915
c24447be
JS
3916void
3917dwarf_var_expanding_visitor::visit_cast_op (cast_op *e)
3918{
3919 // Fill in our current module context if needed
3920 if (e->module.empty())
3921 e->module = q.dw.module_name;
3922
3923 var_expanding_visitor::visit_cast_op(e);
3924}
3925
3926
8cc799a5
JS
3927void
3928dwarf_var_expanding_visitor::visit_entry_op (entry_op *e)
3929{
3930 expression *repl = e;
3931 if (q.has_return)
3932 {
3933 // expand the operand as if it weren't a return probe
3934 q.has_return = false;
3935 replace (e->operand);
3936 q.has_return = true;
3937
3938 // XXX it would be nice to use gen_kretprobe_saved_return when available,
3939 // but it requires knowing the types already, which is problematic for
3940 // arbitrary expressons.
cc9001af 3941 repl = gen_mapped_saved_return (e->operand, "entry");
8cc799a5
JS
3942 }
3943 provide (repl);
3944}
3945
3689db05
SC
3946void
3947dwarf_var_expanding_visitor::visit_perf_op (perf_op *e)
3948{
ace7c23f 3949 string e_lit_val = e->operand->value;
b78a0fbb 3950
3689db05 3951 add_block = new block;
9eaaceaa 3952 add_block->tok = e->tok;
3689db05
SC
3953
3954 systemtap_session &s = this->q.sess;
3955 map<string, pair<string,derived_probe*> >::iterator it;
3956 // Find the associated perf.counter probe
3957 for (it=s.perf_counters.begin();
3958 it != s.perf_counters.end();
3959 it++)
3960 if ((*it).first == e_lit_val)
3961 {
3962 // if perf .function("name") omitted, then set it to this process name
3963 if ((*it).second.first.length() == 0)
3964 ((*it).second).first = this->q.user_path;
3965 if (((*it).second).first == this->q.user_path)
3966 break;
3967 }
3968
3969 if (it != s.perf_counters.end())
3970 {
698de6cc 3971 perf_counter_refs.insert((*it).second.second);
3689db05
SC
3972 // __perf_read_N is assigned in the probe prologue
3973 symbol* sym = new symbol;
4653caf1 3974 sym->tok = e->tok;
3689db05
SC
3975 sym->name = "__perf_read_" + (*it).first;
3976 provide (sym);
3977 }
3978 else
dc09353a 3979 throw SEMANTIC_ERROR(_F("perf counter '%s' not defined", e_lit_val.c_str()));
3689db05
SC
3980}
3981
8cc799a5 3982
729455a7
JS
3983vector<Dwarf_Die>&
3984dwarf_var_expanding_visitor::getscopes(target_symbol *e)
3985{
3986 if (scopes.empty())
3987 {
04c8e51b 3988 if(!null_die(scope_die))
f25a9197 3989 scopes = q.dw.getscopes(scope_die);
729455a7 3990 if (scopes.empty())
b530b5b3
LB
3991 //throw semantic_error (_F("unable to find any scopes containing %d", addr), e->tok);
3992 // ((scope_die == NULL) ? "" : (string (" in ") + (dwarf_diename(scope_die) ?: "<unknown>") + "(" + (dwarf_diename(q.dw.cu) ?: "<unknown>") ")" ))
dc09353a 3993 throw SEMANTIC_ERROR ("unable to find any scopes containing "
729455a7 3994 + lex_cast_hex(addr)
04c8e51b 3995 + (null_die(scope_die) ? ""
729455a7
JS
3996 : (string (" in ")
3997 + (dwarf_diename(scope_die) ?: "<unknown>")
3998 + "(" + (dwarf_diename(q.dw.cu) ?: "<unknown>")
3999 + ")"))
4000 + " while searching for local '"
cc9001af 4001 + e->sym_name() + "'",
729455a7
JS
4002 e->tok);
4003 }
4004 return scopes;
4005}
4006
4007
5f36109e
JS
4008struct dwarf_cast_expanding_visitor: public var_expanding_visitor
4009{
4010 systemtap_session& s;
4011 dwarf_builder& db;
4012
4013 dwarf_cast_expanding_visitor(systemtap_session& s, dwarf_builder& db):
4014 s(s), db(db) {}
4015 void visit_cast_op (cast_op* e);
4016 void filter_special_modules(string& module);
4017};
4018
4019
c4ce66a1
JS
4020struct dwarf_cast_query : public base_query
4021{
946e1a48 4022 cast_op& e;
c4ce66a1 4023 const bool lvalue;
5f36109e
JS
4024 const bool userspace_p;
4025 functioncall*& result;
c4ce66a1 4026
5f36109e
JS
4027 dwarf_cast_query(dwflpp& dw, const string& module, cast_op& e, bool lvalue,
4028 const bool userspace_p, functioncall*& result):
abb41d92 4029 base_query(dw, module), e(e), lvalue(lvalue),
5f36109e 4030 userspace_p(userspace_p), result(result) {}
c4ce66a1
JS
4031
4032 void handle_query_module();
822a6a3d 4033 void query_library (const char *) {}
576eaefe 4034 void query_plt (const char *entry, size_t addr) {}
c4ce66a1
JS
4035};
4036
4037
c4ce66a1
JS
4038void
4039dwarf_cast_query::handle_query_module()
4040{
5f36109e
JS
4041 static unsigned tick = 0;
4042
4043 if (result)
c4ce66a1
JS
4044 return;
4045
ea1e477a 4046 // look for the type in any CU
a44a7cb5
JS
4047 Dwarf_Die* type_die = NULL;
4048 if (startswith(e.type_name, "class "))
4049 {
4050 // normalize to match dwflpp::global_alias_caching_callback
4051 string struct_name = "struct " + e.type_name.substr(6);
4052 type_die = dw.declaration_resolve_other_cus(struct_name);
4053 }
4054 else
4055 type_die = dw.declaration_resolve_other_cus(e.type_name);
4056
4057 // NB: We now index the types as "struct name"/"union name"/etc. instead of
4058 // just "name". But since we didn't require users to be explicit before, and
4059 // actually sort of discouraged it, we must be flexible now. So if a lookup
4060 // fails with a bare name, try augmenting it.
4061 if (!type_die &&
4062 !startswith(e.type_name, "class ") &&
4063 !startswith(e.type_name, "struct ") &&
4064 !startswith(e.type_name, "union ") &&
4065 !startswith(e.type_name, "enum "))
4066 {
4067 type_die = dw.declaration_resolve_other_cus("struct " + e.type_name);
4068 if (!type_die)
4069 type_die = dw.declaration_resolve_other_cus("union " + e.type_name);
4070 if (!type_die)
4071 type_die = dw.declaration_resolve_other_cus("enum " + e.type_name);
4072 }
4073
ea1e477a
JS
4074 if (!type_die)
4075 return;
c4ce66a1 4076
5f36109e
JS
4077 string code;
4078 exp_type type = pe_long;
4079
ea1e477a 4080 try
c4ce66a1 4081 {
ea1e477a
JS
4082 Dwarf_Die cu_mem;
4083 dw.focus_on_cu(dwarf_diecu(type_die, &cu_mem, NULL, NULL));
5f36109e
JS
4084
4085 if (!e.components.empty() &&
4086 e.components.back().type == target_symbol::comp_pretty_print)
4087 {
4088 if (lvalue)
dc09353a 4089 throw SEMANTIC_ERROR(_("cannot write to pretty-printed variable"), e.tok);
5f36109e 4090
d19a9a82 4091 dwarf_pretty_print dpp(dw, type_die, e.operand, true, userspace_p, e);
5f36109e
JS
4092 result = dpp.expand();
4093 return;
4094 }
4095
4096 code = dw.literal_stmt_for_pointer (type_die, &e, lvalue, type);
ea1e477a
JS
4097 }
4098 catch (const semantic_error& er)
4099 {
4100 // NB: we can have multiple errors, since a @cast
1af1e62d
JS
4101 // may be attempted using several different modules:
4102 // @cast(ptr, "type", "module1:module2:...")
4103 e.chain (er);
c4ce66a1 4104 }
c4ce66a1 4105
5f36109e
JS
4106 if (code.empty())
4107 return;
c4ce66a1 4108
5f36109e 4109 string fname = (string(lvalue ? "_dwarf_cast_set" : "_dwarf_cast_get")
cc9001af 4110 + "_" + e.sym_name()
5f36109e 4111 + "_" + lex_cast(tick++));
c4ce66a1 4112
1c0be8c7
JS
4113 result = synthetic_embedded_deref_call(dw.sess, fname, code, type,
4114 userspace_p, lvalue, &e, e.operand);
5f36109e 4115}
c4ce66a1
JS
4116
4117
fb0274bc
JS
4118void dwarf_cast_expanding_visitor::filter_special_modules(string& module)
4119{
d90053e7 4120 // look for "<path/to/header>" or "kernel<path/to/header>"
fb0274bc 4121 // for those cases, build a module including that header
d90053e7 4122 if (module[module.size() - 1] == '>' &&
60d98537 4123 (module[0] == '<' || startswith(module, "kernel<")))
fb0274bc
JS
4124 {
4125 string cached_module;
4126 if (s.use_cache)
4127 {
4128 // see if the cached module exists
a2639cb7 4129 cached_module = find_typequery_hash(s, module);
d105f664 4130 if (!cached_module.empty() && !s.poison_cache)
fb0274bc
JS
4131 {
4132 int fd = open(cached_module.c_str(), O_RDONLY);
4133 if (fd != -1)
4134 {
4135 if (s.verbose > 2)
b530b5b3
LB
4136 //TRANSLATORS: Here we're using a cached module.
4137 clog << _("Pass 2: using cached ") << cached_module << endl;
fb0274bc
JS
4138 module = cached_module;
4139 close(fd);
4140 return;
4141 }
4142 }
4143 }
4144
4145 // no cached module, time to make it
d90053e7 4146 if (make_typequery(s, module) == 0)
fb0274bc 4147 {
e16dc041 4148 // try to save typequery in the cache
fb0274bc 4149 if (s.use_cache)
e16dc041 4150 copy_file(module, cached_module, s.verbose > 2);
fb0274bc
JS
4151 }
4152 }
4153}
4154
4155
c4ce66a1
JS
4156void dwarf_cast_expanding_visitor::visit_cast_op (cast_op* e)
4157{
4158 bool lvalue = is_active_lvalue(e);
4159 if (lvalue && !s.guru_mode)
dc09353a 4160 throw SEMANTIC_ERROR(_("write to @cast context variable not permitted; need stap -g"), e->tok);
c4ce66a1
JS
4161
4162 if (e->module.empty())
4163 e->module = "kernel"; // "*" may also be reasonable to search all kernel modules
4164
5f36109e 4165 functioncall* result = NULL;
8b31197b
JS
4166
4167 // split the module string by ':' for alternatives
4168 vector<string> modules;
4169 tokenize(e->module, modules, ":");
b5a0dd41 4170 bool userspace_p=false; // PR10601
5f36109e 4171 for (unsigned i = 0; !result && i < modules.size(); ++i)
c4ce66a1 4172 {
8b31197b 4173 string& module = modules[i];
fb0274bc 4174 filter_special_modules(module);
abb41d92 4175
c4ce66a1
JS
4176 // NB: This uses '/' to distinguish between kernel modules and userspace,
4177 // which means that userspace modules won't get any PATH searching.
4178 dwflpp* dw;
707bf35e
JS
4179 try
4180 {
b5a0dd41
FCE
4181 userspace_p=is_user_module (module);
4182 if (! userspace_p)
707bf35e
JS
4183 {
4184 // kernel or kernel module target
ae2552da 4185 dw = db.get_kern_dw(s, module);
707bf35e
JS
4186 }
4187 else
4188 {
05fb3e0c 4189 module = find_executable (module, "", s.sysenv); // canonicalize it
707bf35e
JS
4190 dw = db.get_user_dw(s, module);
4191 }
4192 }
4193 catch (const semantic_error& er)
4194 {
4195 /* ignore and go to the next module */
4196 continue;
4197 }
c4ce66a1 4198
5f36109e 4199 dwarf_cast_query q (*dw, module, *e, lvalue, userspace_p, result);
06de3a04 4200 dw->iterate_over_modules<base_query>(&query_module, &q);
c4ce66a1 4201 }
abb41d92 4202
5f36109e 4203 if (!result)
c4ce66a1 4204 {
946e1a48
JS
4205 // We pass the unresolved cast_op to the next pass, and hope
4206 // that this value ends up not being referenced after all, so
4207 // it can be optimized out quietly.
c4ce66a1
JS
4208 provide (e);
4209 return;
4210 }
4211
c4ce66a1
JS
4212 if (lvalue)
4213 {
4214 // Provide the functioncall to our parent, so that it can be
4215 // used to substitute for the assignment node immediately above
4216 // us.
4217 assert(!target_symbol_setter_functioncalls.empty());
5f36109e 4218 *(target_symbol_setter_functioncalls.top()) = result;
c4ce66a1
JS
4219 }
4220
5f36109e 4221 result->visit (this);
77de5e9e
GH
4222}
4223
4224
251707c8
JS
4225struct dwarf_autocast_expanding_visitor: public var_expanding_visitor
4226{
4227 systemtap_session& s;
4228
4229 dwarf_autocast_expanding_visitor(systemtap_session& s): s(s) {}
4230 void visit_autocast_op (autocast_op* e);
4231};
4232
4233
4234void
4235dwarf_autocast_expanding_visitor::visit_autocast_op (autocast_op* e)
4236{
4237 // TODO PR13664, glean the type from e->operand, then expand like @cast.
4238
4239 var_expanding_visitor::visit_autocast_op (e);
4240}
4241
4242
bd1fcbad
YZ
4243struct dwarf_atvar_expanding_visitor: public var_expanding_visitor
4244{
4245 systemtap_session& s;
4246 dwarf_builder& db;
4247
4248 dwarf_atvar_expanding_visitor(systemtap_session& s, dwarf_builder& db):
4249 s(s), db(db) {}
4250 void visit_atvar_op (atvar_op* e);
4251};
4252
4253
4254struct dwarf_atvar_query: public base_query
4255{
4256 atvar_op& e;
4257 const bool userspace_p, lvalue;
4258 functioncall*& result;
4259 unsigned& tick;
4260 const string cu_name_pattern;
4261
4262 dwarf_atvar_query(dwflpp& dw, const string& module, atvar_op& e,
4263 const bool userspace_p, const bool lvalue,
4264 functioncall*& result,
4265 unsigned& tick):
4266 base_query(dw, module), e(e),
4267 userspace_p(userspace_p), lvalue(lvalue), result(result),
4268 tick(tick), cu_name_pattern(string("*/") + e.cu_name) {}
4269
4270 void handle_query_module ();
4271 void query_library (const char *) {}
4272 void query_plt (const char *entry, size_t addr) {}
5c378838 4273 static int atvar_query_cu (Dwarf_Die *cudie, dwarf_atvar_query *q);
bd1fcbad
YZ
4274};
4275
4276
4277int
5c378838 4278dwarf_atvar_query::atvar_query_cu (Dwarf_Die * cudie, dwarf_atvar_query *q)
bd1fcbad 4279{
bd1fcbad
YZ
4280 if (! q->e.cu_name.empty())
4281 {
c60517ca 4282 const char *die_name = dwarf_diename(cudie) ?: "";
bd1fcbad
YZ
4283 if (strcmp(die_name, q->e.cu_name.c_str()) != 0 // Perfect match
4284 && fnmatch(q->cu_name_pattern.c_str(), die_name, 0) != 0)
4285 {
4286 return DWARF_CB_OK;
4287 }
4288 }
4289
4290 try
4291 {
4292 vector<Dwarf_Die> scopes(1, *cudie);
4293
4294 q->dw.focus_on_cu (cudie);
4295
4296 if (! q->e.components.empty() &&
4297 q->e.components.back().type == target_symbol::comp_pretty_print)
4298 {
4299 dwarf_pretty_print dpp (q->dw, scopes, 0, q->e.sym_name(),
4300 q->userspace_p, q->e);
4301 q->result = dpp.expand();
4302 return DWARF_CB_ABORT;
4303 }
4304
4305 exp_type type = pe_long;
4306 string code = q->dw.literal_stmt_for_local (scopes, 0, q->e.sym_name(),
4307 &q->e, q->lvalue, type);
4308
4309 if (code.empty())
4310 return DWARF_CB_OK;
4311
4312 string fname = (string(q->lvalue ? "_dwarf_tvar_set"
4313 : "_dwarf_tvar_get")
4314 + "_" + q->e.sym_name()
4315 + "_" + lex_cast(q->tick++));
4316
4317 q->result = synthetic_embedded_deref_call (q->sess, fname, code, type,
4318 q->userspace_p, q->lvalue,
4319 &q->e);
4320 }
4321 catch (const semantic_error& er)
4322 {
4323 // Here we suppress the error because we often just have too many
4324 // when scanning all the CUs.
4325 return DWARF_CB_OK;
4326 }
4327
4328 if (q->result) {
4329 return DWARF_CB_ABORT;
4330 }
4331
4332 return DWARF_CB_OK;
4333}
4334
4335
4336void
4337dwarf_atvar_query::handle_query_module ()
4338{
4339
4340 dw.iterate_over_cus(atvar_query_cu, this, false);
4341}
4342
4343
4344void
4345dwarf_atvar_expanding_visitor::visit_atvar_op (atvar_op* e)
4346{
4347 const bool lvalue = is_active_lvalue(e);
4348 if (lvalue && !s.guru_mode)
dc09353a 4349 throw SEMANTIC_ERROR(_("write to @var variable not permitted; "
bd1fcbad
YZ
4350 "need stap -g"), e->tok);
4351
4352 if (e->module.empty())
4353 e->module = "kernel";
4354
4355 functioncall* result = NULL;
4356
4357 // split the module string by ':' for alternatives
4358 vector<string> modules;
4359 tokenize(e->module, modules, ":");
4360 bool userspace_p = false;
4361 for (unsigned i = 0; !result && i < modules.size(); ++i)
4362 {
4363 string& module = modules[i];
4364
4365 dwflpp* dw;
4366 try
4367 {
4368 userspace_p = is_user_module(module);
4369 if (!userspace_p)
4370 {
4371 // kernel or kernel module target
4372 dw = db.get_kern_dw(s, module);
4373 }
4374 else
4375 {
4376 module = find_executable(module, "", s.sysenv);
4377 dw = db.get_user_dw(s, module);
4378 }
4379 }
4380 catch (const semantic_error& er)
4381 {
4382 /* ignore and go to the next module */
4383 continue;
4384 }
4385
4386 dwarf_atvar_query q (*dw, module, *e, userspace_p, lvalue, result, tick);
06de3a04 4387 dw->iterate_over_modules<base_query>(&query_module, &q);
bd1fcbad
YZ
4388
4389 if (result)
4390 {
4391 s.unwindsym_modules.insert(module);
bd1fcbad
YZ
4392
4393 if (lvalue)
4394 {
4395 // Provide the functioncall to our parent, so that it can be
4396 // used to substitute for the assignment node immediately above
4397 // us.
4398 assert(!target_symbol_setter_functioncalls.empty());
4399 *(target_symbol_setter_functioncalls.top()) = result;
4400 }
4401
4402 result->visit(this);
4403 return;
4404 }
4405
4406 /* Unable to find the variable in the current module, so we chain
4407 * an error in atvar_op */
dc09353a 4408 semantic_error er(ERR_SRC, _F("unable to find global '%s' in %s%s%s",
bd1fcbad
YZ
4409 e->sym_name().c_str(), module.c_str(),
4410 e->cu_name.empty() ? "" : _(", in "),
4411 e->cu_name.c_str()));
4412 e->chain (er);
4413 }
4414
4415 provide(e);
4416}
4417
4418
b8da0ad1
FCE
4419void
4420dwarf_derived_probe::printsig (ostream& o) const
4421{
4422 // Instead of just printing the plain locations, we add a PC value
4423 // as a comment as a way of telling e.g. apart multiple inlined
4424 // function instances. This is distinct from the verbose/clog
4425 // output, since this part goes into the cache hash calculations.
4426 sole_location()->print (o);
6d0f3f0c 4427 o << " /* pc=" << section << "+0x" << hex << addr << dec << " */";
b8da0ad1
FCE
4428 printsig_nested (o);
4429}
4430
4431
4432
dc38c0ae 4433void
b20febf3
FCE
4434dwarf_derived_probe::join_group (systemtap_session& s)
4435{
af234c40
JS
4436 // skip probes which are paired entry-handlers
4437 if (!has_return && (saved_longs || saved_strings))
4438 return;
4439
b20febf3
FCE
4440 if (! s.dwarf_derived_probes)
4441 s.dwarf_derived_probes = new dwarf_derived_probe_group ();
4442 s.dwarf_derived_probes->enroll (this);
4443}
4444
4445
2b69faaf
JS
4446static bool
4447kernel_supports_inode_uprobes(systemtap_session& s)
4448{
4449 // The arch-supports is new to the builtin inode-uprobes, so it makes a
4450 // reasonable indicator of the new API. Else we'll need an autoconf...
64e807c2 4451 // see also buildrun.cxx:kernel_built_uprobs()
2b69faaf
JS
4452 return (s.kernel_config["CONFIG_ARCH_SUPPORTS_UPROBES"] == "y"
4453 && s.kernel_config["CONFIG_UPROBES"] == "y");
4454}
4455
4456
3667d615
JS
4457static bool
4458kernel_supports_inode_uretprobes(systemtap_session& s)
4459{
766013af
JS
4460 // We need inode-uprobes first, then look for a sign of uretprobes. The only
4461 // non-static function at present is arch_uretprobe_hijack_return_addr.
3667d615 4462 return kernel_supports_inode_uprobes(s) &&
af9e147f 4463 (s.kernel_functions.count("arch_uretprobe_hijack_return_addr") > 0);
3667d615
JS
4464}
4465
4466
5261f7ab
DS
4467void
4468check_process_probe_kernel_support(systemtap_session& s)
4469{
4470 // If we've got utrace, we're good to go.
4471 if (s.kernel_config["CONFIG_UTRACE"] == "y")
4472 return;
4473
8c021542
DS
4474 // We don't have utrace. For process probes that aren't
4475 // uprobes-based, we just need the task_finder. The task_finder
b266d318
DS
4476 // needs CONFIG_TRACEPOINTS and specific tracepoints. There is a
4477 // specific autoconf test for its needs.
8c021542
DS
4478 //
4479 // We'll just require CONFIG_TRACEPOINTS here as a quick-and-dirty
4480 // approximation.
4481 if (! s.need_uprobes && s.kernel_config["CONFIG_TRACEPOINTS"] == "y")
4482 return;
4483
d3e959b0
DS
4484 // For uprobes-based process probes, we need the task_finder plus
4485 // the builtin inode-uprobes.
8c021542
DS
4486 if (s.need_uprobes
4487 && s.kernel_config["CONFIG_TRACEPOINTS"] == "y"
d3e959b0 4488 && kernel_supports_inode_uprobes(s))
8c021542
DS
4489 return;
4490
dc09353a 4491 throw SEMANTIC_ERROR (_("process probes not available without kernel CONFIG_UTRACE or CONFIG_TRACEPOINTS/CONFIG_ARCH_SUPPORTS_UPROBES/CONFIG_UPROBES"));
5261f7ab
DS
4492}
4493
4494
b20febf3
FCE
4495dwarf_derived_probe::dwarf_derived_probe(const string& funcname,
4496 const string& filename,
4497 int line,
91af0778 4498 // module & section specify a relocation
b20febf3
FCE
4499 // base for <addr>, unless section==""
4500 // (equivalently module=="kernel")
4501 const string& module,
4502 const string& section,
4503 // NB: dwfl_addr is the virtualized
4504 // address for this symbol.
4505 Dwarf_Addr dwfl_addr,
4506 // addr is the section-offset for
4507 // actual relocation.
4508 Dwarf_Addr addr,
4509 dwarf_query& q,
37ebca01 4510 Dwarf_Die* scope_die /* may be null */)
4c5d1300 4511 : derived_probe (q.base_probe, q.base_loc, true /* .components soon rewritten */ ),
b20febf3 4512 module (module), section (section), addr (addr),
63b4fd14 4513 path (q.path),
27dc09b1 4514 has_process (q.has_process),
c9bad430
DS
4515 has_return (q.has_return),
4516 has_maxactive (q.has_maxactive),
c57ea854 4517 has_library (q.has_library),
6b66b9f7 4518 maxactive_val (q.maxactive_val),
b642c901
SC
4519 user_path (q.user_path),
4520 user_lib (q.user_lib),
af234c40 4521 access_vars(false),
c57ea854 4522 saved_longs(0), saved_strings(0),
af234c40 4523 entry_handler(0)
bd2b1e68 4524{
b642c901
SC
4525 if (user_lib.size() != 0)
4526 has_library = true;
4527
6b66b9f7
JS
4528 if (q.has_process)
4529 {
4530 // We may receive probes on two types of ELF objects: ET_EXEC or ET_DYN.
4531 // ET_EXEC ones need no further relocation on the addr(==dwfl_addr), whereas
4532 // ET_DYN ones do (addr += run-time mmap base address). We tell these apart
4533 // by the incoming section value (".absolute" vs. ".dynamic").
4534 // XXX Assert invariants here too?
2b69faaf
JS
4535
4536 // inode-uprobes needs an offset rather than an absolute VM address.
4441e344 4537 // ditto for userspace runtimes (dyninst)
ac3af990 4538 if ((kernel_supports_inode_uprobes(q.dw.sess) || q.dw.sess.runtime_usermode_p()) &&
2b69faaf
JS
4539 section == ".absolute" && addr == dwfl_addr &&
4540 addr >= q.dw.module_start && addr < q.dw.module_end)
4541 this->addr = addr - q.dw.module_start;
6b66b9f7
JS
4542 }
4543 else
4544 {
4545 // Assert kernel relocation invariants
4546 if (section == "" && dwfl_addr != addr) // addr should be absolute
dc09353a 4547 throw SEMANTIC_ERROR (_("missing relocation basis"), tok);
6b66b9f7 4548 if (section != "" && dwfl_addr == addr) // addr should be an offset
dc09353a 4549 throw SEMANTIC_ERROR (_("inconsistent relocation address"), tok);
6b66b9f7 4550 }
2930abc7 4551
21beacc9
FCE
4552 // XXX: hack for strange g++/gcc's
4553#ifndef USHRT_MAX
4554#define USHRT_MAX 32767
4555#endif
4556
606fd9c8 4557 // Range limit maxactive() value
6b66b9f7 4558 if (has_maxactive && (maxactive_val < 0 || maxactive_val > USHRT_MAX))
dc09353a 4559 throw SEMANTIC_ERROR (_F("maxactive value out of range [0,%s]",
b530b5b3 4560 lex_cast(USHRT_MAX).c_str()), q.base_loc->components.front()->tok);
606fd9c8 4561
a7696882
JL
4562 // Expand target variables in the probe body. Even if the scope_die is
4563 // invalid, we still want to expand things such as $$vars/$$parms/etc...
4564 // (PR15999, PR16473). Access to specific context vars e.g. $argc will not be
4565 // expanded and will produce an error during the typeresolution_info pass.
8fc05e57 4566 {
6b66b9f7 4567 // XXX: user-space deref's for q.has_process!
5a617dc6
JL
4568
4569 // PR14436: if we're expanding target variables in the probe body of a
4570 // .return probe, we need to make the expansion at the postprologue addr
4571 // instead (if any), which is then also the spot where the entry handler
4572 // probe is placed. (Note that at this point, a nonzero prologue_end
4573 // implies that it should be used, i.e. code is unoptimized).
4574 Dwarf_Addr handler_dwfl_addr = dwfl_addr;
4575 if (q.prologue_end != 0 && q.has_return)
4576 {
4577 handler_dwfl_addr = q.prologue_end;
4578 if (q.sess.verbose > 2)
4579 clog << _F("expanding .return vars at prologue_end (0x%s) "
4580 "rather than entrypc (0x%s)\n",
4581 lex_cast_hex(handler_dwfl_addr).c_str(),
4582 lex_cast_hex(dwfl_addr).c_str());
4583 }
4584 dwarf_var_expanding_visitor v (q, scope_die, handler_dwfl_addr);
8b095b45 4585 v.replace (this->body);
3689db05
SC
4586
4587 // Propagate perf.counters so we can emit later
4588 this->perf_counter_refs = v.perf_counter_refs;
4589 // Emit local var used to save the perf counter read value
698de6cc 4590 std::set<derived_probe*>::iterator pcii;
3689db05
SC
4591 for (pcii = v.perf_counter_refs.begin();
4592 pcii != v.perf_counter_refs.end(); pcii++)
4593 {
4594 map<string, pair<string,derived_probe*> >::iterator it;
4595 // Find the associated perf counter probe
4596 for (it=q.sess.perf_counters.begin() ;
4597 it != q.sess.perf_counters.end();
4598 it++)
4599 if ((*it).second.second == (*pcii))
4600 break;
4601 vardecl* vd = new vardecl;
4602 vd->name = "__perf_read_" + (*it).first;
4603 vd->tok = this->tok;
4604 vd->set_arity(0, this->tok);
4605 vd->type = pe_long;
4606 vd->synthetic = true;
4607 this->locals.push_back (vd);
4608 }
4609
4610
6b66b9f7
JS
4611 if (!q.has_process)
4612 access_vars = v.visited;
37ebca01
FCE
4613
4614 // If during target-variable-expanding the probe, we added a new block
4615 // of code, add it to the start of the probe.
4616 if (v.add_block)
ba6f838d 4617 this->body = new block(v.add_block, this->body);
2260f4e3
FCE
4618
4619 // If when target-variable-expanding the probe, we need to synthesize a
4620 // sibling function-entry probe. We don't go through the whole probe derivation
4621 // business (PR10642) that could lead to wildcard/alias resolution, or for that
4622 // dwarf-induced duplication.
4623 if (v.add_call_probe)
37ebca01 4624 {
2260f4e3
FCE
4625 assert (q.has_return && !q.has_call);
4626
4627 // We temporarily replace q.base_probe.
4628 statement* old_body = q.base_probe->body;
4629 q.base_probe->body = v.add_call_probe;
4630 q.has_return = false;
4631 q.has_call = true;
af234c40 4632
da23eceb 4633 if (q.has_process)
5a617dc6
JL
4634 {
4635 // Place handler probe at the same addr as where the vars were
4636 // expanded (which may not be the same addr as the one for the
4637 // main retprobe, PR14436).
4638 Dwarf_Addr handler_addr = addr;
4639 if (handler_dwfl_addr != dwfl_addr)
4640 // adjust section offset by prologue_end-entrypc
4641 handler_addr += handler_dwfl_addr - dwfl_addr;
4642 entry_handler = new uprobe_derived_probe (funcname, filename,
4643 line, module, section,
4644 handler_dwfl_addr,
4645 handler_addr, q,
4646 scope_die);
4647 }
da23eceb 4648 else
af234c40
JS
4649 entry_handler = new dwarf_derived_probe (funcname, filename, line,
4650 module, section, dwfl_addr,
4651 addr, q, scope_die);
4652
4653 saved_longs = entry_handler->saved_longs = v.saved_longs;
4654 saved_strings = entry_handler->saved_strings = v.saved_strings;
4655
4656 q.results.push_back (entry_handler);
2260f4e3
FCE
4657
4658 q.has_return = true;
4659 q.has_call = false;
4660 q.base_probe->body = old_body;
37ebca01 4661 }
a7696882
JL
4662 // Save the local variables for listing mode. If the scope_die is null,
4663 // local vars aren't accessible, so no need to invoke saveargs (PR10820).
bba368c5
JL
4664 if (!null_die(scope_die) &&
4665 q.sess.dump_mode == systemtap_session::dump_matched_probes_vars)
4666 saveargs(q, scope_die, dwfl_addr);
8fc05e57 4667 }
0a98fd42 4668
5d23847d 4669 // Reset the sole element of the "locations" vector as a
b20febf3
FCE
4670 // "reverse-engineered" form of the incoming (q.base_loc) probe
4671 // point. This allows a user to see what function / file / line
4672 // number any particular match of the wildcards.
919debfc
JL
4673
4674 vector<probe_point::component*> comps;
4675 if (q.has_kernel)
4676 comps.push_back (new probe_point::component(TOK_KERNEL));
4677 else if(q.has_module)
4678 comps.push_back (new probe_point::component(TOK_MODULE, new literal_string(module)));
4679 else if(q.has_process)
4680 comps.push_back (new probe_point::component(TOK_PROCESS, new literal_string(module)));
4681 else
4682 assert (0);
4683
4684 string fn_or_stmt;
4685 if (q.has_function_str || q.has_function_num)
e772a6e7 4686 fn_or_stmt = TOK_FUNCTION;
919debfc 4687 else
e772a6e7 4688 fn_or_stmt = TOK_STATEMENT;
919debfc
JL
4689
4690 if (q.has_function_str || q.has_statement_str)
4691 {
e772a6e7 4692 string retro_name = q.final_function_name(funcname, filename, line);
919debfc
JL
4693 comps.push_back
4694 (new probe_point::component
4695 (fn_or_stmt, new literal_string (retro_name)));
4696 }
4697 else if (q.has_function_num || q.has_statement_num)
4698 {
4699 Dwarf_Addr retro_addr;
4700 if (q.has_function_num)
4701 retro_addr = q.function_num_val;
4702 else
4703 retro_addr = q.statement_num_val;
4704 comps.push_back (new probe_point::component
4705 (fn_or_stmt,
4706 new literal_number(retro_addr, true)));
4707
4708 if (q.has_absolute)
4709 comps.push_back (new probe_point::component (TOK_ABSOLUTE));
4710 }
4711
4712 if (q.has_call)
4713 comps.push_back (new probe_point::component(TOK_CALL));
4714 if (q.has_exported)
4715 comps.push_back (new probe_point::component(TOK_EXPORTED));
4716 if (q.has_inline)
4717 comps.push_back (new probe_point::component(TOK_INLINE));
4718 if (has_return)
4719 comps.push_back (new probe_point::component(TOK_RETURN));
4720 if (has_maxactive)
4721 comps.push_back (new probe_point::component
4722 (TOK_MAXACTIVE, new literal_number(maxactive_val)));
4723
4724 // Overwrite it.
4725 this->sole_location()->components = comps;
e2941743
JL
4726
4727 // if it's a .callee[s[(N)]] call, add checks to the probe body so that the
4728 // user body is only 'triggered' when called from q.callers[N-1], which
4729 // itself is called from q.callers[N-2], etc... I.E.
4730 // callees(N) --> N elements in q.callers --> N checks against [u]stack(0..N-1)
4731 if ((q.has_callee || q.has_callees_num) && q.callers && !q.callers->empty())
4732 {
4733 if (q.sess.verbose > 2)
7761da8b 4734 clog << _F("adding caller checks for callee %s\n", funcname.c_str());
e2941743
JL
4735
4736 // Copy the stack and empty it out
4737 stack<Dwarf_Addr> callers(*q.callers);
4738 for (unsigned level = 1; !callers.empty(); level++,
4739 callers.pop())
4740 {
4741 Dwarf_Addr caller = callers.top();
4742
4743 // We first need to make the caller addr relocatable
4744 string caller_section;
4745 Dwarf_Addr caller_reloc;
4746 if (module == TOK_KERNEL)
4747 { // allow for relocatable kernel (see also add_probe_point())
4748 caller_reloc = caller - q.sess.sym_stext;
4749 caller_section = "_stext";
4750 }
4751 else
4752 caller_reloc = q.dw.relocate_address(caller,
4753 caller_section);
4754
4755 if (q.sess.verbose > 2)
7761da8b
JL
4756 clog << _F("adding caller check [u]stack(%d) == reloc(0x%s)\n",
4757 level, lex_cast_hex(caller_reloc).c_str());
e2941743
JL
4758
4759 // We want to add a statement like this:
6211aed9 4760 // if (!_caller_match(user, mod, sec, addr)) next;
e2941743
JL
4761 // Something similar is done in semantic_pass_conditions()
4762
4763 functioncall* check = new functioncall();
4764 check->tok = this->tok;
6211aed9 4765 check->function = "_caller_match";
e2941743
JL
4766 check->args.push_back(new literal_number(q.has_process));
4767 check->args[0]->tok = this->tok;
4768 check->args.push_back(new literal_number(level));
4769 check->args[1]->tok = this->tok;
4770 check->args.push_back(new literal_string(module));
4771 check->args[2]->tok = this->tok;
4772 check->args.push_back(new literal_string(caller_section));
4773 check->args[3]->tok = this->tok;
4774 check->args.push_back(new literal_number(caller_reloc, true /* hex */));
4775 check->args[4]->tok = this->tok;
4776
4777 unary_expression* notexp = new unary_expression();
4778 notexp->tok = this->tok;
4779 notexp->op = "!";
4780 notexp->operand = check;
4781
4782 if_statement* ifs = new if_statement();
4783 ifs->tok = this->tok;
4784 ifs->thenblock = new next_statement();
4785 ifs->thenblock->tok = this->tok;
4786 ifs->elseblock = NULL;
4787 ifs->condition = notexp;
4788
4789 this->body = new block(ifs, this->body);
4790 }
4791 }
2930abc7
FCE
4792}
4793
bd2b1e68 4794
0a98fd42 4795void
8c67c337
JS
4796dwarf_derived_probe::saveargs(dwarf_query& q, Dwarf_Die* scope_die,
4797 Dwarf_Addr dwfl_addr)
0a98fd42 4798{
9aa8ffce 4799 if (null_die(scope_die))
0a98fd42 4800 return;
0a98fd42 4801
8c67c337 4802 bool verbose = q.sess.verbose > 2;
0a98fd42 4803
8c67c337 4804 if (verbose)
b530b5b3 4805 clog << _F("saveargs: examining '%s' (dieoffset: %#" PRIx64 ")\n", (dwarf_diename(scope_die)?: "unknown"), dwarf_dieoffset(scope_die));
0a98fd42 4806
8c67c337
JS
4807 if (has_return)
4808 {
4809 /* Only save the return value if it has a type. */
4810 string type_name;
4811 Dwarf_Die type_die;
4812 if (dwarf_attr_die (scope_die, DW_AT_type, &type_die) &&
4813 dwarf_type_name(&type_die, type_name))
4814 args.push_back("$return:"+type_name);
4815
4816 else if (verbose)
b530b5b3
LB
4817 clog << _F("saveargs: failed to retrieve type name for return value (dieoffset: %s)\n",
4818 lex_cast_hex(dwarf_dieoffset(scope_die)).c_str());
8c67c337 4819 }
d87623a1 4820
0a98fd42 4821 Dwarf_Die arg;
4ef35696
JS
4822 vector<Dwarf_Die> scopes = q.dw.getscopes(scope_die);
4823 for (unsigned i = 0; i < scopes.size(); ++i)
4824 {
4825 if (dwarf_tag(&scopes[i]) == DW_TAG_compile_unit)
4826 break; // we don't want file-level variables
4827 if (dwarf_child (&scopes[i], &arg) == 0)
4828 do
0a98fd42 4829 {
4ef35696
JS
4830 switch (dwarf_tag (&arg))
4831 {
4832 case DW_TAG_variable:
4833 case DW_TAG_formal_parameter:
4834 break;
0a98fd42 4835
4ef35696
JS
4836 default:
4837 continue;
4838 }
0a98fd42 4839
4ef35696
JS
4840 /* Ignore this local if it has no name. */
4841 const char *arg_name = dwarf_diename (&arg);
4842 if (!arg_name)
8c67c337
JS
4843 {
4844 if (verbose)
b530b5b3
LB
4845 clog << _F("saveargs: failed to retrieve name for local (dieoffset: %s)\n",
4846 lex_cast_hex(dwarf_dieoffset(&arg)).c_str());
8c67c337
JS
4847 continue;
4848 }
4ef35696
JS
4849
4850 if (verbose)
b530b5b3
LB
4851 clog << _F("saveargs: finding location for local '%s' (dieoffset: %s)\n",
4852 arg_name, lex_cast_hex(dwarf_dieoffset(&arg)).c_str());
4ef35696
JS
4853
4854 /* Ignore this local if it has no location (or not at this PC). */
4855 /* NB: It still may not be directly accessible, e.g. if it is an
4856 * aggregate type, implicit_pointer, etc., but the user can later
4857 * figure out how to access the interesting parts. */
45b02a36
FCE
4858
4859 /* XXX: Perhaps saveargs() / listings-mode should work by synthesizing
4860 * several synthetic
4861 * probe foo { $var }
4862 * probes, testing them for overall resolvability.
4863 */
4864
4ef35696
JS
4865 Dwarf_Attribute attr_mem;
4866 if (!dwarf_attr_integrate (&arg, DW_AT_const_value, &attr_mem))
4867 {
4868 Dwarf_Op *expr;
4869 size_t len;
4870 if (!dwarf_attr_integrate (&arg, DW_AT_location, &attr_mem))
4871 {
4872 if (verbose)
b530b5b3
LB
4873 clog << _F("saveargs: failed to resolve the location for local '%s' (dieoffset: %s)\n",
4874 arg_name, lex_cast_hex(dwarf_dieoffset(&arg)).c_str());
4ef35696
JS
4875 continue;
4876 }
4877 else if (!(dwarf_getlocation_addr(&attr_mem, dwfl_addr, &expr,
4878 &len, 1) == 1 && len > 0))
4879 {
45b02a36
FCE
4880 Dwarf_Addr dwfl_addr2 = q.dw.pr15123_retry_addr (dwfl_addr, & arg);
4881 if (!dwfl_addr2 || (!(dwarf_getlocation_addr(&attr_mem, dwfl_addr2, &expr,
4882 &len, 1) == 1 && len > 0))) {
4883 if (verbose)
4884 clog << _F("saveargs: local '%s' (dieoffset: %s) is not available at this address (%s)\n",
4885 arg_name, lex_cast_hex(dwarf_dieoffset(&arg)).c_str(), lex_cast_hex(dwfl_addr).c_str());
4886 continue;
4887 }
4ef35696
JS
4888 }
4889 }
4890
4891 /* Ignore this local if it has no type. */
4892 string type_name;
4893 Dwarf_Die type_die;
4894 if (!dwarf_attr_die (&arg, DW_AT_type, &type_die) ||
4895 !dwarf_type_name(&type_die, type_name))
8c67c337
JS
4896 {
4897 if (verbose)
b530b5b3
LB
4898 clog << _F("saveargs: failed to retrieve type name for local '%s' (dieoffset: %s)\n",
4899 arg_name, lex_cast_hex(dwarf_dieoffset(&arg)).c_str());
8c67c337
JS
4900 continue;
4901 }
8c67c337 4902
4ef35696
JS
4903 /* This local looks good -- save it! */
4904 args.push_back("$"+string(arg_name)+":"+type_name);
8c67c337 4905 }
4ef35696
JS
4906 while (dwarf_siblingof (&arg, &arg) == 0);
4907 }
0a98fd42
JS
4908}
4909
4910
4911void
d0bfd2ac 4912dwarf_derived_probe::getargs(std::list<std::string> &arg_set) const
0a98fd42 4913{
d0bfd2ac 4914 arg_set.insert(arg_set.end(), args.begin(), args.end());
0a98fd42
JS
4915}
4916
4917
27dc09b1 4918void
42e38653 4919dwarf_derived_probe::emit_privilege_assertion (translator_output* o)
27dc09b1
DB
4920{
4921 if (has_process)
4922 {
4923 // These probes are allowed for unprivileged users, but only in the
4924 // context of processes which they own.
4925 emit_process_owner_assertion (o);
4926 return;
4927 }
4928
4929 // Other probes must contain the default assertion which aborts
4930 // if executed by an unprivileged user.
42e38653 4931 derived_probe::emit_privilege_assertion (o);
27dc09b1
DB
4932}
4933
4934
4935void
4936dwarf_derived_probe::print_dupe_stamp(ostream& o)
4937{
4938 if (has_process)
4939 {
4940 // These probes are allowed for unprivileged users, but only in the
4941 // context of processes which they own.
4942 print_dupe_stamp_unprivileged_process_owner (o);
4943 return;
4944 }
4945
4946 // Other probes must contain the default dupe stamp
4947 derived_probe::print_dupe_stamp (o);
4948}
4949
64211010 4950
7a053d3b 4951void
20c6c071 4952dwarf_derived_probe::register_statement_variants(match_node * root,
27dc09b1 4953 dwarf_builder * dw,
42e38653 4954 privilege_t privilege)
bd2b1e68 4955{
27dc09b1 4956 root
42e38653 4957 ->bind_privilege(privilege)
27dc09b1 4958 ->bind(dw);
54efe513
GH
4959}
4960
7a053d3b 4961void
fd6602a0 4962dwarf_derived_probe::register_function_variants(match_node * root,
27dc09b1 4963 dwarf_builder * dw,
42e38653 4964 privilege_t privilege)
2865d17a 4965{
27dc09b1 4966 root
42e38653 4967 ->bind_privilege(privilege)
27dc09b1 4968 ->bind(dw);
27dc09b1 4969 root->bind(TOK_CALL)
42e38653 4970 ->bind_privilege(privilege)
27dc09b1 4971 ->bind(dw);
4bda987e
SC
4972 root->bind(TOK_EXPORTED)
4973 ->bind_privilege(privilege)
4974 ->bind(dw);
27dc09b1 4975 root->bind(TOK_RETURN)
42e38653 4976 ->bind_privilege(privilege)
27dc09b1 4977 ->bind(dw);
1e035395 4978
f6be7c06
DB
4979 // For process probes / uprobes, .maxactive() is unused.
4980 if (! pr_contains (privilege, pr_stapusr))
1e035395
FCE
4981 {
4982 root->bind(TOK_RETURN)
1e035395
FCE
4983 ->bind_num(TOK_MAXACTIVE)->bind(dw);
4984 }
bd2b1e68
GH
4985}
4986
7a053d3b 4987void
27dc09b1 4988dwarf_derived_probe::register_function_and_statement_variants(
440d9b00 4989 systemtap_session& s,
27dc09b1
DB
4990 match_node * root,
4991 dwarf_builder * dw,
42e38653 4992 privilege_t privilege
27dc09b1 4993)
bd2b1e68
GH
4994{
4995 // Here we match 4 forms:
4996 //
4997 // .function("foo")
4998 // .function(0xdeadbeef)
4999 // .statement("foo")
5000 // .statement(0xdeadbeef)
5001
440d9b00 5002 match_node *fv_root = root->bind_str(TOK_FUNCTION);
42e38653 5003 register_function_variants(fv_root, dw, privilege);
7f02ca94 5004 // ROOT.function("STRING") always gets the .inline and .label variants.
440d9b00 5005 fv_root->bind(TOK_INLINE)
42e38653 5006 ->bind_privilege(privilege)
440d9b00 5007 ->bind(dw);
7f02ca94
JS
5008 fv_root->bind_str(TOK_LABEL)
5009 ->bind_privilege(privilege)
440d9b00 5010 ->bind(dw);
c31add50
JL
5011 fv_root->bind_str(TOK_CALLEE)
5012 ->bind_privilege(privilege)
5013 ->bind(dw);
5014 fv_root->bind(TOK_CALLEES)
5015 ->bind_privilege(privilege)
5016 ->bind(dw);
5017 fv_root->bind_num(TOK_CALLEES)
5018 ->bind_privilege(privilege)
5019 ->bind(dw);
440d9b00
DB
5020
5021 fv_root = root->bind_num(TOK_FUNCTION);
42e38653 5022 register_function_variants(fv_root, dw, privilege);
440d9b00
DB
5023 // ROOT.function(NUMBER).inline is deprecated in release 1.7 and removed thereafter.
5024 if (strverscmp(s.compatible.c_str(), "1.7") <= 0)
5025 {
5026 fv_root->bind(TOK_INLINE)
42e38653 5027 ->bind_privilege(privilege)
440d9b00
DB
5028 ->bind(dw);
5029 }
5030
42e38653
DB
5031 register_statement_variants(root->bind_str(TOK_STATEMENT), dw, privilege);
5032 register_statement_variants(root->bind_num(TOK_STATEMENT), dw, privilege);
bd2b1e68
GH
5033}
5034
b1615c74
JS
5035void
5036dwarf_derived_probe::register_sdt_variants(systemtap_session& s,
5037 match_node * root,
5038 dwarf_builder * dw)
5039{
5040 root->bind_str(TOK_MARK)
f66bb29a 5041 ->bind_privilege(pr_all)
b1615c74
JS
5042 ->bind(dw);
5043 root->bind_str(TOK_PROVIDER)->bind_str(TOK_MARK)
f66bb29a 5044 ->bind_privilege(pr_all)
b1615c74
JS
5045 ->bind(dw);
5046}
5047
5048void
5049dwarf_derived_probe::register_plt_variants(systemtap_session& s,
5050 match_node * root,
5051 dwarf_builder * dw)
5052{
5053 root->bind(TOK_PLT)
f66bb29a 5054 ->bind_privilege(pr_all)
b1615c74
JS
5055 ->bind(dw);
5056 root->bind_str(TOK_PLT)
f66bb29a 5057 ->bind_privilege(pr_all)
b1615c74
JS
5058 ->bind(dw);
5059 root->bind(TOK_PLT)->bind_num(TOK_STATEMENT)
f66bb29a 5060 ->bind_privilege(pr_all)
b1615c74
JS
5061 ->bind(dw);
5062 root->bind_str(TOK_PLT)->bind_num(TOK_STATEMENT)
f66bb29a 5063 ->bind_privilege(pr_all)
b1615c74 5064 ->bind(dw);
bd2b1e68
GH
5065}
5066
5067void
c4ce66a1 5068dwarf_derived_probe::register_patterns(systemtap_session& s)
bd2b1e68 5069{
c4ce66a1 5070 match_node* root = s.pattern_root;
bd2b1e68
GH
5071 dwarf_builder *dw = new dwarf_builder();
5072
c4ce66a1
JS
5073 update_visitor *filter = new dwarf_cast_expanding_visitor(s, *dw);
5074 s.code_filters.push_back(filter);
5075
bd1fcbad
YZ
5076 filter = new dwarf_atvar_expanding_visitor(s, *dw);
5077 s.code_filters.push_back(filter);
5078
251707c8
JS
5079 // NB: visit autocast last, so it can use types resolved from @cast/@var
5080 filter = new dwarf_autocast_expanding_visitor(s);
5081 s.code_filters.push_back(filter);
5082
73f52eb4
DB
5083 register_function_and_statement_variants(s, root->bind(TOK_KERNEL), dw, pr_privileged);
5084 register_function_and_statement_variants(s, root->bind_str(TOK_MODULE), dw, pr_privileged);
27dc09b1
DB
5085 root->bind(TOK_KERNEL)->bind_num(TOK_STATEMENT)->bind(TOK_ABSOLUTE)
5086 ->bind(dw);
2cab6244 5087
7f02ca94
JS
5088 match_node* uprobes[] = {
5089 root->bind(TOK_PROCESS),
5090 root->bind_str(TOK_PROCESS),
5091 root->bind(TOK_PROCESS)->bind_str(TOK_LIBRARY),
5092 root->bind_str(TOK_PROCESS)->bind_str(TOK_LIBRARY),
5093 };
5094 for (size_t i = 0; i < sizeof(uprobes) / sizeof(*uprobes); ++i)
5095 {
f66bb29a 5096 register_function_and_statement_variants(s, uprobes[i], dw, pr_all);
7f02ca94
JS
5097 register_sdt_variants(s, uprobes[i], dw);
5098 register_plt_variants(s, uprobes[i], dw);
5099 }
bd2b1e68
GH
5100}
5101
9020300d 5102void
3689db05
SC
5103dwarf_derived_probe::emit_probe_local_init(systemtap_session& s, translator_output * o)
5104{
698de6cc 5105 std::set<derived_probe*>::iterator pcii;
3689db05
SC
5106 for (pcii = perf_counter_refs.begin();
5107 pcii != perf_counter_refs.end();
5108 pcii++)
5109 {
5110 map<string, pair<string,derived_probe*> >::iterator it;
5111 // Find the associated perf.counter probe
5112 unsigned i = 0;
5113 for (it=s.perf_counters.begin() ;
5114 it != s.perf_counters.end();
5115 it++, i++)
5116 if ((*it).second.second == (*pcii))
5117 break;
5118 // place the perf counter read so it precedes stp_lock_probe
5119 o->newline() << "l->l___perf_read_" + (*it).first
5120 + " = (((int64_t) (_stp_perf_read(smp_processor_id(),"
5121 + lex_cast(i) + "))));";
5122 }
b78a0fbb 5123
b95e2b79
MH
5124 if (access_vars)
5125 {
5126 // if accessing $variables, emit bsp cache setup for speeding up
d4670309 5127 o->newline() << "#if defined __ia64__";
d9aed31e 5128 o->newline() << "bspcache(c->unwaddr, c->kregs);";
d4670309 5129 o->newline() << "#endif";
b95e2b79 5130 }
9020300d 5131}
2930abc7 5132
b20febf3 5133// ------------------------------------------------------------------------
46b84a80
DS
5134
5135void
b20febf3 5136dwarf_derived_probe_group::enroll (dwarf_derived_probe* p)
46b84a80 5137{
b20febf3 5138 probes_by_module.insert (make_pair (p->module, p));
b8da0ad1
FCE
5139
5140 // XXX: probes put at the same address should all share a
5141 // single kprobe/kretprobe, and have their handlers executed
5142 // sequentially.
b55bc428
FCE
5143}
5144
7a053d3b 5145void
775d51e5 5146dwarf_derived_probe_group::emit_module_decls (systemtap_session& s)
ec4373ff 5147{
b20febf3 5148 if (probes_by_module.empty()) return;
2930abc7 5149
775d51e5
DS
5150 s.op->newline() << "/* ---- dwarf probes ---- */";
5151
5152 // Warn of misconfigured kernels
f41595cc
FCE
5153 s.op->newline() << "#if ! defined(CONFIG_KPROBES)";
5154 s.op->newline() << "#error \"Need CONFIG_KPROBES!\"";
5155 s.op->newline() << "#endif";
775d51e5 5156 s.op->newline();
f41595cc 5157
f07c3b68 5158 s.op->newline() << "#ifndef KRETACTIVE";
1ee6b5fc 5159 s.op->newline() << "#define KRETACTIVE (max(15,6*(int)num_possible_cpus()))";
f07c3b68
FCE
5160 s.op->newline() << "#endif";
5161
14cf7e42 5162 // Forward decls
2ba1736a 5163 s.op->newline() << "#include \"linux/kprobes-common.h\"";
14cf7e42 5164
b20febf3
FCE
5165 // Forward declare the master entry functions
5166 s.op->newline() << "static int enter_kprobe_probe (struct kprobe *inst,";
5167 s.op->line() << " struct pt_regs *regs);";
5168 s.op->newline() << "static int enter_kretprobe_probe (struct kretprobe_instance *inst,";
5169 s.op->line() << " struct pt_regs *regs);";
5170
42cb22bd
MH
5171 // Emit an array of kprobe/kretprobe pointers
5172 s.op->newline() << "#if defined(STAPCONF_UNREGISTER_KPROBES)";
5173 s.op->newline() << "static void * stap_unreg_kprobes[" << probes_by_module.size() << "];";
5174 s.op->newline() << "#endif";
5175
b20febf3 5176 // Emit the actual probe list.
606fd9c8
FCE
5177
5178 // NB: we used to plop a union { struct kprobe; struct kretprobe } into
5179 // struct stap_dwarf_probe, but it being initialized data makes it add
5180 // hundreds of bytes of padding per stap_dwarf_probe. (PR5673)
14cf7e42 5181 s.op->newline() << "static struct stap_dwarf_kprobe stap_dwarf_kprobes[" << probes_by_module.size() << "];";
606fd9c8
FCE
5182 // NB: bss!
5183
4c2732a1 5184 s.op->newline() << "static struct stap_dwarf_probe {";
b0986e7a
DS
5185 s.op->newline(1) << "const unsigned return_p:1;";
5186 s.op->newline() << "const unsigned maxactive_p:1;";
b350f56b 5187 s.op->newline() << "const unsigned optional_p:1;";
b20febf3 5188 s.op->newline() << "unsigned registered_p:1;";
b0986e7a 5189 s.op->newline() << "const unsigned short maxactive_val;";
606fd9c8 5190
af234c40
JS
5191 // data saved in the kretprobe_instance packet
5192 s.op->newline() << "const unsigned short saved_longs;";
5193 s.op->newline() << "const unsigned short saved_strings;";
5194
faea5e16 5195 // Let's find some stats for the embedded strings. Maybe they
606fd9c8
FCE
5196 // are small and uniform enough to justify putting char[MAX]'s into
5197 // the array instead of relocated char*'s.
faea5e16
JS
5198 size_t module_name_max = 0, section_name_max = 0;
5199 size_t module_name_tot = 0, section_name_tot = 0;
606fd9c8
FCE
5200 size_t all_name_cnt = probes_by_module.size(); // for average
5201 for (p_b_m_iterator it = probes_by_module.begin(); it != probes_by_module.end(); it++)
5202 {
5203 dwarf_derived_probe* p = it->second;
5204#define DOIT(var,expr) do { \
5205 size_t var##_size = (expr) + 1; \
5206 var##_max = max (var##_max, var##_size); \
5207 var##_tot += var##_size; } while (0)
5208 DOIT(module_name, p->module.size());
5209 DOIT(section_name, p->section.size());
606fd9c8
FCE
5210#undef DOIT
5211 }
5212
5213 // Decide whether it's worthwhile to use char[] or char* by comparing
5214 // the amount of average waste (max - avg) to the relocation data size
5215 // (3 native long words).
5216#define CALCIT(var) \
5217 if ((var##_name_max-(var##_name_tot/all_name_cnt)) < (3 * sizeof(void*))) \
5218 { \
5219 s.op->newline() << "const char " << #var << "[" << var##_name_max << "];"; \
5220 if (s.verbose > 2) clog << "stap_dwarf_probe " << #var \
5221 << "[" << var##_name_max << "]" << endl; \
5222 } \
5223 else \
5224 { \
b0986e7a 5225 s.op->newline() << "const char * const " << #var << ";"; \
606fd9c8
FCE
5226 if (s.verbose > 2) clog << "stap_dwarf_probe *" << #var << endl; \
5227 }
5228
5229 CALCIT(module);
5230 CALCIT(section);
e6fe60e7 5231#undef CALCIT
606fd9c8 5232
b0986e7a 5233 s.op->newline() << "const unsigned long address;";
7c3e97f4
JS
5234 s.op->newline() << "const struct stap_probe * const probe;";
5235 s.op->newline() << "const struct stap_probe * const entry_probe;";
b20febf3
FCE
5236 s.op->newline(-1) << "} stap_dwarf_probes[] = {";
5237 s.op->indent(1);
5238
5239 for (p_b_m_iterator it = probes_by_module.begin(); it != probes_by_module.end(); it++)
2930abc7 5240 {
b20febf3
FCE
5241 dwarf_derived_probe* p = it->second;
5242 s.op->newline() << "{";
5243 if (p->has_return)
5244 s.op->line() << " .return_p=1,";
c9bad430 5245 if (p->has_maxactive)
606fd9c8
FCE
5246 {
5247 s.op->line() << " .maxactive_p=1,";
5248 assert (p->maxactive_val >= 0 && p->maxactive_val <= USHRT_MAX);
5249 s.op->line() << " .maxactive_val=" << p->maxactive_val << ",";
5250 }
af234c40
JS
5251 if (p->saved_longs || p->saved_strings)
5252 {
5253 if (p->saved_longs)
5254 s.op->line() << " .saved_longs=" << p->saved_longs << ",";
5255 if (p->saved_strings)
5256 s.op->line() << " .saved_strings=" << p->saved_strings << ",";
5257 if (p->entry_handler)
c87ae2c1 5258 s.op->line() << " .entry_probe=" << common_probe_init (p->entry_handler) << ",";
af234c40 5259 }
b350f56b
JS
5260 if (p->locations[0]->optional)
5261 s.op->line() << " .optional_p=1,";
dc38c256 5262 s.op->line() << " .address=(unsigned long)0x" << hex << p->addr << dec << "ULL,";
84048984
FCE
5263 s.op->line() << " .module=\"" << p->module << "\",";
5264 s.op->line() << " .section=\"" << p->section << "\",";
faea5e16 5265 s.op->line() << " .probe=" << common_probe_init (p) << ",";
b20febf3 5266 s.op->line() << " },";
2930abc7 5267 }
2930abc7 5268
b20febf3
FCE
5269 s.op->newline(-1) << "};";
5270
5271 // Emit the kprobes callback function
5272 s.op->newline();
5273 s.op->newline() << "static int enter_kprobe_probe (struct kprobe *inst,";
5274 s.op->line() << " struct pt_regs *regs) {";
606fd9c8
FCE
5275 // NB: as of PR5673, the kprobe|kretprobe union struct is in BSS
5276 s.op->newline(1) << "int kprobe_idx = ((uintptr_t)inst-(uintptr_t)stap_dwarf_kprobes)/sizeof(struct stap_dwarf_kprobe);";
5277 // Check that the index is plausible
5278 s.op->newline() << "struct stap_dwarf_probe *sdp = &stap_dwarf_probes[";
5279 s.op->line() << "((kprobe_idx >= 0 && kprobe_idx < " << probes_by_module.size() << ")?";
5280 s.op->line() << "kprobe_idx:0)"; // NB: at least we avoid memory corruption
5281 // XXX: it would be nice to give a more verbose error though; BUG_ON later?
5282 s.op->line() << "];";
71db462b 5283 common_probe_entryfn_prologue (s, "STAP_SESSION_RUNNING", "sdp->probe",
cda141c2 5284 "stp_probe_type_kprobe");
d9aed31e 5285 s.op->newline() << "c->kregs = regs;";
6415ddde
MW
5286
5287 // Make it look like the IP is set as it wouldn't have been replaced
5288 // by a breakpoint instruction when calling real probe handler. Reset
5289 // IP regs on return, so we don't confuse kprobes. PR10458
5290 s.op->newline() << "{";
5291 s.op->indent(1);
d9aed31e 5292 s.op->newline() << "unsigned long kprobes_ip = REG_IP(c->kregs);";
259d54c0 5293 s.op->newline() << "SET_REG_IP(regs, (unsigned long) inst->addr);";
26e63673 5294 s.op->newline() << "(*sdp->probe->ph) (c);";
259d54c0 5295 s.op->newline() << "SET_REG_IP(regs, kprobes_ip);";
6415ddde
MW
5296 s.op->newline(-1) << "}";
5297
f887a8c9 5298 common_probe_entryfn_epilogue (s, true);
b20febf3
FCE
5299 s.op->newline() << "return 0;";
5300 s.op->newline(-1) << "}";
5301
5302 // Same for kretprobes
5303 s.op->newline();
af234c40
JS
5304 s.op->newline() << "static int enter_kretprobe_common (struct kretprobe_instance *inst,";
5305 s.op->line() << " struct pt_regs *regs, int entry) {";
b20febf3 5306 s.op->newline(1) << "struct kretprobe *krp = inst->rp;";
606fd9c8
FCE
5307
5308 // NB: as of PR5673, the kprobe|kretprobe union struct is in BSS
a36378d7 5309 s.op->newline() << "int kprobe_idx = ((uintptr_t)krp-(uintptr_t)stap_dwarf_kprobes)/sizeof(struct stap_dwarf_kprobe);";
606fd9c8
FCE
5310 // Check that the index is plausible
5311 s.op->newline() << "struct stap_dwarf_probe *sdp = &stap_dwarf_probes[";
5312 s.op->line() << "((kprobe_idx >= 0 && kprobe_idx < " << probes_by_module.size() << ")?";
5313 s.op->line() << "kprobe_idx:0)"; // NB: at least we avoid memory corruption
5314 // XXX: it would be nice to give a more verbose error though; BUG_ON later?
5315 s.op->line() << "];";
5316
7c3e97f4 5317 s.op->newline() << "const struct stap_probe *sp = entry ? sdp->entry_probe : sdp->probe;";
c87ae2c1
JS
5318 s.op->newline() << "if (sp) {";
5319 s.op->indent(1);
71db462b 5320 common_probe_entryfn_prologue (s, "STAP_SESSION_RUNNING", "sp",
cda141c2 5321 "stp_probe_type_kretprobe");
d9aed31e 5322 s.op->newline() << "c->kregs = regs;";
af234c40
JS
5323
5324 // for assisting runtime's backtrace logic and accessing kretprobe data packets
6dceb5c9
MW
5325 s.op->newline() << "c->ips.krp.pi = inst;";
5326 s.op->newline() << "c->ips.krp.pi_longs = sdp->saved_longs;";
6415ddde
MW
5327
5328 // Make it look like the IP is set as it wouldn't have been replaced
5329 // by a breakpoint instruction when calling real probe handler. Reset
5330 // IP regs on return, so we don't confuse kprobes. PR10458
5331 s.op->newline() << "{";
d9aed31e 5332 s.op->newline(1) << "unsigned long kprobes_ip = REG_IP(c->kregs);";
c87ae2c1
JS
5333 s.op->newline() << "if (entry)";
5334 s.op->newline(1) << "SET_REG_IP(regs, (unsigned long) inst->rp->kp.addr);";
5335 s.op->newline(-1) << "else";
5336 s.op->newline(1) << "SET_REG_IP(regs, (unsigned long)inst->ret_addr);";
5337 s.op->newline(-1) << "(sp->ph) (c);";
259d54c0 5338 s.op->newline() << "SET_REG_IP(regs, kprobes_ip);";
6415ddde
MW
5339 s.op->newline(-1) << "}";
5340
f887a8c9 5341 common_probe_entryfn_epilogue (s, true);
c87ae2c1 5342 s.op->newline(-1) << "}";
b20febf3
FCE
5343 s.op->newline() << "return 0;";
5344 s.op->newline(-1) << "}";
af234c40
JS
5345
5346 s.op->newline();
5347 s.op->newline() << "static int enter_kretprobe_probe (struct kretprobe_instance *inst,";
5348 s.op->line() << " struct pt_regs *regs) {";
5349 s.op->newline(1) << "return enter_kretprobe_common(inst, regs, 0);";
5350 s.op->newline(-1) << "}";
5351
5352 s.op->newline();
5353 s.op->newline() << "static int enter_kretprobe_entry_probe (struct kretprobe_instance *inst,";
5354 s.op->line() << " struct pt_regs *regs) {";
5355 s.op->newline(1) << "return enter_kretprobe_common(inst, regs, 1);";
5356 s.op->newline(-1) << "}";
b642c901 5357
14cf7e42 5358 s.op->newline();
20c6c071 5359}
ec4373ff 5360
20c6c071 5361
dc38c0ae 5362void
b20febf3
FCE
5363dwarf_derived_probe_group::emit_module_init (systemtap_session& s)
5364{
5365 s.op->newline() << "for (i=0; i<" << probes_by_module.size() << "; i++) {";
5366 s.op->newline(1) << "struct stap_dwarf_probe *sdp = & stap_dwarf_probes[i];";
a36378d7 5367 s.op->newline() << "struct stap_dwarf_kprobe *kp = & stap_dwarf_kprobes[i];";
a049e342 5368 s.op->newline() << "unsigned long relocated_addr = _stp_kmodule_relocate (sdp->module, sdp->section, sdp->address);";
b20febf3 5369 s.op->newline() << "if (relocated_addr == 0) continue;"; // quietly; assume module is absent
26e63673 5370 s.op->newline() << "probe_point = sdp->probe->pp;"; // for error messages
b20febf3 5371 s.op->newline() << "if (sdp->return_p) {";
606fd9c8 5372 s.op->newline(1) << "kp->u.krp.kp.addr = (void *) relocated_addr;";
c9bad430 5373 s.op->newline() << "if (sdp->maxactive_p) {";
606fd9c8 5374 s.op->newline(1) << "kp->u.krp.maxactive = sdp->maxactive_val;";
c9bad430 5375 s.op->newline(-1) << "} else {";
f07c3b68 5376 s.op->newline(1) << "kp->u.krp.maxactive = KRETACTIVE;";
c9bad430 5377 s.op->newline(-1) << "}";
606fd9c8 5378 s.op->newline() << "kp->u.krp.handler = &enter_kretprobe_probe;";
af234c40 5379 s.op->newline() << "#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,25)";
c87ae2c1 5380 s.op->newline() << "if (sdp->entry_probe) {";
af234c40
JS
5381 s.op->newline(1) << "kp->u.krp.entry_handler = &enter_kretprobe_entry_probe;";
5382 s.op->newline() << "kp->u.krp.data_size = sdp->saved_longs * sizeof(int64_t) + ";
5383 s.op->newline() << " sdp->saved_strings * MAXSTRINGLEN;";
5384 s.op->newline(-1) << "}";
5385 s.op->newline() << "#endif";
e4cb375f
MH
5386 // to ensure safeness of bspcache, always use aggr_kprobe on ia64
5387 s.op->newline() << "#ifdef __ia64__";
5388 s.op->newline() << "kp->dummy.addr = kp->u.krp.kp.addr;";
5389 s.op->newline() << "kp->dummy.pre_handler = NULL;";
5390 s.op->newline() << "rc = register_kprobe (& kp->dummy);";
5391 s.op->newline() << "if (rc == 0) {";
5392 s.op->newline(1) << "rc = register_kretprobe (& kp->u.krp);";
5393 s.op->newline() << "if (rc != 0)";
5394 s.op->newline(1) << "unregister_kprobe (& kp->dummy);";
5395 s.op->newline(-2) << "}";
5396 s.op->newline() << "#else";
606fd9c8 5397 s.op->newline() << "rc = register_kretprobe (& kp->u.krp);";
e4cb375f 5398 s.op->newline() << "#endif";
b20febf3 5399 s.op->newline(-1) << "} else {";
e4cb375f 5400 // to ensure safeness of bspcache, always use aggr_kprobe on ia64
606fd9c8
FCE
5401 s.op->newline(1) << "kp->u.kp.addr = (void *) relocated_addr;";
5402 s.op->newline() << "kp->u.kp.pre_handler = &enter_kprobe_probe;";
e4cb375f
MH
5403 s.op->newline() << "#ifdef __ia64__";
5404 s.op->newline() << "kp->dummy.addr = kp->u.kp.addr;";
5405 s.op->newline() << "kp->dummy.pre_handler = NULL;";
5406 s.op->newline() << "rc = register_kprobe (& kp->dummy);";
5407 s.op->newline() << "if (rc == 0) {";
5408 s.op->newline(1) << "rc = register_kprobe (& kp->u.kp);";
5409 s.op->newline() << "if (rc != 0)";
5410 s.op->newline(1) << "unregister_kprobe (& kp->dummy);";
5411 s.op->newline(-2) << "}";
5412 s.op->newline() << "#else";
606fd9c8 5413 s.op->newline() << "rc = register_kprobe (& kp->u.kp);";
e4cb375f 5414 s.op->newline() << "#endif";
b20febf3 5415 s.op->newline(-1) << "}";
9063462a
FCE
5416 s.op->newline() << "if (rc) {"; // PR6749: tolerate a failed register_*probe.
5417 s.op->newline(1) << "sdp->registered_p = 0;";
b350f56b 5418 s.op->newline() << "if (!sdp->optional_p)";
50b6acf7 5419 s.op->newline(1) << "_stp_warn (\"probe %s (address 0x%lx) registration error (rc %d)\", probe_point, (unsigned long) relocated_addr, rc);";
b350f56b 5420 s.op->newline(-1) << "rc = 0;"; // continue with other probes
9063462a
FCE
5421 // XXX: shall we increment numskipped?
5422 s.op->newline(-1) << "}";
5423
5424#if 0 /* pre PR 6749; XXX consider making an option */
c48cb0cc 5425 s.op->newline(1) << "for (j=i-1; j>=0; j--) {"; // partial rollback
b20febf3 5426 s.op->newline(1) << "struct stap_dwarf_probe *sdp2 = & stap_dwarf_probes[j];";
606fd9c8
FCE
5427 s.op->newline() << "struct stap_dwarf_kprobe *kp2 = & stap_dwarf_kprobes[j];";
5428 s.op->newline() << "if (sdp2->return_p) unregister_kretprobe (&kp2->u.krp);";
5429 s.op->newline() << "else unregister_kprobe (&kp2->u.kp);";
e4cb375f
MH
5430 s.op->newline() << "#ifdef __ia64__";
5431 s.op->newline() << "unregister_kprobe (&kp2->dummy);";
5432 s.op->newline() << "#endif";
c48cb0cc
FCE
5433 // NB: we don't have to clear sdp2->registered_p, since the module_exit code is
5434 // not run for this early-abort case.
5435 s.op->newline(-1) << "}";
5436 s.op->newline() << "break;"; // don't attempt to register any more probes
b20febf3 5437 s.op->newline(-1) << "}";
9063462a
FCE
5438#endif
5439
b20febf3
FCE
5440 s.op->newline() << "else sdp->registered_p = 1;";
5441 s.op->newline(-1) << "}"; // for loop
dc38c0ae
DS
5442}
5443
5444
b4be7cbc
FCE
5445void
5446dwarf_derived_probe_group::emit_module_refresh (systemtap_session& s)
5447{
5448 s.op->newline() << "for (i=0; i<" << probes_by_module.size() << "; i++) {";
5449 s.op->newline(1) << "struct stap_dwarf_probe *sdp = & stap_dwarf_probes[i];";
5450 s.op->newline() << "struct stap_dwarf_kprobe *kp = & stap_dwarf_kprobes[i];";
5451 s.op->newline() << "unsigned long relocated_addr = _stp_kmodule_relocate (sdp->module, sdp->section, sdp->address);";
5452 s.op->newline() << "int rc;";
5453
5454 // new module arrived?
5455 s.op->newline() << "if (sdp->registered_p == 0 && relocated_addr != 0) {";
5456 s.op->newline(1) << "if (sdp->return_p) {";
5457 s.op->newline(1) << "kp->u.krp.kp.addr = (void *) relocated_addr;";
5458 s.op->newline() << "if (sdp->maxactive_p) {";
5459 s.op->newline(1) << "kp->u.krp.maxactive = sdp->maxactive_val;";
5460 s.op->newline(-1) << "} else {";
5461 s.op->newline(1) << "kp->u.krp.maxactive = KRETACTIVE;";
5462 s.op->newline(-1) << "}";
5463 s.op->newline() << "kp->u.krp.handler = &enter_kretprobe_probe;";
5464 s.op->newline() << "#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,25)";
5465 s.op->newline() << "if (sdp->entry_probe) {";
5466 s.op->newline(1) << "kp->u.krp.entry_handler = &enter_kretprobe_entry_probe;";
5467 s.op->newline() << "kp->u.krp.data_size = sdp->saved_longs * sizeof(int64_t) + ";
5468 s.op->newline() << " sdp->saved_strings * MAXSTRINGLEN;";
5469 s.op->newline(-1) << "}";
5470 s.op->newline() << "#endif";
5471 // to ensure safeness of bspcache, always use aggr_kprobe on ia64
5472 s.op->newline() << "#ifdef __ia64__";
5473 s.op->newline() << "kp->dummy.addr = kp->u.krp.kp.addr;";
5474 s.op->newline() << "kp->dummy.pre_handler = NULL;";
5475 s.op->newline() << "rc = register_kprobe (& kp->dummy);";
5476 s.op->newline() << "if (rc == 0) {";
5477 s.op->newline(1) << "rc = register_kretprobe (& kp->u.krp);";
5478 s.op->newline() << "if (rc != 0)";
5479 s.op->newline(1) << "unregister_kprobe (& kp->dummy);";
5480 s.op->newline(-2) << "}";
5481 s.op->newline() << "#else";
5482 s.op->newline() << "rc = register_kretprobe (& kp->u.krp);";
5483 s.op->newline() << "#endif";
5484 s.op->newline(-1) << "} else {";
5485 // to ensure safeness of bspcache, always use aggr_kprobe on ia64
5486 s.op->newline(1) << "kp->u.kp.addr = (void *) relocated_addr;";
5487 s.op->newline() << "kp->u.kp.pre_handler = &enter_kprobe_probe;";
5488 s.op->newline() << "#ifdef __ia64__";
5489 s.op->newline() << "kp->dummy.addr = kp->u.kp.addr;";
5490 s.op->newline() << "kp->dummy.pre_handler = NULL;";
5491 s.op->newline() << "rc = register_kprobe (& kp->dummy);";
5492 s.op->newline() << "if (rc == 0) {";
5493 s.op->newline(1) << "rc = register_kprobe (& kp->u.kp);";
5494 s.op->newline() << "if (rc != 0)";
5495 s.op->newline(1) << "unregister_kprobe (& kp->dummy);";
5496 s.op->newline(-2) << "}";
5497 s.op->newline() << "#else";
5498 s.op->newline() << "rc = register_kprobe (& kp->u.kp);";
5499 s.op->newline() << "#endif";
5500 s.op->newline(-1) << "}";
5501 s.op->newline() << "if (rc == 0) sdp->registered_p = 1;";
5502
5503 // old module disappeared?
5504 s.op->newline(-1) << "} else if (sdp->registered_p == 1 && relocated_addr == 0) {";
5505 s.op->newline(1) << "if (sdp->return_p) {";
5506 s.op->newline(1) << "unregister_kretprobe (&kp->u.krp);";
065d5567 5507 s.op->newline() << "atomic_add (kp->u.krp.nmissed, skipped_count());";
b4be7cbc
FCE
5508 s.op->newline() << "#ifdef STP_TIMING";
5509 s.op->newline() << "if (kp->u.krp.nmissed)";
5510 s.op->newline(1) << "_stp_warn (\"Skipped due to missed kretprobe/1 on '%s': %d\\n\", sdp->probe->pp, kp->u.krp.nmissed);";
5511 s.op->newline(-1) << "#endif";
065d5567 5512 s.op->newline() << "atomic_add (kp->u.krp.kp.nmissed, skipped_count());";
b4be7cbc
FCE
5513 s.op->newline() << "#ifdef STP_TIMING";
5514 s.op->newline() << "if (kp->u.krp.kp.nmissed)";
5515 s.op->newline(1) << "_stp_warn (\"Skipped due to missed kretprobe/2 on '%s': %lu\\n\", sdp->probe->pp, kp->u.krp.kp.nmissed);";
5516 s.op->newline(-1) << "#endif";
5517 s.op->newline(-1) << "} else {";
5518 s.op->newline(1) << "unregister_kprobe (&kp->u.kp);";
065d5567 5519 s.op->newline() << "atomic_add (kp->u.kp.nmissed, skipped_count());";
b4be7cbc
FCE
5520 s.op->newline() << "#ifdef STP_TIMING";
5521 s.op->newline() << "if (kp->u.kp.nmissed)";
5522 s.op->newline(1) << "_stp_warn (\"Skipped due to missed kprobe on '%s': %lu\\n\", sdp->probe->pp, kp->u.kp.nmissed);";
5523 s.op->newline(-1) << "#endif";
5524 s.op->newline(-1) << "}";
5525 s.op->newline() << "#if defined(__ia64__)";
5526 s.op->newline() << "unregister_kprobe (&kp->dummy);";
5527 s.op->newline() << "#endif";
5528 s.op->newline() << "sdp->registered_p = 0;";
5529 s.op->newline(-1) << "}";
5530
5531 s.op->newline(-1) << "}"; // for loop
5532}
5533
5534
5535
5536
46b84a80 5537void
b20febf3 5538dwarf_derived_probe_group::emit_module_exit (systemtap_session& s)
46b84a80 5539{
42cb22bd
MH
5540 //Unregister kprobes by batch interfaces.
5541 s.op->newline() << "#if defined(STAPCONF_UNREGISTER_KPROBES)";
5542 s.op->newline() << "j = 0;";
5543 s.op->newline() << "for (i=0; i<" << probes_by_module.size() << "; i++) {";
5544 s.op->newline(1) << "struct stap_dwarf_probe *sdp = & stap_dwarf_probes[i];";
5545 s.op->newline() << "struct stap_dwarf_kprobe *kp = & stap_dwarf_kprobes[i];";
5546 s.op->newline() << "if (! sdp->registered_p) continue;";
5547 s.op->newline() << "if (!sdp->return_p)";
5548 s.op->newline(1) << "stap_unreg_kprobes[j++] = &kp->u.kp;";
5549 s.op->newline(-2) << "}";
5550 s.op->newline() << "unregister_kprobes((struct kprobe **)stap_unreg_kprobes, j);";
5551 s.op->newline() << "j = 0;";
5552 s.op->newline() << "for (i=0; i<" << probes_by_module.size() << "; i++) {";
5553 s.op->newline(1) << "struct stap_dwarf_probe *sdp = & stap_dwarf_probes[i];";
5554 s.op->newline() << "struct stap_dwarf_kprobe *kp = & stap_dwarf_kprobes[i];";
5555 s.op->newline() << "if (! sdp->registered_p) continue;";
5556 s.op->newline() << "if (sdp->return_p)";
5557 s.op->newline(1) << "stap_unreg_kprobes[j++] = &kp->u.krp;";
5558 s.op->newline(-2) << "}";
5559 s.op->newline() << "unregister_kretprobes((struct kretprobe **)stap_unreg_kprobes, j);";
e4cb375f
MH
5560 s.op->newline() << "#ifdef __ia64__";
5561 s.op->newline() << "j = 0;";
5562 s.op->newline() << "for (i=0; i<" << probes_by_module.size() << "; i++) {";
5563 s.op->newline(1) << "struct stap_dwarf_probe *sdp = & stap_dwarf_probes[i];";
5564 s.op->newline() << "struct stap_dwarf_kprobe *kp = & stap_dwarf_kprobes[i];";
5565 s.op->newline() << "if (! sdp->registered_p) continue;";
5566 s.op->newline() << "stap_unreg_kprobes[j++] = &kp->dummy;";
5567 s.op->newline(-1) << "}";
5568 s.op->newline() << "unregister_kprobes((struct kprobe **)stap_unreg_kprobes, j);";
5569 s.op->newline() << "#endif";
42cb22bd
MH
5570 s.op->newline() << "#endif";
5571
b20febf3
FCE
5572 s.op->newline() << "for (i=0; i<" << probes_by_module.size() << "; i++) {";
5573 s.op->newline(1) << "struct stap_dwarf_probe *sdp = & stap_dwarf_probes[i];";
a36378d7 5574 s.op->newline() << "struct stap_dwarf_kprobe *kp = & stap_dwarf_kprobes[i];";
b20febf3
FCE
5575 s.op->newline() << "if (! sdp->registered_p) continue;";
5576 s.op->newline() << "if (sdp->return_p) {";
42cb22bd 5577 s.op->newline() << "#if !defined(STAPCONF_UNREGISTER_KPROBES)";
606fd9c8 5578 s.op->newline(1) << "unregister_kretprobe (&kp->u.krp);";
42cb22bd 5579 s.op->newline() << "#endif";
065d5567 5580 s.op->newline() << "atomic_add (kp->u.krp.nmissed, skipped_count());";
73209876
FCE
5581 s.op->newline() << "#ifdef STP_TIMING";
5582 s.op->newline() << "if (kp->u.krp.nmissed)";
26e63673 5583 s.op->newline(1) << "_stp_warn (\"Skipped due to missed kretprobe/1 on '%s': %d\\n\", sdp->probe->pp, kp->u.krp.nmissed);";
73209876 5584 s.op->newline(-1) << "#endif";
065d5567 5585 s.op->newline() << "atomic_add (kp->u.krp.kp.nmissed, skipped_count());";
73209876
FCE
5586 s.op->newline() << "#ifdef STP_TIMING";
5587 s.op->newline() << "if (kp->u.krp.kp.nmissed)";
26e63673 5588 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 5589 s.op->newline(-1) << "#endif";
557fb7a8 5590 s.op->newline(-1) << "} else {";
42cb22bd 5591 s.op->newline() << "#if !defined(STAPCONF_UNREGISTER_KPROBES)";
606fd9c8 5592 s.op->newline(1) << "unregister_kprobe (&kp->u.kp);";
42cb22bd 5593 s.op->newline() << "#endif";
065d5567 5594 s.op->newline() << "atomic_add (kp->u.kp.nmissed, skipped_count());";
73209876
FCE
5595 s.op->newline() << "#ifdef STP_TIMING";
5596 s.op->newline() << "if (kp->u.kp.nmissed)";
26e63673 5597 s.op->newline(1) << "_stp_warn (\"Skipped due to missed kprobe on '%s': %lu\\n\", sdp->probe->pp, kp->u.kp.nmissed);";
73209876 5598 s.op->newline(-1) << "#endif";
b20febf3 5599 s.op->newline(-1) << "}";
e4cb375f
MH
5600 s.op->newline() << "#if !defined(STAPCONF_UNREGISTER_KPROBES) && defined(__ia64__)";
5601 s.op->newline() << "unregister_kprobe (&kp->dummy);";
5602 s.op->newline() << "#endif";
b20febf3
FCE
5603 s.op->newline() << "sdp->registered_p = 0;";
5604 s.op->newline(-1) << "}";
46b84a80
DS
5605}
5606
272c9036
WF
5607static void sdt_v3_tokenize(const string& str, vector<string>& tokens)
5608{
5609 string::size_type pos;
5610 string::size_type lastPos = str.find_first_not_of(" ", 0);
5611 string::size_type nextAt = str.find("@", lastPos);
e9a90eee
JS
5612
5613 if (nextAt == string::npos)
5614 {
5615 // PR13934: Assembly probes are not forced to use the N@OP form.
5616 // In this case, N is inferred to be the native word size. Since we
5617 // don't have a nice delimiter, just split it on spaces. SDT-asm authors
5618 // then must not put any spaces in arguments, to avoid ambiguity.
5619 tokenize(str, tokens, " ");
5620 return;
5621 }
5622
272c9036
WF
5623 while (lastPos != string::npos)
5624 {
5625 pos = nextAt + 1;
5626 nextAt = str.find("@", pos);
5627 if (nextAt == string::npos)
5628 pos = string::npos;
5629 else
5630 pos = str.rfind(" ", nextAt);
5631
5632 tokens.push_back(str.substr(lastPos, pos - lastPos));
5633 lastPos = str.find_first_not_of(" ", pos);
5634 }
5635}
8aabf152 5636
8aabf152 5637
aff5d390 5638struct sdt_uprobe_var_expanding_visitor: public var_expanding_visitor
7a05f484 5639{
ae1418f0 5640 enum regwidths {QI, QIh, HI, SI, DI};
f83336a5
FCE
5641 sdt_uprobe_var_expanding_visitor(systemtap_session& s,
5642 int elf_machine,
5643 const string & process_name,
a794dbeb 5644 const string & provider_name,
aff5d390 5645 const string & probe_name,
71e5e13d 5646 stap_sdt_probe_type probe_type,
aff5d390 5647 const string & arg_string,
8aabf152 5648 int ac):
332ba7e7 5649 session (s), elf_machine (elf_machine), process_name (process_name),
71e5e13d
SC
5650 provider_name (provider_name), probe_name (probe_name),
5651 probe_type (probe_type), arg_count ((unsigned) ac)
a8ec7719 5652 {
f83336a5
FCE
5653 /* Register name mapping table depends on the elf machine of this particular
5654 probe target process/file, not upon the host. So we can't just
5655 #ifdef _i686_ etc. */
ae1418f0
FCE
5656
5657#define DRI(name,num,width) dwarf_regs[name]=make_pair(num,width)
f83336a5 5658 if (elf_machine == EM_X86_64) {
b78a0fbb
LB
5659 DRI ("%rax", 0, DI); DRI ("%eax", 0, SI); DRI ("%ax", 0, HI);
5660 DRI ("%al", 0, QI); DRI ("%ah", 0, QIh);
46a94997
SC
5661 DRI ("%rdx", 1, DI); DRI ("%edx", 1, SI); DRI ("%dx", 1, HI);
5662 DRI ("%dl", 1, QI); DRI ("%dh", 1, QIh);
5663 DRI ("%rcx", 2, DI); DRI ("%ecx", 2, SI); DRI ("%cx", 2, HI);
5664 DRI ("%cl", 2, QI); DRI ("%ch", 2, QIh);
5665 DRI ("%rbx", 3, DI); DRI ("%ebx", 3, SI); DRI ("%bx", 3, HI);
b78a0fbb
LB
5666 DRI ("%bl", 3, QI); DRI ("%bh", 3, QIh);
5667 DRI ("%rsi", 4, DI); DRI ("%esi", 4, SI); DRI ("%si", 4, HI);
46a94997
SC
5668 DRI ("%sil", 4, QI);
5669 DRI ("%rdi", 5, DI); DRI ("%edi", 5, SI); DRI ("%di", 5, HI);
5670 DRI ("%dil", 5, QI);
5671 DRI ("%rbp", 6, DI); DRI ("%ebp", 6, SI); DRI ("%bp", 6, HI);
2c7281e7 5672 DRI ("%bpl", 6, QI);
46a94997 5673 DRI ("%rsp", 7, DI); DRI ("%esp", 7, SI); DRI ("%sp", 7, HI);
2c7281e7 5674 DRI ("%spl", 7, QI);
46a94997
SC
5675 DRI ("%r8", 8, DI); DRI ("%r8d", 8, SI); DRI ("%r8w", 8, HI);
5676 DRI ("%r8b", 8, QI);
5677 DRI ("%r9", 9, DI); DRI ("%r9d", 9, SI); DRI ("%r9w", 9, HI);
5678 DRI ("%r9b", 9, QI);
5679 DRI ("%r10", 10, DI); DRI ("%r10d", 10, SI); DRI ("%r10w", 10, HI);
5680 DRI ("%r10b", 10, QI);
5681 DRI ("%r11", 11, DI); DRI ("%r11d", 11, SI); DRI ("%r11w", 11, HI);
5682 DRI ("%r11b", 11, QI);
5683 DRI ("%r12", 12, DI); DRI ("%r12d", 12, SI); DRI ("%r12w", 12, HI);
5684 DRI ("%r12b", 12, QI);
5685 DRI ("%r13", 13, DI); DRI ("%r13d", 13, SI); DRI ("%r13w", 13, HI);
5686 DRI ("%r13b", 13, QI);
5687 DRI ("%r14", 14, DI); DRI ("%r14d", 14, SI); DRI ("%r14w", 14, HI);
5688 DRI ("%r14b", 14, QI);
5689 DRI ("%r15", 15, DI); DRI ("%r15d", 15, SI); DRI ("%r15w", 15, HI);
5690 DRI ("%r15b", 15, QI);
f83336a5 5691 } else if (elf_machine == EM_386) {
46a94997
SC
5692 DRI ("%eax", 0, SI); DRI ("%ax", 0, HI); DRI ("%al", 0, QI);
5693 DRI ("%ah", 0, QIh);
5694 DRI ("%ecx", 1, SI); DRI ("%cx", 1, HI); DRI ("%cl", 1, QI);
5695 DRI ("%ch", 1, QIh);
5696 DRI ("%edx", 2, SI); DRI ("%dx", 2, HI); DRI ("%dl", 2, QI);
5697 DRI ("%dh", 2, QIh);
5698 DRI ("%ebx", 3, SI); DRI ("%bx", 3, HI); DRI ("%bl", 3, QI);
5699 DRI ("%bh", 3, QIh);
b78a0fbb 5700 DRI ("%esp", 4, SI); DRI ("%sp", 4, HI);
46a94997
SC
5701 DRI ("%ebp", 5, SI); DRI ("%bp", 5, HI);
5702 DRI ("%esi", 6, SI); DRI ("%si", 6, HI); DRI ("%sil", 6, QI);
5703 DRI ("%edi", 7, SI); DRI ("%di", 7, HI); DRI ("%dil", 7, QI);
0491c523 5704 } else if (elf_machine == EM_PPC || elf_machine == EM_PPC64) {
46a94997
SC
5705 DRI ("%r0", 0, DI);
5706 DRI ("%r1", 1, DI);
5707 DRI ("%r2", 2, DI);
5708 DRI ("%r3", 3, DI);
5709 DRI ("%r4", 4, DI);
5710 DRI ("%r5", 5, DI);
5711 DRI ("%r6", 6, DI);
5712 DRI ("%r7", 7, DI);
5713 DRI ("%r8", 8, DI);
5714 DRI ("%r9", 9, DI);
5715 DRI ("%r10", 10, DI);
5716 DRI ("%r11", 11, DI);
5717 DRI ("%r12", 12, DI);
5718 DRI ("%r13", 13, DI);
5719 DRI ("%r14", 14, DI);
5720 DRI ("%r15", 15, DI);
5721 DRI ("%r16", 16, DI);
5722 DRI ("%r17", 17, DI);
5723 DRI ("%r18", 18, DI);
5724 DRI ("%r19", 19, DI);
5725 DRI ("%r20", 20, DI);
5726 DRI ("%r21", 21, DI);
5727 DRI ("%r22", 22, DI);
5728 DRI ("%r23", 23, DI);
5729 DRI ("%r24", 24, DI);
5730 DRI ("%r25", 25, DI);
5731 DRI ("%r26", 26, DI);
5732 DRI ("%r27", 27, DI);
5733 DRI ("%r28", 28, DI);
5734 DRI ("%r29", 29, DI);
5735 DRI ("%r30", 30, DI);
5736 DRI ("%r31", 31, DI);
8aabf152 5737 // PR11821: unadorned register "names" without -mregnames
46a94997
SC
5738 DRI ("0", 0, DI);
5739 DRI ("1", 1, DI);
5740 DRI ("2", 2, DI);
5741 DRI ("3", 3, DI);
5742 DRI ("4", 4, DI);
5743 DRI ("5", 5, DI);
5744 DRI ("6", 6, DI);
5745 DRI ("7", 7, DI);
5746 DRI ("8", 8, DI);
5747 DRI ("9", 9, DI);
5748 DRI ("10", 10, DI);
5749 DRI ("11", 11, DI);
5750 DRI ("12", 12, DI);
5751 DRI ("13", 13, DI);
5752 DRI ("14", 14, DI);
5753 DRI ("15", 15, DI);
5754 DRI ("16", 16, DI);
5755 DRI ("17", 17, DI);
5756 DRI ("18", 18, DI);
5757 DRI ("19", 19, DI);
5758 DRI ("20", 20, DI);
5759 DRI ("21", 21, DI);
5760 DRI ("22", 22, DI);
5761 DRI ("23", 23, DI);
5762 DRI ("24", 24, DI);
5763 DRI ("25", 25, DI);
5764 DRI ("26", 26, DI);
5765 DRI ("27", 27, DI);
5766 DRI ("28", 28, DI);
5767 DRI ("29", 29, DI);
5768 DRI ("30", 30, DI);
5769 DRI ("31", 31, DI);
14900130 5770 } else if (elf_machine == EM_S390) {
46a94997
SC
5771 DRI ("%r0", 0, DI);
5772 DRI ("%r1", 1, DI);
5773 DRI ("%r2", 2, DI);
5774 DRI ("%r3", 3, DI);
5775 DRI ("%r4", 4, DI);
5776 DRI ("%r5", 5, DI);
5777 DRI ("%r6", 6, DI);
5778 DRI ("%r7", 7, DI);
5779 DRI ("%r8", 8, DI);
5780 DRI ("%r9", 9, DI);
5781 DRI ("%r10", 10, DI);
5782 DRI ("%r11", 11, DI);
5783 DRI ("%r12", 12, DI);
5784 DRI ("%r13", 13, DI);
5785 DRI ("%r14", 14, DI);
5786 DRI ("%r15", 15, DI);
272c9036
WF
5787 } else if (elf_machine == EM_ARM) {
5788 DRI ("r0", 0, SI);
5789 DRI ("r1", 1, SI);
5790 DRI ("r2", 2, SI);
5791 DRI ("r3", 3, SI);
5792 DRI ("r4", 4, SI);
5793 DRI ("r5", 5, SI);
5794 DRI ("r6", 6, SI);
5795 DRI ("r7", 7, SI);
5796 DRI ("r8", 8, SI);
5797 DRI ("r9", 9, SI);
669a2feb 5798 DRI ("r10", 10, SI); DRI ("sl", 10, SI);
272c9036
WF
5799 DRI ("fp", 11, SI);
5800 DRI ("ip", 12, SI);
5801 DRI ("sp", 13, SI);
5802 DRI ("lr", 14, SI);
5803 DRI ("pc", 15, SI);
86aae990
WC
5804 } else if (elf_machine == EM_AARCH64) {
5805 DRI ("x0", 0, DI); DRI ("w0", 0, SI);
5806 DRI ("x1", 1, DI); DRI ("w1", 1, SI);
5807 DRI ("x2", 2, DI); DRI ("w2", 2, SI);
5808 DRI ("x3", 3, DI); DRI ("w3", 3, SI);
5809 DRI ("x4", 4, DI); DRI ("w4", 4, SI);
5810 DRI ("x5", 5, DI); DRI ("w5", 5, SI);
5811 DRI ("x6", 6, DI); DRI ("w6", 6, SI);
5812 DRI ("x7", 7, DI); DRI ("w7", 7, SI);
5813 DRI ("x8", 8, DI); DRI ("w8", 8, SI);
5814 DRI ("x9", 9, DI); DRI ("w9", 9, SI);
5815 DRI ("x10", 10, DI); DRI ("w10", 10, SI);
5816 DRI ("x11", 11, DI); DRI ("w11", 11, SI);
5817 DRI ("x12", 12, DI); DRI ("w12", 12, SI);
5818 DRI ("x13", 13, DI); DRI ("w13", 13, SI);
5819 DRI ("x14", 14, DI); DRI ("w14", 14, SI);
5820 DRI ("x15", 15, DI); DRI ("w15", 15, SI);
5821 DRI ("x16", 16, DI); DRI ("w16", 16, SI);
5822 DRI ("x17", 17, DI); DRI ("w17", 17, SI);
5823 DRI ("x18", 18, DI); DRI ("w18", 18, SI);
5824 DRI ("x19", 19, DI); DRI ("w19", 19, SI);
5825 DRI ("x20", 20, DI); DRI ("w20", 20, SI);
5826 DRI ("x21", 21, DI); DRI ("w21", 21, SI);
5827 DRI ("x22", 22, DI); DRI ("w22", 22, SI);
5828 DRI ("x23", 23, DI); DRI ("w23", 23, SI);
5829 DRI ("x24", 24, DI); DRI ("w24", 24, SI);
5830 DRI ("x25", 25, DI); DRI ("w25", 25, SI);
5831 DRI ("x26", 26, DI); DRI ("w26", 26, SI);
5832 DRI ("x27", 27, DI); DRI ("w27", 27, SI);
5833 DRI ("x28", 28, DI); DRI ("w28", 28, SI);
5834 DRI ("x29", 29, DI); DRI ("w29", 29, SI);
5835 DRI ("x30", 30, DI); DRI ("w30", 30, SI);
5836 DRI ("sp", 31, DI);
14900130 5837 } else if (arg_count) {
8aabf152 5838 /* permit this case; just fall back to dwarf */
f83336a5 5839 }
ae1418f0 5840#undef DRI
f83336a5 5841
ebbd2b45 5842 need_debug_info = false;
88e39987 5843 if (probe_type == uprobe3_type)
272c9036
WF
5844 {
5845 sdt_v3_tokenize(arg_string, arg_tokens);
5846 assert(arg_count <= 12);
5847 }
88e39987 5848 else
272c9036
WF
5849 {
5850 tokenize(arg_string, arg_tokens, " ");
5851 assert(arg_count <= 10);
5852 }
a8ec7719 5853 }
8aabf152 5854
f83336a5 5855 systemtap_session& session;
332ba7e7 5856 int elf_machine;
aff5d390 5857 const string & process_name;
a794dbeb 5858 const string & provider_name;
aff5d390 5859 const string & probe_name;
71e5e13d 5860 stap_sdt_probe_type probe_type;
8aabf152 5861 unsigned arg_count;
aff5d390 5862 vector<string> arg_tokens;
46a94997 5863 map<string, pair<unsigned,int> > dwarf_regs;
ebbd2b45 5864 bool need_debug_info;
aff5d390
SC
5865
5866 void visit_target_symbol (target_symbol* e);
6ef331c8
SC
5867 void visit_target_symbol_arg (target_symbol* e);
5868 void visit_target_symbol_context (target_symbol* e);
bd1fcbad 5869 void visit_atvar_op (atvar_op* e);
40a0c64e 5870 void visit_cast_op (cast_op* e);
aff5d390
SC
5871};
5872
7a05f484
SC
5873
5874void
6ef331c8 5875sdt_uprobe_var_expanding_visitor::visit_target_symbol_context (target_symbol* e)
7a05f484 5876{
6ef331c8 5877 if (e->addressof)
dc09353a 5878 throw SEMANTIC_ERROR(_("cannot take address of context variable"), e->tok);
6ef331c8
SC
5879
5880 if (e->name == "$$name")
7a05f484 5881 {
6ef331c8
SC
5882 literal_string *myname = new literal_string (probe_name);
5883 myname->tok = e->tok;
5884 provide(myname);
5885 return;
5886 }
7a05f484 5887
6ef331c8
SC
5888 else if (e->name == "$$provider")
5889 {
5890 literal_string *myname = new literal_string (provider_name);
5891 myname->tok = e->tok;
5892 provide(myname);
5893 return;
5894 }
a794dbeb 5895
6ef331c8
SC
5896 else if (e->name == "$$vars" || e->name == "$$parms")
5897 {
5898 e->assert_no_components("sdt", true);
6ef331c8
SC
5899
5900 // Convert $$vars to sprintf of a list of vars which we recursively evaluate
63ea4244 5901
1c922ad7 5902 print_format* pf = print_format::create(e->tok, "sprintf");
6ef331c8
SC
5903
5904 for (unsigned i = 1; i <= arg_count; ++i)
5905 {
5906 if (i > 1)
5907 pf->raw_components += " ";
5908 target_symbol *tsym = new target_symbol;
5909 tsym->tok = e->tok;
5910 tsym->name = "$arg" + lex_cast(i);
5911 pf->raw_components += tsym->name;
5912 tsym->components = e->components;
5913
8c2f50c0 5914 expression *texp = require<expression> (tsym);
6ef331c8
SC
5915 if (!e->components.empty() &&
5916 e->components[0].type == target_symbol::comp_pretty_print)
5917 pf->raw_components += "=%s";
5918 else
5919 pf->raw_components += "=%#x";
5920 pf->args.push_back(texp);
5921 }
5922
5923 pf->components = print_format::string_to_components(pf->raw_components);
5924 provide (pf);
5925 }
5926 else
5927 assert(0); // shouldn't get here
5928}
5929
5930
5931void
5932sdt_uprobe_var_expanding_visitor::visit_target_symbol_arg (target_symbol *e)
5933{
5934 try
5935 {
8aabf152 5936 unsigned argno = 0; // the N in $argN
c69a87e0 5937 try
aff5d390 5938 {
5ecaa5a7 5939 if (startswith(e->name, "$arg"))
8aabf152 5940 argno = lex_cast<unsigned>(e->name.substr(4));
aff5d390 5941 }
c69a87e0 5942 catch (const runtime_error& f) // non-integral $arg suffix: e.g. $argKKKSDF
aff5d390 5943 {
8aabf152 5944 argno = 0;
aff5d390 5945 }
5ecaa5a7 5946
8aabf152
FCE
5947 if (arg_count == 0 || // a sdt.h variant without .probe-stored arg_count
5948 argno < 1 || argno > arg_count) // a $argN with out-of-range N
aff5d390 5949 {
8aabf152
FCE
5950 // NB: Either
5951 // 1) uprobe1_type $argN or $FOO (we don't know the arg_count)
5952 // 2) uprobe2_type $FOO (no probe args)
5953 // both of which get resolved later.
73d53dd2 5954 // Throw it now, and it might be resolved by DWARF later.
8aabf152 5955 need_debug_info = true;
dc09353a 5956 throw SEMANTIC_ERROR(_("target-symbol requires debuginfo"), e->tok);
aff5d390 5957 }
277c21bc 5958
8aabf152
FCE
5959 assert (arg_tokens.size() >= argno);
5960 string asmarg = arg_tokens[argno-1]; // $arg1 => arg_tokens[0]
c57ea854 5961
8aabf152
FCE
5962 // Now we try to parse this thing, which is an assembler operand
5963 // expression. If we can't, we warn, back down to need_debug_info
b874bd52 5964 // and hope for the best. Here is the syntax for a few architectures.
9859b766 5965 // Note that the power iN syntax is only for V3 sdt.h; gcc emits the i.
8095a157
FCE
5966 //
5967 // literal reg reg reg + base+index*size+offset
b874bd52 5968 // indirect offset
8095a157 5969 // x86 $N %rR (%rR) N(%rR) O(%bR,%iR,S)
b874bd52 5970 // power iN R (R) N(R)
b78a0fbb 5971 // ia64 N rR [r16]
b874bd52
SC
5972 // s390 N %rR 0(rR) N(r15)
5973 // arm #N rR [rR] [rR, #N]
5974
8aabf152
FCE
5975 expression* argexpr = 0; // filled in in case of successful parse
5976
5977 string percent_regnames;
5978 string regnames;
5979 vector<string> matches;
71e5e13d 5980 long precision;
8aabf152
FCE
5981 int rc;
5982
40fe32e0
SC
5983 // Parse the leading length
5984
5985 if (asmarg.find('@') != string::npos)
5986 {
5987 precision = lex_cast<int>(asmarg.substr(0, asmarg.find('@')));
5988 asmarg = asmarg.substr(asmarg.find('@')+1);
5989 }
71e5e13d
SC
5990 else
5991 {
5992 // V1/V2 do not have precision field so default to signed long
5993 // V3 asm does not have precision field so default to unsigned long
5994 if (probe_type == uprobe3_type)
5995 precision = sizeof(long); // this is an asm probe
5996 else
5997 precision = -sizeof(long);
5998 }
40fe32e0 5999
8aabf152
FCE
6000 // test for a numeric literal.
6001 // Only accept (signed) decimals throughout. XXX
6002
6003 // PR11821. NB: on powerpc, literals are not prefixed with $,
6004 // so this regex does not match. But that's OK, since without
6005 // -mregnames, we can't tell them apart from register numbers
6006 // anyway. With -mregnames, we could, if gcc somehow
6007 // communicated to us the presence of that option, but alas it
6008 // doesn't. http://gcc.gnu.org/PR44995.
272c9036 6009 rc = regexp_match (asmarg, "^[i\\$#][-]?[0-9][0-9]*$", matches);
8aabf152
FCE
6010 if (! rc)
6011 {
75a371ce
JS
6012 string sn = matches[0].substr(1);
6013 int64_t n;
6014 try
6015 {
6016 // We have to pay attention to the size & sign, as gcc sometimes
6017 // propagates constants that don't quite match, like a negative
6018 // value to fill an unsigned type.
6019 switch (precision)
6020 {
6021 case -1: n = lex_cast< int8_t>(sn); break;
6022 case 1: n = lex_cast< uint8_t>(sn); break;
6023 case -2: n = lex_cast< int16_t>(sn); break;
6024 case 2: n = lex_cast<uint16_t>(sn); break;
6025 case -4: n = lex_cast< int32_t>(sn); break;
6026 case 4: n = lex_cast<uint32_t>(sn); break;
6027 default:
6028 case -8: n = lex_cast< int64_t>(sn); break;
6029 case 8: n = lex_cast<uint64_t>(sn); break;
6030 }
6031 }
6032 catch (std::runtime_error&)
6033 {
6034 goto not_matched;
6035 }
6036 literal_number* ln = new literal_number(n);
8aabf152
FCE
6037 ln->tok = e->tok;
6038 argexpr = ln;
6039 goto matched;
6040 }
6041
14900130
SC
6042 if (dwarf_regs.empty())
6043 goto not_matched;
b78a0fbb 6044
8aabf152
FCE
6045 // Build regex pieces out of the known dwarf_regs. We keep two separate
6046 // lists: ones with the % prefix (and thus unambigiuous even despite PR11821),
6047 // and ones with no prefix (and thus only usable in unambiguous contexts).
46a94997 6048 for (map<string,pair<unsigned,int> >::iterator ri = dwarf_regs.begin(); ri != dwarf_regs.end(); ri++)
8aabf152
FCE
6049 {
6050 string regname = ri->first;
6051 assert (regname != "");
6052 regnames += string("|")+regname;
6053 if (regname[0]=='%')
6054 percent_regnames += string("|")+regname;
6055 }
6056 // clip off leading |
6057 regnames = regnames.substr(1);
272c9036
WF
6058 if (percent_regnames != "")
6059 percent_regnames = percent_regnames.substr(1);
8aabf152
FCE
6060
6061 // test for REGISTER
6062 // NB: Because PR11821, we must use percent_regnames here.
272c9036 6063 if (elf_machine == EM_PPC || elf_machine == EM_PPC64 || elf_machine == EM_ARM)
9109f487
SC
6064 rc = regexp_match (asmarg, string("^(")+regnames+string(")$"), matches);
6065 else
332ba7e7 6066 rc = regexp_match (asmarg, string("^(")+percent_regnames+string(")$"), matches);
8aabf152
FCE
6067 if (! rc)
6068 {
6069 string regname = matches[1];
46a94997
SC
6070 map<string,pair<unsigned,int> >::iterator ri = dwarf_regs.find (regname);
6071 if (ri != dwarf_regs.end()) // known register
8aabf152
FCE
6072 {
6073 embedded_expr *get_arg1 = new embedded_expr;
19c22e1f 6074 string width_adjust;
46a94997 6075 switch (ri->second.second)
19c22e1f 6076 {
892ec39a
SC
6077 case QI: width_adjust = ") & 0xff)"; break;
6078 case QIh: width_adjust = ">>8) & 0xff)"; break;
46a94997 6079 case HI:
71e5e13d 6080 // preserve 16 bit register signness
892ec39a
SC
6081 width_adjust = ") & 0xffff)";
6082 if (precision < 0)
55b377f4 6083 width_adjust += " << 48 >> 48";
ac8a78aa
SC
6084 break;
6085 case SI:
6086 // preserve 32 bit register signness
892ec39a
SC
6087 width_adjust = ") & 0xffffffff)";
6088 if (precision < 0)
55b377f4 6089 width_adjust += " << 32 >> 32";
19c22e1f 6090 break;
892ec39a 6091 default: width_adjust = "))";
19c22e1f 6092 }
55b377f4
SC
6093 string type = "";
6094 if (probe_type == uprobe3_type)
6095 type = (precision < 0
6096 ? "(int" : "(uint") + lex_cast(abs(precision) * 8) + "_t)";
6097 type = type + "((";
8aabf152
FCE
6098 get_arg1->tok = e->tok;
6099 get_arg1->code = string("/* unprivileged */ /* pure */")
892ec39a 6100 + string(" ((int64_t)") + type
8aabf152
FCE
6101 + (is_user_module (process_name)
6102 ? string("u_fetch_register(")
6103 : string("k_fetch_register("))
46a94997 6104 + lex_cast(dwarf_regs[regname].first) + string("))")
19c22e1f 6105 + width_adjust;
8aabf152
FCE
6106 argexpr = get_arg1;
6107 goto matched;
6108 }
6109 // invalid register name, fall through
6110 }
40fe32e0 6111
272c9036 6112 int reg, offset1;
e5b7b83f 6113 // test for OFFSET(REGISTER) where OFFSET is +-N+-N+-N
40fe32e0 6114 // NB: Despite PR11821, we can use regnames here, since the parentheses
e5b7b83f 6115 // make things unambiguous. (Note: gdb/stap-probe.c also parses this)
272c9036
WF
6116 // On ARM test for [REGISTER, OFFSET]
6117 if (elf_machine == EM_ARM)
6118 {
669a2feb 6119 rc = regexp_match (asmarg, string("^\\[(")+regnames+string(")(, #([+-]?[0-9]+)([+-][0-9]*)?([+-][0-9]*)?)?\\]$"), matches);
272c9036 6120 reg = 1;
669a2feb 6121 offset1 = 3;
272c9036
WF
6122 }
6123 else
6124 {
6125 rc = regexp_match (asmarg, string("^([+-]?[0-9]*)([+-][0-9]*)?([+-][0-9]*)?[(](")+regnames+string(")[)]$"), matches);
6126 reg = 4;
6127 offset1 = 1;
6128 }
8aabf152
FCE
6129 if (! rc)
6130 {
e5b7b83f 6131 string regname;
8aabf152 6132 int64_t disp = 0;
272c9036
WF
6133 if (matches[reg].length())
6134 regname = matches[reg];
8095a157
FCE
6135 if (dwarf_regs.find (regname) == dwarf_regs.end())
6136 goto not_matched;
6137
272c9036 6138 for (int i=offset1; i <= (offset1 + 2); i++)
e5b7b83f
SC
6139 if (matches[i].length())
6140 try
6141 {
6142 disp += lex_cast<int64_t>(matches[i]); // should decode positive/negative hex/decimal
6143 }
8aabf152
FCE
6144 catch (const runtime_error& f) // unparseable offset
6145 {
6146 goto not_matched; // can't just 'break' out of
6147 // this case or use a sentinel
6148 // value, unfortunately
6149 }
6150
8aabf152 6151 // synthesize user_long(%{fetch_register(R)%} + D)
8aabf152
FCE
6152 embedded_expr *get_arg1 = new embedded_expr;
6153 get_arg1->tok = e->tok;
6154 get_arg1->code = string("/* unprivileged */ /* pure */")
6155 + (is_user_module (process_name)
6156 ? string("u_fetch_register(")
6157 : string("k_fetch_register("))
46a94997 6158 + lex_cast(dwarf_regs[regname].first) + string(")");
8aabf152 6159 // XXX: may we ever need to cast that to a narrower type?
40fe32e0 6160
8aabf152
FCE
6161 literal_number* inc = new literal_number(disp);
6162 inc->tok = e->tok;
40fe32e0 6163
8aabf152
FCE
6164 binary_expression *be = new binary_expression;
6165 be->tok = e->tok;
6166 be->left = get_arg1;
6167 be->op = "+";
6168 be->right = inc;
40fe32e0 6169
8aabf152 6170 functioncall *fc = new functioncall;
40fe32e0
SC
6171 switch (precision)
6172 {
7f6ce9ab
SC
6173 case 1: case -1:
6174 fc->function = "user_int8"; break;
6175 case 2:
6176 fc->function = "user_uint16"; break;
6177 case -2:
6178 fc->function = "user_int16"; break;
6179 case 4:
6180 fc->function = "user_uint32"; break;
6181 case -4:
6182 fc->function = "user_int32"; break;
6183 case 8: case -8:
6184 fc->function = "user_int64"; break;
40fe32e0
SC
6185 default: fc->function = "user_long";
6186 }
8aabf152
FCE
6187 fc->tok = e->tok;
6188 fc->args.push_back(be);
366af4e7 6189
8aabf152
FCE
6190 argexpr = fc;
6191 goto matched;
6192 }
8095a157
FCE
6193
6194 // test for OFFSET(BASE_REGISTER,INDEX_REGISTER[,SCALE]) where OFFSET is +-N+-N+-N
6195 // NB: Despite PR11821, we can use regnames here, since the parentheses
6196 // make things unambiguous. (Note: gdb/stap-probe.c also parses this)
0f7b51d6 6197 rc = regexp_match (asmarg, string("^([+-]?[0-9]*)([+-][0-9]*)?([+-][0-9]*)?[(](")+regnames+string("),(")+regnames+string(")(,[1248])?[)]$"), matches);
8095a157
FCE
6198 if (! rc)
6199 {
6200 string baseregname;
6201 string indexregname;
6202 int64_t disp = 0;
6203 short scale = 1;
6204
6205 if (matches[6].length())
6206 try
6207 {
6208 scale = lex_cast<short>(matches[6].substr(1)); // NB: skip the comma!
6209 // We could verify that scale is one of 1,2,4,8,
6210 // but it doesn't really matter. An erroneous
6211 // address merely results in run-time errors.
8aabf152 6212 }
8095a157
FCE
6213 catch (const runtime_error &f) // unparseable scale
6214 {
6215 goto not_matched;
6216 }
6217
6218 if (matches[4].length())
6219 baseregname = matches[4];
6220 if (dwarf_regs.find (baseregname) == dwarf_regs.end())
6221 goto not_matched;
6222
6223 if (matches[5].length())
6224 indexregname = matches[5];
6225 if (dwarf_regs.find (indexregname) == dwarf_regs.end())
6226 goto not_matched;
6227
f7719b3d 6228 for (int i = 1; i <= 3; i++) // up to three OFFSET terms
8095a157
FCE
6229 if (matches[i].length())
6230 try
6231 {
6232 disp += lex_cast<int64_t>(matches[i]); // should decode positive/negative hex/decimal
6233 }
6234 catch (const runtime_error& f) // unparseable offset
6235 {
6236 goto not_matched; // can't just 'break' out of
6237 // this case or use a sentinel
6238 // value, unfortunately
6239 }
6240
6241 // synthesize user_long(%{fetch_register(R1)+fetch_register(R2)*N%} + D)
6242
6243 embedded_expr *get_arg1 = new embedded_expr;
6244 string regfn = is_user_module (process_name)
6245 ? string("u_fetch_register")
6246 : string("k_fetch_register"); // NB: in practice sdt.h probes are for userspace only
6247
6248 get_arg1->tok = e->tok;
6249 get_arg1->code = string("/* unprivileged */ /* pure */")
6250 + regfn + string("(")+lex_cast(dwarf_regs[baseregname].first)+string(")")
6251 + string("+(")
6252 + regfn + string("(")+lex_cast(dwarf_regs[indexregname].first)+string(")")
6253 + string("*")
6254 + lex_cast(scale)
6255 + string(")");
6256
6257 // NB: could plop this +DISPLACEMENT bit into the embedded-c expression too
6258 literal_number* inc = new literal_number(disp);
6259 inc->tok = e->tok;
6260
6261 binary_expression *be = new binary_expression;
6262 be->tok = e->tok;
6263 be->left = get_arg1;
6264 be->op = "+";
6265 be->right = inc;
6266
6267 functioncall *fc = new functioncall;
6268 switch (precision)
6269 {
6270 case 1: case -1:
6271 fc->function = "user_int8"; break;
6272 case 2:
6273 fc->function = "user_uint16"; break;
6274 case -2:
6275 fc->function = "user_int16"; break;
6276 case 4:
6277 fc->function = "user_uint32"; break;
6278 case -4:
6279 fc->function = "user_int32"; break;
6280 case 8: case -8:
6281 fc->function = "user_int64"; break;
6282 default: fc->function = "user_long";
6283 }
6284 fc->tok = e->tok;
6285 fc->args.push_back(be);
6286
6287 argexpr = fc;
6288 goto matched;
8aabf152
FCE
6289 }
6290
8aabf152
FCE
6291
6292 not_matched:
6293 // The asmarg operand was not recognized. Back down to dwarf.
6294 if (! session.suppress_warnings)
84fef8ee
FCE
6295 {
6296 if (probe_type == UPROBE3_TYPE)
6c9e1946 6297 session.print_warning (_F("Can't parse SDT_V3 operand '%s' [man error::sdt]", asmarg.c_str()), e->tok);
84fef8ee 6298 else // must be *PROBE2; others don't get asm operands
b78a0fbb 6299 session.print_warning (_F("Downgrading SDT_V2 probe argument to dwarf, can't parse '%s' [man error::sdt]",
84fef8ee
FCE
6300 asmarg.c_str()), e->tok);
6301 }
8aabf152
FCE
6302 assert (argexpr == 0);
6303 need_debug_info = true;
dc09353a 6304 throw SEMANTIC_ERROR(_("SDT asm not understood, requires debuginfo [man error::sdt]"), e->tok);
366af4e7 6305
8aabf152
FCE
6306 matched:
6307 assert (argexpr != 0);
366af4e7
RM
6308
6309 if (session.verbose > 2)
1e41115c 6310 //TRANSLATORS: We're mapping the operand to a new expression*.
b530b5b3 6311 clog << _F("mapped asm operand %s to ", asmarg.c_str()) << *argexpr << endl;
366af4e7 6312
aff5d390 6313 if (e->components.empty()) // We have a scalar
8aabf152
FCE
6314 {
6315 if (e->addressof)
dc09353a 6316 throw SEMANTIC_ERROR(_("cannot take address of sdt variable"), e->tok);
8aabf152
FCE
6317 provide (argexpr);
6318 return;
6319 }
6320 else // $var->foo
6321 {
6322 cast_op *cast = new cast_op;
6323 cast->name = "@cast";
6324 cast->tok = e->tok;
6325 cast->operand = argexpr;
6326 cast->components = e->components;
6327 cast->type_name = probe_name + "_arg" + lex_cast(argno);
6328 cast->module = process_name;
6329 cast->visit(this);
6330 return;
6331 }
366af4e7 6332
8aabf152 6333 /* NOTREACHED */
aff5d390
SC
6334 }
6335 catch (const semantic_error &er)
6336 {
6337 e->chain (er);
6338 provide (e);
6339 }
6340}
6341
6342
6ef331c8
SC
6343void
6344sdt_uprobe_var_expanding_visitor::visit_target_symbol (target_symbol* e)
6345{
6346 try
6347 {
49131a6d 6348 assert(e->name.size() > 0
bd1fcbad 6349 && (e->name[0] == '$' || e->name == "@var"));
6ef331c8
SC
6350
6351 if (e->name == "$$name" || e->name == "$$provider" || e->name == "$$parms" || e->name == "$$vars")
6352 visit_target_symbol_context (e);
6353 else
6354 visit_target_symbol_arg (e);
6355 }
6356 catch (const semantic_error &er)
6357 {
6358 e->chain (er);
6359 provide (e);
6360 }
6361}
6362
6363
bd1fcbad
YZ
6364void
6365sdt_uprobe_var_expanding_visitor::visit_atvar_op (atvar_op* e)
6366{
6367 need_debug_info = true;
6368
6369 // Fill in our current module context if needed
6370 if (e->module.empty())
6371 e->module = process_name;
6372
6373 var_expanding_visitor::visit_atvar_op(e);
6374}
6375
6376
40a0c64e
JS
6377void
6378sdt_uprobe_var_expanding_visitor::visit_cast_op (cast_op* e)
6379{
6380 // Fill in our current module context if needed
6381 if (e->module.empty())
6382 e->module = process_name;
6383
6384 var_expanding_visitor::visit_cast_op(e);
6385}
6386
6387
576eaefe
SC
6388void
6389plt_expanding_visitor::visit_target_symbol (target_symbol *e)
6390{
6391 try
6392 {
6393 if (e->name == "$$name")
6394 {
6395 literal_string *myname = new literal_string (entry);
6396 myname->tok = e->tok;
6397 provide(myname);
6398 return;
6399 }
3d69c03f
JS
6400
6401 // variable not found -> throw a semantic error
6402 // (only to be caught right away, but this may be more complex later...)
6403 string alternatives = "$$name";
dc09353a 6404 throw SEMANTIC_ERROR(_F("unable to find plt variable '%s' (alternatives: %s)",
3d69c03f 6405 e->name.c_str(), alternatives.c_str()), e->tok);
576eaefe
SC
6406 }
6407 catch (const semantic_error &er)
6408 {
6409 e->chain (er);
6410 provide (e);
6411 }
6412}
6413
6414
edce5b67
JS
6415struct sdt_query : public base_query
6416{
6417 sdt_query(probe * base_probe, probe_point * base_loc,
6418 dwflpp & dw, literal_map_t const & params,
51d6bda3 6419 vector<derived_probe *> & results, const string user_lib);
edce5b67 6420
51d6bda3 6421 void query_library (const char *data);
576eaefe 6422 void query_plt (const char *entry, size_t addr) {}
edce5b67
JS
6423 void handle_query_module();
6424
6425private:
15284963 6426 stap_sdt_probe_type probe_type;
d61ea602 6427 enum { probe_section=0, note_section=1, unknown_section=-1 } probe_loc;
edce5b67
JS
6428 probe * base_probe;
6429 probe_point * base_loc;
6846cfc8 6430 literal_map_t const & params;
edce5b67 6431 vector<derived_probe *> & results;
a794dbeb
FCE
6432 string pp_mark;
6433 string pp_provider;
51d6bda3 6434 string user_lib;
edce5b67
JS
6435
6436 set<string> probes_handled;
6437
6438 Elf_Data *pdata;
6439 size_t probe_scn_offset;
6440 size_t probe_scn_addr;
aff5d390 6441 uint64_t arg_count;
40fe32e0 6442 GElf_Addr base;
c57ea854 6443 GElf_Addr pc;
aff5d390 6444 string arg_string;
edce5b67 6445 string probe_name;
a794dbeb 6446 string provider_name;
909ab234 6447 GElf_Addr semaphore_load_offset;
79a0ca08 6448 Dwarf_Addr semaphore;
edce5b67
JS
6449
6450 bool init_probe_scn();
6b51ee12 6451 bool get_next_probe();
c57ea854
SC
6452 void iterate_over_probe_entries();
6453 void handle_probe_entry();
edce5b67 6454
488d3c1b
JL
6455 static void setup_note_probe_entry_callback (sdt_query *me,
6456 int type,
6457 const char *data,
6458 size_t len);
40fe32e0
SC
6459 void setup_note_probe_entry (int type, const char *data, size_t len);
6460
edce5b67 6461 void convert_probe(probe *base);
4ddb6dd0 6462 void record_semaphore(vector<derived_probe *> & results, unsigned start);
c72aa911 6463 probe* convert_location();
40fe32e0 6464 bool have_uprobe() {return probe_type == uprobe1_type || probe_type == uprobe2_type || probe_type == uprobe3_type;}
c57ea854
SC
6465 bool have_debuginfo_uprobe(bool need_debug_info)
6466 {return probe_type == uprobe1_type
40fe32e0 6467 || ((probe_type == uprobe2_type || probe_type == uprobe3_type)
c57ea854 6468 && need_debug_info);}
40fe32e0 6469 bool have_debuginfoless_uprobe() {return probe_type == uprobe2_type || probe_type == uprobe3_type;}
edce5b67
JS
6470};
6471
6472
6473sdt_query::sdt_query(probe * base_probe, probe_point * base_loc,
6474 dwflpp & dw, literal_map_t const & params,
51d6bda3 6475 vector<derived_probe *> & results, const string user_lib):
d61ea602
JS
6476 base_query(dw, params), probe_type(unknown_probe_type),
6477 probe_loc(unknown_section), base_probe(base_probe),
74fe61bc
LB
6478 base_loc(base_loc), params(params), results(results), user_lib(user_lib),
6479 probe_scn_offset(0), probe_scn_addr(0), arg_count(0), base(0), pc(0),
909ab234 6480 semaphore_load_offset(0), semaphore(0)
edce5b67 6481{
a794dbeb
FCE
6482 assert(get_string_param(params, TOK_MARK, pp_mark));
6483 get_string_param(params, TOK_PROVIDER, pp_provider); // pp_provider == "" -> unspecified
6484
ef428667
FCE
6485 // PR10245: permit usage of dtrace-y "-" separator in marker name;
6486 // map it to double-underscores.
6487 size_t pos = 0;
6488 while (1) // there may be more than one
6489 {
a794dbeb 6490 size_t i = pp_mark.find("-", pos);
ef428667 6491 if (i == string::npos) break;
a794dbeb 6492 pp_mark.replace (i, 1, "__");
ef428667
FCE
6493 pos = i+1; // resume searching after the inserted __
6494 }
a794dbeb
FCE
6495
6496 // XXX: same for pp_provider?
edce5b67
JS
6497}
6498
6499
6500void
c57ea854 6501sdt_query::handle_probe_entry()
edce5b67 6502{
c57ea854
SC
6503 if (! have_uprobe()
6504 && !probes_handled.insert(probe_name).second)
edce5b67
JS
6505 return;
6506
6507 if (sess.verbose > 3)
c57ea854 6508 {
b78a0fbb 6509 //TRANSLATORS: Describing what probe type (kprobe or uprobe) the probe
b530b5b3
LB
6510 //TRANSLATORS: is matched to.
6511 clog << _F("matched probe_name %s probe type ", probe_name.c_str());
c57ea854
SC
6512 switch (probe_type)
6513 {
6514 case uprobe1_type:
6515 clog << "uprobe1 at 0x" << hex << pc << dec << endl;
6516 break;
6517 case uprobe2_type:
6518 clog << "uprobe2 at 0x" << hex << pc << dec << endl;
6519 break;
40fe32e0
SC
6520 case uprobe3_type:
6521 clog << "uprobe3 at 0x" << hex << pc << dec << endl;
6522 break;
d61ea602
JS
6523 default:
6524 clog << "unknown!" << endl;
6525 break;
c57ea854
SC
6526 }
6527 }
edce5b67 6528
c57ea854
SC
6529 // Extend the derivation chain
6530 probe *new_base = convert_location();
6531 probe_point *new_location = new_base->locations[0];
6532
c57ea854
SC
6533 bool need_debug_info = false;
6534
7d395255
JS
6535 // We could get the Elf* from either dwarf_getelf(dwfl_module_getdwarf(...))
6536 // or dwfl_module_getelf(...). We only need it for the machine type, which
6537 // should be the same. The bias is used for relocating debuginfoless probes,
6538 // though, so that must come from the possibly-prelinked ELF file, not DWARF.
c57ea854 6539 Dwarf_Addr bias;
7d395255 6540 Elf* elf = dwfl_module_getelf (dw.mod_info->mod, &bias);
c57ea854 6541
1cc41cd6
DS
6542 /* Figure out the architecture of this particular ELF file. The
6543 dwarfless register-name mappings depend on it. */
6544 GElf_Ehdr ehdr_mem;
6545 GElf_Ehdr* em = gelf_getehdr (elf, &ehdr_mem);
e1c3b13a 6546 if (em == 0) { DWFL_ASSERT ("dwfl_getehdr", dwfl_errno()); }
d8f31d0a 6547 assert(em);
1cc41cd6
DS
6548 int elf_machine = em->e_machine;
6549 sdt_uprobe_var_expanding_visitor svv (sess, elf_machine, module_val,
6550 provider_name, probe_name,
6551 probe_type, arg_string, arg_count);
6552 svv.replace (new_base->body);
6553 need_debug_info = svv.need_debug_info;
c57ea854 6554
1cc41cd6
DS
6555 // XXX: why not derive_probes() in the uprobes case too?
6556 literal_map_t params;
6557 for (unsigned i = 0; i < new_location->components.size(); ++i)
6558 {
6559 probe_point::component *c = new_location->components[i];
6560 params[c->functor] = c->arg;
6561 }
c57ea854 6562
73d53dd2 6563 unsigned prior_results_size = results.size();
1cc41cd6
DS
6564 dwarf_query q(new_base, new_location, dw, params, results, "", "");
6565 q.has_mark = true; // enables mid-statement probing
30263a73 6566
73d53dd2
JS
6567 // V1 probes always need dwarf info
6568 // V2+ probes need dwarf info in case of a variable reference
1cc41cd6 6569 if (have_debuginfo_uprobe(need_debug_info))
06de3a04 6570 dw.iterate_over_modules<base_query>(&query_module, &q);
73d53dd2
JS
6571
6572 // For V2+ probes, if variable references weren't used or failed (PR14369),
6573 // then try with the more direct approach. Unresolved $vars might still
6574 // cause their own error, but this gives them a chance to be optimized out.
6575 if (have_debuginfoless_uprobe() && results.size() == prior_results_size)
1cc41cd6
DS
6576 {
6577 string section;
6578 Dwarf_Addr reloc_addr = q.statement_num_val + bias;
6579 if (dwfl_module_relocations (q.dw.mod_info->mod) > 0)
6580 {
6581 dwfl_module_relocate_address (q.dw.mod_info->mod, &reloc_addr);
6582 section = ".dynamic";
6583 }
6584 else
6585 section = ".absolute";
edce5b67 6586
1cc41cd6
DS
6587 uprobe_derived_probe* p =
6588 new uprobe_derived_probe ("", "", 0,
6589 path_remove_sysroot(sess,q.module_val),
6590 section,
6591 q.statement_num_val, reloc_addr, q, 0);
6592 p->saveargs (arg_count);
6593 results.push_back (p);
c57ea854 6594 }
487bf4e2 6595 sess.unwindsym_modules.insert (dw.module_name);
73d53dd2 6596 record_semaphore(results, prior_results_size);
c57ea854 6597}
edce5b67 6598
4ddb6dd0 6599
c57ea854
SC
6600void
6601sdt_query::handle_query_module()
6602{
6603 if (!init_probe_scn())
6604 return;
edce5b67 6605
c57ea854
SC
6606 if (sess.verbose > 3)
6607 clog << "TOK_MARK: " << pp_mark << " TOK_PROVIDER: " << pp_provider << endl;
edce5b67 6608
40fe32e0
SC
6609 if (probe_loc == note_section)
6610 {
6611 GElf_Shdr shdr_mem;
6612 GElf_Shdr *shdr = dw.get_section (".stapsdt.base", &shdr_mem);
6613
909ab234
JS
6614 // The 'base' lets us adjust the hardcoded addresses in notes for prelink
6615 // effects. The 'semaphore_load_offset' accounts for the difference in
6616 // load addresses between text and data, so the semaphore can be
6617 // converted to a file offset if needed.
40fe32e0 6618 if (shdr)
909ab234
JS
6619 {
6620 base = shdr->sh_addr;
6621 GElf_Addr base_offset = shdr->sh_offset;
6622 shdr = dw.get_section (".probes", &shdr_mem);
6623 if (shdr)
6624 semaphore_load_offset =
6625 (shdr->sh_addr - shdr->sh_offset) - (base - base_offset);
6626 }
40fe32e0 6627 else
909ab234
JS
6628 base = semaphore_load_offset = 0;
6629
488d3c1b 6630 dw.iterate_over_notes (this, &sdt_query::setup_note_probe_entry_callback);
40fe32e0 6631 }
d61ea602 6632 else if (probe_loc == probe_section)
40fe32e0 6633 iterate_over_probe_entries ();
edce5b67
JS
6634}
6635
6636
6637bool
6638sdt_query::init_probe_scn()
6639{
448a86b7 6640 Elf* elf;
edce5b67 6641 GElf_Shdr shdr_mem;
40fe32e0
SC
6642
6643 GElf_Shdr *shdr = dw.get_section (".note.stapsdt", &shdr_mem);
6644 if (shdr)
6645 {
6646 probe_loc = note_section;
6647 return true;
6648 }
edce5b67 6649
448a86b7 6650 shdr = dw.get_section (".probes", &shdr_mem, &elf);
fea74777 6651 if (shdr)
edce5b67 6652 {
fea74777
SC
6653 pdata = elf_getdata_rawchunk (elf, shdr->sh_offset, shdr->sh_size, ELF_T_BYTE);
6654 probe_scn_offset = 0;
6655 probe_scn_addr = shdr->sh_addr;
6656 assert (pdata != NULL);
6657 if (sess.verbose > 4)
ce0f6648
LB
6658 clog << "got .probes elf scn_addr@0x" << probe_scn_addr << ", size: "
6659 << pdata->d_size << endl;
40fe32e0 6660 probe_loc = probe_section;
fea74777 6661 return true;
edce5b67 6662 }
fea74777 6663 else
edce5b67 6664 return false;
edce5b67
JS
6665}
6666
40fe32e0 6667void
488d3c1b 6668sdt_query::setup_note_probe_entry_callback (sdt_query *me, int type, const char *data, size_t len)
40fe32e0 6669{
40fe32e0
SC
6670 me->setup_note_probe_entry (type, data, len);
6671}
6672
6673
6674void
6675sdt_query::setup_note_probe_entry (int type, const char *data, size_t len)
6676{
6677 // if (nhdr.n_namesz == sizeof _SDT_NOTE_NAME
6678 // && !memcmp (data->d_buf + name_off,
6679 // _SDT_NOTE_NAME, sizeof _SDT_NOTE_NAME))
6680
6681 // probes are in the .note.stapsdt section
6682#define _SDT_NOTE_TYPE 3
6683 if (type != _SDT_NOTE_TYPE)
6684 return;
6685
6686 union
6687 {
6688 Elf64_Addr a64[3];
6689 Elf32_Addr a32[3];
6690 } buf;
6691 Dwarf_Addr bias;
6692 Elf* elf = (dwfl_module_getelf (dw.mod_info->mod, &bias));
6693 Elf_Data dst =
6694 {
6695 &buf, ELF_T_ADDR, EV_CURRENT,
6696 gelf_fsize (elf, ELF_T_ADDR, 3, EV_CURRENT), 0, 0
6697 };
6698 assert (dst.d_size <= sizeof buf);
6699
6700 if (len < dst.d_size + 3)
6701 return;
6702
6703 Elf_Data src =
6704 {
6705 (void *) data, ELF_T_ADDR, EV_CURRENT,
6706 dst.d_size, 0, 0
6707 };
6708
6709 if (gelf_xlatetom (elf, &dst, &src,
6710 elf_getident (elf, NULL)[EI_DATA]) == NULL)
6711 printf ("gelf_xlatetom: %s", elf_errmsg (-1));
6712
6713 probe_type = uprobe3_type;
6714 const char * provider = data + dst.d_size;
3f803f9e 6715
40fe32e0 6716 const char *name = (const char*)memchr (provider, '\0', data + len - provider);
3f803f9e
CM
6717 if(name++ == NULL)
6718 return;
6719
6720 const char *args = (const char*)memchr (name, '\0', data + len - name);
6721 if (args++ == NULL || memchr (args, '\0', data + len - name) != data + len - 1)
6722 return;
6723
6724 provider_name = provider;
6725 probe_name = name;
6726 arg_string = args;
40fe32e0 6727
e90006d7
JL
6728 dw.mod_info->marks.insert(make_pair(provider, name));
6729
40fe32e0
SC
6730 // Did we find a matching probe?
6731 if (! (dw.function_name_matches_pattern (probe_name, pp_mark)
6732 && ((pp_provider == "")
6733 || dw.function_name_matches_pattern (provider_name, pp_provider))))
6734 return;
6735
e9a90eee
JS
6736 // PR13934: Assembly probes are not forced to use the N@OP form.
6737 // If we have '@' then great, else count based on space-delimiters.
6738 arg_count = count(arg_string.begin(), arg_string.end(), '@');
6739 if (!arg_count && !arg_string.empty())
6740 arg_count = 1 + count(arg_string.begin(), arg_string.end(), ' ');
6741
40fe32e0
SC
6742 GElf_Addr base_ref;
6743 if (gelf_getclass (elf) == ELFCLASS32)
6744 {
6745 pc = buf.a32[0];
6746 base_ref = buf.a32[1];
6747 semaphore = buf.a32[2];
6748 }
6749 else
6750 {
6751 pc = buf.a64[0];
6752 base_ref = buf.a64[1];
6753 semaphore = buf.a64[2];
6754 }
6755
6756 semaphore += base - base_ref;
6757 pc += base - base_ref;
6758
7d395255
JS
6759 // The semaphore also needs the ELF bias added now, so
6760 // record_semaphore can properly relocate it later.
6761 semaphore += bias;
6762
40fe32e0 6763 if (sess.verbose > 4)
b530b5b3 6764 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
6765
6766 handle_probe_entry();
6767}
6768
6769
c57ea854
SC
6770void
6771sdt_query::iterate_over_probe_entries()
edce5b67 6772{
c57ea854 6773 // probes are in the .probe section
edce5b67
JS
6774 while (probe_scn_offset < pdata->d_size)
6775 {
aff5d390
SC
6776 stap_sdt_probe_entry_v1 *pbe_v1 = (stap_sdt_probe_entry_v1 *) ((char*)pdata->d_buf + probe_scn_offset);
6777 stap_sdt_probe_entry_v2 *pbe_v2 = (stap_sdt_probe_entry_v2 *) ((char*)pdata->d_buf + probe_scn_offset);
15284963 6778 probe_type = (stap_sdt_probe_type)(pbe_v1->type_a);
1cc41cd6 6779 if (! have_uprobe())
edce5b67
JS
6780 {
6781 // Unless this is a mangled .probes section, this happens
6782 // because the name of the probe comes first, followed by
6783 // the sentinel.
6784 if (sess.verbose > 5)
b530b5b3 6785 clog << _F("got unknown probe_type : 0x%x", probe_type) << endl;
edce5b67
JS
6786 probe_scn_offset += sizeof(__uint32_t);
6787 continue;
6788 }
aff5d390
SC
6789 if ((long)pbe_v1 % sizeof(__uint64_t)) // we have stap_sdt_probe_entry_v1.type_b
6790 {
6791 pbe_v1 = (stap_sdt_probe_entry_v1*)((char*)pbe_v1 - sizeof(__uint32_t));
1cc41cd6 6792 if (pbe_v1->type_b != uprobe1_type)
aff5d390
SC
6793 continue;
6794 }
6795
1cc41cd6 6796 if (probe_type == uprobe1_type)
aff5d390 6797 {
79a0ca08 6798 if (pbe_v1->name == 0) // No name possibly means we have a .so with a relocation
c57ea854 6799 return;
79a0ca08 6800 semaphore = 0;
aff5d390 6801 probe_name = (char*)((char*)pdata->d_buf + pbe_v1->name - (char*)probe_scn_addr);
a794dbeb 6802 provider_name = ""; // unknown
1cc41cd6
DS
6803 pc = pbe_v1->arg;
6804 arg_count = 0;
aff5d390
SC
6805 probe_scn_offset += sizeof (stap_sdt_probe_entry_v1);
6806 }
08b5a50c 6807 else if (probe_type == uprobe2_type)
aff5d390 6808 {
79a0ca08 6809 if (pbe_v2->name == 0) // No name possibly means we have a .so with a relocation
c57ea854 6810 return;
79a0ca08 6811 semaphore = pbe_v2->semaphore;
aff5d390 6812 probe_name = (char*)((char*)pdata->d_buf + pbe_v2->name - (char*)probe_scn_addr);
a794dbeb 6813 provider_name = (char*)((char*)pdata->d_buf + pbe_v2->provider - (char*)probe_scn_addr);
aff5d390
SC
6814 arg_count = pbe_v2->arg_count;
6815 pc = pbe_v2->pc;
6816 if (pbe_v2->arg_string)
6817 arg_string = (char*)((char*)pdata->d_buf + pbe_v2->arg_string - (char*)probe_scn_addr);
79a0ca08
SC
6818 // skip over pbe_v2, probe_name text and provider text
6819 probe_scn_offset = ((long)(pbe_v2->name) - (long)(probe_scn_addr)) + probe_name.length();
6820 probe_scn_offset += sizeof (__uint32_t) - probe_scn_offset % sizeof (__uint32_t);
aff5d390 6821 }
edce5b67 6822 if (sess.verbose > 4)
b530b5b3 6823 clog << _("saw .probes ") << probe_name << (provider_name != "" ? _(" (provider ")+provider_name+") " : "")
aff5d390 6824 << "@0x" << hex << pc << dec << endl;
edce5b67 6825
e90006d7
JL
6826 dw.mod_info->marks.insert(make_pair(provider_name, probe_name));
6827
a794dbeb
FCE
6828 if (dw.function_name_matches_pattern (probe_name, pp_mark)
6829 && ((pp_provider == "") || dw.function_name_matches_pattern (provider_name, pp_provider)))
c57ea854 6830 handle_probe_entry ();
edce5b67 6831 }
edce5b67
JS
6832}
6833
6834
6846cfc8 6835void
4ddb6dd0 6836sdt_query::record_semaphore (vector<derived_probe *> & results, unsigned start)
6846cfc8 6837{
a794dbeb
FCE
6838 for (unsigned i=0; i<2; i++) {
6839 // prefer with-provider symbol; look without provider prefix for backward compatibility only
6840 string semaphore = (i==0 ? (provider_name+"_") : "") + probe_name + "_semaphore";
6841 // XXX: multiple addresses?
6842 if (sess.verbose > 2)
b530b5b3 6843 clog << _F("looking for semaphore symbol %s ", semaphore.c_str());
a794dbeb 6844
79a0ca08
SC
6845 Dwarf_Addr addr;
6846 if (this->semaphore)
6847 addr = this->semaphore;
6848 else
6849 addr = lookup_symbol_address(dw.module, semaphore.c_str());
a794dbeb
FCE
6850 if (addr)
6851 {
7d395255 6852 if (dwfl_module_relocations (dw.module) > 0)
a794dbeb
FCE
6853 dwfl_module_relocate_address (dw.module, &addr);
6854 // XXX: relocation basis?
909ab234
JS
6855
6856 // Dyninst needs the *file*-based offset for semaphores,
6857 // so subtract the difference in load addresses between .text and .probes
6858 if (dw.sess.runtime_usermode_p())
6859 addr -= semaphore_load_offset;
6860
a794dbeb
FCE
6861 for (unsigned i = start; i < results.size(); ++i)
6862 results[i]->sdt_semaphore_addr = addr;
6863 if (sess.verbose > 2)
b530b5b3 6864 clog << _(", found at 0x") << hex << addr << dec << endl;
a794dbeb
FCE
6865 return;
6866 }
6867 else
6868 if (sess.verbose > 2)
b530b5b3 6869 clog << _(", not found") << endl;
a794dbeb 6870 }
6846cfc8
SC
6871}
6872
6873
edce5b67
JS
6874void
6875sdt_query::convert_probe (probe *base)
6876{
6877 block *b = new block;
6878 b->tok = base->body->tok;
6879
edce5b67
JS
6880 // Generate: if (arg1 != mark("label")) next;
6881 functioncall *fc = new functioncall;
bbafcb1e 6882 fc->function = "ulong_arg";
edce5b67 6883 fc->tok = b->tok;
bbafcb1e 6884 literal_number* num = new literal_number(1);
edce5b67
JS
6885 num->tok = b->tok;
6886 fc->args.push_back(num);
6887
6888 functioncall *fcus = new functioncall;
6889 fcus->function = "user_string";
6890 fcus->type = pe_string;
6891 fcus->tok = b->tok;
6892 fcus->args.push_back(fc);
6893
6894 if_statement *is = new if_statement;
6895 is->thenblock = new next_statement;
6896 is->elseblock = NULL;
6897 is->tok = b->tok;
63ea4244 6898 is->thenblock->tok = b->tok;
edce5b67
JS
6899 comparison *be = new comparison;
6900 be->op = "!=";
6901 be->tok = b->tok;
6902 be->left = fcus;
6903 be->right = new literal_string(probe_name);
63ea4244 6904 be->right->tok = b->tok;
edce5b67
JS
6905 is->condition = be;
6906 b->statements.push_back(is);
6907
6908 // Now replace the body
6909 b->statements.push_back(base->body);
6910 base->body = b;
6911}
6912
6913
c72aa911
JS
6914probe*
6915sdt_query::convert_location ()
edce5b67 6916{
c72aa911 6917 probe_point* specific_loc = new probe_point(*base_loc);
662539d9 6918 vector<probe_point::component*> derived_comps;
edce5b67 6919
662539d9
JS
6920 vector<probe_point::component*>::iterator it;
6921 for (it = specific_loc->components.begin();
6922 it != specific_loc->components.end(); ++it)
6923 if ((*it)->functor == TOK_PROCESS)
6924 {
1cc41cd6
DS
6925 // copy the process name
6926 derived_comps.push_back(*it);
662539d9
JS
6927 }
6928 else if ((*it)->functor == TOK_LIBRARY)
6929 {
1cc41cd6
DS
6930 // copy the library name for process probes
6931 derived_comps.push_back(*it);
662539d9
JS
6932 }
6933 else if ((*it)->functor == TOK_PROVIDER)
6934 {
6935 // replace the possibly wildcarded arg with the specific provider name
6936 *it = new probe_point::component(TOK_PROVIDER,
6937 new literal_string(provider_name));
6938 }
6939 else if ((*it)->functor == TOK_MARK)
c72aa911
JS
6940 {
6941 // replace the possibly wildcarded arg with the specific marker name
662539d9
JS
6942 *it = new probe_point::component(TOK_MARK,
6943 new literal_string(probe_name));
a794dbeb 6944
aff5d390
SC
6945 if (sess.verbose > 3)
6946 switch (probe_type)
6947 {
6948 case uprobe1_type:
b530b5b3 6949 clog << _("probe_type == uprobe1, use statement addr: 0x")
aff5d390
SC
6950 << hex << pc << dec << endl;
6951 break;
6952 case uprobe2_type:
b530b5b3 6953 clog << _("probe_type == uprobe2, use statement addr: 0x")
aff5d390
SC
6954 << hex << pc << dec << endl;
6955 break;
40fe32e0 6956 case uprobe3_type:
b530b5b3 6957 clog << _("probe_type == uprobe3, use statement addr: 0x")
40fe32e0
SC
6958 << hex << pc << dec << endl;
6959 break;
aff5d390 6960 default:
b530b5b3
LB
6961 clog << _F("probe_type == use_uprobe_no_dwarf, use label name: _stapprobe1_%s",
6962 pp_mark.c_str()) << endl;
aff5d390
SC
6963 }
6964
c72aa911
JS
6965 switch (probe_type)
6966 {
aff5d390
SC
6967 case uprobe1_type:
6968 case uprobe2_type:
40fe32e0 6969 case uprobe3_type:
c72aa911 6970 // process("executable").statement(probe_arg)
662539d9
JS
6971 derived_comps.push_back
6972 (new probe_point::component(TOK_STATEMENT,
6973 new literal_number(pc, true)));
c72aa911
JS
6974 break;
6975
a794dbeb 6976 default: // deprecated
c72aa911 6977 // process("executable").function("*").label("_stapprobe1_MARK_NAME")
662539d9
JS
6978 derived_comps.push_back
6979 (new probe_point::component(TOK_FUNCTION,
6980 new literal_string("*")));
6981 derived_comps.push_back
c72aa911 6982 (new probe_point::component(TOK_LABEL,
a794dbeb 6983 new literal_string("_stapprobe1_" + pp_mark)));
c72aa911
JS
6984 break;
6985 }
6986 }
edce5b67 6987
662539d9
JS
6988 probe_point* derived_loc = new probe_point(*specific_loc);
6989 derived_loc->components = derived_comps;
8159bf55 6990 return new probe (new probe (base_probe, specific_loc), derived_loc);
edce5b67
JS
6991}
6992
6993
51d6bda3
SC
6994void
6995sdt_query::query_library (const char *library)
6996{
6997 query_one_library (library, dw, user_lib, base_probe, base_loc, results);
6998}
6999
e90006d7
JL
7000string
7001suggest_marks(systemtap_session& sess,
7002 const set<string>& modules,
7003 const string& mark,
7004 const string& provider)
7005{
7006 if (mark.empty() || modules.empty() || sess.module_cache == NULL)
7007 return "";
7008
7009 set<string> marks;
7010 const map<string, module_info*> &cache = sess.module_cache->cache;
7011 bool dash_suggestions = (mark.find("-") != string::npos);
7012
7013 for (set<string>::iterator itmod = modules.begin();
7014 itmod != modules.end(); ++itmod)
7015 {
7016 map<string, module_info*>::const_iterator itcache;
7017 if ((itcache = cache.find(*itmod)) != cache.end())
7018 {
7019 set<pair<string,string> >::const_iterator itmarks;
7020 for (itmarks = itcache->second->marks.begin();
7021 itmarks != itcache->second->marks.end(); ++itmarks)
7022 {
7023 if (provider.empty()
7024 // simulating dw.function_name_matches_pattern()
7025 || (fnmatch(provider.c_str(), itmarks->first.c_str(), 0) == 0))
7026 {
7027 string marksug = itmarks->second;
7028 if (dash_suggestions)
7029 {
7030 size_t pos = 0;
7031 while (1) // there may be more than one
7032 {
7033 size_t i = marksug.find("__", pos);
7034 if (i == string::npos) break;
7035 marksug.replace (i, 2, "-");
7036 pos = i+1; // resume searching after the inserted -
7037 }
7038 }
7039 marks.insert(marksug);
7040 }
7041 }
7042 }
7043 }
7044
7045 if (sess.verbose > 2)
7046 {
7047 clog << "suggesting " << marks.size() << " marks "
7048 << "from modules:" << endl;
7049 for (set<string>::iterator itmod = modules.begin();
7050 itmod != modules.end(); ++itmod)
7051 clog << *itmod << endl;
7052 }
7053
7054 if (marks.empty())
7055 return "";
7056
7057 return levenshtein_suggest(mark, marks, 5); // print top 5 marks only
7058}
7059
40a70f52
JL
7060string
7061suggest_plt_functions(systemtap_session& sess,
7062 const set<string>& modules,
7063 const string& func)
7064{
7065 if (func.empty() || modules.empty() || sess.module_cache == NULL)
7066 return "";
7067
7068 set<string> funcs;
7069 const map<string, module_info*> &cache = sess.module_cache->cache;
7070
7071 for (set<string>::iterator itmod = modules.begin();
7072 itmod != modules.end(); ++itmod)
7073 {
7074 map<string, module_info*>::const_iterator itcache;
7075 if ((itcache = cache.find(*itmod)) != cache.end())
7076 funcs.insert(itcache->second->plt_funcs.begin(),
7077 itcache->second->plt_funcs.end());
7078 }
7079
7080 if (sess.verbose > 2)
d82dd71f
JL
7081 {
7082 clog << "suggesting " << funcs.size() << " plt functions "
7083 << "from modules:" << endl;
7084 for (set<string>::iterator itmod = modules.begin();
7085 itmod != modules.end(); ++itmod)
7086 clog << *itmod << endl;
7087 }
40a70f52
JL
7088
7089 if (funcs.empty())
7090 return "";
7091
7092 return levenshtein_suggest(func, funcs, 5); // print top 5 funcs only
7093}
51d6bda3 7094
0508c45f
JL
7095string
7096suggest_dwarf_functions(systemtap_session& sess,
7097 const set<string>& modules,
7098 string func)
44ffe90c
JL
7099{
7100 // Trim any @ component
7101 size_t pos = func.find('@');
7102 if (pos != string::npos)
7103 func.erase(pos);
7104
0508c45f 7105 if (func.empty() || modules.empty() || sess.module_cache == NULL)
44ffe90c
JL
7106 return "";
7107
7108 // We must first aggregate all the functions from the cache
7109 set<string> funcs;
7110 const map<string, module_info*> &cache = sess.module_cache->cache;
7111
0508c45f
JL
7112 for (set<string>::iterator itmod = modules.begin();
7113 itmod != modules.end(); ++itmod)
44ffe90c 7114 {
6e6d9a3b
JL
7115 module_info *module;
7116
7117 // retrieve module_info from cache
44ffe90c
JL
7118 map<string, module_info*>::const_iterator itcache;
7119 if ((itcache = cache.find(*itmod)) != cache.end())
6e6d9a3b
JL
7120 module = itcache->second;
7121 else // module not found
7122 continue;
7123
7124 // add inlines
7125 funcs.insert(module->inlined_funcs.begin(),
7126 module->inlined_funcs.end());
7127
7128 // add all function symbols in cache
7129 if (module->symtab_status != info_present || module->sym_table == NULL)
7130 continue;
7131 map<string, func_info*>& modfuncs = module->sym_table->map_by_name;
7132 for (map<string, func_info*>::const_iterator itfuncs = modfuncs.begin();
7133 itfuncs != modfuncs.end(); ++itfuncs)
7134 funcs.insert(itfuncs->first);
44ffe90c
JL
7135 }
7136
7137 if (sess.verbose > 2)
d82dd71f
JL
7138 {
7139 clog << "suggesting " << funcs.size() << " dwarf functions "
7140 << "from modules:" << endl;
7141 for (set<string>::iterator itmod = modules.begin();
7142 itmod != modules.end(); ++itmod)
7143 clog << *itmod << endl;
7144 }
44ffe90c
JL
7145
7146 if (funcs.empty())
7147 return "";
7148
593f09eb 7149 return levenshtein_suggest(func, funcs, 5); // print top 5 funcs only
44ffe90c
JL
7150}
7151
20c6c071 7152void
5227f1ea 7153dwarf_builder::build(systemtap_session & sess,
7a053d3b 7154 probe * base,
20c6c071 7155 probe_point * location,
86bf665e 7156 literal_map_t const & parameters,
20c6c071
GH
7157 vector<derived_probe *> & finished_results)
7158{
b20febf3
FCE
7159 // NB: the kernel/user dwlfpp objects are long-lived.
7160 // XXX: but they should be per-session, as this builder object
7161 // may be reused if we try to cross-instrument multiple targets.
84048984 7162
7a24d422 7163 dwflpp* dw = 0;
6d5d594e 7164 literal_map_t filled_parameters = parameters;
7a24d422 7165
7a24d422 7166 string module_name;
ae2552da
FCE
7167 if (has_null_param (parameters, TOK_KERNEL))
7168 {
7169 dw = get_kern_dw(sess, "kernel");
7170 }
7171 else if (get_param (parameters, TOK_MODULE, module_name))
b8da0ad1 7172 {
c523a015
LB
7173 size_t dash_pos = 0;
7174 while((dash_pos=module_name.find('-'))!=string::npos)
7175 module_name.replace(int(dash_pos),1,"_");
7176 filled_parameters[TOK_MODULE] = new literal_string(module_name);
37001baa
FCE
7177 // NB: glob patterns get expanded later, during the offline
7178 // elfutils module listing.
ae2552da 7179 dw = get_kern_dw(sess, module_name);
b8da0ad1 7180 }
6d5d594e 7181 else if (get_param (parameters, TOK_PROCESS, module_name) || has_null_param(parameters, TOK_PROCESS))
84c84ac4 7182 {
05fb3e0c 7183 module_name = sess.sysroot + module_name;
6d5d594e
LB
7184 if(has_null_param(filled_parameters, TOK_PROCESS))
7185 {
fc18e6c4
JL
7186 string file;
7187 try
7188 {
7189 file = sess.cmd_file();
7190 }
7191 catch (const semantic_error& e)
7192 {
7193 throw SEMANTIC_ERROR(_("invalid -c command for unspecified process"
7194 " probe [man stapprobes]"), NULL, NULL, &e);
7195 }
48cd804a 7196 if(file.empty())
fc18e6c4
JL
7197 throw SEMANTIC_ERROR(_("unspecified process probe is invalid without"
7198 " a -c COMMAND [man stapprobes]"));
48cd804a 7199 module_name = sess.sysroot + file;
6d5d594e
LB
7200 filled_parameters[TOK_PROCESS] = new literal_string(module_name);// this needs to be used in place of the blank map
7201 // in the case of TOK_MARK we need to modify locations as well
7202 if(location->components[0]->functor==TOK_PROCESS &&
7203 location->components[0]->arg == 0)
7204 location->components[0]->arg = new literal_string(module_name);
b78a0fbb 7205 }
5750ecc6 7206
37001baa
FCE
7207 // PR6456 process("/bin/*") glob handling
7208 if (contains_glob_chars (module_name))
7209 {
7210 // Expand glob via rewriting the probe-point process("....")
7211 // parameter, asserted to be the first one.
7212
7213 assert (location->components.size() > 0);
7214 assert (location->components[0]->functor == TOK_PROCESS);
7215 assert (location->components[0]->arg);
7216 literal_string* lit = dynamic_cast<literal_string*>(location->components[0]->arg);
7217 assert (lit);
7218
7219 // Evaluate glob here, and call derive_probes recursively with each match.
7220 glob_t the_blob;
88f8e2c0 7221 set<string> dupes;
37001baa 7222 int rc = glob (module_name.c_str(), 0, NULL, & the_blob);
b530b5b3 7223 if (rc)
dc09353a 7224 throw SEMANTIC_ERROR (_F("glob %s error (%s)", module_name.c_str(), lex_cast(rc).c_str() ));
44ffe90c 7225 unsigned results_pre = finished_results.size();
37001baa
FCE
7226 for (unsigned i = 0; i < the_blob.gl_pathc; ++i)
7227 {
e19ebcf7 7228 assert_no_interrupts();
37001baa
FCE
7229
7230 const char* globbed = the_blob.gl_pathv[i];
7231 struct stat st;
7232
7233 if (access (globbed, X_OK) == 0
7234 && stat (globbed, &st) == 0
7235 && S_ISREG (st.st_mode)) // see find_executable()
7236 {
7977a734
FCE
7237 // Need to call canonicalize here, in order to path-expand
7238 // patterns like process("stap*"). Otherwise it may go through
7239 // to the next round of expansion as ("stap"), leading to a $PATH
7240 // search that's not consistent with the glob search already done.
5bca76a8
JS
7241 string canononicalized = resolve_path (globbed);
7242 globbed = canononicalized.c_str();
7977a734 7243
88f8e2c0
JS
7244 // The canonical names can result in duplication, for example
7245 // having followed symlinks that are common with shared
7246 // libraries. Filter those out.
7247 if (!dupes.insert(canononicalized).second)
7248 continue;
7249
37001baa
FCE
7250 // synthesize a new probe_point, with the glob-expanded string
7251 probe_point *pp = new probe_point (*location);
ef0943df
JL
7252 pp->from_glob = true;
7253
5750ecc6
FCE
7254 // PR13338: quote results to prevent recursion
7255 string eglobbed = escape_glob_chars (globbed);
7256
7257 if (sess.verbose > 1)
7258 clog << _F("Expanded process(\"%s\") to process(\"%s\")",
7259 module_name.c_str(), eglobbed.c_str()) << endl;
05fb3e0c 7260 string eglobbed_tgt = path_remove_sysroot(sess, eglobbed);
5750ecc6 7261
37001baa 7262 probe_point::component* ppc = new probe_point::component (TOK_PROCESS,
05fb3e0c 7263 new literal_string (eglobbed_tgt));
37001baa
FCE
7264 ppc->tok = location->components[0]->tok; // overwrite [0] slot, pattern matched above
7265 pp->components[0] = ppc;
7266
d885563b 7267 probe* new_probe = new probe (base, pp);
7977a734
FCE
7268
7269 // We override "optional = true" here, as if the
7270 // wildcarded probe point was given a "?" suffix.
7271
7272 // This is because wildcard probes will be expected
7273 // by users to apply only to some subset of the
7274 // matching binaries, in the sense of "any", rather
7275 // than "all", sort of similarly how
7276 // module("*").function("...") patterns work.
7277
7278 derive_probes (sess, new_probe, finished_results,
7279 true /* NB: not location->optional */ );
37001baa
FCE
7280 }
7281 }
7282
7283 globfree (& the_blob);
44ffe90c
JL
7284
7285 unsigned results_post = finished_results.size();
7286
e90006d7 7287 // Did we fail to find a function/plt/mark by name? Let's suggest
44ffe90c
JL
7288 // something!
7289 string func;
7290 if (results_pre == results_post
7291 && get_param(filled_parameters, TOK_FUNCTION, func)
7292 && !func.empty())
7293 {
0508c45f 7294 string sugs = suggest_dwarf_functions(sess, modules_seen, func);
44ffe90c
JL
7295 modules_seen.clear();
7296 if (!sugs.empty())
dc09353a 7297 throw SEMANTIC_ERROR (_NF("no match (similar function: %s)",
44ffe90c
JL
7298 "no match (similar functions: %s)",
7299 sugs.find(',') == string::npos,
7300 sugs.c_str()));
7301 }
40a70f52
JL
7302 else if (results_pre == results_post
7303 && get_param(filled_parameters, TOK_PLT, func)
7304 && !func.empty())
7305 {
7306 string sugs = suggest_plt_functions(sess, modules_seen, func);
7307 modules_seen.clear();
7308 if (!sugs.empty())
7309 throw SEMANTIC_ERROR (_NF("no match (similar function: %s)",
7310 "no match (similar functions: %s)",
7311 sugs.find(',') == string::npos,
7312 sugs.c_str()));
7313 }
e90006d7
JL
7314 else if (results_pre == results_post
7315 && get_param(filled_parameters, TOK_MARK, func)
7316 && !func.empty())
7317 {
7318 string provider;
7319 get_param(filled_parameters, TOK_PROVIDER, provider);
7320
7321 string sugs = suggest_marks(sess, modules_seen, func, provider);
7322 modules_seen.clear();
7323 if (!sugs.empty())
7324 throw SEMANTIC_ERROR (_NF("no match (similar mark: %s)",
7325 "no match (similar marks: %s)",
7326 sugs.find(',') == string::npos,
7327 sugs.c_str()));
7328 }
44ffe90c 7329
37001baa
FCE
7330 return; // avoid falling through
7331 }
7332
5750ecc6
FCE
7333 // PR13338: unquote glob results
7334 module_name = unescape_glob_chars (module_name);
05fb3e0c 7335 user_path = find_executable (module_name, "", sess.sysenv); // canonicalize it
d1bcbe71
RH
7336
7337 // if the executable starts with "#!", we look for the interpreter of the script
7338 {
7339 ifstream script_file (user_path.c_str () );
7340
7341 if (script_file.good ())
7342 {
7343 string line;
7344
7345 getline (script_file, line);
7346
7347 if (line.compare (0, 2, "#!") == 0)
7348 {
7349 string path_head = line.substr(2);
7350
7351 // remove white spaces at the beginning of the string
7352 size_t p2 = path_head.find_first_not_of(" \t");
7353
7354 if (p2 != string::npos)
7355 {
7356 string path = path_head.substr(p2);
7357
7358 // remove white spaces at the end of the string
7359 p2 = path.find_last_not_of(" \t\n");
7360 if (string::npos != p2)
7361 path.erase(p2+1);
7362
8e13c1a1
RH
7363 // handle "#!/usr/bin/env" redirect
7364 size_t offset = 0;
7365 if (path.compare(0, sizeof("/bin/env")-1, "/bin/env") == 0)
7366 {
7367 offset = sizeof("/bin/env")-1;
7368 }
7369 else if (path.compare(0, sizeof("/usr/bin/env")-1, "/usr/bin/env") == 0)
7370 {
7371 offset = sizeof("/usr/bin/env")-1;
7372 }
7373
7374 if (offset != 0)
7375 {
7376 size_t p3 = path.find_first_not_of(" \t", offset);
7377
7378 if (p3 != string::npos)
7379 {
7380 string env_path = path.substr(p3);
05fb3e0c
WF
7381 user_path = find_executable (env_path, sess.sysroot,
7382 sess.sysenv);
8e13c1a1
RH
7383 }
7384 }
7385 else
7386 {
05fb3e0c 7387 user_path = find_executable (path, sess.sysroot, sess.sysenv);
8e13c1a1 7388 }
d1bcbe71
RH
7389
7390 struct stat st;
7391
7392 if (access (user_path.c_str(), X_OK) == 0
7393 && stat (user_path.c_str(), &st) == 0
7394 && S_ISREG (st.st_mode)) // see find_executable()
7395 {
7396 if (sess.verbose > 1)
b530b5b3
LB
7397 clog << _F("Expanded process(\"%s\") to process(\"%s\")",
7398 module_name.c_str(), user_path.c_str()) << endl;
d1bcbe71
RH
7399
7400 assert (location->components.size() > 0);
7401 assert (location->components[0]->functor == TOK_PROCESS);
7402 assert (location->components[0]->arg);
7403 literal_string* lit = dynamic_cast<literal_string*>(location->components[0]->arg);
7404 assert (lit);
7405
7406 // synthesize a new probe_point, with the expanded string
7407 probe_point *pp = new probe_point (*location);
05fb3e0c 7408 string user_path_tgt = path_remove_sysroot(sess, user_path);
d1bcbe71 7409 probe_point::component* ppc = new probe_point::component (TOK_PROCESS,
05fb3e0c 7410 new literal_string (user_path_tgt.c_str()));
d1bcbe71
RH
7411 ppc->tok = location->components[0]->tok; // overwrite [0] slot, pattern matched above
7412 pp->components[0] = ppc;
7413
d885563b 7414 probe* new_probe = new probe (base, pp);
d1bcbe71
RH
7415
7416 derive_probes (sess, new_probe, finished_results);
7417
7418 script_file.close();
7419 return;
7420 }
7421 }
7422 }
7423 }
7424 script_file.close();
7425 }
7426
47e226ed 7427 if (get_param (parameters, TOK_LIBRARY, user_lib)
378d78b5 7428 && user_lib.length() && ! contains_glob_chars (user_lib))
47e226ed
SC
7429 {
7430 module_name = find_executable (user_lib, sess.sysroot, sess.sysenv,
7431 "LD_LIBRARY_PATH");
7432 if (module_name.find('/') == string::npos)
7433 // We didn't find user_lib so use iterate_over_libraries
7434 module_name = user_path;
7435 }
63b4fd14 7436 else
b642c901 7437 module_name = user_path; // canonicalize it
d0a7f5a9 7438
3667d615 7439 // uretprobes aren't available everywhere
536f1261 7440 if (has_null_param(parameters, TOK_RETURN) && !sess.runtime_usermode_p())
2b69faaf 7441 {
3667d615
JS
7442 if (kernel_supports_inode_uprobes(sess) &&
7443 !kernel_supports_inode_uretprobes(sess))
dc09353a 7444 throw SEMANTIC_ERROR
3ffeaf3c 7445 (_("process return probes not available [man error::inode-uprobes]"));
2b69faaf 7446 }
3667d615 7447
f4000852
MW
7448 // There is a similar check in pass 4 (buildrun), but it is
7449 // needed here too to make sure alternatives for optional
7450 // (? or !) process probes are disposed and/or alternatives
7451 // are selected.
3667d615
JS
7452 if (!sess.runtime_usermode_p())
7453 check_process_probe_kernel_support(sess);
e34d5d13 7454
7a24d422
FCE
7455 // user-space target; we use one dwflpp instance per module name
7456 // (= program or shared library)
707bf35e 7457 dw = get_user_dw(sess, module_name);
c8959a29 7458 }
20c6c071 7459
1acfc030
JS
7460 assert(dw);
7461
e90006d7
JL
7462 unsigned results_pre = finished_results.size();
7463
5896cd05 7464 if (sess.verbose > 3)
b530b5b3 7465 clog << _F("dwarf_builder::build for %s", module_name.c_str()) << endl;
5896cd05 7466
a794dbeb
FCE
7467 string dummy_mark_name; // NB: PR10245: dummy value, need not substitute - => __
7468 if (get_param(parameters, TOK_MARK, dummy_mark_name))
f28a8c28 7469 {
51d6bda3 7470 sdt_query sdtq(base, location, *dw, filled_parameters, finished_results, user_lib);
06de3a04 7471 dw->iterate_over_modules<base_query>(&query_module, &sdtq);
e90006d7
JL
7472
7473 // We need to update modules_seen with the modules we've visited
7474 modules_seen.insert(sdtq.visited_modules.begin(),
7475 sdtq.visited_modules.end());
7476
7477 // Did we fail to find a mark?
7478 if (results_pre == finished_results.size() && !location->from_glob)
7479 {
7480 string provider;
7481 get_param(filled_parameters, TOK_PROVIDER, provider);
7482
7483 string sugs = suggest_marks(sess, modules_seen, dummy_mark_name, provider);
7484 modules_seen.clear();
7485 if (!sugs.empty())
7486 throw SEMANTIC_ERROR (_NF("no match (similar mark: %s)",
7487 "no match (similar marks: %s)",
7488 sugs.find(',') == string::npos,
7489 sugs.c_str()));
7490 }
7491
edce5b67 7492 return;
7a05f484 7493 }
20c6c071 7494
6d5d594e 7495 dwarf_query q(base, location, *dw, filled_parameters, finished_results, user_path, user_lib);
7a24d422
FCE
7496
7497 // XXX: kernel.statement.absolute is a special case that requires no
7498 // dwfl processing. This code should be in a separate builder.
7a24d422 7499 if (q.has_kernel && q.has_absolute)
37ebca01 7500 {
4baf0e53 7501 // assert guru mode for absolute probes
37ebca01
FCE
7502 if (! q.base_probe->privileged)
7503 {
dc09353a 7504 throw SEMANTIC_ERROR (_("absolute statement probe in unprivileged script; need stap -g"),
edce5b67 7505 q.base_probe->tok);
37ebca01
FCE
7506 }
7507
7508 // For kernel.statement(NUM).absolute probe points, we bypass
7509 // all the debuginfo stuff: We just wire up a
7510 // dwarf_derived_probe right here and now.
4baf0e53 7511 dwarf_derived_probe* p =
b8da0ad1
FCE
7512 new dwarf_derived_probe ("", "", 0, "kernel", "",
7513 q.statement_num_val, q.statement_num_val,
7514 q, 0);
37ebca01 7515 finished_results.push_back (p);
1a0dbc5a 7516 sess.unwindsym_modules.insert ("kernel");
37ebca01
FCE
7517 return;
7518 }
7519
06de3a04 7520 dw->iterate_over_modules<base_query>(&query_module, &q);
8f14e444 7521
44ffe90c
JL
7522 // We need to update modules_seen with the modules we've visited
7523 modules_seen.insert(q.visited_modules.begin(),
7524 q.visited_modules.end());
8f14e444
FCE
7525
7526 // PR11553 special processing: .return probes requested, but
7527 // some inlined function instances matched.
7528 unsigned i_n_r = q.inlined_non_returnable.size();
7529 unsigned results_post = finished_results.size();
7530 if (i_n_r > 0)
7531 {
7532 if ((results_pre == results_post) && (! sess.suppress_warnings)) // no matches; issue warning
7533 {
7534 string quicklist;
7535 for (set<string>::iterator it = q.inlined_non_returnable.begin();
7536 it != q.inlined_non_returnable.end();
7537 it++)
7538 {
7539 quicklist += " " + (*it);
7540 if (quicklist.size() > 80) // heuristic, don't make an overlong report line
7541 {
7542 quicklist += " ...";
7543 break;
7544 }
7545 }
c57ea854 7546
52c2652f 7547 sess.print_warning (_NF("cannot probe .return of %u inlined function %s",
b530b5b3 7548 "cannot probe .return of %u inlined functions %s",
52c2652f 7549 quicklist.size(), i_n_r, quicklist.c_str()));
8f14e444
FCE
7550 // There will be also a "no matches" semantic error generated.
7551 }
7552 if (sess.verbose > 1)
52c2652f
NMA
7553 clog << _NF("skipped .return probe of %u inlined function",
7554 "skipped .return probe of %u inlined functions", i_n_r, i_n_r) << endl;
8f14e444
FCE
7555 if ((sess.verbose > 3) || (sess.verbose > 2 && results_pre == results_post)) // issue details with high verbosity
7556 {
7557 for (set<string>::iterator it = q.inlined_non_returnable.begin();
7558 it != q.inlined_non_returnable.end();
7559 it++)
7560 clog << (*it) << " ";
7561 clog << endl;
7562 }
7563 } // i_n_r > 0
44ffe90c 7564
e90006d7 7565 // If we just failed to resolve a function/plt by name, we can suggest
ef0943df
JL
7566 // something. We only suggest things for probe points that were not
7567 // synthesized from a glob, i.e. only for 'real' probes. This is also
7568 // required because modules_seen needs to accumulate across recursive
7569 // calls for process(glob)[.library(glob)] probes.
44ffe90c 7570 string func;
ef0943df 7571 if (results_pre == results_post && !location->from_glob
44ffe90c
JL
7572 && get_param(filled_parameters, TOK_FUNCTION, func)
7573 && !func.empty())
7574 {
0508c45f 7575 string sugs = suggest_dwarf_functions(sess, modules_seen, func);
44ffe90c
JL
7576 modules_seen.clear();
7577 if (!sugs.empty())
dc09353a 7578 throw SEMANTIC_ERROR (_NF("no match (similar function: %s)",
44ffe90c
JL
7579 "no match (similar functions: %s)",
7580 sugs.find(',') == string::npos,
7581 sugs.c_str()));
7582 }
ef0943df 7583 else if (results_pre == results_post && !location->from_glob
40a70f52
JL
7584 && get_param(filled_parameters, TOK_PLT, func)
7585 && !func.empty())
7586 {
7587 string sugs = suggest_plt_functions(sess, modules_seen, func);
7588 modules_seen.clear();
7589 if (!sugs.empty())
7590 throw SEMANTIC_ERROR (_NF("no match (similar function: %s)",
7591 "no match (similar functions: %s)",
7592 sugs.find(',') == string::npos,
7593 sugs.c_str()));
7594 }
44ffe90c
JL
7595 else if (results_pre != results_post)
7596 // Something was derived so we won't need to suggest something
7597 modules_seen.clear();
5f0a03a6
JK
7598}
7599
7600symbol_table::~symbol_table()
7601{
c9efa5c9 7602 delete_map(map_by_addr);
5f0a03a6
JK
7603}
7604
7605void
2867a2a1 7606symbol_table::add_symbol(const char *name, bool weak, bool descriptor,
44ffe90c 7607 Dwarf_Addr addr, Dwarf_Addr* /*high_addr*/)
5f0a03a6 7608{
ab91b232
JK
7609#ifdef __powerpc__
7610 // Map ".sys_foo" to "sys_foo".
7611 if (name[0] == '.')
7612 name++;
7613#endif
5f0a03a6
JK
7614 func_info *fi = new func_info();
7615 fi->addr = addr;
7616 fi->name = name;
ab91b232 7617 fi->weak = weak;
2867a2a1 7618 fi->descriptor = descriptor;
5f0a03a6
JK
7619 map_by_name[fi->name] = fi;
7620 // TODO: Use a multimap in case there are multiple static
7621 // functions with the same name?
1c6b77e5 7622 map_by_addr.insert(make_pair(addr, fi));
5f0a03a6
JK
7623}
7624
7625enum info_status
7626symbol_table::read_symbols(FILE *f, const string& path)
7627{
7628 // Based on do_kernel_symbols() in runtime/staprun/symbols.c
7629 int ret;
2e67a43b
TM
7630 char *name = 0;
7631 char *mod = 0;
5f0a03a6
JK
7632 char type;
7633 unsigned long long addr;
7634 Dwarf_Addr high_addr = 0;
7635 int line = 0;
7636
62a4021d
FCE
7637#if __GLIBC__ >2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 7)
7638#define MS_FMT "%ms"
7639#else
7640#define MS_FMT "%as"
7641#endif
7642 // %ms (newer than %as) mallocs space for the string and stores its address.
7643 while ((ret = fscanf(f, "%llx %c " MS_FMT " [" MS_FMT, &addr, &type, &name, &mod)) > 0)
5f0a03a6 7644 {
2e67a43b
TM
7645 auto_free free_name(name);
7646 auto_free free_mod(mod);
5f0a03a6
JK
7647 line++;
7648 if (ret < 3)
7649 {
3d372d6b 7650 cerr << _F("Symbol table error: Line %d of symbol list from %s is not in correct format: address type name [module]\n",
b530b5b3 7651 line, path.c_str());
5f0a03a6
JK
7652 // Caller should delete symbol_table object.
7653 return info_absent;
7654 }
2e67a43b 7655 else if (ret > 3)
5f0a03a6
JK
7656 {
7657 // Modules are loaded above the kernel, so if we're getting
7658 // modules, we're done.
2e67a43b 7659 break;
5f0a03a6 7660 }
ab91b232 7661 if (type == 'T' || type == 't' || type == 'W')
2867a2a1 7662 add_symbol(name, (type == 'W'), false, (Dwarf_Addr) addr, &high_addr);
5f0a03a6
JK
7663 }
7664
1c6b77e5 7665 if (map_by_addr.size() < 1)
5f0a03a6 7666 {
3d372d6b 7667 cerr << _F("Symbol table error: %s contains no function symbols.\n",
b530b5b3 7668 path.c_str()) << endl;
5f0a03a6
JK
7669 return info_absent;
7670 }
7671 return info_present;
7672}
7673
7674// NB: This currently unused. We use get_from_elf() instead because
7675// that gives us raw addresses -- which we need for modules -- whereas
7676// nm provides the address relative to the beginning of the section.
7677enum info_status
83ca3872 7678symbol_table::read_from_elf_file(const string &path,
2713ea24 7679 systemtap_session &sess)
5f0a03a6 7680{
58502ae4
JS
7681 vector<string> cmd;
7682 cmd.push_back("/usr/bin/nm");
7683 cmd.push_back("-n");
7684 cmd.push_back("--defined-only");
7685 cmd.push_back("path");
7686
5f0a03a6 7687 FILE *f;
58502ae4
JS
7688 int child_fd;
7689 pid_t child = stap_spawn_piped(sess.verbose, cmd, NULL, &child_fd);
7690 if (child <= 0 || !(f = fdopen(child_fd, "r")))
5f0a03a6 7691 {
58502ae4 7692 // nm failures are detected by stap_waitpid
3d372d6b 7693 cerr << _F("Internal error reading symbol table from %s -- %s\n",
b530b5b3 7694 path.c_str(), strerror(errno));
5f0a03a6
JK
7695 return info_absent;
7696 }
7697 enum info_status status = read_symbols(f, path);
58502ae4 7698 if (fclose(f) || stap_waitpid(sess.verbose, child))
5f0a03a6 7699 {
2713ea24
CM
7700 if (status == info_present)
7701 sess.print_warning("nm cannot read symbol table from " + path);
5f0a03a6
JK
7702 return info_absent;
7703 }
7704 return status;
7705}
7706
7707enum info_status
83ca3872 7708symbol_table::read_from_text_file(const string& path,
2713ea24 7709 systemtap_session &sess)
5f0a03a6
JK
7710{
7711 FILE *f = fopen(path.c_str(), "r");
7712 if (!f)
7713 {
2713ea24 7714 sess.print_warning("cannot read symbol table from " + path + " -- " + strerror(errno));
5f0a03a6
JK
7715 return info_absent;
7716 }
7717 enum info_status status = read_symbols(f, path);
7718 (void) fclose(f);
7719 return status;
7720}
7721
46f7b6be 7722void
f98c6346 7723symbol_table::prepare_section_rejection(Dwfl_Module *mod __attribute__ ((unused)))
46f7b6be
JK
7724{
7725#ifdef __powerpc__
7726 /*
7727 * The .opd section contains function descriptors that can look
7728 * just like function entry points. For example, there's a function
7729 * descriptor called "do_exit" that links to the entry point ".do_exit".
7730 * Reject all symbols in .opd.
7731 */
7732 opd_section = SHN_UNDEF;
7733 Dwarf_Addr bias;
7734 Elf* elf = (dwarf_getelf (dwfl_module_getdwarf (mod, &bias))
7735 ?: dwfl_module_getelf (mod, &bias));
7736 Elf_Scn* scn = 0;
7737 size_t shstrndx;
7738
7739 if (!elf)
7740 return;
fcc30d6d 7741 if (elf_getshdrstrndx (elf, &shstrndx) != 0)
46f7b6be
JK
7742 return;
7743 while ((scn = elf_nextscn(elf, scn)) != NULL)
7744 {
7745 GElf_Shdr shdr_mem;
7746 GElf_Shdr *shdr = gelf_getshdr(scn, &shdr_mem);
7747 if (!shdr)
7748 continue;
7749 const char *name = elf_strptr(elf, shstrndx, shdr->sh_name);
7750 if (!strcmp(name, ".opd"))
7751 {
7752 opd_section = elf_ndxscn(scn);
7753 return;
7754 }
7755 }
7756#endif
7757}
7758
7759bool
7760symbol_table::reject_section(GElf_Word section)
7761{
7762 if (section == SHN_UNDEF)
7763 return true;
7764#ifdef __powerpc__
7765 if (section == opd_section)
7766 return true;
7767#endif
7768 return false;
7769}
7770
5f0a03a6
JK
7771enum info_status
7772symbol_table::get_from_elf()
7773{
7774 Dwarf_Addr high_addr = 0;
7775 Dwfl_Module *mod = mod_info->mod;
7776 int syments = dwfl_module_getsymtab(mod);
7777 assert(syments);
46f7b6be 7778 prepare_section_rejection(mod);
5f0a03a6
JK
7779 for (int i = 1; i < syments; ++i)
7780 {
7781 GElf_Sym sym;
ab91b232 7782 GElf_Word section;
cba67d8f
MW
7783 const char *name;
7784 GElf_Addr addr;
7785 bool reject;
7786
7787/* Note that dwfl_module_getsym does adjust the sym.st_value but doesn't
7788 try to resolve it to a function address. dwfl_module_getsym_info leaves
7789 the st_value in tact (no adjustment applied) and returns the fully
7790 resolved address separately. In that case we can simply reject the
7791 symbol if it is SHN_UNDEF and don't need to call reject_section which
7792 does extra checks to see whether the address fall in an architecture
7793 specific descriptor table (which will never be the case when using the
7794 new dwfl_module_getsym_info). dwfl_module_getsym will only provide us
7795 with the (adjusted) st_value of the symbol, which might point into a
7796 function descriptor table. So in that case we still have to call
7797 reject_section. */
7798#if _ELFUTILS_PREREQ (0, 158)
7799 name = dwfl_module_getsym_info (mod, i, &sym, &addr, &section,
7800 NULL, NULL);
7801 reject = section == SHN_UNDEF;
7802#else
7803 name = dwfl_module_getsym (mod, i, &sym, &section);
b23098cd 7804 addr = sym.st_value;
cba67d8f
MW
7805 reject = reject_section(section);
7806#endif
7807
2867a2a1 7808 if (name && GELF_ST_TYPE(sym.st_info) == STT_FUNC)
ab91b232 7809 add_symbol(name, (GELF_ST_BIND(sym.st_info) == STB_WEAK),
cba67d8f 7810 reject, addr, &high_addr);
5f0a03a6
JK
7811 }
7812 return info_present;
7813}
7814
5f0a03a6
JK
7815func_info *
7816symbol_table::get_func_containing_address(Dwarf_Addr addr)
7817{
1c6b77e5
JS
7818 iterator_t iter = map_by_addr.upper_bound(addr);
7819 if (iter == map_by_addr.begin())
5f0a03a6 7820 return NULL;
2e67a43b 7821 else
1c6b77e5 7822 return (--iter)->second;
5f0a03a6
JK
7823}
7824
3d372d6b
SC
7825func_info *
7826symbol_table::get_first_func()
7827{
7828 iterator_t iter = map_by_addr.begin();
7829 return (iter)->second;
7830}
7831
5f0a03a6
JK
7832func_info *
7833symbol_table::lookup_symbol(const string& name)
7834{
7835 map<string, func_info*>::iterator i = map_by_name.find(name);
7836 if (i == map_by_name.end())
7837 return NULL;
7838 return i->second;
7839}
7840
7841Dwarf_Addr
7842symbol_table::lookup_symbol_address(const string& name)
7843{
7844 func_info *fi = lookup_symbol(name);
7845 if (fi)
7846 return fi->addr;
7847 return 0;
7848}
7849
ab91b232
JK
7850// This is the kernel symbol table. The kernel macro cond_syscall creates
7851// a weak symbol for each system call and maps it to sys_ni_syscall.
7852// For system calls not implemented elsewhere, this weak symbol shows up
7853// in the kernel symbol table. Following the precedent of dwarfful stap,
7854// we refuse to consider such symbols. Here we delete them from our
7855// symbol table.
7856// TODO: Consider generalizing this and/or making it part of blacklist
7857// processing.
7858void
7859symbol_table::purge_syscall_stubs()
7860{
7861 Dwarf_Addr stub_addr = lookup_symbol_address("sys_ni_syscall");
7862 if (stub_addr == 0)
7863 return;
1c6b77e5 7864 range_t purge_range = map_by_addr.equal_range(stub_addr);
2e67a43b
TM
7865 for (iterator_t iter = purge_range.first;
7866 iter != purge_range.second;
1c6b77e5 7867 )
ab91b232 7868 {
1c6b77e5 7869 func_info *fi = iter->second;
2e67a43b 7870 if (fi->weak && fi->name != "sys_ni_syscall")
ab91b232 7871 {
2e67a43b 7872 map_by_name.erase(fi->name);
1c6b77e5 7873 map_by_addr.erase(iter++);
2e67a43b 7874 delete fi;
2e67a43b 7875 }
1c6b77e5
JS
7876 else
7877 iter++;
ab91b232
JK
7878 }
7879}
7880
5f0a03a6 7881void
d906ab9d 7882module_info::get_symtab(base_query *q)
5f0a03a6 7883{
1c6b77e5
JS
7884 if (symtab_status != info_unknown)
7885 return;
7886
5f0a03a6
JK
7887 sym_table = new symbol_table(this);
7888 if (!elf_path.empty())
7889 {
5f0a03a6
JK
7890 symtab_status = sym_table->get_from_elf();
7891 }
7892 else
7893 {
7894 assert(name == TOK_KERNEL);
ab3ed72d
DS
7895 symtab_status = info_absent;
7896 cerr << _("Error: Cannot find vmlinux.") << endl;;
5f0a03a6
JK
7897 }
7898 if (symtab_status == info_absent)
7899 {
7900 delete sym_table;
7901 sym_table = NULL;
7902 return;
7903 }
7904
ab91b232
JK
7905 if (name == TOK_KERNEL)
7906 sym_table->purge_syscall_stubs();
5f0a03a6
JK
7907}
7908
1c6b77e5
JS
7909// update_symtab reconciles data between the elf symbol table and the dwarf
7910// function enumeration. It updates the symbol table entries with the dwarf
7911// die that describes the function, which also signals to query_module_symtab
7912// that a statement probe isn't needed. In return, it also adds aliases to the
7913// function table for names that share the same addr/die.
7914void
7915module_info::update_symtab(cu_function_cache_t *funcs)
7916{
7917 if (!sym_table)
7918 return;
7919
7920 cu_function_cache_t new_funcs;
7921
7922 for (cu_function_cache_t::iterator func = funcs->begin();
7923 func != funcs->end(); func++)
7924 {
7925 // optimization: inlines will never be in the symbol table
7926 if (dwarf_func_inline(&func->second) != 0)
6e6d9a3b
JL
7927 {
7928 inlined_funcs.insert(func->first);
7929 continue;
7930 }
1c6b77e5 7931
1ffb8bd1
JS
7932 // XXX We may want to make additional efforts to match mangled elf names
7933 // to dwarf too. MIPS_linkage_name can help, but that's sometimes
7934 // missing, so we may also need to try matching by address. See also the
7935 // notes about _Z in dwflpp::iterate_over_functions().
7936
1c6b77e5
JS
7937 func_info *fi = sym_table->lookup_symbol(func->first);
7938 if (!fi)
7939 continue;
7940
7941 // iterate over all functions at the same address
7942 symbol_table::range_t er = sym_table->map_by_addr.equal_range(fi->addr);
7943 for (symbol_table::iterator_t it = er.first; it != er.second; ++it)
7944 {
7945 // update this function with the dwarf die
7946 it->second->die = func->second;
7947
7948 // if this function is a new alias, then
7949 // save it to merge into the function cache
7950 if (it->second != fi)
b7478964 7951 new_funcs.insert(make_pair(it->second->name, it->second->die));
1c6b77e5
JS
7952 }
7953 }
7954
7955 // add all discovered aliases back into the function cache
7956 // NB: this won't replace any names that dwarf may have already found
7957 funcs->insert(new_funcs.begin(), new_funcs.end());
7958}
7959
5f0a03a6
JK
7960module_info::~module_info()
7961{
7962 if (sym_table)
7963 delete sym_table;
b55bc428
FCE
7964}
7965
935447c8 7966// ------------------------------------------------------------------------
888af770 7967// user-space probes
935447c8
DS
7968// ------------------------------------------------------------------------
7969
935447c8 7970
888af770 7971struct uprobe_derived_probe_group: public generic_dpg<uprobe_derived_probe>
935447c8 7972{
89ba3085
FCE
7973private:
7974 string make_pbm_key (uprobe_derived_probe* p) {
cfcab6c7 7975 return p->path + "|" + p->module + "|" + p->section + "|" + lex_cast(p->pid);
89ba3085
FCE
7976 }
7977
cfcab6c7
JS
7978 void emit_module_maxuprobes (systemtap_session& s);
7979
2b69faaf
JS
7980 // Using our own utrace-based uprobes
7981 void emit_module_utrace_decls (systemtap_session& s);
7982 void emit_module_utrace_init (systemtap_session& s);
7983 void emit_module_utrace_exit (systemtap_session& s);
7984
7985 // Using the upstream inode-based uprobes
7986 void emit_module_inode_decls (systemtap_session& s);
7987 void emit_module_inode_init (systemtap_session& s);
7988 void emit_module_inode_exit (systemtap_session& s);
7989
3a894f7e
JS
7990 // Using the dyninst backend (via stapdyn)
7991 void emit_module_dyninst_decls (systemtap_session& s);
7992 void emit_module_dyninst_init (systemtap_session& s);
7993 void emit_module_dyninst_exit (systemtap_session& s);
7994
935447c8 7995public:
888af770 7996 void emit_module_decls (systemtap_session& s);
935447c8
DS
7997 void emit_module_init (systemtap_session& s);
7998 void emit_module_exit (systemtap_session& s);
7999};
8000
8001
888af770
FCE
8002void
8003uprobe_derived_probe::join_group (systemtap_session& s)
8004{
8005 if (! s.uprobe_derived_probes)
8006 s.uprobe_derived_probes = new uprobe_derived_probe_group ();
8007 s.uprobe_derived_probes->enroll (this);
f31a77f5
DS
8008 if (s.runtime_usermode_p())
8009 enable_dynprobes(s);
8010 else
4441e344 8011 enable_task_finder(s);
a96d1db0 8012
8a03658e 8013 // Ask buildrun.cxx to build extra module if needed, and
d3e959b0
DS
8014 // signal staprun to load that module. If we're using the builtin
8015 // inode-uprobes, we still need to know that it is required.
8a03658e 8016 s.need_uprobes = true;
a96d1db0
DN
8017}
8018
888af770 8019
c0f84e7b
SC
8020void
8021uprobe_derived_probe::getargs(std::list<std::string> &arg_set) const
8022{
8023 dwarf_derived_probe::getargs(arg_set);
8024 arg_set.insert(arg_set.end(), args.begin(), args.end());
8025}
8026
8027
8028void
8029uprobe_derived_probe::saveargs(int nargs)
8030{
8031 for (int i = 1; i <= nargs; i++)
8032 args.push_back("$arg" + lex_cast (i) + ":long");
8033}
8034
8035
2865d17a 8036void
42e38653 8037uprobe_derived_probe::emit_privilege_assertion (translator_output* o)
2865d17a
DB
8038{
8039 // These probes are allowed for unprivileged users, but only in the
8040 // context of processes which they own.
8041 emit_process_owner_assertion (o);
8042}
8043
8044
888af770 8045struct uprobe_builder: public derived_probe_builder
a96d1db0 8046{
888af770 8047 uprobe_builder() {}
2b69faaf 8048 virtual void build(systemtap_session & sess,
a96d1db0
DN
8049 probe * base,
8050 probe_point * location,
86bf665e 8051 literal_map_t const & parameters,
a96d1db0
DN
8052 vector<derived_probe *> & finished_results)
8053 {
888af770 8054 int64_t process, address;
a96d1db0 8055
2b69faaf 8056 if (kernel_supports_inode_uprobes(sess))
dc09353a 8057 throw SEMANTIC_ERROR (_("absolute process probes not available [man error::inode-uprobes]"));
2b69faaf 8058
888af770 8059 bool b1 = get_param (parameters, TOK_PROCESS, process);
ced347a9 8060 (void) b1;
888af770 8061 bool b2 = get_param (parameters, TOK_STATEMENT, address);
ced347a9 8062 (void) b2;
888af770
FCE
8063 bool rr = has_null_param (parameters, TOK_RETURN);
8064 assert (b1 && b2); // by pattern_root construction
a96d1db0 8065
0973d815 8066 finished_results.push_back(new uprobe_derived_probe(base, location, process, address, rr));
a96d1db0
DN
8067 }
8068};
8069
8070
8071void
cfcab6c7 8072uprobe_derived_probe_group::emit_module_maxuprobes (systemtap_session& s)
a96d1db0 8073{
43241c44
FCE
8074 // We'll probably need at least this many:
8075 unsigned minuprobes = probes.size();
8076 // .. but we don't want so many that .bss is inflated (PR10507):
8077 unsigned uprobesize = 64;
8078 unsigned maxuprobesmem = 10*1024*1024; // 10 MB
8079 unsigned maxuprobes = maxuprobesmem / uprobesize;
8080
aaf7ffe8
FCE
8081 // Let's choose a value on the geometric middle. This should end up
8082 // between minuprobes and maxuprobes. It's OK if this number turns
8083 // out to be < minuprobes or > maxuprobes. At worst, we get a
8084 // run-time error of one kind (too few: missed uprobe registrations)
8085 // or another (too many: vmalloc errors at module load time).
8086 unsigned default_maxuprobes = (unsigned)sqrt((double)minuprobes * (double)maxuprobes);
43241c44 8087
6d0f3f0c 8088 s.op->newline() << "#ifndef MAXUPROBES";
43241c44 8089 s.op->newline() << "#define MAXUPROBES " << default_maxuprobes;
6d0f3f0c 8090 s.op->newline() << "#endif";
cfcab6c7
JS
8091}
8092
8093
8094void
8095uprobe_derived_probe_group::emit_module_utrace_decls (systemtap_session& s)
8096{
8097 if (probes.empty()) return;
8098 s.op->newline() << "/* ---- utrace uprobes ---- */";
8099 // If uprobes isn't in the kernel, pull it in from the runtime.
8100
8101 s.op->newline() << "#if defined(CONFIG_UPROBES) || defined(CONFIG_UPROBES_MODULE)";
8102 s.op->newline() << "#include <linux/uprobes.h>";
8103 s.op->newline() << "#else";
2ba1736a 8104 s.op->newline() << "#include \"linux/uprobes/uprobes.h\"";
cfcab6c7
JS
8105 s.op->newline() << "#endif";
8106 s.op->newline() << "#ifndef UPROBES_API_VERSION";
8107 s.op->newline() << "#define UPROBES_API_VERSION 1";
8108 s.op->newline() << "#endif";
8109
8110 emit_module_maxuprobes (s);
a96d1db0 8111
cc52276b 8112 // Forward decls
2ba1736a 8113 s.op->newline() << "#include \"linux/uprobes-common.h\"";
cc52276b 8114
5e112f92
FCE
8115 // In .bss, the shared pool of uprobe/uretprobe structs. These are
8116 // too big to embed in the initialized .data stap_uprobe_spec array.
cc52276b
WC
8117 // XXX: consider a slab cache or somesuch for stap_uprobes
8118 s.op->newline() << "static struct stap_uprobe stap_uprobes [MAXUPROBES];";
6b378b7c 8119 s.op->newline() << "static DEFINE_MUTEX(stap_uprobes_lock);"; // protects against concurrent registration/unregistration
a96d1db0 8120
89ba3085
FCE
8121 s.op->assert_0_indent();
8122
89ba3085
FCE
8123 // Assign task-finder numbers as we build up the stap_uprobe_tf table.
8124 // This means we process probes[] in two passes.
8125 map <string,unsigned> module_index;
8126 unsigned module_index_ctr = 0;
8127
cc52276b
WC
8128 // not const since embedded task_finder_target struct changes
8129 s.op->newline() << "static struct stap_uprobe_tf stap_uprobe_finders[] = {";
89ba3085
FCE
8130 s.op->indent(1);
8131 for (unsigned i=0; i<probes.size(); i++)
8132 {
8133 uprobe_derived_probe *p = probes[i];
8134 string pbmkey = make_pbm_key (p);
8135 if (module_index.find (pbmkey) == module_index.end())
8136 {
8137 module_index[pbmkey] = module_index_ctr++;
8138
8139 s.op->newline() << "{";
8140 // NB: it's essential that make_pbm_key() use all of and
8141 // only the same fields as we're about to emit.
8142 s.op->line() << " .finder={";
1af100fc 8143 s.op->line() << " .purpose=\"uprobes\",";
89ba3085 8144 if (p->pid != 0)
68910c97
JK
8145 s.op->line() << " .pid=" << p->pid << ",";
8146
8147 if (p->section == "") // .statement(addr).absolute
8148 s.op->line() << " .callback=&stap_uprobe_process_found,";
89ba3085
FCE
8149 else if (p->section == ".absolute") // proxy for ET_EXEC -> exec()'d program
8150 {
8151 s.op->line() << " .procname=" << lex_cast_qstring(p->module) << ",";
8152 s.op->line() << " .callback=&stap_uprobe_process_found,";
8153 }
68910c97 8154 else if (p->section != ".absolute") // ET_DYN
89ba3085 8155 {
4ad95bbc
SC
8156 if (p->has_library)
8157 s.op->line() << " .procname=\"" << p->path << "\", ";
89ba3085
FCE
8158 s.op->line() << " .mmap_callback=&stap_uprobe_mmap_found, ";
8159 s.op->line() << " .munmap_callback=&stap_uprobe_munmap_found, ";
19d91f6c 8160 s.op->line() << " .callback=&stap_uprobe_process_munmap,";
89ba3085 8161 }
89ba3085 8162 s.op->line() << " },";
68910c97
JK
8163 if (p->module != "")
8164 s.op->line() << " .pathname=" << lex_cast_qstring(p->module) << ", ";
89ba3085
FCE
8165 s.op->line() << " },";
8166 }
c57ea854 8167 else
822a6a3d 8168 { } // skip it in this pass, already have a suitable stap_uprobe_tf slot for it.
89ba3085
FCE
8169 }
8170 s.op->newline(-1) << "};";
8171
8172 s.op->assert_0_indent();
8173
3689db05
SC
8174 unsigned pci;
8175 for (pci=0; pci<probes.size(); pci++)
8176 {
8177 // List of perf counters used by each probe
8178 // This list is an index into struct stap_perf_probe,
8179 uprobe_derived_probe *p = probes[pci];
698de6cc 8180 std::set<derived_probe*>::iterator pcii;
3689db05
SC
8181 s.op->newline() << "long perf_counters_" + lex_cast(pci) + "[] = {";
8182 for (pcii = p->perf_counter_refs.begin();
8183 pcii != p->perf_counter_refs.end(); pcii++)
8184 {
8185 map<string, pair<string,derived_probe*> >::iterator it;
8186 unsigned i = 0;
8187 // Find the associated perf.counter probe
8188 for (it=s.perf_counters.begin() ;
8189 it != s.perf_counters.end(); it++, i++)
8190 if ((*it).second.second == (*pcii))
8191 break;
8192 s.op->line() << lex_cast(i) << ", ";
8193 }
8194 s.op->newline() << "};";
8195 }
8196
cc52276b
WC
8197 // NB: read-only structure
8198 s.op->newline() << "static const struct stap_uprobe_spec stap_uprobe_specs [] = {";
a96d1db0 8199 s.op->indent(1);
888af770
FCE
8200 for (unsigned i =0; i<probes.size(); i++)
8201 {
8202 uprobe_derived_probe* p = probes[i];
8203 s.op->newline() << "{";
89ba3085
FCE
8204 string key = make_pbm_key (p);
8205 unsigned value = module_index[key];
759e1d76
FCE
8206 if (value != 0)
8207 s.op->line() << " .tfi=" << value << ",";
6b66b9f7 8208 s.op->line() << " .address=(unsigned long)0x" << hex << p->addr << dec << "ULL,";
faea5e16 8209 s.op->line() << " .probe=" << common_probe_init (p) << ",";
4ddb6dd0 8210
038c38c6 8211 if (p->sdt_semaphore_addr != 0)
63b4fd14 8212 s.op->line() << " .sdt_sem_offset=(unsigned long)0x"
038c38c6 8213 << hex << p->sdt_semaphore_addr << dec << "ULL,";
4ddb6dd0 8214
0d049a1d 8215 // XXX: don't bother emit if array is empty
3689db05
SC
8216 s.op->line() << " .perf_counters_dim=ARRAY_SIZE(perf_counters_" << lex_cast(i) << "),";
8217 // List of perf counters used by a probe from above
0d049a1d 8218 s.op->line() << " .perf_counters=perf_counters_" + lex_cast(i) + ",";
3689db05 8219
4ddb6dd0
JS
8220 if (p->has_return)
8221 s.op->line() << " .return_p=1,";
888af770
FCE
8222 s.op->line() << " },";
8223 }
8224 s.op->newline(-1) << "};";
a96d1db0 8225
89ba3085
FCE
8226 s.op->assert_0_indent();
8227
48e685da 8228 s.op->newline() << "static void enter_uprobe_probe (struct uprobe *inst, struct pt_regs *regs) {";
888af770 8229 s.op->newline(1) << "struct stap_uprobe *sup = container_of(inst, struct stap_uprobe, up);";
89ba3085 8230 s.op->newline() << "const struct stap_uprobe_spec *sups = &stap_uprobe_specs [sup->spec_index];";
71db462b 8231 common_probe_entryfn_prologue (s, "STAP_SESSION_RUNNING", "sups->probe",
cda141c2 8232 "stp_probe_type_uprobe");
0e090c74 8233 s.op->newline() << "if (sup->spec_index < 0 || "
6e895029
DS
8234 << "sup->spec_index >= " << probes.size() << ") {";
8235 s.op->newline(1) << "_stp_error (\"bad spec_index %d (max " << probes.size()
0e090c74 8236 << "): %s\", sup->spec_index, c->probe_point);";
6e895029
DS
8237 s.op->newline() << "goto probe_epilogue;";
8238 s.op->newline(-1) << "}";
d9aed31e 8239 s.op->newline() << "c->uregs = regs;";
e04b5d74 8240 s.op->newline() << "c->user_mode_p = 1;";
6415ddde
MW
8241
8242 // Make it look like the IP is set as it would in the actual user
8243 // task when calling real probe handler. Reset IP regs on return, so
8244 // we don't confuse uprobes. PR10458
8245 s.op->newline() << "{";
8246 s.op->indent(1);
d9aed31e 8247 s.op->newline() << "unsigned long uprobes_ip = REG_IP(c->uregs);";
259d54c0 8248 s.op->newline() << "SET_REG_IP(regs, inst->vaddr);";
26e63673 8249 s.op->newline() << "(*sups->probe->ph) (c);";
259d54c0 8250 s.op->newline() << "SET_REG_IP(regs, uprobes_ip);";
6415ddde
MW
8251 s.op->newline(-1) << "}";
8252
f887a8c9 8253 common_probe_entryfn_epilogue (s, true);
888af770 8254 s.op->newline(-1) << "}";
a96d1db0 8255
48e685da 8256 s.op->newline() << "static void enter_uretprobe_probe (struct uretprobe_instance *inst, struct pt_regs *regs) {";
888af770 8257 s.op->newline(1) << "struct stap_uprobe *sup = container_of(inst->rp, struct stap_uprobe, urp);";
89ba3085 8258 s.op->newline() << "const struct stap_uprobe_spec *sups = &stap_uprobe_specs [sup->spec_index];";
71db462b 8259 common_probe_entryfn_prologue (s, "STAP_SESSION_RUNNING", "sups->probe",
cda141c2 8260 "stp_probe_type_uretprobe");
6dceb5c9 8261 s.op->newline() << "c->ips.ri = inst;";
0e090c74 8262 s.op->newline() << "if (sup->spec_index < 0 || "
0d5561a5
DS
8263 << "sup->spec_index >= " << probes.size() << ") {";
8264 s.op->newline(1) << "_stp_error (\"bad spec_index %d (max " << probes.size()
0e090c74 8265 << "): %s\", sup->spec_index, c->probe_point);";
0d5561a5
DS
8266 s.op->newline() << "goto probe_epilogue;";
8267 s.op->newline(-1) << "}";
8268
d9aed31e 8269 s.op->newline() << "c->uregs = regs;";
e04b5d74 8270 s.op->newline() << "c->user_mode_p = 1;";
6415ddde
MW
8271
8272 // Make it look like the IP is set as it would in the actual user
8273 // task when calling real probe handler. Reset IP regs on return, so
8274 // we don't confuse uprobes. PR10458
8275 s.op->newline() << "{";
8276 s.op->indent(1);
d9aed31e 8277 s.op->newline() << "unsigned long uprobes_ip = REG_IP(c->uregs);";
5e562a69 8278 s.op->newline() << "SET_REG_IP(regs, inst->ret_addr);";
26e63673 8279 s.op->newline() << "(*sups->probe->ph) (c);";
259d54c0 8280 s.op->newline() << "SET_REG_IP(regs, uprobes_ip);";
6415ddde
MW
8281 s.op->newline(-1) << "}";
8282
f887a8c9 8283 common_probe_entryfn_epilogue (s, true);
a96d1db0
DN
8284 s.op->newline(-1) << "}";
8285
89ba3085 8286 s.op->newline();
2ba1736a 8287 s.op->newline() << "#include \"linux/uprobes-common.c\"";
6d0f3f0c 8288 s.op->newline();
888af770 8289}
935447c8
DS
8290
8291
888af770 8292void
2b69faaf 8293uprobe_derived_probe_group::emit_module_utrace_init (systemtap_session& s)
935447c8 8294{
888af770 8295 if (probes.empty()) return;
935447c8 8296
2b69faaf 8297 s.op->newline() << "/* ---- utrace uprobes ---- */";
935447c8 8298
01b05e2e 8299 s.op->newline() << "for (j=0; j<MAXUPROBES; j++) {";
5e112f92
FCE
8300 s.op->newline(1) << "struct stap_uprobe *sup = & stap_uprobes[j];";
8301 s.op->newline() << "sup->spec_index = -1;"; // free slot
80b4ad8b
FCE
8302 // NB: we assume the rest of the struct (specificaly, sup->up) is
8303 // initialized to zero. This is so that we can use
8304 // sup->up->kdata = NULL for "really free!" PR 6829.
5e112f92
FCE
8305 s.op->newline(-1) << "}";
8306 s.op->newline() << "mutex_init (& stap_uprobes_lock);";
935447c8 8307
89ba3085
FCE
8308 // Set up the task_finders
8309 s.op->newline() << "for (i=0; i<sizeof(stap_uprobe_finders)/sizeof(stap_uprobe_finders[0]); i++) {";
8310 s.op->newline(1) << "struct stap_uprobe_tf *stf = & stap_uprobe_finders[i];";
c57ea854 8311 s.op->newline() << "probe_point = stf->pathname;"; // for error messages; XXX: would prefer pp() or something better
89ba3085 8312 s.op->newline() << "rc = stap_register_task_finder_target (& stf->finder);";
935447c8 8313
5e112f92
FCE
8314 // NB: if (rc), there is no need (XXX: nor any way) to clean up any
8315 // finders already registered, since mere registration does not
8316 // cause any utrace or memory allocation actions. That happens only
8317 // later, once the task finder engine starts running. So, for a
8318 // partial initialization requiring unwind, we need do nothing.
8319 s.op->newline() << "if (rc) break;";
a7a68293 8320
888af770
FCE
8321 s.op->newline(-1) << "}";
8322}
d0ea46ce 8323
d0a7f5a9 8324
888af770 8325void
2b69faaf 8326uprobe_derived_probe_group::emit_module_utrace_exit (systemtap_session& s)
888af770
FCE
8327{
8328 if (probes.empty()) return;
2b69faaf 8329 s.op->newline() << "/* ---- utrace uprobes ---- */";
e56e51c9 8330
6d0f3f0c
FCE
8331 // NB: there is no stap_unregister_task_finder_target call;
8332 // important stuff like utrace cleanups are done by
d41d451c
FCE
8333 // __stp_task_finder_cleanup() via stap_stop_task_finder().
8334 //
8335 // This function blocks until all callbacks are completed, so there
8336 // is supposed to be no possibility of any registration-related code starting
8337 // to run in parallel with our shutdown here. So we don't need to protect the
8338 // stap_uprobes[] array with the mutex.
d0a7f5a9 8339
01b05e2e 8340 s.op->newline() << "for (j=0; j<MAXUPROBES; j++) {";
5e112f92 8341 s.op->newline(1) << "struct stap_uprobe *sup = & stap_uprobes[j];";
89ba3085 8342 s.op->newline() << "const struct stap_uprobe_spec *sups = &stap_uprobe_specs [sup->spec_index];";
6d0f3f0c 8343 s.op->newline() << "if (sup->spec_index < 0) continue;"; // free slot
3568f1dd 8344
8faa1fc5 8345 // PR10655: decrement that ENABLED semaphore
c116c31b 8346 s.op->newline() << "if (sup->sdt_sem_address) {";
8faa1fc5
FCE
8347 s.op->newline(1) << "unsigned short sdt_semaphore;"; // NB: fixed size
8348 s.op->newline() << "pid_t pid = (sups->return_p ? sup->urp.u.pid : sup->up.pid);";
8349 s.op->newline() << "struct task_struct *tsk;";
8350 s.op->newline() << "rcu_read_lock();";
6846cfc8 8351
86229a55
DS
8352 // Do a pid->task_struct* lookup. For 2.6.24+, this code assumes
8353 // that the pid is always in the global namespace, not in any
8354 // private namespace.
8faa1fc5 8355 s.op->newline() << "#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,24)";
86229a55
DS
8356 // We'd like to call find_task_by_pid_ns() here, but it isn't
8357 // exported. So, we call what it calls...
8358 s.op->newline() << " tsk = pid_task(find_pid_ns(pid, &init_pid_ns), PIDTYPE_PID);";
8faa1fc5
FCE
8359 s.op->newline() << "#else";
8360 s.op->newline() << " tsk = find_task_by_pid (pid);";
8361 s.op->newline() << "#endif /* 2.6.24 */";
8faa1fc5
FCE
8362
8363 s.op->newline() << "if (tsk) {"; // just in case the thing exited while we weren't watching
3c5b8e2b 8364 s.op->newline(1) << "if (__access_process_vm_noflush(tsk, sup->sdt_sem_address, &sdt_semaphore, sizeof(sdt_semaphore), 0)) {";
63b4fd14 8365 s.op->newline(1) << "sdt_semaphore --;";
903b9fcd 8366 s.op->newline() << "#ifdef DEBUG_UPROBES";
c116c31b 8367 s.op->newline() << "_stp_dbug (__FUNCTION__,__LINE__, \"-semaphore %#x @ %#lx\\n\", sdt_semaphore, sup->sdt_sem_address);";
903b9fcd 8368 s.op->newline() << "#endif";
3c5b8e2b 8369 s.op->newline() << "__access_process_vm_noflush(tsk, sup->sdt_sem_address, &sdt_semaphore, sizeof(sdt_semaphore), 1);";
93c84191 8370 s.op->newline(-1) << "}";
8faa1fc5
FCE
8371 // XXX: need to analyze possibility of race condition
8372 s.op->newline(-1) << "}";
8373 s.op->newline() << "rcu_read_unlock();";
8374 s.op->newline(-1) << "}";
6846cfc8 8375
3568f1dd
FCE
8376 s.op->newline() << "if (sups->return_p) {";
8377 s.op->newline(1) << "#ifdef DEBUG_UPROBES";
89ba3085 8378 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 8379 s.op->newline() << "#endif";
80b4ad8b
FCE
8380 // NB: PR6829 does not change that we still need to unregister at
8381 // *this* time -- when the script as a whole exits.
3568f1dd
FCE
8382 s.op->newline() << "unregister_uretprobe (& sup->urp);";
8383 s.op->newline(-1) << "} else {";
8384 s.op->newline(1) << "#ifdef DEBUG_UPROBES";
8faa1fc5 8385 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
8386 s.op->newline() << "#endif";
8387 s.op->newline() << "unregister_uprobe (& sup->up);";
8388 s.op->newline(-1) << "}";
935447c8 8389
6d0f3f0c 8390 s.op->newline() << "sup->spec_index = -1;";
935447c8 8391
3568f1dd
FCE
8392 // XXX: uprobe missed counts?
8393
6d0f3f0c 8394 s.op->newline(-1) << "}";
935447c8 8395
5e112f92 8396 s.op->newline() << "mutex_destroy (& stap_uprobes_lock);";
935447c8
DS
8397}
8398
2b69faaf
JS
8399
8400void
8401uprobe_derived_probe_group::emit_module_inode_decls (systemtap_session& s)
8402{
8403 if (probes.empty()) return;
8404 s.op->newline() << "/* ---- inode uprobes ---- */";
cfcab6c7 8405 emit_module_maxuprobes (s);
2ba1736a 8406 s.op->newline() << "#include \"linux/uprobes-inode.c\"";
2b69faaf
JS
8407
8408 // Write the probe handler.
79af55c3
JS
8409 s.op->newline() << "static int stapiu_probe_handler "
8410 << "(struct stapiu_consumer *sup, struct pt_regs *regs) {";
8411 s.op->newline(1);
2dbbd473
JS
8412
8413 // Since we're sharing the entry function, we have to dynamically choose the probe_type
8414 string probe_type = "(sup->return_p ? stp_probe_type_uretprobe : stp_probe_type_uprobe)";
71db462b 8415 common_probe_entryfn_prologue (s, "STAP_SESSION_RUNNING", "sup->probe",
2dbbd473
JS
8416 probe_type);
8417
3bff6634 8418 s.op->newline() << "c->uregs = regs;";
e04b5d74 8419 s.op->newline() << "c->user_mode_p = 1;";
497cfcb2 8420 // NB: IP is already set by stapiu_probe_prehandler in uprobes-inode.c
2b69faaf 8421 s.op->newline() << "(*sup->probe->ph) (c);";
39e63bae 8422
f887a8c9 8423 common_probe_entryfn_epilogue (s, true);
2b69faaf
JS
8424 s.op->newline() << "return 0;";
8425 s.op->newline(-1) << "}";
8426 s.op->assert_0_indent();
8427
8428 // Index of all the modules for which we need inodes.
8429 map<string, unsigned> module_index;
8430 unsigned module_index_ctr = 0;
8431
8432 // Discover and declare targets for each unique path.
cfcab6c7 8433 s.op->newline() << "static struct stapiu_target "
2b69faaf
JS
8434 << "stap_inode_uprobe_targets[] = {";
8435 s.op->indent(1);
8436 for (unsigned i=0; i<probes.size(); i++)
8437 {
8438 uprobe_derived_probe *p = probes[i];
cfcab6c7
JS
8439 const string key = make_pbm_key(p);
8440 if (module_index.find (key) == module_index.end())
2b69faaf 8441 {
cfcab6c7
JS
8442 module_index[key] = module_index_ctr++;
8443 s.op->newline() << "{";
8444 s.op->line() << " .finder={";
b78a0fbb 8445 s.op->line() << " .purpose=\"inode-uprobes\",";
cfcab6c7
JS
8446 if (p->pid != 0)
8447 s.op->line() << " .pid=" << p->pid << ",";
8448
8449 if (p->section == "") // .statement(addr).absolute XXX?
8450 s.op->line() << " .callback=&stapiu_process_found,";
8451 else if (p->section == ".absolute") // proxy for ET_EXEC -> exec()'d program
8452 {
8453 s.op->line() << " .procname=" << lex_cast_qstring(p->module) << ",";
8454 s.op->line() << " .callback=&stapiu_process_found,";
8455 }
8456 else if (p->section != ".absolute") // ET_DYN
8457 {
8458 if (p->has_library)
8459 s.op->line() << " .procname=\"" << p->path << "\", ";
8460 s.op->line() << " .mmap_callback=&stapiu_mmap_found, ";
8461 s.op->line() << " .munmap_callback=&stapiu_munmap_found, ";
8462 s.op->line() << " .callback=&stapiu_process_munmap,";
8463 }
8464 s.op->line() << " },";
8465 s.op->line() << " .filename=" << lex_cast_qstring(p->module) << ",";
8466 s.op->line() << " },";
2b69faaf
JS
8467 }
8468 }
8469 s.op->newline(-1) << "};";
8470 s.op->assert_0_indent();
8471
8472 // Declare the actual probes.
3689db05
SC
8473 unsigned pci;
8474 for (pci=0; pci<probes.size(); pci++)
8475 {
8476 // List of perf counters used by each probe
8477 // This list is an index into struct stap_perf_probe,
8478 uprobe_derived_probe *p = probes[pci];
698de6cc 8479 std::set<derived_probe*>::iterator pcii;
3689db05
SC
8480 s.op->newline() << "long perf_counters_" + lex_cast(pci) + "[] = {";
8481 for (pcii = p->perf_counter_refs.begin();
8482 pcii != p->perf_counter_refs.end(); pcii++)
8483 {
8484 map<string, pair<string,derived_probe*> >::iterator it;
8485 unsigned i = 0;
8486 // Find the associated perf.counter probe
4fa83377
SC
8487 for (it=s.perf_counters.begin() ;
8488 it != s.perf_counters.end(); it++, i++)
3689db05
SC
8489 if ((*it).second.second == (*pcii))
8490 break;
8491 s.op->line() << lex_cast(i) << ", ";
8492 }
8493 s.op->newline() << "};";
8494 }
8495
cfcab6c7 8496 s.op->newline() << "static struct stapiu_consumer "
2b69faaf
JS
8497 << "stap_inode_uprobe_consumers[] = {";
8498 s.op->indent(1);
8499 for (unsigned i=0; i<probes.size(); i++)
8500 {
8501 uprobe_derived_probe *p = probes[i];
cfcab6c7
JS
8502 unsigned index = module_index[make_pbm_key(p)];
8503 s.op->newline() << "{";
79af55c3
JS
8504 if (p->has_return)
8505 s.op->line() << " .return_p=1,";
cfcab6c7
JS
8506 s.op->line() << " .target=&stap_inode_uprobe_targets[" << index << "],";
8507 s.op->line() << " .offset=(loff_t)0x" << hex << p->addr << dec << "ULL,";
8508 if (p->sdt_semaphore_addr)
8509 s.op->line() << " .sdt_sem_offset=(loff_t)0x"
8510 << hex << p->sdt_semaphore_addr << dec << "ULL,";
0d049a1d 8511 // XXX: don't bother emit if array is empty
3689db05
SC
8512 s.op->line() << " .perf_counters_dim=ARRAY_SIZE(perf_counters_" << lex_cast(i) << "),";
8513 // List of perf counters used by a probe from above
0d049a1d 8514 s.op->line() << " .perf_counters=perf_counters_" + lex_cast(i) + ",";
cfcab6c7
JS
8515 s.op->line() << " .probe=" << common_probe_init (p) << ",";
8516 s.op->line() << " },";
2b69faaf
JS
8517 }
8518 s.op->newline(-1) << "};";
8519 s.op->assert_0_indent();
8520}
8521
8522
8523void
8524uprobe_derived_probe_group::emit_module_inode_init (systemtap_session& s)
8525{
8526 if (probes.empty()) return;
8527 s.op->newline() << "/* ---- inode uprobes ---- */";
b0f614a0
DS
8528 // Let stapiu_init() handle reporting errors by setting probe_point
8529 // to NULL.
8530 s.op->newline() << "probe_point = NULL;";
cfcab6c7 8531 s.op->newline() << "rc = stapiu_init ("
2b69faaf
JS
8532 << "stap_inode_uprobe_targets, "
8533 << "ARRAY_SIZE(stap_inode_uprobe_targets), "
8534 << "stap_inode_uprobe_consumers, "
8535 << "ARRAY_SIZE(stap_inode_uprobe_consumers));";
8536}
8537
8538
8539void
8540uprobe_derived_probe_group::emit_module_inode_exit (systemtap_session& s)
8541{
8542 if (probes.empty()) return;
8543 s.op->newline() << "/* ---- inode uprobes ---- */";
cfcab6c7 8544 s.op->newline() << "stapiu_exit ("
2b69faaf
JS
8545 << "stap_inode_uprobe_targets, "
8546 << "ARRAY_SIZE(stap_inode_uprobe_targets), "
8547 << "stap_inode_uprobe_consumers, "
8548 << "ARRAY_SIZE(stap_inode_uprobe_consumers));";
8549}
8550
8551
3a894f7e
JS
8552void
8553uprobe_derived_probe_group::emit_module_dyninst_decls (systemtap_session& s)
8554{
8555 if (probes.empty()) return;
8556 s.op->newline() << "/* ---- dyninst uprobes ---- */";
8557 emit_module_maxuprobes (s);
e00f3fb7 8558 s.op->newline() << "#include \"dyninst/uprobes.h\"";
3a894f7e 8559
f31a77f5
DS
8560 // Let the dynprobe_derived_probe_group handle outputting targets
8561 // and probes. This allows us to merge different types of probes.
8562 s.op->newline() << "static struct stapdu_probe stapdu_probes[];";
8563 for (unsigned i = 0; i < probes.size(); i++)
3a894f7e
JS
8564 {
8565 uprobe_derived_probe *p = probes[i];
e00f3fb7 8566
f31a77f5
DS
8567 dynprobe_add_uprobe(s, p->module, p->addr, p->sdt_semaphore_addr,
8568 (p->has_return ? "STAPDYN_PROBE_FLAG_RETURN" : "0"),
8569 common_probe_init(p));
3a894f7e 8570 }
874d38bf
JS
8571 // loc2c-generated code assumes pt_regs are available, so use this to make
8572 // sure we always have *something* for it to dereference...
f31a77f5 8573 s.op->newline() << "static struct pt_regs stapdu_dummy_uregs;";
e00f3fb7 8574
3a894f7e
JS
8575 // Write the probe handler.
8576 // NB: not static, so dyninst can find it
8577 s.op->newline() << "int enter_dyninst_uprobe "
8578 << "(uint64_t index, struct pt_regs *regs) {";
e00f3fb7 8579 s.op->newline(1) << "struct stapdu_probe *sup = &stapdu_probes[index];";
92b97104
JS
8580
8581 // Since we're sharing the entry function, we have to dynamically choose the probe_type
8582 string probe_type = "((sup->flags & STAPDYN_PROBE_FLAG_RETURN) ?"
8583 " stp_probe_type_uretprobe : stp_probe_type_uprobe)";
71db462b 8584 common_probe_entryfn_prologue (s, "STAP_SESSION_RUNNING", "sup->probe",
92b97104
JS
8585 probe_type);
8586
874d38bf 8587 s.op->newline() << "c->uregs = regs ?: &stapdu_dummy_uregs;";
e04b5d74 8588 s.op->newline() << "c->user_mode_p = 1;";
3a894f7e
JS
8589 // XXX: once we have regs, check how dyninst sets the IP
8590 // XXX: the way that dyninst rewrites stuff is probably going to be
8591 // ... very confusing to our backtracer (at least if we stay in process)
8592 s.op->newline() << "(*sup->probe->ph) (c);";
f887a8c9 8593 common_probe_entryfn_epilogue (s, true);
3a894f7e
JS
8594 s.op->newline() << "return 0;";
8595 s.op->newline(-1) << "}";
3debb935 8596 s.op->newline() << "#include \"dyninst/uprobes-regs.c\"";
3a894f7e
JS
8597 s.op->assert_0_indent();
8598}
8599
8600
8601void
8602uprobe_derived_probe_group::emit_module_dyninst_init (systemtap_session& s)
8603{
8604 if (probes.empty()) return;
8605
8606 /* stapdyn handles the dirty work via dyninst */
8607 s.op->newline() << "/* ---- dyninst uprobes ---- */";
8608 s.op->newline() << "/* this section left intentionally blank */";
8609}
8610
8611
8612void
8613uprobe_derived_probe_group::emit_module_dyninst_exit (systemtap_session& s)
8614{
8615 if (probes.empty()) return;
8616
8617 /* stapdyn handles the dirty work via dyninst */
8618 s.op->newline() << "/* ---- dyninst uprobes ---- */";
8619 s.op->newline() << "/* this section left intentionally blank */";
8620}
8621
8622
2b69faaf
JS
8623void
8624uprobe_derived_probe_group::emit_module_decls (systemtap_session& s)
8625{
ac3af990 8626 if (s.runtime_usermode_p())
4441e344
JS
8627 emit_module_dyninst_decls (s);
8628 else if (kernel_supports_inode_uprobes (s))
2b69faaf
JS
8629 emit_module_inode_decls (s);
8630 else
8631 emit_module_utrace_decls (s);
8632}
8633
8634
8635void
8636uprobe_derived_probe_group::emit_module_init (systemtap_session& s)
8637{
ac3af990 8638 if (s.runtime_usermode_p())
4441e344
JS
8639 emit_module_dyninst_init (s);
8640 else if (kernel_supports_inode_uprobes (s))
2b69faaf
JS
8641 emit_module_inode_init (s);
8642 else
8643 emit_module_utrace_init (s);
8644}
8645
8646
8647void
8648uprobe_derived_probe_group::emit_module_exit (systemtap_session& s)
8649{
ac3af990 8650 if (s.runtime_usermode_p())
4441e344
JS
8651 emit_module_dyninst_exit (s);
8652 else if (kernel_supports_inode_uprobes (s))
2b69faaf
JS
8653 emit_module_inode_exit (s);
8654 else
8655 emit_module_utrace_exit (s);
8656}
8657
8658
e6fe60e7
AM
8659// ------------------------------------------------------------------------
8660// Kprobe derived probes
8661// ------------------------------------------------------------------------
8662
4627ed58 8663static const string TOK_KPROBE("kprobe");
935447c8 8664
bae55db9 8665struct kprobe_derived_probe: public derived_probe
d0ea46ce 8666{
23dc94f6
DS
8667 kprobe_derived_probe (systemtap_session& sess,
8668 vector<derived_probe *> & results,
8669 probe *base,
bae55db9
JS
8670 probe_point *location,
8671 const string& name,
8672 int64_t stmt_addr,
8673 bool has_return,
8674 bool has_statement,
8675 bool has_maxactive,
b642c901
SC
8676 bool has_path,
8677 bool has_library,
8678 long maxactive_val,
8679 const string& path,
8680 const string& library
bae55db9
JS
8681 );
8682 string symbol_name;
8683 Dwarf_Addr addr;
8684 bool has_return;
8685 bool has_statement;
8686 bool has_maxactive;
b642c901
SC
8687 bool has_path;
8688 bool has_library;
bae55db9 8689 long maxactive_val;
b642c901
SC
8690 string path;
8691 string library;
bae55db9
JS
8692 bool access_var;
8693 void printsig (std::ostream &o) const;
8694 void join_group (systemtap_session& s);
8695};
d0ea46ce 8696
bae55db9
JS
8697struct kprobe_derived_probe_group: public derived_probe_group
8698{
8699private:
8700 multimap<string,kprobe_derived_probe*> probes_by_module;
8701 typedef multimap<string,kprobe_derived_probe*>::iterator p_b_m_iterator;
d0ea46ce 8702
bae55db9
JS
8703public:
8704 void enroll (kprobe_derived_probe* probe);
8705 void emit_module_decls (systemtap_session& s);
8706 void emit_module_init (systemtap_session& s);
8707 void emit_module_exit (systemtap_session& s);
8708};
d0ea46ce 8709
23dc94f6
DS
8710struct kprobe_var_expanding_visitor: public var_expanding_visitor
8711{
8712 systemtap_session& sess;
8713 block *add_block;
8714 block *add_call_probe; // synthesized from .return probes with saved $vars
8715 bool add_block_tid, add_call_probe_tid;
bd5b25e1 8716 bool has_return;
23dc94f6 8717
bd5b25e1 8718 kprobe_var_expanding_visitor(systemtap_session& sess, bool has_return):
23dc94f6 8719 sess(sess), add_block(NULL), add_call_probe(NULL),
bd5b25e1
JS
8720 add_block_tid(false), add_call_probe_tid(false),
8721 has_return(has_return) {}
23dc94f6
DS
8722
8723 void visit_entry_op (entry_op* e);
8724};
8725
8726
8727kprobe_derived_probe::kprobe_derived_probe (systemtap_session& sess,
8728 vector<derived_probe *> & results,
8729 probe *base,
e6fe60e7 8730 probe_point *location,
b6371390 8731 const string& name,
e6fe60e7 8732 int64_t stmt_addr,
b6371390
JS
8733 bool has_return,
8734 bool has_statement,
8735 bool has_maxactive,
b642c901
SC
8736 bool has_path,
8737 bool has_library,
8738 long maxactive_val,
8739 const string& path,
8740 const string& library
b6371390 8741 ):
4c5d1300 8742 derived_probe (base, location, true /* .components soon rewritten */ ),
e6fe60e7 8743 symbol_name (name), addr (stmt_addr),
b6371390 8744 has_return (has_return), has_statement (has_statement),
b642c901
SC
8745 has_maxactive (has_maxactive), has_path (has_path),
8746 has_library (has_library),
8747 maxactive_val (maxactive_val),
8748 path (path), library (library)
e6fe60e7
AM
8749{
8750 this->tok = base->tok;
8751 this->access_var = false;
d0ea46ce 8752
e6fe60e7
AM
8753#ifndef USHRT_MAX
8754#define USHRT_MAX 32767
8755#endif
d0ea46ce 8756
46856d8d
JS
8757 // Expansion of $target variables in the probe body produces an error during
8758 // translate phase, since we're not using debuginfo
d0ea46ce 8759
e6fe60e7 8760 vector<probe_point::component*> comps;
46856d8d 8761 comps.push_back (new probe_point::component(TOK_KPROBE));
e6fe60e7 8762
46856d8d
JS
8763 if (has_statement)
8764 {
9ea68eb9
JS
8765 comps.push_back (new probe_point::component(TOK_STATEMENT,
8766 new literal_number(addr, true)));
46856d8d
JS
8767 comps.push_back (new probe_point::component(TOK_ABSOLUTE));
8768 }
8769 else
8770 {
8771 size_t pos = name.find(':');
8772 if (pos != string::npos)
d0ea46ce 8773 {
46856d8d
JS
8774 string module = name.substr(0, pos);
8775 string function = name.substr(pos + 1);
8776 comps.push_back (new probe_point::component(TOK_MODULE, new literal_string(module)));
8777 comps.push_back (new probe_point::component(TOK_FUNCTION, new literal_string(function)));
8778 }
8779 else
8780 comps.push_back (new probe_point::component(TOK_FUNCTION, new literal_string(name)));
46856d8d 8781 }
d0ea46ce 8782
b6371390
JS
8783 if (has_return)
8784 comps.push_back (new probe_point::component(TOK_RETURN));
8785 if (has_maxactive)
8786 comps.push_back (new probe_point::component(TOK_MAXACTIVE, new literal_number(maxactive_val)));
d0ea46ce 8787
bd5b25e1 8788 kprobe_var_expanding_visitor v (sess, has_return);
23dc94f6
DS
8789 v.replace (this->body);
8790
8791 // If during target-variable-expanding the probe, we added a new block
8792 // of code, add it to the start of the probe.
8793 if (v.add_block)
8794 this->body = new block(v.add_block, this->body);
8795
8796 // If when target-variable-expanding the probe, we need to
8797 // synthesize a sibling function-entry probe. We don't go through
8798 // the whole probe derivation business (PR10642) that could lead to
8799 // wildcard/alias resolution, or for that dwarf-induced duplication.
8800 if (v.add_call_probe)
8801 {
8802 assert (has_return);
8803
8804 // We temporarily replace base.
8805 statement* old_body = base->body;
8806 base->body = v.add_call_probe;
8807
8808 derived_probe *entry_handler
8809 = new kprobe_derived_probe (sess, results, base, location, name, 0,
8810 false, has_statement, has_maxactive,
8811 has_path, has_library, maxactive_val,
8812 path, library);
8813 results.push_back (entry_handler);
8814
8815 base->body = old_body;
8816 }
8817
e6fe60e7
AM
8818 this->sole_location()->components = comps;
8819}
d0ea46ce 8820
e6fe60e7
AM
8821void kprobe_derived_probe::printsig (ostream& o) const
8822{
8823 sole_location()->print (o);
8824 o << " /* " << " name = " << symbol_name << "*/";
8825 printsig_nested (o);
8826}
d0ea46ce 8827
e6fe60e7
AM
8828void kprobe_derived_probe::join_group (systemtap_session& s)
8829{
d0ea46ce 8830
e6fe60e7
AM
8831 if (! s.kprobe_derived_probes)
8832 s.kprobe_derived_probes = new kprobe_derived_probe_group ();
8833 s.kprobe_derived_probes->enroll (this);
d0ea46ce 8834
e6fe60e7 8835}
d0ea46ce 8836
e6fe60e7
AM
8837void kprobe_derived_probe_group::enroll (kprobe_derived_probe* p)
8838{
8839 probes_by_module.insert (make_pair (p->symbol_name, p));
8840 // probes of same symbol should share single kprobe/kretprobe
8841}
d0ea46ce 8842
e6fe60e7
AM
8843void
8844kprobe_derived_probe_group::emit_module_decls (systemtap_session& s)
8845{
8846 if (probes_by_module.empty()) return;
d0ea46ce 8847
e6fe60e7 8848 s.op->newline() << "/* ---- kprobe-based probes ---- */";
d0ea46ce 8849
e6fe60e7
AM
8850 // Warn of misconfigured kernels
8851 s.op->newline() << "#if ! defined(CONFIG_KPROBES)";
8852 s.op->newline() << "#error \"Need CONFIG_KPROBES!\"";
8853 s.op->newline() << "#endif";
8854 s.op->newline();
d0ea46ce 8855
f07c3b68 8856 s.op->newline() << "#ifndef KRETACTIVE";
1ee6b5fc 8857 s.op->newline() << "#define KRETACTIVE (max(15,6*(int)num_possible_cpus()))";
f07c3b68
FCE
8858 s.op->newline() << "#endif";
8859
e6fe60e7 8860 // Forward declare the master entry functions
88747011 8861 s.op->newline() << "static int enter_kprobe2_probe (struct kprobe *inst,";
e6fe60e7 8862 s.op->line() << " struct pt_regs *regs);";
88747011 8863 s.op->newline() << "static int enter_kretprobe2_probe (struct kretprobe_instance *inst,";
e6fe60e7 8864 s.op->line() << " struct pt_regs *regs);";
d0ea46ce 8865
e6fe60e7
AM
8866 // Emit an array of kprobe/kretprobe pointers
8867 s.op->newline() << "#if defined(STAPCONF_UNREGISTER_KPROBES)";
c9116e99 8868 s.op->newline() << "static void * stap_unreg_kprobes2[" << probes_by_module.size() << "];";
e6fe60e7 8869 s.op->newline() << "#endif";
d0ea46ce 8870
e6fe60e7 8871 // Emit the actual probe list.
d0ea46ce 8872
e6fe60e7
AM
8873 s.op->newline() << "static struct stap_dwarfless_kprobe {";
8874 s.op->newline(1) << "union { struct kprobe kp; struct kretprobe krp; } u;";
8875 s.op->newline() << "#ifdef __ia64__";
8876 s.op->newline() << "struct kprobe dummy;";
8877 s.op->newline() << "#endif";
8878 s.op->newline(-1) << "} stap_dwarfless_kprobes[" << probes_by_module.size() << "];";
8879 // NB: bss!
d0ea46ce 8880
e6fe60e7
AM
8881 s.op->newline() << "static struct stap_dwarfless_probe {";
8882 s.op->newline(1) << "const unsigned return_p:1;";
8883 s.op->newline() << "const unsigned maxactive_p:1;";
b350f56b 8884 s.op->newline() << "const unsigned optional_p:1;";
e6fe60e7
AM
8885 s.op->newline() << "unsigned registered_p:1;";
8886 s.op->newline() << "const unsigned short maxactive_val;";
935447c8 8887
e6fe60e7
AM
8888 // Function Names are mostly small and uniform enough to justify putting
8889 // char[MAX]'s into the array instead of relocated char*'s.
935447c8 8890
faea5e16
JS
8891 size_t symbol_string_name_max = 0;
8892 size_t symbol_string_name_tot = 0;
e6fe60e7 8893 for (p_b_m_iterator it = probes_by_module.begin(); it != probes_by_module.end(); it++)
6270adc1 8894 {
e6fe60e7
AM
8895 kprobe_derived_probe* p = it->second;
8896#define DOIT(var,expr) do { \
8897 size_t var##_size = (expr) + 1; \
8898 var##_max = max (var##_max, var##_size); \
8899 var##_tot += var##_size; } while (0)
e6fe60e7
AM
8900 DOIT(symbol_string_name, p->symbol_name.size());
8901#undef DOIT
6270adc1
MH
8902 }
8903
e6fe60e7
AM
8904#define CALCIT(var) \
8905 s.op->newline() << "const char " << #var << "[" << var##_name_max << "] ;";
935447c8 8906
e6fe60e7
AM
8907 CALCIT(symbol_string);
8908#undef CALCIT
6270adc1 8909
bd659351 8910 s.op->newline() << "unsigned long address;";
7c3e97f4 8911 s.op->newline() << "const struct stap_probe * const probe;";
e6fe60e7
AM
8912 s.op->newline(-1) << "} stap_dwarfless_probes[] = {";
8913 s.op->indent(1);
6270adc1 8914
e6fe60e7
AM
8915 for (p_b_m_iterator it = probes_by_module.begin(); it != probes_by_module.end(); it++)
8916 {
8917 kprobe_derived_probe* p = it->second;
8918 s.op->newline() << "{";
8919 if (p->has_return)
8920 s.op->line() << " .return_p=1,";
6270adc1 8921
e6fe60e7
AM
8922 if (p->has_maxactive)
8923 {
8924 s.op->line() << " .maxactive_p=1,";
8925 assert (p->maxactive_val >= 0 && p->maxactive_val <= USHRT_MAX);
8926 s.op->line() << " .maxactive_val=" << p->maxactive_val << ",";
8927 }
6270adc1 8928
b350f56b
JS
8929 if (p->locations[0]->optional)
8930 s.op->line() << " .optional_p=1,";
8931
e6fe60e7 8932 if (p->has_statement)
c8d9d15e 8933 s.op->line() << " .address=(unsigned long)0x" << hex << p->addr << dec << "ULL,";
e6fe60e7 8934 else
c8d9d15e 8935 s.op->line() << " .symbol_string=\"" << p->symbol_name << "\",";
5d67b47c 8936
faea5e16 8937 s.op->line() << " .probe=" << common_probe_init (p) << ",";
e6fe60e7 8938 s.op->line() << " },";
935447c8
DS
8939 }
8940
e6fe60e7 8941 s.op->newline(-1) << "};";
5d67b47c 8942
e6fe60e7
AM
8943 // Emit the kprobes callback function
8944 s.op->newline();
88747011 8945 s.op->newline() << "static int enter_kprobe2_probe (struct kprobe *inst,";
e6fe60e7
AM
8946 s.op->line() << " struct pt_regs *regs) {";
8947 // NB: as of PR5673, the kprobe|kretprobe union struct is in BSS
8948 s.op->newline(1) << "int kprobe_idx = ((uintptr_t)inst-(uintptr_t)stap_dwarfless_kprobes)/sizeof(struct stap_dwarfless_kprobe);";
8949 // Check that the index is plausible
8950 s.op->newline() << "struct stap_dwarfless_probe *sdp = &stap_dwarfless_probes[";
8951 s.op->line() << "((kprobe_idx >= 0 && kprobe_idx < " << probes_by_module.size() << ")?";
8952 s.op->line() << "kprobe_idx:0)"; // NB: at least we avoid memory corruption
8953 // XXX: it would be nice to give a more verbose error though; BUG_ON later?
8954 s.op->line() << "];";
71db462b 8955 common_probe_entryfn_prologue (s, "STAP_SESSION_RUNNING", "sdp->probe",
cda141c2 8956 "stp_probe_type_kprobe");
d9aed31e 8957 s.op->newline() << "c->kregs = regs;";
6415ddde
MW
8958
8959 // Make it look like the IP is set as it wouldn't have been replaced
8960 // by a breakpoint instruction when calling real probe handler. Reset
8961 // IP regs on return, so we don't confuse kprobes. PR10458
8962 s.op->newline() << "{";
8963 s.op->indent(1);
d9aed31e 8964 s.op->newline() << "unsigned long kprobes_ip = REG_IP(c->kregs);";
259d54c0 8965 s.op->newline() << "SET_REG_IP(regs, (unsigned long) inst->addr);";
26e63673 8966 s.op->newline() << "(*sdp->probe->ph) (c);";
259d54c0 8967 s.op->newline() << "SET_REG_IP(regs, kprobes_ip);";
6415ddde
MW
8968 s.op->newline(-1) << "}";
8969
f887a8c9 8970 common_probe_entryfn_epilogue (s, true);
e6fe60e7
AM
8971 s.op->newline() << "return 0;";
8972 s.op->newline(-1) << "}";
935447c8 8973
e6fe60e7
AM
8974 // Same for kretprobes
8975 s.op->newline();
88747011 8976 s.op->newline() << "static int enter_kretprobe2_probe (struct kretprobe_instance *inst,";
e6fe60e7
AM
8977 s.op->line() << " struct pt_regs *regs) {";
8978 s.op->newline(1) << "struct kretprobe *krp = inst->rp;";
935447c8 8979
e6fe60e7
AM
8980 // NB: as of PR5673, the kprobe|kretprobe union struct is in BSS
8981 s.op->newline() << "int kprobe_idx = ((uintptr_t)krp-(uintptr_t)stap_dwarfless_kprobes)/sizeof(struct stap_dwarfless_kprobe);";
8982 // Check that the index is plausible
8983 s.op->newline() << "struct stap_dwarfless_probe *sdp = &stap_dwarfless_probes[";
8984 s.op->line() << "((kprobe_idx >= 0 && kprobe_idx < " << probes_by_module.size() << ")?";
8985 s.op->line() << "kprobe_idx:0)"; // NB: at least we avoid memory corruption
8986 // XXX: it would be nice to give a more verbose error though; BUG_ON later?
8987 s.op->line() << "];";
935447c8 8988
71db462b 8989 common_probe_entryfn_prologue (s, "STAP_SESSION_RUNNING", "sdp->probe",
cda141c2 8990 "stp_probe_type_kretprobe");
d9aed31e 8991 s.op->newline() << "c->kregs = regs;";
6dceb5c9 8992 s.op->newline() << "c->ips.krp.pi = inst;"; // for assisting runtime's backtrace logic
6415ddde
MW
8993
8994 // Make it look like the IP is set as it wouldn't have been replaced
8995 // by a breakpoint instruction when calling real probe handler. Reset
8996 // IP regs on return, so we don't confuse kprobes. PR10458
8997 s.op->newline() << "{";
8998 s.op->indent(1);
d9aed31e 8999 s.op->newline() << "unsigned long kprobes_ip = REG_IP(c->kregs);";
259d54c0 9000 s.op->newline() << "SET_REG_IP(regs, (unsigned long) inst->rp->kp.addr);";
26e63673 9001 s.op->newline() << "(*sdp->probe->ph) (c);";
259d54c0 9002 s.op->newline() << "SET_REG_IP(regs, kprobes_ip);";
6415ddde
MW
9003 s.op->newline(-1) << "}";
9004
f887a8c9 9005 common_probe_entryfn_epilogue (s, true);
e6fe60e7
AM
9006 s.op->newline() << "return 0;";
9007 s.op->newline(-1) << "}";
bd659351 9008
03a4ec63 9009 s.op->newline() << "#ifdef STAPCONF_KALLSYMS_ON_EACH_SYMBOL";
bd659351
MW
9010 s.op->newline() << "static int kprobe_resolve(void *data, const char *name,";
9011 s.op->newline() << " struct module *owner,";
9012 s.op->newline() << " unsigned long val) {";
9013 s.op->newline(1) << "int i;";
fc1d2aa2
MW
9014 s.op->newline() << "int *p = (int *) data;";
9015 s.op->newline() << "for (i=0; i<" << probes_by_module.size()
9016 << " && *p > 0; i++) {";
bd659351 9017 s.op->newline(1) << "struct stap_dwarfless_probe *sdp = & stap_dwarfless_probes[i];";
8c272819
YW
9018 s.op->newline() << "if (! sdp->address) {";
9019 s.op->indent(1);
9020 s.op->newline() << "const char *colon;";
9021 s.op->newline() << "if (owner && (colon = strchr(sdp->symbol_string, ':'))) {";
9022 s.op->indent(1);
9023 s.op->newline() << "if ((strlen(owner->name) == (colon - sdp->symbol_string))";
9024 s.op->newline() << " && (strncmp(sdp->symbol_string, owner->name, colon - sdp->symbol_string) == 0)";
9025 s.op->newline() << " && (strcmp(colon + 1, name) == 0)) {";
9026 s.op->newline(1) << "sdp->address = val;";
9027 s.op->newline() << "(*p)--;";
9028 s.op->newline(-1) << "}";
9029 s.op->newline(-1) << "}";
9030 s.op->newline() << "else {";
fc1d2aa2 9031 s.op->newline(1) << "if (strcmp(sdp->symbol_string, name) == 0) {";
bd659351 9032 s.op->newline(1) << "sdp->address = val;";
fc1d2aa2
MW
9033 s.op->newline() << "(*p)--;";
9034 s.op->newline(-1) << "}";
8c272819
YW
9035 s.op->newline(-1) << "}";
9036 s.op->newline(-1) << "}";
9037 s.op->newline(-1) << "}";
fc1d2aa2 9038 s.op->newline() << "return (p > 0) ? 0 : -1;";
bd659351 9039 s.op->newline(-1) << "}";
03a4ec63 9040 s.op->newline() << "#endif";
935447c8
DS
9041}
9042
e6fe60e7 9043
6270adc1 9044void
e6fe60e7 9045kprobe_derived_probe_group::emit_module_init (systemtap_session& s)
6270adc1 9046{
03a4ec63 9047 s.op->newline() << "#ifdef STAPCONF_KALLSYMS_ON_EACH_SYMBOL";
fc1d2aa2
MW
9048 s.op->newline() << "{";
9049 s.op->newline(1) << "int p = 0;";
9050 s.op->newline() << "for (i = 0; i < " << probes_by_module.size() << "; i++) {";
9051 s.op->newline(1) << "struct stap_dwarfless_probe *sdp = & stap_dwarfless_probes[i];";
9052 s.op->newline() << "if (! sdp->address)";
9053 s.op->newline(1) << "p++;";
9054 s.op->newline(-2) << "}";
9055 s.op->newline() << "kallsyms_on_each_symbol(kprobe_resolve, &p);";
9056 s.op->newline(-1) << "}";
03a4ec63 9057 s.op->newline() << "#endif";
bd659351 9058
e6fe60e7 9059 s.op->newline() << "for (i=0; i<" << probes_by_module.size() << "; i++) {";
c8d9d15e 9060 s.op->newline(1) << "struct stap_dwarfless_probe *sdp = & stap_dwarfless_probes[i];";
e6fe60e7 9061 s.op->newline() << "struct stap_dwarfless_kprobe *kp = & stap_dwarfless_kprobes[i];";
c8d9d15e 9062 s.op->newline() << "void *addr = (void *) sdp->address;";
03a4ec63
MW
9063 s.op->newline() << "const char *symbol_name = addr ? NULL : sdp->symbol_string;";
9064
9065 s.op->newline() << "#ifdef STAPCONF_KALLSYMS_ON_EACH_SYMBOL";
bd659351
MW
9066 s.op->newline() << "if (! addr) {";
9067 s.op->newline(1) << "sdp->registered_p = 0;";
9319b767
MW
9068 s.op->newline() << "if (!sdp->optional_p)";
9069 s.op->newline(1) << "_stp_warn (\"probe %s registration error (symbol not found)\", probe_point);";
9070 s.op->newline(-1) << "continue;";
bd659351 9071 s.op->newline(-1) << "}";
03a4ec63
MW
9072 s.op->newline() << "#endif";
9073
26e63673 9074 s.op->newline() << "probe_point = sdp->probe->pp;"; // for error messages
e6fe60e7 9075 s.op->newline() << "if (sdp->return_p) {";
c8d9d15e 9076 s.op->newline(1) << "kp->u.krp.kp.addr = addr;";
766cee5f 9077 s.op->newline() << "#ifdef STAPCONF_KPROBE_SYMBOL_NAME";
03a4ec63 9078 s.op->newline() << "kp->u.krp.kp.symbol_name = (char *) symbol_name;";
766cee5f 9079 s.op->newline() << "#endif";
e6fe60e7
AM
9080 s.op->newline() << "if (sdp->maxactive_p) {";
9081 s.op->newline(1) << "kp->u.krp.maxactive = sdp->maxactive_val;";
9082 s.op->newline(-1) << "} else {";
f07c3b68 9083 s.op->newline(1) << "kp->u.krp.maxactive = KRETACTIVE;";
e6fe60e7 9084 s.op->newline(-1) << "}";
88747011 9085 s.op->newline() << "kp->u.krp.handler = &enter_kretprobe2_probe;";
e6fe60e7
AM
9086 // to ensure safeness of bspcache, always use aggr_kprobe on ia64
9087 s.op->newline() << "#ifdef __ia64__";
e6fe60e7 9088 s.op->newline() << "kp->dummy.addr = kp->u.krp.kp.addr;";
766cee5f 9089 s.op->newline() << "#ifdef STAPCONF_KPROBE_SYMBOL_NAME";
c8d9d15e 9090 s.op->newline() << "kp->dummy.symbol_name = kp->u.krp.kp.symbol_name;";
766cee5f 9091 s.op->newline() << "#endif";
c8d9d15e 9092 s.op->newline() << "kp->dummy.pre_handler = NULL;";
e6fe60e7
AM
9093 s.op->newline() << "rc = register_kprobe (& kp->dummy);";
9094 s.op->newline() << "if (rc == 0) {";
9095 s.op->newline(1) << "rc = register_kretprobe (& kp->u.krp);";
9096 s.op->newline() << "if (rc != 0)";
9097 s.op->newline(1) << "unregister_kprobe (& kp->dummy);";
9098 s.op->newline(-2) << "}";
9099 s.op->newline() << "#else";
9100 s.op->newline() << "rc = register_kretprobe (& kp->u.krp);";
9101 s.op->newline() << "#endif";
9102 s.op->newline(-1) << "} else {";
9103 // to ensure safeness of bspcache, always use aggr_kprobe on ia64
c8d9d15e 9104 s.op->newline(1) << "kp->u.kp.addr = addr;";
766cee5f 9105 s.op->newline() << "#ifdef STAPCONF_KPROBE_SYMBOL_NAME";
03a4ec63 9106 s.op->newline() << "kp->u.kp.symbol_name = (char *) symbol_name;";
766cee5f 9107 s.op->newline() << "#endif";
88747011 9108 s.op->newline() << "kp->u.kp.pre_handler = &enter_kprobe2_probe;";
e6fe60e7 9109 s.op->newline() << "#ifdef __ia64__";
e6fe60e7 9110 s.op->newline() << "kp->dummy.pre_handler = NULL;";
c8d9d15e 9111 s.op->newline() << "kp->dummy.addr = kp->u.kp.addr;";
766cee5f 9112 s.op->newline() << "#ifdef STAPCONF_KPROBE_SYMBOL_NAME";
c8d9d15e 9113 s.op->newline() << "kp->dummy.symbol_name = kp->u.kp.symbol_name;";
766cee5f 9114 s.op->newline() << "#endif";
e6fe60e7
AM
9115 s.op->newline() << "rc = register_kprobe (& kp->dummy);";
9116 s.op->newline() << "if (rc == 0) {";
9117 s.op->newline(1) << "rc = register_kprobe (& kp->u.kp);";
9118 s.op->newline() << "if (rc != 0)";
9119 s.op->newline(1) << "unregister_kprobe (& kp->dummy);";
9120 s.op->newline(-2) << "}";
9121 s.op->newline() << "#else";
9122 s.op->newline() << "rc = register_kprobe (& kp->u.kp);";
9123 s.op->newline() << "#endif";
9124 s.op->newline(-1) << "}";
9125 s.op->newline() << "if (rc) {"; // PR6749: tolerate a failed register_*probe.
9126 s.op->newline(1) << "sdp->registered_p = 0;";
b350f56b 9127 s.op->newline() << "if (!sdp->optional_p)";
50b6acf7 9128 s.op->newline(1) << "_stp_warn (\"probe %s (address 0x%lx) registration error (rc %d)\", probe_point, (unsigned long) addr, rc);";
b350f56b 9129 s.op->newline(-1) << "rc = 0;"; // continue with other probes
e6fe60e7
AM
9130 // XXX: shall we increment numskipped?
9131 s.op->newline(-1) << "}";
6270adc1 9132
e6fe60e7
AM
9133 s.op->newline() << "else sdp->registered_p = 1;";
9134 s.op->newline(-1) << "}"; // for loop
6270adc1
MH
9135}
9136
b4be7cbc 9137
e6fe60e7
AM
9138void
9139kprobe_derived_probe_group::emit_module_exit (systemtap_session& s)
935447c8 9140{
e6fe60e7
AM
9141 //Unregister kprobes by batch interfaces.
9142 s.op->newline() << "#if defined(STAPCONF_UNREGISTER_KPROBES)";
9143 s.op->newline() << "j = 0;";
9144 s.op->newline() << "for (i=0; i<" << probes_by_module.size() << "; i++) {";
9145 s.op->newline(1) << "struct stap_dwarfless_probe *sdp = & stap_dwarfless_probes[i];";
9146 s.op->newline() << "struct stap_dwarfless_kprobe *kp = & stap_dwarfless_kprobes[i];";
9147 s.op->newline() << "if (! sdp->registered_p) continue;";
9148 s.op->newline() << "if (!sdp->return_p)";
c9116e99 9149 s.op->newline(1) << "stap_unreg_kprobes2[j++] = &kp->u.kp;";
e6fe60e7 9150 s.op->newline(-2) << "}";
c9116e99 9151 s.op->newline() << "unregister_kprobes((struct kprobe **)stap_unreg_kprobes2, j);";
e6fe60e7
AM
9152 s.op->newline() << "j = 0;";
9153 s.op->newline() << "for (i=0; i<" << probes_by_module.size() << "; i++) {";
9154 s.op->newline(1) << "struct stap_dwarfless_probe *sdp = & stap_dwarfless_probes[i];";
9155 s.op->newline() << "struct stap_dwarfless_kprobe *kp = & stap_dwarfless_kprobes[i];";
9156 s.op->newline() << "if (! sdp->registered_p) continue;";
9157 s.op->newline() << "if (sdp->return_p)";
c9116e99 9158 s.op->newline(1) << "stap_unreg_kprobes2[j++] = &kp->u.krp;";
e6fe60e7 9159 s.op->newline(-2) << "}";
c9116e99 9160 s.op->newline() << "unregister_kretprobes((struct kretprobe **)stap_unreg_kprobes2, j);";
e6fe60e7
AM
9161 s.op->newline() << "#ifdef __ia64__";
9162 s.op->newline() << "j = 0;";
9163 s.op->newline() << "for (i=0; i<" << probes_by_module.size() << "; i++) {";
9164 s.op->newline(1) << "struct stap_dwarfless_probe *sdp = & stap_dwarfless_probes[i];";
9165 s.op->newline() << "struct stap_dwarfless_kprobe *kp = & stap_dwarfless_kprobes[i];";
9166 s.op->newline() << "if (! sdp->registered_p) continue;";
c9116e99 9167 s.op->newline() << "stap_unreg_kprobes2[j++] = &kp->dummy;";
e6fe60e7 9168 s.op->newline(-1) << "}";
c9116e99 9169 s.op->newline() << "unregister_kprobes((struct kprobe **)stap_unreg_kprobes2, j);";
e6fe60e7
AM
9170 s.op->newline() << "#endif";
9171 s.op->newline() << "#endif";
3e3bd7b6 9172
e6fe60e7
AM
9173 s.op->newline() << "for (i=0; i<" << probes_by_module.size() << "; i++) {";
9174 s.op->newline(1) << "struct stap_dwarfless_probe *sdp = & stap_dwarfless_probes[i];";
9175 s.op->newline() << "struct stap_dwarfless_kprobe *kp = & stap_dwarfless_kprobes[i];";
9176 s.op->newline() << "if (! sdp->registered_p) continue;";
9177 s.op->newline() << "if (sdp->return_p) {";
9178 s.op->newline() << "#if !defined(STAPCONF_UNREGISTER_KPROBES)";
9179 s.op->newline(1) << "unregister_kretprobe (&kp->u.krp);";
9180 s.op->newline() << "#endif";
065d5567 9181 s.op->newline() << "atomic_add (kp->u.krp.nmissed, skipped_count());";
e6fe60e7
AM
9182 s.op->newline() << "#ifdef STP_TIMING";
9183 s.op->newline() << "if (kp->u.krp.nmissed)";
26e63673 9184 s.op->newline(1) << "_stp_warn (\"Skipped due to missed kretprobe/1 on '%s': %d\\n\", sdp->probe->pp, kp->u.krp.nmissed);";
e6fe60e7 9185 s.op->newline(-1) << "#endif";
065d5567 9186 s.op->newline() << "atomic_add (kp->u.krp.kp.nmissed, skipped_count());";
e6fe60e7
AM
9187 s.op->newline() << "#ifdef STP_TIMING";
9188 s.op->newline() << "if (kp->u.krp.kp.nmissed)";
26e63673 9189 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
9190 s.op->newline(-1) << "#endif";
9191 s.op->newline(-1) << "} else {";
9192 s.op->newline() << "#if !defined(STAPCONF_UNREGISTER_KPROBES)";
9193 s.op->newline(1) << "unregister_kprobe (&kp->u.kp);";
9194 s.op->newline() << "#endif";
065d5567 9195 s.op->newline() << "atomic_add (kp->u.kp.nmissed, skipped_count());";
e6fe60e7
AM
9196 s.op->newline() << "#ifdef STP_TIMING";
9197 s.op->newline() << "if (kp->u.kp.nmissed)";
26e63673 9198 s.op->newline(1) << "_stp_warn (\"Skipped due to missed kprobe on '%s': %lu\\n\", sdp->probe->pp, kp->u.kp.nmissed);";
e6fe60e7
AM
9199 s.op->newline(-1) << "#endif";
9200 s.op->newline(-1) << "}";
9201 s.op->newline() << "#if !defined(STAPCONF_UNREGISTER_KPROBES) && defined(__ia64__)";
9202 s.op->newline() << "unregister_kprobe (&kp->dummy);";
9203 s.op->newline() << "#endif";
9204 s.op->newline() << "sdp->registered_p = 0;";
9205 s.op->newline(-1) << "}";
f8a968bc
JS
9206}
9207
e6fe60e7 9208struct kprobe_builder: public derived_probe_builder
3c1b3d06 9209{
9fdf787d 9210public:
2a639817 9211 kprobe_builder() {}
9fdf787d 9212
2a639817 9213 void build_no_more (systemtap_session &s) {}
9fdf787d 9214
e6fe60e7
AM
9215 virtual void build(systemtap_session & sess,
9216 probe * base,
9217 probe_point * location,
9218 literal_map_t const & parameters,
9219 vector<derived_probe *> & finished_results);
9220};
3c1b3d06
FCE
9221
9222
79189b84 9223void
05fb3e0c 9224kprobe_builder::build(systemtap_session & sess,
e6fe60e7
AM
9225 probe * base,
9226 probe_point * location,
9227 literal_map_t const & parameters,
9228 vector<derived_probe *> & finished_results)
79189b84 9229{
e6fe60e7 9230 string function_string_val, module_string_val;
05fb3e0c 9231 string path, library, path_tgt, library_tgt;
b6371390
JS
9232 int64_t statement_num_val = 0, maxactive_val = 0;
9233 bool has_function_str, has_module_str, has_statement_num;
9234 bool has_absolute, has_return, has_maxactive;
b642c901 9235 bool has_path, has_library;
79189b84 9236
b6371390
JS
9237 has_function_str = get_param(parameters, TOK_FUNCTION, function_string_val);
9238 has_module_str = get_param(parameters, TOK_MODULE, module_string_val);
9239 has_return = has_null_param (parameters, TOK_RETURN);
9240 has_maxactive = get_param(parameters, TOK_MAXACTIVE, maxactive_val);
9241 has_statement_num = get_param(parameters, TOK_STATEMENT, statement_num_val);
9242 has_absolute = has_null_param (parameters, TOK_ABSOLUTE);
b642c901
SC
9243 has_path = get_param (parameters, TOK_PROCESS, path);
9244 has_library = get_param (parameters, TOK_LIBRARY, library);
9245
9246 if (has_path)
05fb3e0c
WF
9247 {
9248 path = find_executable (path, sess.sysroot, sess.sysenv);
9249 path_tgt = path_remove_sysroot(sess, path);
9250 }
b642c901 9251 if (has_library)
05fb3e0c
WF
9252 {
9253 library = find_executable (library, sess.sysroot, sess.sysenv,
9254 "LD_LIBRARY_PATH");
9255 library_tgt = path_remove_sysroot(sess, library);
9256 }
c57ea854 9257
b6371390 9258 if (has_function_str)
6fb70fb7 9259 {
2a639817 9260 if (has_module_str)
9fdf787d
DS
9261 {
9262 function_string_val = module_string_val + ":" + function_string_val;
9263 derived_probe *dp
23dc94f6
DS
9264 = new kprobe_derived_probe (sess, finished_results, base,
9265 location, function_string_val,
9fdf787d
DS
9266 0, has_return, has_statement_num,
9267 has_maxactive, has_path, has_library,
9268 maxactive_val, path_tgt, library_tgt);
9269 finished_results.push_back (dp);
9270 }
9271 else
9272 {
2a639817
JS
9273 vector<string> matches;
9274
9275 // Simple names can be found directly
9276 if (function_string_val.find_first_of("*?[") == string::npos)
9277 {
9278 if (sess.kernel_functions.count(function_string_val))
9279 matches.push_back(function_string_val);
9280 }
9281 else // Search function name list for matching names
9282 {
9283 for (set<string>::const_iterator it = sess.kernel_functions.begin();
9284 it != sess.kernel_functions.end(); it++)
9285 // fnmatch returns zero for matching.
9286 if (fnmatch(function_string_val.c_str(), it->c_str(), 0) == 0)
9287 matches.push_back(*it);
9288 }
86758d5f 9289
2a639817
JS
9290 for (vector<string>::const_iterator it = matches.begin();
9291 it != matches.end(); it++)
9fdf787d 9292 {
2a639817
JS
9293 derived_probe *dp
9294 = new kprobe_derived_probe (sess, finished_results, base,
9295 location, *it, 0, has_return,
9296 has_statement_num,
9297 has_maxactive, has_path,
9298 has_library, maxactive_val,
9299 path_tgt, library_tgt);
9300 finished_results.push_back (dp);
9fdf787d
DS
9301 }
9302 }
6fb70fb7 9303 }
e6fe60e7 9304 else
b6371390
JS
9305 {
9306 // assert guru mode for absolute probes
9307 if ( has_statement_num && has_absolute && !base->privileged )
dc09353a 9308 throw SEMANTIC_ERROR (_("absolute statement probe in unprivileged script; need stap -g"), base->tok);
b6371390 9309
23dc94f6
DS
9310 finished_results.push_back (new kprobe_derived_probe (sess,
9311 finished_results,
9312 base,
b6371390
JS
9313 location, "",
9314 statement_num_val,
9315 has_return,
9316 has_statement_num,
9317 has_maxactive,
b642c901
SC
9318 has_path,
9319 has_library,
9320 maxactive_val,
05fb3e0c
WF
9321 path_tgt,
9322 library_tgt));
96b030fe 9323 }
79189b84
JS
9324}
9325
23dc94f6
DS
9326
9327void
9328kprobe_var_expanding_visitor::visit_entry_op (entry_op *e)
9329{
9330 expression *repl = e;
9331
bd5b25e1
JS
9332 if (has_return)
9333 {
9334 // expand the operand as if it weren't a return probe
9335 has_return = false;
9336 replace (e->operand);
9337 has_return = true;
23dc94f6 9338
bd5b25e1
JS
9339 // XXX it would be nice to use gen_kretprobe_saved_return when
9340 // available, but it requires knowing the types already, which is
9341 // problematic for arbitrary expressons.
9342 repl = gen_mapped_saved_return (sess, e->operand, "entry",
9343 add_block, add_block_tid,
9344 add_call_probe, add_call_probe_tid);
9345 }
23dc94f6
DS
9346 provide (repl);
9347}
9348
9349
dd225250
PS
9350// ------------------------------------------------------------------------
9351// Hardware breakpoint based probes.
9352// ------------------------------------------------------------------------
9353
9354static const string TOK_HWBKPT("data");
9355static const string TOK_HWBKPT_WRITE("write");
9356static const string TOK_HWBKPT_RW("rw");
9357static const string TOK_LENGTH("length");
9358
9359#define HWBKPT_READ 0
9360#define HWBKPT_WRITE 1
9361#define HWBKPT_RW 2
9362struct hwbkpt_derived_probe: public derived_probe
9363{
9364 hwbkpt_derived_probe (probe *base,
9365 probe_point *location,
9366 uint64_t addr,
9367 string symname,
9368 unsigned int len,
9369 bool has_only_read_access,
9370 bool has_only_write_access,
9371 bool has_rw_access
9372 );
9373 Dwarf_Addr hwbkpt_addr;
9374 string symbol_name;
9375 unsigned int hwbkpt_access,hwbkpt_len;
9376
9377 void printsig (std::ostream &o) const;
9378 void join_group (systemtap_session& s);
9379};
9380
9381struct hwbkpt_derived_probe_group: public derived_probe_group
9382{
dd225250 9383private:
dac77b80 9384 vector<hwbkpt_derived_probe*> hwbkpt_probes;
dd225250
PS
9385
9386public:
9387 void enroll (hwbkpt_derived_probe* probe, systemtap_session& s);
9388 void emit_module_decls (systemtap_session& s);
9389 void emit_module_init (systemtap_session& s);
9390 void emit_module_exit (systemtap_session& s);
9391};
9392
9393hwbkpt_derived_probe::hwbkpt_derived_probe (probe *base,
9ea68eb9
JS
9394 probe_point *location,
9395 uint64_t addr,
9396 string symname,
9397 unsigned int len,
9398 bool has_only_read_access,
9399 bool has_only_write_access,
822a6a3d 9400 bool):
4c5d1300 9401 derived_probe (base, location, true /* .components soon rewritten */ ),
dd225250
PS
9402 hwbkpt_addr (addr),
9403 symbol_name (symname),
9404 hwbkpt_len (len)
9405{
9406 this->tok = base->tok;
9407
9408 vector<probe_point::component*> comps;
9409 comps.push_back (new probe_point::component(TOK_KERNEL));
9410
9411 if (hwbkpt_addr)
9ea68eb9
JS
9412 comps.push_back (new probe_point::component (TOK_HWBKPT,
9413 new literal_number(hwbkpt_addr, true)));
9414 else if (symbol_name.size())
9415 comps.push_back (new probe_point::component (TOK_HWBKPT, new literal_string(symbol_name)));
dd225250
PS
9416
9417 comps.push_back (new probe_point::component (TOK_LENGTH, new literal_number(hwbkpt_len)));
9418
9419 if (has_only_read_access)
9ea68eb9 9420 this->hwbkpt_access = HWBKPT_READ ;
dd225250
PS
9421//TODO add code for comps.push_back for read, since this flag is not for x86
9422
9423 else
9ea68eb9
JS
9424 {
9425 if (has_only_write_access)
9426 {
9427 this->hwbkpt_access = HWBKPT_WRITE ;
9428 comps.push_back (new probe_point::component(TOK_HWBKPT_WRITE));
9429 }
9430 else
9431 {
9432 this->hwbkpt_access = HWBKPT_RW ;
9433 comps.push_back (new probe_point::component(TOK_HWBKPT_RW));
9434 }
9435 }
dd225250
PS
9436
9437 this->sole_location()->components = comps;
9438}
9439
9440void hwbkpt_derived_probe::printsig (ostream& o) const
9441{
9442 sole_location()->print (o);
9443 printsig_nested (o);
9444}
9445
9446void hwbkpt_derived_probe::join_group (systemtap_session& s)
9447{
dac77b80
FCE
9448 if (! s.hwbkpt_derived_probes)
9449 s.hwbkpt_derived_probes = new hwbkpt_derived_probe_group ();
dd225250
PS
9450 s.hwbkpt_derived_probes->enroll (this, s);
9451}
9452
9453void hwbkpt_derived_probe_group::enroll (hwbkpt_derived_probe* p, systemtap_session& s)
9454{
dac77b80
FCE
9455 hwbkpt_probes.push_back (p);
9456
9457 unsigned max_hwbkpt_probes_by_arch = 0;
9458 if (s.architecture == "i386" || s.architecture == "x86_64")
9459 max_hwbkpt_probes_by_arch = 4;
9460 else if (s.architecture == "s390")
9461 max_hwbkpt_probes_by_arch = 1;
9462
c57ea854 9463 if (hwbkpt_probes.size() >= max_hwbkpt_probes_by_arch)
2713ea24 9464 s.print_warning (_F("Too many hardware breakpoint probes requested for %s (%zu vs. %u)",
b530b5b3 9465 s.architecture.c_str(), hwbkpt_probes.size(), max_hwbkpt_probes_by_arch));
dd225250
PS
9466}
9467
9468void
9469hwbkpt_derived_probe_group::emit_module_decls (systemtap_session& s)
9470{
dac77b80 9471 if (hwbkpt_probes.empty()) return;
dd225250
PS
9472
9473 s.op->newline() << "/* ---- hwbkpt-based probes ---- */";
9474
9475 s.op->newline() << "#include <linux/perf_event.h>";
9476 s.op->newline() << "#include <linux/hw_breakpoint.h>";
9477 s.op->newline();
9478
9479 // Forward declare the master entry functions
23063de1 9480 s.op->newline() << "#ifdef STAPCONF_PERF_HANDLER_NMI";
dd225250
PS
9481 s.op->newline() << "static int enter_hwbkpt_probe (struct perf_event *bp,";
9482 s.op->line() << " int nmi,";
9483 s.op->line() << " struct perf_sample_data *data,";
9484 s.op->line() << " struct pt_regs *regs);";
23063de1
FCE
9485 s.op->newline() << "#else";
9486 s.op->newline() << "static int enter_hwbkpt_probe (struct perf_event *bp,";
9487 s.op->line() << " struct perf_sample_data *data,";
9488 s.op->line() << " struct pt_regs *regs);";
9489 s.op->newline() << "#endif";
79189b84 9490
dd225250
PS
9491 // Emit the actual probe list.
9492
9493 s.op->newline() << "static struct perf_event_attr ";
dac77b80 9494 s.op->newline() << "stap_hwbkpt_probe_array[" << hwbkpt_probes.size() << "];";
dd225250
PS
9495
9496 s.op->newline() << "static struct perf_event **";
dac77b80 9497 s.op->newline() << "stap_hwbkpt_ret_array[" << hwbkpt_probes.size() << "];";
dd225250
PS
9498 s.op->newline() << "static struct stap_hwbkpt_probe {";
9499 s.op->newline() << "int registered_p:1;";
43650b10 9500// registered_p = 0 signifies a probe that is unregistered (or failed)
dd225250
PS
9501// registered_p = 1 signifies a probe that got registered successfully
9502
faea5e16 9503 // Symbol Names are mostly small and uniform enough
dd225250 9504 // to justify putting const char*.
dac77b80 9505 s.op->newline() << "const char * const symbol;";
dd225250
PS
9506
9507 s.op->newline() << "const unsigned long address;";
9508 s.op->newline() << "uint8_t atype;";
bb0a4e12 9509 s.op->newline() << "unsigned int len;";
7c3e97f4 9510 s.op->newline() << "const struct stap_probe * const probe;";
dd225250
PS
9511 s.op->newline() << "} stap_hwbkpt_probes[] = {";
9512 s.op->indent(1);
9513
dac77b80 9514 for (unsigned int it = 0; it < hwbkpt_probes.size(); it++)
dd225250 9515 {
dac77b80 9516 hwbkpt_derived_probe* p = hwbkpt_probes.at(it);
dd225250 9517 s.op->newline() << "{";
dd225250
PS
9518 if (p->symbol_name.size())
9519 s.op->line() << " .address=(unsigned long)0x0" << "ULL,";
9520 else
9521 s.op->line() << " .address=(unsigned long)0x" << hex << p->hwbkpt_addr << dec << "ULL,";
9522 switch(p->hwbkpt_access){
9523 case HWBKPT_READ:
9524 s.op->line() << " .atype=HW_BREAKPOINT_R ,";
bb0a4e12 9525 break;
dd225250
PS
9526 case HWBKPT_WRITE:
9527 s.op->line() << " .atype=HW_BREAKPOINT_W ,";
bb0a4e12 9528 break;
dd225250
PS
9529 case HWBKPT_RW:
9530 s.op->line() << " .atype=HW_BREAKPOINT_R|HW_BREAKPOINT_W ,";
bb0a4e12 9531 break;
dd225250
PS
9532 };
9533 s.op->line() << " .len=" << p->hwbkpt_len << ",";
faea5e16 9534 s.op->line() << " .probe=" << common_probe_init (p) << ",";
dd225250 9535 s.op->line() << " .symbol=\"" << p->symbol_name << "\",";
dd225250
PS
9536 s.op->line() << " },";
9537 }
dac77b80 9538 s.op->newline(-1) << "};";
dd225250
PS
9539
9540 // Emit the hwbkpt callback function
9541 s.op->newline() ;
23063de1 9542 s.op->newline() << "#ifdef STAPCONF_PERF_HANDLER_NMI";
dd225250
PS
9543 s.op->newline() << "static int enter_hwbkpt_probe (struct perf_event *bp,";
9544 s.op->line() << " int nmi,";
9545 s.op->line() << " struct perf_sample_data *data,";
9546 s.op->line() << " struct pt_regs *regs) {";
23063de1
FCE
9547 s.op->newline() << "#else";
9548 s.op->newline() << "static int enter_hwbkpt_probe (struct perf_event *bp,";
9549 s.op->line() << " struct perf_sample_data *data,";
9550 s.op->line() << " struct pt_regs *regs) {";
9551 s.op->newline() << "#endif";
dac77b80
FCE
9552 s.op->newline(1) << "unsigned int i;";
9553 s.op->newline() << "if (bp->attr.type != PERF_TYPE_BREAKPOINT) return -1;";
9554 s.op->newline() << "for (i=0; i<" << hwbkpt_probes.size() << "; i++) {";
9555 s.op->newline(1) << "struct perf_event_attr *hp = & stap_hwbkpt_probe_array[i];";
9556 // XXX: why not match stap_hwbkpt_ret_array[i] against bp instead?
9557 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) {";
9558 s.op->newline(1) << "struct stap_hwbkpt_probe *sdp = &stap_hwbkpt_probes[i];";
71db462b 9559 common_probe_entryfn_prologue (s, "STAP_SESSION_RUNNING", "sdp->probe",
cda141c2 9560 "stp_probe_type_hwbkpt");
d9aed31e 9561 s.op->newline() << "if (user_mode(regs)) {";
e04b5d74 9562 s.op->newline(1)<< "c->user_mode_p = 1;";
d9aed31e
MW
9563 s.op->newline() << "c->uregs = regs;";
9564 s.op->newline(-1) << "} else {";
9565 s.op->newline(1) << "c->kregs = regs;";
9566 s.op->newline(-1) << "}";
26e63673 9567 s.op->newline() << "(*sdp->probe->ph) (c);";
f887a8c9 9568 common_probe_entryfn_epilogue (s, true);
dac77b80 9569 s.op->newline(-1) << "}";
dd225250
PS
9570 s.op->newline(-1) << "}";
9571 s.op->newline() << "return 0;";
dac77b80 9572 s.op->newline(-1) << "}";
dd225250
PS
9573}
9574
9575void
9576hwbkpt_derived_probe_group::emit_module_init (systemtap_session& s)
9577{
dac77b80 9578 s.op->newline() << "for (i=0; i<" << hwbkpt_probes.size() << "; i++) {";
dd225250
PS
9579 s.op->newline(1) << "struct stap_hwbkpt_probe *sdp = & stap_hwbkpt_probes[i];";
9580 s.op->newline() << "struct perf_event_attr *hp = & stap_hwbkpt_probe_array[i];";
9581 s.op->newline() << "void *addr = (void *) sdp->address;";
9582 s.op->newline() << "const char *hwbkpt_symbol_name = addr ? NULL : sdp->symbol;";
dac77b80
FCE
9583 s.op->newline() << "hw_breakpoint_init(hp);";
9584 s.op->newline() << "if (addr)";
9585 s.op->newline(1) << "hp->bp_addr = (unsigned long) addr;";
9586 s.op->newline(-1) << "else { ";
9587 s.op->newline(1) << "hp->bp_addr = kallsyms_lookup_name(hwbkpt_symbol_name);";
9588 s.op->newline() << "if (!hp->bp_addr) { ";
26e63673 9589 s.op->newline(1) << "_stp_warn(\"Probe %s registration skipped: invalid symbol %s \",sdp->probe->pp,hwbkpt_symbol_name);";
dac77b80
FCE
9590 s.op->newline() << "continue;";
9591 s.op->newline(-1) << "}";
9592 s.op->newline(-1) << "}";
9593 s.op->newline() << "hp->bp_type = sdp->atype;";
9594
9595 // On x86 & x86-64, hp->bp_len is not just a number but a macro/enum (!?!).
c57ea854 9596 if (s.architecture == "i386" || s.architecture == "x86_64" )
dac77b80
FCE
9597 {
9598 s.op->newline() << "switch(sdp->len) {";
9599 s.op->newline() << "case 1:";
9600 s.op->newline(1) << "hp->bp_len = HW_BREAKPOINT_LEN_1;";
9601 s.op->newline() << "break;";
9602 s.op->newline(-1) << "case 2:";
9603 s.op->newline(1) << "hp->bp_len = HW_BREAKPOINT_LEN_2;";
9604 s.op->newline() << "break;";
9605 s.op->newline(-1) << "case 3:";
9606 s.op->newline() << "case 4:";
9607 s.op->newline(1) << "hp->bp_len = HW_BREAKPOINT_LEN_4;";
9608 s.op->newline() << "break;";
9609 s.op->newline(-1) << "case 5:";
9610 s.op->newline() << "case 6:";
9611 s.op->newline() << "case 7:";
9612 s.op->newline() << "case 8:";
9613 s.op->newline() << "default:"; // XXX: could instead reject
9614 s.op->newline(1) << "hp->bp_len = HW_BREAKPOINT_LEN_8;";
9615 s.op->newline() << "break;";
9616 s.op->newline(-1) << "}";
9617 }
9618 else // other architectures presumed straightforward
9619 s.op->newline() << "hp->bp_len = sdp->len;";
9620
26e63673 9621 s.op->newline() << "probe_point = sdp->probe->pp;"; // for error messages
b273669e
MW
9622 s.op->newline() << "#ifdef STAPCONF_HW_BREAKPOINT_CONTEXT";
9623 s.op->newline() << "stap_hwbkpt_ret_array[i] = register_wide_hw_breakpoint(hp, (void *)&enter_hwbkpt_probe, NULL);";
9624 s.op->newline() << "#else";
dac77b80 9625 s.op->newline() << "stap_hwbkpt_ret_array[i] = register_wide_hw_breakpoint(hp, (void *)&enter_hwbkpt_probe);";
b273669e 9626 s.op->newline() << "#endif";
43650b10 9627 s.op->newline() << "rc = 0;";
dac77b80 9628 s.op->newline() << "if (IS_ERR(stap_hwbkpt_ret_array[i])) {";
43650b10
WC
9629 s.op->newline(1) << "rc = PTR_ERR(stap_hwbkpt_ret_array[i]);";
9630 s.op->newline() << "stap_hwbkpt_ret_array[i] = 0;";
9631 s.op->newline(-1) << "}";
217ef1f4
WC
9632 s.op->newline() << "if (rc) {";
9633 s.op->newline(1) << "_stp_warn(\"Hwbkpt probe %s: registration error %d, addr %p, name %s\", probe_point, rc, addr, hwbkpt_symbol_name);";
43650b10 9634 s.op->newline() << "sdp->registered_p = 0;";
dac77b80 9635 s.op->newline(-1) << "}";
dd225250 9636 s.op->newline() << " else sdp->registered_p = 1;";
dd225250
PS
9637 s.op->newline(-1) << "}"; // for loop
9638}
9639
9640void
9641hwbkpt_derived_probe_group::emit_module_exit (systemtap_session& s)
9642{
9643 //Unregister hwbkpt probes.
dac77b80 9644 s.op->newline() << "for (i=0; i<" << hwbkpt_probes.size() << "; i++) {";
dd225250 9645 s.op->newline(1) << "struct stap_hwbkpt_probe *sdp = & stap_hwbkpt_probes[i];";
dac77b80
FCE
9646 s.op->newline() << "if (sdp->registered_p == 0) continue;";
9647 s.op->newline() << "unregister_wide_hw_breakpoint(stap_hwbkpt_ret_array[i]);";
dd225250
PS
9648 s.op->newline() << "sdp->registered_p = 0;";
9649 s.op->newline(-1) << "}";
9650}
9651
9652struct hwbkpt_builder: public derived_probe_builder
9653{
9654 hwbkpt_builder() {}
9655 virtual void build(systemtap_session & sess,
9656 probe * base,
9657 probe_point * location,
9658 literal_map_t const & parameters,
9659 vector<derived_probe *> & finished_results);
9660};
9661
9662void
9663hwbkpt_builder::build(systemtap_session & sess,
9664 probe * base,
9665 probe_point * location,
9666 literal_map_t const & parameters,
9667 vector<derived_probe *> & finished_results)
9668{
9669 string symbol_str_val;
9670 int64_t hwbkpt_address, len;
9671 bool has_addr, has_symbol_str, has_write, has_rw, has_len;
9672
b47f3a55 9673 if (! (sess.kernel_config["CONFIG_PERF_EVENTS"] == string("y")))
dc09353a 9674 throw SEMANTIC_ERROR (_("CONFIG_PERF_EVENTS not available on this kernel"),
b47f3a55
FCE
9675 location->components[0]->tok);
9676 if (! (sess.kernel_config["CONFIG_HAVE_HW_BREAKPOINT"] == string("y")))
dc09353a 9677 throw SEMANTIC_ERROR (_("CONFIG_HAVE_HW_BREAKPOINT not available on this kernel"),
b47f3a55
FCE
9678 location->components[0]->tok);
9679
dd225250
PS
9680 has_addr = get_param (parameters, TOK_HWBKPT, hwbkpt_address);
9681 has_symbol_str = get_param (parameters, TOK_HWBKPT, symbol_str_val);
9682 has_len = get_param (parameters, TOK_LENGTH, len);
9683 has_write = (parameters.find(TOK_HWBKPT_WRITE) != parameters.end());
9684 has_rw = (parameters.find(TOK_HWBKPT_RW) != parameters.end());
9685
9686 if (!has_len)
9687 len = 1;
9688
9689 if (has_addr)
9690 finished_results.push_back (new hwbkpt_derived_probe (base,
9691 location,
9692 hwbkpt_address,
9693 "",len,0,
9694 has_write,
9695 has_rw));
5d8a0aea 9696 else if (has_symbol_str)
dd225250
PS
9697 finished_results.push_back (new hwbkpt_derived_probe (base,
9698 location,
9699 0,
9700 symbol_str_val,len,0,
9701 has_write,
9702 has_rw));
5d8a0aea
FCE
9703 else
9704 assert (0);
dd225250 9705}
342d3f96 9706
0a6f5a3f
JS
9707// ------------------------------------------------------------------------
9708// statically inserted kernel-tracepoint derived probes
9709// ------------------------------------------------------------------------
9710
6fb70fb7 9711struct tracepoint_arg
79189b84 9712{
ad370dcc 9713 string name, c_type, typecast;
dcaa1a65 9714 bool usable, used, isptr;
f8a968bc 9715 Dwarf_Die type_die;
dcaa1a65 9716 tracepoint_arg(): usable(false), used(false), isptr(false) {}
6fb70fb7 9717};
79189b84 9718
0a6f5a3f
JS
9719struct tracepoint_derived_probe: public derived_probe
9720{
79189b84
JS
9721 tracepoint_derived_probe (systemtap_session& s,
9722 dwflpp& dw, Dwarf_Die& func_die,
9723 const string& tracepoint_name,
9724 probe* base_probe, probe_point* location);
bc9a523d 9725
79189b84 9726 systemtap_session& sess;
6fb70fb7
JS
9727 string tracepoint_name, header;
9728 vector <struct tracepoint_arg> args;
bc9a523d 9729
6fb70fb7 9730 void build_args(dwflpp& dw, Dwarf_Die& func_die);
d0bfd2ac 9731 void getargs (std::list<std::string> &arg_set) const;
79189b84 9732 void join_group (systemtap_session& s);
3e3bd7b6 9733 void print_dupe_stamp(ostream& o);
0a6f5a3f 9734};
79189b84
JS
9735
9736
0a6f5a3f 9737struct tracepoint_derived_probe_group: public generic_dpg<tracepoint_derived_probe>
79189b84 9738{
79189b84
JS
9739 void emit_module_decls (systemtap_session& s);
9740 void emit_module_init (systemtap_session& s);
9741 void emit_module_exit (systemtap_session& s);
0a6f5a3f 9742};
79189b84 9743
bc9a523d 9744
f8a968bc
JS
9745struct tracepoint_var_expanding_visitor: public var_expanding_visitor
9746{
9747 tracepoint_var_expanding_visitor(dwflpp& dw, const string& probe_name,
9748 vector <struct tracepoint_arg>& args):
9749 dw (dw), probe_name (probe_name), args (args) {}
9750 dwflpp& dw;
9751 const string& probe_name;
9752 vector <struct tracepoint_arg>& args;
bc9a523d 9753
f8a968bc
JS
9754 void visit_target_symbol (target_symbol* e);
9755 void visit_target_symbol_arg (target_symbol* e);
9756 void visit_target_symbol_context (target_symbol* e);
9757};
79189b84
JS
9758
9759
f8a968bc
JS
9760void
9761tracepoint_var_expanding_visitor::visit_target_symbol_arg (target_symbol* e)
75ead1f7 9762{
cc9001af 9763 string argname = e->sym_name();
75ead1f7 9764
f8a968bc
JS
9765 // search for a tracepoint parameter matching this name
9766 tracepoint_arg *arg = NULL;
9767 for (unsigned i = 0; i < args.size(); ++i)
dcaa1a65 9768 if (args[i].usable && args[i].name == argname)
f8a968bc
JS
9769 {
9770 arg = &args[i];
9771 arg->used = true;
9772 break;
9773 }
75ead1f7 9774
f8a968bc
JS
9775 if (arg == NULL)
9776 {
1d0499c2 9777 set<string> vars;
f8a968bc 9778 for (unsigned i = 0; i < args.size(); ++i)
1d0499c2
JL
9779 vars.insert("$" + args[i].name);
9780 vars.insert("$$name");
9781 vars.insert("$$parms");
9782 vars.insert("$$vars");
9783 string sugs = levenshtein_suggest(e->name, vars); // no need to limit, there's not that many
75ead1f7 9784
f8a968bc
JS
9785 // We hope that this value ends up not being referenced after all, so it
9786 // can be optimized out quietly.
1d0499c2
JL
9787 throw SEMANTIC_ERROR(_F("unable to find tracepoint variable '%s'%s",
9788 e->name.c_str(), sugs.empty() ? "" :
9789 (_(" (alternatives: ") + sugs + ")").c_str()), e->tok);
9790 // NB: we use 'alternatives' because we list all
f8a968bc
JS
9791 // NB: we can have multiple errors, since a target variable
9792 // may be expanded in several different contexts:
9793 // trace ("*") { $foo->bar }
f8a968bc 9794 }
75ead1f7 9795
f8a968bc 9796 // make sure we're not dereferencing base types
dc5a09fc 9797 if (!arg->isptr)
d19a9a82 9798 e->assert_no_components("tracepoint", true);
75ead1f7 9799
f8a968bc
JS
9800 // we can only write to dereferenced fields, and only if guru mode is on
9801 bool lvalue = is_active_lvalue(e);
9802 if (lvalue && (!dw.sess.guru_mode || e->components.empty()))
dc09353a 9803 throw SEMANTIC_ERROR(_F("write to tracepoint variable '%s' not permitted; need stap -g", e->name.c_str()), e->tok);
c69a87e0 9804
ad370dcc
JS
9805 // XXX: if a struct/union arg is passed by value, then writing to its fields
9806 // is also meaningless until you dereference past a pointer member. It's
9807 // harder to detect and prevent that though...
75ead1f7 9808
f8a968bc
JS
9809 if (e->components.empty())
9810 {
03c75a4a 9811 if (e->addressof)
dc09353a 9812 throw SEMANTIC_ERROR(_("cannot take address of tracepoint variable"), e->tok);
a45664f4 9813
3e3bd7b6 9814 // Just grab the value from the probe locals
a45664f4
JS
9815 symbol* sym = new symbol;
9816 sym->tok = e->tok;
9817 sym->name = "__tracepoint_arg_" + arg->name;
9818 provide (sym);
f8a968bc
JS
9819 }
9820 else
9821 {
5f36109e
JS
9822 // make a copy of the original as a bare target symbol for the tracepoint
9823 // value, which will be passed into the dwarf dereferencing code
9824 target_symbol* e2 = deep_copy_visitor::deep_copy(e);
9825 e2->components.clear();
9826
9827 if (e->components.back().type == target_symbol::comp_pretty_print)
9828 {
9829 if (lvalue)
dc09353a 9830 throw SEMANTIC_ERROR(_("cannot write to pretty-printed variable"), e->tok);
5f36109e 9831
d19a9a82 9832 dwarf_pretty_print dpp(dw, &arg->type_die, e2, arg->isptr, false, *e);
5f36109e
JS
9833 dpp.expand()->visit (this);
9834 return;
9835 }
9836
1c0be8c7 9837 bool userspace_p = false;
f8a968bc 9838 string fname = (string(lvalue ? "_tracepoint_tvar_set" : "_tracepoint_tvar_get")
cc9001af 9839 + "_" + e->sym_name()
aca66a36 9840 + "_" + lex_cast(tick++));
75ead1f7 9841
1c0be8c7
JS
9842 exp_type type = pe_long;
9843 string code = dw.literal_stmt_for_pointer (&arg->type_die, e, lvalue, type);
6fda2dff 9844
1c0be8c7
JS
9845 functioncall* n = synthetic_embedded_deref_call(dw.sess, fname, code, type,
9846 userspace_p, lvalue, e, e2);
75ead1f7 9847
f8a968bc
JS
9848 if (lvalue)
9849 {
9850 // Provide the functioncall to our parent, so that it can be
9851 // used to substitute for the assignment node immediately above
9852 // us.
9853 assert(!target_symbol_setter_functioncalls.empty());
9854 *(target_symbol_setter_functioncalls.top()) = n;
9855 }
75ead1f7 9856
1c0be8c7
JS
9857 // Revisit the functioncall so arguments can be expanded.
9858 n->visit (this);
f8a968bc 9859 }
75ead1f7
JS
9860}
9861
9862
f8a968bc
JS
9863void
9864tracepoint_var_expanding_visitor::visit_target_symbol_context (target_symbol* e)
0a6f5a3f 9865{
03c75a4a 9866 if (e->addressof)
dc09353a 9867 throw SEMANTIC_ERROR(_("cannot take address of context variable"), e->tok);
03c75a4a 9868
f8a968bc 9869 if (is_active_lvalue (e))
dc09353a 9870 throw SEMANTIC_ERROR(_F("write to tracepoint '%s' not permitted", e->name.c_str()), e->tok);
0a6f5a3f 9871
277c21bc 9872 if (e->name == "$$name")
f8a968bc 9873 {
5f36109e
JS
9874 e->assert_no_components("tracepoint");
9875
bfdaad1e
DS
9876 // Synthesize an embedded expression.
9877 embedded_expr *expr = new embedded_expr;
9878 expr->tok = e->tok;
9879 expr->code = string("/* string */ /* pure */ ")
6dceb5c9 9880 + string("c->ips.tracepoint_name ? c->ips.tracepoint_name : \"\"");
bfdaad1e 9881 provide (expr);
f8a968bc 9882 }
277c21bc 9883 else if (e->name == "$$vars" || e->name == "$$parms")
f8a968bc 9884 {
5f36109e
JS
9885 e->assert_no_components("tracepoint", true);
9886
1c922ad7 9887 print_format* pf = print_format::create(e->tok, "sprintf");
0a6f5a3f 9888
f8a968bc 9889 for (unsigned i = 0; i < args.size(); ++i)
b278033a 9890 {
dcaa1a65
JS
9891 if (!args[i].usable)
9892 continue;
f8a968bc
JS
9893 if (i > 0)
9894 pf->raw_components += " ";
9895 pf->raw_components += args[i].name;
3e3bd7b6 9896 target_symbol *tsym = new target_symbol;
f8a968bc 9897 tsym->tok = e->tok;
277c21bc 9898 tsym->name = "$" + args[i].name;
5f36109e 9899 tsym->components = e->components;
b278033a 9900
f8a968bc
JS
9901 // every variable should always be accessible!
9902 tsym->saved_conversion_error = 0;
8c2f50c0 9903 expression *texp = require<expression> (tsym); // NB: throws nothing ...
14a97852
JS
9904 if (tsym->saved_conversion_error) // ... but this is how we know it happened.
9905 {
9906 if (dw.sess.verbose>2)
e26c2f83 9907 for (const semantic_error *c = tsym->saved_conversion_error;
c081af73 9908 c != 0; c = c->get_chain())
4c5d9906 9909 clog << _("variable location problem [man error::dwarf]: ") << c->what() << endl;
14a97852
JS
9910 pf->raw_components += "=?";
9911 continue;
9912 }
b278033a 9913
5f36109e
JS
9914 if (!e->components.empty() &&
9915 e->components[0].type == target_symbol::comp_pretty_print)
9916 pf->raw_components += "=%s";
9917 else
9918 pf->raw_components += args[i].isptr ? "=%p" : "=%#x";
f8a968bc
JS
9919 pf->args.push_back(texp);
9920 }
0a6f5a3f 9921
f8a968bc
JS
9922 pf->components = print_format::string_to_components(pf->raw_components);
9923 provide (pf);
b278033a 9924 }
f8a968bc
JS
9925 else
9926 assert(0); // shouldn't get here
0a6f5a3f
JS
9927}
9928
0a6f5a3f 9929void
f8a968bc 9930tracepoint_var_expanding_visitor::visit_target_symbol (target_symbol* e)
0a6f5a3f 9931{
aff5d390 9932 try
c69a87e0 9933 {
bd1fcbad 9934 assert(e->name.size() > 0 && e->name[0] == '$');
aff5d390 9935
277c21bc 9936 if (e->name == "$$name" || e->name == "$$parms" || e->name == "$$vars")
c69a87e0 9937 visit_target_symbol_context (e);
bd1fcbad 9938
c69a87e0
FCE
9939 else
9940 visit_target_symbol_arg (e);
9941 }
9942 catch (const semantic_error &er)
9943 {
1af1e62d 9944 e->chain (er);
c69a87e0
FCE
9945 provide (e);
9946 }
0a6f5a3f
JS
9947}
9948
9949
79189b84
JS
9950tracepoint_derived_probe::tracepoint_derived_probe (systemtap_session& s,
9951 dwflpp& dw, Dwarf_Die& func_die,
9952 const string& tracepoint_name,
9953 probe* base, probe_point* loc):
4c5d1300 9954 derived_probe (base, loc, true /* .components soon rewritten */),
79189b84 9955 sess (s), tracepoint_name (tracepoint_name)
56894e91 9956{
79189b84
JS
9957 // create synthetic probe point name; preserve condition
9958 vector<probe_point::component*> comps;
9959 comps.push_back (new probe_point::component (TOK_KERNEL));
9960 comps.push_back (new probe_point::component (TOK_TRACE, new literal_string (tracepoint_name)));
9961 this->sole_location()->components = comps;
9962
6fb70fb7
JS
9963 // fill out the available arguments in this tracepoint
9964 build_args(dw, func_die);
56894e91 9965
6fb70fb7
JS
9966 // determine which header defined this tracepoint
9967 string decl_file = dwarf_decl_file(&func_die);
b78a0fbb 9968 header = decl_file;
d4393459
FCE
9969
9970#if 0 /* This convention is not enforced. */
6fb70fb7
JS
9971 size_t header_pos = decl_file.rfind("trace/");
9972 if (header_pos == string::npos)
dc09353a 9973 throw SEMANTIC_ERROR ("cannot parse header location for tracepoint '"
6fb70fb7
JS
9974 + tracepoint_name + "' in '"
9975 + decl_file + "'");
9976 header = decl_file.substr(header_pos);
d4393459 9977#endif
56894e91 9978
6fb70fb7
JS
9979 // tracepoints from FOO_event_types.h should really be included from FOO.h
9980 // XXX can dwarf tell us the include hierarchy? it would be better to
9981 // ... walk up to see which one was directly included by tracequery.c
3c1b3d06 9982 // XXX: see also PR9993.
d4393459 9983 size_t header_pos = header.find("_event_types");
6fb70fb7
JS
9984 if (header_pos != string::npos)
9985 header.erase(header_pos, 12);
56894e91 9986
f8a968bc
JS
9987 // Now expand the local variables in the probe body
9988 tracepoint_var_expanding_visitor v (dw, name, args);
8b095b45 9989 v.replace (this->body);
a45664f4
JS
9990 for (unsigned i = 0; i < args.size(); i++)
9991 if (args[i].used)
9992 {
9993 vardecl* v = new vardecl;
9994 v->name = "__tracepoint_arg_" + args[i].name;
9995 v->tok = this->tok;
58701b78 9996 v->set_arity(0, this->tok);
a45664f4 9997 v->type = pe_long;
69aa668e 9998 v->synthetic = true;
a45664f4
JS
9999 this->locals.push_back (v);
10000 }
56894e91 10001
79189b84 10002 if (sess.verbose > 2)
ce0f6648 10003 clog << "tracepoint-based " << name << " tracepoint='" << tracepoint_name << "'" << endl;
79189b84 10004}
dc38c0ae 10005
56894e91 10006
f8a968bc 10007static bool
dcaa1a65 10008resolve_tracepoint_arg_type(tracepoint_arg& arg)
46b84a80 10009{
d19a9a82 10010 Dwarf_Die type;
dcaa1a65 10011 switch (dwarf_tag(&arg.type_die))
b20febf3 10012 {
f8a968bc
JS
10013 case DW_TAG_typedef:
10014 case DW_TAG_const_type:
10015 case DW_TAG_volatile_type:
10016 // iterate on the referent type
3d1ad340 10017 return (dwarf_attr_die(&arg.type_die, DW_AT_type, &arg.type_die)
dcaa1a65 10018 && resolve_tracepoint_arg_type(arg));
f8a968bc 10019 case DW_TAG_base_type:
a52d2ac0 10020 case DW_TAG_enumeration_type:
f8a968bc 10021 // base types will simply be treated as script longs
dcaa1a65 10022 arg.isptr = false;
f8a968bc
JS
10023 return true;
10024 case DW_TAG_pointer_type:
dcaa1a65
JS
10025 // pointers can be treated as script longs,
10026 // and if we know their type, they can also be dereferenced
d19a9a82
JS
10027 type = arg.type_die;
10028 while (dwarf_attr_die(&arg.type_die, DW_AT_type, &arg.type_die))
10029 {
10030 // It still might be a non-type, e.g. const void,
10031 // so we need to strip away all qualifiers.
10032 int tag = dwarf_tag(&arg.type_die);
10033 if (tag != DW_TAG_typedef &&
10034 tag != DW_TAG_const_type &&
10035 tag != DW_TAG_volatile_type)
10036 {
10037 arg.isptr = true;
10038 break;
10039 }
10040 }
10041 if (!arg.isptr)
10042 arg.type_die = type;
ad370dcc
JS
10043 arg.typecast = "(intptr_t)";
10044 return true;
10045 case DW_TAG_structure_type:
10046 case DW_TAG_union_type:
10047 // for structs/unions which are passed by value, we turn it into
10048 // a pointer that can be dereferenced.
10049 arg.isptr = true;
10050 arg.typecast = "(intptr_t)&";
dcaa1a65 10051 return true;
f8a968bc
JS
10052 default:
10053 // should we consider other types too?
10054 return false;
b20febf3 10055 }
56894e91
JS
10056}
10057
10058
10059void
822a6a3d 10060tracepoint_derived_probe::build_args(dwflpp&, Dwarf_Die& func_die)
56894e91 10061{
6fb70fb7
JS
10062 Dwarf_Die arg;
10063 if (dwarf_child(&func_die, &arg) == 0)
10064 do
10065 if (dwarf_tag(&arg) == DW_TAG_formal_parameter)
10066 {
10067 // build a tracepoint_arg for this parameter
10068 tracepoint_arg tparg;
c60517ca 10069 tparg.name = dwarf_diename(&arg) ?: "";
56894e91 10070
6fb70fb7 10071 // read the type of this parameter
3d1ad340 10072 if (!dwarf_attr_die (&arg, DW_AT_type, &tparg.type_die)
f1c8f8a5 10073 || !dwarf_type_name(&tparg.type_die, tparg.c_type))
dc09353a 10074 throw SEMANTIC_ERROR (_F("cannot get type of parameter '%s' of tracepoint '%s'",
ce0f6648 10075 tparg.name.c_str(), tracepoint_name.c_str()));
a68f81a2 10076
dcaa1a65 10077 tparg.usable = resolve_tracepoint_arg_type(tparg);
6fb70fb7
JS
10078 args.push_back(tparg);
10079 if (sess.verbose > 4)
a52d2ac0
JS
10080 clog << _F("found parameter for tracepoint '%s': type:'%s' name:'%s' %s",
10081 tracepoint_name.c_str(), tparg.c_type.c_str(), tparg.name.c_str(),
10082 tparg.usable ? "ok" : "unavailable") << endl;
6fb70fb7
JS
10083 }
10084 while (dwarf_siblingof(&arg, &arg) == 0);
56894e91
JS
10085}
10086
dc38c0ae 10087void
d0bfd2ac 10088tracepoint_derived_probe::getargs(std::list<std::string> &arg_set) const
dc38c0ae 10089{
dcaa1a65
JS
10090 for (unsigned i = 0; i < args.size(); ++i)
10091 if (args[i].usable)
d0bfd2ac 10092 arg_set.push_back("$"+args[i].name+":"+args[i].c_type);
dc38c0ae
DS
10093}
10094
79189b84
JS
10095void
10096tracepoint_derived_probe::join_group (systemtap_session& s)
197a4d62 10097{
79189b84
JS
10098 if (! s.tracepoint_derived_probes)
10099 s.tracepoint_derived_probes = new tracepoint_derived_probe_group ();
10100 s.tracepoint_derived_probes->enroll (this);
10101}
e38d6504 10102
56894e91 10103
197a4d62 10104void
3e3bd7b6 10105tracepoint_derived_probe::print_dupe_stamp(ostream& o)
56894e91 10106{
3e3bd7b6
JS
10107 for (unsigned i = 0; i < args.size(); i++)
10108 if (args[i].used)
10109 o << "__tracepoint_arg_" << args[i].name << endl;
197a4d62 10110}
56894e91 10111
3e3bd7b6 10112
c9ccb642 10113static vector<string> tracepoint_extra_decls (systemtap_session& s, const string& header)
47dd066d 10114{
3c1b3d06
FCE
10115 vector<string> they_live;
10116 // PR 9993
10117 // XXX: may need this to be configurable
d4393459 10118 they_live.push_back ("#include <linux/skbuff.h>");
9e0cd21a
FCE
10119
10120 // PR11649: conditional extra header
10121 // for kvm tracepoints in 2.6.33ish
10122 if (s.kernel_config["CONFIG_KVM"] != string("")) {
d4393459
FCE
10123 they_live.push_back ("#include <linux/kvm_host.h>");
10124 }
10125
50b72692 10126 if (header.find("xfs") != string::npos && s.kernel_config["CONFIG_XFS_FS"] != string("")) {
d4393459 10127 they_live.push_back ("#define XFS_BIG_BLKNOS 1");
88637c31
FCE
10128 if (s.kernel_source_tree != "")
10129 they_live.push_back ("#include \"fs/xfs/xfs_types.h\""); // in kernel-source tree
d4393459
FCE
10130 they_live.push_back ("struct xfs_mount;");
10131 they_live.push_back ("struct xfs_inode;");
10132 they_live.push_back ("struct xfs_buf;");
10133 they_live.push_back ("struct xfs_bmbt_irec;");
c2cf1b87 10134 they_live.push_back ("struct xfs_trans;");
9e0cd21a 10135 }
d4393459 10136
50b72692 10137 if (header.find("nfs") != string::npos && s.kernel_config["CONFIG_NFSD"] != string("")) {
d4393459
FCE
10138 they_live.push_back ("struct rpc_task;");
10139 }
b64d65e2
FCE
10140 // RHEL6.3
10141 if (header.find("rpc") != string::npos && s.kernel_config["CONFIG_NFSD"] != string("")) {
10142 they_live.push_back ("struct rpc_clnt;");
10143 they_live.push_back ("struct rpc_wait_queue;");
10144 }
d4393459
FCE
10145
10146 they_live.push_back ("#include <asm/cputime.h>");
10147
c2cf1b87
FCE
10148 // linux 3.0
10149 they_live.push_back ("struct cpu_workqueue_struct;");
10150
50b72692 10151 if (header.find("ext4") != string::npos && s.kernel_config["CONFIG_EXT4_FS"] != string(""))
c2cf1b87
FCE
10152 if (s.kernel_source_tree != "")
10153 they_live.push_back ("#include \"fs/ext4/ext4.h\""); // in kernel-source tree
10154
d97d428e 10155 if (header.find("ext3") != string::npos)
50b72692
HP
10156 they_live.push_back ("struct ext3_reserve_window_node;");
10157
d97d428e
FCE
10158 if (header.find("workqueue") != string::npos)
10159 {
10160 they_live.push_back ("struct pool_workqueue;");
10161 they_live.push_back ("struct work_struct;");
10162 }
10163
10164 if (header.find("asoc") != string::npos)
10165 they_live.push_back ("struct snd_soc_dapm_path;");
10166
10167 if (header.find("9p") != string::npos)
10168 {
10169 they_live.push_back ("struct p9_client;");
10170 they_live.push_back ("struct p9_fcall;");
10171 }
10172
10173 if (header.find("bcache") != string::npos)
10174 {
10175 they_live.push_back ("struct bkey;");
10176 they_live.push_back ("struct btree;");
10177 they_live.push_back ("struct cache_set;");
10178 they_live.push_back ("struct cache;");
10179 }
10180
10181 if (header.find("f2fs") != string::npos)
10182 {
10183 // cannot get fs/f2fs/f2fs.h #included
10184 they_live.push_back ("typedef u32 block_t;");
10185 they_live.push_back ("typedef u32 nid_t;");
10186 }
10187
10188 if (header.find("radeon") != string::npos)
10189 they_live.push_back ("struct radeon_bo;");
10190
10191 // argh, 3.11, i915_trace.h -> i915_drv.h -> i915_reg.h without -I
10192 // also brcms_trace_events.h -> ... -> "types.h"
10193 // XXX: need a way to add a temporary -I flag
10194
10195 if (header.find("/ath/") != string::npos)
10196 they_live.push_back ("struct ath5k_hw;");
10197
10198
3c1b3d06
FCE
10199 return they_live;
10200}
47dd066d
WC
10201
10202
10203void
79189b84 10204tracepoint_derived_probe_group::emit_module_decls (systemtap_session& s)
47dd066d 10205{
79189b84
JS
10206 if (probes.empty())
10207 return;
47dd066d 10208
96b030fe 10209 s.op->newline() << "/* ---- tracepoint probes ---- */";
3ef9830a 10210 s.op->newline() << "#include <linux/stp_tracepoint.h>" << endl;
96b030fe 10211 s.op->newline();
79189b84 10212
47dd066d 10213
a4b9c3b3
FCE
10214 // We create a MODULE_aux_N.c file for each tracepoint header, to allow them
10215 // to be separately compiled. That's because kernel tracepoint headers sometimes
10216 // conflict. PR13155.
10217
10218 map<string,translator_output*> per_header_aux;
10219 // GC NB: the translator_output* structs are owned/retained by the systemtap_session.
47dd066d 10220
6fb70fb7
JS
10221 for (unsigned i = 0; i < probes.size(); ++i)
10222 {
10223 tracepoint_derived_probe *p = probes[i];
75ae2ec9 10224 string header = p->header;
5f73a260 10225
a4b9c3b3
FCE
10226 // We cache the auxiliary output files on a per-header basis. We don't
10227 // need one aux file per tracepoint, only one per tracepoint-header.
10228 translator_output *tpop = per_header_aux[header];
10229 if (tpop == 0)
10230 {
10231 tpop = s.op_create_auxiliary();
10232 per_header_aux[header] = tpop;
10233
10234 // PR9993: Add extra headers to work around undeclared types in individual
10235 // include/trace/foo.h files
10236 const vector<string>& extra_decls = tracepoint_extra_decls (s, header);
10237 for (unsigned z=0; z<extra_decls.size(); z++)
10238 tpop->newline() << extra_decls[z] << "\n";
720c435f 10239
a4b9c3b3
FCE
10240 // strip include/ substring, the same way as done in get_tracequery_module()
10241 size_t root_pos = header.rfind("include/");
10242 header = ((root_pos != string::npos) ? header.substr(root_pos + 8) : header);
bfffa443 10243
3ef9830a 10244 tpop->newline() << "#include <linux/stp_tracepoint.h>" << endl;
a4b9c3b3 10245 tpop->newline() << "#include <" << header << ">";
a4b9c3b3
FCE
10246 }
10247
720c435f
JS
10248 // collect the args that are actually in use
10249 vector<const tracepoint_arg*> used_args;
6fb70fb7 10250 for (unsigned j = 0; j < p->args.size(); ++j)
720c435f
JS
10251 if (p->args[j].used)
10252 used_args.push_back(&p->args[j]);
10253
3ef9830a
JS
10254 // forward-declare the generated-side tracepoint callback, and define the
10255 // generated-side tracepoint callback in the main translator-output
10256 string enter_real_fn = "enter_real_tracepoint_probe_" + lex_cast(i);
720c435f 10257 if (used_args.empty())
6fb70fb7 10258 {
3ef9830a
JS
10259 tpop->newline() << "STP_TRACE_ENTER_REAL_NOARGS(" << enter_real_fn << ");";
10260 s.op->newline() << "STP_TRACE_ENTER_REAL_NOARGS(" << enter_real_fn << ")";
6fb70fb7 10261 }
3ef9830a 10262 else
a4b9c3b3 10263 {
3ef9830a
JS
10264 tpop->newline() << "STP_TRACE_ENTER_REAL(" << enter_real_fn;
10265 s.op->newline() << "STP_TRACE_ENTER_REAL(" << enter_real_fn;
10266 s.op->indent(2);
10267 for (unsigned j = 0; j < used_args.size(); ++j)
10268 {
10269 tpop->line() << ", int64_t";
10270 s.op->newline() << ", int64_t __tracepoint_arg_" << used_args[j]->name;
10271 }
10272 tpop->line() << ");";
10273 s.op->newline() << ")";
10274 s.op->indent(-2);
6fb70fb7 10275 }
3ef9830a 10276 s.op->newline() << "{";
7c3e97f4 10277 s.op->newline(1) << "const struct stap_probe * const probe = "
faea5e16 10278 << common_probe_init (p) << ";";
71db462b 10279 common_probe_entryfn_prologue (s, "STAP_SESSION_RUNNING", "probe",
cda141c2 10280 "stp_probe_type_tracepoint");
6dceb5c9 10281 s.op->newline() << "c->ips.tracepoint_name = "
c12d974f
FCE
10282 << lex_cast_qstring (p->tracepoint_name)
10283 << ";";
720c435f
JS
10284 for (unsigned j = 0; j < used_args.size(); ++j)
10285 {
10286 s.op->newline() << "c->probe_locals." << p->name
0386bbcf 10287 << "." + s.up->c_localname("__tracepoint_arg_" + used_args[j]->name)
720c435f
JS
10288 << " = __tracepoint_arg_" << used_args[j]->name << ";";
10289 }
26e63673 10290 s.op->newline() << "(*probe->ph) (c);";
f887a8c9 10291 common_probe_entryfn_epilogue (s, true);
6fb70fb7 10292 s.op->newline(-1) << "}";
47dd066d 10293
a4b9c3b3 10294 // define the real tracepoint callback function
3ef9830a
JS
10295 string enter_fn = "enter_tracepoint_probe_" + lex_cast(i);
10296 if (p->args.empty())
10297 tpop->newline() << "static STP_TRACE_ENTER_NOARGS(" << enter_fn << ")";
10298 else
a4b9c3b3 10299 {
3ef9830a
JS
10300 tpop->newline() << "static STP_TRACE_ENTER(" << enter_fn;
10301 s.op->indent(2);
10302 for (unsigned j = 0; j < p->args.size(); ++j)
10303 {
10304 tpop->newline() << ", " << p->args[j].c_type
10305 << " __tracepoint_arg_" << p->args[j].name;
10306 }
10307 tpop->newline() << ")";
10308 s.op->indent(-2);
a4b9c3b3 10309 }
3ef9830a
JS
10310 tpop->newline() << "{";
10311 tpop->newline(1) << enter_real_fn << "(";
720c435f
JS
10312 tpop->indent(2);
10313 for (unsigned j = 0; j < used_args.size(); ++j)
10314 {
10315 if (j > 0)
10316 tpop->line() << ", ";
10317 tpop->newline() << "(int64_t)" << used_args[j]->typecast
10318 << "__tracepoint_arg_" << used_args[j]->name;
10319 }
10320 tpop->newline() << ");";
10321 tpop->newline(-3) << "}";
a4b9c3b3
FCE
10322
10323
96b030fe 10324 // emit normalized registration functions
720c435f 10325 tpop->newline() << "int register_tracepoint_probe_" << i << "(void) {";
3ef9830a
JS
10326 tpop->newline(1) << "return STP_TRACE_REGISTER(" << p->tracepoint_name
10327 << ", " << enter_fn << ");";
a4b9c3b3 10328 tpop->newline(-1) << "}";
47dd066d 10329
86758d5f
JS
10330 // NB: we're not prepared to deal with unreg failures. However, failures
10331 // can only occur if the tracepoint doesn't exist (yet?), or if we
10332 // weren't even registered. The former should be OKed by the initial
10333 // registration call, and the latter is safe to ignore.
720c435f 10334 tpop->newline() << "void unregister_tracepoint_probe_" << i << "(void) {";
3ef9830a
JS
10335 tpop->newline(1) << "(void) STP_TRACE_UNREGISTER(" << p->tracepoint_name
10336 << ", " << enter_fn << ");";
a4b9c3b3
FCE
10337 tpop->newline(-1) << "}";
10338 tpop->newline();
5f73a260 10339
720c435f
JS
10340 // declare normalized registration functions
10341 s.op->newline() << "int register_tracepoint_probe_" << i << "(void);";
10342 s.op->newline() << "void unregister_tracepoint_probe_" << i << "(void);";
5f73a260 10343
a4b9c3b3 10344 tpop->assert_0_indent();
af304783
DS
10345 }
10346
96b030fe
JS
10347 // emit an array of registration functions for easy init/shutdown
10348 s.op->newline() << "static struct stap_tracepoint_probe {";
10349 s.op->newline(1) << "int (*reg)(void);";
86758d5f 10350 s.op->newline(0) << "void (*unreg)(void);";
96b030fe
JS
10351 s.op->newline(-1) << "} stap_tracepoint_probes[] = {";
10352 s.op->indent(1);
10353 for (unsigned i = 0; i < probes.size(); ++i)
10354 {
10355 s.op->newline () << "{";
10356 s.op->line() << " .reg=&register_tracepoint_probe_" << i << ",";
10357 s.op->line() << " .unreg=&unregister_tracepoint_probe_" << i;
10358 s.op->line() << " },";
10359 }
10360 s.op->newline(-1) << "};";
10361 s.op->newline();
47dd066d
WC
10362}
10363
10364
79189b84
JS
10365void
10366tracepoint_derived_probe_group::emit_module_init (systemtap_session &s)
47dd066d 10367{
79189b84
JS
10368 if (probes.size () == 0)
10369 return;
47dd066d 10370
79189b84 10371 s.op->newline() << "/* init tracepoint probes */";
96b030fe
JS
10372 s.op->newline() << "for (i=0; i<" << probes.size() << "; i++) {";
10373 s.op->newline(1) << "rc = stap_tracepoint_probes[i].reg();";
10374 s.op->newline() << "if (rc) {";
10375 s.op->newline(1) << "for (j=i-1; j>=0; j--)"; // partial rollback
10376 s.op->newline(1) << "stap_tracepoint_probes[j].unreg();";
10377 s.op->newline(-1) << "break;"; // don't attempt to register any more probes
10378 s.op->newline(-1) << "}";
10379 s.op->newline(-1) << "}";
47dd066d 10380
bc9a523d
FCE
10381 // This would be technically proper (on those autoconf-detectable
10382 // kernels that include this function in tracepoint.h), however we
10383 // already make several calls to synchronze_sched() during our
10384 // shutdown processes.
47dd066d 10385
bc9a523d
FCE
10386 // s.op->newline() << "if (rc)";
10387 // s.op->newline(1) << "tracepoint_synchronize_unregister();";
10388 // s.op->indent(-1);
79189b84 10389}
47dd066d
WC
10390
10391
79189b84
JS
10392void
10393tracepoint_derived_probe_group::emit_module_exit (systemtap_session& s)
47dd066d 10394{
79189b84
JS
10395 if (probes.empty())
10396 return;
47dd066d 10397
96b030fe
JS
10398 s.op->newline() << "/* deregister tracepoint probes */";
10399 s.op->newline() << "for (i=0; i<" << probes.size() << "; i++)";
10400 s.op->newline(1) << "stap_tracepoint_probes[i].unreg();";
10401 s.op->indent(-1);
47dd066d 10402
bc9a523d 10403 // Not necessary: see above.
47dd066d 10404
bc9a523d 10405 // s.op->newline() << "tracepoint_synchronize_unregister();";
79189b84 10406}
b20febf3 10407
47dd066d 10408
75ead1f7 10409struct tracepoint_query : public base_query
47dd066d 10410{
75ead1f7
JS
10411 tracepoint_query(dwflpp & dw, const string & tracepoint,
10412 probe * base_probe, probe_point * base_loc,
10413 vector<derived_probe *> & results):
10414 base_query(dw, "*"), tracepoint(tracepoint),
10415 base_probe(base_probe), base_loc(base_loc),
10416 results(results) {}
47dd066d 10417
75ead1f7 10418 const string& tracepoint;
47dd066d 10419
75ead1f7
JS
10420 probe * base_probe;
10421 probe_point * base_loc;
10422 vector<derived_probe *> & results;
f982c59b 10423 set<string> probed_names;
47dd066d 10424
75ead1f7
JS
10425 void handle_query_module();
10426 int handle_query_cu(Dwarf_Die * cudie);
10427 int handle_query_func(Dwarf_Die * func);
822a6a3d 10428 void query_library (const char *) {}
576eaefe 10429 void query_plt (const char *entry, size_t addr) {}
b20febf3 10430
5c378838 10431 static int tracepoint_query_cu (Dwarf_Die * cudie, tracepoint_query * q);
7d007451 10432 static int tracepoint_query_func (Dwarf_Die * func, tracepoint_query * q);
75ead1f7 10433};
47dd066d
WC
10434
10435
10436void
75ead1f7 10437tracepoint_query::handle_query_module()
47dd066d 10438{
75ead1f7 10439 // look for the tracepoints in each CU
337b7c44 10440 dw.iterate_over_cus(tracepoint_query_cu, this, false);
47dd066d
WC
10441}
10442
10443
75ead1f7
JS
10444int
10445tracepoint_query::handle_query_cu(Dwarf_Die * cudie)
47dd066d 10446{
75ead1f7 10447 dw.focus_on_cu (cudie);
d906ab9d 10448 dw.mod_info->get_symtab(this);
47dd066d 10449
75ead1f7
JS
10450 // look at each function to see if it's a tracepoint
10451 string function = "stapprobe_" + tracepoint;
10452 return dw.iterate_over_functions (tracepoint_query_func, this, function);
47dd066d
WC
10453}
10454
10455
75ead1f7
JS
10456int
10457tracepoint_query::handle_query_func(Dwarf_Die * func)
47dd066d 10458{
75ead1f7 10459 dw.focus_on_function (func);
47dd066d 10460
60d98537 10461 assert(startswith(dw.function_name, "stapprobe_"));
75ead1f7 10462 string tracepoint_instance = dw.function_name.substr(10);
f982c59b
JS
10463
10464 // check for duplicates -- sometimes tracepoint headers may be indirectly
10465 // included in more than one of our tracequery modules.
10466 if (!probed_names.insert(tracepoint_instance).second)
10467 return DWARF_CB_OK;
10468
79189b84
JS
10469 derived_probe *dp = new tracepoint_derived_probe (dw.sess, dw, *func,
10470 tracepoint_instance,
10471 base_probe, base_loc);
10472 results.push_back (dp);
75ead1f7 10473 return DWARF_CB_OK;
47dd066d
WC
10474}
10475
10476
75ead1f7 10477int
5c378838 10478tracepoint_query::tracepoint_query_cu (Dwarf_Die * cudie, tracepoint_query * q)
47dd066d 10479{
85007c04 10480 if (pending_interrupts) return DWARF_CB_ABORT;
75ead1f7 10481 return q->handle_query_cu(cudie);
47dd066d
WC
10482}
10483
10484
75ead1f7 10485int
7d007451 10486tracepoint_query::tracepoint_query_func (Dwarf_Die * func, tracepoint_query * q)
47dd066d 10487{
85007c04 10488 if (pending_interrupts) return DWARF_CB_ABORT;
75ead1f7 10489 return q->handle_query_func(func);
47dd066d
WC
10490}
10491
10492
0a6f5a3f 10493struct tracepoint_builder: public derived_probe_builder
47dd066d 10494{
0a6f5a3f
JS
10495private:
10496 dwflpp *dw;
10497 bool init_dw(systemtap_session& s);
c9ccb642
FCE
10498 void get_tracequery_modules(systemtap_session& s,
10499 const vector<string>& headers,
10500 vector<string>& modules);
47dd066d 10501
0a6f5a3f 10502public:
47dd066d 10503
0a6f5a3f
JS
10504 tracepoint_builder(): dw(0) {}
10505 ~tracepoint_builder() { delete dw; }
47dd066d 10506
0a6f5a3f
JS
10507 void build_no_more (systemtap_session& s)
10508 {
10509 if (dw && s.verbose > 3)
b530b5b3 10510 clog << _("tracepoint_builder releasing dwflpp") << endl;
0a6f5a3f
JS
10511 delete dw;
10512 dw = NULL;
435f53a7
FCE
10513
10514 delete_session_module_cache (s);
0a6f5a3f 10515 }
47dd066d 10516
0a6f5a3f
JS
10517 void build(systemtap_session& s,
10518 probe *base, probe_point *location,
10519 literal_map_t const& parameters,
10520 vector<derived_probe*>& finished_results);
10521};
47dd066d 10522
47dd066d 10523
c9ccb642 10524
2a0e62a8 10525// Create (or cache) one or more tracequery .o modules, based upon the
c9ccb642
FCE
10526// tracepoint-related header files given. Return the generated or cached
10527// modules[].
10528
10529void
10530tracepoint_builder::get_tracequery_modules(systemtap_session& s,
10531 const vector<string>& headers,
10532 vector<string>& modules)
0a6f5a3f 10533{
c95eddf7 10534 if (s.verbose > 2)
55e50c24 10535 {
ce0f6648 10536 clog << _F("Pass 2: getting a tracepoint query for %zu headers: ", headers.size()) << endl;
55e50c24
JS
10537 for (size_t i = 0; i < headers.size(); ++i)
10538 clog << " " << headers[i] << endl;
10539 }
c95eddf7 10540
2a0e62a8
JS
10541 map<string,string> headers_cache_obj; // header name -> cache/.../tracequery_hash.o file name
10542 // Map the headers to cache .o names. Note that this has side-effects of
c9ccb642
FCE
10543 // creating the $SYSTEMTAP_DIR/.cache/XX/... directory and the hash-log file,
10544 // so we prefer not to repeat this.
10545 vector<string> uncached_headers;
10546 for (size_t i=0; i<headers.size(); i++)
2a0e62a8 10547 headers_cache_obj[headers[i]] = find_tracequery_hash(s, headers[i]);
c9ccb642
FCE
10548
10549 // They may be in the cache already.
10550 if (s.use_cache && !s.poison_cache)
10551 for (size_t i=0; i<headers.size(); i++)
10552 {
10553 // see if the cached module exists
2a0e62a8 10554 const string& tracequery_path = headers_cache_obj[headers[i]];
c9ccb642
FCE
10555 if (!tracequery_path.empty() && file_exists(tracequery_path))
10556 {
10557 if (s.verbose > 2)
10558 clog << _F("Pass 2: using cached %s", tracequery_path.c_str()) << endl;
47dd066d 10559
c252fca2
JS
10560 // an empty file is a cached failure
10561 if (get_file_size(tracequery_path) > 0)
10562 modules.push_back (tracequery_path);
c9ccb642
FCE
10563 }
10564 else
10565 uncached_headers.push_back(headers[i]);
10566 }
10567 else
10568 uncached_headers = headers;
f982c59b 10569
c9ccb642
FCE
10570 // If we have nothing left to search for, quit
10571 if (uncached_headers.empty()) return;
55e50c24 10572
c9ccb642 10573 map<string,string> headers_tracequery_src; // header -> C-source code mapping
55e50c24 10574
c9ccb642
FCE
10575 // We could query several subsets of headers[] to make this go
10576 // faster, but let's KISS and do one at a time.
10577 for (size_t i=0; i<uncached_headers.size(); i++)
55e50c24 10578 {
c9ccb642
FCE
10579 const string& header = uncached_headers[i];
10580
10581 // create a tracequery source file
10582 ostringstream osrc;
10583
10584 // PR9993: Add extra headers to work around undeclared types in individual
10585 // include/trace/foo.h files
10586 vector<string> short_decls = tracepoint_extra_decls(s, header);
10587
10588 // add each requested tracepoint header
75ae2ec9 10589 size_t root_pos = header.rfind("include/");
832f100d 10590 short_decls.push_back(string("#include <") +
75ae2ec9 10591 ((root_pos != string::npos) ? header.substr(root_pos + 8) : header) +
d4393459 10592 string(">"));
f982c59b 10593
c9ccb642
FCE
10594 osrc << "#ifdef CONFIG_TRACEPOINTS" << endl;
10595 osrc << "#include <linux/tracepoint.h>" << endl;
832f100d
JS
10596
10597 // the kernel has changed this naming a few times, previously TPPROTO,
10598 // TP_PROTO, TPARGS, TP_ARGS, etc. so let's just dupe the latest.
10599 osrc << "#ifndef PARAMS" << endl;
10600 osrc << "#define PARAMS(args...) args" << endl;
10601 osrc << "#endif" << endl;
10602
c9ccb642
FCE
10603 // override DECLARE_TRACE to synthesize probe functions for us
10604 osrc << "#undef DECLARE_TRACE" << endl;
10605 osrc << "#define DECLARE_TRACE(name, proto, args) \\" << endl;
10606 osrc << " void stapprobe_##name(proto) {}" << endl;
832f100d 10607
c9ccb642
FCE
10608 // 2.6.35 added the NOARGS variant, but it's the same for us
10609 osrc << "#undef DECLARE_TRACE_NOARGS" << endl;
10610 osrc << "#define DECLARE_TRACE_NOARGS(name) \\" << endl;
10611 osrc << " DECLARE_TRACE(name, void, )" << endl;
832f100d
JS
10612
10613 // 2.6.38 added the CONDITION variant, which can also just redirect
10614 osrc << "#undef DECLARE_TRACE_CONDITION" << endl;
10615 osrc << "#define DECLARE_TRACE_CONDITION(name, proto, args, cond) \\" << endl;
10616 osrc << " DECLARE_TRACE(name, PARAMS(proto), PARAMS(args))" << endl;
10617
c9ccb642
FCE
10618 // older tracepoints used DEFINE_TRACE, so redirect that too
10619 osrc << "#undef DEFINE_TRACE" << endl;
10620 osrc << "#define DEFINE_TRACE(name, proto, args) \\" << endl;
832f100d
JS
10621 osrc << " DECLARE_TRACE(name, PARAMS(proto), PARAMS(args))" << endl;
10622
c9ccb642
FCE
10623 // add the specified decls/#includes
10624 for (unsigned z=0; z<short_decls.size(); z++)
10625 osrc << "#undef TRACE_INCLUDE_FILE\n"
10626 << "#undef TRACE_INCLUDE_PATH\n"
10627 << short_decls[z] << "\n";
10628
10629 // finish up the module source
10630 osrc << "#endif /* CONFIG_TRACEPOINTS */" << endl;
47dd066d 10631
c9ccb642
FCE
10632 // save the source file away
10633 headers_tracequery_src[header] = osrc.str();
55e50c24 10634 }
f982c59b 10635
c9ccb642 10636 // now build them all together
2a0e62a8 10637 map<string,string> tracequery_objs = make_tracequeries(s, headers_tracequery_src);
47dd066d 10638
c9ccb642 10639 // now plop them into the cache
b278033a 10640 if (s.use_cache)
c9ccb642
FCE
10641 for (size_t i=0; i<uncached_headers.size(); i++)
10642 {
10643 const string& header = uncached_headers[i];
2a0e62a8
JS
10644 const string& tracequery_obj = tracequery_objs[header];
10645 const string& tracequery_path = headers_cache_obj[header];
10646 if (tracequery_obj !="" && file_exists(tracequery_obj))
c9ccb642 10647 {
2a0e62a8 10648 copy_file(tracequery_obj, tracequery_path, s.verbose > 2);
c9ccb642
FCE
10649 modules.push_back (tracequery_path);
10650 }
c252fca2
JS
10651 else
10652 // cache an empty file for failures
10653 copy_file("/dev/null", tracequery_path, s.verbose > 2);
c9ccb642 10654 }
f982c59b
JS
10655}
10656
10657
d4393459 10658
f982c59b
JS
10659bool
10660tracepoint_builder::init_dw(systemtap_session& s)
10661{
10662 if (dw != NULL)
10663 return true;
10664
10665 vector<string> tracequery_modules;
55e50c24 10666 vector<string> system_headers;
f982c59b
JS
10667
10668 glob_t trace_glob;
d4393459
FCE
10669
10670 // find kernel_source_tree
10671 if (s.kernel_source_tree == "")
f982c59b 10672 {
d4393459 10673 unsigned found;
ccf2c922 10674 Dwfl *dwfl = setup_dwfl_kernel ("kernel", &found, s);
d4393459
FCE
10675 if (found)
10676 {
10677 Dwarf_Die *cudie = 0;
10678 Dwarf_Addr bias;
10679 while ((cudie = dwfl_nextcu (dwfl, cudie, &bias)) != NULL)
10680 {
e19ebcf7 10681 assert_no_interrupts();
d4393459
FCE
10682 Dwarf_Attribute attr;
10683 const char* name = dwarf_formstring (dwarf_attr (cudie, DW_AT_comp_dir, &attr));
36d65b45 10684 if (name)
d4393459 10685 {
36d65b45
JS
10686 // check that the path actually exists locally before we try to use it
10687 if (file_exists(name))
10688 {
10689 if (s.verbose > 2)
10690 clog << _F("Located kernel source tree (DW_AT_comp_dir) at '%s'", name) << endl;
10691 s.kernel_source_tree = name;
10692 }
10693 else
10694 {
10695 if (s.verbose > 2)
10696 clog << _F("Ignoring inaccessible kernel source tree (DW_AT_comp_dir) at '%s'", name) << endl;
10697 }
61f1a63b 10698
d4393459
FCE
10699 break; // skip others; modern Kbuild uses same comp_dir for them all
10700 }
10701 }
10702 }
44392d73 10703 dwfl_end (dwfl);
d4393459
FCE
10704 }
10705
10706 // prefixes
10707 vector<string> glob_prefixes;
10708 glob_prefixes.push_back (s.kernel_build_tree);
10709 if (s.kernel_source_tree != "")
10710 glob_prefixes.push_back (s.kernel_source_tree);
10711
10712 // suffixes
10713 vector<string> glob_suffixes;
10714 glob_suffixes.push_back("include/trace/events/*.h");
10715 glob_suffixes.push_back("include/trace/*.h");
d97d428e 10716 glob_suffixes.push_back("include/ras/*_event.h");
d4393459 10717 glob_suffixes.push_back("arch/x86/kvm/*trace.h");
d97d428e
FCE
10718 glob_suffixes.push_back("arch/x86/kernel/*trace.h");
10719 glob_suffixes.push_back("arch/*/include/asm/trace*.h");
10720 glob_suffixes.push_back("arch/*/include/asm/trace/*.h");
f89ff3e2 10721 glob_suffixes.push_back("fs/xfs/linux-*/xfs_tr*.h");
d97d428e
FCE
10722 glob_suffixes.push_back("fs/*/*trace*.h");
10723 glob_suffixes.push_back("net/*/*trace*.h");
10724 glob_suffixes.push_back("sound/pci/hda/*_trace.h");
10725 glob_suffixes.push_back("drivers/gpu/drm/*_trace.h");
10726 glob_suffixes.push_back("drivers/gpu/drm/*/*_trace.h");
10727 glob_suffixes.push_back("drivers/net/wireless/*/*/*trace*.h");
10728 glob_suffixes.push_back("drivers/usb/host/*trace*.h");
10729
10730 // see also tracepoint_extra_decls above
d4393459
FCE
10731
10732 // compute cartesian product
10733 vector<string> globs;
10734 for (unsigned i=0; i<glob_prefixes.size(); i++)
10735 for (unsigned j=0; j<glob_suffixes.size(); j++)
10736 globs.push_back (glob_prefixes[i]+string("/")+glob_suffixes[j]);
10737
8aa43b8d 10738 set<string> duped_headers;
d4393459
FCE
10739 for (unsigned z = 0; z < globs.size(); z++)
10740 {
10741 string glob_str = globs[z];
10742 if (s.verbose > 3)
b530b5b3 10743 clog << _("Checking tracepoint glob ") << glob_str << endl;
d4393459 10744
067cc66f
CM
10745 int r = glob(glob_str.c_str(), 0, NULL, &trace_glob);
10746 if (r == GLOB_NOSPACE || r == GLOB_ABORTED)
10747 throw runtime_error("Error globbing tracepoint");
10748
f982c59b
JS
10749 for (unsigned i = 0; i < trace_glob.gl_pathc; ++i)
10750 {
10751 string header(trace_glob.gl_pathv[i]);
10752
10753 // filter out a few known "internal-only" headers
60d98537
JS
10754 if (endswith(header, "/define_trace.h") ||
10755 endswith(header, "/ftrace.h") ||
10756 endswith(header, "/trace_events.h") ||
10757 endswith(header, "_event_types.h"))
f982c59b
JS
10758 continue;
10759
b1966849
FCE
10760 // With headers now plopped under arch/FOO/include/asm/*,
10761 // the following logic miss some tracepoints.
10762#if 0
8aa43b8d
JS
10763 // skip identical headers from the build and source trees.
10764 size_t root_pos = header.rfind("include/");
10765 if (root_pos != string::npos &&
10766 !duped_headers.insert(header.substr(root_pos + 8)).second)
10767 continue;
b1966849 10768#endif
8aa43b8d 10769
55e50c24 10770 system_headers.push_back(header);
f982c59b
JS
10771 }
10772 globfree(&trace_glob);
10773 }
10774
c9ccb642
FCE
10775 // Build tracequery modules
10776 get_tracequery_modules(s, system_headers, tracequery_modules);
55e50c24 10777
f982c59b
JS
10778 // TODO: consider other sources of tracepoint headers too, like from
10779 // a command-line parameter or some environment or .systemtaprc
47dd066d 10780
59c11f91 10781 dw = new dwflpp(s, tracequery_modules, true);
0a6f5a3f
JS
10782 return true;
10783}
47dd066d 10784
0a6f5a3f
JS
10785void
10786tracepoint_builder::build(systemtap_session& s,
10787 probe *base, probe_point *location,
10788 literal_map_t const& parameters,
10789 vector<derived_probe*>& finished_results)
10790{
10791 if (!init_dw(s))
10792 return;
47dd066d 10793
75ead1f7
JS
10794 string tracepoint;
10795 assert(get_param (parameters, TOK_TRACE, tracepoint));
47dd066d 10796
75ead1f7 10797 tracepoint_query q(*dw, tracepoint, base, location, finished_results);
d906ab9d 10798 unsigned results_pre = finished_results.size();
06de3a04 10799 dw->iterate_over_modules<base_query>(&query_module, &q);
d906ab9d
JL
10800 unsigned results_post = finished_results.size();
10801
10802 // Did we fail to find a match? Let's suggest something!
10803 if (results_pre == results_post)
10804 {
10805 size_t pos;
10806 string sugs = suggest_dwarf_functions(s, q.visited_modules, tracepoint);
10807 while ((pos = sugs.find("stapprobe_")) != string::npos)
10808 sugs.erase(pos, string("stapprobe_").size());
10809 if (!sugs.empty())
ece93f53
JL
10810 throw SEMANTIC_ERROR (_NF("no match (similar tracepoint: %s)",
10811 "no match (similar tracepoints: %s)",
d906ab9d
JL
10812 sugs.find(',') == string::npos,
10813 sugs.c_str()));
10814 }
47dd066d 10815}
47dd066d 10816
e6fe60e7 10817
b55bc428 10818// ------------------------------------------------------------------------
bd2b1e68 10819// Standard tapset registry.
b55bc428
FCE
10820// ------------------------------------------------------------------------
10821
7a053d3b 10822void
f8220a7b 10823register_standard_tapsets(systemtap_session & s)
b55bc428 10824{
47e0478e 10825 register_tapset_been(s);
93646f4d 10826 register_tapset_itrace(s);
dd0e4fa7 10827 register_tapset_mark(s);
7a212aa8 10828 register_tapset_procfs(s);
912e8c59 10829 register_tapset_timers(s);
8d9609f5 10830 register_tapset_netfilter(s);
b84779a5 10831 register_tapset_utrace(s);
b98a8d73 10832
7a24d422 10833 // dwarf-based kprobe/uprobe parts
c4ce66a1 10834 dwarf_derived_probe::register_patterns(s);
30a279be 10835
888af770
FCE
10836 // XXX: user-space starter set
10837 s.pattern_root->bind_num(TOK_PROCESS)
10838 ->bind_num(TOK_STATEMENT)->bind(TOK_ABSOLUTE)
f66bb29a 10839 ->bind_privilege(pr_all)
888af770
FCE
10840 ->bind(new uprobe_builder ());
10841 s.pattern_root->bind_num(TOK_PROCESS)
10842 ->bind_num(TOK_STATEMENT)->bind(TOK_ABSOLUTE)->bind(TOK_RETURN)
f66bb29a 10843 ->bind_privilege(pr_all)
888af770
FCE
10844 ->bind(new uprobe_builder ());
10845
0a6f5a3f
JS
10846 // kernel tracepoint probes
10847 s.pattern_root->bind(TOK_KERNEL)->bind_str(TOK_TRACE)
10848 ->bind(new tracepoint_builder());
10849
e6fe60e7
AM
10850 // Kprobe based probe
10851 s.pattern_root->bind(TOK_KPROBE)->bind_str(TOK_FUNCTION)
10852 ->bind(new kprobe_builder());
3c57fe1f
JS
10853 s.pattern_root->bind(TOK_KPROBE)->bind_str(TOK_FUNCTION)->bind(TOK_CALL)
10854 ->bind(new kprobe_builder());
e6fe60e7
AM
10855 s.pattern_root->bind(TOK_KPROBE)->bind_str(TOK_MODULE)
10856 ->bind_str(TOK_FUNCTION)->bind(new kprobe_builder());
3c57fe1f
JS
10857 s.pattern_root->bind(TOK_KPROBE)->bind_str(TOK_MODULE)
10858 ->bind_str(TOK_FUNCTION)->bind(TOK_CALL)->bind(new kprobe_builder());
e6fe60e7
AM
10859 s.pattern_root->bind(TOK_KPROBE)->bind_str(TOK_FUNCTION)->bind(TOK_RETURN)
10860 ->bind(new kprobe_builder());
b6371390
JS
10861 s.pattern_root->bind(TOK_KPROBE)->bind_str(TOK_FUNCTION)->bind(TOK_RETURN)
10862 ->bind_num(TOK_MAXACTIVE)->bind(new kprobe_builder());
e6fe60e7
AM
10863 s.pattern_root->bind(TOK_KPROBE)->bind_str(TOK_MODULE)
10864 ->bind_str(TOK_FUNCTION)->bind(TOK_RETURN)->bind(new kprobe_builder());
b6371390
JS
10865 s.pattern_root->bind(TOK_KPROBE)->bind_str(TOK_MODULE)
10866 ->bind_str(TOK_FUNCTION)->bind(TOK_RETURN)
10867 ->bind_num(TOK_MAXACTIVE)->bind(new kprobe_builder());
e6fe60e7
AM
10868 s.pattern_root->bind(TOK_KPROBE)->bind_num(TOK_STATEMENT)
10869 ->bind(TOK_ABSOLUTE)->bind(new kprobe_builder());
dd225250
PS
10870
10871 //Hwbkpt based probe
b47f3a55
FCE
10872 // NB: we formerly registered the probe point types only if the kernel configuration
10873 // allowed it. However, we get better error messages if we allow probes to resolve.
10874 s.pattern_root->bind(TOK_KERNEL)->bind_num(TOK_HWBKPT)
10875 ->bind(TOK_HWBKPT_WRITE)->bind(new hwbkpt_builder());
10876 s.pattern_root->bind(TOK_KERNEL)->bind_str(TOK_HWBKPT)
10877 ->bind(TOK_HWBKPT_WRITE)->bind(new hwbkpt_builder());
10878 s.pattern_root->bind(TOK_KERNEL)->bind_num(TOK_HWBKPT)
10879 ->bind(TOK_HWBKPT_RW)->bind(new hwbkpt_builder());
10880 s.pattern_root->bind(TOK_KERNEL)->bind_str(TOK_HWBKPT)
10881 ->bind(TOK_HWBKPT_RW)->bind(new hwbkpt_builder());
10882 s.pattern_root->bind(TOK_KERNEL)->bind_num(TOK_HWBKPT)
10883 ->bind_num(TOK_LENGTH)->bind(TOK_HWBKPT_WRITE)->bind(new hwbkpt_builder());
10884 s.pattern_root->bind(TOK_KERNEL)->bind_num(TOK_HWBKPT)
10885 ->bind_num(TOK_LENGTH)->bind(TOK_HWBKPT_RW)->bind(new hwbkpt_builder());
10886 // length supported with address only, not symbol names
83ea76b1
WC
10887
10888 //perf event based probe
4763f713 10889 register_tapset_perf(s);
a29858ef 10890 register_tapset_java(s);
b55bc428 10891}
dc38c0ae
DS
10892
10893
b20febf3
FCE
10894vector<derived_probe_group*>
10895all_session_groups(systemtap_session& s)
dc38c0ae 10896{
b20febf3 10897 vector<derived_probe_group*> g;
912e8c59
JS
10898
10899#define DOONE(x) \
10900 if (s. x##_derived_probes) \
10901 g.push_back ((derived_probe_group*)(s. x##_derived_probes))
ab655cf8
DS
10902
10903 // Note that order *is* important here. We want to make sure we
10904 // register (actually run) begin probes before any other probe type
10905 // is run. Similarly, when unregistering probes, we want to
10906 // unregister (actually run) end probes after every other probe type
10907 // has be unregistered. To do the latter,
10908 // c_unparser::emit_module_exit() will run this list backwards.
b20febf3
FCE
10909 DOONE(be);
10910 DOONE(dwarf);
888af770 10911 DOONE(uprobe);
b20febf3
FCE
10912 DOONE(timer);
10913 DOONE(profile);
10914 DOONE(mark);
0a6f5a3f 10915 DOONE(tracepoint);
e6fe60e7 10916 DOONE(kprobe);
dd225250 10917 DOONE(hwbkpt);
83ea76b1 10918 DOONE(perf);
b20febf3 10919 DOONE(hrtimer);
ce82316f 10920 DOONE(procfs);
8d9609f5 10921 DOONE(netfilter);
935447c8
DS
10922
10923 // Another "order is important" item. We want to make sure we
10924 // "register" the dummy task_finder probe group after all probe
10925 // groups that use the task_finder.
10926 DOONE(utrace);
a96d1db0 10927 DOONE(itrace);
f31a77f5 10928 DOONE(dynprobe);
e7d4410d 10929 DOONE(java);
935447c8 10930 DOONE(task_finder);
b20febf3
FCE
10931#undef DOONE
10932 return g;
46b84a80 10933}
73267b89
JS
10934
10935/* vim: set sw=2 ts=8 cino=>4,n-2,{2,^-2,t0,(0,u0,w1,M1 : */
This page took 3.042281 seconds and 5 git commands to generate.