]> sourceware.org Git - systemtap.git/blame - tapsets.cxx
Run clone/fork/vfork tests with and without semaphores.
[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 {
2491 Dwarf_Die *resolved = dw.declaration_resolve(dwarf_diename(type));
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
7f6b80bd 3460 Dwarf_Die* type_die = dw.declaration_resolve_other_cus(e.type_name.c_str());
ea1e477a
JS
3461 if (!type_die)
3462 return;
c4ce66a1 3463
5f36109e
JS
3464 string code;
3465 exp_type type = pe_long;
3466
ea1e477a 3467 try
c4ce66a1 3468 {
ea1e477a
JS
3469 Dwarf_Die cu_mem;
3470 dw.focus_on_cu(dwarf_diecu(type_die, &cu_mem, NULL, NULL));
5f36109e
JS
3471
3472 if (!e.components.empty() &&
3473 e.components.back().type == target_symbol::comp_pretty_print)
3474 {
3475 if (lvalue)
3476 throw semantic_error("cannot write to pretty-printed variable", e.tok);
3477
d19a9a82 3478 dwarf_pretty_print dpp(dw, type_die, e.operand, true, userspace_p, e);
5f36109e
JS
3479 result = dpp.expand();
3480 return;
3481 }
3482
3483 code = dw.literal_stmt_for_pointer (type_die, &e, lvalue, type);
ea1e477a
JS
3484 }
3485 catch (const semantic_error& er)
3486 {
3487 // NB: we can have multiple errors, since a @cast
1af1e62d
JS
3488 // may be attempted using several different modules:
3489 // @cast(ptr, "type", "module1:module2:...")
3490 e.chain (er);
c4ce66a1 3491 }
c4ce66a1 3492
5f36109e
JS
3493 if (code.empty())
3494 return;
c4ce66a1 3495
5f36109e 3496 string fname = (string(lvalue ? "_dwarf_cast_set" : "_dwarf_cast_get")
277c21bc 3497 + "_" + e.name.substr(1)
5f36109e 3498 + "_" + lex_cast(tick++));
c4ce66a1 3499
5f36109e
JS
3500 // Synthesize a function.
3501 functiondecl *fdecl = new functiondecl;
3502 fdecl->synthetic = true;
3503 fdecl->tok = e.tok;
3504 fdecl->type = type;
3505 fdecl->name = fname;
3506
3507 embeddedcode *ec = new embeddedcode;
3508 ec->tok = e.tok;
3509 fdecl->body = ec;
3510
3511 // PR10601: adapt to kernel-vs-userspace loc2c-runtime
3512 ec->code += "\n#define fetch_register " + string(userspace_p?"u":"k") + "_fetch_register\n";
3513 ec->code += "#define store_register " + string(userspace_p?"u":"k") + "_store_register\n";
3514
3515 ec->code += code;
3516
3517 // Give the fdecl an argument for the pointer we're trying to cast
3518 vardecl *v1 = new vardecl;
3519 v1->type = pe_long;
3520 v1->name = "pointer";
3521 v1->tok = e.tok;
3522 fdecl->formal_args.push_back(v1);
3523
3524 // Any non-literal indexes need to be passed in too.
3525 for (unsigned i = 0; i < e.components.size(); ++i)
3526 if (e.components[i].type == target_symbol::comp_expression_array_index)
3527 {
3528 vardecl *v = new vardecl;
3529 v->type = pe_long;
3530 v->name = "index" + lex_cast(i);
3531 v->tok = e.tok;
3532 fdecl->formal_args.push_back(v);
3533 }
3534
3535 if (lvalue)
3536 {
3537 // Modify the fdecl so it carries a second pe_long formal
3538 // argument called "value".
3539
3540 // FIXME: For the time being we only support setting target
3541 // variables which have base types; these are 'pe_long' in
3542 // stap's type vocabulary. Strings and pointers might be
3543 // reasonable, some day, but not today.
3544
3545 vardecl *v2 = new vardecl;
3546 v2->type = pe_long;
3547 v2->name = "value";
3548 v2->tok = e.tok;
3549 fdecl->formal_args.push_back(v2);
3550 }
3551 else
3552 ec->code += "/* pure */";
3553
3554 ec->code += "/* unprivileged */";
3555
3556 // PR10601
3557 ec->code += "\n#undef fetch_register\n";
3558 ec->code += "\n#undef store_register\n";
3559
f8809d54 3560 fdecl->join (dw.sess);
5f36109e
JS
3561
3562 // Synthesize a functioncall.
3563 functioncall* n = new functioncall;
3564 n->tok = e.tok;
3565 n->function = fname;
5f36109e
JS
3566 n->args.push_back(e.operand);
3567
3568 // Any non-literal indexes need to be passed in too.
3569 for (unsigned i = 0; i < e.components.size(); ++i)
3570 if (e.components[i].type == target_symbol::comp_expression_array_index)
3571 n->args.push_back(e.components[i].expr_index);
3572
3573 result = n;
3574}
c4ce66a1
JS
3575
3576
fb0274bc
JS
3577void dwarf_cast_expanding_visitor::filter_special_modules(string& module)
3578{
d90053e7 3579 // look for "<path/to/header>" or "kernel<path/to/header>"
fb0274bc 3580 // for those cases, build a module including that header
d90053e7 3581 if (module[module.size() - 1] == '>' &&
60d98537 3582 (module[0] == '<' || startswith(module, "kernel<")))
fb0274bc
JS
3583 {
3584 string cached_module;
3585 if (s.use_cache)
3586 {
3587 // see if the cached module exists
a2639cb7 3588 cached_module = find_typequery_hash(s, module);
d105f664 3589 if (!cached_module.empty() && !s.poison_cache)
fb0274bc
JS
3590 {
3591 int fd = open(cached_module.c_str(), O_RDONLY);
3592 if (fd != -1)
3593 {
3594 if (s.verbose > 2)
3595 clog << "Pass 2: using cached " << cached_module << endl;
3596 module = cached_module;
3597 close(fd);
3598 return;
3599 }
3600 }
3601 }
3602
3603 // no cached module, time to make it
d90053e7 3604 if (make_typequery(s, module) == 0)
fb0274bc 3605 {
e16dc041 3606 // try to save typequery in the cache
fb0274bc 3607 if (s.use_cache)
e16dc041 3608 copy_file(module, cached_module, s.verbose > 2);
fb0274bc
JS
3609 }
3610 }
3611}
3612
3613
c4ce66a1
JS
3614void dwarf_cast_expanding_visitor::visit_cast_op (cast_op* e)
3615{
3616 bool lvalue = is_active_lvalue(e);
3617 if (lvalue && !s.guru_mode)
3618 throw semantic_error("write to typecast value not permitted", e->tok);
3619
3620 if (e->module.empty())
3621 e->module = "kernel"; // "*" may also be reasonable to search all kernel modules
3622
5f36109e 3623 functioncall* result = NULL;
8b31197b
JS
3624
3625 // split the module string by ':' for alternatives
3626 vector<string> modules;
3627 tokenize(e->module, modules, ":");
b5a0dd41 3628 bool userspace_p=false; // PR10601
5f36109e 3629 for (unsigned i = 0; !result && i < modules.size(); ++i)
c4ce66a1 3630 {
8b31197b 3631 string& module = modules[i];
fb0274bc 3632 filter_special_modules(module);
abb41d92 3633
c4ce66a1
JS
3634 // NB: This uses '/' to distinguish between kernel modules and userspace,
3635 // which means that userspace modules won't get any PATH searching.
3636 dwflpp* dw;
707bf35e
JS
3637 try
3638 {
b5a0dd41
FCE
3639 userspace_p=is_user_module (module);
3640 if (! userspace_p)
707bf35e
JS
3641 {
3642 // kernel or kernel module target
ae2552da 3643 dw = db.get_kern_dw(s, module);
707bf35e
JS
3644 }
3645 else
3646 {
3647 module = find_executable (module); // canonicalize it
3648 dw = db.get_user_dw(s, module);
3649 }
3650 }
3651 catch (const semantic_error& er)
3652 {
3653 /* ignore and go to the next module */
3654 continue;
3655 }
c4ce66a1 3656
5f36109e 3657 dwarf_cast_query q (*dw, module, *e, lvalue, userspace_p, result);
51178501 3658 dw->iterate_over_modules(&query_module, &q);
c4ce66a1 3659 }
abb41d92 3660
5f36109e 3661 if (!result)
c4ce66a1 3662 {
946e1a48
JS
3663 // We pass the unresolved cast_op to the next pass, and hope
3664 // that this value ends up not being referenced after all, so
3665 // it can be optimized out quietly.
c4ce66a1
JS
3666 provide (e);
3667 return;
3668 }
3669
c4ce66a1
JS
3670 if (lvalue)
3671 {
3672 // Provide the functioncall to our parent, so that it can be
3673 // used to substitute for the assignment node immediately above
3674 // us.
3675 assert(!target_symbol_setter_functioncalls.empty());
5f36109e 3676 *(target_symbol_setter_functioncalls.top()) = result;
c4ce66a1
JS
3677 }
3678
5f36109e 3679 result->visit (this);
77de5e9e
GH
3680}
3681
3682
b8da0ad1
FCE
3683void
3684dwarf_derived_probe::printsig (ostream& o) const
3685{
3686 // Instead of just printing the plain locations, we add a PC value
3687 // as a comment as a way of telling e.g. apart multiple inlined
3688 // function instances. This is distinct from the verbose/clog
3689 // output, since this part goes into the cache hash calculations.
3690 sole_location()->print (o);
6d0f3f0c 3691 o << " /* pc=" << section << "+0x" << hex << addr << dec << " */";
b8da0ad1
FCE
3692 printsig_nested (o);
3693}
3694
3695
3696
dc38c0ae 3697void
b20febf3
FCE
3698dwarf_derived_probe::join_group (systemtap_session& s)
3699{
af234c40
JS
3700 // skip probes which are paired entry-handlers
3701 if (!has_return && (saved_longs || saved_strings))
3702 return;
3703
b20febf3
FCE
3704 if (! s.dwarf_derived_probes)
3705 s.dwarf_derived_probes = new dwarf_derived_probe_group ();
3706 s.dwarf_derived_probes->enroll (this);
b642c901
SC
3707
3708 enable_task_finder(s);
b20febf3
FCE
3709}
3710
3711
3712dwarf_derived_probe::dwarf_derived_probe(const string& funcname,
3713 const string& filename,
3714 int line,
91af0778 3715 // module & section specify a relocation
b20febf3
FCE
3716 // base for <addr>, unless section==""
3717 // (equivalently module=="kernel")
3718 const string& module,
3719 const string& section,
3720 // NB: dwfl_addr is the virtualized
3721 // address for this symbol.
3722 Dwarf_Addr dwfl_addr,
3723 // addr is the section-offset for
3724 // actual relocation.
3725 Dwarf_Addr addr,
3726 dwarf_query& q,
37ebca01 3727 Dwarf_Die* scope_die /* may be null */)
4c5d1300 3728 : derived_probe (q.base_probe, q.base_loc, true /* .components soon rewritten */ ),
b20febf3 3729 module (module), section (section), addr (addr),
63b4fd14 3730 path (q.path),
27dc09b1 3731 has_process (q.has_process),
c9bad430
DS
3732 has_return (q.has_return),
3733 has_maxactive (q.has_maxactive),
c57ea854 3734 has_library (q.has_library),
6b66b9f7 3735 maxactive_val (q.maxactive_val),
b642c901
SC
3736 user_path (q.user_path),
3737 user_lib (q.user_lib),
af234c40 3738 access_vars(false),
c57ea854 3739 saved_longs(0), saved_strings(0),
af234c40 3740 entry_handler(0)
bd2b1e68 3741{
b642c901
SC
3742 if (user_lib.size() != 0)
3743 has_library = true;
3744
6b66b9f7
JS
3745 if (q.has_process)
3746 {
3747 // We may receive probes on two types of ELF objects: ET_EXEC or ET_DYN.
3748 // ET_EXEC ones need no further relocation on the addr(==dwfl_addr), whereas
3749 // ET_DYN ones do (addr += run-time mmap base address). We tell these apart
3750 // by the incoming section value (".absolute" vs. ".dynamic").
3751 // XXX Assert invariants here too?
3752 }
3753 else
3754 {
3755 // Assert kernel relocation invariants
3756 if (section == "" && dwfl_addr != addr) // addr should be absolute
3757 throw semantic_error ("missing relocation base against", tok);
3758 if (section != "" && dwfl_addr == addr) // addr should be an offset
3759 throw semantic_error ("inconsistent relocation address", tok);
3760 }
2930abc7 3761
21beacc9
FCE
3762 // XXX: hack for strange g++/gcc's
3763#ifndef USHRT_MAX
3764#define USHRT_MAX 32767
3765#endif
3766
606fd9c8 3767 // Range limit maxactive() value
6b66b9f7 3768 if (has_maxactive && (maxactive_val < 0 || maxactive_val > USHRT_MAX))
606fd9c8 3769 throw semantic_error ("maxactive value out of range [0,"
aca66a36 3770 + lex_cast(USHRT_MAX) + "]",
f1a0157a 3771 q.base_loc->components.front()->tok);
606fd9c8 3772
de688825 3773 // Expand target variables in the probe body
5f0a03a6 3774 if (!null_die(scope_die))
8fc05e57 3775 {
6b66b9f7 3776 // XXX: user-space deref's for q.has_process!
de688825 3777 dwarf_var_expanding_visitor v (q, scope_die, dwfl_addr);
8b095b45 3778 v.replace (this->body);
6b66b9f7
JS
3779 if (!q.has_process)
3780 access_vars = v.visited;
37ebca01
FCE
3781
3782 // If during target-variable-expanding the probe, we added a new block
3783 // of code, add it to the start of the probe.
3784 if (v.add_block)
ba6f838d 3785 this->body = new block(v.add_block, this->body);
2260f4e3
FCE
3786
3787 // If when target-variable-expanding the probe, we need to synthesize a
3788 // sibling function-entry probe. We don't go through the whole probe derivation
3789 // business (PR10642) that could lead to wildcard/alias resolution, or for that
3790 // dwarf-induced duplication.
3791 if (v.add_call_probe)
37ebca01 3792 {
2260f4e3
FCE
3793 assert (q.has_return && !q.has_call);
3794
3795 // We temporarily replace q.base_probe.
3796 statement* old_body = q.base_probe->body;
3797 q.base_probe->body = v.add_call_probe;
3798 q.has_return = false;
3799 q.has_call = true;
af234c40 3800
da23eceb 3801 if (q.has_process)
af234c40
JS
3802 entry_handler = new uprobe_derived_probe (funcname, filename, line,
3803 module, section, dwfl_addr,
3804 addr, q, scope_die);
da23eceb 3805 else
af234c40
JS
3806 entry_handler = new dwarf_derived_probe (funcname, filename, line,
3807 module, section, dwfl_addr,
3808 addr, q, scope_die);
3809
3810 saved_longs = entry_handler->saved_longs = v.saved_longs;
3811 saved_strings = entry_handler->saved_strings = v.saved_strings;
3812
3813 q.results.push_back (entry_handler);
2260f4e3
FCE
3814
3815 q.has_return = true;
3816 q.has_call = false;
3817 q.base_probe->body = old_body;
37ebca01 3818 }
f10534c6
WH
3819 // Save the local variables for listing mode
3820 if (q.sess.listing_mode_vars)
8c67c337 3821 saveargs(q, scope_die, dwfl_addr);
8fc05e57 3822 }
37ebca01 3823 // else - null scope_die - $target variables will produce an error during translate phase
8fc05e57 3824
f10534c6 3825 // PR10820: null scope die, local variables aren't accessible, not necessary to invoke saveargs
0a98fd42 3826
5d23847d 3827 // Reset the sole element of the "locations" vector as a
b20febf3
FCE
3828 // "reverse-engineered" form of the incoming (q.base_loc) probe
3829 // point. This allows a user to see what function / file / line
3830 // number any particular match of the wildcards.
2930abc7 3831
a229fcd7 3832 vector<probe_point::component*> comps;
91af0778
FCE
3833 if (q.has_kernel)
3834 comps.push_back (new probe_point::component(TOK_KERNEL));
3835 else if(q.has_module)
3836 comps.push_back (new probe_point::component(TOK_MODULE, new literal_string(module)));
3837 else if(q.has_process)
3838 comps.push_back (new probe_point::component(TOK_PROCESS, new literal_string(module)));
3839 else
3840 assert (0);
b5d77020 3841
db520b00
FCE
3842 string fn_or_stmt;
3843 if (q.has_function_str || q.has_function_num)
3844 fn_or_stmt = "function";
3845 else
3846 fn_or_stmt = "statement";
a229fcd7 3847
b8da0ad1 3848 if (q.has_function_str || q.has_statement_str)
db520b00 3849 {
4cd232e4 3850 string retro_name = funcname;
b20febf3 3851 if (filename != "")
cee35f73 3852 {
fb84c077 3853 retro_name += ("@" + string (filename));
cee35f73 3854 if (line > 0)
aca66a36 3855 retro_name += (":" + lex_cast (line));
cee35f73 3856 }
db520b00
FCE
3857 comps.push_back
3858 (new probe_point::component
3859 (fn_or_stmt, new literal_string (retro_name)));
3860 }
b8da0ad1 3861 else if (q.has_function_num || q.has_statement_num)
db520b00
FCE
3862 {
3863 Dwarf_Addr retro_addr;
3864 if (q.has_function_num)
3865 retro_addr = q.function_num_val;
3866 else
3867 retro_addr = q.statement_num_val;
db520b00
FCE
3868 comps.push_back (new probe_point::component
3869 (fn_or_stmt,
9ea68eb9 3870 new literal_number(retro_addr, true)));
37ebca01
FCE
3871
3872 if (q.has_absolute)
3873 comps.push_back (new probe_point::component (TOK_ABSOLUTE));
a229fcd7
GH
3874 }
3875
b8da0ad1
FCE
3876 if (q.has_call)
3877 comps.push_back (new probe_point::component(TOK_CALL));
3878 if (q.has_inline)
3879 comps.push_back (new probe_point::component(TOK_INLINE));
db520b00 3880 if (has_return)
b8da0ad1
FCE
3881 comps.push_back (new probe_point::component(TOK_RETURN));
3882 if (has_maxactive)
3883 comps.push_back (new probe_point::component
3884 (TOK_MAXACTIVE, new literal_number(maxactive_val)));
d9b516ca 3885
5d23847d
FCE
3886 // Overwrite it.
3887 this->sole_location()->components = comps;
2930abc7
FCE
3888}
3889
bd2b1e68 3890
0a98fd42 3891void
8c67c337
JS
3892dwarf_derived_probe::saveargs(dwarf_query& q, Dwarf_Die* scope_die,
3893 Dwarf_Addr dwfl_addr)
0a98fd42 3894{
9aa8ffce 3895 if (null_die(scope_die))
0a98fd42 3896 return;
0a98fd42 3897
8c67c337 3898 bool verbose = q.sess.verbose > 2;
0a98fd42 3899
8c67c337
JS
3900 if (verbose)
3901 clog << "saveargs: examining '" << (dwarf_diename(scope_die) ?: "<unknown>")
3902 << "' (dieoffset: " << lex_cast_hex(dwarf_dieoffset(scope_die)) << ")\n";
0a98fd42 3903
8c67c337
JS
3904 if (has_return)
3905 {
3906 /* Only save the return value if it has a type. */
3907 string type_name;
3908 Dwarf_Die type_die;
3909 if (dwarf_attr_die (scope_die, DW_AT_type, &type_die) &&
3910 dwarf_type_name(&type_die, type_name))
3911 args.push_back("$return:"+type_name);
3912
3913 else if (verbose)
3914 clog << "saveargs: failed to retrieve type name for return value"
3915 << " (dieoffset: " << lex_cast_hex(dwarf_dieoffset(scope_die))
3916 << ")\n";
3917 }
d87623a1 3918
0a98fd42 3919 Dwarf_Die arg;
4ef35696
JS
3920 vector<Dwarf_Die> scopes = q.dw.getscopes(scope_die);
3921 for (unsigned i = 0; i < scopes.size(); ++i)
3922 {
3923 if (dwarf_tag(&scopes[i]) == DW_TAG_compile_unit)
3924 break; // we don't want file-level variables
3925 if (dwarf_child (&scopes[i], &arg) == 0)
3926 do
0a98fd42 3927 {
4ef35696
JS
3928 switch (dwarf_tag (&arg))
3929 {
3930 case DW_TAG_variable:
3931 case DW_TAG_formal_parameter:
3932 break;
0a98fd42 3933
4ef35696
JS
3934 default:
3935 continue;
3936 }
0a98fd42 3937
4ef35696
JS
3938 /* Ignore this local if it has no name. */
3939 const char *arg_name = dwarf_diename (&arg);
3940 if (!arg_name)
8c67c337
JS
3941 {
3942 if (verbose)
4ef35696 3943 clog << "saveargs: failed to retrieve name for local (dieoffset: "
8c67c337
JS
3944 << lex_cast_hex(dwarf_dieoffset(&arg)) << ")\n";
3945 continue;
3946 }
4ef35696
JS
3947
3948 if (verbose)
3949 clog << "saveargs: finding location for local '"
3950 << arg_name << "' (dieoffset: "
3951 << lex_cast_hex(dwarf_dieoffset(&arg)) << ")\n";
3952
3953 /* Ignore this local if it has no location (or not at this PC). */
3954 /* NB: It still may not be directly accessible, e.g. if it is an
3955 * aggregate type, implicit_pointer, etc., but the user can later
3956 * figure out how to access the interesting parts. */
3957 Dwarf_Attribute attr_mem;
3958 if (!dwarf_attr_integrate (&arg, DW_AT_const_value, &attr_mem))
3959 {
3960 Dwarf_Op *expr;
3961 size_t len;
3962 if (!dwarf_attr_integrate (&arg, DW_AT_location, &attr_mem))
3963 {
3964 if (verbose)
3965 clog << "saveargs: failed to resolve the location for local '"
3966 << arg_name << "' (dieoffset: "
3967 << lex_cast_hex(dwarf_dieoffset(&arg)) << ")\n";
3968 continue;
3969 }
3970 else if (!(dwarf_getlocation_addr(&attr_mem, dwfl_addr, &expr,
3971 &len, 1) == 1 && len > 0))
3972 {
3973 if (verbose)
3974 clog << "saveargs: local '" << arg_name << "' (dieoffset: "
3975 << lex_cast_hex(dwarf_dieoffset(&arg))
3976 << ") is not available at this address ("
3977 << lex_cast_hex(dwfl_addr) << ")\n";
3978 continue;
3979 }
3980 }
3981
3982 /* Ignore this local if it has no type. */
3983 string type_name;
3984 Dwarf_Die type_die;
3985 if (!dwarf_attr_die (&arg, DW_AT_type, &type_die) ||
3986 !dwarf_type_name(&type_die, type_name))
8c67c337
JS
3987 {
3988 if (verbose)
4ef35696
JS
3989 clog << "saveargs: failed to retrieve type name for local '"
3990 << arg_name << "' (dieoffset: "
3991 << lex_cast_hex(dwarf_dieoffset(&arg)) << ")\n";
8c67c337
JS
3992 continue;
3993 }
8c67c337 3994
4ef35696
JS
3995 /* This local looks good -- save it! */
3996 args.push_back("$"+string(arg_name)+":"+type_name);
8c67c337 3997 }
4ef35696
JS
3998 while (dwarf_siblingof (&arg, &arg) == 0);
3999 }
0a98fd42
JS
4000}
4001
4002
4003void
d0bfd2ac 4004dwarf_derived_probe::getargs(std::list<std::string> &arg_set) const
0a98fd42 4005{
d0bfd2ac 4006 arg_set.insert(arg_set.end(), args.begin(), args.end());
0a98fd42
JS
4007}
4008
4009
27dc09b1
DB
4010void
4011dwarf_derived_probe::emit_unprivileged_assertion (translator_output* o)
4012{
4013 if (has_process)
4014 {
4015 // These probes are allowed for unprivileged users, but only in the
4016 // context of processes which they own.
4017 emit_process_owner_assertion (o);
4018 return;
4019 }
4020
4021 // Other probes must contain the default assertion which aborts
4022 // if executed by an unprivileged user.
4023 derived_probe::emit_unprivileged_assertion (o);
4024}
4025
4026
4027void
4028dwarf_derived_probe::print_dupe_stamp(ostream& o)
4029{
4030 if (has_process)
4031 {
4032 // These probes are allowed for unprivileged users, but only in the
4033 // context of processes which they own.
4034 print_dupe_stamp_unprivileged_process_owner (o);
4035 return;
4036 }
4037
4038 // Other probes must contain the default dupe stamp
4039 derived_probe::print_dupe_stamp (o);
4040}
4041
64211010 4042
7a053d3b 4043void
20c6c071 4044dwarf_derived_probe::register_statement_variants(match_node * root,
27dc09b1
DB
4045 dwarf_builder * dw,
4046 bool bind_unprivileged_p)
bd2b1e68 4047{
27dc09b1
DB
4048 root
4049 ->bind_unprivileged(bind_unprivileged_p)
4050 ->bind(dw);
54efe513
GH
4051}
4052
7a053d3b 4053void
fd6602a0 4054dwarf_derived_probe::register_function_variants(match_node * root,
27dc09b1
DB
4055 dwarf_builder * dw,
4056 bool bind_unprivileged_p)
2865d17a 4057{
27dc09b1
DB
4058 root
4059 ->bind_unprivileged(bind_unprivileged_p)
4060 ->bind(dw);
4061 root->bind(TOK_INLINE)
4062 ->bind_unprivileged(bind_unprivileged_p)
4063 ->bind(dw);
4064 root->bind(TOK_CALL)
4065 ->bind_unprivileged(bind_unprivileged_p)
4066 ->bind(dw);
4067 root->bind(TOK_RETURN)
4068 ->bind_unprivileged(bind_unprivileged_p)
4069 ->bind(dw);
4070 root->bind(TOK_RETURN)
4071 ->bind_unprivileged(bind_unprivileged_p)
4072 ->bind_num(TOK_MAXACTIVE)->bind(dw);
bd2b1e68
GH
4073}
4074
7a053d3b 4075void
27dc09b1
DB
4076dwarf_derived_probe::register_function_and_statement_variants(
4077 match_node * root,
4078 dwarf_builder * dw,
4079 bool bind_unprivileged_p
4080)
bd2b1e68
GH
4081{
4082 // Here we match 4 forms:
4083 //
4084 // .function("foo")
4085 // .function(0xdeadbeef)
4086 // .statement("foo")
4087 // .statement(0xdeadbeef)
4088
27dc09b1
DB
4089 register_function_variants(root->bind_str(TOK_FUNCTION), dw, bind_unprivileged_p);
4090 register_function_variants(root->bind_num(TOK_FUNCTION), dw, bind_unprivileged_p);
4091 register_statement_variants(root->bind_str(TOK_STATEMENT), dw, bind_unprivileged_p);
4092 register_statement_variants(root->bind_num(TOK_STATEMENT), dw, bind_unprivileged_p);
bd2b1e68
GH
4093}
4094
4095void
c4ce66a1 4096dwarf_derived_probe::register_patterns(systemtap_session& s)
bd2b1e68 4097{
c4ce66a1 4098 match_node* root = s.pattern_root;
bd2b1e68
GH
4099 dwarf_builder *dw = new dwarf_builder();
4100
c4ce66a1
JS
4101 update_visitor *filter = new dwarf_cast_expanding_visitor(s, *dw);
4102 s.code_filters.push_back(filter);
4103
27dc09b1
DB
4104 register_function_and_statement_variants(root->bind(TOK_KERNEL), dw);
4105 register_function_and_statement_variants(root->bind_str(TOK_MODULE), dw);
4106
4107 root->bind(TOK_KERNEL)->bind_num(TOK_STATEMENT)->bind(TOK_ABSOLUTE)
4108 ->bind(dw);
4109 root->bind(TOK_KERNEL)->bind_str(TOK_FUNCTION)->bind_str(TOK_LABEL)
4110 ->bind(dw);
d2c9ec9b 4111
27dc09b1
DB
4112 register_function_and_statement_variants(root->bind_str(TOK_PROCESS), dw,
4113 true/*bind_unprivileged*/);
d2c9ec9b 4114 root->bind_str(TOK_PROCESS)->bind_str(TOK_FUNCTION)->bind_str(TOK_LABEL)
27dc09b1 4115 ->bind_unprivileged()
d2c9ec9b 4116 ->bind(dw);
63b4fd14 4117 root->bind_str(TOK_PROCESS)->bind_str(TOK_LIBRARY)->bind_str(TOK_MARK)
27dc09b1 4118 ->bind_unprivileged()
63b4fd14 4119 ->bind(dw);
a794dbeb
FCE
4120 root->bind_str(TOK_PROCESS)->bind_str(TOK_LIBRARY)->bind_str(TOK_PROVIDER)->bind_str(TOK_MARK)
4121 ->bind_unprivileged()
4122 ->bind(dw);
d2c9ec9b 4123 root->bind_str(TOK_PROCESS)->bind_str(TOK_MARK)
27dc09b1 4124 ->bind_unprivileged()
d2c9ec9b 4125 ->bind(dw);
a794dbeb
FCE
4126 root->bind_str(TOK_PROCESS)->bind_str(TOK_PROVIDER)->bind_str(TOK_MARK)
4127 ->bind_unprivileged()
4128 ->bind(dw);
bd2b1e68
GH
4129}
4130
9020300d
FCE
4131void
4132dwarf_derived_probe::emit_probe_local_init(translator_output * o)
4133{
b95e2b79
MH
4134 if (access_vars)
4135 {
4136 // if accessing $variables, emit bsp cache setup for speeding up
d4670309 4137 o->newline() << "#if defined __ia64__";
b95e2b79 4138 o->newline() << "bspcache(c->unwaddr, c->regs);";
d4670309 4139 o->newline() << "#endif";
b95e2b79 4140 }
9020300d 4141}
2930abc7 4142
b20febf3 4143// ------------------------------------------------------------------------
46b84a80
DS
4144
4145void
b20febf3 4146dwarf_derived_probe_group::enroll (dwarf_derived_probe* p)
46b84a80 4147{
b20febf3 4148 probes_by_module.insert (make_pair (p->module, p));
b8da0ad1
FCE
4149
4150 // XXX: probes put at the same address should all share a
4151 // single kprobe/kretprobe, and have their handlers executed
4152 // sequentially.
b55bc428
FCE
4153}
4154
7a053d3b 4155void
775d51e5 4156dwarf_derived_probe_group::emit_module_decls (systemtap_session& s)
ec4373ff 4157{
b20febf3 4158 if (probes_by_module.empty()) return;
2930abc7 4159
775d51e5
DS
4160 s.op->newline() << "/* ---- dwarf probes ---- */";
4161
4162 // Warn of misconfigured kernels
f41595cc
FCE
4163 s.op->newline() << "#if ! defined(CONFIG_KPROBES)";
4164 s.op->newline() << "#error \"Need CONFIG_KPROBES!\"";
4165 s.op->newline() << "#endif";
775d51e5 4166 s.op->newline();
f41595cc 4167
f07c3b68 4168 s.op->newline() << "#ifndef KRETACTIVE";
1ee6b5fc 4169 s.op->newline() << "#define KRETACTIVE (max(15,6*(int)num_possible_cpus()))";
f07c3b68
FCE
4170 s.op->newline() << "#endif";
4171
14cf7e42
SC
4172 // Forward decls
4173 s.op->newline() << "#include \"kprobes-common.h\"";
4174
b20febf3
FCE
4175 // Forward declare the master entry functions
4176 s.op->newline() << "static int enter_kprobe_probe (struct kprobe *inst,";
4177 s.op->line() << " struct pt_regs *regs);";
4178 s.op->newline() << "static int enter_kretprobe_probe (struct kretprobe_instance *inst,";
4179 s.op->line() << " struct pt_regs *regs);";
4180
42cb22bd
MH
4181 // Emit an array of kprobe/kretprobe pointers
4182 s.op->newline() << "#if defined(STAPCONF_UNREGISTER_KPROBES)";
4183 s.op->newline() << "static void * stap_unreg_kprobes[" << probes_by_module.size() << "];";
4184 s.op->newline() << "#endif";
4185
b20febf3 4186 // Emit the actual probe list.
606fd9c8
FCE
4187
4188 // NB: we used to plop a union { struct kprobe; struct kretprobe } into
4189 // struct stap_dwarf_probe, but it being initialized data makes it add
4190 // hundreds of bytes of padding per stap_dwarf_probe. (PR5673)
14cf7e42 4191 s.op->newline() << "static struct stap_dwarf_kprobe stap_dwarf_kprobes[" << probes_by_module.size() << "];";
606fd9c8
FCE
4192 // NB: bss!
4193
4c2732a1 4194 s.op->newline() << "static struct stap_dwarf_probe {";
b0986e7a
DS
4195 s.op->newline(1) << "const unsigned return_p:1;";
4196 s.op->newline() << "const unsigned maxactive_p:1;";
b350f56b 4197 s.op->newline() << "const unsigned optional_p:1;";
b20febf3 4198 s.op->newline() << "unsigned registered_p:1;";
b0986e7a 4199 s.op->newline() << "const unsigned short maxactive_val;";
606fd9c8 4200
af234c40
JS
4201 // data saved in the kretprobe_instance packet
4202 s.op->newline() << "const unsigned short saved_longs;";
4203 s.op->newline() << "const unsigned short saved_strings;";
4204
faea5e16 4205 // Let's find some stats for the embedded strings. Maybe they
606fd9c8
FCE
4206 // are small and uniform enough to justify putting char[MAX]'s into
4207 // the array instead of relocated char*'s.
faea5e16
JS
4208 size_t module_name_max = 0, section_name_max = 0;
4209 size_t module_name_tot = 0, section_name_tot = 0;
606fd9c8
FCE
4210 size_t all_name_cnt = probes_by_module.size(); // for average
4211 for (p_b_m_iterator it = probes_by_module.begin(); it != probes_by_module.end(); it++)
4212 {
4213 dwarf_derived_probe* p = it->second;
4214#define DOIT(var,expr) do { \
4215 size_t var##_size = (expr) + 1; \
4216 var##_max = max (var##_max, var##_size); \
4217 var##_tot += var##_size; } while (0)
4218 DOIT(module_name, p->module.size());
4219 DOIT(section_name, p->section.size());
606fd9c8
FCE
4220#undef DOIT
4221 }
4222
4223 // Decide whether it's worthwhile to use char[] or char* by comparing
4224 // the amount of average waste (max - avg) to the relocation data size
4225 // (3 native long words).
4226#define CALCIT(var) \
4227 if ((var##_name_max-(var##_name_tot/all_name_cnt)) < (3 * sizeof(void*))) \
4228 { \
4229 s.op->newline() << "const char " << #var << "[" << var##_name_max << "];"; \
4230 if (s.verbose > 2) clog << "stap_dwarf_probe " << #var \
4231 << "[" << var##_name_max << "]" << endl; \
4232 } \
4233 else \
4234 { \
b0986e7a 4235 s.op->newline() << "const char * const " << #var << ";"; \
606fd9c8
FCE
4236 if (s.verbose > 2) clog << "stap_dwarf_probe *" << #var << endl; \
4237 }
4238
4239 CALCIT(module);
4240 CALCIT(section);
e6fe60e7 4241#undef CALCIT
606fd9c8 4242
b0986e7a 4243 s.op->newline() << "const unsigned long address;";
26e63673 4244 s.op->newline() << "struct stap_probe * const probe;";
c87ae2c1 4245 s.op->newline() << "struct stap_probe * const entry_probe;";
b642c901
SC
4246 s.op->newline() << "const unsigned long sdt_sem_offset;";
4247 s.op->newline() << "unsigned long sdt_sem_address;";
4248 s.op->newline() << "struct task_struct *tsk;";
4249 s.op->newline() << "const char *pathname;";
4250 s.op->newline() << "struct stap_task_finder_target finder;";
b20febf3
FCE
4251 s.op->newline(-1) << "} stap_dwarf_probes[] = {";
4252 s.op->indent(1);
4253
4254 for (p_b_m_iterator it = probes_by_module.begin(); it != probes_by_module.end(); it++)
2930abc7 4255 {
b20febf3
FCE
4256 dwarf_derived_probe* p = it->second;
4257 s.op->newline() << "{";
4258 if (p->has_return)
4259 s.op->line() << " .return_p=1,";
c9bad430 4260 if (p->has_maxactive)
606fd9c8
FCE
4261 {
4262 s.op->line() << " .maxactive_p=1,";
4263 assert (p->maxactive_val >= 0 && p->maxactive_val <= USHRT_MAX);
4264 s.op->line() << " .maxactive_val=" << p->maxactive_val << ",";
4265 }
af234c40
JS
4266 if (p->saved_longs || p->saved_strings)
4267 {
4268 if (p->saved_longs)
4269 s.op->line() << " .saved_longs=" << p->saved_longs << ",";
4270 if (p->saved_strings)
4271 s.op->line() << " .saved_strings=" << p->saved_strings << ",";
4272 if (p->entry_handler)
c87ae2c1 4273 s.op->line() << " .entry_probe=" << common_probe_init (p->entry_handler) << ",";
af234c40 4274 }
b350f56b
JS
4275 if (p->locations[0]->optional)
4276 s.op->line() << " .optional_p=1,";
dc38c256 4277 s.op->line() << " .address=(unsigned long)0x" << hex << p->addr << dec << "ULL,";
84048984
FCE
4278 s.op->line() << " .module=\"" << p->module << "\",";
4279 s.op->line() << " .section=\"" << p->section << "\",";
faea5e16 4280 s.op->line() << " .probe=" << common_probe_init (p) << ",";
b642c901
SC
4281 if (p->sdt_semaphore_addr != 0)
4282 {
4283 s.op->line() << " .sdt_sem_offset=(unsigned long)0x" << hex << p->sdt_semaphore_addr << dec << "ULL,";
4284 s.op->line() << " .sdt_sem_address=0,";
4285 if (p->has_library)
4286 {
4287 s.op->line() << " .finder={";
4288 s.op->line() << " .pid=0,";
4289 s.op->line() << " .procname=\"" << p->user_path << "\",";
4290 s.op->line() << " .mmap_callback=&stap_kprobe_mmap_found,";
4291 s.op->line() << " },";
4292 s.op->line() << " .pathname=\"" << p->user_lib << "\",";
4293 }
4294 else
4295 {
4296 s.op->line() << " .finder={";
4297 s.op->line() << " .pid=0,";
4298 s.op->line() << " .procname=\"" << p->user_path << "\",";
4299 s.op->line() << " .callback=&stap_kprobe_process_found,";
4300 s.op->line() << " },";
4301 s.op->line() << " .pathname=\"" << p->user_path << "\",";
4302 }
4303
4304 }
4305 else
4306 s.op->line() << " .sdt_sem_offset=0,";
b642c901 4307
b20febf3 4308 s.op->line() << " },";
2930abc7 4309 }
2930abc7 4310
b20febf3
FCE
4311 s.op->newline(-1) << "};";
4312
4313 // Emit the kprobes callback function
4314 s.op->newline();
4315 s.op->newline() << "static int enter_kprobe_probe (struct kprobe *inst,";
4316 s.op->line() << " struct pt_regs *regs) {";
606fd9c8
FCE
4317 // NB: as of PR5673, the kprobe|kretprobe union struct is in BSS
4318 s.op->newline(1) << "int kprobe_idx = ((uintptr_t)inst-(uintptr_t)stap_dwarf_kprobes)/sizeof(struct stap_dwarf_kprobe);";
4319 // Check that the index is plausible
4320 s.op->newline() << "struct stap_dwarf_probe *sdp = &stap_dwarf_probes[";
4321 s.op->line() << "((kprobe_idx >= 0 && kprobe_idx < " << probes_by_module.size() << ")?";
4322 s.op->line() << "kprobe_idx:0)"; // NB: at least we avoid memory corruption
4323 // XXX: it would be nice to give a more verbose error though; BUG_ON later?
4324 s.op->line() << "];";
faea5e16 4325 common_probe_entryfn_prologue (s.op, "STAP_SESSION_RUNNING", "sdp->probe");
b20febf3 4326 s.op->newline() << "c->regs = regs;";
6415ddde
MW
4327
4328 // Make it look like the IP is set as it wouldn't have been replaced
4329 // by a breakpoint instruction when calling real probe handler. Reset
4330 // IP regs on return, so we don't confuse kprobes. PR10458
4331 s.op->newline() << "{";
4332 s.op->indent(1);
4333 s.op->newline() << "unsigned long kprobes_ip = REG_IP(c->regs);";
259d54c0 4334 s.op->newline() << "SET_REG_IP(regs, (unsigned long) inst->addr);";
26e63673 4335 s.op->newline() << "(*sdp->probe->ph) (c);";
259d54c0 4336 s.op->newline() << "SET_REG_IP(regs, kprobes_ip);";
6415ddde
MW
4337 s.op->newline(-1) << "}";
4338
b20febf3
FCE
4339 common_probe_entryfn_epilogue (s.op);
4340 s.op->newline() << "return 0;";
4341 s.op->newline(-1) << "}";
4342
4343 // Same for kretprobes
4344 s.op->newline();
af234c40
JS
4345 s.op->newline() << "static int enter_kretprobe_common (struct kretprobe_instance *inst,";
4346 s.op->line() << " struct pt_regs *regs, int entry) {";
b20febf3 4347 s.op->newline(1) << "struct kretprobe *krp = inst->rp;";
606fd9c8
FCE
4348
4349 // NB: as of PR5673, the kprobe|kretprobe union struct is in BSS
a36378d7 4350 s.op->newline() << "int kprobe_idx = ((uintptr_t)krp-(uintptr_t)stap_dwarf_kprobes)/sizeof(struct stap_dwarf_kprobe);";
606fd9c8
FCE
4351 // Check that the index is plausible
4352 s.op->newline() << "struct stap_dwarf_probe *sdp = &stap_dwarf_probes[";
4353 s.op->line() << "((kprobe_idx >= 0 && kprobe_idx < " << probes_by_module.size() << ")?";
4354 s.op->line() << "kprobe_idx:0)"; // NB: at least we avoid memory corruption
4355 // XXX: it would be nice to give a more verbose error though; BUG_ON later?
4356 s.op->line() << "];";
4357
c87ae2c1
JS
4358 s.op->newline() << "struct stap_probe *sp = entry ? sdp->entry_probe : sdp->probe;";
4359 s.op->newline() << "if (sp) {";
4360 s.op->indent(1);
4361 common_probe_entryfn_prologue (s.op, "STAP_SESSION_RUNNING", "sp");
b20febf3 4362 s.op->newline() << "c->regs = regs;";
af234c40
JS
4363
4364 // for assisting runtime's backtrace logic and accessing kretprobe data packets
4365 s.op->newline() << "c->pi = inst;";
4366 s.op->newline() << "c->pi_longs = sdp->saved_longs;";
6415ddde
MW
4367
4368 // Make it look like the IP is set as it wouldn't have been replaced
4369 // by a breakpoint instruction when calling real probe handler. Reset
4370 // IP regs on return, so we don't confuse kprobes. PR10458
4371 s.op->newline() << "{";
c87ae2c1
JS
4372 s.op->newline(1) << "unsigned long kprobes_ip = REG_IP(c->regs);";
4373 s.op->newline() << "if (entry)";
4374 s.op->newline(1) << "SET_REG_IP(regs, (unsigned long) inst->rp->kp.addr);";
4375 s.op->newline(-1) << "else";
4376 s.op->newline(1) << "SET_REG_IP(regs, (unsigned long)inst->ret_addr);";
4377 s.op->newline(-1) << "(sp->ph) (c);";
259d54c0 4378 s.op->newline() << "SET_REG_IP(regs, kprobes_ip);";
6415ddde
MW
4379 s.op->newline(-1) << "}";
4380
b20febf3 4381 common_probe_entryfn_epilogue (s.op);
c87ae2c1 4382 s.op->newline(-1) << "}";
b20febf3
FCE
4383 s.op->newline() << "return 0;";
4384 s.op->newline(-1) << "}";
af234c40
JS
4385
4386 s.op->newline();
4387 s.op->newline() << "static int enter_kretprobe_probe (struct kretprobe_instance *inst,";
4388 s.op->line() << " struct pt_regs *regs) {";
4389 s.op->newline(1) << "return enter_kretprobe_common(inst, regs, 0);";
4390 s.op->newline(-1) << "}";
4391
4392 s.op->newline();
4393 s.op->newline() << "static int enter_kretprobe_entry_probe (struct kretprobe_instance *inst,";
4394 s.op->line() << " struct pt_regs *regs) {";
4395 s.op->newline(1) << "return enter_kretprobe_common(inst, regs, 1);";
4396 s.op->newline(-1) << "}";
b642c901 4397
14cf7e42
SC
4398 s.op->newline();
4399 s.op->newline() << "#include \"kprobes-common.c\"";
4400 s.op->newline();
20c6c071 4401}
ec4373ff 4402
20c6c071 4403
dc38c0ae 4404void
b20febf3
FCE
4405dwarf_derived_probe_group::emit_module_init (systemtap_session& s)
4406{
b642c901
SC
4407 p_b_m_iterator it;
4408 for (it = probes_by_module.begin(); it != probes_by_module.end(); it++)
4409 {
4410 dwarf_derived_probe* p = it->second;
4411 if (p->sdt_semaphore_addr != 0)
4412 break;
4413 }
4414 if (it != probes_by_module.end()) // Ignore if there are no semaphores
4415 {
4416 s.op->newline() << "for (i=0; i<ARRAY_SIZE(stap_dwarf_probes); i++) {";
4417 s.op->newline(1) << "int rc;";
4418 s.op->newline() << "struct stap_dwarf_probe *p = &stap_dwarf_probes[i];";
26e63673 4419 s.op->newline() << "probe_point = p->probe->pp;"; // for error messages
b642c901
SC
4420 s.op->newline() << "if (p->sdt_sem_offset) {";
4421 s.op->newline(1) << "rc = stap_register_task_finder_target(&p->finder);";
4422 s.op->newline(-1) << "}";
4423 s.op->newline() << "if (rc) break;";
4424 s.op->newline(-1) << "}";
4425 }
4426
b20febf3
FCE
4427 s.op->newline() << "for (i=0; i<" << probes_by_module.size() << "; i++) {";
4428 s.op->newline(1) << "struct stap_dwarf_probe *sdp = & stap_dwarf_probes[i];";
a36378d7 4429 s.op->newline() << "struct stap_dwarf_kprobe *kp = & stap_dwarf_kprobes[i];";
a049e342 4430 s.op->newline() << "unsigned long relocated_addr = _stp_kmodule_relocate (sdp->module, sdp->section, sdp->address);";
b20febf3 4431 s.op->newline() << "if (relocated_addr == 0) continue;"; // quietly; assume module is absent
26e63673 4432 s.op->newline() << "probe_point = sdp->probe->pp;"; // for error messages
b20febf3 4433 s.op->newline() << "if (sdp->return_p) {";
606fd9c8 4434 s.op->newline(1) << "kp->u.krp.kp.addr = (void *) relocated_addr;";
c9bad430 4435 s.op->newline() << "if (sdp->maxactive_p) {";
606fd9c8 4436 s.op->newline(1) << "kp->u.krp.maxactive = sdp->maxactive_val;";
c9bad430 4437 s.op->newline(-1) << "} else {";
f07c3b68 4438 s.op->newline(1) << "kp->u.krp.maxactive = KRETACTIVE;";
c9bad430 4439 s.op->newline(-1) << "}";
606fd9c8 4440 s.op->newline() << "kp->u.krp.handler = &enter_kretprobe_probe;";
af234c40 4441 s.op->newline() << "#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,25)";
c87ae2c1 4442 s.op->newline() << "if (sdp->entry_probe) {";
af234c40
JS
4443 s.op->newline(1) << "kp->u.krp.entry_handler = &enter_kretprobe_entry_probe;";
4444 s.op->newline() << "kp->u.krp.data_size = sdp->saved_longs * sizeof(int64_t) + ";
4445 s.op->newline() << " sdp->saved_strings * MAXSTRINGLEN;";
4446 s.op->newline(-1) << "}";
4447 s.op->newline() << "#endif";
e4cb375f
MH
4448 // to ensure safeness of bspcache, always use aggr_kprobe on ia64
4449 s.op->newline() << "#ifdef __ia64__";
4450 s.op->newline() << "kp->dummy.addr = kp->u.krp.kp.addr;";
4451 s.op->newline() << "kp->dummy.pre_handler = NULL;";
4452 s.op->newline() << "rc = register_kprobe (& kp->dummy);";
4453 s.op->newline() << "if (rc == 0) {";
4454 s.op->newline(1) << "rc = register_kretprobe (& kp->u.krp);";
4455 s.op->newline() << "if (rc != 0)";
4456 s.op->newline(1) << "unregister_kprobe (& kp->dummy);";
4457 s.op->newline(-2) << "}";
4458 s.op->newline() << "#else";
606fd9c8 4459 s.op->newline() << "rc = register_kretprobe (& kp->u.krp);";
e4cb375f 4460 s.op->newline() << "#endif";
b20febf3 4461 s.op->newline(-1) << "} else {";
e4cb375f 4462 // to ensure safeness of bspcache, always use aggr_kprobe on ia64
606fd9c8
FCE
4463 s.op->newline(1) << "kp->u.kp.addr = (void *) relocated_addr;";
4464 s.op->newline() << "kp->u.kp.pre_handler = &enter_kprobe_probe;";
e4cb375f
MH
4465 s.op->newline() << "#ifdef __ia64__";
4466 s.op->newline() << "kp->dummy.addr = kp->u.kp.addr;";
4467 s.op->newline() << "kp->dummy.pre_handler = NULL;";
4468 s.op->newline() << "rc = register_kprobe (& kp->dummy);";
4469 s.op->newline() << "if (rc == 0) {";
4470 s.op->newline(1) << "rc = register_kprobe (& kp->u.kp);";
4471 s.op->newline() << "if (rc != 0)";
4472 s.op->newline(1) << "unregister_kprobe (& kp->dummy);";
4473 s.op->newline(-2) << "}";
4474 s.op->newline() << "#else";
606fd9c8 4475 s.op->newline() << "rc = register_kprobe (& kp->u.kp);";
e4cb375f 4476 s.op->newline() << "#endif";
b20febf3 4477 s.op->newline(-1) << "}";
9063462a
FCE
4478 s.op->newline() << "if (rc) {"; // PR6749: tolerate a failed register_*probe.
4479 s.op->newline(1) << "sdp->registered_p = 0;";
b350f56b 4480 s.op->newline() << "if (!sdp->optional_p)";
50b6acf7 4481 s.op->newline(1) << "_stp_warn (\"probe %s (address 0x%lx) registration error (rc %d)\", probe_point, (unsigned long) relocated_addr, rc);";
b350f56b 4482 s.op->newline(-1) << "rc = 0;"; // continue with other probes
9063462a
FCE
4483 // XXX: shall we increment numskipped?
4484 s.op->newline(-1) << "}";
4485
4486#if 0 /* pre PR 6749; XXX consider making an option */
c48cb0cc 4487 s.op->newline(1) << "for (j=i-1; j>=0; j--) {"; // partial rollback
b20febf3 4488 s.op->newline(1) << "struct stap_dwarf_probe *sdp2 = & stap_dwarf_probes[j];";
606fd9c8
FCE
4489 s.op->newline() << "struct stap_dwarf_kprobe *kp2 = & stap_dwarf_kprobes[j];";
4490 s.op->newline() << "if (sdp2->return_p) unregister_kretprobe (&kp2->u.krp);";
4491 s.op->newline() << "else unregister_kprobe (&kp2->u.kp);";
e4cb375f
MH
4492 s.op->newline() << "#ifdef __ia64__";
4493 s.op->newline() << "unregister_kprobe (&kp2->dummy);";
4494 s.op->newline() << "#endif";
c48cb0cc
FCE
4495 // NB: we don't have to clear sdp2->registered_p, since the module_exit code is
4496 // not run for this early-abort case.
4497 s.op->newline(-1) << "}";
4498 s.op->newline() << "break;"; // don't attempt to register any more probes
b20febf3 4499 s.op->newline(-1) << "}";
9063462a
FCE
4500#endif
4501
b20febf3
FCE
4502 s.op->newline() << "else sdp->registered_p = 1;";
4503 s.op->newline(-1) << "}"; // for loop
dc38c0ae
DS
4504}
4505
4506
46b84a80 4507void
b20febf3 4508dwarf_derived_probe_group::emit_module_exit (systemtap_session& s)
46b84a80 4509{
b642c901
SC
4510 s.op->newline() << "for (i=0; i<" << probes_by_module.size() << "; i++) {";
4511 s.op->newline(1) << "struct stap_dwarf_probe *sdp = & stap_dwarf_probes[i];";
4512 s.op->newline() << "unsigned short sdt_semaphore = 0;"; // NB: fixed size
4513 s.op->newline() << "if (sdp->sdt_sem_address && __access_process_vm_noflush (sdp->tsk, sdp->sdt_sem_address, &sdt_semaphore, sizeof (sdt_semaphore), 0)) {";
4514 s.op->newline(1) << "sdt_semaphore --;";
4515 s.op->newline() << "__access_process_vm_noflush (sdp->tsk, sdp->sdt_sem_address, &sdt_semaphore, sizeof (sdt_semaphore), 1);";
4516 s.op->newline(-1) << "}";
4517 s.op->newline(-1) << "}";
4518
42cb22bd
MH
4519 //Unregister kprobes by batch interfaces.
4520 s.op->newline() << "#if defined(STAPCONF_UNREGISTER_KPROBES)";
4521 s.op->newline() << "j = 0;";
4522 s.op->newline() << "for (i=0; i<" << probes_by_module.size() << "; i++) {";
4523 s.op->newline(1) << "struct stap_dwarf_probe *sdp = & stap_dwarf_probes[i];";
4524 s.op->newline() << "struct stap_dwarf_kprobe *kp = & stap_dwarf_kprobes[i];";
4525 s.op->newline() << "if (! sdp->registered_p) continue;";
4526 s.op->newline() << "if (!sdp->return_p)";
4527 s.op->newline(1) << "stap_unreg_kprobes[j++] = &kp->u.kp;";
4528 s.op->newline(-2) << "}";
4529 s.op->newline() << "unregister_kprobes((struct kprobe **)stap_unreg_kprobes, j);";
4530 s.op->newline() << "j = 0;";
4531 s.op->newline() << "for (i=0; i<" << probes_by_module.size() << "; i++) {";
4532 s.op->newline(1) << "struct stap_dwarf_probe *sdp = & stap_dwarf_probes[i];";
4533 s.op->newline() << "struct stap_dwarf_kprobe *kp = & stap_dwarf_kprobes[i];";
4534 s.op->newline() << "if (! sdp->registered_p) continue;";
4535 s.op->newline() << "if (sdp->return_p)";
4536 s.op->newline(1) << "stap_unreg_kprobes[j++] = &kp->u.krp;";
4537 s.op->newline(-2) << "}";
4538 s.op->newline() << "unregister_kretprobes((struct kretprobe **)stap_unreg_kprobes, j);";
e4cb375f
MH
4539 s.op->newline() << "#ifdef __ia64__";
4540 s.op->newline() << "j = 0;";
4541 s.op->newline() << "for (i=0; i<" << probes_by_module.size() << "; i++) {";
4542 s.op->newline(1) << "struct stap_dwarf_probe *sdp = & stap_dwarf_probes[i];";
4543 s.op->newline() << "struct stap_dwarf_kprobe *kp = & stap_dwarf_kprobes[i];";
4544 s.op->newline() << "if (! sdp->registered_p) continue;";
4545 s.op->newline() << "stap_unreg_kprobes[j++] = &kp->dummy;";
4546 s.op->newline(-1) << "}";
4547 s.op->newline() << "unregister_kprobes((struct kprobe **)stap_unreg_kprobes, j);";
4548 s.op->newline() << "#endif";
42cb22bd
MH
4549 s.op->newline() << "#endif";
4550
b20febf3
FCE
4551 s.op->newline() << "for (i=0; i<" << probes_by_module.size() << "; i++) {";
4552 s.op->newline(1) << "struct stap_dwarf_probe *sdp = & stap_dwarf_probes[i];";
a36378d7 4553 s.op->newline() << "struct stap_dwarf_kprobe *kp = & stap_dwarf_kprobes[i];";
b20febf3
FCE
4554 s.op->newline() << "if (! sdp->registered_p) continue;";
4555 s.op->newline() << "if (sdp->return_p) {";
42cb22bd 4556 s.op->newline() << "#if !defined(STAPCONF_UNREGISTER_KPROBES)";
606fd9c8 4557 s.op->newline(1) << "unregister_kretprobe (&kp->u.krp);";
42cb22bd 4558 s.op->newline() << "#endif";
606fd9c8 4559 s.op->newline() << "atomic_add (kp->u.krp.nmissed, & skipped_count);";
73209876
FCE
4560 s.op->newline() << "#ifdef STP_TIMING";
4561 s.op->newline() << "if (kp->u.krp.nmissed)";
26e63673 4562 s.op->newline(1) << "_stp_warn (\"Skipped due to missed kretprobe/1 on '%s': %d\\n\", sdp->probe->pp, kp->u.krp.nmissed);";
73209876 4563 s.op->newline(-1) << "#endif";
606fd9c8 4564 s.op->newline() << "atomic_add (kp->u.krp.kp.nmissed, & skipped_count);";
73209876
FCE
4565 s.op->newline() << "#ifdef STP_TIMING";
4566 s.op->newline() << "if (kp->u.krp.kp.nmissed)";
26e63673 4567 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 4568 s.op->newline(-1) << "#endif";
557fb7a8 4569 s.op->newline(-1) << "} else {";
42cb22bd 4570 s.op->newline() << "#if !defined(STAPCONF_UNREGISTER_KPROBES)";
606fd9c8 4571 s.op->newline(1) << "unregister_kprobe (&kp->u.kp);";
42cb22bd 4572 s.op->newline() << "#endif";
606fd9c8 4573 s.op->newline() << "atomic_add (kp->u.kp.nmissed, & skipped_count);";
73209876
FCE
4574 s.op->newline() << "#ifdef STP_TIMING";
4575 s.op->newline() << "if (kp->u.kp.nmissed)";
26e63673 4576 s.op->newline(1) << "_stp_warn (\"Skipped due to missed kprobe on '%s': %lu\\n\", sdp->probe->pp, kp->u.kp.nmissed);";
73209876 4577 s.op->newline(-1) << "#endif";
b20febf3 4578 s.op->newline(-1) << "}";
e4cb375f
MH
4579 s.op->newline() << "#if !defined(STAPCONF_UNREGISTER_KPROBES) && defined(__ia64__)";
4580 s.op->newline() << "unregister_kprobe (&kp->dummy);";
4581 s.op->newline() << "#endif";
b20febf3
FCE
4582 s.op->newline() << "sdp->registered_p = 0;";
4583 s.op->newline(-1) << "}";
46b84a80
DS
4584}
4585
8aabf152
FCE
4586
4587struct sdt_kprobe_var_expanding_visitor: public var_expanding_visitor
4588{
4589 sdt_kprobe_var_expanding_visitor(const string & process_name,
4590 const string & provider_name,
4591 const string & probe_name,
4592 const string & arg_string,
4593 int arg_count):
4594 process_name (process_name), provider_name (provider_name), probe_name (probe_name),
4595 arg_count (arg_count)
4596 {
4597 tokenize(arg_string, arg_tokens, " ");
4598 assert(arg_count >= 0 && arg_count <= 10);
4599 }
4600 const string & process_name;
4601 const string & provider_name;
4602 const string & probe_name;
4603 int arg_count;
4604 vector<string> arg_tokens;
4605
4606 void visit_target_symbol (target_symbol* e);
4607};
4608
4609
aff5d390 4610struct sdt_uprobe_var_expanding_visitor: public var_expanding_visitor
7a05f484 4611{
f83336a5
FCE
4612 sdt_uprobe_var_expanding_visitor(systemtap_session& s,
4613 int elf_machine,
4614 const string & process_name,
a794dbeb 4615 const string & provider_name,
aff5d390
SC
4616 const string & probe_name,
4617 const string & arg_string,
8aabf152 4618 int ac):
332ba7e7
SC
4619 session (s), elf_machine (elf_machine), process_name (process_name),
4620 provider_name (provider_name), probe_name (probe_name), arg_count ((unsigned) ac)
a8ec7719 4621 {
f83336a5
FCE
4622 /* Register name mapping table depends on the elf machine of this particular
4623 probe target process/file, not upon the host. So we can't just
4624 #ifdef _i686_ etc. */
4625 if (elf_machine == EM_X86_64) {
8aabf152
FCE
4626 dwarf_regs["%rax"] = dwarf_regs["%eax"] = dwarf_regs["%ax"] = dwarf_regs["%al"] = 0;
4627 dwarf_regs["%rdx"] = dwarf_regs["%edx"] = dwarf_regs["%dx"] = dwarf_regs["%dl"] = 1;
4628 dwarf_regs["%rcx"] = dwarf_regs["%ecx"] = dwarf_regs["%cx"] = dwarf_regs["%cl"] = 2;
4629 dwarf_regs["%rbx"] = dwarf_regs["%ebx"] = dwarf_regs["%bx"] = dwarf_regs["%bl"] = 3;
4630 dwarf_regs["%rsi"] = dwarf_regs["%esi"] = dwarf_regs["%si"] = dwarf_regs["%sil"] = 4;
4631 dwarf_regs["%rdi"] = dwarf_regs["%edi"] = dwarf_regs["%di"] = dwarf_regs["%dil"] = 5;
4632 dwarf_regs["%rbp"] = dwarf_regs["%ebp"] = dwarf_regs["%bp"] = 6;
4633 dwarf_regs["%rsp"] = dwarf_regs["%esp"] = dwarf_regs["%sp"] = 7;
4634 dwarf_regs["%r8"] = dwarf_regs["%r8d"] = dwarf_regs["%r8w"] = dwarf_regs["%r8b"] = 8;
4635 dwarf_regs["%r9"] = dwarf_regs["%r9d"] = dwarf_regs["%r9w"] = dwarf_regs["%r9b"] = 9;
4636 dwarf_regs["%r10"] = dwarf_regs["%r10d"] = dwarf_regs["%r10w"] = dwarf_regs["%r10b"] = 10;
4637 dwarf_regs["%r11"] = dwarf_regs["%r11d"] = dwarf_regs["%r11w"] = dwarf_regs["%r11b"] = 11;
4638 dwarf_regs["%r12"] = dwarf_regs["%r12d"] = dwarf_regs["%r12w"] = dwarf_regs["%r12b"] = 12;
4639 dwarf_regs["%r13"] = dwarf_regs["%r13d"] = dwarf_regs["%r13w"] = dwarf_regs["%r13b"] = 13;
4640 dwarf_regs["%r14"] = dwarf_regs["%r14d"] = dwarf_regs["%r14w"] = dwarf_regs["%r14b"] = 14;
4641 dwarf_regs["%r15"] = dwarf_regs["%r15d"] = dwarf_regs["%r15w"] = dwarf_regs["%r15b"] = 15;
f83336a5 4642 } else if (elf_machine == EM_386) {
8aabf152
FCE
4643 dwarf_regs["%eax"] = dwarf_regs["%ax"] = dwarf_regs["%al"] = 0;
4644 dwarf_regs["%ecx"] = dwarf_regs["%cx"] = dwarf_regs["%cl"] = 1;
4645 dwarf_regs["%edx"] = dwarf_regs["%dx"] = dwarf_regs["%dl"] = 2;
4646 dwarf_regs["%ebx"] = dwarf_regs["%bx"] = dwarf_regs["%bl"] = 3;
4647 dwarf_regs["%esp"] = dwarf_regs["%sp"] = 4;
4648 dwarf_regs["%ebp"] = dwarf_regs["%bp"] = 5;
4649 dwarf_regs["%esi"] = dwarf_regs["%si"] = dwarf_regs["%sil"] = 6;
4650 dwarf_regs["%edi"] = dwarf_regs["%di"] = dwarf_regs["%dil"] = 7;
0491c523 4651 } else if (elf_machine == EM_PPC || elf_machine == EM_PPC64) {
8aabf152
FCE
4652 dwarf_regs["%r0"] = 0; dwarf_regs["%r1"] = 1; dwarf_regs["%r2"] = 2;
4653 dwarf_regs["%r3"] = 3; dwarf_regs["%r4"] = 4; dwarf_regs["%r5"] = 5;
4654 dwarf_regs["%r6"] = 6; dwarf_regs["%r7"] = 7; dwarf_regs["%r8"] = 8;
4655 dwarf_regs["%r9"] = 9; dwarf_regs["%r10"] = 10; dwarf_regs["%r11"] = 11;
4656 dwarf_regs["%r12"] = 12; dwarf_regs["%r13"] = 13; dwarf_regs["%r14"] = 14;
4657 dwarf_regs["%r15"] = 15; dwarf_regs["%r16"] = 16; dwarf_regs["%r17"] = 17;
4658 dwarf_regs["%r18"] = 18; dwarf_regs["%r19"] = 19; dwarf_regs["%r20"] = 20;
4659 dwarf_regs["%r21"] = 21; dwarf_regs["%r22"] = 22; dwarf_regs["%r23"] = 23;
4660 dwarf_regs["%r24"] = 24; dwarf_regs["%r25"] = 25; dwarf_regs["%r26"] = 26;
4661 dwarf_regs["%r27"] = 27; dwarf_regs["%r28"] = 28; dwarf_regs["%r29"] = 29;
c57ea854 4662 dwarf_regs["%r30"] = 30; dwarf_regs["%r31"] = 31;
8aabf152
FCE
4663 // PR11821: unadorned register "names" without -mregnames
4664 dwarf_regs["0"] = 0; dwarf_regs["1"] = 1; dwarf_regs["2"] = 2;
4665 dwarf_regs["3"] = 3; dwarf_regs["4"] = 4; dwarf_regs["5"] = 5;
4666 dwarf_regs["6"] = 6; dwarf_regs["7"] = 7; dwarf_regs["8"] = 8;
4667 dwarf_regs["9"] = 9; dwarf_regs["10"] = 10; dwarf_regs["11"] = 11;
4668 dwarf_regs["12"] = 12; dwarf_regs["13"] = 13; dwarf_regs["14"] = 14;
4669 dwarf_regs["15"] = 15; dwarf_regs["16"] = 16; dwarf_regs["17"] = 17;
4670 dwarf_regs["18"] = 18; dwarf_regs["19"] = 19; dwarf_regs["20"] = 20;
4671 dwarf_regs["21"] = 21; dwarf_regs["22"] = 22; dwarf_regs["23"] = 23;
4672 dwarf_regs["24"] = 24; dwarf_regs["25"] = 25; dwarf_regs["26"] = 26;
4673 dwarf_regs["27"] = 27; dwarf_regs["28"] = 28; dwarf_regs["29"] = 29;
c57ea854 4674 dwarf_regs["30"] = 30; dwarf_regs["31"] = 31;
0491c523
SC
4675 }
4676 else if (arg_count) {
8aabf152 4677 /* permit this case; just fall back to dwarf */
f83336a5
FCE
4678 }
4679
ebbd2b45 4680 need_debug_info = false;
aff5d390
SC
4681 tokenize(arg_string, arg_tokens, " ");
4682 assert(arg_count >= 0 && arg_count <= 10);
a8ec7719 4683 }
8aabf152 4684
f83336a5 4685 systemtap_session& session;
332ba7e7 4686 int elf_machine;
aff5d390 4687 const string & process_name;
a794dbeb 4688 const string & provider_name;
aff5d390 4689 const string & probe_name;
8aabf152 4690 unsigned arg_count;
aff5d390
SC
4691 vector<string> arg_tokens;
4692 map<string,int> dwarf_regs;
ebbd2b45 4693 bool need_debug_info;
aff5d390
SC
4694
4695 void visit_target_symbol (target_symbol* e);
aff5d390
SC
4696};
4697
7a05f484
SC
4698
4699void
aff5d390 4700sdt_uprobe_var_expanding_visitor::visit_target_symbol (target_symbol *e)
7a05f484 4701{
63ea4244 4702 try
7a05f484 4703 {
277c21bc 4704 if (e->name == "$$name")
aff5d390
SC
4705 {
4706 if (e->addressof)
4707 throw semantic_error("cannot take address of sdt context variable", e->tok);
7a05f484 4708
aff5d390
SC
4709 literal_string *myname = new literal_string (probe_name);
4710 myname->tok = e->tok;
4711 provide(myname);
4712 return;
4713 }
277c21bc 4714 if (e->name == "$$provider")
a794dbeb
FCE
4715 {
4716 if (e->addressof)
4717 throw semantic_error("cannot take address of sdt context variable", e->tok);
4718
4719 literal_string *myname = new literal_string (provider_name);
4720 myname->tok = e->tok;
4721 provide(myname);
4722 return;
4723 }
63ea4244 4724
8aabf152 4725 unsigned argno = 0; // the N in $argN
c69a87e0 4726 try
aff5d390 4727 {
5ecaa5a7 4728 if (startswith(e->name, "$arg"))
8aabf152 4729 argno = lex_cast<unsigned>(e->name.substr(4));
aff5d390 4730 }
c69a87e0 4731 catch (const runtime_error& f) // non-integral $arg suffix: e.g. $argKKKSDF
aff5d390 4732 {
8aabf152 4733 argno = 0;
aff5d390 4734 }
5ecaa5a7 4735
8aabf152
FCE
4736 if (arg_count == 0 || // a sdt.h variant without .probe-stored arg_count
4737 argno < 1 || argno > arg_count) // a $argN with out-of-range N
aff5d390 4738 {
8aabf152
FCE
4739 // NB: Either
4740 // 1) uprobe1_type $argN or $FOO (we don't know the arg_count)
4741 // 2) uprobe2_type $FOO (no probe args)
4742 // both of which get resolved later.
4743 need_debug_info = true;
4744 provide(e);
4745 return;
aff5d390 4746 }
277c21bc 4747
8aabf152
FCE
4748 assert (arg_tokens.size() >= argno);
4749 string asmarg = arg_tokens[argno-1]; // $arg1 => arg_tokens[0]
c57ea854 4750
8aabf152
FCE
4751 // Now we try to parse this thing, which is an assembler operand
4752 // expression. If we can't, we warn, back down to need_debug_info
4753 // and hope for the best.
4754 expression* argexpr = 0; // filled in in case of successful parse
4755
4756 string percent_regnames;
4757 string regnames;
4758 vector<string> matches;
40fe32e0 4759 int precision = 0;
8aabf152
FCE
4760 int rc;
4761
40fe32e0
SC
4762 // Parse the leading length
4763
4764 if (asmarg.find('@') != string::npos)
4765 {
4766 precision = lex_cast<int>(asmarg.substr(0, asmarg.find('@')));
4767 asmarg = asmarg.substr(asmarg.find('@')+1);
4768 }
4769
8aabf152
FCE
4770 // test for a numeric literal.
4771 // Only accept (signed) decimals throughout. XXX
4772
4773 // PR11821. NB: on powerpc, literals are not prefixed with $,
4774 // so this regex does not match. But that's OK, since without
4775 // -mregnames, we can't tell them apart from register numbers
4776 // anyway. With -mregnames, we could, if gcc somehow
4777 // communicated to us the presence of that option, but alas it
4778 // doesn't. http://gcc.gnu.org/PR44995.
9109f487 4779 rc = regexp_match (asmarg, "^[i\\$][-]?[0-9][0-9]*$", matches);
8aabf152
FCE
4780 if (! rc)
4781 {
4782 literal_number* ln = new literal_number(lex_cast<int>(matches[0].substr(1))); // assume decimal
4783 ln->tok = e->tok;
4784 argexpr = ln;
4785 goto matched;
4786 }
4787
4788 // Build regex pieces out of the known dwarf_regs. We keep two separate
4789 // lists: ones with the % prefix (and thus unambigiuous even despite PR11821),
4790 // and ones with no prefix (and thus only usable in unambiguous contexts).
4791 for (map<string,int>::iterator ri = dwarf_regs.begin(); ri != dwarf_regs.end(); ri++)
4792 {
4793 string regname = ri->first;
4794 assert (regname != "");
4795 regnames += string("|")+regname;
4796 if (regname[0]=='%')
4797 percent_regnames += string("|")+regname;
4798 }
4799 // clip off leading |
4800 regnames = regnames.substr(1);
4801 percent_regnames = percent_regnames.substr(1);
4802
4803 // test for REGISTER
4804 // NB: Because PR11821, we must use percent_regnames here.
332ba7e7 4805 if (elf_machine == EM_PPC || elf_machine == EM_PPC64)
9109f487
SC
4806 rc = regexp_match (asmarg, string("^(")+regnames+string(")$"), matches);
4807 else
332ba7e7 4808 rc = regexp_match (asmarg, string("^(")+percent_regnames+string(")$"), matches);
8aabf152
FCE
4809 if (! rc)
4810 {
4811 string regname = matches[1];
4812 if (dwarf_regs.find (regname) != dwarf_regs.end()) // known register
4813 {
4814 embedded_expr *get_arg1 = new embedded_expr;
4815 get_arg1->tok = e->tok;
4816 get_arg1->code = string("/* unprivileged */ /* pure */")
4817 + (is_user_module (process_name)
4818 ? string("u_fetch_register(")
4819 : string("k_fetch_register("))
4820 + lex_cast(dwarf_regs[regname]) + string(")");
4821 // XXX: may we ever need to cast that to a narrower type?
4822 argexpr = get_arg1;
4823 goto matched;
4824 }
4825 // invalid register name, fall through
4826 }
40fe32e0 4827
8aabf152 4828 // test for OFFSET(REGISTER)
40fe32e0 4829 // NB: Despite PR11821, we can use regnames here, since the parentheses
8aabf152
FCE
4830 // make things unambiguous.
4831 rc = regexp_match (asmarg, string("^([-]?[0-9]*)[(](")+regnames+string(")[)]$"), matches);
4832 if (! rc)
4833 {
4834 string dispstr = matches[1];
4835 int64_t disp = 0;
4836 if (dispstr == "")
4837 disp = 0;
4838 else
4839 try
4840 {
4841 disp = lex_cast<int64_t>(dispstr); // should decode positive/negative hex/decimal
4842 }
4843 catch (const runtime_error& f) // unparseable offset
4844 {
4845 goto not_matched; // can't just 'break' out of
4846 // this case or use a sentinel
4847 // value, unfortunately
4848 }
4849
4850 string regname = matches[2];
4851 if (dwarf_regs.find (regname) != dwarf_regs.end()) // known register
4852 {
4853 // synthesize user_long(%{fetch_register(R)%} + D)
40fe32e0 4854
8aabf152
FCE
4855 embedded_expr *get_arg1 = new embedded_expr;
4856 get_arg1->tok = e->tok;
4857 get_arg1->code = string("/* unprivileged */ /* pure */")
4858 + (is_user_module (process_name)
4859 ? string("u_fetch_register(")
4860 : string("k_fetch_register("))
4861 + lex_cast(dwarf_regs[regname]) + string(")");
4862 // XXX: may we ever need to cast that to a narrower type?
40fe32e0 4863
8aabf152
FCE
4864 literal_number* inc = new literal_number(disp);
4865 inc->tok = e->tok;
40fe32e0 4866
8aabf152
FCE
4867 binary_expression *be = new binary_expression;
4868 be->tok = e->tok;
4869 be->left = get_arg1;
4870 be->op = "+";
4871 be->right = inc;
40fe32e0 4872
8aabf152 4873 functioncall *fc = new functioncall;
40fe32e0
SC
4874 switch (precision)
4875 {
4876 case 4: case -4:
4877 fc->function = "user_int"; break;
4878 case 8: case -8:
4879 fc->function = "user_long"; break;
4880 default: fc->function = "user_long";
4881 }
8aabf152
FCE
4882 fc->tok = e->tok;
4883 fc->args.push_back(be);
366af4e7 4884
8aabf152
FCE
4885 argexpr = fc;
4886 goto matched;
4887 }
4888 // invalid register name, fall through
4889 }
4890
4891 not_matched:
4892 // The asmarg operand was not recognized. Back down to dwarf.
4893 if (! session.suppress_warnings)
4894 session.print_warning ("Downgrading SDT_V2 probe argument to dwarf, can't parse '"+asmarg+"'", e->tok);
4895 assert (argexpr == 0);
4896 need_debug_info = true;
4897 provide (e);
4898 return;
366af4e7 4899
8aabf152
FCE
4900 matched:
4901 assert (argexpr != 0);
366af4e7
RM
4902
4903 if (session.verbose > 2)
8aabf152 4904 clog << "mapped asm operand " << asmarg << " to " << *argexpr << endl;
366af4e7 4905
aff5d390 4906 if (e->components.empty()) // We have a scalar
8aabf152
FCE
4907 {
4908 if (e->addressof)
4909 throw semantic_error("cannot take address of sdt variable", e->tok);
4910 provide (argexpr);
4911 return;
4912 }
4913 else // $var->foo
4914 {
4915 cast_op *cast = new cast_op;
4916 cast->name = "@cast";
4917 cast->tok = e->tok;
4918 cast->operand = argexpr;
4919 cast->components = e->components;
4920 cast->type_name = probe_name + "_arg" + lex_cast(argno);
4921 cast->module = process_name;
4922 cast->visit(this);
4923 return;
4924 }
366af4e7 4925
8aabf152 4926 /* NOTREACHED */
aff5d390
SC
4927 }
4928 catch (const semantic_error &er)
4929 {
4930 e->chain (er);
4931 provide (e);
4932 }
4933}
4934
4935
4936void
4937sdt_kprobe_var_expanding_visitor::visit_target_symbol (target_symbol *e)
4938{
4939 try
4940 {
277c21bc 4941 if (e->name == "$$name")
aff5d390
SC
4942 {
4943 if (e->addressof)
4944 throw semantic_error("cannot take address of sdt context variable", e->tok);
4945
4946 literal_string *myname = new literal_string (probe_name);
4947 myname->tok = e->tok;
4948 provide(myname);
4949 return;
4950 }
277c21bc 4951 if (e->name == "$$provider")
a794dbeb
FCE
4952 {
4953 if (e->addressof)
4954 throw semantic_error("cannot take address of sdt context variable", e->tok);
4955
4956 literal_string *myname = new literal_string (provider_name);
4957 myname->tok = e->tok;
4958 provide(myname);
4959 return;
4960 }
aff5d390 4961
5ecaa5a7 4962 int argno = -1;
aff5d390
SC
4963 try
4964 {
5ecaa5a7
JS
4965 if (startswith(e->name, "$arg"))
4966 argno = lex_cast<int>(e->name.substr(4));
aff5d390
SC
4967 }
4968 catch (const runtime_error& f) // non-integral $arg suffix: e.g. $argKKKSDF
4969 {
aff5d390 4970 }
5ecaa5a7
JS
4971 if (argno < 0)
4972 throw semantic_error("invalid variable, must be of the form $argN", e->tok);
aff5d390
SC
4973 if (argno < 1 || argno > arg_count)
4974 throw semantic_error("invalid argument number", e->tok);
5ecaa5a7 4975
c69a87e0
FCE
4976 bool lvalue = is_active_lvalue(e);
4977 functioncall *fc = new functioncall;
63ea4244 4978
c69a87e0
FCE
4979 // First two args are hidden: 1. pointer to probe name 2. task id
4980 if (arg_count < 2)
aff5d390
SC
4981 {
4982 fc->function = "ulong_arg";
4983 fc->type = pe_long;
4984 fc->tok = e->tok;
4985 // skip the hidden args
4986 literal_number* num = new literal_number(argno + 2);
4987 num->tok = e->tok;
4988 fc->args.push_back(num);
4989 }
4990 else
4991 {
4992 // args are passed in arg3 as members of a struct
4993 fc->function = "user_long";
4994 fc->tok = e->tok;
4995 binary_expression *be = new binary_expression;
4996 be->tok = e->tok;
4997 functioncall *get_arg1 = new functioncall;
4998 get_arg1->function = "pointer_arg";
4999 get_arg1->tok = e->tok;
5000 // arg3 is the pointer to a struct of arguments
c57ea854 5001 literal_number* num = new literal_number(3);
aff5d390
SC
5002 num->tok = e->tok;
5003 get_arg1->args.push_back(num);
5004
5005 be->left = get_arg1;
5006 be->op = "+";
5007 // offset in struct to the desired arg
5008 literal_number* inc = new literal_number((argno - 1) * 8);
5009 inc->tok = e->tok;
5010 be->right = inc;
5011 fc->args.push_back(be);
5012 }
c69a87e0 5013 if (lvalue)
aff5d390 5014 *(target_symbol_setter_functioncalls.top()) = fc;
63ea4244 5015
aff5d390
SC
5016 if (e->components.empty()) // We have a scalar
5017 {
5018 if (e->addressof)
5019 throw semantic_error("cannot take address of sdt variable", e->tok);
63ea4244 5020
aff5d390
SC
5021 provide(fc);
5022 return;
5023 }
c69a87e0 5024 cast_op *cast = new cast_op;
277c21bc 5025 cast->name = "@cast";
c69a87e0
FCE
5026 cast->tok = e->tok;
5027 cast->operand = fc;
5028 cast->components = e->components;
7f6b80bd 5029 cast->type_name = probe_name + "_arg" + lex_cast(argno);
c69a87e0 5030 cast->module = process_name;
63ea4244 5031
c69a87e0 5032 cast->visit(this);
7a05f484 5033 }
c69a87e0 5034 catch (const semantic_error &er)
ad002306 5035 {
1af1e62d 5036 e->chain (er);
c69a87e0 5037 provide (e);
ad002306 5038 }
7a05f484 5039}
46b84a80 5040
277c21bc 5041
edce5b67
JS
5042struct sdt_query : public base_query
5043{
5044 sdt_query(probe * base_probe, probe_point * base_loc,
5045 dwflpp & dw, literal_map_t const & params,
5046 vector<derived_probe *> & results);
5047
5048 void handle_query_module();
5049
5050private:
15284963 5051 stap_sdt_probe_type probe_type;
9109f487 5052 enum {probe_section=0, note_section=1} probe_loc;
edce5b67
JS
5053 probe * base_probe;
5054 probe_point * base_loc;
6846cfc8 5055 literal_map_t const & params;
edce5b67 5056 vector<derived_probe *> & results;
a794dbeb
FCE
5057 string pp_mark;
5058 string pp_provider;
edce5b67
JS
5059
5060 set<string> probes_handled;
5061
5062 Elf_Data *pdata;
5063 size_t probe_scn_offset;
5064 size_t probe_scn_addr;
aff5d390 5065 uint64_t arg_count;
40fe32e0 5066 GElf_Addr base;
c57ea854 5067 GElf_Addr pc;
aff5d390 5068 string arg_string;
edce5b67 5069 string probe_name;
a794dbeb 5070 string provider_name;
79a0ca08 5071 Dwarf_Addr semaphore;
edce5b67
JS
5072
5073 bool init_probe_scn();
6b51ee12 5074 bool get_next_probe();
c57ea854
SC
5075 void iterate_over_probe_entries();
5076 void handle_probe_entry();
edce5b67 5077
40fe32e0
SC
5078 static void setup_note_probe_entry_callback (void *object, int type, const char *data, size_t len);
5079 void setup_note_probe_entry (int type, const char *data, size_t len);
5080
edce5b67 5081 void convert_probe(probe *base);
4ddb6dd0 5082 void record_semaphore(vector<derived_probe *> & results, unsigned start);
c72aa911 5083 probe* convert_location();
40fe32e0 5084 bool have_uprobe() {return probe_type == uprobe1_type || probe_type == uprobe2_type || probe_type == uprobe3_type;}
aff5d390 5085 bool have_kprobe() {return probe_type == kprobe1_type || probe_type == kprobe2_type;}
c57ea854
SC
5086 bool have_debuginfo_uprobe(bool need_debug_info)
5087 {return probe_type == uprobe1_type
40fe32e0 5088 || ((probe_type == uprobe2_type || probe_type == uprobe3_type)
c57ea854 5089 && need_debug_info);}
40fe32e0 5090 bool have_debuginfoless_uprobe() {return probe_type == uprobe2_type || probe_type == uprobe3_type;}
edce5b67
JS
5091};
5092
5093
5094sdt_query::sdt_query(probe * base_probe, probe_point * base_loc,
5095 dwflpp & dw, literal_map_t const & params,
5096 vector<derived_probe *> & results):
5097 base_query(dw, params), base_probe(base_probe),
6846cfc8 5098 base_loc(base_loc), params(params), results(results)
edce5b67 5099{
a794dbeb
FCE
5100 assert(get_string_param(params, TOK_MARK, pp_mark));
5101 get_string_param(params, TOK_PROVIDER, pp_provider); // pp_provider == "" -> unspecified
5102
ef428667
FCE
5103 // PR10245: permit usage of dtrace-y "-" separator in marker name;
5104 // map it to double-underscores.
5105 size_t pos = 0;
5106 while (1) // there may be more than one
5107 {
a794dbeb 5108 size_t i = pp_mark.find("-", pos);
ef428667 5109 if (i == string::npos) break;
a794dbeb 5110 pp_mark.replace (i, 1, "__");
ef428667
FCE
5111 pos = i+1; // resume searching after the inserted __
5112 }
a794dbeb
FCE
5113
5114 // XXX: same for pp_provider?
edce5b67
JS
5115}
5116
5117
5118void
c57ea854 5119sdt_query::handle_probe_entry()
edce5b67 5120{
c57ea854
SC
5121 if (! have_uprobe()
5122 && !probes_handled.insert(probe_name).second)
edce5b67
JS
5123 return;
5124
5125 if (sess.verbose > 3)
c57ea854
SC
5126 {
5127 clog << "matched probe_name " << probe_name << " probe_type ";
5128 switch (probe_type)
5129 {
5130 case uprobe1_type:
5131 clog << "uprobe1 at 0x" << hex << pc << dec << endl;
5132 break;
5133 case uprobe2_type:
5134 clog << "uprobe2 at 0x" << hex << pc << dec << endl;
5135 break;
40fe32e0
SC
5136 case uprobe3_type:
5137 clog << "uprobe3 at 0x" << hex << pc << dec << endl;
5138 break;
c57ea854
SC
5139 case kprobe1_type:
5140 clog << "kprobe1" << endl;
5141 break;
5142 case kprobe2_type:
5143 clog << "kprobe2" << endl;
5144 break;
5145 }
5146 }
edce5b67 5147
c57ea854
SC
5148 // Extend the derivation chain
5149 probe *new_base = convert_location();
5150 probe_point *new_location = new_base->locations[0];
5151
5152 bool kprobe_found = false;
5153 bool need_debug_info = false;
5154
5155 Dwarf_Addr bias;
5156 Elf* elf = (dwarf_getelf (dwfl_module_getdwarf (dw.mod_info->mod, &bias))
5157 ?: dwfl_module_getelf (dw.mod_info->mod, &bias));
5158
5159 if (have_kprobe())
edce5b67 5160 {
c57ea854
SC
5161 convert_probe(new_base);
5162 kprobe_found = true;
5163 // Expand the local variables in the probe body
5164 sdt_kprobe_var_expanding_visitor svv (module_val,
5165 provider_name,
5166 probe_name,
5167 arg_string,
5168 arg_count);
5169 svv.replace (new_base->body);
5170 }
5171 else
5172 {
5173 /* Figure out the architecture of this particular ELF file.
5174 The dwarfless register-name mappings depend on it. */
5175 GElf_Ehdr ehdr_mem;
5176 GElf_Ehdr* em = gelf_getehdr (elf, &ehdr_mem);
5177 if (em == 0) { dwfl_assert ("dwfl_getehdr", dwfl_errno()); }
5178 int elf_machine = em->e_machine;
5179 sdt_uprobe_var_expanding_visitor svv (sess, elf_machine,
5180 module_val,
5181 provider_name,
5182 probe_name,
5183 arg_string,
5184 arg_count);
5185 svv.replace (new_base->body);
5186 need_debug_info = svv.need_debug_info;
5187 }
5188
5189 unsigned i = results.size();
edce5b67 5190
c57ea854
SC
5191 if (have_kprobe())
5192 derive_probes(sess, new_base, results);
5193
5194 else
5195 {
5196 // XXX: why not derive_probes() in the uprobes case too?
5197 literal_map_t params;
5198 for (unsigned i = 0; i < new_location->components.size(); ++i)
39a3e397 5199 {
c57ea854
SC
5200 probe_point::component *c = new_location->components[i];
5201 params[c->functor] = c->arg;
39a3e397 5202 }
30263a73 5203
c57ea854
SC
5204 dwarf_query q(new_base, new_location, dw, params, results, "", "");
5205 q.has_mark = true; // enables mid-statement probing
edce5b67 5206
c57ea854
SC
5207 // V2 probes need dwarf info in case of a variable reference
5208 if (have_debuginfo_uprobe(need_debug_info))
5209 dw.iterate_over_modules(&query_module, &q);
5210 else if (have_debuginfoless_uprobe())
aff5d390 5211 {
c57ea854
SC
5212 string section;
5213 Dwarf_Addr reloc_addr = q.statement_num_val + bias;
5214 if (dwfl_module_relocations (q.dw.mod_info->mod) > 0)
5215 {
5216 dwfl_module_relocate_address (q.dw.mod_info->mod, &reloc_addr);
5217 section = ".dynamic";
5218 }
5219 else
5220 section = ".absolute";
5221
5222 uprobe_derived_probe* p =
5223 new uprobe_derived_probe ("", "", 0, q.module_val, section,
5224 q.statement_num_val, reloc_addr, q, 0);
5225 p->saveargs (arg_count);
5226 results.push_back (p);
aff5d390 5227 }
c57ea854
SC
5228 }
5229 record_semaphore(results, i);
5230}
edce5b67 5231
4ddb6dd0 5232
c57ea854
SC
5233void
5234sdt_query::handle_query_module()
5235{
5236 if (!init_probe_scn())
5237 return;
edce5b67 5238
c57ea854
SC
5239 if (sess.verbose > 3)
5240 clog << "TOK_MARK: " << pp_mark << " TOK_PROVIDER: " << pp_provider << endl;
edce5b67 5241
40fe32e0
SC
5242 if (probe_loc == note_section)
5243 {
5244 GElf_Shdr shdr_mem;
5245 GElf_Shdr *shdr = dw.get_section (".stapsdt.base", &shdr_mem);
5246
5247 if (shdr)
5248 base = shdr->sh_addr;
5249 else
5250 base = 0;
5251 dw.iterate_over_notes ((void*) this, &sdt_query::setup_note_probe_entry_callback);
5252 }
5253 else
5254 iterate_over_probe_entries ();
edce5b67
JS
5255}
5256
5257
5258bool
5259sdt_query::init_probe_scn()
5260{
448a86b7 5261 Elf* elf;
edce5b67 5262 GElf_Shdr shdr_mem;
40fe32e0
SC
5263
5264 GElf_Shdr *shdr = dw.get_section (".note.stapsdt", &shdr_mem);
5265 if (shdr)
5266 {
5267 probe_loc = note_section;
5268 return true;
5269 }
edce5b67 5270
448a86b7 5271 shdr = dw.get_section (".probes", &shdr_mem, &elf);
fea74777 5272 if (shdr)
edce5b67 5273 {
fea74777
SC
5274 pdata = elf_getdata_rawchunk (elf, shdr->sh_offset, shdr->sh_size, ELF_T_BYTE);
5275 probe_scn_offset = 0;
5276 probe_scn_addr = shdr->sh_addr;
5277 assert (pdata != NULL);
5278 if (sess.verbose > 4)
5279 clog << "got .probes elf scn_addr@0x" << probe_scn_addr << dec
5280 << ", size: " << pdata->d_size << endl;
40fe32e0 5281 probe_loc = probe_section;
fea74777 5282 return true;
edce5b67 5283 }
fea74777 5284 else
edce5b67 5285 return false;
edce5b67
JS
5286}
5287
40fe32e0
SC
5288void
5289sdt_query::setup_note_probe_entry_callback (void *object, int type, const char *data, size_t len)
5290{
5291 sdt_query *me = (sdt_query*)object;
5292 me->setup_note_probe_entry (type, data, len);
5293}
5294
5295
5296void
5297sdt_query::setup_note_probe_entry (int type, const char *data, size_t len)
5298{
5299 // if (nhdr.n_namesz == sizeof _SDT_NOTE_NAME
5300 // && !memcmp (data->d_buf + name_off,
5301 // _SDT_NOTE_NAME, sizeof _SDT_NOTE_NAME))
5302
5303 // probes are in the .note.stapsdt section
5304#define _SDT_NOTE_TYPE 3
5305 if (type != _SDT_NOTE_TYPE)
5306 return;
5307
5308 union
5309 {
5310 Elf64_Addr a64[3];
5311 Elf32_Addr a32[3];
5312 } buf;
5313 Dwarf_Addr bias;
5314 Elf* elf = (dwfl_module_getelf (dw.mod_info->mod, &bias));
5315 Elf_Data dst =
5316 {
5317 &buf, ELF_T_ADDR, EV_CURRENT,
5318 gelf_fsize (elf, ELF_T_ADDR, 3, EV_CURRENT), 0, 0
5319 };
5320 assert (dst.d_size <= sizeof buf);
5321
5322 if (len < dst.d_size + 3)
5323 return;
5324
5325 Elf_Data src =
5326 {
5327 (void *) data, ELF_T_ADDR, EV_CURRENT,
5328 dst.d_size, 0, 0
5329 };
5330
5331 if (gelf_xlatetom (elf, &dst, &src,
5332 elf_getident (elf, NULL)[EI_DATA]) == NULL)
5333 printf ("gelf_xlatetom: %s", elf_errmsg (-1));
5334
5335 probe_type = uprobe3_type;
5336 const char * provider = data + dst.d_size;
5337 provider_name = provider;
5338 const char *name = (const char*)memchr (provider, '\0', data + len - provider);
5339 probe_name = ++name;
5340
5341 // Did we find a matching probe?
5342 if (! (dw.function_name_matches_pattern (probe_name, pp_mark)
5343 && ((pp_provider == "")
5344 || dw.function_name_matches_pattern (provider_name, pp_provider))))
5345 return;
5346
5347 const char *args = (const char*)memchr (name, '\0', data + len - name);
5348 if (args++ == NULL ||
5349 memchr (args, '\0', data + len - name) != data + len - 1)
5350 if (name == NULL)
5351 return;
5352 arg_string = args;
5353
5354 arg_count = 0;
5355 for (unsigned i = 0; i < arg_string.length(); i++)
5356 if (arg_string[i] == ' ')
5357 arg_count += 1;
5358 if (arg_string.length() != 0)
5359 arg_count += 1;
5360
5361 GElf_Addr base_ref;
5362 if (gelf_getclass (elf) == ELFCLASS32)
5363 {
5364 pc = buf.a32[0];
5365 base_ref = buf.a32[1];
5366 semaphore = buf.a32[2];
5367 }
5368 else
5369 {
5370 pc = buf.a64[0];
5371 base_ref = buf.a64[1];
5372 semaphore = buf.a64[2];
5373 }
5374
5375 semaphore += base - base_ref;
5376 pc += base - base_ref;
5377
5378 if (sess.verbose > 4)
5379 clog << "saw .note.stapsdt " << probe_name << (provider_name != "" ? " (provider "+provider_name+") " : "")
5380 << "@0x" << hex << pc << dec << endl;
5381
5382 handle_probe_entry();
5383}
5384
5385
c57ea854
SC
5386void
5387sdt_query::iterate_over_probe_entries()
edce5b67 5388{
c57ea854 5389 // probes are in the .probe section
edce5b67
JS
5390 while (probe_scn_offset < pdata->d_size)
5391 {
aff5d390
SC
5392 stap_sdt_probe_entry_v1 *pbe_v1 = (stap_sdt_probe_entry_v1 *) ((char*)pdata->d_buf + probe_scn_offset);
5393 stap_sdt_probe_entry_v2 *pbe_v2 = (stap_sdt_probe_entry_v2 *) ((char*)pdata->d_buf + probe_scn_offset);
15284963 5394 probe_type = (stap_sdt_probe_type)(pbe_v1->type_a);
aff5d390 5395 if (! have_uprobe() && ! have_kprobe())
edce5b67
JS
5396 {
5397 // Unless this is a mangled .probes section, this happens
5398 // because the name of the probe comes first, followed by
5399 // the sentinel.
5400 if (sess.verbose > 5)
5401 clog << "got unknown probe_type: 0x" << hex << probe_type
5402 << dec << endl;
5403 probe_scn_offset += sizeof(__uint32_t);
5404 continue;
5405 }
aff5d390
SC
5406 if ((long)pbe_v1 % sizeof(__uint64_t)) // we have stap_sdt_probe_entry_v1.type_b
5407 {
5408 pbe_v1 = (stap_sdt_probe_entry_v1*)((char*)pbe_v1 - sizeof(__uint32_t));
5409 if (pbe_v1->type_b != uprobe1_type && pbe_v1->type_b != kprobe1_type)
5410 continue;
5411 }
5412
5413 if (probe_type == uprobe1_type || probe_type == kprobe1_type)
5414 {
79a0ca08 5415 if (pbe_v1->name == 0) // No name possibly means we have a .so with a relocation
c57ea854 5416 return;
79a0ca08 5417 semaphore = 0;
aff5d390 5418 probe_name = (char*)((char*)pdata->d_buf + pbe_v1->name - (char*)probe_scn_addr);
a794dbeb 5419 provider_name = ""; // unknown
aff5d390
SC
5420 if (probe_type == uprobe1_type)
5421 {
5422 pc = pbe_v1->arg;
5423 arg_count = 0;
5424 }
5425 else if (probe_type == kprobe1_type)
5426 arg_count = pbe_v1->arg;
5427 probe_scn_offset += sizeof (stap_sdt_probe_entry_v1);
5428 }
5429 else if (probe_type == uprobe2_type || probe_type == kprobe2_type)
5430 {
79a0ca08 5431 if (pbe_v2->name == 0) // No name possibly means we have a .so with a relocation
c57ea854 5432 return;
79a0ca08 5433 semaphore = pbe_v2->semaphore;
aff5d390 5434 probe_name = (char*)((char*)pdata->d_buf + pbe_v2->name - (char*)probe_scn_addr);
a794dbeb 5435 provider_name = (char*)((char*)pdata->d_buf + pbe_v2->provider - (char*)probe_scn_addr);
aff5d390
SC
5436 arg_count = pbe_v2->arg_count;
5437 pc = pbe_v2->pc;
5438 if (pbe_v2->arg_string)
5439 arg_string = (char*)((char*)pdata->d_buf + pbe_v2->arg_string - (char*)probe_scn_addr);
79a0ca08
SC
5440 // skip over pbe_v2, probe_name text and provider text
5441 probe_scn_offset = ((long)(pbe_v2->name) - (long)(probe_scn_addr)) + probe_name.length();
5442 probe_scn_offset += sizeof (__uint32_t) - probe_scn_offset % sizeof (__uint32_t);
aff5d390 5443 }
edce5b67 5444 if (sess.verbose > 4)
a794dbeb 5445 clog << "saw .probes " << probe_name << (provider_name != "" ? " (provider "+provider_name+") " : "")
aff5d390 5446 << "@0x" << hex << pc << dec << endl;
edce5b67 5447
a794dbeb
FCE
5448 if (dw.function_name_matches_pattern (probe_name, pp_mark)
5449 && ((pp_provider == "") || dw.function_name_matches_pattern (provider_name, pp_provider)))
c57ea854 5450 handle_probe_entry ();
edce5b67 5451 }
edce5b67
JS
5452}
5453
5454
6846cfc8 5455void
4ddb6dd0 5456sdt_query::record_semaphore (vector<derived_probe *> & results, unsigned start)
6846cfc8 5457{
a794dbeb
FCE
5458 for (unsigned i=0; i<2; i++) {
5459 // prefer with-provider symbol; look without provider prefix for backward compatibility only
5460 string semaphore = (i==0 ? (provider_name+"_") : "") + probe_name + "_semaphore";
5461 // XXX: multiple addresses?
5462 if (sess.verbose > 2)
5463 clog << "looking for semaphore symbol " << semaphore;
5464
79a0ca08
SC
5465 Dwarf_Addr addr;
5466 if (this->semaphore)
5467 addr = this->semaphore;
5468 else
5469 addr = lookup_symbol_address(dw.module, semaphore.c_str());
a794dbeb
FCE
5470 if (addr)
5471 {
40fe32e0
SC
5472 if (probe_type != uprobe3_type
5473 && dwfl_module_relocations (dw.module) > 0)
a794dbeb
FCE
5474 dwfl_module_relocate_address (dw.module, &addr);
5475 // XXX: relocation basis?
5476 for (unsigned i = start; i < results.size(); ++i)
5477 results[i]->sdt_semaphore_addr = addr;
5478 if (sess.verbose > 2)
5479 clog << ", found at 0x" << hex << addr << dec << endl;
5480 return;
5481 }
5482 else
5483 if (sess.verbose > 2)
5484 clog << ", not found" << endl;
5485 }
6846cfc8
SC
5486}
5487
5488
edce5b67
JS
5489void
5490sdt_query::convert_probe (probe *base)
5491{
5492 block *b = new block;
5493 b->tok = base->body->tok;
5494
5495 // XXX: Does this also need to happen for i386 under x86_64 stap?
ebbd2b45 5496 if (sess.architecture == "i386" && have_kprobe())
edce5b67
JS
5497 {
5498 functioncall *rp = new functioncall;
edce5b67
JS
5499 rp->function = "regparm";
5500 rp->tok = b->tok;
5501 literal_number* littid = new literal_number(0);
5502 littid->tok = b->tok;
5503 rp->args.push_back(littid);
5504 expr_statement* es = new expr_statement;
5505 es->tok = b->tok;
5506 es->value = rp;
5507 b->statements.push_back(es);
5508 }
edce5b67 5509
aff5d390 5510 if (have_kprobe())
edce5b67 5511 {
aff5d390 5512 // Generate: if (arg2 != kprobe2_type) next;
edce5b67
JS
5513 if_statement *istid = new if_statement;
5514 istid->thenblock = new next_statement;
5515 istid->elseblock = NULL;
5516 istid->tok = b->tok;
63ea4244 5517 istid->thenblock->tok = b->tok;
edce5b67
JS
5518 comparison *betid = new comparison;
5519 betid->op = "!=";
5520 betid->tok = b->tok;
5521
5522 functioncall *arg2 = new functioncall;
5523 arg2->function = "ulong_arg";
5524 arg2->tok = b->tok;
5525 literal_number* num = new literal_number(2);
5526 num->tok = b->tok;
5527 arg2->args.push_back(num);
5528
5529 betid->left = arg2;
aff5d390 5530 literal_number* littid = new literal_number(probe_type);
edce5b67
JS
5531 littid->tok = b->tok;
5532 betid->right = littid;
5533 istid->condition = betid;
5534 b->statements.push_back(istid);
5535 }
5536
5537 // Generate: if (arg1 != mark("label")) next;
5538 functioncall *fc = new functioncall;
bbafcb1e 5539 fc->function = "ulong_arg";
edce5b67 5540 fc->tok = b->tok;
bbafcb1e 5541 literal_number* num = new literal_number(1);
edce5b67
JS
5542 num->tok = b->tok;
5543 fc->args.push_back(num);
5544
5545 functioncall *fcus = new functioncall;
5546 fcus->function = "user_string";
5547 fcus->type = pe_string;
5548 fcus->tok = b->tok;
5549 fcus->args.push_back(fc);
5550
5551 if_statement *is = new if_statement;
5552 is->thenblock = new next_statement;
5553 is->elseblock = NULL;
5554 is->tok = b->tok;
63ea4244 5555 is->thenblock->tok = b->tok;
edce5b67
JS
5556 comparison *be = new comparison;
5557 be->op = "!=";
5558 be->tok = b->tok;
5559 be->left = fcus;
5560 be->right = new literal_string(probe_name);
63ea4244 5561 be->right->tok = b->tok;
edce5b67
JS
5562 is->condition = be;
5563 b->statements.push_back(is);
5564
5565 // Now replace the body
5566 b->statements.push_back(base->body);
5567 base->body = b;
5568}
5569
5570
c72aa911
JS
5571probe*
5572sdt_query::convert_location ()
edce5b67 5573{
c72aa911
JS
5574 probe_point* specific_loc = new probe_point(*base_loc);
5575 probe_point* derived_loc = new probe_point(*base_loc);
edce5b67 5576
c72aa911
JS
5577 for (unsigned i = 0; i < derived_loc->components.size(); ++i)
5578 if (derived_loc->components[i]->functor == TOK_MARK)
5579 {
5580 // replace the possibly wildcarded arg with the specific marker name
5581 specific_loc->components[i] =
5582 new probe_point::component(TOK_MARK, new literal_string(probe_name));
edce5b67 5583
a794dbeb
FCE
5584 // XXX: similarly, fill in a TOK_PROVIDER() on the base_loc if
5585 // one was missing/unspecified/wildcarded.
5586
aff5d390
SC
5587 if (sess.verbose > 3)
5588 switch (probe_type)
5589 {
5590 case uprobe1_type:
5591 clog << "probe_type == uprobe1, use statement addr: 0x"
5592 << hex << pc << dec << endl;
5593 break;
5594 case uprobe2_type:
5595 clog << "probe_type == uprobe2, use statement addr: 0x"
5596 << hex << pc << dec << endl;
5597 break;
40fe32e0
SC
5598 case uprobe3_type:
5599 clog << "probe_type == uprobe3, use statement addr: 0x"
5600 << hex << pc << dec << endl;
5601 break;
5602 case kprobe1_type:
5603 clog << "probe_type == kprobe1" << endl;
5604 break;
5605 case kprobe2_type:
5606 clog << "probe_type == kprobe2" << endl;
5607 break;
aff5d390
SC
5608 default:
5609 clog << "probe_type == use_uprobe_no_dwarf, use label name: "
a794dbeb 5610 << "_stapprobe1_" << pp_mark << endl;
aff5d390
SC
5611 }
5612
c72aa911
JS
5613 switch (probe_type)
5614 {
aff5d390
SC
5615 case uprobe1_type:
5616 case uprobe2_type:
40fe32e0 5617 case uprobe3_type:
c72aa911
JS
5618 // process("executable").statement(probe_arg)
5619 derived_loc->components[i] =
9ea68eb9 5620 new probe_point::component(TOK_STATEMENT,
aff5d390 5621 new literal_number(pc, true));
c72aa911
JS
5622 break;
5623
aff5d390
SC
5624 case kprobe1_type:
5625 case kprobe2_type:
b642c901
SC
5626 // kernel.function("*getegid*")
5627 derived_loc->components[i] =
5628 new probe_point::component(TOK_FUNCTION, new literal_string("*getegid*"));
5629 if (derived_loc->components[i - 1]->functor == TOK_LIBRARY)
5630 derived_loc->components.erase (derived_loc->components.begin() + i - 1);
5631 break;
c72aa911 5632
a794dbeb 5633 default: // deprecated
c72aa911
JS
5634 // process("executable").function("*").label("_stapprobe1_MARK_NAME")
5635 derived_loc->components[i] =
5636 new probe_point::component(TOK_FUNCTION, new literal_string("*"));
5637 derived_loc->components.push_back
5638 (new probe_point::component(TOK_LABEL,
a794dbeb 5639 new literal_string("_stapprobe1_" + pp_mark)));
c72aa911
JS
5640 break;
5641 }
5642 }
5643 else if (derived_loc->components[i]->functor == TOK_PROCESS
aff5d390 5644 && have_kprobe())
63b4fd14 5645 {
c72aa911 5646 derived_loc->components[i] = new probe_point::component(TOK_KERNEL);
63b4fd14 5647 }
edce5b67 5648
c72aa911 5649 return base_probe->create_alias(derived_loc, specific_loc);
edce5b67
JS
5650}
5651
5652
20c6c071 5653void
5227f1ea 5654dwarf_builder::build(systemtap_session & sess,
7a053d3b 5655 probe * base,
20c6c071 5656 probe_point * location,
86bf665e 5657 literal_map_t const & parameters,
20c6c071
GH
5658 vector<derived_probe *> & finished_results)
5659{
b20febf3
FCE
5660 // NB: the kernel/user dwlfpp objects are long-lived.
5661 // XXX: but they should be per-session, as this builder object
5662 // may be reused if we try to cross-instrument multiple targets.
84048984 5663
7a24d422
FCE
5664 dwflpp* dw = 0;
5665
7a24d422 5666 string module_name;
ae2552da
FCE
5667 if (has_null_param (parameters, TOK_KERNEL))
5668 {
5669 dw = get_kern_dw(sess, "kernel");
5670 }
5671 else if (get_param (parameters, TOK_MODULE, module_name))
b8da0ad1 5672 {
ae2552da 5673 dw = get_kern_dw(sess, module_name);
b8da0ad1 5674 }
7a24d422 5675 else if (get_param (parameters, TOK_PROCESS, module_name))
b8da0ad1 5676 {
63b4fd14 5677 string library_name;
b642c901 5678 user_path = find_executable (module_name); // canonicalize it
63b4fd14 5679 if (get_param (parameters, TOK_LIBRARY, library_name))
b642c901
SC
5680 {
5681 module_name = find_executable (library_name, "LD_LIBRARY_PATH");
5682 user_lib = module_name;
5683 }
63b4fd14 5684 else
b642c901 5685 module_name = user_path; // canonicalize it
d0a7f5a9 5686
e34d5d13
FCE
5687 if (sess.kernel_config["CONFIG_UTRACE"] != string("y"))
5688 throw semantic_error ("process probes not available without kernel CONFIG_UTRACE");
5689
7a24d422
FCE
5690 // user-space target; we use one dwflpp instance per module name
5691 // (= program or shared library)
707bf35e 5692 dw = get_user_dw(sess, module_name);
c8959a29 5693 }
20c6c071 5694
5896cd05
MW
5695 if (sess.verbose > 3)
5696 clog << "dwarf_builder::build for " << module_name << endl;
5697
a794dbeb
FCE
5698 string dummy_mark_name; // NB: PR10245: dummy value, need not substitute - => __
5699 if (get_param(parameters, TOK_MARK, dummy_mark_name))
f28a8c28 5700 {
edce5b67
JS
5701 sdt_query sdtq(base, location, *dw, parameters, finished_results);
5702 dw->iterate_over_modules(&query_module, &sdtq);
5703 return;
7a05f484 5704 }
20c6c071 5705
8f14e444 5706 unsigned results_pre = finished_results.size();
b642c901 5707 dwarf_query q(base, location, *dw, parameters, finished_results, user_path, user_lib);
7a24d422
FCE
5708
5709 // XXX: kernel.statement.absolute is a special case that requires no
5710 // dwfl processing. This code should be in a separate builder.
7a24d422 5711 if (q.has_kernel && q.has_absolute)
37ebca01 5712 {
4baf0e53 5713 // assert guru mode for absolute probes
37ebca01
FCE
5714 if (! q.base_probe->privileged)
5715 {
edce5b67
JS
5716 throw semantic_error ("absolute statement probe in unprivileged script",
5717 q.base_probe->tok);
37ebca01
FCE
5718 }
5719
5720 // For kernel.statement(NUM).absolute probe points, we bypass
5721 // all the debuginfo stuff: We just wire up a
5722 // dwarf_derived_probe right here and now.
4baf0e53 5723 dwarf_derived_probe* p =
b8da0ad1
FCE
5724 new dwarf_derived_probe ("", "", 0, "kernel", "",
5725 q.statement_num_val, q.statement_num_val,
5726 q, 0);
37ebca01 5727 finished_results.push_back (p);
1a0dbc5a 5728 sess.unwindsym_modules.insert ("kernel");
37ebca01
FCE
5729 return;
5730 }
5731
51178501 5732 dw->iterate_over_modules(&query_module, &q);
8f14e444
FCE
5733
5734
5735 // PR11553 special processing: .return probes requested, but
5736 // some inlined function instances matched.
5737 unsigned i_n_r = q.inlined_non_returnable.size();
5738 unsigned results_post = finished_results.size();
5739 if (i_n_r > 0)
5740 {
5741 if ((results_pre == results_post) && (! sess.suppress_warnings)) // no matches; issue warning
5742 {
5743 string quicklist;
5744 for (set<string>::iterator it = q.inlined_non_returnable.begin();
5745 it != q.inlined_non_returnable.end();
5746 it++)
5747 {
5748 quicklist += " " + (*it);
5749 if (quicklist.size() > 80) // heuristic, don't make an overlong report line
5750 {
5751 quicklist += " ...";
5752 break;
5753 }
5754 }
c57ea854 5755
8f14e444
FCE
5756 sess.print_warning ("cannot probe .return of " + lex_cast(i_n_r) + " inlined function(s):" + quicklist);
5757 // There will be also a "no matches" semantic error generated.
5758 }
5759 if (sess.verbose > 1)
5760 clog << "skipped .return probe of " + lex_cast(i_n_r) + " inlined function(s)" << endl;
5761 if ((sess.verbose > 3) || (sess.verbose > 2 && results_pre == results_post)) // issue details with high verbosity
5762 {
5763 for (set<string>::iterator it = q.inlined_non_returnable.begin();
5764 it != q.inlined_non_returnable.end();
5765 it++)
5766 clog << (*it) << " ";
5767 clog << endl;
5768 }
5769 } // i_n_r > 0
5f0a03a6
JK
5770}
5771
5772symbol_table::~symbol_table()
5773{
c9efa5c9 5774 delete_map(map_by_addr);
5f0a03a6
JK
5775}
5776
5777void
2867a2a1
JS
5778symbol_table::add_symbol(const char *name, bool weak, bool descriptor,
5779 Dwarf_Addr addr, Dwarf_Addr *high_addr)
5f0a03a6 5780{
ab91b232
JK
5781#ifdef __powerpc__
5782 // Map ".sys_foo" to "sys_foo".
5783 if (name[0] == '.')
5784 name++;
5785#endif
5f0a03a6
JK
5786 func_info *fi = new func_info();
5787 fi->addr = addr;
5788 fi->name = name;
ab91b232 5789 fi->weak = weak;
2867a2a1 5790 fi->descriptor = descriptor;
5f0a03a6
JK
5791 map_by_name[fi->name] = fi;
5792 // TODO: Use a multimap in case there are multiple static
5793 // functions with the same name?
1c6b77e5 5794 map_by_addr.insert(make_pair(addr, fi));
5f0a03a6
JK
5795}
5796
5797enum info_status
5798symbol_table::read_symbols(FILE *f, const string& path)
5799{
5800 // Based on do_kernel_symbols() in runtime/staprun/symbols.c
5801 int ret;
2e67a43b
TM
5802 char *name = 0;
5803 char *mod = 0;
5f0a03a6
JK
5804 char type;
5805 unsigned long long addr;
5806 Dwarf_Addr high_addr = 0;
5807 int line = 0;
5808
5809 // %as (non-POSIX) mallocs space for the string and stores its address.
5810 while ((ret = fscanf(f, "%llx %c %as [%as", &addr, &type, &name, &mod)) > 0)
5811 {
2e67a43b
TM
5812 auto_free free_name(name);
5813 auto_free free_mod(mod);
5f0a03a6
JK
5814 line++;
5815 if (ret < 3)
5816 {
41c262f3 5817 cerr << "Symbol table error: Line "
5f0a03a6
JK
5818 << line
5819 << " of symbol list from "
5820 << path
5821 << " is not in correct format: address type name [module]";
5822 // Caller should delete symbol_table object.
5823 return info_absent;
5824 }
2e67a43b 5825 else if (ret > 3)
5f0a03a6
JK
5826 {
5827 // Modules are loaded above the kernel, so if we're getting
5828 // modules, we're done.
2e67a43b 5829 break;
5f0a03a6 5830 }
ab91b232 5831 if (type == 'T' || type == 't' || type == 'W')
2867a2a1 5832 add_symbol(name, (type == 'W'), false, (Dwarf_Addr) addr, &high_addr);
5f0a03a6
JK
5833 }
5834
1c6b77e5 5835 if (map_by_addr.size() < 1)
5f0a03a6
JK
5836 {
5837 cerr << "Symbol table error: "
5838 << path << " contains no function symbols." << endl;
5839 return info_absent;
5840 }
5841 return info_present;
5842}
5843
5844// NB: This currently unused. We use get_from_elf() instead because
5845// that gives us raw addresses -- which we need for modules -- whereas
5846// nm provides the address relative to the beginning of the section.
5847enum info_status
83ca3872
MW
5848symbol_table::read_from_elf_file(const string &path,
5849 const systemtap_session &sess)
5f0a03a6
JK
5850{
5851 FILE *f;
5852 string cmd = string("/usr/bin/nm -n --defined-only ") + path;
5853 f = popen(cmd.c_str(), "r");
5854 if (!f)
5855 {
5856 // nm failures are detected by pclose, not popen.
5857 cerr << "Internal error reading symbol table from "
5858 << path << " -- " << strerror (errno);
5859 return info_absent;
5860 }
5861 enum info_status status = read_symbols(f, path);
5862 if (pclose(f) != 0)
5863 {
83ca3872 5864 if (status == info_present && ! sess.suppress_warnings)
5f0a03a6
JK
5865 cerr << "Warning: nm cannot read symbol table from " << path;
5866 return info_absent;
5867 }
5868 return status;
5869}
5870
5871enum info_status
83ca3872
MW
5872symbol_table::read_from_text_file(const string& path,
5873 const systemtap_session &sess)
5f0a03a6
JK
5874{
5875 FILE *f = fopen(path.c_str(), "r");
5876 if (!f)
5877 {
83ca3872
MW
5878 if (! sess.suppress_warnings)
5879 cerr << "Warning: cannot read symbol table from "
5880 << path << " -- " << strerror (errno);
5f0a03a6
JK
5881 return info_absent;
5882 }
5883 enum info_status status = read_symbols(f, path);
5884 (void) fclose(f);
5885 return status;
5886}
5887
46f7b6be
JK
5888void
5889symbol_table::prepare_section_rejection(Dwfl_Module *mod)
5890{
5891#ifdef __powerpc__
5892 /*
5893 * The .opd section contains function descriptors that can look
5894 * just like function entry points. For example, there's a function
5895 * descriptor called "do_exit" that links to the entry point ".do_exit".
5896 * Reject all symbols in .opd.
5897 */
5898 opd_section = SHN_UNDEF;
5899 Dwarf_Addr bias;
5900 Elf* elf = (dwarf_getelf (dwfl_module_getdwarf (mod, &bias))
5901 ?: dwfl_module_getelf (mod, &bias));
5902 Elf_Scn* scn = 0;
5903 size_t shstrndx;
5904
5905 if (!elf)
5906 return;
fcc30d6d 5907 if (elf_getshdrstrndx (elf, &shstrndx) != 0)
46f7b6be
JK
5908 return;
5909 while ((scn = elf_nextscn(elf, scn)) != NULL)
5910 {
5911 GElf_Shdr shdr_mem;
5912 GElf_Shdr *shdr = gelf_getshdr(scn, &shdr_mem);
5913 if (!shdr)
5914 continue;
5915 const char *name = elf_strptr(elf, shstrndx, shdr->sh_name);
5916 if (!strcmp(name, ".opd"))
5917 {
5918 opd_section = elf_ndxscn(scn);
5919 return;
5920 }
5921 }
5922#endif
5923}
5924
5925bool
5926symbol_table::reject_section(GElf_Word section)
5927{
5928 if (section == SHN_UNDEF)
5929 return true;
5930#ifdef __powerpc__
5931 if (section == opd_section)
5932 return true;
5933#endif
5934 return false;
5935}
5936
5f0a03a6
JK
5937enum info_status
5938symbol_table::get_from_elf()
5939{
5940 Dwarf_Addr high_addr = 0;
5941 Dwfl_Module *mod = mod_info->mod;
5942 int syments = dwfl_module_getsymtab(mod);
5943 assert(syments);
46f7b6be 5944 prepare_section_rejection(mod);
5f0a03a6
JK
5945 for (int i = 1; i < syments; ++i)
5946 {
5947 GElf_Sym sym;
ab91b232
JK
5948 GElf_Word section;
5949 const char *name = dwfl_module_getsym(mod, i, &sym, &section);
2867a2a1 5950 if (name && GELF_ST_TYPE(sym.st_info) == STT_FUNC)
ab91b232 5951 add_symbol(name, (GELF_ST_BIND(sym.st_info) == STB_WEAK),
2867a2a1 5952 reject_section(section), sym.st_value, &high_addr);
5f0a03a6
JK
5953 }
5954 return info_present;
5955}
5956
5f0a03a6
JK
5957func_info *
5958symbol_table::get_func_containing_address(Dwarf_Addr addr)
5959{
1c6b77e5
JS
5960 iterator_t iter = map_by_addr.upper_bound(addr);
5961 if (iter == map_by_addr.begin())
5f0a03a6 5962 return NULL;
2e67a43b 5963 else
1c6b77e5 5964 return (--iter)->second;
5f0a03a6
JK
5965}
5966
5967func_info *
5968symbol_table::lookup_symbol(const string& name)
5969{
5970 map<string, func_info*>::iterator i = map_by_name.find(name);
5971 if (i == map_by_name.end())
5972 return NULL;
5973 return i->second;
5974}
5975
5976Dwarf_Addr
5977symbol_table::lookup_symbol_address(const string& name)
5978{
5979 func_info *fi = lookup_symbol(name);
5980 if (fi)
5981 return fi->addr;
5982 return 0;
5983}
5984
ab91b232
JK
5985// This is the kernel symbol table. The kernel macro cond_syscall creates
5986// a weak symbol for each system call and maps it to sys_ni_syscall.
5987// For system calls not implemented elsewhere, this weak symbol shows up
5988// in the kernel symbol table. Following the precedent of dwarfful stap,
5989// we refuse to consider such symbols. Here we delete them from our
5990// symbol table.
5991// TODO: Consider generalizing this and/or making it part of blacklist
5992// processing.
5993void
5994symbol_table::purge_syscall_stubs()
5995{
5996 Dwarf_Addr stub_addr = lookup_symbol_address("sys_ni_syscall");
5997 if (stub_addr == 0)
5998 return;
1c6b77e5 5999 range_t purge_range = map_by_addr.equal_range(stub_addr);
2e67a43b
TM
6000 for (iterator_t iter = purge_range.first;
6001 iter != purge_range.second;
1c6b77e5 6002 )
ab91b232 6003 {
1c6b77e5 6004 func_info *fi = iter->second;
2e67a43b 6005 if (fi->weak && fi->name != "sys_ni_syscall")
ab91b232 6006 {
2e67a43b 6007 map_by_name.erase(fi->name);
1c6b77e5 6008 map_by_addr.erase(iter++);
2e67a43b 6009 delete fi;
2e67a43b 6010 }
1c6b77e5
JS
6011 else
6012 iter++;
ab91b232
JK
6013 }
6014}
6015
5f0a03a6
JK
6016void
6017module_info::get_symtab(dwarf_query *q)
6018{
6019 systemtap_session &sess = q->sess;
6020
1c6b77e5
JS
6021 if (symtab_status != info_unknown)
6022 return;
6023
5f0a03a6
JK
6024 sym_table = new symbol_table(this);
6025 if (!elf_path.empty())
6026 {
83ca3872
MW
6027 if (name == TOK_KERNEL && !sess.kernel_symtab_path.empty()
6028 && ! sess.suppress_warnings)
5f0a03a6
JK
6029 cerr << "Warning: reading symbol table from "
6030 << elf_path
6031 << " -- ignoring "
6032 << sess.kernel_symtab_path
83ca3872 6033 << endl;
5f0a03a6
JK
6034 symtab_status = sym_table->get_from_elf();
6035 }
6036 else
6037 {
6038 assert(name == TOK_KERNEL);
6039 if (sess.kernel_symtab_path.empty())
6040 {
6041 symtab_status = info_absent;
6042 cerr << "Error: Cannot find vmlinux."
6043 << " Consider using --kmap instead of --kelf."
6044 << endl;;
6045 }
6046 else
6047 {
6048 symtab_status =
83ca3872 6049 sym_table->read_from_text_file(sess.kernel_symtab_path, sess);
5f0a03a6
JK
6050 if (symtab_status == info_present)
6051 {
6052 sess.sym_kprobes_text_start =
6053 sym_table->lookup_symbol_address("__kprobes_text_start");
6054 sess.sym_kprobes_text_end =
6055 sym_table->lookup_symbol_address("__kprobes_text_end");
6056 sess.sym_stext = sym_table->lookup_symbol_address("_stext");
5f0a03a6
JK
6057 }
6058 }
6059 }
6060 if (symtab_status == info_absent)
6061 {
6062 delete sym_table;
6063 sym_table = NULL;
6064 return;
6065 }
6066
ab91b232
JK
6067 if (name == TOK_KERNEL)
6068 sym_table->purge_syscall_stubs();
5f0a03a6
JK
6069}
6070
1c6b77e5
JS
6071// update_symtab reconciles data between the elf symbol table and the dwarf
6072// function enumeration. It updates the symbol table entries with the dwarf
6073// die that describes the function, which also signals to query_module_symtab
6074// that a statement probe isn't needed. In return, it also adds aliases to the
6075// function table for names that share the same addr/die.
6076void
6077module_info::update_symtab(cu_function_cache_t *funcs)
6078{
6079 if (!sym_table)
6080 return;
6081
6082 cu_function_cache_t new_funcs;
6083
6084 for (cu_function_cache_t::iterator func = funcs->begin();
6085 func != funcs->end(); func++)
6086 {
6087 // optimization: inlines will never be in the symbol table
6088 if (dwarf_func_inline(&func->second) != 0)
6089 continue;
6090
1ffb8bd1
JS
6091 // XXX We may want to make additional efforts to match mangled elf names
6092 // to dwarf too. MIPS_linkage_name can help, but that's sometimes
6093 // missing, so we may also need to try matching by address. See also the
6094 // notes about _Z in dwflpp::iterate_over_functions().
6095
1c6b77e5
JS
6096 func_info *fi = sym_table->lookup_symbol(func->first);
6097 if (!fi)
6098 continue;
6099
6100 // iterate over all functions at the same address
6101 symbol_table::range_t er = sym_table->map_by_addr.equal_range(fi->addr);
6102 for (symbol_table::iterator_t it = er.first; it != er.second; ++it)
6103 {
6104 // update this function with the dwarf die
6105 it->second->die = func->second;
6106
6107 // if this function is a new alias, then
6108 // save it to merge into the function cache
6109 if (it->second != fi)
b7478964 6110 new_funcs.insert(make_pair(it->second->name, it->second->die));
1c6b77e5
JS
6111 }
6112 }
6113
6114 // add all discovered aliases back into the function cache
6115 // NB: this won't replace any names that dwarf may have already found
6116 funcs->insert(new_funcs.begin(), new_funcs.end());
6117}
6118
5f0a03a6
JK
6119module_info::~module_info()
6120{
6121 if (sym_table)
6122 delete sym_table;
b55bc428
FCE
6123}
6124
935447c8 6125// ------------------------------------------------------------------------
888af770 6126// user-space probes
935447c8
DS
6127// ------------------------------------------------------------------------
6128
935447c8 6129
888af770 6130struct uprobe_derived_probe_group: public generic_dpg<uprobe_derived_probe>
935447c8 6131{
89ba3085
FCE
6132private:
6133 string make_pbm_key (uprobe_derived_probe* p) {
6134 return p->module + "|" + p->section + "|" + lex_cast(p->pid);
6135 }
6136
935447c8 6137public:
888af770 6138 void emit_module_decls (systemtap_session& s);
935447c8
DS
6139 void emit_module_init (systemtap_session& s);
6140 void emit_module_exit (systemtap_session& s);
6141};
6142
6143
888af770
FCE
6144void
6145uprobe_derived_probe::join_group (systemtap_session& s)
6146{
6147 if (! s.uprobe_derived_probes)
6148 s.uprobe_derived_probes = new uprobe_derived_probe_group ();
6149 s.uprobe_derived_probes->enroll (this);
93646f4d 6150 enable_task_finder(s);
a96d1db0 6151
8a03658e
JS
6152 // Ask buildrun.cxx to build extra module if needed, and
6153 // signal staprun to load that module
6154 s.need_uprobes = true;
a96d1db0
DN
6155}
6156
888af770 6157
c0f84e7b
SC
6158void
6159uprobe_derived_probe::getargs(std::list<std::string> &arg_set) const
6160{
6161 dwarf_derived_probe::getargs(arg_set);
6162 arg_set.insert(arg_set.end(), args.begin(), args.end());
6163}
6164
6165
6166void
6167uprobe_derived_probe::saveargs(int nargs)
6168{
6169 for (int i = 1; i <= nargs; i++)
6170 args.push_back("$arg" + lex_cast (i) + ":long");
6171}
6172
6173
2865d17a
DB
6174void
6175uprobe_derived_probe::emit_unprivileged_assertion (translator_output* o)
6176{
6177 // These probes are allowed for unprivileged users, but only in the
6178 // context of processes which they own.
6179 emit_process_owner_assertion (o);
6180}
6181
6182
888af770 6183struct uprobe_builder: public derived_probe_builder
a96d1db0 6184{
888af770 6185 uprobe_builder() {}
a96d1db0
DN
6186 virtual void build(systemtap_session & sess,
6187 probe * base,
6188 probe_point * location,
86bf665e 6189 literal_map_t const & parameters,
a96d1db0
DN
6190 vector<derived_probe *> & finished_results)
6191 {
888af770 6192 int64_t process, address;
a96d1db0 6193
888af770 6194 bool b1 = get_param (parameters, TOK_PROCESS, process);
ced347a9 6195 (void) b1;
888af770 6196 bool b2 = get_param (parameters, TOK_STATEMENT, address);
ced347a9 6197 (void) b2;
888af770
FCE
6198 bool rr = has_null_param (parameters, TOK_RETURN);
6199 assert (b1 && b2); // by pattern_root construction
a96d1db0 6200
0973d815 6201 finished_results.push_back(new uprobe_derived_probe(base, location, process, address, rr));
a96d1db0
DN
6202 }
6203};
6204
6205
6206void
775d51e5 6207uprobe_derived_probe_group::emit_module_decls (systemtap_session& s)
a96d1db0 6208{
888af770 6209 if (probes.empty()) return;
775d51e5 6210 s.op->newline() << "/* ---- user probes ---- */";
471fca5e
TM
6211 // If uprobes isn't in the kernel, pull it in from the runtime.
6212
6213 s.op->newline() << "#if defined(CONFIG_UPROBES) || defined(CONFIG_UPROBES_MODULE)";
6214 s.op->newline() << "#include <linux/uprobes.h>";
6215 s.op->newline() << "#else";
6216 s.op->newline() << "#include \"uprobes/uprobes.h\"";
6217 s.op->newline() << "#endif";
6218 s.op->newline() << "#ifndef UPROBES_API_VERSION";
6219 s.op->newline() << "#define UPROBES_API_VERSION 1";
6220 s.op->newline() << "#endif";
a96d1db0 6221
43241c44
FCE
6222 // We'll probably need at least this many:
6223 unsigned minuprobes = probes.size();
6224 // .. but we don't want so many that .bss is inflated (PR10507):
6225 unsigned uprobesize = 64;
6226 unsigned maxuprobesmem = 10*1024*1024; // 10 MB
6227 unsigned maxuprobes = maxuprobesmem / uprobesize;
6228
aaf7ffe8
FCE
6229 // Let's choose a value on the geometric middle. This should end up
6230 // between minuprobes and maxuprobes. It's OK if this number turns
6231 // out to be < minuprobes or > maxuprobes. At worst, we get a
6232 // run-time error of one kind (too few: missed uprobe registrations)
6233 // or another (too many: vmalloc errors at module load time).
6234 unsigned default_maxuprobes = (unsigned)sqrt((double)minuprobes * (double)maxuprobes);
43241c44 6235
6d0f3f0c 6236 s.op->newline() << "#ifndef MAXUPROBES";
43241c44 6237 s.op->newline() << "#define MAXUPROBES " << default_maxuprobes;
6d0f3f0c 6238 s.op->newline() << "#endif";
a96d1db0 6239
cc52276b
WC
6240 // Forward decls
6241 s.op->newline() << "#include \"uprobes-common.h\"";
6242
5e112f92
FCE
6243 // In .bss, the shared pool of uprobe/uretprobe structs. These are
6244 // too big to embed in the initialized .data stap_uprobe_spec array.
cc52276b
WC
6245 // XXX: consider a slab cache or somesuch for stap_uprobes
6246 s.op->newline() << "static struct stap_uprobe stap_uprobes [MAXUPROBES];";
5e112f92 6247 s.op->newline() << "DEFINE_MUTEX(stap_uprobes_lock);"; // protects against concurrent registration/unregistration
a96d1db0 6248
89ba3085
FCE
6249 s.op->assert_0_indent();
6250
89ba3085
FCE
6251 // Assign task-finder numbers as we build up the stap_uprobe_tf table.
6252 // This means we process probes[] in two passes.
6253 map <string,unsigned> module_index;
6254 unsigned module_index_ctr = 0;
6255
cc52276b
WC
6256 // not const since embedded task_finder_target struct changes
6257 s.op->newline() << "static struct stap_uprobe_tf stap_uprobe_finders[] = {";
89ba3085
FCE
6258 s.op->indent(1);
6259 for (unsigned i=0; i<probes.size(); i++)
6260 {
6261 uprobe_derived_probe *p = probes[i];
6262 string pbmkey = make_pbm_key (p);
6263 if (module_index.find (pbmkey) == module_index.end())
6264 {
6265 module_index[pbmkey] = module_index_ctr++;
6266
6267 s.op->newline() << "{";
6268 // NB: it's essential that make_pbm_key() use all of and
6269 // only the same fields as we're about to emit.
6270 s.op->line() << " .finder={";
6271 if (p->pid != 0)
6272 s.op->line() << " .pid=" << p->pid;
6273 else if (p->section == ".absolute") // proxy for ET_EXEC -> exec()'d program
6274 {
6275 s.op->line() << " .procname=" << lex_cast_qstring(p->module) << ",";
6276 s.op->line() << " .callback=&stap_uprobe_process_found,";
6277 }
aff5d390 6278 if (p->section != ".absolute") // ET_DYN
89ba3085 6279 {
63b4fd14
SC
6280 if (p->has_library && p->sdt_semaphore_addr != 0)
6281 s.op->line() << " .procname=\"" << p->path << "\", ";
89ba3085
FCE
6282 s.op->line() << " .mmap_callback=&stap_uprobe_mmap_found, ";
6283 s.op->line() << " .munmap_callback=&stap_uprobe_munmap_found, ";
19d91f6c 6284 s.op->line() << " .callback=&stap_uprobe_process_munmap,";
89ba3085
FCE
6285 }
6286
6287 s.op->line() << " },";
6288 s.op->line() << " .pathname=" << lex_cast_qstring(p->module) << ", ";
6289 s.op->line() << " },";
6290 }
c57ea854 6291 else
89ba3085
FCE
6292 ; // skip it in this pass, already have a suitable stap_uprobe_tf slot for it.
6293 }
6294 s.op->newline(-1) << "};";
6295
6296 s.op->assert_0_indent();
6297
cc52276b
WC
6298 // NB: read-only structure
6299 s.op->newline() << "static const struct stap_uprobe_spec stap_uprobe_specs [] = {";
a96d1db0 6300 s.op->indent(1);
888af770
FCE
6301 for (unsigned i =0; i<probes.size(); i++)
6302 {
6303 uprobe_derived_probe* p = probes[i];
6304 s.op->newline() << "{";
89ba3085
FCE
6305 string key = make_pbm_key (p);
6306 unsigned value = module_index[key];
759e1d76
FCE
6307 if (value != 0)
6308 s.op->line() << " .tfi=" << value << ",";
6b66b9f7 6309 s.op->line() << " .address=(unsigned long)0x" << hex << p->addr << dec << "ULL,";
faea5e16 6310 s.op->line() << " .probe=" << common_probe_init (p) << ",";
4ddb6dd0 6311
038c38c6 6312 if (p->sdt_semaphore_addr != 0)
63b4fd14 6313 s.op->line() << " .sdt_sem_offset=(unsigned long)0x"
038c38c6 6314 << hex << p->sdt_semaphore_addr << dec << "ULL,";
4ddb6dd0
JS
6315
6316 if (p->has_return)
6317 s.op->line() << " .return_p=1,";
888af770
FCE
6318 s.op->line() << " },";
6319 }
6320 s.op->newline(-1) << "};";
a96d1db0 6321
89ba3085
FCE
6322 s.op->assert_0_indent();
6323
48e685da 6324 s.op->newline() << "static void enter_uprobe_probe (struct uprobe *inst, struct pt_regs *regs) {";
888af770 6325 s.op->newline(1) << "struct stap_uprobe *sup = container_of(inst, struct stap_uprobe, up);";
89ba3085 6326 s.op->newline() << "const struct stap_uprobe_spec *sups = &stap_uprobe_specs [sup->spec_index];";
faea5e16 6327 common_probe_entryfn_prologue (s.op, "STAP_SESSION_RUNNING", "sups->probe");
6d0f3f0c
FCE
6328 s.op->newline() << "if (sup->spec_index < 0 ||"
6329 << "sup->spec_index >= " << probes.size() << ") return;"; // XXX: should not happen
a96d1db0 6330 s.op->newline() << "c->regs = regs;";
fd9187da 6331 s.op->newline() << "c->ri = GET_PC_URETPROBE_NONE;";
97cd9334 6332 s.op->newline() << "c->regflags |= _STP_REGS_USER_FLAG;";
6415ddde
MW
6333
6334 // Make it look like the IP is set as it would in the actual user
6335 // task when calling real probe handler. Reset IP regs on return, so
6336 // we don't confuse uprobes. PR10458
6337 s.op->newline() << "{";
6338 s.op->indent(1);
6339 s.op->newline() << "unsigned long uprobes_ip = REG_IP(c->regs);";
259d54c0 6340 s.op->newline() << "SET_REG_IP(regs, inst->vaddr);";
26e63673 6341 s.op->newline() << "(*sups->probe->ph) (c);";
259d54c0 6342 s.op->newline() << "SET_REG_IP(regs, uprobes_ip);";
6415ddde
MW
6343 s.op->newline(-1) << "}";
6344
a96d1db0 6345 common_probe_entryfn_epilogue (s.op);
888af770 6346 s.op->newline(-1) << "}";
a96d1db0 6347
48e685da 6348 s.op->newline() << "static void enter_uretprobe_probe (struct uretprobe_instance *inst, struct pt_regs *regs) {";
888af770 6349 s.op->newline(1) << "struct stap_uprobe *sup = container_of(inst->rp, struct stap_uprobe, urp);";
89ba3085 6350 s.op->newline() << "const struct stap_uprobe_spec *sups = &stap_uprobe_specs [sup->spec_index];";
faea5e16 6351 common_probe_entryfn_prologue (s.op, "STAP_SESSION_RUNNING", "sups->probe");
5e562a69 6352 s.op->newline() << "c->ri = inst;";
6d0f3f0c
FCE
6353 s.op->newline() << "if (sup->spec_index < 0 ||"
6354 << "sup->spec_index >= " << probes.size() << ") return;"; // XXX: should not happen
888af770
FCE
6355 // XXX: kretprobes saves "c->pi = inst;" too
6356 s.op->newline() << "c->regs = regs;";
97cd9334 6357 s.op->newline() << "c->regflags |= _STP_REGS_USER_FLAG;";
6415ddde
MW
6358
6359 // Make it look like the IP is set as it would in the actual user
6360 // task when calling real probe handler. Reset IP regs on return, so
6361 // we don't confuse uprobes. PR10458
6362 s.op->newline() << "{";
6363 s.op->indent(1);
6364 s.op->newline() << "unsigned long uprobes_ip = REG_IP(c->regs);";
5e562a69 6365 s.op->newline() << "SET_REG_IP(regs, inst->ret_addr);";
26e63673 6366 s.op->newline() << "(*sups->probe->ph) (c);";
259d54c0 6367 s.op->newline() << "SET_REG_IP(regs, uprobes_ip);";
6415ddde
MW
6368 s.op->newline(-1) << "}";
6369
888af770 6370 common_probe_entryfn_epilogue (s.op);
a96d1db0
DN
6371 s.op->newline(-1) << "}";
6372
89ba3085 6373 s.op->newline();
cc52276b 6374 s.op->newline() << "#include \"uprobes-common.c\"";
6d0f3f0c 6375 s.op->newline();
888af770 6376}
935447c8
DS
6377
6378
888af770
FCE
6379void
6380uprobe_derived_probe_group::emit_module_init (systemtap_session& s)
935447c8 6381{
888af770 6382 if (probes.empty()) return;
935447c8 6383
5e112f92 6384 s.op->newline() << "/* ---- user probes ---- */";
935447c8 6385
01b05e2e 6386 s.op->newline() << "for (j=0; j<MAXUPROBES; j++) {";
5e112f92
FCE
6387 s.op->newline(1) << "struct stap_uprobe *sup = & stap_uprobes[j];";
6388 s.op->newline() << "sup->spec_index = -1;"; // free slot
80b4ad8b
FCE
6389 // NB: we assume the rest of the struct (specificaly, sup->up) is
6390 // initialized to zero. This is so that we can use
6391 // sup->up->kdata = NULL for "really free!" PR 6829.
5e112f92
FCE
6392 s.op->newline(-1) << "}";
6393 s.op->newline() << "mutex_init (& stap_uprobes_lock);";
935447c8 6394
89ba3085
FCE
6395 // Set up the task_finders
6396 s.op->newline() << "for (i=0; i<sizeof(stap_uprobe_finders)/sizeof(stap_uprobe_finders[0]); i++) {";
6397 s.op->newline(1) << "struct stap_uprobe_tf *stf = & stap_uprobe_finders[i];";
c57ea854 6398 s.op->newline() << "probe_point = stf->pathname;"; // for error messages; XXX: would prefer pp() or something better
89ba3085 6399 s.op->newline() << "rc = stap_register_task_finder_target (& stf->finder);";
935447c8 6400
5e112f92
FCE
6401 // NB: if (rc), there is no need (XXX: nor any way) to clean up any
6402 // finders already registered, since mere registration does not
6403 // cause any utrace or memory allocation actions. That happens only
6404 // later, once the task finder engine starts running. So, for a
6405 // partial initialization requiring unwind, we need do nothing.
6406 s.op->newline() << "if (rc) break;";
a7a68293 6407
888af770
FCE
6408 s.op->newline(-1) << "}";
6409}
d0ea46ce 6410
d0a7f5a9 6411
888af770
FCE
6412void
6413uprobe_derived_probe_group::emit_module_exit (systemtap_session& s)
6414{
6415 if (probes.empty()) return;
6416 s.op->newline() << "/* ---- user probes ---- */";
e56e51c9 6417
6d0f3f0c
FCE
6418 // NB: there is no stap_unregister_task_finder_target call;
6419 // important stuff like utrace cleanups are done by
d41d451c
FCE
6420 // __stp_task_finder_cleanup() via stap_stop_task_finder().
6421 //
6422 // This function blocks until all callbacks are completed, so there
6423 // is supposed to be no possibility of any registration-related code starting
6424 // to run in parallel with our shutdown here. So we don't need to protect the
6425 // stap_uprobes[] array with the mutex.
d0a7f5a9 6426
01b05e2e 6427 s.op->newline() << "for (j=0; j<MAXUPROBES; j++) {";
5e112f92 6428 s.op->newline(1) << "struct stap_uprobe *sup = & stap_uprobes[j];";
89ba3085 6429 s.op->newline() << "const struct stap_uprobe_spec *sups = &stap_uprobe_specs [sup->spec_index];";
6d0f3f0c 6430 s.op->newline() << "if (sup->spec_index < 0) continue;"; // free slot
3568f1dd 6431
8faa1fc5 6432 // PR10655: decrement that ENABLED semaphore
c116c31b 6433 s.op->newline() << "if (sup->sdt_sem_address) {";
8faa1fc5
FCE
6434 s.op->newline(1) << "unsigned short sdt_semaphore;"; // NB: fixed size
6435 s.op->newline() << "pid_t pid = (sups->return_p ? sup->urp.u.pid : sup->up.pid);";
6436 s.op->newline() << "struct task_struct *tsk;";
6437 s.op->newline() << "rcu_read_lock();";
6846cfc8 6438
86229a55
DS
6439 // Do a pid->task_struct* lookup. For 2.6.24+, this code assumes
6440 // that the pid is always in the global namespace, not in any
6441 // private namespace.
8faa1fc5 6442 s.op->newline() << "#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,24)";
86229a55
DS
6443 // We'd like to call find_task_by_pid_ns() here, but it isn't
6444 // exported. So, we call what it calls...
6445 s.op->newline() << " tsk = pid_task(find_pid_ns(pid, &init_pid_ns), PIDTYPE_PID);";
8faa1fc5
FCE
6446 s.op->newline() << "#else";
6447 s.op->newline() << " tsk = find_task_by_pid (pid);";
6448 s.op->newline() << "#endif /* 2.6.24 */";
8faa1fc5
FCE
6449
6450 s.op->newline() << "if (tsk) {"; // just in case the thing exited while we weren't watching
3c5b8e2b 6451 s.op->newline(1) << "if (__access_process_vm_noflush(tsk, sup->sdt_sem_address, &sdt_semaphore, sizeof(sdt_semaphore), 0)) {";
63b4fd14 6452 s.op->newline(1) << "sdt_semaphore --;";
903b9fcd 6453 s.op->newline() << "#ifdef DEBUG_UPROBES";
c116c31b 6454 s.op->newline() << "_stp_dbug (__FUNCTION__,__LINE__, \"-semaphore %#x @ %#lx\\n\", sdt_semaphore, sup->sdt_sem_address);";
903b9fcd 6455 s.op->newline() << "#endif";
3c5b8e2b 6456 s.op->newline() << "__access_process_vm_noflush(tsk, sup->sdt_sem_address, &sdt_semaphore, sizeof(sdt_semaphore), 1);";
93c84191 6457 s.op->newline(-1) << "}";
8faa1fc5
FCE
6458 // XXX: need to analyze possibility of race condition
6459 s.op->newline(-1) << "}";
6460 s.op->newline() << "rcu_read_unlock();";
6461 s.op->newline(-1) << "}";
6846cfc8 6462
3568f1dd
FCE
6463 s.op->newline() << "if (sups->return_p) {";
6464 s.op->newline(1) << "#ifdef DEBUG_UPROBES";
89ba3085 6465 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 6466 s.op->newline() << "#endif";
80b4ad8b
FCE
6467 // NB: PR6829 does not change that we still need to unregister at
6468 // *this* time -- when the script as a whole exits.
3568f1dd
FCE
6469 s.op->newline() << "unregister_uretprobe (& sup->urp);";
6470 s.op->newline(-1) << "} else {";
6471 s.op->newline(1) << "#ifdef DEBUG_UPROBES";
8faa1fc5 6472 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
6473 s.op->newline() << "#endif";
6474 s.op->newline() << "unregister_uprobe (& sup->up);";
6475 s.op->newline(-1) << "}";
935447c8 6476
6d0f3f0c 6477 s.op->newline() << "sup->spec_index = -1;";
935447c8 6478
3568f1dd
FCE
6479 // XXX: uprobe missed counts?
6480
6d0f3f0c 6481 s.op->newline(-1) << "}";
935447c8 6482
5e112f92 6483 s.op->newline() << "mutex_destroy (& stap_uprobes_lock);";
935447c8
DS
6484}
6485
e6fe60e7
AM
6486// ------------------------------------------------------------------------
6487// Kprobe derived probes
6488// ------------------------------------------------------------------------
6489
4627ed58 6490static const string TOK_KPROBE("kprobe");
935447c8 6491
bae55db9 6492struct kprobe_derived_probe: public derived_probe
d0ea46ce 6493{
bae55db9
JS
6494 kprobe_derived_probe (probe *base,
6495 probe_point *location,
6496 const string& name,
6497 int64_t stmt_addr,
6498 bool has_return,
6499 bool has_statement,
6500 bool has_maxactive,
b642c901
SC
6501 bool has_path,
6502 bool has_library,
6503 long maxactive_val,
6504 const string& path,
6505 const string& library
bae55db9
JS
6506 );
6507 string symbol_name;
6508 Dwarf_Addr addr;
6509 bool has_return;
6510 bool has_statement;
6511 bool has_maxactive;
b642c901
SC
6512 bool has_path;
6513 bool has_library;
bae55db9 6514 long maxactive_val;
b642c901
SC
6515 string path;
6516 string library;
bae55db9
JS
6517 bool access_var;
6518 void printsig (std::ostream &o) const;
6519 void join_group (systemtap_session& s);
6520};
d0ea46ce 6521
bae55db9
JS
6522struct kprobe_derived_probe_group: public derived_probe_group
6523{
6524private:
6525 multimap<string,kprobe_derived_probe*> probes_by_module;
6526 typedef multimap<string,kprobe_derived_probe*>::iterator p_b_m_iterator;
d0ea46ce 6527
bae55db9
JS
6528public:
6529 void enroll (kprobe_derived_probe* probe);
6530 void emit_module_decls (systemtap_session& s);
6531 void emit_module_init (systemtap_session& s);
6532 void emit_module_exit (systemtap_session& s);
6533};
d0ea46ce 6534
e6fe60e7
AM
6535kprobe_derived_probe::kprobe_derived_probe (probe *base,
6536 probe_point *location,
b6371390 6537 const string& name,
e6fe60e7 6538 int64_t stmt_addr,
b6371390
JS
6539 bool has_return,
6540 bool has_statement,
6541 bool has_maxactive,
b642c901
SC
6542 bool has_path,
6543 bool has_library,
6544 long maxactive_val,
6545 const string& path,
6546 const string& library
b6371390 6547 ):
4c5d1300 6548 derived_probe (base, location, true /* .components soon rewritten */ ),
e6fe60e7 6549 symbol_name (name), addr (stmt_addr),
b6371390 6550 has_return (has_return), has_statement (has_statement),
b642c901
SC
6551 has_maxactive (has_maxactive), has_path (has_path),
6552 has_library (has_library),
6553 maxactive_val (maxactive_val),
6554 path (path), library (library)
e6fe60e7
AM
6555{
6556 this->tok = base->tok;
6557 this->access_var = false;
d0ea46ce 6558
e6fe60e7
AM
6559#ifndef USHRT_MAX
6560#define USHRT_MAX 32767
6561#endif
d0ea46ce 6562
46856d8d
JS
6563 // Expansion of $target variables in the probe body produces an error during
6564 // translate phase, since we're not using debuginfo
d0ea46ce 6565
e6fe60e7 6566 vector<probe_point::component*> comps;
46856d8d 6567 comps.push_back (new probe_point::component(TOK_KPROBE));
e6fe60e7 6568
46856d8d
JS
6569 if (has_statement)
6570 {
9ea68eb9
JS
6571 comps.push_back (new probe_point::component(TOK_STATEMENT,
6572 new literal_number(addr, true)));
46856d8d
JS
6573 comps.push_back (new probe_point::component(TOK_ABSOLUTE));
6574 }
6575 else
6576 {
6577 size_t pos = name.find(':');
6578 if (pos != string::npos)
d0ea46ce 6579 {
46856d8d
JS
6580 string module = name.substr(0, pos);
6581 string function = name.substr(pos + 1);
6582 comps.push_back (new probe_point::component(TOK_MODULE, new literal_string(module)));
6583 comps.push_back (new probe_point::component(TOK_FUNCTION, new literal_string(function)));
6584 }
6585 else
6586 comps.push_back (new probe_point::component(TOK_FUNCTION, new literal_string(name)));
46856d8d 6587 }
d0ea46ce 6588
b6371390
JS
6589 if (has_return)
6590 comps.push_back (new probe_point::component(TOK_RETURN));
6591 if (has_maxactive)
6592 comps.push_back (new probe_point::component(TOK_MAXACTIVE, new literal_number(maxactive_val)));
d0ea46ce 6593
e6fe60e7
AM
6594 this->sole_location()->components = comps;
6595}
d0ea46ce 6596
e6fe60e7
AM
6597void kprobe_derived_probe::printsig (ostream& o) const
6598{
6599 sole_location()->print (o);
6600 o << " /* " << " name = " << symbol_name << "*/";
6601 printsig_nested (o);
6602}
d0ea46ce 6603
e6fe60e7
AM
6604void kprobe_derived_probe::join_group (systemtap_session& s)
6605{
d0ea46ce 6606
e6fe60e7
AM
6607 if (! s.kprobe_derived_probes)
6608 s.kprobe_derived_probes = new kprobe_derived_probe_group ();
6609 s.kprobe_derived_probes->enroll (this);
d0ea46ce 6610
e6fe60e7 6611}
d0ea46ce 6612
e6fe60e7
AM
6613void kprobe_derived_probe_group::enroll (kprobe_derived_probe* p)
6614{
6615 probes_by_module.insert (make_pair (p->symbol_name, p));
6616 // probes of same symbol should share single kprobe/kretprobe
6617}
d0ea46ce 6618
e6fe60e7
AM
6619void
6620kprobe_derived_probe_group::emit_module_decls (systemtap_session& s)
6621{
6622 if (probes_by_module.empty()) return;
d0ea46ce 6623
e6fe60e7 6624 s.op->newline() << "/* ---- kprobe-based probes ---- */";
d0ea46ce 6625
e6fe60e7
AM
6626 // Warn of misconfigured kernels
6627 s.op->newline() << "#if ! defined(CONFIG_KPROBES)";
6628 s.op->newline() << "#error \"Need CONFIG_KPROBES!\"";
6629 s.op->newline() << "#endif";
6630 s.op->newline();
d0ea46ce 6631
f07c3b68 6632 s.op->newline() << "#ifndef KRETACTIVE";
1ee6b5fc 6633 s.op->newline() << "#define KRETACTIVE (max(15,6*(int)num_possible_cpus()))";
f07c3b68
FCE
6634 s.op->newline() << "#endif";
6635
e6fe60e7 6636 // Forward declare the master entry functions
88747011 6637 s.op->newline() << "static int enter_kprobe2_probe (struct kprobe *inst,";
e6fe60e7 6638 s.op->line() << " struct pt_regs *regs);";
88747011 6639 s.op->newline() << "static int enter_kretprobe2_probe (struct kretprobe_instance *inst,";
e6fe60e7 6640 s.op->line() << " struct pt_regs *regs);";
d0ea46ce 6641
e6fe60e7
AM
6642 // Emit an array of kprobe/kretprobe pointers
6643 s.op->newline() << "#if defined(STAPCONF_UNREGISTER_KPROBES)";
c9116e99 6644 s.op->newline() << "static void * stap_unreg_kprobes2[" << probes_by_module.size() << "];";
e6fe60e7 6645 s.op->newline() << "#endif";
d0ea46ce 6646
e6fe60e7 6647 // Emit the actual probe list.
d0ea46ce 6648
e6fe60e7
AM
6649 s.op->newline() << "static struct stap_dwarfless_kprobe {";
6650 s.op->newline(1) << "union { struct kprobe kp; struct kretprobe krp; } u;";
6651 s.op->newline() << "#ifdef __ia64__";
6652 s.op->newline() << "struct kprobe dummy;";
6653 s.op->newline() << "#endif";
6654 s.op->newline(-1) << "} stap_dwarfless_kprobes[" << probes_by_module.size() << "];";
6655 // NB: bss!
d0ea46ce 6656
e6fe60e7
AM
6657 s.op->newline() << "static struct stap_dwarfless_probe {";
6658 s.op->newline(1) << "const unsigned return_p:1;";
6659 s.op->newline() << "const unsigned maxactive_p:1;";
b350f56b 6660 s.op->newline() << "const unsigned optional_p:1;";
e6fe60e7
AM
6661 s.op->newline() << "unsigned registered_p:1;";
6662 s.op->newline() << "const unsigned short maxactive_val;";
935447c8 6663
e6fe60e7
AM
6664 // Function Names are mostly small and uniform enough to justify putting
6665 // char[MAX]'s into the array instead of relocated char*'s.
935447c8 6666
faea5e16
JS
6667 size_t symbol_string_name_max = 0;
6668 size_t symbol_string_name_tot = 0;
e6fe60e7 6669 for (p_b_m_iterator it = probes_by_module.begin(); it != probes_by_module.end(); it++)
6270adc1 6670 {
e6fe60e7
AM
6671 kprobe_derived_probe* p = it->second;
6672#define DOIT(var,expr) do { \
6673 size_t var##_size = (expr) + 1; \
6674 var##_max = max (var##_max, var##_size); \
6675 var##_tot += var##_size; } while (0)
e6fe60e7
AM
6676 DOIT(symbol_string_name, p->symbol_name.size());
6677#undef DOIT
6270adc1
MH
6678 }
6679
e6fe60e7
AM
6680#define CALCIT(var) \
6681 s.op->newline() << "const char " << #var << "[" << var##_name_max << "] ;";
935447c8 6682
e6fe60e7
AM
6683 CALCIT(symbol_string);
6684#undef CALCIT
6270adc1 6685
bd659351 6686 s.op->newline() << "unsigned long address;";
26e63673 6687 s.op->newline() << "struct stap_probe * const probe;";
e6fe60e7
AM
6688 s.op->newline(-1) << "} stap_dwarfless_probes[] = {";
6689 s.op->indent(1);
6270adc1 6690
e6fe60e7
AM
6691 for (p_b_m_iterator it = probes_by_module.begin(); it != probes_by_module.end(); it++)
6692 {
6693 kprobe_derived_probe* p = it->second;
6694 s.op->newline() << "{";
6695 if (p->has_return)
6696 s.op->line() << " .return_p=1,";
6270adc1 6697
e6fe60e7
AM
6698 if (p->has_maxactive)
6699 {
6700 s.op->line() << " .maxactive_p=1,";
6701 assert (p->maxactive_val >= 0 && p->maxactive_val <= USHRT_MAX);
6702 s.op->line() << " .maxactive_val=" << p->maxactive_val << ",";
6703 }
6270adc1 6704
b350f56b
JS
6705 if (p->locations[0]->optional)
6706 s.op->line() << " .optional_p=1,";
6707
e6fe60e7 6708 if (p->has_statement)
c8d9d15e 6709 s.op->line() << " .address=(unsigned long)0x" << hex << p->addr << dec << "ULL,";
e6fe60e7 6710 else
c8d9d15e 6711 s.op->line() << " .symbol_string=\"" << p->symbol_name << "\",";
5d67b47c 6712
faea5e16 6713 s.op->line() << " .probe=" << common_probe_init (p) << ",";
e6fe60e7 6714 s.op->line() << " },";
935447c8
DS
6715 }
6716
e6fe60e7 6717 s.op->newline(-1) << "};";
5d67b47c 6718
e6fe60e7
AM
6719 // Emit the kprobes callback function
6720 s.op->newline();
88747011 6721 s.op->newline() << "static int enter_kprobe2_probe (struct kprobe *inst,";
e6fe60e7
AM
6722 s.op->line() << " struct pt_regs *regs) {";
6723 // NB: as of PR5673, the kprobe|kretprobe union struct is in BSS
6724 s.op->newline(1) << "int kprobe_idx = ((uintptr_t)inst-(uintptr_t)stap_dwarfless_kprobes)/sizeof(struct stap_dwarfless_kprobe);";
6725 // Check that the index is plausible
6726 s.op->newline() << "struct stap_dwarfless_probe *sdp = &stap_dwarfless_probes[";
6727 s.op->line() << "((kprobe_idx >= 0 && kprobe_idx < " << probes_by_module.size() << ")?";
6728 s.op->line() << "kprobe_idx:0)"; // NB: at least we avoid memory corruption
6729 // XXX: it would be nice to give a more verbose error though; BUG_ON later?
6730 s.op->line() << "];";
faea5e16 6731 common_probe_entryfn_prologue (s.op, "STAP_SESSION_RUNNING", "sdp->probe");
e6fe60e7 6732 s.op->newline() << "c->regs = regs;";
6415ddde
MW
6733
6734 // Make it look like the IP is set as it wouldn't have been replaced
6735 // by a breakpoint instruction when calling real probe handler. Reset
6736 // IP regs on return, so we don't confuse kprobes. PR10458
6737 s.op->newline() << "{";
6738 s.op->indent(1);
6739 s.op->newline() << "unsigned long kprobes_ip = REG_IP(c->regs);";
259d54c0 6740 s.op->newline() << "SET_REG_IP(regs, (unsigned long) inst->addr);";
26e63673 6741 s.op->newline() << "(*sdp->probe->ph) (c);";
259d54c0 6742 s.op->newline() << "SET_REG_IP(regs, kprobes_ip);";
6415ddde
MW
6743 s.op->newline(-1) << "}";
6744
e6fe60e7
AM
6745 common_probe_entryfn_epilogue (s.op);
6746 s.op->newline() << "return 0;";
6747 s.op->newline(-1) << "}";
935447c8 6748
e6fe60e7
AM
6749 // Same for kretprobes
6750 s.op->newline();
88747011 6751 s.op->newline() << "static int enter_kretprobe2_probe (struct kretprobe_instance *inst,";
e6fe60e7
AM
6752 s.op->line() << " struct pt_regs *regs) {";
6753 s.op->newline(1) << "struct kretprobe *krp = inst->rp;";
935447c8 6754
e6fe60e7
AM
6755 // NB: as of PR5673, the kprobe|kretprobe union struct is in BSS
6756 s.op->newline() << "int kprobe_idx = ((uintptr_t)krp-(uintptr_t)stap_dwarfless_kprobes)/sizeof(struct stap_dwarfless_kprobe);";
6757 // Check that the index is plausible
6758 s.op->newline() << "struct stap_dwarfless_probe *sdp = &stap_dwarfless_probes[";
6759 s.op->line() << "((kprobe_idx >= 0 && kprobe_idx < " << probes_by_module.size() << ")?";
6760 s.op->line() << "kprobe_idx:0)"; // NB: at least we avoid memory corruption
6761 // XXX: it would be nice to give a more verbose error though; BUG_ON later?
6762 s.op->line() << "];";
935447c8 6763
faea5e16 6764 common_probe_entryfn_prologue (s.op, "STAP_SESSION_RUNNING", "sdp->probe");
e6fe60e7
AM
6765 s.op->newline() << "c->regs = regs;";
6766 s.op->newline() << "c->pi = inst;"; // for assisting runtime's backtrace logic
6415ddde
MW
6767
6768 // Make it look like the IP is set as it wouldn't have been replaced
6769 // by a breakpoint instruction when calling real probe handler. Reset
6770 // IP regs on return, so we don't confuse kprobes. PR10458
6771 s.op->newline() << "{";
6772 s.op->indent(1);
6773 s.op->newline() << "unsigned long kprobes_ip = REG_IP(c->regs);";
259d54c0 6774 s.op->newline() << "SET_REG_IP(regs, (unsigned long) inst->rp->kp.addr);";
26e63673 6775 s.op->newline() << "(*sdp->probe->ph) (c);";
259d54c0 6776 s.op->newline() << "SET_REG_IP(regs, kprobes_ip);";
6415ddde
MW
6777 s.op->newline(-1) << "}";
6778
e6fe60e7
AM
6779 common_probe_entryfn_epilogue (s.op);
6780 s.op->newline() << "return 0;";
6781 s.op->newline(-1) << "}";
bd659351 6782
03a4ec63 6783 s.op->newline() << "#ifdef STAPCONF_KALLSYMS_ON_EACH_SYMBOL";
bd659351
MW
6784 s.op->newline() << "static int kprobe_resolve(void *data, const char *name,";
6785 s.op->newline() << " struct module *owner,";
6786 s.op->newline() << " unsigned long val) {";
6787 s.op->newline(1) << "int i;";
fc1d2aa2
MW
6788 s.op->newline() << "int *p = (int *) data;";
6789 s.op->newline() << "for (i=0; i<" << probes_by_module.size()
6790 << " && *p > 0; i++) {";
bd659351
MW
6791 s.op->newline(1) << "struct stap_dwarfless_probe *sdp = & stap_dwarfless_probes[i];";
6792 s.op->newline() << "if (! sdp->address)";
fc1d2aa2 6793 s.op->newline(1) << "if (strcmp(sdp->symbol_string, name) == 0) {";
bd659351 6794 s.op->newline(1) << "sdp->address = val;";
fc1d2aa2
MW
6795 s.op->newline() << "(*p)--;";
6796 s.op->newline(-1) << "}";
6797 s.op->newline(-2) << "}";
6798 s.op->newline() << "return (p > 0) ? 0 : -1;";
bd659351 6799 s.op->newline(-1) << "}";
03a4ec63 6800 s.op->newline() << "#endif";
935447c8
DS
6801}
6802
e6fe60e7 6803
6270adc1 6804void
e6fe60e7 6805kprobe_derived_probe_group::emit_module_init (systemtap_session& s)
6270adc1 6806{
03a4ec63 6807 s.op->newline() << "#ifdef STAPCONF_KALLSYMS_ON_EACH_SYMBOL";
fc1d2aa2
MW
6808 s.op->newline() << "{";
6809 s.op->newline(1) << "int p = 0;";
6810 s.op->newline() << "for (i = 0; i < " << probes_by_module.size() << "; i++) {";
6811 s.op->newline(1) << "struct stap_dwarfless_probe *sdp = & stap_dwarfless_probes[i];";
6812 s.op->newline() << "if (! sdp->address)";
6813 s.op->newline(1) << "p++;";
6814 s.op->newline(-2) << "}";
6815 s.op->newline() << "kallsyms_on_each_symbol(kprobe_resolve, &p);";
6816 s.op->newline(-1) << "}";
03a4ec63 6817 s.op->newline() << "#endif";
bd659351 6818
e6fe60e7 6819 s.op->newline() << "for (i=0; i<" << probes_by_module.size() << "; i++) {";
c8d9d15e 6820 s.op->newline(1) << "struct stap_dwarfless_probe *sdp = & stap_dwarfless_probes[i];";
e6fe60e7 6821 s.op->newline() << "struct stap_dwarfless_kprobe *kp = & stap_dwarfless_kprobes[i];";
c8d9d15e 6822 s.op->newline() << "void *addr = (void *) sdp->address;";
03a4ec63
MW
6823 s.op->newline() << "const char *symbol_name = addr ? NULL : sdp->symbol_string;";
6824
6825 s.op->newline() << "#ifdef STAPCONF_KALLSYMS_ON_EACH_SYMBOL";
bd659351
MW
6826 s.op->newline() << "if (! addr) {";
6827 s.op->newline(1) << "sdp->registered_p = 0;";
9319b767
MW
6828 s.op->newline() << "if (!sdp->optional_p)";
6829 s.op->newline(1) << "_stp_warn (\"probe %s registration error (symbol not found)\", probe_point);";
6830 s.op->newline(-1) << "continue;";
bd659351 6831 s.op->newline(-1) << "}";
03a4ec63
MW
6832 s.op->newline() << "#endif";
6833
26e63673 6834 s.op->newline() << "probe_point = sdp->probe->pp;"; // for error messages
e6fe60e7 6835 s.op->newline() << "if (sdp->return_p) {";
c8d9d15e 6836 s.op->newline(1) << "kp->u.krp.kp.addr = addr;";
03a4ec63 6837 s.op->newline() << "kp->u.krp.kp.symbol_name = (char *) symbol_name;";
e6fe60e7
AM
6838 s.op->newline() << "if (sdp->maxactive_p) {";
6839 s.op->newline(1) << "kp->u.krp.maxactive = sdp->maxactive_val;";
6840 s.op->newline(-1) << "} else {";
f07c3b68 6841 s.op->newline(1) << "kp->u.krp.maxactive = KRETACTIVE;";
e6fe60e7 6842 s.op->newline(-1) << "}";
88747011 6843 s.op->newline() << "kp->u.krp.handler = &enter_kretprobe2_probe;";
e6fe60e7
AM
6844 // to ensure safeness of bspcache, always use aggr_kprobe on ia64
6845 s.op->newline() << "#ifdef __ia64__";
e6fe60e7 6846 s.op->newline() << "kp->dummy.addr = kp->u.krp.kp.addr;";
c8d9d15e
JS
6847 s.op->newline() << "kp->dummy.symbol_name = kp->u.krp.kp.symbol_name;";
6848 s.op->newline() << "kp->dummy.pre_handler = NULL;";
e6fe60e7
AM
6849 s.op->newline() << "rc = register_kprobe (& kp->dummy);";
6850 s.op->newline() << "if (rc == 0) {";
6851 s.op->newline(1) << "rc = register_kretprobe (& kp->u.krp);";
6852 s.op->newline() << "if (rc != 0)";
6853 s.op->newline(1) << "unregister_kprobe (& kp->dummy);";
6854 s.op->newline(-2) << "}";
6855 s.op->newline() << "#else";
6856 s.op->newline() << "rc = register_kretprobe (& kp->u.krp);";
6857 s.op->newline() << "#endif";
6858 s.op->newline(-1) << "} else {";
6859 // to ensure safeness of bspcache, always use aggr_kprobe on ia64
c8d9d15e 6860 s.op->newline(1) << "kp->u.kp.addr = addr;";
03a4ec63 6861 s.op->newline() << "kp->u.kp.symbol_name = (char *) symbol_name;";
88747011 6862 s.op->newline() << "kp->u.kp.pre_handler = &enter_kprobe2_probe;";
e6fe60e7 6863 s.op->newline() << "#ifdef __ia64__";
e6fe60e7 6864 s.op->newline() << "kp->dummy.pre_handler = NULL;";
c8d9d15e
JS
6865 s.op->newline() << "kp->dummy.addr = kp->u.kp.addr;";
6866 s.op->newline() << "kp->dummy.symbol_name = kp->u.kp.symbol_name;";
e6fe60e7
AM
6867 s.op->newline() << "rc = register_kprobe (& kp->dummy);";
6868 s.op->newline() << "if (rc == 0) {";
6869 s.op->newline(1) << "rc = register_kprobe (& kp->u.kp);";
6870 s.op->newline() << "if (rc != 0)";
6871 s.op->newline(1) << "unregister_kprobe (& kp->dummy);";
6872 s.op->newline(-2) << "}";
6873 s.op->newline() << "#else";
6874 s.op->newline() << "rc = register_kprobe (& kp->u.kp);";
6875 s.op->newline() << "#endif";
6876 s.op->newline(-1) << "}";
6877 s.op->newline() << "if (rc) {"; // PR6749: tolerate a failed register_*probe.
6878 s.op->newline(1) << "sdp->registered_p = 0;";
b350f56b 6879 s.op->newline() << "if (!sdp->optional_p)";
50b6acf7 6880 s.op->newline(1) << "_stp_warn (\"probe %s (address 0x%lx) registration error (rc %d)\", probe_point, (unsigned long) addr, rc);";
b350f56b 6881 s.op->newline(-1) << "rc = 0;"; // continue with other probes
e6fe60e7
AM
6882 // XXX: shall we increment numskipped?
6883 s.op->newline(-1) << "}";
6270adc1 6884
e6fe60e7
AM
6885 s.op->newline() << "else sdp->registered_p = 1;";
6886 s.op->newline(-1) << "}"; // for loop
6270adc1
MH
6887}
6888
e6fe60e7
AM
6889void
6890kprobe_derived_probe_group::emit_module_exit (systemtap_session& s)
935447c8 6891{
e6fe60e7
AM
6892 //Unregister kprobes by batch interfaces.
6893 s.op->newline() << "#if defined(STAPCONF_UNREGISTER_KPROBES)";
6894 s.op->newline() << "j = 0;";
6895 s.op->newline() << "for (i=0; i<" << probes_by_module.size() << "; i++) {";
6896 s.op->newline(1) << "struct stap_dwarfless_probe *sdp = & stap_dwarfless_probes[i];";
6897 s.op->newline() << "struct stap_dwarfless_kprobe *kp = & stap_dwarfless_kprobes[i];";
6898 s.op->newline() << "if (! sdp->registered_p) continue;";
6899 s.op->newline() << "if (!sdp->return_p)";
c9116e99 6900 s.op->newline(1) << "stap_unreg_kprobes2[j++] = &kp->u.kp;";
e6fe60e7 6901 s.op->newline(-2) << "}";
c9116e99 6902 s.op->newline() << "unregister_kprobes((struct kprobe **)stap_unreg_kprobes2, j);";
e6fe60e7
AM
6903 s.op->newline() << "j = 0;";
6904 s.op->newline() << "for (i=0; i<" << probes_by_module.size() << "; i++) {";
6905 s.op->newline(1) << "struct stap_dwarfless_probe *sdp = & stap_dwarfless_probes[i];";
6906 s.op->newline() << "struct stap_dwarfless_kprobe *kp = & stap_dwarfless_kprobes[i];";
6907 s.op->newline() << "if (! sdp->registered_p) continue;";
6908 s.op->newline() << "if (sdp->return_p)";
c9116e99 6909 s.op->newline(1) << "stap_unreg_kprobes2[j++] = &kp->u.krp;";
e6fe60e7 6910 s.op->newline(-2) << "}";
c9116e99 6911 s.op->newline() << "unregister_kretprobes((struct kretprobe **)stap_unreg_kprobes2, j);";
e6fe60e7
AM
6912 s.op->newline() << "#ifdef __ia64__";
6913 s.op->newline() << "j = 0;";
6914 s.op->newline() << "for (i=0; i<" << probes_by_module.size() << "; i++) {";
6915 s.op->newline(1) << "struct stap_dwarfless_probe *sdp = & stap_dwarfless_probes[i];";
6916 s.op->newline() << "struct stap_dwarfless_kprobe *kp = & stap_dwarfless_kprobes[i];";
6917 s.op->newline() << "if (! sdp->registered_p) continue;";
c9116e99 6918 s.op->newline() << "stap_unreg_kprobes2[j++] = &kp->dummy;";
e6fe60e7 6919 s.op->newline(-1) << "}";
c9116e99 6920 s.op->newline() << "unregister_kprobes((struct kprobe **)stap_unreg_kprobes2, j);";
e6fe60e7
AM
6921 s.op->newline() << "#endif";
6922 s.op->newline() << "#endif";
3e3bd7b6 6923
e6fe60e7
AM
6924 s.op->newline() << "for (i=0; i<" << probes_by_module.size() << "; i++) {";
6925 s.op->newline(1) << "struct stap_dwarfless_probe *sdp = & stap_dwarfless_probes[i];";
6926 s.op->newline() << "struct stap_dwarfless_kprobe *kp = & stap_dwarfless_kprobes[i];";
6927 s.op->newline() << "if (! sdp->registered_p) continue;";
6928 s.op->newline() << "if (sdp->return_p) {";
6929 s.op->newline() << "#if !defined(STAPCONF_UNREGISTER_KPROBES)";
6930 s.op->newline(1) << "unregister_kretprobe (&kp->u.krp);";
6931 s.op->newline() << "#endif";
6932 s.op->newline() << "atomic_add (kp->u.krp.nmissed, & skipped_count);";
6933 s.op->newline() << "#ifdef STP_TIMING";
6934 s.op->newline() << "if (kp->u.krp.nmissed)";
26e63673 6935 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
6936 s.op->newline(-1) << "#endif";
6937 s.op->newline() << "atomic_add (kp->u.krp.kp.nmissed, & skipped_count);";
6938 s.op->newline() << "#ifdef STP_TIMING";
6939 s.op->newline() << "if (kp->u.krp.kp.nmissed)";
26e63673 6940 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
6941 s.op->newline(-1) << "#endif";
6942 s.op->newline(-1) << "} else {";
6943 s.op->newline() << "#if !defined(STAPCONF_UNREGISTER_KPROBES)";
6944 s.op->newline(1) << "unregister_kprobe (&kp->u.kp);";
6945 s.op->newline() << "#endif";
6946 s.op->newline() << "atomic_add (kp->u.kp.nmissed, & skipped_count);";
6947 s.op->newline() << "#ifdef STP_TIMING";
6948 s.op->newline() << "if (kp->u.kp.nmissed)";
26e63673 6949 s.op->newline(1) << "_stp_warn (\"Skipped due to missed kprobe on '%s': %lu\\n\", sdp->probe->pp, kp->u.kp.nmissed);";
e6fe60e7
AM
6950 s.op->newline(-1) << "#endif";
6951 s.op->newline(-1) << "}";
6952 s.op->newline() << "#if !defined(STAPCONF_UNREGISTER_KPROBES) && defined(__ia64__)";
6953 s.op->newline() << "unregister_kprobe (&kp->dummy);";
6954 s.op->newline() << "#endif";
6955 s.op->newline() << "sdp->registered_p = 0;";
6956 s.op->newline(-1) << "}";
f8a968bc
JS
6957}
6958
e6fe60e7 6959struct kprobe_builder: public derived_probe_builder
3c1b3d06 6960{
e6fe60e7
AM
6961 kprobe_builder() {}
6962 virtual void build(systemtap_session & sess,
6963 probe * base,
6964 probe_point * location,
6965 literal_map_t const & parameters,
6966 vector<derived_probe *> & finished_results);
6967};
3c1b3d06
FCE
6968
6969
79189b84 6970void
e6fe60e7
AM
6971kprobe_builder::build(systemtap_session & sess,
6972 probe * base,
6973 probe_point * location,
6974 literal_map_t const & parameters,
6975 vector<derived_probe *> & finished_results)
79189b84 6976{
e6fe60e7 6977 string function_string_val, module_string_val;
b642c901 6978 string path, library;
b6371390
JS
6979 int64_t statement_num_val = 0, maxactive_val = 0;
6980 bool has_function_str, has_module_str, has_statement_num;
6981 bool has_absolute, has_return, has_maxactive;
b642c901 6982 bool has_path, has_library;
79189b84 6983
b6371390
JS
6984 has_function_str = get_param(parameters, TOK_FUNCTION, function_string_val);
6985 has_module_str = get_param(parameters, TOK_MODULE, module_string_val);
6986 has_return = has_null_param (parameters, TOK_RETURN);
6987 has_maxactive = get_param(parameters, TOK_MAXACTIVE, maxactive_val);
6988 has_statement_num = get_param(parameters, TOK_STATEMENT, statement_num_val);
6989 has_absolute = has_null_param (parameters, TOK_ABSOLUTE);
b642c901
SC
6990 has_path = get_param (parameters, TOK_PROCESS, path);
6991 has_library = get_param (parameters, TOK_LIBRARY, library);
6992
6993 if (has_path)
6994 path = find_executable (path);
6995 if (has_library)
6996 library = find_executable (library, "LD_LIBRARY_PATH");
c57ea854 6997
b6371390 6998 if (has_function_str)
6fb70fb7 6999 {
b6371390
JS
7000 if (has_module_str)
7001 function_string_val = module_string_val + ":" + function_string_val;
86758d5f 7002
b6371390
JS
7003 finished_results.push_back (new kprobe_derived_probe (base,
7004 location, function_string_val,
7005 0, has_return,
7006 has_statement_num,
7007 has_maxactive,
b642c901
SC
7008 has_path,
7009 has_library,
7010 maxactive_val,
7011 path,
7012 library));
6fb70fb7 7013 }
e6fe60e7 7014 else
b6371390
JS
7015 {
7016 // assert guru mode for absolute probes
7017 if ( has_statement_num && has_absolute && !base->privileged )
7018 throw semantic_error ("absolute statement probe in unprivileged script", base->tok);
7019
7020 finished_results.push_back (new kprobe_derived_probe (base,
7021 location, "",
7022 statement_num_val,
7023 has_return,
7024 has_statement_num,
7025 has_maxactive,
b642c901
SC
7026 has_path,
7027 has_library,
7028 maxactive_val,
7029 path,
7030 library));
96b030fe 7031 }
79189b84
JS
7032}
7033
dd225250
PS
7034// ------------------------------------------------------------------------
7035// Hardware breakpoint based probes.
7036// ------------------------------------------------------------------------
7037
7038static const string TOK_HWBKPT("data");
7039static const string TOK_HWBKPT_WRITE("write");
7040static const string TOK_HWBKPT_RW("rw");
7041static const string TOK_LENGTH("length");
7042
7043#define HWBKPT_READ 0
7044#define HWBKPT_WRITE 1
7045#define HWBKPT_RW 2
7046struct hwbkpt_derived_probe: public derived_probe
7047{
7048 hwbkpt_derived_probe (probe *base,
7049 probe_point *location,
7050 uint64_t addr,
7051 string symname,
7052 unsigned int len,
7053 bool has_only_read_access,
7054 bool has_only_write_access,
7055 bool has_rw_access
7056 );
7057 Dwarf_Addr hwbkpt_addr;
7058 string symbol_name;
7059 unsigned int hwbkpt_access,hwbkpt_len;
7060
7061 void printsig (std::ostream &o) const;
7062 void join_group (systemtap_session& s);
7063};
7064
7065struct hwbkpt_derived_probe_group: public derived_probe_group
7066{
dd225250 7067private:
dac77b80 7068 vector<hwbkpt_derived_probe*> hwbkpt_probes;
dd225250
PS
7069
7070public:
7071 void enroll (hwbkpt_derived_probe* probe, systemtap_session& s);
7072 void emit_module_decls (systemtap_session& s);
7073 void emit_module_init (systemtap_session& s);
7074 void emit_module_exit (systemtap_session& s);
7075};
7076
7077hwbkpt_derived_probe::hwbkpt_derived_probe (probe *base,
9ea68eb9
JS
7078 probe_point *location,
7079 uint64_t addr,
7080 string symname,
7081 unsigned int len,
7082 bool has_only_read_access,
7083 bool has_only_write_access,
7084 bool has_rw_access
7085 ):
4c5d1300 7086 derived_probe (base, location, true /* .components soon rewritten */ ),
dd225250
PS
7087 hwbkpt_addr (addr),
7088 symbol_name (symname),
7089 hwbkpt_len (len)
7090{
7091 this->tok = base->tok;
7092
7093 vector<probe_point::component*> comps;
7094 comps.push_back (new probe_point::component(TOK_KERNEL));
7095
7096 if (hwbkpt_addr)
9ea68eb9
JS
7097 comps.push_back (new probe_point::component (TOK_HWBKPT,
7098 new literal_number(hwbkpt_addr, true)));
7099 else if (symbol_name.size())
7100 comps.push_back (new probe_point::component (TOK_HWBKPT, new literal_string(symbol_name)));
dd225250
PS
7101
7102 comps.push_back (new probe_point::component (TOK_LENGTH, new literal_number(hwbkpt_len)));
7103
7104 if (has_only_read_access)
9ea68eb9 7105 this->hwbkpt_access = HWBKPT_READ ;
dd225250
PS
7106//TODO add code for comps.push_back for read, since this flag is not for x86
7107
7108 else
9ea68eb9
JS
7109 {
7110 if (has_only_write_access)
7111 {
7112 this->hwbkpt_access = HWBKPT_WRITE ;
7113 comps.push_back (new probe_point::component(TOK_HWBKPT_WRITE));
7114 }
7115 else
7116 {
7117 this->hwbkpt_access = HWBKPT_RW ;
7118 comps.push_back (new probe_point::component(TOK_HWBKPT_RW));
7119 }
7120 }
dd225250
PS
7121
7122 this->sole_location()->components = comps;
7123}
7124
7125void hwbkpt_derived_probe::printsig (ostream& o) const
7126{
7127 sole_location()->print (o);
7128 printsig_nested (o);
7129}
7130
7131void hwbkpt_derived_probe::join_group (systemtap_session& s)
7132{
dac77b80
FCE
7133 if (! s.hwbkpt_derived_probes)
7134 s.hwbkpt_derived_probes = new hwbkpt_derived_probe_group ();
dd225250
PS
7135 s.hwbkpt_derived_probes->enroll (this, s);
7136}
7137
7138void hwbkpt_derived_probe_group::enroll (hwbkpt_derived_probe* p, systemtap_session& s)
7139{
dac77b80
FCE
7140 hwbkpt_probes.push_back (p);
7141
7142 unsigned max_hwbkpt_probes_by_arch = 0;
7143 if (s.architecture == "i386" || s.architecture == "x86_64")
7144 max_hwbkpt_probes_by_arch = 4;
7145 else if (s.architecture == "s390")
7146 max_hwbkpt_probes_by_arch = 1;
7147
c57ea854 7148 if (hwbkpt_probes.size() >= max_hwbkpt_probes_by_arch)
dac77b80
FCE
7149 if (! s.suppress_warnings)
7150 s.print_warning ("Too many hardware breakpoint probes requested for " + s.architecture
7151 + "(" + lex_cast(hwbkpt_probes.size()) +
7152 " vs. " + lex_cast(max_hwbkpt_probes_by_arch) + ")");
dd225250
PS
7153}
7154
7155void
7156hwbkpt_derived_probe_group::emit_module_decls (systemtap_session& s)
7157{
dac77b80 7158 if (hwbkpt_probes.empty()) return;
dd225250
PS
7159
7160 s.op->newline() << "/* ---- hwbkpt-based probes ---- */";
7161
7162 s.op->newline() << "#include <linux/perf_event.h>";
7163 s.op->newline() << "#include <linux/hw_breakpoint.h>";
7164 s.op->newline();
7165
7166 // Forward declare the master entry functions
7167 s.op->newline() << "static int enter_hwbkpt_probe (struct perf_event *bp,";
7168 s.op->line() << " int nmi,";
7169 s.op->line() << " struct perf_sample_data *data,";
7170 s.op->line() << " struct pt_regs *regs);";
79189b84 7171
dd225250
PS
7172 // Emit the actual probe list.
7173
7174 s.op->newline() << "static struct perf_event_attr ";
dac77b80 7175 s.op->newline() << "stap_hwbkpt_probe_array[" << hwbkpt_probes.size() << "];";
dd225250
PS
7176
7177 s.op->newline() << "static struct perf_event **";
dac77b80 7178 s.op->newline() << "stap_hwbkpt_ret_array[" << hwbkpt_probes.size() << "];";
dd225250
PS
7179 s.op->newline() << "static struct stap_hwbkpt_probe {";
7180 s.op->newline() << "int registered_p:1;";
7181// registered_p = 0 signifies a probe that failed registration
7182// registered_p = 1 signifies a probe that got registered successfully
7183
faea5e16 7184 // Symbol Names are mostly small and uniform enough
dd225250 7185 // to justify putting const char*.
dac77b80 7186 s.op->newline() << "const char * const symbol;";
dd225250
PS
7187
7188 s.op->newline() << "const unsigned long address;";
7189 s.op->newline() << "uint8_t atype;";
bb0a4e12 7190 s.op->newline() << "unsigned int len;";
26e63673 7191 s.op->newline() << "struct stap_probe * const probe;";
dd225250
PS
7192 s.op->newline() << "} stap_hwbkpt_probes[] = {";
7193 s.op->indent(1);
7194
dac77b80 7195 for (unsigned int it = 0; it < hwbkpt_probes.size(); it++)
dd225250 7196 {
dac77b80 7197 hwbkpt_derived_probe* p = hwbkpt_probes.at(it);
dd225250
PS
7198 s.op->newline() << "{";
7199 s.op->line() << " .registered_p=1,";
7200 if (p->symbol_name.size())
7201 s.op->line() << " .address=(unsigned long)0x0" << "ULL,";
7202 else
7203 s.op->line() << " .address=(unsigned long)0x" << hex << p->hwbkpt_addr << dec << "ULL,";
7204 switch(p->hwbkpt_access){
7205 case HWBKPT_READ:
7206 s.op->line() << " .atype=HW_BREAKPOINT_R ,";
bb0a4e12 7207 break;
dd225250
PS
7208 case HWBKPT_WRITE:
7209 s.op->line() << " .atype=HW_BREAKPOINT_W ,";
bb0a4e12 7210 break;
dd225250
PS
7211 case HWBKPT_RW:
7212 s.op->line() << " .atype=HW_BREAKPOINT_R|HW_BREAKPOINT_W ,";
bb0a4e12 7213 break;
dd225250
PS
7214 };
7215 s.op->line() << " .len=" << p->hwbkpt_len << ",";
faea5e16 7216 s.op->line() << " .probe=" << common_probe_init (p) << ",";
dd225250 7217 s.op->line() << " .symbol=\"" << p->symbol_name << "\",";
dd225250
PS
7218 s.op->line() << " },";
7219 }
dac77b80 7220 s.op->newline(-1) << "};";
dd225250
PS
7221
7222 // Emit the hwbkpt callback function
7223 s.op->newline() ;
7224 s.op->newline() << "static int enter_hwbkpt_probe (struct perf_event *bp,";
7225 s.op->line() << " int nmi,";
7226 s.op->line() << " struct perf_sample_data *data,";
7227 s.op->line() << " struct pt_regs *regs) {";
dac77b80
FCE
7228 s.op->newline(1) << "unsigned int i;";
7229 s.op->newline() << "if (bp->attr.type != PERF_TYPE_BREAKPOINT) return -1;";
7230 s.op->newline() << "for (i=0; i<" << hwbkpt_probes.size() << "; i++) {";
7231 s.op->newline(1) << "struct perf_event_attr *hp = & stap_hwbkpt_probe_array[i];";
7232 // XXX: why not match stap_hwbkpt_ret_array[i] against bp instead?
7233 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) {";
7234 s.op->newline(1) << "struct stap_hwbkpt_probe *sdp = &stap_hwbkpt_probes[i];";
faea5e16 7235 common_probe_entryfn_prologue (s.op, "STAP_SESSION_RUNNING", "sdp->probe");
dd225250 7236 s.op->newline() << "c->regs = regs;";
26e63673 7237 s.op->newline() << "(*sdp->probe->ph) (c);";
dd225250 7238 common_probe_entryfn_epilogue (s.op);
dac77b80 7239 s.op->newline(-1) << "}";
dd225250
PS
7240 s.op->newline(-1) << "}";
7241 s.op->newline() << "return 0;";
dac77b80 7242 s.op->newline(-1) << "}";
dd225250
PS
7243}
7244
7245void
7246hwbkpt_derived_probe_group::emit_module_init (systemtap_session& s)
7247{
dac77b80 7248 s.op->newline() << "for (i=0; i<" << hwbkpt_probes.size() << "; i++) {";
dd225250
PS
7249 s.op->newline(1) << "struct stap_hwbkpt_probe *sdp = & stap_hwbkpt_probes[i];";
7250 s.op->newline() << "struct perf_event_attr *hp = & stap_hwbkpt_probe_array[i];";
7251 s.op->newline() << "void *addr = (void *) sdp->address;";
7252 s.op->newline() << "const char *hwbkpt_symbol_name = addr ? NULL : sdp->symbol;";
dac77b80
FCE
7253 s.op->newline() << "hw_breakpoint_init(hp);";
7254 s.op->newline() << "if (addr)";
7255 s.op->newline(1) << "hp->bp_addr = (unsigned long) addr;";
7256 s.op->newline(-1) << "else { ";
7257 s.op->newline(1) << "hp->bp_addr = kallsyms_lookup_name(hwbkpt_symbol_name);";
7258 s.op->newline() << "if (!hp->bp_addr) { ";
26e63673 7259 s.op->newline(1) << "_stp_warn(\"Probe %s registration skipped: invalid symbol %s \",sdp->probe->pp,hwbkpt_symbol_name);";
dac77b80
FCE
7260 s.op->newline() << "continue;";
7261 s.op->newline(-1) << "}";
7262 s.op->newline(-1) << "}";
7263 s.op->newline() << "hp->bp_type = sdp->atype;";
7264
7265 // On x86 & x86-64, hp->bp_len is not just a number but a macro/enum (!?!).
c57ea854 7266 if (s.architecture == "i386" || s.architecture == "x86_64" )
dac77b80
FCE
7267 {
7268 s.op->newline() << "switch(sdp->len) {";
7269 s.op->newline() << "case 1:";
7270 s.op->newline(1) << "hp->bp_len = HW_BREAKPOINT_LEN_1;";
7271 s.op->newline() << "break;";
7272 s.op->newline(-1) << "case 2:";
7273 s.op->newline(1) << "hp->bp_len = HW_BREAKPOINT_LEN_2;";
7274 s.op->newline() << "break;";
7275 s.op->newline(-1) << "case 3:";
7276 s.op->newline() << "case 4:";
7277 s.op->newline(1) << "hp->bp_len = HW_BREAKPOINT_LEN_4;";
7278 s.op->newline() << "break;";
7279 s.op->newline(-1) << "case 5:";
7280 s.op->newline() << "case 6:";
7281 s.op->newline() << "case 7:";
7282 s.op->newline() << "case 8:";
7283 s.op->newline() << "default:"; // XXX: could instead reject
7284 s.op->newline(1) << "hp->bp_len = HW_BREAKPOINT_LEN_8;";
7285 s.op->newline() << "break;";
7286 s.op->newline(-1) << "}";
7287 }
7288 else // other architectures presumed straightforward
7289 s.op->newline() << "hp->bp_len = sdp->len;";
7290
26e63673 7291 s.op->newline() << "probe_point = sdp->probe->pp;"; // for error messages
dac77b80
FCE
7292 s.op->newline() << "stap_hwbkpt_ret_array[i] = register_wide_hw_breakpoint(hp, (void *)&enter_hwbkpt_probe);";
7293 s.op->newline() << "if (IS_ERR(stap_hwbkpt_ret_array[i])) {";
7294 s.op->newline(1) << "int err_code = PTR_ERR(stap_hwbkpt_ret_array[i]);";
7295 s.op->newline(0) << "_stp_warn(\"Hwbkpt probe %s: registration error %d, addr %p, name %s\", probe_point, err_code, addr, hwbkpt_symbol_name);";
7296 s.op->newline(-1) << "}";
dd225250 7297 s.op->newline() << " else sdp->registered_p = 1;";
dd225250
PS
7298 s.op->newline(-1) << "}"; // for loop
7299}
7300
7301void
7302hwbkpt_derived_probe_group::emit_module_exit (systemtap_session& s)
7303{
7304 //Unregister hwbkpt probes.
dac77b80 7305 s.op->newline() << "for (i=0; i<" << hwbkpt_probes.size() << "; i++) {";
dd225250 7306 s.op->newline(1) << "struct stap_hwbkpt_probe *sdp = & stap_hwbkpt_probes[i];";
dac77b80
FCE
7307 s.op->newline() << "if (sdp->registered_p == 0) continue;";
7308 s.op->newline() << "unregister_wide_hw_breakpoint(stap_hwbkpt_ret_array[i]);";
dd225250
PS
7309 s.op->newline() << "sdp->registered_p = 0;";
7310 s.op->newline(-1) << "}";
7311}
7312
7313struct hwbkpt_builder: public derived_probe_builder
7314{
7315 hwbkpt_builder() {}
7316 virtual void build(systemtap_session & sess,
7317 probe * base,
7318 probe_point * location,
7319 literal_map_t const & parameters,
7320 vector<derived_probe *> & finished_results);
7321};
7322
7323void
7324hwbkpt_builder::build(systemtap_session & sess,
7325 probe * base,
7326 probe_point * location,
7327 literal_map_t const & parameters,
7328 vector<derived_probe *> & finished_results)
7329{
7330 string symbol_str_val;
7331 int64_t hwbkpt_address, len;
7332 bool has_addr, has_symbol_str, has_write, has_rw, has_len;
7333
b47f3a55
FCE
7334 if (! (sess.kernel_config["CONFIG_PERF_EVENTS"] == string("y")))
7335 throw semantic_error ("CONFIG_PERF_EVENTS not available on this kernel",
7336 location->components[0]->tok);
7337 if (! (sess.kernel_config["CONFIG_HAVE_HW_BREAKPOINT"] == string("y")))
7338 throw semantic_error ("CONFIG_HAVE_HW_BREAKPOINT not available on this kernel",
7339 location->components[0]->tok);
7340
dd225250
PS
7341 has_addr = get_param (parameters, TOK_HWBKPT, hwbkpt_address);
7342 has_symbol_str = get_param (parameters, TOK_HWBKPT, symbol_str_val);
7343 has_len = get_param (parameters, TOK_LENGTH, len);
7344 has_write = (parameters.find(TOK_HWBKPT_WRITE) != parameters.end());
7345 has_rw = (parameters.find(TOK_HWBKPT_RW) != parameters.end());
7346
7347 if (!has_len)
7348 len = 1;
7349
7350 if (has_addr)
7351 finished_results.push_back (new hwbkpt_derived_probe (base,
7352 location,
7353 hwbkpt_address,
7354 "",len,0,
7355 has_write,
7356 has_rw));
7357 else // has symbol_str
7358 finished_results.push_back (new hwbkpt_derived_probe (base,
7359 location,
7360 0,
7361 symbol_str_val,len,0,
7362 has_write,
7363 has_rw));
7364}
342d3f96 7365
0a6f5a3f
JS
7366// ------------------------------------------------------------------------
7367// statically inserted kernel-tracepoint derived probes
7368// ------------------------------------------------------------------------
7369
6fb70fb7 7370struct tracepoint_arg
79189b84 7371{
ad370dcc 7372 string name, c_type, typecast;
dcaa1a65 7373 bool usable, used, isptr;
f8a968bc 7374 Dwarf_Die type_die;
dcaa1a65 7375 tracepoint_arg(): usable(false), used(false), isptr(false) {}
6fb70fb7 7376};
79189b84 7377
0a6f5a3f
JS
7378struct tracepoint_derived_probe: public derived_probe
7379{
79189b84
JS
7380 tracepoint_derived_probe (systemtap_session& s,
7381 dwflpp& dw, Dwarf_Die& func_die,
7382 const string& tracepoint_name,
7383 probe* base_probe, probe_point* location);
bc9a523d 7384
79189b84 7385 systemtap_session& sess;
6fb70fb7
JS
7386 string tracepoint_name, header;
7387 vector <struct tracepoint_arg> args;
bc9a523d 7388
6fb70fb7 7389 void build_args(dwflpp& dw, Dwarf_Die& func_die);
d0bfd2ac 7390 void getargs (std::list<std::string> &arg_set) const;
79189b84 7391 void join_group (systemtap_session& s);
3e3bd7b6 7392 void print_dupe_stamp(ostream& o);
0a6f5a3f 7393};
79189b84
JS
7394
7395
0a6f5a3f 7396struct tracepoint_derived_probe_group: public generic_dpg<tracepoint_derived_probe>
79189b84 7397{
79189b84
JS
7398 void emit_module_decls (systemtap_session& s);
7399 void emit_module_init (systemtap_session& s);
7400 void emit_module_exit (systemtap_session& s);
0a6f5a3f 7401};
79189b84 7402
bc9a523d 7403
f8a968bc
JS
7404struct tracepoint_var_expanding_visitor: public var_expanding_visitor
7405{
7406 tracepoint_var_expanding_visitor(dwflpp& dw, const string& probe_name,
7407 vector <struct tracepoint_arg>& args):
7408 dw (dw), probe_name (probe_name), args (args) {}
7409 dwflpp& dw;
7410 const string& probe_name;
7411 vector <struct tracepoint_arg>& args;
bc9a523d 7412
f8a968bc
JS
7413 void visit_target_symbol (target_symbol* e);
7414 void visit_target_symbol_arg (target_symbol* e);
7415 void visit_target_symbol_context (target_symbol* e);
7416};
79189b84
JS
7417
7418
f8a968bc
JS
7419void
7420tracepoint_var_expanding_visitor::visit_target_symbol_arg (target_symbol* e)
75ead1f7 7421{
277c21bc 7422 string argname = e->name.substr(1);
75ead1f7 7423
f8a968bc
JS
7424 // search for a tracepoint parameter matching this name
7425 tracepoint_arg *arg = NULL;
7426 for (unsigned i = 0; i < args.size(); ++i)
dcaa1a65 7427 if (args[i].usable && args[i].name == argname)
f8a968bc
JS
7428 {
7429 arg = &args[i];
7430 arg->used = true;
7431 break;
7432 }
75ead1f7 7433
f8a968bc
JS
7434 if (arg == NULL)
7435 {
7436 stringstream alternatives;
7437 for (unsigned i = 0; i < args.size(); ++i)
7438 alternatives << " $" << args[i].name;
046e7190 7439 alternatives << " $$name $$parms $$vars";
75ead1f7 7440
f8a968bc
JS
7441 // We hope that this value ends up not being referenced after all, so it
7442 // can be optimized out quietly.
277c21bc 7443 throw semantic_error("unable to find tracepoint variable '" + e->name
f8a968bc
JS
7444 + "' (alternatives:" + alternatives.str () + ")", e->tok);
7445 // NB: we can have multiple errors, since a target variable
7446 // may be expanded in several different contexts:
7447 // trace ("*") { $foo->bar }
f8a968bc 7448 }
75ead1f7 7449
f8a968bc 7450 // make sure we're not dereferencing base types
dc5a09fc 7451 if (!arg->isptr)
d19a9a82 7452 e->assert_no_components("tracepoint", true);
75ead1f7 7453
f8a968bc
JS
7454 // we can only write to dereferenced fields, and only if guru mode is on
7455 bool lvalue = is_active_lvalue(e);
7456 if (lvalue && (!dw.sess.guru_mode || e->components.empty()))
277c21bc 7457 throw semantic_error("write to tracepoint variable '" + e->name
f8a968bc 7458 + "' not permitted", e->tok);
c69a87e0 7459
ad370dcc
JS
7460 // XXX: if a struct/union arg is passed by value, then writing to its fields
7461 // is also meaningless until you dereference past a pointer member. It's
7462 // harder to detect and prevent that though...
75ead1f7 7463
f8a968bc
JS
7464 if (e->components.empty())
7465 {
03c75a4a
JS
7466 if (e->addressof)
7467 throw semantic_error("cannot take address of tracepoint variable", e->tok);
a45664f4 7468
3e3bd7b6 7469 // Just grab the value from the probe locals
a45664f4
JS
7470 symbol* sym = new symbol;
7471 sym->tok = e->tok;
7472 sym->name = "__tracepoint_arg_" + arg->name;
7473 provide (sym);
f8a968bc
JS
7474 }
7475 else
7476 {
5f36109e
JS
7477 // make a copy of the original as a bare target symbol for the tracepoint
7478 // value, which will be passed into the dwarf dereferencing code
7479 target_symbol* e2 = deep_copy_visitor::deep_copy(e);
7480 e2->components.clear();
7481
7482 if (e->components.back().type == target_symbol::comp_pretty_print)
7483 {
7484 if (lvalue)
7485 throw semantic_error("cannot write to pretty-printed variable", e->tok);
7486
d19a9a82 7487 dwarf_pretty_print dpp(dw, &arg->type_die, e2, arg->isptr, false, *e);
5f36109e
JS
7488 dpp.expand()->visit (this);
7489 return;
7490 }
7491
f8a968bc
JS
7492 // Synthesize a function to dereference the dwarf fields,
7493 // with a pointer parameter that is the base tracepoint variable
7494 functiondecl *fdecl = new functiondecl;
59de45f1 7495 fdecl->synthetic = true;
f8a968bc
JS
7496 fdecl->tok = e->tok;
7497 embeddedcode *ec = new embeddedcode;
7498 ec->tok = e->tok;
75ead1f7 7499
f8a968bc 7500 string fname = (string(lvalue ? "_tracepoint_tvar_set" : "_tracepoint_tvar_get")
277c21bc 7501 + "_" + e->name.substr(1)
aca66a36 7502 + "_" + lex_cast(tick++));
75ead1f7 7503
f8a968bc
JS
7504 fdecl->name = fname;
7505 fdecl->body = ec;
75ead1f7 7506
b5a0dd41
FCE
7507 // PR10601: adapt to kernel-vs-userspace loc2c-runtime
7508 ec->code += "\n#define fetch_register k_fetch_register\n";
7509 ec->code += "#define store_register k_store_register\n";
c69a87e0
FCE
7510
7511 ec->code += dw.literal_stmt_for_pointer (&arg->type_die, e,
f8a968bc 7512 lvalue, fdecl->type);
75ead1f7 7513
f8a968bc
JS
7514 // Give the fdecl an argument for the raw tracepoint value
7515 vardecl *v1 = new vardecl;
7516 v1->type = pe_long;
7517 v1->name = "pointer";
7518 v1->tok = e->tok;
7519 fdecl->formal_args.push_back(v1);
75ead1f7 7520
6fda2dff
JS
7521 // Any non-literal indexes need to be passed in too.
7522 for (unsigned i = 0; i < e->components.size(); ++i)
7523 if (e->components[i].type == target_symbol::comp_expression_array_index)
7524 {
7525 vardecl *v = new vardecl;
7526 v->type = pe_long;
aca66a36 7527 v->name = "index" + lex_cast(i);
6fda2dff
JS
7528 v->tok = e->tok;
7529 fdecl->formal_args.push_back(v);
7530 }
7531
f8a968bc
JS
7532 if (lvalue)
7533 {
7534 // Modify the fdecl so it carries a pe_long formal
7535 // argument called "value".
75ead1f7 7536
f8a968bc
JS
7537 // FIXME: For the time being we only support setting target
7538 // variables which have base types; these are 'pe_long' in
7539 // stap's type vocabulary. Strings and pointers might be
7540 // reasonable, some day, but not today.
7541
7542 vardecl *v2 = new vardecl;
7543 v2->type = pe_long;
7544 v2->name = "value";
7545 v2->tok = e->tok;
7546 fdecl->formal_args.push_back(v2);
7547 }
7548 else
7549 ec->code += "/* pure */";
7550
64211010
DB
7551 ec->code += "/* unprivileged */";
7552
b5a0dd41
FCE
7553 // PR10601
7554 ec->code += "\n#undef fetch_register\n";
7555 ec->code += "\n#undef store_register\n";
aff5d390 7556
f8809d54 7557 fdecl->join (dw.sess);
75ead1f7 7558
f8a968bc
JS
7559 // Synthesize a functioncall.
7560 functioncall* n = new functioncall;
7561 n->tok = e->tok;
7562 n->function = fname;
6fda2dff
JS
7563 n->args.push_back(require(e2));
7564
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 n->args.push_back(require(e->components[i].expr_index));
75ead1f7 7569
f8a968bc
JS
7570 if (lvalue)
7571 {
7572 // Provide the functioncall to our parent, so that it can be
7573 // used to substitute for the assignment node immediately above
7574 // us.
7575 assert(!target_symbol_setter_functioncalls.empty());
7576 *(target_symbol_setter_functioncalls.top()) = n;
7577 }
75ead1f7 7578
f8a968bc
JS
7579 provide (n);
7580 }
75ead1f7
JS
7581}
7582
7583
f8a968bc
JS
7584void
7585tracepoint_var_expanding_visitor::visit_target_symbol_context (target_symbol* e)
0a6f5a3f 7586{
03c75a4a
JS
7587 if (e->addressof)
7588 throw semantic_error("cannot take address of context variable", e->tok);
7589
f8a968bc 7590 if (is_active_lvalue (e))
277c21bc 7591 throw semantic_error("write to tracepoint '" + e->name + "' not permitted", e->tok);
0a6f5a3f 7592
277c21bc 7593 if (e->name == "$$name")
f8a968bc 7594 {
5f36109e
JS
7595 e->assert_no_components("tracepoint");
7596
bfdaad1e
DS
7597 // Synthesize an embedded expression.
7598 embedded_expr *expr = new embedded_expr;
7599 expr->tok = e->tok;
7600 expr->code = string("/* string */ /* pure */ ")
7601 + string("c->marker_name ? c->marker_name : \"\"");
7602 provide (expr);
f8a968bc 7603 }
277c21bc 7604 else if (e->name == "$$vars" || e->name == "$$parms")
f8a968bc 7605 {
5f36109e
JS
7606 e->assert_no_components("tracepoint", true);
7607
f8a968bc
JS
7608 // Convert $$vars to sprintf of a list of vars which we recursively evaluate
7609 // NB: we synthesize a new token here rather than reusing
7610 // e->tok, because print_format::print likes to use
7611 // its tok->content.
7612 token* pf_tok = new token(*e->tok);
7613 pf_tok->content = "sprintf";
0a6f5a3f 7614
d5e178c1 7615 print_format* pf = print_format::create(pf_tok);
0a6f5a3f 7616
f8a968bc 7617 for (unsigned i = 0; i < args.size(); ++i)
b278033a 7618 {
dcaa1a65
JS
7619 if (!args[i].usable)
7620 continue;
f8a968bc
JS
7621 if (i > 0)
7622 pf->raw_components += " ";
7623 pf->raw_components += args[i].name;
3e3bd7b6 7624 target_symbol *tsym = new target_symbol;
f8a968bc 7625 tsym->tok = e->tok;
277c21bc 7626 tsym->name = "$" + args[i].name;
5f36109e 7627 tsym->components = e->components;
b278033a 7628
f8a968bc
JS
7629 // every variable should always be accessible!
7630 tsym->saved_conversion_error = 0;
7631 expression *texp = require (tsym); // NB: throws nothing ...
14a97852
JS
7632 if (tsym->saved_conversion_error) // ... but this is how we know it happened.
7633 {
7634 if (dw.sess.verbose>2)
7635 for (semantic_error *c = tsym->saved_conversion_error;
7636 c != 0; c = c->chain)
7637 clog << "variable location problem: " << c->what() << endl;
7638 pf->raw_components += "=?";
7639 continue;
7640 }
b278033a 7641
5f36109e
JS
7642 if (!e->components.empty() &&
7643 e->components[0].type == target_symbol::comp_pretty_print)
7644 pf->raw_components += "=%s";
7645 else
7646 pf->raw_components += args[i].isptr ? "=%p" : "=%#x";
f8a968bc
JS
7647 pf->args.push_back(texp);
7648 }
0a6f5a3f 7649
f8a968bc
JS
7650 pf->components = print_format::string_to_components(pf->raw_components);
7651 provide (pf);
b278033a 7652 }
f8a968bc
JS
7653 else
7654 assert(0); // shouldn't get here
0a6f5a3f
JS
7655}
7656
0a6f5a3f 7657void
f8a968bc 7658tracepoint_var_expanding_visitor::visit_target_symbol (target_symbol* e)
0a6f5a3f 7659{
aff5d390 7660 try
c69a87e0 7661 {
277c21bc 7662 assert(e->name.size() > 0 && e->name[0] == '$');
aff5d390 7663
277c21bc 7664 if (e->name == "$$name" || e->name == "$$parms" || e->name == "$$vars")
c69a87e0
FCE
7665 visit_target_symbol_context (e);
7666 else
7667 visit_target_symbol_arg (e);
7668 }
7669 catch (const semantic_error &er)
7670 {
1af1e62d 7671 e->chain (er);
c69a87e0
FCE
7672 provide (e);
7673 }
0a6f5a3f
JS
7674}
7675
7676
7677
79189b84
JS
7678tracepoint_derived_probe::tracepoint_derived_probe (systemtap_session& s,
7679 dwflpp& dw, Dwarf_Die& func_die,
7680 const string& tracepoint_name,
7681 probe* base, probe_point* loc):
4c5d1300 7682 derived_probe (base, loc, true /* .components soon rewritten */),
79189b84 7683 sess (s), tracepoint_name (tracepoint_name)
56894e91 7684{
79189b84
JS
7685 // create synthetic probe point name; preserve condition
7686 vector<probe_point::component*> comps;
7687 comps.push_back (new probe_point::component (TOK_KERNEL));
7688 comps.push_back (new probe_point::component (TOK_TRACE, new literal_string (tracepoint_name)));
7689 this->sole_location()->components = comps;
7690
6fb70fb7
JS
7691 // fill out the available arguments in this tracepoint
7692 build_args(dw, func_die);
56894e91 7693
6fb70fb7
JS
7694 // determine which header defined this tracepoint
7695 string decl_file = dwarf_decl_file(&func_die);
7696 size_t header_pos = decl_file.rfind("trace/");
7697 if (header_pos == string::npos)
7698 throw semantic_error ("cannot parse header location for tracepoint '"
7699 + tracepoint_name + "' in '"
7700 + decl_file + "'");
7701 header = decl_file.substr(header_pos);
56894e91 7702
6fb70fb7
JS
7703 // tracepoints from FOO_event_types.h should really be included from FOO.h
7704 // XXX can dwarf tell us the include hierarchy? it would be better to
7705 // ... walk up to see which one was directly included by tracequery.c
3c1b3d06 7706 // XXX: see also PR9993.
6fb70fb7
JS
7707 header_pos = header.find("_event_types");
7708 if (header_pos != string::npos)
7709 header.erase(header_pos, 12);
56894e91 7710
f8a968bc
JS
7711 // Now expand the local variables in the probe body
7712 tracepoint_var_expanding_visitor v (dw, name, args);
8b095b45 7713 v.replace (this->body);
a45664f4
JS
7714 for (unsigned i = 0; i < args.size(); i++)
7715 if (args[i].used)
7716 {
7717 vardecl* v = new vardecl;
7718 v->name = "__tracepoint_arg_" + args[i].name;
7719 v->tok = this->tok;
58701b78 7720 v->set_arity(0, this->tok);
a45664f4
JS
7721 v->type = pe_long;
7722 v->skip_init = true;
7723 this->locals.push_back (v);
7724 }
56894e91 7725
79189b84
JS
7726 if (sess.verbose > 2)
7727 clog << "tracepoint-based " << name << " tracepoint='" << tracepoint_name
7728 << "'" << endl;
7729}
dc38c0ae 7730
56894e91 7731
f8a968bc 7732static bool
dcaa1a65 7733resolve_tracepoint_arg_type(tracepoint_arg& arg)
46b84a80 7734{
d19a9a82 7735 Dwarf_Die type;
dcaa1a65 7736 switch (dwarf_tag(&arg.type_die))
b20febf3 7737 {
f8a968bc
JS
7738 case DW_TAG_typedef:
7739 case DW_TAG_const_type:
7740 case DW_TAG_volatile_type:
7741 // iterate on the referent type
3d1ad340 7742 return (dwarf_attr_die(&arg.type_die, DW_AT_type, &arg.type_die)
dcaa1a65 7743 && resolve_tracepoint_arg_type(arg));
f8a968bc
JS
7744 case DW_TAG_base_type:
7745 // base types will simply be treated as script longs
dcaa1a65 7746 arg.isptr = false;
f8a968bc
JS
7747 return true;
7748 case DW_TAG_pointer_type:
dcaa1a65
JS
7749 // pointers can be treated as script longs,
7750 // and if we know their type, they can also be dereferenced
d19a9a82
JS
7751 type = arg.type_die;
7752 while (dwarf_attr_die(&arg.type_die, DW_AT_type, &arg.type_die))
7753 {
7754 // It still might be a non-type, e.g. const void,
7755 // so we need to strip away all qualifiers.
7756 int tag = dwarf_tag(&arg.type_die);
7757 if (tag != DW_TAG_typedef &&
7758 tag != DW_TAG_const_type &&
7759 tag != DW_TAG_volatile_type)
7760 {
7761 arg.isptr = true;
7762 break;
7763 }
7764 }
7765 if (!arg.isptr)
7766 arg.type_die = type;
ad370dcc
JS
7767 arg.typecast = "(intptr_t)";
7768 return true;
7769 case DW_TAG_structure_type:
7770 case DW_TAG_union_type:
7771 // for structs/unions which are passed by value, we turn it into
7772 // a pointer that can be dereferenced.
7773 arg.isptr = true;
7774 arg.typecast = "(intptr_t)&";
dcaa1a65 7775 return true;
f8a968bc
JS
7776 default:
7777 // should we consider other types too?
7778 return false;
b20febf3 7779 }
56894e91
JS
7780}
7781
7782
7783void
6fb70fb7 7784tracepoint_derived_probe::build_args(dwflpp& dw, Dwarf_Die& func_die)
56894e91 7785{
6fb70fb7
JS
7786 Dwarf_Die arg;
7787 if (dwarf_child(&func_die, &arg) == 0)
7788 do
7789 if (dwarf_tag(&arg) == DW_TAG_formal_parameter)
7790 {
7791 // build a tracepoint_arg for this parameter
7792 tracepoint_arg tparg;
23d106b9 7793 tparg.name = dwarf_diename(&arg);
56894e91 7794
6fb70fb7 7795 // read the type of this parameter
3d1ad340 7796 if (!dwarf_attr_die (&arg, DW_AT_type, &tparg.type_die)
f1c8f8a5 7797 || !dwarf_type_name(&tparg.type_die, tparg.c_type))
6fb70fb7
JS
7798 throw semantic_error ("cannot get type of tracepoint '"
7799 + tracepoint_name + "' parameter '"
7800 + tparg.name + "'");
a68f81a2 7801
dcaa1a65 7802 tparg.usable = resolve_tracepoint_arg_type(tparg);
6fb70fb7
JS
7803 args.push_back(tparg);
7804 if (sess.verbose > 4)
7805 clog << "found parameter for tracepoint '" << tracepoint_name
7806 << "': type:'" << tparg.c_type
7807 << "' name:'" << tparg.name << "'" << endl;
7808 }
7809 while (dwarf_siblingof(&arg, &arg) == 0);
56894e91
JS
7810}
7811
dc38c0ae 7812void
d0bfd2ac 7813tracepoint_derived_probe::getargs(std::list<std::string> &arg_set) const
dc38c0ae 7814{
dcaa1a65
JS
7815 for (unsigned i = 0; i < args.size(); ++i)
7816 if (args[i].usable)
d0bfd2ac 7817 arg_set.push_back("$"+args[i].name+":"+args[i].c_type);
dc38c0ae
DS
7818}
7819
79189b84
JS
7820void
7821tracepoint_derived_probe::join_group (systemtap_session& s)
197a4d62 7822{
79189b84
JS
7823 if (! s.tracepoint_derived_probes)
7824 s.tracepoint_derived_probes = new tracepoint_derived_probe_group ();
7825 s.tracepoint_derived_probes->enroll (this);
7826}
e38d6504 7827
56894e91 7828
197a4d62 7829void
3e3bd7b6 7830tracepoint_derived_probe::print_dupe_stamp(ostream& o)
56894e91 7831{
3e3bd7b6
JS
7832 for (unsigned i = 0; i < args.size(); i++)
7833 if (args[i].used)
7834 o << "__tracepoint_arg_" << args[i].name << endl;
197a4d62 7835}
56894e91 7836
3e3bd7b6 7837
9e0cd21a 7838static vector<string> tracepoint_extra_headers (systemtap_session& s)
47dd066d 7839{
3c1b3d06
FCE
7840 vector<string> they_live;
7841 // PR 9993
7842 // XXX: may need this to be configurable
7843 they_live.push_back ("linux/skbuff.h");
9e0cd21a
FCE
7844
7845 // PR11649: conditional extra header
7846 // for kvm tracepoints in 2.6.33ish
7847 if (s.kernel_config["CONFIG_KVM"] != string("")) {
7848 they_live.push_back ("linux/kvm_host.h");
7849 }
3c1b3d06
FCE
7850 return they_live;
7851}
47dd066d
WC
7852
7853
7854void
79189b84 7855tracepoint_derived_probe_group::emit_module_decls (systemtap_session& s)
47dd066d 7856{
79189b84
JS
7857 if (probes.empty())
7858 return;
47dd066d 7859
96b030fe
JS
7860 s.op->newline() << "/* ---- tracepoint probes ---- */";
7861 s.op->newline();
79189b84 7862
3c1b3d06
FCE
7863 // PR9993: Add extra headers to work around undeclared types in individual
7864 // include/trace/foo.h files
9e0cd21a 7865 const vector<string>& extra_headers = tracepoint_extra_headers (s);
3c1b3d06
FCE
7866 for (unsigned z=0; z<extra_headers.size(); z++)
7867 s.op->newline() << "#include <" << extra_headers[z] << ">\n";
47dd066d 7868
6fb70fb7
JS
7869 for (unsigned i = 0; i < probes.size(); ++i)
7870 {
7871 tracepoint_derived_probe *p = probes[i];
47dd066d 7872
96b030fe
JS
7873 // emit a separate entry function for each probe, since tracepoints
7874 // don't provide any sort of context pointer.
392e08b7 7875 s.op->newline() << "#undef TRACE_INCLUDE_FILE";
6fb70fb7 7876 s.op->newline() << "#include <" << p->header << ">";
5f73a260
JS
7877
7878 // Starting in 2.6.35, at the same time NOARGS was added, the callback
7879 // always has a void* as the first parameter. PR11599
7880 s.op->newline() << "#ifdef DECLARE_TRACE_NOARGS";
7881 s.op->newline() << "#define STAP_TP_DATA , NULL";
7882 s.op->newline() << "#define STAP_TP_PROTO void *cb_data"
7883 << " __attribute__ ((unused))";
7884 if (!p->args.empty())
7885 s.op->line() << ",";
7886 s.op->newline() << "#else";
7887 s.op->newline() << "#define STAP_TP_DATA";
7888 s.op->newline() << "#define STAP_TP_PROTO";
7889 if (p->args.empty())
7890 s.op->line() << " void";
7891 s.op->newline() << "#endif";
7892
6fb70fb7 7893 s.op->newline() << "static void enter_tracepoint_probe_" << i << "(";
5f73a260
JS
7894 s.op->newline(2) << "STAP_TP_PROTO";
7895
6fb70fb7
JS
7896 for (unsigned j = 0; j < p->args.size(); ++j)
7897 {
7898 if (j > 0)
5f73a260
JS
7899 s.op->line() << ",";
7900 s.op->newline() << p->args[j].c_type << " __tracepoint_arg_" << p->args[j].name;
6fb70fb7 7901 }
5f73a260
JS
7902 s.op->newline() << ")";
7903 s.op->newline(-2) << "{";
7904
26e63673 7905 s.op->newline(1) << "struct stap_probe * const probe = "
faea5e16
JS
7906 << common_probe_init (p) << ";";
7907 common_probe_entryfn_prologue (s.op, "STAP_SESSION_RUNNING", "probe");
f8a968bc 7908 s.op->newline() << "c->marker_name = "
c12d974f
FCE
7909 << lex_cast_qstring (p->tracepoint_name)
7910 << ";";
f8a968bc
JS
7911 for (unsigned j = 0; j < p->args.size(); ++j)
7912 if (p->args[j].used)
7913 {
66671fd8 7914 s.op->newline() << "c->probe_locals." << p->name << ".__tracepoint_arg_"
f8a968bc 7915 << p->args[j].name << " = (int64_t)";
ad370dcc 7916 s.op->line() << p->args[j].typecast;
f8a968bc
JS
7917 s.op->line() << "__tracepoint_arg_" << p->args[j].name << ";";
7918 }
26e63673 7919 s.op->newline() << "(*probe->ph) (c);";
6fb70fb7
JS
7920 common_probe_entryfn_epilogue (s.op);
7921 s.op->newline(-1) << "}";
47dd066d 7922
96b030fe
JS
7923 // emit normalized registration functions
7924 s.op->newline() << "static int register_tracepoint_probe_" << i << "(void) {";
7925 s.op->newline(1) << "return register_trace_" << p->tracepoint_name
5f73a260 7926 << "(enter_tracepoint_probe_" << i << " STAP_TP_DATA);";
96b030fe 7927 s.op->newline(-1) << "}";
47dd066d 7928
86758d5f
JS
7929 // NB: we're not prepared to deal with unreg failures. However, failures
7930 // can only occur if the tracepoint doesn't exist (yet?), or if we
7931 // weren't even registered. The former should be OKed by the initial
7932 // registration call, and the latter is safe to ignore.
7933 s.op->newline() << "static void unregister_tracepoint_probe_" << i << "(void) {";
7934 s.op->newline(1) << "(void) unregister_trace_" << p->tracepoint_name
5f73a260 7935 << "(enter_tracepoint_probe_" << i << " STAP_TP_DATA);";
96b030fe 7936 s.op->newline(-1) << "}";
6fb70fb7 7937 s.op->newline();
5f73a260
JS
7938
7939 s.op->newline() << "#undef STAP_TP_DATA";
7940 s.op->newline() << "#undef STAP_TP_PROTO";
7941 s.op->newline();
af304783
DS
7942 }
7943
96b030fe
JS
7944 // emit an array of registration functions for easy init/shutdown
7945 s.op->newline() << "static struct stap_tracepoint_probe {";
7946 s.op->newline(1) << "int (*reg)(void);";
86758d5f 7947 s.op->newline(0) << "void (*unreg)(void);";
96b030fe
JS
7948 s.op->newline(-1) << "} stap_tracepoint_probes[] = {";
7949 s.op->indent(1);
7950 for (unsigned i = 0; i < probes.size(); ++i)
7951 {
7952 s.op->newline () << "{";
7953 s.op->line() << " .reg=&register_tracepoint_probe_" << i << ",";
7954 s.op->line() << " .unreg=&unregister_tracepoint_probe_" << i;
7955 s.op->line() << " },";
7956 }
7957 s.op->newline(-1) << "};";
7958 s.op->newline();
47dd066d
WC
7959}
7960
7961
79189b84
JS
7962void
7963tracepoint_derived_probe_group::emit_module_init (systemtap_session &s)
47dd066d 7964{
79189b84
JS
7965 if (probes.size () == 0)
7966 return;
47dd066d 7967
79189b84 7968 s.op->newline() << "/* init tracepoint probes */";
96b030fe
JS
7969 s.op->newline() << "for (i=0; i<" << probes.size() << "; i++) {";
7970 s.op->newline(1) << "rc = stap_tracepoint_probes[i].reg();";
7971 s.op->newline() << "if (rc) {";
7972 s.op->newline(1) << "for (j=i-1; j>=0; j--)"; // partial rollback
7973 s.op->newline(1) << "stap_tracepoint_probes[j].unreg();";
7974 s.op->newline(-1) << "break;"; // don't attempt to register any more probes
7975 s.op->newline(-1) << "}";
7976 s.op->newline(-1) << "}";
47dd066d 7977
bc9a523d
FCE
7978 // This would be technically proper (on those autoconf-detectable
7979 // kernels that include this function in tracepoint.h), however we
7980 // already make several calls to synchronze_sched() during our
7981 // shutdown processes.
47dd066d 7982
bc9a523d
FCE
7983 // s.op->newline() << "if (rc)";
7984 // s.op->newline(1) << "tracepoint_synchronize_unregister();";
7985 // s.op->indent(-1);
79189b84 7986}
47dd066d
WC
7987
7988
79189b84
JS
7989void
7990tracepoint_derived_probe_group::emit_module_exit (systemtap_session& s)
47dd066d 7991{
79189b84
JS
7992 if (probes.empty())
7993 return;
47dd066d 7994
96b030fe
JS
7995 s.op->newline() << "/* deregister tracepoint probes */";
7996 s.op->newline() << "for (i=0; i<" << probes.size() << "; i++)";
7997 s.op->newline(1) << "stap_tracepoint_probes[i].unreg();";
7998 s.op->indent(-1);
47dd066d 7999
bc9a523d 8000 // Not necessary: see above.
47dd066d 8001
bc9a523d 8002 // s.op->newline() << "tracepoint_synchronize_unregister();";
79189b84 8003}
b20febf3 8004
47dd066d 8005
75ead1f7 8006struct tracepoint_query : public base_query
47dd066d 8007{
75ead1f7
JS
8008 tracepoint_query(dwflpp & dw, const string & tracepoint,
8009 probe * base_probe, probe_point * base_loc,
8010 vector<derived_probe *> & results):
8011 base_query(dw, "*"), tracepoint(tracepoint),
8012 base_probe(base_probe), base_loc(base_loc),
8013 results(results) {}
47dd066d 8014
75ead1f7 8015 const string& tracepoint;
47dd066d 8016
75ead1f7
JS
8017 probe * base_probe;
8018 probe_point * base_loc;
8019 vector<derived_probe *> & results;
f982c59b 8020 set<string> probed_names;
47dd066d 8021
75ead1f7
JS
8022 void handle_query_module();
8023 int handle_query_cu(Dwarf_Die * cudie);
8024 int handle_query_func(Dwarf_Die * func);
b20febf3 8025
75ead1f7
JS
8026 static int tracepoint_query_cu (Dwarf_Die * cudie, void * arg);
8027 static int tracepoint_query_func (Dwarf_Die * func, base_query * query);
8028};
47dd066d
WC
8029
8030
8031void
75ead1f7 8032tracepoint_query::handle_query_module()
47dd066d 8033{
75ead1f7
JS
8034 // look for the tracepoints in each CU
8035 dw.iterate_over_cus(tracepoint_query_cu, this);
47dd066d
WC
8036}
8037
8038
75ead1f7
JS
8039int
8040tracepoint_query::handle_query_cu(Dwarf_Die * cudie)
47dd066d 8041{
75ead1f7 8042 dw.focus_on_cu (cudie);
47dd066d 8043
75ead1f7
JS
8044 // look at each function to see if it's a tracepoint
8045 string function = "stapprobe_" + tracepoint;
8046 return dw.iterate_over_functions (tracepoint_query_func, this, function);
47dd066d
WC
8047}
8048
8049
75ead1f7
JS
8050int
8051tracepoint_query::handle_query_func(Dwarf_Die * func)
47dd066d 8052{
75ead1f7 8053 dw.focus_on_function (func);
47dd066d 8054
60d98537 8055 assert(startswith(dw.function_name, "stapprobe_"));
75ead1f7 8056 string tracepoint_instance = dw.function_name.substr(10);
f982c59b
JS
8057
8058 // check for duplicates -- sometimes tracepoint headers may be indirectly
8059 // included in more than one of our tracequery modules.
8060 if (!probed_names.insert(tracepoint_instance).second)
8061 return DWARF_CB_OK;
8062
79189b84
JS
8063 derived_probe *dp = new tracepoint_derived_probe (dw.sess, dw, *func,
8064 tracepoint_instance,
8065 base_probe, base_loc);
8066 results.push_back (dp);
75ead1f7 8067 return DWARF_CB_OK;
47dd066d
WC
8068}
8069
8070
75ead1f7
JS
8071int
8072tracepoint_query::tracepoint_query_cu (Dwarf_Die * cudie, void * arg)
47dd066d 8073{
75ead1f7 8074 tracepoint_query * q = static_cast<tracepoint_query *>(arg);
85007c04 8075 if (pending_interrupts) return DWARF_CB_ABORT;
75ead1f7 8076 return q->handle_query_cu(cudie);
47dd066d
WC
8077}
8078
8079
75ead1f7
JS
8080int
8081tracepoint_query::tracepoint_query_func (Dwarf_Die * func, base_query * query)
47dd066d 8082{
75ead1f7 8083 tracepoint_query * q = static_cast<tracepoint_query *>(query);
85007c04 8084 if (pending_interrupts) return DWARF_CB_ABORT;
75ead1f7 8085 return q->handle_query_func(func);
47dd066d
WC
8086}
8087
8088
0a6f5a3f 8089struct tracepoint_builder: public derived_probe_builder
47dd066d 8090{
0a6f5a3f
JS
8091private:
8092 dwflpp *dw;
8093 bool init_dw(systemtap_session& s);
55e50c24
JS
8094 string get_tracequery_module(systemtap_session& s,
8095 const vector<string>& headers);
47dd066d 8096
0a6f5a3f 8097public:
47dd066d 8098
0a6f5a3f
JS
8099 tracepoint_builder(): dw(0) {}
8100 ~tracepoint_builder() { delete dw; }
47dd066d 8101
0a6f5a3f
JS
8102 void build_no_more (systemtap_session& s)
8103 {
8104 if (dw && s.verbose > 3)
8105 clog << "tracepoint_builder releasing dwflpp" << endl;
8106 delete dw;
8107 dw = NULL;
8108 }
47dd066d 8109
0a6f5a3f
JS
8110 void build(systemtap_session& s,
8111 probe *base, probe_point *location,
8112 literal_map_t const& parameters,
8113 vector<derived_probe*>& finished_results);
8114};
47dd066d 8115
47dd066d 8116
f982c59b
JS
8117string
8118tracepoint_builder::get_tracequery_module(systemtap_session& s,
55e50c24 8119 const vector<string>& headers)
0a6f5a3f 8120{
c95eddf7 8121 if (s.verbose > 2)
55e50c24
JS
8122 {
8123 clog << "Pass 2: getting a tracequery for "
8124 << headers.size() << " headers:" << endl;
8125 for (size_t i = 0; i < headers.size(); ++i)
8126 clog << " " << headers[i] << endl;
8127 }
c95eddf7 8128
a2639cb7 8129 string tracequery_path;
b278033a
JS
8130 if (s.use_cache)
8131 {
8132 // see if the cached module exists
55e50c24 8133 tracequery_path = find_tracequery_hash(s, headers);
d105f664 8134 if (!tracequery_path.empty() && !s.poison_cache)
b278033a 8135 {
a2639cb7 8136 int fd = open(tracequery_path.c_str(), O_RDONLY);
b278033a
JS
8137 if (fd != -1)
8138 {
8139 if (s.verbose > 2)
a2639cb7 8140 clog << "Pass 2: using cached " << tracequery_path << endl;
b278033a 8141 close(fd);
f982c59b 8142 return tracequery_path;
b278033a
JS
8143 }
8144 }
8145 }
47dd066d 8146
b278033a 8147 // no cached module, time to make it
f982c59b 8148
55e50c24
JS
8149 // PR9993: Add extra headers to work around undeclared types in individual
8150 // include/trace/foo.h files
9e0cd21a 8151 vector<string> short_headers = tracepoint_extra_headers(s);
55e50c24
JS
8152
8153 // add each requested tracepoint header
8154 for (size_t i = 0; i < headers.size(); ++i)
8155 {
8156 const string &header = headers[i];
8157 size_t root_pos = header.rfind("/include/");
8158 short_headers.push_back((root_pos != string::npos) ?
8159 header.substr(root_pos + 9) :
8160 header);
8161 }
f982c59b 8162
0a6f5a3f 8163 string tracequery_ko;
55e50c24 8164 int rc = make_tracequery(s, tracequery_ko, short_headers);
0a6f5a3f 8165 if (rc != 0)
c95eddf7 8166 tracequery_ko = "/dev/null";
47dd066d 8167
e16dc041 8168 // try to save tracequery in the cache
b278033a 8169 if (s.use_cache)
e16dc041
JS
8170 copy_file(tracequery_ko, tracequery_path, s.verbose > 2);
8171
f982c59b
JS
8172 return tracequery_ko;
8173}
8174
8175
8176bool
8177tracepoint_builder::init_dw(systemtap_session& s)
8178{
8179 if (dw != NULL)
8180 return true;
8181
8182 vector<string> tracequery_modules;
55e50c24 8183 vector<string> system_headers;
f982c59b
JS
8184
8185 glob_t trace_glob;
8186 string globs[] = {
f982c59b 8187 "/include/trace/events/*.h",
f982c59b 8188 "/source/include/trace/events/*.h",
508968a7
JS
8189 "/include/trace/*.h",
8190 "/source/include/trace/*.h",
f982c59b
JS
8191 };
8192 for (unsigned z = 0; z < sizeof(globs) / sizeof(globs[0]); z++)
8193 {
8194 string glob_str(s.kernel_build_tree + globs[z]);
8195 glob(glob_str.c_str(), 0, NULL, &trace_glob);
8196 for (unsigned i = 0; i < trace_glob.gl_pathc; ++i)
8197 {
8198 string header(trace_glob.gl_pathv[i]);
8199
8200 // filter out a few known "internal-only" headers
60d98537
JS
8201 if (endswith(header, "/define_trace.h") ||
8202 endswith(header, "/ftrace.h") ||
8203 endswith(header, "/trace_events.h") ||
8204 endswith(header, "_event_types.h"))
f982c59b
JS
8205 continue;
8206
55e50c24 8207 system_headers.push_back(header);
f982c59b
JS
8208 }
8209 globfree(&trace_glob);
8210 }
8211
55e50c24
JS
8212 // First attempt to do all system headers in one go
8213 string tracequery_path = get_tracequery_module(s, system_headers);
8214 // NB: An empty tracequery means that the header didn't compile correctly
8215 if (get_file_size(tracequery_path))
8216 tracequery_modules.push_back(tracequery_path);
8217 else
8218 // Otherwise try to do them one at a time (PR10424)
8219 for (size_t i = 0; i < system_headers.size(); ++i)
8220 {
85007c04 8221 if (pending_interrupts) return false;
392e08b7 8222
55e50c24
JS
8223 vector<string> one_header(1, system_headers[i]);
8224 tracequery_path = get_tracequery_module(s, one_header);
8225 if (get_file_size(tracequery_path))
8226 tracequery_modules.push_back(tracequery_path);
8227 }
8228
f982c59b
JS
8229 // TODO: consider other sources of tracepoint headers too, like from
8230 // a command-line parameter or some environment or .systemtaprc
47dd066d 8231
59c11f91 8232 dw = new dwflpp(s, tracequery_modules, true);
0a6f5a3f
JS
8233 return true;
8234}
47dd066d 8235
0a6f5a3f
JS
8236void
8237tracepoint_builder::build(systemtap_session& s,
8238 probe *base, probe_point *location,
8239 literal_map_t const& parameters,
8240 vector<derived_probe*>& finished_results)
8241{
8242 if (!init_dw(s))
8243 return;
47dd066d 8244
75ead1f7
JS
8245 string tracepoint;
8246 assert(get_param (parameters, TOK_TRACE, tracepoint));
47dd066d 8247
75ead1f7 8248 tracepoint_query q(*dw, tracepoint, base, location, finished_results);
51178501 8249 dw->iterate_over_modules(&query_module, &q);
47dd066d 8250}
47dd066d 8251
e6fe60e7 8252
b55bc428 8253// ------------------------------------------------------------------------
bd2b1e68 8254// Standard tapset registry.
b55bc428
FCE
8255// ------------------------------------------------------------------------
8256
7a053d3b 8257void
f8220a7b 8258register_standard_tapsets(systemtap_session & s)
b55bc428 8259{
47e0478e 8260 register_tapset_been(s);
93646f4d 8261 register_tapset_itrace(s);
dd0e4fa7 8262 register_tapset_mark(s);
7a212aa8 8263 register_tapset_procfs(s);
912e8c59 8264 register_tapset_timers(s);
b84779a5 8265 register_tapset_utrace(s);
b98a8d73 8266
7a24d422 8267 // dwarf-based kprobe/uprobe parts
c4ce66a1 8268 dwarf_derived_probe::register_patterns(s);
30a279be 8269
888af770
FCE
8270 // XXX: user-space starter set
8271 s.pattern_root->bind_num(TOK_PROCESS)
8272 ->bind_num(TOK_STATEMENT)->bind(TOK_ABSOLUTE)
27dc09b1 8273 ->bind_unprivileged()
888af770
FCE
8274 ->bind(new uprobe_builder ());
8275 s.pattern_root->bind_num(TOK_PROCESS)
8276 ->bind_num(TOK_STATEMENT)->bind(TOK_ABSOLUTE)->bind(TOK_RETURN)
27dc09b1 8277 ->bind_unprivileged()
888af770
FCE
8278 ->bind(new uprobe_builder ());
8279
0a6f5a3f
JS
8280 // kernel tracepoint probes
8281 s.pattern_root->bind(TOK_KERNEL)->bind_str(TOK_TRACE)
8282 ->bind(new tracepoint_builder());
8283
e6fe60e7
AM
8284 // Kprobe based probe
8285 s.pattern_root->bind(TOK_KPROBE)->bind_str(TOK_FUNCTION)
8286 ->bind(new kprobe_builder());
8287 s.pattern_root->bind(TOK_KPROBE)->bind_str(TOK_MODULE)
8288 ->bind_str(TOK_FUNCTION)->bind(new kprobe_builder());
8289 s.pattern_root->bind(TOK_KPROBE)->bind_str(TOK_FUNCTION)->bind(TOK_RETURN)
8290 ->bind(new kprobe_builder());
b6371390
JS
8291 s.pattern_root->bind(TOK_KPROBE)->bind_str(TOK_FUNCTION)->bind(TOK_RETURN)
8292 ->bind_num(TOK_MAXACTIVE)->bind(new kprobe_builder());
e6fe60e7
AM
8293 s.pattern_root->bind(TOK_KPROBE)->bind_str(TOK_MODULE)
8294 ->bind_str(TOK_FUNCTION)->bind(TOK_RETURN)->bind(new kprobe_builder());
b6371390
JS
8295 s.pattern_root->bind(TOK_KPROBE)->bind_str(TOK_MODULE)
8296 ->bind_str(TOK_FUNCTION)->bind(TOK_RETURN)
8297 ->bind_num(TOK_MAXACTIVE)->bind(new kprobe_builder());
e6fe60e7
AM
8298 s.pattern_root->bind(TOK_KPROBE)->bind_num(TOK_STATEMENT)
8299 ->bind(TOK_ABSOLUTE)->bind(new kprobe_builder());
dd225250
PS
8300
8301 //Hwbkpt based probe
b47f3a55
FCE
8302 // NB: we formerly registered the probe point types only if the kernel configuration
8303 // allowed it. However, we get better error messages if we allow probes to resolve.
8304 s.pattern_root->bind(TOK_KERNEL)->bind_num(TOK_HWBKPT)
8305 ->bind(TOK_HWBKPT_WRITE)->bind(new hwbkpt_builder());
8306 s.pattern_root->bind(TOK_KERNEL)->bind_str(TOK_HWBKPT)
8307 ->bind(TOK_HWBKPT_WRITE)->bind(new hwbkpt_builder());
8308 s.pattern_root->bind(TOK_KERNEL)->bind_num(TOK_HWBKPT)
8309 ->bind(TOK_HWBKPT_RW)->bind(new hwbkpt_builder());
8310 s.pattern_root->bind(TOK_KERNEL)->bind_str(TOK_HWBKPT)
8311 ->bind(TOK_HWBKPT_RW)->bind(new hwbkpt_builder());
8312 s.pattern_root->bind(TOK_KERNEL)->bind_num(TOK_HWBKPT)
8313 ->bind_num(TOK_LENGTH)->bind(TOK_HWBKPT_WRITE)->bind(new hwbkpt_builder());
8314 s.pattern_root->bind(TOK_KERNEL)->bind_num(TOK_HWBKPT)
8315 ->bind_num(TOK_LENGTH)->bind(TOK_HWBKPT_RW)->bind(new hwbkpt_builder());
8316 // length supported with address only, not symbol names
83ea76b1
WC
8317
8318 //perf event based probe
4763f713 8319 register_tapset_perf(s);
b55bc428 8320}
dc38c0ae
DS
8321
8322
b20febf3
FCE
8323vector<derived_probe_group*>
8324all_session_groups(systemtap_session& s)
dc38c0ae 8325{
b20febf3 8326 vector<derived_probe_group*> g;
912e8c59
JS
8327
8328#define DOONE(x) \
8329 if (s. x##_derived_probes) \
8330 g.push_back ((derived_probe_group*)(s. x##_derived_probes))
ab655cf8
DS
8331
8332 // Note that order *is* important here. We want to make sure we
8333 // register (actually run) begin probes before any other probe type
8334 // is run. Similarly, when unregistering probes, we want to
8335 // unregister (actually run) end probes after every other probe type
8336 // has be unregistered. To do the latter,
8337 // c_unparser::emit_module_exit() will run this list backwards.
b20febf3
FCE
8338 DOONE(be);
8339 DOONE(dwarf);
888af770 8340 DOONE(uprobe);
b20febf3
FCE
8341 DOONE(timer);
8342 DOONE(profile);
8343 DOONE(mark);
0a6f5a3f 8344 DOONE(tracepoint);
e6fe60e7 8345 DOONE(kprobe);
dd225250 8346 DOONE(hwbkpt);
83ea76b1 8347 DOONE(perf);
b20febf3 8348 DOONE(hrtimer);
ce82316f 8349 DOONE(procfs);
935447c8
DS
8350
8351 // Another "order is important" item. We want to make sure we
8352 // "register" the dummy task_finder probe group after all probe
8353 // groups that use the task_finder.
8354 DOONE(utrace);
a96d1db0 8355 DOONE(itrace);
935447c8 8356 DOONE(task_finder);
b20febf3
FCE
8357#undef DOONE
8358 return g;
46b84a80 8359}
73267b89
JS
8360
8361/* vim: set sw=2 ts=8 cino=>4,n-2,{2,^-2,t0,(0,u0,w1,M1 : */
This page took 2.153464 seconds and 5 git commands to generate.