]> sourceware.org Git - systemtap.git/blob - tapsets.cxx
kill interned_string::c_str(), allowing shared substr()
[systemtap.git] / tapsets.cxx
1 // tapset resolution
2 // Copyright (C) 2005-2015 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 #include "stringtable.h"
30
31 #include <cstdlib>
32 #include <algorithm>
33 #include <deque>
34 #include <iostream>
35 #include <fstream>
36 #include <map>
37 #include <set>
38 #include <sstream>
39 #include <stdexcept>
40 #include <vector>
41 #include <stack>
42 #include <cstdarg>
43 #include <cassert>
44 #include <iomanip>
45 #include <cerrno>
46
47 extern "C" {
48 #include <fcntl.h>
49 #include <elfutils/libdwfl.h>
50 #include <elfutils/libdw.h>
51 #include <dwarf.h>
52 #include <elf.h>
53 #include <obstack.h>
54 #include <glob.h>
55 #include <fnmatch.h>
56 #include <stdio.h>
57 #include <sys/types.h>
58 #include <sys/stat.h>
59 #include <math.h>
60 #include <regex.h>
61 #include <unistd.h>
62
63 #define __STDC_FORMAT_MACROS
64 #include <inttypes.h>
65 }
66
67 using namespace std;
68 using namespace __gnu_cxx;
69
70 // for elf.h where PPC64_LOCAL_ENTRY_OFFSET isn't defined
71 #ifndef PPC64_LOCAL_ENTRY_OFFSET
72 #define STO_PPC64_LOCAL_BIT 5
73 #define STO_PPC64_LOCAL_MASK (7 << STO_PPC64_LOCAL_BIT)
74 #define PPC64_LOCAL_ENTRY_OFFSET(other) \
75 (((1 << (((other) & STO_PPC64_LOCAL_MASK) >> STO_PPC64_LOCAL_BIT)) >> 2) << 2)
76 #endif
77 // for elf.h where EF_PPC64_ABI isn't defined
78 #ifndef EF_PPC64_ABI
79 #define EF_PPC64_ABI 3
80 #endif
81
82 // ------------------------------------------------------------------------
83
84 string
85 common_probe_init (derived_probe* p)
86 {
87 assert(p->session_index != (unsigned)-1);
88 return "(&stap_probes[" + lex_cast(p->session_index) + "])";
89 }
90
91
92 void
93 common_probe_entryfn_prologue (systemtap_session& s,
94 string statestr, string probe,
95 string probe_type, bool overload_processing)
96 {
97 if (s.runtime_usermode_p())
98 {
99 // If session_state() is NULL, then we haven't even initialized shm yet,
100 // and there's *nothing* for the probe to do. (even alibi is in shm)
101 // So failure skips this whole block through the end of the epilogue.
102 s.op->newline() << "if (likely(session_state())) {";
103 s.op->indent(1);
104 }
105
106 s.op->newline() << "#ifdef STP_ALIBI";
107 s.op->newline() << "atomic_inc(probe_alibi(" << probe << "->index));";
108 s.op->newline() << "#else";
109
110 if (s.runtime_usermode_p())
111 s.op->newline() << "int _stp_saved_errno = errno;";
112
113 s.op->newline() << "struct context* __restrict__ c = NULL;";
114 s.op->newline() << "#if !INTERRUPTIBLE";
115 s.op->newline() << "unsigned long flags;";
116 s.op->newline() << "#endif";
117
118 s.op->newline() << "#ifdef STP_TIMING";
119 s.op->newline() << "Stat stat = probe_timing(" << probe << "->index);";
120 s.op->newline() << "#endif";
121 if (overload_processing && !s.runtime_usermode_p())
122 s.op->newline() << "#if defined(STP_TIMING) || defined(STP_OVERLOAD)";
123 else
124 s.op->newline() << "#ifdef STP_TIMING";
125
126 if (! s.runtime_usermode_p())
127 s.op->newline() << "cycles_t cycles_atstart = get_cycles ();";
128 else
129 {
130 s.op->newline() << "struct timespec timespec_atstart;";
131 s.op->newline() << "(void)clock_gettime(CLOCK_MONOTONIC, &timespec_atstart);";
132 }
133 s.op->newline() << "#endif";
134
135 s.op->newline() << "#if !INTERRUPTIBLE";
136 s.op->newline() << "local_irq_save (flags);";
137 s.op->newline() << "#endif";
138
139 if (! s.runtime_usermode_p())
140 {
141 // Check for enough free enough stack space
142 s.op->newline() << "if (unlikely ((((unsigned long) (& c)) & (THREAD_SIZE-1))"; // free space
143 s.op->newline(1) << "< (MINSTACKSPACE + sizeof (struct thread_info)))) {"; // needed space
144 // XXX: may need porting to platforms where task_struct is not
145 // at bottom of kernel stack NB: see also
146 // CONFIG_DEBUG_STACKOVERFLOW
147 s.op->newline() << "atomic_inc (skipped_count());";
148 s.op->newline() << "#ifdef STP_TIMING";
149 s.op->newline() << "atomic_inc (skipped_count_lowstack());";
150 s.op->newline() << "#endif";
151 s.op->newline() << "goto probe_epilogue;";
152 s.op->newline(-1) << "}";
153 }
154
155 s.op->newline() << "if (atomic_read (session_state()) != " << statestr << ")";
156 s.op->newline(1) << "goto probe_epilogue;";
157 s.op->indent(-1);
158
159 s.op->newline() << "c = _stp_runtime_entryfn_get_context();";
160 s.op->newline() << "if (!c) {";
161 s.op->newline(1) << "#if !INTERRUPTIBLE";
162 s.op->newline() << "atomic_inc (skipped_count());";
163 s.op->newline() << "#endif";
164 s.op->newline() << "#ifdef STP_TIMING";
165 s.op->newline() << "atomic_inc (skipped_count_reentrant());";
166 s.op->newline() << "#endif";
167 s.op->newline() << "goto probe_epilogue;";
168 s.op->newline(-1) << "}";
169
170 s.op->newline();
171 s.op->newline() << "c->last_stmt = 0;";
172 s.op->newline() << "c->last_error = 0;";
173 s.op->newline() << "c->nesting = -1;"; // NB: PR10516 packs locals[] tighter
174 s.op->newline() << "c->uregs = 0;";
175 s.op->newline() << "c->kregs = 0;";
176 s.op->newline() << "#if defined __ia64__";
177 s.op->newline() << "c->unwaddr = 0;";
178 s.op->newline() << "#endif";
179 if (s.runtime_usermode_p())
180 s.op->newline() << "c->probe_index = " << probe << "->index;";
181 s.op->newline() << "c->probe_point = " << probe << "->pp;";
182 s.op->newline() << "#ifdef STP_NEED_PROBE_NAME";
183 s.op->newline() << "c->probe_name = " << probe << "->pn;";
184 s.op->newline() << "#endif";
185 s.op->newline() << "c->probe_type = " << probe_type << ";";
186 // reset Individual Probe State union
187 s.op->newline() << "memset(&c->ips, 0, sizeof(c->ips));";
188 s.op->newline() << "c->user_mode_p = 0; c->full_uregs_p = 0;";
189 s.op->newline() << "#ifdef STAP_NEED_REGPARM"; // i386 or x86_64 register.stp
190 s.op->newline() << "c->regparm = 0;";
191 s.op->newline() << "#endif";
192
193 if(!s.suppress_time_limits){
194 s.op->newline() << "#if INTERRUPTIBLE";
195 s.op->newline() << "c->actionremaining = MAXACTION_INTERRUPTIBLE;";
196 s.op->newline() << "#else";
197 s.op->newline() << "c->actionremaining = MAXACTION;";
198 s.op->newline() << "#endif";
199 }
200 // NB: The following would actually be incorrect.
201 // That's because cycles_sum/cycles_base values are supposed to survive
202 // between consecutive probes. Periodically (STP_OVERLOAD_INTERVAL
203 // cycles), the values will be reset.
204 /*
205 s.op->newline() << "#ifdef STP_OVERLOAD";
206 s.op->newline() << "c->cycles_sum = 0;";
207 s.op->newline() << "c->cycles_base = 0;";
208 s.op->newline() << "#endif";
209 */
210
211 s.op->newline() << "#if defined(STP_NEED_UNWIND_DATA)";
212 s.op->newline() << "c->uwcache_user.state = uwcache_uninitialized;";
213 s.op->newline() << "c->uwcache_kernel.state = uwcache_uninitialized;";
214 s.op->newline() << "#endif";
215 }
216
217
218 void
219 common_probe_entryfn_epilogue (systemtap_session& s,
220 bool overload_processing,
221 bool schedule_work_safe)
222 {
223 if (!s.runtime_usermode_p()
224 && schedule_work_safe)
225 {
226 // If a refresh is required, we can safely schedule_work() here
227 s.op->newline( 0) << "if (atomic_cmpxchg(&need_module_refresh, 1, 0) == 1)";
228 s.op->newline(+1) << "schedule_work(&module_refresher_work);";
229 s.op->indent(-1);
230 }
231
232 if (overload_processing && !s.runtime_usermode_p())
233 s.op->newline() << "#if defined(STP_TIMING) || defined(STP_OVERLOAD)";
234 else
235 s.op->newline() << "#ifdef STP_TIMING";
236 s.op->newline() << "{";
237 s.op->indent(1);
238 if (! s.runtime_usermode_p())
239 {
240 s.op->newline() << "cycles_t cycles_atend = get_cycles ();";
241 // NB: we truncate cycles counts to 32 bits. Perhaps it should be
242 // fewer, if the hardware counter rolls over really quickly. We
243 // handle 32-bit wraparound here.
244 s.op->newline() << "int32_t cycles_elapsed = ((int32_t)cycles_atend > (int32_t)cycles_atstart)";
245 s.op->newline(1) << "? ((int32_t)cycles_atend - (int32_t)cycles_atstart)";
246 s.op->newline() << ": (~(int32_t)0) - (int32_t)cycles_atstart + (int32_t)cycles_atend + 1;";
247 s.op->indent(-1);
248 }
249 else
250 {
251 s.op->newline() << "struct timespec timespec_atend, timespec_elapsed;";
252 s.op->newline() << "long cycles_elapsed;";
253 s.op->newline() << "(void)clock_gettime(CLOCK_MONOTONIC, &timespec_atend);";
254 s.op->newline() << "_stp_timespec_sub(&timespec_atend, &timespec_atstart, &timespec_elapsed);";
255 // 'cycles_elapsed' is really elapsed nanoseconds
256 s.op->newline() << "cycles_elapsed = (timespec_elapsed.tv_sec * NSEC_PER_SEC) + timespec_elapsed.tv_nsec;";
257 }
258
259 s.op->newline() << "#ifdef STP_TIMING";
260 s.op->newline() << "if (likely (stat)) _stp_stat_add(stat, cycles_elapsed);";
261 s.op->newline() << "#endif";
262
263 if (overload_processing && !s.runtime_usermode_p())
264 {
265 s.op->newline() << "#ifdef STP_OVERLOAD";
266 s.op->newline() << "{";
267 // If the cycle count has wrapped (cycles_atend > cycles_base),
268 // let's go ahead and pretend the interval has been reached.
269 // This should reset cycles_base and cycles_sum.
270 s.op->newline(1) << "cycles_t interval = (cycles_atend > c->cycles_base)";
271 s.op->newline(1) << "? (cycles_atend - c->cycles_base)";
272 s.op->newline() << ": (STP_OVERLOAD_INTERVAL + 1);";
273 s.op->newline(-1) << "c->cycles_sum += cycles_elapsed;";
274
275 // If we've spent more than STP_OVERLOAD_THRESHOLD cycles in a
276 // probe during the last STP_OVERLOAD_INTERVAL cycles, the probe
277 // has overloaded the system and we need to quit.
278 // NB: this is not suppressible via --suppress-runtime-errors,
279 // because this is a system safety metric that we cannot trust
280 // unprivileged users to override.
281 s.op->newline() << "if (interval > STP_OVERLOAD_INTERVAL) {";
282 s.op->newline(1) << "if (c->cycles_sum > STP_OVERLOAD_THRESHOLD) {";
283 s.op->newline(1) << "_stp_error (\"probe overhead exceeded threshold\");";
284 s.op->newline() << "atomic_set (session_state(), STAP_SESSION_ERROR);";
285 s.op->newline() << "atomic_inc (error_count());";
286 s.op->newline(-1) << "}";
287
288 s.op->newline() << "c->cycles_base = cycles_atend;";
289 s.op->newline() << "c->cycles_sum = 0;";
290 s.op->newline(-1) << "}";
291 s.op->newline(-1) << "}";
292 s.op->newline() << "#endif";
293 }
294
295 s.op->newline(-1) << "}";
296 s.op->newline() << "#endif";
297
298 s.op->newline() << "c->probe_point = 0;"; // vacated
299 s.op->newline() << "#ifdef STP_NEED_PROBE_NAME";
300 s.op->newline() << "c->probe_name = 0;";
301 s.op->newline() << "#endif";
302 s.op->newline() << "c->probe_type = 0;";
303
304
305 s.op->newline() << "if (unlikely (c->last_error)) {";
306 s.op->indent(1);
307 if (s.suppress_handler_errors) // PR 13306
308 {
309 s.op->newline() << "atomic_inc (error_count());";
310 }
311 else
312 {
313 s.op->newline() << "if (c->last_stmt != NULL)";
314 s.op->newline(1) << "_stp_softerror (\"%s near %s\", c->last_error, c->last_stmt);";
315 s.op->newline(-1) << "else";
316 s.op->newline(1) << "_stp_softerror (\"%s\", c->last_error);";
317 s.op->indent(-1);
318 s.op->newline() << "atomic_inc (error_count());";
319 s.op->newline() << "if (atomic_read (error_count()) > MAXERRORS) {";
320 s.op->newline(1) << "atomic_set (session_state(), STAP_SESSION_ERROR);";
321 s.op->newline() << "_stp_exit ();";
322 s.op->newline(-1) << "}";
323 }
324
325 s.op->newline(-1) << "}";
326
327
328 s.op->newline(-1) << "probe_epilogue:"; // context is free
329 s.op->indent(1);
330
331 if (! s.suppress_handler_errors) // PR 13306
332 {
333 // Check for excessive skip counts.
334 s.op->newline() << "if (unlikely (atomic_read (skipped_count()) > MAXSKIPPED)) {";
335 s.op->newline(1) << "if (unlikely (pseudo_atomic_cmpxchg(session_state(), STAP_SESSION_RUNNING, STAP_SESSION_ERROR) == STAP_SESSION_RUNNING))";
336 s.op->newline() << "_stp_error (\"Skipped too many probes, check MAXSKIPPED or try again with stap -t for more details.\");";
337 s.op->newline(-1) << "}";
338 }
339
340 // We mustn't release the context until after all _stp_error(), so dyninst
341 // mode can still access the log buffers stored therein.
342 s.op->newline() << "_stp_runtime_entryfn_put_context(c);";
343
344 s.op->newline() << "#if !INTERRUPTIBLE";
345 s.op->newline() << "local_irq_restore (flags);";
346 s.op->newline() << "#endif";
347
348 if (s.runtime_usermode_p())
349 {
350 s.op->newline() << "errno = _stp_saved_errno;";
351 }
352
353 s.op->newline() << "#endif // STP_ALIBI";
354
355 if (s.runtime_usermode_p())
356 s.op->newline(-1) << "}";
357 }
358
359
360 // ------------------------------------------------------------------------
361
362 // ------------------------------------------------------------------------
363 // Dwarf derived probes. "We apologize for the inconvience."
364 // ------------------------------------------------------------------------
365
366 static const string TOK_KERNEL("kernel");
367 static const string TOK_MODULE("module");
368 static const string TOK_FUNCTION("function");
369 static const string TOK_INLINE("inline");
370 static const string TOK_CALL("call");
371 static const string TOK_EXPORTED("exported");
372 static const string TOK_RETURN("return");
373 static const string TOK_MAXACTIVE("maxactive");
374 static const string TOK_STATEMENT("statement");
375 static const string TOK_ABSOLUTE("absolute");
376 static const string TOK_PROCESS("process");
377 static const string TOK_PROVIDER("provider");
378 static const string TOK_MARK("mark");
379 static const string TOK_TRACE("trace");
380 static const string TOK_LABEL("label");
381 static const string TOK_LIBRARY("library");
382 static const string TOK_PLT("plt");
383 static const string TOK_METHOD("method");
384 static const string TOK_CLASS("class");;
385 static const string TOK_CALLEE("callee");;
386 static const string TOK_CALLEES("callees");;
387 static const string TOK_NEAREST("nearest");;
388
389 // Can we handle this query with just symbol-table info?
390 enum dbinfo_reqt
391 {
392 dbr_unknown,
393 dbr_none, // kernel.statement(NUM).absolute
394 dbr_need_symtab, // can get by with symbol table if there's no dwarf
395 dbr_need_dwarf
396 };
397
398
399 struct dwarf_query; // forward decl
400
401 static int query_cu (Dwarf_Die * cudie, dwarf_query *q);
402 static void query_addr(Dwarf_Addr addr, dwarf_query *q);
403 static void query_plt_statement(dwarf_query *q);
404
405 struct
406 symbol_table
407 {
408 module_info *mod_info; // associated module
409 unordered_multimap<interned_string, func_info*> map_by_name;
410 multimap<Dwarf_Addr, func_info*> map_by_addr;
411 unordered_map<interned_string, Dwarf_Addr> globals;
412 unordered_map<interned_string, Dwarf_Addr> locals;
413 typedef multimap<Dwarf_Addr, func_info*>::iterator iterator_t;
414 typedef pair<iterator_t, iterator_t> range_t;
415 // Section describing function descriptors.
416 // Set to SHN_UNDEF if there is no such section.
417 GElf_Word opd_section;
418 void add_symbol(interned_string name, bool weak, bool descriptor,
419 Dwarf_Addr addr, Dwarf_Addr entrypc);
420 enum info_status get_from_elf();
421 void prepare_section_rejection(Dwfl_Module *mod);
422 bool reject_section(GElf_Word section);
423 void purge_syscall_stubs();
424 set <func_info*> lookup_symbol(interned_string name);
425 set <Dwarf_Addr> lookup_symbol_address(interned_string name);
426 func_info *get_func_containing_address(Dwarf_Addr addr);
427 func_info *get_first_func();
428
429 symbol_table(module_info *mi) : mod_info(mi), opd_section(SHN_UNDEF) {}
430 ~symbol_table();
431 };
432
433 static bool null_die(Dwarf_Die *die)
434 {
435 static Dwarf_Die null;
436 return (!die || !memcmp(die, &null, sizeof(null)));
437 }
438
439 struct exp_type_dwarf : public exp_type_details
440 {
441 // NB: We don't own this dwflpp, so don't use it after build_no_more!
442 // A shared_ptr might help, but expressions are currently so leaky
443 // that we'd probably never clear all references... :/
444 dwflpp* dw;
445 Dwarf_Die die;
446 bool userspace_p;
447 bool is_pointer;
448 exp_type_dwarf(dwflpp* dw, Dwarf_Die* die, bool userspace_p, bool addressof);
449 uintptr_t id () const { return reinterpret_cast<uintptr_t>(die.addr); }
450 bool expandable() const { return true; }
451 functioncall *expand(autocast_op* e, bool lvalue);
452 };
453
454
455 enum
456 function_spec_type
457 {
458 function_alone,
459 function_and_file,
460 function_file_and_line
461 };
462
463
464 struct dwarf_builder;
465 struct dwarf_var_expanding_visitor;
466
467
468 // XXX: This class is a candidate for subclassing to separate
469 // the relocation vs non-relocation variants. Likewise for
470 // kprobe vs kretprobe variants.
471
472 struct dwarf_derived_probe: public derived_probe
473 {
474 dwarf_derived_probe (interned_string function,
475 interned_string filename,
476 int line,
477 interned_string module,
478 interned_string section,
479 Dwarf_Addr dwfl_addr,
480 Dwarf_Addr addr,
481 dwarf_query & q,
482 Dwarf_Die* scope_die);
483
484 interned_string module;
485 interned_string section;
486 Dwarf_Addr addr;
487 interned_string path;
488 bool has_process;
489 bool has_return;
490 bool has_maxactive;
491 bool has_library;
492 int64_t maxactive_val;
493 // dwarf_derived_probe_group::emit_module_decls uses this to emit sdt kprobe definition
494 interned_string user_path;
495 interned_string user_lib;
496 bool access_vars;
497
498 unsigned saved_longs, saved_strings;
499 dwarf_derived_probe* entry_handler;
500
501 void printsig (std::ostream &o) const;
502 virtual void join_group (systemtap_session& s);
503 void emit_probe_local_init(systemtap_session& s, translator_output * o);
504 void getargs(std::list<std::string> &arg_set) const;
505
506 void emit_privilege_assertion (translator_output*);
507 void print_dupe_stamp(ostream& o);
508
509 // Pattern registration helpers.
510 static void register_statement_variants(match_node * root,
511 dwarf_builder * dw,
512 privilege_t privilege);
513 static void register_function_variants(match_node * root,
514 dwarf_builder * dw,
515 privilege_t privilege);
516 static void register_function_and_statement_variants(systemtap_session& s,
517 match_node * root,
518 dwarf_builder * dw,
519 privilege_t privilege);
520 static void register_sdt_variants(systemtap_session& s,
521 match_node * root,
522 dwarf_builder * dw);
523 static void register_plt_variants(systemtap_session& s,
524 match_node * root,
525 dwarf_builder * dw);
526 static void register_patterns(systemtap_session& s);
527
528 protected:
529 dwarf_derived_probe(probe *base,
530 probe_point *location,
531 Dwarf_Addr addr,
532 bool has_return):
533 derived_probe(base, location), addr(addr), has_process(0),
534 has_return(has_return), has_maxactive(0), has_library(0),
535 maxactive_val(0), access_vars(false), saved_longs(0),
536 saved_strings(0), entry_handler(0)
537 {}
538
539 private:
540 list<string> args;
541 void saveargs(dwarf_query& q, Dwarf_Die* scope_die, Dwarf_Addr dwfl_addr);
542 };
543
544
545 struct uprobe_derived_probe: public dwarf_derived_probe
546 {
547 int pid; // 0 => unrestricted
548
549 uprobe_derived_probe (interned_string function,
550 interned_string filename,
551 int line,
552 interned_string module,
553 interned_string section,
554 Dwarf_Addr dwfl_addr,
555 Dwarf_Addr addr,
556 dwarf_query & q,
557 Dwarf_Die* scope_die);
558
559 // alternate constructor for process(PID).statement(ADDR).absolute
560 uprobe_derived_probe (probe *base,
561 probe_point *location,
562 int pid,
563 Dwarf_Addr addr,
564 bool has_return):
565 dwarf_derived_probe(base, location, addr, has_return), pid(pid)
566 {}
567
568 void join_group (systemtap_session& s);
569
570 void emit_privilege_assertion (translator_output*);
571 void print_dupe_stamp(ostream& o) { print_dupe_stamp_unprivileged_process_owner (o); }
572 void getargs(std::list<std::string> &arg_set) const;
573 void saveargs(int nargs);
574 private:
575 list<string> args;
576 };
577
578 struct dwarf_derived_probe_group: public derived_probe_group
579 {
580 private:
581 unordered_multimap<interned_string,dwarf_derived_probe*> probes_by_module;
582 typedef unordered_multimap<interned_string,dwarf_derived_probe*>::iterator p_b_m_iterator;
583
584 public:
585 dwarf_derived_probe_group() {}
586 void enroll (dwarf_derived_probe* probe);
587 void emit_module_decls (systemtap_session& s);
588 void emit_module_init (systemtap_session& s);
589 void emit_module_refresh (systemtap_session& s);
590 void emit_module_exit (systemtap_session& s);
591 bool otf_supported (systemtap_session& s) { return true; }
592
593 // workqueue handling not safe in kprobes context
594 bool otf_safe_context (systemtap_session& s) { return false; }
595 };
596
597 // Helper struct to thread through the dwfl callbacks.
598 struct base_query
599 {
600 base_query(dwflpp & dw, literal_map_t const & params);
601 base_query(dwflpp & dw, interned_string module_val);
602 virtual ~base_query() {}
603
604 systemtap_session & sess;
605 dwflpp & dw;
606
607 // Used to keep track of which modules were visited during
608 // iterate_over_modules()
609 set<string> visited_modules;
610
611 // Parameter extractors.
612 static bool has_null_param(literal_map_t const & params,
613 interned_string k);
614 static bool get_string_param(literal_map_t const & params,
615 interned_string k, interned_string &v);
616 static bool get_number_param(literal_map_t const & params,
617 interned_string k, int64_t & v);
618 static bool get_number_param(literal_map_t const & params,
619 interned_string k, Dwarf_Addr & v);
620 static void query_library_callback (base_query *me, const char *data);
621 static void query_plt_callback (base_query *me, const char *link, size_t addr);
622 virtual void query_library (const char *data) = 0;
623 virtual void query_plt (const char *link, size_t addr) = 0;
624
625
626 // Extracted parameters.
627 bool has_kernel;
628 bool has_module;
629 bool has_process;
630 bool has_library;
631 bool has_plt;
632 bool has_statement;
633 interned_string module_val; // has_kernel => module_val = "kernel"
634 interned_string path; // executable path if module is a .so
635 interned_string plt_val; // has_plt => plt wildcard
636 int64_t pid_val;
637
638 virtual void handle_query_module() = 0;
639 };
640
641 base_query::base_query(dwflpp & dw, literal_map_t const & params):
642 sess(dw.sess), dw(dw),
643 has_kernel(false), has_module(false), has_process(false),
644 has_library(false), has_plt(false), has_statement(false),
645 pid_val(0)
646 {
647 has_kernel = has_null_param (params, TOK_KERNEL);
648 if (has_kernel)
649 module_val = "kernel";
650
651 has_module = get_string_param (params, TOK_MODULE, module_val);
652 if (has_module)
653 has_process = false;
654 else
655 {
656 interned_string library_name;
657 Dwarf_Addr statement_num_val;
658 has_process = derived_probe_builder::has_param(params, TOK_PROCESS);
659 has_library = get_string_param (params, TOK_LIBRARY, library_name);
660 if ((has_plt = has_null_param (params, TOK_PLT)))
661 plt_val = "*";
662 else has_plt = get_string_param (params, TOK_PLT, plt_val);
663 has_statement = get_number_param(params, TOK_STATEMENT, statement_num_val);
664
665 if (has_process)
666 {
667 if (get_number_param(params, TOK_PROCESS, pid_val))
668 {
669 // check that the pid given corresponds to a running process
670 string pid_err_msg;
671 if (!is_valid_pid(pid_val, pid_err_msg))
672 throw SEMANTIC_ERROR(pid_err_msg);
673
674 string pid_path = string("/proc/") + lex_cast(pid_val) + "/exe";
675 module_val = sess.sysroot + pid_path;
676 }
677 else
678 {
679 // reset the pid_val in case anything weird got written into it
680 pid_val = 0;
681 get_string_param(params, TOK_PROCESS, module_val);
682 }
683 module_val = find_executable (module_val, sess.sysroot, sess.sysenv);
684 if (!is_fully_resolved(module_val, sess.sysroot, sess.sysenv))
685 throw SEMANTIC_ERROR(_F("cannot find executable '%s'",
686 module_val.to_string().c_str()));
687 }
688
689 // Library probe? Let's target that instead if it is fully resolved (such
690 // as what query_one_library() would have done for us). Otherwise, we
691 // resort to iterate_over_libraries().
692 if (has_library && is_fully_resolved(library_name, sess.sysroot, sess.sysenv,
693 "LD_LIBRARY_PATH"))
694 {
695 path = path_remove_sysroot(sess, module_val);
696 module_val = library_name;
697 }
698 }
699
700 assert (has_kernel || has_process || has_module);
701 }
702
703 base_query::base_query(dwflpp & dw, interned_string module_val)
704 : sess(dw.sess), dw(dw),
705 has_kernel(false), has_module(false), has_process(false),
706 has_library(false), has_plt(false), has_statement(false),
707 module_val(module_val), pid_val(0)
708 {
709 // NB: This uses '/' to distinguish between kernel modules and userspace,
710 // which means that userspace modules won't get any PATH searching.
711 if (module_val.find('/') == string::npos)
712 {
713 has_kernel = (module_val == TOK_KERNEL);
714 has_module = !has_kernel;
715 has_process = false;
716 }
717 else
718 {
719 has_kernel = has_module = false;
720 has_process = true;
721 }
722 }
723
724 bool
725 base_query::has_null_param(literal_map_t const & params,
726 interned_string k)
727 {
728 return derived_probe_builder::has_null_param(params, k);
729 }
730
731
732 bool
733 base_query::get_string_param(literal_map_t const & params,
734 interned_string k, interned_string & v)
735 {
736 return derived_probe_builder::get_param (params, k, v);
737 }
738
739
740 bool
741 base_query::get_number_param(literal_map_t const & params,
742 interned_string k, int64_t & v)
743 {
744 return derived_probe_builder::get_param (params, k, v);
745 }
746
747
748 bool
749 base_query::get_number_param(literal_map_t const & params,
750 interned_string k, Dwarf_Addr & v)
751 {
752 int64_t value = 0;
753 bool present = derived_probe_builder::get_param (params, k, value);
754 if (present)
755 v = (Dwarf_Addr) value;
756 return present;
757 }
758
759 struct dwarf_query : public base_query
760 {
761 dwarf_query(probe * base_probe,
762 probe_point * base_loc,
763 dwflpp & dw,
764 literal_map_t const & params,
765 vector<derived_probe *> & results,
766 interned_string user_path,
767 interned_string user_lib);
768
769 vector<derived_probe *> & results;
770 set<interned_string> inlined_non_returnable; // function names
771 probe * base_probe;
772 probe_point * base_loc;
773 interned_string user_path;
774 interned_string user_lib;
775
776 set<string> visited_libraries;
777 bool resolved_library;
778
779 virtual void handle_query_module();
780 void query_module_dwarf();
781 void query_module_symtab();
782 void query_library (const char *data);
783 void query_plt (const char *entry, size_t addr);
784
785 void add_probe_point(interned_string funcname,
786 interned_string filename,
787 int line,
788 Dwarf_Die *scope_die,
789 Dwarf_Addr addr);
790
791 void mount_well_formed_probe_point();
792 void unmount_well_formed_probe_point();
793 stack<pair<probe_point*, probe*> > previous_bases;
794
795 void replace_probe_point_component_arg(interned_string functor,
796 interned_string new_functor,
797 int64_t new_arg,
798 bool hex = false);
799 void replace_probe_point_component_arg(interned_string functor,
800 int64_t new_arg,
801 bool hex = false);
802 void replace_probe_point_component_arg(interned_string functor,
803 interned_string new_functor,
804 interned_string new_arg);
805 void replace_probe_point_component_arg(interned_string functor,
806 interned_string new_arg);
807 void remove_probe_point_component(interned_string functor);
808
809 // Track addresses we've already seen in a given module
810 set<Dwarf_Addr> alias_dupes;
811
812 // Track inlines we've already seen as well
813 // NB: this can't be compared just by entrypc, as inlines can overlap
814 set<inline_instance_info> inline_dupes;
815
816 // Used in .callee[s] probes, when calling iterate_over_callees() (which
817 // provides the actual stack). Retains the addrs of the callers unwind addr
818 // where the callee is found. Specifies multiple callers. E.g. when a callee
819 // at depth 2 is found, callers[1] has the addr of the caller, and callers[0]
820 // has the addr of the caller's caller.
821 stack<Dwarf_Addr> *callers;
822
823 bool has_function_str;
824 bool has_statement_str;
825 bool has_function_num;
826 bool has_statement_num;
827 interned_string statement_str_val;
828 interned_string function_str_val;
829 Dwarf_Addr statement_num_val;
830 Dwarf_Addr function_num_val;
831
832 bool has_call;
833 bool has_exported;
834 bool has_inline;
835 bool has_return;
836
837 bool has_nearest;
838
839 bool has_maxactive;
840 int64_t maxactive_val;
841
842 bool has_label;
843 interned_string label_val;
844
845 bool has_callee;
846 interned_string callee_val;
847
848 bool has_callees_num;
849 int64_t callees_num_val;
850
851 bool has_absolute;
852
853 bool has_mark;
854
855 enum dbinfo_reqt dbinfo_reqt;
856 enum dbinfo_reqt assess_dbinfo_reqt();
857
858 void parse_function_spec(const string & spec);
859 function_spec_type spec_type;
860 vector<string> scopes;
861 interned_string function;
862 interned_string file;
863 lineno_t lineno_type;
864 vector<int> linenos;
865 bool query_done; // Found exact match
866
867 // Holds the prologue end of the current function
868 Dwarf_Addr prologue_end;
869
870 set<string> filtered_srcfiles;
871
872 // Map official entrypc -> func_info object
873 inline_instance_map_t filtered_inlines;
874 func_info_map_t filtered_functions;
875
876 // Helper when we want to iterate over both
877 base_func_info_map_t filtered_all();
878
879 void query_module_functions ();
880
881 interned_string final_function_name(interned_string final_func,
882 interned_string final_file,
883 int final_line);
884
885 bool is_fully_specified_function();
886 };
887
888
889 uprobe_derived_probe::uprobe_derived_probe (interned_string function,
890 interned_string filename,
891 int line,
892 interned_string module,
893 interned_string section,
894 Dwarf_Addr dwfl_addr,
895 Dwarf_Addr addr,
896 dwarf_query & q,
897 Dwarf_Die* scope_die):
898 dwarf_derived_probe(function, filename, line, module, section,
899 dwfl_addr, addr, q, scope_die), pid(q.pid_val)
900 {}
901
902
903 static void delete_session_module_cache (systemtap_session& s); // forward decl
904
905 struct dwarf_builder: public derived_probe_builder
906 {
907 map <string,dwflpp*> kern_dw; /* NB: key string could be a wildcard */
908 map <string,dwflpp*> user_dw;
909 interned_string user_path;
910 interned_string user_lib;
911
912 // Holds modules to suggest functions from. NB: aggregates over
913 // recursive calls to build() when deriving globby probes.
914 set <string> modules_seen;
915
916 dwarf_builder() {}
917
918 dwflpp *get_kern_dw(systemtap_session& sess, const string& module)
919 {
920 if (kern_dw[module] == 0)
921 kern_dw[module] = new dwflpp(sess, module, true); // might throw
922 return kern_dw[module];
923 }
924
925 dwflpp *get_user_dw(systemtap_session& sess, const string& module)
926 {
927 if (user_dw[module] == 0)
928 user_dw[module] = new dwflpp(sess, module, false); // might throw
929 return user_dw[module];
930 }
931
932 /* NB: not virtual, so can be called from dtor too: */
933 void dwarf_build_no_more (bool)
934 {
935 delete_map(kern_dw);
936 delete_map(user_dw);
937 }
938
939 void build_no_more (systemtap_session &s)
940 {
941 dwarf_build_no_more (s.verbose > 3);
942 delete_session_module_cache (s);
943 }
944
945 ~dwarf_builder()
946 {
947 dwarf_build_no_more (false);
948 }
949
950 virtual void build(systemtap_session & sess,
951 probe * base,
952 probe_point * location,
953 literal_map_t const & parameters,
954 vector<derived_probe *> & finished_results);
955 };
956
957
958 dwarf_query::dwarf_query(probe * base_probe,
959 probe_point * base_loc,
960 dwflpp & dw,
961 literal_map_t const & params,
962 vector<derived_probe *> & results,
963 interned_string user_path,
964 interned_string user_lib)
965 : base_query(dw, params), results(results), base_probe(base_probe),
966 base_loc(base_loc), user_path(user_path), user_lib(user_lib),
967 resolved_library(false), callers(NULL),
968 has_function_str(false), has_statement_str(false),
969 has_function_num(false), has_statement_num(false),
970 statement_num_val(0), function_num_val(0),
971 has_call(false), has_exported(false), has_inline(false),
972 has_return(false), has_nearest(false),
973 has_maxactive(false), maxactive_val(0),
974 has_label(false), has_callee(false),
975 has_callees_num(false), callees_num_val(0),
976 has_absolute(false), has_mark(false),
977 dbinfo_reqt(dbr_unknown),
978 spec_type(function_alone),
979 lineno_type(ABSOLUTE),
980 query_done(false), prologue_end(0)
981 {
982 // Reduce the query to more reasonable semantic values (booleans,
983 // extracted strings, numbers, etc).
984 has_function_str = get_string_param(params, TOK_FUNCTION, function_str_val);
985 has_function_num = get_number_param(params, TOK_FUNCTION, function_num_val);
986
987 has_statement_str = get_string_param(params, TOK_STATEMENT, statement_str_val);
988 has_statement_num = get_number_param(params, TOK_STATEMENT, statement_num_val);
989
990 has_label = get_string_param(params, TOK_LABEL, label_val);
991 has_callee = get_string_param(params, TOK_CALLEE, callee_val);
992 if (has_null_param(params, TOK_CALLEES))
993 { // .callees ==> .callees(1) (also equivalent to .callee("*"))
994 has_callees_num = true;
995 callees_num_val = 1;
996 }
997 else
998 {
999 has_callees_num = get_number_param(params, TOK_CALLEES, callees_num_val);
1000 if (has_callees_num && callees_num_val < 1)
1001 throw SEMANTIC_ERROR(_(".callees(N) only acceptable for N >= 1"),
1002 base_probe->tok);
1003 }
1004
1005 has_call = has_null_param(params, TOK_CALL);
1006 has_exported = has_null_param(params, TOK_EXPORTED);
1007 has_inline = has_null_param(params, TOK_INLINE);
1008 has_return = has_null_param(params, TOK_RETURN);
1009 has_nearest = has_null_param(params, TOK_NEAREST);
1010 has_maxactive = get_number_param(params, TOK_MAXACTIVE, maxactive_val);
1011 has_absolute = has_null_param(params, TOK_ABSOLUTE);
1012 has_mark = false;
1013
1014 if (has_function_str)
1015 parse_function_spec(function_str_val);
1016 else if (has_statement_str)
1017 parse_function_spec(statement_str_val);
1018
1019 dbinfo_reqt = assess_dbinfo_reqt();
1020 query_done = false;
1021 }
1022
1023
1024 void
1025 dwarf_query::query_module_dwarf()
1026 {
1027 if (has_function_num || has_statement_num)
1028 {
1029 // If we have module("foo").function(0xbeef) or
1030 // module("foo").statement(0xbeef), the address is relative
1031 // to the start of the module, so we seek the function
1032 // number plus the module's bias.
1033 Dwarf_Addr addr = has_function_num ?
1034 function_num_val : statement_num_val;
1035
1036 // These are raw addresses, we need to know what the elf_bias
1037 // is to feed it to libdwfl based functions.
1038 Dwarf_Addr elf_bias;
1039 Elf *elf = dwfl_module_getelf (dw.module, &elf_bias);
1040 assert(elf);
1041 addr += elf_bias;
1042 query_addr(addr, this);
1043 }
1044 else
1045 {
1046 // Otherwise if we have a function("foo") or statement("foo")
1047 // specifier, we have to scan over all the CUs looking for
1048 // the function(s) in question
1049 assert(has_function_str || has_statement_str);
1050
1051 // For simple cases, no wildcard and no source:line, we can do a very
1052 // quick function lookup in a module-wide cache.
1053 if (spec_type == function_alone &&
1054 !dw.name_has_wildcard(function) &&
1055 !startswith(function, "_Z"))
1056 query_module_functions();
1057 else
1058 dw.iterate_over_cus(&query_cu, this, false);
1059 }
1060 }
1061
1062 static void query_func_info (Dwarf_Addr entrypc, func_info & fi,
1063 dwarf_query * q);
1064
1065 static void
1066 query_symtab_func_info (func_info & fi, dwarf_query * q)
1067 {
1068 assert(null_die(&fi.die));
1069
1070 Dwarf_Addr entrypc = fi.entrypc;
1071
1072 // Now compensate for the dw bias because the addresses come
1073 // from dwfl_module_symtab, so fi->entrypc is NOT a normal dw address.
1074 q->dw.get_module_dwarf(false, false);
1075 entrypc -= q->dw.module_bias;
1076
1077 // If there are already probes in this module, lets not duplicate.
1078 // This can come from other weak symbols/aliases or existing
1079 // matches from Dwarf DIE functions. Try to add this entrypc to the
1080 // collection, and only continue if it was new.
1081 if (q->alias_dupes.insert(entrypc).second)
1082 query_func_info(entrypc, fi, q);
1083 }
1084
1085 void
1086 dwarf_query::query_module_symtab()
1087 {
1088 // Get the symbol table if it's necessary, sufficient, and not already got.
1089 if (dbinfo_reqt == dbr_need_dwarf)
1090 return;
1091
1092 module_info *mi = dw.mod_info;
1093 if (dbinfo_reqt == dbr_need_symtab)
1094 {
1095 if (mi->symtab_status == info_unknown)
1096 mi->get_symtab();
1097 if (mi->symtab_status == info_absent)
1098 return;
1099 }
1100
1101 func_info *fi = NULL;
1102 symbol_table *sym_table = mi->sym_table;
1103
1104 if (has_function_str)
1105 {
1106 // Per dwarf_query::assess_dbinfo_reqt()...
1107 assert(spec_type == function_alone);
1108 if (dw.name_has_wildcard(function_str_val))
1109 {
1110 symbol_table::iterator_t iter;
1111 for (iter = sym_table->map_by_addr.begin();
1112 iter != sym_table->map_by_addr.end();
1113 ++iter)
1114 {
1115 fi = iter->second;
1116 if (!null_die(&fi->die) // already handled in query_module_dwarf()
1117 || fi->descriptor) // ppc opd (and also undefined symbols)
1118 continue;
1119 if (dw.function_name_matches_pattern(fi->name, function_str_val))
1120 query_symtab_func_info(*fi, this);
1121 }
1122 }
1123 else
1124 {
1125 set<func_info*> fis = sym_table->lookup_symbol(function_str_val);
1126 for (set<func_info*>::iterator it=fis.begin(); it!=fis.end(); ++it)
1127 {
1128 fi = *it;
1129 if (fi && null_die(&fi->die))
1130 query_symtab_func_info(*fi, this);
1131 }
1132 }
1133 }
1134 }
1135
1136 void
1137 dwarf_query::handle_query_module()
1138 {
1139 if (has_plt && has_statement_num)
1140 {
1141 query_plt_statement (this);
1142 return;
1143 }
1144
1145 bool report = dbinfo_reqt == dbr_need_dwarf;
1146 dw.get_module_dwarf(false, report);
1147
1148 // prebuild the symbol table to resolve aliases
1149 dw.mod_info->get_symtab();
1150
1151 // reset the dupe-checking for each new module
1152 alias_dupes.clear();
1153 inline_dupes.clear();
1154
1155 if (dw.mod_info->dwarf_status == info_present)
1156 query_module_dwarf();
1157
1158 // Consult the symbol table, asm and weak functions can show up
1159 // in the symbol table but not in dwarf and minidebuginfo is
1160 // located in the gnu_debugdata section, alias_dupes checking
1161 // is done before adding any probe points
1162 if (!query_done && !pending_interrupts)
1163 query_module_symtab();
1164 }
1165
1166
1167 void
1168 dwarf_query::parse_function_spec(const string & spec)
1169 {
1170 lineno_type = ABSOLUTE;
1171 size_t src_pos, line_pos, scope_pos;
1172
1173 // look for named scopes
1174 scope_pos = spec.rfind("::");
1175 if (scope_pos != string::npos)
1176 {
1177 tokenize_cxx(spec.substr(0, scope_pos), scopes);
1178 scope_pos += 2;
1179 }
1180 else
1181 scope_pos = 0;
1182
1183 // look for a source separator
1184 src_pos = spec.find('@', scope_pos);
1185 if (src_pos == string::npos)
1186 {
1187 function = spec.substr(scope_pos);
1188 spec_type = function_alone;
1189 }
1190 else
1191 {
1192 function = spec.substr(scope_pos, src_pos - scope_pos);
1193
1194 // look for a line-number separator
1195 line_pos = spec.find_first_of(":+", src_pos);
1196 if (line_pos == string::npos)
1197 {
1198 file = spec.substr(src_pos + 1);
1199 spec_type = function_and_file;
1200 }
1201 else
1202 {
1203 file = spec.substr(src_pos + 1, line_pos - src_pos - 1);
1204
1205 // classify the line spec
1206 spec_type = function_file_and_line;
1207 if (spec[line_pos] == '+')
1208 lineno_type = RELATIVE;
1209 else if (spec[line_pos + 1] == '*' &&
1210 spec.length() == line_pos + 2)
1211 lineno_type = WILDCARD;
1212 else
1213 lineno_type = ABSOLUTE;
1214
1215 if (lineno_type != WILDCARD)
1216 try
1217 {
1218 // try to parse N, N-M, or N,M,O,P, or combination thereof...
1219 if (spec.find_first_of(",-", line_pos + 1) != string::npos)
1220 {
1221 lineno_type = ENUMERATED;
1222 vector<string> sub_specs;
1223 tokenize(spec.substr(line_pos + 1), sub_specs, ",");
1224 vector<string>::const_iterator line_spec;
1225 for (line_spec = sub_specs.begin(); line_spec != sub_specs.end(); ++line_spec)
1226 {
1227 vector<string> ranges;
1228 tokenize(*line_spec, ranges, "-");
1229 if (ranges.size() > 1)
1230 {
1231 int low = lex_cast<int>(ranges.front());
1232 int high = lex_cast<int>(ranges.back());
1233 for (int i = low; i <= high; i++)
1234 linenos.push_back(i);
1235 }
1236 else
1237 linenos.push_back(lex_cast<int>(ranges.at(0)));
1238 }
1239 sort(linenos.begin(), linenos.end());
1240 }
1241 else
1242 {
1243 linenos.push_back(lex_cast<int>(spec.substr(line_pos + 1)));
1244 linenos.push_back(lex_cast<int>(spec.substr(line_pos + 1)));
1245 }
1246 }
1247 catch (runtime_error & exn)
1248 {
1249 goto bad;
1250 }
1251 }
1252 }
1253
1254 if (function.empty() ||
1255 (spec_type != function_alone && file.empty()))
1256 goto bad;
1257
1258 if (sess.verbose > 2)
1259 {
1260 //clog << "parsed '" << spec << "'";
1261 clog << _F("parse '%s'", spec.c_str());
1262
1263 if (!scopes.empty())
1264 clog << ", scope '" << scopes[0] << "'";
1265 for (unsigned i = 1; i < scopes.size(); ++i)
1266 clog << "::'" << scopes[i] << "'";
1267
1268 clog << ", func '" << function << "'";
1269
1270 if (spec_type != function_alone)
1271 clog << ", file '" << file << "'";
1272
1273 if (spec_type == function_file_and_line)
1274 {
1275 clog << ", line ";
1276 switch (lineno_type)
1277 {
1278 case ABSOLUTE:
1279 clog << linenos[0];
1280 break;
1281
1282 case RELATIVE:
1283 clog << "+" << linenos[0];
1284 break;
1285
1286 case ENUMERATED:
1287 {
1288 vector<int>::const_iterator linenos_it;
1289 for (linenos_it = linenos.begin(); linenos_it != linenos.end(); ++linenos_it)
1290 {
1291 vector<int>::const_iterator range_it(linenos_it);
1292 while ((range_it+1) != linenos.end() && *range_it + 1 == *(range_it+1))
1293 ++range_it;
1294 if (linenos_it == range_it)
1295 clog << *linenos_it;
1296 else
1297 clog << *linenos_it << "-" << *range_it;
1298 if (range_it + 1 != linenos.end())
1299 clog << ",";
1300 linenos_it = range_it;
1301 }
1302 }
1303 break;
1304
1305 case WILDCARD:
1306 clog << "*";
1307 break;
1308 }
1309 }
1310
1311 clog << endl;
1312 }
1313
1314 return;
1315
1316 bad:
1317 throw SEMANTIC_ERROR(_F("malformed specification '%s'", spec.c_str()),
1318 base_probe->tok);
1319 }
1320
1321 string path_remove_sysroot(const systemtap_session& sess, const string& path)
1322 {
1323 size_t pos;
1324 string retval = path;
1325 if (!sess.sysroot.empty() &&
1326 (pos = retval.find(sess.sysroot)) != string::npos)
1327 retval.replace(pos, sess.sysroot.length(), "/");
1328 return retval;
1329 }
1330
1331 void
1332 dwarf_query::add_probe_point(interned_string dw_funcname,
1333 interned_string filename,
1334 int line,
1335 Dwarf_Die* scope_die,
1336 Dwarf_Addr addr)
1337 {
1338 interned_string reloc_section; // base section for relocation purposes
1339 Dwarf_Addr reloc_addr; // relocated
1340 interned_string module = dw.module_name; // "kernel" or other
1341 interned_string funcname = dw_funcname;
1342
1343 assert (! has_absolute); // already handled in dwarf_builder::build()
1344
1345 reloc_addr = dw.relocate_address(addr, reloc_section);
1346
1347 // If we originally used the linkage name, then let's call it that way
1348 const char* linkage_name;
1349 if (!null_die(scope_die) && startswith (this->function, "_Z")
1350 && (linkage_name = dwarf_linkage_name (scope_die)))
1351 funcname = linkage_name;
1352
1353 if (sess.verbose > 1)
1354 {
1355 clog << _("probe ") << funcname << "@" << filename << ":" << line;
1356 if (string(module) == TOK_KERNEL)
1357 clog << _(" kernel");
1358 else if (has_module)
1359 clog << _(" module=") << module;
1360 else if (has_process)
1361 clog << _(" process=") << module;
1362 if (reloc_section != "") clog << " reloc=" << reloc_section;
1363 clog << " pc=0x" << hex << addr << dec;
1364 }
1365
1366 dwflpp::blacklisted_type blacklisted = dw.blacklisted_p (funcname, filename,
1367 line, module, addr,
1368 has_return);
1369 if (sess.verbose > 1)
1370 clog << endl;
1371
1372 if (module == TOK_KERNEL)
1373 {
1374 // PR 4224: adapt to relocatable kernel by subtracting the _stext address here.
1375 reloc_addr = addr - sess.sym_stext;
1376 reloc_section = "_stext"; // a message to runtime's _stp_module_relocate
1377 }
1378
1379 if (!blacklisted)
1380 {
1381 sess.unwindsym_modules.insert (module);
1382
1383 if (has_process)
1384 {
1385 string module_tgt = path_remove_sysroot(sess, module);
1386 results.push_back (new uprobe_derived_probe(funcname, filename, line,
1387 module_tgt, reloc_section, addr, reloc_addr,
1388 *this, scope_die));
1389 }
1390 else
1391 {
1392 assert (has_kernel || has_module);
1393 results.push_back (new dwarf_derived_probe(funcname, filename, line,
1394 module, reloc_section, addr, reloc_addr,
1395 *this, scope_die));
1396 }
1397 }
1398 else
1399 {
1400 switch (blacklisted)
1401 {
1402 case dwflpp::blacklisted_section:
1403 sess.print_warning(_F("function %s is in blacklisted section",
1404 funcname.to_string().c_str()), base_probe->tok);
1405 break;
1406 case dwflpp::blacklisted_kprobes:
1407 sess.print_warning(_F("kprobes function %s is blacklisted",
1408 funcname.to_string().c_str()), base_probe->tok);
1409 break;
1410 case dwflpp::blacklisted_function_return:
1411 sess.print_warning(_F("function %s return probe is blacklisted",
1412 funcname.to_string().c_str()), base_probe->tok);
1413 break;
1414 case dwflpp::blacklisted_file:
1415 sess.print_warning(_F("function %s is in blacklisted file",
1416 funcname.to_string().c_str()), base_probe->tok);
1417 break;
1418 case dwflpp::blacklisted_function:
1419 default:
1420 sess.print_warning(_F("function %s is blacklisted",
1421 funcname.to_string().c_str()), base_probe->tok);
1422 break;
1423 }
1424 }
1425 }
1426
1427 void
1428 dwarf_query::mount_well_formed_probe_point()
1429 {
1430 interned_string module = dw.module_name;
1431 if (has_process)
1432 module = path_remove_sysroot(sess, module);
1433
1434 vector<probe_point::component*> comps;
1435 vector<probe_point::component*>::iterator it;
1436 for (it = base_loc->components.begin();
1437 it != base_loc->components.end(); ++it)
1438 {
1439 if ((*it)->functor == TOK_PROCESS || (*it)->functor == TOK_MODULE)
1440 comps.push_back(new probe_point::component((*it)->functor,
1441 new literal_string(has_library ? path : module)));
1442 else
1443 comps.push_back(*it);
1444 }
1445
1446 probe_point *pp = new probe_point(*base_loc);
1447 pp->well_formed = true;
1448 pp->components = comps;
1449
1450 previous_bases.push(make_pair(base_loc, base_probe));
1451
1452 base_loc = pp;
1453 base_probe = new probe(base_probe, pp);
1454 }
1455
1456 void
1457 dwarf_query::unmount_well_formed_probe_point()
1458 {
1459 assert(!previous_bases.empty());
1460
1461 base_loc = previous_bases.top().first;
1462 base_probe = previous_bases.top().second;
1463
1464 previous_bases.pop();
1465 }
1466
1467 void
1468 dwarf_query::replace_probe_point_component_arg(interned_string functor,
1469 interned_string new_functor,
1470 int64_t new_arg,
1471 bool hex)
1472 {
1473 // only allow these operations if we're editing the well-formed loc
1474 assert(!previous_bases.empty());
1475
1476 vector<probe_point::component*>::iterator it;
1477 for (it = base_loc->components.begin();
1478 it != base_loc->components.end(); ++it)
1479 if ((*it)->functor == functor)
1480 *it = new probe_point::component(new_functor,
1481 new literal_number(new_arg, hex));
1482 }
1483
1484 void
1485 dwarf_query::replace_probe_point_component_arg(interned_string functor,
1486 int64_t new_arg,
1487 bool hex)
1488 {
1489 replace_probe_point_component_arg(functor, functor, new_arg, hex);
1490 }
1491
1492 void
1493 dwarf_query::replace_probe_point_component_arg(interned_string functor,
1494 interned_string new_functor,
1495 interned_string new_arg)
1496 {
1497 // only allow these operations if we're editing the well-formed loc
1498 assert(!previous_bases.empty());
1499
1500 vector<probe_point::component*>::iterator it;
1501 for (it = base_loc->components.begin();
1502 it != base_loc->components.end(); ++it)
1503 if ((*it)->functor == functor)
1504 *it = new probe_point::component(new_functor,
1505 new literal_string(new_arg));
1506 }
1507
1508 void
1509 dwarf_query::replace_probe_point_component_arg(interned_string functor,
1510 interned_string new_arg)
1511 {
1512 replace_probe_point_component_arg(functor, functor, new_arg);
1513 }
1514
1515 void
1516 dwarf_query::remove_probe_point_component(interned_string functor)
1517 {
1518 // only allow these operations if we're editing the well-formed loc
1519 assert(!previous_bases.empty());
1520
1521 vector<probe_point::component*> new_comps;
1522 vector<probe_point::component*>::iterator it;
1523 for (it = base_loc->components.begin();
1524 it != base_loc->components.end(); ++it)
1525 if ((*it)->functor != functor)
1526 new_comps.push_back(*it);
1527
1528 base_loc->components = new_comps;
1529 }
1530
1531 enum dbinfo_reqt
1532 dwarf_query::assess_dbinfo_reqt()
1533 {
1534 if (has_absolute)
1535 {
1536 // kernel.statement(NUM).absolute
1537 return dbr_none;
1538 }
1539 if (has_inline || has_label || has_callee || has_callees_num)
1540 {
1541 // kernel.function("f").inline or module("m").function("f").inline
1542 return dbr_need_dwarf;
1543 }
1544 if (has_function_str && spec_type == function_alone)
1545 {
1546 // kernel.function("f") or module("m").function("f")
1547 return dbr_need_symtab;
1548 }
1549 if (has_statement_num)
1550 {
1551 // kernel.statement(NUM) or module("m").statement(NUM)
1552 // Technically, all we need is the module offset (or _stext, for
1553 // the kernel). But for that we need either the ELF file or (for
1554 // _stext) the symbol table. In either case, the symbol table
1555 // is available, and that allows us to map the NUM (address)
1556 // to a function, which is goodness.
1557 return dbr_need_symtab;
1558 }
1559 if (has_function_num)
1560 {
1561 // kernel.function(NUM) or module("m").function(NUM)
1562 // Need the symbol table so we can back up from NUM to the
1563 // start of the function.
1564 return dbr_need_symtab;
1565 }
1566 // Symbol table tells us nothing about source files or line numbers.
1567 return dbr_need_dwarf;
1568 }
1569
1570 interned_string
1571 dwarf_query::final_function_name(interned_string final_func,
1572 interned_string final_file,
1573 int final_line)
1574 {
1575 string final_name = final_func;
1576 if (final_file != "")
1577 {
1578 final_name += ("@" + string(final_file));
1579 if (final_line > 0)
1580 final_name += (":" + lex_cast(final_line));
1581 }
1582 return final_name;
1583 }
1584
1585 bool
1586 dwarf_query::is_fully_specified_function()
1587 {
1588 // A fully specified function is one that was given using a .function() probe
1589 // by full name (no wildcards), and specific srcfile and decl_line.
1590 return (has_function_str
1591 && spec_type == function_file_and_line
1592 && !dw.name_has_wildcard(function)
1593 && filtered_srcfiles.size() == 1
1594 && !filtered_functions.empty()
1595 && lineno_type == ABSOLUTE
1596 && filtered_functions[0].decl_line == linenos[0]);
1597 }
1598
1599 base_func_info_map_t
1600 dwarf_query::filtered_all(void)
1601 {
1602 base_func_info_map_t r;
1603 func_info_map_t::const_iterator f;
1604 for (f = filtered_functions.begin();
1605 f != filtered_functions.end(); ++f)
1606 r.push_back(*f);
1607 inline_instance_map_t::const_iterator i;
1608 for (i = filtered_inlines.begin();
1609 i != filtered_inlines.end(); ++i)
1610 r.push_back(*i);
1611 return r;
1612 }
1613
1614 // The critical determining factor when interpreting a pattern
1615 // string is, perhaps surprisingly: "presence of a lineno". The
1616 // presence of a lineno changes the search strategy completely.
1617 //
1618 // Compare the two cases:
1619 //
1620 // 1. {statement,function}(foo@file.c:lineno)
1621 // - find the files matching file.c
1622 // - in each file, find the functions matching foo
1623 // - query the file for line records matching lineno
1624 // - iterate over the line records,
1625 // - and iterate over the functions,
1626 // - if(haspc(function.DIE, line.addr))
1627 // - if looking for statements: probe(lineno.addr)
1628 // - if looking for functions: probe(function.{entrypc,return,etc.})
1629 //
1630 // 2. {statement,function}(foo@file.c)
1631 // - find the files matching file.c
1632 // - in each file, find the functions matching foo
1633 // - probe(function.{entrypc,return,etc.})
1634 //
1635 // Thus the first decision we make is based on the presence of a
1636 // lineno, and we enter entirely different sets of callbacks
1637 // depending on that decision.
1638 //
1639 // Note that the first case is a generalization fo the second, in that
1640 // we could theoretically search through line records for matching
1641 // file names (a "table scan" in rdbms lingo). Luckily, file names
1642 // are already cached elsewhere, so we can do an "index scan" as an
1643 // optimization.
1644
1645 static void
1646 query_statement (interned_string func,
1647 interned_string file,
1648 int line,
1649 Dwarf_Die *scope_die,
1650 Dwarf_Addr stmt_addr,
1651 dwarf_query * q)
1652 {
1653 try
1654 {
1655 q->add_probe_point(func, file,
1656 line, scope_die, stmt_addr);
1657 }
1658 catch (const semantic_error& e)
1659 {
1660 q->sess.print_error (e);
1661 }
1662 }
1663
1664 static void
1665 query_addr(Dwarf_Addr addr, dwarf_query *q)
1666 {
1667 assert(q->has_function_num || q->has_statement_num);
1668
1669 dwflpp &dw = q->dw;
1670
1671 if (q->sess.verbose > 2)
1672 clog << "query_addr 0x" << hex << addr << dec << endl;
1673
1674 // First pick which CU contains this address
1675 Dwarf_Die* cudie = dw.query_cu_containing_address(addr);
1676 if (!cudie) // address could be wildly out of range
1677 return;
1678 dw.focus_on_cu(cudie);
1679
1680 // Now compensate for the dw bias
1681 addr -= dw.module_bias;
1682
1683 // Per PR5787, we look up the scope die even for
1684 // statement_num's, for blacklist sensitivity and $var
1685 // resolution purposes.
1686
1687 // Find the scopes containing this address
1688 vector<Dwarf_Die> scopes = dw.getscopes(addr);
1689 if (scopes.empty())
1690 return;
1691
1692 // Look for the innermost containing function
1693 Dwarf_Die *fnscope = NULL;
1694 for (size_t i = 0; i < scopes.size(); ++i)
1695 {
1696 int tag = dwarf_tag(&scopes[i]);
1697 if ((tag == DW_TAG_subprogram && !q->has_inline) ||
1698 (tag == DW_TAG_inlined_subroutine &&
1699 !q->has_call && !q->has_return && !q->has_exported))
1700 {
1701 fnscope = &scopes[i];
1702 break;
1703 }
1704 }
1705 if (!fnscope)
1706 return;
1707 dw.focus_on_function(fnscope);
1708
1709 Dwarf_Die *scope = q->has_function_num ? fnscope : &scopes[0];
1710
1711 const char *file = dwarf_decl_file(fnscope) ?: "";
1712 int line;
1713 dwarf_decl_line(fnscope, &line);
1714
1715 // Function probes should reset the addr to the function entry
1716 // and possibly perform prologue searching
1717 if (q->has_function_num)
1718 {
1719 if (!dw.die_entrypc(fnscope, &addr))
1720 return;
1721 if (dwarf_tag(fnscope) == DW_TAG_subprogram &&
1722 q->sess.prologue_searching_mode != systemtap_session::prologue_searching_never &&
1723 (q->sess.prologue_searching_mode == systemtap_session::prologue_searching_always ||
1724 (q->has_process && !q->dw.has_valid_locs()))) // PR 6871 && PR 6941
1725 {
1726 func_info func;
1727 func.die = *fnscope;
1728 func.name = dw.function_name;
1729 func.decl_file = file;
1730 func.decl_line = line;
1731 func.entrypc = addr;
1732
1733 func_info_map_t funcs(1, func);
1734 dw.resolve_prologue_endings (funcs);
1735 q->prologue_end = funcs[0].prologue_end;
1736
1737 // PR13200: if it's a .return probe, we need to emit a *retprobe based
1738 // on the entrypc so here we only use prologue_end for non .return
1739 // probes (note however that .return probes still take advantage of
1740 // prologue_end: PR14436)
1741 if (!q->has_return)
1742 addr = funcs[0].prologue_end;
1743 }
1744 }
1745 else
1746 {
1747 Dwarf_Line *address_line = dwarf_getsrc_die(cudie, addr);
1748 Dwarf_Addr address_line_addr = addr;
1749 if (address_line)
1750 {
1751 file = DWARF_LINESRC(address_line);
1752 line = DWARF_LINENO(address_line);
1753 address_line_addr = DWARF_LINEADDR(address_line);
1754 }
1755
1756 // Verify that a raw address matches the beginning of a
1757 // statement. This is a somewhat lame check that the address
1758 // is at the start of an assembly instruction. Mark probes are in the
1759 // middle of a macro and thus not strictly at a statement beginning.
1760 // Guru mode may override this check.
1761 if (!q->has_mark && (!address_line || address_line_addr != addr))
1762 {
1763 stringstream msg;
1764 msg << _F("address %#" PRIx64 " does not match the beginning of a statement",
1765 addr);
1766 if (address_line)
1767 msg << _F(" (try %#" PRIx64 ")", address_line_addr);
1768 else
1769 msg << _F(" (no line info found for '%s', in module '%s')",
1770 dw.cu_name().c_str(), dw.module_name.c_str());
1771 if (! q->sess.guru_mode)
1772 throw SEMANTIC_ERROR(msg.str());
1773 else
1774 q->sess.print_warning(msg.str());
1775 }
1776 }
1777
1778 // We're ready to build a probe, but before, we need to create the final,
1779 // well-formed version of this location with all the components filled in
1780 q->mount_well_formed_probe_point();
1781 q->replace_probe_point_component_arg(TOK_FUNCTION, addr, true /* hex */ );
1782 q->replace_probe_point_component_arg(TOK_STATEMENT, addr, true /* hex */ );
1783
1784 // Build a probe at this point
1785 query_statement(dw.function_name, file, line, scope, addr, q);
1786
1787 q->unmount_well_formed_probe_point();
1788 }
1789
1790 static void
1791 query_plt_statement(dwarf_query *q)
1792 {
1793 assert (q->has_plt && q->has_statement_num);
1794
1795 Dwarf_Addr addr = q->statement_num_val;
1796 if (q->sess.verbose > 2)
1797 clog << "query_plt_statement 0x" << hex << addr << dec << endl;
1798
1799 // First adjust the raw address to dwfl's elf bias.
1800 Dwarf_Addr elf_bias;
1801 Elf *elf = dwfl_module_getelf (q->dw.module, &elf_bias);
1802 assert(elf);
1803 addr += elf_bias;
1804
1805 // Now compensate for the dw bias
1806 q->dw.get_module_dwarf(false, false);
1807 addr -= q->dw.module_bias;
1808
1809 // Create the final well-formed probe point
1810 q->mount_well_formed_probe_point();
1811 q->replace_probe_point_component_arg(TOK_STATEMENT, q->statement_num_val, true /* hex */ );
1812
1813 // We remove the .plt part here, since if the user provided a .plt probe, then
1814 // the higher-level probe point is already well-formed. On the other hand, if
1815 // the user provides a .plt(PATTERN).statement(0xABCD), the PATTERN is
1816 // irrelevant (we won't iterate over plts) so just take it out.
1817 q->remove_probe_point_component(TOK_PLT);
1818
1819 // Build a probe at this point
1820 query_statement(q->plt_val, NULL, -1, NULL, addr, q);
1821
1822 q->unmount_well_formed_probe_point();
1823 }
1824
1825 static void
1826 query_label (const base_func_info& func,
1827 char const * label,
1828 char const * file,
1829 int line,
1830 Dwarf_Die *scope_die,
1831 Dwarf_Addr stmt_addr,
1832 dwarf_query * q)
1833 {
1834 assert (q->has_statement_str || q->has_function_str);
1835
1836 // weed out functions whose decl_file isn't one of
1837 // the source files that we actually care about
1838 if (q->spec_type != function_alone &&
1839 q->filtered_srcfiles.count(file) == 0)
1840 return;
1841
1842 // Create the final well-formed probe
1843 interned_string canon_func = q->final_function_name(func.name, file ?: "", line);
1844
1845 q->mount_well_formed_probe_point();
1846 q->replace_probe_point_component_arg(TOK_FUNCTION, canon_func);
1847 q->replace_probe_point_component_arg(TOK_LABEL, label);
1848
1849 query_statement(func.name, file, line, scope_die, stmt_addr, q);
1850
1851 q->unmount_well_formed_probe_point();
1852 }
1853
1854 static void
1855 query_callee (base_func_info& callee,
1856 base_func_info& caller,
1857 stack<Dwarf_Addr> *callers,
1858 dwarf_query * q)
1859 {
1860 assert (q->has_function_str);
1861 assert (q->has_callee || q->has_callees_num);
1862
1863 // OK, we found a callee for a targeted caller. To help users see the
1864 // derivation, we add the well-formed form .function(caller).callee(callee).
1865
1866 interned_string canon_caller = q->final_function_name(caller.name, caller.decl_file,
1867 caller.decl_line);
1868 interned_string canon_callee = q->final_function_name(callee.name, callee.decl_file,
1869 callee.decl_line);
1870
1871 q->mount_well_formed_probe_point();
1872 q->replace_probe_point_component_arg(TOK_FUNCTION, canon_caller);
1873 q->replace_probe_point_component_arg(TOK_CALLEES, TOK_CALLEE, canon_callee);
1874 q->replace_probe_point_component_arg(TOK_CALLEE, canon_callee);
1875
1876 // Pass on the callers we'll need to add checks for
1877 q->callers = callers;
1878
1879 query_statement(callee.name, callee.decl_file,
1880 callee.decl_line,
1881 &callee.die, callee.entrypc, q);
1882
1883 q->unmount_well_formed_probe_point();
1884 }
1885
1886 static void
1887 query_inline_instance_info (inline_instance_info & ii,
1888 dwarf_query * q)
1889 {
1890 try
1891 {
1892 assert (! q->has_return); // checked by caller already
1893 assert (q->has_function_str || q->has_statement_str);
1894
1895 if (q->sess.verbose>2)
1896 clog << _F("querying entrypc %#" PRIx64 " of instance of inline '%s'\n",
1897 ii.entrypc, ii.name.to_string().c_str());
1898
1899 interned_string canon_func = q->final_function_name(ii.name, ii.decl_file,
1900 ii.decl_line);
1901
1902 q->mount_well_formed_probe_point();
1903 q->replace_probe_point_component_arg(TOK_FUNCTION, canon_func);
1904 q->replace_probe_point_component_arg(TOK_STATEMENT, canon_func);
1905
1906 query_statement (ii.name, ii.decl_file, ii.decl_line,
1907 &ii.die, ii.entrypc, q);
1908
1909 q->unmount_well_formed_probe_point();
1910 }
1911 catch (semantic_error &e)
1912 {
1913 q->sess.print_error (e);
1914 }
1915 }
1916
1917 static void
1918 query_func_info (Dwarf_Addr entrypc,
1919 func_info & fi,
1920 dwarf_query * q)
1921 {
1922 assert(q->has_function_str || q->has_statement_str);
1923
1924 try
1925 {
1926 interned_string canon_func = q->final_function_name(fi.name, fi.decl_file,
1927 fi.decl_line);
1928
1929 q->mount_well_formed_probe_point();
1930 q->replace_probe_point_component_arg(TOK_FUNCTION, canon_func);
1931 q->replace_probe_point_component_arg(TOK_STATEMENT, canon_func);
1932
1933 // If it's a .return probe, we need to emit a *retprobe based on the
1934 // entrypc (PR13200). Note however that if prologue_end is valid,
1935 // dwarf_derived_probe will still take advantage of it by creating a new
1936 // probe there if necessary to pick up target vars (PR14436).
1937 if (fi.prologue_end == 0 || q->has_return)
1938 {
1939 q->prologue_end = fi.prologue_end;
1940 query_statement (fi.name, fi.decl_file, fi.decl_line,
1941 &fi.die, entrypc, q);
1942 }
1943 else
1944 {
1945 query_statement (fi.name, fi.decl_file, fi.decl_line,
1946 &fi.die, fi.prologue_end, q);
1947 }
1948
1949 q->unmount_well_formed_probe_point();
1950 }
1951 catch (semantic_error &e)
1952 {
1953 q->sess.print_error (e);
1954 }
1955 }
1956
1957 static void
1958 query_srcfile_line (Dwarf_Addr addr, int lineno, dwarf_query * q)
1959 {
1960 assert (q->has_statement_str || q->has_function_str);
1961 assert (q->spec_type == function_file_and_line);
1962
1963 base_func_info_map_t bfis = q->filtered_all();
1964 base_func_info_map_t::iterator i;
1965 for (i = bfis.begin(); i != bfis.end(); ++i)
1966 {
1967 if (q->dw.die_has_pc (i->die, addr))
1968 {
1969 if (q->sess.verbose>3)
1970 clog << _("filtered DIE lands on srcfile\n");
1971 Dwarf_Die scope;
1972 q->dw.inner_die_containing_pc(i->die, addr, scope);
1973
1974 interned_string canon_func = q->final_function_name(i->name, i->decl_file,
1975 lineno /* NB: not i->decl_line */ );
1976
1977 if (q->has_nearest && (q->lineno_type == ABSOLUTE ||
1978 q->lineno_type == RELATIVE))
1979 {
1980 int lineno_nearest = q->linenos[0];
1981 if (q->lineno_type == RELATIVE)
1982 lineno_nearest += i->decl_line;
1983 interned_string canon_func_nearest = q->final_function_name(i->name,
1984 i->decl_file,
1985 lineno_nearest);
1986 q->mount_well_formed_probe_point();
1987 q->replace_probe_point_component_arg(TOK_STATEMENT, canon_func_nearest);
1988 }
1989
1990 q->mount_well_formed_probe_point();
1991 q->replace_probe_point_component_arg(TOK_FUNCTION, canon_func);
1992 q->replace_probe_point_component_arg(TOK_STATEMENT, canon_func);
1993
1994 query_statement (i->name, i->decl_file,
1995 lineno, // NB: not q->line !
1996 &scope, addr, q);
1997
1998 q->unmount_well_formed_probe_point();
1999 if (q->has_nearest && (q->lineno_type == ABSOLUTE ||
2000 q->lineno_type == RELATIVE))
2001 q->unmount_well_formed_probe_point();
2002 }
2003 }
2004 }
2005
2006 bool
2007 inline_instance_info::operator<(const inline_instance_info& other) const
2008 {
2009 if (entrypc != other.entrypc)
2010 return entrypc < other.entrypc;
2011
2012 if (decl_line != other.decl_line)
2013 return decl_line < other.decl_line;
2014
2015 int cmp = name.compare(other.name);
2016 if (!cmp) // tiebreaker
2017 cmp = decl_file.compare(other.decl_file);
2018
2019 return cmp < 0;
2020 }
2021
2022
2023 static int
2024 query_dwarf_inline_instance (Dwarf_Die * die, dwarf_query * q)
2025 {
2026 assert (q->has_statement_str || q->has_function_str);
2027 assert (!q->has_call && !q->has_return && !q->has_exported);
2028
2029 try
2030 {
2031 if (q->sess.verbose>2)
2032 clog << _F("selected inline instance of %s\n", q->dw.function_name.c_str());
2033
2034 Dwarf_Addr entrypc;
2035 if (q->dw.die_entrypc (die, &entrypc))
2036 {
2037 inline_instance_info inl;
2038 inl.die = *die;
2039 inl.name = q->dw.function_name;
2040 inl.entrypc = entrypc;
2041 const char* df;
2042 q->dw.function_file (&df);
2043 inl.decl_file = df ?: "";
2044 q->dw.function_line (&inl.decl_line);
2045
2046 // make sure that this inline hasn't already
2047 // been matched from a different CU
2048 if (q->inline_dupes.insert(inl).second)
2049 q->filtered_inlines.push_back(inl);
2050 }
2051 return DWARF_CB_OK;
2052 }
2053 catch (const semantic_error& e)
2054 {
2055 q->sess.print_error (e);
2056 return DWARF_CB_ABORT;
2057 }
2058 }
2059
2060 static bool
2061 is_filtered_func_exists (func_info_map_t const& filtered, func_info *fi)
2062 {
2063 for (unsigned i = 0; i < filtered.size(); i++)
2064 {
2065 if ((filtered[i].entrypc == fi->entrypc) && (filtered[i].name == fi->name))
2066 return true;
2067 }
2068
2069 return false;
2070 }
2071
2072 static int
2073 query_dwarf_func (Dwarf_Die * func, dwarf_query * q)
2074 {
2075 assert (q->has_statement_str || q->has_function_str);
2076
2077 // weed out functions whose decl_file isn't one of
2078 // the source files that we actually care about
2079 if (q->spec_type != function_alone &&
2080 q->filtered_srcfiles.count(dwarf_decl_file(func)?:"") == 0)
2081 return DWARF_CB_OK;
2082
2083 try
2084 {
2085 q->dw.focus_on_function (func);
2086
2087 if (!q->dw.function_scope_matches(q->scopes))
2088 return DWARF_CB_OK;
2089
2090 // make sure that this function address hasn't
2091 // already been matched under an aliased name
2092 Dwarf_Addr addr;
2093 if (!q->dw.func_is_inline() &&
2094 dwarf_entrypc(func, &addr) == 0 &&
2095 !q->alias_dupes.insert(addr).second)
2096 return DWARF_CB_OK;
2097
2098 if (q->dw.func_is_inline () && (! q->has_call) && (! q->has_return) && (! q->has_exported))
2099 {
2100 if (q->sess.verbose>3)
2101 clog << _F("checking instances of inline %s\n", q->dw.function_name.c_str());
2102 q->dw.iterate_over_inline_instances (query_dwarf_inline_instance, q);
2103 }
2104 else if (q->dw.func_is_inline () && (q->has_return)) // PR 11553
2105 {
2106 q->inlined_non_returnable.insert (q->dw.function_name);
2107 }
2108 else if (!q->dw.func_is_inline () && (! q->has_inline))
2109 {
2110 if (q->has_exported && !q->dw.func_is_exported ())
2111 return DWARF_CB_OK;
2112 if (q->sess.verbose>2)
2113 clog << _F("selected function %s\n", q->dw.function_name.c_str());
2114
2115 func_info func;
2116 q->dw.function_die (&func.die);
2117 func.name = q->dw.function_name;
2118 const char *df;
2119 q->dw.function_file (&df);
2120 func.decl_file = df ?: "";
2121 q->dw.function_line (&func.decl_line);
2122
2123 Dwarf_Addr entrypc;
2124
2125 func.entrypc = 0;
2126 Dwarf_Addr bias;
2127 Dwfl_Module *mod = q->dw.module;
2128 Elf* elf = (dwarf_getelf (dwfl_module_getdwarf (mod, &bias))
2129 ?: dwfl_module_getelf (mod, &bias));
2130
2131 GElf_Ehdr ehdr_mem;
2132 GElf_Ehdr* em = gelf_getehdr (elf, &ehdr_mem);
2133 if (em == NULL) throw SEMANTIC_ERROR (_("Couldn't get elf header"));
2134
2135 /* Giving priority to sym_table for ppc64*/
2136 if ((em->e_machine == EM_PPC64) && ((em->e_flags & EF_PPC64_ABI) == 2)
2137 && (q->dw.mod_info->sym_table))
2138 {
2139 /* The linkage name is the best match for the symbol table. */
2140 const string& linkage_name = dwarf_linkage_name(&func.die)
2141 ?: dwarf_diename(&func.die) ?: (string)func.name;
2142
2143 set<func_info *> fis = q->dw.mod_info->sym_table->lookup_symbol(linkage_name);
2144 for (set<func_info*>::iterator it=fis.begin(); it!=fis.end() ; ++it)
2145 {
2146 func.entrypc = (*it)->entrypc;
2147 if (is_filtered_func_exists(q->filtered_functions, &func))
2148 continue;
2149 q->filtered_functions.push_back(func);
2150 }
2151 }
2152
2153 /* If not ppc64 or not found in sym_table, try it directly. */
2154 if (!func.entrypc && q->dw.function_entrypc (&entrypc))
2155 {
2156 func.entrypc = entrypc;
2157 q->filtered_functions.push_back (func);
2158 }
2159 /* else this function is fully inlined, just ignore it */
2160 }
2161 return DWARF_CB_OK;
2162 }
2163 catch (const semantic_error& e)
2164 {
2165 q->sess.print_error (e);
2166 return DWARF_CB_ABORT;
2167 }
2168 }
2169
2170 static int
2171 query_cu (Dwarf_Die * cudie, dwarf_query * q)
2172 {
2173 assert (q->has_statement_str || q->has_function_str);
2174
2175 if (pending_interrupts) return DWARF_CB_ABORT;
2176
2177 try
2178 {
2179 q->dw.focus_on_cu (cudie);
2180
2181 if (false && q->sess.verbose>2)
2182 clog << _F("focused on CU '%s', in module '%s'\n",
2183 q->dw.cu_name().c_str(), q->dw.module_name.c_str());
2184
2185 q->filtered_srcfiles.clear();
2186 q->filtered_functions.clear();
2187 q->filtered_inlines.clear();
2188
2189 // In this path, we find "abstract functions", record
2190 // information about them, and then (depending on lineno
2191 // matching) possibly emit one or more of the function's
2192 // associated addresses. Unfortunately the control of this
2193 // cannot easily be turned inside out.
2194
2195 if (q->spec_type != function_alone)
2196 {
2197 // If we have a pattern string with a filename, we need
2198 // to elaborate the srcfile mask in question first.
2199 q->dw.collect_srcfiles_matching (q->file, q->filtered_srcfiles);
2200
2201 // If we have a file pattern and *no* srcfile matches, there's
2202 // no need to look further into this CU, so skip.
2203 if (q->filtered_srcfiles.empty())
2204 return DWARF_CB_OK;
2205 }
2206
2207 // Pick up [entrypc, name, DIE] tuples for all the functions
2208 // matching the query, and fill in the prologue endings of them
2209 // all in a single pass.
2210 int rc = q->dw.iterate_over_functions (query_dwarf_func, q, q->function);
2211 if (rc != DWARF_CB_OK)
2212 q->query_done = true;
2213
2214 if (!q->filtered_functions.empty() &&
2215 !q->has_statement_str && // PR 2608
2216 q->sess.prologue_searching_mode != systemtap_session::prologue_searching_never &&
2217 (q->sess.prologue_searching_mode == systemtap_session::prologue_searching_always ||
2218 (q->has_process && !q->dw.has_valid_locs()))) // PR 6871 && PR 6941
2219 q->dw.resolve_prologue_endings (q->filtered_functions);
2220
2221 if (q->has_label)
2222 {
2223 enum lineno_t lineno_type = WILDCARD;
2224 if (q->spec_type == function_file_and_line)
2225 lineno_type = q->lineno_type;
2226 base_func_info_map_t bfis = q->filtered_all();
2227 base_func_info_map_t::iterator i;
2228 for (i = bfis.begin(); i != bfis.end(); ++i)
2229 q->dw.iterate_over_labels (&i->die, q->label_val, *i, q->linenos,
2230 lineno_type, q, query_label);
2231 }
2232 else if (q->has_callee || q->has_callees_num)
2233 {
2234 // .callee(str) --> str, .callees[(N)] --> "*"
2235 string callee_val = q->has_callee ? q->callee_val : "*";
2236 int64_t callees_num_val = q->has_callees_num ? q->callees_num_val : 1;
2237
2238 // NB: We filter functions that do not match the file here rather than
2239 // in query_callee because we only want the filtering to apply to the
2240 // first level, not to callees that are recursed into if
2241 // callees_num_val > 1.
2242 base_func_info_map_t bfis = q->filtered_all();
2243 base_func_info_map_t::iterator i;
2244 for (i = bfis.begin(); i != bfis.end(); ++i)
2245 {
2246 if (q->spec_type != function_alone &&
2247 q->filtered_srcfiles.count(i->decl_file) == 0)
2248 continue;
2249 q->dw.iterate_over_callees (&i->die, callee_val,
2250 callees_num_val,
2251 q, query_callee, *i);
2252 }
2253 }
2254 else if (q->spec_type == function_file_and_line
2255 // User specified function, file and lineno, but if they match
2256 // exactly a specific function in a specific line at a specific
2257 // decl_line, the user doesn't actually want to probe a lineno,
2258 // but rather the function itself. So let fall through to
2259 // query_func_info/query_inline_instance_info in final else.
2260 && !q->is_fully_specified_function())
2261 {
2262 // .statement(...:NN) often gets mixed up with .function(...:NN)
2263 if (q->has_function_str)
2264 q->sess.print_warning (_("For probing a particular line, use a "
2265 ".statement() probe, not .function()"),
2266 q->base_probe->tok);
2267
2268 base_func_info_map_t bfis = q->filtered_all();
2269
2270 set<string>::const_iterator srcfile;
2271 for (srcfile = q->filtered_srcfiles.begin();
2272 srcfile != q->filtered_srcfiles.end(); ++srcfile)
2273 q->dw.iterate_over_srcfile_lines(srcfile->c_str(), q->linenos,
2274 q->lineno_type, bfis,
2275 query_srcfile_line,
2276 q->has_nearest, q);
2277 }
2278 else
2279 {
2280 // Otherwise, simply probe all resolved functions.
2281 for (func_info_map_t::iterator i = q->filtered_functions.begin();
2282 i != q->filtered_functions.end(); ++i)
2283 query_func_info (i->entrypc, *i, q);
2284
2285 // And all inline instances (if we're not excluding inlines with ".call")
2286 if (! q->has_call)
2287 for (inline_instance_map_t::iterator i
2288 = q->filtered_inlines.begin(); i != q->filtered_inlines.end(); ++i)
2289 query_inline_instance_info (*i, q);
2290 }
2291 return DWARF_CB_OK;
2292 }
2293 catch (const semantic_error& e)
2294 {
2295 q->sess.print_error (e);
2296 return DWARF_CB_ABORT;
2297 }
2298 }
2299
2300
2301 void
2302 dwarf_query::query_module_functions ()
2303 {
2304 try
2305 {
2306 filtered_srcfiles.clear();
2307 filtered_functions.clear();
2308 filtered_inlines.clear();
2309
2310 // Collect all module functions so we know which CUs are interesting
2311 int rc = dw.iterate_single_function(query_dwarf_func, this, function);
2312 if (rc != DWARF_CB_OK)
2313 {
2314 query_done = true;
2315 return;
2316 }
2317
2318 set<void*> used_cus; // by cu->addr
2319 vector<Dwarf_Die> cus;
2320 Dwarf_Die cu_mem;
2321
2322 base_func_info_map_t bfis = filtered_all();
2323 base_func_info_map_t::iterator i;
2324 for (i = bfis.begin(); i != bfis.end(); ++i)
2325 if (dwarf_diecu(&i->die, &cu_mem, NULL, NULL) &&
2326 used_cus.insert(cu_mem.addr).second)
2327 cus.push_back(cu_mem);
2328
2329 // Reset the dupes since we didn't actually collect them the first time
2330 alias_dupes.clear();
2331 inline_dupes.clear();
2332
2333 // Run the query again on the individual CUs
2334 for (vector<Dwarf_Die>::iterator i = cus.begin(); i != cus.end(); ++i){
2335 rc = query_cu(&*i, this);
2336 if (rc != DWARF_CB_OK)
2337 {
2338 query_done = true;
2339 return;
2340 }
2341 }
2342 }
2343 catch (const semantic_error& e)
2344 {
2345 sess.print_error (e);
2346 }
2347 }
2348
2349
2350 static void
2351 validate_module_elf (Dwfl_Module *mod, const char *name, base_query *q)
2352 {
2353 // Validate the machine code in this elf file against the
2354 // session machine. This is important, in case the wrong kind
2355 // of debuginfo is being automagically processed by elfutils.
2356 // While we can tell i686 apart from x86-64, unfortunately
2357 // we can't help confusing i586 vs i686 (both EM_386).
2358
2359 Dwarf_Addr bias;
2360 // We prefer dwfl_module_getdwarf to dwfl_module_getelf here,
2361 // because dwfl_module_getelf can force costly section relocations
2362 // we don't really need, while either will do for this purpose.
2363 Elf* elf = (dwarf_getelf (dwfl_module_getdwarf (mod, &bias))
2364 ?: dwfl_module_getelf (mod, &bias));
2365
2366 GElf_Ehdr ehdr_mem;
2367 GElf_Ehdr* em = gelf_getehdr (elf, &ehdr_mem);
2368 if (em == 0) { DWFL_ASSERT ("dwfl_getehdr", dwfl_errno()); }
2369 assert(em);
2370 int elf_machine = em->e_machine;
2371 const char* debug_filename = "";
2372 const char* main_filename = "";
2373 (void) dwfl_module_info (mod, NULL, NULL,
2374 NULL, NULL, NULL,
2375 & main_filename,
2376 & debug_filename);
2377 const string& sess_machine = q->sess.architecture;
2378
2379 string expect_machine; // to match sess.machine (i.e., kernel machine)
2380 string expect_machine2;
2381
2382 // NB: See also the 'uname -m' squashing done in main.cxx.
2383 switch (elf_machine)
2384 {
2385 // x86 and ppc are bi-architecture; a 64-bit kernel
2386 // can normally run either 32-bit or 64-bit *userspace*.
2387 case EM_386:
2388 expect_machine = "i?86";
2389 if (! q->has_process) break; // 32-bit kernel/module
2390 /* FALLSTHROUGH */
2391 case EM_X86_64:
2392 expect_machine2 = "x86_64";
2393 break;
2394 case EM_PPC:
2395 case EM_PPC64:
2396 expect_machine = "powerpc";
2397 break;
2398 case EM_S390: expect_machine = "s390"; break;
2399 case EM_IA_64: expect_machine = "ia64"; break;
2400 case EM_ARM: expect_machine = "arm*"; break;
2401 case EM_AARCH64: expect_machine = "arm64"; break;
2402 // XXX: fill in some more of these
2403 default: expect_machine = "?"; break;
2404 }
2405
2406 if (! debug_filename) debug_filename = main_filename;
2407 if (! debug_filename) debug_filename = name;
2408
2409 if (fnmatch (expect_machine.c_str(), sess_machine.c_str(), 0) != 0 &&
2410 fnmatch (expect_machine2.c_str(), sess_machine.c_str(), 0) != 0)
2411 {
2412 stringstream msg;
2413 msg << _F("ELF machine %s|%s (code %d) mismatch with target %s in '%s'",
2414 expect_machine.c_str(), expect_machine2.c_str(), elf_machine,
2415 sess_machine.c_str(), debug_filename);
2416 throw SEMANTIC_ERROR(msg.str ());
2417 }
2418
2419 if (q->sess.verbose>1)
2420 clog << _F("focused on module '%s' = [%#" PRIx64 "-%#" PRIx64 ", bias %#" PRIx64
2421 " file %s ELF machine %s|%s (code %d)\n",
2422 q->dw.module_name.c_str(), q->dw.module_start, q->dw.module_end,
2423 q->dw.module_bias, debug_filename, expect_machine.c_str(),
2424 expect_machine2.c_str(), elf_machine);
2425 }
2426
2427
2428
2429 static Dwarf_Addr
2430 lookup_symbol_address (Dwfl_Module *m, const char* wanted)
2431 {
2432 int syments = dwfl_module_getsymtab(m);
2433 assert(syments);
2434 for (int i = 1; i < syments; ++i)
2435 {
2436 GElf_Sym sym;
2437 const char *name = dwfl_module_getsym(m, i, &sym, NULL);
2438 if (name != NULL && strcmp(name, wanted) == 0)
2439 return sym.st_value;
2440 }
2441
2442 return 0;
2443 }
2444
2445
2446
2447 static int
2448 query_module (Dwfl_Module *mod,
2449 void **,
2450 const char *name,
2451 Dwarf_Addr addr,
2452 base_query *q)
2453 {
2454 try
2455 {
2456 module_info* mi = q->sess.module_cache->cache[name];
2457 if (mi == 0)
2458 {
2459 mi = q->sess.module_cache->cache[name] = new module_info(name);
2460
2461 mi->mod = mod;
2462 mi->addr = addr;
2463
2464 const char* debug_filename = "";
2465 const char* main_filename = "";
2466 (void) dwfl_module_info (mod, NULL, NULL,
2467 NULL, NULL, NULL,
2468 & main_filename,
2469 & debug_filename);
2470
2471 if (debug_filename || main_filename)
2472 {
2473 mi->elf_path = debug_filename ?: main_filename;
2474 }
2475 else if (name == TOK_KERNEL)
2476 {
2477 mi->dwarf_status = info_absent;
2478 }
2479 }
2480 // OK, enough of that module_info caching business.
2481
2482 q->dw.focus_on_module(mod, mi);
2483
2484 // If we have enough information in the pattern to skip a module and
2485 // the module does not match that information, return early.
2486 if (!q->dw.module_name_matches(q->module_val))
2487 return pending_interrupts ? DWARF_CB_ABORT : DWARF_CB_OK;
2488
2489 // Don't allow module("*kernel*") type expressions to match the
2490 // elfutils module "kernel", which we refer to in the probe
2491 // point syntax exclusively as "kernel.*".
2492 if (q->dw.module_name == TOK_KERNEL && ! q->has_kernel)
2493 return pending_interrupts ? DWARF_CB_ABORT : DWARF_CB_OK;
2494
2495 if (mod)
2496 validate_module_elf(mod, name, q);
2497 else
2498 assert(q->has_kernel); // and no vmlinux to examine
2499
2500 if (q->sess.verbose>2)
2501 cerr << _F("focused on module '%s'\n", q->dw.module_name.c_str());
2502
2503
2504 // Collect a few kernel addresses. XXX: these belong better
2505 // to the sess.module_info["kernel"] struct.
2506 if (q->dw.module_name == TOK_KERNEL)
2507 {
2508 if (! q->sess.sym_kprobes_text_start)
2509 q->sess.sym_kprobes_text_start = lookup_symbol_address (mod, "__kprobes_text_start");
2510 if (! q->sess.sym_kprobes_text_end)
2511 q->sess.sym_kprobes_text_end = lookup_symbol_address (mod, "__kprobes_text_end");
2512 if (! q->sess.sym_stext)
2513 q->sess.sym_stext = lookup_symbol_address (mod, "_stext");
2514 }
2515
2516 // If there is a .library component, then q->path will hold the path to
2517 // the executable if the library was fully resolved. If not (e.g. not
2518 // absolute, or globby), resort to iterate_over_libraries().
2519 if (q->has_library && q->path.empty())
2520 q->dw.iterate_over_libraries (&q->query_library_callback, q);
2521 // .plt is translated to .plt.statement(N). We only want to iterate for the
2522 // .plt case
2523 else if (q->has_plt && ! q->has_statement)
2524 {
2525 q->dw.iterate_over_plt (q, &q->query_plt_callback);
2526 q->visited_modules.insert(name);
2527 }
2528 else
2529 {
2530 // search the module for matches of the probe point.
2531 q->handle_query_module();
2532 q->visited_modules.insert(name);
2533 }
2534
2535 // If we know that there will be no more matches, abort early.
2536 if (q->dw.module_name_final_match(q->module_val) || pending_interrupts)
2537 return DWARF_CB_ABORT;
2538 else
2539 return DWARF_CB_OK;
2540 }
2541 catch (const semantic_error& e)
2542 {
2543 q->sess.print_error (e);
2544 return DWARF_CB_ABORT;
2545 }
2546 }
2547
2548
2549 void
2550 base_query::query_library_callback (base_query *me, const char *data)
2551 {
2552 me->query_library (data);
2553 }
2554
2555
2556 probe*
2557 build_library_probe(dwflpp& dw,
2558 const string& library,
2559 probe *base_probe,
2560 probe_point *base_loc)
2561 {
2562 probe_point* specific_loc = new probe_point(*base_loc);
2563 vector<probe_point::component*> derived_comps;
2564
2565 // Create new probe point for the matching library. This is what will be
2566 // shown in listing mode. Also replace the process(str) with the real
2567 // absolute path rather than keeping what the user typed in.
2568 vector<probe_point::component*>::iterator it;
2569 for (it = specific_loc->components.begin();
2570 it != specific_loc->components.end(); ++it)
2571 if ((*it)->functor == TOK_PROCESS)
2572 derived_comps.push_back(new probe_point::component(TOK_PROCESS,
2573 new literal_string(path_remove_sysroot(dw.sess, dw.module_name))));
2574 else if ((*it)->functor == TOK_LIBRARY)
2575 derived_comps.push_back(new probe_point::component(TOK_LIBRARY,
2576 new literal_string(path_remove_sysroot(dw.sess, library)),
2577 true /* from_glob */ ));
2578 else
2579 derived_comps.push_back(*it);
2580 probe_point* derived_loc = new probe_point(*specific_loc);
2581 derived_loc->components = derived_comps;
2582 return new probe (new probe (base_probe, specific_loc), derived_loc);
2583 }
2584
2585 bool
2586 query_one_library (const char *library, dwflpp & dw,
2587 const string user_lib, probe * base_probe, probe_point *base_loc,
2588 vector<derived_probe *> & results)
2589 {
2590 if (dw.function_name_matches_pattern(library, "*" + user_lib))
2591 {
2592 string library_path = find_executable (library, "", dw.sess.sysenv,
2593 "LD_LIBRARY_PATH");
2594 probe *new_base = build_library_probe(dw, library_path,
2595 base_probe, base_loc);
2596
2597 // We pass true for the optional parameter of derive_probes() here to
2598 // indicate that we don't mind if the probe doesn't resolve. This is
2599 // because users expect wildcarded probe points to only apply to a subset
2600 // of matching libraries, in the sense of "any", rather than "all", just
2601 // like module("*") and process("*"). See also dwarf_builder::build().
2602 derive_probes(dw.sess, new_base, results, true /* optional */ );
2603
2604 if (dw.sess.verbose > 2)
2605 clog << _("module=") << library_path << endl;
2606 return true;
2607 }
2608 return false;
2609 }
2610
2611
2612 void
2613 dwarf_query::query_library (const char *library)
2614 {
2615 visited_libraries.insert(library);
2616 if (query_one_library (library, dw, user_lib, base_probe, base_loc, results))
2617 resolved_library = true;
2618 }
2619
2620 struct plt_expanding_visitor: public var_expanding_visitor
2621 {
2622 plt_expanding_visitor(const string & entry):
2623 entry (entry)
2624 {
2625 }
2626 const string & entry;
2627
2628 void visit_target_symbol (target_symbol* e);
2629 };
2630
2631
2632 void
2633 base_query::query_plt_callback (base_query *me, const char *entry, size_t address)
2634 {
2635 if (me->dw.function_name_matches_pattern (entry, me->plt_val))
2636 me->query_plt (entry, address);
2637 me->dw.mod_info->plt_funcs.insert(entry);
2638 }
2639
2640
2641 void
2642 query_one_plt (const char *entry, long addr, dwflpp & dw,
2643 probe * base_probe, probe_point *base_loc,
2644 vector<derived_probe *> & results, base_query *q)
2645 {
2646 interned_string module = dw.module_name;
2647 if (q->has_process)
2648 module = path_remove_sysroot(dw.sess, module);
2649
2650 probe_point* specific_loc = new probe_point(*base_loc);
2651 specific_loc->well_formed = true;
2652
2653 vector<probe_point::component*> derived_comps;
2654
2655 if (dw.sess.verbose > 2)
2656 clog << _F("plt entry=%s\n", entry);
2657
2658 vector<probe_point::component*>::iterator it;
2659 for (it = specific_loc->components.begin();
2660 it != specific_loc->components.end(); ++it)
2661 if ((*it)->functor == TOK_PROCESS)
2662 {
2663 // Replace with fully resolved path
2664 *it = new probe_point::component(TOK_PROCESS,
2665 new literal_string(q->has_library ? q->path : module));
2666 derived_comps.push_back(*it);
2667 }
2668 else if ((*it)->functor == TOK_PLT)
2669 {
2670 // Replace possibly globby component
2671 *it = new probe_point::component(TOK_PLT,
2672 new literal_string(string(entry)));
2673 derived_comps.push_back(*it);
2674 derived_comps.push_back(new probe_point::component(TOK_STATEMENT,
2675 new literal_number(addr, true)));
2676 }
2677 else
2678 derived_comps.push_back(*it);
2679 probe_point* derived_loc = new probe_point(*specific_loc);
2680 derived_loc->components = derived_comps;
2681 probe *new_base = new probe (new probe (base_probe, specific_loc),
2682 derived_loc);
2683 string e = string(entry);
2684 plt_expanding_visitor pltv (e);
2685 pltv.replace (new_base->body);
2686
2687 literal_map_t params;
2688 for (unsigned i = 0; i < derived_loc->components.size(); ++i)
2689 {
2690 probe_point::component *c = derived_loc->components[i];
2691 params[c->functor] = c->arg;
2692 }
2693 dwarf_query derived_q(new_base, derived_loc, dw, params, results, "", "");
2694 dw.iterate_over_modules<base_query>(&query_module, &derived_q);
2695 }
2696
2697
2698 void
2699 dwarf_query::query_plt (const char *entry, size_t address)
2700 {
2701 query_one_plt (entry, address, dw, base_probe, base_loc, results, this);
2702 }
2703
2704 // This would more naturally fit into elaborate.cxx:semantic_pass_symbols,
2705 // but the needed declaration for module_cache is not available there.
2706 // Nor for that matter in session.cxx. Only in this CU is that field ever
2707 // set (in query_module() above), so we clean it up here too.
2708 static void
2709 delete_session_module_cache (systemtap_session& s)
2710 {
2711 if (s.module_cache) {
2712 if (s.verbose > 3)
2713 clog << _("deleting module_cache") << endl;
2714 delete s.module_cache;
2715 s.module_cache = 0;
2716 }
2717 }
2718
2719
2720 struct dwarf_var_expanding_visitor: public var_expanding_visitor
2721 {
2722 dwarf_query & q;
2723 Dwarf_Die *scope_die;
2724 Dwarf_Addr addr;
2725 block *add_block;
2726 block *add_call_probe; // synthesized from .return probes with saved $vars
2727 // NB: tids are not always collected in add_block & add_call_probe, because
2728 // gen_kretprobe_saved_return doesn't need them. Thus we need these extra
2729 // *_tid bools for gen_mapped_saved_return to tell what's there.
2730 bool add_block_tid, add_call_probe_tid;
2731 unsigned saved_longs, saved_strings; // data saved within kretprobes
2732 unordered_map<std::string, expression *> return_ts_map;
2733 vector<Dwarf_Die> scopes;
2734 // probe counter name -> pointer of associated probe
2735 std::set<std::string> perf_counter_refs;
2736 bool visited;
2737
2738 dwarf_var_expanding_visitor(dwarf_query & q, Dwarf_Die *sd, Dwarf_Addr a):
2739 q(q), scope_die(sd), addr(a), add_block(NULL), add_call_probe(NULL),
2740 add_block_tid(false), add_call_probe_tid(false),
2741 saved_longs(0), saved_strings(0), visited(false) {}
2742 expression* gen_mapped_saved_return(expression* e, const string& name);
2743 expression* gen_kretprobe_saved_return(expression* e);
2744 void visit_target_symbol_saved_return (target_symbol* e);
2745 void visit_target_symbol_context (target_symbol* e);
2746 void visit_target_symbol (target_symbol* e);
2747 void visit_atvar_op (atvar_op* e);
2748 void visit_cast_op (cast_op* e);
2749 void visit_entry_op (entry_op* e);
2750 void visit_perf_op (perf_op* e);
2751 private:
2752 vector<Dwarf_Die>& getscopes(target_symbol *e);
2753 };
2754
2755
2756 unsigned var_expanding_visitor::tick = 0;
2757
2758
2759 var_expanding_visitor::var_expanding_visitor (): op()
2760 {
2761 // FIXME: for the time being, by default we only support plain '$foo
2762 // = bar', not '+=' or any other op= variant. This is fixable, but a
2763 // bit ugly.
2764 //
2765 // If derived classes desire to add additional operator support, add
2766 // new operators to this list in the derived class constructor.
2767 valid_ops.insert ("=");
2768 }
2769
2770
2771 void
2772 var_expanding_visitor::provide_lvalue_call(functioncall* fcall)
2773 {
2774 // Provide the functioncall to our parent, so that it can be used to
2775 // substitute for the assignment node immediately above us.
2776 assert(!target_symbol_setter_functioncalls.empty());
2777 *(target_symbol_setter_functioncalls.top()) = fcall;
2778 }
2779
2780
2781 bool
2782 var_expanding_visitor::rewrite_lvalue(const token* tok, interned_string& eop,
2783 expression*& lvalue, expression*& rvalue)
2784 {
2785 // Our job would normally be to require() the left and right sides
2786 // into a new assignment. What we're doing is slightly trickier:
2787 // we're pushing a functioncall** onto a stack, and if our left
2788 // child sets the functioncall* for that value, we're going to
2789 // assume our left child was a target symbol -- transformed into a
2790 // set_target_foo(value) call, and it wants to take our right child
2791 // as the argument "value".
2792 //
2793 // This is why some people claim that languages with
2794 // constructor-decomposing case expressions have a leg up on
2795 // visitors.
2796
2797 functioncall *fcall = NULL;
2798
2799 // Let visit_target_symbol know what operator it should handle.
2800 interned_string* old_op = op;
2801 op = & eop;
2802
2803 target_symbol_setter_functioncalls.push (&fcall);
2804 replace (lvalue);
2805 target_symbol_setter_functioncalls.pop ();
2806 replace (rvalue);
2807
2808 op = old_op;
2809
2810 if (fcall != NULL)
2811 {
2812 // Our left child is informing us that it was a target variable
2813 // and it has been replaced with a set_target_foo() function
2814 // call; we are going to provide that function call -- with the
2815 // right child spliced in as sole argument -- in place of
2816 // ourselves, in the var expansion we're in the middle of making.
2817
2818 if (valid_ops.find (eop) == valid_ops.end ())
2819 {
2820 // Build up a list of supported operators.
2821 string ops;
2822 std::set<string>::iterator i;
2823 int valid_ops_size = 0;
2824 for (i = valid_ops.begin(); i != valid_ops.end(); i++)
2825 {
2826 ops += " " + *i + ",";
2827 valid_ops_size++;
2828 }
2829 ops.resize(ops.size() - 1); // chop off the last ','
2830
2831 // Throw the error.
2832 throw SEMANTIC_ERROR (_NF("Only the following assign operator is implemented on target variables: %s",
2833 "Only the following assign operators are implemented on target variables: %s",
2834 valid_ops_size, ops.c_str()), tok);
2835
2836 }
2837
2838 assert (lvalue == fcall);
2839 if (rvalue)
2840 fcall->args.push_back (rvalue);
2841 provide (fcall);
2842 return true;
2843 }
2844 else
2845 return false;
2846 }
2847
2848
2849 void
2850 var_expanding_visitor::visit_assignment (assignment* e)
2851 {
2852 if (!rewrite_lvalue (e->tok, e->op, e->left, e->right))
2853 provide (e);
2854 }
2855
2856
2857 void
2858 var_expanding_visitor::visit_pre_crement (pre_crement* e)
2859 {
2860 expression *dummy = NULL;
2861 if (!rewrite_lvalue (e->tok, e->op, e->operand, dummy))
2862 provide (e);
2863 }
2864
2865
2866 void
2867 var_expanding_visitor::visit_post_crement (post_crement* e)
2868 {
2869 expression *dummy = NULL;
2870 if (!rewrite_lvalue (e->tok, e->op, e->operand, dummy))
2871 provide (e);
2872 }
2873
2874
2875 void
2876 var_expanding_visitor::visit_delete_statement (delete_statement* s)
2877 {
2878 string fakeop = "delete";
2879 interned_string fopr = fakeop;
2880 expression *dummy = NULL;
2881 if (!rewrite_lvalue (s->tok, fopr, s->value, dummy))
2882 provide (s);
2883 }
2884
2885
2886 void
2887 var_expanding_visitor::visit_defined_op (defined_op* e)
2888 {
2889 bool resolved = true;
2890
2891 defined_ops.push (e);
2892 try {
2893 replace (e->operand);
2894
2895 // NB: Formerly, we had some curious cases to consider here, depending on what
2896 // various visit_target_symbol() implementations do for successful or
2897 // erroneous resolutions. Some would signal a visit_target_symbol failure
2898 // with an exception, with a set flag within the target_symbol, or nothing
2899 // at all.
2900 //
2901 // Now, failures always have to be signalled with a
2902 // saved_conversion_error being chained to the target_symbol.
2903 // Successes have to result in an attempted rewrite of the
2904 // target_symbol (via provide()).
2905 //
2906 // Edna Mode: "no capes". fche: "no exceptions".
2907
2908 // dwarf stuff: success: rewrites to a function; failure: retains target_symbol, sets saved_conversion_error
2909 //
2910 // sdt-kprobes sdt.h: success: string or functioncall; failure: semantic_error
2911 //
2912 // sdt-uprobes: success: string or no op; failure: no op; expect derived/synthetic
2913 // dwarf probe to take care of it.
2914 // But this is rather unhelpful. So we rig the sdt_var_expanding_visitor
2915 // to pass through @defined() to the synthetic dwarf probe.
2916 //
2917 // utrace: success: rewrites to function; failure: semantic_error
2918 //
2919 // procfs: success: rewrites to function; failure: semantic_error
2920
2921 target_symbol* tsym = dynamic_cast<target_symbol*> (e->operand);
2922 if (tsym && tsym->saved_conversion_error) // failing
2923 resolved = false;
2924 else if (tsym) // unresolved but not marked failing
2925 {
2926 // There are some visitors that won't touch certain target_symbols,
2927 // e.g. dwarf_var_expanding_visitor won't resolve @cast. We should
2928 // leave it for now so some other visitor can have a chance.
2929 provide (e);
2930 return;
2931 }
2932 else // resolved, rewritten to some other expression type
2933 resolved = true;
2934 } catch (const semantic_error& e) {
2935 assert (0); // should not happen
2936 }
2937 defined_ops.pop ();
2938
2939 literal_number* ln = new literal_number (resolved ? 1 : 0);
2940 ln->tok = e->tok;
2941 provide (ln);
2942 }
2943
2944
2945 struct dwarf_pretty_print
2946 {
2947 dwarf_pretty_print (dwflpp& dw, vector<Dwarf_Die>& scopes, Dwarf_Addr pc,
2948 const string& local, bool userspace_p,
2949 const target_symbol& e):
2950 dw(dw), local(local), scopes(scopes), pc(pc),
2951 pointer(NULL), pointer_type(),
2952 userspace_p(userspace_p), deref_p(true)
2953 {
2954 init_ts (e);
2955 dw.type_die_for_local (scopes, pc, local, ts, &base_type);
2956 }
2957
2958 dwarf_pretty_print (dwflpp& dw, Dwarf_Die *scope_die, Dwarf_Addr pc,
2959 bool userspace_p, const target_symbol& e):
2960 dw(dw), scopes(1, *scope_die), pc(pc),
2961 pointer(NULL), pointer_type(),
2962 userspace_p(userspace_p), deref_p(true)
2963 {
2964 init_ts (e);
2965 dw.type_die_for_return (&scopes[0], pc, ts, &base_type);
2966 }
2967
2968 dwarf_pretty_print (dwflpp& dw, Dwarf_Die *type_die, expression* pointer,
2969 bool deref_p, bool userspace_p, const target_symbol& e):
2970 dw(dw), pc(0), pointer(pointer), pointer_type(*type_die),
2971 userspace_p(userspace_p), deref_p(deref_p)
2972 {
2973 init_ts (e);
2974 dw.type_die_for_pointer (type_die, ts, &base_type);
2975 }
2976
2977 functioncall* expand ();
2978 ~dwarf_pretty_print () { delete ts; }
2979
2980 private:
2981 dwflpp& dw;
2982 target_symbol* ts;
2983 bool print_full;
2984 Dwarf_Die base_type;
2985
2986 string local;
2987 vector<Dwarf_Die> scopes;
2988 Dwarf_Addr pc;
2989
2990 expression* pointer;
2991 Dwarf_Die pointer_type;
2992
2993 const bool userspace_p, deref_p;
2994
2995 void recurse (Dwarf_Die* type, target_symbol* e,
2996 print_format* pf, bool top=false);
2997 void recurse_bitfield (Dwarf_Die* type, target_symbol* e,
2998 print_format* pf);
2999 void recurse_base (Dwarf_Die* type, target_symbol* e,
3000 print_format* pf);
3001 void recurse_array (Dwarf_Die* type, target_symbol* e,
3002 print_format* pf, bool top);
3003 void recurse_pointer (Dwarf_Die* type, target_symbol* e,
3004 print_format* pf, bool top);
3005 void recurse_struct (Dwarf_Die* type, target_symbol* e,
3006 print_format* pf, bool top);
3007 void recurse_struct_members (Dwarf_Die* type, target_symbol* e,
3008 print_format* pf, int& count);
3009 bool print_chars (Dwarf_Die* type, target_symbol* e, print_format* pf);
3010
3011 void init_ts (const target_symbol& e);
3012 expression* deref (target_symbol* e);
3013 bool push_deref (print_format* pf, const string& fmt, target_symbol* e);
3014 };
3015
3016
3017 void
3018 dwarf_pretty_print::init_ts (const target_symbol& e)
3019 {
3020 // Work with a new target_symbol so we can modify arguments
3021 ts = new target_symbol (e);
3022
3023 if (ts->addressof)
3024 throw SEMANTIC_ERROR(_("cannot take address of pretty-printed variable"), ts->tok);
3025
3026 size_t depth = ts->pretty_print_depth ();
3027 if (depth == 0)
3028 throw SEMANTIC_ERROR(_("invalid target_symbol for pretty-print"), ts->tok);
3029 print_full = depth > 1;
3030 ts->components.pop_back();
3031 }
3032
3033
3034 functioncall*
3035 dwarf_pretty_print::expand ()
3036 {
3037 static unsigned tick = 0;
3038
3039 // function pretty_print_X([pointer], [arg1, arg2, ...]) {
3040 // try {
3041 // return sprintf("{.foo=...}", (ts)->foo, ...)
3042 // } catch {
3043 // return "ERROR"
3044 // }
3045 // }
3046
3047 // Create the function decl and call.
3048
3049 functiondecl *fdecl = new functiondecl;
3050 fdecl->tok = ts->tok;
3051 fdecl->synthetic = true;
3052 fdecl->name = "_dwarf_pretty_print_" + lex_cast(tick++);
3053 fdecl->type = pe_string;
3054
3055 functioncall* fcall = new functioncall;
3056 fcall->referent = fdecl;
3057 fcall->tok = ts->tok;
3058 fcall->function = fdecl->name;
3059 fcall->type = pe_string;
3060
3061 // If there's a <pointer>, replace it with a new var and make that
3062 // the first function argument.
3063 if (pointer)
3064 {
3065 vardecl *v = new vardecl;
3066 v->type = pe_long;
3067 v->name = "pointer";
3068 v->tok = ts->tok;
3069 fdecl->formal_args.push_back (v);
3070 fcall->args.push_back (pointer);
3071
3072 symbol* sym = new symbol;
3073 sym->tok = ts->tok;
3074 sym->name = v->name;
3075 pointer = sym;
3076 }
3077
3078 // For each expression argument, replace it with a function argument.
3079 for (unsigned i = 0; i < ts->components.size(); ++i)
3080 if (ts->components[i].type == target_symbol::comp_expression_array_index)
3081 {
3082 vardecl *v = new vardecl;
3083 v->type = pe_long;
3084 v->name = "index" + lex_cast(i);
3085 v->tok = ts->tok;
3086 fdecl->formal_args.push_back (v);
3087 fcall->args.push_back (ts->components[i].expr_index);
3088
3089 symbol* sym = new symbol;
3090 sym->tok = ts->tok;
3091 sym->name = v->name;
3092 ts->components[i].expr_index = sym;
3093 }
3094
3095 // Create the return sprintf.
3096 print_format* pf = print_format::create(ts->tok, "sprintf");
3097 return_statement* rs = new return_statement;
3098 rs->tok = ts->tok;
3099 rs->value = pf;
3100
3101 // Recurse into the actual values.
3102 recurse (&base_type, ts, pf, true);
3103 pf->components = print_format::string_to_components(pf->raw_components);
3104
3105 // Create the try-catch net
3106 try_block* tb = new try_block;
3107 tb->tok = ts->tok;
3108 tb->try_block = rs;
3109 tb->catch_error_var = 0;
3110 return_statement* rs2 = new return_statement;
3111 rs2->tok = ts->tok;
3112 rs2->value = new literal_string (string("ERROR"));
3113 rs2->value->tok = ts->tok;
3114 tb->catch_block = rs2;
3115 fdecl->body = tb;
3116
3117 fdecl->join (dw.sess);
3118 return fcall;
3119 }
3120
3121
3122 void
3123 dwarf_pretty_print::recurse (Dwarf_Die* start_type, target_symbol* e,
3124 print_format* pf, bool top)
3125 {
3126 // deal with initial void* pointers
3127 if (!deref_p && null_die(start_type))
3128 {
3129 push_deref (pf, "%p", e);
3130 return;
3131 }
3132
3133 Dwarf_Die type;
3134 dw.resolve_unqualified_inner_typedie (start_type, &type, e);
3135
3136 switch (dwarf_tag(&type))
3137 {
3138 default:
3139 // XXX need a warning?
3140 // throw semantic_error ("unsupported type (tag " + lex_cast(dwarf_tag(&type))
3141 // + ") for " + dwarf_type_name(&type), e->tok);
3142 pf->raw_components.append("?");
3143 break;
3144
3145 case DW_TAG_enumeration_type:
3146 case DW_TAG_base_type:
3147 recurse_base (&type, e, pf);
3148 break;
3149
3150 case DW_TAG_array_type:
3151 recurse_array (&type, e, pf, top);
3152 break;
3153
3154 case DW_TAG_pointer_type:
3155 case DW_TAG_reference_type:
3156 case DW_TAG_rvalue_reference_type:
3157 recurse_pointer (&type, e, pf, top);
3158 break;
3159
3160 case DW_TAG_subroutine_type:
3161 push_deref (pf, "<function>:%p", e);
3162 break;
3163
3164 case DW_TAG_union_type:
3165 case DW_TAG_structure_type:
3166 case DW_TAG_class_type:
3167 recurse_struct (&type, e, pf, top);
3168 break;
3169 }
3170 }
3171
3172
3173 // Bit fields are handled as a special-case combination of recurse() and
3174 // recurse_base(), only called from recurse_struct_members(). The main
3175 // difference is that the value is always printed numerically, even if the
3176 // underlying type is a char.
3177 void
3178 dwarf_pretty_print::recurse_bitfield (Dwarf_Die* start_type, target_symbol* e,
3179 print_format* pf)
3180 {
3181 Dwarf_Die type;
3182 dw.resolve_unqualified_inner_typedie (start_type, &type, e);
3183
3184 int tag = dwarf_tag(&type);
3185 if (tag != DW_TAG_base_type && tag != DW_TAG_enumeration_type)
3186 {
3187 // XXX need a warning?
3188 // throw semantic_error ("unsupported bitfield type (tag " + lex_cast(tag)
3189 // + ") for " + dwarf_type_name(&type), e->tok);
3190 pf->raw_components.append("?");
3191 return;
3192 }
3193
3194 Dwarf_Attribute attr;
3195 Dwarf_Word encoding = (Dwarf_Word) -1;
3196 dwarf_formudata (dwarf_attr_integrate (&type, DW_AT_encoding, &attr),
3197 &encoding);
3198 switch (encoding)
3199 {
3200 case DW_ATE_float:
3201 case DW_ATE_complex_float:
3202 // XXX need a warning?
3203 // throw semantic_error ("unsupported bitfield type (encoding " + lex_cast(encoding)
3204 // + ") for " + dwarf_type_name(&type), e->tok);
3205 pf->raw_components.append("?");
3206 break;
3207
3208 case DW_ATE_unsigned:
3209 case DW_ATE_unsigned_char:
3210 push_deref (pf, "%u", e);
3211 break;
3212
3213 case DW_ATE_signed:
3214 case DW_ATE_signed_char:
3215 default:
3216 push_deref (pf, "%i", e);
3217 break;
3218 }
3219 }
3220
3221
3222 void
3223 dwarf_pretty_print::recurse_base (Dwarf_Die* type, target_symbol* e,
3224 print_format* pf)
3225 {
3226 Dwarf_Attribute attr;
3227 Dwarf_Word encoding = (Dwarf_Word) -1;
3228 dwarf_formudata (dwarf_attr_integrate (type, DW_AT_encoding, &attr),
3229 &encoding);
3230 switch (encoding)
3231 {
3232 case DW_ATE_float:
3233 case DW_ATE_complex_float:
3234 // XXX need a warning?
3235 // throw semantic_error ("unsupported type (encoding " + lex_cast(encoding)
3236 // + ") for " + dwarf_type_name(type), e->tok);
3237 pf->raw_components.append("?");
3238 break;
3239
3240 case DW_ATE_UTF: // XXX need to add unicode to _stp_vsprint_char
3241 case DW_ATE_signed_char:
3242 case DW_ATE_unsigned_char:
3243 // Use escapes to make sure that non-printable characters
3244 // don't interrupt our stream (especially '\0' values).
3245 push_deref (pf, "'%#c'", e);
3246 break;
3247
3248 case DW_ATE_unsigned:
3249 push_deref (pf, "%u", e);
3250 break;
3251
3252 case DW_ATE_signed:
3253 default:
3254 push_deref (pf, "%i", e);
3255 break;
3256 }
3257 }
3258
3259
3260 void
3261 dwarf_pretty_print::recurse_array (Dwarf_Die* type, target_symbol* e,
3262 print_format* pf, bool top)
3263 {
3264 if (!top && !print_full)
3265 {
3266 pf->raw_components.append("[...]");
3267 return;
3268 }
3269
3270 Dwarf_Die childtype;
3271 dwarf_attr_die (type, DW_AT_type, &childtype);
3272
3273 if (print_chars (&childtype, e, pf))
3274 return;
3275
3276 pf->raw_components.append("[");
3277
3278 // We print the array up to the first 5 elements.
3279 // XXX how can we determine the array size?
3280 // ... for now, just print the first element
3281 // NB: limit to 32 args; see PR10750 and c_unparser::visit_print_format.
3282 unsigned i, size = 1;
3283 for (i=0; i < size && i < 5 && pf->args.size() < 32; ++i)
3284 {
3285 if (i > 0)
3286 pf->raw_components.append(", ");
3287 target_symbol* e2 = new target_symbol(*e);
3288 e2->components.push_back (target_symbol::component(e->tok, i));
3289 recurse (&childtype, e2, pf);
3290 }
3291 if (i < size || 1/*XXX until real size is known */)
3292 pf->raw_components.append(", ...");
3293 pf->raw_components.append("]");
3294 }
3295
3296
3297 void
3298 dwarf_pretty_print::recurse_pointer (Dwarf_Die* type, target_symbol* e,
3299 print_format* pf, bool top)
3300 {
3301 // We chase to top-level pointers, but leave the rest alone
3302 bool void_p = true;
3303 Dwarf_Die pointee;
3304 if (dwarf_attr_die (type, DW_AT_type, &pointee))
3305 {
3306 try
3307 {
3308 dw.resolve_unqualified_inner_typedie (&pointee, &pointee, e);
3309 void_p = false;
3310 }
3311 catch (const semantic_error&) {}
3312 }
3313
3314 if (!void_p)
3315 {
3316 if (print_chars (&pointee, e, pf))
3317 return;
3318
3319 if (top)
3320 {
3321 recurse (&pointee, e, pf, top);
3322 return;
3323 }
3324 }
3325
3326 push_deref (pf, "%p", e);
3327 }
3328
3329
3330 void
3331 dwarf_pretty_print::recurse_struct (Dwarf_Die* type, target_symbol* e,
3332 print_format* pf, bool top)
3333 {
3334 if (dwarf_hasattr(type, DW_AT_declaration))
3335 {
3336 Dwarf_Die *resolved = dw.declaration_resolve(type);
3337 if (!resolved)
3338 {
3339 // could be an error, but for now just stub it
3340 // throw semantic_error ("unresolved " + dwarf_type_name(type), e->tok);
3341 pf->raw_components.append("{...}");
3342 return;
3343 }
3344 type = resolved;
3345 }
3346
3347 int count = 0;
3348 pf->raw_components.append("{");
3349 if (top || print_full)
3350 recurse_struct_members (type, e, pf, count);
3351 else
3352 pf->raw_components.append("...");
3353 pf->raw_components.append("}");
3354 }
3355
3356
3357 void
3358 dwarf_pretty_print::recurse_struct_members (Dwarf_Die* type, target_symbol* e,
3359 print_format* pf, int& count)
3360 {
3361 /* With inheritance, a subclass may mask member names of parent classes, so
3362 * our search among the inheritance tree must be breadth-first rather than
3363 * depth-first (recursive). The type die is still our starting point. When
3364 * we encounter a masked name, just skip it. */
3365 set<string> dupes;
3366 deque<Dwarf_Die> inheritees(1, *type);
3367 for (; !inheritees.empty(); inheritees.pop_front())
3368 {
3369 Dwarf_Die child, childtype, import;
3370 if (dwarf_child (&inheritees.front(), &child) == 0)
3371 do
3372 {
3373 target_symbol* e2 = e;
3374
3375 // skip static members
3376 if (dwarf_hasattr(&child, DW_AT_declaration))
3377 continue;
3378
3379 int tag = dwarf_tag (&child);
3380
3381 /* Pretend imported units contain members by recursing into
3382 struct_member printing with the same count. */
3383 if (tag == DW_TAG_imported_unit
3384 && dwarf_attr_die (&child, DW_AT_import, &import))
3385 recurse_struct_members (&import, e2, pf, count);
3386
3387 if (tag != DW_TAG_member && tag != DW_TAG_inheritance)
3388 continue;
3389
3390 dwarf_attr_die (&child, DW_AT_type, &childtype);
3391
3392 if (tag == DW_TAG_inheritance)
3393 {
3394 inheritees.push_back(childtype);
3395 continue;
3396 }
3397
3398 int childtag = dwarf_tag (&childtype);
3399 const char *member = dwarf_diename (&child);
3400
3401 // "_vptr.foo" members are C++ virtual function tables,
3402 // which (generally?) aren't interesting for users.
3403 if (member && startswith(member, "_vptr."))
3404 continue;
3405
3406 // skip inheritance-masked duplicates
3407 if (member && !dupes.insert(member).second)
3408 continue;
3409
3410 if (++count > 1)
3411 pf->raw_components.append(", ");
3412
3413 // NB: limit to 32 args; see PR10750 and c_unparser::visit_print_format.
3414 if (pf->args.size() >= 32)
3415 {
3416 pf->raw_components.append("...");
3417 break;
3418 }
3419
3420 if (member)
3421 {
3422 pf->raw_components.append(".");
3423 pf->raw_components.append(member);
3424
3425 e2 = new target_symbol(*e);
3426 e2->components.push_back (target_symbol::component(e->tok, member));
3427 }
3428 else if (childtag == DW_TAG_union_type)
3429 pf->raw_components.append("<union>");
3430 else if (childtag == DW_TAG_structure_type)
3431 pf->raw_components.append("<class>");
3432 else if (childtag == DW_TAG_class_type)
3433 pf->raw_components.append("<struct>");
3434 pf->raw_components.append("=");
3435
3436 if (dwarf_hasattr_integrate (&child, DW_AT_bit_offset))
3437 recurse_bitfield (&childtype, e2, pf);
3438 else
3439 recurse (&childtype, e2, pf);
3440 }
3441 while (dwarf_siblingof (&child, &child) == 0);
3442 }
3443 }
3444
3445
3446 bool
3447 dwarf_pretty_print::print_chars (Dwarf_Die* start_type, target_symbol* e,
3448 print_format* pf)
3449 {
3450 Dwarf_Die type;
3451 dw.resolve_unqualified_inner_typedie (start_type, &type, e);
3452
3453 Dwarf_Attribute attr;
3454 Dwarf_Word encoding = (Dwarf_Word) -1;
3455 dwarf_formudata (dwarf_attr_integrate (&type, DW_AT_encoding, &attr),
3456 &encoding);
3457 switch (encoding)
3458 {
3459 case DW_ATE_UTF:
3460 case DW_ATE_signed_char:
3461 case DW_ATE_unsigned_char:
3462 break;
3463 default:
3464 return false;
3465 }
3466
3467 string function = userspace_p ? "user_string_quoted" : "kernel_string_quoted";
3468 Dwarf_Word size = (Dwarf_Word) -1;
3469 dwarf_formudata (dwarf_attr_integrate (&type, DW_AT_byte_size, &attr), &size);
3470 switch (size)
3471 {
3472 case 1:
3473 break;
3474 case 2:
3475 function += "_utf16";
3476 break;
3477 case 4:
3478 function += "_utf32";
3479 break;
3480 default:
3481 return false;
3482 }
3483
3484 if (push_deref (pf, "%s", e))
3485 {
3486 // steal the last arg for a string access
3487 assert (!pf->args.empty());
3488 functioncall* fcall = new functioncall;
3489 fcall->tok = e->tok;
3490 fcall->function = function;
3491 fcall->args.push_back (pf->args.back());
3492 pf->args.back() = fcall;
3493 }
3494 return true;
3495 }
3496
3497 // PR10601: adapt to kernel-vs-userspace loc2c-runtime
3498 static const string EMBEDDED_FETCH_DEREF_KERNEL = string("\n")
3499 + "#define fetch_register k_fetch_register\n"
3500 + "#define store_register k_store_register\n"
3501 + "#define deref kderef\n"
3502 + "#define store_deref store_kderef\n";
3503
3504 static const string EMBEDDED_FETCH_DEREF_USER = string("\n")
3505 + "#define fetch_register u_fetch_register\n"
3506 + "#define store_register u_store_register\n"
3507 + "#define deref uderef\n"
3508 + "#define store_deref store_uderef\n";
3509
3510 #define EMBEDDED_FETCH_DEREF(U) \
3511 (U ? EMBEDDED_FETCH_DEREF_USER : EMBEDDED_FETCH_DEREF_KERNEL)
3512
3513 static const string EMBEDDED_FETCH_DEREF_DONE = string("\n")
3514 + "#undef fetch_register\n"
3515 + "#undef store_register\n"
3516 + "#undef deref\n"
3517 + "#undef store_deref\n";
3518
3519 static functioncall*
3520 synthetic_embedded_deref_call(dwflpp& dw,
3521 Dwarf_Die* function_type,
3522 const string& function_name,
3523 const string& function_code,
3524 bool userspace_p,
3525 bool lvalue_p,
3526 target_symbol* e,
3527 expression* pointer=NULL)
3528 {
3529 // Synthesize a functiondecl for the given embedded code string.
3530 string fhash = detox_path(string(e->tok->location.file->name));
3531 functiondecl *fdecl = new functiondecl;
3532 fdecl->synthetic = true;
3533 fdecl->tok = e->tok;
3534 fdecl->name = "__private_" + fhash + function_name;
3535 // The fdecl type is generic, but we'll be detailed on the fcall below.
3536 fdecl->type = pe_long;
3537 fdecl->type_details.reset(new exp_type_dwarf(&dw, function_type,
3538 userspace_p, e->addressof));
3539
3540 embeddedcode *ec = new embeddedcode;
3541 ec->tok = e->tok;
3542 string code;
3543 code += "/* unprivileged */";
3544 if (! lvalue_p)
3545 code += "/* pure */";
3546 code += EMBEDDED_FETCH_DEREF(userspace_p);
3547 code += function_code;
3548 code += EMBEDDED_FETCH_DEREF_DONE;
3549 fdecl->body = ec;
3550
3551 // Synthesize a functioncall.
3552 functioncall* fcall = new functioncall;
3553 fcall->tok = e->tok;
3554 fcall->referent = fdecl;
3555 fcall->function = fdecl->name;
3556 fcall->type = fdecl->type;
3557 fcall->type_details = fdecl->type_details;
3558
3559 // If this code snippet uses a precomputed pointer,
3560 // pass that as the first argument.
3561 if (pointer)
3562 {
3563 vardecl *v = new vardecl;
3564 v->type = pe_long;
3565 v->name = "pointer";
3566 v->tok = e->tok;
3567 fdecl->formal_args.push_back(v);
3568 fcall->args.push_back(pointer);
3569 }
3570
3571 // Any non-literal indexes need to be passed as arguments too.
3572 for (unsigned i = 0; i < e->components.size(); ++i)
3573 if (e->components[i].type == target_symbol::comp_expression_array_index)
3574 {
3575 vardecl *v = new vardecl;
3576 v->type = pe_long;
3577 v->name = "index" + lex_cast(i);
3578 v->tok = e->tok;
3579 fdecl->formal_args.push_back(v);
3580 fcall->args.push_back(e->components[i].expr_index);
3581 }
3582
3583 // If this code snippet is assigning to an lvalue,
3584 // add a final argument for the rvalue.
3585 if (lvalue_p)
3586 {
3587 // Modify the fdecl so it carries a single pe_long formal
3588 // argument called "value".
3589
3590 // FIXME: For the time being we only support setting target
3591 // variables which have base types; these are 'pe_long' in
3592 // stap's type vocabulary. Strings and pointers might be
3593 // reasonable, some day, but not today.
3594
3595 vardecl *v = new vardecl;
3596 v->type = pe_long;
3597 v->name = "value";
3598 v->tok = e->tok;
3599 fdecl->formal_args.push_back(v);
3600 // NB: We don't know the value for fcall argument yet.
3601 // (see target_symbol_setter_functioncalls)
3602 }
3603
3604 if (!dw.sess.guru_mode && fdecl->formal_args.empty())
3605 code += "/* stable */";
3606 ec->code = code;
3607
3608 // Add the synthesized decl to the session, and return the call.
3609 fdecl->join (dw.sess);
3610 return fcall;
3611 }
3612
3613 expression*
3614 dwarf_pretty_print::deref (target_symbol* e)
3615 {
3616 static unsigned tick = 0;
3617
3618 if (!deref_p)
3619 {
3620 assert (pointer && e->components.empty());
3621 return pointer;
3622 }
3623
3624 bool lvalue_p = false;
3625 string name = "_dwarf_pretty_print_deref_" + lex_cast(tick++);
3626
3627 string code;
3628 Dwarf_Die endtype;
3629 if (pointer)
3630 code = dw.literal_stmt_for_pointer (&pointer_type, e, false, &endtype);
3631 else if (!local.empty())
3632 code = dw.literal_stmt_for_local (scopes, pc, local, e, false, &endtype);
3633 else
3634 code = dw.literal_stmt_for_return (&scopes[0], pc, e, false, &endtype);
3635
3636 return synthetic_embedded_deref_call(dw, &endtype, name, code,
3637 userspace_p, lvalue_p, e, pointer);
3638 }
3639
3640
3641 bool
3642 dwarf_pretty_print::push_deref (print_format* pf, const string& fmt,
3643 target_symbol* e)
3644 {
3645 expression* e2 = NULL;
3646 try
3647 {
3648 e2 = deref (e);
3649 }
3650 catch (const semantic_error&)
3651 {
3652 pf->raw_components.append ("?");
3653 return false;
3654 }
3655 pf->raw_components.append (fmt);
3656 pf->args.push_back (e2);
3657 return true;
3658 }
3659
3660
3661 void
3662 dwarf_var_expanding_visitor::visit_target_symbol_saved_return (target_symbol* e)
3663 {
3664 // Get the full name of the target symbol.
3665 stringstream ts_name_stream;
3666 e->print(ts_name_stream);
3667 string ts_name = ts_name_stream.str();
3668
3669 // Check and make sure we haven't already seen this target
3670 // variable in this return probe. If we have, just return our
3671 // last replacement.
3672 unordered_map<string, expression *>::iterator i = return_ts_map.find(ts_name);
3673 if (i != return_ts_map.end())
3674 {
3675 provide (i->second);
3676 return;
3677 }
3678
3679 // Attempt the expansion directly first, so if there's a problem with the
3680 // variable we won't have a bogus entry probe lying around. Like in
3681 // saveargs(), we pretend for a moment that we're not in a .return.
3682 bool saved_has_return = q.has_return;
3683 q.has_return = false;
3684 expression *repl = e;
3685 replace (repl);
3686 q.has_return = saved_has_return;
3687
3688 // If it's still a target_symbol, then it couldn't be resolved. It may
3689 // not have a saved_conversion_error yet, e.g. for null_die(scope_die),
3690 // but we know it's not worth making that bogus entry anyway.
3691 if (dynamic_cast<target_symbol*>(repl))
3692 {
3693 provide (repl);
3694 return;
3695 }
3696
3697 expression *exp;
3698 if (!q.has_process &&
3699 strverscmp(q.sess.kernel_base_release.c_str(), "2.6.25") >= 0)
3700 exp = gen_kretprobe_saved_return(repl);
3701 else
3702 exp = gen_mapped_saved_return(repl, e->sym_name());
3703
3704 // Propagate the DWARF type to the expression in the return probe.
3705 if (repl->type_details && !exp->type_details)
3706 exp->type_details = repl->type_details;
3707
3708 // Provide the variable to our parent so it can be used as a
3709 // substitute for the target symbol.
3710 provide (exp);
3711
3712 // Remember this replacement since we might be able to reuse
3713 // it later if the same return probe references this target
3714 // symbol again.
3715 return_ts_map[ts_name] = exp;
3716 }
3717
3718 static expression*
3719 gen_mapped_saved_return(systemtap_session &sess, expression* e,
3720 const string& name,
3721 block *& add_block, bool& add_block_tid,
3722 block *& add_call_probe, bool& add_call_probe_tid)
3723 {
3724 static unsigned tick = 0;
3725
3726 // We've got to do several things here to handle target
3727 // variables in return probes.
3728
3729 // (1) Synthesize two global arrays. One is the cache of the
3730 // target variable and the other contains a thread specific
3731 // nesting level counter. The arrays will look like
3732 // this:
3733 //
3734 // _entry_tvar_{name}_{num}
3735 // _entry_tvar_{name}_{num}_ctr
3736
3737 string aname = (string("__global_entry_tvar_")
3738 + name
3739 + "_" + lex_cast(tick++));
3740 vardecl* vd = new vardecl;
3741 vd->name = aname;
3742 vd->tok = e->tok;
3743 sess.globals.push_back (vd);
3744
3745 string ctrname = aname + "_ctr";
3746 vd = new vardecl;
3747 vd->name = ctrname;
3748 vd->tok = e->tok;
3749 sess.globals.push_back (vd);
3750
3751 // (2) Create a new code block we're going to insert at the
3752 // beginning of this probe to get the cached value into a
3753 // temporary variable. We'll replace the target variable
3754 // reference with the temporary variable reference. The code
3755 // will look like this:
3756 //
3757 // _entry_tvar_tid = tid()
3758 // _entry_tvar_{name}_{num}_tmp
3759 // = _entry_tvar_{name}_{num}[_entry_tvar_tid,
3760 // _entry_tvar_{name}_{num}_ctr[_entry_tvar_tid]]
3761 // delete _entry_tvar_{name}_{num}[_entry_tvar_tid,
3762 // _entry_tvar_{name}_{num}_ctr[_entry_tvar_tid]--]
3763 // if (! _entry_tvar_{name}_{num}_ctr[_entry_tvar_tid])
3764 // delete _entry_tvar_{name}_{num}_ctr[_entry_tvar_tid]
3765
3766 // (2a) Synthesize the tid temporary expression, which will look
3767 // like this:
3768 //
3769 // _entry_tvar_tid = tid()
3770 symbol* tidsym = new symbol;
3771 tidsym->name = string("_entry_tvar_tid");
3772 tidsym->tok = e->tok;
3773
3774 if (add_block == NULL)
3775 {
3776 add_block = new block;
3777 add_block->tok = e->tok;
3778 }
3779
3780 if (!add_block_tid)
3781 {
3782 // Synthesize a functioncall to grab the thread id.
3783 functioncall* fc = new functioncall;
3784 fc->tok = e->tok;
3785 fc->function = string("tid");
3786
3787 // Assign the tid to '_entry_tvar_tid'.
3788 assignment* a = new assignment;
3789 a->tok = e->tok;
3790 a->op = "=";
3791 a->left = tidsym;
3792 a->right = fc;
3793
3794 expr_statement* es = new expr_statement;
3795 es->tok = e->tok;
3796 es->value = a;
3797 add_block->statements.push_back (es);
3798 add_block_tid = true;
3799 }
3800
3801 // (2b) Synthesize an array reference and assign it to a
3802 // temporary variable (that we'll use as replacement for the
3803 // target variable reference). It will look like this:
3804 //
3805 // _entry_tvar_{name}_{num}_tmp
3806 // = _entry_tvar_{name}_{num}[_entry_tvar_tid,
3807 // _entry_tvar_{name}_{num}_ctr[_entry_tvar_tid]]
3808
3809 arrayindex* ai_tvar_base = new arrayindex;
3810 ai_tvar_base->tok = e->tok;
3811
3812 symbol* sym = new symbol;
3813 sym->name = aname;
3814 sym->tok = e->tok;
3815 ai_tvar_base->base = sym;
3816
3817 ai_tvar_base->indexes.push_back(tidsym);
3818
3819 // We need to create a copy of the array index in its current
3820 // state so we can have 2 variants of it (the original and one
3821 // that post-decrements the second index).
3822 arrayindex* ai_tvar = new arrayindex;
3823 arrayindex* ai_tvar_postdec = new arrayindex;
3824 *ai_tvar = *ai_tvar_base;
3825 *ai_tvar_postdec = *ai_tvar_base;
3826
3827 // Synthesize the
3828 // "_entry_tvar_{name}_{num}_ctr[_entry_tvar_tid]" used as the
3829 // second index into the array.
3830 arrayindex* ai_ctr = new arrayindex;
3831 ai_ctr->tok = e->tok;
3832
3833 sym = new symbol;
3834 sym->name = ctrname;
3835 sym->tok = e->tok;
3836 ai_ctr->base = sym;
3837 ai_ctr->indexes.push_back(tidsym);
3838 ai_tvar->indexes.push_back(ai_ctr);
3839
3840 symbol* tmpsym = new symbol;
3841 tmpsym->name = aname + "_tmp";
3842 tmpsym->tok = e->tok;
3843
3844 assignment* a = new assignment;
3845 a->tok = e->tok;
3846 a->op = "=";
3847 a->left = tmpsym;
3848 a->right = ai_tvar;
3849
3850 expr_statement* es = new expr_statement;
3851 es->tok = e->tok;
3852 es->value = a;
3853
3854 add_block->statements.push_back (es);
3855
3856 // (2c) Add a post-decrement to the second array index and
3857 // delete the array value. It will look like this:
3858 //
3859 // delete _entry_tvar_{name}_{num}[_entry_tvar_tid,
3860 // _entry_tvar_{name}_{num}_ctr[_entry_tvar_tid]--]
3861
3862 post_crement* pc = new post_crement;
3863 pc->tok = e->tok;
3864 pc->op = "--";
3865 pc->operand = ai_ctr;
3866 ai_tvar_postdec->indexes.push_back(pc);
3867
3868 delete_statement* ds = new delete_statement;
3869 ds->tok = e->tok;
3870 ds->value = ai_tvar_postdec;
3871
3872 add_block->statements.push_back (ds);
3873
3874 // (2d) Delete the counter value if it is 0. It will look like
3875 // this:
3876 // if (! _entry_tvar_{name}_{num}_ctr[_entry_tvar_tid])
3877 // delete _entry_tvar_{name}_{num}_ctr[_entry_tvar_tid]
3878
3879 ds = new delete_statement;
3880 ds->tok = e->tok;
3881 ds->value = ai_ctr;
3882
3883 unary_expression *ue = new unary_expression;
3884 ue->tok = e->tok;
3885 ue->op = "!";
3886 ue->operand = ai_ctr;
3887
3888 if_statement *ifs = new if_statement;
3889 ifs->tok = e->tok;
3890 ifs->condition = ue;
3891 ifs->thenblock = ds;
3892 ifs->elseblock = NULL;
3893
3894 add_block->statements.push_back (ifs);
3895
3896 // (3) We need an entry probe that saves the value for us in the
3897 // global array we created. Create the entry probe, which will
3898 // look like this:
3899 //
3900 // probe kernel.function("{function}").call {
3901 // _entry_tvar_tid = tid()
3902 // _entry_tvar_{name}_{num}[_entry_tvar_tid,
3903 // ++_entry_tvar_{name}_{num}_ctr[_entry_tvar_tid]]
3904 // = ${param}
3905 // }
3906
3907 if (add_call_probe == NULL)
3908 {
3909 add_call_probe = new block;
3910 add_call_probe->tok = e->tok;
3911 }
3912
3913 if (!add_call_probe_tid)
3914 {
3915 // Synthesize a functioncall to grab the thread id.
3916 functioncall* fc = new functioncall;
3917 fc->tok = e->tok;
3918 fc->function = string("tid");
3919
3920 // Assign the tid to '_entry_tvar_tid'.
3921 assignment* a = new assignment;
3922 a->tok = e->tok;
3923 a->op = "=";
3924 a->left = tidsym;
3925 a->right = fc;
3926
3927 expr_statement* es = new expr_statement;
3928 es->tok = e->tok;
3929 es->value = a;
3930 add_call_probe = new block(add_call_probe, es);
3931 add_call_probe_tid = true;
3932 }
3933
3934 // Save the value, like this:
3935 // _entry_tvar_{name}_{num}[_entry_tvar_tid,
3936 // ++_entry_tvar_{name}_{num}_ctr[_entry_tvar_tid]]
3937 // = ${param}
3938 arrayindex* ai_tvar_preinc = new arrayindex;
3939 *ai_tvar_preinc = *ai_tvar_base;
3940
3941 pre_crement* preinc = new pre_crement;
3942 preinc->tok = e->tok;
3943 preinc->op = "++";
3944 preinc->operand = ai_ctr;
3945 ai_tvar_preinc->indexes.push_back(preinc);
3946
3947 a = new assignment;
3948 a->tok = e->tok;
3949 a->op = "=";
3950 a->left = ai_tvar_preinc;
3951 a->right = e;
3952
3953 es = new expr_statement;
3954 es->tok = e->tok;
3955 es->value = a;
3956
3957 add_call_probe = new block(add_call_probe, es);
3958
3959 // (4) Provide the '_entry_tvar_{name}_{num}_tmp' variable to
3960 // our parent so it can be used as a substitute for the target
3961 // symbol.
3962 delete ai_tvar_base;
3963 return tmpsym;
3964 }
3965
3966
3967 expression*
3968 dwarf_var_expanding_visitor::gen_mapped_saved_return(expression* e,
3969 const string& name)
3970 {
3971 return ::gen_mapped_saved_return(q.sess, e, name, add_block,
3972 add_block_tid, add_call_probe,
3973 add_call_probe_tid);
3974 }
3975
3976
3977 expression*
3978 dwarf_var_expanding_visitor::gen_kretprobe_saved_return(expression* e)
3979 {
3980 // The code for this is simple.
3981 //
3982 // .call:
3983 // _set_kretprobe_long(index, $value)
3984 //
3985 // .return:
3986 // _get_kretprobe_long(index)
3987 //
3988 // (or s/long/string/ for things like $$parms)
3989
3990 unsigned index;
3991 string setfn, getfn;
3992
3993 // We need the caller to predetermine the type of the expression!
3994 switch (e->type)
3995 {
3996 case pe_string:
3997 index = saved_strings++;
3998 setfn = "_set_kretprobe_string";
3999 getfn = "_get_kretprobe_string";
4000 break;
4001 case pe_long:
4002 index = saved_longs++;
4003 setfn = "_set_kretprobe_long";
4004 getfn = "_get_kretprobe_long";
4005 break;
4006 default:
4007 throw SEMANTIC_ERROR(_("unknown type to save in kretprobe"), e->tok);
4008 }
4009
4010 // Create the entry code
4011 // _set_kretprobe_{long|string}(index, $value)
4012
4013 if (add_call_probe == NULL)
4014 {
4015 add_call_probe = new block;
4016 add_call_probe->tok = e->tok;
4017 }
4018
4019 functioncall* set_fc = new functioncall;
4020 set_fc->tok = e->tok;
4021 set_fc->function = setfn;
4022 set_fc->args.push_back(new literal_number(index));
4023 set_fc->args.back()->tok = e->tok;
4024 set_fc->args.push_back(e);
4025
4026 expr_statement* set_es = new expr_statement;
4027 set_es->tok = e->tok;
4028 set_es->value = set_fc;
4029
4030 add_call_probe->statements.push_back(set_es);
4031
4032 // Create the return code
4033 // _get_kretprobe_{long|string}(index)
4034
4035 functioncall* get_fc = new functioncall;
4036 get_fc->tok = e->tok;
4037 get_fc->function = getfn;
4038 get_fc->args.push_back(new literal_number(index));
4039 get_fc->args.back()->tok = e->tok;
4040
4041 return get_fc;
4042 }
4043
4044
4045 void
4046 dwarf_var_expanding_visitor::visit_target_symbol_context (target_symbol* e)
4047 {
4048 if (pending_interrupts) {
4049 provide(e);
4050 return;
4051 }
4052
4053 if (null_die(scope_die)) {
4054 literal_string *empty = new literal_string(string(""));
4055 empty->tok = e->tok;
4056 provide(empty);
4057 return;
4058 }
4059
4060 target_symbol *tsym = new target_symbol(*e);
4061
4062 bool pretty = e->check_pretty_print ();
4063 string format = pretty ? "=%s" : "=%#x";
4064
4065 // Convert $$parms to sprintf of a list of parms and active local vars
4066 // which we recursively evaluate
4067
4068 print_format* pf = print_format::create(e->tok, "sprintf");
4069
4070 if (q.has_return && (e->name == "$$return"))
4071 {
4072 tsym->name = "$return";
4073
4074 // Ignore any variable that isn't accessible.
4075 tsym->saved_conversion_error = 0;
4076 expression *texp = tsym;
4077 replace (texp); // NB: throws nothing ...
4078 if (tsym->saved_conversion_error) // ... but this is how we know it happened.
4079 {
4080
4081 }
4082 else
4083 {
4084 pf->raw_components += "return";
4085 pf->raw_components += format;
4086 pf->args.push_back(texp);
4087 }
4088 }
4089 else
4090 {
4091 // non-.return probe: support $$parms, $$vars, $$locals
4092 bool first = true;
4093 Dwarf_Die result;
4094 vector<Dwarf_Die> scopes = q.dw.getscopes(scope_die);
4095 for (unsigned i = 0; i < scopes.size(); ++i)
4096 {
4097 if (dwarf_tag(&scopes[i]) == DW_TAG_compile_unit)
4098 break; // we don't want file-level variables
4099 if (dwarf_child (&scopes[i], &result) == 0)
4100 do
4101 {
4102 switch (dwarf_tag (&result))
4103 {
4104 case DW_TAG_variable:
4105 if (e->name == "$$parms")
4106 continue;
4107 break;
4108 case DW_TAG_formal_parameter:
4109 if (e->name == "$$locals")
4110 continue;
4111 break;
4112
4113 default:
4114 continue;
4115 }
4116
4117 const char *diename = dwarf_diename (&result);
4118 if (! diename) continue;
4119
4120 if (! first)
4121 pf->raw_components += " ";
4122 pf->raw_components += diename;
4123 first = false;
4124
4125 // Write a placeholder for ugly aggregates
4126 Dwarf_Die type;
4127 if (!pretty && dwarf_attr_die(&result, DW_AT_type, &type))
4128 {
4129 q.dw.resolve_unqualified_inner_typedie(&type, &type, e);
4130 switch (dwarf_tag(&type))
4131 {
4132 case DW_TAG_union_type:
4133 case DW_TAG_structure_type:
4134 case DW_TAG_class_type:
4135 pf->raw_components += "={...}";
4136 continue;
4137
4138 case DW_TAG_array_type:
4139 pf->raw_components += "=[...]";
4140 continue;
4141 }
4142 }
4143
4144 tsym->name = string("$") + diename;
4145
4146 // Ignore any variable that isn't accessible.
4147 tsym->saved_conversion_error = 0;
4148 expression *texp = tsym;
4149 replace (texp); // NB: throws nothing ...
4150 if (tsym->saved_conversion_error) // ... but this is how we know it happened.
4151 {
4152 if (q.sess.verbose>2)
4153 {
4154 for (const semantic_error *c = tsym->saved_conversion_error;
4155 c != 0;
4156 c = c->get_chain()) {
4157 clog << _("variable location problem [man error::dwarf]: ") << c->what() << endl;
4158 }
4159 }
4160
4161 pf->raw_components += "=?";
4162 }
4163 else
4164 {
4165 pf->raw_components += format;
4166 pf->args.push_back(texp);
4167 }
4168 }
4169 while (dwarf_siblingof (&result, &result) == 0);
4170 }
4171 }
4172
4173 pf->components = print_format::string_to_components(pf->raw_components);
4174 pf->type = pe_string;
4175 provide (pf);
4176 }
4177
4178
4179 void
4180 dwarf_var_expanding_visitor::visit_atvar_op (atvar_op *e)
4181 {
4182 // Fill in our current module context if needed
4183 if (e->module.empty())
4184 e->module = q.dw.module_name;
4185
4186 if (e->module == q.dw.module_name && e->cu_name.empty())
4187 {
4188 // process like any other local
4189 // e->sym_name() will do the right thing
4190 visit_target_symbol(e);
4191 return;
4192 }
4193
4194 var_expanding_visitor::visit_atvar_op(e);
4195 }
4196
4197
4198 void
4199 dwarf_var_expanding_visitor::visit_target_symbol (target_symbol *e)
4200 {
4201 assert(e->name.size() > 0 && (e->name[0] == '$' || e->name == "@var"));
4202 visited = true;
4203 bool defined_being_checked = (defined_ops.size() > 0 && (defined_ops.top()->operand == e));
4204 // In this mode, we avoid hiding errors or generating extra code such as for .return saved $vars
4205
4206 try
4207 {
4208 bool lvalue = is_active_lvalue(e);
4209 if (lvalue && !q.sess.guru_mode)
4210 throw SEMANTIC_ERROR(_("write to target variable not permitted; need stap -g"), e->tok);
4211
4212 // XXX: process $context vars should be writable
4213
4214 // See if we need to generate a new probe to save/access function
4215 // parameters from a return probe. PR 1382.
4216 if (q.has_return
4217 && !defined_being_checked
4218 && e->name != "$return" // not the special return-value variable handled below
4219 && e->name != "$$return") // nor the other special variable handled below
4220 {
4221 if (lvalue)
4222 throw SEMANTIC_ERROR(_("write to target variable not permitted in .return probes"), e->tok);
4223 visit_target_symbol_saved_return(e);
4224 return;
4225 }
4226
4227 if (e->name == "$$vars" || e->name == "$$parms" || e->name == "$$locals"
4228 || (q.has_return && (e->name == "$$return")))
4229 {
4230 if (lvalue)
4231 throw SEMANTIC_ERROR(_("cannot write to context variable"), e->tok);
4232
4233 if (e->addressof)
4234 throw SEMANTIC_ERROR(_("cannot take address of context variable"), e->tok);
4235
4236 e->assert_no_components("dwarf", true);
4237
4238 visit_target_symbol_context(e);
4239 return;
4240 }
4241
4242 // Everything else (pretty-printed vars, and context vars) require a
4243 // scope_die in which to search for them. If we don't have that, just
4244 // leave it unresolved; we'll produce an error later on.
4245 if (null_die(scope_die))
4246 {
4247 provide(e);
4248 return;
4249 }
4250
4251 if (e->check_pretty_print (lvalue))
4252 {
4253 if (q.has_return && (e->name == "$return"))
4254 {
4255 dwarf_pretty_print dpp (q.dw, scope_die, addr,
4256 q.has_process, *e);
4257 dpp.expand()->visit(this);
4258 }
4259 else
4260 {
4261 dwarf_pretty_print dpp (q.dw, getscopes(e), addr,
4262 e->sym_name(),
4263 q.has_process, *e);
4264 dpp.expand()->visit(this);
4265 }
4266 return;
4267 }
4268
4269 bool userspace_p = q.has_process;
4270 string fname = (string(lvalue ? "_dwarf_tvar_set" : "_dwarf_tvar_get")
4271 + "_" + escaped_indentifier_string (e->sym_name())
4272 + "_" + lex_cast(tick++));
4273
4274
4275 string code;
4276 Dwarf_Die endtype;
4277 if (q.has_return && (e->name == "$return"))
4278 code = q.dw.literal_stmt_for_return (scope_die, addr, e, lvalue, &endtype);
4279 else
4280 code = q.dw.literal_stmt_for_local (getscopes(e), addr, e->sym_name(),
4281 e, lvalue, &endtype);
4282
4283 functioncall* n = synthetic_embedded_deref_call(q.dw, &endtype, fname, code,
4284 userspace_p, lvalue, e);
4285
4286 if (lvalue)
4287 provide_lvalue_call (n);
4288
4289 // Revisit the functioncall so arguments can be expanded.
4290 n->visit (this);
4291 }
4292 catch (const semantic_error& er)
4293 {
4294 // We suppress this error message, and pass the unresolved
4295 // target_symbol to the next pass. We hope that this value ends
4296 // up not being referenced after all, so it can be optimized out
4297 // quietly.
4298 e->chain (er);
4299 provide (e);
4300 }
4301 }
4302
4303
4304 void
4305 dwarf_var_expanding_visitor::visit_cast_op (cast_op *e)
4306 {
4307 // Fill in our current module context if needed
4308 if (e->module.empty())
4309 e->module = q.dw.module_name;
4310
4311 var_expanding_visitor::visit_cast_op(e);
4312 }
4313
4314
4315 void
4316 dwarf_var_expanding_visitor::visit_entry_op (entry_op *e)
4317 {
4318 expression *repl = e;
4319 if (q.has_return)
4320 {
4321 // expand the operand as if it weren't a return probe
4322 q.has_return = false;
4323 replace (e->operand);
4324 q.has_return = true;
4325
4326 // XXX it would be nice to use gen_kretprobe_saved_return when available,
4327 // but it requires knowing the types already, which is problematic for
4328 // arbitrary expressons.
4329 repl = gen_mapped_saved_return (e->operand, "entry");
4330 }
4331 provide (repl);
4332 }
4333
4334 void
4335 dwarf_var_expanding_visitor::visit_perf_op (perf_op *e)
4336 {
4337 string e_lit_val = e->operand->value;
4338
4339 add_block = new block;
4340 add_block->tok = e->tok;
4341
4342 systemtap_session &s = this->q.sess;
4343 std::vector<std::pair<std::string,std::string> >::iterator it;
4344 // Find the associated perf.counter probe
4345 for (it=s.perf_counters.begin();
4346 it != s.perf_counters.end();
4347 it++)
4348 if ((*it).first == e_lit_val)
4349 {
4350 // if perf .process("name") omitted, then set it to this process name
4351 if ((*it).second.length() == 0)
4352 (*it).second = this->q.user_path;
4353 if ((*it).second == this->q.user_path)
4354 break;
4355 }
4356
4357 if (it != s.perf_counters.end())
4358 {
4359 perf_counter_refs.insert((*it).first);
4360 // __perf_read_N is assigned in the probe prologue
4361 symbol* sym = new symbol;
4362 sym->tok = e->tok;
4363 sym->name = "__perf_read_" + (*it).first;
4364 provide (sym);
4365 }
4366 else
4367 throw SEMANTIC_ERROR(_F("perf counter '%s' not defined", e_lit_val.c_str()));
4368 }
4369
4370
4371 vector<Dwarf_Die>&
4372 dwarf_var_expanding_visitor::getscopes(target_symbol *e)
4373 {
4374 if (scopes.empty())
4375 {
4376 if(!null_die(scope_die))
4377 scopes = q.dw.getscopes(scope_die);
4378 if (scopes.empty())
4379 //throw semantic_error (_F("unable to find any scopes containing %d", addr), e->tok);
4380 // ((scope_die == NULL) ? "" : (string (" in ") + (dwarf_diename(scope_die) ?: "<unknown>") + "(" + (dwarf_diename(q.dw.cu) ?: "<unknown>") ")" ))
4381 throw SEMANTIC_ERROR ("unable to find any scopes containing "
4382 + lex_cast_hex(addr)
4383 + (null_die(scope_die) ? ""
4384 : (string (" in ")
4385 + (dwarf_diename(scope_die) ?: "<unknown>")
4386 + "(" + (dwarf_diename(q.dw.cu) ?: "<unknown>")
4387 + ")"))
4388 + " while searching for local '"
4389 + e->sym_name() + "'",
4390 e->tok);
4391 }
4392 return scopes;
4393 }
4394
4395
4396 struct dwarf_cast_expanding_visitor: public var_expanding_visitor
4397 {
4398 systemtap_session& s;
4399 dwarf_builder& db;
4400 map<string,string> compiled_headers;
4401
4402 dwarf_cast_expanding_visitor(systemtap_session& s, dwarf_builder& db):
4403 s(s), db(db) {}
4404 void visit_cast_op (cast_op* e);
4405 void filter_special_modules(string& module);
4406 };
4407
4408
4409 struct dwarf_cast_query : public base_query
4410 {
4411 cast_op& e;
4412 const bool lvalue;
4413 const bool userspace_p;
4414 functioncall*& result;
4415
4416 dwarf_cast_query(dwflpp& dw, const string& module, cast_op& e, bool lvalue,
4417 const bool userspace_p, functioncall*& result):
4418 base_query(dw, module), e(e), lvalue(lvalue),
4419 userspace_p(userspace_p), result(result) {}
4420
4421 void handle_query_module();
4422 void query_library (const char *) {}
4423 void query_plt (const char *entry, size_t addr) {}
4424 };
4425
4426
4427 void
4428 dwarf_cast_query::handle_query_module()
4429 {
4430 static unsigned tick = 0;
4431
4432 if (result)
4433 return;
4434
4435 // look for the type in any CU
4436 Dwarf_Die* type_die = NULL;
4437 string tns = e.type_name;
4438
4439 if (startswith(tns, "class "))
4440 {
4441 // normalize to match dwflpp::global_alias_caching_callback
4442 string struct_name = "struct " + (string)e.type_name.substr(6);
4443 type_die = dw.declaration_resolve_other_cus(struct_name);
4444 }
4445 else
4446 type_die = dw.declaration_resolve_other_cus(tns);
4447
4448 // NB: We now index the types as "struct name"/"union name"/etc. instead of
4449 // just "name". But since we didn't require users to be explicit before, and
4450 // actually sort of discouraged it, we must be flexible now. So if a lookup
4451 // fails with a bare name, try augmenting it.
4452 if (!type_die &&
4453 !startswith(tns, "class ") &&
4454 !startswith(tns, "struct ") &&
4455 !startswith(tns, "union ") &&
4456 !startswith(tns, "enum "))
4457 {
4458 type_die = dw.declaration_resolve_other_cus("struct " + tns);
4459 if (!type_die)
4460 type_die = dw.declaration_resolve_other_cus("union " + tns);
4461 if (!type_die)
4462 type_die = dw.declaration_resolve_other_cus("enum " + tns);
4463 }
4464
4465 if (!type_die)
4466 return;
4467
4468 string code;
4469 Dwarf_Die endtype;
4470
4471 try
4472 {
4473 Dwarf_Die cu_mem;
4474 dw.focus_on_cu(dwarf_diecu(type_die, &cu_mem, NULL, NULL));
4475
4476 if (e.check_pretty_print (lvalue))
4477 {
4478 dwarf_pretty_print dpp(dw, type_die, e.operand, true, userspace_p, e);
4479 result = dpp.expand();
4480 return;
4481 }
4482
4483 code = dw.literal_stmt_for_pointer (type_die, &e, lvalue, &endtype);
4484 }
4485 catch (const semantic_error& er)
4486 {
4487 // NB: we can have multiple errors, since a @cast
4488 // may be attempted using several different modules:
4489 // @cast(ptr, "type", "module1:module2:...")
4490 e.chain (er);
4491 }
4492
4493 if (code.empty())
4494 return;
4495
4496 string fname = (string(lvalue ? "_dwarf_cast_set" : "_dwarf_cast_get")
4497 + "_" + e.sym_name()
4498 + "_" + lex_cast(tick++));
4499
4500 result = synthetic_embedded_deref_call(dw, &endtype, fname, code,
4501 userspace_p, lvalue, &e, e.operand);
4502 }
4503
4504
4505 void dwarf_cast_expanding_visitor::filter_special_modules(string& module)
4506 {
4507 // look for "<path/to/header>" or "kernel<path/to/header>"
4508 // for those cases, build a module including that header
4509 if (module[module.size() - 1] == '>' &&
4510 (module[0] == '<' || startswith(module, "kernel<")))
4511 {
4512 string header = module;
4513 map<string,string>::const_iterator it = compiled_headers.find(header);
4514 if (it != compiled_headers.end())
4515 {
4516 module = it->second;
4517 return;
4518 }
4519
4520 string cached_module;
4521 if (s.use_cache)
4522 {
4523 // see if the cached module exists
4524 cached_module = find_typequery_hash(s, module);
4525 if (!cached_module.empty() && !s.poison_cache)
4526 {
4527 int fd = open(cached_module.c_str(), O_RDONLY);
4528 if (fd != -1)
4529 {
4530 if (s.verbose > 2)
4531 //TRANSLATORS: Here we're using a cached module.
4532 clog << _("Pass 2: using cached ") << cached_module << endl;
4533 compiled_headers[header] = module = cached_module;
4534 close(fd);
4535 return;
4536 }
4537 }
4538 }
4539
4540 // no cached module, time to make it
4541 if (make_typequery(s, module) == 0)
4542 {
4543 // try to save typequery in the cache
4544 if (s.use_cache)
4545 copy_file(module, cached_module, s.verbose > 2);
4546 compiled_headers[header] = module;
4547 }
4548 }
4549 }
4550
4551
4552 void dwarf_cast_expanding_visitor::visit_cast_op (cast_op* e)
4553 {
4554 bool lvalue = is_active_lvalue(e);
4555 if (lvalue && !s.guru_mode)
4556 throw SEMANTIC_ERROR(_("write to @cast context variable not permitted; need stap -g"), e->tok);
4557
4558 if (e->module.empty())
4559 e->module = "kernel"; // "*" may also be reasonable to search all kernel modules
4560
4561 functioncall* result = NULL;
4562
4563 // split the module string by ':' for alternatives
4564 vector<string> modules;
4565 tokenize(e->module, modules, ":");
4566 bool userspace_p=false; // PR10601
4567 for (unsigned i = 0; !result && i < modules.size(); ++i)
4568 {
4569 string& module = modules[i];
4570 filter_special_modules(module);
4571
4572 // NB: This uses '/' to distinguish between kernel modules and userspace,
4573 // which means that userspace modules won't get any PATH searching.
4574 dwflpp* dw;
4575 try
4576 {
4577 userspace_p=is_user_module (module);
4578 if (! userspace_p)
4579 {
4580 // kernel or kernel module target
4581 dw = db.get_kern_dw(s, module);
4582 }
4583 else
4584 {
4585 module = find_executable (module, "", s.sysenv); // canonicalize it
4586 dw = db.get_user_dw(s, module);
4587 }
4588 }
4589 catch (const semantic_error& er)
4590 {
4591 /* ignore and go to the next module */
4592 continue;
4593 }
4594
4595 dwarf_cast_query q (*dw, module, *e, lvalue, userspace_p, result);
4596 dw->iterate_over_modules<base_query>(&query_module, &q);
4597 }
4598
4599 if (!result)
4600 {
4601 // We pass the unresolved cast_op to the next pass, and hope
4602 // that this value ends up not being referenced after all, so
4603 // it can be optimized out quietly.
4604 provide (e);
4605 return;
4606 }
4607
4608 if (lvalue)
4609 provide_lvalue_call (result);
4610
4611 result->visit (this);
4612 }
4613
4614
4615 static bool resolve_pointer_type(Dwarf_Die& die, bool& isptr);
4616
4617 exp_type_dwarf::exp_type_dwarf(dwflpp* dw, Dwarf_Die* die,
4618 bool userspace_p, bool addressof):
4619 dw(dw), die(*die), userspace_p(userspace_p), is_pointer(false)
4620 {
4621 // is_pointer tells us whether a value is a pointer to the given type, so we
4622 // can dereference it; otherwise it will be treated as an end point.
4623 if (addressof)
4624 // we're already looking at the pointed-to type
4625 is_pointer = true;
4626 else
4627 // use the same test as tracepoints to see what we have
4628 resolve_pointer_type(this->die, is_pointer);
4629 }
4630
4631
4632 functioncall *
4633 exp_type_dwarf::expand(autocast_op* e, bool lvalue)
4634 {
4635 static unsigned tick = 0;
4636
4637 try
4638 {
4639 // make sure we're not dereferencing base types or void
4640 bool deref_p = is_pointer && !null_die(&die);
4641 if (!deref_p)
4642 e->assert_no_components("autocast", true);
4643
4644 if (lvalue && !dw->sess.guru_mode)
4645 throw SEMANTIC_ERROR(_("write not permitted; need stap -g"), e->tok);
4646
4647 if (e->components.empty())
4648 {
4649 if (e->addressof)
4650 throw SEMANTIC_ERROR(_("cannot take address of tracepoint variable"), e->tok);
4651
4652 // no components and no addressof? how did this autocast come to be?
4653 throw SEMANTIC_ERROR(_("internal error: no-op autocast encountered"), e->tok);
4654 }
4655
4656 Dwarf_Die cu_mem;
4657 if (!null_die(&die))
4658 dw->focus_on_cu(dwarf_diecu(&die, &cu_mem, NULL, NULL));
4659
4660 if (e->check_pretty_print (lvalue))
4661 {
4662 dwarf_pretty_print dpp(*dw, &die, e->operand, deref_p, userspace_p, *e);
4663 return dpp.expand();
4664 }
4665
4666 Dwarf_Die endtype;
4667 string code = dw->literal_stmt_for_pointer (&die, e, lvalue, &endtype);
4668
4669 string fname = (string(lvalue ? "_dwarf_autocast_set" : "_dwarf_autocast_get")
4670 + "_" + lex_cast(tick++));
4671
4672 return synthetic_embedded_deref_call(*dw, &endtype, fname, code,
4673 userspace_p, lvalue, e, e->operand);
4674 }
4675 catch (const semantic_error &er)
4676 {
4677 e->chain (er);
4678 return NULL;
4679 }
4680 }
4681
4682
4683 struct dwarf_atvar_expanding_visitor: public var_expanding_visitor
4684 {
4685 systemtap_session& s;
4686 dwarf_builder& db;
4687
4688 dwarf_atvar_expanding_visitor(systemtap_session& s, dwarf_builder& db):
4689 s(s), db(db) {}
4690 void visit_atvar_op (atvar_op* e);
4691 };
4692
4693
4694 struct dwarf_atvar_query: public base_query
4695 {
4696 atvar_op& e;
4697 const bool userspace_p, lvalue;
4698 functioncall*& result;
4699 unsigned& tick;
4700 const string cu_name_pattern;
4701
4702 dwarf_atvar_query(dwflpp& dw, const string& module, atvar_op& e,
4703 const bool userspace_p, const bool lvalue,
4704 functioncall*& result,
4705 unsigned& tick):
4706 base_query(dw, module), e(e),
4707 userspace_p(userspace_p), lvalue(lvalue), result(result),
4708 tick(tick), cu_name_pattern(string("*/") + (string)e.cu_name) {}
4709
4710 void handle_query_module ();
4711 void query_library (const char *) {}
4712 void query_plt (const char *entry, size_t addr) {}
4713 static int atvar_query_cu (Dwarf_Die *cudie, dwarf_atvar_query *q);
4714 };
4715
4716
4717 int
4718 dwarf_atvar_query::atvar_query_cu (Dwarf_Die * cudie, dwarf_atvar_query *q)
4719 {
4720 if (! q->e.cu_name.empty())
4721 {
4722 const char *die_name = dwarf_diename(cudie) ?: "";
4723 string cns = q->e.cu_name;
4724 if (strcmp(die_name, cns.c_str()) != 0 // Perfect match
4725 && fnmatch(q->cu_name_pattern.c_str(), die_name, 0) != 0)
4726 {
4727 return DWARF_CB_OK;
4728 }
4729 }
4730
4731 try
4732 {
4733 vector<Dwarf_Die> scopes(1, *cudie);
4734
4735 q->dw.focus_on_cu (cudie);
4736
4737 if (q->e.check_pretty_print (q->lvalue))
4738 {
4739 dwarf_pretty_print dpp (q->dw, scopes, 0, q->e.sym_name(),
4740 q->userspace_p, q->e);
4741 q->result = dpp.expand();
4742 return DWARF_CB_ABORT;
4743 }
4744
4745 Dwarf_Die endtype;
4746 string code = q->dw.literal_stmt_for_local (scopes, 0, q->e.sym_name(),
4747 &q->e, q->lvalue, &endtype);
4748
4749 if (code.empty())
4750 return DWARF_CB_OK;
4751
4752 string fname = (string(q->lvalue ? "_dwarf_tvar_set"
4753 : "_dwarf_tvar_get")
4754 + "_" + q->e.sym_name()
4755 + "_" + lex_cast(q->tick++));
4756
4757 q->result = synthetic_embedded_deref_call (q->dw, &endtype, fname, code,
4758 q->userspace_p, q->lvalue,
4759 &q->e);
4760 }
4761 catch (const semantic_error& er)
4762 {
4763 // Here we suppress the error because we often just have too many
4764 // when scanning all the CUs.
4765 return DWARF_CB_OK;
4766 }
4767
4768 if (q->result) {
4769 return DWARF_CB_ABORT;
4770 }
4771
4772 return DWARF_CB_OK;
4773 }
4774
4775
4776 void
4777 dwarf_atvar_query::handle_query_module ()
4778 {
4779
4780 dw.iterate_over_cus(atvar_query_cu, this, false);
4781 }
4782
4783
4784 void
4785 dwarf_atvar_expanding_visitor::visit_atvar_op (atvar_op* e)
4786 {
4787 const bool lvalue = is_active_lvalue(e);
4788 if (lvalue && !s.guru_mode)
4789 throw SEMANTIC_ERROR(_("write to @var variable not permitted; "
4790 "need stap -g"), e->tok);
4791
4792 if (e->module.empty())
4793 e->module = "kernel";
4794
4795 functioncall* result = NULL;
4796
4797 // split the module string by ':' for alternatives
4798 vector<string> modules;
4799 tokenize(e->module, modules, ":");
4800 bool userspace_p = false;
4801 for (unsigned i = 0; !result && i < modules.size(); ++i)
4802 {
4803 string& module = modules[i];
4804
4805 dwflpp* dw;
4806 try
4807 {
4808 userspace_p = is_user_module(module);
4809 if (!userspace_p)
4810 {
4811 // kernel or kernel module target
4812 dw = db.get_kern_dw(s, module);
4813 }
4814 else
4815 {
4816 module = find_executable(module, "", s.sysenv);
4817 dw = db.get_user_dw(s, module);
4818 }
4819 }
4820 catch (const semantic_error& er)
4821 {
4822 /* ignore and go to the next module */
4823 continue;
4824 }
4825
4826 dwarf_atvar_query q (*dw, module, *e, userspace_p, lvalue, result, tick);
4827 dw->iterate_over_modules<base_query>(&query_module, &q);
4828
4829 if (result)
4830 {
4831 s.unwindsym_modules.insert(module);
4832
4833 if (lvalue)
4834 provide_lvalue_call (result);
4835
4836 result->visit(this);
4837 return;
4838 }
4839
4840 /* Unable to find the variable in the current module, so we chain
4841 * an error in atvar_op */
4842 string esn = e->sym_name();
4843 string mn = module;
4844 string cun = e->cu_name;
4845 semantic_error er(ERR_SRC, _F("unable to find global '%s' in %s%s%s",
4846 esn.c_str(), mn.c_str(),
4847 cun.empty() ? "" : _(", in "),
4848 cun.c_str()));
4849 e->chain (er);
4850 }
4851
4852 provide(e);
4853 }
4854
4855
4856 void
4857 dwarf_derived_probe::printsig (ostream& o) const
4858 {
4859 // Instead of just printing the plain locations, we add a PC value
4860 // as a comment as a way of telling e.g. apart multiple inlined
4861 // function instances. This is distinct from the verbose/clog
4862 // output, since this part goes into the cache hash calculations.
4863 sole_location()->print (o);
4864 o << " /* pc=" << section << "+0x" << hex << addr << dec << " */";
4865 printsig_nested (o);
4866 }
4867
4868
4869
4870 void
4871 dwarf_derived_probe::join_group (systemtap_session& s)
4872 {
4873 // skip probes which are paired entry-handlers
4874 if (!has_return && (saved_longs || saved_strings))
4875 return;
4876
4877 if (! s.dwarf_derived_probes)
4878 s.dwarf_derived_probes = new dwarf_derived_probe_group ();
4879 s.dwarf_derived_probes->enroll (this);
4880 this->group = s.dwarf_derived_probes;
4881 if (has_return && entry_handler)
4882 entry_handler->group = s.dwarf_derived_probes;
4883 }
4884
4885
4886 static bool
4887 kernel_supports_inode_uprobes(systemtap_session& s)
4888 {
4889 // The arch-supports is new to the builtin inode-uprobes, so it makes a
4890 // reasonable indicator of the new API. Else we'll need an autoconf...
4891 // see also buildrun.cxx:kernel_built_uprobs()
4892 return (s.kernel_config["CONFIG_ARCH_SUPPORTS_UPROBES"] == "y"
4893 && s.kernel_config["CONFIG_UPROBES"] == "y");
4894 }
4895
4896
4897 static bool
4898 kernel_supports_inode_uretprobes(systemtap_session& s)
4899 {
4900 // We need inode-uprobes first, then look for a sign of uretprobes. The only
4901 // non-static function at present is arch_uretprobe_hijack_return_addr.
4902 return kernel_supports_inode_uprobes(s) &&
4903 (s.kernel_functions.count("arch_uretprobe_hijack_return_addr") > 0);
4904 }
4905
4906
4907 void
4908 check_process_probe_kernel_support(systemtap_session& s)
4909 {
4910 // If we've got utrace, we're good to go.
4911 if (s.kernel_config["CONFIG_UTRACE"] == "y")
4912 return;
4913
4914 // We don't have utrace. For process probes that aren't
4915 // uprobes-based, we just need the task_finder. The task_finder
4916 // needs CONFIG_TRACEPOINTS and specific tracepoints. There is a
4917 // specific autoconf test for its needs.
4918 //
4919 // We'll just require CONFIG_TRACEPOINTS here as a quick-and-dirty
4920 // approximation.
4921 if (! s.need_uprobes && s.kernel_config["CONFIG_TRACEPOINTS"] == "y")
4922 return;
4923
4924 // For uprobes-based process probes, we need the task_finder plus
4925 // the builtin inode-uprobes.
4926 if (s.need_uprobes
4927 && s.kernel_config["CONFIG_TRACEPOINTS"] == "y"
4928 && kernel_supports_inode_uprobes(s))
4929 return;
4930
4931 throw SEMANTIC_ERROR (_("process probes not available without kernel CONFIG_UTRACE or CONFIG_TRACEPOINTS/CONFIG_ARCH_SUPPORTS_UPROBES/CONFIG_UPROBES"));
4932 }
4933
4934
4935 dwarf_derived_probe::dwarf_derived_probe(interned_string funcname,
4936 interned_string filename,
4937 int line,
4938 // module & section specify a relocation
4939 // base for <addr>, unless section==""
4940 // (equivalently module=="kernel")
4941 // for userspace, it's a full path, for
4942 // modules, it's either a full path, or
4943 // the basename (e.g. 'btrfs')
4944 interned_string module,
4945 interned_string section,
4946 // NB: dwfl_addr is the virtualized
4947 // address for this symbol.
4948 Dwarf_Addr dwfl_addr,
4949 // addr is the section-offset for
4950 // actual relocation.
4951 Dwarf_Addr addr,
4952 dwarf_query& q,
4953 Dwarf_Die* scope_die /* may be null */)
4954 : derived_probe (q.base_probe, q.base_loc, true /* .components soon rewritten */ ),
4955 module (module), section (section), addr (addr),
4956 path (q.path),
4957 has_process (q.has_process),
4958 has_return (q.has_return),
4959 has_maxactive (q.has_maxactive),
4960 has_library (q.has_library),
4961 maxactive_val (q.maxactive_val),
4962 user_path (q.user_path),
4963 user_lib (q.user_lib),
4964 access_vars(false),
4965 saved_longs(0), saved_strings(0),
4966 entry_handler(0)
4967 {
4968 // If we were given a fullpath to a kernel module, then get the simple name
4969 if (q.has_module && is_fully_resolved(module, q.dw.sess.sysroot, q.dw.sess.sysenv))
4970 this->module = modname_from_path(module);
4971
4972 if (user_lib.size() != 0)
4973 has_library = true;
4974
4975 if (q.has_process)
4976 {
4977 // We may receive probes on two types of ELF objects: ET_EXEC or ET_DYN.
4978 // ET_EXEC ones need no further relocation on the addr(==dwfl_addr), whereas
4979 // ET_DYN ones do (addr += run-time mmap base address). We tell these apart
4980 // by the incoming section value (".absolute" vs. ".dynamic").
4981 // XXX Assert invariants here too?
4982
4983 // inode-uprobes needs an offset rather than an absolute VM address.
4984 // ditto for userspace runtimes (dyninst)
4985 if ((kernel_supports_inode_uprobes(q.dw.sess) || q.dw.sess.runtime_usermode_p()) &&
4986 section == ".absolute" && addr == dwfl_addr &&
4987 addr >= q.dw.module_start && addr < q.dw.module_end)
4988 this->addr = addr - q.dw.module_start;
4989 }
4990 else
4991 {
4992 // Assert kernel relocation invariants
4993 if (section == "" && dwfl_addr != addr) // addr should be absolute
4994 throw SEMANTIC_ERROR (_("missing relocation basis"), tok);
4995 if (section != "" && dwfl_addr == addr) // addr should be an offset
4996 throw SEMANTIC_ERROR (_("inconsistent relocation address"), tok);
4997 }
4998
4999 // XXX: hack for strange g++/gcc's
5000 #ifndef USHRT_MAX
5001 #define USHRT_MAX 32767
5002 #endif
5003
5004 // Range limit maxactive() value
5005 if (has_maxactive && (maxactive_val < 0 || maxactive_val > USHRT_MAX))
5006 throw SEMANTIC_ERROR (_F("maxactive value out of range [0,%s]",
5007 lex_cast(USHRT_MAX).c_str()), q.base_loc->components.front()->tok);
5008
5009 // Expand target variables in the probe body. Even if the scope_die is
5010 // invalid, we still want to expand things such as $$vars/$$parms/etc...
5011 // (PR15999, PR16473). Access to specific context vars e.g. $argc will not be
5012 // expanded and will produce an error during the typeresolution_info pass.
5013 {
5014 // XXX: user-space deref's for q.has_process!
5015
5016 // PR14436: if we're expanding target variables in the probe body of a
5017 // .return probe, we need to make the expansion at the postprologue addr
5018 // instead (if any), which is then also the spot where the entry handler
5019 // probe is placed. (Note that at this point, a nonzero prologue_end
5020 // implies that it should be used, i.e. code is unoptimized).
5021 Dwarf_Addr handler_dwfl_addr = dwfl_addr;
5022 if (q.prologue_end != 0 && q.has_return)
5023 {
5024 handler_dwfl_addr = q.prologue_end;
5025 if (q.sess.verbose > 2)
5026 clog << _F("expanding .return vars at prologue_end (0x%s) "
5027 "rather than entrypc (0x%s)\n",
5028 lex_cast_hex(handler_dwfl_addr).c_str(),
5029 lex_cast_hex(dwfl_addr).c_str());
5030 }
5031 dwarf_var_expanding_visitor v (q, scope_die, handler_dwfl_addr);
5032 v.replace (this->body);
5033
5034 // Propagate perf.counters so we can emit later
5035 this->perf_counter_refs = v.perf_counter_refs;
5036 // Emit local var used to save the perf counter read value
5037 std::set<string>::iterator pcii;
5038 for (pcii = v.perf_counter_refs.begin();
5039 pcii != v.perf_counter_refs.end(); pcii++)
5040 {
5041 std::vector<std::pair<std::string,std::string> >::iterator it;
5042 // Find the associated perf counter probe
5043 for (it=q.sess.perf_counters.begin() ;
5044 it != q.sess.perf_counters.end();
5045 it++)
5046 if ((*it).first == (*pcii))
5047 {
5048 vardecl* vd = new vardecl;
5049 vd->name = "__perf_read_" + (*it).first;
5050 vd->tok = this->tok;
5051 vd->set_arity(0, this->tok);
5052 vd->type = pe_long;
5053 vd->synthetic = true;
5054 this->locals.push_back (vd);
5055 break;
5056 }
5057 }
5058
5059
5060 if (!q.has_process)
5061 access_vars = v.visited;
5062
5063 // If during target-variable-expanding the probe, we added a new block
5064 // of code, add it to the start of the probe.
5065 if (v.add_block)
5066 this->body = new block(v.add_block, this->body);
5067
5068 // If when target-variable-expanding the probe, we need to synthesize a
5069 // sibling function-entry probe. We don't go through the whole probe derivation
5070 // business (PR10642) that could lead to wildcard/alias resolution, or for that
5071 // dwarf-induced duplication.
5072 if (v.add_call_probe)
5073 {
5074 assert (q.has_return && !q.has_call);
5075
5076 // We temporarily replace q.base_probe.
5077 statement* old_body = q.base_probe->body;
5078 q.base_probe->body = v.add_call_probe;
5079 q.has_return = false;
5080 q.has_call = true;
5081
5082 if (q.has_process)
5083 {
5084 // Place handler probe at the same addr as where the vars were
5085 // expanded (which may not be the same addr as the one for the
5086 // main retprobe, PR14436).
5087 Dwarf_Addr handler_addr = addr;
5088 if (handler_dwfl_addr != dwfl_addr)
5089 // adjust section offset by prologue_end-entrypc
5090 handler_addr += handler_dwfl_addr - dwfl_addr;
5091 entry_handler = new uprobe_derived_probe (funcname, filename,
5092 line, module, section,
5093 handler_dwfl_addr,
5094 handler_addr, q,
5095 scope_die);
5096 }
5097 else
5098 entry_handler = new dwarf_derived_probe (funcname, filename, line,
5099 module, section, dwfl_addr,
5100 addr, q, scope_die);
5101
5102 saved_longs = entry_handler->saved_longs = v.saved_longs;
5103 saved_strings = entry_handler->saved_strings = v.saved_strings;
5104
5105 q.results.push_back (entry_handler);
5106
5107 q.has_return = true;
5108 q.has_call = false;
5109 q.base_probe->body = old_body;
5110 }
5111 // Save the local variables for listing mode. If the scope_die is null,
5112 // local vars aren't accessible, so no need to invoke saveargs (PR10820).
5113 if (!null_die(scope_die) &&
5114 q.sess.dump_mode == systemtap_session::dump_matched_probes_vars)
5115 saveargs(q, scope_die, dwfl_addr);
5116 }
5117
5118 // Reset the sole element of the "locations" vector as a
5119 // "reverse-engineered" form of the incoming (q.base_loc) probe
5120 // point. This allows a user to see what function / file / line
5121 // number any particular match of the wildcards.
5122
5123 vector<probe_point::component*> comps;
5124 if (q.has_kernel)
5125 comps.push_back (new probe_point::component(TOK_KERNEL));
5126 else if(q.has_module)
5127 comps.push_back (new probe_point::component(TOK_MODULE, new literal_string(module)));
5128 else if(q.has_process)
5129 comps.push_back (new probe_point::component(TOK_PROCESS, new literal_string(module)));
5130 else
5131 assert (0);
5132
5133 string fn_or_stmt;
5134 if (q.has_function_str || q.has_function_num)
5135 fn_or_stmt = TOK_FUNCTION;
5136 else
5137 fn_or_stmt = TOK_STATEMENT;
5138
5139 if (q.has_function_str || q.has_statement_str)
5140 {
5141 interned_string retro_name = q.final_function_name(funcname, filename, line);
5142 comps.push_back
5143 (new probe_point::component
5144 (fn_or_stmt, new literal_string (retro_name)));
5145 }
5146 else if (q.has_function_num || q.has_statement_num)
5147 {
5148 Dwarf_Addr retro_addr;
5149 if (q.has_function_num)
5150 retro_addr = q.function_num_val;
5151 else
5152 retro_addr = q.statement_num_val;
5153 comps.push_back (new probe_point::component
5154 (fn_or_stmt,
5155 new literal_number(retro_addr, true)));
5156
5157 if (q.has_absolute)
5158 comps.push_back (new probe_point::component (TOK_ABSOLUTE));
5159 }
5160
5161 if (q.has_call)
5162 comps.push_back (new probe_point::component(TOK_CALL));
5163 if (q.has_exported)
5164 comps.push_back (new probe_point::component(TOK_EXPORTED));
5165 if (q.has_inline)
5166 comps.push_back (new probe_point::component(TOK_INLINE));
5167 if (has_return)
5168 comps.push_back (new probe_point::component(TOK_RETURN));
5169 if (has_maxactive)
5170 comps.push_back (new probe_point::component
5171 (TOK_MAXACTIVE, new literal_number(maxactive_val)));
5172
5173 // Overwrite it.
5174 this->sole_location()->components = comps;
5175
5176 // if it's a .callee[s[(N)]] call, add checks to the probe body so that the
5177 // user body is only 'triggered' when called from q.callers[N-1], which
5178 // itself is called from q.callers[N-2], etc... I.E.
5179 // callees(N) --> N elements in q.callers --> N checks against [u]stack(0..N-1)
5180 if ((q.has_callee || q.has_callees_num) && q.callers && !q.callers->empty())
5181 {
5182 if (q.sess.verbose > 2)
5183 clog << _F("adding caller checks for callee %s\n",
5184 funcname.to_string().c_str());
5185
5186 // Copy the stack and empty it out
5187 stack<Dwarf_Addr> callers(*q.callers);
5188 for (unsigned level = 1; !callers.empty(); level++,
5189 callers.pop())
5190 {
5191 Dwarf_Addr caller = callers.top();
5192
5193 // We first need to make the caller addr relocatable
5194 interned_string caller_section;
5195 Dwarf_Addr caller_reloc;
5196 if (module == TOK_KERNEL)
5197 { // allow for relocatable kernel (see also add_probe_point())
5198 caller_reloc = caller - q.sess.sym_stext;
5199 caller_section = "_stext";
5200 }
5201 else
5202 caller_reloc = q.dw.relocate_address(caller,
5203 caller_section);
5204
5205 if (q.sess.verbose > 2)
5206 clog << _F("adding caller check [u]stack(%d) == reloc(0x%s)\n",
5207 level, lex_cast_hex(caller_reloc).c_str());
5208
5209 // We want to add a statement like this:
5210 // if (!_caller_match(user, mod, sec, addr)) next;
5211 // Something similar is done in semantic_pass_conditions()
5212
5213 functioncall* check = new functioncall();
5214 check->tok = this->tok;
5215 check->function = "_caller_match";
5216 check->args.push_back(new literal_number(q.has_process));
5217 check->args[0]->tok = this->tok;
5218 // For callee .return probes, the callee is popped off stack
5219 // so we don't want to match the frame below the caller
5220 if (q.has_return)
5221 check->args.push_back(new literal_number(level-1));
5222 else
5223 check->args.push_back(new literal_number(level));
5224 check->args[1]->tok = this->tok;
5225 check->args.push_back(new literal_string(this->module));
5226 check->args[2]->tok = this->tok;
5227 check->args.push_back(new literal_string(caller_section));
5228 check->args[3]->tok = this->tok;
5229 check->args.push_back(new literal_number(caller_reloc, true /* hex */));
5230 check->args[4]->tok = this->tok;
5231
5232 unary_expression* notexp = new unary_expression();
5233 notexp->tok = this->tok;
5234 notexp->op = "!";
5235 notexp->operand = check;
5236
5237 if_statement* ifs = new if_statement();
5238 ifs->tok = this->tok;
5239 ifs->thenblock = new next_statement();
5240 ifs->thenblock->tok = this->tok;
5241 ifs->elseblock = NULL;
5242 ifs->condition = notexp;
5243
5244 this->body = new block(ifs, this->body);
5245 }
5246 }
5247 }
5248
5249
5250 void
5251 dwarf_derived_probe::saveargs(dwarf_query& q, Dwarf_Die* scope_die,
5252 Dwarf_Addr dwfl_addr)
5253 {
5254 if (null_die(scope_die))
5255 return;
5256
5257 bool verbose = q.sess.verbose > 2;
5258
5259 if (verbose)
5260 clog << _F("saveargs: examining '%s' (dieoffset: %#" PRIx64 ")\n", (dwarf_diename(scope_die)?: "unknown"), dwarf_dieoffset(scope_die));
5261
5262 if (has_return)
5263 {
5264 /* Only save the return value if it has a type. */
5265 string type_name;
5266 Dwarf_Die type_die;
5267 if (dwarf_attr_die (scope_die, DW_AT_type, &type_die) &&
5268 dwarf_type_name(&type_die, type_name))
5269 args.push_back("$return:"+type_name);
5270
5271 else if (verbose)
5272 clog << _F("saveargs: failed to retrieve type name for return value (dieoffset: %s)\n",
5273 lex_cast_hex(dwarf_dieoffset(scope_die)).c_str());
5274 }
5275
5276 Dwarf_Die arg;
5277 vector<Dwarf_Die> scopes = q.dw.getscopes(scope_die);
5278 for (unsigned i = 0; i < scopes.size(); ++i)
5279 {
5280 if (dwarf_tag(&scopes[i]) == DW_TAG_compile_unit)
5281 break; // we don't want file-level variables
5282 if (dwarf_child (&scopes[i], &arg) == 0)
5283 do
5284 {
5285 switch (dwarf_tag (&arg))
5286 {
5287 case DW_TAG_variable:
5288 case DW_TAG_formal_parameter:
5289 break;
5290
5291 default:
5292 continue;
5293 }
5294
5295 /* Ignore this local if it has no name. */
5296 const char *arg_name = dwarf_diename (&arg);
5297 if (!arg_name)
5298 {
5299 if (verbose)
5300 clog << _F("saveargs: failed to retrieve name for local (dieoffset: %s)\n",
5301 lex_cast_hex(dwarf_dieoffset(&arg)).c_str());
5302 continue;
5303 }
5304
5305 if (verbose)
5306 clog << _F("saveargs: finding location for local '%s' (dieoffset: %s)\n",
5307 arg_name, lex_cast_hex(dwarf_dieoffset(&arg)).c_str());
5308
5309 /* Ignore this local if it has no location (or not at this PC). */
5310 /* NB: It still may not be directly accessible, e.g. if it is an
5311 * aggregate type, implicit_pointer, etc., but the user can later
5312 * figure out how to access the interesting parts. */
5313
5314 /* XXX: Perhaps saveargs() / listings-mode should work by synthesizing
5315 * several synthetic
5316 * probe foo { $var }
5317 * probes, testing them for overall resolvability.
5318 */
5319
5320 Dwarf_Attribute attr_mem;
5321 if (!dwarf_attr_integrate (&arg, DW_AT_const_value, &attr_mem))
5322 {
5323 Dwarf_Op *expr;
5324 size_t len;
5325 if (!dwarf_attr_integrate (&arg, DW_AT_location, &attr_mem))
5326 {
5327 if (verbose)
5328 clog << _F("saveargs: failed to resolve the location for local '%s' (dieoffset: %s)\n",
5329 arg_name, lex_cast_hex(dwarf_dieoffset(&arg)).c_str());
5330 continue;
5331 }
5332 else if (!(dwarf_getlocation_addr(&attr_mem, dwfl_addr, &expr,
5333 &len, 1) == 1 && len > 0))
5334 {
5335 Dwarf_Addr dwfl_addr2 = q.dw.pr15123_retry_addr (dwfl_addr, & arg);
5336 if (!dwfl_addr2 || (!(dwarf_getlocation_addr(&attr_mem, dwfl_addr2, &expr,
5337 &len, 1) == 1 && len > 0))) {
5338 if (verbose)
5339 clog << _F("saveargs: local '%s' (dieoffset: %s) is not available at this address (%s)\n",
5340 arg_name, lex_cast_hex(dwarf_dieoffset(&arg)).c_str(), lex_cast_hex(dwfl_addr).c_str());
5341 continue;
5342 }
5343 }
5344 }
5345
5346 /* Ignore this local if it has no type. */
5347 string type_name;
5348 Dwarf_Die type_die;
5349 if (!dwarf_attr_die (&arg, DW_AT_type, &type_die) ||
5350 !dwarf_type_name(&type_die, type_name))
5351 {
5352 if (verbose)
5353 clog << _F("saveargs: failed to retrieve type name for local '%s' (dieoffset: %s)\n",
5354 arg_name, lex_cast_hex(dwarf_dieoffset(&arg)).c_str());
5355 continue;
5356 }
5357
5358 /* This local looks good -- save it! */
5359 args.push_back("$"+string(arg_name)+":"+type_name);
5360 }
5361 while (dwarf_siblingof (&arg, &arg) == 0);
5362 }
5363 }
5364
5365
5366 void
5367 dwarf_derived_probe::getargs(std::list<std::string> &arg_set) const
5368 {
5369 arg_set.insert(arg_set.end(), args.begin(), args.end());
5370 }
5371
5372
5373 void
5374 dwarf_derived_probe::emit_privilege_assertion (translator_output* o)
5375 {
5376 if (has_process)
5377 {
5378 // These probes are allowed for unprivileged users, but only in the
5379 // context of processes which they own.
5380 emit_process_owner_assertion (o);
5381 return;
5382 }
5383
5384 // Other probes must contain the default assertion which aborts
5385 // if executed by an unprivileged user.
5386 derived_probe::emit_privilege_assertion (o);
5387 }
5388
5389
5390 void
5391 dwarf_derived_probe::print_dupe_stamp(ostream& o)
5392 {
5393 if (has_process)
5394 {
5395 // These probes are allowed for unprivileged users, but only in the
5396 // context of processes which they own.
5397 print_dupe_stamp_unprivileged_process_owner (o);
5398 return;
5399 }
5400
5401 // Other probes must contain the default dupe stamp
5402 derived_probe::print_dupe_stamp (o);
5403 }
5404
5405
5406 void
5407 dwarf_derived_probe::register_statement_variants(match_node * root,
5408 dwarf_builder * dw,
5409 privilege_t privilege)
5410 {
5411 root
5412 ->bind_privilege(privilege)
5413 ->bind(dw);
5414 root->bind(TOK_NEAREST)
5415 ->bind_privilege(privilege)
5416 ->bind(dw);
5417 }
5418
5419 void
5420 dwarf_derived_probe::register_function_variants(match_node * root,
5421 dwarf_builder * dw,
5422 privilege_t privilege)
5423 {
5424 root
5425 ->bind_privilege(privilege)
5426 ->bind(dw);
5427 root->bind(TOK_CALL)
5428 ->bind_privilege(privilege)
5429 ->bind(dw);
5430 root->bind(TOK_EXPORTED)
5431 ->bind_privilege(privilege)
5432 ->bind(dw);
5433 root->bind(TOK_RETURN)
5434 ->bind_privilege(privilege)
5435 ->bind(dw);
5436
5437 // For process probes / uprobes, .maxactive() is unused.
5438 if (! pr_contains (privilege, pr_stapusr))
5439 {
5440 root->bind(TOK_RETURN)
5441 ->bind_num(TOK_MAXACTIVE)->bind(dw);
5442 }
5443 }
5444
5445 void
5446 dwarf_derived_probe::register_function_and_statement_variants(
5447 systemtap_session& s,
5448 match_node * root,
5449 dwarf_builder * dw,
5450 privilege_t privilege
5451 )
5452 {
5453 // Here we match 4 forms:
5454 //
5455 // .function("foo")
5456 // .function(0xdeadbeef)
5457 // .statement("foo")
5458 // .statement(0xdeadbeef)
5459
5460 match_node *fv_root = root->bind_str(TOK_FUNCTION);
5461 register_function_variants(fv_root, dw, privilege);
5462 // ROOT.function("STRING") always gets the .inline and .label variants.
5463 fv_root->bind(TOK_INLINE)
5464 ->bind_privilege(privilege)
5465 ->bind(dw);
5466 fv_root->bind_str(TOK_LABEL)
5467 ->bind_privilege(privilege)
5468 ->bind(dw);
5469 fv_root->bind_str(TOK_CALLEE)
5470 ->bind_privilege(privilege)
5471 ->bind(dw);
5472 fv_root->bind_str(TOK_CALLEE)
5473 ->bind(TOK_RETURN)
5474 ->bind_privilege(privilege)
5475 ->bind(dw);
5476 fv_root->bind_str(TOK_CALLEE)
5477 ->bind(TOK_CALL)
5478 ->bind_privilege(privilege)
5479 ->bind(dw);
5480 fv_root->bind(TOK_CALLEES)
5481 ->bind_privilege(privilege)
5482 ->bind(dw);
5483 fv_root->bind_num(TOK_CALLEES)
5484 ->bind_privilege(privilege)
5485 ->bind(dw);
5486
5487 fv_root = root->bind_num(TOK_FUNCTION);
5488 register_function_variants(fv_root, dw, privilege);
5489 // ROOT.function(NUMBER).inline is deprecated in release 1.7 and removed thereafter.
5490 if (strverscmp(s.compatible.c_str(), "1.7") <= 0)
5491 {
5492 fv_root->bind(TOK_INLINE)
5493 ->bind_privilege(privilege)
5494 ->bind(dw);
5495 }
5496
5497 register_statement_variants(root->bind_str(TOK_STATEMENT), dw, privilege);
5498 register_statement_variants(root->bind_num(TOK_STATEMENT), dw, privilege);
5499 }
5500
5501 void
5502 dwarf_derived_probe::register_sdt_variants(systemtap_session& s,
5503 match_node * root,
5504 dwarf_builder * dw)
5505 {
5506 root->bind_str(TOK_MARK)
5507 ->bind_privilege(pr_all)
5508 ->bind(dw);
5509 root->bind_str(TOK_PROVIDER)->bind_str(TOK_MARK)
5510 ->bind_privilege(pr_all)
5511 ->bind(dw);
5512 }
5513
5514 void
5515 dwarf_derived_probe::register_plt_variants(systemtap_session& s,
5516 match_node * root,
5517 dwarf_builder * dw)
5518 {
5519 root->bind(TOK_PLT)
5520 ->bind_privilege(pr_all)
5521 ->bind(dw);
5522 root->bind_str(TOK_PLT)
5523 ->bind_privilege(pr_all)
5524 ->bind(dw);
5525
5526 root->bind(TOK_PLT)
5527 ->bind(TOK_RETURN)
5528 ->bind_privilege(pr_all)
5529 ->bind(dw);
5530 root->bind_str(TOK_PLT)
5531 ->bind(TOK_RETURN)
5532 ->bind_privilege(pr_all)
5533 ->bind(dw);
5534 }
5535
5536 void
5537 dwarf_derived_probe::register_patterns(systemtap_session& s)
5538 {
5539 match_node* root = s.pattern_root;
5540 dwarf_builder *dw = new dwarf_builder();
5541
5542 update_visitor *filter = new dwarf_cast_expanding_visitor(s, *dw);
5543 s.code_filters.push_back(filter);
5544
5545 filter = new dwarf_atvar_expanding_visitor(s, *dw);
5546 s.code_filters.push_back(filter);
5547
5548 register_function_and_statement_variants(s, root->bind(TOK_KERNEL), dw, pr_privileged);
5549 register_function_and_statement_variants(s, root->bind_str(TOK_MODULE), dw, pr_privileged);
5550 root->bind(TOK_KERNEL)->bind_num(TOK_STATEMENT)->bind(TOK_ABSOLUTE)
5551 ->bind(dw);
5552
5553 match_node* uprobes[] = {
5554 root->bind(TOK_PROCESS),
5555 root->bind_str(TOK_PROCESS),
5556 root->bind_num(TOK_PROCESS),
5557 root->bind(TOK_PROCESS)->bind_str(TOK_LIBRARY),
5558 root->bind_str(TOK_PROCESS)->bind_str(TOK_LIBRARY),
5559 };
5560 for (size_t i = 0; i < sizeof(uprobes) / sizeof(*uprobes); ++i)
5561 {
5562 register_function_and_statement_variants(s, uprobes[i], dw, pr_all);
5563 register_sdt_variants(s, uprobes[i], dw);
5564 register_plt_variants(s, uprobes[i], dw);
5565 }
5566 }
5567
5568 void
5569 dwarf_derived_probe::emit_probe_local_init(systemtap_session& s, translator_output * o)
5570 {
5571 std::set<string>::iterator pcii;
5572 for (pcii = perf_counter_refs.begin();
5573 pcii != perf_counter_refs.end();
5574 pcii++)
5575 {
5576 std::vector<std::pair<std::string,std::string> >::iterator it;
5577 // Find the associated perf.counter probe
5578 unsigned i = 0;
5579 for (it=s.perf_counters.begin() ;
5580 it != s.perf_counters.end();
5581 it++, i++)
5582 if ((*it).first == (*pcii))
5583 {
5584 // place the perf counter read so it precedes stp_lock_probe
5585 o->newline() << "l->l___perf_read_" + (*it).first
5586 + " = (((int64_t) (_stp_perf_read(smp_processor_id(),"
5587 + lex_cast(i) + "))));";
5588 break;
5589 }
5590 }
5591
5592 if (access_vars)
5593 {
5594 // if accessing $variables, emit bsp cache setup for speeding up
5595 o->newline() << "#if defined __ia64__";
5596 o->newline() << "bspcache(c->unwaddr, c->kregs);";
5597 o->newline() << "#endif";
5598 }
5599 }
5600
5601 // ------------------------------------------------------------------------
5602
5603 void
5604 dwarf_derived_probe_group::enroll (dwarf_derived_probe* p)
5605 {
5606 probes_by_module.insert (make_pair (p->module, p));
5607
5608 // XXX: probes put at the same address should all share a
5609 // single kprobe/kretprobe, and have their handlers executed
5610 // sequentially.
5611 }
5612
5613 void
5614 dwarf_derived_probe_group::emit_module_decls (systemtap_session& s)
5615 {
5616 if (probes_by_module.empty()) return;
5617
5618 s.op->newline() << "/* ---- dwarf probes ---- */";
5619
5620 // Let's find some stats for the embedded strings. Maybe they
5621 // are small and uniform enough to justify putting char[MAX]'s into
5622 // the array instead of relocated char*'s.
5623 size_t module_name_max = 0, section_name_max = 0;
5624 size_t module_name_tot = 0, section_name_tot = 0;
5625 size_t all_name_cnt = probes_by_module.size(); // for average
5626 for (p_b_m_iterator it = probes_by_module.begin(); it != probes_by_module.end(); it++)
5627 {
5628 dwarf_derived_probe* p = it->second;
5629 #define DOIT(var,expr) do { \
5630 size_t var##_size = (expr) + 1; \
5631 var##_max = max (var##_max, var##_size); \
5632 var##_tot += var##_size; } while (0)
5633 DOIT(module_name, p->module.size());
5634 DOIT(section_name, p->section.size());
5635 #undef DOIT
5636 }
5637
5638 // Decide whether it's worthwhile to use char[] or char* by comparing
5639 // the amount of average waste (max - avg) to the relocation data size
5640 // (3 native long words).
5641 #define CALCIT(var) \
5642 if ((var##_name_max-(var##_name_tot/all_name_cnt)) < (3 * sizeof(void*))) \
5643 { \
5644 s.op->newline() << "#define STAP_DWARF_PROBE_STR_" << #var << " " \
5645 << "const char " << #var \
5646 << "[" << var##_name_max << "]"; \
5647 if (s.verbose > 2) clog << "stap_dwarf_probe " << #var \
5648 << "[" << var##_name_max << "]" << endl; \
5649 } \
5650 else \
5651 { \
5652 s.op->newline() << "#define STAP_DWARF_PROBE_STR_" << #var << " " \
5653 << "const char * const " << #var << ""; \
5654 if (s.verbose > 2) clog << "stap_dwarf_probe *" << #var << endl; \
5655 }
5656
5657 CALCIT(module);
5658 CALCIT(section);
5659
5660 #undef CALCIT
5661
5662 s.op->newline() << "#include \"linux/kprobes.c\"";
5663
5664 #define UNDEFIT(var) s.op->newline() << "#undef STAP_DWARF_PROBE_STR_" << #var
5665 UNDEFIT(module);
5666 UNDEFIT(section);
5667 #undef UNDEFIT
5668
5669 // Emit an array of kprobe/kretprobe pointers
5670 s.op->newline() << "#if defined(STAPCONF_UNREGISTER_KPROBES)";
5671 s.op->newline() << "static void * stap_unreg_kprobes[" << probes_by_module.size() << "];";
5672 s.op->newline() << "#endif";
5673
5674 // Emit the actual probe list.
5675
5676 // NB: we used to plop a union { struct kprobe; struct kretprobe } into
5677 // struct stap_dwarf_probe, but it being initialized data makes it add
5678 // hundreds of bytes of padding per stap_dwarf_probe. (PR5673)
5679 s.op->newline() << "static struct stap_dwarf_kprobe stap_dwarf_kprobes[" << probes_by_module.size() << "];";
5680 // NB: bss!
5681
5682 s.op->newline() << "static struct stap_dwarf_probe stap_dwarf_probes[] = {";
5683 s.op->indent(1);
5684
5685 size_t stap_dwarf_kprobe_idx = 0;
5686 for (p_b_m_iterator it = probes_by_module.begin(); it != probes_by_module.end(); it++)
5687 {
5688 dwarf_derived_probe* p = it->second;
5689 s.op->newline() << "{";
5690 if (p->has_return)
5691 s.op->line() << " .return_p=1,";
5692 if (p->has_maxactive)
5693 {
5694 s.op->line() << " .maxactive_p=1,";
5695 assert (p->maxactive_val >= 0 && p->maxactive_val <= USHRT_MAX);
5696 s.op->line() << " .maxactive_val=" << p->maxactive_val << ",";
5697 }
5698 if (p->saved_longs || p->saved_strings)
5699 {
5700 if (p->saved_longs)
5701 s.op->line() << " .saved_longs=" << p->saved_longs << ",";
5702 if (p->saved_strings)
5703 s.op->line() << " .saved_strings=" << p->saved_strings << ",";
5704 if (p->entry_handler)
5705 s.op->line() << " .entry_probe=" << common_probe_init (p->entry_handler) << ",";
5706 }
5707 if (p->locations[0]->optional)
5708 s.op->line() << " .optional_p=1,";
5709 s.op->line() << " .address=(unsigned long)0x" << hex << p->addr << dec << "ULL,";
5710 s.op->line() << " .module=\"" << p->module << "\",";
5711 s.op->line() << " .section=\"" << p->section << "\",";
5712 s.op->line() << " .probe=" << common_probe_init (p) << ",";
5713 s.op->line() << " .kprobe=&stap_dwarf_kprobes[" << stap_dwarf_kprobe_idx++ << "],";
5714 s.op->line() << " },";
5715 }
5716
5717 s.op->newline(-1) << "};";
5718
5719 // Emit the kprobes callback function
5720 s.op->newline();
5721 s.op->newline() << "static int enter_kprobe_probe (struct kprobe *inst,";
5722 s.op->line() << " struct pt_regs *regs) {";
5723 // NB: as of PR5673, the kprobe|kretprobe union struct is in BSS
5724 s.op->newline(1) << "int kprobe_idx = ((uintptr_t)inst-(uintptr_t)stap_dwarf_kprobes)/sizeof(struct stap_dwarf_kprobe);";
5725 // Check that the index is plausible
5726 s.op->newline() << "struct stap_dwarf_probe *sdp = &stap_dwarf_probes[";
5727 s.op->line() << "((kprobe_idx >= 0 && kprobe_idx < " << probes_by_module.size() << ")?";
5728 s.op->line() << "kprobe_idx:0)"; // NB: at least we avoid memory corruption
5729 // XXX: it would be nice to give a more verbose error though; BUG_ON later?
5730 s.op->line() << "];";
5731 common_probe_entryfn_prologue (s, "STAP_SESSION_RUNNING", "sdp->probe",
5732 "stp_probe_type_kprobe");
5733 s.op->newline() << "c->kregs = regs;";
5734
5735 // Make it look like the IP is set as it wouldn't have been replaced
5736 // by a breakpoint instruction when calling real probe handler. Reset
5737 // IP regs on return, so we don't confuse kprobes. PR10458
5738 s.op->newline() << "{";
5739 s.op->indent(1);
5740 s.op->newline() << "unsigned long kprobes_ip = REG_IP(c->kregs);";
5741 s.op->newline() << "SET_REG_IP(regs, (unsigned long) inst->addr);";
5742 s.op->newline() << "(*sdp->probe->ph) (c);";
5743 s.op->newline() << "SET_REG_IP(regs, kprobes_ip);";
5744 s.op->newline(-1) << "}";
5745
5746 common_probe_entryfn_epilogue (s, true, otf_safe_context(s));
5747 s.op->newline() << "return 0;";
5748 s.op->newline(-1) << "}";
5749
5750 // Same for kretprobes
5751 s.op->newline();
5752 s.op->newline() << "static int enter_kretprobe_common (struct kretprobe_instance *inst,";
5753 s.op->line() << " struct pt_regs *regs, int entry) {";
5754 s.op->newline(1) << "struct kretprobe *krp = inst->rp;";
5755
5756 // NB: as of PR5673, the kprobe|kretprobe union struct is in BSS
5757 s.op->newline() << "int kprobe_idx = ((uintptr_t)krp-(uintptr_t)stap_dwarf_kprobes)/sizeof(struct stap_dwarf_kprobe);";
5758 // Check that the index is plausible
5759 s.op->newline() << "struct stap_dwarf_probe *sdp = &stap_dwarf_probes[";
5760 s.op->line() << "((kprobe_idx >= 0 && kprobe_idx < " << probes_by_module.size() << ")?";
5761 s.op->line() << "kprobe_idx:0)"; // NB: at least we avoid memory corruption
5762 // XXX: it would be nice to give a more verbose error though; BUG_ON later?
5763 s.op->line() << "];";
5764
5765 s.op->newline() << "const struct stap_probe *sp = entry ? sdp->entry_probe : sdp->probe;";
5766 s.op->newline() << "if (sp) {";
5767 s.op->indent(1);
5768 common_probe_entryfn_prologue (s, "STAP_SESSION_RUNNING", "sp",
5769 "stp_probe_type_kretprobe");
5770 s.op->newline() << "c->kregs = regs;";
5771
5772 // for assisting runtime's backtrace logic and accessing kretprobe data packets
5773 s.op->newline() << "c->ips.krp.pi = inst;";
5774 s.op->newline() << "c->ips.krp.pi_longs = sdp->saved_longs;";
5775
5776 // Make it look like the IP is set as it wouldn't have been replaced
5777 // by a breakpoint instruction when calling real probe handler. Reset
5778 // IP regs on return, so we don't confuse kprobes. PR10458
5779 s.op->newline() << "{";
5780 s.op->newline(1) << "unsigned long kprobes_ip = REG_IP(c->kregs);";
5781 s.op->newline() << "if (entry)";
5782 s.op->newline(1) << "SET_REG_IP(regs, (unsigned long) inst->rp->kp.addr);";
5783 s.op->newline(-1) << "else";
5784 s.op->newline(1) << "SET_REG_IP(regs, (unsigned long)inst->ret_addr);";
5785 s.op->newline(-1) << "(sp->ph) (c);";
5786 s.op->newline() << "SET_REG_IP(regs, kprobes_ip);";
5787 s.op->newline(-1) << "}";
5788
5789 common_probe_entryfn_epilogue (s, true, otf_safe_context(s));
5790 s.op->newline(-1) << "}";
5791 s.op->newline() << "return 0;";
5792 s.op->newline(-1) << "}";
5793
5794 s.op->newline();
5795 }
5796
5797
5798 void
5799 dwarf_derived_probe_group::emit_module_init (systemtap_session& s)
5800 {
5801 if (probes_by_module.empty()) return;
5802
5803 s.op->newline() << "/* ---- dwarf probes ---- */";
5804
5805 // We'll let stapkp_init() handle reporting errors by setting probe_point to
5806 // NULL.
5807 s.op->newline() << "probe_point = NULL;";
5808
5809 s.op->newline() << "rc = stapkp_init( "
5810 << "stap_dwarf_probes, "
5811 << "ARRAY_SIZE(stap_dwarf_probes));";
5812 }
5813
5814
5815 void
5816 dwarf_derived_probe_group::emit_module_refresh (systemtap_session& s)
5817 {
5818 if (probes_by_module.empty()) return;
5819
5820 s.op->newline() << "/* ---- dwarf probes ---- */";
5821
5822 s.op->newline() << "stapkp_refresh( "
5823 << "modname, "
5824 << "stap_dwarf_probes, "
5825 << "ARRAY_SIZE(stap_dwarf_probes));";
5826 }
5827
5828
5829 void
5830 dwarf_derived_probe_group::emit_module_exit (systemtap_session& s)
5831 {
5832 if (probes_by_module.empty()) return;
5833
5834 s.op->newline() << "/* ---- dwarf probes ---- */";
5835
5836 s.op->newline() << "stapkp_exit( "
5837 << "stap_dwarf_probes, "
5838 << "ARRAY_SIZE(stap_dwarf_probes));";
5839 }
5840
5841 static void sdt_v3_tokenize(const string& str, vector<string>& tokens)
5842 {
5843 string::size_type pos;
5844 string::size_type lastPos = str.find_first_not_of(" ", 0);
5845 string::size_type nextAt = str.find("@", lastPos);
5846
5847 if (nextAt == string::npos)
5848 {
5849 // PR13934: Assembly probes are not forced to use the N@OP form.
5850 // In this case, N is inferred to be the native word size. Since we
5851 // don't have a nice delimiter, just split it on spaces. SDT-asm authors
5852 // then must not put any spaces in arguments, to avoid ambiguity.
5853 tokenize(str, tokens, " ");
5854 return;
5855 }
5856
5857 while (lastPos != string::npos)
5858 {
5859 pos = nextAt + 1;
5860 nextAt = str.find("@", pos);
5861 if (nextAt == string::npos)
5862 pos = string::npos;
5863 else
5864 pos = str.rfind(" ", nextAt);
5865
5866 tokens.push_back(str.substr(lastPos, pos - lastPos));
5867 lastPos = str.find_first_not_of(" ", pos);
5868 }
5869 }
5870
5871
5872 struct sdt_uprobe_var_expanding_visitor: public var_expanding_visitor
5873 {
5874 enum regwidths {QI, QIh, HI, SI, DI};
5875 sdt_uprobe_var_expanding_visitor(systemtap_session& s,
5876 dwflpp& dw,
5877 int elf_machine,
5878 interned_string process_name,
5879 interned_string provider_name,
5880 interned_string probe_name,
5881 stap_sdt_probe_type probe_type,
5882 interned_string arg_string,
5883 int ac):
5884 session (s), dw (dw), elf_machine (elf_machine),
5885 process_name (process_name), provider_name (provider_name),
5886 probe_name (probe_name), probe_type (probe_type), arg_count ((unsigned) ac)
5887 {
5888 // sanity check that we're not somehow here for a kernel probe
5889 assert(is_user_module(process_name));
5890
5891 build_dwarf_registers();
5892
5893 need_debug_info = false;
5894 if (probe_type == uprobe3_type)
5895 {
5896 sdt_v3_tokenize(arg_string, arg_tokens);
5897 assert(arg_count <= 12);
5898 }
5899 else
5900 {
5901 tokenize(arg_string, arg_tokens, " ");
5902 assert(arg_count <= 10);
5903 }
5904 }
5905
5906 systemtap_session& session;
5907 dwflpp& dw;
5908 int elf_machine;
5909 interned_string process_name;
5910 interned_string provider_name;
5911 interned_string probe_name;
5912 stap_sdt_probe_type probe_type;
5913 unsigned arg_count;
5914 vector<string> arg_tokens;
5915
5916 map<string, pair<unsigned,int> > dwarf_regs;
5917 string regnames;
5918 string percent_regnames;
5919
5920 bool need_debug_info;
5921
5922 void build_dwarf_registers();
5923 void visit_target_symbol (target_symbol* e);
5924 unsigned get_target_symbol_argno_and_validate (target_symbol* e);
5925 long parse_out_arg_precision(string& asmarg);
5926 expression* try_parse_arg_literal (target_symbol *e,
5927 const string& asmarg,
5928 long precision);
5929 expression* try_parse_arg_register (target_symbol *e,
5930 const string& asmarg,
5931 long precision);
5932 expression* try_parse_arg_offset_register (target_symbol *e,
5933 const string& asmarg,
5934 long precision);
5935 expression* try_parse_arg_effective_addr (target_symbol *e,
5936 const string& asmarg,
5937 long precision);
5938 expression* try_parse_arg_varname (target_symbol *e,
5939 const string& asmarg,
5940 long precision);
5941 void visit_target_symbol_arg (target_symbol* e);
5942 void visit_target_symbol_context (target_symbol* e);
5943 void visit_atvar_op (atvar_op* e);
5944 void visit_cast_op (cast_op* e);
5945 };
5946
5947 void
5948 sdt_uprobe_var_expanding_visitor::build_dwarf_registers ()
5949 {
5950 /* Register name mapping table depends on the elf machine of this particular
5951 probe target process/file, not upon the host. So we can't just
5952 #ifdef _i686_ etc. */
5953
5954 #define DRI(name,num,width) dwarf_regs[name]=make_pair(num,width)
5955 if (elf_machine == EM_X86_64) {
5956 DRI ("%rax", 0, DI); DRI ("%eax", 0, SI); DRI ("%ax", 0, HI);
5957 DRI ("%al", 0, QI); DRI ("%ah", 0, QIh);
5958 DRI ("%rdx", 1, DI); DRI ("%edx", 1, SI); DRI ("%dx", 1, HI);
5959 DRI ("%dl", 1, QI); DRI ("%dh", 1, QIh);
5960 DRI ("%rcx", 2, DI); DRI ("%ecx", 2, SI); DRI ("%cx", 2, HI);
5961 DRI ("%cl", 2, QI); DRI ("%ch", 2, QIh);
5962 DRI ("%rbx", 3, DI); DRI ("%ebx", 3, SI); DRI ("%bx", 3, HI);
5963 DRI ("%bl", 3, QI); DRI ("%bh", 3, QIh);
5964 DRI ("%rsi", 4, DI); DRI ("%esi", 4, SI); DRI ("%si", 4, HI);
5965 DRI ("%sil", 4, QI);
5966 DRI ("%rdi", 5, DI); DRI ("%edi", 5, SI); DRI ("%di", 5, HI);
5967 DRI ("%dil", 5, QI);
5968 DRI ("%rbp", 6, DI); DRI ("%ebp", 6, SI); DRI ("%bp", 6, HI);
5969 DRI ("%bpl", 6, QI);
5970 DRI ("%rsp", 7, DI); DRI ("%esp", 7, SI); DRI ("%sp", 7, HI);
5971 DRI ("%spl", 7, QI);
5972 DRI ("%r8", 8, DI); DRI ("%r8d", 8, SI); DRI ("%r8w", 8, HI);
5973 DRI ("%r8b", 8, QI);
5974 DRI ("%r9", 9, DI); DRI ("%r9d", 9, SI); DRI ("%r9w", 9, HI);
5975 DRI ("%r9b", 9, QI);
5976 DRI ("%r10", 10, DI); DRI ("%r10d", 10, SI); DRI ("%r10w", 10, HI);
5977 DRI ("%r10b", 10, QI);
5978 DRI ("%r11", 11, DI); DRI ("%r11d", 11, SI); DRI ("%r11w", 11, HI);
5979 DRI ("%r11b", 11, QI);
5980 DRI ("%r12", 12, DI); DRI ("%r12d", 12, SI); DRI ("%r12w", 12, HI);
5981 DRI ("%r12b", 12, QI);
5982 DRI ("%r13", 13, DI); DRI ("%r13d", 13, SI); DRI ("%r13w", 13, HI);
5983 DRI ("%r13b", 13, QI);
5984 DRI ("%r14", 14, DI); DRI ("%r14d", 14, SI); DRI ("%r14w", 14, HI);
5985 DRI ("%r14b", 14, QI);
5986 DRI ("%r15", 15, DI); DRI ("%r15d", 15, SI); DRI ("%r15w", 15, HI);
5987 DRI ("%r15b", 15, QI);
5988 DRI ("%rip", 16, DI); DRI ("%eip", 16, SI); DRI ("%ip", 16, HI);
5989 } else if (elf_machine == EM_386) {
5990 DRI ("%eax", 0, SI); DRI ("%ax", 0, HI); DRI ("%al", 0, QI);
5991 DRI ("%ah", 0, QIh);
5992 DRI ("%ecx", 1, SI); DRI ("%cx", 1, HI); DRI ("%cl", 1, QI);
5993 DRI ("%ch", 1, QIh);
5994 DRI ("%edx", 2, SI); DRI ("%dx", 2, HI); DRI ("%dl", 2, QI);
5995 DRI ("%dh", 2, QIh);
5996 DRI ("%ebx", 3, SI); DRI ("%bx", 3, HI); DRI ("%bl", 3, QI);
5997 DRI ("%bh", 3, QIh);
5998 DRI ("%esp", 4, SI); DRI ("%sp", 4, HI);
5999 DRI ("%ebp", 5, SI); DRI ("%bp", 5, HI);
6000 DRI ("%esi", 6, SI); DRI ("%si", 6, HI); DRI ("%sil", 6, QI);
6001 DRI ("%edi", 7, SI); DRI ("%di", 7, HI); DRI ("%dil", 7, QI);
6002 } else if (elf_machine == EM_PPC || elf_machine == EM_PPC64) {
6003 DRI ("%r0", 0, DI);
6004 DRI ("%r1", 1, DI);
6005 DRI ("%r2", 2, DI);
6006 DRI ("%r3", 3, DI);
6007 DRI ("%r4", 4, DI);
6008 DRI ("%r5", 5, DI);
6009 DRI ("%r6", 6, DI);
6010 DRI ("%r7", 7, DI);
6011 DRI ("%r8", 8, DI);
6012 DRI ("%r9", 9, DI);
6013 DRI ("%r10", 10, DI);
6014 DRI ("%r11", 11, DI);
6015 DRI ("%r12", 12, DI);
6016 DRI ("%r13", 13, DI);
6017 DRI ("%r14", 14, DI);
6018 DRI ("%r15", 15, DI);
6019 DRI ("%r16", 16, DI);
6020 DRI ("%r17", 17, DI);
6021 DRI ("%r18", 18, DI);
6022 DRI ("%r19", 19, DI);
6023 DRI ("%r20", 20, DI);
6024 DRI ("%r21", 21, DI);
6025 DRI ("%r22", 22, DI);
6026 DRI ("%r23", 23, DI);
6027 DRI ("%r24", 24, DI);
6028 DRI ("%r25", 25, DI);
6029 DRI ("%r26", 26, DI);
6030 DRI ("%r27", 27, DI);
6031 DRI ("%r28", 28, DI);
6032 DRI ("%r29", 29, DI);
6033 DRI ("%r30", 30, DI);
6034 DRI ("%r31", 31, DI);
6035 // PR11821: unadorned register "names" without -mregnames
6036 DRI ("0", 0, DI);
6037 DRI ("1", 1, DI);
6038 DRI ("2", 2, DI);
6039 DRI ("3", 3, DI);
6040 DRI ("4", 4, DI);
6041 DRI ("5", 5, DI);
6042 DRI ("6", 6, DI);
6043 DRI ("7", 7, DI);
6044 DRI ("8", 8, DI);
6045 DRI ("9", 9, DI);
6046 DRI ("10", 10, DI);
6047 DRI ("11", 11, DI);
6048 DRI ("12", 12, DI);
6049 DRI ("13", 13, DI);
6050 DRI ("14", 14, DI);
6051 DRI ("15", 15, DI);
6052 DRI ("16", 16, DI);
6053 DRI ("17", 17, DI);
6054 DRI ("18", 18, DI);
6055 DRI ("19", 19, DI);
6056 DRI ("20", 20, DI);
6057 DRI ("21", 21, DI);
6058 DRI ("22", 22, DI);
6059 DRI ("23", 23, DI);
6060 DRI ("24", 24, DI);
6061 DRI ("25", 25, DI);
6062 DRI ("26", 26, DI);
6063 DRI ("27", 27, DI);
6064 DRI ("28", 28, DI);
6065 DRI ("29", 29, DI);
6066 DRI ("30", 30, DI);
6067 DRI ("31", 31, DI);
6068 } else if (elf_machine == EM_S390) {
6069 DRI ("%r0", 0, DI);
6070 DRI ("%r1", 1, DI);
6071 DRI ("%r2", 2, DI);
6072 DRI ("%r3", 3, DI);
6073 DRI ("%r4", 4, DI);
6074 DRI ("%r5", 5, DI);
6075 DRI ("%r6", 6, DI);
6076 DRI ("%r7", 7, DI);
6077 DRI ("%r8", 8, DI);
6078 DRI ("%r9", 9, DI);
6079 DRI ("%r10", 10, DI);
6080 DRI ("%r11", 11, DI);
6081 DRI ("%r12", 12, DI);
6082 DRI ("%r13", 13, DI);
6083 DRI ("%r14", 14, DI);
6084 DRI ("%r15", 15, DI);
6085 } else if (elf_machine == EM_ARM) {
6086 DRI ("r0", 0, SI);
6087 DRI ("r1", 1, SI);
6088 DRI ("r2", 2, SI);
6089 DRI ("r3", 3, SI);
6090 DRI ("r4", 4, SI);
6091 DRI ("r5", 5, SI);
6092 DRI ("r6", 6, SI);
6093 DRI ("r7", 7, SI);
6094 DRI ("r8", 8, SI);
6095 DRI ("r9", 9, SI);
6096 DRI ("r10", 10, SI); DRI ("sl", 10, SI);
6097 DRI ("fp", 11, SI);
6098 DRI ("ip", 12, SI);
6099 DRI ("sp", 13, SI);
6100 DRI ("lr", 14, SI);
6101 DRI ("pc", 15, SI);
6102 } else if (elf_machine == EM_AARCH64) {
6103 DRI ("x0", 0, DI); DRI ("w0", 0, SI);
6104 DRI ("x1", 1, DI); DRI ("w1", 1, SI);
6105 DRI ("x2", 2, DI); DRI ("w2", 2, SI);
6106 DRI ("x3", 3, DI); DRI ("w3", 3, SI);
6107 DRI ("x4", 4, DI); DRI ("w4", 4, SI);
6108 DRI ("x5", 5, DI); DRI ("w5", 5, SI);
6109 DRI ("x6", 6, DI); DRI ("w6", 6, SI);
6110 DRI ("x7", 7, DI); DRI ("w7", 7, SI);
6111 DRI ("x8", 8, DI); DRI ("w8", 8, SI);
6112 DRI ("x9", 9, DI); DRI ("w9", 9, SI);
6113 DRI ("x10", 10, DI); DRI ("w10", 10, SI);
6114 DRI ("x11", 11, DI); DRI ("w11", 11, SI);
6115 DRI ("x12", 12, DI); DRI ("w12", 12, SI);
6116 DRI ("x13", 13, DI); DRI ("w13", 13, SI);
6117 DRI ("x14", 14, DI); DRI ("w14", 14, SI);
6118 DRI ("x15", 15, DI); DRI ("w15", 15, SI);
6119 DRI ("x16", 16, DI); DRI ("w16", 16, SI);
6120 DRI ("x17", 17, DI); DRI ("w17", 17, SI);
6121 DRI ("x18", 18, DI); DRI ("w18", 18, SI);
6122 DRI ("x19", 19, DI); DRI ("w19", 19, SI);
6123 DRI ("x20", 20, DI); DRI ("w20", 20, SI);
6124 DRI ("x21", 21, DI); DRI ("w21", 21, SI);
6125 DRI ("x22", 22, DI); DRI ("w22", 22, SI);
6126 DRI ("x23", 23, DI); DRI ("w23", 23, SI);
6127 DRI ("x24", 24, DI); DRI ("w24", 24, SI);
6128 DRI ("x25", 25, DI); DRI ("w25", 25, SI);
6129 DRI ("x26", 26, DI); DRI ("w26", 26, SI);
6130 DRI ("x27", 27, DI); DRI ("w27", 27, SI);
6131 DRI ("x28", 28, DI); DRI ("w28", 28, SI);
6132 DRI ("x29", 29, DI); DRI ("w29", 29, SI);
6133 DRI ("x30", 30, DI); DRI ("w30", 30, SI);
6134 DRI ("sp", 31, DI);
6135 } else if (arg_count) {
6136 /* permit this case; just fall back to dwarf */
6137 }
6138 #undef DRI
6139
6140 // Build regex pieces out of the known dwarf_regs. We keep two separate
6141 // lists: ones with the % prefix (and thus unambigiuous even despite PR11821),
6142 // and ones with no prefix (and thus only usable in unambiguous contexts).
6143 map<string,pair<unsigned,int> >::const_iterator ri;
6144 for (ri = dwarf_regs.begin(); ri != dwarf_regs.end(); ri++)
6145 {
6146 string regname = ri->first;
6147 assert (regname != "");
6148 regnames += string("|")+regname;
6149 if (regname[0]=='%')
6150 percent_regnames += string("|")+regname;
6151 }
6152
6153 // clip off leading |
6154 if (regnames != "")
6155 regnames = regnames.substr(1);
6156 if (percent_regnames != "")
6157 percent_regnames = percent_regnames.substr(1);
6158 }
6159
6160 void
6161 sdt_uprobe_var_expanding_visitor::visit_target_symbol_context (target_symbol* e)
6162 {
6163 if (e->addressof)
6164 throw SEMANTIC_ERROR(_("cannot take address of context variable"), e->tok);
6165
6166 if (e->name == "$$name")
6167 {
6168 literal_string *myname = new literal_string (probe_name);
6169 myname->tok = e->tok;
6170 provide(myname);
6171 return;
6172 }
6173
6174 else if (e->name == "$$provider")
6175 {
6176 literal_string *myname = new literal_string (provider_name);
6177 myname->tok = e->tok;
6178 provide(myname);
6179 return;
6180 }
6181
6182 else if (e->name == "$$vars" || e->name == "$$parms")
6183 {
6184 e->assert_no_components("sdt", true);
6185
6186 // Convert $$vars to sprintf of a list of vars which we recursively evaluate
6187
6188 print_format* pf = print_format::create(e->tok, "sprintf");
6189
6190 for (unsigned i = 1; i <= arg_count; ++i)
6191 {
6192 if (i > 1)
6193 pf->raw_components += " ";
6194 target_symbol *tsym = new target_symbol;
6195 tsym->tok = e->tok;
6196 tsym->name = "$arg" + lex_cast(i);
6197 pf->raw_components += tsym->name;
6198 tsym->components = e->components;
6199
6200 expression *texp = require<expression> (tsym);
6201 if (e->check_pretty_print ())
6202 pf->raw_components += "=%s";
6203 else
6204 pf->raw_components += "=%#x";
6205 pf->args.push_back(texp);
6206 }
6207
6208 pf->components = print_format::string_to_components(pf->raw_components);
6209 provide (pf);
6210 }
6211 else
6212 assert(0); // shouldn't get here
6213 }
6214
6215 unsigned
6216 sdt_uprobe_var_expanding_visitor::get_target_symbol_argno_and_validate (target_symbol *e)
6217 {
6218 // parsing
6219 unsigned argno = 0;
6220 if (startswith(e->name, "$arg"))
6221 {
6222 try
6223 {
6224 argno = lex_cast<unsigned>(e->name.substr(4).to_string());
6225 }
6226 catch (const runtime_error& f)
6227 {
6228 // non-integral $arg suffix: e.g. $argKKKSDF
6229 argno = 0;
6230 }
6231 }
6232
6233 // validation
6234 if (arg_count == 0 || // a sdt.h variant without .probe-stored arg_count
6235 argno < 1 || argno > arg_count) // a $argN with out-of-range N
6236 {
6237 // NB: Either
6238 // 1) uprobe1_type $argN or $FOO (we don't know the arg_count)
6239 // 2) uprobe2_type $FOO (no probe args)
6240 // both of which get resolved later.
6241 // Throw it now, and it might be resolved by DWARF later.
6242 need_debug_info = true;
6243 throw SEMANTIC_ERROR(_("target-symbol requires debuginfo"), e->tok);
6244 }
6245 assert (arg_tokens.size() >= argno);
6246 return argno;
6247 }
6248
6249 long
6250 sdt_uprobe_var_expanding_visitor::parse_out_arg_precision(string& asmarg)
6251 {
6252 long precision;
6253 if (asmarg.find('@') != string::npos)
6254 {
6255 precision = lex_cast<int>(asmarg.substr(0, asmarg.find('@')));
6256 asmarg = asmarg.substr(asmarg.find('@')+1);
6257 }
6258 else
6259 {
6260 // V1/V2 do not have precision field so default to signed long
6261 // V3 asm does not have precision field so default to unsigned long
6262 if (probe_type == uprobe3_type)
6263 precision = sizeof(long); // this is an asm probe
6264 else
6265 precision = -sizeof(long);
6266 }
6267 return precision;
6268 }
6269
6270 expression*
6271 sdt_uprobe_var_expanding_visitor::try_parse_arg_literal (target_symbol *e,
6272 const string& asmarg,
6273 long precision)
6274 {
6275 expression *argexpr = NULL;
6276
6277 // Here, we test for a numeric literal.
6278 // Only accept (signed) decimals throughout. XXX
6279
6280 // PR11821. NB: on powerpc, literals are not prefixed with $,
6281 // so this regex does not match. But that's OK, since without
6282 // -mregnames, we can't tell them apart from register numbers
6283 // anyway. With -mregnames, we could, if gcc somehow
6284 // communicated to us the presence of that option, but alas it
6285 // doesn't. http://gcc.gnu.org/PR44995.
6286 vector<string> matches;
6287 string regexp;
6288
6289 if (elf_machine == EM_AARCH64) {
6290 regexp = "^([-]?[0-9][0-9]*)$";
6291 } else {
6292 regexp = "^[i\\$#]([-]?[0-9][0-9]*)$";
6293 }
6294
6295 if (!regexp_match (asmarg, regexp, matches)) {
6296 string sn =matches[1];
6297 int64_t n;
6298
6299 // We have to pay attention to the size & sign, as gcc sometimes
6300 // propagates constants that don't quite match, like a negative
6301 // value to fill an unsigned type.
6302 // NB: let it throw if something happens
6303 switch (precision)
6304 {
6305 case -1: n = lex_cast< int8_t>(sn); break;
6306 case 1: n = lex_cast< uint8_t>(sn); break;
6307 case -2: n = lex_cast< int16_t>(sn); break;
6308 case 2: n = lex_cast<uint16_t>(sn); break;
6309 case -4: n = lex_cast< int32_t>(sn); break;
6310 case 4: n = lex_cast<uint32_t>(sn); break;
6311 default:
6312 case -8: n = lex_cast< int64_t>(sn); break;
6313 case 8: n = lex_cast<uint64_t>(sn); break;
6314 }
6315
6316 literal_number* ln = new literal_number(n);
6317 ln->tok = e->tok;
6318 argexpr = ln;
6319 }
6320
6321 return argexpr;
6322 }
6323
6324 expression*
6325 sdt_uprobe_var_expanding_visitor::try_parse_arg_register (target_symbol *e,
6326 const string& asmarg,
6327 long precision)
6328 {
6329 expression *argexpr = NULL;
6330
6331 // test for REGISTER
6332 // NB: Because PR11821, we must use percent_regnames here.
6333 string regexp;
6334 if (elf_machine == EM_PPC || elf_machine == EM_PPC64
6335 || elf_machine == EM_ARM || elf_machine == EM_AARCH64)
6336 regexp = "^(" + regnames + ")$";
6337 else
6338 regexp = "^(" + percent_regnames + ")$";
6339
6340 vector<string> matches;
6341 if (!regexp_match(asmarg, regexp, matches))
6342 {
6343 string regname = matches[1];
6344 map<string,pair<unsigned,int> >::iterator ri = dwarf_regs.find (regname);
6345 if (ri != dwarf_regs.end()) // known register
6346 {
6347 embedded_expr *get_arg1 = new embedded_expr;
6348 string width_adjust;
6349 switch (ri->second.second)
6350 {
6351 case QI: width_adjust = ") & 0xff)"; break;
6352 case QIh: width_adjust = ">>8) & 0xff)"; break;
6353 case HI:
6354 // preserve 16 bit register signness
6355 width_adjust = ") & 0xffff)";
6356 if (precision < 0)
6357 width_adjust += " << 48 >> 48";
6358 break;
6359 case SI:
6360 // preserve 32 bit register signness
6361 width_adjust = ") & 0xffffffff)";
6362 if (precision < 0)
6363 width_adjust += " << 32 >> 32";
6364 break;
6365 default: width_adjust = "))";
6366 }
6367 string type = "";
6368 if (probe_type == uprobe3_type)
6369 type = (precision < 0
6370 ? "(int" : "(uint") + lex_cast(abs(precision) * 8) + "_t)";
6371 type = type + "((";
6372 get_arg1->tok = e->tok;
6373 get_arg1->code = string("/* unprivileged */ /* pure */")
6374 + string(" ((int64_t)") + type
6375 + string("u_fetch_register(")
6376 + lex_cast(dwarf_regs[regname].first) + string("))")
6377 + width_adjust;
6378 argexpr = get_arg1;
6379 }
6380 }
6381 return argexpr;
6382 }
6383
6384 static string
6385 precision_to_function(long precision)
6386 {
6387 switch (precision)
6388 {
6389 case 1: case -1:
6390 return "user_int8";
6391 case 2:
6392 return "user_uint16";
6393 case -2:
6394 return "user_int16";
6395 case 4:
6396 return "user_uint32";
6397 case -4:
6398 return "user_int32";
6399 case 8: case -8:
6400 return "user_int64";
6401 default:
6402 return "user_long";
6403 }
6404 }
6405
6406 expression*
6407 sdt_uprobe_var_expanding_visitor::try_parse_arg_offset_register (target_symbol *e,
6408 const string& asmarg,
6409 long precision)
6410 {
6411 expression *argexpr = NULL;
6412
6413 // test for OFFSET(REGISTER) where OFFSET is +-N+-N+-N
6414 // NB: Despite PR11821, we can use regnames here, since the parentheses
6415 // make things unambiguous. (Note: gdb/stap-probe.c also parses this)
6416 // On ARM test for [REGISTER, OFFSET]
6417
6418 string regexp;
6419 int reg, offset1;
6420 if (elf_machine == EM_ARM || elf_machine == EM_AARCH64)
6421 {
6422 regexp = "^\\[(" + regnames + ")(,[ ]*[#]?([+-]?[0-9]+)([+-][0-9]*)?([+-][0-9]*)?)?\\]$";
6423 reg = 1;
6424 offset1 = 3;
6425 }
6426 else
6427 {
6428 regexp = "^([+-]?[0-9]*)([+-][0-9]*)?([+-][0-9]*)?[(](" + regnames + ")[)]$";
6429 reg = 4;
6430 offset1 = 1;
6431 }
6432
6433 vector<string> matches;
6434 if (!regexp_match(asmarg, regexp, matches))
6435 {
6436 string regname;
6437 int64_t disp = 0;
6438 if (matches[reg].length())
6439 regname = matches[reg];
6440 if (dwarf_regs.find (regname) == dwarf_regs.end())
6441 throw SEMANTIC_ERROR(_F("unrecognized register '%s'", regname.c_str()));
6442
6443 for (int i=offset1; i <= (offset1 + 2); i++)
6444 if (matches[i].length())
6445 // should decode positive/negative hex/decimal
6446 // NB: let it throw if something happens
6447 disp += lex_cast<int64_t>(matches[i]);
6448
6449 // synthesize user_long(%{fetch_register(R)%} + D)
6450 embedded_expr *get_arg1 = new embedded_expr;
6451 get_arg1->tok = e->tok;
6452 get_arg1->code = string("/* unprivileged */ /* pure */")
6453 + string("u_fetch_register(")
6454 + lex_cast(dwarf_regs[regname].first) + string(")");
6455 // XXX: may we ever need to cast that to a narrower type?
6456
6457 literal_number* inc = new literal_number(disp);
6458 inc->tok = e->tok;
6459
6460 binary_expression *be = new binary_expression;
6461 be->tok = e->tok;
6462 be->left = get_arg1;
6463 be->op = "+";
6464 be->right = inc;
6465
6466 functioncall *fc = new functioncall;
6467 fc->function = precision_to_function(precision);
6468 fc->tok = e->tok;
6469 fc->args.push_back(be);
6470
6471 argexpr = fc;
6472 }
6473
6474 return argexpr;
6475 }
6476
6477 expression*
6478 sdt_uprobe_var_expanding_visitor::try_parse_arg_effective_addr (target_symbol *e,
6479 const string& asmarg,
6480 long precision)
6481 {
6482 expression *argexpr = NULL;
6483
6484 // test for OFFSET(BASE_REGISTER,INDEX_REGISTER[,SCALE]) where OFFSET is +-N+-N+-N
6485 // NB: Despite PR11821, we can use regnames here, since the parentheses
6486 // make things unambiguous. (Note: gdb/stap-probe.c also parses this)
6487 string regexp = "^([+-]?[0-9]*)([+-][0-9]*)?([+-][0-9]*)?[(](" + regnames + "),(" +
6488 regnames + ")(,[1248])?[)]$";
6489 vector<string> matches;
6490 if (!regexp_match(asmarg, regexp, matches))
6491 {
6492 string baseregname;
6493 string indexregname;
6494 int64_t disp = 0;
6495 short scale = 1;
6496
6497 if (matches[6].length())
6498 // NB: let it throw if we can't cast
6499 scale = lex_cast<short>(matches[6].substr(1)); // NB: skip the comma!
6500
6501 if (matches[4].length())
6502 baseregname = matches[4];
6503 if (dwarf_regs.find (baseregname) == dwarf_regs.end())
6504 throw SEMANTIC_ERROR(_F("unrecognized base register '%s'", baseregname.c_str()));
6505
6506 if (matches[5].length())
6507 indexregname = matches[5];
6508 if (dwarf_regs.find (indexregname) == dwarf_regs.end())
6509 throw SEMANTIC_ERROR(_F("unrecognized index register '%s'", indexregname.c_str()));
6510
6511 for (int i = 1; i <= 3; i++) // up to three OFFSET terms
6512 if (matches[i].length())
6513 // should decode positive/negative hex/decimal
6514 // NB: let it throw if something happens
6515 disp += lex_cast<int64_t>(matches[i]);
6516
6517 // synthesize user_long(%{fetch_register(R1)+fetch_register(R2)*N%} + D)
6518
6519 embedded_expr *get_arg1 = new embedded_expr;
6520 string regfn = "u_fetch_register";
6521
6522 get_arg1->tok = e->tok;
6523 get_arg1->code = string("/* unprivileged */ /* pure */")
6524 + regfn + string("(")+lex_cast(dwarf_regs[baseregname].first)+string(")")
6525 + string("+(")
6526 + regfn + string("(")+lex_cast(dwarf_regs[indexregname].first)+string(")")
6527 + string("*")
6528 + lex_cast(scale)
6529 + string(")");
6530
6531 // NB: could plop this +DISPLACEMENT bit into the embedded-c expression too
6532 literal_number* inc = new literal_number(disp);
6533 inc->tok = e->tok;
6534
6535 binary_expression *be = new binary_expression;
6536 be->tok = e->tok;
6537 be->left = get_arg1;
6538 be->op = "+";
6539 be->right = inc;
6540
6541 functioncall *fc = new functioncall;
6542 fc->function = precision_to_function(precision);
6543 fc->tok = e->tok;
6544 fc->args.push_back(be);
6545
6546 argexpr = fc;
6547 }
6548
6549 return argexpr;
6550 }
6551
6552 expression*
6553 sdt_uprobe_var_expanding_visitor::try_parse_arg_varname (target_symbol *e,
6554 const string& asmarg,
6555 long precision)
6556 {
6557 static unsigned tick = 0;
6558 expression *argexpr = NULL;
6559
6560 // test for [OFF+]VARNAME[+OFF][(REGISTER)], where VARNAME is a variable
6561 // name. NB: Despite PR11821, we can use regnames here, since the parentheses
6562 // make things unambiguous.
6563 string regex = "^(([0-9]+)[+])?([a-zA-Z_][a-zA-Z0-9_]*)([+][0-9]+)?([(]("
6564 + regnames + ")[)])?$";
6565 vector<string> matches;
6566 if (!regexp_match(asmarg, regex, matches))
6567 {
6568 assert(matches.size() >= 4);
6569 interned_string varname = matches[3];
6570
6571 // OFF can be before VARNAME (put in matches[2]) or after (put in
6572 // matches[4]) (or both?). Seems like in most cases it comes after,
6573 // unless the code was compiled with -fPIC.
6574 int64_t offset = 0;
6575 if (!matches[2].empty())
6576 offset += lex_cast<int64_t>(matches[2]);
6577 if (matches.size() >= 5 && !matches[4].empty())
6578 offset += lex_cast<int64_t>(matches[4]);
6579
6580 string regname;
6581 if (matches.size() >= 7)
6582 regname = matches[6];
6583
6584 // If it's just VARNAME, then proceed. If it's VARNAME(REGISTER), then
6585 // only proceed if it's RIP-relative addressing on x86_64.
6586 if (regname.empty() || (regname == "%rip" && elf_machine == EM_X86_64))
6587 {
6588 dw.mod_info->get_symtab();
6589 if (dw.mod_info->symtab_status != info_present)
6590 throw SEMANTIC_ERROR(_("can't retrieve symbol table"));
6591
6592 assert(dw.mod_info->sym_table);
6593 unordered_map<interned_string, Dwarf_Addr>& globals = dw.mod_info->sym_table->globals;
6594 unordered_map<interned_string, Dwarf_Addr>& locals = dw.mod_info->sym_table->locals;
6595 Dwarf_Addr addr = 0;
6596
6597 // check symtab locals then globals
6598 if (locals.count(varname))
6599 addr = locals[varname];
6600 if (globals.count(varname))
6601 addr = globals[varname];
6602
6603 if (addr)
6604 {
6605 // add whatever offset is in the operand
6606 addr += offset;
6607
6608 // adjust for dw bias because relocate_address() expects a
6609 // libdw address and this addr is from the symtab
6610 dw.get_module_dwarf(false, false);
6611 addr -= dw.module_bias;
6612
6613 interned_string reloc_section;
6614 Dwarf_Addr reloc_addr = dw.relocate_address(addr, reloc_section);
6615
6616 // OK, we have an address for the variable. Let's create a
6617 // function that will just relocate it at runtime, and then
6618 // call user_[u]int*() on the address it returns.
6619
6620 functioncall *user_int_call = new functioncall;
6621 user_int_call->function = precision_to_function(precision);
6622 user_int_call->tok = e->tok;
6623
6624 string fhash = detox_path(string(e->tok->location.file->name));
6625 functiondecl *get_addr_decl = new functiondecl;
6626 get_addr_decl->tok = e->tok;
6627 get_addr_decl->synthetic = true;
6628 get_addr_decl->name = "__private_" + fhash + "_sdt_arg_get_addr_" + lex_cast(tick++);
6629 get_addr_decl->type = pe_long;
6630
6631 // build _stp_umodule_relocate(module, addr, current)
6632 stringstream ss;
6633 ss << " /* unprivileged */ /* pure */ /* pragma:vma */" << endl;
6634 ss << "STAP_RETURN(_stp_umodule_relocate(";
6635 ss << "\"" << path_remove_sysroot(session, process_name) << "\", ";
6636 ss << "0x" << hex << reloc_addr << dec << ", ";
6637 ss << "current";
6638 ss << "));" << endl;
6639
6640 embeddedcode *ec = new embeddedcode;
6641 ec->tok = e->tok;
6642 ec->code = ss.str();
6643 get_addr_decl->body = ec;
6644 get_addr_decl->join(session);
6645
6646 functioncall *get_addr_call = new functioncall;
6647 get_addr_call->tok = e->tok;
6648 get_addr_call->function = get_addr_decl->name;
6649 user_int_call->args.push_back(get_addr_call);
6650
6651 argexpr = user_int_call;
6652 }
6653 }
6654 }
6655
6656 return argexpr;
6657 }
6658
6659 void
6660 sdt_uprobe_var_expanding_visitor::visit_target_symbol_arg (target_symbol *e)
6661 {
6662 try
6663 {
6664 unsigned argno = get_target_symbol_argno_and_validate(e); // the N in $argN
6665 string asmarg = arg_tokens[argno-1]; // $arg1 => arg_tokens[0]
6666
6667 // Now we try to parse this thing, which is an assembler operand
6668 // expression. If we can't, we warn, back down to need_debug_info
6669 // and hope for the best. Here is the syntax for a few architectures.
6670 // Note that the power iN syntax is only for V3 sdt.h; gcc emits the i.
6671 //
6672 // literal reg reg reg+ base+index*size+ VAR VAR+off RIP-relative
6673 // indirect offset offset VAR+off
6674 // x86 $N %rR (%rR) N(%rR) O(%bR,%iR,S) var var+off var+off(%rip)
6675 // x86_64 $N %rR (%rR) N(%rR) O(%bR,%iR,S) var var+off var+off(%rip)
6676 // power iN R (R) N(R)
6677 // ia64 N rR [r16]
6678 // s390 N %rR 0(rR) N(r15)
6679 // arm #N rR [rR] [rR, #N]
6680 // arm64 N rR [rR] [rR, N]
6681
6682 expression* argexpr = 0; // filled in in case of successful parse
6683
6684 // Parse (and remove from asmarg) the leading length
6685 long precision = parse_out_arg_precision(asmarg);
6686
6687 try
6688 {
6689 if ((argexpr = try_parse_arg_literal(e, asmarg, precision)) != NULL)
6690 goto matched;
6691
6692 // all other matches require registers
6693 if (regnames == "")
6694 throw SEMANTIC_ERROR("no registers to use for parsing");
6695
6696 if ((argexpr = try_parse_arg_register(e, asmarg, precision)) != NULL)
6697 goto matched;
6698 if ((argexpr = try_parse_arg_offset_register(e, asmarg, precision)) != NULL)
6699 goto matched;
6700 if ((argexpr = try_parse_arg_effective_addr(e, asmarg, precision)) != NULL)
6701 goto matched;
6702 if ((argexpr = try_parse_arg_varname(e, asmarg, precision)) != NULL)
6703 goto matched;
6704 }
6705 catch (const semantic_error& err)
6706 {
6707 e->chain(err);
6708 }
6709
6710 // The asmarg operand was not recognized. Back down to dwarf.
6711 if (! session.suppress_warnings)
6712 {
6713 if (probe_type == UPROBE3_TYPE)
6714 session.print_warning (_F("Can't parse SDT_V3 operand '%s' "
6715 "[man error::sdt]", asmarg.c_str()),
6716 e->tok);
6717 else // must be *PROBE2; others don't get asm operands
6718 session.print_warning (_F("Downgrading SDT_V2 probe argument to "
6719 "dwarf, can't parse '%s' [man error::sdt]",
6720 asmarg.c_str()),
6721 e->tok);
6722 }
6723
6724 need_debug_info = true;
6725 throw SEMANTIC_ERROR(_("SDT asm not understood, requires debuginfo "
6726 "[man error::sdt]"), e->tok);
6727
6728 /* NOTREACHED */
6729
6730 matched:
6731 assert (argexpr != 0);
6732
6733 if (session.verbose > 2)
6734 //TRANSLATORS: We're mapping the operand to a new expression*.
6735 clog << _F("mapped asm operand %s to ", asmarg.c_str()) << *argexpr << endl;
6736
6737 if (e->components.empty()) // We have a scalar
6738 {
6739 if (e->addressof)
6740 throw SEMANTIC_ERROR(_("cannot take address of sdt variable"), e->tok);
6741 provide (argexpr);
6742 }
6743 else // $var->foo
6744 {
6745 cast_op *cast = new cast_op;
6746 cast->name = "@cast";
6747 cast->tok = e->tok;
6748 cast->operand = argexpr;
6749 cast->components = e->components;
6750 cast->type_name = (string)probe_name + "_arg" + lex_cast(argno);
6751 cast->module = process_name;
6752 cast->visit(this);
6753 }
6754 }
6755 catch (const semantic_error &er)
6756 {
6757 e->chain (er);
6758 provide (e);
6759 }
6760 }
6761
6762
6763 void
6764 sdt_uprobe_var_expanding_visitor::visit_target_symbol (target_symbol* e)
6765 {
6766 try
6767 {
6768 assert(e->name.size() > 0
6769 && (e->name[0] == '$' || e->name == "@var"));
6770
6771 if (e->name == "$$name" || e->name == "$$provider" || e->name == "$$parms" || e->name == "$$vars")
6772 visit_target_symbol_context (e);
6773 else
6774 visit_target_symbol_arg (e);
6775 }
6776 catch (const semantic_error &er)
6777 {
6778 e->chain (er);
6779 provide (e);
6780 }
6781 }
6782
6783
6784 void
6785 sdt_uprobe_var_expanding_visitor::visit_atvar_op (atvar_op* e)
6786 {
6787 need_debug_info = true;
6788
6789 // Fill in our current module context if needed
6790 if (e->module.empty())
6791 e->module = process_name;
6792
6793 var_expanding_visitor::visit_atvar_op(e);
6794 }
6795
6796
6797 void
6798 sdt_uprobe_var_expanding_visitor::visit_cast_op (cast_op* e)
6799 {
6800 // Fill in our current module context if needed
6801 if (e->module.empty())
6802 e->module = process_name;
6803
6804 var_expanding_visitor::visit_cast_op(e);
6805 }
6806
6807
6808 void
6809 plt_expanding_visitor::visit_target_symbol (target_symbol *e)
6810 {
6811 try
6812 {
6813 if (e->name == "$$name")
6814 {
6815 literal_string *myname = new literal_string (entry);
6816 myname->tok = e->tok;
6817 provide(myname);
6818 return;
6819 }
6820
6821 // variable not found -> throw a semantic error
6822 // (only to be caught right away, but this may be more complex later...)
6823 string alternatives = "$$name";
6824 throw SEMANTIC_ERROR(_F("unable to find plt variable '%s' (alternatives: %s)",
6825 e->name.to_string().c_str(), alternatives.c_str()), e->tok);
6826 }
6827 catch (const semantic_error &er)
6828 {
6829 e->chain (er);
6830 provide (e);
6831 }
6832 }
6833
6834
6835 struct sdt_query : public base_query
6836 {
6837 sdt_query(probe * base_probe, probe_point * base_loc,
6838 dwflpp & dw, literal_map_t const & params,
6839 vector<derived_probe *> & results, const string user_lib);
6840
6841 void query_library (const char *data);
6842 set<string> visited_libraries;
6843 bool resolved_library;
6844
6845 void query_plt (const char *entry, size_t addr) {}
6846 void handle_query_module();
6847
6848 private:
6849 stap_sdt_probe_type probe_type;
6850 enum { probe_section=0, note_section=1, unknown_section=-1 } probe_loc;
6851 probe * base_probe;
6852 probe_point * base_loc;
6853 literal_map_t const & params;
6854 vector<derived_probe *> & results;
6855 interned_string pp_mark;
6856 interned_string pp_provider;
6857 string user_lib;
6858
6859 set<string> probes_handled;
6860
6861 Elf_Data *pdata;
6862 size_t probe_scn_offset;
6863 size_t probe_scn_addr;
6864 uint64_t arg_count;
6865 GElf_Addr base;
6866 GElf_Addr pc;
6867 string arg_string;
6868 string probe_name;
6869 string provider_name;
6870 GElf_Addr semaphore_load_offset;
6871 Dwarf_Addr semaphore;
6872
6873 bool init_probe_scn();
6874 bool get_next_probe();
6875 void iterate_over_probe_entries();
6876 void handle_probe_entry();
6877
6878 static void setup_note_probe_entry_callback (sdt_query *me,
6879 const string& scn_name,
6880 const string& note_name,
6881 int type,
6882 const char *data,
6883 size_t len);
6884 void setup_note_probe_entry (const string& scn_name,
6885 const string& note_name, int type,
6886 const char *data, size_t len);
6887
6888 void convert_probe(probe *base);
6889 void record_semaphore(vector<derived_probe *> & results, unsigned start);
6890 probe* convert_location();
6891 bool have_uprobe() {return probe_type == uprobe1_type || probe_type == uprobe2_type || probe_type == uprobe3_type;}
6892 bool have_debuginfo_uprobe(bool need_debug_info)
6893 {return probe_type == uprobe1_type
6894 || ((probe_type == uprobe2_type || probe_type == uprobe3_type)
6895 && need_debug_info);}
6896 bool have_debuginfoless_uprobe() {return probe_type == uprobe2_type || probe_type == uprobe3_type;}
6897 };
6898
6899
6900 sdt_query::sdt_query(probe * base_probe, probe_point * base_loc,
6901 dwflpp & dw, literal_map_t const & params,
6902 vector<derived_probe *> & results, const string user_lib):
6903 base_query(dw, params), resolved_library(false),
6904 probe_type(unknown_probe_type), probe_loc(unknown_section),
6905 base_probe(base_probe), base_loc(base_loc), params(params), results(results),
6906 user_lib(user_lib), pdata(0), probe_scn_offset(0), probe_scn_addr(0), arg_count(0),
6907 base(0), pc(0), semaphore_load_offset(0), semaphore(0)
6908 {
6909 assert(get_string_param(params, TOK_MARK, pp_mark));
6910 get_string_param(params, TOK_PROVIDER, pp_provider); // pp_provider == "" -> unspecified
6911
6912 // PR10245: permit usage of dtrace-y "-" separator in marker name;
6913 // map it to double-underscores.
6914 size_t pos = 0;
6915 string pp_mark2 = pp_mark; // copy for string replacement processing
6916 while (1) // there may be more than one
6917 {
6918 size_t i = pp_mark2.find("-", pos);
6919 if (i == string::npos) break;
6920 pp_mark2.replace (i, 1, "__");
6921 pos = i+1; // resume searching after the inserted __
6922 }
6923 pp_mark = pp_mark2;
6924
6925 // XXX: same for pp_provider?
6926 }
6927
6928
6929 void
6930 sdt_query::handle_probe_entry()
6931 {
6932 if (! have_uprobe()
6933 && !probes_handled.insert(probe_name).second)
6934 return;
6935
6936 if (sess.verbose > 3)
6937 {
6938 //TRANSLATORS: Describing what probe type (kprobe or uprobe) the probe
6939 //TRANSLATORS: is matched to.
6940 clog << _F("matched probe_name %s probe type ", probe_name.c_str());
6941 switch (probe_type)
6942 {
6943 case uprobe1_type:
6944 clog << "uprobe1 at 0x" << hex << pc << dec << endl;
6945 break;
6946 case uprobe2_type:
6947 clog << "uprobe2 at 0x" << hex << pc << dec << endl;
6948 break;
6949 case uprobe3_type:
6950 clog << "uprobe3 at 0x" << hex << pc << dec << endl;
6951 break;
6952 default:
6953 clog << "unknown!" << endl;
6954 break;
6955 }
6956 }
6957
6958 // Extend the derivation chain
6959 probe *new_base = convert_location();
6960 probe_point *new_location = new_base->locations[0];
6961
6962 bool need_debug_info = false;
6963
6964 // We could get the Elf* from either dwarf_getelf(dwfl_module_getdwarf(...))
6965 // or dwfl_module_getelf(...). We only need it for the machine type, which
6966 // should be the same. The bias is used for relocating debuginfoless probes,
6967 // though, so that must come from the possibly-prelinked ELF file, not DWARF.
6968 Dwarf_Addr bias;
6969 Elf* elf = dwfl_module_getelf (dw.mod_info->mod, &bias);
6970
6971 /* Figure out the architecture of this particular ELF file. The
6972 dwarfless register-name mappings depend on it. */
6973 GElf_Ehdr ehdr_mem;
6974 GElf_Ehdr* em = gelf_getehdr (elf, &ehdr_mem);
6975 if (em == 0) { DWFL_ASSERT ("dwfl_getehdr", dwfl_errno()); }
6976 assert(em);
6977 int elf_machine = em->e_machine;
6978 sdt_uprobe_var_expanding_visitor svv (sess, dw, elf_machine, module_val,
6979 provider_name, probe_name, probe_type,
6980 arg_string, arg_count);
6981 svv.replace (new_base->body);
6982 need_debug_info = svv.need_debug_info;
6983
6984 // XXX: why not derive_probes() in the uprobes case too?
6985 literal_map_t params;
6986 for (unsigned i = 0; i < new_location->components.size(); ++i)
6987 {
6988 probe_point::component *c = new_location->components[i];
6989 params[c->functor] = c->arg;
6990 }
6991
6992 unsigned prior_results_size = results.size();
6993 dwarf_query q(new_base, new_location, dw, params, results, "", "");
6994 q.has_mark = true; // enables mid-statement probing
6995
6996 // V1 probes always need dwarf info
6997 // V2+ probes need dwarf info in case of a variable reference
6998 if (have_debuginfo_uprobe(need_debug_info))
6999 dw.iterate_over_modules<base_query>(&query_module, &q);
7000
7001 // For V2+ probes, if variable references weren't used or failed (PR14369),
7002 // then try with the more direct approach. Unresolved $vars might still
7003 // cause their own error, but this gives them a chance to be optimized out.
7004 if (have_debuginfoless_uprobe() && results.size() == prior_results_size)
7005 {
7006 string section;
7007 Dwarf_Addr reloc_addr = q.statement_num_val + bias;
7008 if (dwfl_module_relocations (q.dw.mod_info->mod) > 0)
7009 {
7010 dwfl_module_relocate_address (q.dw.mod_info->mod, &reloc_addr);
7011 section = ".dynamic";
7012 }
7013 else
7014 section = ".absolute";
7015
7016 uprobe_derived_probe* p =
7017 new uprobe_derived_probe ("", "", 0,
7018 path_remove_sysroot(sess,q.module_val),
7019 section,
7020 q.statement_num_val, reloc_addr, q, 0);
7021 p->saveargs (arg_count);
7022 results.push_back (p);
7023 }
7024 sess.unwindsym_modules.insert (dw.module_name);
7025 record_semaphore(results, prior_results_size);
7026 }
7027
7028
7029 void
7030 sdt_query::handle_query_module()
7031 {
7032 if (!init_probe_scn())
7033 return;
7034
7035 if (sess.verbose > 3)
7036 clog << "TOK_MARK: " << pp_mark << " TOK_PROVIDER: " << pp_provider << endl;
7037
7038 if (probe_loc == note_section)
7039 {
7040 GElf_Shdr shdr_mem;
7041 GElf_Shdr *shdr = dw.get_section (".stapsdt.base", &shdr_mem);
7042
7043 // The 'base' lets us adjust the hardcoded addresses in notes for prelink
7044 // effects. The 'semaphore_load_offset' accounts for the difference in
7045 // load addresses between text and data, so the semaphore can be
7046 // converted to a file offset if needed.
7047 if (shdr)
7048 {
7049 base = shdr->sh_addr;
7050 GElf_Addr base_offset = shdr->sh_offset;
7051 shdr = dw.get_section (".probes", &shdr_mem);
7052 if (shdr)
7053 semaphore_load_offset =
7054 (shdr->sh_addr - shdr->sh_offset) - (base - base_offset);
7055 }
7056 else
7057 base = semaphore_load_offset = 0;
7058
7059 dw.iterate_over_notes (this, &sdt_query::setup_note_probe_entry_callback);
7060 }
7061 else if (probe_loc == probe_section)
7062 iterate_over_probe_entries ();
7063 }
7064
7065
7066 bool
7067 sdt_query::init_probe_scn()
7068 {
7069 Elf* elf;
7070 GElf_Shdr shdr_mem;
7071
7072 GElf_Shdr *shdr = dw.get_section (".note.stapsdt", &shdr_mem);
7073 if (shdr)
7074 {
7075 probe_loc = note_section;
7076 return true;
7077 }
7078
7079 shdr = dw.get_section (".probes", &shdr_mem, &elf);
7080 if (shdr)
7081 {
7082 pdata = elf_getdata_rawchunk (elf, shdr->sh_offset, shdr->sh_size, ELF_T_BYTE);
7083 probe_scn_offset = 0;
7084 probe_scn_addr = shdr->sh_addr;
7085 assert (pdata != NULL);
7086 if (sess.verbose > 4)
7087 clog << "got .probes elf scn_addr@0x" << probe_scn_addr << ", size: "
7088 << pdata->d_size << endl;
7089 probe_loc = probe_section;
7090 return true;
7091 }
7092 else
7093 return false;
7094 }
7095
7096 void
7097 sdt_query::setup_note_probe_entry_callback (sdt_query *me,
7098 const string& scn_name,
7099 const string& note_name, int type,
7100 const char *data, size_t len)
7101 {
7102 me->setup_note_probe_entry (scn_name, note_name, type, data, len);
7103 }
7104
7105
7106 void
7107 sdt_query::setup_note_probe_entry (const string& scn_name,
7108 const string& note_name, int type,
7109 const char *data, size_t len)
7110 {
7111 if (scn_name.compare(".note.stapsdt"))
7112 return;
7113 #define _SDT_NOTE_NAME "stapsdt"
7114 if (note_name.compare(_SDT_NOTE_NAME))
7115 return;
7116 #define _SDT_NOTE_TYPE 3
7117 if (type != _SDT_NOTE_TYPE)
7118 return;
7119
7120 // we found a probe entry
7121 union
7122 {
7123 Elf64_Addr a64[3];
7124 Elf32_Addr a32[3];
7125 } buf;
7126 Dwarf_Addr bias;
7127 Elf* elf = (dwfl_module_getelf (dw.mod_info->mod, &bias));
7128 Elf_Data dst =
7129 {
7130 &buf, ELF_T_ADDR, EV_CURRENT,
7131 gelf_fsize (elf, ELF_T_ADDR, 3, EV_CURRENT), 0, 0
7132 };
7133 assert (dst.d_size <= sizeof buf);
7134
7135 if (len < dst.d_size + 3)
7136 return;
7137
7138 Elf_Data src =
7139 {
7140 (void *) data, ELF_T_ADDR, EV_CURRENT,
7141 dst.d_size, 0, 0
7142 };
7143
7144 if (gelf_xlatetom (elf, &dst, &src,
7145 elf_getident (elf, NULL)[EI_DATA]) == NULL)
7146 printf ("gelf_xlatetom: %s", elf_errmsg (-1));
7147
7148 probe_type = uprobe3_type;
7149 const char * provider = data + dst.d_size;
7150
7151 const char *name = (const char*)memchr (provider, '\0', data + len - provider);
7152 if(name++ == NULL)
7153 return;
7154
7155 const char *args = (const char*)memchr (name, '\0', data + len - name);
7156 if (args++ == NULL || memchr (args, '\0', data + len - name) != data + len - 1)
7157 return;
7158
7159 provider_name = provider;
7160 probe_name = name;
7161 arg_string = args;
7162
7163 dw.mod_info->marks.insert(make_pair(provider, name));
7164
7165 // Did we find a matching probe?
7166 if (! (dw.function_name_matches_pattern (probe_name, pp_mark)
7167 && ((pp_provider == "")
7168 || dw.function_name_matches_pattern (provider_name, pp_provider))))
7169 return;
7170
7171 // PR13934: Assembly probes are not forced to use the N@OP form.
7172 // If we have '@' then great, else count based on space-delimiters.
7173 arg_count = count(arg_string.begin(), arg_string.end(), '@');
7174 if (!arg_count && !arg_string.empty())
7175 arg_count = 1 + count(arg_string.begin(), arg_string.end(), ' ');
7176
7177 GElf_Addr base_ref;
7178 if (gelf_getclass (elf) == ELFCLASS32)
7179 {
7180 pc = buf.a32[0];
7181 base_ref = buf.a32[1];
7182 semaphore = buf.a32[2];
7183 }
7184 else
7185 {
7186 pc = buf.a64[0];
7187 base_ref = buf.a64[1];
7188 semaphore = buf.a64[2];
7189 }
7190
7191 semaphore += base - base_ref;
7192 pc += base - base_ref;
7193
7194 // The semaphore also needs the ELF bias added now, so
7195 // record_semaphore can properly relocate it later.
7196 semaphore += bias;
7197
7198 if (sess.verbose > 4)
7199 clog << _F(" saw .note.stapsdt %s%s ", probe_name.c_str(), (provider_name != "" ? _(" (provider ")+provider_name+") " : "").c_str()) << "@0x" << hex << pc << dec << endl;
7200
7201 handle_probe_entry();
7202 }
7203
7204
7205 void
7206 sdt_query::iterate_over_probe_entries()
7207 {
7208 // probes are in the .probe section
7209 while (probe_scn_offset < pdata->d_size)
7210 {
7211 stap_sdt_probe_entry_v1 *pbe_v1 = (stap_sdt_probe_entry_v1 *) ((char*)pdata->d_buf + probe_scn_offset);
7212 stap_sdt_probe_entry_v2 *pbe_v2 = (stap_sdt_probe_entry_v2 *) ((char*)pdata->d_buf + probe_scn_offset);
7213 probe_type = (stap_sdt_probe_type)(pbe_v1->type_a);
7214 if (! have_uprobe())
7215 {
7216 // Unless this is a mangled .probes section, this happens
7217 // because the name of the probe comes first, followed by
7218 // the sentinel.
7219 if (sess.verbose > 5)
7220 clog << _F("got unknown probe_type : 0x%x", probe_type) << endl;
7221 probe_scn_offset += sizeof(__uint32_t);
7222 continue;
7223 }
7224 if ((long)pbe_v1 % sizeof(__uint64_t)) // we have stap_sdt_probe_entry_v1.type_b
7225 {
7226 pbe_v1 = (stap_sdt_probe_entry_v1*)((char*)pbe_v1 - sizeof(__uint32_t));
7227 if (pbe_v1->type_b != uprobe1_type)
7228 continue;
7229 }
7230
7231 if (probe_type == uprobe1_type)
7232 {
7233 if (pbe_v1->name == 0) // No name possibly means we have a .so with a relocation
7234 return;
7235 semaphore = 0;
7236 probe_name = (char*)((char*)pdata->d_buf + pbe_v1->name - (char*)probe_scn_addr);
7237 provider_name = ""; // unknown
7238 pc = pbe_v1->arg;
7239 arg_count = 0;
7240 probe_scn_offset += sizeof (stap_sdt_probe_entry_v1);
7241 }
7242 else if (probe_type == uprobe2_type)
7243 {
7244 if (pbe_v2->name == 0) // No name possibly means we have a .so with a relocation
7245 return;
7246 semaphore = pbe_v2->semaphore;
7247 probe_name = (char*)((char*)pdata->d_buf + pbe_v2->name - (char*)probe_scn_addr);
7248 provider_name = (char*)((char*)pdata->d_buf + pbe_v2->provider - (char*)probe_scn_addr);
7249 arg_count = pbe_v2->arg_count;
7250 pc = pbe_v2->pc;
7251 if (pbe_v2->arg_string)
7252 arg_string = (char*)((char*)pdata->d_buf + pbe_v2->arg_string - (char*)probe_scn_addr);
7253 // skip over pbe_v2, probe_name text and provider text
7254 probe_scn_offset = ((long)(pbe_v2->name) - (long)(probe_scn_addr)) + probe_name.length();
7255 probe_scn_offset += sizeof (__uint32_t) - probe_scn_offset % sizeof (__uint32_t);
7256 }
7257 if (sess.verbose > 4)
7258 clog << _("saw .probes ") << probe_name << (provider_name != "" ? _(" (provider ")+provider_name+") " : "")
7259 << "@0x" << hex << pc << dec << endl;
7260
7261 dw.mod_info->marks.insert(make_pair(provider_name, probe_name));
7262
7263 if (dw.function_name_matches_pattern (probe_name, pp_mark)
7264 && ((pp_provider == "") || dw.function_name_matches_pattern (provider_name, pp_provider)))
7265 handle_probe_entry ();
7266 }
7267 }
7268
7269
7270 void
7271 sdt_query::record_semaphore (vector<derived_probe *> & results, unsigned start)
7272 {
7273 for (unsigned i=0; i<2; i++) {
7274 // prefer with-provider symbol; look without provider prefix for backward compatibility only
7275 string semaphore = (i==0 ? (provider_name+"_") : "") + probe_name + "_semaphore";
7276 // XXX: multiple addresses?
7277 if (sess.verbose > 2)
7278 clog << _F("looking for semaphore symbol %s ", semaphore.c_str());
7279
7280 Dwarf_Addr addr;
7281 if (this->semaphore)
7282 addr = this->semaphore;
7283 else
7284 addr = lookup_symbol_address(dw.module, semaphore.c_str());
7285 if (addr)
7286 {
7287 if (dwfl_module_relocations (dw.module) > 0)
7288 dwfl_module_relocate_address (dw.module, &addr);
7289 // XXX: relocation basis?
7290
7291 // Dyninst needs the *file*-based offset for semaphores,
7292 // so subtract the difference in load addresses between .text and .probes
7293 if (dw.sess.runtime_usermode_p())
7294 addr -= semaphore_load_offset;
7295
7296 for (unsigned i = start; i < results.size(); ++i)
7297 results[i]->sdt_semaphore_addr = addr;
7298 if (sess.verbose > 2)
7299 clog << _(", found at 0x") << hex << addr << dec << endl;
7300 return;
7301 }
7302 else
7303 if (sess.verbose > 2)
7304 clog << _(", not found") << endl;
7305 }
7306 }
7307
7308
7309 void
7310 sdt_query::convert_probe (probe *base)
7311 {
7312 block *b = new block;
7313 b->tok = base->body->tok;
7314
7315 // Generate: if (arg1 != mark("label")) next;
7316 functioncall *fc = new functioncall;
7317 fc->function = "ulong_arg";
7318 fc->tok = b->tok;
7319 literal_number* num = new literal_number(1);
7320 num->tok = b->tok;
7321 fc->args.push_back(num);
7322
7323 functioncall *fcus = new functioncall;
7324 fcus->function = "user_string";
7325 fcus->type = pe_string;
7326 fcus->tok = b->tok;
7327 fcus->args.push_back(fc);
7328
7329 if_statement *is = new if_statement;
7330 is->thenblock = new next_statement;
7331 is->elseblock = NULL;
7332 is->tok = b->tok;
7333 is->thenblock->tok = b->tok;
7334 comparison *be = new comparison;
7335 be->op = "!=";
7336 be->tok = b->tok;
7337 be->left = fcus;
7338 be->right = new literal_string(probe_name);
7339 be->right->tok = b->tok;
7340 is->condition = be;
7341 b->statements.push_back(is);
7342
7343 // Now replace the body
7344 b->statements.push_back(base->body);
7345 base->body = b;
7346 }
7347
7348
7349 probe*
7350 sdt_query::convert_location ()
7351 {
7352 interned_string module = dw.module_name;
7353 if (has_process)
7354 module = path_remove_sysroot(sess, module);
7355
7356 probe_point* specific_loc = new probe_point(*base_loc);
7357 specific_loc->well_formed = true;
7358
7359 vector<probe_point::component*> derived_comps;
7360
7361 vector<probe_point::component*>::iterator it;
7362 for (it = specific_loc->components.begin();
7363 it != specific_loc->components.end(); ++it)
7364 if ((*it)->functor == TOK_PROCESS)
7365 {
7366 // replace the possibly incomplete path to process
7367 *it = new probe_point::component(TOK_PROCESS,
7368 new literal_string(has_library ? path : module));
7369
7370 // copy the process name
7371 derived_comps.push_back(*it);
7372 }
7373 else if ((*it)->functor == TOK_LIBRARY)
7374 {
7375 // copy the library name for process probes
7376 derived_comps.push_back(*it);
7377 }
7378 else if ((*it)->functor == TOK_PROVIDER)
7379 {
7380 // replace the possibly wildcarded arg with the specific provider name
7381 *it = new probe_point::component(TOK_PROVIDER,
7382 new literal_string(provider_name));
7383 }
7384 else if ((*it)->functor == TOK_MARK)
7385 {
7386 // replace the possibly wildcarded arg with the specific marker name
7387 *it = new probe_point::component(TOK_MARK,
7388 new literal_string(probe_name));
7389
7390 if (sess.verbose > 3)
7391 switch (probe_type)
7392 {
7393 case uprobe1_type:
7394 clog << _("probe_type == uprobe1, use statement addr: 0x")
7395 << hex << pc << dec << endl;
7396 break;
7397 case uprobe2_type:
7398 clog << _("probe_type == uprobe2, use statement addr: 0x")
7399 << hex << pc << dec << endl;
7400 break;
7401 case uprobe3_type:
7402 clog << _("probe_type == uprobe3, use statement addr: 0x")
7403 << hex << pc << dec << endl;
7404 break;
7405 default:
7406 clog << _F("probe_type == use_uprobe_no_dwarf, use label name: _stapprobe1_%s",
7407 pp_mark.to_string().c_str()) << endl;
7408 }
7409
7410 switch (probe_type)
7411 {
7412 case uprobe1_type:
7413 case uprobe2_type:
7414 case uprobe3_type:
7415 // process("executable").statement(probe_arg)
7416 derived_comps.push_back
7417 (new probe_point::component(TOK_STATEMENT,
7418 new literal_number(pc, true)));
7419 break;
7420
7421 default: // deprecated
7422 // process("executable").function("*").label("_stapprobe1_MARK_NAME")
7423 derived_comps.push_back
7424 (new probe_point::component(TOK_FUNCTION,
7425 new literal_string(string("*"))));
7426 derived_comps.push_back
7427 (new probe_point::component(TOK_LABEL,
7428 new literal_string(string("_stapprobe1_") + (string)pp_mark)));
7429 break;
7430 }
7431 }
7432
7433 probe_point* derived_loc = new probe_point(*specific_loc);
7434 derived_loc->components = derived_comps;
7435 return new probe (new probe (base_probe, specific_loc), derived_loc);
7436 }
7437
7438
7439 void
7440 sdt_query::query_library (const char *library)
7441 {
7442 visited_libraries.insert(library);
7443 if (query_one_library (library, dw, user_lib, base_probe, base_loc, results))
7444 resolved_library = true;
7445 }
7446
7447 string
7448 suggest_marks(systemtap_session& sess,
7449 const set<string>& modules,
7450 const string& mark,
7451 const string& provider)
7452 {
7453 if (mark.empty() || modules.empty() || sess.module_cache == NULL)
7454 return "";
7455
7456 // PR18577: There isn't any point in generating a suggestion list if
7457 // we're not going to display it.
7458 if ((sess.dump_mode == systemtap_session::dump_matched_probes
7459 || sess.dump_mode == systemtap_session::dump_matched_probes_vars)
7460 && sess.verbose < 2)
7461 return "";
7462
7463 set<string> marks;
7464 const map<string, module_info*> &cache = sess.module_cache->cache;
7465 bool dash_suggestions = (mark.find("-") != string::npos);
7466
7467 for (set<string>::iterator itmod = modules.begin();
7468 itmod != modules.end(); ++itmod)
7469 {
7470 map<string, module_info*>::const_iterator itcache;
7471 if ((itcache = cache.find(*itmod)) != cache.end())
7472 {
7473 set<pair<string,string> >::const_iterator itmarks;
7474 for (itmarks = itcache->second->marks.begin();
7475 itmarks != itcache->second->marks.end(); ++itmarks)
7476 {
7477 if (provider.empty()
7478 // simulating dw.function_name_matches_pattern()
7479 || (fnmatch(provider.c_str(), itmarks->first.c_str(), 0) == 0))
7480 {
7481 string marksug = itmarks->second;
7482 if (dash_suggestions)
7483 {
7484 size_t pos = 0;
7485 while (1) // there may be more than one
7486 {
7487 size_t i = marksug.find("__", pos);
7488 if (i == string::npos) break;
7489 marksug.replace (i, 2, "-");
7490 pos = i+1; // resume searching after the inserted -
7491 }
7492 }
7493 marks.insert(marksug);
7494 }
7495 }
7496 }
7497 }
7498
7499 if (sess.verbose > 2)
7500 {
7501 clog << "suggesting " << marks.size() << " marks "
7502 << "from modules:" << endl;
7503 for (set<string>::iterator itmod = modules.begin();
7504 itmod != modules.end(); ++itmod)
7505 clog << *itmod << endl;
7506 }
7507
7508 if (marks.empty())
7509 return "";
7510
7511 return levenshtein_suggest(mark, marks, 5); // print top 5 marks only
7512 }
7513
7514 string
7515 suggest_plt_functions(systemtap_session& sess,
7516 const set<string>& modules,
7517 const string& func)
7518 {
7519 if (func.empty() || modules.empty() || sess.module_cache == NULL)
7520 return "";
7521
7522 // PR18577: There isn't any point in generating a suggestion list if
7523 // we're not going to display it.
7524 if ((sess.dump_mode == systemtap_session::dump_matched_probes
7525 || sess.dump_mode == systemtap_session::dump_matched_probes_vars)
7526 && sess.verbose < 2)
7527 return "";
7528
7529 set<interned_string> funcs;
7530 const map<string, module_info*> &cache = sess.module_cache->cache;
7531
7532 for (set<string>::iterator itmod = modules.begin();
7533 itmod != modules.end(); ++itmod)
7534 {
7535 map<string, module_info*>::const_iterator itcache;
7536 if ((itcache = cache.find(*itmod)) != cache.end())
7537 funcs.insert(itcache->second->plt_funcs.begin(),
7538 itcache->second->plt_funcs.end());
7539 }
7540
7541 if (sess.verbose > 2)
7542 {
7543 clog << "suggesting " << funcs.size() << " plt functions "
7544 << "from modules:" << endl;
7545 for (set<string>::iterator itmod = modules.begin();
7546 itmod != modules.end(); ++itmod)
7547 clog << *itmod << endl;
7548 }
7549
7550 if (funcs.empty())
7551 return "";
7552
7553 return levenshtein_suggest(func, funcs, 5); // print top 5 funcs only
7554 }
7555
7556 string
7557 suggest_dwarf_functions(systemtap_session& sess,
7558 const set<string>& modules,
7559 string func)
7560 {
7561 // Trim any @ component
7562 size_t pos = func.find('@');
7563 if (pos != string::npos)
7564 func.erase(pos);
7565
7566 if (func.empty() || modules.empty() || sess.module_cache == NULL)
7567 return "";
7568
7569 // PR18577: There isn't any point in generating a suggestion list if
7570 // we're not going to display it.
7571 if ((sess.dump_mode == systemtap_session::dump_matched_probes
7572 || sess.dump_mode == systemtap_session::dump_matched_probes_vars)
7573 && sess.verbose < 2)
7574 return "";
7575
7576 // We must first aggregate all the functions from the cache
7577 set<interned_string> funcs;
7578 const map<string, module_info*> &cache = sess.module_cache->cache;
7579
7580 for (set<string>::iterator itmod = modules.begin();
7581 itmod != modules.end(); ++itmod)
7582 {
7583 module_info *module;
7584
7585 // retrieve module_info from cache
7586 map<string, module_info*>::const_iterator itcache;
7587 if ((itcache = cache.find(*itmod)) != cache.end())
7588 module = itcache->second;
7589 else // module not found
7590 continue;
7591
7592 // add inlines
7593 funcs.insert(module->inlined_funcs.begin(),
7594 module->inlined_funcs.end());
7595
7596 // add all function symbols in cache
7597 if (module->symtab_status != info_present || module->sym_table == NULL)
7598 continue;
7599 unordered_multimap<interned_string, func_info*>& modfuncs = module->sym_table->map_by_name;
7600 for (unordered_multimap<interned_string, func_info*>::const_iterator itfuncs = modfuncs.begin();
7601 itfuncs != modfuncs.end(); ++itfuncs)
7602 funcs.insert(itfuncs->first);
7603 }
7604
7605 if (sess.verbose > 2)
7606 {
7607 clog << "suggesting " << funcs.size() << " dwarf functions "
7608 << "from modules:" << endl;
7609 for (set<string>::iterator itmod = modules.begin();
7610 itmod != modules.end(); ++itmod)
7611 clog << *itmod << endl;
7612 }
7613
7614 if (funcs.empty())
7615 return "";
7616
7617 return levenshtein_suggest(func, funcs, 5); // print top 5 funcs only
7618 }
7619
7620
7621 // Use a glob pattern to find executables or shared libraries
7622 static set<string>
7623 glob_executable(const string& pattern)
7624 {
7625 glob_t the_blob;
7626 set<string> globs;
7627
7628 int rc = glob (pattern.c_str(), 0, NULL, & the_blob);
7629 if (rc)
7630 throw SEMANTIC_ERROR (_F("glob %s error (%d)", pattern.c_str(), rc));
7631
7632 for (unsigned i = 0; i < the_blob.gl_pathc; ++i)
7633 {
7634 const char* globbed = the_blob.gl_pathv[i];
7635 struct stat st;
7636
7637 if (access (globbed, X_OK) == 0
7638 && stat (globbed, &st) == 0
7639 && S_ISREG (st.st_mode)) // see find_executable()
7640 {
7641 // Need to call resolve_path here, in order to path-expand
7642 // patterns like process("stap*"). Otherwise it may go through
7643 // to the next round of expansion as ("stap"), leading to a $PATH
7644 // search that's not consistent with the glob search already done.
7645 string canononicalized = resolve_path (globbed);
7646
7647 // The canonical names can result in duplication, for example
7648 // having followed symlinks that are common with shared libraries,
7649 // so we use a set for unique results.
7650 globs.insert(canononicalized);
7651 }
7652 }
7653
7654 globfree (& the_blob);
7655 return globs;
7656 }
7657
7658 static bool
7659 resolve_library_by_path(base_query & q,
7660 set<string> const & visited_libraries,
7661 probe * base,
7662 probe_point * location,
7663 literal_map_t const & parameters,
7664 vector<derived_probe *> & finished_results)
7665 {
7666 size_t results_pre = finished_results.size();
7667 systemtap_session & sess = q.sess;
7668 dwflpp & dw = q.dw;
7669
7670 interned_string lib;
7671 if (!location->from_globby_comp(TOK_LIBRARY) && q.has_library
7672 && !visited_libraries.empty()
7673 && q.get_string_param(parameters, TOK_LIBRARY, lib))
7674 {
7675 // The library didn't fit any DT_NEEDED libraries. As a last effort,
7676 // let's try to look for the library directly.
7677
7678 if (contains_glob_chars (lib))
7679 {
7680 // Evaluate glob here, and call derive_probes recursively with each match.
7681 set<string> globs = glob_executable (lib);
7682 for (set<string>::const_iterator it = globs.begin();
7683 it != globs.end(); ++it)
7684 {
7685 assert_no_interrupts();
7686
7687 const string& globbed = *it;
7688 if (sess.verbose > 1)
7689 clog << _F("Expanded library(\"%s\") to library(\"%s\")",
7690 lib.to_string().c_str(), globbed.c_str()) << endl;
7691
7692 probe *new_base = build_library_probe(dw, globbed,
7693 base, location);
7694
7695 // We override "optional = true" here, as if the
7696 // wildcarded probe point was given a "?" suffix.
7697
7698 // This is because wildcard probes will be expected
7699 // by users to apply only to some subset of the
7700 // matching binaries, in the sense of "any", rather
7701 // than "all", sort of similarly how
7702 // module("*").function("...") patterns work.
7703
7704 derive_probes (sess, new_base, finished_results,
7705 true /* NB: not location->optional */ );
7706 }
7707 }
7708 else
7709 {
7710 string resolved_lib = find_executable(lib, sess.sysroot, sess.sysenv,
7711 "LD_LIBRARY_PATH");
7712 if (resolved_lib.find('/') != string::npos)
7713 {
7714 probe *new_base = build_library_probe(dw, resolved_lib,
7715 base, location);
7716 derive_probes(sess, new_base, finished_results);
7717 if (lib.find('/') == string::npos)
7718 sess.print_warning(_F("'%s' is not a needed library of '%s'. "
7719 "Specify the full path to squelch this warning.",
7720 resolved_lib.c_str(), dw.module_name.c_str()));
7721 }
7722 else
7723 {
7724 // Otherwise, let's suggest from the DT_NEEDED libraries
7725 string sugs = levenshtein_suggest(lib, visited_libraries, 5);
7726 if (!sugs.empty())
7727 throw SEMANTIC_ERROR (_NF("no match (similar library: %s)",
7728 "no match (similar libraries: %s)",
7729 sugs.find(',') == string::npos,
7730 sugs.c_str()));
7731 }
7732 }
7733 }
7734
7735 return results_pre != finished_results.size();
7736 }
7737
7738 void
7739 dwarf_builder::build(systemtap_session & sess,
7740 probe * base,
7741 probe_point * location,
7742 literal_map_t const & parameters,
7743 vector<derived_probe *> & finished_results)
7744 {
7745 // NB: the kernel/user dwlfpp objects are long-lived.
7746 // XXX: but they should be per-session, as this builder object
7747 // may be reused if we try to cross-instrument multiple targets.
7748
7749 dwflpp* dw = 0;
7750 literal_map_t filled_parameters = parameters;
7751
7752 interned_string module_name;
7753 int64_t proc_pid;
7754 if (has_null_param (parameters, TOK_KERNEL))
7755 {
7756 dw = get_kern_dw(sess, "kernel");
7757 }
7758 else if (get_param (parameters, TOK_MODULE, module_name))
7759 {
7760 // If not a full path was given, then it's an in-tree module. Replace any
7761 // dashes with underscores.
7762 if (!is_fully_resolved(module_name, sess.sysroot, sess.sysenv))
7763 {
7764 size_t dash_pos = 0;
7765 string module_name2 = module_name; // copy out for replace operations
7766 while((dash_pos=module_name2.find('-'))!=string::npos)
7767 module_name2.replace(int(dash_pos),1,"_");
7768 module_name = module_name2;
7769 filled_parameters[TOK_MODULE] = new literal_string(module_name);
7770 }
7771
7772 // NB: glob patterns get expanded later, during the offline
7773 // elfutils module listing.
7774 dw = get_kern_dw(sess, module_name);
7775 }
7776 else if (has_param(filled_parameters, TOK_PROCESS))
7777 {
7778 // NB: module_name is not yet set!
7779
7780 if(has_null_param(filled_parameters, TOK_PROCESS))
7781 {
7782 string file;
7783 try
7784 {
7785 file = sess.cmd_file();
7786 }
7787 catch (const semantic_error& e)
7788 {
7789 if(sess.target_pid)
7790 throw SEMANTIC_ERROR(_("invalid -x pid for unspecified process"
7791 " probe [man stapprobes]"), NULL, NULL, &e);
7792 else
7793 throw SEMANTIC_ERROR(_("invalid -c command for unspecified process"
7794 " probe [man stapprobes]"), NULL, NULL, &e);
7795 }
7796 if(file.empty())
7797 throw SEMANTIC_ERROR(_("unspecified process probe is invalid without"
7798 " a -c COMMAND or -x PID [man stapprobes]"));
7799 module_name = sess.sysroot + file;
7800 filled_parameters[TOK_PROCESS] = new literal_string(module_name);// this needs to be used in place of the blank map
7801 // in the case of TOK_MARK we need to modify locations as well // XXX why?
7802 if(location->components[0]->functor==TOK_PROCESS &&
7803 location->components[0]->arg == 0)
7804 {
7805 if (sess.target_pid)
7806 location->components[0]->arg = new literal_number(sess.target_pid);
7807 else
7808 location->components[0]->arg = new literal_string(module_name);
7809 }
7810 }
7811
7812 // NB: must specifically handle the classical ("string") form here, to make sure
7813 // we get the module_name out.
7814 else if (get_param (parameters, TOK_PROCESS, module_name))
7815 {
7816 module_name = (string)sess.sysroot + (string)module_name;
7817 filled_parameters[TOK_PROCESS] = new literal_string(module_name);
7818 }
7819
7820 else if (get_param (parameters, TOK_PROCESS, proc_pid))
7821 {
7822 // check that the pid given corresponds to a running process
7823 string pid_err_msg;
7824 if (!is_valid_pid(proc_pid, pid_err_msg))
7825 throw SEMANTIC_ERROR(pid_err_msg);
7826
7827 string pid_path = string("/proc/") + lex_cast(proc_pid) + "/exe";
7828 module_name = sess.sysroot + pid_path;
7829
7830 // in the case of TOK_MARK we need to modify locations as well // XXX why?
7831 if(location->components[0]->functor==TOK_PROCESS &&
7832 location->components[0]->arg == 0)
7833 location->components[0]->arg = new literal_number(sess.target_pid);
7834 }
7835
7836 // PR6456 process("/bin/*") glob handling
7837 if (contains_glob_chars (module_name))
7838 {
7839 // Expand glob via rewriting the probe-point process("....")
7840 // parameter, asserted to be the first one.
7841
7842 assert (location->components.size() > 0);
7843 assert (location->components[0]->functor == TOK_PROCESS);
7844 assert (location->components[0]->arg);
7845 literal_string* lit = dynamic_cast<literal_string*>(location->components[0]->arg);
7846 assert (lit);
7847
7848 // Evaluate glob here, and call derive_probes recursively with each match.
7849 set<string> globs = glob_executable (module_name);
7850 unsigned results_pre = finished_results.size();
7851 for (set<string>::const_iterator it = globs.begin();
7852 it != globs.end(); ++it)
7853 {
7854 assert_no_interrupts();
7855
7856 const string& globbed = *it;
7857
7858 // synthesize a new probe_point, with the glob-expanded string
7859 probe_point *pp = new probe_point (*location);
7860
7861 // PR13338: quote results to prevent recursion
7862 string eglobbed = escape_glob_chars (globbed);
7863
7864 if (sess.verbose > 1)
7865 clog << _F("Expanded process(\"%s\") to process(\"%s\")",
7866 module_name.to_string().c_str(), eglobbed.c_str()) << endl;
7867 string eglobbed_tgt = path_remove_sysroot(sess, eglobbed);
7868
7869 probe_point::component* ppc
7870 = new probe_point::component (TOK_PROCESS,
7871 new literal_string (eglobbed_tgt),
7872 true /* from_glob */ );
7873 ppc->tok = location->components[0]->tok; // overwrite [0] slot, pattern matched above
7874 pp->components[0] = ppc;
7875
7876 probe* new_probe = new probe (base, pp);
7877
7878 // We override "optional = true" here, as if the
7879 // wildcarded probe point was given a "?" suffix.
7880
7881 // This is because wildcard probes will be expected
7882 // by users to apply only to some subset of the
7883 // matching binaries, in the sense of "any", rather
7884 // than "all", sort of similarly how
7885 // module("*").function("...") patterns work.
7886
7887 derive_probes (sess, new_probe, finished_results,
7888 true /* NB: not location->optional */ );
7889 }
7890
7891 unsigned results_post = finished_results.size();
7892
7893 // Did we fail to find a function/plt/mark by name? Let's suggest
7894 // something!
7895 interned_string func;
7896 if (results_pre == results_post
7897 && get_param(filled_parameters, TOK_FUNCTION, func)
7898 && !func.empty())
7899 {
7900 string sugs = suggest_dwarf_functions(sess, modules_seen, func);
7901 modules_seen.clear();
7902 if (!sugs.empty())
7903 throw SEMANTIC_ERROR (_NF("no match (similar function: %s)",
7904 "no match (similar functions: %s)",
7905 sugs.find(',') == string::npos,
7906 sugs.c_str()));
7907 }
7908 else if (results_pre == results_post
7909 && get_param(filled_parameters, TOK_PLT, func)
7910 && !func.empty())
7911 {
7912 string sugs = suggest_plt_functions(sess, modules_seen, func);
7913 modules_seen.clear();
7914 if (!sugs.empty())
7915 throw SEMANTIC_ERROR (_NF("no match (similar function: %s)",
7916 "no match (similar functions: %s)",
7917 sugs.find(',') == string::npos,
7918 sugs.c_str()));
7919 }
7920 else if (results_pre == results_post
7921 && get_param(filled_parameters, TOK_MARK, func)
7922 && !func.empty())
7923 {
7924 interned_string provider;
7925 get_param(filled_parameters, TOK_PROVIDER, provider);
7926
7927 string sugs = suggest_marks(sess, modules_seen, func, provider);
7928 modules_seen.clear();
7929 if (!sugs.empty())
7930 throw SEMANTIC_ERROR (_NF("no match (similar mark: %s)",
7931 "no match (similar marks: %s)",
7932 sugs.find(',') == string::npos,
7933 sugs.c_str()));
7934 }
7935
7936 return; // avoid falling through
7937 }
7938
7939 // PR13338: unquote glob results
7940 module_name = unescape_glob_chars (module_name);
7941 user_path = find_executable (module_name, "", sess.sysenv); // canonicalize it
7942 if (!is_fully_resolved(user_path, "", sess.sysenv))
7943 throw SEMANTIC_ERROR(_F("cannot find executable '%s'",
7944 user_path.to_string().c_str()));
7945
7946 // if the executable starts with "#!", we look for the interpreter of the script
7947 {
7948 ifstream script_file (user_path.to_string().c_str());
7949
7950 if (script_file.good ())
7951 {
7952 string line;
7953
7954 getline (script_file, line);
7955
7956 if (line.compare (0, 2, "#!") == 0)
7957 {
7958 string path_head = line.substr(2);
7959
7960 // remove white spaces at the beginning of the string
7961 size_t p2 = path_head.find_first_not_of(" \t");
7962
7963 if (p2 != string::npos)
7964 {
7965 string path = path_head.substr(p2);
7966
7967 // remove white spaces at the end of the string
7968 p2 = path.find_last_not_of(" \t\n");
7969 if (string::npos != p2)
7970 path.erase(p2+1);
7971
7972 // handle "#!/usr/bin/env" redirect
7973 size_t offset = 0;
7974 if (path.compare(0, sizeof("/bin/env")-1, "/bin/env") == 0)
7975 {
7976 offset = sizeof("/bin/env")-1;
7977 }
7978 else if (path.compare(0, sizeof("/usr/bin/env")-1, "/usr/bin/env") == 0)
7979 {
7980 offset = sizeof("/usr/bin/env")-1;
7981 }
7982
7983 if (offset != 0)
7984 {
7985 size_t p3 = path.find_first_not_of(" \t", offset);
7986
7987 if (p3 != string::npos)
7988 {
7989 string env_path = path.substr(p3);
7990 user_path = find_executable (env_path, sess.sysroot,
7991 sess.sysenv);
7992 }
7993 }
7994 else
7995 {
7996 user_path = find_executable (path, sess.sysroot, sess.sysenv);
7997 }
7998
7999 struct stat st;
8000
8001 const string& new_path = user_path;
8002 if (access (new_path.c_str(), X_OK) == 0
8003 && stat (new_path.c_str(), &st) == 0
8004 && S_ISREG (st.st_mode)) // see find_executable()
8005 {
8006 if (sess.verbose > 1)
8007 clog << _F("Expanded process(\"%s\") to process(\"%s\")",
8008 module_name.to_string().c_str(), new_path.c_str()) << endl;
8009
8010 assert (location->components.size() > 0);
8011 assert (location->components[0]->functor == TOK_PROCESS);
8012 assert (location->components[0]->arg);
8013 literal_string* lit = dynamic_cast<literal_string*>(location->components[0]->arg);
8014 assert (lit);
8015
8016 // synthesize a new probe_point, with the expanded string
8017 probe_point *pp = new probe_point (*location);
8018 string user_path_tgt = path_remove_sysroot(sess, new_path);
8019 probe_point::component* ppc = new probe_point::component (TOK_PROCESS,
8020 new literal_string (user_path_tgt));
8021 ppc->tok = location->components[0]->tok; // overwrite [0] slot, pattern matched above
8022 pp->components[0] = ppc;
8023
8024 probe* new_probe = new probe (base, pp);
8025
8026 derive_probes (sess, new_probe, finished_results);
8027
8028 script_file.close();
8029 return;
8030 }
8031 }
8032 }
8033 }
8034 script_file.close();
8035 }
8036
8037 // If this is a library probe, then target the library module instead. We
8038 // do this only if the library path is already fully resolved (such as
8039 // what query_one_library() would have done for us). Otherwise, we resort
8040 // to iterate_over_libraries.
8041 if (get_param (parameters, TOK_LIBRARY, user_lib) && !user_lib.empty()
8042 && is_fully_resolved(user_lib, sess.sysroot, sess.sysenv, "LD_LIBRARY_PATH"))
8043 module_name = user_lib;
8044 else
8045 module_name = user_path; // canonicalize it
8046
8047 // uretprobes aren't available everywhere
8048 if (has_null_param(parameters, TOK_RETURN) && !sess.runtime_usermode_p())
8049 {
8050 if (kernel_supports_inode_uprobes(sess) &&
8051 !kernel_supports_inode_uretprobes(sess))
8052 throw SEMANTIC_ERROR
8053 (_("process return probes not available [man error::inode-uprobes]"));
8054 }
8055
8056 // There is a similar check in pass 4 (buildrun), but it is
8057 // needed here too to make sure alternatives for optional
8058 // (? or !) process probes are disposed and/or alternatives
8059 // are selected.
8060 if (!sess.runtime_usermode_p())
8061 check_process_probe_kernel_support(sess);
8062
8063 // user-space target; we use one dwflpp instance per module name
8064 // (= program or shared library)
8065 dw = get_user_dw(sess, module_name);
8066 }
8067
8068 assert(dw);
8069
8070 unsigned results_pre = finished_results.size();
8071
8072 if (sess.verbose > 3)
8073 clog << _F("dwarf_builder::build for %s",
8074 module_name.to_string().c_str()) << endl;
8075
8076 interned_string dummy_mark_name; // NB: PR10245: dummy value, need not substitute - => __
8077 if (get_param(parameters, TOK_MARK, dummy_mark_name))
8078 {
8079 sdt_query sdtq(base, location, *dw, filled_parameters, finished_results, user_lib);
8080 dw->iterate_over_modules<base_query>(&query_module, &sdtq);
8081
8082 // We need to update modules_seen with the modules we've visited
8083 modules_seen.insert(sdtq.visited_modules.begin(),
8084 sdtq.visited_modules.end());
8085
8086 if (results_pre == finished_results.size()
8087 && sdtq.has_library && !sdtq.resolved_library
8088 && resolve_library_by_path (sdtq, sdtq.visited_libraries,
8089 base, location, filled_parameters,
8090 finished_results))
8091 return;
8092
8093 // Did we fail to find a mark?
8094 if (results_pre == finished_results.size()
8095 && !location->from_globby_comp(TOK_MARK))
8096 {
8097 interned_string provider;
8098 (void) get_param(filled_parameters, TOK_PROVIDER, provider);
8099
8100 string sugs = suggest_marks(sess, modules_seen, dummy_mark_name, provider);
8101 modules_seen.clear();
8102 if (!sugs.empty())
8103 throw SEMANTIC_ERROR (_NF("no match (similar mark: %s)",
8104 "no match (similar marks: %s)",
8105 sugs.find(',') == string::npos,
8106 sugs.c_str()));
8107 }
8108
8109 return;
8110 }
8111
8112 dwarf_query q(base, location, *dw, filled_parameters, finished_results, user_path, user_lib);
8113
8114 // XXX: kernel.statement.absolute is a special case that requires no
8115 // dwfl processing. This code should be in a separate builder.
8116 if (q.has_kernel && q.has_absolute)
8117 {
8118 // assert guru mode for absolute probes
8119 if (! q.base_probe->privileged)
8120 {
8121 throw SEMANTIC_ERROR (_("absolute statement probe in unprivileged script; need stap -g"),
8122 q.base_probe->tok);
8123 }
8124
8125 // For kernel.statement(NUM).absolute probe points, we bypass
8126 // all the debuginfo stuff: We just wire up a
8127 // dwarf_derived_probe right here and now.
8128 dwarf_derived_probe* p =
8129 new dwarf_derived_probe ("", "", 0, "kernel", "",
8130 q.statement_num_val, q.statement_num_val,
8131 q, 0);
8132 finished_results.push_back (p);
8133 sess.unwindsym_modules.insert ("kernel");
8134 return;
8135 }
8136
8137 dw->iterate_over_modules<base_query>(&query_module, &q);
8138
8139 // We need to update modules_seen with the modules we've visited
8140 modules_seen.insert(q.visited_modules.begin(),
8141 q.visited_modules.end());
8142
8143 // PR11553 special processing: .return probes requested, but
8144 // some inlined function instances matched.
8145 unsigned i_n_r = q.inlined_non_returnable.size();
8146 unsigned results_post = finished_results.size();
8147 if (i_n_r > 0)
8148 {
8149 if ((results_pre == results_post) && (! sess.suppress_warnings)) // no matches; issue warning
8150 {
8151 string quicklist;
8152 for (set<interned_string>::iterator it = q.inlined_non_returnable.begin();
8153 it != q.inlined_non_returnable.end();
8154 it++)
8155 {
8156 quicklist += " " + (string)(*it);
8157 if (quicklist.size() > 80) // heuristic, don't make an overlong report line
8158 {
8159 quicklist += " ...";
8160 break;
8161 }
8162 }
8163
8164 sess.print_warning (_NF("cannot probe .return of %u inlined function %s",
8165 "cannot probe .return of %u inlined functions %s",
8166 quicklist.size(), i_n_r, quicklist.c_str()));
8167 // There will be also a "no matches" semantic error generated.
8168 }
8169 if (sess.verbose > 1)
8170 clog << _NF("skipped .return probe of %u inlined function",
8171 "skipped .return probe of %u inlined functions", i_n_r, i_n_r) << endl;
8172 if ((sess.verbose > 3) || (sess.verbose > 2 && results_pre == results_post)) // issue details with high verbosity
8173 {
8174 for (set<interned_string>::iterator it = q.inlined_non_returnable.begin();
8175 it != q.inlined_non_returnable.end();
8176 it++)
8177 clog << (*it) << " ";
8178 clog << endl;
8179 }
8180 } // i_n_r > 0
8181
8182 if (results_pre == finished_results.size()
8183 && q.has_library && !q.resolved_library
8184 && resolve_library_by_path (q, q.visited_libraries,
8185 base, location, filled_parameters,
8186 finished_results))
8187 return;
8188
8189 // If we just failed to resolve a function/plt by name, we can suggest
8190 // something. We only suggest things for probe points that were not
8191 // synthesized from a glob, i.e. only for 'real' probes. This is also
8192 // required because modules_seen needs to accumulate across recursive
8193 // calls for process(glob)[.library(glob)] probes.
8194 interned_string func;
8195 if (results_pre == results_post && !location->from_globby_comp(TOK_FUNCTION)
8196 && get_param(filled_parameters, TOK_FUNCTION, func)
8197 && !func.empty())
8198 {
8199 string sugs = suggest_dwarf_functions(sess, modules_seen, func);
8200 modules_seen.clear();
8201 if (!sugs.empty())
8202 throw SEMANTIC_ERROR (_NF("no match (similar function: %s)",
8203 "no match (similar functions: %s)",
8204 sugs.find(',') == string::npos,
8205 sugs.c_str()));
8206 }
8207 else if (results_pre == results_post && !location->from_globby_comp(TOK_PLT)
8208 && get_param(filled_parameters, TOK_PLT, func)
8209 && !func.empty())
8210 {
8211 string sugs = suggest_plt_functions(sess, modules_seen, func);
8212 modules_seen.clear();
8213 if (!sugs.empty())
8214 throw SEMANTIC_ERROR (_NF("no match (similar function: %s)",
8215 "no match (similar functions: %s)",
8216 sugs.find(',') == string::npos,
8217 sugs.c_str()));
8218 }
8219 else if (results_pre != results_post)
8220 // Something was derived so we won't need to suggest something
8221 modules_seen.clear();
8222 }
8223
8224 symbol_table::~symbol_table()
8225 {
8226 delete_map(map_by_addr);
8227 }
8228
8229 void
8230 symbol_table::add_symbol(interned_string name, bool weak, bool descriptor,
8231 Dwarf_Addr addr, Dwarf_Addr entrypc)
8232 {
8233 /* Does the target architecture have function descriptors?
8234 Then we want to filter them out. When seeing a symbol with a name
8235 starting with '.' we assume it is a regular function pointer and
8236 not a pointer to a function descriptor. Note that this might create
8237 duplicates if we also found the function descriptor symbol itself.
8238 dwfl_module_getsym_info () will have resolved the actual function
8239 address for us. But we won't know if we see either or both. */
8240 if (opd_section != SHN_UNDEF)
8241 {
8242 // Map ".sys_foo" to "sys_foo".
8243 if (name[0] == '.')
8244 name.remove_prefix(1);
8245
8246 // Make sure we don't create duplicate func_info's
8247 range_t er = map_by_addr.equal_range(addr);
8248 for (iterator_t it = er.first; it != er.second; ++it)
8249 if (it->second->name == name)
8250 return;
8251 }
8252
8253 func_info *fi = new func_info();
8254 fi->entrypc = entrypc;
8255 fi->addr = addr;
8256 fi->name = name;
8257 fi->weak = weak;
8258 fi->descriptor = descriptor;
8259
8260 map_by_name.insert(make_pair(fi->name, fi));
8261 map_by_addr.insert(make_pair(addr, fi));
8262 }
8263
8264 void
8265 symbol_table::prepare_section_rejection(Dwfl_Module *mod __attribute__ ((unused)))
8266 {
8267 Dwarf_Addr bias;
8268 Elf* elf = (dwarf_getelf (dwfl_module_getdwarf (mod, &bias))
8269 ?: dwfl_module_getelf (mod, &bias));
8270
8271 GElf_Ehdr ehdr_mem;
8272 GElf_Ehdr* em = gelf_getehdr (elf, &ehdr_mem);
8273 if (em == NULL) throw SEMANTIC_ERROR (_("Couldn't get elf header"));
8274
8275 /* Only old ELFv1 PPC64 ABI have function descriptors. */
8276 opd_section = SHN_UNDEF;
8277 if (em->e_machine != EM_PPC64 || (em->e_flags & EF_PPC64_ABI) == 2)
8278 return;
8279
8280 /*
8281 * The .opd section contains function descriptors that can look
8282 * just like function entry points. For example, there's a function
8283 * descriptor called "do_exit" that links to the entry point ".do_exit".
8284 * Reject all symbols in .opd.
8285 */
8286 Elf_Scn* scn = 0;
8287 size_t shstrndx;
8288
8289 if (!elf)
8290 return;
8291 if (elf_getshdrstrndx (elf, &shstrndx) != 0)
8292 return;
8293 while ((scn = elf_nextscn(elf, scn)) != NULL)
8294 {
8295 GElf_Shdr shdr_mem;
8296 GElf_Shdr *shdr = gelf_getshdr(scn, &shdr_mem);
8297 if (!shdr)
8298 continue;
8299 const char *name = elf_strptr(elf, shstrndx, shdr->sh_name);
8300 if (!strcmp(name, ".opd"))
8301 {
8302 opd_section = elf_ndxscn(scn);
8303 return;
8304 }
8305 }
8306 }
8307
8308 bool
8309 symbol_table::reject_section(GElf_Word section)
8310 {
8311 if (section == SHN_UNDEF || section == opd_section)
8312 return true;
8313 return false;
8314 }
8315
8316 enum info_status
8317 symbol_table::get_from_elf()
8318 {
8319 Dwfl_Module *mod = mod_info->mod;
8320 int syments = dwfl_module_getsymtab(mod);
8321 assert(syments);
8322 prepare_section_rejection(mod);
8323 Dwarf_Addr bias;
8324 Elf* elf = (dwarf_getelf (dwfl_module_getdwarf (mod, &bias))
8325 ?: dwfl_module_getelf (mod, &bias));
8326
8327 GElf_Ehdr ehdr_mem;
8328 GElf_Ehdr* em = gelf_getehdr (elf, &ehdr_mem);
8329 if (em == NULL) throw SEMANTIC_ERROR (_("Couldn't get elf header"));
8330
8331 for (int i = 1; i < syments; ++i)
8332 {
8333 GElf_Sym sym;
8334 GElf_Word section;
8335 GElf_Addr addr;
8336 bool reject;
8337
8338 /* Note that dwfl_module_getsym does adjust the sym.st_value but doesn't
8339 try to resolve it to a function address. dwfl_module_getsym_info leaves
8340 the st_value in tact (no adjustment applied) and returns the fully
8341 resolved address separately. In that case we can simply reject the
8342 symbol if it is SHN_UNDEF and don't need to call reject_section which
8343 does extra checks to see whether the address fall in an architecture
8344 specific descriptor table (which will never be the case when using the
8345 new dwfl_module_getsym_info). dwfl_module_getsym will only provide us
8346 with the (adjusted) st_value of the symbol, which might point into a
8347 function descriptor table. So in that case we still have to call
8348 reject_section. */
8349 #if _ELFUTILS_PREREQ (0, 158)
8350 const char* n = dwfl_module_getsym_info (mod, i, &sym, &addr, &section,
8351 NULL, NULL);
8352 reject = section == SHN_UNDEF;
8353 #else
8354 const char* n = dwfl_module_getsym (mod, i, &sym, &section);
8355 addr = sym.st_value;
8356 reject = reject_section(section);
8357 #endif
8358 if (! n)
8359 continue;
8360 interned_string name = n;
8361
8362 /*
8363 * For ELF ABI v2 on PPC64 LE, we need to adjust sym.st_value corresponding
8364 * to the bits of sym.st_other. These bits will tell us what's the offset
8365 * of the local entry point from the global entry point.
8366 *
8367 * st_other field is currently only used with ABIv2 on ppc64
8368 */
8369 Dwarf_Addr entrypc = addr;
8370 if ((em->e_machine == EM_PPC64) && ((em->e_flags & EF_PPC64_ABI) == 2)
8371 && (GELF_ST_TYPE(sym.st_info) == STT_FUNC) && sym.st_other)
8372 entrypc += PPC64_LOCAL_ENTRY_OFFSET(sym.st_other);
8373
8374 if (GELF_ST_TYPE(sym.st_info) == STT_FUNC)
8375 add_symbol(name, (GELF_ST_BIND(sym.st_info) == STB_WEAK),
8376 reject, addr, entrypc);
8377 if (GELF_ST_TYPE(sym.st_info) == STT_OBJECT
8378 && GELF_ST_BIND(sym.st_info) == STB_GLOBAL)
8379 globals[name] = addr;
8380 if (GELF_ST_TYPE(sym.st_info) == STT_OBJECT
8381 && GELF_ST_BIND(sym.st_info) == STB_LOCAL)
8382 locals[name] = addr;
8383 }
8384 return info_present;
8385 }
8386
8387 func_info *
8388 symbol_table::get_func_containing_address(Dwarf_Addr addr)
8389 {
8390 iterator_t iter = map_by_addr.upper_bound(addr);
8391 if (iter == map_by_addr.begin())
8392 return NULL;
8393 else
8394 return (--iter)->second;
8395 }
8396
8397 func_info *
8398 symbol_table::get_first_func()
8399 {
8400 iterator_t iter = map_by_addr.begin();
8401 return (iter)->second;
8402 }
8403
8404 /* Note this function filters out any symbols that are "rejected" because
8405 they are "descriptor" function symbols or SHN_UNDEF symbols. */
8406 set <func_info*>
8407 symbol_table::lookup_symbol(interned_string name)
8408 {
8409 set<func_info*> fis;
8410 pair <unordered_multimap<interned_string, func_info*>::iterator,
8411 unordered_multimap<interned_string, func_info*>::iterator> ret;
8412 ret = map_by_name.equal_range(name);
8413 for (unordered_multimap<interned_string, func_info*>::iterator it = ret.first; it != ret.second; ++it)
8414 if (! it->second->descriptor)
8415 fis.insert(it->second);
8416 return fis;
8417 }
8418
8419 /* Filters out the same "descriptor" or SHN_UNDEF symbols as
8420 symbol_table::lookup_symbol. */
8421 set <Dwarf_Addr>
8422 symbol_table::lookup_symbol_address(interned_string name)
8423 {
8424 set <Dwarf_Addr> addrs;
8425 set <func_info*> fis = lookup_symbol(name);
8426
8427 for (set<func_info*>::iterator it=fis.begin(); it!=fis.end(); ++it)
8428 addrs.insert((*it)->addr);
8429
8430 return addrs;
8431 }
8432
8433 // This is the kernel symbol table. The kernel macro cond_syscall creates
8434 // a weak symbol for each system call and maps it to sys_ni_syscall.
8435 // For system calls not implemented elsewhere, this weak symbol shows up
8436 // in the kernel symbol table. Following the precedent of dwarfful stap,
8437 // we refuse to consider such symbols. Here we delete them from our
8438 // symbol table.
8439 // TODO: Consider generalizing this and/or making it part of blacklist
8440 // processing.
8441 void
8442 symbol_table::purge_syscall_stubs()
8443 {
8444 set<Dwarf_Addr> addrs = lookup_symbol_address("sys_ni_syscall");
8445 if (addrs.empty())
8446 return;
8447
8448 /* Highly unlikely that multiple symbols named "sys_ni_syscall" may exist */
8449 if (addrs.size() > 1)
8450 cerr << _("Multiple 'sys_ni_syscall' symbols found.\n");
8451 Dwarf_Addr stub_addr = * addrs.begin();
8452
8453 range_t purge_range = map_by_addr.equal_range(stub_addr);
8454 for (iterator_t iter = purge_range.first;
8455 iter != purge_range.second;
8456 )
8457 {
8458 func_info *fi = iter->second;
8459 if (fi->weak && fi->name != "sys_ni_syscall")
8460 {
8461 map_by_name.erase(fi->name);
8462 map_by_addr.erase(iter++);
8463 delete fi;
8464 }
8465 else
8466 iter++;
8467 }
8468 }
8469
8470 void
8471 module_info::get_symtab()
8472 {
8473 if (symtab_status != info_unknown)
8474 return;
8475
8476 sym_table = new symbol_table(this);
8477 if (!elf_path.empty())
8478 {
8479 symtab_status = sym_table->get_from_elf();
8480 }
8481 else
8482 {
8483 assert(name == TOK_KERNEL);
8484 symtab_status = info_absent;
8485 cerr << _("Error: Cannot find vmlinux.") << endl;;
8486 }
8487 if (symtab_status == info_absent)
8488 {
8489 delete sym_table;
8490 sym_table = NULL;
8491 return;
8492 }
8493
8494 if (name == TOK_KERNEL)
8495 sym_table->purge_syscall_stubs();
8496 }
8497
8498 // update_symtab reconciles data between the elf symbol table and the dwarf
8499 // function enumeration. It updates the symbol table entries with the dwarf
8500 // die that describes the function, which also signals to query_module_symtab
8501 // that a statement probe isn't needed. In return, it also adds aliases to the
8502 // function table for names that share the same addr/die.
8503 void
8504 module_info::update_symtab(cu_function_cache_t *funcs)
8505 {
8506 if (!sym_table)
8507 return;
8508
8509 cu_function_cache_t new_funcs;
8510
8511 for (cu_function_cache_t::iterator func = funcs->begin();
8512 func != funcs->end(); func++)
8513 {
8514 // optimization: inlines will never be in the symbol table
8515 if (dwarf_func_inline(&func->second) != 0)
8516 {
8517 inlined_funcs.insert(func->first);
8518 continue;
8519 }
8520
8521 // We need to make additional efforts to match mangled elf names to dwarf
8522 // too. DW_AT_linkage_name (or w/ MIPS) can help, but that's sometimes
8523 // missing, so we may also need to try matching by address. See also the
8524 // notes about _Z in dwflpp::iterate_over_functions().
8525 interned_string name = dwarf_linkage_name(&func->second) ?: func->first;
8526
8527 set<func_info*> fis = sym_table->lookup_symbol(name);
8528 if (fis.empty())
8529 continue;
8530
8531 for (set<func_info*>::iterator fi = fis.begin(); fi!=fis.end(); ++fi)
8532 {
8533 // iterate over all functions at the same address
8534 symbol_table::range_t er = sym_table->map_by_addr.equal_range((*fi)->addr);
8535
8536 for (symbol_table::iterator_t it = er.first; it != er.second; ++it)
8537 {
8538 // update this function with the dwarf die
8539 it->second->die = func->second;
8540
8541 // if this function is a new alias, then
8542 // save it to merge into the function cache
8543 if (it->second != *fi)
8544 new_funcs.insert(make_pair(it->second->name, it->second->die));
8545 }
8546 }
8547 }
8548
8549 // add all discovered aliases back into the function cache
8550 // NB: this won't replace any names that dwarf may have already found
8551 funcs->insert(new_funcs.begin(), new_funcs.end());
8552 }
8553
8554 module_info::~module_info()
8555 {
8556 if (sym_table)
8557 delete sym_table;
8558 }
8559
8560 // ------------------------------------------------------------------------
8561 // user-space probes
8562 // ------------------------------------------------------------------------
8563
8564
8565 struct uprobe_derived_probe_group: public generic_dpg<uprobe_derived_probe>
8566 {
8567 private:
8568 string make_pbm_key (uprobe_derived_probe* p) {
8569 return (string)p->path + "|" + (string)p->module + "|" + (string)p->section + "|" + (string)lex_cast(p->pid);
8570 }
8571
8572 void emit_module_maxuprobes (systemtap_session& s);
8573
8574 // Using our own utrace-based uprobes
8575 void emit_module_utrace_decls (systemtap_session& s);
8576 void emit_module_utrace_init (systemtap_session& s);
8577 void emit_module_utrace_exit (systemtap_session& s);
8578
8579 // Using the upstream inode-based uprobes
8580 void emit_module_inode_decls (systemtap_session& s);
8581 void emit_module_inode_init (systemtap_session& s);
8582 void emit_module_inode_refresh (systemtap_session& s);
8583 void emit_module_inode_exit (systemtap_session& s);
8584
8585 // Using the dyninst backend (via stapdyn)
8586 void emit_module_dyninst_decls (systemtap_session& s);
8587 void emit_module_dyninst_init (systemtap_session& s);
8588 void emit_module_dyninst_exit (systemtap_session& s);
8589
8590 public:
8591 void emit_module_decls (systemtap_session& s);
8592 void emit_module_init (systemtap_session& s);
8593 void emit_module_refresh (systemtap_session& s);
8594 void emit_module_exit (systemtap_session& s);
8595
8596 // on-the-fly only supported for inode-uprobes
8597 bool otf_supported (systemtap_session& s)
8598 { return !s.runtime_usermode_p()
8599 && kernel_supports_inode_uprobes(s); }
8600
8601 // workqueue manipulation is safe in uprobes
8602 bool otf_safe_context (systemtap_session& s)
8603 { return otf_supported(s); }
8604 };
8605
8606
8607 void
8608 uprobe_derived_probe::join_group (systemtap_session& s)
8609 {
8610 if (! s.uprobe_derived_probes)
8611 s.uprobe_derived_probes = new uprobe_derived_probe_group ();
8612 s.uprobe_derived_probes->enroll (this);
8613 this->group = s.uprobe_derived_probes;
8614
8615 if (s.runtime_usermode_p())
8616 enable_dynprobes(s);
8617 else
8618 enable_task_finder(s);
8619
8620 // Ask buildrun.cxx to build extra module if needed, and
8621 // signal staprun to load that module. If we're using the builtin
8622 // inode-uprobes, we still need to know that it is required.
8623 s.need_uprobes = true;
8624 }
8625
8626
8627 void
8628 uprobe_derived_probe::getargs(std::list<std::string> &arg_set) const
8629 {
8630 dwarf_derived_probe::getargs(arg_set);
8631 arg_set.insert(arg_set.end(), args.begin(), args.end());
8632 }
8633
8634
8635 void
8636 uprobe_derived_probe::saveargs(int nargs)
8637 {
8638 for (int i = 1; i <= nargs; i++)
8639 args.push_back("$arg" + lex_cast (i) + ":long");
8640 }
8641
8642
8643 void
8644 uprobe_derived_probe::emit_privilege_assertion (translator_output* o)
8645 {
8646 // These probes are allowed for unprivileged users, but only in the
8647 // context of processes which they own.
8648 emit_process_owner_assertion (o);
8649 }
8650
8651
8652 struct uprobe_builder: public derived_probe_builder
8653 {
8654 uprobe_builder() {}
8655 virtual void build(systemtap_session & sess,
8656 probe * base,
8657 probe_point * location,
8658 literal_map_t const & parameters,
8659 vector<derived_probe *> & finished_results)
8660 {
8661 int64_t process, address;
8662
8663 if (kernel_supports_inode_uprobes(sess))
8664 throw SEMANTIC_ERROR (_("absolute process probes not available [man error::inode-uprobes]"));
8665
8666 bool b1 = get_param (parameters, TOK_PROCESS, process);
8667 (void) b1;
8668 bool b2 = get_param (parameters, TOK_STATEMENT, address);
8669 (void) b2;
8670 bool rr = has_null_param (parameters, TOK_RETURN);
8671 assert (b1 && b2); // by pattern_root construction
8672
8673 finished_results.push_back(new uprobe_derived_probe(base, location, process, address, rr));
8674 }
8675 };
8676
8677
8678 void
8679 uprobe_derived_probe_group::emit_module_maxuprobes (systemtap_session& s)
8680 {
8681 // We'll probably need at least this many:
8682 unsigned minuprobes = probes.size();
8683 // .. but we don't want so many that .bss is inflated (PR10507):
8684 unsigned uprobesize = 64;
8685 unsigned maxuprobesmem = 10*1024*1024; // 10 MB
8686 unsigned maxuprobes = maxuprobesmem / uprobesize;
8687
8688 // Let's choose a value on the geometric middle. This should end up
8689 // between minuprobes and maxuprobes. It's OK if this number turns
8690 // out to be < minuprobes or > maxuprobes. At worst, we get a
8691 // run-time error of one kind (too few: missed uprobe registrations)
8692 // or another (too many: vmalloc errors at module load time).
8693 unsigned default_maxuprobes = (unsigned)sqrt((double)minuprobes * (double)maxuprobes);
8694
8695 s.op->newline() << "#ifndef MAXUPROBES";
8696 s.op->newline() << "#define MAXUPROBES " << default_maxuprobes;
8697 s.op->newline() << "#endif";
8698 }
8699
8700
8701 void
8702 uprobe_derived_probe_group::emit_module_utrace_decls (systemtap_session& s)
8703 {
8704 if (probes.empty()) return;
8705 s.op->newline() << "/* ---- utrace uprobes ---- */";
8706 // If uprobes isn't in the kernel, pull it in from the runtime.
8707
8708 s.op->newline() << "#if defined(CONFIG_UPROBES) || defined(CONFIG_UPROBES_MODULE)";
8709 s.op->newline() << "#include <linux/uprobes.h>";
8710 s.op->newline() << "#else";
8711 s.op->newline() << "#include \"linux/uprobes/uprobes.h\"";
8712 s.op->newline() << "#endif";
8713 s.op->newline() << "#ifndef UPROBES_API_VERSION";
8714 s.op->newline() << "#define UPROBES_API_VERSION 1";
8715 s.op->newline() << "#endif";
8716
8717 emit_module_maxuprobes (s);
8718
8719 // Forward decls
8720 s.op->newline() << "#include \"linux/uprobes-common.h\"";
8721
8722 // In .bss, the shared pool of uprobe/uretprobe structs. These are
8723 // too big to embed in the initialized .data stap_uprobe_spec array.
8724 // XXX: consider a slab cache or somesuch for stap_uprobes
8725 s.op->newline() << "static struct stap_uprobe stap_uprobes [MAXUPROBES];";
8726 s.op->newline() << "static DEFINE_MUTEX(stap_uprobes_lock);"; // protects against concurrent registration/unregistration
8727
8728 s.op->assert_0_indent();
8729
8730 // Assign task-finder numbers as we build up the stap_uprobe_tf table.
8731 // This means we process probes[] in two passes.
8732 map <string,unsigned> module_index;
8733 unsigned module_index_ctr = 0;
8734
8735 // not const since embedded task_finder_target struct changes
8736 s.op->newline() << "static struct stap_uprobe_tf stap_uprobe_finders[] = {";
8737 s.op->indent(1);
8738 for (unsigned i=0; i<probes.size(); i++)
8739 {
8740 uprobe_derived_probe *p = probes[i];
8741 string pbmkey = make_pbm_key (p);
8742 if (module_index.find (pbmkey) == module_index.end())
8743 {
8744 module_index[pbmkey] = module_index_ctr++;
8745
8746 s.op->newline() << "{";
8747 // NB: it's essential that make_pbm_key() use all of and
8748 // only the same fields as we're about to emit.
8749 s.op->line() << " .finder={";
8750 s.op->line() << " .purpose=\"uprobes\",";
8751 if (p->pid != 0)
8752 s.op->line() << " .pid=" << p->pid << ",";
8753
8754 if (p->section == "") // .statement(addr).absolute
8755 s.op->line() << " .callback=&stap_uprobe_process_found,";
8756 else if (p->section == ".absolute") // proxy for ET_EXEC -> exec()'d program
8757 {
8758 s.op->line() << " .procname=" << lex_cast_qstring(p->module) << ",";
8759 s.op->line() << " .callback=&stap_uprobe_process_found,";
8760 }
8761 else if (p->section != ".absolute") // ET_DYN
8762 {
8763 if (p->has_library)
8764 s.op->line() << " .procname=\"" << p->path << "\", ";
8765 s.op->line() << " .mmap_callback=&stap_uprobe_mmap_found, ";
8766 s.op->line() << " .munmap_callback=&stap_uprobe_munmap_found, ";
8767 s.op->line() << " .callback=&stap_uprobe_process_munmap,";
8768 }
8769 s.op->line() << " },";
8770 if (p->module != "")
8771 s.op->line() << " .pathname=" << lex_cast_qstring(p->module) << ", ";
8772 s.op->line() << " },";
8773 }
8774 else
8775 { } // skip it in this pass, already have a suitable stap_uprobe_tf slot for it.
8776 }
8777 s.op->newline(-1) << "};";
8778
8779 s.op->assert_0_indent();
8780
8781 unsigned pci;
8782 for (pci=0; pci<probes.size(); pci++)
8783 {
8784 // List of perf counters used by each probe
8785 // This list is an index into struct stap_perf_probe,
8786 uprobe_derived_probe *p = probes[pci];
8787 std::set<string>::iterator pcii;
8788 s.op->newline() << "long perf_counters_" + lex_cast(pci) + "[] = {";
8789 for (pcii = p->perf_counter_refs.begin();
8790 pcii != p->perf_counter_refs.end(); pcii++)
8791 {
8792 std::vector<std::pair<std::string,std::string> >::iterator it;
8793 unsigned i = 0;
8794 // Find the associated perf.counter probe
8795 for (it=s.perf_counters.begin() ;
8796 it != s.perf_counters.end(); it++, i++)
8797 if ((*it).first == (*pcii))
8798 break;
8799 s.op->line() << lex_cast(i) << ", ";
8800 }
8801 s.op->newline() << "};";
8802 }
8803
8804 // NB: read-only structure
8805 s.op->newline() << "static const struct stap_uprobe_spec stap_uprobe_specs [] = {";
8806 s.op->indent(1);
8807 for (unsigned i =0; i<probes.size(); i++)
8808 {
8809 uprobe_derived_probe* p = probes[i];
8810 s.op->newline() << "{";
8811 string key = make_pbm_key (p);
8812 unsigned value = module_index[key];
8813 if (value != 0)
8814 s.op->line() << " .tfi=" << value << ",";
8815 s.op->line() << " .address=(unsigned long)0x" << hex << p->addr << dec << "ULL,";
8816 s.op->line() << " .probe=" << common_probe_init (p) << ",";
8817
8818 if (p->sdt_semaphore_addr != 0)
8819 s.op->line() << " .sdt_sem_offset=(unsigned long)0x"
8820 << hex << p->sdt_semaphore_addr << dec << "ULL,";
8821
8822 // XXX: don't bother emit if array is empty
8823 s.op->line() << " .perf_counters_dim=ARRAY_SIZE(perf_counters_" << lex_cast(i) << "),";
8824 // List of perf counters used by a probe from above
8825 s.op->line() << " .perf_counters=perf_counters_" + lex_cast(i) + ",";
8826
8827 if (p->has_return)
8828 s.op->line() << " .return_p=1,";
8829 s.op->line() << " },";
8830 }
8831 s.op->newline(-1) << "};";
8832
8833 s.op->assert_0_indent();
8834
8835 s.op->newline() << "static void enter_uprobe_probe (struct uprobe *inst, struct pt_regs *regs) {";
8836 s.op->newline(1) << "struct stap_uprobe *sup = container_of(inst, struct stap_uprobe, up);";
8837 s.op->newline() << "const struct stap_uprobe_spec *sups = &stap_uprobe_specs [sup->spec_index];";
8838 common_probe_entryfn_prologue (s, "STAP_SESSION_RUNNING", "sups->probe",
8839 "stp_probe_type_uprobe");
8840 s.op->newline() << "if (sup->spec_index < 0 || "
8841 << "sup->spec_index >= " << probes.size() << ") {";
8842 s.op->newline(1) << "_stp_error (\"bad spec_index %d (max " << probes.size()
8843 << "): %s\", sup->spec_index, c->probe_point);";
8844 s.op->newline() << "goto probe_epilogue;";
8845 s.op->newline(-1) << "}";
8846 s.op->newline() << "c->uregs = regs;";
8847 s.op->newline() << "c->user_mode_p = 1;";
8848
8849 // Make it look like the IP is set as it would in the actual user
8850 // task when calling real probe handler. Reset IP regs on return, so
8851 // we don't confuse uprobes. PR10458
8852 s.op->newline() << "{";
8853 s.op->indent(1);
8854 s.op->newline() << "unsigned long uprobes_ip = REG_IP(c->uregs);";
8855 s.op->newline() << "SET_REG_IP(regs, inst->vaddr);";
8856 s.op->newline() << "(*sups->probe->ph) (c);";
8857 s.op->newline() << "SET_REG_IP(regs, uprobes_ip);";
8858 s.op->newline(-1) << "}";
8859
8860 common_probe_entryfn_epilogue (s, true, otf_safe_context(s));
8861 s.op->newline(-1) << "}";
8862
8863 s.op->newline() << "static void enter_uretprobe_probe (struct uretprobe_instance *inst, struct pt_regs *regs) {";
8864 s.op->newline(1) << "struct stap_uprobe *sup = container_of(inst->rp, struct stap_uprobe, urp);";
8865 s.op->newline() << "const struct stap_uprobe_spec *sups = &stap_uprobe_specs [sup->spec_index];";
8866 common_probe_entryfn_prologue (s, "STAP_SESSION_RUNNING", "sups->probe",
8867 "stp_probe_type_uretprobe");
8868 s.op->newline() << "c->ips.ri = inst;";
8869 s.op->newline() << "if (sup->spec_index < 0 || "
8870 << "sup->spec_index >= " << probes.size() << ") {";
8871 s.op->newline(1) << "_stp_error (\"bad spec_index %d (max " << probes.size()
8872 << "): %s\", sup->spec_index, c->probe_point);";
8873 s.op->newline() << "goto probe_epilogue;";
8874 s.op->newline(-1) << "}";
8875
8876 s.op->newline() << "c->uregs = regs;";
8877 s.op->newline() << "c->user_mode_p = 1;";
8878
8879 // Make it look like the IP is set as it would in the actual user
8880 // task when calling real probe handler. Reset IP regs on return, so
8881 // we don't confuse uprobes. PR10458
8882 s.op->newline() << "{";
8883 s.op->indent(1);
8884 s.op->newline() << "unsigned long uprobes_ip = REG_IP(c->uregs);";
8885 s.op->newline() << "SET_REG_IP(regs, inst->ret_addr);";
8886 s.op->newline() << "(*sups->probe->ph) (c);";
8887 s.op->newline() << "SET_REG_IP(regs, uprobes_ip);";
8888 s.op->newline(-1) << "}";
8889
8890 common_probe_entryfn_epilogue (s, true, otf_safe_context(s));
8891 s.op->newline(-1) << "}";
8892
8893 s.op->newline();
8894 s.op->newline() << "#include \"linux/uprobes-common.c\"";
8895 s.op->newline();
8896 }
8897
8898
8899 void
8900 uprobe_derived_probe_group::emit_module_utrace_init (systemtap_session& s)
8901 {
8902 if (probes.empty()) return;
8903
8904 s.op->newline() << "/* ---- utrace uprobes ---- */";
8905
8906 s.op->newline() << "for (j=0; j<MAXUPROBES; j++) {";
8907 s.op->newline(1) << "struct stap_uprobe *sup = & stap_uprobes[j];";
8908 s.op->newline() << "sup->spec_index = -1;"; // free slot
8909 // NB: we assume the rest of the struct (specificaly, sup->up) is
8910 // initialized to zero. This is so that we can use
8911 // sup->up->kdata = NULL for "really free!" PR 6829.
8912 s.op->newline(-1) << "}";
8913 s.op->newline() << "mutex_init (& stap_uprobes_lock);";
8914
8915 // Set up the task_finders
8916 s.op->newline() << "for (i=0; i<sizeof(stap_uprobe_finders)/sizeof(stap_uprobe_finders[0]); i++) {";
8917 s.op->newline(1) << "struct stap_uprobe_tf *stf = & stap_uprobe_finders[i];";
8918 s.op->newline() << "probe_point = stf->pathname;"; // for error messages; XXX: would prefer pp() or something better
8919 s.op->newline() << "rc = stap_register_task_finder_target (& stf->finder);";
8920
8921 // NB: if (rc), there is no need (XXX: nor any way) to clean up any
8922 // finders already registered, since mere registration does not
8923 // cause any utrace or memory allocation actions. That happens only
8924 // later, once the task finder engine starts running. So, for a
8925 // partial initialization requiring unwind, we need do nothing.
8926 s.op->newline() << "if (rc) break;";
8927
8928 s.op->newline(-1) << "}";
8929 }
8930
8931
8932 void
8933 uprobe_derived_probe_group::emit_module_utrace_exit (systemtap_session& s)
8934 {
8935 if (probes.empty()) return;
8936 s.op->newline() << "/* ---- utrace uprobes ---- */";
8937
8938 // NB: there is no stap_unregister_task_finder_target call;
8939 // important stuff like utrace cleanups are done by
8940 // __stp_task_finder_cleanup() via stap_stop_task_finder().
8941 //
8942 // This function blocks until all callbacks are completed, so there
8943 // is supposed to be no possibility of any registration-related code starting
8944 // to run in parallel with our shutdown here. So we don't need to protect the
8945 // stap_uprobes[] array with the mutex.
8946
8947 s.op->newline() << "for (j=0; j<MAXUPROBES; j++) {";
8948 s.op->newline(1) << "struct stap_uprobe *sup = & stap_uprobes[j];";
8949 s.op->newline() << "const struct stap_uprobe_spec *sups = &stap_uprobe_specs [sup->spec_index];";
8950 s.op->newline() << "if (sup->spec_index < 0) continue;"; // free slot
8951
8952 // PR10655: decrement that ENABLED semaphore
8953 s.op->newline() << "if (sup->sdt_sem_address) {";
8954 s.op->newline(1) << "unsigned short sdt_semaphore;"; // NB: fixed size
8955 s.op->newline() << "pid_t pid = (sups->return_p ? sup->urp.u.pid : sup->up.pid);";
8956 s.op->newline() << "struct task_struct *tsk;";
8957 s.op->newline() << "rcu_read_lock();";
8958
8959 // Do a pid->task_struct* lookup. For 2.6.24+, this code assumes
8960 // that the pid is always in the global namespace, not in any
8961 // private namespace.
8962 s.op->newline() << "#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,24)";
8963 // We'd like to call find_task_by_pid_ns() here, but it isn't
8964 // exported. So, we call what it calls...
8965 s.op->newline() << " tsk = pid_task(find_pid_ns(pid, &init_pid_ns), PIDTYPE_PID);";
8966 s.op->newline() << "#else";
8967 s.op->newline() << " tsk = find_task_by_pid (pid);";
8968 s.op->newline() << "#endif /* 2.6.24 */";
8969
8970 s.op->newline() << "if (tsk) {"; // just in case the thing exited while we weren't watching
8971 s.op->newline(1) << "if (__access_process_vm_noflush(tsk, sup->sdt_sem_address, &sdt_semaphore, sizeof(sdt_semaphore), 0)) {";
8972 s.op->newline(1) << "sdt_semaphore --;";
8973 s.op->newline() << "#ifdef DEBUG_UPROBES";
8974 s.op->newline() << "_stp_dbug (__FUNCTION__,__LINE__, \"-semaphore %#x @ %#lx\\n\", sdt_semaphore, sup->sdt_sem_address);";
8975 s.op->newline() << "#endif";
8976 s.op->newline() << "__access_process_vm_noflush(tsk, sup->sdt_sem_address, &sdt_semaphore, sizeof(sdt_semaphore), 1);";
8977 s.op->newline(-1) << "}";
8978 // XXX: need to analyze possibility of race condition
8979 s.op->newline(-1) << "}";
8980 s.op->newline() << "rcu_read_unlock();";
8981 s.op->newline(-1) << "}";
8982
8983 s.op->newline() << "if (sups->return_p) {";
8984 s.op->newline(1) << "#ifdef DEBUG_UPROBES";
8985 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);";
8986 s.op->newline() << "#endif";
8987 // NB: PR6829 does not change that we still need to unregister at
8988 // *this* time -- when the script as a whole exits.
8989 s.op->newline() << "unregister_uretprobe (& sup->urp);";
8990 s.op->newline(-1) << "} else {";
8991 s.op->newline(1) << "#ifdef DEBUG_UPROBES";
8992 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);";
8993 s.op->newline() << "#endif";
8994 s.op->newline() << "unregister_uprobe (& sup->up);";
8995 s.op->newline(-1) << "}";
8996
8997 s.op->newline() << "sup->spec_index = -1;";
8998
8999 // XXX: uprobe missed counts?
9000
9001 s.op->newline(-1) << "}";
9002
9003 s.op->newline() << "mutex_destroy (& stap_uprobes_lock);";
9004 }
9005
9006
9007 void
9008 uprobe_derived_probe_group::emit_module_inode_decls (systemtap_session& s)
9009 {
9010 if (probes.empty()) return;
9011 s.op->newline() << "/* ---- inode uprobes ---- */";
9012 emit_module_maxuprobes (s);
9013 s.op->newline() << "#include \"linux/uprobes-inode.c\"";
9014
9015 // Write the probe handler.
9016 s.op->newline() << "static int stapiu_probe_handler "
9017 << "(struct stapiu_consumer *sup, struct pt_regs *regs) {";
9018 s.op->newline(1);
9019
9020 // Since we're sharing the entry function, we have to dynamically choose the probe_type
9021 string probe_type = "(sup->return_p ? stp_probe_type_uretprobe : stp_probe_type_uprobe)";
9022 common_probe_entryfn_prologue (s, "STAP_SESSION_RUNNING", "sup->probe",
9023 probe_type);
9024
9025 s.op->newline() << "c->uregs = regs;";
9026 s.op->newline() << "c->user_mode_p = 1;";
9027 // NB: IP is already set by stapiu_probe_prehandler in uprobes-inode.c
9028 s.op->newline() << "(*sup->probe->ph) (c);";
9029
9030 common_probe_entryfn_epilogue (s, true, otf_safe_context(s));
9031 s.op->newline() << "return 0;";
9032 s.op->newline(-1) << "}";
9033 s.op->assert_0_indent();
9034
9035 // Index of all the modules for which we need inodes.
9036 map<string, unsigned> module_index;
9037 unsigned module_index_ctr = 0;
9038
9039 // Discover and declare targets for each unique path.
9040 s.op->newline() << "static struct stapiu_target "
9041 << "stap_inode_uprobe_targets[] = {";
9042 s.op->indent(1);
9043 for (unsigned i=0; i<probes.size(); i++)
9044 {
9045 uprobe_derived_probe *p = probes[i];
9046 const string key = make_pbm_key(p);
9047 if (module_index.find (key) == module_index.end())
9048 {
9049 module_index[key] = module_index_ctr++;
9050 s.op->newline() << "{";
9051 s.op->line() << " .finder={";
9052 s.op->line() << " .purpose=\"inode-uprobes\",";
9053 if (p->pid != 0)
9054 s.op->line() << " .pid=" << p->pid << ",";
9055
9056 if (p->section == "") // .statement(addr).absolute XXX?
9057 s.op->line() << " .callback=&stapiu_process_found,";
9058 else if (p->section == ".absolute") // proxy for ET_EXEC -> exec()'d program
9059 {
9060 s.op->line() << " .procname=" << lex_cast_qstring(p->module) << ",";
9061 s.op->line() << " .callback=&stapiu_process_found,";
9062 }
9063 else if (p->section != ".absolute") // ET_DYN
9064 {
9065 if (p->has_library)
9066 s.op->line() << " .procname=\"" << p->path << "\", ";
9067 s.op->line() << " .mmap_callback=&stapiu_mmap_found, ";
9068 s.op->line() << " .munmap_callback=&stapiu_munmap_found, ";
9069 s.op->line() << " .callback=&stapiu_process_munmap,";
9070 }
9071 s.op->line() << " },";
9072 s.op->line() << " .filename=" << lex_cast_qstring(p->module) << ",";
9073 s.op->line() << " },";
9074 }
9075 }
9076 s.op->newline(-1) << "};";
9077 s.op->assert_0_indent();
9078
9079 // Declare the actual probes.
9080 unsigned pci;
9081 for (pci=0; pci<probes.size(); pci++)
9082 {
9083 // List of perf counters used by each probe
9084 // This list is an index into struct stap_perf_probe,
9085 uprobe_derived_probe *p = probes[pci];
9086 std::set<string>::iterator pcii;
9087 s.op->newline() << "long perf_counters_" + lex_cast(pci) + "[] = {";
9088 for (pcii = p->perf_counter_refs.begin();
9089 pcii != p->perf_counter_refs.end(); pcii++)
9090 {
9091 vector<std::pair<string,string> >:: iterator it;
9092 unsigned i = 0;
9093 // Find the associated perf.counter probe
9094 for (it=s.perf_counters.begin() ;
9095 it != s.perf_counters.end(); it++, i++)
9096 if ((*it).first == (*pcii))
9097 break;
9098 s.op->line() << lex_cast(i) << ", ";
9099 }
9100 s.op->newline() << "};";
9101 }
9102
9103 s.op->newline() << "static struct stapiu_consumer "
9104 << "stap_inode_uprobe_consumers[] = {";
9105 s.op->indent(1);
9106 for (unsigned i=0; i<probes.size(); i++)
9107 {
9108 uprobe_derived_probe *p = probes[i];
9109 unsigned index = module_index[make_pbm_key(p)];
9110 s.op->newline() << "{";
9111 if (p->has_return)
9112 s.op->line() << " .return_p=1,";
9113 s.op->line() << " .target=&stap_inode_uprobe_targets[" << index << "],";
9114 s.op->line() << " .offset=(loff_t)0x" << hex << p->addr << dec << "ULL,";
9115 if (p->sdt_semaphore_addr)
9116 s.op->line() << " .sdt_sem_offset=(loff_t)0x"
9117 << hex << p->sdt_semaphore_addr << dec << "ULL,";
9118 // XXX: don't bother emit if array is empty
9119 s.op->line() << " .perf_counters_dim=ARRAY_SIZE(perf_counters_" << lex_cast(i) << "),";
9120 // List of perf counters used by a probe from above
9121 s.op->line() << " .perf_counters=perf_counters_" + lex_cast(i) + ",";
9122 s.op->line() << " .probe=" << common_probe_init (p) << ",";
9123 s.op->line() << " },";
9124 }
9125 s.op->newline(-1) << "};";
9126 s.op->assert_0_indent();
9127 }
9128
9129
9130 void
9131 uprobe_derived_probe_group::emit_module_inode_init (systemtap_session& s)
9132 {
9133 if (probes.empty()) return;
9134 s.op->newline() << "/* ---- inode uprobes ---- */";
9135 // Let stapiu_init() handle reporting errors by setting probe_point
9136 // to NULL.
9137 s.op->newline() << "probe_point = NULL;";
9138 s.op->newline() << "rc = stapiu_init ("
9139 << "stap_inode_uprobe_targets, "
9140 << "ARRAY_SIZE(stap_inode_uprobe_targets), "
9141 << "stap_inode_uprobe_consumers, "
9142 << "ARRAY_SIZE(stap_inode_uprobe_consumers));";
9143 }
9144
9145
9146 void
9147 uprobe_derived_probe_group::emit_module_inode_refresh (systemtap_session& s)
9148 {
9149 if (probes.empty()) return;
9150 s.op->newline() << "/* ---- inode uprobes ---- */";
9151 s.op->newline() << "stapiu_refresh ("
9152 << "stap_inode_uprobe_targets, "
9153 << "ARRAY_SIZE(stap_inode_uprobe_targets));";
9154 }
9155
9156
9157 void
9158 uprobe_derived_probe_group::emit_module_inode_exit (systemtap_session& s)
9159 {
9160 if (probes.empty()) return;
9161 s.op->newline() << "/* ---- inode uprobes ---- */";
9162 s.op->newline() << "stapiu_exit ("
9163 << "stap_inode_uprobe_targets, "
9164 << "ARRAY_SIZE(stap_inode_uprobe_targets), "
9165 << "stap_inode_uprobe_consumers, "
9166 << "ARRAY_SIZE(stap_inode_uprobe_consumers));";
9167 }
9168
9169
9170 void
9171 uprobe_derived_probe_group::emit_module_dyninst_decls (systemtap_session& s)
9172 {
9173 if (probes.empty()) return;
9174 s.op->newline() << "/* ---- dyninst uprobes ---- */";
9175 emit_module_maxuprobes (s);
9176 s.op->newline() << "#include \"dyninst/uprobes.h\"";
9177
9178 // Let the dynprobe_derived_probe_group handle outputting targets
9179 // and probes. This allows us to merge different types of probes.
9180 s.op->newline() << "static struct stapdu_probe stapdu_probes[];";
9181 for (unsigned i = 0; i < probes.size(); i++)
9182 {
9183 uprobe_derived_probe *p = probes[i];
9184
9185 dynprobe_add_uprobe(s, p->module, p->addr, p->sdt_semaphore_addr,
9186 (p->has_return ? "STAPDYN_PROBE_FLAG_RETURN" : "0"),
9187 common_probe_init(p));
9188 }
9189 // loc2c-generated code assumes pt_regs are available, so use this to make
9190 // sure we always have *something* for it to dereference...
9191 s.op->newline() << "static struct pt_regs stapdu_dummy_uregs;";
9192
9193 // Write the probe handler.
9194 // NB: not static, so dyninst can find it
9195 s.op->newline() << "int enter_dyninst_uprobe "
9196 << "(uint64_t index, struct pt_regs *regs) {";
9197 s.op->newline(1) << "struct stapdu_probe *sup = &stapdu_probes[index];";
9198
9199 // Since we're sharing the entry function, we have to dynamically choose the probe_type
9200 string probe_type = "((sup->flags & STAPDYN_PROBE_FLAG_RETURN) ?"
9201 " stp_probe_type_uretprobe : stp_probe_type_uprobe)";
9202 common_probe_entryfn_prologue (s, "STAP_SESSION_RUNNING", "sup->probe",
9203 probe_type);
9204
9205 s.op->newline() << "c->uregs = regs ?: &stapdu_dummy_uregs;";
9206 s.op->newline() << "c->user_mode_p = 1;";
9207 // XXX: once we have regs, check how dyninst sets the IP
9208 // XXX: the way that dyninst rewrites stuff is probably going to be
9209 // ... very confusing to our backtracer (at least if we stay in process)
9210 s.op->newline() << "(*sup->probe->ph) (c);";
9211 common_probe_entryfn_epilogue (s, true, otf_safe_context(s));
9212 s.op->newline() << "return 0;";
9213 s.op->newline(-1) << "}";
9214 s.op->newline() << "#include \"dyninst/uprobes-regs.c\"";
9215 s.op->assert_0_indent();
9216 }
9217
9218
9219 void
9220 uprobe_derived_probe_group::emit_module_dyninst_init (systemtap_session& s)
9221 {
9222 if (probes.empty()) return;
9223
9224 /* stapdyn handles the dirty work via dyninst */
9225 s.op->newline() << "/* ---- dyninst uprobes ---- */";
9226 s.op->newline() << "/* this section left intentionally blank */";
9227 }
9228
9229
9230 void
9231 uprobe_derived_probe_group::emit_module_dyninst_exit (systemtap_session& s)
9232 {
9233 if (probes.empty()) return;
9234
9235 /* stapdyn handles the dirty work via dyninst */
9236 s.op->newline() << "/* ---- dyninst uprobes ---- */";
9237 s.op->newline() << "/* this section left intentionally blank */";
9238 }
9239
9240
9241 void
9242 uprobe_derived_probe_group::emit_module_decls (systemtap_session& s)
9243 {
9244 if (s.runtime_usermode_p())
9245 emit_module_dyninst_decls (s);
9246 else if (kernel_supports_inode_uprobes (s))
9247 emit_module_inode_decls (s);
9248 else
9249 emit_module_utrace_decls (s);
9250 }
9251
9252
9253 void
9254 uprobe_derived_probe_group::emit_module_init (systemtap_session& s)
9255 {
9256 if (s.runtime_usermode_p())
9257 emit_module_dyninst_init (s);
9258 else if (kernel_supports_inode_uprobes (s))
9259 emit_module_inode_init (s);
9260 else
9261 emit_module_utrace_init (s);
9262 }
9263
9264
9265 void
9266 uprobe_derived_probe_group::emit_module_refresh (systemtap_session& s)
9267 {
9268 if (!s.runtime_usermode_p() && kernel_supports_inode_uprobes (s))
9269 emit_module_inode_refresh (s);
9270 }
9271
9272
9273 void
9274 uprobe_derived_probe_group::emit_module_exit (systemtap_session& s)
9275 {
9276 if (s.runtime_usermode_p())
9277 emit_module_dyninst_exit (s);
9278 else if (kernel_supports_inode_uprobes (s))
9279 emit_module_inode_exit (s);
9280 else
9281 emit_module_utrace_exit (s);
9282 }
9283
9284
9285 // ------------------------------------------------------------------------
9286 // Kprobe derived probes
9287 // ------------------------------------------------------------------------
9288
9289 static const string TOK_KPROBE("kprobe");
9290
9291 struct kprobe_derived_probe: public derived_probe
9292 {
9293 kprobe_derived_probe (systemtap_session& sess,
9294 vector<derived_probe *> & results,
9295 probe *base,
9296 probe_point *location,
9297 interned_string name,
9298 int64_t stmt_addr,
9299 bool has_call,
9300 bool has_return,
9301 bool has_statement,
9302 bool has_maxactive,
9303 bool has_path,
9304 bool has_library,
9305 int64_t maxactive_val,
9306 const string& path,
9307 const string& library
9308 );
9309 string symbol_name;
9310 Dwarf_Addr addr;
9311 bool has_call;
9312 bool has_return;
9313 bool has_statement;
9314 bool has_maxactive;
9315 bool has_path;
9316 bool has_library;
9317 int64_t maxactive_val;
9318 string path;
9319 string library;
9320 bool access_var;
9321 void printsig (std::ostream &o) const;
9322 void join_group (systemtap_session& s);
9323 };
9324
9325 struct kprobe_derived_probe_group: public derived_probe_group
9326 {
9327 private:
9328 multimap<string,kprobe_derived_probe*> probes_by_module;
9329 typedef multimap<string,kprobe_derived_probe*>::iterator p_b_m_iterator;
9330
9331 public:
9332 void enroll (kprobe_derived_probe* probe);
9333 void emit_module_decls (systemtap_session& s);
9334 void emit_module_init (systemtap_session& s);
9335 void emit_module_exit (systemtap_session& s);
9336 };
9337
9338 struct kprobe_var_expanding_visitor: public var_expanding_visitor
9339 {
9340 systemtap_session& sess;
9341 block *add_block;
9342 block *add_call_probe; // synthesized from .return probes with saved $vars
9343 bool add_block_tid, add_call_probe_tid;
9344 bool has_return;
9345
9346 kprobe_var_expanding_visitor(systemtap_session& sess, bool has_return):
9347 sess(sess), add_block(NULL), add_call_probe(NULL),
9348 add_block_tid(false), add_call_probe_tid(false),
9349 has_return(has_return) {}
9350
9351 void visit_entry_op (entry_op* e);
9352 };
9353
9354
9355 kprobe_derived_probe::kprobe_derived_probe (systemtap_session& sess,
9356 vector<derived_probe *> & results,
9357 probe *base,
9358 probe_point *location,
9359 interned_string name,
9360 int64_t stmt_addr,
9361 bool has_call,
9362 bool has_return,
9363 bool has_statement,
9364 bool has_maxactive,
9365 bool has_path,
9366 bool has_library,
9367 int64_t maxactive_val,
9368 const string& path,
9369 const string& library
9370 ):
9371 derived_probe (base, location, true /* .components soon rewritten */ ),
9372 symbol_name (name), addr (stmt_addr), has_call (has_call),
9373 has_return (has_return), has_statement (has_statement),
9374 has_maxactive (has_maxactive), has_path (has_path),
9375 has_library (has_library),
9376 maxactive_val (maxactive_val),
9377 path (path), library (library)
9378 {
9379 this->tok = base->tok;
9380 this->access_var = false;
9381
9382 #ifndef USHRT_MAX
9383 #define USHRT_MAX 32767
9384 #endif
9385
9386 // Expansion of $target variables in the probe body produces an error during
9387 // translate phase, since we're not using debuginfo
9388
9389 vector<probe_point::component*> comps;
9390 comps.push_back (new probe_point::component(TOK_KPROBE));
9391
9392 if (has_statement)
9393 {
9394 comps.push_back (new probe_point::component(TOK_STATEMENT,
9395 new literal_number(addr, true)));
9396 comps.push_back (new probe_point::component(TOK_ABSOLUTE));
9397 }
9398 else
9399 {
9400 size_t pos = name.find(':');
9401 if (pos != string::npos)
9402 {
9403 interned_string module = name.substr(0, pos);
9404 interned_string function = name.substr(pos + 1);
9405 comps.push_back (new probe_point::component(TOK_MODULE, new literal_string(module)));
9406 comps.push_back (new probe_point::component(TOK_FUNCTION, new literal_string(function)));
9407 }
9408 else
9409 comps.push_back (new probe_point::component(TOK_FUNCTION, new literal_string(name)));
9410 }
9411
9412 if (has_call)
9413 comps.push_back (new probe_point::component(TOK_CALL));
9414 if (has_return)
9415 comps.push_back (new probe_point::component(TOK_RETURN));
9416 if (has_maxactive)
9417 comps.push_back (new probe_point::component(TOK_MAXACTIVE, new literal_number(maxactive_val)));
9418
9419 kprobe_var_expanding_visitor v (sess, has_return);
9420 v.replace (this->body);
9421
9422 // If during target-variable-expanding the probe, we added a new block
9423 // of code, add it to the start of the probe.
9424 if (v.add_block)
9425 this->body = new block(v.add_block, this->body);
9426
9427 // If when target-variable-expanding the probe, we need to
9428 // synthesize a sibling function-entry probe. We don't go through
9429 // the whole probe derivation business (PR10642) that could lead to
9430 // wildcard/alias resolution, or for that dwarf-induced duplication.
9431 if (v.add_call_probe)
9432 {
9433 assert (has_return);
9434
9435 // We temporarily replace base.
9436 statement* old_body = base->body;
9437 base->body = v.add_call_probe;
9438
9439 derived_probe *entry_handler
9440 = new kprobe_derived_probe (sess, results, base, location, name, 0,
9441 true /* has_call */, false /* has_return */,
9442 has_statement, has_maxactive, has_path,
9443 has_library, maxactive_val, path, library);
9444 results.push_back (entry_handler);
9445
9446 base->body = old_body;
9447 }
9448
9449 this->sole_location()->components = comps;
9450 }
9451
9452 void kprobe_derived_probe::printsig (ostream& o) const
9453 {
9454 sole_location()->print (o);
9455 o << " /* " << " name = " << symbol_name << "*/";
9456 printsig_nested (o);
9457 }
9458
9459 void kprobe_derived_probe::join_group (systemtap_session& s)
9460 {
9461 if (! s.kprobe_derived_probes)
9462 s.kprobe_derived_probes = new kprobe_derived_probe_group ();
9463 s.kprobe_derived_probes->enroll (this);
9464 this->group = s.kprobe_derived_probes;
9465 }
9466
9467 void kprobe_derived_probe_group::enroll (kprobe_derived_probe* p)
9468 {
9469 probes_by_module.insert (make_pair (p->symbol_name, p));
9470 // probes of same symbol should share single kprobe/kretprobe
9471 }
9472
9473 void
9474 kprobe_derived_probe_group::emit_module_decls (systemtap_session& s)
9475 {
9476 if (probes_by_module.empty()) return;
9477
9478 s.op->newline() << "/* ---- kprobe-based probes ---- */";
9479
9480 // Warn of misconfigured kernels
9481 s.op->newline() << "#if ! defined(CONFIG_KPROBES)";
9482 s.op->newline() << "#error \"Need CONFIG_KPROBES!\"";
9483 s.op->newline() << "#endif";
9484 s.op->newline();
9485
9486 s.op->newline() << "#ifndef KRETACTIVE";
9487 s.op->newline() << "#define KRETACTIVE (max(15,6*(int)num_possible_cpus()))";
9488 s.op->newline() << "#endif";
9489
9490 // Forward declare the master entry functions
9491 s.op->newline() << "static int enter_kprobe2_probe (struct kprobe *inst,";
9492 s.op->line() << " struct pt_regs *regs);";
9493 s.op->newline() << "static int enter_kretprobe2_probe (struct kretprobe_instance *inst,";
9494 s.op->line() << " struct pt_regs *regs);";
9495
9496 // Emit an array of kprobe/kretprobe pointers
9497 s.op->newline() << "#if defined(STAPCONF_UNREGISTER_KPROBES)";
9498 s.op->newline() << "static void * stap_unreg_kprobes2[" << probes_by_module.size() << "];";
9499 s.op->newline() << "#endif";
9500
9501 // Emit the actual probe list.
9502
9503 s.op->newline() << "static struct stap_dwarfless_kprobe {";
9504 s.op->newline(1) << "union { struct kprobe kp; struct kretprobe krp; } u;";
9505 s.op->newline() << "#ifdef __ia64__";
9506 s.op->newline() << "struct kprobe dummy;";
9507 s.op->newline() << "#endif";
9508 s.op->newline(-1) << "} stap_dwarfless_kprobes[" << probes_by_module.size() << "];";
9509 // NB: bss!
9510
9511 s.op->newline() << "static struct stap_dwarfless_probe {";
9512 s.op->newline(1) << "const unsigned return_p:1;";
9513 s.op->newline() << "const unsigned maxactive_p:1;";
9514 s.op->newline() << "const unsigned optional_p:1;";
9515 s.op->newline() << "unsigned registered_p:1;";
9516 s.op->newline() << "const unsigned short maxactive_val;";
9517
9518 // Function Names are mostly small and uniform enough to justify putting
9519 // char[MAX]'s into the array instead of relocated char*'s.
9520
9521 size_t symbol_string_name_max = 0;
9522 size_t symbol_string_name_tot = 0;
9523 for (p_b_m_iterator it = probes_by_module.begin(); it != probes_by_module.end(); it++)
9524 {
9525 kprobe_derived_probe* p = it->second;
9526 #define DOIT(var,expr) do { \
9527 size_t var##_size = (expr) + 1; \
9528 var##_max = max (var##_max, var##_size); \
9529 var##_tot += var##_size; } while (0)
9530 DOIT(symbol_string_name, p->symbol_name.size());
9531 #undef DOIT
9532 }
9533
9534 #define CALCIT(var) \
9535 s.op->newline() << "const char " << #var << "[" << var##_name_max << "] ;";
9536
9537 CALCIT(symbol_string);
9538 #undef CALCIT
9539
9540 s.op->newline() << "unsigned long address;";
9541 s.op->newline() << "const struct stap_probe * const probe;";
9542 s.op->newline(-1) << "} stap_dwarfless_probes[] = {";
9543 s.op->indent(1);
9544
9545 for (p_b_m_iterator it = probes_by_module.begin(); it != probes_by_module.end(); it++)
9546 {
9547 kprobe_derived_probe* p = it->second;
9548 s.op->newline() << "{";
9549 if (p->has_return)
9550 s.op->line() << " .return_p=1,";
9551
9552 if (p->has_maxactive)
9553 {
9554 s.op->line() << " .maxactive_p=1,";
9555 assert (p->maxactive_val >= 0 && p->maxactive_val <= USHRT_MAX);
9556 s.op->line() << " .maxactive_val=" << p->maxactive_val << ",";
9557 }
9558
9559 if (p->locations[0]->optional)
9560 s.op->line() << " .optional_p=1,";
9561
9562 if (p->has_statement)
9563 s.op->line() << " .address=(unsigned long)0x" << hex << p->addr << dec << "ULL,";
9564 else
9565 s.op->line() << " .symbol_string=\"" << p->symbol_name << "\",";
9566
9567 s.op->line() << " .probe=" << common_probe_init (p) << ",";
9568 s.op->line() << " },";
9569 }
9570
9571 s.op->newline(-1) << "};";
9572
9573 // Emit the kprobes callback function
9574 s.op->newline();
9575 s.op->newline() << "static int enter_kprobe2_probe (struct kprobe *inst,";
9576 s.op->line() << " struct pt_regs *regs) {";
9577 // NB: as of PR5673, the kprobe|kretprobe union struct is in BSS
9578 s.op->newline(1) << "int kprobe_idx = ((uintptr_t)inst-(uintptr_t)stap_dwarfless_kprobes)/sizeof(struct stap_dwarfless_kprobe);";
9579 // Check that the index is plausible
9580 s.op->newline() << "struct stap_dwarfless_probe *sdp = &stap_dwarfless_probes[";
9581 s.op->line() << "((kprobe_idx >= 0 && kprobe_idx < " << probes_by_module.size() << ")?";
9582 s.op->line() << "kprobe_idx:0)"; // NB: at least we avoid memory corruption
9583 // XXX: it would be nice to give a more verbose error though; BUG_ON later?
9584 s.op->line() << "];";
9585 common_probe_entryfn_prologue (s, "STAP_SESSION_RUNNING", "sdp->probe",
9586 "stp_probe_type_kprobe");
9587 s.op->newline() << "c->kregs = regs;";
9588
9589 // Make it look like the IP is set as it wouldn't have been replaced
9590 // by a breakpoint instruction when calling real probe handler. Reset
9591 // IP regs on return, so we don't confuse kprobes. PR10458
9592 s.op->newline() << "{";
9593 s.op->indent(1);
9594 s.op->newline() << "unsigned long kprobes_ip = REG_IP(c->kregs);";
9595 s.op->newline() << "SET_REG_IP(regs, (unsigned long) inst->addr);";
9596 s.op->newline() << "(*sdp->probe->ph) (c);";
9597 s.op->newline() << "SET_REG_IP(regs, kprobes_ip);";
9598 s.op->newline(-1) << "}";
9599
9600 common_probe_entryfn_epilogue (s, true, otf_safe_context(s));
9601 s.op->newline() << "return 0;";
9602 s.op->newline(-1) << "}";
9603
9604 // Same for kretprobes
9605 s.op->newline();
9606 s.op->newline() << "static int enter_kretprobe2_probe (struct kretprobe_instance *inst,";
9607 s.op->line() << " struct pt_regs *regs) {";
9608 s.op->newline(1) << "struct kretprobe *krp = inst->rp;";
9609
9610 // NB: as of PR5673, the kprobe|kretprobe union struct is in BSS
9611 s.op->newline() << "int kprobe_idx = ((uintptr_t)krp-(uintptr_t)stap_dwarfless_kprobes)/sizeof(struct stap_dwarfless_kprobe);";
9612 // Check that the index is plausible
9613 s.op->newline() << "struct stap_dwarfless_probe *sdp = &stap_dwarfless_probes[";
9614 s.op->line() << "((kprobe_idx >= 0 && kprobe_idx < " << probes_by_module.size() << ")?";
9615 s.op->line() << "kprobe_idx:0)"; // NB: at least we avoid memory corruption
9616 // XXX: it would be nice to give a more verbose error though; BUG_ON later?
9617 s.op->line() << "];";
9618
9619 common_probe_entryfn_prologue (s, "STAP_SESSION_RUNNING", "sdp->probe",
9620 "stp_probe_type_kretprobe");
9621 s.op->newline() << "c->kregs = regs;";
9622 s.op->newline() << "c->ips.krp.pi = inst;"; // for assisting runtime's backtrace logic
9623
9624 // Make it look like the IP is set as it wouldn't have been replaced
9625 // by a breakpoint instruction when calling real probe handler. Reset
9626 // IP regs on return, so we don't confuse kprobes. PR10458
9627 s.op->newline() << "{";
9628 s.op->indent(1);
9629 s.op->newline() << "unsigned long kprobes_ip = REG_IP(c->kregs);";
9630 s.op->newline() << "SET_REG_IP(regs, (unsigned long) inst->rp->kp.addr);";
9631 s.op->newline() << "(*sdp->probe->ph) (c);";
9632 s.op->newline() << "SET_REG_IP(regs, kprobes_ip);";
9633 s.op->newline(-1) << "}";
9634
9635 common_probe_entryfn_epilogue (s, true, otf_safe_context(s));
9636 s.op->newline() << "return 0;";
9637 s.op->newline(-1) << "}";
9638
9639 s.op->newline() << "#ifdef STAPCONF_KALLSYMS_ON_EACH_SYMBOL";
9640 s.op->newline() << "static int kprobe_resolve(void *data, const char *name,";
9641 s.op->newline() << " struct module *owner,";
9642 s.op->newline() << " unsigned long val) {";
9643 s.op->newline(1) << "int i;";
9644 s.op->newline() << "int *p = (int *) data;";
9645 s.op->newline() << "for (i=0; i<" << probes_by_module.size()
9646 << " && *p > 0; i++) {";
9647 s.op->newline(1) << "struct stap_dwarfless_probe *sdp = & stap_dwarfless_probes[i];";
9648 s.op->newline() << "if (! sdp->address) {";
9649 s.op->indent(1);
9650 s.op->newline() << "const char *colon;";
9651 s.op->newline() << "if (owner && (colon = strchr(sdp->symbol_string, ':'))) {";
9652 s.op->indent(1);
9653 s.op->newline() << "if ((strlen(owner->name) == (colon - sdp->symbol_string))";
9654 s.op->newline() << " && (strncmp(sdp->symbol_string, owner->name, colon - sdp->symbol_string) == 0)";
9655 s.op->newline() << " && (strcmp(colon + 1, name) == 0)) {";
9656 s.op->newline(1) << "sdp->address = val;";
9657 s.op->newline() << "(*p)--;";
9658 s.op->newline(-1) << "}";
9659 s.op->newline(-1) << "}";
9660 s.op->newline() << "else {";
9661 s.op->newline(1) << "if (strcmp(sdp->symbol_string, name) == 0) {";
9662 s.op->newline(1) << "sdp->address = val;";
9663 s.op->newline() << "(*p)--;";
9664 s.op->newline(-1) << "}";
9665 s.op->newline(-1) << "}";
9666 s.op->newline(-1) << "}";
9667 s.op->newline(-1) << "}";
9668 s.op->newline() << "return (p > 0) ? 0 : -1;";
9669 s.op->newline(-1) << "}";
9670 s.op->newline() << "#endif";
9671 }
9672
9673
9674 void
9675 kprobe_derived_probe_group::emit_module_init (systemtap_session& s)
9676 {
9677 s.op->newline() << "#ifdef STAPCONF_KALLSYMS_ON_EACH_SYMBOL";
9678 s.op->newline() << "{";
9679 s.op->newline(1) << "int p = 0;";
9680 s.op->newline() << "for (i = 0; i < " << probes_by_module.size() << "; i++) {";
9681 s.op->newline(1) << "struct stap_dwarfless_probe *sdp = & stap_dwarfless_probes[i];";
9682 s.op->newline() << "if (! sdp->address)";
9683 s.op->newline(1) << "p++;";
9684 s.op->newline(-2) << "}";
9685 s.op->newline() << "rcu_read_lock();";
9686 s.op->newline() << "kallsyms_on_each_symbol(kprobe_resolve, &p);";
9687 s.op->newline() << "rcu_read_unlock();";
9688 s.op->newline(-1) << "}";
9689 s.op->newline() << "#endif";
9690
9691 s.op->newline() << "for (i=0; i<" << probes_by_module.size() << "; i++) {";
9692 s.op->newline(1) << "struct stap_dwarfless_probe *sdp = & stap_dwarfless_probes[i];";
9693 s.op->newline() << "struct stap_dwarfless_kprobe *kp = & stap_dwarfless_kprobes[i];";
9694 s.op->newline() << "void *addr = (void *) sdp->address;";
9695 s.op->newline() << "const char *symbol_name = addr ? NULL : sdp->symbol_string;";
9696
9697 s.op->newline() << "#ifdef STAPCONF_KALLSYMS_ON_EACH_SYMBOL";
9698 s.op->newline() << "if (! addr) {";
9699 s.op->newline(1) << "sdp->registered_p = 0;";
9700 s.op->newline() << "if (!sdp->optional_p)";
9701 s.op->newline(1) << "_stp_warn (\"probe %s registration error (symbol not found)\", probe_point);";
9702 s.op->newline(-1) << "continue;";
9703 s.op->newline(-1) << "}";
9704 s.op->newline() << "#endif";
9705
9706 s.op->newline() << "probe_point = sdp->probe->pp;"; // for error messages
9707 s.op->newline() << "if (sdp->return_p) {";
9708 s.op->newline(1) << "kp->u.krp.kp.addr = addr;";
9709 s.op->newline() << "#ifdef STAPCONF_KPROBE_SYMBOL_NAME";
9710 s.op->newline() << "kp->u.krp.kp.symbol_name = (char *) symbol_name;";
9711 s.op->newline() << "#endif";
9712 s.op->newline() << "if (sdp->maxactive_p) {";
9713 s.op->newline(1) << "kp->u.krp.maxactive = sdp->maxactive_val;";
9714 s.op->newline(-1) << "} else {";
9715 s.op->newline(1) << "kp->u.krp.maxactive = KRETACTIVE;";
9716 s.op->newline(-1) << "}";
9717 s.op->newline() << "kp->u.krp.handler = &enter_kretprobe2_probe;";
9718 // to ensure safeness of bspcache, always use aggr_kprobe on ia64
9719 s.op->newline() << "#ifdef __ia64__";
9720 s.op->newline() << "kp->dummy.addr = kp->u.krp.kp.addr;";
9721 s.op->newline() << "#ifdef STAPCONF_KPROBE_SYMBOL_NAME";
9722 s.op->newline() << "kp->dummy.symbol_name = kp->u.krp.kp.symbol_name;";
9723 s.op->newline() << "#endif";
9724 s.op->newline() << "kp->dummy.pre_handler = NULL;";
9725 s.op->newline() << "rc = register_kprobe (& kp->dummy);";
9726 s.op->newline() << "if (rc == 0) {";
9727 s.op->newline(1) << "rc = register_kretprobe (& kp->u.krp);";
9728 s.op->newline() << "if (rc != 0)";
9729 s.op->newline(1) << "unregister_kprobe (& kp->dummy);";
9730 s.op->newline(-2) << "}";
9731 s.op->newline() << "#else";
9732 s.op->newline() << "rc = register_kretprobe (& kp->u.krp);";
9733 s.op->newline() << "#endif";
9734 s.op->newline(-1) << "} else {";
9735 // to ensure safeness of bspcache, always use aggr_kprobe on ia64
9736 s.op->newline(1) << "kp->u.kp.addr = addr;";
9737 s.op->newline() << "#ifdef STAPCONF_KPROBE_SYMBOL_NAME";
9738 s.op->newline() << "kp->u.kp.symbol_name = (char *) symbol_name;";
9739 s.op->newline() << "#endif";
9740 s.op->newline() << "kp->u.kp.pre_handler = &enter_kprobe2_probe;";
9741 s.op->newline() << "#ifdef __ia64__";
9742 s.op->newline() << "kp->dummy.pre_handler = NULL;";
9743 s.op->newline() << "kp->dummy.addr = kp->u.kp.addr;";
9744 s.op->newline() << "#ifdef STAPCONF_KPROBE_SYMBOL_NAME";
9745 s.op->newline() << "kp->dummy.symbol_name = kp->u.kp.symbol_name;";
9746 s.op->newline() << "#endif";
9747 s.op->newline() << "rc = register_kprobe (& kp->dummy);";
9748 s.op->newline() << "if (rc == 0) {";
9749 s.op->newline(1) << "rc = register_kprobe (& kp->u.kp);";
9750 s.op->newline() << "if (rc != 0)";
9751 s.op->newline(1) << "unregister_kprobe (& kp->dummy);";
9752 s.op->newline(-2) << "}";
9753 s.op->newline() << "#else";
9754 s.op->newline() << "rc = register_kprobe (& kp->u.kp);";
9755 s.op->newline() << "#endif";
9756 s.op->newline(-1) << "}";
9757 s.op->newline() << "if (rc) {"; // PR6749: tolerate a failed register_*probe.
9758 s.op->newline(1) << "sdp->registered_p = 0;";
9759 s.op->newline() << "if (!sdp->optional_p)";
9760 s.op->newline(1) << "_stp_warn (\"probe %s (address 0x%lx) registration error (rc %d)\", probe_point, (unsigned long) addr, rc);";
9761 s.op->newline(-1) << "rc = 0;"; // continue with other probes
9762 // XXX: shall we increment numskipped?
9763 s.op->newline(-1) << "}";
9764
9765 s.op->newline() << "else sdp->registered_p = 1;";
9766 s.op->newline(-1) << "}"; // for loop
9767 }
9768
9769
9770 void
9771 kprobe_derived_probe_group::emit_module_exit (systemtap_session& s)
9772 {
9773 //Unregister kprobes by batch interfaces.
9774 s.op->newline() << "#if defined(STAPCONF_UNREGISTER_KPROBES)";
9775 s.op->newline() << "j = 0;";
9776 s.op->newline() << "for (i=0; i<" << probes_by_module.size() << "; i++) {";
9777 s.op->newline(1) << "struct stap_dwarfless_probe *sdp = & stap_dwarfless_probes[i];";
9778 s.op->newline() << "struct stap_dwarfless_kprobe *kp = & stap_dwarfless_kprobes[i];";
9779 s.op->newline() << "if (! sdp->registered_p) continue;";
9780 s.op->newline() << "if (!sdp->return_p)";
9781 s.op->newline(1) << "stap_unreg_kprobes2[j++] = &kp->u.kp;";
9782 s.op->newline(-2) << "}";
9783 s.op->newline() << "unregister_kprobes((struct kprobe **)stap_unreg_kprobes2, j);";
9784 s.op->newline() << "j = 0;";
9785 s.op->newline() << "for (i=0; i<" << probes_by_module.size() << "; i++) {";
9786 s.op->newline(1) << "struct stap_dwarfless_probe *sdp = & stap_dwarfless_probes[i];";
9787 s.op->newline() << "struct stap_dwarfless_kprobe *kp = & stap_dwarfless_kprobes[i];";
9788 s.op->newline() << "if (! sdp->registered_p) continue;";
9789 s.op->newline() << "if (sdp->return_p)";
9790 s.op->newline(1) << "stap_unreg_kprobes2[j++] = &kp->u.krp;";
9791 s.op->newline(-2) << "}";
9792 s.op->newline() << "unregister_kretprobes((struct kretprobe **)stap_unreg_kprobes2, j);";
9793 s.op->newline() << "#ifdef __ia64__";
9794 s.op->newline() << "j = 0;";
9795 s.op->newline() << "for (i=0; i<" << probes_by_module.size() << "; i++) {";
9796 s.op->newline(1) << "struct stap_dwarfless_probe *sdp = & stap_dwarfless_probes[i];";
9797 s.op->newline() << "struct stap_dwarfless_kprobe *kp = & stap_dwarfless_kprobes[i];";
9798 s.op->newline() << "if (! sdp->registered_p) continue;";
9799 s.op->newline() << "stap_unreg_kprobes2[j++] = &kp->dummy;";
9800 s.op->newline(-1) << "}";
9801 s.op->newline() << "unregister_kprobes((struct kprobe **)stap_unreg_kprobes2, j);";
9802 s.op->newline() << "#endif";
9803 s.op->newline() << "#endif";
9804
9805 s.op->newline() << "for (i=0; i<" << probes_by_module.size() << "; i++) {";
9806 s.op->newline(1) << "struct stap_dwarfless_probe *sdp = & stap_dwarfless_probes[i];";
9807 s.op->newline() << "struct stap_dwarfless_kprobe *kp = & stap_dwarfless_kprobes[i];";
9808 s.op->newline() << "if (! sdp->registered_p) continue;";
9809 s.op->newline() << "if (sdp->return_p) {";
9810 s.op->newline() << "#if !defined(STAPCONF_UNREGISTER_KPROBES)";
9811 s.op->newline(1) << "unregister_kretprobe (&kp->u.krp);";
9812 s.op->newline() << "#endif";
9813 s.op->newline() << "atomic_add (kp->u.krp.nmissed, skipped_count());";
9814 s.op->newline() << "#ifdef STP_TIMING";
9815 s.op->newline() << "if (kp->u.krp.nmissed)";
9816 s.op->newline(1) << "_stp_warn (\"Skipped due to missed kretprobe/1 on '%s': %d\\n\", sdp->probe->pp, kp->u.krp.nmissed);";
9817 s.op->newline(-1) << "#endif";
9818 s.op->newline() << "atomic_add (kp->u.krp.kp.nmissed, skipped_count());";
9819 s.op->newline() << "#ifdef STP_TIMING";
9820 s.op->newline() << "if (kp->u.krp.kp.nmissed)";
9821 s.op->newline(1) << "_stp_warn (\"Skipped due to missed kretprobe/2 on '%s': %lu\\n\", sdp->probe->pp, kp->u.krp.kp.nmissed);";
9822 s.op->newline(-1) << "#endif";
9823 s.op->newline(-1) << "} else {";
9824 s.op->newline() << "#if !defined(STAPCONF_UNREGISTER_KPROBES)";
9825 s.op->newline(1) << "unregister_kprobe (&kp->u.kp);";
9826 s.op->newline() << "#endif";
9827 s.op->newline() << "atomic_add (kp->u.kp.nmissed, skipped_count());";
9828 s.op->newline() << "#ifdef STP_TIMING";
9829 s.op->newline() << "if (kp->u.kp.nmissed)";
9830 s.op->newline(1) << "_stp_warn (\"Skipped due to missed kprobe on '%s': %lu\\n\", sdp->probe->pp, kp->u.kp.nmissed);";
9831 s.op->newline(-1) << "#endif";
9832 s.op->newline(-1) << "}";
9833 s.op->newline() << "#if !defined(STAPCONF_UNREGISTER_KPROBES) && defined(__ia64__)";
9834 s.op->newline() << "unregister_kprobe (&kp->dummy);";
9835 s.op->newline() << "#endif";
9836 s.op->newline() << "sdp->registered_p = 0;";
9837 s.op->newline(-1) << "}";
9838 }
9839
9840 struct kprobe_builder: public derived_probe_builder
9841 {
9842 public:
9843 kprobe_builder() {}
9844
9845 void build_no_more (systemtap_session &s) {}
9846
9847 virtual void build(systemtap_session & sess,
9848 probe * base,
9849 probe_point * location,
9850 literal_map_t const & parameters,
9851 vector<derived_probe *> & finished_results);
9852 };
9853
9854
9855 void
9856 kprobe_builder::build(systemtap_session & sess,
9857 probe * base,
9858 probe_point * location,
9859 literal_map_t const & parameters,
9860 vector<derived_probe *> & finished_results)
9861 {
9862 interned_string function_string_val, module_string_val;
9863 interned_string path, library, path_tgt, library_tgt;
9864 int64_t statement_num_val = 0, maxactive_val = 0;
9865 bool has_function_str, has_module_str, has_statement_num;
9866 bool has_absolute, has_call, has_return, has_maxactive;
9867 bool has_path, has_library;
9868
9869 has_function_str = get_param(parameters, TOK_FUNCTION, function_string_val);
9870 has_module_str = get_param(parameters, TOK_MODULE, module_string_val);
9871 has_call = has_null_param (parameters, TOK_CALL);
9872 has_return = has_null_param (parameters, TOK_RETURN);
9873 has_maxactive = get_param(parameters, TOK_MAXACTIVE, maxactive_val);
9874 has_statement_num = get_param(parameters, TOK_STATEMENT, statement_num_val);
9875 has_absolute = has_null_param (parameters, TOK_ABSOLUTE);
9876 has_path = get_param (parameters, TOK_PROCESS, path);
9877 has_library = get_param (parameters, TOK_LIBRARY, library);
9878
9879 if (has_path)
9880 {
9881 path = find_executable (path, sess.sysroot, sess.sysenv);
9882 path_tgt = path_remove_sysroot(sess, path);
9883 }
9884 if (has_library)
9885 {
9886 library = find_executable (library, sess.sysroot, sess.sysenv,
9887 "LD_LIBRARY_PATH");
9888 library_tgt = path_remove_sysroot(sess, library);
9889 }
9890
9891 if (has_function_str)
9892 {
9893 if (has_module_str)
9894 {
9895 function_string_val = (string)module_string_val + ":" + (string)function_string_val;
9896 derived_probe *dp
9897 = new kprobe_derived_probe (sess, finished_results, base,
9898 location, function_string_val,
9899 0, has_call, has_return,
9900 has_statement_num, has_maxactive,
9901 has_path, has_library, maxactive_val,
9902 path_tgt, library_tgt);
9903 finished_results.push_back (dp);
9904 }
9905 else
9906 {
9907 vector<interned_string> matches;
9908
9909 // Simple names can be found directly
9910 if (function_string_val.find_first_of("*?[") == string::npos)
9911 {
9912 if (sess.kernel_functions.count(function_string_val))
9913 matches.push_back(function_string_val);
9914 }
9915 else // Search function name list for matching names
9916 {
9917 const string& val = function_string_val;
9918 for (set<interned_string>::const_iterator it = sess.kernel_functions.begin();
9919 it != sess.kernel_functions.end(); it++)
9920 {
9921 // fnmatch returns zero for matching.
9922 if (fnmatch(val.c_str(), it->to_string().c_str(), 0) == 0)
9923 matches.push_back(*it);
9924 }
9925 }
9926
9927 for (vector<interned_string>::const_iterator it = matches.begin();
9928 it != matches.end(); it++)
9929 {
9930 derived_probe *dp
9931 = new kprobe_derived_probe (sess, finished_results, base,
9932 location, *it, 0, has_call,
9933 has_return, has_statement_num,
9934 has_maxactive, has_path,
9935 has_library, maxactive_val,
9936 path_tgt, library_tgt);
9937 finished_results.push_back (dp);
9938 }
9939 }
9940 }
9941 else
9942 {
9943 // assert guru mode for absolute probes
9944 if ( has_statement_num && has_absolute && !base->privileged )
9945 throw SEMANTIC_ERROR (_("absolute statement probe in unprivileged script; need stap -g"), base->tok);
9946
9947 finished_results.push_back (new kprobe_derived_probe (sess,
9948 finished_results,
9949 base,
9950 location, "",
9951 statement_num_val,
9952 has_call,
9953 has_return,
9954 has_statement_num,
9955 has_maxactive,
9956 has_path,
9957 has_library,
9958 maxactive_val,
9959 path_tgt,
9960 library_tgt));
9961 }
9962 }
9963
9964
9965 void
9966 kprobe_var_expanding_visitor::visit_entry_op (entry_op *e)
9967 {
9968 expression *repl = e;
9969
9970 if (has_return)
9971 {
9972 // expand the operand as if it weren't a return probe
9973 has_return = false;
9974 replace (e->operand);
9975 has_return = true;
9976
9977 // XXX it would be nice to use gen_kretprobe_saved_return when
9978 // available, but it requires knowing the types already, which is
9979 // problematic for arbitrary expressons.
9980 repl = gen_mapped_saved_return (sess, e->operand, "entry",
9981 add_block, add_block_tid,
9982 add_call_probe, add_call_probe_tid);
9983 }
9984 provide (repl);
9985 }
9986
9987
9988 // ------------------------------------------------------------------------
9989 // Hardware breakpoint based probes.
9990 // ------------------------------------------------------------------------
9991
9992 static const string TOK_HWBKPT("data");
9993 static const string TOK_HWBKPT_WRITE("write");
9994 static const string TOK_HWBKPT_RW("rw");
9995 static const string TOK_LENGTH("length");
9996
9997 #define HWBKPT_READ 0
9998 #define HWBKPT_WRITE 1
9999 #define HWBKPT_RW 2
10000 struct hwbkpt_derived_probe: public derived_probe
10001 {
10002 hwbkpt_derived_probe (probe *base,
10003 probe_point *location,
10004 uint64_t addr,
10005 string symname,
10006 unsigned int len,
10007 bool has_only_read_access,
10008 bool has_only_write_access,
10009 bool has_rw_access
10010 );
10011 Dwarf_Addr hwbkpt_addr;
10012 string symbol_name;
10013 unsigned int hwbkpt_access,hwbkpt_len;
10014
10015 void printsig (std::ostream &o) const;
10016 void join_group (systemtap_session& s);
10017 };
10018
10019 struct hwbkpt_derived_probe_group: public derived_probe_group
10020 {
10021 private:
10022 vector<hwbkpt_derived_probe*> hwbkpt_probes;
10023
10024 public:
10025 void enroll (hwbkpt_derived_probe* probe, systemtap_session& s);
10026 void emit_module_decls (systemtap_session& s);
10027 void emit_module_init (systemtap_session& s);
10028 void emit_module_exit (systemtap_session& s);
10029 };
10030
10031 hwbkpt_derived_probe::hwbkpt_derived_probe (probe *base,
10032 probe_point *location,
10033 uint64_t addr,
10034 string symname,
10035 unsigned int len,
10036 bool has_only_read_access,
10037 bool has_only_write_access,
10038 bool):
10039 derived_probe (base, location, true /* .components soon rewritten */ ),
10040 hwbkpt_addr (addr),
10041 symbol_name (symname),
10042 hwbkpt_len (len)
10043 {
10044 this->tok = base->tok;
10045
10046 vector<probe_point::component*> comps;
10047 comps.push_back (new probe_point::component(TOK_KERNEL));
10048
10049 if (hwbkpt_addr)
10050 comps.push_back (new probe_point::component (TOK_HWBKPT,
10051 new literal_number(hwbkpt_addr, true)));
10052 else if (symbol_name.size())
10053 comps.push_back (new probe_point::component (TOK_HWBKPT, new literal_string(symbol_name)));
10054
10055 comps.push_back (new probe_point::component (TOK_LENGTH, new literal_number(hwbkpt_len)));
10056
10057 if (has_only_read_access)
10058 this->hwbkpt_access = HWBKPT_READ ;
10059 //TODO add code for comps.push_back for read, since this flag is not for x86
10060
10061 else
10062 {
10063 if (has_only_write_access)
10064 {
10065 this->hwbkpt_access = HWBKPT_WRITE ;
10066 comps.push_back (new probe_point::component(TOK_HWBKPT_WRITE));
10067 }
10068 else
10069 {
10070 this->hwbkpt_access = HWBKPT_RW ;
10071 comps.push_back (new probe_point::component(TOK_HWBKPT_RW));
10072 }
10073 }
10074
10075 this->sole_location()->components = comps;
10076 }
10077
10078 void hwbkpt_derived_probe::printsig (ostream& o) const
10079 {
10080 sole_location()->print (o);
10081 printsig_nested (o);
10082 }
10083
10084 void hwbkpt_derived_probe::join_group (systemtap_session& s)
10085 {
10086 if (! s.hwbkpt_derived_probes)
10087 s.hwbkpt_derived_probes = new hwbkpt_derived_probe_group ();
10088 s.hwbkpt_derived_probes->enroll (this, s);
10089 this->group = s.hwbkpt_derived_probes;
10090 }
10091
10092 void hwbkpt_derived_probe_group::enroll (hwbkpt_derived_probe* p, systemtap_session& s)
10093 {
10094 hwbkpt_probes.push_back (p);
10095
10096 unsigned max_hwbkpt_probes_by_arch = 0;
10097 if (s.architecture == "i386" || s.architecture == "x86_64")
10098 max_hwbkpt_probes_by_arch = 4;
10099 else if (s.architecture == "s390")
10100 max_hwbkpt_probes_by_arch = 1;
10101
10102 if (hwbkpt_probes.size() >= max_hwbkpt_probes_by_arch)
10103 s.print_warning (_F("Too many hardware breakpoint probes requested for %s (%zu vs. %u)",
10104 s.architecture.c_str(), hwbkpt_probes.size(), max_hwbkpt_probes_by_arch));
10105 }
10106
10107 void
10108 hwbkpt_derived_probe_group::emit_module_decls (systemtap_session& s)
10109 {
10110 if (hwbkpt_probes.empty()) return;
10111
10112 s.op->newline() << "/* ---- hwbkpt-based probes ---- */";
10113
10114 s.op->newline() << "#include <linux/perf_event.h>";
10115 s.op->newline() << "#include <linux/hw_breakpoint.h>";
10116 s.op->newline();
10117
10118 // Forward declare the master entry functions
10119 s.op->newline() << "#ifdef STAPCONF_PERF_HANDLER_NMI";
10120 s.op->newline() << "static int enter_hwbkpt_probe (struct perf_event *bp,";
10121 s.op->line() << " int nmi,";
10122 s.op->line() << " struct perf_sample_data *data,";
10123 s.op->line() << " struct pt_regs *regs);";
10124 s.op->newline() << "#else";
10125 s.op->newline() << "static int enter_hwbkpt_probe (struct perf_event *bp,";
10126 s.op->line() << " struct perf_sample_data *data,";
10127 s.op->line() << " struct pt_regs *regs);";
10128 s.op->newline() << "#endif";
10129
10130 // Emit the actual probe list.
10131
10132 s.op->newline() << "static struct perf_event_attr ";
10133 s.op->newline() << "stap_hwbkpt_probe_array[" << hwbkpt_probes.size() << "];";
10134
10135 s.op->newline() << "static struct perf_event **";
10136 s.op->newline() << "stap_hwbkpt_ret_array[" << hwbkpt_probes.size() << "];";
10137 s.op->newline() << "static struct stap_hwbkpt_probe {";
10138 s.op->newline() << "int registered_p:1;";
10139 // registered_p = 0 signifies a probe that is unregistered (or failed)
10140 // registered_p = 1 signifies a probe that got registered successfully
10141
10142 // Symbol Names are mostly small and uniform enough
10143 // to justify putting const char*.
10144 s.op->newline() << "const char * const symbol;";
10145
10146 s.op->newline() << "const unsigned long address;";
10147 s.op->newline() << "uint8_t atype;";
10148 s.op->newline() << "unsigned int len;";
10149 s.op->newline() << "const struct stap_probe * const probe;";
10150 s.op->newline() << "} stap_hwbkpt_probes[] = {";
10151 s.op->indent(1);
10152
10153 for (unsigned int it = 0; it < hwbkpt_probes.size(); it++)
10154 {
10155 hwbkpt_derived_probe* p = hwbkpt_probes.at(it);
10156 s.op->newline() << "{";
10157 if (p->symbol_name.size())
10158 s.op->line() << " .address=(unsigned long)0x0" << "ULL,";
10159 else
10160 s.op->line() << " .address=(unsigned long)0x" << hex << p->hwbkpt_addr << dec << "ULL,";
10161 switch(p->hwbkpt_access){
10162 case HWBKPT_READ:
10163 s.op->line() << " .atype=HW_BREAKPOINT_R ,";
10164 break;
10165 case HWBKPT_WRITE:
10166 s.op->line() << " .atype=HW_BREAKPOINT_W ,";
10167 break;
10168 case HWBKPT_RW:
10169 s.op->line() << " .atype=HW_BREAKPOINT_R|HW_BREAKPOINT_W ,";
10170 break;
10171 };
10172 s.op->line() << " .len=" << p->hwbkpt_len << ",";
10173 s.op->line() << " .probe=" << common_probe_init (p) << ",";
10174 s.op->line() << " .symbol=\"" << p->symbol_name << "\",";
10175 s.op->line() << " },";
10176 }
10177 s.op->newline(-1) << "};";
10178
10179 // Emit the hwbkpt callback function
10180 s.op->newline() ;
10181 s.op->newline() << "#ifdef STAPCONF_PERF_HANDLER_NMI";
10182 s.op->newline() << "static int enter_hwbkpt_probe (struct perf_event *bp,";
10183 s.op->line() << " int nmi,";
10184 s.op->line() << " struct perf_sample_data *data,";
10185 s.op->line() << " struct pt_regs *regs) {";
10186 s.op->newline() << "#else";
10187 s.op->newline() << "static int enter_hwbkpt_probe (struct perf_event *bp,";
10188 s.op->line() << " struct perf_sample_data *data,";
10189 s.op->line() << " struct pt_regs *regs) {";
10190 s.op->newline() << "#endif";
10191 s.op->newline(1) << "unsigned int i;";
10192 s.op->newline() << "if (bp->attr.type != PERF_TYPE_BREAKPOINT) return -1;";
10193 s.op->newline() << "for (i=0; i<" << hwbkpt_probes.size() << "; i++) {";
10194 s.op->newline(1) << "struct perf_event_attr *hp = & stap_hwbkpt_probe_array[i];";
10195 // XXX: why not match stap_hwbkpt_ret_array[i] against bp instead?
10196 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) {";
10197 s.op->newline(1) << "struct stap_hwbkpt_probe *sdp = &stap_hwbkpt_probes[i];";
10198 common_probe_entryfn_prologue (s, "STAP_SESSION_RUNNING", "sdp->probe",
10199 "stp_probe_type_hwbkpt");
10200 s.op->newline() << "if (user_mode(regs)) {";
10201 s.op->newline(1)<< "c->user_mode_p = 1;";
10202 s.op->newline() << "c->uregs = regs;";
10203 s.op->newline(-1) << "} else {";
10204 s.op->newline(1) << "c->kregs = regs;";
10205 s.op->newline(-1) << "}";
10206 s.op->newline() << "(*sdp->probe->ph) (c);";
10207 common_probe_entryfn_epilogue (s, true, otf_safe_context(s));
10208 s.op->newline(-1) << "}";
10209 s.op->newline(-1) << "}";
10210 s.op->newline() << "return 0;";
10211 s.op->newline(-1) << "}";
10212 }
10213
10214 void
10215 hwbkpt_derived_probe_group::emit_module_init (systemtap_session& s)
10216 {
10217 s.op->newline() << "for (i=0; i<" << hwbkpt_probes.size() << "; i++) {";
10218 s.op->newline(1) << "struct stap_hwbkpt_probe *sdp = & stap_hwbkpt_probes[i];";
10219 s.op->newline() << "struct perf_event_attr *hp = & stap_hwbkpt_probe_array[i];";
10220 s.op->newline() << "void *addr = (void *) sdp->address;";
10221 s.op->newline() << "const char *hwbkpt_symbol_name = addr ? NULL : sdp->symbol;";
10222 s.op->newline() << "hw_breakpoint_init(hp);";
10223 s.op->newline() << "if (addr)";
10224 s.op->newline(1) << "hp->bp_addr = (unsigned long) addr;";
10225 s.op->newline(-1) << "else { ";
10226 s.op->newline(1) << "hp->bp_addr = kallsyms_lookup_name(hwbkpt_symbol_name);";
10227 s.op->newline() << "if (!hp->bp_addr) { ";
10228 s.op->newline(1) << "_stp_warn(\"Probe %s registration skipped: invalid symbol %s \",sdp->probe->pp,hwbkpt_symbol_name);";
10229 s.op->newline() << "continue;";
10230 s.op->newline(-1) << "}";
10231 s.op->newline(-1) << "}";
10232 s.op->newline() << "hp->bp_type = sdp->atype;";
10233
10234 // On x86 & x86-64, hp->bp_len is not just a number but a macro/enum (!?!).
10235 if (s.architecture == "i386" || s.architecture == "x86_64" )
10236 {
10237 s.op->newline() << "switch(sdp->len) {";
10238 s.op->newline() << "case 1:";
10239 s.op->newline(1) << "hp->bp_len = HW_BREAKPOINT_LEN_1;";
10240 s.op->newline() << "break;";
10241 s.op->newline(-1) << "case 2:";
10242 s.op->newline(1) << "hp->bp_len = HW_BREAKPOINT_LEN_2;";
10243 s.op->newline() << "break;";
10244 s.op->newline(-1) << "case 3:";
10245 s.op->newline() << "case 4:";
10246 s.op->newline(1) << "hp->bp_len = HW_BREAKPOINT_LEN_4;";
10247 s.op->newline() << "break;";
10248 s.op->newline(-1) << "case 5:";
10249 s.op->newline() << "case 6:";
10250 s.op->newline() << "case 7:";
10251 s.op->newline() << "case 8:";
10252 s.op->newline() << "default:"; // XXX: could instead reject
10253 s.op->newline(1) << "hp->bp_len = HW_BREAKPOINT_LEN_8;";
10254 s.op->newline() << "break;";
10255 s.op->newline(-1) << "}";
10256 }
10257 else // other architectures presumed straightforward
10258 s.op->newline() << "hp->bp_len = sdp->len;";
10259
10260 s.op->newline() << "probe_point = sdp->probe->pp;"; // for error messages
10261 s.op->newline() << "#ifdef STAPCONF_HW_BREAKPOINT_CONTEXT";
10262 s.op->newline() << "stap_hwbkpt_ret_array[i] = register_wide_hw_breakpoint(hp, (void *)&enter_hwbkpt_probe, NULL);";
10263 s.op->newline() << "#else";
10264 s.op->newline() << "stap_hwbkpt_ret_array[i] = register_wide_hw_breakpoint(hp, (void *)&enter_hwbkpt_probe);";
10265 s.op->newline() << "#endif";
10266 s.op->newline() << "rc = 0;";
10267 s.op->newline() << "if (IS_ERR(stap_hwbkpt_ret_array[i])) {";
10268 s.op->newline(1) << "rc = PTR_ERR(stap_hwbkpt_ret_array[i]);";
10269 s.op->newline() << "stap_hwbkpt_ret_array[i] = 0;";
10270 s.op->newline(-1) << "}";
10271 s.op->newline() << "if (rc) {";
10272 s.op->newline(1) << "_stp_warn(\"Hwbkpt probe %s: registration error %d, addr %p, name %s\", probe_point, rc, addr, hwbkpt_symbol_name);";
10273 s.op->newline() << "sdp->registered_p = 0;";
10274 s.op->newline(-1) << "}";
10275 s.op->newline() << " else sdp->registered_p = 1;";
10276 s.op->newline(-1) << "}"; // for loop
10277 }
10278
10279 void
10280 hwbkpt_derived_probe_group::emit_module_exit (systemtap_session& s)
10281 {
10282 //Unregister hwbkpt probes.
10283 s.op->newline() << "for (i=0; i<" << hwbkpt_probes.size() << "; i++) {";
10284 s.op->newline(1) << "struct stap_hwbkpt_probe *sdp = & stap_hwbkpt_probes[i];";
10285 s.op->newline() << "if (sdp->registered_p == 0) continue;";
10286 s.op->newline() << "unregister_wide_hw_breakpoint(stap_hwbkpt_ret_array[i]);";
10287 s.op->newline() << "sdp->registered_p = 0;";
10288 s.op->newline(-1) << "}";
10289 }
10290
10291 struct hwbkpt_builder: public derived_probe_builder
10292 {
10293 hwbkpt_builder() {}
10294 virtual void build(systemtap_session & sess,
10295 probe * base,
10296 probe_point * location,
10297 literal_map_t const & parameters,
10298 vector<derived_probe *> & finished_results);
10299 };
10300
10301 void
10302 hwbkpt_builder::build(systemtap_session & sess,
10303 probe * base,
10304 probe_point * location,
10305 literal_map_t const & parameters,
10306 vector<derived_probe *> & finished_results)
10307 {
10308 interned_string symbol_str_val;
10309 int64_t hwbkpt_address, len;
10310 bool has_addr, has_symbol_str, has_write, has_rw, has_len;
10311
10312 if (! (sess.kernel_config["CONFIG_PERF_EVENTS"] == string("y")))
10313 throw SEMANTIC_ERROR (_("CONFIG_PERF_EVENTS not available on this kernel"),
10314 location->components[0]->tok);
10315 if (! (sess.kernel_config["CONFIG_HAVE_HW_BREAKPOINT"] == string("y")))
10316 throw SEMANTIC_ERROR (_("CONFIG_HAVE_HW_BREAKPOINT not available on this kernel"),
10317 location->components[0]->tok);
10318
10319 has_addr = get_param (parameters, TOK_HWBKPT, hwbkpt_address);
10320 has_symbol_str = get_param (parameters, TOK_HWBKPT, symbol_str_val);
10321 has_len = get_param (parameters, TOK_LENGTH, len);
10322 has_write = (parameters.find(TOK_HWBKPT_WRITE) != parameters.end());
10323 has_rw = (parameters.find(TOK_HWBKPT_RW) != parameters.end());
10324
10325 // Make an intermediate pp that is well-formed. It's pretty much the same as
10326 // the user-provided one, except that the addr literal is well-typed.
10327 probe_point* well_formed_loc = new probe_point(*location);
10328 well_formed_loc->well_formed = true;
10329
10330 vector<probe_point::component*> well_formed_comps;
10331 vector<probe_point::component*>::iterator it;
10332 for (it = location->components.begin();
10333 it != location->components.end(); ++it)
10334 if ((*it)->functor == TOK_HWBKPT && has_addr)
10335 well_formed_comps.push_back(new probe_point::component(TOK_HWBKPT,
10336 new literal_number(hwbkpt_address, true /* hex */ )));
10337 else
10338 well_formed_comps.push_back(*it);
10339 well_formed_loc->components = well_formed_comps;
10340 probe *new_base = new probe (base, well_formed_loc);
10341
10342 if (!has_len)
10343 len = 1;
10344
10345 if (has_addr)
10346 finished_results.push_back (new hwbkpt_derived_probe (new_base,
10347 location,
10348 hwbkpt_address,
10349 "",len,0,
10350 has_write,
10351 has_rw));
10352 else if (has_symbol_str)
10353 finished_results.push_back (new hwbkpt_derived_probe (new_base,
10354 location,
10355 0,
10356 symbol_str_val,len,0,
10357 has_write,
10358 has_rw));
10359 else
10360 assert (0);
10361 }
10362
10363 // ------------------------------------------------------------------------
10364 // statically inserted kernel-tracepoint derived probes
10365 // ------------------------------------------------------------------------
10366
10367 struct tracepoint_arg
10368 {
10369 string name, c_type, typecast;
10370 bool usable, used, isptr;
10371 Dwarf_Die type_die;
10372 tracepoint_arg(): usable(false), used(false), isptr(false), type_die() {}
10373 };
10374
10375 struct tracepoint_derived_probe: public derived_probe
10376 {
10377 tracepoint_derived_probe (systemtap_session& s,
10378 dwflpp& dw, Dwarf_Die& func_die,
10379 const string& tracepoint_system,
10380 const string& tracepoint_name,
10381 probe* base_probe, probe_point* location);
10382
10383 systemtap_session& sess;
10384 string tracepoint_system, tracepoint_name, header;
10385 vector <struct tracepoint_arg> args;
10386
10387 void build_args(dwflpp& dw, Dwarf_Die& func_die);
10388 void getargs (std::list<std::string> &arg_set) const;
10389 void join_group (systemtap_session& s);
10390 void print_dupe_stamp(ostream& o);
10391 };
10392
10393
10394 struct tracepoint_derived_probe_group: public generic_dpg<tracepoint_derived_probe>
10395 {
10396 void emit_module_decls (systemtap_session& s);
10397 void emit_module_init (systemtap_session& s);
10398 void emit_module_exit (systemtap_session& s);
10399 };
10400
10401
10402 struct tracepoint_var_expanding_visitor: public var_expanding_visitor
10403 {
10404 tracepoint_var_expanding_visitor(dwflpp& dw, const string& probe_name,
10405 vector <struct tracepoint_arg>& args):
10406 dw (dw), probe_name (probe_name), args (args) {}
10407 dwflpp& dw;
10408 const string& probe_name;
10409 vector <struct tracepoint_arg>& args;
10410
10411 void visit_target_symbol (target_symbol* e);
10412 void visit_target_symbol_arg (target_symbol* e);
10413 void visit_target_symbol_context (target_symbol* e);
10414 };
10415
10416
10417 void
10418 tracepoint_var_expanding_visitor::visit_target_symbol_arg (target_symbol* e)
10419 {
10420 string argname = e->sym_name();
10421 string en = e->name;
10422
10423 // search for a tracepoint parameter matching this name
10424 tracepoint_arg *arg = NULL;
10425 for (unsigned i = 0; i < args.size(); ++i)
10426 if (args[i].usable && args[i].name == argname)
10427 {
10428 arg = &args[i];
10429 arg->used = true;
10430 break;
10431 }
10432
10433 if (arg == NULL)
10434 {
10435 set<string> vars;
10436 for (unsigned i = 0; i < args.size(); ++i)
10437 vars.insert("$" + args[i].name);
10438 vars.insert("$$name");
10439 vars.insert("$$parms");
10440 vars.insert("$$vars");
10441 string sugs = levenshtein_suggest(en, vars); // no need to limit, there's not that many
10442
10443 // We hope that this value ends up not being referenced after all, so it
10444 // can be optimized out quietly.
10445 throw SEMANTIC_ERROR(_F("unable to find tracepoint variable '%s'%s",
10446 en.c_str(), sugs.empty() ? "" :
10447 (_(" (alternatives: ") + sugs + ")").c_str()), e->tok);
10448 // NB: we use 'alternatives' because we list all
10449 // NB: we can have multiple errors, since a target variable
10450 // may be expanded in several different contexts:
10451 // trace ("*") { $foo->bar }
10452 }
10453
10454 // make sure we're not dereferencing base types or void
10455 bool deref_p = arg->isptr && !null_die(&arg->type_die);
10456 if (!deref_p)
10457 e->assert_no_components("tracepoint", true);
10458
10459 // we can only write to dereferenced fields, and only if guru mode is on
10460 bool lvalue = is_active_lvalue(e);
10461 if (lvalue && (!dw.sess.guru_mode || e->components.empty()))
10462 throw SEMANTIC_ERROR(_F("write to tracepoint variable '%s' not permitted; need stap -g", en.c_str()), e->tok);
10463
10464 // XXX: if a struct/union arg is passed by value, then writing to its fields
10465 // is also meaningless until you dereference past a pointer member. It's
10466 // harder to detect and prevent that though...
10467
10468 if (e->components.empty())
10469 {
10470 if (e->addressof)
10471 throw SEMANTIC_ERROR(_("cannot take address of tracepoint variable"), e->tok);
10472
10473 // Just grab the value from the probe locals
10474 symbol* sym = new symbol;
10475 sym->tok = e->tok;
10476 sym->name = "__tracepoint_arg_" + arg->name;
10477 sym->type_details.reset(new exp_type_dwarf(&dw, &arg->type_die, false, false));
10478 provide (sym);
10479 }
10480 else
10481 {
10482 // make a copy of the original as a bare target symbol for the tracepoint
10483 // value, which will be passed into the dwarf dereferencing code
10484 target_symbol* e2 = deep_copy_visitor::deep_copy(e);
10485 e2->components.clear();
10486
10487 if (e->check_pretty_print (lvalue))
10488 {
10489 dwarf_pretty_print dpp(dw, &arg->type_die, e2, deref_p, false, *e);
10490 dpp.expand()->visit (this);
10491 return;
10492 }
10493
10494 bool userspace_p = false;
10495 string fname = (string(lvalue ? "_tracepoint_tvar_set" : "_tracepoint_tvar_get")
10496 + "_" + e->sym_name()
10497 + "_" + lex_cast(tick++));
10498
10499 Dwarf_Die endtype;
10500 string code = dw.literal_stmt_for_pointer (&arg->type_die, e, lvalue, &endtype);
10501
10502 functioncall* n = synthetic_embedded_deref_call(dw, &endtype, fname, code,
10503 userspace_p, lvalue, e, e2);
10504
10505 if (lvalue)
10506 provide_lvalue_call (n);
10507
10508 // Revisit the functioncall so arguments can be expanded.
10509 n->visit (this);
10510 }
10511 }
10512
10513
10514 void
10515 tracepoint_var_expanding_visitor::visit_target_symbol_context (target_symbol* e)
10516 {
10517 if (e->addressof)
10518 throw SEMANTIC_ERROR(_("cannot take address of context variable"), e->tok);
10519
10520 if (is_active_lvalue (e))
10521 throw SEMANTIC_ERROR(_F("write to tracepoint '%s' not permitted",
10522 e->name.to_string().c_str()), e->tok);
10523
10524 if (e->name == "$$name" || e->name == "$$system")
10525 {
10526 e->assert_no_components("tracepoint");
10527
10528 string member = (e->name == "$$name") ? "c->ips.tp.tracepoint_name"
10529 : "c->ips.tp.tracepoint_system";
10530
10531 // Synthesize an embedded expression.
10532 embedded_expr *expr = new embedded_expr;
10533 expr->tok = e->tok;
10534 expr->code = string("/* string */ /* pure */ " +
10535 member + " ? " + member + " : \"\"");
10536 provide (expr);
10537 }
10538 else if (e->name == "$$vars" || e->name == "$$parms")
10539 {
10540 e->assert_no_components("tracepoint", true);
10541
10542 print_format* pf = print_format::create(e->tok, "sprintf");
10543
10544 for (unsigned i = 0; i < args.size(); ++i)
10545 {
10546 if (!args[i].usable)
10547 continue;
10548 if (i > 0)
10549 pf->raw_components += " ";
10550 pf->raw_components += args[i].name;
10551 target_symbol *tsym = new target_symbol;
10552 tsym->tok = e->tok;
10553 tsym->name = "$" + args[i].name;
10554 tsym->components = e->components;
10555
10556 // every variable should always be accessible!
10557 tsym->saved_conversion_error = 0;
10558 expression *texp = require<expression> (tsym); // NB: throws nothing ...
10559 if (tsym->saved_conversion_error) // ... but this is how we know it happened.
10560 {
10561 if (dw.sess.verbose>2)
10562 for (const semantic_error *c = tsym->saved_conversion_error;
10563 c != 0; c = c->get_chain())
10564 clog << _("variable location problem [man error::dwarf]: ") << c->what() << endl;
10565 pf->raw_components += "=?";
10566 continue;
10567 }
10568
10569 if (e->check_pretty_print ())
10570 pf->raw_components += "=%s";
10571 else
10572 pf->raw_components += args[i].isptr ? "=%p" : "=%#x";
10573 pf->args.push_back(texp);
10574 }
10575
10576 pf->components = print_format::string_to_components(pf->raw_components);
10577 provide (pf);
10578 }
10579 else
10580 assert(0); // shouldn't get here
10581 }
10582
10583 void
10584 tracepoint_var_expanding_visitor::visit_target_symbol (target_symbol* e)
10585 {
10586 try
10587 {
10588 assert(e->name.size() > 0 && e->name[0] == '$');
10589
10590 if (e->name == "$$name" || e->name == "$$system"
10591 || e->name == "$$parms" || e->name == "$$vars")
10592 visit_target_symbol_context (e);
10593 else
10594 visit_target_symbol_arg (e);
10595 }
10596 catch (const semantic_error &er)
10597 {
10598 e->chain (er);
10599 provide (e);
10600 }
10601 }
10602
10603
10604 tracepoint_derived_probe::tracepoint_derived_probe (systemtap_session& s,
10605 dwflpp& dw, Dwarf_Die& func_die,
10606 const string& tracepoint_system,
10607 const string& tracepoint_name,
10608 probe* base, probe_point* loc):
10609 derived_probe (base, loc, true /* .components soon rewritten */), sess (s),
10610 tracepoint_system (tracepoint_system), tracepoint_name (tracepoint_name)
10611 {
10612 // create synthetic probe point name; preserve condition
10613 vector<probe_point::component*> comps;
10614 comps.push_back (new probe_point::component (TOK_KERNEL));
10615
10616 // tag on system to the final name unless we're in compatibility mode so that
10617 // e.g. pn() returns just the name as before
10618 string final_name = tracepoint_name;
10619 if (!tracepoint_system.empty()
10620 && strverscmp(s.compatible.c_str(), "2.6") > 0)
10621 final_name = tracepoint_system + ":" + final_name;
10622
10623 comps.push_back (new probe_point::component (TOK_TRACE,
10624 new literal_string(final_name)));
10625 this->sole_location()->components = comps;
10626
10627 // fill out the available arguments in this tracepoint
10628 build_args(dw, func_die);
10629
10630 // determine which header defined this tracepoint
10631 string decl_file = dwarf_decl_file(&func_die);
10632 header = decl_file;
10633
10634 #if 0 /* This convention is not enforced. */
10635 size_t header_pos = decl_file.rfind("trace/");
10636 if (header_pos == string::npos)
10637 throw SEMANTIC_ERROR ("cannot parse header location for tracepoint '"
10638 + tracepoint_name + "' in '"
10639 + decl_file + "'");
10640 header = decl_file.substr(header_pos);
10641 #endif
10642
10643 // tracepoints from FOO_event_types.h should really be included from FOO.h
10644 // XXX can dwarf tell us the include hierarchy? it would be better to
10645 // ... walk up to see which one was directly included by tracequery.c
10646 // XXX: see also PR9993.
10647 size_t header_pos = header.find("_event_types");
10648 if (header_pos != string::npos)
10649 header.erase(header_pos, 12);
10650
10651 // Now expand the local variables in the probe body
10652 tracepoint_var_expanding_visitor v (dw, name, args);
10653 v.replace (this->body);
10654 for (unsigned i = 0; i < args.size(); i++)
10655 if (args[i].used)
10656 {
10657 vardecl* v = new vardecl;
10658 v->name = "__tracepoint_arg_" + args[i].name;
10659 v->tok = this->tok;
10660 v->set_arity(0, this->tok);
10661 v->type = pe_long;
10662 v->synthetic = true;
10663 this->locals.push_back (v);
10664 }
10665
10666 if (sess.verbose > 2)
10667 clog << "tracepoint-based " << name << " tracepoint='" << tracepoint_name << "'" << endl;
10668 }
10669
10670
10671 static bool
10672 resolve_pointer_type(Dwarf_Die& die, bool& isptr)
10673 {
10674 if (null_die(&die))
10675 {
10676 isptr = true;
10677 return true;
10678 }
10679
10680 Dwarf_Die type;
10681 switch (dwarf_tag(&die))
10682 {
10683 case DW_TAG_typedef:
10684 case DW_TAG_const_type:
10685 case DW_TAG_volatile_type:
10686 case DW_TAG_restrict_type:
10687 // iterate on the referent type
10688 return (dwarf_attr_die(&die, DW_AT_type, &die)
10689 && resolve_pointer_type(die, isptr));
10690
10691 case DW_TAG_base_type:
10692 case DW_TAG_enumeration_type:
10693 case DW_TAG_structure_type:
10694 case DW_TAG_union_type:
10695 // base types will simply be treated as script longs
10696 // structs/unions must be referenced by pointer elsewhere
10697 isptr = false;
10698 return true;
10699
10700 case DW_TAG_array_type:
10701 case DW_TAG_pointer_type:
10702 case DW_TAG_reference_type:
10703 case DW_TAG_rvalue_reference_type:
10704 // pointer-like types can be treated as script longs,
10705 // and if we know their type, they can also be dereferenced
10706 isptr = true;
10707 type = die;
10708 while (dwarf_attr_die(&type, DW_AT_type, &type))
10709 {
10710 // It still might be a non-type, e.g. const void,
10711 // so we need to strip away all qualifiers.
10712 int tag = dwarf_tag(&type);
10713 if (tag != DW_TAG_typedef &&
10714 tag != DW_TAG_const_type &&
10715 tag != DW_TAG_volatile_type &&
10716 tag != DW_TAG_restrict_type)
10717 {
10718 die = type;
10719 return true;
10720 }
10721 }
10722 // otherwise use a null_die to indicate void
10723 std::memset(&die, 0, sizeof(die));
10724 return true;
10725
10726 default:
10727 // should we consider other types too?
10728 return false;
10729 }
10730 }
10731
10732
10733 static bool
10734 resolve_tracepoint_arg_type(tracepoint_arg& arg)
10735 {
10736 if (!resolve_pointer_type(arg.type_die, arg.isptr))
10737 return false;
10738
10739 if (arg.isptr)
10740 arg.typecast = "(intptr_t)";
10741 else if (dwarf_tag(&arg.type_die) == DW_TAG_structure_type ||
10742 dwarf_tag(&arg.type_die) == DW_TAG_union_type)
10743 {
10744 // for structs/unions which are passed by value, we turn it into
10745 // a pointer that can be dereferenced.
10746 arg.isptr = true;
10747 arg.typecast = "(intptr_t)&";
10748 }
10749 return true;
10750 }
10751
10752
10753 void
10754 tracepoint_derived_probe::build_args(dwflpp&, Dwarf_Die& func_die)
10755 {
10756 Dwarf_Die arg;
10757 if (dwarf_child(&func_die, &arg) == 0)
10758 do
10759 if (dwarf_tag(&arg) == DW_TAG_formal_parameter)
10760 {
10761 // build a tracepoint_arg for this parameter
10762 tracepoint_arg tparg;
10763 tparg.name = dwarf_diename(&arg) ?: "";
10764
10765 // read the type of this parameter
10766 if (!dwarf_attr_die (&arg, DW_AT_type, &tparg.type_die)
10767 || !dwarf_type_name(&tparg.type_die, tparg.c_type))
10768 throw SEMANTIC_ERROR (_F("cannot get type of parameter '%s' of tracepoint '%s'",
10769 tparg.name.c_str(), tracepoint_name.c_str()));
10770
10771 tparg.usable = resolve_tracepoint_arg_type(tparg);
10772 args.push_back(tparg);
10773 if (sess.verbose > 4)
10774 clog << _F("found parameter for tracepoint '%s': type:'%s' name:'%s' %s",
10775 tracepoint_name.c_str(), tparg.c_type.c_str(), tparg.name.c_str(),
10776 tparg.usable ? "ok" : "unavailable") << endl;
10777 }
10778 while (dwarf_siblingof(&arg, &arg) == 0);
10779 }
10780
10781 void
10782 tracepoint_derived_probe::getargs(std::list<std::string> &arg_set) const
10783 {
10784 for (unsigned i = 0; i < args.size(); ++i)
10785 if (args[i].usable)
10786 arg_set.push_back("$"+args[i].name+":"+args[i].c_type);
10787 }
10788
10789 void
10790 tracepoint_derived_probe::join_group (systemtap_session& s)
10791 {
10792 if (! s.tracepoint_derived_probes)
10793 s.tracepoint_derived_probes = new tracepoint_derived_probe_group ();
10794 s.tracepoint_derived_probes->enroll (this);
10795 this->group = s.tracepoint_derived_probes;
10796 }
10797
10798
10799 void
10800 tracepoint_derived_probe::print_dupe_stamp(ostream& o)
10801 {
10802 for (unsigned i = 0; i < args.size(); i++)
10803 if (args[i].used)
10804 o << "__tracepoint_arg_" << args[i].name << endl;
10805 }
10806
10807
10808 static vector<string> tracepoint_extra_decls (systemtap_session& s, const string& header)
10809 {
10810 vector<string> they_live;
10811 // PR 9993
10812 // XXX: may need this to be configurable
10813 they_live.push_back ("#include <linux/skbuff.h>");
10814
10815 // PR11649: conditional extra header
10816 // for kvm tracepoints in 2.6.33ish
10817 if (s.kernel_config["CONFIG_KVM"] != string("")) {
10818 they_live.push_back ("#include <linux/kvm_host.h>");
10819 }
10820
10821 if (header.find("xfs") != string::npos && s.kernel_config["CONFIG_XFS_FS"] != string("")) {
10822 they_live.push_back ("#define XFS_BIG_BLKNOS 1");
10823 if (s.kernel_source_tree != "")
10824 they_live.push_back ("#include \"fs/xfs/xfs_types.h\""); // in kernel-source tree
10825 they_live.push_back ("struct xfs_mount;");
10826 they_live.push_back ("struct xfs_inode;");
10827 they_live.push_back ("struct xfs_buf;");
10828 they_live.push_back ("struct xfs_bmbt_irec;");
10829 they_live.push_back ("struct xfs_trans;");
10830 }
10831
10832 if (header.find("nfs") != string::npos && s.kernel_config["CONFIG_NFSD"] != string("")) {
10833 they_live.push_back ("struct rpc_task;");
10834 }
10835 // RHEL6.3
10836 if (header.find("rpc") != string::npos && s.kernel_config["CONFIG_NFSD"] != string("")) {
10837 they_live.push_back ("struct rpc_clnt;");
10838 they_live.push_back ("struct rpc_wait_queue;");
10839 }
10840
10841 they_live.push_back ("#include <asm/cputime.h>");
10842
10843 // linux 3.0
10844 they_live.push_back ("struct cpu_workqueue_struct;");
10845
10846 if (header.find("ext4") != string::npos && s.kernel_config["CONFIG_EXT4_FS"] != string(""))
10847 if (s.kernel_source_tree != "")
10848 they_live.push_back ("#include \"fs/ext4/ext4.h\""); // in kernel-source tree
10849
10850 if (header.find("ext3") != string::npos)
10851 they_live.push_back ("struct ext3_reserve_window_node;");
10852
10853 if (header.find("workqueue") != string::npos)
10854 {
10855 they_live.push_back ("struct pool_workqueue;");
10856 they_live.push_back ("struct work_struct;");
10857 }
10858
10859 if (header.find("asoc") != string::npos)
10860 they_live.push_back ("struct snd_soc_dapm_path;");
10861
10862 if (header.find("9p") != string::npos)
10863 {
10864 they_live.push_back ("struct p9_client;");
10865 they_live.push_back ("struct p9_fcall;");
10866 }
10867
10868 if (header.find("bcache") != string::npos)
10869 {
10870 they_live.push_back ("struct bkey;");
10871 they_live.push_back ("struct btree;");
10872 they_live.push_back ("struct cache_set;");
10873 they_live.push_back ("struct cache;");
10874 }
10875
10876 if (header.find("f2fs") != string::npos)
10877 {
10878 // cannot get fs/f2fs/f2fs.h #included
10879 they_live.push_back ("typedef u32 block_t;");
10880 they_live.push_back ("typedef u32 nid_t;");
10881 }
10882
10883 if (header.find("radeon") != string::npos)
10884 they_live.push_back ("struct radeon_bo;");
10885
10886 // argh, 3.11, i915_trace.h -> i915_drv.h -> i915_reg.h without -I
10887 // also brcms_trace_events.h -> ... -> "types.h"
10888 // XXX: need a way to add a temporary -I flag
10889
10890 if (header.find("/ath/") != string::npos)
10891 they_live.push_back ("struct ath5k_hw;");
10892
10893
10894 return they_live;
10895 }
10896
10897
10898 void
10899 tracepoint_derived_probe_group::emit_module_decls (systemtap_session& s)
10900 {
10901 if (probes.empty())
10902 return;
10903
10904 s.op->newline() << "/* ---- tracepoint probes ---- */";
10905 s.op->newline() << "#include <linux/stp_tracepoint.h>" << endl;
10906 s.op->newline();
10907
10908
10909 // We create a MODULE_aux_N.c file for each tracepoint header, to allow them
10910 // to be separately compiled. That's because kernel tracepoint headers sometimes
10911 // conflict. PR13155.
10912
10913 map<string,translator_output*> per_header_aux;
10914 // GC NB: the translator_output* structs are owned/retained by the systemtap_session.
10915
10916 for (unsigned i = 0; i < probes.size(); ++i)
10917 {
10918 tracepoint_derived_probe *p = probes[i];
10919 string header = p->header;
10920
10921 // We cache the auxiliary output files on a per-header basis. We don't
10922 // need one aux file per tracepoint, only one per tracepoint-header.
10923 translator_output *tpop = per_header_aux[header];
10924 if (tpop == 0)
10925 {
10926 tpop = s.op_create_auxiliary();
10927 per_header_aux[header] = tpop;
10928
10929 // PR9993: Add extra headers to work around undeclared types in individual
10930 // include/trace/foo.h files
10931 const vector<string>& extra_decls = tracepoint_extra_decls (s, header);
10932 for (unsigned z=0; z<extra_decls.size(); z++)
10933 tpop->newline() << extra_decls[z] << "\n";
10934
10935 // strip include/ substring, the same way as done in get_tracequery_module()
10936 size_t root_pos = header.rfind("include/");
10937 header = ((root_pos != string::npos) ? header.substr(root_pos + 8) : header);
10938
10939 tpop->newline() << "#include <linux/stp_tracepoint.h>" << endl;
10940 tpop->newline() << "#include <" << header << ">";
10941 }
10942
10943 // collect the args that are actually in use
10944 vector<const tracepoint_arg*> used_args;
10945 for (unsigned j = 0; j < p->args.size(); ++j)
10946 if (p->args[j].used)
10947 used_args.push_back(&p->args[j]);
10948
10949 // forward-declare the generated-side tracepoint callback, and define the
10950 // generated-side tracepoint callback in the main translator-output
10951 string enter_real_fn = "enter_real_tracepoint_probe_" + lex_cast(i);
10952 if (used_args.empty())
10953 {
10954 tpop->newline() << "STP_TRACE_ENTER_REAL_NOARGS(" << enter_real_fn << ");";
10955 s.op->newline() << "STP_TRACE_ENTER_REAL_NOARGS(" << enter_real_fn << ")";
10956 }
10957 else
10958 {
10959 tpop->newline() << "STP_TRACE_ENTER_REAL(" << enter_real_fn;
10960 s.op->newline() << "STP_TRACE_ENTER_REAL(" << enter_real_fn;
10961 s.op->indent(2);
10962 for (unsigned j = 0; j < used_args.size(); ++j)
10963 {
10964 tpop->line() << ", int64_t";
10965 s.op->newline() << ", int64_t __tracepoint_arg_" << used_args[j]->name;
10966 }
10967 tpop->line() << ");";
10968 s.op->newline() << ")";
10969 s.op->indent(-2);
10970 }
10971 s.op->newline() << "{";
10972 s.op->newline(1) << "const struct stap_probe * const probe = "
10973 << common_probe_init (p) << ";";
10974 common_probe_entryfn_prologue (s, "STAP_SESSION_RUNNING", "probe",
10975 "stp_probe_type_tracepoint");
10976 s.op->newline() << "c->ips.tp.tracepoint_system = "
10977 << lex_cast_qstring (p->tracepoint_system)
10978 << ";";
10979 s.op->newline() << "c->ips.tp.tracepoint_name = "
10980 << lex_cast_qstring (p->tracepoint_name)
10981 << ";";
10982 for (unsigned j = 0; j < used_args.size(); ++j)
10983 {
10984 s.op->newline() << "c->probe_locals." << p->name
10985 << "." + s.up->c_localname("__tracepoint_arg_" + used_args[j]->name)
10986 << " = __tracepoint_arg_" << used_args[j]->name << ";";
10987 }
10988 s.op->newline() << "(*probe->ph) (c);";
10989 common_probe_entryfn_epilogue (s, true, otf_safe_context(s));
10990 s.op->newline(-1) << "}";
10991
10992 // define the real tracepoint callback function
10993 string enter_fn = "enter_tracepoint_probe_" + lex_cast(i);
10994 if (p->args.empty())
10995 tpop->newline() << "static STP_TRACE_ENTER_NOARGS(" << enter_fn << ")";
10996 else
10997 {
10998 tpop->newline() << "static STP_TRACE_ENTER(" << enter_fn;
10999 s.op->indent(2);
11000 for (unsigned j = 0; j < p->args.size(); ++j)
11001 {
11002 tpop->newline() << ", " << p->args[j].c_type
11003 << " __tracepoint_arg_" << p->args[j].name;
11004 }
11005 tpop->newline() << ")";
11006 s.op->indent(-2);
11007 }
11008 tpop->newline() << "{";
11009 tpop->newline(1) << enter_real_fn << "(";
11010 tpop->indent(2);
11011 for (unsigned j = 0; j < used_args.size(); ++j)
11012 {
11013 if (j > 0)
11014 tpop->line() << ", ";
11015 tpop->newline() << "(int64_t)" << used_args[j]->typecast
11016 << "__tracepoint_arg_" << used_args[j]->name;
11017 }
11018 tpop->newline() << ");";
11019 tpop->newline(-3) << "}";
11020
11021
11022 // emit normalized registration functions
11023 tpop->newline() << "int register_tracepoint_probe_" << i << "(void) {";
11024 tpop->newline(1) << "return STP_TRACE_REGISTER(" << p->tracepoint_name
11025 << ", " << enter_fn << ");";
11026 tpop->newline(-1) << "}";
11027
11028 // NB: we're not prepared to deal with unreg failures. However, failures
11029 // can only occur if the tracepoint doesn't exist (yet?), or if we
11030 // weren't even registered. The former should be OKed by the initial
11031 // registration call, and the latter is safe to ignore.
11032 tpop->newline() << "void unregister_tracepoint_probe_" << i << "(void) {";
11033 tpop->newline(1) << "(void) STP_TRACE_UNREGISTER(" << p->tracepoint_name
11034 << ", " << enter_fn << ");";
11035 tpop->newline(-1) << "}";
11036 tpop->newline();
11037
11038 // declare normalized registration functions
11039 s.op->newline() << "int register_tracepoint_probe_" << i << "(void);";
11040 s.op->newline() << "void unregister_tracepoint_probe_" << i << "(void);";
11041
11042 tpop->assert_0_indent();
11043 }
11044
11045 // emit an array of registration functions for easy init/shutdown
11046 s.op->newline() << "static struct stap_tracepoint_probe {";
11047 s.op->newline(1) << "int (*reg)(void);";
11048 s.op->newline(0) << "void (*unreg)(void);";
11049 s.op->newline(-1) << "} stap_tracepoint_probes[] = {";
11050 s.op->indent(1);
11051 for (unsigned i = 0; i < probes.size(); ++i)
11052 {
11053 s.op->newline () << "{";
11054 s.op->line() << " .reg=&register_tracepoint_probe_" << i << ",";
11055 s.op->line() << " .unreg=&unregister_tracepoint_probe_" << i;
11056 s.op->line() << " },";
11057 }
11058 s.op->newline(-1) << "};";
11059 s.op->newline();
11060 }
11061
11062
11063 void
11064 tracepoint_derived_probe_group::emit_module_init (systemtap_session &s)
11065 {
11066 if (probes.size () == 0)
11067 return;
11068
11069 s.op->newline() << "/* init tracepoint probes */";
11070 s.op->newline() << "for (i=0; i<" << probes.size() << "; i++) {";
11071 s.op->newline(1) << "rc = stap_tracepoint_probes[i].reg();";
11072 s.op->newline() << "if (rc) {";
11073 s.op->newline(1) << "for (j=i-1; j>=0; j--)"; // partial rollback
11074 s.op->newline(1) << "stap_tracepoint_probes[j].unreg();";
11075 s.op->newline(-1) << "break;"; // don't attempt to register any more probes
11076 s.op->newline(-1) << "}";
11077 s.op->newline(-1) << "}";
11078
11079 // This would be technically proper (on those autoconf-detectable
11080 // kernels that include this function in tracepoint.h), however we
11081 // already make several calls to synchronze_sched() during our
11082 // shutdown processes.
11083
11084 // s.op->newline() << "if (rc)";
11085 // s.op->newline(1) << "tracepoint_synchronize_unregister();";
11086 // s.op->indent(-1);
11087 }
11088
11089
11090 void
11091 tracepoint_derived_probe_group::emit_module_exit (systemtap_session& s)
11092 {
11093 if (probes.empty())
11094 return;
11095
11096 s.op->newline() << "/* deregister tracepoint probes */";
11097 s.op->newline() << "for (i=0; i<" << probes.size() << "; i++)";
11098 s.op->newline(1) << "stap_tracepoint_probes[i].unreg();";
11099 s.op->indent(-1);
11100
11101 // Not necessary: see above.
11102
11103 // s.op->newline() << "tracepoint_synchronize_unregister();";
11104 }
11105
11106
11107 struct tracepoint_query : public base_query
11108 {
11109 probe * base_probe;
11110 probe_point * base_loc;
11111 vector<derived_probe *> & results;
11112 set<string> probed_names;
11113
11114 void handle_query_module();
11115 int handle_query_cu(Dwarf_Die * cudie);
11116 int handle_query_func(Dwarf_Die * func);
11117 void query_library (const char *) {}
11118 void query_plt (const char *entry, size_t addr) {}
11119
11120 static int tracepoint_query_cu (Dwarf_Die * cudie, tracepoint_query * q);
11121 static int tracepoint_query_func (Dwarf_Die * func, tracepoint_query * q);
11122
11123 tracepoint_query(dwflpp & dw, const string & tracepoint,
11124 probe * base_probe, probe_point * base_loc,
11125 vector<derived_probe *> & results):
11126 base_query(dw, "*"), base_probe(base_probe),
11127 base_loc(base_loc), results(results)
11128 {
11129 // The user may have specified the system to probe, e.g. all of the
11130 // following are possible:
11131 //
11132 // sched_switch --> tracepoint named sched_switch
11133 // sched:sched_switch --> tracepoint named sched_switch in the sched system
11134 // sch*:sched_* --> system starts with sch and tracepoint starts with sched_
11135 // sched:* --> all tracepoints in system sched
11136 // *:sched_switch --> same as just sched_switch
11137
11138 size_t sys_pos = tracepoint.find(':');
11139 if (sys_pos == string::npos)
11140 {
11141 this->system = "";
11142 this->tracepoint = tracepoint;
11143 }
11144 else
11145 {
11146 if (strverscmp(sess.compatible.c_str(), "2.6") <= 0)
11147 throw SEMANTIC_ERROR (_("SYSTEM:TRACEPOINT syntax not supported "
11148 "with --compatible <= 2.6"));
11149
11150 this->system = tracepoint.substr(0, sys_pos);
11151 this->tracepoint = tracepoint.substr(sys_pos+1);
11152 }
11153
11154 // make sure we have something to query (filters out e.g. "" and ":")
11155 if (this->tracepoint.empty())
11156 throw SEMANTIC_ERROR (_("invalid tracepoint string provided"));
11157 }
11158
11159 private:
11160 string system; // target subsystem(s) to query
11161 string tracepoint; // target tracepoint(s) to query
11162 string current_system; // subsystem of module currently being visited
11163
11164 string retrieve_trace_system();
11165 };
11166
11167 // name of section where TRACE_SYSTEM is stored
11168 // (see tracepoint_builder::get_tracequery_modules())
11169 #define STAP_TRACE_SYSTEM ".stap_trace_system"
11170
11171 string
11172 tracepoint_query::retrieve_trace_system()
11173 {
11174 Dwarf_Addr bias;
11175 Elf* elf = dwfl_module_getelf(dw.module, &bias);
11176 if (!elf)
11177 return "";
11178
11179 size_t shstrndx;
11180 if (elf_getshdrstrndx(elf, &shstrndx))
11181 return "";
11182
11183 Elf_Scn *scn = NULL;
11184 GElf_Shdr shdr_mem;
11185
11186 while ((scn = elf_nextscn(elf, scn)))
11187 {
11188 if (gelf_getshdr(scn, &shdr_mem) == NULL)
11189 return "";
11190
11191 const char *name = elf_strptr(elf, shstrndx, shdr_mem.sh_name);
11192 if (name == NULL)
11193 return "";
11194
11195 if (strcmp(name, STAP_TRACE_SYSTEM) == 0)
11196 break;
11197 }
11198
11199 if (scn == NULL)
11200 return "";
11201
11202 // Extract saved TRACE_SYSTEM in section
11203 Elf_Data *data = elf_getdata(scn, NULL);
11204 if (!data)
11205 return "";
11206
11207 return string((char*)data->d_buf);
11208 }
11209
11210
11211 void
11212 tracepoint_query::handle_query_module()
11213 {
11214 // Get the TRACE_SYSTEM for this module, if any. It will be found in the
11215 // STAP_TRACE_SYSTEM section if it exists.
11216 current_system = retrieve_trace_system();
11217
11218 // check if user wants a specific system
11219 if (!system.empty())
11220 {
11221 // don't need to go further if module has no system or doesn't
11222 // match the one we want
11223 if (current_system.empty()
11224 || !dw.function_name_matches_pattern(current_system, system))
11225 return;
11226 }
11227
11228 // look for the tracepoints in each CU
11229 dw.iterate_over_cus(tracepoint_query_cu, this, false);
11230 }
11231
11232
11233 int
11234 tracepoint_query::handle_query_cu(Dwarf_Die * cudie)
11235 {
11236 dw.focus_on_cu (cudie);
11237 dw.mod_info->get_symtab();
11238
11239 // look at each function to see if it's a tracepoint
11240 string function = "stapprobe_" + tracepoint;
11241 return dw.iterate_over_functions (tracepoint_query_func, this, function);
11242 }
11243
11244
11245 int
11246 tracepoint_query::handle_query_func(Dwarf_Die * func)
11247 {
11248 dw.focus_on_function (func);
11249
11250 assert(startswith(dw.function_name, "stapprobe_"));
11251 string tracepoint_instance = dw.function_name.substr(10);
11252
11253 // check for duplicates -- sometimes tracepoint headers may be indirectly
11254 // included in more than one of our tracequery modules.
11255 if (!probed_names.insert(tracepoint_instance).second)
11256 return DWARF_CB_OK;
11257
11258 // PR17126: blacklist
11259 if (!sess.guru_mode)
11260 {
11261 if ((sess.architecture.substr(0,3) == "ppc" ||
11262 sess.architecture.substr(0,7) == "powerpc") &&
11263 (tracepoint_instance == "hcall_entry" ||
11264 tracepoint_instance == "hcall_exit"))
11265 {
11266 sess.print_warning(_F("tracepoint %s is blacklisted on architecture %s",
11267 tracepoint_instance.c_str(), sess.architecture.c_str()));
11268 return DWARF_CB_OK;
11269 }
11270 }
11271
11272 derived_probe *dp = new tracepoint_derived_probe (dw.sess, dw, *func,
11273 current_system,
11274 tracepoint_instance,
11275 base_probe, base_loc);
11276 results.push_back (dp);
11277 return DWARF_CB_OK;
11278 }
11279
11280
11281 int
11282 tracepoint_query::tracepoint_query_cu (Dwarf_Die * cudie, tracepoint_query * q)
11283 {
11284 if (pending_interrupts) return DWARF_CB_ABORT;
11285 return q->handle_query_cu(cudie);
11286 }
11287
11288
11289 int
11290 tracepoint_query::tracepoint_query_func (Dwarf_Die * func, tracepoint_query * q)
11291 {
11292 if (pending_interrupts) return DWARF_CB_ABORT;
11293 return q->handle_query_func(func);
11294 }
11295
11296
11297 struct tracepoint_builder: public derived_probe_builder
11298 {
11299 private:
11300 dwflpp *dw;
11301 bool init_dw(systemtap_session& s);
11302 void get_tracequery_modules(systemtap_session& s,
11303 const vector<string>& headers,
11304 vector<string>& modules);
11305
11306 public:
11307
11308 tracepoint_builder(): dw(0) {}
11309 ~tracepoint_builder() { delete dw; }
11310
11311 void build_no_more (systemtap_session& s)
11312 {
11313 if (dw && s.verbose > 3)
11314 clog << _("tracepoint_builder releasing dwflpp") << endl;
11315 delete dw;
11316 dw = NULL;
11317
11318 delete_session_module_cache (s);
11319 }
11320
11321 void build(systemtap_session& s,
11322 probe *base, probe_point *location,
11323 literal_map_t const& parameters,
11324 vector<derived_probe*>& finished_results);
11325 };
11326
11327
11328
11329 // Create (or cache) one or more tracequery .o modules, based upon the
11330 // tracepoint-related header files given. Return the generated or cached
11331 // modules[].
11332
11333 void
11334 tracepoint_builder::get_tracequery_modules(systemtap_session& s,
11335 const vector<string>& headers,
11336 vector<string>& modules)
11337 {
11338 if (s.verbose > 2)
11339 {
11340 clog << _F("Pass 2: getting a tracepoint query for %zu headers: ", headers.size()) << endl;
11341 for (size_t i = 0; i < headers.size(); ++i)
11342 clog << " " << headers[i] << endl;
11343 }
11344
11345 map<string,string> headers_cache_obj; // header name -> cache/.../tracequery_hash.o file name
11346 // Map the headers to cache .o names. Note that this has side-effects of
11347 // creating the $SYSTEMTAP_DIR/.cache/XX/... directory and the hash-log file,
11348 // so we prefer not to repeat this.
11349 vector<string> uncached_headers;
11350 for (size_t i=0; i<headers.size(); i++)
11351 headers_cache_obj[headers[i]] = find_tracequery_hash(s, headers[i]);
11352
11353 // They may be in the cache already.
11354 if (s.use_cache && !s.poison_cache)
11355 for (size_t i=0; i<headers.size(); i++)
11356 {
11357 // see if the cached module exists
11358 const string& tracequery_path = headers_cache_obj[headers[i]];
11359 if (!tracequery_path.empty() && file_exists(tracequery_path))
11360 {
11361 if (s.verbose > 2)
11362 clog << _F("Pass 2: using cached %s", tracequery_path.c_str()) << endl;
11363
11364 // an empty file is a cached failure
11365 if (get_file_size(tracequery_path) > 0)
11366 modules.push_back (tracequery_path);
11367 }
11368 else
11369 uncached_headers.push_back(headers[i]);
11370 }
11371 else
11372 uncached_headers = headers;
11373
11374 // If we have nothing left to search for, quit
11375 if (uncached_headers.empty()) return;
11376
11377 map<string,string> headers_tracequery_src; // header -> C-source code mapping
11378
11379 // We could query several subsets of headers[] to make this go
11380 // faster, but let's KISS and do one at a time.
11381 for (size_t i=0; i<uncached_headers.size(); i++)
11382 {
11383 const string& header = uncached_headers[i];
11384
11385 // create a tracequery source file
11386 ostringstream osrc;
11387
11388 // PR9993: Add extra headers to work around undeclared types in individual
11389 // include/trace/foo.h files
11390 vector<string> short_decls = tracepoint_extra_decls(s, header);
11391
11392 // add each requested tracepoint header
11393 size_t root_pos = header.rfind("include/");
11394 short_decls.push_back(string("#include <") +
11395 ((root_pos != string::npos) ? header.substr(root_pos + 8) : header) +
11396 string(">"));
11397
11398 osrc << "#ifdef CONFIG_TRACEPOINTS" << endl;
11399 osrc << "#include <linux/tracepoint.h>" << endl;
11400
11401 // the kernel has changed this naming a few times, previously TPPROTO,
11402 // TP_PROTO, TPARGS, TP_ARGS, etc. so let's just dupe the latest.
11403 osrc << "#ifndef PARAMS" << endl;
11404 osrc << "#define PARAMS(args...) args" << endl;
11405 osrc << "#endif" << endl;
11406
11407 // override DECLARE_TRACE to synthesize probe functions for us
11408 osrc << "#undef DECLARE_TRACE" << endl;
11409 osrc << "#define DECLARE_TRACE(name, proto, args) \\" << endl;
11410 osrc << " void stapprobe_##name(proto) {}" << endl;
11411
11412 // 2.6.35 added the NOARGS variant, but it's the same for us
11413 osrc << "#undef DECLARE_TRACE_NOARGS" << endl;
11414 osrc << "#define DECLARE_TRACE_NOARGS(name) \\" << endl;
11415 osrc << " DECLARE_TRACE(name, void, )" << endl;
11416
11417 // 2.6.38 added the CONDITION variant, which can also just redirect
11418 osrc << "#undef DECLARE_TRACE_CONDITION" << endl;
11419 osrc << "#define DECLARE_TRACE_CONDITION(name, proto, args, cond) \\" << endl;
11420 osrc << " DECLARE_TRACE(name, PARAMS(proto), PARAMS(args))" << endl;
11421
11422 // older tracepoints used DEFINE_TRACE, so redirect that too
11423 osrc << "#undef DEFINE_TRACE" << endl;
11424 osrc << "#define DEFINE_TRACE(name, proto, args) \\" << endl;
11425 osrc << " DECLARE_TRACE(name, PARAMS(proto), PARAMS(args))" << endl;
11426
11427 // add the specified decls/#includes
11428 for (unsigned z=0; z<short_decls.size(); z++)
11429 osrc << "#undef TRACE_INCLUDE_FILE\n"
11430 << "#undef TRACE_INCLUDE_PATH\n"
11431 << short_decls[z] << "\n";
11432
11433 // create a section that will hold the TRACE_SYSTEM for this header
11434 osrc << "#ifdef TRACE_SYSTEM" << endl;
11435 osrc << "const char stap_trace_system[]" << endl;
11436 osrc << " __attribute__((section(\"" STAP_TRACE_SYSTEM "\")))" << endl;
11437 osrc << " = __stringify(TRACE_SYSTEM);" << endl;
11438 osrc << "#endif" << endl;
11439
11440 // finish up the module source
11441 osrc << "#endif /* CONFIG_TRACEPOINTS */" << endl;
11442
11443 // save the source file away
11444 headers_tracequery_src[header] = osrc.str();
11445 }
11446
11447 // now build them all together
11448 map<string,string> tracequery_objs = make_tracequeries(s, headers_tracequery_src);
11449
11450 // now extend the modules list, and maybe plop them into the cache
11451 for (size_t i=0; i<uncached_headers.size(); i++)
11452 {
11453 const string& header = uncached_headers[i];
11454 const string& tracequery_obj = tracequery_objs[header];
11455 const string& tracequery_path = headers_cache_obj[header];
11456 if (tracequery_obj !="" && file_exists(tracequery_obj))
11457 {
11458 modules.push_back (tracequery_obj);
11459 if (s.use_cache)
11460 copy_file(tracequery_obj, tracequery_path, s.verbose > 2);
11461 }
11462 else if (s.use_cache)
11463 // cache an empty file for failures
11464 copy_file("/dev/null", tracequery_path, s.verbose > 2);
11465 }
11466 }
11467
11468
11469
11470 bool
11471 tracepoint_builder::init_dw(systemtap_session& s)
11472 {
11473 if (dw != NULL)
11474 return true;
11475
11476 vector<string> tracequery_modules;
11477 vector<string> system_headers;
11478
11479 glob_t trace_glob;
11480
11481 // find kernel_source_tree from DW_AT_comp_dir
11482 if (s.kernel_source_tree == "")
11483 {
11484 unsigned found;
11485 Dwfl *dwfl = setup_dwfl_kernel ("kernel", &found, s);
11486 if (found)
11487 {
11488 Dwarf_Die *cudie = 0;
11489 Dwarf_Addr bias;
11490 while ((cudie = dwfl_nextcu (dwfl, cudie, &bias)) != NULL)
11491 {
11492 assert_no_interrupts();
11493 Dwarf_Attribute attr;
11494 const char* name = dwarf_formstring (dwarf_attr (cudie, DW_AT_comp_dir, &attr));
11495 if (name)
11496 {
11497 // Before we try to use it, check that the path actually
11498 // exists locally and is distinct from the build tree.
11499 if (!file_exists(name))
11500 {
11501 if (s.verbose > 2)
11502 clog << _F("Ignoring inaccessible kernel source tree (DW_AT_comp_dir) at '%s'", name) << endl;
11503 }
11504 else if (resolve_path(name) == resolve_path(s.kernel_build_tree))
11505 {
11506 if (s.verbose > 2)
11507 clog << _F("Ignoring duplicate kernel source tree (DW_AT_comp_dir) at '%s'", name) << endl;
11508 }
11509 else
11510 {
11511 if (s.verbose > 2)
11512 clog << _F("Located kernel source tree (DW_AT_comp_dir) at '%s'", name) << endl;
11513 s.kernel_source_tree = name;
11514 }
11515
11516 break; // skip others; modern Kbuild uses same comp_dir for them all
11517 }
11518 }
11519 }
11520 dwfl_end (dwfl);
11521 }
11522
11523 // find kernel_source_tree from a source link, when different from build
11524 if (s.kernel_source_tree == "" && endswith(s.kernel_build_tree, "/build"))
11525 {
11526 string source_tree = s.kernel_build_tree;
11527 source_tree.replace(source_tree.length() - 5, 5, "source");
11528 if (file_exists(source_tree) &&
11529 resolve_path(source_tree) != resolve_path(s.kernel_build_tree))
11530 {
11531 if (s.verbose > 2)
11532 clog << _F("Located kernel source tree at '%s'", source_tree.c_str()) << endl;
11533 s.kernel_source_tree = source_tree;
11534 }
11535 }
11536
11537 // prefixes
11538 vector<string> glob_prefixes;
11539 glob_prefixes.push_back (s.kernel_build_tree);
11540 if (s.kernel_source_tree != "")
11541 glob_prefixes.push_back (s.kernel_source_tree);
11542
11543 // suffixes
11544 vector<string> glob_suffixes;
11545 glob_suffixes.push_back("include/trace/events/*.h");
11546 glob_suffixes.push_back("include/trace/*.h");
11547 glob_suffixes.push_back("include/ras/*_event.h");
11548 glob_suffixes.push_back("arch/x86/kvm/*trace.h");
11549 glob_suffixes.push_back("arch/x86/kernel/*trace.h");
11550 glob_suffixes.push_back("arch/*/include/asm/trace*.h");
11551 glob_suffixes.push_back("arch/*/include/asm/trace/*.h");
11552 glob_suffixes.push_back("fs/xfs/linux-*/xfs_tr*.h");
11553 glob_suffixes.push_back("fs/*/*trace*.h");
11554 glob_suffixes.push_back("net/*/*trace*.h");
11555 glob_suffixes.push_back("sound/pci/hda/*_trace.h");
11556 glob_suffixes.push_back("drivers/gpu/drm/*_trace.h");
11557 glob_suffixes.push_back("drivers/gpu/drm/*/*_trace.h");
11558 glob_suffixes.push_back("drivers/net/wireless/*/*/*trace*.h");
11559 glob_suffixes.push_back("drivers/usb/host/*trace*.h");
11560
11561 // see also tracepoint_extra_decls above
11562
11563 // compute cartesian product
11564 vector<string> globs;
11565 for (unsigned i=0; i<glob_prefixes.size(); i++)
11566 for (unsigned j=0; j<glob_suffixes.size(); j++)
11567 globs.push_back (glob_prefixes[i]+string("/")+glob_suffixes[j]);
11568
11569 set<string> duped_headers;
11570 for (unsigned z = 0; z < globs.size(); z++)
11571 {
11572 string glob_str = globs[z];
11573 if (s.verbose > 3)
11574 clog << _("Checking tracepoint glob ") << glob_str << endl;
11575
11576 int r = glob(glob_str.c_str(), 0, NULL, &trace_glob);
11577 if (r == GLOB_NOSPACE || r == GLOB_ABORTED)
11578 throw runtime_error("Error globbing tracepoint");
11579
11580 for (unsigned i = 0; i < trace_glob.gl_pathc; ++i)
11581 {
11582 string header(trace_glob.gl_pathv[i]);
11583
11584 // filter out a few known "internal-only" headers
11585 if (endswith(header, "/define_trace.h") ||
11586 endswith(header, "/ftrace.h") ||
11587 endswith(header, "/trace_events.h") ||
11588 endswith(header, "_event_types.h"))
11589 continue;
11590
11591 // With headers now plopped under arch/FOO/include/asm/*,
11592 // the following logic miss some tracepoints.
11593 #if 0
11594 // skip identical headers from the build and source trees.
11595 size_t root_pos = header.rfind("include/");
11596 if (root_pos != string::npos &&
11597 !duped_headers.insert(header.substr(root_pos + 8)).second)
11598 continue;
11599 #endif
11600
11601 system_headers.push_back(header);
11602 }
11603 globfree(&trace_glob);
11604 }
11605
11606 // Build tracequery modules
11607 get_tracequery_modules(s, system_headers, tracequery_modules);
11608
11609 // TODO: consider other sources of tracepoint headers too, like from
11610 // a command-line parameter or some environment or .systemtaprc
11611
11612 dw = new dwflpp(s, tracequery_modules, true);
11613 return true;
11614 }
11615
11616 void
11617 tracepoint_builder::build(systemtap_session& s,
11618 probe *base, probe_point *location,
11619 literal_map_t const& parameters,
11620 vector<derived_probe*>& finished_results)
11621 {
11622 if (!init_dw(s))
11623 return;
11624
11625 interned_string tracepoint;
11626 assert(get_param (parameters, TOK_TRACE, tracepoint));
11627
11628 tracepoint_query q(*dw, tracepoint, base, location, finished_results);
11629 unsigned results_pre = finished_results.size();
11630 dw->iterate_over_modules<base_query>(&query_module, &q);
11631 unsigned results_post = finished_results.size();
11632
11633 // Did we fail to find a match? Let's suggest something!
11634 if (results_pre == results_post)
11635 {
11636 size_t pos;
11637 string sugs = suggest_dwarf_functions(s, q.visited_modules, tracepoint);
11638 while ((pos = sugs.find("stapprobe_")) != string::npos)
11639 sugs.erase(pos, string("stapprobe_").size());
11640 if (!sugs.empty())
11641 throw SEMANTIC_ERROR (_NF("no match (similar tracepoint: %s)",
11642 "no match (similar tracepoints: %s)",
11643 sugs.find(',') == string::npos,
11644 sugs.c_str()));
11645 }
11646 }
11647
11648
11649 // ------------------------------------------------------------------------
11650 // Standard tapset registry.
11651 // ------------------------------------------------------------------------
11652
11653 void
11654 register_standard_tapsets(systemtap_session & s)
11655 {
11656 register_tapset_been(s);
11657 register_tapset_itrace(s);
11658 register_tapset_mark(s);
11659 register_tapset_procfs(s);
11660 register_tapset_timers(s);
11661 register_tapset_netfilter(s);
11662 register_tapset_utrace(s);
11663
11664 // dwarf-based kprobe/uprobe parts
11665 dwarf_derived_probe::register_patterns(s);
11666
11667 // XXX: user-space starter set
11668 s.pattern_root->bind_num(TOK_PROCESS)
11669 ->bind_num(TOK_STATEMENT)->bind(TOK_ABSOLUTE)
11670 ->bind_privilege(pr_all)
11671 ->bind(new uprobe_builder ());
11672 s.pattern_root->bind_num(TOK_PROCESS)
11673 ->bind_num(TOK_STATEMENT)->bind(TOK_ABSOLUTE)->bind(TOK_RETURN)
11674 ->bind_privilege(pr_all)
11675 ->bind(new uprobe_builder ());
11676
11677 // kernel tracepoint probes
11678 s.pattern_root->bind(TOK_KERNEL)->bind_str(TOK_TRACE)
11679 ->bind(new tracepoint_builder());
11680
11681 // Kprobe based probe
11682 s.pattern_root->bind(TOK_KPROBE)->bind_str(TOK_FUNCTION)
11683 ->bind(new kprobe_builder());
11684 s.pattern_root->bind(TOK_KPROBE)->bind_str(TOK_FUNCTION)->bind(TOK_CALL)
11685 ->bind(new kprobe_builder());
11686 s.pattern_root->bind(TOK_KPROBE)->bind_str(TOK_MODULE)
11687 ->bind_str(TOK_FUNCTION)->bind(new kprobe_builder());
11688 s.pattern_root->bind(TOK_KPROBE)->bind_str(TOK_MODULE)
11689 ->bind_str(TOK_FUNCTION)->bind(TOK_CALL)->bind(new kprobe_builder());
11690 s.pattern_root->bind(TOK_KPROBE)->bind_str(TOK_FUNCTION)->bind(TOK_RETURN)
11691 ->bind(new kprobe_builder());
11692 s.pattern_root->bind(TOK_KPROBE)->bind_str(TOK_FUNCTION)->bind(TOK_RETURN)
11693 ->bind_num(TOK_MAXACTIVE)->bind(new kprobe_builder());
11694 s.pattern_root->bind(TOK_KPROBE)->bind_str(TOK_MODULE)
11695 ->bind_str(TOK_FUNCTION)->bind(TOK_RETURN)->bind(new kprobe_builder());
11696 s.pattern_root->bind(TOK_KPROBE)->bind_str(TOK_MODULE)
11697 ->bind_str(TOK_FUNCTION)->bind(TOK_RETURN)
11698 ->bind_num(TOK_MAXACTIVE)->bind(new kprobe_builder());
11699 s.pattern_root->bind(TOK_KPROBE)->bind_num(TOK_STATEMENT)
11700 ->bind(TOK_ABSOLUTE)->bind(new kprobe_builder());
11701
11702 //Hwbkpt based probe
11703 // NB: we formerly registered the probe point types only if the kernel configuration
11704 // allowed it. However, we get better error messages if we allow probes to resolve.
11705 s.pattern_root->bind(TOK_KERNEL)->bind_num(TOK_HWBKPT)
11706 ->bind(TOK_HWBKPT_WRITE)->bind(new hwbkpt_builder());
11707 s.pattern_root->bind(TOK_KERNEL)->bind_str(TOK_HWBKPT)
11708 ->bind(TOK_HWBKPT_WRITE)->bind(new hwbkpt_builder());
11709 s.pattern_root->bind(TOK_KERNEL)->bind_num(TOK_HWBKPT)
11710 ->bind(TOK_HWBKPT_RW)->bind(new hwbkpt_builder());
11711 s.pattern_root->bind(TOK_KERNEL)->bind_str(TOK_HWBKPT)
11712 ->bind(TOK_HWBKPT_RW)->bind(new hwbkpt_builder());
11713 s.pattern_root->bind(TOK_KERNEL)->bind_num(TOK_HWBKPT)
11714 ->bind_num(TOK_LENGTH)->bind(TOK_HWBKPT_WRITE)->bind(new hwbkpt_builder());
11715 s.pattern_root->bind(TOK_KERNEL)->bind_num(TOK_HWBKPT)
11716 ->bind_num(TOK_LENGTH)->bind(TOK_HWBKPT_RW)->bind(new hwbkpt_builder());
11717 // length supported with address only, not symbol names
11718
11719 //perf event based probe
11720 register_tapset_perf(s);
11721 register_tapset_java(s);
11722 }
11723
11724
11725 vector<derived_probe_group*>
11726 all_session_groups(systemtap_session& s)
11727 {
11728 vector<derived_probe_group*> g;
11729
11730 #define DOONE(x) \
11731 if (s. x##_derived_probes) \
11732 g.push_back ((derived_probe_group*)(s. x##_derived_probes))
11733
11734 // Note that order *is* important here. We want to make sure we
11735 // register (actually run) begin probes before any other probe type
11736 // is run. Similarly, when unregistering probes, we want to
11737 // unregister (actually run) end probes after every other probe type
11738 // has be unregistered. To do the latter,
11739 // c_unparser::emit_module_exit() will run this list backwards.
11740 DOONE(be);
11741 DOONE(dwarf);
11742 DOONE(uprobe);
11743 DOONE(timer);
11744 DOONE(profile);
11745 DOONE(mark);
11746 DOONE(tracepoint);
11747 DOONE(kprobe);
11748 DOONE(hwbkpt);
11749 DOONE(perf);
11750 DOONE(hrtimer);
11751 DOONE(procfs);
11752 DOONE(netfilter);
11753
11754 // Another "order is important" item. We want to make sure we
11755 // "register" the dummy task_finder probe group after all probe
11756 // groups that use the task_finder.
11757 DOONE(utrace);
11758 DOONE(itrace);
11759 DOONE(dynprobe);
11760 DOONE(task_finder);
11761 #undef DOONE
11762 return g;
11763 }
11764
11765 /* vim: set sw=2 ts=8 cino=>4,n-2,{2,^-2,t0,(0,u0,w1,M1 : */
This page took 0.637763 seconds and 5 git commands to generate.