]> sourceware.org Git - systemtap.git/blob - tapsets.cxx
Define __NR_epoll_wait if not defined (PR17462)
[systemtap.git] / tapsets.cxx
1 // tapset resolution
2 // Copyright (C) 2005-2014 Red Hat Inc.
3 // Copyright (C) 2005-2007 Intel Corporation.
4 // Copyright (C) 2008 James.Bottomley@HansenPartnership.com
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"
14 #include "tapsets.h"
15 #include "task_finder.h"
16 #include "tapset-dynprobe.h"
17 #include "translate.h"
18 #include "session.h"
19 #include "util.h"
20 #include "buildrun.h"
21 #include "dwarf_wrappers.h"
22 #include "auto_free.h"
23 #include "hash.h"
24 #include "dwflpp.h"
25 #include "setupdwfl.h"
26 #include <gelf.h>
27
28 #include "sdt_types.h"
29
30 #include <cstdlib>
31 #include <algorithm>
32 #include <deque>
33 #include <iostream>
34 #include <fstream>
35 #include <map>
36 #include <set>
37 #include <sstream>
38 #include <stdexcept>
39 #include <vector>
40 #include <stack>
41 #include <cstdarg>
42 #include <cassert>
43 #include <iomanip>
44 #include <cerrno>
45
46 extern "C" {
47 #include <fcntl.h>
48 #include <elfutils/libdwfl.h>
49 #include <elfutils/libdw.h>
50 #include <dwarf.h>
51 #include <elf.h>
52 #include <obstack.h>
53 #include <glob.h>
54 #include <fnmatch.h>
55 #include <stdio.h>
56 #include <sys/types.h>
57 #include <sys/stat.h>
58 #include <math.h>
59 #include <regex.h>
60 #include <unistd.h>
61
62 #define __STDC_FORMAT_MACROS
63 #include <inttypes.h>
64 }
65
66 // Old elf.h doesn't know about this machine type.
67 #ifndef EM_AARCH64
68 #define EM_AARCH64 183
69 #endif
70
71
72 using namespace std;
73 using namespace __gnu_cxx;
74
75
76
77 // ------------------------------------------------------------------------
78
79 string
80 common_probe_init (derived_probe* p)
81 {
82 assert(p->session_index != (unsigned)-1);
83 return "(&stap_probes[" + lex_cast(p->session_index) + "])";
84 }
85
86
87 void
88 common_probe_entryfn_prologue (systemtap_session& s,
89 string statestr, string probe,
90 string probe_type, bool overload_processing)
91 {
92 if (s.runtime_usermode_p())
93 {
94 // If session_state() is NULL, then we haven't even initialized shm yet,
95 // and there's *nothing* for the probe to do. (even alibi is in shm)
96 // So failure skips this whole block through the end of the epilogue.
97 s.op->newline() << "if (likely(session_state())) {";
98 s.op->indent(1);
99 }
100
101 s.op->newline() << "#ifdef STP_ALIBI";
102 s.op->newline() << "atomic_inc(probe_alibi(" << probe << "->index));";
103 s.op->newline() << "#else";
104
105 if (s.runtime_usermode_p())
106 s.op->newline() << "int _stp_saved_errno = errno;";
107
108 s.op->newline() << "struct context* __restrict__ c = NULL;";
109 s.op->newline() << "#if !INTERRUPTIBLE";
110 s.op->newline() << "unsigned long flags;";
111 s.op->newline() << "#endif";
112
113 s.op->newline() << "#ifdef STP_TIMING";
114 s.op->newline() << "Stat stat = probe_timing(" << probe << "->index);";
115 s.op->newline() << "#endif";
116 if (overload_processing && !s.runtime_usermode_p())
117 s.op->newline() << "#if defined(STP_TIMING) || defined(STP_OVERLOAD)";
118 else
119 s.op->newline() << "#ifdef STP_TIMING";
120
121 if (! s.runtime_usermode_p())
122 s.op->newline() << "cycles_t cycles_atstart = get_cycles ();";
123 else
124 {
125 s.op->newline() << "struct timespec timespec_atstart;";
126 s.op->newline() << "(void)clock_gettime(CLOCK_MONOTONIC, &timespec_atstart);";
127 }
128 s.op->newline() << "#endif";
129
130 s.op->newline() << "#if !INTERRUPTIBLE";
131 s.op->newline() << "local_irq_save (flags);";
132 s.op->newline() << "#endif";
133
134 if (! s.runtime_usermode_p())
135 {
136 // Check for enough free enough stack space
137 s.op->newline() << "if (unlikely ((((unsigned long) (& c)) & (THREAD_SIZE-1))"; // free space
138 s.op->newline(1) << "< (MINSTACKSPACE + sizeof (struct thread_info)))) {"; // needed space
139 // XXX: may need porting to platforms where task_struct is not
140 // at bottom of kernel stack NB: see also
141 // CONFIG_DEBUG_STACKOVERFLOW
142 s.op->newline() << "atomic_inc (skipped_count());";
143 s.op->newline() << "#ifdef STP_TIMING";
144 s.op->newline() << "atomic_inc (skipped_count_lowstack());";
145 s.op->newline() << "#endif";
146 s.op->newline() << "goto probe_epilogue;";
147 s.op->newline(-1) << "}";
148 }
149
150 s.op->newline() << "if (atomic_read (session_state()) != " << statestr << ")";
151 s.op->newline(1) << "goto probe_epilogue;";
152 s.op->indent(-1);
153
154 s.op->newline() << "c = _stp_runtime_entryfn_get_context();";
155 s.op->newline() << "if (!c) {";
156 s.op->newline(1) << "#if !INTERRUPTIBLE";
157 s.op->newline() << "atomic_inc (skipped_count());";
158 s.op->newline() << "#endif";
159 s.op->newline() << "#ifdef STP_TIMING";
160 s.op->newline() << "atomic_inc (skipped_count_reentrant());";
161 s.op->newline() << "#endif";
162 s.op->newline() << "goto probe_epilogue;";
163 s.op->newline(-1) << "}";
164
165 s.op->newline();
166 s.op->newline() << "c->last_stmt = 0;";
167 s.op->newline() << "c->last_error = 0;";
168 s.op->newline() << "c->nesting = -1;"; // NB: PR10516 packs locals[] tighter
169 s.op->newline() << "c->uregs = 0;";
170 s.op->newline() << "c->kregs = 0;";
171 s.op->newline() << "#if defined __ia64__";
172 s.op->newline() << "c->unwaddr = 0;";
173 s.op->newline() << "#endif";
174 if (s.runtime_usermode_p())
175 s.op->newline() << "c->probe_index = " << probe << "->index;";
176 s.op->newline() << "c->probe_point = " << probe << "->pp;";
177 s.op->newline() << "#ifdef STP_NEED_PROBE_NAME";
178 s.op->newline() << "c->probe_name = " << probe << "->pn;";
179 s.op->newline() << "#endif";
180 s.op->newline() << "c->probe_type = " << probe_type << ";";
181 // reset Individual Probe State union
182 s.op->newline() << "memset(&c->ips, 0, sizeof(c->ips));";
183 s.op->newline() << "c->user_mode_p = 0; c->full_uregs_p = 0;";
184 s.op->newline() << "#ifdef STAP_NEED_REGPARM"; // i386 or x86_64 register.stp
185 s.op->newline() << "c->regparm = 0;";
186 s.op->newline() << "#endif";
187
188 if(!s.suppress_time_limits){
189 s.op->newline() << "#if INTERRUPTIBLE";
190 s.op->newline() << "c->actionremaining = MAXACTION_INTERRUPTIBLE;";
191 s.op->newline() << "#else";
192 s.op->newline() << "c->actionremaining = MAXACTION;";
193 s.op->newline() << "#endif";
194 }
195 // NB: The following would actually be incorrect.
196 // That's because cycles_sum/cycles_base values are supposed to survive
197 // between consecutive probes. Periodically (STP_OVERLOAD_INTERVAL
198 // cycles), the values will be reset.
199 /*
200 s.op->newline() << "#ifdef STP_OVERLOAD";
201 s.op->newline() << "c->cycles_sum = 0;";
202 s.op->newline() << "c->cycles_base = 0;";
203 s.op->newline() << "#endif";
204 */
205
206 s.op->newline() << "#if defined(STP_NEED_UNWIND_DATA)";
207 s.op->newline() << "c->uwcache_user.state = uwcache_uninitialized;";
208 s.op->newline() << "c->uwcache_kernel.state = uwcache_uninitialized;";
209 s.op->newline() << "#endif";
210 }
211
212
213 void
214 common_probe_entryfn_epilogue (systemtap_session& s,
215 bool overload_processing,
216 bool schedule_work_safe)
217 {
218 if (!s.runtime_usermode_p()
219 && schedule_work_safe)
220 {
221 // If a refresh is required, we can safely schedule_work() here
222 s.op->newline( 0) << "if (atomic_cmpxchg(&need_module_refresh, 1, 0) == 1)";
223 s.op->newline(+1) << "schedule_work(&module_refresher_work);";
224 s.op->indent(-1);
225 }
226
227 if (overload_processing && !s.runtime_usermode_p())
228 s.op->newline() << "#if defined(STP_TIMING) || defined(STP_OVERLOAD)";
229 else
230 s.op->newline() << "#ifdef STP_TIMING";
231 s.op->newline() << "{";
232 s.op->indent(1);
233 if (! s.runtime_usermode_p())
234 {
235 s.op->newline() << "cycles_t cycles_atend = get_cycles ();";
236 // NB: we truncate cycles counts to 32 bits. Perhaps it should be
237 // fewer, if the hardware counter rolls over really quickly. We
238 // handle 32-bit wraparound here.
239 s.op->newline() << "int32_t cycles_elapsed = ((int32_t)cycles_atend > (int32_t)cycles_atstart)";
240 s.op->newline(1) << "? ((int32_t)cycles_atend - (int32_t)cycles_atstart)";
241 s.op->newline() << ": (~(int32_t)0) - (int32_t)cycles_atstart + (int32_t)cycles_atend + 1;";
242 s.op->indent(-1);
243 }
244 else
245 {
246 s.op->newline() << "struct timespec timespec_atend, timespec_elapsed;";
247 s.op->newline() << "long cycles_elapsed;";
248 s.op->newline() << "(void)clock_gettime(CLOCK_MONOTONIC, &timespec_atend);";
249 s.op->newline() << "_stp_timespec_sub(&timespec_atend, &timespec_atstart, &timespec_elapsed);";
250 // 'cycles_elapsed' is really elapsed nanoseconds
251 s.op->newline() << "cycles_elapsed = (timespec_elapsed.tv_sec * NSEC_PER_SEC) + timespec_elapsed.tv_nsec;";
252 }
253
254 s.op->newline() << "#ifdef STP_TIMING";
255 s.op->newline() << "if (likely (stat)) _stp_stat_add(stat, cycles_elapsed);";
256 s.op->newline() << "#endif";
257
258 if (overload_processing && !s.runtime_usermode_p())
259 {
260 s.op->newline() << "#ifdef STP_OVERLOAD";
261 s.op->newline() << "{";
262 // If the cycle count has wrapped (cycles_atend > cycles_base),
263 // let's go ahead and pretend the interval has been reached.
264 // This should reset cycles_base and cycles_sum.
265 s.op->newline(1) << "cycles_t interval = (cycles_atend > c->cycles_base)";
266 s.op->newline(1) << "? (cycles_atend - c->cycles_base)";
267 s.op->newline() << ": (STP_OVERLOAD_INTERVAL + 1);";
268 s.op->newline(-1) << "c->cycles_sum += cycles_elapsed;";
269
270 // If we've spent more than STP_OVERLOAD_THRESHOLD cycles in a
271 // probe during the last STP_OVERLOAD_INTERVAL cycles, the probe
272 // has overloaded the system and we need to quit.
273 // NB: this is not suppressible via --suppress-runtime-errors,
274 // because this is a system safety metric that we cannot trust
275 // unprivileged users to override.
276 s.op->newline() << "if (interval > STP_OVERLOAD_INTERVAL) {";
277 s.op->newline(1) << "if (c->cycles_sum > STP_OVERLOAD_THRESHOLD) {";
278 s.op->newline(1) << "_stp_error (\"probe overhead exceeded threshold\");";
279 s.op->newline() << "atomic_set (session_state(), STAP_SESSION_ERROR);";
280 s.op->newline() << "atomic_inc (error_count());";
281 s.op->newline(-1) << "}";
282
283 s.op->newline() << "c->cycles_base = cycles_atend;";
284 s.op->newline() << "c->cycles_sum = 0;";
285 s.op->newline(-1) << "}";
286 s.op->newline(-1) << "}";
287 s.op->newline() << "#endif";
288 }
289
290 s.op->newline(-1) << "}";
291 s.op->newline() << "#endif";
292
293 s.op->newline() << "c->probe_point = 0;"; // vacated
294 s.op->newline() << "#ifdef STP_NEED_PROBE_NAME";
295 s.op->newline() << "c->probe_name = 0;";
296 s.op->newline() << "#endif";
297 s.op->newline() << "c->probe_type = 0;";
298
299
300 s.op->newline() << "if (unlikely (c->last_error && c->last_error[0])) {";
301 s.op->indent(1);
302 if (s.suppress_handler_errors) // PR 13306
303 {
304 s.op->newline() << "atomic_inc (error_count());";
305 }
306 else
307 {
308 s.op->newline() << "if (c->last_stmt != NULL)";
309 s.op->newline(1) << "_stp_softerror (\"%s near %s\", c->last_error, c->last_stmt);";
310 s.op->newline(-1) << "else";
311 s.op->newline(1) << "_stp_softerror (\"%s\", c->last_error);";
312 s.op->indent(-1);
313 s.op->newline() << "atomic_inc (error_count());";
314 s.op->newline() << "if (atomic_read (error_count()) > MAXERRORS) {";
315 s.op->newline(1) << "atomic_set (session_state(), STAP_SESSION_ERROR);";
316 s.op->newline() << "_stp_exit ();";
317 s.op->newline(-1) << "}";
318 }
319
320 s.op->newline(-1) << "}";
321
322
323 s.op->newline(-1) << "probe_epilogue:"; // context is free
324 s.op->indent(1);
325
326 if (! s.suppress_handler_errors) // PR 13306
327 {
328 // Check for excessive skip counts.
329 s.op->newline() << "if (unlikely (atomic_read (skipped_count()) > MAXSKIPPED)) {";
330 s.op->newline(1) << "if (unlikely (pseudo_atomic_cmpxchg(session_state(), STAP_SESSION_RUNNING, STAP_SESSION_ERROR) == STAP_SESSION_RUNNING))";
331 s.op->newline() << "_stp_error (\"Skipped too many probes, check MAXSKIPPED or try again with stap -t for more details.\");";
332 s.op->newline(-1) << "}";
333 }
334
335 // We mustn't release the context until after all _stp_error(), so dyninst
336 // mode can still access the log buffers stored therein.
337 s.op->newline() << "_stp_runtime_entryfn_put_context(c);";
338
339 s.op->newline() << "#if !INTERRUPTIBLE";
340 s.op->newline() << "local_irq_restore (flags);";
341 s.op->newline() << "#endif";
342
343 if (s.runtime_usermode_p())
344 {
345 s.op->newline() << "errno = _stp_saved_errno;";
346 }
347
348 s.op->newline() << "#endif // STP_ALIBI";
349
350 if (s.runtime_usermode_p())
351 s.op->newline(-1) << "}";
352 }
353
354
355 // ------------------------------------------------------------------------
356
357 // ------------------------------------------------------------------------
358 // Dwarf derived probes. "We apologize for the inconvience."
359 // ------------------------------------------------------------------------
360
361 static const string TOK_KERNEL("kernel");
362 static const string TOK_MODULE("module");
363 static const string TOK_FUNCTION("function");
364 static const string TOK_INLINE("inline");
365 static const string TOK_CALL("call");
366 static const string TOK_EXPORTED("exported");
367 static const string TOK_RETURN("return");
368 static const string TOK_MAXACTIVE("maxactive");
369 static const string TOK_STATEMENT("statement");
370 static const string TOK_ABSOLUTE("absolute");
371 static const string TOK_PROCESS("process");
372 static const string TOK_PROVIDER("provider");
373 static const string TOK_MARK("mark");
374 static const string TOK_TRACE("trace");
375 static const string TOK_LABEL("label");
376 static const string TOK_LIBRARY("library");
377 static const string TOK_PLT("plt");
378 static const string TOK_METHOD("method");
379 static const string TOK_CLASS("class");;
380 static const string TOK_CALLEE("callee");;
381 static const string TOK_CALLEES("callees");;
382 static const string TOK_NEAREST("nearest");;
383
384 // Can we handle this query with just symbol-table info?
385 enum dbinfo_reqt
386 {
387 dbr_unknown,
388 dbr_none, // kernel.statement(NUM).absolute
389 dbr_need_symtab, // can get by with symbol table if there's no dwarf
390 dbr_need_dwarf
391 };
392
393
394 struct dwarf_query; // forward decl
395
396 static int query_cu (Dwarf_Die * cudie, dwarf_query *q);
397 static void query_addr(Dwarf_Addr addr, dwarf_query *q);
398 static void query_plt_statement(dwarf_query *q);
399
400 struct
401 symbol_table
402 {
403 module_info *mod_info; // associated module
404 map<string, func_info*> map_by_name;
405 multimap<Dwarf_Addr, func_info*> map_by_addr;
406 map<string, Dwarf_Addr> globals;
407 map<string, Dwarf_Addr> locals;
408 typedef multimap<Dwarf_Addr, func_info*>::iterator iterator_t;
409 typedef pair<iterator_t, iterator_t> range_t;
410 #ifdef __powerpc__
411 GElf_Word opd_section;
412 #endif
413 void add_symbol(const char *name, bool weak, bool descriptor,
414 Dwarf_Addr addr, Dwarf_Addr *high_addr);
415 enum info_status get_from_elf();
416 void prepare_section_rejection(Dwfl_Module *mod);
417 bool reject_section(GElf_Word section);
418 void purge_syscall_stubs();
419 func_info *lookup_symbol(const string& name);
420 Dwarf_Addr lookup_symbol_address(const string& name);
421 func_info *get_func_containing_address(Dwarf_Addr addr);
422 func_info *get_first_func();
423
424 symbol_table(module_info *mi) : mod_info(mi) {}
425 ~symbol_table();
426 };
427
428 static bool null_die(Dwarf_Die *die)
429 {
430 static Dwarf_Die null;
431 return (!die || !memcmp(die, &null, sizeof(null)));
432 }
433
434 struct exp_type_dwarf : public exp_type_details
435 {
436 // NB: We don't own this dwflpp, so don't use it after build_no_more!
437 // A shared_ptr might help, but expressions are currently so leaky
438 // that we'd probably never clear all references... :/
439 dwflpp* dw;
440 Dwarf_Die die;
441 bool userspace_p;
442 bool is_pointer;
443 exp_type_dwarf(dwflpp* dw, Dwarf_Die* die, bool userspace_p, bool addressof);
444 uintptr_t id () const { return reinterpret_cast<uintptr_t>(die.addr); }
445 bool expandable() const { return true; }
446 functioncall *expand(autocast_op* e, bool lvalue);
447 };
448
449
450 enum
451 function_spec_type
452 {
453 function_alone,
454 function_and_file,
455 function_file_and_line
456 };
457
458
459 struct dwarf_builder;
460 struct dwarf_var_expanding_visitor;
461
462
463 // XXX: This class is a candidate for subclassing to separate
464 // the relocation vs non-relocation variants. Likewise for
465 // kprobe vs kretprobe variants.
466
467 struct dwarf_derived_probe: public derived_probe
468 {
469 dwarf_derived_probe (const string& function,
470 const string& filename,
471 int line,
472 const string& module,
473 const string& section,
474 Dwarf_Addr dwfl_addr,
475 Dwarf_Addr addr,
476 dwarf_query & q,
477 Dwarf_Die* scope_die);
478
479 string module;
480 string section;
481 Dwarf_Addr addr;
482 string path;
483 bool has_process;
484 bool has_return;
485 bool has_maxactive;
486 bool has_library;
487 long maxactive_val;
488 // dwarf_derived_probe_group::emit_module_decls uses this to emit sdt kprobe definition
489 string user_path;
490 string user_lib;
491 bool access_vars;
492
493 unsigned saved_longs, saved_strings;
494 dwarf_derived_probe* entry_handler;
495
496 void printsig (std::ostream &o) const;
497 virtual void join_group (systemtap_session& s);
498 void emit_probe_local_init(systemtap_session& s, translator_output * o);
499 void getargs(std::list<std::string> &arg_set) const;
500
501 void emit_privilege_assertion (translator_output*);
502 void print_dupe_stamp(ostream& o);
503
504 // Pattern registration helpers.
505 static void register_statement_variants(match_node * root,
506 dwarf_builder * dw,
507 privilege_t privilege);
508 static void register_function_variants(match_node * root,
509 dwarf_builder * dw,
510 privilege_t privilege);
511 static void register_function_and_statement_variants(systemtap_session& s,
512 match_node * root,
513 dwarf_builder * dw,
514 privilege_t privilege);
515 static void register_sdt_variants(systemtap_session& s,
516 match_node * root,
517 dwarf_builder * dw);
518 static void register_plt_variants(systemtap_session& s,
519 match_node * root,
520 dwarf_builder * dw);
521 static void register_patterns(systemtap_session& s);
522
523 protected:
524 dwarf_derived_probe(probe *base,
525 probe_point *location,
526 Dwarf_Addr addr,
527 bool has_return):
528 derived_probe(base, location), addr(addr), has_process(0),
529 has_return(has_return), has_maxactive(0), has_library(0),
530 maxactive_val(0), access_vars(false), saved_longs(0),
531 saved_strings(0), entry_handler(0)
532 {}
533
534 private:
535 list<string> args;
536 void saveargs(dwarf_query& q, Dwarf_Die* scope_die, Dwarf_Addr dwfl_addr);
537 };
538
539
540 struct uprobe_derived_probe: public dwarf_derived_probe
541 {
542 int pid; // 0 => unrestricted
543
544 uprobe_derived_probe (const string& function,
545 const string& filename,
546 int line,
547 const string& module,
548 const string& section,
549 Dwarf_Addr dwfl_addr,
550 Dwarf_Addr addr,
551 dwarf_query & q,
552 Dwarf_Die* scope_die);
553
554 // alternate constructor for process(PID).statement(ADDR).absolute
555 uprobe_derived_probe (probe *base,
556 probe_point *location,
557 int pid,
558 Dwarf_Addr addr,
559 bool has_return):
560 dwarf_derived_probe(base, location, addr, has_return), pid(pid)
561 {}
562
563 void join_group (systemtap_session& s);
564
565 void emit_privilege_assertion (translator_output*);
566 void print_dupe_stamp(ostream& o) { print_dupe_stamp_unprivileged_process_owner (o); }
567 void getargs(std::list<std::string> &arg_set) const;
568 void saveargs(int nargs);
569 private:
570 list<string> args;
571 };
572
573 struct dwarf_derived_probe_group: public derived_probe_group
574 {
575 private:
576 multimap<string,dwarf_derived_probe*> probes_by_module;
577 typedef multimap<string,dwarf_derived_probe*>::iterator p_b_m_iterator;
578
579 public:
580 dwarf_derived_probe_group() {}
581 void enroll (dwarf_derived_probe* probe);
582 void emit_module_decls (systemtap_session& s);
583 void emit_module_init (systemtap_session& s);
584 void emit_module_refresh (systemtap_session& s);
585 void emit_module_exit (systemtap_session& s);
586 bool otf_supported (systemtap_session& s) { return true; }
587
588 // workqueue handling not safe in kprobes context
589 bool otf_safe_context (systemtap_session& s) { return false; }
590 };
591
592 // Helper struct to thread through the dwfl callbacks.
593 struct base_query
594 {
595 base_query(dwflpp & dw, literal_map_t const & params);
596 base_query(dwflpp & dw, const string & module_val);
597 virtual ~base_query() {}
598
599 systemtap_session & sess;
600 dwflpp & dw;
601
602 // Used to keep track of which modules were visited during
603 // iterate_over_modules()
604 set<string> visited_modules;
605
606 // Parameter extractors.
607 static bool has_null_param(literal_map_t const & params,
608 string const & k);
609 static bool get_string_param(literal_map_t const & params,
610 string const & k, string & v);
611 static bool get_number_param(literal_map_t const & params,
612 string const & k, long & v);
613 static bool get_number_param(literal_map_t const & params,
614 string const & k, long long & v);
615 static bool get_number_param(literal_map_t const & params,
616 string const & k, Dwarf_Addr & v);
617 static void query_library_callback (base_query *me, const char *data);
618 static void query_plt_callback (base_query *me, const char *link, size_t addr);
619 virtual void query_library (const char *data) = 0;
620 virtual void query_plt (const char *link, size_t addr) = 0;
621
622
623 // Extracted parameters.
624 bool has_kernel;
625 bool has_module;
626 bool has_process;
627 bool has_library;
628 bool has_plt;
629 bool has_statement;
630 string module_val; // has_kernel => module_val = "kernel"
631 string path; // executable path if module is a .so
632 string plt_val; // has_plt => plt wildcard
633 int64_t pid_val;
634
635 virtual void handle_query_module() = 0;
636 };
637
638 base_query::base_query(dwflpp & dw, literal_map_t const & params):
639 sess(dw.sess), dw(dw), has_library(false), has_plt(false), has_statement(false),
640 pid_val(0)
641 {
642 has_kernel = has_null_param (params, TOK_KERNEL);
643 if (has_kernel)
644 module_val = "kernel";
645
646 has_module = get_string_param (params, TOK_MODULE, module_val);
647 if (has_module)
648 has_process = false;
649 else
650 {
651 string library_name;
652 long statement_num_val;
653 has_process = derived_probe_builder::has_param(params, TOK_PROCESS);
654 has_library = get_string_param (params, TOK_LIBRARY, library_name);
655 if ((has_plt = has_null_param (params, TOK_PLT)))
656 plt_val = "*";
657 else has_plt = get_string_param (params, TOK_PLT, plt_val);
658 has_statement = get_number_param(params, TOK_STATEMENT, statement_num_val);
659
660 if (has_process)
661 {
662 if (get_number_param(params, TOK_PROCESS, pid_val))
663 {
664 // check that the pid given corresponds to a running process
665 string pid_err_msg;
666 if (!is_valid_pid(pid_val, pid_err_msg))
667 throw SEMANTIC_ERROR(pid_err_msg);
668
669 string pid_path = string("/proc/") + lex_cast(pid_val) + "/exe";
670 module_val = sess.sysroot + pid_path;
671 }
672 else
673 {
674 // reset the pid_val in case anything weird got written into it
675 pid_val = 0;
676 get_string_param(params, TOK_PROCESS, module_val);
677 }
678 module_val = find_executable (module_val, sess.sysroot, sess.sysenv);
679 if (!is_fully_resolved(module_val, sess.sysroot, sess.sysenv))
680 throw SEMANTIC_ERROR(_F("cannot find executable '%s'",
681 module_val.c_str()));
682 }
683
684 // Library probe? Let's target that instead if it is fully resolved (such
685 // as what query_one_library() would have done for us). Otherwise, we
686 // resort to iterate_over_libraries().
687 if (has_library && is_fully_resolved(library_name, sess.sysroot, sess.sysenv,
688 "LD_LIBRARY_PATH"))
689 {
690 path = path_remove_sysroot(sess, module_val);
691 module_val = library_name;
692 }
693 }
694
695 assert (has_kernel || has_process || has_module);
696 }
697
698 base_query::base_query(dwflpp & dw, const string & module_val)
699 : sess(dw.sess), dw(dw), has_library(false), has_plt(false), has_statement(false),
700 module_val(module_val), pid_val(0)
701 {
702 // NB: This uses '/' to distinguish between kernel modules and userspace,
703 // which means that userspace modules won't get any PATH searching.
704 if (module_val.find('/') == string::npos)
705 {
706 has_kernel = (module_val == TOK_KERNEL);
707 has_module = !has_kernel;
708 has_process = false;
709 }
710 else
711 {
712 has_kernel = has_module = false;
713 has_process = true;
714 }
715 }
716
717 bool
718 base_query::has_null_param(literal_map_t const & params,
719 string const & k)
720 {
721 return derived_probe_builder::has_null_param(params, k);
722 }
723
724
725 bool
726 base_query::get_string_param(literal_map_t const & params,
727 string const & k, string & v)
728 {
729 return derived_probe_builder::get_param (params, k, v);
730 }
731
732
733 bool
734 base_query::get_number_param(literal_map_t const & params,
735 string const & k, long & v)
736 {
737 int64_t value;
738 bool present = derived_probe_builder::get_param (params, k, value);
739 v = (long) value;
740 return present;
741 }
742
743
744 bool
745 base_query::get_number_param(literal_map_t const & params,
746 string const & k, long long & v)
747 {
748 int64_t value;
749 bool present = derived_probe_builder::get_param (params, k, value);
750 v = (long) value;
751 return present;
752 }
753
754
755 bool
756 base_query::get_number_param(literal_map_t const & params,
757 string const & k, Dwarf_Addr & v)
758 {
759 int64_t value;
760 bool present = derived_probe_builder::get_param (params, k, value);
761 v = (Dwarf_Addr) value;
762 return present;
763 }
764
765 struct dwarf_query : public base_query
766 {
767 dwarf_query(probe * base_probe,
768 probe_point * base_loc,
769 dwflpp & dw,
770 literal_map_t const & params,
771 vector<derived_probe *> & results,
772 const string user_path,
773 const string user_lib);
774
775 vector<derived_probe *> & results;
776 set<string> inlined_non_returnable; // function names
777 probe * base_probe;
778 probe_point * base_loc;
779 string user_path;
780 string user_lib;
781
782 set<string> visited_libraries;
783 bool resolved_library;
784
785 virtual void handle_query_module();
786 void query_module_dwarf();
787 void query_module_symtab();
788 void query_library (const char *data);
789 void query_plt (const char *entry, size_t addr);
790
791 void add_probe_point(string const & funcname,
792 char const * filename,
793 int line,
794 Dwarf_Die *scope_die,
795 Dwarf_Addr addr);
796
797 void mount_well_formed_probe_point();
798 void unmount_well_formed_probe_point();
799 stack<pair<probe_point*, probe*> > previous_bases;
800
801 void replace_probe_point_component_arg(const string& functor,
802 const string& new_functor,
803 int64_t new_arg,
804 bool hex = false);
805 void replace_probe_point_component_arg(const string& functor,
806 int64_t new_arg,
807 bool hex = false);
808 void replace_probe_point_component_arg(const string& functor,
809 const string& new_functor,
810 const string& new_arg);
811 void replace_probe_point_component_arg(const string& functor,
812 const string& new_arg);
813 void remove_probe_point_component(const string& functor);
814
815 // Track addresses we've already seen in a given module
816 set<Dwarf_Addr> alias_dupes;
817
818 // Track inlines we've already seen as well
819 // NB: this can't be compared just by entrypc, as inlines can overlap
820 set<inline_instance_info> inline_dupes;
821
822 // Used in .callee[s] probes, when calling iterate_over_callees() (which
823 // provides the actual stack). Retains the addrs of the callers unwind addr
824 // where the callee is found. Specifies multiple callers. E.g. when a callee
825 // at depth 2 is found, callers[1] has the addr of the caller, and callers[0]
826 // has the addr of the caller's caller.
827 stack<Dwarf_Addr> *callers;
828
829 bool has_function_str;
830 bool has_statement_str;
831 bool has_function_num;
832 bool has_statement_num;
833 string statement_str_val;
834 string function_str_val;
835 Dwarf_Addr statement_num_val;
836 Dwarf_Addr function_num_val;
837
838 bool has_call;
839 bool has_exported;
840 bool has_inline;
841 bool has_return;
842
843 bool has_nearest;
844
845 bool has_maxactive;
846 long maxactive_val;
847
848 bool has_label;
849 string label_val;
850
851 bool has_callee;
852 string callee_val;
853
854 bool has_callees_num;
855 long callees_num_val;
856
857 bool has_relative;
858 long relative_val;
859
860 bool has_absolute;
861
862 bool has_mark;
863
864 enum dbinfo_reqt dbinfo_reqt;
865 enum dbinfo_reqt assess_dbinfo_reqt();
866
867 void parse_function_spec(const string & spec);
868 function_spec_type spec_type;
869 vector<string> scopes;
870 string function;
871 string file;
872 lineno_t lineno_type;
873 vector<int> linenos;
874 bool query_done; // Found exact match
875
876 // Holds the prologue end of the current function
877 Dwarf_Addr prologue_end;
878
879 set<string> filtered_srcfiles;
880
881 // Map official entrypc -> func_info object
882 inline_instance_map_t filtered_inlines;
883 func_info_map_t filtered_functions;
884
885 // Helper when we want to iterate over both
886 base_func_info_map_t filtered_all();
887
888 void query_module_functions ();
889
890 string final_function_name(const string& final_func,
891 const char* final_file,
892 int final_line);
893
894 bool is_fully_specified_function();
895 };
896
897
898 uprobe_derived_probe::uprobe_derived_probe (const string& function,
899 const string& filename,
900 int line,
901 const string& module,
902 const string& section,
903 Dwarf_Addr dwfl_addr,
904 Dwarf_Addr addr,
905 dwarf_query & q,
906 Dwarf_Die* scope_die):
907 dwarf_derived_probe(function, filename, line, module, section,
908 dwfl_addr, addr, q, scope_die), pid(q.pid_val)
909 {}
910
911
912 static void delete_session_module_cache (systemtap_session& s); // forward decl
913
914 struct dwarf_builder: public derived_probe_builder
915 {
916 map <string,dwflpp*> kern_dw; /* NB: key string could be a wildcard */
917 map <string,dwflpp*> user_dw;
918 string user_path;
919 string user_lib;
920
921 // Holds modules to suggest functions from. NB: aggregates over
922 // recursive calls to build() when deriving globby probes.
923 set <string> modules_seen;
924
925 dwarf_builder() {}
926
927 dwflpp *get_kern_dw(systemtap_session& sess, const string& module)
928 {
929 if (kern_dw[module] == 0)
930 kern_dw[module] = new dwflpp(sess, module, true); // might throw
931 return kern_dw[module];
932 }
933
934 dwflpp *get_user_dw(systemtap_session& sess, const string& module)
935 {
936 if (user_dw[module] == 0)
937 user_dw[module] = new dwflpp(sess, module, false); // might throw
938 return user_dw[module];
939 }
940
941 /* NB: not virtual, so can be called from dtor too: */
942 void dwarf_build_no_more (bool)
943 {
944 delete_map(kern_dw);
945 delete_map(user_dw);
946 }
947
948 void build_no_more (systemtap_session &s)
949 {
950 dwarf_build_no_more (s.verbose > 3);
951 delete_session_module_cache (s);
952 }
953
954 ~dwarf_builder()
955 {
956 dwarf_build_no_more (false);
957 }
958
959 virtual void build(systemtap_session & sess,
960 probe * base,
961 probe_point * location,
962 literal_map_t const & parameters,
963 vector<derived_probe *> & finished_results);
964 };
965
966
967 dwarf_query::dwarf_query(probe * base_probe,
968 probe_point * base_loc,
969 dwflpp & dw,
970 literal_map_t const & params,
971 vector<derived_probe *> & results,
972 const string user_path,
973 const string user_lib)
974 : base_query(dw, params), results(results), base_probe(base_probe),
975 base_loc(base_loc), user_path(user_path), user_lib(user_lib),
976 resolved_library(false), callers(NULL), has_relative(false),
977 relative_val(0), prologue_end(0)
978 {
979 // Reduce the query to more reasonable semantic values (booleans,
980 // extracted strings, numbers, etc).
981 has_function_str = get_string_param(params, TOK_FUNCTION, function_str_val);
982 has_function_num = get_number_param(params, TOK_FUNCTION, function_num_val);
983
984 has_statement_str = get_string_param(params, TOK_STATEMENT, statement_str_val);
985 has_statement_num = get_number_param(params, TOK_STATEMENT, statement_num_val);
986
987 has_label = get_string_param(params, TOK_LABEL, label_val);
988 has_callee = get_string_param(params, TOK_CALLEE, callee_val);
989 if (has_null_param(params, TOK_CALLEES))
990 { // .callees ==> .callees(1) (also equivalent to .callee("*"))
991 has_callees_num = true;
992 callees_num_val = 1;
993 }
994 else
995 {
996 has_callees_num = get_number_param(params, TOK_CALLEES, callees_num_val);
997 if (has_callees_num && callees_num_val < 1)
998 throw SEMANTIC_ERROR(_(".callees(N) only acceptable for N >= 1"),
999 base_probe->tok);
1000 }
1001
1002 has_call = has_null_param(params, TOK_CALL);
1003 has_exported = has_null_param(params, TOK_EXPORTED);
1004 has_inline = has_null_param(params, TOK_INLINE);
1005 has_return = has_null_param(params, TOK_RETURN);
1006 has_nearest = has_null_param(params, TOK_NEAREST);
1007 has_maxactive = get_number_param(params, TOK_MAXACTIVE, maxactive_val);
1008 has_absolute = has_null_param(params, TOK_ABSOLUTE);
1009 has_mark = false;
1010
1011 if (has_function_str)
1012 parse_function_spec(function_str_val);
1013 else if (has_statement_str)
1014 parse_function_spec(statement_str_val);
1015
1016 dbinfo_reqt = assess_dbinfo_reqt();
1017 query_done = false;
1018 }
1019
1020
1021 void
1022 dwarf_query::query_module_dwarf()
1023 {
1024 if (has_function_num || has_statement_num)
1025 {
1026 // If we have module("foo").function(0xbeef) or
1027 // module("foo").statement(0xbeef), the address is relative
1028 // to the start of the module, so we seek the function
1029 // number plus the module's bias.
1030 Dwarf_Addr addr = has_function_num ?
1031 function_num_val : statement_num_val;
1032
1033 // These are raw addresses, we need to know what the elf_bias
1034 // is to feed it to libdwfl based functions.
1035 Dwarf_Addr elf_bias;
1036 Elf *elf = dwfl_module_getelf (dw.module, &elf_bias);
1037 assert(elf);
1038 addr += elf_bias;
1039 query_addr(addr, this);
1040 }
1041 else
1042 {
1043 // Otherwise if we have a function("foo") or statement("foo")
1044 // specifier, we have to scan over all the CUs looking for
1045 // the function(s) in question
1046 assert(has_function_str || has_statement_str);
1047
1048 // For simple cases, no wildcard and no source:line, we can do a very
1049 // quick function lookup in a module-wide cache.
1050 if (spec_type == function_alone &&
1051 !dw.name_has_wildcard(function) &&
1052 !startswith(function, "_Z"))
1053 query_module_functions();
1054 else
1055 dw.iterate_over_cus(&query_cu, this, false);
1056 }
1057 }
1058
1059 static void query_func_info (Dwarf_Addr entrypc, func_info & fi,
1060 dwarf_query * q);
1061
1062 static void
1063 query_symtab_func_info (func_info & fi, dwarf_query * q)
1064 {
1065 assert(null_die(&fi.die));
1066
1067 Dwarf_Addr addr = fi.addr;
1068
1069 // Now compensate for the dw bias because the addresses come
1070 // from dwfl_module_symtab, so fi->addr is NOT a normal dw address.
1071 q->dw.get_module_dwarf(false, false);
1072 addr -= q->dw.module_bias;
1073
1074 // If there are already probes in this module, lets not duplicate.
1075 // This can come from other weak symbols/aliases or existing
1076 // matches from Dwarf DIE functions. Try to add this addr to the
1077 // collection, and only continue if it was new.
1078 if (q->alias_dupes.insert(addr).second)
1079 query_func_info(addr, fi, q);
1080 }
1081
1082 void
1083 dwarf_query::query_module_symtab()
1084 {
1085 // Get the symbol table if it's necessary, sufficient, and not already got.
1086 if (dbinfo_reqt == dbr_need_dwarf)
1087 return;
1088
1089 module_info *mi = dw.mod_info;
1090 if (dbinfo_reqt == dbr_need_symtab)
1091 {
1092 if (mi->symtab_status == info_unknown)
1093 mi->get_symtab();
1094 if (mi->symtab_status == info_absent)
1095 return;
1096 }
1097
1098 func_info *fi = NULL;
1099 symbol_table *sym_table = mi->sym_table;
1100
1101 if (has_function_str)
1102 {
1103 // Per dwarf_query::assess_dbinfo_reqt()...
1104 assert(spec_type == function_alone);
1105 if (dw.name_has_wildcard(function_str_val))
1106 {
1107 symbol_table::iterator_t iter;
1108 for (iter = sym_table->map_by_addr.begin();
1109 iter != sym_table->map_by_addr.end();
1110 ++iter)
1111 {
1112 fi = iter->second;
1113 if (!null_die(&fi->die) // already handled in query_module_dwarf()
1114 || fi->descriptor) // ppc opd (and also undefined symbols)
1115 continue;
1116 if (dw.function_name_matches_pattern(fi->name, function_str_val))
1117 query_symtab_func_info(*fi, this);
1118 }
1119 }
1120 else
1121 {
1122 fi = sym_table->lookup_symbol(function_str_val);
1123 if (fi && !fi->descriptor && null_die(&fi->die))
1124 query_symtab_func_info(*fi, this);
1125 }
1126 }
1127 }
1128
1129 void
1130 dwarf_query::handle_query_module()
1131 {
1132 if (has_plt && has_statement_num)
1133 {
1134 query_plt_statement (this);
1135 return;
1136 }
1137
1138 bool report = dbinfo_reqt == dbr_need_dwarf;
1139 dw.get_module_dwarf(false, report);
1140
1141 // prebuild the symbol table to resolve aliases
1142 dw.mod_info->get_symtab();
1143
1144 // reset the dupe-checking for each new module
1145 alias_dupes.clear();
1146 inline_dupes.clear();
1147
1148 if (dw.mod_info->dwarf_status == info_present)
1149 query_module_dwarf();
1150
1151 // Consult the symbol table, asm and weak functions can show up
1152 // in the symbol table but not in dwarf and minidebuginfo is
1153 // located in the gnu_debugdata section, alias_dupes checking
1154 // is done before adding any probe points
1155 if (!query_done && !pending_interrupts)
1156 query_module_symtab();
1157 }
1158
1159
1160 void
1161 dwarf_query::parse_function_spec(const string & spec)
1162 {
1163 lineno_type = ABSOLUTE;
1164 size_t src_pos, line_pos, scope_pos;
1165
1166 // look for named scopes
1167 scope_pos = spec.rfind("::");
1168 if (scope_pos != string::npos)
1169 {
1170 tokenize_cxx(spec.substr(0, scope_pos), scopes);
1171 scope_pos += 2;
1172 }
1173 else
1174 scope_pos = 0;
1175
1176 // look for a source separator
1177 src_pos = spec.find('@', scope_pos);
1178 if (src_pos == string::npos)
1179 {
1180 function = spec.substr(scope_pos);
1181 spec_type = function_alone;
1182 }
1183 else
1184 {
1185 function = spec.substr(scope_pos, src_pos - scope_pos);
1186
1187 // look for a line-number separator
1188 line_pos = spec.find_first_of(":+", src_pos);
1189 if (line_pos == string::npos)
1190 {
1191 file = spec.substr(src_pos + 1);
1192 spec_type = function_and_file;
1193 }
1194 else
1195 {
1196 file = spec.substr(src_pos + 1, line_pos - src_pos - 1);
1197
1198 // classify the line spec
1199 spec_type = function_file_and_line;
1200 if (spec[line_pos] == '+')
1201 lineno_type = RELATIVE;
1202 else if (spec[line_pos + 1] == '*' &&
1203 spec.length() == line_pos + 2)
1204 lineno_type = WILDCARD;
1205 else
1206 lineno_type = ABSOLUTE;
1207
1208 if (lineno_type != WILDCARD)
1209 try
1210 {
1211 // try to parse N, N-M, or N,M,O,P, or combination thereof...
1212 if (spec.find_first_of(",-", line_pos + 1) != string::npos)
1213 {
1214 lineno_type = ENUMERATED;
1215 vector<string> sub_specs;
1216 tokenize(spec.substr(line_pos + 1), sub_specs, ",");
1217 vector<string>::const_iterator line_spec;
1218 for (line_spec = sub_specs.begin(); line_spec != sub_specs.end(); ++line_spec)
1219 {
1220 vector<string> ranges;
1221 tokenize(*line_spec, ranges, "-");
1222 if (ranges.size() > 1)
1223 for (int i = lex_cast<int>(ranges.front()); i <= lex_cast<int>(ranges.back()); i++)
1224 linenos.push_back(i);
1225 else
1226 linenos.push_back(lex_cast<int>(ranges.at(0)));
1227 }
1228 sort(linenos.begin(), linenos.end());
1229 }
1230 else
1231 {
1232 linenos.push_back(lex_cast<int>(spec.substr(line_pos + 1)));
1233 linenos.push_back(lex_cast<int>(spec.substr(line_pos + 1)));
1234 }
1235 }
1236 catch (runtime_error & exn)
1237 {
1238 goto bad;
1239 }
1240
1241 // only allow .nearest with ABSOLUTE and RELATIVE linenos
1242 if (has_nearest && lineno_type != ABSOLUTE
1243 && lineno_type != RELATIVE)
1244 throw SEMANTIC_ERROR(_(".nearest is only valid with absolute or relative line numbers"));
1245 }
1246 }
1247
1248 if (function.empty() ||
1249 (spec_type != function_alone && file.empty()))
1250 goto bad;
1251
1252 if (sess.verbose > 2)
1253 {
1254 //clog << "parsed '" << spec << "'";
1255 clog << _F("parse '%s'", spec.c_str());
1256
1257 if (!scopes.empty())
1258 clog << ", scope '" << scopes[0] << "'";
1259 for (unsigned i = 1; i < scopes.size(); ++i)
1260 clog << "::'" << scopes[i] << "'";
1261
1262 clog << ", func '" << function << "'";
1263
1264 if (spec_type != function_alone)
1265 clog << ", file '" << file << "'";
1266
1267 if (spec_type == function_file_and_line)
1268 {
1269 clog << ", line ";
1270 switch (lineno_type)
1271 {
1272 case ABSOLUTE:
1273 clog << linenos[0];
1274 break;
1275
1276 case RELATIVE:
1277 clog << "+" << linenos[0];
1278 break;
1279
1280 case ENUMERATED:
1281 {
1282 vector<int>::const_iterator linenos_it;
1283 for (linenos_it = linenos.begin(); linenos_it != linenos.end(); ++linenos_it)
1284 {
1285 vector<int>::const_iterator range_it(linenos_it);
1286 while ((range_it+1) != linenos.end() && *range_it + 1 == *(range_it+1))
1287 ++range_it;
1288 if (linenos_it == range_it)
1289 clog << *linenos_it;
1290 else
1291 clog << *linenos_it << "-" << *range_it;
1292 if (range_it + 1 != linenos.end())
1293 clog << ",";
1294 linenos_it = range_it;
1295 }
1296 }
1297 break;
1298
1299 case WILDCARD:
1300 clog << "*";
1301 break;
1302 }
1303 }
1304
1305 clog << endl;
1306 }
1307
1308 return;
1309
1310 bad:
1311 throw SEMANTIC_ERROR(_F("malformed specification '%s'", spec.c_str()),
1312 base_probe->tok);
1313 }
1314
1315 string path_remove_sysroot(const systemtap_session& sess, const string& path)
1316 {
1317 size_t pos;
1318 string retval = path;
1319 if (!sess.sysroot.empty() &&
1320 (pos = retval.find(sess.sysroot)) != string::npos)
1321 retval.replace(pos, sess.sysroot.length(), "/");
1322 return retval;
1323 }
1324
1325 void
1326 dwarf_query::add_probe_point(const string& dw_funcname,
1327 const char* filename,
1328 int line,
1329 Dwarf_Die* scope_die,
1330 Dwarf_Addr addr)
1331 {
1332 string reloc_section; // base section for relocation purposes
1333 Dwarf_Addr reloc_addr; // relocated
1334 const string& module = dw.module_name; // "kernel" or other
1335 string funcname = dw_funcname;
1336
1337 assert (! has_absolute); // already handled in dwarf_builder::build()
1338
1339 reloc_addr = dw.relocate_address(addr, reloc_section);
1340
1341 // If we originally used the linkage name, then let's call it that way
1342 const char* linkage_name;
1343 if (!null_die(scope_die) && startswith (this->function, "_Z")
1344 && (linkage_name = dwarf_linkage_name (scope_die)))
1345 funcname = linkage_name;
1346
1347 if (sess.verbose > 1)
1348 {
1349 clog << _("probe ") << funcname << "@" << filename << ":" << line;
1350 if (string(module) == TOK_KERNEL)
1351 clog << _(" kernel");
1352 else if (has_module)
1353 clog << _(" module=") << module;
1354 else if (has_process)
1355 clog << _(" process=") << module;
1356 if (reloc_section != "") clog << " reloc=" << reloc_section;
1357 clog << " pc=0x" << hex << addr << dec;
1358 }
1359
1360 dwflpp::blacklisted_type blacklisted = dw.blacklisted_p (funcname, filename,
1361 line, module, addr,
1362 has_return);
1363 if (sess.verbose > 1)
1364 clog << endl;
1365
1366 if (module == TOK_KERNEL)
1367 {
1368 // PR 4224: adapt to relocatable kernel by subtracting the _stext address here.
1369 reloc_addr = addr - sess.sym_stext;
1370 reloc_section = "_stext"; // a message to runtime's _stp_module_relocate
1371 }
1372
1373 if (!blacklisted)
1374 {
1375 sess.unwindsym_modules.insert (module);
1376
1377 if (has_process)
1378 {
1379 string module_tgt = path_remove_sysroot(sess, module);
1380 results.push_back (new uprobe_derived_probe(funcname, filename, line,
1381 module_tgt, reloc_section, addr, reloc_addr,
1382 *this, scope_die));
1383 }
1384 else
1385 {
1386 assert (has_kernel || has_module);
1387 results.push_back (new dwarf_derived_probe(funcname, filename, line,
1388 module, reloc_section, addr, reloc_addr,
1389 *this, scope_die));
1390 }
1391 }
1392 else
1393 {
1394 switch (blacklisted)
1395 {
1396 case dwflpp::blacklisted_section:
1397 sess.print_warning(_F("function %s is in blacklisted section",
1398 funcname.c_str()), base_probe->tok);
1399 break;
1400 case dwflpp::blacklisted_kprobes:
1401 sess.print_warning(_F("kprobes function %s is blacklisted",
1402 funcname.c_str()), base_probe->tok);
1403 break;
1404 case dwflpp::blacklisted_function_return:
1405 sess.print_warning(_F("function %s return probe is blacklisted",
1406 funcname.c_str()), base_probe->tok);
1407 break;
1408 case dwflpp::blacklisted_file:
1409 sess.print_warning(_F("function %s is in blacklisted file",
1410 funcname.c_str()), base_probe->tok);
1411 break;
1412 case dwflpp::blacklisted_function:
1413 default:
1414 sess.print_warning(_F("function %s is blacklisted",
1415 funcname.c_str()), base_probe->tok);
1416 break;
1417 }
1418 }
1419 }
1420
1421 void
1422 dwarf_query::mount_well_formed_probe_point()
1423 {
1424 string module = dw.module_name;
1425 if (has_process)
1426 module = path_remove_sysroot(sess, module);
1427
1428 vector<probe_point::component*> comps;
1429 vector<probe_point::component*>::iterator it;
1430 for (it = base_loc->components.begin();
1431 it != base_loc->components.end(); ++it)
1432 {
1433 if ((*it)->functor == TOK_PROCESS || (*it)->functor == TOK_MODULE)
1434 comps.push_back(new probe_point::component((*it)->functor,
1435 new literal_string(has_library ? path : module)));
1436 else
1437 comps.push_back(*it);
1438 }
1439
1440 probe_point *pp = new probe_point(*base_loc);
1441 pp->well_formed = true;
1442 pp->components = comps;
1443
1444 previous_bases.push(make_pair(base_loc, base_probe));
1445
1446 base_loc = pp;
1447 base_probe = new probe(base_probe, pp);
1448 }
1449
1450 void
1451 dwarf_query::unmount_well_formed_probe_point()
1452 {
1453 assert(!previous_bases.empty());
1454
1455 base_loc = previous_bases.top().first;
1456 base_probe = previous_bases.top().second;
1457
1458 previous_bases.pop();
1459 }
1460
1461 void
1462 dwarf_query::replace_probe_point_component_arg(const string& functor,
1463 const string& new_functor,
1464 int64_t new_arg,
1465 bool hex)
1466 {
1467 // only allow these operations if we're editing the well-formed loc
1468 assert(!previous_bases.empty());
1469
1470 vector<probe_point::component*>::iterator it;
1471 for (it = base_loc->components.begin();
1472 it != base_loc->components.end(); ++it)
1473 if ((*it)->functor == functor)
1474 *it = new probe_point::component(new_functor,
1475 new literal_number(new_arg, hex));
1476 }
1477
1478 void
1479 dwarf_query::replace_probe_point_component_arg(const string& functor,
1480 int64_t new_arg,
1481 bool hex)
1482 {
1483 replace_probe_point_component_arg(functor, functor, new_arg, hex);
1484 }
1485
1486 void
1487 dwarf_query::replace_probe_point_component_arg(const string& functor,
1488 const string& new_functor,
1489 const string& new_arg)
1490 {
1491 // only allow these operations if we're editing the well-formed loc
1492 assert(!previous_bases.empty());
1493
1494 vector<probe_point::component*>::iterator it;
1495 for (it = base_loc->components.begin();
1496 it != base_loc->components.end(); ++it)
1497 if ((*it)->functor == functor)
1498 *it = new probe_point::component(new_functor,
1499 new literal_string(new_arg));
1500 }
1501
1502 void
1503 dwarf_query::replace_probe_point_component_arg(const string& functor,
1504 const string& new_arg)
1505 {
1506 replace_probe_point_component_arg(functor, functor, new_arg);
1507 }
1508
1509 void
1510 dwarf_query::remove_probe_point_component(const string& functor)
1511 {
1512 // only allow these operations if we're editing the well-formed loc
1513 assert(!previous_bases.empty());
1514
1515 vector<probe_point::component*> new_comps;
1516 vector<probe_point::component*>::iterator it;
1517 for (it = base_loc->components.begin();
1518 it != base_loc->components.end(); ++it)
1519 if ((*it)->functor != functor)
1520 new_comps.push_back(*it);
1521
1522 base_loc->components = new_comps;
1523 }
1524
1525 enum dbinfo_reqt
1526 dwarf_query::assess_dbinfo_reqt()
1527 {
1528 if (has_absolute)
1529 {
1530 // kernel.statement(NUM).absolute
1531 return dbr_none;
1532 }
1533 if (has_inline || has_label || has_callee || has_callees_num)
1534 {
1535 // kernel.function("f").inline or module("m").function("f").inline
1536 return dbr_need_dwarf;
1537 }
1538 if (has_function_str && spec_type == function_alone)
1539 {
1540 // kernel.function("f") or module("m").function("f")
1541 return dbr_need_symtab;
1542 }
1543 if (has_statement_num)
1544 {
1545 // kernel.statement(NUM) or module("m").statement(NUM)
1546 // Technically, all we need is the module offset (or _stext, for
1547 // the kernel). But for that we need either the ELF file or (for
1548 // _stext) the symbol table. In either case, the symbol table
1549 // is available, and that allows us to map the NUM (address)
1550 // to a function, which is goodness.
1551 return dbr_need_symtab;
1552 }
1553 if (has_function_num)
1554 {
1555 // kernel.function(NUM) or module("m").function(NUM)
1556 // Need the symbol table so we can back up from NUM to the
1557 // start of the function.
1558 return dbr_need_symtab;
1559 }
1560 // Symbol table tells us nothing about source files or line numbers.
1561 return dbr_need_dwarf;
1562 }
1563
1564 string
1565 dwarf_query::final_function_name(const string& final_func,
1566 const char* final_file,
1567 int final_line)
1568 {
1569 string final_name = final_func;
1570 if (final_file && *final_file != '\0')
1571 {
1572 final_name += ("@" + string(final_file));
1573 if (final_line > 0)
1574 final_name += (":" + lex_cast(final_line));
1575 }
1576 return final_name;
1577 }
1578
1579 bool
1580 dwarf_query::is_fully_specified_function()
1581 {
1582 // A fully specified function is one that was given using a .function() probe
1583 // by full name (no wildcards), and specific srcfile and decl_line.
1584 return (has_function_str
1585 && spec_type == function_file_and_line
1586 && !dw.name_has_wildcard(function)
1587 && filtered_srcfiles.size() == 1
1588 && !filtered_functions.empty()
1589 && lineno_type == ABSOLUTE
1590 && filtered_functions[0].decl_line == linenos[0]);
1591 }
1592
1593 base_func_info_map_t
1594 dwarf_query::filtered_all(void)
1595 {
1596 base_func_info_map_t r;
1597 func_info_map_t::const_iterator f;
1598 for (f = filtered_functions.begin();
1599 f != filtered_functions.end(); ++f)
1600 r.push_back(*f);
1601 inline_instance_map_t::const_iterator i;
1602 for (i = filtered_inlines.begin();
1603 i != filtered_inlines.end(); ++i)
1604 r.push_back(*i);
1605 return r;
1606 }
1607
1608 // The critical determining factor when interpreting a pattern
1609 // string is, perhaps surprisingly: "presence of a lineno". The
1610 // presence of a lineno changes the search strategy completely.
1611 //
1612 // Compare the two cases:
1613 //
1614 // 1. {statement,function}(foo@file.c:lineno)
1615 // - find the files matching file.c
1616 // - in each file, find the functions matching foo
1617 // - query the file for line records matching lineno
1618 // - iterate over the line records,
1619 // - and iterate over the functions,
1620 // - if(haspc(function.DIE, line.addr))
1621 // - if looking for statements: probe(lineno.addr)
1622 // - if looking for functions: probe(function.{entrypc,return,etc.})
1623 //
1624 // 2. {statement,function}(foo@file.c)
1625 // - find the files matching file.c
1626 // - in each file, find the functions matching foo
1627 // - probe(function.{entrypc,return,etc.})
1628 //
1629 // Thus the first decision we make is based on the presence of a
1630 // lineno, and we enter entirely different sets of callbacks
1631 // depending on that decision.
1632 //
1633 // Note that the first case is a generalization fo the second, in that
1634 // we could theoretically search through line records for matching
1635 // file names (a "table scan" in rdbms lingo). Luckily, file names
1636 // are already cached elsewhere, so we can do an "index scan" as an
1637 // optimization.
1638
1639 static void
1640 query_statement (string const & func,
1641 char const * file,
1642 int line,
1643 Dwarf_Die *scope_die,
1644 Dwarf_Addr stmt_addr,
1645 dwarf_query * q)
1646 {
1647 try
1648 {
1649 q->add_probe_point(func, file ? file : "",
1650 line, scope_die, stmt_addr);
1651 }
1652 catch (const semantic_error& e)
1653 {
1654 q->sess.print_error (e);
1655 }
1656 }
1657
1658 static void
1659 query_addr(Dwarf_Addr addr, dwarf_query *q)
1660 {
1661 assert(q->has_function_num || q->has_statement_num);
1662
1663 dwflpp &dw = q->dw;
1664
1665 if (q->sess.verbose > 2)
1666 clog << "query_addr 0x" << hex << addr << dec << endl;
1667
1668 // First pick which CU contains this address
1669 Dwarf_Die* cudie = dw.query_cu_containing_address(addr);
1670 if (!cudie) // address could be wildly out of range
1671 return;
1672 dw.focus_on_cu(cudie);
1673
1674 // Now compensate for the dw bias
1675 addr -= dw.module_bias;
1676
1677 // Per PR5787, we look up the scope die even for
1678 // statement_num's, for blacklist sensitivity and $var
1679 // resolution purposes.
1680
1681 // Find the scopes containing this address
1682 vector<Dwarf_Die> scopes = dw.getscopes(addr);
1683 if (scopes.empty())
1684 return;
1685
1686 // Look for the innermost containing function
1687 Dwarf_Die *fnscope = NULL;
1688 for (size_t i = 0; i < scopes.size(); ++i)
1689 {
1690 int tag = dwarf_tag(&scopes[i]);
1691 if ((tag == DW_TAG_subprogram && !q->has_inline) ||
1692 (tag == DW_TAG_inlined_subroutine &&
1693 !q->has_call && !q->has_return && !q->has_exported))
1694 {
1695 fnscope = &scopes[i];
1696 break;
1697 }
1698 }
1699 if (!fnscope)
1700 return;
1701 dw.focus_on_function(fnscope);
1702
1703 Dwarf_Die *scope = q->has_function_num ? fnscope : &scopes[0];
1704
1705 const char *file = dwarf_decl_file(fnscope);
1706 int line;
1707 dwarf_decl_line(fnscope, &line);
1708
1709 // Function probes should reset the addr to the function entry
1710 // and possibly perform prologue searching
1711 if (q->has_function_num)
1712 {
1713 if (!dw.die_entrypc(fnscope, &addr))
1714 return;
1715 if (dwarf_tag(fnscope) == DW_TAG_subprogram &&
1716 (q->sess.prologue_searching ||
1717 (q->has_process && !q->dw.has_valid_locs()))) // PR 6871 && PR 6941
1718 {
1719 func_info func;
1720 func.die = *fnscope;
1721 func.name = dw.function_name;
1722 func.decl_file = file;
1723 func.decl_line = line;
1724 func.entrypc = addr;
1725
1726 func_info_map_t funcs(1, func);
1727 dw.resolve_prologue_endings (funcs);
1728 q->prologue_end = funcs[0].prologue_end;
1729
1730 // PR13200: if it's a .return probe, we need to emit a *retprobe based
1731 // on the entrypc so here we only use prologue_end for non .return
1732 // probes (note however that .return probes still take advantage of
1733 // prologue_end: PR14436)
1734 if (!q->has_return)
1735 addr = funcs[0].prologue_end;
1736 }
1737 }
1738 else
1739 {
1740 Dwarf_Line *address_line = dwarf_getsrc_die(cudie, addr);
1741 Dwarf_Addr address_line_addr = addr;
1742 if (address_line)
1743 {
1744 file = DWARF_LINESRC(address_line);
1745 line = DWARF_LINENO(address_line);
1746 address_line_addr = DWARF_LINEADDR(address_line);
1747 }
1748
1749 // Verify that a raw address matches the beginning of a
1750 // statement. This is a somewhat lame check that the address
1751 // is at the start of an assembly instruction. Mark probes are in the
1752 // middle of a macro and thus not strictly at a statement beginning.
1753 // Guru mode may override this check.
1754 if (!q->has_mark && (!address_line || address_line_addr != addr))
1755 {
1756 stringstream msg;
1757 msg << _F("address %#" PRIx64 " does not match the beginning of a statement",
1758 addr);
1759 if (address_line)
1760 msg << _F(" (try %#" PRIx64 ")", address_line_addr);
1761 else
1762 msg << _F(" (no line info found for '%s', in module '%s')",
1763 dw.cu_name().c_str(), dw.module_name.c_str());
1764 if (! q->sess.guru_mode)
1765 throw SEMANTIC_ERROR(msg.str());
1766 else
1767 q->sess.print_warning(msg.str());
1768 }
1769 }
1770
1771 // We're ready to build a probe, but before, we need to create the final,
1772 // well-formed version of this location with all the components filled in
1773 q->mount_well_formed_probe_point();
1774 q->replace_probe_point_component_arg(TOK_FUNCTION, addr, true /* hex */ );
1775 q->replace_probe_point_component_arg(TOK_STATEMENT, addr, true /* hex */ );
1776
1777 // Build a probe at this point
1778 query_statement(dw.function_name, file, line, scope, addr, q);
1779
1780 q->unmount_well_formed_probe_point();
1781 }
1782
1783 static void
1784 query_plt_statement(dwarf_query *q)
1785 {
1786 assert (q->has_plt && q->has_statement_num);
1787
1788 Dwarf_Addr addr = q->statement_num_val;
1789 if (q->sess.verbose > 2)
1790 clog << "query_plt_statement 0x" << hex << addr << dec << endl;
1791
1792 // First adjust the raw address to dwfl's elf bias.
1793 Dwarf_Addr elf_bias;
1794 Elf *elf = dwfl_module_getelf (q->dw.module, &elf_bias);
1795 assert(elf);
1796 addr += elf_bias;
1797
1798 // Now compensate for the dw bias
1799 q->dw.get_module_dwarf(false, false);
1800 addr -= q->dw.module_bias;
1801
1802 // Create the final well-formed probe point
1803 q->mount_well_formed_probe_point();
1804 q->replace_probe_point_component_arg(TOK_STATEMENT, q->statement_num_val, true /* hex */ );
1805
1806 // We remove the .plt part here, since if the user provided a .plt probe, then
1807 // the higher-level probe point is already well-formed. On the other hand, if
1808 // the user provides a .plt(PATTERN).statement(0xABCD), the PATTERN is
1809 // irrelevant (we won't iterate over plts) so just take it out.
1810 q->remove_probe_point_component(TOK_PLT);
1811
1812 // Build a probe at this point
1813 query_statement(q->plt_val, NULL, -1, NULL, addr, q);
1814
1815 q->unmount_well_formed_probe_point();
1816 }
1817
1818 static void
1819 query_label (const base_func_info& func,
1820 char const * label,
1821 char const * file,
1822 int line,
1823 Dwarf_Die *scope_die,
1824 Dwarf_Addr stmt_addr,
1825 dwarf_query * q)
1826 {
1827 assert (q->has_statement_str || q->has_function_str);
1828
1829 // weed out functions whose decl_file isn't one of
1830 // the source files that we actually care about
1831 if (q->spec_type != function_alone &&
1832 q->filtered_srcfiles.count(file) == 0)
1833 return;
1834
1835 // Create the final well-formed probe
1836 string canon_func = q->final_function_name(func.name, file, line);
1837
1838 q->mount_well_formed_probe_point();
1839 q->replace_probe_point_component_arg(TOK_FUNCTION, canon_func);
1840 q->replace_probe_point_component_arg(TOK_LABEL, label);
1841
1842 query_statement(func.name, file, line, scope_die, stmt_addr, q);
1843
1844 q->unmount_well_formed_probe_point();
1845 }
1846
1847 static void
1848 query_callee (base_func_info& callee,
1849 base_func_info& caller,
1850 stack<Dwarf_Addr> *callers,
1851 dwarf_query * q)
1852 {
1853 assert (q->has_function_str);
1854 assert (q->has_callee || q->has_callees_num);
1855
1856 // OK, we found a callee for a targeted caller. To help users see the
1857 // derivation, we add the well-formed form .function(caller).callee(callee).
1858
1859 string canon_caller = q->final_function_name(caller.name, caller.decl_file,
1860 caller.decl_line);
1861 string canon_callee = q->final_function_name(callee.name, callee.decl_file,
1862 callee.decl_line);
1863
1864 q->mount_well_formed_probe_point();
1865 q->replace_probe_point_component_arg(TOK_FUNCTION, canon_caller);
1866 q->replace_probe_point_component_arg(TOK_CALLEES, TOK_CALLEE, canon_callee);
1867 q->replace_probe_point_component_arg(TOK_CALLEE, canon_callee);
1868
1869 // Pass on the callers we'll need to add checks for
1870 q->callers = callers;
1871
1872 query_statement(callee.name, callee.decl_file, callee.decl_line,
1873 &callee.die, callee.entrypc, q);
1874
1875 q->unmount_well_formed_probe_point();
1876 }
1877
1878 static void
1879 query_inline_instance_info (inline_instance_info & ii,
1880 dwarf_query * q)
1881 {
1882 try
1883 {
1884 assert (! q->has_return); // checked by caller already
1885 assert (q->has_function_str || q->has_statement_str);
1886
1887 if (q->sess.verbose>2)
1888 clog << _F("querying entrypc %#" PRIx64 " of instance of inline '%s'\n",
1889 ii.entrypc, ii.name.c_str());
1890
1891 string canon_func = q->final_function_name(ii.name, ii.decl_file,
1892 ii.decl_line);
1893
1894 q->mount_well_formed_probe_point();
1895 q->replace_probe_point_component_arg(TOK_FUNCTION, canon_func);
1896 q->replace_probe_point_component_arg(TOK_STATEMENT, canon_func);
1897
1898 query_statement (ii.name, ii.decl_file, ii.decl_line,
1899 &ii.die, ii.entrypc, q);
1900
1901 q->unmount_well_formed_probe_point();
1902 }
1903 catch (semantic_error &e)
1904 {
1905 q->sess.print_error (e);
1906 }
1907 }
1908
1909 static void
1910 query_func_info (Dwarf_Addr entrypc,
1911 func_info & fi,
1912 dwarf_query * q)
1913 {
1914 assert(q->has_function_str || q->has_statement_str);
1915
1916 try
1917 {
1918 string canon_func = q->final_function_name(fi.name, fi.decl_file,
1919 fi.decl_line);
1920
1921 q->mount_well_formed_probe_point();
1922 q->replace_probe_point_component_arg(TOK_FUNCTION, canon_func);
1923 q->replace_probe_point_component_arg(TOK_STATEMENT, canon_func);
1924
1925 // If it's a .return probe, we need to emit a *retprobe based on the
1926 // entrypc (PR13200). Note however that if prologue_end is valid,
1927 // dwarf_derived_probe will still take advantage of it by creating a new
1928 // probe there if necessary to pick up target vars (PR14436).
1929 if (fi.prologue_end == 0 || q->has_return)
1930 {
1931 q->prologue_end = fi.prologue_end;
1932 query_statement (fi.name, fi.decl_file, fi.decl_line,
1933 &fi.die, entrypc, q);
1934 }
1935 else
1936 {
1937 query_statement (fi.name, fi.decl_file, fi.decl_line,
1938 &fi.die, fi.prologue_end, q);
1939 }
1940
1941 q->unmount_well_formed_probe_point();
1942 }
1943 catch (semantic_error &e)
1944 {
1945 q->sess.print_error (e);
1946 }
1947 }
1948
1949 static void
1950 query_srcfile_line (Dwarf_Addr addr, int lineno, dwarf_query * q)
1951 {
1952 assert (q->has_statement_str || q->has_function_str);
1953 assert (q->spec_type == function_file_and_line);
1954
1955 base_func_info_map_t bfis = q->filtered_all();
1956 base_func_info_map_t::iterator i;
1957 for (i = bfis.begin(); i != bfis.end(); ++i)
1958 {
1959 if (q->dw.die_has_pc (i->die, addr))
1960 {
1961 if (q->sess.verbose>3)
1962 clog << _("filtered DIE lands on srcfile\n");
1963 Dwarf_Die scope;
1964 q->dw.inner_die_containing_pc(i->die, addr, scope);
1965
1966 string canon_func = q->final_function_name(i->name, i->decl_file,
1967 lineno /* NB: not i->decl_line */ );
1968
1969 if (q->has_nearest)
1970 {
1971 int lineno_nearest = q->linenos[0];
1972 if (q->lineno_type == RELATIVE)
1973 lineno_nearest += i->decl_line;
1974 string canon_func_nearest = q->final_function_name(i->name, i->decl_file,
1975 lineno_nearest);
1976 q->mount_well_formed_probe_point();
1977 q->replace_probe_point_component_arg(TOK_STATEMENT, canon_func_nearest);
1978 }
1979
1980 q->mount_well_formed_probe_point();
1981 q->replace_probe_point_component_arg(TOK_FUNCTION, canon_func);
1982 q->replace_probe_point_component_arg(TOK_STATEMENT, canon_func);
1983
1984 query_statement (i->name, i->decl_file,
1985 lineno, // NB: not q->line !
1986 &scope, addr, q);
1987
1988 q->unmount_well_formed_probe_point();
1989 if (q->has_nearest)
1990 q->unmount_well_formed_probe_point();
1991 }
1992 }
1993 }
1994
1995 bool
1996 inline_instance_info::operator<(const inline_instance_info& other) const
1997 {
1998 if (entrypc != other.entrypc)
1999 return entrypc < other.entrypc;
2000
2001 if (decl_line != other.decl_line)
2002 return decl_line < other.decl_line;
2003
2004 int cmp = name.compare(other.name);
2005
2006 if (!cmp)
2007 {
2008 assert (decl_file);
2009 assert (other.decl_file);
2010 cmp = strcmp(decl_file, other.decl_file);
2011 }
2012
2013 return cmp < 0;
2014 }
2015
2016
2017 static int
2018 query_dwarf_inline_instance (Dwarf_Die * die, dwarf_query * q)
2019 {
2020 assert (q->has_statement_str || q->has_function_str);
2021 assert (!q->has_call && !q->has_return && !q->has_exported);
2022
2023 try
2024 {
2025 if (q->sess.verbose>2)
2026 clog << _F("selected inline instance of %s\n", q->dw.function_name.c_str());
2027
2028 Dwarf_Addr entrypc;
2029 if (q->dw.die_entrypc (die, &entrypc))
2030 {
2031 inline_instance_info inl;
2032 inl.die = *die;
2033 inl.name = q->dw.function_name;
2034 inl.entrypc = entrypc;
2035 q->dw.function_file (&inl.decl_file);
2036 q->dw.function_line (&inl.decl_line);
2037
2038 // make sure that this inline hasn't already
2039 // been matched from a different CU
2040 if (q->inline_dupes.insert(inl).second)
2041 q->filtered_inlines.push_back(inl);
2042 }
2043 return DWARF_CB_OK;
2044 }
2045 catch (const semantic_error& e)
2046 {
2047 q->sess.print_error (e);
2048 return DWARF_CB_ABORT;
2049 }
2050 }
2051
2052 static int
2053 query_dwarf_func (Dwarf_Die * func, dwarf_query * q)
2054 {
2055 assert (q->has_statement_str || q->has_function_str);
2056
2057 // weed out functions whose decl_file isn't one of
2058 // the source files that we actually care about
2059 if (q->spec_type != function_alone &&
2060 q->filtered_srcfiles.count(dwarf_decl_file(func)?:"") == 0)
2061 return DWARF_CB_OK;
2062
2063 try
2064 {
2065 q->dw.focus_on_function (func);
2066
2067 if (!q->dw.function_scope_matches(q->scopes))
2068 return DWARF_CB_OK;
2069
2070 // make sure that this function address hasn't
2071 // already been matched under an aliased name
2072 Dwarf_Addr addr;
2073 if (!q->dw.func_is_inline() &&
2074 dwarf_entrypc(func, &addr) == 0 &&
2075 !q->alias_dupes.insert(addr).second)
2076 return DWARF_CB_OK;
2077
2078 if (q->dw.func_is_inline () && (! q->has_call) && (! q->has_return) && (! q->has_exported))
2079 {
2080 if (q->sess.verbose>3)
2081 clog << _F("checking instances of inline %s\n", q->dw.function_name.c_str());
2082 q->dw.iterate_over_inline_instances (query_dwarf_inline_instance, q);
2083 }
2084 else if (q->dw.func_is_inline () && (q->has_return)) // PR 11553
2085 {
2086 q->inlined_non_returnable.insert (q->dw.function_name);
2087 }
2088 else if (!q->dw.func_is_inline () && (! q->has_inline))
2089 {
2090 if (q->has_exported && !q->dw.func_is_exported ())
2091 return DWARF_CB_OK;
2092 if (q->sess.verbose>2)
2093 clog << _F("selected function %s\n", q->dw.function_name.c_str());
2094
2095 func_info func;
2096 q->dw.function_die (&func.die);
2097 func.name = q->dw.function_name;
2098 q->dw.function_file (&func.decl_file);
2099 q->dw.function_line (&func.decl_line);
2100
2101 Dwarf_Addr entrypc;
2102 if (q->dw.function_entrypc (&entrypc))
2103 {
2104 func.entrypc = entrypc;
2105 q->filtered_functions.push_back (func);
2106 }
2107 /* else this function is fully inlined, just ignore it */
2108 }
2109 return DWARF_CB_OK;
2110 }
2111 catch (const semantic_error& e)
2112 {
2113 q->sess.print_error (e);
2114 return DWARF_CB_ABORT;
2115 }
2116 }
2117
2118 static int
2119 query_cu (Dwarf_Die * cudie, dwarf_query * q)
2120 {
2121 assert (q->has_statement_str || q->has_function_str);
2122
2123 if (pending_interrupts) return DWARF_CB_ABORT;
2124
2125 try
2126 {
2127 q->dw.focus_on_cu (cudie);
2128
2129 if (false && q->sess.verbose>2)
2130 clog << _F("focused on CU '%s', in module '%s'\n",
2131 q->dw.cu_name().c_str(), q->dw.module_name.c_str());
2132
2133 q->filtered_srcfiles.clear();
2134 q->filtered_functions.clear();
2135 q->filtered_inlines.clear();
2136
2137 // In this path, we find "abstract functions", record
2138 // information about them, and then (depending on lineno
2139 // matching) possibly emit one or more of the function's
2140 // associated addresses. Unfortunately the control of this
2141 // cannot easily be turned inside out.
2142
2143 if (q->spec_type != function_alone)
2144 {
2145 // If we have a pattern string with a filename, we need
2146 // to elaborate the srcfile mask in question first.
2147 q->dw.collect_srcfiles_matching (q->file, q->filtered_srcfiles);
2148
2149 // If we have a file pattern and *no* srcfile matches, there's
2150 // no need to look further into this CU, so skip.
2151 if (q->filtered_srcfiles.empty())
2152 return DWARF_CB_OK;
2153 }
2154
2155 // Pick up [entrypc, name, DIE] tuples for all the functions
2156 // matching the query, and fill in the prologue endings of them
2157 // all in a single pass.
2158 int rc = q->dw.iterate_over_functions (query_dwarf_func, q, q->function);
2159 if (rc != DWARF_CB_OK)
2160 q->query_done = true;
2161
2162 if (!q->filtered_functions.empty() &&
2163 !q->has_statement_str && // PR 2608
2164 (q->sess.prologue_searching ||
2165 (q->has_process && !q->dw.has_valid_locs()))) // PR 6871 && PR 6941
2166 q->dw.resolve_prologue_endings (q->filtered_functions);
2167
2168 if (q->has_label)
2169 {
2170 enum lineno_t lineno_type = WILDCARD;
2171 if (q->spec_type == function_file_and_line)
2172 lineno_type = q->lineno_type;
2173 base_func_info_map_t bfis = q->filtered_all();
2174 base_func_info_map_t::iterator i;
2175 for (i = bfis.begin(); i != bfis.end(); ++i)
2176 q->dw.iterate_over_labels (&i->die, q->label_val, *i, q->linenos,
2177 lineno_type, q, query_label);
2178 }
2179 else if (q->has_callee || q->has_callees_num)
2180 {
2181 // .callee(str) --> str, .callees[(N)] --> "*"
2182 string callee_val = q->has_callee ? q->callee_val : "*";
2183 long callees_num_val = q->has_callees_num ? q->callees_num_val : 1;
2184
2185 // NB: We filter functions that do not match the file here rather than
2186 // in query_callee because we only want the filtering to apply to the
2187 // first level, not to callees that are recursed into if
2188 // callees_num_val > 1.
2189 base_func_info_map_t bfis = q->filtered_all();
2190 base_func_info_map_t::iterator i;
2191 for (i = bfis.begin(); i != bfis.end(); ++i)
2192 {
2193 if (q->spec_type != function_alone &&
2194 q->filtered_srcfiles.count(i->decl_file) == 0)
2195 continue;
2196 q->dw.iterate_over_callees (&i->die, callee_val,
2197 callees_num_val,
2198 q, query_callee, *i);
2199 }
2200 }
2201 else if (q->spec_type == function_file_and_line
2202 // User specified function, file and lineno, but if they match
2203 // exactly a specific function in a specific line at a specific
2204 // decl_line, the user doesn't actually want to probe a lineno,
2205 // but rather the function itself. So let fall through to
2206 // query_func_info/query_inline_instance_info in final else.
2207 && !q->is_fully_specified_function())
2208 {
2209 // .statement(...:NN) often gets mixed up with .function(...:NN)
2210 if (q->has_function_str)
2211 q->sess.print_warning (_("For probing a particular line, use a "
2212 ".statement() probe, not .function()"),
2213 q->base_probe->tok);
2214
2215 base_func_info_map_t bfis = q->filtered_all();
2216
2217 set<string>::const_iterator srcfile;
2218 for (srcfile = q->filtered_srcfiles.begin();
2219 srcfile != q->filtered_srcfiles.end(); ++srcfile)
2220 q->dw.iterate_over_srcfile_lines(srcfile->c_str(), q->linenos,
2221 q->lineno_type, bfis,
2222 query_srcfile_line,
2223 q->has_nearest, q);
2224 }
2225 else
2226 {
2227 // Otherwise, simply probe all resolved functions.
2228 for (func_info_map_t::iterator i = q->filtered_functions.begin();
2229 i != q->filtered_functions.end(); ++i)
2230 query_func_info (i->entrypc, *i, q);
2231
2232 // And all inline instances (if we're not excluding inlines with ".call")
2233 if (! q->has_call)
2234 for (inline_instance_map_t::iterator i
2235 = q->filtered_inlines.begin(); i != q->filtered_inlines.end(); ++i)
2236 query_inline_instance_info (*i, q);
2237 }
2238 return DWARF_CB_OK;
2239 }
2240 catch (const semantic_error& e)
2241 {
2242 q->sess.print_error (e);
2243 return DWARF_CB_ABORT;
2244 }
2245 }
2246
2247
2248 void
2249 dwarf_query::query_module_functions ()
2250 {
2251 try
2252 {
2253 filtered_srcfiles.clear();
2254 filtered_functions.clear();
2255 filtered_inlines.clear();
2256
2257 // Collect all module functions so we know which CUs are interesting
2258 int rc = dw.iterate_single_function(query_dwarf_func, this, function);
2259 if (rc != DWARF_CB_OK)
2260 {
2261 query_done = true;
2262 return;
2263 }
2264
2265 set<void*> used_cus; // by cu->addr
2266 vector<Dwarf_Die> cus;
2267 Dwarf_Die cu_mem;
2268
2269 base_func_info_map_t bfis = filtered_all();
2270 base_func_info_map_t::iterator i;
2271 for (i = bfis.begin(); i != bfis.end(); ++i)
2272 if (dwarf_diecu(&i->die, &cu_mem, NULL, NULL) &&
2273 used_cus.insert(cu_mem.addr).second)
2274 cus.push_back(cu_mem);
2275
2276 // Reset the dupes since we didn't actually collect them the first time
2277 alias_dupes.clear();
2278 inline_dupes.clear();
2279
2280 // Run the query again on the individual CUs
2281 for (vector<Dwarf_Die>::iterator i = cus.begin(); i != cus.end(); ++i){
2282 rc = query_cu(&*i, this);
2283 if (rc != DWARF_CB_OK)
2284 {
2285 query_done = true;
2286 return;
2287 }
2288 }
2289 }
2290 catch (const semantic_error& e)
2291 {
2292 sess.print_error (e);
2293 }
2294 }
2295
2296
2297 static void
2298 validate_module_elf (Dwfl_Module *mod, const char *name, base_query *q)
2299 {
2300 // Validate the machine code in this elf file against the
2301 // session machine. This is important, in case the wrong kind
2302 // of debuginfo is being automagically processed by elfutils.
2303 // While we can tell i686 apart from x86-64, unfortunately
2304 // we can't help confusing i586 vs i686 (both EM_386).
2305
2306 Dwarf_Addr bias;
2307 // We prefer dwfl_module_getdwarf to dwfl_module_getelf here,
2308 // because dwfl_module_getelf can force costly section relocations
2309 // we don't really need, while either will do for this purpose.
2310 Elf* elf = (dwarf_getelf (dwfl_module_getdwarf (mod, &bias))
2311 ?: dwfl_module_getelf (mod, &bias));
2312
2313 GElf_Ehdr ehdr_mem;
2314 GElf_Ehdr* em = gelf_getehdr (elf, &ehdr_mem);
2315 if (em == 0) { DWFL_ASSERT ("dwfl_getehdr", dwfl_errno()); }
2316 assert(em);
2317 int elf_machine = em->e_machine;
2318 const char* debug_filename = "";
2319 const char* main_filename = "";
2320 (void) dwfl_module_info (mod, NULL, NULL,
2321 NULL, NULL, NULL,
2322 & main_filename,
2323 & debug_filename);
2324 const string& sess_machine = q->sess.architecture;
2325
2326 string expect_machine; // to match sess.machine (i.e., kernel machine)
2327 string expect_machine2;
2328
2329 // NB: See also the 'uname -m' squashing done in main.cxx.
2330 switch (elf_machine)
2331 {
2332 // x86 and ppc are bi-architecture; a 64-bit kernel
2333 // can normally run either 32-bit or 64-bit *userspace*.
2334 case EM_386:
2335 expect_machine = "i?86";
2336 if (! q->has_process) break; // 32-bit kernel/module
2337 /* FALLSTHROUGH */
2338 case EM_X86_64:
2339 expect_machine2 = "x86_64";
2340 break;
2341 case EM_PPC:
2342 case EM_PPC64:
2343 expect_machine = "powerpc";
2344 break;
2345 case EM_S390: expect_machine = "s390"; break;
2346 case EM_IA_64: expect_machine = "ia64"; break;
2347 case EM_ARM: expect_machine = "arm*"; break;
2348 case EM_AARCH64: expect_machine = "arm64"; break;
2349 // XXX: fill in some more of these
2350 default: expect_machine = "?"; break;
2351 }
2352
2353 if (! debug_filename) debug_filename = main_filename;
2354 if (! debug_filename) debug_filename = name;
2355
2356 if (fnmatch (expect_machine.c_str(), sess_machine.c_str(), 0) != 0 &&
2357 fnmatch (expect_machine2.c_str(), sess_machine.c_str(), 0) != 0)
2358 {
2359 stringstream msg;
2360 msg << _F("ELF machine %s|%s (code %d) mismatch with target %s in '%s'",
2361 expect_machine.c_str(), expect_machine2.c_str(), elf_machine,
2362 sess_machine.c_str(), debug_filename);
2363 throw SEMANTIC_ERROR(msg.str ());
2364 }
2365
2366 if (q->sess.verbose>1)
2367 clog << _F("focused on module '%s' = [%#" PRIx64 "-%#" PRIx64 ", bias %#" PRIx64
2368 " file %s ELF machine %s|%s (code %d)\n",
2369 q->dw.module_name.c_str(), q->dw.module_start, q->dw.module_end,
2370 q->dw.module_bias, debug_filename, expect_machine.c_str(),
2371 expect_machine2.c_str(), elf_machine);
2372 }
2373
2374
2375
2376 static Dwarf_Addr
2377 lookup_symbol_address (Dwfl_Module *m, const char* wanted)
2378 {
2379 int syments = dwfl_module_getsymtab(m);
2380 assert(syments);
2381 for (int i = 1; i < syments; ++i)
2382 {
2383 GElf_Sym sym;
2384 const char *name = dwfl_module_getsym(m, i, &sym, NULL);
2385 if (name != NULL && strcmp(name, wanted) == 0)
2386 return sym.st_value;
2387 }
2388
2389 return 0;
2390 }
2391
2392
2393
2394 static int
2395 query_module (Dwfl_Module *mod,
2396 void **,
2397 const char *name,
2398 Dwarf_Addr addr,
2399 base_query *q)
2400 {
2401 try
2402 {
2403 module_info* mi = q->sess.module_cache->cache[name];
2404 if (mi == 0)
2405 {
2406 mi = q->sess.module_cache->cache[name] = new module_info(name);
2407
2408 mi->mod = mod;
2409 mi->addr = addr;
2410
2411 const char* debug_filename = "";
2412 const char* main_filename = "";
2413 (void) dwfl_module_info (mod, NULL, NULL,
2414 NULL, NULL, NULL,
2415 & main_filename,
2416 & debug_filename);
2417
2418 if (debug_filename || main_filename)
2419 {
2420 mi->elf_path = debug_filename ?: main_filename;
2421 }
2422 else if (name == TOK_KERNEL)
2423 {
2424 mi->dwarf_status = info_absent;
2425 }
2426 }
2427 // OK, enough of that module_info caching business.
2428
2429 q->dw.focus_on_module(mod, mi);
2430
2431 // If we have enough information in the pattern to skip a module and
2432 // the module does not match that information, return early.
2433 if (!q->dw.module_name_matches(q->module_val))
2434 return pending_interrupts ? DWARF_CB_ABORT : DWARF_CB_OK;
2435
2436 // Don't allow module("*kernel*") type expressions to match the
2437 // elfutils module "kernel", which we refer to in the probe
2438 // point syntax exclusively as "kernel.*".
2439 if (q->dw.module_name == TOK_KERNEL && ! q->has_kernel)
2440 return pending_interrupts ? DWARF_CB_ABORT : DWARF_CB_OK;
2441
2442 if (mod)
2443 validate_module_elf(mod, name, q);
2444 else
2445 assert(q->has_kernel); // and no vmlinux to examine
2446
2447 if (q->sess.verbose>2)
2448 cerr << _F("focused on module '%s'\n", q->dw.module_name.c_str());
2449
2450
2451 // Collect a few kernel addresses. XXX: these belong better
2452 // to the sess.module_info["kernel"] struct.
2453 if (q->dw.module_name == TOK_KERNEL)
2454 {
2455 if (! q->sess.sym_kprobes_text_start)
2456 q->sess.sym_kprobes_text_start = lookup_symbol_address (mod, "__kprobes_text_start");
2457 if (! q->sess.sym_kprobes_text_end)
2458 q->sess.sym_kprobes_text_end = lookup_symbol_address (mod, "__kprobes_text_end");
2459 if (! q->sess.sym_stext)
2460 q->sess.sym_stext = lookup_symbol_address (mod, "_stext");
2461 }
2462
2463 // If there is a .library component, then q->path will hold the path to
2464 // the executable if the library was fully resolved. If not (e.g. not
2465 // absolute, or globby), resort to iterate_over_libraries().
2466 if (q->has_library && q->path.empty())
2467 q->dw.iterate_over_libraries (&q->query_library_callback, q);
2468 // .plt is translated to .plt.statement(N). We only want to iterate for the
2469 // .plt case
2470 else if (q->has_plt && ! q->has_statement)
2471 {
2472 q->dw.iterate_over_plt (q, &q->query_plt_callback);
2473 q->visited_modules.insert(name);
2474 }
2475 else
2476 {
2477 // search the module for matches of the probe point.
2478 q->handle_query_module();
2479 q->visited_modules.insert(name);
2480 }
2481
2482 // If we know that there will be no more matches, abort early.
2483 if (q->dw.module_name_final_match(q->module_val) || pending_interrupts)
2484 return DWARF_CB_ABORT;
2485 else
2486 return DWARF_CB_OK;
2487 }
2488 catch (const semantic_error& e)
2489 {
2490 q->sess.print_error (e);
2491 return DWARF_CB_ABORT;
2492 }
2493 }
2494
2495
2496 void
2497 base_query::query_library_callback (base_query *me, const char *data)
2498 {
2499 me->query_library (data);
2500 }
2501
2502
2503 probe*
2504 build_library_probe(dwflpp& dw,
2505 const string& library,
2506 probe *base_probe,
2507 probe_point *base_loc)
2508 {
2509 probe_point* specific_loc = new probe_point(*base_loc);
2510 specific_loc->from_glob = true;
2511 vector<probe_point::component*> derived_comps;
2512
2513 // Create new probe point for the matching library. This is what will be
2514 // shown in listing mode. Also replace the process(str) with the real
2515 // absolute path rather than keeping what the user typed in.
2516 vector<probe_point::component*>::iterator it;
2517 for (it = specific_loc->components.begin();
2518 it != specific_loc->components.end(); ++it)
2519 if ((*it)->functor == TOK_PROCESS)
2520 derived_comps.push_back(new probe_point::component(TOK_PROCESS,
2521 new literal_string(path_remove_sysroot(dw.sess, dw.module_name))));
2522 else if ((*it)->functor == TOK_LIBRARY)
2523 derived_comps.push_back(new probe_point::component(TOK_LIBRARY,
2524 new literal_string(path_remove_sysroot(dw.sess, library))));
2525 else
2526 derived_comps.push_back(*it);
2527 probe_point* derived_loc = new probe_point(*specific_loc);
2528 derived_loc->components = derived_comps;
2529 return new probe (new probe (base_probe, specific_loc), derived_loc);
2530 }
2531
2532 bool
2533 query_one_library (const char *library, dwflpp & dw,
2534 const string user_lib, probe * base_probe, probe_point *base_loc,
2535 vector<derived_probe *> & results)
2536 {
2537 if (dw.function_name_matches_pattern(library, "*" + user_lib))
2538 {
2539 string library_path = find_executable (library, "", dw.sess.sysenv,
2540 "LD_LIBRARY_PATH");
2541 probe *new_base = build_library_probe(dw, library_path,
2542 base_probe, base_loc);
2543
2544 // We pass true for the optional parameter of derive_probes() here to
2545 // indicate that we don't mind if the probe doesn't resolve. This is
2546 // because users expect wildcarded probe points to only apply to a subset
2547 // of matching libraries, in the sense of "any", rather than "all", just
2548 // like module("*") and process("*"). See also dwarf_builder::build().
2549 derive_probes(dw.sess, new_base, results, true /* optional */ );
2550
2551 if (dw.sess.verbose > 2)
2552 clog << _("module=") << library_path << endl;
2553 return true;
2554 }
2555 return false;
2556 }
2557
2558
2559 void
2560 dwarf_query::query_library (const char *library)
2561 {
2562 visited_libraries.insert(library);
2563 if (query_one_library (library, dw, user_lib, base_probe, base_loc, results))
2564 resolved_library = true;
2565 }
2566
2567 struct plt_expanding_visitor: public var_expanding_visitor
2568 {
2569 plt_expanding_visitor(const string & entry):
2570 entry (entry)
2571 {
2572 }
2573 const string & entry;
2574
2575 void visit_target_symbol (target_symbol* e);
2576 };
2577
2578
2579 void
2580 base_query::query_plt_callback (base_query *me, const char *entry, size_t address)
2581 {
2582 if (me->dw.function_name_matches_pattern (entry, me->plt_val))
2583 me->query_plt (entry, address);
2584 me->dw.mod_info->plt_funcs.insert(entry);
2585 }
2586
2587
2588 void
2589 query_one_plt (const char *entry, long addr, dwflpp & dw,
2590 probe * base_probe, probe_point *base_loc,
2591 vector<derived_probe *> & results, base_query *q)
2592 {
2593 string module = dw.module_name;
2594 if (q->has_process)
2595 module = path_remove_sysroot(dw.sess, module);
2596
2597 probe_point* specific_loc = new probe_point(*base_loc);
2598 specific_loc->well_formed = true;
2599
2600 vector<probe_point::component*> derived_comps;
2601
2602 if (dw.sess.verbose > 2)
2603 clog << _F("plt entry=%s\n", entry);
2604
2605 vector<probe_point::component*>::iterator it;
2606 for (it = specific_loc->components.begin();
2607 it != specific_loc->components.end(); ++it)
2608 if ((*it)->functor == TOK_PROCESS)
2609 {
2610 // Replace with fully resolved path
2611 *it = new probe_point::component(TOK_PROCESS,
2612 new literal_string(q->has_library ? q->path : module));
2613 derived_comps.push_back(*it);
2614 }
2615 else if ((*it)->functor == TOK_PLT)
2616 {
2617 // Replace possibly globby component
2618 *it = new probe_point::component(TOK_PLT,
2619 new literal_string(entry));
2620 derived_comps.push_back(*it);
2621 derived_comps.push_back(new probe_point::component(TOK_STATEMENT,
2622 new literal_number(addr, true)));
2623 }
2624 else
2625 derived_comps.push_back(*it);
2626 probe_point* derived_loc = new probe_point(*specific_loc);
2627 derived_loc->components = derived_comps;
2628 probe *new_base = new probe (new probe (base_probe, specific_loc),
2629 derived_loc);
2630 string e = string(entry);
2631 plt_expanding_visitor pltv (e);
2632 pltv.replace (new_base->body);
2633
2634 literal_map_t params;
2635 for (unsigned i = 0; i < derived_loc->components.size(); ++i)
2636 {
2637 probe_point::component *c = derived_loc->components[i];
2638 params[c->functor] = c->arg;
2639 }
2640 dwarf_query derived_q(new_base, derived_loc, dw, params, results, "", "");
2641 dw.iterate_over_modules<base_query>(&query_module, &derived_q);
2642 }
2643
2644
2645 void
2646 dwarf_query::query_plt (const char *entry, size_t address)
2647 {
2648 query_one_plt (entry, address, dw, base_probe, base_loc, results, this);
2649 }
2650
2651 // This would more naturally fit into elaborate.cxx:semantic_pass_symbols,
2652 // but the needed declaration for module_cache is not available there.
2653 // Nor for that matter in session.cxx. Only in this CU is that field ever
2654 // set (in query_module() above), so we clean it up here too.
2655 static void
2656 delete_session_module_cache (systemtap_session& s)
2657 {
2658 if (s.module_cache) {
2659 if (s.verbose > 3)
2660 clog << _("deleting module_cache") << endl;
2661 delete s.module_cache;
2662 s.module_cache = 0;
2663 }
2664 }
2665
2666
2667 struct dwarf_var_expanding_visitor: public var_expanding_visitor
2668 {
2669 dwarf_query & q;
2670 Dwarf_Die *scope_die;
2671 Dwarf_Addr addr;
2672 block *add_block;
2673 block *add_call_probe; // synthesized from .return probes with saved $vars
2674 // NB: tids are not always collected in add_block & add_call_probe, because
2675 // gen_kretprobe_saved_return doesn't need them. Thus we need these extra
2676 // *_tid bools for gen_mapped_saved_return to tell what's there.
2677 bool add_block_tid, add_call_probe_tid;
2678 unsigned saved_longs, saved_strings; // data saved within kretprobes
2679 map<std::string, expression *> return_ts_map;
2680 vector<Dwarf_Die> scopes;
2681 // probe counter name -> pointer of associated probe
2682 std::set<std::string> perf_counter_refs;
2683 bool visited;
2684
2685 dwarf_var_expanding_visitor(dwarf_query & q, Dwarf_Die *sd, Dwarf_Addr a):
2686 q(q), scope_die(sd), addr(a), add_block(NULL), add_call_probe(NULL),
2687 add_block_tid(false), add_call_probe_tid(false),
2688 saved_longs(0), saved_strings(0), visited(false) {}
2689 expression* gen_mapped_saved_return(expression* e, const string& name);
2690 expression* gen_kretprobe_saved_return(expression* e);
2691 void visit_target_symbol_saved_return (target_symbol* e);
2692 void visit_target_symbol_context (target_symbol* e);
2693 void visit_target_symbol (target_symbol* e);
2694 void visit_atvar_op (atvar_op* e);
2695 void visit_cast_op (cast_op* e);
2696 void visit_entry_op (entry_op* e);
2697 void visit_perf_op (perf_op* e);
2698 private:
2699 vector<Dwarf_Die>& getscopes(target_symbol *e);
2700 };
2701
2702
2703 unsigned var_expanding_visitor::tick = 0;
2704
2705
2706 var_expanding_visitor::var_expanding_visitor (): op()
2707 {
2708 // FIXME: for the time being, by default we only support plain '$foo
2709 // = bar', not '+=' or any other op= variant. This is fixable, but a
2710 // bit ugly.
2711 //
2712 // If derived classes desire to add additional operator support, add
2713 // new operators to this list in the derived class constructor.
2714 valid_ops.insert ("=");
2715 }
2716
2717
2718 void
2719 var_expanding_visitor::provide_lvalue_call(functioncall* fcall)
2720 {
2721 // Provide the functioncall to our parent, so that it can be used to
2722 // substitute for the assignment node immediately above us.
2723 assert(!target_symbol_setter_functioncalls.empty());
2724 *(target_symbol_setter_functioncalls.top()) = fcall;
2725 }
2726
2727
2728 bool
2729 var_expanding_visitor::rewrite_lvalue(const token* tok, const std::string& eop,
2730 expression*& lvalue, expression*& rvalue)
2731 {
2732 // Our job would normally be to require() the left and right sides
2733 // into a new assignment. What we're doing is slightly trickier:
2734 // we're pushing a functioncall** onto a stack, and if our left
2735 // child sets the functioncall* for that value, we're going to
2736 // assume our left child was a target symbol -- transformed into a
2737 // set_target_foo(value) call, and it wants to take our right child
2738 // as the argument "value".
2739 //
2740 // This is why some people claim that languages with
2741 // constructor-decomposing case expressions have a leg up on
2742 // visitors.
2743
2744 functioncall *fcall = NULL;
2745
2746 // Let visit_target_symbol know what operator it should handle.
2747 const string* old_op = op;
2748 op = &eop;
2749
2750 target_symbol_setter_functioncalls.push (&fcall);
2751 replace (lvalue);
2752 target_symbol_setter_functioncalls.pop ();
2753 replace (rvalue);
2754
2755 op = old_op;
2756
2757 if (fcall != NULL)
2758 {
2759 // Our left child is informing us that it was a target variable
2760 // and it has been replaced with a set_target_foo() function
2761 // call; we are going to provide that function call -- with the
2762 // right child spliced in as sole argument -- in place of
2763 // ourselves, in the var expansion we're in the middle of making.
2764
2765 if (valid_ops.find (eop) == valid_ops.end ())
2766 {
2767 // Build up a list of supported operators.
2768 string ops;
2769 std::set<string>::iterator i;
2770 int valid_ops_size = 0;
2771 for (i = valid_ops.begin(); i != valid_ops.end(); i++)
2772 {
2773 ops += " " + *i + ",";
2774 valid_ops_size++;
2775 }
2776 ops.resize(ops.size() - 1); // chop off the last ','
2777
2778 // Throw the error.
2779 throw SEMANTIC_ERROR (_NF("Only the following assign operator is implemented on target variables: %s",
2780 "Only the following assign operators are implemented on target variables: %s",
2781 valid_ops_size, ops.c_str()), tok);
2782
2783 }
2784
2785 assert (lvalue == fcall);
2786 if (rvalue)
2787 fcall->args.push_back (rvalue);
2788 provide (fcall);
2789 return true;
2790 }
2791 else
2792 return false;
2793 }
2794
2795
2796 void
2797 var_expanding_visitor::visit_assignment (assignment* e)
2798 {
2799 if (!rewrite_lvalue (e->tok, e->op, e->left, e->right))
2800 provide (e);
2801 }
2802
2803
2804 void
2805 var_expanding_visitor::visit_pre_crement (pre_crement* e)
2806 {
2807 expression *dummy = NULL;
2808 if (!rewrite_lvalue (e->tok, e->op, e->operand, dummy))
2809 provide (e);
2810 }
2811
2812
2813 void
2814 var_expanding_visitor::visit_post_crement (post_crement* e)
2815 {
2816 expression *dummy = NULL;
2817 if (!rewrite_lvalue (e->tok, e->op, e->operand, dummy))
2818 provide (e);
2819 }
2820
2821
2822 void
2823 var_expanding_visitor::visit_delete_statement (delete_statement* s)
2824 {
2825 string fakeop = "delete";
2826 expression *dummy = NULL;
2827 if (!rewrite_lvalue (s->tok, fakeop, s->value, dummy))
2828 provide (s);
2829 }
2830
2831
2832 void
2833 var_expanding_visitor::visit_defined_op (defined_op* e)
2834 {
2835 bool resolved = true;
2836
2837 defined_ops.push (e);
2838 try {
2839 replace (e->operand);
2840
2841 // NB: Formerly, we had some curious cases to consider here, depending on what
2842 // various visit_target_symbol() implementations do for successful or
2843 // erroneous resolutions. Some would signal a visit_target_symbol failure
2844 // with an exception, with a set flag within the target_symbol, or nothing
2845 // at all.
2846 //
2847 // Now, failures always have to be signalled with a
2848 // saved_conversion_error being chained to the target_symbol.
2849 // Successes have to result in an attempted rewrite of the
2850 // target_symbol (via provide()).
2851 //
2852 // Edna Mode: "no capes". fche: "no exceptions".
2853
2854 // dwarf stuff: success: rewrites to a function; failure: retains target_symbol, sets saved_conversion_error
2855 //
2856 // sdt-kprobes sdt.h: success: string or functioncall; failure: semantic_error
2857 //
2858 // sdt-uprobes: success: string or no op; failure: no op; expect derived/synthetic
2859 // dwarf probe to take care of it.
2860 // But this is rather unhelpful. So we rig the sdt_var_expanding_visitor
2861 // to pass through @defined() to the synthetic dwarf probe.
2862 //
2863 // utrace: success: rewrites to function; failure: semantic_error
2864 //
2865 // procfs: success: rewrites to function; failure: semantic_error
2866
2867 target_symbol* tsym = dynamic_cast<target_symbol*> (e->operand);
2868 if (tsym && tsym->saved_conversion_error) // failing
2869 resolved = false;
2870 else if (tsym) // unresolved but not marked failing
2871 {
2872 // There are some visitors that won't touch certain target_symbols,
2873 // e.g. dwarf_var_expanding_visitor won't resolve @cast. We should
2874 // leave it for now so some other visitor can have a chance.
2875 provide (e);
2876 return;
2877 }
2878 else // resolved, rewritten to some other expression type
2879 resolved = true;
2880 } catch (const semantic_error& e) {
2881 assert (0); // should not happen
2882 }
2883 defined_ops.pop ();
2884
2885 literal_number* ln = new literal_number (resolved ? 1 : 0);
2886 ln->tok = e->tok;
2887 provide (ln);
2888 }
2889
2890
2891 struct dwarf_pretty_print
2892 {
2893 dwarf_pretty_print (dwflpp& dw, vector<Dwarf_Die>& scopes, Dwarf_Addr pc,
2894 const string& local, bool userspace_p,
2895 const target_symbol& e):
2896 dw(dw), local(local), scopes(scopes), pc(pc), pointer(NULL),
2897 userspace_p(userspace_p), deref_p(true)
2898 {
2899 init_ts (e);
2900 dw.type_die_for_local (scopes, pc, local, ts, &base_type);
2901 }
2902
2903 dwarf_pretty_print (dwflpp& dw, Dwarf_Die *scope_die, Dwarf_Addr pc,
2904 bool userspace_p, const target_symbol& e):
2905 dw(dw), scopes(1, *scope_die), pc(pc), pointer(NULL),
2906 userspace_p(userspace_p), deref_p(true)
2907 {
2908 init_ts (e);
2909 dw.type_die_for_return (&scopes[0], pc, ts, &base_type);
2910 }
2911
2912 dwarf_pretty_print (dwflpp& dw, Dwarf_Die *type_die, expression* pointer,
2913 bool deref_p, bool userspace_p, const target_symbol& e):
2914 dw(dw), pc(0), pointer(pointer), pointer_type(*type_die),
2915 userspace_p(userspace_p), deref_p(deref_p)
2916 {
2917 init_ts (e);
2918 dw.type_die_for_pointer (type_die, ts, &base_type);
2919 }
2920
2921 functioncall* expand ();
2922 ~dwarf_pretty_print () { delete ts; }
2923
2924 private:
2925 dwflpp& dw;
2926 target_symbol* ts;
2927 bool print_full;
2928 Dwarf_Die base_type;
2929
2930 string local;
2931 vector<Dwarf_Die> scopes;
2932 Dwarf_Addr pc;
2933
2934 expression* pointer;
2935 Dwarf_Die pointer_type;
2936
2937 const bool userspace_p, deref_p;
2938
2939 void recurse (Dwarf_Die* type, target_symbol* e,
2940 print_format* pf, bool top=false);
2941 void recurse_bitfield (Dwarf_Die* type, target_symbol* e,
2942 print_format* pf);
2943 void recurse_base (Dwarf_Die* type, target_symbol* e,
2944 print_format* pf);
2945 void recurse_array (Dwarf_Die* type, target_symbol* e,
2946 print_format* pf, bool top);
2947 void recurse_pointer (Dwarf_Die* type, target_symbol* e,
2948 print_format* pf, bool top);
2949 void recurse_struct (Dwarf_Die* type, target_symbol* e,
2950 print_format* pf, bool top);
2951 void recurse_struct_members (Dwarf_Die* type, target_symbol* e,
2952 print_format* pf, int& count);
2953 bool print_chars (Dwarf_Die* type, target_symbol* e, print_format* pf);
2954
2955 void init_ts (const target_symbol& e);
2956 expression* deref (target_symbol* e);
2957 bool push_deref (print_format* pf, const string& fmt, target_symbol* e);
2958 };
2959
2960
2961 void
2962 dwarf_pretty_print::init_ts (const target_symbol& e)
2963 {
2964 // Work with a new target_symbol so we can modify arguments
2965 ts = new target_symbol (e);
2966
2967 if (ts->addressof)
2968 throw SEMANTIC_ERROR(_("cannot take address of pretty-printed variable"), ts->tok);
2969
2970 size_t depth = ts->pretty_print_depth ();
2971 if (depth == 0)
2972 throw SEMANTIC_ERROR(_("invalid target_symbol for pretty-print"), ts->tok);
2973 print_full = depth > 1;
2974 ts->components.pop_back();
2975 }
2976
2977
2978 functioncall*
2979 dwarf_pretty_print::expand ()
2980 {
2981 static unsigned tick = 0;
2982
2983 // function pretty_print_X([pointer], [arg1, arg2, ...]) {
2984 // try {
2985 // return sprintf("{.foo=...}", (ts)->foo, ...)
2986 // } catch {
2987 // return "ERROR"
2988 // }
2989 // }
2990
2991 // Create the function decl and call.
2992
2993 functiondecl *fdecl = new functiondecl;
2994 fdecl->tok = ts->tok;
2995 fdecl->synthetic = true;
2996 fdecl->name = "_dwarf_pretty_print_" + lex_cast(tick++);
2997 fdecl->type = pe_string;
2998
2999 functioncall* fcall = new functioncall;
3000 fcall->referent = fdecl;
3001 fcall->tok = ts->tok;
3002 fcall->function = fdecl->name;
3003 fcall->type = pe_string;
3004
3005 // If there's a <pointer>, replace it with a new var and make that
3006 // the first function argument.
3007 if (pointer)
3008 {
3009 vardecl *v = new vardecl;
3010 v->type = pe_long;
3011 v->name = "pointer";
3012 v->tok = ts->tok;
3013 fdecl->formal_args.push_back (v);
3014 fcall->args.push_back (pointer);
3015
3016 symbol* sym = new symbol;
3017 sym->tok = ts->tok;
3018 sym->name = v->name;
3019 pointer = sym;
3020 }
3021
3022 // For each expression argument, replace it with a function argument.
3023 for (unsigned i = 0; i < ts->components.size(); ++i)
3024 if (ts->components[i].type == target_symbol::comp_expression_array_index)
3025 {
3026 vardecl *v = new vardecl;
3027 v->type = pe_long;
3028 v->name = "index" + lex_cast(i);
3029 v->tok = ts->tok;
3030 fdecl->formal_args.push_back (v);
3031 fcall->args.push_back (ts->components[i].expr_index);
3032
3033 symbol* sym = new symbol;
3034 sym->tok = ts->tok;
3035 sym->name = v->name;
3036 ts->components[i].expr_index = sym;
3037 }
3038
3039 // Create the return sprintf.
3040 print_format* pf = print_format::create(ts->tok, "sprintf");
3041 return_statement* rs = new return_statement;
3042 rs->tok = ts->tok;
3043 rs->value = pf;
3044
3045 // Recurse into the actual values.
3046 recurse (&base_type, ts, pf, true);
3047 pf->components = print_format::string_to_components(pf->raw_components);
3048
3049 // Create the try-catch net
3050 try_block* tb = new try_block;
3051 tb->tok = ts->tok;
3052 tb->try_block = rs;
3053 tb->catch_error_var = 0;
3054 return_statement* rs2 = new return_statement;
3055 rs2->tok = ts->tok;
3056 rs2->value = new literal_string ("ERROR");
3057 rs2->value->tok = ts->tok;
3058 tb->catch_block = rs2;
3059 fdecl->body = tb;
3060
3061 fdecl->join (dw.sess);
3062 return fcall;
3063 }
3064
3065
3066 void
3067 dwarf_pretty_print::recurse (Dwarf_Die* start_type, target_symbol* e,
3068 print_format* pf, bool top)
3069 {
3070 // deal with initial void* pointers
3071 if (!deref_p && null_die(start_type))
3072 {
3073 push_deref (pf, "%p", e);
3074 return;
3075 }
3076
3077 Dwarf_Die type;
3078 dw.resolve_unqualified_inner_typedie (start_type, &type, e);
3079
3080 switch (dwarf_tag(&type))
3081 {
3082 default:
3083 // XXX need a warning?
3084 // throw semantic_error ("unsupported type (tag " + lex_cast(dwarf_tag(&type))
3085 // + ") for " + dwarf_type_name(&type), e->tok);
3086 pf->raw_components.append("?");
3087 break;
3088
3089 case DW_TAG_enumeration_type:
3090 case DW_TAG_base_type:
3091 recurse_base (&type, e, pf);
3092 break;
3093
3094 case DW_TAG_array_type:
3095 recurse_array (&type, e, pf, top);
3096 break;
3097
3098 case DW_TAG_pointer_type:
3099 case DW_TAG_reference_type:
3100 case DW_TAG_rvalue_reference_type:
3101 recurse_pointer (&type, e, pf, top);
3102 break;
3103
3104 case DW_TAG_subroutine_type:
3105 push_deref (pf, "<function>:%p", e);
3106 break;
3107
3108 case DW_TAG_union_type:
3109 case DW_TAG_structure_type:
3110 case DW_TAG_class_type:
3111 recurse_struct (&type, e, pf, top);
3112 break;
3113 }
3114 }
3115
3116
3117 // Bit fields are handled as a special-case combination of recurse() and
3118 // recurse_base(), only called from recurse_struct_members(). The main
3119 // difference is that the value is always printed numerically, even if the
3120 // underlying type is a char.
3121 void
3122 dwarf_pretty_print::recurse_bitfield (Dwarf_Die* start_type, target_symbol* e,
3123 print_format* pf)
3124 {
3125 Dwarf_Die type;
3126 dw.resolve_unqualified_inner_typedie (start_type, &type, e);
3127
3128 int tag = dwarf_tag(&type);
3129 if (tag != DW_TAG_base_type && tag != DW_TAG_enumeration_type)
3130 {
3131 // XXX need a warning?
3132 // throw semantic_error ("unsupported bitfield type (tag " + lex_cast(tag)
3133 // + ") for " + dwarf_type_name(&type), e->tok);
3134 pf->raw_components.append("?");
3135 return;
3136 }
3137
3138 Dwarf_Attribute attr;
3139 Dwarf_Word encoding = (Dwarf_Word) -1;
3140 dwarf_formudata (dwarf_attr_integrate (&type, DW_AT_encoding, &attr),
3141 &encoding);
3142 switch (encoding)
3143 {
3144 case DW_ATE_float:
3145 case DW_ATE_complex_float:
3146 // XXX need a warning?
3147 // throw semantic_error ("unsupported bitfield type (encoding " + lex_cast(encoding)
3148 // + ") for " + dwarf_type_name(&type), e->tok);
3149 pf->raw_components.append("?");
3150 break;
3151
3152 case DW_ATE_unsigned:
3153 case DW_ATE_unsigned_char:
3154 push_deref (pf, "%u", e);
3155 break;
3156
3157 case DW_ATE_signed:
3158 case DW_ATE_signed_char:
3159 default:
3160 push_deref (pf, "%i", e);
3161 break;
3162 }
3163 }
3164
3165
3166 void
3167 dwarf_pretty_print::recurse_base (Dwarf_Die* type, target_symbol* e,
3168 print_format* pf)
3169 {
3170 Dwarf_Attribute attr;
3171 Dwarf_Word encoding = (Dwarf_Word) -1;
3172 dwarf_formudata (dwarf_attr_integrate (type, DW_AT_encoding, &attr),
3173 &encoding);
3174 switch (encoding)
3175 {
3176 case DW_ATE_float:
3177 case DW_ATE_complex_float:
3178 // XXX need a warning?
3179 // throw semantic_error ("unsupported type (encoding " + lex_cast(encoding)
3180 // + ") for " + dwarf_type_name(type), e->tok);
3181 pf->raw_components.append("?");
3182 break;
3183
3184 case DW_ATE_UTF: // XXX need to add unicode to _stp_vsprint_char
3185 case DW_ATE_signed_char:
3186 case DW_ATE_unsigned_char:
3187 // Use escapes to make sure that non-printable characters
3188 // don't interrupt our stream (especially '\0' values).
3189 push_deref (pf, "'%#c'", e);
3190 break;
3191
3192 case DW_ATE_unsigned:
3193 push_deref (pf, "%u", e);
3194 break;
3195
3196 case DW_ATE_signed:
3197 default:
3198 push_deref (pf, "%i", e);
3199 break;
3200 }
3201 }
3202
3203
3204 void
3205 dwarf_pretty_print::recurse_array (Dwarf_Die* type, target_symbol* e,
3206 print_format* pf, bool top)
3207 {
3208 if (!top && !print_full)
3209 {
3210 pf->raw_components.append("[...]");
3211 return;
3212 }
3213
3214 Dwarf_Die childtype;
3215 dwarf_attr_die (type, DW_AT_type, &childtype);
3216
3217 if (print_chars (&childtype, e, pf))
3218 return;
3219
3220 pf->raw_components.append("[");
3221
3222 // We print the array up to the first 5 elements.
3223 // XXX how can we determine the array size?
3224 // ... for now, just print the first element
3225 // NB: limit to 32 args; see PR10750 and c_unparser::visit_print_format.
3226 unsigned i, size = 1;
3227 for (i=0; i < size && i < 5 && pf->args.size() < 32; ++i)
3228 {
3229 if (i > 0)
3230 pf->raw_components.append(", ");
3231 target_symbol* e2 = new target_symbol(*e);
3232 e2->components.push_back (target_symbol::component(e->tok, i));
3233 recurse (&childtype, e2, pf);
3234 }
3235 if (i < size || 1/*XXX until real size is known */)
3236 pf->raw_components.append(", ...");
3237 pf->raw_components.append("]");
3238 }
3239
3240
3241 void
3242 dwarf_pretty_print::recurse_pointer (Dwarf_Die* type, target_symbol* e,
3243 print_format* pf, bool top)
3244 {
3245 // We chase to top-level pointers, but leave the rest alone
3246 bool void_p = true;
3247 Dwarf_Die pointee;
3248 if (dwarf_attr_die (type, DW_AT_type, &pointee))
3249 {
3250 try
3251 {
3252 dw.resolve_unqualified_inner_typedie (&pointee, &pointee, e);
3253 void_p = false;
3254 }
3255 catch (const semantic_error&) {}
3256 }
3257
3258 if (!void_p)
3259 {
3260 if (print_chars (&pointee, e, pf))
3261 return;
3262
3263 if (top)
3264 {
3265 recurse (&pointee, e, pf, top);
3266 return;
3267 }
3268 }
3269
3270 push_deref (pf, "%p", e);
3271 }
3272
3273
3274 void
3275 dwarf_pretty_print::recurse_struct (Dwarf_Die* type, target_symbol* e,
3276 print_format* pf, bool top)
3277 {
3278 if (dwarf_hasattr(type, DW_AT_declaration))
3279 {
3280 Dwarf_Die *resolved = dw.declaration_resolve(type);
3281 if (!resolved)
3282 {
3283 // could be an error, but for now just stub it
3284 // throw semantic_error ("unresolved " + dwarf_type_name(type), e->tok);
3285 pf->raw_components.append("{...}");
3286 return;
3287 }
3288 type = resolved;
3289 }
3290
3291 int count = 0;
3292 pf->raw_components.append("{");
3293 if (top || print_full)
3294 recurse_struct_members (type, e, pf, count);
3295 else
3296 pf->raw_components.append("...");
3297 pf->raw_components.append("}");
3298 }
3299
3300
3301 void
3302 dwarf_pretty_print::recurse_struct_members (Dwarf_Die* type, target_symbol* e,
3303 print_format* pf, int& count)
3304 {
3305 /* With inheritance, a subclass may mask member names of parent classes, so
3306 * our search among the inheritance tree must be breadth-first rather than
3307 * depth-first (recursive). The type die is still our starting point. When
3308 * we encounter a masked name, just skip it. */
3309 set<string> dupes;
3310 deque<Dwarf_Die> inheritees(1, *type);
3311 for (; !inheritees.empty(); inheritees.pop_front())
3312 {
3313 Dwarf_Die child, childtype, import;
3314 if (dwarf_child (&inheritees.front(), &child) == 0)
3315 do
3316 {
3317 target_symbol* e2 = e;
3318
3319 // skip static members
3320 if (dwarf_hasattr(&child, DW_AT_declaration))
3321 continue;
3322
3323 int tag = dwarf_tag (&child);
3324
3325 /* Pretend imported units contain members by recursing into
3326 struct_member printing with the same count. */
3327 if (tag == DW_TAG_imported_unit
3328 && dwarf_attr_die (&child, DW_AT_import, &import))
3329 recurse_struct_members (&import, e2, pf, count);
3330
3331 if (tag != DW_TAG_member && tag != DW_TAG_inheritance)
3332 continue;
3333
3334 dwarf_attr_die (&child, DW_AT_type, &childtype);
3335
3336 if (tag == DW_TAG_inheritance)
3337 {
3338 inheritees.push_back(childtype);
3339 continue;
3340 }
3341
3342 int childtag = dwarf_tag (&childtype);
3343 const char *member = dwarf_diename (&child);
3344
3345 // "_vptr.foo" members are C++ virtual function tables,
3346 // which (generally?) aren't interesting for users.
3347 if (member && startswith(member, "_vptr."))
3348 continue;
3349
3350 // skip inheritance-masked duplicates
3351 if (member && !dupes.insert(member).second)
3352 continue;
3353
3354 if (++count > 1)
3355 pf->raw_components.append(", ");
3356
3357 // NB: limit to 32 args; see PR10750 and c_unparser::visit_print_format.
3358 if (pf->args.size() >= 32)
3359 {
3360 pf->raw_components.append("...");
3361 break;
3362 }
3363
3364 if (member)
3365 {
3366 pf->raw_components.append(".");
3367 pf->raw_components.append(member);
3368
3369 e2 = new target_symbol(*e);
3370 e2->components.push_back (target_symbol::component(e->tok, member));
3371 }
3372 else if (childtag == DW_TAG_union_type)
3373 pf->raw_components.append("<union>");
3374 else if (childtag == DW_TAG_structure_type)
3375 pf->raw_components.append("<class>");
3376 else if (childtag == DW_TAG_class_type)
3377 pf->raw_components.append("<struct>");
3378 pf->raw_components.append("=");
3379
3380 if (dwarf_hasattr_integrate (&child, DW_AT_bit_offset))
3381 recurse_bitfield (&childtype, e2, pf);
3382 else
3383 recurse (&childtype, e2, pf);
3384 }
3385 while (dwarf_siblingof (&child, &child) == 0);
3386 }
3387 }
3388
3389
3390 bool
3391 dwarf_pretty_print::print_chars (Dwarf_Die* start_type, target_symbol* e,
3392 print_format* pf)
3393 {
3394 Dwarf_Die type;
3395 dw.resolve_unqualified_inner_typedie (start_type, &type, e);
3396
3397 Dwarf_Attribute attr;
3398 Dwarf_Word encoding = (Dwarf_Word) -1;
3399 dwarf_formudata (dwarf_attr_integrate (&type, DW_AT_encoding, &attr),
3400 &encoding);
3401 switch (encoding)
3402 {
3403 case DW_ATE_UTF:
3404 case DW_ATE_signed_char:
3405 case DW_ATE_unsigned_char:
3406 break;
3407 default:
3408 return false;
3409 }
3410
3411 string function = userspace_p ? "user_string2" : "kernel_string2";
3412 Dwarf_Word size = (Dwarf_Word) -1;
3413 dwarf_formudata (dwarf_attr_integrate (&type, DW_AT_byte_size, &attr), &size);
3414 switch (size)
3415 {
3416 case 1:
3417 break;
3418 case 2:
3419 function += "_utf16";
3420 break;
3421 case 4:
3422 function += "_utf32";
3423 break;
3424 default:
3425 return false;
3426 }
3427
3428 if (push_deref (pf, "\"%s\"", e))
3429 {
3430 // steal the last arg for a string access
3431 assert (!pf->args.empty());
3432 functioncall* fcall = new functioncall;
3433 fcall->tok = e->tok;
3434 fcall->function = function;
3435 fcall->args.push_back (pf->args.back());
3436 expression *err_msg = new literal_string ("<unknown>");
3437 err_msg->tok = e->tok;
3438 fcall->args.push_back (err_msg);
3439 pf->args.back() = fcall;
3440 }
3441 return true;
3442 }
3443
3444 // PR10601: adapt to kernel-vs-userspace loc2c-runtime
3445 static const string EMBEDDED_FETCH_DEREF_KERNEL = string("\n")
3446 + "#define fetch_register k_fetch_register\n"
3447 + "#define store_register k_store_register\n"
3448 + "#define deref kderef\n"
3449 + "#define store_deref store_kderef\n";
3450
3451 static const string EMBEDDED_FETCH_DEREF_USER = string("\n")
3452 + "#define fetch_register u_fetch_register\n"
3453 + "#define store_register u_store_register\n"
3454 + "#define deref uderef\n"
3455 + "#define store_deref store_uderef\n";
3456
3457 #define EMBEDDED_FETCH_DEREF(U) \
3458 (U ? EMBEDDED_FETCH_DEREF_USER : EMBEDDED_FETCH_DEREF_KERNEL)
3459
3460 static const string EMBEDDED_FETCH_DEREF_DONE = string("\n")
3461 + "#undef fetch_register\n"
3462 + "#undef store_register\n"
3463 + "#undef deref\n"
3464 + "#undef store_deref\n";
3465
3466 static functioncall*
3467 synthetic_embedded_deref_call(dwflpp& dw,
3468 Dwarf_Die* function_type,
3469 const string& function_name,
3470 const string& function_code,
3471 bool userspace_p,
3472 bool lvalue_p,
3473 target_symbol* e,
3474 expression* pointer=NULL)
3475 {
3476 // Synthesize a functiondecl for the given embedded code string.
3477 functiondecl *fdecl = new functiondecl;
3478 fdecl->synthetic = true;
3479 fdecl->tok = e->tok;
3480 fdecl->name = function_name;
3481 // The fdecl type is generic, but we'll be detailed on the fcall below.
3482 fdecl->type = pe_long;
3483 fdecl->type_details.reset(new exp_type_dwarf(&dw, function_type,
3484 userspace_p, e->addressof));
3485
3486 embeddedcode *ec = new embeddedcode;
3487 ec->tok = e->tok;
3488 ec->code += "/* unprivileged */";
3489 if (! lvalue_p)
3490 ec->code += "/* pure */";
3491 ec->code += EMBEDDED_FETCH_DEREF(userspace_p);
3492 ec->code += function_code;
3493 ec->code += EMBEDDED_FETCH_DEREF_DONE;
3494 fdecl->body = ec;
3495
3496 // Synthesize a functioncall.
3497 functioncall* fcall = new functioncall;
3498 fcall->tok = e->tok;
3499 fcall->referent = fdecl;
3500 fcall->function = fdecl->name;
3501 fcall->type = fdecl->type;
3502 fcall->type_details = fdecl->type_details;
3503
3504 // If this code snippet uses a precomputed pointer,
3505 // pass that as the first argument.
3506 if (pointer)
3507 {
3508 vardecl *v = new vardecl;
3509 v->type = pe_long;
3510 v->name = "pointer";
3511 v->tok = e->tok;
3512 fdecl->formal_args.push_back(v);
3513 fcall->args.push_back(pointer);
3514 }
3515
3516 // Any non-literal indexes need to be passed as arguments too.
3517 for (unsigned i = 0; i < e->components.size(); ++i)
3518 if (e->components[i].type == target_symbol::comp_expression_array_index)
3519 {
3520 vardecl *v = new vardecl;
3521 v->type = pe_long;
3522 v->name = "index" + lex_cast(i);
3523 v->tok = e->tok;
3524 fdecl->formal_args.push_back(v);
3525 fcall->args.push_back(e->components[i].expr_index);
3526 }
3527
3528 // If this code snippet is assigning to an lvalue,
3529 // add a final argument for the rvalue.
3530 if (lvalue_p)
3531 {
3532 // Modify the fdecl so it carries a single pe_long formal
3533 // argument called "value".
3534
3535 // FIXME: For the time being we only support setting target
3536 // variables which have base types; these are 'pe_long' in
3537 // stap's type vocabulary. Strings and pointers might be
3538 // reasonable, some day, but not today.
3539
3540 vardecl *v = new vardecl;
3541 v->type = pe_long;
3542 v->name = "value";
3543 v->tok = e->tok;
3544 fdecl->formal_args.push_back(v);
3545 // NB: We don't know the value for fcall argument yet.
3546 // (see target_symbol_setter_functioncalls)
3547 }
3548
3549 // Add the synthesized decl to the session, and return the call.
3550 fdecl->join (dw.sess);
3551 return fcall;
3552 }
3553
3554 expression*
3555 dwarf_pretty_print::deref (target_symbol* e)
3556 {
3557 static unsigned tick = 0;
3558
3559 if (!deref_p)
3560 {
3561 assert (pointer && e->components.empty());
3562 return pointer;
3563 }
3564
3565 bool lvalue_p = false;
3566 string name = "_dwarf_pretty_print_deref_" + lex_cast(tick++);
3567
3568 string code;
3569 Dwarf_Die endtype;
3570 if (pointer)
3571 code = dw.literal_stmt_for_pointer (&pointer_type, e, false, &endtype);
3572 else if (!local.empty())
3573 code = dw.literal_stmt_for_local (scopes, pc, local, e, false, &endtype);
3574 else
3575 code = dw.literal_stmt_for_return (&scopes[0], pc, e, false, &endtype);
3576
3577 return synthetic_embedded_deref_call(dw, &endtype, name, code,
3578 userspace_p, lvalue_p, e, pointer);
3579 }
3580
3581
3582 bool
3583 dwarf_pretty_print::push_deref (print_format* pf, const string& fmt,
3584 target_symbol* e)
3585 {
3586 expression* e2 = NULL;
3587 try
3588 {
3589 e2 = deref (e);
3590 }
3591 catch (const semantic_error&)
3592 {
3593 pf->raw_components.append ("?");
3594 return false;
3595 }
3596 pf->raw_components.append (fmt);
3597 pf->args.push_back (e2);
3598 return true;
3599 }
3600
3601
3602 void
3603 dwarf_var_expanding_visitor::visit_target_symbol_saved_return (target_symbol* e)
3604 {
3605 // Get the full name of the target symbol.
3606 stringstream ts_name_stream;
3607 e->print(ts_name_stream);
3608 string ts_name = ts_name_stream.str();
3609
3610 // Check and make sure we haven't already seen this target
3611 // variable in this return probe. If we have, just return our
3612 // last replacement.
3613 map<string, expression *>::iterator i = return_ts_map.find(ts_name);
3614 if (i != return_ts_map.end())
3615 {
3616 provide (i->second);
3617 return;
3618 }
3619
3620 // Attempt the expansion directly first, so if there's a problem with the
3621 // variable we won't have a bogus entry probe lying around. Like in
3622 // saveargs(), we pretend for a moment that we're not in a .return.
3623 bool saved_has_return = q.has_return;
3624 q.has_return = false;
3625 expression *repl = e;
3626 replace (repl);
3627 q.has_return = saved_has_return;
3628 target_symbol* n = dynamic_cast<target_symbol*>(repl);
3629 if (n && n->saved_conversion_error)
3630 {
3631 provide (repl);
3632 return;
3633 }
3634
3635 expression *exp;
3636 if (!q.has_process &&
3637 strverscmp(q.sess.kernel_base_release.c_str(), "2.6.25") >= 0)
3638 exp = gen_kretprobe_saved_return(repl);
3639 else
3640 exp = gen_mapped_saved_return(repl, e->sym_name());
3641
3642 // Propagate the DWARF type to the expression in the return probe.
3643 if (repl->type_details && !exp->type_details)
3644 exp->type_details = repl->type_details;
3645
3646 // Provide the variable to our parent so it can be used as a
3647 // substitute for the target symbol.
3648 provide (exp);
3649
3650 // Remember this replacement since we might be able to reuse
3651 // it later if the same return probe references this target
3652 // symbol again.
3653 return_ts_map[ts_name] = exp;
3654 }
3655
3656 static expression*
3657 gen_mapped_saved_return(systemtap_session &sess, expression* e,
3658 const string& name,
3659 block *& add_block, bool& add_block_tid,
3660 block *& add_call_probe, bool& add_call_probe_tid)
3661 {
3662 static unsigned tick = 0;
3663
3664 // We've got to do several things here to handle target
3665 // variables in return probes.
3666
3667 // (1) Synthesize two global arrays. One is the cache of the
3668 // target variable and the other contains a thread specific
3669 // nesting level counter. The arrays will look like
3670 // this:
3671 //
3672 // _entry_tvar_{name}_{num}
3673 // _entry_tvar_{name}_{num}_ctr
3674
3675 string aname = (string("_entry_tvar_")
3676 + name
3677 + "_" + lex_cast(tick++));
3678 vardecl* vd = new vardecl;
3679 vd->name = aname;
3680 vd->tok = e->tok;
3681 sess.globals.push_back (vd);
3682
3683 string ctrname = aname + "_ctr";
3684 vd = new vardecl;
3685 vd->name = ctrname;
3686 vd->tok = e->tok;
3687 sess.globals.push_back (vd);
3688
3689 // (2) Create a new code block we're going to insert at the
3690 // beginning of this probe to get the cached value into a
3691 // temporary variable. We'll replace the target variable
3692 // reference with the temporary variable reference. The code
3693 // will look like this:
3694 //
3695 // _entry_tvar_tid = tid()
3696 // _entry_tvar_{name}_{num}_tmp
3697 // = _entry_tvar_{name}_{num}[_entry_tvar_tid,
3698 // _entry_tvar_{name}_{num}_ctr[_entry_tvar_tid]]
3699 // delete _entry_tvar_{name}_{num}[_entry_tvar_tid,
3700 // _entry_tvar_{name}_{num}_ctr[_entry_tvar_tid]--]
3701 // if (! _entry_tvar_{name}_{num}_ctr[_entry_tvar_tid])
3702 // delete _entry_tvar_{name}_{num}_ctr[_entry_tvar_tid]
3703
3704 // (2a) Synthesize the tid temporary expression, which will look
3705 // like this:
3706 //
3707 // _entry_tvar_tid = tid()
3708 symbol* tidsym = new symbol;
3709 tidsym->name = string("_entry_tvar_tid");
3710 tidsym->tok = e->tok;
3711
3712 if (add_block == NULL)
3713 {
3714 add_block = new block;
3715 add_block->tok = e->tok;
3716 }
3717
3718 if (!add_block_tid)
3719 {
3720 // Synthesize a functioncall to grab the thread id.
3721 functioncall* fc = new functioncall;
3722 fc->tok = e->tok;
3723 fc->function = string("tid");
3724
3725 // Assign the tid to '_entry_tvar_tid'.
3726 assignment* a = new assignment;
3727 a->tok = e->tok;
3728 a->op = "=";
3729 a->left = tidsym;
3730 a->right = fc;
3731
3732 expr_statement* es = new expr_statement;
3733 es->tok = e->tok;
3734 es->value = a;
3735 add_block->statements.push_back (es);
3736 add_block_tid = true;
3737 }
3738
3739 // (2b) Synthesize an array reference and assign it to a
3740 // temporary variable (that we'll use as replacement for the
3741 // target variable reference). It will look like this:
3742 //
3743 // _entry_tvar_{name}_{num}_tmp
3744 // = _entry_tvar_{name}_{num}[_entry_tvar_tid,
3745 // _entry_tvar_{name}_{num}_ctr[_entry_tvar_tid]]
3746
3747 arrayindex* ai_tvar_base = new arrayindex;
3748 ai_tvar_base->tok = e->tok;
3749
3750 symbol* sym = new symbol;
3751 sym->name = aname;
3752 sym->tok = e->tok;
3753 ai_tvar_base->base = sym;
3754
3755 ai_tvar_base->indexes.push_back(tidsym);
3756
3757 // We need to create a copy of the array index in its current
3758 // state so we can have 2 variants of it (the original and one
3759 // that post-decrements the second index).
3760 arrayindex* ai_tvar = new arrayindex;
3761 arrayindex* ai_tvar_postdec = new arrayindex;
3762 *ai_tvar = *ai_tvar_base;
3763 *ai_tvar_postdec = *ai_tvar_base;
3764
3765 // Synthesize the
3766 // "_entry_tvar_{name}_{num}_ctr[_entry_tvar_tid]" used as the
3767 // second index into the array.
3768 arrayindex* ai_ctr = new arrayindex;
3769 ai_ctr->tok = e->tok;
3770
3771 sym = new symbol;
3772 sym->name = ctrname;
3773 sym->tok = e->tok;
3774 ai_ctr->base = sym;
3775 ai_ctr->indexes.push_back(tidsym);
3776 ai_tvar->indexes.push_back(ai_ctr);
3777
3778 symbol* tmpsym = new symbol;
3779 tmpsym->name = aname + "_tmp";
3780 tmpsym->tok = e->tok;
3781
3782 assignment* a = new assignment;
3783 a->tok = e->tok;
3784 a->op = "=";
3785 a->left = tmpsym;
3786 a->right = ai_tvar;
3787
3788 expr_statement* es = new expr_statement;
3789 es->tok = e->tok;
3790 es->value = a;
3791
3792 add_block->statements.push_back (es);
3793
3794 // (2c) Add a post-decrement to the second array index and
3795 // delete the array value. It will look like this:
3796 //
3797 // delete _entry_tvar_{name}_{num}[_entry_tvar_tid,
3798 // _entry_tvar_{name}_{num}_ctr[_entry_tvar_tid]--]
3799
3800 post_crement* pc = new post_crement;
3801 pc->tok = e->tok;
3802 pc->op = "--";
3803 pc->operand = ai_ctr;
3804 ai_tvar_postdec->indexes.push_back(pc);
3805
3806 delete_statement* ds = new delete_statement;
3807 ds->tok = e->tok;
3808 ds->value = ai_tvar_postdec;
3809
3810 add_block->statements.push_back (ds);
3811
3812 // (2d) Delete the counter value if it is 0. It will look like
3813 // this:
3814 // if (! _entry_tvar_{name}_{num}_ctr[_entry_tvar_tid])
3815 // delete _entry_tvar_{name}_{num}_ctr[_entry_tvar_tid]
3816
3817 ds = new delete_statement;
3818 ds->tok = e->tok;
3819 ds->value = ai_ctr;
3820
3821 unary_expression *ue = new unary_expression;
3822 ue->tok = e->tok;
3823 ue->op = "!";
3824 ue->operand = ai_ctr;
3825
3826 if_statement *ifs = new if_statement;
3827 ifs->tok = e->tok;
3828 ifs->condition = ue;
3829 ifs->thenblock = ds;
3830 ifs->elseblock = NULL;
3831
3832 add_block->statements.push_back (ifs);
3833
3834 // (3) We need an entry probe that saves the value for us in the
3835 // global array we created. Create the entry probe, which will
3836 // look like this:
3837 //
3838 // probe kernel.function("{function}").call {
3839 // _entry_tvar_tid = tid()
3840 // _entry_tvar_{name}_{num}[_entry_tvar_tid,
3841 // ++_entry_tvar_{name}_{num}_ctr[_entry_tvar_tid]]
3842 // = ${param}
3843 // }
3844
3845 if (add_call_probe == NULL)
3846 {
3847 add_call_probe = new block;
3848 add_call_probe->tok = e->tok;
3849 }
3850
3851 if (!add_call_probe_tid)
3852 {
3853 // Synthesize a functioncall to grab the thread id.
3854 functioncall* fc = new functioncall;
3855 fc->tok = e->tok;
3856 fc->function = string("tid");
3857
3858 // Assign the tid to '_entry_tvar_tid'.
3859 assignment* a = new assignment;
3860 a->tok = e->tok;
3861 a->op = "=";
3862 a->left = tidsym;
3863 a->right = fc;
3864
3865 expr_statement* es = new expr_statement;
3866 es->tok = e->tok;
3867 es->value = a;
3868 add_call_probe = new block(add_call_probe, es);
3869 add_call_probe_tid = true;
3870 }
3871
3872 // Save the value, like this:
3873 // _entry_tvar_{name}_{num}[_entry_tvar_tid,
3874 // ++_entry_tvar_{name}_{num}_ctr[_entry_tvar_tid]]
3875 // = ${param}
3876 arrayindex* ai_tvar_preinc = new arrayindex;
3877 *ai_tvar_preinc = *ai_tvar_base;
3878
3879 pre_crement* preinc = new pre_crement;
3880 preinc->tok = e->tok;
3881 preinc->op = "++";
3882 preinc->operand = ai_ctr;
3883 ai_tvar_preinc->indexes.push_back(preinc);
3884
3885 a = new assignment;
3886 a->tok = e->tok;
3887 a->op = "=";
3888 a->left = ai_tvar_preinc;
3889 a->right = e;
3890
3891 es = new expr_statement;
3892 es->tok = e->tok;
3893 es->value = a;
3894
3895 add_call_probe = new block(add_call_probe, es);
3896
3897 // (4) Provide the '_entry_tvar_{name}_{num}_tmp' variable to
3898 // our parent so it can be used as a substitute for the target
3899 // symbol.
3900 delete ai_tvar_base;
3901 return tmpsym;
3902 }
3903
3904
3905 expression*
3906 dwarf_var_expanding_visitor::gen_mapped_saved_return(expression* e,
3907 const string& name)
3908 {
3909 return ::gen_mapped_saved_return(q.sess, e, name, add_block,
3910 add_block_tid, add_call_probe,
3911 add_call_probe_tid);
3912 }
3913
3914
3915 expression*
3916 dwarf_var_expanding_visitor::gen_kretprobe_saved_return(expression* e)
3917 {
3918 // The code for this is simple.
3919 //
3920 // .call:
3921 // _set_kretprobe_long(index, $value)
3922 //
3923 // .return:
3924 // _get_kretprobe_long(index)
3925 //
3926 // (or s/long/string/ for things like $$parms)
3927
3928 unsigned index;
3929 string setfn, getfn;
3930
3931 // We need the caller to predetermine the type of the expression!
3932 switch (e->type)
3933 {
3934 case pe_string:
3935 index = saved_strings++;
3936 setfn = "_set_kretprobe_string";
3937 getfn = "_get_kretprobe_string";
3938 break;
3939 case pe_long:
3940 index = saved_longs++;
3941 setfn = "_set_kretprobe_long";
3942 getfn = "_get_kretprobe_long";
3943 break;
3944 default:
3945 throw SEMANTIC_ERROR(_("unknown type to save in kretprobe"), e->tok);
3946 }
3947
3948 // Create the entry code
3949 // _set_kretprobe_{long|string}(index, $value)
3950
3951 if (add_call_probe == NULL)
3952 {
3953 add_call_probe = new block;
3954 add_call_probe->tok = e->tok;
3955 }
3956
3957 functioncall* set_fc = new functioncall;
3958 set_fc->tok = e->tok;
3959 set_fc->function = setfn;
3960 set_fc->args.push_back(new literal_number(index));
3961 set_fc->args.back()->tok = e->tok;
3962 set_fc->args.push_back(e);
3963
3964 expr_statement* set_es = new expr_statement;
3965 set_es->tok = e->tok;
3966 set_es->value = set_fc;
3967
3968 add_call_probe->statements.push_back(set_es);
3969
3970 // Create the return code
3971 // _get_kretprobe_{long|string}(index)
3972
3973 functioncall* get_fc = new functioncall;
3974 get_fc->tok = e->tok;
3975 get_fc->function = getfn;
3976 get_fc->args.push_back(new literal_number(index));
3977 get_fc->args.back()->tok = e->tok;
3978
3979 return get_fc;
3980 }
3981
3982
3983 void
3984 dwarf_var_expanding_visitor::visit_target_symbol_context (target_symbol* e)
3985 {
3986 if (null_die(scope_die)) {
3987 literal_string *empty = new literal_string("");
3988 empty->tok = e->tok;
3989 provide(empty);
3990 return;
3991 }
3992
3993 target_symbol *tsym = new target_symbol(*e);
3994
3995 bool pretty = e->check_pretty_print ();
3996 string format = pretty ? "=%s" : "=%#x";
3997
3998 // Convert $$parms to sprintf of a list of parms and active local vars
3999 // which we recursively evaluate
4000
4001 print_format* pf = print_format::create(e->tok, "sprintf");
4002
4003 if (q.has_return && (e->name == "$$return"))
4004 {
4005 tsym->name = "$return";
4006
4007 // Ignore any variable that isn't accessible.
4008 tsym->saved_conversion_error = 0;
4009 expression *texp = tsym;
4010 replace (texp); // NB: throws nothing ...
4011 if (tsym->saved_conversion_error) // ... but this is how we know it happened.
4012 {
4013
4014 }
4015 else
4016 {
4017 pf->raw_components += "return";
4018 pf->raw_components += format;
4019 pf->args.push_back(texp);
4020 }
4021 }
4022 else
4023 {
4024 // non-.return probe: support $$parms, $$vars, $$locals
4025 bool first = true;
4026 Dwarf_Die result;
4027 vector<Dwarf_Die> scopes = q.dw.getscopes(scope_die);
4028 for (unsigned i = 0; i < scopes.size(); ++i)
4029 {
4030 if (dwarf_tag(&scopes[i]) == DW_TAG_compile_unit)
4031 break; // we don't want file-level variables
4032 if (dwarf_child (&scopes[i], &result) == 0)
4033 do
4034 {
4035 switch (dwarf_tag (&result))
4036 {
4037 case DW_TAG_variable:
4038 if (e->name == "$$parms")
4039 continue;
4040 break;
4041 case DW_TAG_formal_parameter:
4042 if (e->name == "$$locals")
4043 continue;
4044 break;
4045
4046 default:
4047 continue;
4048 }
4049
4050 const char *diename = dwarf_diename (&result);
4051 if (! diename) continue;
4052
4053 if (! first)
4054 pf->raw_components += " ";
4055 pf->raw_components += diename;
4056 first = false;
4057
4058 // Write a placeholder for ugly aggregates
4059 Dwarf_Die type;
4060 if (!pretty && dwarf_attr_die(&result, DW_AT_type, &type))
4061 {
4062 q.dw.resolve_unqualified_inner_typedie(&type, &type, e);
4063 switch (dwarf_tag(&type))
4064 {
4065 case DW_TAG_union_type:
4066 case DW_TAG_structure_type:
4067 case DW_TAG_class_type:
4068 pf->raw_components += "={...}";
4069 continue;
4070
4071 case DW_TAG_array_type:
4072 pf->raw_components += "=[...]";
4073 continue;
4074 }
4075 }
4076
4077 tsym->name = "$";
4078 tsym->name += diename;
4079
4080 // Ignore any variable that isn't accessible.
4081 tsym->saved_conversion_error = 0;
4082 expression *texp = tsym;
4083 replace (texp); // NB: throws nothing ...
4084 if (tsym->saved_conversion_error) // ... but this is how we know it happened.
4085 {
4086 if (q.sess.verbose>2)
4087 {
4088 for (const semantic_error *c = tsym->saved_conversion_error;
4089 c != 0;
4090 c = c->get_chain()) {
4091 clog << _("variable location problem [man error::dwarf]: ") << c->what() << endl;
4092 }
4093 }
4094
4095 pf->raw_components += "=?";
4096 }
4097 else
4098 {
4099 pf->raw_components += format;
4100 pf->args.push_back(texp);
4101 }
4102 }
4103 while (dwarf_siblingof (&result, &result) == 0);
4104 }
4105 }
4106
4107 pf->components = print_format::string_to_components(pf->raw_components);
4108 pf->type = pe_string;
4109 provide (pf);
4110 }
4111
4112
4113 void
4114 dwarf_var_expanding_visitor::visit_atvar_op (atvar_op *e)
4115 {
4116 // Fill in our current module context if needed
4117 if (e->module.empty())
4118 e->module = q.dw.module_name;
4119
4120 if (e->module == q.dw.module_name && e->cu_name.empty())
4121 {
4122 // process like any other local
4123 // e->sym_name() will do the right thing
4124 visit_target_symbol(e);
4125 return;
4126 }
4127
4128 var_expanding_visitor::visit_atvar_op(e);
4129 }
4130
4131
4132 void
4133 dwarf_var_expanding_visitor::visit_target_symbol (target_symbol *e)
4134 {
4135 assert(e->name.size() > 0 && (e->name[0] == '$' || e->name == "@var"));
4136 visited = true;
4137 bool defined_being_checked = (defined_ops.size() > 0 && (defined_ops.top()->operand == e));
4138 // In this mode, we avoid hiding errors or generating extra code such as for .return saved $vars
4139
4140 try
4141 {
4142 bool lvalue = is_active_lvalue(e);
4143 if (lvalue && !q.sess.guru_mode)
4144 throw SEMANTIC_ERROR(_("write to target variable not permitted; need stap -g"), e->tok);
4145
4146 // XXX: process $context vars should be writable
4147
4148 // See if we need to generate a new probe to save/access function
4149 // parameters from a return probe. PR 1382.
4150 if (q.has_return
4151 && !defined_being_checked
4152 && e->name != "$return" // not the special return-value variable handled below
4153 && e->name != "$$return") // nor the other special variable handled below
4154 {
4155 if (lvalue)
4156 throw SEMANTIC_ERROR(_("write to target variable not permitted in .return probes"), e->tok);
4157 visit_target_symbol_saved_return(e);
4158 return;
4159 }
4160
4161 if (e->name == "$$vars" || e->name == "$$parms" || e->name == "$$locals"
4162 || (q.has_return && (e->name == "$$return")))
4163 {
4164 if (lvalue)
4165 throw SEMANTIC_ERROR(_("cannot write to context variable"), e->tok);
4166
4167 if (e->addressof)
4168 throw SEMANTIC_ERROR(_("cannot take address of context variable"), e->tok);
4169
4170 e->assert_no_components("dwarf", true);
4171
4172 visit_target_symbol_context(e);
4173 return;
4174 }
4175
4176 // Everything else (pretty-printed vars, and context vars) require a
4177 // scope_die in which to search for them. If we don't have that, just
4178 // leave it unresolved; we'll produce an error later on.
4179 if (null_die(scope_die))
4180 {
4181 provide(e);
4182 return;
4183 }
4184
4185 if (e->check_pretty_print (lvalue))
4186 {
4187 if (q.has_return && (e->name == "$return"))
4188 {
4189 dwarf_pretty_print dpp (q.dw, scope_die, addr,
4190 q.has_process, *e);
4191 dpp.expand()->visit(this);
4192 }
4193 else
4194 {
4195 dwarf_pretty_print dpp (q.dw, getscopes(e), addr,
4196 e->sym_name(),
4197 q.has_process, *e);
4198 dpp.expand()->visit(this);
4199 }
4200 return;
4201 }
4202
4203 bool userspace_p = q.has_process;
4204 string fname = (string(lvalue ? "_dwarf_tvar_set" : "_dwarf_tvar_get")
4205 + "_" + e->sym_name()
4206 + "_" + lex_cast(tick++));
4207
4208
4209 string code;
4210 Dwarf_Die endtype;
4211 if (q.has_return && (e->name == "$return"))
4212 code = q.dw.literal_stmt_for_return (scope_die, addr, e, lvalue, &endtype);
4213 else
4214 code = q.dw.literal_stmt_for_local (getscopes(e), addr, e->sym_name(),
4215 e, lvalue, &endtype);
4216
4217 functioncall* n = synthetic_embedded_deref_call(q.dw, &endtype, fname, code,
4218 userspace_p, lvalue, e);
4219
4220 if (lvalue)
4221 provide_lvalue_call (n);
4222
4223 // Revisit the functioncall so arguments can be expanded.
4224 n->visit (this);
4225 }
4226 catch (const semantic_error& er)
4227 {
4228 // We suppress this error message, and pass the unresolved
4229 // target_symbol to the next pass. We hope that this value ends
4230 // up not being referenced after all, so it can be optimized out
4231 // quietly.
4232 e->chain (er);
4233 provide (e);
4234 }
4235 }
4236
4237
4238 void
4239 dwarf_var_expanding_visitor::visit_cast_op (cast_op *e)
4240 {
4241 // Fill in our current module context if needed
4242 if (e->module.empty())
4243 e->module = q.dw.module_name;
4244
4245 var_expanding_visitor::visit_cast_op(e);
4246 }
4247
4248
4249 void
4250 dwarf_var_expanding_visitor::visit_entry_op (entry_op *e)
4251 {
4252 expression *repl = e;
4253 if (q.has_return)
4254 {
4255 // expand the operand as if it weren't a return probe
4256 q.has_return = false;
4257 replace (e->operand);
4258 q.has_return = true;
4259
4260 // XXX it would be nice to use gen_kretprobe_saved_return when available,
4261 // but it requires knowing the types already, which is problematic for
4262 // arbitrary expressons.
4263 repl = gen_mapped_saved_return (e->operand, "entry");
4264 }
4265 provide (repl);
4266 }
4267
4268 void
4269 dwarf_var_expanding_visitor::visit_perf_op (perf_op *e)
4270 {
4271 string e_lit_val = e->operand->value;
4272
4273 add_block = new block;
4274 add_block->tok = e->tok;
4275
4276 systemtap_session &s = this->q.sess;
4277 std::vector<std::pair<std::string,std::string> >::iterator it;
4278 // Find the associated perf.counter probe
4279 for (it=s.perf_counters.begin();
4280 it != s.perf_counters.end();
4281 it++)
4282 if ((*it).first == e_lit_val)
4283 {
4284 // if perf .process("name") omitted, then set it to this process name
4285 if ((*it).second.length() == 0)
4286 (*it).second = this->q.user_path;
4287 if ((*it).second == this->q.user_path)
4288 break;
4289 }
4290
4291 if (it != s.perf_counters.end())
4292 {
4293 perf_counter_refs.insert((*it).first);
4294 // __perf_read_N is assigned in the probe prologue
4295 symbol* sym = new symbol;
4296 sym->tok = e->tok;
4297 sym->name = "__perf_read_" + (*it).first;
4298 provide (sym);
4299 }
4300 else
4301 throw SEMANTIC_ERROR(_F("perf counter '%s' not defined", e_lit_val.c_str()));
4302 }
4303
4304
4305 vector<Dwarf_Die>&
4306 dwarf_var_expanding_visitor::getscopes(target_symbol *e)
4307 {
4308 if (scopes.empty())
4309 {
4310 if(!null_die(scope_die))
4311 scopes = q.dw.getscopes(scope_die);
4312 if (scopes.empty())
4313 //throw semantic_error (_F("unable to find any scopes containing %d", addr), e->tok);
4314 // ((scope_die == NULL) ? "" : (string (" in ") + (dwarf_diename(scope_die) ?: "<unknown>") + "(" + (dwarf_diename(q.dw.cu) ?: "<unknown>") ")" ))
4315 throw SEMANTIC_ERROR ("unable to find any scopes containing "
4316 + lex_cast_hex(addr)
4317 + (null_die(scope_die) ? ""
4318 : (string (" in ")
4319 + (dwarf_diename(scope_die) ?: "<unknown>")
4320 + "(" + (dwarf_diename(q.dw.cu) ?: "<unknown>")
4321 + ")"))
4322 + " while searching for local '"
4323 + e->sym_name() + "'",
4324 e->tok);
4325 }
4326 return scopes;
4327 }
4328
4329
4330 struct dwarf_cast_expanding_visitor: public var_expanding_visitor
4331 {
4332 systemtap_session& s;
4333 dwarf_builder& db;
4334 map<string,string> compiled_headers;
4335
4336 dwarf_cast_expanding_visitor(systemtap_session& s, dwarf_builder& db):
4337 s(s), db(db) {}
4338 void visit_cast_op (cast_op* e);
4339 void filter_special_modules(string& module);
4340 };
4341
4342
4343 struct dwarf_cast_query : public base_query
4344 {
4345 cast_op& e;
4346 const bool lvalue;
4347 const bool userspace_p;
4348 functioncall*& result;
4349
4350 dwarf_cast_query(dwflpp& dw, const string& module, cast_op& e, bool lvalue,
4351 const bool userspace_p, functioncall*& result):
4352 base_query(dw, module), e(e), lvalue(lvalue),
4353 userspace_p(userspace_p), result(result) {}
4354
4355 void handle_query_module();
4356 void query_library (const char *) {}
4357 void query_plt (const char *entry, size_t addr) {}
4358 };
4359
4360
4361 void
4362 dwarf_cast_query::handle_query_module()
4363 {
4364 static unsigned tick = 0;
4365
4366 if (result)
4367 return;
4368
4369 // look for the type in any CU
4370 Dwarf_Die* type_die = NULL;
4371 if (startswith(e.type_name, "class "))
4372 {
4373 // normalize to match dwflpp::global_alias_caching_callback
4374 string struct_name = "struct " + e.type_name.substr(6);
4375 type_die = dw.declaration_resolve_other_cus(struct_name);
4376 }
4377 else
4378 type_die = dw.declaration_resolve_other_cus(e.type_name);
4379
4380 // NB: We now index the types as "struct name"/"union name"/etc. instead of
4381 // just "name". But since we didn't require users to be explicit before, and
4382 // actually sort of discouraged it, we must be flexible now. So if a lookup
4383 // fails with a bare name, try augmenting it.
4384 if (!type_die &&
4385 !startswith(e.type_name, "class ") &&
4386 !startswith(e.type_name, "struct ") &&
4387 !startswith(e.type_name, "union ") &&
4388 !startswith(e.type_name, "enum "))
4389 {
4390 type_die = dw.declaration_resolve_other_cus("struct " + e.type_name);
4391 if (!type_die)
4392 type_die = dw.declaration_resolve_other_cus("union " + e.type_name);
4393 if (!type_die)
4394 type_die = dw.declaration_resolve_other_cus("enum " + e.type_name);
4395 }
4396
4397 if (!type_die)
4398 return;
4399
4400 string code;
4401 Dwarf_Die endtype;
4402
4403 try
4404 {
4405 Dwarf_Die cu_mem;
4406 dw.focus_on_cu(dwarf_diecu(type_die, &cu_mem, NULL, NULL));
4407
4408 if (e.check_pretty_print (lvalue))
4409 {
4410 dwarf_pretty_print dpp(dw, type_die, e.operand, true, userspace_p, e);
4411 result = dpp.expand();
4412 return;
4413 }
4414
4415 code = dw.literal_stmt_for_pointer (type_die, &e, lvalue, &endtype);
4416 }
4417 catch (const semantic_error& er)
4418 {
4419 // NB: we can have multiple errors, since a @cast
4420 // may be attempted using several different modules:
4421 // @cast(ptr, "type", "module1:module2:...")
4422 e.chain (er);
4423 }
4424
4425 if (code.empty())
4426 return;
4427
4428 string fname = (string(lvalue ? "_dwarf_cast_set" : "_dwarf_cast_get")
4429 + "_" + e.sym_name()
4430 + "_" + lex_cast(tick++));
4431
4432 result = synthetic_embedded_deref_call(dw, &endtype, fname, code,
4433 userspace_p, lvalue, &e, e.operand);
4434 }
4435
4436
4437 void dwarf_cast_expanding_visitor::filter_special_modules(string& module)
4438 {
4439 // look for "<path/to/header>" or "kernel<path/to/header>"
4440 // for those cases, build a module including that header
4441 if (module[module.size() - 1] == '>' &&
4442 (module[0] == '<' || startswith(module, "kernel<")))
4443 {
4444 string header = module;
4445 map<string,string>::const_iterator it = compiled_headers.find(header);
4446 if (it != compiled_headers.end())
4447 {
4448 module = it->second;
4449 return;
4450 }
4451
4452 string cached_module;
4453 if (s.use_cache)
4454 {
4455 // see if the cached module exists
4456 cached_module = find_typequery_hash(s, module);
4457 if (!cached_module.empty() && !s.poison_cache)
4458 {
4459 int fd = open(cached_module.c_str(), O_RDONLY);
4460 if (fd != -1)
4461 {
4462 if (s.verbose > 2)
4463 //TRANSLATORS: Here we're using a cached module.
4464 clog << _("Pass 2: using cached ") << cached_module << endl;
4465 compiled_headers[header] = module = cached_module;
4466 close(fd);
4467 return;
4468 }
4469 }
4470 }
4471
4472 // no cached module, time to make it
4473 if (make_typequery(s, module) == 0)
4474 {
4475 // try to save typequery in the cache
4476 if (s.use_cache)
4477 copy_file(module, cached_module, s.verbose > 2);
4478 compiled_headers[header] = module;
4479 }
4480 }
4481 }
4482
4483
4484 void dwarf_cast_expanding_visitor::visit_cast_op (cast_op* e)
4485 {
4486 bool lvalue = is_active_lvalue(e);
4487 if (lvalue && !s.guru_mode)
4488 throw SEMANTIC_ERROR(_("write to @cast context variable not permitted; need stap -g"), e->tok);
4489
4490 if (e->module.empty())
4491 e->module = "kernel"; // "*" may also be reasonable to search all kernel modules
4492
4493 functioncall* result = NULL;
4494
4495 // split the module string by ':' for alternatives
4496 vector<string> modules;
4497 tokenize(e->module, modules, ":");
4498 bool userspace_p=false; // PR10601
4499 for (unsigned i = 0; !result && i < modules.size(); ++i)
4500 {
4501 string& module = modules[i];
4502 filter_special_modules(module);
4503
4504 // NB: This uses '/' to distinguish between kernel modules and userspace,
4505 // which means that userspace modules won't get any PATH searching.
4506 dwflpp* dw;
4507 try
4508 {
4509 userspace_p=is_user_module (module);
4510 if (! userspace_p)
4511 {
4512 // kernel or kernel module target
4513 dw = db.get_kern_dw(s, module);
4514 }
4515 else
4516 {
4517 module = find_executable (module, "", s.sysenv); // canonicalize it
4518 dw = db.get_user_dw(s, module);
4519 }
4520 }
4521 catch (const semantic_error& er)
4522 {
4523 /* ignore and go to the next module */
4524 continue;
4525 }
4526
4527 dwarf_cast_query q (*dw, module, *e, lvalue, userspace_p, result);
4528 dw->iterate_over_modules<base_query>(&query_module, &q);
4529 }
4530
4531 if (!result)
4532 {
4533 // We pass the unresolved cast_op to the next pass, and hope
4534 // that this value ends up not being referenced after all, so
4535 // it can be optimized out quietly.
4536 provide (e);
4537 return;
4538 }
4539
4540 if (lvalue)
4541 provide_lvalue_call (result);
4542
4543 result->visit (this);
4544 }
4545
4546
4547 static bool resolve_pointer_type(Dwarf_Die& die, bool& isptr);
4548
4549 exp_type_dwarf::exp_type_dwarf(dwflpp* dw, Dwarf_Die* die,
4550 bool userspace_p, bool addressof):
4551 dw(dw), die(*die), userspace_p(userspace_p), is_pointer(false)
4552 {
4553 // is_pointer tells us whether a value is a pointer to the given type, so we
4554 // can dereference it; otherwise it will be treated as an end point.
4555 if (addressof)
4556 // we're already looking at the pointed-to type
4557 is_pointer = true;
4558 else
4559 // use the same test as tracepoints to see what we have
4560 resolve_pointer_type(this->die, is_pointer);
4561 }
4562
4563
4564 functioncall *
4565 exp_type_dwarf::expand(autocast_op* e, bool lvalue)
4566 {
4567 static unsigned tick = 0;
4568
4569 try
4570 {
4571 // make sure we're not dereferencing base types or void
4572 bool deref_p = is_pointer && !null_die(&die);
4573 if (!deref_p)
4574 e->assert_no_components("autocast", true);
4575
4576 if (lvalue && !dw->sess.guru_mode)
4577 throw SEMANTIC_ERROR(_("write not permitted; need stap -g"), e->tok);
4578
4579 if (e->components.empty())
4580 {
4581 if (e->addressof)
4582 throw SEMANTIC_ERROR(_("cannot take address of tracepoint variable"), e->tok);
4583
4584 // no components and no addressof? how did this autocast come to be?
4585 throw SEMANTIC_ERROR(_("internal error: no-op autocast encountered"), e->tok);
4586 }
4587
4588 Dwarf_Die cu_mem;
4589 if (!null_die(&die))
4590 dw->focus_on_cu(dwarf_diecu(&die, &cu_mem, NULL, NULL));
4591
4592 if (e->check_pretty_print (lvalue))
4593 {
4594 dwarf_pretty_print dpp(*dw, &die, e->operand, deref_p, userspace_p, *e);
4595 return dpp.expand();
4596 }
4597
4598 Dwarf_Die endtype;
4599 string code = dw->literal_stmt_for_pointer (&die, e, lvalue, &endtype);
4600
4601 string fname = (string(lvalue ? "_dwarf_autocast_set" : "_dwarf_autocast_get")
4602 + "_" + lex_cast(tick++));
4603
4604 return synthetic_embedded_deref_call(*dw, &endtype, fname, code,
4605 userspace_p, lvalue, e, e->operand);
4606 }
4607 catch (const semantic_error &er)
4608 {
4609 e->chain (er);
4610 return NULL;
4611 }
4612 }
4613
4614
4615 struct dwarf_atvar_expanding_visitor: public var_expanding_visitor
4616 {
4617 systemtap_session& s;
4618 dwarf_builder& db;
4619
4620 dwarf_atvar_expanding_visitor(systemtap_session& s, dwarf_builder& db):
4621 s(s), db(db) {}
4622 void visit_atvar_op (atvar_op* e);
4623 };
4624
4625
4626 struct dwarf_atvar_query: public base_query
4627 {
4628 atvar_op& e;
4629 const bool userspace_p, lvalue;
4630 functioncall*& result;
4631 unsigned& tick;
4632 const string cu_name_pattern;
4633
4634 dwarf_atvar_query(dwflpp& dw, const string& module, atvar_op& e,
4635 const bool userspace_p, const bool lvalue,
4636 functioncall*& result,
4637 unsigned& tick):
4638 base_query(dw, module), e(e),
4639 userspace_p(userspace_p), lvalue(lvalue), result(result),
4640 tick(tick), cu_name_pattern(string("*/") + e.cu_name) {}
4641
4642 void handle_query_module ();
4643 void query_library (const char *) {}
4644 void query_plt (const char *entry, size_t addr) {}
4645 static int atvar_query_cu (Dwarf_Die *cudie, dwarf_atvar_query *q);
4646 };
4647
4648
4649 int
4650 dwarf_atvar_query::atvar_query_cu (Dwarf_Die * cudie, dwarf_atvar_query *q)
4651 {
4652 if (! q->e.cu_name.empty())
4653 {
4654 const char *die_name = dwarf_diename(cudie) ?: "";
4655 if (strcmp(die_name, q->e.cu_name.c_str()) != 0 // Perfect match
4656 && fnmatch(q->cu_name_pattern.c_str(), die_name, 0) != 0)
4657 {
4658 return DWARF_CB_OK;
4659 }
4660 }
4661
4662 try
4663 {
4664 vector<Dwarf_Die> scopes(1, *cudie);
4665
4666 q->dw.focus_on_cu (cudie);
4667
4668 if (q->e.check_pretty_print (q->lvalue))
4669 {
4670 dwarf_pretty_print dpp (q->dw, scopes, 0, q->e.sym_name(),
4671 q->userspace_p, q->e);
4672 q->result = dpp.expand();
4673 return DWARF_CB_ABORT;
4674 }
4675
4676 Dwarf_Die endtype;
4677 string code = q->dw.literal_stmt_for_local (scopes, 0, q->e.sym_name(),
4678 &q->e, q->lvalue, &endtype);
4679
4680 if (code.empty())
4681 return DWARF_CB_OK;
4682
4683 string fname = (string(q->lvalue ? "_dwarf_tvar_set"
4684 : "_dwarf_tvar_get")
4685 + "_" + q->e.sym_name()
4686 + "_" + lex_cast(q->tick++));
4687
4688 q->result = synthetic_embedded_deref_call (q->dw, &endtype, fname, code,
4689 q->userspace_p, q->lvalue,
4690 &q->e);
4691 }
4692 catch (const semantic_error& er)
4693 {
4694 // Here we suppress the error because we often just have too many
4695 // when scanning all the CUs.
4696 return DWARF_CB_OK;
4697 }
4698
4699 if (q->result) {
4700 return DWARF_CB_ABORT;
4701 }
4702
4703 return DWARF_CB_OK;
4704 }
4705
4706
4707 void
4708 dwarf_atvar_query::handle_query_module ()
4709 {
4710
4711 dw.iterate_over_cus(atvar_query_cu, this, false);
4712 }
4713
4714
4715 void
4716 dwarf_atvar_expanding_visitor::visit_atvar_op (atvar_op* e)
4717 {
4718 const bool lvalue = is_active_lvalue(e);
4719 if (lvalue && !s.guru_mode)
4720 throw SEMANTIC_ERROR(_("write to @var variable not permitted; "
4721 "need stap -g"), e->tok);
4722
4723 if (e->module.empty())
4724 e->module = "kernel";
4725
4726 functioncall* result = NULL;
4727
4728 // split the module string by ':' for alternatives
4729 vector<string> modules;
4730 tokenize(e->module, modules, ":");
4731 bool userspace_p = false;
4732 for (unsigned i = 0; !result && i < modules.size(); ++i)
4733 {
4734 string& module = modules[i];
4735
4736 dwflpp* dw;
4737 try
4738 {
4739 userspace_p = is_user_module(module);
4740 if (!userspace_p)
4741 {
4742 // kernel or kernel module target
4743 dw = db.get_kern_dw(s, module);
4744 }
4745 else
4746 {
4747 module = find_executable(module, "", s.sysenv);
4748 dw = db.get_user_dw(s, module);
4749 }
4750 }
4751 catch (const semantic_error& er)
4752 {
4753 /* ignore and go to the next module */
4754 continue;
4755 }
4756
4757 dwarf_atvar_query q (*dw, module, *e, userspace_p, lvalue, result, tick);
4758 dw->iterate_over_modules<base_query>(&query_module, &q);
4759
4760 if (result)
4761 {
4762 s.unwindsym_modules.insert(module);
4763
4764 if (lvalue)
4765 provide_lvalue_call (result);
4766
4767 result->visit(this);
4768 return;
4769 }
4770
4771 /* Unable to find the variable in the current module, so we chain
4772 * an error in atvar_op */
4773 semantic_error er(ERR_SRC, _F("unable to find global '%s' in %s%s%s",
4774 e->sym_name().c_str(), module.c_str(),
4775 e->cu_name.empty() ? "" : _(", in "),
4776 e->cu_name.c_str()));
4777 e->chain (er);
4778 }
4779
4780 provide(e);
4781 }
4782
4783
4784 void
4785 dwarf_derived_probe::printsig (ostream& o) const
4786 {
4787 // Instead of just printing the plain locations, we add a PC value
4788 // as a comment as a way of telling e.g. apart multiple inlined
4789 // function instances. This is distinct from the verbose/clog
4790 // output, since this part goes into the cache hash calculations.
4791 sole_location()->print (o);
4792 o << " /* pc=" << section << "+0x" << hex << addr << dec << " */";
4793 printsig_nested (o);
4794 }
4795
4796
4797
4798 void
4799 dwarf_derived_probe::join_group (systemtap_session& s)
4800 {
4801 // skip probes which are paired entry-handlers
4802 if (!has_return && (saved_longs || saved_strings))
4803 return;
4804
4805 if (! s.dwarf_derived_probes)
4806 s.dwarf_derived_probes = new dwarf_derived_probe_group ();
4807 s.dwarf_derived_probes->enroll (this);
4808 this->group = s.dwarf_derived_probes;
4809 if (has_return && entry_handler)
4810 entry_handler->group = s.dwarf_derived_probes;
4811 }
4812
4813
4814 static bool
4815 kernel_supports_inode_uprobes(systemtap_session& s)
4816 {
4817 // The arch-supports is new to the builtin inode-uprobes, so it makes a
4818 // reasonable indicator of the new API. Else we'll need an autoconf...
4819 // see also buildrun.cxx:kernel_built_uprobs()
4820 return (s.kernel_config["CONFIG_ARCH_SUPPORTS_UPROBES"] == "y"
4821 && s.kernel_config["CONFIG_UPROBES"] == "y");
4822 }
4823
4824
4825 static bool
4826 kernel_supports_inode_uretprobes(systemtap_session& s)
4827 {
4828 // We need inode-uprobes first, then look for a sign of uretprobes. The only
4829 // non-static function at present is arch_uretprobe_hijack_return_addr.
4830 return kernel_supports_inode_uprobes(s) &&
4831 (s.kernel_functions.count("arch_uretprobe_hijack_return_addr") > 0);
4832 }
4833
4834
4835 void
4836 check_process_probe_kernel_support(systemtap_session& s)
4837 {
4838 // If we've got utrace, we're good to go.
4839 if (s.kernel_config["CONFIG_UTRACE"] == "y")
4840 return;
4841
4842 // We don't have utrace. For process probes that aren't
4843 // uprobes-based, we just need the task_finder. The task_finder
4844 // needs CONFIG_TRACEPOINTS and specific tracepoints. There is a
4845 // specific autoconf test for its needs.
4846 //
4847 // We'll just require CONFIG_TRACEPOINTS here as a quick-and-dirty
4848 // approximation.
4849 if (! s.need_uprobes && s.kernel_config["CONFIG_TRACEPOINTS"] == "y")
4850 return;
4851
4852 // For uprobes-based process probes, we need the task_finder plus
4853 // the builtin inode-uprobes.
4854 if (s.need_uprobes
4855 && s.kernel_config["CONFIG_TRACEPOINTS"] == "y"
4856 && kernel_supports_inode_uprobes(s))
4857 return;
4858
4859 throw SEMANTIC_ERROR (_("process probes not available without kernel CONFIG_UTRACE or CONFIG_TRACEPOINTS/CONFIG_ARCH_SUPPORTS_UPROBES/CONFIG_UPROBES"));
4860 }
4861
4862
4863 dwarf_derived_probe::dwarf_derived_probe(const string& funcname,
4864 const string& filename,
4865 int line,
4866 // module & section specify a relocation
4867 // base for <addr>, unless section==""
4868 // (equivalently module=="kernel")
4869 // for userspace, it's a full path, for
4870 // modules, it's either a full path, or
4871 // the basename (e.g. 'btrfs')
4872 const string& module,
4873 const string& section,
4874 // NB: dwfl_addr is the virtualized
4875 // address for this symbol.
4876 Dwarf_Addr dwfl_addr,
4877 // addr is the section-offset for
4878 // actual relocation.
4879 Dwarf_Addr addr,
4880 dwarf_query& q,
4881 Dwarf_Die* scope_die /* may be null */)
4882 : derived_probe (q.base_probe, q.base_loc, true /* .components soon rewritten */ ),
4883 module (module), section (section), addr (addr),
4884 path (q.path),
4885 has_process (q.has_process),
4886 has_return (q.has_return),
4887 has_maxactive (q.has_maxactive),
4888 has_library (q.has_library),
4889 maxactive_val (q.maxactive_val),
4890 user_path (q.user_path),
4891 user_lib (q.user_lib),
4892 access_vars(false),
4893 saved_longs(0), saved_strings(0),
4894 entry_handler(0)
4895 {
4896 // If we were given a fullpath to a kernel module, then get the simple name
4897 if (q.has_module && is_fully_resolved(module, q.dw.sess.sysroot, q.dw.sess.sysenv))
4898 this->module = modname_from_path(module);
4899
4900 if (user_lib.size() != 0)
4901 has_library = true;
4902
4903 if (q.has_process)
4904 {
4905 // We may receive probes on two types of ELF objects: ET_EXEC or ET_DYN.
4906 // ET_EXEC ones need no further relocation on the addr(==dwfl_addr), whereas
4907 // ET_DYN ones do (addr += run-time mmap base address). We tell these apart
4908 // by the incoming section value (".absolute" vs. ".dynamic").
4909 // XXX Assert invariants here too?
4910
4911 // inode-uprobes needs an offset rather than an absolute VM address.
4912 // ditto for userspace runtimes (dyninst)
4913 if ((kernel_supports_inode_uprobes(q.dw.sess) || q.dw.sess.runtime_usermode_p()) &&
4914 section == ".absolute" && addr == dwfl_addr &&
4915 addr >= q.dw.module_start && addr < q.dw.module_end)
4916 this->addr = addr - q.dw.module_start;
4917 }
4918 else
4919 {
4920 // Assert kernel relocation invariants
4921 if (section == "" && dwfl_addr != addr) // addr should be absolute
4922 throw SEMANTIC_ERROR (_("missing relocation basis"), tok);
4923 if (section != "" && dwfl_addr == addr) // addr should be an offset
4924 throw SEMANTIC_ERROR (_("inconsistent relocation address"), tok);
4925 }
4926
4927 // XXX: hack for strange g++/gcc's
4928 #ifndef USHRT_MAX
4929 #define USHRT_MAX 32767
4930 #endif
4931
4932 // Range limit maxactive() value
4933 if (has_maxactive && (maxactive_val < 0 || maxactive_val > USHRT_MAX))
4934 throw SEMANTIC_ERROR (_F("maxactive value out of range [0,%s]",
4935 lex_cast(USHRT_MAX).c_str()), q.base_loc->components.front()->tok);
4936
4937 // Expand target variables in the probe body. Even if the scope_die is
4938 // invalid, we still want to expand things such as $$vars/$$parms/etc...
4939 // (PR15999, PR16473). Access to specific context vars e.g. $argc will not be
4940 // expanded and will produce an error during the typeresolution_info pass.
4941 {
4942 // XXX: user-space deref's for q.has_process!
4943
4944 // PR14436: if we're expanding target variables in the probe body of a
4945 // .return probe, we need to make the expansion at the postprologue addr
4946 // instead (if any), which is then also the spot where the entry handler
4947 // probe is placed. (Note that at this point, a nonzero prologue_end
4948 // implies that it should be used, i.e. code is unoptimized).
4949 Dwarf_Addr handler_dwfl_addr = dwfl_addr;
4950 if (q.prologue_end != 0 && q.has_return)
4951 {
4952 handler_dwfl_addr = q.prologue_end;
4953 if (q.sess.verbose > 2)
4954 clog << _F("expanding .return vars at prologue_end (0x%s) "
4955 "rather than entrypc (0x%s)\n",
4956 lex_cast_hex(handler_dwfl_addr).c_str(),
4957 lex_cast_hex(dwfl_addr).c_str());
4958 }
4959 dwarf_var_expanding_visitor v (q, scope_die, handler_dwfl_addr);
4960 v.replace (this->body);
4961
4962 // Propagate perf.counters so we can emit later
4963 this->perf_counter_refs = v.perf_counter_refs;
4964 // Emit local var used to save the perf counter read value
4965 std::set<string>::iterator pcii;
4966 for (pcii = v.perf_counter_refs.begin();
4967 pcii != v.perf_counter_refs.end(); pcii++)
4968 {
4969 std::vector<std::pair<std::string,std::string> >::iterator it;
4970 // Find the associated perf counter probe
4971 for (it=q.sess.perf_counters.begin() ;
4972 it != q.sess.perf_counters.end();
4973 it++)
4974 if ((*it).first == (*pcii))
4975 break;
4976 vardecl* vd = new vardecl;
4977 vd->name = "__perf_read_" + (*it).first;
4978 vd->tok = this->tok;
4979 vd->set_arity(0, this->tok);
4980 vd->type = pe_long;
4981 vd->synthetic = true;
4982 this->locals.push_back (vd);
4983 }
4984
4985
4986 if (!q.has_process)
4987 access_vars = v.visited;
4988
4989 // If during target-variable-expanding the probe, we added a new block
4990 // of code, add it to the start of the probe.
4991 if (v.add_block)
4992 this->body = new block(v.add_block, this->body);
4993
4994 // If when target-variable-expanding the probe, we need to synthesize a
4995 // sibling function-entry probe. We don't go through the whole probe derivation
4996 // business (PR10642) that could lead to wildcard/alias resolution, or for that
4997 // dwarf-induced duplication.
4998 if (v.add_call_probe)
4999 {
5000 assert (q.has_return && !q.has_call);
5001
5002 // We temporarily replace q.base_probe.
5003 statement* old_body = q.base_probe->body;
5004 q.base_probe->body = v.add_call_probe;
5005 q.has_return = false;
5006 q.has_call = true;
5007
5008 if (q.has_process)
5009 {
5010 // Place handler probe at the same addr as where the vars were
5011 // expanded (which may not be the same addr as the one for the
5012 // main retprobe, PR14436).
5013 Dwarf_Addr handler_addr = addr;
5014 if (handler_dwfl_addr != dwfl_addr)
5015 // adjust section offset by prologue_end-entrypc
5016 handler_addr += handler_dwfl_addr - dwfl_addr;
5017 entry_handler = new uprobe_derived_probe (funcname, filename,
5018 line, module, section,
5019 handler_dwfl_addr,
5020 handler_addr, q,
5021 scope_die);
5022 }
5023 else
5024 entry_handler = new dwarf_derived_probe (funcname, filename, line,
5025 module, section, dwfl_addr,
5026 addr, q, scope_die);
5027
5028 saved_longs = entry_handler->saved_longs = v.saved_longs;
5029 saved_strings = entry_handler->saved_strings = v.saved_strings;
5030
5031 q.results.push_back (entry_handler);
5032
5033 q.has_return = true;
5034 q.has_call = false;
5035 q.base_probe->body = old_body;
5036 }
5037 // Save the local variables for listing mode. If the scope_die is null,
5038 // local vars aren't accessible, so no need to invoke saveargs (PR10820).
5039 if (!null_die(scope_die) &&
5040 q.sess.dump_mode == systemtap_session::dump_matched_probes_vars)
5041 saveargs(q, scope_die, dwfl_addr);
5042 }
5043
5044 // Reset the sole element of the "locations" vector as a
5045 // "reverse-engineered" form of the incoming (q.base_loc) probe
5046 // point. This allows a user to see what function / file / line
5047 // number any particular match of the wildcards.
5048
5049 vector<probe_point::component*> comps;
5050 if (q.has_kernel)
5051 comps.push_back (new probe_point::component(TOK_KERNEL));
5052 else if(q.has_module)
5053 comps.push_back (new probe_point::component(TOK_MODULE, new literal_string(module)));
5054 else if(q.has_process)
5055 comps.push_back (new probe_point::component(TOK_PROCESS, new literal_string(module)));
5056 else
5057 assert (0);
5058
5059 string fn_or_stmt;
5060 if (q.has_function_str || q.has_function_num)
5061 fn_or_stmt = TOK_FUNCTION;
5062 else
5063 fn_or_stmt = TOK_STATEMENT;
5064
5065 if (q.has_function_str || q.has_statement_str)
5066 {
5067 string retro_name = q.final_function_name(funcname, filename.c_str(), line);
5068 comps.push_back
5069 (new probe_point::component
5070 (fn_or_stmt, new literal_string (retro_name)));
5071 }
5072 else if (q.has_function_num || q.has_statement_num)
5073 {
5074 Dwarf_Addr retro_addr;
5075 if (q.has_function_num)
5076 retro_addr = q.function_num_val;
5077 else
5078 retro_addr = q.statement_num_val;
5079 comps.push_back (new probe_point::component
5080 (fn_or_stmt,
5081 new literal_number(retro_addr, true)));
5082
5083 if (q.has_absolute)
5084 comps.push_back (new probe_point::component (TOK_ABSOLUTE));
5085 }
5086
5087 if (q.has_call)
5088 comps.push_back (new probe_point::component(TOK_CALL));
5089 if (q.has_exported)
5090 comps.push_back (new probe_point::component(TOK_EXPORTED));
5091 if (q.has_inline)
5092 comps.push_back (new probe_point::component(TOK_INLINE));
5093 if (has_return)
5094 comps.push_back (new probe_point::component(TOK_RETURN));
5095 if (has_maxactive)
5096 comps.push_back (new probe_point::component
5097 (TOK_MAXACTIVE, new literal_number(maxactive_val)));
5098
5099 // Overwrite it.
5100 this->sole_location()->components = comps;
5101
5102 // if it's a .callee[s[(N)]] call, add checks to the probe body so that the
5103 // user body is only 'triggered' when called from q.callers[N-1], which
5104 // itself is called from q.callers[N-2], etc... I.E.
5105 // callees(N) --> N elements in q.callers --> N checks against [u]stack(0..N-1)
5106 if ((q.has_callee || q.has_callees_num) && q.callers && !q.callers->empty())
5107 {
5108 if (q.sess.verbose > 2)
5109 clog << _F("adding caller checks for callee %s\n", funcname.c_str());
5110
5111 // Copy the stack and empty it out
5112 stack<Dwarf_Addr> callers(*q.callers);
5113 for (unsigned level = 1; !callers.empty(); level++,
5114 callers.pop())
5115 {
5116 Dwarf_Addr caller = callers.top();
5117
5118 // We first need to make the caller addr relocatable
5119 string caller_section;
5120 Dwarf_Addr caller_reloc;
5121 if (module == TOK_KERNEL)
5122 { // allow for relocatable kernel (see also add_probe_point())
5123 caller_reloc = caller - q.sess.sym_stext;
5124 caller_section = "_stext";
5125 }
5126 else
5127 caller_reloc = q.dw.relocate_address(caller,
5128 caller_section);
5129
5130 if (q.sess.verbose > 2)
5131 clog << _F("adding caller check [u]stack(%d) == reloc(0x%s)\n",
5132 level, lex_cast_hex(caller_reloc).c_str());
5133
5134 // We want to add a statement like this:
5135 // if (!_caller_match(user, mod, sec, addr)) next;
5136 // Something similar is done in semantic_pass_conditions()
5137
5138 functioncall* check = new functioncall();
5139 check->tok = this->tok;
5140 check->function = "_caller_match";
5141 check->args.push_back(new literal_number(q.has_process));
5142 check->args[0]->tok = this->tok;
5143 check->args.push_back(new literal_number(level));
5144 check->args[1]->tok = this->tok;
5145 check->args.push_back(new literal_string(this->module));
5146 check->args[2]->tok = this->tok;
5147 check->args.push_back(new literal_string(caller_section));
5148 check->args[3]->tok = this->tok;
5149 check->args.push_back(new literal_number(caller_reloc, true /* hex */));
5150 check->args[4]->tok = this->tok;
5151
5152 unary_expression* notexp = new unary_expression();
5153 notexp->tok = this->tok;
5154 notexp->op = "!";
5155 notexp->operand = check;
5156
5157 if_statement* ifs = new if_statement();
5158 ifs->tok = this->tok;
5159 ifs->thenblock = new next_statement();
5160 ifs->thenblock->tok = this->tok;
5161 ifs->elseblock = NULL;
5162 ifs->condition = notexp;
5163
5164 this->body = new block(ifs, this->body);
5165 }
5166 }
5167 }
5168
5169
5170 void
5171 dwarf_derived_probe::saveargs(dwarf_query& q, Dwarf_Die* scope_die,
5172 Dwarf_Addr dwfl_addr)
5173 {
5174 if (null_die(scope_die))
5175 return;
5176
5177 bool verbose = q.sess.verbose > 2;
5178
5179 if (verbose)
5180 clog << _F("saveargs: examining '%s' (dieoffset: %#" PRIx64 ")\n", (dwarf_diename(scope_die)?: "unknown"), dwarf_dieoffset(scope_die));
5181
5182 if (has_return)
5183 {
5184 /* Only save the return value if it has a type. */
5185 string type_name;
5186 Dwarf_Die type_die;
5187 if (dwarf_attr_die (scope_die, DW_AT_type, &type_die) &&
5188 dwarf_type_name(&type_die, type_name))
5189 args.push_back("$return:"+type_name);
5190
5191 else if (verbose)
5192 clog << _F("saveargs: failed to retrieve type name for return value (dieoffset: %s)\n",
5193 lex_cast_hex(dwarf_dieoffset(scope_die)).c_str());
5194 }
5195
5196 Dwarf_Die arg;
5197 vector<Dwarf_Die> scopes = q.dw.getscopes(scope_die);
5198 for (unsigned i = 0; i < scopes.size(); ++i)
5199 {
5200 if (dwarf_tag(&scopes[i]) == DW_TAG_compile_unit)
5201 break; // we don't want file-level variables
5202 if (dwarf_child (&scopes[i], &arg) == 0)
5203 do
5204 {
5205 switch (dwarf_tag (&arg))
5206 {
5207 case DW_TAG_variable:
5208 case DW_TAG_formal_parameter:
5209 break;
5210
5211 default:
5212 continue;
5213 }
5214
5215 /* Ignore this local if it has no name. */
5216 const char *arg_name = dwarf_diename (&arg);
5217 if (!arg_name)
5218 {
5219 if (verbose)
5220 clog << _F("saveargs: failed to retrieve name for local (dieoffset: %s)\n",
5221 lex_cast_hex(dwarf_dieoffset(&arg)).c_str());
5222 continue;
5223 }
5224
5225 if (verbose)
5226 clog << _F("saveargs: finding location for local '%s' (dieoffset: %s)\n",
5227 arg_name, lex_cast_hex(dwarf_dieoffset(&arg)).c_str());
5228
5229 /* Ignore this local if it has no location (or not at this PC). */
5230 /* NB: It still may not be directly accessible, e.g. if it is an
5231 * aggregate type, implicit_pointer, etc., but the user can later
5232 * figure out how to access the interesting parts. */
5233
5234 /* XXX: Perhaps saveargs() / listings-mode should work by synthesizing
5235 * several synthetic
5236 * probe foo { $var }
5237 * probes, testing them for overall resolvability.
5238 */
5239
5240 Dwarf_Attribute attr_mem;
5241 if (!dwarf_attr_integrate (&arg, DW_AT_const_value, &attr_mem))
5242 {
5243 Dwarf_Op *expr;
5244 size_t len;
5245 if (!dwarf_attr_integrate (&arg, DW_AT_location, &attr_mem))
5246 {
5247 if (verbose)
5248 clog << _F("saveargs: failed to resolve the location for local '%s' (dieoffset: %s)\n",
5249 arg_name, lex_cast_hex(dwarf_dieoffset(&arg)).c_str());
5250 continue;
5251 }
5252 else if (!(dwarf_getlocation_addr(&attr_mem, dwfl_addr, &expr,
5253 &len, 1) == 1 && len > 0))
5254 {
5255 Dwarf_Addr dwfl_addr2 = q.dw.pr15123_retry_addr (dwfl_addr, & arg);
5256 if (!dwfl_addr2 || (!(dwarf_getlocation_addr(&attr_mem, dwfl_addr2, &expr,
5257 &len, 1) == 1 && len > 0))) {
5258 if (verbose)
5259 clog << _F("saveargs: local '%s' (dieoffset: %s) is not available at this address (%s)\n",
5260 arg_name, lex_cast_hex(dwarf_dieoffset(&arg)).c_str(), lex_cast_hex(dwfl_addr).c_str());
5261 continue;
5262 }
5263 }
5264 }
5265
5266 /* Ignore this local if it has no type. */
5267 string type_name;
5268 Dwarf_Die type_die;
5269 if (!dwarf_attr_die (&arg, DW_AT_type, &type_die) ||
5270 !dwarf_type_name(&type_die, type_name))
5271 {
5272 if (verbose)
5273 clog << _F("saveargs: failed to retrieve type name for local '%s' (dieoffset: %s)\n",
5274 arg_name, lex_cast_hex(dwarf_dieoffset(&arg)).c_str());
5275 continue;
5276 }
5277
5278 /* This local looks good -- save it! */
5279 args.push_back("$"+string(arg_name)+":"+type_name);
5280 }
5281 while (dwarf_siblingof (&arg, &arg) == 0);
5282 }
5283 }
5284
5285
5286 void
5287 dwarf_derived_probe::getargs(std::list<std::string> &arg_set) const
5288 {
5289 arg_set.insert(arg_set.end(), args.begin(), args.end());
5290 }
5291
5292
5293 void
5294 dwarf_derived_probe::emit_privilege_assertion (translator_output* o)
5295 {
5296 if (has_process)
5297 {
5298 // These probes are allowed for unprivileged users, but only in the
5299 // context of processes which they own.
5300 emit_process_owner_assertion (o);
5301 return;
5302 }
5303
5304 // Other probes must contain the default assertion which aborts
5305 // if executed by an unprivileged user.
5306 derived_probe::emit_privilege_assertion (o);
5307 }
5308
5309
5310 void
5311 dwarf_derived_probe::print_dupe_stamp(ostream& o)
5312 {
5313 if (has_process)
5314 {
5315 // These probes are allowed for unprivileged users, but only in the
5316 // context of processes which they own.
5317 print_dupe_stamp_unprivileged_process_owner (o);
5318 return;
5319 }
5320
5321 // Other probes must contain the default dupe stamp
5322 derived_probe::print_dupe_stamp (o);
5323 }
5324
5325
5326 void
5327 dwarf_derived_probe::register_statement_variants(match_node * root,
5328 dwarf_builder * dw,
5329 privilege_t privilege)
5330 {
5331 root
5332 ->bind_privilege(privilege)
5333 ->bind(dw);
5334 root->bind(TOK_NEAREST)
5335 ->bind_privilege(privilege)
5336 ->bind(dw);
5337 }
5338
5339 void
5340 dwarf_derived_probe::register_function_variants(match_node * root,
5341 dwarf_builder * dw,
5342 privilege_t privilege)
5343 {
5344 root
5345 ->bind_privilege(privilege)
5346 ->bind(dw);
5347 root->bind(TOK_CALL)
5348 ->bind_privilege(privilege)
5349 ->bind(dw);
5350 root->bind(TOK_EXPORTED)
5351 ->bind_privilege(privilege)
5352 ->bind(dw);
5353 root->bind(TOK_RETURN)
5354 ->bind_privilege(privilege)
5355 ->bind(dw);
5356
5357 // For process probes / uprobes, .maxactive() is unused.
5358 if (! pr_contains (privilege, pr_stapusr))
5359 {
5360 root->bind(TOK_RETURN)
5361 ->bind_num(TOK_MAXACTIVE)->bind(dw);
5362 }
5363 }
5364
5365 void
5366 dwarf_derived_probe::register_function_and_statement_variants(
5367 systemtap_session& s,
5368 match_node * root,
5369 dwarf_builder * dw,
5370 privilege_t privilege
5371 )
5372 {
5373 // Here we match 4 forms:
5374 //
5375 // .function("foo")
5376 // .function(0xdeadbeef)
5377 // .statement("foo")
5378 // .statement(0xdeadbeef)
5379
5380 match_node *fv_root = root->bind_str(TOK_FUNCTION);
5381 register_function_variants(fv_root, dw, privilege);
5382 // ROOT.function("STRING") always gets the .inline and .label variants.
5383 fv_root->bind(TOK_INLINE)
5384 ->bind_privilege(privilege)
5385 ->bind(dw);
5386 fv_root->bind_str(TOK_LABEL)
5387 ->bind_privilege(privilege)
5388 ->bind(dw);
5389 fv_root->bind_str(TOK_CALLEE)
5390 ->bind_privilege(privilege)
5391 ->bind(dw);
5392 fv_root->bind(TOK_CALLEES)
5393 ->bind_privilege(privilege)
5394 ->bind(dw);
5395 fv_root->bind_num(TOK_CALLEES)
5396 ->bind_privilege(privilege)
5397 ->bind(dw);
5398
5399 fv_root = root->bind_num(TOK_FUNCTION);
5400 register_function_variants(fv_root, dw, privilege);
5401 // ROOT.function(NUMBER).inline is deprecated in release 1.7 and removed thereafter.
5402 if (strverscmp(s.compatible.c_str(), "1.7") <= 0)
5403 {
5404 fv_root->bind(TOK_INLINE)
5405 ->bind_privilege(privilege)
5406 ->bind(dw);
5407 }
5408
5409 register_statement_variants(root->bind_str(TOK_STATEMENT), dw, privilege);
5410 register_statement_variants(root->bind_num(TOK_STATEMENT), dw, privilege);
5411 }
5412
5413 void
5414 dwarf_derived_probe::register_sdt_variants(systemtap_session& s,
5415 match_node * root,
5416 dwarf_builder * dw)
5417 {
5418 root->bind_str(TOK_MARK)
5419 ->bind_privilege(pr_all)
5420 ->bind(dw);
5421 root->bind_str(TOK_PROVIDER)->bind_str(TOK_MARK)
5422 ->bind_privilege(pr_all)
5423 ->bind(dw);
5424 }
5425
5426 void
5427 dwarf_derived_probe::register_plt_variants(systemtap_session& s,
5428 match_node * root,
5429 dwarf_builder * dw)
5430 {
5431 root->bind(TOK_PLT)
5432 ->bind_privilege(pr_all)
5433 ->bind(dw);
5434 root->bind_str(TOK_PLT)
5435 ->bind_privilege(pr_all)
5436 ->bind(dw);
5437
5438 root->bind(TOK_PLT)
5439 ->bind(TOK_RETURN)
5440 ->bind_privilege(pr_all)
5441 ->bind(dw);
5442 root->bind_str(TOK_PLT)
5443 ->bind(TOK_RETURN)
5444 ->bind_privilege(pr_all)
5445 ->bind(dw);
5446 }
5447
5448 void
5449 dwarf_derived_probe::register_patterns(systemtap_session& s)
5450 {
5451 match_node* root = s.pattern_root;
5452 dwarf_builder *dw = new dwarf_builder();
5453
5454 update_visitor *filter = new dwarf_cast_expanding_visitor(s, *dw);
5455 s.code_filters.push_back(filter);
5456
5457 filter = new dwarf_atvar_expanding_visitor(s, *dw);
5458 s.code_filters.push_back(filter);
5459
5460 register_function_and_statement_variants(s, root->bind(TOK_KERNEL), dw, pr_privileged);
5461 register_function_and_statement_variants(s, root->bind_str(TOK_MODULE), dw, pr_privileged);
5462 root->bind(TOK_KERNEL)->bind_num(TOK_STATEMENT)->bind(TOK_ABSOLUTE)
5463 ->bind(dw);
5464
5465 match_node* uprobes[] = {
5466 root->bind(TOK_PROCESS),
5467 root->bind_str(TOK_PROCESS),
5468 root->bind_num(TOK_PROCESS),
5469 root->bind(TOK_PROCESS)->bind_str(TOK_LIBRARY),
5470 root->bind_str(TOK_PROCESS)->bind_str(TOK_LIBRARY),
5471 };
5472 for (size_t i = 0; i < sizeof(uprobes) / sizeof(*uprobes); ++i)
5473 {
5474 register_function_and_statement_variants(s, uprobes[i], dw, pr_all);
5475 register_sdt_variants(s, uprobes[i], dw);
5476 register_plt_variants(s, uprobes[i], dw);
5477 }
5478 }
5479
5480 void
5481 dwarf_derived_probe::emit_probe_local_init(systemtap_session& s, translator_output * o)
5482 {
5483 std::set<string>::iterator pcii;
5484 for (pcii = perf_counter_refs.begin();
5485 pcii != perf_counter_refs.end();
5486 pcii++)
5487 {
5488 std::vector<std::pair<std::string,std::string> >::iterator it;
5489 // Find the associated perf.counter probe
5490 unsigned i = 0;
5491 for (it=s.perf_counters.begin() ;
5492 it != s.perf_counters.end();
5493 it++, i++)
5494 if ((*it).first == (*pcii))
5495 break;
5496 // place the perf counter read so it precedes stp_lock_probe
5497 o->newline() << "l->l___perf_read_" + (*it).first
5498 + " = (((int64_t) (_stp_perf_read(smp_processor_id(),"
5499 + lex_cast(i) + "))));";
5500 }
5501
5502 if (access_vars)
5503 {
5504 // if accessing $variables, emit bsp cache setup for speeding up
5505 o->newline() << "#if defined __ia64__";
5506 o->newline() << "bspcache(c->unwaddr, c->kregs);";
5507 o->newline() << "#endif";
5508 }
5509 }
5510
5511 // ------------------------------------------------------------------------
5512
5513 void
5514 dwarf_derived_probe_group::enroll (dwarf_derived_probe* p)
5515 {
5516 probes_by_module.insert (make_pair (p->module, p));
5517
5518 // XXX: probes put at the same address should all share a
5519 // single kprobe/kretprobe, and have their handlers executed
5520 // sequentially.
5521 }
5522
5523 void
5524 dwarf_derived_probe_group::emit_module_decls (systemtap_session& s)
5525 {
5526 if (probes_by_module.empty()) return;
5527
5528 s.op->newline() << "/* ---- dwarf probes ---- */";
5529
5530 // Let's find some stats for the embedded strings. Maybe they
5531 // are small and uniform enough to justify putting char[MAX]'s into
5532 // the array instead of relocated char*'s.
5533 size_t module_name_max = 0, section_name_max = 0;
5534 size_t module_name_tot = 0, section_name_tot = 0;
5535 size_t all_name_cnt = probes_by_module.size(); // for average
5536 for (p_b_m_iterator it = probes_by_module.begin(); it != probes_by_module.end(); it++)
5537 {
5538 dwarf_derived_probe* p = it->second;
5539 #define DOIT(var,expr) do { \
5540 size_t var##_size = (expr) + 1; \
5541 var##_max = max (var##_max, var##_size); \
5542 var##_tot += var##_size; } while (0)
5543 DOIT(module_name, p->module.size());
5544 DOIT(section_name, p->section.size());
5545 #undef DOIT
5546 }
5547
5548 // Decide whether it's worthwhile to use char[] or char* by comparing
5549 // the amount of average waste (max - avg) to the relocation data size
5550 // (3 native long words).
5551 #define CALCIT(var) \
5552 if ((var##_name_max-(var##_name_tot/all_name_cnt)) < (3 * sizeof(void*))) \
5553 { \
5554 s.op->newline() << "#define STAP_DWARF_PROBE_STR_" << #var << " " \
5555 << "const char " << #var \
5556 << "[" << var##_name_max << "]"; \
5557 if (s.verbose > 2) clog << "stap_dwarf_probe " << #var \
5558 << "[" << var##_name_max << "]" << endl; \
5559 } \
5560 else \
5561 { \
5562 s.op->newline() << "#define STAP_DWARF_PROBE_STR_" << #var << " " \
5563 << "const char * const " << #var << ""; \
5564 if (s.verbose > 2) clog << "stap_dwarf_probe *" << #var << endl; \
5565 }
5566
5567 CALCIT(module);
5568 CALCIT(section);
5569
5570 #undef CALCIT
5571
5572 s.op->newline() << "#include \"linux/kprobes.c\"";
5573
5574 #define UNDEFIT(var) s.op->newline() << "#undef STAP_DWARF_PROBE_STR_" << #var
5575 UNDEFIT(module);
5576 UNDEFIT(section);
5577 #undef UNDEFIT
5578
5579 // Emit an array of kprobe/kretprobe pointers
5580 s.op->newline() << "#if defined(STAPCONF_UNREGISTER_KPROBES)";
5581 s.op->newline() << "static void * stap_unreg_kprobes[" << probes_by_module.size() << "];";
5582 s.op->newline() << "#endif";
5583
5584 // Emit the actual probe list.
5585
5586 // NB: we used to plop a union { struct kprobe; struct kretprobe } into
5587 // struct stap_dwarf_probe, but it being initialized data makes it add
5588 // hundreds of bytes of padding per stap_dwarf_probe. (PR5673)
5589 s.op->newline() << "static struct stap_dwarf_kprobe stap_dwarf_kprobes[" << probes_by_module.size() << "];";
5590 // NB: bss!
5591
5592 s.op->newline() << "static struct stap_dwarf_probe stap_dwarf_probes[] = {";
5593 s.op->indent(1);
5594
5595 size_t stap_dwarf_kprobe_idx = 0;
5596 for (p_b_m_iterator it = probes_by_module.begin(); it != probes_by_module.end(); it++)
5597 {
5598 dwarf_derived_probe* p = it->second;
5599 s.op->newline() << "{";
5600 if (p->has_return)
5601 s.op->line() << " .return_p=1,";
5602 if (p->has_maxactive)
5603 {
5604 s.op->line() << " .maxactive_p=1,";
5605 assert (p->maxactive_val >= 0 && p->maxactive_val <= USHRT_MAX);
5606 s.op->line() << " .maxactive_val=" << p->maxactive_val << ",";
5607 }
5608 if (p->saved_longs || p->saved_strings)
5609 {
5610 if (p->saved_longs)
5611 s.op->line() << " .saved_longs=" << p->saved_longs << ",";
5612 if (p->saved_strings)
5613 s.op->line() << " .saved_strings=" << p->saved_strings << ",";
5614 if (p->entry_handler)
5615 s.op->line() << " .entry_probe=" << common_probe_init (p->entry_handler) << ",";
5616 }
5617 if (p->locations[0]->optional)
5618 s.op->line() << " .optional_p=1,";
5619 s.op->line() << " .address=(unsigned long)0x" << hex << p->addr << dec << "ULL,";
5620 s.op->line() << " .module=\"" << p->module << "\",";
5621 s.op->line() << " .section=\"" << p->section << "\",";
5622 s.op->line() << " .probe=" << common_probe_init (p) << ",";
5623 s.op->line() << " .kprobe=&stap_dwarf_kprobes[" << stap_dwarf_kprobe_idx++ << "],";
5624 s.op->line() << " },";
5625 }
5626
5627 s.op->newline(-1) << "};";
5628
5629 // Emit the kprobes callback function
5630 s.op->newline();
5631 s.op->newline() << "static int enter_kprobe_probe (struct kprobe *inst,";
5632 s.op->line() << " struct pt_regs *regs) {";
5633 // NB: as of PR5673, the kprobe|kretprobe union struct is in BSS
5634 s.op->newline(1) << "int kprobe_idx = ((uintptr_t)inst-(uintptr_t)stap_dwarf_kprobes)/sizeof(struct stap_dwarf_kprobe);";
5635 // Check that the index is plausible
5636 s.op->newline() << "struct stap_dwarf_probe *sdp = &stap_dwarf_probes[";
5637 s.op->line() << "((kprobe_idx >= 0 && kprobe_idx < " << probes_by_module.size() << ")?";
5638 s.op->line() << "kprobe_idx:0)"; // NB: at least we avoid memory corruption
5639 // XXX: it would be nice to give a more verbose error though; BUG_ON later?
5640 s.op->line() << "];";
5641 common_probe_entryfn_prologue (s, "STAP_SESSION_RUNNING", "sdp->probe",
5642 "stp_probe_type_kprobe");
5643 s.op->newline() << "c->kregs = regs;";
5644
5645 // Make it look like the IP is set as it wouldn't have been replaced
5646 // by a breakpoint instruction when calling real probe handler. Reset
5647 // IP regs on return, so we don't confuse kprobes. PR10458
5648 s.op->newline() << "{";
5649 s.op->indent(1);
5650 s.op->newline() << "unsigned long kprobes_ip = REG_IP(c->kregs);";
5651 s.op->newline() << "SET_REG_IP(regs, (unsigned long) inst->addr);";
5652 s.op->newline() << "(*sdp->probe->ph) (c);";
5653 s.op->newline() << "SET_REG_IP(regs, kprobes_ip);";
5654 s.op->newline(-1) << "}";
5655
5656 common_probe_entryfn_epilogue (s, true, otf_safe_context(s));
5657 s.op->newline() << "return 0;";
5658 s.op->newline(-1) << "}";
5659
5660 // Same for kretprobes
5661 s.op->newline();
5662 s.op->newline() << "static int enter_kretprobe_common (struct kretprobe_instance *inst,";
5663 s.op->line() << " struct pt_regs *regs, int entry) {";
5664 s.op->newline(1) << "struct kretprobe *krp = inst->rp;";
5665
5666 // NB: as of PR5673, the kprobe|kretprobe union struct is in BSS
5667 s.op->newline() << "int kprobe_idx = ((uintptr_t)krp-(uintptr_t)stap_dwarf_kprobes)/sizeof(struct stap_dwarf_kprobe);";
5668 // Check that the index is plausible
5669 s.op->newline() << "struct stap_dwarf_probe *sdp = &stap_dwarf_probes[";
5670 s.op->line() << "((kprobe_idx >= 0 && kprobe_idx < " << probes_by_module.size() << ")?";
5671 s.op->line() << "kprobe_idx:0)"; // NB: at least we avoid memory corruption
5672 // XXX: it would be nice to give a more verbose error though; BUG_ON later?
5673 s.op->line() << "];";
5674
5675 s.op->newline() << "const struct stap_probe *sp = entry ? sdp->entry_probe : sdp->probe;";
5676 s.op->newline() << "if (sp) {";
5677 s.op->indent(1);
5678 common_probe_entryfn_prologue (s, "STAP_SESSION_RUNNING", "sp",
5679 "stp_probe_type_kretprobe");
5680 s.op->newline() << "c->kregs = regs;";
5681
5682 // for assisting runtime's backtrace logic and accessing kretprobe data packets
5683 s.op->newline() << "c->ips.krp.pi = inst;";
5684 s.op->newline() << "c->ips.krp.pi_longs = sdp->saved_longs;";
5685
5686 // Make it look like the IP is set as it wouldn't have been replaced
5687 // by a breakpoint instruction when calling real probe handler. Reset
5688 // IP regs on return, so we don't confuse kprobes. PR10458
5689 s.op->newline() << "{";
5690 s.op->newline(1) << "unsigned long kprobes_ip = REG_IP(c->kregs);";
5691 s.op->newline() << "if (entry)";
5692 s.op->newline(1) << "SET_REG_IP(regs, (unsigned long) inst->rp->kp.addr);";
5693 s.op->newline(-1) << "else";
5694 s.op->newline(1) << "SET_REG_IP(regs, (unsigned long)inst->ret_addr);";
5695 s.op->newline(-1) << "(sp->ph) (c);";
5696 s.op->newline() << "SET_REG_IP(regs, kprobes_ip);";
5697 s.op->newline(-1) << "}";
5698
5699 common_probe_entryfn_epilogue (s, true, otf_safe_context(s));
5700 s.op->newline(-1) << "}";
5701 s.op->newline() << "return 0;";
5702 s.op->newline(-1) << "}";
5703
5704 s.op->newline();
5705 }
5706
5707
5708 void
5709 dwarf_derived_probe_group::emit_module_init (systemtap_session& s)
5710 {
5711 if (probes_by_module.empty()) return;
5712
5713 s.op->newline() << "/* ---- dwarf probes ---- */";
5714
5715 // We'll let stapkp_init() handle reporting errors by setting probe_point to
5716 // NULL.
5717 s.op->newline() << "probe_point = NULL;";
5718
5719 s.op->newline() << "rc = stapkp_init( "
5720 << "stap_dwarf_probes, "
5721 << "ARRAY_SIZE(stap_dwarf_probes));";
5722 }
5723
5724
5725 void
5726 dwarf_derived_probe_group::emit_module_refresh (systemtap_session& s)
5727 {
5728 if (probes_by_module.empty()) return;
5729
5730 s.op->newline() << "/* ---- dwarf probes ---- */";
5731
5732 s.op->newline() << "stapkp_refresh( "
5733 << "modname, "
5734 << "stap_dwarf_probes, "
5735 << "ARRAY_SIZE(stap_dwarf_probes));";
5736 }
5737
5738
5739 void
5740 dwarf_derived_probe_group::emit_module_exit (systemtap_session& s)
5741 {
5742 if (probes_by_module.empty()) return;
5743
5744 s.op->newline() << "/* ---- dwarf probes ---- */";
5745
5746 s.op->newline() << "stapkp_exit( "
5747 << "stap_dwarf_probes, "
5748 << "ARRAY_SIZE(stap_dwarf_probes));";
5749 }
5750
5751 static void sdt_v3_tokenize(const string& str, vector<string>& tokens)
5752 {
5753 string::size_type pos;
5754 string::size_type lastPos = str.find_first_not_of(" ", 0);
5755 string::size_type nextAt = str.find("@", lastPos);
5756
5757 if (nextAt == string::npos)
5758 {
5759 // PR13934: Assembly probes are not forced to use the N@OP form.
5760 // In this case, N is inferred to be the native word size. Since we
5761 // don't have a nice delimiter, just split it on spaces. SDT-asm authors
5762 // then must not put any spaces in arguments, to avoid ambiguity.
5763 tokenize(str, tokens, " ");
5764 return;
5765 }
5766
5767 while (lastPos != string::npos)
5768 {
5769 pos = nextAt + 1;
5770 nextAt = str.find("@", pos);
5771 if (nextAt == string::npos)
5772 pos = string::npos;
5773 else
5774 pos = str.rfind(" ", nextAt);
5775
5776 tokens.push_back(str.substr(lastPos, pos - lastPos));
5777 lastPos = str.find_first_not_of(" ", pos);
5778 }
5779 }
5780
5781
5782 struct sdt_uprobe_var_expanding_visitor: public var_expanding_visitor
5783 {
5784 enum regwidths {QI, QIh, HI, SI, DI};
5785 sdt_uprobe_var_expanding_visitor(systemtap_session& s,
5786 dwflpp& dw,
5787 int elf_machine,
5788 const string & process_name,
5789 const string & provider_name,
5790 const string & probe_name,
5791 stap_sdt_probe_type probe_type,
5792 const string & arg_string,
5793 int ac):
5794 session (s), dw (dw), elf_machine (elf_machine),
5795 process_name (process_name), provider_name (provider_name),
5796 probe_name (probe_name), probe_type (probe_type), arg_count ((unsigned) ac)
5797 {
5798 // sanity check that we're not somehow here for a kernel probe
5799 assert(is_user_module(process_name));
5800
5801 build_dwarf_registers();
5802
5803 need_debug_info = false;
5804 if (probe_type == uprobe3_type)
5805 {
5806 sdt_v3_tokenize(arg_string, arg_tokens);
5807 assert(arg_count <= 12);
5808 }
5809 else
5810 {
5811 tokenize(arg_string, arg_tokens, " ");
5812 assert(arg_count <= 10);
5813 }
5814 }
5815
5816 systemtap_session& session;
5817 dwflpp& dw;
5818 int elf_machine;
5819 const string & process_name;
5820 const string & provider_name;
5821 const string & probe_name;
5822 stap_sdt_probe_type probe_type;
5823 unsigned arg_count;
5824 vector<string> arg_tokens;
5825
5826 map<string, pair<unsigned,int> > dwarf_regs;
5827 string regnames;
5828 string percent_regnames;
5829
5830 bool need_debug_info;
5831
5832 void build_dwarf_registers();
5833 void visit_target_symbol (target_symbol* e);
5834 unsigned get_target_symbol_argno_and_validate (target_symbol* e);
5835 long parse_out_arg_precision(string& asmarg);
5836 expression* try_parse_arg_literal (target_symbol *e,
5837 const string& asmarg,
5838 long precision);
5839 expression* try_parse_arg_register (target_symbol *e,
5840 const string& asmarg,
5841 long precision);
5842 expression* try_parse_arg_offset_register (target_symbol *e,
5843 const string& asmarg,
5844 long precision);
5845 expression* try_parse_arg_effective_addr (target_symbol *e,
5846 const string& asmarg,
5847 long precision);
5848 expression* try_parse_arg_varname (target_symbol *e,
5849 const string& asmarg,
5850 long precision);
5851 void visit_target_symbol_arg (target_symbol* e);
5852 void visit_target_symbol_context (target_symbol* e);
5853 void visit_atvar_op (atvar_op* e);
5854 void visit_cast_op (cast_op* e);
5855 };
5856
5857 void
5858 sdt_uprobe_var_expanding_visitor::build_dwarf_registers ()
5859 {
5860 /* Register name mapping table depends on the elf machine of this particular
5861 probe target process/file, not upon the host. So we can't just
5862 #ifdef _i686_ etc. */
5863
5864 #define DRI(name,num,width) dwarf_regs[name]=make_pair(num,width)
5865 if (elf_machine == EM_X86_64) {
5866 DRI ("%rax", 0, DI); DRI ("%eax", 0, SI); DRI ("%ax", 0, HI);
5867 DRI ("%al", 0, QI); DRI ("%ah", 0, QIh);
5868 DRI ("%rdx", 1, DI); DRI ("%edx", 1, SI); DRI ("%dx", 1, HI);
5869 DRI ("%dl", 1, QI); DRI ("%dh", 1, QIh);
5870 DRI ("%rcx", 2, DI); DRI ("%ecx", 2, SI); DRI ("%cx", 2, HI);
5871 DRI ("%cl", 2, QI); DRI ("%ch", 2, QIh);
5872 DRI ("%rbx", 3, DI); DRI ("%ebx", 3, SI); DRI ("%bx", 3, HI);
5873 DRI ("%bl", 3, QI); DRI ("%bh", 3, QIh);
5874 DRI ("%rsi", 4, DI); DRI ("%esi", 4, SI); DRI ("%si", 4, HI);
5875 DRI ("%sil", 4, QI);
5876 DRI ("%rdi", 5, DI); DRI ("%edi", 5, SI); DRI ("%di", 5, HI);
5877 DRI ("%dil", 5, QI);
5878 DRI ("%rbp", 6, DI); DRI ("%ebp", 6, SI); DRI ("%bp", 6, HI);
5879 DRI ("%bpl", 6, QI);
5880 DRI ("%rsp", 7, DI); DRI ("%esp", 7, SI); DRI ("%sp", 7, HI);
5881 DRI ("%spl", 7, QI);
5882 DRI ("%r8", 8, DI); DRI ("%r8d", 8, SI); DRI ("%r8w", 8, HI);
5883 DRI ("%r8b", 8, QI);
5884 DRI ("%r9", 9, DI); DRI ("%r9d", 9, SI); DRI ("%r9w", 9, HI);
5885 DRI ("%r9b", 9, QI);
5886 DRI ("%r10", 10, DI); DRI ("%r10d", 10, SI); DRI ("%r10w", 10, HI);
5887 DRI ("%r10b", 10, QI);
5888 DRI ("%r11", 11, DI); DRI ("%r11d", 11, SI); DRI ("%r11w", 11, HI);
5889 DRI ("%r11b", 11, QI);
5890 DRI ("%r12", 12, DI); DRI ("%r12d", 12, SI); DRI ("%r12w", 12, HI);
5891 DRI ("%r12b", 12, QI);
5892 DRI ("%r13", 13, DI); DRI ("%r13d", 13, SI); DRI ("%r13w", 13, HI);
5893 DRI ("%r13b", 13, QI);
5894 DRI ("%r14", 14, DI); DRI ("%r14d", 14, SI); DRI ("%r14w", 14, HI);
5895 DRI ("%r14b", 14, QI);
5896 DRI ("%r15", 15, DI); DRI ("%r15d", 15, SI); DRI ("%r15w", 15, HI);
5897 DRI ("%r15b", 15, QI);
5898 DRI ("%rip", 16, DI); DRI ("%eip", 16, SI); DRI ("%ip", 16, HI);
5899 } else if (elf_machine == EM_386) {
5900 DRI ("%eax", 0, SI); DRI ("%ax", 0, HI); DRI ("%al", 0, QI);
5901 DRI ("%ah", 0, QIh);
5902 DRI ("%ecx", 1, SI); DRI ("%cx", 1, HI); DRI ("%cl", 1, QI);
5903 DRI ("%ch", 1, QIh);
5904 DRI ("%edx", 2, SI); DRI ("%dx", 2, HI); DRI ("%dl", 2, QI);
5905 DRI ("%dh", 2, QIh);
5906 DRI ("%ebx", 3, SI); DRI ("%bx", 3, HI); DRI ("%bl", 3, QI);
5907 DRI ("%bh", 3, QIh);
5908 DRI ("%esp", 4, SI); DRI ("%sp", 4, HI);
5909 DRI ("%ebp", 5, SI); DRI ("%bp", 5, HI);
5910 DRI ("%esi", 6, SI); DRI ("%si", 6, HI); DRI ("%sil", 6, QI);
5911 DRI ("%edi", 7, SI); DRI ("%di", 7, HI); DRI ("%dil", 7, QI);
5912 } else if (elf_machine == EM_PPC || elf_machine == EM_PPC64) {
5913 DRI ("%r0", 0, DI);
5914 DRI ("%r1", 1, DI);
5915 DRI ("%r2", 2, DI);
5916 DRI ("%r3", 3, DI);
5917 DRI ("%r4", 4, DI);
5918 DRI ("%r5", 5, DI);
5919 DRI ("%r6", 6, DI);
5920 DRI ("%r7", 7, DI);
5921 DRI ("%r8", 8, DI);
5922 DRI ("%r9", 9, DI);
5923 DRI ("%r10", 10, DI);
5924 DRI ("%r11", 11, DI);
5925 DRI ("%r12", 12, DI);
5926 DRI ("%r13", 13, DI);
5927 DRI ("%r14", 14, DI);
5928 DRI ("%r15", 15, DI);
5929 DRI ("%r16", 16, DI);
5930 DRI ("%r17", 17, DI);
5931 DRI ("%r18", 18, DI);
5932 DRI ("%r19", 19, DI);
5933 DRI ("%r20", 20, DI);
5934 DRI ("%r21", 21, DI);
5935 DRI ("%r22", 22, DI);
5936 DRI ("%r23", 23, DI);
5937 DRI ("%r24", 24, DI);
5938 DRI ("%r25", 25, DI);
5939 DRI ("%r26", 26, DI);
5940 DRI ("%r27", 27, DI);
5941 DRI ("%r28", 28, DI);
5942 DRI ("%r29", 29, DI);
5943 DRI ("%r30", 30, DI);
5944 DRI ("%r31", 31, DI);
5945 // PR11821: unadorned register "names" without -mregnames
5946 DRI ("0", 0, DI);
5947 DRI ("1", 1, DI);
5948 DRI ("2", 2, DI);
5949 DRI ("3", 3, DI);
5950 DRI ("4", 4, DI);
5951 DRI ("5", 5, DI);
5952 DRI ("6", 6, DI);
5953 DRI ("7", 7, DI);
5954 DRI ("8", 8, DI);
5955 DRI ("9", 9, DI);
5956 DRI ("10", 10, DI);
5957 DRI ("11", 11, DI);
5958 DRI ("12", 12, DI);
5959 DRI ("13", 13, DI);
5960 DRI ("14", 14, DI);
5961 DRI ("15", 15, DI);
5962 DRI ("16", 16, DI);
5963 DRI ("17", 17, DI);
5964 DRI ("18", 18, DI);
5965 DRI ("19", 19, DI);
5966 DRI ("20", 20, DI);
5967 DRI ("21", 21, DI);
5968 DRI ("22", 22, DI);
5969 DRI ("23", 23, DI);
5970 DRI ("24", 24, DI);
5971 DRI ("25", 25, DI);
5972 DRI ("26", 26, DI);
5973 DRI ("27", 27, DI);
5974 DRI ("28", 28, DI);
5975 DRI ("29", 29, DI);
5976 DRI ("30", 30, DI);
5977 DRI ("31", 31, DI);
5978 } else if (elf_machine == EM_S390) {
5979 DRI ("%r0", 0, DI);
5980 DRI ("%r1", 1, DI);
5981 DRI ("%r2", 2, DI);
5982 DRI ("%r3", 3, DI);
5983 DRI ("%r4", 4, DI);
5984 DRI ("%r5", 5, DI);
5985 DRI ("%r6", 6, DI);
5986 DRI ("%r7", 7, DI);
5987 DRI ("%r8", 8, DI);
5988 DRI ("%r9", 9, DI);
5989 DRI ("%r10", 10, DI);
5990 DRI ("%r11", 11, DI);
5991 DRI ("%r12", 12, DI);
5992 DRI ("%r13", 13, DI);
5993 DRI ("%r14", 14, DI);
5994 DRI ("%r15", 15, DI);
5995 } else if (elf_machine == EM_ARM) {
5996 DRI ("r0", 0, SI);
5997 DRI ("r1", 1, SI);
5998 DRI ("r2", 2, SI);
5999 DRI ("r3", 3, SI);
6000 DRI ("r4", 4, SI);
6001 DRI ("r5", 5, SI);
6002 DRI ("r6", 6, SI);
6003 DRI ("r7", 7, SI);
6004 DRI ("r8", 8, SI);
6005 DRI ("r9", 9, SI);
6006 DRI ("r10", 10, SI); DRI ("sl", 10, SI);
6007 DRI ("fp", 11, SI);
6008 DRI ("ip", 12, SI);
6009 DRI ("sp", 13, SI);
6010 DRI ("lr", 14, SI);
6011 DRI ("pc", 15, SI);
6012 } else if (elf_machine == EM_AARCH64) {
6013 DRI ("x0", 0, DI); DRI ("w0", 0, SI);
6014 DRI ("x1", 1, DI); DRI ("w1", 1, SI);
6015 DRI ("x2", 2, DI); DRI ("w2", 2, SI);
6016 DRI ("x3", 3, DI); DRI ("w3", 3, SI);
6017 DRI ("x4", 4, DI); DRI ("w4", 4, SI);
6018 DRI ("x5", 5, DI); DRI ("w5", 5, SI);
6019 DRI ("x6", 6, DI); DRI ("w6", 6, SI);
6020 DRI ("x7", 7, DI); DRI ("w7", 7, SI);
6021 DRI ("x8", 8, DI); DRI ("w8", 8, SI);
6022 DRI ("x9", 9, DI); DRI ("w9", 9, SI);
6023 DRI ("x10", 10, DI); DRI ("w10", 10, SI);
6024 DRI ("x11", 11, DI); DRI ("w11", 11, SI);
6025 DRI ("x12", 12, DI); DRI ("w12", 12, SI);
6026 DRI ("x13", 13, DI); DRI ("w13", 13, SI);
6027 DRI ("x14", 14, DI); DRI ("w14", 14, SI);
6028 DRI ("x15", 15, DI); DRI ("w15", 15, SI);
6029 DRI ("x16", 16, DI); DRI ("w16", 16, SI);
6030 DRI ("x17", 17, DI); DRI ("w17", 17, SI);
6031 DRI ("x18", 18, DI); DRI ("w18", 18, SI);
6032 DRI ("x19", 19, DI); DRI ("w19", 19, SI);
6033 DRI ("x20", 20, DI); DRI ("w20", 20, SI);
6034 DRI ("x21", 21, DI); DRI ("w21", 21, SI);
6035 DRI ("x22", 22, DI); DRI ("w22", 22, SI);
6036 DRI ("x23", 23, DI); DRI ("w23", 23, SI);
6037 DRI ("x24", 24, DI); DRI ("w24", 24, SI);
6038 DRI ("x25", 25, DI); DRI ("w25", 25, SI);
6039 DRI ("x26", 26, DI); DRI ("w26", 26, SI);
6040 DRI ("x27", 27, DI); DRI ("w27", 27, SI);
6041 DRI ("x28", 28, DI); DRI ("w28", 28, SI);
6042 DRI ("x29", 29, DI); DRI ("w29", 29, SI);
6043 DRI ("x30", 30, DI); DRI ("w30", 30, SI);
6044 DRI ("sp", 31, DI);
6045 } else if (arg_count) {
6046 /* permit this case; just fall back to dwarf */
6047 }
6048 #undef DRI
6049
6050 // Build regex pieces out of the known dwarf_regs. We keep two separate
6051 // lists: ones with the % prefix (and thus unambigiuous even despite PR11821),
6052 // and ones with no prefix (and thus only usable in unambiguous contexts).
6053 map<string,pair<unsigned,int> >::const_iterator ri;
6054 for (ri = dwarf_regs.begin(); ri != dwarf_regs.end(); ri++)
6055 {
6056 string regname = ri->first;
6057 assert (regname != "");
6058 regnames += string("|")+regname;
6059 if (regname[0]=='%')
6060 percent_regnames += string("|")+regname;
6061 }
6062
6063 // clip off leading |
6064 if (regnames != "")
6065 regnames = regnames.substr(1);
6066 if (percent_regnames != "")
6067 percent_regnames = percent_regnames.substr(1);
6068 }
6069
6070 void
6071 sdt_uprobe_var_expanding_visitor::visit_target_symbol_context (target_symbol* e)
6072 {
6073 if (e->addressof)
6074 throw SEMANTIC_ERROR(_("cannot take address of context variable"), e->tok);
6075
6076 if (e->name == "$$name")
6077 {
6078 literal_string *myname = new literal_string (probe_name);
6079 myname->tok = e->tok;
6080 provide(myname);
6081 return;
6082 }
6083
6084 else if (e->name == "$$provider")
6085 {
6086 literal_string *myname = new literal_string (provider_name);
6087 myname->tok = e->tok;
6088 provide(myname);
6089 return;
6090 }
6091
6092 else if (e->name == "$$vars" || e->name == "$$parms")
6093 {
6094 e->assert_no_components("sdt", true);
6095
6096 // Convert $$vars to sprintf of a list of vars which we recursively evaluate
6097
6098 print_format* pf = print_format::create(e->tok, "sprintf");
6099
6100 for (unsigned i = 1; i <= arg_count; ++i)
6101 {
6102 if (i > 1)
6103 pf->raw_components += " ";
6104 target_symbol *tsym = new target_symbol;
6105 tsym->tok = e->tok;
6106 tsym->name = "$arg" + lex_cast(i);
6107 pf->raw_components += tsym->name;
6108 tsym->components = e->components;
6109
6110 expression *texp = require<expression> (tsym);
6111 if (e->check_pretty_print ())
6112 pf->raw_components += "=%s";
6113 else
6114 pf->raw_components += "=%#x";
6115 pf->args.push_back(texp);
6116 }
6117
6118 pf->components = print_format::string_to_components(pf->raw_components);
6119 provide (pf);
6120 }
6121 else
6122 assert(0); // shouldn't get here
6123 }
6124
6125 unsigned
6126 sdt_uprobe_var_expanding_visitor::get_target_symbol_argno_and_validate (target_symbol *e)
6127 {
6128 // parsing
6129 unsigned argno = 0;
6130 if (startswith(e->name, "$arg"))
6131 {
6132 try
6133 {
6134 argno = lex_cast<unsigned>(e->name.substr(4));
6135 }
6136 catch (const runtime_error& f)
6137 {
6138 // non-integral $arg suffix: e.g. $argKKKSDF
6139 argno = 0;
6140 }
6141 }
6142
6143 // validation
6144 if (arg_count == 0 || // a sdt.h variant without .probe-stored arg_count
6145 argno < 1 || argno > arg_count) // a $argN with out-of-range N
6146 {
6147 // NB: Either
6148 // 1) uprobe1_type $argN or $FOO (we don't know the arg_count)
6149 // 2) uprobe2_type $FOO (no probe args)
6150 // both of which get resolved later.
6151 // Throw it now, and it might be resolved by DWARF later.
6152 need_debug_info = true;
6153 throw SEMANTIC_ERROR(_("target-symbol requires debuginfo"), e->tok);
6154 }
6155 assert (arg_tokens.size() >= argno);
6156 return argno;
6157 }
6158
6159 long
6160 sdt_uprobe_var_expanding_visitor::parse_out_arg_precision(string& asmarg)
6161 {
6162 long precision;
6163 if (asmarg.find('@') != string::npos)
6164 {
6165 precision = lex_cast<int>(asmarg.substr(0, asmarg.find('@')));
6166 asmarg = asmarg.substr(asmarg.find('@')+1);
6167 }
6168 else
6169 {
6170 // V1/V2 do not have precision field so default to signed long
6171 // V3 asm does not have precision field so default to unsigned long
6172 if (probe_type == uprobe3_type)
6173 precision = sizeof(long); // this is an asm probe
6174 else
6175 precision = -sizeof(long);
6176 }
6177 return precision;
6178 }
6179
6180 expression*
6181 sdt_uprobe_var_expanding_visitor::try_parse_arg_literal (target_symbol *e,
6182 const string& asmarg,
6183 long precision)
6184 {
6185 expression *argexpr = NULL;
6186
6187 // Here, we test for a numeric literal.
6188 // Only accept (signed) decimals throughout. XXX
6189
6190 // PR11821. NB: on powerpc, literals are not prefixed with $,
6191 // so this regex does not match. But that's OK, since without
6192 // -mregnames, we can't tell them apart from register numbers
6193 // anyway. With -mregnames, we could, if gcc somehow
6194 // communicated to us the presence of that option, but alas it
6195 // doesn't. http://gcc.gnu.org/PR44995.
6196 vector<string> matches;
6197 if (!regexp_match (asmarg, "^[i\\$#][-]?[0-9][0-9]*$", matches))
6198 {
6199 string sn = matches[0].substr(1);
6200 int64_t n;
6201
6202 // We have to pay attention to the size & sign, as gcc sometimes
6203 // propagates constants that don't quite match, like a negative
6204 // value to fill an unsigned type.
6205 // NB: let it throw if something happens
6206 switch (precision)
6207 {
6208 case -1: n = lex_cast< int8_t>(sn); break;
6209 case 1: n = lex_cast< uint8_t>(sn); break;
6210 case -2: n = lex_cast< int16_t>(sn); break;
6211 case 2: n = lex_cast<uint16_t>(sn); break;
6212 case -4: n = lex_cast< int32_t>(sn); break;
6213 case 4: n = lex_cast<uint32_t>(sn); break;
6214 default:
6215 case -8: n = lex_cast< int64_t>(sn); break;
6216 case 8: n = lex_cast<uint64_t>(sn); break;
6217 }
6218
6219 literal_number* ln = new literal_number(n);
6220 ln->tok = e->tok;
6221 argexpr = ln;
6222 }
6223
6224 return argexpr;
6225 }
6226
6227 expression*
6228 sdt_uprobe_var_expanding_visitor::try_parse_arg_register (target_symbol *e,
6229 const string& asmarg,
6230 long precision)
6231 {
6232 expression *argexpr = NULL;
6233
6234 // test for REGISTER
6235 // NB: Because PR11821, we must use percent_regnames here.
6236 string regexp;
6237 if (elf_machine == EM_PPC || elf_machine == EM_PPC64 || elf_machine == EM_ARM)
6238 regexp = "^(" + regnames + ")$";
6239 else
6240 regexp = "^(" + percent_regnames + ")$";
6241
6242 vector<string> matches;
6243 if (!regexp_match(asmarg, regexp, matches))
6244 {
6245 string regname = matches[1];
6246 map<string,pair<unsigned,int> >::iterator ri = dwarf_regs.find (regname);
6247 if (ri != dwarf_regs.end()) // known register
6248 {
6249 embedded_expr *get_arg1 = new embedded_expr;
6250 string width_adjust;
6251 switch (ri->second.second)
6252 {
6253 case QI: width_adjust = ") & 0xff)"; break;
6254 case QIh: width_adjust = ">>8) & 0xff)"; break;
6255 case HI:
6256 // preserve 16 bit register signness
6257 width_adjust = ") & 0xffff)";
6258 if (precision < 0)
6259 width_adjust += " << 48 >> 48";
6260 break;
6261 case SI:
6262 // preserve 32 bit register signness
6263 width_adjust = ") & 0xffffffff)";
6264 if (precision < 0)
6265 width_adjust += " << 32 >> 32";
6266 break;
6267 default: width_adjust = "))";
6268 }
6269 string type = "";
6270 if (probe_type == uprobe3_type)
6271 type = (precision < 0
6272 ? "(int" : "(uint") + lex_cast(abs(precision) * 8) + "_t)";
6273 type = type + "((";
6274 get_arg1->tok = e->tok;
6275 get_arg1->code = string("/* unprivileged */ /* pure */")
6276 + string(" ((int64_t)") + type
6277 + string("u_fetch_register(")
6278 + lex_cast(dwarf_regs[regname].first) + string("))")
6279 + width_adjust;
6280 argexpr = get_arg1;
6281 }
6282 }
6283 return argexpr;
6284 }
6285
6286 static string
6287 precision_to_function(long precision)
6288 {
6289 switch (precision)
6290 {
6291 case 1: case -1:
6292 return "user_int8";
6293 case 2:
6294 return "user_uint16";
6295 case -2:
6296 return "user_int16";
6297 case 4:
6298 return "user_uint32";
6299 case -4:
6300 return "user_int32";
6301 case 8: case -8:
6302 return "user_int64";
6303 default:
6304 return "user_long";
6305 }
6306 }
6307
6308 expression*
6309 sdt_uprobe_var_expanding_visitor::try_parse_arg_offset_register (target_symbol *e,
6310 const string& asmarg,
6311 long precision)
6312 {
6313 expression *argexpr = NULL;
6314
6315 // test for OFFSET(REGISTER) where OFFSET is +-N+-N+-N
6316 // NB: Despite PR11821, we can use regnames here, since the parentheses
6317 // make things unambiguous. (Note: gdb/stap-probe.c also parses this)
6318 // On ARM test for [REGISTER, OFFSET]
6319
6320 string regexp;
6321 int reg, offset1;
6322 if (elf_machine == EM_ARM)
6323 {
6324 regexp = "^\\[(" + regnames + ")(, #([+-]?[0-9]+)([+-][0-9]*)?([+-][0-9]*)?)?\\]$";
6325 reg = 1;
6326 offset1 = 3;
6327 }
6328 else
6329 {
6330 regexp = "^([+-]?[0-9]*)([+-][0-9]*)?([+-][0-9]*)?[(](" + regnames + ")[)]$";
6331 reg = 4;
6332 offset1 = 1;
6333 }
6334
6335 vector<string> matches;
6336 if (!regexp_match(asmarg, regexp, matches))
6337 {
6338 string regname;
6339 int64_t disp = 0;
6340 if (matches[reg].length())
6341 regname = matches[reg];
6342 if (dwarf_regs.find (regname) == dwarf_regs.end())
6343 throw SEMANTIC_ERROR(_F("unrecognized register '%s'", regname.c_str()));
6344
6345 for (int i=offset1; i <= (offset1 + 2); i++)
6346 if (matches[i].length())
6347 // should decode positive/negative hex/decimal
6348 // NB: let it throw if something happens
6349 disp += lex_cast<int64_t>(matches[i]);
6350
6351 // synthesize user_long(%{fetch_register(R)%} + D)
6352 embedded_expr *get_arg1 = new embedded_expr;
6353 get_arg1->tok = e->tok;
6354 get_arg1->code = string("/* unprivileged */ /* pure */")
6355 + string("u_fetch_register(")
6356 + lex_cast(dwarf_regs[regname].first) + string(")");
6357 // XXX: may we ever need to cast that to a narrower type?
6358
6359 literal_number* inc = new literal_number(disp);
6360 inc->tok = e->tok;
6361
6362 binary_expression *be = new binary_expression;
6363 be->tok = e->tok;
6364 be->left = get_arg1;
6365 be->op = "+";
6366 be->right = inc;
6367
6368 functioncall *fc = new functioncall;
6369 fc->function = precision_to_function(precision);
6370 fc->tok = e->tok;
6371 fc->args.push_back(be);
6372
6373 argexpr = fc;
6374 }
6375
6376 return argexpr;
6377 }
6378
6379 expression*
6380 sdt_uprobe_var_expanding_visitor::try_parse_arg_effective_addr (target_symbol *e,
6381 const string& asmarg,
6382 long precision)
6383 {
6384 expression *argexpr = NULL;
6385
6386 // test for OFFSET(BASE_REGISTER,INDEX_REGISTER[,SCALE]) where OFFSET is +-N+-N+-N
6387 // NB: Despite PR11821, we can use regnames here, since the parentheses
6388 // make things unambiguous. (Note: gdb/stap-probe.c also parses this)
6389 string regexp = "^([+-]?[0-9]*)([+-][0-9]*)?([+-][0-9]*)?[(](" + regnames + "),(" +
6390 regnames + ")(,[1248])?[)]$";
6391 vector<string> matches;
6392 if (!regexp_match(asmarg, regexp, matches))
6393 {
6394 string baseregname;
6395 string indexregname;
6396 int64_t disp = 0;
6397 short scale = 1;
6398
6399 if (matches[6].length())
6400 // NB: let it throw if we can't cast
6401 scale = lex_cast<short>(matches[6].substr(1)); // NB: skip the comma!
6402
6403 if (matches[4].length())
6404 baseregname = matches[4];
6405 if (dwarf_regs.find (baseregname) == dwarf_regs.end())
6406 throw SEMANTIC_ERROR(_F("unrecognized base register '%s'", baseregname.c_str()));
6407
6408 if (matches[5].length())
6409 indexregname = matches[5];
6410 if (dwarf_regs.find (indexregname) == dwarf_regs.end())
6411 throw SEMANTIC_ERROR(_F("unrecognized index register '%s'", indexregname.c_str()));
6412
6413 for (int i = 1; i <= 3; i++) // up to three OFFSET terms
6414 if (matches[i].length())
6415 // should decode positive/negative hex/decimal
6416 // NB: let it throw if something happens
6417 disp += lex_cast<int64_t>(matches[i]);
6418
6419 // synthesize user_long(%{fetch_register(R1)+fetch_register(R2)*N%} + D)
6420
6421 embedded_expr *get_arg1 = new embedded_expr;
6422 string regfn = "u_fetch_register";
6423
6424 get_arg1->tok = e->tok;
6425 get_arg1->code = string("/* unprivileged */ /* pure */")
6426 + regfn + string("(")+lex_cast(dwarf_regs[baseregname].first)+string(")")
6427 + string("+(")
6428 + regfn + string("(")+lex_cast(dwarf_regs[indexregname].first)+string(")")
6429 + string("*")
6430 + lex_cast(scale)
6431 + string(")");
6432
6433 // NB: could plop this +DISPLACEMENT bit into the embedded-c expression too
6434 literal_number* inc = new literal_number(disp);
6435 inc->tok = e->tok;
6436
6437 binary_expression *be = new binary_expression;
6438 be->tok = e->tok;
6439 be->left = get_arg1;
6440 be->op = "+";
6441 be->right = inc;
6442
6443 functioncall *fc = new functioncall;
6444 fc->function = precision_to_function(precision);
6445 fc->tok = e->tok;
6446 fc->args.push_back(be);
6447
6448 argexpr = fc;
6449 }
6450
6451 return argexpr;
6452 }
6453
6454 expression*
6455 sdt_uprobe_var_expanding_visitor::try_parse_arg_varname (target_symbol *e,
6456 const string& asmarg,
6457 long precision)
6458 {
6459 static unsigned tick = 0;
6460 expression *argexpr = NULL;
6461
6462 // test for [OFF+]VARNAME[+OFF][(REGISTER)], where VARNAME is a variable
6463 // name. NB: Despite PR11821, we can use regnames here, since the parentheses
6464 // make things unambiguous.
6465 string regex = "^(([0-9]+)[+])?([a-zA-Z_][a-zA-Z0-9_]*)([+][0-9]+)?([(]("
6466 + regnames + ")[)])?$";
6467 vector<string> matches;
6468 if (!regexp_match(asmarg, regex, matches))
6469 {
6470 assert(matches.size() >= 4);
6471 string varname = matches[3];
6472
6473 // OFF can be before VARNAME (put in matches[2]) or after (put in
6474 // matches[4]) (or both?). Seems like in most cases it comes after,
6475 // unless the code was compiled with -fPIC.
6476 int64_t offset = 0;
6477 if (!matches[2].empty())
6478 offset += lex_cast<int64_t>(matches[2]);
6479 if (matches.size() >= 5 && !matches[4].empty())
6480 offset += lex_cast<int64_t>(matches[4]);
6481
6482 string regname;
6483 if (matches.size() >= 7)
6484 regname = matches[6];
6485
6486 // If it's just VARNAME, then proceed. If it's VARNAME(REGISTER), then
6487 // only proceed if it's RIP-relative addressing on x86_64.
6488 if (regname.empty() || (regname == "%rip" && elf_machine == EM_X86_64))
6489 {
6490 dw.mod_info->get_symtab();
6491 if (dw.mod_info->symtab_status != info_present)
6492 throw SEMANTIC_ERROR(_("can't retrieve symbol table"));
6493
6494 assert(dw.mod_info->sym_table);
6495 map<string, Dwarf_Addr>& globals = dw.mod_info->sym_table->globals;
6496 map<string, Dwarf_Addr>& locals = dw.mod_info->sym_table->locals;
6497 Dwarf_Addr addr = 0;
6498
6499 // check symtab locals then globals
6500 if (locals.count(varname))
6501 addr = locals[varname];
6502 if (globals.count(varname))
6503 addr = globals[varname];
6504
6505 if (addr)
6506 {
6507 // add whatever offset is in the operand
6508 addr += offset;
6509
6510 // adjust for dw bias because relocate_address() expects a
6511 // libdw address and this addr is from the symtab
6512 dw.get_module_dwarf(false, false);
6513 addr -= dw.module_bias;
6514
6515 string reloc_section;
6516 Dwarf_Addr reloc_addr = dw.relocate_address(addr, reloc_section);
6517
6518 // OK, we have an address for the variable. Let's create a
6519 // function that will just relocate it at runtime, and then
6520 // call user_[u]int*() on the address it returns.
6521
6522 functioncall *user_int_call = new functioncall;
6523 user_int_call->function = precision_to_function(precision);
6524 user_int_call->tok = e->tok;
6525
6526 functiondecl *get_addr_decl = new functiondecl;
6527 get_addr_decl->tok = e->tok;
6528 get_addr_decl->synthetic = true;
6529 get_addr_decl->name = "_sdt_arg_get_addr_" + lex_cast(tick++);
6530 get_addr_decl->type = pe_long;
6531
6532 // build _stp_umodule_relocate(module, addr, current)
6533 stringstream ss;
6534 ss << " /* unprivileged */ /* pure */ /* pragma:vma */" << endl;
6535 ss << "STAP_RETURN(_stp_umodule_relocate(";
6536 ss << "\"" << path_remove_sysroot(session, process_name) << "\", ";
6537 ss << "0x" << hex << reloc_addr << dec << ", ";
6538 ss << "current";
6539 ss << "));" << endl;
6540
6541 embeddedcode *ec = new embeddedcode;
6542 ec->tok = e->tok;
6543 ec->code = ss.str();
6544 get_addr_decl->body = ec;
6545 get_addr_decl->join(session);
6546
6547 functioncall *get_addr_call = new functioncall;
6548 get_addr_call->tok = e->tok;
6549 get_addr_call->function = get_addr_decl->name;
6550 user_int_call->args.push_back(get_addr_call);
6551
6552 argexpr = user_int_call;
6553 }
6554 }
6555 }
6556
6557 return argexpr;
6558 }
6559
6560 void
6561 sdt_uprobe_var_expanding_visitor::visit_target_symbol_arg (target_symbol *e)
6562 {
6563 try
6564 {
6565 unsigned argno = get_target_symbol_argno_and_validate(e); // the N in $argN
6566 string asmarg = arg_tokens[argno-1]; // $arg1 => arg_tokens[0]
6567
6568 // Now we try to parse this thing, which is an assembler operand
6569 // expression. If we can't, we warn, back down to need_debug_info
6570 // and hope for the best. Here is the syntax for a few architectures.
6571 // Note that the power iN syntax is only for V3 sdt.h; gcc emits the i.
6572 //
6573 // literal reg reg reg+ base+index*size+ VAR VAR+off RIP-relative
6574 // indirect offset offset VAR+off
6575 // x86 $N %rR (%rR) N(%rR) O(%bR,%iR,S) var var+off var+off(%rip)
6576 // x86_64 $N %rR (%rR) N(%rR) O(%bR,%iR,S) var var+off var+off(%rip)
6577 // power iN R (R) N(R)
6578 // ia64 N rR [r16]
6579 // s390 N %rR 0(rR) N(r15)
6580 // arm #N rR [rR] [rR, #N]
6581
6582 expression* argexpr = 0; // filled in in case of successful parse
6583
6584 // Parse (and remove from asmarg) the leading length
6585 long precision = parse_out_arg_precision(asmarg);
6586
6587 try
6588 {
6589 if ((argexpr = try_parse_arg_literal(e, asmarg, precision)) != NULL)
6590 goto matched;
6591
6592 // all other matches require registers
6593 if (regnames == "")
6594 throw SEMANTIC_ERROR("no registers to use for parsing");
6595
6596 if ((argexpr = try_parse_arg_register(e, asmarg, precision)) != NULL)
6597 goto matched;
6598 if ((argexpr = try_parse_arg_offset_register(e, asmarg, precision)) != NULL)
6599 goto matched;
6600 if ((argexpr = try_parse_arg_effective_addr(e, asmarg, precision)) != NULL)
6601 goto matched;
6602 if ((argexpr = try_parse_arg_varname(e, asmarg, precision)) != NULL)
6603 goto matched;
6604 }
6605 catch (const semantic_error& err)
6606 {
6607 e->chain(err);
6608 }
6609
6610 // The asmarg operand was not recognized. Back down to dwarf.
6611 if (! session.suppress_warnings)
6612 {
6613 if (probe_type == UPROBE3_TYPE)
6614 session.print_warning (_F("Can't parse SDT_V3 operand '%s' "
6615 "[man error::sdt]", asmarg.c_str()),
6616 e->tok);
6617 else // must be *PROBE2; others don't get asm operands
6618 session.print_warning (_F("Downgrading SDT_V2 probe argument to "
6619 "dwarf, can't parse '%s' [man error::sdt]",
6620 asmarg.c_str()),
6621 e->tok);
6622 }
6623
6624 need_debug_info = true;
6625 throw SEMANTIC_ERROR(_("SDT asm not understood, requires debuginfo "
6626 "[man error::sdt]"), e->tok);
6627
6628 /* NOTREACHED */
6629
6630 matched:
6631 assert (argexpr != 0);
6632
6633 if (session.verbose > 2)
6634 //TRANSLATORS: We're mapping the operand to a new expression*.
6635 clog << _F("mapped asm operand %s to ", asmarg.c_str()) << *argexpr << endl;
6636
6637 if (e->components.empty()) // We have a scalar
6638 {
6639 if (e->addressof)
6640 throw SEMANTIC_ERROR(_("cannot take address of sdt variable"), e->tok);
6641 provide (argexpr);
6642 }
6643 else // $var->foo
6644 {
6645 cast_op *cast = new cast_op;
6646 cast->name = "@cast";
6647 cast->tok = e->tok;
6648 cast->operand = argexpr;
6649 cast->components = e->components;
6650 cast->type_name = probe_name + "_arg" + lex_cast(argno);
6651 cast->module = process_name;
6652 cast->visit(this);
6653 }
6654 }
6655 catch (const semantic_error &er)
6656 {
6657 e->chain (er);
6658 provide (e);
6659 }
6660 }
6661
6662
6663 void
6664 sdt_uprobe_var_expanding_visitor::visit_target_symbol (target_symbol* e)
6665 {
6666 try
6667 {
6668 assert(e->name.size() > 0
6669 && (e->name[0] == '$' || e->name == "@var"));
6670
6671 if (e->name == "$$name" || e->name == "$$provider" || e->name == "$$parms" || e->name == "$$vars")
6672 visit_target_symbol_context (e);
6673 else
6674 visit_target_symbol_arg (e);
6675 }
6676 catch (const semantic_error &er)
6677 {
6678 e->chain (er);
6679 provide (e);
6680 }
6681 }
6682
6683
6684 void
6685 sdt_uprobe_var_expanding_visitor::visit_atvar_op (atvar_op* e)
6686 {
6687 need_debug_info = true;
6688
6689 // Fill in our current module context if needed
6690 if (e->module.empty())
6691 e->module = process_name;
6692
6693 var_expanding_visitor::visit_atvar_op(e);
6694 }
6695
6696
6697 void
6698 sdt_uprobe_var_expanding_visitor::visit_cast_op (cast_op* e)
6699 {
6700 // Fill in our current module context if needed
6701 if (e->module.empty())
6702 e->module = process_name;
6703
6704 var_expanding_visitor::visit_cast_op(e);
6705 }
6706
6707
6708 void
6709 plt_expanding_visitor::visit_target_symbol (target_symbol *e)
6710 {
6711 try
6712 {
6713 if (e->name == "$$name")
6714 {
6715 literal_string *myname = new literal_string (entry);
6716 myname->tok = e->tok;
6717 provide(myname);
6718 return;
6719 }
6720
6721 // variable not found -> throw a semantic error
6722 // (only to be caught right away, but this may be more complex later...)
6723 string alternatives = "$$name";
6724 throw SEMANTIC_ERROR(_F("unable to find plt variable '%s' (alternatives: %s)",
6725 e->name.c_str(), alternatives.c_str()), e->tok);
6726 }
6727 catch (const semantic_error &er)
6728 {
6729 e->chain (er);
6730 provide (e);
6731 }
6732 }
6733
6734
6735 struct sdt_query : public base_query
6736 {
6737 sdt_query(probe * base_probe, probe_point * base_loc,
6738 dwflpp & dw, literal_map_t const & params,
6739 vector<derived_probe *> & results, const string user_lib);
6740
6741 void query_library (const char *data);
6742 set<string> visited_libraries;
6743 bool resolved_library;
6744
6745 void query_plt (const char *entry, size_t addr) {}
6746 void handle_query_module();
6747
6748 private:
6749 stap_sdt_probe_type probe_type;
6750 enum { probe_section=0, note_section=1, unknown_section=-1 } probe_loc;
6751 probe * base_probe;
6752 probe_point * base_loc;
6753 literal_map_t const & params;
6754 vector<derived_probe *> & results;
6755 string pp_mark;
6756 string pp_provider;
6757 string user_lib;
6758
6759 set<string> probes_handled;
6760
6761 Elf_Data *pdata;
6762 size_t probe_scn_offset;
6763 size_t probe_scn_addr;
6764 uint64_t arg_count;
6765 GElf_Addr base;
6766 GElf_Addr pc;
6767 string arg_string;
6768 string probe_name;
6769 string provider_name;
6770 GElf_Addr semaphore_load_offset;
6771 Dwarf_Addr semaphore;
6772
6773 bool init_probe_scn();
6774 bool get_next_probe();
6775 void iterate_over_probe_entries();
6776 void handle_probe_entry();
6777
6778 static void setup_note_probe_entry_callback (sdt_query *me,
6779 const string& scn_name,
6780 const string& note_name,
6781 int type,
6782 const char *data,
6783 size_t len);
6784 void setup_note_probe_entry (const string& scn_name,
6785 const string& note_name, int type,
6786 const char *data, size_t len);
6787
6788 void convert_probe(probe *base);
6789 void record_semaphore(vector<derived_probe *> & results, unsigned start);
6790 probe* convert_location();
6791 bool have_uprobe() {return probe_type == uprobe1_type || probe_type == uprobe2_type || probe_type == uprobe3_type;}
6792 bool have_debuginfo_uprobe(bool need_debug_info)
6793 {return probe_type == uprobe1_type
6794 || ((probe_type == uprobe2_type || probe_type == uprobe3_type)
6795 && need_debug_info);}
6796 bool have_debuginfoless_uprobe() {return probe_type == uprobe2_type || probe_type == uprobe3_type;}
6797 };
6798
6799
6800 sdt_query::sdt_query(probe * base_probe, probe_point * base_loc,
6801 dwflpp & dw, literal_map_t const & params,
6802 vector<derived_probe *> & results, const string user_lib):
6803 base_query(dw, params), resolved_library(false),
6804 probe_type(unknown_probe_type), probe_loc(unknown_section),
6805 base_probe(base_probe), base_loc(base_loc), params(params), results(results),
6806 user_lib(user_lib), probe_scn_offset(0), probe_scn_addr(0), arg_count(0),
6807 base(0), pc(0), semaphore_load_offset(0), semaphore(0)
6808 {
6809 assert(get_string_param(params, TOK_MARK, pp_mark));
6810 get_string_param(params, TOK_PROVIDER, pp_provider); // pp_provider == "" -> unspecified
6811
6812 // PR10245: permit usage of dtrace-y "-" separator in marker name;
6813 // map it to double-underscores.
6814 size_t pos = 0;
6815 while (1) // there may be more than one
6816 {
6817 size_t i = pp_mark.find("-", pos);
6818 if (i == string::npos) break;
6819 pp_mark.replace (i, 1, "__");
6820 pos = i+1; // resume searching after the inserted __
6821 }
6822
6823 // XXX: same for pp_provider?
6824 }
6825
6826
6827 void
6828 sdt_query::handle_probe_entry()
6829 {
6830 if (! have_uprobe()
6831 && !probes_handled.insert(probe_name).second)
6832 return;
6833
6834 if (sess.verbose > 3)
6835 {
6836 //TRANSLATORS: Describing what probe type (kprobe or uprobe) the probe
6837 //TRANSLATORS: is matched to.
6838 clog << _F("matched probe_name %s probe type ", probe_name.c_str());
6839 switch (probe_type)
6840 {
6841 case uprobe1_type:
6842 clog << "uprobe1 at 0x" << hex << pc << dec << endl;
6843 break;
6844 case uprobe2_type:
6845 clog << "uprobe2 at 0x" << hex << pc << dec << endl;
6846 break;
6847 case uprobe3_type:
6848 clog << "uprobe3 at 0x" << hex << pc << dec << endl;
6849 break;
6850 default:
6851 clog << "unknown!" << endl;
6852 break;
6853 }
6854 }
6855
6856 // Extend the derivation chain
6857 probe *new_base = convert_location();
6858 probe_point *new_location = new_base->locations[0];
6859
6860 bool need_debug_info = false;
6861
6862 // We could get the Elf* from either dwarf_getelf(dwfl_module_getdwarf(...))
6863 // or dwfl_module_getelf(...). We only need it for the machine type, which
6864 // should be the same. The bias is used for relocating debuginfoless probes,
6865 // though, so that must come from the possibly-prelinked ELF file, not DWARF.
6866 Dwarf_Addr bias;
6867 Elf* elf = dwfl_module_getelf (dw.mod_info->mod, &bias);
6868
6869 /* Figure out the architecture of this particular ELF file. The
6870 dwarfless register-name mappings depend on it. */
6871 GElf_Ehdr ehdr_mem;
6872 GElf_Ehdr* em = gelf_getehdr (elf, &ehdr_mem);
6873 if (em == 0) { DWFL_ASSERT ("dwfl_getehdr", dwfl_errno()); }
6874 assert(em);
6875 int elf_machine = em->e_machine;
6876 sdt_uprobe_var_expanding_visitor svv (sess, dw, elf_machine, module_val,
6877 provider_name, probe_name, probe_type,
6878 arg_string, arg_count);
6879 svv.replace (new_base->body);
6880 need_debug_info = svv.need_debug_info;
6881
6882 // XXX: why not derive_probes() in the uprobes case too?
6883 literal_map_t params;
6884 for (unsigned i = 0; i < new_location->components.size(); ++i)
6885 {
6886 probe_point::component *c = new_location->components[i];
6887 params[c->functor] = c->arg;
6888 }
6889
6890 unsigned prior_results_size = results.size();
6891 dwarf_query q(new_base, new_location, dw, params, results, "", "");
6892 q.has_mark = true; // enables mid-statement probing
6893
6894 // V1 probes always need dwarf info
6895 // V2+ probes need dwarf info in case of a variable reference
6896 if (have_debuginfo_uprobe(need_debug_info))
6897 dw.iterate_over_modules<base_query>(&query_module, &q);
6898
6899 // For V2+ probes, if variable references weren't used or failed (PR14369),
6900 // then try with the more direct approach. Unresolved $vars might still
6901 // cause their own error, but this gives them a chance to be optimized out.
6902 if (have_debuginfoless_uprobe() && results.size() == prior_results_size)
6903 {
6904 string section;
6905 Dwarf_Addr reloc_addr = q.statement_num_val + bias;
6906 if (dwfl_module_relocations (q.dw.mod_info->mod) > 0)
6907 {
6908 dwfl_module_relocate_address (q.dw.mod_info->mod, &reloc_addr);
6909 section = ".dynamic";
6910 }
6911 else
6912 section = ".absolute";
6913
6914 uprobe_derived_probe* p =
6915 new uprobe_derived_probe ("", "", 0,
6916 path_remove_sysroot(sess,q.module_val),
6917 section,
6918 q.statement_num_val, reloc_addr, q, 0);
6919 p->saveargs (arg_count);
6920 results.push_back (p);
6921 }
6922 sess.unwindsym_modules.insert (dw.module_name);
6923 record_semaphore(results, prior_results_size);
6924 }
6925
6926
6927 void
6928 sdt_query::handle_query_module()
6929 {
6930 if (!init_probe_scn())
6931 return;
6932
6933 if (sess.verbose > 3)
6934 clog << "TOK_MARK: " << pp_mark << " TOK_PROVIDER: " << pp_provider << endl;
6935
6936 if (probe_loc == note_section)
6937 {
6938 GElf_Shdr shdr_mem;
6939 GElf_Shdr *shdr = dw.get_section (".stapsdt.base", &shdr_mem);
6940
6941 // The 'base' lets us adjust the hardcoded addresses in notes for prelink
6942 // effects. The 'semaphore_load_offset' accounts for the difference in
6943 // load addresses between text and data, so the semaphore can be
6944 // converted to a file offset if needed.
6945 if (shdr)
6946 {
6947 base = shdr->sh_addr;
6948 GElf_Addr base_offset = shdr->sh_offset;
6949 shdr = dw.get_section (".probes", &shdr_mem);
6950 if (shdr)
6951 semaphore_load_offset =
6952 (shdr->sh_addr - shdr->sh_offset) - (base - base_offset);
6953 }
6954 else
6955 base = semaphore_load_offset = 0;
6956
6957 dw.iterate_over_notes (this, &sdt_query::setup_note_probe_entry_callback);
6958 }
6959 else if (probe_loc == probe_section)
6960 iterate_over_probe_entries ();
6961 }
6962
6963
6964 bool
6965 sdt_query::init_probe_scn()
6966 {
6967 Elf* elf;
6968 GElf_Shdr shdr_mem;
6969
6970 GElf_Shdr *shdr = dw.get_section (".note.stapsdt", &shdr_mem);
6971 if (shdr)
6972 {
6973 probe_loc = note_section;
6974 return true;
6975 }
6976
6977 shdr = dw.get_section (".probes", &shdr_mem, &elf);
6978 if (shdr)
6979 {
6980 pdata = elf_getdata_rawchunk (elf, shdr->sh_offset, shdr->sh_size, ELF_T_BYTE);
6981 probe_scn_offset = 0;
6982 probe_scn_addr = shdr->sh_addr;
6983 assert (pdata != NULL);
6984 if (sess.verbose > 4)
6985 clog << "got .probes elf scn_addr@0x" << probe_scn_addr << ", size: "
6986 << pdata->d_size << endl;
6987 probe_loc = probe_section;
6988 return true;
6989 }
6990 else
6991 return false;
6992 }
6993
6994 void
6995 sdt_query::setup_note_probe_entry_callback (sdt_query *me,
6996 const string& scn_name,
6997 const string& note_name, int type,
6998 const char *data, size_t len)
6999 {
7000 me->setup_note_probe_entry (scn_name, note_name, type, data, len);
7001 }
7002
7003
7004 void
7005 sdt_query::setup_note_probe_entry (const string& scn_name,
7006 const string& note_name, int type,
7007 const char *data, size_t len)
7008 {
7009 if (scn_name.compare(".note.stapsdt"))
7010 return;
7011 #define _SDT_NOTE_NAME "stapsdt"
7012 if (note_name.compare(_SDT_NOTE_NAME))
7013 return;
7014 #define _SDT_NOTE_TYPE 3
7015 if (type != _SDT_NOTE_TYPE)
7016 return;
7017
7018 // we found a probe entry
7019 union
7020 {
7021 Elf64_Addr a64[3];
7022 Elf32_Addr a32[3];
7023 } buf;
7024 Dwarf_Addr bias;
7025 Elf* elf = (dwfl_module_getelf (dw.mod_info->mod, &bias));
7026 Elf_Data dst =
7027 {
7028 &buf, ELF_T_ADDR, EV_CURRENT,
7029 gelf_fsize (elf, ELF_T_ADDR, 3, EV_CURRENT), 0, 0
7030 };
7031 assert (dst.d_size <= sizeof buf);
7032
7033 if (len < dst.d_size + 3)
7034 return;
7035
7036 Elf_Data src =
7037 {
7038 (void *) data, ELF_T_ADDR, EV_CURRENT,
7039 dst.d_size, 0, 0
7040 };
7041
7042 if (gelf_xlatetom (elf, &dst, &src,
7043 elf_getident (elf, NULL)[EI_DATA]) == NULL)
7044 printf ("gelf_xlatetom: %s", elf_errmsg (-1));
7045
7046 probe_type = uprobe3_type;
7047 const char * provider = data + dst.d_size;
7048
7049 const char *name = (const char*)memchr (provider, '\0', data + len - provider);
7050 if(name++ == NULL)
7051 return;
7052
7053 const char *args = (const char*)memchr (name, '\0', data + len - name);
7054 if (args++ == NULL || memchr (args, '\0', data + len - name) != data + len - 1)
7055 return;
7056
7057 provider_name = provider;
7058 probe_name = name;
7059 arg_string = args;
7060
7061 dw.mod_info->marks.insert(make_pair(provider, name));
7062
7063 // Did we find a matching probe?
7064 if (! (dw.function_name_matches_pattern (probe_name, pp_mark)
7065 && ((pp_provider == "")
7066 || dw.function_name_matches_pattern (provider_name, pp_provider))))
7067 return;
7068
7069 // PR13934: Assembly probes are not forced to use the N@OP form.
7070 // If we have '@' then great, else count based on space-delimiters.
7071 arg_count = count(arg_string.begin(), arg_string.end(), '@');
7072 if (!arg_count && !arg_string.empty())
7073 arg_count = 1 + count(arg_string.begin(), arg_string.end(), ' ');
7074
7075 GElf_Addr base_ref;
7076 if (gelf_getclass (elf) == ELFCLASS32)
7077 {
7078 pc = buf.a32[0];
7079 base_ref = buf.a32[1];
7080 semaphore = buf.a32[2];
7081 }
7082 else
7083 {
7084 pc = buf.a64[0];
7085 base_ref = buf.a64[1];
7086 semaphore = buf.a64[2];
7087 }
7088
7089 semaphore += base - base_ref;
7090 pc += base - base_ref;
7091
7092 // The semaphore also needs the ELF bias added now, so
7093 // record_semaphore can properly relocate it later.
7094 semaphore += bias;
7095
7096 if (sess.verbose > 4)
7097 clog << _F(" saw .note.stapsdt %s%s ", probe_name.c_str(), (provider_name != "" ? _(" (provider ")+provider_name+") " : "").c_str()) << "@0x" << hex << pc << dec << endl;
7098
7099 handle_probe_entry();
7100 }
7101
7102
7103 void
7104 sdt_query::iterate_over_probe_entries()
7105 {
7106 // probes are in the .probe section
7107 while (probe_scn_offset < pdata->d_size)
7108 {
7109 stap_sdt_probe_entry_v1 *pbe_v1 = (stap_sdt_probe_entry_v1 *) ((char*)pdata->d_buf + probe_scn_offset);
7110 stap_sdt_probe_entry_v2 *pbe_v2 = (stap_sdt_probe_entry_v2 *) ((char*)pdata->d_buf + probe_scn_offset);
7111 probe_type = (stap_sdt_probe_type)(pbe_v1->type_a);
7112 if (! have_uprobe())
7113 {
7114 // Unless this is a mangled .probes section, this happens
7115 // because the name of the probe comes first, followed by
7116 // the sentinel.
7117 if (sess.verbose > 5)
7118 clog << _F("got unknown probe_type : 0x%x", probe_type) << endl;
7119 probe_scn_offset += sizeof(__uint32_t);
7120 continue;
7121 }
7122 if ((long)pbe_v1 % sizeof(__uint64_t)) // we have stap_sdt_probe_entry_v1.type_b
7123 {
7124 pbe_v1 = (stap_sdt_probe_entry_v1*)((char*)pbe_v1 - sizeof(__uint32_t));
7125 if (pbe_v1->type_b != uprobe1_type)
7126 continue;
7127 }
7128
7129 if (probe_type == uprobe1_type)
7130 {
7131 if (pbe_v1->name == 0) // No name possibly means we have a .so with a relocation
7132 return;
7133 semaphore = 0;
7134 probe_name = (char*)((char*)pdata->d_buf + pbe_v1->name - (char*)probe_scn_addr);
7135 provider_name = ""; // unknown
7136 pc = pbe_v1->arg;
7137 arg_count = 0;
7138 probe_scn_offset += sizeof (stap_sdt_probe_entry_v1);
7139 }
7140 else if (probe_type == uprobe2_type)
7141 {
7142 if (pbe_v2->name == 0) // No name possibly means we have a .so with a relocation
7143 return;
7144 semaphore = pbe_v2->semaphore;
7145 probe_name = (char*)((char*)pdata->d_buf + pbe_v2->name - (char*)probe_scn_addr);
7146 provider_name = (char*)((char*)pdata->d_buf + pbe_v2->provider - (char*)probe_scn_addr);
7147 arg_count = pbe_v2->arg_count;
7148 pc = pbe_v2->pc;
7149 if (pbe_v2->arg_string)
7150 arg_string = (char*)((char*)pdata->d_buf + pbe_v2->arg_string - (char*)probe_scn_addr);
7151 // skip over pbe_v2, probe_name text and provider text
7152 probe_scn_offset = ((long)(pbe_v2->name) - (long)(probe_scn_addr)) + probe_name.length();
7153 probe_scn_offset += sizeof (__uint32_t) - probe_scn_offset % sizeof (__uint32_t);
7154 }
7155 if (sess.verbose > 4)
7156 clog << _("saw .probes ") << probe_name << (provider_name != "" ? _(" (provider ")+provider_name+") " : "")
7157 << "@0x" << hex << pc << dec << endl;
7158
7159 dw.mod_info->marks.insert(make_pair(provider_name, probe_name));
7160
7161 if (dw.function_name_matches_pattern (probe_name, pp_mark)
7162 && ((pp_provider == "") || dw.function_name_matches_pattern (provider_name, pp_provider)))
7163 handle_probe_entry ();
7164 }
7165 }
7166
7167
7168 void
7169 sdt_query::record_semaphore (vector<derived_probe *> & results, unsigned start)
7170 {
7171 for (unsigned i=0; i<2; i++) {
7172 // prefer with-provider symbol; look without provider prefix for backward compatibility only
7173 string semaphore = (i==0 ? (provider_name+"_") : "") + probe_name + "_semaphore";
7174 // XXX: multiple addresses?
7175 if (sess.verbose > 2)
7176 clog << _F("looking for semaphore symbol %s ", semaphore.c_str());
7177
7178 Dwarf_Addr addr;
7179 if (this->semaphore)
7180 addr = this->semaphore;
7181 else
7182 addr = lookup_symbol_address(dw.module, semaphore.c_str());
7183 if (addr)
7184 {
7185 if (dwfl_module_relocations (dw.module) > 0)
7186 dwfl_module_relocate_address (dw.module, &addr);
7187 // XXX: relocation basis?
7188
7189 // Dyninst needs the *file*-based offset for semaphores,
7190 // so subtract the difference in load addresses between .text and .probes
7191 if (dw.sess.runtime_usermode_p())
7192 addr -= semaphore_load_offset;
7193
7194 for (unsigned i = start; i < results.size(); ++i)
7195 results[i]->sdt_semaphore_addr = addr;
7196 if (sess.verbose > 2)
7197 clog << _(", found at 0x") << hex << addr << dec << endl;
7198 return;
7199 }
7200 else
7201 if (sess.verbose > 2)
7202 clog << _(", not found") << endl;
7203 }
7204 }
7205
7206
7207 void
7208 sdt_query::convert_probe (probe *base)
7209 {
7210 block *b = new block;
7211 b->tok = base->body->tok;
7212
7213 // Generate: if (arg1 != mark("label")) next;
7214 functioncall *fc = new functioncall;
7215 fc->function = "ulong_arg";
7216 fc->tok = b->tok;
7217 literal_number* num = new literal_number(1);
7218 num->tok = b->tok;
7219 fc->args.push_back(num);
7220
7221 functioncall *fcus = new functioncall;
7222 fcus->function = "user_string";
7223 fcus->type = pe_string;
7224 fcus->tok = b->tok;
7225 fcus->args.push_back(fc);
7226
7227 if_statement *is = new if_statement;
7228 is->thenblock = new next_statement;
7229 is->elseblock = NULL;
7230 is->tok = b->tok;
7231 is->thenblock->tok = b->tok;
7232 comparison *be = new comparison;
7233 be->op = "!=";
7234 be->tok = b->tok;
7235 be->left = fcus;
7236 be->right = new literal_string(probe_name);
7237 be->right->tok = b->tok;
7238 is->condition = be;
7239 b->statements.push_back(is);
7240
7241 // Now replace the body
7242 b->statements.push_back(base->body);
7243 base->body = b;
7244 }
7245
7246
7247 probe*
7248 sdt_query::convert_location ()
7249 {
7250 string module = dw.module_name;
7251 if (has_process)
7252 module = path_remove_sysroot(sess, module);
7253
7254 probe_point* specific_loc = new probe_point(*base_loc);
7255 specific_loc->well_formed = true;
7256
7257 vector<probe_point::component*> derived_comps;
7258
7259 vector<probe_point::component*>::iterator it;
7260 for (it = specific_loc->components.begin();
7261 it != specific_loc->components.end(); ++it)
7262 if ((*it)->functor == TOK_PROCESS)
7263 {
7264 // replace the possibly incomplete path to process
7265 *it = new probe_point::component(TOK_PROCESS,
7266 new literal_string(has_library ? path : module));
7267
7268 // copy the process name
7269 derived_comps.push_back(*it);
7270 }
7271 else if ((*it)->functor == TOK_LIBRARY)
7272 {
7273 // copy the library name for process probes
7274 derived_comps.push_back(*it);
7275 }
7276 else if ((*it)->functor == TOK_PROVIDER)
7277 {
7278 // replace the possibly wildcarded arg with the specific provider name
7279 *it = new probe_point::component(TOK_PROVIDER,
7280 new literal_string(provider_name));
7281 }
7282 else if ((*it)->functor == TOK_MARK)
7283 {
7284 // replace the possibly wildcarded arg with the specific marker name
7285 *it = new probe_point::component(TOK_MARK,
7286 new literal_string(probe_name));
7287
7288 if (sess.verbose > 3)
7289 switch (probe_type)
7290 {
7291 case uprobe1_type:
7292 clog << _("probe_type == uprobe1, use statement addr: 0x")
7293 << hex << pc << dec << endl;
7294 break;
7295 case uprobe2_type:
7296 clog << _("probe_type == uprobe2, use statement addr: 0x")
7297 << hex << pc << dec << endl;
7298 break;
7299 case uprobe3_type:
7300 clog << _("probe_type == uprobe3, use statement addr: 0x")
7301 << hex << pc << dec << endl;
7302 break;
7303 default:
7304 clog << _F("probe_type == use_uprobe_no_dwarf, use label name: _stapprobe1_%s",
7305 pp_mark.c_str()) << endl;
7306 }
7307
7308 switch (probe_type)
7309 {
7310 case uprobe1_type:
7311 case uprobe2_type:
7312 case uprobe3_type:
7313 // process("executable").statement(probe_arg)
7314 derived_comps.push_back
7315 (new probe_point::component(TOK_STATEMENT,
7316 new literal_number(pc, true)));
7317 break;
7318
7319 default: // deprecated
7320 // process("executable").function("*").label("_stapprobe1_MARK_NAME")
7321 derived_comps.push_back
7322 (new probe_point::component(TOK_FUNCTION,
7323 new literal_string("*")));
7324 derived_comps.push_back
7325 (new probe_point::component(TOK_LABEL,
7326 new literal_string("_stapprobe1_" + pp_mark)));
7327 break;
7328 }
7329 }
7330
7331 probe_point* derived_loc = new probe_point(*specific_loc);
7332 derived_loc->components = derived_comps;
7333 return new probe (new probe (base_probe, specific_loc), derived_loc);
7334 }
7335
7336
7337 void
7338 sdt_query::query_library (const char *library)
7339 {
7340 visited_libraries.insert(library);
7341 if (query_one_library (library, dw, user_lib, base_probe, base_loc, results))
7342 resolved_library = true;
7343 }
7344
7345 string
7346 suggest_marks(systemtap_session& sess,
7347 const set<string>& modules,
7348 const string& mark,
7349 const string& provider)
7350 {
7351 if (mark.empty() || modules.empty() || sess.module_cache == NULL)
7352 return "";
7353
7354 set<string> marks;
7355 const map<string, module_info*> &cache = sess.module_cache->cache;
7356 bool dash_suggestions = (mark.find("-") != string::npos);
7357
7358 for (set<string>::iterator itmod = modules.begin();
7359 itmod != modules.end(); ++itmod)
7360 {
7361 map<string, module_info*>::const_iterator itcache;
7362 if ((itcache = cache.find(*itmod)) != cache.end())
7363 {
7364 set<pair<string,string> >::const_iterator itmarks;
7365 for (itmarks = itcache->second->marks.begin();
7366 itmarks != itcache->second->marks.end(); ++itmarks)
7367 {
7368 if (provider.empty()
7369 // simulating dw.function_name_matches_pattern()
7370 || (fnmatch(provider.c_str(), itmarks->first.c_str(), 0) == 0))
7371 {
7372 string marksug = itmarks->second;
7373 if (dash_suggestions)
7374 {
7375 size_t pos = 0;
7376 while (1) // there may be more than one
7377 {
7378 size_t i = marksug.find("__", pos);
7379 if (i == string::npos) break;
7380 marksug.replace (i, 2, "-");
7381 pos = i+1; // resume searching after the inserted -
7382 }
7383 }
7384 marks.insert(marksug);
7385 }
7386 }
7387 }
7388 }
7389
7390 if (sess.verbose > 2)
7391 {
7392 clog << "suggesting " << marks.size() << " marks "
7393 << "from modules:" << endl;
7394 for (set<string>::iterator itmod = modules.begin();
7395 itmod != modules.end(); ++itmod)
7396 clog << *itmod << endl;
7397 }
7398
7399 if (marks.empty())
7400 return "";
7401
7402 return levenshtein_suggest(mark, marks, 5); // print top 5 marks only
7403 }
7404
7405 string
7406 suggest_plt_functions(systemtap_session& sess,
7407 const set<string>& modules,
7408 const string& func)
7409 {
7410 if (func.empty() || modules.empty() || sess.module_cache == NULL)
7411 return "";
7412
7413 set<string> funcs;
7414 const map<string, module_info*> &cache = sess.module_cache->cache;
7415
7416 for (set<string>::iterator itmod = modules.begin();
7417 itmod != modules.end(); ++itmod)
7418 {
7419 map<string, module_info*>::const_iterator itcache;
7420 if ((itcache = cache.find(*itmod)) != cache.end())
7421 funcs.insert(itcache->second->plt_funcs.begin(),
7422 itcache->second->plt_funcs.end());
7423 }
7424
7425 if (sess.verbose > 2)
7426 {
7427 clog << "suggesting " << funcs.size() << " plt functions "
7428 << "from modules:" << endl;
7429 for (set<string>::iterator itmod = modules.begin();
7430 itmod != modules.end(); ++itmod)
7431 clog << *itmod << endl;
7432 }
7433
7434 if (funcs.empty())
7435 return "";
7436
7437 return levenshtein_suggest(func, funcs, 5); // print top 5 funcs only
7438 }
7439
7440 string
7441 suggest_dwarf_functions(systemtap_session& sess,
7442 const set<string>& modules,
7443 string func)
7444 {
7445 // Trim any @ component
7446 size_t pos = func.find('@');
7447 if (pos != string::npos)
7448 func.erase(pos);
7449
7450 if (func.empty() || modules.empty() || sess.module_cache == NULL)
7451 return "";
7452
7453 // We must first aggregate all the functions from the cache
7454 set<string> funcs;
7455 const map<string, module_info*> &cache = sess.module_cache->cache;
7456
7457 for (set<string>::iterator itmod = modules.begin();
7458 itmod != modules.end(); ++itmod)
7459 {
7460 module_info *module;
7461
7462 // retrieve module_info from cache
7463 map<string, module_info*>::const_iterator itcache;
7464 if ((itcache = cache.find(*itmod)) != cache.end())
7465 module = itcache->second;
7466 else // module not found
7467 continue;
7468
7469 // add inlines
7470 funcs.insert(module->inlined_funcs.begin(),
7471 module->inlined_funcs.end());
7472
7473 // add all function symbols in cache
7474 if (module->symtab_status != info_present || module->sym_table == NULL)
7475 continue;
7476 map<string, func_info*>& modfuncs = module->sym_table->map_by_name;
7477 for (map<string, func_info*>::const_iterator itfuncs = modfuncs.begin();
7478 itfuncs != modfuncs.end(); ++itfuncs)
7479 funcs.insert(itfuncs->first);
7480 }
7481
7482 if (sess.verbose > 2)
7483 {
7484 clog << "suggesting " << funcs.size() << " dwarf functions "
7485 << "from modules:" << endl;
7486 for (set<string>::iterator itmod = modules.begin();
7487 itmod != modules.end(); ++itmod)
7488 clog << *itmod << endl;
7489 }
7490
7491 if (funcs.empty())
7492 return "";
7493
7494 return levenshtein_suggest(func, funcs, 5); // print top 5 funcs only
7495 }
7496
7497 void
7498 dwarf_builder::build(systemtap_session & sess,
7499 probe * base,
7500 probe_point * location,
7501 literal_map_t const & parameters,
7502 vector<derived_probe *> & finished_results)
7503 {
7504 // NB: the kernel/user dwlfpp objects are long-lived.
7505 // XXX: but they should be per-session, as this builder object
7506 // may be reused if we try to cross-instrument multiple targets.
7507
7508 dwflpp* dw = 0;
7509 literal_map_t filled_parameters = parameters;
7510
7511 string module_name;
7512 int64_t proc_pid;
7513 if (has_null_param (parameters, TOK_KERNEL))
7514 {
7515 dw = get_kern_dw(sess, "kernel");
7516 }
7517 else if (get_param (parameters, TOK_MODULE, module_name))
7518 {
7519 // If not a full path was given, then it's an in-tree module. Replace any
7520 // dashes with underscores.
7521 if (!is_fully_resolved(module_name, sess.sysroot, sess.sysenv))
7522 {
7523 size_t dash_pos = 0;
7524 while((dash_pos=module_name.find('-'))!=string::npos)
7525 module_name.replace(int(dash_pos),1,"_");
7526 filled_parameters[TOK_MODULE] = new literal_string(module_name);
7527 }
7528
7529 // NB: glob patterns get expanded later, during the offline
7530 // elfutils module listing.
7531 dw = get_kern_dw(sess, module_name);
7532 }
7533 else if (has_param(filled_parameters, TOK_PROCESS))
7534 {
7535 // NB: module_name is not yet set!
7536
7537 if(has_null_param(filled_parameters, TOK_PROCESS))
7538 {
7539 string file;
7540 try
7541 {
7542 file = sess.cmd_file();
7543 }
7544 catch (const semantic_error& e)
7545 {
7546 if(sess.target_pid)
7547 throw SEMANTIC_ERROR(_("invalid -x pid for unspecified process"
7548 " probe [man stapprobes]"), NULL, NULL, &e);
7549 else
7550 throw SEMANTIC_ERROR(_("invalid -c command for unspecified process"
7551 " probe [man stapprobes]"), NULL, NULL, &e);
7552 }
7553 if(file.empty())
7554 throw SEMANTIC_ERROR(_("unspecified process probe is invalid without"
7555 " a -c COMMAND or -x PID [man stapprobes]"));
7556 module_name = sess.sysroot + file;
7557 filled_parameters[TOK_PROCESS] = new literal_string(module_name);// this needs to be used in place of the blank map
7558 // in the case of TOK_MARK we need to modify locations as well // XXX why?
7559 if(location->components[0]->functor==TOK_PROCESS &&
7560 location->components[0]->arg == 0)
7561 {
7562 if (sess.target_pid)
7563 location->components[0]->arg = new literal_number(sess.target_pid);
7564 else
7565 location->components[0]->arg = new literal_string(module_name);
7566 }
7567 }
7568
7569 // NB: must specifically handle the classical ("string") form here, to make sure
7570 // we get the module_name out.
7571 else if (get_param (parameters, TOK_PROCESS, module_name))
7572 {
7573 module_name = sess.sysroot + module_name;
7574 filled_parameters[TOK_PROCESS] = new literal_string(module_name);
7575 }
7576
7577 else if (get_param (parameters, TOK_PROCESS, proc_pid))
7578 {
7579 // check that the pid given corresponds to a running process
7580 string pid_err_msg;
7581 if (!is_valid_pid(proc_pid, pid_err_msg))
7582 throw SEMANTIC_ERROR(pid_err_msg);
7583
7584 string pid_path = string("/proc/") + lex_cast(proc_pid) + "/exe";
7585 module_name = sess.sysroot + pid_path;
7586
7587 // in the case of TOK_MARK we need to modify locations as well // XXX why?
7588 if(location->components[0]->functor==TOK_PROCESS &&
7589 location->components[0]->arg == 0)
7590 location->components[0]->arg = new literal_number(sess.target_pid);
7591 }
7592
7593 // PR6456 process("/bin/*") glob handling
7594 if (contains_glob_chars (module_name))
7595 {
7596 // Expand glob via rewriting the probe-point process("....")
7597 // parameter, asserted to be the first one.
7598
7599 assert (location->components.size() > 0);
7600 assert (location->components[0]->functor == TOK_PROCESS);
7601 assert (location->components[0]->arg);
7602 literal_string* lit = dynamic_cast<literal_string*>(location->components[0]->arg);
7603 assert (lit);
7604
7605 // Evaluate glob here, and call derive_probes recursively with each match.
7606 glob_t the_blob;
7607 set<string> dupes;
7608 int rc = glob (module_name.c_str(), 0, NULL, & the_blob);
7609 if (rc)
7610 throw SEMANTIC_ERROR (_F("glob %s error (%s)", module_name.c_str(), lex_cast(rc).c_str() ));
7611 unsigned results_pre = finished_results.size();
7612 for (unsigned i = 0; i < the_blob.gl_pathc; ++i)
7613 {
7614 assert_no_interrupts();
7615
7616 const char* globbed = the_blob.gl_pathv[i];
7617 struct stat st;
7618
7619 if (access (globbed, X_OK) == 0
7620 && stat (globbed, &st) == 0
7621 && S_ISREG (st.st_mode)) // see find_executable()
7622 {
7623 // Need to call canonicalize here, in order to path-expand
7624 // patterns like process("stap*"). Otherwise it may go through
7625 // to the next round of expansion as ("stap"), leading to a $PATH
7626 // search that's not consistent with the glob search already done.
7627 string canononicalized = resolve_path (globbed);
7628 globbed = canononicalized.c_str();
7629
7630 // The canonical names can result in duplication, for example
7631 // having followed symlinks that are common with shared
7632 // libraries. Filter those out.
7633 if (!dupes.insert(canononicalized).second)
7634 continue;
7635
7636 // synthesize a new probe_point, with the glob-expanded string
7637 probe_point *pp = new probe_point (*location);
7638 pp->from_glob = true;
7639
7640 // PR13338: quote results to prevent recursion
7641 string eglobbed = escape_glob_chars (globbed);
7642
7643 if (sess.verbose > 1)
7644 clog << _F("Expanded process(\"%s\") to process(\"%s\")",
7645 module_name.c_str(), eglobbed.c_str()) << endl;
7646 string eglobbed_tgt = path_remove_sysroot(sess, eglobbed);
7647
7648 probe_point::component* ppc = new probe_point::component (TOK_PROCESS,
7649 new literal_string (eglobbed_tgt));
7650 ppc->tok = location->components[0]->tok; // overwrite [0] slot, pattern matched above
7651 pp->components[0] = ppc;
7652
7653 probe* new_probe = new probe (base, pp);
7654
7655 // We override "optional = true" here, as if the
7656 // wildcarded probe point was given a "?" suffix.
7657
7658 // This is because wildcard probes will be expected
7659 // by users to apply only to some subset of the
7660 // matching binaries, in the sense of "any", rather
7661 // than "all", sort of similarly how
7662 // module("*").function("...") patterns work.
7663
7664 derive_probes (sess, new_probe, finished_results,
7665 true /* NB: not location->optional */ );
7666 }
7667 }
7668
7669 globfree (& the_blob);
7670
7671 unsigned results_post = finished_results.size();
7672
7673 // Did we fail to find a function/plt/mark by name? Let's suggest
7674 // something!
7675 string func;
7676 if (results_pre == results_post
7677 && get_param(filled_parameters, TOK_FUNCTION, func)
7678 && !func.empty())
7679 {
7680 string sugs = suggest_dwarf_functions(sess, modules_seen, func);
7681 modules_seen.clear();
7682 if (!sugs.empty())
7683 throw SEMANTIC_ERROR (_NF("no match (similar function: %s)",
7684 "no match (similar functions: %s)",
7685 sugs.find(',') == string::npos,
7686 sugs.c_str()));
7687 }
7688 else if (results_pre == results_post
7689 && get_param(filled_parameters, TOK_PLT, func)
7690 && !func.empty())
7691 {
7692 string sugs = suggest_plt_functions(sess, modules_seen, func);
7693 modules_seen.clear();
7694 if (!sugs.empty())
7695 throw SEMANTIC_ERROR (_NF("no match (similar function: %s)",
7696 "no match (similar functions: %s)",
7697 sugs.find(',') == string::npos,
7698 sugs.c_str()));
7699 }
7700 else if (results_pre == results_post
7701 && get_param(filled_parameters, TOK_MARK, func)
7702 && !func.empty())
7703 {
7704 string provider;
7705 get_param(filled_parameters, TOK_PROVIDER, provider);
7706
7707 string sugs = suggest_marks(sess, modules_seen, func, provider);
7708 modules_seen.clear();
7709 if (!sugs.empty())
7710 throw SEMANTIC_ERROR (_NF("no match (similar mark: %s)",
7711 "no match (similar marks: %s)",
7712 sugs.find(',') == string::npos,
7713 sugs.c_str()));
7714 }
7715
7716 return; // avoid falling through
7717 }
7718
7719 // PR13338: unquote glob results
7720 module_name = unescape_glob_chars (module_name);
7721 user_path = find_executable (module_name, "", sess.sysenv); // canonicalize it
7722 if (!is_fully_resolved(user_path, "", sess.sysenv))
7723 throw SEMANTIC_ERROR(_F("cannot find executable '%s'",
7724 user_path.c_str()));
7725
7726 // if the executable starts with "#!", we look for the interpreter of the script
7727 {
7728 ifstream script_file (user_path.c_str () );
7729
7730 if (script_file.good ())
7731 {
7732 string line;
7733
7734 getline (script_file, line);
7735
7736 if (line.compare (0, 2, "#!") == 0)
7737 {
7738 string path_head = line.substr(2);
7739
7740 // remove white spaces at the beginning of the string
7741 size_t p2 = path_head.find_first_not_of(" \t");
7742
7743 if (p2 != string::npos)
7744 {
7745 string path = path_head.substr(p2);
7746
7747 // remove white spaces at the end of the string
7748 p2 = path.find_last_not_of(" \t\n");
7749 if (string::npos != p2)
7750 path.erase(p2+1);
7751
7752 // handle "#!/usr/bin/env" redirect
7753 size_t offset = 0;
7754 if (path.compare(0, sizeof("/bin/env")-1, "/bin/env") == 0)
7755 {
7756 offset = sizeof("/bin/env")-1;
7757 }
7758 else if (path.compare(0, sizeof("/usr/bin/env")-1, "/usr/bin/env") == 0)
7759 {
7760 offset = sizeof("/usr/bin/env")-1;
7761 }
7762
7763 if (offset != 0)
7764 {
7765 size_t p3 = path.find_first_not_of(" \t", offset);
7766
7767 if (p3 != string::npos)
7768 {
7769 string env_path = path.substr(p3);
7770 user_path = find_executable (env_path, sess.sysroot,
7771 sess.sysenv);
7772 }
7773 }
7774 else
7775 {
7776 user_path = find_executable (path, sess.sysroot, sess.sysenv);
7777 }
7778
7779 struct stat st;
7780
7781 if (access (user_path.c_str(), X_OK) == 0
7782 && stat (user_path.c_str(), &st) == 0
7783 && S_ISREG (st.st_mode)) // see find_executable()
7784 {
7785 if (sess.verbose > 1)
7786 clog << _F("Expanded process(\"%s\") to process(\"%s\")",
7787 module_name.c_str(), user_path.c_str()) << endl;
7788
7789 assert (location->components.size() > 0);
7790 assert (location->components[0]->functor == TOK_PROCESS);
7791 assert (location->components[0]->arg);
7792 literal_string* lit = dynamic_cast<literal_string*>(location->components[0]->arg);
7793 assert (lit);
7794
7795 // synthesize a new probe_point, with the expanded string
7796 probe_point *pp = new probe_point (*location);
7797 string user_path_tgt = path_remove_sysroot(sess, user_path);
7798 probe_point::component* ppc = new probe_point::component (TOK_PROCESS,
7799 new literal_string (user_path_tgt.c_str()));
7800 ppc->tok = location->components[0]->tok; // overwrite [0] slot, pattern matched above
7801 pp->components[0] = ppc;
7802
7803 probe* new_probe = new probe (base, pp);
7804
7805 derive_probes (sess, new_probe, finished_results);
7806
7807 script_file.close();
7808 return;
7809 }
7810 }
7811 }
7812 }
7813 script_file.close();
7814 }
7815
7816 // If this is a library probe, then target the library module instead. We
7817 // do this only if the library path is already fully resolved (such as
7818 // what query_one_library() would have done for us). Otherwise, we resort
7819 // to iterate_over_libraries.
7820 if (get_param (parameters, TOK_LIBRARY, user_lib) && !user_lib.empty()
7821 && is_fully_resolved(user_lib, sess.sysroot, sess.sysenv, "LD_LIBRARY_PATH"))
7822 module_name = user_lib;
7823 else
7824 module_name = user_path; // canonicalize it
7825
7826 // uretprobes aren't available everywhere
7827 if (has_null_param(parameters, TOK_RETURN) && !sess.runtime_usermode_p())
7828 {
7829 if (kernel_supports_inode_uprobes(sess) &&
7830 !kernel_supports_inode_uretprobes(sess))
7831 throw SEMANTIC_ERROR
7832 (_("process return probes not available [man error::inode-uprobes]"));
7833 }
7834
7835 // There is a similar check in pass 4 (buildrun), but it is
7836 // needed here too to make sure alternatives for optional
7837 // (? or !) process probes are disposed and/or alternatives
7838 // are selected.
7839 if (!sess.runtime_usermode_p())
7840 check_process_probe_kernel_support(sess);
7841
7842 // user-space target; we use one dwflpp instance per module name
7843 // (= program or shared library)
7844 dw = get_user_dw(sess, module_name);
7845 }
7846
7847 assert(dw);
7848
7849 unsigned results_pre = finished_results.size();
7850
7851 if (sess.verbose > 3)
7852 clog << _F("dwarf_builder::build for %s", module_name.c_str()) << endl;
7853
7854 string dummy_mark_name; // NB: PR10245: dummy value, need not substitute - => __
7855 if (get_param(parameters, TOK_MARK, dummy_mark_name))
7856 {
7857 sdt_query sdtq(base, location, *dw, filled_parameters, finished_results, user_lib);
7858 dw->iterate_over_modules<base_query>(&query_module, &sdtq);
7859
7860 // We need to update modules_seen with the modules we've visited
7861 modules_seen.insert(sdtq.visited_modules.begin(),
7862 sdtq.visited_modules.end());
7863
7864 string lib;
7865 if (results_pre == finished_results.size() && !sdtq.resolved_library
7866 && get_param(filled_parameters, TOK_LIBRARY, lib)
7867 && !lib.empty() && !sdtq.visited_libraries.empty())
7868 {
7869 // The library didn't fit any DT_NEEDED libraries. As a last effort,
7870 // let's try to look for the library directly.
7871 string resolved_lib = find_executable(lib, sess.sysroot, sess.sysenv,
7872 "LD_LIBRARY_PATH");
7873 if (resolved_lib.find('/') != string::npos)
7874 {
7875 probe *new_base = build_library_probe(*dw, resolved_lib,
7876 base, location);
7877 derive_probes(sess, new_base, finished_results);
7878 sess.print_warning(_F("'%s' is not a needed library of '%s'. "
7879 "Specify the full path to squelch this warning.",
7880 resolved_lib.c_str(), dw->module_name.c_str()));
7881 return;
7882 }
7883
7884 // Otherwise, let's suggest from the DT_NEEDED libraries
7885 string sugs = levenshtein_suggest(lib, sdtq.visited_libraries, 5);
7886 if (!sugs.empty())
7887 throw SEMANTIC_ERROR (_NF("no match (similar library: %s)",
7888 "no match (similar libraries: %s)",
7889 sugs.find(',') == string::npos,
7890 sugs.c_str()));
7891 }
7892
7893 // Did we fail to find a mark?
7894 if (results_pre == finished_results.size() && !location->from_glob)
7895 {
7896 string provider;
7897 get_param(filled_parameters, TOK_PROVIDER, provider);
7898
7899 string sugs = suggest_marks(sess, modules_seen, dummy_mark_name, provider);
7900 modules_seen.clear();
7901 if (!sugs.empty())
7902 throw SEMANTIC_ERROR (_NF("no match (similar mark: %s)",
7903 "no match (similar marks: %s)",
7904 sugs.find(',') == string::npos,
7905 sugs.c_str()));
7906 }
7907
7908 return;
7909 }
7910
7911 dwarf_query q(base, location, *dw, filled_parameters, finished_results, user_path, user_lib);
7912
7913 // XXX: kernel.statement.absolute is a special case that requires no
7914 // dwfl processing. This code should be in a separate builder.
7915 if (q.has_kernel && q.has_absolute)
7916 {
7917 // assert guru mode for absolute probes
7918 if (! q.base_probe->privileged)
7919 {
7920 throw SEMANTIC_ERROR (_("absolute statement probe in unprivileged script; need stap -g"),
7921 q.base_probe->tok);
7922 }
7923
7924 // For kernel.statement(NUM).absolute probe points, we bypass
7925 // all the debuginfo stuff: We just wire up a
7926 // dwarf_derived_probe right here and now.
7927 dwarf_derived_probe* p =
7928 new dwarf_derived_probe ("", "", 0, "kernel", "",
7929 q.statement_num_val, q.statement_num_val,
7930 q, 0);
7931 finished_results.push_back (p);
7932 sess.unwindsym_modules.insert ("kernel");
7933 return;
7934 }
7935
7936 dw->iterate_over_modules<base_query>(&query_module, &q);
7937
7938 // We need to update modules_seen with the modules we've visited
7939 modules_seen.insert(q.visited_modules.begin(),
7940 q.visited_modules.end());
7941
7942 // PR11553 special processing: .return probes requested, but
7943 // some inlined function instances matched.
7944 unsigned i_n_r = q.inlined_non_returnable.size();
7945 unsigned results_post = finished_results.size();
7946 if (i_n_r > 0)
7947 {
7948 if ((results_pre == results_post) && (! sess.suppress_warnings)) // no matches; issue warning
7949 {
7950 string quicklist;
7951 for (set<string>::iterator it = q.inlined_non_returnable.begin();
7952 it != q.inlined_non_returnable.end();
7953 it++)
7954 {
7955 quicklist += " " + (*it);
7956 if (quicklist.size() > 80) // heuristic, don't make an overlong report line
7957 {
7958 quicklist += " ...";
7959 break;
7960 }
7961 }
7962
7963 sess.print_warning (_NF("cannot probe .return of %u inlined function %s",
7964 "cannot probe .return of %u inlined functions %s",
7965 quicklist.size(), i_n_r, quicklist.c_str()));
7966 // There will be also a "no matches" semantic error generated.
7967 }
7968 if (sess.verbose > 1)
7969 clog << _NF("skipped .return probe of %u inlined function",
7970 "skipped .return probe of %u inlined functions", i_n_r, i_n_r) << endl;
7971 if ((sess.verbose > 3) || (sess.verbose > 2 && results_pre == results_post)) // issue details with high verbosity
7972 {
7973 for (set<string>::iterator it = q.inlined_non_returnable.begin();
7974 it != q.inlined_non_returnable.end();
7975 it++)
7976 clog << (*it) << " ";
7977 clog << endl;
7978 }
7979 } // i_n_r > 0
7980
7981 string lib;
7982 if (results_pre == results_post && !q.resolved_library
7983 && get_param(filled_parameters, TOK_LIBRARY, lib)
7984 && !lib.empty() && !q.visited_libraries.empty())
7985 {
7986 // The library didn't fit any DT_NEEDED libraries. As a last effort,
7987 // let's try to look for the library directly.
7988 string resolved_lib = find_executable(lib, sess.sysroot, sess.sysenv,
7989 "LD_LIBRARY_PATH");
7990 if (resolved_lib.find('/') != string::npos)
7991 {
7992 probe *new_base = build_library_probe(*dw, resolved_lib,
7993 base, location);
7994 derive_probes(sess, new_base, finished_results);
7995 sess.print_warning(_F("'%s' is not a needed library of '%s'. "
7996 "Specify the full path to squelch this warning.",
7997 resolved_lib.c_str(), dw->module_name.c_str()));
7998 return;
7999 }
8000
8001 // Otherwise, let's suggest from the DT_NEEDED libraries
8002 string sugs = levenshtein_suggest(lib, q.visited_libraries, 5);
8003 if (!sugs.empty())
8004 throw SEMANTIC_ERROR (_NF("no match (similar library: %s)",
8005 "no match (similar libraries: %s)",
8006 sugs.find(',') == string::npos,
8007 sugs.c_str()));
8008 }
8009
8010 // If we just failed to resolve a function/plt by name, we can suggest
8011 // something. We only suggest things for probe points that were not
8012 // synthesized from a glob, i.e. only for 'real' probes. This is also
8013 // required because modules_seen needs to accumulate across recursive
8014 // calls for process(glob)[.library(glob)] probes.
8015 string func;
8016 if (results_pre == results_post && !location->from_glob
8017 && get_param(filled_parameters, TOK_FUNCTION, func)
8018 && !func.empty())
8019 {
8020 string sugs = suggest_dwarf_functions(sess, modules_seen, func);
8021 modules_seen.clear();
8022 if (!sugs.empty())
8023 throw SEMANTIC_ERROR (_NF("no match (similar function: %s)",
8024 "no match (similar functions: %s)",
8025 sugs.find(',') == string::npos,
8026 sugs.c_str()));
8027 }
8028 else if (results_pre == results_post && !location->from_glob
8029 && get_param(filled_parameters, TOK_PLT, func)
8030 && !func.empty())
8031 {
8032 string sugs = suggest_plt_functions(sess, modules_seen, func);
8033 modules_seen.clear();
8034 if (!sugs.empty())
8035 throw SEMANTIC_ERROR (_NF("no match (similar function: %s)",
8036 "no match (similar functions: %s)",
8037 sugs.find(',') == string::npos,
8038 sugs.c_str()));
8039 }
8040 else if (results_pre != results_post)
8041 // Something was derived so we won't need to suggest something
8042 modules_seen.clear();
8043 }
8044
8045 symbol_table::~symbol_table()
8046 {
8047 delete_map(map_by_addr);
8048 }
8049
8050 void
8051 symbol_table::add_symbol(const char *name, bool weak, bool descriptor,
8052 Dwarf_Addr addr, Dwarf_Addr* /*high_addr*/)
8053 {
8054 #ifdef __powerpc__
8055 // Map ".sys_foo" to "sys_foo".
8056 if (name[0] == '.')
8057 name++;
8058 #endif
8059 func_info *fi = new func_info();
8060 fi->addr = addr;
8061 fi->name = name;
8062 fi->weak = weak;
8063 fi->descriptor = descriptor;
8064 map_by_name[fi->name] = fi;
8065 // TODO: Use a multimap in case there are multiple static
8066 // functions with the same name?
8067 map_by_addr.insert(make_pair(addr, fi));
8068 }
8069
8070 void
8071 symbol_table::prepare_section_rejection(Dwfl_Module *mod __attribute__ ((unused)))
8072 {
8073 #ifdef __powerpc__
8074 /*
8075 * The .opd section contains function descriptors that can look
8076 * just like function entry points. For example, there's a function
8077 * descriptor called "do_exit" that links to the entry point ".do_exit".
8078 * Reject all symbols in .opd.
8079 */
8080 opd_section = SHN_UNDEF;
8081 Dwarf_Addr bias;
8082 Elf* elf = (dwarf_getelf (dwfl_module_getdwarf (mod, &bias))
8083 ?: dwfl_module_getelf (mod, &bias));
8084 Elf_Scn* scn = 0;
8085 size_t shstrndx;
8086
8087 if (!elf)
8088 return;
8089 if (elf_getshdrstrndx (elf, &shstrndx) != 0)
8090 return;
8091 while ((scn = elf_nextscn(elf, scn)) != NULL)
8092 {
8093 GElf_Shdr shdr_mem;
8094 GElf_Shdr *shdr = gelf_getshdr(scn, &shdr_mem);
8095 if (!shdr)
8096 continue;
8097 const char *name = elf_strptr(elf, shstrndx, shdr->sh_name);
8098 if (!strcmp(name, ".opd"))
8099 {
8100 opd_section = elf_ndxscn(scn);
8101 return;
8102 }
8103 }
8104 #endif
8105 }
8106
8107 bool
8108 symbol_table::reject_section(GElf_Word section)
8109 {
8110 if (section == SHN_UNDEF)
8111 return true;
8112 #ifdef __powerpc__
8113 if (section == opd_section)
8114 return true;
8115 #endif
8116 return false;
8117 }
8118
8119 enum info_status
8120 symbol_table::get_from_elf()
8121 {
8122 Dwarf_Addr high_addr = 0;
8123 Dwfl_Module *mod = mod_info->mod;
8124 int syments = dwfl_module_getsymtab(mod);
8125 assert(syments);
8126 prepare_section_rejection(mod);
8127 for (int i = 1; i < syments; ++i)
8128 {
8129 GElf_Sym sym;
8130 GElf_Word section;
8131 const char *name;
8132 GElf_Addr addr;
8133 bool reject;
8134
8135 /* Note that dwfl_module_getsym does adjust the sym.st_value but doesn't
8136 try to resolve it to a function address. dwfl_module_getsym_info leaves
8137 the st_value in tact (no adjustment applied) and returns the fully
8138 resolved address separately. In that case we can simply reject the
8139 symbol if it is SHN_UNDEF and don't need to call reject_section which
8140 does extra checks to see whether the address fall in an architecture
8141 specific descriptor table (which will never be the case when using the
8142 new dwfl_module_getsym_info). dwfl_module_getsym will only provide us
8143 with the (adjusted) st_value of the symbol, which might point into a
8144 function descriptor table. So in that case we still have to call
8145 reject_section. */
8146 #if _ELFUTILS_PREREQ (0, 158)
8147 name = dwfl_module_getsym_info (mod, i, &sym, &addr, &section,
8148 NULL, NULL);
8149 reject = section == SHN_UNDEF;
8150 #else
8151 name = dwfl_module_getsym (mod, i, &sym, &section);
8152 addr = sym.st_value;
8153 reject = reject_section(section);
8154 #endif
8155
8156 if (name && GELF_ST_TYPE(sym.st_info) == STT_FUNC)
8157 add_symbol(name, (GELF_ST_BIND(sym.st_info) == STB_WEAK),
8158 reject, addr, &high_addr);
8159 if (name && GELF_ST_TYPE(sym.st_info) == STT_OBJECT
8160 && GELF_ST_BIND(sym.st_info) == STB_GLOBAL)
8161 globals[name] = addr;
8162 if (name && GELF_ST_TYPE(sym.st_info) == STT_OBJECT
8163 && GELF_ST_BIND(sym.st_info) == STB_LOCAL)
8164 locals[name] = addr;
8165 }
8166 return info_present;
8167 }
8168
8169 func_info *
8170 symbol_table::get_func_containing_address(Dwarf_Addr addr)
8171 {
8172 iterator_t iter = map_by_addr.upper_bound(addr);
8173 if (iter == map_by_addr.begin())
8174 return NULL;
8175 else
8176 return (--iter)->second;
8177 }
8178
8179 func_info *
8180 symbol_table::get_first_func()
8181 {
8182 iterator_t iter = map_by_addr.begin();
8183 return (iter)->second;
8184 }
8185
8186 func_info *
8187 symbol_table::lookup_symbol(const string& name)
8188 {
8189 map<string, func_info*>::iterator i = map_by_name.find(name);
8190 if (i == map_by_name.end())
8191 return NULL;
8192 return i->second;
8193 }
8194
8195 Dwarf_Addr
8196 symbol_table::lookup_symbol_address(const string& name)
8197 {
8198 func_info *fi = lookup_symbol(name);
8199 if (fi)
8200 return fi->addr;
8201 return 0;
8202 }
8203
8204 // This is the kernel symbol table. The kernel macro cond_syscall creates
8205 // a weak symbol for each system call and maps it to sys_ni_syscall.
8206 // For system calls not implemented elsewhere, this weak symbol shows up
8207 // in the kernel symbol table. Following the precedent of dwarfful stap,
8208 // we refuse to consider such symbols. Here we delete them from our
8209 // symbol table.
8210 // TODO: Consider generalizing this and/or making it part of blacklist
8211 // processing.
8212 void
8213 symbol_table::purge_syscall_stubs()
8214 {
8215 Dwarf_Addr stub_addr = lookup_symbol_address("sys_ni_syscall");
8216 if (stub_addr == 0)
8217 return;
8218 range_t purge_range = map_by_addr.equal_range(stub_addr);
8219 for (iterator_t iter = purge_range.first;
8220 iter != purge_range.second;
8221 )
8222 {
8223 func_info *fi = iter->second;
8224 if (fi->weak && fi->name != "sys_ni_syscall")
8225 {
8226 map_by_name.erase(fi->name);
8227 map_by_addr.erase(iter++);
8228 delete fi;
8229 }
8230 else
8231 iter++;
8232 }
8233 }
8234
8235 void
8236 module_info::get_symtab()
8237 {
8238 if (symtab_status != info_unknown)
8239 return;
8240
8241 sym_table = new symbol_table(this);
8242 if (!elf_path.empty())
8243 {
8244 symtab_status = sym_table->get_from_elf();
8245 }
8246 else
8247 {
8248 assert(name == TOK_KERNEL);
8249 symtab_status = info_absent;
8250 cerr << _("Error: Cannot find vmlinux.") << endl;;
8251 }
8252 if (symtab_status == info_absent)
8253 {
8254 delete sym_table;
8255 sym_table = NULL;
8256 return;
8257 }
8258
8259 if (name == TOK_KERNEL)
8260 sym_table->purge_syscall_stubs();
8261 }
8262
8263 // update_symtab reconciles data between the elf symbol table and the dwarf
8264 // function enumeration. It updates the symbol table entries with the dwarf
8265 // die that describes the function, which also signals to query_module_symtab
8266 // that a statement probe isn't needed. In return, it also adds aliases to the
8267 // function table for names that share the same addr/die.
8268 void
8269 module_info::update_symtab(cu_function_cache_t *funcs)
8270 {
8271 if (!sym_table)
8272 return;
8273
8274 cu_function_cache_t new_funcs;
8275
8276 for (cu_function_cache_t::iterator func = funcs->begin();
8277 func != funcs->end(); func++)
8278 {
8279 // optimization: inlines will never be in the symbol table
8280 if (dwarf_func_inline(&func->second) != 0)
8281 {
8282 inlined_funcs.insert(func->first);
8283 continue;
8284 }
8285
8286 // XXX We may want to make additional efforts to match mangled elf names
8287 // to dwarf too. MIPS_linkage_name can help, but that's sometimes
8288 // missing, so we may also need to try matching by address. See also the
8289 // notes about _Z in dwflpp::iterate_over_functions().
8290
8291 func_info *fi = sym_table->lookup_symbol(func->first);
8292 if (!fi)
8293 continue;
8294
8295 // iterate over all functions at the same address
8296 symbol_table::range_t er = sym_table->map_by_addr.equal_range(fi->addr);
8297 for (symbol_table::iterator_t it = er.first; it != er.second; ++it)
8298 {
8299 // update this function with the dwarf die
8300 it->second->die = func->second;
8301
8302 // if this function is a new alias, then
8303 // save it to merge into the function cache
8304 if (it->second != fi)
8305 new_funcs.insert(make_pair(it->second->name, it->second->die));
8306 }
8307 }
8308
8309 // add all discovered aliases back into the function cache
8310 // NB: this won't replace any names that dwarf may have already found
8311 funcs->insert(new_funcs.begin(), new_funcs.end());
8312 }
8313
8314 module_info::~module_info()
8315 {
8316 if (sym_table)
8317 delete sym_table;
8318 }
8319
8320 // ------------------------------------------------------------------------
8321 // user-space probes
8322 // ------------------------------------------------------------------------
8323
8324
8325 struct uprobe_derived_probe_group: public generic_dpg<uprobe_derived_probe>
8326 {
8327 private:
8328 string make_pbm_key (uprobe_derived_probe* p) {
8329 return p->path + "|" + p->module + "|" + p->section + "|" + lex_cast(p->pid);
8330 }
8331
8332 void emit_module_maxuprobes (systemtap_session& s);
8333
8334 // Using our own utrace-based uprobes
8335 void emit_module_utrace_decls (systemtap_session& s);
8336 void emit_module_utrace_init (systemtap_session& s);
8337 void emit_module_utrace_exit (systemtap_session& s);
8338
8339 // Using the upstream inode-based uprobes
8340 void emit_module_inode_decls (systemtap_session& s);
8341 void emit_module_inode_init (systemtap_session& s);
8342 void emit_module_inode_refresh (systemtap_session& s);
8343 void emit_module_inode_exit (systemtap_session& s);
8344
8345 // Using the dyninst backend (via stapdyn)
8346 void emit_module_dyninst_decls (systemtap_session& s);
8347 void emit_module_dyninst_init (systemtap_session& s);
8348 void emit_module_dyninst_exit (systemtap_session& s);
8349
8350 public:
8351 void emit_module_decls (systemtap_session& s);
8352 void emit_module_init (systemtap_session& s);
8353 void emit_module_refresh (systemtap_session& s);
8354 void emit_module_exit (systemtap_session& s);
8355
8356 // on-the-fly only supported for inode-uprobes
8357 bool otf_supported (systemtap_session& s)
8358 { return !s.runtime_usermode_p()
8359 && kernel_supports_inode_uprobes(s); }
8360
8361 // workqueue manipulation is safe in uprobes
8362 bool otf_safe_context (systemtap_session& s)
8363 { return otf_supported(s); }
8364 };
8365
8366
8367 void
8368 uprobe_derived_probe::join_group (systemtap_session& s)
8369 {
8370 if (! s.uprobe_derived_probes)
8371 s.uprobe_derived_probes = new uprobe_derived_probe_group ();
8372 s.uprobe_derived_probes->enroll (this);
8373 this->group = s.uprobe_derived_probes;
8374
8375 if (s.runtime_usermode_p())
8376 enable_dynprobes(s);
8377 else
8378 enable_task_finder(s);
8379
8380 // Ask buildrun.cxx to build extra module if needed, and
8381 // signal staprun to load that module. If we're using the builtin
8382 // inode-uprobes, we still need to know that it is required.
8383 s.need_uprobes = true;
8384 }
8385
8386
8387 void
8388 uprobe_derived_probe::getargs(std::list<std::string> &arg_set) const
8389 {
8390 dwarf_derived_probe::getargs(arg_set);
8391 arg_set.insert(arg_set.end(), args.begin(), args.end());
8392 }
8393
8394
8395 void
8396 uprobe_derived_probe::saveargs(int nargs)
8397 {
8398 for (int i = 1; i <= nargs; i++)
8399 args.push_back("$arg" + lex_cast (i) + ":long");
8400 }
8401
8402
8403 void
8404 uprobe_derived_probe::emit_privilege_assertion (translator_output* o)
8405 {
8406 // These probes are allowed for unprivileged users, but only in the
8407 // context of processes which they own.
8408 emit_process_owner_assertion (o);
8409 }
8410
8411
8412 struct uprobe_builder: public derived_probe_builder
8413 {
8414 uprobe_builder() {}
8415 virtual void build(systemtap_session & sess,
8416 probe * base,
8417 probe_point * location,
8418 literal_map_t const & parameters,
8419 vector<derived_probe *> & finished_results)
8420 {
8421 int64_t process, address;
8422
8423 if (kernel_supports_inode_uprobes(sess))
8424 throw SEMANTIC_ERROR (_("absolute process probes not available [man error::inode-uprobes]"));
8425
8426 bool b1 = get_param (parameters, TOK_PROCESS, process);
8427 (void) b1;
8428 bool b2 = get_param (parameters, TOK_STATEMENT, address);
8429 (void) b2;
8430 bool rr = has_null_param (parameters, TOK_RETURN);
8431 assert (b1 && b2); // by pattern_root construction
8432
8433 finished_results.push_back(new uprobe_derived_probe(base, location, process, address, rr));
8434 }
8435 };
8436
8437
8438 void
8439 uprobe_derived_probe_group::emit_module_maxuprobes (systemtap_session& s)
8440 {
8441 // We'll probably need at least this many:
8442 unsigned minuprobes = probes.size();
8443 // .. but we don't want so many that .bss is inflated (PR10507):
8444 unsigned uprobesize = 64;
8445 unsigned maxuprobesmem = 10*1024*1024; // 10 MB
8446 unsigned maxuprobes = maxuprobesmem / uprobesize;
8447
8448 // Let's choose a value on the geometric middle. This should end up
8449 // between minuprobes and maxuprobes. It's OK if this number turns
8450 // out to be < minuprobes or > maxuprobes. At worst, we get a
8451 // run-time error of one kind (too few: missed uprobe registrations)
8452 // or another (too many: vmalloc errors at module load time).
8453 unsigned default_maxuprobes = (unsigned)sqrt((double)minuprobes * (double)maxuprobes);
8454
8455 s.op->newline() << "#ifndef MAXUPROBES";
8456 s.op->newline() << "#define MAXUPROBES " << default_maxuprobes;
8457 s.op->newline() << "#endif";
8458 }
8459
8460
8461 void
8462 uprobe_derived_probe_group::emit_module_utrace_decls (systemtap_session& s)
8463 {
8464 if (probes.empty()) return;
8465 s.op->newline() << "/* ---- utrace uprobes ---- */";
8466 // If uprobes isn't in the kernel, pull it in from the runtime.
8467
8468 s.op->newline() << "#if defined(CONFIG_UPROBES) || defined(CONFIG_UPROBES_MODULE)";
8469 s.op->newline() << "#include <linux/uprobes.h>";
8470 s.op->newline() << "#else";
8471 s.op->newline() << "#include \"linux/uprobes/uprobes.h\"";
8472 s.op->newline() << "#endif";
8473 s.op->newline() << "#ifndef UPROBES_API_VERSION";
8474 s.op->newline() << "#define UPROBES_API_VERSION 1";
8475 s.op->newline() << "#endif";
8476
8477 emit_module_maxuprobes (s);
8478
8479 // Forward decls
8480 s.op->newline() << "#include \"linux/uprobes-common.h\"";
8481
8482 // In .bss, the shared pool of uprobe/uretprobe structs. These are
8483 // too big to embed in the initialized .data stap_uprobe_spec array.
8484 // XXX: consider a slab cache or somesuch for stap_uprobes
8485 s.op->newline() << "static struct stap_uprobe stap_uprobes [MAXUPROBES];";
8486 s.op->newline() << "static DEFINE_MUTEX(stap_uprobes_lock);"; // protects against concurrent registration/unregistration
8487
8488 s.op->assert_0_indent();
8489
8490 // Assign task-finder numbers as we build up the stap_uprobe_tf table.
8491 // This means we process probes[] in two passes.
8492 map <string,unsigned> module_index;
8493 unsigned module_index_ctr = 0;
8494
8495 // not const since embedded task_finder_target struct changes
8496 s.op->newline() << "static struct stap_uprobe_tf stap_uprobe_finders[] = {";
8497 s.op->indent(1);
8498 for (unsigned i=0; i<probes.size(); i++)
8499 {
8500 uprobe_derived_probe *p = probes[i];
8501 string pbmkey = make_pbm_key (p);
8502 if (module_index.find (pbmkey) == module_index.end())
8503 {
8504 module_index[pbmkey] = module_index_ctr++;
8505
8506 s.op->newline() << "{";
8507 // NB: it's essential that make_pbm_key() use all of and
8508 // only the same fields as we're about to emit.
8509 s.op->line() << " .finder={";
8510 s.op->line() << " .purpose=\"uprobes\",";
8511 if (p->pid != 0)
8512 s.op->line() << " .pid=" << p->pid << ",";
8513
8514 if (p->section == "") // .statement(addr).absolute
8515 s.op->line() << " .callback=&stap_uprobe_process_found,";
8516 else if (p->section == ".absolute") // proxy for ET_EXEC -> exec()'d program
8517 {
8518 s.op->line() << " .procname=" << lex_cast_qstring(p->module) << ",";
8519 s.op->line() << " .callback=&stap_uprobe_process_found,";
8520 }
8521 else if (p->section != ".absolute") // ET_DYN
8522 {
8523 if (p->has_library)
8524 s.op->line() << " .procname=\"" << p->path << "\", ";
8525 s.op->line() << " .mmap_callback=&stap_uprobe_mmap_found, ";
8526 s.op->line() << " .munmap_callback=&stap_uprobe_munmap_found, ";
8527 s.op->line() << " .callback=&stap_uprobe_process_munmap,";
8528 }
8529 s.op->line() << " },";
8530 if (p->module != "")
8531 s.op->line() << " .pathname=" << lex_cast_qstring(p->module) << ", ";
8532 s.op->line() << " },";
8533 }
8534 else
8535 { } // skip it in this pass, already have a suitable stap_uprobe_tf slot for it.
8536 }
8537 s.op->newline(-1) << "};";
8538
8539 s.op->assert_0_indent();
8540
8541 unsigned pci;
8542 for (pci=0; pci<probes.size(); pci++)
8543 {
8544 // List of perf counters used by each probe
8545 // This list is an index into struct stap_perf_probe,
8546 uprobe_derived_probe *p = probes[pci];
8547 std::set<string>::iterator pcii;
8548 s.op->newline() << "long perf_counters_" + lex_cast(pci) + "[] = {";
8549 for (pcii = p->perf_counter_refs.begin();
8550 pcii != p->perf_counter_refs.end(); pcii++)
8551 {
8552 std::vector<std::pair<std::string,std::string> >::iterator it;
8553 unsigned i = 0;
8554 // Find the associated perf.counter probe
8555 for (it=s.perf_counters.begin() ;
8556 it != s.perf_counters.end(); it++, i++)
8557 if ((*it).first == (*pcii))
8558 break;
8559 s.op->line() << lex_cast(i) << ", ";
8560 }
8561 s.op->newline() << "};";
8562 }
8563
8564 // NB: read-only structure
8565 s.op->newline() << "static const struct stap_uprobe_spec stap_uprobe_specs [] = {";
8566 s.op->indent(1);
8567 for (unsigned i =0; i<probes.size(); i++)
8568 {
8569 uprobe_derived_probe* p = probes[i];
8570 s.op->newline() << "{";
8571 string key = make_pbm_key (p);
8572 unsigned value = module_index[key];
8573 if (value != 0)
8574 s.op->line() << " .tfi=" << value << ",";
8575 s.op->line() << " .address=(unsigned long)0x" << hex << p->addr << dec << "ULL,";
8576 s.op->line() << " .probe=" << common_probe_init (p) << ",";
8577
8578 if (p->sdt_semaphore_addr != 0)
8579 s.op->line() << " .sdt_sem_offset=(unsigned long)0x"
8580 << hex << p->sdt_semaphore_addr << dec << "ULL,";
8581
8582 // XXX: don't bother emit if array is empty
8583 s.op->line() << " .perf_counters_dim=ARRAY_SIZE(perf_counters_" << lex_cast(i) << "),";
8584 // List of perf counters used by a probe from above
8585 s.op->line() << " .perf_counters=perf_counters_" + lex_cast(i) + ",";
8586
8587 if (p->has_return)
8588 s.op->line() << " .return_p=1,";
8589 s.op->line() << " },";
8590 }
8591 s.op->newline(-1) << "};";
8592
8593 s.op->assert_0_indent();
8594
8595 s.op->newline() << "static void enter_uprobe_probe (struct uprobe *inst, struct pt_regs *regs) {";
8596 s.op->newline(1) << "struct stap_uprobe *sup = container_of(inst, struct stap_uprobe, up);";
8597 s.op->newline() << "const struct stap_uprobe_spec *sups = &stap_uprobe_specs [sup->spec_index];";
8598 common_probe_entryfn_prologue (s, "STAP_SESSION_RUNNING", "sups->probe",
8599 "stp_probe_type_uprobe");
8600 s.op->newline() << "if (sup->spec_index < 0 || "
8601 << "sup->spec_index >= " << probes.size() << ") {";
8602 s.op->newline(1) << "_stp_error (\"bad spec_index %d (max " << probes.size()
8603 << "): %s\", sup->spec_index, c->probe_point);";
8604 s.op->newline() << "goto probe_epilogue;";
8605 s.op->newline(-1) << "}";
8606 s.op->newline() << "c->uregs = regs;";
8607 s.op->newline() << "c->user_mode_p = 1;";
8608
8609 // Make it look like the IP is set as it would in the actual user
8610 // task when calling real probe handler. Reset IP regs on return, so
8611 // we don't confuse uprobes. PR10458
8612 s.op->newline() << "{";
8613 s.op->indent(1);
8614 s.op->newline() << "unsigned long uprobes_ip = REG_IP(c->uregs);";
8615 s.op->newline() << "SET_REG_IP(regs, inst->vaddr);";
8616 s.op->newline() << "(*sups->probe->ph) (c);";
8617 s.op->newline() << "SET_REG_IP(regs, uprobes_ip);";
8618 s.op->newline(-1) << "}";
8619
8620 common_probe_entryfn_epilogue (s, true, otf_safe_context(s));
8621 s.op->newline(-1) << "}";
8622
8623 s.op->newline() << "static void enter_uretprobe_probe (struct uretprobe_instance *inst, struct pt_regs *regs) {";
8624 s.op->newline(1) << "struct stap_uprobe *sup = container_of(inst->rp, struct stap_uprobe, urp);";
8625 s.op->newline() << "const struct stap_uprobe_spec *sups = &stap_uprobe_specs [sup->spec_index];";
8626 common_probe_entryfn_prologue (s, "STAP_SESSION_RUNNING", "sups->probe",
8627 "stp_probe_type_uretprobe");
8628 s.op->newline() << "c->ips.ri = inst;";
8629 s.op->newline() << "if (sup->spec_index < 0 || "
8630 << "sup->spec_index >= " << probes.size() << ") {";
8631 s.op->newline(1) << "_stp_error (\"bad spec_index %d (max " << probes.size()
8632 << "): %s\", sup->spec_index, c->probe_point);";
8633 s.op->newline() << "goto probe_epilogue;";
8634 s.op->newline(-1) << "}";
8635
8636 s.op->newline() << "c->uregs = regs;";
8637 s.op->newline() << "c->user_mode_p = 1;";
8638
8639 // Make it look like the IP is set as it would in the actual user
8640 // task when calling real probe handler. Reset IP regs on return, so
8641 // we don't confuse uprobes. PR10458
8642 s.op->newline() << "{";
8643 s.op->indent(1);
8644 s.op->newline() << "unsigned long uprobes_ip = REG_IP(c->uregs);";
8645 s.op->newline() << "SET_REG_IP(regs, inst->ret_addr);";
8646 s.op->newline() << "(*sups->probe->ph) (c);";
8647 s.op->newline() << "SET_REG_IP(regs, uprobes_ip);";
8648 s.op->newline(-1) << "}";
8649
8650 common_probe_entryfn_epilogue (s, true, otf_safe_context(s));
8651 s.op->newline(-1) << "}";
8652
8653 s.op->newline();
8654 s.op->newline() << "#include \"linux/uprobes-common.c\"";
8655 s.op->newline();
8656 }
8657
8658
8659 void
8660 uprobe_derived_probe_group::emit_module_utrace_init (systemtap_session& s)
8661 {
8662 if (probes.empty()) return;
8663
8664 s.op->newline() << "/* ---- utrace uprobes ---- */";
8665
8666 s.op->newline() << "for (j=0; j<MAXUPROBES; j++) {";
8667 s.op->newline(1) << "struct stap_uprobe *sup = & stap_uprobes[j];";
8668 s.op->newline() << "sup->spec_index = -1;"; // free slot
8669 // NB: we assume the rest of the struct (specificaly, sup->up) is
8670 // initialized to zero. This is so that we can use
8671 // sup->up->kdata = NULL for "really free!" PR 6829.
8672 s.op->newline(-1) << "}";
8673 s.op->newline() << "mutex_init (& stap_uprobes_lock);";
8674
8675 // Set up the task_finders
8676 s.op->newline() << "for (i=0; i<sizeof(stap_uprobe_finders)/sizeof(stap_uprobe_finders[0]); i++) {";
8677 s.op->newline(1) << "struct stap_uprobe_tf *stf = & stap_uprobe_finders[i];";
8678 s.op->newline() << "probe_point = stf->pathname;"; // for error messages; XXX: would prefer pp() or something better
8679 s.op->newline() << "rc = stap_register_task_finder_target (& stf->finder);";
8680
8681 // NB: if (rc), there is no need (XXX: nor any way) to clean up any
8682 // finders already registered, since mere registration does not
8683 // cause any utrace or memory allocation actions. That happens only
8684 // later, once the task finder engine starts running. So, for a
8685 // partial initialization requiring unwind, we need do nothing.
8686 s.op->newline() << "if (rc) break;";
8687
8688 s.op->newline(-1) << "}";
8689 }
8690
8691
8692 void
8693 uprobe_derived_probe_group::emit_module_utrace_exit (systemtap_session& s)
8694 {
8695 if (probes.empty()) return;
8696 s.op->newline() << "/* ---- utrace uprobes ---- */";
8697
8698 // NB: there is no stap_unregister_task_finder_target call;
8699 // important stuff like utrace cleanups are done by
8700 // __stp_task_finder_cleanup() via stap_stop_task_finder().
8701 //
8702 // This function blocks until all callbacks are completed, so there
8703 // is supposed to be no possibility of any registration-related code starting
8704 // to run in parallel with our shutdown here. So we don't need to protect the
8705 // stap_uprobes[] array with the mutex.
8706
8707 s.op->newline() << "for (j=0; j<MAXUPROBES; j++) {";
8708 s.op->newline(1) << "struct stap_uprobe *sup = & stap_uprobes[j];";
8709 s.op->newline() << "const struct stap_uprobe_spec *sups = &stap_uprobe_specs [sup->spec_index];";
8710 s.op->newline() << "if (sup->spec_index < 0) continue;"; // free slot
8711
8712 // PR10655: decrement that ENABLED semaphore
8713 s.op->newline() << "if (sup->sdt_sem_address) {";
8714 s.op->newline(1) << "unsigned short sdt_semaphore;"; // NB: fixed size
8715 s.op->newline() << "pid_t pid = (sups->return_p ? sup->urp.u.pid : sup->up.pid);";
8716 s.op->newline() << "struct task_struct *tsk;";
8717 s.op->newline() << "rcu_read_lock();";
8718
8719 // Do a pid->task_struct* lookup. For 2.6.24+, this code assumes
8720 // that the pid is always in the global namespace, not in any
8721 // private namespace.
8722 s.op->newline() << "#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,24)";
8723 // We'd like to call find_task_by_pid_ns() here, but it isn't
8724 // exported. So, we call what it calls...
8725 s.op->newline() << " tsk = pid_task(find_pid_ns(pid, &init_pid_ns), PIDTYPE_PID);";
8726 s.op->newline() << "#else";
8727 s.op->newline() << " tsk = find_task_by_pid (pid);";
8728 s.op->newline() << "#endif /* 2.6.24 */";
8729
8730 s.op->newline() << "if (tsk) {"; // just in case the thing exited while we weren't watching
8731 s.op->newline(1) << "if (__access_process_vm_noflush(tsk, sup->sdt_sem_address, &sdt_semaphore, sizeof(sdt_semaphore), 0)) {";
8732 s.op->newline(1) << "sdt_semaphore --;";
8733 s.op->newline() << "#ifdef DEBUG_UPROBES";
8734 s.op->newline() << "_stp_dbug (__FUNCTION__,__LINE__, \"-semaphore %#x @ %#lx\\n\", sdt_semaphore, sup->sdt_sem_address);";
8735 s.op->newline() << "#endif";
8736 s.op->newline() << "__access_process_vm_noflush(tsk, sup->sdt_sem_address, &sdt_semaphore, sizeof(sdt_semaphore), 1);";
8737 s.op->newline(-1) << "}";
8738 // XXX: need to analyze possibility of race condition
8739 s.op->newline(-1) << "}";
8740 s.op->newline() << "rcu_read_unlock();";
8741 s.op->newline(-1) << "}";
8742
8743 s.op->newline() << "if (sups->return_p) {";
8744 s.op->newline(1) << "#ifdef DEBUG_UPROBES";
8745 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);";
8746 s.op->newline() << "#endif";
8747 // NB: PR6829 does not change that we still need to unregister at
8748 // *this* time -- when the script as a whole exits.
8749 s.op->newline() << "unregister_uretprobe (& sup->urp);";
8750 s.op->newline(-1) << "} else {";
8751 s.op->newline(1) << "#ifdef DEBUG_UPROBES";
8752 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);";
8753 s.op->newline() << "#endif";
8754 s.op->newline() << "unregister_uprobe (& sup->up);";
8755 s.op->newline(-1) << "}";
8756
8757 s.op->newline() << "sup->spec_index = -1;";
8758
8759 // XXX: uprobe missed counts?
8760
8761 s.op->newline(-1) << "}";
8762
8763 s.op->newline() << "mutex_destroy (& stap_uprobes_lock);";
8764 }
8765
8766
8767 void
8768 uprobe_derived_probe_group::emit_module_inode_decls (systemtap_session& s)
8769 {
8770 if (probes.empty()) return;
8771 s.op->newline() << "/* ---- inode uprobes ---- */";
8772 emit_module_maxuprobes (s);
8773 s.op->newline() << "#include \"linux/uprobes-inode.c\"";
8774
8775 // Write the probe handler.
8776 s.op->newline() << "static int stapiu_probe_handler "
8777 << "(struct stapiu_consumer *sup, struct pt_regs *regs) {";
8778 s.op->newline(1);
8779
8780 // Since we're sharing the entry function, we have to dynamically choose the probe_type
8781 string probe_type = "(sup->return_p ? stp_probe_type_uretprobe : stp_probe_type_uprobe)";
8782 common_probe_entryfn_prologue (s, "STAP_SESSION_RUNNING", "sup->probe",
8783 probe_type);
8784
8785 s.op->newline() << "c->uregs = regs;";
8786 s.op->newline() << "c->user_mode_p = 1;";
8787 // NB: IP is already set by stapiu_probe_prehandler in uprobes-inode.c
8788 s.op->newline() << "(*sup->probe->ph) (c);";
8789
8790 common_probe_entryfn_epilogue (s, true, otf_safe_context(s));
8791 s.op->newline() << "return 0;";
8792 s.op->newline(-1) << "}";
8793 s.op->assert_0_indent();
8794
8795 // Index of all the modules for which we need inodes.
8796 map<string, unsigned> module_index;
8797 unsigned module_index_ctr = 0;
8798
8799 // Discover and declare targets for each unique path.
8800 s.op->newline() << "static struct stapiu_target "
8801 << "stap_inode_uprobe_targets[] = {";
8802 s.op->indent(1);
8803 for (unsigned i=0; i<probes.size(); i++)
8804 {
8805 uprobe_derived_probe *p = probes[i];
8806 const string key = make_pbm_key(p);
8807 if (module_index.find (key) == module_index.end())
8808 {
8809 module_index[key] = module_index_ctr++;
8810 s.op->newline() << "{";
8811 s.op->line() << " .finder={";
8812 s.op->line() << " .purpose=\"inode-uprobes\",";
8813 if (p->pid != 0)
8814 s.op->line() << " .pid=" << p->pid << ",";
8815
8816 if (p->section == "") // .statement(addr).absolute XXX?
8817 s.op->line() << " .callback=&stapiu_process_found,";
8818 else if (p->section == ".absolute") // proxy for ET_EXEC -> exec()'d program
8819 {
8820 s.op->line() << " .procname=" << lex_cast_qstring(p->module) << ",";
8821 s.op->line() << " .callback=&stapiu_process_found,";
8822 }
8823 else if (p->section != ".absolute") // ET_DYN
8824 {
8825 if (p->has_library)
8826 s.op->line() << " .procname=\"" << p->path << "\", ";
8827 s.op->line() << " .mmap_callback=&stapiu_mmap_found, ";
8828 s.op->line() << " .munmap_callback=&stapiu_munmap_found, ";
8829 s.op->line() << " .callback=&stapiu_process_munmap,";
8830 }
8831 s.op->line() << " },";
8832 s.op->line() << " .filename=" << lex_cast_qstring(p->module) << ",";
8833 s.op->line() << " },";
8834 }
8835 }
8836 s.op->newline(-1) << "};";
8837 s.op->assert_0_indent();
8838
8839 // Declare the actual probes.
8840 unsigned pci;
8841 for (pci=0; pci<probes.size(); pci++)
8842 {
8843 // List of perf counters used by each probe
8844 // This list is an index into struct stap_perf_probe,
8845 uprobe_derived_probe *p = probes[pci];
8846 std::set<string>::iterator pcii;
8847 s.op->newline() << "long perf_counters_" + lex_cast(pci) + "[] = {";
8848 for (pcii = p->perf_counter_refs.begin();
8849 pcii != p->perf_counter_refs.end(); pcii++)
8850 {
8851 vector<std::pair<string,string> >:: iterator it;
8852 unsigned i = 0;
8853 // Find the associated perf.counter probe
8854 for (it=s.perf_counters.begin() ;
8855 it != s.perf_counters.end(); it++, i++)
8856 if ((*it).first == (*pcii))
8857 break;
8858 s.op->line() << lex_cast(i) << ", ";
8859 }
8860 s.op->newline() << "};";
8861 }
8862
8863 s.op->newline() << "static struct stapiu_consumer "
8864 << "stap_inode_uprobe_consumers[] = {";
8865 s.op->indent(1);
8866 for (unsigned i=0; i<probes.size(); i++)
8867 {
8868 uprobe_derived_probe *p = probes[i];
8869 unsigned index = module_index[make_pbm_key(p)];
8870 s.op->newline() << "{";
8871 if (p->has_return)
8872 s.op->line() << " .return_p=1,";
8873 s.op->line() << " .target=&stap_inode_uprobe_targets[" << index << "],";
8874 s.op->line() << " .offset=(loff_t)0x" << hex << p->addr << dec << "ULL,";
8875 if (p->sdt_semaphore_addr)
8876 s.op->line() << " .sdt_sem_offset=(loff_t)0x"
8877 << hex << p->sdt_semaphore_addr << dec << "ULL,";
8878 // XXX: don't bother emit if array is empty
8879 s.op->line() << " .perf_counters_dim=ARRAY_SIZE(perf_counters_" << lex_cast(i) << "),";
8880 // List of perf counters used by a probe from above
8881 s.op->line() << " .perf_counters=perf_counters_" + lex_cast(i) + ",";
8882 s.op->line() << " .probe=" << common_probe_init (p) << ",";
8883 s.op->line() << " },";
8884 }
8885 s.op->newline(-1) << "};";
8886 s.op->assert_0_indent();
8887 }
8888
8889
8890 void
8891 uprobe_derived_probe_group::emit_module_inode_init (systemtap_session& s)
8892 {
8893 if (probes.empty()) return;
8894 s.op->newline() << "/* ---- inode uprobes ---- */";
8895 // Let stapiu_init() handle reporting errors by setting probe_point
8896 // to NULL.
8897 s.op->newline() << "probe_point = NULL;";
8898 s.op->newline() << "rc = stapiu_init ("
8899 << "stap_inode_uprobe_targets, "
8900 << "ARRAY_SIZE(stap_inode_uprobe_targets), "
8901 << "stap_inode_uprobe_consumers, "
8902 << "ARRAY_SIZE(stap_inode_uprobe_consumers));";
8903 }
8904
8905
8906 void
8907 uprobe_derived_probe_group::emit_module_inode_refresh (systemtap_session& s)
8908 {
8909 if (probes.empty()) return;
8910 s.op->newline() << "/* ---- inode uprobes ---- */";
8911 s.op->newline() << "stapiu_refresh ("
8912 << "stap_inode_uprobe_targets, "
8913 << "ARRAY_SIZE(stap_inode_uprobe_targets));";
8914 }
8915
8916
8917 void
8918 uprobe_derived_probe_group::emit_module_inode_exit (systemtap_session& s)
8919 {
8920 if (probes.empty()) return;
8921 s.op->newline() << "/* ---- inode uprobes ---- */";
8922 s.op->newline() << "stapiu_exit ("
8923 << "stap_inode_uprobe_targets, "
8924 << "ARRAY_SIZE(stap_inode_uprobe_targets), "
8925 << "stap_inode_uprobe_consumers, "
8926 << "ARRAY_SIZE(stap_inode_uprobe_consumers));";
8927 }
8928
8929
8930 void
8931 uprobe_derived_probe_group::emit_module_dyninst_decls (systemtap_session& s)
8932 {
8933 if (probes.empty()) return;
8934 s.op->newline() << "/* ---- dyninst uprobes ---- */";
8935 emit_module_maxuprobes (s);
8936 s.op->newline() << "#include \"dyninst/uprobes.h\"";
8937
8938 // Let the dynprobe_derived_probe_group handle outputting targets
8939 // and probes. This allows us to merge different types of probes.
8940 s.op->newline() << "static struct stapdu_probe stapdu_probes[];";
8941 for (unsigned i = 0; i < probes.size(); i++)
8942 {
8943 uprobe_derived_probe *p = probes[i];
8944
8945 dynprobe_add_uprobe(s, p->module, p->addr, p->sdt_semaphore_addr,
8946 (p->has_return ? "STAPDYN_PROBE_FLAG_RETURN" : "0"),
8947 common_probe_init(p));
8948 }
8949 // loc2c-generated code assumes pt_regs are available, so use this to make
8950 // sure we always have *something* for it to dereference...
8951 s.op->newline() << "static struct pt_regs stapdu_dummy_uregs;";
8952
8953 // Write the probe handler.
8954 // NB: not static, so dyninst can find it
8955 s.op->newline() << "int enter_dyninst_uprobe "
8956 << "(uint64_t index, struct pt_regs *regs) {";
8957 s.op->newline(1) << "struct stapdu_probe *sup = &stapdu_probes[index];";
8958
8959 // Since we're sharing the entry function, we have to dynamically choose the probe_type
8960 string probe_type = "((sup->flags & STAPDYN_PROBE_FLAG_RETURN) ?"
8961 " stp_probe_type_uretprobe : stp_probe_type_uprobe)";
8962 common_probe_entryfn_prologue (s, "STAP_SESSION_RUNNING", "sup->probe",
8963 probe_type);
8964
8965 s.op->newline() << "c->uregs = regs ?: &stapdu_dummy_uregs;";
8966 s.op->newline() << "c->user_mode_p = 1;";
8967 // XXX: once we have regs, check how dyninst sets the IP
8968 // XXX: the way that dyninst rewrites stuff is probably going to be
8969 // ... very confusing to our backtracer (at least if we stay in process)
8970 s.op->newline() << "(*sup->probe->ph) (c);";
8971 common_probe_entryfn_epilogue (s, true, otf_safe_context(s));
8972 s.op->newline() << "return 0;";
8973 s.op->newline(-1) << "}";
8974 s.op->newline() << "#include \"dyninst/uprobes-regs.c\"";
8975 s.op->assert_0_indent();
8976 }
8977
8978
8979 void
8980 uprobe_derived_probe_group::emit_module_dyninst_init (systemtap_session& s)
8981 {
8982 if (probes.empty()) return;
8983
8984 /* stapdyn handles the dirty work via dyninst */
8985 s.op->newline() << "/* ---- dyninst uprobes ---- */";
8986 s.op->newline() << "/* this section left intentionally blank */";
8987 }
8988
8989
8990 void
8991 uprobe_derived_probe_group::emit_module_dyninst_exit (systemtap_session& s)
8992 {
8993 if (probes.empty()) return;
8994
8995 /* stapdyn handles the dirty work via dyninst */
8996 s.op->newline() << "/* ---- dyninst uprobes ---- */";
8997 s.op->newline() << "/* this section left intentionally blank */";
8998 }
8999
9000
9001 void
9002 uprobe_derived_probe_group::emit_module_decls (systemtap_session& s)
9003 {
9004 if (s.runtime_usermode_p())
9005 emit_module_dyninst_decls (s);
9006 else if (kernel_supports_inode_uprobes (s))
9007 emit_module_inode_decls (s);
9008 else
9009 emit_module_utrace_decls (s);
9010 }
9011
9012
9013 void
9014 uprobe_derived_probe_group::emit_module_init (systemtap_session& s)
9015 {
9016 if (s.runtime_usermode_p())
9017 emit_module_dyninst_init (s);
9018 else if (kernel_supports_inode_uprobes (s))
9019 emit_module_inode_init (s);
9020 else
9021 emit_module_utrace_init (s);
9022 }
9023
9024
9025 void
9026 uprobe_derived_probe_group::emit_module_refresh (systemtap_session& s)
9027 {
9028 if (!s.runtime_usermode_p() && kernel_supports_inode_uprobes (s))
9029 emit_module_inode_refresh (s);
9030 }
9031
9032
9033 void
9034 uprobe_derived_probe_group::emit_module_exit (systemtap_session& s)
9035 {
9036 if (s.runtime_usermode_p())
9037 emit_module_dyninst_exit (s);
9038 else if (kernel_supports_inode_uprobes (s))
9039 emit_module_inode_exit (s);
9040 else
9041 emit_module_utrace_exit (s);
9042 }
9043
9044
9045 // ------------------------------------------------------------------------
9046 // Kprobe derived probes
9047 // ------------------------------------------------------------------------
9048
9049 static const string TOK_KPROBE("kprobe");
9050
9051 struct kprobe_derived_probe: public derived_probe
9052 {
9053 kprobe_derived_probe (systemtap_session& sess,
9054 vector<derived_probe *> & results,
9055 probe *base,
9056 probe_point *location,
9057 const string& name,
9058 int64_t stmt_addr,
9059 bool has_call,
9060 bool has_return,
9061 bool has_statement,
9062 bool has_maxactive,
9063 bool has_path,
9064 bool has_library,
9065 long maxactive_val,
9066 const string& path,
9067 const string& library
9068 );
9069 string symbol_name;
9070 Dwarf_Addr addr;
9071 bool has_call;
9072 bool has_return;
9073 bool has_statement;
9074 bool has_maxactive;
9075 bool has_path;
9076 bool has_library;
9077 long maxactive_val;
9078 string path;
9079 string library;
9080 bool access_var;
9081 void printsig (std::ostream &o) const;
9082 void join_group (systemtap_session& s);
9083 };
9084
9085 struct kprobe_derived_probe_group: public derived_probe_group
9086 {
9087 private:
9088 multimap<string,kprobe_derived_probe*> probes_by_module;
9089 typedef multimap<string,kprobe_derived_probe*>::iterator p_b_m_iterator;
9090
9091 public:
9092 void enroll (kprobe_derived_probe* probe);
9093 void emit_module_decls (systemtap_session& s);
9094 void emit_module_init (systemtap_session& s);
9095 void emit_module_exit (systemtap_session& s);
9096 };
9097
9098 struct kprobe_var_expanding_visitor: public var_expanding_visitor
9099 {
9100 systemtap_session& sess;
9101 block *add_block;
9102 block *add_call_probe; // synthesized from .return probes with saved $vars
9103 bool add_block_tid, add_call_probe_tid;
9104 bool has_return;
9105
9106 kprobe_var_expanding_visitor(systemtap_session& sess, bool has_return):
9107 sess(sess), add_block(NULL), add_call_probe(NULL),
9108 add_block_tid(false), add_call_probe_tid(false),
9109 has_return(has_return) {}
9110
9111 void visit_entry_op (entry_op* e);
9112 };
9113
9114
9115 kprobe_derived_probe::kprobe_derived_probe (systemtap_session& sess,
9116 vector<derived_probe *> & results,
9117 probe *base,
9118 probe_point *location,
9119 const string& name,
9120 int64_t stmt_addr,
9121 bool has_call,
9122 bool has_return,
9123 bool has_statement,
9124 bool has_maxactive,
9125 bool has_path,
9126 bool has_library,
9127 long maxactive_val,
9128 const string& path,
9129 const string& library
9130 ):
9131 derived_probe (base, location, true /* .components soon rewritten */ ),
9132 symbol_name (name), addr (stmt_addr), has_call (has_call),
9133 has_return (has_return), has_statement (has_statement),
9134 has_maxactive (has_maxactive), has_path (has_path),
9135 has_library (has_library),
9136 maxactive_val (maxactive_val),
9137 path (path), library (library)
9138 {
9139 this->tok = base->tok;
9140 this->access_var = false;
9141
9142 #ifndef USHRT_MAX
9143 #define USHRT_MAX 32767
9144 #endif
9145
9146 // Expansion of $target variables in the probe body produces an error during
9147 // translate phase, since we're not using debuginfo
9148
9149 vector<probe_point::component*> comps;
9150 comps.push_back (new probe_point::component(TOK_KPROBE));
9151
9152 if (has_statement)
9153 {
9154 comps.push_back (new probe_point::component(TOK_STATEMENT,
9155 new literal_number(addr, true)));
9156 comps.push_back (new probe_point::component(TOK_ABSOLUTE));
9157 }
9158 else
9159 {
9160 size_t pos = name.find(':');
9161 if (pos != string::npos)
9162 {
9163 string module = name.substr(0, pos);
9164 string function = name.substr(pos + 1);
9165 comps.push_back (new probe_point::component(TOK_MODULE, new literal_string(module)));
9166 comps.push_back (new probe_point::component(TOK_FUNCTION, new literal_string(function)));
9167 }
9168 else
9169 comps.push_back (new probe_point::component(TOK_FUNCTION, new literal_string(name)));
9170 }
9171
9172 if (has_call)
9173 comps.push_back (new probe_point::component(TOK_CALL));
9174 if (has_return)
9175 comps.push_back (new probe_point::component(TOK_RETURN));
9176 if (has_maxactive)
9177 comps.push_back (new probe_point::component(TOK_MAXACTIVE, new literal_number(maxactive_val)));
9178
9179 kprobe_var_expanding_visitor v (sess, has_return);
9180 v.replace (this->body);
9181
9182 // If during target-variable-expanding the probe, we added a new block
9183 // of code, add it to the start of the probe.
9184 if (v.add_block)
9185 this->body = new block(v.add_block, this->body);
9186
9187 // If when target-variable-expanding the probe, we need to
9188 // synthesize a sibling function-entry probe. We don't go through
9189 // the whole probe derivation business (PR10642) that could lead to
9190 // wildcard/alias resolution, or for that dwarf-induced duplication.
9191 if (v.add_call_probe)
9192 {
9193 assert (has_return);
9194
9195 // We temporarily replace base.
9196 statement* old_body = base->body;
9197 base->body = v.add_call_probe;
9198
9199 derived_probe *entry_handler
9200 = new kprobe_derived_probe (sess, results, base, location, name, 0,
9201 true /* has_call */, false /* has_return */,
9202 has_statement, has_maxactive, has_path,
9203 has_library, maxactive_val, path, library);
9204 results.push_back (entry_handler);
9205
9206 base->body = old_body;
9207 }
9208
9209 this->sole_location()->components = comps;
9210 }
9211
9212 void kprobe_derived_probe::printsig (ostream& o) const
9213 {
9214 sole_location()->print (o);
9215 o << " /* " << " name = " << symbol_name << "*/";
9216 printsig_nested (o);
9217 }
9218
9219 void kprobe_derived_probe::join_group (systemtap_session& s)
9220 {
9221 if (! s.kprobe_derived_probes)
9222 s.kprobe_derived_probes = new kprobe_derived_probe_group ();
9223 s.kprobe_derived_probes->enroll (this);
9224 this->group = s.kprobe_derived_probes;
9225 }
9226
9227 void kprobe_derived_probe_group::enroll (kprobe_derived_probe* p)
9228 {
9229 probes_by_module.insert (make_pair (p->symbol_name, p));
9230 // probes of same symbol should share single kprobe/kretprobe
9231 }
9232
9233 void
9234 kprobe_derived_probe_group::emit_module_decls (systemtap_session& s)
9235 {
9236 if (probes_by_module.empty()) return;
9237
9238 s.op->newline() << "/* ---- kprobe-based probes ---- */";
9239
9240 // Warn of misconfigured kernels
9241 s.op->newline() << "#if ! defined(CONFIG_KPROBES)";
9242 s.op->newline() << "#error \"Need CONFIG_KPROBES!\"";
9243 s.op->newline() << "#endif";
9244 s.op->newline();
9245
9246 s.op->newline() << "#ifndef KRETACTIVE";
9247 s.op->newline() << "#define KRETACTIVE (max(15,6*(int)num_possible_cpus()))";
9248 s.op->newline() << "#endif";
9249
9250 // Forward declare the master entry functions
9251 s.op->newline() << "static int enter_kprobe2_probe (struct kprobe *inst,";
9252 s.op->line() << " struct pt_regs *regs);";
9253 s.op->newline() << "static int enter_kretprobe2_probe (struct kretprobe_instance *inst,";
9254 s.op->line() << " struct pt_regs *regs);";
9255
9256 // Emit an array of kprobe/kretprobe pointers
9257 s.op->newline() << "#if defined(STAPCONF_UNREGISTER_KPROBES)";
9258 s.op->newline() << "static void * stap_unreg_kprobes2[" << probes_by_module.size() << "];";
9259 s.op->newline() << "#endif";
9260
9261 // Emit the actual probe list.
9262
9263 s.op->newline() << "static struct stap_dwarfless_kprobe {";
9264 s.op->newline(1) << "union { struct kprobe kp; struct kretprobe krp; } u;";
9265 s.op->newline() << "#ifdef __ia64__";
9266 s.op->newline() << "struct kprobe dummy;";
9267 s.op->newline() << "#endif";
9268 s.op->newline(-1) << "} stap_dwarfless_kprobes[" << probes_by_module.size() << "];";
9269 // NB: bss!
9270
9271 s.op->newline() << "static struct stap_dwarfless_probe {";
9272 s.op->newline(1) << "const unsigned return_p:1;";
9273 s.op->newline() << "const unsigned maxactive_p:1;";
9274 s.op->newline() << "const unsigned optional_p:1;";
9275 s.op->newline() << "unsigned registered_p:1;";
9276 s.op->newline() << "const unsigned short maxactive_val;";
9277
9278 // Function Names are mostly small and uniform enough to justify putting
9279 // char[MAX]'s into the array instead of relocated char*'s.
9280
9281 size_t symbol_string_name_max = 0;
9282 size_t symbol_string_name_tot = 0;
9283 for (p_b_m_iterator it = probes_by_module.begin(); it != probes_by_module.end(); it++)
9284 {
9285 kprobe_derived_probe* p = it->second;
9286 #define DOIT(var,expr) do { \
9287 size_t var##_size = (expr) + 1; \
9288 var##_max = max (var##_max, var##_size); \
9289 var##_tot += var##_size; } while (0)
9290 DOIT(symbol_string_name, p->symbol_name.size());
9291 #undef DOIT
9292 }
9293
9294 #define CALCIT(var) \
9295 s.op->newline() << "const char " << #var << "[" << var##_name_max << "] ;";
9296
9297 CALCIT(symbol_string);
9298 #undef CALCIT
9299
9300 s.op->newline() << "unsigned long address;";
9301 s.op->newline() << "const struct stap_probe * const probe;";
9302 s.op->newline(-1) << "} stap_dwarfless_probes[] = {";
9303 s.op->indent(1);
9304
9305 for (p_b_m_iterator it = probes_by_module.begin(); it != probes_by_module.end(); it++)
9306 {
9307 kprobe_derived_probe* p = it->second;
9308 s.op->newline() << "{";
9309 if (p->has_return)
9310 s.op->line() << " .return_p=1,";
9311
9312 if (p->has_maxactive)
9313 {
9314 s.op->line() << " .maxactive_p=1,";
9315 assert (p->maxactive_val >= 0 && p->maxactive_val <= USHRT_MAX);
9316 s.op->line() << " .maxactive_val=" << p->maxactive_val << ",";
9317 }
9318
9319 if (p->locations[0]->optional)
9320 s.op->line() << " .optional_p=1,";
9321
9322 if (p->has_statement)
9323 s.op->line() << " .address=(unsigned long)0x" << hex << p->addr << dec << "ULL,";
9324 else
9325 s.op->line() << " .symbol_string=\"" << p->symbol_name << "\",";
9326
9327 s.op->line() << " .probe=" << common_probe_init (p) << ",";
9328 s.op->line() << " },";
9329 }
9330
9331 s.op->newline(-1) << "};";
9332
9333 // Emit the kprobes callback function
9334 s.op->newline();
9335 s.op->newline() << "static int enter_kprobe2_probe (struct kprobe *inst,";
9336 s.op->line() << " struct pt_regs *regs) {";
9337 // NB: as of PR5673, the kprobe|kretprobe union struct is in BSS
9338 s.op->newline(1) << "int kprobe_idx = ((uintptr_t)inst-(uintptr_t)stap_dwarfless_kprobes)/sizeof(struct stap_dwarfless_kprobe);";
9339 // Check that the index is plausible
9340 s.op->newline() << "struct stap_dwarfless_probe *sdp = &stap_dwarfless_probes[";
9341 s.op->line() << "((kprobe_idx >= 0 && kprobe_idx < " << probes_by_module.size() << ")?";
9342 s.op->line() << "kprobe_idx:0)"; // NB: at least we avoid memory corruption
9343 // XXX: it would be nice to give a more verbose error though; BUG_ON later?
9344 s.op->line() << "];";
9345 common_probe_entryfn_prologue (s, "STAP_SESSION_RUNNING", "sdp->probe",
9346 "stp_probe_type_kprobe");
9347 s.op->newline() << "c->kregs = regs;";
9348
9349 // Make it look like the IP is set as it wouldn't have been replaced
9350 // by a breakpoint instruction when calling real probe handler. Reset
9351 // IP regs on return, so we don't confuse kprobes. PR10458
9352 s.op->newline() << "{";
9353 s.op->indent(1);
9354 s.op->newline() << "unsigned long kprobes_ip = REG_IP(c->kregs);";
9355 s.op->newline() << "SET_REG_IP(regs, (unsigned long) inst->addr);";
9356 s.op->newline() << "(*sdp->probe->ph) (c);";
9357 s.op->newline() << "SET_REG_IP(regs, kprobes_ip);";
9358 s.op->newline(-1) << "}";
9359
9360 common_probe_entryfn_epilogue (s, true, otf_safe_context(s));
9361 s.op->newline() << "return 0;";
9362 s.op->newline(-1) << "}";
9363
9364 // Same for kretprobes
9365 s.op->newline();
9366 s.op->newline() << "static int enter_kretprobe2_probe (struct kretprobe_instance *inst,";
9367 s.op->line() << " struct pt_regs *regs) {";
9368 s.op->newline(1) << "struct kretprobe *krp = inst->rp;";
9369
9370 // NB: as of PR5673, the kprobe|kretprobe union struct is in BSS
9371 s.op->newline() << "int kprobe_idx = ((uintptr_t)krp-(uintptr_t)stap_dwarfless_kprobes)/sizeof(struct stap_dwarfless_kprobe);";
9372 // Check that the index is plausible
9373 s.op->newline() << "struct stap_dwarfless_probe *sdp = &stap_dwarfless_probes[";
9374 s.op->line() << "((kprobe_idx >= 0 && kprobe_idx < " << probes_by_module.size() << ")?";
9375 s.op->line() << "kprobe_idx:0)"; // NB: at least we avoid memory corruption
9376 // XXX: it would be nice to give a more verbose error though; BUG_ON later?
9377 s.op->line() << "];";
9378
9379 common_probe_entryfn_prologue (s, "STAP_SESSION_RUNNING", "sdp->probe",
9380 "stp_probe_type_kretprobe");
9381 s.op->newline() << "c->kregs = regs;";
9382 s.op->newline() << "c->ips.krp.pi = inst;"; // for assisting runtime's backtrace logic
9383
9384 // Make it look like the IP is set as it wouldn't have been replaced
9385 // by a breakpoint instruction when calling real probe handler. Reset
9386 // IP regs on return, so we don't confuse kprobes. PR10458
9387 s.op->newline() << "{";
9388 s.op->indent(1);
9389 s.op->newline() << "unsigned long kprobes_ip = REG_IP(c->kregs);";
9390 s.op->newline() << "SET_REG_IP(regs, (unsigned long) inst->rp->kp.addr);";
9391 s.op->newline() << "(*sdp->probe->ph) (c);";
9392 s.op->newline() << "SET_REG_IP(regs, kprobes_ip);";
9393 s.op->newline(-1) << "}";
9394
9395 common_probe_entryfn_epilogue (s, true, otf_safe_context(s));
9396 s.op->newline() << "return 0;";
9397 s.op->newline(-1) << "}";
9398
9399 s.op->newline() << "#ifdef STAPCONF_KALLSYMS_ON_EACH_SYMBOL";
9400 s.op->newline() << "static int kprobe_resolve(void *data, const char *name,";
9401 s.op->newline() << " struct module *owner,";
9402 s.op->newline() << " unsigned long val) {";
9403 s.op->newline(1) << "int i;";
9404 s.op->newline() << "int *p = (int *) data;";
9405 s.op->newline() << "for (i=0; i<" << probes_by_module.size()
9406 << " && *p > 0; i++) {";
9407 s.op->newline(1) << "struct stap_dwarfless_probe *sdp = & stap_dwarfless_probes[i];";
9408 s.op->newline() << "if (! sdp->address) {";
9409 s.op->indent(1);
9410 s.op->newline() << "const char *colon;";
9411 s.op->newline() << "if (owner && (colon = strchr(sdp->symbol_string, ':'))) {";
9412 s.op->indent(1);
9413 s.op->newline() << "if ((strlen(owner->name) == (colon - sdp->symbol_string))";
9414 s.op->newline() << " && (strncmp(sdp->symbol_string, owner->name, colon - sdp->symbol_string) == 0)";
9415 s.op->newline() << " && (strcmp(colon + 1, name) == 0)) {";
9416 s.op->newline(1) << "sdp->address = val;";
9417 s.op->newline() << "(*p)--;";
9418 s.op->newline(-1) << "}";
9419 s.op->newline(-1) << "}";
9420 s.op->newline() << "else {";
9421 s.op->newline(1) << "if (strcmp(sdp->symbol_string, name) == 0) {";
9422 s.op->newline(1) << "sdp->address = val;";
9423 s.op->newline() << "(*p)--;";
9424 s.op->newline(-1) << "}";
9425 s.op->newline(-1) << "}";
9426 s.op->newline(-1) << "}";
9427 s.op->newline(-1) << "}";
9428 s.op->newline() << "return (p > 0) ? 0 : -1;";
9429 s.op->newline(-1) << "}";
9430 s.op->newline() << "#endif";
9431 }
9432
9433
9434 void
9435 kprobe_derived_probe_group::emit_module_init (systemtap_session& s)
9436 {
9437 s.op->newline() << "#ifdef STAPCONF_KALLSYMS_ON_EACH_SYMBOL";
9438 s.op->newline() << "{";
9439 s.op->newline(1) << "int p = 0;";
9440 s.op->newline() << "for (i = 0; i < " << probes_by_module.size() << "; i++) {";
9441 s.op->newline(1) << "struct stap_dwarfless_probe *sdp = & stap_dwarfless_probes[i];";
9442 s.op->newline() << "if (! sdp->address)";
9443 s.op->newline(1) << "p++;";
9444 s.op->newline(-2) << "}";
9445 s.op->newline() << "kallsyms_on_each_symbol(kprobe_resolve, &p);";
9446 s.op->newline(-1) << "}";
9447 s.op->newline() << "#endif";
9448
9449 s.op->newline() << "for (i=0; i<" << probes_by_module.size() << "; i++) {";
9450 s.op->newline(1) << "struct stap_dwarfless_probe *sdp = & stap_dwarfless_probes[i];";
9451 s.op->newline() << "struct stap_dwarfless_kprobe *kp = & stap_dwarfless_kprobes[i];";
9452 s.op->newline() << "void *addr = (void *) sdp->address;";
9453 s.op->newline() << "const char *symbol_name = addr ? NULL : sdp->symbol_string;";
9454
9455 s.op->newline() << "#ifdef STAPCONF_KALLSYMS_ON_EACH_SYMBOL";
9456 s.op->newline() << "if (! addr) {";
9457 s.op->newline(1) << "sdp->registered_p = 0;";
9458 s.op->newline() << "if (!sdp->optional_p)";
9459 s.op->newline(1) << "_stp_warn (\"probe %s registration error (symbol not found)\", probe_point);";
9460 s.op->newline(-1) << "continue;";
9461 s.op->newline(-1) << "}";
9462 s.op->newline() << "#endif";
9463
9464 s.op->newline() << "probe_point = sdp->probe->pp;"; // for error messages
9465 s.op->newline() << "if (sdp->return_p) {";
9466 s.op->newline(1) << "kp->u.krp.kp.addr = addr;";
9467 s.op->newline() << "#ifdef STAPCONF_KPROBE_SYMBOL_NAME";
9468 s.op->newline() << "kp->u.krp.kp.symbol_name = (char *) symbol_name;";
9469 s.op->newline() << "#endif";
9470 s.op->newline() << "if (sdp->maxactive_p) {";
9471 s.op->newline(1) << "kp->u.krp.maxactive = sdp->maxactive_val;";
9472 s.op->newline(-1) << "} else {";
9473 s.op->newline(1) << "kp->u.krp.maxactive = KRETACTIVE;";
9474 s.op->newline(-1) << "}";
9475 s.op->newline() << "kp->u.krp.handler = &enter_kretprobe2_probe;";
9476 // to ensure safeness of bspcache, always use aggr_kprobe on ia64
9477 s.op->newline() << "#ifdef __ia64__";
9478 s.op->newline() << "kp->dummy.addr = kp->u.krp.kp.addr;";
9479 s.op->newline() << "#ifdef STAPCONF_KPROBE_SYMBOL_NAME";
9480 s.op->newline() << "kp->dummy.symbol_name = kp->u.krp.kp.symbol_name;";
9481 s.op->newline() << "#endif";
9482 s.op->newline() << "kp->dummy.pre_handler = NULL;";
9483 s.op->newline() << "rc = register_kprobe (& kp->dummy);";
9484 s.op->newline() << "if (rc == 0) {";
9485 s.op->newline(1) << "rc = register_kretprobe (& kp->u.krp);";
9486 s.op->newline() << "if (rc != 0)";
9487 s.op->newline(1) << "unregister_kprobe (& kp->dummy);";
9488 s.op->newline(-2) << "}";
9489 s.op->newline() << "#else";
9490 s.op->newline() << "rc = register_kretprobe (& kp->u.krp);";
9491 s.op->newline() << "#endif";
9492 s.op->newline(-1) << "} else {";
9493 // to ensure safeness of bspcache, always use aggr_kprobe on ia64
9494 s.op->newline(1) << "kp->u.kp.addr = addr;";
9495 s.op->newline() << "#ifdef STAPCONF_KPROBE_SYMBOL_NAME";
9496 s.op->newline() << "kp->u.kp.symbol_name = (char *) symbol_name;";
9497 s.op->newline() << "#endif";
9498 s.op->newline() << "kp->u.kp.pre_handler = &enter_kprobe2_probe;";
9499 s.op->newline() << "#ifdef __ia64__";
9500 s.op->newline() << "kp->dummy.pre_handler = NULL;";
9501 s.op->newline() << "kp->dummy.addr = kp->u.kp.addr;";
9502 s.op->newline() << "#ifdef STAPCONF_KPROBE_SYMBOL_NAME";
9503 s.op->newline() << "kp->dummy.symbol_name = kp->u.kp.symbol_name;";
9504 s.op->newline() << "#endif";
9505 s.op->newline() << "rc = register_kprobe (& kp->dummy);";
9506 s.op->newline() << "if (rc == 0) {";
9507 s.op->newline(1) << "rc = register_kprobe (& kp->u.kp);";
9508 s.op->newline() << "if (rc != 0)";
9509 s.op->newline(1) << "unregister_kprobe (& kp->dummy);";
9510 s.op->newline(-2) << "}";
9511 s.op->newline() << "#else";
9512 s.op->newline() << "rc = register_kprobe (& kp->u.kp);";
9513 s.op->newline() << "#endif";
9514 s.op->newline(-1) << "}";
9515 s.op->newline() << "if (rc) {"; // PR6749: tolerate a failed register_*probe.
9516 s.op->newline(1) << "sdp->registered_p = 0;";
9517 s.op->newline() << "if (!sdp->optional_p)";
9518 s.op->newline(1) << "_stp_warn (\"probe %s (address 0x%lx) registration error (rc %d)\", probe_point, (unsigned long) addr, rc);";
9519 s.op->newline(-1) << "rc = 0;"; // continue with other probes
9520 // XXX: shall we increment numskipped?
9521 s.op->newline(-1) << "}";
9522
9523 s.op->newline() << "else sdp->registered_p = 1;";
9524 s.op->newline(-1) << "}"; // for loop
9525 }
9526
9527
9528 void
9529 kprobe_derived_probe_group::emit_module_exit (systemtap_session& s)
9530 {
9531 //Unregister kprobes by batch interfaces.
9532 s.op->newline() << "#if defined(STAPCONF_UNREGISTER_KPROBES)";
9533 s.op->newline() << "j = 0;";
9534 s.op->newline() << "for (i=0; i<" << probes_by_module.size() << "; i++) {";
9535 s.op->newline(1) << "struct stap_dwarfless_probe *sdp = & stap_dwarfless_probes[i];";
9536 s.op->newline() << "struct stap_dwarfless_kprobe *kp = & stap_dwarfless_kprobes[i];";
9537 s.op->newline() << "if (! sdp->registered_p) continue;";
9538 s.op->newline() << "if (!sdp->return_p)";
9539 s.op->newline(1) << "stap_unreg_kprobes2[j++] = &kp->u.kp;";
9540 s.op->newline(-2) << "}";
9541 s.op->newline() << "unregister_kprobes((struct kprobe **)stap_unreg_kprobes2, j);";
9542 s.op->newline() << "j = 0;";
9543 s.op->newline() << "for (i=0; i<" << probes_by_module.size() << "; i++) {";
9544 s.op->newline(1) << "struct stap_dwarfless_probe *sdp = & stap_dwarfless_probes[i];";
9545 s.op->newline() << "struct stap_dwarfless_kprobe *kp = & stap_dwarfless_kprobes[i];";
9546 s.op->newline() << "if (! sdp->registered_p) continue;";
9547 s.op->newline() << "if (sdp->return_p)";
9548 s.op->newline(1) << "stap_unreg_kprobes2[j++] = &kp->u.krp;";
9549 s.op->newline(-2) << "}";
9550 s.op->newline() << "unregister_kretprobes((struct kretprobe **)stap_unreg_kprobes2, j);";
9551 s.op->newline() << "#ifdef __ia64__";
9552 s.op->newline() << "j = 0;";
9553 s.op->newline() << "for (i=0; i<" << probes_by_module.size() << "; i++) {";
9554 s.op->newline(1) << "struct stap_dwarfless_probe *sdp = & stap_dwarfless_probes[i];";
9555 s.op->newline() << "struct stap_dwarfless_kprobe *kp = & stap_dwarfless_kprobes[i];";
9556 s.op->newline() << "if (! sdp->registered_p) continue;";
9557 s.op->newline() << "stap_unreg_kprobes2[j++] = &kp->dummy;";
9558 s.op->newline(-1) << "}";
9559 s.op->newline() << "unregister_kprobes((struct kprobe **)stap_unreg_kprobes2, j);";
9560 s.op->newline() << "#endif";
9561 s.op->newline() << "#endif";
9562
9563 s.op->newline() << "for (i=0; i<" << probes_by_module.size() << "; i++) {";
9564 s.op->newline(1) << "struct stap_dwarfless_probe *sdp = & stap_dwarfless_probes[i];";
9565 s.op->newline() << "struct stap_dwarfless_kprobe *kp = & stap_dwarfless_kprobes[i];";
9566 s.op->newline() << "if (! sdp->registered_p) continue;";
9567 s.op->newline() << "if (sdp->return_p) {";
9568 s.op->newline() << "#if !defined(STAPCONF_UNREGISTER_KPROBES)";
9569 s.op->newline(1) << "unregister_kretprobe (&kp->u.krp);";
9570 s.op->newline() << "#endif";
9571 s.op->newline() << "atomic_add (kp->u.krp.nmissed, skipped_count());";
9572 s.op->newline() << "#ifdef STP_TIMING";
9573 s.op->newline() << "if (kp->u.krp.nmissed)";
9574 s.op->newline(1) << "_stp_warn (\"Skipped due to missed kretprobe/1 on '%s': %d\\n\", sdp->probe->pp, kp->u.krp.nmissed);";
9575 s.op->newline(-1) << "#endif";
9576 s.op->newline() << "atomic_add (kp->u.krp.kp.nmissed, skipped_count());";
9577 s.op->newline() << "#ifdef STP_TIMING";
9578 s.op->newline() << "if (kp->u.krp.kp.nmissed)";
9579 s.op->newline(1) << "_stp_warn (\"Skipped due to missed kretprobe/2 on '%s': %lu\\n\", sdp->probe->pp, kp->u.krp.kp.nmissed);";
9580 s.op->newline(-1) << "#endif";
9581 s.op->newline(-1) << "} else {";
9582 s.op->newline() << "#if !defined(STAPCONF_UNREGISTER_KPROBES)";
9583 s.op->newline(1) << "unregister_kprobe (&kp->u.kp);";
9584 s.op->newline() << "#endif";
9585 s.op->newline() << "atomic_add (kp->u.kp.nmissed, skipped_count());";
9586 s.op->newline() << "#ifdef STP_TIMING";
9587 s.op->newline() << "if (kp->u.kp.nmissed)";
9588 s.op->newline(1) << "_stp_warn (\"Skipped due to missed kprobe on '%s': %lu\\n\", sdp->probe->pp, kp->u.kp.nmissed);";
9589 s.op->newline(-1) << "#endif";
9590 s.op->newline(-1) << "}";
9591 s.op->newline() << "#if !defined(STAPCONF_UNREGISTER_KPROBES) && defined(__ia64__)";
9592 s.op->newline() << "unregister_kprobe (&kp->dummy);";
9593 s.op->newline() << "#endif";
9594 s.op->newline() << "sdp->registered_p = 0;";
9595 s.op->newline(-1) << "}";
9596 }
9597
9598 struct kprobe_builder: public derived_probe_builder
9599 {
9600 public:
9601 kprobe_builder() {}
9602
9603 void build_no_more (systemtap_session &s) {}
9604
9605 virtual void build(systemtap_session & sess,
9606 probe * base,
9607 probe_point * location,
9608 literal_map_t const & parameters,
9609 vector<derived_probe *> & finished_results);
9610 };
9611
9612
9613 void
9614 kprobe_builder::build(systemtap_session & sess,
9615 probe * base,
9616 probe_point * location,
9617 literal_map_t const & parameters,
9618 vector<derived_probe *> & finished_results)
9619 {
9620 string function_string_val, module_string_val;
9621 string path, library, path_tgt, library_tgt;
9622 int64_t statement_num_val = 0, maxactive_val = 0;
9623 bool has_function_str, has_module_str, has_statement_num;
9624 bool has_absolute, has_call, has_return, has_maxactive;
9625 bool has_path, has_library;
9626
9627 has_function_str = get_param(parameters, TOK_FUNCTION, function_string_val);
9628 has_module_str = get_param(parameters, TOK_MODULE, module_string_val);
9629 has_call = has_null_param (parameters, TOK_CALL);
9630 has_return = has_null_param (parameters, TOK_RETURN);
9631 has_maxactive = get_param(parameters, TOK_MAXACTIVE, maxactive_val);
9632 has_statement_num = get_param(parameters, TOK_STATEMENT, statement_num_val);
9633 has_absolute = has_null_param (parameters, TOK_ABSOLUTE);
9634 has_path = get_param (parameters, TOK_PROCESS, path);
9635 has_library = get_param (parameters, TOK_LIBRARY, library);
9636
9637 if (has_path)
9638 {
9639 path = find_executable (path, sess.sysroot, sess.sysenv);
9640 path_tgt = path_remove_sysroot(sess, path);
9641 }
9642 if (has_library)
9643 {
9644 library = find_executable (library, sess.sysroot, sess.sysenv,
9645 "LD_LIBRARY_PATH");
9646 library_tgt = path_remove_sysroot(sess, library);
9647 }
9648
9649 if (has_function_str)
9650 {
9651 if (has_module_str)
9652 {
9653 function_string_val = module_string_val + ":" + function_string_val;
9654 derived_probe *dp
9655 = new kprobe_derived_probe (sess, finished_results, base,
9656 location, function_string_val,
9657 0, has_call, has_return,
9658 has_statement_num, has_maxactive,
9659 has_path, has_library, maxactive_val,
9660 path_tgt, library_tgt);
9661 finished_results.push_back (dp);
9662 }
9663 else
9664 {
9665 vector<string> matches;
9666
9667 // Simple names can be found directly
9668 if (function_string_val.find_first_of("*?[") == string::npos)
9669 {
9670 if (sess.kernel_functions.count(function_string_val))
9671 matches.push_back(function_string_val);
9672 }
9673 else // Search function name list for matching names
9674 {
9675 for (set<string>::const_iterator it = sess.kernel_functions.begin();
9676 it != sess.kernel_functions.end(); it++)
9677 // fnmatch returns zero for matching.
9678 if (fnmatch(function_string_val.c_str(), it->c_str(), 0) == 0)
9679 matches.push_back(*it);
9680 }
9681
9682 for (vector<string>::const_iterator it = matches.begin();
9683 it != matches.end(); it++)
9684 {
9685 derived_probe *dp
9686 = new kprobe_derived_probe (sess, finished_results, base,
9687 location, *it, 0, has_call,
9688 has_return, has_statement_num,
9689 has_maxactive, has_path,
9690 has_library, maxactive_val,
9691 path_tgt, library_tgt);
9692 finished_results.push_back (dp);
9693 }
9694 }
9695 }
9696 else
9697 {
9698 // assert guru mode for absolute probes
9699 if ( has_statement_num && has_absolute && !base->privileged )
9700 throw SEMANTIC_ERROR (_("absolute statement probe in unprivileged script; need stap -g"), base->tok);
9701
9702 finished_results.push_back (new kprobe_derived_probe (sess,
9703 finished_results,
9704 base,
9705 location, "",
9706 statement_num_val,
9707 has_call,
9708 has_return,
9709 has_statement_num,
9710 has_maxactive,
9711 has_path,
9712 has_library,
9713 maxactive_val,
9714 path_tgt,
9715 library_tgt));
9716 }
9717 }
9718
9719
9720 void
9721 kprobe_var_expanding_visitor::visit_entry_op (entry_op *e)
9722 {
9723 expression *repl = e;
9724
9725 if (has_return)
9726 {
9727 // expand the operand as if it weren't a return probe
9728 has_return = false;
9729 replace (e->operand);
9730 has_return = true;
9731
9732 // XXX it would be nice to use gen_kretprobe_saved_return when
9733 // available, but it requires knowing the types already, which is
9734 // problematic for arbitrary expressons.
9735 repl = gen_mapped_saved_return (sess, e->operand, "entry",
9736 add_block, add_block_tid,
9737 add_call_probe, add_call_probe_tid);
9738 }
9739 provide (repl);
9740 }
9741
9742
9743 // ------------------------------------------------------------------------
9744 // Hardware breakpoint based probes.
9745 // ------------------------------------------------------------------------
9746
9747 static const string TOK_HWBKPT("data");
9748 static const string TOK_HWBKPT_WRITE("write");
9749 static const string TOK_HWBKPT_RW("rw");
9750 static const string TOK_LENGTH("length");
9751
9752 #define HWBKPT_READ 0
9753 #define HWBKPT_WRITE 1
9754 #define HWBKPT_RW 2
9755 struct hwbkpt_derived_probe: public derived_probe
9756 {
9757 hwbkpt_derived_probe (probe *base,
9758 probe_point *location,
9759 uint64_t addr,
9760 string symname,
9761 unsigned int len,
9762 bool has_only_read_access,
9763 bool has_only_write_access,
9764 bool has_rw_access
9765 );
9766 Dwarf_Addr hwbkpt_addr;
9767 string symbol_name;
9768 unsigned int hwbkpt_access,hwbkpt_len;
9769
9770 void printsig (std::ostream &o) const;
9771 void join_group (systemtap_session& s);
9772 };
9773
9774 struct hwbkpt_derived_probe_group: public derived_probe_group
9775 {
9776 private:
9777 vector<hwbkpt_derived_probe*> hwbkpt_probes;
9778
9779 public:
9780 void enroll (hwbkpt_derived_probe* probe, systemtap_session& s);
9781 void emit_module_decls (systemtap_session& s);
9782 void emit_module_init (systemtap_session& s);
9783 void emit_module_exit (systemtap_session& s);
9784 };
9785
9786 hwbkpt_derived_probe::hwbkpt_derived_probe (probe *base,
9787 probe_point *location,
9788 uint64_t addr,
9789 string symname,
9790 unsigned int len,
9791 bool has_only_read_access,
9792 bool has_only_write_access,
9793 bool):
9794 derived_probe (base, location, true /* .components soon rewritten */ ),
9795 hwbkpt_addr (addr),
9796 symbol_name (symname),
9797 hwbkpt_len (len)
9798 {
9799 this->tok = base->tok;
9800
9801 vector<probe_point::component*> comps;
9802 comps.push_back (new probe_point::component(TOK_KERNEL));
9803
9804 if (hwbkpt_addr)
9805 comps.push_back (new probe_point::component (TOK_HWBKPT,
9806 new literal_number(hwbkpt_addr, true)));
9807 else if (symbol_name.size())
9808 comps.push_back (new probe_point::component (TOK_HWBKPT, new literal_string(symbol_name)));
9809
9810 comps.push_back (new probe_point::component (TOK_LENGTH, new literal_number(hwbkpt_len)));
9811
9812 if (has_only_read_access)
9813 this->hwbkpt_access = HWBKPT_READ ;
9814 //TODO add code for comps.push_back for read, since this flag is not for x86
9815
9816 else
9817 {
9818 if (has_only_write_access)
9819 {
9820 this->hwbkpt_access = HWBKPT_WRITE ;
9821 comps.push_back (new probe_point::component(TOK_HWBKPT_WRITE));
9822 }
9823 else
9824 {
9825 this->hwbkpt_access = HWBKPT_RW ;
9826 comps.push_back (new probe_point::component(TOK_HWBKPT_RW));
9827 }
9828 }
9829
9830 this->sole_location()->components = comps;
9831 }
9832
9833 void hwbkpt_derived_probe::printsig (ostream& o) const
9834 {
9835 sole_location()->print (o);
9836 printsig_nested (o);
9837 }
9838
9839 void hwbkpt_derived_probe::join_group (systemtap_session& s)
9840 {
9841 if (! s.hwbkpt_derived_probes)
9842 s.hwbkpt_derived_probes = new hwbkpt_derived_probe_group ();
9843 s.hwbkpt_derived_probes->enroll (this, s);
9844 this->group = s.hwbkpt_derived_probes;
9845 }
9846
9847 void hwbkpt_derived_probe_group::enroll (hwbkpt_derived_probe* p, systemtap_session& s)
9848 {
9849 hwbkpt_probes.push_back (p);
9850
9851 unsigned max_hwbkpt_probes_by_arch = 0;
9852 if (s.architecture == "i386" || s.architecture == "x86_64")
9853 max_hwbkpt_probes_by_arch = 4;
9854 else if (s.architecture == "s390")
9855 max_hwbkpt_probes_by_arch = 1;
9856
9857 if (hwbkpt_probes.size() >= max_hwbkpt_probes_by_arch)
9858 s.print_warning (_F("Too many hardware breakpoint probes requested for %s (%zu vs. %u)",
9859 s.architecture.c_str(), hwbkpt_probes.size(), max_hwbkpt_probes_by_arch));
9860 }
9861
9862 void
9863 hwbkpt_derived_probe_group::emit_module_decls (systemtap_session& s)
9864 {
9865 if (hwbkpt_probes.empty()) return;
9866
9867 s.op->newline() << "/* ---- hwbkpt-based probes ---- */";
9868
9869 s.op->newline() << "#include <linux/perf_event.h>";
9870 s.op->newline() << "#include <linux/hw_breakpoint.h>";
9871 s.op->newline();
9872
9873 // Forward declare the master entry functions
9874 s.op->newline() << "#ifdef STAPCONF_PERF_HANDLER_NMI";
9875 s.op->newline() << "static int enter_hwbkpt_probe (struct perf_event *bp,";
9876 s.op->line() << " int nmi,";
9877 s.op->line() << " struct perf_sample_data *data,";
9878 s.op->line() << " struct pt_regs *regs);";
9879 s.op->newline() << "#else";
9880 s.op->newline() << "static int enter_hwbkpt_probe (struct perf_event *bp,";
9881 s.op->line() << " struct perf_sample_data *data,";
9882 s.op->line() << " struct pt_regs *regs);";
9883 s.op->newline() << "#endif";
9884
9885 // Emit the actual probe list.
9886
9887 s.op->newline() << "static struct perf_event_attr ";
9888 s.op->newline() << "stap_hwbkpt_probe_array[" << hwbkpt_probes.size() << "];";
9889
9890 s.op->newline() << "static struct perf_event **";
9891 s.op->newline() << "stap_hwbkpt_ret_array[" << hwbkpt_probes.size() << "];";
9892 s.op->newline() << "static struct stap_hwbkpt_probe {";
9893 s.op->newline() << "int registered_p:1;";
9894 // registered_p = 0 signifies a probe that is unregistered (or failed)
9895 // registered_p = 1 signifies a probe that got registered successfully
9896
9897 // Symbol Names are mostly small and uniform enough
9898 // to justify putting const char*.
9899 s.op->newline() << "const char * const symbol;";
9900
9901 s.op->newline() << "const unsigned long address;";
9902 s.op->newline() << "uint8_t atype;";
9903 s.op->newline() << "unsigned int len;";
9904 s.op->newline() << "const struct stap_probe * const probe;";
9905 s.op->newline() << "} stap_hwbkpt_probes[] = {";
9906 s.op->indent(1);
9907
9908 for (unsigned int it = 0; it < hwbkpt_probes.size(); it++)
9909 {
9910 hwbkpt_derived_probe* p = hwbkpt_probes.at(it);
9911 s.op->newline() << "{";
9912 if (p->symbol_name.size())
9913 s.op->line() << " .address=(unsigned long)0x0" << "ULL,";
9914 else
9915 s.op->line() << " .address=(unsigned long)0x" << hex << p->hwbkpt_addr << dec << "ULL,";
9916 switch(p->hwbkpt_access){
9917 case HWBKPT_READ:
9918 s.op->line() << " .atype=HW_BREAKPOINT_R ,";
9919 break;
9920 case HWBKPT_WRITE:
9921 s.op->line() << " .atype=HW_BREAKPOINT_W ,";
9922 break;
9923 case HWBKPT_RW:
9924 s.op->line() << " .atype=HW_BREAKPOINT_R|HW_BREAKPOINT_W ,";
9925 break;
9926 };
9927 s.op->line() << " .len=" << p->hwbkpt_len << ",";
9928 s.op->line() << " .probe=" << common_probe_init (p) << ",";
9929 s.op->line() << " .symbol=\"" << p->symbol_name << "\",";
9930 s.op->line() << " },";
9931 }
9932 s.op->newline(-1) << "};";
9933
9934 // Emit the hwbkpt callback function
9935 s.op->newline() ;
9936 s.op->newline() << "#ifdef STAPCONF_PERF_HANDLER_NMI";
9937 s.op->newline() << "static int enter_hwbkpt_probe (struct perf_event *bp,";
9938 s.op->line() << " int nmi,";
9939 s.op->line() << " struct perf_sample_data *data,";
9940 s.op->line() << " struct pt_regs *regs) {";
9941 s.op->newline() << "#else";
9942 s.op->newline() << "static int enter_hwbkpt_probe (struct perf_event *bp,";
9943 s.op->line() << " struct perf_sample_data *data,";
9944 s.op->line() << " struct pt_regs *regs) {";
9945 s.op->newline() << "#endif";
9946 s.op->newline(1) << "unsigned int i;";
9947 s.op->newline() << "if (bp->attr.type != PERF_TYPE_BREAKPOINT) return -1;";
9948 s.op->newline() << "for (i=0; i<" << hwbkpt_probes.size() << "; i++) {";
9949 s.op->newline(1) << "struct perf_event_attr *hp = & stap_hwbkpt_probe_array[i];";
9950 // XXX: why not match stap_hwbkpt_ret_array[i] against bp instead?
9951 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) {";
9952 s.op->newline(1) << "struct stap_hwbkpt_probe *sdp = &stap_hwbkpt_probes[i];";
9953 common_probe_entryfn_prologue (s, "STAP_SESSION_RUNNING", "sdp->probe",
9954 "stp_probe_type_hwbkpt");
9955 s.op->newline() << "if (user_mode(regs)) {";
9956 s.op->newline(1)<< "c->user_mode_p = 1;";
9957 s.op->newline() << "c->uregs = regs;";
9958 s.op->newline(-1) << "} else {";
9959 s.op->newline(1) << "c->kregs = regs;";
9960 s.op->newline(-1) << "}";
9961 s.op->newline() << "(*sdp->probe->ph) (c);";
9962 common_probe_entryfn_epilogue (s, true, otf_safe_context(s));
9963 s.op->newline(-1) << "}";
9964 s.op->newline(-1) << "}";
9965 s.op->newline() << "return 0;";
9966 s.op->newline(-1) << "}";
9967 }
9968
9969 void
9970 hwbkpt_derived_probe_group::emit_module_init (systemtap_session& s)
9971 {
9972 s.op->newline() << "for (i=0; i<" << hwbkpt_probes.size() << "; i++) {";
9973 s.op->newline(1) << "struct stap_hwbkpt_probe *sdp = & stap_hwbkpt_probes[i];";
9974 s.op->newline() << "struct perf_event_attr *hp = & stap_hwbkpt_probe_array[i];";
9975 s.op->newline() << "void *addr = (void *) sdp->address;";
9976 s.op->newline() << "const char *hwbkpt_symbol_name = addr ? NULL : sdp->symbol;";
9977 s.op->newline() << "hw_breakpoint_init(hp);";
9978 s.op->newline() << "if (addr)";
9979 s.op->newline(1) << "hp->bp_addr = (unsigned long) addr;";
9980 s.op->newline(-1) << "else { ";
9981 s.op->newline(1) << "hp->bp_addr = kallsyms_lookup_name(hwbkpt_symbol_name);";
9982 s.op->newline() << "if (!hp->bp_addr) { ";
9983 s.op->newline(1) << "_stp_warn(\"Probe %s registration skipped: invalid symbol %s \",sdp->probe->pp,hwbkpt_symbol_name);";
9984 s.op->newline() << "continue;";
9985 s.op->newline(-1) << "}";
9986 s.op->newline(-1) << "}";
9987 s.op->newline() << "hp->bp_type = sdp->atype;";
9988
9989 // On x86 & x86-64, hp->bp_len is not just a number but a macro/enum (!?!).
9990 if (s.architecture == "i386" || s.architecture == "x86_64" )
9991 {
9992 s.op->newline() << "switch(sdp->len) {";
9993 s.op->newline() << "case 1:";
9994 s.op->newline(1) << "hp->bp_len = HW_BREAKPOINT_LEN_1;";
9995 s.op->newline() << "break;";
9996 s.op->newline(-1) << "case 2:";
9997 s.op->newline(1) << "hp->bp_len = HW_BREAKPOINT_LEN_2;";
9998 s.op->newline() << "break;";
9999 s.op->newline(-1) << "case 3:";
10000 s.op->newline() << "case 4:";
10001 s.op->newline(1) << "hp->bp_len = HW_BREAKPOINT_LEN_4;";
10002 s.op->newline() << "break;";
10003 s.op->newline(-1) << "case 5:";
10004 s.op->newline() << "case 6:";
10005 s.op->newline() << "case 7:";
10006 s.op->newline() << "case 8:";
10007 s.op->newline() << "default:"; // XXX: could instead reject
10008 s.op->newline(1) << "hp->bp_len = HW_BREAKPOINT_LEN_8;";
10009 s.op->newline() << "break;";
10010 s.op->newline(-1) << "}";
10011 }
10012 else // other architectures presumed straightforward
10013 s.op->newline() << "hp->bp_len = sdp->len;";
10014
10015 s.op->newline() << "probe_point = sdp->probe->pp;"; // for error messages
10016 s.op->newline() << "#ifdef STAPCONF_HW_BREAKPOINT_CONTEXT";
10017 s.op->newline() << "stap_hwbkpt_ret_array[i] = register_wide_hw_breakpoint(hp, (void *)&enter_hwbkpt_probe, NULL);";
10018 s.op->newline() << "#else";
10019 s.op->newline() << "stap_hwbkpt_ret_array[i] = register_wide_hw_breakpoint(hp, (void *)&enter_hwbkpt_probe);";
10020 s.op->newline() << "#endif";
10021 s.op->newline() << "rc = 0;";
10022 s.op->newline() << "if (IS_ERR(stap_hwbkpt_ret_array[i])) {";
10023 s.op->newline(1) << "rc = PTR_ERR(stap_hwbkpt_ret_array[i]);";
10024 s.op->newline() << "stap_hwbkpt_ret_array[i] = 0;";
10025 s.op->newline(-1) << "}";
10026 s.op->newline() << "if (rc) {";
10027 s.op->newline(1) << "_stp_warn(\"Hwbkpt probe %s: registration error %d, addr %p, name %s\", probe_point, rc, addr, hwbkpt_symbol_name);";
10028 s.op->newline() << "sdp->registered_p = 0;";
10029 s.op->newline(-1) << "}";
10030 s.op->newline() << " else sdp->registered_p = 1;";
10031 s.op->newline(-1) << "}"; // for loop
10032 }
10033
10034 void
10035 hwbkpt_derived_probe_group::emit_module_exit (systemtap_session& s)
10036 {
10037 //Unregister hwbkpt probes.
10038 s.op->newline() << "for (i=0; i<" << hwbkpt_probes.size() << "; i++) {";
10039 s.op->newline(1) << "struct stap_hwbkpt_probe *sdp = & stap_hwbkpt_probes[i];";
10040 s.op->newline() << "if (sdp->registered_p == 0) continue;";
10041 s.op->newline() << "unregister_wide_hw_breakpoint(stap_hwbkpt_ret_array[i]);";
10042 s.op->newline() << "sdp->registered_p = 0;";
10043 s.op->newline(-1) << "}";
10044 }
10045
10046 struct hwbkpt_builder: public derived_probe_builder
10047 {
10048 hwbkpt_builder() {}
10049 virtual void build(systemtap_session & sess,
10050 probe * base,
10051 probe_point * location,
10052 literal_map_t const & parameters,
10053 vector<derived_probe *> & finished_results);
10054 };
10055
10056 void
10057 hwbkpt_builder::build(systemtap_session & sess,
10058 probe * base,
10059 probe_point * location,
10060 literal_map_t const & parameters,
10061 vector<derived_probe *> & finished_results)
10062 {
10063 string symbol_str_val;
10064 int64_t hwbkpt_address, len;
10065 bool has_addr, has_symbol_str, has_write, has_rw, has_len;
10066
10067 if (! (sess.kernel_config["CONFIG_PERF_EVENTS"] == string("y")))
10068 throw SEMANTIC_ERROR (_("CONFIG_PERF_EVENTS not available on this kernel"),
10069 location->components[0]->tok);
10070 if (! (sess.kernel_config["CONFIG_HAVE_HW_BREAKPOINT"] == string("y")))
10071 throw SEMANTIC_ERROR (_("CONFIG_HAVE_HW_BREAKPOINT not available on this kernel"),
10072 location->components[0]->tok);
10073
10074 has_addr = get_param (parameters, TOK_HWBKPT, hwbkpt_address);
10075 has_symbol_str = get_param (parameters, TOK_HWBKPT, symbol_str_val);
10076 has_len = get_param (parameters, TOK_LENGTH, len);
10077 has_write = (parameters.find(TOK_HWBKPT_WRITE) != parameters.end());
10078 has_rw = (parameters.find(TOK_HWBKPT_RW) != parameters.end());
10079
10080 // Make an intermediate pp that is well-formed. It's pretty much the same as
10081 // the user-provided one, except that the addr literal is well-typed.
10082 probe_point* well_formed_loc = new probe_point(*location);
10083 well_formed_loc->well_formed = true;
10084
10085 vector<probe_point::component*> well_formed_comps;
10086 vector<probe_point::component*>::iterator it;
10087 for (it = location->components.begin();
10088 it != location->components.end(); ++it)
10089 if ((*it)->functor == TOK_HWBKPT && has_addr)
10090 well_formed_comps.push_back(new probe_point::component(TOK_HWBKPT,
10091 new literal_number(hwbkpt_address, true /* hex */ )));
10092 else
10093 well_formed_comps.push_back(*it);
10094 well_formed_loc->components = well_formed_comps;
10095 probe *new_base = new probe (base, well_formed_loc);
10096
10097 if (!has_len)
10098 len = 1;
10099
10100 if (has_addr)
10101 finished_results.push_back (new hwbkpt_derived_probe (new_base,
10102 location,
10103 hwbkpt_address,
10104 "",len,0,
10105 has_write,
10106 has_rw));
10107 else if (has_symbol_str)
10108 finished_results.push_back (new hwbkpt_derived_probe (new_base,
10109 location,
10110 0,
10111 symbol_str_val,len,0,
10112 has_write,
10113 has_rw));
10114 else
10115 assert (0);
10116 }
10117
10118 // ------------------------------------------------------------------------
10119 // statically inserted kernel-tracepoint derived probes
10120 // ------------------------------------------------------------------------
10121
10122 struct tracepoint_arg
10123 {
10124 string name, c_type, typecast;
10125 bool usable, used, isptr;
10126 Dwarf_Die type_die;
10127 tracepoint_arg(): usable(false), used(false), isptr(false) {}
10128 };
10129
10130 struct tracepoint_derived_probe: public derived_probe
10131 {
10132 tracepoint_derived_probe (systemtap_session& s,
10133 dwflpp& dw, Dwarf_Die& func_die,
10134 const string& tracepoint_name,
10135 probe* base_probe, probe_point* location);
10136
10137 systemtap_session& sess;
10138 string tracepoint_name, header;
10139 vector <struct tracepoint_arg> args;
10140
10141 void build_args(dwflpp& dw, Dwarf_Die& func_die);
10142 void getargs (std::list<std::string> &arg_set) const;
10143 void join_group (systemtap_session& s);
10144 void print_dupe_stamp(ostream& o);
10145 };
10146
10147
10148 struct tracepoint_derived_probe_group: public generic_dpg<tracepoint_derived_probe>
10149 {
10150 void emit_module_decls (systemtap_session& s);
10151 void emit_module_init (systemtap_session& s);
10152 void emit_module_exit (systemtap_session& s);
10153 };
10154
10155
10156 struct tracepoint_var_expanding_visitor: public var_expanding_visitor
10157 {
10158 tracepoint_var_expanding_visitor(dwflpp& dw, const string& probe_name,
10159 vector <struct tracepoint_arg>& args):
10160 dw (dw), probe_name (probe_name), args (args) {}
10161 dwflpp& dw;
10162 const string& probe_name;
10163 vector <struct tracepoint_arg>& args;
10164
10165 void visit_target_symbol (target_symbol* e);
10166 void visit_target_symbol_arg (target_symbol* e);
10167 void visit_target_symbol_context (target_symbol* e);
10168 };
10169
10170
10171 void
10172 tracepoint_var_expanding_visitor::visit_target_symbol_arg (target_symbol* e)
10173 {
10174 string argname = e->sym_name();
10175
10176 // search for a tracepoint parameter matching this name
10177 tracepoint_arg *arg = NULL;
10178 for (unsigned i = 0; i < args.size(); ++i)
10179 if (args[i].usable && args[i].name == argname)
10180 {
10181 arg = &args[i];
10182 arg->used = true;
10183 break;
10184 }
10185
10186 if (arg == NULL)
10187 {
10188 set<string> vars;
10189 for (unsigned i = 0; i < args.size(); ++i)
10190 vars.insert("$" + args[i].name);
10191 vars.insert("$$name");
10192 vars.insert("$$parms");
10193 vars.insert("$$vars");
10194 string sugs = levenshtein_suggest(e->name, vars); // no need to limit, there's not that many
10195
10196 // We hope that this value ends up not being referenced after all, so it
10197 // can be optimized out quietly.
10198 throw SEMANTIC_ERROR(_F("unable to find tracepoint variable '%s'%s",
10199 e->name.c_str(), sugs.empty() ? "" :
10200 (_(" (alternatives: ") + sugs + ")").c_str()), e->tok);
10201 // NB: we use 'alternatives' because we list all
10202 // NB: we can have multiple errors, since a target variable
10203 // may be expanded in several different contexts:
10204 // trace ("*") { $foo->bar }
10205 }
10206
10207 // make sure we're not dereferencing base types or void
10208 bool deref_p = arg->isptr && !null_die(&arg->type_die);
10209 if (!deref_p)
10210 e->assert_no_components("tracepoint", true);
10211
10212 // we can only write to dereferenced fields, and only if guru mode is on
10213 bool lvalue = is_active_lvalue(e);
10214 if (lvalue && (!dw.sess.guru_mode || e->components.empty()))
10215 throw SEMANTIC_ERROR(_F("write to tracepoint variable '%s' not permitted; need stap -g", e->name.c_str()), e->tok);
10216
10217 // XXX: if a struct/union arg is passed by value, then writing to its fields
10218 // is also meaningless until you dereference past a pointer member. It's
10219 // harder to detect and prevent that though...
10220
10221 if (e->components.empty())
10222 {
10223 if (e->addressof)
10224 throw SEMANTIC_ERROR(_("cannot take address of tracepoint variable"), e->tok);
10225
10226 // Just grab the value from the probe locals
10227 symbol* sym = new symbol;
10228 sym->tok = e->tok;
10229 sym->name = "__tracepoint_arg_" + arg->name;
10230 sym->type_details.reset(new exp_type_dwarf(&dw, &arg->type_die, false, false));
10231 provide (sym);
10232 }
10233 else
10234 {
10235 // make a copy of the original as a bare target symbol for the tracepoint
10236 // value, which will be passed into the dwarf dereferencing code
10237 target_symbol* e2 = deep_copy_visitor::deep_copy(e);
10238 e2->components.clear();
10239
10240 if (e->check_pretty_print (lvalue))
10241 {
10242 dwarf_pretty_print dpp(dw, &arg->type_die, e2, deref_p, false, *e);
10243 dpp.expand()->visit (this);
10244 return;
10245 }
10246
10247 bool userspace_p = false;
10248 string fname = (string(lvalue ? "_tracepoint_tvar_set" : "_tracepoint_tvar_get")
10249 + "_" + e->sym_name()
10250 + "_" + lex_cast(tick++));
10251
10252 Dwarf_Die endtype;
10253 string code = dw.literal_stmt_for_pointer (&arg->type_die, e, lvalue, &endtype);
10254
10255 functioncall* n = synthetic_embedded_deref_call(dw, &endtype, fname, code,
10256 userspace_p, lvalue, e, e2);
10257
10258 if (lvalue)
10259 provide_lvalue_call (n);
10260
10261 // Revisit the functioncall so arguments can be expanded.
10262 n->visit (this);
10263 }
10264 }
10265
10266
10267 void
10268 tracepoint_var_expanding_visitor::visit_target_symbol_context (target_symbol* e)
10269 {
10270 if (e->addressof)
10271 throw SEMANTIC_ERROR(_("cannot take address of context variable"), e->tok);
10272
10273 if (is_active_lvalue (e))
10274 throw SEMANTIC_ERROR(_F("write to tracepoint '%s' not permitted", e->name.c_str()), e->tok);
10275
10276 if (e->name == "$$name")
10277 {
10278 e->assert_no_components("tracepoint");
10279
10280 // Synthesize an embedded expression.
10281 embedded_expr *expr = new embedded_expr;
10282 expr->tok = e->tok;
10283 expr->code = string("/* string */ /* pure */ ")
10284 + string("c->ips.tracepoint_name ? c->ips.tracepoint_name : \"\"");
10285 provide (expr);
10286 }
10287 else if (e->name == "$$vars" || e->name == "$$parms")
10288 {
10289 e->assert_no_components("tracepoint", true);
10290
10291 print_format* pf = print_format::create(e->tok, "sprintf");
10292
10293 for (unsigned i = 0; i < args.size(); ++i)
10294 {
10295 if (!args[i].usable)
10296 continue;
10297 if (i > 0)
10298 pf->raw_components += " ";
10299 pf->raw_components += args[i].name;
10300 target_symbol *tsym = new target_symbol;
10301 tsym->tok = e->tok;
10302 tsym->name = "$" + args[i].name;
10303 tsym->components = e->components;
10304
10305 // every variable should always be accessible!
10306 tsym->saved_conversion_error = 0;
10307 expression *texp = require<expression> (tsym); // NB: throws nothing ...
10308 if (tsym->saved_conversion_error) // ... but this is how we know it happened.
10309 {
10310 if (dw.sess.verbose>2)
10311 for (const semantic_error *c = tsym->saved_conversion_error;
10312 c != 0; c = c->get_chain())
10313 clog << _("variable location problem [man error::dwarf]: ") << c->what() << endl;
10314 pf->raw_components += "=?";
10315 continue;
10316 }
10317
10318 if (e->check_pretty_print ())
10319 pf->raw_components += "=%s";
10320 else
10321 pf->raw_components += args[i].isptr ? "=%p" : "=%#x";
10322 pf->args.push_back(texp);
10323 }
10324
10325 pf->components = print_format::string_to_components(pf->raw_components);
10326 provide (pf);
10327 }
10328 else
10329 assert(0); // shouldn't get here
10330 }
10331
10332 void
10333 tracepoint_var_expanding_visitor::visit_target_symbol (target_symbol* e)
10334 {
10335 try
10336 {
10337 assert(e->name.size() > 0 && e->name[0] == '$');
10338
10339 if (e->name == "$$name" || e->name == "$$parms" || e->name == "$$vars")
10340 visit_target_symbol_context (e);
10341
10342 else
10343 visit_target_symbol_arg (e);
10344 }
10345 catch (const semantic_error &er)
10346 {
10347 e->chain (er);
10348 provide (e);
10349 }
10350 }
10351
10352
10353 tracepoint_derived_probe::tracepoint_derived_probe (systemtap_session& s,
10354 dwflpp& dw, Dwarf_Die& func_die,
10355 const string& tracepoint_name,
10356 probe* base, probe_point* loc):
10357 derived_probe (base, loc, true /* .components soon rewritten */),
10358 sess (s), tracepoint_name (tracepoint_name)
10359 {
10360 // create synthetic probe point name; preserve condition
10361 vector<probe_point::component*> comps;
10362 comps.push_back (new probe_point::component (TOK_KERNEL));
10363 comps.push_back (new probe_point::component (TOK_TRACE, new literal_string (tracepoint_name)));
10364 this->sole_location()->components = comps;
10365
10366 // fill out the available arguments in this tracepoint
10367 build_args(dw, func_die);
10368
10369 // determine which header defined this tracepoint
10370 string decl_file = dwarf_decl_file(&func_die);
10371 header = decl_file;
10372
10373 #if 0 /* This convention is not enforced. */
10374 size_t header_pos = decl_file.rfind("trace/");
10375 if (header_pos == string::npos)
10376 throw SEMANTIC_ERROR ("cannot parse header location for tracepoint '"
10377 + tracepoint_name + "' in '"
10378 + decl_file + "'");
10379 header = decl_file.substr(header_pos);
10380 #endif
10381
10382 // tracepoints from FOO_event_types.h should really be included from FOO.h
10383 // XXX can dwarf tell us the include hierarchy? it would be better to
10384 // ... walk up to see which one was directly included by tracequery.c
10385 // XXX: see also PR9993.
10386 size_t header_pos = header.find("_event_types");
10387 if (header_pos != string::npos)
10388 header.erase(header_pos, 12);
10389
10390 // Now expand the local variables in the probe body
10391 tracepoint_var_expanding_visitor v (dw, name, args);
10392 v.replace (this->body);
10393 for (unsigned i = 0; i < args.size(); i++)
10394 if (args[i].used)
10395 {
10396 vardecl* v = new vardecl;
10397 v->name = "__tracepoint_arg_" + args[i].name;
10398 v->tok = this->tok;
10399 v->set_arity(0, this->tok);
10400 v->type = pe_long;
10401 v->synthetic = true;
10402 this->locals.push_back (v);
10403 }
10404
10405 if (sess.verbose > 2)
10406 clog << "tracepoint-based " << name << " tracepoint='" << tracepoint_name << "'" << endl;
10407 }
10408
10409
10410 static bool
10411 resolve_pointer_type(Dwarf_Die& die, bool& isptr)
10412 {
10413 if (null_die(&die))
10414 {
10415 isptr = true;
10416 return true;
10417 }
10418
10419 Dwarf_Die type;
10420 switch (dwarf_tag(&die))
10421 {
10422 case DW_TAG_typedef:
10423 case DW_TAG_const_type:
10424 case DW_TAG_volatile_type:
10425 case DW_TAG_restrict_type:
10426 // iterate on the referent type
10427 return (dwarf_attr_die(&die, DW_AT_type, &die)
10428 && resolve_pointer_type(die, isptr));
10429
10430 case DW_TAG_base_type:
10431 case DW_TAG_enumeration_type:
10432 case DW_TAG_structure_type:
10433 case DW_TAG_union_type:
10434 // base types will simply be treated as script longs
10435 // structs/unions must be referenced by pointer elsewhere
10436 isptr = false;
10437 return true;
10438
10439 case DW_TAG_array_type:
10440 case DW_TAG_pointer_type:
10441 case DW_TAG_reference_type:
10442 case DW_TAG_rvalue_reference_type:
10443 // pointer-like types can be treated as script longs,
10444 // and if we know their type, they can also be dereferenced
10445 isptr = true;
10446 type = die;
10447 while (dwarf_attr_die(&type, DW_AT_type, &type))
10448 {
10449 // It still might be a non-type, e.g. const void,
10450 // so we need to strip away all qualifiers.
10451 int tag = dwarf_tag(&type);
10452 if (tag != DW_TAG_typedef &&
10453 tag != DW_TAG_const_type &&
10454 tag != DW_TAG_volatile_type &&
10455 tag != DW_TAG_restrict_type)
10456 {
10457 die = type;
10458 return true;
10459 }
10460 }
10461 // otherwise use a null_die to indicate void
10462 std::memset(&die, 0, sizeof(die));
10463 return true;
10464
10465 default:
10466 // should we consider other types too?
10467 return false;
10468 }
10469 }
10470
10471
10472 static bool
10473 resolve_tracepoint_arg_type(tracepoint_arg& arg)
10474 {
10475 if (!resolve_pointer_type(arg.type_die, arg.isptr))
10476 return false;
10477
10478 if (arg.isptr)
10479 arg.typecast = "(intptr_t)";
10480 else if (dwarf_tag(&arg.type_die) == DW_TAG_structure_type ||
10481 dwarf_tag(&arg.type_die) == DW_TAG_union_type)
10482 {
10483 // for structs/unions which are passed by value, we turn it into
10484 // a pointer that can be dereferenced.
10485 arg.isptr = true;
10486 arg.typecast = "(intptr_t)&";
10487 }
10488 return true;
10489 }
10490
10491
10492 void
10493 tracepoint_derived_probe::build_args(dwflpp&, Dwarf_Die& func_die)
10494 {
10495 Dwarf_Die arg;
10496 if (dwarf_child(&func_die, &arg) == 0)
10497 do
10498 if (dwarf_tag(&arg) == DW_TAG_formal_parameter)
10499 {
10500 // build a tracepoint_arg for this parameter
10501 tracepoint_arg tparg;
10502 tparg.name = dwarf_diename(&arg) ?: "";
10503
10504 // read the type of this parameter
10505 if (!dwarf_attr_die (&arg, DW_AT_type, &tparg.type_die)
10506 || !dwarf_type_name(&tparg.type_die, tparg.c_type))
10507 throw SEMANTIC_ERROR (_F("cannot get type of parameter '%s' of tracepoint '%s'",
10508 tparg.name.c_str(), tracepoint_name.c_str()));
10509
10510 tparg.usable = resolve_tracepoint_arg_type(tparg);
10511 args.push_back(tparg);
10512 if (sess.verbose > 4)
10513 clog << _F("found parameter for tracepoint '%s': type:'%s' name:'%s' %s",
10514 tracepoint_name.c_str(), tparg.c_type.c_str(), tparg.name.c_str(),
10515 tparg.usable ? "ok" : "unavailable") << endl;
10516 }
10517 while (dwarf_siblingof(&arg, &arg) == 0);
10518 }
10519
10520 void
10521 tracepoint_derived_probe::getargs(std::list<std::string> &arg_set) const
10522 {
10523 for (unsigned i = 0; i < args.size(); ++i)
10524 if (args[i].usable)
10525 arg_set.push_back("$"+args[i].name+":"+args[i].c_type);
10526 }
10527
10528 void
10529 tracepoint_derived_probe::join_group (systemtap_session& s)
10530 {
10531 if (! s.tracepoint_derived_probes)
10532 s.tracepoint_derived_probes = new tracepoint_derived_probe_group ();
10533 s.tracepoint_derived_probes->enroll (this);
10534 this->group = s.tracepoint_derived_probes;
10535 }
10536
10537
10538 void
10539 tracepoint_derived_probe::print_dupe_stamp(ostream& o)
10540 {
10541 for (unsigned i = 0; i < args.size(); i++)
10542 if (args[i].used)
10543 o << "__tracepoint_arg_" << args[i].name << endl;
10544 }
10545
10546
10547 static vector<string> tracepoint_extra_decls (systemtap_session& s, const string& header)
10548 {
10549 vector<string> they_live;
10550 // PR 9993
10551 // XXX: may need this to be configurable
10552 they_live.push_back ("#include <linux/skbuff.h>");
10553
10554 // PR11649: conditional extra header
10555 // for kvm tracepoints in 2.6.33ish
10556 if (s.kernel_config["CONFIG_KVM"] != string("")) {
10557 they_live.push_back ("#include <linux/kvm_host.h>");
10558 }
10559
10560 if (header.find("xfs") != string::npos && s.kernel_config["CONFIG_XFS_FS"] != string("")) {
10561 they_live.push_back ("#define XFS_BIG_BLKNOS 1");
10562 if (s.kernel_source_tree != "")
10563 they_live.push_back ("#include \"fs/xfs/xfs_types.h\""); // in kernel-source tree
10564 they_live.push_back ("struct xfs_mount;");
10565 they_live.push_back ("struct xfs_inode;");
10566 they_live.push_back ("struct xfs_buf;");
10567 they_live.push_back ("struct xfs_bmbt_irec;");
10568 they_live.push_back ("struct xfs_trans;");
10569 }
10570
10571 if (header.find("nfs") != string::npos && s.kernel_config["CONFIG_NFSD"] != string("")) {
10572 they_live.push_back ("struct rpc_task;");
10573 }
10574 // RHEL6.3
10575 if (header.find("rpc") != string::npos && s.kernel_config["CONFIG_NFSD"] != string("")) {
10576 they_live.push_back ("struct rpc_clnt;");
10577 they_live.push_back ("struct rpc_wait_queue;");
10578 }
10579
10580 they_live.push_back ("#include <asm/cputime.h>");
10581
10582 // linux 3.0
10583 they_live.push_back ("struct cpu_workqueue_struct;");
10584
10585 if (header.find("ext4") != string::npos && s.kernel_config["CONFIG_EXT4_FS"] != string(""))
10586 if (s.kernel_source_tree != "")
10587 they_live.push_back ("#include \"fs/ext4/ext4.h\""); // in kernel-source tree
10588
10589 if (header.find("ext3") != string::npos)
10590 they_live.push_back ("struct ext3_reserve_window_node;");
10591
10592 if (header.find("workqueue") != string::npos)
10593 {
10594 they_live.push_back ("struct pool_workqueue;");
10595 they_live.push_back ("struct work_struct;");
10596 }
10597
10598 if (header.find("asoc") != string::npos)
10599 they_live.push_back ("struct snd_soc_dapm_path;");
10600
10601 if (header.find("9p") != string::npos)
10602 {
10603 they_live.push_back ("struct p9_client;");
10604 they_live.push_back ("struct p9_fcall;");
10605 }
10606
10607 if (header.find("bcache") != string::npos)
10608 {
10609 they_live.push_back ("struct bkey;");
10610 they_live.push_back ("struct btree;");
10611 they_live.push_back ("struct cache_set;");
10612 they_live.push_back ("struct cache;");
10613 }
10614
10615 if (header.find("f2fs") != string::npos)
10616 {
10617 // cannot get fs/f2fs/f2fs.h #included
10618 they_live.push_back ("typedef u32 block_t;");
10619 they_live.push_back ("typedef u32 nid_t;");
10620 }
10621
10622 if (header.find("radeon") != string::npos)
10623 they_live.push_back ("struct radeon_bo;");
10624
10625 // argh, 3.11, i915_trace.h -> i915_drv.h -> i915_reg.h without -I
10626 // also brcms_trace_events.h -> ... -> "types.h"
10627 // XXX: need a way to add a temporary -I flag
10628
10629 if (header.find("/ath/") != string::npos)
10630 they_live.push_back ("struct ath5k_hw;");
10631
10632
10633 return they_live;
10634 }
10635
10636
10637 void
10638 tracepoint_derived_probe_group::emit_module_decls (systemtap_session& s)
10639 {
10640 if (probes.empty())
10641 return;
10642
10643 s.op->newline() << "/* ---- tracepoint probes ---- */";
10644 s.op->newline() << "#include <linux/stp_tracepoint.h>" << endl;
10645 s.op->newline();
10646
10647
10648 // We create a MODULE_aux_N.c file for each tracepoint header, to allow them
10649 // to be separately compiled. That's because kernel tracepoint headers sometimes
10650 // conflict. PR13155.
10651
10652 map<string,translator_output*> per_header_aux;
10653 // GC NB: the translator_output* structs are owned/retained by the systemtap_session.
10654
10655 for (unsigned i = 0; i < probes.size(); ++i)
10656 {
10657 tracepoint_derived_probe *p = probes[i];
10658 string header = p->header;
10659
10660 // We cache the auxiliary output files on a per-header basis. We don't
10661 // need one aux file per tracepoint, only one per tracepoint-header.
10662 translator_output *tpop = per_header_aux[header];
10663 if (tpop == 0)
10664 {
10665 tpop = s.op_create_auxiliary();
10666 per_header_aux[header] = tpop;
10667
10668 // PR9993: Add extra headers to work around undeclared types in individual
10669 // include/trace/foo.h files
10670 const vector<string>& extra_decls = tracepoint_extra_decls (s, header);
10671 for (unsigned z=0; z<extra_decls.size(); z++)
10672 tpop->newline() << extra_decls[z] << "\n";
10673
10674 // strip include/ substring, the same way as done in get_tracequery_module()
10675 size_t root_pos = header.rfind("include/");
10676 header = ((root_pos != string::npos) ? header.substr(root_pos + 8) : header);
10677
10678 tpop->newline() << "#include <linux/stp_tracepoint.h>" << endl;
10679 tpop->newline() << "#include <" << header << ">";
10680 }
10681
10682 // collect the args that are actually in use
10683 vector<const tracepoint_arg*> used_args;
10684 for (unsigned j = 0; j < p->args.size(); ++j)
10685 if (p->args[j].used)
10686 used_args.push_back(&p->args[j]);
10687
10688 // forward-declare the generated-side tracepoint callback, and define the
10689 // generated-side tracepoint callback in the main translator-output
10690 string enter_real_fn = "enter_real_tracepoint_probe_" + lex_cast(i);
10691 if (used_args.empty())
10692 {
10693 tpop->newline() << "STP_TRACE_ENTER_REAL_NOARGS(" << enter_real_fn << ");";
10694 s.op->newline() << "STP_TRACE_ENTER_REAL_NOARGS(" << enter_real_fn << ")";
10695 }
10696 else
10697 {
10698 tpop->newline() << "STP_TRACE_ENTER_REAL(" << enter_real_fn;
10699 s.op->newline() << "STP_TRACE_ENTER_REAL(" << enter_real_fn;
10700 s.op->indent(2);
10701 for (unsigned j = 0; j < used_args.size(); ++j)
10702 {
10703 tpop->line() << ", int64_t";
10704 s.op->newline() << ", int64_t __tracepoint_arg_" << used_args[j]->name;
10705 }
10706 tpop->line() << ");";
10707 s.op->newline() << ")";
10708 s.op->indent(-2);
10709 }
10710 s.op->newline() << "{";
10711 s.op->newline(1) << "const struct stap_probe * const probe = "
10712 << common_probe_init (p) << ";";
10713 common_probe_entryfn_prologue (s, "STAP_SESSION_RUNNING", "probe",
10714 "stp_probe_type_tracepoint");
10715 s.op->newline() << "c->ips.tracepoint_name = "
10716 << lex_cast_qstring (p->tracepoint_name)
10717 << ";";
10718 for (unsigned j = 0; j < used_args.size(); ++j)
10719 {
10720 s.op->newline() << "c->probe_locals." << p->name
10721 << "." + s.up->c_localname("__tracepoint_arg_" + used_args[j]->name)
10722 << " = __tracepoint_arg_" << used_args[j]->name << ";";
10723 }
10724 s.op->newline() << "(*probe->ph) (c);";
10725 common_probe_entryfn_epilogue (s, true, otf_safe_context(s));
10726 s.op->newline(-1) << "}";
10727
10728 // define the real tracepoint callback function
10729 string enter_fn = "enter_tracepoint_probe_" + lex_cast(i);
10730 if (p->args.empty())
10731 tpop->newline() << "static STP_TRACE_ENTER_NOARGS(" << enter_fn << ")";
10732 else
10733 {
10734 tpop->newline() << "static STP_TRACE_ENTER(" << enter_fn;
10735 s.op->indent(2);
10736 for (unsigned j = 0; j < p->args.size(); ++j)
10737 {
10738 tpop->newline() << ", " << p->args[j].c_type
10739 << " __tracepoint_arg_" << p->args[j].name;
10740 }
10741 tpop->newline() << ")";
10742 s.op->indent(-2);
10743 }
10744 tpop->newline() << "{";
10745 tpop->newline(1) << enter_real_fn << "(";
10746 tpop->indent(2);
10747 for (unsigned j = 0; j < used_args.size(); ++j)
10748 {
10749 if (j > 0)
10750 tpop->line() << ", ";
10751 tpop->newline() << "(int64_t)" << used_args[j]->typecast
10752 << "__tracepoint_arg_" << used_args[j]->name;
10753 }
10754 tpop->newline() << ");";
10755 tpop->newline(-3) << "}";
10756
10757
10758 // emit normalized registration functions
10759 tpop->newline() << "int register_tracepoint_probe_" << i << "(void) {";
10760 tpop->newline(1) << "return STP_TRACE_REGISTER(" << p->tracepoint_name
10761 << ", " << enter_fn << ");";
10762 tpop->newline(-1) << "}";
10763
10764 // NB: we're not prepared to deal with unreg failures. However, failures
10765 // can only occur if the tracepoint doesn't exist (yet?), or if we
10766 // weren't even registered. The former should be OKed by the initial
10767 // registration call, and the latter is safe to ignore.
10768 tpop->newline() << "void unregister_tracepoint_probe_" << i << "(void) {";
10769 tpop->newline(1) << "(void) STP_TRACE_UNREGISTER(" << p->tracepoint_name
10770 << ", " << enter_fn << ");";
10771 tpop->newline(-1) << "}";
10772 tpop->newline();
10773
10774 // declare normalized registration functions
10775 s.op->newline() << "int register_tracepoint_probe_" << i << "(void);";
10776 s.op->newline() << "void unregister_tracepoint_probe_" << i << "(void);";
10777
10778 tpop->assert_0_indent();
10779 }
10780
10781 // emit an array of registration functions for easy init/shutdown
10782 s.op->newline() << "static struct stap_tracepoint_probe {";
10783 s.op->newline(1) << "int (*reg)(void);";
10784 s.op->newline(0) << "void (*unreg)(void);";
10785 s.op->newline(-1) << "} stap_tracepoint_probes[] = {";
10786 s.op->indent(1);
10787 for (unsigned i = 0; i < probes.size(); ++i)
10788 {
10789 s.op->newline () << "{";
10790 s.op->line() << " .reg=&register_tracepoint_probe_" << i << ",";
10791 s.op->line() << " .unreg=&unregister_tracepoint_probe_" << i;
10792 s.op->line() << " },";
10793 }
10794 s.op->newline(-1) << "};";
10795 s.op->newline();
10796 }
10797
10798
10799 void
10800 tracepoint_derived_probe_group::emit_module_init (systemtap_session &s)
10801 {
10802 if (probes.size () == 0)
10803 return;
10804
10805 s.op->newline() << "/* init tracepoint probes */";
10806 s.op->newline() << "for (i=0; i<" << probes.size() << "; i++) {";
10807 s.op->newline(1) << "rc = stap_tracepoint_probes[i].reg();";
10808 s.op->newline() << "if (rc) {";
10809 s.op->newline(1) << "for (j=i-1; j>=0; j--)"; // partial rollback
10810 s.op->newline(1) << "stap_tracepoint_probes[j].unreg();";
10811 s.op->newline(-1) << "break;"; // don't attempt to register any more probes
10812 s.op->newline(-1) << "}";
10813 s.op->newline(-1) << "}";
10814
10815 // This would be technically proper (on those autoconf-detectable
10816 // kernels that include this function in tracepoint.h), however we
10817 // already make several calls to synchronze_sched() during our
10818 // shutdown processes.
10819
10820 // s.op->newline() << "if (rc)";
10821 // s.op->newline(1) << "tracepoint_synchronize_unregister();";
10822 // s.op->indent(-1);
10823 }
10824
10825
10826 void
10827 tracepoint_derived_probe_group::emit_module_exit (systemtap_session& s)
10828 {
10829 if (probes.empty())
10830 return;
10831
10832 s.op->newline() << "/* deregister tracepoint probes */";
10833 s.op->newline() << "for (i=0; i<" << probes.size() << "; i++)";
10834 s.op->newline(1) << "stap_tracepoint_probes[i].unreg();";
10835 s.op->indent(-1);
10836
10837 // Not necessary: see above.
10838
10839 // s.op->newline() << "tracepoint_synchronize_unregister();";
10840 }
10841
10842
10843 struct tracepoint_query : public base_query
10844 {
10845 tracepoint_query(dwflpp & dw, const string & tracepoint,
10846 probe * base_probe, probe_point * base_loc,
10847 vector<derived_probe *> & results):
10848 base_query(dw, "*"), tracepoint(tracepoint),
10849 base_probe(base_probe), base_loc(base_loc),
10850 results(results) {}
10851
10852 const string& tracepoint;
10853
10854 probe * base_probe;
10855 probe_point * base_loc;
10856 vector<derived_probe *> & results;
10857 set<string> probed_names;
10858
10859 void handle_query_module();
10860 int handle_query_cu(Dwarf_Die * cudie);
10861 int handle_query_func(Dwarf_Die * func);
10862 void query_library (const char *) {}
10863 void query_plt (const char *entry, size_t addr) {}
10864
10865 static int tracepoint_query_cu (Dwarf_Die * cudie, tracepoint_query * q);
10866 static int tracepoint_query_func (Dwarf_Die * func, tracepoint_query * q);
10867 };
10868
10869
10870 void
10871 tracepoint_query::handle_query_module()
10872 {
10873 // look for the tracepoints in each CU
10874 dw.iterate_over_cus(tracepoint_query_cu, this, false);
10875 }
10876
10877
10878 int
10879 tracepoint_query::handle_query_cu(Dwarf_Die * cudie)
10880 {
10881 dw.focus_on_cu (cudie);
10882 dw.mod_info->get_symtab();
10883
10884 // look at each function to see if it's a tracepoint
10885 string function = "stapprobe_" + tracepoint;
10886 return dw.iterate_over_functions (tracepoint_query_func, this, function);
10887 }
10888
10889
10890 int
10891 tracepoint_query::handle_query_func(Dwarf_Die * func)
10892 {
10893 dw.focus_on_function (func);
10894
10895 assert(startswith(dw.function_name, "stapprobe_"));
10896 string tracepoint_instance = dw.function_name.substr(10);
10897
10898 // check for duplicates -- sometimes tracepoint headers may be indirectly
10899 // included in more than one of our tracequery modules.
10900 if (!probed_names.insert(tracepoint_instance).second)
10901 return DWARF_CB_OK;
10902
10903 derived_probe *dp = new tracepoint_derived_probe (dw.sess, dw, *func,
10904 tracepoint_instance,
10905 base_probe, base_loc);
10906 results.push_back (dp);
10907 return DWARF_CB_OK;
10908 }
10909
10910
10911 int
10912 tracepoint_query::tracepoint_query_cu (Dwarf_Die * cudie, tracepoint_query * q)
10913 {
10914 if (pending_interrupts) return DWARF_CB_ABORT;
10915 return q->handle_query_cu(cudie);
10916 }
10917
10918
10919 int
10920 tracepoint_query::tracepoint_query_func (Dwarf_Die * func, tracepoint_query * q)
10921 {
10922 if (pending_interrupts) return DWARF_CB_ABORT;
10923 return q->handle_query_func(func);
10924 }
10925
10926
10927 struct tracepoint_builder: public derived_probe_builder
10928 {
10929 private:
10930 dwflpp *dw;
10931 bool init_dw(systemtap_session& s);
10932 void get_tracequery_modules(systemtap_session& s,
10933 const vector<string>& headers,
10934 vector<string>& modules);
10935
10936 public:
10937
10938 tracepoint_builder(): dw(0) {}
10939 ~tracepoint_builder() { delete dw; }
10940
10941 void build_no_more (systemtap_session& s)
10942 {
10943 if (dw && s.verbose > 3)
10944 clog << _("tracepoint_builder releasing dwflpp") << endl;
10945 delete dw;
10946 dw = NULL;
10947
10948 delete_session_module_cache (s);
10949 }
10950
10951 void build(systemtap_session& s,
10952 probe *base, probe_point *location,
10953 literal_map_t const& parameters,
10954 vector<derived_probe*>& finished_results);
10955 };
10956
10957
10958
10959 // Create (or cache) one or more tracequery .o modules, based upon the
10960 // tracepoint-related header files given. Return the generated or cached
10961 // modules[].
10962
10963 void
10964 tracepoint_builder::get_tracequery_modules(systemtap_session& s,
10965 const vector<string>& headers,
10966 vector<string>& modules)
10967 {
10968 if (s.verbose > 2)
10969 {
10970 clog << _F("Pass 2: getting a tracepoint query for %zu headers: ", headers.size()) << endl;
10971 for (size_t i = 0; i < headers.size(); ++i)
10972 clog << " " << headers[i] << endl;
10973 }
10974
10975 map<string,string> headers_cache_obj; // header name -> cache/.../tracequery_hash.o file name
10976 // Map the headers to cache .o names. Note that this has side-effects of
10977 // creating the $SYSTEMTAP_DIR/.cache/XX/... directory and the hash-log file,
10978 // so we prefer not to repeat this.
10979 vector<string> uncached_headers;
10980 for (size_t i=0; i<headers.size(); i++)
10981 headers_cache_obj[headers[i]] = find_tracequery_hash(s, headers[i]);
10982
10983 // They may be in the cache already.
10984 if (s.use_cache && !s.poison_cache)
10985 for (size_t i=0; i<headers.size(); i++)
10986 {
10987 // see if the cached module exists
10988 const string& tracequery_path = headers_cache_obj[headers[i]];
10989 if (!tracequery_path.empty() && file_exists(tracequery_path))
10990 {
10991 if (s.verbose > 2)
10992 clog << _F("Pass 2: using cached %s", tracequery_path.c_str()) << endl;
10993
10994 // an empty file is a cached failure
10995 if (get_file_size(tracequery_path) > 0)
10996 modules.push_back (tracequery_path);
10997 }
10998 else
10999 uncached_headers.push_back(headers[i]);
11000 }
11001 else
11002 uncached_headers = headers;
11003
11004 // If we have nothing left to search for, quit
11005 if (uncached_headers.empty()) return;
11006
11007 map<string,string> headers_tracequery_src; // header -> C-source code mapping
11008
11009 // We could query several subsets of headers[] to make this go
11010 // faster, but let's KISS and do one at a time.
11011 for (size_t i=0; i<uncached_headers.size(); i++)
11012 {
11013 const string& header = uncached_headers[i];
11014
11015 // create a tracequery source file
11016 ostringstream osrc;
11017
11018 // PR9993: Add extra headers to work around undeclared types in individual
11019 // include/trace/foo.h files
11020 vector<string> short_decls = tracepoint_extra_decls(s, header);
11021
11022 // add each requested tracepoint header
11023 size_t root_pos = header.rfind("include/");
11024 short_decls.push_back(string("#include <") +
11025 ((root_pos != string::npos) ? header.substr(root_pos + 8) : header) +
11026 string(">"));
11027
11028 osrc << "#ifdef CONFIG_TRACEPOINTS" << endl;
11029 osrc << "#include <linux/tracepoint.h>" << endl;
11030
11031 // the kernel has changed this naming a few times, previously TPPROTO,
11032 // TP_PROTO, TPARGS, TP_ARGS, etc. so let's just dupe the latest.
11033 osrc << "#ifndef PARAMS" << endl;
11034 osrc << "#define PARAMS(args...) args" << endl;
11035 osrc << "#endif" << endl;
11036
11037 // override DECLARE_TRACE to synthesize probe functions for us
11038 osrc << "#undef DECLARE_TRACE" << endl;
11039 osrc << "#define DECLARE_TRACE(name, proto, args) \\" << endl;
11040 osrc << " void stapprobe_##name(proto) {}" << endl;
11041
11042 // 2.6.35 added the NOARGS variant, but it's the same for us
11043 osrc << "#undef DECLARE_TRACE_NOARGS" << endl;
11044 osrc << "#define DECLARE_TRACE_NOARGS(name) \\" << endl;
11045 osrc << " DECLARE_TRACE(name, void, )" << endl;
11046
11047 // 2.6.38 added the CONDITION variant, which can also just redirect
11048 osrc << "#undef DECLARE_TRACE_CONDITION" << endl;
11049 osrc << "#define DECLARE_TRACE_CONDITION(name, proto, args, cond) \\" << endl;
11050 osrc << " DECLARE_TRACE(name, PARAMS(proto), PARAMS(args))" << endl;
11051
11052 // older tracepoints used DEFINE_TRACE, so redirect that too
11053 osrc << "#undef DEFINE_TRACE" << endl;
11054 osrc << "#define DEFINE_TRACE(name, proto, args) \\" << endl;
11055 osrc << " DECLARE_TRACE(name, PARAMS(proto), PARAMS(args))" << endl;
11056
11057 // add the specified decls/#includes
11058 for (unsigned z=0; z<short_decls.size(); z++)
11059 osrc << "#undef TRACE_INCLUDE_FILE\n"
11060 << "#undef TRACE_INCLUDE_PATH\n"
11061 << short_decls[z] << "\n";
11062
11063 // finish up the module source
11064 osrc << "#endif /* CONFIG_TRACEPOINTS */" << endl;
11065
11066 // save the source file away
11067 headers_tracequery_src[header] = osrc.str();
11068 }
11069
11070 // now build them all together
11071 map<string,string> tracequery_objs = make_tracequeries(s, headers_tracequery_src);
11072
11073 // now plop them into the cache
11074 if (s.use_cache)
11075 for (size_t i=0; i<uncached_headers.size(); i++)
11076 {
11077 const string& header = uncached_headers[i];
11078 const string& tracequery_obj = tracequery_objs[header];
11079 const string& tracequery_path = headers_cache_obj[header];
11080 if (tracequery_obj !="" && file_exists(tracequery_obj))
11081 {
11082 copy_file(tracequery_obj, tracequery_path, s.verbose > 2);
11083 modules.push_back (tracequery_path);
11084 }
11085 else
11086 // cache an empty file for failures
11087 copy_file("/dev/null", tracequery_path, s.verbose > 2);
11088 }
11089 }
11090
11091
11092
11093 bool
11094 tracepoint_builder::init_dw(systemtap_session& s)
11095 {
11096 if (dw != NULL)
11097 return true;
11098
11099 vector<string> tracequery_modules;
11100 vector<string> system_headers;
11101
11102 glob_t trace_glob;
11103
11104 // find kernel_source_tree
11105 if (s.kernel_source_tree == "")
11106 {
11107 unsigned found;
11108 Dwfl *dwfl = setup_dwfl_kernel ("kernel", &found, s);
11109 if (found)
11110 {
11111 Dwarf_Die *cudie = 0;
11112 Dwarf_Addr bias;
11113 while ((cudie = dwfl_nextcu (dwfl, cudie, &bias)) != NULL)
11114 {
11115 assert_no_interrupts();
11116 Dwarf_Attribute attr;
11117 const char* name = dwarf_formstring (dwarf_attr (cudie, DW_AT_comp_dir, &attr));
11118 if (name)
11119 {
11120 // check that the path actually exists locally before we try to use it
11121 if (file_exists(name))
11122 {
11123 if (s.verbose > 2)
11124 clog << _F("Located kernel source tree (DW_AT_comp_dir) at '%s'", name) << endl;
11125 s.kernel_source_tree = name;
11126 }
11127 else
11128 {
11129 if (s.verbose > 2)
11130 clog << _F("Ignoring inaccessible kernel source tree (DW_AT_comp_dir) at '%s'", name) << endl;
11131 }
11132
11133 break; // skip others; modern Kbuild uses same comp_dir for them all
11134 }
11135 }
11136 }
11137 dwfl_end (dwfl);
11138 }
11139
11140 // prefixes
11141 vector<string> glob_prefixes;
11142 glob_prefixes.push_back (s.kernel_build_tree);
11143 if (s.kernel_source_tree != "")
11144 glob_prefixes.push_back (s.kernel_source_tree);
11145
11146 // suffixes
11147 vector<string> glob_suffixes;
11148 glob_suffixes.push_back("include/trace/events/*.h");
11149 glob_suffixes.push_back("include/trace/*.h");
11150 glob_suffixes.push_back("include/ras/*_event.h");
11151 glob_suffixes.push_back("arch/x86/kvm/*trace.h");
11152 glob_suffixes.push_back("arch/x86/kernel/*trace.h");
11153 glob_suffixes.push_back("arch/*/include/asm/trace*.h");
11154 glob_suffixes.push_back("arch/*/include/asm/trace/*.h");
11155 glob_suffixes.push_back("fs/xfs/linux-*/xfs_tr*.h");
11156 glob_suffixes.push_back("fs/*/*trace*.h");
11157 glob_suffixes.push_back("net/*/*trace*.h");
11158 glob_suffixes.push_back("sound/pci/hda/*_trace.h");
11159 glob_suffixes.push_back("drivers/gpu/drm/*_trace.h");
11160 glob_suffixes.push_back("drivers/gpu/drm/*/*_trace.h");
11161 glob_suffixes.push_back("drivers/net/wireless/*/*/*trace*.h");
11162 glob_suffixes.push_back("drivers/usb/host/*trace*.h");
11163
11164 // see also tracepoint_extra_decls above
11165
11166 // compute cartesian product
11167 vector<string> globs;
11168 for (unsigned i=0; i<glob_prefixes.size(); i++)
11169 for (unsigned j=0; j<glob_suffixes.size(); j++)
11170 globs.push_back (glob_prefixes[i]+string("/")+glob_suffixes[j]);
11171
11172 set<string> duped_headers;
11173 for (unsigned z = 0; z < globs.size(); z++)
11174 {
11175 string glob_str = globs[z];
11176 if (s.verbose > 3)
11177 clog << _("Checking tracepoint glob ") << glob_str << endl;
11178
11179 int r = glob(glob_str.c_str(), 0, NULL, &trace_glob);
11180 if (r == GLOB_NOSPACE || r == GLOB_ABORTED)
11181 throw runtime_error("Error globbing tracepoint");
11182
11183 for (unsigned i = 0; i < trace_glob.gl_pathc; ++i)
11184 {
11185 string header(trace_glob.gl_pathv[i]);
11186
11187 // filter out a few known "internal-only" headers
11188 if (endswith(header, "/define_trace.h") ||
11189 endswith(header, "/ftrace.h") ||
11190 endswith(header, "/trace_events.h") ||
11191 endswith(header, "_event_types.h"))
11192 continue;
11193
11194 // With headers now plopped under arch/FOO/include/asm/*,
11195 // the following logic miss some tracepoints.
11196 #if 0
11197 // skip identical headers from the build and source trees.
11198 size_t root_pos = header.rfind("include/");
11199 if (root_pos != string::npos &&
11200 !duped_headers.insert(header.substr(root_pos + 8)).second)
11201 continue;
11202 #endif
11203
11204 system_headers.push_back(header);
11205 }
11206 globfree(&trace_glob);
11207 }
11208
11209 // Build tracequery modules
11210 get_tracequery_modules(s, system_headers, tracequery_modules);
11211
11212 // TODO: consider other sources of tracepoint headers too, like from
11213 // a command-line parameter or some environment or .systemtaprc
11214
11215 dw = new dwflpp(s, tracequery_modules, true);
11216 return true;
11217 }
11218
11219 void
11220 tracepoint_builder::build(systemtap_session& s,
11221 probe *base, probe_point *location,
11222 literal_map_t const& parameters,
11223 vector<derived_probe*>& finished_results)
11224 {
11225 if (!init_dw(s))
11226 return;
11227
11228 string tracepoint;
11229 assert(get_param (parameters, TOK_TRACE, tracepoint));
11230
11231 tracepoint_query q(*dw, tracepoint, base, location, finished_results);
11232 unsigned results_pre = finished_results.size();
11233 dw->iterate_over_modules<base_query>(&query_module, &q);
11234 unsigned results_post = finished_results.size();
11235
11236 // Did we fail to find a match? Let's suggest something!
11237 if (results_pre == results_post)
11238 {
11239 size_t pos;
11240 string sugs = suggest_dwarf_functions(s, q.visited_modules, tracepoint);
11241 while ((pos = sugs.find("stapprobe_")) != string::npos)
11242 sugs.erase(pos, string("stapprobe_").size());
11243 if (!sugs.empty())
11244 throw SEMANTIC_ERROR (_NF("no match (similar tracepoint: %s)",
11245 "no match (similar tracepoints: %s)",
11246 sugs.find(',') == string::npos,
11247 sugs.c_str()));
11248 }
11249 }
11250
11251
11252 // ------------------------------------------------------------------------
11253 // Standard tapset registry.
11254 // ------------------------------------------------------------------------
11255
11256 void
11257 register_standard_tapsets(systemtap_session & s)
11258 {
11259 register_tapset_been(s);
11260 register_tapset_itrace(s);
11261 register_tapset_mark(s);
11262 register_tapset_procfs(s);
11263 register_tapset_timers(s);
11264 register_tapset_netfilter(s);
11265 register_tapset_utrace(s);
11266
11267 // dwarf-based kprobe/uprobe parts
11268 dwarf_derived_probe::register_patterns(s);
11269
11270 // XXX: user-space starter set
11271 s.pattern_root->bind_num(TOK_PROCESS)
11272 ->bind_num(TOK_STATEMENT)->bind(TOK_ABSOLUTE)
11273 ->bind_privilege(pr_all)
11274 ->bind(new uprobe_builder ());
11275 s.pattern_root->bind_num(TOK_PROCESS)
11276 ->bind_num(TOK_STATEMENT)->bind(TOK_ABSOLUTE)->bind(TOK_RETURN)
11277 ->bind_privilege(pr_all)
11278 ->bind(new uprobe_builder ());
11279
11280 // kernel tracepoint probes
11281 s.pattern_root->bind(TOK_KERNEL)->bind_str(TOK_TRACE)
11282 ->bind(new tracepoint_builder());
11283
11284 // Kprobe based probe
11285 s.pattern_root->bind(TOK_KPROBE)->bind_str(TOK_FUNCTION)
11286 ->bind(new kprobe_builder());
11287 s.pattern_root->bind(TOK_KPROBE)->bind_str(TOK_FUNCTION)->bind(TOK_CALL)
11288 ->bind(new kprobe_builder());
11289 s.pattern_root->bind(TOK_KPROBE)->bind_str(TOK_MODULE)
11290 ->bind_str(TOK_FUNCTION)->bind(new kprobe_builder());
11291 s.pattern_root->bind(TOK_KPROBE)->bind_str(TOK_MODULE)
11292 ->bind_str(TOK_FUNCTION)->bind(TOK_CALL)->bind(new kprobe_builder());
11293 s.pattern_root->bind(TOK_KPROBE)->bind_str(TOK_FUNCTION)->bind(TOK_RETURN)
11294 ->bind(new kprobe_builder());
11295 s.pattern_root->bind(TOK_KPROBE)->bind_str(TOK_FUNCTION)->bind(TOK_RETURN)
11296 ->bind_num(TOK_MAXACTIVE)->bind(new kprobe_builder());
11297 s.pattern_root->bind(TOK_KPROBE)->bind_str(TOK_MODULE)
11298 ->bind_str(TOK_FUNCTION)->bind(TOK_RETURN)->bind(new kprobe_builder());
11299 s.pattern_root->bind(TOK_KPROBE)->bind_str(TOK_MODULE)
11300 ->bind_str(TOK_FUNCTION)->bind(TOK_RETURN)
11301 ->bind_num(TOK_MAXACTIVE)->bind(new kprobe_builder());
11302 s.pattern_root->bind(TOK_KPROBE)->bind_num(TOK_STATEMENT)
11303 ->bind(TOK_ABSOLUTE)->bind(new kprobe_builder());
11304
11305 //Hwbkpt based probe
11306 // NB: we formerly registered the probe point types only if the kernel configuration
11307 // allowed it. However, we get better error messages if we allow probes to resolve.
11308 s.pattern_root->bind(TOK_KERNEL)->bind_num(TOK_HWBKPT)
11309 ->bind(TOK_HWBKPT_WRITE)->bind(new hwbkpt_builder());
11310 s.pattern_root->bind(TOK_KERNEL)->bind_str(TOK_HWBKPT)
11311 ->bind(TOK_HWBKPT_WRITE)->bind(new hwbkpt_builder());
11312 s.pattern_root->bind(TOK_KERNEL)->bind_num(TOK_HWBKPT)
11313 ->bind(TOK_HWBKPT_RW)->bind(new hwbkpt_builder());
11314 s.pattern_root->bind(TOK_KERNEL)->bind_str(TOK_HWBKPT)
11315 ->bind(TOK_HWBKPT_RW)->bind(new hwbkpt_builder());
11316 s.pattern_root->bind(TOK_KERNEL)->bind_num(TOK_HWBKPT)
11317 ->bind_num(TOK_LENGTH)->bind(TOK_HWBKPT_WRITE)->bind(new hwbkpt_builder());
11318 s.pattern_root->bind(TOK_KERNEL)->bind_num(TOK_HWBKPT)
11319 ->bind_num(TOK_LENGTH)->bind(TOK_HWBKPT_RW)->bind(new hwbkpt_builder());
11320 // length supported with address only, not symbol names
11321
11322 //perf event based probe
11323 register_tapset_perf(s);
11324 register_tapset_java(s);
11325 }
11326
11327
11328 vector<derived_probe_group*>
11329 all_session_groups(systemtap_session& s)
11330 {
11331 vector<derived_probe_group*> g;
11332
11333 #define DOONE(x) \
11334 if (s. x##_derived_probes) \
11335 g.push_back ((derived_probe_group*)(s. x##_derived_probes))
11336
11337 // Note that order *is* important here. We want to make sure we
11338 // register (actually run) begin probes before any other probe type
11339 // is run. Similarly, when unregistering probes, we want to
11340 // unregister (actually run) end probes after every other probe type
11341 // has be unregistered. To do the latter,
11342 // c_unparser::emit_module_exit() will run this list backwards.
11343 DOONE(be);
11344 DOONE(dwarf);
11345 DOONE(uprobe);
11346 DOONE(timer);
11347 DOONE(profile);
11348 DOONE(mark);
11349 DOONE(tracepoint);
11350 DOONE(kprobe);
11351 DOONE(hwbkpt);
11352 DOONE(perf);
11353 DOONE(hrtimer);
11354 DOONE(procfs);
11355 DOONE(netfilter);
11356
11357 // Another "order is important" item. We want to make sure we
11358 // "register" the dummy task_finder probe group after all probe
11359 // groups that use the task_finder.
11360 DOONE(utrace);
11361 DOONE(itrace);
11362 DOONE(dynprobe);
11363 DOONE(task_finder);
11364 #undef DOONE
11365 return g;
11366 }
11367
11368 /* vim: set sw=2 ts=8 cino=>4,n-2,{2,^-2,t0,(0,u0,w1,M1 : */
This page took 0.632396 seconds and 5 git commands to generate.