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