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