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