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