]> sourceware.org Git - systemtap.git/blame - tapsets.cxx
BZ814248: staplog crash/defs.h support for arm
[systemtap.git] / tapsets.cxx
CommitLineData
56e12059 1// tapset resolution
e3bbc038 2// Copyright (C) 2005-2012 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,
7baf48e9
FCE
189 bool overload_processing,
190 bool suppress_handler_errors)
b20febf3 191{
a58d79d0
DS
192 if (overload_processing)
193 o->newline() << "#if defined(STP_TIMING) || defined(STP_OVERLOAD)";
194 else
195 o->newline() << "#ifdef STP_TIMING";
dbb68664 196 o->newline() << "{";
a58d79d0
DS
197 o->newline(1) << "cycles_t cycles_atend = get_cycles ();";
198 // NB: we truncate cycles counts to 32 bits. Perhaps it should be
199 // fewer, if the hardware counter rolls over really quickly. We
200 // handle 32-bit wraparound here.
201 o->newline() << "int32_t cycles_elapsed = ((int32_t)cycles_atend > (int32_t)cycles_atstart)";
202 o->newline(1) << "? ((int32_t)cycles_atend - (int32_t)cycles_atstart)";
203 o->newline() << ": (~(int32_t)0) - (int32_t)cycles_atstart + (int32_t)cycles_atend + 1;";
204 o->indent(-1);
dbb68664 205
a58d79d0 206 o->newline() << "#ifdef STP_TIMING";
994aac0e 207 o->newline() << "if (likely (stat)) _stp_stat_add(stat, cycles_elapsed);";
a58d79d0
DS
208 o->newline() << "#endif";
209
210 if (overload_processing)
211 {
212 o->newline() << "#ifdef STP_OVERLOAD";
213 o->newline() << "{";
214 // If the cycle count has wrapped (cycles_atend > cycles_base),
215 // let's go ahead and pretend the interval has been reached.
216 // This should reset cycles_base and cycles_sum.
217 o->newline(1) << "cycles_t interval = (cycles_atend > c->cycles_base)";
218 o->newline(1) << "? (cycles_atend - c->cycles_base)";
219 o->newline() << ": (STP_OVERLOAD_INTERVAL + 1);";
220 o->newline(-1) << "c->cycles_sum += cycles_elapsed;";
221
222 // If we've spent more than STP_OVERLOAD_THRESHOLD cycles in a
223 // probe during the last STP_OVERLOAD_INTERVAL cycles, the probe
224 // has overloaded the system and we need to quit.
7baf48e9
FCE
225 // NB: this is not suppressible via --suppress-runtime-errors,
226 // because this is a system safety metric that we cannot trust
227 // unprivileged users to override.
a58d79d0
DS
228 o->newline() << "if (interval > STP_OVERLOAD_INTERVAL) {";
229 o->newline(1) << "if (c->cycles_sum > STP_OVERLOAD_THRESHOLD) {";
230 o->newline(1) << "_stp_error (\"probe overhead exceeded threshold\");";
231 o->newline() << "atomic_set (&session_state, STAP_SESSION_ERROR);";
551e9f14 232 o->newline() << "atomic_inc (&error_count);";
a58d79d0 233 o->newline(-1) << "}";
e57b735a 234
a58d79d0
DS
235 o->newline() << "c->cycles_base = cycles_atend;";
236 o->newline() << "c->cycles_sum = 0;";
237 o->newline(-1) << "}";
238 o->newline(-1) << "}";
239 o->newline() << "#endif";
240 }
e57b735a 241
440f755a
JS
242 o->newline(-1) << "}";
243 o->newline() << "#endif";
e57b735a 244
440f755a 245 o->newline() << "c->probe_point = 0;"; // vacated
dc575eac 246 o->newline() << "#ifdef STP_NEED_PROBE_NAME";
d48df0cf 247 o->newline() << "c->probe_name = 0;";
dc575eac 248 o->newline() << "#endif";
6eefe942 249 o->newline() << "c->probe_type = 0;";
7baf48e9
FCE
250
251
440f755a 252 o->newline() << "if (unlikely (c->last_error && c->last_error[0])) {";
7baf48e9
FCE
253 o->indent(1);
254 if (suppress_handler_errors) // PR 13306
255 {
256 o->newline() << "atomic_inc (& error_count);";
257 }
258 else
259 {
260 o->newline() << "if (c->last_stmt != NULL)";
261 o->newline(1) << "_stp_softerror (\"%s near %s\", c->last_error, c->last_stmt);";
262 o->newline(-1) << "else";
263 o->newline(1) << "_stp_softerror (\"%s\", c->last_error);";
264 o->indent(-1);
265 o->newline() << "atomic_inc (& error_count);";
266 o->newline() << "if (atomic_read (& error_count) > MAXERRORS) {";
267 o->newline(1) << "atomic_set (& session_state, STAP_SESSION_ERROR);";
268 o->newline() << "_stp_exit ();";
269 o->newline(-1) << "}";
270 }
271
440f755a 272 o->newline(-1) << "}";
7baf48e9
FCE
273
274
440f755a 275 o->newline() << "atomic_dec (&c->busy);";
e57b735a 276
440f755a
JS
277 o->newline(-1) << "probe_epilogue:"; // context is free
278 o->indent(1);
e57b735a 279
7baf48e9
FCE
280 if (! suppress_handler_errors) // PR 13306
281 {
282 // Check for excessive skip counts.
283 o->newline() << "if (unlikely (atomic_read (& skipped_count) > MAXSKIPPED)) {";
284 o->newline(1) << "if (unlikely (pseudo_atomic_cmpxchg(& session_state, STAP_SESSION_RUNNING, STAP_SESSION_ERROR) == STAP_SESSION_RUNNING))";
285 o->newline() << "_stp_error (\"Skipped too many probes, check MAXSKIPPED or try again with stap -t for more details.\");";
286 o->newline(-1) << "}";
287 }
e57b735a 288
440f755a
JS
289 o->newline() << "#if INTERRUPTIBLE";
290 o->newline() << "preempt_enable_no_resched ();";
291 o->newline() << "#else";
292 o->newline() << "local_irq_restore (flags);";
293 o->newline() << "#endif";
653e6a9a
JS
294
295 o->newline() << "#endif // STP_ALIBI";
440f755a 296}
e57b735a 297
e57b735a 298
440f755a 299// ------------------------------------------------------------------------
e57b735a 300
440f755a
JS
301// ------------------------------------------------------------------------
302// Dwarf derived probes. "We apologize for the inconvience."
303// ------------------------------------------------------------------------
e57b735a 304
4627ed58
JS
305static const string TOK_KERNEL("kernel");
306static const string TOK_MODULE("module");
307static const string TOK_FUNCTION("function");
308static const string TOK_INLINE("inline");
309static const string TOK_CALL("call");
4bda987e 310static const string TOK_EXPORTED("exported");
4627ed58
JS
311static const string TOK_RETURN("return");
312static const string TOK_MAXACTIVE("maxactive");
313static const string TOK_STATEMENT("statement");
314static const string TOK_ABSOLUTE("absolute");
315static const string TOK_PROCESS("process");
a794dbeb 316static const string TOK_PROVIDER("provider");
4627ed58
JS
317static const string TOK_MARK("mark");
318static const string TOK_TRACE("trace");
319static const string TOK_LABEL("label");
63b4fd14 320static const string TOK_LIBRARY("library");
576eaefe 321static const string TOK_PLT("plt");
e57b735a 322
1adf8ef1 323static int query_cu (Dwarf_Die * cudie, void * arg);
6b517475 324static void query_addr(Dwarf_Addr addr, dwarf_query *q);
e57b735a 325
440f755a
JS
326// Can we handle this query with just symbol-table info?
327enum dbinfo_reqt
328{
329 dbr_unknown,
330 dbr_none, // kernel.statement(NUM).absolute
331 dbr_need_symtab, // can get by with symbol table if there's no dwarf
332 dbr_need_dwarf
333};
e57b735a 334
20e4a32c 335
440f755a
JS
336struct base_query; // forward decls
337struct dwarf_query;
338struct dwflpp;
339struct symbol_table;
20e4a32c 340
a781f401 341
440f755a
JS
342struct
343symbol_table
344{
345 module_info *mod_info; // associated module
346 map<string, func_info*> map_by_name;
1c6b77e5
JS
347 multimap<Dwarf_Addr, func_info*> map_by_addr;
348 typedef multimap<Dwarf_Addr, func_info*>::iterator iterator_t;
440f755a
JS
349 typedef pair<iterator_t, iterator_t> range_t;
350#ifdef __powerpc__
351 GElf_Word opd_section;
352#endif
2867a2a1
JS
353 void add_symbol(const char *name, bool weak, bool descriptor,
354 Dwarf_Addr addr, Dwarf_Addr *high_addr);
440f755a 355 enum info_status read_symbols(FILE *f, const string& path);
83ca3872 356 enum info_status read_from_elf_file(const string& path,
2713ea24 357 systemtap_session &sess);
83ca3872 358 enum info_status read_from_text_file(const string& path,
2713ea24 359 systemtap_session &sess);
440f755a
JS
360 enum info_status get_from_elf();
361 void prepare_section_rejection(Dwfl_Module *mod);
362 bool reject_section(GElf_Word section);
440f755a
JS
363 void purge_syscall_stubs();
364 func_info *lookup_symbol(const string& name);
365 Dwarf_Addr lookup_symbol_address(const string& name);
366 func_info *get_func_containing_address(Dwarf_Addr addr);
3d372d6b 367 func_info *get_first_func();
7a053d3b 368
440f755a
JS
369 symbol_table(module_info *mi) : mod_info(mi) {}
370 ~symbol_table();
371};
77de5e9e 372
440f755a
JS
373static bool null_die(Dwarf_Die *die)
374{
822a6a3d 375 static Dwarf_Die null;
440f755a
JS
376 return (!die || !memcmp(die, &null, sizeof(null)));
377}
c4ce66a1
JS
378
379
7a053d3b 380enum
bd2b1e68 381function_spec_type
7a053d3b 382 {
bd2b1e68
GH
383 function_alone,
384 function_and_file,
7a053d3b 385 function_file_and_line
bd2b1e68
GH
386 };
387
ec4373ff 388
bd2b1e68 389struct dwarf_builder;
f10534c6 390struct dwarf_var_expanding_visitor;
77de5e9e 391
2930abc7 392
b20febf3
FCE
393// XXX: This class is a candidate for subclassing to separate
394// the relocation vs non-relocation variants. Likewise for
395// kprobe vs kretprobe variants.
396
397struct dwarf_derived_probe: public derived_probe
b55bc428 398{
b20febf3
FCE
399 dwarf_derived_probe (const string& function,
400 const string& filename,
401 int line,
402 const string& module,
403 const string& section,
404 Dwarf_Addr dwfl_addr,
2930abc7 405 Dwarf_Addr addr,
b20febf3
FCE
406 dwarf_query & q,
407 Dwarf_Die* scope_die);
20e4a32c 408
b20febf3
FCE
409 string module;
410 string section;
411 Dwarf_Addr addr;
63b4fd14 412 string path;
27dc09b1 413 bool has_process;
2930abc7 414 bool has_return;
c9bad430 415 bool has_maxactive;
63b4fd14 416 bool has_library;
c9bad430 417 long maxactive_val;
4ad95bbc 418 // dwarf_derived_probe_group::emit_module_decls uses this to emit sdt kprobe definition
b642c901
SC
419 string user_path;
420 string user_lib;
b95e2b79 421 bool access_vars;
2930abc7 422
af234c40
JS
423 unsigned saved_longs, saved_strings;
424 dwarf_derived_probe* entry_handler;
425
b8da0ad1 426 void printsig (std::ostream &o) const;
6b66b9f7 427 virtual void join_group (systemtap_session& s);
9020300d 428 void emit_probe_local_init(translator_output * o);
d0bfd2ac 429 void getargs(std::list<std::string> &arg_set) const;
0a98fd42 430
42e38653 431 void emit_privilege_assertion (translator_output*);
27dc09b1
DB
432 void print_dupe_stamp(ostream& o);
433
bd2b1e68 434 // Pattern registration helpers.
7a053d3b 435 static void register_statement_variants(match_node * root,
27dc09b1 436 dwarf_builder * dw,
42e38653 437 privilege_t privilege);
fd6602a0 438 static void register_function_variants(match_node * root,
27dc09b1 439 dwarf_builder * dw,
42e38653 440 privilege_t privilege);
440d9b00
DB
441 static void register_function_and_statement_variants(systemtap_session& s,
442 match_node * root,
27dc09b1 443 dwarf_builder * dw,
42e38653 444 privilege_t privilege);
b1615c74
JS
445 static void register_sdt_variants(systemtap_session& s,
446 match_node * root,
447 dwarf_builder * dw);
448 static void register_plt_variants(systemtap_session& s,
449 match_node * root,
450 dwarf_builder * dw);
c4ce66a1 451 static void register_patterns(systemtap_session& s);
6b66b9f7
JS
452
453protected:
454 dwarf_derived_probe(probe *base,
455 probe_point *location,
456 Dwarf_Addr addr,
457 bool has_return):
74fe61bc
LB
458 derived_probe(base, location), addr(addr), has_process(0),
459 has_return(has_return), has_maxactive(0), has_library(0),
460 maxactive_val(0), access_vars(false), saved_longs(0),
461 saved_strings(0), entry_handler(0)
6b66b9f7
JS
462 {}
463
464private:
d0bfd2ac 465 list<string> args;
8c67c337 466 void saveargs(dwarf_query& q, Dwarf_Die* scope_die, Dwarf_Addr dwfl_addr);
20c6c071
GH
467};
468
dc38c0ae 469
6b66b9f7 470struct uprobe_derived_probe: public dwarf_derived_probe
6d0f3f0c 471{
6d0f3f0c 472 int pid; // 0 => unrestricted
0973d815 473
6d0f3f0c
FCE
474 uprobe_derived_probe (const string& function,
475 const string& filename,
476 int line,
477 const string& module,
6d0f3f0c
FCE
478 const string& section,
479 Dwarf_Addr dwfl_addr,
480 Dwarf_Addr addr,
481 dwarf_query & q,
6b66b9f7
JS
482 Dwarf_Die* scope_die):
483 dwarf_derived_probe(function, filename, line, module, section,
484 dwfl_addr, addr, q, scope_die), pid(0)
485 {}
6d0f3f0c 486
0973d815
FCE
487 // alternate constructor for process(PID).statement(ADDR).absolute
488 uprobe_derived_probe (probe *base,
489 probe_point *location,
490 int pid,
491 Dwarf_Addr addr,
6b66b9f7
JS
492 bool has_return):
493 dwarf_derived_probe(base, location, addr, has_return), pid(pid)
494 {}
9ace370f 495
6d0f3f0c 496 void join_group (systemtap_session& s);
2865d17a 497
42e38653 498 void emit_privilege_assertion (translator_output*);
8f6d8c2b 499 void print_dupe_stamp(ostream& o) { print_dupe_stamp_unprivileged_process_owner (o); }
c0f84e7b
SC
500 void getargs(std::list<std::string> &arg_set) const;
501 void saveargs(int nargs);
502private:
503 list<string> args;
6d0f3f0c
FCE
504};
505
dc38c0ae
DS
506struct dwarf_derived_probe_group: public derived_probe_group
507{
508private:
62f52bb0 509 bool has_semaphores;
b20febf3
FCE
510 multimap<string,dwarf_derived_probe*> probes_by_module;
511 typedef multimap<string,dwarf_derived_probe*>::iterator p_b_m_iterator;
dc38c0ae
DS
512
513public:
62f52bb0 514 dwarf_derived_probe_group(): has_semaphores(false) {}
b20febf3
FCE
515 void enroll (dwarf_derived_probe* probe);
516 void emit_module_decls (systemtap_session& s);
517 void emit_module_init (systemtap_session& s);
b4be7cbc 518 void emit_module_refresh (systemtap_session& s);
b20febf3 519 void emit_module_exit (systemtap_session& s);
dc38c0ae
DS
520};
521
522
20c6c071 523// Helper struct to thread through the dwfl callbacks.
2c384610 524struct base_query
20c6c071 525{
c4ce66a1
JS
526 base_query(dwflpp & dw, literal_map_t const & params);
527 base_query(dwflpp & dw, const string & module_val);
2c384610 528 virtual ~base_query() {}
bd2b1e68 529
5227f1ea 530 systemtap_session & sess;
2c384610 531 dwflpp & dw;
5227f1ea 532
bd2b1e68 533 // Parameter extractors.
86bf665e 534 static bool has_null_param(literal_map_t const & params,
888af770 535 string const & k);
86bf665e 536 static bool get_string_param(literal_map_t const & params,
bd2b1e68 537 string const & k, string & v);
86bf665e 538 static bool get_number_param(literal_map_t const & params,
bd2b1e68 539 string const & k, long & v);
86bf665e 540 static bool get_number_param(literal_map_t const & params,
c239d28c 541 string const & k, Dwarf_Addr & v);
f301a9ff 542 static void query_library_callback (void *object, const char *data);
576eaefe 543 static void query_plt_callback (void *object, const char *link, size_t addr);
f301a9ff 544 virtual void query_library (const char *data) = 0;
576eaefe 545 virtual void query_plt (const char *link, size_t addr) = 0;
84c84ac4 546
b55bc428 547
2c384610
DS
548 // Extracted parameters.
549 bool has_kernel;
91af0778
FCE
550 bool has_module;
551 bool has_process;
63b4fd14 552 bool has_library;
576eaefe
SC
553 bool has_plt;
554 bool has_statement;
2c384610 555 string module_val; // has_kernel => module_val = "kernel"
63b4fd14 556 string path; // executable path if module is a .so
576eaefe 557 string plt_val; // has_plt => plt wildcard
2c384610
DS
558
559 virtual void handle_query_module() = 0;
560};
561
562
c4ce66a1 563base_query::base_query(dwflpp & dw, literal_map_t const & params):
576eaefe 564 sess(dw.sess), dw(dw), has_library(false), has_plt(false), has_statement(false)
2c384610 565{
91af0778 566 has_kernel = has_null_param (params, TOK_KERNEL);
2c384610
DS
567 if (has_kernel)
568 module_val = "kernel";
91af0778
FCE
569
570 has_module = get_string_param (params, TOK_MODULE, module_val);
571 if (has_module)
572 has_process = false;
4baf0e53 573 else
d0a7f5a9 574 {
63b4fd14 575 string library_name;
576eaefe 576 long statement_num_val;
d0a7f5a9 577 has_process = get_string_param(params, TOK_PROCESS, module_val);
63b4fd14 578 has_library = get_string_param (params, TOK_LIBRARY, library_name);
576eaefe
SC
579 if ((has_plt = has_null_param (params, TOK_PLT)))
580 plt_val = "*";
581 else has_plt = get_string_param (params, TOK_PLT, plt_val);
18418d34
SC
582 if (has_plt)
583 sess.consult_symtab = true;
576eaefe
SC
584 has_statement = get_number_param(params, TOK_STATEMENT, statement_num_val);
585
84c84ac4 586 if (has_process)
05fb3e0c 587 module_val = find_executable (module_val, sess.sysroot, sess.sysenv);
84c84ac4
SC
588 if (has_library)
589 {
590 if (! contains_glob_chars (library_name))
591 {
05fb3e0c
WF
592 path = path_remove_sysroot(sess, module_val);
593 module_val = find_executable (library_name, sess.sysroot,
594 sess.sysenv, "LD_LIBRARY_PATH");
47e226ed
SC
595 if (module_val.find('/') == string::npos)
596 {
597 // We didn't find library_name so use iterate_over_libraries
598 module_val = path;
599 path = library_name;
600 }
84c84ac4
SC
601 }
602 else
603 path = library_name;
604 }
d0a7f5a9 605 }
91af0778
FCE
606
607 assert (has_kernel || has_process || has_module);
2c384610
DS
608}
609
c4ce66a1 610base_query::base_query(dwflpp & dw, const string & module_val)
576eaefe
SC
611 : sess(dw.sess), dw(dw), has_library(false), has_plt(false), has_statement(false),
612 module_val(module_val)
c4ce66a1
JS
613{
614 // NB: This uses '/' to distinguish between kernel modules and userspace,
615 // which means that userspace modules won't get any PATH searching.
616 if (module_val.find('/') == string::npos)
617 {
618 has_kernel = (module_val == TOK_KERNEL);
619 has_module = !has_kernel;
620 has_process = false;
621 }
622 else
623 {
624 has_kernel = has_module = false;
625 has_process = true;
626 }
627}
628
2c384610 629bool
86bf665e 630base_query::has_null_param(literal_map_t const & params,
2c384610
DS
631 string const & k)
632{
888af770 633 return derived_probe_builder::has_null_param(params, k);
2c384610
DS
634}
635
636
637bool
86bf665e 638base_query::get_string_param(literal_map_t const & params,
2c384610
DS
639 string const & k, string & v)
640{
641 return derived_probe_builder::get_param (params, k, v);
642}
643
644
645bool
86bf665e 646base_query::get_number_param(literal_map_t const & params,
2c384610
DS
647 string const & k, long & v)
648{
649 int64_t value;
650 bool present = derived_probe_builder::get_param (params, k, value);
651 v = (long) value;
652 return present;
653}
654
655
656bool
86bf665e 657base_query::get_number_param(literal_map_t const & params,
2c384610
DS
658 string const & k, Dwarf_Addr & v)
659{
660 int64_t value;
661 bool present = derived_probe_builder::get_param (params, k, value);
662 v = (Dwarf_Addr) value;
663 return present;
664}
665
2c384610
DS
666struct dwarf_query : public base_query
667{
e1278bd4 668 dwarf_query(probe * base_probe,
2c384610
DS
669 probe_point * base_loc,
670 dwflpp & dw,
86bf665e 671 literal_map_t const & params,
b642c901
SC
672 vector<derived_probe *> & results,
673 const string user_path,
674 const string user_lib);
2c384610 675
c4ce66a1 676 vector<derived_probe *> & results;
8f14e444 677 set<string> inlined_non_returnable; // function names
c4ce66a1
JS
678 probe * base_probe;
679 probe_point * base_loc;
b642c901
SC
680 string user_path;
681 string user_lib;
c4ce66a1 682
2c384610 683 virtual void handle_query_module();
5f0a03a6
JK
684 void query_module_dwarf();
685 void query_module_symtab();
5d5bd369 686 void query_library (const char *data);
576eaefe 687 void query_plt (const char *entry, size_t addr);
2c384610 688
2930abc7
FCE
689 void add_probe_point(string const & funcname,
690 char const * filename,
691 int line,
692 Dwarf_Die *scope_die,
693 Dwarf_Addr addr);
36f9dd1d 694
857bdfd1
JS
695 // Track addresses we've already seen in a given module
696 set<Dwarf_Addr> alias_dupes;
697
7fdd3e2c
JS
698 // Track inlines we've already seen as well
699 // NB: this can't be compared just by entrypc, as inlines can overlap
700 set<inline_instance_info> inline_dupes;
701
2930abc7 702 // Extracted parameters.
7a053d3b 703 string function_val;
20c6c071
GH
704
705 bool has_function_str;
706 bool has_statement_str;
707 bool has_function_num;
708 bool has_statement_num;
7a053d3b
RM
709 string statement_str_val;
710 string function_str_val;
c239d28c
GH
711 Dwarf_Addr statement_num_val;
712 Dwarf_Addr function_num_val;
20c6c071 713
b8da0ad1 714 bool has_call;
4bda987e 715 bool has_exported;
b8da0ad1 716 bool has_inline;
20c6c071
GH
717 bool has_return;
718
c9bad430
DS
719 bool has_maxactive;
720 long maxactive_val;
721
20c6c071
GH
722 bool has_label;
723 string label_val;
724
725 bool has_relative;
726 long relative_val;
727
37ebca01
FCE
728 bool has_absolute;
729
467bea43
SC
730 bool has_mark;
731
5f0a03a6
JK
732 enum dbinfo_reqt dbinfo_reqt;
733 enum dbinfo_reqt assess_dbinfo_reqt();
734
7d6d0afc 735 void parse_function_spec(const string & spec);
20c6c071 736 function_spec_type spec_type;
7d6d0afc 737 vector<string> scopes;
20c6c071
GH
738 string function;
739 string file;
0c8b7d37 740 line_t line_type;
879eb9e9 741 int line[2];
5f0a03a6 742 bool query_done; // Found exact match
20c6c071 743
bd25380d 744 set<string> filtered_srcfiles;
7e1279ea
FCE
745
746 // Map official entrypc -> func_info object
86bf665e
TM
747 inline_instance_map_t filtered_inlines;
748 func_info_map_t filtered_functions;
7e1279ea
FCE
749 bool choose_next_line;
750 Dwarf_Addr entrypc_for_next_line;
4df79aaf
JS
751
752 void query_module_functions ();
b55bc428
FCE
753};
754
98afd80e 755
435f53a7
FCE
756static void delete_session_module_cache (systemtap_session& s); // forward decl
757
758
98afd80e 759struct dwarf_builder: public derived_probe_builder
b55bc428 760{
665e1256 761 map <string,dwflpp*> kern_dw; /* NB: key string could be a wildcard */
7a24d422 762 map <string,dwflpp*> user_dw;
b642c901
SC
763 string user_path;
764 string user_lib;
ae2552da 765 dwarf_builder() {}
aa30ccd3 766
ae2552da 767 dwflpp *get_kern_dw(systemtap_session& sess, const string& module)
707bf35e 768 {
ea14cf67
FCE
769 if (kern_dw[module] == 0)
770 kern_dw[module] = new dwflpp(sess, module, true); // might throw
ae2552da 771 return kern_dw[module];
707bf35e
JS
772 }
773
774 dwflpp *get_user_dw(systemtap_session& sess, const string& module)
775 {
ea14cf67
FCE
776 if (user_dw[module] == 0)
777 user_dw[module] = new dwflpp(sess, module, false); // might throw
707bf35e
JS
778 return user_dw[module];
779 }
7a24d422
FCE
780
781 /* NB: not virtual, so can be called from dtor too: */
822a6a3d 782 void dwarf_build_no_more (bool)
aa30ccd3 783 {
435f53a7
FCE
784 delete_map(kern_dw);
785 delete_map(user_dw);
7a24d422
FCE
786 }
787
788 void build_no_more (systemtap_session &s)
789 {
790 dwarf_build_no_more (s.verbose > 3);
435f53a7 791 delete_session_module_cache (s);
aa30ccd3
FCE
792 }
793
e38d6504
RM
794 ~dwarf_builder()
795 {
7a24d422 796 dwarf_build_no_more (false);
c8959a29 797 }
aa30ccd3 798
5227f1ea 799 virtual void build(systemtap_session & sess,
7a053d3b 800 probe * base,
20c6c071 801 probe_point * location,
86bf665e 802 literal_map_t const & parameters,
20c6c071 803 vector<derived_probe *> & finished_results);
b55bc428
FCE
804};
805
5111fc3e 806
e1278bd4 807dwarf_query::dwarf_query(probe * base_probe,
20c6c071
GH
808 probe_point * base_loc,
809 dwflpp & dw,
86bf665e 810 literal_map_t const & params,
b642c901
SC
811 vector<derived_probe *> & results,
812 const string user_path,
813 const string user_lib)
c4ce66a1 814 : base_query(dw, params), results(results),
b642c901 815 base_probe(base_probe), base_loc(base_loc),
74fe61bc
LB
816 user_path(user_path), user_lib(user_lib), has_relative(false),
817 relative_val(0), choose_next_line(false), entrypc_for_next_line(0)
bd2b1e68
GH
818{
819 // Reduce the query to more reasonable semantic values (booleans,
820 // extracted strings, numbers, etc).
bd2b1e68
GH
821 has_function_str = get_string_param(params, TOK_FUNCTION, function_str_val);
822 has_function_num = get_number_param(params, TOK_FUNCTION, function_num_val);
823
824 has_statement_str = get_string_param(params, TOK_STATEMENT, statement_str_val);
825 has_statement_num = get_number_param(params, TOK_STATEMENT, statement_num_val);
826
0f336e95
SC
827 has_label = get_string_param(params, TOK_LABEL, label_val);
828
b8da0ad1 829 has_call = has_null_param(params, TOK_CALL);
4bda987e 830 has_exported = has_null_param(params, TOK_EXPORTED);
b8da0ad1 831 has_inline = has_null_param(params, TOK_INLINE);
bd2b1e68 832 has_return = has_null_param(params, TOK_RETURN);
c9bad430 833 has_maxactive = get_number_param(params, TOK_MAXACTIVE, maxactive_val);
37ebca01 834 has_absolute = has_null_param(params, TOK_ABSOLUTE);
467bea43 835 has_mark = false;
37ebca01 836
bd2b1e68 837 if (has_function_str)
7d6d0afc 838 parse_function_spec(function_str_val);
bd2b1e68 839 else if (has_statement_str)
7d6d0afc 840 parse_function_spec(statement_str_val);
0daad364 841
5f0a03a6
JK
842 dbinfo_reqt = assess_dbinfo_reqt();
843 query_done = false;
0daad364
JS
844}
845
846
440f755a
JS
847func_info_map_t *
848get_filtered_functions(dwarf_query *q)
849{
850 return &q->filtered_functions;
851}
852
853
854inline_instance_map_t *
855get_filtered_inlines(dwarf_query *q)
856{
857 return &q->filtered_inlines;
858}
859
860
2c384610 861void
5f0a03a6 862dwarf_query::query_module_dwarf()
2c384610
DS
863{
864 if (has_function_num || has_statement_num)
865 {
866 // If we have module("foo").function(0xbeef) or
867 // module("foo").statement(0xbeef), the address is relative
868 // to the start of the module, so we seek the function
869 // number plus the module's bias.
6b517475
JS
870 Dwarf_Addr addr = has_function_num ?
871 function_num_val : statement_num_val;
08d1d520
MW
872
873 // These are raw addresses, we need to know what the elf_bias
874 // is to feed it to libdwfl based functions.
875 Dwarf_Addr elf_bias;
876 Elf *elf = dwfl_module_getelf (dw.module, &elf_bias);
877 assert(elf);
878 addr += elf_bias;
6b517475 879 query_addr(addr, this);
2c384610
DS
880 }
881 else
882 {
883 // Otherwise if we have a function("foo") or statement("foo")
884 // specifier, we have to scan over all the CUs looking for
885 // the function(s) in question
886 assert(has_function_str || has_statement_str);
4df79aaf
JS
887
888 // For simple cases, no wildcard and no source:line, we can do a very
889 // quick function lookup in a module-wide cache.
1ffb8bd1
JS
890 if (spec_type == function_alone &&
891 !dw.name_has_wildcard(function) &&
892 !startswith(function, "_Z"))
4df79aaf
JS
893 query_module_functions();
894 else
337b7c44 895 dw.iterate_over_cus(&query_cu, this, false);
2c384610
DS
896 }
897}
898
5f0a03a6
JK
899static void query_func_info (Dwarf_Addr entrypc, func_info & fi,
900 dwarf_query * q);
901
902void
903dwarf_query::query_module_symtab()
904{
905 // Get the symbol table if it's necessary, sufficient, and not already got.
906 if (dbinfo_reqt == dbr_need_dwarf)
907 return;
908
909 module_info *mi = dw.mod_info;
910 if (dbinfo_reqt == dbr_need_symtab)
911 {
912 if (mi->symtab_status == info_unknown)
913 mi->get_symtab(this);
914 if (mi->symtab_status == info_absent)
915 return;
916 }
917
918 func_info *fi = NULL;
919 symbol_table *sym_table = mi->sym_table;
920
921 if (has_function_str)
922 {
923 // Per dwarf_query::assess_dbinfo_reqt()...
924 assert(spec_type == function_alone);
925 if (dw.name_has_wildcard(function_str_val))
926 {
927 // Until we augment the blacklist sufficently...
928 if (function_str_val.find_first_not_of("*?") == string::npos)
929 {
930 // e.g., kernel.function("*")
b530b5b3
LB
931 cerr << _F("Error: Pattern '%s' matches every single "
932 "instruction address in the symbol table,\n"
933 "some of which aren't even functions.\n", function_str_val.c_str()) << endl;
5f0a03a6
JK
934 return;
935 }
2e67a43b 936 symbol_table::iterator_t iter;
1c6b77e5
JS
937 for (iter = sym_table->map_by_addr.begin();
938 iter != sym_table->map_by_addr.end();
2e67a43b 939 ++iter)
5f0a03a6 940 {
1c6b77e5 941 fi = iter->second;
5f0a03a6
JK
942 if (!null_die(&fi->die))
943 continue; // already handled in query_module_dwarf()
944 if (dw.function_name_matches_pattern(fi->name, function_str_val))
945 query_func_info(fi->addr, *fi, this);
946 }
947 }
948 else
949 {
950 fi = sym_table->lookup_symbol(function_str_val);
2867a2a1 951 if (fi && !fi->descriptor && null_die(&fi->die))
5f0a03a6
JK
952 query_func_info(fi->addr, *fi, this);
953 }
954 }
955 else
956 {
957 assert(has_function_num || has_statement_num);
958 // Find the "function" in which the indicated address resides.
959 Dwarf_Addr addr =
960 (has_function_num ? function_num_val : statement_num_val);
576eaefe 961 if (has_plt)
3d372d6b
SC
962 {
963 // Use the raw address from the .plt
964 fi = sym_table->get_first_func();
965 fi->addr = addr;
966 }
967 else
968 fi = sym_table->get_func_containing_address(addr);
969
5f0a03a6
JK
970 if (!fi)
971 {
2713ea24
CM
972 sess.print_warning(_F("address %#" PRIx64 " out of range for module %s",
973 addr, dw.module_name.c_str()));
974 return;
5f0a03a6
JK
975 }
976 if (!null_die(&fi->die))
977 {
978 // addr looks like it's in the compilation unit containing
979 // the indicated function, but query_module_dwarf() didn't
980 // match addr to any compilation unit, so addr must be
981 // above that cu's address range.
2713ea24
CM
982 sess.print_warning(_F("address %#" PRIx64 " maps to no known compilation unit in module %s",
983 addr, dw.module_name.c_str()));
5f0a03a6
JK
984 return;
985 }
986 query_func_info(fi->addr, *fi, this);
987 }
988}
989
990void
991dwarf_query::handle_query_module()
992{
1c6b77e5
JS
993 bool report = dbinfo_reqt == dbr_need_dwarf || !sess.consult_symtab;
994 dw.get_module_dwarf(false, report);
995
996 // prebuild the symbol table to resolve aliases
997 dw.mod_info->get_symtab(this);
998
857bdfd1
JS
999 // reset the dupe-checking for each new module
1000 alias_dupes.clear();
7fdd3e2c 1001 inline_dupes.clear();
857bdfd1 1002
5f0a03a6
JK
1003 if (dw.mod_info->dwarf_status == info_present)
1004 query_module_dwarf();
1c6b77e5 1005
5f0a03a6
JK
1006 // Consult the symbol table if we haven't found all we're looking for.
1007 // asm functions can show up in the symbol table but not in dwarf.
1008 if (sess.consult_symtab && !query_done)
1009 query_module_symtab();
1010}
1011
2c384610 1012
7d6d0afc
JS
1013void
1014dwarf_query::parse_function_spec(const string & spec)
bd2b1e68 1015{
1d12a9b2
JS
1016 line_type = ABSOLUTE;
1017 line[0] = line[1] = 0;
1018
91699a70 1019 size_t src_pos, line_pos, dash_pos, scope_pos;
bd2b1e68 1020
7d6d0afc 1021 // look for named scopes
91699a70
JS
1022 scope_pos = spec.rfind("::");
1023 if (scope_pos != string::npos)
bd2b1e68 1024 {
91699a70
JS
1025 tokenize_cxx(spec.substr(0, scope_pos), scopes);
1026 scope_pos += 2;
bd2b1e68 1027 }
91699a70
JS
1028 else
1029 scope_pos = 0;
bd2b1e68 1030
7d6d0afc
JS
1031 // look for a source separator
1032 src_pos = spec.find('@', scope_pos);
1033 if (src_pos == string::npos)
bd2b1e68 1034 {
7d6d0afc
JS
1035 function = spec.substr(scope_pos);
1036 spec_type = function_alone;
bd2b1e68 1037 }
7d6d0afc 1038 else
879eb9e9 1039 {
7d6d0afc 1040 function = spec.substr(scope_pos, src_pos - scope_pos);
7a053d3b 1041
7d6d0afc
JS
1042 // look for a line-number separator
1043 line_pos = spec.find_first_of(":+", src_pos);
1044 if (line_pos == string::npos)
1045 {
1046 file = spec.substr(src_pos + 1);
1047 spec_type = function_and_file;
1048 }
1049 else
1050 {
1051 file = spec.substr(src_pos + 1, line_pos - src_pos - 1);
1052
1053 // classify the line spec
1054 spec_type = function_file_and_line;
1055 if (spec[line_pos] == '+')
1056 line_type = RELATIVE;
1057 else if (spec[line_pos + 1] == '*' &&
1058 spec.length() == line_pos + 2)
1059 line_type = WILDCARD;
1060 else
1061 line_type = ABSOLUTE;
1062
1063 if (line_type != WILDCARD)
1064 try
1065 {
1066 // try to parse either N or N-M
1067 dash_pos = spec.find('-', line_pos + 1);
1068 if (dash_pos == string::npos)
1069 line[0] = line[1] = lex_cast<int>(spec.substr(line_pos + 1));
1070 else
1071 {
1072 line_type = RANGE;
1073 line[0] = lex_cast<int>(spec.substr(line_pos + 1,
1074 dash_pos - line_pos - 1));
1075 line[1] = lex_cast<int>(spec.substr(dash_pos + 1));
1076 }
1077 }
1078 catch (runtime_error & exn)
1079 {
1080 goto bad;
1081 }
1082 }
bd2b1e68
GH
1083 }
1084
7d6d0afc
JS
1085 if (function.empty() ||
1086 (spec_type != function_alone && file.empty()))
bd2b1e68
GH
1087 goto bad;
1088
7d6d0afc 1089 if (sess.verbose > 2)
bd2b1e68 1090 {
b530b5b3
LB
1091 //clog << "parsed '" << spec << "'";
1092 clog << _F("parse '%s'", spec.c_str());
41c262f3 1093
7d6d0afc
JS
1094 if (!scopes.empty())
1095 clog << ", scope '" << scopes[0] << "'";
1096 for (unsigned i = 1; i < scopes.size(); ++i)
1097 clog << "::'" << scopes[i] << "'";
41c262f3 1098
7d6d0afc
JS
1099 clog << ", func '" << function << "'";
1100
1101 if (spec_type != function_alone)
1102 clog << ", file '" << file << "'";
1103
1104 if (spec_type == function_file_and_line)
1105 {
1106 clog << ", line ";
1107 switch (line_type)
1108 {
1109 case ABSOLUTE:
1110 clog << line[0];
1111 break;
1112
1113 case RELATIVE:
1114 clog << "+" << line[0];
1115 break;
1116
1117 case RANGE:
1118 clog << line[0] << " - " << line[1];
1119 break;
1120
1121 case WILDCARD:
1122 clog << "*";
1123 break;
1124 }
1125 }
1126
1127 clog << endl;
bd2b1e68
GH
1128 }
1129
7d6d0afc
JS
1130 return;
1131
1132bad:
b530b5b3 1133 throw semantic_error(_F("malformed specification '%s'", spec.c_str()),
7d6d0afc 1134 base_probe->tok);
bd2b1e68
GH
1135}
1136
05fb3e0c
WF
1137string path_remove_sysroot(const systemtap_session& sess, const string& path)
1138{
1139 size_t pos;
1140 string retval = path;
1141 if (!sess.sysroot.empty() &&
1142 (pos = retval.find(sess.sysroot)) != string::npos)
1143 retval.replace(pos, sess.sysroot.length(), "/");
1144 return retval;
1145}
bd2b1e68 1146
36f9dd1d 1147void
1ffb8bd1 1148dwarf_query::add_probe_point(const string& dw_funcname,
b20febf3 1149 const char* filename,
36f9dd1d 1150 int line,
b20febf3 1151 Dwarf_Die* scope_die,
36f9dd1d
FCE
1152 Dwarf_Addr addr)
1153{
b20febf3 1154 string reloc_section; // base section for relocation purposes
27646582 1155 Dwarf_Addr reloc_addr; // relocated
b20febf3 1156 const string& module = dw.module_name; // "kernel" or other
1ffb8bd1 1157 string funcname = dw_funcname;
36f9dd1d 1158
37ebca01
FCE
1159 assert (! has_absolute); // already handled in dwarf_builder::build()
1160
576eaefe
SC
1161 if (!has_plt)
1162 reloc_addr = dw.relocate_address(addr, reloc_section);
1163 else
1164 {
3d372d6b 1165 // Set the reloc_section but use the plt entry for reloc_addr
576eaefe
SC
1166 dw.relocate_address(addr, reloc_section);
1167 reloc_addr = addr;
1168 }
2930abc7 1169
1ffb8bd1 1170 // If we originally used the linkage name, then let's call it that way
1ffb8bd1
JS
1171 const char* linkage_name;
1172 if (scope_die && startswith (this->function, "_Z")
f450a7e3 1173 && (linkage_name = dwarf_linkage_name (scope_die)))
1ffb8bd1
JS
1174 funcname = linkage_name;
1175
7f9f3386
FCE
1176 if (sess.verbose > 1)
1177 {
b530b5b3 1178 clog << _("probe ") << funcname << "@" << filename << ":" << line;
b20febf3 1179 if (string(module) == TOK_KERNEL)
b530b5b3 1180 clog << _(" kernel");
91af0778 1181 else if (has_module)
b530b5b3 1182 clog << _(" module=") << module;
91af0778 1183 else if (has_process)
b530b5b3 1184 clog << _(" process=") << module;
b20febf3 1185 if (reloc_section != "") clog << " reloc=" << reloc_section;
b20febf3 1186 clog << " pc=0x" << hex << addr << dec;
7f9f3386 1187 }
4baf0e53 1188
27646582 1189 bool bad = dw.blacklisted_p (funcname, filename, line, module,
789448a3 1190 addr, has_return);
b20febf3
FCE
1191 if (sess.verbose > 1)
1192 clog << endl;
7f9f3386 1193
84048984
FCE
1194 if (module == TOK_KERNEL)
1195 {
1196 // PR 4224: adapt to relocatable kernel by subtracting the _stext address here.
1197 reloc_addr = addr - sess.sym_stext;
37ebca01 1198 reloc_section = "_stext"; // a message to runtime's _stp_module_relocate
84048984
FCE
1199 }
1200
b20febf3
FCE
1201 if (! bad)
1202 {
1a0dbc5a 1203 sess.unwindsym_modules.insert (module);
6d0f3f0c
FCE
1204
1205 if (has_process)
1206 {
05fb3e0c 1207 string module_tgt = path_remove_sysroot(sess, module);
6d0f3f0c 1208 results.push_back (new uprobe_derived_probe(funcname, filename, line,
05fb3e0c 1209 module_tgt, reloc_section, addr, reloc_addr,
6d0f3f0c
FCE
1210 *this, scope_die));
1211 }
1212 else
1213 {
1214 assert (has_kernel || has_module);
1215 results.push_back (new dwarf_derived_probe(funcname, filename, line,
06aca46a 1216 module, reloc_section, addr, reloc_addr,
6d0f3f0c
FCE
1217 *this, scope_die));
1218 }
b20febf3 1219 }
2930abc7
FCE
1220}
1221
5f0a03a6
JK
1222enum dbinfo_reqt
1223dwarf_query::assess_dbinfo_reqt()
1224{
1225 if (has_absolute)
1226 {
1227 // kernel.statement(NUM).absolute
1228 return dbr_none;
1229 }
1230 if (has_inline)
1231 {
1232 // kernel.function("f").inline or module("m").function("f").inline
1233 return dbr_need_dwarf;
1234 }
1235 if (has_function_str && spec_type == function_alone)
1236 {
1237 // kernel.function("f") or module("m").function("f")
1238 return dbr_need_symtab;
1239 }
1240 if (has_statement_num)
1241 {
1242 // kernel.statement(NUM) or module("m").statement(NUM)
1243 // Technically, all we need is the module offset (or _stext, for
1244 // the kernel). But for that we need either the ELF file or (for
1245 // _stext) the symbol table. In either case, the symbol table
1246 // is available, and that allows us to map the NUM (address)
1247 // to a function, which is goodness.
1248 return dbr_need_symtab;
1249 }
1250 if (has_function_num)
1251 {
1252 // kernel.function(NUM) or module("m").function(NUM)
1253 // Need the symbol table so we can back up from NUM to the
1254 // start of the function.
1255 return dbr_need_symtab;
1256 }
1257 // Symbol table tells us nothing about source files or line numbers.
1258 return dbr_need_dwarf;
1259}
2930abc7
FCE
1260
1261
b8da0ad1
FCE
1262// The critical determining factor when interpreting a pattern
1263// string is, perhaps surprisingly: "presence of a lineno". The
1264// presence of a lineno changes the search strategy completely.
1265//
1266// Compare the two cases:
1267//
1268// 1. {statement,function}(foo@file.c:lineno)
1269// - find the files matching file.c
1270// - in each file, find the functions matching foo
1271// - query the file for line records matching lineno
1272// - iterate over the line records,
1273// - and iterate over the functions,
1274// - if(haspc(function.DIE, line.addr))
1275// - if looking for statements: probe(lineno.addr)
1276// - if looking for functions: probe(function.{entrypc,return,etc.})
1277//
1278// 2. {statement,function}(foo@file.c)
1279// - find the files matching file.c
1280// - in each file, find the functions matching foo
1281// - probe(function.{entrypc,return,etc.})
1282//
1283// Thus the first decision we make is based on the presence of a
1284// lineno, and we enter entirely different sets of callbacks
1285// depending on that decision.
1286//
1287// Note that the first case is a generalization fo the second, in that
1288// we could theoretically search through line records for matching
1289// file names (a "table scan" in rdbms lingo). Luckily, file names
1290// are already cached elsewhere, so we can do an "index scan" as an
1291// optimization.
7e1279ea 1292
bd2b1e68 1293static void
4cd232e4 1294query_statement (string const & func,
20e4a32c 1295 char const * file,
4cd232e4 1296 int line,
bcc12710 1297 Dwarf_Die *scope_die,
20e4a32c 1298 Dwarf_Addr stmt_addr,
4cd232e4 1299 dwarf_query * q)
bd2b1e68 1300{
39bcd429
FCE
1301 try
1302 {
cee35f73 1303 q->add_probe_point(func, file ? file : "",
a9b2f3a5 1304 line, scope_die, stmt_addr);
39bcd429
FCE
1305 }
1306 catch (const semantic_error& e)
1307 {
1308 q->sess.print_error (e);
1309 }
bd2b1e68
GH
1310}
1311
6b517475
JS
1312static void
1313query_addr(Dwarf_Addr addr, dwarf_query *q)
1314{
1315 dwflpp &dw = q->dw;
1316
08d1d520
MW
1317 if (q->sess.verbose > 2)
1318 clog << "query_addr 0x" << hex << addr << dec << endl;
6b517475
JS
1319
1320 // First pick which CU contains this address
1321 Dwarf_Die* cudie = dw.query_cu_containing_address(addr);
1322 if (!cudie) // address could be wildly out of range
1323 return;
1324 dw.focus_on_cu(cudie);
1325
1326 // Now compensate for the dw bias
1327 addr -= dw.module_bias;
1328
1329 // Per PR5787, we look up the scope die even for
1330 // statement_num's, for blacklist sensitivity and $var
1331 // resolution purposes.
1332
1333 // Find the scopes containing this address
1334 vector<Dwarf_Die> scopes = dw.getscopes(addr);
1335 if (scopes.empty())
1336 return;
1337
1338 // Look for the innermost containing function
1339 Dwarf_Die *fnscope = NULL;
1340 for (size_t i = 0; i < scopes.size(); ++i)
1341 {
1342 int tag = dwarf_tag(&scopes[i]);
1343 if ((tag == DW_TAG_subprogram && !q->has_inline) ||
1344 (tag == DW_TAG_inlined_subroutine &&
4bda987e 1345 !q->has_call && !q->has_return && !q->has_exported))
6b517475
JS
1346 {
1347 fnscope = &scopes[i];
1348 break;
1349 }
1350 }
1351 if (!fnscope)
1352 return;
1353 dw.focus_on_function(fnscope);
1354
1355 Dwarf_Die *scope = q->has_function_num ? fnscope : &scopes[0];
1356
1357 const char *file = dwarf_decl_file(fnscope);
1358 int line;
1359 dwarf_decl_line(fnscope, &line);
1360
1361 // Function probes should reset the addr to the function entry
1362 // and possibly perform prologue searching
1363 if (q->has_function_num)
1364 {
1365 dw.die_entrypc(fnscope, &addr);
1366 if (dwarf_tag(fnscope) == DW_TAG_subprogram &&
1367 (q->sess.prologue_searching || q->has_process)) // PR 6871
1368 {
1369 func_info func;
1370 func.die = *fnscope;
1371 func.name = dw.function_name;
1372 func.decl_file = file;
1373 func.decl_line = line;
1374 func.entrypc = addr;
1375
1376 func_info_map_t funcs(1, func);
1377 dw.resolve_prologue_endings (funcs);
464379bb
FCE
1378 if (q->has_return) // PR13200
1379 {
1380 if (q->sess.verbose > 2)
1381 clog << "ignoring prologue for .return probes" << endl;
1382 }
1383 else
1384 {
1385 if (funcs[0].prologue_end)
1386 addr = funcs[0].prologue_end;
1387 }
6b517475
JS
1388 }
1389 }
1390 else
1391 {
1392 dwarf_line_t address_line(dwarf_getsrc_die(cudie, addr));
1393 if (address_line)
1394 {
1395 file = address_line.linesrc();
1396 line = address_line.lineno();
1397 }
1398
1399 // Verify that a raw address matches the beginning of a
1400 // statement. This is a somewhat lame check that the address
1401 // is at the start of an assembly instruction. Mark probes are in the
1402 // middle of a macro and thus not strictly at a statement beginning.
1403 // Guru mode may override this check.
1404 if (!q->has_mark && (!address_line || address_line.addr() != addr))
1405 {
1406 stringstream msg;
2a97f50b 1407 msg << _F("address %#" PRIx64 " does not match the beginning of a statement",
b530b5b3 1408 addr);
6b517475 1409 if (address_line)
2a97f50b 1410 msg << _F(" (try %#" PRIx64 ")", address_line.addr());
6b517475 1411 else
b530b5b3
LB
1412 msg << _F(" (no line info found for '%s', in module '%s')",
1413 dw.cu_name().c_str(), dw.module_name.c_str());
6b517475
JS
1414 if (! q->sess.guru_mode)
1415 throw semantic_error(msg.str());
2713ea24 1416 else
6b517475
JS
1417 q->sess.print_warning(msg.str());
1418 }
1419 }
1420
1421 // Build a probe at this point
1422 query_statement(dw.function_name, file, line, scope, addr, q);
1423}
1424
8096dd7d
JS
1425static void
1426query_label (string const & func,
1427 char const * label,
1428 char const * file,
1429 int line,
1430 Dwarf_Die *scope_die,
1431 Dwarf_Addr stmt_addr,
1432 dwarf_query * q)
1433{
6b517475
JS
1434 assert (q->has_statement_str || q->has_function_str);
1435
8096dd7d
JS
1436 size_t i = q->results.size();
1437
1438 // weed out functions whose decl_file isn't one of
1439 // the source files that we actually care about
6b517475 1440 if (q->spec_type != function_alone &&
8096dd7d
JS
1441 q->filtered_srcfiles.count(file) == 0)
1442 return;
1443
1444 query_statement(func, file, line, scope_die, stmt_addr, q);
1445
c72aa911
JS
1446 // after the fact, insert the label back into the derivation chain
1447 probe_point::component* ppc =
1448 new probe_point::component(TOK_LABEL, new literal_string (label));
1449 for (; i < q->results.size(); ++i)
1450 {
1451 derived_probe* p = q->results[i];
1452 probe_point* pp = new probe_point(*p->locations[0]);
1453 pp->components.push_back (ppc);
1454 p->base = p->base->create_alias(p->locations[0], pp);
1455 }
8096dd7d
JS
1456}
1457
7e1279ea 1458static void
3e961ba6 1459query_inline_instance_info (inline_instance_info & ii,
7e1279ea
FCE
1460 dwarf_query * q)
1461{
b6581717 1462 try
7e1279ea 1463 {
8f14e444
FCE
1464 assert (! q->has_return); // checked by caller already
1465 if (q->sess.verbose>2)
b530b5b3
LB
1466 clog << _F("querying entrypc %#" PRIx64 " of instance of inline '%s'\n",
1467 ii.entrypc, ii.name.c_str());
8f14e444
FCE
1468 query_statement (ii.name, ii.decl_file, ii.decl_line,
1469 &ii.die, ii.entrypc, q);
7e1279ea 1470 }
b6581717 1471 catch (semantic_error &e)
7e1279ea 1472 {
b6581717 1473 q->sess.print_error (e);
7e1279ea
FCE
1474 }
1475}
1476
1477static void
1478query_func_info (Dwarf_Addr entrypc,
bcc12710 1479 func_info & fi,
7e1279ea
FCE
1480 dwarf_query * q)
1481{
b6581717 1482 try
7e1279ea 1483 {
b6581717
GH
1484 if (q->has_return)
1485 {
1486 // NB. dwarf_derived_probe::emit_registrations will emit a
1487 // kretprobe based on the entrypc in this case.
464379bb
FCE
1488 if (fi.prologue_end != 0 && q->has_return) // PR13200
1489 {
1490 if (q->sess.verbose > 2)
1491 clog << "ignoring prologue for .return probes" << endl;
1492 }
20e4a32c 1493 query_statement (fi.name, fi.decl_file, fi.decl_line,
b6581717
GH
1494 &fi.die, entrypc, q);
1495 }
1496 else
1497 {
35dc8b04 1498 if (fi.prologue_end != 0)
44f75386 1499 {
44f75386
FCE
1500 query_statement (fi.name, fi.decl_file, fi.decl_line,
1501 &fi.die, fi.prologue_end, q);
1502 }
1503 else
1504 {
1505 query_statement (fi.name, fi.decl_file, fi.decl_line,
1506 &fi.die, entrypc, q);
1507 }
b6581717 1508 }
7e1279ea 1509 }
b6581717 1510 catch (semantic_error &e)
7e1279ea 1511 {
b6581717 1512 q->sess.print_error (e);
7e1279ea
FCE
1513 }
1514}
1515
1516
bd4b874d
SC
1517static void
1518query_srcfile_label (const dwarf_line_t& line, void * arg)
1519{
1520 dwarf_query * q = static_cast<dwarf_query *>(arg);
1521
1522 Dwarf_Addr addr = line.addr();
1523
1524 for (func_info_map_t::iterator i = q->filtered_functions.begin();
1525 i != q->filtered_functions.end(); ++i)
1526 if (q->dw.die_has_pc (i->die, addr))
f09d0d1e
JS
1527 q->dw.iterate_over_labels (&i->die, q->label_val, i->name,
1528 q, query_label);
1529
1530 for (inline_instance_map_t::iterator i = q->filtered_inlines.begin();
1531 i != q->filtered_inlines.end(); ++i)
1532 if (q->dw.die_has_pc (i->die, addr))
1533 q->dw.iterate_over_labels (&i->die, q->label_val, i->name,
1534 q, query_label);
bd4b874d
SC
1535}
1536
7e1279ea 1537static void
86bf665e 1538query_srcfile_line (const dwarf_line_t& line, void * arg)
7e1279ea
FCE
1539{
1540 dwarf_query * q = static_cast<dwarf_query *>(arg);
1541
86bf665e 1542 Dwarf_Addr addr = line.addr();
4cd232e4 1543
86bf665e 1544 int lineno = line.lineno();
847bf07f 1545
86bf665e 1546 for (func_info_map_t::iterator i = q->filtered_functions.begin();
7e1279ea
FCE
1547 i != q->filtered_functions.end(); ++i)
1548 {
3e961ba6 1549 if (q->dw.die_has_pc (i->die, addr))
7e1279ea 1550 {
b0ee93c4 1551 if (q->sess.verbose>3)
b530b5b3 1552 clog << _("function DIE lands on srcfile\n");
4cd232e4 1553 if (q->has_statement_str)
f5958c8f
JS
1554 {
1555 Dwarf_Die scope;
1556 q->dw.inner_die_containing_pc(i->die, addr, scope);
1557 query_statement (i->name, i->decl_file,
1558 lineno, // NB: not q->line !
1559 &scope, addr, q);
1560 }
4cd232e4 1561 else
3e961ba6 1562 query_func_info (i->entrypc, *i, q);
7e1279ea 1563 }
20e4a32c
RM
1564 }
1565
86bf665e 1566 for (inline_instance_map_t::iterator i
897820ca
GH
1567 = q->filtered_inlines.begin();
1568 i != q->filtered_inlines.end(); ++i)
1569 {
3e961ba6 1570 if (q->dw.die_has_pc (i->die, addr))
7e1279ea 1571 {
b0ee93c4 1572 if (q->sess.verbose>3)
b530b5b3 1573 clog << _("inline instance DIE lands on srcfile\n");
897820ca 1574 if (q->has_statement_str)
f5958c8f
JS
1575 {
1576 Dwarf_Die scope;
1577 q->dw.inner_die_containing_pc(i->die, addr, scope);
1578 query_statement (i->name, i->decl_file,
1579 q->line[0], &scope, addr, q);
1580 }
897820ca 1581 else
3e961ba6 1582 query_inline_instance_info (*i, q);
897820ca 1583 }
20e4a32c 1584 }
7e1279ea
FCE
1585}
1586
1587
7fdd3e2c
JS
1588bool
1589inline_instance_info::operator<(const inline_instance_info& other) const
1590{
1591 if (entrypc != other.entrypc)
1592 return entrypc < other.entrypc;
1593
1594 if (decl_line != other.decl_line)
1595 return decl_line < other.decl_line;
1596
1597 int cmp = name.compare(other.name);
1598 if (!cmp)
1599 cmp = strcmp(decl_file, other.decl_file);
1600 return cmp < 0;
1601}
1602
1603
4fa7b22b 1604static int
7e1279ea 1605query_dwarf_inline_instance (Dwarf_Die * die, void * arg)
4fa7b22b
GH
1606{
1607 dwarf_query * q = static_cast<dwarf_query *>(arg);
6b517475 1608 assert (q->has_statement_str || q->has_function_str);
4bda987e 1609 assert (!q->has_call && !q->has_return && !q->has_exported);
bd2b1e68 1610
39bcd429 1611 try
7a053d3b 1612 {
b0ee93c4 1613 if (q->sess.verbose>2)
b530b5b3 1614 clog << _F("selected inline instance of %s\n", q->dw.function_name.c_str());
7e1279ea 1615
6b517475
JS
1616 Dwarf_Addr entrypc;
1617 if (q->dw.die_entrypc (die, &entrypc))
1618 {
1619 inline_instance_info inl;
1620 inl.die = *die;
1621 inl.name = q->dw.function_name;
1622 inl.entrypc = entrypc;
1623 q->dw.function_file (&inl.decl_file);
1624 q->dw.function_line (&inl.decl_line);
1625
1626 // make sure that this inline hasn't already
1627 // been matched from a different CU
1628 if (q->inline_dupes.insert(inl).second)
1629 q->filtered_inlines.push_back(inl);
1630 }
7e1279ea
FCE
1631 return DWARF_CB_OK;
1632 }
1633 catch (const semantic_error& e)
1634 {
1635 q->sess.print_error (e);
1636 return DWARF_CB_ABORT;
1637 }
1638}
bb788f9f 1639
7e1279ea 1640static int
2da9cedb 1641query_dwarf_func (Dwarf_Die * func, base_query * bq)
7e1279ea 1642{
2da9cedb 1643 dwarf_query * q = static_cast<dwarf_query *>(bq);
6b517475 1644 assert (q->has_statement_str || q->has_function_str);
bb788f9f 1645
bd25380d
JS
1646 // weed out functions whose decl_file isn't one of
1647 // the source files that we actually care about
6b517475 1648 if (q->spec_type != function_alone &&
bd25380d 1649 q->filtered_srcfiles.count(dwarf_decl_file(func)?:"") == 0)
8096dd7d 1650 return DWARF_CB_OK;
bd25380d 1651
7e1279ea
FCE
1652 try
1653 {
7e1279ea
FCE
1654 q->dw.focus_on_function (func);
1655
7d6d0afc
JS
1656 if (!q->dw.function_scope_matches(q->scopes))
1657 return DWARF_CB_OK;
1658
857bdfd1
JS
1659 // make sure that this function address hasn't
1660 // already been matched under an aliased name
1661 Dwarf_Addr addr;
1662 if (!q->dw.func_is_inline() &&
1663 dwarf_entrypc(func, &addr) == 0 &&
1664 !q->alias_dupes.insert(addr).second)
1665 return DWARF_CB_OK;
1666
4bda987e 1667 if (q->dw.func_is_inline () && (! q->has_call) && (! q->has_return) && (! q->has_exported))
7e1279ea 1668 {
4bda987e 1669 if (q->sess.verbose>3)
b530b5b3 1670 clog << _F("checking instances of inline %s\n", q->dw.function_name.c_str());
4bda987e 1671 q->dw.iterate_over_inline_instances (query_dwarf_inline_instance, q);
7e1279ea 1672 }
8f14e444
FCE
1673 else if (q->dw.func_is_inline () && (q->has_return)) // PR 11553
1674 {
1675 q->inlined_non_returnable.insert (q->dw.function_name);
1676 }
396afcee 1677 else if (!q->dw.func_is_inline () && (! q->has_inline))
20e4a32c 1678 {
4bda987e
SC
1679 if (q->has_exported && !q->dw.func_is_exported ())
1680 return DWARF_CB_OK;
6b517475 1681 if (q->sess.verbose>2)
b530b5b3 1682 clog << _F("selected function %s\n", q->dw.function_name.c_str());
6b517475
JS
1683
1684 func_info func;
1685 q->dw.function_die (&func.die);
1686 func.name = q->dw.function_name;
1687 q->dw.function_file (&func.decl_file);
1688 q->dw.function_line (&func.decl_line);
1689
1690 Dwarf_Addr entrypc;
1691 if (q->dw.function_entrypc (&entrypc))
1692 {
1693 func.entrypc = entrypc;
1694 q->filtered_functions.push_back (func);
1695 }
1696 /* else this function is fully inlined, just ignore it */
7e1279ea 1697 }
39bcd429 1698 return DWARF_CB_OK;
bd2b1e68 1699 }
39bcd429 1700 catch (const semantic_error& e)
bd2b1e68 1701 {
39bcd429
FCE
1702 q->sess.print_error (e);
1703 return DWARF_CB_ABORT;
bd2b1e68 1704 }
bd2b1e68
GH
1705}
1706
1707static int
1708query_cu (Dwarf_Die * cudie, void * arg)
1709{
20c6c071 1710 dwarf_query * q = static_cast<dwarf_query *>(arg);
6b517475
JS
1711 assert (q->has_statement_str || q->has_function_str);
1712
85007c04 1713 if (pending_interrupts) return DWARF_CB_ABORT;
7a053d3b 1714
39bcd429 1715 try
bd2b1e68 1716 {
7e1279ea 1717 q->dw.focus_on_cu (cudie);
b5d77020 1718
b0ee93c4 1719 if (false && q->sess.verbose>2)
b530b5b3
LB
1720 clog << _F("focused on CU '%s', in module '%s'\n",
1721 q->dw.cu_name().c_str(), q->dw.module_name.c_str());
d9b516ca 1722
6b517475
JS
1723 q->filtered_srcfiles.clear();
1724 q->filtered_functions.clear();
1725 q->filtered_inlines.clear();
1726
1727 // In this path, we find "abstract functions", record
1728 // information about them, and then (depending on lineno
1729 // matching) possibly emit one or more of the function's
1730 // associated addresses. Unfortunately the control of this
1731 // cannot easily be turned inside out.
1732
1733 if (q->spec_type != function_alone)
39bcd429 1734 {
6b517475
JS
1735 // If we have a pattern string with a filename, we need
1736 // to elaborate the srcfile mask in question first.
1737 q->dw.collect_srcfiles_matching (q->file, q->filtered_srcfiles);
1738
1739 // If we have a file pattern and *no* srcfile matches, there's
1740 // no need to look further into this CU, so skip.
1741 if (q->filtered_srcfiles.empty())
1742 return DWARF_CB_OK;
1743 }
e4c58386 1744
6b517475
JS
1745 // Pick up [entrypc, name, DIE] tuples for all the functions
1746 // matching the query, and fill in the prologue endings of them
1747 // all in a single pass.
5898b6e1 1748 int rc = q->dw.iterate_over_functions (query_dwarf_func, q, q->function);
6b517475
JS
1749 if (rc != DWARF_CB_OK)
1750 q->query_done = true;
1751
1752 if ((q->sess.prologue_searching || q->has_process) // PR 6871
1753 && !q->has_statement_str) // PR 2608
1754 if (! q->filtered_functions.empty())
1755 q->dw.resolve_prologue_endings (q->filtered_functions);
464379bb
FCE
1756 // NB: we could skip the resolve_prologue_endings() call here for has_return case (PR13200),
1757 // but don't have to. We can resolve the prologue, just not actually use it in query_addr().
6b517475
JS
1758
1759 if (q->spec_type == function_file_and_line)
1760 {
58b070fb 1761 // .statement(...:NN) often gets mixed up with .function(...:NN)
2713ea24 1762 if (q->has_function_str)
b530b5b3
LB
1763 q->sess.print_warning (_("For probing a particular line, use a "
1764 ".statement() probe, not .function()"),
af2e341f 1765 q->base_probe->tok);
58b070fb 1766
6b517475
JS
1767 // If we have a pattern string with target *line*, we
1768 // have to look at lines in all the matched srcfiles.
1769 void (* callback) (const dwarf_line_t&, void*) =
1770 q->has_label ? query_srcfile_label : query_srcfile_line;
1771 for (set<string>::const_iterator i = q->filtered_srcfiles.begin();
1772 i != q->filtered_srcfiles.end(); ++i)
1773 q->dw.iterate_over_srcfile_lines (i->c_str(), q->line, q->has_statement_str,
1774 q->line_type, callback, q->function, q);
1775 }
1776 else if (q->has_label)
1777 {
1778 for (func_info_map_t::iterator i = q->filtered_functions.begin();
1779 i != q->filtered_functions.end(); ++i)
1780 q->dw.iterate_over_labels (&i->die, q->label_val, i->name,
1781 q, query_label);
1782
1783 for (inline_instance_map_t::iterator i = q->filtered_inlines.begin();
1784 i != q->filtered_inlines.end(); ++i)
1785 q->dw.iterate_over_labels (&i->die, q->label_val, i->name,
1786 q, query_label);
39bcd429 1787 }
6b517475
JS
1788 else
1789 {
1790 // Otherwise, simply probe all resolved functions.
1791 for (func_info_map_t::iterator i = q->filtered_functions.begin();
1792 i != q->filtered_functions.end(); ++i)
1793 query_func_info (i->entrypc, *i, q);
1794
1795 // And all inline instances (if we're not excluding inlines with ".call")
1796 if (! q->has_call)
1797 for (inline_instance_map_t::iterator i
1798 = q->filtered_inlines.begin(); i != q->filtered_inlines.end(); ++i)
1799 query_inline_instance_info (*i, q);
1800 }
39bcd429 1801 return DWARF_CB_OK;
bd2b1e68 1802 }
39bcd429 1803 catch (const semantic_error& e)
bd2b1e68 1804 {
39bcd429
FCE
1805 q->sess.print_error (e);
1806 return DWARF_CB_ABORT;
bd2b1e68 1807 }
bd2b1e68
GH
1808}
1809
0ce64fb8 1810
4df79aaf
JS
1811void
1812dwarf_query::query_module_functions ()
1813{
1814 try
1815 {
1816 filtered_srcfiles.clear();
1817 filtered_functions.clear();
1818 filtered_inlines.clear();
1819
1820 // Collect all module functions so we know which CUs are interesting
1821 int rc = dw.iterate_single_function(query_dwarf_func, this, function);
1822 if (rc != DWARF_CB_OK)
1823 {
1824 query_done = true;
1825 return;
1826 }
1827
1828 set<void*> used_cus; // by cu->addr
1829 vector<Dwarf_Die> cus;
1830 Dwarf_Die cu_mem;
1831
1832 for (func_info_map_t::iterator i = filtered_functions.begin();
1833 i != filtered_functions.end(); ++i)
1834 if (dwarf_diecu(&i->die, &cu_mem, NULL, NULL) &&
1835 used_cus.insert(cu_mem.addr).second)
1836 cus.push_back(cu_mem);
1837
1838 for (inline_instance_map_t::iterator i = filtered_inlines.begin();
1839 i != filtered_inlines.end(); ++i)
1840 if (dwarf_diecu(&i->die, &cu_mem, NULL, NULL) &&
1841 used_cus.insert(cu_mem.addr).second)
1842 cus.push_back(cu_mem);
1843
1844 // Reset the dupes since we didn't actually collect them the first time
1845 alias_dupes.clear();
1846 inline_dupes.clear();
1847
1848 // Run the query again on the individual CUs
1849 for (vector<Dwarf_Die>::iterator i = cus.begin(); i != cus.end(); ++i)
1850 query_cu(&*i, this);
1851 }
1852 catch (const semantic_error& e)
1853 {
1854 sess.print_error (e);
1855 }
1856}
1857
1858
5f0a03a6
JK
1859static void
1860validate_module_elf (Dwfl_Module *mod, const char *name, base_query *q)
1861{
1862 // Validate the machine code in this elf file against the
1863 // session machine. This is important, in case the wrong kind
1864 // of debuginfo is being automagically processed by elfutils.
1865 // While we can tell i686 apart from x86-64, unfortunately
1866 // we can't help confusing i586 vs i686 (both EM_386).
1867
1868 Dwarf_Addr bias;
1869 // We prefer dwfl_module_getdwarf to dwfl_module_getelf here,
1870 // because dwfl_module_getelf can force costly section relocations
1871 // we don't really need, while either will do for this purpose.
1872 Elf* elf = (dwarf_getelf (dwfl_module_getdwarf (mod, &bias))
1873 ?: dwfl_module_getelf (mod, &bias));
1874
1875 GElf_Ehdr ehdr_mem;
1876 GElf_Ehdr* em = gelf_getehdr (elf, &ehdr_mem);
86bf665e 1877 if (em == 0) { dwfl_assert ("dwfl_getehdr", dwfl_errno()); }
5f0a03a6
JK
1878 int elf_machine = em->e_machine;
1879 const char* debug_filename = "";
1880 const char* main_filename = "";
1881 (void) dwfl_module_info (mod, NULL, NULL,
1882 NULL, NULL, NULL,
1883 & main_filename,
1884 & debug_filename);
1885 const string& sess_machine = q->sess.architecture;
756c9462
FCE
1886
1887 string expect_machine; // to match sess.machine (i.e., kernel machine)
1888 string expect_machine2;
5f0a03a6 1889
d27e6fd5 1890 // NB: See also the 'uname -m' squashing done in main.cxx.
5f0a03a6
JK
1891 switch (elf_machine)
1892 {
756c9462
FCE
1893 // x86 and ppc are bi-architecture; a 64-bit kernel
1894 // can normally run either 32-bit or 64-bit *userspace*.
1895 case EM_386:
1896 expect_machine = "i?86";
1897 if (! q->has_process) break; // 32-bit kernel/module
1898 /* FALLSTHROUGH */
1899 case EM_X86_64:
1900 expect_machine2 = "x86_64";
1901 break;
1902 case EM_PPC:
756c9462 1903 case EM_PPC64:
5a1c472e 1904 expect_machine = "powerpc";
756c9462 1905 break;
3fe7d888 1906 case EM_S390: expect_machine = "s390"; break;
5f0a03a6 1907 case EM_IA_64: expect_machine = "ia64"; break;
d27e6fd5 1908 case EM_ARM: expect_machine = "arm*"; break;
5f0a03a6
JK
1909 // XXX: fill in some more of these
1910 default: expect_machine = "?"; break;
1911 }
1912
1913 if (! debug_filename) debug_filename = main_filename;
1914 if (! debug_filename) debug_filename = name;
1915
756c9462
FCE
1916 if (fnmatch (expect_machine.c_str(), sess_machine.c_str(), 0) != 0 &&
1917 fnmatch (expect_machine2.c_str(), sess_machine.c_str(), 0) != 0)
5f0a03a6
JK
1918 {
1919 stringstream msg;
b530b5b3
LB
1920 msg << _F("ELF machine %s|%s (code %d) mismatch with target %s in '%s'",
1921 expect_machine.c_str(), expect_machine2.c_str(), elf_machine,
1922 sess_machine.c_str(), debug_filename);
5f0a03a6
JK
1923 throw semantic_error(msg.str ());
1924 }
1925
1926 if (q->sess.verbose>2)
2a97f50b 1927 clog << _F("focused on module '%s' = [%#" PRIx64 "-%#" PRIx64 ", bias %#" PRIx64
b530b5b3
LB
1928 " file %s ELF machine %s|%s (code %d)\n",
1929 q->dw.module_name.c_str(), q->dw.module_start, q->dw.module_end,
1930 q->dw.module_bias, debug_filename, expect_machine.c_str(),
1931 expect_machine2.c_str(), elf_machine);
5f0a03a6 1932}
1d3a40b6 1933
91af0778
FCE
1934
1935
1936static Dwarf_Addr
1937lookup_symbol_address (Dwfl_Module *m, const char* wanted)
1938{
1939 int syments = dwfl_module_getsymtab(m);
1940 assert(syments);
1941 for (int i = 1; i < syments; ++i)
1942 {
1943 GElf_Sym sym;
1944 const char *name = dwfl_module_getsym(m, i, &sym, NULL);
1945 if (name != NULL && strcmp(name, wanted) == 0)
1946 return sym.st_value;
1947 }
1948
1949 return 0;
1950}
1951
1952
1953
bd2b1e68 1954static int
b8da0ad1 1955query_module (Dwfl_Module *mod,
91af0778 1956 void **,
b8da0ad1 1957 const char *name,
6f4c1275 1958 Dwarf_Addr addr,
b8da0ad1 1959 void *arg)
bd2b1e68 1960{
91af0778 1961 base_query *q = static_cast<base_query *>(arg);
bd2b1e68 1962
39bcd429 1963 try
e38d6504 1964 {
91af0778
FCE
1965 module_info* mi = q->sess.module_cache->cache[name];
1966 if (mi == 0)
1967 {
1968 mi = q->sess.module_cache->cache[name] = new module_info(name);
1969
6f4c1275
FCE
1970 mi->mod = mod;
1971 mi->addr = addr;
91af0778 1972
6f4c1275
FCE
1973 const char* debug_filename = "";
1974 const char* main_filename = "";
1975 (void) dwfl_module_info (mod, NULL, NULL,
1976 NULL, NULL, NULL,
1977 & main_filename,
1978 & debug_filename);
1979
1980 if (q->sess.ignore_vmlinux && name == TOK_KERNEL)
91af0778
FCE
1981 {
1982 // report_kernel() in elfutils found vmlinux, but pretend it didn't.
1983 // Given a non-null path, returning 1 means keep reporting modules.
1984 mi->dwarf_status = info_absent;
1985 }
6f4c1275 1986 else if (debug_filename || main_filename)
91af0778 1987 {
6f4c1275
FCE
1988 mi->elf_path = debug_filename ?: main_filename;
1989 }
1990 else if (name == TOK_KERNEL)
1991 {
1992 mi->dwarf_status = info_absent;
91af0778 1993 }
91af0778
FCE
1994 }
1995 // OK, enough of that module_info caching business.
1996
5f0a03a6 1997 q->dw.focus_on_module(mod, mi);
d9b516ca 1998
39bcd429
FCE
1999 // If we have enough information in the pattern to skip a module and
2000 // the module does not match that information, return early.
b8da0ad1 2001 if (!q->dw.module_name_matches(q->module_val))
85007c04 2002 return pending_interrupts ? DWARF_CB_ABORT : DWARF_CB_OK;
0cbbf9d1
FCE
2003
2004 // Don't allow module("*kernel*") type expressions to match the
2005 // elfutils module "kernel", which we refer to in the probe
2006 // point syntax exclusively as "kernel.*".
2007 if (q->dw.module_name == TOK_KERNEL && ! q->has_kernel)
85007c04 2008 return pending_interrupts ? DWARF_CB_ABORT : DWARF_CB_OK;
b5d77020 2009
5f0a03a6
JK
2010 if (mod)
2011 validate_module_elf(mod, name, q);
2012 else
91af0778
FCE
2013 assert(q->has_kernel); // and no vmlinux to examine
2014
2015 if (q->sess.verbose>2)
b530b5b3 2016 cerr << _F("focused on module '%s'\n", q->dw.module_name.c_str());
91af0778
FCE
2017
2018
2019 // Collect a few kernel addresses. XXX: these belong better
2020 // to the sess.module_info["kernel"] struct.
2021 if (q->dw.module_name == TOK_KERNEL)
c931ec8a 2022 {
91af0778
FCE
2023 if (! q->sess.sym_kprobes_text_start)
2024 q->sess.sym_kprobes_text_start = lookup_symbol_address (mod, "__kprobes_text_start");
2025 if (! q->sess.sym_kprobes_text_end)
2026 q->sess.sym_kprobes_text_end = lookup_symbol_address (mod, "__kprobes_text_end");
2027 if (! q->sess.sym_stext)
2028 q->sess.sym_stext = lookup_symbol_address (mod, "_stext");
c931ec8a
FCE
2029 }
2030
47e226ed
SC
2031 // We either have a wildcard or an unresolved library
2032 if (q->has_library && (contains_glob_chars (q->path)
2033 || q->path.find('/') == string::npos))
84c84ac4
SC
2034 // handle .library(GLOB)
2035 q->dw.iterate_over_libraries (&q->query_library_callback, q);
576eaefe
SC
2036 // .plt is translated to .plt.statement(N). We only want to iterate for the
2037 // .plt case
2038 else if (q->has_plt && ! q->has_statement)
2039 q->dw.iterate_over_plt (q, &q->query_plt_callback);
84c84ac4
SC
2040 else
2041 // search the module for matches of the probe point.
2042 q->handle_query_module();
bb788f9f 2043
b8da0ad1 2044 // If we know that there will be no more matches, abort early.
85007c04 2045 if (q->dw.module_name_final_match(q->module_val) || pending_interrupts)
b8da0ad1
FCE
2046 return DWARF_CB_ABORT;
2047 else
2048 return DWARF_CB_OK;
7a053d3b 2049 }
39bcd429 2050 catch (const semantic_error& e)
bd2b1e68 2051 {
39bcd429
FCE
2052 q->sess.print_error (e);
2053 return DWARF_CB_ABORT;
bd2b1e68 2054 }
bd2b1e68
GH
2055}
2056
35d4ab18 2057
84c84ac4 2058void
5d5bd369 2059base_query::query_library_callback (void *q, const char *data)
84c84ac4
SC
2060{
2061 base_query *me = (base_query*)q;
5d5bd369 2062 me->query_library (data);
84c84ac4
SC
2063}
2064
2065
2066void
51d6bda3
SC
2067query_one_library (const char *library, dwflpp & dw,
2068 const string user_lib, probe * base_probe, probe_point *base_loc,
2069 vector<derived_probe *> & results)
84c84ac4 2070{
47e226ed 2071 if (dw.function_name_matches_pattern(library, "*" + user_lib))
84c84ac4 2072 {
05fb3e0c
WF
2073 string library_path = find_executable (library, "", dw.sess.sysenv,
2074 "LD_LIBRARY_PATH");
84c84ac4
SC
2075 probe_point* specific_loc = new probe_point(*base_loc);
2076 specific_loc->optional = true;
2077 vector<probe_point::component*> derived_comps;
2078
2079 vector<probe_point::component*>::iterator it;
2080 for (it = specific_loc->components.begin();
2081 it != specific_loc->components.end(); ++it)
2082 if ((*it)->functor == TOK_LIBRARY)
2083 derived_comps.push_back(new probe_point::component(TOK_LIBRARY,
2084 new literal_string(library_path)));
2085 else
2086 derived_comps.push_back(*it);
2087 probe_point* derived_loc = new probe_point(*specific_loc);
2088 derived_loc->components = derived_comps;
2089 probe *new_base = base_probe->create_alias(derived_loc, specific_loc);
51d6bda3
SC
2090 derive_probes(dw.sess, new_base, results);
2091 if (dw.sess.verbose > 2)
84c84ac4
SC
2092 clog << _("module=") << library_path;
2093 }
2094}
2095
2096
51d6bda3
SC
2097void
2098dwarf_query::query_library (const char *library)
2099{
2100 query_one_library (library, dw, user_lib, base_probe, base_loc, results);
2101}
2102
576eaefe
SC
2103struct plt_expanding_visitor: public var_expanding_visitor
2104{
2105 plt_expanding_visitor(const string & entry):
2106 entry (entry)
2107 {
2108 }
2109 const string & entry;
2110
2111 void visit_target_symbol (target_symbol* e);
2112};
2113
2114
2115void
2116base_query::query_plt_callback (void *q, const char *entry, size_t address)
2117{
2118 base_query *me = (base_query*)q;
2119 if (me->dw.function_name_matches_pattern (entry, me->plt_val))
2120 me->query_plt (entry, address);
2121}
2122
2123
2124void
2125query_one_plt (const char *entry, long addr, dwflpp & dw,
2126 probe * base_probe, probe_point *base_loc,
2127 vector<derived_probe *> & results)
2128{
2129 probe_point* specific_loc = new probe_point(*base_loc);
2130 specific_loc->optional = true;
2131 vector<probe_point::component*> derived_comps;
2132
2133 if (dw.sess.verbose > 2)
2134 clog << _F("plt entry=%s\n", entry);
2135
2136 // query_module_symtab requires .plt to recognize that it can set the probe at
2137 // a plt entry so we convert process.plt to process.plt.statement
2138 vector<probe_point::component*>::iterator it;
2139 for (it = specific_loc->components.begin();
2140 it != specific_loc->components.end(); ++it)
2141 if ((*it)->functor == TOK_PLT)
3d372d6b
SC
2142 {
2143 derived_comps.push_back(*it);
2144 derived_comps.push_back(new probe_point::component(TOK_STATEMENT,
2145 new literal_number(addr)));
2146 }
576eaefe
SC
2147 else
2148 derived_comps.push_back(*it);
2149 probe_point* derived_loc = new probe_point(*specific_loc);
2150 derived_loc->components = derived_comps;
2151 probe *new_base = base_probe->create_alias(derived_loc, specific_loc);
2152 string e = string(entry);
2153 plt_expanding_visitor pltv (e);
2154 pltv.replace (new_base->body);
2155 derive_probes(dw.sess, new_base, results);
2156}
2157
2158
2159void
2160dwarf_query::query_plt (const char *entry, size_t address)
2161{
2162 query_one_plt (entry, address, dw, base_probe, base_loc, results);
2163}
51d6bda3 2164
435f53a7
FCE
2165// This would more naturally fit into elaborate.cxx:semantic_pass_symbols,
2166// but the needed declaration for module_cache is not available there.
2167// Nor for that matter in session.cxx. Only in this CU is that field ever
2168// set (in query_module() above), so we clean it up here too.
2169static void
2170delete_session_module_cache (systemtap_session& s)
2171{
2172 if (s.module_cache) {
2173 if (s.verbose > 3)
b530b5b3 2174 clog << _("deleting module_cache") << endl;
435f53a7
FCE
2175 delete s.module_cache;
2176 s.module_cache = 0;
2177 }
2178}
2179
2180
de688825 2181struct dwarf_var_expanding_visitor: public var_expanding_visitor
35d4ab18 2182{
77de5e9e 2183 dwarf_query & q;
bcc12710 2184 Dwarf_Die *scope_die;
77de5e9e 2185 Dwarf_Addr addr;
8c819921 2186 block *add_block;
2260f4e3 2187 block *add_call_probe; // synthesized from .return probes with saved $vars
8cc799a5 2188 bool add_block_tid, add_call_probe_tid;
af234c40
JS
2189 unsigned saved_longs, saved_strings; // data saved within kretprobes
2190 map<std::string, expression *> return_ts_map;
729455a7 2191 vector<Dwarf_Die> scopes;
b95e2b79 2192 bool visited;
77de5e9e 2193
de688825 2194 dwarf_var_expanding_visitor(dwarf_query & q, Dwarf_Die *sd, Dwarf_Addr a):
af234c40 2195 q(q), scope_die(sd), addr(a), add_block(NULL), add_call_probe(NULL),
8cc799a5 2196 add_block_tid(false), add_call_probe_tid(false),
af234c40 2197 saved_longs(0), saved_strings(0), visited(false) {}
277c21bc 2198 expression* gen_mapped_saved_return(expression* e, const string& name);
140be17a 2199 expression* gen_kretprobe_saved_return(expression* e);
a7999c82
JS
2200 void visit_target_symbol_saved_return (target_symbol* e);
2201 void visit_target_symbol_context (target_symbol* e);
d7f3e0c5 2202 void visit_target_symbol (target_symbol* e);
c24447be 2203 void visit_cast_op (cast_op* e);
8cc799a5 2204 void visit_entry_op (entry_op* e);
729455a7 2205private:
bfa7e523 2206 vector<Dwarf_Die>& getcuscope(target_symbol *e);
729455a7 2207 vector<Dwarf_Die>& getscopes(target_symbol *e);
77de5e9e
GH
2208};
2209
2210
de688825 2211unsigned var_expanding_visitor::tick = 0;
77de5e9e 2212
a50de939 2213
74fe61bc 2214var_expanding_visitor::var_expanding_visitor (): op()
a50de939
DS
2215{
2216 // FIXME: for the time being, by default we only support plain '$foo
2217 // = bar', not '+=' or any other op= variant. This is fixable, but a
2218 // bit ugly.
2219 //
2220 // If derived classes desire to add additional operator support, add
2221 // new operators to this list in the derived class constructor.
2222 valid_ops.insert ("=");
2223}
2224
2225
87214add
JS
2226bool
2227var_expanding_visitor::rewrite_lvalue(const token* tok, const std::string& eop,
2228 expression*& lvalue, expression*& rvalue)
77de5e9e 2229{
e57b735a
GH
2230 // Our job would normally be to require() the left and right sides
2231 // into a new assignment. What we're doing is slightly trickier:
2232 // we're pushing a functioncall** onto a stack, and if our left
2233 // child sets the functioncall* for that value, we're going to
2234 // assume our left child was a target symbol -- transformed into a
2235 // set_target_foo(value) call, and it wants to take our right child
2236 // as the argument "value".
2237 //
2238 // This is why some people claim that languages with
2239 // constructor-decomposing case expressions have a leg up on
2240 // visitors.
2241
2242 functioncall *fcall = NULL;
d9b516ca 2243
a50de939 2244 // Let visit_target_symbol know what operator it should handle.
87214add
JS
2245 const string* old_op = op;
2246 op = &eop;
a50de939 2247
e57b735a 2248 target_symbol_setter_functioncalls.push (&fcall);
87214add 2249 replace (lvalue);
e57b735a 2250 target_symbol_setter_functioncalls.pop ();
87214add
JS
2251 replace (rvalue);
2252
2253 op = old_op;
e57b735a
GH
2254
2255 if (fcall != NULL)
77de5e9e 2256 {
e57b735a
GH
2257 // Our left child is informing us that it was a target variable
2258 // and it has been replaced with a set_target_foo() function
2259 // call; we are going to provide that function call -- with the
2260 // right child spliced in as sole argument -- in place of
de688825 2261 // ourselves, in the var expansion we're in the middle of making.
e57b735a 2262
87214add 2263 if (valid_ops.find (eop) == valid_ops.end ())
a50de939
DS
2264 {
2265 // Build up a list of supported operators.
2266 string ops;
2267 std::set<string>::iterator i;
b530b5b3 2268 int valid_ops_size = 0;
a50de939 2269 for (i = valid_ops.begin(); i != valid_ops.end(); i++)
b530b5b3 2270 {
a50de939 2271 ops += " " + *i + ",";
b530b5b3
LB
2272 valid_ops_size++;
2273 }
a50de939
DS
2274 ops.resize(ops.size() - 1); // chop off the last ','
2275
2276 // Throw the error.
1e41115c
LB
2277 throw semantic_error (_F(ngettext("Only the following assign operator is implemented on target variables: %s",
2278 "Only the following assign operators are implemented on target variables: %s",
b530b5b3
LB
2279 valid_ops_size), ops.c_str()), tok);
2280
a50de939 2281 }
e57b735a 2282
87214add
JS
2283 assert (lvalue == fcall);
2284 if (rvalue)
2285 fcall->args.push_back (rvalue);
4ed05b15 2286 provide (fcall);
87214add 2287 return true;
77de5e9e 2288 }
e57b735a 2289 else
87214add
JS
2290 return false;
2291}
2292
2293
2294void
2295var_expanding_visitor::visit_assignment (assignment* e)
2296{
2297 if (!rewrite_lvalue (e->tok, e->op, e->left, e->right))
2298 provide (e);
2299}
2300
2301
2302void
2303var_expanding_visitor::visit_pre_crement (pre_crement* e)
2304{
2305 expression *dummy = NULL;
2306 if (!rewrite_lvalue (e->tok, e->op, e->operand, dummy))
2307 provide (e);
2308}
2309
2310
2311void
2312var_expanding_visitor::visit_post_crement (post_crement* e)
2313{
2314 expression *dummy = NULL;
2315 if (!rewrite_lvalue (e->tok, e->op, e->operand, dummy))
2316 provide (e);
2317}
2318
2319
2320void
2321var_expanding_visitor::visit_delete_statement (delete_statement* s)
2322{
2323 string fakeop = "delete";
2324 expression *dummy = NULL;
2325 if (!rewrite_lvalue (s->tok, fakeop, s->value, dummy))
2326 provide (s);
e57b735a 2327}
d9b516ca 2328
d7f3e0c5 2329
30263a73
FCE
2330void
2331var_expanding_visitor::visit_defined_op (defined_op* e)
2332{
2333 bool resolved = true;
2334
2335 defined_ops.push (e);
2336 try {
2337 // NB: provide<>/require<> are NOT typesafe. So even though a defined_op is
2338 // defined with a target_symbol* operand, a subsidiary call may attempt to
2339 // rewrite it to a general expression* instead, and require<> happily
2340 // casts to/from void*, causing possible memory corruption. We use
2341 // expression* here, being the general case of rewritten $variable.
2342 expression *foo1 = e->operand;
2343 foo1 = require (foo1);
2344
c69a87e0 2345 // NB: Formerly, we had some curious cases to consider here, depending on what
30263a73 2346 // various visit_target_symbol() implementations do for successful or
c69a87e0
FCE
2347 // erroneous resolutions. Some would signal a visit_target_symbol failure
2348 // with an exception, with a set flag within the target_symbol, or nothing
2349 // at all.
30263a73 2350 //
c69a87e0
FCE
2351 // Now, failures always have to be signalled with a
2352 // saved_conversion_error being chained to the target_symbol.
2353 // Successes have to result in an attempted rewrite of the
850bfddd 2354 // target_symbol (via provide()).
780f11ff 2355 //
c69a87e0
FCE
2356 // Edna Mode: "no capes". fche: "no exceptions".
2357
30263a73
FCE
2358 // dwarf stuff: success: rewrites to a function; failure: retains target_symbol, sets saved_conversion_error
2359 //
2360 // sdt-kprobes sdt.h: success: string or functioncall; failure: semantic_error
2361 //
2362 // sdt-uprobes: success: string or no op; failure: no op; expect derived/synthetic
2363 // dwarf probe to take care of it.
2364 // But this is rather unhelpful. So we rig the sdt_var_expanding_visitor
2365 // to pass through @defined() to the synthetic dwarf probe.
780f11ff 2366 //
30263a73
FCE
2367 // utrace: success: rewrites to function; failure: semantic_error
2368 //
850bfddd 2369 // procfs: success: rewrites to function; failure: semantic_error
30263a73
FCE
2370
2371 target_symbol* foo2 = dynamic_cast<target_symbol*> (foo1);
c69a87e0 2372 if (foo2 && foo2->saved_conversion_error) // failing
30263a73 2373 resolved = false;
a45664f4 2374 else if (foo2) // unresolved but not marked failing
b7aedf26 2375 {
780f11ff
JS
2376 // There are some visitors that won't touch certain target_symbols,
2377 // e.g. dwarf_var_expanding_visitor won't resolve @cast. We should
2378 // leave it for now so some other visitor can have a chance.
b7aedf26
JS
2379 e->operand = foo2;
2380 provide (e);
2381 return;
2382 }
30263a73
FCE
2383 else // resolved, rewritten to some other expression type
2384 resolved = true;
780f11ff 2385 } catch (const semantic_error& e) {
c69a87e0 2386 assert (0); // should not happen
30263a73
FCE
2387 }
2388 defined_ops.pop ();
2389
2390 literal_number* ln = new literal_number (resolved ? 1 : 0);
2391 ln->tok = e->tok;
2392 provide (ln);
2393}
2394
2395
5f36109e
JS
2396struct dwarf_pretty_print
2397{
2398 dwarf_pretty_print (dwflpp& dw, vector<Dwarf_Die>& scopes, Dwarf_Addr pc,
2399 const string& local, bool userspace_p,
2400 const target_symbol& e):
d19a9a82
JS
2401 dw(dw), local(local), scopes(scopes), pc(pc), pointer(NULL),
2402 userspace_p(userspace_p), deref_p(true)
5f36109e
JS
2403 {
2404 init_ts (e);
2405 dw.type_die_for_local (scopes, pc, local, ts, &base_type);
2406 }
2407
2408 dwarf_pretty_print (dwflpp& dw, Dwarf_Die *scope_die, Dwarf_Addr pc,
2409 bool userspace_p, const target_symbol& e):
d19a9a82
JS
2410 dw(dw), scopes(1, *scope_die), pc(pc), pointer(NULL),
2411 userspace_p(userspace_p), deref_p(true)
5f36109e
JS
2412 {
2413 init_ts (e);
2414 dw.type_die_for_return (&scopes[0], pc, ts, &base_type);
2415 }
2416
2417 dwarf_pretty_print (dwflpp& dw, Dwarf_Die *type_die, expression* pointer,
d19a9a82 2418 bool deref_p, bool userspace_p, const target_symbol& e):
5f36109e 2419 dw(dw), pc(0), pointer(pointer), pointer_type(*type_die),
d19a9a82 2420 userspace_p(userspace_p), deref_p(deref_p)
5f36109e
JS
2421 {
2422 init_ts (e);
2423 dw.type_die_for_pointer (type_die, ts, &base_type);
2424 }
2425
2426 functioncall* expand ();
ce83ff57 2427 ~dwarf_pretty_print () { delete ts; }
5f36109e
JS
2428
2429private:
2430 dwflpp& dw;
2431 target_symbol* ts;
7d11d8c9 2432 bool print_full;
5f36109e
JS
2433 Dwarf_Die base_type;
2434
2435 string local;
2436 vector<Dwarf_Die> scopes;
2437 Dwarf_Addr pc;
2438
2439 expression* pointer;
2440 Dwarf_Die pointer_type;
2441
d19a9a82 2442 const bool userspace_p, deref_p;
5f36109e
JS
2443
2444 void recurse (Dwarf_Die* type, target_symbol* e,
7d11d8c9 2445 print_format* pf, bool top=false);
5f36109e 2446 void recurse_base (Dwarf_Die* type, target_symbol* e,
7d11d8c9 2447 print_format* pf);
5f36109e 2448 void recurse_array (Dwarf_Die* type, target_symbol* e,
7d11d8c9 2449 print_format* pf, bool top);
5f36109e 2450 void recurse_pointer (Dwarf_Die* type, target_symbol* e,
7d11d8c9 2451 print_format* pf, bool top);
5f36109e 2452 void recurse_struct (Dwarf_Die* type, target_symbol* e,
7d11d8c9 2453 print_format* pf, bool top);
5f36109e 2454 void recurse_struct_members (Dwarf_Die* type, target_symbol* e,
7d11d8c9 2455 print_format* pf, int& count);
bbee5bb8 2456 bool print_chars (Dwarf_Die* type, target_symbol* e, print_format* pf);
5f36109e
JS
2457
2458 void init_ts (const target_symbol& e);
2459 expression* deref (target_symbol* e);
c55ea10d 2460 bool push_deref (print_format* pf, const string& fmt, target_symbol* e);
5f36109e
JS
2461};
2462
2463
2464void
2465dwarf_pretty_print::init_ts (const target_symbol& e)
2466{
2467 // Work with a new target_symbol so we can modify arguments
2468 ts = new target_symbol (e);
2469
2470 if (ts->addressof)
b530b5b3 2471 throw semantic_error(_("cannot take address of pretty-printed variable"), ts->tok);
5f36109e
JS
2472
2473 if (ts->components.empty() ||
2474 ts->components.back().type != target_symbol::comp_pretty_print)
b530b5b3 2475 throw semantic_error(_("invalid target_symbol for pretty-print"), ts->tok);
7d11d8c9 2476 print_full = ts->components.back().member.length() > 1;
5f36109e
JS
2477 ts->components.pop_back();
2478}
2479
2480
2481functioncall*
2482dwarf_pretty_print::expand ()
2483{
2484 static unsigned tick = 0;
2485
2486 // function pretty_print_X([pointer], [arg1, arg2, ...]) {
7d11d8c9
JS
2487 // try {
2488 // return sprintf("{.foo=...}", (ts)->foo, ...)
2489 // } catch {
2490 // return "ERROR"
2491 // }
5f36109e
JS
2492 // }
2493
2494 // Create the function decl and call.
2495
2496 functiondecl *fdecl = new functiondecl;
2497 fdecl->tok = ts->tok;
2498 fdecl->synthetic = true;
2499 fdecl->name = "_dwarf_pretty_print_" + lex_cast(tick++);
2500 fdecl->type = pe_string;
2501
2502 functioncall* fcall = new functioncall;
2503 fcall->tok = ts->tok;
2504 fcall->function = fdecl->name;
140be17a 2505 fcall->type = pe_string;
5f36109e
JS
2506
2507 // If there's a <pointer>, replace it with a new var and make that
2508 // the first function argument.
2509 if (pointer)
2510 {
2511 vardecl *v = new vardecl;
2512 v->type = pe_long;
2513 v->name = "pointer";
2514 v->tok = ts->tok;
2515 fdecl->formal_args.push_back (v);
2516 fcall->args.push_back (pointer);
2517
2518 symbol* sym = new symbol;
2519 sym->tok = ts->tok;
2520 sym->name = v->name;
5f36109e
JS
2521 pointer = sym;
2522 }
2523
2524 // For each expression argument, replace it with a function argument.
2525 for (unsigned i = 0; i < ts->components.size(); ++i)
2526 if (ts->components[i].type == target_symbol::comp_expression_array_index)
2527 {
2528 vardecl *v = new vardecl;
2529 v->type = pe_long;
2530 v->name = "index" + lex_cast(i);
2531 v->tok = ts->tok;
2532 fdecl->formal_args.push_back (v);
2533 fcall->args.push_back (ts->components[i].expr_index);
2534
2535 symbol* sym = new symbol;
2536 sym->tok = ts->tok;
2537 sym->name = v->name;
5f36109e
JS
2538 ts->components[i].expr_index = sym;
2539 }
2540
2541 // Create the return sprintf.
2542 token* pf_tok = new token(*ts->tok);
2543 pf_tok->content = "sprintf";
2544 print_format* pf = print_format::create(pf_tok);
2545 return_statement* rs = new return_statement;
2546 rs->tok = ts->tok;
2547 rs->value = pf;
5f36109e
JS
2548
2549 // Recurse into the actual values.
7d11d8c9 2550 recurse (&base_type, ts, pf, true);
5f36109e
JS
2551 pf->components = print_format::string_to_components(pf->raw_components);
2552
7d11d8c9
JS
2553 // Create the try-catch net
2554 try_block* tb = new try_block;
2555 tb->tok = ts->tok;
2556 tb->try_block = rs;
2557 tb->catch_error_var = 0;
2558 return_statement* rs2 = new return_statement;
2559 rs2->tok = ts->tok;
2560 rs2->value = new literal_string ("ERROR");
2561 rs2->value->tok = ts->tok;
2562 tb->catch_block = rs2;
2563 fdecl->body = tb;
2564
f8809d54 2565 fdecl->join (dw.sess);
5f36109e
JS
2566 return fcall;
2567}
2568
2569
2570void
2571dwarf_pretty_print::recurse (Dwarf_Die* start_type, target_symbol* e,
7d11d8c9 2572 print_format* pf, bool top)
5f36109e
JS
2573{
2574 Dwarf_Die type;
2575 dw.resolve_unqualified_inner_typedie (start_type, &type, e);
2576
2577 switch (dwarf_tag(&type))
2578 {
2579 default:
2580 // XXX need a warning?
2581 // throw semantic_error ("unsupported type (tag " + lex_cast(dwarf_tag(&type))
2582 // + ") for " + dwarf_type_name(&type), e->tok);
2583 pf->raw_components.append("?");
2584 break;
2585
2586 case DW_TAG_enumeration_type:
2587 case DW_TAG_base_type:
7d11d8c9 2588 recurse_base (&type, e, pf);
5f36109e
JS
2589 break;
2590
2591 case DW_TAG_array_type:
7d11d8c9 2592 recurse_array (&type, e, pf, top);
5f36109e
JS
2593 break;
2594
2595 case DW_TAG_pointer_type:
2596 case DW_TAG_reference_type:
2597 case DW_TAG_rvalue_reference_type:
7d11d8c9 2598 recurse_pointer (&type, e, pf, top);
5f36109e
JS
2599 break;
2600
2601 case DW_TAG_subroutine_type:
c55ea10d 2602 push_deref (pf, "<function>:%p", e);
5f36109e
JS
2603 break;
2604
2605 case DW_TAG_union_type:
5f36109e
JS
2606 case DW_TAG_structure_type:
2607 case DW_TAG_class_type:
7d11d8c9 2608 recurse_struct (&type, e, pf, top);
5f36109e
JS
2609 break;
2610 }
2611}
2612
2613
2614void
2615dwarf_pretty_print::recurse_base (Dwarf_Die* type, target_symbol* e,
7d11d8c9 2616 print_format* pf)
5f36109e
JS
2617{
2618 Dwarf_Attribute attr;
2619 Dwarf_Word encoding = (Dwarf_Word) -1;
2620 dwarf_formudata (dwarf_attr_integrate (type, DW_AT_encoding, &attr),
2621 &encoding);
5f36109e
JS
2622 switch (encoding)
2623 {
2624 case DW_ATE_float:
2625 case DW_ATE_complex_float:
2626 // XXX need a warning?
2627 // throw semantic_error ("unsupported type (encoding " + lex_cast(encoding)
2628 // + ") for " + dwarf_type_name(type), e->tok);
2629 pf->raw_components.append("?");
5f36109e
JS
2630 break;
2631
2632 case DW_ATE_signed_char:
2633 case DW_ATE_unsigned_char:
c55ea10d 2634 push_deref (pf, "'%c'", e);
5f36109e
JS
2635 break;
2636
2637 case DW_ATE_unsigned:
c55ea10d 2638 push_deref (pf, "%u", e);
5f36109e
JS
2639 break;
2640
2641 default:
c55ea10d 2642 push_deref (pf, "%i", e);
5f36109e
JS
2643 break;
2644 }
5f36109e
JS
2645}
2646
2647
2648void
2649dwarf_pretty_print::recurse_array (Dwarf_Die* type, target_symbol* e,
7d11d8c9 2650 print_format* pf, bool top)
5f36109e 2651{
7d11d8c9
JS
2652 if (!top && !print_full)
2653 {
2654 pf->raw_components.append("[...]");
2655 return;
2656 }
2657
5f36109e
JS
2658 Dwarf_Die childtype;
2659 dwarf_attr_die (type, DW_AT_type, &childtype);
bbee5bb8
JS
2660
2661 if (print_chars (&childtype, e, pf))
2662 return;
2663
5f36109e
JS
2664 pf->raw_components.append("[");
2665
2666 // We print the array up to the first 5 elements.
2667 // XXX how can we determine the array size?
2668 // ... for now, just print the first element
64cddf39 2669 // NB: limit to 32 args; see PR10750 and c_unparser::visit_print_format.
5f36109e 2670 unsigned i, size = 1;
64cddf39 2671 for (i=0; i < size && i < 5 && pf->args.size() < 32; ++i)
5f36109e
JS
2672 {
2673 if (i > 0)
2674 pf->raw_components.append(", ");
2675 target_symbol* e2 = new target_symbol(*e);
2676 e2->components.push_back (target_symbol::component(e->tok, i));
7d11d8c9 2677 recurse (&childtype, e2, pf);
5f36109e
JS
2678 }
2679 if (i < size || 1/*XXX until real size is known */)
2680 pf->raw_components.append(", ...");
2681 pf->raw_components.append("]");
2682}
2683
2684
2685void
2686dwarf_pretty_print::recurse_pointer (Dwarf_Die* type, target_symbol* e,
7d11d8c9 2687 print_format* pf, bool top)
5f36109e 2688{
7d11d8c9 2689 // We chase to top-level pointers, but leave the rest alone
d19a9a82 2690 bool void_p = true;
7d11d8c9 2691 Dwarf_Die pointee;
bbee5bb8 2692 if (dwarf_attr_die (type, DW_AT_type, &pointee))
d19a9a82
JS
2693 {
2694 try
2695 {
2696 dw.resolve_unqualified_inner_typedie (&pointee, &pointee, e);
2697 void_p = false;
2698 }
2699 catch (const semantic_error&) {}
2700 }
2701
2702 if (!void_p)
5f36109e 2703 {
bbee5bb8
JS
2704 if (print_chars (&pointee, e, pf))
2705 return;
2706
2707 if (top)
2708 {
2709 recurse (&pointee, e, pf, top);
2710 return;
2711 }
5f36109e 2712 }
bbee5bb8 2713
c55ea10d 2714 push_deref (pf, "%p", e);
5f36109e
JS
2715}
2716
2717
2718void
2719dwarf_pretty_print::recurse_struct (Dwarf_Die* type, target_symbol* e,
7d11d8c9 2720 print_format* pf, bool top)
5f36109e 2721{
bdec0e18
JS
2722 if (dwarf_hasattr(type, DW_AT_declaration))
2723 {
a44a7cb5 2724 Dwarf_Die *resolved = dw.declaration_resolve(type);
bdec0e18
JS
2725 if (!resolved)
2726 {
2727 // could be an error, but for now just stub it
2728 // throw semantic_error ("unresolved " + dwarf_type_name(type), e->tok);
2729 pf->raw_components.append("{...}");
2730 return;
2731 }
2732 type = resolved;
2733 }
2734
5f36109e
JS
2735 int count = 0;
2736 pf->raw_components.append("{");
7d11d8c9
JS
2737 if (top || print_full)
2738 recurse_struct_members (type, e, pf, count);
2739 else
2740 pf->raw_components.append("...");
5f36109e
JS
2741 pf->raw_components.append("}");
2742}
2743
2744
2745void
2746dwarf_pretty_print::recurse_struct_members (Dwarf_Die* type, target_symbol* e,
7d11d8c9 2747 print_format* pf, int& count)
5f36109e 2748{
a80f28d8
JS
2749 /* With inheritance, a subclass may mask member names of parent classes, so
2750 * our search among the inheritance tree must be breadth-first rather than
2751 * depth-first (recursive). The type die is still our starting point. When
2752 * we encounter a masked name, just skip it. */
2753 set<string> dupes;
2754 deque<Dwarf_Die> inheritees(1, *type);
2755 for (; !inheritees.empty(); inheritees.pop_front())
2756 {
2757 Dwarf_Die child, childtype;
2758 if (dwarf_child (&inheritees.front(), &child) == 0)
2759 do
2760 {
2761 target_symbol* e2 = e;
5f36109e 2762
a80f28d8
JS
2763 // skip static members
2764 if (dwarf_hasattr(&child, DW_AT_declaration))
2765 continue;
5f36109e 2766
a80f28d8 2767 int tag = dwarf_tag (&child);
5f36109e 2768
a80f28d8
JS
2769 if (tag != DW_TAG_member && tag != DW_TAG_inheritance)
2770 continue;
5f36109e 2771
a80f28d8 2772 dwarf_attr_die (&child, DW_AT_type, &childtype);
5f36109e 2773
a80f28d8
JS
2774 if (tag == DW_TAG_inheritance)
2775 {
2776 inheritees.push_back(childtype);
2777 continue;
2778 }
5f36109e 2779
a80f28d8
JS
2780 int childtag = dwarf_tag (&childtype);
2781 const char *member = dwarf_diename (&child);
3a147004 2782
a80f28d8
JS
2783 // "_vptr.foo" members are C++ virtual function tables,
2784 // which (generally?) aren't interesting for users.
2785 if (member && startswith(member, "_vptr."))
2786 continue;
3a147004 2787
a80f28d8
JS
2788 // skip inheritance-masked duplicates
2789 if (member && !dupes.insert(member).second)
2790 continue;
64cddf39 2791
a80f28d8
JS
2792 if (++count > 1)
2793 pf->raw_components.append(", ");
64cddf39 2794
a80f28d8
JS
2795 // NB: limit to 32 args; see PR10750 and c_unparser::visit_print_format.
2796 if (pf->args.size() >= 32)
2797 {
2798 pf->raw_components.append("...");
2799 break;
2800 }
2801
2802 if (member)
2803 {
2804 pf->raw_components.append(".");
2805 pf->raw_components.append(member);
5f36109e 2806
a80f28d8
JS
2807 e2 = new target_symbol(*e);
2808 e2->components.push_back (target_symbol::component(e->tok, member));
2809 }
2810 else if (childtag == DW_TAG_union_type)
2811 pf->raw_components.append("<union>");
2812 else if (childtag == DW_TAG_structure_type)
2813 pf->raw_components.append("<class>");
2814 else if (childtag == DW_TAG_class_type)
2815 pf->raw_components.append("<struct>");
2816 pf->raw_components.append("=");
2817 recurse (&childtype, e2, pf);
5f36109e 2818 }
a80f28d8
JS
2819 while (dwarf_siblingof (&child, &child) == 0);
2820 }
5f36109e
JS
2821}
2822
2823
bbee5bb8
JS
2824bool
2825dwarf_pretty_print::print_chars (Dwarf_Die* start_type, target_symbol* e,
2826 print_format* pf)
2827{
2828 Dwarf_Die type;
2829 dw.resolve_unqualified_inner_typedie (start_type, &type, e);
2830 const char *name = dwarf_diename (&type);
2831 if (name && (name == string("char") || name == string("unsigned char")))
2832 {
c55ea10d
JS
2833 if (push_deref (pf, "\"%s\"", e))
2834 {
2835 // steal the last arg for a string access
2836 assert (!pf->args.empty());
2837 functioncall* fcall = new functioncall;
2838 fcall->tok = e->tok;
2839 fcall->function = userspace_p ? "user_string2" : "kernel_string2";
2840 fcall->args.push_back (pf->args.back());
2841 expression *err_msg = new literal_string ("<unknown>");
2842 err_msg->tok = e->tok;
2843 fcall->args.push_back (err_msg);
2844 pf->args.back() = fcall;
2845 }
bbee5bb8
JS
2846 return true;
2847 }
2848 return false;
2849}
2850
a5ce5211
MW
2851// PR10601: adapt to kernel-vs-userspace loc2c-runtime
2852static const string EMBEDDED_FETCH_DEREF_KERNEL = string("\n")
f1e8e7e0
MW
2853 + "#define fetch_register k_fetch_register\n"
2854 + "#define store_register k_store_register\n"
2855 + "#define deref kderef\n"
2856 + "#define store_deref store_kderef\n";
a5ce5211
MW
2857
2858static const string EMBEDDED_FETCH_DEREF_USER = string("\n")
f1e8e7e0
MW
2859 + "#define fetch_register u_fetch_register\n"
2860 + "#define store_register u_store_register\n"
2861 + "#define deref uderef\n"
2862 + "#define store_deref store_uderef\n";
a5ce5211
MW
2863
2864#define EMBEDDED_FETCH_DEREF(U) \
f1e8e7e0 2865 (U ? EMBEDDED_FETCH_DEREF_USER : EMBEDDED_FETCH_DEREF_KERNEL)
a5ce5211
MW
2866
2867static const string EMBEDDED_FETCH_DEREF_DONE = string("\n")
f1e8e7e0
MW
2868 + "#undef fetch_register\n"
2869 + "#undef store_register\n"
2870 + "#undef deref\n"
2871 + "#undef store_deref\n";
bbee5bb8 2872
5f36109e
JS
2873expression*
2874dwarf_pretty_print::deref (target_symbol* e)
2875{
2876 static unsigned tick = 0;
2877
d19a9a82
JS
2878 if (!deref_p)
2879 {
2880 assert (pointer && e->components.empty());
2881 return pointer;
2882 }
2883
5f36109e
JS
2884 // Synthesize a function to dereference the dwarf fields,
2885 // with a pointer parameter that is the base tracepoint variable
2886 functiondecl *fdecl = new functiondecl;
2887 fdecl->synthetic = true;
2888 fdecl->tok = e->tok;
2889 embeddedcode *ec = new embeddedcode;
2890 ec->tok = e->tok;
2891
2892 fdecl->name = "_dwarf_pretty_print_deref_" + lex_cast(tick++);
2893 fdecl->body = ec;
2894
2895 // Synthesize a functioncall.
2896 functioncall* fcall = new functioncall;
2897 fcall->tok = e->tok;
2898 fcall->function = fdecl->name;
5f36109e 2899
a5ce5211 2900 ec->code += EMBEDDED_FETCH_DEREF(userspace_p);
5f36109e
JS
2901
2902 if (pointer)
2903 {
2904 ec->code += dw.literal_stmt_for_pointer (&pointer_type, e,
2905 false, fdecl->type);
2906
2907 vardecl *v = new vardecl;
2908 v->type = pe_long;
2909 v->name = "pointer";
2910 v->tok = e->tok;
2911 fdecl->formal_args.push_back(v);
2912 fcall->args.push_back(pointer);
2913 }
2914 else if (!local.empty())
2915 ec->code += dw.literal_stmt_for_local (scopes, pc, local, e,
2916 false, fdecl->type);
2917 else
2918 ec->code += dw.literal_stmt_for_return (&scopes[0], pc, e,
2919 false, fdecl->type);
2920
2921 // Any non-literal indexes need to be passed in too.
2922 for (unsigned i = 0; i < e->components.size(); ++i)
2923 if (e->components[i].type == target_symbol::comp_expression_array_index)
2924 {
2925 vardecl *v = new vardecl;
2926 v->type = pe_long;
2927 v->name = "index" + lex_cast(i);
2928 v->tok = e->tok;
2929 fdecl->formal_args.push_back(v);
2930 fcall->args.push_back(e->components[i].expr_index);
2931 }
2932
2933 ec->code += "/* pure */";
2934 ec->code += "/* unprivileged */";
2935
a5ce5211 2936 ec->code += EMBEDDED_FETCH_DEREF_DONE;
5f36109e 2937
f8809d54 2938 fdecl->join (dw.sess);
5f36109e
JS
2939 return fcall;
2940}
2941
2942
c55ea10d
JS
2943bool
2944dwarf_pretty_print::push_deref (print_format* pf, const string& fmt,
2945 target_symbol* e)
2946{
2947 expression* e2 = NULL;
2948 try
2949 {
2950 e2 = deref (e);
2951 }
2952 catch (const semantic_error&)
2953 {
2954 pf->raw_components.append ("?");
2955 return false;
2956 }
2957 pf->raw_components.append (fmt);
2958 pf->args.push_back (e2);
2959 return true;
2960}
2961
2962
e57b735a 2963void
a7999c82 2964dwarf_var_expanding_visitor::visit_target_symbol_saved_return (target_symbol* e)
e57b735a 2965{
a7999c82
JS
2966 // Get the full name of the target symbol.
2967 stringstream ts_name_stream;
2968 e->print(ts_name_stream);
2969 string ts_name = ts_name_stream.str();
2970
2971 // Check and make sure we haven't already seen this target
2972 // variable in this return probe. If we have, just return our
2973 // last replacement.
af234c40 2974 map<string, expression *>::iterator i = return_ts_map.find(ts_name);
a7999c82 2975 if (i != return_ts_map.end())
85ecf79a 2976 {
a7999c82
JS
2977 provide (i->second);
2978 return;
2979 }
85ecf79a 2980
70208613
JS
2981 // Attempt the expansion directly first, so if there's a problem with the
2982 // variable we won't have a bogus entry probe lying around. Like in
2983 // saveargs(), we pretend for a moment that we're not in a .return.
2984 bool saved_has_return = q.has_return;
2985 q.has_return = false;
2986 expression *repl = e;
2987 replace (repl);
2988 q.has_return = saved_has_return;
2989 target_symbol* n = dynamic_cast<target_symbol*>(repl);
2990 if (n && n->saved_conversion_error)
2991 {
2992 provide (repl);
2993 return;
2994 }
2995
af234c40
JS
2996 expression *exp;
2997 if (!q.has_process &&
2998 strverscmp(q.sess.kernel_base_release.c_str(), "2.6.25") >= 0)
140be17a 2999 exp = gen_kretprobe_saved_return(repl);
af234c40 3000 else
cc9001af 3001 exp = gen_mapped_saved_return(repl, e->sym_name());
af234c40
JS
3002
3003 // Provide the variable to our parent so it can be used as a
3004 // substitute for the target symbol.
3005 provide (exp);
3006
3007 // Remember this replacement since we might be able to reuse
3008 // it later if the same return probe references this target
3009 // symbol again.
3010 return_ts_map[ts_name] = exp;
3011}
3012
3013expression*
70208613 3014dwarf_var_expanding_visitor::gen_mapped_saved_return(expression* e,
277c21bc 3015 const string& name)
af234c40 3016{
a7999c82
JS
3017 // We've got to do several things here to handle target
3018 // variables in return probes.
85ecf79a 3019
a7999c82
JS
3020 // (1) Synthesize two global arrays. One is the cache of the
3021 // target variable and the other contains a thread specific
3022 // nesting level counter. The arrays will look like
3023 // this:
3024 //
3025 // _dwarf_tvar_{name}_{num}
3026 // _dwarf_tvar_{name}_{num}_ctr
3027
3028 string aname = (string("_dwarf_tvar_")
cc9001af 3029 + name
aca66a36 3030 + "_" + lex_cast(tick++));
a7999c82
JS
3031 vardecl* vd = new vardecl;
3032 vd->name = aname;
3033 vd->tok = e->tok;
3034 q.sess.globals.push_back (vd);
3035
3036 string ctrname = aname + "_ctr";
3037 vd = new vardecl;
3038 vd->name = ctrname;
3039 vd->tok = e->tok;
3040 q.sess.globals.push_back (vd);
3041
3042 // (2) Create a new code block we're going to insert at the
3043 // beginning of this probe to get the cached value into a
3044 // temporary variable. We'll replace the target variable
3045 // reference with the temporary variable reference. The code
3046 // will look like this:
3047 //
3048 // _dwarf_tvar_tid = tid()
3049 // _dwarf_tvar_{name}_{num}_tmp
3050 // = _dwarf_tvar_{name}_{num}[_dwarf_tvar_tid,
3051 // _dwarf_tvar_{name}_{num}_ctr[_dwarf_tvar_tid]]
3052 // delete _dwarf_tvar_{name}_{num}[_dwarf_tvar_tid,
3053 // _dwarf_tvar_{name}_{num}_ctr[_dwarf_tvar_tid]--]
3054 // if (! _dwarf_tvar_{name}_{num}_ctr[_dwarf_tvar_tid])
3055 // delete _dwarf_tvar_{name}_{num}_ctr[_dwarf_tvar_tid]
3056
3057 // (2a) Synthesize the tid temporary expression, which will look
3058 // like this:
3059 //
3060 // _dwarf_tvar_tid = tid()
3061 symbol* tidsym = new symbol;
3062 tidsym->name = string("_dwarf_tvar_tid");
3063 tidsym->tok = e->tok;
85ecf79a 3064
a7999c82
JS
3065 if (add_block == NULL)
3066 {
3067 add_block = new block;
3068 add_block->tok = e->tok;
8cc799a5 3069 }
8c819921 3070
8cc799a5
JS
3071 if (!add_block_tid)
3072 {
a7999c82
JS
3073 // Synthesize a functioncall to grab the thread id.
3074 functioncall* fc = new functioncall;
3075 fc->tok = e->tok;
3076 fc->function = string("tid");
8c819921 3077
a7999c82 3078 // Assign the tid to '_dwarf_tvar_tid'.
8c819921
DS
3079 assignment* a = new assignment;
3080 a->tok = e->tok;
3081 a->op = "=";
a7999c82
JS
3082 a->left = tidsym;
3083 a->right = fc;
8c819921
DS
3084
3085 expr_statement* es = new expr_statement;
3086 es->tok = e->tok;
3087 es->value = a;
8c819921 3088 add_block->statements.push_back (es);
8cc799a5 3089 add_block_tid = true;
a7999c82 3090 }
8c819921 3091
a7999c82
JS
3092 // (2b) Synthesize an array reference and assign it to a
3093 // temporary variable (that we'll use as replacement for the
3094 // target variable reference). It will look like this:
3095 //
3096 // _dwarf_tvar_{name}_{num}_tmp
3097 // = _dwarf_tvar_{name}_{num}[_dwarf_tvar_tid,
3098 // _dwarf_tvar_{name}_{num}_ctr[_dwarf_tvar_tid]]
3099
3100 arrayindex* ai_tvar_base = new arrayindex;
3101 ai_tvar_base->tok = e->tok;
3102
3103 symbol* sym = new symbol;
3104 sym->name = aname;
3105 sym->tok = e->tok;
3106 ai_tvar_base->base = sym;
3107
3108 ai_tvar_base->indexes.push_back(tidsym);
3109
3110 // We need to create a copy of the array index in its current
3111 // state so we can have 2 variants of it (the original and one
3112 // that post-decrements the second index).
3113 arrayindex* ai_tvar = new arrayindex;
3114 arrayindex* ai_tvar_postdec = new arrayindex;
3115 *ai_tvar = *ai_tvar_base;
3116 *ai_tvar_postdec = *ai_tvar_base;
3117
3118 // Synthesize the
3119 // "_dwarf_tvar_{name}_{num}_ctr[_dwarf_tvar_tid]" used as the
3120 // second index into the array.
3121 arrayindex* ai_ctr = new arrayindex;
3122 ai_ctr->tok = e->tok;
3123
3124 sym = new symbol;
3125 sym->name = ctrname;
3126 sym->tok = e->tok;
3127 ai_ctr->base = sym;
3128 ai_ctr->indexes.push_back(tidsym);
3129 ai_tvar->indexes.push_back(ai_ctr);
3130
3131 symbol* tmpsym = new symbol;
3132 tmpsym->name = aname + "_tmp";
3133 tmpsym->tok = e->tok;
3134
3135 assignment* a = new assignment;
3136 a->tok = e->tok;
3137 a->op = "=";
3138 a->left = tmpsym;
3139 a->right = ai_tvar;
3140
3141 expr_statement* es = new expr_statement;
3142 es->tok = e->tok;
3143 es->value = a;
3144
3145 add_block->statements.push_back (es);
3146
3147 // (2c) Add a post-decrement to the second array index and
3148 // delete the array value. It will look like this:
3149 //
3150 // delete _dwarf_tvar_{name}_{num}[_dwarf_tvar_tid,
3151 // _dwarf_tvar_{name}_{num}_ctr[_dwarf_tvar_tid]--]
3152
3153 post_crement* pc = new post_crement;
3154 pc->tok = e->tok;
3155 pc->op = "--";
3156 pc->operand = ai_ctr;
3157 ai_tvar_postdec->indexes.push_back(pc);
3158
3159 delete_statement* ds = new delete_statement;
3160 ds->tok = e->tok;
3161 ds->value = ai_tvar_postdec;
3162
3163 add_block->statements.push_back (ds);
3164
3165 // (2d) Delete the counter value if it is 0. It will look like
3166 // this:
3167 // if (! _dwarf_tvar_{name}_{num}_ctr[_dwarf_tvar_tid])
3168 // delete _dwarf_tvar_{name}_{num}_ctr[_dwarf_tvar_tid]
3169
3170 ds = new delete_statement;
3171 ds->tok = e->tok;
3172 ds->value = ai_ctr;
3173
3174 unary_expression *ue = new unary_expression;
3175 ue->tok = e->tok;
3176 ue->op = "!";
3177 ue->operand = ai_ctr;
3178
3179 if_statement *ifs = new if_statement;
3180 ifs->tok = e->tok;
3181 ifs->condition = ue;
3182 ifs->thenblock = ds;
3183 ifs->elseblock = NULL;
3184
3185 add_block->statements.push_back (ifs);
3186
3187 // (3) We need an entry probe that saves the value for us in the
3188 // global array we created. Create the entry probe, which will
3189 // look like this:
3190 //
2260f4e3 3191 // probe kernel.function("{function}").call {
a7999c82
JS
3192 // _dwarf_tvar_tid = tid()
3193 // _dwarf_tvar_{name}_{num}[_dwarf_tvar_tid,
3194 // ++_dwarf_tvar_{name}_{num}_ctr[_dwarf_tvar_tid]]
3195 // = ${param}
3196 // }
3197
2260f4e3 3198 if (add_call_probe == NULL)
a7999c82 3199 {
2260f4e3
FCE
3200 add_call_probe = new block;
3201 add_call_probe->tok = e->tok;
8cc799a5 3202 }
4baf0e53 3203
8cc799a5
JS
3204 if (!add_call_probe_tid)
3205 {
a7999c82
JS
3206 // Synthesize a functioncall to grab the thread id.
3207 functioncall* fc = new functioncall;
3208 fc->tok = e->tok;
3209 fc->function = string("tid");
4baf0e53 3210
a7999c82
JS
3211 // Assign the tid to '_dwarf_tvar_tid'.
3212 assignment* a = new assignment;
8fc05e57
DS
3213 a->tok = e->tok;
3214 a->op = "=";
a7999c82
JS
3215 a->left = tidsym;
3216 a->right = fc;
8fc05e57 3217
a7999c82 3218 expr_statement* es = new expr_statement;
8fc05e57
DS
3219 es->tok = e->tok;
3220 es->value = a;
2260f4e3 3221 add_call_probe = new block(add_call_probe, es);
8cc799a5 3222 add_call_probe_tid = true;
85ecf79a 3223 }
cf2a1f85 3224
a7999c82
JS
3225 // Save the value, like this:
3226 // _dwarf_tvar_{name}_{num}[_dwarf_tvar_tid,
3227 // ++_dwarf_tvar_{name}_{num}_ctr[_dwarf_tvar_tid]]
3228 // = ${param}
3229 arrayindex* ai_tvar_preinc = new arrayindex;
3230 *ai_tvar_preinc = *ai_tvar_base;
3231
3232 pre_crement* preinc = new pre_crement;
3233 preinc->tok = e->tok;
3234 preinc->op = "++";
3235 preinc->operand = ai_ctr;
3236 ai_tvar_preinc->indexes.push_back(preinc);
3237
3238 a = new assignment;
3239 a->tok = e->tok;
3240 a->op = "=";
3241 a->left = ai_tvar_preinc;
3242 a->right = e;
3243
3244 es = new expr_statement;
3245 es->tok = e->tok;
3246 es->value = a;
3247
2260f4e3 3248 add_call_probe = new block(add_call_probe, es);
a7999c82
JS
3249
3250 // (4) Provide the '_dwarf_tvar_{name}_{num}_tmp' variable to
3251 // our parent so it can be used as a substitute for the target
3252 // symbol.
af234c40
JS
3253 return tmpsym;
3254}
a7999c82 3255
af234c40
JS
3256
3257expression*
140be17a 3258dwarf_var_expanding_visitor::gen_kretprobe_saved_return(expression* e)
af234c40
JS
3259{
3260 // The code for this is simple.
3261 //
3262 // .call:
3263 // _set_kretprobe_long(index, $value)
3264 //
3265 // .return:
3266 // _get_kretprobe_long(index)
3267 //
3268 // (or s/long/string/ for things like $$parms)
3269
3270 unsigned index;
3271 string setfn, getfn;
3272
140be17a
JS
3273 // We need the caller to predetermine the type of the expression!
3274 switch (e->type)
af234c40 3275 {
140be17a 3276 case pe_string:
af234c40
JS
3277 index = saved_strings++;
3278 setfn = "_set_kretprobe_string";
3279 getfn = "_get_kretprobe_string";
140be17a
JS
3280 break;
3281 case pe_long:
af234c40
JS
3282 index = saved_longs++;
3283 setfn = "_set_kretprobe_long";
3284 getfn = "_get_kretprobe_long";
140be17a
JS
3285 break;
3286 default:
b530b5b3 3287 throw semantic_error(_("unknown type to save in kretprobe"), e->tok);
af234c40
JS
3288 }
3289
3290 // Create the entry code
3291 // _set_kretprobe_{long|string}(index, $value)
3292
3293 if (add_call_probe == NULL)
3294 {
3295 add_call_probe = new block;
3296 add_call_probe->tok = e->tok;
3297 }
3298
3299 functioncall* set_fc = new functioncall;
3300 set_fc->tok = e->tok;
3301 set_fc->function = setfn;
3302 set_fc->args.push_back(new literal_number(index));
3303 set_fc->args.back()->tok = e->tok;
3304 set_fc->args.push_back(e);
3305
3306 expr_statement* set_es = new expr_statement;
3307 set_es->tok = e->tok;
3308 set_es->value = set_fc;
3309
3310 add_call_probe->statements.push_back(set_es);
3311
3312 // Create the return code
3313 // _get_kretprobe_{long|string}(index)
3314
3315 functioncall* get_fc = new functioncall;
3316 get_fc->tok = e->tok;
3317 get_fc->function = getfn;
3318 get_fc->args.push_back(new literal_number(index));
3319 get_fc->args.back()->tok = e->tok;
3320
3321 return get_fc;
a7999c82 3322}
a43ba433 3323
2cb3fe26 3324
a7999c82
JS
3325void
3326dwarf_var_expanding_visitor::visit_target_symbol_context (target_symbol* e)
3327{
9aa8ffce 3328 if (null_die(scope_die))
a7999c82 3329 return;
2cb3fe26 3330
5f36109e
JS
3331 target_symbol *tsym = new target_symbol(*e);
3332
fde50242
JS
3333 bool pretty = (!e->components.empty() &&
3334 e->components[0].type == target_symbol::comp_pretty_print);
3335 string format = pretty ? "=%s" : "=%#x";
a43ba433 3336
a7999c82
JS
3337 // Convert $$parms to sprintf of a list of parms and active local vars
3338 // which we recursively evaluate
a43ba433 3339
a7999c82
JS
3340 // NB: we synthesize a new token here rather than reusing
3341 // e->tok, because print_format::print likes to use
3342 // its tok->content.
5f36109e 3343 token* pf_tok = new token(*e->tok);
a7999c82 3344 pf_tok->type = tok_identifier;
b393f6f2 3345 pf_tok->content = "sprintf";
2cb3fe26 3346
d5e178c1 3347 print_format* pf = print_format::create(pf_tok);
a7999c82 3348
277c21bc 3349 if (q.has_return && (e->name == "$$return"))
a7999c82 3350 {
277c21bc 3351 tsym->name = "$return";
a7999c82
JS
3352
3353 // Ignore any variable that isn't accessible.
3354 tsym->saved_conversion_error = 0;
3355 expression *texp = tsym;
8b095b45 3356 replace (texp); // NB: throws nothing ...
a7999c82 3357 if (tsym->saved_conversion_error) // ... but this is how we know it happened.
a43ba433 3358 {
2cb3fe26 3359
a43ba433
FCE
3360 }
3361 else
3362 {
a7999c82 3363 pf->raw_components += "return";
5f36109e 3364 pf->raw_components += format;
a7999c82
JS
3365 pf->args.push_back(texp);
3366 }
3367 }
3368 else
3369 {
3370 // non-.return probe: support $$parms, $$vars, $$locals
345bbb3d 3371 bool first = true;
a7999c82 3372 Dwarf_Die result;
d48bc7eb
JS
3373 vector<Dwarf_Die> scopes = q.dw.getscopes(scope_die);
3374 for (unsigned i = 0; i < scopes.size(); ++i)
3375 {
3376 if (dwarf_tag(&scopes[i]) == DW_TAG_compile_unit)
3377 break; // we don't want file-level variables
3378 if (dwarf_child (&scopes[i], &result) == 0)
3379 do
00cf3709 3380 {
d48bc7eb
JS
3381 switch (dwarf_tag (&result))
3382 {
3383 case DW_TAG_variable:
3384 if (e->name == "$$parms")
3385 continue;
3386 break;
3387 case DW_TAG_formal_parameter:
3388 if (e->name == "$$locals")
3389 continue;
3390 break;
3391
3392 default:
3393 continue;
3394 }
41c262f3 3395
d48bc7eb
JS
3396 const char *diename = dwarf_diename (&result);
3397 if (! diename) continue;
f76427a2 3398
d48bc7eb
JS
3399 if (! first)
3400 pf->raw_components += " ";
3401 pf->raw_components += diename;
fde50242
JS
3402 first = false;
3403
3404 // Write a placeholder for ugly aggregates
3405 Dwarf_Die type;
3406 if (!pretty && dwarf_attr_die(&result, DW_AT_type, &type))
3407 {
3408 q.dw.resolve_unqualified_inner_typedie(&type, &type, e);
3409 switch (dwarf_tag(&type))
3410 {
3411 case DW_TAG_union_type:
3412 case DW_TAG_structure_type:
3413 case DW_TAG_class_type:
3414 pf->raw_components += "={...}";
3415 continue;
3416
3417 case DW_TAG_array_type:
3418 pf->raw_components += "=[...]";
3419 continue;
3420 }
3421 }
345bbb3d 3422
d48bc7eb
JS
3423 tsym->name = "$";
3424 tsym->name += diename;
41c262f3 3425
d48bc7eb
JS
3426 // Ignore any variable that isn't accessible.
3427 tsym->saved_conversion_error = 0;
3428 expression *texp = tsym;
3429 replace (texp); // NB: throws nothing ...
3430 if (tsym->saved_conversion_error) // ... but this is how we know it happened.
a43ba433 3431 {
d48bc7eb
JS
3432 if (q.sess.verbose>2)
3433 {
e26c2f83 3434 for (const semantic_error *c = tsym->saved_conversion_error;
d48bc7eb
JS
3435 c != 0;
3436 c = c->chain) {
b530b5b3 3437 clog << _("variable location problem: ") << c->what() << endl;
d48bc7eb
JS
3438 }
3439 }
3440
3441 pf->raw_components += "=?";
a43ba433 3442 }
d48bc7eb
JS
3443 else
3444 {
3445 pf->raw_components += format;
3446 pf->args.push_back(texp);
3447 }
a7999c82 3448 }
d48bc7eb
JS
3449 while (dwarf_siblingof (&result, &result) == 0);
3450 }
a7999c82 3451 }
2cb3fe26 3452
a7999c82 3453 pf->components = print_format::string_to_components(pf->raw_components);
140be17a 3454 pf->type = pe_string;
a7999c82
JS
3455 provide (pf);
3456}
3457
2cb3fe26 3458
a7999c82
JS
3459void
3460dwarf_var_expanding_visitor::visit_target_symbol (target_symbol *e)
3461{
cc9001af
MW
3462 assert(e->name.size() > 0
3463 && ((e->name[0] == '$' && e->target_name == "")
3464 || (e->name == "@var" && e->target_name != "")));
a7999c82 3465 visited = true;
30263a73
FCE
3466 bool defined_being_checked = (defined_ops.size() > 0 && (defined_ops.top()->operand == e));
3467 // In this mode, we avoid hiding errors or generating extra code such as for .return saved $vars
a7999c82 3468
70208613 3469 try
a7999c82 3470 {
c69a87e0
FCE
3471 bool lvalue = is_active_lvalue(e);
3472 if (lvalue && !q.sess.guru_mode)
b3741c9d 3473 throw semantic_error(_("write to target variable not permitted; need stap -g"), e->tok);
2cb3fe26 3474
100a540e 3475 // XXX: process $context vars should be writable
70208613 3476
c69a87e0
FCE
3477 // See if we need to generate a new probe to save/access function
3478 // parameters from a return probe. PR 1382.
3479 if (q.has_return
3480 && !defined_being_checked
277c21bc
JS
3481 && e->name != "$return" // not the special return-value variable handled below
3482 && e->name != "$$return") // nor the other special variable handled below
c69a87e0
FCE
3483 {
3484 if (lvalue)
b530b5b3 3485 throw semantic_error(_("write to target variable not permitted in .return probes"), e->tok);
c69a87e0
FCE
3486 visit_target_symbol_saved_return(e);
3487 return;
3488 }
e57b735a 3489
277c21bc
JS
3490 if (e->name == "$$vars" || e->name == "$$parms" || e->name == "$$locals"
3491 || (q.has_return && (e->name == "$$return")))
c69a87e0
FCE
3492 {
3493 if (lvalue)
b530b5b3 3494 throw semantic_error(_("cannot write to context variable"), e->tok);
70208613 3495
c69a87e0 3496 if (e->addressof)
b530b5b3 3497 throw semantic_error(_("cannot take address of context variable"), e->tok);
70208613 3498
5f36109e
JS
3499 e->assert_no_components("dwarf", true);
3500
c69a87e0
FCE
3501 visit_target_symbol_context(e);
3502 return;
3503 }
70208613 3504
5f36109e
JS
3505 if (!e->components.empty() &&
3506 e->components.back().type == target_symbol::comp_pretty_print)
3507 {
3508 if (lvalue)
b530b5b3 3509 throw semantic_error(_("cannot write to pretty-printed variable"), e->tok);
5f36109e 3510
277c21bc 3511 if (q.has_return && (e->name == "$return"))
5f36109e
JS
3512 {
3513 dwarf_pretty_print dpp (q.dw, scope_die, addr,
3514 q.has_process, *e);
3515 dpp.expand()->visit(this);
3516 }
3517 else
3518 {
3519 dwarf_pretty_print dpp (q.dw, getscopes(e), addr,
cc9001af 3520 e->sym_name(),
5f36109e
JS
3521 q.has_process, *e);
3522 dpp.expand()->visit(this);
3523 }
3524 return;
3525 }
3526
c69a87e0
FCE
3527 // Synthesize a function.
3528 functiondecl *fdecl = new functiondecl;
59de45f1 3529 fdecl->synthetic = true;
c69a87e0
FCE
3530 fdecl->tok = e->tok;
3531 embeddedcode *ec = new embeddedcode;
3532 ec->tok = e->tok;
70208613 3533
c69a87e0 3534 string fname = (string(lvalue ? "_dwarf_tvar_set" : "_dwarf_tvar_get")
cc9001af 3535 + "_" + e->sym_name()
c69a87e0 3536 + "_" + lex_cast(tick++));
70208613 3537
a5ce5211 3538 ec->code += EMBEDDED_FETCH_DEREF(q.has_process);
70208613 3539
277c21bc 3540 if (q.has_return && (e->name == "$return"))
e19fda4e 3541 {
b5a0dd41 3542 ec->code += q.dw.literal_stmt_for_return (scope_die,
e19fda4e 3543 addr,
b4c34c26 3544 e,
e19fda4e
DS
3545 lvalue,
3546 fdecl->type);
3547 }
3548 else
3549 {
b5a0dd41 3550 ec->code += q.dw.literal_stmt_for_local (getscopes(e),
e19fda4e 3551 addr,
cc9001af 3552 e->sym_name(),
b4c34c26 3553 e,
e19fda4e
DS
3554 lvalue,
3555 fdecl->type);
3556 }
3557
1b07c728
FCE
3558 if (! lvalue)
3559 ec->code += "/* pure */";
64211010
DB
3560
3561 ec->code += "/* unprivileged */";
a5ce5211 3562 ec->code += EMBEDDED_FETCH_DEREF_DONE;
c69a87e0
FCE
3563
3564 fdecl->name = fname;
3565 fdecl->body = ec;
70208613 3566
c69a87e0
FCE
3567 // Any non-literal indexes need to be passed in too.
3568 for (unsigned i = 0; i < e->components.size(); ++i)
3569 if (e->components[i].type == target_symbol::comp_expression_array_index)
3570 {
3571 vardecl *v = new vardecl;
3572 v->type = pe_long;
3573 v->name = "index" + lex_cast(i);
3574 v->tok = e->tok;
3575 fdecl->formal_args.push_back(v);
3576 }
70208613 3577
c69a87e0
FCE
3578 if (lvalue)
3579 {
3580 // Modify the fdecl so it carries a single pe_long formal
3581 // argument called "value".
70208613 3582
c69a87e0
FCE
3583 // FIXME: For the time being we only support setting target
3584 // variables which have base types; these are 'pe_long' in
3585 // stap's type vocabulary. Strings and pointers might be
3586 // reasonable, some day, but not today.
70208613 3587
c69a87e0
FCE
3588 vardecl *v = new vardecl;
3589 v->type = pe_long;
3590 v->name = "value";
3591 v->tok = e->tok;
3592 fdecl->formal_args.push_back(v);
3593 }
f8809d54 3594 fdecl->join (q.sess);
70208613 3595
c69a87e0
FCE
3596 // Synthesize a functioncall.
3597 functioncall* n = new functioncall;
3598 n->tok = e->tok;
3599 n->function = fname;
140be17a 3600 n->type = fdecl->type;
70208613 3601
c69a87e0
FCE
3602 // Any non-literal indexes need to be passed in too.
3603 for (unsigned i = 0; i < e->components.size(); ++i)
3604 if (e->components[i].type == target_symbol::comp_expression_array_index)
3605 n->args.push_back(require(e->components[i].expr_index));
70208613 3606
c69a87e0
FCE
3607 if (lvalue)
3608 {
3609 // Provide the functioncall to our parent, so that it can be
3610 // used to substitute for the assignment node immediately above
3611 // us.
3612 assert(!target_symbol_setter_functioncalls.empty());
3613 *(target_symbol_setter_functioncalls.top()) = n;
3614 }
70208613 3615
c69a87e0 3616 provide (n);
66d284f4
FCE
3617 }
3618 catch (const semantic_error& er)
3619 {
9fab2262
JS
3620 // We suppress this error message, and pass the unresolved
3621 // target_symbol to the next pass. We hope that this value ends
3622 // up not being referenced after all, so it can be optimized out
3623 // quietly.
1af1e62d 3624 e->chain (er);
9fab2262 3625 provide (e);
66d284f4 3626 }
77de5e9e
GH
3627}
3628
3629
c24447be
JS
3630void
3631dwarf_var_expanding_visitor::visit_cast_op (cast_op *e)
3632{
3633 // Fill in our current module context if needed
3634 if (e->module.empty())
3635 e->module = q.dw.module_name;
3636
3637 var_expanding_visitor::visit_cast_op(e);
3638}
3639
3640
8cc799a5
JS
3641void
3642dwarf_var_expanding_visitor::visit_entry_op (entry_op *e)
3643{
3644 expression *repl = e;
3645 if (q.has_return)
3646 {
3647 // expand the operand as if it weren't a return probe
3648 q.has_return = false;
3649 replace (e->operand);
3650 q.has_return = true;
3651
3652 // XXX it would be nice to use gen_kretprobe_saved_return when available,
3653 // but it requires knowing the types already, which is problematic for
3654 // arbitrary expressons.
cc9001af 3655 repl = gen_mapped_saved_return (e->operand, "entry");
8cc799a5
JS
3656 }
3657 provide (repl);
3658}
3659
bfa7e523
MW
3660vector<Dwarf_Die>&
3661dwarf_var_expanding_visitor::getcuscope(target_symbol *e)
3662{
54e9f062
MW
3663 Dwarf_Off cu_off = 0;
3664 const char *cu_name = NULL;
bfa7e523
MW
3665
3666 string prefixed_srcfile = string("*/") + e->cu_name;
3667
3668 Dwarf_Off off = 0;
3669 size_t cuhl;
3670 Dwarf_Off noff;
3671 Dwarf_Off module_bias;
3672 Dwarf *dw = dwfl_module_getdwarf(q.dw.module, &module_bias);
54e9f062 3673 while (dwarf_nextcu (dw, off, &noff, &cuhl, NULL, NULL, NULL) == 0)
bfa7e523
MW
3674 {
3675 Dwarf_Die die_mem;
3676 Dwarf_Die *die;
3677 die = dwarf_offdie (dw, off + cuhl, &die_mem);
54e9f062
MW
3678 const char *die_name = dwarf_diename (die);
3679
3680 if (strcmp (die_name, e->cu_name.c_str()) == 0) // Perfect match.
3681 {
3682 cu_name = die_name;
3683 cu_off = off + cuhl;
3684 break;
3685 }
3686
3687 if (fnmatch(prefixed_srcfile.c_str(), die_name, 0) == 0)
3688 if (cu_name == NULL || strlen (die_name) < strlen (cu_name))
3689 {
3690 cu_name = die_name;
3691 cu_off = off + cuhl;
3692 }
bfa7e523
MW
3693 off = noff;
3694 }
3695
54e9f062 3696 if (cu_name == NULL)
bfa7e523
MW
3697 throw semantic_error ("unable to find CU '" + e->cu_name + "'"
3698 + " while searching for '" + e->target_name + "'",
3699 e->tok);
3700
3701 vector<Dwarf_Die> *cu_scope = new vector<Dwarf_Die>;
54e9f062
MW
3702 Dwarf_Die cu_die;
3703 dwarf_offdie (dw, cu_off, &cu_die);
3704 cu_scope->push_back(cu_die);
bfa7e523
MW
3705 return *cu_scope;
3706}
8cc799a5 3707
729455a7
JS
3708vector<Dwarf_Die>&
3709dwarf_var_expanding_visitor::getscopes(target_symbol *e)
3710{
bfa7e523
MW
3711 // "static globals" can only be found in the top-level CU.
3712 if (e->name == "@var" && e->cu_name != "")
3713 return this->getcuscope(e);
3714
729455a7
JS
3715 if (scopes.empty())
3716 {
f5958c8f 3717 scopes = q.dw.getscopes(scope_die);
729455a7 3718 if (scopes.empty())
b530b5b3
LB
3719 //throw semantic_error (_F("unable to find any scopes containing %d", addr), e->tok);
3720 // ((scope_die == NULL) ? "" : (string (" in ") + (dwarf_diename(scope_die) ?: "<unknown>") + "(" + (dwarf_diename(q.dw.cu) ?: "<unknown>") ")" ))
729455a7
JS
3721 throw semantic_error ("unable to find any scopes containing "
3722 + lex_cast_hex(addr)
3723 + ((scope_die == NULL) ? ""
3724 : (string (" in ")
3725 + (dwarf_diename(scope_die) ?: "<unknown>")
3726 + "(" + (dwarf_diename(q.dw.cu) ?: "<unknown>")
3727 + ")"))
3728 + " while searching for local '"
cc9001af 3729 + e->sym_name() + "'",
729455a7
JS
3730 e->tok);
3731 }
3732 return scopes;
3733}
3734
3735
5f36109e
JS
3736struct dwarf_cast_expanding_visitor: public var_expanding_visitor
3737{
3738 systemtap_session& s;
3739 dwarf_builder& db;
3740
3741 dwarf_cast_expanding_visitor(systemtap_session& s, dwarf_builder& db):
3742 s(s), db(db) {}
3743 void visit_cast_op (cast_op* e);
3744 void filter_special_modules(string& module);
3745};
3746
3747
c4ce66a1
JS
3748struct dwarf_cast_query : public base_query
3749{
946e1a48 3750 cast_op& e;
c4ce66a1 3751 const bool lvalue;
5f36109e
JS
3752 const bool userspace_p;
3753 functioncall*& result;
c4ce66a1 3754
5f36109e
JS
3755 dwarf_cast_query(dwflpp& dw, const string& module, cast_op& e, bool lvalue,
3756 const bool userspace_p, functioncall*& result):
abb41d92 3757 base_query(dw, module), e(e), lvalue(lvalue),
5f36109e 3758 userspace_p(userspace_p), result(result) {}
c4ce66a1
JS
3759
3760 void handle_query_module();
822a6a3d 3761 void query_library (const char *) {}
576eaefe 3762 void query_plt (const char *entry, size_t addr) {}
c4ce66a1
JS
3763};
3764
3765
c4ce66a1
JS
3766void
3767dwarf_cast_query::handle_query_module()
3768{
5f36109e
JS
3769 static unsigned tick = 0;
3770
3771 if (result)
c4ce66a1
JS
3772 return;
3773
ea1e477a 3774 // look for the type in any CU
a44a7cb5
JS
3775 Dwarf_Die* type_die = NULL;
3776 if (startswith(e.type_name, "class "))
3777 {
3778 // normalize to match dwflpp::global_alias_caching_callback
3779 string struct_name = "struct " + e.type_name.substr(6);
3780 type_die = dw.declaration_resolve_other_cus(struct_name);
3781 }
3782 else
3783 type_die = dw.declaration_resolve_other_cus(e.type_name);
3784
3785 // NB: We now index the types as "struct name"/"union name"/etc. instead of
3786 // just "name". But since we didn't require users to be explicit before, and
3787 // actually sort of discouraged it, we must be flexible now. So if a lookup
3788 // fails with a bare name, try augmenting it.
3789 if (!type_die &&
3790 !startswith(e.type_name, "class ") &&
3791 !startswith(e.type_name, "struct ") &&
3792 !startswith(e.type_name, "union ") &&
3793 !startswith(e.type_name, "enum "))
3794 {
3795 type_die = dw.declaration_resolve_other_cus("struct " + e.type_name);
3796 if (!type_die)
3797 type_die = dw.declaration_resolve_other_cus("union " + e.type_name);
3798 if (!type_die)
3799 type_die = dw.declaration_resolve_other_cus("enum " + e.type_name);
3800 }
3801
ea1e477a
JS
3802 if (!type_die)
3803 return;
c4ce66a1 3804
5f36109e
JS
3805 string code;
3806 exp_type type = pe_long;
3807
ea1e477a 3808 try
c4ce66a1 3809 {
ea1e477a
JS
3810 Dwarf_Die cu_mem;
3811 dw.focus_on_cu(dwarf_diecu(type_die, &cu_mem, NULL, NULL));
5f36109e
JS
3812
3813 if (!e.components.empty() &&
3814 e.components.back().type == target_symbol::comp_pretty_print)
3815 {
3816 if (lvalue)
b530b5b3 3817 throw semantic_error(_("cannot write to pretty-printed variable"), e.tok);
5f36109e 3818
d19a9a82 3819 dwarf_pretty_print dpp(dw, type_die, e.operand, true, userspace_p, e);
5f36109e
JS
3820 result = dpp.expand();
3821 return;
3822 }
3823
3824 code = dw.literal_stmt_for_pointer (type_die, &e, lvalue, type);
ea1e477a
JS
3825 }
3826 catch (const semantic_error& er)
3827 {
3828 // NB: we can have multiple errors, since a @cast
1af1e62d
JS
3829 // may be attempted using several different modules:
3830 // @cast(ptr, "type", "module1:module2:...")
3831 e.chain (er);
c4ce66a1 3832 }
c4ce66a1 3833
5f36109e
JS
3834 if (code.empty())
3835 return;
c4ce66a1 3836
5f36109e 3837 string fname = (string(lvalue ? "_dwarf_cast_set" : "_dwarf_cast_get")
cc9001af 3838 + "_" + e.sym_name()
5f36109e 3839 + "_" + lex_cast(tick++));
c4ce66a1 3840
5f36109e
JS
3841 // Synthesize a function.
3842 functiondecl *fdecl = new functiondecl;
3843 fdecl->synthetic = true;
3844 fdecl->tok = e.tok;
3845 fdecl->type = type;
3846 fdecl->name = fname;
3847
3848 embeddedcode *ec = new embeddedcode;
3849 ec->tok = e.tok;
3850 fdecl->body = ec;
3851
a5ce5211 3852 ec->code += EMBEDDED_FETCH_DEREF(userspace_p);
5f36109e
JS
3853 ec->code += code;
3854
3855 // Give the fdecl an argument for the pointer we're trying to cast
3856 vardecl *v1 = new vardecl;
3857 v1->type = pe_long;
3858 v1->name = "pointer";
3859 v1->tok = e.tok;
3860 fdecl->formal_args.push_back(v1);
3861
3862 // Any non-literal indexes need to be passed in too.
3863 for (unsigned i = 0; i < e.components.size(); ++i)
3864 if (e.components[i].type == target_symbol::comp_expression_array_index)
3865 {
3866 vardecl *v = new vardecl;
3867 v->type = pe_long;
3868 v->name = "index" + lex_cast(i);
3869 v->tok = e.tok;
3870 fdecl->formal_args.push_back(v);
3871 }
3872
3873 if (lvalue)
3874 {
3875 // Modify the fdecl so it carries a second pe_long formal
3876 // argument called "value".
3877
3878 // FIXME: For the time being we only support setting target
3879 // variables which have base types; these are 'pe_long' in
3880 // stap's type vocabulary. Strings and pointers might be
3881 // reasonable, some day, but not today.
3882
3883 vardecl *v2 = new vardecl;
3884 v2->type = pe_long;
3885 v2->name = "value";
3886 v2->tok = e.tok;
3887 fdecl->formal_args.push_back(v2);
3888 }
3889 else
3890 ec->code += "/* pure */";
3891
3892 ec->code += "/* unprivileged */";
a5ce5211 3893 ec->code += EMBEDDED_FETCH_DEREF_DONE;
5f36109e 3894
f8809d54 3895 fdecl->join (dw.sess);
5f36109e
JS
3896
3897 // Synthesize a functioncall.
3898 functioncall* n = new functioncall;
3899 n->tok = e.tok;
3900 n->function = fname;
5f36109e
JS
3901 n->args.push_back(e.operand);
3902
3903 // Any non-literal indexes need to be passed in too.
3904 for (unsigned i = 0; i < e.components.size(); ++i)
3905 if (e.components[i].type == target_symbol::comp_expression_array_index)
3906 n->args.push_back(e.components[i].expr_index);
3907
3908 result = n;
3909}
c4ce66a1
JS
3910
3911
fb0274bc
JS
3912void dwarf_cast_expanding_visitor::filter_special_modules(string& module)
3913{
d90053e7 3914 // look for "<path/to/header>" or "kernel<path/to/header>"
fb0274bc 3915 // for those cases, build a module including that header
d90053e7 3916 if (module[module.size() - 1] == '>' &&
60d98537 3917 (module[0] == '<' || startswith(module, "kernel<")))
fb0274bc
JS
3918 {
3919 string cached_module;
3920 if (s.use_cache)
3921 {
3922 // see if the cached module exists
a2639cb7 3923 cached_module = find_typequery_hash(s, module);
d105f664 3924 if (!cached_module.empty() && !s.poison_cache)
fb0274bc
JS
3925 {
3926 int fd = open(cached_module.c_str(), O_RDONLY);
3927 if (fd != -1)
3928 {
3929 if (s.verbose > 2)
b530b5b3
LB
3930 //TRANSLATORS: Here we're using a cached module.
3931 clog << _("Pass 2: using cached ") << cached_module << endl;
fb0274bc
JS
3932 module = cached_module;
3933 close(fd);
3934 return;
3935 }
3936 }
3937 }
3938
3939 // no cached module, time to make it
d90053e7 3940 if (make_typequery(s, module) == 0)
fb0274bc 3941 {
e16dc041 3942 // try to save typequery in the cache
fb0274bc 3943 if (s.use_cache)
e16dc041 3944 copy_file(module, cached_module, s.verbose > 2);
fb0274bc
JS
3945 }
3946 }
3947}
3948
3949
c4ce66a1
JS
3950void dwarf_cast_expanding_visitor::visit_cast_op (cast_op* e)
3951{
3952 bool lvalue = is_active_lvalue(e);
3953 if (lvalue && !s.guru_mode)
b3741c9d 3954 throw semantic_error(_("write to @cast context variable not permitted; need stap -g"), e->tok);
c4ce66a1
JS
3955
3956 if (e->module.empty())
3957 e->module = "kernel"; // "*" may also be reasonable to search all kernel modules
3958
5f36109e 3959 functioncall* result = NULL;
8b31197b
JS
3960
3961 // split the module string by ':' for alternatives
3962 vector<string> modules;
3963 tokenize(e->module, modules, ":");
b5a0dd41 3964 bool userspace_p=false; // PR10601
5f36109e 3965 for (unsigned i = 0; !result && i < modules.size(); ++i)
c4ce66a1 3966 {
8b31197b 3967 string& module = modules[i];
fb0274bc 3968 filter_special_modules(module);
abb41d92 3969
c4ce66a1
JS
3970 // NB: This uses '/' to distinguish between kernel modules and userspace,
3971 // which means that userspace modules won't get any PATH searching.
3972 dwflpp* dw;
707bf35e
JS
3973 try
3974 {
b5a0dd41
FCE
3975 userspace_p=is_user_module (module);
3976 if (! userspace_p)
707bf35e
JS
3977 {
3978 // kernel or kernel module target
ae2552da 3979 dw = db.get_kern_dw(s, module);
707bf35e
JS
3980 }
3981 else
3982 {
05fb3e0c 3983 module = find_executable (module, "", s.sysenv); // canonicalize it
707bf35e
JS
3984 dw = db.get_user_dw(s, module);
3985 }
3986 }
3987 catch (const semantic_error& er)
3988 {
3989 /* ignore and go to the next module */
3990 continue;
3991 }
c4ce66a1 3992
5f36109e 3993 dwarf_cast_query q (*dw, module, *e, lvalue, userspace_p, result);
51178501 3994 dw->iterate_over_modules(&query_module, &q);
c4ce66a1 3995 }
abb41d92 3996
5f36109e 3997 if (!result)
c4ce66a1 3998 {
946e1a48
JS
3999 // We pass the unresolved cast_op to the next pass, and hope
4000 // that this value ends up not being referenced after all, so
4001 // it can be optimized out quietly.
c4ce66a1
JS
4002 provide (e);
4003 return;
4004 }
4005
c4ce66a1
JS
4006 if (lvalue)
4007 {
4008 // Provide the functioncall to our parent, so that it can be
4009 // used to substitute for the assignment node immediately above
4010 // us.
4011 assert(!target_symbol_setter_functioncalls.empty());
5f36109e 4012 *(target_symbol_setter_functioncalls.top()) = result;
c4ce66a1
JS
4013 }
4014
5f36109e 4015 result->visit (this);
77de5e9e
GH
4016}
4017
4018
b8da0ad1
FCE
4019void
4020dwarf_derived_probe::printsig (ostream& o) const
4021{
4022 // Instead of just printing the plain locations, we add a PC value
4023 // as a comment as a way of telling e.g. apart multiple inlined
4024 // function instances. This is distinct from the verbose/clog
4025 // output, since this part goes into the cache hash calculations.
4026 sole_location()->print (o);
6d0f3f0c 4027 o << " /* pc=" << section << "+0x" << hex << addr << dec << " */";
b8da0ad1
FCE
4028 printsig_nested (o);
4029}
4030
4031
4032
dc38c0ae 4033void
b20febf3
FCE
4034dwarf_derived_probe::join_group (systemtap_session& s)
4035{
af234c40
JS
4036 // skip probes which are paired entry-handlers
4037 if (!has_return && (saved_longs || saved_strings))
4038 return;
4039
b20febf3
FCE
4040 if (! s.dwarf_derived_probes)
4041 s.dwarf_derived_probes = new dwarf_derived_probe_group ();
4042 s.dwarf_derived_probes->enroll (this);
b642c901 4043
62f52bb0
JS
4044 if (sdt_semaphore_addr != 0)
4045 enable_task_finder(s);
b20febf3
FCE
4046}
4047
4048
2b69faaf
JS
4049static bool
4050kernel_supports_inode_uprobes(systemtap_session& s)
4051{
4052 // The arch-supports is new to the builtin inode-uprobes, so it makes a
4053 // reasonable indicator of the new API. Else we'll need an autoconf...
64e807c2 4054 // see also buildrun.cxx:kernel_built_uprobs()
2b69faaf
JS
4055 return (s.kernel_config["CONFIG_ARCH_SUPPORTS_UPROBES"] == "y"
4056 && s.kernel_config["CONFIG_UPROBES"] == "y");
4057}
4058
4059
5261f7ab
DS
4060void
4061check_process_probe_kernel_support(systemtap_session& s)
4062{
4063 // If we've got utrace, we're good to go.
4064 if (s.kernel_config["CONFIG_UTRACE"] == "y")
4065 return;
4066
8c021542
DS
4067 // We don't have utrace. For process probes that aren't
4068 // uprobes-based, we just need the task_finder. The task_finder
4069 // needs CONFIG_TRACEPOINTS and specific tracepoints (and perhaps
4070 // some CONFIG_FTRACE support). There are specific autoconf tests
4071 // for its needs.
4072 //
4073 // We'll just require CONFIG_TRACEPOINTS here as a quick-and-dirty
4074 // approximation.
4075 if (! s.need_uprobes && s.kernel_config["CONFIG_TRACEPOINTS"] == "y")
4076 return;
4077
d3e959b0
DS
4078 // For uprobes-based process probes, we need the task_finder plus
4079 // the builtin inode-uprobes.
8c021542
DS
4080 if (s.need_uprobes
4081 && s.kernel_config["CONFIG_TRACEPOINTS"] == "y"
d3e959b0 4082 && kernel_supports_inode_uprobes(s))
8c021542
DS
4083 return;
4084
4085 throw semantic_error (_("process probes not available without kernel CONFIG_UTRACE or CONFIG_TRACEPOINTS/CONFIG_ARCH_SUPPORTS_UPROBES/CONFIG_UPROBES"));
5261f7ab
DS
4086}
4087
4088
b20febf3
FCE
4089dwarf_derived_probe::dwarf_derived_probe(const string& funcname,
4090 const string& filename,
4091 int line,
91af0778 4092 // module & section specify a relocation
b20febf3
FCE
4093 // base for <addr>, unless section==""
4094 // (equivalently module=="kernel")
4095 const string& module,
4096 const string& section,
4097 // NB: dwfl_addr is the virtualized
4098 // address for this symbol.
4099 Dwarf_Addr dwfl_addr,
4100 // addr is the section-offset for
4101 // actual relocation.
4102 Dwarf_Addr addr,
4103 dwarf_query& q,
37ebca01 4104 Dwarf_Die* scope_die /* may be null */)
4c5d1300 4105 : derived_probe (q.base_probe, q.base_loc, true /* .components soon rewritten */ ),
b20febf3 4106 module (module), section (section), addr (addr),
63b4fd14 4107 path (q.path),
27dc09b1 4108 has_process (q.has_process),
c9bad430
DS
4109 has_return (q.has_return),
4110 has_maxactive (q.has_maxactive),
c57ea854 4111 has_library (q.has_library),
6b66b9f7 4112 maxactive_val (q.maxactive_val),
b642c901
SC
4113 user_path (q.user_path),
4114 user_lib (q.user_lib),
af234c40 4115 access_vars(false),
c57ea854 4116 saved_longs(0), saved_strings(0),
af234c40 4117 entry_handler(0)
bd2b1e68 4118{
b642c901
SC
4119 if (user_lib.size() != 0)
4120 has_library = true;
4121
6b66b9f7
JS
4122 if (q.has_process)
4123 {
4124 // We may receive probes on two types of ELF objects: ET_EXEC or ET_DYN.
4125 // ET_EXEC ones need no further relocation on the addr(==dwfl_addr), whereas
4126 // ET_DYN ones do (addr += run-time mmap base address). We tell these apart
4127 // by the incoming section value (".absolute" vs. ".dynamic").
4128 // XXX Assert invariants here too?
2b69faaf
JS
4129
4130 // inode-uprobes needs an offset rather than an absolute VM address.
4131 if (kernel_supports_inode_uprobes(q.dw.sess) &&
4132 section == ".absolute" && addr == dwfl_addr &&
4133 addr >= q.dw.module_start && addr < q.dw.module_end)
4134 this->addr = addr - q.dw.module_start;
6b66b9f7
JS
4135 }
4136 else
4137 {
4138 // Assert kernel relocation invariants
4139 if (section == "" && dwfl_addr != addr) // addr should be absolute
ce0f6648 4140 throw semantic_error (_("missing relocation basis"), tok);
6b66b9f7 4141 if (section != "" && dwfl_addr == addr) // addr should be an offset
b530b5b3 4142 throw semantic_error (_("inconsistent relocation address"), tok);
6b66b9f7 4143 }
2930abc7 4144
21beacc9
FCE
4145 // XXX: hack for strange g++/gcc's
4146#ifndef USHRT_MAX
4147#define USHRT_MAX 32767
4148#endif
4149
606fd9c8 4150 // Range limit maxactive() value
6b66b9f7 4151 if (has_maxactive && (maxactive_val < 0 || maxactive_val > USHRT_MAX))
b530b5b3
LB
4152 throw semantic_error (_F("maxactive value out of range [0,%s]",
4153 lex_cast(USHRT_MAX).c_str()), q.base_loc->components.front()->tok);
606fd9c8 4154
de688825 4155 // Expand target variables in the probe body
5f0a03a6 4156 if (!null_die(scope_die))
8fc05e57 4157 {
6b66b9f7 4158 // XXX: user-space deref's for q.has_process!
de688825 4159 dwarf_var_expanding_visitor v (q, scope_die, dwfl_addr);
8b095b45 4160 v.replace (this->body);
6b66b9f7
JS
4161 if (!q.has_process)
4162 access_vars = v.visited;
37ebca01
FCE
4163
4164 // If during target-variable-expanding the probe, we added a new block
4165 // of code, add it to the start of the probe.
4166 if (v.add_block)
ba6f838d 4167 this->body = new block(v.add_block, this->body);
2260f4e3
FCE
4168
4169 // If when target-variable-expanding the probe, we need to synthesize a
4170 // sibling function-entry probe. We don't go through the whole probe derivation
4171 // business (PR10642) that could lead to wildcard/alias resolution, or for that
4172 // dwarf-induced duplication.
4173 if (v.add_call_probe)
37ebca01 4174 {
2260f4e3
FCE
4175 assert (q.has_return && !q.has_call);
4176
4177 // We temporarily replace q.base_probe.
4178 statement* old_body = q.base_probe->body;
4179 q.base_probe->body = v.add_call_probe;
4180 q.has_return = false;
4181 q.has_call = true;
af234c40 4182
da23eceb 4183 if (q.has_process)
af234c40
JS
4184 entry_handler = new uprobe_derived_probe (funcname, filename, line,
4185 module, section, dwfl_addr,
4186 addr, q, scope_die);
da23eceb 4187 else
af234c40
JS
4188 entry_handler = new dwarf_derived_probe (funcname, filename, line,
4189 module, section, dwfl_addr,
4190 addr, q, scope_die);
4191
4192 saved_longs = entry_handler->saved_longs = v.saved_longs;
4193 saved_strings = entry_handler->saved_strings = v.saved_strings;
4194
4195 q.results.push_back (entry_handler);
2260f4e3
FCE
4196
4197 q.has_return = true;
4198 q.has_call = false;
4199 q.base_probe->body = old_body;
37ebca01 4200 }
f10534c6
WH
4201 // Save the local variables for listing mode
4202 if (q.sess.listing_mode_vars)
8c67c337 4203 saveargs(q, scope_die, dwfl_addr);
8fc05e57 4204 }
37ebca01 4205 // else - null scope_die - $target variables will produce an error during translate phase
8fc05e57 4206
f10534c6 4207 // PR10820: null scope die, local variables aren't accessible, not necessary to invoke saveargs
0a98fd42 4208
5d23847d 4209 // Reset the sole element of the "locations" vector as a
b20febf3
FCE
4210 // "reverse-engineered" form of the incoming (q.base_loc) probe
4211 // point. This allows a user to see what function / file / line
4212 // number any particular match of the wildcards.
2930abc7 4213
a229fcd7 4214 vector<probe_point::component*> comps;
91af0778
FCE
4215 if (q.has_kernel)
4216 comps.push_back (new probe_point::component(TOK_KERNEL));
4217 else if(q.has_module)
4218 comps.push_back (new probe_point::component(TOK_MODULE, new literal_string(module)));
4219 else if(q.has_process)
4220 comps.push_back (new probe_point::component(TOK_PROCESS, new literal_string(module)));
4221 else
4222 assert (0);
b5d77020 4223
db520b00
FCE
4224 string fn_or_stmt;
4225 if (q.has_function_str || q.has_function_num)
4226 fn_or_stmt = "function";
4227 else
4228 fn_or_stmt = "statement";
a229fcd7 4229
b8da0ad1 4230 if (q.has_function_str || q.has_statement_str)
db520b00 4231 {
4cd232e4 4232 string retro_name = funcname;
b20febf3 4233 if (filename != "")
cee35f73 4234 {
fb84c077 4235 retro_name += ("@" + string (filename));
cee35f73 4236 if (line > 0)
aca66a36 4237 retro_name += (":" + lex_cast (line));
cee35f73 4238 }
db520b00
FCE
4239 comps.push_back
4240 (new probe_point::component
4241 (fn_or_stmt, new literal_string (retro_name)));
4242 }
b8da0ad1 4243 else if (q.has_function_num || q.has_statement_num)
db520b00
FCE
4244 {
4245 Dwarf_Addr retro_addr;
4246 if (q.has_function_num)
4247 retro_addr = q.function_num_val;
4248 else
4249 retro_addr = q.statement_num_val;
db520b00
FCE
4250 comps.push_back (new probe_point::component
4251 (fn_or_stmt,
9ea68eb9 4252 new literal_number(retro_addr, true)));
37ebca01
FCE
4253
4254 if (q.has_absolute)
4255 comps.push_back (new probe_point::component (TOK_ABSOLUTE));
a229fcd7
GH
4256 }
4257
b8da0ad1
FCE
4258 if (q.has_call)
4259 comps.push_back (new probe_point::component(TOK_CALL));
4bda987e
SC
4260 if (q.has_exported)
4261 comps.push_back (new probe_point::component(TOK_EXPORTED));
b8da0ad1
FCE
4262 if (q.has_inline)
4263 comps.push_back (new probe_point::component(TOK_INLINE));
db520b00 4264 if (has_return)
b8da0ad1
FCE
4265 comps.push_back (new probe_point::component(TOK_RETURN));
4266 if (has_maxactive)
4267 comps.push_back (new probe_point::component
4268 (TOK_MAXACTIVE, new literal_number(maxactive_val)));
d9b516ca 4269
5d23847d
FCE
4270 // Overwrite it.
4271 this->sole_location()->components = comps;
2930abc7
FCE
4272}
4273
bd2b1e68 4274
0a98fd42 4275void
8c67c337
JS
4276dwarf_derived_probe::saveargs(dwarf_query& q, Dwarf_Die* scope_die,
4277 Dwarf_Addr dwfl_addr)
0a98fd42 4278{
9aa8ffce 4279 if (null_die(scope_die))
0a98fd42 4280 return;
0a98fd42 4281
8c67c337 4282 bool verbose = q.sess.verbose > 2;
0a98fd42 4283
8c67c337 4284 if (verbose)
b530b5b3 4285 clog << _F("saveargs: examining '%s' (dieoffset: %#" PRIx64 ")\n", (dwarf_diename(scope_die)?: "unknown"), dwarf_dieoffset(scope_die));
0a98fd42 4286
8c67c337
JS
4287 if (has_return)
4288 {
4289 /* Only save the return value if it has a type. */
4290 string type_name;
4291 Dwarf_Die type_die;
4292 if (dwarf_attr_die (scope_die, DW_AT_type, &type_die) &&
4293 dwarf_type_name(&type_die, type_name))
4294 args.push_back("$return:"+type_name);
4295
4296 else if (verbose)
b530b5b3
LB
4297 clog << _F("saveargs: failed to retrieve type name for return value (dieoffset: %s)\n",
4298 lex_cast_hex(dwarf_dieoffset(scope_die)).c_str());
8c67c337 4299 }
d87623a1 4300
0a98fd42 4301 Dwarf_Die arg;
4ef35696
JS
4302 vector<Dwarf_Die> scopes = q.dw.getscopes(scope_die);
4303 for (unsigned i = 0; i < scopes.size(); ++i)
4304 {
4305 if (dwarf_tag(&scopes[i]) == DW_TAG_compile_unit)
4306 break; // we don't want file-level variables
4307 if (dwarf_child (&scopes[i], &arg) == 0)
4308 do
0a98fd42 4309 {
4ef35696
JS
4310 switch (dwarf_tag (&arg))
4311 {
4312 case DW_TAG_variable:
4313 case DW_TAG_formal_parameter:
4314 break;
0a98fd42 4315
4ef35696
JS
4316 default:
4317 continue;
4318 }
0a98fd42 4319
4ef35696
JS
4320 /* Ignore this local if it has no name. */
4321 const char *arg_name = dwarf_diename (&arg);
4322 if (!arg_name)
8c67c337
JS
4323 {
4324 if (verbose)
b530b5b3
LB
4325 clog << _F("saveargs: failed to retrieve name for local (dieoffset: %s)\n",
4326 lex_cast_hex(dwarf_dieoffset(&arg)).c_str());
8c67c337
JS
4327 continue;
4328 }
4ef35696
JS
4329
4330 if (verbose)
b530b5b3
LB
4331 clog << _F("saveargs: finding location for local '%s' (dieoffset: %s)\n",
4332 arg_name, lex_cast_hex(dwarf_dieoffset(&arg)).c_str());
4ef35696
JS
4333
4334 /* Ignore this local if it has no location (or not at this PC). */
4335 /* NB: It still may not be directly accessible, e.g. if it is an
4336 * aggregate type, implicit_pointer, etc., but the user can later
4337 * figure out how to access the interesting parts. */
4338 Dwarf_Attribute attr_mem;
4339 if (!dwarf_attr_integrate (&arg, DW_AT_const_value, &attr_mem))
4340 {
4341 Dwarf_Op *expr;
4342 size_t len;
4343 if (!dwarf_attr_integrate (&arg, DW_AT_location, &attr_mem))
4344 {
4345 if (verbose)
b530b5b3
LB
4346 clog << _F("saveargs: failed to resolve the location for local '%s' (dieoffset: %s)\n",
4347 arg_name, lex_cast_hex(dwarf_dieoffset(&arg)).c_str());
4ef35696
JS
4348 continue;
4349 }
4350 else if (!(dwarf_getlocation_addr(&attr_mem, dwfl_addr, &expr,
4351 &len, 1) == 1 && len > 0))
4352 {
4353 if (verbose)
b530b5b3
LB
4354 clog << _F("saveargs: local '%s' (dieoffset: %s) is not available at this address (%s)\n",
4355 arg_name, lex_cast_hex(dwarf_dieoffset(&arg)).c_str(), lex_cast_hex(dwfl_addr).c_str());
4ef35696
JS
4356 continue;
4357 }
4358 }
4359
4360 /* Ignore this local if it has no type. */
4361 string type_name;
4362 Dwarf_Die type_die;
4363 if (!dwarf_attr_die (&arg, DW_AT_type, &type_die) ||
4364 !dwarf_type_name(&type_die, type_name))
8c67c337
JS
4365 {
4366 if (verbose)
b530b5b3
LB
4367 clog << _F("saveargs: failed to retrieve type name for local '%s' (dieoffset: %s)\n",
4368 arg_name, lex_cast_hex(dwarf_dieoffset(&arg)).c_str());
8c67c337
JS
4369 continue;
4370 }
8c67c337 4371
4ef35696
JS
4372 /* This local looks good -- save it! */
4373 args.push_back("$"+string(arg_name)+":"+type_name);
8c67c337 4374 }
4ef35696
JS
4375 while (dwarf_siblingof (&arg, &arg) == 0);
4376 }
0a98fd42
JS
4377}
4378
4379
4380void
d0bfd2ac 4381dwarf_derived_probe::getargs(std::list<std::string> &arg_set) const
0a98fd42 4382{
d0bfd2ac 4383 arg_set.insert(arg_set.end(), args.begin(), args.end());
0a98fd42
JS
4384}
4385
4386
27dc09b1 4387void
42e38653 4388dwarf_derived_probe::emit_privilege_assertion (translator_output* o)
27dc09b1
DB
4389{
4390 if (has_process)
4391 {
4392 // These probes are allowed for unprivileged users, but only in the
4393 // context of processes which they own.
4394 emit_process_owner_assertion (o);
4395 return;
4396 }
4397
4398 // Other probes must contain the default assertion which aborts
4399 // if executed by an unprivileged user.
42e38653 4400 derived_probe::emit_privilege_assertion (o);
27dc09b1
DB
4401}
4402
4403
4404void
4405dwarf_derived_probe::print_dupe_stamp(ostream& o)
4406{
4407 if (has_process)
4408 {
4409 // These probes are allowed for unprivileged users, but only in the
4410 // context of processes which they own.
4411 print_dupe_stamp_unprivileged_process_owner (o);
4412 return;
4413 }
4414
4415 // Other probes must contain the default dupe stamp
4416 derived_probe::print_dupe_stamp (o);
4417}
4418
64211010 4419
7a053d3b 4420void
20c6c071 4421dwarf_derived_probe::register_statement_variants(match_node * root,
27dc09b1 4422 dwarf_builder * dw,
42e38653 4423 privilege_t privilege)
bd2b1e68 4424{
27dc09b1 4425 root
42e38653 4426 ->bind_privilege(privilege)
27dc09b1 4427 ->bind(dw);
54efe513
GH
4428}
4429
7a053d3b 4430void
fd6602a0 4431dwarf_derived_probe::register_function_variants(match_node * root,
27dc09b1 4432 dwarf_builder * dw,
42e38653 4433 privilege_t privilege)
2865d17a 4434{
27dc09b1 4435 root
42e38653 4436 ->bind_privilege(privilege)
27dc09b1 4437 ->bind(dw);
27dc09b1 4438 root->bind(TOK_CALL)
42e38653 4439 ->bind_privilege(privilege)
27dc09b1 4440 ->bind(dw);
4bda987e
SC
4441 root->bind(TOK_EXPORTED)
4442 ->bind_privilege(privilege)
4443 ->bind(dw);
27dc09b1 4444 root->bind(TOK_RETURN)
42e38653 4445 ->bind_privilege(privilege)
27dc09b1 4446 ->bind(dw);
1e035395 4447
f6be7c06
DB
4448 // For process probes / uprobes, .maxactive() is unused.
4449 if (! pr_contains (privilege, pr_stapusr))
1e035395
FCE
4450 {
4451 root->bind(TOK_RETURN)
1e035395
FCE
4452 ->bind_num(TOK_MAXACTIVE)->bind(dw);
4453 }
bd2b1e68
GH
4454}
4455
7a053d3b 4456void
27dc09b1 4457dwarf_derived_probe::register_function_and_statement_variants(
440d9b00 4458 systemtap_session& s,
27dc09b1
DB
4459 match_node * root,
4460 dwarf_builder * dw,
42e38653 4461 privilege_t privilege
27dc09b1 4462)
bd2b1e68
GH
4463{
4464 // Here we match 4 forms:
4465 //
4466 // .function("foo")
4467 // .function(0xdeadbeef)
4468 // .statement("foo")
4469 // .statement(0xdeadbeef)
4470
440d9b00 4471 match_node *fv_root = root->bind_str(TOK_FUNCTION);
42e38653 4472 register_function_variants(fv_root, dw, privilege);
7f02ca94 4473 // ROOT.function("STRING") always gets the .inline and .label variants.
440d9b00 4474 fv_root->bind(TOK_INLINE)
42e38653 4475 ->bind_privilege(privilege)
440d9b00 4476 ->bind(dw);
7f02ca94
JS
4477 fv_root->bind_str(TOK_LABEL)
4478 ->bind_privilege(privilege)
440d9b00
DB
4479 ->bind(dw);
4480
4481 fv_root = root->bind_num(TOK_FUNCTION);
42e38653 4482 register_function_variants(fv_root, dw, privilege);
440d9b00
DB
4483 // ROOT.function(NUMBER).inline is deprecated in release 1.7 and removed thereafter.
4484 if (strverscmp(s.compatible.c_str(), "1.7") <= 0)
4485 {
4486 fv_root->bind(TOK_INLINE)
42e38653 4487 ->bind_privilege(privilege)
440d9b00
DB
4488 ->bind(dw);
4489 }
4490
42e38653
DB
4491 register_statement_variants(root->bind_str(TOK_STATEMENT), dw, privilege);
4492 register_statement_variants(root->bind_num(TOK_STATEMENT), dw, privilege);
bd2b1e68
GH
4493}
4494
b1615c74
JS
4495void
4496dwarf_derived_probe::register_sdt_variants(systemtap_session& s,
4497 match_node * root,
4498 dwarf_builder * dw)
4499{
4500 root->bind_str(TOK_MARK)
f66bb29a 4501 ->bind_privilege(pr_all)
b1615c74
JS
4502 ->bind(dw);
4503 root->bind_str(TOK_PROVIDER)->bind_str(TOK_MARK)
f66bb29a 4504 ->bind_privilege(pr_all)
b1615c74
JS
4505 ->bind(dw);
4506}
4507
4508void
4509dwarf_derived_probe::register_plt_variants(systemtap_session& s,
4510 match_node * root,
4511 dwarf_builder * dw)
4512{
4513 root->bind(TOK_PLT)
f66bb29a 4514 ->bind_privilege(pr_all)
b1615c74
JS
4515 ->bind(dw);
4516 root->bind_str(TOK_PLT)
f66bb29a 4517 ->bind_privilege(pr_all)
b1615c74
JS
4518 ->bind(dw);
4519 root->bind(TOK_PLT)->bind_num(TOK_STATEMENT)
f66bb29a 4520 ->bind_privilege(pr_all)
b1615c74
JS
4521 ->bind(dw);
4522 root->bind_str(TOK_PLT)->bind_num(TOK_STATEMENT)
f66bb29a 4523 ->bind_privilege(pr_all)
b1615c74 4524 ->bind(dw);
bd2b1e68
GH
4525}
4526
4527void
c4ce66a1 4528dwarf_derived_probe::register_patterns(systemtap_session& s)
bd2b1e68 4529{
c4ce66a1 4530 match_node* root = s.pattern_root;
bd2b1e68
GH
4531 dwarf_builder *dw = new dwarf_builder();
4532
c4ce66a1
JS
4533 update_visitor *filter = new dwarf_cast_expanding_visitor(s, *dw);
4534 s.code_filters.push_back(filter);
4535
73f52eb4
DB
4536 register_function_and_statement_variants(s, root->bind(TOK_KERNEL), dw, pr_privileged);
4537 register_function_and_statement_variants(s, root->bind_str(TOK_MODULE), dw, pr_privileged);
27dc09b1
DB
4538 root->bind(TOK_KERNEL)->bind_num(TOK_STATEMENT)->bind(TOK_ABSOLUTE)
4539 ->bind(dw);
2cab6244 4540
7f02ca94
JS
4541 match_node* uprobes[] = {
4542 root->bind(TOK_PROCESS),
4543 root->bind_str(TOK_PROCESS),
4544 root->bind(TOK_PROCESS)->bind_str(TOK_LIBRARY),
4545 root->bind_str(TOK_PROCESS)->bind_str(TOK_LIBRARY),
4546 };
4547 for (size_t i = 0; i < sizeof(uprobes) / sizeof(*uprobes); ++i)
4548 {
f66bb29a 4549 register_function_and_statement_variants(s, uprobes[i], dw, pr_all);
7f02ca94
JS
4550 register_sdt_variants(s, uprobes[i], dw);
4551 register_plt_variants(s, uprobes[i], dw);
4552 }
bd2b1e68
GH
4553}
4554
9020300d
FCE
4555void
4556dwarf_derived_probe::emit_probe_local_init(translator_output * o)
4557{
b95e2b79
MH
4558 if (access_vars)
4559 {
4560 // if accessing $variables, emit bsp cache setup for speeding up
d4670309 4561 o->newline() << "#if defined __ia64__";
d9aed31e 4562 o->newline() << "bspcache(c->unwaddr, c->kregs);";
d4670309 4563 o->newline() << "#endif";
b95e2b79 4564 }
9020300d 4565}
2930abc7 4566
b20febf3 4567// ------------------------------------------------------------------------
46b84a80
DS
4568
4569void
b20febf3 4570dwarf_derived_probe_group::enroll (dwarf_derived_probe* p)
46b84a80 4571{
62f52bb0
JS
4572 if (p->sdt_semaphore_addr != 0)
4573 has_semaphores = true;
b20febf3 4574 probes_by_module.insert (make_pair (p->module, p));
b8da0ad1
FCE
4575
4576 // XXX: probes put at the same address should all share a
4577 // single kprobe/kretprobe, and have their handlers executed
4578 // sequentially.
b55bc428
FCE
4579}
4580
7a053d3b 4581void
775d51e5 4582dwarf_derived_probe_group::emit_module_decls (systemtap_session& s)
ec4373ff 4583{
b20febf3 4584 if (probes_by_module.empty()) return;
2930abc7 4585
775d51e5
DS
4586 s.op->newline() << "/* ---- dwarf probes ---- */";
4587
4588 // Warn of misconfigured kernels
f41595cc
FCE
4589 s.op->newline() << "#if ! defined(CONFIG_KPROBES)";
4590 s.op->newline() << "#error \"Need CONFIG_KPROBES!\"";
4591 s.op->newline() << "#endif";
775d51e5 4592 s.op->newline();
f41595cc 4593
f07c3b68 4594 s.op->newline() << "#ifndef KRETACTIVE";
1ee6b5fc 4595 s.op->newline() << "#define KRETACTIVE (max(15,6*(int)num_possible_cpus()))";
f07c3b68
FCE
4596 s.op->newline() << "#endif";
4597
14cf7e42
SC
4598 // Forward decls
4599 s.op->newline() << "#include \"kprobes-common.h\"";
4600
b20febf3
FCE
4601 // Forward declare the master entry functions
4602 s.op->newline() << "static int enter_kprobe_probe (struct kprobe *inst,";
4603 s.op->line() << " struct pt_regs *regs);";
4604 s.op->newline() << "static int enter_kretprobe_probe (struct kretprobe_instance *inst,";
4605 s.op->line() << " struct pt_regs *regs);";
4606
42cb22bd
MH
4607 // Emit an array of kprobe/kretprobe pointers
4608 s.op->newline() << "#if defined(STAPCONF_UNREGISTER_KPROBES)";
4609 s.op->newline() << "static void * stap_unreg_kprobes[" << probes_by_module.size() << "];";
4610 s.op->newline() << "#endif";
4611
b20febf3 4612 // Emit the actual probe list.
606fd9c8
FCE
4613
4614 // NB: we used to plop a union { struct kprobe; struct kretprobe } into
4615 // struct stap_dwarf_probe, but it being initialized data makes it add
4616 // hundreds of bytes of padding per stap_dwarf_probe. (PR5673)
14cf7e42 4617 s.op->newline() << "static struct stap_dwarf_kprobe stap_dwarf_kprobes[" << probes_by_module.size() << "];";
606fd9c8
FCE
4618 // NB: bss!
4619
4c2732a1 4620 s.op->newline() << "static struct stap_dwarf_probe {";
b0986e7a
DS
4621 s.op->newline(1) << "const unsigned return_p:1;";
4622 s.op->newline() << "const unsigned maxactive_p:1;";
b350f56b 4623 s.op->newline() << "const unsigned optional_p:1;";
b20febf3 4624 s.op->newline() << "unsigned registered_p:1;";
b0986e7a 4625 s.op->newline() << "const unsigned short maxactive_val;";
606fd9c8 4626
af234c40
JS
4627 // data saved in the kretprobe_instance packet
4628 s.op->newline() << "const unsigned short saved_longs;";
4629 s.op->newline() << "const unsigned short saved_strings;";
4630
faea5e16 4631 // Let's find some stats for the embedded strings. Maybe they
606fd9c8
FCE
4632 // are small and uniform enough to justify putting char[MAX]'s into
4633 // the array instead of relocated char*'s.
faea5e16
JS
4634 size_t module_name_max = 0, section_name_max = 0;
4635 size_t module_name_tot = 0, section_name_tot = 0;
606fd9c8
FCE
4636 size_t all_name_cnt = probes_by_module.size(); // for average
4637 for (p_b_m_iterator it = probes_by_module.begin(); it != probes_by_module.end(); it++)
4638 {
4639 dwarf_derived_probe* p = it->second;
4640#define DOIT(var,expr) do { \
4641 size_t var##_size = (expr) + 1; \
4642 var##_max = max (var##_max, var##_size); \
4643 var##_tot += var##_size; } while (0)
4644 DOIT(module_name, p->module.size());
4645 DOIT(section_name, p->section.size());
606fd9c8
FCE
4646#undef DOIT
4647 }
4648
4649 // Decide whether it's worthwhile to use char[] or char* by comparing
4650 // the amount of average waste (max - avg) to the relocation data size
4651 // (3 native long words).
4652#define CALCIT(var) \
4653 if ((var##_name_max-(var##_name_tot/all_name_cnt)) < (3 * sizeof(void*))) \
4654 { \
4655 s.op->newline() << "const char " << #var << "[" << var##_name_max << "];"; \
4656 if (s.verbose > 2) clog << "stap_dwarf_probe " << #var \
4657 << "[" << var##_name_max << "]" << endl; \
4658 } \
4659 else \
4660 { \
b0986e7a 4661 s.op->newline() << "const char * const " << #var << ";"; \
606fd9c8
FCE
4662 if (s.verbose > 2) clog << "stap_dwarf_probe *" << #var << endl; \
4663 }
4664
4665 CALCIT(module);
4666 CALCIT(section);
e6fe60e7 4667#undef CALCIT
606fd9c8 4668
b0986e7a 4669 s.op->newline() << "const unsigned long address;";
26e63673 4670 s.op->newline() << "struct stap_probe * const probe;";
c87ae2c1 4671 s.op->newline() << "struct stap_probe * const entry_probe;";
62f52bb0
JS
4672 if (has_semaphores)
4673 {
4674 s.op->newline() << "const unsigned long sdt_sem_offset;";
4675 s.op->newline() << "unsigned long sdt_sem_address;";
4676 s.op->newline() << "struct task_struct *tsk;";
4677 s.op->newline() << "const char *pathname;";
4678 s.op->newline() << "struct stap_task_finder_target finder;";
4679 }
b20febf3
FCE
4680 s.op->newline(-1) << "} stap_dwarf_probes[] = {";
4681 s.op->indent(1);
4682
4683 for (p_b_m_iterator it = probes_by_module.begin(); it != probes_by_module.end(); it++)
2930abc7 4684 {
b20febf3
FCE
4685 dwarf_derived_probe* p = it->second;
4686 s.op->newline() << "{";
4687 if (p->has_return)
4688 s.op->line() << " .return_p=1,";
c9bad430 4689 if (p->has_maxactive)
606fd9c8
FCE
4690 {
4691 s.op->line() << " .maxactive_p=1,";
4692 assert (p->maxactive_val >= 0 && p->maxactive_val <= USHRT_MAX);
4693 s.op->line() << " .maxactive_val=" << p->maxactive_val << ",";
4694 }
af234c40
JS
4695 if (p->saved_longs || p->saved_strings)
4696 {
4697 if (p->saved_longs)
4698 s.op->line() << " .saved_longs=" << p->saved_longs << ",";
4699 if (p->saved_strings)
4700 s.op->line() << " .saved_strings=" << p->saved_strings << ",";
4701 if (p->entry_handler)
c87ae2c1 4702 s.op->line() << " .entry_probe=" << common_probe_init (p->entry_handler) << ",";
af234c40 4703 }
b350f56b
JS
4704 if (p->locations[0]->optional)
4705 s.op->line() << " .optional_p=1,";
dc38c256 4706 s.op->line() << " .address=(unsigned long)0x" << hex << p->addr << dec << "ULL,";
84048984
FCE
4707 s.op->line() << " .module=\"" << p->module << "\",";
4708 s.op->line() << " .section=\"" << p->section << "\",";
faea5e16 4709 s.op->line() << " .probe=" << common_probe_init (p) << ",";
b642c901
SC
4710 if (p->sdt_semaphore_addr != 0)
4711 {
4712 s.op->line() << " .sdt_sem_offset=(unsigned long)0x" << hex << p->sdt_semaphore_addr << dec << "ULL,";
4713 s.op->line() << " .sdt_sem_address=0,";
4714 if (p->has_library)
4715 {
4716 s.op->line() << " .finder={";
4717 s.op->line() << " .pid=0,";
4718 s.op->line() << " .procname=\"" << p->user_path << "\",";
4719 s.op->line() << " .mmap_callback=&stap_kprobe_mmap_found,";
4720 s.op->line() << " },";
4721 s.op->line() << " .pathname=\"" << p->user_lib << "\",";
4722 }
4723 else
4724 {
4725 s.op->line() << " .finder={";
4726 s.op->line() << " .pid=0,";
4727 s.op->line() << " .procname=\"" << p->user_path << "\",";
4728 s.op->line() << " .callback=&stap_kprobe_process_found,";
4729 s.op->line() << " },";
4730 s.op->line() << " .pathname=\"" << p->user_path << "\",";
4731 }
4732
4733 }
b642c901 4734
b20febf3 4735 s.op->line() << " },";
2930abc7 4736 }
2930abc7 4737
b20febf3
FCE
4738 s.op->newline(-1) << "};";
4739
4740 // Emit the kprobes callback function
4741 s.op->newline();
4742 s.op->newline() << "static int enter_kprobe_probe (struct kprobe *inst,";
4743 s.op->line() << " struct pt_regs *regs) {";
606fd9c8
FCE
4744 // NB: as of PR5673, the kprobe|kretprobe union struct is in BSS
4745 s.op->newline(1) << "int kprobe_idx = ((uintptr_t)inst-(uintptr_t)stap_dwarf_kprobes)/sizeof(struct stap_dwarf_kprobe);";
4746 // Check that the index is plausible
4747 s.op->newline() << "struct stap_dwarf_probe *sdp = &stap_dwarf_probes[";
4748 s.op->line() << "((kprobe_idx >= 0 && kprobe_idx < " << probes_by_module.size() << ")?";
4749 s.op->line() << "kprobe_idx:0)"; // NB: at least we avoid memory corruption
4750 // XXX: it would be nice to give a more verbose error though; BUG_ON later?
4751 s.op->line() << "];";
6eefe942
MW
4752 common_probe_entryfn_prologue (s.op, "STAP_SESSION_RUNNING", "sdp->probe",
4753 "_STP_PROBE_HANDLER_KPROBE");
d9aed31e 4754 s.op->newline() << "c->kregs = regs;";
6415ddde
MW
4755
4756 // Make it look like the IP is set as it wouldn't have been replaced
4757 // by a breakpoint instruction when calling real probe handler. Reset
4758 // IP regs on return, so we don't confuse kprobes. PR10458
4759 s.op->newline() << "{";
4760 s.op->indent(1);
d9aed31e 4761 s.op->newline() << "unsigned long kprobes_ip = REG_IP(c->kregs);";
259d54c0 4762 s.op->newline() << "SET_REG_IP(regs, (unsigned long) inst->addr);";
26e63673 4763 s.op->newline() << "(*sdp->probe->ph) (c);";
259d54c0 4764 s.op->newline() << "SET_REG_IP(regs, kprobes_ip);";
6415ddde
MW
4765 s.op->newline(-1) << "}";
4766
7baf48e9 4767 common_probe_entryfn_epilogue (s.op, true, s.suppress_handler_errors);
b20febf3
FCE
4768 s.op->newline() << "return 0;";
4769 s.op->newline(-1) << "}";
4770
4771 // Same for kretprobes
4772 s.op->newline();
af234c40
JS
4773 s.op->newline() << "static int enter_kretprobe_common (struct kretprobe_instance *inst,";
4774 s.op->line() << " struct pt_regs *regs, int entry) {";
b20febf3 4775 s.op->newline(1) << "struct kretprobe *krp = inst->rp;";
606fd9c8
FCE
4776
4777 // NB: as of PR5673, the kprobe|kretprobe union struct is in BSS
a36378d7 4778 s.op->newline() << "int kprobe_idx = ((uintptr_t)krp-(uintptr_t)stap_dwarf_kprobes)/sizeof(struct stap_dwarf_kprobe);";
606fd9c8
FCE
4779 // Check that the index is plausible
4780 s.op->newline() << "struct stap_dwarf_probe *sdp = &stap_dwarf_probes[";
4781 s.op->line() << "((kprobe_idx >= 0 && kprobe_idx < " << probes_by_module.size() << ")?";
4782 s.op->line() << "kprobe_idx:0)"; // NB: at least we avoid memory corruption
4783 // XXX: it would be nice to give a more verbose error though; BUG_ON later?
4784 s.op->line() << "];";
4785
c87ae2c1
JS
4786 s.op->newline() << "struct stap_probe *sp = entry ? sdp->entry_probe : sdp->probe;";
4787 s.op->newline() << "if (sp) {";
4788 s.op->indent(1);
6eefe942
MW
4789 common_probe_entryfn_prologue (s.op, "STAP_SESSION_RUNNING", "sp",
4790 "_STP_PROBE_HANDLER_KRETPROBE");
d9aed31e 4791 s.op->newline() << "c->kregs = regs;";
af234c40
JS
4792
4793 // for assisting runtime's backtrace logic and accessing kretprobe data packets
6dceb5c9
MW
4794 s.op->newline() << "c->ips.krp.pi = inst;";
4795 s.op->newline() << "c->ips.krp.pi_longs = sdp->saved_longs;";
6415ddde
MW
4796
4797 // Make it look like the IP is set as it wouldn't have been replaced
4798 // by a breakpoint instruction when calling real probe handler. Reset
4799 // IP regs on return, so we don't confuse kprobes. PR10458
4800 s.op->newline() << "{";
d9aed31e 4801 s.op->newline(1) << "unsigned long kprobes_ip = REG_IP(c->kregs);";
c87ae2c1
JS
4802 s.op->newline() << "if (entry)";
4803 s.op->newline(1) << "SET_REG_IP(regs, (unsigned long) inst->rp->kp.addr);";
4804 s.op->newline(-1) << "else";
4805 s.op->newline(1) << "SET_REG_IP(regs, (unsigned long)inst->ret_addr);";
4806 s.op->newline(-1) << "(sp->ph) (c);";
259d54c0 4807 s.op->newline() << "SET_REG_IP(regs, kprobes_ip);";
6415ddde
MW
4808 s.op->newline(-1) << "}";
4809
7baf48e9 4810 common_probe_entryfn_epilogue (s.op, true, s.suppress_handler_errors);
c87ae2c1 4811 s.op->newline(-1) << "}";
b20febf3
FCE
4812 s.op->newline() << "return 0;";
4813 s.op->newline(-1) << "}";
af234c40
JS
4814
4815 s.op->newline();
4816 s.op->newline() << "static int enter_kretprobe_probe (struct kretprobe_instance *inst,";
4817 s.op->line() << " struct pt_regs *regs) {";
4818 s.op->newline(1) << "return enter_kretprobe_common(inst, regs, 0);";
4819 s.op->newline(-1) << "}";
4820
4821 s.op->newline();
4822 s.op->newline() << "static int enter_kretprobe_entry_probe (struct kretprobe_instance *inst,";
4823 s.op->line() << " struct pt_regs *regs) {";
4824 s.op->newline(1) << "return enter_kretprobe_common(inst, regs, 1);";
4825 s.op->newline(-1) << "}";
b642c901 4826
14cf7e42 4827 s.op->newline();
62f52bb0
JS
4828 if (has_semaphores)
4829 s.op->newline() << "#define KPROBES_TASK_FINDER 1";
14cf7e42
SC
4830 s.op->newline() << "#include \"kprobes-common.c\"";
4831 s.op->newline();
20c6c071 4832}
ec4373ff 4833
20c6c071 4834
dc38c0ae 4835void
b20febf3
FCE
4836dwarf_derived_probe_group::emit_module_init (systemtap_session& s)
4837{
62f52bb0 4838 if (has_semaphores) // Ignore if there are no semaphores
b642c901
SC
4839 {
4840 s.op->newline() << "for (i=0; i<ARRAY_SIZE(stap_dwarf_probes); i++) {";
4841 s.op->newline(1) << "int rc;";
4842 s.op->newline() << "struct stap_dwarf_probe *p = &stap_dwarf_probes[i];";
26e63673 4843 s.op->newline() << "probe_point = p->probe->pp;"; // for error messages
b642c901
SC
4844 s.op->newline() << "if (p->sdt_sem_offset) {";
4845 s.op->newline(1) << "rc = stap_register_task_finder_target(&p->finder);";
4846 s.op->newline(-1) << "}";
4847 s.op->newline() << "if (rc) break;";
4848 s.op->newline(-1) << "}";
4849 }
4850
b20febf3
FCE
4851 s.op->newline() << "for (i=0; i<" << probes_by_module.size() << "; i++) {";
4852 s.op->newline(1) << "struct stap_dwarf_probe *sdp = & stap_dwarf_probes[i];";
a36378d7 4853 s.op->newline() << "struct stap_dwarf_kprobe *kp = & stap_dwarf_kprobes[i];";
a049e342 4854 s.op->newline() << "unsigned long relocated_addr = _stp_kmodule_relocate (sdp->module, sdp->section, sdp->address);";
b20febf3 4855 s.op->newline() << "if (relocated_addr == 0) continue;"; // quietly; assume module is absent
26e63673 4856 s.op->newline() << "probe_point = sdp->probe->pp;"; // for error messages
b20febf3 4857 s.op->newline() << "if (sdp->return_p) {";
606fd9c8 4858 s.op->newline(1) << "kp->u.krp.kp.addr = (void *) relocated_addr;";
c9bad430 4859 s.op->newline() << "if (sdp->maxactive_p) {";
606fd9c8 4860 s.op->newline(1) << "kp->u.krp.maxactive = sdp->maxactive_val;";
c9bad430 4861 s.op->newline(-1) << "} else {";
f07c3b68 4862 s.op->newline(1) << "kp->u.krp.maxactive = KRETACTIVE;";
c9bad430 4863 s.op->newline(-1) << "}";
606fd9c8 4864 s.op->newline() << "kp->u.krp.handler = &enter_kretprobe_probe;";
af234c40 4865 s.op->newline() << "#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,25)";
c87ae2c1 4866 s.op->newline() << "if (sdp->entry_probe) {";
af234c40
JS
4867 s.op->newline(1) << "kp->u.krp.entry_handler = &enter_kretprobe_entry_probe;";
4868 s.op->newline() << "kp->u.krp.data_size = sdp->saved_longs * sizeof(int64_t) + ";
4869 s.op->newline() << " sdp->saved_strings * MAXSTRINGLEN;";
4870 s.op->newline(-1) << "}";
4871 s.op->newline() << "#endif";
e4cb375f
MH
4872 // to ensure safeness of bspcache, always use aggr_kprobe on ia64
4873 s.op->newline() << "#ifdef __ia64__";
4874 s.op->newline() << "kp->dummy.addr = kp->u.krp.kp.addr;";
4875 s.op->newline() << "kp->dummy.pre_handler = NULL;";
4876 s.op->newline() << "rc = register_kprobe (& kp->dummy);";
4877 s.op->newline() << "if (rc == 0) {";
4878 s.op->newline(1) << "rc = register_kretprobe (& kp->u.krp);";
4879 s.op->newline() << "if (rc != 0)";
4880 s.op->newline(1) << "unregister_kprobe (& kp->dummy);";
4881 s.op->newline(-2) << "}";
4882 s.op->newline() << "#else";
606fd9c8 4883 s.op->newline() << "rc = register_kretprobe (& kp->u.krp);";
e4cb375f 4884 s.op->newline() << "#endif";
b20febf3 4885 s.op->newline(-1) << "} else {";
e4cb375f 4886 // to ensure safeness of bspcache, always use aggr_kprobe on ia64
606fd9c8
FCE
4887 s.op->newline(1) << "kp->u.kp.addr = (void *) relocated_addr;";
4888 s.op->newline() << "kp->u.kp.pre_handler = &enter_kprobe_probe;";
e4cb375f
MH
4889 s.op->newline() << "#ifdef __ia64__";
4890 s.op->newline() << "kp->dummy.addr = kp->u.kp.addr;";
4891 s.op->newline() << "kp->dummy.pre_handler = NULL;";
4892 s.op->newline() << "rc = register_kprobe (& kp->dummy);";
4893 s.op->newline() << "if (rc == 0) {";
4894 s.op->newline(1) << "rc = register_kprobe (& kp->u.kp);";
4895 s.op->newline() << "if (rc != 0)";
4896 s.op->newline(1) << "unregister_kprobe (& kp->dummy);";
4897 s.op->newline(-2) << "}";
4898 s.op->newline() << "#else";
606fd9c8 4899 s.op->newline() << "rc = register_kprobe (& kp->u.kp);";
e4cb375f 4900 s.op->newline() << "#endif";
b20febf3 4901 s.op->newline(-1) << "}";
9063462a
FCE
4902 s.op->newline() << "if (rc) {"; // PR6749: tolerate a failed register_*probe.
4903 s.op->newline(1) << "sdp->registered_p = 0;";
b350f56b 4904 s.op->newline() << "if (!sdp->optional_p)";
50b6acf7 4905 s.op->newline(1) << "_stp_warn (\"probe %s (address 0x%lx) registration error (rc %d)\", probe_point, (unsigned long) relocated_addr, rc);";
b350f56b 4906 s.op->newline(-1) << "rc = 0;"; // continue with other probes
9063462a
FCE
4907 // XXX: shall we increment numskipped?
4908 s.op->newline(-1) << "}";
4909
4910#if 0 /* pre PR 6749; XXX consider making an option */
c48cb0cc 4911 s.op->newline(1) << "for (j=i-1; j>=0; j--) {"; // partial rollback
b20febf3 4912 s.op->newline(1) << "struct stap_dwarf_probe *sdp2 = & stap_dwarf_probes[j];";
606fd9c8
FCE
4913 s.op->newline() << "struct stap_dwarf_kprobe *kp2 = & stap_dwarf_kprobes[j];";
4914 s.op->newline() << "if (sdp2->return_p) unregister_kretprobe (&kp2->u.krp);";
4915 s.op->newline() << "else unregister_kprobe (&kp2->u.kp);";
e4cb375f
MH
4916 s.op->newline() << "#ifdef __ia64__";
4917 s.op->newline() << "unregister_kprobe (&kp2->dummy);";
4918 s.op->newline() << "#endif";
c48cb0cc
FCE
4919 // NB: we don't have to clear sdp2->registered_p, since the module_exit code is
4920 // not run for this early-abort case.
4921 s.op->newline(-1) << "}";
4922 s.op->newline() << "break;"; // don't attempt to register any more probes
b20febf3 4923 s.op->newline(-1) << "}";
9063462a
FCE
4924#endif
4925
b20febf3
FCE
4926 s.op->newline() << "else sdp->registered_p = 1;";
4927 s.op->newline(-1) << "}"; // for loop
dc38c0ae
DS
4928}
4929
4930
b4be7cbc
FCE
4931void
4932dwarf_derived_probe_group::emit_module_refresh (systemtap_session& s)
4933{
4934 s.op->newline() << "for (i=0; i<" << probes_by_module.size() << "; i++) {";
4935 s.op->newline(1) << "struct stap_dwarf_probe *sdp = & stap_dwarf_probes[i];";
4936 s.op->newline() << "struct stap_dwarf_kprobe *kp = & stap_dwarf_kprobes[i];";
4937 s.op->newline() << "unsigned long relocated_addr = _stp_kmodule_relocate (sdp->module, sdp->section, sdp->address);";
4938 s.op->newline() << "int rc;";
4939
4940 // new module arrived?
4941 s.op->newline() << "if (sdp->registered_p == 0 && relocated_addr != 0) {";
4942 s.op->newline(1) << "if (sdp->return_p) {";
4943 s.op->newline(1) << "kp->u.krp.kp.addr = (void *) relocated_addr;";
4944 s.op->newline() << "if (sdp->maxactive_p) {";
4945 s.op->newline(1) << "kp->u.krp.maxactive = sdp->maxactive_val;";
4946 s.op->newline(-1) << "} else {";
4947 s.op->newline(1) << "kp->u.krp.maxactive = KRETACTIVE;";
4948 s.op->newline(-1) << "}";
4949 s.op->newline() << "kp->u.krp.handler = &enter_kretprobe_probe;";
4950 s.op->newline() << "#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,25)";
4951 s.op->newline() << "if (sdp->entry_probe) {";
4952 s.op->newline(1) << "kp->u.krp.entry_handler = &enter_kretprobe_entry_probe;";
4953 s.op->newline() << "kp->u.krp.data_size = sdp->saved_longs * sizeof(int64_t) + ";
4954 s.op->newline() << " sdp->saved_strings * MAXSTRINGLEN;";
4955 s.op->newline(-1) << "}";
4956 s.op->newline() << "#endif";
4957 // to ensure safeness of bspcache, always use aggr_kprobe on ia64
4958 s.op->newline() << "#ifdef __ia64__";
4959 s.op->newline() << "kp->dummy.addr = kp->u.krp.kp.addr;";
4960 s.op->newline() << "kp->dummy.pre_handler = NULL;";
4961 s.op->newline() << "rc = register_kprobe (& kp->dummy);";
4962 s.op->newline() << "if (rc == 0) {";
4963 s.op->newline(1) << "rc = register_kretprobe (& kp->u.krp);";
4964 s.op->newline() << "if (rc != 0)";
4965 s.op->newline(1) << "unregister_kprobe (& kp->dummy);";
4966 s.op->newline(-2) << "}";
4967 s.op->newline() << "#else";
4968 s.op->newline() << "rc = register_kretprobe (& kp->u.krp);";
4969 s.op->newline() << "#endif";
4970 s.op->newline(-1) << "} else {";
4971 // to ensure safeness of bspcache, always use aggr_kprobe on ia64
4972 s.op->newline(1) << "kp->u.kp.addr = (void *) relocated_addr;";
4973 s.op->newline() << "kp->u.kp.pre_handler = &enter_kprobe_probe;";
4974 s.op->newline() << "#ifdef __ia64__";
4975 s.op->newline() << "kp->dummy.addr = kp->u.kp.addr;";
4976 s.op->newline() << "kp->dummy.pre_handler = NULL;";
4977 s.op->newline() << "rc = register_kprobe (& kp->dummy);";
4978 s.op->newline() << "if (rc == 0) {";
4979 s.op->newline(1) << "rc = register_kprobe (& kp->u.kp);";
4980 s.op->newline() << "if (rc != 0)";
4981 s.op->newline(1) << "unregister_kprobe (& kp->dummy);";
4982 s.op->newline(-2) << "}";
4983 s.op->newline() << "#else";
4984 s.op->newline() << "rc = register_kprobe (& kp->u.kp);";
4985 s.op->newline() << "#endif";
4986 s.op->newline(-1) << "}";
4987 s.op->newline() << "if (rc == 0) sdp->registered_p = 1;";
4988
4989 // old module disappeared?
4990 s.op->newline(-1) << "} else if (sdp->registered_p == 1 && relocated_addr == 0) {";
4991 s.op->newline(1) << "if (sdp->return_p) {";
4992 s.op->newline(1) << "unregister_kretprobe (&kp->u.krp);";
4993 s.op->newline() << "atomic_add (kp->u.krp.nmissed, & skipped_count);";
4994 s.op->newline() << "#ifdef STP_TIMING";
4995 s.op->newline() << "if (kp->u.krp.nmissed)";
4996 s.op->newline(1) << "_stp_warn (\"Skipped due to missed kretprobe/1 on '%s': %d\\n\", sdp->probe->pp, kp->u.krp.nmissed);";
4997 s.op->newline(-1) << "#endif";
4998 s.op->newline() << "atomic_add (kp->u.krp.kp.nmissed, & skipped_count);";
4999 s.op->newline() << "#ifdef STP_TIMING";
5000 s.op->newline() << "if (kp->u.krp.kp.nmissed)";
5001 s.op->newline(1) << "_stp_warn (\"Skipped due to missed kretprobe/2 on '%s': %lu\\n\", sdp->probe->pp, kp->u.krp.kp.nmissed);";
5002 s.op->newline(-1) << "#endif";
5003 s.op->newline(-1) << "} else {";
5004 s.op->newline(1) << "unregister_kprobe (&kp->u.kp);";
5005 s.op->newline() << "atomic_add (kp->u.kp.nmissed, & skipped_count);";
5006 s.op->newline() << "#ifdef STP_TIMING";
5007 s.op->newline() << "if (kp->u.kp.nmissed)";
5008 s.op->newline(1) << "_stp_warn (\"Skipped due to missed kprobe on '%s': %lu\\n\", sdp->probe->pp, kp->u.kp.nmissed);";
5009 s.op->newline(-1) << "#endif";
5010 s.op->newline(-1) << "}";
5011 s.op->newline() << "#if defined(__ia64__)";
5012 s.op->newline() << "unregister_kprobe (&kp->dummy);";
5013 s.op->newline() << "#endif";
5014 s.op->newline() << "sdp->registered_p = 0;";
5015 s.op->newline(-1) << "}";
5016
5017 s.op->newline(-1) << "}"; // for loop
5018}
5019
5020
5021
5022
46b84a80 5023void
b20febf3 5024dwarf_derived_probe_group::emit_module_exit (systemtap_session& s)
46b84a80 5025{
62f52bb0
JS
5026 if (has_semaphores)
5027 {
5028 s.op->newline() << "for (i=0; i<" << probes_by_module.size() << "; i++) {";
5029 s.op->newline(1) << "struct stap_dwarf_probe *sdp = & stap_dwarf_probes[i];";
5030 s.op->newline() << "unsigned short sdt_semaphore = 0;"; // NB: fixed size
5031 s.op->newline() << "if (sdp->sdt_sem_address && __access_process_vm_noflush (sdp->tsk, sdp->sdt_sem_address, &sdt_semaphore, sizeof (sdt_semaphore), 0)) {";
5032 s.op->newline(1) << "sdt_semaphore --;";
5033 s.op->newline() << "__access_process_vm_noflush (sdp->tsk, sdp->sdt_sem_address, &sdt_semaphore, sizeof (sdt_semaphore), 1);";
5034 s.op->newline(-1) << "}";
5035 s.op->newline(-1) << "}";
5036 }
b642c901 5037
42cb22bd
MH
5038 //Unregister kprobes by batch interfaces.
5039 s.op->newline() << "#if defined(STAPCONF_UNREGISTER_KPROBES)";
5040 s.op->newline() << "j = 0;";
5041 s.op->newline() << "for (i=0; i<" << probes_by_module.size() << "; i++) {";
5042 s.op->newline(1) << "struct stap_dwarf_probe *sdp = & stap_dwarf_probes[i];";
5043 s.op->newline() << "struct stap_dwarf_kprobe *kp = & stap_dwarf_kprobes[i];";
5044 s.op->newline() << "if (! sdp->registered_p) continue;";
5045 s.op->newline() << "if (!sdp->return_p)";
5046 s.op->newline(1) << "stap_unreg_kprobes[j++] = &kp->u.kp;";
5047 s.op->newline(-2) << "}";
5048 s.op->newline() << "unregister_kprobes((struct kprobe **)stap_unreg_kprobes, j);";
5049 s.op->newline() << "j = 0;";
5050 s.op->newline() << "for (i=0; i<" << probes_by_module.size() << "; i++) {";
5051 s.op->newline(1) << "struct stap_dwarf_probe *sdp = & stap_dwarf_probes[i];";
5052 s.op->newline() << "struct stap_dwarf_kprobe *kp = & stap_dwarf_kprobes[i];";
5053 s.op->newline() << "if (! sdp->registered_p) continue;";
5054 s.op->newline() << "if (sdp->return_p)";
5055 s.op->newline(1) << "stap_unreg_kprobes[j++] = &kp->u.krp;";
5056 s.op->newline(-2) << "}";
5057 s.op->newline() << "unregister_kretprobes((struct kretprobe **)stap_unreg_kprobes, j);";
e4cb375f
MH
5058 s.op->newline() << "#ifdef __ia64__";
5059 s.op->newline() << "j = 0;";
5060 s.op->newline() << "for (i=0; i<" << probes_by_module.size() << "; i++) {";
5061 s.op->newline(1) << "struct stap_dwarf_probe *sdp = & stap_dwarf_probes[i];";
5062 s.op->newline() << "struct stap_dwarf_kprobe *kp = & stap_dwarf_kprobes[i];";
5063 s.op->newline() << "if (! sdp->registered_p) continue;";
5064 s.op->newline() << "stap_unreg_kprobes[j++] = &kp->dummy;";
5065 s.op->newline(-1) << "}";
5066 s.op->newline() << "unregister_kprobes((struct kprobe **)stap_unreg_kprobes, j);";
5067 s.op->newline() << "#endif";
42cb22bd
MH
5068 s.op->newline() << "#endif";
5069
b20febf3
FCE
5070 s.op->newline() << "for (i=0; i<" << probes_by_module.size() << "; i++) {";
5071 s.op->newline(1) << "struct stap_dwarf_probe *sdp = & stap_dwarf_probes[i];";
a36378d7 5072 s.op->newline() << "struct stap_dwarf_kprobe *kp = & stap_dwarf_kprobes[i];";
b20febf3
FCE
5073 s.op->newline() << "if (! sdp->registered_p) continue;";
5074 s.op->newline() << "if (sdp->return_p) {";
42cb22bd 5075 s.op->newline() << "#if !defined(STAPCONF_UNREGISTER_KPROBES)";
606fd9c8 5076 s.op->newline(1) << "unregister_kretprobe (&kp->u.krp);";
42cb22bd 5077 s.op->newline() << "#endif";
606fd9c8 5078 s.op->newline() << "atomic_add (kp->u.krp.nmissed, & skipped_count);";
73209876
FCE
5079 s.op->newline() << "#ifdef STP_TIMING";
5080 s.op->newline() << "if (kp->u.krp.nmissed)";
26e63673 5081 s.op->newline(1) << "_stp_warn (\"Skipped due to missed kretprobe/1 on '%s': %d\\n\", sdp->probe->pp, kp->u.krp.nmissed);";
73209876 5082 s.op->newline(-1) << "#endif";
606fd9c8 5083 s.op->newline() << "atomic_add (kp->u.krp.kp.nmissed, & skipped_count);";
73209876
FCE
5084 s.op->newline() << "#ifdef STP_TIMING";
5085 s.op->newline() << "if (kp->u.krp.kp.nmissed)";
26e63673 5086 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 5087 s.op->newline(-1) << "#endif";
557fb7a8 5088 s.op->newline(-1) << "} else {";
42cb22bd 5089 s.op->newline() << "#if !defined(STAPCONF_UNREGISTER_KPROBES)";
606fd9c8 5090 s.op->newline(1) << "unregister_kprobe (&kp->u.kp);";
42cb22bd 5091 s.op->newline() << "#endif";
606fd9c8 5092 s.op->newline() << "atomic_add (kp->u.kp.nmissed, & skipped_count);";
73209876
FCE
5093 s.op->newline() << "#ifdef STP_TIMING";
5094 s.op->newline() << "if (kp->u.kp.nmissed)";
26e63673 5095 s.op->newline(1) << "_stp_warn (\"Skipped due to missed kprobe on '%s': %lu\\n\", sdp->probe->pp, kp->u.kp.nmissed);";
73209876 5096 s.op->newline(-1) << "#endif";
b20febf3 5097 s.op->newline(-1) << "}";
e4cb375f
MH
5098 s.op->newline() << "#if !defined(STAPCONF_UNREGISTER_KPROBES) && defined(__ia64__)";
5099 s.op->newline() << "unregister_kprobe (&kp->dummy);";
5100 s.op->newline() << "#endif";
b20febf3
FCE
5101 s.op->newline() << "sdp->registered_p = 0;";
5102 s.op->newline(-1) << "}";
46b84a80
DS
5103}
5104
272c9036
WF
5105static void sdt_v3_tokenize(const string& str, vector<string>& tokens)
5106{
5107 string::size_type pos;
5108 string::size_type lastPos = str.find_first_not_of(" ", 0);
5109 string::size_type nextAt = str.find("@", lastPos);
5110 while (lastPos != string::npos)
5111 {
5112 pos = nextAt + 1;
5113 nextAt = str.find("@", pos);
5114 if (nextAt == string::npos)
5115 pos = string::npos;
5116 else
5117 pos = str.rfind(" ", nextAt);
5118
5119 tokens.push_back(str.substr(lastPos, pos - lastPos));
5120 lastPos = str.find_first_not_of(" ", pos);
5121 }
5122}
8aabf152
FCE
5123
5124struct sdt_kprobe_var_expanding_visitor: public var_expanding_visitor
5125{
5126 sdt_kprobe_var_expanding_visitor(const string & process_name,
5127 const string & provider_name,
5128 const string & probe_name,
5129 const string & arg_string,
5130 int arg_count):
5131 process_name (process_name), provider_name (provider_name), probe_name (probe_name),
5132 arg_count (arg_count)
5133 {
5134 tokenize(arg_string, arg_tokens, " ");
822a6a3d 5135 assert(arg_count <= 10);
8aabf152
FCE
5136 }
5137 const string & process_name;
5138 const string & provider_name;
5139 const string & probe_name;
5140 int arg_count;
5141 vector<string> arg_tokens;
5142
5143 void visit_target_symbol (target_symbol* e);
40a0c64e 5144 void visit_cast_op (cast_op* e);
8aabf152
FCE
5145};
5146
5147
aff5d390 5148struct sdt_uprobe_var_expanding_visitor: public var_expanding_visitor
7a05f484 5149{
ae1418f0 5150 enum regwidths {QI, QIh, HI, SI, DI};
f83336a5
FCE
5151 sdt_uprobe_var_expanding_visitor(systemtap_session& s,
5152 int elf_machine,
5153 const string & process_name,
a794dbeb 5154 const string & provider_name,
aff5d390 5155 const string & probe_name,
71e5e13d 5156 stap_sdt_probe_type probe_type,
aff5d390 5157 const string & arg_string,
8aabf152 5158 int ac):
332ba7e7 5159 session (s), elf_machine (elf_machine), process_name (process_name),
71e5e13d
SC
5160 provider_name (provider_name), probe_name (probe_name),
5161 probe_type (probe_type), arg_count ((unsigned) ac)
a8ec7719 5162 {
f83336a5
FCE
5163 /* Register name mapping table depends on the elf machine of this particular
5164 probe target process/file, not upon the host. So we can't just
5165 #ifdef _i686_ etc. */
ae1418f0
FCE
5166
5167#define DRI(name,num,width) dwarf_regs[name]=make_pair(num,width)
f83336a5 5168 if (elf_machine == EM_X86_64) {
46a94997
SC
5169 DRI ("%rax", 0, DI); DRI ("%eax", 0, SI); DRI ("%ax", 0, HI);
5170 DRI ("%al", 0, QI); DRI ("%ah", 0, QIh);
5171 DRI ("%rdx", 1, DI); DRI ("%edx", 1, SI); DRI ("%dx", 1, HI);
5172 DRI ("%dl", 1, QI); DRI ("%dh", 1, QIh);
5173 DRI ("%rcx", 2, DI); DRI ("%ecx", 2, SI); DRI ("%cx", 2, HI);
5174 DRI ("%cl", 2, QI); DRI ("%ch", 2, QIh);
5175 DRI ("%rbx", 3, DI); DRI ("%ebx", 3, SI); DRI ("%bx", 3, HI);
5176 DRI ("%bl", 3, QI); DRI ("%bh", 3, QIh);
5177 DRI ("%rsi", 4, DI); DRI ("%esi", 4, SI); DRI ("%si", 4, HI);
5178 DRI ("%sil", 4, QI);
5179 DRI ("%rdi", 5, DI); DRI ("%edi", 5, SI); DRI ("%di", 5, HI);
5180 DRI ("%dil", 5, QI);
5181 DRI ("%rbp", 6, DI); DRI ("%ebp", 6, SI); DRI ("%bp", 6, HI);
5182 DRI ("%rsp", 7, DI); DRI ("%esp", 7, SI); DRI ("%sp", 7, HI);
5183 DRI ("%r8", 8, DI); DRI ("%r8d", 8, SI); DRI ("%r8w", 8, HI);
5184 DRI ("%r8b", 8, QI);
5185 DRI ("%r9", 9, DI); DRI ("%r9d", 9, SI); DRI ("%r9w", 9, HI);
5186 DRI ("%r9b", 9, QI);
5187 DRI ("%r10", 10, DI); DRI ("%r10d", 10, SI); DRI ("%r10w", 10, HI);
5188 DRI ("%r10b", 10, QI);
5189 DRI ("%r11", 11, DI); DRI ("%r11d", 11, SI); DRI ("%r11w", 11, HI);
5190 DRI ("%r11b", 11, QI);
5191 DRI ("%r12", 12, DI); DRI ("%r12d", 12, SI); DRI ("%r12w", 12, HI);
5192 DRI ("%r12b", 12, QI);
5193 DRI ("%r13", 13, DI); DRI ("%r13d", 13, SI); DRI ("%r13w", 13, HI);
5194 DRI ("%r13b", 13, QI);
5195 DRI ("%r14", 14, DI); DRI ("%r14d", 14, SI); DRI ("%r14w", 14, HI);
5196 DRI ("%r14b", 14, QI);
5197 DRI ("%r15", 15, DI); DRI ("%r15d", 15, SI); DRI ("%r15w", 15, HI);
5198 DRI ("%r15b", 15, QI);
f83336a5 5199 } else if (elf_machine == EM_386) {
46a94997
SC
5200 DRI ("%eax", 0, SI); DRI ("%ax", 0, HI); DRI ("%al", 0, QI);
5201 DRI ("%ah", 0, QIh);
5202 DRI ("%ecx", 1, SI); DRI ("%cx", 1, HI); DRI ("%cl", 1, QI);
5203 DRI ("%ch", 1, QIh);
5204 DRI ("%edx", 2, SI); DRI ("%dx", 2, HI); DRI ("%dl", 2, QI);
5205 DRI ("%dh", 2, QIh);
5206 DRI ("%ebx", 3, SI); DRI ("%bx", 3, HI); DRI ("%bl", 3, QI);
5207 DRI ("%bh", 3, QIh);
5208 DRI ("%esp", 4, SI); DRI ("%sp", 4, HI);
5209 DRI ("%ebp", 5, SI); DRI ("%bp", 5, HI);
5210 DRI ("%esi", 6, SI); DRI ("%si", 6, HI); DRI ("%sil", 6, QI);
5211 DRI ("%edi", 7, SI); DRI ("%di", 7, HI); DRI ("%dil", 7, QI);
0491c523 5212 } else if (elf_machine == EM_PPC || elf_machine == EM_PPC64) {
46a94997
SC
5213 DRI ("%r0", 0, DI);
5214 DRI ("%r1", 1, DI);
5215 DRI ("%r2", 2, DI);
5216 DRI ("%r3", 3, DI);
5217 DRI ("%r4", 4, DI);
5218 DRI ("%r5", 5, DI);
5219 DRI ("%r6", 6, DI);
5220 DRI ("%r7", 7, DI);
5221 DRI ("%r8", 8, DI);
5222 DRI ("%r9", 9, DI);
5223 DRI ("%r10", 10, DI);
5224 DRI ("%r11", 11, DI);
5225 DRI ("%r12", 12, DI);
5226 DRI ("%r13", 13, DI);
5227 DRI ("%r14", 14, DI);
5228 DRI ("%r15", 15, DI);
5229 DRI ("%r16", 16, DI);
5230 DRI ("%r17", 17, DI);
5231 DRI ("%r18", 18, DI);
5232 DRI ("%r19", 19, DI);
5233 DRI ("%r20", 20, DI);
5234 DRI ("%r21", 21, DI);
5235 DRI ("%r22", 22, DI);
5236 DRI ("%r23", 23, DI);
5237 DRI ("%r24", 24, DI);
5238 DRI ("%r25", 25, DI);
5239 DRI ("%r26", 26, DI);
5240 DRI ("%r27", 27, DI);
5241 DRI ("%r28", 28, DI);
5242 DRI ("%r29", 29, DI);
5243 DRI ("%r30", 30, DI);
5244 DRI ("%r31", 31, DI);
8aabf152 5245 // PR11821: unadorned register "names" without -mregnames
46a94997
SC
5246 DRI ("0", 0, DI);
5247 DRI ("1", 1, DI);
5248 DRI ("2", 2, DI);
5249 DRI ("3", 3, DI);
5250 DRI ("4", 4, DI);
5251 DRI ("5", 5, DI);
5252 DRI ("6", 6, DI);
5253 DRI ("7", 7, DI);
5254 DRI ("8", 8, DI);
5255 DRI ("9", 9, DI);
5256 DRI ("10", 10, DI);
5257 DRI ("11", 11, DI);
5258 DRI ("12", 12, DI);
5259 DRI ("13", 13, DI);
5260 DRI ("14", 14, DI);
5261 DRI ("15", 15, DI);
5262 DRI ("16", 16, DI);
5263 DRI ("17", 17, DI);
5264 DRI ("18", 18, DI);
5265 DRI ("19", 19, DI);
5266 DRI ("20", 20, DI);
5267 DRI ("21", 21, DI);
5268 DRI ("22", 22, DI);
5269 DRI ("23", 23, DI);
5270 DRI ("24", 24, DI);
5271 DRI ("25", 25, DI);
5272 DRI ("26", 26, DI);
5273 DRI ("27", 27, DI);
5274 DRI ("28", 28, DI);
5275 DRI ("29", 29, DI);
5276 DRI ("30", 30, DI);
5277 DRI ("31", 31, DI);
14900130 5278 } else if (elf_machine == EM_S390) {
46a94997
SC
5279 DRI ("%r0", 0, DI);
5280 DRI ("%r1", 1, DI);
5281 DRI ("%r2", 2, DI);
5282 DRI ("%r3", 3, DI);
5283 DRI ("%r4", 4, DI);
5284 DRI ("%r5", 5, DI);
5285 DRI ("%r6", 6, DI);
5286 DRI ("%r7", 7, DI);
5287 DRI ("%r8", 8, DI);
5288 DRI ("%r9", 9, DI);
5289 DRI ("%r10", 10, DI);
5290 DRI ("%r11", 11, DI);
5291 DRI ("%r12", 12, DI);
5292 DRI ("%r13", 13, DI);
5293 DRI ("%r14", 14, DI);
5294 DRI ("%r15", 15, DI);
272c9036
WF
5295 } else if (elf_machine == EM_ARM) {
5296 DRI ("r0", 0, SI);
5297 DRI ("r1", 1, SI);
5298 DRI ("r2", 2, SI);
5299 DRI ("r3", 3, SI);
5300 DRI ("r4", 4, SI);
5301 DRI ("r5", 5, SI);
5302 DRI ("r6", 6, SI);
5303 DRI ("r7", 7, SI);
5304 DRI ("r8", 8, SI);
5305 DRI ("r9", 9, SI);
5306 DRI ("sl", 10, SI);
5307 DRI ("fp", 11, SI);
5308 DRI ("ip", 12, SI);
5309 DRI ("sp", 13, SI);
5310 DRI ("lr", 14, SI);
5311 DRI ("pc", 15, SI);
14900130 5312 } else if (arg_count) {
8aabf152 5313 /* permit this case; just fall back to dwarf */
f83336a5 5314 }
ae1418f0 5315#undef DRI
f83336a5 5316
ebbd2b45 5317 need_debug_info = false;
88e39987 5318 if (probe_type == uprobe3_type)
272c9036
WF
5319 {
5320 sdt_v3_tokenize(arg_string, arg_tokens);
5321 assert(arg_count <= 12);
5322 }
88e39987 5323 else
272c9036
WF
5324 {
5325 tokenize(arg_string, arg_tokens, " ");
5326 assert(arg_count <= 10);
5327 }
a8ec7719 5328 }
8aabf152 5329
f83336a5 5330 systemtap_session& session;
332ba7e7 5331 int elf_machine;
aff5d390 5332 const string & process_name;
a794dbeb 5333 const string & provider_name;
aff5d390 5334 const string & probe_name;
71e5e13d 5335 stap_sdt_probe_type probe_type;
8aabf152 5336 unsigned arg_count;
aff5d390 5337 vector<string> arg_tokens;
46a94997 5338 map<string, pair<unsigned,int> > dwarf_regs;
ebbd2b45 5339 bool need_debug_info;
aff5d390
SC
5340
5341 void visit_target_symbol (target_symbol* e);
6ef331c8
SC
5342 void visit_target_symbol_arg (target_symbol* e);
5343 void visit_target_symbol_context (target_symbol* e);
40a0c64e 5344 void visit_cast_op (cast_op* e);
aff5d390
SC
5345};
5346
7a05f484
SC
5347
5348void
6ef331c8 5349sdt_uprobe_var_expanding_visitor::visit_target_symbol_context (target_symbol* e)
7a05f484 5350{
6ef331c8 5351 if (e->addressof)
b530b5b3 5352 throw semantic_error(_("cannot take address of context variable"), e->tok);
6ef331c8
SC
5353
5354 if (e->name == "$$name")
7a05f484 5355 {
6ef331c8
SC
5356 literal_string *myname = new literal_string (probe_name);
5357 myname->tok = e->tok;
5358 provide(myname);
5359 return;
5360 }
7a05f484 5361
6ef331c8
SC
5362 else if (e->name == "$$provider")
5363 {
5364 literal_string *myname = new literal_string (provider_name);
5365 myname->tok = e->tok;
5366 provide(myname);
5367 return;
5368 }
a794dbeb 5369
6ef331c8
SC
5370 else if (e->name == "$$vars" || e->name == "$$parms")
5371 {
5372 e->assert_no_components("sdt", true);
6ef331c8
SC
5373
5374 // Convert $$vars to sprintf of a list of vars which we recursively evaluate
5375 // NB: we synthesize a new token here rather than reusing
5376 // e->tok, because print_format::print likes to use
5377 // its tok->content.
5378 token* pf_tok = new token(*e->tok);
5379 pf_tok->content = "sprintf";
63ea4244 5380
6ef331c8
SC
5381 print_format* pf = print_format::create(pf_tok);
5382
5383 for (unsigned i = 1; i <= arg_count; ++i)
5384 {
5385 if (i > 1)
5386 pf->raw_components += " ";
5387 target_symbol *tsym = new target_symbol;
5388 tsym->tok = e->tok;
5389 tsym->name = "$arg" + lex_cast(i);
5390 pf->raw_components += tsym->name;
5391 tsym->components = e->components;
5392
5393 expression *texp = require (tsym);
5394 if (!e->components.empty() &&
5395 e->components[0].type == target_symbol::comp_pretty_print)
5396 pf->raw_components += "=%s";
5397 else
5398 pf->raw_components += "=%#x";
5399 pf->args.push_back(texp);
5400 }
5401
5402 pf->components = print_format::string_to_components(pf->raw_components);
5403 provide (pf);
5404 }
5405 else
5406 assert(0); // shouldn't get here
5407}
5408
5409
5410void
5411sdt_uprobe_var_expanding_visitor::visit_target_symbol_arg (target_symbol *e)
5412{
5413 try
5414 {
8aabf152 5415 unsigned argno = 0; // the N in $argN
c69a87e0 5416 try
aff5d390 5417 {
5ecaa5a7 5418 if (startswith(e->name, "$arg"))
8aabf152 5419 argno = lex_cast<unsigned>(e->name.substr(4));
aff5d390 5420 }
c69a87e0 5421 catch (const runtime_error& f) // non-integral $arg suffix: e.g. $argKKKSDF
aff5d390 5422 {
8aabf152 5423 argno = 0;
aff5d390 5424 }
5ecaa5a7 5425
8aabf152
FCE
5426 if (arg_count == 0 || // a sdt.h variant without .probe-stored arg_count
5427 argno < 1 || argno > arg_count) // a $argN with out-of-range N
aff5d390 5428 {
8aabf152
FCE
5429 // NB: Either
5430 // 1) uprobe1_type $argN or $FOO (we don't know the arg_count)
5431 // 2) uprobe2_type $FOO (no probe args)
5432 // both of which get resolved later.
5433 need_debug_info = true;
5434 provide(e);
5435 return;
aff5d390 5436 }
277c21bc 5437
8aabf152
FCE
5438 assert (arg_tokens.size() >= argno);
5439 string asmarg = arg_tokens[argno-1]; // $arg1 => arg_tokens[0]
c57ea854 5440
8aabf152
FCE
5441 // Now we try to parse this thing, which is an assembler operand
5442 // expression. If we can't, we warn, back down to need_debug_info
b874bd52 5443 // and hope for the best. Here is the syntax for a few architectures.
9859b766 5444 // Note that the power iN syntax is only for V3 sdt.h; gcc emits the i.
8095a157
FCE
5445 //
5446 // literal reg reg reg + base+index*size+offset
b874bd52 5447 // indirect offset
8095a157 5448 // x86 $N %rR (%rR) N(%rR) O(%bR,%iR,S)
b874bd52
SC
5449 // power iN R (R) N(R)
5450 // ia64 N rR [r16]
5451 // s390 N %rR 0(rR) N(r15)
5452 // arm #N rR [rR] [rR, #N]
5453
8aabf152
FCE
5454 expression* argexpr = 0; // filled in in case of successful parse
5455
5456 string percent_regnames;
5457 string regnames;
5458 vector<string> matches;
71e5e13d 5459 long precision;
8aabf152
FCE
5460 int rc;
5461
40fe32e0
SC
5462 // Parse the leading length
5463
5464 if (asmarg.find('@') != string::npos)
5465 {
5466 precision = lex_cast<int>(asmarg.substr(0, asmarg.find('@')));
5467 asmarg = asmarg.substr(asmarg.find('@')+1);
5468 }
71e5e13d
SC
5469 else
5470 {
5471 // V1/V2 do not have precision field so default to signed long
5472 // V3 asm does not have precision field so default to unsigned long
5473 if (probe_type == uprobe3_type)
5474 precision = sizeof(long); // this is an asm probe
5475 else
5476 precision = -sizeof(long);
5477 }
40fe32e0 5478
8aabf152
FCE
5479 // test for a numeric literal.
5480 // Only accept (signed) decimals throughout. XXX
5481
5482 // PR11821. NB: on powerpc, literals are not prefixed with $,
5483 // so this regex does not match. But that's OK, since without
5484 // -mregnames, we can't tell them apart from register numbers
5485 // anyway. With -mregnames, we could, if gcc somehow
5486 // communicated to us the presence of that option, but alas it
5487 // doesn't. http://gcc.gnu.org/PR44995.
272c9036 5488 rc = regexp_match (asmarg, "^[i\\$#][-]?[0-9][0-9]*$", matches);
8aabf152
FCE
5489 if (! rc)
5490 {
75a371ce
JS
5491 string sn = matches[0].substr(1);
5492 int64_t n;
5493 try
5494 {
5495 // We have to pay attention to the size & sign, as gcc sometimes
5496 // propagates constants that don't quite match, like a negative
5497 // value to fill an unsigned type.
5498 switch (precision)
5499 {
5500 case -1: n = lex_cast< int8_t>(sn); break;
5501 case 1: n = lex_cast< uint8_t>(sn); break;
5502 case -2: n = lex_cast< int16_t>(sn); break;
5503 case 2: n = lex_cast<uint16_t>(sn); break;
5504 case -4: n = lex_cast< int32_t>(sn); break;
5505 case 4: n = lex_cast<uint32_t>(sn); break;
5506 default:
5507 case -8: n = lex_cast< int64_t>(sn); break;
5508 case 8: n = lex_cast<uint64_t>(sn); break;
5509 }
5510 }
5511 catch (std::runtime_error&)
5512 {
5513 goto not_matched;
5514 }
5515 literal_number* ln = new literal_number(n);
8aabf152
FCE
5516 ln->tok = e->tok;
5517 argexpr = ln;
5518 goto matched;
5519 }
5520
14900130
SC
5521 if (dwarf_regs.empty())
5522 goto not_matched;
d5b83cee 5523
8aabf152
FCE
5524 // Build regex pieces out of the known dwarf_regs. We keep two separate
5525 // lists: ones with the % prefix (and thus unambigiuous even despite PR11821),
5526 // and ones with no prefix (and thus only usable in unambiguous contexts).
46a94997 5527 for (map<string,pair<unsigned,int> >::iterator ri = dwarf_regs.begin(); ri != dwarf_regs.end(); ri++)
8aabf152
FCE
5528 {
5529 string regname = ri->first;
5530 assert (regname != "");
5531 regnames += string("|")+regname;
5532 if (regname[0]=='%')
5533 percent_regnames += string("|")+regname;
5534 }
5535 // clip off leading |
5536 regnames = regnames.substr(1);
272c9036
WF
5537 if (percent_regnames != "")
5538 percent_regnames = percent_regnames.substr(1);
8aabf152
FCE
5539
5540 // test for REGISTER
5541 // NB: Because PR11821, we must use percent_regnames here.
272c9036 5542 if (elf_machine == EM_PPC || elf_machine == EM_PPC64 || elf_machine == EM_ARM)
9109f487
SC
5543 rc = regexp_match (asmarg, string("^(")+regnames+string(")$"), matches);
5544 else
332ba7e7 5545 rc = regexp_match (asmarg, string("^(")+percent_regnames+string(")$"), matches);
8aabf152
FCE
5546 if (! rc)
5547 {
5548 string regname = matches[1];
46a94997
SC
5549 map<string,pair<unsigned,int> >::iterator ri = dwarf_regs.find (regname);
5550 if (ri != dwarf_regs.end()) // known register
8aabf152
FCE
5551 {
5552 embedded_expr *get_arg1 = new embedded_expr;
19c22e1f 5553 string width_adjust;
46a94997 5554 switch (ri->second.second)
19c22e1f 5555 {
892ec39a
SC
5556 case QI: width_adjust = ") & 0xff)"; break;
5557 case QIh: width_adjust = ">>8) & 0xff)"; break;
46a94997 5558 case HI:
71e5e13d 5559 // preserve 16 bit register signness
892ec39a
SC
5560 width_adjust = ") & 0xffff)";
5561 if (precision < 0)
55b377f4 5562 width_adjust += " << 48 >> 48";
ac8a78aa
SC
5563 break;
5564 case SI:
5565 // preserve 32 bit register signness
892ec39a
SC
5566 width_adjust = ") & 0xffffffff)";
5567 if (precision < 0)
55b377f4 5568 width_adjust += " << 32 >> 32";
19c22e1f 5569 break;
892ec39a 5570 default: width_adjust = "))";
19c22e1f 5571 }
55b377f4
SC
5572 string type = "";
5573 if (probe_type == uprobe3_type)
5574 type = (precision < 0
5575 ? "(int" : "(uint") + lex_cast(abs(precision) * 8) + "_t)";
5576 type = type + "((";
8aabf152
FCE
5577 get_arg1->tok = e->tok;
5578 get_arg1->code = string("/* unprivileged */ /* pure */")
892ec39a 5579 + string(" ((int64_t)") + type
8aabf152
FCE
5580 + (is_user_module (process_name)
5581 ? string("u_fetch_register(")
5582 : string("k_fetch_register("))
46a94997 5583 + lex_cast(dwarf_regs[regname].first) + string("))")
19c22e1f 5584 + width_adjust;
8aabf152
FCE
5585 argexpr = get_arg1;
5586 goto matched;
5587 }
5588 // invalid register name, fall through
5589 }
40fe32e0 5590
272c9036 5591 int reg, offset1;
e5b7b83f 5592 // test for OFFSET(REGISTER) where OFFSET is +-N+-N+-N
40fe32e0 5593 // NB: Despite PR11821, we can use regnames here, since the parentheses
e5b7b83f 5594 // make things unambiguous. (Note: gdb/stap-probe.c also parses this)
272c9036
WF
5595 // On ARM test for [REGISTER, OFFSET]
5596 if (elf_machine == EM_ARM)
5597 {
5598 rc = regexp_match (asmarg, string("^\\[(")+regnames+string("), #([+-]?[0-9]+)([+-][0-9]*)?([+-][0-9]*)?\\]$"), matches);
5599 reg = 1;
5600 offset1 = 2;
5601 }
5602 else
5603 {
5604 rc = regexp_match (asmarg, string("^([+-]?[0-9]*)([+-][0-9]*)?([+-][0-9]*)?[(](")+regnames+string(")[)]$"), matches);
5605 reg = 4;
5606 offset1 = 1;
5607 }
8aabf152
FCE
5608 if (! rc)
5609 {
e5b7b83f 5610 string regname;
8aabf152 5611 int64_t disp = 0;
272c9036
WF
5612 if (matches[reg].length())
5613 regname = matches[reg];
8095a157
FCE
5614 if (dwarf_regs.find (regname) == dwarf_regs.end())
5615 goto not_matched;
5616
272c9036 5617 for (int i=offset1; i <= (offset1 + 2); i++)
e5b7b83f
SC
5618 if (matches[i].length())
5619 try
5620 {
5621 disp += lex_cast<int64_t>(matches[i]); // should decode positive/negative hex/decimal
5622 }
8aabf152
FCE
5623 catch (const runtime_error& f) // unparseable offset
5624 {
5625 goto not_matched; // can't just 'break' out of
5626 // this case or use a sentinel
5627 // value, unfortunately
5628 }
5629
8aabf152 5630 // synthesize user_long(%{fetch_register(R)%} + D)
8aabf152
FCE
5631 embedded_expr *get_arg1 = new embedded_expr;
5632 get_arg1->tok = e->tok;
5633 get_arg1->code = string("/* unprivileged */ /* pure */")
5634 + (is_user_module (process_name)
5635 ? string("u_fetch_register(")
5636 : string("k_fetch_register("))
46a94997 5637 + lex_cast(dwarf_regs[regname].first) + string(")");
8aabf152 5638 // XXX: may we ever need to cast that to a narrower type?
40fe32e0 5639
8aabf152
FCE
5640 literal_number* inc = new literal_number(disp);
5641 inc->tok = e->tok;
40fe32e0 5642
8aabf152
FCE
5643 binary_expression *be = new binary_expression;
5644 be->tok = e->tok;
5645 be->left = get_arg1;
5646 be->op = "+";
5647 be->right = inc;
40fe32e0 5648
8aabf152 5649 functioncall *fc = new functioncall;
40fe32e0
SC
5650 switch (precision)
5651 {
7f6ce9ab
SC
5652 case 1: case -1:
5653 fc->function = "user_int8"; break;
5654 case 2:
5655 fc->function = "user_uint16"; break;
5656 case -2:
5657 fc->function = "user_int16"; break;
5658 case 4:
5659 fc->function = "user_uint32"; break;
5660 case -4:
5661 fc->function = "user_int32"; break;
5662 case 8: case -8:
5663 fc->function = "user_int64"; break;
40fe32e0
SC
5664 default: fc->function = "user_long";
5665 }
8aabf152
FCE
5666 fc->tok = e->tok;
5667 fc->args.push_back(be);
366af4e7 5668
8aabf152
FCE
5669 argexpr = fc;
5670 goto matched;
5671 }
8095a157
FCE
5672
5673 // test for OFFSET(BASE_REGISTER,INDEX_REGISTER[,SCALE]) where OFFSET is +-N+-N+-N
5674 // NB: Despite PR11821, we can use regnames here, since the parentheses
5675 // make things unambiguous. (Note: gdb/stap-probe.c also parses this)
0f7b51d6 5676 rc = regexp_match (asmarg, string("^([+-]?[0-9]*)([+-][0-9]*)?([+-][0-9]*)?[(](")+regnames+string("),(")+regnames+string(")(,[1248])?[)]$"), matches);
8095a157
FCE
5677 if (! rc)
5678 {
5679 string baseregname;
5680 string indexregname;
5681 int64_t disp = 0;
5682 short scale = 1;
5683
5684 if (matches[6].length())
5685 try
5686 {
5687 scale = lex_cast<short>(matches[6].substr(1)); // NB: skip the comma!
5688 // We could verify that scale is one of 1,2,4,8,
5689 // but it doesn't really matter. An erroneous
5690 // address merely results in run-time errors.
8aabf152 5691 }
8095a157
FCE
5692 catch (const runtime_error &f) // unparseable scale
5693 {
5694 goto not_matched;
5695 }
5696
5697 if (matches[4].length())
5698 baseregname = matches[4];
5699 if (dwarf_regs.find (baseregname) == dwarf_regs.end())
5700 goto not_matched;
5701
5702 if (matches[5].length())
5703 indexregname = matches[5];
5704 if (dwarf_regs.find (indexregname) == dwarf_regs.end())
5705 goto not_matched;
5706
f7719b3d 5707 for (int i = 1; i <= 3; i++) // up to three OFFSET terms
8095a157
FCE
5708 if (matches[i].length())
5709 try
5710 {
5711 disp += lex_cast<int64_t>(matches[i]); // should decode positive/negative hex/decimal
5712 }
5713 catch (const runtime_error& f) // unparseable offset
5714 {
5715 goto not_matched; // can't just 'break' out of
5716 // this case or use a sentinel
5717 // value, unfortunately
5718 }
5719
5720 // synthesize user_long(%{fetch_register(R1)+fetch_register(R2)*N%} + D)
5721
5722 embedded_expr *get_arg1 = new embedded_expr;
5723 string regfn = is_user_module (process_name)
5724 ? string("u_fetch_register")
5725 : string("k_fetch_register"); // NB: in practice sdt.h probes are for userspace only
5726
5727 get_arg1->tok = e->tok;
5728 get_arg1->code = string("/* unprivileged */ /* pure */")
5729 + regfn + string("(")+lex_cast(dwarf_regs[baseregname].first)+string(")")
5730 + string("+(")
5731 + regfn + string("(")+lex_cast(dwarf_regs[indexregname].first)+string(")")
5732 + string("*")
5733 + lex_cast(scale)
5734 + string(")");
5735
5736 // NB: could plop this +DISPLACEMENT bit into the embedded-c expression too
5737 literal_number* inc = new literal_number(disp);
5738 inc->tok = e->tok;
5739
5740 binary_expression *be = new binary_expression;
5741 be->tok = e->tok;
5742 be->left = get_arg1;
5743 be->op = "+";
5744 be->right = inc;
5745
5746 functioncall *fc = new functioncall;
5747 switch (precision)
5748 {
5749 case 1: case -1:
5750 fc->function = "user_int8"; break;
5751 case 2:
5752 fc->function = "user_uint16"; break;
5753 case -2:
5754 fc->function = "user_int16"; break;
5755 case 4:
5756 fc->function = "user_uint32"; break;
5757 case -4:
5758 fc->function = "user_int32"; break;
5759 case 8: case -8:
5760 fc->function = "user_int64"; break;
5761 default: fc->function = "user_long";
5762 }
5763 fc->tok = e->tok;
5764 fc->args.push_back(be);
5765
5766 argexpr = fc;
5767 goto matched;
8aabf152
FCE
5768 }
5769
8aabf152
FCE
5770
5771 not_matched:
5772 // The asmarg operand was not recognized. Back down to dwarf.
5773 if (! session.suppress_warnings)
84fef8ee
FCE
5774 {
5775 if (probe_type == UPROBE3_TYPE)
5776 session.print_warning (_F("Can't parse SDT_V3 operand '%s'", asmarg.c_str()), e->tok);
5777 else // must be *PROBE2; others don't get asm operands
5778 session.print_warning (_F("Downgrading SDT_V2 probe argument to dwarf, can't parse '%s'",
5779 asmarg.c_str()), e->tok);
5780 }
8aabf152
FCE
5781 assert (argexpr == 0);
5782 need_debug_info = true;
5783 provide (e);
5784 return;
366af4e7 5785
8aabf152
FCE
5786 matched:
5787 assert (argexpr != 0);
366af4e7
RM
5788
5789 if (session.verbose > 2)
1e41115c 5790 //TRANSLATORS: We're mapping the operand to a new expression*.
b530b5b3 5791 clog << _F("mapped asm operand %s to ", asmarg.c_str()) << *argexpr << endl;
366af4e7 5792
aff5d390 5793 if (e->components.empty()) // We have a scalar
8aabf152
FCE
5794 {
5795 if (e->addressof)
b530b5b3 5796 throw semantic_error(_("cannot take address of sdt variable"), e->tok);
8aabf152
FCE
5797 provide (argexpr);
5798 return;
5799 }
5800 else // $var->foo
5801 {
5802 cast_op *cast = new cast_op;
5803 cast->name = "@cast";
5804 cast->tok = e->tok;
5805 cast->operand = argexpr;
5806 cast->components = e->components;
5807 cast->type_name = probe_name + "_arg" + lex_cast(argno);
5808 cast->module = process_name;
5809 cast->visit(this);
5810 return;
5811 }
366af4e7 5812
8aabf152 5813 /* NOTREACHED */
aff5d390
SC
5814 }
5815 catch (const semantic_error &er)
5816 {
5817 e->chain (er);
5818 provide (e);
5819 }
5820}
5821
5822
6ef331c8
SC
5823void
5824sdt_uprobe_var_expanding_visitor::visit_target_symbol (target_symbol* e)
5825{
5826 try
5827 {
49131a6d
MW
5828 assert(e->name.size() > 0
5829 && ((e->name[0] == '$' && e->target_name == "")
5830 || (e->name == "@var" && e->target_name != "")));
6ef331c8
SC
5831
5832 if (e->name == "$$name" || e->name == "$$provider" || e->name == "$$parms" || e->name == "$$vars")
5833 visit_target_symbol_context (e);
5834 else
5835 visit_target_symbol_arg (e);
5836 }
5837 catch (const semantic_error &er)
5838 {
5839 e->chain (er);
5840 provide (e);
5841 }
5842}
5843
5844
40a0c64e
JS
5845void
5846sdt_uprobe_var_expanding_visitor::visit_cast_op (cast_op* e)
5847{
5848 // Fill in our current module context if needed
5849 if (e->module.empty())
5850 e->module = process_name;
5851
5852 var_expanding_visitor::visit_cast_op(e);
5853}
5854
5855
aff5d390
SC
5856void
5857sdt_kprobe_var_expanding_visitor::visit_target_symbol (target_symbol *e)
5858{
5859 try
5860 {
277c21bc 5861 if (e->name == "$$name")
aff5d390
SC
5862 {
5863 if (e->addressof)
b530b5b3 5864 throw semantic_error(_("cannot take address of sdt context variable"), e->tok);
aff5d390
SC
5865
5866 literal_string *myname = new literal_string (probe_name);
5867 myname->tok = e->tok;
5868 provide(myname);
5869 return;
5870 }
277c21bc 5871 if (e->name == "$$provider")
a794dbeb
FCE
5872 {
5873 if (e->addressof)
b530b5b3 5874 throw semantic_error(_("cannot take address of sdt context variable"), e->tok);
a794dbeb
FCE
5875
5876 literal_string *myname = new literal_string (provider_name);
5877 myname->tok = e->tok;
5878 provide(myname);
5879 return;
5880 }
aff5d390 5881
5ecaa5a7 5882 int argno = -1;
aff5d390
SC
5883 try
5884 {
5ecaa5a7
JS
5885 if (startswith(e->name, "$arg"))
5886 argno = lex_cast<int>(e->name.substr(4));
aff5d390
SC
5887 }
5888 catch (const runtime_error& f) // non-integral $arg suffix: e.g. $argKKKSDF
5889 {
aff5d390 5890 }
5ecaa5a7 5891 if (argno < 0)
b530b5b3 5892 throw semantic_error(_("invalid variable, must be of the form $argN"), e->tok);
aff5d390 5893 if (argno < 1 || argno > arg_count)
b530b5b3 5894 throw semantic_error(_("invalid argument number"), e->tok);
5ecaa5a7 5895
c69a87e0
FCE
5896 bool lvalue = is_active_lvalue(e);
5897 functioncall *fc = new functioncall;
63ea4244 5898
c69a87e0
FCE
5899 // First two args are hidden: 1. pointer to probe name 2. task id
5900 if (arg_count < 2)
aff5d390 5901 {
71e5e13d 5902 fc->function = "long_arg";
aff5d390
SC
5903 fc->type = pe_long;
5904 fc->tok = e->tok;
5905 // skip the hidden args
5906 literal_number* num = new literal_number(argno + 2);
5907 num->tok = e->tok;
5908 fc->args.push_back(num);
5909 }
5910 else
5911 {
5912 // args are passed in arg3 as members of a struct
5913 fc->function = "user_long";
5914 fc->tok = e->tok;
5915 binary_expression *be = new binary_expression;
5916 be->tok = e->tok;
5917 functioncall *get_arg1 = new functioncall;
5918 get_arg1->function = "pointer_arg";
5919 get_arg1->tok = e->tok;
5920 // arg3 is the pointer to a struct of arguments
c57ea854 5921 literal_number* num = new literal_number(3);
aff5d390
SC
5922 num->tok = e->tok;
5923 get_arg1->args.push_back(num);
5924
5925 be->left = get_arg1;
5926 be->op = "+";
5927 // offset in struct to the desired arg
5928 literal_number* inc = new literal_number((argno - 1) * 8);
5929 inc->tok = e->tok;
5930 be->right = inc;
5931 fc->args.push_back(be);
5932 }
c69a87e0 5933 if (lvalue)
aff5d390 5934 *(target_symbol_setter_functioncalls.top()) = fc;
63ea4244 5935
aff5d390
SC
5936 if (e->components.empty()) // We have a scalar
5937 {
5938 if (e->addressof)
b530b5b3 5939 throw semantic_error(_("cannot take address of sdt variable"), e->tok);
63ea4244 5940
aff5d390
SC
5941 provide(fc);
5942 return;
5943 }
c69a87e0 5944 cast_op *cast = new cast_op;
277c21bc 5945 cast->name = "@cast";
c69a87e0
FCE
5946 cast->tok = e->tok;
5947 cast->operand = fc;
5948 cast->components = e->components;
7f6b80bd 5949 cast->type_name = probe_name + "_arg" + lex_cast(argno);
c69a87e0 5950 cast->module = process_name;
63ea4244 5951
c69a87e0 5952 cast->visit(this);
7a05f484 5953 }
c69a87e0 5954 catch (const semantic_error &er)
ad002306 5955 {
1af1e62d 5956 e->chain (er);
c69a87e0 5957 provide (e);
ad002306 5958 }
7a05f484 5959}
46b84a80 5960
277c21bc 5961
40a0c64e
JS
5962void
5963sdt_kprobe_var_expanding_visitor::visit_cast_op (cast_op* e)
5964{
5965 // Fill in our current module context if needed
5966 if (e->module.empty())
5967 e->module = process_name;
5968
5969 var_expanding_visitor::visit_cast_op(e);
5970}
5971
5972
576eaefe
SC
5973void
5974plt_expanding_visitor::visit_target_symbol (target_symbol *e)
5975{
5976 try
5977 {
5978 if (e->name == "$$name")
5979 {
5980 literal_string *myname = new literal_string (entry);
5981 myname->tok = e->tok;
5982 provide(myname);
5983 return;
5984 }
3d69c03f
JS
5985
5986 // variable not found -> throw a semantic error
5987 // (only to be caught right away, but this may be more complex later...)
5988 string alternatives = "$$name";
5989 throw semantic_error(_F("unable to find plt variable '%s' (alternatives: %s)",
5990 e->name.c_str(), alternatives.c_str()), e->tok);
576eaefe
SC
5991 }
5992 catch (const semantic_error &er)
5993 {
5994 e->chain (er);
5995 provide (e);
5996 }
5997}
5998
5999
edce5b67
JS
6000struct sdt_query : public base_query
6001{
6002 sdt_query(probe * base_probe, probe_point * base_loc,
6003 dwflpp & dw, literal_map_t const & params,
51d6bda3 6004 vector<derived_probe *> & results, const string user_lib);
edce5b67 6005
51d6bda3 6006 void query_library (const char *data);
576eaefe 6007 void query_plt (const char *entry, size_t addr) {}
edce5b67
JS
6008 void handle_query_module();
6009
6010private:
15284963 6011 stap_sdt_probe_type probe_type;
d61ea602 6012 enum { probe_section=0, note_section=1, unknown_section=-1 } probe_loc;
edce5b67
JS
6013 probe * base_probe;
6014 probe_point * base_loc;
6846cfc8 6015 literal_map_t const & params;
edce5b67 6016 vector<derived_probe *> & results;
a794dbeb
FCE
6017 string pp_mark;
6018 string pp_provider;
51d6bda3 6019 string user_lib;
edce5b67
JS
6020
6021 set<string> probes_handled;
6022
6023 Elf_Data *pdata;
6024 size_t probe_scn_offset;
6025 size_t probe_scn_addr;
aff5d390 6026 uint64_t arg_count;
40fe32e0 6027 GElf_Addr base;
c57ea854 6028 GElf_Addr pc;
aff5d390 6029 string arg_string;
edce5b67 6030 string probe_name;
a794dbeb 6031 string provider_name;
79a0ca08 6032 Dwarf_Addr semaphore;
edce5b67
JS
6033
6034 bool init_probe_scn();
6b51ee12 6035 bool get_next_probe();
c57ea854
SC
6036 void iterate_over_probe_entries();
6037 void handle_probe_entry();
edce5b67 6038
40fe32e0
SC
6039 static void setup_note_probe_entry_callback (void *object, int type, const char *data, size_t len);
6040 void setup_note_probe_entry (int type, const char *data, size_t len);
6041
edce5b67 6042 void convert_probe(probe *base);
4ddb6dd0 6043 void record_semaphore(vector<derived_probe *> & results, unsigned start);
c72aa911 6044 probe* convert_location();
40fe32e0 6045 bool have_uprobe() {return probe_type == uprobe1_type || probe_type == uprobe2_type || probe_type == uprobe3_type;}
aff5d390 6046 bool have_kprobe() {return probe_type == kprobe1_type || probe_type == kprobe2_type;}
c57ea854
SC
6047 bool have_debuginfo_uprobe(bool need_debug_info)
6048 {return probe_type == uprobe1_type
40fe32e0 6049 || ((probe_type == uprobe2_type || probe_type == uprobe3_type)
c57ea854 6050 && need_debug_info);}
40fe32e0 6051 bool have_debuginfoless_uprobe() {return probe_type == uprobe2_type || probe_type == uprobe3_type;}
edce5b67
JS
6052};
6053
6054
6055sdt_query::sdt_query(probe * base_probe, probe_point * base_loc,
6056 dwflpp & dw, literal_map_t const & params,
51d6bda3 6057 vector<derived_probe *> & results, const string user_lib):
d61ea602
JS
6058 base_query(dw, params), probe_type(unknown_probe_type),
6059 probe_loc(unknown_section), base_probe(base_probe),
74fe61bc
LB
6060 base_loc(base_loc), params(params), results(results), user_lib(user_lib),
6061 probe_scn_offset(0), probe_scn_addr(0), arg_count(0), base(0), pc(0),
6062 semaphore(0)
edce5b67 6063{
a794dbeb
FCE
6064 assert(get_string_param(params, TOK_MARK, pp_mark));
6065 get_string_param(params, TOK_PROVIDER, pp_provider); // pp_provider == "" -> unspecified
6066
ef428667
FCE
6067 // PR10245: permit usage of dtrace-y "-" separator in marker name;
6068 // map it to double-underscores.
6069 size_t pos = 0;
6070 while (1) // there may be more than one
6071 {
a794dbeb 6072 size_t i = pp_mark.find("-", pos);
ef428667 6073 if (i == string::npos) break;
a794dbeb 6074 pp_mark.replace (i, 1, "__");
ef428667
FCE
6075 pos = i+1; // resume searching after the inserted __
6076 }
a794dbeb
FCE
6077
6078 // XXX: same for pp_provider?
edce5b67
JS
6079}
6080
6081
6082void
c57ea854 6083sdt_query::handle_probe_entry()
edce5b67 6084{
c57ea854
SC
6085 if (! have_uprobe()
6086 && !probes_handled.insert(probe_name).second)
edce5b67
JS
6087 return;
6088
6089 if (sess.verbose > 3)
c57ea854 6090 {
b530b5b3
LB
6091 //TRANSLATORS: Describing what probe type (kprobe or uprobe) the probe
6092 //TRANSLATORS: is matched to.
6093 clog << _F("matched probe_name %s probe type ", probe_name.c_str());
c57ea854
SC
6094 switch (probe_type)
6095 {
6096 case uprobe1_type:
6097 clog << "uprobe1 at 0x" << hex << pc << dec << endl;
6098 break;
6099 case uprobe2_type:
6100 clog << "uprobe2 at 0x" << hex << pc << dec << endl;
6101 break;
40fe32e0
SC
6102 case uprobe3_type:
6103 clog << "uprobe3 at 0x" << hex << pc << dec << endl;
6104 break;
c57ea854
SC
6105 case kprobe1_type:
6106 clog << "kprobe1" << endl;
6107 break;
6108 case kprobe2_type:
6109 clog << "kprobe2" << endl;
6110 break;
d61ea602
JS
6111 default:
6112 clog << "unknown!" << endl;
6113 break;
c57ea854
SC
6114 }
6115 }
edce5b67 6116
c57ea854
SC
6117 // Extend the derivation chain
6118 probe *new_base = convert_location();
6119 probe_point *new_location = new_base->locations[0];
6120
c57ea854
SC
6121 bool need_debug_info = false;
6122
7d395255
JS
6123 // We could get the Elf* from either dwarf_getelf(dwfl_module_getdwarf(...))
6124 // or dwfl_module_getelf(...). We only need it for the machine type, which
6125 // should be the same. The bias is used for relocating debuginfoless probes,
6126 // though, so that must come from the possibly-prelinked ELF file, not DWARF.
c57ea854 6127 Dwarf_Addr bias;
7d395255 6128 Elf* elf = dwfl_module_getelf (dw.mod_info->mod, &bias);
c57ea854
SC
6129
6130 if (have_kprobe())
edce5b67 6131 {
c57ea854 6132 convert_probe(new_base);
c57ea854
SC
6133 // Expand the local variables in the probe body
6134 sdt_kprobe_var_expanding_visitor svv (module_val,
6135 provider_name,
6136 probe_name,
6137 arg_string,
6138 arg_count);
6139 svv.replace (new_base->body);
6140 }
6141 else
6142 {
6143 /* Figure out the architecture of this particular ELF file.
6144 The dwarfless register-name mappings depend on it. */
6145 GElf_Ehdr ehdr_mem;
6146 GElf_Ehdr* em = gelf_getehdr (elf, &ehdr_mem);
6147 if (em == 0) { dwfl_assert ("dwfl_getehdr", dwfl_errno()); }
6148 int elf_machine = em->e_machine;
6149 sdt_uprobe_var_expanding_visitor svv (sess, elf_machine,
6150 module_val,
6151 provider_name,
6152 probe_name,
71e5e13d 6153 probe_type,
c57ea854
SC
6154 arg_string,
6155 arg_count);
6156 svv.replace (new_base->body);
6157 need_debug_info = svv.need_debug_info;
6158 }
6159
6160 unsigned i = results.size();
edce5b67 6161
c57ea854
SC
6162 if (have_kprobe())
6163 derive_probes(sess, new_base, results);
6164
6165 else
6166 {
6167 // XXX: why not derive_probes() in the uprobes case too?
6168 literal_map_t params;
6169 for (unsigned i = 0; i < new_location->components.size(); ++i)
39a3e397 6170 {
c57ea854
SC
6171 probe_point::component *c = new_location->components[i];
6172 params[c->functor] = c->arg;
39a3e397 6173 }
30263a73 6174
c57ea854
SC
6175 dwarf_query q(new_base, new_location, dw, params, results, "", "");
6176 q.has_mark = true; // enables mid-statement probing
edce5b67 6177
c57ea854
SC
6178 // V2 probes need dwarf info in case of a variable reference
6179 if (have_debuginfo_uprobe(need_debug_info))
6180 dw.iterate_over_modules(&query_module, &q);
6181 else if (have_debuginfoless_uprobe())
aff5d390 6182 {
c57ea854
SC
6183 string section;
6184 Dwarf_Addr reloc_addr = q.statement_num_val + bias;
6185 if (dwfl_module_relocations (q.dw.mod_info->mod) > 0)
6186 {
6187 dwfl_module_relocate_address (q.dw.mod_info->mod, &reloc_addr);
6188 section = ".dynamic";
6189 }
6190 else
6191 section = ".absolute";
6192
6193 uprobe_derived_probe* p =
6194 new uprobe_derived_probe ("", "", 0, q.module_val, section,
6195 q.statement_num_val, reloc_addr, q, 0);
6196 p->saveargs (arg_count);
6197 results.push_back (p);
aff5d390 6198 }
c57ea854 6199 }
487bf4e2 6200 sess.unwindsym_modules.insert (dw.module_name);
c57ea854
SC
6201 record_semaphore(results, i);
6202}
edce5b67 6203
4ddb6dd0 6204
c57ea854
SC
6205void
6206sdt_query::handle_query_module()
6207{
6208 if (!init_probe_scn())
6209 return;
edce5b67 6210
c57ea854
SC
6211 if (sess.verbose > 3)
6212 clog << "TOK_MARK: " << pp_mark << " TOK_PROVIDER: " << pp_provider << endl;
edce5b67 6213
40fe32e0
SC
6214 if (probe_loc == note_section)
6215 {
6216 GElf_Shdr shdr_mem;
6217 GElf_Shdr *shdr = dw.get_section (".stapsdt.base", &shdr_mem);
6218
6219 if (shdr)
6220 base = shdr->sh_addr;
6221 else
6222 base = 0;
6223 dw.iterate_over_notes ((void*) this, &sdt_query::setup_note_probe_entry_callback);
6224 }
d61ea602 6225 else if (probe_loc == probe_section)
40fe32e0 6226 iterate_over_probe_entries ();
edce5b67
JS
6227}
6228
6229
6230bool
6231sdt_query::init_probe_scn()
6232{
448a86b7 6233 Elf* elf;
edce5b67 6234 GElf_Shdr shdr_mem;
40fe32e0
SC
6235
6236 GElf_Shdr *shdr = dw.get_section (".note.stapsdt", &shdr_mem);
6237 if (shdr)
6238 {
6239 probe_loc = note_section;
6240 return true;
6241 }
edce5b67 6242
448a86b7 6243 shdr = dw.get_section (".probes", &shdr_mem, &elf);
fea74777 6244 if (shdr)
edce5b67 6245 {
fea74777
SC
6246 pdata = elf_getdata_rawchunk (elf, shdr->sh_offset, shdr->sh_size, ELF_T_BYTE);
6247 probe_scn_offset = 0;
6248 probe_scn_addr = shdr->sh_addr;
6249 assert (pdata != NULL);
6250 if (sess.verbose > 4)
ce0f6648
LB
6251 clog << "got .probes elf scn_addr@0x" << probe_scn_addr << ", size: "
6252 << pdata->d_size << endl;
40fe32e0 6253 probe_loc = probe_section;
fea74777 6254 return true;
edce5b67 6255 }
fea74777 6256 else
edce5b67 6257 return false;
edce5b67
JS
6258}
6259
40fe32e0
SC
6260void
6261sdt_query::setup_note_probe_entry_callback (void *object, int type, const char *data, size_t len)
6262{
6263 sdt_query *me = (sdt_query*)object;
6264 me->setup_note_probe_entry (type, data, len);
6265}
6266
6267
6268void
6269sdt_query::setup_note_probe_entry (int type, const char *data, size_t len)
6270{
6271 // if (nhdr.n_namesz == sizeof _SDT_NOTE_NAME
6272 // && !memcmp (data->d_buf + name_off,
6273 // _SDT_NOTE_NAME, sizeof _SDT_NOTE_NAME))
6274
6275 // probes are in the .note.stapsdt section
6276#define _SDT_NOTE_TYPE 3
6277 if (type != _SDT_NOTE_TYPE)
6278 return;
6279
6280 union
6281 {
6282 Elf64_Addr a64[3];
6283 Elf32_Addr a32[3];
6284 } buf;
6285 Dwarf_Addr bias;
6286 Elf* elf = (dwfl_module_getelf (dw.mod_info->mod, &bias));
6287 Elf_Data dst =
6288 {
6289 &buf, ELF_T_ADDR, EV_CURRENT,
6290 gelf_fsize (elf, ELF_T_ADDR, 3, EV_CURRENT), 0, 0
6291 };
6292 assert (dst.d_size <= sizeof buf);
6293
6294 if (len < dst.d_size + 3)
6295 return;
6296
6297 Elf_Data src =
6298 {
6299 (void *) data, ELF_T_ADDR, EV_CURRENT,
6300 dst.d_size, 0, 0
6301 };
6302
6303 if (gelf_xlatetom (elf, &dst, &src,
6304 elf_getident (elf, NULL)[EI_DATA]) == NULL)
6305 printf ("gelf_xlatetom: %s", elf_errmsg (-1));
6306
6307 probe_type = uprobe3_type;
6308 const char * provider = data + dst.d_size;
6309 provider_name = provider;
6310 const char *name = (const char*)memchr (provider, '\0', data + len - provider);
6311 probe_name = ++name;
6312
6313 // Did we find a matching probe?
6314 if (! (dw.function_name_matches_pattern (probe_name, pp_mark)
6315 && ((pp_provider == "")
6316 || dw.function_name_matches_pattern (provider_name, pp_provider))))
6317 return;
6318
6319 const char *args = (const char*)memchr (name, '\0', data + len - name);
6320 if (args++ == NULL ||
6321 memchr (args, '\0', data + len - name) != data + len - 1)
6322 if (name == NULL)
6323 return;
6324 arg_string = args;
6325
6326 arg_count = 0;
6327 for (unsigned i = 0; i < arg_string.length(); i++)
272c9036 6328 if (arg_string[i] == '@')
40fe32e0 6329 arg_count += 1;
40fe32e0
SC
6330
6331 GElf_Addr base_ref;
6332 if (gelf_getclass (elf) == ELFCLASS32)
6333 {
6334 pc = buf.a32[0];
6335 base_ref = buf.a32[1];
6336 semaphore = buf.a32[2];
6337 }
6338 else
6339 {
6340 pc = buf.a64[0];
6341 base_ref = buf.a64[1];
6342 semaphore = buf.a64[2];
6343 }
6344
6345 semaphore += base - base_ref;
6346 pc += base - base_ref;
6347
7d395255
JS
6348 // The semaphore also needs the ELF bias added now, so
6349 // record_semaphore can properly relocate it later.
6350 semaphore += bias;
6351
40fe32e0 6352 if (sess.verbose > 4)
b530b5b3 6353 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
6354
6355 handle_probe_entry();
6356}
6357
6358
c57ea854
SC
6359void
6360sdt_query::iterate_over_probe_entries()
edce5b67 6361{
c57ea854 6362 // probes are in the .probe section
edce5b67
JS
6363 while (probe_scn_offset < pdata->d_size)
6364 {
aff5d390
SC
6365 stap_sdt_probe_entry_v1 *pbe_v1 = (stap_sdt_probe_entry_v1 *) ((char*)pdata->d_buf + probe_scn_offset);
6366 stap_sdt_probe_entry_v2 *pbe_v2 = (stap_sdt_probe_entry_v2 *) ((char*)pdata->d_buf + probe_scn_offset);
15284963 6367 probe_type = (stap_sdt_probe_type)(pbe_v1->type_a);
aff5d390 6368 if (! have_uprobe() && ! have_kprobe())
edce5b67
JS
6369 {
6370 // Unless this is a mangled .probes section, this happens
6371 // because the name of the probe comes first, followed by
6372 // the sentinel.
6373 if (sess.verbose > 5)
b530b5b3 6374 clog << _F("got unknown probe_type : 0x%x", probe_type) << endl;
edce5b67
JS
6375 probe_scn_offset += sizeof(__uint32_t);
6376 continue;
6377 }
aff5d390
SC
6378 if ((long)pbe_v1 % sizeof(__uint64_t)) // we have stap_sdt_probe_entry_v1.type_b
6379 {
6380 pbe_v1 = (stap_sdt_probe_entry_v1*)((char*)pbe_v1 - sizeof(__uint32_t));
6381 if (pbe_v1->type_b != uprobe1_type && pbe_v1->type_b != kprobe1_type)
6382 continue;
6383 }
6384
6385 if (probe_type == uprobe1_type || probe_type == kprobe1_type)
6386 {
79a0ca08 6387 if (pbe_v1->name == 0) // No name possibly means we have a .so with a relocation
c57ea854 6388 return;
79a0ca08 6389 semaphore = 0;
aff5d390 6390 probe_name = (char*)((char*)pdata->d_buf + pbe_v1->name - (char*)probe_scn_addr);
a794dbeb 6391 provider_name = ""; // unknown
aff5d390
SC
6392 if (probe_type == uprobe1_type)
6393 {
6394 pc = pbe_v1->arg;
6395 arg_count = 0;
6396 }
6397 else if (probe_type == kprobe1_type)
6398 arg_count = pbe_v1->arg;
6399 probe_scn_offset += sizeof (stap_sdt_probe_entry_v1);
6400 }
6401 else if (probe_type == uprobe2_type || probe_type == kprobe2_type)
6402 {
79a0ca08 6403 if (pbe_v2->name == 0) // No name possibly means we have a .so with a relocation
c57ea854 6404 return;
79a0ca08 6405 semaphore = pbe_v2->semaphore;
aff5d390 6406 probe_name = (char*)((char*)pdata->d_buf + pbe_v2->name - (char*)probe_scn_addr);
a794dbeb 6407 provider_name = (char*)((char*)pdata->d_buf + pbe_v2->provider - (char*)probe_scn_addr);
aff5d390
SC
6408 arg_count = pbe_v2->arg_count;
6409 pc = pbe_v2->pc;
6410 if (pbe_v2->arg_string)
6411 arg_string = (char*)((char*)pdata->d_buf + pbe_v2->arg_string - (char*)probe_scn_addr);
79a0ca08
SC
6412 // skip over pbe_v2, probe_name text and provider text
6413 probe_scn_offset = ((long)(pbe_v2->name) - (long)(probe_scn_addr)) + probe_name.length();
6414 probe_scn_offset += sizeof (__uint32_t) - probe_scn_offset % sizeof (__uint32_t);
aff5d390 6415 }
edce5b67 6416 if (sess.verbose > 4)
b530b5b3 6417 clog << _("saw .probes ") << probe_name << (provider_name != "" ? _(" (provider ")+provider_name+") " : "")
aff5d390 6418 << "@0x" << hex << pc << dec << endl;
edce5b67 6419
a794dbeb
FCE
6420 if (dw.function_name_matches_pattern (probe_name, pp_mark)
6421 && ((pp_provider == "") || dw.function_name_matches_pattern (provider_name, pp_provider)))
c57ea854 6422 handle_probe_entry ();
edce5b67 6423 }
edce5b67
JS
6424}
6425
6426
6846cfc8 6427void
4ddb6dd0 6428sdt_query::record_semaphore (vector<derived_probe *> & results, unsigned start)
6846cfc8 6429{
a794dbeb
FCE
6430 for (unsigned i=0; i<2; i++) {
6431 // prefer with-provider symbol; look without provider prefix for backward compatibility only
6432 string semaphore = (i==0 ? (provider_name+"_") : "") + probe_name + "_semaphore";
6433 // XXX: multiple addresses?
6434 if (sess.verbose > 2)
b530b5b3 6435 clog << _F("looking for semaphore symbol %s ", semaphore.c_str());
a794dbeb 6436
79a0ca08
SC
6437 Dwarf_Addr addr;
6438 if (this->semaphore)
6439 addr = this->semaphore;
6440 else
6441 addr = lookup_symbol_address(dw.module, semaphore.c_str());
a794dbeb
FCE
6442 if (addr)
6443 {
7d395255 6444 if (dwfl_module_relocations (dw.module) > 0)
a794dbeb
FCE
6445 dwfl_module_relocate_address (dw.module, &addr);
6446 // XXX: relocation basis?
6447 for (unsigned i = start; i < results.size(); ++i)
6448 results[i]->sdt_semaphore_addr = addr;
6449 if (sess.verbose > 2)
b530b5b3 6450 clog << _(", found at 0x") << hex << addr << dec << endl;
a794dbeb
FCE
6451 return;
6452 }
6453 else
6454 if (sess.verbose > 2)
b530b5b3 6455 clog << _(", not found") << endl;
a794dbeb 6456 }
6846cfc8
SC
6457}
6458
6459
edce5b67
JS
6460void
6461sdt_query::convert_probe (probe *base)
6462{
6463 block *b = new block;
6464 b->tok = base->body->tok;
6465
6466 // XXX: Does this also need to happen for i386 under x86_64 stap?
ebbd2b45 6467 if (sess.architecture == "i386" && have_kprobe())
edce5b67
JS
6468 {
6469 functioncall *rp = new functioncall;
edce5b67
JS
6470 rp->function = "regparm";
6471 rp->tok = b->tok;
6472 literal_number* littid = new literal_number(0);
6473 littid->tok = b->tok;
6474 rp->args.push_back(littid);
6475 expr_statement* es = new expr_statement;
6476 es->tok = b->tok;
6477 es->value = rp;
6478 b->statements.push_back(es);
6479 }
edce5b67 6480
aff5d390 6481 if (have_kprobe())
edce5b67 6482 {
aff5d390 6483 // Generate: if (arg2 != kprobe2_type) next;
edce5b67
JS
6484 if_statement *istid = new if_statement;
6485 istid->thenblock = new next_statement;
6486 istid->elseblock = NULL;
6487 istid->tok = b->tok;
63ea4244 6488 istid->thenblock->tok = b->tok;
edce5b67
JS
6489 comparison *betid = new comparison;
6490 betid->op = "!=";
6491 betid->tok = b->tok;
6492
6493 functioncall *arg2 = new functioncall;
6494 arg2->function = "ulong_arg";
6495 arg2->tok = b->tok;
6496 literal_number* num = new literal_number(2);
6497 num->tok = b->tok;
6498 arg2->args.push_back(num);
6499
6500 betid->left = arg2;
aff5d390 6501 literal_number* littid = new literal_number(probe_type);
edce5b67
JS
6502 littid->tok = b->tok;
6503 betid->right = littid;
6504 istid->condition = betid;
6505 b->statements.push_back(istid);
6506 }
6507
6508 // Generate: if (arg1 != mark("label")) next;
6509 functioncall *fc = new functioncall;
bbafcb1e 6510 fc->function = "ulong_arg";
edce5b67 6511 fc->tok = b->tok;
bbafcb1e 6512 literal_number* num = new literal_number(1);
edce5b67
JS
6513 num->tok = b->tok;
6514 fc->args.push_back(num);
6515
6516 functioncall *fcus = new functioncall;
6517 fcus->function = "user_string";
6518 fcus->type = pe_string;
6519 fcus->tok = b->tok;
6520 fcus->args.push_back(fc);
6521
6522 if_statement *is = new if_statement;
6523 is->thenblock = new next_statement;
6524 is->elseblock = NULL;
6525 is->tok = b->tok;
63ea4244 6526 is->thenblock->tok = b->tok;
edce5b67
JS
6527 comparison *be = new comparison;
6528 be->op = "!=";
6529 be->tok = b->tok;
6530 be->left = fcus;
6531 be->right = new literal_string(probe_name);
63ea4244 6532 be->right->tok = b->tok;
edce5b67
JS
6533 is->condition = be;
6534 b->statements.push_back(is);
6535
6536 // Now replace the body
6537 b->statements.push_back(base->body);
6538 base->body = b;
6539}
6540
6541
c72aa911
JS
6542probe*
6543sdt_query::convert_location ()
edce5b67 6544{
c72aa911 6545 probe_point* specific_loc = new probe_point(*base_loc);
662539d9 6546 vector<probe_point::component*> derived_comps;
edce5b67 6547
662539d9
JS
6548 vector<probe_point::component*>::iterator it;
6549 for (it = specific_loc->components.begin();
6550 it != specific_loc->components.end(); ++it)
6551 if ((*it)->functor == TOK_PROCESS)
6552 {
6553 if (have_kprobe())
6554 // start the kernel probe_point
6555 derived_comps.push_back(new probe_point::component(TOK_KERNEL));
6556 else
6557 // copy the process name
6558 derived_comps.push_back(*it);
6559 }
6560 else if ((*it)->functor == TOK_LIBRARY)
6561 {
6562 if (!have_kprobe())
6563 // copy the library name for process probes
6564 derived_comps.push_back(*it);
6565 }
6566 else if ((*it)->functor == TOK_PROVIDER)
6567 {
6568 // replace the possibly wildcarded arg with the specific provider name
6569 *it = new probe_point::component(TOK_PROVIDER,
6570 new literal_string(provider_name));
6571 }
6572 else if ((*it)->functor == TOK_MARK)
c72aa911
JS
6573 {
6574 // replace the possibly wildcarded arg with the specific marker name
662539d9
JS
6575 *it = new probe_point::component(TOK_MARK,
6576 new literal_string(probe_name));
a794dbeb 6577
aff5d390
SC
6578 if (sess.verbose > 3)
6579 switch (probe_type)
6580 {
6581 case uprobe1_type:
b530b5b3 6582 clog << _("probe_type == uprobe1, use statement addr: 0x")
aff5d390
SC
6583 << hex << pc << dec << endl;
6584 break;
6585 case uprobe2_type:
b530b5b3 6586 clog << _("probe_type == uprobe2, use statement addr: 0x")
aff5d390
SC
6587 << hex << pc << dec << endl;
6588 break;
40fe32e0 6589 case uprobe3_type:
b530b5b3 6590 clog << _("probe_type == uprobe3, use statement addr: 0x")
40fe32e0
SC
6591 << hex << pc << dec << endl;
6592 break;
6593 case kprobe1_type:
6594 clog << "probe_type == kprobe1" << endl;
6595 break;
6596 case kprobe2_type:
6597 clog << "probe_type == kprobe2" << endl;
6598 break;
aff5d390 6599 default:
b530b5b3
LB
6600 clog << _F("probe_type == use_uprobe_no_dwarf, use label name: _stapprobe1_%s",
6601 pp_mark.c_str()) << endl;
aff5d390
SC
6602 }
6603
c72aa911
JS
6604 switch (probe_type)
6605 {
aff5d390
SC
6606 case uprobe1_type:
6607 case uprobe2_type:
40fe32e0 6608 case uprobe3_type:
c72aa911 6609 // process("executable").statement(probe_arg)
662539d9
JS
6610 derived_comps.push_back
6611 (new probe_point::component(TOK_STATEMENT,
6612 new literal_number(pc, true)));
c72aa911
JS
6613 break;
6614
aff5d390
SC
6615 case kprobe1_type:
6616 case kprobe2_type:
b642c901 6617 // kernel.function("*getegid*")
662539d9
JS
6618 derived_comps.push_back
6619 (new probe_point::component(TOK_FUNCTION,
6620 new literal_string("*getegid*")));
b642c901 6621 break;
c72aa911 6622
a794dbeb 6623 default: // deprecated
c72aa911 6624 // process("executable").function("*").label("_stapprobe1_MARK_NAME")
662539d9
JS
6625 derived_comps.push_back
6626 (new probe_point::component(TOK_FUNCTION,
6627 new literal_string("*")));
6628 derived_comps.push_back
c72aa911 6629 (new probe_point::component(TOK_LABEL,
a794dbeb 6630 new literal_string("_stapprobe1_" + pp_mark)));
c72aa911
JS
6631 break;
6632 }
6633 }
edce5b67 6634
662539d9
JS
6635 probe_point* derived_loc = new probe_point(*specific_loc);
6636 derived_loc->components = derived_comps;
c72aa911 6637 return base_probe->create_alias(derived_loc, specific_loc);
edce5b67
JS
6638}
6639
6640
51d6bda3
SC
6641void
6642sdt_query::query_library (const char *library)
6643{
6644 query_one_library (library, dw, user_lib, base_probe, base_loc, results);
6645}
6646
6647
20c6c071 6648void
5227f1ea 6649dwarf_builder::build(systemtap_session & sess,
7a053d3b 6650 probe * base,
20c6c071 6651 probe_point * location,
86bf665e 6652 literal_map_t const & parameters,
20c6c071
GH
6653 vector<derived_probe *> & finished_results)
6654{
b20febf3
FCE
6655 // NB: the kernel/user dwlfpp objects are long-lived.
6656 // XXX: but they should be per-session, as this builder object
6657 // may be reused if we try to cross-instrument multiple targets.
84048984 6658
7a24d422 6659 dwflpp* dw = 0;
6d5d594e 6660 literal_map_t filled_parameters = parameters;
7a24d422 6661
7a24d422 6662 string module_name;
ae2552da
FCE
6663 if (has_null_param (parameters, TOK_KERNEL))
6664 {
6665 dw = get_kern_dw(sess, "kernel");
6666 }
6667 else if (get_param (parameters, TOK_MODULE, module_name))
b8da0ad1 6668 {
c523a015
LB
6669 size_t dash_pos = 0;
6670 while((dash_pos=module_name.find('-'))!=string::npos)
6671 module_name.replace(int(dash_pos),1,"_");
6672 filled_parameters[TOK_MODULE] = new literal_string(module_name);
37001baa
FCE
6673 // NB: glob patterns get expanded later, during the offline
6674 // elfutils module listing.
ae2552da 6675 dw = get_kern_dw(sess, module_name);
b8da0ad1 6676 }
6d5d594e 6677 else if (get_param (parameters, TOK_PROCESS, module_name) || has_null_param(parameters, TOK_PROCESS))
84c84ac4 6678 {
05fb3e0c 6679 module_name = sess.sysroot + module_name;
6d5d594e
LB
6680 if(has_null_param(filled_parameters, TOK_PROCESS))
6681 {
6682 wordexp_t words;
6683 int rc = wordexp(sess.cmd.c_str(), &words, WRDE_NOCMD|WRDE_UNDEF);
6684 if(rc || words.we_wordc <= 0)
6685 throw semantic_error(_("unspecified process probe is invalid without a -c COMMAND"));
05fb3e0c 6686 module_name = sess.sysroot + words.we_wordv[0];
6d5d594e
LB
6687 filled_parameters[TOK_PROCESS] = new literal_string(module_name);// this needs to be used in place of the blank map
6688 // in the case of TOK_MARK we need to modify locations as well
6689 if(location->components[0]->functor==TOK_PROCESS &&
6690 location->components[0]->arg == 0)
6691 location->components[0]->arg = new literal_string(module_name);
6692 wordfree (& words);
6693 }
5750ecc6 6694
37001baa
FCE
6695 // PR6456 process("/bin/*") glob handling
6696 if (contains_glob_chars (module_name))
6697 {
6698 // Expand glob via rewriting the probe-point process("....")
6699 // parameter, asserted to be the first one.
6700
6701 assert (location->components.size() > 0);
6702 assert (location->components[0]->functor == TOK_PROCESS);
6703 assert (location->components[0]->arg);
6704 literal_string* lit = dynamic_cast<literal_string*>(location->components[0]->arg);
6705 assert (lit);
6706
6707 // Evaluate glob here, and call derive_probes recursively with each match.
6708 glob_t the_blob;
6709 int rc = glob (module_name.c_str(), 0, NULL, & the_blob);
b530b5b3
LB
6710 if (rc)
6711 throw semantic_error (_F("glob %s error (%s)", module_name.c_str(), lex_cast(rc).c_str() ));
37001baa
FCE
6712 for (unsigned i = 0; i < the_blob.gl_pathc; ++i)
6713 {
6714 if (pending_interrupts) return;
6715
6716 const char* globbed = the_blob.gl_pathv[i];
6717 struct stat st;
6718
6719 if (access (globbed, X_OK) == 0
6720 && stat (globbed, &st) == 0
6721 && S_ISREG (st.st_mode)) // see find_executable()
6722 {
7977a734
FCE
6723 // Need to call canonicalize here, in order to path-expand
6724 // patterns like process("stap*"). Otherwise it may go through
6725 // to the next round of expansion as ("stap"), leading to a $PATH
6726 // search that's not consistent with the glob search already done.
6727
6728 char *cf = canonicalize_file_name (globbed);
6729 if (cf) globbed = cf;
6730
37001baa
FCE
6731 // synthesize a new probe_point, with the glob-expanded string
6732 probe_point *pp = new probe_point (*location);
5750ecc6
FCE
6733 // PR13338: quote results to prevent recursion
6734 string eglobbed = escape_glob_chars (globbed);
6735
6736 if (sess.verbose > 1)
6737 clog << _F("Expanded process(\"%s\") to process(\"%s\")",
6738 module_name.c_str(), eglobbed.c_str()) << endl;
05fb3e0c 6739 string eglobbed_tgt = path_remove_sysroot(sess, eglobbed);
5750ecc6 6740
37001baa 6741 probe_point::component* ppc = new probe_point::component (TOK_PROCESS,
05fb3e0c 6742 new literal_string (eglobbed_tgt));
37001baa
FCE
6743 ppc->tok = location->components[0]->tok; // overwrite [0] slot, pattern matched above
6744 pp->components[0] = ppc;
6745
7977a734
FCE
6746 probe* new_probe = new probe (*base, pp);
6747
6748 // We override "optional = true" here, as if the
6749 // wildcarded probe point was given a "?" suffix.
6750
6751 // This is because wildcard probes will be expected
6752 // by users to apply only to some subset of the
6753 // matching binaries, in the sense of "any", rather
6754 // than "all", sort of similarly how
6755 // module("*").function("...") patterns work.
6756
6757 derive_probes (sess, new_probe, finished_results,
6758 true /* NB: not location->optional */ );
37001baa
FCE
6759 }
6760 }
6761
6762 globfree (& the_blob);
6763 return; // avoid falling through
6764 }
6765
5750ecc6
FCE
6766 // PR13338: unquote glob results
6767 module_name = unescape_glob_chars (module_name);
05fb3e0c 6768 user_path = find_executable (module_name, "", sess.sysenv); // canonicalize it
d1bcbe71
RH
6769
6770 // if the executable starts with "#!", we look for the interpreter of the script
6771 {
6772 ifstream script_file (user_path.c_str () );
6773
6774 if (script_file.good ())
6775 {
6776 string line;
6777
6778 getline (script_file, line);
6779
6780 if (line.compare (0, 2, "#!") == 0)
6781 {
6782 string path_head = line.substr(2);
6783
6784 // remove white spaces at the beginning of the string
6785 size_t p2 = path_head.find_first_not_of(" \t");
6786
6787 if (p2 != string::npos)
6788 {
6789 string path = path_head.substr(p2);
6790
6791 // remove white spaces at the end of the string
6792 p2 = path.find_last_not_of(" \t\n");
6793 if (string::npos != p2)
6794 path.erase(p2+1);
6795
8e13c1a1
RH
6796 // handle "#!/usr/bin/env" redirect
6797 size_t offset = 0;
6798 if (path.compare(0, sizeof("/bin/env")-1, "/bin/env") == 0)
6799 {
6800 offset = sizeof("/bin/env")-1;
6801 }
6802 else if (path.compare(0, sizeof("/usr/bin/env")-1, "/usr/bin/env") == 0)
6803 {
6804 offset = sizeof("/usr/bin/env")-1;
6805 }
6806
6807 if (offset != 0)
6808 {
6809 size_t p3 = path.find_first_not_of(" \t", offset);
6810
6811 if (p3 != string::npos)
6812 {
6813 string env_path = path.substr(p3);
05fb3e0c
WF
6814 user_path = find_executable (env_path, sess.sysroot,
6815 sess.sysenv);
8e13c1a1
RH
6816 }
6817 }
6818 else
6819 {
05fb3e0c 6820 user_path = find_executable (path, sess.sysroot, sess.sysenv);
8e13c1a1 6821 }
d1bcbe71
RH
6822
6823 struct stat st;
6824
6825 if (access (user_path.c_str(), X_OK) == 0
6826 && stat (user_path.c_str(), &st) == 0
6827 && S_ISREG (st.st_mode)) // see find_executable()
6828 {
6829 if (sess.verbose > 1)
b530b5b3
LB
6830 clog << _F("Expanded process(\"%s\") to process(\"%s\")",
6831 module_name.c_str(), user_path.c_str()) << endl;
d1bcbe71
RH
6832
6833 assert (location->components.size() > 0);
6834 assert (location->components[0]->functor == TOK_PROCESS);
6835 assert (location->components[0]->arg);
6836 literal_string* lit = dynamic_cast<literal_string*>(location->components[0]->arg);
6837 assert (lit);
6838
6839 // synthesize a new probe_point, with the expanded string
6840 probe_point *pp = new probe_point (*location);
05fb3e0c 6841 string user_path_tgt = path_remove_sysroot(sess, user_path);
d1bcbe71 6842 probe_point::component* ppc = new probe_point::component (TOK_PROCESS,
05fb3e0c 6843 new literal_string (user_path_tgt.c_str()));
d1bcbe71
RH
6844 ppc->tok = location->components[0]->tok; // overwrite [0] slot, pattern matched above
6845 pp->components[0] = ppc;
6846
6847 probe* new_probe = new probe (*base, pp);
6848
6849 derive_probes (sess, new_probe, finished_results);
6850
6851 script_file.close();
6852 return;
6853 }
6854 }
6855 }
6856 }
6857 script_file.close();
6858 }
6859
47e226ed 6860 if (get_param (parameters, TOK_LIBRARY, user_lib)
378d78b5 6861 && user_lib.length() && ! contains_glob_chars (user_lib))
47e226ed
SC
6862 {
6863 module_name = find_executable (user_lib, sess.sysroot, sess.sysenv,
6864 "LD_LIBRARY_PATH");
6865 if (module_name.find('/') == string::npos)
6866 // We didn't find user_lib so use iterate_over_libraries
6867 module_name = user_path;
6868 }
63b4fd14 6869 else
b642c901 6870 module_name = user_path; // canonicalize it
d0a7f5a9 6871
2b69faaf
JS
6872 if (kernel_supports_inode_uprobes(sess))
6873 {
64e807c2 6874 // XXX: autoconf this?
2b69faaf
JS
6875 if (has_null_param(parameters, TOK_RETURN))
6876 throw semantic_error
6877 (_("process return probes not available with inode-based uprobes"));
6878 }
f4000852
MW
6879 // There is a similar check in pass 4 (buildrun), but it is
6880 // needed here too to make sure alternatives for optional
6881 // (? or !) process probes are disposed and/or alternatives
6882 // are selected.
5261f7ab 6883 check_process_probe_kernel_support(sess);
e34d5d13 6884
7a24d422
FCE
6885 // user-space target; we use one dwflpp instance per module name
6886 // (= program or shared library)
707bf35e 6887 dw = get_user_dw(sess, module_name);
c8959a29 6888 }
20c6c071 6889
5896cd05 6890 if (sess.verbose > 3)
b530b5b3 6891 clog << _F("dwarf_builder::build for %s", module_name.c_str()) << endl;
5896cd05 6892
a794dbeb
FCE
6893 string dummy_mark_name; // NB: PR10245: dummy value, need not substitute - => __
6894 if (get_param(parameters, TOK_MARK, dummy_mark_name))
f28a8c28 6895 {
51d6bda3 6896 sdt_query sdtq(base, location, *dw, filled_parameters, finished_results, user_lib);
edce5b67
JS
6897 dw->iterate_over_modules(&query_module, &sdtq);
6898 return;
7a05f484 6899 }
20c6c071 6900
8f14e444 6901 unsigned results_pre = finished_results.size();
6d5d594e 6902 dwarf_query q(base, location, *dw, filled_parameters, finished_results, user_path, user_lib);
7a24d422
FCE
6903
6904 // XXX: kernel.statement.absolute is a special case that requires no
6905 // dwfl processing. This code should be in a separate builder.
7a24d422 6906 if (q.has_kernel && q.has_absolute)
37ebca01 6907 {
4baf0e53 6908 // assert guru mode for absolute probes
37ebca01
FCE
6909 if (! q.base_probe->privileged)
6910 {
e3bbc038 6911 throw semantic_error (_("absolute statement probe in unprivileged script; need stap -g"),
edce5b67 6912 q.base_probe->tok);
37ebca01
FCE
6913 }
6914
6915 // For kernel.statement(NUM).absolute probe points, we bypass
6916 // all the debuginfo stuff: We just wire up a
6917 // dwarf_derived_probe right here and now.
4baf0e53 6918 dwarf_derived_probe* p =
b8da0ad1
FCE
6919 new dwarf_derived_probe ("", "", 0, "kernel", "",
6920 q.statement_num_val, q.statement_num_val,
6921 q, 0);
37ebca01 6922 finished_results.push_back (p);
1a0dbc5a 6923 sess.unwindsym_modules.insert ("kernel");
37ebca01
FCE
6924 return;
6925 }
6926
51178501 6927 dw->iterate_over_modules(&query_module, &q);
8f14e444
FCE
6928
6929
6930 // PR11553 special processing: .return probes requested, but
6931 // some inlined function instances matched.
6932 unsigned i_n_r = q.inlined_non_returnable.size();
6933 unsigned results_post = finished_results.size();
6934 if (i_n_r > 0)
6935 {
6936 if ((results_pre == results_post) && (! sess.suppress_warnings)) // no matches; issue warning
6937 {
6938 string quicklist;
6939 for (set<string>::iterator it = q.inlined_non_returnable.begin();
6940 it != q.inlined_non_returnable.end();
6941 it++)
6942 {
6943 quicklist += " " + (*it);
6944 if (quicklist.size() > 80) // heuristic, don't make an overlong report line
6945 {
6946 quicklist += " ...";
6947 break;
6948 }
6949 }
c57ea854 6950
b530b5b3
LB
6951 sess.print_warning (_F(ngettext("cannot probe .return of %u inlined function %s",
6952 "cannot probe .return of %u inlined functions %s",
6953 quicklist.size()), i_n_r, quicklist.c_str()));
8f14e444
FCE
6954 // There will be also a "no matches" semantic error generated.
6955 }
6956 if (sess.verbose > 1)
b530b5b3
LB
6957 clog << _F(ngettext("skipped .return probe of %u inlined function",
6958 "skipped .return probe of %u inlined functions", i_n_r), i_n_r) << endl;
8f14e444
FCE
6959 if ((sess.verbose > 3) || (sess.verbose > 2 && results_pre == results_post)) // issue details with high verbosity
6960 {
6961 for (set<string>::iterator it = q.inlined_non_returnable.begin();
6962 it != q.inlined_non_returnable.end();
6963 it++)
6964 clog << (*it) << " ";
6965 clog << endl;
6966 }
6967 } // i_n_r > 0
5f0a03a6
JK
6968}
6969
6970symbol_table::~symbol_table()
6971{
c9efa5c9 6972 delete_map(map_by_addr);
5f0a03a6
JK
6973}
6974
6975void
2867a2a1 6976symbol_table::add_symbol(const char *name, bool weak, bool descriptor,
822a6a3d 6977 Dwarf_Addr addr, Dwarf_Addr */*high_addr*/)
5f0a03a6 6978{
ab91b232
JK
6979#ifdef __powerpc__
6980 // Map ".sys_foo" to "sys_foo".
6981 if (name[0] == '.')
6982 name++;
6983#endif
5f0a03a6
JK
6984 func_info *fi = new func_info();
6985 fi->addr = addr;
6986 fi->name = name;
ab91b232 6987 fi->weak = weak;
2867a2a1 6988 fi->descriptor = descriptor;
5f0a03a6
JK
6989 map_by_name[fi->name] = fi;
6990 // TODO: Use a multimap in case there are multiple static
6991 // functions with the same name?
1c6b77e5 6992 map_by_addr.insert(make_pair(addr, fi));
5f0a03a6
JK
6993}
6994
6995enum info_status
6996symbol_table::read_symbols(FILE *f, const string& path)
6997{
6998 // Based on do_kernel_symbols() in runtime/staprun/symbols.c
6999 int ret;
2e67a43b
TM
7000 char *name = 0;
7001 char *mod = 0;
5f0a03a6
JK
7002 char type;
7003 unsigned long long addr;
7004 Dwarf_Addr high_addr = 0;
7005 int line = 0;
7006
7007 // %as (non-POSIX) mallocs space for the string and stores its address.
7008 while ((ret = fscanf(f, "%llx %c %as [%as", &addr, &type, &name, &mod)) > 0)
7009 {
2e67a43b
TM
7010 auto_free free_name(name);
7011 auto_free free_mod(mod);
5f0a03a6
JK
7012 line++;
7013 if (ret < 3)
7014 {
3d372d6b 7015 cerr << _F("Symbol table error: Line %d of symbol list from %s is not in correct format: address type name [module]\n",
b530b5b3 7016 line, path.c_str());
5f0a03a6
JK
7017 // Caller should delete symbol_table object.
7018 return info_absent;
7019 }
2e67a43b 7020 else if (ret > 3)
5f0a03a6
JK
7021 {
7022 // Modules are loaded above the kernel, so if we're getting
7023 // modules, we're done.
2e67a43b 7024 break;
5f0a03a6 7025 }
ab91b232 7026 if (type == 'T' || type == 't' || type == 'W')
2867a2a1 7027 add_symbol(name, (type == 'W'), false, (Dwarf_Addr) addr, &high_addr);
5f0a03a6
JK
7028 }
7029
1c6b77e5 7030 if (map_by_addr.size() < 1)
5f0a03a6 7031 {
3d372d6b 7032 cerr << _F("Symbol table error: %s contains no function symbols.\n",
b530b5b3 7033 path.c_str()) << endl;
5f0a03a6
JK
7034 return info_absent;
7035 }
7036 return info_present;
7037}
7038
7039// NB: This currently unused. We use get_from_elf() instead because
7040// that gives us raw addresses -- which we need for modules -- whereas
7041// nm provides the address relative to the beginning of the section.
7042enum info_status
83ca3872 7043symbol_table::read_from_elf_file(const string &path,
2713ea24 7044 systemtap_session &sess)
5f0a03a6 7045{
58502ae4
JS
7046 vector<string> cmd;
7047 cmd.push_back("/usr/bin/nm");
7048 cmd.push_back("-n");
7049 cmd.push_back("--defined-only");
7050 cmd.push_back("path");
7051
5f0a03a6 7052 FILE *f;
58502ae4
JS
7053 int child_fd;
7054 pid_t child = stap_spawn_piped(sess.verbose, cmd, NULL, &child_fd);
7055 if (child <= 0 || !(f = fdopen(child_fd, "r")))
5f0a03a6 7056 {
58502ae4 7057 // nm failures are detected by stap_waitpid
3d372d6b 7058 cerr << _F("Internal error reading symbol table from %s -- %s\n",
b530b5b3 7059 path.c_str(), strerror(errno));
5f0a03a6
JK
7060 return info_absent;
7061 }
7062 enum info_status status = read_symbols(f, path);
58502ae4 7063 if (fclose(f) || stap_waitpid(sess.verbose, child))
5f0a03a6 7064 {
2713ea24
CM
7065 if (status == info_present)
7066 sess.print_warning("nm cannot read symbol table from " + path);
5f0a03a6
JK
7067 return info_absent;
7068 }
7069 return status;
7070}
7071
7072enum info_status
83ca3872 7073symbol_table::read_from_text_file(const string& path,
2713ea24 7074 systemtap_session &sess)
5f0a03a6
JK
7075{
7076 FILE *f = fopen(path.c_str(), "r");
7077 if (!f)
7078 {
2713ea24 7079 sess.print_warning("cannot read symbol table from " + path + " -- " + strerror(errno));
5f0a03a6
JK
7080 return info_absent;
7081 }
7082 enum info_status status = read_symbols(f, path);
7083 (void) fclose(f);
7084 return status;
7085}
7086
46f7b6be 7087void
f98c6346 7088symbol_table::prepare_section_rejection(Dwfl_Module *mod __attribute__ ((unused)))
46f7b6be
JK
7089{
7090#ifdef __powerpc__
7091 /*
7092 * The .opd section contains function descriptors that can look
7093 * just like function entry points. For example, there's a function
7094 * descriptor called "do_exit" that links to the entry point ".do_exit".
7095 * Reject all symbols in .opd.
7096 */
7097 opd_section = SHN_UNDEF;
7098 Dwarf_Addr bias;
7099 Elf* elf = (dwarf_getelf (dwfl_module_getdwarf (mod, &bias))
7100 ?: dwfl_module_getelf (mod, &bias));
7101 Elf_Scn* scn = 0;
7102 size_t shstrndx;
7103
7104 if (!elf)
7105 return;
fcc30d6d 7106 if (elf_getshdrstrndx (elf, &shstrndx) != 0)
46f7b6be
JK
7107 return;
7108 while ((scn = elf_nextscn(elf, scn)) != NULL)
7109 {
7110 GElf_Shdr shdr_mem;
7111 GElf_Shdr *shdr = gelf_getshdr(scn, &shdr_mem);
7112 if (!shdr)
7113 continue;
7114 const char *name = elf_strptr(elf, shstrndx, shdr->sh_name);
7115 if (!strcmp(name, ".opd"))
7116 {
7117 opd_section = elf_ndxscn(scn);
7118 return;
7119 }
7120 }
7121#endif
7122}
7123
7124bool
7125symbol_table::reject_section(GElf_Word section)
7126{
7127 if (section == SHN_UNDEF)
7128 return true;
7129#ifdef __powerpc__
7130 if (section == opd_section)
7131 return true;
7132#endif
7133 return false;
7134}
7135
5f0a03a6
JK
7136enum info_status
7137symbol_table::get_from_elf()
7138{
7139 Dwarf_Addr high_addr = 0;
7140 Dwfl_Module *mod = mod_info->mod;
7141 int syments = dwfl_module_getsymtab(mod);
7142 assert(syments);
46f7b6be 7143 prepare_section_rejection(mod);
5f0a03a6
JK
7144 for (int i = 1; i < syments; ++i)
7145 {
7146 GElf_Sym sym;
ab91b232
JK
7147 GElf_Word section;
7148 const char *name = dwfl_module_getsym(mod, i, &sym, &section);
2867a2a1 7149 if (name && GELF_ST_TYPE(sym.st_info) == STT_FUNC)
ab91b232 7150 add_symbol(name, (GELF_ST_BIND(sym.st_info) == STB_WEAK),
2867a2a1 7151 reject_section(section), sym.st_value, &high_addr);
5f0a03a6
JK
7152 }
7153 return info_present;
7154}
7155
5f0a03a6
JK
7156func_info *
7157symbol_table::get_func_containing_address(Dwarf_Addr addr)
7158{
1c6b77e5
JS
7159 iterator_t iter = map_by_addr.upper_bound(addr);
7160 if (iter == map_by_addr.begin())
5f0a03a6 7161 return NULL;
2e67a43b 7162 else
1c6b77e5 7163 return (--iter)->second;
5f0a03a6
JK
7164}
7165
3d372d6b
SC
7166func_info *
7167symbol_table::get_first_func()
7168{
7169 iterator_t iter = map_by_addr.begin();
7170 return (iter)->second;
7171}
7172
5f0a03a6
JK
7173func_info *
7174symbol_table::lookup_symbol(const string& name)
7175{
7176 map<string, func_info*>::iterator i = map_by_name.find(name);
7177 if (i == map_by_name.end())
7178 return NULL;
7179 return i->second;
7180}
7181
7182Dwarf_Addr
7183symbol_table::lookup_symbol_address(const string& name)
7184{
7185 func_info *fi = lookup_symbol(name);
7186 if (fi)
7187 return fi->addr;
7188 return 0;
7189}
7190
ab91b232
JK
7191// This is the kernel symbol table. The kernel macro cond_syscall creates
7192// a weak symbol for each system call and maps it to sys_ni_syscall.
7193// For system calls not implemented elsewhere, this weak symbol shows up
7194// in the kernel symbol table. Following the precedent of dwarfful stap,
7195// we refuse to consider such symbols. Here we delete them from our
7196// symbol table.
7197// TODO: Consider generalizing this and/or making it part of blacklist
7198// processing.
7199void
7200symbol_table::purge_syscall_stubs()
7201{
7202 Dwarf_Addr stub_addr = lookup_symbol_address("sys_ni_syscall");
7203 if (stub_addr == 0)
7204 return;
1c6b77e5 7205 range_t purge_range = map_by_addr.equal_range(stub_addr);
2e67a43b
TM
7206 for (iterator_t iter = purge_range.first;
7207 iter != purge_range.second;
1c6b77e5 7208 )
ab91b232 7209 {
1c6b77e5 7210 func_info *fi = iter->second;
2e67a43b 7211 if (fi->weak && fi->name != "sys_ni_syscall")
ab91b232 7212 {
2e67a43b 7213 map_by_name.erase(fi->name);
1c6b77e5 7214 map_by_addr.erase(iter++);
2e67a43b 7215 delete fi;
2e67a43b 7216 }
1c6b77e5
JS
7217 else
7218 iter++;
ab91b232
JK
7219 }
7220}
7221
5f0a03a6
JK
7222void
7223module_info::get_symtab(dwarf_query *q)
7224{
7225 systemtap_session &sess = q->sess;
7226
1c6b77e5
JS
7227 if (symtab_status != info_unknown)
7228 return;
7229
5f0a03a6
JK
7230 sym_table = new symbol_table(this);
7231 if (!elf_path.empty())
7232 {
2713ea24
CM
7233 if (name == TOK_KERNEL && !sess.kernel_symtab_path.empty())
7234 sess.print_warning("reading symbol table from " + elf_path + " -- ignoring " + sess.kernel_symtab_path.c_str());
5f0a03a6
JK
7235 symtab_status = sym_table->get_from_elf();
7236 }
7237 else
7238 {
7239 assert(name == TOK_KERNEL);
7240 if (sess.kernel_symtab_path.empty())
7241 {
7242 symtab_status = info_absent;
3d372d6b 7243 cerr << _("Error: Cannot find vmlinux.\n"
b530b5b3 7244 " Consider using --kmap instead of --kelf.")
5f0a03a6
JK
7245 << endl;;
7246 }
7247 else
7248 {
7249 symtab_status =
83ca3872 7250 sym_table->read_from_text_file(sess.kernel_symtab_path, sess);
5f0a03a6
JK
7251 if (symtab_status == info_present)
7252 {
7253 sess.sym_kprobes_text_start =
7254 sym_table->lookup_symbol_address("__kprobes_text_start");
7255 sess.sym_kprobes_text_end =
7256 sym_table->lookup_symbol_address("__kprobes_text_end");
7257 sess.sym_stext = sym_table->lookup_symbol_address("_stext");
5f0a03a6
JK
7258 }
7259 }
7260 }
7261 if (symtab_status == info_absent)
7262 {
7263 delete sym_table;
7264 sym_table = NULL;
7265 return;
7266 }
7267
ab91b232
JK
7268 if (name == TOK_KERNEL)
7269 sym_table->purge_syscall_stubs();
5f0a03a6
JK
7270}
7271
1c6b77e5
JS
7272// update_symtab reconciles data between the elf symbol table and the dwarf
7273// function enumeration. It updates the symbol table entries with the dwarf
7274// die that describes the function, which also signals to query_module_symtab
7275// that a statement probe isn't needed. In return, it also adds aliases to the
7276// function table for names that share the same addr/die.
7277void
7278module_info::update_symtab(cu_function_cache_t *funcs)
7279{
7280 if (!sym_table)
7281 return;
7282
7283 cu_function_cache_t new_funcs;
7284
7285 for (cu_function_cache_t::iterator func = funcs->begin();
7286 func != funcs->end(); func++)
7287 {
7288 // optimization: inlines will never be in the symbol table
7289 if (dwarf_func_inline(&func->second) != 0)
7290 continue;
7291
1ffb8bd1
JS
7292 // XXX We may want to make additional efforts to match mangled elf names
7293 // to dwarf too. MIPS_linkage_name can help, but that's sometimes
7294 // missing, so we may also need to try matching by address. See also the
7295 // notes about _Z in dwflpp::iterate_over_functions().
7296
1c6b77e5
JS
7297 func_info *fi = sym_table->lookup_symbol(func->first);
7298 if (!fi)
7299 continue;
7300
7301 // iterate over all functions at the same address
7302 symbol_table::range_t er = sym_table->map_by_addr.equal_range(fi->addr);
7303 for (symbol_table::iterator_t it = er.first; it != er.second; ++it)
7304 {
7305 // update this function with the dwarf die
7306 it->second->die = func->second;
7307
7308 // if this function is a new alias, then
7309 // save it to merge into the function cache
7310 if (it->second != fi)
b7478964 7311 new_funcs.insert(make_pair(it->second->name, it->second->die));
1c6b77e5
JS
7312 }
7313 }
7314
7315 // add all discovered aliases back into the function cache
7316 // NB: this won't replace any names that dwarf may have already found
7317 funcs->insert(new_funcs.begin(), new_funcs.end());
7318}
7319
5f0a03a6
JK
7320module_info::~module_info()
7321{
7322 if (sym_table)
7323 delete sym_table;
b55bc428
FCE
7324}
7325
935447c8 7326// ------------------------------------------------------------------------
888af770 7327// user-space probes
935447c8
DS
7328// ------------------------------------------------------------------------
7329
935447c8 7330
888af770 7331struct uprobe_derived_probe_group: public generic_dpg<uprobe_derived_probe>
935447c8 7332{
89ba3085
FCE
7333private:
7334 string make_pbm_key (uprobe_derived_probe* p) {
cfcab6c7 7335 return p->path + "|" + p->module + "|" + p->section + "|" + lex_cast(p->pid);
89ba3085
FCE
7336 }
7337
cfcab6c7
JS
7338 void emit_module_maxuprobes (systemtap_session& s);
7339
2b69faaf
JS
7340 // Using our own utrace-based uprobes
7341 void emit_module_utrace_decls (systemtap_session& s);
7342 void emit_module_utrace_init (systemtap_session& s);
7343 void emit_module_utrace_exit (systemtap_session& s);
7344
7345 // Using the upstream inode-based uprobes
7346 void emit_module_inode_decls (systemtap_session& s);
7347 void emit_module_inode_init (systemtap_session& s);
7348 void emit_module_inode_exit (systemtap_session& s);
7349
935447c8 7350public:
888af770 7351 void emit_module_decls (systemtap_session& s);
935447c8
DS
7352 void emit_module_init (systemtap_session& s);
7353 void emit_module_exit (systemtap_session& s);
7354};
7355
7356
888af770
FCE
7357void
7358uprobe_derived_probe::join_group (systemtap_session& s)
7359{
7360 if (! s.uprobe_derived_probes)
7361 s.uprobe_derived_probes = new uprobe_derived_probe_group ();
7362 s.uprobe_derived_probes->enroll (this);
93646f4d 7363 enable_task_finder(s);
a96d1db0 7364
8a03658e 7365 // Ask buildrun.cxx to build extra module if needed, and
d3e959b0
DS
7366 // signal staprun to load that module. If we're using the builtin
7367 // inode-uprobes, we still need to know that it is required.
8a03658e 7368 s.need_uprobes = true;
a96d1db0
DN
7369}
7370
888af770 7371
c0f84e7b
SC
7372void
7373uprobe_derived_probe::getargs(std::list<std::string> &arg_set) const
7374{
7375 dwarf_derived_probe::getargs(arg_set);
7376 arg_set.insert(arg_set.end(), args.begin(), args.end());
7377}
7378
7379
7380void
7381uprobe_derived_probe::saveargs(int nargs)
7382{
7383 for (int i = 1; i <= nargs; i++)
7384 args.push_back("$arg" + lex_cast (i) + ":long");
7385}
7386
7387
2865d17a 7388void
42e38653 7389uprobe_derived_probe::emit_privilege_assertion (translator_output* o)
2865d17a
DB
7390{
7391 // These probes are allowed for unprivileged users, but only in the
7392 // context of processes which they own.
7393 emit_process_owner_assertion (o);
7394}
7395
7396
888af770 7397struct uprobe_builder: public derived_probe_builder
a96d1db0 7398{
888af770 7399 uprobe_builder() {}
2b69faaf 7400 virtual void build(systemtap_session & sess,
a96d1db0
DN
7401 probe * base,
7402 probe_point * location,
86bf665e 7403 literal_map_t const & parameters,
a96d1db0
DN
7404 vector<derived_probe *> & finished_results)
7405 {
888af770 7406 int64_t process, address;
a96d1db0 7407
2b69faaf
JS
7408 if (kernel_supports_inode_uprobes(sess))
7409 throw semantic_error (_("absolute process probes not available with inode-based uprobes"));
7410
888af770 7411 bool b1 = get_param (parameters, TOK_PROCESS, process);
ced347a9 7412 (void) b1;
888af770 7413 bool b2 = get_param (parameters, TOK_STATEMENT, address);
ced347a9 7414 (void) b2;
888af770
FCE
7415 bool rr = has_null_param (parameters, TOK_RETURN);
7416 assert (b1 && b2); // by pattern_root construction
a96d1db0 7417
0973d815 7418 finished_results.push_back(new uprobe_derived_probe(base, location, process, address, rr));
a96d1db0
DN
7419 }
7420};
7421
7422
7423void
cfcab6c7 7424uprobe_derived_probe_group::emit_module_maxuprobes (systemtap_session& s)
a96d1db0 7425{
43241c44
FCE
7426 // We'll probably need at least this many:
7427 unsigned minuprobes = probes.size();
7428 // .. but we don't want so many that .bss is inflated (PR10507):
7429 unsigned uprobesize = 64;
7430 unsigned maxuprobesmem = 10*1024*1024; // 10 MB
7431 unsigned maxuprobes = maxuprobesmem / uprobesize;
7432
aaf7ffe8
FCE
7433 // Let's choose a value on the geometric middle. This should end up
7434 // between minuprobes and maxuprobes. It's OK if this number turns
7435 // out to be < minuprobes or > maxuprobes. At worst, we get a
7436 // run-time error of one kind (too few: missed uprobe registrations)
7437 // or another (too many: vmalloc errors at module load time).
7438 unsigned default_maxuprobes = (unsigned)sqrt((double)minuprobes * (double)maxuprobes);
43241c44 7439
6d0f3f0c 7440 s.op->newline() << "#ifndef MAXUPROBES";
43241c44 7441 s.op->newline() << "#define MAXUPROBES " << default_maxuprobes;
6d0f3f0c 7442 s.op->newline() << "#endif";
cfcab6c7
JS
7443}
7444
7445
7446void
7447uprobe_derived_probe_group::emit_module_utrace_decls (systemtap_session& s)
7448{
7449 if (probes.empty()) return;
7450 s.op->newline() << "/* ---- utrace uprobes ---- */";
7451 // If uprobes isn't in the kernel, pull it in from the runtime.
7452
7453 s.op->newline() << "#if defined(CONFIG_UPROBES) || defined(CONFIG_UPROBES_MODULE)";
7454 s.op->newline() << "#include <linux/uprobes.h>";
7455 s.op->newline() << "#else";
7456 s.op->newline() << "#include \"uprobes/uprobes.h\"";
7457 s.op->newline() << "#endif";
7458 s.op->newline() << "#ifndef UPROBES_API_VERSION";
7459 s.op->newline() << "#define UPROBES_API_VERSION 1";
7460 s.op->newline() << "#endif";
7461
7462 emit_module_maxuprobes (s);
a96d1db0 7463
cc52276b
WC
7464 // Forward decls
7465 s.op->newline() << "#include \"uprobes-common.h\"";
7466
5e112f92
FCE
7467 // In .bss, the shared pool of uprobe/uretprobe structs. These are
7468 // too big to embed in the initialized .data stap_uprobe_spec array.
cc52276b
WC
7469 // XXX: consider a slab cache or somesuch for stap_uprobes
7470 s.op->newline() << "static struct stap_uprobe stap_uprobes [MAXUPROBES];";
5e112f92 7471 s.op->newline() << "DEFINE_MUTEX(stap_uprobes_lock);"; // protects against concurrent registration/unregistration
a96d1db0 7472
89ba3085
FCE
7473 s.op->assert_0_indent();
7474
89ba3085
FCE
7475 // Assign task-finder numbers as we build up the stap_uprobe_tf table.
7476 // This means we process probes[] in two passes.
7477 map <string,unsigned> module_index;
7478 unsigned module_index_ctr = 0;
7479
cc52276b
WC
7480 // not const since embedded task_finder_target struct changes
7481 s.op->newline() << "static struct stap_uprobe_tf stap_uprobe_finders[] = {";
89ba3085
FCE
7482 s.op->indent(1);
7483 for (unsigned i=0; i<probes.size(); i++)
7484 {
7485 uprobe_derived_probe *p = probes[i];
7486 string pbmkey = make_pbm_key (p);
7487 if (module_index.find (pbmkey) == module_index.end())
7488 {
7489 module_index[pbmkey] = module_index_ctr++;
7490
7491 s.op->newline() << "{";
7492 // NB: it's essential that make_pbm_key() use all of and
7493 // only the same fields as we're about to emit.
7494 s.op->line() << " .finder={";
7495 if (p->pid != 0)
68910c97
JK
7496 s.op->line() << " .pid=" << p->pid << ",";
7497
7498 if (p->section == "") // .statement(addr).absolute
7499 s.op->line() << " .callback=&stap_uprobe_process_found,";
89ba3085
FCE
7500 else if (p->section == ".absolute") // proxy for ET_EXEC -> exec()'d program
7501 {
7502 s.op->line() << " .procname=" << lex_cast_qstring(p->module) << ",";
7503 s.op->line() << " .callback=&stap_uprobe_process_found,";
7504 }
68910c97 7505 else if (p->section != ".absolute") // ET_DYN
89ba3085 7506 {
4ad95bbc
SC
7507 if (p->has_library)
7508 s.op->line() << " .procname=\"" << p->path << "\", ";
89ba3085
FCE
7509 s.op->line() << " .mmap_callback=&stap_uprobe_mmap_found, ";
7510 s.op->line() << " .munmap_callback=&stap_uprobe_munmap_found, ";
19d91f6c 7511 s.op->line() << " .callback=&stap_uprobe_process_munmap,";
89ba3085 7512 }
89ba3085 7513 s.op->line() << " },";
68910c97
JK
7514 if (p->module != "")
7515 s.op->line() << " .pathname=" << lex_cast_qstring(p->module) << ", ";
89ba3085
FCE
7516 s.op->line() << " },";
7517 }
c57ea854 7518 else
822a6a3d 7519 { } // skip it in this pass, already have a suitable stap_uprobe_tf slot for it.
89ba3085
FCE
7520 }
7521 s.op->newline(-1) << "};";
7522
7523 s.op->assert_0_indent();
7524
cc52276b
WC
7525 // NB: read-only structure
7526 s.op->newline() << "static const struct stap_uprobe_spec stap_uprobe_specs [] = {";
a96d1db0 7527 s.op->indent(1);
888af770
FCE
7528 for (unsigned i =0; i<probes.size(); i++)
7529 {
7530 uprobe_derived_probe* p = probes[i];
7531 s.op->newline() << "{";
89ba3085
FCE
7532 string key = make_pbm_key (p);
7533 unsigned value = module_index[key];
759e1d76
FCE
7534 if (value != 0)
7535 s.op->line() << " .tfi=" << value << ",";
6b66b9f7 7536 s.op->line() << " .address=(unsigned long)0x" << hex << p->addr << dec << "ULL,";
faea5e16 7537 s.op->line() << " .probe=" << common_probe_init (p) << ",";
4ddb6dd0 7538
038c38c6 7539 if (p->sdt_semaphore_addr != 0)
63b4fd14 7540 s.op->line() << " .sdt_sem_offset=(unsigned long)0x"
038c38c6 7541 << hex << p->sdt_semaphore_addr << dec << "ULL,";
4ddb6dd0
JS
7542
7543 if (p->has_return)
7544 s.op->line() << " .return_p=1,";
888af770
FCE
7545 s.op->line() << " },";
7546 }
7547 s.op->newline(-1) << "};";
a96d1db0 7548
89ba3085
FCE
7549 s.op->assert_0_indent();
7550
48e685da 7551 s.op->newline() << "static void enter_uprobe_probe (struct uprobe *inst, struct pt_regs *regs) {";
888af770 7552 s.op->newline(1) << "struct stap_uprobe *sup = container_of(inst, struct stap_uprobe, up);";
89ba3085 7553 s.op->newline() << "const struct stap_uprobe_spec *sups = &stap_uprobe_specs [sup->spec_index];";
6eefe942
MW
7554 common_probe_entryfn_prologue (s.op, "STAP_SESSION_RUNNING", "sups->probe",
7555 "_STP_PROBE_HANDLER_UPROBE");
0e090c74 7556 s.op->newline() << "if (sup->spec_index < 0 || "
6e895029
DS
7557 << "sup->spec_index >= " << probes.size() << ") {";
7558 s.op->newline(1) << "_stp_error (\"bad spec_index %d (max " << probes.size()
0e090c74 7559 << "): %s\", sup->spec_index, c->probe_point);";
6e895029
DS
7560 s.op->newline() << "atomic_dec (&c->busy);";
7561 s.op->newline() << "goto probe_epilogue;";
7562 s.op->newline(-1) << "}";
d9aed31e 7563 s.op->newline() << "c->uregs = regs;";
92c25572 7564 s.op->newline() << "c->probe_flags |= _STP_PROBE_STATE_USER_MODE;";
6415ddde
MW
7565
7566 // Make it look like the IP is set as it would in the actual user
7567 // task when calling real probe handler. Reset IP regs on return, so
7568 // we don't confuse uprobes. PR10458
7569 s.op->newline() << "{";
7570 s.op->indent(1);
d9aed31e 7571 s.op->newline() << "unsigned long uprobes_ip = REG_IP(c->uregs);";
259d54c0 7572 s.op->newline() << "SET_REG_IP(regs, inst->vaddr);";
26e63673 7573 s.op->newline() << "(*sups->probe->ph) (c);";
259d54c0 7574 s.op->newline() << "SET_REG_IP(regs, uprobes_ip);";
6415ddde
MW
7575 s.op->newline(-1) << "}";
7576
7baf48e9 7577 common_probe_entryfn_epilogue (s.op, true, s.suppress_handler_errors);
888af770 7578 s.op->newline(-1) << "}";
a96d1db0 7579
48e685da 7580 s.op->newline() << "static void enter_uretprobe_probe (struct uretprobe_instance *inst, struct pt_regs *regs) {";
888af770 7581 s.op->newline(1) << "struct stap_uprobe *sup = container_of(inst->rp, struct stap_uprobe, urp);";
89ba3085 7582 s.op->newline() << "const struct stap_uprobe_spec *sups = &stap_uprobe_specs [sup->spec_index];";
6eefe942
MW
7583 common_probe_entryfn_prologue (s.op, "STAP_SESSION_RUNNING", "sups->probe",
7584 "_STP_PROBE_HANDLER_URETPROBE");
6dceb5c9 7585 s.op->newline() << "c->ips.ri = inst;";
0e090c74 7586 s.op->newline() << "if (sup->spec_index < 0 || "
0d5561a5
DS
7587 << "sup->spec_index >= " << probes.size() << ") {";
7588 s.op->newline(1) << "_stp_error (\"bad spec_index %d (max " << probes.size()
0e090c74 7589 << "): %s\", sup->spec_index, c->probe_point);";
0d5561a5
DS
7590 s.op->newline() << "atomic_dec (&c->busy);";
7591 s.op->newline() << "goto probe_epilogue;";
7592 s.op->newline(-1) << "}";
7593
d9aed31e 7594 s.op->newline() << "c->uregs = regs;";
92c25572 7595 s.op->newline() << "c->probe_flags |= _STP_PROBE_STATE_USER_MODE;";
6415ddde
MW
7596
7597 // Make it look like the IP is set as it would in the actual user
7598 // task when calling real probe handler. Reset IP regs on return, so
7599 // we don't confuse uprobes. PR10458
7600 s.op->newline() << "{";
7601 s.op->indent(1);
d9aed31e 7602 s.op->newline() << "unsigned long uprobes_ip = REG_IP(c->uregs);";
5e562a69 7603 s.op->newline() << "SET_REG_IP(regs, inst->ret_addr);";
26e63673 7604 s.op->newline() << "(*sups->probe->ph) (c);";
259d54c0 7605 s.op->newline() << "SET_REG_IP(regs, uprobes_ip);";
6415ddde
MW
7606 s.op->newline(-1) << "}";
7607
7baf48e9 7608 common_probe_entryfn_epilogue (s.op, true, s.suppress_handler_errors);
a96d1db0
DN
7609 s.op->newline(-1) << "}";
7610
89ba3085 7611 s.op->newline();
cc52276b 7612 s.op->newline() << "#include \"uprobes-common.c\"";
6d0f3f0c 7613 s.op->newline();
888af770 7614}
935447c8
DS
7615
7616
888af770 7617void
2b69faaf 7618uprobe_derived_probe_group::emit_module_utrace_init (systemtap_session& s)
935447c8 7619{
888af770 7620 if (probes.empty()) return;
935447c8 7621
2b69faaf 7622 s.op->newline() << "/* ---- utrace uprobes ---- */";
935447c8 7623
01b05e2e 7624 s.op->newline() << "for (j=0; j<MAXUPROBES; j++) {";
5e112f92
FCE
7625 s.op->newline(1) << "struct stap_uprobe *sup = & stap_uprobes[j];";
7626 s.op->newline() << "sup->spec_index = -1;"; // free slot
80b4ad8b
FCE
7627 // NB: we assume the rest of the struct (specificaly, sup->up) is
7628 // initialized to zero. This is so that we can use
7629 // sup->up->kdata = NULL for "really free!" PR 6829.
5e112f92
FCE
7630 s.op->newline(-1) << "}";
7631 s.op->newline() << "mutex_init (& stap_uprobes_lock);";
935447c8 7632
89ba3085
FCE
7633 // Set up the task_finders
7634 s.op->newline() << "for (i=0; i<sizeof(stap_uprobe_finders)/sizeof(stap_uprobe_finders[0]); i++) {";
7635 s.op->newline(1) << "struct stap_uprobe_tf *stf = & stap_uprobe_finders[i];";
c57ea854 7636 s.op->newline() << "probe_point = stf->pathname;"; // for error messages; XXX: would prefer pp() or something better
89ba3085 7637 s.op->newline() << "rc = stap_register_task_finder_target (& stf->finder);";
935447c8 7638
5e112f92
FCE
7639 // NB: if (rc), there is no need (XXX: nor any way) to clean up any
7640 // finders already registered, since mere registration does not
7641 // cause any utrace or memory allocation actions. That happens only
7642 // later, once the task finder engine starts running. So, for a
7643 // partial initialization requiring unwind, we need do nothing.
7644 s.op->newline() << "if (rc) break;";
a7a68293 7645
888af770
FCE
7646 s.op->newline(-1) << "}";
7647}
d0ea46ce 7648
d0a7f5a9 7649
888af770 7650void
2b69faaf 7651uprobe_derived_probe_group::emit_module_utrace_exit (systemtap_session& s)
888af770
FCE
7652{
7653 if (probes.empty()) return;
2b69faaf 7654 s.op->newline() << "/* ---- utrace uprobes ---- */";
e56e51c9 7655
6d0f3f0c
FCE
7656 // NB: there is no stap_unregister_task_finder_target call;
7657 // important stuff like utrace cleanups are done by
d41d451c
FCE
7658 // __stp_task_finder_cleanup() via stap_stop_task_finder().
7659 //
7660 // This function blocks until all callbacks are completed, so there
7661 // is supposed to be no possibility of any registration-related code starting
7662 // to run in parallel with our shutdown here. So we don't need to protect the
7663 // stap_uprobes[] array with the mutex.
d0a7f5a9 7664
01b05e2e 7665 s.op->newline() << "for (j=0; j<MAXUPROBES; j++) {";
5e112f92 7666 s.op->newline(1) << "struct stap_uprobe *sup = & stap_uprobes[j];";
89ba3085 7667 s.op->newline() << "const struct stap_uprobe_spec *sups = &stap_uprobe_specs [sup->spec_index];";
6d0f3f0c 7668 s.op->newline() << "if (sup->spec_index < 0) continue;"; // free slot
3568f1dd 7669
8faa1fc5 7670 // PR10655: decrement that ENABLED semaphore
c116c31b 7671 s.op->newline() << "if (sup->sdt_sem_address) {";
8faa1fc5
FCE
7672 s.op->newline(1) << "unsigned short sdt_semaphore;"; // NB: fixed size
7673 s.op->newline() << "pid_t pid = (sups->return_p ? sup->urp.u.pid : sup->up.pid);";
7674 s.op->newline() << "struct task_struct *tsk;";
7675 s.op->newline() << "rcu_read_lock();";
6846cfc8 7676
86229a55
DS
7677 // Do a pid->task_struct* lookup. For 2.6.24+, this code assumes
7678 // that the pid is always in the global namespace, not in any
7679 // private namespace.
8faa1fc5 7680 s.op->newline() << "#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,24)";
86229a55
DS
7681 // We'd like to call find_task_by_pid_ns() here, but it isn't
7682 // exported. So, we call what it calls...
7683 s.op->newline() << " tsk = pid_task(find_pid_ns(pid, &init_pid_ns), PIDTYPE_PID);";
8faa1fc5
FCE
7684 s.op->newline() << "#else";
7685 s.op->newline() << " tsk = find_task_by_pid (pid);";
7686 s.op->newline() << "#endif /* 2.6.24 */";
8faa1fc5
FCE
7687
7688 s.op->newline() << "if (tsk) {"; // just in case the thing exited while we weren't watching
3c5b8e2b 7689 s.op->newline(1) << "if (__access_process_vm_noflush(tsk, sup->sdt_sem_address, &sdt_semaphore, sizeof(sdt_semaphore), 0)) {";
63b4fd14 7690 s.op->newline(1) << "sdt_semaphore --;";
903b9fcd 7691 s.op->newline() << "#ifdef DEBUG_UPROBES";
c116c31b 7692 s.op->newline() << "_stp_dbug (__FUNCTION__,__LINE__, \"-semaphore %#x @ %#lx\\n\", sdt_semaphore, sup->sdt_sem_address);";
903b9fcd 7693 s.op->newline() << "#endif";
3c5b8e2b 7694 s.op->newline() << "__access_process_vm_noflush(tsk, sup->sdt_sem_address, &sdt_semaphore, sizeof(sdt_semaphore), 1);";
93c84191 7695 s.op->newline(-1) << "}";
8faa1fc5
FCE
7696 // XXX: need to analyze possibility of race condition
7697 s.op->newline(-1) << "}";
7698 s.op->newline() << "rcu_read_unlock();";
7699 s.op->newline(-1) << "}";
6846cfc8 7700
3568f1dd
FCE
7701 s.op->newline() << "if (sups->return_p) {";
7702 s.op->newline(1) << "#ifdef DEBUG_UPROBES";
89ba3085 7703 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 7704 s.op->newline() << "#endif";
80b4ad8b
FCE
7705 // NB: PR6829 does not change that we still need to unregister at
7706 // *this* time -- when the script as a whole exits.
3568f1dd
FCE
7707 s.op->newline() << "unregister_uretprobe (& sup->urp);";
7708 s.op->newline(-1) << "} else {";
7709 s.op->newline(1) << "#ifdef DEBUG_UPROBES";
8faa1fc5 7710 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
7711 s.op->newline() << "#endif";
7712 s.op->newline() << "unregister_uprobe (& sup->up);";
7713 s.op->newline(-1) << "}";
935447c8 7714
6d0f3f0c 7715 s.op->newline() << "sup->spec_index = -1;";
935447c8 7716
3568f1dd
FCE
7717 // XXX: uprobe missed counts?
7718
6d0f3f0c 7719 s.op->newline(-1) << "}";
935447c8 7720
5e112f92 7721 s.op->newline() << "mutex_destroy (& stap_uprobes_lock);";
935447c8
DS
7722}
7723
2b69faaf
JS
7724
7725void
7726uprobe_derived_probe_group::emit_module_inode_decls (systemtap_session& s)
7727{
7728 if (probes.empty()) return;
7729 s.op->newline() << "/* ---- inode uprobes ---- */";
cfcab6c7 7730 emit_module_maxuprobes (s);
2b69faaf
JS
7731 s.op->newline() << "#include \"uprobes-inode.c\"";
7732
7733 // Write the probe handler.
7734 s.op->newline() << "static int enter_inode_uprobe "
7735 << "(struct uprobe_consumer *inst, struct pt_regs *regs) {";
cfcab6c7
JS
7736 s.op->newline(1) << "struct stapiu_consumer *sup = "
7737 << "container_of(inst, struct stapiu_consumer, consumer);";
fe5acded
JS
7738 common_probe_entryfn_prologue (s.op, "STAP_SESSION_RUNNING", "sup->probe",
7739 "_STP_PROBE_HANDLER_UPROBE");
3bff6634
JS
7740 s.op->newline() << "c->uregs = regs;";
7741 s.op->newline() << "c->probe_flags |= _STP_PROBE_STATE_USER_MODE;";
2b69faaf
JS
7742 // XXX: Can't set SET_REG_IP; we don't actually know the relocated address.
7743 // ... In some error cases, uprobes itself calls uprobes_get_bkpt_addr().
7744 s.op->newline() << "(*sup->probe->ph) (c);";
7ee95b6b 7745 common_probe_entryfn_epilogue (s.op, true, s.suppress_handler_errors);
2b69faaf
JS
7746 s.op->newline() << "return 0;";
7747 s.op->newline(-1) << "}";
7748 s.op->assert_0_indent();
7749
7750 // Index of all the modules for which we need inodes.
7751 map<string, unsigned> module_index;
7752 unsigned module_index_ctr = 0;
7753
7754 // Discover and declare targets for each unique path.
cfcab6c7 7755 s.op->newline() << "static struct stapiu_target "
2b69faaf
JS
7756 << "stap_inode_uprobe_targets[] = {";
7757 s.op->indent(1);
7758 for (unsigned i=0; i<probes.size(); i++)
7759 {
7760 uprobe_derived_probe *p = probes[i];
cfcab6c7
JS
7761 const string key = make_pbm_key(p);
7762 if (module_index.find (key) == module_index.end())
2b69faaf 7763 {
cfcab6c7
JS
7764 module_index[key] = module_index_ctr++;
7765 s.op->newline() << "{";
7766 s.op->line() << " .finder={";
7767 if (p->pid != 0)
7768 s.op->line() << " .pid=" << p->pid << ",";
7769
7770 if (p->section == "") // .statement(addr).absolute XXX?
7771 s.op->line() << " .callback=&stapiu_process_found,";
7772 else if (p->section == ".absolute") // proxy for ET_EXEC -> exec()'d program
7773 {
7774 s.op->line() << " .procname=" << lex_cast_qstring(p->module) << ",";
7775 s.op->line() << " .callback=&stapiu_process_found,";
7776 }
7777 else if (p->section != ".absolute") // ET_DYN
7778 {
7779 if (p->has_library)
7780 s.op->line() << " .procname=\"" << p->path << "\", ";
7781 s.op->line() << " .mmap_callback=&stapiu_mmap_found, ";
7782 s.op->line() << " .munmap_callback=&stapiu_munmap_found, ";
7783 s.op->line() << " .callback=&stapiu_process_munmap,";
7784 }
7785 s.op->line() << " },";
7786 s.op->line() << " .filename=" << lex_cast_qstring(p->module) << ",";
7787 s.op->line() << " },";
2b69faaf
JS
7788 }
7789 }
7790 s.op->newline(-1) << "};";
7791 s.op->assert_0_indent();
7792
7793 // Declare the actual probes.
cfcab6c7 7794 s.op->newline() << "static struct stapiu_consumer "
2b69faaf
JS
7795 << "stap_inode_uprobe_consumers[] = {";
7796 s.op->indent(1);
7797 for (unsigned i=0; i<probes.size(); i++)
7798 {
7799 uprobe_derived_probe *p = probes[i];
cfcab6c7
JS
7800 unsigned index = module_index[make_pbm_key(p)];
7801 s.op->newline() << "{";
7802 s.op->line() << " .consumer={ .handler=enter_inode_uprobe },";
7803 s.op->line() << " .target=&stap_inode_uprobe_targets[" << index << "],";
7804 s.op->line() << " .offset=(loff_t)0x" << hex << p->addr << dec << "ULL,";
7805 if (p->sdt_semaphore_addr)
7806 s.op->line() << " .sdt_sem_offset=(loff_t)0x"
7807 << hex << p->sdt_semaphore_addr << dec << "ULL,";
7808 s.op->line() << " .probe=" << common_probe_init (p) << ",";
7809 s.op->line() << " },";
2b69faaf
JS
7810 }
7811 s.op->newline(-1) << "};";
7812 s.op->assert_0_indent();
7813}
7814
7815
7816void
7817uprobe_derived_probe_group::emit_module_inode_init (systemtap_session& s)
7818{
7819 if (probes.empty()) return;
7820 s.op->newline() << "/* ---- inode uprobes ---- */";
cfcab6c7 7821 s.op->newline() << "rc = stapiu_init ("
2b69faaf
JS
7822 << "stap_inode_uprobe_targets, "
7823 << "ARRAY_SIZE(stap_inode_uprobe_targets), "
7824 << "stap_inode_uprobe_consumers, "
7825 << "ARRAY_SIZE(stap_inode_uprobe_consumers));";
7826}
7827
7828
7829void
7830uprobe_derived_probe_group::emit_module_inode_exit (systemtap_session& s)
7831{
7832 if (probes.empty()) return;
7833 s.op->newline() << "/* ---- inode uprobes ---- */";
cfcab6c7 7834 s.op->newline() << "stapiu_exit ("
2b69faaf
JS
7835 << "stap_inode_uprobe_targets, "
7836 << "ARRAY_SIZE(stap_inode_uprobe_targets), "
7837 << "stap_inode_uprobe_consumers, "
7838 << "ARRAY_SIZE(stap_inode_uprobe_consumers));";
7839}
7840
7841
7842void
7843uprobe_derived_probe_group::emit_module_decls (systemtap_session& s)
7844{
7845 if (kernel_supports_inode_uprobes (s))
7846 emit_module_inode_decls (s);
7847 else
7848 emit_module_utrace_decls (s);
7849}
7850
7851
7852void
7853uprobe_derived_probe_group::emit_module_init (systemtap_session& s)
7854{
7855 if (kernel_supports_inode_uprobes (s))
7856 emit_module_inode_init (s);
7857 else
7858 emit_module_utrace_init (s);
7859}
7860
7861
7862void
7863uprobe_derived_probe_group::emit_module_exit (systemtap_session& s)
7864{
7865 if (kernel_supports_inode_uprobes (s))
7866 emit_module_inode_exit (s);
7867 else
7868 emit_module_utrace_exit (s);
7869}
7870
7871
e6fe60e7
AM
7872// ------------------------------------------------------------------------
7873// Kprobe derived probes
7874// ------------------------------------------------------------------------
7875
4627ed58 7876static const string TOK_KPROBE("kprobe");
935447c8 7877
bae55db9 7878struct kprobe_derived_probe: public derived_probe
d0ea46ce 7879{
bae55db9
JS
7880 kprobe_derived_probe (probe *base,
7881 probe_point *location,
7882 const string& name,
7883 int64_t stmt_addr,
7884 bool has_return,
7885 bool has_statement,
7886 bool has_maxactive,
b642c901
SC
7887 bool has_path,
7888 bool has_library,
7889 long maxactive_val,
7890 const string& path,
7891 const string& library
bae55db9
JS
7892 );
7893 string symbol_name;
7894 Dwarf_Addr addr;
7895 bool has_return;
7896 bool has_statement;
7897 bool has_maxactive;
b642c901
SC
7898 bool has_path;
7899 bool has_library;
bae55db9 7900 long maxactive_val;
b642c901
SC
7901 string path;
7902 string library;
bae55db9
JS
7903 bool access_var;
7904 void printsig (std::ostream &o) const;
7905 void join_group (systemtap_session& s);
7906};
d0ea46ce 7907
bae55db9
JS
7908struct kprobe_derived_probe_group: public derived_probe_group
7909{
7910private:
7911 multimap<string,kprobe_derived_probe*> probes_by_module;
7912 typedef multimap<string,kprobe_derived_probe*>::iterator p_b_m_iterator;
d0ea46ce 7913
bae55db9
JS
7914public:
7915 void enroll (kprobe_derived_probe* probe);
7916 void emit_module_decls (systemtap_session& s);
7917 void emit_module_init (systemtap_session& s);
7918 void emit_module_exit (systemtap_session& s);
7919};
d0ea46ce 7920
e6fe60e7
AM
7921kprobe_derived_probe::kprobe_derived_probe (probe *base,
7922 probe_point *location,
b6371390 7923 const string& name,
e6fe60e7 7924 int64_t stmt_addr,
b6371390
JS
7925 bool has_return,
7926 bool has_statement,
7927 bool has_maxactive,
b642c901
SC
7928 bool has_path,
7929 bool has_library,
7930 long maxactive_val,
7931 const string& path,
7932 const string& library
b6371390 7933 ):
4c5d1300 7934 derived_probe (base, location, true /* .components soon rewritten */ ),
e6fe60e7 7935 symbol_name (name), addr (stmt_addr),
b6371390 7936 has_return (has_return), has_statement (has_statement),
b642c901
SC
7937 has_maxactive (has_maxactive), has_path (has_path),
7938 has_library (has_library),
7939 maxactive_val (maxactive_val),
7940 path (path), library (library)
e6fe60e7
AM
7941{
7942 this->tok = base->tok;
7943 this->access_var = false;
d0ea46ce 7944
e6fe60e7
AM
7945#ifndef USHRT_MAX
7946#define USHRT_MAX 32767
7947#endif
d0ea46ce 7948
46856d8d
JS
7949 // Expansion of $target variables in the probe body produces an error during
7950 // translate phase, since we're not using debuginfo
d0ea46ce 7951
e6fe60e7 7952 vector<probe_point::component*> comps;
46856d8d 7953 comps.push_back (new probe_point::component(TOK_KPROBE));
e6fe60e7 7954
46856d8d
JS
7955 if (has_statement)
7956 {
9ea68eb9
JS
7957 comps.push_back (new probe_point::component(TOK_STATEMENT,
7958 new literal_number(addr, true)));
46856d8d
JS
7959 comps.push_back (new probe_point::component(TOK_ABSOLUTE));
7960 }
7961 else
7962 {
7963 size_t pos = name.find(':');
7964 if (pos != string::npos)
d0ea46ce 7965 {
46856d8d
JS
7966 string module = name.substr(0, pos);
7967 string function = name.substr(pos + 1);
7968 comps.push_back (new probe_point::component(TOK_MODULE, new literal_string(module)));
7969 comps.push_back (new probe_point::component(TOK_FUNCTION, new literal_string(function)));
7970 }
7971 else
7972 comps.push_back (new probe_point::component(TOK_FUNCTION, new literal_string(name)));
46856d8d 7973 }
d0ea46ce 7974
b6371390
JS
7975 if (has_return)
7976 comps.push_back (new probe_point::component(TOK_RETURN));
7977 if (has_maxactive)
7978 comps.push_back (new probe_point::component(TOK_MAXACTIVE, new literal_number(maxactive_val)));
d0ea46ce 7979
e6fe60e7
AM
7980 this->sole_location()->components = comps;
7981}
d0ea46ce 7982
e6fe60e7
AM
7983void kprobe_derived_probe::printsig (ostream& o) const
7984{
7985 sole_location()->print (o);
7986 o << " /* " << " name = " << symbol_name << "*/";
7987 printsig_nested (o);
7988}
d0ea46ce 7989
e6fe60e7
AM
7990void kprobe_derived_probe::join_group (systemtap_session& s)
7991{
d0ea46ce 7992
e6fe60e7
AM
7993 if (! s.kprobe_derived_probes)
7994 s.kprobe_derived_probes = new kprobe_derived_probe_group ();
7995 s.kprobe_derived_probes->enroll (this);
d0ea46ce 7996
e6fe60e7 7997}
d0ea46ce 7998
e6fe60e7
AM
7999void kprobe_derived_probe_group::enroll (kprobe_derived_probe* p)
8000{
8001 probes_by_module.insert (make_pair (p->symbol_name, p));
8002 // probes of same symbol should share single kprobe/kretprobe
8003}
d0ea46ce 8004
e6fe60e7
AM
8005void
8006kprobe_derived_probe_group::emit_module_decls (systemtap_session& s)
8007{
8008 if (probes_by_module.empty()) return;
d0ea46ce 8009
e6fe60e7 8010 s.op->newline() << "/* ---- kprobe-based probes ---- */";
d0ea46ce 8011
e6fe60e7
AM
8012 // Warn of misconfigured kernels
8013 s.op->newline() << "#if ! defined(CONFIG_KPROBES)";
8014 s.op->newline() << "#error \"Need CONFIG_KPROBES!\"";
8015 s.op->newline() << "#endif";
8016 s.op->newline();
d0ea46ce 8017
f07c3b68 8018 s.op->newline() << "#ifndef KRETACTIVE";
1ee6b5fc 8019 s.op->newline() << "#define KRETACTIVE (max(15,6*(int)num_possible_cpus()))";
f07c3b68
FCE
8020 s.op->newline() << "#endif";
8021
e6fe60e7 8022 // Forward declare the master entry functions
88747011 8023 s.op->newline() << "static int enter_kprobe2_probe (struct kprobe *inst,";
e6fe60e7 8024 s.op->line() << " struct pt_regs *regs);";
88747011 8025 s.op->newline() << "static int enter_kretprobe2_probe (struct kretprobe_instance *inst,";
e6fe60e7 8026 s.op->line() << " struct pt_regs *regs);";
d0ea46ce 8027
e6fe60e7
AM
8028 // Emit an array of kprobe/kretprobe pointers
8029 s.op->newline() << "#if defined(STAPCONF_UNREGISTER_KPROBES)";
c9116e99 8030 s.op->newline() << "static void * stap_unreg_kprobes2[" << probes_by_module.size() << "];";
e6fe60e7 8031 s.op->newline() << "#endif";
d0ea46ce 8032
e6fe60e7 8033 // Emit the actual probe list.
d0ea46ce 8034
e6fe60e7
AM
8035 s.op->newline() << "static struct stap_dwarfless_kprobe {";
8036 s.op->newline(1) << "union { struct kprobe kp; struct kretprobe krp; } u;";
8037 s.op->newline() << "#ifdef __ia64__";
8038 s.op->newline() << "struct kprobe dummy;";
8039 s.op->newline() << "#endif";
8040 s.op->newline(-1) << "} stap_dwarfless_kprobes[" << probes_by_module.size() << "];";
8041 // NB: bss!
d0ea46ce 8042
e6fe60e7
AM
8043 s.op->newline() << "static struct stap_dwarfless_probe {";
8044 s.op->newline(1) << "const unsigned return_p:1;";
8045 s.op->newline() << "const unsigned maxactive_p:1;";
b350f56b 8046 s.op->newline() << "const unsigned optional_p:1;";
e6fe60e7
AM
8047 s.op->newline() << "unsigned registered_p:1;";
8048 s.op->newline() << "const unsigned short maxactive_val;";
935447c8 8049
e6fe60e7
AM
8050 // Function Names are mostly small and uniform enough to justify putting
8051 // char[MAX]'s into the array instead of relocated char*'s.
935447c8 8052
faea5e16
JS
8053 size_t symbol_string_name_max = 0;
8054 size_t symbol_string_name_tot = 0;
e6fe60e7 8055 for (p_b_m_iterator it = probes_by_module.begin(); it != probes_by_module.end(); it++)
6270adc1 8056 {
e6fe60e7
AM
8057 kprobe_derived_probe* p = it->second;
8058#define DOIT(var,expr) do { \
8059 size_t var##_size = (expr) + 1; \
8060 var##_max = max (var##_max, var##_size); \
8061 var##_tot += var##_size; } while (0)
e6fe60e7
AM
8062 DOIT(symbol_string_name, p->symbol_name.size());
8063#undef DOIT
6270adc1
MH
8064 }
8065
e6fe60e7
AM
8066#define CALCIT(var) \
8067 s.op->newline() << "const char " << #var << "[" << var##_name_max << "] ;";
935447c8 8068
e6fe60e7
AM
8069 CALCIT(symbol_string);
8070#undef CALCIT
6270adc1 8071
bd659351 8072 s.op->newline() << "unsigned long address;";
26e63673 8073 s.op->newline() << "struct stap_probe * const probe;";
e6fe60e7
AM
8074 s.op->newline(-1) << "} stap_dwarfless_probes[] = {";
8075 s.op->indent(1);
6270adc1 8076
e6fe60e7
AM
8077 for (p_b_m_iterator it = probes_by_module.begin(); it != probes_by_module.end(); it++)
8078 {
8079 kprobe_derived_probe* p = it->second;
8080 s.op->newline() << "{";
8081 if (p->has_return)
8082 s.op->line() << " .return_p=1,";
6270adc1 8083
e6fe60e7
AM
8084 if (p->has_maxactive)
8085 {
8086 s.op->line() << " .maxactive_p=1,";
8087 assert (p->maxactive_val >= 0 && p->maxactive_val <= USHRT_MAX);
8088 s.op->line() << " .maxactive_val=" << p->maxactive_val << ",";
8089 }
6270adc1 8090
b350f56b
JS
8091 if (p->locations[0]->optional)
8092 s.op->line() << " .optional_p=1,";
8093
e6fe60e7 8094 if (p->has_statement)
c8d9d15e 8095 s.op->line() << " .address=(unsigned long)0x" << hex << p->addr << dec << "ULL,";
e6fe60e7 8096 else
c8d9d15e 8097 s.op->line() << " .symbol_string=\"" << p->symbol_name << "\",";
5d67b47c 8098
faea5e16 8099 s.op->line() << " .probe=" << common_probe_init (p) << ",";
e6fe60e7 8100 s.op->line() << " },";
935447c8
DS
8101 }
8102
e6fe60e7 8103 s.op->newline(-1) << "};";
5d67b47c 8104
e6fe60e7
AM
8105 // Emit the kprobes callback function
8106 s.op->newline();
88747011 8107 s.op->newline() << "static int enter_kprobe2_probe (struct kprobe *inst,";
e6fe60e7
AM
8108 s.op->line() << " struct pt_regs *regs) {";
8109 // NB: as of PR5673, the kprobe|kretprobe union struct is in BSS
8110 s.op->newline(1) << "int kprobe_idx = ((uintptr_t)inst-(uintptr_t)stap_dwarfless_kprobes)/sizeof(struct stap_dwarfless_kprobe);";
8111 // Check that the index is plausible
8112 s.op->newline() << "struct stap_dwarfless_probe *sdp = &stap_dwarfless_probes[";
8113 s.op->line() << "((kprobe_idx >= 0 && kprobe_idx < " << probes_by_module.size() << ")?";
8114 s.op->line() << "kprobe_idx:0)"; // NB: at least we avoid memory corruption
8115 // XXX: it would be nice to give a more verbose error though; BUG_ON later?
8116 s.op->line() << "];";
6eefe942
MW
8117 common_probe_entryfn_prologue (s.op, "STAP_SESSION_RUNNING", "sdp->probe",
8118 "_STP_PROBE_HANDLER_KPROBE");
d9aed31e 8119 s.op->newline() << "c->kregs = regs;";
6415ddde
MW
8120
8121 // Make it look like the IP is set as it wouldn't have been replaced
8122 // by a breakpoint instruction when calling real probe handler. Reset
8123 // IP regs on return, so we don't confuse kprobes. PR10458
8124 s.op->newline() << "{";
8125 s.op->indent(1);
d9aed31e 8126 s.op->newline() << "unsigned long kprobes_ip = REG_IP(c->kregs);";
259d54c0 8127 s.op->newline() << "SET_REG_IP(regs, (unsigned long) inst->addr);";
26e63673 8128 s.op->newline() << "(*sdp->probe->ph) (c);";
259d54c0 8129 s.op->newline() << "SET_REG_IP(regs, kprobes_ip);";
6415ddde
MW
8130 s.op->newline(-1) << "}";
8131
7baf48e9 8132 common_probe_entryfn_epilogue (s.op, true, s.suppress_handler_errors);
e6fe60e7
AM
8133 s.op->newline() << "return 0;";
8134 s.op->newline(-1) << "}";
935447c8 8135
e6fe60e7
AM
8136 // Same for kretprobes
8137 s.op->newline();
88747011 8138 s.op->newline() << "static int enter_kretprobe2_probe (struct kretprobe_instance *inst,";
e6fe60e7
AM
8139 s.op->line() << " struct pt_regs *regs) {";
8140 s.op->newline(1) << "struct kretprobe *krp = inst->rp;";
935447c8 8141
e6fe60e7
AM
8142 // NB: as of PR5673, the kprobe|kretprobe union struct is in BSS
8143 s.op->newline() << "int kprobe_idx = ((uintptr_t)krp-(uintptr_t)stap_dwarfless_kprobes)/sizeof(struct stap_dwarfless_kprobe);";
8144 // Check that the index is plausible
8145 s.op->newline() << "struct stap_dwarfless_probe *sdp = &stap_dwarfless_probes[";
8146 s.op->line() << "((kprobe_idx >= 0 && kprobe_idx < " << probes_by_module.size() << ")?";
8147 s.op->line() << "kprobe_idx:0)"; // NB: at least we avoid memory corruption
8148 // XXX: it would be nice to give a more verbose error though; BUG_ON later?
8149 s.op->line() << "];";
935447c8 8150
6eefe942
MW
8151 common_probe_entryfn_prologue (s.op, "STAP_SESSION_RUNNING", "sdp->probe",
8152 "_STP_PROBE_HANDLER_KRETPROBE");
d9aed31e 8153 s.op->newline() << "c->kregs = regs;";
6dceb5c9 8154 s.op->newline() << "c->ips.krp.pi = inst;"; // for assisting runtime's backtrace logic
6415ddde
MW
8155
8156 // Make it look like the IP is set as it wouldn't have been replaced
8157 // by a breakpoint instruction when calling real probe handler. Reset
8158 // IP regs on return, so we don't confuse kprobes. PR10458
8159 s.op->newline() << "{";
8160 s.op->indent(1);
d9aed31e 8161 s.op->newline() << "unsigned long kprobes_ip = REG_IP(c->kregs);";
259d54c0 8162 s.op->newline() << "SET_REG_IP(regs, (unsigned long) inst->rp->kp.addr);";
26e63673 8163 s.op->newline() << "(*sdp->probe->ph) (c);";
259d54c0 8164 s.op->newline() << "SET_REG_IP(regs, kprobes_ip);";
6415ddde
MW
8165 s.op->newline(-1) << "}";
8166
7baf48e9 8167 common_probe_entryfn_epilogue (s.op, true, s.suppress_handler_errors);
e6fe60e7
AM
8168 s.op->newline() << "return 0;";
8169 s.op->newline(-1) << "}";
bd659351 8170
03a4ec63 8171 s.op->newline() << "#ifdef STAPCONF_KALLSYMS_ON_EACH_SYMBOL";
bd659351
MW
8172 s.op->newline() << "static int kprobe_resolve(void *data, const char *name,";
8173 s.op->newline() << " struct module *owner,";
8174 s.op->newline() << " unsigned long val) {";
8175 s.op->newline(1) << "int i;";
fc1d2aa2
MW
8176 s.op->newline() << "int *p = (int *) data;";
8177 s.op->newline() << "for (i=0; i<" << probes_by_module.size()
8178 << " && *p > 0; i++) {";
bd659351
MW
8179 s.op->newline(1) << "struct stap_dwarfless_probe *sdp = & stap_dwarfless_probes[i];";
8180 s.op->newline() << "if (! sdp->address)";
fc1d2aa2 8181 s.op->newline(1) << "if (strcmp(sdp->symbol_string, name) == 0) {";
bd659351 8182 s.op->newline(1) << "sdp->address = val;";
fc1d2aa2
MW
8183 s.op->newline() << "(*p)--;";
8184 s.op->newline(-1) << "}";
8185 s.op->newline(-2) << "}";
8186 s.op->newline() << "return (p > 0) ? 0 : -1;";
bd659351 8187 s.op->newline(-1) << "}";
03a4ec63 8188 s.op->newline() << "#endif";
935447c8
DS
8189}
8190
e6fe60e7 8191
6270adc1 8192void
e6fe60e7 8193kprobe_derived_probe_group::emit_module_init (systemtap_session& s)
6270adc1 8194{
03a4ec63 8195 s.op->newline() << "#ifdef STAPCONF_KALLSYMS_ON_EACH_SYMBOL";
fc1d2aa2
MW
8196 s.op->newline() << "{";
8197 s.op->newline(1) << "int p = 0;";
8198 s.op->newline() << "for (i = 0; i < " << probes_by_module.size() << "; i++) {";
8199 s.op->newline(1) << "struct stap_dwarfless_probe *sdp = & stap_dwarfless_probes[i];";
8200 s.op->newline() << "if (! sdp->address)";
8201 s.op->newline(1) << "p++;";
8202 s.op->newline(-2) << "}";
8203 s.op->newline() << "kallsyms_on_each_symbol(kprobe_resolve, &p);";
8204 s.op->newline(-1) << "}";
03a4ec63 8205 s.op->newline() << "#endif";
bd659351 8206
e6fe60e7 8207 s.op->newline() << "for (i=0; i<" << probes_by_module.size() << "; i++) {";
c8d9d15e 8208 s.op->newline(1) << "struct stap_dwarfless_probe *sdp = & stap_dwarfless_probes[i];";
e6fe60e7 8209 s.op->newline() << "struct stap_dwarfless_kprobe *kp = & stap_dwarfless_kprobes[i];";
c8d9d15e 8210 s.op->newline() << "void *addr = (void *) sdp->address;";
03a4ec63
MW
8211 s.op->newline() << "const char *symbol_name = addr ? NULL : sdp->symbol_string;";
8212
8213 s.op->newline() << "#ifdef STAPCONF_KALLSYMS_ON_EACH_SYMBOL";
bd659351
MW
8214 s.op->newline() << "if (! addr) {";
8215 s.op->newline(1) << "sdp->registered_p = 0;";
9319b767
MW
8216 s.op->newline() << "if (!sdp->optional_p)";
8217 s.op->newline(1) << "_stp_warn (\"probe %s registration error (symbol not found)\", probe_point);";
8218 s.op->newline(-1) << "continue;";
bd659351 8219 s.op->newline(-1) << "}";
03a4ec63
MW
8220 s.op->newline() << "#endif";
8221
26e63673 8222 s.op->newline() << "probe_point = sdp->probe->pp;"; // for error messages
e6fe60e7 8223 s.op->newline() << "if (sdp->return_p) {";
c8d9d15e 8224 s.op->newline(1) << "kp->u.krp.kp.addr = addr;";
766cee5f 8225 s.op->newline() << "#ifdef STAPCONF_KPROBE_SYMBOL_NAME";
03a4ec63 8226 s.op->newline() << "kp->u.krp.kp.symbol_name = (char *) symbol_name;";
766cee5f 8227 s.op->newline() << "#endif";
e6fe60e7
AM
8228 s.op->newline() << "if (sdp->maxactive_p) {";
8229 s.op->newline(1) << "kp->u.krp.maxactive = sdp->maxactive_val;";
8230 s.op->newline(-1) << "} else {";
f07c3b68 8231 s.op->newline(1) << "kp->u.krp.maxactive = KRETACTIVE;";
e6fe60e7 8232 s.op->newline(-1) << "}";
88747011 8233 s.op->newline() << "kp->u.krp.handler = &enter_kretprobe2_probe;";
e6fe60e7
AM
8234 // to ensure safeness of bspcache, always use aggr_kprobe on ia64
8235 s.op->newline() << "#ifdef __ia64__";
e6fe60e7 8236 s.op->newline() << "kp->dummy.addr = kp->u.krp.kp.addr;";
766cee5f 8237 s.op->newline() << "#ifdef STAPCONF_KPROBE_SYMBOL_NAME";
c8d9d15e 8238 s.op->newline() << "kp->dummy.symbol_name = kp->u.krp.kp.symbol_name;";
766cee5f 8239 s.op->newline() << "#endif";
c8d9d15e 8240 s.op->newline() << "kp->dummy.pre_handler = NULL;";
e6fe60e7
AM
8241 s.op->newline() << "rc = register_kprobe (& kp->dummy);";
8242 s.op->newline() << "if (rc == 0) {";
8243 s.op->newline(1) << "rc = register_kretprobe (& kp->u.krp);";
8244 s.op->newline() << "if (rc != 0)";
8245 s.op->newline(1) << "unregister_kprobe (& kp->dummy);";
8246 s.op->newline(-2) << "}";
8247 s.op->newline() << "#else";
8248 s.op->newline() << "rc = register_kretprobe (& kp->u.krp);";
8249 s.op->newline() << "#endif";
8250 s.op->newline(-1) << "} else {";
8251 // to ensure safeness of bspcache, always use aggr_kprobe on ia64
c8d9d15e 8252 s.op->newline(1) << "kp->u.kp.addr = addr;";
766cee5f 8253 s.op->newline() << "#ifdef STAPCONF_KPROBE_SYMBOL_NAME";
03a4ec63 8254 s.op->newline() << "kp->u.kp.symbol_name = (char *) symbol_name;";
766cee5f 8255 s.op->newline() << "#endif";
88747011 8256 s.op->newline() << "kp->u.kp.pre_handler = &enter_kprobe2_probe;";
e6fe60e7 8257 s.op->newline() << "#ifdef __ia64__";
e6fe60e7 8258 s.op->newline() << "kp->dummy.pre_handler = NULL;";
c8d9d15e 8259 s.op->newline() << "kp->dummy.addr = kp->u.kp.addr;";
766cee5f 8260 s.op->newline() << "#ifdef STAPCONF_KPROBE_SYMBOL_NAME";
c8d9d15e 8261 s.op->newline() << "kp->dummy.symbol_name = kp->u.kp.symbol_name;";
766cee5f 8262 s.op->newline() << "#endif";
e6fe60e7
AM
8263 s.op->newline() << "rc = register_kprobe (& kp->dummy);";
8264 s.op->newline() << "if (rc == 0) {";
8265 s.op->newline(1) << "rc = register_kprobe (& kp->u.kp);";
8266 s.op->newline() << "if (rc != 0)";
8267 s.op->newline(1) << "unregister_kprobe (& kp->dummy);";
8268 s.op->newline(-2) << "}";
8269 s.op->newline() << "#else";
8270 s.op->newline() << "rc = register_kprobe (& kp->u.kp);";
8271 s.op->newline() << "#endif";
8272 s.op->newline(-1) << "}";
8273 s.op->newline() << "if (rc) {"; // PR6749: tolerate a failed register_*probe.
8274 s.op->newline(1) << "sdp->registered_p = 0;";
b350f56b 8275 s.op->newline() << "if (!sdp->optional_p)";
50b6acf7 8276 s.op->newline(1) << "_stp_warn (\"probe %s (address 0x%lx) registration error (rc %d)\", probe_point, (unsigned long) addr, rc);";
b350f56b 8277 s.op->newline(-1) << "rc = 0;"; // continue with other probes
e6fe60e7
AM
8278 // XXX: shall we increment numskipped?
8279 s.op->newline(-1) << "}";
6270adc1 8280
e6fe60e7
AM
8281 s.op->newline() << "else sdp->registered_p = 1;";
8282 s.op->newline(-1) << "}"; // for loop
6270adc1
MH
8283}
8284
b4be7cbc 8285
e6fe60e7
AM
8286void
8287kprobe_derived_probe_group::emit_module_exit (systemtap_session& s)
935447c8 8288{
e6fe60e7
AM
8289 //Unregister kprobes by batch interfaces.
8290 s.op->newline() << "#if defined(STAPCONF_UNREGISTER_KPROBES)";
8291 s.op->newline() << "j = 0;";
8292 s.op->newline() << "for (i=0; i<" << probes_by_module.size() << "; i++) {";
8293 s.op->newline(1) << "struct stap_dwarfless_probe *sdp = & stap_dwarfless_probes[i];";
8294 s.op->newline() << "struct stap_dwarfless_kprobe *kp = & stap_dwarfless_kprobes[i];";
8295 s.op->newline() << "if (! sdp->registered_p) continue;";
8296 s.op->newline() << "if (!sdp->return_p)";
c9116e99 8297 s.op->newline(1) << "stap_unreg_kprobes2[j++] = &kp->u.kp;";
e6fe60e7 8298 s.op->newline(-2) << "}";
c9116e99 8299 s.op->newline() << "unregister_kprobes((struct kprobe **)stap_unreg_kprobes2, j);";
e6fe60e7
AM
8300 s.op->newline() << "j = 0;";
8301 s.op->newline() << "for (i=0; i<" << probes_by_module.size() << "; i++) {";
8302 s.op->newline(1) << "struct stap_dwarfless_probe *sdp = & stap_dwarfless_probes[i];";
8303 s.op->newline() << "struct stap_dwarfless_kprobe *kp = & stap_dwarfless_kprobes[i];";
8304 s.op->newline() << "if (! sdp->registered_p) continue;";
8305 s.op->newline() << "if (sdp->return_p)";
c9116e99 8306 s.op->newline(1) << "stap_unreg_kprobes2[j++] = &kp->u.krp;";
e6fe60e7 8307 s.op->newline(-2) << "}";
c9116e99 8308 s.op->newline() << "unregister_kretprobes((struct kretprobe **)stap_unreg_kprobes2, j);";
e6fe60e7
AM
8309 s.op->newline() << "#ifdef __ia64__";
8310 s.op->newline() << "j = 0;";
8311 s.op->newline() << "for (i=0; i<" << probes_by_module.size() << "; i++) {";
8312 s.op->newline(1) << "struct stap_dwarfless_probe *sdp = & stap_dwarfless_probes[i];";
8313 s.op->newline() << "struct stap_dwarfless_kprobe *kp = & stap_dwarfless_kprobes[i];";
8314 s.op->newline() << "if (! sdp->registered_p) continue;";
c9116e99 8315 s.op->newline() << "stap_unreg_kprobes2[j++] = &kp->dummy;";
e6fe60e7 8316 s.op->newline(-1) << "}";
c9116e99 8317 s.op->newline() << "unregister_kprobes((struct kprobe **)stap_unreg_kprobes2, j);";
e6fe60e7
AM
8318 s.op->newline() << "#endif";
8319 s.op->newline() << "#endif";
3e3bd7b6 8320
e6fe60e7
AM
8321 s.op->newline() << "for (i=0; i<" << probes_by_module.size() << "; i++) {";
8322 s.op->newline(1) << "struct stap_dwarfless_probe *sdp = & stap_dwarfless_probes[i];";
8323 s.op->newline() << "struct stap_dwarfless_kprobe *kp = & stap_dwarfless_kprobes[i];";
8324 s.op->newline() << "if (! sdp->registered_p) continue;";
8325 s.op->newline() << "if (sdp->return_p) {";
8326 s.op->newline() << "#if !defined(STAPCONF_UNREGISTER_KPROBES)";
8327 s.op->newline(1) << "unregister_kretprobe (&kp->u.krp);";
8328 s.op->newline() << "#endif";
8329 s.op->newline() << "atomic_add (kp->u.krp.nmissed, & skipped_count);";
8330 s.op->newline() << "#ifdef STP_TIMING";
8331 s.op->newline() << "if (kp->u.krp.nmissed)";
26e63673 8332 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
8333 s.op->newline(-1) << "#endif";
8334 s.op->newline() << "atomic_add (kp->u.krp.kp.nmissed, & skipped_count);";
8335 s.op->newline() << "#ifdef STP_TIMING";
8336 s.op->newline() << "if (kp->u.krp.kp.nmissed)";
26e63673 8337 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
8338 s.op->newline(-1) << "#endif";
8339 s.op->newline(-1) << "} else {";
8340 s.op->newline() << "#if !defined(STAPCONF_UNREGISTER_KPROBES)";
8341 s.op->newline(1) << "unregister_kprobe (&kp->u.kp);";
8342 s.op->newline() << "#endif";
8343 s.op->newline() << "atomic_add (kp->u.kp.nmissed, & skipped_count);";
8344 s.op->newline() << "#ifdef STP_TIMING";
8345 s.op->newline() << "if (kp->u.kp.nmissed)";
26e63673 8346 s.op->newline(1) << "_stp_warn (\"Skipped due to missed kprobe on '%s': %lu\\n\", sdp->probe->pp, kp->u.kp.nmissed);";
e6fe60e7
AM
8347 s.op->newline(-1) << "#endif";
8348 s.op->newline(-1) << "}";
8349 s.op->newline() << "#if !defined(STAPCONF_UNREGISTER_KPROBES) && defined(__ia64__)";
8350 s.op->newline() << "unregister_kprobe (&kp->dummy);";
8351 s.op->newline() << "#endif";
8352 s.op->newline() << "sdp->registered_p = 0;";
8353 s.op->newline(-1) << "}";
f8a968bc
JS
8354}
8355
e6fe60e7 8356struct kprobe_builder: public derived_probe_builder
3c1b3d06 8357{
e6fe60e7
AM
8358 kprobe_builder() {}
8359 virtual void build(systemtap_session & sess,
8360 probe * base,
8361 probe_point * location,
8362 literal_map_t const & parameters,
8363 vector<derived_probe *> & finished_results);
8364};
3c1b3d06
FCE
8365
8366
79189b84 8367void
05fb3e0c 8368kprobe_builder::build(systemtap_session & sess,
e6fe60e7
AM
8369 probe * base,
8370 probe_point * location,
8371 literal_map_t const & parameters,
8372 vector<derived_probe *> & finished_results)
79189b84 8373{
e6fe60e7 8374 string function_string_val, module_string_val;
05fb3e0c 8375 string path, library, path_tgt, library_tgt;
b6371390
JS
8376 int64_t statement_num_val = 0, maxactive_val = 0;
8377 bool has_function_str, has_module_str, has_statement_num;
8378 bool has_absolute, has_return, has_maxactive;
b642c901 8379 bool has_path, has_library;
79189b84 8380
b6371390
JS
8381 has_function_str = get_param(parameters, TOK_FUNCTION, function_string_val);
8382 has_module_str = get_param(parameters, TOK_MODULE, module_string_val);
8383 has_return = has_null_param (parameters, TOK_RETURN);
8384 has_maxactive = get_param(parameters, TOK_MAXACTIVE, maxactive_val);
8385 has_statement_num = get_param(parameters, TOK_STATEMENT, statement_num_val);
8386 has_absolute = has_null_param (parameters, TOK_ABSOLUTE);
b642c901
SC
8387 has_path = get_param (parameters, TOK_PROCESS, path);
8388 has_library = get_param (parameters, TOK_LIBRARY, library);
8389
8390 if (has_path)
05fb3e0c
WF
8391 {
8392 path = find_executable (path, sess.sysroot, sess.sysenv);
8393 path_tgt = path_remove_sysroot(sess, path);
8394 }
b642c901 8395 if (has_library)
05fb3e0c
WF
8396 {
8397 library = find_executable (library, sess.sysroot, sess.sysenv,
8398 "LD_LIBRARY_PATH");
8399 library_tgt = path_remove_sysroot(sess, library);
8400 }
c57ea854 8401
b6371390 8402 if (has_function_str)
6fb70fb7 8403 {
b6371390
JS
8404 if (has_module_str)
8405 function_string_val = module_string_val + ":" + function_string_val;
86758d5f 8406
b6371390
JS
8407 finished_results.push_back (new kprobe_derived_probe (base,
8408 location, function_string_val,
8409 0, has_return,
8410 has_statement_num,
8411 has_maxactive,
b642c901
SC
8412 has_path,
8413 has_library,
8414 maxactive_val,
05fb3e0c
WF
8415 path_tgt,
8416 library_tgt));
6fb70fb7 8417 }
e6fe60e7 8418 else
b6371390
JS
8419 {
8420 // assert guru mode for absolute probes
8421 if ( has_statement_num && has_absolute && !base->privileged )
e3bbc038 8422 throw semantic_error (_("absolute statement probe in unprivileged script; need stap -g"), base->tok);
b6371390
JS
8423
8424 finished_results.push_back (new kprobe_derived_probe (base,
8425 location, "",
8426 statement_num_val,
8427 has_return,
8428 has_statement_num,
8429 has_maxactive,
b642c901
SC
8430 has_path,
8431 has_library,
8432 maxactive_val,
05fb3e0c
WF
8433 path_tgt,
8434 library_tgt));
96b030fe 8435 }
79189b84
JS
8436}
8437
dd225250
PS
8438// ------------------------------------------------------------------------
8439// Hardware breakpoint based probes.
8440// ------------------------------------------------------------------------
8441
8442static const string TOK_HWBKPT("data");
8443static const string TOK_HWBKPT_WRITE("write");
8444static const string TOK_HWBKPT_RW("rw");
8445static const string TOK_LENGTH("length");
8446
8447#define HWBKPT_READ 0
8448#define HWBKPT_WRITE 1
8449#define HWBKPT_RW 2
8450struct hwbkpt_derived_probe: public derived_probe
8451{
8452 hwbkpt_derived_probe (probe *base,
8453 probe_point *location,
8454 uint64_t addr,
8455 string symname,
8456 unsigned int len,
8457 bool has_only_read_access,
8458 bool has_only_write_access,
8459 bool has_rw_access
8460 );
8461 Dwarf_Addr hwbkpt_addr;
8462 string symbol_name;
8463 unsigned int hwbkpt_access,hwbkpt_len;
8464
8465 void printsig (std::ostream &o) const;
8466 void join_group (systemtap_session& s);
8467};
8468
8469struct hwbkpt_derived_probe_group: public derived_probe_group
8470{
dd225250 8471private:
dac77b80 8472 vector<hwbkpt_derived_probe*> hwbkpt_probes;
dd225250
PS
8473
8474public:
8475 void enroll (hwbkpt_derived_probe* probe, systemtap_session& s);
8476 void emit_module_decls (systemtap_session& s);
8477 void emit_module_init (systemtap_session& s);
8478 void emit_module_exit (systemtap_session& s);
8479};
8480
8481hwbkpt_derived_probe::hwbkpt_derived_probe (probe *base,
9ea68eb9
JS
8482 probe_point *location,
8483 uint64_t addr,
8484 string symname,
8485 unsigned int len,
8486 bool has_only_read_access,
8487 bool has_only_write_access,
822a6a3d 8488 bool):
4c5d1300 8489 derived_probe (base, location, true /* .components soon rewritten */ ),
dd225250
PS
8490 hwbkpt_addr (addr),
8491 symbol_name (symname),
8492 hwbkpt_len (len)
8493{
8494 this->tok = base->tok;
8495
8496 vector<probe_point::component*> comps;
8497 comps.push_back (new probe_point::component(TOK_KERNEL));
8498
8499 if (hwbkpt_addr)
9ea68eb9
JS
8500 comps.push_back (new probe_point::component (TOK_HWBKPT,
8501 new literal_number(hwbkpt_addr, true)));
8502 else if (symbol_name.size())
8503 comps.push_back (new probe_point::component (TOK_HWBKPT, new literal_string(symbol_name)));
dd225250
PS
8504
8505 comps.push_back (new probe_point::component (TOK_LENGTH, new literal_number(hwbkpt_len)));
8506
8507 if (has_only_read_access)
9ea68eb9 8508 this->hwbkpt_access = HWBKPT_READ ;
dd225250
PS
8509//TODO add code for comps.push_back for read, since this flag is not for x86
8510
8511 else
9ea68eb9
JS
8512 {
8513 if (has_only_write_access)
8514 {
8515 this->hwbkpt_access = HWBKPT_WRITE ;
8516 comps.push_back (new probe_point::component(TOK_HWBKPT_WRITE));
8517 }
8518 else
8519 {
8520 this->hwbkpt_access = HWBKPT_RW ;
8521 comps.push_back (new probe_point::component(TOK_HWBKPT_RW));
8522 }
8523 }
dd225250
PS
8524
8525 this->sole_location()->components = comps;
8526}
8527
8528void hwbkpt_derived_probe::printsig (ostream& o) const
8529{
8530 sole_location()->print (o);
8531 printsig_nested (o);
8532}
8533
8534void hwbkpt_derived_probe::join_group (systemtap_session& s)
8535{
dac77b80
FCE
8536 if (! s.hwbkpt_derived_probes)
8537 s.hwbkpt_derived_probes = new hwbkpt_derived_probe_group ();
dd225250
PS
8538 s.hwbkpt_derived_probes->enroll (this, s);
8539}
8540
8541void hwbkpt_derived_probe_group::enroll (hwbkpt_derived_probe* p, systemtap_session& s)
8542{
dac77b80
FCE
8543 hwbkpt_probes.push_back (p);
8544
8545 unsigned max_hwbkpt_probes_by_arch = 0;
8546 if (s.architecture == "i386" || s.architecture == "x86_64")
8547 max_hwbkpt_probes_by_arch = 4;
8548 else if (s.architecture == "s390")
8549 max_hwbkpt_probes_by_arch = 1;
8550
c57ea854 8551 if (hwbkpt_probes.size() >= max_hwbkpt_probes_by_arch)
2713ea24 8552 s.print_warning (_F("Too many hardware breakpoint probes requested for %s (%zu vs. %u)",
b530b5b3 8553 s.architecture.c_str(), hwbkpt_probes.size(), max_hwbkpt_probes_by_arch));
dd225250
PS
8554}
8555
8556void
8557hwbkpt_derived_probe_group::emit_module_decls (systemtap_session& s)
8558{
dac77b80 8559 if (hwbkpt_probes.empty()) return;
dd225250
PS
8560
8561 s.op->newline() << "/* ---- hwbkpt-based probes ---- */";
8562
8563 s.op->newline() << "#include <linux/perf_event.h>";
8564 s.op->newline() << "#include <linux/hw_breakpoint.h>";
8565 s.op->newline();
8566
8567 // Forward declare the master entry functions
8568 s.op->newline() << "static int enter_hwbkpt_probe (struct perf_event *bp,";
8569 s.op->line() << " int nmi,";
8570 s.op->line() << " struct perf_sample_data *data,";
8571 s.op->line() << " struct pt_regs *regs);";
79189b84 8572
dd225250
PS
8573 // Emit the actual probe list.
8574
8575 s.op->newline() << "static struct perf_event_attr ";
dac77b80 8576 s.op->newline() << "stap_hwbkpt_probe_array[" << hwbkpt_probes.size() << "];";
dd225250
PS
8577
8578 s.op->newline() << "static struct perf_event **";
dac77b80 8579 s.op->newline() << "stap_hwbkpt_ret_array[" << hwbkpt_probes.size() << "];";
dd225250
PS
8580 s.op->newline() << "static struct stap_hwbkpt_probe {";
8581 s.op->newline() << "int registered_p:1;";
43650b10 8582// registered_p = 0 signifies a probe that is unregistered (or failed)
dd225250
PS
8583// registered_p = 1 signifies a probe that got registered successfully
8584
faea5e16 8585 // Symbol Names are mostly small and uniform enough
dd225250 8586 // to justify putting const char*.
dac77b80 8587 s.op->newline() << "const char * const symbol;";
dd225250
PS
8588
8589 s.op->newline() << "const unsigned long address;";
8590 s.op->newline() << "uint8_t atype;";
bb0a4e12 8591 s.op->newline() << "unsigned int len;";
26e63673 8592 s.op->newline() << "struct stap_probe * const probe;";
dd225250
PS
8593 s.op->newline() << "} stap_hwbkpt_probes[] = {";
8594 s.op->indent(1);
8595
dac77b80 8596 for (unsigned int it = 0; it < hwbkpt_probes.size(); it++)
dd225250 8597 {
dac77b80 8598 hwbkpt_derived_probe* p = hwbkpt_probes.at(it);
dd225250 8599 s.op->newline() << "{";
dd225250
PS
8600 if (p->symbol_name.size())
8601 s.op->line() << " .address=(unsigned long)0x0" << "ULL,";
8602 else
8603 s.op->line() << " .address=(unsigned long)0x" << hex << p->hwbkpt_addr << dec << "ULL,";
8604 switch(p->hwbkpt_access){
8605 case HWBKPT_READ:
8606 s.op->line() << " .atype=HW_BREAKPOINT_R ,";
bb0a4e12 8607 break;
dd225250
PS
8608 case HWBKPT_WRITE:
8609 s.op->line() << " .atype=HW_BREAKPOINT_W ,";
bb0a4e12 8610 break;
dd225250
PS
8611 case HWBKPT_RW:
8612 s.op->line() << " .atype=HW_BREAKPOINT_R|HW_BREAKPOINT_W ,";
bb0a4e12 8613 break;
dd225250
PS
8614 };
8615 s.op->line() << " .len=" << p->hwbkpt_len << ",";
faea5e16 8616 s.op->line() << " .probe=" << common_probe_init (p) << ",";
dd225250 8617 s.op->line() << " .symbol=\"" << p->symbol_name << "\",";
dd225250
PS
8618 s.op->line() << " },";
8619 }
dac77b80 8620 s.op->newline(-1) << "};";
dd225250
PS
8621
8622 // Emit the hwbkpt callback function
8623 s.op->newline() ;
8624 s.op->newline() << "static int enter_hwbkpt_probe (struct perf_event *bp,";
8625 s.op->line() << " int nmi,";
8626 s.op->line() << " struct perf_sample_data *data,";
8627 s.op->line() << " struct pt_regs *regs) {";
dac77b80
FCE
8628 s.op->newline(1) << "unsigned int i;";
8629 s.op->newline() << "if (bp->attr.type != PERF_TYPE_BREAKPOINT) return -1;";
8630 s.op->newline() << "for (i=0; i<" << hwbkpt_probes.size() << "; i++) {";
8631 s.op->newline(1) << "struct perf_event_attr *hp = & stap_hwbkpt_probe_array[i];";
8632 // XXX: why not match stap_hwbkpt_ret_array[i] against bp instead?
8633 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) {";
8634 s.op->newline(1) << "struct stap_hwbkpt_probe *sdp = &stap_hwbkpt_probes[i];";
6eefe942
MW
8635 common_probe_entryfn_prologue (s.op, "STAP_SESSION_RUNNING", "sdp->probe",
8636 "_STP_PROBE_HANDLER_HWBKPT");
d9aed31e
MW
8637 s.op->newline() << "if (user_mode(regs)) {";
8638 s.op->newline(1)<< "c->probe_flags |= _STP_PROBE_STATE_USER_MODE;";
8639 s.op->newline() << "c->uregs = regs;";
8640 s.op->newline(-1) << "} else {";
8641 s.op->newline(1) << "c->kregs = regs;";
8642 s.op->newline(-1) << "}";
26e63673 8643 s.op->newline() << "(*sdp->probe->ph) (c);";
7baf48e9 8644 common_probe_entryfn_epilogue (s.op, true, s.suppress_handler_errors);
dac77b80 8645 s.op->newline(-1) << "}";
dd225250
PS
8646 s.op->newline(-1) << "}";
8647 s.op->newline() << "return 0;";
dac77b80 8648 s.op->newline(-1) << "}";
dd225250
PS
8649}
8650
8651void
8652hwbkpt_derived_probe_group::emit_module_init (systemtap_session& s)
8653{
dac77b80 8654 s.op->newline() << "for (i=0; i<" << hwbkpt_probes.size() << "; i++) {";
dd225250
PS
8655 s.op->newline(1) << "struct stap_hwbkpt_probe *sdp = & stap_hwbkpt_probes[i];";
8656 s.op->newline() << "struct perf_event_attr *hp = & stap_hwbkpt_probe_array[i];";
8657 s.op->newline() << "void *addr = (void *) sdp->address;";
8658 s.op->newline() << "const char *hwbkpt_symbol_name = addr ? NULL : sdp->symbol;";
dac77b80
FCE
8659 s.op->newline() << "hw_breakpoint_init(hp);";
8660 s.op->newline() << "if (addr)";
8661 s.op->newline(1) << "hp->bp_addr = (unsigned long) addr;";
8662 s.op->newline(-1) << "else { ";
8663 s.op->newline(1) << "hp->bp_addr = kallsyms_lookup_name(hwbkpt_symbol_name);";
8664 s.op->newline() << "if (!hp->bp_addr) { ";
26e63673 8665 s.op->newline(1) << "_stp_warn(\"Probe %s registration skipped: invalid symbol %s \",sdp->probe->pp,hwbkpt_symbol_name);";
dac77b80
FCE
8666 s.op->newline() << "continue;";
8667 s.op->newline(-1) << "}";
8668 s.op->newline(-1) << "}";
8669 s.op->newline() << "hp->bp_type = sdp->atype;";
8670
8671 // On x86 & x86-64, hp->bp_len is not just a number but a macro/enum (!?!).
c57ea854 8672 if (s.architecture == "i386" || s.architecture == "x86_64" )
dac77b80
FCE
8673 {
8674 s.op->newline() << "switch(sdp->len) {";
8675 s.op->newline() << "case 1:";
8676 s.op->newline(1) << "hp->bp_len = HW_BREAKPOINT_LEN_1;";
8677 s.op->newline() << "break;";
8678 s.op->newline(-1) << "case 2:";
8679 s.op->newline(1) << "hp->bp_len = HW_BREAKPOINT_LEN_2;";
8680 s.op->newline() << "break;";
8681 s.op->newline(-1) << "case 3:";
8682 s.op->newline() << "case 4:";
8683 s.op->newline(1) << "hp->bp_len = HW_BREAKPOINT_LEN_4;";
8684 s.op->newline() << "break;";
8685 s.op->newline(-1) << "case 5:";
8686 s.op->newline() << "case 6:";
8687 s.op->newline() << "case 7:";
8688 s.op->newline() << "case 8:";
8689 s.op->newline() << "default:"; // XXX: could instead reject
8690 s.op->newline(1) << "hp->bp_len = HW_BREAKPOINT_LEN_8;";
8691 s.op->newline() << "break;";
8692 s.op->newline(-1) << "}";
8693 }
8694 else // other architectures presumed straightforward
8695 s.op->newline() << "hp->bp_len = sdp->len;";
8696
26e63673 8697 s.op->newline() << "probe_point = sdp->probe->pp;"; // for error messages
b273669e
MW
8698 s.op->newline() << "#ifdef STAPCONF_HW_BREAKPOINT_CONTEXT";
8699 s.op->newline() << "stap_hwbkpt_ret_array[i] = register_wide_hw_breakpoint(hp, (void *)&enter_hwbkpt_probe, NULL);";
8700 s.op->newline() << "#else";
dac77b80 8701 s.op->newline() << "stap_hwbkpt_ret_array[i] = register_wide_hw_breakpoint(hp, (void *)&enter_hwbkpt_probe);";
b273669e 8702 s.op->newline() << "#endif";
43650b10 8703 s.op->newline() << "rc = 0;";
dac77b80 8704 s.op->newline() << "if (IS_ERR(stap_hwbkpt_ret_array[i])) {";
43650b10
WC
8705 s.op->newline(1) << "rc = PTR_ERR(stap_hwbkpt_ret_array[i]);";
8706 s.op->newline() << "stap_hwbkpt_ret_array[i] = 0;";
8707 s.op->newline(-1) << "}";
217ef1f4
WC
8708 s.op->newline() << "if (rc) {";
8709 s.op->newline(1) << "_stp_warn(\"Hwbkpt probe %s: registration error %d, addr %p, name %s\", probe_point, rc, addr, hwbkpt_symbol_name);";
43650b10 8710 s.op->newline() << "sdp->registered_p = 0;";
dac77b80 8711 s.op->newline(-1) << "}";
dd225250 8712 s.op->newline() << " else sdp->registered_p = 1;";
dd225250
PS
8713 s.op->newline(-1) << "}"; // for loop
8714}
8715
8716void
8717hwbkpt_derived_probe_group::emit_module_exit (systemtap_session& s)
8718{
8719 //Unregister hwbkpt probes.
dac77b80 8720 s.op->newline() << "for (i=0; i<" << hwbkpt_probes.size() << "; i++) {";
dd225250 8721 s.op->newline(1) << "struct stap_hwbkpt_probe *sdp = & stap_hwbkpt_probes[i];";
dac77b80
FCE
8722 s.op->newline() << "if (sdp->registered_p == 0) continue;";
8723 s.op->newline() << "unregister_wide_hw_breakpoint(stap_hwbkpt_ret_array[i]);";
dd225250
PS
8724 s.op->newline() << "sdp->registered_p = 0;";
8725 s.op->newline(-1) << "}";
8726}
8727
8728struct hwbkpt_builder: public derived_probe_builder
8729{
8730 hwbkpt_builder() {}
8731 virtual void build(systemtap_session & sess,
8732 probe * base,
8733 probe_point * location,
8734 literal_map_t const & parameters,
8735 vector<derived_probe *> & finished_results);
8736};
8737
8738void
8739hwbkpt_builder::build(systemtap_session & sess,
8740 probe * base,
8741 probe_point * location,
8742 literal_map_t const & parameters,
8743 vector<derived_probe *> & finished_results)
8744{
8745 string symbol_str_val;
8746 int64_t hwbkpt_address, len;
8747 bool has_addr, has_symbol_str, has_write, has_rw, has_len;
8748
b47f3a55 8749 if (! (sess.kernel_config["CONFIG_PERF_EVENTS"] == string("y")))
b530b5b3 8750 throw semantic_error (_("CONFIG_PERF_EVENTS not available on this kernel"),
b47f3a55
FCE
8751 location->components[0]->tok);
8752 if (! (sess.kernel_config["CONFIG_HAVE_HW_BREAKPOINT"] == string("y")))
b530b5b3 8753 throw semantic_error (_("CONFIG_HAVE_HW_BREAKPOINT not available on this kernel"),
b47f3a55
FCE
8754 location->components[0]->tok);
8755
dd225250
PS
8756 has_addr = get_param (parameters, TOK_HWBKPT, hwbkpt_address);
8757 has_symbol_str = get_param (parameters, TOK_HWBKPT, symbol_str_val);
8758 has_len = get_param (parameters, TOK_LENGTH, len);
8759 has_write = (parameters.find(TOK_HWBKPT_WRITE) != parameters.end());
8760 has_rw = (parameters.find(TOK_HWBKPT_RW) != parameters.end());
8761
8762 if (!has_len)
8763 len = 1;
8764
8765 if (has_addr)
8766 finished_results.push_back (new hwbkpt_derived_probe (base,
8767 location,
8768 hwbkpt_address,
8769 "",len,0,
8770 has_write,
8771 has_rw));
5d8a0aea 8772 else if (has_symbol_str)
dd225250
PS
8773 finished_results.push_back (new hwbkpt_derived_probe (base,
8774 location,
8775 0,
8776 symbol_str_val,len,0,
8777 has_write,
8778 has_rw));
5d8a0aea
FCE
8779 else
8780 assert (0);
dd225250 8781}
342d3f96 8782
0a6f5a3f
JS
8783// ------------------------------------------------------------------------
8784// statically inserted kernel-tracepoint derived probes
8785// ------------------------------------------------------------------------
8786
6fb70fb7 8787struct tracepoint_arg
79189b84 8788{
ad370dcc 8789 string name, c_type, typecast;
dcaa1a65 8790 bool usable, used, isptr;
f8a968bc 8791 Dwarf_Die type_die;
dcaa1a65 8792 tracepoint_arg(): usable(false), used(false), isptr(false) {}
6fb70fb7 8793};
79189b84 8794
0a6f5a3f
JS
8795struct tracepoint_derived_probe: public derived_probe
8796{
79189b84
JS
8797 tracepoint_derived_probe (systemtap_session& s,
8798 dwflpp& dw, Dwarf_Die& func_die,
8799 const string& tracepoint_name,
8800 probe* base_probe, probe_point* location);
bc9a523d 8801
79189b84 8802 systemtap_session& sess;
6fb70fb7
JS
8803 string tracepoint_name, header;
8804 vector <struct tracepoint_arg> args;
bc9a523d 8805
6fb70fb7 8806 void build_args(dwflpp& dw, Dwarf_Die& func_die);
d0bfd2ac 8807 void getargs (std::list<std::string> &arg_set) const;
79189b84 8808 void join_group (systemtap_session& s);
3e3bd7b6 8809 void print_dupe_stamp(ostream& o);
0a6f5a3f 8810};
79189b84
JS
8811
8812
0a6f5a3f 8813struct tracepoint_derived_probe_group: public generic_dpg<tracepoint_derived_probe>
79189b84 8814{
79189b84
JS
8815 void emit_module_decls (systemtap_session& s);
8816 void emit_module_init (systemtap_session& s);
8817 void emit_module_exit (systemtap_session& s);
0a6f5a3f 8818};
79189b84 8819
bc9a523d 8820
f8a968bc
JS
8821struct tracepoint_var_expanding_visitor: public var_expanding_visitor
8822{
8823 tracepoint_var_expanding_visitor(dwflpp& dw, const string& probe_name,
8824 vector <struct tracepoint_arg>& args):
8825 dw (dw), probe_name (probe_name), args (args) {}
8826 dwflpp& dw;
8827 const string& probe_name;
8828 vector <struct tracepoint_arg>& args;
bc9a523d 8829
f8a968bc
JS
8830 void visit_target_symbol (target_symbol* e);
8831 void visit_target_symbol_arg (target_symbol* e);
8832 void visit_target_symbol_context (target_symbol* e);
8833};
79189b84
JS
8834
8835
f8a968bc
JS
8836void
8837tracepoint_var_expanding_visitor::visit_target_symbol_arg (target_symbol* e)
75ead1f7 8838{
cc9001af 8839 string argname = e->sym_name();
75ead1f7 8840
f8a968bc
JS
8841 // search for a tracepoint parameter matching this name
8842 tracepoint_arg *arg = NULL;
8843 for (unsigned i = 0; i < args.size(); ++i)
dcaa1a65 8844 if (args[i].usable && args[i].name == argname)
f8a968bc
JS
8845 {
8846 arg = &args[i];
8847 arg->used = true;
8848 break;
8849 }
75ead1f7 8850
f8a968bc
JS
8851 if (arg == NULL)
8852 {
8853 stringstream alternatives;
8854 for (unsigned i = 0; i < args.size(); ++i)
8855 alternatives << " $" << args[i].name;
046e7190 8856 alternatives << " $$name $$parms $$vars";
75ead1f7 8857
f8a968bc
JS
8858 // We hope that this value ends up not being referenced after all, so it
8859 // can be optimized out quietly.
b530b5b3
LB
8860 throw semantic_error(_F("unable to find tracepoint variable '%s' (alternatives: %s)",
8861 e->name.c_str(), alternatives.str().c_str()), e->tok);
f8a968bc
JS
8862 // NB: we can have multiple errors, since a target variable
8863 // may be expanded in several different contexts:
8864 // trace ("*") { $foo->bar }
f8a968bc 8865 }
75ead1f7 8866
f8a968bc 8867 // make sure we're not dereferencing base types
dc5a09fc 8868 if (!arg->isptr)
d19a9a82 8869 e->assert_no_components("tracepoint", true);
75ead1f7 8870
f8a968bc
JS
8871 // we can only write to dereferenced fields, and only if guru mode is on
8872 bool lvalue = is_active_lvalue(e);
8873 if (lvalue && (!dw.sess.guru_mode || e->components.empty()))
b3741c9d 8874 throw semantic_error(_F("write to tracepoint variable '%s' not permitted; need stap -g", e->name.c_str()), e->tok);
c69a87e0 8875
ad370dcc
JS
8876 // XXX: if a struct/union arg is passed by value, then writing to its fields
8877 // is also meaningless until you dereference past a pointer member. It's
8878 // harder to detect and prevent that though...
75ead1f7 8879
f8a968bc
JS
8880 if (e->components.empty())
8881 {
03c75a4a 8882 if (e->addressof)
b530b5b3 8883 throw semantic_error(_("cannot take address of tracepoint variable"), e->tok);
a45664f4 8884
3e3bd7b6 8885 // Just grab the value from the probe locals
a45664f4
JS
8886 symbol* sym = new symbol;
8887 sym->tok = e->tok;
8888 sym->name = "__tracepoint_arg_" + arg->name;
8889 provide (sym);
f8a968bc
JS
8890 }
8891 else
8892 {
5f36109e
JS
8893 // make a copy of the original as a bare target symbol for the tracepoint
8894 // value, which will be passed into the dwarf dereferencing code
8895 target_symbol* e2 = deep_copy_visitor::deep_copy(e);
8896 e2->components.clear();
8897
8898 if (e->components.back().type == target_symbol::comp_pretty_print)
8899 {
8900 if (lvalue)
b530b5b3 8901 throw semantic_error(_("cannot write to pretty-printed variable"), e->tok);
5f36109e 8902
d19a9a82 8903 dwarf_pretty_print dpp(dw, &arg->type_die, e2, arg->isptr, false, *e);
5f36109e
JS
8904 dpp.expand()->visit (this);
8905 return;
8906 }
8907
f8a968bc
JS
8908 // Synthesize a function to dereference the dwarf fields,
8909 // with a pointer parameter that is the base tracepoint variable
8910 functiondecl *fdecl = new functiondecl;
59de45f1 8911 fdecl->synthetic = true;
f8a968bc
JS
8912 fdecl->tok = e->tok;
8913 embeddedcode *ec = new embeddedcode;
8914 ec->tok = e->tok;
75ead1f7 8915
f8a968bc 8916 string fname = (string(lvalue ? "_tracepoint_tvar_set" : "_tracepoint_tvar_get")
cc9001af 8917 + "_" + e->sym_name()
aca66a36 8918 + "_" + lex_cast(tick++));
75ead1f7 8919
f8a968bc
JS
8920 fdecl->name = fname;
8921 fdecl->body = ec;
75ead1f7 8922
a5ce5211 8923 ec->code += EMBEDDED_FETCH_DEREF(false);
c69a87e0 8924 ec->code += dw.literal_stmt_for_pointer (&arg->type_die, e,
f8a968bc 8925 lvalue, fdecl->type);
75ead1f7 8926
f8a968bc
JS
8927 // Give the fdecl an argument for the raw tracepoint value
8928 vardecl *v1 = new vardecl;
8929 v1->type = pe_long;
8930 v1->name = "pointer";
8931 v1->tok = e->tok;
8932 fdecl->formal_args.push_back(v1);
75ead1f7 8933
6fda2dff
JS
8934 // Any non-literal indexes need to be passed in too.
8935 for (unsigned i = 0; i < e->components.size(); ++i)
8936 if (e->components[i].type == target_symbol::comp_expression_array_index)
8937 {
8938 vardecl *v = new vardecl;
8939 v->type = pe_long;
aca66a36 8940 v->name = "index" + lex_cast(i);
6fda2dff
JS
8941 v->tok = e->tok;
8942 fdecl->formal_args.push_back(v);
8943 }
8944
f8a968bc
JS
8945 if (lvalue)
8946 {
8947 // Modify the fdecl so it carries a pe_long formal
8948 // argument called "value".
75ead1f7 8949
f8a968bc
JS
8950 // FIXME: For the time being we only support setting target
8951 // variables which have base types; these are 'pe_long' in
8952 // stap's type vocabulary. Strings and pointers might be
8953 // reasonable, some day, but not today.
8954
8955 vardecl *v2 = new vardecl;
8956 v2->type = pe_long;
8957 v2->name = "value";
8958 v2->tok = e->tok;
8959 fdecl->formal_args.push_back(v2);
8960 }
8961 else
8962 ec->code += "/* pure */";
8963
64211010 8964 ec->code += "/* unprivileged */";
a5ce5211 8965 ec->code += EMBEDDED_FETCH_DEREF_DONE;
aff5d390 8966
f8809d54 8967 fdecl->join (dw.sess);
75ead1f7 8968
f8a968bc
JS
8969 // Synthesize a functioncall.
8970 functioncall* n = new functioncall;
8971 n->tok = e->tok;
8972 n->function = fname;
6fda2dff
JS
8973 n->args.push_back(require(e2));
8974
8975 // Any non-literal indexes need to be passed in too.
8976 for (unsigned i = 0; i < e->components.size(); ++i)
8977 if (e->components[i].type == target_symbol::comp_expression_array_index)
8978 n->args.push_back(require(e->components[i].expr_index));
75ead1f7 8979
f8a968bc
JS
8980 if (lvalue)
8981 {
8982 // Provide the functioncall to our parent, so that it can be
8983 // used to substitute for the assignment node immediately above
8984 // us.
8985 assert(!target_symbol_setter_functioncalls.empty());
8986 *(target_symbol_setter_functioncalls.top()) = n;
8987 }
75ead1f7 8988
f8a968bc
JS
8989 provide (n);
8990 }
75ead1f7
JS
8991}
8992
8993
f8a968bc
JS
8994void
8995tracepoint_var_expanding_visitor::visit_target_symbol_context (target_symbol* e)
0a6f5a3f 8996{
03c75a4a 8997 if (e->addressof)
b530b5b3 8998 throw semantic_error(_("cannot take address of context variable"), e->tok);
03c75a4a 8999
f8a968bc 9000 if (is_active_lvalue (e))
b530b5b3 9001 throw semantic_error(_F("write to tracepoint '%s' not permitted", e->name.c_str()), e->tok);
0a6f5a3f 9002
277c21bc 9003 if (e->name == "$$name")
f8a968bc 9004 {
5f36109e
JS
9005 e->assert_no_components("tracepoint");
9006
bfdaad1e
DS
9007 // Synthesize an embedded expression.
9008 embedded_expr *expr = new embedded_expr;
9009 expr->tok = e->tok;
9010 expr->code = string("/* string */ /* pure */ ")
6dceb5c9 9011 + string("c->ips.tracepoint_name ? c->ips.tracepoint_name : \"\"");
bfdaad1e 9012 provide (expr);
f8a968bc 9013 }
277c21bc 9014 else if (e->name == "$$vars" || e->name == "$$parms")
f8a968bc 9015 {
5f36109e
JS
9016 e->assert_no_components("tracepoint", true);
9017
f8a968bc
JS
9018 token* pf_tok = new token(*e->tok);
9019 pf_tok->content = "sprintf";
0a6f5a3f 9020
d5e178c1 9021 print_format* pf = print_format::create(pf_tok);
0a6f5a3f 9022
f8a968bc 9023 for (unsigned i = 0; i < args.size(); ++i)
b278033a 9024 {
dcaa1a65
JS
9025 if (!args[i].usable)
9026 continue;
f8a968bc
JS
9027 if (i > 0)
9028 pf->raw_components += " ";
9029 pf->raw_components += args[i].name;
3e3bd7b6 9030 target_symbol *tsym = new target_symbol;
f8a968bc 9031 tsym->tok = e->tok;
277c21bc 9032 tsym->name = "$" + args[i].name;
5f36109e 9033 tsym->components = e->components;
b278033a 9034
f8a968bc
JS
9035 // every variable should always be accessible!
9036 tsym->saved_conversion_error = 0;
9037 expression *texp = require (tsym); // NB: throws nothing ...
14a97852
JS
9038 if (tsym->saved_conversion_error) // ... but this is how we know it happened.
9039 {
9040 if (dw.sess.verbose>2)
e26c2f83 9041 for (const semantic_error *c = tsym->saved_conversion_error;
14a97852 9042 c != 0; c = c->chain)
b530b5b3 9043 clog << _("variable location problem: ") << c->what() << endl;
14a97852
JS
9044 pf->raw_components += "=?";
9045 continue;
9046 }
b278033a 9047
5f36109e
JS
9048 if (!e->components.empty() &&
9049 e->components[0].type == target_symbol::comp_pretty_print)
9050 pf->raw_components += "=%s";
9051 else
9052 pf->raw_components += args[i].isptr ? "=%p" : "=%#x";
f8a968bc
JS
9053 pf->args.push_back(texp);
9054 }
0a6f5a3f 9055
f8a968bc
JS
9056 pf->components = print_format::string_to_components(pf->raw_components);
9057 provide (pf);
b278033a 9058 }
f8a968bc
JS
9059 else
9060 assert(0); // shouldn't get here
0a6f5a3f
JS
9061}
9062
0a6f5a3f 9063void
f8a968bc 9064tracepoint_var_expanding_visitor::visit_target_symbol (target_symbol* e)
0a6f5a3f 9065{
aff5d390 9066 try
c69a87e0 9067 {
8fa5f271
MW
9068 assert(e->name.size() > 0
9069 && ((e->name[0] == '$' && e->target_name == "")
9070 || (e->name == "@var" && e->target_name != "")));
aff5d390 9071
277c21bc 9072 if (e->name == "$$name" || e->name == "$$parms" || e->name == "$$vars")
c69a87e0 9073 visit_target_symbol_context (e);
8fa5f271
MW
9074 else if (e->name == "@var")
9075 throw semantic_error(_("cannot use @var DWARF variables in tracepoints"), e->tok);
c69a87e0
FCE
9076 else
9077 visit_target_symbol_arg (e);
9078 }
9079 catch (const semantic_error &er)
9080 {
1af1e62d 9081 e->chain (er);
c69a87e0
FCE
9082 provide (e);
9083 }
0a6f5a3f
JS
9084}
9085
9086
9087
79189b84
JS
9088tracepoint_derived_probe::tracepoint_derived_probe (systemtap_session& s,
9089 dwflpp& dw, Dwarf_Die& func_die,
9090 const string& tracepoint_name,
9091 probe* base, probe_point* loc):
4c5d1300 9092 derived_probe (base, loc, true /* .components soon rewritten */),
79189b84 9093 sess (s), tracepoint_name (tracepoint_name)
56894e91 9094{
79189b84
JS
9095 // create synthetic probe point name; preserve condition
9096 vector<probe_point::component*> comps;
9097 comps.push_back (new probe_point::component (TOK_KERNEL));
9098 comps.push_back (new probe_point::component (TOK_TRACE, new literal_string (tracepoint_name)));
9099 this->sole_location()->components = comps;
9100
6fb70fb7
JS
9101 // fill out the available arguments in this tracepoint
9102 build_args(dw, func_die);
56894e91 9103
6fb70fb7
JS
9104 // determine which header defined this tracepoint
9105 string decl_file = dwarf_decl_file(&func_die);
d4393459
FCE
9106 header = decl_file;
9107
9108#if 0 /* This convention is not enforced. */
6fb70fb7
JS
9109 size_t header_pos = decl_file.rfind("trace/");
9110 if (header_pos == string::npos)
9111 throw semantic_error ("cannot parse header location for tracepoint '"
9112 + tracepoint_name + "' in '"
9113 + decl_file + "'");
9114 header = decl_file.substr(header_pos);
d4393459 9115#endif
56894e91 9116
6fb70fb7
JS
9117 // tracepoints from FOO_event_types.h should really be included from FOO.h
9118 // XXX can dwarf tell us the include hierarchy? it would be better to
9119 // ... walk up to see which one was directly included by tracequery.c
3c1b3d06 9120 // XXX: see also PR9993.
d4393459 9121 size_t header_pos = header.find("_event_types");
6fb70fb7
JS
9122 if (header_pos != string::npos)
9123 header.erase(header_pos, 12);
56894e91 9124
f8a968bc
JS
9125 // Now expand the local variables in the probe body
9126 tracepoint_var_expanding_visitor v (dw, name, args);
8b095b45 9127 v.replace (this->body);
a45664f4
JS
9128 for (unsigned i = 0; i < args.size(); i++)
9129 if (args[i].used)
9130 {
9131 vardecl* v = new vardecl;
9132 v->name = "__tracepoint_arg_" + args[i].name;
9133 v->tok = this->tok;
58701b78 9134 v->set_arity(0, this->tok);
a45664f4
JS
9135 v->type = pe_long;
9136 v->skip_init = true;
9137 this->locals.push_back (v);
9138 }
56894e91 9139
79189b84 9140 if (sess.verbose > 2)
ce0f6648 9141 clog << "tracepoint-based " << name << " tracepoint='" << tracepoint_name << "'" << endl;
79189b84 9142}
dc38c0ae 9143
56894e91 9144
f8a968bc 9145static bool
dcaa1a65 9146resolve_tracepoint_arg_type(tracepoint_arg& arg)
46b84a80 9147{
d19a9a82 9148 Dwarf_Die type;
dcaa1a65 9149 switch (dwarf_tag(&arg.type_die))
b20febf3 9150 {
f8a968bc
JS
9151 case DW_TAG_typedef:
9152 case DW_TAG_const_type:
9153 case DW_TAG_volatile_type:
9154 // iterate on the referent type
3d1ad340 9155 return (dwarf_attr_die(&arg.type_die, DW_AT_type, &arg.type_die)
dcaa1a65 9156 && resolve_tracepoint_arg_type(arg));
f8a968bc 9157 case DW_TAG_base_type:
a52d2ac0 9158 case DW_TAG_enumeration_type:
f8a968bc 9159 // base types will simply be treated as script longs
dcaa1a65 9160 arg.isptr = false;
f8a968bc
JS
9161 return true;
9162 case DW_TAG_pointer_type:
dcaa1a65
JS
9163 // pointers can be treated as script longs,
9164 // and if we know their type, they can also be dereferenced
d19a9a82
JS
9165 type = arg.type_die;
9166 while (dwarf_attr_die(&arg.type_die, DW_AT_type, &arg.type_die))
9167 {
9168 // It still might be a non-type, e.g. const void,
9169 // so we need to strip away all qualifiers.
9170 int tag = dwarf_tag(&arg.type_die);
9171 if (tag != DW_TAG_typedef &&
9172 tag != DW_TAG_const_type &&
9173 tag != DW_TAG_volatile_type)
9174 {
9175 arg.isptr = true;
9176 break;
9177 }
9178 }
9179 if (!arg.isptr)
9180 arg.type_die = type;
ad370dcc
JS
9181 arg.typecast = "(intptr_t)";
9182 return true;
9183 case DW_TAG_structure_type:
9184 case DW_TAG_union_type:
9185 // for structs/unions which are passed by value, we turn it into
9186 // a pointer that can be dereferenced.
9187 arg.isptr = true;
9188 arg.typecast = "(intptr_t)&";
dcaa1a65 9189 return true;
f8a968bc
JS
9190 default:
9191 // should we consider other types too?
9192 return false;
b20febf3 9193 }
56894e91
JS
9194}
9195
9196
9197void
822a6a3d 9198tracepoint_derived_probe::build_args(dwflpp&, Dwarf_Die& func_die)
56894e91 9199{
6fb70fb7
JS
9200 Dwarf_Die arg;
9201 if (dwarf_child(&func_die, &arg) == 0)
9202 do
9203 if (dwarf_tag(&arg) == DW_TAG_formal_parameter)
9204 {
9205 // build a tracepoint_arg for this parameter
9206 tracepoint_arg tparg;
23d106b9 9207 tparg.name = dwarf_diename(&arg);
56894e91 9208
6fb70fb7 9209 // read the type of this parameter
3d1ad340 9210 if (!dwarf_attr_die (&arg, DW_AT_type, &tparg.type_die)
f1c8f8a5 9211 || !dwarf_type_name(&tparg.type_die, tparg.c_type))
ce0f6648
LB
9212 throw semantic_error (_F("cannot get type of parameter '%s' of tracepoint '%s'",
9213 tparg.name.c_str(), tracepoint_name.c_str()));
a68f81a2 9214
dcaa1a65 9215 tparg.usable = resolve_tracepoint_arg_type(tparg);
6fb70fb7
JS
9216 args.push_back(tparg);
9217 if (sess.verbose > 4)
a52d2ac0
JS
9218 clog << _F("found parameter for tracepoint '%s': type:'%s' name:'%s' %s",
9219 tracepoint_name.c_str(), tparg.c_type.c_str(), tparg.name.c_str(),
9220 tparg.usable ? "ok" : "unavailable") << endl;
6fb70fb7
JS
9221 }
9222 while (dwarf_siblingof(&arg, &arg) == 0);
56894e91
JS
9223}
9224
dc38c0ae 9225void
d0bfd2ac 9226tracepoint_derived_probe::getargs(std::list<std::string> &arg_set) const
dc38c0ae 9227{
dcaa1a65
JS
9228 for (unsigned i = 0; i < args.size(); ++i)
9229 if (args[i].usable)
d0bfd2ac 9230 arg_set.push_back("$"+args[i].name+":"+args[i].c_type);
dc38c0ae
DS
9231}
9232
79189b84
JS
9233void
9234tracepoint_derived_probe::join_group (systemtap_session& s)
197a4d62 9235{
79189b84
JS
9236 if (! s.tracepoint_derived_probes)
9237 s.tracepoint_derived_probes = new tracepoint_derived_probe_group ();
9238 s.tracepoint_derived_probes->enroll (this);
9239}
e38d6504 9240
56894e91 9241
197a4d62 9242void
3e3bd7b6 9243tracepoint_derived_probe::print_dupe_stamp(ostream& o)
56894e91 9244{
3e3bd7b6
JS
9245 for (unsigned i = 0; i < args.size(); i++)
9246 if (args[i].used)
9247 o << "__tracepoint_arg_" << args[i].name << endl;
197a4d62 9248}
56894e91 9249
3e3bd7b6 9250
c9ccb642 9251static vector<string> tracepoint_extra_decls (systemtap_session& s, const string& header)
47dd066d 9252{
3c1b3d06
FCE
9253 vector<string> they_live;
9254 // PR 9993
9255 // XXX: may need this to be configurable
d4393459 9256 they_live.push_back ("#include <linux/skbuff.h>");
9e0cd21a
FCE
9257
9258 // PR11649: conditional extra header
9259 // for kvm tracepoints in 2.6.33ish
9260 if (s.kernel_config["CONFIG_KVM"] != string("")) {
d4393459
FCE
9261 they_live.push_back ("#include <linux/kvm_host.h>");
9262 }
9263
50b72692 9264 if (header.find("xfs") != string::npos && s.kernel_config["CONFIG_XFS_FS"] != string("")) {
d4393459 9265 they_live.push_back ("#define XFS_BIG_BLKNOS 1");
88637c31
FCE
9266 if (s.kernel_source_tree != "")
9267 they_live.push_back ("#include \"fs/xfs/xfs_types.h\""); // in kernel-source tree
d4393459
FCE
9268 they_live.push_back ("struct xfs_mount;");
9269 they_live.push_back ("struct xfs_inode;");
9270 they_live.push_back ("struct xfs_buf;");
9271 they_live.push_back ("struct xfs_bmbt_irec;");
c2cf1b87 9272 they_live.push_back ("struct xfs_trans;");
9e0cd21a 9273 }
d4393459 9274
50b72692 9275 if (header.find("nfs") != string::npos && s.kernel_config["CONFIG_NFSD"] != string("")) {
d4393459
FCE
9276 they_live.push_back ("struct rpc_task;");
9277 }
9278
9279 they_live.push_back ("#include <asm/cputime.h>");
9280
c2cf1b87
FCE
9281 // linux 3.0
9282 they_live.push_back ("struct cpu_workqueue_struct;");
9283
50b72692 9284 if (header.find("ext4") != string::npos && s.kernel_config["CONFIG_EXT4_FS"] != string(""))
c2cf1b87
FCE
9285 if (s.kernel_source_tree != "")
9286 they_live.push_back ("#include \"fs/ext4/ext4.h\""); // in kernel-source tree
9287
50b72692
HP
9288 if (header.find("ext3") != string::npos && s.kernel_config["CONFIG_EXT3_FS"] != string(""))
9289 they_live.push_back ("struct ext3_reserve_window_node;");
9290
3c1b3d06
FCE
9291 return they_live;
9292}
47dd066d
WC
9293
9294
9295void
79189b84 9296tracepoint_derived_probe_group::emit_module_decls (systemtap_session& s)
47dd066d 9297{
79189b84
JS
9298 if (probes.empty())
9299 return;
47dd066d 9300
96b030fe
JS
9301 s.op->newline() << "/* ---- tracepoint probes ---- */";
9302 s.op->newline();
79189b84 9303
47dd066d 9304
a4b9c3b3
FCE
9305 // We create a MODULE_aux_N.c file for each tracepoint header, to allow them
9306 // to be separately compiled. That's because kernel tracepoint headers sometimes
9307 // conflict. PR13155.
9308
9309 map<string,translator_output*> per_header_aux;
9310 // GC NB: the translator_output* structs are owned/retained by the systemtap_session.
47dd066d 9311
6fb70fb7
JS
9312 for (unsigned i = 0; i < probes.size(); ++i)
9313 {
9314 tracepoint_derived_probe *p = probes[i];
75ae2ec9 9315 string header = p->header;
5f73a260 9316
a4b9c3b3
FCE
9317 // We cache the auxiliary output files on a per-header basis. We don't
9318 // need one aux file per tracepoint, only one per tracepoint-header.
9319 translator_output *tpop = per_header_aux[header];
9320 if (tpop == 0)
9321 {
9322 tpop = s.op_create_auxiliary();
9323 per_header_aux[header] = tpop;
9324
9325 // PR9993: Add extra headers to work around undeclared types in individual
9326 // include/trace/foo.h files
9327 const vector<string>& extra_decls = tracepoint_extra_decls (s, header);
9328 for (unsigned z=0; z<extra_decls.size(); z++)
9329 tpop->newline() << extra_decls[z] << "\n";
720c435f 9330
a4b9c3b3
FCE
9331 // strip include/ substring, the same way as done in get_tracequery_module()
9332 size_t root_pos = header.rfind("include/");
9333 header = ((root_pos != string::npos) ? header.substr(root_pos + 8) : header);
bfffa443
FCE
9334
9335 tpop->newline() << "#include <linux/tracepoint.h>" << endl;
a4b9c3b3 9336 tpop->newline() << "#include <" << header << ">";
720c435f 9337
a4b9c3b3
FCE
9338 // Starting in 2.6.35, at the same time NOARGS was added, the callback
9339 // always has a void* as the first parameter. PR11599
9340 tpop->newline() << "#ifdef DECLARE_TRACE_NOARGS";
9341 tpop->newline() << "#define STAP_TP_DATA , NULL";
9342 tpop->newline() << "#define STAP_TP_PROTO void *cb_data"
9343 << " __attribute__ ((unused))";
9344 if (!p->args.empty())
9345 tpop->line() << ",";
9346 tpop->newline() << "#else";
9347 tpop->newline() << "#define STAP_TP_DATA";
9348 tpop->newline() << "#define STAP_TP_PROTO";
9349 if (p->args.empty())
9350 tpop->line() << " void";
9351 tpop->newline() << "#endif";
720c435f
JS
9352
9353 tpop->newline() << "#define intptr_t long";
a4b9c3b3
FCE
9354 }
9355
720c435f
JS
9356 // collect the args that are actually in use
9357 vector<const tracepoint_arg*> used_args;
6fb70fb7 9358 for (unsigned j = 0; j < p->args.size(); ++j)
720c435f
JS
9359 if (p->args[j].used)
9360 used_args.push_back(&p->args[j]);
9361
a4b9c3b3
FCE
9362 // forward-declare the generated-side tracepoint callback
9363 tpop->newline() << "void enter_real_tracepoint_probe_" << i << "(";
720c435f
JS
9364 tpop->indent(2);
9365 if (used_args.empty())
9366 tpop->line() << "void";
9367 for (unsigned j = 0; j < used_args.size(); ++j)
6fb70fb7
JS
9368 {
9369 if (j > 0)
a4b9c3b3
FCE
9370 tpop->line() << ", ";
9371 tpop->line() << "int64_t";
6fb70fb7 9372 }
720c435f
JS
9373 tpop->line() << ");";
9374 tpop->indent(-2);
5f73a260 9375
a4b9c3b3
FCE
9376 // define the generated-side tracepoint callback - in the main translator-output
9377 s.op->newline() << "void enter_real_tracepoint_probe_" << i << "(";
720c435f
JS
9378 s.op->indent(2);
9379 if (used_args.empty())
9380 s.op->newline() << "void";
9381 for (unsigned j = 0; j < used_args.size(); ++j)
a4b9c3b3 9382 {
a4b9c3b3
FCE
9383 if (j > 0)
9384 s.op->line() << ", ";
720c435f 9385 s.op->newline() << "int64_t __tracepoint_arg_" << used_args[j]->name;
6fb70fb7 9386 }
5f73a260
JS
9387 s.op->newline() << ")";
9388 s.op->newline(-2) << "{";
26e63673 9389 s.op->newline(1) << "struct stap_probe * const probe = "
faea5e16 9390 << common_probe_init (p) << ";";
6eefe942
MW
9391 common_probe_entryfn_prologue (s.op, "STAP_SESSION_RUNNING", "probe",
9392 "_STP_PROBE_HANDLER_TRACEPOINT");
6dceb5c9 9393 s.op->newline() << "c->ips.tracepoint_name = "
c12d974f
FCE
9394 << lex_cast_qstring (p->tracepoint_name)
9395 << ";";
720c435f
JS
9396 for (unsigned j = 0; j < used_args.size(); ++j)
9397 {
9398 s.op->newline() << "c->probe_locals." << p->name
9399 << ".__tracepoint_arg_" << used_args[j]->name
9400 << " = __tracepoint_arg_" << used_args[j]->name << ";";
9401 }
26e63673 9402 s.op->newline() << "(*probe->ph) (c);";
7baf48e9 9403 common_probe_entryfn_epilogue (s.op, true, s.suppress_handler_errors);
6fb70fb7 9404 s.op->newline(-1) << "}";
47dd066d 9405
a4b9c3b3 9406 // define the real tracepoint callback function
720c435f 9407 tpop->newline() << "static void enter_tracepoint_probe_" << i << "(";
a4b9c3b3
FCE
9408 tpop->newline(2) << "STAP_TP_PROTO";
9409 for (unsigned j = 0; j < p->args.size(); ++j)
9410 {
9411 if (j > 0)
9412 tpop->line() << ", ";
9413 tpop->newline() << p->args[j].c_type << " __tracepoint_arg_" << p->args[j].name;
9414 }
9415 tpop->newline() << ")";
9416 tpop->newline(-2) << "{";
720c435f
JS
9417 tpop->newline(1) << "enter_real_tracepoint_probe_" << i << "(";
9418 tpop->indent(2);
9419 for (unsigned j = 0; j < used_args.size(); ++j)
9420 {
9421 if (j > 0)
9422 tpop->line() << ", ";
9423 tpop->newline() << "(int64_t)" << used_args[j]->typecast
9424 << "__tracepoint_arg_" << used_args[j]->name;
9425 }
9426 tpop->newline() << ");";
9427 tpop->newline(-3) << "}";
a4b9c3b3
FCE
9428
9429
96b030fe 9430 // emit normalized registration functions
720c435f 9431 tpop->newline() << "int register_tracepoint_probe_" << i << "(void) {";
a4b9c3b3 9432 tpop->newline(1) << "return register_trace_" << p->tracepoint_name
5f73a260 9433 << "(enter_tracepoint_probe_" << i << " STAP_TP_DATA);";
a4b9c3b3 9434 tpop->newline(-1) << "}";
47dd066d 9435
86758d5f
JS
9436 // NB: we're not prepared to deal with unreg failures. However, failures
9437 // can only occur if the tracepoint doesn't exist (yet?), or if we
9438 // weren't even registered. The former should be OKed by the initial
9439 // registration call, and the latter is safe to ignore.
720c435f 9440 tpop->newline() << "void unregister_tracepoint_probe_" << i << "(void) {";
a4b9c3b3 9441 tpop->newline(1) << "(void) unregister_trace_" << p->tracepoint_name
5f73a260 9442 << "(enter_tracepoint_probe_" << i << " STAP_TP_DATA);";
a4b9c3b3
FCE
9443 tpop->newline(-1) << "}";
9444 tpop->newline();
5f73a260 9445
720c435f
JS
9446 // declare normalized registration functions
9447 s.op->newline() << "int register_tracepoint_probe_" << i << "(void);";
9448 s.op->newline() << "void unregister_tracepoint_probe_" << i << "(void);";
5f73a260 9449
a4b9c3b3 9450 tpop->assert_0_indent();
af304783
DS
9451 }
9452
96b030fe
JS
9453 // emit an array of registration functions for easy init/shutdown
9454 s.op->newline() << "static struct stap_tracepoint_probe {";
9455 s.op->newline(1) << "int (*reg)(void);";
86758d5f 9456 s.op->newline(0) << "void (*unreg)(void);";
96b030fe
JS
9457 s.op->newline(-1) << "} stap_tracepoint_probes[] = {";
9458 s.op->indent(1);
9459 for (unsigned i = 0; i < probes.size(); ++i)
9460 {
9461 s.op->newline () << "{";
9462 s.op->line() << " .reg=&register_tracepoint_probe_" << i << ",";
9463 s.op->line() << " .unreg=&unregister_tracepoint_probe_" << i;
9464 s.op->line() << " },";
9465 }
9466 s.op->newline(-1) << "};";
9467 s.op->newline();
47dd066d
WC
9468}
9469
9470
79189b84
JS
9471void
9472tracepoint_derived_probe_group::emit_module_init (systemtap_session &s)
47dd066d 9473{
79189b84
JS
9474 if (probes.size () == 0)
9475 return;
47dd066d 9476
79189b84 9477 s.op->newline() << "/* init tracepoint probes */";
96b030fe
JS
9478 s.op->newline() << "for (i=0; i<" << probes.size() << "; i++) {";
9479 s.op->newline(1) << "rc = stap_tracepoint_probes[i].reg();";
9480 s.op->newline() << "if (rc) {";
9481 s.op->newline(1) << "for (j=i-1; j>=0; j--)"; // partial rollback
9482 s.op->newline(1) << "stap_tracepoint_probes[j].unreg();";
9483 s.op->newline(-1) << "break;"; // don't attempt to register any more probes
9484 s.op->newline(-1) << "}";
9485 s.op->newline(-1) << "}";
47dd066d 9486
bc9a523d
FCE
9487 // This would be technically proper (on those autoconf-detectable
9488 // kernels that include this function in tracepoint.h), however we
9489 // already make several calls to synchronze_sched() during our
9490 // shutdown processes.
47dd066d 9491
bc9a523d
FCE
9492 // s.op->newline() << "if (rc)";
9493 // s.op->newline(1) << "tracepoint_synchronize_unregister();";
9494 // s.op->indent(-1);
79189b84 9495}
47dd066d
WC
9496
9497
79189b84
JS
9498void
9499tracepoint_derived_probe_group::emit_module_exit (systemtap_session& s)
47dd066d 9500{
79189b84
JS
9501 if (probes.empty())
9502 return;
47dd066d 9503
96b030fe
JS
9504 s.op->newline() << "/* deregister tracepoint probes */";
9505 s.op->newline() << "for (i=0; i<" << probes.size() << "; i++)";
9506 s.op->newline(1) << "stap_tracepoint_probes[i].unreg();";
9507 s.op->indent(-1);
47dd066d 9508
bc9a523d 9509 // Not necessary: see above.
47dd066d 9510
bc9a523d 9511 // s.op->newline() << "tracepoint_synchronize_unregister();";
79189b84 9512}
b20febf3 9513
47dd066d 9514
75ead1f7 9515struct tracepoint_query : public base_query
47dd066d 9516{
75ead1f7
JS
9517 tracepoint_query(dwflpp & dw, const string & tracepoint,
9518 probe * base_probe, probe_point * base_loc,
9519 vector<derived_probe *> & results):
9520 base_query(dw, "*"), tracepoint(tracepoint),
9521 base_probe(base_probe), base_loc(base_loc),
9522 results(results) {}
47dd066d 9523
75ead1f7 9524 const string& tracepoint;
47dd066d 9525
75ead1f7
JS
9526 probe * base_probe;
9527 probe_point * base_loc;
9528 vector<derived_probe *> & results;
f982c59b 9529 set<string> probed_names;
47dd066d 9530
75ead1f7
JS
9531 void handle_query_module();
9532 int handle_query_cu(Dwarf_Die * cudie);
9533 int handle_query_func(Dwarf_Die * func);
822a6a3d 9534 void query_library (const char *) {}
576eaefe 9535 void query_plt (const char *entry, size_t addr) {}
b20febf3 9536
75ead1f7
JS
9537 static int tracepoint_query_cu (Dwarf_Die * cudie, void * arg);
9538 static int tracepoint_query_func (Dwarf_Die * func, base_query * query);
9539};
47dd066d
WC
9540
9541
9542void
75ead1f7 9543tracepoint_query::handle_query_module()
47dd066d 9544{
75ead1f7 9545 // look for the tracepoints in each CU
337b7c44 9546 dw.iterate_over_cus(tracepoint_query_cu, this, false);
47dd066d
WC
9547}
9548
9549
75ead1f7
JS
9550int
9551tracepoint_query::handle_query_cu(Dwarf_Die * cudie)
47dd066d 9552{
75ead1f7 9553 dw.focus_on_cu (cudie);
47dd066d 9554
75ead1f7
JS
9555 // look at each function to see if it's a tracepoint
9556 string function = "stapprobe_" + tracepoint;
9557 return dw.iterate_over_functions (tracepoint_query_func, this, function);
47dd066d
WC
9558}
9559
9560
75ead1f7
JS
9561int
9562tracepoint_query::handle_query_func(Dwarf_Die * func)
47dd066d 9563{
75ead1f7 9564 dw.focus_on_function (func);
47dd066d 9565
60d98537 9566 assert(startswith(dw.function_name, "stapprobe_"));
75ead1f7 9567 string tracepoint_instance = dw.function_name.substr(10);
f982c59b
JS
9568
9569 // check for duplicates -- sometimes tracepoint headers may be indirectly
9570 // included in more than one of our tracequery modules.
9571 if (!probed_names.insert(tracepoint_instance).second)
9572 return DWARF_CB_OK;
9573
79189b84
JS
9574 derived_probe *dp = new tracepoint_derived_probe (dw.sess, dw, *func,
9575 tracepoint_instance,
9576 base_probe, base_loc);
9577 results.push_back (dp);
75ead1f7 9578 return DWARF_CB_OK;
47dd066d
WC
9579}
9580
9581
75ead1f7
JS
9582int
9583tracepoint_query::tracepoint_query_cu (Dwarf_Die * cudie, void * arg)
47dd066d 9584{
75ead1f7 9585 tracepoint_query * q = static_cast<tracepoint_query *>(arg);
85007c04 9586 if (pending_interrupts) return DWARF_CB_ABORT;
75ead1f7 9587 return q->handle_query_cu(cudie);
47dd066d
WC
9588}
9589
9590
75ead1f7
JS
9591int
9592tracepoint_query::tracepoint_query_func (Dwarf_Die * func, base_query * query)
47dd066d 9593{
75ead1f7 9594 tracepoint_query * q = static_cast<tracepoint_query *>(query);
85007c04 9595 if (pending_interrupts) return DWARF_CB_ABORT;
75ead1f7 9596 return q->handle_query_func(func);
47dd066d
WC
9597}
9598
9599
0a6f5a3f 9600struct tracepoint_builder: public derived_probe_builder
47dd066d 9601{
0a6f5a3f
JS
9602private:
9603 dwflpp *dw;
9604 bool init_dw(systemtap_session& s);
c9ccb642
FCE
9605 void get_tracequery_modules(systemtap_session& s,
9606 const vector<string>& headers,
9607 vector<string>& modules);
47dd066d 9608
0a6f5a3f 9609public:
47dd066d 9610
0a6f5a3f
JS
9611 tracepoint_builder(): dw(0) {}
9612 ~tracepoint_builder() { delete dw; }
47dd066d 9613
0a6f5a3f
JS
9614 void build_no_more (systemtap_session& s)
9615 {
9616 if (dw && s.verbose > 3)
b530b5b3 9617 clog << _("tracepoint_builder releasing dwflpp") << endl;
0a6f5a3f
JS
9618 delete dw;
9619 dw = NULL;
435f53a7
FCE
9620
9621 delete_session_module_cache (s);
0a6f5a3f 9622 }
47dd066d 9623
0a6f5a3f
JS
9624 void build(systemtap_session& s,
9625 probe *base, probe_point *location,
9626 literal_map_t const& parameters,
9627 vector<derived_probe*>& finished_results);
9628};
47dd066d 9629
47dd066d 9630
c9ccb642 9631
2a0e62a8 9632// Create (or cache) one or more tracequery .o modules, based upon the
c9ccb642
FCE
9633// tracepoint-related header files given. Return the generated or cached
9634// modules[].
9635
9636void
9637tracepoint_builder::get_tracequery_modules(systemtap_session& s,
9638 const vector<string>& headers,
9639 vector<string>& modules)
0a6f5a3f 9640{
c95eddf7 9641 if (s.verbose > 2)
55e50c24 9642 {
ce0f6648 9643 clog << _F("Pass 2: getting a tracepoint query for %zu headers: ", headers.size()) << endl;
55e50c24
JS
9644 for (size_t i = 0; i < headers.size(); ++i)
9645 clog << " " << headers[i] << endl;
9646 }
c95eddf7 9647
2a0e62a8
JS
9648 map<string,string> headers_cache_obj; // header name -> cache/.../tracequery_hash.o file name
9649 // Map the headers to cache .o names. Note that this has side-effects of
c9ccb642
FCE
9650 // creating the $SYSTEMTAP_DIR/.cache/XX/... directory and the hash-log file,
9651 // so we prefer not to repeat this.
9652 vector<string> uncached_headers;
9653 for (size_t i=0; i<headers.size(); i++)
2a0e62a8 9654 headers_cache_obj[headers[i]] = find_tracequery_hash(s, headers[i]);
c9ccb642
FCE
9655
9656 // They may be in the cache already.
9657 if (s.use_cache && !s.poison_cache)
9658 for (size_t i=0; i<headers.size(); i++)
9659 {
9660 // see if the cached module exists
2a0e62a8 9661 const string& tracequery_path = headers_cache_obj[headers[i]];
c9ccb642
FCE
9662 if (!tracequery_path.empty() && file_exists(tracequery_path))
9663 {
9664 if (s.verbose > 2)
9665 clog << _F("Pass 2: using cached %s", tracequery_path.c_str()) << endl;
47dd066d 9666
c252fca2
JS
9667 // an empty file is a cached failure
9668 if (get_file_size(tracequery_path) > 0)
9669 modules.push_back (tracequery_path);
c9ccb642
FCE
9670 }
9671 else
9672 uncached_headers.push_back(headers[i]);
9673 }
9674 else
9675 uncached_headers = headers;
f982c59b 9676
c9ccb642
FCE
9677 // If we have nothing left to search for, quit
9678 if (uncached_headers.empty()) return;
55e50c24 9679
c9ccb642 9680 map<string,string> headers_tracequery_src; // header -> C-source code mapping
55e50c24 9681
c9ccb642
FCE
9682 // We could query several subsets of headers[] to make this go
9683 // faster, but let's KISS and do one at a time.
9684 for (size_t i=0; i<uncached_headers.size(); i++)
55e50c24 9685 {
c9ccb642
FCE
9686 const string& header = uncached_headers[i];
9687
9688 // create a tracequery source file
9689 ostringstream osrc;
9690
9691 // PR9993: Add extra headers to work around undeclared types in individual
9692 // include/trace/foo.h files
9693 vector<string> short_decls = tracepoint_extra_decls(s, header);
9694
9695 // add each requested tracepoint header
75ae2ec9 9696 size_t root_pos = header.rfind("include/");
832f100d 9697 short_decls.push_back(string("#include <") +
75ae2ec9 9698 ((root_pos != string::npos) ? header.substr(root_pos + 8) : header) +
d4393459 9699 string(">"));
f982c59b 9700
c9ccb642
FCE
9701 osrc << "#ifdef CONFIG_TRACEPOINTS" << endl;
9702 osrc << "#include <linux/tracepoint.h>" << endl;
832f100d
JS
9703
9704 // the kernel has changed this naming a few times, previously TPPROTO,
9705 // TP_PROTO, TPARGS, TP_ARGS, etc. so let's just dupe the latest.
9706 osrc << "#ifndef PARAMS" << endl;
9707 osrc << "#define PARAMS(args...) args" << endl;
9708 osrc << "#endif" << endl;
9709
c9ccb642
FCE
9710 // override DECLARE_TRACE to synthesize probe functions for us
9711 osrc << "#undef DECLARE_TRACE" << endl;
9712 osrc << "#define DECLARE_TRACE(name, proto, args) \\" << endl;
9713 osrc << " void stapprobe_##name(proto) {}" << endl;
832f100d 9714
c9ccb642
FCE
9715 // 2.6.35 added the NOARGS variant, but it's the same for us
9716 osrc << "#undef DECLARE_TRACE_NOARGS" << endl;
9717 osrc << "#define DECLARE_TRACE_NOARGS(name) \\" << endl;
9718 osrc << " DECLARE_TRACE(name, void, )" << endl;
832f100d
JS
9719
9720 // 2.6.38 added the CONDITION variant, which can also just redirect
9721 osrc << "#undef DECLARE_TRACE_CONDITION" << endl;
9722 osrc << "#define DECLARE_TRACE_CONDITION(name, proto, args, cond) \\" << endl;
9723 osrc << " DECLARE_TRACE(name, PARAMS(proto), PARAMS(args))" << endl;
9724
c9ccb642
FCE
9725 // older tracepoints used DEFINE_TRACE, so redirect that too
9726 osrc << "#undef DEFINE_TRACE" << endl;
9727 osrc << "#define DEFINE_TRACE(name, proto, args) \\" << endl;
832f100d
JS
9728 osrc << " DECLARE_TRACE(name, PARAMS(proto), PARAMS(args))" << endl;
9729
c9ccb642
FCE
9730 // add the specified decls/#includes
9731 for (unsigned z=0; z<short_decls.size(); z++)
9732 osrc << "#undef TRACE_INCLUDE_FILE\n"
9733 << "#undef TRACE_INCLUDE_PATH\n"
9734 << short_decls[z] << "\n";
9735
9736 // finish up the module source
9737 osrc << "#endif /* CONFIG_TRACEPOINTS */" << endl;
47dd066d 9738
c9ccb642
FCE
9739 // save the source file away
9740 headers_tracequery_src[header] = osrc.str();
55e50c24 9741 }
f982c59b 9742
c9ccb642 9743 // now build them all together
2a0e62a8 9744 map<string,string> tracequery_objs = make_tracequeries(s, headers_tracequery_src);
47dd066d 9745
c9ccb642 9746 // now plop them into the cache
b278033a 9747 if (s.use_cache)
c9ccb642
FCE
9748 for (size_t i=0; i<uncached_headers.size(); i++)
9749 {
9750 const string& header = uncached_headers[i];
2a0e62a8
JS
9751 const string& tracequery_obj = tracequery_objs[header];
9752 const string& tracequery_path = headers_cache_obj[header];
9753 if (tracequery_obj !="" && file_exists(tracequery_obj))
c9ccb642 9754 {
2a0e62a8 9755 copy_file(tracequery_obj, tracequery_path, s.verbose > 2);
c9ccb642
FCE
9756 modules.push_back (tracequery_path);
9757 }
c252fca2
JS
9758 else
9759 // cache an empty file for failures
9760 copy_file("/dev/null", tracequery_path, s.verbose > 2);
c9ccb642 9761 }
f982c59b
JS
9762}
9763
9764
d4393459 9765
f982c59b
JS
9766bool
9767tracepoint_builder::init_dw(systemtap_session& s)
9768{
9769 if (dw != NULL)
9770 return true;
9771
9772 vector<string> tracequery_modules;
55e50c24 9773 vector<string> system_headers;
f982c59b
JS
9774
9775 glob_t trace_glob;
d4393459
FCE
9776
9777 // find kernel_source_tree
9778 if (s.kernel_source_tree == "")
f982c59b 9779 {
d4393459
FCE
9780 unsigned found;
9781 DwflPtr dwfl_ptr = setup_dwfl_kernel ("kernel", &found, s);
9782 Dwfl *dwfl = dwfl_ptr.get()->dwfl;
9783 if (found)
9784 {
9785 Dwarf_Die *cudie = 0;
9786 Dwarf_Addr bias;
9787 while ((cudie = dwfl_nextcu (dwfl, cudie, &bias)) != NULL)
9788 {
9789 if (pending_interrupts) break;
9790 Dwarf_Attribute attr;
9791 const char* name = dwarf_formstring (dwarf_attr (cudie, DW_AT_comp_dir, &attr));
9792 if (name)
9793 {
61f1a63b 9794 if (s.verbose > 2)
b530b5b3 9795 clog << _F("Located kernel source tree (DW_AT_comp_dir) at '%s'", name) << endl;
61f1a63b 9796
d4393459
FCE
9797 s.kernel_source_tree = name;
9798 break; // skip others; modern Kbuild uses same comp_dir for them all
9799 }
9800 }
9801 }
9802 }
9803
9804 // prefixes
9805 vector<string> glob_prefixes;
9806 glob_prefixes.push_back (s.kernel_build_tree);
9807 if (s.kernel_source_tree != "")
9808 glob_prefixes.push_back (s.kernel_source_tree);
9809
9810 // suffixes
9811 vector<string> glob_suffixes;
9812 glob_suffixes.push_back("include/trace/events/*.h");
9813 glob_suffixes.push_back("include/trace/*.h");
9814 glob_suffixes.push_back("arch/x86/kvm/*trace.h");
f89ff3e2 9815 glob_suffixes.push_back("fs/xfs/linux-*/xfs_tr*.h");
d4393459
FCE
9816
9817 // compute cartesian product
9818 vector<string> globs;
9819 for (unsigned i=0; i<glob_prefixes.size(); i++)
9820 for (unsigned j=0; j<glob_suffixes.size(); j++)
9821 globs.push_back (glob_prefixes[i]+string("/")+glob_suffixes[j]);
9822
8aa43b8d 9823 set<string> duped_headers;
d4393459
FCE
9824 for (unsigned z = 0; z < globs.size(); z++)
9825 {
9826 string glob_str = globs[z];
9827 if (s.verbose > 3)
b530b5b3 9828 clog << _("Checking tracepoint glob ") << glob_str << endl;
d4393459 9829
f982c59b
JS
9830 glob(glob_str.c_str(), 0, NULL, &trace_glob);
9831 for (unsigned i = 0; i < trace_glob.gl_pathc; ++i)
9832 {
9833 string header(trace_glob.gl_pathv[i]);
9834
9835 // filter out a few known "internal-only" headers
60d98537
JS
9836 if (endswith(header, "/define_trace.h") ||
9837 endswith(header, "/ftrace.h") ||
9838 endswith(header, "/trace_events.h") ||
9839 endswith(header, "_event_types.h"))
f982c59b
JS
9840 continue;
9841
8aa43b8d
JS
9842 // skip identical headers from the build and source trees.
9843 size_t root_pos = header.rfind("include/");
9844 if (root_pos != string::npos &&
9845 !duped_headers.insert(header.substr(root_pos + 8)).second)
9846 continue;
9847
55e50c24 9848 system_headers.push_back(header);
f982c59b
JS
9849 }
9850 globfree(&trace_glob);
9851 }
9852
c9ccb642
FCE
9853 // Build tracequery modules
9854 get_tracequery_modules(s, system_headers, tracequery_modules);
55e50c24 9855
f982c59b
JS
9856 // TODO: consider other sources of tracepoint headers too, like from
9857 // a command-line parameter or some environment or .systemtaprc
47dd066d 9858
59c11f91 9859 dw = new dwflpp(s, tracequery_modules, true);
0a6f5a3f
JS
9860 return true;
9861}
47dd066d 9862
0a6f5a3f
JS
9863void
9864tracepoint_builder::build(systemtap_session& s,
9865 probe *base, probe_point *location,
9866 literal_map_t const& parameters,
9867 vector<derived_probe*>& finished_results)
9868{
9869 if (!init_dw(s))
9870 return;
47dd066d 9871
75ead1f7
JS
9872 string tracepoint;
9873 assert(get_param (parameters, TOK_TRACE, tracepoint));
47dd066d 9874
75ead1f7 9875 tracepoint_query q(*dw, tracepoint, base, location, finished_results);
51178501 9876 dw->iterate_over_modules(&query_module, &q);
47dd066d 9877}
47dd066d 9878
e6fe60e7 9879
b55bc428 9880// ------------------------------------------------------------------------
bd2b1e68 9881// Standard tapset registry.
b55bc428
FCE
9882// ------------------------------------------------------------------------
9883
7a053d3b 9884void
f8220a7b 9885register_standard_tapsets(systemtap_session & s)
b55bc428 9886{
47e0478e 9887 register_tapset_been(s);
93646f4d 9888 register_tapset_itrace(s);
dd0e4fa7 9889 register_tapset_mark(s);
7a212aa8 9890 register_tapset_procfs(s);
912e8c59 9891 register_tapset_timers(s);
b84779a5 9892 register_tapset_utrace(s);
b98a8d73 9893
7a24d422 9894 // dwarf-based kprobe/uprobe parts
c4ce66a1 9895 dwarf_derived_probe::register_patterns(s);
30a279be 9896
888af770
FCE
9897 // XXX: user-space starter set
9898 s.pattern_root->bind_num(TOK_PROCESS)
9899 ->bind_num(TOK_STATEMENT)->bind(TOK_ABSOLUTE)
f66bb29a 9900 ->bind_privilege(pr_all)
888af770
FCE
9901 ->bind(new uprobe_builder ());
9902 s.pattern_root->bind_num(TOK_PROCESS)
9903 ->bind_num(TOK_STATEMENT)->bind(TOK_ABSOLUTE)->bind(TOK_RETURN)
f66bb29a 9904 ->bind_privilege(pr_all)
888af770
FCE
9905 ->bind(new uprobe_builder ());
9906
0a6f5a3f
JS
9907 // kernel tracepoint probes
9908 s.pattern_root->bind(TOK_KERNEL)->bind_str(TOK_TRACE)
9909 ->bind(new tracepoint_builder());
9910
e6fe60e7
AM
9911 // Kprobe based probe
9912 s.pattern_root->bind(TOK_KPROBE)->bind_str(TOK_FUNCTION)
9913 ->bind(new kprobe_builder());
9914 s.pattern_root->bind(TOK_KPROBE)->bind_str(TOK_MODULE)
9915 ->bind_str(TOK_FUNCTION)->bind(new kprobe_builder());
9916 s.pattern_root->bind(TOK_KPROBE)->bind_str(TOK_FUNCTION)->bind(TOK_RETURN)
9917 ->bind(new kprobe_builder());
b6371390
JS
9918 s.pattern_root->bind(TOK_KPROBE)->bind_str(TOK_FUNCTION)->bind(TOK_RETURN)
9919 ->bind_num(TOK_MAXACTIVE)->bind(new kprobe_builder());
e6fe60e7
AM
9920 s.pattern_root->bind(TOK_KPROBE)->bind_str(TOK_MODULE)
9921 ->bind_str(TOK_FUNCTION)->bind(TOK_RETURN)->bind(new kprobe_builder());
b6371390
JS
9922 s.pattern_root->bind(TOK_KPROBE)->bind_str(TOK_MODULE)
9923 ->bind_str(TOK_FUNCTION)->bind(TOK_RETURN)
9924 ->bind_num(TOK_MAXACTIVE)->bind(new kprobe_builder());
e6fe60e7
AM
9925 s.pattern_root->bind(TOK_KPROBE)->bind_num(TOK_STATEMENT)
9926 ->bind(TOK_ABSOLUTE)->bind(new kprobe_builder());
dd225250
PS
9927
9928 //Hwbkpt based probe
b47f3a55
FCE
9929 // NB: we formerly registered the probe point types only if the kernel configuration
9930 // allowed it. However, we get better error messages if we allow probes to resolve.
9931 s.pattern_root->bind(TOK_KERNEL)->bind_num(TOK_HWBKPT)
9932 ->bind(TOK_HWBKPT_WRITE)->bind(new hwbkpt_builder());
9933 s.pattern_root->bind(TOK_KERNEL)->bind_str(TOK_HWBKPT)
9934 ->bind(TOK_HWBKPT_WRITE)->bind(new hwbkpt_builder());
9935 s.pattern_root->bind(TOK_KERNEL)->bind_num(TOK_HWBKPT)
9936 ->bind(TOK_HWBKPT_RW)->bind(new hwbkpt_builder());
9937 s.pattern_root->bind(TOK_KERNEL)->bind_str(TOK_HWBKPT)
9938 ->bind(TOK_HWBKPT_RW)->bind(new hwbkpt_builder());
9939 s.pattern_root->bind(TOK_KERNEL)->bind_num(TOK_HWBKPT)
9940 ->bind_num(TOK_LENGTH)->bind(TOK_HWBKPT_WRITE)->bind(new hwbkpt_builder());
9941 s.pattern_root->bind(TOK_KERNEL)->bind_num(TOK_HWBKPT)
9942 ->bind_num(TOK_LENGTH)->bind(TOK_HWBKPT_RW)->bind(new hwbkpt_builder());
9943 // length supported with address only, not symbol names
83ea76b1
WC
9944
9945 //perf event based probe
4763f713 9946 register_tapset_perf(s);
b55bc428 9947}
dc38c0ae
DS
9948
9949
b20febf3
FCE
9950vector<derived_probe_group*>
9951all_session_groups(systemtap_session& s)
dc38c0ae 9952{
b20febf3 9953 vector<derived_probe_group*> g;
912e8c59
JS
9954
9955#define DOONE(x) \
9956 if (s. x##_derived_probes) \
9957 g.push_back ((derived_probe_group*)(s. x##_derived_probes))
ab655cf8
DS
9958
9959 // Note that order *is* important here. We want to make sure we
9960 // register (actually run) begin probes before any other probe type
9961 // is run. Similarly, when unregistering probes, we want to
9962 // unregister (actually run) end probes after every other probe type
9963 // has be unregistered. To do the latter,
9964 // c_unparser::emit_module_exit() will run this list backwards.
b20febf3
FCE
9965 DOONE(be);
9966 DOONE(dwarf);
888af770 9967 DOONE(uprobe);
b20febf3
FCE
9968 DOONE(timer);
9969 DOONE(profile);
9970 DOONE(mark);
0a6f5a3f 9971 DOONE(tracepoint);
e6fe60e7 9972 DOONE(kprobe);
dd225250 9973 DOONE(hwbkpt);
83ea76b1 9974 DOONE(perf);
b20febf3 9975 DOONE(hrtimer);
ce82316f 9976 DOONE(procfs);
935447c8
DS
9977
9978 // Another "order is important" item. We want to make sure we
9979 // "register" the dummy task_finder probe group after all probe
9980 // groups that use the task_finder.
9981 DOONE(utrace);
a96d1db0 9982 DOONE(itrace);
935447c8 9983 DOONE(task_finder);
b20febf3
FCE
9984#undef DOONE
9985 return g;
46b84a80 9986}
73267b89
JS
9987
9988/* vim: set sw=2 ts=8 cino=>4,n-2,{2,^-2,t0,(0,u0,w1,M1 : */
This page took 2.287044 seconds and 5 git commands to generate.