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