]> sourceware.org Git - systemtap.git/blame - tapsets.cxx
PR10538: Use {...} for naming anonymous types
[systemtap.git] / tapsets.cxx
CommitLineData
56e12059 1// tapset resolution
12b44fb3 2// Copyright (C) 2005-2009 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"
bd2b1e68 24
3b579393
FCE
25#include <cstdlib>
26#include <algorithm>
bd2b1e68 27#include <deque>
56e12059 28#include <iostream>
bd2b1e68 29#include <map>
ec4373ff 30#include <set>
56e12059 31#include <sstream>
bd2b1e68 32#include <stdexcept>
b55bc428 33#include <vector>
e36387d7 34#include <cstdarg>
29e64872 35#include <cassert>
1969b5bc 36#include <iomanip>
f781f849 37#include <cerrno>
bd2b1e68
GH
38
39extern "C" {
df8fadee 40#include <fcntl.h>
bd2b1e68 41#include <elfutils/libdwfl.h>
7a053d3b 42#include <elfutils/libdw.h>
77de5e9e
GH
43#include <dwarf.h>
44#include <elf.h>
45#include <obstack.h>
b20febf3 46#include <glob.h>
30a279be 47#include <fnmatch.h>
5f0a03a6 48#include <stdio.h>
349dc70e 49#include <sys/types.h>
4b1ad75e
RM
50
51#define __STDC_FORMAT_MACROS
52#include <inttypes.h>
bd2b1e68 53}
77de5e9e 54
56e12059
FCE
55
56using namespace std;
2171f774 57using namespace __gnu_cxx;
56e12059 58
47dd066d 59
b20febf3
FCE
60
61// ------------------------------------------------------------------------
62void
a58d79d0 63common_probe_entryfn_prologue (translator_output* o, string statestr,
c12d974f 64 string new_pp,
912e8c59 65 bool overload_processing)
b20febf3 66{
72d18b98 67 o->newline() << "struct context* __restrict__ c;";
e0a17418
JS
68 o->newline() << "#if !INTERRUPTIBLE";
69 o->newline() << "unsigned long flags;";
70 o->newline() << "#endif";
b20febf3 71
a58d79d0
DS
72 if (overload_processing)
73 o->newline() << "#if defined(STP_TIMING) || defined(STP_OVERLOAD)";
74 else
75 o->newline() << "#ifdef STP_TIMING";
76 o->newline() << "cycles_t cycles_atstart = get_cycles ();";
b20febf3 77 o->newline() << "#endif";
b20febf3 78
e0a17418
JS
79 o->newline() << "#if INTERRUPTIBLE";
80 o->newline() << "preempt_disable ();";
81 o->newline() << "#else";
82 o->newline() << "local_irq_save (flags);";
83 o->newline() << "#endif";
b20febf3 84
c931ec8a 85 // Check for enough free enough stack space
d05a1d00 86 o->newline() << "if (unlikely ((((unsigned long) (& c)) & (THREAD_SIZE-1))"; // free space
a63401b1 87 o->newline(1) << "< (MINSTACKSPACE + sizeof (struct thread_info)))) {"; // needed space
d05a1d00
FCE
88 // XXX: may need porting to platforms where task_struct is not at bottom of kernel stack
89 // NB: see also CONFIG_DEBUG_STACKOVERFLOW
b3c3ca7c
FCE
90 o->newline() << "atomic_inc (& skipped_count);";
91 o->newline() << "#ifdef STP_TIMING";
92 o->newline() << "atomic_inc (& skipped_count_lowstack);";
93 o->newline() << "#endif";
c931ec8a
FCE
94 o->newline() << "goto probe_epilogue;";
95 o->newline(-1) << "}";
96
b20febf3
FCE
97 o->newline() << "if (atomic_read (&session_state) != " << statestr << ")";
98 o->newline(1) << "goto probe_epilogue;";
99 o->indent(-1);
9a604fac 100
a44a0785 101 o->newline() << "c = per_cpu_ptr (contexts, smp_processor_id());";
b3c3ca7c 102 o->newline() << "if (atomic_inc_return (& c->busy) != 1) {";
9c736061
FCE
103 o->newline(1) << "#if !INTERRUPTIBLE";
104 o->newline() << "atomic_inc (& skipped_count);";
105 o->newline() << "#endif";
b3c3ca7c
FCE
106 o->newline() << "#ifdef STP_TIMING";
107 o->newline() << "atomic_inc (& skipped_count_reentrant);";
c12d974f
FCE
108 o->newline() << "#ifdef DEBUG_REENTRANCY";
109 o->newline() << "_stp_warn (\"Skipped %s due to %s residency on cpu %u\\n\", "
110 << new_pp << ", c->probe_point ?: \"?\", smp_processor_id());";
111 // NB: There is a conceivable race condition here with reading
112 // c->probe_point, knowing that this other probe is sort of running.
113 // However, in reality, it's interrupted. Plus even if it were able
114 // to somehow start again, and stop before we read c->probe_point,
115 // at least we have that ?: "?" bit in there to avoid a NULL deref.
116 o->newline() << "#endif";
b3c3ca7c 117 o->newline() << "#endif";
9a604fac 118 o->newline() << "atomic_dec (& c->busy);";
b20febf3 119 o->newline() << "goto probe_epilogue;";
9a604fac
FCE
120 o->newline(-1) << "}";
121 o->newline();
1e00cfb1 122 o->newline() << "c->last_stmt = 0;";
9a604fac 123 o->newline() << "c->last_error = 0;";
a7ed0d3e 124 o->newline() << "c->nesting = -1;"; // NB: PR10516 packs locals[] tighter
22f8b401 125 o->newline() << "c->regs = 0;";
b916df9c 126 o->newline() << "c->unwaddr = 0;";
c12d974f 127 o->newline() << "c->probe_point = " << new_pp << ";";
b916df9c 128 // reset unwound address cache
fcff848e 129 o->newline() << "c->pi = 0;";
9addf322 130 o->newline() << "c->regparm = 0;";
bc54e71c
MH
131 o->newline() << "c->marker_name = NULL;";
132 o->newline() << "c->marker_format = NULL;";
e0a17418
JS
133
134 o->newline() << "#if INTERRUPTIBLE";
135 o->newline() << "c->actionremaining = MAXACTION_INTERRUPTIBLE;";
136 o->newline() << "#else";
137 o->newline() << "c->actionremaining = MAXACTION;";
138 o->newline() << "#endif";
dbb68664
FCE
139 o->newline() << "#ifdef STP_TIMING";
140 o->newline() << "c->statp = 0;";
141 o->newline() << "#endif";
9915575b
FCE
142 // NB: The following would actually be incorrect.
143 // That's because cycles_sum/cycles_base values are supposed to survive
144 // between consecutive probes. Periodically (STP_OVERLOAD_INTERVAL
145 // cycles), the values will be reset.
146 /*
f0e6dc63
FCE
147 o->newline() << "#ifdef STP_OVERLOAD";
148 o->newline() << "c->cycles_sum = 0;";
149 o->newline() << "c->cycles_base = 0;";
41c262f3 150 o->newline() << "#endif";
9915575b 151 */
b20febf3 152}
9a604fac 153
a44a0785 154
b20febf3 155void
a58d79d0 156common_probe_entryfn_epilogue (translator_output* o,
912e8c59 157 bool overload_processing)
b20febf3 158{
a58d79d0
DS
159 if (overload_processing)
160 o->newline() << "#if defined(STP_TIMING) || defined(STP_OVERLOAD)";
161 else
162 o->newline() << "#ifdef STP_TIMING";
dbb68664 163 o->newline() << "{";
a58d79d0
DS
164 o->newline(1) << "cycles_t cycles_atend = get_cycles ();";
165 // NB: we truncate cycles counts to 32 bits. Perhaps it should be
166 // fewer, if the hardware counter rolls over really quickly. We
167 // handle 32-bit wraparound here.
168 o->newline() << "int32_t cycles_elapsed = ((int32_t)cycles_atend > (int32_t)cycles_atstart)";
169 o->newline(1) << "? ((int32_t)cycles_atend - (int32_t)cycles_atstart)";
170 o->newline() << ": (~(int32_t)0) - (int32_t)cycles_atstart + (int32_t)cycles_atend + 1;";
171 o->indent(-1);
dbb68664 172
a58d79d0 173 o->newline() << "#ifdef STP_TIMING";
dbb68664 174 o->newline() << "if (likely (c->statp)) _stp_stat_add(*c->statp, cycles_elapsed);";
a58d79d0
DS
175 o->newline() << "#endif";
176
177 if (overload_processing)
178 {
179 o->newline() << "#ifdef STP_OVERLOAD";
180 o->newline() << "{";
181 // If the cycle count has wrapped (cycles_atend > cycles_base),
182 // let's go ahead and pretend the interval has been reached.
183 // This should reset cycles_base and cycles_sum.
184 o->newline(1) << "cycles_t interval = (cycles_atend > c->cycles_base)";
185 o->newline(1) << "? (cycles_atend - c->cycles_base)";
186 o->newline() << ": (STP_OVERLOAD_INTERVAL + 1);";
187 o->newline(-1) << "c->cycles_sum += cycles_elapsed;";
188
189 // If we've spent more than STP_OVERLOAD_THRESHOLD cycles in a
190 // probe during the last STP_OVERLOAD_INTERVAL cycles, the probe
191 // has overloaded the system and we need to quit.
192 o->newline() << "if (interval > STP_OVERLOAD_INTERVAL) {";
193 o->newline(1) << "if (c->cycles_sum > STP_OVERLOAD_THRESHOLD) {";
194 o->newline(1) << "_stp_error (\"probe overhead exceeded threshold\");";
195 o->newline() << "atomic_set (&session_state, STAP_SESSION_ERROR);";
551e9f14 196 o->newline() << "atomic_inc (&error_count);";
a58d79d0 197 o->newline(-1) << "}";
e57b735a 198
a58d79d0
DS
199 o->newline() << "c->cycles_base = cycles_atend;";
200 o->newline() << "c->cycles_sum = 0;";
201 o->newline(-1) << "}";
202 o->newline(-1) << "}";
203 o->newline() << "#endif";
204 }
e57b735a 205
440f755a
JS
206 o->newline(-1) << "}";
207 o->newline() << "#endif";
e57b735a 208
440f755a
JS
209 o->newline() << "c->probe_point = 0;"; // vacated
210 o->newline() << "if (unlikely (c->last_error && c->last_error[0])) {";
211 o->newline(1) << "if (c->last_stmt != NULL)";
212 o->newline(1) << "_stp_softerror (\"%s near %s\", c->last_error, c->last_stmt);";
213 o->newline(-1) << "else";
214 o->newline(1) << "_stp_softerror (\"%s\", c->last_error);";
215 o->indent(-1);
216 o->newline() << "atomic_inc (& error_count);";
217 o->newline() << "if (atomic_read (& error_count) > MAXERRORS) {";
218 o->newline(1) << "atomic_set (& session_state, STAP_SESSION_ERROR);";
219 o->newline() << "_stp_exit ();";
220 o->newline(-1) << "}";
221 o->newline(-1) << "}";
222 o->newline() << "atomic_dec (&c->busy);";
e57b735a 223
440f755a
JS
224 o->newline(-1) << "probe_epilogue:"; // context is free
225 o->indent(1);
e57b735a 226
440f755a
JS
227 // Check for excessive skip counts.
228 o->newline() << "if (unlikely (atomic_read (& skipped_count) > MAXSKIPPED)) {";
229 o->newline(1) << "atomic_set (& session_state, STAP_SESSION_ERROR);";
230 o->newline() << "_stp_exit ();";
231 o->newline(-1) << "}";
e57b735a 232
440f755a
JS
233 o->newline() << "#if INTERRUPTIBLE";
234 o->newline() << "preempt_enable_no_resched ();";
235 o->newline() << "#else";
236 o->newline() << "local_irq_restore (flags);";
237 o->newline() << "#endif";
238}
e57b735a 239
e57b735a 240
440f755a 241// ------------------------------------------------------------------------
e57b735a 242
440f755a
JS
243// ------------------------------------------------------------------------
244// Dwarf derived probes. "We apologize for the inconvience."
245// ------------------------------------------------------------------------
e57b735a 246
4627ed58
JS
247static const string TOK_KERNEL("kernel");
248static const string TOK_MODULE("module");
249static const string TOK_FUNCTION("function");
250static const string TOK_INLINE("inline");
251static const string TOK_CALL("call");
252static const string TOK_RETURN("return");
253static const string TOK_MAXACTIVE("maxactive");
254static const string TOK_STATEMENT("statement");
255static const string TOK_ABSOLUTE("absolute");
256static const string TOK_PROCESS("process");
257static const string TOK_MARK("mark");
258static const string TOK_TRACE("trace");
259static const string TOK_LABEL("label");
e57b735a 260
1adf8ef1 261static int query_cu (Dwarf_Die * cudie, void * arg);
e57b735a 262
440f755a
JS
263// Can we handle this query with just symbol-table info?
264enum dbinfo_reqt
265{
266 dbr_unknown,
267 dbr_none, // kernel.statement(NUM).absolute
268 dbr_need_symtab, // can get by with symbol table if there's no dwarf
269 dbr_need_dwarf
270};
e57b735a 271
20e4a32c 272
440f755a
JS
273struct base_query; // forward decls
274struct dwarf_query;
275struct dwflpp;
276struct symbol_table;
20e4a32c 277
a781f401 278
440f755a
JS
279struct
280symbol_table
281{
282 module_info *mod_info; // associated module
283 map<string, func_info*> map_by_name;
1c6b77e5
JS
284 multimap<Dwarf_Addr, func_info*> map_by_addr;
285 typedef multimap<Dwarf_Addr, func_info*>::iterator iterator_t;
440f755a
JS
286 typedef pair<iterator_t, iterator_t> range_t;
287#ifdef __powerpc__
288 GElf_Word opd_section;
289#endif
440f755a
JS
290 void add_symbol(const char *name, bool weak, Dwarf_Addr addr,
291 Dwarf_Addr *high_addr);
440f755a 292 enum info_status read_symbols(FILE *f, const string& path);
83ca3872
MW
293 enum info_status read_from_elf_file(const string& path,
294 const systemtap_session &sess);
295 enum info_status read_from_text_file(const string& path,
296 const systemtap_session &sess);
440f755a
JS
297 enum info_status get_from_elf();
298 void prepare_section_rejection(Dwfl_Module *mod);
299 bool reject_section(GElf_Word section);
440f755a
JS
300 void purge_syscall_stubs();
301 func_info *lookup_symbol(const string& name);
302 Dwarf_Addr lookup_symbol_address(const string& name);
303 func_info *get_func_containing_address(Dwarf_Addr addr);
7a053d3b 304
440f755a
JS
305 symbol_table(module_info *mi) : mod_info(mi) {}
306 ~symbol_table();
307};
77de5e9e 308
440f755a
JS
309static bool null_die(Dwarf_Die *die)
310{
311 static Dwarf_Die null = { 0 };
312 return (!die || !memcmp(die, &null, sizeof(null)));
313}
c4ce66a1
JS
314
315
7a053d3b 316enum
bd2b1e68 317function_spec_type
7a053d3b 318 {
bd2b1e68
GH
319 function_alone,
320 function_and_file,
7a053d3b 321 function_file_and_line
bd2b1e68
GH
322 };
323
ec4373ff 324
bd2b1e68 325struct dwarf_builder;
77de5e9e 326
2930abc7 327
b20febf3
FCE
328// XXX: This class is a candidate for subclassing to separate
329// the relocation vs non-relocation variants. Likewise for
330// kprobe vs kretprobe variants.
331
332struct dwarf_derived_probe: public derived_probe
b55bc428 333{
b20febf3
FCE
334 dwarf_derived_probe (const string& function,
335 const string& filename,
336 int line,
337 const string& module,
338 const string& section,
339 Dwarf_Addr dwfl_addr,
2930abc7 340 Dwarf_Addr addr,
b20febf3
FCE
341 dwarf_query & q,
342 Dwarf_Die* scope_die);
20e4a32c 343
b20febf3
FCE
344 string module;
345 string section;
346 Dwarf_Addr addr;
2930abc7 347 bool has_return;
c9bad430
DS
348 bool has_maxactive;
349 long maxactive_val;
b95e2b79 350 bool access_vars;
2930abc7 351
b8da0ad1 352 void printsig (std::ostream &o) const;
b20febf3 353 void join_group (systemtap_session& s);
9020300d
FCE
354 void emit_probe_local_init(translator_output * o);
355
0a98fd42
JS
356 string args;
357 void saveargs(Dwarf_Die* scope_die);
358 void printargs(std::ostream &o) const;
359
bd2b1e68 360 // Pattern registration helpers.
7a053d3b 361 static void register_statement_variants(match_node * root,
88e8da38 362 dwarf_builder * dw,
bbedb0a6 363 bool unprivileged_ok_p = false);
fd6602a0 364 static void register_function_variants(match_node * root,
88e8da38 365 dwarf_builder * dw,
bbedb0a6 366 bool unprivileged_ok_p = false);
7a053d3b 367 static void register_function_and_statement_variants(match_node * root,
88e8da38 368 dwarf_builder * dw,
bbedb0a6 369 bool unprivileged_ok_p = false);
c4ce66a1 370 static void register_patterns(systemtap_session& s);
20c6c071
GH
371};
372
dc38c0ae 373
6d0f3f0c
FCE
374struct uprobe_derived_probe: public derived_probe
375{
376 bool return_p;
377 string module; // * => unrestricted
378 int pid; // 0 => unrestricted
379 string section; // empty => absolute address
380 Dwarf_Addr address;
381 // bool has_maxactive;
382 // long maxactive_val;
0973d815 383
6d0f3f0c
FCE
384 uprobe_derived_probe (const string& function,
385 const string& filename,
386 int line,
387 const string& module,
388 int pid,
389 const string& section,
390 Dwarf_Addr dwfl_addr,
391 Dwarf_Addr addr,
392 dwarf_query & q,
393 Dwarf_Die* scope_die);
394
0973d815
FCE
395 // alternate constructor for process(PID).statement(ADDR).absolute
396 uprobe_derived_probe (probe *base,
397 probe_point *location,
398 int pid,
399 Dwarf_Addr addr,
400 bool return_p);
401
9ace370f
JS
402 string args;
403 void saveargs(Dwarf_Die* scope_die);
404 void printargs(std::ostream &o) const;
405
6d0f3f0c
FCE
406 void printsig (std::ostream &o) const;
407 void join_group (systemtap_session& s);
408};
409
dc38c0ae
DS
410struct dwarf_derived_probe_group: public derived_probe_group
411{
412private:
b20febf3
FCE
413 multimap<string,dwarf_derived_probe*> probes_by_module;
414 typedef multimap<string,dwarf_derived_probe*>::iterator p_b_m_iterator;
dc38c0ae
DS
415
416public:
b20febf3
FCE
417 void enroll (dwarf_derived_probe* probe);
418 void emit_module_decls (systemtap_session& s);
419 void emit_module_init (systemtap_session& s);
420 void emit_module_exit (systemtap_session& s);
dc38c0ae
DS
421};
422
423
20c6c071 424// Helper struct to thread through the dwfl callbacks.
2c384610 425struct base_query
20c6c071 426{
c4ce66a1
JS
427 base_query(dwflpp & dw, literal_map_t const & params);
428 base_query(dwflpp & dw, const string & module_val);
2c384610 429 virtual ~base_query() {}
bd2b1e68 430
5227f1ea 431 systemtap_session & sess;
2c384610 432 dwflpp & dw;
5227f1ea 433
bd2b1e68 434 // Parameter extractors.
86bf665e 435 static bool has_null_param(literal_map_t const & params,
888af770 436 string const & k);
86bf665e 437 static bool get_string_param(literal_map_t const & params,
bd2b1e68 438 string const & k, string & v);
86bf665e 439 static bool get_number_param(literal_map_t const & params,
bd2b1e68 440 string const & k, long & v);
86bf665e 441 static bool get_number_param(literal_map_t const & params,
c239d28c 442 string const & k, Dwarf_Addr & v);
b55bc428 443
2c384610
DS
444 // Extracted parameters.
445 bool has_kernel;
91af0778
FCE
446 bool has_module;
447 bool has_process;
2c384610
DS
448 string module_val; // has_kernel => module_val = "kernel"
449
450 virtual void handle_query_module() = 0;
451};
452
453
c4ce66a1
JS
454base_query::base_query(dwflpp & dw, literal_map_t const & params):
455 sess(dw.sess), dw(dw)
2c384610 456{
91af0778 457 has_kernel = has_null_param (params, TOK_KERNEL);
2c384610
DS
458 if (has_kernel)
459 module_val = "kernel";
91af0778
FCE
460
461 has_module = get_string_param (params, TOK_MODULE, module_val);
462 if (has_module)
463 has_process = false;
4baf0e53 464 else
d0a7f5a9
FCE
465 {
466 has_process = get_string_param(params, TOK_PROCESS, module_val);
06aca46a 467 if (has_process)
d0a7f5a9
FCE
468 module_val = find_executable (module_val);
469 }
91af0778
FCE
470
471 assert (has_kernel || has_process || has_module);
2c384610
DS
472}
473
c4ce66a1
JS
474base_query::base_query(dwflpp & dw, const string & module_val)
475 : sess(dw.sess), dw(dw), module_val(module_val)
476{
477 // NB: This uses '/' to distinguish between kernel modules and userspace,
478 // which means that userspace modules won't get any PATH searching.
479 if (module_val.find('/') == string::npos)
480 {
481 has_kernel = (module_val == TOK_KERNEL);
482 has_module = !has_kernel;
483 has_process = false;
484 }
485 else
486 {
487 has_kernel = has_module = false;
488 has_process = true;
489 }
490}
491
2c384610 492bool
86bf665e 493base_query::has_null_param(literal_map_t const & params,
2c384610
DS
494 string const & k)
495{
888af770 496 return derived_probe_builder::has_null_param(params, k);
2c384610
DS
497}
498
499
500bool
86bf665e 501base_query::get_string_param(literal_map_t const & params,
2c384610
DS
502 string const & k, string & v)
503{
504 return derived_probe_builder::get_param (params, k, v);
505}
506
507
508bool
86bf665e 509base_query::get_number_param(literal_map_t const & params,
2c384610
DS
510 string const & k, long & v)
511{
512 int64_t value;
513 bool present = derived_probe_builder::get_param (params, k, value);
514 v = (long) value;
515 return present;
516}
517
518
519bool
86bf665e 520base_query::get_number_param(literal_map_t const & params,
2c384610
DS
521 string const & k, Dwarf_Addr & v)
522{
523 int64_t value;
524 bool present = derived_probe_builder::get_param (params, k, value);
525 v = (Dwarf_Addr) value;
526 return present;
527}
528
2c384610
DS
529struct dwarf_query : public base_query
530{
e1278bd4 531 dwarf_query(probe * base_probe,
2c384610
DS
532 probe_point * base_loc,
533 dwflpp & dw,
86bf665e 534 literal_map_t const & params,
2c384610
DS
535 vector<derived_probe *> & results);
536
c4ce66a1
JS
537 vector<derived_probe *> & results;
538 probe * base_probe;
539 probe_point * base_loc;
540
2c384610 541 virtual void handle_query_module();
5f0a03a6
JK
542 void query_module_dwarf();
543 void query_module_symtab();
2c384610 544
2930abc7
FCE
545 void add_probe_point(string const & funcname,
546 char const * filename,
547 int line,
548 Dwarf_Die *scope_die,
549 Dwarf_Addr addr);
36f9dd1d 550
857bdfd1
JS
551 // Track addresses we've already seen in a given module
552 set<Dwarf_Addr> alias_dupes;
553
2930abc7 554 // Extracted parameters.
7a053d3b 555 string function_val;
20c6c071
GH
556
557 bool has_function_str;
558 bool has_statement_str;
559 bool has_function_num;
560 bool has_statement_num;
7a053d3b
RM
561 string statement_str_val;
562 string function_str_val;
c239d28c
GH
563 Dwarf_Addr statement_num_val;
564 Dwarf_Addr function_num_val;
20c6c071 565
b8da0ad1
FCE
566 bool has_call;
567 bool has_inline;
20c6c071
GH
568 bool has_return;
569
c9bad430
DS
570 bool has_maxactive;
571 long maxactive_val;
572
20c6c071
GH
573 bool has_label;
574 string label_val;
575
576 bool has_relative;
577 long relative_val;
578
37ebca01
FCE
579 bool has_absolute;
580
467bea43
SC
581 bool has_mark;
582
5f0a03a6
JK
583 enum dbinfo_reqt dbinfo_reqt;
584 enum dbinfo_reqt assess_dbinfo_reqt();
585
20c6c071
GH
586 function_spec_type parse_function_spec(string & spec);
587 function_spec_type spec_type;
588 string function;
589 string file;
0c8b7d37 590 line_t line_type;
879eb9e9 591 int line[2];
5f0a03a6 592 bool query_done; // Found exact match
20c6c071 593
7e1279ea
FCE
594 set<char const *> filtered_srcfiles;
595
596 // Map official entrypc -> func_info object
86bf665e
TM
597 inline_instance_map_t filtered_inlines;
598 func_info_map_t filtered_functions;
7e1279ea
FCE
599 bool choose_next_line;
600 Dwarf_Addr entrypc_for_next_line;
b55bc428
FCE
601};
602
98afd80e
FCE
603
604struct dwarf_builder: public derived_probe_builder
b55bc428 605{
665e1256 606 map <string,dwflpp*> kern_dw; /* NB: key string could be a wildcard */
7a24d422 607 map <string,dwflpp*> user_dw;
ae2552da 608 dwarf_builder() {}
aa30ccd3 609
ae2552da 610 dwflpp *get_kern_dw(systemtap_session& sess, const string& module)
707bf35e 611 {
ea14cf67
FCE
612 if (kern_dw[module] == 0)
613 kern_dw[module] = new dwflpp(sess, module, true); // might throw
ae2552da 614 return kern_dw[module];
707bf35e
JS
615 }
616
617 dwflpp *get_user_dw(systemtap_session& sess, const string& module)
618 {
ea14cf67
FCE
619 if (user_dw[module] == 0)
620 user_dw[module] = new dwflpp(sess, module, false); // might throw
707bf35e
JS
621 return user_dw[module];
622 }
7a24d422
FCE
623
624 /* NB: not virtual, so can be called from dtor too: */
06aca46a 625 void dwarf_build_no_more (bool verbose)
aa30ccd3 626 {
ae2552da
FCE
627 for (map<string,dwflpp*>::iterator udi = kern_dw.begin();
628 udi != kern_dw.end();
629 udi ++)
aa30ccd3 630 {
7a24d422 631 if (verbose)
ae2552da
FCE
632 clog << "dwarf_builder releasing kernel dwflpp " << udi->first << endl;
633 delete udi->second;
aa30ccd3 634 }
ae2552da 635 kern_dw.erase (kern_dw.begin(), kern_dw.end());
7a24d422
FCE
636
637 for (map<string,dwflpp*>::iterator udi = user_dw.begin();
638 udi != user_dw.end();
639 udi ++)
640 {
641 if (verbose)
642 clog << "dwarf_builder releasing user dwflpp " << udi->first << endl;
643 delete udi->second;
644 }
645 user_dw.erase (user_dw.begin(), user_dw.end());
646 }
647
648 void build_no_more (systemtap_session &s)
649 {
650 dwarf_build_no_more (s.verbose > 3);
aa30ccd3
FCE
651 }
652
e38d6504
RM
653 ~dwarf_builder()
654 {
7a24d422 655 dwarf_build_no_more (false);
c8959a29 656 }
aa30ccd3 657
5227f1ea 658 virtual void build(systemtap_session & sess,
7a053d3b 659 probe * base,
20c6c071 660 probe_point * location,
86bf665e 661 literal_map_t const & parameters,
20c6c071 662 vector<derived_probe *> & finished_results);
b55bc428
FCE
663};
664
5111fc3e 665
e1278bd4 666dwarf_query::dwarf_query(probe * base_probe,
20c6c071
GH
667 probe_point * base_loc,
668 dwflpp & dw,
86bf665e 669 literal_map_t const & params,
20c6c071 670 vector<derived_probe *> & results)
c4ce66a1
JS
671 : base_query(dw, params), results(results),
672 base_probe(base_probe), base_loc(base_loc)
bd2b1e68
GH
673{
674 // Reduce the query to more reasonable semantic values (booleans,
675 // extracted strings, numbers, etc).
bd2b1e68
GH
676 has_function_str = get_string_param(params, TOK_FUNCTION, function_str_val);
677 has_function_num = get_number_param(params, TOK_FUNCTION, function_num_val);
678
679 has_statement_str = get_string_param(params, TOK_STATEMENT, statement_str_val);
680 has_statement_num = get_number_param(params, TOK_STATEMENT, statement_num_val);
681
0f336e95
SC
682 has_label = get_string_param(params, TOK_LABEL, label_val);
683
b8da0ad1
FCE
684 has_call = has_null_param(params, TOK_CALL);
685 has_inline = has_null_param(params, TOK_INLINE);
bd2b1e68 686 has_return = has_null_param(params, TOK_RETURN);
c9bad430 687 has_maxactive = get_number_param(params, TOK_MAXACTIVE, maxactive_val);
37ebca01 688 has_absolute = has_null_param(params, TOK_ABSOLUTE);
467bea43 689 has_mark = false;
37ebca01 690
bd2b1e68
GH
691 if (has_function_str)
692 spec_type = parse_function_spec(function_str_val);
693 else if (has_statement_str)
694 spec_type = parse_function_spec(statement_str_val);
0daad364 695
5f0a03a6
JK
696 dbinfo_reqt = assess_dbinfo_reqt();
697 query_done = false;
0daad364
JS
698}
699
700
440f755a
JS
701func_info_map_t *
702get_filtered_functions(dwarf_query *q)
703{
704 return &q->filtered_functions;
705}
706
707
708inline_instance_map_t *
709get_filtered_inlines(dwarf_query *q)
710{
711 return &q->filtered_inlines;
712}
713
714
715void
716add_label_name(dwarf_query *q, const char *name)
717{
718 // this is a kludge to let the listing mode show labels to the user
719 if (q->sess.listing_mode)
720 q->results.back()->locations[0]->components.push_back
721 (new probe_point::component(TOK_LABEL, new literal_string (name)));
722}
723
724
2c384610 725void
5f0a03a6 726dwarf_query::query_module_dwarf()
2c384610
DS
727{
728 if (has_function_num || has_statement_num)
729 {
730 // If we have module("foo").function(0xbeef) or
731 // module("foo").statement(0xbeef), the address is relative
732 // to the start of the module, so we seek the function
733 // number plus the module's bias.
734
735 Dwarf_Addr addr;
736 if (has_function_num)
737 addr = function_num_val;
738 else
739 addr = statement_num_val;
4baf0e53 740
1f8592d1
MW
741 // Translate to an actual symbol address.
742 addr = dw.literal_addr_to_sym_addr (addr);
743
1adf8ef1
JS
744 Dwarf_Die* cudie = dw.query_cu_containing_address(addr);
745 if (cudie) // address could be wildly out of range
746 query_cu(cudie, this);
2c384610
DS
747 }
748 else
749 {
750 // Otherwise if we have a function("foo") or statement("foo")
751 // specifier, we have to scan over all the CUs looking for
752 // the function(s) in question
753 assert(has_function_str || has_statement_str);
754 dw.iterate_over_cus(&query_cu, this);
755 }
756}
757
5f0a03a6
JK
758static void query_func_info (Dwarf_Addr entrypc, func_info & fi,
759 dwarf_query * q);
760
761void
762dwarf_query::query_module_symtab()
763{
764 // Get the symbol table if it's necessary, sufficient, and not already got.
765 if (dbinfo_reqt == dbr_need_dwarf)
766 return;
767
768 module_info *mi = dw.mod_info;
769 if (dbinfo_reqt == dbr_need_symtab)
770 {
771 if (mi->symtab_status == info_unknown)
772 mi->get_symtab(this);
773 if (mi->symtab_status == info_absent)
774 return;
775 }
776
777 func_info *fi = NULL;
778 symbol_table *sym_table = mi->sym_table;
779
780 if (has_function_str)
781 {
782 // Per dwarf_query::assess_dbinfo_reqt()...
783 assert(spec_type == function_alone);
784 if (dw.name_has_wildcard(function_str_val))
785 {
786 // Until we augment the blacklist sufficently...
787 if (function_str_val.find_first_not_of("*?") == string::npos)
788 {
789 // e.g., kernel.function("*")
790 cerr << "Error: Pattern '"
791 << function_str_val
792 << "' matches every instruction address in the symbol table,"
793 << endl
794 << "some of which aren't even functions."
795 << " Please be more precise."
796 << endl;
797 return;
798 }
2e67a43b 799 symbol_table::iterator_t iter;
1c6b77e5
JS
800 for (iter = sym_table->map_by_addr.begin();
801 iter != sym_table->map_by_addr.end();
2e67a43b 802 ++iter)
5f0a03a6 803 {
1c6b77e5 804 fi = iter->second;
5f0a03a6
JK
805 if (!null_die(&fi->die))
806 continue; // already handled in query_module_dwarf()
807 if (dw.function_name_matches_pattern(fi->name, function_str_val))
808 query_func_info(fi->addr, *fi, this);
809 }
810 }
811 else
812 {
813 fi = sym_table->lookup_symbol(function_str_val);
814 if (fi && null_die(&fi->die))
815 query_func_info(fi->addr, *fi, this);
816 }
817 }
818 else
819 {
820 assert(has_function_num || has_statement_num);
821 // Find the "function" in which the indicated address resides.
822 Dwarf_Addr addr =
823 (has_function_num ? function_num_val : statement_num_val);
824 fi = sym_table->get_func_containing_address(addr);
825 if (!fi)
826 {
83ca3872
MW
827 if (! sess.suppress_warnings)
828 cerr << "Warning: address "
829 << hex << addr << dec
830 << " out of range for module "
831 << dw.module_name;
5f0a03a6
JK
832 return;
833 }
834 if (!null_die(&fi->die))
835 {
836 // addr looks like it's in the compilation unit containing
837 // the indicated function, but query_module_dwarf() didn't
838 // match addr to any compilation unit, so addr must be
839 // above that cu's address range.
83ca3872
MW
840 if (! sess.suppress_warnings)
841 cerr << "Warning: address "
842 << hex << addr << dec
843 << " maps to no known compilation unit in module "
844 << dw.module_name;
5f0a03a6
JK
845 return;
846 }
847 query_func_info(fi->addr, *fi, this);
848 }
849}
850
851void
852dwarf_query::handle_query_module()
853{
1c6b77e5
JS
854 bool report = dbinfo_reqt == dbr_need_dwarf || !sess.consult_symtab;
855 dw.get_module_dwarf(false, report);
856
857 // prebuild the symbol table to resolve aliases
858 dw.mod_info->get_symtab(this);
859
857bdfd1
JS
860 // reset the dupe-checking for each new module
861 alias_dupes.clear();
862
5f0a03a6
JK
863 if (dw.mod_info->dwarf_status == info_present)
864 query_module_dwarf();
1c6b77e5 865
5f0a03a6
JK
866 // Consult the symbol table if we haven't found all we're looking for.
867 // asm functions can show up in the symbol table but not in dwarf.
868 if (sess.consult_symtab && !query_done)
869 query_module_symtab();
870}
871
2c384610 872
bd2b1e68 873function_spec_type
20c6c071 874dwarf_query::parse_function_spec(string & spec)
bd2b1e68
GH
875{
876 string::const_iterator i = spec.begin(), e = spec.end();
7a053d3b 877
bd2b1e68
GH
878 function.clear();
879 file.clear();
879eb9e9
SC
880 line[0] = 0;
881 line[1] = 0;
bd2b1e68
GH
882
883 while (i != e && *i != '@')
884 {
0c8b7d37 885 if (*i == ':' || *i == '+')
bd2b1e68
GH
886 goto bad;
887 function += *i++;
888 }
889
890 if (i == e)
891 {
b0ee93c4 892 if (sess.verbose>2)
7a053d3b
RM
893 clog << "parsed '" << spec
894 << "' -> func '" << function
db22e55f 895 << "'\n";
bd2b1e68
GH
896 return function_alone;
897 }
898
899 if (i++ == e)
900 goto bad;
901
0c8b7d37 902 while (i != e && *i != ':' && *i != '+')
bd2b1e68 903 file += *i++;
41c262f3 904 if (*i == ':')
879eb9e9
SC
905 {
906 if (*(i + 1) == '*')
907 line_type = WILDCARD;
908 else
909 line_type = ABSOLUTE;
910 }
0c8b7d37
SC
911 else if (*i == '+')
912 line_type = RELATIVE;
7a053d3b 913
bd2b1e68
GH
914 if (i == e)
915 {
b0ee93c4 916 if (sess.verbose>2)
7a053d3b
RM
917 clog << "parsed '" << spec
918 << "' -> func '"<< function
919 << "', file '" << file
db22e55f 920 << "'\n";
bd2b1e68
GH
921 return function_and_file;
922 }
923
924 if (i++ == e)
925 goto bad;
926
927 try
928 {
879eb9e9
SC
929 if (line_type != WILDCARD)
930 {
931 string::const_iterator dash = i;
41c262f3 932
879eb9e9
SC
933 while (dash != e && *dash != '-')
934 dash++;
935 if (dash == e)
936 line[0] = line[1] = lex_cast<int>(string(i, e));
937 else
938 {
939 line_type = RANGE;
940 line[0] = lex_cast<int>(string(i, dash));
941 line[1] = lex_cast<int>(string(dash + 1, e));
942 }
943 }
41c262f3 944
b0ee93c4 945 if (sess.verbose>2)
7a053d3b
RM
946 clog << "parsed '" << spec
947 << "' -> func '"<< function
948 << "', file '" << file
db22e55f 949 << "', line " << line << "\n";
bd2b1e68
GH
950 return function_file_and_line;
951 }
952 catch (runtime_error & exn)
953 {
954 goto bad;
955 }
956
957 bad:
7a053d3b 958 throw semantic_error("malformed specification '" + spec + "'",
20c6c071 959 base_probe->tok);
bd2b1e68
GH
960}
961
962
36f9dd1d 963void
b20febf3
FCE
964dwarf_query::add_probe_point(const string& funcname,
965 const char* filename,
36f9dd1d 966 int line,
b20febf3 967 Dwarf_Die* scope_die,
36f9dd1d
FCE
968 Dwarf_Addr addr)
969{
b20febf3 970 string reloc_section; // base section for relocation purposes
27646582 971 Dwarf_Addr reloc_addr; // relocated
b20febf3
FCE
972 string blacklist_section; // linking section for blacklist purposes
973 const string& module = dw.module_name; // "kernel" or other
36f9dd1d 974
37ebca01
FCE
975 assert (! has_absolute); // already handled in dwarf_builder::build()
976
27646582 977 reloc_addr = dw.relocate_address(addr, reloc_section, blacklist_section);
2930abc7 978
7f9f3386
FCE
979 if (sess.verbose > 1)
980 {
b20febf3
FCE
981 clog << "probe " << funcname << "@" << filename << ":" << line;
982 if (string(module) == TOK_KERNEL)
983 clog << " kernel";
91af0778 984 else if (has_module)
b20febf3 985 clog << " module=" << module;
91af0778
FCE
986 else if (has_process)
987 clog << " process=" << module;
b20febf3
FCE
988 if (reloc_section != "") clog << " reloc=" << reloc_section;
989 if (blacklist_section != "") clog << " section=" << blacklist_section;
990 clog << " pc=0x" << hex << addr << dec;
7f9f3386 991 }
4baf0e53 992
27646582
JS
993 bool bad = dw.blacklisted_p (funcname, filename, line, module,
994 blacklist_section, addr, has_return);
b20febf3
FCE
995 if (sess.verbose > 1)
996 clog << endl;
7f9f3386 997
84048984
FCE
998 if (module == TOK_KERNEL)
999 {
1000 // PR 4224: adapt to relocatable kernel by subtracting the _stext address here.
1001 reloc_addr = addr - sess.sym_stext;
37ebca01 1002 reloc_section = "_stext"; // a message to runtime's _stp_module_relocate
84048984
FCE
1003 }
1004
b20febf3
FCE
1005 if (! bad)
1006 {
1a0dbc5a 1007 sess.unwindsym_modules.insert (module);
6d0f3f0c
FCE
1008
1009 if (has_process)
1010 {
1011 results.push_back (new uprobe_derived_probe(funcname, filename, line,
1012 module, 0, reloc_section, addr, reloc_addr,
1013 *this, scope_die));
1014 }
1015 else
1016 {
1017 assert (has_kernel || has_module);
1018 results.push_back (new dwarf_derived_probe(funcname, filename, line,
06aca46a 1019 module, reloc_section, addr, reloc_addr,
6d0f3f0c
FCE
1020 *this, scope_die));
1021 }
b20febf3 1022 }
2930abc7
FCE
1023}
1024
5f0a03a6
JK
1025enum dbinfo_reqt
1026dwarf_query::assess_dbinfo_reqt()
1027{
1028 if (has_absolute)
1029 {
1030 // kernel.statement(NUM).absolute
1031 return dbr_none;
1032 }
1033 if (has_inline)
1034 {
1035 // kernel.function("f").inline or module("m").function("f").inline
1036 return dbr_need_dwarf;
1037 }
1038 if (has_function_str && spec_type == function_alone)
1039 {
1040 // kernel.function("f") or module("m").function("f")
1041 return dbr_need_symtab;
1042 }
1043 if (has_statement_num)
1044 {
1045 // kernel.statement(NUM) or module("m").statement(NUM)
1046 // Technically, all we need is the module offset (or _stext, for
1047 // the kernel). But for that we need either the ELF file or (for
1048 // _stext) the symbol table. In either case, the symbol table
1049 // is available, and that allows us to map the NUM (address)
1050 // to a function, which is goodness.
1051 return dbr_need_symtab;
1052 }
1053 if (has_function_num)
1054 {
1055 // kernel.function(NUM) or module("m").function(NUM)
1056 // Need the symbol table so we can back up from NUM to the
1057 // start of the function.
1058 return dbr_need_symtab;
1059 }
1060 // Symbol table tells us nothing about source files or line numbers.
1061 return dbr_need_dwarf;
1062}
2930abc7
FCE
1063
1064
b8da0ad1
FCE
1065// The critical determining factor when interpreting a pattern
1066// string is, perhaps surprisingly: "presence of a lineno". The
1067// presence of a lineno changes the search strategy completely.
1068//
1069// Compare the two cases:
1070//
1071// 1. {statement,function}(foo@file.c:lineno)
1072// - find the files matching file.c
1073// - in each file, find the functions matching foo
1074// - query the file for line records matching lineno
1075// - iterate over the line records,
1076// - and iterate over the functions,
1077// - if(haspc(function.DIE, line.addr))
1078// - if looking for statements: probe(lineno.addr)
1079// - if looking for functions: probe(function.{entrypc,return,etc.})
1080//
1081// 2. {statement,function}(foo@file.c)
1082// - find the files matching file.c
1083// - in each file, find the functions matching foo
1084// - probe(function.{entrypc,return,etc.})
1085//
1086// Thus the first decision we make is based on the presence of a
1087// lineno, and we enter entirely different sets of callbacks
1088// depending on that decision.
1089//
1090// Note that the first case is a generalization fo the second, in that
1091// we could theoretically search through line records for matching
1092// file names (a "table scan" in rdbms lingo). Luckily, file names
1093// are already cached elsewhere, so we can do an "index scan" as an
1094// optimization.
7e1279ea 1095
bd2b1e68 1096static void
4cd232e4 1097query_statement (string const & func,
20e4a32c 1098 char const * file,
4cd232e4 1099 int line,
bcc12710 1100 Dwarf_Die *scope_die,
20e4a32c 1101 Dwarf_Addr stmt_addr,
4cd232e4 1102 dwarf_query * q)
bd2b1e68 1103{
39bcd429
FCE
1104 try
1105 {
cee35f73 1106 q->add_probe_point(func, file ? file : "",
a9b2f3a5 1107 line, scope_die, stmt_addr);
39bcd429
FCE
1108 }
1109 catch (const semantic_error& e)
1110 {
1111 q->sess.print_error (e);
1112 }
bd2b1e68
GH
1113}
1114
7e1279ea 1115static void
3e961ba6 1116query_inline_instance_info (inline_instance_info & ii,
7e1279ea
FCE
1117 dwarf_query * q)
1118{
b6581717 1119 try
7e1279ea 1120 {
b6581717
GH
1121 if (q->has_return)
1122 {
1123 throw semantic_error ("cannot probe .return of inline function '" + ii.name + "'");
1124 }
1125 else
1126 {
b0ee93c4 1127 if (q->sess.verbose>2)
20e4a32c 1128 clog << "querying entrypc "
3e961ba6 1129 << hex << ii.entrypc << dec
db22e55f 1130 << " of instance of inline '" << ii.name << "'\n";
20e4a32c 1131 query_statement (ii.name, ii.decl_file, ii.decl_line,
3e961ba6 1132 &ii.die, ii.entrypc, q);
b6581717 1133 }
7e1279ea 1134 }
b6581717 1135 catch (semantic_error &e)
7e1279ea 1136 {
b6581717 1137 q->sess.print_error (e);
7e1279ea
FCE
1138 }
1139}
1140
1141static void
1142query_func_info (Dwarf_Addr entrypc,
bcc12710 1143 func_info & fi,
7e1279ea
FCE
1144 dwarf_query * q)
1145{
b6581717 1146 try
7e1279ea 1147 {
b6581717
GH
1148 if (q->has_return)
1149 {
1150 // NB. dwarf_derived_probe::emit_registrations will emit a
1151 // kretprobe based on the entrypc in this case.
20e4a32c 1152 query_statement (fi.name, fi.decl_file, fi.decl_line,
b6581717
GH
1153 &fi.die, entrypc, q);
1154 }
1155 else
1156 {
35dc8b04 1157 if (fi.prologue_end != 0)
44f75386 1158 {
44f75386
FCE
1159 query_statement (fi.name, fi.decl_file, fi.decl_line,
1160 &fi.die, fi.prologue_end, q);
1161 }
1162 else
1163 {
1164 query_statement (fi.name, fi.decl_file, fi.decl_line,
1165 &fi.die, entrypc, q);
1166 }
b6581717 1167 }
7e1279ea 1168 }
b6581717 1169 catch (semantic_error &e)
7e1279ea 1170 {
b6581717 1171 q->sess.print_error (e);
7e1279ea
FCE
1172 }
1173}
1174
1175
bd4b874d
SC
1176static void
1177query_srcfile_label (const dwarf_line_t& line, void * arg)
1178{
1179 dwarf_query * q = static_cast<dwarf_query *>(arg);
1180
1181 Dwarf_Addr addr = line.addr();
1182
1183 for (func_info_map_t::iterator i = q->filtered_functions.begin();
1184 i != q->filtered_functions.end(); ++i)
1185 if (q->dw.die_has_pc (i->die, addr))
440f755a
JS
1186 q->dw.iterate_over_labels (&i->die, q->label_val.c_str(), q->function.c_str(),
1187 q, query_statement);
bd4b874d
SC
1188}
1189
7e1279ea 1190static void
86bf665e 1191query_srcfile_line (const dwarf_line_t& line, void * arg)
7e1279ea
FCE
1192{
1193 dwarf_query * q = static_cast<dwarf_query *>(arg);
1194
86bf665e 1195 Dwarf_Addr addr = line.addr();
4cd232e4 1196
86bf665e 1197 int lineno = line.lineno();
847bf07f 1198
86bf665e 1199 for (func_info_map_t::iterator i = q->filtered_functions.begin();
7e1279ea
FCE
1200 i != q->filtered_functions.end(); ++i)
1201 {
3e961ba6 1202 if (q->dw.die_has_pc (i->die, addr))
7e1279ea 1203 {
b0ee93c4 1204 if (q->sess.verbose>3)
db22e55f 1205 clog << "function DIE lands on srcfile\n";
4cd232e4 1206 if (q->has_statement_str)
3e961ba6 1207 query_statement (i->name, i->decl_file,
847bf07f 1208 lineno, // NB: not q->line !
3e961ba6 1209 &(i->die), addr, q);
4cd232e4 1210 else
3e961ba6 1211 query_func_info (i->entrypc, *i, q);
7e1279ea 1212 }
20e4a32c
RM
1213 }
1214
86bf665e 1215 for (inline_instance_map_t::iterator i
897820ca
GH
1216 = q->filtered_inlines.begin();
1217 i != q->filtered_inlines.end(); ++i)
1218 {
3e961ba6 1219 if (q->dw.die_has_pc (i->die, addr))
7e1279ea 1220 {
b0ee93c4 1221 if (q->sess.verbose>3)
db22e55f 1222 clog << "inline instance DIE lands on srcfile\n";
897820ca 1223 if (q->has_statement_str)
3e961ba6
JB
1224 query_statement (i->name, i->decl_file,
1225 q->line[0], &(i->die), addr, q);
897820ca 1226 else
3e961ba6 1227 query_inline_instance_info (*i, q);
897820ca 1228 }
20e4a32c 1229 }
7e1279ea
FCE
1230}
1231
1232
4fa7b22b 1233static int
7e1279ea 1234query_dwarf_inline_instance (Dwarf_Die * die, void * arg)
4fa7b22b
GH
1235{
1236 dwarf_query * q = static_cast<dwarf_query *>(arg);
7e1279ea 1237 assert (!q->has_statement_num);
bd2b1e68 1238
39bcd429 1239 try
7a053d3b 1240 {
b0ee93c4 1241 if (q->sess.verbose>2)
db22e55f 1242 clog << "examining inline instance of " << q->dw.function_name << "\n";
7e1279ea 1243
4baf0e53 1244 if ((q->has_function_str && ! q->has_call)
b8da0ad1 1245 || q->has_statement_str)
7e1279ea 1246 {
b0ee93c4 1247 if (q->sess.verbose>2)
db22e55f
FCE
1248 clog << "selected inline instance of " << q->dw.function_name
1249 << "\n";
7e1279ea
FCE
1250
1251 Dwarf_Addr entrypc;
1252 if (q->dw.die_entrypc (die, &entrypc))
1253 {
1254 inline_instance_info inl;
1255 inl.die = *die;
1256 inl.name = q->dw.function_name;
3e961ba6 1257 inl.entrypc = entrypc;
4cd232e4
GH
1258 q->dw.function_file (&inl.decl_file);
1259 q->dw.function_line (&inl.decl_line);
3e961ba6 1260 q->filtered_inlines.push_back(inl);
7e1279ea
FCE
1261 }
1262 }
1263 return DWARF_CB_OK;
1264 }
1265 catch (const semantic_error& e)
1266 {
1267 q->sess.print_error (e);
1268 return DWARF_CB_ABORT;
1269 }
1270}
bb788f9f 1271
7e1279ea 1272static int
2da9cedb 1273query_dwarf_func (Dwarf_Die * func, base_query * bq)
7e1279ea 1274{
2da9cedb 1275 dwarf_query * q = static_cast<dwarf_query *>(bq);
bb788f9f 1276
7e1279ea
FCE
1277 try
1278 {
7e1279ea
FCE
1279 q->dw.focus_on_function (func);
1280
857bdfd1
JS
1281 // make sure that this function address hasn't
1282 // already been matched under an aliased name
1283 Dwarf_Addr addr;
1284 if (!q->dw.func_is_inline() &&
1285 dwarf_entrypc(func, &addr) == 0 &&
1286 !q->alias_dupes.insert(addr).second)
1287 return DWARF_CB_OK;
1288
20e4a32c 1289 if (q->dw.func_is_inline ()
b8da0ad1 1290 && (! q->has_call) && (! q->has_return)
1c6b77e5 1291 && (q->has_statement_str || q->has_function_str))
7e1279ea 1292 {
b0ee93c4 1293 if (q->sess.verbose>3)
db22e55f
FCE
1294 clog << "checking instances of inline " << q->dw.function_name
1295 << "\n";
2da9cedb 1296 q->dw.iterate_over_inline_instances (query_dwarf_inline_instance, q);
275f40a6
FCE
1297
1298 if (q->dw.function_name_final_match (q->function))
1299 return DWARF_CB_ABORT;
7e1279ea 1300 }
396afcee 1301 else if (!q->dw.func_is_inline () && (! q->has_inline))
20e4a32c 1302 {
7e1279ea
FCE
1303 bool record_this_function = false;
1304
1c6b77e5 1305 if (q->has_statement_str || q->has_function_str)
7e1279ea
FCE
1306 {
1307 record_this_function = true;
1308 }
e4c58386 1309 else if (q->has_function_num || q->has_statement_num)
7e1279ea 1310 {
e4c58386 1311 Dwarf_Addr query_addr =
9b692b91
SC
1312 (q->has_function_num ? q->function_num_val :
1313 q->has_statement_num ? q->statement_num_val :
1314 (assert(0) , 0));
7e1279ea
FCE
1315 Dwarf_Die d;
1316 q->dw.function_die (&d);
20e4a32c 1317
1f8592d1
MW
1318 // Translate literal address to symbol address, then
1319 // compensate for dw bias.
1320 query_addr = q->dw.literal_addr_to_sym_addr(query_addr);
1321 query_addr -= q->dw.module_bias;
1322
86bf665e 1323 if (q->dw.die_has_pc (d, query_addr))
7e1279ea
FCE
1324 record_this_function = true;
1325 }
1326
1327 if (record_this_function)
1328 {
b0ee93c4 1329 if (q->sess.verbose>2)
db22e55f 1330 clog << "selected function " << q->dw.function_name << "\n";
7e1279ea 1331
e4c58386
FCE
1332 func_info func;
1333 q->dw.function_die (&func.die);
1334 func.name = q->dw.function_name;
1335 q->dw.function_file (&func.decl_file);
1336 q->dw.function_line (&func.decl_line);
1337
1338 if (q->has_function_num || q->has_function_str || q->has_statement_str)
1339 {
1340 Dwarf_Addr entrypc;
1341 if (q->dw.function_entrypc (&entrypc))
3e961ba6
JB
1342 {
1343 func.entrypc = entrypc;
1344 q->filtered_functions.push_back (func);
1345 }
e4c58386 1346 else
552fdd9f
JB
1347 /* this function just be fully inlined, just ignore it */
1348 return DWARF_CB_OK;
e4c58386
FCE
1349 }
1350 else if (q->has_statement_num)
1351 {
3e961ba6 1352 func.entrypc = q->statement_num_val;
1f8592d1
MW
1353
1354 // Translate literal address to symbol address, then
1355 // compensate for dw bias (will be used for query dw funcs).
1356 func.entrypc = q->dw.literal_addr_to_sym_addr(func.entrypc);
1357 func.entrypc -= q->dw.module_bias;
1358
3e961ba6 1359 q->filtered_functions.push_back (func);
275f40a6
FCE
1360 if (q->dw.function_name_final_match (q->function))
1361 return DWARF_CB_ABORT;
e4c58386
FCE
1362 }
1363 else
1364 assert(0);
1365
1366 if (q->dw.function_name_final_match (q->function))
1367 return DWARF_CB_ABORT;
7e1279ea
FCE
1368 }
1369 }
39bcd429 1370 return DWARF_CB_OK;
bd2b1e68 1371 }
39bcd429 1372 catch (const semantic_error& e)
bd2b1e68 1373 {
39bcd429
FCE
1374 q->sess.print_error (e);
1375 return DWARF_CB_ABORT;
bd2b1e68 1376 }
bd2b1e68
GH
1377}
1378
1379static int
1380query_cu (Dwarf_Die * cudie, void * arg)
1381{
20c6c071 1382 dwarf_query * q = static_cast<dwarf_query *>(arg);
49abf162 1383 if (pending_interrupts) return DWARF_CB_ABORT;
7a053d3b 1384
39bcd429 1385 try
bd2b1e68 1386 {
7e1279ea 1387 q->dw.focus_on_cu (cudie);
b5d77020 1388
b0ee93c4 1389 if (false && q->sess.verbose>2)
b5d77020 1390 clog << "focused on CU '" << q->dw.cu_name
db22e55f 1391 << "', in module '" << q->dw.module_name << "'\n";
d9b516ca 1392
e4c58386 1393 if (q->has_statement_str || q->has_statement_num
54efe513 1394 || q->has_function_str || q->has_function_num)
7e1279ea
FCE
1395 {
1396 q->filtered_srcfiles.clear();
1397 q->filtered_functions.clear();
1398 q->filtered_inlines.clear();
1399
1400 // In this path, we find "abstract functions", record
1401 // information about them, and then (depending on lineno
1402 // matching) possibly emit one or more of the function's
1403 // associated addresses. Unfortunately the control of this
1404 // cannot easily be turned inside out.
1405
b8da0ad1 1406 if ((q->has_statement_str || q->has_function_str)
7e1279ea
FCE
1407 && (q->spec_type != function_alone))
1408 {
1409 // If we have a pattern string with a filename, we need
1410 // to elaborate the srcfile mask in question first.
1411 q->dw.collect_srcfiles_matching (q->file, q->filtered_srcfiles);
1412
1413 // If we have a file pattern and *no* srcfile matches, there's
1414 // no need to look further into this CU, so skip.
1415 if (q->filtered_srcfiles.empty())
1416 return DWARF_CB_OK;
1417 }
86bf665e
TM
1418 // Verify that a raw address matches the beginning of a
1419 // statement. This is a somewhat lame check that the address
467bea43
SC
1420 // is at the start of an assembly instruction. Mark probes are in the
1421 // middle of a macro and thus not strictly at a statement beginning.
6a35d102 1422 // Guru mode may override this check.
467bea43 1423 if (q->has_statement_num && ! q->has_mark)
86bf665e
TM
1424 {
1425 Dwarf_Addr queryaddr = q->statement_num_val;
1426 dwarf_line_t address_line(dwarf_getsrc_die(cudie, queryaddr));
1427 Dwarf_Addr lineaddr = 0;
1428 if (address_line)
1429 lineaddr = address_line.addr();
1430 if (!address_line || lineaddr != queryaddr)
1431 {
1432 stringstream msg;
1433 msg << "address 0x" << hex << queryaddr
1b1b4ceb 1434 << " does not match the beginning of a statement";
4116c576
MW
1435 if (address_line)
1436 msg << " (try 0x" << hex << lineaddr << ")";
5c5099fa
MW
1437 else
1438 msg << " (no line info found for '" << q->dw.cu_name
1439 << "', in module '" << q->dw.module_name << "')";
6a35d102
MW
1440 if (! q->sess.guru_mode)
1441 throw semantic_error(msg.str());
1442 else if (! q->sess.suppress_warnings)
1443 q->sess.print_warning(msg.str());
86bf665e
TM
1444 }
1445 }
7e1279ea
FCE
1446 // Pick up [entrypc, name, DIE] tuples for all the functions
1447 // matching the query, and fill in the prologue endings of them
1448 // all in a single pass.
2da9cedb
JS
1449 int rc = q->dw.iterate_over_functions (query_dwarf_func, q,
1450 q->function,
1451 q->has_statement_num);
5f0a03a6
JK
1452 if (rc != DWARF_CB_OK)
1453 q->query_done = true;
44f75386 1454
35dc8b04 1455 if ((q->sess.prologue_searching || q->has_process) // PR 6871
e4c58386 1456 && !q->has_statement_str && !q->has_statement_num) // PR 2608
44f75386
FCE
1457 if (! q->filtered_functions.empty())
1458 q->dw.resolve_prologue_endings (q->filtered_functions);
7e1279ea 1459
bd4b874d
SC
1460 if (q->has_label)
1461 {
1462 if (q->line[0] == 0) // No line number specified
440f755a
JS
1463 q->dw.iterate_over_labels (q->dw.cu, q->label_val.c_str(), q->function.c_str(),
1464 q, query_statement);
bd4b874d
SC
1465 else
1466 for (set<char const *>::const_iterator i = q->filtered_srcfiles.begin();
1467 i != q->filtered_srcfiles.end(); ++i)
1468 q->dw.iterate_over_srcfile_lines (*i, q->line, q->has_statement_str,
9b988eff 1469 q->line_type, query_srcfile_label, q->function, q);
bd4b874d
SC
1470 }
1471 else if ((q->has_statement_str || q->has_function_str)
7e1279ea
FCE
1472 && (q->spec_type == function_file_and_line))
1473 {
1474 // If we have a pattern string with target *line*, we
20e4a32c 1475 // have to look at lines in all the matched srcfiles.
7e1279ea
FCE
1476 for (set<char const *>::const_iterator i = q->filtered_srcfiles.begin();
1477 i != q->filtered_srcfiles.end(); ++i)
897820ca 1478 q->dw.iterate_over_srcfile_lines (*i, q->line, q->has_statement_str,
9b988eff 1479 q->line_type, query_srcfile_line, q->function, q);
7e1279ea
FCE
1480 }
1481 else
1482 {
e4c58386 1483 // Otherwise, simply probe all resolved functions.
86bf665e 1484 for (func_info_map_t::iterator i = q->filtered_functions.begin();
e4c58386 1485 i != q->filtered_functions.end(); ++i)
3e961ba6 1486 query_func_info (i->entrypc, *i, q);
e4c58386
FCE
1487
1488 // And all inline instances (if we're not excluding inlines with ".call")
1489 if (! q->has_call)
86bf665e 1490 for (inline_instance_map_t::iterator i
54efe513 1491 = q->filtered_inlines.begin(); i != q->filtered_inlines.end(); ++i)
3e961ba6 1492 query_inline_instance_info (*i, q);
7e1279ea
FCE
1493 }
1494 }
39bcd429
FCE
1495 else
1496 {
e4c58386
FCE
1497 // Before PR 5787, we used to have this:
1498#if 0
7e1279ea
FCE
1499 // Otherwise we have a statement number, and we can just
1500 // query it directly within this module.
7e1279ea
FCE
1501 assert (q->has_statement_num);
1502 Dwarf_Addr query_addr = q->statement_num_val;
b8da0ad1 1503 query_addr = q->dw.module_address_to_global(query_addr);
7e1279ea 1504
bcc12710 1505 query_statement ("", "", -1, NULL, query_addr, q);
e4c58386
FCE
1506#endif
1507 // But now, we traverse CUs/functions even for
1508 // statement_num's, for blacklist sensitivity and $var
1509 // resolution purposes.
1510
1511 assert (0); // NOTREACHED
39bcd429
FCE
1512 }
1513 return DWARF_CB_OK;
bd2b1e68 1514 }
39bcd429 1515 catch (const semantic_error& e)
bd2b1e68 1516 {
39bcd429
FCE
1517 q->sess.print_error (e);
1518 return DWARF_CB_ABORT;
bd2b1e68 1519 }
bd2b1e68
GH
1520}
1521
0ce64fb8 1522
5f0a03a6
JK
1523static void
1524validate_module_elf (Dwfl_Module *mod, const char *name, base_query *q)
1525{
1526 // Validate the machine code in this elf file against the
1527 // session machine. This is important, in case the wrong kind
1528 // of debuginfo is being automagically processed by elfutils.
1529 // While we can tell i686 apart from x86-64, unfortunately
1530 // we can't help confusing i586 vs i686 (both EM_386).
1531
1532 Dwarf_Addr bias;
1533 // We prefer dwfl_module_getdwarf to dwfl_module_getelf here,
1534 // because dwfl_module_getelf can force costly section relocations
1535 // we don't really need, while either will do for this purpose.
1536 Elf* elf = (dwarf_getelf (dwfl_module_getdwarf (mod, &bias))
1537 ?: dwfl_module_getelf (mod, &bias));
1538
1539 GElf_Ehdr ehdr_mem;
1540 GElf_Ehdr* em = gelf_getehdr (elf, &ehdr_mem);
86bf665e 1541 if (em == 0) { dwfl_assert ("dwfl_getehdr", dwfl_errno()); }
5f0a03a6
JK
1542 int elf_machine = em->e_machine;
1543 const char* debug_filename = "";
1544 const char* main_filename = "";
1545 (void) dwfl_module_info (mod, NULL, NULL,
1546 NULL, NULL, NULL,
1547 & main_filename,
1548 & debug_filename);
1549 const string& sess_machine = q->sess.architecture;
756c9462
FCE
1550
1551 string expect_machine; // to match sess.machine (i.e., kernel machine)
1552 string expect_machine2;
5f0a03a6
JK
1553
1554 switch (elf_machine)
1555 {
756c9462
FCE
1556 // x86 and ppc are bi-architecture; a 64-bit kernel
1557 // can normally run either 32-bit or 64-bit *userspace*.
1558 case EM_386:
1559 expect_machine = "i?86";
1560 if (! q->has_process) break; // 32-bit kernel/module
1561 /* FALLSTHROUGH */
1562 case EM_X86_64:
1563 expect_machine2 = "x86_64";
1564 break;
1565 case EM_PPC:
1566 expect_machine = "ppc";
1567 if (! q->has_process) break; // 32-bit kernel/module
1568 /* FALLSTHROUGH */
1569 case EM_PPC64:
1570 expect_machine2 = "ppc64";
1571 break;
5f0a03a6
JK
1572 case EM_S390: expect_machine = "s390x"; break;
1573 case EM_IA_64: expect_machine = "ia64"; break;
1574 case EM_ARM: expect_machine = "armv*"; break;
1575 // XXX: fill in some more of these
1576 default: expect_machine = "?"; break;
1577 }
1578
1579 if (! debug_filename) debug_filename = main_filename;
1580 if (! debug_filename) debug_filename = name;
1581
756c9462
FCE
1582 if (fnmatch (expect_machine.c_str(), sess_machine.c_str(), 0) != 0 &&
1583 fnmatch (expect_machine2.c_str(), sess_machine.c_str(), 0) != 0)
5f0a03a6
JK
1584 {
1585 stringstream msg;
756c9462
FCE
1586 msg << "ELF machine " << expect_machine << "|" << expect_machine2
1587 << " (code " << elf_machine
5f0a03a6
JK
1588 << ") mismatch with target " << sess_machine
1589 << " in '" << debug_filename << "'";
1590 throw semantic_error(msg.str ());
1591 }
1592
1593 if (q->sess.verbose>2)
1594 clog << "focused on module '" << q->dw.module_name
1595 << " = [0x" << hex << q->dw.module_start
1596 << "-0x" << q->dw.module_end
1597 << ", bias 0x" << q->dw.module_bias << "]" << dec
1598 << " file " << debug_filename
756c9462 1599 << " ELF machine " << expect_machine << "|" << expect_machine2
5f0a03a6
JK
1600 << " (code " << elf_machine << ")"
1601 << "\n";
1602}
1d3a40b6 1603
91af0778
FCE
1604
1605
1606static Dwarf_Addr
1607lookup_symbol_address (Dwfl_Module *m, const char* wanted)
1608{
1609 int syments = dwfl_module_getsymtab(m);
1610 assert(syments);
1611 for (int i = 1; i < syments; ++i)
1612 {
1613 GElf_Sym sym;
1614 const char *name = dwfl_module_getsym(m, i, &sym, NULL);
1615 if (name != NULL && strcmp(name, wanted) == 0)
1616 return sym.st_value;
1617 }
1618
1619 return 0;
1620}
1621
1622
1623
bd2b1e68 1624static int
b8da0ad1 1625query_module (Dwfl_Module *mod,
91af0778 1626 void **,
b8da0ad1 1627 const char *name,
6f4c1275 1628 Dwarf_Addr addr,
b8da0ad1 1629 void *arg)
bd2b1e68 1630{
91af0778 1631 base_query *q = static_cast<base_query *>(arg);
bd2b1e68 1632
39bcd429 1633 try
e38d6504 1634 {
91af0778
FCE
1635 module_info* mi = q->sess.module_cache->cache[name];
1636 if (mi == 0)
1637 {
1638 mi = q->sess.module_cache->cache[name] = new module_info(name);
1639
6f4c1275
FCE
1640 mi->mod = mod;
1641 mi->addr = addr;
91af0778 1642
6f4c1275
FCE
1643 const char* debug_filename = "";
1644 const char* main_filename = "";
1645 (void) dwfl_module_info (mod, NULL, NULL,
1646 NULL, NULL, NULL,
1647 & main_filename,
1648 & debug_filename);
1649
1650 if (q->sess.ignore_vmlinux && name == TOK_KERNEL)
91af0778
FCE
1651 {
1652 // report_kernel() in elfutils found vmlinux, but pretend it didn't.
1653 // Given a non-null path, returning 1 means keep reporting modules.
1654 mi->dwarf_status = info_absent;
1655 }
6f4c1275 1656 else if (debug_filename || main_filename)
91af0778 1657 {
6f4c1275
FCE
1658 mi->elf_path = debug_filename ?: main_filename;
1659 }
1660 else if (name == TOK_KERNEL)
1661 {
1662 mi->dwarf_status = info_absent;
91af0778 1663 }
91af0778
FCE
1664 }
1665 // OK, enough of that module_info caching business.
1666
5f0a03a6 1667 q->dw.focus_on_module(mod, mi);
d9b516ca 1668
39bcd429
FCE
1669 // If we have enough information in the pattern to skip a module and
1670 // the module does not match that information, return early.
b8da0ad1 1671 if (!q->dw.module_name_matches(q->module_val))
0e14e079 1672 return pending_interrupts ? DWARF_CB_ABORT : DWARF_CB_OK;
0cbbf9d1
FCE
1673
1674 // Don't allow module("*kernel*") type expressions to match the
1675 // elfutils module "kernel", which we refer to in the probe
1676 // point syntax exclusively as "kernel.*".
1677 if (q->dw.module_name == TOK_KERNEL && ! q->has_kernel)
0e14e079 1678 return pending_interrupts ? DWARF_CB_ABORT : DWARF_CB_OK;
b5d77020 1679
5f0a03a6
JK
1680 if (mod)
1681 validate_module_elf(mod, name, q);
1682 else
91af0778
FCE
1683 assert(q->has_kernel); // and no vmlinux to examine
1684
1685 if (q->sess.verbose>2)
1686 cerr << "focused on module '" << q->dw.module_name << "'\n";
1687
1688
1689 // Collect a few kernel addresses. XXX: these belong better
1690 // to the sess.module_info["kernel"] struct.
1691 if (q->dw.module_name == TOK_KERNEL)
c931ec8a 1692 {
91af0778
FCE
1693 if (! q->sess.sym_kprobes_text_start)
1694 q->sess.sym_kprobes_text_start = lookup_symbol_address (mod, "__kprobes_text_start");
1695 if (! q->sess.sym_kprobes_text_end)
1696 q->sess.sym_kprobes_text_end = lookup_symbol_address (mod, "__kprobes_text_end");
1697 if (! q->sess.sym_stext)
1698 q->sess.sym_stext = lookup_symbol_address (mod, "_stext");
c931ec8a
FCE
1699 }
1700
91af0778 1701 // Finally, search the module for matches of the probe point.
2c384610 1702 q->handle_query_module();
bb788f9f 1703
91af0778 1704
b8da0ad1 1705 // If we know that there will be no more matches, abort early.
0e14e079 1706 if (q->dw.module_name_final_match(q->module_val) || pending_interrupts)
b8da0ad1
FCE
1707 return DWARF_CB_ABORT;
1708 else
1709 return DWARF_CB_OK;
7a053d3b 1710 }
39bcd429 1711 catch (const semantic_error& e)
bd2b1e68 1712 {
39bcd429
FCE
1713 q->sess.print_error (e);
1714 return DWARF_CB_ABORT;
bd2b1e68 1715 }
bd2b1e68
GH
1716}
1717
35d4ab18 1718
de688825 1719struct dwarf_var_expanding_visitor: public var_expanding_visitor
35d4ab18 1720{
77de5e9e 1721 dwarf_query & q;
bcc12710 1722 Dwarf_Die *scope_die;
77de5e9e 1723 Dwarf_Addr addr;
8c819921 1724 block *add_block;
8fc05e57 1725 probe *add_probe;
cd5b28b2 1726 std::map<std::string, symbol *> return_ts_map;
b95e2b79 1727 bool visited;
77de5e9e 1728
de688825 1729 dwarf_var_expanding_visitor(dwarf_query & q, Dwarf_Die *sd, Dwarf_Addr a):
b95e2b79 1730 q(q), scope_die(sd), addr(a), add_block(NULL), add_probe(NULL), visited(false) {}
a7999c82
JS
1731 void visit_target_symbol_saved_return (target_symbol* e);
1732 void visit_target_symbol_context (target_symbol* e);
d7f3e0c5 1733 void visit_target_symbol (target_symbol* e);
c24447be 1734 void visit_cast_op (cast_op* e);
77de5e9e
GH
1735};
1736
1737
de688825 1738unsigned var_expanding_visitor::tick = 0;
77de5e9e 1739
77de5e9e 1740void
de688825 1741var_expanding_visitor::visit_assignment (assignment* e)
77de5e9e 1742{
e57b735a
GH
1743 // Our job would normally be to require() the left and right sides
1744 // into a new assignment. What we're doing is slightly trickier:
1745 // we're pushing a functioncall** onto a stack, and if our left
1746 // child sets the functioncall* for that value, we're going to
1747 // assume our left child was a target symbol -- transformed into a
1748 // set_target_foo(value) call, and it wants to take our right child
1749 // as the argument "value".
1750 //
1751 // This is why some people claim that languages with
1752 // constructor-decomposing case expressions have a leg up on
1753 // visitors.
1754
1755 functioncall *fcall = NULL;
1756 expression *new_left, *new_right;
d9b516ca 1757
e57b735a 1758 target_symbol_setter_functioncalls.push (&fcall);
4ed05b15 1759 new_left = require (e->left);
e57b735a 1760 target_symbol_setter_functioncalls.pop ();
4ed05b15 1761 new_right = require (e->right);
e57b735a
GH
1762
1763 if (fcall != NULL)
77de5e9e 1764 {
e57b735a
GH
1765 // Our left child is informing us that it was a target variable
1766 // and it has been replaced with a set_target_foo() function
1767 // call; we are going to provide that function call -- with the
1768 // right child spliced in as sole argument -- in place of
de688825 1769 // ourselves, in the var expansion we're in the middle of making.
e57b735a
GH
1770
1771 // FIXME: for the time being, we only support plan $foo = bar,
1772 // not += or any other op= variant. This is fixable, but a bit
1773 // ugly.
1774 if (e->op != "=")
1775 throw semantic_error ("Operator-assign expressions on target "
1776 "variables not implemented", e->tok);
1777
1778 assert (new_left == fcall);
1779 fcall->args.push_back (new_right);
4ed05b15 1780 provide (fcall);
77de5e9e 1781 }
e57b735a
GH
1782 else
1783 {
de688825
JS
1784 e->left = new_left;
1785 e->right = new_right;
1786 provide (e);
e57b735a
GH
1787 }
1788}
d9b516ca 1789
d7f3e0c5 1790
e57b735a 1791void
a7999c82 1792dwarf_var_expanding_visitor::visit_target_symbol_saved_return (target_symbol* e)
e57b735a 1793{
a7999c82
JS
1794 // Get the full name of the target symbol.
1795 stringstream ts_name_stream;
1796 e->print(ts_name_stream);
1797 string ts_name = ts_name_stream.str();
1798
1799 // Check and make sure we haven't already seen this target
1800 // variable in this return probe. If we have, just return our
1801 // last replacement.
1802 map<string, symbol *>::iterator i = return_ts_map.find(ts_name);
1803 if (i != return_ts_map.end())
85ecf79a 1804 {
a7999c82
JS
1805 provide (i->second);
1806 return;
1807 }
85ecf79a 1808
a7999c82
JS
1809 // We've got to do several things here to handle target
1810 // variables in return probes.
85ecf79a 1811
a7999c82
JS
1812 // (1) Synthesize two global arrays. One is the cache of the
1813 // target variable and the other contains a thread specific
1814 // nesting level counter. The arrays will look like
1815 // this:
1816 //
1817 // _dwarf_tvar_{name}_{num}
1818 // _dwarf_tvar_{name}_{num}_ctr
1819
1820 string aname = (string("_dwarf_tvar_")
1821 + e->base_name.substr(1)
1822 + "_" + lex_cast<string>(tick++));
1823 vardecl* vd = new vardecl;
1824 vd->name = aname;
1825 vd->tok = e->tok;
1826 q.sess.globals.push_back (vd);
1827
1828 string ctrname = aname + "_ctr";
1829 vd = new vardecl;
1830 vd->name = ctrname;
1831 vd->tok = e->tok;
1832 q.sess.globals.push_back (vd);
1833
1834 // (2) Create a new code block we're going to insert at the
1835 // beginning of this probe to get the cached value into a
1836 // temporary variable. We'll replace the target variable
1837 // reference with the temporary variable reference. The code
1838 // will look like this:
1839 //
1840 // _dwarf_tvar_tid = tid()
1841 // _dwarf_tvar_{name}_{num}_tmp
1842 // = _dwarf_tvar_{name}_{num}[_dwarf_tvar_tid,
1843 // _dwarf_tvar_{name}_{num}_ctr[_dwarf_tvar_tid]]
1844 // delete _dwarf_tvar_{name}_{num}[_dwarf_tvar_tid,
1845 // _dwarf_tvar_{name}_{num}_ctr[_dwarf_tvar_tid]--]
1846 // if (! _dwarf_tvar_{name}_{num}_ctr[_dwarf_tvar_tid])
1847 // delete _dwarf_tvar_{name}_{num}_ctr[_dwarf_tvar_tid]
1848
1849 // (2a) Synthesize the tid temporary expression, which will look
1850 // like this:
1851 //
1852 // _dwarf_tvar_tid = tid()
1853 symbol* tidsym = new symbol;
1854 tidsym->name = string("_dwarf_tvar_tid");
1855 tidsym->tok = e->tok;
85ecf79a 1856
a7999c82
JS
1857 if (add_block == NULL)
1858 {
1859 add_block = new block;
1860 add_block->tok = e->tok;
8c819921 1861
a7999c82
JS
1862 // Synthesize a functioncall to grab the thread id.
1863 functioncall* fc = new functioncall;
1864 fc->tok = e->tok;
1865 fc->function = string("tid");
8c819921 1866
a7999c82 1867 // Assign the tid to '_dwarf_tvar_tid'.
8c819921
DS
1868 assignment* a = new assignment;
1869 a->tok = e->tok;
1870 a->op = "=";
a7999c82
JS
1871 a->left = tidsym;
1872 a->right = fc;
8c819921
DS
1873
1874 expr_statement* es = new expr_statement;
1875 es->tok = e->tok;
1876 es->value = a;
8c819921 1877 add_block->statements.push_back (es);
a7999c82 1878 }
8c819921 1879
a7999c82
JS
1880 // (2b) Synthesize an array reference and assign it to a
1881 // temporary variable (that we'll use as replacement for the
1882 // target variable reference). It will look like this:
1883 //
1884 // _dwarf_tvar_{name}_{num}_tmp
1885 // = _dwarf_tvar_{name}_{num}[_dwarf_tvar_tid,
1886 // _dwarf_tvar_{name}_{num}_ctr[_dwarf_tvar_tid]]
1887
1888 arrayindex* ai_tvar_base = new arrayindex;
1889 ai_tvar_base->tok = e->tok;
1890
1891 symbol* sym = new symbol;
1892 sym->name = aname;
1893 sym->tok = e->tok;
1894 ai_tvar_base->base = sym;
1895
1896 ai_tvar_base->indexes.push_back(tidsym);
1897
1898 // We need to create a copy of the array index in its current
1899 // state so we can have 2 variants of it (the original and one
1900 // that post-decrements the second index).
1901 arrayindex* ai_tvar = new arrayindex;
1902 arrayindex* ai_tvar_postdec = new arrayindex;
1903 *ai_tvar = *ai_tvar_base;
1904 *ai_tvar_postdec = *ai_tvar_base;
1905
1906 // Synthesize the
1907 // "_dwarf_tvar_{name}_{num}_ctr[_dwarf_tvar_tid]" used as the
1908 // second index into the array.
1909 arrayindex* ai_ctr = new arrayindex;
1910 ai_ctr->tok = e->tok;
1911
1912 sym = new symbol;
1913 sym->name = ctrname;
1914 sym->tok = e->tok;
1915 ai_ctr->base = sym;
1916 ai_ctr->indexes.push_back(tidsym);
1917 ai_tvar->indexes.push_back(ai_ctr);
1918
1919 symbol* tmpsym = new symbol;
1920 tmpsym->name = aname + "_tmp";
1921 tmpsym->tok = e->tok;
1922
1923 assignment* a = new assignment;
1924 a->tok = e->tok;
1925 a->op = "=";
1926 a->left = tmpsym;
1927 a->right = ai_tvar;
1928
1929 expr_statement* es = new expr_statement;
1930 es->tok = e->tok;
1931 es->value = a;
1932
1933 add_block->statements.push_back (es);
1934
1935 // (2c) Add a post-decrement to the second array index and
1936 // delete the array value. It will look like this:
1937 //
1938 // delete _dwarf_tvar_{name}_{num}[_dwarf_tvar_tid,
1939 // _dwarf_tvar_{name}_{num}_ctr[_dwarf_tvar_tid]--]
1940
1941 post_crement* pc = new post_crement;
1942 pc->tok = e->tok;
1943 pc->op = "--";
1944 pc->operand = ai_ctr;
1945 ai_tvar_postdec->indexes.push_back(pc);
1946
1947 delete_statement* ds = new delete_statement;
1948 ds->tok = e->tok;
1949 ds->value = ai_tvar_postdec;
1950
1951 add_block->statements.push_back (ds);
1952
1953 // (2d) Delete the counter value if it is 0. It will look like
1954 // this:
1955 // if (! _dwarf_tvar_{name}_{num}_ctr[_dwarf_tvar_tid])
1956 // delete _dwarf_tvar_{name}_{num}_ctr[_dwarf_tvar_tid]
1957
1958 ds = new delete_statement;
1959 ds->tok = e->tok;
1960 ds->value = ai_ctr;
1961
1962 unary_expression *ue = new unary_expression;
1963 ue->tok = e->tok;
1964 ue->op = "!";
1965 ue->operand = ai_ctr;
1966
1967 if_statement *ifs = new if_statement;
1968 ifs->tok = e->tok;
1969 ifs->condition = ue;
1970 ifs->thenblock = ds;
1971 ifs->elseblock = NULL;
1972
1973 add_block->statements.push_back (ifs);
1974
1975 // (3) We need an entry probe that saves the value for us in the
1976 // global array we created. Create the entry probe, which will
1977 // look like this:
1978 //
1979 // probe kernel.function("{function}") {
1980 // _dwarf_tvar_tid = tid()
1981 // _dwarf_tvar_{name}_{num}[_dwarf_tvar_tid,
1982 // ++_dwarf_tvar_{name}_{num}_ctr[_dwarf_tvar_tid]]
1983 // = ${param}
1984 // }
1985
1986 if (add_probe == NULL)
1987 {
1988 add_probe = new probe;
1989 add_probe->tok = e->tok;
1990
1991 // We need the name of the current probe point, minus the
1992 // ".return" (or anything after it, such as ".maxactive(N)").
1993 // Create a new probe point, copying all the components,
1994 // stopping when we see the ".return" component.
1995 probe_point* pp = new probe_point;
1996 for (unsigned c = 0; c < q.base_loc->components.size(); c++)
85ecf79a 1997 {
a7999c82
JS
1998 if (q.base_loc->components[c]->functor == "return")
1999 break;
2000 else
2001 pp->components.push_back(q.base_loc->components[c]);
2002 }
2003 pp->tok = e->tok;
2004 pp->optional = q.base_loc->optional;
2005 add_probe->locations.push_back(pp);
8fc05e57 2006
a7999c82
JS
2007 add_probe->body = new block;
2008 add_probe->body->tok = e->tok;
4baf0e53 2009
a7999c82
JS
2010 // Synthesize a functioncall to grab the thread id.
2011 functioncall* fc = new functioncall;
2012 fc->tok = e->tok;
2013 fc->function = string("tid");
4baf0e53 2014
a7999c82
JS
2015 // Assign the tid to '_dwarf_tvar_tid'.
2016 assignment* a = new assignment;
8fc05e57
DS
2017 a->tok = e->tok;
2018 a->op = "=";
a7999c82
JS
2019 a->left = tidsym;
2020 a->right = fc;
8fc05e57 2021
a7999c82 2022 expr_statement* es = new expr_statement;
8fc05e57
DS
2023 es->tok = e->tok;
2024 es->value = a;
ba6f838d 2025 add_probe->body = new block(add_probe->body, es);
85ecf79a 2026
a7999c82
JS
2027 vardecl* vd = new vardecl;
2028 vd->tok = e->tok;
2029 vd->name = tidsym->name;
2030 vd->type = pe_long;
2031 vd->set_arity(0);
2032 add_probe->locals.push_back(vd);
85ecf79a 2033 }
cf2a1f85 2034
a7999c82
JS
2035 // Save the value, like this:
2036 // _dwarf_tvar_{name}_{num}[_dwarf_tvar_tid,
2037 // ++_dwarf_tvar_{name}_{num}_ctr[_dwarf_tvar_tid]]
2038 // = ${param}
2039 arrayindex* ai_tvar_preinc = new arrayindex;
2040 *ai_tvar_preinc = *ai_tvar_base;
2041
2042 pre_crement* preinc = new pre_crement;
2043 preinc->tok = e->tok;
2044 preinc->op = "++";
2045 preinc->operand = ai_ctr;
2046 ai_tvar_preinc->indexes.push_back(preinc);
2047
2048 a = new assignment;
2049 a->tok = e->tok;
2050 a->op = "=";
2051 a->left = ai_tvar_preinc;
2052 a->right = e;
2053
2054 es = new expr_statement;
2055 es->tok = e->tok;
2056 es->value = a;
2057
2058 add_probe->body = new block(add_probe->body, es);
2059
2060 // (4) Provide the '_dwarf_tvar_{name}_{num}_tmp' variable to
2061 // our parent so it can be used as a substitute for the target
2062 // symbol.
2063 provide (tmpsym);
2064
2065 // (5) Remember this replacement since we might be able to reuse
2066 // it later if the same return probe references this target
2067 // symbol again.
2068 return_ts_map[ts_name] = tmpsym;
2069}
a43ba433 2070
2cb3fe26 2071
a7999c82
JS
2072void
2073dwarf_var_expanding_visitor::visit_target_symbol_context (target_symbol* e)
2074{
2075 Dwarf_Die *scopes;
2076 if (dwarf_getscopes_die (scope_die, &scopes) == 0)
2077 return;
0a98fd42 2078 auto_free free_scopes(scopes);
2cb3fe26 2079
a7999c82
JS
2080 target_symbol *tsym = new target_symbol;
2081 print_format* pf = new print_format;
a43ba433 2082
a7999c82
JS
2083 // Convert $$parms to sprintf of a list of parms and active local vars
2084 // which we recursively evaluate
a43ba433 2085
a7999c82
JS
2086 // NB: we synthesize a new token here rather than reusing
2087 // e->tok, because print_format::print likes to use
2088 // its tok->content.
2089 token* pf_tok = new token;
2090 pf_tok->location = e->tok->location;
2091 pf_tok->type = tok_identifier;
2092 pf_tok->content = "sprint";
2cb3fe26 2093
a7999c82
JS
2094 pf->tok = pf_tok;
2095 pf->print_to_stream = false;
2096 pf->print_with_format = true;
2097 pf->print_with_delim = false;
2098 pf->print_with_newline = false;
2099 pf->print_char = false;
2100
2101 if (q.has_return && (e->base_name == "$$return"))
2102 {
2103 tsym->tok = e->tok;
2104 tsym->base_name = "$return";
2105
2106 // Ignore any variable that isn't accessible.
2107 tsym->saved_conversion_error = 0;
2108 expression *texp = tsym;
8b095b45 2109 replace (texp); // NB: throws nothing ...
a7999c82 2110 if (tsym->saved_conversion_error) // ... but this is how we know it happened.
a43ba433 2111 {
2cb3fe26 2112
a43ba433
FCE
2113 }
2114 else
2115 {
a7999c82
JS
2116 pf->raw_components += "return";
2117 pf->raw_components += "=%#x ";
2118 pf->args.push_back(texp);
2119 }
2120 }
2121 else
2122 {
2123 // non-.return probe: support $$parms, $$vars, $$locals
2124 Dwarf_Die result;
2125 if (dwarf_child (&scopes[0], &result) == 0)
2126 do
2127 {
2128 switch (dwarf_tag (&result))
00cf3709 2129 {
a7999c82
JS
2130 case DW_TAG_variable:
2131 if (e->base_name == "$$parms")
2132 continue;
2133 break;
2134 case DW_TAG_formal_parameter:
2135 if (e->base_name == "$$locals")
2136 continue;
2137 break;
2138
2139 default:
2140 continue;
2141 }
41c262f3 2142
a7999c82
JS
2143 const char *diename = dwarf_diename (&result);
2144 if (! diename) continue;
f76427a2 2145
a7999c82
JS
2146 tsym->tok = e->tok;
2147 tsym->base_name = "$";
2148 tsym->base_name += diename;
41c262f3 2149
a7999c82
JS
2150 // Ignore any variable that isn't accessible.
2151 tsym->saved_conversion_error = 0;
2152 expression *texp = tsym;
8b095b45 2153 replace (texp); // NB: throws nothing ...
a7999c82
JS
2154 if (tsym->saved_conversion_error) // ... but this is how we know it happened.
2155 {
2156 if (q.sess.verbose>2)
a43ba433 2157 {
a7999c82
JS
2158 for (semantic_error *c = tsym->saved_conversion_error;
2159 c != 0;
2160 c = c->chain) {
2161 clog << "variable location problem: " << c->what() << endl;
2162 }
a43ba433 2163 }
a7999c82
JS
2164
2165 pf->raw_components += diename;
2166 pf->raw_components += "=? ";
00cf3709 2167 }
a7999c82
JS
2168 else
2169 {
2170 pf->raw_components += diename;
2171 pf->raw_components += "=%#x ";
2172 pf->args.push_back(texp);
2173 }
2174 }
2175 while (dwarf_siblingof (&result, &result) == 0);
2176 }
2cb3fe26 2177
a7999c82
JS
2178 pf->components = print_format::string_to_components(pf->raw_components);
2179 provide (pf);
2180}
2181
2cb3fe26 2182
a7999c82
JS
2183void
2184dwarf_var_expanding_visitor::visit_target_symbol (target_symbol *e)
2185{
2186 assert(e->base_name.size() > 0 && e->base_name[0] == '$');
2187 visited = true;
2188
2189 bool lvalue = is_active_lvalue(e);
2190 if (lvalue && !q.sess.guru_mode)
2191 throw semantic_error("write to target variable not permitted", e->tok);
2192
2193 // See if we need to generate a new probe to save/access function
2194 // parameters from a return probe. PR 1382.
2195 if (q.has_return
2196 && e->base_name != "$return" // not the special return-value variable handled below
2197 && e->base_name != "$$return") // nor the other special variable handled below
2198 {
2199 if (lvalue)
2200 throw semantic_error("write to target variable not permitted in .return probes", e->tok);
2201
2202 visit_target_symbol_saved_return(e);
2203 return;
2204 }
2205
2206 if (e->base_name == "$$vars"
2207 || e->base_name == "$$parms"
2208 || e->base_name == "$$locals"
2209 || (q.has_return && (e->base_name == "$$return")))
2210 {
2211 if (lvalue)
2212 throw semantic_error("cannot write to context variable", e->tok);
2213
2214 if (e->addressof)
2215 throw semantic_error("cannot take address of context variable", e->tok);
2cb3fe26 2216
a7999c82 2217 visit_target_symbol_context(e);
2cb3fe26
SC
2218 return;
2219 }
2220
e57b735a 2221 // Synthesize a function.
d7f3e0c5 2222 functiondecl *fdecl = new functiondecl;
7b99c7d3 2223 fdecl->tok = e->tok;
d7f3e0c5 2224 embeddedcode *ec = new embeddedcode;
5e309481 2225 ec->tok = e->tok;
e8fbc5e8 2226
1b07c728 2227 string fname = (string(lvalue ? "_dwarf_tvar_set" : "_dwarf_tvar_get")
20e4a32c 2228 + "_" + e->base_name.substr(1)
e57b735a
GH
2229 + "_" + lex_cast<string>(tick++));
2230
66d284f4 2231 try
e57b735a 2232 {
a43ba433 2233 if (q.has_return && (e->base_name == "$return"))
e19fda4e
DS
2234 {
2235 ec->code = q.dw.literal_stmt_for_return (scope_die,
2236 addr,
b4c34c26 2237 e,
e19fda4e
DS
2238 lvalue,
2239 fdecl->type);
2240 }
2241 else
2242 {
2243 ec->code = q.dw.literal_stmt_for_local (scope_die,
2244 addr,
2245 e->base_name.substr(1),
b4c34c26 2246 e,
e19fda4e
DS
2247 lvalue,
2248 fdecl->type);
2249 }
2250
1b07c728
FCE
2251 if (! lvalue)
2252 ec->code += "/* pure */";
66d284f4
FCE
2253 }
2254 catch (const semantic_error& er)
2255 {
3bd0d4df
RA
2256 if (!q.sess.skip_badvars)
2257 {
2258 // We suppress this error message, and pass the unresolved
2259 // target_symbol to the next pass. We hope that this value ends
2260 // up not being referenced after all, so it can be optimized out
2261 // quietly.
2262 provide (e);
2263 semantic_error* saveme = new semantic_error (er); // copy it
3bd0d4df
RA
2264 // NB: we can have multiple errors, since a $target variable
2265 // may be expanded in several different contexts:
2266 // function ("*") { $var }
2267 saveme->chain = e->saved_conversion_error;
2268 e->saved_conversion_error = saveme;
2269 }
ad002306 2270 else
3bd0d4df 2271 {
ad002306 2272 // Upon user request for ignoring context, the symbol is replaced
3bd0d4df
RA
2273 // with a literal 0 and a warning message displayed
2274 literal_number* ln_zero = new literal_number (0);
2275 ln_zero->tok = e->tok;
2276 provide (ln_zero);
8c1b9e27
FCE
2277 if (!q.sess.suppress_warnings)
2278 q.sess.print_warning ("Bad $context variable being substituted with literal 0",
2279 e->tok);
3bd0d4df 2280 }
1cde5ba5
JS
2281 delete fdecl;
2282 delete ec;
cbfbbf69 2283 return;
66d284f4 2284 }
e57b735a 2285
d7f3e0c5
GH
2286 fdecl->name = fname;
2287 fdecl->body = ec;
6fda2dff
JS
2288
2289 // Any non-literal indexes need to be passed in too.
2290 for (unsigned i = 0; i < e->components.size(); ++i)
2291 if (e->components[i].type == target_symbol::comp_expression_array_index)
2292 {
2293 vardecl *v = new vardecl;
2294 v->type = pe_long;
2295 v->name = "index" + lex_cast<string>(i);
2296 v->tok = e->tok;
2297 fdecl->formal_args.push_back(v);
2298 }
2299
e57b735a
GH
2300 if (lvalue)
2301 {
2302 // Modify the fdecl so it carries a single pe_long formal
2303 // argument called "value".
2304
2305 // FIXME: For the time being we only support setting target
2306 // variables which have base types; these are 'pe_long' in
2307 // stap's type vocabulary. Strings and pointers might be
2308 // reasonable, some day, but not today.
2309
2310 vardecl *v = new vardecl;
2311 v->type = pe_long;
2312 v->name = "value";
2313 v->tok = e->tok;
2314 fdecl->formal_args.push_back(v);
2315 }
f76427a2 2316 q.sess.functions[fdecl->name]=fdecl;
d9b516ca 2317
e57b735a 2318 // Synthesize a functioncall.
d7f3e0c5
GH
2319 functioncall* n = new functioncall;
2320 n->tok = e->tok;
2321 n->function = fname;
35d4ab18 2322 n->referent = 0; // NB: must not resolve yet, to ensure inclusion in session
e57b735a 2323
6fda2dff
JS
2324 // Any non-literal indexes need to be passed in too.
2325 for (unsigned i = 0; i < e->components.size(); ++i)
2326 if (e->components[i].type == target_symbol::comp_expression_array_index)
2327 n->args.push_back(require(e->components[i].expr_index));
2328
e57b735a
GH
2329 if (lvalue)
2330 {
2331 // Provide the functioncall to our parent, so that it can be
2332 // used to substitute for the assignment node immediately above
2333 // us.
2334 assert(!target_symbol_setter_functioncalls.empty());
2335 *(target_symbol_setter_functioncalls.top()) = n;
2336 }
2337
4ed05b15 2338 provide (n);
77de5e9e
GH
2339}
2340
2341
c24447be
JS
2342void
2343dwarf_var_expanding_visitor::visit_cast_op (cast_op *e)
2344{
2345 // Fill in our current module context if needed
2346 if (e->module.empty())
2347 e->module = q.dw.module_name;
2348
2349 var_expanding_visitor::visit_cast_op(e);
2350}
2351
2352
c4ce66a1
JS
2353struct dwarf_cast_query : public base_query
2354{
946e1a48 2355 cast_op& e;
c4ce66a1 2356 const bool lvalue;
c4ce66a1 2357
abb41d92
JS
2358 exp_type& pe_type;
2359 string& code;
c4ce66a1 2360
946e1a48 2361 dwarf_cast_query(dwflpp& dw, const string& module, cast_op& e,
abb41d92
JS
2362 bool lvalue, exp_type& pe_type, string& code):
2363 base_query(dw, module), e(e), lvalue(lvalue),
2364 pe_type(pe_type), code(code) {}
c4ce66a1
JS
2365
2366 void handle_query_module();
2367 int handle_query_cu(Dwarf_Die * cudie);
2368
2369 static int cast_query_cu (Dwarf_Die * cudie, void * arg);
2370};
2371
2372
c4ce66a1
JS
2373void
2374dwarf_cast_query::handle_query_module()
2375{
abb41d92 2376 if (!code.empty())
c4ce66a1
JS
2377 return;
2378
2379 // look for the type in each CU
2380 dw.iterate_over_cus(cast_query_cu, this);
2381}
2382
2383
2384int
2385dwarf_cast_query::handle_query_cu(Dwarf_Die * cudie)
2386{
abb41d92 2387 if (!code.empty())
c4ce66a1
JS
2388 return DWARF_CB_ABORT;
2389
2390 dw.focus_on_cu (cudie);
2391 Dwarf_Die* type_die = dw.declaration_resolve(e.type.c_str());
2392 if (type_die)
2393 {
2394 try
2395 {
b4c34c26 2396 code = dw.literal_stmt_for_pointer (type_die, &e,
c4ce66a1
JS
2397 lvalue, pe_type);
2398 }
946e1a48 2399 catch (const semantic_error& er)
c4ce66a1 2400 {
946e1a48
JS
2401 // XXX might be better to try again in another CU
2402 // NB: we can have multiple errors, since a @cast
2403 // may be expanded in several different contexts:
2404 // function ("*") { @cast(...) }
2405 semantic_error* new_er = new semantic_error(er);
2406 new_er->chain = e.saved_conversion_error;
2407 e.saved_conversion_error = new_er;
c4ce66a1 2408 }
c4ce66a1
JS
2409 return DWARF_CB_ABORT;
2410 }
2411 return DWARF_CB_OK;
2412}
2413
2414
2415int
2416dwarf_cast_query::cast_query_cu (Dwarf_Die * cudie, void * arg)
2417{
2418 dwarf_cast_query * q = static_cast<dwarf_cast_query *>(arg);
2419 if (pending_interrupts) return DWARF_CB_ABORT;
2420 return q->handle_query_cu(cudie);
2421}
2422
2423
2424struct dwarf_cast_expanding_visitor: public var_expanding_visitor
2425{
2426 systemtap_session& s;
2427 dwarf_builder& db;
2428
2429 dwarf_cast_expanding_visitor(systemtap_session& s, dwarf_builder& db):
2430 s(s), db(db) {}
2431 void visit_cast_op (cast_op* e);
fb0274bc 2432 void filter_special_modules(string& module);
c4ce66a1
JS
2433};
2434
2435
fb0274bc
JS
2436void dwarf_cast_expanding_visitor::filter_special_modules(string& module)
2437{
d90053e7 2438 // look for "<path/to/header>" or "kernel<path/to/header>"
fb0274bc 2439 // for those cases, build a module including that header
d90053e7
JS
2440 if (module[module.size() - 1] == '>' &&
2441 (module[0] == '<' || module.compare(0, 7, "kernel<") == 0))
fb0274bc
JS
2442 {
2443 string cached_module;
2444 if (s.use_cache)
2445 {
2446 // see if the cached module exists
a2639cb7 2447 cached_module = find_typequery_hash(s, module);
fb0274bc
JS
2448 if (!cached_module.empty())
2449 {
2450 int fd = open(cached_module.c_str(), O_RDONLY);
2451 if (fd != -1)
2452 {
2453 if (s.verbose > 2)
2454 clog << "Pass 2: using cached " << cached_module << endl;
2455 module = cached_module;
2456 close(fd);
2457 return;
2458 }
2459 }
2460 }
2461
2462 // no cached module, time to make it
d90053e7 2463 if (make_typequery(s, module) == 0)
fb0274bc 2464 {
fb0274bc
JS
2465 if (s.use_cache)
2466 {
2467 // try to save typequery in the cache
2468 if (s.verbose > 2)
d90053e7 2469 clog << "Copying " << module
fb0274bc 2470 << " to " << cached_module << endl;
d90053e7 2471 if (copy_file(module.c_str(),
fb0274bc 2472 cached_module.c_str()) != 0)
d90053e7 2473 cerr << "Copy failed (\"" << module << "\" to \""
fb0274bc
JS
2474 << cached_module << "\"): " << strerror(errno) << endl;
2475 }
2476 }
2477 }
2478}
2479
2480
c4ce66a1
JS
2481void dwarf_cast_expanding_visitor::visit_cast_op (cast_op* e)
2482{
313db8e6
DB
2483 if (s.unprivileged)
2484 throw semantic_error("typecasting may not be used when --unprivileged is specified", e->tok);
2485
c4ce66a1
JS
2486 bool lvalue = is_active_lvalue(e);
2487 if (lvalue && !s.guru_mode)
2488 throw semantic_error("write to typecast value not permitted", e->tok);
2489
2490 if (e->module.empty())
2491 e->module = "kernel"; // "*" may also be reasonable to search all kernel modules
2492
c4ce66a1
JS
2493 string code;
2494 exp_type type = pe_long;
8b31197b
JS
2495
2496 // split the module string by ':' for alternatives
2497 vector<string> modules;
2498 tokenize(e->module, modules, ":");
2499 for (unsigned i = 0; code.empty() && i < modules.size(); ++i)
c4ce66a1 2500 {
8b31197b 2501 string& module = modules[i];
fb0274bc 2502 filter_special_modules(module);
abb41d92 2503
c4ce66a1
JS
2504 // NB: This uses '/' to distinguish between kernel modules and userspace,
2505 // which means that userspace modules won't get any PATH searching.
2506 dwflpp* dw;
707bf35e
JS
2507 try
2508 {
2509 if (module.find('/') == string::npos)
2510 {
2511 // kernel or kernel module target
ae2552da 2512 dw = db.get_kern_dw(s, module);
707bf35e
JS
2513 }
2514 else
2515 {
2516 module = find_executable (module); // canonicalize it
2517 dw = db.get_user_dw(s, module);
2518 }
2519 }
2520 catch (const semantic_error& er)
2521 {
2522 /* ignore and go to the next module */
2523 continue;
2524 }
c4ce66a1 2525
abb41d92 2526 dwarf_cast_query q (*dw, module, *e, lvalue, type, code);
51178501 2527 dw->iterate_over_modules(&query_module, &q);
c4ce66a1 2528 }
abb41d92
JS
2529
2530 if (code.empty())
c4ce66a1 2531 {
946e1a48
JS
2532 // We pass the unresolved cast_op to the next pass, and hope
2533 // that this value ends up not being referenced after all, so
2534 // it can be optimized out quietly.
c4ce66a1
JS
2535 provide (e);
2536 return;
2537 }
2538
2539 string fname = (string(lvalue ? "_dwarf_tvar_set" : "_dwarf_tvar_get")
2540 + "_" + e->base_name.substr(1)
2541 + "_" + lex_cast<string>(tick++));
2542
2543 // Synthesize a function.
2544 functiondecl *fdecl = new functiondecl;
2545 fdecl->tok = e->tok;
2546 fdecl->type = type;
2547 fdecl->name = fname;
2548
2549 embeddedcode *ec = new embeddedcode;
2550 ec->tok = e->tok;
2551 ec->code = code;
2552 fdecl->body = ec;
2553
2554 // Give the fdecl an argument for the pointer we're trying to cast
2555 vardecl *v1 = new vardecl;
2556 v1->type = pe_long;
2557 v1->name = "pointer";
2558 v1->tok = e->tok;
2559 fdecl->formal_args.push_back(v1);
2560
6fda2dff
JS
2561 // Any non-literal indexes need to be passed in too.
2562 for (unsigned i = 0; i < e->components.size(); ++i)
2563 if (e->components[i].type == target_symbol::comp_expression_array_index)
2564 {
2565 vardecl *v = new vardecl;
2566 v->type = pe_long;
2567 v->name = "index" + lex_cast<string>(i);
2568 v->tok = e->tok;
2569 fdecl->formal_args.push_back(v);
2570 }
2571
c4ce66a1
JS
2572 if (lvalue)
2573 {
2574 // Modify the fdecl so it carries a second pe_long formal
2575 // argument called "value".
2576
2577 // FIXME: For the time being we only support setting target
2578 // variables which have base types; these are 'pe_long' in
2579 // stap's type vocabulary. Strings and pointers might be
2580 // reasonable, some day, but not today.
2581
2582 vardecl *v2 = new vardecl;
2583 v2->type = pe_long;
2584 v2->name = "value";
2585 v2->tok = e->tok;
2586 fdecl->formal_args.push_back(v2);
2587 }
2588 else
2589 ec->code += "/* pure */";
2590
2591 s.functions[fdecl->name] = fdecl;
2592
2593 // Synthesize a functioncall.
2594 functioncall* n = new functioncall;
2595 n->tok = e->tok;
2596 n->function = fname;
2597 n->referent = 0; // NB: must not resolve yet, to ensure inclusion in session
2598 n->args.push_back(e->operand);
2599
6fda2dff
JS
2600 // Any non-literal indexes need to be passed in too.
2601 for (unsigned i = 0; i < e->components.size(); ++i)
2602 if (e->components[i].type == target_symbol::comp_expression_array_index)
2603 n->args.push_back(require(e->components[i].expr_index));
2604
c4ce66a1
JS
2605 if (lvalue)
2606 {
2607 // Provide the functioncall to our parent, so that it can be
2608 // used to substitute for the assignment node immediately above
2609 // us.
2610 assert(!target_symbol_setter_functioncalls.empty());
2611 *(target_symbol_setter_functioncalls.top()) = n;
2612 }
2613
2614 provide (n);
77de5e9e
GH
2615}
2616
2617
b8da0ad1
FCE
2618void
2619dwarf_derived_probe::printsig (ostream& o) const
2620{
2621 // Instead of just printing the plain locations, we add a PC value
2622 // as a comment as a way of telling e.g. apart multiple inlined
2623 // function instances. This is distinct from the verbose/clog
2624 // output, since this part goes into the cache hash calculations.
2625 sole_location()->print (o);
6d0f3f0c 2626 o << " /* pc=" << section << "+0x" << hex << addr << dec << " */";
b8da0ad1
FCE
2627 printsig_nested (o);
2628}
2629
2630
2631
dc38c0ae 2632void
b20febf3
FCE
2633dwarf_derived_probe::join_group (systemtap_session& s)
2634{
2635 if (! s.dwarf_derived_probes)
2636 s.dwarf_derived_probes = new dwarf_derived_probe_group ();
2637 s.dwarf_derived_probes->enroll (this);
2638}
2639
2640
2641dwarf_derived_probe::dwarf_derived_probe(const string& funcname,
2642 const string& filename,
2643 int line,
91af0778 2644 // module & section specify a relocation
b20febf3
FCE
2645 // base for <addr>, unless section==""
2646 // (equivalently module=="kernel")
2647 const string& module,
2648 const string& section,
2649 // NB: dwfl_addr is the virtualized
2650 // address for this symbol.
2651 Dwarf_Addr dwfl_addr,
2652 // addr is the section-offset for
2653 // actual relocation.
2654 Dwarf_Addr addr,
2655 dwarf_query& q,
37ebca01 2656 Dwarf_Die* scope_die /* may be null */)
1939ea32 2657 : derived_probe (q.base_probe, new probe_point(*q.base_loc) /* .components soon rewritten */ ),
b20febf3 2658 module (module), section (section), addr (addr),
c9bad430
DS
2659 has_return (q.has_return),
2660 has_maxactive (q.has_maxactive),
2661 maxactive_val (q.maxactive_val)
bd2b1e68 2662{
b20febf3 2663 // Assert relocation invariants
4baf0e53 2664 if (section == "" && dwfl_addr != addr) // addr should be absolute
84048984
FCE
2665 throw semantic_error ("missing relocation base against", q.base_loc->tok);
2666 if (section != "" && dwfl_addr == addr) // addr should be an offset
b20febf3 2667 throw semantic_error ("inconsistent relocation address", q.base_loc->tok);
df8fadee 2668
b20febf3 2669 this->tok = q.base_probe->tok;
b95e2b79 2670 this->access_vars = false;
2930abc7 2671
21beacc9
FCE
2672 // XXX: hack for strange g++/gcc's
2673#ifndef USHRT_MAX
2674#define USHRT_MAX 32767
2675#endif
2676
606fd9c8
FCE
2677 // Range limit maxactive() value
2678 if (q.has_maxactive && (q.maxactive_val < 0 || q.maxactive_val > USHRT_MAX))
2679 throw semantic_error ("maxactive value out of range [0,"
2680 + lex_cast<string>(USHRT_MAX) + "]",
2681 q.base_loc->tok);
2682
de688825 2683 // Expand target variables in the probe body
5f0a03a6 2684 if (!null_die(scope_die))
8fc05e57 2685 {
de688825 2686 dwarf_var_expanding_visitor v (q, scope_die, dwfl_addr);
8b095b45 2687 v.replace (this->body);
b95e2b79 2688 this->access_vars = v.visited;
37ebca01
FCE
2689
2690 // If during target-variable-expanding the probe, we added a new block
2691 // of code, add it to the start of the probe.
2692 if (v.add_block)
ba6f838d 2693 this->body = new block(v.add_block, this->body);
37ebca01
FCE
2694 // If when target-variable-expanding the probe, we added a new
2695 // probe, add it in a new file to the list of files to be processed.
2696 if (v.add_probe)
2697 {
2698 stapfile *f = new stapfile;
2699 f->probes.push_back(v.add_probe);
2700 q.sess.files.push_back(f);
2701 }
8fc05e57 2702 }
37ebca01 2703 // else - null scope_die - $target variables will produce an error during translate phase
8fc05e57 2704
0a98fd42
JS
2705 // Save the local variables for listing mode
2706 if (q.sess.listing_mode_vars)
2707 saveargs(scope_die);
2708
5d23847d 2709 // Reset the sole element of the "locations" vector as a
b20febf3
FCE
2710 // "reverse-engineered" form of the incoming (q.base_loc) probe
2711 // point. This allows a user to see what function / file / line
2712 // number any particular match of the wildcards.
2930abc7 2713
a229fcd7 2714 vector<probe_point::component*> comps;
91af0778
FCE
2715 if (q.has_kernel)
2716 comps.push_back (new probe_point::component(TOK_KERNEL));
2717 else if(q.has_module)
2718 comps.push_back (new probe_point::component(TOK_MODULE, new literal_string(module)));
2719 else if(q.has_process)
2720 comps.push_back (new probe_point::component(TOK_PROCESS, new literal_string(module)));
2721 else
2722 assert (0);
b5d77020 2723
db520b00
FCE
2724 string fn_or_stmt;
2725 if (q.has_function_str || q.has_function_num)
2726 fn_or_stmt = "function";
2727 else
2728 fn_or_stmt = "statement";
a229fcd7 2729
b8da0ad1 2730 if (q.has_function_str || q.has_statement_str)
db520b00 2731 {
4cd232e4 2732 string retro_name = funcname;
b20febf3 2733 if (filename != "")
cee35f73 2734 {
fb84c077 2735 retro_name += ("@" + string (filename));
cee35f73 2736 if (line > 0)
fb84c077 2737 retro_name += (":" + lex_cast<string> (line));
cee35f73 2738 }
db520b00
FCE
2739 comps.push_back
2740 (new probe_point::component
2741 (fn_or_stmt, new literal_string (retro_name)));
2742 }
b8da0ad1 2743 else if (q.has_function_num || q.has_statement_num)
db520b00
FCE
2744 {
2745 Dwarf_Addr retro_addr;
2746 if (q.has_function_num)
2747 retro_addr = q.function_num_val;
2748 else
2749 retro_addr = q.statement_num_val;
db520b00
FCE
2750 comps.push_back (new probe_point::component
2751 (fn_or_stmt,
2752 new literal_number(retro_addr))); // XXX: should be hex if possible
37ebca01
FCE
2753
2754 if (q.has_absolute)
2755 comps.push_back (new probe_point::component (TOK_ABSOLUTE));
a229fcd7
GH
2756 }
2757
b8da0ad1
FCE
2758 if (q.has_call)
2759 comps.push_back (new probe_point::component(TOK_CALL));
2760 if (q.has_inline)
2761 comps.push_back (new probe_point::component(TOK_INLINE));
db520b00 2762 if (has_return)
b8da0ad1
FCE
2763 comps.push_back (new probe_point::component(TOK_RETURN));
2764 if (has_maxactive)
2765 comps.push_back (new probe_point::component
2766 (TOK_MAXACTIVE, new literal_number(maxactive_val)));
d9b516ca 2767
5d23847d
FCE
2768 // Overwrite it.
2769 this->sole_location()->components = comps;
2930abc7
FCE
2770}
2771
bd2b1e68 2772
0a98fd42
JS
2773static bool dwarf_type_name(Dwarf_Die& type_die, string& c_type);
2774
2775void
2776dwarf_derived_probe::saveargs(Dwarf_Die* scope_die)
2777{
2778 Dwarf_Die *scopes;
2779 if (!null_die(scope_die) && dwarf_getscopes_die (scope_die, &scopes) == 0)
2780 return;
2781 auto_free free_scopes(scopes);
2782
2783 stringstream argstream;
2784 string type_name;
2785 Dwarf_Attribute type_attr;
2786 Dwarf_Die type_die;
2787
2788 if (has_return &&
2789 dwarf_attr_integrate (scope_die, DW_AT_type, &type_attr) &&
2790 dwarf_formref_die (&type_attr, &type_die) &&
2791 dwarf_type_name(type_die, type_name))
2792 argstream << " $return:" << type_name;
2793
2794 Dwarf_Die arg;
2795 if (dwarf_child (&scopes[0], &arg) == 0)
2796 do
2797 {
2798 switch (dwarf_tag (&arg))
2799 {
2800 case DW_TAG_variable:
2801 case DW_TAG_formal_parameter:
2802 break;
2803
2804 default:
2805 continue;
2806 }
2807
2808 const char *arg_name = dwarf_diename (&arg);
2809 if (!arg_name)
2810 continue;
2811
2812 type_name.clear();
2813 if (!dwarf_attr_integrate (&arg, DW_AT_type, &type_attr) ||
2814 !dwarf_formref_die (&type_attr, &type_die) ||
2815 !dwarf_type_name(type_die, type_name))
2816 continue;
2817
2818 argstream << " $" << arg_name << ":" << type_name;
2819 }
2820 while (dwarf_siblingof (&arg, &arg) == 0);
2821
2822 args = argstream.str();
2823}
2824
2825
2826void
2827dwarf_derived_probe::printargs(std::ostream &o) const
2828{
2829 o << args;
2830}
2831
2832
7a053d3b 2833void
20c6c071 2834dwarf_derived_probe::register_statement_variants(match_node * root,
88e8da38 2835 dwarf_builder * dw,
bbedb0a6 2836 bool unprivileged_ok_p)
bd2b1e68 2837{
bbedb0a6 2838 root->allow_unprivileged(unprivileged_ok_p)->bind(dw);
54efe513
GH
2839}
2840
7a053d3b 2841void
fd6602a0 2842dwarf_derived_probe::register_function_variants(match_node * root,
88e8da38 2843 dwarf_builder * dw,
bbedb0a6 2844 bool unprivileged_ok_p)
bd2b1e68 2845{
bbedb0a6
DB
2846 root->allow_unprivileged(unprivileged_ok_p)->bind(dw);
2847 root->bind(TOK_INLINE)->allow_unprivileged(unprivileged_ok_p)->bind(dw);
2848 root->bind(TOK_CALL)->allow_unprivileged(unprivileged_ok_p)->bind(dw);
2849 root->bind(TOK_RETURN)->allow_unprivileged(unprivileged_ok_p)->bind(dw);
2850 root->bind(TOK_RETURN)->bind_num(TOK_MAXACTIVE)->allow_unprivileged(unprivileged_ok_p)->bind(dw);
bd2b1e68
GH
2851}
2852
7a053d3b 2853void
20c6c071 2854dwarf_derived_probe::register_function_and_statement_variants(match_node * root,
88e8da38 2855 dwarf_builder * dw,
bbedb0a6 2856 bool unprivileged_ok_p)
bd2b1e68
GH
2857{
2858 // Here we match 4 forms:
2859 //
2860 // .function("foo")
2861 // .function(0xdeadbeef)
2862 // .statement("foo")
2863 // .statement(0xdeadbeef)
2864
bbedb0a6
DB
2865 register_function_variants(root->bind_str(TOK_FUNCTION), dw, unprivileged_ok_p);
2866 register_function_variants(root->bind_num(TOK_FUNCTION), dw, unprivileged_ok_p);
2867 register_statement_variants(root->bind_str(TOK_STATEMENT), dw, unprivileged_ok_p);
2868 register_statement_variants(root->bind_num(TOK_STATEMENT), dw, unprivileged_ok_p);
bd2b1e68
GH
2869}
2870
2871void
c4ce66a1 2872dwarf_derived_probe::register_patterns(systemtap_session& s)
bd2b1e68 2873{
c4ce66a1 2874 match_node* root = s.pattern_root;
bd2b1e68
GH
2875 dwarf_builder *dw = new dwarf_builder();
2876
c4ce66a1
JS
2877 update_visitor *filter = new dwarf_cast_expanding_visitor(s, *dw);
2878 s.code_filters.push_back(filter);
2879
20c6c071
GH
2880 register_function_and_statement_variants(root->bind(TOK_KERNEL), dw);
2881 register_function_and_statement_variants(root->bind_str(TOK_MODULE), dw);
37ebca01 2882 root->bind(TOK_KERNEL)->bind_num(TOK_STATEMENT)->bind(TOK_ABSOLUTE)->bind(dw);
0f336e95 2883 root->bind(TOK_KERNEL)->bind_str(TOK_FUNCTION)->bind_str(TOK_LABEL)->bind(dw);
bbedb0a6
DB
2884 root->bind_str(TOK_PROCESS)->bind_str(TOK_FUNCTION)->bind_str(TOK_LABEL)->bind(dw);
2885 register_function_and_statement_variants(root->bind_str(TOK_PROCESS), dw, false/*!unprivileged_ok_p*/);
2886 root->bind_str(TOK_PROCESS)->bind_str(TOK_MARK)->bind(dw);
2887 root->bind_str(TOK_PROCESS)->bind_num(TOK_MARK)->bind(dw);
bd2b1e68
GH
2888}
2889
9020300d
FCE
2890void
2891dwarf_derived_probe::emit_probe_local_init(translator_output * o)
2892{
b95e2b79
MH
2893 if (access_vars)
2894 {
2895 // if accessing $variables, emit bsp cache setup for speeding up
2896 o->newline() << "bspcache(c->unwaddr, c->regs);";
2897 }
9020300d 2898}
2930abc7 2899
b20febf3 2900// ------------------------------------------------------------------------
46b84a80
DS
2901
2902void
b20febf3 2903dwarf_derived_probe_group::enroll (dwarf_derived_probe* p)
46b84a80 2904{
b20febf3 2905 probes_by_module.insert (make_pair (p->module, p));
b8da0ad1
FCE
2906
2907 // XXX: probes put at the same address should all share a
2908 // single kprobe/kretprobe, and have their handlers executed
2909 // sequentially.
b55bc428
FCE
2910}
2911
7a053d3b 2912void
775d51e5 2913dwarf_derived_probe_group::emit_module_decls (systemtap_session& s)
ec4373ff 2914{
b20febf3 2915 if (probes_by_module.empty()) return;
2930abc7 2916
775d51e5
DS
2917 s.op->newline() << "/* ---- dwarf probes ---- */";
2918
2919 // Warn of misconfigured kernels
f41595cc
FCE
2920 s.op->newline() << "#if ! defined(CONFIG_KPROBES)";
2921 s.op->newline() << "#error \"Need CONFIG_KPROBES!\"";
2922 s.op->newline() << "#endif";
775d51e5 2923 s.op->newline();
f41595cc 2924
f07c3b68
FCE
2925 s.op->newline() << "#ifndef KRETACTIVE";
2926 s.op->newline() << "#define KRETACTIVE (max(15,6*NR_CPUS))";
2927 s.op->newline() << "#endif";
2928
b20febf3
FCE
2929 // Forward declare the master entry functions
2930 s.op->newline() << "static int enter_kprobe_probe (struct kprobe *inst,";
2931 s.op->line() << " struct pt_regs *regs);";
2932 s.op->newline() << "static int enter_kretprobe_probe (struct kretprobe_instance *inst,";
2933 s.op->line() << " struct pt_regs *regs);";
2934
42cb22bd
MH
2935 // Emit an array of kprobe/kretprobe pointers
2936 s.op->newline() << "#if defined(STAPCONF_UNREGISTER_KPROBES)";
2937 s.op->newline() << "static void * stap_unreg_kprobes[" << probes_by_module.size() << "];";
2938 s.op->newline() << "#endif";
2939
b20febf3 2940 // Emit the actual probe list.
606fd9c8
FCE
2941
2942 // NB: we used to plop a union { struct kprobe; struct kretprobe } into
2943 // struct stap_dwarf_probe, but it being initialized data makes it add
2944 // hundreds of bytes of padding per stap_dwarf_probe. (PR5673)
4c2732a1 2945 s.op->newline() << "static struct stap_dwarf_kprobe {";
b20febf3 2946 s.op->newline(1) << "union { struct kprobe kp; struct kretprobe krp; } u;";
e4cb375f
MH
2947 s.op->newline() << "#ifdef __ia64__";
2948 s.op->newline() << "struct kprobe dummy;";
2949 s.op->newline() << "#endif";
606fd9c8
FCE
2950 s.op->newline(-1) << "} stap_dwarf_kprobes[" << probes_by_module.size() << "];";
2951 // NB: bss!
2952
4c2732a1 2953 s.op->newline() << "static struct stap_dwarf_probe {";
b0986e7a
DS
2954 s.op->newline(1) << "const unsigned return_p:1;";
2955 s.op->newline() << "const unsigned maxactive_p:1;";
b350f56b 2956 s.op->newline() << "const unsigned optional_p:1;";
b20febf3 2957 s.op->newline() << "unsigned registered_p:1;";
b0986e7a 2958 s.op->newline() << "const unsigned short maxactive_val;";
606fd9c8
FCE
2959
2960 // Let's find some stats for the three embedded strings. Maybe they
2961 // are small and uniform enough to justify putting char[MAX]'s into
2962 // the array instead of relocated char*'s.
2963 size_t module_name_max = 0, section_name_max = 0, pp_name_max = 0;
2964 size_t module_name_tot = 0, section_name_tot = 0, pp_name_tot = 0;
2965 size_t all_name_cnt = probes_by_module.size(); // for average
2966 for (p_b_m_iterator it = probes_by_module.begin(); it != probes_by_module.end(); it++)
2967 {
2968 dwarf_derived_probe* p = it->second;
2969#define DOIT(var,expr) do { \
2970 size_t var##_size = (expr) + 1; \
2971 var##_max = max (var##_max, var##_size); \
2972 var##_tot += var##_size; } while (0)
2973 DOIT(module_name, p->module.size());
2974 DOIT(section_name, p->section.size());
2975 DOIT(pp_name, lex_cast_qstring(*p->sole_location()).size());
2976#undef DOIT
2977 }
2978
2979 // Decide whether it's worthwhile to use char[] or char* by comparing
2980 // the amount of average waste (max - avg) to the relocation data size
2981 // (3 native long words).
2982#define CALCIT(var) \
2983 if ((var##_name_max-(var##_name_tot/all_name_cnt)) < (3 * sizeof(void*))) \
2984 { \
2985 s.op->newline() << "const char " << #var << "[" << var##_name_max << "];"; \
2986 if (s.verbose > 2) clog << "stap_dwarf_probe " << #var \
2987 << "[" << var##_name_max << "]" << endl; \
2988 } \
2989 else \
2990 { \
b0986e7a 2991 s.op->newline() << "const char * const " << #var << ";"; \
606fd9c8
FCE
2992 if (s.verbose > 2) clog << "stap_dwarf_probe *" << #var << endl; \
2993 }
2994
2995 CALCIT(module);
2996 CALCIT(section);
2997 CALCIT(pp);
e6fe60e7 2998#undef CALCIT
606fd9c8 2999
b0986e7a
DS
3000 s.op->newline() << "const unsigned long address;";
3001 s.op->newline() << "void (* const ph) (struct context*);";
b20febf3
FCE
3002 s.op->newline(-1) << "} stap_dwarf_probes[] = {";
3003 s.op->indent(1);
3004
3005 for (p_b_m_iterator it = probes_by_module.begin(); it != probes_by_module.end(); it++)
2930abc7 3006 {
b20febf3
FCE
3007 dwarf_derived_probe* p = it->second;
3008 s.op->newline() << "{";
3009 if (p->has_return)
3010 s.op->line() << " .return_p=1,";
c9bad430 3011 if (p->has_maxactive)
606fd9c8
FCE
3012 {
3013 s.op->line() << " .maxactive_p=1,";
3014 assert (p->maxactive_val >= 0 && p->maxactive_val <= USHRT_MAX);
3015 s.op->line() << " .maxactive_val=" << p->maxactive_val << ",";
3016 }
b350f56b
JS
3017 if (p->locations[0]->optional)
3018 s.op->line() << " .optional_p=1,";
dc38c256 3019 s.op->line() << " .address=(unsigned long)0x" << hex << p->addr << dec << "ULL,";
84048984
FCE
3020 s.op->line() << " .module=\"" << p->module << "\",";
3021 s.op->line() << " .section=\"" << p->section << "\",";
b20febf3
FCE
3022 s.op->line() << " .pp=" << lex_cast_qstring (*p->sole_location()) << ",";
3023 s.op->line() << " .ph=&" << p->name;
3024 s.op->line() << " },";
2930abc7 3025 }
2930abc7 3026
b20febf3
FCE
3027 s.op->newline(-1) << "};";
3028
3029 // Emit the kprobes callback function
3030 s.op->newline();
3031 s.op->newline() << "static int enter_kprobe_probe (struct kprobe *inst,";
3032 s.op->line() << " struct pt_regs *regs) {";
606fd9c8
FCE
3033 // NB: as of PR5673, the kprobe|kretprobe union struct is in BSS
3034 s.op->newline(1) << "int kprobe_idx = ((uintptr_t)inst-(uintptr_t)stap_dwarf_kprobes)/sizeof(struct stap_dwarf_kprobe);";
3035 // Check that the index is plausible
3036 s.op->newline() << "struct stap_dwarf_probe *sdp = &stap_dwarf_probes[";
3037 s.op->line() << "((kprobe_idx >= 0 && kprobe_idx < " << probes_by_module.size() << ")?";
3038 s.op->line() << "kprobe_idx:0)"; // NB: at least we avoid memory corruption
3039 // XXX: it would be nice to give a more verbose error though; BUG_ON later?
3040 s.op->line() << "];";
c12d974f 3041 common_probe_entryfn_prologue (s.op, "STAP_SESSION_RUNNING", "sdp->pp");
b20febf3 3042 s.op->newline() << "c->regs = regs;";
6415ddde
MW
3043
3044 // Make it look like the IP is set as it wouldn't have been replaced
3045 // by a breakpoint instruction when calling real probe handler. Reset
3046 // IP regs on return, so we don't confuse kprobes. PR10458
3d988997
MW
3047 // But only for architectures where REG_IP is a proper lvalue. PR10491
3048 s.op->newline() << "#ifdef REG_IP_LVALUE";
6415ddde
MW
3049 s.op->newline() << "{";
3050 s.op->indent(1);
3051 s.op->newline() << "unsigned long kprobes_ip = REG_IP(c->regs);";
3052 s.op->newline() << "REG_IP(regs) = (unsigned long) inst->addr;";
b20febf3 3053 s.op->newline() << "(*sdp->ph) (c);";
6415ddde
MW
3054 s.op->newline() << "REG_IP(regs) = kprobes_ip;";
3055 s.op->newline(-1) << "}";
3d988997
MW
3056 s.op->newline() << "#else";
3057 s.op->newline() << "(*sdp->ph) (c);";
3058 s.op->newline() << "#endif";
6415ddde 3059
b20febf3
FCE
3060 common_probe_entryfn_epilogue (s.op);
3061 s.op->newline() << "return 0;";
3062 s.op->newline(-1) << "}";
3063
3064 // Same for kretprobes
3065 s.op->newline();
3066 s.op->newline() << "static int enter_kretprobe_probe (struct kretprobe_instance *inst,";
3067 s.op->line() << " struct pt_regs *regs) {";
3068 s.op->newline(1) << "struct kretprobe *krp = inst->rp;";
606fd9c8
FCE
3069
3070 // NB: as of PR5673, the kprobe|kretprobe union struct is in BSS
a36378d7 3071 s.op->newline() << "int kprobe_idx = ((uintptr_t)krp-(uintptr_t)stap_dwarf_kprobes)/sizeof(struct stap_dwarf_kprobe);";
606fd9c8
FCE
3072 // Check that the index is plausible
3073 s.op->newline() << "struct stap_dwarf_probe *sdp = &stap_dwarf_probes[";
3074 s.op->line() << "((kprobe_idx >= 0 && kprobe_idx < " << probes_by_module.size() << ")?";
3075 s.op->line() << "kprobe_idx:0)"; // NB: at least we avoid memory corruption
3076 // XXX: it would be nice to give a more verbose error though; BUG_ON later?
3077 s.op->line() << "];";
3078
c12d974f 3079 common_probe_entryfn_prologue (s.op, "STAP_SESSION_RUNNING", "sdp->pp");
b20febf3
FCE
3080 s.op->newline() << "c->regs = regs;";
3081 s.op->newline() << "c->pi = inst;"; // for assisting runtime's backtrace logic
6415ddde
MW
3082
3083 // Make it look like the IP is set as it wouldn't have been replaced
3084 // by a breakpoint instruction when calling real probe handler. Reset
3085 // IP regs on return, so we don't confuse kprobes. PR10458
3d988997
MW
3086 // But only for architectures where REG_IP is a proper lvalue. PR10491
3087 s.op->newline() << "#ifdef REG_IP_LVALUE";
6415ddde
MW
3088 s.op->newline() << "{";
3089 s.op->indent(1);
3090 s.op->newline() << "unsigned long kprobes_ip = REG_IP(c->regs);";
3091 s.op->newline() << "REG_IP(regs) = (unsigned long) inst->rp->kp.addr;";
b20febf3 3092 s.op->newline() << "(*sdp->ph) (c);";
6415ddde
MW
3093 s.op->newline() << "REG_IP(regs) = kprobes_ip;";
3094 s.op->newline(-1) << "}";
3d988997
MW
3095 s.op->newline() << "#else";
3096 s.op->newline() << "(*sdp->ph) (c);";
3097 s.op->newline() << "#endif";
6415ddde 3098
b20febf3
FCE
3099 common_probe_entryfn_epilogue (s.op);
3100 s.op->newline() << "return 0;";
3101 s.op->newline(-1) << "}";
20c6c071 3102}
ec4373ff 3103
20c6c071 3104
dc38c0ae 3105void
b20febf3
FCE
3106dwarf_derived_probe_group::emit_module_init (systemtap_session& s)
3107{
3108 s.op->newline() << "for (i=0; i<" << probes_by_module.size() << "; i++) {";
3109 s.op->newline(1) << "struct stap_dwarf_probe *sdp = & stap_dwarf_probes[i];";
a36378d7 3110 s.op->newline() << "struct stap_dwarf_kprobe *kp = & stap_dwarf_kprobes[i];";
f1bad60c 3111 s.op->newline() << "unsigned long relocated_addr = _stp_module_relocate (sdp->module, sdp->section, sdp->address);";
b20febf3 3112 s.op->newline() << "if (relocated_addr == 0) continue;"; // quietly; assume module is absent
6d0f3f0c 3113 s.op->newline() << "probe_point = sdp->pp;"; // for error messages
b20febf3 3114 s.op->newline() << "if (sdp->return_p) {";
606fd9c8 3115 s.op->newline(1) << "kp->u.krp.kp.addr = (void *) relocated_addr;";
c9bad430 3116 s.op->newline() << "if (sdp->maxactive_p) {";
606fd9c8 3117 s.op->newline(1) << "kp->u.krp.maxactive = sdp->maxactive_val;";
c9bad430 3118 s.op->newline(-1) << "} else {";
f07c3b68 3119 s.op->newline(1) << "kp->u.krp.maxactive = KRETACTIVE;";
c9bad430 3120 s.op->newline(-1) << "}";
606fd9c8 3121 s.op->newline() << "kp->u.krp.handler = &enter_kretprobe_probe;";
e4cb375f
MH
3122 // to ensure safeness of bspcache, always use aggr_kprobe on ia64
3123 s.op->newline() << "#ifdef __ia64__";
3124 s.op->newline() << "kp->dummy.addr = kp->u.krp.kp.addr;";
3125 s.op->newline() << "kp->dummy.pre_handler = NULL;";
3126 s.op->newline() << "rc = register_kprobe (& kp->dummy);";
3127 s.op->newline() << "if (rc == 0) {";
3128 s.op->newline(1) << "rc = register_kretprobe (& kp->u.krp);";
3129 s.op->newline() << "if (rc != 0)";
3130 s.op->newline(1) << "unregister_kprobe (& kp->dummy);";
3131 s.op->newline(-2) << "}";
3132 s.op->newline() << "#else";
606fd9c8 3133 s.op->newline() << "rc = register_kretprobe (& kp->u.krp);";
e4cb375f 3134 s.op->newline() << "#endif";
b20febf3 3135 s.op->newline(-1) << "} else {";
e4cb375f 3136 // to ensure safeness of bspcache, always use aggr_kprobe on ia64
606fd9c8
FCE
3137 s.op->newline(1) << "kp->u.kp.addr = (void *) relocated_addr;";
3138 s.op->newline() << "kp->u.kp.pre_handler = &enter_kprobe_probe;";
e4cb375f
MH
3139 s.op->newline() << "#ifdef __ia64__";
3140 s.op->newline() << "kp->dummy.addr = kp->u.kp.addr;";
3141 s.op->newline() << "kp->dummy.pre_handler = NULL;";
3142 s.op->newline() << "rc = register_kprobe (& kp->dummy);";
3143 s.op->newline() << "if (rc == 0) {";
3144 s.op->newline(1) << "rc = register_kprobe (& kp->u.kp);";
3145 s.op->newline() << "if (rc != 0)";
3146 s.op->newline(1) << "unregister_kprobe (& kp->dummy);";
3147 s.op->newline(-2) << "}";
3148 s.op->newline() << "#else";
606fd9c8 3149 s.op->newline() << "rc = register_kprobe (& kp->u.kp);";
e4cb375f 3150 s.op->newline() << "#endif";
b20febf3 3151 s.op->newline(-1) << "}";
9063462a
FCE
3152 s.op->newline() << "if (rc) {"; // PR6749: tolerate a failed register_*probe.
3153 s.op->newline(1) << "sdp->registered_p = 0;";
b350f56b 3154 s.op->newline() << "if (!sdp->optional_p)";
4a4edc21 3155 s.op->newline(1) << "_stp_warn (\"probe %s (address 0x%lx) registration error (rc %d)\", probe_point, relocated_addr, rc);";
b350f56b 3156 s.op->newline(-1) << "rc = 0;"; // continue with other probes
9063462a
FCE
3157 // XXX: shall we increment numskipped?
3158 s.op->newline(-1) << "}";
3159
3160#if 0 /* pre PR 6749; XXX consider making an option */
c48cb0cc 3161 s.op->newline(1) << "for (j=i-1; j>=0; j--) {"; // partial rollback
b20febf3 3162 s.op->newline(1) << "struct stap_dwarf_probe *sdp2 = & stap_dwarf_probes[j];";
606fd9c8
FCE
3163 s.op->newline() << "struct stap_dwarf_kprobe *kp2 = & stap_dwarf_kprobes[j];";
3164 s.op->newline() << "if (sdp2->return_p) unregister_kretprobe (&kp2->u.krp);";
3165 s.op->newline() << "else unregister_kprobe (&kp2->u.kp);";
e4cb375f
MH
3166 s.op->newline() << "#ifdef __ia64__";
3167 s.op->newline() << "unregister_kprobe (&kp2->dummy);";
3168 s.op->newline() << "#endif";
c48cb0cc
FCE
3169 // NB: we don't have to clear sdp2->registered_p, since the module_exit code is
3170 // not run for this early-abort case.
3171 s.op->newline(-1) << "}";
3172 s.op->newline() << "break;"; // don't attempt to register any more probes
b20febf3 3173 s.op->newline(-1) << "}";
9063462a
FCE
3174#endif
3175
b20febf3
FCE
3176 s.op->newline() << "else sdp->registered_p = 1;";
3177 s.op->newline(-1) << "}"; // for loop
dc38c0ae
DS
3178}
3179
3180
46b84a80 3181void
b20febf3 3182dwarf_derived_probe_group::emit_module_exit (systemtap_session& s)
46b84a80 3183{
42cb22bd
MH
3184 //Unregister kprobes by batch interfaces.
3185 s.op->newline() << "#if defined(STAPCONF_UNREGISTER_KPROBES)";
3186 s.op->newline() << "j = 0;";
3187 s.op->newline() << "for (i=0; i<" << probes_by_module.size() << "; i++) {";
3188 s.op->newline(1) << "struct stap_dwarf_probe *sdp = & stap_dwarf_probes[i];";
3189 s.op->newline() << "struct stap_dwarf_kprobe *kp = & stap_dwarf_kprobes[i];";
3190 s.op->newline() << "if (! sdp->registered_p) continue;";
3191 s.op->newline() << "if (!sdp->return_p)";
3192 s.op->newline(1) << "stap_unreg_kprobes[j++] = &kp->u.kp;";
3193 s.op->newline(-2) << "}";
3194 s.op->newline() << "unregister_kprobes((struct kprobe **)stap_unreg_kprobes, j);";
3195 s.op->newline() << "j = 0;";
3196 s.op->newline() << "for (i=0; i<" << probes_by_module.size() << "; i++) {";
3197 s.op->newline(1) << "struct stap_dwarf_probe *sdp = & stap_dwarf_probes[i];";
3198 s.op->newline() << "struct stap_dwarf_kprobe *kp = & stap_dwarf_kprobes[i];";
3199 s.op->newline() << "if (! sdp->registered_p) continue;";
3200 s.op->newline() << "if (sdp->return_p)";
3201 s.op->newline(1) << "stap_unreg_kprobes[j++] = &kp->u.krp;";
3202 s.op->newline(-2) << "}";
3203 s.op->newline() << "unregister_kretprobes((struct kretprobe **)stap_unreg_kprobes, j);";
e4cb375f
MH
3204 s.op->newline() << "#ifdef __ia64__";
3205 s.op->newline() << "j = 0;";
3206 s.op->newline() << "for (i=0; i<" << probes_by_module.size() << "; i++) {";
3207 s.op->newline(1) << "struct stap_dwarf_probe *sdp = & stap_dwarf_probes[i];";
3208 s.op->newline() << "struct stap_dwarf_kprobe *kp = & stap_dwarf_kprobes[i];";
3209 s.op->newline() << "if (! sdp->registered_p) continue;";
3210 s.op->newline() << "stap_unreg_kprobes[j++] = &kp->dummy;";
3211 s.op->newline(-1) << "}";
3212 s.op->newline() << "unregister_kprobes((struct kprobe **)stap_unreg_kprobes, j);";
3213 s.op->newline() << "#endif";
42cb22bd
MH
3214 s.op->newline() << "#endif";
3215
b20febf3
FCE
3216 s.op->newline() << "for (i=0; i<" << probes_by_module.size() << "; i++) {";
3217 s.op->newline(1) << "struct stap_dwarf_probe *sdp = & stap_dwarf_probes[i];";
a36378d7 3218 s.op->newline() << "struct stap_dwarf_kprobe *kp = & stap_dwarf_kprobes[i];";
b20febf3
FCE
3219 s.op->newline() << "if (! sdp->registered_p) continue;";
3220 s.op->newline() << "if (sdp->return_p) {";
42cb22bd 3221 s.op->newline() << "#if !defined(STAPCONF_UNREGISTER_KPROBES)";
606fd9c8 3222 s.op->newline(1) << "unregister_kretprobe (&kp->u.krp);";
42cb22bd 3223 s.op->newline() << "#endif";
606fd9c8 3224 s.op->newline() << "atomic_add (kp->u.krp.nmissed, & skipped_count);";
73209876
FCE
3225 s.op->newline() << "#ifdef STP_TIMING";
3226 s.op->newline() << "if (kp->u.krp.nmissed)";
d01eaa30 3227 s.op->newline(1) << "_stp_warn (\"Skipped due to missed kretprobe/1 on '%s': %d\\n\", sdp->pp, kp->u.krp.nmissed);";
73209876 3228 s.op->newline(-1) << "#endif";
606fd9c8 3229 s.op->newline() << "atomic_add (kp->u.krp.kp.nmissed, & skipped_count);";
73209876
FCE
3230 s.op->newline() << "#ifdef STP_TIMING";
3231 s.op->newline() << "if (kp->u.krp.kp.nmissed)";
d01eaa30 3232 s.op->newline(1) << "_stp_warn (\"Skipped due to missed kretprobe/2 on '%s': %d\\n\", sdp->pp, kp->u.krp.kp.nmissed);";
73209876 3233 s.op->newline(-1) << "#endif";
557fb7a8 3234 s.op->newline(-1) << "} else {";
42cb22bd 3235 s.op->newline() << "#if !defined(STAPCONF_UNREGISTER_KPROBES)";
606fd9c8 3236 s.op->newline(1) << "unregister_kprobe (&kp->u.kp);";
42cb22bd 3237 s.op->newline() << "#endif";
606fd9c8 3238 s.op->newline() << "atomic_add (kp->u.kp.nmissed, & skipped_count);";
73209876
FCE
3239 s.op->newline() << "#ifdef STP_TIMING";
3240 s.op->newline() << "if (kp->u.kp.nmissed)";
d01eaa30 3241 s.op->newline(1) << "_stp_warn (\"Skipped due to missed kprobe on '%s': %d\\n\", sdp->pp, kp->u.kp.nmissed);";
73209876 3242 s.op->newline(-1) << "#endif";
b20febf3 3243 s.op->newline(-1) << "}";
e4cb375f
MH
3244 s.op->newline() << "#if !defined(STAPCONF_UNREGISTER_KPROBES) && defined(__ia64__)";
3245 s.op->newline() << "unregister_kprobe (&kp->dummy);";
3246 s.op->newline() << "#endif";
b20febf3
FCE
3247 s.op->newline() << "sdp->registered_p = 0;";
3248 s.op->newline(-1) << "}";
46b84a80
DS
3249}
3250
7a05f484
SC
3251struct sdt_var_expanding_visitor: public var_expanding_visitor
3252{
9f02b156 3253 sdt_var_expanding_visitor(string & process_name, string & probe_name,
0c3bfb1e 3254 int arg_count, bool have_reg_args, bool utrace_probe):
9f02b156 3255 process_name (process_name), probe_name (probe_name),
0c3bfb1e
SC
3256 have_reg_args (have_reg_args), utrace_probe (utrace_probe),
3257 arg_count (arg_count)
a8ec7719
JS
3258 {
3259 assert(!have_reg_args || (arg_count >= 0 && arg_count <= 10));
3260 }
56e33af5
SC
3261 string & process_name;
3262 string & probe_name;
7a05f484 3263 bool have_reg_args;
0c3bfb1e 3264 bool utrace_probe;
7a05f484
SC
3265 int arg_count;
3266
3267 void visit_target_symbol (target_symbol* e);
3268};
3269
3270void
3271sdt_var_expanding_visitor::visit_target_symbol (target_symbol *e)
3272{
3273 if (e->base_name == "$$name")
3274 {
03c75a4a
JS
3275 if (e->addressof)
3276 throw semantic_error("cannot take address of sdt variable", e->tok);
3277
7a05f484
SC
3278 literal_string *myname = new literal_string (probe_name);
3279 myname->tok = e->tok;
3280 provide(myname);
3281 return;
3282 }
3283 else if (e->base_name.find("$arg") == string::npos || ! have_reg_args)
3284 {
3285 provide(e);
3286 return;
3287 }
a8ec7719
JS
3288
3289 int argno = lex_cast<int>(e->base_name.substr(4));
3290 if (argno < 1 || argno > arg_count)
3291 throw semantic_error ("invalid argument number", e->tok);
3292
7a05f484 3293 bool lvalue = is_active_lvalue(e);
7a05f484
SC
3294 functioncall *fc = new functioncall;
3295
5111fc3e
SC
3296 // First two args are hidden: 1. pointer to probe name 2. task id
3297 if (arg_count < 2)
7a05f484 3298 {
0c3bfb1e 3299 fc->function = utrace_probe ? "_utrace_syscall_arg" : "ulong_arg";
7a05f484
SC
3300 fc->type = pe_long;
3301 fc->tok = e->tok;
0c3bfb1e 3302 literal_number* num = new literal_number(argno + (utrace_probe ? 1 : 2));
7a05f484
SC
3303 num->tok = e->tok;
3304 fc->args.push_back(num);
3305 }
3306 else // args passed as a struct
3307 {
3308 fc->function = "user_long";
3309 fc->tok = e->tok;
3310 binary_expression *be = new binary_expression;
3311 be->tok = e->tok;
3312 functioncall *get_arg1 = new functioncall;
0c3bfb1e 3313 get_arg1->function = utrace_probe ? "_utrace_syscall_arg" : "pointer_arg";
7a05f484 3314 get_arg1->tok = e->tok;
0c3bfb1e 3315 literal_number* num = new literal_number((utrace_probe ? 2 : 3));
7a05f484
SC
3316 num->tok = e->tok;
3317 get_arg1->args.push_back(num);
3318
3319 be->left = get_arg1;
3320 be->op = "+";
3321 literal_number* inc = new literal_number((argno - 1) * 8);
3322 be->right = inc;
3323 fc->args.push_back(be);
3324 }
ad002306 3325
7a05f484
SC
3326 if (lvalue)
3327 *(target_symbol_setter_functioncalls.top()) = fc;
3328
ad002306
SC
3329 if (e->components.empty())
3330 {
03c75a4a
JS
3331 if (e->addressof)
3332 throw semantic_error("cannot take address of sdt variable", e->tok);
3333
ad002306
SC
3334 provide(fc);
3335 return;
3336 }
7a05f484
SC
3337 cast_op *cast = new cast_op;
3338 cast->base_name = "@cast";
3339 cast->tok = e->tok;
3340 cast->operand = fc;
3341 cast->components = e->components;
ad002306 3342 cast->type = probe_name + "_arg" + lex_cast<string>(argno);
56e33af5 3343 cast->module = process_name;
7a05f484 3344
6fda2dff 3345 cast->visit(this);
7a05f484 3346}
46b84a80 3347
edce5b67
JS
3348
3349struct sdt_query : public base_query
3350{
3351 sdt_query(probe * base_probe, probe_point * base_loc,
3352 dwflpp & dw, literal_map_t const & params,
3353 vector<derived_probe *> & results);
3354
3355 void handle_query_module();
3356
3357private:
3358 enum probe_types
3359 {
3360 uprobe_type = 0x31425250, // "PRB1"
3361 kprobe_type = 0x32425250, // "PRB2"
3362 utrace_type = 0x33425250, // "PRB3"
3363 } probe_type;
3364
3365 probe * base_probe;
3366 probe_point * base_loc;
3367 vector<derived_probe *> & results;
3368 string mark_name;
3369
3370 set<string> probes_handled;
3371
3372 Elf_Data *pdata;
3373 size_t probe_scn_offset;
3374 size_t probe_scn_addr;
3375 uint64_t probe_arg;
3376 string probe_name;
3377
3378 bool init_probe_scn();
3379 bool get_next_probe();
3380
3381 void convert_probe(probe *base);
3382 void convert_location(probe *base, probe_point *location);
3383};
3384
3385
3386sdt_query::sdt_query(probe * base_probe, probe_point * base_loc,
3387 dwflpp & dw, literal_map_t const & params,
3388 vector<derived_probe *> & results):
3389 base_query(dw, params), base_probe(base_probe),
3390 base_loc(base_loc), results(results)
3391{
3392 assert(get_string_param(params, TOK_MARK, mark_name));
3393}
3394
3395
3396void
3397sdt_query::handle_query_module()
3398{
3399 if (!init_probe_scn())
3400 return;
3401
3402 if (sess.verbose > 3)
3403 clog << "TOK_MARK: " << mark_name << endl;
3404
3405 while (get_next_probe())
3406 {
696ec154
SC
3407 if (probe_type != uprobe_type
3408 && !probes_handled.insert(probe_name).second)
edce5b67
JS
3409 continue;
3410
3411 probe *new_base = new probe(*base_probe);
3412 probe_point *new_location = new probe_point(*base_loc);
3413 convert_location(new_base, new_location);
3414 new_base->body = deep_copy_visitor::deep_copy(base_probe->body);
3415
3416 bool have_reg_args = false;
3417 if (probe_type == kprobe_type || probe_type == utrace_type)
3418 {
3419 convert_probe(new_base);
3420 have_reg_args = true;
3421 }
3422
3423 // Expand the local variables in the probe body
3424 sdt_var_expanding_visitor svv (module_val, probe_name,
3425 probe_arg, have_reg_args,
3426 probe_type == utrace_type);
8b095b45 3427 svv.replace (new_base->body);
edce5b67
JS
3428
3429 unsigned i = results.size();
3430
3431 if (probe_type == kprobe_type || probe_type == utrace_type)
3432 derive_probes(sess, new_base, results);
3433 else
3434 {
3435 literal_map_t params;
3436 for (unsigned i = 0; i < new_location->components.size(); ++i)
3437 {
3438 probe_point::component *c = new_location->components[i];
3439 params[c->functor] = c->arg;
3440 }
3441
696ec154 3442 dwarf_query q(new_base, new_location, dw, params, results);
edce5b67
JS
3443 q.has_mark = true; // enables mid-statement probing
3444 dw.iterate_over_modules(&query_module, &q);
3445 }
3446
3447 if (sess.listing_mode)
3448 {
3449 // restore the locations to print a nicer probe name
3450 probe_point loc(*base_loc);
3451 loc.components[1] =
3452 new probe_point::component(TOK_MARK, new literal_string (probe_name));
3453 for (; i < results.size(); ++i)
3454 for (unsigned j = 0; j < results[i]->locations.size(); ++j)
3455 *results[i]->locations[j] = loc;
3456 }
3457 }
3458}
3459
3460
3461bool
3462sdt_query::init_probe_scn()
3463{
3464 Elf* elf;
3465 GElf_Shdr shdr_mem;
3466 GElf_Shdr *shdr = NULL;
3467 Dwarf_Addr bias;
3468 size_t shstrndx;
3469
3470 // Explicitly look in the main elf file first.
3471 elf = dwfl_module_getelf (dw.module, &bias);
3472 Elf_Scn *probe_scn = NULL;
3473
3474 dwfl_assert ("getshdrstrndx", elf_getshdrstrndx (elf, &shstrndx));
3475
3476 bool have_probes = false;
3477
3478 // Is there a .probes section?
3479 while ((probe_scn = elf_nextscn (elf, probe_scn)))
3480 {
3481 shdr = gelf_getshdr (probe_scn, &shdr_mem);
3482 assert (shdr != NULL);
3483
3484 if (strcmp (elf_strptr (elf, shstrndx, shdr->sh_name), ".probes") == 0)
3485 {
3486 have_probes = true;
3487 break;
3488 }
3489 }
3490
3491 // Older versions put .probes section in the debuginfo dwarf file,
3492 // so check if it actually exists, if not take a look in the debuginfo file
3493 if (! have_probes || (have_probes && shdr->sh_type == SHT_NOBITS))
3494 {
3495 elf = dwarf_getelf (dwfl_module_getdwarf (dw.module, &bias));
3496 if (! elf)
3497 return false;
3498 dwfl_assert ("getshdrstrndx", elf_getshdrstrndx (elf, &shstrndx));
3499 probe_scn = NULL;
3500 while ((probe_scn = elf_nextscn (elf, probe_scn)))
3501 {
3502 shdr = gelf_getshdr (probe_scn, &shdr_mem);
3503 if (strcmp (elf_strptr (elf, shstrndx, shdr->sh_name),
3504 ".probes") == 0)
3505 have_probes = true;
3506 break;
3507 }
3508 }
3509
3510 if (!have_probes)
3511 return false;
3512
3513 pdata = elf_getdata_rawchunk (elf, shdr->sh_offset, shdr->sh_size, ELF_T_BYTE);
3514 probe_scn_offset = 0;
3515 probe_scn_addr = shdr->sh_addr;
3516 assert (pdata != NULL);
3517 if (sess.verbose > 4)
3518 clog << "got .probes elf scn_addr@0x" << probe_scn_addr << dec
3519 << ", size: " << pdata->d_size << endl;
3520 return true;
3521}
3522
3523bool
3524sdt_query::get_next_probe()
3525{
3526 // Extract probe info from the .probes section
3527
3528 while (probe_scn_offset < pdata->d_size)
3529 {
3530 struct probe_entry
3531 {
3532 __uint64_t name;
3533 __uint64_t arg;
3534 } *pbe;
3535 __uint32_t *type = (__uint32_t*) ((char*)pdata->d_buf + probe_scn_offset);
3536 probe_type = (enum probe_types)*type;
3537 if (probe_type != uprobe_type && probe_type != kprobe_type
3538 && probe_type != utrace_type)
3539 {
3540 // Unless this is a mangled .probes section, this happens
3541 // because the name of the probe comes first, followed by
3542 // the sentinel.
3543 if (sess.verbose > 5)
3544 clog << "got unknown probe_type: 0x" << hex << probe_type
3545 << dec << endl;
3546 probe_scn_offset += sizeof(__uint32_t);
3547 continue;
3548 }
3549 probe_scn_offset += sizeof(__uint32_t);
3550 probe_scn_offset += probe_scn_offset % sizeof(__uint64_t);
3551 pbe = (struct probe_entry*) ((char*)pdata->d_buf + probe_scn_offset);
3552 probe_name = (char*)((char*)pdata->d_buf + pbe->name - (char*)probe_scn_addr);
3553 probe_arg = pbe->arg;
3554 if (sess.verbose > 4)
3555 clog << "saw .probes " << probe_name
3556 << "@0x" << hex << probe_arg << dec << endl;
3557
3558 probe_scn_offset += sizeof (struct probe_entry);
3559 if ((mark_name == probe_name)
3560 || (dw.name_has_wildcard (mark_name)
3561 && dw.function_name_matches_pattern (probe_name, mark_name)))
3562 {
3563 if (sess.verbose > 3)
3564 clog << "found probe_name" << probe_name << " at 0x"
3565 << hex << probe_arg << dec << endl;
3566 return true;
3567 }
3568 else
3569 continue;
3570 }
3571 return false;
3572}
3573
3574
3575void
3576sdt_query::convert_probe (probe *base)
3577{
3578 block *b = new block;
3579 b->tok = base->body->tok;
3580
3581 // XXX: Does this also need to happen for i386 under x86_64 stap?
3582#ifdef __i386__
3583 if (probe_type == kprobe_type)
3584 {
3585 functioncall *rp = new functioncall;
3586 rp->tok = b->tok;
3587 rp->function = "regparm";
3588 rp->tok = b->tok;
3589 literal_number* littid = new literal_number(0);
3590 littid->tok = b->tok;
3591 rp->args.push_back(littid);
3592 expr_statement* es = new expr_statement;
3593 es->tok = b->tok;
3594 es->value = rp;
3595 b->statements.push_back(es);
3596 }
3597#endif
3598
3599 if (probe_type == utrace_type)
3600 {
3601 // Generate: if ($syscall != 0xbead) next;
3602 if_statement *issc = new if_statement;
3603 issc->thenblock = new next_statement;
3604 issc->elseblock = NULL;
3605 issc->tok = b->tok;
3606 comparison *besc = new comparison;
3607 besc->op = "!=";
3608 besc->tok = b->tok;
3609 functioncall* n = new functioncall;
3610 n->tok = b->tok;
3611 n->function = "_utrace_syscall_nr";
3612 n->referent = 0;
3613 besc->left = n;
3614 literal_number* fake_syscall = new literal_number(0xbead);
3615 fake_syscall->tok = b->tok;
3616 besc->right = fake_syscall;
3617 issc->condition = besc;
3618 b->statements.push_back(issc);
3619 }
3620 else if (probe_type == kprobe_type)
3621 {
3622 // Generate: if (arg2 != kprobe_type) next;
3623 if_statement *istid = new if_statement;
3624 istid->thenblock = new next_statement;
3625 istid->elseblock = NULL;
3626 istid->tok = b->tok;
3627 comparison *betid = new comparison;
3628 betid->op = "!=";
3629 betid->tok = b->tok;
3630
3631 functioncall *arg2 = new functioncall;
3632 arg2->function = "ulong_arg";
3633 arg2->tok = b->tok;
3634 literal_number* num = new literal_number(2);
3635 num->tok = b->tok;
3636 arg2->args.push_back(num);
3637
3638 betid->left = arg2;
3639 literal_number* littid = new literal_number(kprobe_type);
3640 littid->tok = b->tok;
3641 betid->right = littid;
3642 istid->condition = betid;
3643 b->statements.push_back(istid);
3644 }
3645
3646 // Generate: if (arg1 != mark("label")) next;
3647 functioncall *fc = new functioncall;
3648 fc->function = (probe_type == utrace_type) ? "_utrace_syscall_arg" : "ulong_arg";
3649 fc->tok = b->tok;
3650 literal_number* num = new literal_number((probe_type == utrace_type) ? 0 : 1);
3651 num->tok = b->tok;
3652 fc->args.push_back(num);
3653
3654 functioncall *fcus = new functioncall;
3655 fcus->function = "user_string";
3656 fcus->type = pe_string;
3657 fcus->tok = b->tok;
3658 fcus->args.push_back(fc);
3659
3660 if_statement *is = new if_statement;
3661 is->thenblock = new next_statement;
3662 is->elseblock = NULL;
3663 is->tok = b->tok;
3664 comparison *be = new comparison;
3665 be->op = "!=";
3666 be->tok = b->tok;
3667 be->left = fcus;
3668 be->right = new literal_string(probe_name);
3669 is->condition = be;
3670 b->statements.push_back(is);
3671
3672 // Now replace the body
3673 b->statements.push_back(base->body);
3674 base->body = b;
3675}
3676
3677
3678void
3679sdt_query::convert_location (probe *base, probe_point *location)
3680{
3681 switch (probe_type)
3682 {
3683 case uprobe_type:
3684 if (sess.verbose > 3)
3685 clog << "probe_type == uprobe_type, use statement addr: 0x"
3686 << hex << probe_arg << dec << endl;
3687 // process("executable").statement(probe_arg)
3688 location->components[1]->functor = TOK_STATEMENT;
3689 location->components[1]->arg = new literal_number(probe_arg);
3690 break;
3691
3692 case kprobe_type:
3693 if (sess.verbose > 3)
3694 clog << "probe_type == kprobe_type" << endl;
3695 // kernel.function("*getegid*")
3696 location->components[0]->functor = TOK_KERNEL;
3697 location->components[0]->arg = NULL;
3698 location->components[1]->functor = TOK_FUNCTION;
3699 location->components[1]->arg = new literal_string("*getegid*");
3700 break;
3701
3702 case utrace_type:
3703 if (sess.verbose > 3)
3704 clog << "probe_type == utrace_type" << endl;
3705 // process("executable").syscall
3706 location->components[1]->functor = "syscall";
3707 location->components[1]->arg = NULL;
3708 break;
3709
3710 default:
3711 if (sess.verbose > 3)
3712 clog << "probe_type == use_uprobe_no_dwarf, use label name: "
3713 << "_stapprobe1_" << mark_name << endl;
3714 // process("executable").function("*").label("_stapprobe1_MARK_NAME")
3715 location->components[1]->functor = TOK_FUNCTION;
3716 location->components[1]->arg = new literal_string("*");
3717 location->components.push_back(new probe_point::component(TOK_LABEL));
3718 location->components[2]->arg = new literal_string("_stapprobe1_" + mark_name);
3719 break;
3720 }
3721
3722 base->locations.clear();
3723 base->locations.push_back(location);
3724}
3725
3726
20c6c071 3727void
5227f1ea 3728dwarf_builder::build(systemtap_session & sess,
7a053d3b 3729 probe * base,
20c6c071 3730 probe_point * location,
86bf665e 3731 literal_map_t const & parameters,
20c6c071
GH
3732 vector<derived_probe *> & finished_results)
3733{
b20febf3
FCE
3734 // NB: the kernel/user dwlfpp objects are long-lived.
3735 // XXX: but they should be per-session, as this builder object
3736 // may be reused if we try to cross-instrument multiple targets.
84048984 3737
7a24d422
FCE
3738 dwflpp* dw = 0;
3739
7a24d422 3740 string module_name;
ae2552da
FCE
3741 if (has_null_param (parameters, TOK_KERNEL))
3742 {
3743 dw = get_kern_dw(sess, "kernel");
3744 }
3745 else if (get_param (parameters, TOK_MODULE, module_name))
b8da0ad1 3746 {
ae2552da 3747 dw = get_kern_dw(sess, module_name);
b8da0ad1 3748 }
7a24d422 3749 else if (get_param (parameters, TOK_PROCESS, module_name))
b8da0ad1 3750 {
d0a7f5a9
FCE
3751 module_name = find_executable (module_name); // canonicalize it
3752
7a24d422
FCE
3753 // user-space target; we use one dwflpp instance per module name
3754 // (= program or shared library)
707bf35e 3755 dw = get_user_dw(sess, module_name);
c8959a29 3756 }
20c6c071 3757
5896cd05
MW
3758 if (sess.verbose > 3)
3759 clog << "dwarf_builder::build for " << module_name << endl;
3760
3e1e31fb
JS
3761 string mark_name;
3762 if (get_param(parameters, TOK_MARK, mark_name))
f28a8c28 3763 {
edce5b67
JS
3764 sdt_query sdtq(base, location, *dw, parameters, finished_results);
3765 dw->iterate_over_modules(&query_module, &sdtq);
3766 return;
7a05f484 3767 }
20c6c071 3768
edce5b67 3769 dwarf_query q(base, location, *dw, parameters, finished_results);
7a24d422
FCE
3770
3771 // XXX: kernel.statement.absolute is a special case that requires no
3772 // dwfl processing. This code should be in a separate builder.
7a24d422 3773 if (q.has_kernel && q.has_absolute)
37ebca01 3774 {
4baf0e53 3775 // assert guru mode for absolute probes
37ebca01
FCE
3776 if (! q.base_probe->privileged)
3777 {
edce5b67
JS
3778 throw semantic_error ("absolute statement probe in unprivileged script",
3779 q.base_probe->tok);
37ebca01
FCE
3780 }
3781
3782 // For kernel.statement(NUM).absolute probe points, we bypass
3783 // all the debuginfo stuff: We just wire up a
3784 // dwarf_derived_probe right here and now.
4baf0e53 3785 dwarf_derived_probe* p =
b8da0ad1
FCE
3786 new dwarf_derived_probe ("", "", 0, "kernel", "",
3787 q.statement_num_val, q.statement_num_val,
3788 q, 0);
37ebca01 3789 finished_results.push_back (p);
1a0dbc5a 3790 sess.unwindsym_modules.insert ("kernel");
37ebca01
FCE
3791 return;
3792 }
3793
51178501 3794 dw->iterate_over_modules(&query_module, &q);
5f0a03a6
JK
3795}
3796
3797symbol_table::~symbol_table()
3798{
1c6b77e5
JS
3799 for (iterator_t i = map_by_addr.begin(); i != map_by_addr.end(); ++i)
3800 delete i->second;
5f0a03a6
JK
3801}
3802
3803void
ab91b232
JK
3804symbol_table::add_symbol(const char *name, bool weak, Dwarf_Addr addr,
3805 Dwarf_Addr *high_addr)
5f0a03a6 3806{
ab91b232
JK
3807#ifdef __powerpc__
3808 // Map ".sys_foo" to "sys_foo".
3809 if (name[0] == '.')
3810 name++;
3811#endif
5f0a03a6
JK
3812 func_info *fi = new func_info();
3813 fi->addr = addr;
3814 fi->name = name;
ab91b232 3815 fi->weak = weak;
5f0a03a6
JK
3816 map_by_name[fi->name] = fi;
3817 // TODO: Use a multimap in case there are multiple static
3818 // functions with the same name?
1c6b77e5 3819 map_by_addr.insert(make_pair(addr, fi));
5f0a03a6
JK
3820}
3821
3822enum info_status
3823symbol_table::read_symbols(FILE *f, const string& path)
3824{
3825 // Based on do_kernel_symbols() in runtime/staprun/symbols.c
3826 int ret;
2e67a43b
TM
3827 char *name = 0;
3828 char *mod = 0;
5f0a03a6
JK
3829 char type;
3830 unsigned long long addr;
3831 Dwarf_Addr high_addr = 0;
3832 int line = 0;
3833
3834 // %as (non-POSIX) mallocs space for the string and stores its address.
3835 while ((ret = fscanf(f, "%llx %c %as [%as", &addr, &type, &name, &mod)) > 0)
3836 {
2e67a43b
TM
3837 auto_free free_name(name);
3838 auto_free free_mod(mod);
5f0a03a6
JK
3839 line++;
3840 if (ret < 3)
3841 {
41c262f3 3842 cerr << "Symbol table error: Line "
5f0a03a6
JK
3843 << line
3844 << " of symbol list from "
3845 << path
3846 << " is not in correct format: address type name [module]";
3847 // Caller should delete symbol_table object.
3848 return info_absent;
3849 }
2e67a43b 3850 else if (ret > 3)
5f0a03a6
JK
3851 {
3852 // Modules are loaded above the kernel, so if we're getting
3853 // modules, we're done.
2e67a43b 3854 break;
5f0a03a6 3855 }
ab91b232
JK
3856 if (type == 'T' || type == 't' || type == 'W')
3857 add_symbol(name, (type == 'W'), (Dwarf_Addr) addr, &high_addr);
5f0a03a6
JK
3858 }
3859
1c6b77e5 3860 if (map_by_addr.size() < 1)
5f0a03a6
JK
3861 {
3862 cerr << "Symbol table error: "
3863 << path << " contains no function symbols." << endl;
3864 return info_absent;
3865 }
3866 return info_present;
3867}
3868
3869// NB: This currently unused. We use get_from_elf() instead because
3870// that gives us raw addresses -- which we need for modules -- whereas
3871// nm provides the address relative to the beginning of the section.
3872enum info_status
83ca3872
MW
3873symbol_table::read_from_elf_file(const string &path,
3874 const systemtap_session &sess)
5f0a03a6
JK
3875{
3876 FILE *f;
3877 string cmd = string("/usr/bin/nm -n --defined-only ") + path;
3878 f = popen(cmd.c_str(), "r");
3879 if (!f)
3880 {
3881 // nm failures are detected by pclose, not popen.
3882 cerr << "Internal error reading symbol table from "
3883 << path << " -- " << strerror (errno);
3884 return info_absent;
3885 }
3886 enum info_status status = read_symbols(f, path);
3887 if (pclose(f) != 0)
3888 {
83ca3872 3889 if (status == info_present && ! sess.suppress_warnings)
5f0a03a6
JK
3890 cerr << "Warning: nm cannot read symbol table from " << path;
3891 return info_absent;
3892 }
3893 return status;
3894}
3895
3896enum info_status
83ca3872
MW
3897symbol_table::read_from_text_file(const string& path,
3898 const systemtap_session &sess)
5f0a03a6
JK
3899{
3900 FILE *f = fopen(path.c_str(), "r");
3901 if (!f)
3902 {
83ca3872
MW
3903 if (! sess.suppress_warnings)
3904 cerr << "Warning: cannot read symbol table from "
3905 << path << " -- " << strerror (errno);
5f0a03a6
JK
3906 return info_absent;
3907 }
3908 enum info_status status = read_symbols(f, path);
3909 (void) fclose(f);
3910 return status;
3911}
3912
46f7b6be
JK
3913void
3914symbol_table::prepare_section_rejection(Dwfl_Module *mod)
3915{
3916#ifdef __powerpc__
3917 /*
3918 * The .opd section contains function descriptors that can look
3919 * just like function entry points. For example, there's a function
3920 * descriptor called "do_exit" that links to the entry point ".do_exit".
3921 * Reject all symbols in .opd.
3922 */
3923 opd_section = SHN_UNDEF;
3924 Dwarf_Addr bias;
3925 Elf* elf = (dwarf_getelf (dwfl_module_getdwarf (mod, &bias))
3926 ?: dwfl_module_getelf (mod, &bias));
3927 Elf_Scn* scn = 0;
3928 size_t shstrndx;
3929
3930 if (!elf)
3931 return;
fcc30d6d 3932 if (elf_getshdrstrndx (elf, &shstrndx) != 0)
46f7b6be
JK
3933 return;
3934 while ((scn = elf_nextscn(elf, scn)) != NULL)
3935 {
3936 GElf_Shdr shdr_mem;
3937 GElf_Shdr *shdr = gelf_getshdr(scn, &shdr_mem);
3938 if (!shdr)
3939 continue;
3940 const char *name = elf_strptr(elf, shstrndx, shdr->sh_name);
3941 if (!strcmp(name, ".opd"))
3942 {
3943 opd_section = elf_ndxscn(scn);
3944 return;
3945 }
3946 }
3947#endif
3948}
3949
3950bool
3951symbol_table::reject_section(GElf_Word section)
3952{
3953 if (section == SHN_UNDEF)
3954 return true;
3955#ifdef __powerpc__
3956 if (section == opd_section)
3957 return true;
3958#endif
3959 return false;
3960}
3961
5f0a03a6
JK
3962enum info_status
3963symbol_table::get_from_elf()
3964{
3965 Dwarf_Addr high_addr = 0;
3966 Dwfl_Module *mod = mod_info->mod;
3967 int syments = dwfl_module_getsymtab(mod);
3968 assert(syments);
46f7b6be 3969 prepare_section_rejection(mod);
5f0a03a6
JK
3970 for (int i = 1; i < syments; ++i)
3971 {
3972 GElf_Sym sym;
ab91b232
JK
3973 GElf_Word section;
3974 const char *name = dwfl_module_getsym(mod, i, &sym, &section);
46f7b6be
JK
3975 if (name && GELF_ST_TYPE(sym.st_info) == STT_FUNC &&
3976 !reject_section(section))
ab91b232
JK
3977 add_symbol(name, (GELF_ST_BIND(sym.st_info) == STB_WEAK),
3978 sym.st_value, &high_addr);
5f0a03a6
JK
3979 }
3980 return info_present;
3981}
3982
5f0a03a6
JK
3983func_info *
3984symbol_table::get_func_containing_address(Dwarf_Addr addr)
3985{
1c6b77e5
JS
3986 iterator_t iter = map_by_addr.upper_bound(addr);
3987 if (iter == map_by_addr.begin())
5f0a03a6 3988 return NULL;
2e67a43b 3989 else
1c6b77e5 3990 return (--iter)->second;
5f0a03a6
JK
3991}
3992
3993func_info *
3994symbol_table::lookup_symbol(const string& name)
3995{
3996 map<string, func_info*>::iterator i = map_by_name.find(name);
3997 if (i == map_by_name.end())
3998 return NULL;
3999 return i->second;
4000}
4001
4002Dwarf_Addr
4003symbol_table::lookup_symbol_address(const string& name)
4004{
4005 func_info *fi = lookup_symbol(name);
4006 if (fi)
4007 return fi->addr;
4008 return 0;
4009}
4010
ab91b232
JK
4011// This is the kernel symbol table. The kernel macro cond_syscall creates
4012// a weak symbol for each system call and maps it to sys_ni_syscall.
4013// For system calls not implemented elsewhere, this weak symbol shows up
4014// in the kernel symbol table. Following the precedent of dwarfful stap,
4015// we refuse to consider such symbols. Here we delete them from our
4016// symbol table.
4017// TODO: Consider generalizing this and/or making it part of blacklist
4018// processing.
4019void
4020symbol_table::purge_syscall_stubs()
4021{
4022 Dwarf_Addr stub_addr = lookup_symbol_address("sys_ni_syscall");
4023 if (stub_addr == 0)
4024 return;
1c6b77e5 4025 range_t purge_range = map_by_addr.equal_range(stub_addr);
2e67a43b
TM
4026 for (iterator_t iter = purge_range.first;
4027 iter != purge_range.second;
1c6b77e5 4028 )
ab91b232 4029 {
1c6b77e5 4030 func_info *fi = iter->second;
2e67a43b 4031 if (fi->weak && fi->name != "sys_ni_syscall")
ab91b232 4032 {
2e67a43b 4033 map_by_name.erase(fi->name);
1c6b77e5 4034 map_by_addr.erase(iter++);
2e67a43b 4035 delete fi;
2e67a43b 4036 }
1c6b77e5
JS
4037 else
4038 iter++;
ab91b232
JK
4039 }
4040}
4041
5f0a03a6
JK
4042void
4043module_info::get_symtab(dwarf_query *q)
4044{
4045 systemtap_session &sess = q->sess;
4046
1c6b77e5
JS
4047 if (symtab_status != info_unknown)
4048 return;
4049
5f0a03a6
JK
4050 sym_table = new symbol_table(this);
4051 if (!elf_path.empty())
4052 {
83ca3872
MW
4053 if (name == TOK_KERNEL && !sess.kernel_symtab_path.empty()
4054 && ! sess.suppress_warnings)
5f0a03a6
JK
4055 cerr << "Warning: reading symbol table from "
4056 << elf_path
4057 << " -- ignoring "
4058 << sess.kernel_symtab_path
83ca3872 4059 << endl;
5f0a03a6
JK
4060 symtab_status = sym_table->get_from_elf();
4061 }
4062 else
4063 {
4064 assert(name == TOK_KERNEL);
4065 if (sess.kernel_symtab_path.empty())
4066 {
4067 symtab_status = info_absent;
4068 cerr << "Error: Cannot find vmlinux."
4069 << " Consider using --kmap instead of --kelf."
4070 << endl;;
4071 }
4072 else
4073 {
4074 symtab_status =
83ca3872 4075 sym_table->read_from_text_file(sess.kernel_symtab_path, sess);
5f0a03a6
JK
4076 if (symtab_status == info_present)
4077 {
4078 sess.sym_kprobes_text_start =
4079 sym_table->lookup_symbol_address("__kprobes_text_start");
4080 sess.sym_kprobes_text_end =
4081 sym_table->lookup_symbol_address("__kprobes_text_end");
4082 sess.sym_stext = sym_table->lookup_symbol_address("_stext");
5f0a03a6
JK
4083 }
4084 }
4085 }
4086 if (symtab_status == info_absent)
4087 {
4088 delete sym_table;
4089 sym_table = NULL;
4090 return;
4091 }
4092
ab91b232
JK
4093 if (name == TOK_KERNEL)
4094 sym_table->purge_syscall_stubs();
5f0a03a6
JK
4095}
4096
1c6b77e5
JS
4097// update_symtab reconciles data between the elf symbol table and the dwarf
4098// function enumeration. It updates the symbol table entries with the dwarf
4099// die that describes the function, which also signals to query_module_symtab
4100// that a statement probe isn't needed. In return, it also adds aliases to the
4101// function table for names that share the same addr/die.
4102void
4103module_info::update_symtab(cu_function_cache_t *funcs)
4104{
4105 if (!sym_table)
4106 return;
4107
4108 cu_function_cache_t new_funcs;
4109
4110 for (cu_function_cache_t::iterator func = funcs->begin();
4111 func != funcs->end(); func++)
4112 {
4113 // optimization: inlines will never be in the symbol table
4114 if (dwarf_func_inline(&func->second) != 0)
4115 continue;
4116
4117 func_info *fi = sym_table->lookup_symbol(func->first);
4118 if (!fi)
4119 continue;
4120
4121 // iterate over all functions at the same address
4122 symbol_table::range_t er = sym_table->map_by_addr.equal_range(fi->addr);
4123 for (symbol_table::iterator_t it = er.first; it != er.second; ++it)
4124 {
4125 // update this function with the dwarf die
4126 it->second->die = func->second;
4127
4128 // if this function is a new alias, then
4129 // save it to merge into the function cache
4130 if (it->second != fi)
4131 new_funcs[it->second->name] = it->second->die;
4132 }
4133 }
4134
4135 // add all discovered aliases back into the function cache
4136 // NB: this won't replace any names that dwarf may have already found
4137 funcs->insert(new_funcs.begin(), new_funcs.end());
4138}
4139
5f0a03a6
JK
4140module_info::~module_info()
4141{
4142 if (sym_table)
4143 delete sym_table;
b55bc428
FCE
4144}
4145
935447c8 4146// ------------------------------------------------------------------------
888af770 4147// user-space probes
935447c8
DS
4148// ------------------------------------------------------------------------
4149
935447c8 4150
888af770 4151struct uprobe_derived_probe_group: public generic_dpg<uprobe_derived_probe>
935447c8
DS
4152{
4153public:
888af770 4154 void emit_module_decls (systemtap_session& s);
935447c8
DS
4155 void emit_module_init (systemtap_session& s);
4156 void emit_module_exit (systemtap_session& s);
4157};
4158
4159
6d0f3f0c
FCE
4160uprobe_derived_probe::uprobe_derived_probe (const string& function,
4161 const string& filename,
4162 int line,
4163 const string& module,
4164 int pid,
4165 const string& section,
4166 Dwarf_Addr dwfl_addr,
4167 Dwarf_Addr addr,
4168 dwarf_query & q,
4169 Dwarf_Die* scope_die /* may be null */):
4170 derived_probe (q.base_probe, new probe_point (*q.base_loc) /* .components soon rewritten */ ),
4171 return_p (q.has_return), module (module), pid (pid), section (section), address (addr)
935447c8 4172{
17c128f2
FCE
4173 // We may receive probes on two types of ELF objects: ET_EXEC or ET_DYN.
4174 // ET_EXEC ones need no further relocation on the addr(==dwfl_addr), whereas
4175 // ET_DYN ones do (addr += run-time mmap base address). We tell these apart
4176 // by the incoming section value (".absolute" vs. ".dynamic").
935447c8 4177
6d0f3f0c 4178 this->tok = q.base_probe->tok;
a96d1db0 4179
de688825 4180 // Expand target variables in the probe body
6d0f3f0c
FCE
4181 if (!null_die(scope_die))
4182 {
de688825 4183 dwarf_var_expanding_visitor v (q, scope_die, dwfl_addr); // XXX: user-space deref's!
8b095b45 4184 v.replace (this->body);
a96d1db0 4185
6d0f3f0c
FCE
4186 // If during target-variable-expanding the probe, we added a new block
4187 // of code, add it to the start of the probe.
4188 if (v.add_block)
4189 this->body = new block(v.add_block, this->body);
0afb7073 4190
6d0f3f0c
FCE
4191 // If when target-variable-expanding the probe, we added a new
4192 // probe, add it in a new file to the list of files to be processed.
4193 if (v.add_probe)
4194 {
4195 stapfile *f = new stapfile;
4196 f->probes.push_back(v.add_probe);
4197 q.sess.files.push_back(f);
4198 }
4199 }
4200 // else - null scope_die - $target variables will produce an error during translate phase
a96d1db0 4201
9ace370f
JS
4202 // Save the local variables for listing mode
4203 if (q.sess.listing_mode_vars)
4204 saveargs(scope_die);
4205
6d0f3f0c
FCE
4206 // Reset the sole element of the "locations" vector as a
4207 // "reverse-engineered" form of the incoming (q.base_loc) probe
4208 // point. This allows a user to see what function / file / line
4209 // number any particular match of the wildcards.
a96d1db0 4210
6d0f3f0c
FCE
4211 vector<probe_point::component*> comps;
4212 if(q.has_process)
4213 comps.push_back (new probe_point::component(TOK_PROCESS, new literal_string(module)));
4214 else
4215 assert (0);
a96d1db0 4216
6d0f3f0c
FCE
4217 string fn_or_stmt;
4218 if (q.has_function_str || q.has_function_num)
4219 fn_or_stmt = "function";
4220 else
4221 fn_or_stmt = "statement";
a96d1db0 4222
6d0f3f0c
FCE
4223 if (q.has_function_str || q.has_statement_str)
4224 {
4225 string retro_name = function;
4226 if (filename != "")
fb84c077
FCE
4227 {
4228 retro_name += ("@" + string (filename));
4229 if (line > 0)
4230 retro_name += (":" + lex_cast<string> (line));
4231 }
6d0f3f0c
FCE
4232 comps.push_back
4233 (new probe_point::component
4234 (fn_or_stmt, new literal_string (retro_name)));
4235 }
4236 else if (q.has_function_num || q.has_statement_num)
4237 {
4238 Dwarf_Addr retro_addr;
4239 if (q.has_function_num)
4240 retro_addr = q.function_num_val;
4241 else
4242 retro_addr = q.statement_num_val;
4243 comps.push_back (new probe_point::component
4244 (fn_or_stmt,
4245 new literal_number(retro_addr))); // XXX: should be hex if possible
a96d1db0 4246
6d0f3f0c
FCE
4247 if (q.has_absolute)
4248 comps.push_back (new probe_point::component (TOK_ABSOLUTE));
4249 }
a96d1db0 4250
6d0f3f0c
FCE
4251 if (q.has_call)
4252 comps.push_back (new probe_point::component(TOK_CALL));
4253 if (q.has_inline)
4254 comps.push_back (new probe_point::component(TOK_INLINE));
4255 if (return_p)
4256 comps.push_back (new probe_point::component(TOK_RETURN));
4257 /*
4258 if (has_maxactive)
4259 comps.push_back (new probe_point::component
4260 (TOK_MAXACTIVE, new literal_number(maxactive_val)));
4261 */
4262
4263 // Overwrite it.
4264 this->sole_location()->components = comps;
4265}
a96d1db0
DN
4266
4267
0973d815
FCE
4268uprobe_derived_probe::uprobe_derived_probe (probe *base,
4269 probe_point *location,
4270 int pid,
4271 Dwarf_Addr addr,
4272 bool has_return):
4273 derived_probe (base, location), // location is not rewritten here
4274 return_p (has_return), pid (pid), address (addr)
a96d1db0
DN
4275{
4276}
4277
4278
9ace370f
JS
4279void
4280uprobe_derived_probe::saveargs(Dwarf_Die* scope_die)
4281{
4282 // same as dwarf_derived_probe::saveargs
4283
4284 Dwarf_Die *scopes;
4285 if (!null_die(scope_die) && dwarf_getscopes_die (scope_die, &scopes) == 0)
4286 return;
4287 auto_free free_scopes(scopes);
4288
4289 stringstream argstream;
4290 string type_name;
4291 Dwarf_Attribute type_attr;
4292 Dwarf_Die type_die;
4293
4294 if (return_p &&
4295 dwarf_attr_integrate (scope_die, DW_AT_type, &type_attr) &&
4296 dwarf_formref_die (&type_attr, &type_die) &&
4297 dwarf_type_name(type_die, type_name))
4298 argstream << " $return:" << type_name;
4299
4300 Dwarf_Die arg;
4301 if (dwarf_child (&scopes[0], &arg) == 0)
4302 do
4303 {
4304 switch (dwarf_tag (&arg))
4305 {
4306 case DW_TAG_variable:
4307 case DW_TAG_formal_parameter:
4308 break;
4309
4310 default:
4311 continue;
4312 }
4313
4314 const char *arg_name = dwarf_diename (&arg);
4315 if (!arg_name)
4316 continue;
4317
4318 type_name.clear();
4319 if (!dwarf_attr_integrate (&arg, DW_AT_type, &type_attr) ||
4320 !dwarf_formref_die (&type_attr, &type_die) ||
4321 !dwarf_type_name(type_die, type_name))
4322 continue;
4323
4324 argstream << " $" << arg_name << ":" << type_name;
4325 }
4326 while (dwarf_siblingof (&arg, &arg) == 0);
4327
4328 args = argstream.str();
4329}
4330
4331
4332void
4333uprobe_derived_probe::printargs(std::ostream &o) const
4334{
4335 // same as dwarf_derived_probe::printargs
4336 o << args;
4337}
4338
4339
a96d1db0 4340void
6d0f3f0c 4341uprobe_derived_probe::printsig (ostream& o) const
a96d1db0 4342{
6d0f3f0c
FCE
4343 // Same as dwarf_derived_probe.
4344 sole_location()->print (o);
4345 o << " /* pc=" << section << "+0x" << hex << address << dec << " */";
4346 printsig_nested (o);
888af770
FCE
4347}
4348
a96d1db0 4349
888af770
FCE
4350void
4351uprobe_derived_probe::join_group (systemtap_session& s)
4352{
4353 if (! s.uprobe_derived_probes)
4354 s.uprobe_derived_probes = new uprobe_derived_probe_group ();
4355 s.uprobe_derived_probes->enroll (this);
93646f4d 4356 enable_task_finder(s);
a96d1db0 4357
8a03658e
JS
4358 // Ask buildrun.cxx to build extra module if needed, and
4359 // signal staprun to load that module
4360 s.need_uprobes = true;
a96d1db0
DN
4361}
4362
888af770
FCE
4363
4364struct uprobe_builder: public derived_probe_builder
a96d1db0 4365{
888af770 4366 uprobe_builder() {}
a96d1db0
DN
4367 virtual void build(systemtap_session & sess,
4368 probe * base,
4369 probe_point * location,
86bf665e 4370 literal_map_t const & parameters,
a96d1db0
DN
4371 vector<derived_probe *> & finished_results)
4372 {
888af770 4373 int64_t process, address;
a96d1db0 4374
888af770 4375 bool b1 = get_param (parameters, TOK_PROCESS, process);
ced347a9 4376 (void) b1;
888af770 4377 bool b2 = get_param (parameters, TOK_STATEMENT, address);
ced347a9 4378 (void) b2;
888af770
FCE
4379 bool rr = has_null_param (parameters, TOK_RETURN);
4380 assert (b1 && b2); // by pattern_root construction
a96d1db0 4381
0973d815 4382 finished_results.push_back(new uprobe_derived_probe(base, location, process, address, rr));
a96d1db0
DN
4383 }
4384};
4385
4386
4387void
775d51e5 4388uprobe_derived_probe_group::emit_module_decls (systemtap_session& s)
a96d1db0 4389{
888af770 4390 if (probes.empty()) return;
775d51e5 4391 s.op->newline() << "/* ---- user probes ---- */";
a96d1db0 4392
c480bf3d 4393 // If uprobes isn't in the kernel, pull it in from the runtime.
6274464e 4394 s.op->newline() << "#if defined(CONFIG_UPROBES) || defined(CONFIG_UPROBES_MODULE)";
888af770 4395 s.op->newline() << "#include <linux/uprobes.h>";
c480bf3d 4396 s.op->newline() << "#else";
6274464e 4397 s.op->newline() << "#include \"uprobes/uprobes.h\"";
c480bf3d 4398 s.op->newline() << "#endif";
9d451878
JK
4399 s.op->newline() << "#ifndef UPROBES_API_VERSION";
4400 s.op->newline() << "#define UPROBES_API_VERSION 1";
4401 s.op->newline() << "#endif";
a96d1db0 4402
43241c44
FCE
4403 // We'll probably need at least this many:
4404 unsigned minuprobes = probes.size();
4405 // .. but we don't want so many that .bss is inflated (PR10507):
4406 unsigned uprobesize = 64;
4407 unsigned maxuprobesmem = 10*1024*1024; // 10 MB
4408 unsigned maxuprobes = maxuprobesmem / uprobesize;
4409
4410 // Let's choose a value on the middle, but clamped on the minimum size
4411 unsigned default_maxuprobes =
4412 (minuprobes < maxuprobes) ? ((minuprobes + maxuprobes) / 2) : minuprobes;
4413
6d0f3f0c 4414 s.op->newline() << "#ifndef MAXUPROBES";
43241c44 4415 s.op->newline() << "#define MAXUPROBES " << default_maxuprobes;
6d0f3f0c 4416 s.op->newline() << "#endif";
a96d1db0 4417
5e112f92
FCE
4418 // In .bss, the shared pool of uprobe/uretprobe structs. These are
4419 // too big to embed in the initialized .data stap_uprobe_spec array.
4c2732a1 4420 s.op->newline() << "static struct stap_uprobe {";
888af770 4421 s.op->newline(1) << "union { struct uprobe up; struct uretprobe urp; };";
6d0f3f0c 4422 s.op->newline() << "int spec_index;"; // index into stap_uprobe_specs; <0 == free && unregistered
01b05e2e 4423 s.op->newline(-1) << "} stap_uprobes [MAXUPROBES];";
5e112f92 4424 s.op->newline() << "DEFINE_MUTEX(stap_uprobes_lock);"; // protects against concurrent registration/unregistration
a96d1db0 4425
4c2732a1 4426 s.op->newline() << "static struct stap_uprobe_spec {";
5e112f92 4427 s.op->newline(1) << "struct stap_task_finder_target finder;";
888af770 4428 s.op->newline() << "unsigned long address;";
17c128f2 4429 s.op->newline() << "const char *pathname;";
a96d1db0
DN
4430 s.op->newline() << "const char *pp;";
4431 s.op->newline() << "void (*ph) (struct context*);";
6d0f3f0c
FCE
4432 s.op->newline() << "unsigned return_p:1;";
4433 s.op->newline(-1) << "} stap_uprobe_specs [] = {";
a96d1db0 4434 s.op->indent(1);
888af770
FCE
4435 for (unsigned i =0; i<probes.size(); i++)
4436 {
4437 uprobe_derived_probe* p = probes[i];
4438 s.op->newline() << "{";
0973d815
FCE
4439 s.op->line() << " .finder = {";
4440 if (p->pid != 0)
17c128f2
FCE
4441 s.op->line() << " .pid=" << p->pid;
4442 else if (p->section == ".absolute")
0973d815 4443 s.op->line() << " .pathname=" << lex_cast_qstring(p->module) << ", ";
06aca46a 4444 // else ".dynamic" gets pathname=0, pid=0, activating task_finder "global tracing"
0973d815 4445 s.op->line() << "},";
17c128f2
FCE
4446 if (p->section != ".absolute")
4447 s.op->line() << " .pathname=" << lex_cast_qstring(p->module) << ", ";
dc38c256 4448 s.op->line() << " .address=(unsigned long)0x" << hex << p->address << dec << "ULL,";
888af770
FCE
4449 s.op->line() << " .pp=" << lex_cast_qstring (*p->sole_location()) << ",";
4450 s.op->line() << " .ph=&" << p->name << ",";
4451 if (p->return_p) s.op->line() << " .return_p=1,";
4452 s.op->line() << " },";
4453 }
4454 s.op->newline(-1) << "};";
a96d1db0 4455
48e685da 4456 s.op->newline() << "static void enter_uprobe_probe (struct uprobe *inst, struct pt_regs *regs) {";
888af770 4457 s.op->newline(1) << "struct stap_uprobe *sup = container_of(inst, struct stap_uprobe, up);";
6d0f3f0c 4458 s.op->newline() << "struct stap_uprobe_spec *sups = &stap_uprobe_specs [sup->spec_index];";
c12d974f 4459 common_probe_entryfn_prologue (s.op, "STAP_SESSION_RUNNING", "sups->pp");
6d0f3f0c
FCE
4460 s.op->newline() << "if (sup->spec_index < 0 ||"
4461 << "sup->spec_index >= " << probes.size() << ") return;"; // XXX: should not happen
a96d1db0 4462 s.op->newline() << "c->regs = regs;";
6415ddde
MW
4463
4464 // Make it look like the IP is set as it would in the actual user
4465 // task when calling real probe handler. Reset IP regs on return, so
4466 // we don't confuse uprobes. PR10458
3d988997
MW
4467 // But only for architectures where REG_IP is a proper lvalue. PR10491
4468 s.op->newline() << "#ifdef REG_IP_LVALUE";
6415ddde
MW
4469 s.op->newline() << "{";
4470 s.op->indent(1);
4471 s.op->newline() << "unsigned long uprobes_ip = REG_IP(c->regs);";
4472 s.op->newline() << "REG_IP(regs) = inst->vaddr;";
6d0f3f0c 4473 s.op->newline() << "(*sups->ph) (c);";
6415ddde
MW
4474 s.op->newline() << "REG_IP(regs) = uprobes_ip;";
4475 s.op->newline(-1) << "}";
3d988997
MW
4476 s.op->newline() << "#else";
4477 s.op->newline() << "(*sdp->ph) (c);";
4478 s.op->newline() << "#endif";
6415ddde 4479
a96d1db0 4480 common_probe_entryfn_epilogue (s.op);
888af770 4481 s.op->newline(-1) << "}";
a96d1db0 4482
48e685da 4483 s.op->newline() << "static void enter_uretprobe_probe (struct uretprobe_instance *inst, struct pt_regs *regs) {";
888af770 4484 s.op->newline(1) << "struct stap_uprobe *sup = container_of(inst->rp, struct stap_uprobe, urp);";
6d0f3f0c 4485 s.op->newline() << "struct stap_uprobe_spec *sups = &stap_uprobe_specs [sup->spec_index];";
c12d974f 4486 common_probe_entryfn_prologue (s.op, "STAP_SESSION_RUNNING", "sups->pp");
6d0f3f0c
FCE
4487 s.op->newline() << "if (sup->spec_index < 0 ||"
4488 << "sup->spec_index >= " << probes.size() << ") return;"; // XXX: should not happen
888af770
FCE
4489 // XXX: kretprobes saves "c->pi = inst;" too
4490 s.op->newline() << "c->regs = regs;";
6415ddde
MW
4491
4492 // Make it look like the IP is set as it would in the actual user
4493 // task when calling real probe handler. Reset IP regs on return, so
4494 // we don't confuse uprobes. PR10458
3d988997
MW
4495 // But only for architectures where REG_IP is a proper lvalue. PR10491
4496 s.op->newline() << "#ifdef REG_IP_LVALUE";
6415ddde
MW
4497 s.op->newline() << "{";
4498 s.op->indent(1);
4499 s.op->newline() << "unsigned long uprobes_ip = REG_IP(c->regs);";
4500 s.op->newline() << "REG_IP(regs) = inst->rp->u.vaddr;";
6d0f3f0c 4501 s.op->newline() << "(*sups->ph) (c);";
6415ddde
MW
4502 s.op->newline() << "REG_IP(regs) = uprobes_ip;";
4503 s.op->newline(-1) << "}";
3d988997
MW
4504 s.op->newline() << "#else";
4505 s.op->newline() << "(*sdp->ph) (c);";
4506 s.op->newline() << "#endif";
6415ddde 4507
888af770 4508 common_probe_entryfn_epilogue (s.op);
a96d1db0
DN
4509 s.op->newline(-1) << "}";
4510
a96d1db0 4511
a96d1db0 4512
6d0f3f0c
FCE
4513 // NB: Because these utrace callbacks only occur before / after
4514 // userspace instructions run, there is no concurrency control issue
4515 // between active uprobe callbacks and these registration /
06aca46a 4516 // unregistration pieces.
a96d1db0 4517
6d0f3f0c 4518 // We protect the stap_uprobe->spec_index (which also serves as a
d41d451c
FCE
4519 // free/busy flag) value with the outer protective stap_probes_lock
4520 // spinlock, to protect it against concurrent registration /
4521 // unregistration.
8813a27c 4522
6d0f3f0c 4523 s.op->newline();
17c128f2
FCE
4524 s.op->newline() << "static int stap_uprobe_change (struct task_struct *tsk, int register_p, unsigned long relocation, struct stap_uprobe_spec *sups) {";
4525 s.op->newline(1) << "int spec_index = (sups - stap_uprobe_specs);";
6d0f3f0c 4526 s.op->newline() << "int handled_p = 0;";
d41d451c 4527 s.op->newline() << "int slotted_p = 0;";
6d0f3f0c
FCE
4528 s.op->newline() << "int rc = 0;";
4529 s.op->newline() << "int i;";
a96d1db0 4530
d41d451c 4531 s.op->newline() << "mutex_lock (& stap_uprobes_lock);";
01b05e2e 4532 s.op->newline() << "for (i=0; i<MAXUPROBES; i++) {"; // XXX: slow linear search
5e112f92 4533 s.op->newline(1) << "struct stap_uprobe *sup = & stap_uprobes[i];";
a96d1db0 4534
6d0f3f0c
FCE
4535 // register new uprobe
4536 s.op->newline() << "if (register_p && sup->spec_index < 0) {";
9d451878
JK
4537 s.op->newline(1) << "#if (UPROBES_API_VERSION < 2)";
4538 // See PR6829 comment.
4539 s.op->newline() << "if (sup->spec_index == -1 && sup->up.kdata != NULL) continue;";
80b4ad8b 4540 s.op->newline() << "else if (sup->spec_index == -2 && sup->urp.u.kdata != NULL) continue;";
9d451878 4541 s.op->newline() << "#endif";
80b4ad8b 4542 s.op->newline() << "sup->spec_index = spec_index;";
d41d451c
FCE
4543 s.op->newline() << "slotted_p = 1;";
4544 s.op->newline() << "break;";
4545 s.op->newline(-1) << "} else if (!register_p && "
4546 << "sup->spec_index == spec_index && " // a u[ret]probe set up for this probe point
4547 << "((sups->return_p && sup->urp.u.pid == tsk->tgid && sup->urp.u.vaddr == relocation + sups->address) ||" // dying uretprobe
4548 << "(!sups->return_p && sup->up.pid == tsk->tgid && sup->up.vaddr == relocation + sups->address))) {"; // dying uprobe
4549 s.op->newline(1) << "slotted_p = 1;";
4550 s.op->newline() << "break;"; // exit to-free slot search
4551 s.op->newline(-1) << "}";
a96d1db0 4552
d41d451c
FCE
4553 s.op->newline(-1) << "}";
4554 s.op->newline() << "mutex_unlock (& stap_uprobes_lock);";
a96d1db0 4555
d41d451c
FCE
4556 s.op->newline() << "#ifdef DEBUG_UPROBES";
4557 s.op->newline() << "printk (KERN_INFO \"%cuprobe spec %d idx %d process %s[%d] reloc %p pp '%s'\\n\", ";
4558 s.op->line() << "(register_p ? '+' : '-'), spec_index, (slotted_p ? i : -1), tsk->comm, tsk->tgid, (void*) relocation, sups->pp);";
4559 s.op->newline() << "#endif";
a96d1db0 4560
d41d451c
FCE
4561 // Here, slotted_p implies that `i' points to the single
4562 // stap_uprobes[] element that has been slotted in for registration
4563 // or unregistration processing. !slotted_p implies that the table
4564 // was full (registration; MAXUPROBES) or that no matching entry was
4565 // found (unregistration; should not happen).
4566
4567 s.op->newline() << "if (register_p && slotted_p) {";
4568 s.op->newline(1) << "struct stap_uprobe *sup = & stap_uprobes[i];";
6d0f3f0c
FCE
4569 s.op->newline() << "if (sups->return_p) {";
4570 s.op->newline(1) << "sup->urp.u.pid = tsk->tgid;";
17c128f2 4571 s.op->newline() << "sup->urp.u.vaddr = relocation + sups->address;";
6d0f3f0c
FCE
4572 s.op->newline() << "sup->urp.handler = &enter_uretprobe_probe;";
4573 s.op->newline() << "rc = register_uretprobe (& sup->urp);";
4574 s.op->newline(-1) << "} else {";
4575 s.op->newline(1) << "sup->up.pid = tsk->tgid;";
17c128f2 4576 s.op->newline() << "sup->up.vaddr = relocation + sups->address;";
6d0f3f0c
FCE
4577 s.op->newline() << "sup->up.handler = &enter_uprobe_probe;";
4578 s.op->newline() << "rc = register_uprobe (& sup->up);";
4579 s.op->newline(-1) << "}";
6d0f3f0c 4580 s.op->newline() << "if (rc) {"; // failed to register
d41d451c
FCE
4581 s.op->newline(1) << "printk (KERN_WARNING \"uprobe failed %s[%d] '%s' addr %p rc %d\\n\", tsk->comm, tsk->tgid, sups->pp, (void*)(relocation + sups->address), rc);";
4582 // NB: we need to release this slot, so we need to borrow the mutex temporarily.
4583 s.op->newline() << "mutex_lock (& stap_uprobes_lock);";
3568f1dd 4584 s.op->newline() << "sup->spec_index = -1;";
d41d451c 4585 s.op->newline() << "mutex_unlock (& stap_uprobes_lock);";
6d0f3f0c
FCE
4586 s.op->newline(-1) << "} else {";
4587 s.op->newline(1) << "handled_p = 1;"; // success
8813a27c
MW
4588 s.op->newline(-1) << "}";
4589
d41d451c
FCE
4590 s.op->newline(-1) << "} else if (!register_p && slotted_p) {";
4591 s.op->newline(1) << "struct stap_uprobe *sup = & stap_uprobes[i];";
9d451878
JK
4592 s.op->newline() << "int unregistered_flag;";
4593 // PR6829, PR9940:
4594 // Here we're unregistering for one of two reasons:
4595 // 1. the process image is going away (or gone) due to exit or exec; or
4596 // 2. the vma containing the probepoint has been unmapped.
4597 // In case 1, it's sort of a nop, because uprobes will notice the event
4598 // and dispose of the probes eventually, if it hasn't already. But by
4599 // calling unmap_u[ret]probe() ourselves, we free up sup right away.
4600 //
4601 // In both cases, we must use unmap_u[ret]probe instead of
4602 // unregister_u[ret]probe, so uprobes knows not to try to restore the
4603 // original opcode.
4604 s.op->newline() << "#if (UPROBES_API_VERSION >= 2)";
4605 s.op->newline() << "if (sups->return_p)";
4606 s.op->newline(1) << "unmap_uretprobe (& sup->urp);";
4607 s.op->newline(-1) << "else";
4608 s.op->newline(1) << "unmap_uprobe (& sup->up);";
4609 s.op->newline(-1) << "unregistered_flag = -1;";
4610 s.op->newline() << "#else";
4611 // Uprobes lacks unmap_u[ret]probe. Before reusing sup, we must wait
4612 // until uprobes turns loose of the u[ret]probe on its own, as indicated
4613 // by uprobe.kdata = NULL.
4614 s.op->newline() << "unregistered_flag = (sups->return_p ? -2 : -1);";
4615 s.op->newline() << "#endif";
d41d451c 4616 s.op->newline() << "mutex_lock (& stap_uprobes_lock);";
9d451878 4617 s.op->newline() << "sup->spec_index = unregistered_flag;";
5e112f92 4618 s.op->newline() << "mutex_unlock (& stap_uprobes_lock);";
d41d451c
FCE
4619 s.op->newline() << "handled_p = 1;";
4620 s.op->newline(-1) << "}"; // if slotted_p
a96d1db0 4621
d41d451c 4622 // NB: handled_p implies slotted_p
3598cf01 4623
6d0f3f0c 4624 s.op->newline() << "if (! handled_p) {";
73209876
FCE
4625 s.op->newline(1) << "#ifdef STP_TIMING";
4626 s.op->newline() << "atomic_inc (register_p ? & skipped_count_uprobe_reg : & skipped_count_uprobe_unreg);";
4627 s.op->newline() << "#endif";
4628 // NB: duplicates common_entryfn_epilogue, but then this is not a probe entry fn epilogue.
4629 s.op->newline() << "if (unlikely (atomic_inc_return (& skipped_count) > MAXSKIPPED)) {";
6d0f3f0c
FCE
4630 s.op->newline(1) << "atomic_set (& session_state, STAP_SESSION_ERROR);";
4631 s.op->newline() << "_stp_exit ();";
3598cf01 4632 s.op->newline(-1) << "}";
3598cf01
MJ
4633 s.op->newline(-1) << "}";
4634
6d0f3f0c 4635 s.op->newline() << "return 0;"; // XXX: or rc?
a96d1db0 4636 s.op->newline(-1) << "}";
6d0f3f0c 4637 s.op->assert_0_indent();
a96d1db0
DN
4638
4639
17c128f2 4640 // The task_finder_callback we use for ET_EXEC targets.
a96d1db0 4641 s.op->newline();
17c128f2 4642 s.op->newline() << "static int stap_uprobe_process_found (struct stap_task_finder_target *tgt, struct task_struct *tsk, int register_p, int process_p) {";
935447c8 4643
17c128f2
FCE
4644 s.op->newline(1) << "struct stap_uprobe_spec *sups = container_of(tgt, struct stap_uprobe_spec, finder);";
4645 s.op->newline() << "if (! process_p) return 0;";
4646 s.op->newline(0) << "return stap_uprobe_change (tsk, register_p, 0, sups);";
4647 s.op->newline(-1) << "}";
935447c8 4648
782040b3 4649 // The task_finder_mmap_callback we use for ET_DYN targets.
17c128f2 4650 s.op->newline();
782040b3 4651 s.op->newline() << "static int stap_uprobe_mmap_found (struct stap_task_finder_target *tgt, struct task_struct *tsk, char *path, unsigned long addr, unsigned long length, unsigned long offset, unsigned long vm_flags) {";
17c128f2
FCE
4652 s.op->newline(1) << "struct stap_uprobe_spec *sups = container_of(tgt, struct stap_uprobe_spec, finder);";
4653 // 1 - shared libraries' executable segments load from offset 0 - ld.so convention
782040b3 4654 s.op->newline() << "if (offset != 0) return 0;";
17c128f2 4655 // 2 - the shared library we're interested in
782040b3 4656 s.op->newline() << "if (path == NULL || strcmp (path, sups->pathname)) return 0;";
17c128f2 4657 // 3 - probe address within the mapping limits; test should not fail
782040b3
DS
4658 s.op->newline() << "if (sups->address >= addr && sups->address < (addr + length)) return 0;";
4659 // 4 - mapping should be executable
4660 s.op->newline() << "if (!(vm_flags & VM_EXEC)) return 0;";
935447c8 4661
c16d425a 4662 s.op->newline() << "#ifdef DEBUG_TASK_FINDER_VMA";
782040b3 4663 s.op->newline() << "printk (KERN_INFO \"vmchange pid %d path %s addr %p length %lu offset %p\\n\", tsk->tgid, path, (void *) addr, length, (void*) offset);";
c16d425a
FCE
4664 s.op->newline() << "printk (KERN_INFO \"sups %p pp %s path %s address %p\\n\", sups, sups->pp, sups->pathname ?: \"\", (void*) sups->address);";
4665 s.op->newline() << "#endif";
935447c8 4666
782040b3 4667 s.op->newline(0) << "return stap_uprobe_change (tsk, 1, addr, sups);";
17c128f2
FCE
4668 s.op->newline(-1) << "}";
4669 s.op->assert_0_indent();
935447c8 4670
935447c8 4671
6d0f3f0c 4672 s.op->newline();
888af770 4673}
935447c8
DS
4674
4675
888af770
FCE
4676void
4677uprobe_derived_probe_group::emit_module_init (systemtap_session& s)
935447c8 4678{
888af770 4679 if (probes.empty()) return;
935447c8 4680
5e112f92 4681 s.op->newline() << "/* ---- user probes ---- */";
935447c8 4682
01b05e2e 4683 s.op->newline() << "for (j=0; j<MAXUPROBES; j++) {";
5e112f92
FCE
4684 s.op->newline(1) << "struct stap_uprobe *sup = & stap_uprobes[j];";
4685 s.op->newline() << "sup->spec_index = -1;"; // free slot
80b4ad8b
FCE
4686 // NB: we assume the rest of the struct (specificaly, sup->up) is
4687 // initialized to zero. This is so that we can use
4688 // sup->up->kdata = NULL for "really free!" PR 6829.
5e112f92
FCE
4689 s.op->newline(-1) << "}";
4690 s.op->newline() << "mutex_init (& stap_uprobes_lock);";
935447c8 4691
888af770 4692 s.op->newline() << "for (i=0; i<" << probes.size() << "; i++) {";
6d0f3f0c
FCE
4693 s.op->newline(1) << "struct stap_uprobe_spec *sups = & stap_uprobe_specs[i];";
4694 s.op->newline() << "probe_point = sups->pp;"; // for error messages
17c128f2 4695 s.op->newline() << "if (sups->finder.pathname) sups->finder.callback = & stap_uprobe_process_found;";
782040b3 4696 s.op->newline() << "else if (sups->pathname) sups->finder.mmap_callback = & stap_uprobe_mmap_found;";
6d0f3f0c 4697 s.op->newline() << "rc = stap_register_task_finder_target (& sups->finder);";
935447c8 4698
5e112f92
FCE
4699 // NB: if (rc), there is no need (XXX: nor any way) to clean up any
4700 // finders already registered, since mere registration does not
4701 // cause any utrace or memory allocation actions. That happens only
4702 // later, once the task finder engine starts running. So, for a
4703 // partial initialization requiring unwind, we need do nothing.
4704 s.op->newline() << "if (rc) break;";
a7a68293 4705
888af770
FCE
4706 s.op->newline(-1) << "}";
4707}
d0ea46ce 4708
d0a7f5a9 4709
888af770
FCE
4710void
4711uprobe_derived_probe_group::emit_module_exit (systemtap_session& s)
4712{
4713 if (probes.empty()) return;
4714 s.op->newline() << "/* ---- user probes ---- */";
e56e51c9 4715
6d0f3f0c
FCE
4716 // NB: there is no stap_unregister_task_finder_target call;
4717 // important stuff like utrace cleanups are done by
d41d451c
FCE
4718 // __stp_task_finder_cleanup() via stap_stop_task_finder().
4719 //
4720 // This function blocks until all callbacks are completed, so there
4721 // is supposed to be no possibility of any registration-related code starting
4722 // to run in parallel with our shutdown here. So we don't need to protect the
4723 // stap_uprobes[] array with the mutex.
d0a7f5a9 4724
01b05e2e 4725 s.op->newline() << "for (j=0; j<MAXUPROBES; j++) {";
5e112f92
FCE
4726 s.op->newline(1) << "struct stap_uprobe *sup = & stap_uprobes[j];";
4727 s.op->newline() << "struct stap_uprobe_spec *sups = &stap_uprobe_specs [sup->spec_index];";
6d0f3f0c 4728 s.op->newline() << "if (sup->spec_index < 0) continue;"; // free slot
3568f1dd
FCE
4729
4730 s.op->newline() << "if (sups->return_p) {";
4731 s.op->newline(1) << "#ifdef DEBUG_UPROBES";
d41d451c 4732 s.op->newline() << "printk (KERN_INFO \"-uretprobe spec %d index %d pid %d addr %p\\n\", sup->spec_index, j, sup->up.pid, (void*) sup->up.vaddr);";
3568f1dd 4733 s.op->newline() << "#endif";
80b4ad8b
FCE
4734 // NB: PR6829 does not change that we still need to unregister at
4735 // *this* time -- when the script as a whole exits.
3568f1dd
FCE
4736 s.op->newline() << "unregister_uretprobe (& sup->urp);";
4737 s.op->newline(-1) << "} else {";
4738 s.op->newline(1) << "#ifdef DEBUG_UPROBES";
d41d451c 4739 s.op->newline() << "printk (KERN_INFO \"-uprobe spec %d index %d pid %d addr %p\\n\", sup->spec_index, j, sup->urp.u.pid, (void*) sup->urp.u.vaddr);";
3568f1dd
FCE
4740 s.op->newline() << "#endif";
4741 s.op->newline() << "unregister_uprobe (& sup->up);";
4742 s.op->newline(-1) << "}";
935447c8 4743
6d0f3f0c 4744 s.op->newline() << "sup->spec_index = -1;";
935447c8 4745
3568f1dd
FCE
4746 // XXX: uprobe missed counts?
4747
6d0f3f0c 4748 s.op->newline(-1) << "}";
935447c8 4749
5e112f92 4750 s.op->newline() << "mutex_destroy (& stap_uprobes_lock);";
935447c8
DS
4751}
4752
e6fe60e7
AM
4753// ------------------------------------------------------------------------
4754// Kprobe derived probes
4755// ------------------------------------------------------------------------
4756
4627ed58 4757static const string TOK_KPROBE("kprobe");
935447c8 4758
bae55db9 4759struct kprobe_derived_probe: public derived_probe
d0ea46ce 4760{
bae55db9
JS
4761 kprobe_derived_probe (probe *base,
4762 probe_point *location,
4763 const string& name,
4764 int64_t stmt_addr,
4765 bool has_return,
4766 bool has_statement,
4767 bool has_maxactive,
4768 long maxactive_val
4769 );
4770 string symbol_name;
4771 Dwarf_Addr addr;
4772 bool has_return;
4773 bool has_statement;
4774 bool has_maxactive;
4775 long maxactive_val;
4776 bool access_var;
4777 void printsig (std::ostream &o) const;
4778 void join_group (systemtap_session& s);
4779};
d0ea46ce 4780
bae55db9
JS
4781struct kprobe_derived_probe_group: public derived_probe_group
4782{
4783private:
4784 multimap<string,kprobe_derived_probe*> probes_by_module;
4785 typedef multimap<string,kprobe_derived_probe*>::iterator p_b_m_iterator;
d0ea46ce 4786
bae55db9
JS
4787public:
4788 void enroll (kprobe_derived_probe* probe);
4789 void emit_module_decls (systemtap_session& s);
4790 void emit_module_init (systemtap_session& s);
4791 void emit_module_exit (systemtap_session& s);
4792};
d0ea46ce 4793
e6fe60e7
AM
4794kprobe_derived_probe::kprobe_derived_probe (probe *base,
4795 probe_point *location,
b6371390 4796 const string& name,
e6fe60e7 4797 int64_t stmt_addr,
b6371390
JS
4798 bool has_return,
4799 bool has_statement,
4800 bool has_maxactive,
4801 long maxactive_val
4802 ):
e6fe60e7
AM
4803 derived_probe (base, location),
4804 symbol_name (name), addr (stmt_addr),
b6371390
JS
4805 has_return (has_return), has_statement (has_statement),
4806 has_maxactive (has_maxactive), maxactive_val (maxactive_val)
e6fe60e7
AM
4807{
4808 this->tok = base->tok;
4809 this->access_var = false;
d0ea46ce 4810
e6fe60e7
AM
4811#ifndef USHRT_MAX
4812#define USHRT_MAX 32767
4813#endif
d0ea46ce 4814
46856d8d
JS
4815 // Expansion of $target variables in the probe body produces an error during
4816 // translate phase, since we're not using debuginfo
d0ea46ce 4817
e6fe60e7 4818 vector<probe_point::component*> comps;
46856d8d 4819 comps.push_back (new probe_point::component(TOK_KPROBE));
e6fe60e7 4820
46856d8d
JS
4821 if (has_statement)
4822 {
4823 comps.push_back (new probe_point::component(TOK_STATEMENT, new literal_number(addr)));
4824 comps.push_back (new probe_point::component(TOK_ABSOLUTE));
4825 }
4826 else
4827 {
4828 size_t pos = name.find(':');
4829 if (pos != string::npos)
d0ea46ce 4830 {
46856d8d
JS
4831 string module = name.substr(0, pos);
4832 string function = name.substr(pos + 1);
4833 comps.push_back (new probe_point::component(TOK_MODULE, new literal_string(module)));
4834 comps.push_back (new probe_point::component(TOK_FUNCTION, new literal_string(function)));
4835 }
4836 else
4837 comps.push_back (new probe_point::component(TOK_FUNCTION, new literal_string(name)));
46856d8d 4838 }
d0ea46ce 4839
b6371390
JS
4840 if (has_return)
4841 comps.push_back (new probe_point::component(TOK_RETURN));
4842 if (has_maxactive)
4843 comps.push_back (new probe_point::component(TOK_MAXACTIVE, new literal_number(maxactive_val)));
d0ea46ce 4844
e6fe60e7
AM
4845 this->sole_location()->components = comps;
4846}
d0ea46ce 4847
e6fe60e7
AM
4848void kprobe_derived_probe::printsig (ostream& o) const
4849{
4850 sole_location()->print (o);
4851 o << " /* " << " name = " << symbol_name << "*/";
4852 printsig_nested (o);
4853}
d0ea46ce 4854
e6fe60e7
AM
4855void kprobe_derived_probe::join_group (systemtap_session& s)
4856{
d0ea46ce 4857
e6fe60e7
AM
4858 if (! s.kprobe_derived_probes)
4859 s.kprobe_derived_probes = new kprobe_derived_probe_group ();
4860 s.kprobe_derived_probes->enroll (this);
d0ea46ce 4861
e6fe60e7 4862}
d0ea46ce 4863
e6fe60e7
AM
4864void kprobe_derived_probe_group::enroll (kprobe_derived_probe* p)
4865{
4866 probes_by_module.insert (make_pair (p->symbol_name, p));
4867 // probes of same symbol should share single kprobe/kretprobe
4868}
d0ea46ce 4869
e6fe60e7
AM
4870void
4871kprobe_derived_probe_group::emit_module_decls (systemtap_session& s)
4872{
4873 if (probes_by_module.empty()) return;
d0ea46ce 4874
e6fe60e7 4875 s.op->newline() << "/* ---- kprobe-based probes ---- */";
d0ea46ce 4876
e6fe60e7
AM
4877 // Warn of misconfigured kernels
4878 s.op->newline() << "#if ! defined(CONFIG_KPROBES)";
4879 s.op->newline() << "#error \"Need CONFIG_KPROBES!\"";
4880 s.op->newline() << "#endif";
4881 s.op->newline();
d0ea46ce 4882
f07c3b68
FCE
4883 s.op->newline() << "#ifndef KRETACTIVE";
4884 s.op->newline() << "#define KRETACTIVE (max(15,6*NR_CPUS))";
4885 s.op->newline() << "#endif";
4886
e6fe60e7 4887 // Forward declare the master entry functions
88747011 4888 s.op->newline() << "static int enter_kprobe2_probe (struct kprobe *inst,";
e6fe60e7 4889 s.op->line() << " struct pt_regs *regs);";
88747011 4890 s.op->newline() << "static int enter_kretprobe2_probe (struct kretprobe_instance *inst,";
e6fe60e7 4891 s.op->line() << " struct pt_regs *regs);";
d0ea46ce 4892
e6fe60e7
AM
4893 // Emit an array of kprobe/kretprobe pointers
4894 s.op->newline() << "#if defined(STAPCONF_UNREGISTER_KPROBES)";
c9116e99 4895 s.op->newline() << "static void * stap_unreg_kprobes2[" << probes_by_module.size() << "];";
e6fe60e7 4896 s.op->newline() << "#endif";
d0ea46ce 4897
e6fe60e7 4898 // Emit the actual probe list.
d0ea46ce 4899
e6fe60e7
AM
4900 s.op->newline() << "static struct stap_dwarfless_kprobe {";
4901 s.op->newline(1) << "union { struct kprobe kp; struct kretprobe krp; } u;";
4902 s.op->newline() << "#ifdef __ia64__";
4903 s.op->newline() << "struct kprobe dummy;";
4904 s.op->newline() << "#endif";
4905 s.op->newline(-1) << "} stap_dwarfless_kprobes[" << probes_by_module.size() << "];";
4906 // NB: bss!
d0ea46ce 4907
e6fe60e7
AM
4908 s.op->newline() << "static struct stap_dwarfless_probe {";
4909 s.op->newline(1) << "const unsigned return_p:1;";
4910 s.op->newline() << "const unsigned maxactive_p:1;";
b350f56b 4911 s.op->newline() << "const unsigned optional_p:1;";
e6fe60e7
AM
4912 s.op->newline() << "unsigned registered_p:1;";
4913 s.op->newline() << "const unsigned short maxactive_val;";
935447c8 4914
e6fe60e7
AM
4915 // Function Names are mostly small and uniform enough to justify putting
4916 // char[MAX]'s into the array instead of relocated char*'s.
935447c8 4917
e6fe60e7
AM
4918 size_t pp_name_max = 0, symbol_string_name_max = 0;
4919 size_t pp_name_tot = 0, symbol_string_name_tot = 0;
4920 for (p_b_m_iterator it = probes_by_module.begin(); it != probes_by_module.end(); it++)
6270adc1 4921 {
e6fe60e7
AM
4922 kprobe_derived_probe* p = it->second;
4923#define DOIT(var,expr) do { \
4924 size_t var##_size = (expr) + 1; \
4925 var##_max = max (var##_max, var##_size); \
4926 var##_tot += var##_size; } while (0)
4927 DOIT(pp_name, lex_cast_qstring(*p->sole_location()).size());
4928 DOIT(symbol_string_name, p->symbol_name.size());
4929#undef DOIT
6270adc1
MH
4930 }
4931
e6fe60e7
AM
4932#define CALCIT(var) \
4933 s.op->newline() << "const char " << #var << "[" << var##_name_max << "] ;";
935447c8 4934
e6fe60e7
AM
4935 CALCIT(pp);
4936 CALCIT(symbol_string);
4937#undef CALCIT
6270adc1 4938
e6fe60e7
AM
4939 s.op->newline() << "const unsigned long address;";
4940 s.op->newline() << "void (* const ph) (struct context*);";
4941 s.op->newline(-1) << "} stap_dwarfless_probes[] = {";
4942 s.op->indent(1);
6270adc1 4943
e6fe60e7
AM
4944 for (p_b_m_iterator it = probes_by_module.begin(); it != probes_by_module.end(); it++)
4945 {
4946 kprobe_derived_probe* p = it->second;
4947 s.op->newline() << "{";
4948 if (p->has_return)
4949 s.op->line() << " .return_p=1,";
6270adc1 4950
e6fe60e7
AM
4951 if (p->has_maxactive)
4952 {
4953 s.op->line() << " .maxactive_p=1,";
4954 assert (p->maxactive_val >= 0 && p->maxactive_val <= USHRT_MAX);
4955 s.op->line() << " .maxactive_val=" << p->maxactive_val << ",";
4956 }
6270adc1 4957
b350f56b
JS
4958 if (p->locations[0]->optional)
4959 s.op->line() << " .optional_p=1,";
4960
e6fe60e7 4961 if (p->has_statement)
c8d9d15e 4962 s.op->line() << " .address=(unsigned long)0x" << hex << p->addr << dec << "ULL,";
e6fe60e7 4963 else
c8d9d15e 4964 s.op->line() << " .symbol_string=\"" << p->symbol_name << "\",";
5d67b47c 4965
e6fe60e7
AM
4966 s.op->line() << " .pp=" << lex_cast_qstring (*p->sole_location()) << ",";
4967 s.op->line() << " .ph=&" << p->name;
4968 s.op->line() << " },";
935447c8
DS
4969 }
4970
e6fe60e7 4971 s.op->newline(-1) << "};";
5d67b47c 4972
e6fe60e7
AM
4973 // Emit the kprobes callback function
4974 s.op->newline();
88747011 4975 s.op->newline() << "static int enter_kprobe2_probe (struct kprobe *inst,";
e6fe60e7
AM
4976 s.op->line() << " struct pt_regs *regs) {";
4977 // NB: as of PR5673, the kprobe|kretprobe union struct is in BSS
4978 s.op->newline(1) << "int kprobe_idx = ((uintptr_t)inst-(uintptr_t)stap_dwarfless_kprobes)/sizeof(struct stap_dwarfless_kprobe);";
4979 // Check that the index is plausible
4980 s.op->newline() << "struct stap_dwarfless_probe *sdp = &stap_dwarfless_probes[";
4981 s.op->line() << "((kprobe_idx >= 0 && kprobe_idx < " << probes_by_module.size() << ")?";
4982 s.op->line() << "kprobe_idx:0)"; // NB: at least we avoid memory corruption
4983 // XXX: it would be nice to give a more verbose error though; BUG_ON later?
4984 s.op->line() << "];";
4985 common_probe_entryfn_prologue (s.op, "STAP_SESSION_RUNNING", "sdp->pp");
4986 s.op->newline() << "c->regs = regs;";
6415ddde
MW
4987
4988 // Make it look like the IP is set as it wouldn't have been replaced
4989 // by a breakpoint instruction when calling real probe handler. Reset
4990 // IP regs on return, so we don't confuse kprobes. PR10458
3d988997
MW
4991 // But only for architectures where REG_IP is a proper lvalue. PR10491
4992 s.op->newline() << "#ifdef REG_IP_LVALUE";
6415ddde
MW
4993 s.op->newline() << "{";
4994 s.op->indent(1);
4995 s.op->newline() << "unsigned long kprobes_ip = REG_IP(c->regs);";
4996 s.op->newline() << "REG_IP(regs) = (unsigned long) inst->addr;";
e6fe60e7 4997 s.op->newline() << "(*sdp->ph) (c);";
6415ddde
MW
4998 s.op->newline() << "REG_IP(regs) = kprobes_ip;";
4999 s.op->newline(-1) << "}";
3d988997
MW
5000 s.op->newline() << "#else";
5001 s.op->newline() << "(*sdp->ph) (c);";
5002 s.op->newline() << "#endif";
6415ddde 5003
e6fe60e7
AM
5004 common_probe_entryfn_epilogue (s.op);
5005 s.op->newline() << "return 0;";
5006 s.op->newline(-1) << "}";
935447c8 5007
e6fe60e7
AM
5008 // Same for kretprobes
5009 s.op->newline();
88747011 5010 s.op->newline() << "static int enter_kretprobe2_probe (struct kretprobe_instance *inst,";
e6fe60e7
AM
5011 s.op->line() << " struct pt_regs *regs) {";
5012 s.op->newline(1) << "struct kretprobe *krp = inst->rp;";
935447c8 5013
e6fe60e7
AM
5014 // NB: as of PR5673, the kprobe|kretprobe union struct is in BSS
5015 s.op->newline() << "int kprobe_idx = ((uintptr_t)krp-(uintptr_t)stap_dwarfless_kprobes)/sizeof(struct stap_dwarfless_kprobe);";
5016 // Check that the index is plausible
5017 s.op->newline() << "struct stap_dwarfless_probe *sdp = &stap_dwarfless_probes[";
5018 s.op->line() << "((kprobe_idx >= 0 && kprobe_idx < " << probes_by_module.size() << ")?";
5019 s.op->line() << "kprobe_idx:0)"; // NB: at least we avoid memory corruption
5020 // XXX: it would be nice to give a more verbose error though; BUG_ON later?
5021 s.op->line() << "];";
935447c8 5022
e6fe60e7
AM
5023 common_probe_entryfn_prologue (s.op, "STAP_SESSION_RUNNING", "sdp->pp");
5024 s.op->newline() << "c->regs = regs;";
5025 s.op->newline() << "c->pi = inst;"; // for assisting runtime's backtrace logic
6415ddde
MW
5026
5027 // Make it look like the IP is set as it wouldn't have been replaced
5028 // by a breakpoint instruction when calling real probe handler. Reset
5029 // IP regs on return, so we don't confuse kprobes. PR10458
3d988997
MW
5030 // But only for architectures where REG_IP is a proper lvalue. PR10491
5031 s.op->newline() << "#ifdef REG_IP_LVALUE";
6415ddde
MW
5032 s.op->newline() << "{";
5033 s.op->indent(1);
5034 s.op->newline() << "unsigned long kprobes_ip = REG_IP(c->regs);";
5035 s.op->newline() << "REG_IP(regs) = (unsigned long) inst->rp->kp.addr;";
e6fe60e7 5036 s.op->newline() << "(*sdp->ph) (c);";
6415ddde
MW
5037 s.op->newline() << "REG_IP(regs) = kprobes_ip;";
5038 s.op->newline(-1) << "}";
3d988997
MW
5039 s.op->newline() << "#else";
5040 s.op->newline() << "(*sdp->ph) (c);";
5041 s.op->newline() << "#endif";
6415ddde 5042
e6fe60e7
AM
5043 common_probe_entryfn_epilogue (s.op);
5044 s.op->newline() << "return 0;";
5045 s.op->newline(-1) << "}";
935447c8
DS
5046}
5047
e6fe60e7 5048
6270adc1 5049void
e6fe60e7 5050kprobe_derived_probe_group::emit_module_init (systemtap_session& s)
6270adc1 5051{
e6fe60e7 5052 s.op->newline() << "for (i=0; i<" << probes_by_module.size() << "; i++) {";
c8d9d15e 5053 s.op->newline(1) << "struct stap_dwarfless_probe *sdp = & stap_dwarfless_probes[i];";
e6fe60e7 5054 s.op->newline() << "struct stap_dwarfless_kprobe *kp = & stap_dwarfless_kprobes[i];";
c8d9d15e
JS
5055 s.op->newline() << "void *addr = (void *) sdp->address;";
5056 s.op->newline() << "const char *symbol_name = addr ? NULL : sdp->symbol_string;";
e6fe60e7
AM
5057 s.op->newline() << "probe_point = sdp->pp;"; // for error messages
5058 s.op->newline() << "if (sdp->return_p) {";
c8d9d15e 5059 s.op->newline(1) << "kp->u.krp.kp.addr = addr;";
9f38e653 5060 s.op->newline() << "kp->u.krp.kp.symbol_name = (char *) symbol_name;";
e6fe60e7
AM
5061 s.op->newline() << "if (sdp->maxactive_p) {";
5062 s.op->newline(1) << "kp->u.krp.maxactive = sdp->maxactive_val;";
5063 s.op->newline(-1) << "} else {";
f07c3b68 5064 s.op->newline(1) << "kp->u.krp.maxactive = KRETACTIVE;";
e6fe60e7 5065 s.op->newline(-1) << "}";
88747011 5066 s.op->newline() << "kp->u.krp.handler = &enter_kretprobe2_probe;";
e6fe60e7
AM
5067 // to ensure safeness of bspcache, always use aggr_kprobe on ia64
5068 s.op->newline() << "#ifdef __ia64__";
e6fe60e7 5069 s.op->newline() << "kp->dummy.addr = kp->u.krp.kp.addr;";
c8d9d15e
JS
5070 s.op->newline() << "kp->dummy.symbol_name = kp->u.krp.kp.symbol_name;";
5071 s.op->newline() << "kp->dummy.pre_handler = NULL;";
e6fe60e7
AM
5072 s.op->newline() << "rc = register_kprobe (& kp->dummy);";
5073 s.op->newline() << "if (rc == 0) {";
5074 s.op->newline(1) << "rc = register_kretprobe (& kp->u.krp);";
5075 s.op->newline() << "if (rc != 0)";
5076 s.op->newline(1) << "unregister_kprobe (& kp->dummy);";
5077 s.op->newline(-2) << "}";
5078 s.op->newline() << "#else";
5079 s.op->newline() << "rc = register_kretprobe (& kp->u.krp);";
5080 s.op->newline() << "#endif";
5081 s.op->newline(-1) << "} else {";
5082 // to ensure safeness of bspcache, always use aggr_kprobe on ia64
c8d9d15e 5083 s.op->newline(1) << "kp->u.kp.addr = addr;";
9f38e653 5084 s.op->newline() << "kp->u.kp.symbol_name = (char *) symbol_name;";
88747011 5085 s.op->newline() << "kp->u.kp.pre_handler = &enter_kprobe2_probe;";
e6fe60e7 5086 s.op->newline() << "#ifdef __ia64__";
e6fe60e7 5087 s.op->newline() << "kp->dummy.pre_handler = NULL;";
c8d9d15e
JS
5088 s.op->newline() << "kp->dummy.addr = kp->u.kp.addr;";
5089 s.op->newline() << "kp->dummy.symbol_name = kp->u.kp.symbol_name;";
e6fe60e7
AM
5090 s.op->newline() << "rc = register_kprobe (& kp->dummy);";
5091 s.op->newline() << "if (rc == 0) {";
5092 s.op->newline(1) << "rc = register_kprobe (& kp->u.kp);";
5093 s.op->newline() << "if (rc != 0)";
5094 s.op->newline(1) << "unregister_kprobe (& kp->dummy);";
5095 s.op->newline(-2) << "}";
5096 s.op->newline() << "#else";
5097 s.op->newline() << "rc = register_kprobe (& kp->u.kp);";
5098 s.op->newline() << "#endif";
5099 s.op->newline(-1) << "}";
5100 s.op->newline() << "if (rc) {"; // PR6749: tolerate a failed register_*probe.
5101 s.op->newline(1) << "sdp->registered_p = 0;";
b350f56b 5102 s.op->newline() << "if (!sdp->optional_p)";
4a4edc21 5103 s.op->newline(1) << "_stp_warn (\"probe %s (address 0x%lx) registration error (rc %d)\", probe_point, addr, rc);";
b350f56b 5104 s.op->newline(-1) << "rc = 0;"; // continue with other probes
e6fe60e7
AM
5105 // XXX: shall we increment numskipped?
5106 s.op->newline(-1) << "}";
6270adc1 5107
e6fe60e7
AM
5108 s.op->newline() << "else sdp->registered_p = 1;";
5109 s.op->newline(-1) << "}"; // for loop
6270adc1
MH
5110}
5111
e6fe60e7
AM
5112void
5113kprobe_derived_probe_group::emit_module_exit (systemtap_session& s)
935447c8 5114{
e6fe60e7
AM
5115 //Unregister kprobes by batch interfaces.
5116 s.op->newline() << "#if defined(STAPCONF_UNREGISTER_KPROBES)";
5117 s.op->newline() << "j = 0;";
5118 s.op->newline() << "for (i=0; i<" << probes_by_module.size() << "; i++) {";
5119 s.op->newline(1) << "struct stap_dwarfless_probe *sdp = & stap_dwarfless_probes[i];";
5120 s.op->newline() << "struct stap_dwarfless_kprobe *kp = & stap_dwarfless_kprobes[i];";
5121 s.op->newline() << "if (! sdp->registered_p) continue;";
5122 s.op->newline() << "if (!sdp->return_p)";
c9116e99 5123 s.op->newline(1) << "stap_unreg_kprobes2[j++] = &kp->u.kp;";
e6fe60e7 5124 s.op->newline(-2) << "}";
c9116e99 5125 s.op->newline() << "unregister_kprobes((struct kprobe **)stap_unreg_kprobes2, j);";
e6fe60e7
AM
5126 s.op->newline() << "j = 0;";
5127 s.op->newline() << "for (i=0; i<" << probes_by_module.size() << "; i++) {";
5128 s.op->newline(1) << "struct stap_dwarfless_probe *sdp = & stap_dwarfless_probes[i];";
5129 s.op->newline() << "struct stap_dwarfless_kprobe *kp = & stap_dwarfless_kprobes[i];";
5130 s.op->newline() << "if (! sdp->registered_p) continue;";
5131 s.op->newline() << "if (sdp->return_p)";
c9116e99 5132 s.op->newline(1) << "stap_unreg_kprobes2[j++] = &kp->u.krp;";
e6fe60e7 5133 s.op->newline(-2) << "}";
c9116e99 5134 s.op->newline() << "unregister_kretprobes((struct kretprobe **)stap_unreg_kprobes2, j);";
e6fe60e7
AM
5135 s.op->newline() << "#ifdef __ia64__";
5136 s.op->newline() << "j = 0;";
5137 s.op->newline() << "for (i=0; i<" << probes_by_module.size() << "; i++) {";
5138 s.op->newline(1) << "struct stap_dwarfless_probe *sdp = & stap_dwarfless_probes[i];";
5139 s.op->newline() << "struct stap_dwarfless_kprobe *kp = & stap_dwarfless_kprobes[i];";
5140 s.op->newline() << "if (! sdp->registered_p) continue;";
c9116e99 5141 s.op->newline() << "stap_unreg_kprobes2[j++] = &kp->dummy;";
e6fe60e7 5142 s.op->newline(-1) << "}";
c9116e99 5143 s.op->newline() << "unregister_kprobes((struct kprobe **)stap_unreg_kprobes2, j);";
e6fe60e7
AM
5144 s.op->newline() << "#endif";
5145 s.op->newline() << "#endif";
3e3bd7b6 5146
e6fe60e7
AM
5147 s.op->newline() << "for (i=0; i<" << probes_by_module.size() << "; i++) {";
5148 s.op->newline(1) << "struct stap_dwarfless_probe *sdp = & stap_dwarfless_probes[i];";
5149 s.op->newline() << "struct stap_dwarfless_kprobe *kp = & stap_dwarfless_kprobes[i];";
5150 s.op->newline() << "if (! sdp->registered_p) continue;";
5151 s.op->newline() << "if (sdp->return_p) {";
5152 s.op->newline() << "#if !defined(STAPCONF_UNREGISTER_KPROBES)";
5153 s.op->newline(1) << "unregister_kretprobe (&kp->u.krp);";
5154 s.op->newline() << "#endif";
5155 s.op->newline() << "atomic_add (kp->u.krp.nmissed, & skipped_count);";
5156 s.op->newline() << "#ifdef STP_TIMING";
5157 s.op->newline() << "if (kp->u.krp.nmissed)";
5158 s.op->newline(1) << "_stp_warn (\"Skipped due to missed kretprobe/1 on '%s': %d\\n\", sdp->pp, kp->u.krp.nmissed);";
5159 s.op->newline(-1) << "#endif";
5160 s.op->newline() << "atomic_add (kp->u.krp.kp.nmissed, & skipped_count);";
5161 s.op->newline() << "#ifdef STP_TIMING";
5162 s.op->newline() << "if (kp->u.krp.kp.nmissed)";
5163 s.op->newline(1) << "_stp_warn (\"Skipped due to missed kretprobe/2 on '%s': %d\\n\", sdp->pp, kp->u.krp.kp.nmissed);";
5164 s.op->newline(-1) << "#endif";
5165 s.op->newline(-1) << "} else {";
5166 s.op->newline() << "#if !defined(STAPCONF_UNREGISTER_KPROBES)";
5167 s.op->newline(1) << "unregister_kprobe (&kp->u.kp);";
5168 s.op->newline() << "#endif";
5169 s.op->newline() << "atomic_add (kp->u.kp.nmissed, & skipped_count);";
5170 s.op->newline() << "#ifdef STP_TIMING";
5171 s.op->newline() << "if (kp->u.kp.nmissed)";
5172 s.op->newline(1) << "_stp_warn (\"Skipped due to missed kprobe on '%s': %d\\n\", sdp->pp, kp->u.kp.nmissed);";
5173 s.op->newline(-1) << "#endif";
5174 s.op->newline(-1) << "}";
5175 s.op->newline() << "#if !defined(STAPCONF_UNREGISTER_KPROBES) && defined(__ia64__)";
5176 s.op->newline() << "unregister_kprobe (&kp->dummy);";
5177 s.op->newline() << "#endif";
5178 s.op->newline() << "sdp->registered_p = 0;";
5179 s.op->newline(-1) << "}";
f8a968bc
JS
5180}
5181
e6fe60e7 5182struct kprobe_builder: public derived_probe_builder
3c1b3d06 5183{
e6fe60e7
AM
5184 kprobe_builder() {}
5185 virtual void build(systemtap_session & sess,
5186 probe * base,
5187 probe_point * location,
5188 literal_map_t const & parameters,
5189 vector<derived_probe *> & finished_results);
5190};
3c1b3d06
FCE
5191
5192
79189b84 5193void
e6fe60e7
AM
5194kprobe_builder::build(systemtap_session & sess,
5195 probe * base,
5196 probe_point * location,
5197 literal_map_t const & parameters,
5198 vector<derived_probe *> & finished_results)
79189b84 5199{
e6fe60e7 5200 string function_string_val, module_string_val;
b6371390
JS
5201 int64_t statement_num_val = 0, maxactive_val = 0;
5202 bool has_function_str, has_module_str, has_statement_num;
5203 bool has_absolute, has_return, has_maxactive;
79189b84 5204
b6371390
JS
5205 has_function_str = get_param(parameters, TOK_FUNCTION, function_string_val);
5206 has_module_str = get_param(parameters, TOK_MODULE, module_string_val);
5207 has_return = has_null_param (parameters, TOK_RETURN);
5208 has_maxactive = get_param(parameters, TOK_MAXACTIVE, maxactive_val);
5209 has_statement_num = get_param(parameters, TOK_STATEMENT, statement_num_val);
5210 has_absolute = has_null_param (parameters, TOK_ABSOLUTE);
3c1b3d06 5211
b6371390 5212 if (has_function_str)
6fb70fb7 5213 {
b6371390
JS
5214 if (has_module_str)
5215 function_string_val = module_string_val + ":" + function_string_val;
86758d5f 5216
b6371390
JS
5217 finished_results.push_back (new kprobe_derived_probe (base,
5218 location, function_string_val,
5219 0, has_return,
5220 has_statement_num,
5221 has_maxactive,
5222 maxactive_val));
6fb70fb7 5223 }
e6fe60e7 5224 else
b6371390
JS
5225 {
5226 // assert guru mode for absolute probes
5227 if ( has_statement_num && has_absolute && !base->privileged )
5228 throw semantic_error ("absolute statement probe in unprivileged script", base->tok);
5229
5230 finished_results.push_back (new kprobe_derived_probe (base,
5231 location, "",
5232 statement_num_val,
5233 has_return,
5234 has_statement_num,
5235 has_maxactive,
5236 maxactive_val));
96b030fe 5237 }
79189b84
JS
5238}
5239
5240
342d3f96 5241
0a6f5a3f
JS
5242// ------------------------------------------------------------------------
5243// statically inserted kernel-tracepoint derived probes
5244// ------------------------------------------------------------------------
5245
6fb70fb7 5246struct tracepoint_arg
79189b84 5247{
ad370dcc 5248 string name, c_type, typecast;
dcaa1a65 5249 bool usable, used, isptr;
f8a968bc 5250 Dwarf_Die type_die;
dcaa1a65 5251 tracepoint_arg(): usable(false), used(false), isptr(false) {}
6fb70fb7 5252};
79189b84 5253
0a6f5a3f
JS
5254struct tracepoint_derived_probe: public derived_probe
5255{
79189b84
JS
5256 tracepoint_derived_probe (systemtap_session& s,
5257 dwflpp& dw, Dwarf_Die& func_die,
5258 const string& tracepoint_name,
5259 probe* base_probe, probe_point* location);
bc9a523d 5260
79189b84 5261 systemtap_session& sess;
6fb70fb7
JS
5262 string tracepoint_name, header;
5263 vector <struct tracepoint_arg> args;
bc9a523d 5264
6fb70fb7 5265 void build_args(dwflpp& dw, Dwarf_Die& func_die);
e2086848 5266 void printargs (std::ostream &o) const;
79189b84 5267 void join_group (systemtap_session& s);
3e3bd7b6 5268 void print_dupe_stamp(ostream& o);
f8a968bc 5269 void emit_probe_context_vars (translator_output* o);
0a6f5a3f 5270};
79189b84
JS
5271
5272
0a6f5a3f 5273struct tracepoint_derived_probe_group: public generic_dpg<tracepoint_derived_probe>
79189b84 5274{
79189b84
JS
5275 void emit_module_decls (systemtap_session& s);
5276 void emit_module_init (systemtap_session& s);
5277 void emit_module_exit (systemtap_session& s);
0a6f5a3f 5278};
79189b84 5279
bc9a523d 5280
f8a968bc
JS
5281struct tracepoint_var_expanding_visitor: public var_expanding_visitor
5282{
5283 tracepoint_var_expanding_visitor(dwflpp& dw, const string& probe_name,
5284 vector <struct tracepoint_arg>& args):
5285 dw (dw), probe_name (probe_name), args (args) {}
5286 dwflpp& dw;
5287 const string& probe_name;
5288 vector <struct tracepoint_arg>& args;
bc9a523d 5289
f8a968bc
JS
5290 void visit_target_symbol (target_symbol* e);
5291 void visit_target_symbol_arg (target_symbol* e);
5292 void visit_target_symbol_context (target_symbol* e);
5293};
79189b84
JS
5294
5295
f8a968bc
JS
5296void
5297tracepoint_var_expanding_visitor::visit_target_symbol_arg (target_symbol* e)
75ead1f7 5298{
f8a968bc 5299 string argname = e->base_name.substr(1);
75ead1f7 5300
f8a968bc
JS
5301 // search for a tracepoint parameter matching this name
5302 tracepoint_arg *arg = NULL;
5303 for (unsigned i = 0; i < args.size(); ++i)
dcaa1a65 5304 if (args[i].usable && args[i].name == argname)
f8a968bc
JS
5305 {
5306 arg = &args[i];
5307 arg->used = true;
5308 break;
5309 }
75ead1f7 5310
f8a968bc
JS
5311 if (arg == NULL)
5312 {
5313 stringstream alternatives;
5314 for (unsigned i = 0; i < args.size(); ++i)
5315 alternatives << " $" << args[i].name;
046e7190 5316 alternatives << " $$name $$parms $$vars";
75ead1f7 5317
f8a968bc
JS
5318 // We hope that this value ends up not being referenced after all, so it
5319 // can be optimized out quietly.
5320 semantic_error* saveme =
5321 new semantic_error("unable to find tracepoint variable '" + e->base_name
5322 + "' (alternatives:" + alternatives.str () + ")", e->tok);
5323 // NB: we can have multiple errors, since a target variable
5324 // may be expanded in several different contexts:
5325 // trace ("*") { $foo->bar }
5326 saveme->chain = e->saved_conversion_error;
5327 e->saved_conversion_error = saveme;
5328 provide (e);
5329 return;
5330 }
75ead1f7 5331
f8a968bc 5332 // make sure we're not dereferencing base types
dc5a09fc
JS
5333 if (!arg->isptr)
5334 e->assert_no_components("tracepoint");
75ead1f7 5335
f8a968bc
JS
5336 // we can only write to dereferenced fields, and only if guru mode is on
5337 bool lvalue = is_active_lvalue(e);
5338 if (lvalue && (!dw.sess.guru_mode || e->components.empty()))
5339 throw semantic_error("write to tracepoint variable '" + e->base_name
5340 + "' not permitted", e->tok);
ad370dcc
JS
5341 // XXX: if a struct/union arg is passed by value, then writing to its fields
5342 // is also meaningless until you dereference past a pointer member. It's
5343 // harder to detect and prevent that though...
75ead1f7 5344
f8a968bc
JS
5345 if (e->components.empty())
5346 {
03c75a4a
JS
5347 if (e->addressof)
5348 throw semantic_error("cannot take address of tracepoint variable", e->tok);
5349
3e3bd7b6
JS
5350 // Just grab the value from the probe locals
5351 e->probe_context_var = "__tracepoint_arg_" + arg->name;
5352 e->type = pe_long;
5353 provide (e);
f8a968bc
JS
5354 }
5355 else
5356 {
5357 // Synthesize a function to dereference the dwarf fields,
5358 // with a pointer parameter that is the base tracepoint variable
5359 functiondecl *fdecl = new functiondecl;
5360 fdecl->tok = e->tok;
5361 embeddedcode *ec = new embeddedcode;
5362 ec->tok = e->tok;
75ead1f7 5363
f8a968bc
JS
5364 string fname = (string(lvalue ? "_tracepoint_tvar_set" : "_tracepoint_tvar_get")
5365 + "_" + e->base_name.substr(1)
5366 + "_" + lex_cast<string>(tick++));
75ead1f7 5367
f8a968bc
JS
5368 fdecl->name = fname;
5369 fdecl->body = ec;
75ead1f7 5370
f8a968bc
JS
5371 try
5372 {
b4c34c26 5373 ec->code = dw.literal_stmt_for_pointer (&arg->type_die, e,
f8a968bc
JS
5374 lvalue, fdecl->type);
5375 }
5376 catch (const semantic_error& er)
5377 {
5378 // We suppress this error message, and pass the unresolved
5379 // variable to the next pass. We hope that this value ends
5380 // up not being referenced after all, so it can be optimized out
5381 // quietly.
5382 semantic_error* saveme = new semantic_error (er); // copy it
f8a968bc
JS
5383 // NB: we can have multiple errors, since a target variable
5384 // may be expanded in several different contexts:
5385 // trace ("*") { $foo->bar }
5386 saveme->chain = e->saved_conversion_error;
5387 e->saved_conversion_error = saveme;
5388 provide (e);
5389 return;
5390 }
75ead1f7 5391
f8a968bc
JS
5392 // Give the fdecl an argument for the raw tracepoint value
5393 vardecl *v1 = new vardecl;
5394 v1->type = pe_long;
5395 v1->name = "pointer";
5396 v1->tok = e->tok;
5397 fdecl->formal_args.push_back(v1);
75ead1f7 5398
6fda2dff
JS
5399 // Any non-literal indexes need to be passed in too.
5400 for (unsigned i = 0; i < e->components.size(); ++i)
5401 if (e->components[i].type == target_symbol::comp_expression_array_index)
5402 {
5403 vardecl *v = new vardecl;
5404 v->type = pe_long;
5405 v->name = "index" + lex_cast<string>(i);
5406 v->tok = e->tok;
5407 fdecl->formal_args.push_back(v);
5408 }
5409
f8a968bc
JS
5410 if (lvalue)
5411 {
5412 // Modify the fdecl so it carries a pe_long formal
5413 // argument called "value".
75ead1f7 5414
f8a968bc
JS
5415 // FIXME: For the time being we only support setting target
5416 // variables which have base types; these are 'pe_long' in
5417 // stap's type vocabulary. Strings and pointers might be
5418 // reasonable, some day, but not today.
5419
5420 vardecl *v2 = new vardecl;
5421 v2->type = pe_long;
5422 v2->name = "value";
5423 v2->tok = e->tok;
5424 fdecl->formal_args.push_back(v2);
5425 }
5426 else
5427 ec->code += "/* pure */";
5428
5429 dw.sess.functions[fdecl->name] = fdecl;
75ead1f7 5430
f8a968bc
JS
5431 // Synthesize a functioncall.
5432 functioncall* n = new functioncall;
5433 n->tok = e->tok;
5434 n->function = fname;
5435 n->referent = 0; // NB: must not resolve yet, to ensure inclusion in session
75ead1f7 5436
6fda2dff
JS
5437 // make a copy of the original as a bare target symbol for the tracepoint
5438 // value, which will be passed into the dwarf dereferencing code
5439 target_symbol* e2 = deep_copy_visitor::deep_copy(e);
5440 e2->components.clear();
5441 n->args.push_back(require(e2));
5442
5443 // Any non-literal indexes need to be passed in too.
5444 for (unsigned i = 0; i < e->components.size(); ++i)
5445 if (e->components[i].type == target_symbol::comp_expression_array_index)
5446 n->args.push_back(require(e->components[i].expr_index));
75ead1f7 5447
f8a968bc
JS
5448 if (lvalue)
5449 {
5450 // Provide the functioncall to our parent, so that it can be
5451 // used to substitute for the assignment node immediately above
5452 // us.
5453 assert(!target_symbol_setter_functioncalls.empty());
5454 *(target_symbol_setter_functioncalls.top()) = n;
5455 }
75ead1f7 5456
f8a968bc
JS
5457 provide (n);
5458 }
75ead1f7
JS
5459}
5460
5461
f8a968bc
JS
5462void
5463tracepoint_var_expanding_visitor::visit_target_symbol_context (target_symbol* e)
0a6f5a3f 5464{
03c75a4a
JS
5465 if (e->addressof)
5466 throw semantic_error("cannot take address of context variable", e->tok);
5467
f8a968bc
JS
5468 if (is_active_lvalue (e))
5469 throw semantic_error("write to tracepoint '" + e->base_name + "' not permitted", e->tok);
0a6f5a3f 5470
dc5a09fc 5471 e->assert_no_components("tracepoint");
0a6f5a3f 5472
f8a968bc
JS
5473 if (e->base_name == "$$name")
5474 {
5475 // Synthesize a functioncall.
5476 functioncall* n = new functioncall;
5477 n->tok = e->tok;
5478 n->function = "_mark_name_get";
5479 n->referent = 0; // NB: must not resolve yet, to ensure inclusion in session
5480 provide (n);
5481 }
046e7190 5482 else if (e->base_name == "$$vars" || e->base_name == "$$parms")
f8a968bc 5483 {
f8a968bc 5484 print_format* pf = new print_format;
0a6f5a3f 5485
f8a968bc
JS
5486 // Convert $$vars to sprintf of a list of vars which we recursively evaluate
5487 // NB: we synthesize a new token here rather than reusing
5488 // e->tok, because print_format::print likes to use
5489 // its tok->content.
5490 token* pf_tok = new token(*e->tok);
5491 pf_tok->content = "sprintf";
0a6f5a3f 5492
f8a968bc
JS
5493 pf->tok = pf_tok;
5494 pf->print_to_stream = false;
5495 pf->print_with_format = true;
5496 pf->print_with_delim = false;
5497 pf->print_with_newline = false;
5498 pf->print_char = false;
0a6f5a3f 5499
f8a968bc 5500 for (unsigned i = 0; i < args.size(); ++i)
b278033a 5501 {
dcaa1a65
JS
5502 if (!args[i].usable)
5503 continue;
f8a968bc
JS
5504 if (i > 0)
5505 pf->raw_components += " ";
5506 pf->raw_components += args[i].name;
3e3bd7b6 5507 target_symbol *tsym = new target_symbol;
f8a968bc
JS
5508 tsym->tok = e->tok;
5509 tsym->base_name = "$" + args[i].name;
b278033a 5510
f8a968bc
JS
5511 // every variable should always be accessible!
5512 tsym->saved_conversion_error = 0;
5513 expression *texp = require (tsym); // NB: throws nothing ...
5514 assert (!tsym->saved_conversion_error); // ... but this is how we know it happened.
b278033a 5515
d0ad1746 5516 pf->raw_components += args[i].isptr ? "=%p" : "=%#x";
f8a968bc
JS
5517 pf->args.push_back(texp);
5518 }
0a6f5a3f 5519
f8a968bc
JS
5520 pf->components = print_format::string_to_components(pf->raw_components);
5521 provide (pf);
b278033a 5522 }
f8a968bc
JS
5523 else
5524 assert(0); // shouldn't get here
0a6f5a3f
JS
5525}
5526
0a6f5a3f 5527void
f8a968bc 5528tracepoint_var_expanding_visitor::visit_target_symbol (target_symbol* e)
0a6f5a3f 5529{
f8a968bc 5530 assert(e->base_name.size() > 0 && e->base_name[0] == '$');
75ead1f7 5531
046e7190
JS
5532 if (e->base_name == "$$name" ||
5533 e->base_name == "$$parms" ||
5534 e->base_name == "$$vars")
f8a968bc
JS
5535 visit_target_symbol_context (e);
5536 else
5537 visit_target_symbol_arg (e);
0a6f5a3f
JS
5538}
5539
5540
5541
79189b84
JS
5542tracepoint_derived_probe::tracepoint_derived_probe (systemtap_session& s,
5543 dwflpp& dw, Dwarf_Die& func_die,
5544 const string& tracepoint_name,
5545 probe* base, probe_point* loc):
5546 derived_probe (base, new probe_point(*loc) /* .components soon rewritten */),
5547 sess (s), tracepoint_name (tracepoint_name)
56894e91 5548{
79189b84
JS
5549 // create synthetic probe point name; preserve condition
5550 vector<probe_point::component*> comps;
5551 comps.push_back (new probe_point::component (TOK_KERNEL));
5552 comps.push_back (new probe_point::component (TOK_TRACE, new literal_string (tracepoint_name)));
5553 this->sole_location()->components = comps;
5554
6fb70fb7
JS
5555 // fill out the available arguments in this tracepoint
5556 build_args(dw, func_die);
56894e91 5557
6fb70fb7
JS
5558 // determine which header defined this tracepoint
5559 string decl_file = dwarf_decl_file(&func_die);
5560 size_t header_pos = decl_file.rfind("trace/");
5561 if (header_pos == string::npos)
5562 throw semantic_error ("cannot parse header location for tracepoint '"
5563 + tracepoint_name + "' in '"
5564 + decl_file + "'");
5565 header = decl_file.substr(header_pos);
56894e91 5566
6fb70fb7
JS
5567 // tracepoints from FOO_event_types.h should really be included from FOO.h
5568 // XXX can dwarf tell us the include hierarchy? it would be better to
5569 // ... walk up to see which one was directly included by tracequery.c
3c1b3d06 5570 // XXX: see also PR9993.
6fb70fb7
JS
5571 header_pos = header.find("_event_types");
5572 if (header_pos != string::npos)
5573 header.erase(header_pos, 12);
56894e91 5574
f8a968bc
JS
5575 // Now expand the local variables in the probe body
5576 tracepoint_var_expanding_visitor v (dw, name, args);
8b095b45 5577 v.replace (this->body);
56894e91 5578
79189b84
JS
5579 if (sess.verbose > 2)
5580 clog << "tracepoint-based " << name << " tracepoint='" << tracepoint_name
5581 << "'" << endl;
5582}
dc38c0ae 5583
56894e91 5584
6fb70fb7
JS
5585static bool
5586dwarf_type_name(Dwarf_Die& type_die, string& c_type)
b20febf3 5587{
219e62c7
JS
5588 // if we've gotten down to a basic type, then we're done
5589 bool done = true;
5590 switch (dwarf_tag(&type_die))
6fb70fb7 5591 {
219e62c7
JS
5592 case DW_TAG_structure_type:
5593 c_type.append("struct ");
5594 break;
5595 case DW_TAG_union_type:
5596 c_type.append("union ");
5597 break;
5598 case DW_TAG_typedef:
5599 case DW_TAG_base_type:
5600 break;
5601 default:
5602 done = false;
5603 break;
5604 }
5605 if (done)
5606 {
96694042
JS
5607 // this follows gdb precedent that anonymous structs/unions
5608 // are displayed as "struct {...}" and "union {...}".
5609 c_type.append(dwarf_diename(&type_die) ?: "{...}");
6fb70fb7
JS
5610 return true;
5611 }
56894e91 5612
6fb70fb7 5613 // otherwise, this die is a type modifier.
56894e91 5614
6fb70fb7 5615 // recurse into the referent type
219e62c7 5616 // if it can't be named, just call it "void"
6fb70fb7
JS
5617 Dwarf_Attribute subtype_attr;
5618 Dwarf_Die subtype_die;
5619 if (!dwarf_attr_integrate(&type_die, DW_AT_type, &subtype_attr)
5620 || !dwarf_formref_die(&subtype_attr, &subtype_die)
5621 || !dwarf_type_name(subtype_die, c_type))
219e62c7 5622 c_type = "void";
dc38c0ae 5623
6fb70fb7
JS
5624 const char *suffix = NULL;
5625 switch (dwarf_tag(&type_die))
5626 {
5627 case DW_TAG_pointer_type:
5628 suffix = "*";
5629 break;
5630 case DW_TAG_array_type:
5631 suffix = "[]";
5632 break;
5633 case DW_TAG_const_type:
5634 suffix = " const";
5635 break;
5636 case DW_TAG_volatile_type:
5637 suffix = " volatile";
5638 break;
5639 default:
5640 return false;
5641 }
5642 c_type.append(suffix);
dc38c0ae 5643
219e62c7
JS
5644 // XXX HACK! The va_list isn't usable as found in the debuginfo...
5645 if (c_type == "struct __va_list_tag*")
5646 c_type = "va_list";
5647
6fb70fb7 5648 return true;
56894e91
JS
5649}
5650
5651
f8a968bc 5652static bool
dcaa1a65 5653resolve_tracepoint_arg_type(tracepoint_arg& arg)
46b84a80 5654{
f8a968bc 5655 Dwarf_Attribute type_attr;
dcaa1a65 5656 switch (dwarf_tag(&arg.type_die))
b20febf3 5657 {
f8a968bc
JS
5658 case DW_TAG_typedef:
5659 case DW_TAG_const_type:
5660 case DW_TAG_volatile_type:
5661 // iterate on the referent type
dcaa1a65
JS
5662 return (dwarf_attr_integrate(&arg.type_die, DW_AT_type, &type_attr)
5663 && dwarf_formref_die(&type_attr, &arg.type_die)
5664 && resolve_tracepoint_arg_type(arg));
f8a968bc
JS
5665 case DW_TAG_base_type:
5666 // base types will simply be treated as script longs
dcaa1a65 5667 arg.isptr = false;
f8a968bc
JS
5668 return true;
5669 case DW_TAG_pointer_type:
dcaa1a65
JS
5670 // pointers can be treated as script longs,
5671 // and if we know their type, they can also be dereferenced
5672 if (dwarf_attr_integrate(&arg.type_die, DW_AT_type, &type_attr)
5673 && dwarf_formref_die(&type_attr, &arg.type_die))
5674 arg.isptr = true;
ad370dcc
JS
5675 arg.typecast = "(intptr_t)";
5676 return true;
5677 case DW_TAG_structure_type:
5678 case DW_TAG_union_type:
5679 // for structs/unions which are passed by value, we turn it into
5680 // a pointer that can be dereferenced.
5681 arg.isptr = true;
5682 arg.typecast = "(intptr_t)&";
dcaa1a65 5683 return true;
f8a968bc
JS
5684 default:
5685 // should we consider other types too?
5686 return false;
b20febf3 5687 }
56894e91
JS
5688}
5689
5690
5691void
6fb70fb7 5692tracepoint_derived_probe::build_args(dwflpp& dw, Dwarf_Die& func_die)
56894e91 5693{
6fb70fb7
JS
5694 Dwarf_Die arg;
5695 if (dwarf_child(&func_die, &arg) == 0)
5696 do
5697 if (dwarf_tag(&arg) == DW_TAG_formal_parameter)
5698 {
5699 // build a tracepoint_arg for this parameter
5700 tracepoint_arg tparg;
23d106b9 5701 tparg.name = dwarf_diename(&arg);
56894e91 5702
6fb70fb7
JS
5703 // read the type of this parameter
5704 Dwarf_Attribute type_attr;
6fb70fb7 5705 if (!dwarf_attr_integrate (&arg, DW_AT_type, &type_attr)
f8a968bc 5706 || !dwarf_formref_die (&type_attr, &tparg.type_die)
dcaa1a65 5707 || !dwarf_type_name(tparg.type_die, tparg.c_type))
6fb70fb7
JS
5708 throw semantic_error ("cannot get type of tracepoint '"
5709 + tracepoint_name + "' parameter '"
5710 + tparg.name + "'");
a68f81a2 5711
dcaa1a65 5712 tparg.usable = resolve_tracepoint_arg_type(tparg);
6fb70fb7
JS
5713 args.push_back(tparg);
5714 if (sess.verbose > 4)
5715 clog << "found parameter for tracepoint '" << tracepoint_name
5716 << "': type:'" << tparg.c_type
5717 << "' name:'" << tparg.name << "'" << endl;
5718 }
5719 while (dwarf_siblingof(&arg, &arg) == 0);
56894e91
JS
5720}
5721
dc38c0ae 5722void
e2086848 5723tracepoint_derived_probe::printargs(std::ostream &o) const
dc38c0ae 5724{
dcaa1a65
JS
5725 for (unsigned i = 0; i < args.size(); ++i)
5726 if (args[i].usable)
5727 o << " $" << args[i].name << ":" << args[i].c_type;
dc38c0ae
DS
5728}
5729
79189b84
JS
5730void
5731tracepoint_derived_probe::join_group (systemtap_session& s)
197a4d62 5732{
79189b84
JS
5733 if (! s.tracepoint_derived_probes)
5734 s.tracepoint_derived_probes = new tracepoint_derived_probe_group ();
5735 s.tracepoint_derived_probes->enroll (this);
5736}
e38d6504 5737
56894e91 5738
197a4d62 5739void
3e3bd7b6 5740tracepoint_derived_probe::print_dupe_stamp(ostream& o)
56894e91 5741{
3e3bd7b6
JS
5742 for (unsigned i = 0; i < args.size(); i++)
5743 if (args[i].used)
5744 o << "__tracepoint_arg_" << args[i].name << endl;
197a4d62 5745}
56894e91 5746
3e3bd7b6 5747
197a4d62 5748void
f8a968bc 5749tracepoint_derived_probe::emit_probe_context_vars (translator_output* o)
197a4d62 5750{
f8a968bc
JS
5751 for (unsigned i = 0; i < args.size(); i++)
5752 if (args[i].used)
5753 o->newline() << "int64_t __tracepoint_arg_" << args[i].name << ";";
56894e91
JS
5754}
5755
5756
3c1b3d06 5757static vector<string> tracepoint_extra_headers ()
47dd066d 5758{
3c1b3d06
FCE
5759 vector<string> they_live;
5760 // PR 9993
5761 // XXX: may need this to be configurable
5762 they_live.push_back ("linux/skbuff.h");
5763 return they_live;
5764}
47dd066d
WC
5765
5766
5767void
79189b84 5768tracepoint_derived_probe_group::emit_module_decls (systemtap_session& s)
47dd066d 5769{
79189b84
JS
5770 if (probes.empty())
5771 return;
47dd066d 5772
96b030fe
JS
5773 s.op->newline() << "/* ---- tracepoint probes ---- */";
5774 s.op->newline();
79189b84 5775
3c1b3d06
FCE
5776 // PR9993: Add extra headers to work around undeclared types in individual
5777 // include/trace/foo.h files
5778 const vector<string>& extra_headers = tracepoint_extra_headers ();
5779 for (unsigned z=0; z<extra_headers.size(); z++)
5780 s.op->newline() << "#include <" << extra_headers[z] << ">\n";
47dd066d 5781
6fb70fb7
JS
5782 for (unsigned i = 0; i < probes.size(); ++i)
5783 {
5784 tracepoint_derived_probe *p = probes[i];
47dd066d 5785
96b030fe
JS
5786 // emit a separate entry function for each probe, since tracepoints
5787 // don't provide any sort of context pointer.
6fb70fb7
JS
5788 s.op->newline() << "#include <" << p->header << ">";
5789 s.op->newline() << "static void enter_tracepoint_probe_" << i << "(";
8df306c4
JS
5790 if (p->args.size() == 0)
5791 s.op->line() << "void";
6fb70fb7
JS
5792 for (unsigned j = 0; j < p->args.size(); ++j)
5793 {
5794 if (j > 0)
5795 s.op->line() << ", ";
5796 s.op->line() << p->args[j].c_type << " __tracepoint_arg_" << p->args[j].name;
5797 }
5798 s.op->line() << ") {";
5799 s.op->indent(1);
ad002306 5800 common_probe_entryfn_prologue (s.op, "STAP_SESSION_RUNNING",
c12d974f 5801 lex_cast_qstring (*p->sole_location()));
f8a968bc 5802 s.op->newline() << "c->marker_name = "
c12d974f
FCE
5803 << lex_cast_qstring (p->tracepoint_name)
5804 << ";";
f8a968bc
JS
5805 for (unsigned j = 0; j < p->args.size(); ++j)
5806 if (p->args[j].used)
5807 {
66671fd8 5808 s.op->newline() << "c->probe_locals." << p->name << ".__tracepoint_arg_"
f8a968bc 5809 << p->args[j].name << " = (int64_t)";
ad370dcc 5810 s.op->line() << p->args[j].typecast;
f8a968bc
JS
5811 s.op->line() << "__tracepoint_arg_" << p->args[j].name << ";";
5812 }
6fb70fb7
JS
5813 s.op->newline() << p->name << " (c);";
5814 common_probe_entryfn_epilogue (s.op);
5815 s.op->newline(-1) << "}";
47dd066d 5816
96b030fe
JS
5817 // emit normalized registration functions
5818 s.op->newline() << "static int register_tracepoint_probe_" << i << "(void) {";
5819 s.op->newline(1) << "return register_trace_" << p->tracepoint_name
5820 << "(enter_tracepoint_probe_" << i << ");";
5821 s.op->newline(-1) << "}";
47dd066d 5822
86758d5f
JS
5823 // NB: we're not prepared to deal with unreg failures. However, failures
5824 // can only occur if the tracepoint doesn't exist (yet?), or if we
5825 // weren't even registered. The former should be OKed by the initial
5826 // registration call, and the latter is safe to ignore.
5827 s.op->newline() << "static void unregister_tracepoint_probe_" << i << "(void) {";
5828 s.op->newline(1) << "(void) unregister_trace_" << p->tracepoint_name
96b030fe
JS
5829 << "(enter_tracepoint_probe_" << i << ");";
5830 s.op->newline(-1) << "}";
6fb70fb7 5831 s.op->newline();
af304783
DS
5832 }
5833
96b030fe
JS
5834 // emit an array of registration functions for easy init/shutdown
5835 s.op->newline() << "static struct stap_tracepoint_probe {";
5836 s.op->newline(1) << "int (*reg)(void);";
86758d5f 5837 s.op->newline(0) << "void (*unreg)(void);";
96b030fe
JS
5838 s.op->newline(-1) << "} stap_tracepoint_probes[] = {";
5839 s.op->indent(1);
5840 for (unsigned i = 0; i < probes.size(); ++i)
5841 {
5842 s.op->newline () << "{";
5843 s.op->line() << " .reg=&register_tracepoint_probe_" << i << ",";
5844 s.op->line() << " .unreg=&unregister_tracepoint_probe_" << i;
5845 s.op->line() << " },";
5846 }
5847 s.op->newline(-1) << "};";
5848 s.op->newline();
47dd066d
WC
5849}
5850
5851
79189b84
JS
5852void
5853tracepoint_derived_probe_group::emit_module_init (systemtap_session &s)
47dd066d 5854{
79189b84
JS
5855 if (probes.size () == 0)
5856 return;
47dd066d 5857
79189b84 5858 s.op->newline() << "/* init tracepoint probes */";
96b030fe
JS
5859 s.op->newline() << "for (i=0; i<" << probes.size() << "; i++) {";
5860 s.op->newline(1) << "rc = stap_tracepoint_probes[i].reg();";
5861 s.op->newline() << "if (rc) {";
5862 s.op->newline(1) << "for (j=i-1; j>=0; j--)"; // partial rollback
5863 s.op->newline(1) << "stap_tracepoint_probes[j].unreg();";
5864 s.op->newline(-1) << "break;"; // don't attempt to register any more probes
5865 s.op->newline(-1) << "}";
5866 s.op->newline(-1) << "}";
47dd066d 5867
bc9a523d
FCE
5868 // This would be technically proper (on those autoconf-detectable
5869 // kernels that include this function in tracepoint.h), however we
5870 // already make several calls to synchronze_sched() during our
5871 // shutdown processes.
47dd066d 5872
bc9a523d
FCE
5873 // s.op->newline() << "if (rc)";
5874 // s.op->newline(1) << "tracepoint_synchronize_unregister();";
5875 // s.op->indent(-1);
79189b84 5876}
47dd066d
WC
5877
5878
79189b84
JS
5879void
5880tracepoint_derived_probe_group::emit_module_exit (systemtap_session& s)
47dd066d 5881{
79189b84
JS
5882 if (probes.empty())
5883 return;
47dd066d 5884
96b030fe
JS
5885 s.op->newline() << "/* deregister tracepoint probes */";
5886 s.op->newline() << "for (i=0; i<" << probes.size() << "; i++)";
5887 s.op->newline(1) << "stap_tracepoint_probes[i].unreg();";
5888 s.op->indent(-1);
47dd066d 5889
bc9a523d 5890 // Not necessary: see above.
47dd066d 5891
bc9a523d 5892 // s.op->newline() << "tracepoint_synchronize_unregister();";
79189b84 5893}
b20febf3 5894
47dd066d 5895
75ead1f7 5896struct tracepoint_query : public base_query
47dd066d 5897{
75ead1f7
JS
5898 tracepoint_query(dwflpp & dw, const string & tracepoint,
5899 probe * base_probe, probe_point * base_loc,
5900 vector<derived_probe *> & results):
5901 base_query(dw, "*"), tracepoint(tracepoint),
5902 base_probe(base_probe), base_loc(base_loc),
5903 results(results) {}
47dd066d 5904
75ead1f7 5905 const string& tracepoint;
47dd066d 5906
75ead1f7
JS
5907 probe * base_probe;
5908 probe_point * base_loc;
5909 vector<derived_probe *> & results;
f982c59b 5910 set<string> probed_names;
47dd066d 5911
75ead1f7
JS
5912 void handle_query_module();
5913 int handle_query_cu(Dwarf_Die * cudie);
5914 int handle_query_func(Dwarf_Die * func);
b20febf3 5915
75ead1f7
JS
5916 static int tracepoint_query_cu (Dwarf_Die * cudie, void * arg);
5917 static int tracepoint_query_func (Dwarf_Die * func, base_query * query);
5918};
47dd066d
WC
5919
5920
5921void
75ead1f7 5922tracepoint_query::handle_query_module()
47dd066d 5923{
75ead1f7
JS
5924 // look for the tracepoints in each CU
5925 dw.iterate_over_cus(tracepoint_query_cu, this);
47dd066d
WC
5926}
5927
5928
75ead1f7
JS
5929int
5930tracepoint_query::handle_query_cu(Dwarf_Die * cudie)
47dd066d 5931{
75ead1f7 5932 dw.focus_on_cu (cudie);
47dd066d 5933
75ead1f7
JS
5934 // look at each function to see if it's a tracepoint
5935 string function = "stapprobe_" + tracepoint;
5936 return dw.iterate_over_functions (tracepoint_query_func, this, function);
47dd066d
WC
5937}
5938
5939
75ead1f7
JS
5940int
5941tracepoint_query::handle_query_func(Dwarf_Die * func)
47dd066d 5942{
75ead1f7 5943 dw.focus_on_function (func);
47dd066d 5944
75ead1f7
JS
5945 assert(dw.function_name.compare(0, 10, "stapprobe_") == 0);
5946 string tracepoint_instance = dw.function_name.substr(10);
f982c59b
JS
5947
5948 // check for duplicates -- sometimes tracepoint headers may be indirectly
5949 // included in more than one of our tracequery modules.
5950 if (!probed_names.insert(tracepoint_instance).second)
5951 return DWARF_CB_OK;
5952
79189b84
JS
5953 derived_probe *dp = new tracepoint_derived_probe (dw.sess, dw, *func,
5954 tracepoint_instance,
5955 base_probe, base_loc);
5956 results.push_back (dp);
75ead1f7 5957 return DWARF_CB_OK;
47dd066d
WC
5958}
5959
5960
75ead1f7
JS
5961int
5962tracepoint_query::tracepoint_query_cu (Dwarf_Die * cudie, void * arg)
47dd066d 5963{
75ead1f7
JS
5964 tracepoint_query * q = static_cast<tracepoint_query *>(arg);
5965 if (pending_interrupts) return DWARF_CB_ABORT;
5966 return q->handle_query_cu(cudie);
47dd066d
WC
5967}
5968
5969
75ead1f7
JS
5970int
5971tracepoint_query::tracepoint_query_func (Dwarf_Die * func, base_query * query)
47dd066d 5972{
75ead1f7
JS
5973 tracepoint_query * q = static_cast<tracepoint_query *>(query);
5974 if (pending_interrupts) return DWARF_CB_ABORT;
5975 return q->handle_query_func(func);
47dd066d
WC
5976}
5977
5978
0a6f5a3f 5979struct tracepoint_builder: public derived_probe_builder
47dd066d 5980{
0a6f5a3f
JS
5981private:
5982 dwflpp *dw;
5983 bool init_dw(systemtap_session& s);
f982c59b 5984 string get_tracequery_module(systemtap_session& s, const string& header);
47dd066d 5985
0a6f5a3f 5986public:
47dd066d 5987
0a6f5a3f
JS
5988 tracepoint_builder(): dw(0) {}
5989 ~tracepoint_builder() { delete dw; }
47dd066d 5990
0a6f5a3f
JS
5991 void build_no_more (systemtap_session& s)
5992 {
5993 if (dw && s.verbose > 3)
5994 clog << "tracepoint_builder releasing dwflpp" << endl;
5995 delete dw;
5996 dw = NULL;
5997 }
47dd066d 5998
0a6f5a3f
JS
5999 void build(systemtap_session& s,
6000 probe *base, probe_point *location,
6001 literal_map_t const& parameters,
6002 vector<derived_probe*>& finished_results);
6003};
47dd066d 6004
47dd066d 6005
f982c59b
JS
6006string
6007tracepoint_builder::get_tracequery_module(systemtap_session& s,
6008 const string& header)
0a6f5a3f 6009{
c95eddf7
JS
6010 if (s.verbose > 2)
6011 clog << "Pass 2: getting a tracequery for " << header << endl;
6012
a2639cb7 6013 string tracequery_path;
b278033a
JS
6014 if (s.use_cache)
6015 {
6016 // see if the cached module exists
f982c59b 6017 tracequery_path = find_tracequery_hash(s, header);
a2639cb7 6018 if (!tracequery_path.empty())
b278033a 6019 {
a2639cb7 6020 int fd = open(tracequery_path.c_str(), O_RDONLY);
b278033a
JS
6021 if (fd != -1)
6022 {
6023 if (s.verbose > 2)
a2639cb7 6024 clog << "Pass 2: using cached " << tracequery_path << endl;
b278033a 6025 close(fd);
f982c59b 6026 return tracequery_path;
b278033a
JS
6027 }
6028 }
6029 }
47dd066d 6030
b278033a 6031 // no cached module, time to make it
f982c59b
JS
6032
6033 size_t root_pos = header.rfind("/include/");
6034 string short_header = (root_pos != string::npos) ?
6035 header.substr(root_pos + 9) : header;
6036
0a6f5a3f 6037 string tracequery_ko;
f982c59b
JS
6038 int rc = make_tracequery(s, tracequery_ko, short_header,
6039 tracepoint_extra_headers());
0a6f5a3f 6040 if (rc != 0)
c95eddf7 6041 tracequery_ko = "/dev/null";
47dd066d 6042
b278033a 6043 if (s.use_cache)
47dd066d 6044 {
b278033a
JS
6045 // try to save tracequery in the cache
6046 if (s.verbose > 2)
6047 clog << "Copying " << tracequery_ko
a2639cb7 6048 << " to " << tracequery_path << endl;
b278033a 6049 if (copy_file(tracequery_ko.c_str(),
a2639cb7 6050 tracequery_path.c_str()) != 0)
b278033a 6051 cerr << "Copy failed (\"" << tracequery_ko << "\" to \""
a2639cb7 6052 << tracequery_path << "\"): " << strerror(errno) << endl;
47dd066d 6053 }
f982c59b
JS
6054 return tracequery_ko;
6055}
6056
6057
6058bool
6059tracepoint_builder::init_dw(systemtap_session& s)
6060{
6061 if (dw != NULL)
6062 return true;
6063
6064 vector<string> tracequery_modules;
6065
6066 glob_t trace_glob;
6067 string globs[] = {
6068 "/include/trace/*.h",
6069 "/include/trace/events/*.h",
6070 "/source/include/trace/*.h",
6071 "/source/include/trace/events/*.h",
6072 };
6073 for (unsigned z = 0; z < sizeof(globs) / sizeof(globs[0]); z++)
6074 {
6075 string glob_str(s.kernel_build_tree + globs[z]);
6076 glob(glob_str.c_str(), 0, NULL, &trace_glob);
6077 for (unsigned i = 0; i < trace_glob.gl_pathc; ++i)
6078 {
6079 string header(trace_glob.gl_pathv[i]);
6080
6081 // filter out a few known "internal-only" headers
6082 if (header.find("/ftrace.h") != string::npos)
6083 continue;
6084 if (header.find("/trace_events.h") != string::npos)
6085 continue;
6086 if (header.find("_event_types.h") != string::npos)
6087 continue;
6088
6089 string tracequery_path = get_tracequery_module(s, header);
c95eddf7
JS
6090
6091 /* NB: An empty tracequery means that the
6092 * header didn't even compile correctly. */
6093 if (get_file_size(tracequery_path))
f982c59b
JS
6094 tracequery_modules.push_back(tracequery_path);
6095 }
6096 globfree(&trace_glob);
6097 }
6098
6099 // TODO: consider other sources of tracepoint headers too, like from
6100 // a command-line parameter or some environment or .systemtaprc
47dd066d 6101
f982c59b 6102 dw = new dwflpp(s, tracequery_modules);
0a6f5a3f
JS
6103 return true;
6104}
47dd066d 6105
47dd066d 6106
0a6f5a3f
JS
6107void
6108tracepoint_builder::build(systemtap_session& s,
6109 probe *base, probe_point *location,
6110 literal_map_t const& parameters,
6111 vector<derived_probe*>& finished_results)
6112{
6113 if (!init_dw(s))
6114 return;
47dd066d 6115
75ead1f7
JS
6116 string tracepoint;
6117 assert(get_param (parameters, TOK_TRACE, tracepoint));
47dd066d 6118
75ead1f7 6119 tracepoint_query q(*dw, tracepoint, base, location, finished_results);
51178501 6120 dw->iterate_over_modules(&query_module, &q);
47dd066d 6121}
47dd066d 6122
e6fe60e7 6123
47dd066d 6124
b55bc428 6125// ------------------------------------------------------------------------
bd2b1e68 6126// Standard tapset registry.
b55bc428
FCE
6127// ------------------------------------------------------------------------
6128
7a053d3b 6129void
f8220a7b 6130register_standard_tapsets(systemtap_session & s)
b55bc428 6131{
47e0478e 6132 register_tapset_been(s);
93646f4d 6133 register_tapset_itrace(s);
dd0e4fa7 6134 register_tapset_mark(s);
01c2eefe 6135 register_tapset_perfmon(s);
7a212aa8 6136 register_tapset_procfs(s);
912e8c59 6137 register_tapset_timers(s);
b84779a5 6138 register_tapset_utrace(s);
b98a8d73 6139
7a24d422 6140 // dwarf-based kprobe/uprobe parts
c4ce66a1 6141 dwarf_derived_probe::register_patterns(s);
30a279be 6142
888af770
FCE
6143 // XXX: user-space starter set
6144 s.pattern_root->bind_num(TOK_PROCESS)
6145 ->bind_num(TOK_STATEMENT)->bind(TOK_ABSOLUTE)
6146 ->bind(new uprobe_builder ());
6147 s.pattern_root->bind_num(TOK_PROCESS)
6148 ->bind_num(TOK_STATEMENT)->bind(TOK_ABSOLUTE)->bind(TOK_RETURN)
6149 ->bind(new uprobe_builder ());
6150
0a6f5a3f
JS
6151 // kernel tracepoint probes
6152 s.pattern_root->bind(TOK_KERNEL)->bind_str(TOK_TRACE)
6153 ->bind(new tracepoint_builder());
6154
e6fe60e7
AM
6155 // Kprobe based probe
6156 s.pattern_root->bind(TOK_KPROBE)->bind_str(TOK_FUNCTION)
6157 ->bind(new kprobe_builder());
6158 s.pattern_root->bind(TOK_KPROBE)->bind_str(TOK_MODULE)
6159 ->bind_str(TOK_FUNCTION)->bind(new kprobe_builder());
6160 s.pattern_root->bind(TOK_KPROBE)->bind_str(TOK_FUNCTION)->bind(TOK_RETURN)
6161 ->bind(new kprobe_builder());
b6371390
JS
6162 s.pattern_root->bind(TOK_KPROBE)->bind_str(TOK_FUNCTION)->bind(TOK_RETURN)
6163 ->bind_num(TOK_MAXACTIVE)->bind(new kprobe_builder());
e6fe60e7
AM
6164 s.pattern_root->bind(TOK_KPROBE)->bind_str(TOK_MODULE)
6165 ->bind_str(TOK_FUNCTION)->bind(TOK_RETURN)->bind(new kprobe_builder());
b6371390
JS
6166 s.pattern_root->bind(TOK_KPROBE)->bind_str(TOK_MODULE)
6167 ->bind_str(TOK_FUNCTION)->bind(TOK_RETURN)
6168 ->bind_num(TOK_MAXACTIVE)->bind(new kprobe_builder());
e6fe60e7
AM
6169 s.pattern_root->bind(TOK_KPROBE)->bind_num(TOK_STATEMENT)
6170 ->bind(TOK_ABSOLUTE)->bind(new kprobe_builder());
b55bc428 6171}
dc38c0ae
DS
6172
6173
b20febf3
FCE
6174vector<derived_probe_group*>
6175all_session_groups(systemtap_session& s)
dc38c0ae 6176{
b20febf3 6177 vector<derived_probe_group*> g;
912e8c59
JS
6178
6179#define DOONE(x) \
6180 if (s. x##_derived_probes) \
6181 g.push_back ((derived_probe_group*)(s. x##_derived_probes))
ab655cf8
DS
6182
6183 // Note that order *is* important here. We want to make sure we
6184 // register (actually run) begin probes before any other probe type
6185 // is run. Similarly, when unregistering probes, we want to
6186 // unregister (actually run) end probes after every other probe type
6187 // has be unregistered. To do the latter,
6188 // c_unparser::emit_module_exit() will run this list backwards.
b20febf3
FCE
6189 DOONE(be);
6190 DOONE(dwarf);
888af770 6191 DOONE(uprobe);
b20febf3
FCE
6192 DOONE(timer);
6193 DOONE(profile);
6194 DOONE(mark);
0a6f5a3f 6195 DOONE(tracepoint);
e6fe60e7 6196 DOONE(kprobe);
b20febf3
FCE
6197 DOONE(hrtimer);
6198 DOONE(perfmon);
ce82316f 6199 DOONE(procfs);
935447c8
DS
6200
6201 // Another "order is important" item. We want to make sure we
6202 // "register" the dummy task_finder probe group after all probe
6203 // groups that use the task_finder.
6204 DOONE(utrace);
a96d1db0 6205 DOONE(itrace);
935447c8 6206 DOONE(task_finder);
b20febf3
FCE
6207#undef DOONE
6208 return g;
46b84a80 6209}
73267b89
JS
6210
6211/* vim: set sw=2 ts=8 cino=>4,n-2,{2,^-2,t0,(0,u0,w1,M1 : */
This page took 1.865324 seconds and 5 git commands to generate.