]> sourceware.org Git - systemtap.git/blame - tapsets.cxx
PR12137: Separate the .stapsdt.base logic from probe asm
[systemtap.git] / tapsets.cxx
CommitLineData
56e12059 1// tapset resolution
bbafcb1e 2// Copyright (C) 2005-2010 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"
56e12059 16#include "translate.h"
dc38c0ae 17#include "session.h"
72dbc915 18#include "util.h"
0a6f5a3f 19#include "buildrun.h"
86bf665e 20#include "dwarf_wrappers.h"
2e67a43b 21#include "auto_free.h"
b278033a 22#include "hash.h"
440f755a 23#include "dwflpp.h"
5f8ca04f 24#include "setupdwfl.h"
6b51ee12 25#include <gelf.h>
22d737e8 26
366af4e7 27#include "sys/sdt.h"
22d737e8 28#include "sdt-compat.h"
bd2b1e68 29
3b579393
FCE
30#include <cstdlib>
31#include <algorithm>
bd2b1e68 32#include <deque>
56e12059 33#include <iostream>
bd2b1e68 34#include <map>
ec4373ff 35#include <set>
56e12059 36#include <sstream>
bd2b1e68 37#include <stdexcept>
b55bc428 38#include <vector>
e36387d7 39#include <cstdarg>
29e64872 40#include <cassert>
1969b5bc 41#include <iomanip>
f781f849 42#include <cerrno>
bd2b1e68
GH
43
44extern "C" {
df8fadee 45#include <fcntl.h>
bd2b1e68 46#include <elfutils/libdwfl.h>
7a053d3b 47#include <elfutils/libdw.h>
77de5e9e
GH
48#include <dwarf.h>
49#include <elf.h>
50#include <obstack.h>
b20febf3 51#include <glob.h>
30a279be 52#include <fnmatch.h>
5f0a03a6 53#include <stdio.h>
349dc70e 54#include <sys/types.h>
aaf7ffe8 55#include <math.h>
aff5d390 56#include <regex.h>
4b1ad75e
RM
57
58#define __STDC_FORMAT_MACROS
59#include <inttypes.h>
bd2b1e68 60}
77de5e9e 61
56e12059
FCE
62
63using namespace std;
2171f774 64using namespace __gnu_cxx;
56e12059 65
47dd066d 66
b20febf3
FCE
67
68// ------------------------------------------------------------------------
faea5e16
JS
69
70string
71common_probe_init (derived_probe* p)
72{
26e63673
JS
73 assert(p->session_index != (unsigned)-1);
74 return "(&stap_probes[" + lex_cast(p->session_index) + "])";
faea5e16
JS
75}
76
77
b20febf3 78void
a58d79d0 79common_probe_entryfn_prologue (translator_output* o, string statestr,
faea5e16 80 string probe, bool overload_processing)
b20febf3 81{
653e6a9a
JS
82 o->newline() << "#ifdef STP_ALIBI";
83 o->newline() << "atomic_inc(&(" << probe << "->alibi));";
84 o->newline() << "#else";
85
72d18b98 86 o->newline() << "struct context* __restrict__ c;";
e0a17418
JS
87 o->newline() << "#if !INTERRUPTIBLE";
88 o->newline() << "unsigned long flags;";
89 o->newline() << "#endif";
b20febf3 90
a58d79d0
DS
91 if (overload_processing)
92 o->newline() << "#if defined(STP_TIMING) || defined(STP_OVERLOAD)";
93 else
94 o->newline() << "#ifdef STP_TIMING";
95 o->newline() << "cycles_t cycles_atstart = get_cycles ();";
b20febf3 96 o->newline() << "#endif";
b20febf3 97
994aac0e 98 o->newline() << "#ifdef STP_TIMING";
26e63673 99 o->newline() << "Stat stat = " << probe << "->timing;";
994aac0e
JS
100 o->newline() << "#endif";
101
e0a17418
JS
102 o->newline() << "#if INTERRUPTIBLE";
103 o->newline() << "preempt_disable ();";
104 o->newline() << "#else";
105 o->newline() << "local_irq_save (flags);";
106 o->newline() << "#endif";
b20febf3 107
c931ec8a 108 // Check for enough free enough stack space
d05a1d00 109 o->newline() << "if (unlikely ((((unsigned long) (& c)) & (THREAD_SIZE-1))"; // free space
a63401b1 110 o->newline(1) << "< (MINSTACKSPACE + sizeof (struct thread_info)))) {"; // needed space
d05a1d00
FCE
111 // XXX: may need porting to platforms where task_struct is not at bottom of kernel stack
112 // NB: see also CONFIG_DEBUG_STACKOVERFLOW
b3c3ca7c
FCE
113 o->newline() << "atomic_inc (& skipped_count);";
114 o->newline() << "#ifdef STP_TIMING";
115 o->newline() << "atomic_inc (& skipped_count_lowstack);";
116 o->newline() << "#endif";
c931ec8a
FCE
117 o->newline() << "goto probe_epilogue;";
118 o->newline(-1) << "}";
119
b20febf3
FCE
120 o->newline() << "if (atomic_read (&session_state) != " << statestr << ")";
121 o->newline(1) << "goto probe_epilogue;";
122 o->indent(-1);
9a604fac 123
4a0ae64c 124 o->newline() << "c = contexts[smp_processor_id()];";
b3c3ca7c 125 o->newline() << "if (atomic_inc_return (& c->busy) != 1) {";
9c736061
FCE
126 o->newline(1) << "#if !INTERRUPTIBLE";
127 o->newline() << "atomic_inc (& skipped_count);";
128 o->newline() << "#endif";
b3c3ca7c
FCE
129 o->newline() << "#ifdef STP_TIMING";
130 o->newline() << "atomic_inc (& skipped_count_reentrant);";
c12d974f
FCE
131 o->newline() << "#ifdef DEBUG_REENTRANCY";
132 o->newline() << "_stp_warn (\"Skipped %s due to %s residency on cpu %u\\n\", "
26e63673 133 << probe << "->pp, c->probe_point ?: \"?\", smp_processor_id());";
c12d974f
FCE
134 // NB: There is a conceivable race condition here with reading
135 // c->probe_point, knowing that this other probe is sort of running.
136 // However, in reality, it's interrupted. Plus even if it were able
137 // to somehow start again, and stop before we read c->probe_point,
138 // at least we have that ?: "?" bit in there to avoid a NULL deref.
139 o->newline() << "#endif";
b3c3ca7c 140 o->newline() << "#endif";
9a604fac 141 o->newline() << "atomic_dec (& c->busy);";
b20febf3 142 o->newline() << "goto probe_epilogue;";
9a604fac
FCE
143 o->newline(-1) << "}";
144 o->newline();
1e00cfb1 145 o->newline() << "c->last_stmt = 0;";
9a604fac 146 o->newline() << "c->last_error = 0;";
a7ed0d3e 147 o->newline() << "c->nesting = -1;"; // NB: PR10516 packs locals[] tighter
22f8b401 148 o->newline() << "c->regs = 0;";
d4670309 149 o->newline() << "#if defined __ia64__";
b916df9c 150 o->newline() << "c->unwaddr = 0;";
d4670309 151 o->newline() << "#endif";
26e63673 152 o->newline() << "c->probe_point = " << probe << "->pp;";
d48df0cf 153 o->newline() << "#ifdef STP_NEED_PROBE_NAME";
26e63673 154 o->newline() << "c->probe_name = " << probe << "->pn;";
2d767770 155 o->newline() << "#else";
d48df0cf 156 o->newline() << "c->probe_name = 0;";
2d767770 157 o->newline() << "#endif";
b916df9c 158 // reset unwound address cache
fcff848e 159 o->newline() << "c->pi = 0;";
af234c40 160 o->newline() << "c->pi_longs = 0;";
97cd9334 161 o->newline() << "c->regflags = 0;";
9addf322 162 o->newline() << "c->regparm = 0;";
bc54e71c
MH
163 o->newline() << "c->marker_name = NULL;";
164 o->newline() << "c->marker_format = NULL;";
e0a17418
JS
165
166 o->newline() << "#if INTERRUPTIBLE";
167 o->newline() << "c->actionremaining = MAXACTION_INTERRUPTIBLE;";
168 o->newline() << "#else";
169 o->newline() << "c->actionremaining = MAXACTION;";
170 o->newline() << "#endif";
5e562a69 171 o->newline() << "c->ri = 0;";
9915575b
FCE
172 // NB: The following would actually be incorrect.
173 // That's because cycles_sum/cycles_base values are supposed to survive
174 // between consecutive probes. Periodically (STP_OVERLOAD_INTERVAL
175 // cycles), the values will be reset.
176 /*
f0e6dc63
FCE
177 o->newline() << "#ifdef STP_OVERLOAD";
178 o->newline() << "c->cycles_sum = 0;";
179 o->newline() << "c->cycles_base = 0;";
41c262f3 180 o->newline() << "#endif";
9915575b 181 */
b20febf3 182}
9a604fac 183
a44a0785 184
b20febf3 185void
a58d79d0 186common_probe_entryfn_epilogue (translator_output* o,
912e8c59 187 bool overload_processing)
b20febf3 188{
a58d79d0
DS
189 if (overload_processing)
190 o->newline() << "#if defined(STP_TIMING) || defined(STP_OVERLOAD)";
191 else
192 o->newline() << "#ifdef STP_TIMING";
dbb68664 193 o->newline() << "{";
a58d79d0
DS
194 o->newline(1) << "cycles_t cycles_atend = get_cycles ();";
195 // NB: we truncate cycles counts to 32 bits. Perhaps it should be
196 // fewer, if the hardware counter rolls over really quickly. We
197 // handle 32-bit wraparound here.
198 o->newline() << "int32_t cycles_elapsed = ((int32_t)cycles_atend > (int32_t)cycles_atstart)";
199 o->newline(1) << "? ((int32_t)cycles_atend - (int32_t)cycles_atstart)";
200 o->newline() << ": (~(int32_t)0) - (int32_t)cycles_atstart + (int32_t)cycles_atend + 1;";
201 o->indent(-1);
dbb68664 202
a58d79d0 203 o->newline() << "#ifdef STP_TIMING";
994aac0e 204 o->newline() << "if (likely (stat)) _stp_stat_add(stat, cycles_elapsed);";
a58d79d0
DS
205 o->newline() << "#endif";
206
207 if (overload_processing)
208 {
209 o->newline() << "#ifdef STP_OVERLOAD";
210 o->newline() << "{";
211 // If the cycle count has wrapped (cycles_atend > cycles_base),
212 // let's go ahead and pretend the interval has been reached.
213 // This should reset cycles_base and cycles_sum.
214 o->newline(1) << "cycles_t interval = (cycles_atend > c->cycles_base)";
215 o->newline(1) << "? (cycles_atend - c->cycles_base)";
216 o->newline() << ": (STP_OVERLOAD_INTERVAL + 1);";
217 o->newline(-1) << "c->cycles_sum += cycles_elapsed;";
218
219 // If we've spent more than STP_OVERLOAD_THRESHOLD cycles in a
220 // probe during the last STP_OVERLOAD_INTERVAL cycles, the probe
221 // has overloaded the system and we need to quit.
222 o->newline() << "if (interval > STP_OVERLOAD_INTERVAL) {";
223 o->newline(1) << "if (c->cycles_sum > STP_OVERLOAD_THRESHOLD) {";
224 o->newline(1) << "_stp_error (\"probe overhead exceeded threshold\");";
225 o->newline() << "atomic_set (&session_state, STAP_SESSION_ERROR);";
551e9f14 226 o->newline() << "atomic_inc (&error_count);";
a58d79d0 227 o->newline(-1) << "}";
e57b735a 228
a58d79d0
DS
229 o->newline() << "c->cycles_base = cycles_atend;";
230 o->newline() << "c->cycles_sum = 0;";
231 o->newline(-1) << "}";
232 o->newline(-1) << "}";
233 o->newline() << "#endif";
234 }
e57b735a 235
440f755a
JS
236 o->newline(-1) << "}";
237 o->newline() << "#endif";
e57b735a 238
440f755a 239 o->newline() << "c->probe_point = 0;"; // vacated
d48df0cf 240 o->newline() << "c->probe_name = 0;";
440f755a
JS
241 o->newline() << "if (unlikely (c->last_error && c->last_error[0])) {";
242 o->newline(1) << "if (c->last_stmt != NULL)";
243 o->newline(1) << "_stp_softerror (\"%s near %s\", c->last_error, c->last_stmt);";
244 o->newline(-1) << "else";
245 o->newline(1) << "_stp_softerror (\"%s\", c->last_error);";
246 o->indent(-1);
247 o->newline() << "atomic_inc (& error_count);";
248 o->newline() << "if (atomic_read (& error_count) > MAXERRORS) {";
249 o->newline(1) << "atomic_set (& session_state, STAP_SESSION_ERROR);";
250 o->newline() << "_stp_exit ();";
251 o->newline(-1) << "}";
252 o->newline(-1) << "}";
253 o->newline() << "atomic_dec (&c->busy);";
e57b735a 254
440f755a
JS
255 o->newline(-1) << "probe_epilogue:"; // context is free
256 o->indent(1);
e57b735a 257
440f755a
JS
258 // Check for excessive skip counts.
259 o->newline() << "if (unlikely (atomic_read (& skipped_count) > MAXSKIPPED)) {";
079915a5 260 o->newline(1) << "if (unlikely (pseudo_atomic_cmpxchg(& session_state, STAP_SESSION_RUNNING, STAP_SESSION_ERROR) == STAP_SESSION_RUNNING))";
f65166cc 261 o->newline() << "_stp_error (\"Skipped too many probes, check MAXSKIPPED or try again with stap -t for more details.\");";
440f755a 262 o->newline(-1) << "}";
e57b735a 263
440f755a
JS
264 o->newline() << "#if INTERRUPTIBLE";
265 o->newline() << "preempt_enable_no_resched ();";
266 o->newline() << "#else";
267 o->newline() << "local_irq_restore (flags);";
268 o->newline() << "#endif";
653e6a9a
JS
269
270 o->newline() << "#endif // STP_ALIBI";
440f755a 271}
e57b735a 272
e57b735a 273
440f755a 274// ------------------------------------------------------------------------
e57b735a 275
440f755a
JS
276// ------------------------------------------------------------------------
277// Dwarf derived probes. "We apologize for the inconvience."
278// ------------------------------------------------------------------------
e57b735a 279
4627ed58
JS
280static const string TOK_KERNEL("kernel");
281static const string TOK_MODULE("module");
282static const string TOK_FUNCTION("function");
283static const string TOK_INLINE("inline");
284static const string TOK_CALL("call");
285static const string TOK_RETURN("return");
286static const string TOK_MAXACTIVE("maxactive");
287static const string TOK_STATEMENT("statement");
288static const string TOK_ABSOLUTE("absolute");
289static const string TOK_PROCESS("process");
a794dbeb 290static const string TOK_PROVIDER("provider");
4627ed58
JS
291static const string TOK_MARK("mark");
292static const string TOK_TRACE("trace");
293static const string TOK_LABEL("label");
63b4fd14 294static const string TOK_LIBRARY("library");
e57b735a 295
1adf8ef1 296static int query_cu (Dwarf_Die * cudie, void * arg);
6b517475 297static void query_addr(Dwarf_Addr addr, dwarf_query *q);
e57b735a 298
440f755a
JS
299// Can we handle this query with just symbol-table info?
300enum dbinfo_reqt
301{
302 dbr_unknown,
303 dbr_none, // kernel.statement(NUM).absolute
304 dbr_need_symtab, // can get by with symbol table if there's no dwarf
305 dbr_need_dwarf
306};
e57b735a 307
20e4a32c 308
440f755a
JS
309struct base_query; // forward decls
310struct dwarf_query;
311struct dwflpp;
312struct symbol_table;
20e4a32c 313
a781f401 314
440f755a
JS
315struct
316symbol_table
317{
318 module_info *mod_info; // associated module
319 map<string, func_info*> map_by_name;
1c6b77e5
JS
320 multimap<Dwarf_Addr, func_info*> map_by_addr;
321 typedef multimap<Dwarf_Addr, func_info*>::iterator iterator_t;
440f755a
JS
322 typedef pair<iterator_t, iterator_t> range_t;
323#ifdef __powerpc__
324 GElf_Word opd_section;
325#endif
2867a2a1
JS
326 void add_symbol(const char *name, bool weak, bool descriptor,
327 Dwarf_Addr addr, Dwarf_Addr *high_addr);
440f755a 328 enum info_status read_symbols(FILE *f, const string& path);
83ca3872
MW
329 enum info_status read_from_elf_file(const string& path,
330 const systemtap_session &sess);
331 enum info_status read_from_text_file(const string& path,
332 const systemtap_session &sess);
440f755a
JS
333 enum info_status get_from_elf();
334 void prepare_section_rejection(Dwfl_Module *mod);
335 bool reject_section(GElf_Word section);
440f755a
JS
336 void purge_syscall_stubs();
337 func_info *lookup_symbol(const string& name);
338 Dwarf_Addr lookup_symbol_address(const string& name);
339 func_info *get_func_containing_address(Dwarf_Addr addr);
7a053d3b 340
440f755a
JS
341 symbol_table(module_info *mi) : mod_info(mi) {}
342 ~symbol_table();
343};
77de5e9e 344
440f755a
JS
345static bool null_die(Dwarf_Die *die)
346{
347 static Dwarf_Die null = { 0 };
348 return (!die || !memcmp(die, &null, sizeof(null)));
349}
c4ce66a1
JS
350
351
7a053d3b 352enum
bd2b1e68 353function_spec_type
7a053d3b 354 {
bd2b1e68
GH
355 function_alone,
356 function_and_file,
7a053d3b 357 function_file_and_line
bd2b1e68
GH
358 };
359
ec4373ff 360
bd2b1e68 361struct dwarf_builder;
f10534c6 362struct dwarf_var_expanding_visitor;
77de5e9e 363
2930abc7 364
b20febf3
FCE
365// XXX: This class is a candidate for subclassing to separate
366// the relocation vs non-relocation variants. Likewise for
367// kprobe vs kretprobe variants.
368
369struct dwarf_derived_probe: public derived_probe
b55bc428 370{
b20febf3
FCE
371 dwarf_derived_probe (const string& function,
372 const string& filename,
373 int line,
374 const string& module,
375 const string& section,
376 Dwarf_Addr dwfl_addr,
2930abc7 377 Dwarf_Addr addr,
b20febf3
FCE
378 dwarf_query & q,
379 Dwarf_Die* scope_die);
20e4a32c 380
b20febf3
FCE
381 string module;
382 string section;
383 Dwarf_Addr addr;
63b4fd14 384 string path;
27dc09b1 385 bool has_process;
2930abc7 386 bool has_return;
c9bad430 387 bool has_maxactive;
63b4fd14 388 bool has_library;
c9bad430 389 long maxactive_val;
b642c901
SC
390 string user_path;
391 string user_lib;
b95e2b79 392 bool access_vars;
2930abc7 393
af234c40
JS
394 unsigned saved_longs, saved_strings;
395 dwarf_derived_probe* entry_handler;
396
b8da0ad1 397 void printsig (std::ostream &o) const;
6b66b9f7 398 virtual void join_group (systemtap_session& s);
9020300d 399 void emit_probe_local_init(translator_output * o);
d0bfd2ac 400 void getargs(std::list<std::string> &arg_set) const;
0a98fd42 401
27dc09b1
DB
402 void emit_unprivileged_assertion (translator_output*);
403 void print_dupe_stamp(ostream& o);
404
bd2b1e68 405 // Pattern registration helpers.
7a053d3b 406 static void register_statement_variants(match_node * root,
27dc09b1
DB
407 dwarf_builder * dw,
408 bool bind_unprivileged_p = false);
fd6602a0 409 static void register_function_variants(match_node * root,
27dc09b1
DB
410 dwarf_builder * dw,
411 bool bind_unprivileged_p = false);
7a053d3b 412 static void register_function_and_statement_variants(match_node * root,
27dc09b1
DB
413 dwarf_builder * dw,
414 bool bind_unprivileged_p = false);
c4ce66a1 415 static void register_patterns(systemtap_session& s);
6b66b9f7
JS
416
417protected:
418 dwarf_derived_probe(probe *base,
419 probe_point *location,
420 Dwarf_Addr addr,
421 bool has_return):
422 derived_probe(base, location), addr(addr), has_return(has_return),
af234c40
JS
423 has_maxactive(0), maxactive_val(0), access_vars(false),
424 saved_longs(0), saved_strings(0), entry_handler(0)
6b66b9f7
JS
425 {}
426
427private:
d0bfd2ac 428 list<string> args;
8c67c337 429 void saveargs(dwarf_query& q, Dwarf_Die* scope_die, Dwarf_Addr dwfl_addr);
20c6c071
GH
430};
431
dc38c0ae 432
6b66b9f7 433struct uprobe_derived_probe: public dwarf_derived_probe
6d0f3f0c 434{
6d0f3f0c 435 int pid; // 0 => unrestricted
0973d815 436
6d0f3f0c
FCE
437 uprobe_derived_probe (const string& function,
438 const string& filename,
439 int line,
440 const string& module,
6d0f3f0c
FCE
441 const string& section,
442 Dwarf_Addr dwfl_addr,
443 Dwarf_Addr addr,
444 dwarf_query & q,
6b66b9f7
JS
445 Dwarf_Die* scope_die):
446 dwarf_derived_probe(function, filename, line, module, section,
447 dwfl_addr, addr, q, scope_die), pid(0)
448 {}
6d0f3f0c 449
0973d815
FCE
450 // alternate constructor for process(PID).statement(ADDR).absolute
451 uprobe_derived_probe (probe *base,
452 probe_point *location,
453 int pid,
454 Dwarf_Addr addr,
6b66b9f7
JS
455 bool has_return):
456 dwarf_derived_probe(base, location, addr, has_return), pid(pid)
457 {}
9ace370f 458
6d0f3f0c 459 void join_group (systemtap_session& s);
2865d17a
DB
460
461 void emit_unprivileged_assertion (translator_output*);
8f6d8c2b 462 void print_dupe_stamp(ostream& o) { print_dupe_stamp_unprivileged_process_owner (o); }
c0f84e7b
SC
463 void getargs(std::list<std::string> &arg_set) const;
464 void saveargs(int nargs);
465private:
466 list<string> args;
6d0f3f0c
FCE
467};
468
dc38c0ae
DS
469struct dwarf_derived_probe_group: public derived_probe_group
470{
471private:
b20febf3
FCE
472 multimap<string,dwarf_derived_probe*> probes_by_module;
473 typedef multimap<string,dwarf_derived_probe*>::iterator p_b_m_iterator;
dc38c0ae
DS
474
475public:
b20febf3
FCE
476 void enroll (dwarf_derived_probe* probe);
477 void emit_module_decls (systemtap_session& s);
478 void emit_module_init (systemtap_session& s);
479 void emit_module_exit (systemtap_session& s);
dc38c0ae
DS
480};
481
482
20c6c071 483// Helper struct to thread through the dwfl callbacks.
2c384610 484struct base_query
20c6c071 485{
c4ce66a1
JS
486 base_query(dwflpp & dw, literal_map_t const & params);
487 base_query(dwflpp & dw, const string & module_val);
2c384610 488 virtual ~base_query() {}
bd2b1e68 489
5227f1ea 490 systemtap_session & sess;
2c384610 491 dwflpp & dw;
5227f1ea 492
bd2b1e68 493 // Parameter extractors.
86bf665e 494 static bool has_null_param(literal_map_t const & params,
888af770 495 string const & k);
86bf665e 496 static bool get_string_param(literal_map_t const & params,
bd2b1e68 497 string const & k, string & v);
86bf665e 498 static bool get_number_param(literal_map_t const & params,
bd2b1e68 499 string const & k, long & v);
86bf665e 500 static bool get_number_param(literal_map_t const & params,
c239d28c 501 string const & k, Dwarf_Addr & v);
b55bc428 502
2c384610
DS
503 // Extracted parameters.
504 bool has_kernel;
91af0778
FCE
505 bool has_module;
506 bool has_process;
63b4fd14 507 bool has_library;
2c384610 508 string module_val; // has_kernel => module_val = "kernel"
63b4fd14 509 string path; // executable path if module is a .so
2c384610
DS
510
511 virtual void handle_query_module() = 0;
512};
513
514
c4ce66a1
JS
515base_query::base_query(dwflpp & dw, literal_map_t const & params):
516 sess(dw.sess), dw(dw)
2c384610 517{
91af0778 518 has_kernel = has_null_param (params, TOK_KERNEL);
2c384610
DS
519 if (has_kernel)
520 module_val = "kernel";
91af0778
FCE
521
522 has_module = get_string_param (params, TOK_MODULE, module_val);
523 if (has_module)
524 has_process = false;
4baf0e53 525 else
d0a7f5a9 526 {
63b4fd14 527 string library_name;
d0a7f5a9 528 has_process = get_string_param(params, TOK_PROCESS, module_val);
63b4fd14
SC
529 has_library = get_string_param (params, TOK_LIBRARY, library_name);
530 if (has_library)
531 {
532 path = find_executable (module_val);
533 module_val = find_executable (library_name, "LD_LIBRARY_PATH");
534 }
535 else if (has_process)
d0a7f5a9
FCE
536 module_val = find_executable (module_val);
537 }
91af0778
FCE
538
539 assert (has_kernel || has_process || has_module);
2c384610
DS
540}
541
c4ce66a1
JS
542base_query::base_query(dwflpp & dw, const string & module_val)
543 : sess(dw.sess), dw(dw), module_val(module_val)
544{
545 // NB: This uses '/' to distinguish between kernel modules and userspace,
546 // which means that userspace modules won't get any PATH searching.
547 if (module_val.find('/') == string::npos)
548 {
549 has_kernel = (module_val == TOK_KERNEL);
550 has_module = !has_kernel;
551 has_process = false;
552 }
553 else
554 {
555 has_kernel = has_module = false;
556 has_process = true;
557 }
558}
559
2c384610 560bool
86bf665e 561base_query::has_null_param(literal_map_t const & params,
2c384610
DS
562 string const & k)
563{
888af770 564 return derived_probe_builder::has_null_param(params, k);
2c384610
DS
565}
566
567
568bool
86bf665e 569base_query::get_string_param(literal_map_t const & params,
2c384610
DS
570 string const & k, string & v)
571{
572 return derived_probe_builder::get_param (params, k, v);
573}
574
575
576bool
86bf665e 577base_query::get_number_param(literal_map_t const & params,
2c384610
DS
578 string const & k, long & v)
579{
580 int64_t value;
581 bool present = derived_probe_builder::get_param (params, k, value);
582 v = (long) value;
583 return present;
584}
585
586
587bool
86bf665e 588base_query::get_number_param(literal_map_t const & params,
2c384610
DS
589 string const & k, Dwarf_Addr & v)
590{
591 int64_t value;
592 bool present = derived_probe_builder::get_param (params, k, value);
593 v = (Dwarf_Addr) value;
594 return present;
595}
596
2c384610
DS
597struct dwarf_query : public base_query
598{
e1278bd4 599 dwarf_query(probe * base_probe,
2c384610
DS
600 probe_point * base_loc,
601 dwflpp & dw,
86bf665e 602 literal_map_t const & params,
b642c901
SC
603 vector<derived_probe *> & results,
604 const string user_path,
605 const string user_lib);
2c384610 606
c4ce66a1 607 vector<derived_probe *> & results;
8f14e444 608 set<string> inlined_non_returnable; // function names
c4ce66a1
JS
609 probe * base_probe;
610 probe_point * base_loc;
b642c901
SC
611 string user_path;
612 string user_lib;
c4ce66a1 613
2c384610 614 virtual void handle_query_module();
5f0a03a6
JK
615 void query_module_dwarf();
616 void query_module_symtab();
2c384610 617
2930abc7
FCE
618 void add_probe_point(string const & funcname,
619 char const * filename,
620 int line,
621 Dwarf_Die *scope_die,
622 Dwarf_Addr addr);
36f9dd1d 623
857bdfd1
JS
624 // Track addresses we've already seen in a given module
625 set<Dwarf_Addr> alias_dupes;
626
7fdd3e2c
JS
627 // Track inlines we've already seen as well
628 // NB: this can't be compared just by entrypc, as inlines can overlap
629 set<inline_instance_info> inline_dupes;
630
2930abc7 631 // Extracted parameters.
7a053d3b 632 string function_val;
20c6c071
GH
633
634 bool has_function_str;
635 bool has_statement_str;
636 bool has_function_num;
637 bool has_statement_num;
7a053d3b
RM
638 string statement_str_val;
639 string function_str_val;
c239d28c
GH
640 Dwarf_Addr statement_num_val;
641 Dwarf_Addr function_num_val;
20c6c071 642
b8da0ad1
FCE
643 bool has_call;
644 bool has_inline;
20c6c071
GH
645 bool has_return;
646
c9bad430
DS
647 bool has_maxactive;
648 long maxactive_val;
649
20c6c071
GH
650 bool has_label;
651 string label_val;
652
653 bool has_relative;
654 long relative_val;
655
37ebca01
FCE
656 bool has_absolute;
657
467bea43
SC
658 bool has_mark;
659
5f0a03a6
JK
660 enum dbinfo_reqt dbinfo_reqt;
661 enum dbinfo_reqt assess_dbinfo_reqt();
662
7d6d0afc 663 void parse_function_spec(const string & spec);
20c6c071 664 function_spec_type spec_type;
7d6d0afc 665 vector<string> scopes;
20c6c071
GH
666 string function;
667 string file;
0c8b7d37 668 line_t line_type;
879eb9e9 669 int line[2];
5f0a03a6 670 bool query_done; // Found exact match
20c6c071 671
bd25380d 672 set<string> filtered_srcfiles;
7e1279ea
FCE
673
674 // Map official entrypc -> func_info object
86bf665e
TM
675 inline_instance_map_t filtered_inlines;
676 func_info_map_t filtered_functions;
7e1279ea
FCE
677 bool choose_next_line;
678 Dwarf_Addr entrypc_for_next_line;
4df79aaf
JS
679
680 void query_module_functions ();
b55bc428
FCE
681};
682
98afd80e
FCE
683
684struct dwarf_builder: public derived_probe_builder
b55bc428 685{
665e1256 686 map <string,dwflpp*> kern_dw; /* NB: key string could be a wildcard */
7a24d422 687 map <string,dwflpp*> user_dw;
b642c901
SC
688 string user_path;
689 string user_lib;
ae2552da 690 dwarf_builder() {}
aa30ccd3 691
ae2552da 692 dwflpp *get_kern_dw(systemtap_session& sess, const string& module)
707bf35e 693 {
ea14cf67
FCE
694 if (kern_dw[module] == 0)
695 kern_dw[module] = new dwflpp(sess, module, true); // might throw
ae2552da 696 return kern_dw[module];
707bf35e
JS
697 }
698
699 dwflpp *get_user_dw(systemtap_session& sess, const string& module)
700 {
ea14cf67
FCE
701 if (user_dw[module] == 0)
702 user_dw[module] = new dwflpp(sess, module, false); // might throw
707bf35e
JS
703 return user_dw[module];
704 }
7a24d422
FCE
705
706 /* NB: not virtual, so can be called from dtor too: */
06aca46a 707 void dwarf_build_no_more (bool verbose)
aa30ccd3 708 {
ae2552da
FCE
709 for (map<string,dwflpp*>::iterator udi = kern_dw.begin();
710 udi != kern_dw.end();
711 udi ++)
aa30ccd3 712 {
7a24d422 713 if (verbose)
ae2552da
FCE
714 clog << "dwarf_builder releasing kernel dwflpp " << udi->first << endl;
715 delete udi->second;
aa30ccd3 716 }
ae2552da 717 kern_dw.erase (kern_dw.begin(), kern_dw.end());
7a24d422
FCE
718
719 for (map<string,dwflpp*>::iterator udi = user_dw.begin();
720 udi != user_dw.end();
721 udi ++)
722 {
723 if (verbose)
724 clog << "dwarf_builder releasing user dwflpp " << udi->first << endl;
725 delete udi->second;
726 }
727 user_dw.erase (user_dw.begin(), user_dw.end());
728 }
729
730 void build_no_more (systemtap_session &s)
731 {
732 dwarf_build_no_more (s.verbose > 3);
aa30ccd3
FCE
733 }
734
e38d6504
RM
735 ~dwarf_builder()
736 {
7a24d422 737 dwarf_build_no_more (false);
c8959a29 738 }
aa30ccd3 739
5227f1ea 740 virtual void build(systemtap_session & sess,
7a053d3b 741 probe * base,
20c6c071 742 probe_point * location,
86bf665e 743 literal_map_t const & parameters,
20c6c071 744 vector<derived_probe *> & finished_results);
b55bc428
FCE
745};
746
5111fc3e 747
e1278bd4 748dwarf_query::dwarf_query(probe * base_probe,
20c6c071
GH
749 probe_point * base_loc,
750 dwflpp & dw,
86bf665e 751 literal_map_t const & params,
b642c901
SC
752 vector<derived_probe *> & results,
753 const string user_path,
754 const string user_lib)
c4ce66a1 755 : base_query(dw, params), results(results),
b642c901
SC
756 base_probe(base_probe), base_loc(base_loc),
757 user_path(user_path), user_lib(user_lib)
bd2b1e68
GH
758{
759 // Reduce the query to more reasonable semantic values (booleans,
760 // extracted strings, numbers, etc).
bd2b1e68
GH
761 has_function_str = get_string_param(params, TOK_FUNCTION, function_str_val);
762 has_function_num = get_number_param(params, TOK_FUNCTION, function_num_val);
763
764 has_statement_str = get_string_param(params, TOK_STATEMENT, statement_str_val);
765 has_statement_num = get_number_param(params, TOK_STATEMENT, statement_num_val);
766
0f336e95
SC
767 has_label = get_string_param(params, TOK_LABEL, label_val);
768
b8da0ad1
FCE
769 has_call = has_null_param(params, TOK_CALL);
770 has_inline = has_null_param(params, TOK_INLINE);
bd2b1e68 771 has_return = has_null_param(params, TOK_RETURN);
c9bad430 772 has_maxactive = get_number_param(params, TOK_MAXACTIVE, maxactive_val);
37ebca01 773 has_absolute = has_null_param(params, TOK_ABSOLUTE);
467bea43 774 has_mark = false;
37ebca01 775
bd2b1e68 776 if (has_function_str)
7d6d0afc 777 parse_function_spec(function_str_val);
bd2b1e68 778 else if (has_statement_str)
7d6d0afc 779 parse_function_spec(statement_str_val);
0daad364 780
5f0a03a6
JK
781 dbinfo_reqt = assess_dbinfo_reqt();
782 query_done = false;
0daad364
JS
783}
784
785
440f755a
JS
786func_info_map_t *
787get_filtered_functions(dwarf_query *q)
788{
789 return &q->filtered_functions;
790}
791
792
793inline_instance_map_t *
794get_filtered_inlines(dwarf_query *q)
795{
796 return &q->filtered_inlines;
797}
798
799
2c384610 800void
5f0a03a6 801dwarf_query::query_module_dwarf()
2c384610
DS
802{
803 if (has_function_num || has_statement_num)
804 {
805 // If we have module("foo").function(0xbeef) or
806 // module("foo").statement(0xbeef), the address is relative
807 // to the start of the module, so we seek the function
808 // number plus the module's bias.
6b517475
JS
809 Dwarf_Addr addr = has_function_num ?
810 function_num_val : statement_num_val;
08d1d520
MW
811
812 // These are raw addresses, we need to know what the elf_bias
813 // is to feed it to libdwfl based functions.
814 Dwarf_Addr elf_bias;
815 Elf *elf = dwfl_module_getelf (dw.module, &elf_bias);
816 assert(elf);
817 addr += elf_bias;
6b517475 818 query_addr(addr, this);
2c384610
DS
819 }
820 else
821 {
822 // Otherwise if we have a function("foo") or statement("foo")
823 // specifier, we have to scan over all the CUs looking for
824 // the function(s) in question
825 assert(has_function_str || has_statement_str);
4df79aaf
JS
826
827 // For simple cases, no wildcard and no source:line, we can do a very
828 // quick function lookup in a module-wide cache.
1ffb8bd1
JS
829 if (spec_type == function_alone &&
830 !dw.name_has_wildcard(function) &&
831 !startswith(function, "_Z"))
4df79aaf
JS
832 query_module_functions();
833 else
834 dw.iterate_over_cus(&query_cu, this);
2c384610
DS
835 }
836}
837
5f0a03a6
JK
838static void query_func_info (Dwarf_Addr entrypc, func_info & fi,
839 dwarf_query * q);
840
841void
842dwarf_query::query_module_symtab()
843{
844 // Get the symbol table if it's necessary, sufficient, and not already got.
845 if (dbinfo_reqt == dbr_need_dwarf)
846 return;
847
848 module_info *mi = dw.mod_info;
849 if (dbinfo_reqt == dbr_need_symtab)
850 {
851 if (mi->symtab_status == info_unknown)
852 mi->get_symtab(this);
853 if (mi->symtab_status == info_absent)
854 return;
855 }
856
857 func_info *fi = NULL;
858 symbol_table *sym_table = mi->sym_table;
859
860 if (has_function_str)
861 {
862 // Per dwarf_query::assess_dbinfo_reqt()...
863 assert(spec_type == function_alone);
864 if (dw.name_has_wildcard(function_str_val))
865 {
866 // Until we augment the blacklist sufficently...
867 if (function_str_val.find_first_not_of("*?") == string::npos)
868 {
869 // e.g., kernel.function("*")
870 cerr << "Error: Pattern '"
871 << function_str_val
872 << "' matches every instruction address in the symbol table,"
873 << endl
874 << "some of which aren't even functions."
875 << " Please be more precise."
876 << endl;
877 return;
878 }
2e67a43b 879 symbol_table::iterator_t iter;
1c6b77e5
JS
880 for (iter = sym_table->map_by_addr.begin();
881 iter != sym_table->map_by_addr.end();
2e67a43b 882 ++iter)
5f0a03a6 883 {
1c6b77e5 884 fi = iter->second;
5f0a03a6
JK
885 if (!null_die(&fi->die))
886 continue; // already handled in query_module_dwarf()
887 if (dw.function_name_matches_pattern(fi->name, function_str_val))
888 query_func_info(fi->addr, *fi, this);
889 }
890 }
891 else
892 {
893 fi = sym_table->lookup_symbol(function_str_val);
2867a2a1 894 if (fi && !fi->descriptor && null_die(&fi->die))
5f0a03a6
JK
895 query_func_info(fi->addr, *fi, this);
896 }
897 }
898 else
899 {
900 assert(has_function_num || has_statement_num);
901 // Find the "function" in which the indicated address resides.
902 Dwarf_Addr addr =
903 (has_function_num ? function_num_val : statement_num_val);
904 fi = sym_table->get_func_containing_address(addr);
905 if (!fi)
906 {
83ca3872
MW
907 if (! sess.suppress_warnings)
908 cerr << "Warning: address "
909 << hex << addr << dec
910 << " out of range for module "
911 << dw.module_name;
5f0a03a6
JK
912 return;
913 }
914 if (!null_die(&fi->die))
915 {
916 // addr looks like it's in the compilation unit containing
917 // the indicated function, but query_module_dwarf() didn't
918 // match addr to any compilation unit, so addr must be
919 // above that cu's address range.
83ca3872
MW
920 if (! sess.suppress_warnings)
921 cerr << "Warning: address "
922 << hex << addr << dec
923 << " maps to no known compilation unit in module "
924 << dw.module_name;
5f0a03a6
JK
925 return;
926 }
927 query_func_info(fi->addr, *fi, this);
928 }
929}
930
931void
932dwarf_query::handle_query_module()
933{
1c6b77e5
JS
934 bool report = dbinfo_reqt == dbr_need_dwarf || !sess.consult_symtab;
935 dw.get_module_dwarf(false, report);
936
937 // prebuild the symbol table to resolve aliases
938 dw.mod_info->get_symtab(this);
939
857bdfd1
JS
940 // reset the dupe-checking for each new module
941 alias_dupes.clear();
7fdd3e2c 942 inline_dupes.clear();
857bdfd1 943
5f0a03a6
JK
944 if (dw.mod_info->dwarf_status == info_present)
945 query_module_dwarf();
1c6b77e5 946
5f0a03a6
JK
947 // Consult the symbol table if we haven't found all we're looking for.
948 // asm functions can show up in the symbol table but not in dwarf.
949 if (sess.consult_symtab && !query_done)
950 query_module_symtab();
951}
952
2c384610 953
7d6d0afc
JS
954void
955dwarf_query::parse_function_spec(const string & spec)
bd2b1e68 956{
1d12a9b2
JS
957 line_type = ABSOLUTE;
958 line[0] = line[1] = 0;
959
7d6d0afc 960 size_t src_pos, line_pos, dash_pos, scope_pos, next_scope_pos;
bd2b1e68 961
7d6d0afc
JS
962 // look for named scopes
963 scope_pos = 0;
964 next_scope_pos = spec.find("::");
965 while (next_scope_pos != string::npos)
bd2b1e68 966 {
7d6d0afc
JS
967 scopes.push_back(spec.substr(scope_pos, next_scope_pos - scope_pos));
968 scope_pos = next_scope_pos + 2;
969 next_scope_pos = spec.find("::", scope_pos);
bd2b1e68
GH
970 }
971
7d6d0afc
JS
972 // look for a source separator
973 src_pos = spec.find('@', scope_pos);
974 if (src_pos == string::npos)
bd2b1e68 975 {
7d6d0afc
JS
976 function = spec.substr(scope_pos);
977 spec_type = function_alone;
bd2b1e68 978 }
7d6d0afc 979 else
879eb9e9 980 {
7d6d0afc 981 function = spec.substr(scope_pos, src_pos - scope_pos);
7a053d3b 982
7d6d0afc
JS
983 // look for a line-number separator
984 line_pos = spec.find_first_of(":+", src_pos);
985 if (line_pos == string::npos)
986 {
987 file = spec.substr(src_pos + 1);
988 spec_type = function_and_file;
989 }
990 else
991 {
992 file = spec.substr(src_pos + 1, line_pos - src_pos - 1);
993
994 // classify the line spec
995 spec_type = function_file_and_line;
996 if (spec[line_pos] == '+')
997 line_type = RELATIVE;
998 else if (spec[line_pos + 1] == '*' &&
999 spec.length() == line_pos + 2)
1000 line_type = WILDCARD;
1001 else
1002 line_type = ABSOLUTE;
1003
1004 if (line_type != WILDCARD)
1005 try
1006 {
1007 // try to parse either N or N-M
1008 dash_pos = spec.find('-', line_pos + 1);
1009 if (dash_pos == string::npos)
1010 line[0] = line[1] = lex_cast<int>(spec.substr(line_pos + 1));
1011 else
1012 {
1013 line_type = RANGE;
1014 line[0] = lex_cast<int>(spec.substr(line_pos + 1,
1015 dash_pos - line_pos - 1));
1016 line[1] = lex_cast<int>(spec.substr(dash_pos + 1));
1017 }
1018 }
1019 catch (runtime_error & exn)
1020 {
1021 goto bad;
1022 }
1023 }
bd2b1e68
GH
1024 }
1025
7d6d0afc
JS
1026 if (function.empty() ||
1027 (spec_type != function_alone && file.empty()))
bd2b1e68
GH
1028 goto bad;
1029
7d6d0afc 1030 if (sess.verbose > 2)
bd2b1e68 1031 {
7d6d0afc 1032 clog << "parsed '" << spec << "'";
41c262f3 1033
7d6d0afc
JS
1034 if (!scopes.empty())
1035 clog << ", scope '" << scopes[0] << "'";
1036 for (unsigned i = 1; i < scopes.size(); ++i)
1037 clog << "::'" << scopes[i] << "'";
41c262f3 1038
7d6d0afc
JS
1039 clog << ", func '" << function << "'";
1040
1041 if (spec_type != function_alone)
1042 clog << ", file '" << file << "'";
1043
1044 if (spec_type == function_file_and_line)
1045 {
1046 clog << ", line ";
1047 switch (line_type)
1048 {
1049 case ABSOLUTE:
1050 clog << line[0];
1051 break;
1052
1053 case RELATIVE:
1054 clog << "+" << line[0];
1055 break;
1056
1057 case RANGE:
1058 clog << line[0] << " - " << line[1];
1059 break;
1060
1061 case WILDCARD:
1062 clog << "*";
1063 break;
1064 }
1065 }
1066
1067 clog << endl;
bd2b1e68
GH
1068 }
1069
7d6d0afc
JS
1070 return;
1071
1072bad:
1073 throw semantic_error("malformed specification '" + spec + "'",
1074 base_probe->tok);
bd2b1e68
GH
1075}
1076
1077
36f9dd1d 1078void
1ffb8bd1 1079dwarf_query::add_probe_point(const string& dw_funcname,
b20febf3 1080 const char* filename,
36f9dd1d 1081 int line,
b20febf3 1082 Dwarf_Die* scope_die,
36f9dd1d
FCE
1083 Dwarf_Addr addr)
1084{
b20febf3 1085 string reloc_section; // base section for relocation purposes
27646582 1086 Dwarf_Addr reloc_addr; // relocated
b20febf3 1087 const string& module = dw.module_name; // "kernel" or other
1ffb8bd1 1088 string funcname = dw_funcname;
36f9dd1d 1089
37ebca01
FCE
1090 assert (! has_absolute); // already handled in dwarf_builder::build()
1091
789448a3 1092 reloc_addr = dw.relocate_address(addr, reloc_section);
2930abc7 1093
1ffb8bd1 1094 // If we originally used the linkage name, then let's call it that way
1ffb8bd1
JS
1095 const char* linkage_name;
1096 if (scope_die && startswith (this->function, "_Z")
f450a7e3 1097 && (linkage_name = dwarf_linkage_name (scope_die)))
1ffb8bd1
JS
1098 funcname = linkage_name;
1099
7f9f3386
FCE
1100 if (sess.verbose > 1)
1101 {
b20febf3
FCE
1102 clog << "probe " << funcname << "@" << filename << ":" << line;
1103 if (string(module) == TOK_KERNEL)
1104 clog << " kernel";
91af0778 1105 else if (has_module)
b20febf3 1106 clog << " module=" << module;
91af0778
FCE
1107 else if (has_process)
1108 clog << " process=" << module;
b20febf3 1109 if (reloc_section != "") clog << " reloc=" << reloc_section;
b20febf3 1110 clog << " pc=0x" << hex << addr << dec;
7f9f3386 1111 }
4baf0e53 1112
27646582 1113 bool bad = dw.blacklisted_p (funcname, filename, line, module,
789448a3 1114 addr, has_return);
b20febf3
FCE
1115 if (sess.verbose > 1)
1116 clog << endl;
7f9f3386 1117
84048984
FCE
1118 if (module == TOK_KERNEL)
1119 {
1120 // PR 4224: adapt to relocatable kernel by subtracting the _stext address here.
1121 reloc_addr = addr - sess.sym_stext;
37ebca01 1122 reloc_section = "_stext"; // a message to runtime's _stp_module_relocate
84048984
FCE
1123 }
1124
b20febf3
FCE
1125 if (! bad)
1126 {
1a0dbc5a 1127 sess.unwindsym_modules.insert (module);
6d0f3f0c
FCE
1128
1129 if (has_process)
1130 {
1131 results.push_back (new uprobe_derived_probe(funcname, filename, line,
6b66b9f7 1132 module, reloc_section, addr, reloc_addr,
6d0f3f0c
FCE
1133 *this, scope_die));
1134 }
1135 else
1136 {
1137 assert (has_kernel || has_module);
1138 results.push_back (new dwarf_derived_probe(funcname, filename, line,
06aca46a 1139 module, reloc_section, addr, reloc_addr,
6d0f3f0c
FCE
1140 *this, scope_die));
1141 }
b20febf3 1142 }
2930abc7
FCE
1143}
1144
5f0a03a6
JK
1145enum dbinfo_reqt
1146dwarf_query::assess_dbinfo_reqt()
1147{
1148 if (has_absolute)
1149 {
1150 // kernel.statement(NUM).absolute
1151 return dbr_none;
1152 }
1153 if (has_inline)
1154 {
1155 // kernel.function("f").inline or module("m").function("f").inline
1156 return dbr_need_dwarf;
1157 }
1158 if (has_function_str && spec_type == function_alone)
1159 {
1160 // kernel.function("f") or module("m").function("f")
1161 return dbr_need_symtab;
1162 }
1163 if (has_statement_num)
1164 {
1165 // kernel.statement(NUM) or module("m").statement(NUM)
1166 // Technically, all we need is the module offset (or _stext, for
1167 // the kernel). But for that we need either the ELF file or (for
1168 // _stext) the symbol table. In either case, the symbol table
1169 // is available, and that allows us to map the NUM (address)
1170 // to a function, which is goodness.
1171 return dbr_need_symtab;
1172 }
1173 if (has_function_num)
1174 {
1175 // kernel.function(NUM) or module("m").function(NUM)
1176 // Need the symbol table so we can back up from NUM to the
1177 // start of the function.
1178 return dbr_need_symtab;
1179 }
1180 // Symbol table tells us nothing about source files or line numbers.
1181 return dbr_need_dwarf;
1182}
2930abc7
FCE
1183
1184
b8da0ad1
FCE
1185// The critical determining factor when interpreting a pattern
1186// string is, perhaps surprisingly: "presence of a lineno". The
1187// presence of a lineno changes the search strategy completely.
1188//
1189// Compare the two cases:
1190//
1191// 1. {statement,function}(foo@file.c:lineno)
1192// - find the files matching file.c
1193// - in each file, find the functions matching foo
1194// - query the file for line records matching lineno
1195// - iterate over the line records,
1196// - and iterate over the functions,
1197// - if(haspc(function.DIE, line.addr))
1198// - if looking for statements: probe(lineno.addr)
1199// - if looking for functions: probe(function.{entrypc,return,etc.})
1200//
1201// 2. {statement,function}(foo@file.c)
1202// - find the files matching file.c
1203// - in each file, find the functions matching foo
1204// - probe(function.{entrypc,return,etc.})
1205//
1206// Thus the first decision we make is based on the presence of a
1207// lineno, and we enter entirely different sets of callbacks
1208// depending on that decision.
1209//
1210// Note that the first case is a generalization fo the second, in that
1211// we could theoretically search through line records for matching
1212// file names (a "table scan" in rdbms lingo). Luckily, file names
1213// are already cached elsewhere, so we can do an "index scan" as an
1214// optimization.
7e1279ea 1215
bd2b1e68 1216static void
4cd232e4 1217query_statement (string const & func,
20e4a32c 1218 char const * file,
4cd232e4 1219 int line,
bcc12710 1220 Dwarf_Die *scope_die,
20e4a32c 1221 Dwarf_Addr stmt_addr,
4cd232e4 1222 dwarf_query * q)
bd2b1e68 1223{
39bcd429
FCE
1224 try
1225 {
cee35f73 1226 q->add_probe_point(func, file ? file : "",
a9b2f3a5 1227 line, scope_die, stmt_addr);
39bcd429
FCE
1228 }
1229 catch (const semantic_error& e)
1230 {
1231 q->sess.print_error (e);
1232 }
bd2b1e68
GH
1233}
1234
6b517475
JS
1235static void
1236query_addr(Dwarf_Addr addr, dwarf_query *q)
1237{
1238 dwflpp &dw = q->dw;
1239
08d1d520
MW
1240 if (q->sess.verbose > 2)
1241 clog << "query_addr 0x" << hex << addr << dec << endl;
6b517475
JS
1242
1243 // First pick which CU contains this address
1244 Dwarf_Die* cudie = dw.query_cu_containing_address(addr);
1245 if (!cudie) // address could be wildly out of range
1246 return;
1247 dw.focus_on_cu(cudie);
1248
1249 // Now compensate for the dw bias
1250 addr -= dw.module_bias;
1251
1252 // Per PR5787, we look up the scope die even for
1253 // statement_num's, for blacklist sensitivity and $var
1254 // resolution purposes.
1255
1256 // Find the scopes containing this address
1257 vector<Dwarf_Die> scopes = dw.getscopes(addr);
1258 if (scopes.empty())
1259 return;
1260
1261 // Look for the innermost containing function
1262 Dwarf_Die *fnscope = NULL;
1263 for (size_t i = 0; i < scopes.size(); ++i)
1264 {
1265 int tag = dwarf_tag(&scopes[i]);
1266 if ((tag == DW_TAG_subprogram && !q->has_inline) ||
1267 (tag == DW_TAG_inlined_subroutine &&
1268 !q->has_call && !q->has_return))
1269 {
1270 fnscope = &scopes[i];
1271 break;
1272 }
1273 }
1274 if (!fnscope)
1275 return;
1276 dw.focus_on_function(fnscope);
1277
1278 Dwarf_Die *scope = q->has_function_num ? fnscope : &scopes[0];
1279
1280 const char *file = dwarf_decl_file(fnscope);
1281 int line;
1282 dwarf_decl_line(fnscope, &line);
1283
1284 // Function probes should reset the addr to the function entry
1285 // and possibly perform prologue searching
1286 if (q->has_function_num)
1287 {
1288 dw.die_entrypc(fnscope, &addr);
1289 if (dwarf_tag(fnscope) == DW_TAG_subprogram &&
1290 (q->sess.prologue_searching || q->has_process)) // PR 6871
1291 {
1292 func_info func;
1293 func.die = *fnscope;
1294 func.name = dw.function_name;
1295 func.decl_file = file;
1296 func.decl_line = line;
1297 func.entrypc = addr;
1298
1299 func_info_map_t funcs(1, func);
1300 dw.resolve_prologue_endings (funcs);
1301 if (funcs[0].prologue_end)
1302 addr = funcs[0].prologue_end;
1303 }
1304 }
1305 else
1306 {
1307 dwarf_line_t address_line(dwarf_getsrc_die(cudie, addr));
1308 if (address_line)
1309 {
1310 file = address_line.linesrc();
1311 line = address_line.lineno();
1312 }
1313
1314 // Verify that a raw address matches the beginning of a
1315 // statement. This is a somewhat lame check that the address
1316 // is at the start of an assembly instruction. Mark probes are in the
1317 // middle of a macro and thus not strictly at a statement beginning.
1318 // Guru mode may override this check.
1319 if (!q->has_mark && (!address_line || address_line.addr() != addr))
1320 {
1321 stringstream msg;
1322 msg << "address 0x" << hex << addr
1323 << " does not match the beginning of a statement";
1324 if (address_line)
1325 msg << " (try 0x" << hex << address_line.addr() << ")";
1326 else
1327 msg << " (no line info found for '" << dw.cu_name()
1328 << "', in module '" << dw.module_name << "')";
1329 if (! q->sess.guru_mode)
1330 throw semantic_error(msg.str());
1331 else if (! q->sess.suppress_warnings)
1332 q->sess.print_warning(msg.str());
1333 }
1334 }
1335
1336 // Build a probe at this point
1337 query_statement(dw.function_name, file, line, scope, addr, q);
1338}
1339
8096dd7d
JS
1340static void
1341query_label (string const & func,
1342 char const * label,
1343 char const * file,
1344 int line,
1345 Dwarf_Die *scope_die,
1346 Dwarf_Addr stmt_addr,
1347 dwarf_query * q)
1348{
6b517475
JS
1349 assert (q->has_statement_str || q->has_function_str);
1350
8096dd7d
JS
1351 size_t i = q->results.size();
1352
1353 // weed out functions whose decl_file isn't one of
1354 // the source files that we actually care about
6b517475 1355 if (q->spec_type != function_alone &&
8096dd7d
JS
1356 q->filtered_srcfiles.count(file) == 0)
1357 return;
1358
1359 query_statement(func, file, line, scope_die, stmt_addr, q);
1360
c72aa911
JS
1361 // after the fact, insert the label back into the derivation chain
1362 probe_point::component* ppc =
1363 new probe_point::component(TOK_LABEL, new literal_string (label));
1364 for (; i < q->results.size(); ++i)
1365 {
1366 derived_probe* p = q->results[i];
1367 probe_point* pp = new probe_point(*p->locations[0]);
1368 pp->components.push_back (ppc);
1369 p->base = p->base->create_alias(p->locations[0], pp);
1370 }
8096dd7d
JS
1371}
1372
7e1279ea 1373static void
3e961ba6 1374query_inline_instance_info (inline_instance_info & ii,
7e1279ea
FCE
1375 dwarf_query * q)
1376{
b6581717 1377 try
7e1279ea 1378 {
8f14e444
FCE
1379 assert (! q->has_return); // checked by caller already
1380 if (q->sess.verbose>2)
1381 clog << "querying entrypc "
1382 << hex << ii.entrypc << dec
1383 << " of instance of inline '" << ii.name << "'\n";
1384 query_statement (ii.name, ii.decl_file, ii.decl_line,
1385 &ii.die, ii.entrypc, q);
7e1279ea 1386 }
b6581717 1387 catch (semantic_error &e)
7e1279ea 1388 {
b6581717 1389 q->sess.print_error (e);
7e1279ea
FCE
1390 }
1391}
1392
1393static void
1394query_func_info (Dwarf_Addr entrypc,
bcc12710 1395 func_info & fi,
7e1279ea
FCE
1396 dwarf_query * q)
1397{
b6581717 1398 try
7e1279ea 1399 {
b6581717
GH
1400 if (q->has_return)
1401 {
1402 // NB. dwarf_derived_probe::emit_registrations will emit a
1403 // kretprobe based on the entrypc in this case.
20e4a32c 1404 query_statement (fi.name, fi.decl_file, fi.decl_line,
b6581717
GH
1405 &fi.die, entrypc, q);
1406 }
1407 else
1408 {
35dc8b04 1409 if (fi.prologue_end != 0)
44f75386 1410 {
44f75386
FCE
1411 query_statement (fi.name, fi.decl_file, fi.decl_line,
1412 &fi.die, fi.prologue_end, q);
1413 }
1414 else
1415 {
1416 query_statement (fi.name, fi.decl_file, fi.decl_line,
1417 &fi.die, entrypc, q);
1418 }
b6581717 1419 }
7e1279ea 1420 }
b6581717 1421 catch (semantic_error &e)
7e1279ea 1422 {
b6581717 1423 q->sess.print_error (e);
7e1279ea
FCE
1424 }
1425}
1426
1427
bd4b874d
SC
1428static void
1429query_srcfile_label (const dwarf_line_t& line, void * arg)
1430{
1431 dwarf_query * q = static_cast<dwarf_query *>(arg);
1432
1433 Dwarf_Addr addr = line.addr();
1434
1435 for (func_info_map_t::iterator i = q->filtered_functions.begin();
1436 i != q->filtered_functions.end(); ++i)
1437 if (q->dw.die_has_pc (i->die, addr))
f09d0d1e
JS
1438 q->dw.iterate_over_labels (&i->die, q->label_val, i->name,
1439 q, query_label);
1440
1441 for (inline_instance_map_t::iterator i = q->filtered_inlines.begin();
1442 i != q->filtered_inlines.end(); ++i)
1443 if (q->dw.die_has_pc (i->die, addr))
1444 q->dw.iterate_over_labels (&i->die, q->label_val, i->name,
1445 q, query_label);
bd4b874d
SC
1446}
1447
7e1279ea 1448static void
86bf665e 1449query_srcfile_line (const dwarf_line_t& line, void * arg)
7e1279ea
FCE
1450{
1451 dwarf_query * q = static_cast<dwarf_query *>(arg);
1452
86bf665e 1453 Dwarf_Addr addr = line.addr();
4cd232e4 1454
86bf665e 1455 int lineno = line.lineno();
847bf07f 1456
86bf665e 1457 for (func_info_map_t::iterator i = q->filtered_functions.begin();
7e1279ea
FCE
1458 i != q->filtered_functions.end(); ++i)
1459 {
3e961ba6 1460 if (q->dw.die_has_pc (i->die, addr))
7e1279ea 1461 {
b0ee93c4 1462 if (q->sess.verbose>3)
db22e55f 1463 clog << "function DIE lands on srcfile\n";
4cd232e4 1464 if (q->has_statement_str)
f5958c8f
JS
1465 {
1466 Dwarf_Die scope;
1467 q->dw.inner_die_containing_pc(i->die, addr, scope);
1468 query_statement (i->name, i->decl_file,
1469 lineno, // NB: not q->line !
1470 &scope, addr, q);
1471 }
4cd232e4 1472 else
3e961ba6 1473 query_func_info (i->entrypc, *i, q);
7e1279ea 1474 }
20e4a32c
RM
1475 }
1476
86bf665e 1477 for (inline_instance_map_t::iterator i
897820ca
GH
1478 = q->filtered_inlines.begin();
1479 i != q->filtered_inlines.end(); ++i)
1480 {
3e961ba6 1481 if (q->dw.die_has_pc (i->die, addr))
7e1279ea 1482 {
b0ee93c4 1483 if (q->sess.verbose>3)
db22e55f 1484 clog << "inline instance DIE lands on srcfile\n";
897820ca 1485 if (q->has_statement_str)
f5958c8f
JS
1486 {
1487 Dwarf_Die scope;
1488 q->dw.inner_die_containing_pc(i->die, addr, scope);
1489 query_statement (i->name, i->decl_file,
1490 q->line[0], &scope, addr, q);
1491 }
897820ca 1492 else
3e961ba6 1493 query_inline_instance_info (*i, q);
897820ca 1494 }
20e4a32c 1495 }
7e1279ea
FCE
1496}
1497
1498
7fdd3e2c
JS
1499bool
1500inline_instance_info::operator<(const inline_instance_info& other) const
1501{
1502 if (entrypc != other.entrypc)
1503 return entrypc < other.entrypc;
1504
1505 if (decl_line != other.decl_line)
1506 return decl_line < other.decl_line;
1507
1508 int cmp = name.compare(other.name);
1509 if (!cmp)
1510 cmp = strcmp(decl_file, other.decl_file);
1511 return cmp < 0;
1512}
1513
1514
4fa7b22b 1515static int
7e1279ea 1516query_dwarf_inline_instance (Dwarf_Die * die, void * arg)
4fa7b22b
GH
1517{
1518 dwarf_query * q = static_cast<dwarf_query *>(arg);
6b517475
JS
1519 assert (q->has_statement_str || q->has_function_str);
1520 assert (!q->has_call && !q->has_return);
bd2b1e68 1521
39bcd429 1522 try
7a053d3b 1523 {
b0ee93c4 1524 if (q->sess.verbose>2)
6b517475 1525 clog << "selected inline instance of " << q->dw.function_name << "\n";
7e1279ea 1526
6b517475
JS
1527 Dwarf_Addr entrypc;
1528 if (q->dw.die_entrypc (die, &entrypc))
1529 {
1530 inline_instance_info inl;
1531 inl.die = *die;
1532 inl.name = q->dw.function_name;
1533 inl.entrypc = entrypc;
1534 q->dw.function_file (&inl.decl_file);
1535 q->dw.function_line (&inl.decl_line);
1536
1537 // make sure that this inline hasn't already
1538 // been matched from a different CU
1539 if (q->inline_dupes.insert(inl).second)
1540 q->filtered_inlines.push_back(inl);
1541 }
7e1279ea
FCE
1542 return DWARF_CB_OK;
1543 }
1544 catch (const semantic_error& e)
1545 {
1546 q->sess.print_error (e);
1547 return DWARF_CB_ABORT;
1548 }
1549}
bb788f9f 1550
7e1279ea 1551static int
2da9cedb 1552query_dwarf_func (Dwarf_Die * func, base_query * bq)
7e1279ea 1553{
2da9cedb 1554 dwarf_query * q = static_cast<dwarf_query *>(bq);
6b517475 1555 assert (q->has_statement_str || q->has_function_str);
bb788f9f 1556
bd25380d
JS
1557 // weed out functions whose decl_file isn't one of
1558 // the source files that we actually care about
6b517475 1559 if (q->spec_type != function_alone &&
bd25380d 1560 q->filtered_srcfiles.count(dwarf_decl_file(func)?:"") == 0)
8096dd7d 1561 return DWARF_CB_OK;
bd25380d 1562
7e1279ea
FCE
1563 try
1564 {
7e1279ea
FCE
1565 q->dw.focus_on_function (func);
1566
7d6d0afc
JS
1567 if (!q->dw.function_scope_matches(q->scopes))
1568 return DWARF_CB_OK;
1569
857bdfd1
JS
1570 // make sure that this function address hasn't
1571 // already been matched under an aliased name
1572 Dwarf_Addr addr;
1573 if (!q->dw.func_is_inline() &&
1574 dwarf_entrypc(func, &addr) == 0 &&
1575 !q->alias_dupes.insert(addr).second)
1576 return DWARF_CB_OK;
1577
6b517475 1578 if (q->dw.func_is_inline () && (! q->has_call) && (! q->has_return))
7e1279ea 1579 {
b0ee93c4 1580 if (q->sess.verbose>3)
db22e55f
FCE
1581 clog << "checking instances of inline " << q->dw.function_name
1582 << "\n";
2da9cedb 1583 q->dw.iterate_over_inline_instances (query_dwarf_inline_instance, q);
7e1279ea 1584 }
8f14e444
FCE
1585 else if (q->dw.func_is_inline () && (q->has_return)) // PR 11553
1586 {
1587 q->inlined_non_returnable.insert (q->dw.function_name);
1588 }
396afcee 1589 else if (!q->dw.func_is_inline () && (! q->has_inline))
20e4a32c 1590 {
6b517475
JS
1591 if (q->sess.verbose>2)
1592 clog << "selected function " << q->dw.function_name << "\n";
1593
1594 func_info func;
1595 q->dw.function_die (&func.die);
1596 func.name = q->dw.function_name;
1597 q->dw.function_file (&func.decl_file);
1598 q->dw.function_line (&func.decl_line);
1599
1600 Dwarf_Addr entrypc;
1601 if (q->dw.function_entrypc (&entrypc))
1602 {
1603 func.entrypc = entrypc;
1604 q->filtered_functions.push_back (func);
1605 }
1606 /* else this function is fully inlined, just ignore it */
7e1279ea 1607 }
39bcd429 1608 return DWARF_CB_OK;
bd2b1e68 1609 }
39bcd429 1610 catch (const semantic_error& e)
bd2b1e68 1611 {
39bcd429
FCE
1612 q->sess.print_error (e);
1613 return DWARF_CB_ABORT;
bd2b1e68 1614 }
bd2b1e68
GH
1615}
1616
1617static int
1618query_cu (Dwarf_Die * cudie, void * arg)
1619{
20c6c071 1620 dwarf_query * q = static_cast<dwarf_query *>(arg);
6b517475
JS
1621 assert (q->has_statement_str || q->has_function_str);
1622
85007c04 1623 if (pending_interrupts) return DWARF_CB_ABORT;
7a053d3b 1624
39bcd429 1625 try
bd2b1e68 1626 {
7e1279ea 1627 q->dw.focus_on_cu (cudie);
b5d77020 1628
b0ee93c4 1629 if (false && q->sess.verbose>2)
54417494 1630 clog << "focused on CU '" << q->dw.cu_name()
db22e55f 1631 << "', in module '" << q->dw.module_name << "'\n";
d9b516ca 1632
6b517475
JS
1633 q->filtered_srcfiles.clear();
1634 q->filtered_functions.clear();
1635 q->filtered_inlines.clear();
1636
1637 // In this path, we find "abstract functions", record
1638 // information about them, and then (depending on lineno
1639 // matching) possibly emit one or more of the function's
1640 // associated addresses. Unfortunately the control of this
1641 // cannot easily be turned inside out.
1642
1643 if (q->spec_type != function_alone)
39bcd429 1644 {
6b517475
JS
1645 // If we have a pattern string with a filename, we need
1646 // to elaborate the srcfile mask in question first.
1647 q->dw.collect_srcfiles_matching (q->file, q->filtered_srcfiles);
1648
1649 // If we have a file pattern and *no* srcfile matches, there's
1650 // no need to look further into this CU, so skip.
1651 if (q->filtered_srcfiles.empty())
1652 return DWARF_CB_OK;
1653 }
e4c58386 1654
6b517475
JS
1655 // Pick up [entrypc, name, DIE] tuples for all the functions
1656 // matching the query, and fill in the prologue endings of them
1657 // all in a single pass.
5898b6e1 1658 int rc = q->dw.iterate_over_functions (query_dwarf_func, q, q->function);
6b517475
JS
1659 if (rc != DWARF_CB_OK)
1660 q->query_done = true;
1661
1662 if ((q->sess.prologue_searching || q->has_process) // PR 6871
1663 && !q->has_statement_str) // PR 2608
1664 if (! q->filtered_functions.empty())
1665 q->dw.resolve_prologue_endings (q->filtered_functions);
1666
1667 if (q->spec_type == function_file_and_line)
1668 {
1669 // If we have a pattern string with target *line*, we
1670 // have to look at lines in all the matched srcfiles.
1671 void (* callback) (const dwarf_line_t&, void*) =
1672 q->has_label ? query_srcfile_label : query_srcfile_line;
1673 for (set<string>::const_iterator i = q->filtered_srcfiles.begin();
1674 i != q->filtered_srcfiles.end(); ++i)
1675 q->dw.iterate_over_srcfile_lines (i->c_str(), q->line, q->has_statement_str,
1676 q->line_type, callback, q->function, q);
1677 }
1678 else if (q->has_label)
1679 {
1680 for (func_info_map_t::iterator i = q->filtered_functions.begin();
1681 i != q->filtered_functions.end(); ++i)
1682 q->dw.iterate_over_labels (&i->die, q->label_val, i->name,
1683 q, query_label);
1684
1685 for (inline_instance_map_t::iterator i = q->filtered_inlines.begin();
1686 i != q->filtered_inlines.end(); ++i)
1687 q->dw.iterate_over_labels (&i->die, q->label_val, i->name,
1688 q, query_label);
39bcd429 1689 }
6b517475
JS
1690 else
1691 {
1692 // Otherwise, simply probe all resolved functions.
1693 for (func_info_map_t::iterator i = q->filtered_functions.begin();
1694 i != q->filtered_functions.end(); ++i)
1695 query_func_info (i->entrypc, *i, q);
1696
1697 // And all inline instances (if we're not excluding inlines with ".call")
1698 if (! q->has_call)
1699 for (inline_instance_map_t::iterator i
1700 = q->filtered_inlines.begin(); i != q->filtered_inlines.end(); ++i)
1701 query_inline_instance_info (*i, q);
1702 }
39bcd429 1703 return DWARF_CB_OK;
bd2b1e68 1704 }
39bcd429 1705 catch (const semantic_error& e)
bd2b1e68 1706 {
39bcd429
FCE
1707 q->sess.print_error (e);
1708 return DWARF_CB_ABORT;
bd2b1e68 1709 }
bd2b1e68
GH
1710}
1711
0ce64fb8 1712
4df79aaf
JS
1713void
1714dwarf_query::query_module_functions ()
1715{
1716 try
1717 {
1718 filtered_srcfiles.clear();
1719 filtered_functions.clear();
1720 filtered_inlines.clear();
1721
1722 // Collect all module functions so we know which CUs are interesting
1723 int rc = dw.iterate_single_function(query_dwarf_func, this, function);
1724 if (rc != DWARF_CB_OK)
1725 {
1726 query_done = true;
1727 return;
1728 }
1729
1730 set<void*> used_cus; // by cu->addr
1731 vector<Dwarf_Die> cus;
1732 Dwarf_Die cu_mem;
1733
1734 for (func_info_map_t::iterator i = filtered_functions.begin();
1735 i != filtered_functions.end(); ++i)
1736 if (dwarf_diecu(&i->die, &cu_mem, NULL, NULL) &&
1737 used_cus.insert(cu_mem.addr).second)
1738 cus.push_back(cu_mem);
1739
1740 for (inline_instance_map_t::iterator i = filtered_inlines.begin();
1741 i != filtered_inlines.end(); ++i)
1742 if (dwarf_diecu(&i->die, &cu_mem, NULL, NULL) &&
1743 used_cus.insert(cu_mem.addr).second)
1744 cus.push_back(cu_mem);
1745
1746 // Reset the dupes since we didn't actually collect them the first time
1747 alias_dupes.clear();
1748 inline_dupes.clear();
1749
1750 // Run the query again on the individual CUs
1751 for (vector<Dwarf_Die>::iterator i = cus.begin(); i != cus.end(); ++i)
1752 query_cu(&*i, this);
1753 }
1754 catch (const semantic_error& e)
1755 {
1756 sess.print_error (e);
1757 }
1758}
1759
1760
5f0a03a6
JK
1761static void
1762validate_module_elf (Dwfl_Module *mod, const char *name, base_query *q)
1763{
1764 // Validate the machine code in this elf file against the
1765 // session machine. This is important, in case the wrong kind
1766 // of debuginfo is being automagically processed by elfutils.
1767 // While we can tell i686 apart from x86-64, unfortunately
1768 // we can't help confusing i586 vs i686 (both EM_386).
1769
1770 Dwarf_Addr bias;
1771 // We prefer dwfl_module_getdwarf to dwfl_module_getelf here,
1772 // because dwfl_module_getelf can force costly section relocations
1773 // we don't really need, while either will do for this purpose.
1774 Elf* elf = (dwarf_getelf (dwfl_module_getdwarf (mod, &bias))
1775 ?: dwfl_module_getelf (mod, &bias));
1776
1777 GElf_Ehdr ehdr_mem;
1778 GElf_Ehdr* em = gelf_getehdr (elf, &ehdr_mem);
86bf665e 1779 if (em == 0) { dwfl_assert ("dwfl_getehdr", dwfl_errno()); }
5f0a03a6
JK
1780 int elf_machine = em->e_machine;
1781 const char* debug_filename = "";
1782 const char* main_filename = "";
1783 (void) dwfl_module_info (mod, NULL, NULL,
1784 NULL, NULL, NULL,
1785 & main_filename,
1786 & debug_filename);
1787 const string& sess_machine = q->sess.architecture;
756c9462
FCE
1788
1789 string expect_machine; // to match sess.machine (i.e., kernel machine)
1790 string expect_machine2;
5f0a03a6 1791
d27e6fd5 1792 // NB: See also the 'uname -m' squashing done in main.cxx.
5f0a03a6
JK
1793 switch (elf_machine)
1794 {
756c9462
FCE
1795 // x86 and ppc are bi-architecture; a 64-bit kernel
1796 // can normally run either 32-bit or 64-bit *userspace*.
1797 case EM_386:
1798 expect_machine = "i?86";
1799 if (! q->has_process) break; // 32-bit kernel/module
1800 /* FALLSTHROUGH */
1801 case EM_X86_64:
1802 expect_machine2 = "x86_64";
1803 break;
1804 case EM_PPC:
756c9462 1805 case EM_PPC64:
5a1c472e 1806 expect_machine = "powerpc";
756c9462 1807 break;
3fe7d888 1808 case EM_S390: expect_machine = "s390"; break;
5f0a03a6 1809 case EM_IA_64: expect_machine = "ia64"; break;
d27e6fd5 1810 case EM_ARM: expect_machine = "arm*"; break;
5f0a03a6
JK
1811 // XXX: fill in some more of these
1812 default: expect_machine = "?"; break;
1813 }
1814
1815 if (! debug_filename) debug_filename = main_filename;
1816 if (! debug_filename) debug_filename = name;
1817
756c9462
FCE
1818 if (fnmatch (expect_machine.c_str(), sess_machine.c_str(), 0) != 0 &&
1819 fnmatch (expect_machine2.c_str(), sess_machine.c_str(), 0) != 0)
5f0a03a6
JK
1820 {
1821 stringstream msg;
756c9462
FCE
1822 msg << "ELF machine " << expect_machine << "|" << expect_machine2
1823 << " (code " << elf_machine
5f0a03a6
JK
1824 << ") mismatch with target " << sess_machine
1825 << " in '" << debug_filename << "'";
1826 throw semantic_error(msg.str ());
1827 }
1828
1829 if (q->sess.verbose>2)
1830 clog << "focused on module '" << q->dw.module_name
1831 << " = [0x" << hex << q->dw.module_start
1832 << "-0x" << q->dw.module_end
1833 << ", bias 0x" << q->dw.module_bias << "]" << dec
1834 << " file " << debug_filename
756c9462 1835 << " ELF machine " << expect_machine << "|" << expect_machine2
5f0a03a6
JK
1836 << " (code " << elf_machine << ")"
1837 << "\n";
1838}
1d3a40b6 1839
91af0778
FCE
1840
1841
1842static Dwarf_Addr
1843lookup_symbol_address (Dwfl_Module *m, const char* wanted)
1844{
1845 int syments = dwfl_module_getsymtab(m);
1846 assert(syments);
1847 for (int i = 1; i < syments; ++i)
1848 {
1849 GElf_Sym sym;
1850 const char *name = dwfl_module_getsym(m, i, &sym, NULL);
1851 if (name != NULL && strcmp(name, wanted) == 0)
1852 return sym.st_value;
1853 }
1854
1855 return 0;
1856}
1857
1858
1859
bd2b1e68 1860static int
b8da0ad1 1861query_module (Dwfl_Module *mod,
91af0778 1862 void **,
b8da0ad1 1863 const char *name,
6f4c1275 1864 Dwarf_Addr addr,
b8da0ad1 1865 void *arg)
bd2b1e68 1866{
91af0778 1867 base_query *q = static_cast<base_query *>(arg);
bd2b1e68 1868
39bcd429 1869 try
e38d6504 1870 {
91af0778
FCE
1871 module_info* mi = q->sess.module_cache->cache[name];
1872 if (mi == 0)
1873 {
1874 mi = q->sess.module_cache->cache[name] = new module_info(name);
1875
6f4c1275
FCE
1876 mi->mod = mod;
1877 mi->addr = addr;
91af0778 1878
6f4c1275
FCE
1879 const char* debug_filename = "";
1880 const char* main_filename = "";
1881 (void) dwfl_module_info (mod, NULL, NULL,
1882 NULL, NULL, NULL,
1883 & main_filename,
1884 & debug_filename);
1885
1886 if (q->sess.ignore_vmlinux && name == TOK_KERNEL)
91af0778
FCE
1887 {
1888 // report_kernel() in elfutils found vmlinux, but pretend it didn't.
1889 // Given a non-null path, returning 1 means keep reporting modules.
1890 mi->dwarf_status = info_absent;
1891 }
6f4c1275 1892 else if (debug_filename || main_filename)
91af0778 1893 {
6f4c1275
FCE
1894 mi->elf_path = debug_filename ?: main_filename;
1895 }
1896 else if (name == TOK_KERNEL)
1897 {
1898 mi->dwarf_status = info_absent;
91af0778 1899 }
91af0778
FCE
1900 }
1901 // OK, enough of that module_info caching business.
1902
5f0a03a6 1903 q->dw.focus_on_module(mod, mi);
d9b516ca 1904
39bcd429
FCE
1905 // If we have enough information in the pattern to skip a module and
1906 // the module does not match that information, return early.
b8da0ad1 1907 if (!q->dw.module_name_matches(q->module_val))
85007c04 1908 return pending_interrupts ? DWARF_CB_ABORT : DWARF_CB_OK;
0cbbf9d1
FCE
1909
1910 // Don't allow module("*kernel*") type expressions to match the
1911 // elfutils module "kernel", which we refer to in the probe
1912 // point syntax exclusively as "kernel.*".
1913 if (q->dw.module_name == TOK_KERNEL && ! q->has_kernel)
85007c04 1914 return pending_interrupts ? DWARF_CB_ABORT : DWARF_CB_OK;
b5d77020 1915
5f0a03a6
JK
1916 if (mod)
1917 validate_module_elf(mod, name, q);
1918 else
91af0778
FCE
1919 assert(q->has_kernel); // and no vmlinux to examine
1920
1921 if (q->sess.verbose>2)
1922 cerr << "focused on module '" << q->dw.module_name << "'\n";
1923
1924
1925 // Collect a few kernel addresses. XXX: these belong better
1926 // to the sess.module_info["kernel"] struct.
1927 if (q->dw.module_name == TOK_KERNEL)
c931ec8a 1928 {
91af0778
FCE
1929 if (! q->sess.sym_kprobes_text_start)
1930 q->sess.sym_kprobes_text_start = lookup_symbol_address (mod, "__kprobes_text_start");
1931 if (! q->sess.sym_kprobes_text_end)
1932 q->sess.sym_kprobes_text_end = lookup_symbol_address (mod, "__kprobes_text_end");
1933 if (! q->sess.sym_stext)
1934 q->sess.sym_stext = lookup_symbol_address (mod, "_stext");
c931ec8a
FCE
1935 }
1936
91af0778 1937 // Finally, search the module for matches of the probe point.
2c384610 1938 q->handle_query_module();
bb788f9f 1939
91af0778 1940
b8da0ad1 1941 // If we know that there will be no more matches, abort early.
85007c04 1942 if (q->dw.module_name_final_match(q->module_val) || pending_interrupts)
b8da0ad1
FCE
1943 return DWARF_CB_ABORT;
1944 else
1945 return DWARF_CB_OK;
7a053d3b 1946 }
39bcd429 1947 catch (const semantic_error& e)
bd2b1e68 1948 {
39bcd429
FCE
1949 q->sess.print_error (e);
1950 return DWARF_CB_ABORT;
bd2b1e68 1951 }
bd2b1e68
GH
1952}
1953
35d4ab18 1954
de688825 1955struct dwarf_var_expanding_visitor: public var_expanding_visitor
35d4ab18 1956{
77de5e9e 1957 dwarf_query & q;
bcc12710 1958 Dwarf_Die *scope_die;
77de5e9e 1959 Dwarf_Addr addr;
8c819921 1960 block *add_block;
2260f4e3 1961 block *add_call_probe; // synthesized from .return probes with saved $vars
8cc799a5 1962 bool add_block_tid, add_call_probe_tid;
af234c40
JS
1963 unsigned saved_longs, saved_strings; // data saved within kretprobes
1964 map<std::string, expression *> return_ts_map;
729455a7 1965 vector<Dwarf_Die> scopes;
b95e2b79 1966 bool visited;
77de5e9e 1967
de688825 1968 dwarf_var_expanding_visitor(dwarf_query & q, Dwarf_Die *sd, Dwarf_Addr a):
af234c40 1969 q(q), scope_die(sd), addr(a), add_block(NULL), add_call_probe(NULL),
8cc799a5 1970 add_block_tid(false), add_call_probe_tid(false),
af234c40 1971 saved_longs(0), saved_strings(0), visited(false) {}
277c21bc 1972 expression* gen_mapped_saved_return(expression* e, const string& name);
140be17a 1973 expression* gen_kretprobe_saved_return(expression* e);
a7999c82
JS
1974 void visit_target_symbol_saved_return (target_symbol* e);
1975 void visit_target_symbol_context (target_symbol* e);
d7f3e0c5 1976 void visit_target_symbol (target_symbol* e);
c24447be 1977 void visit_cast_op (cast_op* e);
8cc799a5 1978 void visit_entry_op (entry_op* e);
729455a7
JS
1979private:
1980 vector<Dwarf_Die>& getscopes(target_symbol *e);
77de5e9e
GH
1981};
1982
1983
de688825 1984unsigned var_expanding_visitor::tick = 0;
77de5e9e 1985
a50de939
DS
1986
1987var_expanding_visitor::var_expanding_visitor ()
1988{
1989 // FIXME: for the time being, by default we only support plain '$foo
1990 // = bar', not '+=' or any other op= variant. This is fixable, but a
1991 // bit ugly.
1992 //
1993 // If derived classes desire to add additional operator support, add
1994 // new operators to this list in the derived class constructor.
1995 valid_ops.insert ("=");
1996}
1997
1998
87214add
JS
1999bool
2000var_expanding_visitor::rewrite_lvalue(const token* tok, const std::string& eop,
2001 expression*& lvalue, expression*& rvalue)
77de5e9e 2002{
e57b735a
GH
2003 // Our job would normally be to require() the left and right sides
2004 // into a new assignment. What we're doing is slightly trickier:
2005 // we're pushing a functioncall** onto a stack, and if our left
2006 // child sets the functioncall* for that value, we're going to
2007 // assume our left child was a target symbol -- transformed into a
2008 // set_target_foo(value) call, and it wants to take our right child
2009 // as the argument "value".
2010 //
2011 // This is why some people claim that languages with
2012 // constructor-decomposing case expressions have a leg up on
2013 // visitors.
2014
2015 functioncall *fcall = NULL;
d9b516ca 2016
a50de939 2017 // Let visit_target_symbol know what operator it should handle.
87214add
JS
2018 const string* old_op = op;
2019 op = &eop;
a50de939 2020
e57b735a 2021 target_symbol_setter_functioncalls.push (&fcall);
87214add 2022 replace (lvalue);
e57b735a 2023 target_symbol_setter_functioncalls.pop ();
87214add
JS
2024 replace (rvalue);
2025
2026 op = old_op;
e57b735a
GH
2027
2028 if (fcall != NULL)
77de5e9e 2029 {
e57b735a
GH
2030 // Our left child is informing us that it was a target variable
2031 // and it has been replaced with a set_target_foo() function
2032 // call; we are going to provide that function call -- with the
2033 // right child spliced in as sole argument -- in place of
de688825 2034 // ourselves, in the var expansion we're in the middle of making.
e57b735a 2035
87214add 2036 if (valid_ops.find (eop) == valid_ops.end ())
a50de939
DS
2037 {
2038 // Build up a list of supported operators.
2039 string ops;
2040 std::set<string>::iterator i;
2041 for (i = valid_ops.begin(); i != valid_ops.end(); i++)
2042 ops += " " + *i + ",";
2043 ops.resize(ops.size() - 1); // chop off the last ','
2044
2045 // Throw the error.
2046 throw semantic_error ("Only the following assign operators are"
2047 " implemented on target variables:" + ops,
87214add 2048 tok);
a50de939 2049 }
e57b735a 2050
87214add
JS
2051 assert (lvalue == fcall);
2052 if (rvalue)
2053 fcall->args.push_back (rvalue);
4ed05b15 2054 provide (fcall);
87214add 2055 return true;
77de5e9e 2056 }
e57b735a 2057 else
87214add
JS
2058 return false;
2059}
2060
2061
2062void
2063var_expanding_visitor::visit_assignment (assignment* e)
2064{
2065 if (!rewrite_lvalue (e->tok, e->op, e->left, e->right))
2066 provide (e);
2067}
2068
2069
2070void
2071var_expanding_visitor::visit_pre_crement (pre_crement* e)
2072{
2073 expression *dummy = NULL;
2074 if (!rewrite_lvalue (e->tok, e->op, e->operand, dummy))
2075 provide (e);
2076}
2077
2078
2079void
2080var_expanding_visitor::visit_post_crement (post_crement* e)
2081{
2082 expression *dummy = NULL;
2083 if (!rewrite_lvalue (e->tok, e->op, e->operand, dummy))
2084 provide (e);
2085}
2086
2087
2088void
2089var_expanding_visitor::visit_delete_statement (delete_statement* s)
2090{
2091 string fakeop = "delete";
2092 expression *dummy = NULL;
2093 if (!rewrite_lvalue (s->tok, fakeop, s->value, dummy))
2094 provide (s);
e57b735a 2095}
d9b516ca 2096
d7f3e0c5 2097
30263a73
FCE
2098void
2099var_expanding_visitor::visit_defined_op (defined_op* e)
2100{
2101 bool resolved = true;
2102
2103 defined_ops.push (e);
2104 try {
2105 // NB: provide<>/require<> are NOT typesafe. So even though a defined_op is
2106 // defined with a target_symbol* operand, a subsidiary call may attempt to
2107 // rewrite it to a general expression* instead, and require<> happily
2108 // casts to/from void*, causing possible memory corruption. We use
2109 // expression* here, being the general case of rewritten $variable.
2110 expression *foo1 = e->operand;
2111 foo1 = require (foo1);
2112
c69a87e0 2113 // NB: Formerly, we had some curious cases to consider here, depending on what
30263a73 2114 // various visit_target_symbol() implementations do for successful or
c69a87e0
FCE
2115 // erroneous resolutions. Some would signal a visit_target_symbol failure
2116 // with an exception, with a set flag within the target_symbol, or nothing
2117 // at all.
30263a73 2118 //
c69a87e0
FCE
2119 // Now, failures always have to be signalled with a
2120 // saved_conversion_error being chained to the target_symbol.
2121 // Successes have to result in an attempted rewrite of the
850bfddd 2122 // target_symbol (via provide()).
780f11ff 2123 //
c69a87e0
FCE
2124 // Edna Mode: "no capes". fche: "no exceptions".
2125
30263a73
FCE
2126 // dwarf stuff: success: rewrites to a function; failure: retains target_symbol, sets saved_conversion_error
2127 //
2128 // sdt-kprobes sdt.h: success: string or functioncall; failure: semantic_error
2129 //
2130 // sdt-uprobes: success: string or no op; failure: no op; expect derived/synthetic
2131 // dwarf probe to take care of it.
2132 // But this is rather unhelpful. So we rig the sdt_var_expanding_visitor
2133 // to pass through @defined() to the synthetic dwarf probe.
780f11ff 2134 //
30263a73
FCE
2135 // utrace: success: rewrites to function; failure: semantic_error
2136 //
850bfddd 2137 // procfs: success: rewrites to function; failure: semantic_error
30263a73
FCE
2138
2139 target_symbol* foo2 = dynamic_cast<target_symbol*> (foo1);
c69a87e0 2140 if (foo2 && foo2->saved_conversion_error) // failing
30263a73 2141 resolved = false;
a45664f4 2142 else if (foo2) // unresolved but not marked failing
b7aedf26 2143 {
780f11ff
JS
2144 // There are some visitors that won't touch certain target_symbols,
2145 // e.g. dwarf_var_expanding_visitor won't resolve @cast. We should
2146 // leave it for now so some other visitor can have a chance.
b7aedf26
JS
2147 e->operand = foo2;
2148 provide (e);
2149 return;
2150 }
30263a73
FCE
2151 else // resolved, rewritten to some other expression type
2152 resolved = true;
780f11ff 2153 } catch (const semantic_error& e) {
c69a87e0 2154 assert (0); // should not happen
30263a73
FCE
2155 }
2156 defined_ops.pop ();
2157
2158 literal_number* ln = new literal_number (resolved ? 1 : 0);
2159 ln->tok = e->tok;
2160 provide (ln);
2161}
2162
2163
5f36109e
JS
2164struct dwarf_pretty_print
2165{
2166 dwarf_pretty_print (dwflpp& dw, vector<Dwarf_Die>& scopes, Dwarf_Addr pc,
2167 const string& local, bool userspace_p,
2168 const target_symbol& e):
d19a9a82
JS
2169 dw(dw), local(local), scopes(scopes), pc(pc), pointer(NULL),
2170 userspace_p(userspace_p), deref_p(true)
5f36109e
JS
2171 {
2172 init_ts (e);
2173 dw.type_die_for_local (scopes, pc, local, ts, &base_type);
2174 }
2175
2176 dwarf_pretty_print (dwflpp& dw, Dwarf_Die *scope_die, Dwarf_Addr pc,
2177 bool userspace_p, const target_symbol& e):
d19a9a82
JS
2178 dw(dw), scopes(1, *scope_die), pc(pc), pointer(NULL),
2179 userspace_p(userspace_p), deref_p(true)
5f36109e
JS
2180 {
2181 init_ts (e);
2182 dw.type_die_for_return (&scopes[0], pc, ts, &base_type);
2183 }
2184
2185 dwarf_pretty_print (dwflpp& dw, Dwarf_Die *type_die, expression* pointer,
d19a9a82 2186 bool deref_p, bool userspace_p, const target_symbol& e):
5f36109e 2187 dw(dw), pc(0), pointer(pointer), pointer_type(*type_die),
d19a9a82 2188 userspace_p(userspace_p), deref_p(deref_p)
5f36109e
JS
2189 {
2190 init_ts (e);
2191 dw.type_die_for_pointer (type_die, ts, &base_type);
2192 }
2193
2194 functioncall* expand ();
2195
2196private:
2197 dwflpp& dw;
2198 target_symbol* ts;
7d11d8c9 2199 bool print_full;
5f36109e
JS
2200 Dwarf_Die base_type;
2201
2202 string local;
2203 vector<Dwarf_Die> scopes;
2204 Dwarf_Addr pc;
2205
2206 expression* pointer;
2207 Dwarf_Die pointer_type;
2208
d19a9a82 2209 const bool userspace_p, deref_p;
5f36109e
JS
2210
2211 void recurse (Dwarf_Die* type, target_symbol* e,
7d11d8c9 2212 print_format* pf, bool top=false);
5f36109e 2213 void recurse_base (Dwarf_Die* type, target_symbol* e,
7d11d8c9 2214 print_format* pf);
5f36109e 2215 void recurse_array (Dwarf_Die* type, target_symbol* e,
7d11d8c9 2216 print_format* pf, bool top);
5f36109e 2217 void recurse_pointer (Dwarf_Die* type, target_symbol* e,
7d11d8c9 2218 print_format* pf, bool top);
5f36109e 2219 void recurse_struct (Dwarf_Die* type, target_symbol* e,
7d11d8c9 2220 print_format* pf, bool top);
5f36109e 2221 void recurse_struct_members (Dwarf_Die* type, target_symbol* e,
7d11d8c9 2222 print_format* pf, int& count);
bbee5bb8 2223 bool print_chars (Dwarf_Die* type, target_symbol* e, print_format* pf);
5f36109e
JS
2224
2225 void init_ts (const target_symbol& e);
2226 expression* deref (target_symbol* e);
c55ea10d 2227 bool push_deref (print_format* pf, const string& fmt, target_symbol* e);
5f36109e
JS
2228};
2229
2230
2231void
2232dwarf_pretty_print::init_ts (const target_symbol& e)
2233{
2234 // Work with a new target_symbol so we can modify arguments
2235 ts = new target_symbol (e);
2236
2237 if (ts->addressof)
2238 throw semantic_error("cannot take address of pretty-printed variable", ts->tok);
2239
2240 if (ts->components.empty() ||
2241 ts->components.back().type != target_symbol::comp_pretty_print)
2242 throw semantic_error("invalid target_symbol for pretty-print", ts->tok);
7d11d8c9 2243 print_full = ts->components.back().member.length() > 1;
5f36109e
JS
2244 ts->components.pop_back();
2245}
2246
2247
2248functioncall*
2249dwarf_pretty_print::expand ()
2250{
2251 static unsigned tick = 0;
2252
2253 // function pretty_print_X([pointer], [arg1, arg2, ...]) {
7d11d8c9
JS
2254 // try {
2255 // return sprintf("{.foo=...}", (ts)->foo, ...)
2256 // } catch {
2257 // return "ERROR"
2258 // }
5f36109e
JS
2259 // }
2260
2261 // Create the function decl and call.
2262
2263 functiondecl *fdecl = new functiondecl;
2264 fdecl->tok = ts->tok;
2265 fdecl->synthetic = true;
2266 fdecl->name = "_dwarf_pretty_print_" + lex_cast(tick++);
2267 fdecl->type = pe_string;
2268
2269 functioncall* fcall = new functioncall;
2270 fcall->tok = ts->tok;
2271 fcall->function = fdecl->name;
140be17a 2272 fcall->type = pe_string;
5f36109e
JS
2273
2274 // If there's a <pointer>, replace it with a new var and make that
2275 // the first function argument.
2276 if (pointer)
2277 {
2278 vardecl *v = new vardecl;
2279 v->type = pe_long;
2280 v->name = "pointer";
2281 v->tok = ts->tok;
2282 fdecl->formal_args.push_back (v);
2283 fcall->args.push_back (pointer);
2284
2285 symbol* sym = new symbol;
2286 sym->tok = ts->tok;
2287 sym->name = v->name;
5f36109e
JS
2288 pointer = sym;
2289 }
2290
2291 // For each expression argument, replace it with a function argument.
2292 for (unsigned i = 0; i < ts->components.size(); ++i)
2293 if (ts->components[i].type == target_symbol::comp_expression_array_index)
2294 {
2295 vardecl *v = new vardecl;
2296 v->type = pe_long;
2297 v->name = "index" + lex_cast(i);
2298 v->tok = ts->tok;
2299 fdecl->formal_args.push_back (v);
2300 fcall->args.push_back (ts->components[i].expr_index);
2301
2302 symbol* sym = new symbol;
2303 sym->tok = ts->tok;
2304 sym->name = v->name;
5f36109e
JS
2305 ts->components[i].expr_index = sym;
2306 }
2307
2308 // Create the return sprintf.
2309 token* pf_tok = new token(*ts->tok);
2310 pf_tok->content = "sprintf";
2311 print_format* pf = print_format::create(pf_tok);
2312 return_statement* rs = new return_statement;
2313 rs->tok = ts->tok;
2314 rs->value = pf;
5f36109e
JS
2315
2316 // Recurse into the actual values.
7d11d8c9 2317 recurse (&base_type, ts, pf, true);
5f36109e
JS
2318 pf->components = print_format::string_to_components(pf->raw_components);
2319
7d11d8c9
JS
2320 // Create the try-catch net
2321 try_block* tb = new try_block;
2322 tb->tok = ts->tok;
2323 tb->try_block = rs;
2324 tb->catch_error_var = 0;
2325 return_statement* rs2 = new return_statement;
2326 rs2->tok = ts->tok;
2327 rs2->value = new literal_string ("ERROR");
2328 rs2->value->tok = ts->tok;
2329 tb->catch_block = rs2;
2330 fdecl->body = tb;
2331
f8809d54 2332 fdecl->join (dw.sess);
5f36109e
JS
2333 return fcall;
2334}
2335
2336
2337void
2338dwarf_pretty_print::recurse (Dwarf_Die* start_type, target_symbol* e,
7d11d8c9 2339 print_format* pf, bool top)
5f36109e
JS
2340{
2341 Dwarf_Die type;
2342 dw.resolve_unqualified_inner_typedie (start_type, &type, e);
2343
2344 switch (dwarf_tag(&type))
2345 {
2346 default:
2347 // XXX need a warning?
2348 // throw semantic_error ("unsupported type (tag " + lex_cast(dwarf_tag(&type))
2349 // + ") for " + dwarf_type_name(&type), e->tok);
2350 pf->raw_components.append("?");
2351 break;
2352
2353 case DW_TAG_enumeration_type:
2354 case DW_TAG_base_type:
7d11d8c9 2355 recurse_base (&type, e, pf);
5f36109e
JS
2356 break;
2357
2358 case DW_TAG_array_type:
7d11d8c9 2359 recurse_array (&type, e, pf, top);
5f36109e
JS
2360 break;
2361
2362 case DW_TAG_pointer_type:
2363 case DW_TAG_reference_type:
2364 case DW_TAG_rvalue_reference_type:
7d11d8c9 2365 recurse_pointer (&type, e, pf, top);
5f36109e
JS
2366 break;
2367
2368 case DW_TAG_subroutine_type:
c55ea10d 2369 push_deref (pf, "<function>:%p", e);
5f36109e
JS
2370 break;
2371
2372 case DW_TAG_union_type:
5f36109e
JS
2373 case DW_TAG_structure_type:
2374 case DW_TAG_class_type:
7d11d8c9 2375 recurse_struct (&type, e, pf, top);
5f36109e
JS
2376 break;
2377 }
2378}
2379
2380
2381void
2382dwarf_pretty_print::recurse_base (Dwarf_Die* type, target_symbol* e,
7d11d8c9 2383 print_format* pf)
5f36109e
JS
2384{
2385 Dwarf_Attribute attr;
2386 Dwarf_Word encoding = (Dwarf_Word) -1;
2387 dwarf_formudata (dwarf_attr_integrate (type, DW_AT_encoding, &attr),
2388 &encoding);
5f36109e
JS
2389 switch (encoding)
2390 {
2391 case DW_ATE_float:
2392 case DW_ATE_complex_float:
2393 // XXX need a warning?
2394 // throw semantic_error ("unsupported type (encoding " + lex_cast(encoding)
2395 // + ") for " + dwarf_type_name(type), e->tok);
2396 pf->raw_components.append("?");
5f36109e
JS
2397 break;
2398
2399 case DW_ATE_signed_char:
2400 case DW_ATE_unsigned_char:
c55ea10d 2401 push_deref (pf, "'%c'", e);
5f36109e
JS
2402 break;
2403
2404 case DW_ATE_unsigned:
c55ea10d 2405 push_deref (pf, "%u", e);
5f36109e
JS
2406 break;
2407
2408 default:
c55ea10d 2409 push_deref (pf, "%i", e);
5f36109e
JS
2410 break;
2411 }
5f36109e
JS
2412}
2413
2414
2415void
2416dwarf_pretty_print::recurse_array (Dwarf_Die* type, target_symbol* e,
7d11d8c9 2417 print_format* pf, bool top)
5f36109e 2418{
7d11d8c9
JS
2419 if (!top && !print_full)
2420 {
2421 pf->raw_components.append("[...]");
2422 return;
2423 }
2424
5f36109e
JS
2425 Dwarf_Die childtype;
2426 dwarf_attr_die (type, DW_AT_type, &childtype);
bbee5bb8
JS
2427
2428 if (print_chars (&childtype, e, pf))
2429 return;
2430
5f36109e
JS
2431 pf->raw_components.append("[");
2432
2433 // We print the array up to the first 5 elements.
2434 // XXX how can we determine the array size?
2435 // ... for now, just print the first element
64cddf39 2436 // NB: limit to 32 args; see PR10750 and c_unparser::visit_print_format.
5f36109e 2437 unsigned i, size = 1;
64cddf39 2438 for (i=0; i < size && i < 5 && pf->args.size() < 32; ++i)
5f36109e
JS
2439 {
2440 if (i > 0)
2441 pf->raw_components.append(", ");
2442 target_symbol* e2 = new target_symbol(*e);
2443 e2->components.push_back (target_symbol::component(e->tok, i));
7d11d8c9 2444 recurse (&childtype, e2, pf);
5f36109e
JS
2445 }
2446 if (i < size || 1/*XXX until real size is known */)
2447 pf->raw_components.append(", ...");
2448 pf->raw_components.append("]");
2449}
2450
2451
2452void
2453dwarf_pretty_print::recurse_pointer (Dwarf_Die* type, target_symbol* e,
7d11d8c9 2454 print_format* pf, bool top)
5f36109e 2455{
7d11d8c9 2456 // We chase to top-level pointers, but leave the rest alone
d19a9a82 2457 bool void_p = true;
7d11d8c9 2458 Dwarf_Die pointee;
bbee5bb8 2459 if (dwarf_attr_die (type, DW_AT_type, &pointee))
d19a9a82
JS
2460 {
2461 try
2462 {
2463 dw.resolve_unqualified_inner_typedie (&pointee, &pointee, e);
2464 void_p = false;
2465 }
2466 catch (const semantic_error&) {}
2467 }
2468
2469 if (!void_p)
5f36109e 2470 {
bbee5bb8
JS
2471 if (print_chars (&pointee, e, pf))
2472 return;
2473
2474 if (top)
2475 {
2476 recurse (&pointee, e, pf, top);
2477 return;
2478 }
5f36109e 2479 }
bbee5bb8 2480
c55ea10d 2481 push_deref (pf, "%p", e);
5f36109e
JS
2482}
2483
2484
2485void
2486dwarf_pretty_print::recurse_struct (Dwarf_Die* type, target_symbol* e,
7d11d8c9 2487 print_format* pf, bool top)
5f36109e 2488{
bdec0e18
JS
2489 if (dwarf_hasattr(type, DW_AT_declaration))
2490 {
a44a7cb5 2491 Dwarf_Die *resolved = dw.declaration_resolve(type);
bdec0e18
JS
2492 if (!resolved)
2493 {
2494 // could be an error, but for now just stub it
2495 // throw semantic_error ("unresolved " + dwarf_type_name(type), e->tok);
2496 pf->raw_components.append("{...}");
2497 return;
2498 }
2499 type = resolved;
2500 }
2501
5f36109e
JS
2502 int count = 0;
2503 pf->raw_components.append("{");
7d11d8c9
JS
2504 if (top || print_full)
2505 recurse_struct_members (type, e, pf, count);
2506 else
2507 pf->raw_components.append("...");
5f36109e
JS
2508 pf->raw_components.append("}");
2509}
2510
2511
2512void
2513dwarf_pretty_print::recurse_struct_members (Dwarf_Die* type, target_symbol* e,
7d11d8c9 2514 print_format* pf, int& count)
5f36109e
JS
2515{
2516 Dwarf_Die child, childtype;
2517 if (dwarf_child (type, &child) == 0)
2518 do
2519 {
2520 target_symbol* e2 = e;
2521
2522 // skip static members
2523 if (dwarf_hasattr(&child, DW_AT_declaration))
2524 continue;
2525
2526 int tag = dwarf_tag (&child);
2527
2528 if (tag != DW_TAG_member && tag != DW_TAG_inheritance)
2529 continue;
2530
2531 dwarf_attr_die (&child, DW_AT_type, &childtype);
2532
2533 if (tag == DW_TAG_inheritance)
2534 {
7d11d8c9 2535 recurse_struct_members (&childtype, e, pf, count);
5f36109e
JS
2536 continue;
2537 }
2538
2539 int childtag = dwarf_tag (&childtype);
2540 const char *member = dwarf_diename (&child);
3a147004
JS
2541
2542 // "_vptr.foo" members are C++ virtual function tables,
2543 // which (generally?) aren't interesting for users.
2544 if (member && startswith(member, "_vptr."))
2545 continue;
2546
5f36109e
JS
2547 if (++count > 1)
2548 pf->raw_components.append(", ");
64cddf39
JS
2549
2550 // NB: limit to 32 args; see PR10750 and c_unparser::visit_print_format.
2551 if (pf->args.size() >= 32)
2552 {
2553 pf->raw_components.append("...");
2554 break;
2555 }
2556
5f36109e
JS
2557 if (member)
2558 {
5f36109e
JS
2559 pf->raw_components.append(".");
2560 pf->raw_components.append(member);
2561
2562 e2 = new target_symbol(*e);
2563 e2->components.push_back (target_symbol::component(e->tok, member));
2564 }
2565 else if (childtag == DW_TAG_union_type)
2566 pf->raw_components.append("<union>");
2567 else if (childtag == DW_TAG_structure_type)
2568 pf->raw_components.append("<class>");
2569 else if (childtag == DW_TAG_class_type)
2570 pf->raw_components.append("<struct>");
2571 pf->raw_components.append("=");
7d11d8c9 2572 recurse (&childtype, e2, pf);
5f36109e
JS
2573 }
2574 while (dwarf_siblingof (&child, &child) == 0);
2575}
2576
2577
bbee5bb8
JS
2578bool
2579dwarf_pretty_print::print_chars (Dwarf_Die* start_type, target_symbol* e,
2580 print_format* pf)
2581{
2582 Dwarf_Die type;
2583 dw.resolve_unqualified_inner_typedie (start_type, &type, e);
2584 const char *name = dwarf_diename (&type);
2585 if (name && (name == string("char") || name == string("unsigned char")))
2586 {
c55ea10d
JS
2587 if (push_deref (pf, "\"%s\"", e))
2588 {
2589 // steal the last arg for a string access
2590 assert (!pf->args.empty());
2591 functioncall* fcall = new functioncall;
2592 fcall->tok = e->tok;
2593 fcall->function = userspace_p ? "user_string2" : "kernel_string2";
2594 fcall->args.push_back (pf->args.back());
2595 expression *err_msg = new literal_string ("<unknown>");
2596 err_msg->tok = e->tok;
2597 fcall->args.push_back (err_msg);
2598 pf->args.back() = fcall;
2599 }
bbee5bb8
JS
2600 return true;
2601 }
2602 return false;
2603}
2604
2605
5f36109e
JS
2606expression*
2607dwarf_pretty_print::deref (target_symbol* e)
2608{
2609 static unsigned tick = 0;
2610
d19a9a82
JS
2611 if (!deref_p)
2612 {
2613 assert (pointer && e->components.empty());
2614 return pointer;
2615 }
2616
5f36109e
JS
2617 // Synthesize a function to dereference the dwarf fields,
2618 // with a pointer parameter that is the base tracepoint variable
2619 functiondecl *fdecl = new functiondecl;
2620 fdecl->synthetic = true;
2621 fdecl->tok = e->tok;
2622 embeddedcode *ec = new embeddedcode;
2623 ec->tok = e->tok;
2624
2625 fdecl->name = "_dwarf_pretty_print_deref_" + lex_cast(tick++);
2626 fdecl->body = ec;
2627
2628 // Synthesize a functioncall.
2629 functioncall* fcall = new functioncall;
2630 fcall->tok = e->tok;
2631 fcall->function = fdecl->name;
5f36109e
JS
2632
2633 // PR10601: adapt to kernel-vs-userspace loc2c-runtime
2634 ec->code += "\n#define fetch_register " + string(userspace_p?"u":"k") + "_fetch_register\n";
2635 ec->code += "#define store_register " + string(userspace_p?"u":"k") + "_store_register\n";
2636
2637 if (pointer)
2638 {
2639 ec->code += dw.literal_stmt_for_pointer (&pointer_type, e,
2640 false, fdecl->type);
2641
2642 vardecl *v = new vardecl;
2643 v->type = pe_long;
2644 v->name = "pointer";
2645 v->tok = e->tok;
2646 fdecl->formal_args.push_back(v);
2647 fcall->args.push_back(pointer);
2648 }
2649 else if (!local.empty())
2650 ec->code += dw.literal_stmt_for_local (scopes, pc, local, e,
2651 false, fdecl->type);
2652 else
2653 ec->code += dw.literal_stmt_for_return (&scopes[0], pc, e,
2654 false, fdecl->type);
2655
2656 // Any non-literal indexes need to be passed in too.
2657 for (unsigned i = 0; i < e->components.size(); ++i)
2658 if (e->components[i].type == target_symbol::comp_expression_array_index)
2659 {
2660 vardecl *v = new vardecl;
2661 v->type = pe_long;
2662 v->name = "index" + lex_cast(i);
2663 v->tok = e->tok;
2664 fdecl->formal_args.push_back(v);
2665 fcall->args.push_back(e->components[i].expr_index);
2666 }
2667
2668 ec->code += "/* pure */";
2669 ec->code += "/* unprivileged */";
2670
2671 // PR10601
2672 ec->code += "\n#undef fetch_register\n";
2673 ec->code += "\n#undef store_register\n";
2674
f8809d54 2675 fdecl->join (dw.sess);
5f36109e
JS
2676 return fcall;
2677}
2678
2679
c55ea10d
JS
2680bool
2681dwarf_pretty_print::push_deref (print_format* pf, const string& fmt,
2682 target_symbol* e)
2683{
2684 expression* e2 = NULL;
2685 try
2686 {
2687 e2 = deref (e);
2688 }
2689 catch (const semantic_error&)
2690 {
2691 pf->raw_components.append ("?");
2692 return false;
2693 }
2694 pf->raw_components.append (fmt);
2695 pf->args.push_back (e2);
2696 return true;
2697}
2698
2699
e57b735a 2700void
a7999c82 2701dwarf_var_expanding_visitor::visit_target_symbol_saved_return (target_symbol* e)
e57b735a 2702{
a7999c82
JS
2703 // Get the full name of the target symbol.
2704 stringstream ts_name_stream;
2705 e->print(ts_name_stream);
2706 string ts_name = ts_name_stream.str();
2707
2708 // Check and make sure we haven't already seen this target
2709 // variable in this return probe. If we have, just return our
2710 // last replacement.
af234c40 2711 map<string, expression *>::iterator i = return_ts_map.find(ts_name);
a7999c82 2712 if (i != return_ts_map.end())
85ecf79a 2713 {
a7999c82
JS
2714 provide (i->second);
2715 return;
2716 }
85ecf79a 2717
70208613
JS
2718 // Attempt the expansion directly first, so if there's a problem with the
2719 // variable we won't have a bogus entry probe lying around. Like in
2720 // saveargs(), we pretend for a moment that we're not in a .return.
2721 bool saved_has_return = q.has_return;
2722 q.has_return = false;
2723 expression *repl = e;
2724 replace (repl);
2725 q.has_return = saved_has_return;
2726 target_symbol* n = dynamic_cast<target_symbol*>(repl);
2727 if (n && n->saved_conversion_error)
2728 {
2729 provide (repl);
2730 return;
2731 }
2732
af234c40
JS
2733 expression *exp;
2734 if (!q.has_process &&
2735 strverscmp(q.sess.kernel_base_release.c_str(), "2.6.25") >= 0)
140be17a 2736 exp = gen_kretprobe_saved_return(repl);
af234c40 2737 else
277c21bc 2738 exp = gen_mapped_saved_return(repl, e->name);
af234c40
JS
2739
2740 // Provide the variable to our parent so it can be used as a
2741 // substitute for the target symbol.
2742 provide (exp);
2743
2744 // Remember this replacement since we might be able to reuse
2745 // it later if the same return probe references this target
2746 // symbol again.
2747 return_ts_map[ts_name] = exp;
2748}
2749
2750expression*
70208613 2751dwarf_var_expanding_visitor::gen_mapped_saved_return(expression* e,
277c21bc 2752 const string& name)
af234c40 2753{
a7999c82
JS
2754 // We've got to do several things here to handle target
2755 // variables in return probes.
85ecf79a 2756
a7999c82
JS
2757 // (1) Synthesize two global arrays. One is the cache of the
2758 // target variable and the other contains a thread specific
2759 // nesting level counter. The arrays will look like
2760 // this:
2761 //
2762 // _dwarf_tvar_{name}_{num}
2763 // _dwarf_tvar_{name}_{num}_ctr
2764
2765 string aname = (string("_dwarf_tvar_")
277c21bc 2766 + name.substr(1)
aca66a36 2767 + "_" + lex_cast(tick++));
a7999c82
JS
2768 vardecl* vd = new vardecl;
2769 vd->name = aname;
2770 vd->tok = e->tok;
2771 q.sess.globals.push_back (vd);
2772
2773 string ctrname = aname + "_ctr";
2774 vd = new vardecl;
2775 vd->name = ctrname;
2776 vd->tok = e->tok;
2777 q.sess.globals.push_back (vd);
2778
2779 // (2) Create a new code block we're going to insert at the
2780 // beginning of this probe to get the cached value into a
2781 // temporary variable. We'll replace the target variable
2782 // reference with the temporary variable reference. The code
2783 // will look like this:
2784 //
2785 // _dwarf_tvar_tid = tid()
2786 // _dwarf_tvar_{name}_{num}_tmp
2787 // = _dwarf_tvar_{name}_{num}[_dwarf_tvar_tid,
2788 // _dwarf_tvar_{name}_{num}_ctr[_dwarf_tvar_tid]]
2789 // delete _dwarf_tvar_{name}_{num}[_dwarf_tvar_tid,
2790 // _dwarf_tvar_{name}_{num}_ctr[_dwarf_tvar_tid]--]
2791 // if (! _dwarf_tvar_{name}_{num}_ctr[_dwarf_tvar_tid])
2792 // delete _dwarf_tvar_{name}_{num}_ctr[_dwarf_tvar_tid]
2793
2794 // (2a) Synthesize the tid temporary expression, which will look
2795 // like this:
2796 //
2797 // _dwarf_tvar_tid = tid()
2798 symbol* tidsym = new symbol;
2799 tidsym->name = string("_dwarf_tvar_tid");
2800 tidsym->tok = e->tok;
85ecf79a 2801
a7999c82
JS
2802 if (add_block == NULL)
2803 {
2804 add_block = new block;
2805 add_block->tok = e->tok;
8cc799a5 2806 }
8c819921 2807
8cc799a5
JS
2808 if (!add_block_tid)
2809 {
a7999c82
JS
2810 // Synthesize a functioncall to grab the thread id.
2811 functioncall* fc = new functioncall;
2812 fc->tok = e->tok;
2813 fc->function = string("tid");
8c819921 2814
a7999c82 2815 // Assign the tid to '_dwarf_tvar_tid'.
8c819921
DS
2816 assignment* a = new assignment;
2817 a->tok = e->tok;
2818 a->op = "=";
a7999c82
JS
2819 a->left = tidsym;
2820 a->right = fc;
8c819921
DS
2821
2822 expr_statement* es = new expr_statement;
2823 es->tok = e->tok;
2824 es->value = a;
8c819921 2825 add_block->statements.push_back (es);
8cc799a5 2826 add_block_tid = true;
a7999c82 2827 }
8c819921 2828
a7999c82
JS
2829 // (2b) Synthesize an array reference and assign it to a
2830 // temporary variable (that we'll use as replacement for the
2831 // target variable reference). It will look like this:
2832 //
2833 // _dwarf_tvar_{name}_{num}_tmp
2834 // = _dwarf_tvar_{name}_{num}[_dwarf_tvar_tid,
2835 // _dwarf_tvar_{name}_{num}_ctr[_dwarf_tvar_tid]]
2836
2837 arrayindex* ai_tvar_base = new arrayindex;
2838 ai_tvar_base->tok = e->tok;
2839
2840 symbol* sym = new symbol;
2841 sym->name = aname;
2842 sym->tok = e->tok;
2843 ai_tvar_base->base = sym;
2844
2845 ai_tvar_base->indexes.push_back(tidsym);
2846
2847 // We need to create a copy of the array index in its current
2848 // state so we can have 2 variants of it (the original and one
2849 // that post-decrements the second index).
2850 arrayindex* ai_tvar = new arrayindex;
2851 arrayindex* ai_tvar_postdec = new arrayindex;
2852 *ai_tvar = *ai_tvar_base;
2853 *ai_tvar_postdec = *ai_tvar_base;
2854
2855 // Synthesize the
2856 // "_dwarf_tvar_{name}_{num}_ctr[_dwarf_tvar_tid]" used as the
2857 // second index into the array.
2858 arrayindex* ai_ctr = new arrayindex;
2859 ai_ctr->tok = e->tok;
2860
2861 sym = new symbol;
2862 sym->name = ctrname;
2863 sym->tok = e->tok;
2864 ai_ctr->base = sym;
2865 ai_ctr->indexes.push_back(tidsym);
2866 ai_tvar->indexes.push_back(ai_ctr);
2867
2868 symbol* tmpsym = new symbol;
2869 tmpsym->name = aname + "_tmp";
2870 tmpsym->tok = e->tok;
2871
2872 assignment* a = new assignment;
2873 a->tok = e->tok;
2874 a->op = "=";
2875 a->left = tmpsym;
2876 a->right = ai_tvar;
2877
2878 expr_statement* es = new expr_statement;
2879 es->tok = e->tok;
2880 es->value = a;
2881
2882 add_block->statements.push_back (es);
2883
2884 // (2c) Add a post-decrement to the second array index and
2885 // delete the array value. It will look like this:
2886 //
2887 // delete _dwarf_tvar_{name}_{num}[_dwarf_tvar_tid,
2888 // _dwarf_tvar_{name}_{num}_ctr[_dwarf_tvar_tid]--]
2889
2890 post_crement* pc = new post_crement;
2891 pc->tok = e->tok;
2892 pc->op = "--";
2893 pc->operand = ai_ctr;
2894 ai_tvar_postdec->indexes.push_back(pc);
2895
2896 delete_statement* ds = new delete_statement;
2897 ds->tok = e->tok;
2898 ds->value = ai_tvar_postdec;
2899
2900 add_block->statements.push_back (ds);
2901
2902 // (2d) Delete the counter value if it is 0. It will look like
2903 // this:
2904 // if (! _dwarf_tvar_{name}_{num}_ctr[_dwarf_tvar_tid])
2905 // delete _dwarf_tvar_{name}_{num}_ctr[_dwarf_tvar_tid]
2906
2907 ds = new delete_statement;
2908 ds->tok = e->tok;
2909 ds->value = ai_ctr;
2910
2911 unary_expression *ue = new unary_expression;
2912 ue->tok = e->tok;
2913 ue->op = "!";
2914 ue->operand = ai_ctr;
2915
2916 if_statement *ifs = new if_statement;
2917 ifs->tok = e->tok;
2918 ifs->condition = ue;
2919 ifs->thenblock = ds;
2920 ifs->elseblock = NULL;
2921
2922 add_block->statements.push_back (ifs);
2923
2924 // (3) We need an entry probe that saves the value for us in the
2925 // global array we created. Create the entry probe, which will
2926 // look like this:
2927 //
2260f4e3 2928 // probe kernel.function("{function}").call {
a7999c82
JS
2929 // _dwarf_tvar_tid = tid()
2930 // _dwarf_tvar_{name}_{num}[_dwarf_tvar_tid,
2931 // ++_dwarf_tvar_{name}_{num}_ctr[_dwarf_tvar_tid]]
2932 // = ${param}
2933 // }
2934
2260f4e3 2935 if (add_call_probe == NULL)
a7999c82 2936 {
2260f4e3
FCE
2937 add_call_probe = new block;
2938 add_call_probe->tok = e->tok;
8cc799a5 2939 }
4baf0e53 2940
8cc799a5
JS
2941 if (!add_call_probe_tid)
2942 {
a7999c82
JS
2943 // Synthesize a functioncall to grab the thread id.
2944 functioncall* fc = new functioncall;
2945 fc->tok = e->tok;
2946 fc->function = string("tid");
4baf0e53 2947
a7999c82
JS
2948 // Assign the tid to '_dwarf_tvar_tid'.
2949 assignment* a = new assignment;
8fc05e57
DS
2950 a->tok = e->tok;
2951 a->op = "=";
a7999c82
JS
2952 a->left = tidsym;
2953 a->right = fc;
8fc05e57 2954
a7999c82 2955 expr_statement* es = new expr_statement;
8fc05e57
DS
2956 es->tok = e->tok;
2957 es->value = a;
2260f4e3 2958 add_call_probe = new block(add_call_probe, es);
8cc799a5 2959 add_call_probe_tid = true;
85ecf79a 2960 }
cf2a1f85 2961
a7999c82
JS
2962 // Save the value, like this:
2963 // _dwarf_tvar_{name}_{num}[_dwarf_tvar_tid,
2964 // ++_dwarf_tvar_{name}_{num}_ctr[_dwarf_tvar_tid]]
2965 // = ${param}
2966 arrayindex* ai_tvar_preinc = new arrayindex;
2967 *ai_tvar_preinc = *ai_tvar_base;
2968
2969 pre_crement* preinc = new pre_crement;
2970 preinc->tok = e->tok;
2971 preinc->op = "++";
2972 preinc->operand = ai_ctr;
2973 ai_tvar_preinc->indexes.push_back(preinc);
2974
2975 a = new assignment;
2976 a->tok = e->tok;
2977 a->op = "=";
2978 a->left = ai_tvar_preinc;
2979 a->right = e;
2980
2981 es = new expr_statement;
2982 es->tok = e->tok;
2983 es->value = a;
2984
2260f4e3 2985 add_call_probe = new block(add_call_probe, es);
a7999c82
JS
2986
2987 // (4) Provide the '_dwarf_tvar_{name}_{num}_tmp' variable to
2988 // our parent so it can be used as a substitute for the target
2989 // symbol.
af234c40
JS
2990 return tmpsym;
2991}
a7999c82 2992
af234c40
JS
2993
2994expression*
140be17a 2995dwarf_var_expanding_visitor::gen_kretprobe_saved_return(expression* e)
af234c40
JS
2996{
2997 // The code for this is simple.
2998 //
2999 // .call:
3000 // _set_kretprobe_long(index, $value)
3001 //
3002 // .return:
3003 // _get_kretprobe_long(index)
3004 //
3005 // (or s/long/string/ for things like $$parms)
3006
3007 unsigned index;
3008 string setfn, getfn;
3009
140be17a
JS
3010 // We need the caller to predetermine the type of the expression!
3011 switch (e->type)
af234c40 3012 {
140be17a 3013 case pe_string:
af234c40
JS
3014 index = saved_strings++;
3015 setfn = "_set_kretprobe_string";
3016 getfn = "_get_kretprobe_string";
140be17a
JS
3017 break;
3018 case pe_long:
af234c40
JS
3019 index = saved_longs++;
3020 setfn = "_set_kretprobe_long";
3021 getfn = "_get_kretprobe_long";
140be17a
JS
3022 break;
3023 default:
3024 throw semantic_error("unknown type to save in kretprobe", e->tok);
af234c40
JS
3025 }
3026
3027 // Create the entry code
3028 // _set_kretprobe_{long|string}(index, $value)
3029
3030 if (add_call_probe == NULL)
3031 {
3032 add_call_probe = new block;
3033 add_call_probe->tok = e->tok;
3034 }
3035
3036 functioncall* set_fc = new functioncall;
3037 set_fc->tok = e->tok;
3038 set_fc->function = setfn;
3039 set_fc->args.push_back(new literal_number(index));
3040 set_fc->args.back()->tok = e->tok;
3041 set_fc->args.push_back(e);
3042
3043 expr_statement* set_es = new expr_statement;
3044 set_es->tok = e->tok;
3045 set_es->value = set_fc;
3046
3047 add_call_probe->statements.push_back(set_es);
3048
3049 // Create the return code
3050 // _get_kretprobe_{long|string}(index)
3051
3052 functioncall* get_fc = new functioncall;
3053 get_fc->tok = e->tok;
3054 get_fc->function = getfn;
3055 get_fc->args.push_back(new literal_number(index));
3056 get_fc->args.back()->tok = e->tok;
3057
3058 return get_fc;
a7999c82 3059}
a43ba433 3060
2cb3fe26 3061
a7999c82
JS
3062void
3063dwarf_var_expanding_visitor::visit_target_symbol_context (target_symbol* e)
3064{
9aa8ffce 3065 if (null_die(scope_die))
a7999c82 3066 return;
2cb3fe26 3067
5f36109e
JS
3068 target_symbol *tsym = new target_symbol(*e);
3069
fde50242
JS
3070 bool pretty = (!e->components.empty() &&
3071 e->components[0].type == target_symbol::comp_pretty_print);
3072 string format = pretty ? "=%s" : "=%#x";
a43ba433 3073
a7999c82
JS
3074 // Convert $$parms to sprintf of a list of parms and active local vars
3075 // which we recursively evaluate
a43ba433 3076
a7999c82
JS
3077 // NB: we synthesize a new token here rather than reusing
3078 // e->tok, because print_format::print likes to use
3079 // its tok->content.
5f36109e 3080 token* pf_tok = new token(*e->tok);
a7999c82 3081 pf_tok->type = tok_identifier;
b393f6f2 3082 pf_tok->content = "sprintf";
2cb3fe26 3083
d5e178c1 3084 print_format* pf = print_format::create(pf_tok);
a7999c82 3085
277c21bc 3086 if (q.has_return && (e->name == "$$return"))
a7999c82 3087 {
277c21bc 3088 tsym->name = "$return";
a7999c82
JS
3089
3090 // Ignore any variable that isn't accessible.
3091 tsym->saved_conversion_error = 0;
3092 expression *texp = tsym;
8b095b45 3093 replace (texp); // NB: throws nothing ...
a7999c82 3094 if (tsym->saved_conversion_error) // ... but this is how we know it happened.
a43ba433 3095 {
2cb3fe26 3096
a43ba433
FCE
3097 }
3098 else
3099 {
a7999c82 3100 pf->raw_components += "return";
5f36109e 3101 pf->raw_components += format;
a7999c82
JS
3102 pf->args.push_back(texp);
3103 }
3104 }
3105 else
3106 {
3107 // non-.return probe: support $$parms, $$vars, $$locals
345bbb3d 3108 bool first = true;
a7999c82 3109 Dwarf_Die result;
d48bc7eb
JS
3110 vector<Dwarf_Die> scopes = q.dw.getscopes(scope_die);
3111 for (unsigned i = 0; i < scopes.size(); ++i)
3112 {
3113 if (dwarf_tag(&scopes[i]) == DW_TAG_compile_unit)
3114 break; // we don't want file-level variables
3115 if (dwarf_child (&scopes[i], &result) == 0)
3116 do
00cf3709 3117 {
d48bc7eb
JS
3118 switch (dwarf_tag (&result))
3119 {
3120 case DW_TAG_variable:
3121 if (e->name == "$$parms")
3122 continue;
3123 break;
3124 case DW_TAG_formal_parameter:
3125 if (e->name == "$$locals")
3126 continue;
3127 break;
3128
3129 default:
3130 continue;
3131 }
41c262f3 3132
d48bc7eb
JS
3133 const char *diename = dwarf_diename (&result);
3134 if (! diename) continue;
f76427a2 3135
d48bc7eb
JS
3136 if (! first)
3137 pf->raw_components += " ";
3138 pf->raw_components += diename;
fde50242
JS
3139 first = false;
3140
3141 // Write a placeholder for ugly aggregates
3142 Dwarf_Die type;
3143 if (!pretty && dwarf_attr_die(&result, DW_AT_type, &type))
3144 {
3145 q.dw.resolve_unqualified_inner_typedie(&type, &type, e);
3146 switch (dwarf_tag(&type))
3147 {
3148 case DW_TAG_union_type:
3149 case DW_TAG_structure_type:
3150 case DW_TAG_class_type:
3151 pf->raw_components += "={...}";
3152 continue;
3153
3154 case DW_TAG_array_type:
3155 pf->raw_components += "=[...]";
3156 continue;
3157 }
3158 }
345bbb3d 3159
d48bc7eb
JS
3160 tsym->name = "$";
3161 tsym->name += diename;
41c262f3 3162
d48bc7eb
JS
3163 // Ignore any variable that isn't accessible.
3164 tsym->saved_conversion_error = 0;
3165 expression *texp = tsym;
3166 replace (texp); // NB: throws nothing ...
3167 if (tsym->saved_conversion_error) // ... but this is how we know it happened.
a43ba433 3168 {
d48bc7eb
JS
3169 if (q.sess.verbose>2)
3170 {
3171 for (semantic_error *c = tsym->saved_conversion_error;
3172 c != 0;
3173 c = c->chain) {
3174 clog << "variable location problem: " << c->what() << endl;
3175 }
3176 }
3177
3178 pf->raw_components += "=?";
a43ba433 3179 }
d48bc7eb
JS
3180 else
3181 {
3182 pf->raw_components += format;
3183 pf->args.push_back(texp);
3184 }
a7999c82 3185 }
d48bc7eb
JS
3186 while (dwarf_siblingof (&result, &result) == 0);
3187 }
a7999c82 3188 }
2cb3fe26 3189
a7999c82 3190 pf->components = print_format::string_to_components(pf->raw_components);
140be17a 3191 pf->type = pe_string;
a7999c82
JS
3192 provide (pf);
3193}
3194
2cb3fe26 3195
a7999c82
JS
3196void
3197dwarf_var_expanding_visitor::visit_target_symbol (target_symbol *e)
3198{
277c21bc 3199 assert(e->name.size() > 0 && e->name[0] == '$');
a7999c82 3200 visited = true;
30263a73
FCE
3201 bool defined_being_checked = (defined_ops.size() > 0 && (defined_ops.top()->operand == e));
3202 // In this mode, we avoid hiding errors or generating extra code such as for .return saved $vars
a7999c82 3203
70208613 3204 try
a7999c82 3205 {
c69a87e0
FCE
3206 bool lvalue = is_active_lvalue(e);
3207 if (lvalue && !q.sess.guru_mode)
3208 throw semantic_error("write to target variable not permitted", e->tok);
2cb3fe26 3209
c69a87e0 3210 // XXX: process $context vars should be writeable
70208613 3211
c69a87e0
FCE
3212 // See if we need to generate a new probe to save/access function
3213 // parameters from a return probe. PR 1382.
3214 if (q.has_return
3215 && !defined_being_checked
277c21bc
JS
3216 && e->name != "$return" // not the special return-value variable handled below
3217 && e->name != "$$return") // nor the other special variable handled below
c69a87e0
FCE
3218 {
3219 if (lvalue)
3220 throw semantic_error("write to target variable not permitted in .return probes", e->tok);
3221 visit_target_symbol_saved_return(e);
3222 return;
3223 }
e57b735a 3224
277c21bc
JS
3225 if (e->name == "$$vars" || e->name == "$$parms" || e->name == "$$locals"
3226 || (q.has_return && (e->name == "$$return")))
c69a87e0
FCE
3227 {
3228 if (lvalue)
3229 throw semantic_error("cannot write to context variable", e->tok);
70208613 3230
c69a87e0
FCE
3231 if (e->addressof)
3232 throw semantic_error("cannot take address of context variable", e->tok);
70208613 3233
5f36109e
JS
3234 e->assert_no_components("dwarf", true);
3235
c69a87e0
FCE
3236 visit_target_symbol_context(e);
3237 return;
3238 }
70208613 3239
5f36109e
JS
3240 if (!e->components.empty() &&
3241 e->components.back().type == target_symbol::comp_pretty_print)
3242 {
3243 if (lvalue)
3244 throw semantic_error("cannot write to pretty-printed variable", e->tok);
3245
277c21bc 3246 if (q.has_return && (e->name == "$return"))
5f36109e
JS
3247 {
3248 dwarf_pretty_print dpp (q.dw, scope_die, addr,
3249 q.has_process, *e);
3250 dpp.expand()->visit(this);
3251 }
3252 else
3253 {
3254 dwarf_pretty_print dpp (q.dw, getscopes(e), addr,
277c21bc 3255 e->name.substr(1),
5f36109e
JS
3256 q.has_process, *e);
3257 dpp.expand()->visit(this);
3258 }
3259 return;
3260 }
3261
c69a87e0
FCE
3262 // Synthesize a function.
3263 functiondecl *fdecl = new functiondecl;
59de45f1 3264 fdecl->synthetic = true;
c69a87e0
FCE
3265 fdecl->tok = e->tok;
3266 embeddedcode *ec = new embeddedcode;
3267 ec->tok = e->tok;
70208613 3268
c69a87e0 3269 string fname = (string(lvalue ? "_dwarf_tvar_set" : "_dwarf_tvar_get")
277c21bc 3270 + "_" + e->name.substr(1)
c69a87e0 3271 + "_" + lex_cast(tick++));
70208613 3272
b5a0dd41
FCE
3273 // PR10601: adapt to kernel-vs-userspace loc2c-runtime
3274 ec->code += "\n#define fetch_register " + string(q.has_process?"u":"k") + "_fetch_register\n";
3275 ec->code += "#define store_register " + string(q.has_process?"u":"k") + "_store_register\n";
70208613 3276
277c21bc 3277 if (q.has_return && (e->name == "$return"))
e19fda4e 3278 {
b5a0dd41 3279 ec->code += q.dw.literal_stmt_for_return (scope_die,
e19fda4e 3280 addr,
b4c34c26 3281 e,
e19fda4e
DS
3282 lvalue,
3283 fdecl->type);
3284 }
3285 else
3286 {
b5a0dd41 3287 ec->code += q.dw.literal_stmt_for_local (getscopes(e),
e19fda4e 3288 addr,
277c21bc 3289 e->name.substr(1),
b4c34c26 3290 e,
e19fda4e
DS
3291 lvalue,
3292 fdecl->type);
3293 }
3294
1b07c728
FCE
3295 if (! lvalue)
3296 ec->code += "/* pure */";
64211010
DB
3297
3298 ec->code += "/* unprivileged */";
b5a0dd41
FCE
3299
3300 // PR10601
3301 ec->code += "\n#undef fetch_register\n";
3302 ec->code += "\n#undef store_register\n";
c69a87e0
FCE
3303
3304 fdecl->name = fname;
3305 fdecl->body = ec;
70208613 3306
c69a87e0
FCE
3307 // Any non-literal indexes need to be passed in too.
3308 for (unsigned i = 0; i < e->components.size(); ++i)
3309 if (e->components[i].type == target_symbol::comp_expression_array_index)
3310 {
3311 vardecl *v = new vardecl;
3312 v->type = pe_long;
3313 v->name = "index" + lex_cast(i);
3314 v->tok = e->tok;
3315 fdecl->formal_args.push_back(v);
3316 }
70208613 3317
c69a87e0
FCE
3318 if (lvalue)
3319 {
3320 // Modify the fdecl so it carries a single pe_long formal
3321 // argument called "value".
70208613 3322
c69a87e0
FCE
3323 // FIXME: For the time being we only support setting target
3324 // variables which have base types; these are 'pe_long' in
3325 // stap's type vocabulary. Strings and pointers might be
3326 // reasonable, some day, but not today.
70208613 3327
c69a87e0
FCE
3328 vardecl *v = new vardecl;
3329 v->type = pe_long;
3330 v->name = "value";
3331 v->tok = e->tok;
3332 fdecl->formal_args.push_back(v);
3333 }
f8809d54 3334 fdecl->join (q.sess);
70208613 3335
c69a87e0
FCE
3336 // Synthesize a functioncall.
3337 functioncall* n = new functioncall;
3338 n->tok = e->tok;
3339 n->function = fname;
140be17a 3340 n->type = fdecl->type;
70208613 3341
c69a87e0
FCE
3342 // Any non-literal indexes need to be passed in too.
3343 for (unsigned i = 0; i < e->components.size(); ++i)
3344 if (e->components[i].type == target_symbol::comp_expression_array_index)
3345 n->args.push_back(require(e->components[i].expr_index));
70208613 3346
c69a87e0
FCE
3347 if (lvalue)
3348 {
3349 // Provide the functioncall to our parent, so that it can be
3350 // used to substitute for the assignment node immediately above
3351 // us.
3352 assert(!target_symbol_setter_functioncalls.empty());
3353 *(target_symbol_setter_functioncalls.top()) = n;
3354 }
70208613 3355
c69a87e0 3356 provide (n);
66d284f4
FCE
3357 }
3358 catch (const semantic_error& er)
3359 {
9fab2262
JS
3360 // We suppress this error message, and pass the unresolved
3361 // target_symbol to the next pass. We hope that this value ends
3362 // up not being referenced after all, so it can be optimized out
3363 // quietly.
1af1e62d 3364 e->chain (er);
9fab2262 3365 provide (e);
66d284f4 3366 }
77de5e9e
GH
3367}
3368
3369
c24447be
JS
3370void
3371dwarf_var_expanding_visitor::visit_cast_op (cast_op *e)
3372{
3373 // Fill in our current module context if needed
3374 if (e->module.empty())
3375 e->module = q.dw.module_name;
3376
3377 var_expanding_visitor::visit_cast_op(e);
3378}
3379
3380
8cc799a5
JS
3381void
3382dwarf_var_expanding_visitor::visit_entry_op (entry_op *e)
3383{
3384 expression *repl = e;
3385 if (q.has_return)
3386 {
3387 // expand the operand as if it weren't a return probe
3388 q.has_return = false;
3389 replace (e->operand);
3390 q.has_return = true;
3391
3392 // XXX it would be nice to use gen_kretprobe_saved_return when available,
3393 // but it requires knowing the types already, which is problematic for
3394 // arbitrary expressons.
3395 repl = gen_mapped_saved_return (e->operand, "@entry");
3396 }
3397 provide (repl);
3398}
3399
3400
729455a7
JS
3401vector<Dwarf_Die>&
3402dwarf_var_expanding_visitor::getscopes(target_symbol *e)
3403{
3404 if (scopes.empty())
3405 {
f5958c8f 3406 scopes = q.dw.getscopes(scope_die);
729455a7
JS
3407 if (scopes.empty())
3408 throw semantic_error ("unable to find any scopes containing "
3409 + lex_cast_hex(addr)
3410 + ((scope_die == NULL) ? ""
3411 : (string (" in ")
3412 + (dwarf_diename(scope_die) ?: "<unknown>")
3413 + "(" + (dwarf_diename(q.dw.cu) ?: "<unknown>")
3414 + ")"))
3415 + " while searching for local '"
277c21bc 3416 + e->name.substr(1) + "'",
729455a7
JS
3417 e->tok);
3418 }
3419 return scopes;
3420}
3421
3422
5f36109e
JS
3423struct dwarf_cast_expanding_visitor: public var_expanding_visitor
3424{
3425 systemtap_session& s;
3426 dwarf_builder& db;
3427
3428 dwarf_cast_expanding_visitor(systemtap_session& s, dwarf_builder& db):
3429 s(s), db(db) {}
3430 void visit_cast_op (cast_op* e);
3431 void filter_special_modules(string& module);
3432};
3433
3434
c4ce66a1
JS
3435struct dwarf_cast_query : public base_query
3436{
946e1a48 3437 cast_op& e;
c4ce66a1 3438 const bool lvalue;
5f36109e
JS
3439 const bool userspace_p;
3440 functioncall*& result;
c4ce66a1 3441
5f36109e
JS
3442 dwarf_cast_query(dwflpp& dw, const string& module, cast_op& e, bool lvalue,
3443 const bool userspace_p, functioncall*& result):
abb41d92 3444 base_query(dw, module), e(e), lvalue(lvalue),
5f36109e 3445 userspace_p(userspace_p), result(result) {}
c4ce66a1
JS
3446
3447 void handle_query_module();
c4ce66a1
JS
3448};
3449
3450
c4ce66a1
JS
3451void
3452dwarf_cast_query::handle_query_module()
3453{
5f36109e
JS
3454 static unsigned tick = 0;
3455
3456 if (result)
c4ce66a1
JS
3457 return;
3458
ea1e477a 3459 // look for the type in any CU
a44a7cb5
JS
3460 Dwarf_Die* type_die = NULL;
3461 if (startswith(e.type_name, "class "))
3462 {
3463 // normalize to match dwflpp::global_alias_caching_callback
3464 string struct_name = "struct " + e.type_name.substr(6);
3465 type_die = dw.declaration_resolve_other_cus(struct_name);
3466 }
3467 else
3468 type_die = dw.declaration_resolve_other_cus(e.type_name);
3469
3470 // NB: We now index the types as "struct name"/"union name"/etc. instead of
3471 // just "name". But since we didn't require users to be explicit before, and
3472 // actually sort of discouraged it, we must be flexible now. So if a lookup
3473 // fails with a bare name, try augmenting it.
3474 if (!type_die &&
3475 !startswith(e.type_name, "class ") &&
3476 !startswith(e.type_name, "struct ") &&
3477 !startswith(e.type_name, "union ") &&
3478 !startswith(e.type_name, "enum "))
3479 {
3480 type_die = dw.declaration_resolve_other_cus("struct " + e.type_name);
3481 if (!type_die)
3482 type_die = dw.declaration_resolve_other_cus("union " + e.type_name);
3483 if (!type_die)
3484 type_die = dw.declaration_resolve_other_cus("enum " + e.type_name);
3485 }
3486
ea1e477a
JS
3487 if (!type_die)
3488 return;
c4ce66a1 3489
5f36109e
JS
3490 string code;
3491 exp_type type = pe_long;
3492
ea1e477a 3493 try
c4ce66a1 3494 {
ea1e477a
JS
3495 Dwarf_Die cu_mem;
3496 dw.focus_on_cu(dwarf_diecu(type_die, &cu_mem, NULL, NULL));
5f36109e
JS
3497
3498 if (!e.components.empty() &&
3499 e.components.back().type == target_symbol::comp_pretty_print)
3500 {
3501 if (lvalue)
3502 throw semantic_error("cannot write to pretty-printed variable", e.tok);
3503
d19a9a82 3504 dwarf_pretty_print dpp(dw, type_die, e.operand, true, userspace_p, e);
5f36109e
JS
3505 result = dpp.expand();
3506 return;
3507 }
3508
3509 code = dw.literal_stmt_for_pointer (type_die, &e, lvalue, type);
ea1e477a
JS
3510 }
3511 catch (const semantic_error& er)
3512 {
3513 // NB: we can have multiple errors, since a @cast
1af1e62d
JS
3514 // may be attempted using several different modules:
3515 // @cast(ptr, "type", "module1:module2:...")
3516 e.chain (er);
c4ce66a1 3517 }
c4ce66a1 3518
5f36109e
JS
3519 if (code.empty())
3520 return;
c4ce66a1 3521
5f36109e 3522 string fname = (string(lvalue ? "_dwarf_cast_set" : "_dwarf_cast_get")
277c21bc 3523 + "_" + e.name.substr(1)
5f36109e 3524 + "_" + lex_cast(tick++));
c4ce66a1 3525
5f36109e
JS
3526 // Synthesize a function.
3527 functiondecl *fdecl = new functiondecl;
3528 fdecl->synthetic = true;
3529 fdecl->tok = e.tok;
3530 fdecl->type = type;
3531 fdecl->name = fname;
3532
3533 embeddedcode *ec = new embeddedcode;
3534 ec->tok = e.tok;
3535 fdecl->body = ec;
3536
3537 // PR10601: adapt to kernel-vs-userspace loc2c-runtime
3538 ec->code += "\n#define fetch_register " + string(userspace_p?"u":"k") + "_fetch_register\n";
3539 ec->code += "#define store_register " + string(userspace_p?"u":"k") + "_store_register\n";
3540
3541 ec->code += code;
3542
3543 // Give the fdecl an argument for the pointer we're trying to cast
3544 vardecl *v1 = new vardecl;
3545 v1->type = pe_long;
3546 v1->name = "pointer";
3547 v1->tok = e.tok;
3548 fdecl->formal_args.push_back(v1);
3549
3550 // Any non-literal indexes need to be passed in too.
3551 for (unsigned i = 0; i < e.components.size(); ++i)
3552 if (e.components[i].type == target_symbol::comp_expression_array_index)
3553 {
3554 vardecl *v = new vardecl;
3555 v->type = pe_long;
3556 v->name = "index" + lex_cast(i);
3557 v->tok = e.tok;
3558 fdecl->formal_args.push_back(v);
3559 }
3560
3561 if (lvalue)
3562 {
3563 // Modify the fdecl so it carries a second pe_long formal
3564 // argument called "value".
3565
3566 // FIXME: For the time being we only support setting target
3567 // variables which have base types; these are 'pe_long' in
3568 // stap's type vocabulary. Strings and pointers might be
3569 // reasonable, some day, but not today.
3570
3571 vardecl *v2 = new vardecl;
3572 v2->type = pe_long;
3573 v2->name = "value";
3574 v2->tok = e.tok;
3575 fdecl->formal_args.push_back(v2);
3576 }
3577 else
3578 ec->code += "/* pure */";
3579
3580 ec->code += "/* unprivileged */";
3581
3582 // PR10601
3583 ec->code += "\n#undef fetch_register\n";
3584 ec->code += "\n#undef store_register\n";
3585
f8809d54 3586 fdecl->join (dw.sess);
5f36109e
JS
3587
3588 // Synthesize a functioncall.
3589 functioncall* n = new functioncall;
3590 n->tok = e.tok;
3591 n->function = fname;
5f36109e
JS
3592 n->args.push_back(e.operand);
3593
3594 // Any non-literal indexes need to be passed in too.
3595 for (unsigned i = 0; i < e.components.size(); ++i)
3596 if (e.components[i].type == target_symbol::comp_expression_array_index)
3597 n->args.push_back(e.components[i].expr_index);
3598
3599 result = n;
3600}
c4ce66a1
JS
3601
3602
fb0274bc
JS
3603void dwarf_cast_expanding_visitor::filter_special_modules(string& module)
3604{
d90053e7 3605 // look for "<path/to/header>" or "kernel<path/to/header>"
fb0274bc 3606 // for those cases, build a module including that header
d90053e7 3607 if (module[module.size() - 1] == '>' &&
60d98537 3608 (module[0] == '<' || startswith(module, "kernel<")))
fb0274bc
JS
3609 {
3610 string cached_module;
3611 if (s.use_cache)
3612 {
3613 // see if the cached module exists
a2639cb7 3614 cached_module = find_typequery_hash(s, module);
d105f664 3615 if (!cached_module.empty() && !s.poison_cache)
fb0274bc
JS
3616 {
3617 int fd = open(cached_module.c_str(), O_RDONLY);
3618 if (fd != -1)
3619 {
3620 if (s.verbose > 2)
3621 clog << "Pass 2: using cached " << cached_module << endl;
3622 module = cached_module;
3623 close(fd);
3624 return;
3625 }
3626 }
3627 }
3628
3629 // no cached module, time to make it
d90053e7 3630 if (make_typequery(s, module) == 0)
fb0274bc 3631 {
e16dc041 3632 // try to save typequery in the cache
fb0274bc 3633 if (s.use_cache)
e16dc041 3634 copy_file(module, cached_module, s.verbose > 2);
fb0274bc
JS
3635 }
3636 }
3637}
3638
3639
c4ce66a1
JS
3640void dwarf_cast_expanding_visitor::visit_cast_op (cast_op* e)
3641{
3642 bool lvalue = is_active_lvalue(e);
3643 if (lvalue && !s.guru_mode)
3644 throw semantic_error("write to typecast value not permitted", e->tok);
3645
3646 if (e->module.empty())
3647 e->module = "kernel"; // "*" may also be reasonable to search all kernel modules
3648
5f36109e 3649 functioncall* result = NULL;
8b31197b
JS
3650
3651 // split the module string by ':' for alternatives
3652 vector<string> modules;
3653 tokenize(e->module, modules, ":");
b5a0dd41 3654 bool userspace_p=false; // PR10601
5f36109e 3655 for (unsigned i = 0; !result && i < modules.size(); ++i)
c4ce66a1 3656 {
8b31197b 3657 string& module = modules[i];
fb0274bc 3658 filter_special_modules(module);
abb41d92 3659
c4ce66a1
JS
3660 // NB: This uses '/' to distinguish between kernel modules and userspace,
3661 // which means that userspace modules won't get any PATH searching.
3662 dwflpp* dw;
707bf35e
JS
3663 try
3664 {
b5a0dd41
FCE
3665 userspace_p=is_user_module (module);
3666 if (! userspace_p)
707bf35e
JS
3667 {
3668 // kernel or kernel module target
ae2552da 3669 dw = db.get_kern_dw(s, module);
707bf35e
JS
3670 }
3671 else
3672 {
3673 module = find_executable (module); // canonicalize it
3674 dw = db.get_user_dw(s, module);
3675 }
3676 }
3677 catch (const semantic_error& er)
3678 {
3679 /* ignore and go to the next module */
3680 continue;
3681 }
c4ce66a1 3682
5f36109e 3683 dwarf_cast_query q (*dw, module, *e, lvalue, userspace_p, result);
51178501 3684 dw->iterate_over_modules(&query_module, &q);
c4ce66a1 3685 }
abb41d92 3686
5f36109e 3687 if (!result)
c4ce66a1 3688 {
946e1a48
JS
3689 // We pass the unresolved cast_op to the next pass, and hope
3690 // that this value ends up not being referenced after all, so
3691 // it can be optimized out quietly.
c4ce66a1
JS
3692 provide (e);
3693 return;
3694 }
3695
c4ce66a1
JS
3696 if (lvalue)
3697 {
3698 // Provide the functioncall to our parent, so that it can be
3699 // used to substitute for the assignment node immediately above
3700 // us.
3701 assert(!target_symbol_setter_functioncalls.empty());
5f36109e 3702 *(target_symbol_setter_functioncalls.top()) = result;
c4ce66a1
JS
3703 }
3704
5f36109e 3705 result->visit (this);
77de5e9e
GH
3706}
3707
3708
b8da0ad1
FCE
3709void
3710dwarf_derived_probe::printsig (ostream& o) const
3711{
3712 // Instead of just printing the plain locations, we add a PC value
3713 // as a comment as a way of telling e.g. apart multiple inlined
3714 // function instances. This is distinct from the verbose/clog
3715 // output, since this part goes into the cache hash calculations.
3716 sole_location()->print (o);
6d0f3f0c 3717 o << " /* pc=" << section << "+0x" << hex << addr << dec << " */";
b8da0ad1
FCE
3718 printsig_nested (o);
3719}
3720
3721
3722
dc38c0ae 3723void
b20febf3
FCE
3724dwarf_derived_probe::join_group (systemtap_session& s)
3725{
af234c40
JS
3726 // skip probes which are paired entry-handlers
3727 if (!has_return && (saved_longs || saved_strings))
3728 return;
3729
b20febf3
FCE
3730 if (! s.dwarf_derived_probes)
3731 s.dwarf_derived_probes = new dwarf_derived_probe_group ();
3732 s.dwarf_derived_probes->enroll (this);
b642c901
SC
3733
3734 enable_task_finder(s);
b20febf3
FCE
3735}
3736
3737
3738dwarf_derived_probe::dwarf_derived_probe(const string& funcname,
3739 const string& filename,
3740 int line,
91af0778 3741 // module & section specify a relocation
b20febf3
FCE
3742 // base for <addr>, unless section==""
3743 // (equivalently module=="kernel")
3744 const string& module,
3745 const string& section,
3746 // NB: dwfl_addr is the virtualized
3747 // address for this symbol.
3748 Dwarf_Addr dwfl_addr,
3749 // addr is the section-offset for
3750 // actual relocation.
3751 Dwarf_Addr addr,
3752 dwarf_query& q,
37ebca01 3753 Dwarf_Die* scope_die /* may be null */)
4c5d1300 3754 : derived_probe (q.base_probe, q.base_loc, true /* .components soon rewritten */ ),
b20febf3 3755 module (module), section (section), addr (addr),
63b4fd14 3756 path (q.path),
27dc09b1 3757 has_process (q.has_process),
c9bad430
DS
3758 has_return (q.has_return),
3759 has_maxactive (q.has_maxactive),
c57ea854 3760 has_library (q.has_library),
6b66b9f7 3761 maxactive_val (q.maxactive_val),
b642c901
SC
3762 user_path (q.user_path),
3763 user_lib (q.user_lib),
af234c40 3764 access_vars(false),
c57ea854 3765 saved_longs(0), saved_strings(0),
af234c40 3766 entry_handler(0)
bd2b1e68 3767{
b642c901
SC
3768 if (user_lib.size() != 0)
3769 has_library = true;
3770
6b66b9f7
JS
3771 if (q.has_process)
3772 {
3773 // We may receive probes on two types of ELF objects: ET_EXEC or ET_DYN.
3774 // ET_EXEC ones need no further relocation on the addr(==dwfl_addr), whereas
3775 // ET_DYN ones do (addr += run-time mmap base address). We tell these apart
3776 // by the incoming section value (".absolute" vs. ".dynamic").
3777 // XXX Assert invariants here too?
3778 }
3779 else
3780 {
3781 // Assert kernel relocation invariants
3782 if (section == "" && dwfl_addr != addr) // addr should be absolute
3783 throw semantic_error ("missing relocation base against", tok);
3784 if (section != "" && dwfl_addr == addr) // addr should be an offset
3785 throw semantic_error ("inconsistent relocation address", tok);
3786 }
2930abc7 3787
21beacc9
FCE
3788 // XXX: hack for strange g++/gcc's
3789#ifndef USHRT_MAX
3790#define USHRT_MAX 32767
3791#endif
3792
606fd9c8 3793 // Range limit maxactive() value
6b66b9f7 3794 if (has_maxactive && (maxactive_val < 0 || maxactive_val > USHRT_MAX))
606fd9c8 3795 throw semantic_error ("maxactive value out of range [0,"
aca66a36 3796 + lex_cast(USHRT_MAX) + "]",
f1a0157a 3797 q.base_loc->components.front()->tok);
606fd9c8 3798
de688825 3799 // Expand target variables in the probe body
5f0a03a6 3800 if (!null_die(scope_die))
8fc05e57 3801 {
6b66b9f7 3802 // XXX: user-space deref's for q.has_process!
de688825 3803 dwarf_var_expanding_visitor v (q, scope_die, dwfl_addr);
8b095b45 3804 v.replace (this->body);
6b66b9f7
JS
3805 if (!q.has_process)
3806 access_vars = v.visited;
37ebca01
FCE
3807
3808 // If during target-variable-expanding the probe, we added a new block
3809 // of code, add it to the start of the probe.
3810 if (v.add_block)
ba6f838d 3811 this->body = new block(v.add_block, this->body);
2260f4e3
FCE
3812
3813 // If when target-variable-expanding the probe, we need to synthesize a
3814 // sibling function-entry probe. We don't go through the whole probe derivation
3815 // business (PR10642) that could lead to wildcard/alias resolution, or for that
3816 // dwarf-induced duplication.
3817 if (v.add_call_probe)
37ebca01 3818 {
2260f4e3
FCE
3819 assert (q.has_return && !q.has_call);
3820
3821 // We temporarily replace q.base_probe.
3822 statement* old_body = q.base_probe->body;
3823 q.base_probe->body = v.add_call_probe;
3824 q.has_return = false;
3825 q.has_call = true;
af234c40 3826
da23eceb 3827 if (q.has_process)
af234c40
JS
3828 entry_handler = new uprobe_derived_probe (funcname, filename, line,
3829 module, section, dwfl_addr,
3830 addr, q, scope_die);
da23eceb 3831 else
af234c40
JS
3832 entry_handler = new dwarf_derived_probe (funcname, filename, line,
3833 module, section, dwfl_addr,
3834 addr, q, scope_die);
3835
3836 saved_longs = entry_handler->saved_longs = v.saved_longs;
3837 saved_strings = entry_handler->saved_strings = v.saved_strings;
3838
3839 q.results.push_back (entry_handler);
2260f4e3
FCE
3840
3841 q.has_return = true;
3842 q.has_call = false;
3843 q.base_probe->body = old_body;
37ebca01 3844 }
f10534c6
WH
3845 // Save the local variables for listing mode
3846 if (q.sess.listing_mode_vars)
8c67c337 3847 saveargs(q, scope_die, dwfl_addr);
8fc05e57 3848 }
37ebca01 3849 // else - null scope_die - $target variables will produce an error during translate phase
8fc05e57 3850
f10534c6 3851 // PR10820: null scope die, local variables aren't accessible, not necessary to invoke saveargs
0a98fd42 3852
5d23847d 3853 // Reset the sole element of the "locations" vector as a
b20febf3
FCE
3854 // "reverse-engineered" form of the incoming (q.base_loc) probe
3855 // point. This allows a user to see what function / file / line
3856 // number any particular match of the wildcards.
2930abc7 3857
a229fcd7 3858 vector<probe_point::component*> comps;
91af0778
FCE
3859 if (q.has_kernel)
3860 comps.push_back (new probe_point::component(TOK_KERNEL));
3861 else if(q.has_module)
3862 comps.push_back (new probe_point::component(TOK_MODULE, new literal_string(module)));
3863 else if(q.has_process)
3864 comps.push_back (new probe_point::component(TOK_PROCESS, new literal_string(module)));
3865 else
3866 assert (0);
b5d77020 3867
db520b00
FCE
3868 string fn_or_stmt;
3869 if (q.has_function_str || q.has_function_num)
3870 fn_or_stmt = "function";
3871 else
3872 fn_or_stmt = "statement";
a229fcd7 3873
b8da0ad1 3874 if (q.has_function_str || q.has_statement_str)
db520b00 3875 {
4cd232e4 3876 string retro_name = funcname;
b20febf3 3877 if (filename != "")
cee35f73 3878 {
fb84c077 3879 retro_name += ("@" + string (filename));
cee35f73 3880 if (line > 0)
aca66a36 3881 retro_name += (":" + lex_cast (line));
cee35f73 3882 }
db520b00
FCE
3883 comps.push_back
3884 (new probe_point::component
3885 (fn_or_stmt, new literal_string (retro_name)));
3886 }
b8da0ad1 3887 else if (q.has_function_num || q.has_statement_num)
db520b00
FCE
3888 {
3889 Dwarf_Addr retro_addr;
3890 if (q.has_function_num)
3891 retro_addr = q.function_num_val;
3892 else
3893 retro_addr = q.statement_num_val;
db520b00
FCE
3894 comps.push_back (new probe_point::component
3895 (fn_or_stmt,
9ea68eb9 3896 new literal_number(retro_addr, true)));
37ebca01
FCE
3897
3898 if (q.has_absolute)
3899 comps.push_back (new probe_point::component (TOK_ABSOLUTE));
a229fcd7
GH
3900 }
3901
b8da0ad1
FCE
3902 if (q.has_call)
3903 comps.push_back (new probe_point::component(TOK_CALL));
3904 if (q.has_inline)
3905 comps.push_back (new probe_point::component(TOK_INLINE));
db520b00 3906 if (has_return)
b8da0ad1
FCE
3907 comps.push_back (new probe_point::component(TOK_RETURN));
3908 if (has_maxactive)
3909 comps.push_back (new probe_point::component
3910 (TOK_MAXACTIVE, new literal_number(maxactive_val)));
d9b516ca 3911
5d23847d
FCE
3912 // Overwrite it.
3913 this->sole_location()->components = comps;
2930abc7
FCE
3914}
3915
bd2b1e68 3916
0a98fd42 3917void
8c67c337
JS
3918dwarf_derived_probe::saveargs(dwarf_query& q, Dwarf_Die* scope_die,
3919 Dwarf_Addr dwfl_addr)
0a98fd42 3920{
9aa8ffce 3921 if (null_die(scope_die))
0a98fd42 3922 return;
0a98fd42 3923
8c67c337 3924 bool verbose = q.sess.verbose > 2;
0a98fd42 3925
8c67c337
JS
3926 if (verbose)
3927 clog << "saveargs: examining '" << (dwarf_diename(scope_die) ?: "<unknown>")
3928 << "' (dieoffset: " << lex_cast_hex(dwarf_dieoffset(scope_die)) << ")\n";
0a98fd42 3929
8c67c337
JS
3930 if (has_return)
3931 {
3932 /* Only save the return value if it has a type. */
3933 string type_name;
3934 Dwarf_Die type_die;
3935 if (dwarf_attr_die (scope_die, DW_AT_type, &type_die) &&
3936 dwarf_type_name(&type_die, type_name))
3937 args.push_back("$return:"+type_name);
3938
3939 else if (verbose)
3940 clog << "saveargs: failed to retrieve type name for return value"
3941 << " (dieoffset: " << lex_cast_hex(dwarf_dieoffset(scope_die))
3942 << ")\n";
3943 }
d87623a1 3944
0a98fd42 3945 Dwarf_Die arg;
4ef35696
JS
3946 vector<Dwarf_Die> scopes = q.dw.getscopes(scope_die);
3947 for (unsigned i = 0; i < scopes.size(); ++i)
3948 {
3949 if (dwarf_tag(&scopes[i]) == DW_TAG_compile_unit)
3950 break; // we don't want file-level variables
3951 if (dwarf_child (&scopes[i], &arg) == 0)
3952 do
0a98fd42 3953 {
4ef35696
JS
3954 switch (dwarf_tag (&arg))
3955 {
3956 case DW_TAG_variable:
3957 case DW_TAG_formal_parameter:
3958 break;
0a98fd42 3959
4ef35696
JS
3960 default:
3961 continue;
3962 }
0a98fd42 3963
4ef35696
JS
3964 /* Ignore this local if it has no name. */
3965 const char *arg_name = dwarf_diename (&arg);
3966 if (!arg_name)
8c67c337
JS
3967 {
3968 if (verbose)
4ef35696 3969 clog << "saveargs: failed to retrieve name for local (dieoffset: "
8c67c337
JS
3970 << lex_cast_hex(dwarf_dieoffset(&arg)) << ")\n";
3971 continue;
3972 }
4ef35696
JS
3973
3974 if (verbose)
3975 clog << "saveargs: finding location for local '"
3976 << arg_name << "' (dieoffset: "
3977 << lex_cast_hex(dwarf_dieoffset(&arg)) << ")\n";
3978
3979 /* Ignore this local if it has no location (or not at this PC). */
3980 /* NB: It still may not be directly accessible, e.g. if it is an
3981 * aggregate type, implicit_pointer, etc., but the user can later
3982 * figure out how to access the interesting parts. */
3983 Dwarf_Attribute attr_mem;
3984 if (!dwarf_attr_integrate (&arg, DW_AT_const_value, &attr_mem))
3985 {
3986 Dwarf_Op *expr;
3987 size_t len;
3988 if (!dwarf_attr_integrate (&arg, DW_AT_location, &attr_mem))
3989 {
3990 if (verbose)
3991 clog << "saveargs: failed to resolve the location for local '"
3992 << arg_name << "' (dieoffset: "
3993 << lex_cast_hex(dwarf_dieoffset(&arg)) << ")\n";
3994 continue;
3995 }
3996 else if (!(dwarf_getlocation_addr(&attr_mem, dwfl_addr, &expr,
3997 &len, 1) == 1 && len > 0))
3998 {
3999 if (verbose)
4000 clog << "saveargs: local '" << arg_name << "' (dieoffset: "
4001 << lex_cast_hex(dwarf_dieoffset(&arg))
4002 << ") is not available at this address ("
4003 << lex_cast_hex(dwfl_addr) << ")\n";
4004 continue;
4005 }
4006 }
4007
4008 /* Ignore this local if it has no type. */
4009 string type_name;
4010 Dwarf_Die type_die;
4011 if (!dwarf_attr_die (&arg, DW_AT_type, &type_die) ||
4012 !dwarf_type_name(&type_die, type_name))
8c67c337
JS
4013 {
4014 if (verbose)
4ef35696
JS
4015 clog << "saveargs: failed to retrieve type name for local '"
4016 << arg_name << "' (dieoffset: "
4017 << lex_cast_hex(dwarf_dieoffset(&arg)) << ")\n";
8c67c337
JS
4018 continue;
4019 }
8c67c337 4020
4ef35696
JS
4021 /* This local looks good -- save it! */
4022 args.push_back("$"+string(arg_name)+":"+type_name);
8c67c337 4023 }
4ef35696
JS
4024 while (dwarf_siblingof (&arg, &arg) == 0);
4025 }
0a98fd42
JS
4026}
4027
4028
4029void
d0bfd2ac 4030dwarf_derived_probe::getargs(std::list<std::string> &arg_set) const
0a98fd42 4031{
d0bfd2ac 4032 arg_set.insert(arg_set.end(), args.begin(), args.end());
0a98fd42
JS
4033}
4034
4035
27dc09b1
DB
4036void
4037dwarf_derived_probe::emit_unprivileged_assertion (translator_output* o)
4038{
4039 if (has_process)
4040 {
4041 // These probes are allowed for unprivileged users, but only in the
4042 // context of processes which they own.
4043 emit_process_owner_assertion (o);
4044 return;
4045 }
4046
4047 // Other probes must contain the default assertion which aborts
4048 // if executed by an unprivileged user.
4049 derived_probe::emit_unprivileged_assertion (o);
4050}
4051
4052
4053void
4054dwarf_derived_probe::print_dupe_stamp(ostream& o)
4055{
4056 if (has_process)
4057 {
4058 // These probes are allowed for unprivileged users, but only in the
4059 // context of processes which they own.
4060 print_dupe_stamp_unprivileged_process_owner (o);
4061 return;
4062 }
4063
4064 // Other probes must contain the default dupe stamp
4065 derived_probe::print_dupe_stamp (o);
4066}
4067
64211010 4068
7a053d3b 4069void
20c6c071 4070dwarf_derived_probe::register_statement_variants(match_node * root,
27dc09b1
DB
4071 dwarf_builder * dw,
4072 bool bind_unprivileged_p)
bd2b1e68 4073{
27dc09b1
DB
4074 root
4075 ->bind_unprivileged(bind_unprivileged_p)
4076 ->bind(dw);
54efe513
GH
4077}
4078
7a053d3b 4079void
fd6602a0 4080dwarf_derived_probe::register_function_variants(match_node * root,
27dc09b1
DB
4081 dwarf_builder * dw,
4082 bool bind_unprivileged_p)
2865d17a 4083{
27dc09b1
DB
4084 root
4085 ->bind_unprivileged(bind_unprivileged_p)
4086 ->bind(dw);
4087 root->bind(TOK_INLINE)
4088 ->bind_unprivileged(bind_unprivileged_p)
4089 ->bind(dw);
4090 root->bind(TOK_CALL)
4091 ->bind_unprivileged(bind_unprivileged_p)
4092 ->bind(dw);
4093 root->bind(TOK_RETURN)
4094 ->bind_unprivileged(bind_unprivileged_p)
4095 ->bind(dw);
4096 root->bind(TOK_RETURN)
4097 ->bind_unprivileged(bind_unprivileged_p)
4098 ->bind_num(TOK_MAXACTIVE)->bind(dw);
bd2b1e68
GH
4099}
4100
7a053d3b 4101void
27dc09b1
DB
4102dwarf_derived_probe::register_function_and_statement_variants(
4103 match_node * root,
4104 dwarf_builder * dw,
4105 bool bind_unprivileged_p
4106)
bd2b1e68
GH
4107{
4108 // Here we match 4 forms:
4109 //
4110 // .function("foo")
4111 // .function(0xdeadbeef)
4112 // .statement("foo")
4113 // .statement(0xdeadbeef)
4114
27dc09b1
DB
4115 register_function_variants(root->bind_str(TOK_FUNCTION), dw, bind_unprivileged_p);
4116 register_function_variants(root->bind_num(TOK_FUNCTION), dw, bind_unprivileged_p);
4117 register_statement_variants(root->bind_str(TOK_STATEMENT), dw, bind_unprivileged_p);
4118 register_statement_variants(root->bind_num(TOK_STATEMENT), dw, bind_unprivileged_p);
bd2b1e68
GH
4119}
4120
4121void
c4ce66a1 4122dwarf_derived_probe::register_patterns(systemtap_session& s)
bd2b1e68 4123{
c4ce66a1 4124 match_node* root = s.pattern_root;
bd2b1e68
GH
4125 dwarf_builder *dw = new dwarf_builder();
4126
c4ce66a1
JS
4127 update_visitor *filter = new dwarf_cast_expanding_visitor(s, *dw);
4128 s.code_filters.push_back(filter);
4129
27dc09b1
DB
4130 register_function_and_statement_variants(root->bind(TOK_KERNEL), dw);
4131 register_function_and_statement_variants(root->bind_str(TOK_MODULE), dw);
4132
4133 root->bind(TOK_KERNEL)->bind_num(TOK_STATEMENT)->bind(TOK_ABSOLUTE)
4134 ->bind(dw);
4135 root->bind(TOK_KERNEL)->bind_str(TOK_FUNCTION)->bind_str(TOK_LABEL)
4136 ->bind(dw);
d2c9ec9b 4137
27dc09b1
DB
4138 register_function_and_statement_variants(root->bind_str(TOK_PROCESS), dw,
4139 true/*bind_unprivileged*/);
d2c9ec9b 4140 root->bind_str(TOK_PROCESS)->bind_str(TOK_FUNCTION)->bind_str(TOK_LABEL)
27dc09b1 4141 ->bind_unprivileged()
d2c9ec9b 4142 ->bind(dw);
63b4fd14 4143 root->bind_str(TOK_PROCESS)->bind_str(TOK_LIBRARY)->bind_str(TOK_MARK)
27dc09b1 4144 ->bind_unprivileged()
63b4fd14 4145 ->bind(dw);
a794dbeb
FCE
4146 root->bind_str(TOK_PROCESS)->bind_str(TOK_LIBRARY)->bind_str(TOK_PROVIDER)->bind_str(TOK_MARK)
4147 ->bind_unprivileged()
4148 ->bind(dw);
d2c9ec9b 4149 root->bind_str(TOK_PROCESS)->bind_str(TOK_MARK)
27dc09b1 4150 ->bind_unprivileged()
d2c9ec9b 4151 ->bind(dw);
a794dbeb
FCE
4152 root->bind_str(TOK_PROCESS)->bind_str(TOK_PROVIDER)->bind_str(TOK_MARK)
4153 ->bind_unprivileged()
4154 ->bind(dw);
bd2b1e68
GH
4155}
4156
9020300d
FCE
4157void
4158dwarf_derived_probe::emit_probe_local_init(translator_output * o)
4159{
b95e2b79
MH
4160 if (access_vars)
4161 {
4162 // if accessing $variables, emit bsp cache setup for speeding up
d4670309 4163 o->newline() << "#if defined __ia64__";
b95e2b79 4164 o->newline() << "bspcache(c->unwaddr, c->regs);";
d4670309 4165 o->newline() << "#endif";
b95e2b79 4166 }
9020300d 4167}
2930abc7 4168
b20febf3 4169// ------------------------------------------------------------------------
46b84a80
DS
4170
4171void
b20febf3 4172dwarf_derived_probe_group::enroll (dwarf_derived_probe* p)
46b84a80 4173{
b20febf3 4174 probes_by_module.insert (make_pair (p->module, p));
b8da0ad1
FCE
4175
4176 // XXX: probes put at the same address should all share a
4177 // single kprobe/kretprobe, and have their handlers executed
4178 // sequentially.
b55bc428
FCE
4179}
4180
7a053d3b 4181void
775d51e5 4182dwarf_derived_probe_group::emit_module_decls (systemtap_session& s)
ec4373ff 4183{
b20febf3 4184 if (probes_by_module.empty()) return;
2930abc7 4185
775d51e5
DS
4186 s.op->newline() << "/* ---- dwarf probes ---- */";
4187
4188 // Warn of misconfigured kernels
f41595cc
FCE
4189 s.op->newline() << "#if ! defined(CONFIG_KPROBES)";
4190 s.op->newline() << "#error \"Need CONFIG_KPROBES!\"";
4191 s.op->newline() << "#endif";
775d51e5 4192 s.op->newline();
f41595cc 4193
f07c3b68 4194 s.op->newline() << "#ifndef KRETACTIVE";
1ee6b5fc 4195 s.op->newline() << "#define KRETACTIVE (max(15,6*(int)num_possible_cpus()))";
f07c3b68
FCE
4196 s.op->newline() << "#endif";
4197
14cf7e42
SC
4198 // Forward decls
4199 s.op->newline() << "#include \"kprobes-common.h\"";
4200
b20febf3
FCE
4201 // Forward declare the master entry functions
4202 s.op->newline() << "static int enter_kprobe_probe (struct kprobe *inst,";
4203 s.op->line() << " struct pt_regs *regs);";
4204 s.op->newline() << "static int enter_kretprobe_probe (struct kretprobe_instance *inst,";
4205 s.op->line() << " struct pt_regs *regs);";
4206
42cb22bd
MH
4207 // Emit an array of kprobe/kretprobe pointers
4208 s.op->newline() << "#if defined(STAPCONF_UNREGISTER_KPROBES)";
4209 s.op->newline() << "static void * stap_unreg_kprobes[" << probes_by_module.size() << "];";
4210 s.op->newline() << "#endif";
4211
b20febf3 4212 // Emit the actual probe list.
606fd9c8
FCE
4213
4214 // NB: we used to plop a union { struct kprobe; struct kretprobe } into
4215 // struct stap_dwarf_probe, but it being initialized data makes it add
4216 // hundreds of bytes of padding per stap_dwarf_probe. (PR5673)
14cf7e42 4217 s.op->newline() << "static struct stap_dwarf_kprobe stap_dwarf_kprobes[" << probes_by_module.size() << "];";
606fd9c8
FCE
4218 // NB: bss!
4219
4c2732a1 4220 s.op->newline() << "static struct stap_dwarf_probe {";
b0986e7a
DS
4221 s.op->newline(1) << "const unsigned return_p:1;";
4222 s.op->newline() << "const unsigned maxactive_p:1;";
b350f56b 4223 s.op->newline() << "const unsigned optional_p:1;";
b20febf3 4224 s.op->newline() << "unsigned registered_p:1;";
b0986e7a 4225 s.op->newline() << "const unsigned short maxactive_val;";
606fd9c8 4226
af234c40
JS
4227 // data saved in the kretprobe_instance packet
4228 s.op->newline() << "const unsigned short saved_longs;";
4229 s.op->newline() << "const unsigned short saved_strings;";
4230
faea5e16 4231 // Let's find some stats for the embedded strings. Maybe they
606fd9c8
FCE
4232 // are small and uniform enough to justify putting char[MAX]'s into
4233 // the array instead of relocated char*'s.
faea5e16
JS
4234 size_t module_name_max = 0, section_name_max = 0;
4235 size_t module_name_tot = 0, section_name_tot = 0;
606fd9c8
FCE
4236 size_t all_name_cnt = probes_by_module.size(); // for average
4237 for (p_b_m_iterator it = probes_by_module.begin(); it != probes_by_module.end(); it++)
4238 {
4239 dwarf_derived_probe* p = it->second;
4240#define DOIT(var,expr) do { \
4241 size_t var##_size = (expr) + 1; \
4242 var##_max = max (var##_max, var##_size); \
4243 var##_tot += var##_size; } while (0)
4244 DOIT(module_name, p->module.size());
4245 DOIT(section_name, p->section.size());
606fd9c8
FCE
4246#undef DOIT
4247 }
4248
4249 // Decide whether it's worthwhile to use char[] or char* by comparing
4250 // the amount of average waste (max - avg) to the relocation data size
4251 // (3 native long words).
4252#define CALCIT(var) \
4253 if ((var##_name_max-(var##_name_tot/all_name_cnt)) < (3 * sizeof(void*))) \
4254 { \
4255 s.op->newline() << "const char " << #var << "[" << var##_name_max << "];"; \
4256 if (s.verbose > 2) clog << "stap_dwarf_probe " << #var \
4257 << "[" << var##_name_max << "]" << endl; \
4258 } \
4259 else \
4260 { \
b0986e7a 4261 s.op->newline() << "const char * const " << #var << ";"; \
606fd9c8
FCE
4262 if (s.verbose > 2) clog << "stap_dwarf_probe *" << #var << endl; \
4263 }
4264
4265 CALCIT(module);
4266 CALCIT(section);
e6fe60e7 4267#undef CALCIT
606fd9c8 4268
b0986e7a 4269 s.op->newline() << "const unsigned long address;";
26e63673 4270 s.op->newline() << "struct stap_probe * const probe;";
c87ae2c1 4271 s.op->newline() << "struct stap_probe * const entry_probe;";
b642c901
SC
4272 s.op->newline() << "const unsigned long sdt_sem_offset;";
4273 s.op->newline() << "unsigned long sdt_sem_address;";
4274 s.op->newline() << "struct task_struct *tsk;";
4275 s.op->newline() << "const char *pathname;";
4276 s.op->newline() << "struct stap_task_finder_target finder;";
b20febf3
FCE
4277 s.op->newline(-1) << "} stap_dwarf_probes[] = {";
4278 s.op->indent(1);
4279
4280 for (p_b_m_iterator it = probes_by_module.begin(); it != probes_by_module.end(); it++)
2930abc7 4281 {
b20febf3
FCE
4282 dwarf_derived_probe* p = it->second;
4283 s.op->newline() << "{";
4284 if (p->has_return)
4285 s.op->line() << " .return_p=1,";
c9bad430 4286 if (p->has_maxactive)
606fd9c8
FCE
4287 {
4288 s.op->line() << " .maxactive_p=1,";
4289 assert (p->maxactive_val >= 0 && p->maxactive_val <= USHRT_MAX);
4290 s.op->line() << " .maxactive_val=" << p->maxactive_val << ",";
4291 }
af234c40
JS
4292 if (p->saved_longs || p->saved_strings)
4293 {
4294 if (p->saved_longs)
4295 s.op->line() << " .saved_longs=" << p->saved_longs << ",";
4296 if (p->saved_strings)
4297 s.op->line() << " .saved_strings=" << p->saved_strings << ",";
4298 if (p->entry_handler)
c87ae2c1 4299 s.op->line() << " .entry_probe=" << common_probe_init (p->entry_handler) << ",";
af234c40 4300 }
b350f56b
JS
4301 if (p->locations[0]->optional)
4302 s.op->line() << " .optional_p=1,";
dc38c256 4303 s.op->line() << " .address=(unsigned long)0x" << hex << p->addr << dec << "ULL,";
84048984
FCE
4304 s.op->line() << " .module=\"" << p->module << "\",";
4305 s.op->line() << " .section=\"" << p->section << "\",";
faea5e16 4306 s.op->line() << " .probe=" << common_probe_init (p) << ",";
b642c901
SC
4307 if (p->sdt_semaphore_addr != 0)
4308 {
4309 s.op->line() << " .sdt_sem_offset=(unsigned long)0x" << hex << p->sdt_semaphore_addr << dec << "ULL,";
4310 s.op->line() << " .sdt_sem_address=0,";
4311 if (p->has_library)
4312 {
4313 s.op->line() << " .finder={";
4314 s.op->line() << " .pid=0,";
4315 s.op->line() << " .procname=\"" << p->user_path << "\",";
4316 s.op->line() << " .mmap_callback=&stap_kprobe_mmap_found,";
4317 s.op->line() << " },";
4318 s.op->line() << " .pathname=\"" << p->user_lib << "\",";
4319 }
4320 else
4321 {
4322 s.op->line() << " .finder={";
4323 s.op->line() << " .pid=0,";
4324 s.op->line() << " .procname=\"" << p->user_path << "\",";
4325 s.op->line() << " .callback=&stap_kprobe_process_found,";
4326 s.op->line() << " },";
4327 s.op->line() << " .pathname=\"" << p->user_path << "\",";
4328 }
4329
4330 }
4331 else
4332 s.op->line() << " .sdt_sem_offset=0,";
b642c901 4333
b20febf3 4334 s.op->line() << " },";
2930abc7 4335 }
2930abc7 4336
b20febf3
FCE
4337 s.op->newline(-1) << "};";
4338
4339 // Emit the kprobes callback function
4340 s.op->newline();
4341 s.op->newline() << "static int enter_kprobe_probe (struct kprobe *inst,";
4342 s.op->line() << " struct pt_regs *regs) {";
606fd9c8
FCE
4343 // NB: as of PR5673, the kprobe|kretprobe union struct is in BSS
4344 s.op->newline(1) << "int kprobe_idx = ((uintptr_t)inst-(uintptr_t)stap_dwarf_kprobes)/sizeof(struct stap_dwarf_kprobe);";
4345 // Check that the index is plausible
4346 s.op->newline() << "struct stap_dwarf_probe *sdp = &stap_dwarf_probes[";
4347 s.op->line() << "((kprobe_idx >= 0 && kprobe_idx < " << probes_by_module.size() << ")?";
4348 s.op->line() << "kprobe_idx:0)"; // NB: at least we avoid memory corruption
4349 // XXX: it would be nice to give a more verbose error though; BUG_ON later?
4350 s.op->line() << "];";
faea5e16 4351 common_probe_entryfn_prologue (s.op, "STAP_SESSION_RUNNING", "sdp->probe");
b20febf3 4352 s.op->newline() << "c->regs = regs;";
6415ddde
MW
4353
4354 // Make it look like the IP is set as it wouldn't have been replaced
4355 // by a breakpoint instruction when calling real probe handler. Reset
4356 // IP regs on return, so we don't confuse kprobes. PR10458
4357 s.op->newline() << "{";
4358 s.op->indent(1);
4359 s.op->newline() << "unsigned long kprobes_ip = REG_IP(c->regs);";
259d54c0 4360 s.op->newline() << "SET_REG_IP(regs, (unsigned long) inst->addr);";
26e63673 4361 s.op->newline() << "(*sdp->probe->ph) (c);";
259d54c0 4362 s.op->newline() << "SET_REG_IP(regs, kprobes_ip);";
6415ddde
MW
4363 s.op->newline(-1) << "}";
4364
b20febf3
FCE
4365 common_probe_entryfn_epilogue (s.op);
4366 s.op->newline() << "return 0;";
4367 s.op->newline(-1) << "}";
4368
4369 // Same for kretprobes
4370 s.op->newline();
af234c40
JS
4371 s.op->newline() << "static int enter_kretprobe_common (struct kretprobe_instance *inst,";
4372 s.op->line() << " struct pt_regs *regs, int entry) {";
b20febf3 4373 s.op->newline(1) << "struct kretprobe *krp = inst->rp;";
606fd9c8
FCE
4374
4375 // NB: as of PR5673, the kprobe|kretprobe union struct is in BSS
a36378d7 4376 s.op->newline() << "int kprobe_idx = ((uintptr_t)krp-(uintptr_t)stap_dwarf_kprobes)/sizeof(struct stap_dwarf_kprobe);";
606fd9c8
FCE
4377 // Check that the index is plausible
4378 s.op->newline() << "struct stap_dwarf_probe *sdp = &stap_dwarf_probes[";
4379 s.op->line() << "((kprobe_idx >= 0 && kprobe_idx < " << probes_by_module.size() << ")?";
4380 s.op->line() << "kprobe_idx:0)"; // NB: at least we avoid memory corruption
4381 // XXX: it would be nice to give a more verbose error though; BUG_ON later?
4382 s.op->line() << "];";
4383
c87ae2c1
JS
4384 s.op->newline() << "struct stap_probe *sp = entry ? sdp->entry_probe : sdp->probe;";
4385 s.op->newline() << "if (sp) {";
4386 s.op->indent(1);
4387 common_probe_entryfn_prologue (s.op, "STAP_SESSION_RUNNING", "sp");
b20febf3 4388 s.op->newline() << "c->regs = regs;";
af234c40
JS
4389
4390 // for assisting runtime's backtrace logic and accessing kretprobe data packets
4391 s.op->newline() << "c->pi = inst;";
4392 s.op->newline() << "c->pi_longs = sdp->saved_longs;";
6415ddde
MW
4393
4394 // Make it look like the IP is set as it wouldn't have been replaced
4395 // by a breakpoint instruction when calling real probe handler. Reset
4396 // IP regs on return, so we don't confuse kprobes. PR10458
4397 s.op->newline() << "{";
c87ae2c1
JS
4398 s.op->newline(1) << "unsigned long kprobes_ip = REG_IP(c->regs);";
4399 s.op->newline() << "if (entry)";
4400 s.op->newline(1) << "SET_REG_IP(regs, (unsigned long) inst->rp->kp.addr);";
4401 s.op->newline(-1) << "else";
4402 s.op->newline(1) << "SET_REG_IP(regs, (unsigned long)inst->ret_addr);";
4403 s.op->newline(-1) << "(sp->ph) (c);";
259d54c0 4404 s.op->newline() << "SET_REG_IP(regs, kprobes_ip);";
6415ddde
MW
4405 s.op->newline(-1) << "}";
4406
b20febf3 4407 common_probe_entryfn_epilogue (s.op);
c87ae2c1 4408 s.op->newline(-1) << "}";
b20febf3
FCE
4409 s.op->newline() << "return 0;";
4410 s.op->newline(-1) << "}";
af234c40
JS
4411
4412 s.op->newline();
4413 s.op->newline() << "static int enter_kretprobe_probe (struct kretprobe_instance *inst,";
4414 s.op->line() << " struct pt_regs *regs) {";
4415 s.op->newline(1) << "return enter_kretprobe_common(inst, regs, 0);";
4416 s.op->newline(-1) << "}";
4417
4418 s.op->newline();
4419 s.op->newline() << "static int enter_kretprobe_entry_probe (struct kretprobe_instance *inst,";
4420 s.op->line() << " struct pt_regs *regs) {";
4421 s.op->newline(1) << "return enter_kretprobe_common(inst, regs, 1);";
4422 s.op->newline(-1) << "}";
b642c901 4423
14cf7e42
SC
4424 s.op->newline();
4425 s.op->newline() << "#include \"kprobes-common.c\"";
4426 s.op->newline();
20c6c071 4427}
ec4373ff 4428
20c6c071 4429
dc38c0ae 4430void
b20febf3
FCE
4431dwarf_derived_probe_group::emit_module_init (systemtap_session& s)
4432{
b642c901
SC
4433 p_b_m_iterator it;
4434 for (it = probes_by_module.begin(); it != probes_by_module.end(); it++)
4435 {
4436 dwarf_derived_probe* p = it->second;
4437 if (p->sdt_semaphore_addr != 0)
4438 break;
4439 }
4440 if (it != probes_by_module.end()) // Ignore if there are no semaphores
4441 {
4442 s.op->newline() << "for (i=0; i<ARRAY_SIZE(stap_dwarf_probes); i++) {";
4443 s.op->newline(1) << "int rc;";
4444 s.op->newline() << "struct stap_dwarf_probe *p = &stap_dwarf_probes[i];";
26e63673 4445 s.op->newline() << "probe_point = p->probe->pp;"; // for error messages
b642c901
SC
4446 s.op->newline() << "if (p->sdt_sem_offset) {";
4447 s.op->newline(1) << "rc = stap_register_task_finder_target(&p->finder);";
4448 s.op->newline(-1) << "}";
4449 s.op->newline() << "if (rc) break;";
4450 s.op->newline(-1) << "}";
4451 }
4452
b20febf3
FCE
4453 s.op->newline() << "for (i=0; i<" << probes_by_module.size() << "; i++) {";
4454 s.op->newline(1) << "struct stap_dwarf_probe *sdp = & stap_dwarf_probes[i];";
a36378d7 4455 s.op->newline() << "struct stap_dwarf_kprobe *kp = & stap_dwarf_kprobes[i];";
a049e342 4456 s.op->newline() << "unsigned long relocated_addr = _stp_kmodule_relocate (sdp->module, sdp->section, sdp->address);";
b20febf3 4457 s.op->newline() << "if (relocated_addr == 0) continue;"; // quietly; assume module is absent
26e63673 4458 s.op->newline() << "probe_point = sdp->probe->pp;"; // for error messages
b20febf3 4459 s.op->newline() << "if (sdp->return_p) {";
606fd9c8 4460 s.op->newline(1) << "kp->u.krp.kp.addr = (void *) relocated_addr;";
c9bad430 4461 s.op->newline() << "if (sdp->maxactive_p) {";
606fd9c8 4462 s.op->newline(1) << "kp->u.krp.maxactive = sdp->maxactive_val;";
c9bad430 4463 s.op->newline(-1) << "} else {";
f07c3b68 4464 s.op->newline(1) << "kp->u.krp.maxactive = KRETACTIVE;";
c9bad430 4465 s.op->newline(-1) << "}";
606fd9c8 4466 s.op->newline() << "kp->u.krp.handler = &enter_kretprobe_probe;";
af234c40 4467 s.op->newline() << "#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,25)";
c87ae2c1 4468 s.op->newline() << "if (sdp->entry_probe) {";
af234c40
JS
4469 s.op->newline(1) << "kp->u.krp.entry_handler = &enter_kretprobe_entry_probe;";
4470 s.op->newline() << "kp->u.krp.data_size = sdp->saved_longs * sizeof(int64_t) + ";
4471 s.op->newline() << " sdp->saved_strings * MAXSTRINGLEN;";
4472 s.op->newline(-1) << "}";
4473 s.op->newline() << "#endif";
e4cb375f
MH
4474 // to ensure safeness of bspcache, always use aggr_kprobe on ia64
4475 s.op->newline() << "#ifdef __ia64__";
4476 s.op->newline() << "kp->dummy.addr = kp->u.krp.kp.addr;";
4477 s.op->newline() << "kp->dummy.pre_handler = NULL;";
4478 s.op->newline() << "rc = register_kprobe (& kp->dummy);";
4479 s.op->newline() << "if (rc == 0) {";
4480 s.op->newline(1) << "rc = register_kretprobe (& kp->u.krp);";
4481 s.op->newline() << "if (rc != 0)";
4482 s.op->newline(1) << "unregister_kprobe (& kp->dummy);";
4483 s.op->newline(-2) << "}";
4484 s.op->newline() << "#else";
606fd9c8 4485 s.op->newline() << "rc = register_kretprobe (& kp->u.krp);";
e4cb375f 4486 s.op->newline() << "#endif";
b20febf3 4487 s.op->newline(-1) << "} else {";
e4cb375f 4488 // to ensure safeness of bspcache, always use aggr_kprobe on ia64
606fd9c8
FCE
4489 s.op->newline(1) << "kp->u.kp.addr = (void *) relocated_addr;";
4490 s.op->newline() << "kp->u.kp.pre_handler = &enter_kprobe_probe;";
e4cb375f
MH
4491 s.op->newline() << "#ifdef __ia64__";
4492 s.op->newline() << "kp->dummy.addr = kp->u.kp.addr;";
4493 s.op->newline() << "kp->dummy.pre_handler = NULL;";
4494 s.op->newline() << "rc = register_kprobe (& kp->dummy);";
4495 s.op->newline() << "if (rc == 0) {";
4496 s.op->newline(1) << "rc = register_kprobe (& kp->u.kp);";
4497 s.op->newline() << "if (rc != 0)";
4498 s.op->newline(1) << "unregister_kprobe (& kp->dummy);";
4499 s.op->newline(-2) << "}";
4500 s.op->newline() << "#else";
606fd9c8 4501 s.op->newline() << "rc = register_kprobe (& kp->u.kp);";
e4cb375f 4502 s.op->newline() << "#endif";
b20febf3 4503 s.op->newline(-1) << "}";
9063462a
FCE
4504 s.op->newline() << "if (rc) {"; // PR6749: tolerate a failed register_*probe.
4505 s.op->newline(1) << "sdp->registered_p = 0;";
b350f56b 4506 s.op->newline() << "if (!sdp->optional_p)";
50b6acf7 4507 s.op->newline(1) << "_stp_warn (\"probe %s (address 0x%lx) registration error (rc %d)\", probe_point, (unsigned long) relocated_addr, rc);";
b350f56b 4508 s.op->newline(-1) << "rc = 0;"; // continue with other probes
9063462a
FCE
4509 // XXX: shall we increment numskipped?
4510 s.op->newline(-1) << "}";
4511
4512#if 0 /* pre PR 6749; XXX consider making an option */
c48cb0cc 4513 s.op->newline(1) << "for (j=i-1; j>=0; j--) {"; // partial rollback
b20febf3 4514 s.op->newline(1) << "struct stap_dwarf_probe *sdp2 = & stap_dwarf_probes[j];";
606fd9c8
FCE
4515 s.op->newline() << "struct stap_dwarf_kprobe *kp2 = & stap_dwarf_kprobes[j];";
4516 s.op->newline() << "if (sdp2->return_p) unregister_kretprobe (&kp2->u.krp);";
4517 s.op->newline() << "else unregister_kprobe (&kp2->u.kp);";
e4cb375f
MH
4518 s.op->newline() << "#ifdef __ia64__";
4519 s.op->newline() << "unregister_kprobe (&kp2->dummy);";
4520 s.op->newline() << "#endif";
c48cb0cc
FCE
4521 // NB: we don't have to clear sdp2->registered_p, since the module_exit code is
4522 // not run for this early-abort case.
4523 s.op->newline(-1) << "}";
4524 s.op->newline() << "break;"; // don't attempt to register any more probes
b20febf3 4525 s.op->newline(-1) << "}";
9063462a
FCE
4526#endif
4527
b20febf3
FCE
4528 s.op->newline() << "else sdp->registered_p = 1;";
4529 s.op->newline(-1) << "}"; // for loop
dc38c0ae
DS
4530}
4531
4532
46b84a80 4533void
b20febf3 4534dwarf_derived_probe_group::emit_module_exit (systemtap_session& s)
46b84a80 4535{
b642c901
SC
4536 s.op->newline() << "for (i=0; i<" << probes_by_module.size() << "; i++) {";
4537 s.op->newline(1) << "struct stap_dwarf_probe *sdp = & stap_dwarf_probes[i];";
4538 s.op->newline() << "unsigned short sdt_semaphore = 0;"; // NB: fixed size
4539 s.op->newline() << "if (sdp->sdt_sem_address && __access_process_vm_noflush (sdp->tsk, sdp->sdt_sem_address, &sdt_semaphore, sizeof (sdt_semaphore), 0)) {";
4540 s.op->newline(1) << "sdt_semaphore --;";
4541 s.op->newline() << "__access_process_vm_noflush (sdp->tsk, sdp->sdt_sem_address, &sdt_semaphore, sizeof (sdt_semaphore), 1);";
4542 s.op->newline(-1) << "}";
4543 s.op->newline(-1) << "}";
4544
42cb22bd
MH
4545 //Unregister kprobes by batch interfaces.
4546 s.op->newline() << "#if defined(STAPCONF_UNREGISTER_KPROBES)";
4547 s.op->newline() << "j = 0;";
4548 s.op->newline() << "for (i=0; i<" << probes_by_module.size() << "; i++) {";
4549 s.op->newline(1) << "struct stap_dwarf_probe *sdp = & stap_dwarf_probes[i];";
4550 s.op->newline() << "struct stap_dwarf_kprobe *kp = & stap_dwarf_kprobes[i];";
4551 s.op->newline() << "if (! sdp->registered_p) continue;";
4552 s.op->newline() << "if (!sdp->return_p)";
4553 s.op->newline(1) << "stap_unreg_kprobes[j++] = &kp->u.kp;";
4554 s.op->newline(-2) << "}";
4555 s.op->newline() << "unregister_kprobes((struct kprobe **)stap_unreg_kprobes, j);";
4556 s.op->newline() << "j = 0;";
4557 s.op->newline() << "for (i=0; i<" << probes_by_module.size() << "; i++) {";
4558 s.op->newline(1) << "struct stap_dwarf_probe *sdp = & stap_dwarf_probes[i];";
4559 s.op->newline() << "struct stap_dwarf_kprobe *kp = & stap_dwarf_kprobes[i];";
4560 s.op->newline() << "if (! sdp->registered_p) continue;";
4561 s.op->newline() << "if (sdp->return_p)";
4562 s.op->newline(1) << "stap_unreg_kprobes[j++] = &kp->u.krp;";
4563 s.op->newline(-2) << "}";
4564 s.op->newline() << "unregister_kretprobes((struct kretprobe **)stap_unreg_kprobes, j);";
e4cb375f
MH
4565 s.op->newline() << "#ifdef __ia64__";
4566 s.op->newline() << "j = 0;";
4567 s.op->newline() << "for (i=0; i<" << probes_by_module.size() << "; i++) {";
4568 s.op->newline(1) << "struct stap_dwarf_probe *sdp = & stap_dwarf_probes[i];";
4569 s.op->newline() << "struct stap_dwarf_kprobe *kp = & stap_dwarf_kprobes[i];";
4570 s.op->newline() << "if (! sdp->registered_p) continue;";
4571 s.op->newline() << "stap_unreg_kprobes[j++] = &kp->dummy;";
4572 s.op->newline(-1) << "}";
4573 s.op->newline() << "unregister_kprobes((struct kprobe **)stap_unreg_kprobes, j);";
4574 s.op->newline() << "#endif";
42cb22bd
MH
4575 s.op->newline() << "#endif";
4576
b20febf3
FCE
4577 s.op->newline() << "for (i=0; i<" << probes_by_module.size() << "; i++) {";
4578 s.op->newline(1) << "struct stap_dwarf_probe *sdp = & stap_dwarf_probes[i];";
a36378d7 4579 s.op->newline() << "struct stap_dwarf_kprobe *kp = & stap_dwarf_kprobes[i];";
b20febf3
FCE
4580 s.op->newline() << "if (! sdp->registered_p) continue;";
4581 s.op->newline() << "if (sdp->return_p) {";
42cb22bd 4582 s.op->newline() << "#if !defined(STAPCONF_UNREGISTER_KPROBES)";
606fd9c8 4583 s.op->newline(1) << "unregister_kretprobe (&kp->u.krp);";
42cb22bd 4584 s.op->newline() << "#endif";
606fd9c8 4585 s.op->newline() << "atomic_add (kp->u.krp.nmissed, & skipped_count);";
73209876
FCE
4586 s.op->newline() << "#ifdef STP_TIMING";
4587 s.op->newline() << "if (kp->u.krp.nmissed)";
26e63673 4588 s.op->newline(1) << "_stp_warn (\"Skipped due to missed kretprobe/1 on '%s': %d\\n\", sdp->probe->pp, kp->u.krp.nmissed);";
73209876 4589 s.op->newline(-1) << "#endif";
606fd9c8 4590 s.op->newline() << "atomic_add (kp->u.krp.kp.nmissed, & skipped_count);";
73209876
FCE
4591 s.op->newline() << "#ifdef STP_TIMING";
4592 s.op->newline() << "if (kp->u.krp.kp.nmissed)";
26e63673 4593 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 4594 s.op->newline(-1) << "#endif";
557fb7a8 4595 s.op->newline(-1) << "} else {";
42cb22bd 4596 s.op->newline() << "#if !defined(STAPCONF_UNREGISTER_KPROBES)";
606fd9c8 4597 s.op->newline(1) << "unregister_kprobe (&kp->u.kp);";
42cb22bd 4598 s.op->newline() << "#endif";
606fd9c8 4599 s.op->newline() << "atomic_add (kp->u.kp.nmissed, & skipped_count);";
73209876
FCE
4600 s.op->newline() << "#ifdef STP_TIMING";
4601 s.op->newline() << "if (kp->u.kp.nmissed)";
26e63673 4602 s.op->newline(1) << "_stp_warn (\"Skipped due to missed kprobe on '%s': %lu\\n\", sdp->probe->pp, kp->u.kp.nmissed);";
73209876 4603 s.op->newline(-1) << "#endif";
b20febf3 4604 s.op->newline(-1) << "}";
e4cb375f
MH
4605 s.op->newline() << "#if !defined(STAPCONF_UNREGISTER_KPROBES) && defined(__ia64__)";
4606 s.op->newline() << "unregister_kprobe (&kp->dummy);";
4607 s.op->newline() << "#endif";
b20febf3
FCE
4608 s.op->newline() << "sdp->registered_p = 0;";
4609 s.op->newline(-1) << "}";
46b84a80
DS
4610}
4611
8aabf152
FCE
4612
4613struct sdt_kprobe_var_expanding_visitor: public var_expanding_visitor
4614{
4615 sdt_kprobe_var_expanding_visitor(const string & process_name,
4616 const string & provider_name,
4617 const string & probe_name,
4618 const string & arg_string,
4619 int arg_count):
4620 process_name (process_name), provider_name (provider_name), probe_name (probe_name),
4621 arg_count (arg_count)
4622 {
4623 tokenize(arg_string, arg_tokens, " ");
4624 assert(arg_count >= 0 && arg_count <= 10);
4625 }
4626 const string & process_name;
4627 const string & provider_name;
4628 const string & probe_name;
4629 int arg_count;
4630 vector<string> arg_tokens;
4631
4632 void visit_target_symbol (target_symbol* e);
4633};
4634
4635
aff5d390 4636struct sdt_uprobe_var_expanding_visitor: public var_expanding_visitor
7a05f484 4637{
f83336a5
FCE
4638 sdt_uprobe_var_expanding_visitor(systemtap_session& s,
4639 int elf_machine,
4640 const string & process_name,
a794dbeb 4641 const string & provider_name,
aff5d390
SC
4642 const string & probe_name,
4643 const string & arg_string,
8aabf152 4644 int ac):
332ba7e7
SC
4645 session (s), elf_machine (elf_machine), process_name (process_name),
4646 provider_name (provider_name), probe_name (probe_name), arg_count ((unsigned) ac)
a8ec7719 4647 {
f83336a5
FCE
4648 /* Register name mapping table depends on the elf machine of this particular
4649 probe target process/file, not upon the host. So we can't just
4650 #ifdef _i686_ etc. */
4651 if (elf_machine == EM_X86_64) {
8aabf152
FCE
4652 dwarf_regs["%rax"] = dwarf_regs["%eax"] = dwarf_regs["%ax"] = dwarf_regs["%al"] = 0;
4653 dwarf_regs["%rdx"] = dwarf_regs["%edx"] = dwarf_regs["%dx"] = dwarf_regs["%dl"] = 1;
4654 dwarf_regs["%rcx"] = dwarf_regs["%ecx"] = dwarf_regs["%cx"] = dwarf_regs["%cl"] = 2;
4655 dwarf_regs["%rbx"] = dwarf_regs["%ebx"] = dwarf_regs["%bx"] = dwarf_regs["%bl"] = 3;
4656 dwarf_regs["%rsi"] = dwarf_regs["%esi"] = dwarf_regs["%si"] = dwarf_regs["%sil"] = 4;
4657 dwarf_regs["%rdi"] = dwarf_regs["%edi"] = dwarf_regs["%di"] = dwarf_regs["%dil"] = 5;
4658 dwarf_regs["%rbp"] = dwarf_regs["%ebp"] = dwarf_regs["%bp"] = 6;
4659 dwarf_regs["%rsp"] = dwarf_regs["%esp"] = dwarf_regs["%sp"] = 7;
4660 dwarf_regs["%r8"] = dwarf_regs["%r8d"] = dwarf_regs["%r8w"] = dwarf_regs["%r8b"] = 8;
4661 dwarf_regs["%r9"] = dwarf_regs["%r9d"] = dwarf_regs["%r9w"] = dwarf_regs["%r9b"] = 9;
4662 dwarf_regs["%r10"] = dwarf_regs["%r10d"] = dwarf_regs["%r10w"] = dwarf_regs["%r10b"] = 10;
4663 dwarf_regs["%r11"] = dwarf_regs["%r11d"] = dwarf_regs["%r11w"] = dwarf_regs["%r11b"] = 11;
4664 dwarf_regs["%r12"] = dwarf_regs["%r12d"] = dwarf_regs["%r12w"] = dwarf_regs["%r12b"] = 12;
4665 dwarf_regs["%r13"] = dwarf_regs["%r13d"] = dwarf_regs["%r13w"] = dwarf_regs["%r13b"] = 13;
4666 dwarf_regs["%r14"] = dwarf_regs["%r14d"] = dwarf_regs["%r14w"] = dwarf_regs["%r14b"] = 14;
4667 dwarf_regs["%r15"] = dwarf_regs["%r15d"] = dwarf_regs["%r15w"] = dwarf_regs["%r15b"] = 15;
f83336a5 4668 } else if (elf_machine == EM_386) {
8aabf152
FCE
4669 dwarf_regs["%eax"] = dwarf_regs["%ax"] = dwarf_regs["%al"] = 0;
4670 dwarf_regs["%ecx"] = dwarf_regs["%cx"] = dwarf_regs["%cl"] = 1;
4671 dwarf_regs["%edx"] = dwarf_regs["%dx"] = dwarf_regs["%dl"] = 2;
4672 dwarf_regs["%ebx"] = dwarf_regs["%bx"] = dwarf_regs["%bl"] = 3;
4673 dwarf_regs["%esp"] = dwarf_regs["%sp"] = 4;
4674 dwarf_regs["%ebp"] = dwarf_regs["%bp"] = 5;
4675 dwarf_regs["%esi"] = dwarf_regs["%si"] = dwarf_regs["%sil"] = 6;
4676 dwarf_regs["%edi"] = dwarf_regs["%di"] = dwarf_regs["%dil"] = 7;
0491c523 4677 } else if (elf_machine == EM_PPC || elf_machine == EM_PPC64) {
8aabf152
FCE
4678 dwarf_regs["%r0"] = 0; dwarf_regs["%r1"] = 1; dwarf_regs["%r2"] = 2;
4679 dwarf_regs["%r3"] = 3; dwarf_regs["%r4"] = 4; dwarf_regs["%r5"] = 5;
4680 dwarf_regs["%r6"] = 6; dwarf_regs["%r7"] = 7; dwarf_regs["%r8"] = 8;
4681 dwarf_regs["%r9"] = 9; dwarf_regs["%r10"] = 10; dwarf_regs["%r11"] = 11;
4682 dwarf_regs["%r12"] = 12; dwarf_regs["%r13"] = 13; dwarf_regs["%r14"] = 14;
4683 dwarf_regs["%r15"] = 15; dwarf_regs["%r16"] = 16; dwarf_regs["%r17"] = 17;
4684 dwarf_regs["%r18"] = 18; dwarf_regs["%r19"] = 19; dwarf_regs["%r20"] = 20;
4685 dwarf_regs["%r21"] = 21; dwarf_regs["%r22"] = 22; dwarf_regs["%r23"] = 23;
4686 dwarf_regs["%r24"] = 24; dwarf_regs["%r25"] = 25; dwarf_regs["%r26"] = 26;
4687 dwarf_regs["%r27"] = 27; dwarf_regs["%r28"] = 28; dwarf_regs["%r29"] = 29;
c57ea854 4688 dwarf_regs["%r30"] = 30; dwarf_regs["%r31"] = 31;
8aabf152
FCE
4689 // PR11821: unadorned register "names" without -mregnames
4690 dwarf_regs["0"] = 0; dwarf_regs["1"] = 1; dwarf_regs["2"] = 2;
4691 dwarf_regs["3"] = 3; dwarf_regs["4"] = 4; dwarf_regs["5"] = 5;
4692 dwarf_regs["6"] = 6; dwarf_regs["7"] = 7; dwarf_regs["8"] = 8;
4693 dwarf_regs["9"] = 9; dwarf_regs["10"] = 10; dwarf_regs["11"] = 11;
4694 dwarf_regs["12"] = 12; dwarf_regs["13"] = 13; dwarf_regs["14"] = 14;
4695 dwarf_regs["15"] = 15; dwarf_regs["16"] = 16; dwarf_regs["17"] = 17;
4696 dwarf_regs["18"] = 18; dwarf_regs["19"] = 19; dwarf_regs["20"] = 20;
4697 dwarf_regs["21"] = 21; dwarf_regs["22"] = 22; dwarf_regs["23"] = 23;
4698 dwarf_regs["24"] = 24; dwarf_regs["25"] = 25; dwarf_regs["26"] = 26;
4699 dwarf_regs["27"] = 27; dwarf_regs["28"] = 28; dwarf_regs["29"] = 29;
c57ea854 4700 dwarf_regs["30"] = 30; dwarf_regs["31"] = 31;
14900130
SC
4701 } else if (elf_machine == EM_S390) {
4702 dwarf_regs["%r0"] = 0; dwarf_regs["%r1"] = 1; dwarf_regs["%r2"] = 2;
4703 dwarf_regs["%r3"] = 3; dwarf_regs["%r4"] = 4; dwarf_regs["%r5"] = 5;
4704 dwarf_regs["%r6"] = 6; dwarf_regs["%r7"] = 7; dwarf_regs["%r8"] = 8;
4705 dwarf_regs["%r9"] = 9; dwarf_regs["%r10"] = 10; dwarf_regs["%r11"] = 11;
4706 dwarf_regs["%r12"] = 12; dwarf_regs["%r13"] = 13; dwarf_regs["%r14"] = 14;
4707 dwarf_regs["%r15"] = 15;
4708 } else if (arg_count) {
8aabf152 4709 /* permit this case; just fall back to dwarf */
f83336a5
FCE
4710 }
4711
ebbd2b45 4712 need_debug_info = false;
aff5d390
SC
4713 tokenize(arg_string, arg_tokens, " ");
4714 assert(arg_count >= 0 && arg_count <= 10);
a8ec7719 4715 }
8aabf152 4716
f83336a5 4717 systemtap_session& session;
332ba7e7 4718 int elf_machine;
aff5d390 4719 const string & process_name;
a794dbeb 4720 const string & provider_name;
aff5d390 4721 const string & probe_name;
8aabf152 4722 unsigned arg_count;
aff5d390
SC
4723 vector<string> arg_tokens;
4724 map<string,int> dwarf_regs;
ebbd2b45 4725 bool need_debug_info;
aff5d390
SC
4726
4727 void visit_target_symbol (target_symbol* e);
aff5d390
SC
4728};
4729
7a05f484
SC
4730
4731void
aff5d390 4732sdt_uprobe_var_expanding_visitor::visit_target_symbol (target_symbol *e)
7a05f484 4733{
63ea4244 4734 try
7a05f484 4735 {
277c21bc 4736 if (e->name == "$$name")
aff5d390
SC
4737 {
4738 if (e->addressof)
4739 throw semantic_error("cannot take address of sdt context variable", e->tok);
7a05f484 4740
aff5d390
SC
4741 literal_string *myname = new literal_string (probe_name);
4742 myname->tok = e->tok;
4743 provide(myname);
4744 return;
4745 }
277c21bc 4746 if (e->name == "$$provider")
a794dbeb
FCE
4747 {
4748 if (e->addressof)
4749 throw semantic_error("cannot take address of sdt context variable", e->tok);
4750
4751 literal_string *myname = new literal_string (provider_name);
4752 myname->tok = e->tok;
4753 provide(myname);
4754 return;
4755 }
63ea4244 4756
8aabf152 4757 unsigned argno = 0; // the N in $argN
c69a87e0 4758 try
aff5d390 4759 {
5ecaa5a7 4760 if (startswith(e->name, "$arg"))
8aabf152 4761 argno = lex_cast<unsigned>(e->name.substr(4));
aff5d390 4762 }
c69a87e0 4763 catch (const runtime_error& f) // non-integral $arg suffix: e.g. $argKKKSDF
aff5d390 4764 {
8aabf152 4765 argno = 0;
aff5d390 4766 }
5ecaa5a7 4767
8aabf152
FCE
4768 if (arg_count == 0 || // a sdt.h variant without .probe-stored arg_count
4769 argno < 1 || argno > arg_count) // a $argN with out-of-range N
aff5d390 4770 {
8aabf152
FCE
4771 // NB: Either
4772 // 1) uprobe1_type $argN or $FOO (we don't know the arg_count)
4773 // 2) uprobe2_type $FOO (no probe args)
4774 // both of which get resolved later.
4775 need_debug_info = true;
4776 provide(e);
4777 return;
aff5d390 4778 }
277c21bc 4779
8aabf152
FCE
4780 assert (arg_tokens.size() >= argno);
4781 string asmarg = arg_tokens[argno-1]; // $arg1 => arg_tokens[0]
c57ea854 4782
8aabf152
FCE
4783 // Now we try to parse this thing, which is an assembler operand
4784 // expression. If we can't, we warn, back down to need_debug_info
b874bd52
SC
4785 // and hope for the best. Here is the syntax for a few architectures.
4786 // Note that the power iN syntax is only for V3 sdt.h; gcc emits N.
4787 // literal reg reg reg +
4788 // indirect offset
4789 // x86 $N %rR (%rR) N(%rR)
4790 // power iN R (R) N(R)
4791 // ia64 N rR [r16]
4792 // s390 N %rR 0(rR) N(r15)
4793 // arm #N rR [rR] [rR, #N]
4794
8aabf152
FCE
4795 expression* argexpr = 0; // filled in in case of successful parse
4796
4797 string percent_regnames;
4798 string regnames;
4799 vector<string> matches;
40fe32e0 4800 int precision = 0;
8aabf152
FCE
4801 int rc;
4802
40fe32e0
SC
4803 // Parse the leading length
4804
4805 if (asmarg.find('@') != string::npos)
4806 {
4807 precision = lex_cast<int>(asmarg.substr(0, asmarg.find('@')));
4808 asmarg = asmarg.substr(asmarg.find('@')+1);
4809 }
4810
8aabf152
FCE
4811 // test for a numeric literal.
4812 // Only accept (signed) decimals throughout. XXX
4813
4814 // PR11821. NB: on powerpc, literals are not prefixed with $,
4815 // so this regex does not match. But that's OK, since without
4816 // -mregnames, we can't tell them apart from register numbers
4817 // anyway. With -mregnames, we could, if gcc somehow
4818 // communicated to us the presence of that option, but alas it
4819 // doesn't. http://gcc.gnu.org/PR44995.
9109f487 4820 rc = regexp_match (asmarg, "^[i\\$][-]?[0-9][0-9]*$", matches);
8aabf152
FCE
4821 if (! rc)
4822 {
4823 literal_number* ln = new literal_number(lex_cast<int>(matches[0].substr(1))); // assume decimal
4824 ln->tok = e->tok;
4825 argexpr = ln;
4826 goto matched;
4827 }
4828
14900130
SC
4829 if (dwarf_regs.empty())
4830 goto not_matched;
d5b83cee 4831
8aabf152
FCE
4832 // Build regex pieces out of the known dwarf_regs. We keep two separate
4833 // lists: ones with the % prefix (and thus unambigiuous even despite PR11821),
4834 // and ones with no prefix (and thus only usable in unambiguous contexts).
4835 for (map<string,int>::iterator ri = dwarf_regs.begin(); ri != dwarf_regs.end(); ri++)
4836 {
4837 string regname = ri->first;
4838 assert (regname != "");
4839 regnames += string("|")+regname;
4840 if (regname[0]=='%')
4841 percent_regnames += string("|")+regname;
4842 }
4843 // clip off leading |
4844 regnames = regnames.substr(1);
4845 percent_regnames = percent_regnames.substr(1);
4846
4847 // test for REGISTER
4848 // NB: Because PR11821, we must use percent_regnames here.
332ba7e7 4849 if (elf_machine == EM_PPC || elf_machine == EM_PPC64)
9109f487
SC
4850 rc = regexp_match (asmarg, string("^(")+regnames+string(")$"), matches);
4851 else
332ba7e7 4852 rc = regexp_match (asmarg, string("^(")+percent_regnames+string(")$"), matches);
8aabf152
FCE
4853 if (! rc)
4854 {
4855 string regname = matches[1];
4856 if (dwarf_regs.find (regname) != dwarf_regs.end()) // known register
4857 {
4858 embedded_expr *get_arg1 = new embedded_expr;
4859 get_arg1->tok = e->tok;
4860 get_arg1->code = string("/* unprivileged */ /* pure */")
4861 + (is_user_module (process_name)
4862 ? string("u_fetch_register(")
4863 : string("k_fetch_register("))
4864 + lex_cast(dwarf_regs[regname]) + string(")");
4865 // XXX: may we ever need to cast that to a narrower type?
4866 argexpr = get_arg1;
4867 goto matched;
4868 }
4869 // invalid register name, fall through
4870 }
40fe32e0 4871
8aabf152 4872 // test for OFFSET(REGISTER)
40fe32e0 4873 // NB: Despite PR11821, we can use regnames here, since the parentheses
8aabf152
FCE
4874 // make things unambiguous.
4875 rc = regexp_match (asmarg, string("^([-]?[0-9]*)[(](")+regnames+string(")[)]$"), matches);
4876 if (! rc)
4877 {
4878 string dispstr = matches[1];
4879 int64_t disp = 0;
4880 if (dispstr == "")
4881 disp = 0;
4882 else
4883 try
4884 {
4885 disp = lex_cast<int64_t>(dispstr); // should decode positive/negative hex/decimal
4886 }
4887 catch (const runtime_error& f) // unparseable offset
4888 {
4889 goto not_matched; // can't just 'break' out of
4890 // this case or use a sentinel
4891 // value, unfortunately
4892 }
4893
4894 string regname = matches[2];
4895 if (dwarf_regs.find (regname) != dwarf_regs.end()) // known register
4896 {
4897 // synthesize user_long(%{fetch_register(R)%} + D)
40fe32e0 4898
8aabf152
FCE
4899 embedded_expr *get_arg1 = new embedded_expr;
4900 get_arg1->tok = e->tok;
4901 get_arg1->code = string("/* unprivileged */ /* pure */")
4902 + (is_user_module (process_name)
4903 ? string("u_fetch_register(")
4904 : string("k_fetch_register("))
4905 + lex_cast(dwarf_regs[regname]) + string(")");
4906 // XXX: may we ever need to cast that to a narrower type?
40fe32e0 4907
8aabf152
FCE
4908 literal_number* inc = new literal_number(disp);
4909 inc->tok = e->tok;
40fe32e0 4910
8aabf152
FCE
4911 binary_expression *be = new binary_expression;
4912 be->tok = e->tok;
4913 be->left = get_arg1;
4914 be->op = "+";
4915 be->right = inc;
40fe32e0 4916
8aabf152 4917 functioncall *fc = new functioncall;
40fe32e0
SC
4918 switch (precision)
4919 {
4920 case 4: case -4:
4921 fc->function = "user_int"; break;
4922 case 8: case -8:
4923 fc->function = "user_long"; break;
4924 default: fc->function = "user_long";
4925 }
8aabf152
FCE
4926 fc->tok = e->tok;
4927 fc->args.push_back(be);
366af4e7 4928
8aabf152
FCE
4929 argexpr = fc;
4930 goto matched;
4931 }
4932 // invalid register name, fall through
4933 }
4934
4935 not_matched:
4936 // The asmarg operand was not recognized. Back down to dwarf.
4937 if (! session.suppress_warnings)
4938 session.print_warning ("Downgrading SDT_V2 probe argument to dwarf, can't parse '"+asmarg+"'", e->tok);
4939 assert (argexpr == 0);
4940 need_debug_info = true;
4941 provide (e);
4942 return;
366af4e7 4943
8aabf152
FCE
4944 matched:
4945 assert (argexpr != 0);
366af4e7
RM
4946
4947 if (session.verbose > 2)
8aabf152 4948 clog << "mapped asm operand " << asmarg << " to " << *argexpr << endl;
366af4e7 4949
aff5d390 4950 if (e->components.empty()) // We have a scalar
8aabf152
FCE
4951 {
4952 if (e->addressof)
4953 throw semantic_error("cannot take address of sdt variable", e->tok);
4954 provide (argexpr);
4955 return;
4956 }
4957 else // $var->foo
4958 {
4959 cast_op *cast = new cast_op;
4960 cast->name = "@cast";
4961 cast->tok = e->tok;
4962 cast->operand = argexpr;
4963 cast->components = e->components;
4964 cast->type_name = probe_name + "_arg" + lex_cast(argno);
4965 cast->module = process_name;
4966 cast->visit(this);
4967 return;
4968 }
366af4e7 4969
8aabf152 4970 /* NOTREACHED */
aff5d390
SC
4971 }
4972 catch (const semantic_error &er)
4973 {
4974 e->chain (er);
4975 provide (e);
4976 }
4977}
4978
4979
4980void
4981sdt_kprobe_var_expanding_visitor::visit_target_symbol (target_symbol *e)
4982{
4983 try
4984 {
277c21bc 4985 if (e->name == "$$name")
aff5d390
SC
4986 {
4987 if (e->addressof)
4988 throw semantic_error("cannot take address of sdt context variable", e->tok);
4989
4990 literal_string *myname = new literal_string (probe_name);
4991 myname->tok = e->tok;
4992 provide(myname);
4993 return;
4994 }
277c21bc 4995 if (e->name == "$$provider")
a794dbeb
FCE
4996 {
4997 if (e->addressof)
4998 throw semantic_error("cannot take address of sdt context variable", e->tok);
4999
5000 literal_string *myname = new literal_string (provider_name);
5001 myname->tok = e->tok;
5002 provide(myname);
5003 return;
5004 }
aff5d390 5005
5ecaa5a7 5006 int argno = -1;
aff5d390
SC
5007 try
5008 {
5ecaa5a7
JS
5009 if (startswith(e->name, "$arg"))
5010 argno = lex_cast<int>(e->name.substr(4));
aff5d390
SC
5011 }
5012 catch (const runtime_error& f) // non-integral $arg suffix: e.g. $argKKKSDF
5013 {
aff5d390 5014 }
5ecaa5a7
JS
5015 if (argno < 0)
5016 throw semantic_error("invalid variable, must be of the form $argN", e->tok);
aff5d390
SC
5017 if (argno < 1 || argno > arg_count)
5018 throw semantic_error("invalid argument number", e->tok);
5ecaa5a7 5019
c69a87e0
FCE
5020 bool lvalue = is_active_lvalue(e);
5021 functioncall *fc = new functioncall;
63ea4244 5022
c69a87e0
FCE
5023 // First two args are hidden: 1. pointer to probe name 2. task id
5024 if (arg_count < 2)
aff5d390
SC
5025 {
5026 fc->function = "ulong_arg";
5027 fc->type = pe_long;
5028 fc->tok = e->tok;
5029 // skip the hidden args
5030 literal_number* num = new literal_number(argno + 2);
5031 num->tok = e->tok;
5032 fc->args.push_back(num);
5033 }
5034 else
5035 {
5036 // args are passed in arg3 as members of a struct
5037 fc->function = "user_long";
5038 fc->tok = e->tok;
5039 binary_expression *be = new binary_expression;
5040 be->tok = e->tok;
5041 functioncall *get_arg1 = new functioncall;
5042 get_arg1->function = "pointer_arg";
5043 get_arg1->tok = e->tok;
5044 // arg3 is the pointer to a struct of arguments
c57ea854 5045 literal_number* num = new literal_number(3);
aff5d390
SC
5046 num->tok = e->tok;
5047 get_arg1->args.push_back(num);
5048
5049 be->left = get_arg1;
5050 be->op = "+";
5051 // offset in struct to the desired arg
5052 literal_number* inc = new literal_number((argno - 1) * 8);
5053 inc->tok = e->tok;
5054 be->right = inc;
5055 fc->args.push_back(be);
5056 }
c69a87e0 5057 if (lvalue)
aff5d390 5058 *(target_symbol_setter_functioncalls.top()) = fc;
63ea4244 5059
aff5d390
SC
5060 if (e->components.empty()) // We have a scalar
5061 {
5062 if (e->addressof)
5063 throw semantic_error("cannot take address of sdt variable", e->tok);
63ea4244 5064
aff5d390
SC
5065 provide(fc);
5066 return;
5067 }
c69a87e0 5068 cast_op *cast = new cast_op;
277c21bc 5069 cast->name = "@cast";
c69a87e0
FCE
5070 cast->tok = e->tok;
5071 cast->operand = fc;
5072 cast->components = e->components;
7f6b80bd 5073 cast->type_name = probe_name + "_arg" + lex_cast(argno);
c69a87e0 5074 cast->module = process_name;
63ea4244 5075
c69a87e0 5076 cast->visit(this);
7a05f484 5077 }
c69a87e0 5078 catch (const semantic_error &er)
ad002306 5079 {
1af1e62d 5080 e->chain (er);
c69a87e0 5081 provide (e);
ad002306 5082 }
7a05f484 5083}
46b84a80 5084
277c21bc 5085
edce5b67
JS
5086struct sdt_query : public base_query
5087{
5088 sdt_query(probe * base_probe, probe_point * base_loc,
5089 dwflpp & dw, literal_map_t const & params,
5090 vector<derived_probe *> & results);
5091
5092 void handle_query_module();
5093
5094private:
15284963 5095 stap_sdt_probe_type probe_type;
9109f487 5096 enum {probe_section=0, note_section=1} probe_loc;
edce5b67
JS
5097 probe * base_probe;
5098 probe_point * base_loc;
6846cfc8 5099 literal_map_t const & params;
edce5b67 5100 vector<derived_probe *> & results;
a794dbeb
FCE
5101 string pp_mark;
5102 string pp_provider;
edce5b67
JS
5103
5104 set<string> probes_handled;
5105
5106 Elf_Data *pdata;
5107 size_t probe_scn_offset;
5108 size_t probe_scn_addr;
aff5d390 5109 uint64_t arg_count;
40fe32e0 5110 GElf_Addr base;
c57ea854 5111 GElf_Addr pc;
aff5d390 5112 string arg_string;
edce5b67 5113 string probe_name;
a794dbeb 5114 string provider_name;
79a0ca08 5115 Dwarf_Addr semaphore;
edce5b67
JS
5116
5117 bool init_probe_scn();
6b51ee12 5118 bool get_next_probe();
c57ea854
SC
5119 void iterate_over_probe_entries();
5120 void handle_probe_entry();
edce5b67 5121
40fe32e0
SC
5122 static void setup_note_probe_entry_callback (void *object, int type, const char *data, size_t len);
5123 void setup_note_probe_entry (int type, const char *data, size_t len);
5124
edce5b67 5125 void convert_probe(probe *base);
4ddb6dd0 5126 void record_semaphore(vector<derived_probe *> & results, unsigned start);
c72aa911 5127 probe* convert_location();
40fe32e0 5128 bool have_uprobe() {return probe_type == uprobe1_type || probe_type == uprobe2_type || probe_type == uprobe3_type;}
aff5d390 5129 bool have_kprobe() {return probe_type == kprobe1_type || probe_type == kprobe2_type;}
c57ea854
SC
5130 bool have_debuginfo_uprobe(bool need_debug_info)
5131 {return probe_type == uprobe1_type
40fe32e0 5132 || ((probe_type == uprobe2_type || probe_type == uprobe3_type)
c57ea854 5133 && need_debug_info);}
40fe32e0 5134 bool have_debuginfoless_uprobe() {return probe_type == uprobe2_type || probe_type == uprobe3_type;}
edce5b67
JS
5135};
5136
5137
5138sdt_query::sdt_query(probe * base_probe, probe_point * base_loc,
5139 dwflpp & dw, literal_map_t const & params,
5140 vector<derived_probe *> & results):
5141 base_query(dw, params), base_probe(base_probe),
6846cfc8 5142 base_loc(base_loc), params(params), results(results)
edce5b67 5143{
a794dbeb
FCE
5144 assert(get_string_param(params, TOK_MARK, pp_mark));
5145 get_string_param(params, TOK_PROVIDER, pp_provider); // pp_provider == "" -> unspecified
5146
ef428667
FCE
5147 // PR10245: permit usage of dtrace-y "-" separator in marker name;
5148 // map it to double-underscores.
5149 size_t pos = 0;
5150 while (1) // there may be more than one
5151 {
a794dbeb 5152 size_t i = pp_mark.find("-", pos);
ef428667 5153 if (i == string::npos) break;
a794dbeb 5154 pp_mark.replace (i, 1, "__");
ef428667
FCE
5155 pos = i+1; // resume searching after the inserted __
5156 }
a794dbeb
FCE
5157
5158 // XXX: same for pp_provider?
edce5b67
JS
5159}
5160
5161
5162void
c57ea854 5163sdt_query::handle_probe_entry()
edce5b67 5164{
c57ea854
SC
5165 if (! have_uprobe()
5166 && !probes_handled.insert(probe_name).second)
edce5b67
JS
5167 return;
5168
5169 if (sess.verbose > 3)
c57ea854
SC
5170 {
5171 clog << "matched probe_name " << probe_name << " probe_type ";
5172 switch (probe_type)
5173 {
5174 case uprobe1_type:
5175 clog << "uprobe1 at 0x" << hex << pc << dec << endl;
5176 break;
5177 case uprobe2_type:
5178 clog << "uprobe2 at 0x" << hex << pc << dec << endl;
5179 break;
40fe32e0
SC
5180 case uprobe3_type:
5181 clog << "uprobe3 at 0x" << hex << pc << dec << endl;
5182 break;
c57ea854
SC
5183 case kprobe1_type:
5184 clog << "kprobe1" << endl;
5185 break;
5186 case kprobe2_type:
5187 clog << "kprobe2" << endl;
5188 break;
5189 }
5190 }
edce5b67 5191
c57ea854
SC
5192 // Extend the derivation chain
5193 probe *new_base = convert_location();
5194 probe_point *new_location = new_base->locations[0];
5195
5196 bool kprobe_found = false;
5197 bool need_debug_info = false;
5198
5199 Dwarf_Addr bias;
5200 Elf* elf = (dwarf_getelf (dwfl_module_getdwarf (dw.mod_info->mod, &bias))
5201 ?: dwfl_module_getelf (dw.mod_info->mod, &bias));
5202
5203 if (have_kprobe())
edce5b67 5204 {
c57ea854
SC
5205 convert_probe(new_base);
5206 kprobe_found = true;
5207 // Expand the local variables in the probe body
5208 sdt_kprobe_var_expanding_visitor svv (module_val,
5209 provider_name,
5210 probe_name,
5211 arg_string,
5212 arg_count);
5213 svv.replace (new_base->body);
5214 }
5215 else
5216 {
5217 /* Figure out the architecture of this particular ELF file.
5218 The dwarfless register-name mappings depend on it. */
5219 GElf_Ehdr ehdr_mem;
5220 GElf_Ehdr* em = gelf_getehdr (elf, &ehdr_mem);
5221 if (em == 0) { dwfl_assert ("dwfl_getehdr", dwfl_errno()); }
5222 int elf_machine = em->e_machine;
5223 sdt_uprobe_var_expanding_visitor svv (sess, elf_machine,
5224 module_val,
5225 provider_name,
5226 probe_name,
5227 arg_string,
5228 arg_count);
5229 svv.replace (new_base->body);
5230 need_debug_info = svv.need_debug_info;
5231 }
5232
5233 unsigned i = results.size();
edce5b67 5234
c57ea854
SC
5235 if (have_kprobe())
5236 derive_probes(sess, new_base, results);
5237
5238 else
5239 {
5240 // XXX: why not derive_probes() in the uprobes case too?
5241 literal_map_t params;
5242 for (unsigned i = 0; i < new_location->components.size(); ++i)
39a3e397 5243 {
c57ea854
SC
5244 probe_point::component *c = new_location->components[i];
5245 params[c->functor] = c->arg;
39a3e397 5246 }
30263a73 5247
c57ea854
SC
5248 dwarf_query q(new_base, new_location, dw, params, results, "", "");
5249 q.has_mark = true; // enables mid-statement probing
edce5b67 5250
c57ea854
SC
5251 // V2 probes need dwarf info in case of a variable reference
5252 if (have_debuginfo_uprobe(need_debug_info))
5253 dw.iterate_over_modules(&query_module, &q);
5254 else if (have_debuginfoless_uprobe())
aff5d390 5255 {
c57ea854
SC
5256 string section;
5257 Dwarf_Addr reloc_addr = q.statement_num_val + bias;
5258 if (dwfl_module_relocations (q.dw.mod_info->mod) > 0)
5259 {
5260 dwfl_module_relocate_address (q.dw.mod_info->mod, &reloc_addr);
5261 section = ".dynamic";
5262 }
5263 else
5264 section = ".absolute";
5265
5266 uprobe_derived_probe* p =
5267 new uprobe_derived_probe ("", "", 0, q.module_val, section,
5268 q.statement_num_val, reloc_addr, q, 0);
5269 p->saveargs (arg_count);
5270 results.push_back (p);
aff5d390 5271 }
c57ea854
SC
5272 }
5273 record_semaphore(results, i);
5274}
edce5b67 5275
4ddb6dd0 5276
c57ea854
SC
5277void
5278sdt_query::handle_query_module()
5279{
5280 if (!init_probe_scn())
5281 return;
edce5b67 5282
c57ea854
SC
5283 if (sess.verbose > 3)
5284 clog << "TOK_MARK: " << pp_mark << " TOK_PROVIDER: " << pp_provider << endl;
edce5b67 5285
40fe32e0
SC
5286 if (probe_loc == note_section)
5287 {
5288 GElf_Shdr shdr_mem;
5289 GElf_Shdr *shdr = dw.get_section (".stapsdt.base", &shdr_mem);
5290
5291 if (shdr)
5292 base = shdr->sh_addr;
5293 else
5294 base = 0;
5295 dw.iterate_over_notes ((void*) this, &sdt_query::setup_note_probe_entry_callback);
5296 }
5297 else
5298 iterate_over_probe_entries ();
edce5b67
JS
5299}
5300
5301
5302bool
5303sdt_query::init_probe_scn()
5304{
448a86b7 5305 Elf* elf;
edce5b67 5306 GElf_Shdr shdr_mem;
40fe32e0
SC
5307
5308 GElf_Shdr *shdr = dw.get_section (".note.stapsdt", &shdr_mem);
5309 if (shdr)
5310 {
5311 probe_loc = note_section;
5312 return true;
5313 }
edce5b67 5314
448a86b7 5315 shdr = dw.get_section (".probes", &shdr_mem, &elf);
fea74777 5316 if (shdr)
edce5b67 5317 {
fea74777
SC
5318 pdata = elf_getdata_rawchunk (elf, shdr->sh_offset, shdr->sh_size, ELF_T_BYTE);
5319 probe_scn_offset = 0;
5320 probe_scn_addr = shdr->sh_addr;
5321 assert (pdata != NULL);
5322 if (sess.verbose > 4)
5323 clog << "got .probes elf scn_addr@0x" << probe_scn_addr << dec
5324 << ", size: " << pdata->d_size << endl;
40fe32e0 5325 probe_loc = probe_section;
fea74777 5326 return true;
edce5b67 5327 }
fea74777 5328 else
edce5b67 5329 return false;
edce5b67
JS
5330}
5331
40fe32e0
SC
5332void
5333sdt_query::setup_note_probe_entry_callback (void *object, int type, const char *data, size_t len)
5334{
5335 sdt_query *me = (sdt_query*)object;
5336 me->setup_note_probe_entry (type, data, len);
5337}
5338
5339
5340void
5341sdt_query::setup_note_probe_entry (int type, const char *data, size_t len)
5342{
5343 // if (nhdr.n_namesz == sizeof _SDT_NOTE_NAME
5344 // && !memcmp (data->d_buf + name_off,
5345 // _SDT_NOTE_NAME, sizeof _SDT_NOTE_NAME))
5346
5347 // probes are in the .note.stapsdt section
5348#define _SDT_NOTE_TYPE 3
5349 if (type != _SDT_NOTE_TYPE)
5350 return;
5351
5352 union
5353 {
5354 Elf64_Addr a64[3];
5355 Elf32_Addr a32[3];
5356 } buf;
5357 Dwarf_Addr bias;
5358 Elf* elf = (dwfl_module_getelf (dw.mod_info->mod, &bias));
5359 Elf_Data dst =
5360 {
5361 &buf, ELF_T_ADDR, EV_CURRENT,
5362 gelf_fsize (elf, ELF_T_ADDR, 3, EV_CURRENT), 0, 0
5363 };
5364 assert (dst.d_size <= sizeof buf);
5365
5366 if (len < dst.d_size + 3)
5367 return;
5368
5369 Elf_Data src =
5370 {
5371 (void *) data, ELF_T_ADDR, EV_CURRENT,
5372 dst.d_size, 0, 0
5373 };
5374
5375 if (gelf_xlatetom (elf, &dst, &src,
5376 elf_getident (elf, NULL)[EI_DATA]) == NULL)
5377 printf ("gelf_xlatetom: %s", elf_errmsg (-1));
5378
5379 probe_type = uprobe3_type;
5380 const char * provider = data + dst.d_size;
5381 provider_name = provider;
5382 const char *name = (const char*)memchr (provider, '\0', data + len - provider);
5383 probe_name = ++name;
5384
5385 // Did we find a matching probe?
5386 if (! (dw.function_name_matches_pattern (probe_name, pp_mark)
5387 && ((pp_provider == "")
5388 || dw.function_name_matches_pattern (provider_name, pp_provider))))
5389 return;
5390
5391 const char *args = (const char*)memchr (name, '\0', data + len - name);
5392 if (args++ == NULL ||
5393 memchr (args, '\0', data + len - name) != data + len - 1)
5394 if (name == NULL)
5395 return;
5396 arg_string = args;
5397
5398 arg_count = 0;
5399 for (unsigned i = 0; i < arg_string.length(); i++)
5400 if (arg_string[i] == ' ')
5401 arg_count += 1;
5402 if (arg_string.length() != 0)
5403 arg_count += 1;
5404
5405 GElf_Addr base_ref;
5406 if (gelf_getclass (elf) == ELFCLASS32)
5407 {
5408 pc = buf.a32[0];
5409 base_ref = buf.a32[1];
5410 semaphore = buf.a32[2];
5411 }
5412 else
5413 {
5414 pc = buf.a64[0];
5415 base_ref = buf.a64[1];
5416 semaphore = buf.a64[2];
5417 }
5418
5419 semaphore += base - base_ref;
5420 pc += base - base_ref;
5421
5422 if (sess.verbose > 4)
5423 clog << "saw .note.stapsdt " << probe_name << (provider_name != "" ? " (provider "+provider_name+") " : "")
5424 << "@0x" << hex << pc << dec << endl;
5425
5426 handle_probe_entry();
5427}
5428
5429
c57ea854
SC
5430void
5431sdt_query::iterate_over_probe_entries()
edce5b67 5432{
c57ea854 5433 // probes are in the .probe section
edce5b67
JS
5434 while (probe_scn_offset < pdata->d_size)
5435 {
aff5d390
SC
5436 stap_sdt_probe_entry_v1 *pbe_v1 = (stap_sdt_probe_entry_v1 *) ((char*)pdata->d_buf + probe_scn_offset);
5437 stap_sdt_probe_entry_v2 *pbe_v2 = (stap_sdt_probe_entry_v2 *) ((char*)pdata->d_buf + probe_scn_offset);
15284963 5438 probe_type = (stap_sdt_probe_type)(pbe_v1->type_a);
aff5d390 5439 if (! have_uprobe() && ! have_kprobe())
edce5b67
JS
5440 {
5441 // Unless this is a mangled .probes section, this happens
5442 // because the name of the probe comes first, followed by
5443 // the sentinel.
5444 if (sess.verbose > 5)
5445 clog << "got unknown probe_type: 0x" << hex << probe_type
5446 << dec << endl;
5447 probe_scn_offset += sizeof(__uint32_t);
5448 continue;
5449 }
aff5d390
SC
5450 if ((long)pbe_v1 % sizeof(__uint64_t)) // we have stap_sdt_probe_entry_v1.type_b
5451 {
5452 pbe_v1 = (stap_sdt_probe_entry_v1*)((char*)pbe_v1 - sizeof(__uint32_t));
5453 if (pbe_v1->type_b != uprobe1_type && pbe_v1->type_b != kprobe1_type)
5454 continue;
5455 }
5456
5457 if (probe_type == uprobe1_type || probe_type == kprobe1_type)
5458 {
79a0ca08 5459 if (pbe_v1->name == 0) // No name possibly means we have a .so with a relocation
c57ea854 5460 return;
79a0ca08 5461 semaphore = 0;
aff5d390 5462 probe_name = (char*)((char*)pdata->d_buf + pbe_v1->name - (char*)probe_scn_addr);
a794dbeb 5463 provider_name = ""; // unknown
aff5d390
SC
5464 if (probe_type == uprobe1_type)
5465 {
5466 pc = pbe_v1->arg;
5467 arg_count = 0;
5468 }
5469 else if (probe_type == kprobe1_type)
5470 arg_count = pbe_v1->arg;
5471 probe_scn_offset += sizeof (stap_sdt_probe_entry_v1);
5472 }
5473 else if (probe_type == uprobe2_type || probe_type == kprobe2_type)
5474 {
79a0ca08 5475 if (pbe_v2->name == 0) // No name possibly means we have a .so with a relocation
c57ea854 5476 return;
79a0ca08 5477 semaphore = pbe_v2->semaphore;
aff5d390 5478 probe_name = (char*)((char*)pdata->d_buf + pbe_v2->name - (char*)probe_scn_addr);
a794dbeb 5479 provider_name = (char*)((char*)pdata->d_buf + pbe_v2->provider - (char*)probe_scn_addr);
aff5d390
SC
5480 arg_count = pbe_v2->arg_count;
5481 pc = pbe_v2->pc;
5482 if (pbe_v2->arg_string)
5483 arg_string = (char*)((char*)pdata->d_buf + pbe_v2->arg_string - (char*)probe_scn_addr);
79a0ca08
SC
5484 // skip over pbe_v2, probe_name text and provider text
5485 probe_scn_offset = ((long)(pbe_v2->name) - (long)(probe_scn_addr)) + probe_name.length();
5486 probe_scn_offset += sizeof (__uint32_t) - probe_scn_offset % sizeof (__uint32_t);
aff5d390 5487 }
edce5b67 5488 if (sess.verbose > 4)
a794dbeb 5489 clog << "saw .probes " << probe_name << (provider_name != "" ? " (provider "+provider_name+") " : "")
aff5d390 5490 << "@0x" << hex << pc << dec << endl;
edce5b67 5491
a794dbeb
FCE
5492 if (dw.function_name_matches_pattern (probe_name, pp_mark)
5493 && ((pp_provider == "") || dw.function_name_matches_pattern (provider_name, pp_provider)))
c57ea854 5494 handle_probe_entry ();
edce5b67 5495 }
edce5b67
JS
5496}
5497
5498
6846cfc8 5499void
4ddb6dd0 5500sdt_query::record_semaphore (vector<derived_probe *> & results, unsigned start)
6846cfc8 5501{
a794dbeb
FCE
5502 for (unsigned i=0; i<2; i++) {
5503 // prefer with-provider symbol; look without provider prefix for backward compatibility only
5504 string semaphore = (i==0 ? (provider_name+"_") : "") + probe_name + "_semaphore";
5505 // XXX: multiple addresses?
5506 if (sess.verbose > 2)
5507 clog << "looking for semaphore symbol " << semaphore;
5508
79a0ca08
SC
5509 Dwarf_Addr addr;
5510 if (this->semaphore)
5511 addr = this->semaphore;
5512 else
5513 addr = lookup_symbol_address(dw.module, semaphore.c_str());
a794dbeb
FCE
5514 if (addr)
5515 {
40fe32e0
SC
5516 if (probe_type != uprobe3_type
5517 && dwfl_module_relocations (dw.module) > 0)
a794dbeb
FCE
5518 dwfl_module_relocate_address (dw.module, &addr);
5519 // XXX: relocation basis?
5520 for (unsigned i = start; i < results.size(); ++i)
5521 results[i]->sdt_semaphore_addr = addr;
5522 if (sess.verbose > 2)
5523 clog << ", found at 0x" << hex << addr << dec << endl;
5524 return;
5525 }
5526 else
5527 if (sess.verbose > 2)
5528 clog << ", not found" << endl;
5529 }
6846cfc8
SC
5530}
5531
5532
edce5b67
JS
5533void
5534sdt_query::convert_probe (probe *base)
5535{
5536 block *b = new block;
5537 b->tok = base->body->tok;
5538
5539 // XXX: Does this also need to happen for i386 under x86_64 stap?
ebbd2b45 5540 if (sess.architecture == "i386" && have_kprobe())
edce5b67
JS
5541 {
5542 functioncall *rp = new functioncall;
edce5b67
JS
5543 rp->function = "regparm";
5544 rp->tok = b->tok;
5545 literal_number* littid = new literal_number(0);
5546 littid->tok = b->tok;
5547 rp->args.push_back(littid);
5548 expr_statement* es = new expr_statement;
5549 es->tok = b->tok;
5550 es->value = rp;
5551 b->statements.push_back(es);
5552 }
edce5b67 5553
aff5d390 5554 if (have_kprobe())
edce5b67 5555 {
aff5d390 5556 // Generate: if (arg2 != kprobe2_type) next;
edce5b67
JS
5557 if_statement *istid = new if_statement;
5558 istid->thenblock = new next_statement;
5559 istid->elseblock = NULL;
5560 istid->tok = b->tok;
63ea4244 5561 istid->thenblock->tok = b->tok;
edce5b67
JS
5562 comparison *betid = new comparison;
5563 betid->op = "!=";
5564 betid->tok = b->tok;
5565
5566 functioncall *arg2 = new functioncall;
5567 arg2->function = "ulong_arg";
5568 arg2->tok = b->tok;
5569 literal_number* num = new literal_number(2);
5570 num->tok = b->tok;
5571 arg2->args.push_back(num);
5572
5573 betid->left = arg2;
aff5d390 5574 literal_number* littid = new literal_number(probe_type);
edce5b67
JS
5575 littid->tok = b->tok;
5576 betid->right = littid;
5577 istid->condition = betid;
5578 b->statements.push_back(istid);
5579 }
5580
5581 // Generate: if (arg1 != mark("label")) next;
5582 functioncall *fc = new functioncall;
bbafcb1e 5583 fc->function = "ulong_arg";
edce5b67 5584 fc->tok = b->tok;
bbafcb1e 5585 literal_number* num = new literal_number(1);
edce5b67
JS
5586 num->tok = b->tok;
5587 fc->args.push_back(num);
5588
5589 functioncall *fcus = new functioncall;
5590 fcus->function = "user_string";
5591 fcus->type = pe_string;
5592 fcus->tok = b->tok;
5593 fcus->args.push_back(fc);
5594
5595 if_statement *is = new if_statement;
5596 is->thenblock = new next_statement;
5597 is->elseblock = NULL;
5598 is->tok = b->tok;
63ea4244 5599 is->thenblock->tok = b->tok;
edce5b67
JS
5600 comparison *be = new comparison;
5601 be->op = "!=";
5602 be->tok = b->tok;
5603 be->left = fcus;
5604 be->right = new literal_string(probe_name);
63ea4244 5605 be->right->tok = b->tok;
edce5b67
JS
5606 is->condition = be;
5607 b->statements.push_back(is);
5608
5609 // Now replace the body
5610 b->statements.push_back(base->body);
5611 base->body = b;
5612}
5613
5614
c72aa911
JS
5615probe*
5616sdt_query::convert_location ()
edce5b67 5617{
c72aa911
JS
5618 probe_point* specific_loc = new probe_point(*base_loc);
5619 probe_point* derived_loc = new probe_point(*base_loc);
edce5b67 5620
c72aa911
JS
5621 for (unsigned i = 0; i < derived_loc->components.size(); ++i)
5622 if (derived_loc->components[i]->functor == TOK_MARK)
5623 {
5624 // replace the possibly wildcarded arg with the specific marker name
5625 specific_loc->components[i] =
5626 new probe_point::component(TOK_MARK, new literal_string(probe_name));
edce5b67 5627
a794dbeb
FCE
5628 // XXX: similarly, fill in a TOK_PROVIDER() on the base_loc if
5629 // one was missing/unspecified/wildcarded.
5630
aff5d390
SC
5631 if (sess.verbose > 3)
5632 switch (probe_type)
5633 {
5634 case uprobe1_type:
5635 clog << "probe_type == uprobe1, use statement addr: 0x"
5636 << hex << pc << dec << endl;
5637 break;
5638 case uprobe2_type:
5639 clog << "probe_type == uprobe2, use statement addr: 0x"
5640 << hex << pc << dec << endl;
5641 break;
40fe32e0
SC
5642 case uprobe3_type:
5643 clog << "probe_type == uprobe3, use statement addr: 0x"
5644 << hex << pc << dec << endl;
5645 break;
5646 case kprobe1_type:
5647 clog << "probe_type == kprobe1" << endl;
5648 break;
5649 case kprobe2_type:
5650 clog << "probe_type == kprobe2" << endl;
5651 break;
aff5d390
SC
5652 default:
5653 clog << "probe_type == use_uprobe_no_dwarf, use label name: "
a794dbeb 5654 << "_stapprobe1_" << pp_mark << endl;
aff5d390
SC
5655 }
5656
c72aa911
JS
5657 switch (probe_type)
5658 {
aff5d390
SC
5659 case uprobe1_type:
5660 case uprobe2_type:
40fe32e0 5661 case uprobe3_type:
c72aa911
JS
5662 // process("executable").statement(probe_arg)
5663 derived_loc->components[i] =
9ea68eb9 5664 new probe_point::component(TOK_STATEMENT,
aff5d390 5665 new literal_number(pc, true));
c72aa911
JS
5666 break;
5667
aff5d390
SC
5668 case kprobe1_type:
5669 case kprobe2_type:
b642c901
SC
5670 // kernel.function("*getegid*")
5671 derived_loc->components[i] =
5672 new probe_point::component(TOK_FUNCTION, new literal_string("*getegid*"));
5673 if (derived_loc->components[i - 1]->functor == TOK_LIBRARY)
5674 derived_loc->components.erase (derived_loc->components.begin() + i - 1);
5675 break;
c72aa911 5676
a794dbeb 5677 default: // deprecated
c72aa911
JS
5678 // process("executable").function("*").label("_stapprobe1_MARK_NAME")
5679 derived_loc->components[i] =
5680 new probe_point::component(TOK_FUNCTION, new literal_string("*"));
5681 derived_loc->components.push_back
5682 (new probe_point::component(TOK_LABEL,
a794dbeb 5683 new literal_string("_stapprobe1_" + pp_mark)));
c72aa911
JS
5684 break;
5685 }
5686 }
5687 else if (derived_loc->components[i]->functor == TOK_PROCESS
aff5d390 5688 && have_kprobe())
63b4fd14 5689 {
c72aa911 5690 derived_loc->components[i] = new probe_point::component(TOK_KERNEL);
63b4fd14 5691 }
edce5b67 5692
c72aa911 5693 return base_probe->create_alias(derived_loc, specific_loc);
edce5b67
JS
5694}
5695
5696
20c6c071 5697void
5227f1ea 5698dwarf_builder::build(systemtap_session & sess,
7a053d3b 5699 probe * base,
20c6c071 5700 probe_point * location,
86bf665e 5701 literal_map_t const & parameters,
20c6c071
GH
5702 vector<derived_probe *> & finished_results)
5703{
b20febf3
FCE
5704 // NB: the kernel/user dwlfpp objects are long-lived.
5705 // XXX: but they should be per-session, as this builder object
5706 // may be reused if we try to cross-instrument multiple targets.
84048984 5707
7a24d422
FCE
5708 dwflpp* dw = 0;
5709
7a24d422 5710 string module_name;
ae2552da
FCE
5711 if (has_null_param (parameters, TOK_KERNEL))
5712 {
5713 dw = get_kern_dw(sess, "kernel");
5714 }
5715 else if (get_param (parameters, TOK_MODULE, module_name))
b8da0ad1 5716 {
ae2552da 5717 dw = get_kern_dw(sess, module_name);
b8da0ad1 5718 }
7a24d422 5719 else if (get_param (parameters, TOK_PROCESS, module_name))
b8da0ad1 5720 {
63b4fd14 5721 string library_name;
b642c901 5722 user_path = find_executable (module_name); // canonicalize it
63b4fd14 5723 if (get_param (parameters, TOK_LIBRARY, library_name))
b642c901
SC
5724 {
5725 module_name = find_executable (library_name, "LD_LIBRARY_PATH");
5726 user_lib = module_name;
5727 }
63b4fd14 5728 else
b642c901 5729 module_name = user_path; // canonicalize it
d0a7f5a9 5730
e34d5d13
FCE
5731 if (sess.kernel_config["CONFIG_UTRACE"] != string("y"))
5732 throw semantic_error ("process probes not available without kernel CONFIG_UTRACE");
5733
7a24d422
FCE
5734 // user-space target; we use one dwflpp instance per module name
5735 // (= program or shared library)
707bf35e 5736 dw = get_user_dw(sess, module_name);
c8959a29 5737 }
20c6c071 5738
5896cd05
MW
5739 if (sess.verbose > 3)
5740 clog << "dwarf_builder::build for " << module_name << endl;
5741
a794dbeb
FCE
5742 string dummy_mark_name; // NB: PR10245: dummy value, need not substitute - => __
5743 if (get_param(parameters, TOK_MARK, dummy_mark_name))
f28a8c28 5744 {
edce5b67
JS
5745 sdt_query sdtq(base, location, *dw, parameters, finished_results);
5746 dw->iterate_over_modules(&query_module, &sdtq);
5747 return;
7a05f484 5748 }
20c6c071 5749
8f14e444 5750 unsigned results_pre = finished_results.size();
b642c901 5751 dwarf_query q(base, location, *dw, parameters, finished_results, user_path, user_lib);
7a24d422
FCE
5752
5753 // XXX: kernel.statement.absolute is a special case that requires no
5754 // dwfl processing. This code should be in a separate builder.
7a24d422 5755 if (q.has_kernel && q.has_absolute)
37ebca01 5756 {
4baf0e53 5757 // assert guru mode for absolute probes
37ebca01
FCE
5758 if (! q.base_probe->privileged)
5759 {
edce5b67
JS
5760 throw semantic_error ("absolute statement probe in unprivileged script",
5761 q.base_probe->tok);
37ebca01
FCE
5762 }
5763
5764 // For kernel.statement(NUM).absolute probe points, we bypass
5765 // all the debuginfo stuff: We just wire up a
5766 // dwarf_derived_probe right here and now.
4baf0e53 5767 dwarf_derived_probe* p =
b8da0ad1
FCE
5768 new dwarf_derived_probe ("", "", 0, "kernel", "",
5769 q.statement_num_val, q.statement_num_val,
5770 q, 0);
37ebca01 5771 finished_results.push_back (p);
1a0dbc5a 5772 sess.unwindsym_modules.insert ("kernel");
37ebca01
FCE
5773 return;
5774 }
5775
51178501 5776 dw->iterate_over_modules(&query_module, &q);
8f14e444
FCE
5777
5778
5779 // PR11553 special processing: .return probes requested, but
5780 // some inlined function instances matched.
5781 unsigned i_n_r = q.inlined_non_returnable.size();
5782 unsigned results_post = finished_results.size();
5783 if (i_n_r > 0)
5784 {
5785 if ((results_pre == results_post) && (! sess.suppress_warnings)) // no matches; issue warning
5786 {
5787 string quicklist;
5788 for (set<string>::iterator it = q.inlined_non_returnable.begin();
5789 it != q.inlined_non_returnable.end();
5790 it++)
5791 {
5792 quicklist += " " + (*it);
5793 if (quicklist.size() > 80) // heuristic, don't make an overlong report line
5794 {
5795 quicklist += " ...";
5796 break;
5797 }
5798 }
c57ea854 5799
8f14e444
FCE
5800 sess.print_warning ("cannot probe .return of " + lex_cast(i_n_r) + " inlined function(s):" + quicklist);
5801 // There will be also a "no matches" semantic error generated.
5802 }
5803 if (sess.verbose > 1)
5804 clog << "skipped .return probe of " + lex_cast(i_n_r) + " inlined function(s)" << endl;
5805 if ((sess.verbose > 3) || (sess.verbose > 2 && results_pre == results_post)) // issue details with high verbosity
5806 {
5807 for (set<string>::iterator it = q.inlined_non_returnable.begin();
5808 it != q.inlined_non_returnable.end();
5809 it++)
5810 clog << (*it) << " ";
5811 clog << endl;
5812 }
5813 } // i_n_r > 0
5f0a03a6
JK
5814}
5815
5816symbol_table::~symbol_table()
5817{
c9efa5c9 5818 delete_map(map_by_addr);
5f0a03a6
JK
5819}
5820
5821void
2867a2a1
JS
5822symbol_table::add_symbol(const char *name, bool weak, bool descriptor,
5823 Dwarf_Addr addr, Dwarf_Addr *high_addr)
5f0a03a6 5824{
ab91b232
JK
5825#ifdef __powerpc__
5826 // Map ".sys_foo" to "sys_foo".
5827 if (name[0] == '.')
5828 name++;
5829#endif
5f0a03a6
JK
5830 func_info *fi = new func_info();
5831 fi->addr = addr;
5832 fi->name = name;
ab91b232 5833 fi->weak = weak;
2867a2a1 5834 fi->descriptor = descriptor;
5f0a03a6
JK
5835 map_by_name[fi->name] = fi;
5836 // TODO: Use a multimap in case there are multiple static
5837 // functions with the same name?
1c6b77e5 5838 map_by_addr.insert(make_pair(addr, fi));
5f0a03a6
JK
5839}
5840
5841enum info_status
5842symbol_table::read_symbols(FILE *f, const string& path)
5843{
5844 // Based on do_kernel_symbols() in runtime/staprun/symbols.c
5845 int ret;
2e67a43b
TM
5846 char *name = 0;
5847 char *mod = 0;
5f0a03a6
JK
5848 char type;
5849 unsigned long long addr;
5850 Dwarf_Addr high_addr = 0;
5851 int line = 0;
5852
5853 // %as (non-POSIX) mallocs space for the string and stores its address.
5854 while ((ret = fscanf(f, "%llx %c %as [%as", &addr, &type, &name, &mod)) > 0)
5855 {
2e67a43b
TM
5856 auto_free free_name(name);
5857 auto_free free_mod(mod);
5f0a03a6
JK
5858 line++;
5859 if (ret < 3)
5860 {
41c262f3 5861 cerr << "Symbol table error: Line "
5f0a03a6
JK
5862 << line
5863 << " of symbol list from "
5864 << path
5865 << " is not in correct format: address type name [module]";
5866 // Caller should delete symbol_table object.
5867 return info_absent;
5868 }
2e67a43b 5869 else if (ret > 3)
5f0a03a6
JK
5870 {
5871 // Modules are loaded above the kernel, so if we're getting
5872 // modules, we're done.
2e67a43b 5873 break;
5f0a03a6 5874 }
ab91b232 5875 if (type == 'T' || type == 't' || type == 'W')
2867a2a1 5876 add_symbol(name, (type == 'W'), false, (Dwarf_Addr) addr, &high_addr);
5f0a03a6
JK
5877 }
5878
1c6b77e5 5879 if (map_by_addr.size() < 1)
5f0a03a6
JK
5880 {
5881 cerr << "Symbol table error: "
5882 << path << " contains no function symbols." << endl;
5883 return info_absent;
5884 }
5885 return info_present;
5886}
5887
5888// NB: This currently unused. We use get_from_elf() instead because
5889// that gives us raw addresses -- which we need for modules -- whereas
5890// nm provides the address relative to the beginning of the section.
5891enum info_status
83ca3872
MW
5892symbol_table::read_from_elf_file(const string &path,
5893 const systemtap_session &sess)
5f0a03a6
JK
5894{
5895 FILE *f;
5896 string cmd = string("/usr/bin/nm -n --defined-only ") + path;
5897 f = popen(cmd.c_str(), "r");
5898 if (!f)
5899 {
5900 // nm failures are detected by pclose, not popen.
5901 cerr << "Internal error reading symbol table from "
5902 << path << " -- " << strerror (errno);
5903 return info_absent;
5904 }
5905 enum info_status status = read_symbols(f, path);
5906 if (pclose(f) != 0)
5907 {
83ca3872 5908 if (status == info_present && ! sess.suppress_warnings)
5f0a03a6
JK
5909 cerr << "Warning: nm cannot read symbol table from " << path;
5910 return info_absent;
5911 }
5912 return status;
5913}
5914
5915enum info_status
83ca3872
MW
5916symbol_table::read_from_text_file(const string& path,
5917 const systemtap_session &sess)
5f0a03a6
JK
5918{
5919 FILE *f = fopen(path.c_str(), "r");
5920 if (!f)
5921 {
83ca3872
MW
5922 if (! sess.suppress_warnings)
5923 cerr << "Warning: cannot read symbol table from "
5924 << path << " -- " << strerror (errno);
5f0a03a6
JK
5925 return info_absent;
5926 }
5927 enum info_status status = read_symbols(f, path);
5928 (void) fclose(f);
5929 return status;
5930}
5931
46f7b6be
JK
5932void
5933symbol_table::prepare_section_rejection(Dwfl_Module *mod)
5934{
5935#ifdef __powerpc__
5936 /*
5937 * The .opd section contains function descriptors that can look
5938 * just like function entry points. For example, there's a function
5939 * descriptor called "do_exit" that links to the entry point ".do_exit".
5940 * Reject all symbols in .opd.
5941 */
5942 opd_section = SHN_UNDEF;
5943 Dwarf_Addr bias;
5944 Elf* elf = (dwarf_getelf (dwfl_module_getdwarf (mod, &bias))
5945 ?: dwfl_module_getelf (mod, &bias));
5946 Elf_Scn* scn = 0;
5947 size_t shstrndx;
5948
5949 if (!elf)
5950 return;
fcc30d6d 5951 if (elf_getshdrstrndx (elf, &shstrndx) != 0)
46f7b6be
JK
5952 return;
5953 while ((scn = elf_nextscn(elf, scn)) != NULL)
5954 {
5955 GElf_Shdr shdr_mem;
5956 GElf_Shdr *shdr = gelf_getshdr(scn, &shdr_mem);
5957 if (!shdr)
5958 continue;
5959 const char *name = elf_strptr(elf, shstrndx, shdr->sh_name);
5960 if (!strcmp(name, ".opd"))
5961 {
5962 opd_section = elf_ndxscn(scn);
5963 return;
5964 }
5965 }
5966#endif
5967}
5968
5969bool
5970symbol_table::reject_section(GElf_Word section)
5971{
5972 if (section == SHN_UNDEF)
5973 return true;
5974#ifdef __powerpc__
5975 if (section == opd_section)
5976 return true;
5977#endif
5978 return false;
5979}
5980
5f0a03a6
JK
5981enum info_status
5982symbol_table::get_from_elf()
5983{
5984 Dwarf_Addr high_addr = 0;
5985 Dwfl_Module *mod = mod_info->mod;
5986 int syments = dwfl_module_getsymtab(mod);
5987 assert(syments);
46f7b6be 5988 prepare_section_rejection(mod);
5f0a03a6
JK
5989 for (int i = 1; i < syments; ++i)
5990 {
5991 GElf_Sym sym;
ab91b232
JK
5992 GElf_Word section;
5993 const char *name = dwfl_module_getsym(mod, i, &sym, &section);
2867a2a1 5994 if (name && GELF_ST_TYPE(sym.st_info) == STT_FUNC)
ab91b232 5995 add_symbol(name, (GELF_ST_BIND(sym.st_info) == STB_WEAK),
2867a2a1 5996 reject_section(section), sym.st_value, &high_addr);
5f0a03a6
JK
5997 }
5998 return info_present;
5999}
6000
5f0a03a6
JK
6001func_info *
6002symbol_table::get_func_containing_address(Dwarf_Addr addr)
6003{
1c6b77e5
JS
6004 iterator_t iter = map_by_addr.upper_bound(addr);
6005 if (iter == map_by_addr.begin())
5f0a03a6 6006 return NULL;
2e67a43b 6007 else
1c6b77e5 6008 return (--iter)->second;
5f0a03a6
JK
6009}
6010
6011func_info *
6012symbol_table::lookup_symbol(const string& name)
6013{
6014 map<string, func_info*>::iterator i = map_by_name.find(name);
6015 if (i == map_by_name.end())
6016 return NULL;
6017 return i->second;
6018}
6019
6020Dwarf_Addr
6021symbol_table::lookup_symbol_address(const string& name)
6022{
6023 func_info *fi = lookup_symbol(name);
6024 if (fi)
6025 return fi->addr;
6026 return 0;
6027}
6028
ab91b232
JK
6029// This is the kernel symbol table. The kernel macro cond_syscall creates
6030// a weak symbol for each system call and maps it to sys_ni_syscall.
6031// For system calls not implemented elsewhere, this weak symbol shows up
6032// in the kernel symbol table. Following the precedent of dwarfful stap,
6033// we refuse to consider such symbols. Here we delete them from our
6034// symbol table.
6035// TODO: Consider generalizing this and/or making it part of blacklist
6036// processing.
6037void
6038symbol_table::purge_syscall_stubs()
6039{
6040 Dwarf_Addr stub_addr = lookup_symbol_address("sys_ni_syscall");
6041 if (stub_addr == 0)
6042 return;
1c6b77e5 6043 range_t purge_range = map_by_addr.equal_range(stub_addr);
2e67a43b
TM
6044 for (iterator_t iter = purge_range.first;
6045 iter != purge_range.second;
1c6b77e5 6046 )
ab91b232 6047 {
1c6b77e5 6048 func_info *fi = iter->second;
2e67a43b 6049 if (fi->weak && fi->name != "sys_ni_syscall")
ab91b232 6050 {
2e67a43b 6051 map_by_name.erase(fi->name);
1c6b77e5 6052 map_by_addr.erase(iter++);
2e67a43b 6053 delete fi;
2e67a43b 6054 }
1c6b77e5
JS
6055 else
6056 iter++;
ab91b232
JK
6057 }
6058}
6059
5f0a03a6
JK
6060void
6061module_info::get_symtab(dwarf_query *q)
6062{
6063 systemtap_session &sess = q->sess;
6064
1c6b77e5
JS
6065 if (symtab_status != info_unknown)
6066 return;
6067
5f0a03a6
JK
6068 sym_table = new symbol_table(this);
6069 if (!elf_path.empty())
6070 {
83ca3872
MW
6071 if (name == TOK_KERNEL && !sess.kernel_symtab_path.empty()
6072 && ! sess.suppress_warnings)
5f0a03a6
JK
6073 cerr << "Warning: reading symbol table from "
6074 << elf_path
6075 << " -- ignoring "
6076 << sess.kernel_symtab_path
83ca3872 6077 << endl;
5f0a03a6
JK
6078 symtab_status = sym_table->get_from_elf();
6079 }
6080 else
6081 {
6082 assert(name == TOK_KERNEL);
6083 if (sess.kernel_symtab_path.empty())
6084 {
6085 symtab_status = info_absent;
6086 cerr << "Error: Cannot find vmlinux."
6087 << " Consider using --kmap instead of --kelf."
6088 << endl;;
6089 }
6090 else
6091 {
6092 symtab_status =
83ca3872 6093 sym_table->read_from_text_file(sess.kernel_symtab_path, sess);
5f0a03a6
JK
6094 if (symtab_status == info_present)
6095 {
6096 sess.sym_kprobes_text_start =
6097 sym_table->lookup_symbol_address("__kprobes_text_start");
6098 sess.sym_kprobes_text_end =
6099 sym_table->lookup_symbol_address("__kprobes_text_end");
6100 sess.sym_stext = sym_table->lookup_symbol_address("_stext");
5f0a03a6
JK
6101 }
6102 }
6103 }
6104 if (symtab_status == info_absent)
6105 {
6106 delete sym_table;
6107 sym_table = NULL;
6108 return;
6109 }
6110
ab91b232
JK
6111 if (name == TOK_KERNEL)
6112 sym_table->purge_syscall_stubs();
5f0a03a6
JK
6113}
6114
1c6b77e5
JS
6115// update_symtab reconciles data between the elf symbol table and the dwarf
6116// function enumeration. It updates the symbol table entries with the dwarf
6117// die that describes the function, which also signals to query_module_symtab
6118// that a statement probe isn't needed. In return, it also adds aliases to the
6119// function table for names that share the same addr/die.
6120void
6121module_info::update_symtab(cu_function_cache_t *funcs)
6122{
6123 if (!sym_table)
6124 return;
6125
6126 cu_function_cache_t new_funcs;
6127
6128 for (cu_function_cache_t::iterator func = funcs->begin();
6129 func != funcs->end(); func++)
6130 {
6131 // optimization: inlines will never be in the symbol table
6132 if (dwarf_func_inline(&func->second) != 0)
6133 continue;
6134
1ffb8bd1
JS
6135 // XXX We may want to make additional efforts to match mangled elf names
6136 // to dwarf too. MIPS_linkage_name can help, but that's sometimes
6137 // missing, so we may also need to try matching by address. See also the
6138 // notes about _Z in dwflpp::iterate_over_functions().
6139
1c6b77e5
JS
6140 func_info *fi = sym_table->lookup_symbol(func->first);
6141 if (!fi)
6142 continue;
6143
6144 // iterate over all functions at the same address
6145 symbol_table::range_t er = sym_table->map_by_addr.equal_range(fi->addr);
6146 for (symbol_table::iterator_t it = er.first; it != er.second; ++it)
6147 {
6148 // update this function with the dwarf die
6149 it->second->die = func->second;
6150
6151 // if this function is a new alias, then
6152 // save it to merge into the function cache
6153 if (it->second != fi)
b7478964 6154 new_funcs.insert(make_pair(it->second->name, it->second->die));
1c6b77e5
JS
6155 }
6156 }
6157
6158 // add all discovered aliases back into the function cache
6159 // NB: this won't replace any names that dwarf may have already found
6160 funcs->insert(new_funcs.begin(), new_funcs.end());
6161}
6162
5f0a03a6
JK
6163module_info::~module_info()
6164{
6165 if (sym_table)
6166 delete sym_table;
b55bc428
FCE
6167}
6168
935447c8 6169// ------------------------------------------------------------------------
888af770 6170// user-space probes
935447c8
DS
6171// ------------------------------------------------------------------------
6172
935447c8 6173
888af770 6174struct uprobe_derived_probe_group: public generic_dpg<uprobe_derived_probe>
935447c8 6175{
89ba3085
FCE
6176private:
6177 string make_pbm_key (uprobe_derived_probe* p) {
6178 return p->module + "|" + p->section + "|" + lex_cast(p->pid);
6179 }
6180
935447c8 6181public:
888af770 6182 void emit_module_decls (systemtap_session& s);
935447c8
DS
6183 void emit_module_init (systemtap_session& s);
6184 void emit_module_exit (systemtap_session& s);
6185};
6186
6187
888af770
FCE
6188void
6189uprobe_derived_probe::join_group (systemtap_session& s)
6190{
6191 if (! s.uprobe_derived_probes)
6192 s.uprobe_derived_probes = new uprobe_derived_probe_group ();
6193 s.uprobe_derived_probes->enroll (this);
93646f4d 6194 enable_task_finder(s);
a96d1db0 6195
8a03658e
JS
6196 // Ask buildrun.cxx to build extra module if needed, and
6197 // signal staprun to load that module
6198 s.need_uprobes = true;
a96d1db0
DN
6199}
6200
888af770 6201
c0f84e7b
SC
6202void
6203uprobe_derived_probe::getargs(std::list<std::string> &arg_set) const
6204{
6205 dwarf_derived_probe::getargs(arg_set);
6206 arg_set.insert(arg_set.end(), args.begin(), args.end());
6207}
6208
6209
6210void
6211uprobe_derived_probe::saveargs(int nargs)
6212{
6213 for (int i = 1; i <= nargs; i++)
6214 args.push_back("$arg" + lex_cast (i) + ":long");
6215}
6216
6217
2865d17a
DB
6218void
6219uprobe_derived_probe::emit_unprivileged_assertion (translator_output* o)
6220{
6221 // These probes are allowed for unprivileged users, but only in the
6222 // context of processes which they own.
6223 emit_process_owner_assertion (o);
6224}
6225
6226
888af770 6227struct uprobe_builder: public derived_probe_builder
a96d1db0 6228{
888af770 6229 uprobe_builder() {}
a96d1db0
DN
6230 virtual void build(systemtap_session & sess,
6231 probe * base,
6232 probe_point * location,
86bf665e 6233 literal_map_t const & parameters,
a96d1db0
DN
6234 vector<derived_probe *> & finished_results)
6235 {
888af770 6236 int64_t process, address;
a96d1db0 6237
888af770 6238 bool b1 = get_param (parameters, TOK_PROCESS, process);
ced347a9 6239 (void) b1;
888af770 6240 bool b2 = get_param (parameters, TOK_STATEMENT, address);
ced347a9 6241 (void) b2;
888af770
FCE
6242 bool rr = has_null_param (parameters, TOK_RETURN);
6243 assert (b1 && b2); // by pattern_root construction
a96d1db0 6244
0973d815 6245 finished_results.push_back(new uprobe_derived_probe(base, location, process, address, rr));
a96d1db0
DN
6246 }
6247};
6248
6249
6250void
775d51e5 6251uprobe_derived_probe_group::emit_module_decls (systemtap_session& s)
a96d1db0 6252{
888af770 6253 if (probes.empty()) return;
775d51e5 6254 s.op->newline() << "/* ---- user probes ---- */";
471fca5e
TM
6255 // If uprobes isn't in the kernel, pull it in from the runtime.
6256
6257 s.op->newline() << "#if defined(CONFIG_UPROBES) || defined(CONFIG_UPROBES_MODULE)";
6258 s.op->newline() << "#include <linux/uprobes.h>";
6259 s.op->newline() << "#else";
6260 s.op->newline() << "#include \"uprobes/uprobes.h\"";
6261 s.op->newline() << "#endif";
6262 s.op->newline() << "#ifndef UPROBES_API_VERSION";
6263 s.op->newline() << "#define UPROBES_API_VERSION 1";
6264 s.op->newline() << "#endif";
a96d1db0 6265
43241c44
FCE
6266 // We'll probably need at least this many:
6267 unsigned minuprobes = probes.size();
6268 // .. but we don't want so many that .bss is inflated (PR10507):
6269 unsigned uprobesize = 64;
6270 unsigned maxuprobesmem = 10*1024*1024; // 10 MB
6271 unsigned maxuprobes = maxuprobesmem / uprobesize;
6272
aaf7ffe8
FCE
6273 // Let's choose a value on the geometric middle. This should end up
6274 // between minuprobes and maxuprobes. It's OK if this number turns
6275 // out to be < minuprobes or > maxuprobes. At worst, we get a
6276 // run-time error of one kind (too few: missed uprobe registrations)
6277 // or another (too many: vmalloc errors at module load time).
6278 unsigned default_maxuprobes = (unsigned)sqrt((double)minuprobes * (double)maxuprobes);
43241c44 6279
6d0f3f0c 6280 s.op->newline() << "#ifndef MAXUPROBES";
43241c44 6281 s.op->newline() << "#define MAXUPROBES " << default_maxuprobes;
6d0f3f0c 6282 s.op->newline() << "#endif";
a96d1db0 6283
cc52276b
WC
6284 // Forward decls
6285 s.op->newline() << "#include \"uprobes-common.h\"";
6286
5e112f92
FCE
6287 // In .bss, the shared pool of uprobe/uretprobe structs. These are
6288 // too big to embed in the initialized .data stap_uprobe_spec array.
cc52276b
WC
6289 // XXX: consider a slab cache or somesuch for stap_uprobes
6290 s.op->newline() << "static struct stap_uprobe stap_uprobes [MAXUPROBES];";
5e112f92 6291 s.op->newline() << "DEFINE_MUTEX(stap_uprobes_lock);"; // protects against concurrent registration/unregistration
a96d1db0 6292
89ba3085
FCE
6293 s.op->assert_0_indent();
6294
89ba3085
FCE
6295 // Assign task-finder numbers as we build up the stap_uprobe_tf table.
6296 // This means we process probes[] in two passes.
6297 map <string,unsigned> module_index;
6298 unsigned module_index_ctr = 0;
6299
cc52276b
WC
6300 // not const since embedded task_finder_target struct changes
6301 s.op->newline() << "static struct stap_uprobe_tf stap_uprobe_finders[] = {";
89ba3085
FCE
6302 s.op->indent(1);
6303 for (unsigned i=0; i<probes.size(); i++)
6304 {
6305 uprobe_derived_probe *p = probes[i];
6306 string pbmkey = make_pbm_key (p);
6307 if (module_index.find (pbmkey) == module_index.end())
6308 {
6309 module_index[pbmkey] = module_index_ctr++;
6310
6311 s.op->newline() << "{";
6312 // NB: it's essential that make_pbm_key() use all of and
6313 // only the same fields as we're about to emit.
6314 s.op->line() << " .finder={";
6315 if (p->pid != 0)
6316 s.op->line() << " .pid=" << p->pid;
6317 else if (p->section == ".absolute") // proxy for ET_EXEC -> exec()'d program
6318 {
6319 s.op->line() << " .procname=" << lex_cast_qstring(p->module) << ",";
6320 s.op->line() << " .callback=&stap_uprobe_process_found,";
6321 }
aff5d390 6322 if (p->section != ".absolute") // ET_DYN
89ba3085 6323 {
63b4fd14
SC
6324 if (p->has_library && p->sdt_semaphore_addr != 0)
6325 s.op->line() << " .procname=\"" << p->path << "\", ";
89ba3085
FCE
6326 s.op->line() << " .mmap_callback=&stap_uprobe_mmap_found, ";
6327 s.op->line() << " .munmap_callback=&stap_uprobe_munmap_found, ";
19d91f6c 6328 s.op->line() << " .callback=&stap_uprobe_process_munmap,";
89ba3085
FCE
6329 }
6330
6331 s.op->line() << " },";
6332 s.op->line() << " .pathname=" << lex_cast_qstring(p->module) << ", ";
6333 s.op->line() << " },";
6334 }
c57ea854 6335 else
89ba3085
FCE
6336 ; // skip it in this pass, already have a suitable stap_uprobe_tf slot for it.
6337 }
6338 s.op->newline(-1) << "};";
6339
6340 s.op->assert_0_indent();
6341
cc52276b
WC
6342 // NB: read-only structure
6343 s.op->newline() << "static const struct stap_uprobe_spec stap_uprobe_specs [] = {";
a96d1db0 6344 s.op->indent(1);
888af770
FCE
6345 for (unsigned i =0; i<probes.size(); i++)
6346 {
6347 uprobe_derived_probe* p = probes[i];
6348 s.op->newline() << "{";
89ba3085
FCE
6349 string key = make_pbm_key (p);
6350 unsigned value = module_index[key];
759e1d76
FCE
6351 if (value != 0)
6352 s.op->line() << " .tfi=" << value << ",";
6b66b9f7 6353 s.op->line() << " .address=(unsigned long)0x" << hex << p->addr << dec << "ULL,";
faea5e16 6354 s.op->line() << " .probe=" << common_probe_init (p) << ",";
4ddb6dd0 6355
038c38c6 6356 if (p->sdt_semaphore_addr != 0)
63b4fd14 6357 s.op->line() << " .sdt_sem_offset=(unsigned long)0x"
038c38c6 6358 << hex << p->sdt_semaphore_addr << dec << "ULL,";
4ddb6dd0
JS
6359
6360 if (p->has_return)
6361 s.op->line() << " .return_p=1,";
888af770
FCE
6362 s.op->line() << " },";
6363 }
6364 s.op->newline(-1) << "};";
a96d1db0 6365
89ba3085
FCE
6366 s.op->assert_0_indent();
6367
48e685da 6368 s.op->newline() << "static void enter_uprobe_probe (struct uprobe *inst, struct pt_regs *regs) {";
888af770 6369 s.op->newline(1) << "struct stap_uprobe *sup = container_of(inst, struct stap_uprobe, up);";
89ba3085 6370 s.op->newline() << "const struct stap_uprobe_spec *sups = &stap_uprobe_specs [sup->spec_index];";
faea5e16 6371 common_probe_entryfn_prologue (s.op, "STAP_SESSION_RUNNING", "sups->probe");
6d0f3f0c
FCE
6372 s.op->newline() << "if (sup->spec_index < 0 ||"
6373 << "sup->spec_index >= " << probes.size() << ") return;"; // XXX: should not happen
a96d1db0 6374 s.op->newline() << "c->regs = regs;";
fd9187da 6375 s.op->newline() << "c->ri = GET_PC_URETPROBE_NONE;";
97cd9334 6376 s.op->newline() << "c->regflags |= _STP_REGS_USER_FLAG;";
6415ddde
MW
6377
6378 // Make it look like the IP is set as it would in the actual user
6379 // task when calling real probe handler. Reset IP regs on return, so
6380 // we don't confuse uprobes. PR10458
6381 s.op->newline() << "{";
6382 s.op->indent(1);
6383 s.op->newline() << "unsigned long uprobes_ip = REG_IP(c->regs);";
259d54c0 6384 s.op->newline() << "SET_REG_IP(regs, inst->vaddr);";
26e63673 6385 s.op->newline() << "(*sups->probe->ph) (c);";
259d54c0 6386 s.op->newline() << "SET_REG_IP(regs, uprobes_ip);";
6415ddde
MW
6387 s.op->newline(-1) << "}";
6388
a96d1db0 6389 common_probe_entryfn_epilogue (s.op);
888af770 6390 s.op->newline(-1) << "}";
a96d1db0 6391
48e685da 6392 s.op->newline() << "static void enter_uretprobe_probe (struct uretprobe_instance *inst, struct pt_regs *regs) {";
888af770 6393 s.op->newline(1) << "struct stap_uprobe *sup = container_of(inst->rp, struct stap_uprobe, urp);";
89ba3085 6394 s.op->newline() << "const struct stap_uprobe_spec *sups = &stap_uprobe_specs [sup->spec_index];";
faea5e16 6395 common_probe_entryfn_prologue (s.op, "STAP_SESSION_RUNNING", "sups->probe");
5e562a69 6396 s.op->newline() << "c->ri = inst;";
6d0f3f0c
FCE
6397 s.op->newline() << "if (sup->spec_index < 0 ||"
6398 << "sup->spec_index >= " << probes.size() << ") return;"; // XXX: should not happen
888af770
FCE
6399 // XXX: kretprobes saves "c->pi = inst;" too
6400 s.op->newline() << "c->regs = regs;";
97cd9334 6401 s.op->newline() << "c->regflags |= _STP_REGS_USER_FLAG;";
6415ddde
MW
6402
6403 // Make it look like the IP is set as it would in the actual user
6404 // task when calling real probe handler. Reset IP regs on return, so
6405 // we don't confuse uprobes. PR10458
6406 s.op->newline() << "{";
6407 s.op->indent(1);
6408 s.op->newline() << "unsigned long uprobes_ip = REG_IP(c->regs);";
5e562a69 6409 s.op->newline() << "SET_REG_IP(regs, inst->ret_addr);";
26e63673 6410 s.op->newline() << "(*sups->probe->ph) (c);";
259d54c0 6411 s.op->newline() << "SET_REG_IP(regs, uprobes_ip);";
6415ddde
MW
6412 s.op->newline(-1) << "}";
6413
888af770 6414 common_probe_entryfn_epilogue (s.op);
a96d1db0
DN
6415 s.op->newline(-1) << "}";
6416
89ba3085 6417 s.op->newline();
cc52276b 6418 s.op->newline() << "#include \"uprobes-common.c\"";
6d0f3f0c 6419 s.op->newline();
888af770 6420}
935447c8
DS
6421
6422
888af770
FCE
6423void
6424uprobe_derived_probe_group::emit_module_init (systemtap_session& s)
935447c8 6425{
888af770 6426 if (probes.empty()) return;
935447c8 6427
5e112f92 6428 s.op->newline() << "/* ---- user probes ---- */";
935447c8 6429
01b05e2e 6430 s.op->newline() << "for (j=0; j<MAXUPROBES; j++) {";
5e112f92
FCE
6431 s.op->newline(1) << "struct stap_uprobe *sup = & stap_uprobes[j];";
6432 s.op->newline() << "sup->spec_index = -1;"; // free slot
80b4ad8b
FCE
6433 // NB: we assume the rest of the struct (specificaly, sup->up) is
6434 // initialized to zero. This is so that we can use
6435 // sup->up->kdata = NULL for "really free!" PR 6829.
5e112f92
FCE
6436 s.op->newline(-1) << "}";
6437 s.op->newline() << "mutex_init (& stap_uprobes_lock);";
935447c8 6438
89ba3085
FCE
6439 // Set up the task_finders
6440 s.op->newline() << "for (i=0; i<sizeof(stap_uprobe_finders)/sizeof(stap_uprobe_finders[0]); i++) {";
6441 s.op->newline(1) << "struct stap_uprobe_tf *stf = & stap_uprobe_finders[i];";
c57ea854 6442 s.op->newline() << "probe_point = stf->pathname;"; // for error messages; XXX: would prefer pp() or something better
89ba3085 6443 s.op->newline() << "rc = stap_register_task_finder_target (& stf->finder);";
935447c8 6444
5e112f92
FCE
6445 // NB: if (rc), there is no need (XXX: nor any way) to clean up any
6446 // finders already registered, since mere registration does not
6447 // cause any utrace or memory allocation actions. That happens only
6448 // later, once the task finder engine starts running. So, for a
6449 // partial initialization requiring unwind, we need do nothing.
6450 s.op->newline() << "if (rc) break;";
a7a68293 6451
888af770
FCE
6452 s.op->newline(-1) << "}";
6453}
d0ea46ce 6454
d0a7f5a9 6455
888af770
FCE
6456void
6457uprobe_derived_probe_group::emit_module_exit (systemtap_session& s)
6458{
6459 if (probes.empty()) return;
6460 s.op->newline() << "/* ---- user probes ---- */";
e56e51c9 6461
6d0f3f0c
FCE
6462 // NB: there is no stap_unregister_task_finder_target call;
6463 // important stuff like utrace cleanups are done by
d41d451c
FCE
6464 // __stp_task_finder_cleanup() via stap_stop_task_finder().
6465 //
6466 // This function blocks until all callbacks are completed, so there
6467 // is supposed to be no possibility of any registration-related code starting
6468 // to run in parallel with our shutdown here. So we don't need to protect the
6469 // stap_uprobes[] array with the mutex.
d0a7f5a9 6470
01b05e2e 6471 s.op->newline() << "for (j=0; j<MAXUPROBES; j++) {";
5e112f92 6472 s.op->newline(1) << "struct stap_uprobe *sup = & stap_uprobes[j];";
89ba3085 6473 s.op->newline() << "const struct stap_uprobe_spec *sups = &stap_uprobe_specs [sup->spec_index];";
6d0f3f0c 6474 s.op->newline() << "if (sup->spec_index < 0) continue;"; // free slot
3568f1dd 6475
8faa1fc5 6476 // PR10655: decrement that ENABLED semaphore
c116c31b 6477 s.op->newline() << "if (sup->sdt_sem_address) {";
8faa1fc5
FCE
6478 s.op->newline(1) << "unsigned short sdt_semaphore;"; // NB: fixed size
6479 s.op->newline() << "pid_t pid = (sups->return_p ? sup->urp.u.pid : sup->up.pid);";
6480 s.op->newline() << "struct task_struct *tsk;";
6481 s.op->newline() << "rcu_read_lock();";
6846cfc8 6482
86229a55
DS
6483 // Do a pid->task_struct* lookup. For 2.6.24+, this code assumes
6484 // that the pid is always in the global namespace, not in any
6485 // private namespace.
8faa1fc5 6486 s.op->newline() << "#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,24)";
86229a55
DS
6487 // We'd like to call find_task_by_pid_ns() here, but it isn't
6488 // exported. So, we call what it calls...
6489 s.op->newline() << " tsk = pid_task(find_pid_ns(pid, &init_pid_ns), PIDTYPE_PID);";
8faa1fc5
FCE
6490 s.op->newline() << "#else";
6491 s.op->newline() << " tsk = find_task_by_pid (pid);";
6492 s.op->newline() << "#endif /* 2.6.24 */";
8faa1fc5
FCE
6493
6494 s.op->newline() << "if (tsk) {"; // just in case the thing exited while we weren't watching
3c5b8e2b 6495 s.op->newline(1) << "if (__access_process_vm_noflush(tsk, sup->sdt_sem_address, &sdt_semaphore, sizeof(sdt_semaphore), 0)) {";
63b4fd14 6496 s.op->newline(1) << "sdt_semaphore --;";
903b9fcd 6497 s.op->newline() << "#ifdef DEBUG_UPROBES";
c116c31b 6498 s.op->newline() << "_stp_dbug (__FUNCTION__,__LINE__, \"-semaphore %#x @ %#lx\\n\", sdt_semaphore, sup->sdt_sem_address);";
903b9fcd 6499 s.op->newline() << "#endif";
3c5b8e2b 6500 s.op->newline() << "__access_process_vm_noflush(tsk, sup->sdt_sem_address, &sdt_semaphore, sizeof(sdt_semaphore), 1);";
93c84191 6501 s.op->newline(-1) << "}";
8faa1fc5
FCE
6502 // XXX: need to analyze possibility of race condition
6503 s.op->newline(-1) << "}";
6504 s.op->newline() << "rcu_read_unlock();";
6505 s.op->newline(-1) << "}";
6846cfc8 6506
3568f1dd
FCE
6507 s.op->newline() << "if (sups->return_p) {";
6508 s.op->newline(1) << "#ifdef DEBUG_UPROBES";
89ba3085 6509 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 6510 s.op->newline() << "#endif";
80b4ad8b
FCE
6511 // NB: PR6829 does not change that we still need to unregister at
6512 // *this* time -- when the script as a whole exits.
3568f1dd
FCE
6513 s.op->newline() << "unregister_uretprobe (& sup->urp);";
6514 s.op->newline(-1) << "} else {";
6515 s.op->newline(1) << "#ifdef DEBUG_UPROBES";
8faa1fc5 6516 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
6517 s.op->newline() << "#endif";
6518 s.op->newline() << "unregister_uprobe (& sup->up);";
6519 s.op->newline(-1) << "}";
935447c8 6520
6d0f3f0c 6521 s.op->newline() << "sup->spec_index = -1;";
935447c8 6522
3568f1dd
FCE
6523 // XXX: uprobe missed counts?
6524
6d0f3f0c 6525 s.op->newline(-1) << "}";
935447c8 6526
5e112f92 6527 s.op->newline() << "mutex_destroy (& stap_uprobes_lock);";
935447c8
DS
6528}
6529
e6fe60e7
AM
6530// ------------------------------------------------------------------------
6531// Kprobe derived probes
6532// ------------------------------------------------------------------------
6533
4627ed58 6534static const string TOK_KPROBE("kprobe");
935447c8 6535
bae55db9 6536struct kprobe_derived_probe: public derived_probe
d0ea46ce 6537{
bae55db9
JS
6538 kprobe_derived_probe (probe *base,
6539 probe_point *location,
6540 const string& name,
6541 int64_t stmt_addr,
6542 bool has_return,
6543 bool has_statement,
6544 bool has_maxactive,
b642c901
SC
6545 bool has_path,
6546 bool has_library,
6547 long maxactive_val,
6548 const string& path,
6549 const string& library
bae55db9
JS
6550 );
6551 string symbol_name;
6552 Dwarf_Addr addr;
6553 bool has_return;
6554 bool has_statement;
6555 bool has_maxactive;
b642c901
SC
6556 bool has_path;
6557 bool has_library;
bae55db9 6558 long maxactive_val;
b642c901
SC
6559 string path;
6560 string library;
bae55db9
JS
6561 bool access_var;
6562 void printsig (std::ostream &o) const;
6563 void join_group (systemtap_session& s);
6564};
d0ea46ce 6565
bae55db9
JS
6566struct kprobe_derived_probe_group: public derived_probe_group
6567{
6568private:
6569 multimap<string,kprobe_derived_probe*> probes_by_module;
6570 typedef multimap<string,kprobe_derived_probe*>::iterator p_b_m_iterator;
d0ea46ce 6571
bae55db9
JS
6572public:
6573 void enroll (kprobe_derived_probe* probe);
6574 void emit_module_decls (systemtap_session& s);
6575 void emit_module_init (systemtap_session& s);
6576 void emit_module_exit (systemtap_session& s);
6577};
d0ea46ce 6578
e6fe60e7
AM
6579kprobe_derived_probe::kprobe_derived_probe (probe *base,
6580 probe_point *location,
b6371390 6581 const string& name,
e6fe60e7 6582 int64_t stmt_addr,
b6371390
JS
6583 bool has_return,
6584 bool has_statement,
6585 bool has_maxactive,
b642c901
SC
6586 bool has_path,
6587 bool has_library,
6588 long maxactive_val,
6589 const string& path,
6590 const string& library
b6371390 6591 ):
4c5d1300 6592 derived_probe (base, location, true /* .components soon rewritten */ ),
e6fe60e7 6593 symbol_name (name), addr (stmt_addr),
b6371390 6594 has_return (has_return), has_statement (has_statement),
b642c901
SC
6595 has_maxactive (has_maxactive), has_path (has_path),
6596 has_library (has_library),
6597 maxactive_val (maxactive_val),
6598 path (path), library (library)
e6fe60e7
AM
6599{
6600 this->tok = base->tok;
6601 this->access_var = false;
d0ea46ce 6602
e6fe60e7
AM
6603#ifndef USHRT_MAX
6604#define USHRT_MAX 32767
6605#endif
d0ea46ce 6606
46856d8d
JS
6607 // Expansion of $target variables in the probe body produces an error during
6608 // translate phase, since we're not using debuginfo
d0ea46ce 6609
e6fe60e7 6610 vector<probe_point::component*> comps;
46856d8d 6611 comps.push_back (new probe_point::component(TOK_KPROBE));
e6fe60e7 6612
46856d8d
JS
6613 if (has_statement)
6614 {
9ea68eb9
JS
6615 comps.push_back (new probe_point::component(TOK_STATEMENT,
6616 new literal_number(addr, true)));
46856d8d
JS
6617 comps.push_back (new probe_point::component(TOK_ABSOLUTE));
6618 }
6619 else
6620 {
6621 size_t pos = name.find(':');
6622 if (pos != string::npos)
d0ea46ce 6623 {
46856d8d
JS
6624 string module = name.substr(0, pos);
6625 string function = name.substr(pos + 1);
6626 comps.push_back (new probe_point::component(TOK_MODULE, new literal_string(module)));
6627 comps.push_back (new probe_point::component(TOK_FUNCTION, new literal_string(function)));
6628 }
6629 else
6630 comps.push_back (new probe_point::component(TOK_FUNCTION, new literal_string(name)));
46856d8d 6631 }
d0ea46ce 6632
b6371390
JS
6633 if (has_return)
6634 comps.push_back (new probe_point::component(TOK_RETURN));
6635 if (has_maxactive)
6636 comps.push_back (new probe_point::component(TOK_MAXACTIVE, new literal_number(maxactive_val)));
d0ea46ce 6637
e6fe60e7
AM
6638 this->sole_location()->components = comps;
6639}
d0ea46ce 6640
e6fe60e7
AM
6641void kprobe_derived_probe::printsig (ostream& o) const
6642{
6643 sole_location()->print (o);
6644 o << " /* " << " name = " << symbol_name << "*/";
6645 printsig_nested (o);
6646}
d0ea46ce 6647
e6fe60e7
AM
6648void kprobe_derived_probe::join_group (systemtap_session& s)
6649{
d0ea46ce 6650
e6fe60e7
AM
6651 if (! s.kprobe_derived_probes)
6652 s.kprobe_derived_probes = new kprobe_derived_probe_group ();
6653 s.kprobe_derived_probes->enroll (this);
d0ea46ce 6654
e6fe60e7 6655}
d0ea46ce 6656
e6fe60e7
AM
6657void kprobe_derived_probe_group::enroll (kprobe_derived_probe* p)
6658{
6659 probes_by_module.insert (make_pair (p->symbol_name, p));
6660 // probes of same symbol should share single kprobe/kretprobe
6661}
d0ea46ce 6662
e6fe60e7
AM
6663void
6664kprobe_derived_probe_group::emit_module_decls (systemtap_session& s)
6665{
6666 if (probes_by_module.empty()) return;
d0ea46ce 6667
e6fe60e7 6668 s.op->newline() << "/* ---- kprobe-based probes ---- */";
d0ea46ce 6669
e6fe60e7
AM
6670 // Warn of misconfigured kernels
6671 s.op->newline() << "#if ! defined(CONFIG_KPROBES)";
6672 s.op->newline() << "#error \"Need CONFIG_KPROBES!\"";
6673 s.op->newline() << "#endif";
6674 s.op->newline();
d0ea46ce 6675
f07c3b68 6676 s.op->newline() << "#ifndef KRETACTIVE";
1ee6b5fc 6677 s.op->newline() << "#define KRETACTIVE (max(15,6*(int)num_possible_cpus()))";
f07c3b68
FCE
6678 s.op->newline() << "#endif";
6679
e6fe60e7 6680 // Forward declare the master entry functions
88747011 6681 s.op->newline() << "static int enter_kprobe2_probe (struct kprobe *inst,";
e6fe60e7 6682 s.op->line() << " struct pt_regs *regs);";
88747011 6683 s.op->newline() << "static int enter_kretprobe2_probe (struct kretprobe_instance *inst,";
e6fe60e7 6684 s.op->line() << " struct pt_regs *regs);";
d0ea46ce 6685
e6fe60e7
AM
6686 // Emit an array of kprobe/kretprobe pointers
6687 s.op->newline() << "#if defined(STAPCONF_UNREGISTER_KPROBES)";
c9116e99 6688 s.op->newline() << "static void * stap_unreg_kprobes2[" << probes_by_module.size() << "];";
e6fe60e7 6689 s.op->newline() << "#endif";
d0ea46ce 6690
e6fe60e7 6691 // Emit the actual probe list.
d0ea46ce 6692
e6fe60e7
AM
6693 s.op->newline() << "static struct stap_dwarfless_kprobe {";
6694 s.op->newline(1) << "union { struct kprobe kp; struct kretprobe krp; } u;";
6695 s.op->newline() << "#ifdef __ia64__";
6696 s.op->newline() << "struct kprobe dummy;";
6697 s.op->newline() << "#endif";
6698 s.op->newline(-1) << "} stap_dwarfless_kprobes[" << probes_by_module.size() << "];";
6699 // NB: bss!
d0ea46ce 6700
e6fe60e7
AM
6701 s.op->newline() << "static struct stap_dwarfless_probe {";
6702 s.op->newline(1) << "const unsigned return_p:1;";
6703 s.op->newline() << "const unsigned maxactive_p:1;";
b350f56b 6704 s.op->newline() << "const unsigned optional_p:1;";
e6fe60e7
AM
6705 s.op->newline() << "unsigned registered_p:1;";
6706 s.op->newline() << "const unsigned short maxactive_val;";
935447c8 6707
e6fe60e7
AM
6708 // Function Names are mostly small and uniform enough to justify putting
6709 // char[MAX]'s into the array instead of relocated char*'s.
935447c8 6710
faea5e16
JS
6711 size_t symbol_string_name_max = 0;
6712 size_t symbol_string_name_tot = 0;
e6fe60e7 6713 for (p_b_m_iterator it = probes_by_module.begin(); it != probes_by_module.end(); it++)
6270adc1 6714 {
e6fe60e7
AM
6715 kprobe_derived_probe* p = it->second;
6716#define DOIT(var,expr) do { \
6717 size_t var##_size = (expr) + 1; \
6718 var##_max = max (var##_max, var##_size); \
6719 var##_tot += var##_size; } while (0)
e6fe60e7
AM
6720 DOIT(symbol_string_name, p->symbol_name.size());
6721#undef DOIT
6270adc1
MH
6722 }
6723
e6fe60e7
AM
6724#define CALCIT(var) \
6725 s.op->newline() << "const char " << #var << "[" << var##_name_max << "] ;";
935447c8 6726
e6fe60e7
AM
6727 CALCIT(symbol_string);
6728#undef CALCIT
6270adc1 6729
bd659351 6730 s.op->newline() << "unsigned long address;";
26e63673 6731 s.op->newline() << "struct stap_probe * const probe;";
e6fe60e7
AM
6732 s.op->newline(-1) << "} stap_dwarfless_probes[] = {";
6733 s.op->indent(1);
6270adc1 6734
e6fe60e7
AM
6735 for (p_b_m_iterator it = probes_by_module.begin(); it != probes_by_module.end(); it++)
6736 {
6737 kprobe_derived_probe* p = it->second;
6738 s.op->newline() << "{";
6739 if (p->has_return)
6740 s.op->line() << " .return_p=1,";
6270adc1 6741
e6fe60e7
AM
6742 if (p->has_maxactive)
6743 {
6744 s.op->line() << " .maxactive_p=1,";
6745 assert (p->maxactive_val >= 0 && p->maxactive_val <= USHRT_MAX);
6746 s.op->line() << " .maxactive_val=" << p->maxactive_val << ",";
6747 }
6270adc1 6748
b350f56b
JS
6749 if (p->locations[0]->optional)
6750 s.op->line() << " .optional_p=1,";
6751
e6fe60e7 6752 if (p->has_statement)
c8d9d15e 6753 s.op->line() << " .address=(unsigned long)0x" << hex << p->addr << dec << "ULL,";
e6fe60e7 6754 else
c8d9d15e 6755 s.op->line() << " .symbol_string=\"" << p->symbol_name << "\",";
5d67b47c 6756
faea5e16 6757 s.op->line() << " .probe=" << common_probe_init (p) << ",";
e6fe60e7 6758 s.op->line() << " },";
935447c8
DS
6759 }
6760
e6fe60e7 6761 s.op->newline(-1) << "};";
5d67b47c 6762
e6fe60e7
AM
6763 // Emit the kprobes callback function
6764 s.op->newline();
88747011 6765 s.op->newline() << "static int enter_kprobe2_probe (struct kprobe *inst,";
e6fe60e7
AM
6766 s.op->line() << " struct pt_regs *regs) {";
6767 // NB: as of PR5673, the kprobe|kretprobe union struct is in BSS
6768 s.op->newline(1) << "int kprobe_idx = ((uintptr_t)inst-(uintptr_t)stap_dwarfless_kprobes)/sizeof(struct stap_dwarfless_kprobe);";
6769 // Check that the index is plausible
6770 s.op->newline() << "struct stap_dwarfless_probe *sdp = &stap_dwarfless_probes[";
6771 s.op->line() << "((kprobe_idx >= 0 && kprobe_idx < " << probes_by_module.size() << ")?";
6772 s.op->line() << "kprobe_idx:0)"; // NB: at least we avoid memory corruption
6773 // XXX: it would be nice to give a more verbose error though; BUG_ON later?
6774 s.op->line() << "];";
faea5e16 6775 common_probe_entryfn_prologue (s.op, "STAP_SESSION_RUNNING", "sdp->probe");
e6fe60e7 6776 s.op->newline() << "c->regs = regs;";
6415ddde
MW
6777
6778 // Make it look like the IP is set as it wouldn't have been replaced
6779 // by a breakpoint instruction when calling real probe handler. Reset
6780 // IP regs on return, so we don't confuse kprobes. PR10458
6781 s.op->newline() << "{";
6782 s.op->indent(1);
6783 s.op->newline() << "unsigned long kprobes_ip = REG_IP(c->regs);";
259d54c0 6784 s.op->newline() << "SET_REG_IP(regs, (unsigned long) inst->addr);";
26e63673 6785 s.op->newline() << "(*sdp->probe->ph) (c);";
259d54c0 6786 s.op->newline() << "SET_REG_IP(regs, kprobes_ip);";
6415ddde
MW
6787 s.op->newline(-1) << "}";
6788
e6fe60e7
AM
6789 common_probe_entryfn_epilogue (s.op);
6790 s.op->newline() << "return 0;";
6791 s.op->newline(-1) << "}";
935447c8 6792
e6fe60e7
AM
6793 // Same for kretprobes
6794 s.op->newline();
88747011 6795 s.op->newline() << "static int enter_kretprobe2_probe (struct kretprobe_instance *inst,";
e6fe60e7
AM
6796 s.op->line() << " struct pt_regs *regs) {";
6797 s.op->newline(1) << "struct kretprobe *krp = inst->rp;";
935447c8 6798
e6fe60e7
AM
6799 // NB: as of PR5673, the kprobe|kretprobe union struct is in BSS
6800 s.op->newline() << "int kprobe_idx = ((uintptr_t)krp-(uintptr_t)stap_dwarfless_kprobes)/sizeof(struct stap_dwarfless_kprobe);";
6801 // Check that the index is plausible
6802 s.op->newline() << "struct stap_dwarfless_probe *sdp = &stap_dwarfless_probes[";
6803 s.op->line() << "((kprobe_idx >= 0 && kprobe_idx < " << probes_by_module.size() << ")?";
6804 s.op->line() << "kprobe_idx:0)"; // NB: at least we avoid memory corruption
6805 // XXX: it would be nice to give a more verbose error though; BUG_ON later?
6806 s.op->line() << "];";
935447c8 6807
faea5e16 6808 common_probe_entryfn_prologue (s.op, "STAP_SESSION_RUNNING", "sdp->probe");
e6fe60e7
AM
6809 s.op->newline() << "c->regs = regs;";
6810 s.op->newline() << "c->pi = inst;"; // for assisting runtime's backtrace logic
6415ddde
MW
6811
6812 // Make it look like the IP is set as it wouldn't have been replaced
6813 // by a breakpoint instruction when calling real probe handler. Reset
6814 // IP regs on return, so we don't confuse kprobes. PR10458
6815 s.op->newline() << "{";
6816 s.op->indent(1);
6817 s.op->newline() << "unsigned long kprobes_ip = REG_IP(c->regs);";
259d54c0 6818 s.op->newline() << "SET_REG_IP(regs, (unsigned long) inst->rp->kp.addr);";
26e63673 6819 s.op->newline() << "(*sdp->probe->ph) (c);";
259d54c0 6820 s.op->newline() << "SET_REG_IP(regs, kprobes_ip);";
6415ddde
MW
6821 s.op->newline(-1) << "}";
6822
e6fe60e7
AM
6823 common_probe_entryfn_epilogue (s.op);
6824 s.op->newline() << "return 0;";
6825 s.op->newline(-1) << "}";
bd659351 6826
03a4ec63 6827 s.op->newline() << "#ifdef STAPCONF_KALLSYMS_ON_EACH_SYMBOL";
bd659351
MW
6828 s.op->newline() << "static int kprobe_resolve(void *data, const char *name,";
6829 s.op->newline() << " struct module *owner,";
6830 s.op->newline() << " unsigned long val) {";
6831 s.op->newline(1) << "int i;";
fc1d2aa2
MW
6832 s.op->newline() << "int *p = (int *) data;";
6833 s.op->newline() << "for (i=0; i<" << probes_by_module.size()
6834 << " && *p > 0; i++) {";
bd659351
MW
6835 s.op->newline(1) << "struct stap_dwarfless_probe *sdp = & stap_dwarfless_probes[i];";
6836 s.op->newline() << "if (! sdp->address)";
fc1d2aa2 6837 s.op->newline(1) << "if (strcmp(sdp->symbol_string, name) == 0) {";
bd659351 6838 s.op->newline(1) << "sdp->address = val;";
fc1d2aa2
MW
6839 s.op->newline() << "(*p)--;";
6840 s.op->newline(-1) << "}";
6841 s.op->newline(-2) << "}";
6842 s.op->newline() << "return (p > 0) ? 0 : -1;";
bd659351 6843 s.op->newline(-1) << "}";
03a4ec63 6844 s.op->newline() << "#endif";
935447c8
DS
6845}
6846
e6fe60e7 6847
6270adc1 6848void
e6fe60e7 6849kprobe_derived_probe_group::emit_module_init (systemtap_session& s)
6270adc1 6850{
03a4ec63 6851 s.op->newline() << "#ifdef STAPCONF_KALLSYMS_ON_EACH_SYMBOL";
fc1d2aa2
MW
6852 s.op->newline() << "{";
6853 s.op->newline(1) << "int p = 0;";
6854 s.op->newline() << "for (i = 0; i < " << probes_by_module.size() << "; i++) {";
6855 s.op->newline(1) << "struct stap_dwarfless_probe *sdp = & stap_dwarfless_probes[i];";
6856 s.op->newline() << "if (! sdp->address)";
6857 s.op->newline(1) << "p++;";
6858 s.op->newline(-2) << "}";
6859 s.op->newline() << "kallsyms_on_each_symbol(kprobe_resolve, &p);";
6860 s.op->newline(-1) << "}";
03a4ec63 6861 s.op->newline() << "#endif";
bd659351 6862
e6fe60e7 6863 s.op->newline() << "for (i=0; i<" << probes_by_module.size() << "; i++) {";
c8d9d15e 6864 s.op->newline(1) << "struct stap_dwarfless_probe *sdp = & stap_dwarfless_probes[i];";
e6fe60e7 6865 s.op->newline() << "struct stap_dwarfless_kprobe *kp = & stap_dwarfless_kprobes[i];";
c8d9d15e 6866 s.op->newline() << "void *addr = (void *) sdp->address;";
03a4ec63
MW
6867 s.op->newline() << "const char *symbol_name = addr ? NULL : sdp->symbol_string;";
6868
6869 s.op->newline() << "#ifdef STAPCONF_KALLSYMS_ON_EACH_SYMBOL";
bd659351
MW
6870 s.op->newline() << "if (! addr) {";
6871 s.op->newline(1) << "sdp->registered_p = 0;";
9319b767
MW
6872 s.op->newline() << "if (!sdp->optional_p)";
6873 s.op->newline(1) << "_stp_warn (\"probe %s registration error (symbol not found)\", probe_point);";
6874 s.op->newline(-1) << "continue;";
bd659351 6875 s.op->newline(-1) << "}";
03a4ec63
MW
6876 s.op->newline() << "#endif";
6877
26e63673 6878 s.op->newline() << "probe_point = sdp->probe->pp;"; // for error messages
e6fe60e7 6879 s.op->newline() << "if (sdp->return_p) {";
c8d9d15e 6880 s.op->newline(1) << "kp->u.krp.kp.addr = addr;";
03a4ec63 6881 s.op->newline() << "kp->u.krp.kp.symbol_name = (char *) symbol_name;";
e6fe60e7
AM
6882 s.op->newline() << "if (sdp->maxactive_p) {";
6883 s.op->newline(1) << "kp->u.krp.maxactive = sdp->maxactive_val;";
6884 s.op->newline(-1) << "} else {";
f07c3b68 6885 s.op->newline(1) << "kp->u.krp.maxactive = KRETACTIVE;";
e6fe60e7 6886 s.op->newline(-1) << "}";
88747011 6887 s.op->newline() << "kp->u.krp.handler = &enter_kretprobe2_probe;";
e6fe60e7
AM
6888 // to ensure safeness of bspcache, always use aggr_kprobe on ia64
6889 s.op->newline() << "#ifdef __ia64__";
e6fe60e7 6890 s.op->newline() << "kp->dummy.addr = kp->u.krp.kp.addr;";
c8d9d15e
JS
6891 s.op->newline() << "kp->dummy.symbol_name = kp->u.krp.kp.symbol_name;";
6892 s.op->newline() << "kp->dummy.pre_handler = NULL;";
e6fe60e7
AM
6893 s.op->newline() << "rc = register_kprobe (& kp->dummy);";
6894 s.op->newline() << "if (rc == 0) {";
6895 s.op->newline(1) << "rc = register_kretprobe (& kp->u.krp);";
6896 s.op->newline() << "if (rc != 0)";
6897 s.op->newline(1) << "unregister_kprobe (& kp->dummy);";
6898 s.op->newline(-2) << "}";
6899 s.op->newline() << "#else";
6900 s.op->newline() << "rc = register_kretprobe (& kp->u.krp);";
6901 s.op->newline() << "#endif";
6902 s.op->newline(-1) << "} else {";
6903 // to ensure safeness of bspcache, always use aggr_kprobe on ia64
c8d9d15e 6904 s.op->newline(1) << "kp->u.kp.addr = addr;";
03a4ec63 6905 s.op->newline() << "kp->u.kp.symbol_name = (char *) symbol_name;";
88747011 6906 s.op->newline() << "kp->u.kp.pre_handler = &enter_kprobe2_probe;";
e6fe60e7 6907 s.op->newline() << "#ifdef __ia64__";
e6fe60e7 6908 s.op->newline() << "kp->dummy.pre_handler = NULL;";
c8d9d15e
JS
6909 s.op->newline() << "kp->dummy.addr = kp->u.kp.addr;";
6910 s.op->newline() << "kp->dummy.symbol_name = kp->u.kp.symbol_name;";
e6fe60e7
AM
6911 s.op->newline() << "rc = register_kprobe (& kp->dummy);";
6912 s.op->newline() << "if (rc == 0) {";
6913 s.op->newline(1) << "rc = register_kprobe (& kp->u.kp);";
6914 s.op->newline() << "if (rc != 0)";
6915 s.op->newline(1) << "unregister_kprobe (& kp->dummy);";
6916 s.op->newline(-2) << "}";
6917 s.op->newline() << "#else";
6918 s.op->newline() << "rc = register_kprobe (& kp->u.kp);";
6919 s.op->newline() << "#endif";
6920 s.op->newline(-1) << "}";
6921 s.op->newline() << "if (rc) {"; // PR6749: tolerate a failed register_*probe.
6922 s.op->newline(1) << "sdp->registered_p = 0;";
b350f56b 6923 s.op->newline() << "if (!sdp->optional_p)";
50b6acf7 6924 s.op->newline(1) << "_stp_warn (\"probe %s (address 0x%lx) registration error (rc %d)\", probe_point, (unsigned long) addr, rc);";
b350f56b 6925 s.op->newline(-1) << "rc = 0;"; // continue with other probes
e6fe60e7
AM
6926 // XXX: shall we increment numskipped?
6927 s.op->newline(-1) << "}";
6270adc1 6928
e6fe60e7
AM
6929 s.op->newline() << "else sdp->registered_p = 1;";
6930 s.op->newline(-1) << "}"; // for loop
6270adc1
MH
6931}
6932
e6fe60e7
AM
6933void
6934kprobe_derived_probe_group::emit_module_exit (systemtap_session& s)
935447c8 6935{
e6fe60e7
AM
6936 //Unregister kprobes by batch interfaces.
6937 s.op->newline() << "#if defined(STAPCONF_UNREGISTER_KPROBES)";
6938 s.op->newline() << "j = 0;";
6939 s.op->newline() << "for (i=0; i<" << probes_by_module.size() << "; i++) {";
6940 s.op->newline(1) << "struct stap_dwarfless_probe *sdp = & stap_dwarfless_probes[i];";
6941 s.op->newline() << "struct stap_dwarfless_kprobe *kp = & stap_dwarfless_kprobes[i];";
6942 s.op->newline() << "if (! sdp->registered_p) continue;";
6943 s.op->newline() << "if (!sdp->return_p)";
c9116e99 6944 s.op->newline(1) << "stap_unreg_kprobes2[j++] = &kp->u.kp;";
e6fe60e7 6945 s.op->newline(-2) << "}";
c9116e99 6946 s.op->newline() << "unregister_kprobes((struct kprobe **)stap_unreg_kprobes2, j);";
e6fe60e7
AM
6947 s.op->newline() << "j = 0;";
6948 s.op->newline() << "for (i=0; i<" << probes_by_module.size() << "; i++) {";
6949 s.op->newline(1) << "struct stap_dwarfless_probe *sdp = & stap_dwarfless_probes[i];";
6950 s.op->newline() << "struct stap_dwarfless_kprobe *kp = & stap_dwarfless_kprobes[i];";
6951 s.op->newline() << "if (! sdp->registered_p) continue;";
6952 s.op->newline() << "if (sdp->return_p)";
c9116e99 6953 s.op->newline(1) << "stap_unreg_kprobes2[j++] = &kp->u.krp;";
e6fe60e7 6954 s.op->newline(-2) << "}";
c9116e99 6955 s.op->newline() << "unregister_kretprobes((struct kretprobe **)stap_unreg_kprobes2, j);";
e6fe60e7
AM
6956 s.op->newline() << "#ifdef __ia64__";
6957 s.op->newline() << "j = 0;";
6958 s.op->newline() << "for (i=0; i<" << probes_by_module.size() << "; i++) {";
6959 s.op->newline(1) << "struct stap_dwarfless_probe *sdp = & stap_dwarfless_probes[i];";
6960 s.op->newline() << "struct stap_dwarfless_kprobe *kp = & stap_dwarfless_kprobes[i];";
6961 s.op->newline() << "if (! sdp->registered_p) continue;";
c9116e99 6962 s.op->newline() << "stap_unreg_kprobes2[j++] = &kp->dummy;";
e6fe60e7 6963 s.op->newline(-1) << "}";
c9116e99 6964 s.op->newline() << "unregister_kprobes((struct kprobe **)stap_unreg_kprobes2, j);";
e6fe60e7
AM
6965 s.op->newline() << "#endif";
6966 s.op->newline() << "#endif";
3e3bd7b6 6967
e6fe60e7
AM
6968 s.op->newline() << "for (i=0; i<" << probes_by_module.size() << "; i++) {";
6969 s.op->newline(1) << "struct stap_dwarfless_probe *sdp = & stap_dwarfless_probes[i];";
6970 s.op->newline() << "struct stap_dwarfless_kprobe *kp = & stap_dwarfless_kprobes[i];";
6971 s.op->newline() << "if (! sdp->registered_p) continue;";
6972 s.op->newline() << "if (sdp->return_p) {";
6973 s.op->newline() << "#if !defined(STAPCONF_UNREGISTER_KPROBES)";
6974 s.op->newline(1) << "unregister_kretprobe (&kp->u.krp);";
6975 s.op->newline() << "#endif";
6976 s.op->newline() << "atomic_add (kp->u.krp.nmissed, & skipped_count);";
6977 s.op->newline() << "#ifdef STP_TIMING";
6978 s.op->newline() << "if (kp->u.krp.nmissed)";
26e63673 6979 s.op->newline(1) << "_stp_warn (\"Skipped due to missed kretprobe/1 on '%s': %d\\n\", sdp->probe->pp, kp->u.krp.nmissed);";
e6fe60e7
AM
6980 s.op->newline(-1) << "#endif";
6981 s.op->newline() << "atomic_add (kp->u.krp.kp.nmissed, & skipped_count);";
6982 s.op->newline() << "#ifdef STP_TIMING";
6983 s.op->newline() << "if (kp->u.krp.kp.nmissed)";
26e63673 6984 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
6985 s.op->newline(-1) << "#endif";
6986 s.op->newline(-1) << "} else {";
6987 s.op->newline() << "#if !defined(STAPCONF_UNREGISTER_KPROBES)";
6988 s.op->newline(1) << "unregister_kprobe (&kp->u.kp);";
6989 s.op->newline() << "#endif";
6990 s.op->newline() << "atomic_add (kp->u.kp.nmissed, & skipped_count);";
6991 s.op->newline() << "#ifdef STP_TIMING";
6992 s.op->newline() << "if (kp->u.kp.nmissed)";
26e63673 6993 s.op->newline(1) << "_stp_warn (\"Skipped due to missed kprobe on '%s': %lu\\n\", sdp->probe->pp, kp->u.kp.nmissed);";
e6fe60e7
AM
6994 s.op->newline(-1) << "#endif";
6995 s.op->newline(-1) << "}";
6996 s.op->newline() << "#if !defined(STAPCONF_UNREGISTER_KPROBES) && defined(__ia64__)";
6997 s.op->newline() << "unregister_kprobe (&kp->dummy);";
6998 s.op->newline() << "#endif";
6999 s.op->newline() << "sdp->registered_p = 0;";
7000 s.op->newline(-1) << "}";
f8a968bc
JS
7001}
7002
e6fe60e7 7003struct kprobe_builder: public derived_probe_builder
3c1b3d06 7004{
e6fe60e7
AM
7005 kprobe_builder() {}
7006 virtual void build(systemtap_session & sess,
7007 probe * base,
7008 probe_point * location,
7009 literal_map_t const & parameters,
7010 vector<derived_probe *> & finished_results);
7011};
3c1b3d06
FCE
7012
7013
79189b84 7014void
e6fe60e7
AM
7015kprobe_builder::build(systemtap_session & sess,
7016 probe * base,
7017 probe_point * location,
7018 literal_map_t const & parameters,
7019 vector<derived_probe *> & finished_results)
79189b84 7020{
e6fe60e7 7021 string function_string_val, module_string_val;
b642c901 7022 string path, library;
b6371390
JS
7023 int64_t statement_num_val = 0, maxactive_val = 0;
7024 bool has_function_str, has_module_str, has_statement_num;
7025 bool has_absolute, has_return, has_maxactive;
b642c901 7026 bool has_path, has_library;
79189b84 7027
b6371390
JS
7028 has_function_str = get_param(parameters, TOK_FUNCTION, function_string_val);
7029 has_module_str = get_param(parameters, TOK_MODULE, module_string_val);
7030 has_return = has_null_param (parameters, TOK_RETURN);
7031 has_maxactive = get_param(parameters, TOK_MAXACTIVE, maxactive_val);
7032 has_statement_num = get_param(parameters, TOK_STATEMENT, statement_num_val);
7033 has_absolute = has_null_param (parameters, TOK_ABSOLUTE);
b642c901
SC
7034 has_path = get_param (parameters, TOK_PROCESS, path);
7035 has_library = get_param (parameters, TOK_LIBRARY, library);
7036
7037 if (has_path)
7038 path = find_executable (path);
7039 if (has_library)
7040 library = find_executable (library, "LD_LIBRARY_PATH");
c57ea854 7041
b6371390 7042 if (has_function_str)
6fb70fb7 7043 {
b6371390
JS
7044 if (has_module_str)
7045 function_string_val = module_string_val + ":" + function_string_val;
86758d5f 7046
b6371390
JS
7047 finished_results.push_back (new kprobe_derived_probe (base,
7048 location, function_string_val,
7049 0, has_return,
7050 has_statement_num,
7051 has_maxactive,
b642c901
SC
7052 has_path,
7053 has_library,
7054 maxactive_val,
7055 path,
7056 library));
6fb70fb7 7057 }
e6fe60e7 7058 else
b6371390
JS
7059 {
7060 // assert guru mode for absolute probes
7061 if ( has_statement_num && has_absolute && !base->privileged )
7062 throw semantic_error ("absolute statement probe in unprivileged script", base->tok);
7063
7064 finished_results.push_back (new kprobe_derived_probe (base,
7065 location, "",
7066 statement_num_val,
7067 has_return,
7068 has_statement_num,
7069 has_maxactive,
b642c901
SC
7070 has_path,
7071 has_library,
7072 maxactive_val,
7073 path,
7074 library));
96b030fe 7075 }
79189b84
JS
7076}
7077
dd225250
PS
7078// ------------------------------------------------------------------------
7079// Hardware breakpoint based probes.
7080// ------------------------------------------------------------------------
7081
7082static const string TOK_HWBKPT("data");
7083static const string TOK_HWBKPT_WRITE("write");
7084static const string TOK_HWBKPT_RW("rw");
7085static const string TOK_LENGTH("length");
7086
7087#define HWBKPT_READ 0
7088#define HWBKPT_WRITE 1
7089#define HWBKPT_RW 2
7090struct hwbkpt_derived_probe: public derived_probe
7091{
7092 hwbkpt_derived_probe (probe *base,
7093 probe_point *location,
7094 uint64_t addr,
7095 string symname,
7096 unsigned int len,
7097 bool has_only_read_access,
7098 bool has_only_write_access,
7099 bool has_rw_access
7100 );
7101 Dwarf_Addr hwbkpt_addr;
7102 string symbol_name;
7103 unsigned int hwbkpt_access,hwbkpt_len;
7104
7105 void printsig (std::ostream &o) const;
7106 void join_group (systemtap_session& s);
7107};
7108
7109struct hwbkpt_derived_probe_group: public derived_probe_group
7110{
dd225250 7111private:
dac77b80 7112 vector<hwbkpt_derived_probe*> hwbkpt_probes;
dd225250
PS
7113
7114public:
7115 void enroll (hwbkpt_derived_probe* probe, systemtap_session& s);
7116 void emit_module_decls (systemtap_session& s);
7117 void emit_module_init (systemtap_session& s);
7118 void emit_module_exit (systemtap_session& s);
7119};
7120
7121hwbkpt_derived_probe::hwbkpt_derived_probe (probe *base,
9ea68eb9
JS
7122 probe_point *location,
7123 uint64_t addr,
7124 string symname,
7125 unsigned int len,
7126 bool has_only_read_access,
7127 bool has_only_write_access,
7128 bool has_rw_access
7129 ):
4c5d1300 7130 derived_probe (base, location, true /* .components soon rewritten */ ),
dd225250
PS
7131 hwbkpt_addr (addr),
7132 symbol_name (symname),
7133 hwbkpt_len (len)
7134{
7135 this->tok = base->tok;
7136
7137 vector<probe_point::component*> comps;
7138 comps.push_back (new probe_point::component(TOK_KERNEL));
7139
7140 if (hwbkpt_addr)
9ea68eb9
JS
7141 comps.push_back (new probe_point::component (TOK_HWBKPT,
7142 new literal_number(hwbkpt_addr, true)));
7143 else if (symbol_name.size())
7144 comps.push_back (new probe_point::component (TOK_HWBKPT, new literal_string(symbol_name)));
dd225250
PS
7145
7146 comps.push_back (new probe_point::component (TOK_LENGTH, new literal_number(hwbkpt_len)));
7147
7148 if (has_only_read_access)
9ea68eb9 7149 this->hwbkpt_access = HWBKPT_READ ;
dd225250
PS
7150//TODO add code for comps.push_back for read, since this flag is not for x86
7151
7152 else
9ea68eb9
JS
7153 {
7154 if (has_only_write_access)
7155 {
7156 this->hwbkpt_access = HWBKPT_WRITE ;
7157 comps.push_back (new probe_point::component(TOK_HWBKPT_WRITE));
7158 }
7159 else
7160 {
7161 this->hwbkpt_access = HWBKPT_RW ;
7162 comps.push_back (new probe_point::component(TOK_HWBKPT_RW));
7163 }
7164 }
dd225250
PS
7165
7166 this->sole_location()->components = comps;
7167}
7168
7169void hwbkpt_derived_probe::printsig (ostream& o) const
7170{
7171 sole_location()->print (o);
7172 printsig_nested (o);
7173}
7174
7175void hwbkpt_derived_probe::join_group (systemtap_session& s)
7176{
dac77b80
FCE
7177 if (! s.hwbkpt_derived_probes)
7178 s.hwbkpt_derived_probes = new hwbkpt_derived_probe_group ();
dd225250
PS
7179 s.hwbkpt_derived_probes->enroll (this, s);
7180}
7181
7182void hwbkpt_derived_probe_group::enroll (hwbkpt_derived_probe* p, systemtap_session& s)
7183{
dac77b80
FCE
7184 hwbkpt_probes.push_back (p);
7185
7186 unsigned max_hwbkpt_probes_by_arch = 0;
7187 if (s.architecture == "i386" || s.architecture == "x86_64")
7188 max_hwbkpt_probes_by_arch = 4;
7189 else if (s.architecture == "s390")
7190 max_hwbkpt_probes_by_arch = 1;
7191
c57ea854 7192 if (hwbkpt_probes.size() >= max_hwbkpt_probes_by_arch)
dac77b80
FCE
7193 if (! s.suppress_warnings)
7194 s.print_warning ("Too many hardware breakpoint probes requested for " + s.architecture
7195 + "(" + lex_cast(hwbkpt_probes.size()) +
7196 " vs. " + lex_cast(max_hwbkpt_probes_by_arch) + ")");
dd225250
PS
7197}
7198
7199void
7200hwbkpt_derived_probe_group::emit_module_decls (systemtap_session& s)
7201{
dac77b80 7202 if (hwbkpt_probes.empty()) return;
dd225250
PS
7203
7204 s.op->newline() << "/* ---- hwbkpt-based probes ---- */";
7205
7206 s.op->newline() << "#include <linux/perf_event.h>";
7207 s.op->newline() << "#include <linux/hw_breakpoint.h>";
7208 s.op->newline();
7209
7210 // Forward declare the master entry functions
7211 s.op->newline() << "static int enter_hwbkpt_probe (struct perf_event *bp,";
7212 s.op->line() << " int nmi,";
7213 s.op->line() << " struct perf_sample_data *data,";
7214 s.op->line() << " struct pt_regs *regs);";
79189b84 7215
dd225250
PS
7216 // Emit the actual probe list.
7217
7218 s.op->newline() << "static struct perf_event_attr ";
dac77b80 7219 s.op->newline() << "stap_hwbkpt_probe_array[" << hwbkpt_probes.size() << "];";
dd225250
PS
7220
7221 s.op->newline() << "static struct perf_event **";
dac77b80 7222 s.op->newline() << "stap_hwbkpt_ret_array[" << hwbkpt_probes.size() << "];";
dd225250
PS
7223 s.op->newline() << "static struct stap_hwbkpt_probe {";
7224 s.op->newline() << "int registered_p:1;";
7225// registered_p = 0 signifies a probe that failed registration
7226// registered_p = 1 signifies a probe that got registered successfully
7227
faea5e16 7228 // Symbol Names are mostly small and uniform enough
dd225250 7229 // to justify putting const char*.
dac77b80 7230 s.op->newline() << "const char * const symbol;";
dd225250
PS
7231
7232 s.op->newline() << "const unsigned long address;";
7233 s.op->newline() << "uint8_t atype;";
bb0a4e12 7234 s.op->newline() << "unsigned int len;";
26e63673 7235 s.op->newline() << "struct stap_probe * const probe;";
dd225250
PS
7236 s.op->newline() << "} stap_hwbkpt_probes[] = {";
7237 s.op->indent(1);
7238
dac77b80 7239 for (unsigned int it = 0; it < hwbkpt_probes.size(); it++)
dd225250 7240 {
dac77b80 7241 hwbkpt_derived_probe* p = hwbkpt_probes.at(it);
dd225250
PS
7242 s.op->newline() << "{";
7243 s.op->line() << " .registered_p=1,";
7244 if (p->symbol_name.size())
7245 s.op->line() << " .address=(unsigned long)0x0" << "ULL,";
7246 else
7247 s.op->line() << " .address=(unsigned long)0x" << hex << p->hwbkpt_addr << dec << "ULL,";
7248 switch(p->hwbkpt_access){
7249 case HWBKPT_READ:
7250 s.op->line() << " .atype=HW_BREAKPOINT_R ,";
bb0a4e12 7251 break;
dd225250
PS
7252 case HWBKPT_WRITE:
7253 s.op->line() << " .atype=HW_BREAKPOINT_W ,";
bb0a4e12 7254 break;
dd225250
PS
7255 case HWBKPT_RW:
7256 s.op->line() << " .atype=HW_BREAKPOINT_R|HW_BREAKPOINT_W ,";
bb0a4e12 7257 break;
dd225250
PS
7258 };
7259 s.op->line() << " .len=" << p->hwbkpt_len << ",";
faea5e16 7260 s.op->line() << " .probe=" << common_probe_init (p) << ",";
dd225250 7261 s.op->line() << " .symbol=\"" << p->symbol_name << "\",";
dd225250
PS
7262 s.op->line() << " },";
7263 }
dac77b80 7264 s.op->newline(-1) << "};";
dd225250
PS
7265
7266 // Emit the hwbkpt callback function
7267 s.op->newline() ;
7268 s.op->newline() << "static int enter_hwbkpt_probe (struct perf_event *bp,";
7269 s.op->line() << " int nmi,";
7270 s.op->line() << " struct perf_sample_data *data,";
7271 s.op->line() << " struct pt_regs *regs) {";
dac77b80
FCE
7272 s.op->newline(1) << "unsigned int i;";
7273 s.op->newline() << "if (bp->attr.type != PERF_TYPE_BREAKPOINT) return -1;";
7274 s.op->newline() << "for (i=0; i<" << hwbkpt_probes.size() << "; i++) {";
7275 s.op->newline(1) << "struct perf_event_attr *hp = & stap_hwbkpt_probe_array[i];";
7276 // XXX: why not match stap_hwbkpt_ret_array[i] against bp instead?
7277 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) {";
7278 s.op->newline(1) << "struct stap_hwbkpt_probe *sdp = &stap_hwbkpt_probes[i];";
faea5e16 7279 common_probe_entryfn_prologue (s.op, "STAP_SESSION_RUNNING", "sdp->probe");
dd225250 7280 s.op->newline() << "c->regs = regs;";
26e63673 7281 s.op->newline() << "(*sdp->probe->ph) (c);";
dd225250 7282 common_probe_entryfn_epilogue (s.op);
dac77b80 7283 s.op->newline(-1) << "}";
dd225250
PS
7284 s.op->newline(-1) << "}";
7285 s.op->newline() << "return 0;";
dac77b80 7286 s.op->newline(-1) << "}";
dd225250
PS
7287}
7288
7289void
7290hwbkpt_derived_probe_group::emit_module_init (systemtap_session& s)
7291{
dac77b80 7292 s.op->newline() << "for (i=0; i<" << hwbkpt_probes.size() << "; i++) {";
dd225250
PS
7293 s.op->newline(1) << "struct stap_hwbkpt_probe *sdp = & stap_hwbkpt_probes[i];";
7294 s.op->newline() << "struct perf_event_attr *hp = & stap_hwbkpt_probe_array[i];";
7295 s.op->newline() << "void *addr = (void *) sdp->address;";
7296 s.op->newline() << "const char *hwbkpt_symbol_name = addr ? NULL : sdp->symbol;";
dac77b80
FCE
7297 s.op->newline() << "hw_breakpoint_init(hp);";
7298 s.op->newline() << "if (addr)";
7299 s.op->newline(1) << "hp->bp_addr = (unsigned long) addr;";
7300 s.op->newline(-1) << "else { ";
7301 s.op->newline(1) << "hp->bp_addr = kallsyms_lookup_name(hwbkpt_symbol_name);";
7302 s.op->newline() << "if (!hp->bp_addr) { ";
26e63673 7303 s.op->newline(1) << "_stp_warn(\"Probe %s registration skipped: invalid symbol %s \",sdp->probe->pp,hwbkpt_symbol_name);";
dac77b80
FCE
7304 s.op->newline() << "continue;";
7305 s.op->newline(-1) << "}";
7306 s.op->newline(-1) << "}";
7307 s.op->newline() << "hp->bp_type = sdp->atype;";
7308
7309 // On x86 & x86-64, hp->bp_len is not just a number but a macro/enum (!?!).
c57ea854 7310 if (s.architecture == "i386" || s.architecture == "x86_64" )
dac77b80
FCE
7311 {
7312 s.op->newline() << "switch(sdp->len) {";
7313 s.op->newline() << "case 1:";
7314 s.op->newline(1) << "hp->bp_len = HW_BREAKPOINT_LEN_1;";
7315 s.op->newline() << "break;";
7316 s.op->newline(-1) << "case 2:";
7317 s.op->newline(1) << "hp->bp_len = HW_BREAKPOINT_LEN_2;";
7318 s.op->newline() << "break;";
7319 s.op->newline(-1) << "case 3:";
7320 s.op->newline() << "case 4:";
7321 s.op->newline(1) << "hp->bp_len = HW_BREAKPOINT_LEN_4;";
7322 s.op->newline() << "break;";
7323 s.op->newline(-1) << "case 5:";
7324 s.op->newline() << "case 6:";
7325 s.op->newline() << "case 7:";
7326 s.op->newline() << "case 8:";
7327 s.op->newline() << "default:"; // XXX: could instead reject
7328 s.op->newline(1) << "hp->bp_len = HW_BREAKPOINT_LEN_8;";
7329 s.op->newline() << "break;";
7330 s.op->newline(-1) << "}";
7331 }
7332 else // other architectures presumed straightforward
7333 s.op->newline() << "hp->bp_len = sdp->len;";
7334
26e63673 7335 s.op->newline() << "probe_point = sdp->probe->pp;"; // for error messages
dac77b80
FCE
7336 s.op->newline() << "stap_hwbkpt_ret_array[i] = register_wide_hw_breakpoint(hp, (void *)&enter_hwbkpt_probe);";
7337 s.op->newline() << "if (IS_ERR(stap_hwbkpt_ret_array[i])) {";
7338 s.op->newline(1) << "int err_code = PTR_ERR(stap_hwbkpt_ret_array[i]);";
7339 s.op->newline(0) << "_stp_warn(\"Hwbkpt probe %s: registration error %d, addr %p, name %s\", probe_point, err_code, addr, hwbkpt_symbol_name);";
7340 s.op->newline(-1) << "}";
dd225250 7341 s.op->newline() << " else sdp->registered_p = 1;";
dd225250
PS
7342 s.op->newline(-1) << "}"; // for loop
7343}
7344
7345void
7346hwbkpt_derived_probe_group::emit_module_exit (systemtap_session& s)
7347{
7348 //Unregister hwbkpt probes.
dac77b80 7349 s.op->newline() << "for (i=0; i<" << hwbkpt_probes.size() << "; i++) {";
dd225250 7350 s.op->newline(1) << "struct stap_hwbkpt_probe *sdp = & stap_hwbkpt_probes[i];";
dac77b80
FCE
7351 s.op->newline() << "if (sdp->registered_p == 0) continue;";
7352 s.op->newline() << "unregister_wide_hw_breakpoint(stap_hwbkpt_ret_array[i]);";
dd225250
PS
7353 s.op->newline() << "sdp->registered_p = 0;";
7354 s.op->newline(-1) << "}";
7355}
7356
7357struct hwbkpt_builder: public derived_probe_builder
7358{
7359 hwbkpt_builder() {}
7360 virtual void build(systemtap_session & sess,
7361 probe * base,
7362 probe_point * location,
7363 literal_map_t const & parameters,
7364 vector<derived_probe *> & finished_results);
7365};
7366
7367void
7368hwbkpt_builder::build(systemtap_session & sess,
7369 probe * base,
7370 probe_point * location,
7371 literal_map_t const & parameters,
7372 vector<derived_probe *> & finished_results)
7373{
7374 string symbol_str_val;
7375 int64_t hwbkpt_address, len;
7376 bool has_addr, has_symbol_str, has_write, has_rw, has_len;
7377
b47f3a55
FCE
7378 if (! (sess.kernel_config["CONFIG_PERF_EVENTS"] == string("y")))
7379 throw semantic_error ("CONFIG_PERF_EVENTS not available on this kernel",
7380 location->components[0]->tok);
7381 if (! (sess.kernel_config["CONFIG_HAVE_HW_BREAKPOINT"] == string("y")))
7382 throw semantic_error ("CONFIG_HAVE_HW_BREAKPOINT not available on this kernel",
7383 location->components[0]->tok);
7384
dd225250
PS
7385 has_addr = get_param (parameters, TOK_HWBKPT, hwbkpt_address);
7386 has_symbol_str = get_param (parameters, TOK_HWBKPT, symbol_str_val);
7387 has_len = get_param (parameters, TOK_LENGTH, len);
7388 has_write = (parameters.find(TOK_HWBKPT_WRITE) != parameters.end());
7389 has_rw = (parameters.find(TOK_HWBKPT_RW) != parameters.end());
7390
7391 if (!has_len)
7392 len = 1;
7393
7394 if (has_addr)
7395 finished_results.push_back (new hwbkpt_derived_probe (base,
7396 location,
7397 hwbkpt_address,
7398 "",len,0,
7399 has_write,
7400 has_rw));
7401 else // has symbol_str
7402 finished_results.push_back (new hwbkpt_derived_probe (base,
7403 location,
7404 0,
7405 symbol_str_val,len,0,
7406 has_write,
7407 has_rw));
7408}
342d3f96 7409
0a6f5a3f
JS
7410// ------------------------------------------------------------------------
7411// statically inserted kernel-tracepoint derived probes
7412// ------------------------------------------------------------------------
7413
6fb70fb7 7414struct tracepoint_arg
79189b84 7415{
ad370dcc 7416 string name, c_type, typecast;
dcaa1a65 7417 bool usable, used, isptr;
f8a968bc 7418 Dwarf_Die type_die;
dcaa1a65 7419 tracepoint_arg(): usable(false), used(false), isptr(false) {}
6fb70fb7 7420};
79189b84 7421
0a6f5a3f
JS
7422struct tracepoint_derived_probe: public derived_probe
7423{
79189b84
JS
7424 tracepoint_derived_probe (systemtap_session& s,
7425 dwflpp& dw, Dwarf_Die& func_die,
7426 const string& tracepoint_name,
7427 probe* base_probe, probe_point* location);
bc9a523d 7428
79189b84 7429 systemtap_session& sess;
6fb70fb7
JS
7430 string tracepoint_name, header;
7431 vector <struct tracepoint_arg> args;
bc9a523d 7432
6fb70fb7 7433 void build_args(dwflpp& dw, Dwarf_Die& func_die);
d0bfd2ac 7434 void getargs (std::list<std::string> &arg_set) const;
79189b84 7435 void join_group (systemtap_session& s);
3e3bd7b6 7436 void print_dupe_stamp(ostream& o);
0a6f5a3f 7437};
79189b84
JS
7438
7439
0a6f5a3f 7440struct tracepoint_derived_probe_group: public generic_dpg<tracepoint_derived_probe>
79189b84 7441{
79189b84
JS
7442 void emit_module_decls (systemtap_session& s);
7443 void emit_module_init (systemtap_session& s);
7444 void emit_module_exit (systemtap_session& s);
0a6f5a3f 7445};
79189b84 7446
bc9a523d 7447
f8a968bc
JS
7448struct tracepoint_var_expanding_visitor: public var_expanding_visitor
7449{
7450 tracepoint_var_expanding_visitor(dwflpp& dw, const string& probe_name,
7451 vector <struct tracepoint_arg>& args):
7452 dw (dw), probe_name (probe_name), args (args) {}
7453 dwflpp& dw;
7454 const string& probe_name;
7455 vector <struct tracepoint_arg>& args;
bc9a523d 7456
f8a968bc
JS
7457 void visit_target_symbol (target_symbol* e);
7458 void visit_target_symbol_arg (target_symbol* e);
7459 void visit_target_symbol_context (target_symbol* e);
7460};
79189b84
JS
7461
7462
f8a968bc
JS
7463void
7464tracepoint_var_expanding_visitor::visit_target_symbol_arg (target_symbol* e)
75ead1f7 7465{
277c21bc 7466 string argname = e->name.substr(1);
75ead1f7 7467
f8a968bc
JS
7468 // search for a tracepoint parameter matching this name
7469 tracepoint_arg *arg = NULL;
7470 for (unsigned i = 0; i < args.size(); ++i)
dcaa1a65 7471 if (args[i].usable && args[i].name == argname)
f8a968bc
JS
7472 {
7473 arg = &args[i];
7474 arg->used = true;
7475 break;
7476 }
75ead1f7 7477
f8a968bc
JS
7478 if (arg == NULL)
7479 {
7480 stringstream alternatives;
7481 for (unsigned i = 0; i < args.size(); ++i)
7482 alternatives << " $" << args[i].name;
046e7190 7483 alternatives << " $$name $$parms $$vars";
75ead1f7 7484
f8a968bc
JS
7485 // We hope that this value ends up not being referenced after all, so it
7486 // can be optimized out quietly.
277c21bc 7487 throw semantic_error("unable to find tracepoint variable '" + e->name
f8a968bc
JS
7488 + "' (alternatives:" + alternatives.str () + ")", e->tok);
7489 // NB: we can have multiple errors, since a target variable
7490 // may be expanded in several different contexts:
7491 // trace ("*") { $foo->bar }
f8a968bc 7492 }
75ead1f7 7493
f8a968bc 7494 // make sure we're not dereferencing base types
dc5a09fc 7495 if (!arg->isptr)
d19a9a82 7496 e->assert_no_components("tracepoint", true);
75ead1f7 7497
f8a968bc
JS
7498 // we can only write to dereferenced fields, and only if guru mode is on
7499 bool lvalue = is_active_lvalue(e);
7500 if (lvalue && (!dw.sess.guru_mode || e->components.empty()))
277c21bc 7501 throw semantic_error("write to tracepoint variable '" + e->name
f8a968bc 7502 + "' not permitted", e->tok);
c69a87e0 7503
ad370dcc
JS
7504 // XXX: if a struct/union arg is passed by value, then writing to its fields
7505 // is also meaningless until you dereference past a pointer member. It's
7506 // harder to detect and prevent that though...
75ead1f7 7507
f8a968bc
JS
7508 if (e->components.empty())
7509 {
03c75a4a
JS
7510 if (e->addressof)
7511 throw semantic_error("cannot take address of tracepoint variable", e->tok);
a45664f4 7512
3e3bd7b6 7513 // Just grab the value from the probe locals
a45664f4
JS
7514 symbol* sym = new symbol;
7515 sym->tok = e->tok;
7516 sym->name = "__tracepoint_arg_" + arg->name;
7517 provide (sym);
f8a968bc
JS
7518 }
7519 else
7520 {
5f36109e
JS
7521 // make a copy of the original as a bare target symbol for the tracepoint
7522 // value, which will be passed into the dwarf dereferencing code
7523 target_symbol* e2 = deep_copy_visitor::deep_copy(e);
7524 e2->components.clear();
7525
7526 if (e->components.back().type == target_symbol::comp_pretty_print)
7527 {
7528 if (lvalue)
7529 throw semantic_error("cannot write to pretty-printed variable", e->tok);
7530
d19a9a82 7531 dwarf_pretty_print dpp(dw, &arg->type_die, e2, arg->isptr, false, *e);
5f36109e
JS
7532 dpp.expand()->visit (this);
7533 return;
7534 }
7535
f8a968bc
JS
7536 // Synthesize a function to dereference the dwarf fields,
7537 // with a pointer parameter that is the base tracepoint variable
7538 functiondecl *fdecl = new functiondecl;
59de45f1 7539 fdecl->synthetic = true;
f8a968bc
JS
7540 fdecl->tok = e->tok;
7541 embeddedcode *ec = new embeddedcode;
7542 ec->tok = e->tok;
75ead1f7 7543
f8a968bc 7544 string fname = (string(lvalue ? "_tracepoint_tvar_set" : "_tracepoint_tvar_get")
277c21bc 7545 + "_" + e->name.substr(1)
aca66a36 7546 + "_" + lex_cast(tick++));
75ead1f7 7547
f8a968bc
JS
7548 fdecl->name = fname;
7549 fdecl->body = ec;
75ead1f7 7550
b5a0dd41
FCE
7551 // PR10601: adapt to kernel-vs-userspace loc2c-runtime
7552 ec->code += "\n#define fetch_register k_fetch_register\n";
7553 ec->code += "#define store_register k_store_register\n";
c69a87e0
FCE
7554
7555 ec->code += dw.literal_stmt_for_pointer (&arg->type_die, e,
f8a968bc 7556 lvalue, fdecl->type);
75ead1f7 7557
f8a968bc
JS
7558 // Give the fdecl an argument for the raw tracepoint value
7559 vardecl *v1 = new vardecl;
7560 v1->type = pe_long;
7561 v1->name = "pointer";
7562 v1->tok = e->tok;
7563 fdecl->formal_args.push_back(v1);
75ead1f7 7564
6fda2dff
JS
7565 // Any non-literal indexes need to be passed in too.
7566 for (unsigned i = 0; i < e->components.size(); ++i)
7567 if (e->components[i].type == target_symbol::comp_expression_array_index)
7568 {
7569 vardecl *v = new vardecl;
7570 v->type = pe_long;
aca66a36 7571 v->name = "index" + lex_cast(i);
6fda2dff
JS
7572 v->tok = e->tok;
7573 fdecl->formal_args.push_back(v);
7574 }
7575
f8a968bc
JS
7576 if (lvalue)
7577 {
7578 // Modify the fdecl so it carries a pe_long formal
7579 // argument called "value".
75ead1f7 7580
f8a968bc
JS
7581 // FIXME: For the time being we only support setting target
7582 // variables which have base types; these are 'pe_long' in
7583 // stap's type vocabulary. Strings and pointers might be
7584 // reasonable, some day, but not today.
7585
7586 vardecl *v2 = new vardecl;
7587 v2->type = pe_long;
7588 v2->name = "value";
7589 v2->tok = e->tok;
7590 fdecl->formal_args.push_back(v2);
7591 }
7592 else
7593 ec->code += "/* pure */";
7594
64211010
DB
7595 ec->code += "/* unprivileged */";
7596
b5a0dd41
FCE
7597 // PR10601
7598 ec->code += "\n#undef fetch_register\n";
7599 ec->code += "\n#undef store_register\n";
aff5d390 7600
f8809d54 7601 fdecl->join (dw.sess);
75ead1f7 7602
f8a968bc
JS
7603 // Synthesize a functioncall.
7604 functioncall* n = new functioncall;
7605 n->tok = e->tok;
7606 n->function = fname;
6fda2dff
JS
7607 n->args.push_back(require(e2));
7608
7609 // Any non-literal indexes need to be passed in too.
7610 for (unsigned i = 0; i < e->components.size(); ++i)
7611 if (e->components[i].type == target_symbol::comp_expression_array_index)
7612 n->args.push_back(require(e->components[i].expr_index));
75ead1f7 7613
f8a968bc
JS
7614 if (lvalue)
7615 {
7616 // Provide the functioncall to our parent, so that it can be
7617 // used to substitute for the assignment node immediately above
7618 // us.
7619 assert(!target_symbol_setter_functioncalls.empty());
7620 *(target_symbol_setter_functioncalls.top()) = n;
7621 }
75ead1f7 7622
f8a968bc
JS
7623 provide (n);
7624 }
75ead1f7
JS
7625}
7626
7627
f8a968bc
JS
7628void
7629tracepoint_var_expanding_visitor::visit_target_symbol_context (target_symbol* e)
0a6f5a3f 7630{
03c75a4a
JS
7631 if (e->addressof)
7632 throw semantic_error("cannot take address of context variable", e->tok);
7633
f8a968bc 7634 if (is_active_lvalue (e))
277c21bc 7635 throw semantic_error("write to tracepoint '" + e->name + "' not permitted", e->tok);
0a6f5a3f 7636
277c21bc 7637 if (e->name == "$$name")
f8a968bc 7638 {
5f36109e
JS
7639 e->assert_no_components("tracepoint");
7640
bfdaad1e
DS
7641 // Synthesize an embedded expression.
7642 embedded_expr *expr = new embedded_expr;
7643 expr->tok = e->tok;
7644 expr->code = string("/* string */ /* pure */ ")
7645 + string("c->marker_name ? c->marker_name : \"\"");
7646 provide (expr);
f8a968bc 7647 }
277c21bc 7648 else if (e->name == "$$vars" || e->name == "$$parms")
f8a968bc 7649 {
5f36109e
JS
7650 e->assert_no_components("tracepoint", true);
7651
f8a968bc
JS
7652 // Convert $$vars to sprintf of a list of vars which we recursively evaluate
7653 // NB: we synthesize a new token here rather than reusing
7654 // e->tok, because print_format::print likes to use
7655 // its tok->content.
7656 token* pf_tok = new token(*e->tok);
7657 pf_tok->content = "sprintf";
0a6f5a3f 7658
d5e178c1 7659 print_format* pf = print_format::create(pf_tok);
0a6f5a3f 7660
f8a968bc 7661 for (unsigned i = 0; i < args.size(); ++i)
b278033a 7662 {
dcaa1a65
JS
7663 if (!args[i].usable)
7664 continue;
f8a968bc
JS
7665 if (i > 0)
7666 pf->raw_components += " ";
7667 pf->raw_components += args[i].name;
3e3bd7b6 7668 target_symbol *tsym = new target_symbol;
f8a968bc 7669 tsym->tok = e->tok;
277c21bc 7670 tsym->name = "$" + args[i].name;
5f36109e 7671 tsym->components = e->components;
b278033a 7672
f8a968bc
JS
7673 // every variable should always be accessible!
7674 tsym->saved_conversion_error = 0;
7675 expression *texp = require (tsym); // NB: throws nothing ...
14a97852
JS
7676 if (tsym->saved_conversion_error) // ... but this is how we know it happened.
7677 {
7678 if (dw.sess.verbose>2)
7679 for (semantic_error *c = tsym->saved_conversion_error;
7680 c != 0; c = c->chain)
7681 clog << "variable location problem: " << c->what() << endl;
7682 pf->raw_components += "=?";
7683 continue;
7684 }
b278033a 7685
5f36109e
JS
7686 if (!e->components.empty() &&
7687 e->components[0].type == target_symbol::comp_pretty_print)
7688 pf->raw_components += "=%s";
7689 else
7690 pf->raw_components += args[i].isptr ? "=%p" : "=%#x";
f8a968bc
JS
7691 pf->args.push_back(texp);
7692 }
0a6f5a3f 7693
f8a968bc
JS
7694 pf->components = print_format::string_to_components(pf->raw_components);
7695 provide (pf);
b278033a 7696 }
f8a968bc
JS
7697 else
7698 assert(0); // shouldn't get here
0a6f5a3f
JS
7699}
7700
0a6f5a3f 7701void
f8a968bc 7702tracepoint_var_expanding_visitor::visit_target_symbol (target_symbol* e)
0a6f5a3f 7703{
aff5d390 7704 try
c69a87e0 7705 {
277c21bc 7706 assert(e->name.size() > 0 && e->name[0] == '$');
aff5d390 7707
277c21bc 7708 if (e->name == "$$name" || e->name == "$$parms" || e->name == "$$vars")
c69a87e0
FCE
7709 visit_target_symbol_context (e);
7710 else
7711 visit_target_symbol_arg (e);
7712 }
7713 catch (const semantic_error &er)
7714 {
1af1e62d 7715 e->chain (er);
c69a87e0
FCE
7716 provide (e);
7717 }
0a6f5a3f
JS
7718}
7719
7720
7721
79189b84
JS
7722tracepoint_derived_probe::tracepoint_derived_probe (systemtap_session& s,
7723 dwflpp& dw, Dwarf_Die& func_die,
7724 const string& tracepoint_name,
7725 probe* base, probe_point* loc):
4c5d1300 7726 derived_probe (base, loc, true /* .components soon rewritten */),
79189b84 7727 sess (s), tracepoint_name (tracepoint_name)
56894e91 7728{
79189b84
JS
7729 // create synthetic probe point name; preserve condition
7730 vector<probe_point::component*> comps;
7731 comps.push_back (new probe_point::component (TOK_KERNEL));
7732 comps.push_back (new probe_point::component (TOK_TRACE, new literal_string (tracepoint_name)));
7733 this->sole_location()->components = comps;
7734
6fb70fb7
JS
7735 // fill out the available arguments in this tracepoint
7736 build_args(dw, func_die);
56894e91 7737
6fb70fb7
JS
7738 // determine which header defined this tracepoint
7739 string decl_file = dwarf_decl_file(&func_die);
d4393459
FCE
7740 header = decl_file;
7741
7742#if 0 /* This convention is not enforced. */
6fb70fb7
JS
7743 size_t header_pos = decl_file.rfind("trace/");
7744 if (header_pos == string::npos)
7745 throw semantic_error ("cannot parse header location for tracepoint '"
7746 + tracepoint_name + "' in '"
7747 + decl_file + "'");
7748 header = decl_file.substr(header_pos);
d4393459 7749#endif
56894e91 7750
6fb70fb7
JS
7751 // tracepoints from FOO_event_types.h should really be included from FOO.h
7752 // XXX can dwarf tell us the include hierarchy? it would be better to
7753 // ... walk up to see which one was directly included by tracequery.c
3c1b3d06 7754 // XXX: see also PR9993.
d4393459 7755 size_t header_pos = header.find("_event_types");
6fb70fb7
JS
7756 if (header_pos != string::npos)
7757 header.erase(header_pos, 12);
56894e91 7758
f8a968bc
JS
7759 // Now expand the local variables in the probe body
7760 tracepoint_var_expanding_visitor v (dw, name, args);
8b095b45 7761 v.replace (this->body);
a45664f4
JS
7762 for (unsigned i = 0; i < args.size(); i++)
7763 if (args[i].used)
7764 {
7765 vardecl* v = new vardecl;
7766 v->name = "__tracepoint_arg_" + args[i].name;
7767 v->tok = this->tok;
58701b78 7768 v->set_arity(0, this->tok);
a45664f4
JS
7769 v->type = pe_long;
7770 v->skip_init = true;
7771 this->locals.push_back (v);
7772 }
56894e91 7773
79189b84
JS
7774 if (sess.verbose > 2)
7775 clog << "tracepoint-based " << name << " tracepoint='" << tracepoint_name
7776 << "'" << endl;
7777}
dc38c0ae 7778
56894e91 7779
f8a968bc 7780static bool
dcaa1a65 7781resolve_tracepoint_arg_type(tracepoint_arg& arg)
46b84a80 7782{
d19a9a82 7783 Dwarf_Die type;
dcaa1a65 7784 switch (dwarf_tag(&arg.type_die))
b20febf3 7785 {
f8a968bc
JS
7786 case DW_TAG_typedef:
7787 case DW_TAG_const_type:
7788 case DW_TAG_volatile_type:
7789 // iterate on the referent type
3d1ad340 7790 return (dwarf_attr_die(&arg.type_die, DW_AT_type, &arg.type_die)
dcaa1a65 7791 && resolve_tracepoint_arg_type(arg));
f8a968bc
JS
7792 case DW_TAG_base_type:
7793 // base types will simply be treated as script longs
dcaa1a65 7794 arg.isptr = false;
f8a968bc
JS
7795 return true;
7796 case DW_TAG_pointer_type:
dcaa1a65
JS
7797 // pointers can be treated as script longs,
7798 // and if we know their type, they can also be dereferenced
d19a9a82
JS
7799 type = arg.type_die;
7800 while (dwarf_attr_die(&arg.type_die, DW_AT_type, &arg.type_die))
7801 {
7802 // It still might be a non-type, e.g. const void,
7803 // so we need to strip away all qualifiers.
7804 int tag = dwarf_tag(&arg.type_die);
7805 if (tag != DW_TAG_typedef &&
7806 tag != DW_TAG_const_type &&
7807 tag != DW_TAG_volatile_type)
7808 {
7809 arg.isptr = true;
7810 break;
7811 }
7812 }
7813 if (!arg.isptr)
7814 arg.type_die = type;
ad370dcc
JS
7815 arg.typecast = "(intptr_t)";
7816 return true;
7817 case DW_TAG_structure_type:
7818 case DW_TAG_union_type:
7819 // for structs/unions which are passed by value, we turn it into
7820 // a pointer that can be dereferenced.
7821 arg.isptr = true;
7822 arg.typecast = "(intptr_t)&";
dcaa1a65 7823 return true;
f8a968bc
JS
7824 default:
7825 // should we consider other types too?
7826 return false;
b20febf3 7827 }
56894e91
JS
7828}
7829
7830
7831void
6fb70fb7 7832tracepoint_derived_probe::build_args(dwflpp& dw, Dwarf_Die& func_die)
56894e91 7833{
6fb70fb7
JS
7834 Dwarf_Die arg;
7835 if (dwarf_child(&func_die, &arg) == 0)
7836 do
7837 if (dwarf_tag(&arg) == DW_TAG_formal_parameter)
7838 {
7839 // build a tracepoint_arg for this parameter
7840 tracepoint_arg tparg;
23d106b9 7841 tparg.name = dwarf_diename(&arg);
56894e91 7842
6fb70fb7 7843 // read the type of this parameter
3d1ad340 7844 if (!dwarf_attr_die (&arg, DW_AT_type, &tparg.type_die)
f1c8f8a5 7845 || !dwarf_type_name(&tparg.type_die, tparg.c_type))
6fb70fb7
JS
7846 throw semantic_error ("cannot get type of tracepoint '"
7847 + tracepoint_name + "' parameter '"
7848 + tparg.name + "'");
a68f81a2 7849
dcaa1a65 7850 tparg.usable = resolve_tracepoint_arg_type(tparg);
6fb70fb7
JS
7851 args.push_back(tparg);
7852 if (sess.verbose > 4)
7853 clog << "found parameter for tracepoint '" << tracepoint_name
7854 << "': type:'" << tparg.c_type
7855 << "' name:'" << tparg.name << "'" << endl;
7856 }
7857 while (dwarf_siblingof(&arg, &arg) == 0);
56894e91
JS
7858}
7859
dc38c0ae 7860void
d0bfd2ac 7861tracepoint_derived_probe::getargs(std::list<std::string> &arg_set) const
dc38c0ae 7862{
dcaa1a65
JS
7863 for (unsigned i = 0; i < args.size(); ++i)
7864 if (args[i].usable)
d0bfd2ac 7865 arg_set.push_back("$"+args[i].name+":"+args[i].c_type);
dc38c0ae
DS
7866}
7867
79189b84
JS
7868void
7869tracepoint_derived_probe::join_group (systemtap_session& s)
197a4d62 7870{
79189b84
JS
7871 if (! s.tracepoint_derived_probes)
7872 s.tracepoint_derived_probes = new tracepoint_derived_probe_group ();
7873 s.tracepoint_derived_probes->enroll (this);
7874}
e38d6504 7875
56894e91 7876
197a4d62 7877void
3e3bd7b6 7878tracepoint_derived_probe::print_dupe_stamp(ostream& o)
56894e91 7879{
3e3bd7b6
JS
7880 for (unsigned i = 0; i < args.size(); i++)
7881 if (args[i].used)
7882 o << "__tracepoint_arg_" << args[i].name << endl;
197a4d62 7883}
56894e91 7884
3e3bd7b6 7885
d4393459 7886static vector<string> tracepoint_extra_decls (systemtap_session& s)
47dd066d 7887{
3c1b3d06
FCE
7888 vector<string> they_live;
7889 // PR 9993
7890 // XXX: may need this to be configurable
d4393459 7891 they_live.push_back ("#include <linux/skbuff.h>");
9e0cd21a
FCE
7892
7893 // PR11649: conditional extra header
7894 // for kvm tracepoints in 2.6.33ish
7895 if (s.kernel_config["CONFIG_KVM"] != string("")) {
d4393459
FCE
7896 they_live.push_back ("#include <linux/kvm_host.h>");
7897 }
7898
7899 if (s.kernel_config["CONFIG_XFS_FS"] != string("")) {
7900 they_live.push_back ("#define XFS_BIG_BLKNOS 1");
88637c31
FCE
7901 if (s.kernel_source_tree != "")
7902 they_live.push_back ("#include \"fs/xfs/xfs_types.h\""); // in kernel-source tree
d4393459
FCE
7903 they_live.push_back ("struct xfs_mount;");
7904 they_live.push_back ("struct xfs_inode;");
7905 they_live.push_back ("struct xfs_buf;");
7906 they_live.push_back ("struct xfs_bmbt_irec;");
9e0cd21a 7907 }
d4393459
FCE
7908
7909 if (s.kernel_config["CONFIG_NFSD"] != string("")) {
7910 they_live.push_back ("struct rpc_task;");
7911 }
7912
7913 they_live.push_back ("#include <asm/cputime.h>");
7914
3c1b3d06
FCE
7915 return they_live;
7916}
47dd066d
WC
7917
7918
7919void
79189b84 7920tracepoint_derived_probe_group::emit_module_decls (systemtap_session& s)
47dd066d 7921{
79189b84
JS
7922 if (probes.empty())
7923 return;
47dd066d 7924
96b030fe
JS
7925 s.op->newline() << "/* ---- tracepoint probes ---- */";
7926 s.op->newline();
79189b84 7927
3c1b3d06
FCE
7928 // PR9993: Add extra headers to work around undeclared types in individual
7929 // include/trace/foo.h files
d4393459
FCE
7930 const vector<string>& extra_decls = tracepoint_extra_decls (s);
7931 for (unsigned z=0; z<extra_decls.size(); z++)
7932 s.op->newline() << extra_decls[z] << "\n";
47dd066d 7933
6fb70fb7
JS
7934 for (unsigned i = 0; i < probes.size(); ++i)
7935 {
7936 tracepoint_derived_probe *p = probes[i];
47dd066d 7937
96b030fe
JS
7938 // emit a separate entry function for each probe, since tracepoints
7939 // don't provide any sort of context pointer.
392e08b7 7940 s.op->newline() << "#undef TRACE_INCLUDE_FILE";
d4393459 7941 s.op->newline() << "#undef TRACE_INCLUDE_PATH";
6fb70fb7 7942 s.op->newline() << "#include <" << p->header << ">";
5f73a260
JS
7943
7944 // Starting in 2.6.35, at the same time NOARGS was added, the callback
7945 // always has a void* as the first parameter. PR11599
7946 s.op->newline() << "#ifdef DECLARE_TRACE_NOARGS";
7947 s.op->newline() << "#define STAP_TP_DATA , NULL";
7948 s.op->newline() << "#define STAP_TP_PROTO void *cb_data"
7949 << " __attribute__ ((unused))";
7950 if (!p->args.empty())
7951 s.op->line() << ",";
7952 s.op->newline() << "#else";
7953 s.op->newline() << "#define STAP_TP_DATA";
7954 s.op->newline() << "#define STAP_TP_PROTO";
7955 if (p->args.empty())
7956 s.op->line() << " void";
7957 s.op->newline() << "#endif";
7958
6fb70fb7 7959 s.op->newline() << "static void enter_tracepoint_probe_" << i << "(";
5f73a260
JS
7960 s.op->newline(2) << "STAP_TP_PROTO";
7961
6fb70fb7
JS
7962 for (unsigned j = 0; j < p->args.size(); ++j)
7963 {
7964 if (j > 0)
5f73a260
JS
7965 s.op->line() << ",";
7966 s.op->newline() << p->args[j].c_type << " __tracepoint_arg_" << p->args[j].name;
6fb70fb7 7967 }
5f73a260
JS
7968 s.op->newline() << ")";
7969 s.op->newline(-2) << "{";
7970
26e63673 7971 s.op->newline(1) << "struct stap_probe * const probe = "
faea5e16
JS
7972 << common_probe_init (p) << ";";
7973 common_probe_entryfn_prologue (s.op, "STAP_SESSION_RUNNING", "probe");
f8a968bc 7974 s.op->newline() << "c->marker_name = "
c12d974f
FCE
7975 << lex_cast_qstring (p->tracepoint_name)
7976 << ";";
f8a968bc
JS
7977 for (unsigned j = 0; j < p->args.size(); ++j)
7978 if (p->args[j].used)
7979 {
66671fd8 7980 s.op->newline() << "c->probe_locals." << p->name << ".__tracepoint_arg_"
f8a968bc 7981 << p->args[j].name << " = (int64_t)";
ad370dcc 7982 s.op->line() << p->args[j].typecast;
f8a968bc
JS
7983 s.op->line() << "__tracepoint_arg_" << p->args[j].name << ";";
7984 }
26e63673 7985 s.op->newline() << "(*probe->ph) (c);";
6fb70fb7
JS
7986 common_probe_entryfn_epilogue (s.op);
7987 s.op->newline(-1) << "}";
47dd066d 7988
96b030fe
JS
7989 // emit normalized registration functions
7990 s.op->newline() << "static int register_tracepoint_probe_" << i << "(void) {";
7991 s.op->newline(1) << "return register_trace_" << p->tracepoint_name
5f73a260 7992 << "(enter_tracepoint_probe_" << i << " STAP_TP_DATA);";
96b030fe 7993 s.op->newline(-1) << "}";
47dd066d 7994
86758d5f
JS
7995 // NB: we're not prepared to deal with unreg failures. However, failures
7996 // can only occur if the tracepoint doesn't exist (yet?), or if we
7997 // weren't even registered. The former should be OKed by the initial
7998 // registration call, and the latter is safe to ignore.
7999 s.op->newline() << "static void unregister_tracepoint_probe_" << i << "(void) {";
8000 s.op->newline(1) << "(void) unregister_trace_" << p->tracepoint_name
5f73a260 8001 << "(enter_tracepoint_probe_" << i << " STAP_TP_DATA);";
96b030fe 8002 s.op->newline(-1) << "}";
6fb70fb7 8003 s.op->newline();
5f73a260
JS
8004
8005 s.op->newline() << "#undef STAP_TP_DATA";
8006 s.op->newline() << "#undef STAP_TP_PROTO";
8007 s.op->newline();
af304783
DS
8008 }
8009
96b030fe
JS
8010 // emit an array of registration functions for easy init/shutdown
8011 s.op->newline() << "static struct stap_tracepoint_probe {";
8012 s.op->newline(1) << "int (*reg)(void);";
86758d5f 8013 s.op->newline(0) << "void (*unreg)(void);";
96b030fe
JS
8014 s.op->newline(-1) << "} stap_tracepoint_probes[] = {";
8015 s.op->indent(1);
8016 for (unsigned i = 0; i < probes.size(); ++i)
8017 {
8018 s.op->newline () << "{";
8019 s.op->line() << " .reg=&register_tracepoint_probe_" << i << ",";
8020 s.op->line() << " .unreg=&unregister_tracepoint_probe_" << i;
8021 s.op->line() << " },";
8022 }
8023 s.op->newline(-1) << "};";
8024 s.op->newline();
47dd066d
WC
8025}
8026
8027
79189b84
JS
8028void
8029tracepoint_derived_probe_group::emit_module_init (systemtap_session &s)
47dd066d 8030{
79189b84
JS
8031 if (probes.size () == 0)
8032 return;
47dd066d 8033
79189b84 8034 s.op->newline() << "/* init tracepoint probes */";
96b030fe
JS
8035 s.op->newline() << "for (i=0; i<" << probes.size() << "; i++) {";
8036 s.op->newline(1) << "rc = stap_tracepoint_probes[i].reg();";
8037 s.op->newline() << "if (rc) {";
8038 s.op->newline(1) << "for (j=i-1; j>=0; j--)"; // partial rollback
8039 s.op->newline(1) << "stap_tracepoint_probes[j].unreg();";
8040 s.op->newline(-1) << "break;"; // don't attempt to register any more probes
8041 s.op->newline(-1) << "}";
8042 s.op->newline(-1) << "}";
47dd066d 8043
bc9a523d
FCE
8044 // This would be technically proper (on those autoconf-detectable
8045 // kernels that include this function in tracepoint.h), however we
8046 // already make several calls to synchronze_sched() during our
8047 // shutdown processes.
47dd066d 8048
bc9a523d
FCE
8049 // s.op->newline() << "if (rc)";
8050 // s.op->newline(1) << "tracepoint_synchronize_unregister();";
8051 // s.op->indent(-1);
79189b84 8052}
47dd066d
WC
8053
8054
79189b84
JS
8055void
8056tracepoint_derived_probe_group::emit_module_exit (systemtap_session& s)
47dd066d 8057{
79189b84
JS
8058 if (probes.empty())
8059 return;
47dd066d 8060
96b030fe
JS
8061 s.op->newline() << "/* deregister tracepoint probes */";
8062 s.op->newline() << "for (i=0; i<" << probes.size() << "; i++)";
8063 s.op->newline(1) << "stap_tracepoint_probes[i].unreg();";
8064 s.op->indent(-1);
47dd066d 8065
bc9a523d 8066 // Not necessary: see above.
47dd066d 8067
bc9a523d 8068 // s.op->newline() << "tracepoint_synchronize_unregister();";
79189b84 8069}
b20febf3 8070
47dd066d 8071
75ead1f7 8072struct tracepoint_query : public base_query
47dd066d 8073{
75ead1f7
JS
8074 tracepoint_query(dwflpp & dw, const string & tracepoint,
8075 probe * base_probe, probe_point * base_loc,
8076 vector<derived_probe *> & results):
8077 base_query(dw, "*"), tracepoint(tracepoint),
8078 base_probe(base_probe), base_loc(base_loc),
8079 results(results) {}
47dd066d 8080
75ead1f7 8081 const string& tracepoint;
47dd066d 8082
75ead1f7
JS
8083 probe * base_probe;
8084 probe_point * base_loc;
8085 vector<derived_probe *> & results;
f982c59b 8086 set<string> probed_names;
47dd066d 8087
75ead1f7
JS
8088 void handle_query_module();
8089 int handle_query_cu(Dwarf_Die * cudie);
8090 int handle_query_func(Dwarf_Die * func);
b20febf3 8091
75ead1f7
JS
8092 static int tracepoint_query_cu (Dwarf_Die * cudie, void * arg);
8093 static int tracepoint_query_func (Dwarf_Die * func, base_query * query);
8094};
47dd066d
WC
8095
8096
8097void
75ead1f7 8098tracepoint_query::handle_query_module()
47dd066d 8099{
75ead1f7
JS
8100 // look for the tracepoints in each CU
8101 dw.iterate_over_cus(tracepoint_query_cu, this);
47dd066d
WC
8102}
8103
8104
75ead1f7
JS
8105int
8106tracepoint_query::handle_query_cu(Dwarf_Die * cudie)
47dd066d 8107{
75ead1f7 8108 dw.focus_on_cu (cudie);
47dd066d 8109
75ead1f7
JS
8110 // look at each function to see if it's a tracepoint
8111 string function = "stapprobe_" + tracepoint;
8112 return dw.iterate_over_functions (tracepoint_query_func, this, function);
47dd066d
WC
8113}
8114
8115
75ead1f7
JS
8116int
8117tracepoint_query::handle_query_func(Dwarf_Die * func)
47dd066d 8118{
75ead1f7 8119 dw.focus_on_function (func);
47dd066d 8120
60d98537 8121 assert(startswith(dw.function_name, "stapprobe_"));
75ead1f7 8122 string tracepoint_instance = dw.function_name.substr(10);
f982c59b
JS
8123
8124 // check for duplicates -- sometimes tracepoint headers may be indirectly
8125 // included in more than one of our tracequery modules.
8126 if (!probed_names.insert(tracepoint_instance).second)
8127 return DWARF_CB_OK;
8128
79189b84
JS
8129 derived_probe *dp = new tracepoint_derived_probe (dw.sess, dw, *func,
8130 tracepoint_instance,
8131 base_probe, base_loc);
8132 results.push_back (dp);
75ead1f7 8133 return DWARF_CB_OK;
47dd066d
WC
8134}
8135
8136
75ead1f7
JS
8137int
8138tracepoint_query::tracepoint_query_cu (Dwarf_Die * cudie, void * arg)
47dd066d 8139{
75ead1f7 8140 tracepoint_query * q = static_cast<tracepoint_query *>(arg);
85007c04 8141 if (pending_interrupts) return DWARF_CB_ABORT;
75ead1f7 8142 return q->handle_query_cu(cudie);
47dd066d
WC
8143}
8144
8145
75ead1f7
JS
8146int
8147tracepoint_query::tracepoint_query_func (Dwarf_Die * func, base_query * query)
47dd066d 8148{
75ead1f7 8149 tracepoint_query * q = static_cast<tracepoint_query *>(query);
85007c04 8150 if (pending_interrupts) return DWARF_CB_ABORT;
75ead1f7 8151 return q->handle_query_func(func);
47dd066d
WC
8152}
8153
8154
0a6f5a3f 8155struct tracepoint_builder: public derived_probe_builder
47dd066d 8156{
0a6f5a3f
JS
8157private:
8158 dwflpp *dw;
8159 bool init_dw(systemtap_session& s);
55e50c24
JS
8160 string get_tracequery_module(systemtap_session& s,
8161 const vector<string>& headers);
47dd066d 8162
0a6f5a3f 8163public:
47dd066d 8164
0a6f5a3f
JS
8165 tracepoint_builder(): dw(0) {}
8166 ~tracepoint_builder() { delete dw; }
47dd066d 8167
0a6f5a3f
JS
8168 void build_no_more (systemtap_session& s)
8169 {
8170 if (dw && s.verbose > 3)
8171 clog << "tracepoint_builder releasing dwflpp" << endl;
8172 delete dw;
8173 dw = NULL;
8174 }
47dd066d 8175
0a6f5a3f
JS
8176 void build(systemtap_session& s,
8177 probe *base, probe_point *location,
8178 literal_map_t const& parameters,
8179 vector<derived_probe*>& finished_results);
8180};
47dd066d 8181
47dd066d 8182
f982c59b
JS
8183string
8184tracepoint_builder::get_tracequery_module(systemtap_session& s,
55e50c24 8185 const vector<string>& headers)
0a6f5a3f 8186{
c95eddf7 8187 if (s.verbose > 2)
55e50c24
JS
8188 {
8189 clog << "Pass 2: getting a tracequery for "
8190 << headers.size() << " headers:" << endl;
8191 for (size_t i = 0; i < headers.size(); ++i)
8192 clog << " " << headers[i] << endl;
8193 }
c95eddf7 8194
a2639cb7 8195 string tracequery_path;
b278033a
JS
8196 if (s.use_cache)
8197 {
8198 // see if the cached module exists
55e50c24 8199 tracequery_path = find_tracequery_hash(s, headers);
d105f664 8200 if (!tracequery_path.empty() && !s.poison_cache)
b278033a 8201 {
a2639cb7 8202 int fd = open(tracequery_path.c_str(), O_RDONLY);
b278033a
JS
8203 if (fd != -1)
8204 {
8205 if (s.verbose > 2)
a2639cb7 8206 clog << "Pass 2: using cached " << tracequery_path << endl;
b278033a 8207 close(fd);
f982c59b 8208 return tracequery_path;
b278033a
JS
8209 }
8210 }
8211 }
47dd066d 8212
b278033a 8213 // no cached module, time to make it
f982c59b 8214
55e50c24
JS
8215 // PR9993: Add extra headers to work around undeclared types in individual
8216 // include/trace/foo.h files
d4393459 8217 vector<string> short_decls = tracepoint_extra_decls(s);
55e50c24
JS
8218
8219 // add each requested tracepoint header
8220 for (size_t i = 0; i < headers.size(); ++i)
8221 {
8222 const string &header = headers[i];
8223 size_t root_pos = header.rfind("/include/");
d4393459
FCE
8224 short_decls.push_back(string("#include <") +
8225 ((root_pos != string::npos) ? header.substr(root_pos + 9) : header) +
8226 string(">"));
55e50c24 8227 }
f982c59b 8228
0a6f5a3f 8229 string tracequery_ko;
d4393459 8230 int rc = make_tracequery(s, tracequery_ko, short_decls);
0a6f5a3f 8231 if (rc != 0)
c95eddf7 8232 tracequery_ko = "/dev/null";
47dd066d 8233
e16dc041 8234 // try to save tracequery in the cache
b278033a 8235 if (s.use_cache)
e16dc041
JS
8236 copy_file(tracequery_ko, tracequery_path, s.verbose > 2);
8237
f982c59b
JS
8238 return tracequery_ko;
8239}
8240
8241
d4393459 8242
f982c59b
JS
8243bool
8244tracepoint_builder::init_dw(systemtap_session& s)
8245{
8246 if (dw != NULL)
8247 return true;
8248
8249 vector<string> tracequery_modules;
55e50c24 8250 vector<string> system_headers;
f982c59b
JS
8251
8252 glob_t trace_glob;
d4393459
FCE
8253
8254 // find kernel_source_tree
8255 if (s.kernel_source_tree == "")
f982c59b 8256 {
d4393459
FCE
8257 unsigned found;
8258 DwflPtr dwfl_ptr = setup_dwfl_kernel ("kernel", &found, s);
8259 Dwfl *dwfl = dwfl_ptr.get()->dwfl;
8260 if (found)
8261 {
8262 Dwarf_Die *cudie = 0;
8263 Dwarf_Addr bias;
8264 while ((cudie = dwfl_nextcu (dwfl, cudie, &bias)) != NULL)
8265 {
8266 if (pending_interrupts) break;
8267 Dwarf_Attribute attr;
8268 const char* name = dwarf_formstring (dwarf_attr (cudie, DW_AT_comp_dir, &attr));
8269 if (name)
8270 {
61f1a63b
FCE
8271 if (s.verbose > 2)
8272 clog << "Located kernel source tree (DW_AT_comp_dir) at '" << name << "'" << endl;
8273
d4393459
FCE
8274 s.kernel_source_tree = name;
8275 break; // skip others; modern Kbuild uses same comp_dir for them all
8276 }
8277 }
8278 }
8279 }
8280
8281 // prefixes
8282 vector<string> glob_prefixes;
8283 glob_prefixes.push_back (s.kernel_build_tree);
8284 if (s.kernel_source_tree != "")
8285 glob_prefixes.push_back (s.kernel_source_tree);
8286
8287 // suffixes
8288 vector<string> glob_suffixes;
8289 glob_suffixes.push_back("include/trace/events/*.h");
8290 glob_suffixes.push_back("include/trace/*.h");
8291 glob_suffixes.push_back("arch/x86/kvm/*trace.h");
8292 glob_suffixes.push_back("fs/xfs/linux-2.6/xfs_tr*.h");
8293
8294 // compute cartesian product
8295 vector<string> globs;
8296 for (unsigned i=0; i<glob_prefixes.size(); i++)
8297 for (unsigned j=0; j<glob_suffixes.size(); j++)
8298 globs.push_back (glob_prefixes[i]+string("/")+glob_suffixes[j]);
8299
8300 for (unsigned z = 0; z < globs.size(); z++)
8301 {
8302 string glob_str = globs[z];
8303 if (s.verbose > 3)
8304 clog << "Checking tracepoint glob " << glob_str << endl;
8305
f982c59b
JS
8306 glob(glob_str.c_str(), 0, NULL, &trace_glob);
8307 for (unsigned i = 0; i < trace_glob.gl_pathc; ++i)
8308 {
8309 string header(trace_glob.gl_pathv[i]);
8310
8311 // filter out a few known "internal-only" headers
60d98537
JS
8312 if (endswith(header, "/define_trace.h") ||
8313 endswith(header, "/ftrace.h") ||
8314 endswith(header, "/trace_events.h") ||
8315 endswith(header, "_event_types.h"))
f982c59b
JS
8316 continue;
8317
55e50c24 8318 system_headers.push_back(header);
f982c59b
JS
8319 }
8320 globfree(&trace_glob);
8321 }
8322
55e50c24
JS
8323 // First attempt to do all system headers in one go
8324 string tracequery_path = get_tracequery_module(s, system_headers);
8325 // NB: An empty tracequery means that the header didn't compile correctly
8326 if (get_file_size(tracequery_path))
8327 tracequery_modules.push_back(tracequery_path);
8328 else
8329 // Otherwise try to do them one at a time (PR10424)
8330 for (size_t i = 0; i < system_headers.size(); ++i)
8331 {
85007c04 8332 if (pending_interrupts) return false;
55e50c24
JS
8333 vector<string> one_header(1, system_headers[i]);
8334 tracequery_path = get_tracequery_module(s, one_header);
8335 if (get_file_size(tracequery_path))
8336 tracequery_modules.push_back(tracequery_path);
8337 }
8338
f982c59b
JS
8339 // TODO: consider other sources of tracepoint headers too, like from
8340 // a command-line parameter or some environment or .systemtaprc
47dd066d 8341
59c11f91 8342 dw = new dwflpp(s, tracequery_modules, true);
0a6f5a3f
JS
8343 return true;
8344}
47dd066d 8345
0a6f5a3f
JS
8346void
8347tracepoint_builder::build(systemtap_session& s,
8348 probe *base, probe_point *location,
8349 literal_map_t const& parameters,
8350 vector<derived_probe*>& finished_results)
8351{
8352 if (!init_dw(s))
8353 return;
47dd066d 8354
75ead1f7
JS
8355 string tracepoint;
8356 assert(get_param (parameters, TOK_TRACE, tracepoint));
47dd066d 8357
75ead1f7 8358 tracepoint_query q(*dw, tracepoint, base, location, finished_results);
51178501 8359 dw->iterate_over_modules(&query_module, &q);
47dd066d 8360}
47dd066d 8361
e6fe60e7 8362
b55bc428 8363// ------------------------------------------------------------------------
bd2b1e68 8364// Standard tapset registry.
b55bc428
FCE
8365// ------------------------------------------------------------------------
8366
7a053d3b 8367void
f8220a7b 8368register_standard_tapsets(systemtap_session & s)
b55bc428 8369{
47e0478e 8370 register_tapset_been(s);
93646f4d 8371 register_tapset_itrace(s);
dd0e4fa7 8372 register_tapset_mark(s);
7a212aa8 8373 register_tapset_procfs(s);
912e8c59 8374 register_tapset_timers(s);
b84779a5 8375 register_tapset_utrace(s);
b98a8d73 8376
7a24d422 8377 // dwarf-based kprobe/uprobe parts
c4ce66a1 8378 dwarf_derived_probe::register_patterns(s);
30a279be 8379
888af770
FCE
8380 // XXX: user-space starter set
8381 s.pattern_root->bind_num(TOK_PROCESS)
8382 ->bind_num(TOK_STATEMENT)->bind(TOK_ABSOLUTE)
27dc09b1 8383 ->bind_unprivileged()
888af770
FCE
8384 ->bind(new uprobe_builder ());
8385 s.pattern_root->bind_num(TOK_PROCESS)
8386 ->bind_num(TOK_STATEMENT)->bind(TOK_ABSOLUTE)->bind(TOK_RETURN)
27dc09b1 8387 ->bind_unprivileged()
888af770
FCE
8388 ->bind(new uprobe_builder ());
8389
0a6f5a3f
JS
8390 // kernel tracepoint probes
8391 s.pattern_root->bind(TOK_KERNEL)->bind_str(TOK_TRACE)
8392 ->bind(new tracepoint_builder());
8393
e6fe60e7
AM
8394 // Kprobe based probe
8395 s.pattern_root->bind(TOK_KPROBE)->bind_str(TOK_FUNCTION)
8396 ->bind(new kprobe_builder());
8397 s.pattern_root->bind(TOK_KPROBE)->bind_str(TOK_MODULE)
8398 ->bind_str(TOK_FUNCTION)->bind(new kprobe_builder());
8399 s.pattern_root->bind(TOK_KPROBE)->bind_str(TOK_FUNCTION)->bind(TOK_RETURN)
8400 ->bind(new kprobe_builder());
b6371390
JS
8401 s.pattern_root->bind(TOK_KPROBE)->bind_str(TOK_FUNCTION)->bind(TOK_RETURN)
8402 ->bind_num(TOK_MAXACTIVE)->bind(new kprobe_builder());
e6fe60e7
AM
8403 s.pattern_root->bind(TOK_KPROBE)->bind_str(TOK_MODULE)
8404 ->bind_str(TOK_FUNCTION)->bind(TOK_RETURN)->bind(new kprobe_builder());
b6371390
JS
8405 s.pattern_root->bind(TOK_KPROBE)->bind_str(TOK_MODULE)
8406 ->bind_str(TOK_FUNCTION)->bind(TOK_RETURN)
8407 ->bind_num(TOK_MAXACTIVE)->bind(new kprobe_builder());
e6fe60e7
AM
8408 s.pattern_root->bind(TOK_KPROBE)->bind_num(TOK_STATEMENT)
8409 ->bind(TOK_ABSOLUTE)->bind(new kprobe_builder());
dd225250
PS
8410
8411 //Hwbkpt based probe
b47f3a55
FCE
8412 // NB: we formerly registered the probe point types only if the kernel configuration
8413 // allowed it. However, we get better error messages if we allow probes to resolve.
8414 s.pattern_root->bind(TOK_KERNEL)->bind_num(TOK_HWBKPT)
8415 ->bind(TOK_HWBKPT_WRITE)->bind(new hwbkpt_builder());
8416 s.pattern_root->bind(TOK_KERNEL)->bind_str(TOK_HWBKPT)
8417 ->bind(TOK_HWBKPT_WRITE)->bind(new hwbkpt_builder());
8418 s.pattern_root->bind(TOK_KERNEL)->bind_num(TOK_HWBKPT)
8419 ->bind(TOK_HWBKPT_RW)->bind(new hwbkpt_builder());
8420 s.pattern_root->bind(TOK_KERNEL)->bind_str(TOK_HWBKPT)
8421 ->bind(TOK_HWBKPT_RW)->bind(new hwbkpt_builder());
8422 s.pattern_root->bind(TOK_KERNEL)->bind_num(TOK_HWBKPT)
8423 ->bind_num(TOK_LENGTH)->bind(TOK_HWBKPT_WRITE)->bind(new hwbkpt_builder());
8424 s.pattern_root->bind(TOK_KERNEL)->bind_num(TOK_HWBKPT)
8425 ->bind_num(TOK_LENGTH)->bind(TOK_HWBKPT_RW)->bind(new hwbkpt_builder());
8426 // length supported with address only, not symbol names
83ea76b1
WC
8427
8428 //perf event based probe
4763f713 8429 register_tapset_perf(s);
b55bc428 8430}
dc38c0ae
DS
8431
8432
b20febf3
FCE
8433vector<derived_probe_group*>
8434all_session_groups(systemtap_session& s)
dc38c0ae 8435{
b20febf3 8436 vector<derived_probe_group*> g;
912e8c59
JS
8437
8438#define DOONE(x) \
8439 if (s. x##_derived_probes) \
8440 g.push_back ((derived_probe_group*)(s. x##_derived_probes))
ab655cf8
DS
8441
8442 // Note that order *is* important here. We want to make sure we
8443 // register (actually run) begin probes before any other probe type
8444 // is run. Similarly, when unregistering probes, we want to
8445 // unregister (actually run) end probes after every other probe type
8446 // has be unregistered. To do the latter,
8447 // c_unparser::emit_module_exit() will run this list backwards.
b20febf3
FCE
8448 DOONE(be);
8449 DOONE(dwarf);
888af770 8450 DOONE(uprobe);
b20febf3
FCE
8451 DOONE(timer);
8452 DOONE(profile);
8453 DOONE(mark);
0a6f5a3f 8454 DOONE(tracepoint);
e6fe60e7 8455 DOONE(kprobe);
dd225250 8456 DOONE(hwbkpt);
83ea76b1 8457 DOONE(perf);
b20febf3 8458 DOONE(hrtimer);
ce82316f 8459 DOONE(procfs);
935447c8
DS
8460
8461 // Another "order is important" item. We want to make sure we
8462 // "register" the dummy task_finder probe group after all probe
8463 // groups that use the task_finder.
8464 DOONE(utrace);
a96d1db0 8465 DOONE(itrace);
935447c8 8466 DOONE(task_finder);
b20febf3
FCE
8467#undef DOONE
8468 return g;
46b84a80 8469}
73267b89
JS
8470
8471/* vim: set sw=2 ts=8 cino=>4,n-2,{2,^-2,t0,(0,u0,w1,M1 : */
This page took 2.131045 seconds and 5 git commands to generate.