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