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