]> sourceware.org Git - systemtap.git/blame - tapsets.cxx
PR11338 (partial): Used @defined in linuxmib.stp and new testcase.
[systemtap.git] / tapsets.cxx
CommitLineData
56e12059 1// tapset resolution
bbafcb1e 2// Copyright (C) 2005-2010 Red Hat Inc.
aa30ccd3 3// Copyright (C) 2005-2007 Intel Corporation.
0b8f6579 4// Copyright (C) 2008 James.Bottomley@HansenPartnership.com
56e12059
FCE
5//
6// This file is part of systemtap, and is free software. You can
7// redistribute it and/or modify it under the terms of the GNU General
8// Public License (GPL); either version 2, or (at your option) any
9// later version.
10
11#include "config.h"
12#include "staptree.h"
13#include "elaborate.h"
b55bc428 14#include "tapsets.h"
93646f4d 15#include "task_finder.h"
56e12059 16#include "translate.h"
dc38c0ae 17#include "session.h"
72dbc915 18#include "util.h"
0a6f5a3f 19#include "buildrun.h"
86bf665e 20#include "dwarf_wrappers.h"
2e67a43b 21#include "auto_free.h"
b278033a 22#include "hash.h"
440f755a 23#include "dwflpp.h"
5f8ca04f 24#include "setupdwfl.h"
aff5d390 25#include "sys/sdt.h"
bd2b1e68 26
3b579393
FCE
27#include <cstdlib>
28#include <algorithm>
bd2b1e68 29#include <deque>
56e12059 30#include <iostream>
bd2b1e68 31#include <map>
ec4373ff 32#include <set>
56e12059 33#include <sstream>
bd2b1e68 34#include <stdexcept>
b55bc428 35#include <vector>
e36387d7 36#include <cstdarg>
29e64872 37#include <cassert>
1969b5bc 38#include <iomanip>
f781f849 39#include <cerrno>
bd2b1e68
GH
40
41extern "C" {
df8fadee 42#include <fcntl.h>
bd2b1e68 43#include <elfutils/libdwfl.h>
7a053d3b 44#include <elfutils/libdw.h>
77de5e9e
GH
45#include <dwarf.h>
46#include <elf.h>
47#include <obstack.h>
b20febf3 48#include <glob.h>
30a279be 49#include <fnmatch.h>
5f0a03a6 50#include <stdio.h>
349dc70e 51#include <sys/types.h>
aaf7ffe8 52#include <math.h>
aff5d390 53#include <regex.h>
4b1ad75e
RM
54
55#define __STDC_FORMAT_MACROS
56#include <inttypes.h>
bd2b1e68 57}
77de5e9e 58
56e12059
FCE
59
60using namespace std;
2171f774 61using namespace __gnu_cxx;
56e12059 62
47dd066d 63
b20febf3
FCE
64
65// ------------------------------------------------------------------------
faea5e16
JS
66
67string
68common_probe_init (derived_probe* p)
69{
70 ostringstream o;
71 o << "STAP_PROBE_INIT(&" << p->name << ", "
2d767770
JS
72 << lex_cast_qstring (*p->sole_location()) << ", "
73 << lex_cast_qstring (*p->script_location()) << ")";
faea5e16
JS
74 return o.str();
75}
76
77
b20febf3 78void
a58d79d0 79common_probe_entryfn_prologue (translator_output* o, string statestr,
faea5e16 80 string probe, bool overload_processing)
b20febf3 81{
72d18b98 82 o->newline() << "struct context* __restrict__ c;";
e0a17418
JS
83 o->newline() << "#if !INTERRUPTIBLE";
84 o->newline() << "unsigned long flags;";
85 o->newline() << "#endif";
b20febf3 86
a58d79d0
DS
87 if (overload_processing)
88 o->newline() << "#if defined(STP_TIMING) || defined(STP_OVERLOAD)";
89 else
90 o->newline() << "#ifdef STP_TIMING";
91 o->newline() << "cycles_t cycles_atstart = get_cycles ();";
b20febf3 92 o->newline() << "#endif";
b20febf3 93
e0a17418
JS
94 o->newline() << "#if INTERRUPTIBLE";
95 o->newline() << "preempt_disable ();";
96 o->newline() << "#else";
97 o->newline() << "local_irq_save (flags);";
98 o->newline() << "#endif";
b20febf3 99
c931ec8a 100 // Check for enough free enough stack space
d05a1d00 101 o->newline() << "if (unlikely ((((unsigned long) (& c)) & (THREAD_SIZE-1))"; // free space
a63401b1 102 o->newline(1) << "< (MINSTACKSPACE + sizeof (struct thread_info)))) {"; // needed space
d05a1d00
FCE
103 // XXX: may need porting to platforms where task_struct is not at bottom of kernel stack
104 // NB: see also CONFIG_DEBUG_STACKOVERFLOW
b3c3ca7c
FCE
105 o->newline() << "atomic_inc (& skipped_count);";
106 o->newline() << "#ifdef STP_TIMING";
107 o->newline() << "atomic_inc (& skipped_count_lowstack);";
108 o->newline() << "#endif";
c931ec8a
FCE
109 o->newline() << "goto probe_epilogue;";
110 o->newline(-1) << "}";
111
b20febf3
FCE
112 o->newline() << "if (atomic_read (&session_state) != " << statestr << ")";
113 o->newline(1) << "goto probe_epilogue;";
114 o->indent(-1);
9a604fac 115
4a0ae64c 116 o->newline() << "c = contexts[smp_processor_id()];";
b3c3ca7c 117 o->newline() << "if (atomic_inc_return (& c->busy) != 1) {";
9c736061
FCE
118 o->newline(1) << "#if !INTERRUPTIBLE";
119 o->newline() << "atomic_inc (& skipped_count);";
120 o->newline() << "#endif";
b3c3ca7c
FCE
121 o->newline() << "#ifdef STP_TIMING";
122 o->newline() << "atomic_inc (& skipped_count_reentrant);";
c12d974f
FCE
123 o->newline() << "#ifdef DEBUG_REENTRANCY";
124 o->newline() << "_stp_warn (\"Skipped %s due to %s residency on cpu %u\\n\", "
faea5e16 125 << probe << ".pp, c->probe_point ?: \"?\", smp_processor_id());";
c12d974f
FCE
126 // NB: There is a conceivable race condition here with reading
127 // c->probe_point, knowing that this other probe is sort of running.
128 // However, in reality, it's interrupted. Plus even if it were able
129 // to somehow start again, and stop before we read c->probe_point,
130 // at least we have that ?: "?" bit in there to avoid a NULL deref.
131 o->newline() << "#endif";
b3c3ca7c 132 o->newline() << "#endif";
9a604fac 133 o->newline() << "atomic_dec (& c->busy);";
b20febf3 134 o->newline() << "goto probe_epilogue;";
9a604fac
FCE
135 o->newline(-1) << "}";
136 o->newline();
1e00cfb1 137 o->newline() << "c->last_stmt = 0;";
9a604fac 138 o->newline() << "c->last_error = 0;";
a7ed0d3e 139 o->newline() << "c->nesting = -1;"; // NB: PR10516 packs locals[] tighter
22f8b401 140 o->newline() << "c->regs = 0;";
b916df9c 141 o->newline() << "c->unwaddr = 0;";
faea5e16 142 o->newline() << "c->probe_point = " << probe << ".pp;";
d48df0cf
JS
143 o->newline() << "#ifdef STP_NEED_PROBE_NAME";
144 o->newline() << "c->probe_name = " << probe << ".pn;";
2d767770 145 o->newline() << "#else";
d48df0cf 146 o->newline() << "c->probe_name = 0;";
2d767770 147 o->newline() << "#endif";
b916df9c 148 // reset unwound address cache
fcff848e 149 o->newline() << "c->pi = 0;";
af234c40 150 o->newline() << "c->pi_longs = 0;";
9addf322 151 o->newline() << "c->regparm = 0;";
bc54e71c
MH
152 o->newline() << "c->marker_name = NULL;";
153 o->newline() << "c->marker_format = NULL;";
e0a17418
JS
154
155 o->newline() << "#if INTERRUPTIBLE";
156 o->newline() << "c->actionremaining = MAXACTION_INTERRUPTIBLE;";
157 o->newline() << "#else";
158 o->newline() << "c->actionremaining = MAXACTION;";
159 o->newline() << "#endif";
dbb68664
FCE
160 o->newline() << "#ifdef STP_TIMING";
161 o->newline() << "c->statp = 0;";
162 o->newline() << "#endif";
5e562a69 163 o->newline() << "c->ri = 0;";
9915575b
FCE
164 // NB: The following would actually be incorrect.
165 // That's because cycles_sum/cycles_base values are supposed to survive
166 // between consecutive probes. Periodically (STP_OVERLOAD_INTERVAL
167 // cycles), the values will be reset.
168 /*
f0e6dc63
FCE
169 o->newline() << "#ifdef STP_OVERLOAD";
170 o->newline() << "c->cycles_sum = 0;";
171 o->newline() << "c->cycles_base = 0;";
41c262f3 172 o->newline() << "#endif";
9915575b 173 */
b20febf3 174}
9a604fac 175
a44a0785 176
b20febf3 177void
a58d79d0 178common_probe_entryfn_epilogue (translator_output* o,
912e8c59 179 bool overload_processing)
b20febf3 180{
a58d79d0
DS
181 if (overload_processing)
182 o->newline() << "#if defined(STP_TIMING) || defined(STP_OVERLOAD)";
183 else
184 o->newline() << "#ifdef STP_TIMING";
dbb68664 185 o->newline() << "{";
a58d79d0
DS
186 o->newline(1) << "cycles_t cycles_atend = get_cycles ();";
187 // NB: we truncate cycles counts to 32 bits. Perhaps it should be
188 // fewer, if the hardware counter rolls over really quickly. We
189 // handle 32-bit wraparound here.
190 o->newline() << "int32_t cycles_elapsed = ((int32_t)cycles_atend > (int32_t)cycles_atstart)";
191 o->newline(1) << "? ((int32_t)cycles_atend - (int32_t)cycles_atstart)";
192 o->newline() << ": (~(int32_t)0) - (int32_t)cycles_atstart + (int32_t)cycles_atend + 1;";
193 o->indent(-1);
dbb68664 194
a58d79d0 195 o->newline() << "#ifdef STP_TIMING";
dbb68664 196 o->newline() << "if (likely (c->statp)) _stp_stat_add(*c->statp, cycles_elapsed);";
a58d79d0
DS
197 o->newline() << "#endif";
198
199 if (overload_processing)
200 {
201 o->newline() << "#ifdef STP_OVERLOAD";
202 o->newline() << "{";
203 // If the cycle count has wrapped (cycles_atend > cycles_base),
204 // let's go ahead and pretend the interval has been reached.
205 // This should reset cycles_base and cycles_sum.
206 o->newline(1) << "cycles_t interval = (cycles_atend > c->cycles_base)";
207 o->newline(1) << "? (cycles_atend - c->cycles_base)";
208 o->newline() << ": (STP_OVERLOAD_INTERVAL + 1);";
209 o->newline(-1) << "c->cycles_sum += cycles_elapsed;";
210
211 // If we've spent more than STP_OVERLOAD_THRESHOLD cycles in a
212 // probe during the last STP_OVERLOAD_INTERVAL cycles, the probe
213 // has overloaded the system and we need to quit.
214 o->newline() << "if (interval > STP_OVERLOAD_INTERVAL) {";
215 o->newline(1) << "if (c->cycles_sum > STP_OVERLOAD_THRESHOLD) {";
216 o->newline(1) << "_stp_error (\"probe overhead exceeded threshold\");";
217 o->newline() << "atomic_set (&session_state, STAP_SESSION_ERROR);";
551e9f14 218 o->newline() << "atomic_inc (&error_count);";
a58d79d0 219 o->newline(-1) << "}";
e57b735a 220
a58d79d0
DS
221 o->newline() << "c->cycles_base = cycles_atend;";
222 o->newline() << "c->cycles_sum = 0;";
223 o->newline(-1) << "}";
224 o->newline(-1) << "}";
225 o->newline() << "#endif";
226 }
e57b735a 227
440f755a
JS
228 o->newline(-1) << "}";
229 o->newline() << "#endif";
e57b735a 230
440f755a 231 o->newline() << "c->probe_point = 0;"; // vacated
d48df0cf 232 o->newline() << "c->probe_name = 0;";
440f755a
JS
233 o->newline() << "if (unlikely (c->last_error && c->last_error[0])) {";
234 o->newline(1) << "if (c->last_stmt != NULL)";
235 o->newline(1) << "_stp_softerror (\"%s near %s\", c->last_error, c->last_stmt);";
236 o->newline(-1) << "else";
237 o->newline(1) << "_stp_softerror (\"%s\", c->last_error);";
238 o->indent(-1);
239 o->newline() << "atomic_inc (& error_count);";
240 o->newline() << "if (atomic_read (& error_count) > MAXERRORS) {";
241 o->newline(1) << "atomic_set (& session_state, STAP_SESSION_ERROR);";
242 o->newline() << "_stp_exit ();";
243 o->newline(-1) << "}";
244 o->newline(-1) << "}";
245 o->newline() << "atomic_dec (&c->busy);";
e57b735a 246
440f755a
JS
247 o->newline(-1) << "probe_epilogue:"; // context is free
248 o->indent(1);
e57b735a 249
440f755a
JS
250 // Check for excessive skip counts.
251 o->newline() << "if (unlikely (atomic_read (& skipped_count) > MAXSKIPPED)) {";
079915a5 252 o->newline(1) << "if (unlikely (pseudo_atomic_cmpxchg(& session_state, STAP_SESSION_RUNNING, STAP_SESSION_ERROR) == STAP_SESSION_RUNNING))";
f65166cc 253 o->newline() << "_stp_error (\"Skipped too many probes, check MAXSKIPPED or try again with stap -t for more details.\");";
440f755a 254 o->newline(-1) << "}";
e57b735a 255
440f755a
JS
256 o->newline() << "#if INTERRUPTIBLE";
257 o->newline() << "preempt_enable_no_resched ();";
258 o->newline() << "#else";
259 o->newline() << "local_irq_restore (flags);";
260 o->newline() << "#endif";
261}
e57b735a 262
e57b735a 263
440f755a 264// ------------------------------------------------------------------------
e57b735a 265
440f755a
JS
266// ------------------------------------------------------------------------
267// Dwarf derived probes. "We apologize for the inconvience."
268// ------------------------------------------------------------------------
e57b735a 269
4627ed58
JS
270static const string TOK_KERNEL("kernel");
271static const string TOK_MODULE("module");
272static const string TOK_FUNCTION("function");
273static const string TOK_INLINE("inline");
274static const string TOK_CALL("call");
275static const string TOK_RETURN("return");
276static const string TOK_MAXACTIVE("maxactive");
277static const string TOK_STATEMENT("statement");
278static const string TOK_ABSOLUTE("absolute");
279static const string TOK_PROCESS("process");
a794dbeb 280static const string TOK_PROVIDER("provider");
4627ed58
JS
281static const string TOK_MARK("mark");
282static const string TOK_TRACE("trace");
283static const string TOK_LABEL("label");
63b4fd14 284static const string TOK_LIBRARY("library");
e57b735a 285
1adf8ef1 286static int query_cu (Dwarf_Die * cudie, void * arg);
6b517475 287static void query_addr(Dwarf_Addr addr, dwarf_query *q);
e57b735a 288
440f755a
JS
289// Can we handle this query with just symbol-table info?
290enum dbinfo_reqt
291{
292 dbr_unknown,
293 dbr_none, // kernel.statement(NUM).absolute
294 dbr_need_symtab, // can get by with symbol table if there's no dwarf
295 dbr_need_dwarf
296};
e57b735a 297
20e4a32c 298
440f755a
JS
299struct base_query; // forward decls
300struct dwarf_query;
301struct dwflpp;
302struct symbol_table;
20e4a32c 303
a781f401 304
440f755a
JS
305struct
306symbol_table
307{
308 module_info *mod_info; // associated module
309 map<string, func_info*> map_by_name;
1c6b77e5
JS
310 multimap<Dwarf_Addr, func_info*> map_by_addr;
311 typedef multimap<Dwarf_Addr, func_info*>::iterator iterator_t;
440f755a
JS
312 typedef pair<iterator_t, iterator_t> range_t;
313#ifdef __powerpc__
314 GElf_Word opd_section;
315#endif
2867a2a1
JS
316 void add_symbol(const char *name, bool weak, bool descriptor,
317 Dwarf_Addr addr, Dwarf_Addr *high_addr);
440f755a 318 enum info_status read_symbols(FILE *f, const string& path);
83ca3872
MW
319 enum info_status read_from_elf_file(const string& path,
320 const systemtap_session &sess);
321 enum info_status read_from_text_file(const string& path,
322 const systemtap_session &sess);
440f755a
JS
323 enum info_status get_from_elf();
324 void prepare_section_rejection(Dwfl_Module *mod);
325 bool reject_section(GElf_Word section);
440f755a
JS
326 void purge_syscall_stubs();
327 func_info *lookup_symbol(const string& name);
328 Dwarf_Addr lookup_symbol_address(const string& name);
329 func_info *get_func_containing_address(Dwarf_Addr addr);
7a053d3b 330
440f755a
JS
331 symbol_table(module_info *mi) : mod_info(mi) {}
332 ~symbol_table();
333};
77de5e9e 334
440f755a
JS
335static bool null_die(Dwarf_Die *die)
336{
337 static Dwarf_Die null = { 0 };
338 return (!die || !memcmp(die, &null, sizeof(null)));
339}
c4ce66a1
JS
340
341
7a053d3b 342enum
bd2b1e68 343function_spec_type
7a053d3b 344 {
bd2b1e68
GH
345 function_alone,
346 function_and_file,
7a053d3b 347 function_file_and_line
bd2b1e68
GH
348 };
349
ec4373ff 350
bd2b1e68 351struct dwarf_builder;
f10534c6 352struct dwarf_var_expanding_visitor;
77de5e9e 353
2930abc7 354
b20febf3
FCE
355// XXX: This class is a candidate for subclassing to separate
356// the relocation vs non-relocation variants. Likewise for
357// kprobe vs kretprobe variants.
358
359struct dwarf_derived_probe: public derived_probe
b55bc428 360{
b20febf3
FCE
361 dwarf_derived_probe (const string& function,
362 const string& filename,
363 int line,
364 const string& module,
365 const string& section,
366 Dwarf_Addr dwfl_addr,
2930abc7 367 Dwarf_Addr addr,
b20febf3
FCE
368 dwarf_query & q,
369 Dwarf_Die* scope_die);
20e4a32c 370
b20febf3
FCE
371 string module;
372 string section;
373 Dwarf_Addr addr;
63b4fd14 374 string path;
27dc09b1 375 bool has_process;
2930abc7 376 bool has_return;
c9bad430 377 bool has_maxactive;
63b4fd14 378 bool has_library;
c9bad430 379 long maxactive_val;
b642c901
SC
380 string user_path;
381 string user_lib;
b95e2b79 382 bool access_vars;
2930abc7 383
af234c40
JS
384 unsigned saved_longs, saved_strings;
385 dwarf_derived_probe* entry_handler;
386
b8da0ad1 387 void printsig (std::ostream &o) const;
6b66b9f7 388 virtual void join_group (systemtap_session& s);
9020300d 389 void emit_probe_local_init(translator_output * o);
d0bfd2ac 390 void getargs(std::list<std::string> &arg_set) const;
0a98fd42 391
27dc09b1
DB
392 void emit_unprivileged_assertion (translator_output*);
393 void print_dupe_stamp(ostream& o);
394
bd2b1e68 395 // Pattern registration helpers.
7a053d3b 396 static void register_statement_variants(match_node * root,
27dc09b1
DB
397 dwarf_builder * dw,
398 bool bind_unprivileged_p = false);
fd6602a0 399 static void register_function_variants(match_node * root,
27dc09b1
DB
400 dwarf_builder * dw,
401 bool bind_unprivileged_p = false);
7a053d3b 402 static void register_function_and_statement_variants(match_node * root,
27dc09b1
DB
403 dwarf_builder * dw,
404 bool bind_unprivileged_p = false);
c4ce66a1 405 static void register_patterns(systemtap_session& s);
6b66b9f7
JS
406
407protected:
408 dwarf_derived_probe(probe *base,
409 probe_point *location,
410 Dwarf_Addr addr,
411 bool has_return):
412 derived_probe(base, location), addr(addr), has_return(has_return),
af234c40
JS
413 has_maxactive(0), maxactive_val(0), access_vars(false),
414 saved_longs(0), saved_strings(0), entry_handler(0)
6b66b9f7
JS
415 {}
416
417private:
d0bfd2ac 418 list<string> args;
f10534c6 419 void saveargs(dwarf_query& q, Dwarf_Die* scope_die, dwarf_var_expanding_visitor& v);
20c6c071
GH
420};
421
dc38c0ae 422
6b66b9f7 423struct uprobe_derived_probe: public dwarf_derived_probe
6d0f3f0c 424{
6d0f3f0c 425 int pid; // 0 => unrestricted
0973d815 426
6d0f3f0c
FCE
427 uprobe_derived_probe (const string& function,
428 const string& filename,
429 int line,
430 const string& module,
6d0f3f0c
FCE
431 const string& section,
432 Dwarf_Addr dwfl_addr,
433 Dwarf_Addr addr,
434 dwarf_query & q,
6b66b9f7
JS
435 Dwarf_Die* scope_die):
436 dwarf_derived_probe(function, filename, line, module, section,
437 dwfl_addr, addr, q, scope_die), pid(0)
438 {}
6d0f3f0c 439
0973d815
FCE
440 // alternate constructor for process(PID).statement(ADDR).absolute
441 uprobe_derived_probe (probe *base,
442 probe_point *location,
443 int pid,
444 Dwarf_Addr addr,
6b66b9f7
JS
445 bool has_return):
446 dwarf_derived_probe(base, location, addr, has_return), pid(pid)
447 {}
9ace370f 448
6d0f3f0c 449 void join_group (systemtap_session& s);
2865d17a
DB
450
451 void emit_unprivileged_assertion (translator_output*);
8f6d8c2b 452 void print_dupe_stamp(ostream& o) { print_dupe_stamp_unprivileged_process_owner (o); }
6d0f3f0c
FCE
453};
454
dc38c0ae
DS
455struct dwarf_derived_probe_group: public derived_probe_group
456{
457private:
b20febf3
FCE
458 multimap<string,dwarf_derived_probe*> probes_by_module;
459 typedef multimap<string,dwarf_derived_probe*>::iterator p_b_m_iterator;
dc38c0ae
DS
460
461public:
b20febf3
FCE
462 void enroll (dwarf_derived_probe* probe);
463 void emit_module_decls (systemtap_session& s);
464 void emit_module_init (systemtap_session& s);
465 void emit_module_exit (systemtap_session& s);
dc38c0ae
DS
466};
467
468
20c6c071 469// Helper struct to thread through the dwfl callbacks.
2c384610 470struct base_query
20c6c071 471{
c4ce66a1
JS
472 base_query(dwflpp & dw, literal_map_t const & params);
473 base_query(dwflpp & dw, const string & module_val);
2c384610 474 virtual ~base_query() {}
bd2b1e68 475
5227f1ea 476 systemtap_session & sess;
2c384610 477 dwflpp & dw;
5227f1ea 478
bd2b1e68 479 // Parameter extractors.
86bf665e 480 static bool has_null_param(literal_map_t const & params,
888af770 481 string const & k);
86bf665e 482 static bool get_string_param(literal_map_t const & params,
bd2b1e68 483 string const & k, string & v);
86bf665e 484 static bool get_number_param(literal_map_t const & params,
bd2b1e68 485 string const & k, long & v);
86bf665e 486 static bool get_number_param(literal_map_t const & params,
c239d28c 487 string const & k, Dwarf_Addr & v);
b55bc428 488
2c384610
DS
489 // Extracted parameters.
490 bool has_kernel;
91af0778
FCE
491 bool has_module;
492 bool has_process;
63b4fd14 493 bool has_library;
2c384610 494 string module_val; // has_kernel => module_val = "kernel"
63b4fd14 495 string path; // executable path if module is a .so
2c384610
DS
496
497 virtual void handle_query_module() = 0;
498};
499
500
c4ce66a1
JS
501base_query::base_query(dwflpp & dw, literal_map_t const & params):
502 sess(dw.sess), dw(dw)
2c384610 503{
91af0778 504 has_kernel = has_null_param (params, TOK_KERNEL);
2c384610
DS
505 if (has_kernel)
506 module_val = "kernel";
91af0778
FCE
507
508 has_module = get_string_param (params, TOK_MODULE, module_val);
509 if (has_module)
510 has_process = false;
4baf0e53 511 else
d0a7f5a9 512 {
63b4fd14 513 string library_name;
d0a7f5a9 514 has_process = get_string_param(params, TOK_PROCESS, module_val);
63b4fd14
SC
515 has_library = get_string_param (params, TOK_LIBRARY, library_name);
516 if (has_library)
517 {
518 path = find_executable (module_val);
519 module_val = find_executable (library_name, "LD_LIBRARY_PATH");
520 }
521 else if (has_process)
d0a7f5a9
FCE
522 module_val = find_executable (module_val);
523 }
91af0778
FCE
524
525 assert (has_kernel || has_process || has_module);
2c384610
DS
526}
527
c4ce66a1
JS
528base_query::base_query(dwflpp & dw, const string & module_val)
529 : sess(dw.sess), dw(dw), module_val(module_val)
530{
531 // NB: This uses '/' to distinguish between kernel modules and userspace,
532 // which means that userspace modules won't get any PATH searching.
533 if (module_val.find('/') == string::npos)
534 {
535 has_kernel = (module_val == TOK_KERNEL);
536 has_module = !has_kernel;
537 has_process = false;
538 }
539 else
540 {
541 has_kernel = has_module = false;
542 has_process = true;
543 }
544}
545
2c384610 546bool
86bf665e 547base_query::has_null_param(literal_map_t const & params,
2c384610
DS
548 string const & k)
549{
888af770 550 return derived_probe_builder::has_null_param(params, k);
2c384610
DS
551}
552
553
554bool
86bf665e 555base_query::get_string_param(literal_map_t const & params,
2c384610
DS
556 string const & k, string & v)
557{
558 return derived_probe_builder::get_param (params, k, v);
559}
560
561
562bool
86bf665e 563base_query::get_number_param(literal_map_t const & params,
2c384610
DS
564 string const & k, long & v)
565{
566 int64_t value;
567 bool present = derived_probe_builder::get_param (params, k, value);
568 v = (long) value;
569 return present;
570}
571
572
573bool
86bf665e 574base_query::get_number_param(literal_map_t const & params,
2c384610
DS
575 string const & k, Dwarf_Addr & v)
576{
577 int64_t value;
578 bool present = derived_probe_builder::get_param (params, k, value);
579 v = (Dwarf_Addr) value;
580 return present;
581}
582
2c384610
DS
583struct dwarf_query : public base_query
584{
e1278bd4 585 dwarf_query(probe * base_probe,
2c384610
DS
586 probe_point * base_loc,
587 dwflpp & dw,
86bf665e 588 literal_map_t const & params,
b642c901
SC
589 vector<derived_probe *> & results,
590 const string user_path,
591 const string user_lib);
2c384610 592
c4ce66a1 593 vector<derived_probe *> & results;
8f14e444 594 set<string> inlined_non_returnable; // function names
c4ce66a1
JS
595 probe * base_probe;
596 probe_point * base_loc;
b642c901
SC
597 string user_path;
598 string user_lib;
c4ce66a1 599
2c384610 600 virtual void handle_query_module();
5f0a03a6
JK
601 void query_module_dwarf();
602 void query_module_symtab();
2c384610 603
2930abc7
FCE
604 void add_probe_point(string const & funcname,
605 char const * filename,
606 int line,
607 Dwarf_Die *scope_die,
608 Dwarf_Addr addr);
36f9dd1d 609
857bdfd1
JS
610 // Track addresses we've already seen in a given module
611 set<Dwarf_Addr> alias_dupes;
612
7fdd3e2c
JS
613 // Track inlines we've already seen as well
614 // NB: this can't be compared just by entrypc, as inlines can overlap
615 set<inline_instance_info> inline_dupes;
616
2930abc7 617 // Extracted parameters.
7a053d3b 618 string function_val;
20c6c071
GH
619
620 bool has_function_str;
621 bool has_statement_str;
622 bool has_function_num;
623 bool has_statement_num;
7a053d3b
RM
624 string statement_str_val;
625 string function_str_val;
c239d28c
GH
626 Dwarf_Addr statement_num_val;
627 Dwarf_Addr function_num_val;
20c6c071 628
b8da0ad1
FCE
629 bool has_call;
630 bool has_inline;
20c6c071
GH
631 bool has_return;
632
c9bad430
DS
633 bool has_maxactive;
634 long maxactive_val;
635
20c6c071
GH
636 bool has_label;
637 string label_val;
638
639 bool has_relative;
640 long relative_val;
641
37ebca01
FCE
642 bool has_absolute;
643
467bea43
SC
644 bool has_mark;
645
5f0a03a6
JK
646 enum dbinfo_reqt dbinfo_reqt;
647 enum dbinfo_reqt assess_dbinfo_reqt();
648
7d6d0afc 649 void parse_function_spec(const string & spec);
20c6c071 650 function_spec_type spec_type;
7d6d0afc 651 vector<string> scopes;
20c6c071
GH
652 string function;
653 string file;
0c8b7d37 654 line_t line_type;
879eb9e9 655 int line[2];
5f0a03a6 656 bool query_done; // Found exact match
20c6c071 657
bd25380d 658 set<string> filtered_srcfiles;
7e1279ea
FCE
659
660 // Map official entrypc -> func_info object
86bf665e
TM
661 inline_instance_map_t filtered_inlines;
662 func_info_map_t filtered_functions;
7e1279ea
FCE
663 bool choose_next_line;
664 Dwarf_Addr entrypc_for_next_line;
4df79aaf
JS
665
666 void query_module_functions ();
b55bc428
FCE
667};
668
98afd80e
FCE
669
670struct dwarf_builder: public derived_probe_builder
b55bc428 671{
665e1256 672 map <string,dwflpp*> kern_dw; /* NB: key string could be a wildcard */
7a24d422 673 map <string,dwflpp*> user_dw;
b642c901
SC
674 string user_path;
675 string user_lib;
ae2552da 676 dwarf_builder() {}
aa30ccd3 677
ae2552da 678 dwflpp *get_kern_dw(systemtap_session& sess, const string& module)
707bf35e 679 {
ea14cf67
FCE
680 if (kern_dw[module] == 0)
681 kern_dw[module] = new dwflpp(sess, module, true); // might throw
ae2552da 682 return kern_dw[module];
707bf35e
JS
683 }
684
685 dwflpp *get_user_dw(systemtap_session& sess, const string& module)
686 {
ea14cf67
FCE
687 if (user_dw[module] == 0)
688 user_dw[module] = new dwflpp(sess, module, false); // might throw
707bf35e
JS
689 return user_dw[module];
690 }
7a24d422
FCE
691
692 /* NB: not virtual, so can be called from dtor too: */
06aca46a 693 void dwarf_build_no_more (bool verbose)
aa30ccd3 694 {
ae2552da
FCE
695 for (map<string,dwflpp*>::iterator udi = kern_dw.begin();
696 udi != kern_dw.end();
697 udi ++)
aa30ccd3 698 {
7a24d422 699 if (verbose)
ae2552da
FCE
700 clog << "dwarf_builder releasing kernel dwflpp " << udi->first << endl;
701 delete udi->second;
aa30ccd3 702 }
ae2552da 703 kern_dw.erase (kern_dw.begin(), kern_dw.end());
7a24d422
FCE
704
705 for (map<string,dwflpp*>::iterator udi = user_dw.begin();
706 udi != user_dw.end();
707 udi ++)
708 {
709 if (verbose)
710 clog << "dwarf_builder releasing user dwflpp " << udi->first << endl;
711 delete udi->second;
712 }
713 user_dw.erase (user_dw.begin(), user_dw.end());
714 }
715
716 void build_no_more (systemtap_session &s)
717 {
718 dwarf_build_no_more (s.verbose > 3);
aa30ccd3
FCE
719 }
720
e38d6504
RM
721 ~dwarf_builder()
722 {
7a24d422 723 dwarf_build_no_more (false);
c8959a29 724 }
aa30ccd3 725
5227f1ea 726 virtual void build(systemtap_session & sess,
7a053d3b 727 probe * base,
20c6c071 728 probe_point * location,
86bf665e 729 literal_map_t const & parameters,
20c6c071 730 vector<derived_probe *> & finished_results);
b55bc428
FCE
731};
732
5111fc3e 733
e1278bd4 734dwarf_query::dwarf_query(probe * base_probe,
20c6c071
GH
735 probe_point * base_loc,
736 dwflpp & dw,
86bf665e 737 literal_map_t const & params,
b642c901
SC
738 vector<derived_probe *> & results,
739 const string user_path,
740 const string user_lib)
c4ce66a1 741 : base_query(dw, params), results(results),
b642c901
SC
742 base_probe(base_probe), base_loc(base_loc),
743 user_path(user_path), user_lib(user_lib)
bd2b1e68
GH
744{
745 // Reduce the query to more reasonable semantic values (booleans,
746 // extracted strings, numbers, etc).
bd2b1e68
GH
747 has_function_str = get_string_param(params, TOK_FUNCTION, function_str_val);
748 has_function_num = get_number_param(params, TOK_FUNCTION, function_num_val);
749
750 has_statement_str = get_string_param(params, TOK_STATEMENT, statement_str_val);
751 has_statement_num = get_number_param(params, TOK_STATEMENT, statement_num_val);
752
0f336e95
SC
753 has_label = get_string_param(params, TOK_LABEL, label_val);
754
b8da0ad1
FCE
755 has_call = has_null_param(params, TOK_CALL);
756 has_inline = has_null_param(params, TOK_INLINE);
bd2b1e68 757 has_return = has_null_param(params, TOK_RETURN);
c9bad430 758 has_maxactive = get_number_param(params, TOK_MAXACTIVE, maxactive_val);
37ebca01 759 has_absolute = has_null_param(params, TOK_ABSOLUTE);
467bea43 760 has_mark = false;
37ebca01 761
bd2b1e68 762 if (has_function_str)
7d6d0afc 763 parse_function_spec(function_str_val);
bd2b1e68 764 else if (has_statement_str)
7d6d0afc 765 parse_function_spec(statement_str_val);
0daad364 766
5f0a03a6
JK
767 dbinfo_reqt = assess_dbinfo_reqt();
768 query_done = false;
0daad364
JS
769}
770
771
440f755a
JS
772func_info_map_t *
773get_filtered_functions(dwarf_query *q)
774{
775 return &q->filtered_functions;
776}
777
778
779inline_instance_map_t *
780get_filtered_inlines(dwarf_query *q)
781{
782 return &q->filtered_inlines;
783}
784
785
2c384610 786void
5f0a03a6 787dwarf_query::query_module_dwarf()
2c384610
DS
788{
789 if (has_function_num || has_statement_num)
790 {
791 // If we have module("foo").function(0xbeef) or
792 // module("foo").statement(0xbeef), the address is relative
793 // to the start of the module, so we seek the function
794 // number plus the module's bias.
6b517475
JS
795 Dwarf_Addr addr = has_function_num ?
796 function_num_val : statement_num_val;
08d1d520
MW
797
798 // These are raw addresses, we need to know what the elf_bias
799 // is to feed it to libdwfl based functions.
800 Dwarf_Addr elf_bias;
801 Elf *elf = dwfl_module_getelf (dw.module, &elf_bias);
802 assert(elf);
803 addr += elf_bias;
6b517475 804 query_addr(addr, this);
2c384610
DS
805 }
806 else
807 {
808 // Otherwise if we have a function("foo") or statement("foo")
809 // specifier, we have to scan over all the CUs looking for
810 // the function(s) in question
811 assert(has_function_str || has_statement_str);
4df79aaf
JS
812
813 // For simple cases, no wildcard and no source:line, we can do a very
814 // quick function lookup in a module-wide cache.
815 if (spec_type == function_alone && !dw.name_has_wildcard(function))
816 query_module_functions();
817 else
818 dw.iterate_over_cus(&query_cu, this);
2c384610
DS
819 }
820}
821
5f0a03a6
JK
822static void query_func_info (Dwarf_Addr entrypc, func_info & fi,
823 dwarf_query * q);
824
825void
826dwarf_query::query_module_symtab()
827{
828 // Get the symbol table if it's necessary, sufficient, and not already got.
829 if (dbinfo_reqt == dbr_need_dwarf)
830 return;
831
832 module_info *mi = dw.mod_info;
833 if (dbinfo_reqt == dbr_need_symtab)
834 {
835 if (mi->symtab_status == info_unknown)
836 mi->get_symtab(this);
837 if (mi->symtab_status == info_absent)
838 return;
839 }
840
841 func_info *fi = NULL;
842 symbol_table *sym_table = mi->sym_table;
843
844 if (has_function_str)
845 {
846 // Per dwarf_query::assess_dbinfo_reqt()...
847 assert(spec_type == function_alone);
848 if (dw.name_has_wildcard(function_str_val))
849 {
850 // Until we augment the blacklist sufficently...
851 if (function_str_val.find_first_not_of("*?") == string::npos)
852 {
853 // e.g., kernel.function("*")
854 cerr << "Error: Pattern '"
855 << function_str_val
856 << "' matches every instruction address in the symbol table,"
857 << endl
858 << "some of which aren't even functions."
859 << " Please be more precise."
860 << endl;
861 return;
862 }
2e67a43b 863 symbol_table::iterator_t iter;
1c6b77e5
JS
864 for (iter = sym_table->map_by_addr.begin();
865 iter != sym_table->map_by_addr.end();
2e67a43b 866 ++iter)
5f0a03a6 867 {
1c6b77e5 868 fi = iter->second;
5f0a03a6
JK
869 if (!null_die(&fi->die))
870 continue; // already handled in query_module_dwarf()
871 if (dw.function_name_matches_pattern(fi->name, function_str_val))
872 query_func_info(fi->addr, *fi, this);
873 }
874 }
875 else
876 {
877 fi = sym_table->lookup_symbol(function_str_val);
2867a2a1 878 if (fi && !fi->descriptor && null_die(&fi->die))
5f0a03a6
JK
879 query_func_info(fi->addr, *fi, this);
880 }
881 }
882 else
883 {
884 assert(has_function_num || has_statement_num);
885 // Find the "function" in which the indicated address resides.
886 Dwarf_Addr addr =
887 (has_function_num ? function_num_val : statement_num_val);
888 fi = sym_table->get_func_containing_address(addr);
889 if (!fi)
890 {
83ca3872
MW
891 if (! sess.suppress_warnings)
892 cerr << "Warning: address "
893 << hex << addr << dec
894 << " out of range for module "
895 << dw.module_name;
5f0a03a6
JK
896 return;
897 }
898 if (!null_die(&fi->die))
899 {
900 // addr looks like it's in the compilation unit containing
901 // the indicated function, but query_module_dwarf() didn't
902 // match addr to any compilation unit, so addr must be
903 // above that cu's address range.
83ca3872
MW
904 if (! sess.suppress_warnings)
905 cerr << "Warning: address "
906 << hex << addr << dec
907 << " maps to no known compilation unit in module "
908 << dw.module_name;
5f0a03a6
JK
909 return;
910 }
911 query_func_info(fi->addr, *fi, this);
912 }
913}
914
915void
916dwarf_query::handle_query_module()
917{
1c6b77e5
JS
918 bool report = dbinfo_reqt == dbr_need_dwarf || !sess.consult_symtab;
919 dw.get_module_dwarf(false, report);
920
921 // prebuild the symbol table to resolve aliases
922 dw.mod_info->get_symtab(this);
923
857bdfd1
JS
924 // reset the dupe-checking for each new module
925 alias_dupes.clear();
7fdd3e2c 926 inline_dupes.clear();
857bdfd1 927
5f0a03a6
JK
928 if (dw.mod_info->dwarf_status == info_present)
929 query_module_dwarf();
1c6b77e5 930
5f0a03a6
JK
931 // Consult the symbol table if we haven't found all we're looking for.
932 // asm functions can show up in the symbol table but not in dwarf.
933 if (sess.consult_symtab && !query_done)
934 query_module_symtab();
935}
936
2c384610 937
7d6d0afc
JS
938void
939dwarf_query::parse_function_spec(const string & spec)
bd2b1e68 940{
1d12a9b2
JS
941 line_type = ABSOLUTE;
942 line[0] = line[1] = 0;
943
7d6d0afc 944 size_t src_pos, line_pos, dash_pos, scope_pos, next_scope_pos;
bd2b1e68 945
7d6d0afc
JS
946 // look for named scopes
947 scope_pos = 0;
948 next_scope_pos = spec.find("::");
949 while (next_scope_pos != string::npos)
bd2b1e68 950 {
7d6d0afc
JS
951 scopes.push_back(spec.substr(scope_pos, next_scope_pos - scope_pos));
952 scope_pos = next_scope_pos + 2;
953 next_scope_pos = spec.find("::", scope_pos);
bd2b1e68
GH
954 }
955
7d6d0afc
JS
956 // look for a source separator
957 src_pos = spec.find('@', scope_pos);
958 if (src_pos == string::npos)
bd2b1e68 959 {
7d6d0afc
JS
960 function = spec.substr(scope_pos);
961 spec_type = function_alone;
bd2b1e68 962 }
7d6d0afc 963 else
879eb9e9 964 {
7d6d0afc 965 function = spec.substr(scope_pos, src_pos - scope_pos);
7a053d3b 966
7d6d0afc
JS
967 // look for a line-number separator
968 line_pos = spec.find_first_of(":+", src_pos);
969 if (line_pos == string::npos)
970 {
971 file = spec.substr(src_pos + 1);
972 spec_type = function_and_file;
973 }
974 else
975 {
976 file = spec.substr(src_pos + 1, line_pos - src_pos - 1);
977
978 // classify the line spec
979 spec_type = function_file_and_line;
980 if (spec[line_pos] == '+')
981 line_type = RELATIVE;
982 else if (spec[line_pos + 1] == '*' &&
983 spec.length() == line_pos + 2)
984 line_type = WILDCARD;
985 else
986 line_type = ABSOLUTE;
987
988 if (line_type != WILDCARD)
989 try
990 {
991 // try to parse either N or N-M
992 dash_pos = spec.find('-', line_pos + 1);
993 if (dash_pos == string::npos)
994 line[0] = line[1] = lex_cast<int>(spec.substr(line_pos + 1));
995 else
996 {
997 line_type = RANGE;
998 line[0] = lex_cast<int>(spec.substr(line_pos + 1,
999 dash_pos - line_pos - 1));
1000 line[1] = lex_cast<int>(spec.substr(dash_pos + 1));
1001 }
1002 }
1003 catch (runtime_error & exn)
1004 {
1005 goto bad;
1006 }
1007 }
bd2b1e68
GH
1008 }
1009
7d6d0afc
JS
1010 if (function.empty() ||
1011 (spec_type != function_alone && file.empty()))
bd2b1e68
GH
1012 goto bad;
1013
7d6d0afc 1014 if (sess.verbose > 2)
bd2b1e68 1015 {
7d6d0afc 1016 clog << "parsed '" << spec << "'";
41c262f3 1017
7d6d0afc
JS
1018 if (!scopes.empty())
1019 clog << ", scope '" << scopes[0] << "'";
1020 for (unsigned i = 1; i < scopes.size(); ++i)
1021 clog << "::'" << scopes[i] << "'";
41c262f3 1022
7d6d0afc
JS
1023 clog << ", func '" << function << "'";
1024
1025 if (spec_type != function_alone)
1026 clog << ", file '" << file << "'";
1027
1028 if (spec_type == function_file_and_line)
1029 {
1030 clog << ", line ";
1031 switch (line_type)
1032 {
1033 case ABSOLUTE:
1034 clog << line[0];
1035 break;
1036
1037 case RELATIVE:
1038 clog << "+" << line[0];
1039 break;
1040
1041 case RANGE:
1042 clog << line[0] << " - " << line[1];
1043 break;
1044
1045 case WILDCARD:
1046 clog << "*";
1047 break;
1048 }
1049 }
1050
1051 clog << endl;
bd2b1e68
GH
1052 }
1053
7d6d0afc
JS
1054 return;
1055
1056bad:
1057 throw semantic_error("malformed specification '" + spec + "'",
1058 base_probe->tok);
bd2b1e68
GH
1059}
1060
1061
36f9dd1d 1062void
b20febf3
FCE
1063dwarf_query::add_probe_point(const string& funcname,
1064 const char* filename,
36f9dd1d 1065 int line,
b20febf3 1066 Dwarf_Die* scope_die,
36f9dd1d
FCE
1067 Dwarf_Addr addr)
1068{
b20febf3 1069 string reloc_section; // base section for relocation purposes
27646582 1070 Dwarf_Addr reloc_addr; // relocated
b20febf3 1071 const string& module = dw.module_name; // "kernel" or other
36f9dd1d 1072
37ebca01
FCE
1073 assert (! has_absolute); // already handled in dwarf_builder::build()
1074
789448a3 1075 reloc_addr = dw.relocate_address(addr, reloc_section);
2930abc7 1076
7f9f3386
FCE
1077 if (sess.verbose > 1)
1078 {
b20febf3
FCE
1079 clog << "probe " << funcname << "@" << filename << ":" << line;
1080 if (string(module) == TOK_KERNEL)
1081 clog << " kernel";
91af0778 1082 else if (has_module)
b20febf3 1083 clog << " module=" << module;
91af0778
FCE
1084 else if (has_process)
1085 clog << " process=" << module;
b20febf3 1086 if (reloc_section != "") clog << " reloc=" << reloc_section;
b20febf3 1087 clog << " pc=0x" << hex << addr << dec;
7f9f3386 1088 }
4baf0e53 1089
27646582 1090 bool bad = dw.blacklisted_p (funcname, filename, line, module,
789448a3 1091 addr, has_return);
b20febf3
FCE
1092 if (sess.verbose > 1)
1093 clog << endl;
7f9f3386 1094
84048984
FCE
1095 if (module == TOK_KERNEL)
1096 {
1097 // PR 4224: adapt to relocatable kernel by subtracting the _stext address here.
1098 reloc_addr = addr - sess.sym_stext;
37ebca01 1099 reloc_section = "_stext"; // a message to runtime's _stp_module_relocate
84048984
FCE
1100 }
1101
b20febf3
FCE
1102 if (! bad)
1103 {
1a0dbc5a 1104 sess.unwindsym_modules.insert (module);
6d0f3f0c
FCE
1105
1106 if (has_process)
1107 {
1108 results.push_back (new uprobe_derived_probe(funcname, filename, line,
6b66b9f7 1109 module, reloc_section, addr, reloc_addr,
6d0f3f0c
FCE
1110 *this, scope_die));
1111 }
1112 else
1113 {
1114 assert (has_kernel || has_module);
1115 results.push_back (new dwarf_derived_probe(funcname, filename, line,
06aca46a 1116 module, reloc_section, addr, reloc_addr,
6d0f3f0c
FCE
1117 *this, scope_die));
1118 }
b20febf3 1119 }
2930abc7
FCE
1120}
1121
5f0a03a6
JK
1122enum dbinfo_reqt
1123dwarf_query::assess_dbinfo_reqt()
1124{
1125 if (has_absolute)
1126 {
1127 // kernel.statement(NUM).absolute
1128 return dbr_none;
1129 }
1130 if (has_inline)
1131 {
1132 // kernel.function("f").inline or module("m").function("f").inline
1133 return dbr_need_dwarf;
1134 }
1135 if (has_function_str && spec_type == function_alone)
1136 {
1137 // kernel.function("f") or module("m").function("f")
1138 return dbr_need_symtab;
1139 }
1140 if (has_statement_num)
1141 {
1142 // kernel.statement(NUM) or module("m").statement(NUM)
1143 // Technically, all we need is the module offset (or _stext, for
1144 // the kernel). But for that we need either the ELF file or (for
1145 // _stext) the symbol table. In either case, the symbol table
1146 // is available, and that allows us to map the NUM (address)
1147 // to a function, which is goodness.
1148 return dbr_need_symtab;
1149 }
1150 if (has_function_num)
1151 {
1152 // kernel.function(NUM) or module("m").function(NUM)
1153 // Need the symbol table so we can back up from NUM to the
1154 // start of the function.
1155 return dbr_need_symtab;
1156 }
1157 // Symbol table tells us nothing about source files or line numbers.
1158 return dbr_need_dwarf;
1159}
2930abc7
FCE
1160
1161
b8da0ad1
FCE
1162// The critical determining factor when interpreting a pattern
1163// string is, perhaps surprisingly: "presence of a lineno". The
1164// presence of a lineno changes the search strategy completely.
1165//
1166// Compare the two cases:
1167//
1168// 1. {statement,function}(foo@file.c:lineno)
1169// - find the files matching file.c
1170// - in each file, find the functions matching foo
1171// - query the file for line records matching lineno
1172// - iterate over the line records,
1173// - and iterate over the functions,
1174// - if(haspc(function.DIE, line.addr))
1175// - if looking for statements: probe(lineno.addr)
1176// - if looking for functions: probe(function.{entrypc,return,etc.})
1177//
1178// 2. {statement,function}(foo@file.c)
1179// - find the files matching file.c
1180// - in each file, find the functions matching foo
1181// - probe(function.{entrypc,return,etc.})
1182//
1183// Thus the first decision we make is based on the presence of a
1184// lineno, and we enter entirely different sets of callbacks
1185// depending on that decision.
1186//
1187// Note that the first case is a generalization fo the second, in that
1188// we could theoretically search through line records for matching
1189// file names (a "table scan" in rdbms lingo). Luckily, file names
1190// are already cached elsewhere, so we can do an "index scan" as an
1191// optimization.
7e1279ea 1192
bd2b1e68 1193static void
4cd232e4 1194query_statement (string const & func,
20e4a32c 1195 char const * file,
4cd232e4 1196 int line,
bcc12710 1197 Dwarf_Die *scope_die,
20e4a32c 1198 Dwarf_Addr stmt_addr,
4cd232e4 1199 dwarf_query * q)
bd2b1e68 1200{
39bcd429
FCE
1201 try
1202 {
cee35f73 1203 q->add_probe_point(func, file ? file : "",
a9b2f3a5 1204 line, scope_die, stmt_addr);
39bcd429
FCE
1205 }
1206 catch (const semantic_error& e)
1207 {
1208 q->sess.print_error (e);
1209 }
bd2b1e68
GH
1210}
1211
6b517475
JS
1212static void
1213query_addr(Dwarf_Addr addr, dwarf_query *q)
1214{
1215 dwflpp &dw = q->dw;
1216
08d1d520
MW
1217 if (q->sess.verbose > 2)
1218 clog << "query_addr 0x" << hex << addr << dec << endl;
6b517475
JS
1219
1220 // First pick which CU contains this address
1221 Dwarf_Die* cudie = dw.query_cu_containing_address(addr);
1222 if (!cudie) // address could be wildly out of range
1223 return;
1224 dw.focus_on_cu(cudie);
1225
1226 // Now compensate for the dw bias
1227 addr -= dw.module_bias;
1228
1229 // Per PR5787, we look up the scope die even for
1230 // statement_num's, for blacklist sensitivity and $var
1231 // resolution purposes.
1232
1233 // Find the scopes containing this address
1234 vector<Dwarf_Die> scopes = dw.getscopes(addr);
1235 if (scopes.empty())
1236 return;
1237
1238 // Look for the innermost containing function
1239 Dwarf_Die *fnscope = NULL;
1240 for (size_t i = 0; i < scopes.size(); ++i)
1241 {
1242 int tag = dwarf_tag(&scopes[i]);
1243 if ((tag == DW_TAG_subprogram && !q->has_inline) ||
1244 (tag == DW_TAG_inlined_subroutine &&
1245 !q->has_call && !q->has_return))
1246 {
1247 fnscope = &scopes[i];
1248 break;
1249 }
1250 }
1251 if (!fnscope)
1252 return;
1253 dw.focus_on_function(fnscope);
1254
1255 Dwarf_Die *scope = q->has_function_num ? fnscope : &scopes[0];
1256
1257 const char *file = dwarf_decl_file(fnscope);
1258 int line;
1259 dwarf_decl_line(fnscope, &line);
1260
1261 // Function probes should reset the addr to the function entry
1262 // and possibly perform prologue searching
1263 if (q->has_function_num)
1264 {
1265 dw.die_entrypc(fnscope, &addr);
1266 if (dwarf_tag(fnscope) == DW_TAG_subprogram &&
1267 (q->sess.prologue_searching || q->has_process)) // PR 6871
1268 {
1269 func_info func;
1270 func.die = *fnscope;
1271 func.name = dw.function_name;
1272 func.decl_file = file;
1273 func.decl_line = line;
1274 func.entrypc = addr;
1275
1276 func_info_map_t funcs(1, func);
1277 dw.resolve_prologue_endings (funcs);
1278 if (funcs[0].prologue_end)
1279 addr = funcs[0].prologue_end;
1280 }
1281 }
1282 else
1283 {
1284 dwarf_line_t address_line(dwarf_getsrc_die(cudie, addr));
1285 if (address_line)
1286 {
1287 file = address_line.linesrc();
1288 line = address_line.lineno();
1289 }
1290
1291 // Verify that a raw address matches the beginning of a
1292 // statement. This is a somewhat lame check that the address
1293 // is at the start of an assembly instruction. Mark probes are in the
1294 // middle of a macro and thus not strictly at a statement beginning.
1295 // Guru mode may override this check.
1296 if (!q->has_mark && (!address_line || address_line.addr() != addr))
1297 {
1298 stringstream msg;
1299 msg << "address 0x" << hex << addr
1300 << " does not match the beginning of a statement";
1301 if (address_line)
1302 msg << " (try 0x" << hex << address_line.addr() << ")";
1303 else
1304 msg << " (no line info found for '" << dw.cu_name()
1305 << "', in module '" << dw.module_name << "')";
1306 if (! q->sess.guru_mode)
1307 throw semantic_error(msg.str());
1308 else if (! q->sess.suppress_warnings)
1309 q->sess.print_warning(msg.str());
1310 }
1311 }
1312
1313 // Build a probe at this point
1314 query_statement(dw.function_name, file, line, scope, addr, q);
1315}
1316
8096dd7d
JS
1317static void
1318query_label (string const & func,
1319 char const * label,
1320 char const * file,
1321 int line,
1322 Dwarf_Die *scope_die,
1323 Dwarf_Addr stmt_addr,
1324 dwarf_query * q)
1325{
6b517475
JS
1326 assert (q->has_statement_str || q->has_function_str);
1327
8096dd7d
JS
1328 size_t i = q->results.size();
1329
1330 // weed out functions whose decl_file isn't one of
1331 // the source files that we actually care about
6b517475 1332 if (q->spec_type != function_alone &&
8096dd7d
JS
1333 q->filtered_srcfiles.count(file) == 0)
1334 return;
1335
1336 query_statement(func, file, line, scope_die, stmt_addr, q);
1337
c72aa911
JS
1338 // after the fact, insert the label back into the derivation chain
1339 probe_point::component* ppc =
1340 new probe_point::component(TOK_LABEL, new literal_string (label));
1341 for (; i < q->results.size(); ++i)
1342 {
1343 derived_probe* p = q->results[i];
1344 probe_point* pp = new probe_point(*p->locations[0]);
1345 pp->components.push_back (ppc);
1346 p->base = p->base->create_alias(p->locations[0], pp);
1347 }
8096dd7d
JS
1348}
1349
7e1279ea 1350static void
3e961ba6 1351query_inline_instance_info (inline_instance_info & ii,
7e1279ea
FCE
1352 dwarf_query * q)
1353{
b6581717 1354 try
7e1279ea 1355 {
8f14e444
FCE
1356 assert (! q->has_return); // checked by caller already
1357 if (q->sess.verbose>2)
1358 clog << "querying entrypc "
1359 << hex << ii.entrypc << dec
1360 << " of instance of inline '" << ii.name << "'\n";
1361 query_statement (ii.name, ii.decl_file, ii.decl_line,
1362 &ii.die, ii.entrypc, q);
7e1279ea 1363 }
b6581717 1364 catch (semantic_error &e)
7e1279ea 1365 {
b6581717 1366 q->sess.print_error (e);
7e1279ea
FCE
1367 }
1368}
1369
1370static void
1371query_func_info (Dwarf_Addr entrypc,
bcc12710 1372 func_info & fi,
7e1279ea
FCE
1373 dwarf_query * q)
1374{
b6581717 1375 try
7e1279ea 1376 {
b6581717
GH
1377 if (q->has_return)
1378 {
1379 // NB. dwarf_derived_probe::emit_registrations will emit a
1380 // kretprobe based on the entrypc in this case.
20e4a32c 1381 query_statement (fi.name, fi.decl_file, fi.decl_line,
b6581717
GH
1382 &fi.die, entrypc, q);
1383 }
1384 else
1385 {
35dc8b04 1386 if (fi.prologue_end != 0)
44f75386 1387 {
44f75386
FCE
1388 query_statement (fi.name, fi.decl_file, fi.decl_line,
1389 &fi.die, fi.prologue_end, q);
1390 }
1391 else
1392 {
1393 query_statement (fi.name, fi.decl_file, fi.decl_line,
1394 &fi.die, entrypc, q);
1395 }
b6581717 1396 }
7e1279ea 1397 }
b6581717 1398 catch (semantic_error &e)
7e1279ea 1399 {
b6581717 1400 q->sess.print_error (e);
7e1279ea
FCE
1401 }
1402}
1403
1404
bd4b874d
SC
1405static void
1406query_srcfile_label (const dwarf_line_t& line, void * arg)
1407{
1408 dwarf_query * q = static_cast<dwarf_query *>(arg);
1409
1410 Dwarf_Addr addr = line.addr();
1411
1412 for (func_info_map_t::iterator i = q->filtered_functions.begin();
1413 i != q->filtered_functions.end(); ++i)
1414 if (q->dw.die_has_pc (i->die, addr))
f09d0d1e
JS
1415 q->dw.iterate_over_labels (&i->die, q->label_val, i->name,
1416 q, query_label);
1417
1418 for (inline_instance_map_t::iterator i = q->filtered_inlines.begin();
1419 i != q->filtered_inlines.end(); ++i)
1420 if (q->dw.die_has_pc (i->die, addr))
1421 q->dw.iterate_over_labels (&i->die, q->label_val, i->name,
1422 q, query_label);
bd4b874d
SC
1423}
1424
7e1279ea 1425static void
86bf665e 1426query_srcfile_line (const dwarf_line_t& line, void * arg)
7e1279ea
FCE
1427{
1428 dwarf_query * q = static_cast<dwarf_query *>(arg);
1429
86bf665e 1430 Dwarf_Addr addr = line.addr();
4cd232e4 1431
86bf665e 1432 int lineno = line.lineno();
847bf07f 1433
86bf665e 1434 for (func_info_map_t::iterator i = q->filtered_functions.begin();
7e1279ea
FCE
1435 i != q->filtered_functions.end(); ++i)
1436 {
3e961ba6 1437 if (q->dw.die_has_pc (i->die, addr))
7e1279ea 1438 {
b0ee93c4 1439 if (q->sess.verbose>3)
db22e55f 1440 clog << "function DIE lands on srcfile\n";
4cd232e4 1441 if (q->has_statement_str)
3e961ba6 1442 query_statement (i->name, i->decl_file,
847bf07f 1443 lineno, // NB: not q->line !
3e961ba6 1444 &(i->die), addr, q);
4cd232e4 1445 else
3e961ba6 1446 query_func_info (i->entrypc, *i, q);
7e1279ea 1447 }
20e4a32c
RM
1448 }
1449
86bf665e 1450 for (inline_instance_map_t::iterator i
897820ca
GH
1451 = q->filtered_inlines.begin();
1452 i != q->filtered_inlines.end(); ++i)
1453 {
3e961ba6 1454 if (q->dw.die_has_pc (i->die, addr))
7e1279ea 1455 {
b0ee93c4 1456 if (q->sess.verbose>3)
db22e55f 1457 clog << "inline instance DIE lands on srcfile\n";
897820ca 1458 if (q->has_statement_str)
3e961ba6
JB
1459 query_statement (i->name, i->decl_file,
1460 q->line[0], &(i->die), addr, q);
897820ca 1461 else
3e961ba6 1462 query_inline_instance_info (*i, q);
897820ca 1463 }
20e4a32c 1464 }
7e1279ea
FCE
1465}
1466
1467
7fdd3e2c
JS
1468bool
1469inline_instance_info::operator<(const inline_instance_info& other) const
1470{
1471 if (entrypc != other.entrypc)
1472 return entrypc < other.entrypc;
1473
1474 if (decl_line != other.decl_line)
1475 return decl_line < other.decl_line;
1476
1477 int cmp = name.compare(other.name);
1478 if (!cmp)
1479 cmp = strcmp(decl_file, other.decl_file);
1480 return cmp < 0;
1481}
1482
1483
4fa7b22b 1484static int
7e1279ea 1485query_dwarf_inline_instance (Dwarf_Die * die, void * arg)
4fa7b22b
GH
1486{
1487 dwarf_query * q = static_cast<dwarf_query *>(arg);
6b517475
JS
1488 assert (q->has_statement_str || q->has_function_str);
1489 assert (!q->has_call && !q->has_return);
bd2b1e68 1490
39bcd429 1491 try
7a053d3b 1492 {
b0ee93c4 1493 if (q->sess.verbose>2)
6b517475 1494 clog << "selected inline instance of " << q->dw.function_name << "\n";
7e1279ea 1495
6b517475
JS
1496 Dwarf_Addr entrypc;
1497 if (q->dw.die_entrypc (die, &entrypc))
1498 {
1499 inline_instance_info inl;
1500 inl.die = *die;
1501 inl.name = q->dw.function_name;
1502 inl.entrypc = entrypc;
1503 q->dw.function_file (&inl.decl_file);
1504 q->dw.function_line (&inl.decl_line);
1505
1506 // make sure that this inline hasn't already
1507 // been matched from a different CU
1508 if (q->inline_dupes.insert(inl).second)
1509 q->filtered_inlines.push_back(inl);
1510 }
7e1279ea
FCE
1511 return DWARF_CB_OK;
1512 }
1513 catch (const semantic_error& e)
1514 {
1515 q->sess.print_error (e);
1516 return DWARF_CB_ABORT;
1517 }
1518}
bb788f9f 1519
7e1279ea 1520static int
2da9cedb 1521query_dwarf_func (Dwarf_Die * func, base_query * bq)
7e1279ea 1522{
2da9cedb 1523 dwarf_query * q = static_cast<dwarf_query *>(bq);
6b517475 1524 assert (q->has_statement_str || q->has_function_str);
bb788f9f 1525
bd25380d
JS
1526 // weed out functions whose decl_file isn't one of
1527 // the source files that we actually care about
6b517475 1528 if (q->spec_type != function_alone &&
bd25380d 1529 q->filtered_srcfiles.count(dwarf_decl_file(func)?:"") == 0)
8096dd7d 1530 return DWARF_CB_OK;
bd25380d 1531
7e1279ea
FCE
1532 try
1533 {
7e1279ea
FCE
1534 q->dw.focus_on_function (func);
1535
7d6d0afc
JS
1536 if (!q->dw.function_scope_matches(q->scopes))
1537 return DWARF_CB_OK;
1538
857bdfd1
JS
1539 // make sure that this function address hasn't
1540 // already been matched under an aliased name
1541 Dwarf_Addr addr;
1542 if (!q->dw.func_is_inline() &&
1543 dwarf_entrypc(func, &addr) == 0 &&
1544 !q->alias_dupes.insert(addr).second)
1545 return DWARF_CB_OK;
1546
6b517475 1547 if (q->dw.func_is_inline () && (! q->has_call) && (! q->has_return))
7e1279ea 1548 {
b0ee93c4 1549 if (q->sess.verbose>3)
db22e55f
FCE
1550 clog << "checking instances of inline " << q->dw.function_name
1551 << "\n";
2da9cedb 1552 q->dw.iterate_over_inline_instances (query_dwarf_inline_instance, q);
7e1279ea 1553 }
8f14e444
FCE
1554 else if (q->dw.func_is_inline () && (q->has_return)) // PR 11553
1555 {
1556 q->inlined_non_returnable.insert (q->dw.function_name);
1557 }
396afcee 1558 else if (!q->dw.func_is_inline () && (! q->has_inline))
20e4a32c 1559 {
6b517475
JS
1560 if (q->sess.verbose>2)
1561 clog << "selected function " << q->dw.function_name << "\n";
1562
1563 func_info func;
1564 q->dw.function_die (&func.die);
1565 func.name = q->dw.function_name;
1566 q->dw.function_file (&func.decl_file);
1567 q->dw.function_line (&func.decl_line);
1568
1569 Dwarf_Addr entrypc;
1570 if (q->dw.function_entrypc (&entrypc))
1571 {
1572 func.entrypc = entrypc;
1573 q->filtered_functions.push_back (func);
1574 }
1575 /* else this function is fully inlined, just ignore it */
7e1279ea 1576 }
39bcd429 1577 return DWARF_CB_OK;
bd2b1e68 1578 }
39bcd429 1579 catch (const semantic_error& e)
bd2b1e68 1580 {
39bcd429
FCE
1581 q->sess.print_error (e);
1582 return DWARF_CB_ABORT;
bd2b1e68 1583 }
bd2b1e68
GH
1584}
1585
1586static int
1587query_cu (Dwarf_Die * cudie, void * arg)
1588{
20c6c071 1589 dwarf_query * q = static_cast<dwarf_query *>(arg);
6b517475
JS
1590 assert (q->has_statement_str || q->has_function_str);
1591
85007c04 1592 if (pending_interrupts) return DWARF_CB_ABORT;
7a053d3b 1593
39bcd429 1594 try
bd2b1e68 1595 {
7e1279ea 1596 q->dw.focus_on_cu (cudie);
b5d77020 1597
b0ee93c4 1598 if (false && q->sess.verbose>2)
54417494 1599 clog << "focused on CU '" << q->dw.cu_name()
db22e55f 1600 << "', in module '" << q->dw.module_name << "'\n";
d9b516ca 1601
6b517475
JS
1602 q->filtered_srcfiles.clear();
1603 q->filtered_functions.clear();
1604 q->filtered_inlines.clear();
1605
1606 // In this path, we find "abstract functions", record
1607 // information about them, and then (depending on lineno
1608 // matching) possibly emit one or more of the function's
1609 // associated addresses. Unfortunately the control of this
1610 // cannot easily be turned inside out.
1611
1612 if (q->spec_type != function_alone)
39bcd429 1613 {
6b517475
JS
1614 // If we have a pattern string with a filename, we need
1615 // to elaborate the srcfile mask in question first.
1616 q->dw.collect_srcfiles_matching (q->file, q->filtered_srcfiles);
1617
1618 // If we have a file pattern and *no* srcfile matches, there's
1619 // no need to look further into this CU, so skip.
1620 if (q->filtered_srcfiles.empty())
1621 return DWARF_CB_OK;
1622 }
e4c58386 1623
6b517475
JS
1624 // Pick up [entrypc, name, DIE] tuples for all the functions
1625 // matching the query, and fill in the prologue endings of them
1626 // all in a single pass.
5898b6e1 1627 int rc = q->dw.iterate_over_functions (query_dwarf_func, q, q->function);
6b517475
JS
1628 if (rc != DWARF_CB_OK)
1629 q->query_done = true;
1630
1631 if ((q->sess.prologue_searching || q->has_process) // PR 6871
1632 && !q->has_statement_str) // PR 2608
1633 if (! q->filtered_functions.empty())
1634 q->dw.resolve_prologue_endings (q->filtered_functions);
1635
1636 if (q->spec_type == function_file_and_line)
1637 {
1638 // If we have a pattern string with target *line*, we
1639 // have to look at lines in all the matched srcfiles.
1640 void (* callback) (const dwarf_line_t&, void*) =
1641 q->has_label ? query_srcfile_label : query_srcfile_line;
1642 for (set<string>::const_iterator i = q->filtered_srcfiles.begin();
1643 i != q->filtered_srcfiles.end(); ++i)
1644 q->dw.iterate_over_srcfile_lines (i->c_str(), q->line, q->has_statement_str,
1645 q->line_type, callback, q->function, q);
1646 }
1647 else if (q->has_label)
1648 {
1649 for (func_info_map_t::iterator i = q->filtered_functions.begin();
1650 i != q->filtered_functions.end(); ++i)
1651 q->dw.iterate_over_labels (&i->die, q->label_val, i->name,
1652 q, query_label);
1653
1654 for (inline_instance_map_t::iterator i = q->filtered_inlines.begin();
1655 i != q->filtered_inlines.end(); ++i)
1656 q->dw.iterate_over_labels (&i->die, q->label_val, i->name,
1657 q, query_label);
39bcd429 1658 }
6b517475
JS
1659 else
1660 {
1661 // Otherwise, simply probe all resolved functions.
1662 for (func_info_map_t::iterator i = q->filtered_functions.begin();
1663 i != q->filtered_functions.end(); ++i)
1664 query_func_info (i->entrypc, *i, q);
1665
1666 // And all inline instances (if we're not excluding inlines with ".call")
1667 if (! q->has_call)
1668 for (inline_instance_map_t::iterator i
1669 = q->filtered_inlines.begin(); i != q->filtered_inlines.end(); ++i)
1670 query_inline_instance_info (*i, q);
1671 }
39bcd429 1672 return DWARF_CB_OK;
bd2b1e68 1673 }
39bcd429 1674 catch (const semantic_error& e)
bd2b1e68 1675 {
39bcd429
FCE
1676 q->sess.print_error (e);
1677 return DWARF_CB_ABORT;
bd2b1e68 1678 }
bd2b1e68
GH
1679}
1680
0ce64fb8 1681
4df79aaf
JS
1682void
1683dwarf_query::query_module_functions ()
1684{
1685 try
1686 {
1687 filtered_srcfiles.clear();
1688 filtered_functions.clear();
1689 filtered_inlines.clear();
1690
1691 // Collect all module functions so we know which CUs are interesting
1692 int rc = dw.iterate_single_function(query_dwarf_func, this, function);
1693 if (rc != DWARF_CB_OK)
1694 {
1695 query_done = true;
1696 return;
1697 }
1698
1699 set<void*> used_cus; // by cu->addr
1700 vector<Dwarf_Die> cus;
1701 Dwarf_Die cu_mem;
1702
1703 for (func_info_map_t::iterator i = filtered_functions.begin();
1704 i != filtered_functions.end(); ++i)
1705 if (dwarf_diecu(&i->die, &cu_mem, NULL, NULL) &&
1706 used_cus.insert(cu_mem.addr).second)
1707 cus.push_back(cu_mem);
1708
1709 for (inline_instance_map_t::iterator i = filtered_inlines.begin();
1710 i != filtered_inlines.end(); ++i)
1711 if (dwarf_diecu(&i->die, &cu_mem, NULL, NULL) &&
1712 used_cus.insert(cu_mem.addr).second)
1713 cus.push_back(cu_mem);
1714
1715 // Reset the dupes since we didn't actually collect them the first time
1716 alias_dupes.clear();
1717 inline_dupes.clear();
1718
1719 // Run the query again on the individual CUs
1720 for (vector<Dwarf_Die>::iterator i = cus.begin(); i != cus.end(); ++i)
1721 query_cu(&*i, this);
1722 }
1723 catch (const semantic_error& e)
1724 {
1725 sess.print_error (e);
1726 }
1727}
1728
1729
5f0a03a6
JK
1730static void
1731validate_module_elf (Dwfl_Module *mod, const char *name, base_query *q)
1732{
1733 // Validate the machine code in this elf file against the
1734 // session machine. This is important, in case the wrong kind
1735 // of debuginfo is being automagically processed by elfutils.
1736 // While we can tell i686 apart from x86-64, unfortunately
1737 // we can't help confusing i586 vs i686 (both EM_386).
1738
1739 Dwarf_Addr bias;
1740 // We prefer dwfl_module_getdwarf to dwfl_module_getelf here,
1741 // because dwfl_module_getelf can force costly section relocations
1742 // we don't really need, while either will do for this purpose.
1743 Elf* elf = (dwarf_getelf (dwfl_module_getdwarf (mod, &bias))
1744 ?: dwfl_module_getelf (mod, &bias));
1745
1746 GElf_Ehdr ehdr_mem;
1747 GElf_Ehdr* em = gelf_getehdr (elf, &ehdr_mem);
86bf665e 1748 if (em == 0) { dwfl_assert ("dwfl_getehdr", dwfl_errno()); }
5f0a03a6
JK
1749 int elf_machine = em->e_machine;
1750 const char* debug_filename = "";
1751 const char* main_filename = "";
1752 (void) dwfl_module_info (mod, NULL, NULL,
1753 NULL, NULL, NULL,
1754 & main_filename,
1755 & debug_filename);
1756 const string& sess_machine = q->sess.architecture;
756c9462
FCE
1757
1758 string expect_machine; // to match sess.machine (i.e., kernel machine)
1759 string expect_machine2;
5f0a03a6 1760
d27e6fd5 1761 // NB: See also the 'uname -m' squashing done in main.cxx.
5f0a03a6
JK
1762 switch (elf_machine)
1763 {
756c9462
FCE
1764 // x86 and ppc are bi-architecture; a 64-bit kernel
1765 // can normally run either 32-bit or 64-bit *userspace*.
1766 case EM_386:
1767 expect_machine = "i?86";
1768 if (! q->has_process) break; // 32-bit kernel/module
1769 /* FALLSTHROUGH */
1770 case EM_X86_64:
1771 expect_machine2 = "x86_64";
1772 break;
1773 case EM_PPC:
756c9462 1774 case EM_PPC64:
5a1c472e 1775 expect_machine = "powerpc";
756c9462 1776 break;
3fe7d888 1777 case EM_S390: expect_machine = "s390"; break;
5f0a03a6 1778 case EM_IA_64: expect_machine = "ia64"; break;
d27e6fd5 1779 case EM_ARM: expect_machine = "arm*"; break;
5f0a03a6
JK
1780 // XXX: fill in some more of these
1781 default: expect_machine = "?"; break;
1782 }
1783
1784 if (! debug_filename) debug_filename = main_filename;
1785 if (! debug_filename) debug_filename = name;
1786
756c9462
FCE
1787 if (fnmatch (expect_machine.c_str(), sess_machine.c_str(), 0) != 0 &&
1788 fnmatch (expect_machine2.c_str(), sess_machine.c_str(), 0) != 0)
5f0a03a6
JK
1789 {
1790 stringstream msg;
756c9462
FCE
1791 msg << "ELF machine " << expect_machine << "|" << expect_machine2
1792 << " (code " << elf_machine
5f0a03a6
JK
1793 << ") mismatch with target " << sess_machine
1794 << " in '" << debug_filename << "'";
1795 throw semantic_error(msg.str ());
1796 }
1797
1798 if (q->sess.verbose>2)
1799 clog << "focused on module '" << q->dw.module_name
1800 << " = [0x" << hex << q->dw.module_start
1801 << "-0x" << q->dw.module_end
1802 << ", bias 0x" << q->dw.module_bias << "]" << dec
1803 << " file " << debug_filename
756c9462 1804 << " ELF machine " << expect_machine << "|" << expect_machine2
5f0a03a6
JK
1805 << " (code " << elf_machine << ")"
1806 << "\n";
1807}
1d3a40b6 1808
91af0778
FCE
1809
1810
1811static Dwarf_Addr
1812lookup_symbol_address (Dwfl_Module *m, const char* wanted)
1813{
1814 int syments = dwfl_module_getsymtab(m);
1815 assert(syments);
1816 for (int i = 1; i < syments; ++i)
1817 {
1818 GElf_Sym sym;
1819 const char *name = dwfl_module_getsym(m, i, &sym, NULL);
1820 if (name != NULL && strcmp(name, wanted) == 0)
1821 return sym.st_value;
1822 }
1823
1824 return 0;
1825}
1826
1827
1828
bd2b1e68 1829static int
b8da0ad1 1830query_module (Dwfl_Module *mod,
91af0778 1831 void **,
b8da0ad1 1832 const char *name,
6f4c1275 1833 Dwarf_Addr addr,
b8da0ad1 1834 void *arg)
bd2b1e68 1835{
91af0778 1836 base_query *q = static_cast<base_query *>(arg);
bd2b1e68 1837
39bcd429 1838 try
e38d6504 1839 {
91af0778
FCE
1840 module_info* mi = q->sess.module_cache->cache[name];
1841 if (mi == 0)
1842 {
1843 mi = q->sess.module_cache->cache[name] = new module_info(name);
1844
6f4c1275
FCE
1845 mi->mod = mod;
1846 mi->addr = addr;
91af0778 1847
6f4c1275
FCE
1848 const char* debug_filename = "";
1849 const char* main_filename = "";
1850 (void) dwfl_module_info (mod, NULL, NULL,
1851 NULL, NULL, NULL,
1852 & main_filename,
1853 & debug_filename);
1854
1855 if (q->sess.ignore_vmlinux && name == TOK_KERNEL)
91af0778
FCE
1856 {
1857 // report_kernel() in elfutils found vmlinux, but pretend it didn't.
1858 // Given a non-null path, returning 1 means keep reporting modules.
1859 mi->dwarf_status = info_absent;
1860 }
6f4c1275 1861 else if (debug_filename || main_filename)
91af0778 1862 {
6f4c1275
FCE
1863 mi->elf_path = debug_filename ?: main_filename;
1864 }
1865 else if (name == TOK_KERNEL)
1866 {
1867 mi->dwarf_status = info_absent;
91af0778 1868 }
91af0778
FCE
1869 }
1870 // OK, enough of that module_info caching business.
1871
5f0a03a6 1872 q->dw.focus_on_module(mod, mi);
d9b516ca 1873
39bcd429
FCE
1874 // If we have enough information in the pattern to skip a module and
1875 // the module does not match that information, return early.
b8da0ad1 1876 if (!q->dw.module_name_matches(q->module_val))
85007c04 1877 return pending_interrupts ? DWARF_CB_ABORT : DWARF_CB_OK;
0cbbf9d1
FCE
1878
1879 // Don't allow module("*kernel*") type expressions to match the
1880 // elfutils module "kernel", which we refer to in the probe
1881 // point syntax exclusively as "kernel.*".
1882 if (q->dw.module_name == TOK_KERNEL && ! q->has_kernel)
85007c04 1883 return pending_interrupts ? DWARF_CB_ABORT : DWARF_CB_OK;
b5d77020 1884
5f0a03a6
JK
1885 if (mod)
1886 validate_module_elf(mod, name, q);
1887 else
91af0778
FCE
1888 assert(q->has_kernel); // and no vmlinux to examine
1889
1890 if (q->sess.verbose>2)
1891 cerr << "focused on module '" << q->dw.module_name << "'\n";
1892
1893
1894 // Collect a few kernel addresses. XXX: these belong better
1895 // to the sess.module_info["kernel"] struct.
1896 if (q->dw.module_name == TOK_KERNEL)
c931ec8a 1897 {
91af0778
FCE
1898 if (! q->sess.sym_kprobes_text_start)
1899 q->sess.sym_kprobes_text_start = lookup_symbol_address (mod, "__kprobes_text_start");
1900 if (! q->sess.sym_kprobes_text_end)
1901 q->sess.sym_kprobes_text_end = lookup_symbol_address (mod, "__kprobes_text_end");
1902 if (! q->sess.sym_stext)
1903 q->sess.sym_stext = lookup_symbol_address (mod, "_stext");
c931ec8a
FCE
1904 }
1905
91af0778 1906 // Finally, search the module for matches of the probe point.
2c384610 1907 q->handle_query_module();
bb788f9f 1908
91af0778 1909
b8da0ad1 1910 // If we know that there will be no more matches, abort early.
85007c04 1911 if (q->dw.module_name_final_match(q->module_val) || pending_interrupts)
b8da0ad1
FCE
1912 return DWARF_CB_ABORT;
1913 else
1914 return DWARF_CB_OK;
7a053d3b 1915 }
39bcd429 1916 catch (const semantic_error& e)
bd2b1e68 1917 {
39bcd429
FCE
1918 q->sess.print_error (e);
1919 return DWARF_CB_ABORT;
bd2b1e68 1920 }
bd2b1e68
GH
1921}
1922
35d4ab18 1923
de688825 1924struct dwarf_var_expanding_visitor: public var_expanding_visitor
35d4ab18 1925{
77de5e9e 1926 dwarf_query & q;
bcc12710 1927 Dwarf_Die *scope_die;
77de5e9e 1928 Dwarf_Addr addr;
8c819921 1929 block *add_block;
2260f4e3 1930 block *add_call_probe; // synthesized from .return probes with saved $vars
8cc799a5 1931 bool add_block_tid, add_call_probe_tid;
af234c40
JS
1932 unsigned saved_longs, saved_strings; // data saved within kretprobes
1933 map<std::string, expression *> return_ts_map;
729455a7 1934 vector<Dwarf_Die> scopes;
b95e2b79 1935 bool visited;
77de5e9e 1936
de688825 1937 dwarf_var_expanding_visitor(dwarf_query & q, Dwarf_Die *sd, Dwarf_Addr a):
af234c40 1938 q(q), scope_die(sd), addr(a), add_block(NULL), add_call_probe(NULL),
8cc799a5 1939 add_block_tid(false), add_call_probe_tid(false),
af234c40 1940 saved_longs(0), saved_strings(0), visited(false) {}
277c21bc
JS
1941 expression* gen_mapped_saved_return(expression* e, const string& name);
1942 expression* gen_kretprobe_saved_return(expression* e, const string& name);
a7999c82
JS
1943 void visit_target_symbol_saved_return (target_symbol* e);
1944 void visit_target_symbol_context (target_symbol* e);
d7f3e0c5 1945 void visit_target_symbol (target_symbol* e);
c24447be 1946 void visit_cast_op (cast_op* e);
8cc799a5 1947 void visit_entry_op (entry_op* e);
729455a7
JS
1948private:
1949 vector<Dwarf_Die>& getscopes(target_symbol *e);
77de5e9e
GH
1950};
1951
1952
de688825 1953unsigned var_expanding_visitor::tick = 0;
77de5e9e 1954
a50de939
DS
1955
1956var_expanding_visitor::var_expanding_visitor ()
1957{
1958 // FIXME: for the time being, by default we only support plain '$foo
1959 // = bar', not '+=' or any other op= variant. This is fixable, but a
1960 // bit ugly.
1961 //
1962 // If derived classes desire to add additional operator support, add
1963 // new operators to this list in the derived class constructor.
1964 valid_ops.insert ("=");
1965}
1966
1967
87214add
JS
1968bool
1969var_expanding_visitor::rewrite_lvalue(const token* tok, const std::string& eop,
1970 expression*& lvalue, expression*& rvalue)
77de5e9e 1971{
e57b735a
GH
1972 // Our job would normally be to require() the left and right sides
1973 // into a new assignment. What we're doing is slightly trickier:
1974 // we're pushing a functioncall** onto a stack, and if our left
1975 // child sets the functioncall* for that value, we're going to
1976 // assume our left child was a target symbol -- transformed into a
1977 // set_target_foo(value) call, and it wants to take our right child
1978 // as the argument "value".
1979 //
1980 // This is why some people claim that languages with
1981 // constructor-decomposing case expressions have a leg up on
1982 // visitors.
1983
1984 functioncall *fcall = NULL;
d9b516ca 1985
a50de939 1986 // Let visit_target_symbol know what operator it should handle.
87214add
JS
1987 const string* old_op = op;
1988 op = &eop;
a50de939 1989
e57b735a 1990 target_symbol_setter_functioncalls.push (&fcall);
87214add 1991 replace (lvalue);
e57b735a 1992 target_symbol_setter_functioncalls.pop ();
87214add
JS
1993 replace (rvalue);
1994
1995 op = old_op;
e57b735a
GH
1996
1997 if (fcall != NULL)
77de5e9e 1998 {
e57b735a
GH
1999 // Our left child is informing us that it was a target variable
2000 // and it has been replaced with a set_target_foo() function
2001 // call; we are going to provide that function call -- with the
2002 // right child spliced in as sole argument -- in place of
de688825 2003 // ourselves, in the var expansion we're in the middle of making.
e57b735a 2004
87214add 2005 if (valid_ops.find (eop) == valid_ops.end ())
a50de939
DS
2006 {
2007 // Build up a list of supported operators.
2008 string ops;
2009 std::set<string>::iterator i;
2010 for (i = valid_ops.begin(); i != valid_ops.end(); i++)
2011 ops += " " + *i + ",";
2012 ops.resize(ops.size() - 1); // chop off the last ','
2013
2014 // Throw the error.
2015 throw semantic_error ("Only the following assign operators are"
2016 " implemented on target variables:" + ops,
87214add 2017 tok);
a50de939 2018 }
e57b735a 2019
87214add
JS
2020 assert (lvalue == fcall);
2021 if (rvalue)
2022 fcall->args.push_back (rvalue);
4ed05b15 2023 provide (fcall);
87214add 2024 return true;
77de5e9e 2025 }
e57b735a 2026 else
87214add
JS
2027 return false;
2028}
2029
2030
2031void
2032var_expanding_visitor::visit_assignment (assignment* e)
2033{
2034 if (!rewrite_lvalue (e->tok, e->op, e->left, e->right))
2035 provide (e);
2036}
2037
2038
2039void
2040var_expanding_visitor::visit_pre_crement (pre_crement* e)
2041{
2042 expression *dummy = NULL;
2043 if (!rewrite_lvalue (e->tok, e->op, e->operand, dummy))
2044 provide (e);
2045}
2046
2047
2048void
2049var_expanding_visitor::visit_post_crement (post_crement* e)
2050{
2051 expression *dummy = NULL;
2052 if (!rewrite_lvalue (e->tok, e->op, e->operand, dummy))
2053 provide (e);
2054}
2055
2056
2057void
2058var_expanding_visitor::visit_delete_statement (delete_statement* s)
2059{
2060 string fakeop = "delete";
2061 expression *dummy = NULL;
2062 if (!rewrite_lvalue (s->tok, fakeop, s->value, dummy))
2063 provide (s);
e57b735a 2064}
d9b516ca 2065
d7f3e0c5 2066
30263a73
FCE
2067void
2068var_expanding_visitor::visit_defined_op (defined_op* e)
2069{
2070 bool resolved = true;
2071
2072 defined_ops.push (e);
2073 try {
2074 // NB: provide<>/require<> are NOT typesafe. So even though a defined_op is
2075 // defined with a target_symbol* operand, a subsidiary call may attempt to
2076 // rewrite it to a general expression* instead, and require<> happily
2077 // casts to/from void*, causing possible memory corruption. We use
2078 // expression* here, being the general case of rewritten $variable.
2079 expression *foo1 = e->operand;
2080 foo1 = require (foo1);
2081
c69a87e0 2082 // NB: Formerly, we had some curious cases to consider here, depending on what
30263a73 2083 // various visit_target_symbol() implementations do for successful or
c69a87e0
FCE
2084 // erroneous resolutions. Some would signal a visit_target_symbol failure
2085 // with an exception, with a set flag within the target_symbol, or nothing
2086 // at all.
30263a73 2087 //
c69a87e0
FCE
2088 // Now, failures always have to be signalled with a
2089 // saved_conversion_error being chained to the target_symbol.
2090 // Successes have to result in an attempted rewrite of the
850bfddd 2091 // target_symbol (via provide()).
780f11ff 2092 //
c69a87e0
FCE
2093 // Edna Mode: "no capes". fche: "no exceptions".
2094
30263a73
FCE
2095 // dwarf stuff: success: rewrites to a function; failure: retains target_symbol, sets saved_conversion_error
2096 //
2097 // sdt-kprobes sdt.h: success: string or functioncall; failure: semantic_error
2098 //
2099 // sdt-uprobes: success: string or no op; failure: no op; expect derived/synthetic
2100 // dwarf probe to take care of it.
2101 // But this is rather unhelpful. So we rig the sdt_var_expanding_visitor
2102 // to pass through @defined() to the synthetic dwarf probe.
780f11ff 2103 //
30263a73
FCE
2104 // utrace: success: rewrites to function; failure: semantic_error
2105 //
850bfddd 2106 // procfs: success: rewrites to function; failure: semantic_error
30263a73
FCE
2107
2108 target_symbol* foo2 = dynamic_cast<target_symbol*> (foo1);
c69a87e0 2109 if (foo2 && foo2->saved_conversion_error) // failing
30263a73 2110 resolved = false;
a45664f4 2111 else if (foo2) // unresolved but not marked failing
b7aedf26 2112 {
780f11ff
JS
2113 // There are some visitors that won't touch certain target_symbols,
2114 // e.g. dwarf_var_expanding_visitor won't resolve @cast. We should
2115 // leave it for now so some other visitor can have a chance.
b7aedf26
JS
2116 e->operand = foo2;
2117 provide (e);
2118 return;
2119 }
30263a73
FCE
2120 else // resolved, rewritten to some other expression type
2121 resolved = true;
780f11ff 2122 } catch (const semantic_error& e) {
c69a87e0 2123 assert (0); // should not happen
30263a73
FCE
2124 }
2125 defined_ops.pop ();
2126
2127 literal_number* ln = new literal_number (resolved ? 1 : 0);
2128 ln->tok = e->tok;
2129 provide (ln);
2130}
2131
2132
5f36109e
JS
2133struct dwarf_pretty_print
2134{
2135 dwarf_pretty_print (dwflpp& dw, vector<Dwarf_Die>& scopes, Dwarf_Addr pc,
2136 const string& local, bool userspace_p,
2137 const target_symbol& e):
2138 dw(dw), local(local), scopes(scopes), pc(pc),
2139 pointer(NULL), userspace_p(userspace_p)
2140 {
2141 init_ts (e);
2142 dw.type_die_for_local (scopes, pc, local, ts, &base_type);
2143 }
2144
2145 dwarf_pretty_print (dwflpp& dw, Dwarf_Die *scope_die, Dwarf_Addr pc,
2146 bool userspace_p, const target_symbol& e):
2147 dw(dw), scopes(1, *scope_die), pc(pc),
2148 pointer(NULL), userspace_p(userspace_p)
2149 {
2150 init_ts (e);
2151 dw.type_die_for_return (&scopes[0], pc, ts, &base_type);
2152 }
2153
2154 dwarf_pretty_print (dwflpp& dw, Dwarf_Die *type_die, expression* pointer,
2155 bool userspace_p, const target_symbol& e):
2156 dw(dw), pc(0), pointer(pointer), pointer_type(*type_die),
2157 userspace_p(userspace_p)
2158 {
2159 init_ts (e);
2160 dw.type_die_for_pointer (type_die, ts, &base_type);
2161 }
2162
2163 functioncall* expand ();
2164
2165private:
2166 dwflpp& dw;
2167 target_symbol* ts;
7d11d8c9 2168 bool print_full;
5f36109e
JS
2169 Dwarf_Die base_type;
2170
2171 string local;
2172 vector<Dwarf_Die> scopes;
2173 Dwarf_Addr pc;
2174
2175 expression* pointer;
2176 Dwarf_Die pointer_type;
2177
2178 const bool userspace_p;
2179
2180 void recurse (Dwarf_Die* type, target_symbol* e,
7d11d8c9 2181 print_format* pf, bool top=false);
5f36109e 2182 void recurse_base (Dwarf_Die* type, target_symbol* e,
7d11d8c9 2183 print_format* pf);
5f36109e 2184 void recurse_array (Dwarf_Die* type, target_symbol* e,
7d11d8c9 2185 print_format* pf, bool top);
5f36109e 2186 void recurse_pointer (Dwarf_Die* type, target_symbol* e,
7d11d8c9 2187 print_format* pf, bool top);
5f36109e 2188 void recurse_struct (Dwarf_Die* type, target_symbol* e,
7d11d8c9 2189 print_format* pf, bool top);
5f36109e 2190 void recurse_struct_members (Dwarf_Die* type, target_symbol* e,
7d11d8c9 2191 print_format* pf, int& count);
bbee5bb8 2192 bool print_chars (Dwarf_Die* type, target_symbol* e, print_format* pf);
5f36109e
JS
2193
2194 void init_ts (const target_symbol& e);
2195 expression* deref (target_symbol* e);
2196};
2197
2198
2199void
2200dwarf_pretty_print::init_ts (const target_symbol& e)
2201{
2202 // Work with a new target_symbol so we can modify arguments
2203 ts = new target_symbol (e);
2204
2205 if (ts->addressof)
2206 throw semantic_error("cannot take address of pretty-printed variable", ts->tok);
2207
2208 if (ts->components.empty() ||
2209 ts->components.back().type != target_symbol::comp_pretty_print)
2210 throw semantic_error("invalid target_symbol for pretty-print", ts->tok);
7d11d8c9 2211 print_full = ts->components.back().member.length() > 1;
5f36109e
JS
2212 ts->components.pop_back();
2213}
2214
2215
2216functioncall*
2217dwarf_pretty_print::expand ()
2218{
2219 static unsigned tick = 0;
2220
2221 // function pretty_print_X([pointer], [arg1, arg2, ...]) {
7d11d8c9
JS
2222 // try {
2223 // return sprintf("{.foo=...}", (ts)->foo, ...)
2224 // } catch {
2225 // return "ERROR"
2226 // }
5f36109e
JS
2227 // }
2228
2229 // Create the function decl and call.
2230
2231 functiondecl *fdecl = new functiondecl;
2232 fdecl->tok = ts->tok;
2233 fdecl->synthetic = true;
2234 fdecl->name = "_dwarf_pretty_print_" + lex_cast(tick++);
2235 fdecl->type = pe_string;
2236
2237 functioncall* fcall = new functioncall;
2238 fcall->tok = ts->tok;
2239 fcall->function = fdecl->name;
5f36109e
JS
2240
2241 // If there's a <pointer>, replace it with a new var and make that
2242 // the first function argument.
2243 if (pointer)
2244 {
2245 vardecl *v = new vardecl;
2246 v->type = pe_long;
2247 v->name = "pointer";
2248 v->tok = ts->tok;
2249 fdecl->formal_args.push_back (v);
2250 fcall->args.push_back (pointer);
2251
2252 symbol* sym = new symbol;
2253 sym->tok = ts->tok;
2254 sym->name = v->name;
5f36109e
JS
2255 pointer = sym;
2256 }
2257
2258 // For each expression argument, replace it with a function argument.
2259 for (unsigned i = 0; i < ts->components.size(); ++i)
2260 if (ts->components[i].type == target_symbol::comp_expression_array_index)
2261 {
2262 vardecl *v = new vardecl;
2263 v->type = pe_long;
2264 v->name = "index" + lex_cast(i);
2265 v->tok = ts->tok;
2266 fdecl->formal_args.push_back (v);
2267 fcall->args.push_back (ts->components[i].expr_index);
2268
2269 symbol* sym = new symbol;
2270 sym->tok = ts->tok;
2271 sym->name = v->name;
5f36109e
JS
2272 ts->components[i].expr_index = sym;
2273 }
2274
2275 // Create the return sprintf.
2276 token* pf_tok = new token(*ts->tok);
2277 pf_tok->content = "sprintf";
2278 print_format* pf = print_format::create(pf_tok);
2279 return_statement* rs = new return_statement;
2280 rs->tok = ts->tok;
2281 rs->value = pf;
5f36109e
JS
2282
2283 // Recurse into the actual values.
7d11d8c9 2284 recurse (&base_type, ts, pf, true);
5f36109e
JS
2285 pf->components = print_format::string_to_components(pf->raw_components);
2286
7d11d8c9
JS
2287 // Create the try-catch net
2288 try_block* tb = new try_block;
2289 tb->tok = ts->tok;
2290 tb->try_block = rs;
2291 tb->catch_error_var = 0;
2292 return_statement* rs2 = new return_statement;
2293 rs2->tok = ts->tok;
2294 rs2->value = new literal_string ("ERROR");
2295 rs2->value->tok = ts->tok;
2296 tb->catch_block = rs2;
2297 fdecl->body = tb;
2298
f8809d54 2299 fdecl->join (dw.sess);
5f36109e
JS
2300 return fcall;
2301}
2302
2303
2304void
2305dwarf_pretty_print::recurse (Dwarf_Die* start_type, target_symbol* e,
7d11d8c9 2306 print_format* pf, bool top)
5f36109e
JS
2307{
2308 Dwarf_Die type;
2309 dw.resolve_unqualified_inner_typedie (start_type, &type, e);
2310
2311 switch (dwarf_tag(&type))
2312 {
2313 default:
2314 // XXX need a warning?
2315 // throw semantic_error ("unsupported type (tag " + lex_cast(dwarf_tag(&type))
2316 // + ") for " + dwarf_type_name(&type), e->tok);
2317 pf->raw_components.append("?");
2318 break;
2319
2320 case DW_TAG_enumeration_type:
2321 case DW_TAG_base_type:
7d11d8c9 2322 recurse_base (&type, e, pf);
5f36109e
JS
2323 break;
2324
2325 case DW_TAG_array_type:
7d11d8c9 2326 recurse_array (&type, e, pf, top);
5f36109e
JS
2327 break;
2328
2329 case DW_TAG_pointer_type:
2330 case DW_TAG_reference_type:
2331 case DW_TAG_rvalue_reference_type:
7d11d8c9 2332 recurse_pointer (&type, e, pf, top);
5f36109e
JS
2333 break;
2334
2335 case DW_TAG_subroutine_type:
2336 pf->raw_components.append("<function>:%p");
2337 pf->args.push_back(deref(e));
2338 break;
2339
2340 case DW_TAG_union_type:
5f36109e
JS
2341 case DW_TAG_structure_type:
2342 case DW_TAG_class_type:
7d11d8c9 2343 recurse_struct (&type, e, pf, top);
5f36109e
JS
2344 break;
2345 }
2346}
2347
2348
2349void
2350dwarf_pretty_print::recurse_base (Dwarf_Die* type, target_symbol* e,
7d11d8c9 2351 print_format* pf)
5f36109e
JS
2352{
2353 Dwarf_Attribute attr;
2354 Dwarf_Word encoding = (Dwarf_Word) -1;
2355 dwarf_formudata (dwarf_attr_integrate (type, DW_AT_encoding, &attr),
2356 &encoding);
2357 bool push = true;
2358 switch (encoding)
2359 {
2360 case DW_ATE_float:
2361 case DW_ATE_complex_float:
2362 // XXX need a warning?
2363 // throw semantic_error ("unsupported type (encoding " + lex_cast(encoding)
2364 // + ") for " + dwarf_type_name(type), e->tok);
2365 pf->raw_components.append("?");
2366 push = false;
2367 break;
2368
2369 case DW_ATE_signed_char:
2370 case DW_ATE_unsigned_char:
2371 pf->raw_components.append("'%c'");
2372 break;
2373
2374 case DW_ATE_unsigned:
2375 pf->raw_components.append("%u");
2376 break;
2377
2378 default:
2379 pf->raw_components.append("%i");
2380 break;
2381 }
2382 if (push)
2383 pf->args.push_back(deref(e));
2384}
2385
2386
2387void
2388dwarf_pretty_print::recurse_array (Dwarf_Die* type, target_symbol* e,
7d11d8c9 2389 print_format* pf, bool top)
5f36109e 2390{
7d11d8c9
JS
2391 if (!top && !print_full)
2392 {
2393 pf->raw_components.append("[...]");
2394 return;
2395 }
2396
5f36109e
JS
2397 Dwarf_Die childtype;
2398 dwarf_attr_die (type, DW_AT_type, &childtype);
bbee5bb8
JS
2399
2400 if (print_chars (&childtype, e, pf))
2401 return;
2402
5f36109e
JS
2403 pf->raw_components.append("[");
2404
2405 // We print the array up to the first 5 elements.
2406 // XXX how can we determine the array size?
2407 // ... for now, just print the first element
2408 unsigned i, size = 1;
2409 for (i=0; i < size && i < 5; ++i)
2410 {
2411 if (i > 0)
2412 pf->raw_components.append(", ");
2413 target_symbol* e2 = new target_symbol(*e);
2414 e2->components.push_back (target_symbol::component(e->tok, i));
7d11d8c9 2415 recurse (&childtype, e2, pf);
5f36109e
JS
2416 }
2417 if (i < size || 1/*XXX until real size is known */)
2418 pf->raw_components.append(", ...");
2419 pf->raw_components.append("]");
2420}
2421
2422
2423void
2424dwarf_pretty_print::recurse_pointer (Dwarf_Die* type, target_symbol* e,
7d11d8c9 2425 print_format* pf, bool top)
5f36109e 2426{
7d11d8c9
JS
2427 // We chase to top-level pointers, but leave the rest alone
2428 Dwarf_Die pointee;
bbee5bb8 2429 if (dwarf_attr_die (type, DW_AT_type, &pointee))
5f36109e 2430 {
bbee5bb8
JS
2431 if (print_chars (&pointee, e, pf))
2432 return;
2433
2434 if (top)
2435 {
2436 recurse (&pointee, e, pf, top);
2437 return;
2438 }
5f36109e 2439 }
bbee5bb8
JS
2440
2441 pf->raw_components.append("%p");
2442 pf->args.push_back(deref(e));
5f36109e
JS
2443}
2444
2445
2446void
2447dwarf_pretty_print::recurse_struct (Dwarf_Die* type, target_symbol* e,
7d11d8c9 2448 print_format* pf, bool top)
5f36109e 2449{
bdec0e18
JS
2450 if (dwarf_hasattr(type, DW_AT_declaration))
2451 {
2452 Dwarf_Die *resolved = dw.declaration_resolve(dwarf_diename(type));
2453 if (!resolved)
2454 {
2455 // could be an error, but for now just stub it
2456 // throw semantic_error ("unresolved " + dwarf_type_name(type), e->tok);
2457 pf->raw_components.append("{...}");
2458 return;
2459 }
2460 type = resolved;
2461 }
2462
5f36109e
JS
2463 int count = 0;
2464 pf->raw_components.append("{");
7d11d8c9
JS
2465 if (top || print_full)
2466 recurse_struct_members (type, e, pf, count);
2467 else
2468 pf->raw_components.append("...");
5f36109e
JS
2469 pf->raw_components.append("}");
2470}
2471
2472
2473void
2474dwarf_pretty_print::recurse_struct_members (Dwarf_Die* type, target_symbol* e,
7d11d8c9 2475 print_format* pf, int& count)
5f36109e
JS
2476{
2477 Dwarf_Die child, childtype;
2478 if (dwarf_child (type, &child) == 0)
2479 do
2480 {
2481 target_symbol* e2 = e;
2482
2483 // skip static members
2484 if (dwarf_hasattr(&child, DW_AT_declaration))
2485 continue;
2486
2487 int tag = dwarf_tag (&child);
2488
2489 if (tag != DW_TAG_member && tag != DW_TAG_inheritance)
2490 continue;
2491
2492 dwarf_attr_die (&child, DW_AT_type, &childtype);
2493
2494 if (tag == DW_TAG_inheritance)
2495 {
7d11d8c9 2496 recurse_struct_members (&childtype, e, pf, count);
5f36109e
JS
2497 continue;
2498 }
2499
2500 int childtag = dwarf_tag (&childtype);
2501 const char *member = dwarf_diename (&child);
3a147004
JS
2502
2503 // "_vptr.foo" members are C++ virtual function tables,
2504 // which (generally?) aren't interesting for users.
2505 if (member && startswith(member, "_vptr."))
2506 continue;
2507
5f36109e
JS
2508 if (++count > 1)
2509 pf->raw_components.append(", ");
2510 if (member)
2511 {
5f36109e
JS
2512 pf->raw_components.append(".");
2513 pf->raw_components.append(member);
2514
2515 e2 = new target_symbol(*e);
2516 e2->components.push_back (target_symbol::component(e->tok, member));
2517 }
2518 else if (childtag == DW_TAG_union_type)
2519 pf->raw_components.append("<union>");
2520 else if (childtag == DW_TAG_structure_type)
2521 pf->raw_components.append("<class>");
2522 else if (childtag == DW_TAG_class_type)
2523 pf->raw_components.append("<struct>");
2524 pf->raw_components.append("=");
7d11d8c9 2525 recurse (&childtype, e2, pf);
5f36109e
JS
2526 }
2527 while (dwarf_siblingof (&child, &child) == 0);
2528}
2529
2530
bbee5bb8
JS
2531bool
2532dwarf_pretty_print::print_chars (Dwarf_Die* start_type, target_symbol* e,
2533 print_format* pf)
2534{
2535 Dwarf_Die type;
2536 dw.resolve_unqualified_inner_typedie (start_type, &type, e);
2537 const char *name = dwarf_diename (&type);
2538 if (name && (name == string("char") || name == string("unsigned char")))
2539 {
2540 functioncall* fcall = new functioncall;
2541 fcall->tok = e->tok;
2542 fcall->function = userspace_p ? "user_string2" : "kernel_string2";
2543 fcall->args.push_back (deref (e));
2544 expression *err_msg = new literal_string ("<unknown>");
2545 err_msg->tok = e->tok;
2546 fcall->args.push_back (err_msg);
2547
2548 pf->raw_components.append ("\"%s\"");
2549 pf->args.push_back (fcall);
2550 return true;
2551 }
2552 return false;
2553}
2554
2555
5f36109e
JS
2556expression*
2557dwarf_pretty_print::deref (target_symbol* e)
2558{
2559 static unsigned tick = 0;
2560
2561 // Synthesize a function to dereference the dwarf fields,
2562 // with a pointer parameter that is the base tracepoint variable
2563 functiondecl *fdecl = new functiondecl;
2564 fdecl->synthetic = true;
2565 fdecl->tok = e->tok;
2566 embeddedcode *ec = new embeddedcode;
2567 ec->tok = e->tok;
2568
2569 fdecl->name = "_dwarf_pretty_print_deref_" + lex_cast(tick++);
2570 fdecl->body = ec;
2571
2572 // Synthesize a functioncall.
2573 functioncall* fcall = new functioncall;
2574 fcall->tok = e->tok;
2575 fcall->function = fdecl->name;
5f36109e
JS
2576
2577 // PR10601: adapt to kernel-vs-userspace loc2c-runtime
2578 ec->code += "\n#define fetch_register " + string(userspace_p?"u":"k") + "_fetch_register\n";
2579 ec->code += "#define store_register " + string(userspace_p?"u":"k") + "_store_register\n";
2580
2581 if (pointer)
2582 {
2583 ec->code += dw.literal_stmt_for_pointer (&pointer_type, e,
2584 false, fdecl->type);
2585
2586 vardecl *v = new vardecl;
2587 v->type = pe_long;
2588 v->name = "pointer";
2589 v->tok = e->tok;
2590 fdecl->formal_args.push_back(v);
2591 fcall->args.push_back(pointer);
2592 }
2593 else if (!local.empty())
2594 ec->code += dw.literal_stmt_for_local (scopes, pc, local, e,
2595 false, fdecl->type);
2596 else
2597 ec->code += dw.literal_stmt_for_return (&scopes[0], pc, e,
2598 false, fdecl->type);
2599
2600 // Any non-literal indexes need to be passed in too.
2601 for (unsigned i = 0; i < e->components.size(); ++i)
2602 if (e->components[i].type == target_symbol::comp_expression_array_index)
2603 {
2604 vardecl *v = new vardecl;
2605 v->type = pe_long;
2606 v->name = "index" + lex_cast(i);
2607 v->tok = e->tok;
2608 fdecl->formal_args.push_back(v);
2609 fcall->args.push_back(e->components[i].expr_index);
2610 }
2611
2612 ec->code += "/* pure */";
2613 ec->code += "/* unprivileged */";
2614
2615 // PR10601
2616 ec->code += "\n#undef fetch_register\n";
2617 ec->code += "\n#undef store_register\n";
2618
f8809d54 2619 fdecl->join (dw.sess);
5f36109e
JS
2620 return fcall;
2621}
2622
2623
e57b735a 2624void
a7999c82 2625dwarf_var_expanding_visitor::visit_target_symbol_saved_return (target_symbol* e)
e57b735a 2626{
a7999c82
JS
2627 // Get the full name of the target symbol.
2628 stringstream ts_name_stream;
2629 e->print(ts_name_stream);
2630 string ts_name = ts_name_stream.str();
2631
2632 // Check and make sure we haven't already seen this target
2633 // variable in this return probe. If we have, just return our
2634 // last replacement.
af234c40 2635 map<string, expression *>::iterator i = return_ts_map.find(ts_name);
a7999c82 2636 if (i != return_ts_map.end())
85ecf79a 2637 {
a7999c82
JS
2638 provide (i->second);
2639 return;
2640 }
85ecf79a 2641
70208613
JS
2642 // Attempt the expansion directly first, so if there's a problem with the
2643 // variable we won't have a bogus entry probe lying around. Like in
2644 // saveargs(), we pretend for a moment that we're not in a .return.
2645 bool saved_has_return = q.has_return;
2646 q.has_return = false;
2647 expression *repl = e;
2648 replace (repl);
2649 q.has_return = saved_has_return;
2650 target_symbol* n = dynamic_cast<target_symbol*>(repl);
2651 if (n && n->saved_conversion_error)
2652 {
2653 provide (repl);
2654 return;
2655 }
2656
af234c40
JS
2657 expression *exp;
2658 if (!q.has_process &&
2659 strverscmp(q.sess.kernel_base_release.c_str(), "2.6.25") >= 0)
277c21bc 2660 exp = gen_kretprobe_saved_return(repl, e->name);
af234c40 2661 else
277c21bc 2662 exp = gen_mapped_saved_return(repl, e->name);
af234c40
JS
2663
2664 // Provide the variable to our parent so it can be used as a
2665 // substitute for the target symbol.
2666 provide (exp);
2667
2668 // Remember this replacement since we might be able to reuse
2669 // it later if the same return probe references this target
2670 // symbol again.
2671 return_ts_map[ts_name] = exp;
2672}
2673
2674expression*
70208613 2675dwarf_var_expanding_visitor::gen_mapped_saved_return(expression* e,
277c21bc 2676 const string& name)
af234c40 2677{
a7999c82
JS
2678 // We've got to do several things here to handle target
2679 // variables in return probes.
85ecf79a 2680
a7999c82
JS
2681 // (1) Synthesize two global arrays. One is the cache of the
2682 // target variable and the other contains a thread specific
2683 // nesting level counter. The arrays will look like
2684 // this:
2685 //
2686 // _dwarf_tvar_{name}_{num}
2687 // _dwarf_tvar_{name}_{num}_ctr
2688
2689 string aname = (string("_dwarf_tvar_")
277c21bc 2690 + name.substr(1)
aca66a36 2691 + "_" + lex_cast(tick++));
a7999c82
JS
2692 vardecl* vd = new vardecl;
2693 vd->name = aname;
2694 vd->tok = e->tok;
2695 q.sess.globals.push_back (vd);
2696
2697 string ctrname = aname + "_ctr";
2698 vd = new vardecl;
2699 vd->name = ctrname;
2700 vd->tok = e->tok;
2701 q.sess.globals.push_back (vd);
2702
2703 // (2) Create a new code block we're going to insert at the
2704 // beginning of this probe to get the cached value into a
2705 // temporary variable. We'll replace the target variable
2706 // reference with the temporary variable reference. The code
2707 // will look like this:
2708 //
2709 // _dwarf_tvar_tid = tid()
2710 // _dwarf_tvar_{name}_{num}_tmp
2711 // = _dwarf_tvar_{name}_{num}[_dwarf_tvar_tid,
2712 // _dwarf_tvar_{name}_{num}_ctr[_dwarf_tvar_tid]]
2713 // delete _dwarf_tvar_{name}_{num}[_dwarf_tvar_tid,
2714 // _dwarf_tvar_{name}_{num}_ctr[_dwarf_tvar_tid]--]
2715 // if (! _dwarf_tvar_{name}_{num}_ctr[_dwarf_tvar_tid])
2716 // delete _dwarf_tvar_{name}_{num}_ctr[_dwarf_tvar_tid]
2717
2718 // (2a) Synthesize the tid temporary expression, which will look
2719 // like this:
2720 //
2721 // _dwarf_tvar_tid = tid()
2722 symbol* tidsym = new symbol;
2723 tidsym->name = string("_dwarf_tvar_tid");
2724 tidsym->tok = e->tok;
85ecf79a 2725
a7999c82
JS
2726 if (add_block == NULL)
2727 {
2728 add_block = new block;
2729 add_block->tok = e->tok;
8cc799a5 2730 }
8c819921 2731
8cc799a5
JS
2732 if (!add_block_tid)
2733 {
a7999c82
JS
2734 // Synthesize a functioncall to grab the thread id.
2735 functioncall* fc = new functioncall;
2736 fc->tok = e->tok;
2737 fc->function = string("tid");
8c819921 2738
a7999c82 2739 // Assign the tid to '_dwarf_tvar_tid'.
8c819921
DS
2740 assignment* a = new assignment;
2741 a->tok = e->tok;
2742 a->op = "=";
a7999c82
JS
2743 a->left = tidsym;
2744 a->right = fc;
8c819921
DS
2745
2746 expr_statement* es = new expr_statement;
2747 es->tok = e->tok;
2748 es->value = a;
8c819921 2749 add_block->statements.push_back (es);
8cc799a5 2750 add_block_tid = true;
a7999c82 2751 }
8c819921 2752
a7999c82
JS
2753 // (2b) Synthesize an array reference and assign it to a
2754 // temporary variable (that we'll use as replacement for the
2755 // target variable reference). It will look like this:
2756 //
2757 // _dwarf_tvar_{name}_{num}_tmp
2758 // = _dwarf_tvar_{name}_{num}[_dwarf_tvar_tid,
2759 // _dwarf_tvar_{name}_{num}_ctr[_dwarf_tvar_tid]]
2760
2761 arrayindex* ai_tvar_base = new arrayindex;
2762 ai_tvar_base->tok = e->tok;
2763
2764 symbol* sym = new symbol;
2765 sym->name = aname;
2766 sym->tok = e->tok;
2767 ai_tvar_base->base = sym;
2768
2769 ai_tvar_base->indexes.push_back(tidsym);
2770
2771 // We need to create a copy of the array index in its current
2772 // state so we can have 2 variants of it (the original and one
2773 // that post-decrements the second index).
2774 arrayindex* ai_tvar = new arrayindex;
2775 arrayindex* ai_tvar_postdec = new arrayindex;
2776 *ai_tvar = *ai_tvar_base;
2777 *ai_tvar_postdec = *ai_tvar_base;
2778
2779 // Synthesize the
2780 // "_dwarf_tvar_{name}_{num}_ctr[_dwarf_tvar_tid]" used as the
2781 // second index into the array.
2782 arrayindex* ai_ctr = new arrayindex;
2783 ai_ctr->tok = e->tok;
2784
2785 sym = new symbol;
2786 sym->name = ctrname;
2787 sym->tok = e->tok;
2788 ai_ctr->base = sym;
2789 ai_ctr->indexes.push_back(tidsym);
2790 ai_tvar->indexes.push_back(ai_ctr);
2791
2792 symbol* tmpsym = new symbol;
2793 tmpsym->name = aname + "_tmp";
2794 tmpsym->tok = e->tok;
2795
2796 assignment* a = new assignment;
2797 a->tok = e->tok;
2798 a->op = "=";
2799 a->left = tmpsym;
2800 a->right = ai_tvar;
2801
2802 expr_statement* es = new expr_statement;
2803 es->tok = e->tok;
2804 es->value = a;
2805
2806 add_block->statements.push_back (es);
2807
2808 // (2c) Add a post-decrement to the second array index and
2809 // delete the array value. It will look like this:
2810 //
2811 // delete _dwarf_tvar_{name}_{num}[_dwarf_tvar_tid,
2812 // _dwarf_tvar_{name}_{num}_ctr[_dwarf_tvar_tid]--]
2813
2814 post_crement* pc = new post_crement;
2815 pc->tok = e->tok;
2816 pc->op = "--";
2817 pc->operand = ai_ctr;
2818 ai_tvar_postdec->indexes.push_back(pc);
2819
2820 delete_statement* ds = new delete_statement;
2821 ds->tok = e->tok;
2822 ds->value = ai_tvar_postdec;
2823
2824 add_block->statements.push_back (ds);
2825
2826 // (2d) Delete the counter value if it is 0. It will look like
2827 // this:
2828 // if (! _dwarf_tvar_{name}_{num}_ctr[_dwarf_tvar_tid])
2829 // delete _dwarf_tvar_{name}_{num}_ctr[_dwarf_tvar_tid]
2830
2831 ds = new delete_statement;
2832 ds->tok = e->tok;
2833 ds->value = ai_ctr;
2834
2835 unary_expression *ue = new unary_expression;
2836 ue->tok = e->tok;
2837 ue->op = "!";
2838 ue->operand = ai_ctr;
2839
2840 if_statement *ifs = new if_statement;
2841 ifs->tok = e->tok;
2842 ifs->condition = ue;
2843 ifs->thenblock = ds;
2844 ifs->elseblock = NULL;
2845
2846 add_block->statements.push_back (ifs);
2847
2848 // (3) We need an entry probe that saves the value for us in the
2849 // global array we created. Create the entry probe, which will
2850 // look like this:
2851 //
2260f4e3 2852 // probe kernel.function("{function}").call {
a7999c82
JS
2853 // _dwarf_tvar_tid = tid()
2854 // _dwarf_tvar_{name}_{num}[_dwarf_tvar_tid,
2855 // ++_dwarf_tvar_{name}_{num}_ctr[_dwarf_tvar_tid]]
2856 // = ${param}
2857 // }
2858
2260f4e3 2859 if (add_call_probe == NULL)
a7999c82 2860 {
2260f4e3
FCE
2861 add_call_probe = new block;
2862 add_call_probe->tok = e->tok;
8cc799a5 2863 }
4baf0e53 2864
8cc799a5
JS
2865 if (!add_call_probe_tid)
2866 {
a7999c82
JS
2867 // Synthesize a functioncall to grab the thread id.
2868 functioncall* fc = new functioncall;
2869 fc->tok = e->tok;
2870 fc->function = string("tid");
4baf0e53 2871
a7999c82
JS
2872 // Assign the tid to '_dwarf_tvar_tid'.
2873 assignment* a = new assignment;
8fc05e57
DS
2874 a->tok = e->tok;
2875 a->op = "=";
a7999c82
JS
2876 a->left = tidsym;
2877 a->right = fc;
8fc05e57 2878
a7999c82 2879 expr_statement* es = new expr_statement;
8fc05e57
DS
2880 es->tok = e->tok;
2881 es->value = a;
2260f4e3 2882 add_call_probe = new block(add_call_probe, es);
8cc799a5 2883 add_call_probe_tid = true;
85ecf79a 2884 }
cf2a1f85 2885
a7999c82
JS
2886 // Save the value, like this:
2887 // _dwarf_tvar_{name}_{num}[_dwarf_tvar_tid,
2888 // ++_dwarf_tvar_{name}_{num}_ctr[_dwarf_tvar_tid]]
2889 // = ${param}
2890 arrayindex* ai_tvar_preinc = new arrayindex;
2891 *ai_tvar_preinc = *ai_tvar_base;
2892
2893 pre_crement* preinc = new pre_crement;
2894 preinc->tok = e->tok;
2895 preinc->op = "++";
2896 preinc->operand = ai_ctr;
2897 ai_tvar_preinc->indexes.push_back(preinc);
2898
2899 a = new assignment;
2900 a->tok = e->tok;
2901 a->op = "=";
2902 a->left = ai_tvar_preinc;
2903 a->right = e;
2904
2905 es = new expr_statement;
2906 es->tok = e->tok;
2907 es->value = a;
2908
2260f4e3 2909 add_call_probe = new block(add_call_probe, es);
a7999c82
JS
2910
2911 // (4) Provide the '_dwarf_tvar_{name}_{num}_tmp' variable to
2912 // our parent so it can be used as a substitute for the target
2913 // symbol.
af234c40
JS
2914 return tmpsym;
2915}
a7999c82 2916
af234c40
JS
2917
2918expression*
70208613 2919dwarf_var_expanding_visitor::gen_kretprobe_saved_return(expression* e,
277c21bc 2920 const string& name)
af234c40
JS
2921{
2922 // The code for this is simple.
2923 //
2924 // .call:
2925 // _set_kretprobe_long(index, $value)
2926 //
2927 // .return:
2928 // _get_kretprobe_long(index)
2929 //
2930 // (or s/long/string/ for things like $$parms)
2931
2932 unsigned index;
2933 string setfn, getfn;
2934
2935 // Cheesy way to predetermine that this is a string -- if the second
2936 // character is a '$', then we're looking at a $$vars, $$parms, or $$locals.
2937 // XXX We need real type resolution here, especially if we are ever to
2938 // support an @entry construct.
277c21bc 2939 if (name[1] == '$')
af234c40
JS
2940 {
2941 index = saved_strings++;
2942 setfn = "_set_kretprobe_string";
2943 getfn = "_get_kretprobe_string";
2944 }
2945 else
2946 {
2947 index = saved_longs++;
2948 setfn = "_set_kretprobe_long";
2949 getfn = "_get_kretprobe_long";
2950 }
2951
2952 // Create the entry code
2953 // _set_kretprobe_{long|string}(index, $value)
2954
2955 if (add_call_probe == NULL)
2956 {
2957 add_call_probe = new block;
2958 add_call_probe->tok = e->tok;
2959 }
2960
2961 functioncall* set_fc = new functioncall;
2962 set_fc->tok = e->tok;
2963 set_fc->function = setfn;
2964 set_fc->args.push_back(new literal_number(index));
2965 set_fc->args.back()->tok = e->tok;
2966 set_fc->args.push_back(e);
2967
2968 expr_statement* set_es = new expr_statement;
2969 set_es->tok = e->tok;
2970 set_es->value = set_fc;
2971
2972 add_call_probe->statements.push_back(set_es);
2973
2974 // Create the return code
2975 // _get_kretprobe_{long|string}(index)
2976
2977 functioncall* get_fc = new functioncall;
2978 get_fc->tok = e->tok;
2979 get_fc->function = getfn;
2980 get_fc->args.push_back(new literal_number(index));
2981 get_fc->args.back()->tok = e->tok;
2982
2983 return get_fc;
a7999c82 2984}
a43ba433 2985
2cb3fe26 2986
a7999c82
JS
2987void
2988dwarf_var_expanding_visitor::visit_target_symbol_context (target_symbol* e)
2989{
9aa8ffce 2990 if (null_die(scope_die))
a7999c82 2991 return;
2cb3fe26 2992
5f36109e
JS
2993 target_symbol *tsym = new target_symbol(*e);
2994
2995 string format = "=%#x";
2996 if (!e->components.empty() &&
2997 e->components[0].type == target_symbol::comp_pretty_print)
2998 format = "=%s";
a43ba433 2999
a7999c82
JS
3000 // Convert $$parms to sprintf of a list of parms and active local vars
3001 // which we recursively evaluate
a43ba433 3002
a7999c82
JS
3003 // NB: we synthesize a new token here rather than reusing
3004 // e->tok, because print_format::print likes to use
3005 // its tok->content.
5f36109e 3006 token* pf_tok = new token(*e->tok);
a7999c82 3007 pf_tok->type = tok_identifier;
b393f6f2 3008 pf_tok->content = "sprintf";
2cb3fe26 3009
d5e178c1 3010 print_format* pf = print_format::create(pf_tok);
a7999c82 3011
277c21bc 3012 if (q.has_return && (e->name == "$$return"))
a7999c82 3013 {
277c21bc 3014 tsym->name = "$return";
a7999c82
JS
3015
3016 // Ignore any variable that isn't accessible.
3017 tsym->saved_conversion_error = 0;
3018 expression *texp = tsym;
8b095b45 3019 replace (texp); // NB: throws nothing ...
a7999c82 3020 if (tsym->saved_conversion_error) // ... but this is how we know it happened.
a43ba433 3021 {
2cb3fe26 3022
a43ba433
FCE
3023 }
3024 else
3025 {
a7999c82 3026 pf->raw_components += "return";
5f36109e 3027 pf->raw_components += format;
a7999c82
JS
3028 pf->args.push_back(texp);
3029 }
3030 }
3031 else
3032 {
3033 // non-.return probe: support $$parms, $$vars, $$locals
345bbb3d 3034 bool first = true;
a7999c82 3035 Dwarf_Die result;
9aa8ffce 3036 vector<Dwarf_Die> scopes = q.dw.getscopes_die(scope_die);
a7999c82
JS
3037 if (dwarf_child (&scopes[0], &result) == 0)
3038 do
3039 {
3040 switch (dwarf_tag (&result))
00cf3709 3041 {
a7999c82 3042 case DW_TAG_variable:
277c21bc 3043 if (e->name == "$$parms")
a7999c82
JS
3044 continue;
3045 break;
3046 case DW_TAG_formal_parameter:
277c21bc 3047 if (e->name == "$$locals")
a7999c82
JS
3048 continue;
3049 break;
3050
3051 default:
3052 continue;
3053 }
41c262f3 3054
a7999c82
JS
3055 const char *diename = dwarf_diename (&result);
3056 if (! diename) continue;
f76427a2 3057
345bbb3d
MW
3058 if (! first)
3059 pf->raw_components += " ";
3060 pf->raw_components += diename;
3061
277c21bc
JS
3062 tsym->name = "$";
3063 tsym->name += diename;
41c262f3 3064
a7999c82
JS
3065 // Ignore any variable that isn't accessible.
3066 tsym->saved_conversion_error = 0;
3067 expression *texp = tsym;
8b095b45 3068 replace (texp); // NB: throws nothing ...
a7999c82
JS
3069 if (tsym->saved_conversion_error) // ... but this is how we know it happened.
3070 {
3071 if (q.sess.verbose>2)
a43ba433 3072 {
a7999c82
JS
3073 for (semantic_error *c = tsym->saved_conversion_error;
3074 c != 0;
3075 c = c->chain) {
3076 clog << "variable location problem: " << c->what() << endl;
3077 }
a43ba433 3078 }
a7999c82 3079
345bbb3d 3080 pf->raw_components += "=?";
00cf3709 3081 }
a7999c82
JS
3082 else
3083 {
5f36109e 3084 pf->raw_components += format;
a7999c82
JS
3085 pf->args.push_back(texp);
3086 }
345bbb3d 3087 first = false;
a7999c82
JS
3088 }
3089 while (dwarf_siblingof (&result, &result) == 0);
3090 }
2cb3fe26 3091
a7999c82
JS
3092 pf->components = print_format::string_to_components(pf->raw_components);
3093 provide (pf);
3094}
3095
2cb3fe26 3096
a7999c82
JS
3097void
3098dwarf_var_expanding_visitor::visit_target_symbol (target_symbol *e)
3099{
277c21bc 3100 assert(e->name.size() > 0 && e->name[0] == '$');
a7999c82 3101 visited = true;
30263a73
FCE
3102 bool defined_being_checked = (defined_ops.size() > 0 && (defined_ops.top()->operand == e));
3103 // In this mode, we avoid hiding errors or generating extra code such as for .return saved $vars
a7999c82 3104
70208613 3105 try
a7999c82 3106 {
c69a87e0
FCE
3107 bool lvalue = is_active_lvalue(e);
3108 if (lvalue && !q.sess.guru_mode)
3109 throw semantic_error("write to target variable not permitted", e->tok);
2cb3fe26 3110
c69a87e0 3111 // XXX: process $context vars should be writeable
70208613 3112
c69a87e0
FCE
3113 // See if we need to generate a new probe to save/access function
3114 // parameters from a return probe. PR 1382.
3115 if (q.has_return
3116 && !defined_being_checked
277c21bc
JS
3117 && e->name != "$return" // not the special return-value variable handled below
3118 && e->name != "$$return") // nor the other special variable handled below
c69a87e0
FCE
3119 {
3120 if (lvalue)
3121 throw semantic_error("write to target variable not permitted in .return probes", e->tok);
3122 visit_target_symbol_saved_return(e);
3123 return;
3124 }
e57b735a 3125
277c21bc
JS
3126 if (e->name == "$$vars" || e->name == "$$parms" || e->name == "$$locals"
3127 || (q.has_return && (e->name == "$$return")))
c69a87e0
FCE
3128 {
3129 if (lvalue)
3130 throw semantic_error("cannot write to context variable", e->tok);
70208613 3131
c69a87e0
FCE
3132 if (e->addressof)
3133 throw semantic_error("cannot take address of context variable", e->tok);
70208613 3134
5f36109e
JS
3135 e->assert_no_components("dwarf", true);
3136
c69a87e0
FCE
3137 visit_target_symbol_context(e);
3138 return;
3139 }
70208613 3140
5f36109e
JS
3141 if (!e->components.empty() &&
3142 e->components.back().type == target_symbol::comp_pretty_print)
3143 {
3144 if (lvalue)
3145 throw semantic_error("cannot write to pretty-printed variable", e->tok);
3146
277c21bc 3147 if (q.has_return && (e->name == "$return"))
5f36109e
JS
3148 {
3149 dwarf_pretty_print dpp (q.dw, scope_die, addr,
3150 q.has_process, *e);
3151 dpp.expand()->visit(this);
3152 }
3153 else
3154 {
3155 dwarf_pretty_print dpp (q.dw, getscopes(e), addr,
277c21bc 3156 e->name.substr(1),
5f36109e
JS
3157 q.has_process, *e);
3158 dpp.expand()->visit(this);
3159 }
3160 return;
3161 }
3162
c69a87e0
FCE
3163 // Synthesize a function.
3164 functiondecl *fdecl = new functiondecl;
59de45f1 3165 fdecl->synthetic = true;
c69a87e0
FCE
3166 fdecl->tok = e->tok;
3167 embeddedcode *ec = new embeddedcode;
3168 ec->tok = e->tok;
70208613 3169
c69a87e0 3170 string fname = (string(lvalue ? "_dwarf_tvar_set" : "_dwarf_tvar_get")
277c21bc 3171 + "_" + e->name.substr(1)
c69a87e0 3172 + "_" + lex_cast(tick++));
70208613 3173
b5a0dd41
FCE
3174 // PR10601: adapt to kernel-vs-userspace loc2c-runtime
3175 ec->code += "\n#define fetch_register " + string(q.has_process?"u":"k") + "_fetch_register\n";
3176 ec->code += "#define store_register " + string(q.has_process?"u":"k") + "_store_register\n";
70208613 3177
277c21bc 3178 if (q.has_return && (e->name == "$return"))
e19fda4e 3179 {
b5a0dd41 3180 ec->code += q.dw.literal_stmt_for_return (scope_die,
e19fda4e 3181 addr,
b4c34c26 3182 e,
e19fda4e
DS
3183 lvalue,
3184 fdecl->type);
3185 }
3186 else
3187 {
b5a0dd41 3188 ec->code += q.dw.literal_stmt_for_local (getscopes(e),
e19fda4e 3189 addr,
277c21bc 3190 e->name.substr(1),
b4c34c26 3191 e,
e19fda4e
DS
3192 lvalue,
3193 fdecl->type);
3194 }
3195
1b07c728
FCE
3196 if (! lvalue)
3197 ec->code += "/* pure */";
64211010
DB
3198
3199 ec->code += "/* unprivileged */";
b5a0dd41
FCE
3200
3201 // PR10601
3202 ec->code += "\n#undef fetch_register\n";
3203 ec->code += "\n#undef store_register\n";
c69a87e0
FCE
3204
3205 fdecl->name = fname;
3206 fdecl->body = ec;
70208613 3207
c69a87e0
FCE
3208 // Any non-literal indexes need to be passed in too.
3209 for (unsigned i = 0; i < e->components.size(); ++i)
3210 if (e->components[i].type == target_symbol::comp_expression_array_index)
3211 {
3212 vardecl *v = new vardecl;
3213 v->type = pe_long;
3214 v->name = "index" + lex_cast(i);
3215 v->tok = e->tok;
3216 fdecl->formal_args.push_back(v);
3217 }
70208613 3218
c69a87e0
FCE
3219 if (lvalue)
3220 {
3221 // Modify the fdecl so it carries a single pe_long formal
3222 // argument called "value".
70208613 3223
c69a87e0
FCE
3224 // FIXME: For the time being we only support setting target
3225 // variables which have base types; these are 'pe_long' in
3226 // stap's type vocabulary. Strings and pointers might be
3227 // reasonable, some day, but not today.
70208613 3228
c69a87e0
FCE
3229 vardecl *v = new vardecl;
3230 v->type = pe_long;
3231 v->name = "value";
3232 v->tok = e->tok;
3233 fdecl->formal_args.push_back(v);
3234 }
f8809d54 3235 fdecl->join (q.sess);
70208613 3236
c69a87e0
FCE
3237 // Synthesize a functioncall.
3238 functioncall* n = new functioncall;
3239 n->tok = e->tok;
3240 n->function = fname;
70208613 3241
c69a87e0
FCE
3242 // Any non-literal indexes need to be passed in too.
3243 for (unsigned i = 0; i < e->components.size(); ++i)
3244 if (e->components[i].type == target_symbol::comp_expression_array_index)
3245 n->args.push_back(require(e->components[i].expr_index));
70208613 3246
c69a87e0
FCE
3247 if (lvalue)
3248 {
3249 // Provide the functioncall to our parent, so that it can be
3250 // used to substitute for the assignment node immediately above
3251 // us.
3252 assert(!target_symbol_setter_functioncalls.empty());
3253 *(target_symbol_setter_functioncalls.top()) = n;
3254 }
70208613 3255
c69a87e0 3256 provide (n);
66d284f4
FCE
3257 }
3258 catch (const semantic_error& er)
3259 {
9fab2262
JS
3260 // We suppress this error message, and pass the unresolved
3261 // target_symbol to the next pass. We hope that this value ends
3262 // up not being referenced after all, so it can be optimized out
3263 // quietly.
1af1e62d 3264 e->chain (er);
9fab2262 3265 provide (e);
66d284f4 3266 }
77de5e9e
GH
3267}
3268
3269
c24447be
JS
3270void
3271dwarf_var_expanding_visitor::visit_cast_op (cast_op *e)
3272{
3273 // Fill in our current module context if needed
3274 if (e->module.empty())
3275 e->module = q.dw.module_name;
3276
3277 var_expanding_visitor::visit_cast_op(e);
3278}
3279
3280
8cc799a5
JS
3281void
3282dwarf_var_expanding_visitor::visit_entry_op (entry_op *e)
3283{
3284 expression *repl = e;
3285 if (q.has_return)
3286 {
3287 // expand the operand as if it weren't a return probe
3288 q.has_return = false;
3289 replace (e->operand);
3290 q.has_return = true;
3291
3292 // XXX it would be nice to use gen_kretprobe_saved_return when available,
3293 // but it requires knowing the types already, which is problematic for
3294 // arbitrary expressons.
3295 repl = gen_mapped_saved_return (e->operand, "@entry");
3296 }
3297 provide (repl);
3298}
3299
3300
729455a7
JS
3301vector<Dwarf_Die>&
3302dwarf_var_expanding_visitor::getscopes(target_symbol *e)
3303{
3304 if (scopes.empty())
3305 {
3306 // If the address is at the beginning of the scope_die, we can do a fast
3307 // getscopes from there. Otherwise we need to look it up by address.
3308 Dwarf_Addr entrypc;
3309 if (q.dw.die_entrypc(scope_die, &entrypc) && entrypc == addr)
3310 scopes = q.dw.getscopes(scope_die);
3311 else
3312 scopes = q.dw.getscopes(addr);
3313
3314 if (scopes.empty())
3315 throw semantic_error ("unable to find any scopes containing "
3316 + lex_cast_hex(addr)
3317 + ((scope_die == NULL) ? ""
3318 : (string (" in ")
3319 + (dwarf_diename(scope_die) ?: "<unknown>")
3320 + "(" + (dwarf_diename(q.dw.cu) ?: "<unknown>")
3321 + ")"))
3322 + " while searching for local '"
277c21bc 3323 + e->name.substr(1) + "'",
729455a7
JS
3324 e->tok);
3325 }
3326 return scopes;
3327}
3328
3329
5f36109e
JS
3330struct dwarf_cast_expanding_visitor: public var_expanding_visitor
3331{
3332 systemtap_session& s;
3333 dwarf_builder& db;
3334
3335 dwarf_cast_expanding_visitor(systemtap_session& s, dwarf_builder& db):
3336 s(s), db(db) {}
3337 void visit_cast_op (cast_op* e);
3338 void filter_special_modules(string& module);
3339};
3340
3341
c4ce66a1
JS
3342struct dwarf_cast_query : public base_query
3343{
946e1a48 3344 cast_op& e;
c4ce66a1 3345 const bool lvalue;
5f36109e
JS
3346 const bool userspace_p;
3347 functioncall*& result;
c4ce66a1 3348
5f36109e
JS
3349 dwarf_cast_query(dwflpp& dw, const string& module, cast_op& e, bool lvalue,
3350 const bool userspace_p, functioncall*& result):
abb41d92 3351 base_query(dw, module), e(e), lvalue(lvalue),
5f36109e 3352 userspace_p(userspace_p), result(result) {}
c4ce66a1
JS
3353
3354 void handle_query_module();
c4ce66a1
JS
3355};
3356
3357
c4ce66a1
JS
3358void
3359dwarf_cast_query::handle_query_module()
3360{
5f36109e
JS
3361 static unsigned tick = 0;
3362
3363 if (result)
c4ce66a1
JS
3364 return;
3365
ea1e477a 3366 // look for the type in any CU
7f6b80bd 3367 Dwarf_Die* type_die = dw.declaration_resolve_other_cus(e.type_name.c_str());
ea1e477a
JS
3368 if (!type_die)
3369 return;
c4ce66a1 3370
5f36109e
JS
3371 string code;
3372 exp_type type = pe_long;
3373
ea1e477a 3374 try
c4ce66a1 3375 {
ea1e477a
JS
3376 Dwarf_Die cu_mem;
3377 dw.focus_on_cu(dwarf_diecu(type_die, &cu_mem, NULL, NULL));
5f36109e
JS
3378
3379 if (!e.components.empty() &&
3380 e.components.back().type == target_symbol::comp_pretty_print)
3381 {
3382 if (lvalue)
3383 throw semantic_error("cannot write to pretty-printed variable", e.tok);
3384
3385 dwarf_pretty_print dpp(dw, type_die, e.operand, userspace_p, e);
3386 result = dpp.expand();
3387 return;
3388 }
3389
3390 code = dw.literal_stmt_for_pointer (type_die, &e, lvalue, type);
ea1e477a
JS
3391 }
3392 catch (const semantic_error& er)
3393 {
3394 // NB: we can have multiple errors, since a @cast
1af1e62d
JS
3395 // may be attempted using several different modules:
3396 // @cast(ptr, "type", "module1:module2:...")
3397 e.chain (er);
c4ce66a1 3398 }
c4ce66a1 3399
5f36109e
JS
3400 if (code.empty())
3401 return;
c4ce66a1 3402
5f36109e 3403 string fname = (string(lvalue ? "_dwarf_cast_set" : "_dwarf_cast_get")
277c21bc 3404 + "_" + e.name.substr(1)
5f36109e 3405 + "_" + lex_cast(tick++));
c4ce66a1 3406
5f36109e
JS
3407 // Synthesize a function.
3408 functiondecl *fdecl = new functiondecl;
3409 fdecl->synthetic = true;
3410 fdecl->tok = e.tok;
3411 fdecl->type = type;
3412 fdecl->name = fname;
3413
3414 embeddedcode *ec = new embeddedcode;
3415 ec->tok = e.tok;
3416 fdecl->body = ec;
3417
3418 // PR10601: adapt to kernel-vs-userspace loc2c-runtime
3419 ec->code += "\n#define fetch_register " + string(userspace_p?"u":"k") + "_fetch_register\n";
3420 ec->code += "#define store_register " + string(userspace_p?"u":"k") + "_store_register\n";
3421
3422 ec->code += code;
3423
3424 // Give the fdecl an argument for the pointer we're trying to cast
3425 vardecl *v1 = new vardecl;
3426 v1->type = pe_long;
3427 v1->name = "pointer";
3428 v1->tok = e.tok;
3429 fdecl->formal_args.push_back(v1);
3430
3431 // Any non-literal indexes need to be passed in too.
3432 for (unsigned i = 0; i < e.components.size(); ++i)
3433 if (e.components[i].type == target_symbol::comp_expression_array_index)
3434 {
3435 vardecl *v = new vardecl;
3436 v->type = pe_long;
3437 v->name = "index" + lex_cast(i);
3438 v->tok = e.tok;
3439 fdecl->formal_args.push_back(v);
3440 }
3441
3442 if (lvalue)
3443 {
3444 // Modify the fdecl so it carries a second pe_long formal
3445 // argument called "value".
3446
3447 // FIXME: For the time being we only support setting target
3448 // variables which have base types; these are 'pe_long' in
3449 // stap's type vocabulary. Strings and pointers might be
3450 // reasonable, some day, but not today.
3451
3452 vardecl *v2 = new vardecl;
3453 v2->type = pe_long;
3454 v2->name = "value";
3455 v2->tok = e.tok;
3456 fdecl->formal_args.push_back(v2);
3457 }
3458 else
3459 ec->code += "/* pure */";
3460
3461 ec->code += "/* unprivileged */";
3462
3463 // PR10601
3464 ec->code += "\n#undef fetch_register\n";
3465 ec->code += "\n#undef store_register\n";
3466
f8809d54 3467 fdecl->join (dw.sess);
5f36109e
JS
3468
3469 // Synthesize a functioncall.
3470 functioncall* n = new functioncall;
3471 n->tok = e.tok;
3472 n->function = fname;
5f36109e
JS
3473 n->args.push_back(e.operand);
3474
3475 // Any non-literal indexes need to be passed in too.
3476 for (unsigned i = 0; i < e.components.size(); ++i)
3477 if (e.components[i].type == target_symbol::comp_expression_array_index)
3478 n->args.push_back(e.components[i].expr_index);
3479
3480 result = n;
3481}
c4ce66a1
JS
3482
3483
fb0274bc
JS
3484void dwarf_cast_expanding_visitor::filter_special_modules(string& module)
3485{
d90053e7 3486 // look for "<path/to/header>" or "kernel<path/to/header>"
fb0274bc 3487 // for those cases, build a module including that header
d90053e7 3488 if (module[module.size() - 1] == '>' &&
60d98537 3489 (module[0] == '<' || startswith(module, "kernel<")))
fb0274bc
JS
3490 {
3491 string cached_module;
3492 if (s.use_cache)
3493 {
3494 // see if the cached module exists
a2639cb7 3495 cached_module = find_typequery_hash(s, module);
d105f664 3496 if (!cached_module.empty() && !s.poison_cache)
fb0274bc
JS
3497 {
3498 int fd = open(cached_module.c_str(), O_RDONLY);
3499 if (fd != -1)
3500 {
3501 if (s.verbose > 2)
3502 clog << "Pass 2: using cached " << cached_module << endl;
3503 module = cached_module;
3504 close(fd);
3505 return;
3506 }
3507 }
3508 }
3509
3510 // no cached module, time to make it
d90053e7 3511 if (make_typequery(s, module) == 0)
fb0274bc 3512 {
e16dc041 3513 // try to save typequery in the cache
fb0274bc 3514 if (s.use_cache)
e16dc041 3515 copy_file(module, cached_module, s.verbose > 2);
fb0274bc
JS
3516 }
3517 }
3518}
3519
3520
c4ce66a1
JS
3521void dwarf_cast_expanding_visitor::visit_cast_op (cast_op* e)
3522{
3523 bool lvalue = is_active_lvalue(e);
3524 if (lvalue && !s.guru_mode)
3525 throw semantic_error("write to typecast value not permitted", e->tok);
3526
3527 if (e->module.empty())
3528 e->module = "kernel"; // "*" may also be reasonable to search all kernel modules
3529
5f36109e 3530 functioncall* result = NULL;
8b31197b
JS
3531
3532 // split the module string by ':' for alternatives
3533 vector<string> modules;
3534 tokenize(e->module, modules, ":");
b5a0dd41 3535 bool userspace_p=false; // PR10601
5f36109e 3536 for (unsigned i = 0; !result && i < modules.size(); ++i)
c4ce66a1 3537 {
8b31197b 3538 string& module = modules[i];
fb0274bc 3539 filter_special_modules(module);
abb41d92 3540
c4ce66a1
JS
3541 // NB: This uses '/' to distinguish between kernel modules and userspace,
3542 // which means that userspace modules won't get any PATH searching.
3543 dwflpp* dw;
707bf35e
JS
3544 try
3545 {
b5a0dd41
FCE
3546 userspace_p=is_user_module (module);
3547 if (! userspace_p)
707bf35e
JS
3548 {
3549 // kernel or kernel module target
ae2552da 3550 dw = db.get_kern_dw(s, module);
707bf35e
JS
3551 }
3552 else
3553 {
3554 module = find_executable (module); // canonicalize it
3555 dw = db.get_user_dw(s, module);
3556 }
3557 }
3558 catch (const semantic_error& er)
3559 {
3560 /* ignore and go to the next module */
3561 continue;
3562 }
c4ce66a1 3563
5f36109e 3564 dwarf_cast_query q (*dw, module, *e, lvalue, userspace_p, result);
51178501 3565 dw->iterate_over_modules(&query_module, &q);
c4ce66a1 3566 }
abb41d92 3567
5f36109e 3568 if (!result)
c4ce66a1 3569 {
946e1a48
JS
3570 // We pass the unresolved cast_op to the next pass, and hope
3571 // that this value ends up not being referenced after all, so
3572 // it can be optimized out quietly.
c4ce66a1
JS
3573 provide (e);
3574 return;
3575 }
3576
c4ce66a1
JS
3577 if (lvalue)
3578 {
3579 // Provide the functioncall to our parent, so that it can be
3580 // used to substitute for the assignment node immediately above
3581 // us.
3582 assert(!target_symbol_setter_functioncalls.empty());
5f36109e 3583 *(target_symbol_setter_functioncalls.top()) = result;
c4ce66a1
JS
3584 }
3585
5f36109e 3586 result->visit (this);
77de5e9e
GH
3587}
3588
3589
b8da0ad1
FCE
3590void
3591dwarf_derived_probe::printsig (ostream& o) const
3592{
3593 // Instead of just printing the plain locations, we add a PC value
3594 // as a comment as a way of telling e.g. apart multiple inlined
3595 // function instances. This is distinct from the verbose/clog
3596 // output, since this part goes into the cache hash calculations.
3597 sole_location()->print (o);
6d0f3f0c 3598 o << " /* pc=" << section << "+0x" << hex << addr << dec << " */";
b8da0ad1
FCE
3599 printsig_nested (o);
3600}
3601
3602
3603
dc38c0ae 3604void
b20febf3
FCE
3605dwarf_derived_probe::join_group (systemtap_session& s)
3606{
af234c40
JS
3607 // skip probes which are paired entry-handlers
3608 if (!has_return && (saved_longs || saved_strings))
3609 return;
3610
b20febf3
FCE
3611 if (! s.dwarf_derived_probes)
3612 s.dwarf_derived_probes = new dwarf_derived_probe_group ();
3613 s.dwarf_derived_probes->enroll (this);
b642c901
SC
3614
3615 enable_task_finder(s);
b20febf3
FCE
3616}
3617
3618
3619dwarf_derived_probe::dwarf_derived_probe(const string& funcname,
3620 const string& filename,
3621 int line,
91af0778 3622 // module & section specify a relocation
b20febf3
FCE
3623 // base for <addr>, unless section==""
3624 // (equivalently module=="kernel")
3625 const string& module,
3626 const string& section,
3627 // NB: dwfl_addr is the virtualized
3628 // address for this symbol.
3629 Dwarf_Addr dwfl_addr,
3630 // addr is the section-offset for
3631 // actual relocation.
3632 Dwarf_Addr addr,
3633 dwarf_query& q,
37ebca01 3634 Dwarf_Die* scope_die /* may be null */)
1939ea32 3635 : derived_probe (q.base_probe, new probe_point(*q.base_loc) /* .components soon rewritten */ ),
b20febf3 3636 module (module), section (section), addr (addr),
63b4fd14 3637 path (q.path),
27dc09b1 3638 has_process (q.has_process),
c9bad430
DS
3639 has_return (q.has_return),
3640 has_maxactive (q.has_maxactive),
63b4fd14 3641 has_library (q.has_library),
6b66b9f7 3642 maxactive_val (q.maxactive_val),
b642c901
SC
3643 user_path (q.user_path),
3644 user_lib (q.user_lib),
af234c40 3645 access_vars(false),
63b4fd14 3646 saved_longs(0), saved_strings(0),
af234c40 3647 entry_handler(0)
bd2b1e68 3648{
b642c901
SC
3649 if (user_lib.size() != 0)
3650 has_library = true;
3651
6b66b9f7
JS
3652 if (q.has_process)
3653 {
3654 // We may receive probes on two types of ELF objects: ET_EXEC or ET_DYN.
3655 // ET_EXEC ones need no further relocation on the addr(==dwfl_addr), whereas
3656 // ET_DYN ones do (addr += run-time mmap base address). We tell these apart
3657 // by the incoming section value (".absolute" vs. ".dynamic").
3658 // XXX Assert invariants here too?
3659 }
3660 else
3661 {
3662 // Assert kernel relocation invariants
3663 if (section == "" && dwfl_addr != addr) // addr should be absolute
3664 throw semantic_error ("missing relocation base against", tok);
3665 if (section != "" && dwfl_addr == addr) // addr should be an offset
3666 throw semantic_error ("inconsistent relocation address", tok);
3667 }
2930abc7 3668
21beacc9
FCE
3669 // XXX: hack for strange g++/gcc's
3670#ifndef USHRT_MAX
3671#define USHRT_MAX 32767
3672#endif
3673
606fd9c8 3674 // Range limit maxactive() value
6b66b9f7 3675 if (has_maxactive && (maxactive_val < 0 || maxactive_val > USHRT_MAX))
606fd9c8 3676 throw semantic_error ("maxactive value out of range [0,"
aca66a36 3677 + lex_cast(USHRT_MAX) + "]",
f1a0157a 3678 q.base_loc->components.front()->tok);
606fd9c8 3679
de688825 3680 // Expand target variables in the probe body
5f0a03a6 3681 if (!null_die(scope_die))
8fc05e57 3682 {
6b66b9f7 3683 // XXX: user-space deref's for q.has_process!
de688825 3684 dwarf_var_expanding_visitor v (q, scope_die, dwfl_addr);
8b095b45 3685 v.replace (this->body);
6b66b9f7
JS
3686 if (!q.has_process)
3687 access_vars = v.visited;
37ebca01
FCE
3688
3689 // If during target-variable-expanding the probe, we added a new block
3690 // of code, add it to the start of the probe.
3691 if (v.add_block)
ba6f838d 3692 this->body = new block(v.add_block, this->body);
2260f4e3
FCE
3693
3694 // If when target-variable-expanding the probe, we need to synthesize a
3695 // sibling function-entry probe. We don't go through the whole probe derivation
3696 // business (PR10642) that could lead to wildcard/alias resolution, or for that
3697 // dwarf-induced duplication.
3698 if (v.add_call_probe)
37ebca01 3699 {
2260f4e3
FCE
3700 assert (q.has_return && !q.has_call);
3701
3702 // We temporarily replace q.base_probe.
3703 statement* old_body = q.base_probe->body;
3704 q.base_probe->body = v.add_call_probe;
3705 q.has_return = false;
3706 q.has_call = true;
af234c40 3707
da23eceb 3708 if (q.has_process)
af234c40
JS
3709 entry_handler = new uprobe_derived_probe (funcname, filename, line,
3710 module, section, dwfl_addr,
3711 addr, q, scope_die);
da23eceb 3712 else
af234c40
JS
3713 entry_handler = new dwarf_derived_probe (funcname, filename, line,
3714 module, section, dwfl_addr,
3715 addr, q, scope_die);
3716
3717 saved_longs = entry_handler->saved_longs = v.saved_longs;
3718 saved_strings = entry_handler->saved_strings = v.saved_strings;
3719
3720 q.results.push_back (entry_handler);
2260f4e3
FCE
3721
3722 q.has_return = true;
3723 q.has_call = false;
3724 q.base_probe->body = old_body;
37ebca01 3725 }
f10534c6
WH
3726 // Save the local variables for listing mode
3727 if (q.sess.listing_mode_vars)
3728 saveargs(q, scope_die, v);
8fc05e57 3729 }
37ebca01 3730 // else - null scope_die - $target variables will produce an error during translate phase
8fc05e57 3731
f10534c6 3732 // PR10820: null scope die, local variables aren't accessible, not necessary to invoke saveargs
0a98fd42 3733
5d23847d 3734 // Reset the sole element of the "locations" vector as a
b20febf3
FCE
3735 // "reverse-engineered" form of the incoming (q.base_loc) probe
3736 // point. This allows a user to see what function / file / line
3737 // number any particular match of the wildcards.
2930abc7 3738
a229fcd7 3739 vector<probe_point::component*> comps;
91af0778
FCE
3740 if (q.has_kernel)
3741 comps.push_back (new probe_point::component(TOK_KERNEL));
3742 else if(q.has_module)
3743 comps.push_back (new probe_point::component(TOK_MODULE, new literal_string(module)));
3744 else if(q.has_process)
3745 comps.push_back (new probe_point::component(TOK_PROCESS, new literal_string(module)));
3746 else
3747 assert (0);
b5d77020 3748
db520b00
FCE
3749 string fn_or_stmt;
3750 if (q.has_function_str || q.has_function_num)
3751 fn_or_stmt = "function";
3752 else
3753 fn_or_stmt = "statement";
a229fcd7 3754
b8da0ad1 3755 if (q.has_function_str || q.has_statement_str)
db520b00 3756 {
4cd232e4 3757 string retro_name = funcname;
b20febf3 3758 if (filename != "")
cee35f73 3759 {
fb84c077 3760 retro_name += ("@" + string (filename));
cee35f73 3761 if (line > 0)
aca66a36 3762 retro_name += (":" + lex_cast (line));
cee35f73 3763 }
db520b00
FCE
3764 comps.push_back
3765 (new probe_point::component
3766 (fn_or_stmt, new literal_string (retro_name)));
3767 }
b8da0ad1 3768 else if (q.has_function_num || q.has_statement_num)
db520b00
FCE
3769 {
3770 Dwarf_Addr retro_addr;
3771 if (q.has_function_num)
3772 retro_addr = q.function_num_val;
3773 else
3774 retro_addr = q.statement_num_val;
db520b00
FCE
3775 comps.push_back (new probe_point::component
3776 (fn_or_stmt,
9ea68eb9 3777 new literal_number(retro_addr, true)));
37ebca01
FCE
3778
3779 if (q.has_absolute)
3780 comps.push_back (new probe_point::component (TOK_ABSOLUTE));
a229fcd7
GH
3781 }
3782
b8da0ad1
FCE
3783 if (q.has_call)
3784 comps.push_back (new probe_point::component(TOK_CALL));
3785 if (q.has_inline)
3786 comps.push_back (new probe_point::component(TOK_INLINE));
db520b00 3787 if (has_return)
b8da0ad1
FCE
3788 comps.push_back (new probe_point::component(TOK_RETURN));
3789 if (has_maxactive)
3790 comps.push_back (new probe_point::component
3791 (TOK_MAXACTIVE, new literal_number(maxactive_val)));
d9b516ca 3792
5d23847d
FCE
3793 // Overwrite it.
3794 this->sole_location()->components = comps;
2930abc7
FCE
3795}
3796
bd2b1e68 3797
0a98fd42 3798void
f10534c6 3799dwarf_derived_probe::saveargs(dwarf_query& q, Dwarf_Die* scope_die, dwarf_var_expanding_visitor& v)
0a98fd42 3800{
9aa8ffce 3801 if (null_die(scope_die))
0a98fd42 3802 return;
0a98fd42 3803
0a98fd42 3804 string type_name;
0a98fd42
JS
3805 Dwarf_Die type_die;
3806
3807 if (has_return &&
3d1ad340 3808 dwarf_attr_die (scope_die, DW_AT_type, &type_die) &&
f1c8f8a5 3809 dwarf_type_name(&type_die, type_name))
d0bfd2ac 3810 args.push_back("$return:"+type_name);
0a98fd42 3811
36b155fa 3812 /* Pretend that we aren't in a .return for a moment, just so we can
e29b2f5a 3813 * check whether variables are accessible. We don't want any of the
36b155fa
FCE
3814 * entry-saving code generated during listing mode. This works
3815 * because the set of $context variables available in a .return
3816 * probe (apart from $return) is the same set as available for the
3817 * corresponding .call probe, since we collect those variables at
3818 * .call time. */
d87623a1
JS
3819 bool saved_has_return = has_return;
3820 q.has_return = has_return = false;
3821
0a98fd42 3822 Dwarf_Die arg;
9aa8ffce 3823 vector<Dwarf_Die> scopes = q.dw.getscopes_die(scope_die);
0a98fd42
JS
3824 if (dwarf_child (&scopes[0], &arg) == 0)
3825 do
3826 {
3827 switch (dwarf_tag (&arg))
3828 {
3829 case DW_TAG_variable:
3830 case DW_TAG_formal_parameter:
3831 break;
3832
3833 default:
3834 continue;
3835 }
3836
3837 const char *arg_name = dwarf_diename (&arg);
3838 if (!arg_name)
3839 continue;
3840
3841 type_name.clear();
3d1ad340 3842 if (!dwarf_attr_die (&arg, DW_AT_type, &type_die) ||
f1c8f8a5 3843 !dwarf_type_name(&type_die, type_name))
0a98fd42
JS
3844 continue;
3845
f10534c6
WH
3846 /* trick from visit_target_symbol_context */
3847 target_symbol *tsym = new target_symbol;
f1a0157a 3848 tsym->tok = q.base_loc->components.front()->tok;
277c21bc
JS
3849 tsym->name = "$";
3850 tsym->name += arg_name;
f10534c6
WH
3851
3852 /* Ignore any variable that isn't accessible */
3853 tsym->saved_conversion_error = 0;
3854 v.require (tsym);
3855 if (!tsym->saved_conversion_error)
d0bfd2ac 3856 args.push_back("$"+string(arg_name)+":"+type_name);
0a98fd42
JS
3857 }
3858 while (dwarf_siblingof (&arg, &arg) == 0);
d87623a1
JS
3859
3860 /* restore the .return status of the probe */
3861 q.has_return = has_return = saved_has_return;
0a98fd42
JS
3862}
3863
3864
3865void
d0bfd2ac 3866dwarf_derived_probe::getargs(std::list<std::string> &arg_set) const
0a98fd42 3867{
d0bfd2ac 3868 arg_set.insert(arg_set.end(), args.begin(), args.end());
0a98fd42
JS
3869}
3870
3871
27dc09b1
DB
3872void
3873dwarf_derived_probe::emit_unprivileged_assertion (translator_output* o)
3874{
3875 if (has_process)
3876 {
3877 // These probes are allowed for unprivileged users, but only in the
3878 // context of processes which they own.
3879 emit_process_owner_assertion (o);
3880 return;
3881 }
3882
3883 // Other probes must contain the default assertion which aborts
3884 // if executed by an unprivileged user.
3885 derived_probe::emit_unprivileged_assertion (o);
3886}
3887
3888
3889void
3890dwarf_derived_probe::print_dupe_stamp(ostream& o)
3891{
3892 if (has_process)
3893 {
3894 // These probes are allowed for unprivileged users, but only in the
3895 // context of processes which they own.
3896 print_dupe_stamp_unprivileged_process_owner (o);
3897 return;
3898 }
3899
3900 // Other probes must contain the default dupe stamp
3901 derived_probe::print_dupe_stamp (o);
3902}
3903
64211010 3904
7a053d3b 3905void
20c6c071 3906dwarf_derived_probe::register_statement_variants(match_node * root,
27dc09b1
DB
3907 dwarf_builder * dw,
3908 bool bind_unprivileged_p)
bd2b1e68 3909{
27dc09b1
DB
3910 root
3911 ->bind_unprivileged(bind_unprivileged_p)
3912 ->bind(dw);
54efe513
GH
3913}
3914
7a053d3b 3915void
fd6602a0 3916dwarf_derived_probe::register_function_variants(match_node * root,
27dc09b1
DB
3917 dwarf_builder * dw,
3918 bool bind_unprivileged_p)
2865d17a 3919{
27dc09b1
DB
3920 root
3921 ->bind_unprivileged(bind_unprivileged_p)
3922 ->bind(dw);
3923 root->bind(TOK_INLINE)
3924 ->bind_unprivileged(bind_unprivileged_p)
3925 ->bind(dw);
3926 root->bind(TOK_CALL)
3927 ->bind_unprivileged(bind_unprivileged_p)
3928 ->bind(dw);
3929 root->bind(TOK_RETURN)
3930 ->bind_unprivileged(bind_unprivileged_p)
3931 ->bind(dw);
3932 root->bind(TOK_RETURN)
3933 ->bind_unprivileged(bind_unprivileged_p)
3934 ->bind_num(TOK_MAXACTIVE)->bind(dw);
bd2b1e68
GH
3935}
3936
7a053d3b 3937void
27dc09b1
DB
3938dwarf_derived_probe::register_function_and_statement_variants(
3939 match_node * root,
3940 dwarf_builder * dw,
3941 bool bind_unprivileged_p
3942)
bd2b1e68
GH
3943{
3944 // Here we match 4 forms:
3945 //
3946 // .function("foo")
3947 // .function(0xdeadbeef)
3948 // .statement("foo")
3949 // .statement(0xdeadbeef)
3950
27dc09b1
DB
3951 register_function_variants(root->bind_str(TOK_FUNCTION), dw, bind_unprivileged_p);
3952 register_function_variants(root->bind_num(TOK_FUNCTION), dw, bind_unprivileged_p);
3953 register_statement_variants(root->bind_str(TOK_STATEMENT), dw, bind_unprivileged_p);
3954 register_statement_variants(root->bind_num(TOK_STATEMENT), dw, bind_unprivileged_p);
bd2b1e68
GH
3955}
3956
3957void
c4ce66a1 3958dwarf_derived_probe::register_patterns(systemtap_session& s)
bd2b1e68 3959{
c4ce66a1 3960 match_node* root = s.pattern_root;
bd2b1e68
GH
3961 dwarf_builder *dw = new dwarf_builder();
3962
c4ce66a1
JS
3963 update_visitor *filter = new dwarf_cast_expanding_visitor(s, *dw);
3964 s.code_filters.push_back(filter);
3965
27dc09b1
DB
3966 register_function_and_statement_variants(root->bind(TOK_KERNEL), dw);
3967 register_function_and_statement_variants(root->bind_str(TOK_MODULE), dw);
3968
3969 root->bind(TOK_KERNEL)->bind_num(TOK_STATEMENT)->bind(TOK_ABSOLUTE)
3970 ->bind(dw);
3971 root->bind(TOK_KERNEL)->bind_str(TOK_FUNCTION)->bind_str(TOK_LABEL)
3972 ->bind(dw);
d2c9ec9b 3973
27dc09b1
DB
3974 register_function_and_statement_variants(root->bind_str(TOK_PROCESS), dw,
3975 true/*bind_unprivileged*/);
d2c9ec9b 3976 root->bind_str(TOK_PROCESS)->bind_str(TOK_FUNCTION)->bind_str(TOK_LABEL)
27dc09b1 3977 ->bind_unprivileged()
d2c9ec9b 3978 ->bind(dw);
63b4fd14 3979 root->bind_str(TOK_PROCESS)->bind_str(TOK_LIBRARY)->bind_str(TOK_MARK)
27dc09b1 3980 ->bind_unprivileged()
63b4fd14 3981 ->bind(dw);
a794dbeb
FCE
3982 root->bind_str(TOK_PROCESS)->bind_str(TOK_LIBRARY)->bind_str(TOK_PROVIDER)->bind_str(TOK_MARK)
3983 ->bind_unprivileged()
3984 ->bind(dw);
d2c9ec9b 3985 root->bind_str(TOK_PROCESS)->bind_str(TOK_MARK)
27dc09b1 3986 ->bind_unprivileged()
d2c9ec9b 3987 ->bind(dw);
a794dbeb
FCE
3988 root->bind_str(TOK_PROCESS)->bind_str(TOK_PROVIDER)->bind_str(TOK_MARK)
3989 ->bind_unprivileged()
3990 ->bind(dw);
3991 // XXX what uses this?
d2c9ec9b 3992 root->bind_str(TOK_PROCESS)->bind_num(TOK_MARK)
27dc09b1 3993 ->bind_unprivileged()
d2c9ec9b 3994 ->bind(dw);
bd2b1e68
GH
3995}
3996
9020300d
FCE
3997void
3998dwarf_derived_probe::emit_probe_local_init(translator_output * o)
3999{
b95e2b79
MH
4000 if (access_vars)
4001 {
4002 // if accessing $variables, emit bsp cache setup for speeding up
4003 o->newline() << "bspcache(c->unwaddr, c->regs);";
4004 }
9020300d 4005}
2930abc7 4006
b20febf3 4007// ------------------------------------------------------------------------
46b84a80
DS
4008
4009void
b20febf3 4010dwarf_derived_probe_group::enroll (dwarf_derived_probe* p)
46b84a80 4011{
b20febf3 4012 probes_by_module.insert (make_pair (p->module, p));
b8da0ad1
FCE
4013
4014 // XXX: probes put at the same address should all share a
4015 // single kprobe/kretprobe, and have their handlers executed
4016 // sequentially.
b55bc428
FCE
4017}
4018
7a053d3b 4019void
775d51e5 4020dwarf_derived_probe_group::emit_module_decls (systemtap_session& s)
ec4373ff 4021{
b20febf3 4022 if (probes_by_module.empty()) return;
2930abc7 4023
775d51e5
DS
4024 s.op->newline() << "/* ---- dwarf probes ---- */";
4025
4026 // Warn of misconfigured kernels
f41595cc
FCE
4027 s.op->newline() << "#if ! defined(CONFIG_KPROBES)";
4028 s.op->newline() << "#error \"Need CONFIG_KPROBES!\"";
4029 s.op->newline() << "#endif";
775d51e5 4030 s.op->newline();
f41595cc 4031
f07c3b68 4032 s.op->newline() << "#ifndef KRETACTIVE";
1ee6b5fc 4033 s.op->newline() << "#define KRETACTIVE (max(15,6*(int)num_possible_cpus()))";
f07c3b68
FCE
4034 s.op->newline() << "#endif";
4035
14cf7e42
SC
4036 // Forward decls
4037 s.op->newline() << "#include \"kprobes-common.h\"";
4038
b20febf3
FCE
4039 // Forward declare the master entry functions
4040 s.op->newline() << "static int enter_kprobe_probe (struct kprobe *inst,";
4041 s.op->line() << " struct pt_regs *regs);";
4042 s.op->newline() << "static int enter_kretprobe_probe (struct kretprobe_instance *inst,";
4043 s.op->line() << " struct pt_regs *regs);";
4044
42cb22bd
MH
4045 // Emit an array of kprobe/kretprobe pointers
4046 s.op->newline() << "#if defined(STAPCONF_UNREGISTER_KPROBES)";
4047 s.op->newline() << "static void * stap_unreg_kprobes[" << probes_by_module.size() << "];";
4048 s.op->newline() << "#endif";
4049
b20febf3 4050 // Emit the actual probe list.
606fd9c8
FCE
4051
4052 // NB: we used to plop a union { struct kprobe; struct kretprobe } into
4053 // struct stap_dwarf_probe, but it being initialized data makes it add
4054 // hundreds of bytes of padding per stap_dwarf_probe. (PR5673)
14cf7e42 4055 s.op->newline() << "static struct stap_dwarf_kprobe stap_dwarf_kprobes[" << probes_by_module.size() << "];";
606fd9c8
FCE
4056 // NB: bss!
4057
4c2732a1 4058 s.op->newline() << "static struct stap_dwarf_probe {";
b0986e7a
DS
4059 s.op->newline(1) << "const unsigned return_p:1;";
4060 s.op->newline() << "const unsigned maxactive_p:1;";
b350f56b 4061 s.op->newline() << "const unsigned optional_p:1;";
b20febf3 4062 s.op->newline() << "unsigned registered_p:1;";
b0986e7a 4063 s.op->newline() << "const unsigned short maxactive_val;";
606fd9c8 4064
af234c40
JS
4065 // data saved in the kretprobe_instance packet
4066 s.op->newline() << "const unsigned short saved_longs;";
4067 s.op->newline() << "const unsigned short saved_strings;";
4068
faea5e16 4069 // Let's find some stats for the embedded strings. Maybe they
606fd9c8
FCE
4070 // are small and uniform enough to justify putting char[MAX]'s into
4071 // the array instead of relocated char*'s.
faea5e16
JS
4072 size_t module_name_max = 0, section_name_max = 0;
4073 size_t module_name_tot = 0, section_name_tot = 0;
606fd9c8
FCE
4074 size_t all_name_cnt = probes_by_module.size(); // for average
4075 for (p_b_m_iterator it = probes_by_module.begin(); it != probes_by_module.end(); it++)
4076 {
4077 dwarf_derived_probe* p = it->second;
4078#define DOIT(var,expr) do { \
4079 size_t var##_size = (expr) + 1; \
4080 var##_max = max (var##_max, var##_size); \
4081 var##_tot += var##_size; } while (0)
4082 DOIT(module_name, p->module.size());
4083 DOIT(section_name, p->section.size());
606fd9c8
FCE
4084#undef DOIT
4085 }
4086
4087 // Decide whether it's worthwhile to use char[] or char* by comparing
4088 // the amount of average waste (max - avg) to the relocation data size
4089 // (3 native long words).
4090#define CALCIT(var) \
4091 if ((var##_name_max-(var##_name_tot/all_name_cnt)) < (3 * sizeof(void*))) \
4092 { \
4093 s.op->newline() << "const char " << #var << "[" << var##_name_max << "];"; \
4094 if (s.verbose > 2) clog << "stap_dwarf_probe " << #var \
4095 << "[" << var##_name_max << "]" << endl; \
4096 } \
4097 else \
4098 { \
b0986e7a 4099 s.op->newline() << "const char * const " << #var << ";"; \
606fd9c8
FCE
4100 if (s.verbose > 2) clog << "stap_dwarf_probe *" << #var << endl; \
4101 }
4102
4103 CALCIT(module);
4104 CALCIT(section);
e6fe60e7 4105#undef CALCIT
606fd9c8 4106
b0986e7a 4107 s.op->newline() << "const unsigned long address;";
faea5e16 4108 s.op->newline() << "struct stap_probe probe;";
af234c40 4109 s.op->newline() << "void (* const entry_ph) (struct context*);";
b642c901
SC
4110 s.op->newline() << "const unsigned long sdt_sem_offset;";
4111 s.op->newline() << "unsigned long sdt_sem_address;";
4112 s.op->newline() << "struct task_struct *tsk;";
4113 s.op->newline() << "const char *pathname;";
4114 s.op->newline() << "struct stap_task_finder_target finder;";
b20febf3
FCE
4115 s.op->newline(-1) << "} stap_dwarf_probes[] = {";
4116 s.op->indent(1);
4117
4118 for (p_b_m_iterator it = probes_by_module.begin(); it != probes_by_module.end(); it++)
2930abc7 4119 {
b20febf3
FCE
4120 dwarf_derived_probe* p = it->second;
4121 s.op->newline() << "{";
4122 if (p->has_return)
4123 s.op->line() << " .return_p=1,";
c9bad430 4124 if (p->has_maxactive)
606fd9c8
FCE
4125 {
4126 s.op->line() << " .maxactive_p=1,";
4127 assert (p->maxactive_val >= 0 && p->maxactive_val <= USHRT_MAX);
4128 s.op->line() << " .maxactive_val=" << p->maxactive_val << ",";
4129 }
af234c40
JS
4130 if (p->saved_longs || p->saved_strings)
4131 {
4132 if (p->saved_longs)
4133 s.op->line() << " .saved_longs=" << p->saved_longs << ",";
4134 if (p->saved_strings)
4135 s.op->line() << " .saved_strings=" << p->saved_strings << ",";
4136 if (p->entry_handler)
4137 s.op->line() << " .entry_ph=&" << p->entry_handler->name << ",";
4138 }
b350f56b
JS
4139 if (p->locations[0]->optional)
4140 s.op->line() << " .optional_p=1,";
dc38c256 4141 s.op->line() << " .address=(unsigned long)0x" << hex << p->addr << dec << "ULL,";
84048984
FCE
4142 s.op->line() << " .module=\"" << p->module << "\",";
4143 s.op->line() << " .section=\"" << p->section << "\",";
faea5e16 4144 s.op->line() << " .probe=" << common_probe_init (p) << ",";
b642c901
SC
4145 if (p->sdt_semaphore_addr != 0)
4146 {
4147 s.op->line() << " .sdt_sem_offset=(unsigned long)0x" << hex << p->sdt_semaphore_addr << dec << "ULL,";
4148 s.op->line() << " .sdt_sem_address=0,";
4149 if (p->has_library)
4150 {
4151 s.op->line() << " .finder={";
4152 s.op->line() << " .pid=0,";
4153 s.op->line() << " .procname=\"" << p->user_path << "\",";
4154 s.op->line() << " .mmap_callback=&stap_kprobe_mmap_found,";
4155 s.op->line() << " },";
4156 s.op->line() << " .pathname=\"" << p->user_lib << "\",";
4157 }
4158 else
4159 {
4160 s.op->line() << " .finder={";
4161 s.op->line() << " .pid=0,";
4162 s.op->line() << " .procname=\"" << p->user_path << "\",";
4163 s.op->line() << " .callback=&stap_kprobe_process_found,";
4164 s.op->line() << " },";
4165 s.op->line() << " .pathname=\"" << p->user_path << "\",";
4166 }
4167
4168 }
4169 else
4170 s.op->line() << " .sdt_sem_offset=0,";
b642c901 4171
b20febf3 4172 s.op->line() << " },";
2930abc7 4173 }
2930abc7 4174
b20febf3
FCE
4175 s.op->newline(-1) << "};";
4176
4177 // Emit the kprobes callback function
4178 s.op->newline();
4179 s.op->newline() << "static int enter_kprobe_probe (struct kprobe *inst,";
4180 s.op->line() << " struct pt_regs *regs) {";
606fd9c8
FCE
4181 // NB: as of PR5673, the kprobe|kretprobe union struct is in BSS
4182 s.op->newline(1) << "int kprobe_idx = ((uintptr_t)inst-(uintptr_t)stap_dwarf_kprobes)/sizeof(struct stap_dwarf_kprobe);";
4183 // Check that the index is plausible
4184 s.op->newline() << "struct stap_dwarf_probe *sdp = &stap_dwarf_probes[";
4185 s.op->line() << "((kprobe_idx >= 0 && kprobe_idx < " << probes_by_module.size() << ")?";
4186 s.op->line() << "kprobe_idx:0)"; // NB: at least we avoid memory corruption
4187 // XXX: it would be nice to give a more verbose error though; BUG_ON later?
4188 s.op->line() << "];";
faea5e16 4189 common_probe_entryfn_prologue (s.op, "STAP_SESSION_RUNNING", "sdp->probe");
b20febf3 4190 s.op->newline() << "c->regs = regs;";
6415ddde
MW
4191
4192 // Make it look like the IP is set as it wouldn't have been replaced
4193 // by a breakpoint instruction when calling real probe handler. Reset
4194 // IP regs on return, so we don't confuse kprobes. PR10458
4195 s.op->newline() << "{";
4196 s.op->indent(1);
4197 s.op->newline() << "unsigned long kprobes_ip = REG_IP(c->regs);";
259d54c0 4198 s.op->newline() << "SET_REG_IP(regs, (unsigned long) inst->addr);";
faea5e16 4199 s.op->newline() << "(*sdp->probe.ph) (c);";
259d54c0 4200 s.op->newline() << "SET_REG_IP(regs, kprobes_ip);";
6415ddde
MW
4201 s.op->newline(-1) << "}";
4202
b20febf3
FCE
4203 common_probe_entryfn_epilogue (s.op);
4204 s.op->newline() << "return 0;";
4205 s.op->newline(-1) << "}";
4206
4207 // Same for kretprobes
4208 s.op->newline();
af234c40
JS
4209 s.op->newline() << "static int enter_kretprobe_common (struct kretprobe_instance *inst,";
4210 s.op->line() << " struct pt_regs *regs, int entry) {";
b20febf3 4211 s.op->newline(1) << "struct kretprobe *krp = inst->rp;";
606fd9c8
FCE
4212
4213 // NB: as of PR5673, the kprobe|kretprobe union struct is in BSS
a36378d7 4214 s.op->newline() << "int kprobe_idx = ((uintptr_t)krp-(uintptr_t)stap_dwarf_kprobes)/sizeof(struct stap_dwarf_kprobe);";
606fd9c8
FCE
4215 // Check that the index is plausible
4216 s.op->newline() << "struct stap_dwarf_probe *sdp = &stap_dwarf_probes[";
4217 s.op->line() << "((kprobe_idx >= 0 && kprobe_idx < " << probes_by_module.size() << ")?";
4218 s.op->line() << "kprobe_idx:0)"; // NB: at least we avoid memory corruption
4219 // XXX: it would be nice to give a more verbose error though; BUG_ON later?
4220 s.op->line() << "];";
4221
faea5e16 4222 common_probe_entryfn_prologue (s.op, "STAP_SESSION_RUNNING", "sdp->probe");
b20febf3 4223 s.op->newline() << "c->regs = regs;";
af234c40
JS
4224
4225 // for assisting runtime's backtrace logic and accessing kretprobe data packets
4226 s.op->newline() << "c->pi = inst;";
4227 s.op->newline() << "c->pi_longs = sdp->saved_longs;";
6415ddde
MW
4228
4229 // Make it look like the IP is set as it wouldn't have been replaced
4230 // by a breakpoint instruction when calling real probe handler. Reset
4231 // IP regs on return, so we don't confuse kprobes. PR10458
4232 s.op->newline() << "{";
4233 s.op->indent(1);
4234 s.op->newline() << "unsigned long kprobes_ip = REG_IP(c->regs);";
5e562a69
TM
4235 s.op->newline() << "if (entry) {";
4236 s.op->indent(1);
259d54c0 4237 s.op->newline() << "SET_REG_IP(regs, (unsigned long) inst->rp->kp.addr);";
5e562a69
TM
4238 s.op->newline() << "(sdp->entry_ph) (c);";
4239 s.op->newline(-1) << "} else {";
4240 s.op->indent(1);
4241 s.op->newline() << "SET_REG_IP(regs, (unsigned long)inst->ret_addr);";
faea5e16 4242 s.op->newline() << "(sdp->probe.ph) (c);";
5e562a69 4243 s.op->newline(-1) << "}";
259d54c0 4244 s.op->newline() << "SET_REG_IP(regs, kprobes_ip);";
6415ddde
MW
4245 s.op->newline(-1) << "}";
4246
b20febf3
FCE
4247 common_probe_entryfn_epilogue (s.op);
4248 s.op->newline() << "return 0;";
4249 s.op->newline(-1) << "}";
af234c40
JS
4250
4251 s.op->newline();
4252 s.op->newline() << "static int enter_kretprobe_probe (struct kretprobe_instance *inst,";
4253 s.op->line() << " struct pt_regs *regs) {";
4254 s.op->newline(1) << "return enter_kretprobe_common(inst, regs, 0);";
4255 s.op->newline(-1) << "}";
4256
4257 s.op->newline();
4258 s.op->newline() << "static int enter_kretprobe_entry_probe (struct kretprobe_instance *inst,";
4259 s.op->line() << " struct pt_regs *regs) {";
4260 s.op->newline(1) << "return enter_kretprobe_common(inst, regs, 1);";
4261 s.op->newline(-1) << "}";
b642c901 4262
14cf7e42
SC
4263 s.op->newline();
4264 s.op->newline() << "#include \"kprobes-common.c\"";
4265 s.op->newline();
20c6c071 4266}
ec4373ff 4267
20c6c071 4268
dc38c0ae 4269void
b20febf3
FCE
4270dwarf_derived_probe_group::emit_module_init (systemtap_session& s)
4271{
b642c901
SC
4272 p_b_m_iterator it;
4273 for (it = probes_by_module.begin(); it != probes_by_module.end(); it++)
4274 {
4275 dwarf_derived_probe* p = it->second;
4276 if (p->sdt_semaphore_addr != 0)
4277 break;
4278 }
4279 if (it != probes_by_module.end()) // Ignore if there are no semaphores
4280 {
4281 s.op->newline() << "for (i=0; i<ARRAY_SIZE(stap_dwarf_probes); i++) {";
4282 s.op->newline(1) << "int rc;";
4283 s.op->newline() << "struct stap_dwarf_probe *p = &stap_dwarf_probes[i];";
faea5e16 4284 s.op->newline() << "probe_point = p->probe.pp;"; // for error messages
b642c901
SC
4285 s.op->newline() << "if (p->sdt_sem_offset) {";
4286 s.op->newline(1) << "rc = stap_register_task_finder_target(&p->finder);";
4287 s.op->newline(-1) << "}";
4288 s.op->newline() << "if (rc) break;";
4289 s.op->newline(-1) << "}";
4290 }
4291
b20febf3
FCE
4292 s.op->newline() << "for (i=0; i<" << probes_by_module.size() << "; i++) {";
4293 s.op->newline(1) << "struct stap_dwarf_probe *sdp = & stap_dwarf_probes[i];";
a36378d7 4294 s.op->newline() << "struct stap_dwarf_kprobe *kp = & stap_dwarf_kprobes[i];";
a049e342 4295 s.op->newline() << "unsigned long relocated_addr = _stp_kmodule_relocate (sdp->module, sdp->section, sdp->address);";
b20febf3 4296 s.op->newline() << "if (relocated_addr == 0) continue;"; // quietly; assume module is absent
faea5e16 4297 s.op->newline() << "probe_point = sdp->probe.pp;"; // for error messages
b20febf3 4298 s.op->newline() << "if (sdp->return_p) {";
606fd9c8 4299 s.op->newline(1) << "kp->u.krp.kp.addr = (void *) relocated_addr;";
c9bad430 4300 s.op->newline() << "if (sdp->maxactive_p) {";
606fd9c8 4301 s.op->newline(1) << "kp->u.krp.maxactive = sdp->maxactive_val;";
c9bad430 4302 s.op->newline(-1) << "} else {";
f07c3b68 4303 s.op->newline(1) << "kp->u.krp.maxactive = KRETACTIVE;";
c9bad430 4304 s.op->newline(-1) << "}";
606fd9c8 4305 s.op->newline() << "kp->u.krp.handler = &enter_kretprobe_probe;";
af234c40
JS
4306 s.op->newline() << "#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,25)";
4307 s.op->newline() << "if (sdp->entry_ph) {";
4308 s.op->newline(1) << "kp->u.krp.entry_handler = &enter_kretprobe_entry_probe;";
4309 s.op->newline() << "kp->u.krp.data_size = sdp->saved_longs * sizeof(int64_t) + ";
4310 s.op->newline() << " sdp->saved_strings * MAXSTRINGLEN;";
4311 s.op->newline(-1) << "}";
4312 s.op->newline() << "#endif";
e4cb375f
MH
4313 // to ensure safeness of bspcache, always use aggr_kprobe on ia64
4314 s.op->newline() << "#ifdef __ia64__";
4315 s.op->newline() << "kp->dummy.addr = kp->u.krp.kp.addr;";
4316 s.op->newline() << "kp->dummy.pre_handler = NULL;";
4317 s.op->newline() << "rc = register_kprobe (& kp->dummy);";
4318 s.op->newline() << "if (rc == 0) {";
4319 s.op->newline(1) << "rc = register_kretprobe (& kp->u.krp);";
4320 s.op->newline() << "if (rc != 0)";
4321 s.op->newline(1) << "unregister_kprobe (& kp->dummy);";
4322 s.op->newline(-2) << "}";
4323 s.op->newline() << "#else";
606fd9c8 4324 s.op->newline() << "rc = register_kretprobe (& kp->u.krp);";
e4cb375f 4325 s.op->newline() << "#endif";
b20febf3 4326 s.op->newline(-1) << "} else {";
e4cb375f 4327 // to ensure safeness of bspcache, always use aggr_kprobe on ia64
606fd9c8
FCE
4328 s.op->newline(1) << "kp->u.kp.addr = (void *) relocated_addr;";
4329 s.op->newline() << "kp->u.kp.pre_handler = &enter_kprobe_probe;";
e4cb375f
MH
4330 s.op->newline() << "#ifdef __ia64__";
4331 s.op->newline() << "kp->dummy.addr = kp->u.kp.addr;";
4332 s.op->newline() << "kp->dummy.pre_handler = NULL;";
4333 s.op->newline() << "rc = register_kprobe (& kp->dummy);";
4334 s.op->newline() << "if (rc == 0) {";
4335 s.op->newline(1) << "rc = register_kprobe (& kp->u.kp);";
4336 s.op->newline() << "if (rc != 0)";
4337 s.op->newline(1) << "unregister_kprobe (& kp->dummy);";
4338 s.op->newline(-2) << "}";
4339 s.op->newline() << "#else";
606fd9c8 4340 s.op->newline() << "rc = register_kprobe (& kp->u.kp);";
e4cb375f 4341 s.op->newline() << "#endif";
b20febf3 4342 s.op->newline(-1) << "}";
9063462a
FCE
4343 s.op->newline() << "if (rc) {"; // PR6749: tolerate a failed register_*probe.
4344 s.op->newline(1) << "sdp->registered_p = 0;";
b350f56b 4345 s.op->newline() << "if (!sdp->optional_p)";
50b6acf7 4346 s.op->newline(1) << "_stp_warn (\"probe %s (address 0x%lx) registration error (rc %d)\", probe_point, (unsigned long) relocated_addr, rc);";
b350f56b 4347 s.op->newline(-1) << "rc = 0;"; // continue with other probes
9063462a
FCE
4348 // XXX: shall we increment numskipped?
4349 s.op->newline(-1) << "}";
4350
4351#if 0 /* pre PR 6749; XXX consider making an option */
c48cb0cc 4352 s.op->newline(1) << "for (j=i-1; j>=0; j--) {"; // partial rollback
b20febf3 4353 s.op->newline(1) << "struct stap_dwarf_probe *sdp2 = & stap_dwarf_probes[j];";
606fd9c8
FCE
4354 s.op->newline() << "struct stap_dwarf_kprobe *kp2 = & stap_dwarf_kprobes[j];";
4355 s.op->newline() << "if (sdp2->return_p) unregister_kretprobe (&kp2->u.krp);";
4356 s.op->newline() << "else unregister_kprobe (&kp2->u.kp);";
e4cb375f
MH
4357 s.op->newline() << "#ifdef __ia64__";
4358 s.op->newline() << "unregister_kprobe (&kp2->dummy);";
4359 s.op->newline() << "#endif";
c48cb0cc
FCE
4360 // NB: we don't have to clear sdp2->registered_p, since the module_exit code is
4361 // not run for this early-abort case.
4362 s.op->newline(-1) << "}";
4363 s.op->newline() << "break;"; // don't attempt to register any more probes
b20febf3 4364 s.op->newline(-1) << "}";
9063462a
FCE
4365#endif
4366
b20febf3
FCE
4367 s.op->newline() << "else sdp->registered_p = 1;";
4368 s.op->newline(-1) << "}"; // for loop
dc38c0ae
DS
4369}
4370
4371
46b84a80 4372void
b20febf3 4373dwarf_derived_probe_group::emit_module_exit (systemtap_session& s)
46b84a80 4374{
b642c901
SC
4375 s.op->newline() << "for (i=0; i<" << probes_by_module.size() << "; i++) {";
4376 s.op->newline(1) << "struct stap_dwarf_probe *sdp = & stap_dwarf_probes[i];";
4377 s.op->newline() << "unsigned short sdt_semaphore = 0;"; // NB: fixed size
4378 s.op->newline() << "if (sdp->sdt_sem_address && __access_process_vm_noflush (sdp->tsk, sdp->sdt_sem_address, &sdt_semaphore, sizeof (sdt_semaphore), 0)) {";
4379 s.op->newline(1) << "sdt_semaphore --;";
4380 s.op->newline() << "__access_process_vm_noflush (sdp->tsk, sdp->sdt_sem_address, &sdt_semaphore, sizeof (sdt_semaphore), 1);";
4381 s.op->newline(-1) << "}";
4382 s.op->newline(-1) << "}";
4383
42cb22bd
MH
4384 //Unregister kprobes by batch interfaces.
4385 s.op->newline() << "#if defined(STAPCONF_UNREGISTER_KPROBES)";
4386 s.op->newline() << "j = 0;";
4387 s.op->newline() << "for (i=0; i<" << probes_by_module.size() << "; i++) {";
4388 s.op->newline(1) << "struct stap_dwarf_probe *sdp = & stap_dwarf_probes[i];";
4389 s.op->newline() << "struct stap_dwarf_kprobe *kp = & stap_dwarf_kprobes[i];";
4390 s.op->newline() << "if (! sdp->registered_p) continue;";
4391 s.op->newline() << "if (!sdp->return_p)";
4392 s.op->newline(1) << "stap_unreg_kprobes[j++] = &kp->u.kp;";
4393 s.op->newline(-2) << "}";
4394 s.op->newline() << "unregister_kprobes((struct kprobe **)stap_unreg_kprobes, j);";
4395 s.op->newline() << "j = 0;";
4396 s.op->newline() << "for (i=0; i<" << probes_by_module.size() << "; i++) {";
4397 s.op->newline(1) << "struct stap_dwarf_probe *sdp = & stap_dwarf_probes[i];";
4398 s.op->newline() << "struct stap_dwarf_kprobe *kp = & stap_dwarf_kprobes[i];";
4399 s.op->newline() << "if (! sdp->registered_p) continue;";
4400 s.op->newline() << "if (sdp->return_p)";
4401 s.op->newline(1) << "stap_unreg_kprobes[j++] = &kp->u.krp;";
4402 s.op->newline(-2) << "}";
4403 s.op->newline() << "unregister_kretprobes((struct kretprobe **)stap_unreg_kprobes, j);";
e4cb375f
MH
4404 s.op->newline() << "#ifdef __ia64__";
4405 s.op->newline() << "j = 0;";
4406 s.op->newline() << "for (i=0; i<" << probes_by_module.size() << "; i++) {";
4407 s.op->newline(1) << "struct stap_dwarf_probe *sdp = & stap_dwarf_probes[i];";
4408 s.op->newline() << "struct stap_dwarf_kprobe *kp = & stap_dwarf_kprobes[i];";
4409 s.op->newline() << "if (! sdp->registered_p) continue;";
4410 s.op->newline() << "stap_unreg_kprobes[j++] = &kp->dummy;";
4411 s.op->newline(-1) << "}";
4412 s.op->newline() << "unregister_kprobes((struct kprobe **)stap_unreg_kprobes, j);";
4413 s.op->newline() << "#endif";
42cb22bd
MH
4414 s.op->newline() << "#endif";
4415
b20febf3
FCE
4416 s.op->newline() << "for (i=0; i<" << probes_by_module.size() << "; i++) {";
4417 s.op->newline(1) << "struct stap_dwarf_probe *sdp = & stap_dwarf_probes[i];";
a36378d7 4418 s.op->newline() << "struct stap_dwarf_kprobe *kp = & stap_dwarf_kprobes[i];";
b20febf3
FCE
4419 s.op->newline() << "if (! sdp->registered_p) continue;";
4420 s.op->newline() << "if (sdp->return_p) {";
42cb22bd 4421 s.op->newline() << "#if !defined(STAPCONF_UNREGISTER_KPROBES)";
606fd9c8 4422 s.op->newline(1) << "unregister_kretprobe (&kp->u.krp);";
42cb22bd 4423 s.op->newline() << "#endif";
606fd9c8 4424 s.op->newline() << "atomic_add (kp->u.krp.nmissed, & skipped_count);";
73209876
FCE
4425 s.op->newline() << "#ifdef STP_TIMING";
4426 s.op->newline() << "if (kp->u.krp.nmissed)";
faea5e16 4427 s.op->newline(1) << "_stp_warn (\"Skipped due to missed kretprobe/1 on '%s': %d\\n\", sdp->probe.pp, kp->u.krp.nmissed);";
73209876 4428 s.op->newline(-1) << "#endif";
606fd9c8 4429 s.op->newline() << "atomic_add (kp->u.krp.kp.nmissed, & skipped_count);";
73209876
FCE
4430 s.op->newline() << "#ifdef STP_TIMING";
4431 s.op->newline() << "if (kp->u.krp.kp.nmissed)";
faea5e16 4432 s.op->newline(1) << "_stp_warn (\"Skipped due to missed kretprobe/2 on '%s': %lu\\n\", sdp->probe.pp, kp->u.krp.kp.nmissed);";
73209876 4433 s.op->newline(-1) << "#endif";
557fb7a8 4434 s.op->newline(-1) << "} else {";
42cb22bd 4435 s.op->newline() << "#if !defined(STAPCONF_UNREGISTER_KPROBES)";
606fd9c8 4436 s.op->newline(1) << "unregister_kprobe (&kp->u.kp);";
42cb22bd 4437 s.op->newline() << "#endif";
606fd9c8 4438 s.op->newline() << "atomic_add (kp->u.kp.nmissed, & skipped_count);";
73209876
FCE
4439 s.op->newline() << "#ifdef STP_TIMING";
4440 s.op->newline() << "if (kp->u.kp.nmissed)";
faea5e16 4441 s.op->newline(1) << "_stp_warn (\"Skipped due to missed kprobe on '%s': %lu\\n\", sdp->probe.pp, kp->u.kp.nmissed);";
73209876 4442 s.op->newline(-1) << "#endif";
b20febf3 4443 s.op->newline(-1) << "}";
e4cb375f
MH
4444 s.op->newline() << "#if !defined(STAPCONF_UNREGISTER_KPROBES) && defined(__ia64__)";
4445 s.op->newline() << "unregister_kprobe (&kp->dummy);";
4446 s.op->newline() << "#endif";
b20febf3
FCE
4447 s.op->newline() << "sdp->registered_p = 0;";
4448 s.op->newline(-1) << "}";
46b84a80
DS
4449}
4450
aff5d390 4451struct sdt_uprobe_var_expanding_visitor: public var_expanding_visitor
7a05f484 4452{
f83336a5
FCE
4453 sdt_uprobe_var_expanding_visitor(systemtap_session& s,
4454 int elf_machine,
4455 const string & process_name,
a794dbeb 4456 const string & provider_name,
aff5d390
SC
4457 const string & probe_name,
4458 const string & arg_string,
4459 int arg_count):
f83336a5 4460 session (s), process_name (process_name),
a794dbeb 4461 provider_name (provider_name), probe_name (probe_name), arg_count (arg_count)
a8ec7719 4462 {
f83336a5
FCE
4463 /* Register name mapping table depends on the elf machine of this particular
4464 probe target process/file, not upon the host. So we can't just
4465 #ifdef _i686_ etc. */
4466 if (elf_machine == EM_X86_64) {
4467 dwarf_regs["rax"] = dwarf_regs["eax"] = dwarf_regs["ax"] = dwarf_regs["al"] = 0;
4468 dwarf_regs["rdx"] = dwarf_regs["edx"] = dwarf_regs["dx"] = dwarf_regs["dl"] = 1;
4469 dwarf_regs["rcx"] = dwarf_regs["ecx"] = dwarf_regs["cx"] = dwarf_regs["cl"] = 2;
4470 dwarf_regs["rbx"] = dwarf_regs["ebx"] = dwarf_regs["bx"] = dwarf_regs["bl"] = 3;
4471 dwarf_regs["rsi"] = dwarf_regs["esi"] = dwarf_regs["si"] = dwarf_regs["sil"] = 4;
4472 dwarf_regs["rdi"] = dwarf_regs["edi"] = dwarf_regs["di"] = dwarf_regs["dil"] = 5;
4473 dwarf_regs["rbp"] = dwarf_regs["ebp"] = dwarf_regs["bp"] = 6;
4474 dwarf_regs["rsp"] = dwarf_regs["esp"] = dwarf_regs["sp"] = 7;
4475 dwarf_regs["r8"] = dwarf_regs["r8d"] = dwarf_regs["r8w"] = dwarf_regs["r8b"] = 8;
4476 dwarf_regs["r9"] = dwarf_regs["r9d"] = dwarf_regs["r9w"] = dwarf_regs["r9b"] = 9;
4477 dwarf_regs["r10"] = dwarf_regs["r10d"] = dwarf_regs["r10w"] = dwarf_regs["r10b"] = 10;
4478 dwarf_regs["r11"] = dwarf_regs["r11d"] = dwarf_regs["r11w"] = dwarf_regs["r11b"] = 11;
4479 dwarf_regs["r12"] = dwarf_regs["r12d"] = dwarf_regs["r12w"] = dwarf_regs["r12b"] = 12;
4480 dwarf_regs["r13"] = dwarf_regs["r13d"] = dwarf_regs["r13w"] = dwarf_regs["r13b"] = 13;
4481 dwarf_regs["r14"] = dwarf_regs["r14d"] = dwarf_regs["r14w"] = dwarf_regs["r14b"] = 14;
4482 dwarf_regs["r15"] = dwarf_regs["r15d"] = dwarf_regs["r15w"] = dwarf_regs["r15b"] = 15;
4483 } else if (elf_machine == EM_386) {
4484 dwarf_regs["eax"] = dwarf_regs["ax"] = dwarf_regs["al"] = 0;
4485 dwarf_regs["ecx"] = dwarf_regs["cx"] = dwarf_regs["cl"] = 1;
4486 dwarf_regs["edx"] = dwarf_regs["dx"] = dwarf_regs["dl"] = 2;
4487 dwarf_regs["ebx"] = dwarf_regs["bx"] = dwarf_regs["bl"] = 3;
4488 dwarf_regs["esp"] = dwarf_regs["sp"] = 4;
4489 dwarf_regs["ebp"] = dwarf_regs["bp"] = 5;
4490 dwarf_regs["esi"] = dwarf_regs["si"] = dwarf_regs["sil"] = 6;
4491 dwarf_regs["edi"] = dwarf_regs["di"] = dwarf_regs["dil"] = 7;
c8bc2044 4492 } else if (arg_count) {
f83336a5
FCE
4493 throw semantic_error (string("Unsupported architecture ")
4494 + "(" + process_name + " ELF code " + lex_cast(elf_machine) + ")"
4495 + "for dwarfless sdt probes.");
4496 }
4497
aff5d390
SC
4498
4499 tokenize(arg_string, arg_tokens, " ");
4500 assert(arg_count >= 0 && arg_count <= 10);
a8ec7719 4501 }
f83336a5 4502 systemtap_session& session;
aff5d390 4503 const string & process_name;
a794dbeb 4504 const string & provider_name;
aff5d390 4505 const string & probe_name;
7a05f484 4506 int arg_count;
aff5d390
SC
4507 vector<string> arg_tokens;
4508 map<string,int> dwarf_regs;
4509
4510 void visit_target_symbol (target_symbol* e);
aff5d390
SC
4511};
4512
4513struct sdt_kprobe_var_expanding_visitor: public var_expanding_visitor
4514{
4515 sdt_kprobe_var_expanding_visitor(const string & process_name,
a794dbeb 4516 const string & provider_name,
aff5d390
SC
4517 const string & probe_name,
4518 const string & arg_string,
4519 int arg_count):
a794dbeb 4520 process_name (process_name), provider_name (provider_name), probe_name (probe_name),
aff5d390
SC
4521 arg_count (arg_count)
4522 {
4523 tokenize(arg_string, arg_tokens, " ");
4524 assert(arg_count >= 0 && arg_count <= 10);
4525 }
4526 const string & process_name;
a794dbeb 4527 const string & provider_name;
aff5d390
SC
4528 const string & probe_name;
4529 int arg_count;
4530 vector<string> arg_tokens;
7a05f484
SC
4531
4532 void visit_target_symbol (target_symbol* e);
4533};
4534
4535void
aff5d390 4536sdt_uprobe_var_expanding_visitor::visit_target_symbol (target_symbol *e)
7a05f484 4537{
63ea4244 4538 try
7a05f484 4539 {
277c21bc 4540 if (e->name == "$$name")
aff5d390
SC
4541 {
4542 if (e->addressof)
4543 throw semantic_error("cannot take address of sdt context variable", e->tok);
7a05f484 4544
aff5d390
SC
4545 literal_string *myname = new literal_string (probe_name);
4546 myname->tok = e->tok;
4547 provide(myname);
4548 return;
4549 }
277c21bc 4550 if (e->name == "$$provider")
a794dbeb
FCE
4551 {
4552 if (e->addressof)
4553 throw semantic_error("cannot take address of sdt context variable", e->tok);
4554
4555 literal_string *myname = new literal_string (provider_name);
4556 myname->tok = e->tok;
4557 provide(myname);
4558 return;
4559 }
aff5d390
SC
4560 if (arg_count == 0)
4561 {
5ecaa5a7
JS
4562 // NB: Either
4563 // 1) uprobe1_type $argN or $FOO (we don't know the arg_count)
4564 // 2) uprobe2_type $FOO (no probe args)
4565 // both of which get resolved later.
4566 provide(e);
4567 return;
aff5d390 4568 }
63ea4244 4569
5ecaa5a7 4570 int argno = -1;
c69a87e0 4571 try
aff5d390 4572 {
5ecaa5a7
JS
4573 if (startswith(e->name, "$arg"))
4574 argno = lex_cast<int>(e->name.substr(4));
aff5d390 4575 }
c69a87e0 4576 catch (const runtime_error& f) // non-integral $arg suffix: e.g. $argKKKSDF
aff5d390 4577 {
aff5d390 4578 }
5ecaa5a7
JS
4579 if (argno < 0)
4580 throw semantic_error("invalid variable, must be of the form $argN", e->tok);
c69a87e0 4581 if (argno < 1 || argno > arg_count)
aff5d390 4582 throw semantic_error("invalid argument number", e->tok);
5ecaa5a7 4583
305a3c44
SC
4584 enum arg_type
4585 {
4586 literal_arg,
4587 register_arg,
4588 memory_arg
4589 } arg_type;
aff5d390
SC
4590 functioncall *fc = new functioncall;
4591 binary_expression *be = new binary_expression;
fc7dfc08 4592 literal_number* ln = NULL;
aff5d390
SC
4593 string reg;
4594 string disp_str;
4595 int disp = 0;
4596 // NB: uprobes-based sdt.h; $argFOO gets resolved later.
4597
4598 string tok = arg_tokens[argno-1];
4599
4600 regex_t preg;
4601 // this pattern matches 0xD(%R) | (%R) | %R
4602 const char *pattern = "\\([0x]*-*[0-9]*\\)\\([(]*\\)\\(%[0-9a-z][0-9a-z][0-9a-z]*\\)\\([)]*\\)";
4603 int rc;
4604 size_t nmatch = 5;
4605 regmatch_t pmatch[5];
277c21bc 4606
aff5d390
SC
4607 if (0 != (rc = regcomp(&preg, pattern, 0)))
4608 throw semantic_error("Failed to parse probe operand");
4609 if (0 != (rc = regexec(&preg, arg_tokens[argno-1].c_str(), nmatch, pmatch, 0)))
aff5d390 4610 {
305a3c44
SC
4611 // Do we have a numeric literal?
4612 // Check this separately instead of complicating the above pattern.
4613 if (0 != (rc = regcomp(&preg, "\\$[0-9][0-9]*", 0)))
4614 throw semantic_error("Failed to parse probe operand");
4615 if (0 != (rc = regexec(&preg, arg_tokens[argno-1].c_str(), nmatch, pmatch, 0)))
4616 throw semantic_error("Unsupported assembler operand while accessing "
4617 + probe_name + " " + e->name + " " + arg_tokens[argno-1],
4618 e->tok);
4619 arg_type = literal_arg;
dc4c488a 4620 ln = new literal_number(lex_cast<int>(arg_tokens[argno-1].substr(pmatch[0].rm_so+1)));
305a3c44 4621 ln->tok = e->tok;
aff5d390 4622 }
305a3c44 4623 else
aff5d390 4624 {
305a3c44
SC
4625 // Is there a displacement?
4626 if (pmatch[2].rm_so > pmatch[1].rm_so)
4627 {
dc4c488a 4628 string disp_str = arg_tokens[argno-1].substr(pmatch[1].rm_so);
305a3c44
SC
4629 disp = lex_cast<int>(disp_str.substr(0,pmatch[1].rm_eo - pmatch[1].rm_so));
4630 }
4631 // Is there an indirect register?
4632 if ((arg_tokens[argno-1][pmatch[2].rm_so]) == '(')
4633 arg_type = memory_arg;
4634 else
4635 arg_type = register_arg;
4636 // Is there a register?
4637 if (pmatch[3].rm_eo >= pmatch[3].rm_so)
4638 {
dc4c488a 4639 reg = arg_tokens[argno-1].substr(pmatch[3].rm_so+1);
305a3c44
SC
4640 reg.erase(pmatch[3].rm_eo - pmatch[3].rm_so - 1);
4641 }
4642 if (reg.length() == 0)
4643 throw semantic_error("Unsupported assembler operand while accessing "
4644 + probe_name + " " + e->name, e->tok);
4645 // synthesize user_long(%{fetch_register(R)%} + D)
4646 fc->function = "user_long";
4647 fc->tok = e->tok;
4648 be->tok = e->tok;
4649
4650 embedded_expr *get_arg1 = new embedded_expr;
4651 get_arg1->tok = e->tok;
4652 get_arg1->code = string("/* unprivileged */ /* pure */")
4653 + (is_user_module (process_name)
4654 ? string("u_fetch_register(")
4655 : string("k_fetch_register("))
4656 + lex_cast(dwarf_regs[reg]) + string(")");
4657 // XXX: may we ever need to cast that to a narrower type?
4658
4659 be->left = get_arg1;
4660 be->op = "+";
4661 literal_number* inc = new literal_number(disp);
4662 inc->tok = e->tok;
4663 be->right = inc;
4664 fc->args.push_back(be);
aff5d390 4665 }
277c21bc 4666
aff5d390
SC
4667 if (e->components.empty()) // We have a scalar
4668 {
4669 if (e->addressof)
4670 throw semantic_error("cannot take address of sdt variable", e->tok);
63ea4244 4671
305a3c44 4672 if (arg_type == memory_arg)
aff5d390 4673 provide(fc);
305a3c44 4674 else if (arg_type == register_arg)
aff5d390 4675 provide(be);
305a3c44
SC
4676 else if (arg_type == literal_arg)
4677 provide(ln);
aff5d390
SC
4678 return;
4679 }
4680 cast_op *cast = new cast_op;
277c21bc 4681 cast->name = "@cast";
aff5d390 4682 cast->tok = e->tok;
305a3c44 4683 if (arg_type == memory_arg)
aff5d390 4684 cast->operand = fc;
305a3c44 4685 else if (arg_type == register_arg)
aff5d390 4686 cast->operand = be;
305a3c44
SC
4687 else if (arg_type == literal_arg)
4688 provide(ln);
aff5d390 4689 cast->components = e->components;
7f6b80bd 4690 cast->type_name = probe_name + "_arg" + lex_cast(argno);
aff5d390
SC
4691 cast->module = process_name;
4692
4693 cast->visit(this);
4694 }
4695 catch (const semantic_error &er)
4696 {
4697 e->chain (er);
4698 provide (e);
4699 }
4700}
4701
4702
4703void
4704sdt_kprobe_var_expanding_visitor::visit_target_symbol (target_symbol *e)
4705{
4706 try
4707 {
277c21bc 4708 if (e->name == "$$name")
aff5d390
SC
4709 {
4710 if (e->addressof)
4711 throw semantic_error("cannot take address of sdt context variable", e->tok);
4712
4713 literal_string *myname = new literal_string (probe_name);
4714 myname->tok = e->tok;
4715 provide(myname);
4716 return;
4717 }
277c21bc 4718 if (e->name == "$$provider")
a794dbeb
FCE
4719 {
4720 if (e->addressof)
4721 throw semantic_error("cannot take address of sdt context variable", e->tok);
4722
4723 literal_string *myname = new literal_string (provider_name);
4724 myname->tok = e->tok;
4725 provide(myname);
4726 return;
4727 }
aff5d390 4728
5ecaa5a7 4729 int argno = -1;
aff5d390
SC
4730 try
4731 {
5ecaa5a7
JS
4732 if (startswith(e->name, "$arg"))
4733 argno = lex_cast<int>(e->name.substr(4));
aff5d390
SC
4734 }
4735 catch (const runtime_error& f) // non-integral $arg suffix: e.g. $argKKKSDF
4736 {
aff5d390 4737 }
5ecaa5a7
JS
4738 if (argno < 0)
4739 throw semantic_error("invalid variable, must be of the form $argN", e->tok);
aff5d390
SC
4740 if (argno < 1 || argno > arg_count)
4741 throw semantic_error("invalid argument number", e->tok);
5ecaa5a7 4742
c69a87e0
FCE
4743 bool lvalue = is_active_lvalue(e);
4744 functioncall *fc = new functioncall;
63ea4244 4745
c69a87e0
FCE
4746 // First two args are hidden: 1. pointer to probe name 2. task id
4747 if (arg_count < 2)
aff5d390
SC
4748 {
4749 fc->function = "ulong_arg";
4750 fc->type = pe_long;
4751 fc->tok = e->tok;
4752 // skip the hidden args
4753 literal_number* num = new literal_number(argno + 2);
4754 num->tok = e->tok;
4755 fc->args.push_back(num);
4756 }
4757 else
4758 {
4759 // args are passed in arg3 as members of a struct
4760 fc->function = "user_long";
4761 fc->tok = e->tok;
4762 binary_expression *be = new binary_expression;
4763 be->tok = e->tok;
4764 functioncall *get_arg1 = new functioncall;
4765 get_arg1->function = "pointer_arg";
4766 get_arg1->tok = e->tok;
4767 // arg3 is the pointer to a struct of arguments
4768 literal_number* num = new literal_number(3);
4769 num->tok = e->tok;
4770 get_arg1->args.push_back(num);
4771
4772 be->left = get_arg1;
4773 be->op = "+";
4774 // offset in struct to the desired arg
4775 literal_number* inc = new literal_number((argno - 1) * 8);
4776 inc->tok = e->tok;
4777 be->right = inc;
4778 fc->args.push_back(be);
4779 }
c69a87e0 4780 if (lvalue)
aff5d390 4781 *(target_symbol_setter_functioncalls.top()) = fc;
63ea4244 4782
aff5d390
SC
4783 if (e->components.empty()) // We have a scalar
4784 {
4785 if (e->addressof)
4786 throw semantic_error("cannot take address of sdt variable", e->tok);
63ea4244 4787
aff5d390
SC
4788 provide(fc);
4789 return;
4790 }
c69a87e0 4791 cast_op *cast = new cast_op;
277c21bc 4792 cast->name = "@cast";
c69a87e0
FCE
4793 cast->tok = e->tok;
4794 cast->operand = fc;
4795 cast->components = e->components;
7f6b80bd 4796 cast->type_name = probe_name + "_arg" + lex_cast(argno);
c69a87e0 4797 cast->module = process_name;
63ea4244 4798
c69a87e0 4799 cast->visit(this);
7a05f484 4800 }
c69a87e0 4801 catch (const semantic_error &er)
ad002306 4802 {
1af1e62d 4803 e->chain (er);
c69a87e0 4804 provide (e);
ad002306 4805 }
7a05f484 4806}
46b84a80 4807
277c21bc 4808
edce5b67
JS
4809struct sdt_query : public base_query
4810{
4811 sdt_query(probe * base_probe, probe_point * base_loc,
4812 dwflpp & dw, literal_map_t const & params,
4813 vector<derived_probe *> & results);
4814
4815 void handle_query_module();
4816
4817private:
15284963 4818 stap_sdt_probe_type probe_type;
edce5b67
JS
4819 probe * base_probe;
4820 probe_point * base_loc;
6846cfc8 4821 literal_map_t const & params;
edce5b67 4822 vector<derived_probe *> & results;
a794dbeb
FCE
4823 string pp_mark;
4824 string pp_provider;
edce5b67
JS
4825
4826 set<string> probes_handled;
4827
4828 Elf_Data *pdata;
4829 size_t probe_scn_offset;
4830 size_t probe_scn_addr;
aff5d390
SC
4831 uint64_t arg_count;
4832 uint64_t pc;
4833 string arg_string;
edce5b67 4834 string probe_name;
a794dbeb 4835 string provider_name;
edce5b67
JS
4836
4837 bool init_probe_scn();
4838 bool get_next_probe();
4839
4840 void convert_probe(probe *base);
4ddb6dd0 4841 void record_semaphore(vector<derived_probe *> & results, unsigned start);
c72aa911 4842 probe* convert_location();
aff5d390
SC
4843 bool have_uprobe() {return probe_type == uprobe1_type || probe_type == uprobe2_type;}
4844 bool have_kprobe() {return probe_type == kprobe1_type || probe_type == kprobe2_type;}
edce5b67
JS
4845};
4846
4847
4848sdt_query::sdt_query(probe * base_probe, probe_point * base_loc,
4849 dwflpp & dw, literal_map_t const & params,
4850 vector<derived_probe *> & results):
4851 base_query(dw, params), base_probe(base_probe),
6846cfc8 4852 base_loc(base_loc), params(params), results(results)
edce5b67 4853{
a794dbeb
FCE
4854 assert(get_string_param(params, TOK_MARK, pp_mark));
4855 get_string_param(params, TOK_PROVIDER, pp_provider); // pp_provider == "" -> unspecified
4856
ef428667
FCE
4857 // PR10245: permit usage of dtrace-y "-" separator in marker name;
4858 // map it to double-underscores.
4859 size_t pos = 0;
4860 while (1) // there may be more than one
4861 {
a794dbeb 4862 size_t i = pp_mark.find("-", pos);
ef428667 4863 if (i == string::npos) break;
a794dbeb 4864 pp_mark.replace (i, 1, "__");
ef428667
FCE
4865 pos = i+1; // resume searching after the inserted __
4866 }
a794dbeb
FCE
4867
4868 // XXX: same for pp_provider?
edce5b67
JS
4869}
4870
4871
4872void
4873sdt_query::handle_query_module()
4874{
4875 if (!init_probe_scn())
4876 return;
4877
4878 if (sess.verbose > 3)
a794dbeb 4879 clog << "TOK_MARK: " << pp_mark << " TOK_PROVIDER: " << pp_provider << endl;
edce5b67
JS
4880
4881 while (get_next_probe())
4882 {
aff5d390 4883 if (! have_uprobe()
696ec154 4884 && !probes_handled.insert(probe_name).second)
edce5b67
JS
4885 continue;
4886
39a3e397
SC
4887 if (sess.verbose > 3)
4888 {
4889 clog << "matched probe_name " << probe_name << " probe_type ";
4890 switch (probe_type)
4891 {
aff5d390
SC
4892 case uprobe1_type:
4893 clog << "uprobe1 at 0x" << hex << pc << dec << endl;
4894 break;
4895 case uprobe2_type:
4896 clog << "uprobe2 at 0x" << hex << pc << dec << endl;
4897 break;
4898 case kprobe1_type:
4899 clog << "kprobe1" << endl;
39a3e397 4900 break;
aff5d390
SC
4901 case kprobe2_type:
4902 clog << "kprobe2" << endl;
39a3e397 4903 break;
39a3e397
SC
4904 }
4905 }
30263a73 4906
c72aa911
JS
4907 // Extend the derivation chain
4908 probe *new_base = convert_location();
4909 probe_point *new_location = new_base->locations[0];
edce5b67 4910
aff5d390
SC
4911 bool kprobe_found = false;
4912 if (have_kprobe())
edce5b67
JS
4913 {
4914 convert_probe(new_base);
aff5d390
SC
4915 kprobe_found = true;
4916 // Expand the local variables in the probe body
4917 sdt_kprobe_var_expanding_visitor svv (module_val,
a794dbeb 4918 provider_name,
aff5d390
SC
4919 probe_name,
4920 arg_string,
4921 arg_count);
4922 svv.replace (new_base->body);
edce5b67 4923 }
aff5d390
SC
4924 else
4925 {
f83336a5
FCE
4926 /* Figure out the architecture of this particular ELF file.
4927 The dwarfless register-name mappings depend on it. */
4928 Dwarf_Addr bias;
4929 Elf* elf = (dwarf_getelf (dwfl_module_getdwarf (dw.mod_info->mod, &bias))
4930 ?: dwfl_module_getelf (dw.mod_info->mod, &bias));
4931 GElf_Ehdr ehdr_mem;
4932 GElf_Ehdr* em = gelf_getehdr (elf, &ehdr_mem);
4933 if (em == 0) { dwfl_assert ("dwfl_getehdr", dwfl_errno()); }
4934 int elf_machine = em->e_machine;
4935 sdt_uprobe_var_expanding_visitor svv (sess, elf_machine,
4936 module_val,
a794dbeb 4937 provider_name,
aff5d390
SC
4938 probe_name,
4939 arg_string,
4940 arg_count);
4941 svv.replace (new_base->body);
4942 }
4943
edce5b67
JS
4944 unsigned i = results.size();
4945
aff5d390 4946 if (have_kprobe())
4ddb6dd0
JS
4947 derive_probes(sess, new_base, results);
4948
edce5b67
JS
4949 else
4950 {
a794dbeb 4951 // XXX: why not derive_probes() in the uprobes case case too?
edce5b67
JS
4952 literal_map_t params;
4953 for (unsigned i = 0; i < new_location->components.size(); ++i)
4954 {
4955 probe_point::component *c = new_location->components[i];
4956 params[c->functor] = c->arg;
4957 }
4958
aff5d390
SC
4959 dwarf_query q(new_base, new_location, dw, params, results, "", "");
4960 q.has_mark = true; // enables mid-statement probing
edce5b67 4961
aff5d390
SC
4962 if (probe_type == uprobe1_type)
4963 dw.iterate_over_modules(&query_module, &q);
4964 else if (probe_type == uprobe2_type)
4965 {
4966 Dwarf_Addr bias;
4967 string section;
4968 Elf* elf = dwfl_module_getelf (q.dw.mod_info->mod, &bias);
4969 assert(elf);
4970 Dwarf_Addr reloc_addr = q.statement_num_val + bias;
4971 if (dwfl_module_relocations (q.dw.mod_info->mod) > 0)
4972 {
4973 dwfl_module_relocate_address (q.dw.mod_info->mod, &reloc_addr);
4974 section = ".dynamic";
4975 }
4976 else
4977 section = ".absolute";
4978
4979 uprobe_derived_probe* p =
4980 new uprobe_derived_probe ("", "", 0, q.module_val, section,
4981 q.statement_num_val, reloc_addr, q, 0);
4982 results.push_back (p);
4983 }
4984 }
4ddb6dd0 4985 record_semaphore(results, i);
edce5b67
JS
4986 }
4987}
4988
4989
4990bool
4991sdt_query::init_probe_scn()
4992{
4993 Elf* elf;
4994 GElf_Shdr shdr_mem;
4995 GElf_Shdr *shdr = NULL;
4996 Dwarf_Addr bias;
4997 size_t shstrndx;
4998
4999 // Explicitly look in the main elf file first.
5000 elf = dwfl_module_getelf (dw.module, &bias);
5001 Elf_Scn *probe_scn = NULL;
5002
5003 dwfl_assert ("getshdrstrndx", elf_getshdrstrndx (elf, &shstrndx));
5004
5005 bool have_probes = false;
5006
5007 // Is there a .probes section?
5008 while ((probe_scn = elf_nextscn (elf, probe_scn)))
5009 {
5010 shdr = gelf_getshdr (probe_scn, &shdr_mem);
5011 assert (shdr != NULL);
5012
5013 if (strcmp (elf_strptr (elf, shstrndx, shdr->sh_name), ".probes") == 0)
5014 {
5015 have_probes = true;
5016 break;
5017 }
5018 }
5019
5020 // Older versions put .probes section in the debuginfo dwarf file,
5021 // so check if it actually exists, if not take a look in the debuginfo file
5022 if (! have_probes || (have_probes && shdr->sh_type == SHT_NOBITS))
5023 {
5024 elf = dwarf_getelf (dwfl_module_getdwarf (dw.module, &bias));
5025 if (! elf)
5026 return false;
5027 dwfl_assert ("getshdrstrndx", elf_getshdrstrndx (elf, &shstrndx));
5028 probe_scn = NULL;
5029 while ((probe_scn = elf_nextscn (elf, probe_scn)))
5030 {
5031 shdr = gelf_getshdr (probe_scn, &shdr_mem);
5032 if (strcmp (elf_strptr (elf, shstrndx, shdr->sh_name),
5033 ".probes") == 0)
aff5d390
SC
5034 {
5035 have_probes = true;
5036 break;
5037 }
edce5b67
JS
5038 }
5039 }
5040
5041 if (!have_probes)
5042 return false;
5043
5044 pdata = elf_getdata_rawchunk (elf, shdr->sh_offset, shdr->sh_size, ELF_T_BYTE);
5045 probe_scn_offset = 0;
5046 probe_scn_addr = shdr->sh_addr;
5047 assert (pdata != NULL);
5048 if (sess.verbose > 4)
5049 clog << "got .probes elf scn_addr@0x" << probe_scn_addr << dec
5050 << ", size: " << pdata->d_size << endl;
5051 return true;
5052}
5053
5054bool
5055sdt_query::get_next_probe()
5056{
3a31e709
SC
5057 // Extract probe info from the .probes section, e.g.
5058 // 74657374 5f70726f 62655f32 00000000 test_probe_2....
5059 // 50524233 00000000 980c2000 00000000 PRB3...... .....
5060 // 01000000 00000000 00000000 00000000 ................
5061 // test_probe_2 is probe_name, probe_type is 50524233,
5062 // *probe_name (pbe->name) is 980c2000, probe_arg (pbe->arg) is 1
5063 // probe_scn_offset is position currently being scanned in .probes
edce5b67
JS
5064
5065 while (probe_scn_offset < pdata->d_size)
5066 {
aff5d390
SC
5067 stap_sdt_probe_entry_v1 *pbe_v1 = (stap_sdt_probe_entry_v1 *) ((char*)pdata->d_buf + probe_scn_offset);
5068 stap_sdt_probe_entry_v2 *pbe_v2 = (stap_sdt_probe_entry_v2 *) ((char*)pdata->d_buf + probe_scn_offset);
15284963 5069 probe_type = (stap_sdt_probe_type)(pbe_v1->type_a);
aff5d390 5070 if (! have_uprobe() && ! have_kprobe())
edce5b67
JS
5071 {
5072 // Unless this is a mangled .probes section, this happens
5073 // because the name of the probe comes first, followed by
5074 // the sentinel.
5075 if (sess.verbose > 5)
5076 clog << "got unknown probe_type: 0x" << hex << probe_type
5077 << dec << endl;
5078 probe_scn_offset += sizeof(__uint32_t);
5079 continue;
5080 }
aff5d390
SC
5081 if ((long)pbe_v1 % sizeof(__uint64_t)) // we have stap_sdt_probe_entry_v1.type_b
5082 {
5083 pbe_v1 = (stap_sdt_probe_entry_v1*)((char*)pbe_v1 - sizeof(__uint32_t));
5084 if (pbe_v1->type_b != uprobe1_type && pbe_v1->type_b != kprobe1_type)
5085 continue;
5086 }
5087
5088 if (probe_type == uprobe1_type || probe_type == kprobe1_type)
5089 {
5090 probe_name = (char*)((char*)pdata->d_buf + pbe_v1->name - (char*)probe_scn_addr);
a794dbeb 5091 provider_name = ""; // unknown
aff5d390
SC
5092 if (probe_type == uprobe1_type)
5093 {
5094 pc = pbe_v1->arg;
5095 arg_count = 0;
5096 }
5097 else if (probe_type == kprobe1_type)
5098 arg_count = pbe_v1->arg;
5099 probe_scn_offset += sizeof (stap_sdt_probe_entry_v1);
5100 }
5101 else if (probe_type == uprobe2_type || probe_type == kprobe2_type)
5102 {
5103 probe_name = (char*)((char*)pdata->d_buf + pbe_v2->name - (char*)probe_scn_addr);
a794dbeb 5104 provider_name = (char*)((char*)pdata->d_buf + pbe_v2->provider - (char*)probe_scn_addr);
aff5d390
SC
5105 arg_count = pbe_v2->arg_count;
5106 pc = pbe_v2->pc;
5107 if (pbe_v2->arg_string)
5108 arg_string = (char*)((char*)pdata->d_buf + pbe_v2->arg_string - (char*)probe_scn_addr);
5109 probe_scn_offset += sizeof (stap_sdt_probe_entry_v2);
5110 }
edce5b67 5111 if (sess.verbose > 4)
a794dbeb 5112 clog << "saw .probes " << probe_name << (provider_name != "" ? " (provider "+provider_name+") " : "")
aff5d390 5113 << "@0x" << hex << pc << dec << endl;
edce5b67 5114
a794dbeb
FCE
5115 if (dw.function_name_matches_pattern (probe_name, pp_mark)
5116 && ((pp_provider == "") || dw.function_name_matches_pattern (provider_name, pp_provider)))
39a3e397 5117 return true;
edce5b67
JS
5118 else
5119 continue;
5120 }
5121 return false;
5122}
5123
5124
6846cfc8 5125void
4ddb6dd0 5126sdt_query::record_semaphore (vector<derived_probe *> & results, unsigned start)
6846cfc8 5127{
a794dbeb
FCE
5128 for (unsigned i=0; i<2; i++) {
5129 // prefer with-provider symbol; look without provider prefix for backward compatibility only
5130 string semaphore = (i==0 ? (provider_name+"_") : "") + probe_name + "_semaphore";
5131 // XXX: multiple addresses?
5132 if (sess.verbose > 2)
5133 clog << "looking for semaphore symbol " << semaphore;
5134
5135 Dwarf_Addr addr = lookup_symbol_address(dw.module, semaphore.c_str());
5136 if (addr)
5137 {
5138 if (dwfl_module_relocations (dw.module) > 0)
5139 dwfl_module_relocate_address (dw.module, &addr);
5140 // XXX: relocation basis?
5141 for (unsigned i = start; i < results.size(); ++i)
5142 results[i]->sdt_semaphore_addr = addr;
5143 if (sess.verbose > 2)
5144 clog << ", found at 0x" << hex << addr << dec << endl;
5145 return;
5146 }
5147 else
5148 if (sess.verbose > 2)
5149 clog << ", not found" << endl;
5150 }
6846cfc8
SC
5151}
5152
5153
edce5b67
JS
5154void
5155sdt_query::convert_probe (probe *base)
5156{
5157 block *b = new block;
5158 b->tok = base->body->tok;
5159
5160 // XXX: Does this also need to happen for i386 under x86_64 stap?
a794dbeb 5161#ifdef __i386__ /* XXX: probably s.architecture[] instead */
aff5d390 5162 if (have_kprobe())
edce5b67
JS
5163 {
5164 functioncall *rp = new functioncall;
edce5b67
JS
5165 rp->function = "regparm";
5166 rp->tok = b->tok;
5167 literal_number* littid = new literal_number(0);
5168 littid->tok = b->tok;
5169 rp->args.push_back(littid);
5170 expr_statement* es = new expr_statement;
5171 es->tok = b->tok;
5172 es->value = rp;
5173 b->statements.push_back(es);
5174 }
5175#endif
5176
aff5d390 5177 if (have_kprobe())
edce5b67 5178 {
aff5d390 5179 // Generate: if (arg2 != kprobe2_type) next;
edce5b67
JS
5180 if_statement *istid = new if_statement;
5181 istid->thenblock = new next_statement;
5182 istid->elseblock = NULL;
5183 istid->tok = b->tok;
63ea4244 5184 istid->thenblock->tok = b->tok;
edce5b67
JS
5185 comparison *betid = new comparison;
5186 betid->op = "!=";
5187 betid->tok = b->tok;
5188
5189 functioncall *arg2 = new functioncall;
5190 arg2->function = "ulong_arg";
5191 arg2->tok = b->tok;
5192 literal_number* num = new literal_number(2);
5193 num->tok = b->tok;
5194 arg2->args.push_back(num);
5195
5196 betid->left = arg2;
aff5d390 5197 literal_number* littid = new literal_number(probe_type);
edce5b67
JS
5198 littid->tok = b->tok;
5199 betid->right = littid;
5200 istid->condition = betid;
5201 b->statements.push_back(istid);
5202 }
5203
5204 // Generate: if (arg1 != mark("label")) next;
5205 functioncall *fc = new functioncall;
bbafcb1e 5206 fc->function = "ulong_arg";
edce5b67 5207 fc->tok = b->tok;
bbafcb1e 5208 literal_number* num = new literal_number(1);
edce5b67
JS
5209 num->tok = b->tok;
5210 fc->args.push_back(num);
5211
5212 functioncall *fcus = new functioncall;
5213 fcus->function = "user_string";
5214 fcus->type = pe_string;
5215 fcus->tok = b->tok;
5216 fcus->args.push_back(fc);
5217
5218 if_statement *is = new if_statement;
5219 is->thenblock = new next_statement;
5220 is->elseblock = NULL;
5221 is->tok = b->tok;
63ea4244 5222 is->thenblock->tok = b->tok;
edce5b67
JS
5223 comparison *be = new comparison;
5224 be->op = "!=";
5225 be->tok = b->tok;
5226 be->left = fcus;
5227 be->right = new literal_string(probe_name);
63ea4244 5228 be->right->tok = b->tok;
edce5b67
JS
5229 is->condition = be;
5230 b->statements.push_back(is);
5231
5232 // Now replace the body
5233 b->statements.push_back(base->body);
5234 base->body = b;
5235}
5236
5237
c72aa911
JS
5238probe*
5239sdt_query::convert_location ()
edce5b67 5240{
c72aa911
JS
5241 probe_point* specific_loc = new probe_point(*base_loc);
5242 probe_point* derived_loc = new probe_point(*base_loc);
edce5b67 5243
c72aa911
JS
5244 for (unsigned i = 0; i < derived_loc->components.size(); ++i)
5245 if (derived_loc->components[i]->functor == TOK_MARK)
5246 {
5247 // replace the possibly wildcarded arg with the specific marker name
5248 specific_loc->components[i] =
5249 new probe_point::component(TOK_MARK, new literal_string(probe_name));
edce5b67 5250
a794dbeb
FCE
5251 // XXX: similarly, fill in a TOK_PROVIDER() on the base_loc if
5252 // one was missing/unspecified/wildcarded.
5253
aff5d390
SC
5254 if (sess.verbose > 3)
5255 switch (probe_type)
5256 {
5257 case uprobe1_type:
5258 clog << "probe_type == uprobe1, use statement addr: 0x"
5259 << hex << pc << dec << endl;
5260 break;
5261 case uprobe2_type:
5262 clog << "probe_type == uprobe2, use statement addr: 0x"
5263 << hex << pc << dec << endl;
5264 break;
5265
5266 case kprobe1_type:
5267 clog << "probe_type == kprobe1" << endl;
5268 break;
5269 case kprobe2_type:
5270 clog << "probe_type == kprobe2" << endl;
5271 break;
5272 default:
5273 clog << "probe_type == use_uprobe_no_dwarf, use label name: "
a794dbeb 5274 << "_stapprobe1_" << pp_mark << endl;
aff5d390
SC
5275 }
5276
c72aa911
JS
5277 switch (probe_type)
5278 {
aff5d390
SC
5279 case uprobe1_type:
5280 case uprobe2_type:
c72aa911
JS
5281 // process("executable").statement(probe_arg)
5282 derived_loc->components[i] =
9ea68eb9 5283 new probe_point::component(TOK_STATEMENT,
aff5d390 5284 new literal_number(pc, true));
c72aa911
JS
5285 break;
5286
aff5d390
SC
5287 case kprobe1_type:
5288 case kprobe2_type:
b642c901
SC
5289 // kernel.function("*getegid*")
5290 derived_loc->components[i] =
5291 new probe_point::component(TOK_FUNCTION, new literal_string("*getegid*"));
5292 if (derived_loc->components[i - 1]->functor == TOK_LIBRARY)
5293 derived_loc->components.erase (derived_loc->components.begin() + i - 1);
5294 break;
c72aa911 5295
a794dbeb 5296 default: // deprecated
c72aa911
JS
5297 // process("executable").function("*").label("_stapprobe1_MARK_NAME")
5298 derived_loc->components[i] =
5299 new probe_point::component(TOK_FUNCTION, new literal_string("*"));
5300 derived_loc->components.push_back
5301 (new probe_point::component(TOK_LABEL,
a794dbeb 5302 new literal_string("_stapprobe1_" + pp_mark)));
c72aa911
JS
5303 break;
5304 }
5305 }
5306 else if (derived_loc->components[i]->functor == TOK_PROCESS
aff5d390 5307 && have_kprobe())
63b4fd14 5308 {
c72aa911 5309 derived_loc->components[i] = new probe_point::component(TOK_KERNEL);
63b4fd14 5310 }
edce5b67 5311
c72aa911 5312 return base_probe->create_alias(derived_loc, specific_loc);
edce5b67
JS
5313}
5314
5315
20c6c071 5316void
5227f1ea 5317dwarf_builder::build(systemtap_session & sess,
7a053d3b 5318 probe * base,
20c6c071 5319 probe_point * location,
86bf665e 5320 literal_map_t const & parameters,
20c6c071
GH
5321 vector<derived_probe *> & finished_results)
5322{
b20febf3
FCE
5323 // NB: the kernel/user dwlfpp objects are long-lived.
5324 // XXX: but they should be per-session, as this builder object
5325 // may be reused if we try to cross-instrument multiple targets.
84048984 5326
7a24d422
FCE
5327 dwflpp* dw = 0;
5328
7a24d422 5329 string module_name;
ae2552da
FCE
5330 if (has_null_param (parameters, TOK_KERNEL))
5331 {
5332 dw = get_kern_dw(sess, "kernel");
5333 }
5334 else if (get_param (parameters, TOK_MODULE, module_name))
b8da0ad1 5335 {
ae2552da 5336 dw = get_kern_dw(sess, module_name);
b8da0ad1 5337 }
7a24d422 5338 else if (get_param (parameters, TOK_PROCESS, module_name))
b8da0ad1 5339 {
63b4fd14 5340 string library_name;
b642c901 5341 user_path = find_executable (module_name); // canonicalize it
63b4fd14 5342 if (get_param (parameters, TOK_LIBRARY, library_name))
b642c901
SC
5343 {
5344 module_name = find_executable (library_name, "LD_LIBRARY_PATH");
5345 user_lib = module_name;
5346 }
63b4fd14 5347 else
b642c901 5348 module_name = user_path; // canonicalize it
d0a7f5a9 5349
e34d5d13
FCE
5350 if (sess.kernel_config["CONFIG_UTRACE"] != string("y"))
5351 throw semantic_error ("process probes not available without kernel CONFIG_UTRACE");
5352
7a24d422
FCE
5353 // user-space target; we use one dwflpp instance per module name
5354 // (= program or shared library)
707bf35e 5355 dw = get_user_dw(sess, module_name);
c8959a29 5356 }
20c6c071 5357
5896cd05
MW
5358 if (sess.verbose > 3)
5359 clog << "dwarf_builder::build for " << module_name << endl;
5360
a794dbeb
FCE
5361 string dummy_mark_name; // NB: PR10245: dummy value, need not substitute - => __
5362 if (get_param(parameters, TOK_MARK, dummy_mark_name))
f28a8c28 5363 {
edce5b67
JS
5364 sdt_query sdtq(base, location, *dw, parameters, finished_results);
5365 dw->iterate_over_modules(&query_module, &sdtq);
5366 return;
7a05f484 5367 }
20c6c071 5368
8f14e444 5369 unsigned results_pre = finished_results.size();
b642c901 5370 dwarf_query q(base, location, *dw, parameters, finished_results, user_path, user_lib);
7a24d422
FCE
5371
5372 // XXX: kernel.statement.absolute is a special case that requires no
5373 // dwfl processing. This code should be in a separate builder.
7a24d422 5374 if (q.has_kernel && q.has_absolute)
37ebca01 5375 {
4baf0e53 5376 // assert guru mode for absolute probes
37ebca01
FCE
5377 if (! q.base_probe->privileged)
5378 {
edce5b67
JS
5379 throw semantic_error ("absolute statement probe in unprivileged script",
5380 q.base_probe->tok);
37ebca01
FCE
5381 }
5382
5383 // For kernel.statement(NUM).absolute probe points, we bypass
5384 // all the debuginfo stuff: We just wire up a
5385 // dwarf_derived_probe right here and now.
4baf0e53 5386 dwarf_derived_probe* p =
b8da0ad1
FCE
5387 new dwarf_derived_probe ("", "", 0, "kernel", "",
5388 q.statement_num_val, q.statement_num_val,
5389 q, 0);
37ebca01 5390 finished_results.push_back (p);
1a0dbc5a 5391 sess.unwindsym_modules.insert ("kernel");
37ebca01
FCE
5392 return;
5393 }
5394
51178501 5395 dw->iterate_over_modules(&query_module, &q);
8f14e444
FCE
5396
5397
5398 // PR11553 special processing: .return probes requested, but
5399 // some inlined function instances matched.
5400 unsigned i_n_r = q.inlined_non_returnable.size();
5401 unsigned results_post = finished_results.size();
5402 if (i_n_r > 0)
5403 {
5404 if ((results_pre == results_post) && (! sess.suppress_warnings)) // no matches; issue warning
5405 {
5406 string quicklist;
5407 for (set<string>::iterator it = q.inlined_non_returnable.begin();
5408 it != q.inlined_non_returnable.end();
5409 it++)
5410 {
5411 quicklist += " " + (*it);
5412 if (quicklist.size() > 80) // heuristic, don't make an overlong report line
5413 {
5414 quicklist += " ...";
5415 break;
5416 }
5417 }
5418
5419 sess.print_warning ("cannot probe .return of " + lex_cast(i_n_r) + " inlined function(s):" + quicklist);
5420 // There will be also a "no matches" semantic error generated.
5421 }
5422 if (sess.verbose > 1)
5423 clog << "skipped .return probe of " + lex_cast(i_n_r) + " inlined function(s)" << endl;
5424 if ((sess.verbose > 3) || (sess.verbose > 2 && results_pre == results_post)) // issue details with high verbosity
5425 {
5426 for (set<string>::iterator it = q.inlined_non_returnable.begin();
5427 it != q.inlined_non_returnable.end();
5428 it++)
5429 clog << (*it) << " ";
5430 clog << endl;
5431 }
5432 } // i_n_r > 0
5f0a03a6
JK
5433}
5434
5435symbol_table::~symbol_table()
5436{
c9efa5c9 5437 delete_map(map_by_addr);
5f0a03a6
JK
5438}
5439
5440void
2867a2a1
JS
5441symbol_table::add_symbol(const char *name, bool weak, bool descriptor,
5442 Dwarf_Addr addr, Dwarf_Addr *high_addr)
5f0a03a6 5443{
ab91b232
JK
5444#ifdef __powerpc__
5445 // Map ".sys_foo" to "sys_foo".
5446 if (name[0] == '.')
5447 name++;
5448#endif
5f0a03a6
JK
5449 func_info *fi = new func_info();
5450 fi->addr = addr;
5451 fi->name = name;
ab91b232 5452 fi->weak = weak;
2867a2a1 5453 fi->descriptor = descriptor;
5f0a03a6
JK
5454 map_by_name[fi->name] = fi;
5455 // TODO: Use a multimap in case there are multiple static
5456 // functions with the same name?
1c6b77e5 5457 map_by_addr.insert(make_pair(addr, fi));
5f0a03a6
JK
5458}
5459
5460enum info_status
5461symbol_table::read_symbols(FILE *f, const string& path)
5462{
5463 // Based on do_kernel_symbols() in runtime/staprun/symbols.c
5464 int ret;
2e67a43b
TM
5465 char *name = 0;
5466 char *mod = 0;
5f0a03a6
JK
5467 char type;
5468 unsigned long long addr;
5469 Dwarf_Addr high_addr = 0;
5470 int line = 0;
5471
5472 // %as (non-POSIX) mallocs space for the string and stores its address.
5473 while ((ret = fscanf(f, "%llx %c %as [%as", &addr, &type, &name, &mod)) > 0)
5474 {
2e67a43b
TM
5475 auto_free free_name(name);
5476 auto_free free_mod(mod);
5f0a03a6
JK
5477 line++;
5478 if (ret < 3)
5479 {
41c262f3 5480 cerr << "Symbol table error: Line "
5f0a03a6
JK
5481 << line
5482 << " of symbol list from "
5483 << path
5484 << " is not in correct format: address type name [module]";
5485 // Caller should delete symbol_table object.
5486 return info_absent;
5487 }
2e67a43b 5488 else if (ret > 3)
5f0a03a6
JK
5489 {
5490 // Modules are loaded above the kernel, so if we're getting
5491 // modules, we're done.
2e67a43b 5492 break;
5f0a03a6 5493 }
ab91b232 5494 if (type == 'T' || type == 't' || type == 'W')
2867a2a1 5495 add_symbol(name, (type == 'W'), false, (Dwarf_Addr) addr, &high_addr);
5f0a03a6
JK
5496 }
5497
1c6b77e5 5498 if (map_by_addr.size() < 1)
5f0a03a6
JK
5499 {
5500 cerr << "Symbol table error: "
5501 << path << " contains no function symbols." << endl;
5502 return info_absent;
5503 }
5504 return info_present;
5505}
5506
5507// NB: This currently unused. We use get_from_elf() instead because
5508// that gives us raw addresses -- which we need for modules -- whereas
5509// nm provides the address relative to the beginning of the section.
5510enum info_status
83ca3872
MW
5511symbol_table::read_from_elf_file(const string &path,
5512 const systemtap_session &sess)
5f0a03a6
JK
5513{
5514 FILE *f;
5515 string cmd = string("/usr/bin/nm -n --defined-only ") + path;
5516 f = popen(cmd.c_str(), "r");
5517 if (!f)
5518 {
5519 // nm failures are detected by pclose, not popen.
5520 cerr << "Internal error reading symbol table from "
5521 << path << " -- " << strerror (errno);
5522 return info_absent;
5523 }
5524 enum info_status status = read_symbols(f, path);
5525 if (pclose(f) != 0)
5526 {
83ca3872 5527 if (status == info_present && ! sess.suppress_warnings)
5f0a03a6
JK
5528 cerr << "Warning: nm cannot read symbol table from " << path;
5529 return info_absent;
5530 }
5531 return status;
5532}
5533
5534enum info_status
83ca3872
MW
5535symbol_table::read_from_text_file(const string& path,
5536 const systemtap_session &sess)
5f0a03a6
JK
5537{
5538 FILE *f = fopen(path.c_str(), "r");
5539 if (!f)
5540 {
83ca3872
MW
5541 if (! sess.suppress_warnings)
5542 cerr << "Warning: cannot read symbol table from "
5543 << path << " -- " << strerror (errno);
5f0a03a6
JK
5544 return info_absent;
5545 }
5546 enum info_status status = read_symbols(f, path);
5547 (void) fclose(f);
5548 return status;
5549}
5550
46f7b6be
JK
5551void
5552symbol_table::prepare_section_rejection(Dwfl_Module *mod)
5553{
5554#ifdef __powerpc__
5555 /*
5556 * The .opd section contains function descriptors that can look
5557 * just like function entry points. For example, there's a function
5558 * descriptor called "do_exit" that links to the entry point ".do_exit".
5559 * Reject all symbols in .opd.
5560 */
5561 opd_section = SHN_UNDEF;
5562 Dwarf_Addr bias;
5563 Elf* elf = (dwarf_getelf (dwfl_module_getdwarf (mod, &bias))
5564 ?: dwfl_module_getelf (mod, &bias));
5565 Elf_Scn* scn = 0;
5566 size_t shstrndx;
5567
5568 if (!elf)
5569 return;
fcc30d6d 5570 if (elf_getshdrstrndx (elf, &shstrndx) != 0)
46f7b6be
JK
5571 return;
5572 while ((scn = elf_nextscn(elf, scn)) != NULL)
5573 {
5574 GElf_Shdr shdr_mem;
5575 GElf_Shdr *shdr = gelf_getshdr(scn, &shdr_mem);
5576 if (!shdr)
5577 continue;
5578 const char *name = elf_strptr(elf, shstrndx, shdr->sh_name);
5579 if (!strcmp(name, ".opd"))
5580 {
5581 opd_section = elf_ndxscn(scn);
5582 return;
5583 }
5584 }
5585#endif
5586}
5587
5588bool
5589symbol_table::reject_section(GElf_Word section)
5590{
5591 if (section == SHN_UNDEF)
5592 return true;
5593#ifdef __powerpc__
5594 if (section == opd_section)
5595 return true;
5596#endif
5597 return false;
5598}
5599
5f0a03a6
JK
5600enum info_status
5601symbol_table::get_from_elf()
5602{
5603 Dwarf_Addr high_addr = 0;
5604 Dwfl_Module *mod = mod_info->mod;
5605 int syments = dwfl_module_getsymtab(mod);
5606 assert(syments);
46f7b6be 5607 prepare_section_rejection(mod);
5f0a03a6
JK
5608 for (int i = 1; i < syments; ++i)
5609 {
5610 GElf_Sym sym;
ab91b232
JK
5611 GElf_Word section;
5612 const char *name = dwfl_module_getsym(mod, i, &sym, &section);
2867a2a1 5613 if (name && GELF_ST_TYPE(sym.st_info) == STT_FUNC)
ab91b232 5614 add_symbol(name, (GELF_ST_BIND(sym.st_info) == STB_WEAK),
2867a2a1 5615 reject_section(section), sym.st_value, &high_addr);
5f0a03a6
JK
5616 }
5617 return info_present;
5618}
5619
5f0a03a6
JK
5620func_info *
5621symbol_table::get_func_containing_address(Dwarf_Addr addr)
5622{
1c6b77e5
JS
5623 iterator_t iter = map_by_addr.upper_bound(addr);
5624 if (iter == map_by_addr.begin())
5f0a03a6 5625 return NULL;
2e67a43b 5626 else
1c6b77e5 5627 return (--iter)->second;
5f0a03a6
JK
5628}
5629
5630func_info *
5631symbol_table::lookup_symbol(const string& name)
5632{
5633 map<string, func_info*>::iterator i = map_by_name.find(name);
5634 if (i == map_by_name.end())
5635 return NULL;
5636 return i->second;
5637}
5638
5639Dwarf_Addr
5640symbol_table::lookup_symbol_address(const string& name)
5641{
5642 func_info *fi = lookup_symbol(name);
5643 if (fi)
5644 return fi->addr;
5645 return 0;
5646}
5647
ab91b232
JK
5648// This is the kernel symbol table. The kernel macro cond_syscall creates
5649// a weak symbol for each system call and maps it to sys_ni_syscall.
5650// For system calls not implemented elsewhere, this weak symbol shows up
5651// in the kernel symbol table. Following the precedent of dwarfful stap,
5652// we refuse to consider such symbols. Here we delete them from our
5653// symbol table.
5654// TODO: Consider generalizing this and/or making it part of blacklist
5655// processing.
5656void
5657symbol_table::purge_syscall_stubs()
5658{
5659 Dwarf_Addr stub_addr = lookup_symbol_address("sys_ni_syscall");
5660 if (stub_addr == 0)
5661 return;
1c6b77e5 5662 range_t purge_range = map_by_addr.equal_range(stub_addr);
2e67a43b
TM
5663 for (iterator_t iter = purge_range.first;
5664 iter != purge_range.second;
1c6b77e5 5665 )
ab91b232 5666 {
1c6b77e5 5667 func_info *fi = iter->second;
2e67a43b 5668 if (fi->weak && fi->name != "sys_ni_syscall")
ab91b232 5669 {
2e67a43b 5670 map_by_name.erase(fi->name);
1c6b77e5 5671 map_by_addr.erase(iter++);
2e67a43b 5672 delete fi;
2e67a43b 5673 }
1c6b77e5
JS
5674 else
5675 iter++;
ab91b232
JK
5676 }
5677}
5678
5f0a03a6
JK
5679void
5680module_info::get_symtab(dwarf_query *q)
5681{
5682 systemtap_session &sess = q->sess;
5683
1c6b77e5
JS
5684 if (symtab_status != info_unknown)
5685 return;
5686
5f0a03a6
JK
5687 sym_table = new symbol_table(this);
5688 if (!elf_path.empty())
5689 {
83ca3872
MW
5690 if (name == TOK_KERNEL && !sess.kernel_symtab_path.empty()
5691 && ! sess.suppress_warnings)
5f0a03a6
JK
5692 cerr << "Warning: reading symbol table from "
5693 << elf_path
5694 << " -- ignoring "
5695 << sess.kernel_symtab_path
83ca3872 5696 << endl;
5f0a03a6
JK
5697 symtab_status = sym_table->get_from_elf();
5698 }
5699 else
5700 {
5701 assert(name == TOK_KERNEL);
5702 if (sess.kernel_symtab_path.empty())
5703 {
5704 symtab_status = info_absent;
5705 cerr << "Error: Cannot find vmlinux."
5706 << " Consider using --kmap instead of --kelf."
5707 << endl;;
5708 }
5709 else
5710 {
5711 symtab_status =
83ca3872 5712 sym_table->read_from_text_file(sess.kernel_symtab_path, sess);
5f0a03a6
JK
5713 if (symtab_status == info_present)
5714 {
5715 sess.sym_kprobes_text_start =
5716 sym_table->lookup_symbol_address("__kprobes_text_start");
5717 sess.sym_kprobes_text_end =
5718 sym_table->lookup_symbol_address("__kprobes_text_end");
5719 sess.sym_stext = sym_table->lookup_symbol_address("_stext");
5f0a03a6
JK
5720 }
5721 }
5722 }
5723 if (symtab_status == info_absent)
5724 {
5725 delete sym_table;
5726 sym_table = NULL;
5727 return;
5728 }
5729
ab91b232
JK
5730 if (name == TOK_KERNEL)
5731 sym_table->purge_syscall_stubs();
5f0a03a6
JK
5732}
5733
1c6b77e5
JS
5734// update_symtab reconciles data between the elf symbol table and the dwarf
5735// function enumeration. It updates the symbol table entries with the dwarf
5736// die that describes the function, which also signals to query_module_symtab
5737// that a statement probe isn't needed. In return, it also adds aliases to the
5738// function table for names that share the same addr/die.
5739void
5740module_info::update_symtab(cu_function_cache_t *funcs)
5741{
5742 if (!sym_table)
5743 return;
5744
5745 cu_function_cache_t new_funcs;
5746
5747 for (cu_function_cache_t::iterator func = funcs->begin();
5748 func != funcs->end(); func++)
5749 {
5750 // optimization: inlines will never be in the symbol table
5751 if (dwarf_func_inline(&func->second) != 0)
5752 continue;
5753
5754 func_info *fi = sym_table->lookup_symbol(func->first);
5755 if (!fi)
5756 continue;
5757
5758 // iterate over all functions at the same address
5759 symbol_table::range_t er = sym_table->map_by_addr.equal_range(fi->addr);
5760 for (symbol_table::iterator_t it = er.first; it != er.second; ++it)
5761 {
5762 // update this function with the dwarf die
5763 it->second->die = func->second;
5764
5765 // if this function is a new alias, then
5766 // save it to merge into the function cache
5767 if (it->second != fi)
b7478964 5768 new_funcs.insert(make_pair(it->second->name, it->second->die));
1c6b77e5
JS
5769 }
5770 }
5771
5772 // add all discovered aliases back into the function cache
5773 // NB: this won't replace any names that dwarf may have already found
5774 funcs->insert(new_funcs.begin(), new_funcs.end());
5775}
5776
5f0a03a6
JK
5777module_info::~module_info()
5778{
5779 if (sym_table)
5780 delete sym_table;
b55bc428
FCE
5781}
5782
935447c8 5783// ------------------------------------------------------------------------
888af770 5784// user-space probes
935447c8
DS
5785// ------------------------------------------------------------------------
5786
935447c8 5787
888af770 5788struct uprobe_derived_probe_group: public generic_dpg<uprobe_derived_probe>
935447c8 5789{
89ba3085
FCE
5790private:
5791 string make_pbm_key (uprobe_derived_probe* p) {
5792 return p->module + "|" + p->section + "|" + lex_cast(p->pid);
5793 }
5794
935447c8 5795public:
888af770 5796 void emit_module_decls (systemtap_session& s);
935447c8
DS
5797 void emit_module_init (systemtap_session& s);
5798 void emit_module_exit (systemtap_session& s);
5799};
5800
5801
888af770
FCE
5802void
5803uprobe_derived_probe::join_group (systemtap_session& s)
5804{
5805 if (! s.uprobe_derived_probes)
5806 s.uprobe_derived_probes = new uprobe_derived_probe_group ();
5807 s.uprobe_derived_probes->enroll (this);
93646f4d 5808 enable_task_finder(s);
a96d1db0 5809
8a03658e
JS
5810 // Ask buildrun.cxx to build extra module if needed, and
5811 // signal staprun to load that module
5812 s.need_uprobes = true;
a96d1db0
DN
5813}
5814
888af770 5815
2865d17a
DB
5816void
5817uprobe_derived_probe::emit_unprivileged_assertion (translator_output* o)
5818{
5819 // These probes are allowed for unprivileged users, but only in the
5820 // context of processes which they own.
5821 emit_process_owner_assertion (o);
5822}
5823
5824
888af770 5825struct uprobe_builder: public derived_probe_builder
a96d1db0 5826{
888af770 5827 uprobe_builder() {}
a96d1db0
DN
5828 virtual void build(systemtap_session & sess,
5829 probe * base,
5830 probe_point * location,
86bf665e 5831 literal_map_t const & parameters,
a96d1db0
DN
5832 vector<derived_probe *> & finished_results)
5833 {
888af770 5834 int64_t process, address;
a96d1db0 5835
888af770 5836 bool b1 = get_param (parameters, TOK_PROCESS, process);
ced347a9 5837 (void) b1;
888af770 5838 bool b2 = get_param (parameters, TOK_STATEMENT, address);
ced347a9 5839 (void) b2;
888af770
FCE
5840 bool rr = has_null_param (parameters, TOK_RETURN);
5841 assert (b1 && b2); // by pattern_root construction
a96d1db0 5842
0973d815 5843 finished_results.push_back(new uprobe_derived_probe(base, location, process, address, rr));
a96d1db0
DN
5844 }
5845};
5846
5847
5848void
775d51e5 5849uprobe_derived_probe_group::emit_module_decls (systemtap_session& s)
a96d1db0 5850{
888af770 5851 if (probes.empty()) return;
775d51e5 5852 s.op->newline() << "/* ---- user probes ---- */";
471fca5e
TM
5853 // If uprobes isn't in the kernel, pull it in from the runtime.
5854
5855 s.op->newline() << "#if defined(CONFIG_UPROBES) || defined(CONFIG_UPROBES_MODULE)";
5856 s.op->newline() << "#include <linux/uprobes.h>";
5857 s.op->newline() << "#else";
5858 s.op->newline() << "#include \"uprobes/uprobes.h\"";
5859 s.op->newline() << "#endif";
5860 s.op->newline() << "#ifndef UPROBES_API_VERSION";
5861 s.op->newline() << "#define UPROBES_API_VERSION 1";
5862 s.op->newline() << "#endif";
a96d1db0 5863
43241c44
FCE
5864 // We'll probably need at least this many:
5865 unsigned minuprobes = probes.size();
5866 // .. but we don't want so many that .bss is inflated (PR10507):
5867 unsigned uprobesize = 64;
5868 unsigned maxuprobesmem = 10*1024*1024; // 10 MB
5869 unsigned maxuprobes = maxuprobesmem / uprobesize;
5870
aaf7ffe8
FCE
5871 // Let's choose a value on the geometric middle. This should end up
5872 // between minuprobes and maxuprobes. It's OK if this number turns
5873 // out to be < minuprobes or > maxuprobes. At worst, we get a
5874 // run-time error of one kind (too few: missed uprobe registrations)
5875 // or another (too many: vmalloc errors at module load time).
5876 unsigned default_maxuprobes = (unsigned)sqrt((double)minuprobes * (double)maxuprobes);
43241c44 5877
6d0f3f0c 5878 s.op->newline() << "#ifndef MAXUPROBES";
43241c44 5879 s.op->newline() << "#define MAXUPROBES " << default_maxuprobes;
6d0f3f0c 5880 s.op->newline() << "#endif";
a96d1db0 5881
cc52276b
WC
5882 // Forward decls
5883 s.op->newline() << "#include \"uprobes-common.h\"";
5884
5e112f92
FCE
5885 // In .bss, the shared pool of uprobe/uretprobe structs. These are
5886 // too big to embed in the initialized .data stap_uprobe_spec array.
cc52276b
WC
5887 // XXX: consider a slab cache or somesuch for stap_uprobes
5888 s.op->newline() << "static struct stap_uprobe stap_uprobes [MAXUPROBES];";
5e112f92 5889 s.op->newline() << "DEFINE_MUTEX(stap_uprobes_lock);"; // protects against concurrent registration/unregistration
a96d1db0 5890
89ba3085
FCE
5891 s.op->assert_0_indent();
5892
89ba3085
FCE
5893 // Assign task-finder numbers as we build up the stap_uprobe_tf table.
5894 // This means we process probes[] in two passes.
5895 map <string,unsigned> module_index;
5896 unsigned module_index_ctr = 0;
5897
cc52276b
WC
5898 // not const since embedded task_finder_target struct changes
5899 s.op->newline() << "static struct stap_uprobe_tf stap_uprobe_finders[] = {";
89ba3085
FCE
5900 s.op->indent(1);
5901 for (unsigned i=0; i<probes.size(); i++)
5902 {
5903 uprobe_derived_probe *p = probes[i];
5904 string pbmkey = make_pbm_key (p);
5905 if (module_index.find (pbmkey) == module_index.end())
5906 {
5907 module_index[pbmkey] = module_index_ctr++;
5908
5909 s.op->newline() << "{";
5910 // NB: it's essential that make_pbm_key() use all of and
5911 // only the same fields as we're about to emit.
5912 s.op->line() << " .finder={";
5913 if (p->pid != 0)
5914 s.op->line() << " .pid=" << p->pid;
5915 else if (p->section == ".absolute") // proxy for ET_EXEC -> exec()'d program
5916 {
5917 s.op->line() << " .procname=" << lex_cast_qstring(p->module) << ",";
5918 s.op->line() << " .callback=&stap_uprobe_process_found,";
5919 }
aff5d390 5920 if (p->section != ".absolute") // ET_DYN
89ba3085 5921 {
63b4fd14
SC
5922 if (p->has_library && p->sdt_semaphore_addr != 0)
5923 s.op->line() << " .procname=\"" << p->path << "\", ";
89ba3085
FCE
5924 s.op->line() << " .mmap_callback=&stap_uprobe_mmap_found, ";
5925 s.op->line() << " .munmap_callback=&stap_uprobe_munmap_found, ";
19d91f6c 5926 s.op->line() << " .callback=&stap_uprobe_process_munmap,";
89ba3085
FCE
5927 }
5928
5929 s.op->line() << " },";
5930 s.op->line() << " .pathname=" << lex_cast_qstring(p->module) << ", ";
5931 s.op->line() << " },";
5932 }
5933 else
5934 ; // skip it in this pass, already have a suitable stap_uprobe_tf slot for it.
5935 }
5936 s.op->newline(-1) << "};";
5937
5938 s.op->assert_0_indent();
5939
cc52276b
WC
5940 // NB: read-only structure
5941 s.op->newline() << "static const struct stap_uprobe_spec stap_uprobe_specs [] = {";
a96d1db0 5942 s.op->indent(1);
888af770
FCE
5943 for (unsigned i =0; i<probes.size(); i++)
5944 {
5945 uprobe_derived_probe* p = probes[i];
5946 s.op->newline() << "{";
89ba3085
FCE
5947 string key = make_pbm_key (p);
5948 unsigned value = module_index[key];
759e1d76
FCE
5949 if (value != 0)
5950 s.op->line() << " .tfi=" << value << ",";
6b66b9f7 5951 s.op->line() << " .address=(unsigned long)0x" << hex << p->addr << dec << "ULL,";
faea5e16 5952 s.op->line() << " .probe=" << common_probe_init (p) << ",";
4ddb6dd0 5953
038c38c6 5954 if (p->sdt_semaphore_addr != 0)
63b4fd14 5955 s.op->line() << " .sdt_sem_offset=(unsigned long)0x"
038c38c6 5956 << hex << p->sdt_semaphore_addr << dec << "ULL,";
4ddb6dd0
JS
5957
5958 if (p->has_return)
5959 s.op->line() << " .return_p=1,";
888af770
FCE
5960 s.op->line() << " },";
5961 }
5962 s.op->newline(-1) << "};";
a96d1db0 5963
89ba3085
FCE
5964 s.op->assert_0_indent();
5965
48e685da 5966 s.op->newline() << "static void enter_uprobe_probe (struct uprobe *inst, struct pt_regs *regs) {";
888af770 5967 s.op->newline(1) << "struct stap_uprobe *sup = container_of(inst, struct stap_uprobe, up);";
89ba3085 5968 s.op->newline() << "const struct stap_uprobe_spec *sups = &stap_uprobe_specs [sup->spec_index];";
faea5e16 5969 common_probe_entryfn_prologue (s.op, "STAP_SESSION_RUNNING", "sups->probe");
6d0f3f0c
FCE
5970 s.op->newline() << "if (sup->spec_index < 0 ||"
5971 << "sup->spec_index >= " << probes.size() << ") return;"; // XXX: should not happen
a96d1db0 5972 s.op->newline() << "c->regs = regs;";
fd9187da 5973 s.op->newline() << "c->ri = GET_PC_URETPROBE_NONE;";
6415ddde
MW
5974
5975 // Make it look like the IP is set as it would in the actual user
5976 // task when calling real probe handler. Reset IP regs on return, so
5977 // we don't confuse uprobes. PR10458
5978 s.op->newline() << "{";
5979 s.op->indent(1);
5980 s.op->newline() << "unsigned long uprobes_ip = REG_IP(c->regs);";
259d54c0 5981 s.op->newline() << "SET_REG_IP(regs, inst->vaddr);";
faea5e16 5982 s.op->newline() << "(*sups->probe.ph) (c);";
259d54c0 5983 s.op->newline() << "SET_REG_IP(regs, uprobes_ip);";
6415ddde
MW
5984 s.op->newline(-1) << "}";
5985
a96d1db0 5986 common_probe_entryfn_epilogue (s.op);
888af770 5987 s.op->newline(-1) << "}";
a96d1db0 5988
48e685da 5989 s.op->newline() << "static void enter_uretprobe_probe (struct uretprobe_instance *inst, struct pt_regs *regs) {";
888af770 5990 s.op->newline(1) << "struct stap_uprobe *sup = container_of(inst->rp, struct stap_uprobe, urp);";
89ba3085 5991 s.op->newline() << "const struct stap_uprobe_spec *sups = &stap_uprobe_specs [sup->spec_index];";
faea5e16 5992 common_probe_entryfn_prologue (s.op, "STAP_SESSION_RUNNING", "sups->probe");
5e562a69 5993 s.op->newline() << "c->ri = inst;";
6d0f3f0c
FCE
5994 s.op->newline() << "if (sup->spec_index < 0 ||"
5995 << "sup->spec_index >= " << probes.size() << ") return;"; // XXX: should not happen
888af770
FCE
5996 // XXX: kretprobes saves "c->pi = inst;" too
5997 s.op->newline() << "c->regs = regs;";
6415ddde
MW
5998
5999 // Make it look like the IP is set as it would in the actual user
6000 // task when calling real probe handler. Reset IP regs on return, so
6001 // we don't confuse uprobes. PR10458
6002 s.op->newline() << "{";
6003 s.op->indent(1);
6004 s.op->newline() << "unsigned long uprobes_ip = REG_IP(c->regs);";
5e562a69 6005 s.op->newline() << "SET_REG_IP(regs, inst->ret_addr);";
faea5e16 6006 s.op->newline() << "(*sups->probe.ph) (c);";
259d54c0 6007 s.op->newline() << "SET_REG_IP(regs, uprobes_ip);";
6415ddde
MW
6008 s.op->newline(-1) << "}";
6009
888af770 6010 common_probe_entryfn_epilogue (s.op);
a96d1db0
DN
6011 s.op->newline(-1) << "}";
6012
89ba3085 6013 s.op->newline();
cc52276b 6014 s.op->newline() << "#include \"uprobes-common.c\"";
6d0f3f0c 6015 s.op->newline();
888af770 6016}
935447c8
DS
6017
6018
888af770
FCE
6019void
6020uprobe_derived_probe_group::emit_module_init (systemtap_session& s)
935447c8 6021{
888af770 6022 if (probes.empty()) return;
935447c8 6023
5e112f92 6024 s.op->newline() << "/* ---- user probes ---- */";
935447c8 6025
01b05e2e 6026 s.op->newline() << "for (j=0; j<MAXUPROBES; j++) {";
5e112f92
FCE
6027 s.op->newline(1) << "struct stap_uprobe *sup = & stap_uprobes[j];";
6028 s.op->newline() << "sup->spec_index = -1;"; // free slot
80b4ad8b
FCE
6029 // NB: we assume the rest of the struct (specificaly, sup->up) is
6030 // initialized to zero. This is so that we can use
6031 // sup->up->kdata = NULL for "really free!" PR 6829.
5e112f92
FCE
6032 s.op->newline(-1) << "}";
6033 s.op->newline() << "mutex_init (& stap_uprobes_lock);";
935447c8 6034
89ba3085
FCE
6035 // Set up the task_finders
6036 s.op->newline() << "for (i=0; i<sizeof(stap_uprobe_finders)/sizeof(stap_uprobe_finders[0]); i++) {";
6037 s.op->newline(1) << "struct stap_uprobe_tf *stf = & stap_uprobe_finders[i];";
6038 s.op->newline() << "probe_point = stf->pathname;"; // for error messages; XXX: would prefer pp() or something better
6039 s.op->newline() << "rc = stap_register_task_finder_target (& stf->finder);";
935447c8 6040
5e112f92
FCE
6041 // NB: if (rc), there is no need (XXX: nor any way) to clean up any
6042 // finders already registered, since mere registration does not
6043 // cause any utrace or memory allocation actions. That happens only
6044 // later, once the task finder engine starts running. So, for a
6045 // partial initialization requiring unwind, we need do nothing.
6046 s.op->newline() << "if (rc) break;";
a7a68293 6047
888af770
FCE
6048 s.op->newline(-1) << "}";
6049}
d0ea46ce 6050
d0a7f5a9 6051
888af770
FCE
6052void
6053uprobe_derived_probe_group::emit_module_exit (systemtap_session& s)
6054{
6055 if (probes.empty()) return;
6056 s.op->newline() << "/* ---- user probes ---- */";
e56e51c9 6057
6d0f3f0c
FCE
6058 // NB: there is no stap_unregister_task_finder_target call;
6059 // important stuff like utrace cleanups are done by
d41d451c
FCE
6060 // __stp_task_finder_cleanup() via stap_stop_task_finder().
6061 //
6062 // This function blocks until all callbacks are completed, so there
6063 // is supposed to be no possibility of any registration-related code starting
6064 // to run in parallel with our shutdown here. So we don't need to protect the
6065 // stap_uprobes[] array with the mutex.
d0a7f5a9 6066
01b05e2e 6067 s.op->newline() << "for (j=0; j<MAXUPROBES; j++) {";
5e112f92 6068 s.op->newline(1) << "struct stap_uprobe *sup = & stap_uprobes[j];";
89ba3085 6069 s.op->newline() << "const struct stap_uprobe_spec *sups = &stap_uprobe_specs [sup->spec_index];";
6d0f3f0c 6070 s.op->newline() << "if (sup->spec_index < 0) continue;"; // free slot
3568f1dd 6071
8faa1fc5 6072 // PR10655: decrement that ENABLED semaphore
c116c31b 6073 s.op->newline() << "if (sup->sdt_sem_address) {";
8faa1fc5
FCE
6074 s.op->newline(1) << "unsigned short sdt_semaphore;"; // NB: fixed size
6075 s.op->newline() << "pid_t pid = (sups->return_p ? sup->urp.u.pid : sup->up.pid);";
6076 s.op->newline() << "struct task_struct *tsk;";
6077 s.op->newline() << "rcu_read_lock();";
6846cfc8 6078
8faa1fc5
FCE
6079 // XXX: what a gross cut & paste job from tapset/task.stp, just for a lousy pid->task_struct* lookup
6080 s.op->newline() << "#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,31)";
6081 s.op->newline() << " { struct pid *p_pid = find_get_pid(pid);";
6082 s.op->newline() << " tsk = pid_task(p_pid, PIDTYPE_PID);";
6083 s.op->newline() << " put_pid(p_pid); }";
6084 s.op->newline() << "#else";
6085 s.op->newline() << "#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,24)";
6086 s.op->newline() << " tsk = find_task_by_vpid (pid);";
6087 s.op->newline() << "#else";
6088 s.op->newline() << " tsk = find_task_by_pid (pid);";
6089 s.op->newline() << "#endif /* 2.6.24 */";
6090 s.op->newline() << "#endif /* 2.6.31 */";
6091
6092 s.op->newline() << "if (tsk) {"; // just in case the thing exited while we weren't watching
3c5b8e2b 6093 s.op->newline(1) << "if (__access_process_vm_noflush(tsk, sup->sdt_sem_address, &sdt_semaphore, sizeof(sdt_semaphore), 0)) {";
63b4fd14 6094 s.op->newline(1) << "sdt_semaphore --;";
903b9fcd 6095 s.op->newline() << "#ifdef DEBUG_UPROBES";
c116c31b 6096 s.op->newline() << "_stp_dbug (__FUNCTION__,__LINE__, \"-semaphore %#x @ %#lx\\n\", sdt_semaphore, sup->sdt_sem_address);";
903b9fcd 6097 s.op->newline() << "#endif";
3c5b8e2b 6098 s.op->newline() << "__access_process_vm_noflush(tsk, sup->sdt_sem_address, &sdt_semaphore, sizeof(sdt_semaphore), 1);";
93c84191 6099 s.op->newline(-1) << "}";
8faa1fc5
FCE
6100 // XXX: need to analyze possibility of race condition
6101 s.op->newline(-1) << "}";
6102 s.op->newline() << "rcu_read_unlock();";
6103 s.op->newline(-1) << "}";
6846cfc8 6104
3568f1dd
FCE
6105 s.op->newline() << "if (sups->return_p) {";
6106 s.op->newline(1) << "#ifdef DEBUG_UPROBES";
89ba3085 6107 s.op->newline() << "_stp_dbug (__FUNCTION__,__LINE__, \"-uretprobe spec %d index %d pid %d addr %p\\n\", sup->spec_index, j, sup->up.pid, (void*) sup->up.vaddr);";
3568f1dd 6108 s.op->newline() << "#endif";
80b4ad8b
FCE
6109 // NB: PR6829 does not change that we still need to unregister at
6110 // *this* time -- when the script as a whole exits.
3568f1dd
FCE
6111 s.op->newline() << "unregister_uretprobe (& sup->urp);";
6112 s.op->newline(-1) << "} else {";
6113 s.op->newline(1) << "#ifdef DEBUG_UPROBES";
8faa1fc5 6114 s.op->newline() << "_stp_dbug (__FUNCTION__,__LINE__, \"-uprobe spec %d index %d pid %d addr %p\\n\", sup->spec_index, j, sup->up.pid, (void*) sup->up.vaddr);";
3568f1dd
FCE
6115 s.op->newline() << "#endif";
6116 s.op->newline() << "unregister_uprobe (& sup->up);";
6117 s.op->newline(-1) << "}";
935447c8 6118
6d0f3f0c 6119 s.op->newline() << "sup->spec_index = -1;";
935447c8 6120
3568f1dd
FCE
6121 // XXX: uprobe missed counts?
6122
6d0f3f0c 6123 s.op->newline(-1) << "}";
935447c8 6124
5e112f92 6125 s.op->newline() << "mutex_destroy (& stap_uprobes_lock);";
935447c8
DS
6126}
6127
e6fe60e7
AM
6128// ------------------------------------------------------------------------
6129// Kprobe derived probes
6130// ------------------------------------------------------------------------
6131
4627ed58 6132static const string TOK_KPROBE("kprobe");
935447c8 6133
bae55db9 6134struct kprobe_derived_probe: public derived_probe
d0ea46ce 6135{
bae55db9
JS
6136 kprobe_derived_probe (probe *base,
6137 probe_point *location,
6138 const string& name,
6139 int64_t stmt_addr,
6140 bool has_return,
6141 bool has_statement,
6142 bool has_maxactive,
b642c901
SC
6143 bool has_path,
6144 bool has_library,
6145 long maxactive_val,
6146 const string& path,
6147 const string& library
bae55db9
JS
6148 );
6149 string symbol_name;
6150 Dwarf_Addr addr;
6151 bool has_return;
6152 bool has_statement;
6153 bool has_maxactive;
b642c901
SC
6154 bool has_path;
6155 bool has_library;
bae55db9 6156 long maxactive_val;
b642c901
SC
6157 string path;
6158 string library;
bae55db9
JS
6159 bool access_var;
6160 void printsig (std::ostream &o) const;
6161 void join_group (systemtap_session& s);
6162};
d0ea46ce 6163
bae55db9
JS
6164struct kprobe_derived_probe_group: public derived_probe_group
6165{
6166private:
6167 multimap<string,kprobe_derived_probe*> probes_by_module;
6168 typedef multimap<string,kprobe_derived_probe*>::iterator p_b_m_iterator;
d0ea46ce 6169
bae55db9
JS
6170public:
6171 void enroll (kprobe_derived_probe* probe);
6172 void emit_module_decls (systemtap_session& s);
6173 void emit_module_init (systemtap_session& s);
6174 void emit_module_exit (systemtap_session& s);
6175};
d0ea46ce 6176
e6fe60e7
AM
6177kprobe_derived_probe::kprobe_derived_probe (probe *base,
6178 probe_point *location,
b6371390 6179 const string& name,
e6fe60e7 6180 int64_t stmt_addr,
b6371390
JS
6181 bool has_return,
6182 bool has_statement,
6183 bool has_maxactive,
b642c901
SC
6184 bool has_path,
6185 bool has_library,
6186 long maxactive_val,
6187 const string& path,
6188 const string& library
b6371390 6189 ):
e6fe60e7
AM
6190 derived_probe (base, location),
6191 symbol_name (name), addr (stmt_addr),
b6371390 6192 has_return (has_return), has_statement (has_statement),
b642c901
SC
6193 has_maxactive (has_maxactive), has_path (has_path),
6194 has_library (has_library),
6195 maxactive_val (maxactive_val),
6196 path (path), library (library)
e6fe60e7
AM
6197{
6198 this->tok = base->tok;
6199 this->access_var = false;
d0ea46ce 6200
e6fe60e7
AM
6201#ifndef USHRT_MAX
6202#define USHRT_MAX 32767
6203#endif
d0ea46ce 6204
46856d8d
JS
6205 // Expansion of $target variables in the probe body produces an error during
6206 // translate phase, since we're not using debuginfo
d0ea46ce 6207
e6fe60e7 6208 vector<probe_point::component*> comps;
46856d8d 6209 comps.push_back (new probe_point::component(TOK_KPROBE));
e6fe60e7 6210
46856d8d
JS
6211 if (has_statement)
6212 {
9ea68eb9
JS
6213 comps.push_back (new probe_point::component(TOK_STATEMENT,
6214 new literal_number(addr, true)));
46856d8d
JS
6215 comps.push_back (new probe_point::component(TOK_ABSOLUTE));
6216 }
6217 else
6218 {
6219 size_t pos = name.find(':');
6220 if (pos != string::npos)
d0ea46ce 6221 {
46856d8d
JS
6222 string module = name.substr(0, pos);
6223 string function = name.substr(pos + 1);
6224 comps.push_back (new probe_point::component(TOK_MODULE, new literal_string(module)));
6225 comps.push_back (new probe_point::component(TOK_FUNCTION, new literal_string(function)));
6226 }
6227 else
6228 comps.push_back (new probe_point::component(TOK_FUNCTION, new literal_string(name)));
46856d8d 6229 }
d0ea46ce 6230
b6371390
JS
6231 if (has_return)
6232 comps.push_back (new probe_point::component(TOK_RETURN));
6233 if (has_maxactive)
6234 comps.push_back (new probe_point::component(TOK_MAXACTIVE, new literal_number(maxactive_val)));
d0ea46ce 6235
e6fe60e7
AM
6236 this->sole_location()->components = comps;
6237}
d0ea46ce 6238
e6fe60e7
AM
6239void kprobe_derived_probe::printsig (ostream& o) const
6240{
6241 sole_location()->print (o);
6242 o << " /* " << " name = " << symbol_name << "*/";
6243 printsig_nested (o);
6244}
d0ea46ce 6245
e6fe60e7
AM
6246void kprobe_derived_probe::join_group (systemtap_session& s)
6247{
d0ea46ce 6248
e6fe60e7
AM
6249 if (! s.kprobe_derived_probes)
6250 s.kprobe_derived_probes = new kprobe_derived_probe_group ();
6251 s.kprobe_derived_probes->enroll (this);
d0ea46ce 6252
e6fe60e7 6253}
d0ea46ce 6254
e6fe60e7
AM
6255void kprobe_derived_probe_group::enroll (kprobe_derived_probe* p)
6256{
6257 probes_by_module.insert (make_pair (p->symbol_name, p));
6258 // probes of same symbol should share single kprobe/kretprobe
6259}
d0ea46ce 6260
e6fe60e7
AM
6261void
6262kprobe_derived_probe_group::emit_module_decls (systemtap_session& s)
6263{
6264 if (probes_by_module.empty()) return;
d0ea46ce 6265
e6fe60e7 6266 s.op->newline() << "/* ---- kprobe-based probes ---- */";
d0ea46ce 6267
e6fe60e7
AM
6268 // Warn of misconfigured kernels
6269 s.op->newline() << "#if ! defined(CONFIG_KPROBES)";
6270 s.op->newline() << "#error \"Need CONFIG_KPROBES!\"";
6271 s.op->newline() << "#endif";
6272 s.op->newline();
d0ea46ce 6273
f07c3b68 6274 s.op->newline() << "#ifndef KRETACTIVE";
1ee6b5fc 6275 s.op->newline() << "#define KRETACTIVE (max(15,6*(int)num_possible_cpus()))";
f07c3b68
FCE
6276 s.op->newline() << "#endif";
6277
e6fe60e7 6278 // Forward declare the master entry functions
88747011 6279 s.op->newline() << "static int enter_kprobe2_probe (struct kprobe *inst,";
e6fe60e7 6280 s.op->line() << " struct pt_regs *regs);";
88747011 6281 s.op->newline() << "static int enter_kretprobe2_probe (struct kretprobe_instance *inst,";
e6fe60e7 6282 s.op->line() << " struct pt_regs *regs);";
d0ea46ce 6283
e6fe60e7
AM
6284 // Emit an array of kprobe/kretprobe pointers
6285 s.op->newline() << "#if defined(STAPCONF_UNREGISTER_KPROBES)";
c9116e99 6286 s.op->newline() << "static void * stap_unreg_kprobes2[" << probes_by_module.size() << "];";
e6fe60e7 6287 s.op->newline() << "#endif";
d0ea46ce 6288
e6fe60e7 6289 // Emit the actual probe list.
d0ea46ce 6290
e6fe60e7
AM
6291 s.op->newline() << "static struct stap_dwarfless_kprobe {";
6292 s.op->newline(1) << "union { struct kprobe kp; struct kretprobe krp; } u;";
6293 s.op->newline() << "#ifdef __ia64__";
6294 s.op->newline() << "struct kprobe dummy;";
6295 s.op->newline() << "#endif";
6296 s.op->newline(-1) << "} stap_dwarfless_kprobes[" << probes_by_module.size() << "];";
6297 // NB: bss!
d0ea46ce 6298
e6fe60e7
AM
6299 s.op->newline() << "static struct stap_dwarfless_probe {";
6300 s.op->newline(1) << "const unsigned return_p:1;";
6301 s.op->newline() << "const unsigned maxactive_p:1;";
b350f56b 6302 s.op->newline() << "const unsigned optional_p:1;";
e6fe60e7
AM
6303 s.op->newline() << "unsigned registered_p:1;";
6304 s.op->newline() << "const unsigned short maxactive_val;";
935447c8 6305
e6fe60e7
AM
6306 // Function Names are mostly small and uniform enough to justify putting
6307 // char[MAX]'s into the array instead of relocated char*'s.
935447c8 6308
faea5e16
JS
6309 size_t symbol_string_name_max = 0;
6310 size_t symbol_string_name_tot = 0;
e6fe60e7 6311 for (p_b_m_iterator it = probes_by_module.begin(); it != probes_by_module.end(); it++)
6270adc1 6312 {
e6fe60e7
AM
6313 kprobe_derived_probe* p = it->second;
6314#define DOIT(var,expr) do { \
6315 size_t var##_size = (expr) + 1; \
6316 var##_max = max (var##_max, var##_size); \
6317 var##_tot += var##_size; } while (0)
e6fe60e7
AM
6318 DOIT(symbol_string_name, p->symbol_name.size());
6319#undef DOIT
6270adc1
MH
6320 }
6321
e6fe60e7
AM
6322#define CALCIT(var) \
6323 s.op->newline() << "const char " << #var << "[" << var##_name_max << "] ;";
935447c8 6324
e6fe60e7
AM
6325 CALCIT(symbol_string);
6326#undef CALCIT
6270adc1 6327
bd659351 6328 s.op->newline() << "unsigned long address;";
faea5e16 6329 s.op->newline() << "struct stap_probe probe;";
e6fe60e7
AM
6330 s.op->newline(-1) << "} stap_dwarfless_probes[] = {";
6331 s.op->indent(1);
6270adc1 6332
e6fe60e7
AM
6333 for (p_b_m_iterator it = probes_by_module.begin(); it != probes_by_module.end(); it++)
6334 {
6335 kprobe_derived_probe* p = it->second;
6336 s.op->newline() << "{";
6337 if (p->has_return)
6338 s.op->line() << " .return_p=1,";
6270adc1 6339
e6fe60e7
AM
6340 if (p->has_maxactive)
6341 {
6342 s.op->line() << " .maxactive_p=1,";
6343 assert (p->maxactive_val >= 0 && p->maxactive_val <= USHRT_MAX);
6344 s.op->line() << " .maxactive_val=" << p->maxactive_val << ",";
6345 }
6270adc1 6346
b350f56b
JS
6347 if (p->locations[0]->optional)
6348 s.op->line() << " .optional_p=1,";
6349
e6fe60e7 6350 if (p->has_statement)
c8d9d15e 6351 s.op->line() << " .address=(unsigned long)0x" << hex << p->addr << dec << "ULL,";
e6fe60e7 6352 else
c8d9d15e 6353 s.op->line() << " .symbol_string=\"" << p->symbol_name << "\",";
5d67b47c 6354
faea5e16 6355 s.op->line() << " .probe=" << common_probe_init (p) << ",";
e6fe60e7 6356 s.op->line() << " },";
935447c8
DS
6357 }
6358
e6fe60e7 6359 s.op->newline(-1) << "};";
5d67b47c 6360
e6fe60e7
AM
6361 // Emit the kprobes callback function
6362 s.op->newline();
88747011 6363 s.op->newline() << "static int enter_kprobe2_probe (struct kprobe *inst,";
e6fe60e7
AM
6364 s.op->line() << " struct pt_regs *regs) {";
6365 // NB: as of PR5673, the kprobe|kretprobe union struct is in BSS
6366 s.op->newline(1) << "int kprobe_idx = ((uintptr_t)inst-(uintptr_t)stap_dwarfless_kprobes)/sizeof(struct stap_dwarfless_kprobe);";
6367 // Check that the index is plausible
6368 s.op->newline() << "struct stap_dwarfless_probe *sdp = &stap_dwarfless_probes[";
6369 s.op->line() << "((kprobe_idx >= 0 && kprobe_idx < " << probes_by_module.size() << ")?";
6370 s.op->line() << "kprobe_idx:0)"; // NB: at least we avoid memory corruption
6371 // XXX: it would be nice to give a more verbose error though; BUG_ON later?
6372 s.op->line() << "];";
faea5e16 6373 common_probe_entryfn_prologue (s.op, "STAP_SESSION_RUNNING", "sdp->probe");
e6fe60e7 6374 s.op->newline() << "c->regs = regs;";
6415ddde
MW
6375
6376 // Make it look like the IP is set as it wouldn't have been replaced
6377 // by a breakpoint instruction when calling real probe handler. Reset
6378 // IP regs on return, so we don't confuse kprobes. PR10458
6379 s.op->newline() << "{";
6380 s.op->indent(1);
6381 s.op->newline() << "unsigned long kprobes_ip = REG_IP(c->regs);";
259d54c0 6382 s.op->newline() << "SET_REG_IP(regs, (unsigned long) inst->addr);";
faea5e16 6383 s.op->newline() << "(*sdp->probe.ph) (c);";
259d54c0 6384 s.op->newline() << "SET_REG_IP(regs, kprobes_ip);";
6415ddde
MW
6385 s.op->newline(-1) << "}";
6386
e6fe60e7
AM
6387 common_probe_entryfn_epilogue (s.op);
6388 s.op->newline() << "return 0;";
6389 s.op->newline(-1) << "}";
935447c8 6390
e6fe60e7
AM
6391 // Same for kretprobes
6392 s.op->newline();
88747011 6393 s.op->newline() << "static int enter_kretprobe2_probe (struct kretprobe_instance *inst,";
e6fe60e7
AM
6394 s.op->line() << " struct pt_regs *regs) {";
6395 s.op->newline(1) << "struct kretprobe *krp = inst->rp;";
935447c8 6396
e6fe60e7
AM
6397 // NB: as of PR5673, the kprobe|kretprobe union struct is in BSS
6398 s.op->newline() << "int kprobe_idx = ((uintptr_t)krp-(uintptr_t)stap_dwarfless_kprobes)/sizeof(struct stap_dwarfless_kprobe);";
6399 // Check that the index is plausible
6400 s.op->newline() << "struct stap_dwarfless_probe *sdp = &stap_dwarfless_probes[";
6401 s.op->line() << "((kprobe_idx >= 0 && kprobe_idx < " << probes_by_module.size() << ")?";
6402 s.op->line() << "kprobe_idx:0)"; // NB: at least we avoid memory corruption
6403 // XXX: it would be nice to give a more verbose error though; BUG_ON later?
6404 s.op->line() << "];";
935447c8 6405
faea5e16 6406 common_probe_entryfn_prologue (s.op, "STAP_SESSION_RUNNING", "sdp->probe");
e6fe60e7
AM
6407 s.op->newline() << "c->regs = regs;";
6408 s.op->newline() << "c->pi = inst;"; // for assisting runtime's backtrace logic
6415ddde
MW
6409
6410 // Make it look like the IP is set as it wouldn't have been replaced
6411 // by a breakpoint instruction when calling real probe handler. Reset
6412 // IP regs on return, so we don't confuse kprobes. PR10458
6413 s.op->newline() << "{";
6414 s.op->indent(1);
6415 s.op->newline() << "unsigned long kprobes_ip = REG_IP(c->regs);";
259d54c0 6416 s.op->newline() << "SET_REG_IP(regs, (unsigned long) inst->rp->kp.addr);";
faea5e16 6417 s.op->newline() << "(*sdp->probe.ph) (c);";
259d54c0 6418 s.op->newline() << "SET_REG_IP(regs, kprobes_ip);";
6415ddde
MW
6419 s.op->newline(-1) << "}";
6420
e6fe60e7
AM
6421 common_probe_entryfn_epilogue (s.op);
6422 s.op->newline() << "return 0;";
6423 s.op->newline(-1) << "}";
bd659351 6424
03a4ec63 6425 s.op->newline() << "#ifdef STAPCONF_KALLSYMS_ON_EACH_SYMBOL";
bd659351
MW
6426 s.op->newline() << "static int kprobe_resolve(void *data, const char *name,";
6427 s.op->newline() << " struct module *owner,";
6428 s.op->newline() << " unsigned long val) {";
6429 s.op->newline(1) << "int i;";
fc1d2aa2
MW
6430 s.op->newline() << "int *p = (int *) data;";
6431 s.op->newline() << "for (i=0; i<" << probes_by_module.size()
6432 << " && *p > 0; i++) {";
bd659351
MW
6433 s.op->newline(1) << "struct stap_dwarfless_probe *sdp = & stap_dwarfless_probes[i];";
6434 s.op->newline() << "if (! sdp->address)";
fc1d2aa2 6435 s.op->newline(1) << "if (strcmp(sdp->symbol_string, name) == 0) {";
bd659351 6436 s.op->newline(1) << "sdp->address = val;";
fc1d2aa2
MW
6437 s.op->newline() << "(*p)--;";
6438 s.op->newline(-1) << "}";
6439 s.op->newline(-2) << "}";
6440 s.op->newline() << "return (p > 0) ? 0 : -1;";
bd659351 6441 s.op->newline(-1) << "}";
03a4ec63 6442 s.op->newline() << "#endif";
935447c8
DS
6443}
6444
e6fe60e7 6445
6270adc1 6446void
e6fe60e7 6447kprobe_derived_probe_group::emit_module_init (systemtap_session& s)
6270adc1 6448{
03a4ec63 6449 s.op->newline() << "#ifdef STAPCONF_KALLSYMS_ON_EACH_SYMBOL";
fc1d2aa2
MW
6450 s.op->newline() << "{";
6451 s.op->newline(1) << "int p = 0;";
6452 s.op->newline() << "for (i = 0; i < " << probes_by_module.size() << "; i++) {";
6453 s.op->newline(1) << "struct stap_dwarfless_probe *sdp = & stap_dwarfless_probes[i];";
6454 s.op->newline() << "if (! sdp->address)";
6455 s.op->newline(1) << "p++;";
6456 s.op->newline(-2) << "}";
6457 s.op->newline() << "kallsyms_on_each_symbol(kprobe_resolve, &p);";
6458 s.op->newline(-1) << "}";
03a4ec63 6459 s.op->newline() << "#endif";
bd659351 6460
e6fe60e7 6461 s.op->newline() << "for (i=0; i<" << probes_by_module.size() << "; i++) {";
c8d9d15e 6462 s.op->newline(1) << "struct stap_dwarfless_probe *sdp = & stap_dwarfless_probes[i];";
e6fe60e7 6463 s.op->newline() << "struct stap_dwarfless_kprobe *kp = & stap_dwarfless_kprobes[i];";
c8d9d15e 6464 s.op->newline() << "void *addr = (void *) sdp->address;";
03a4ec63
MW
6465 s.op->newline() << "const char *symbol_name = addr ? NULL : sdp->symbol_string;";
6466
6467 s.op->newline() << "#ifdef STAPCONF_KALLSYMS_ON_EACH_SYMBOL";
bd659351
MW
6468 s.op->newline() << "if (! addr) {";
6469 s.op->newline(1) << "sdp->registered_p = 0;";
9319b767
MW
6470 s.op->newline() << "if (!sdp->optional_p)";
6471 s.op->newline(1) << "_stp_warn (\"probe %s registration error (symbol not found)\", probe_point);";
6472 s.op->newline(-1) << "continue;";
bd659351 6473 s.op->newline(-1) << "}";
03a4ec63
MW
6474 s.op->newline() << "#endif";
6475
faea5e16 6476 s.op->newline() << "probe_point = sdp->probe.pp;"; // for error messages
e6fe60e7 6477 s.op->newline() << "if (sdp->return_p) {";
c8d9d15e 6478 s.op->newline(1) << "kp->u.krp.kp.addr = addr;";
03a4ec63 6479 s.op->newline() << "kp->u.krp.kp.symbol_name = (char *) symbol_name;";
e6fe60e7
AM
6480 s.op->newline() << "if (sdp->maxactive_p) {";
6481 s.op->newline(1) << "kp->u.krp.maxactive = sdp->maxactive_val;";
6482 s.op->newline(-1) << "} else {";
f07c3b68 6483 s.op->newline(1) << "kp->u.krp.maxactive = KRETACTIVE;";
e6fe60e7 6484 s.op->newline(-1) << "}";
88747011 6485 s.op->newline() << "kp->u.krp.handler = &enter_kretprobe2_probe;";
e6fe60e7
AM
6486 // to ensure safeness of bspcache, always use aggr_kprobe on ia64
6487 s.op->newline() << "#ifdef __ia64__";
e6fe60e7 6488 s.op->newline() << "kp->dummy.addr = kp->u.krp.kp.addr;";
c8d9d15e
JS
6489 s.op->newline() << "kp->dummy.symbol_name = kp->u.krp.kp.symbol_name;";
6490 s.op->newline() << "kp->dummy.pre_handler = NULL;";
e6fe60e7
AM
6491 s.op->newline() << "rc = register_kprobe (& kp->dummy);";
6492 s.op->newline() << "if (rc == 0) {";
6493 s.op->newline(1) << "rc = register_kretprobe (& kp->u.krp);";
6494 s.op->newline() << "if (rc != 0)";
6495 s.op->newline(1) << "unregister_kprobe (& kp->dummy);";
6496 s.op->newline(-2) << "}";
6497 s.op->newline() << "#else";
6498 s.op->newline() << "rc = register_kretprobe (& kp->u.krp);";
6499 s.op->newline() << "#endif";
6500 s.op->newline(-1) << "} else {";
6501 // to ensure safeness of bspcache, always use aggr_kprobe on ia64
c8d9d15e 6502 s.op->newline(1) << "kp->u.kp.addr = addr;";
03a4ec63 6503 s.op->newline() << "kp->u.kp.symbol_name = (char *) symbol_name;";
88747011 6504 s.op->newline() << "kp->u.kp.pre_handler = &enter_kprobe2_probe;";
e6fe60e7 6505 s.op->newline() << "#ifdef __ia64__";
e6fe60e7 6506 s.op->newline() << "kp->dummy.pre_handler = NULL;";
c8d9d15e
JS
6507 s.op->newline() << "kp->dummy.addr = kp->u.kp.addr;";
6508 s.op->newline() << "kp->dummy.symbol_name = kp->u.kp.symbol_name;";
e6fe60e7
AM
6509 s.op->newline() << "rc = register_kprobe (& kp->dummy);";
6510 s.op->newline() << "if (rc == 0) {";
6511 s.op->newline(1) << "rc = register_kprobe (& kp->u.kp);";
6512 s.op->newline() << "if (rc != 0)";
6513 s.op->newline(1) << "unregister_kprobe (& kp->dummy);";
6514 s.op->newline(-2) << "}";
6515 s.op->newline() << "#else";
6516 s.op->newline() << "rc = register_kprobe (& kp->u.kp);";
6517 s.op->newline() << "#endif";
6518 s.op->newline(-1) << "}";
6519 s.op->newline() << "if (rc) {"; // PR6749: tolerate a failed register_*probe.
6520 s.op->newline(1) << "sdp->registered_p = 0;";
b350f56b 6521 s.op->newline() << "if (!sdp->optional_p)";
50b6acf7 6522 s.op->newline(1) << "_stp_warn (\"probe %s (address 0x%lx) registration error (rc %d)\", probe_point, (unsigned long) addr, rc);";
b350f56b 6523 s.op->newline(-1) << "rc = 0;"; // continue with other probes
e6fe60e7
AM
6524 // XXX: shall we increment numskipped?
6525 s.op->newline(-1) << "}";
6270adc1 6526
e6fe60e7
AM
6527 s.op->newline() << "else sdp->registered_p = 1;";
6528 s.op->newline(-1) << "}"; // for loop
6270adc1
MH
6529}
6530
e6fe60e7
AM
6531void
6532kprobe_derived_probe_group::emit_module_exit (systemtap_session& s)
935447c8 6533{
e6fe60e7
AM
6534 //Unregister kprobes by batch interfaces.
6535 s.op->newline() << "#if defined(STAPCONF_UNREGISTER_KPROBES)";
6536 s.op->newline() << "j = 0;";
6537 s.op->newline() << "for (i=0; i<" << probes_by_module.size() << "; i++) {";
6538 s.op->newline(1) << "struct stap_dwarfless_probe *sdp = & stap_dwarfless_probes[i];";
6539 s.op->newline() << "struct stap_dwarfless_kprobe *kp = & stap_dwarfless_kprobes[i];";
6540 s.op->newline() << "if (! sdp->registered_p) continue;";
6541 s.op->newline() << "if (!sdp->return_p)";
c9116e99 6542 s.op->newline(1) << "stap_unreg_kprobes2[j++] = &kp->u.kp;";
e6fe60e7 6543 s.op->newline(-2) << "}";
c9116e99 6544 s.op->newline() << "unregister_kprobes((struct kprobe **)stap_unreg_kprobes2, j);";
e6fe60e7
AM
6545 s.op->newline() << "j = 0;";
6546 s.op->newline() << "for (i=0; i<" << probes_by_module.size() << "; i++) {";
6547 s.op->newline(1) << "struct stap_dwarfless_probe *sdp = & stap_dwarfless_probes[i];";
6548 s.op->newline() << "struct stap_dwarfless_kprobe *kp = & stap_dwarfless_kprobes[i];";
6549 s.op->newline() << "if (! sdp->registered_p) continue;";
6550 s.op->newline() << "if (sdp->return_p)";
c9116e99 6551 s.op->newline(1) << "stap_unreg_kprobes2[j++] = &kp->u.krp;";
e6fe60e7 6552 s.op->newline(-2) << "}";
c9116e99 6553 s.op->newline() << "unregister_kretprobes((struct kretprobe **)stap_unreg_kprobes2, j);";
e6fe60e7
AM
6554 s.op->newline() << "#ifdef __ia64__";
6555 s.op->newline() << "j = 0;";
6556 s.op->newline() << "for (i=0; i<" << probes_by_module.size() << "; i++) {";
6557 s.op->newline(1) << "struct stap_dwarfless_probe *sdp = & stap_dwarfless_probes[i];";
6558 s.op->newline() << "struct stap_dwarfless_kprobe *kp = & stap_dwarfless_kprobes[i];";
6559 s.op->newline() << "if (! sdp->registered_p) continue;";
c9116e99 6560 s.op->newline() << "stap_unreg_kprobes2[j++] = &kp->dummy;";
e6fe60e7 6561 s.op->newline(-1) << "}";
c9116e99 6562 s.op->newline() << "unregister_kprobes((struct kprobe **)stap_unreg_kprobes2, j);";
e6fe60e7
AM
6563 s.op->newline() << "#endif";
6564 s.op->newline() << "#endif";
3e3bd7b6 6565
e6fe60e7
AM
6566 s.op->newline() << "for (i=0; i<" << probes_by_module.size() << "; i++) {";
6567 s.op->newline(1) << "struct stap_dwarfless_probe *sdp = & stap_dwarfless_probes[i];";
6568 s.op->newline() << "struct stap_dwarfless_kprobe *kp = & stap_dwarfless_kprobes[i];";
6569 s.op->newline() << "if (! sdp->registered_p) continue;";
6570 s.op->newline() << "if (sdp->return_p) {";
6571 s.op->newline() << "#if !defined(STAPCONF_UNREGISTER_KPROBES)";
6572 s.op->newline(1) << "unregister_kretprobe (&kp->u.krp);";
6573 s.op->newline() << "#endif";
6574 s.op->newline() << "atomic_add (kp->u.krp.nmissed, & skipped_count);";
6575 s.op->newline() << "#ifdef STP_TIMING";
6576 s.op->newline() << "if (kp->u.krp.nmissed)";
faea5e16 6577 s.op->newline(1) << "_stp_warn (\"Skipped due to missed kretprobe/1 on '%s': %d\\n\", sdp->probe.pp, kp->u.krp.nmissed);";
e6fe60e7
AM
6578 s.op->newline(-1) << "#endif";
6579 s.op->newline() << "atomic_add (kp->u.krp.kp.nmissed, & skipped_count);";
6580 s.op->newline() << "#ifdef STP_TIMING";
6581 s.op->newline() << "if (kp->u.krp.kp.nmissed)";
faea5e16 6582 s.op->newline(1) << "_stp_warn (\"Skipped due to missed kretprobe/2 on '%s': %lu\\n\", sdp->probe.pp, kp->u.krp.kp.nmissed);";
e6fe60e7
AM
6583 s.op->newline(-1) << "#endif";
6584 s.op->newline(-1) << "} else {";
6585 s.op->newline() << "#if !defined(STAPCONF_UNREGISTER_KPROBES)";
6586 s.op->newline(1) << "unregister_kprobe (&kp->u.kp);";
6587 s.op->newline() << "#endif";
6588 s.op->newline() << "atomic_add (kp->u.kp.nmissed, & skipped_count);";
6589 s.op->newline() << "#ifdef STP_TIMING";
6590 s.op->newline() << "if (kp->u.kp.nmissed)";
faea5e16 6591 s.op->newline(1) << "_stp_warn (\"Skipped due to missed kprobe on '%s': %lu\\n\", sdp->probe.pp, kp->u.kp.nmissed);";
e6fe60e7
AM
6592 s.op->newline(-1) << "#endif";
6593 s.op->newline(-1) << "}";
6594 s.op->newline() << "#if !defined(STAPCONF_UNREGISTER_KPROBES) && defined(__ia64__)";
6595 s.op->newline() << "unregister_kprobe (&kp->dummy);";
6596 s.op->newline() << "#endif";
6597 s.op->newline() << "sdp->registered_p = 0;";
6598 s.op->newline(-1) << "}";
f8a968bc
JS
6599}
6600
e6fe60e7 6601struct kprobe_builder: public derived_probe_builder
3c1b3d06 6602{
e6fe60e7
AM
6603 kprobe_builder() {}
6604 virtual void build(systemtap_session & sess,
6605 probe * base,
6606 probe_point * location,
6607 literal_map_t const & parameters,
6608 vector<derived_probe *> & finished_results);
6609};
3c1b3d06
FCE
6610
6611
79189b84 6612void
e6fe60e7
AM
6613kprobe_builder::build(systemtap_session & sess,
6614 probe * base,
6615 probe_point * location,
6616 literal_map_t const & parameters,
6617 vector<derived_probe *> & finished_results)
79189b84 6618{
e6fe60e7 6619 string function_string_val, module_string_val;
b642c901 6620 string path, library;
b6371390
JS
6621 int64_t statement_num_val = 0, maxactive_val = 0;
6622 bool has_function_str, has_module_str, has_statement_num;
6623 bool has_absolute, has_return, has_maxactive;
b642c901 6624 bool has_path, has_library;
79189b84 6625
b6371390
JS
6626 has_function_str = get_param(parameters, TOK_FUNCTION, function_string_val);
6627 has_module_str = get_param(parameters, TOK_MODULE, module_string_val);
6628 has_return = has_null_param (parameters, TOK_RETURN);
6629 has_maxactive = get_param(parameters, TOK_MAXACTIVE, maxactive_val);
6630 has_statement_num = get_param(parameters, TOK_STATEMENT, statement_num_val);
6631 has_absolute = has_null_param (parameters, TOK_ABSOLUTE);
b642c901
SC
6632 has_path = get_param (parameters, TOK_PROCESS, path);
6633 has_library = get_param (parameters, TOK_LIBRARY, library);
6634
6635 if (has_path)
6636 path = find_executable (path);
6637 if (has_library)
6638 library = find_executable (library, "LD_LIBRARY_PATH");
6639
b6371390 6640 if (has_function_str)
6fb70fb7 6641 {
b6371390
JS
6642 if (has_module_str)
6643 function_string_val = module_string_val + ":" + function_string_val;
86758d5f 6644
b6371390
JS
6645 finished_results.push_back (new kprobe_derived_probe (base,
6646 location, function_string_val,
6647 0, has_return,
6648 has_statement_num,
6649 has_maxactive,
b642c901
SC
6650 has_path,
6651 has_library,
6652 maxactive_val,
6653 path,
6654 library));
6fb70fb7 6655 }
e6fe60e7 6656 else
b6371390
JS
6657 {
6658 // assert guru mode for absolute probes
6659 if ( has_statement_num && has_absolute && !base->privileged )
6660 throw semantic_error ("absolute statement probe in unprivileged script", base->tok);
6661
6662 finished_results.push_back (new kprobe_derived_probe (base,
6663 location, "",
6664 statement_num_val,
6665 has_return,
6666 has_statement_num,
6667 has_maxactive,
b642c901
SC
6668 has_path,
6669 has_library,
6670 maxactive_val,
6671 path,
6672 library));
96b030fe 6673 }
79189b84
JS
6674}
6675
dd225250
PS
6676// ------------------------------------------------------------------------
6677// Hardware breakpoint based probes.
6678// ------------------------------------------------------------------------
6679
6680static const string TOK_HWBKPT("data");
6681static const string TOK_HWBKPT_WRITE("write");
6682static const string TOK_HWBKPT_RW("rw");
6683static const string TOK_LENGTH("length");
6684
6685#define HWBKPT_READ 0
6686#define HWBKPT_WRITE 1
6687#define HWBKPT_RW 2
6688struct hwbkpt_derived_probe: public derived_probe
6689{
6690 hwbkpt_derived_probe (probe *base,
6691 probe_point *location,
6692 uint64_t addr,
6693 string symname,
6694 unsigned int len,
6695 bool has_only_read_access,
6696 bool has_only_write_access,
6697 bool has_rw_access
6698 );
6699 Dwarf_Addr hwbkpt_addr;
6700 string symbol_name;
6701 unsigned int hwbkpt_access,hwbkpt_len;
6702
6703 void printsig (std::ostream &o) const;
6704 void join_group (systemtap_session& s);
6705};
6706
6707struct hwbkpt_derived_probe_group: public derived_probe_group
6708{
dd225250 6709private:
dac77b80 6710 vector<hwbkpt_derived_probe*> hwbkpt_probes;
dd225250
PS
6711
6712public:
6713 void enroll (hwbkpt_derived_probe* probe, systemtap_session& s);
6714 void emit_module_decls (systemtap_session& s);
6715 void emit_module_init (systemtap_session& s);
6716 void emit_module_exit (systemtap_session& s);
6717};
6718
6719hwbkpt_derived_probe::hwbkpt_derived_probe (probe *base,
9ea68eb9
JS
6720 probe_point *location,
6721 uint64_t addr,
6722 string symname,
6723 unsigned int len,
6724 bool has_only_read_access,
6725 bool has_only_write_access,
6726 bool has_rw_access
6727 ):
dd225250
PS
6728 derived_probe (base, location),
6729 hwbkpt_addr (addr),
6730 symbol_name (symname),
6731 hwbkpt_len (len)
6732{
6733 this->tok = base->tok;
6734
6735 vector<probe_point::component*> comps;
6736 comps.push_back (new probe_point::component(TOK_KERNEL));
6737
6738 if (hwbkpt_addr)
9ea68eb9
JS
6739 comps.push_back (new probe_point::component (TOK_HWBKPT,
6740 new literal_number(hwbkpt_addr, true)));
6741 else if (symbol_name.size())
6742 comps.push_back (new probe_point::component (TOK_HWBKPT, new literal_string(symbol_name)));
dd225250
PS
6743
6744 comps.push_back (new probe_point::component (TOK_LENGTH, new literal_number(hwbkpt_len)));
6745
6746 if (has_only_read_access)
9ea68eb9 6747 this->hwbkpt_access = HWBKPT_READ ;
dd225250
PS
6748//TODO add code for comps.push_back for read, since this flag is not for x86
6749
6750 else
9ea68eb9
JS
6751 {
6752 if (has_only_write_access)
6753 {
6754 this->hwbkpt_access = HWBKPT_WRITE ;
6755 comps.push_back (new probe_point::component(TOK_HWBKPT_WRITE));
6756 }
6757 else
6758 {
6759 this->hwbkpt_access = HWBKPT_RW ;
6760 comps.push_back (new probe_point::component(TOK_HWBKPT_RW));
6761 }
6762 }
dd225250
PS
6763
6764 this->sole_location()->components = comps;
6765}
6766
6767void hwbkpt_derived_probe::printsig (ostream& o) const
6768{
6769 sole_location()->print (o);
6770 printsig_nested (o);
6771}
6772
6773void hwbkpt_derived_probe::join_group (systemtap_session& s)
6774{
dac77b80
FCE
6775 if (! s.hwbkpt_derived_probes)
6776 s.hwbkpt_derived_probes = new hwbkpt_derived_probe_group ();
dd225250
PS
6777 s.hwbkpt_derived_probes->enroll (this, s);
6778}
6779
6780void hwbkpt_derived_probe_group::enroll (hwbkpt_derived_probe* p, systemtap_session& s)
6781{
dac77b80
FCE
6782 hwbkpt_probes.push_back (p);
6783
6784 unsigned max_hwbkpt_probes_by_arch = 0;
6785 if (s.architecture == "i386" || s.architecture == "x86_64")
6786 max_hwbkpt_probes_by_arch = 4;
6787 else if (s.architecture == "s390")
6788 max_hwbkpt_probes_by_arch = 1;
6789
6790 if (hwbkpt_probes.size() >= max_hwbkpt_probes_by_arch)
6791 if (! s.suppress_warnings)
6792 s.print_warning ("Too many hardware breakpoint probes requested for " + s.architecture
6793 + "(" + lex_cast(hwbkpt_probes.size()) +
6794 " vs. " + lex_cast(max_hwbkpt_probes_by_arch) + ")");
dd225250
PS
6795}
6796
6797void
6798hwbkpt_derived_probe_group::emit_module_decls (systemtap_session& s)
6799{
dac77b80 6800 if (hwbkpt_probes.empty()) return;
dd225250
PS
6801
6802 s.op->newline() << "/* ---- hwbkpt-based probes ---- */";
6803
6804 s.op->newline() << "#include <linux/perf_event.h>";
6805 s.op->newline() << "#include <linux/hw_breakpoint.h>";
6806 s.op->newline();
6807
6808 // Forward declare the master entry functions
6809 s.op->newline() << "static int enter_hwbkpt_probe (struct perf_event *bp,";
6810 s.op->line() << " int nmi,";
6811 s.op->line() << " struct perf_sample_data *data,";
6812 s.op->line() << " struct pt_regs *regs);";
79189b84 6813
dd225250
PS
6814 // Emit the actual probe list.
6815
6816 s.op->newline() << "static struct perf_event_attr ";
dac77b80 6817 s.op->newline() << "stap_hwbkpt_probe_array[" << hwbkpt_probes.size() << "];";
dd225250
PS
6818
6819 s.op->newline() << "static struct perf_event **";
dac77b80 6820 s.op->newline() << "stap_hwbkpt_ret_array[" << hwbkpt_probes.size() << "];";
dd225250
PS
6821 s.op->newline() << "static struct stap_hwbkpt_probe {";
6822 s.op->newline() << "int registered_p:1;";
6823// registered_p = 0 signifies a probe that failed registration
6824// registered_p = 1 signifies a probe that got registered successfully
6825
faea5e16 6826 // Symbol Names are mostly small and uniform enough
dd225250 6827 // to justify putting const char*.
dac77b80 6828 s.op->newline() << "const char * const symbol;";
dd225250
PS
6829
6830 s.op->newline() << "const unsigned long address;";
6831 s.op->newline() << "uint8_t atype;";
bb0a4e12 6832 s.op->newline() << "unsigned int len;";
faea5e16 6833 s.op->newline() << "struct stap_probe probe;";
dd225250
PS
6834 s.op->newline() << "} stap_hwbkpt_probes[] = {";
6835 s.op->indent(1);
6836
dac77b80 6837 for (unsigned int it = 0; it < hwbkpt_probes.size(); it++)
dd225250 6838 {
dac77b80 6839 hwbkpt_derived_probe* p = hwbkpt_probes.at(it);
dd225250
PS
6840 s.op->newline() << "{";
6841 s.op->line() << " .registered_p=1,";
6842 if (p->symbol_name.size())
6843 s.op->line() << " .address=(unsigned long)0x0" << "ULL,";
6844 else
6845 s.op->line() << " .address=(unsigned long)0x" << hex << p->hwbkpt_addr << dec << "ULL,";
6846 switch(p->hwbkpt_access){
6847 case HWBKPT_READ:
6848 s.op->line() << " .atype=HW_BREAKPOINT_R ,";
bb0a4e12 6849 break;
dd225250
PS
6850 case HWBKPT_WRITE:
6851 s.op->line() << " .atype=HW_BREAKPOINT_W ,";
bb0a4e12 6852 break;
dd225250
PS
6853 case HWBKPT_RW:
6854 s.op->line() << " .atype=HW_BREAKPOINT_R|HW_BREAKPOINT_W ,";
bb0a4e12 6855 break;
dd225250
PS
6856 };
6857 s.op->line() << " .len=" << p->hwbkpt_len << ",";
faea5e16 6858 s.op->line() << " .probe=" << common_probe_init (p) << ",";
dd225250 6859 s.op->line() << " .symbol=\"" << p->symbol_name << "\",";
dd225250
PS
6860 s.op->line() << " },";
6861 }
dac77b80 6862 s.op->newline(-1) << "};";
dd225250
PS
6863
6864 // Emit the hwbkpt callback function
6865 s.op->newline() ;
6866 s.op->newline() << "static int enter_hwbkpt_probe (struct perf_event *bp,";
6867 s.op->line() << " int nmi,";
6868 s.op->line() << " struct perf_sample_data *data,";
6869 s.op->line() << " struct pt_regs *regs) {";
dac77b80
FCE
6870 s.op->newline(1) << "unsigned int i;";
6871 s.op->newline() << "if (bp->attr.type != PERF_TYPE_BREAKPOINT) return -1;";
6872 s.op->newline() << "for (i=0; i<" << hwbkpt_probes.size() << "; i++) {";
6873 s.op->newline(1) << "struct perf_event_attr *hp = & stap_hwbkpt_probe_array[i];";
6874 // XXX: why not match stap_hwbkpt_ret_array[i] against bp instead?
6875 s.op->newline() << "if (bp->attr.bp_addr==hp->bp_addr && bp->attr.bp_type==hp->bp_type && bp->attr.bp_len==hp->bp_len) {";
6876 s.op->newline(1) << "struct stap_hwbkpt_probe *sdp = &stap_hwbkpt_probes[i];";
faea5e16 6877 common_probe_entryfn_prologue (s.op, "STAP_SESSION_RUNNING", "sdp->probe");
dd225250 6878 s.op->newline() << "c->regs = regs;";
faea5e16 6879 s.op->newline() << "(*sdp->probe.ph) (c);";
dd225250 6880 common_probe_entryfn_epilogue (s.op);
dac77b80 6881 s.op->newline(-1) << "}";
dd225250
PS
6882 s.op->newline(-1) << "}";
6883 s.op->newline() << "return 0;";
dac77b80 6884 s.op->newline(-1) << "}";
dd225250
PS
6885}
6886
6887void
6888hwbkpt_derived_probe_group::emit_module_init (systemtap_session& s)
6889{
dac77b80 6890 s.op->newline() << "for (i=0; i<" << hwbkpt_probes.size() << "; i++) {";
dd225250
PS
6891 s.op->newline(1) << "struct stap_hwbkpt_probe *sdp = & stap_hwbkpt_probes[i];";
6892 s.op->newline() << "struct perf_event_attr *hp = & stap_hwbkpt_probe_array[i];";
6893 s.op->newline() << "void *addr = (void *) sdp->address;";
6894 s.op->newline() << "const char *hwbkpt_symbol_name = addr ? NULL : sdp->symbol;";
dac77b80
FCE
6895 s.op->newline() << "hw_breakpoint_init(hp);";
6896 s.op->newline() << "if (addr)";
6897 s.op->newline(1) << "hp->bp_addr = (unsigned long) addr;";
6898 s.op->newline(-1) << "else { ";
6899 s.op->newline(1) << "hp->bp_addr = kallsyms_lookup_name(hwbkpt_symbol_name);";
6900 s.op->newline() << "if (!hp->bp_addr) { ";
faea5e16 6901 s.op->newline(1) << "_stp_warn(\"Probe %s registration skipped: invalid symbol %s \",sdp->probe.pp,hwbkpt_symbol_name);";
dac77b80
FCE
6902 s.op->newline() << "continue;";
6903 s.op->newline(-1) << "}";
6904 s.op->newline(-1) << "}";
6905 s.op->newline() << "hp->bp_type = sdp->atype;";
6906
6907 // On x86 & x86-64, hp->bp_len is not just a number but a macro/enum (!?!).
6908 if (s.architecture == "i386" || s.architecture == "x86_64" )
6909 {
6910 s.op->newline() << "switch(sdp->len) {";
6911 s.op->newline() << "case 1:";
6912 s.op->newline(1) << "hp->bp_len = HW_BREAKPOINT_LEN_1;";
6913 s.op->newline() << "break;";
6914 s.op->newline(-1) << "case 2:";
6915 s.op->newline(1) << "hp->bp_len = HW_BREAKPOINT_LEN_2;";
6916 s.op->newline() << "break;";
6917 s.op->newline(-1) << "case 3:";
6918 s.op->newline() << "case 4:";
6919 s.op->newline(1) << "hp->bp_len = HW_BREAKPOINT_LEN_4;";
6920 s.op->newline() << "break;";
6921 s.op->newline(-1) << "case 5:";
6922 s.op->newline() << "case 6:";
6923 s.op->newline() << "case 7:";
6924 s.op->newline() << "case 8:";
6925 s.op->newline() << "default:"; // XXX: could instead reject
6926 s.op->newline(1) << "hp->bp_len = HW_BREAKPOINT_LEN_8;";
6927 s.op->newline() << "break;";
6928 s.op->newline(-1) << "}";
6929 }
6930 else // other architectures presumed straightforward
6931 s.op->newline() << "hp->bp_len = sdp->len;";
6932
faea5e16 6933 s.op->newline() << "probe_point = sdp->probe.pp;"; // for error messages
dac77b80
FCE
6934 s.op->newline() << "stap_hwbkpt_ret_array[i] = register_wide_hw_breakpoint(hp, (void *)&enter_hwbkpt_probe);";
6935 s.op->newline() << "if (IS_ERR(stap_hwbkpt_ret_array[i])) {";
6936 s.op->newline(1) << "int err_code = PTR_ERR(stap_hwbkpt_ret_array[i]);";
6937 s.op->newline(0) << "_stp_warn(\"Hwbkpt probe %s: registration error %d, addr %p, name %s\", probe_point, err_code, addr, hwbkpt_symbol_name);";
6938 s.op->newline(-1) << "}";
dd225250 6939 s.op->newline() << " else sdp->registered_p = 1;";
dd225250
PS
6940 s.op->newline(-1) << "}"; // for loop
6941}
6942
6943void
6944hwbkpt_derived_probe_group::emit_module_exit (systemtap_session& s)
6945{
6946 //Unregister hwbkpt probes.
dac77b80 6947 s.op->newline() << "for (i=0; i<" << hwbkpt_probes.size() << "; i++) {";
dd225250 6948 s.op->newline(1) << "struct stap_hwbkpt_probe *sdp = & stap_hwbkpt_probes[i];";
dac77b80
FCE
6949 s.op->newline() << "if (sdp->registered_p == 0) continue;";
6950 s.op->newline() << "unregister_wide_hw_breakpoint(stap_hwbkpt_ret_array[i]);";
dd225250
PS
6951 s.op->newline() << "sdp->registered_p = 0;";
6952 s.op->newline(-1) << "}";
6953}
6954
6955struct hwbkpt_builder: public derived_probe_builder
6956{
6957 hwbkpt_builder() {}
6958 virtual void build(systemtap_session & sess,
6959 probe * base,
6960 probe_point * location,
6961 literal_map_t const & parameters,
6962 vector<derived_probe *> & finished_results);
6963};
6964
6965void
6966hwbkpt_builder::build(systemtap_session & sess,
6967 probe * base,
6968 probe_point * location,
6969 literal_map_t const & parameters,
6970 vector<derived_probe *> & finished_results)
6971{
6972 string symbol_str_val;
6973 int64_t hwbkpt_address, len;
6974 bool has_addr, has_symbol_str, has_write, has_rw, has_len;
6975
b47f3a55
FCE
6976 if (! (sess.kernel_config["CONFIG_PERF_EVENTS"] == string("y")))
6977 throw semantic_error ("CONFIG_PERF_EVENTS not available on this kernel",
6978 location->components[0]->tok);
6979 if (! (sess.kernel_config["CONFIG_HAVE_HW_BREAKPOINT"] == string("y")))
6980 throw semantic_error ("CONFIG_HAVE_HW_BREAKPOINT not available on this kernel",
6981 location->components[0]->tok);
6982
dd225250
PS
6983 has_addr = get_param (parameters, TOK_HWBKPT, hwbkpt_address);
6984 has_symbol_str = get_param (parameters, TOK_HWBKPT, symbol_str_val);
6985 has_len = get_param (parameters, TOK_LENGTH, len);
6986 has_write = (parameters.find(TOK_HWBKPT_WRITE) != parameters.end());
6987 has_rw = (parameters.find(TOK_HWBKPT_RW) != parameters.end());
6988
6989 if (!has_len)
6990 len = 1;
6991
6992 if (has_addr)
6993 finished_results.push_back (new hwbkpt_derived_probe (base,
6994 location,
6995 hwbkpt_address,
6996 "",len,0,
6997 has_write,
6998 has_rw));
6999 else // has symbol_str
7000 finished_results.push_back (new hwbkpt_derived_probe (base,
7001 location,
7002 0,
7003 symbol_str_val,len,0,
7004 has_write,
7005 has_rw));
7006}
342d3f96 7007
0a6f5a3f
JS
7008// ------------------------------------------------------------------------
7009// statically inserted kernel-tracepoint derived probes
7010// ------------------------------------------------------------------------
7011
6fb70fb7 7012struct tracepoint_arg
79189b84 7013{
ad370dcc 7014 string name, c_type, typecast;
dcaa1a65 7015 bool usable, used, isptr;
f8a968bc 7016 Dwarf_Die type_die;
dcaa1a65 7017 tracepoint_arg(): usable(false), used(false), isptr(false) {}
6fb70fb7 7018};
79189b84 7019
0a6f5a3f
JS
7020struct tracepoint_derived_probe: public derived_probe
7021{
79189b84
JS
7022 tracepoint_derived_probe (systemtap_session& s,
7023 dwflpp& dw, Dwarf_Die& func_die,
7024 const string& tracepoint_name,
7025 probe* base_probe, probe_point* location);
bc9a523d 7026
79189b84 7027 systemtap_session& sess;
6fb70fb7
JS
7028 string tracepoint_name, header;
7029 vector <struct tracepoint_arg> args;
bc9a523d 7030
6fb70fb7 7031 void build_args(dwflpp& dw, Dwarf_Die& func_die);
d0bfd2ac 7032 void getargs (std::list<std::string> &arg_set) const;
79189b84 7033 void join_group (systemtap_session& s);
3e3bd7b6 7034 void print_dupe_stamp(ostream& o);
0a6f5a3f 7035};
79189b84
JS
7036
7037
0a6f5a3f 7038struct tracepoint_derived_probe_group: public generic_dpg<tracepoint_derived_probe>
79189b84 7039{
79189b84
JS
7040 void emit_module_decls (systemtap_session& s);
7041 void emit_module_init (systemtap_session& s);
7042 void emit_module_exit (systemtap_session& s);
0a6f5a3f 7043};
79189b84 7044
bc9a523d 7045
f8a968bc
JS
7046struct tracepoint_var_expanding_visitor: public var_expanding_visitor
7047{
7048 tracepoint_var_expanding_visitor(dwflpp& dw, const string& probe_name,
7049 vector <struct tracepoint_arg>& args):
7050 dw (dw), probe_name (probe_name), args (args) {}
7051 dwflpp& dw;
7052 const string& probe_name;
7053 vector <struct tracepoint_arg>& args;
bc9a523d 7054
f8a968bc
JS
7055 void visit_target_symbol (target_symbol* e);
7056 void visit_target_symbol_arg (target_symbol* e);
7057 void visit_target_symbol_context (target_symbol* e);
7058};
79189b84
JS
7059
7060
f8a968bc
JS
7061void
7062tracepoint_var_expanding_visitor::visit_target_symbol_arg (target_symbol* e)
75ead1f7 7063{
277c21bc 7064 string argname = e->name.substr(1);
75ead1f7 7065
f8a968bc
JS
7066 // search for a tracepoint parameter matching this name
7067 tracepoint_arg *arg = NULL;
7068 for (unsigned i = 0; i < args.size(); ++i)
dcaa1a65 7069 if (args[i].usable && args[i].name == argname)
f8a968bc
JS
7070 {
7071 arg = &args[i];
7072 arg->used = true;
7073 break;
7074 }
75ead1f7 7075
f8a968bc
JS
7076 if (arg == NULL)
7077 {
7078 stringstream alternatives;
7079 for (unsigned i = 0; i < args.size(); ++i)
7080 alternatives << " $" << args[i].name;
046e7190 7081 alternatives << " $$name $$parms $$vars";
75ead1f7 7082
f8a968bc
JS
7083 // We hope that this value ends up not being referenced after all, so it
7084 // can be optimized out quietly.
277c21bc 7085 throw semantic_error("unable to find tracepoint variable '" + e->name
f8a968bc
JS
7086 + "' (alternatives:" + alternatives.str () + ")", e->tok);
7087 // NB: we can have multiple errors, since a target variable
7088 // may be expanded in several different contexts:
7089 // trace ("*") { $foo->bar }
f8a968bc 7090 }
75ead1f7 7091
f8a968bc 7092 // make sure we're not dereferencing base types
dc5a09fc
JS
7093 if (!arg->isptr)
7094 e->assert_no_components("tracepoint");
75ead1f7 7095
f8a968bc
JS
7096 // we can only write to dereferenced fields, and only if guru mode is on
7097 bool lvalue = is_active_lvalue(e);
7098 if (lvalue && (!dw.sess.guru_mode || e->components.empty()))
277c21bc 7099 throw semantic_error("write to tracepoint variable '" + e->name
f8a968bc 7100 + "' not permitted", e->tok);
c69a87e0 7101
ad370dcc
JS
7102 // XXX: if a struct/union arg is passed by value, then writing to its fields
7103 // is also meaningless until you dereference past a pointer member. It's
7104 // harder to detect and prevent that though...
75ead1f7 7105
f8a968bc
JS
7106 if (e->components.empty())
7107 {
03c75a4a
JS
7108 if (e->addressof)
7109 throw semantic_error("cannot take address of tracepoint variable", e->tok);
a45664f4 7110
3e3bd7b6 7111 // Just grab the value from the probe locals
a45664f4
JS
7112 symbol* sym = new symbol;
7113 sym->tok = e->tok;
7114 sym->name = "__tracepoint_arg_" + arg->name;
7115 provide (sym);
f8a968bc
JS
7116 }
7117 else
7118 {
5f36109e
JS
7119 // make a copy of the original as a bare target symbol for the tracepoint
7120 // value, which will be passed into the dwarf dereferencing code
7121 target_symbol* e2 = deep_copy_visitor::deep_copy(e);
7122 e2->components.clear();
7123
7124 if (e->components.back().type == target_symbol::comp_pretty_print)
7125 {
7126 if (lvalue)
7127 throw semantic_error("cannot write to pretty-printed variable", e->tok);
7128
7129 dwarf_pretty_print dpp(dw, &arg->type_die, e2, false, *e);
7130 dpp.expand()->visit (this);
7131 return;
7132 }
7133
f8a968bc
JS
7134 // Synthesize a function to dereference the dwarf fields,
7135 // with a pointer parameter that is the base tracepoint variable
7136 functiondecl *fdecl = new functiondecl;
59de45f1 7137 fdecl->synthetic = true;
f8a968bc
JS
7138 fdecl->tok = e->tok;
7139 embeddedcode *ec = new embeddedcode;
7140 ec->tok = e->tok;
75ead1f7 7141
f8a968bc 7142 string fname = (string(lvalue ? "_tracepoint_tvar_set" : "_tracepoint_tvar_get")
277c21bc 7143 + "_" + e->name.substr(1)
aca66a36 7144 + "_" + lex_cast(tick++));
75ead1f7 7145
f8a968bc
JS
7146 fdecl->name = fname;
7147 fdecl->body = ec;
75ead1f7 7148
b5a0dd41
FCE
7149 // PR10601: adapt to kernel-vs-userspace loc2c-runtime
7150 ec->code += "\n#define fetch_register k_fetch_register\n";
7151 ec->code += "#define store_register k_store_register\n";
c69a87e0
FCE
7152
7153 ec->code += dw.literal_stmt_for_pointer (&arg->type_die, e,
f8a968bc 7154 lvalue, fdecl->type);
75ead1f7 7155
f8a968bc
JS
7156 // Give the fdecl an argument for the raw tracepoint value
7157 vardecl *v1 = new vardecl;
7158 v1->type = pe_long;
7159 v1->name = "pointer";
7160 v1->tok = e->tok;
7161 fdecl->formal_args.push_back(v1);
75ead1f7 7162
6fda2dff
JS
7163 // Any non-literal indexes need to be passed in too.
7164 for (unsigned i = 0; i < e->components.size(); ++i)
7165 if (e->components[i].type == target_symbol::comp_expression_array_index)
7166 {
7167 vardecl *v = new vardecl;
7168 v->type = pe_long;
aca66a36 7169 v->name = "index" + lex_cast(i);
6fda2dff
JS
7170 v->tok = e->tok;
7171 fdecl->formal_args.push_back(v);
7172 }
7173
f8a968bc
JS
7174 if (lvalue)
7175 {
7176 // Modify the fdecl so it carries a pe_long formal
7177 // argument called "value".
75ead1f7 7178
f8a968bc
JS
7179 // FIXME: For the time being we only support setting target
7180 // variables which have base types; these are 'pe_long' in
7181 // stap's type vocabulary. Strings and pointers might be
7182 // reasonable, some day, but not today.
7183
7184 vardecl *v2 = new vardecl;
7185 v2->type = pe_long;
7186 v2->name = "value";
7187 v2->tok = e->tok;
7188 fdecl->formal_args.push_back(v2);
7189 }
7190 else
7191 ec->code += "/* pure */";
7192
64211010
DB
7193 ec->code += "/* unprivileged */";
7194
b5a0dd41
FCE
7195 // PR10601
7196 ec->code += "\n#undef fetch_register\n";
7197 ec->code += "\n#undef store_register\n";
aff5d390 7198
f8809d54 7199 fdecl->join (dw.sess);
75ead1f7 7200
f8a968bc
JS
7201 // Synthesize a functioncall.
7202 functioncall* n = new functioncall;
7203 n->tok = e->tok;
7204 n->function = fname;
6fda2dff
JS
7205 n->args.push_back(require(e2));
7206
7207 // Any non-literal indexes need to be passed in too.
7208 for (unsigned i = 0; i < e->components.size(); ++i)
7209 if (e->components[i].type == target_symbol::comp_expression_array_index)
7210 n->args.push_back(require(e->components[i].expr_index));
75ead1f7 7211
f8a968bc
JS
7212 if (lvalue)
7213 {
7214 // Provide the functioncall to our parent, so that it can be
7215 // used to substitute for the assignment node immediately above
7216 // us.
7217 assert(!target_symbol_setter_functioncalls.empty());
7218 *(target_symbol_setter_functioncalls.top()) = n;
7219 }
75ead1f7 7220
f8a968bc
JS
7221 provide (n);
7222 }
75ead1f7
JS
7223}
7224
7225
f8a968bc
JS
7226void
7227tracepoint_var_expanding_visitor::visit_target_symbol_context (target_symbol* e)
0a6f5a3f 7228{
03c75a4a
JS
7229 if (e->addressof)
7230 throw semantic_error("cannot take address of context variable", e->tok);
7231
f8a968bc 7232 if (is_active_lvalue (e))
277c21bc 7233 throw semantic_error("write to tracepoint '" + e->name + "' not permitted", e->tok);
0a6f5a3f 7234
277c21bc 7235 if (e->name == "$$name")
f8a968bc 7236 {
5f36109e
JS
7237 e->assert_no_components("tracepoint");
7238
f8a968bc
JS
7239 // Synthesize a functioncall.
7240 functioncall* n = new functioncall;
7241 n->tok = e->tok;
7242 n->function = "_mark_name_get";
f8a968bc
JS
7243 provide (n);
7244 }
277c21bc 7245 else if (e->name == "$$vars" || e->name == "$$parms")
f8a968bc 7246 {
5f36109e
JS
7247 e->assert_no_components("tracepoint", true);
7248
f8a968bc
JS
7249 // Convert $$vars to sprintf of a list of vars which we recursively evaluate
7250 // NB: we synthesize a new token here rather than reusing
7251 // e->tok, because print_format::print likes to use
7252 // its tok->content.
7253 token* pf_tok = new token(*e->tok);
7254 pf_tok->content = "sprintf";
0a6f5a3f 7255
d5e178c1 7256 print_format* pf = print_format::create(pf_tok);
0a6f5a3f 7257
f8a968bc 7258 for (unsigned i = 0; i < args.size(); ++i)
b278033a 7259 {
dcaa1a65
JS
7260 if (!args[i].usable)
7261 continue;
f8a968bc
JS
7262 if (i > 0)
7263 pf->raw_components += " ";
7264 pf->raw_components += args[i].name;
3e3bd7b6 7265 target_symbol *tsym = new target_symbol;
f8a968bc 7266 tsym->tok = e->tok;
277c21bc 7267 tsym->name = "$" + args[i].name;
5f36109e 7268 tsym->components = e->components;
b278033a 7269
f8a968bc
JS
7270 // every variable should always be accessible!
7271 tsym->saved_conversion_error = 0;
7272 expression *texp = require (tsym); // NB: throws nothing ...
7273 assert (!tsym->saved_conversion_error); // ... but this is how we know it happened.
b278033a 7274
5f36109e
JS
7275 if (!e->components.empty() &&
7276 e->components[0].type == target_symbol::comp_pretty_print)
7277 pf->raw_components += "=%s";
7278 else
7279 pf->raw_components += args[i].isptr ? "=%p" : "=%#x";
f8a968bc
JS
7280 pf->args.push_back(texp);
7281 }
0a6f5a3f 7282
f8a968bc
JS
7283 pf->components = print_format::string_to_components(pf->raw_components);
7284 provide (pf);
b278033a 7285 }
f8a968bc
JS
7286 else
7287 assert(0); // shouldn't get here
0a6f5a3f
JS
7288}
7289
0a6f5a3f 7290void
f8a968bc 7291tracepoint_var_expanding_visitor::visit_target_symbol (target_symbol* e)
0a6f5a3f 7292{
aff5d390 7293 try
c69a87e0 7294 {
277c21bc 7295 assert(e->name.size() > 0 && e->name[0] == '$');
aff5d390 7296
277c21bc 7297 if (e->name == "$$name" || e->name == "$$parms" || e->name == "$$vars")
c69a87e0
FCE
7298 visit_target_symbol_context (e);
7299 else
7300 visit_target_symbol_arg (e);
7301 }
7302 catch (const semantic_error &er)
7303 {
1af1e62d 7304 e->chain (er);
c69a87e0
FCE
7305 provide (e);
7306 }
0a6f5a3f
JS
7307}
7308
7309
7310
79189b84
JS
7311tracepoint_derived_probe::tracepoint_derived_probe (systemtap_session& s,
7312 dwflpp& dw, Dwarf_Die& func_die,
7313 const string& tracepoint_name,
7314 probe* base, probe_point* loc):
7315 derived_probe (base, new probe_point(*loc) /* .components soon rewritten */),
7316 sess (s), tracepoint_name (tracepoint_name)
56894e91 7317{
79189b84
JS
7318 // create synthetic probe point name; preserve condition
7319 vector<probe_point::component*> comps;
7320 comps.push_back (new probe_point::component (TOK_KERNEL));
7321 comps.push_back (new probe_point::component (TOK_TRACE, new literal_string (tracepoint_name)));
7322 this->sole_location()->components = comps;
7323
6fb70fb7
JS
7324 // fill out the available arguments in this tracepoint
7325 build_args(dw, func_die);
56894e91 7326
6fb70fb7
JS
7327 // determine which header defined this tracepoint
7328 string decl_file = dwarf_decl_file(&func_die);
7329 size_t header_pos = decl_file.rfind("trace/");
7330 if (header_pos == string::npos)
7331 throw semantic_error ("cannot parse header location for tracepoint '"
7332 + tracepoint_name + "' in '"
7333 + decl_file + "'");
7334 header = decl_file.substr(header_pos);
56894e91 7335
6fb70fb7
JS
7336 // tracepoints from FOO_event_types.h should really be included from FOO.h
7337 // XXX can dwarf tell us the include hierarchy? it would be better to
7338 // ... walk up to see which one was directly included by tracequery.c
3c1b3d06 7339 // XXX: see also PR9993.
6fb70fb7
JS
7340 header_pos = header.find("_event_types");
7341 if (header_pos != string::npos)
7342 header.erase(header_pos, 12);
56894e91 7343
f8a968bc
JS
7344 // Now expand the local variables in the probe body
7345 tracepoint_var_expanding_visitor v (dw, name, args);
8b095b45 7346 v.replace (this->body);
a45664f4
JS
7347 for (unsigned i = 0; i < args.size(); i++)
7348 if (args[i].used)
7349 {
7350 vardecl* v = new vardecl;
7351 v->name = "__tracepoint_arg_" + args[i].name;
7352 v->tok = this->tok;
58701b78 7353 v->set_arity(0, this->tok);
a45664f4
JS
7354 v->type = pe_long;
7355 v->skip_init = true;
7356 this->locals.push_back (v);
7357 }
56894e91 7358
79189b84
JS
7359 if (sess.verbose > 2)
7360 clog << "tracepoint-based " << name << " tracepoint='" << tracepoint_name
7361 << "'" << endl;
7362}
dc38c0ae 7363
56894e91 7364
f8a968bc 7365static bool
dcaa1a65 7366resolve_tracepoint_arg_type(tracepoint_arg& arg)
46b84a80 7367{
dcaa1a65 7368 switch (dwarf_tag(&arg.type_die))
b20febf3 7369 {
f8a968bc
JS
7370 case DW_TAG_typedef:
7371 case DW_TAG_const_type:
7372 case DW_TAG_volatile_type:
7373 // iterate on the referent type
3d1ad340 7374 return (dwarf_attr_die(&arg.type_die, DW_AT_type, &arg.type_die)
dcaa1a65 7375 && resolve_tracepoint_arg_type(arg));
f8a968bc
JS
7376 case DW_TAG_base_type:
7377 // base types will simply be treated as script longs
dcaa1a65 7378 arg.isptr = false;
f8a968bc
JS
7379 return true;
7380 case DW_TAG_pointer_type:
dcaa1a65
JS
7381 // pointers can be treated as script longs,
7382 // and if we know their type, they can also be dereferenced
3d1ad340 7383 if (dwarf_attr_die(&arg.type_die, DW_AT_type, &arg.type_die))
dcaa1a65 7384 arg.isptr = true;
ad370dcc
JS
7385 arg.typecast = "(intptr_t)";
7386 return true;
7387 case DW_TAG_structure_type:
7388 case DW_TAG_union_type:
7389 // for structs/unions which are passed by value, we turn it into
7390 // a pointer that can be dereferenced.
7391 arg.isptr = true;
7392 arg.typecast = "(intptr_t)&";
dcaa1a65 7393 return true;
f8a968bc
JS
7394 default:
7395 // should we consider other types too?
7396 return false;
b20febf3 7397 }
56894e91
JS
7398}
7399
7400
7401void
6fb70fb7 7402tracepoint_derived_probe::build_args(dwflpp& dw, Dwarf_Die& func_die)
56894e91 7403{
6fb70fb7
JS
7404 Dwarf_Die arg;
7405 if (dwarf_child(&func_die, &arg) == 0)
7406 do
7407 if (dwarf_tag(&arg) == DW_TAG_formal_parameter)
7408 {
7409 // build a tracepoint_arg for this parameter
7410 tracepoint_arg tparg;
23d106b9 7411 tparg.name = dwarf_diename(&arg);
56894e91 7412
6fb70fb7 7413 // read the type of this parameter
3d1ad340 7414 if (!dwarf_attr_die (&arg, DW_AT_type, &tparg.type_die)
f1c8f8a5 7415 || !dwarf_type_name(&tparg.type_die, tparg.c_type))
6fb70fb7
JS
7416 throw semantic_error ("cannot get type of tracepoint '"
7417 + tracepoint_name + "' parameter '"
7418 + tparg.name + "'");
a68f81a2 7419
dcaa1a65 7420 tparg.usable = resolve_tracepoint_arg_type(tparg);
6fb70fb7
JS
7421 args.push_back(tparg);
7422 if (sess.verbose > 4)
7423 clog << "found parameter for tracepoint '" << tracepoint_name
7424 << "': type:'" << tparg.c_type
7425 << "' name:'" << tparg.name << "'" << endl;
7426 }
7427 while (dwarf_siblingof(&arg, &arg) == 0);
56894e91
JS
7428}
7429
dc38c0ae 7430void
d0bfd2ac 7431tracepoint_derived_probe::getargs(std::list<std::string> &arg_set) const
dc38c0ae 7432{
dcaa1a65
JS
7433 for (unsigned i = 0; i < args.size(); ++i)
7434 if (args[i].usable)
d0bfd2ac 7435 arg_set.push_back("$"+args[i].name+":"+args[i].c_type);
dc38c0ae
DS
7436}
7437
79189b84
JS
7438void
7439tracepoint_derived_probe::join_group (systemtap_session& s)
197a4d62 7440{
79189b84
JS
7441 if (! s.tracepoint_derived_probes)
7442 s.tracepoint_derived_probes = new tracepoint_derived_probe_group ();
7443 s.tracepoint_derived_probes->enroll (this);
7444}
e38d6504 7445
56894e91 7446
197a4d62 7447void
3e3bd7b6 7448tracepoint_derived_probe::print_dupe_stamp(ostream& o)
56894e91 7449{
3e3bd7b6
JS
7450 for (unsigned i = 0; i < args.size(); i++)
7451 if (args[i].used)
7452 o << "__tracepoint_arg_" << args[i].name << endl;
197a4d62 7453}
56894e91 7454
3e3bd7b6 7455
9e0cd21a 7456static vector<string> tracepoint_extra_headers (systemtap_session& s)
47dd066d 7457{
3c1b3d06
FCE
7458 vector<string> they_live;
7459 // PR 9993
7460 // XXX: may need this to be configurable
7461 they_live.push_back ("linux/skbuff.h");
9e0cd21a
FCE
7462
7463 // PR11649: conditional extra header
7464 // for kvm tracepoints in 2.6.33ish
7465 if (s.kernel_config["CONFIG_KVM"] != string("")) {
7466 they_live.push_back ("linux/kvm_host.h");
7467 }
3c1b3d06
FCE
7468 return they_live;
7469}
47dd066d
WC
7470
7471
7472void
79189b84 7473tracepoint_derived_probe_group::emit_module_decls (systemtap_session& s)
47dd066d 7474{
79189b84
JS
7475 if (probes.empty())
7476 return;
47dd066d 7477
96b030fe
JS
7478 s.op->newline() << "/* ---- tracepoint probes ---- */";
7479 s.op->newline();
79189b84 7480
3c1b3d06
FCE
7481 // PR9993: Add extra headers to work around undeclared types in individual
7482 // include/trace/foo.h files
9e0cd21a 7483 const vector<string>& extra_headers = tracepoint_extra_headers (s);
3c1b3d06
FCE
7484 for (unsigned z=0; z<extra_headers.size(); z++)
7485 s.op->newline() << "#include <" << extra_headers[z] << ">\n";
47dd066d 7486
6fb70fb7
JS
7487 for (unsigned i = 0; i < probes.size(); ++i)
7488 {
7489 tracepoint_derived_probe *p = probes[i];
47dd066d 7490
96b030fe
JS
7491 // emit a separate entry function for each probe, since tracepoints
7492 // don't provide any sort of context pointer.
392e08b7 7493 s.op->newline() << "#undef TRACE_INCLUDE_FILE";
6fb70fb7 7494 s.op->newline() << "#include <" << p->header << ">";
5f73a260
JS
7495
7496 // Starting in 2.6.35, at the same time NOARGS was added, the callback
7497 // always has a void* as the first parameter. PR11599
7498 s.op->newline() << "#ifdef DECLARE_TRACE_NOARGS";
7499 s.op->newline() << "#define STAP_TP_DATA , NULL";
7500 s.op->newline() << "#define STAP_TP_PROTO void *cb_data"
7501 << " __attribute__ ((unused))";
7502 if (!p->args.empty())
7503 s.op->line() << ",";
7504 s.op->newline() << "#else";
7505 s.op->newline() << "#define STAP_TP_DATA";
7506 s.op->newline() << "#define STAP_TP_PROTO";
7507 if (p->args.empty())
7508 s.op->line() << " void";
7509 s.op->newline() << "#endif";
7510
6fb70fb7 7511 s.op->newline() << "static void enter_tracepoint_probe_" << i << "(";
5f73a260
JS
7512 s.op->newline(2) << "STAP_TP_PROTO";
7513
6fb70fb7
JS
7514 for (unsigned j = 0; j < p->args.size(); ++j)
7515 {
7516 if (j > 0)
5f73a260
JS
7517 s.op->line() << ",";
7518 s.op->newline() << p->args[j].c_type << " __tracepoint_arg_" << p->args[j].name;
6fb70fb7 7519 }
5f73a260
JS
7520 s.op->newline() << ")";
7521 s.op->newline(-2) << "{";
7522
faea5e16
JS
7523 s.op->newline(1) << "static const struct stap_probe probe = "
7524 << common_probe_init (p) << ";";
7525 common_probe_entryfn_prologue (s.op, "STAP_SESSION_RUNNING", "probe");
f8a968bc 7526 s.op->newline() << "c->marker_name = "
c12d974f
FCE
7527 << lex_cast_qstring (p->tracepoint_name)
7528 << ";";
f8a968bc
JS
7529 for (unsigned j = 0; j < p->args.size(); ++j)
7530 if (p->args[j].used)
7531 {
66671fd8 7532 s.op->newline() << "c->probe_locals." << p->name << ".__tracepoint_arg_"
f8a968bc 7533 << p->args[j].name << " = (int64_t)";
ad370dcc 7534 s.op->line() << p->args[j].typecast;
f8a968bc
JS
7535 s.op->line() << "__tracepoint_arg_" << p->args[j].name << ";";
7536 }
faea5e16 7537 s.op->newline() << "(*probe.ph) (c);";
6fb70fb7
JS
7538 common_probe_entryfn_epilogue (s.op);
7539 s.op->newline(-1) << "}";
47dd066d 7540
96b030fe
JS
7541 // emit normalized registration functions
7542 s.op->newline() << "static int register_tracepoint_probe_" << i << "(void) {";
7543 s.op->newline(1) << "return register_trace_" << p->tracepoint_name
5f73a260 7544 << "(enter_tracepoint_probe_" << i << " STAP_TP_DATA);";
96b030fe 7545 s.op->newline(-1) << "}";
47dd066d 7546
86758d5f
JS
7547 // NB: we're not prepared to deal with unreg failures. However, failures
7548 // can only occur if the tracepoint doesn't exist (yet?), or if we
7549 // weren't even registered. The former should be OKed by the initial
7550 // registration call, and the latter is safe to ignore.
7551 s.op->newline() << "static void unregister_tracepoint_probe_" << i << "(void) {";
7552 s.op->newline(1) << "(void) unregister_trace_" << p->tracepoint_name
5f73a260 7553 << "(enter_tracepoint_probe_" << i << " STAP_TP_DATA);";
96b030fe 7554 s.op->newline(-1) << "}";
6fb70fb7 7555 s.op->newline();
5f73a260
JS
7556
7557 s.op->newline() << "#undef STAP_TP_DATA";
7558 s.op->newline() << "#undef STAP_TP_PROTO";
7559 s.op->newline();
af304783
DS
7560 }
7561
96b030fe
JS
7562 // emit an array of registration functions for easy init/shutdown
7563 s.op->newline() << "static struct stap_tracepoint_probe {";
7564 s.op->newline(1) << "int (*reg)(void);";
86758d5f 7565 s.op->newline(0) << "void (*unreg)(void);";
96b030fe
JS
7566 s.op->newline(-1) << "} stap_tracepoint_probes[] = {";
7567 s.op->indent(1);
7568 for (unsigned i = 0; i < probes.size(); ++i)
7569 {
7570 s.op->newline () << "{";
7571 s.op->line() << " .reg=&register_tracepoint_probe_" << i << ",";
7572 s.op->line() << " .unreg=&unregister_tracepoint_probe_" << i;
7573 s.op->line() << " },";
7574 }
7575 s.op->newline(-1) << "};";
7576 s.op->newline();
47dd066d
WC
7577}
7578
7579
79189b84
JS
7580void
7581tracepoint_derived_probe_group::emit_module_init (systemtap_session &s)
47dd066d 7582{
79189b84
JS
7583 if (probes.size () == 0)
7584 return;
47dd066d 7585
79189b84 7586 s.op->newline() << "/* init tracepoint probes */";
96b030fe
JS
7587 s.op->newline() << "for (i=0; i<" << probes.size() << "; i++) {";
7588 s.op->newline(1) << "rc = stap_tracepoint_probes[i].reg();";
7589 s.op->newline() << "if (rc) {";
7590 s.op->newline(1) << "for (j=i-1; j>=0; j--)"; // partial rollback
7591 s.op->newline(1) << "stap_tracepoint_probes[j].unreg();";
7592 s.op->newline(-1) << "break;"; // don't attempt to register any more probes
7593 s.op->newline(-1) << "}";
7594 s.op->newline(-1) << "}";
47dd066d 7595
bc9a523d
FCE
7596 // This would be technically proper (on those autoconf-detectable
7597 // kernels that include this function in tracepoint.h), however we
7598 // already make several calls to synchronze_sched() during our
7599 // shutdown processes.
47dd066d 7600
bc9a523d
FCE
7601 // s.op->newline() << "if (rc)";
7602 // s.op->newline(1) << "tracepoint_synchronize_unregister();";
7603 // s.op->indent(-1);
79189b84 7604}
47dd066d
WC
7605
7606
79189b84
JS
7607void
7608tracepoint_derived_probe_group::emit_module_exit (systemtap_session& s)
47dd066d 7609{
79189b84
JS
7610 if (probes.empty())
7611 return;
47dd066d 7612
96b030fe
JS
7613 s.op->newline() << "/* deregister tracepoint probes */";
7614 s.op->newline() << "for (i=0; i<" << probes.size() << "; i++)";
7615 s.op->newline(1) << "stap_tracepoint_probes[i].unreg();";
7616 s.op->indent(-1);
47dd066d 7617
bc9a523d 7618 // Not necessary: see above.
47dd066d 7619
bc9a523d 7620 // s.op->newline() << "tracepoint_synchronize_unregister();";
79189b84 7621}
b20febf3 7622
47dd066d 7623
75ead1f7 7624struct tracepoint_query : public base_query
47dd066d 7625{
75ead1f7
JS
7626 tracepoint_query(dwflpp & dw, const string & tracepoint,
7627 probe * base_probe, probe_point * base_loc,
7628 vector<derived_probe *> & results):
7629 base_query(dw, "*"), tracepoint(tracepoint),
7630 base_probe(base_probe), base_loc(base_loc),
7631 results(results) {}
47dd066d 7632
75ead1f7 7633 const string& tracepoint;
47dd066d 7634
75ead1f7
JS
7635 probe * base_probe;
7636 probe_point * base_loc;
7637 vector<derived_probe *> & results;
f982c59b 7638 set<string> probed_names;
47dd066d 7639
75ead1f7
JS
7640 void handle_query_module();
7641 int handle_query_cu(Dwarf_Die * cudie);
7642 int handle_query_func(Dwarf_Die * func);
b20febf3 7643
75ead1f7
JS
7644 static int tracepoint_query_cu (Dwarf_Die * cudie, void * arg);
7645 static int tracepoint_query_func (Dwarf_Die * func, base_query * query);
7646};
47dd066d
WC
7647
7648
7649void
75ead1f7 7650tracepoint_query::handle_query_module()
47dd066d 7651{
75ead1f7
JS
7652 // look for the tracepoints in each CU
7653 dw.iterate_over_cus(tracepoint_query_cu, this);
47dd066d
WC
7654}
7655
7656
75ead1f7
JS
7657int
7658tracepoint_query::handle_query_cu(Dwarf_Die * cudie)
47dd066d 7659{
75ead1f7 7660 dw.focus_on_cu (cudie);
47dd066d 7661
75ead1f7
JS
7662 // look at each function to see if it's a tracepoint
7663 string function = "stapprobe_" + tracepoint;
7664 return dw.iterate_over_functions (tracepoint_query_func, this, function);
47dd066d
WC
7665}
7666
7667
75ead1f7
JS
7668int
7669tracepoint_query::handle_query_func(Dwarf_Die * func)
47dd066d 7670{
75ead1f7 7671 dw.focus_on_function (func);
47dd066d 7672
60d98537 7673 assert(startswith(dw.function_name, "stapprobe_"));
75ead1f7 7674 string tracepoint_instance = dw.function_name.substr(10);
f982c59b
JS
7675
7676 // check for duplicates -- sometimes tracepoint headers may be indirectly
7677 // included in more than one of our tracequery modules.
7678 if (!probed_names.insert(tracepoint_instance).second)
7679 return DWARF_CB_OK;
7680
79189b84
JS
7681 derived_probe *dp = new tracepoint_derived_probe (dw.sess, dw, *func,
7682 tracepoint_instance,
7683 base_probe, base_loc);
7684 results.push_back (dp);
75ead1f7 7685 return DWARF_CB_OK;
47dd066d
WC
7686}
7687
7688
75ead1f7
JS
7689int
7690tracepoint_query::tracepoint_query_cu (Dwarf_Die * cudie, void * arg)
47dd066d 7691{
75ead1f7 7692 tracepoint_query * q = static_cast<tracepoint_query *>(arg);
85007c04 7693 if (pending_interrupts) return DWARF_CB_ABORT;
75ead1f7 7694 return q->handle_query_cu(cudie);
47dd066d
WC
7695}
7696
7697
75ead1f7
JS
7698int
7699tracepoint_query::tracepoint_query_func (Dwarf_Die * func, base_query * query)
47dd066d 7700{
75ead1f7 7701 tracepoint_query * q = static_cast<tracepoint_query *>(query);
85007c04 7702 if (pending_interrupts) return DWARF_CB_ABORT;
75ead1f7 7703 return q->handle_query_func(func);
47dd066d
WC
7704}
7705
7706
0a6f5a3f 7707struct tracepoint_builder: public derived_probe_builder
47dd066d 7708{
0a6f5a3f
JS
7709private:
7710 dwflpp *dw;
7711 bool init_dw(systemtap_session& s);
55e50c24
JS
7712 string get_tracequery_module(systemtap_session& s,
7713 const vector<string>& headers);
47dd066d 7714
0a6f5a3f 7715public:
47dd066d 7716
0a6f5a3f
JS
7717 tracepoint_builder(): dw(0) {}
7718 ~tracepoint_builder() { delete dw; }
47dd066d 7719
0a6f5a3f
JS
7720 void build_no_more (systemtap_session& s)
7721 {
7722 if (dw && s.verbose > 3)
7723 clog << "tracepoint_builder releasing dwflpp" << endl;
7724 delete dw;
7725 dw = NULL;
7726 }
47dd066d 7727
0a6f5a3f
JS
7728 void build(systemtap_session& s,
7729 probe *base, probe_point *location,
7730 literal_map_t const& parameters,
7731 vector<derived_probe*>& finished_results);
7732};
47dd066d 7733
47dd066d 7734
f982c59b
JS
7735string
7736tracepoint_builder::get_tracequery_module(systemtap_session& s,
55e50c24 7737 const vector<string>& headers)
0a6f5a3f 7738{
c95eddf7 7739 if (s.verbose > 2)
55e50c24
JS
7740 {
7741 clog << "Pass 2: getting a tracequery for "
7742 << headers.size() << " headers:" << endl;
7743 for (size_t i = 0; i < headers.size(); ++i)
7744 clog << " " << headers[i] << endl;
7745 }
c95eddf7 7746
a2639cb7 7747 string tracequery_path;
b278033a
JS
7748 if (s.use_cache)
7749 {
7750 // see if the cached module exists
55e50c24 7751 tracequery_path = find_tracequery_hash(s, headers);
d105f664 7752 if (!tracequery_path.empty() && !s.poison_cache)
b278033a 7753 {
a2639cb7 7754 int fd = open(tracequery_path.c_str(), O_RDONLY);
b278033a
JS
7755 if (fd != -1)
7756 {
7757 if (s.verbose > 2)
a2639cb7 7758 clog << "Pass 2: using cached " << tracequery_path << endl;
b278033a 7759 close(fd);
f982c59b 7760 return tracequery_path;
b278033a
JS
7761 }
7762 }
7763 }
47dd066d 7764
b278033a 7765 // no cached module, time to make it
f982c59b 7766
55e50c24
JS
7767 // PR9993: Add extra headers to work around undeclared types in individual
7768 // include/trace/foo.h files
9e0cd21a 7769 vector<string> short_headers = tracepoint_extra_headers(s);
55e50c24
JS
7770
7771 // add each requested tracepoint header
7772 for (size_t i = 0; i < headers.size(); ++i)
7773 {
7774 const string &header = headers[i];
7775 size_t root_pos = header.rfind("/include/");
7776 short_headers.push_back((root_pos != string::npos) ?
7777 header.substr(root_pos + 9) :
7778 header);
7779 }
f982c59b 7780
0a6f5a3f 7781 string tracequery_ko;
55e50c24 7782 int rc = make_tracequery(s, tracequery_ko, short_headers);
0a6f5a3f 7783 if (rc != 0)
c95eddf7 7784 tracequery_ko = "/dev/null";
47dd066d 7785
e16dc041 7786 // try to save tracequery in the cache
b278033a 7787 if (s.use_cache)
e16dc041
JS
7788 copy_file(tracequery_ko, tracequery_path, s.verbose > 2);
7789
f982c59b
JS
7790 return tracequery_ko;
7791}
7792
7793
7794bool
7795tracepoint_builder::init_dw(systemtap_session& s)
7796{
7797 if (dw != NULL)
7798 return true;
7799
7800 vector<string> tracequery_modules;
55e50c24 7801 vector<string> system_headers;
f982c59b
JS
7802
7803 glob_t trace_glob;
7804 string globs[] = {
f982c59b 7805 "/include/trace/events/*.h",
f982c59b 7806 "/source/include/trace/events/*.h",
508968a7
JS
7807 "/include/trace/*.h",
7808 "/source/include/trace/*.h",
f982c59b
JS
7809 };
7810 for (unsigned z = 0; z < sizeof(globs) / sizeof(globs[0]); z++)
7811 {
7812 string glob_str(s.kernel_build_tree + globs[z]);
7813 glob(glob_str.c_str(), 0, NULL, &trace_glob);
7814 for (unsigned i = 0; i < trace_glob.gl_pathc; ++i)
7815 {
7816 string header(trace_glob.gl_pathv[i]);
7817
7818 // filter out a few known "internal-only" headers
60d98537
JS
7819 if (endswith(header, "/define_trace.h") ||
7820 endswith(header, "/ftrace.h") ||
7821 endswith(header, "/trace_events.h") ||
7822 endswith(header, "_event_types.h"))
f982c59b
JS
7823 continue;
7824
55e50c24 7825 system_headers.push_back(header);
f982c59b
JS
7826 }
7827 globfree(&trace_glob);
7828 }
7829
55e50c24
JS
7830 // First attempt to do all system headers in one go
7831 string tracequery_path = get_tracequery_module(s, system_headers);
7832 // NB: An empty tracequery means that the header didn't compile correctly
7833 if (get_file_size(tracequery_path))
7834 tracequery_modules.push_back(tracequery_path);
7835 else
7836 // Otherwise try to do them one at a time (PR10424)
7837 for (size_t i = 0; i < system_headers.size(); ++i)
7838 {
85007c04 7839 if (pending_interrupts) return false;
392e08b7 7840
55e50c24
JS
7841 vector<string> one_header(1, system_headers[i]);
7842 tracequery_path = get_tracequery_module(s, one_header);
7843 if (get_file_size(tracequery_path))
7844 tracequery_modules.push_back(tracequery_path);
7845 }
7846
f982c59b
JS
7847 // TODO: consider other sources of tracepoint headers too, like from
7848 // a command-line parameter or some environment or .systemtaprc
47dd066d 7849
59c11f91 7850 dw = new dwflpp(s, tracequery_modules, true);
0a6f5a3f
JS
7851 return true;
7852}
47dd066d 7853
0a6f5a3f
JS
7854void
7855tracepoint_builder::build(systemtap_session& s,
7856 probe *base, probe_point *location,
7857 literal_map_t const& parameters,
7858 vector<derived_probe*>& finished_results)
7859{
7860 if (!init_dw(s))
7861 return;
47dd066d 7862
75ead1f7
JS
7863 string tracepoint;
7864 assert(get_param (parameters, TOK_TRACE, tracepoint));
47dd066d 7865
75ead1f7 7866 tracepoint_query q(*dw, tracepoint, base, location, finished_results);
51178501 7867 dw->iterate_over_modules(&query_module, &q);
47dd066d 7868}
47dd066d 7869
e6fe60e7 7870
b55bc428 7871// ------------------------------------------------------------------------
bd2b1e68 7872// Standard tapset registry.
b55bc428
FCE
7873// ------------------------------------------------------------------------
7874
7a053d3b 7875void
f8220a7b 7876register_standard_tapsets(systemtap_session & s)
b55bc428 7877{
47e0478e 7878 register_tapset_been(s);
93646f4d 7879 register_tapset_itrace(s);
dd0e4fa7 7880 register_tapset_mark(s);
7a212aa8 7881 register_tapset_procfs(s);
912e8c59 7882 register_tapset_timers(s);
b84779a5 7883 register_tapset_utrace(s);
b98a8d73 7884
7a24d422 7885 // dwarf-based kprobe/uprobe parts
c4ce66a1 7886 dwarf_derived_probe::register_patterns(s);
30a279be 7887
888af770
FCE
7888 // XXX: user-space starter set
7889 s.pattern_root->bind_num(TOK_PROCESS)
7890 ->bind_num(TOK_STATEMENT)->bind(TOK_ABSOLUTE)
27dc09b1 7891 ->bind_unprivileged()
888af770
FCE
7892 ->bind(new uprobe_builder ());
7893 s.pattern_root->bind_num(TOK_PROCESS)
7894 ->bind_num(TOK_STATEMENT)->bind(TOK_ABSOLUTE)->bind(TOK_RETURN)
27dc09b1 7895 ->bind_unprivileged()
888af770
FCE
7896 ->bind(new uprobe_builder ());
7897
0a6f5a3f
JS
7898 // kernel tracepoint probes
7899 s.pattern_root->bind(TOK_KERNEL)->bind_str(TOK_TRACE)
7900 ->bind(new tracepoint_builder());
7901
e6fe60e7
AM
7902 // Kprobe based probe
7903 s.pattern_root->bind(TOK_KPROBE)->bind_str(TOK_FUNCTION)
7904 ->bind(new kprobe_builder());
7905 s.pattern_root->bind(TOK_KPROBE)->bind_str(TOK_MODULE)
7906 ->bind_str(TOK_FUNCTION)->bind(new kprobe_builder());
7907 s.pattern_root->bind(TOK_KPROBE)->bind_str(TOK_FUNCTION)->bind(TOK_RETURN)
7908 ->bind(new kprobe_builder());
b6371390
JS
7909 s.pattern_root->bind(TOK_KPROBE)->bind_str(TOK_FUNCTION)->bind(TOK_RETURN)
7910 ->bind_num(TOK_MAXACTIVE)->bind(new kprobe_builder());
e6fe60e7
AM
7911 s.pattern_root->bind(TOK_KPROBE)->bind_str(TOK_MODULE)
7912 ->bind_str(TOK_FUNCTION)->bind(TOK_RETURN)->bind(new kprobe_builder());
b6371390
JS
7913 s.pattern_root->bind(TOK_KPROBE)->bind_str(TOK_MODULE)
7914 ->bind_str(TOK_FUNCTION)->bind(TOK_RETURN)
7915 ->bind_num(TOK_MAXACTIVE)->bind(new kprobe_builder());
e6fe60e7
AM
7916 s.pattern_root->bind(TOK_KPROBE)->bind_num(TOK_STATEMENT)
7917 ->bind(TOK_ABSOLUTE)->bind(new kprobe_builder());
dd225250
PS
7918
7919 //Hwbkpt based probe
b47f3a55
FCE
7920 // NB: we formerly registered the probe point types only if the kernel configuration
7921 // allowed it. However, we get better error messages if we allow probes to resolve.
7922 s.pattern_root->bind(TOK_KERNEL)->bind_num(TOK_HWBKPT)
7923 ->bind(TOK_HWBKPT_WRITE)->bind(new hwbkpt_builder());
7924 s.pattern_root->bind(TOK_KERNEL)->bind_str(TOK_HWBKPT)
7925 ->bind(TOK_HWBKPT_WRITE)->bind(new hwbkpt_builder());
7926 s.pattern_root->bind(TOK_KERNEL)->bind_num(TOK_HWBKPT)
7927 ->bind(TOK_HWBKPT_RW)->bind(new hwbkpt_builder());
7928 s.pattern_root->bind(TOK_KERNEL)->bind_str(TOK_HWBKPT)
7929 ->bind(TOK_HWBKPT_RW)->bind(new hwbkpt_builder());
7930 s.pattern_root->bind(TOK_KERNEL)->bind_num(TOK_HWBKPT)
7931 ->bind_num(TOK_LENGTH)->bind(TOK_HWBKPT_WRITE)->bind(new hwbkpt_builder());
7932 s.pattern_root->bind(TOK_KERNEL)->bind_num(TOK_HWBKPT)
7933 ->bind_num(TOK_LENGTH)->bind(TOK_HWBKPT_RW)->bind(new hwbkpt_builder());
7934 // length supported with address only, not symbol names
83ea76b1
WC
7935
7936 //perf event based probe
4763f713 7937 register_tapset_perf(s);
b55bc428 7938}
dc38c0ae
DS
7939
7940
b20febf3
FCE
7941vector<derived_probe_group*>
7942all_session_groups(systemtap_session& s)
dc38c0ae 7943{
b20febf3 7944 vector<derived_probe_group*> g;
912e8c59
JS
7945
7946#define DOONE(x) \
7947 if (s. x##_derived_probes) \
7948 g.push_back ((derived_probe_group*)(s. x##_derived_probes))
ab655cf8
DS
7949
7950 // Note that order *is* important here. We want to make sure we
7951 // register (actually run) begin probes before any other probe type
7952 // is run. Similarly, when unregistering probes, we want to
7953 // unregister (actually run) end probes after every other probe type
7954 // has be unregistered. To do the latter,
7955 // c_unparser::emit_module_exit() will run this list backwards.
b20febf3
FCE
7956 DOONE(be);
7957 DOONE(dwarf);
888af770 7958 DOONE(uprobe);
b20febf3
FCE
7959 DOONE(timer);
7960 DOONE(profile);
7961 DOONE(mark);
0a6f5a3f 7962 DOONE(tracepoint);
e6fe60e7 7963 DOONE(kprobe);
dd225250 7964 DOONE(hwbkpt);
83ea76b1 7965 DOONE(perf);
b20febf3 7966 DOONE(hrtimer);
ce82316f 7967 DOONE(procfs);
935447c8
DS
7968
7969 // Another "order is important" item. We want to make sure we
7970 // "register" the dummy task_finder probe group after all probe
7971 // groups that use the task_finder.
7972 DOONE(utrace);
a96d1db0 7973 DOONE(itrace);
935447c8 7974 DOONE(task_finder);
b20febf3
FCE
7975#undef DOONE
7976 return g;
46b84a80 7977}
73267b89
JS
7978
7979/* vim: set sw=2 ts=8 cino=>4,n-2,{2,^-2,t0,(0,u0,w1,M1 : */
This page took 2.514702 seconds and 5 git commands to generate.