]> sourceware.org Git - systemtap.git/blame - tapsets.cxx
buildrun.cxx: adapt to kernel 5.4+
[systemtap.git] / tapsets.cxx
CommitLineData
56e12059 1// tapset resolution
82fc46ce 2// Copyright (C) 2005-2019 Red Hat Inc.
aa30ccd3 3// Copyright (C) 2005-2007 Intel Corporation.
0b8f6579 4// Copyright (C) 2008 James.Bottomley@HansenPartnership.com
56e12059
FCE
5//
6// This file is part of systemtap, and is free software. You can
7// redistribute it and/or modify it under the terms of the GNU General
8// Public License (GPL); either version 2, or (at your option) any
9// later version.
10
11#include "config.h"
12#include "staptree.h"
13#include "elaborate.h"
b55bc428 14#include "tapsets.h"
93646f4d 15#include "task_finder.h"
f31a77f5 16#include "tapset-dynprobe.h"
56e12059 17#include "translate.h"
dc38c0ae 18#include "session.h"
72dbc915 19#include "util.h"
0a6f5a3f 20#include "buildrun.h"
86bf665e 21#include "dwarf_wrappers.h"
b278033a 22#include "hash.h"
440f755a 23#include "dwflpp.h"
5f8ca04f 24#include "setupdwfl.h"
0dbac951 25#include "loc2stap.h"
6b51ee12 26#include <gelf.h>
22d737e8 27
4d0d9e8a 28#include "sdt_types.h"
a3e980f9 29#include "stringtable.h"
bd2b1e68 30
3b579393
FCE
31#include <cstdlib>
32#include <algorithm>
bd2b1e68 33#include <deque>
56e12059 34#include <iostream>
d1bcbe71 35#include <fstream>
bd2b1e68 36#include <map>
ec4373ff 37#include <set>
56e12059 38#include <sstream>
bd2b1e68 39#include <stdexcept>
b55bc428 40#include <vector>
67959c62 41#include <stack>
e36387d7 42#include <cstdarg>
29e64872 43#include <cassert>
1969b5bc 44#include <iomanip>
f781f849 45#include <cerrno>
bd2b1e68
GH
46
47extern "C" {
df8fadee 48#include <fcntl.h>
bd2b1e68 49#include <elfutils/libdwfl.h>
7a053d3b 50#include <elfutils/libdw.h>
77de5e9e
GH
51#include <dwarf.h>
52#include <elf.h>
53#include <obstack.h>
b20febf3 54#include <glob.h>
30a279be 55#include <fnmatch.h>
5f0a03a6 56#include <stdio.h>
349dc70e 57#include <sys/types.h>
37001baa 58#include <sys/stat.h>
aaf7ffe8 59#include <math.h>
aff5d390 60#include <regex.h>
37001baa 61#include <unistd.h>
4b1ad75e
RM
62
63#define __STDC_FORMAT_MACROS
64#include <inttypes.h>
bd2b1e68 65}
77de5e9e 66
56e12059 67using namespace std;
2171f774 68using namespace __gnu_cxx;
56e12059 69
b4c6a4b1
HK
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
b20febf3
FCE
81
82// ------------------------------------------------------------------------
faea5e16
JS
83
84string
85common_probe_init (derived_probe* p)
86{
26e63673
JS
87 assert(p->session_index != (unsigned)-1);
88 return "(&stap_probes[" + lex_cast(p->session_index) + "])";
faea5e16
JS
89}
90
91
b20febf3 92void
71db462b
DS
93common_probe_entryfn_prologue (systemtap_session& s,
94 string statestr, string probe,
285ca427
DS
95 string probe_type, bool overload_processing,
96 void (*declaration_callback)(systemtap_session& s, void *data),
97 void (*pre_context_callback)(systemtap_session& s, void *data),
98 void *callback_data)
b20febf3 99{
944c9a7a 100 if (s.runtime_usermode_p())
ac34f2aa
JS
101 {
102 // If session_state() is NULL, then we haven't even initialized shm yet,
103 // and there's *nothing* for the probe to do. (even alibi is in shm)
104 // So failure skips this whole block through the end of the epilogue.
105 s.op->newline() << "if (likely(session_state())) {";
e481ab2e 106 s.op->indent(1);
ac34f2aa
JS
107 }
108
71db462b 109 s.op->newline() << "#ifdef STP_ALIBI";
7c3e97f4 110 s.op->newline() << "atomic_inc(probe_alibi(" << probe << "->index));";
71db462b 111 s.op->newline() << "#else";
653e6a9a 112
e481ab2e
JS
113 if (s.runtime_usermode_p())
114 s.op->newline() << "int _stp_saved_errno = errno;";
115
fee09ad5 116 s.op->newline() << "struct context* __restrict__ c = NULL;";
71db462b
DS
117 s.op->newline() << "#if !INTERRUPTIBLE";
118 s.op->newline() << "unsigned long flags;";
119 s.op->newline() << "#endif";
b20febf3 120
f887a8c9 121 s.op->newline() << "#ifdef STP_TIMING";
7c3e97f4 122 s.op->newline() << "Stat stat = probe_timing(" << probe << "->index);";
f887a8c9 123 s.op->newline() << "#endif";
285ca427
DS
124 if (declaration_callback)
125 declaration_callback(s, callback_data);
f887a8c9 126 if (overload_processing && !s.runtime_usermode_p())
71db462b 127 s.op->newline() << "#if defined(STP_TIMING) || defined(STP_OVERLOAD)";
a58d79d0 128 else
71db462b 129 s.op->newline() << "#ifdef STP_TIMING";
b20febf3 130
f887a8c9
DS
131 if (! s.runtime_usermode_p())
132 s.op->newline() << "cycles_t cycles_atstart = get_cycles ();";
133 else
134 {
135 s.op->newline() << "struct timespec timespec_atstart;";
45639d96 136 s.op->newline() << "(void)clock_gettime(CLOCK_MONOTONIC, &timespec_atstart);";
f887a8c9 137 }
71db462b 138 s.op->newline() << "#endif";
994aac0e 139
d2d39de6 140 s.op->newline() << "#if !INTERRUPTIBLE";
285ca427
DS
141 if (pre_context_callback)
142 pre_context_callback(s, callback_data);
71db462b
DS
143 s.op->newline() << "local_irq_save (flags);";
144 s.op->newline() << "#endif";
b20febf3 145
ac3af990 146 if (! s.runtime_usermode_p())
71db462b
DS
147 {
148 // Check for enough free enough stack space
149 s.op->newline() << "if (unlikely ((((unsigned long) (& c)) & (THREAD_SIZE-1))"; // free space
150 s.op->newline(1) << "< (MINSTACKSPACE + sizeof (struct thread_info)))) {"; // needed space
151 // XXX: may need porting to platforms where task_struct is not
152 // at bottom of kernel stack NB: see also
153 // CONFIG_DEBUG_STACKOVERFLOW
065d5567 154 s.op->newline() << "atomic_inc (skipped_count());";
71db462b 155 s.op->newline() << "#ifdef STP_TIMING";
065d5567 156 s.op->newline() << "atomic_inc (skipped_count_lowstack());";
71db462b
DS
157 s.op->newline() << "#endif";
158 s.op->newline() << "goto probe_epilogue;";
159 s.op->newline(-1) << "}";
160 }
c931ec8a 161
065d5567 162 s.op->newline() << "if (atomic_read (session_state()) != " << statestr << ")";
71db462b
DS
163 s.op->newline(1) << "goto probe_epilogue;";
164 s.op->indent(-1);
9a604fac 165
285ca427
DS
166 if (pre_context_callback)
167 {
168 s.op->newline() << "#if INTERRUPTIBLE";
169 pre_context_callback(s, callback_data);
170 s.op->newline() << "#endif";
171 }
1d0e697d 172 s.op->newline() << "c = _stp_runtime_entryfn_get_context();";
d2d39de6 173 s.op->newline() << "if (!c) {";
71db462b 174 s.op->newline(1) << "#if !INTERRUPTIBLE";
065d5567 175 s.op->newline() << "atomic_inc (skipped_count());";
71db462b
DS
176 s.op->newline() << "#endif";
177 s.op->newline() << "#ifdef STP_TIMING";
065d5567 178 s.op->newline() << "atomic_inc (skipped_count_reentrant());";
71db462b 179 s.op->newline() << "#endif";
71db462b
DS
180 s.op->newline() << "goto probe_epilogue;";
181 s.op->newline(-1) << "}";
d2d39de6 182
71db462b 183 s.op->newline();
41f4efb6 184 s.op->newline() << "c->aborted = 0;";
71db462b
DS
185 s.op->newline() << "c->last_stmt = 0;";
186 s.op->newline() << "c->last_error = 0;";
187 s.op->newline() << "c->nesting = -1;"; // NB: PR10516 packs locals[] tighter
188 s.op->newline() << "c->uregs = 0;";
189 s.op->newline() << "c->kregs = 0;";
5826dc35 190 s.op->newline() << "c->sregs = 0;";
71db462b
DS
191 s.op->newline() << "#if defined __ia64__";
192 s.op->newline() << "c->unwaddr = 0;";
193 s.op->newline() << "#endif";
f4d70a33
JS
194 if (s.runtime_usermode_p())
195 s.op->newline() << "c->probe_index = " << probe << "->index;";
71db462b
DS
196 s.op->newline() << "c->probe_point = " << probe << "->pp;";
197 s.op->newline() << "#ifdef STP_NEED_PROBE_NAME";
198 s.op->newline() << "c->probe_name = " << probe << "->pn;";
199 s.op->newline() << "#endif";
200 s.op->newline() << "c->probe_type = " << probe_type << ";";
6dceb5c9 201 // reset Individual Probe State union
71db462b 202 s.op->newline() << "memset(&c->ips, 0, sizeof(c->ips));";
5826dc35 203 s.op->newline() << "c->user_mode_p = 0; c->full_uregs_p = 0; ";
71db462b
DS
204 s.op->newline() << "#ifdef STAP_NEED_REGPARM"; // i386 or x86_64 register.stp
205 s.op->newline() << "c->regparm = 0;";
206 s.op->newline() << "#endif";
e0a17418 207
152fa051
LB
208 if(!s.suppress_time_limits){
209 s.op->newline() << "#if INTERRUPTIBLE";
210 s.op->newline() << "c->actionremaining = MAXACTION_INTERRUPTIBLE;";
211 s.op->newline() << "#else";
212 s.op->newline() << "c->actionremaining = MAXACTION;";
213 s.op->newline() << "#endif";
214 }
9915575b
FCE
215 // NB: The following would actually be incorrect.
216 // That's because cycles_sum/cycles_base values are supposed to survive
217 // between consecutive probes. Periodically (STP_OVERLOAD_INTERVAL
218 // cycles), the values will be reset.
219 /*
71db462b
DS
220 s.op->newline() << "#ifdef STP_OVERLOAD";
221 s.op->newline() << "c->cycles_sum = 0;";
222 s.op->newline() << "c->cycles_base = 0;";
223 s.op->newline() << "#endif";
9915575b 224 */
44cfbe25
SM
225
226 s.op->newline() << "#if defined(STP_NEED_UNWIND_DATA)";
227 s.op->newline() << "c->uwcache_user.state = uwcache_uninitialized;";
228 s.op->newline() << "c->uwcache_kernel.state = uwcache_uninitialized;";
229 s.op->newline() << "#endif";
2a8bc65a
FCE
230
231 s.op->newline() << "#if defined(STAP_NEED_CONTEXT_RETURNVAL)";
232 s.op->newline() << "c->returnval_override_p = 0;";
233 s.op->newline() << "c->returnval_override = 0;"; // unnecessary
234 s.op->newline() << "#endif";
b20febf3 235}
9a604fac 236
a44a0785 237
b20febf3 238void
f887a8c9 239common_probe_entryfn_epilogue (systemtap_session& s,
ef1337ee
JL
240 bool overload_processing,
241 bool schedule_work_safe)
b20febf3 242{
ef1337ee
JL
243 if (!s.runtime_usermode_p()
244 && schedule_work_safe)
245 {
246 // If a refresh is required, we can safely schedule_work() here
247 s.op->newline( 0) << "if (atomic_cmpxchg(&need_module_refresh, 1, 0) == 1)";
248 s.op->newline(+1) << "schedule_work(&module_refresher_work);";
249 s.op->indent(-1);
250 }
251
f887a8c9
DS
252 if (overload_processing && !s.runtime_usermode_p())
253 s.op->newline() << "#if defined(STP_TIMING) || defined(STP_OVERLOAD)";
a58d79d0 254 else
f887a8c9
DS
255 s.op->newline() << "#ifdef STP_TIMING";
256 s.op->newline() << "{";
257 s.op->indent(1);
258 if (! s.runtime_usermode_p())
259 {
260 s.op->newline() << "cycles_t cycles_atend = get_cycles ();";
261 // NB: we truncate cycles counts to 32 bits. Perhaps it should be
262 // fewer, if the hardware counter rolls over really quickly. We
263 // handle 32-bit wraparound here.
264 s.op->newline() << "int32_t cycles_elapsed = ((int32_t)cycles_atend > (int32_t)cycles_atstart)";
265 s.op->newline(1) << "? ((int32_t)cycles_atend - (int32_t)cycles_atstart)";
266 s.op->newline() << ": (~(int32_t)0) - (int32_t)cycles_atstart + (int32_t)cycles_atend + 1;";
267 s.op->indent(-1);
268 }
269 else
270 {
271 s.op->newline() << "struct timespec timespec_atend, timespec_elapsed;";
272 s.op->newline() << "long cycles_elapsed;";
45639d96 273 s.op->newline() << "(void)clock_gettime(CLOCK_MONOTONIC, &timespec_atend);";
f887a8c9
DS
274 s.op->newline() << "_stp_timespec_sub(&timespec_atend, &timespec_atstart, &timespec_elapsed);";
275 // 'cycles_elapsed' is really elapsed nanoseconds
276 s.op->newline() << "cycles_elapsed = (timespec_elapsed.tv_sec * NSEC_PER_SEC) + timespec_elapsed.tv_nsec;";
277 }
278
279 s.op->newline() << "#ifdef STP_TIMING";
26382d61
MC
280 // STP_TIMING requires min, max, avg (and thus count and sum), but not variance.
281 s.op->newline() << "if (likely (stat)) _stp_stat_add(stat, cycles_elapsed, 1, 1, 1, 1, 0);";
f887a8c9
DS
282 s.op->newline() << "#endif";
283
284 if (overload_processing && !s.runtime_usermode_p())
285 {
286 s.op->newline() << "#ifdef STP_OVERLOAD";
287 s.op->newline() << "{";
a58d79d0
DS
288 // If the cycle count has wrapped (cycles_atend > cycles_base),
289 // let's go ahead and pretend the interval has been reached.
290 // This should reset cycles_base and cycles_sum.
f887a8c9
DS
291 s.op->newline(1) << "cycles_t interval = (cycles_atend > c->cycles_base)";
292 s.op->newline(1) << "? (cycles_atend - c->cycles_base)";
293 s.op->newline() << ": (STP_OVERLOAD_INTERVAL + 1);";
294 s.op->newline(-1) << "c->cycles_sum += cycles_elapsed;";
a58d79d0
DS
295
296 // If we've spent more than STP_OVERLOAD_THRESHOLD cycles in a
297 // probe during the last STP_OVERLOAD_INTERVAL cycles, the probe
298 // has overloaded the system and we need to quit.
7baf48e9
FCE
299 // NB: this is not suppressible via --suppress-runtime-errors,
300 // because this is a system safety metric that we cannot trust
301 // unprivileged users to override.
f887a8c9
DS
302 s.op->newline() << "if (interval > STP_OVERLOAD_INTERVAL) {";
303 s.op->newline(1) << "if (c->cycles_sum > STP_OVERLOAD_THRESHOLD) {";
d95f8163 304 s.op->newline(1) << "_stp_error (\"probe overhead (%lld cycles) exceeded threshold (%lld cycles) in last"
10b3f049 305 " %lld cycles\", (long long) c->cycles_sum, STP_OVERLOAD_THRESHOLD, STP_OVERLOAD_INTERVAL);";
065d5567
JS
306 s.op->newline() << "atomic_set (session_state(), STAP_SESSION_ERROR);";
307 s.op->newline() << "atomic_inc (error_count());";
f887a8c9
DS
308 s.op->newline(-1) << "}";
309
310 s.op->newline() << "c->cycles_base = cycles_atend;";
311 s.op->newline() << "c->cycles_sum = 0;";
312 s.op->newline(-1) << "}";
313 s.op->newline(-1) << "}";
314 s.op->newline() << "#endif";
a58d79d0 315 }
e57b735a 316
f887a8c9
DS
317 s.op->newline(-1) << "}";
318 s.op->newline() << "#endif";
e57b735a 319
f887a8c9
DS
320 s.op->newline() << "c->probe_point = 0;"; // vacated
321 s.op->newline() << "#ifdef STP_NEED_PROBE_NAME";
322 s.op->newline() << "c->probe_name = 0;";
323 s.op->newline() << "#endif";
324 s.op->newline() << "c->probe_type = 0;";
7baf48e9
FCE
325
326
1511b60c 327 s.op->newline() << "if (unlikely (c->last_error)) {";
f887a8c9
DS
328 s.op->indent(1);
329 if (s.suppress_handler_errors) // PR 13306
b78a0fbb 330 {
065d5567 331 s.op->newline() << "atomic_inc (error_count());";
7baf48e9
FCE
332 }
333 else
334 {
f887a8c9
DS
335 s.op->newline() << "if (c->last_stmt != NULL)";
336 s.op->newline(1) << "_stp_softerror (\"%s near %s\", c->last_error, c->last_stmt);";
337 s.op->newline(-1) << "else";
338 s.op->newline(1) << "_stp_softerror (\"%s\", c->last_error);";
339 s.op->indent(-1);
065d5567
JS
340 s.op->newline() << "atomic_inc (error_count());";
341 s.op->newline() << "if (atomic_read (error_count()) > MAXERRORS) {";
342 s.op->newline(1) << "atomic_set (session_state(), STAP_SESSION_ERROR);";
f887a8c9
DS
343 s.op->newline() << "_stp_exit ();";
344 s.op->newline(-1) << "}";
7baf48e9
FCE
345 }
346
f887a8c9 347 s.op->newline(-1) << "}";
7baf48e9
FCE
348
349
f887a8c9
DS
350 s.op->newline(-1) << "probe_epilogue:"; // context is free
351 s.op->indent(1);
e57b735a 352
f887a8c9 353 if (! s.suppress_handler_errors) // PR 13306
7baf48e9
FCE
354 {
355 // Check for excessive skip counts.
065d5567
JS
356 s.op->newline() << "if (unlikely (atomic_read (skipped_count()) > MAXSKIPPED)) {";
357 s.op->newline(1) << "if (unlikely (pseudo_atomic_cmpxchg(session_state(), STAP_SESSION_RUNNING, STAP_SESSION_ERROR) == STAP_SESSION_RUNNING))";
f887a8c9
DS
358 s.op->newline() << "_stp_error (\"Skipped too many probes, check MAXSKIPPED or try again with stap -t for more details.\");";
359 s.op->newline(-1) << "}";
7baf48e9 360 }
e57b735a 361
d2d39de6
JS
362 // We mustn't release the context until after all _stp_error(), so dyninst
363 // mode can still access the log buffers stored therein.
364 s.op->newline() << "_stp_runtime_entryfn_put_context(c);";
365
366 s.op->newline() << "#if !INTERRUPTIBLE";
f887a8c9
DS
367 s.op->newline() << "local_irq_restore (flags);";
368 s.op->newline() << "#endif";
653e6a9a 369
944c9a7a 370 if (s.runtime_usermode_p())
ac34f2aa
JS
371 {
372 s.op->newline() << "errno = _stp_saved_errno;";
ac34f2aa 373 }
e481ab2e
JS
374
375 s.op->newline() << "#endif // STP_ALIBI";
376
377 if (s.runtime_usermode_p())
378 s.op->newline(-1) << "}";
440f755a 379}
e57b735a 380
e57b735a 381
440f755a 382// ------------------------------------------------------------------------
e57b735a 383
a7fbce1f
DS
384// ------------------------------------------------------------------------
385// kprobes (both dwarf based and non-dwarf based) probes
386// ------------------------------------------------------------------------
387
388
389struct generic_kprobe_derived_probe: public derived_probe
390{
391 generic_kprobe_derived_probe(probe *base,
392 probe_point *location,
393 interned_string module,
394 interned_string section,
395 Dwarf_Addr addr,
396 bool has_return,
397 bool has_maxactive = false,
398 int64_t maxactive_val = 0,
399 interned_string symbol_name = "",
400 Dwarf_Addr offset = 0);
401
402 virtual void join_group(systemtap_session&) = 0;
403
404 interned_string module;
405 interned_string section;
406 Dwarf_Addr addr;
407 bool has_return;
408 bool has_maxactive;
409 int64_t maxactive_val;
410
411 // PR18889: For modules, we have to probe using "symbol+offset"
412 // instead of using an address, otherwise we can't probe the init
413 // section. 'symbol_name' is the closest known symbol to 'addr' and
414 // 'offset' is the offset from the symbol.
415 interned_string symbol_name;
416 Dwarf_Addr offset;
417
418 unsigned saved_longs, saved_strings;
419 generic_kprobe_derived_probe* entry_handler;
0dbac951
RH
420
421 std::string args_for_bpf() const;
cbc16973 422 interned_string sym_name_for_bpf;
a7fbce1f
DS
423};
424
425generic_kprobe_derived_probe::generic_kprobe_derived_probe(probe *base,
426 probe_point *location,
427 interned_string module,
428 interned_string section,
429 Dwarf_Addr addr,
430 bool has_return,
431 bool has_maxactive,
432 int64_t maxactive_val,
433 interned_string symbol_name,
434 Dwarf_Addr offset) :
435 derived_probe (base, location, true /* .components soon rewritten */ ),
436 module(module), section(section), addr(addr), has_return(has_return),
437 has_maxactive(has_maxactive), maxactive_val(maxactive_val),
438 symbol_name(symbol_name), offset(offset),
439 saved_longs(0), saved_strings(0), entry_handler(0)
440{
441}
442
440f755a
JS
443// ------------------------------------------------------------------------
444// Dwarf derived probes. "We apologize for the inconvience."
445// ------------------------------------------------------------------------
e57b735a 446
4627ed58
JS
447static const string TOK_KERNEL("kernel");
448static const string TOK_MODULE("module");
449static const string TOK_FUNCTION("function");
450static const string TOK_INLINE("inline");
451static const string TOK_CALL("call");
4bda987e 452static const string TOK_EXPORTED("exported");
4627ed58
JS
453static const string TOK_RETURN("return");
454static const string TOK_MAXACTIVE("maxactive");
455static const string TOK_STATEMENT("statement");
456static const string TOK_ABSOLUTE("absolute");
457static const string TOK_PROCESS("process");
a794dbeb 458static const string TOK_PROVIDER("provider");
4627ed58
JS
459static const string TOK_MARK("mark");
460static const string TOK_TRACE("trace");
461static const string TOK_LABEL("label");
63b4fd14 462static const string TOK_LIBRARY("library");
576eaefe 463static const string TOK_PLT("plt");
e7d4410d
LB
464static const string TOK_METHOD("method");
465static const string TOK_CLASS("class");;
c31add50
JL
466static const string TOK_CALLEE("callee");;
467static const string TOK_CALLEES("callees");;
83eeb3ac 468static const string TOK_NEAREST("nearest");;
e57b735a 469
440f755a
JS
470// Can we handle this query with just symbol-table info?
471enum dbinfo_reqt
472{
473 dbr_unknown,
474 dbr_none, // kernel.statement(NUM).absolute
475 dbr_need_symtab, // can get by with symbol table if there's no dwarf
476 dbr_need_dwarf
477};
e57b735a 478
20e4a32c 479
673d0add 480struct dwarf_query; // forward decl
20e4a32c 481
45582cbc
JL
482static int query_cu (Dwarf_Die * cudie, dwarf_query *q);
483static void query_addr(Dwarf_Addr addr, dwarf_query *q);
484static void query_plt_statement(dwarf_query *q);
a781f401 485
440f755a
JS
486struct
487symbol_table
488{
489 module_info *mod_info; // associated module
45a63356 490 unordered_multimap<interned_string, func_info*> map_by_name;
1c6b77e5 491 multimap<Dwarf_Addr, func_info*> map_by_addr;
45a63356
FCE
492 unordered_map<interned_string, Dwarf_Addr> globals;
493 unordered_map<interned_string, Dwarf_Addr> locals;
064a90a9
MW
494 // Section describing function descriptors.
495 // Set to SHN_UNDEF if there is no such section.
440f755a 496 GElf_Word opd_section;
45a63356 497 void add_symbol(interned_string name, bool weak, bool descriptor,
61218615 498 Dwarf_Addr addr, Dwarf_Addr entrypc);
440f755a
JS
499 enum info_status get_from_elf();
500 void prepare_section_rejection(Dwfl_Module *mod);
501 bool reject_section(GElf_Word section);
440f755a 502 void purge_syscall_stubs();
45a63356
FCE
503 set <func_info*> lookup_symbol(interned_string name);
504 set <Dwarf_Addr> lookup_symbol_address(interned_string name);
440f755a 505 func_info *get_func_containing_address(Dwarf_Addr addr);
3d372d6b 506 func_info *get_first_func();
7a053d3b 507
b3104a15 508 symbol_table(module_info *mi) : mod_info(mi), opd_section(SHN_UNDEF) {}
440f755a
JS
509 ~symbol_table();
510};
77de5e9e 511
440f755a
JS
512static bool null_die(Dwarf_Die *die)
513{
822a6a3d 514 static Dwarf_Die null;
440f755a
JS
515 return (!die || !memcmp(die, &null, sizeof(null)));
516}
c4ce66a1
JS
517
518
7a053d3b 519enum
bd2b1e68 520function_spec_type
7a053d3b 521 {
bd2b1e68
GH
522 function_alone,
523 function_and_file,
7a053d3b 524 function_file_and_line
bd2b1e68
GH
525 };
526
ec4373ff 527
bd2b1e68 528struct dwarf_builder;
f10534c6 529struct dwarf_var_expanding_visitor;
77de5e9e 530
2930abc7 531
b20febf3
FCE
532// XXX: This class is a candidate for subclassing to separate
533// the relocation vs non-relocation variants. Likewise for
534// kprobe vs kretprobe variants.
535
a7fbce1f 536struct dwarf_derived_probe: public generic_kprobe_derived_probe
b55bc428 537{
45a63356
FCE
538 dwarf_derived_probe (interned_string function,
539 interned_string filename,
b20febf3 540 int line,
45a63356
FCE
541 interned_string module,
542 interned_string section,
b20febf3 543 Dwarf_Addr dwfl_addr,
2930abc7 544 Dwarf_Addr addr,
b20febf3 545 dwarf_query & q,
24897818
DS
546 Dwarf_Die* scope_die,
547 interned_string symbol_name = "",
548 Dwarf_Addr offset = 0);
20e4a32c 549
45a63356 550 interned_string path;
27dc09b1 551 bool has_process;
63b4fd14 552 bool has_library;
a7fbce1f 553 // generic_kprobe_derived_probe_group::emit_module_decls uses this to emit sdt kprobe definition
45a63356
FCE
554 interned_string user_path;
555 interned_string user_lib;
b95e2b79 556 bool access_vars;
2930abc7 557
da1fa82f 558 void printsig (std::ostream &o, bool nest=true) const;
6b66b9f7 559 virtual void join_group (systemtap_session& s);
3689db05 560 void emit_probe_local_init(systemtap_session& s, translator_output * o);
d0bfd2ac 561 void getargs(std::list<std::string> &arg_set) const;
0a98fd42 562
42e38653 563 void emit_privilege_assertion (translator_output*);
27dc09b1
DB
564 void print_dupe_stamp(ostream& o);
565
bd2b1e68 566 // Pattern registration helpers.
7a053d3b 567 static void register_statement_variants(match_node * root,
27dc09b1 568 dwarf_builder * dw,
42e38653 569 privilege_t privilege);
fd6602a0 570 static void register_function_variants(match_node * root,
27dc09b1 571 dwarf_builder * dw,
42e38653 572 privilege_t privilege);
440d9b00
DB
573 static void register_function_and_statement_variants(systemtap_session& s,
574 match_node * root,
27dc09b1 575 dwarf_builder * dw,
42e38653 576 privilege_t privilege);
b1615c74
JS
577 static void register_sdt_variants(systemtap_session& s,
578 match_node * root,
579 dwarf_builder * dw);
580 static void register_plt_variants(systemtap_session& s,
581 match_node * root,
582 dwarf_builder * dw);
c4ce66a1 583 static void register_patterns(systemtap_session& s);
6b66b9f7
JS
584
585protected:
586 dwarf_derived_probe(probe *base,
587 probe_point *location,
588 Dwarf_Addr addr,
589 bool has_return):
a7fbce1f
DS
590 generic_kprobe_derived_probe(base, location, "", "", addr, has_return),
591 has_process(0), has_library(0),
592 access_vars(false)
6b66b9f7
JS
593 {}
594
595private:
d0bfd2ac 596 list<string> args;
8c67c337 597 void saveargs(dwarf_query& q, Dwarf_Die* scope_die, Dwarf_Addr dwfl_addr);
20c6c071
GH
598};
599
dc38c0ae 600
6b66b9f7 601struct uprobe_derived_probe: public dwarf_derived_probe
6d0f3f0c 602{
6d0f3f0c 603 int pid; // 0 => unrestricted
0973d815 604
45a63356
FCE
605 uprobe_derived_probe (interned_string function,
606 interned_string filename,
6d0f3f0c 607 int line,
45a63356
FCE
608 interned_string module,
609 interned_string section,
6d0f3f0c
FCE
610 Dwarf_Addr dwfl_addr,
611 Dwarf_Addr addr,
612 dwarf_query & q,
d11f62b7 613 Dwarf_Die* scope_die);
6d0f3f0c 614
0973d815
FCE
615 // alternate constructor for process(PID).statement(ADDR).absolute
616 uprobe_derived_probe (probe *base,
617 probe_point *location,
618 int pid,
619 Dwarf_Addr addr,
6b66b9f7
JS
620 bool has_return):
621 dwarf_derived_probe(base, location, addr, has_return), pid(pid)
622 {}
9ace370f 623
6d0f3f0c 624 void join_group (systemtap_session& s);
2865d17a 625
42e38653 626 void emit_privilege_assertion (translator_output*);
8f6d8c2b 627 void print_dupe_stamp(ostream& o) { print_dupe_stamp_unprivileged_process_owner (o); }
c0f84e7b
SC
628 void getargs(std::list<std::string> &arg_set) const;
629 void saveargs(int nargs);
285ca427 630 void emit_perf_read_handler(systemtap_session& s, unsigned i);
3530e7a7 631
c0f84e7b
SC
632private:
633 list<string> args;
6d0f3f0c
FCE
634};
635
a7fbce1f 636struct generic_kprobe_derived_probe_group: public derived_probe_group
dc38c0ae 637{
fcdd71ba
WC
638 friend bool sort_for_bpf(systemtap_session& s,
639 generic_kprobe_derived_probe_group *ge,
0dbac951
RH
640 sort_for_bpf_probe_arg_vector &v);
641
dc38c0ae 642private:
a7fbce1f 643 unordered_multimap<interned_string,generic_kprobe_derived_probe*> probes_by_module;
dc38c0ae
DS
644
645public:
a7fbce1f
DS
646 generic_kprobe_derived_probe_group() {}
647 void enroll (generic_kprobe_derived_probe* probe);
b20febf3
FCE
648 void emit_module_decls (systemtap_session& s);
649 void emit_module_init (systemtap_session& s);
b4be7cbc 650 void emit_module_refresh (systemtap_session& s);
b20febf3 651 void emit_module_exit (systemtap_session& s);
dabd71bb 652 bool otf_supported (systemtap_session&) { return true; }
ca6d3b0f
JL
653
654 // workqueue handling not safe in kprobes context
dabd71bb 655 bool otf_safe_context (systemtap_session&) { return false; }
dc38c0ae
DS
656};
657
20c6c071 658// Helper struct to thread through the dwfl callbacks.
2c384610 659struct base_query
20c6c071 660{
c4ce66a1 661 base_query(dwflpp & dw, literal_map_t const & params);
46ccda7c 662 base_query(dwflpp & dw, interned_string module_val);
2c384610 663 virtual ~base_query() {}
bd2b1e68 664
5227f1ea 665 systemtap_session & sess;
2c384610 666 dwflpp & dw;
5227f1ea 667
070764c0
JL
668 // Used to keep track of which modules were visited during
669 // iterate_over_modules()
670 set<string> visited_modules;
671
bd2b1e68 672 // Parameter extractors.
86bf665e 673 static bool has_null_param(literal_map_t const & params,
45a63356 674 interned_string k);
86bf665e 675 static bool get_string_param(literal_map_t const & params,
45a63356 676 interned_string k, interned_string &v);
86bf665e 677 static bool get_number_param(literal_map_t const & params,
b3fbdd3e 678 interned_string k, int64_t & v);
86bf665e 679 static bool get_number_param(literal_map_t const & params,
45a63356 680 interned_string k, Dwarf_Addr & v);
bbbc7241 681 static void query_library_callback (base_query *me, const char *data);
45cdb40e 682 static void query_plt_callback (base_query *me, const char *link, size_t addr);
f301a9ff 683 virtual void query_library (const char *data) = 0;
576eaefe 684 virtual void query_plt (const char *link, size_t addr) = 0;
84c84ac4 685
b55bc428 686
2c384610
DS
687 // Extracted parameters.
688 bool has_kernel;
91af0778
FCE
689 bool has_module;
690 bool has_process;
63b4fd14 691 bool has_library;
576eaefe
SC
692 bool has_plt;
693 bool has_statement;
45a63356
FCE
694 interned_string module_val; // has_kernel => module_val = "kernel"
695 interned_string path; // executable path if module is a .so
696 interned_string plt_val; // has_plt => plt wildcard
ff8bd809 697 int64_t pid_val;
2c384610
DS
698
699 virtual void handle_query_module() = 0;
700};
701
c4ce66a1 702base_query::base_query(dwflpp & dw, literal_map_t const & params):
b3fbdd3e
JS
703 sess(dw.sess), dw(dw),
704 has_kernel(false), has_module(false), has_process(false),
705 has_library(false), has_plt(false), has_statement(false),
c88bcbe8 706 pid_val(0)
2c384610 707{
91af0778 708 has_kernel = has_null_param (params, TOK_KERNEL);
2c384610
DS
709 if (has_kernel)
710 module_val = "kernel";
91af0778
FCE
711
712 has_module = get_string_param (params, TOK_MODULE, module_val);
713 if (has_module)
714 has_process = false;
4baf0e53 715 else
d0a7f5a9 716 {
45a63356 717 interned_string library_name;
b3fbdd3e 718 Dwarf_Addr statement_num_val;
37635b45 719 has_process = derived_probe_builder::has_param(params, TOK_PROCESS);
63b4fd14 720 has_library = get_string_param (params, TOK_LIBRARY, library_name);
576eaefe
SC
721 if ((has_plt = has_null_param (params, TOK_PLT)))
722 plt_val = "*";
723 else has_plt = get_string_param (params, TOK_PLT, plt_val);
724 has_statement = get_number_param(params, TOK_STATEMENT, statement_num_val);
725
84c84ac4 726 if (has_process)
84c84ac4 727 {
c88bcbe8 728 if (get_number_param(params, TOK_PROCESS, pid_val))
37635b45
AJ
729 {
730 // check that the pid given corresponds to a running process
a03a3744
JS
731 string pid_err_msg;
732 if (!is_valid_pid(pid_val, pid_err_msg))
733 throw SEMANTIC_ERROR(pid_err_msg);
734
c88bcbe8 735 string pid_path = string("/proc/") + lex_cast(pid_val) + "/exe";
37635b45
AJ
736 module_val = sess.sysroot + pid_path;
737 }
0dbac951 738 else
c88bcbe8
AJ
739 {
740 // reset the pid_val in case anything weird got written into it
741 pid_val = 0;
37635b45 742 get_string_param(params, TOK_PROCESS, module_val);
c88bcbe8 743 }
4ffecddf 744 module_val = find_executable (module_val, sess.sysroot, sess.sysenv);
c627f967 745 if (!is_fully_resolved(module_val, "", sess.sysenv))
5fa0d811 746 throw SEMANTIC_ERROR(_F("cannot find executable '%s'",
7371cd19 747 module_val.to_string().c_str()));
5fa0d811 748 }
dbe9d133
JL
749
750 // Library probe? Let's target that instead if it is fully resolved (such
751 // as what query_one_library() would have done for us). Otherwise, we
752 // resort to iterate_over_libraries().
c30ff776
TP
753 if (has_library)
754 {
755 string library = find_executable (library_name, sess.sysroot,
756 sess.sysenv, "LD_LIBRARY_PATH");
757 if (is_fully_resolved(library, "", sess.sysenv, "LD_LIBRARY_PATH"))
758 {
759 path = path_remove_sysroot(sess, module_val);
760 module_val = library;
761 }
84c84ac4 762 }
d0a7f5a9 763 }
91af0778
FCE
764
765 assert (has_kernel || has_process || has_module);
2c384610
DS
766}
767
46ccda7c 768base_query::base_query(dwflpp & dw, interned_string module_val)
b3fbdd3e
JS
769 : sess(dw.sess), dw(dw),
770 has_kernel(false), has_module(false), has_process(false),
771 has_library(false), has_plt(false), has_statement(false),
c88bcbe8 772 module_val(module_val), pid_val(0)
c4ce66a1
JS
773{
774 // NB: This uses '/' to distinguish between kernel modules and userspace,
775 // which means that userspace modules won't get any PATH searching.
776 if (module_val.find('/') == string::npos)
777 {
778 has_kernel = (module_val == TOK_KERNEL);
779 has_module = !has_kernel;
780 has_process = false;
781 }
782 else
783 {
784 has_kernel = has_module = false;
785 has_process = true;
786 }
787}
788
2c384610 789bool
86bf665e 790base_query::has_null_param(literal_map_t const & params,
45a63356 791 interned_string k)
2c384610 792{
888af770 793 return derived_probe_builder::has_null_param(params, k);
2c384610
DS
794}
795
796
797bool
86bf665e 798base_query::get_string_param(literal_map_t const & params,
45a63356 799 interned_string k, interned_string & v)
2c384610
DS
800{
801 return derived_probe_builder::get_param (params, k, v);
802}
803
804
805bool
86bf665e 806base_query::get_number_param(literal_map_t const & params,
b3fbdd3e 807 interned_string k, int64_t & v)
2c384610 808{
b3fbdd3e 809 return derived_probe_builder::get_param (params, k, v);
7e28cf76
WC
810}
811
812
2c384610 813bool
86bf665e 814base_query::get_number_param(literal_map_t const & params,
45a63356 815 interned_string k, Dwarf_Addr & v)
2c384610 816{
b3fbdd3e 817 int64_t value = 0;
2c384610 818 bool present = derived_probe_builder::get_param (params, k, value);
b3fbdd3e
JS
819 if (present)
820 v = (Dwarf_Addr) value;
2c384610
DS
821 return present;
822}
823
2c384610
DS
824struct dwarf_query : public base_query
825{
e1278bd4 826 dwarf_query(probe * base_probe,
2c384610
DS
827 probe_point * base_loc,
828 dwflpp & dw,
86bf665e 829 literal_map_t const & params,
b642c901 830 vector<derived_probe *> & results,
45a63356
FCE
831 interned_string user_path,
832 interned_string user_lib);
2c384610 833
c4ce66a1 834 vector<derived_probe *> & results;
45a63356 835 set<interned_string> inlined_non_returnable; // function names
c4ce66a1
JS
836 probe * base_probe;
837 probe_point * base_loc;
45a63356
FCE
838 interned_string user_path;
839 interned_string user_lib;
c4ce66a1 840
614f0fcf
JL
841 set<string> visited_libraries;
842 bool resolved_library;
843
2c384610 844 virtual void handle_query_module();
5f0a03a6
JK
845 void query_module_dwarf();
846 void query_module_symtab();
5d5bd369 847 void query_library (const char *data);
576eaefe 848 void query_plt (const char *entry, size_t addr);
2c384610 849
45a63356
FCE
850 void add_probe_point(interned_string funcname,
851 interned_string filename,
2930abc7
FCE
852 int line,
853 Dwarf_Die *scope_die,
854 Dwarf_Addr addr);
36f9dd1d 855
613c8675
JL
856 void mount_well_formed_probe_point();
857 void unmount_well_formed_probe_point();
858 stack<pair<probe_point*, probe*> > previous_bases;
859
45a63356
FCE
860 void replace_probe_point_component_arg(interned_string functor,
861 interned_string new_functor,
71dfce11
JL
862 int64_t new_arg,
863 bool hex = false);
45a63356 864 void replace_probe_point_component_arg(interned_string functor,
71dfce11
JL
865 int64_t new_arg,
866 bool hex = false);
45a63356
FCE
867 void replace_probe_point_component_arg(interned_string functor,
868 interned_string new_functor,
869 interned_string new_arg);
870 void replace_probe_point_component_arg(interned_string functor,
871 interned_string new_arg);
872 void remove_probe_point_component(interned_string functor);
71dfce11 873
857bdfd1
JS
874 // Track addresses we've already seen in a given module
875 set<Dwarf_Addr> alias_dupes;
876
7fdd3e2c
JS
877 // Track inlines we've already seen as well
878 // NB: this can't be compared just by entrypc, as inlines can overlap
879 set<inline_instance_info> inline_dupes;
880
e2941743
JL
881 // Used in .callee[s] probes, when calling iterate_over_callees() (which
882 // provides the actual stack). Retains the addrs of the callers unwind addr
883 // where the callee is found. Specifies multiple callers. E.g. when a callee
884 // at depth 2 is found, callers[1] has the addr of the caller, and callers[0]
885 // has the addr of the caller's caller.
886 stack<Dwarf_Addr> *callers;
887
20c6c071
GH
888 bool has_function_str;
889 bool has_statement_str;
890 bool has_function_num;
891 bool has_statement_num;
45a63356
FCE
892 interned_string statement_str_val;
893 interned_string function_str_val;
c239d28c
GH
894 Dwarf_Addr statement_num_val;
895 Dwarf_Addr function_num_val;
20c6c071 896
b8da0ad1 897 bool has_call;
4bda987e 898 bool has_exported;
b8da0ad1 899 bool has_inline;
20c6c071
GH
900 bool has_return;
901
83eeb3ac
HK
902 bool has_nearest;
903
c9bad430 904 bool has_maxactive;
b3fbdd3e 905 int64_t maxactive_val;
c9bad430 906
20c6c071 907 bool has_label;
45a63356 908 interned_string label_val;
20c6c071 909
c31add50 910 bool has_callee;
45a63356 911 interned_string callee_val;
c31add50
JL
912
913 bool has_callees_num;
b3fbdd3e 914 int64_t callees_num_val;
20c6c071 915
37ebca01
FCE
916 bool has_absolute;
917
467bea43
SC
918 bool has_mark;
919
5f0a03a6
JK
920 enum dbinfo_reqt dbinfo_reqt;
921 enum dbinfo_reqt assess_dbinfo_reqt();
922
7d6d0afc 923 void parse_function_spec(const string & spec);
20c6c071 924 function_spec_type spec_type;
7d6d0afc 925 vector<string> scopes;
45a63356
FCE
926 interned_string function;
927 interned_string file;
4a81c0d9 928 lineno_t lineno_type;
f01d4ffb 929 vector<int> linenos;
5f0a03a6 930 bool query_done; // Found exact match
20c6c071 931
5a617dc6
JL
932 // Holds the prologue end of the current function
933 Dwarf_Addr prologue_end;
934
bd25380d 935 set<string> filtered_srcfiles;
7e1279ea
FCE
936
937 // Map official entrypc -> func_info object
86bf665e
TM
938 inline_instance_map_t filtered_inlines;
939 func_info_map_t filtered_functions;
4df79aaf 940
959ccfed
JL
941 // Helper when we want to iterate over both
942 base_func_info_map_t filtered_all();
943
4df79aaf 944 void query_module_functions ();
e772a6e7 945
45a63356
FCE
946 interned_string final_function_name(interned_string final_func,
947 interned_string final_file,
948 int final_line);
c5142c66
JL
949
950 bool is_fully_specified_function();
b55bc428
FCE
951};
952
98afd80e 953
45a63356
FCE
954uprobe_derived_probe::uprobe_derived_probe (interned_string function,
955 interned_string filename,
d11f62b7 956 int line,
45a63356
FCE
957 interned_string module,
958 interned_string section,
d11f62b7
AJ
959 Dwarf_Addr dwfl_addr,
960 Dwarf_Addr addr,
961 dwarf_query & q,
962 Dwarf_Die* scope_die):
963 dwarf_derived_probe(function, filename, line, module, section,
964 dwfl_addr, addr, q, scope_die), pid(q.pid_val)
965 {}
435f53a7
FCE
966
967
ff8bd809 968static void delete_session_module_cache (systemtap_session& s); // forward decl
435f53a7 969
98afd80e 970struct dwarf_builder: public derived_probe_builder
b55bc428 971{
665e1256 972 map <string,dwflpp*> kern_dw; /* NB: key string could be a wildcard */
7a24d422 973 map <string,dwflpp*> user_dw;
45a63356
FCE
974 interned_string user_path;
975 interned_string user_lib;
44ffe90c
JL
976
977 // Holds modules to suggest functions from. NB: aggregates over
978 // recursive calls to build() when deriving globby probes.
979 set <string> modules_seen;
980
ae2552da 981 dwarf_builder() {}
aa30ccd3 982
ae2552da 983 dwflpp *get_kern_dw(systemtap_session& sess, const string& module)
707bf35e 984 {
ea14cf67
FCE
985 if (kern_dw[module] == 0)
986 kern_dw[module] = new dwflpp(sess, module, true); // might throw
ae2552da 987 return kern_dw[module];
707bf35e
JS
988 }
989
990 dwflpp *get_user_dw(systemtap_session& sess, const string& module)
991 {
ea14cf67
FCE
992 if (user_dw[module] == 0)
993 user_dw[module] = new dwflpp(sess, module, false); // might throw
707bf35e
JS
994 return user_dw[module];
995 }
7a24d422
FCE
996
997 /* NB: not virtual, so can be called from dtor too: */
822a6a3d 998 void dwarf_build_no_more (bool)
aa30ccd3 999 {
435f53a7
FCE
1000 delete_map(kern_dw);
1001 delete_map(user_dw);
7a24d422
FCE
1002 }
1003
1004 void build_no_more (systemtap_session &s)
1005 {
1006 dwarf_build_no_more (s.verbose > 3);
435f53a7 1007 delete_session_module_cache (s);
aa30ccd3
FCE
1008 }
1009
e38d6504
RM
1010 ~dwarf_builder()
1011 {
7a24d422 1012 dwarf_build_no_more (false);
c8959a29 1013 }
aa30ccd3 1014
5227f1ea 1015 virtual void build(systemtap_session & sess,
7a053d3b 1016 probe * base,
20c6c071 1017 probe_point * location,
86bf665e 1018 literal_map_t const & parameters,
20c6c071 1019 vector<derived_probe *> & finished_results);
352c84fe
FL
1020
1021 virtual string name() { return "DWARF builder"; }
b55bc428
FCE
1022};
1023
5111fc3e 1024
e1278bd4 1025dwarf_query::dwarf_query(probe * base_probe,
20c6c071
GH
1026 probe_point * base_loc,
1027 dwflpp & dw,
86bf665e 1028 literal_map_t const & params,
b642c901 1029 vector<derived_probe *> & results,
45a63356
FCE
1030 interned_string user_path,
1031 interned_string user_lib)
e2941743
JL
1032 : base_query(dw, params), results(results), base_probe(base_probe),
1033 base_loc(base_loc), user_path(user_path), user_lib(user_lib),
b3fbdd3e
JS
1034 resolved_library(false), callers(NULL),
1035 has_function_str(false), has_statement_str(false),
1036 has_function_num(false), has_statement_num(false),
1037 statement_num_val(0), function_num_val(0),
1038 has_call(false), has_exported(false), has_inline(false),
1039 has_return(false), has_nearest(false),
1040 has_maxactive(false), maxactive_val(0),
1041 has_label(false), has_callee(false),
1042 has_callees_num(false), callees_num_val(0),
1043 has_absolute(false), has_mark(false),
1044 dbinfo_reqt(dbr_unknown),
1045 spec_type(function_alone),
1046 lineno_type(ABSOLUTE),
1047 query_done(false), prologue_end(0)
bd2b1e68
GH
1048{
1049 // Reduce the query to more reasonable semantic values (booleans,
1050 // extracted strings, numbers, etc).
bd2b1e68
GH
1051 has_function_str = get_string_param(params, TOK_FUNCTION, function_str_val);
1052 has_function_num = get_number_param(params, TOK_FUNCTION, function_num_val);
1053
1054 has_statement_str = get_string_param(params, TOK_STATEMENT, statement_str_val);
1055 has_statement_num = get_number_param(params, TOK_STATEMENT, statement_num_val);
1056
0f336e95 1057 has_label = get_string_param(params, TOK_LABEL, label_val);
c31add50
JL
1058 has_callee = get_string_param(params, TOK_CALLEE, callee_val);
1059 if (has_null_param(params, TOK_CALLEES))
1060 { // .callees ==> .callees(1) (also equivalent to .callee("*"))
1061 has_callees_num = true;
1062 callees_num_val = 1;
1063 }
1064 else
1065 {
1066 has_callees_num = get_number_param(params, TOK_CALLEES, callees_num_val);
1067 if (has_callees_num && callees_num_val < 1)
1068 throw SEMANTIC_ERROR(_(".callees(N) only acceptable for N >= 1"),
1069 base_probe->tok);
1070 }
0f336e95 1071
b8da0ad1 1072 has_call = has_null_param(params, TOK_CALL);
4bda987e 1073 has_exported = has_null_param(params, TOK_EXPORTED);
b8da0ad1 1074 has_inline = has_null_param(params, TOK_INLINE);
bd2b1e68 1075 has_return = has_null_param(params, TOK_RETURN);
83eeb3ac 1076 has_nearest = has_null_param(params, TOK_NEAREST);
c9bad430 1077 has_maxactive = get_number_param(params, TOK_MAXACTIVE, maxactive_val);
37ebca01 1078 has_absolute = has_null_param(params, TOK_ABSOLUTE);
467bea43 1079 has_mark = false;
37ebca01 1080
bd2b1e68 1081 if (has_function_str)
7d6d0afc 1082 parse_function_spec(function_str_val);
bd2b1e68 1083 else if (has_statement_str)
7d6d0afc 1084 parse_function_spec(statement_str_val);
0daad364 1085
5f0a03a6
JK
1086 dbinfo_reqt = assess_dbinfo_reqt();
1087 query_done = false;
0daad364
JS
1088}
1089
1090
2c384610 1091void
5f0a03a6 1092dwarf_query::query_module_dwarf()
2c384610
DS
1093{
1094 if (has_function_num || has_statement_num)
1095 {
1096 // If we have module("foo").function(0xbeef) or
1097 // module("foo").statement(0xbeef), the address is relative
1098 // to the start of the module, so we seek the function
1099 // number plus the module's bias.
6b517475
JS
1100 Dwarf_Addr addr = has_function_num ?
1101 function_num_val : statement_num_val;
08d1d520
MW
1102
1103 // These are raw addresses, we need to know what the elf_bias
1104 // is to feed it to libdwfl based functions.
1105 Dwarf_Addr elf_bias;
1106 Elf *elf = dwfl_module_getelf (dw.module, &elf_bias);
1107 assert(elf);
1108 addr += elf_bias;
6b517475 1109 query_addr(addr, this);
2c384610
DS
1110 }
1111 else
1112 {
1113 // Otherwise if we have a function("foo") or statement("foo")
1114 // specifier, we have to scan over all the CUs looking for
1115 // the function(s) in question
1116 assert(has_function_str || has_statement_str);
4df79aaf
JS
1117
1118 // For simple cases, no wildcard and no source:line, we can do a very
1119 // quick function lookup in a module-wide cache.
1ffb8bd1
JS
1120 if (spec_type == function_alone &&
1121 !dw.name_has_wildcard(function) &&
1122 !startswith(function, "_Z"))
4df79aaf
JS
1123 query_module_functions();
1124 else
337b7c44 1125 dw.iterate_over_cus(&query_cu, this, false);
2c384610
DS
1126 }
1127}
1128
5f0a03a6
JK
1129static void query_func_info (Dwarf_Addr entrypc, func_info & fi,
1130 dwarf_query * q);
1131
91bb9081
LB
1132static void
1133query_symtab_func_info (func_info & fi, dwarf_query * q)
1134{
1135 assert(null_die(&fi.die));
1136
61218615 1137 Dwarf_Addr entrypc = fi.entrypc;
91bb9081
LB
1138
1139 // Now compensate for the dw bias because the addresses come
61218615 1140 // from dwfl_module_symtab, so fi->entrypc is NOT a normal dw address.
91bb9081 1141 q->dw.get_module_dwarf(false, false);
61218615 1142 entrypc -= q->dw.module_bias;
91bb9081
LB
1143
1144 // If there are already probes in this module, lets not duplicate.
1145 // This can come from other weak symbols/aliases or existing
61218615 1146 // matches from Dwarf DIE functions. Try to add this entrypc to the
a41f2ad7 1147 // collection, and only continue if it was new.
61218615
JS
1148 if (q->alias_dupes.insert(entrypc).second)
1149 query_func_info(entrypc, fi, q);
91bb9081
LB
1150}
1151
5f0a03a6
JK
1152void
1153dwarf_query::query_module_symtab()
1154{
1155 // Get the symbol table if it's necessary, sufficient, and not already got.
1156 if (dbinfo_reqt == dbr_need_dwarf)
1157 return;
1158
1159 module_info *mi = dw.mod_info;
1160 if (dbinfo_reqt == dbr_need_symtab)
1161 {
1162 if (mi->symtab_status == info_unknown)
5f52fafe 1163 mi->get_symtab();
5f0a03a6
JK
1164 if (mi->symtab_status == info_absent)
1165 return;
1166 }
1167
1168 func_info *fi = NULL;
1169 symbol_table *sym_table = mi->sym_table;
1170
1171 if (has_function_str)
1172 {
1173 // Per dwarf_query::assess_dbinfo_reqt()...
1174 assert(spec_type == function_alone);
1175 if (dw.name_has_wildcard(function_str_val))
1176 {
b4662f6b 1177 for (auto iter = sym_table->map_by_addr.begin();
1c6b77e5 1178 iter != sym_table->map_by_addr.end();
2e67a43b 1179 ++iter)
5f0a03a6 1180 {
1c6b77e5 1181 fi = iter->second;
53625401
JS
1182 if (!null_die(&fi->die) // already handled in query_module_dwarf()
1183 || fi->descriptor) // ppc opd (and also undefined symbols)
1184 continue;
47d349b1 1185 if (dw.function_name_matches_pattern(fi->name, function_str_val))
91bb9081 1186 query_symtab_func_info(*fi, this);
5f0a03a6
JK
1187 }
1188 }
1189 else
1190 {
b4662f6b
JS
1191 const auto& fis = sym_table->lookup_symbol(function_str_val);
1192 for (auto it=fis.begin(); it!=fis.end(); ++it)
d16f315f
HK
1193 {
1194 fi = *it;
f80d9cdb 1195 if (fi && null_die(&fi->die))
d16f315f
HK
1196 query_symtab_func_info(*fi, this);
1197 }
5f0a03a6 1198 }
5f0a03a6
JK
1199 }
1200}
1201
1202void
1203dwarf_query::handle_query_module()
1204{
f4faaf86
JS
1205 if (has_plt && has_statement_num)
1206 {
1207 query_plt_statement (this);
1208 return;
1209 }
1210
0035051e 1211 bool report = dbinfo_reqt == dbr_need_dwarf;
1c6b77e5
JS
1212 dw.get_module_dwarf(false, report);
1213
1214 // prebuild the symbol table to resolve aliases
5f52fafe 1215 dw.mod_info->get_symtab();
1c6b77e5 1216
857bdfd1
JS
1217 // reset the dupe-checking for each new module
1218 alias_dupes.clear();
7fdd3e2c 1219 inline_dupes.clear();
857bdfd1 1220
5f0a03a6
JK
1221 if (dw.mod_info->dwarf_status == info_present)
1222 query_module_dwarf();
1c6b77e5 1223
91bb9081
LB
1224 // Consult the symbol table, asm and weak functions can show up
1225 // in the symbol table but not in dwarf and minidebuginfo is
1226 // located in the gnu_debugdata section, alias_dupes checking
1227 // is done before adding any probe points
1228 if (!query_done && !pending_interrupts)
5f0a03a6
JK
1229 query_module_symtab();
1230}
1231
2c384610 1232
7d6d0afc
JS
1233void
1234dwarf_query::parse_function_spec(const string & spec)
bd2b1e68 1235{
4a81c0d9 1236 lineno_type = ABSOLUTE;
f01d4ffb 1237 size_t src_pos, line_pos, scope_pos;
bd2b1e68 1238
7d6d0afc 1239 // look for named scopes
91699a70
JS
1240 scope_pos = spec.rfind("::");
1241 if (scope_pos != string::npos)
bd2b1e68 1242 {
91699a70
JS
1243 tokenize_cxx(spec.substr(0, scope_pos), scopes);
1244 scope_pos += 2;
bd2b1e68 1245 }
91699a70
JS
1246 else
1247 scope_pos = 0;
bd2b1e68 1248
7d6d0afc
JS
1249 // look for a source separator
1250 src_pos = spec.find('@', scope_pos);
1251 if (src_pos == string::npos)
bd2b1e68 1252 {
7d6d0afc
JS
1253 function = spec.substr(scope_pos);
1254 spec_type = function_alone;
bd2b1e68 1255 }
7d6d0afc 1256 else
879eb9e9 1257 {
7d6d0afc 1258 function = spec.substr(scope_pos, src_pos - scope_pos);
7a053d3b 1259
7d6d0afc
JS
1260 // look for a line-number separator
1261 line_pos = spec.find_first_of(":+", src_pos);
1262 if (line_pos == string::npos)
1263 {
1264 file = spec.substr(src_pos + 1);
1265 spec_type = function_and_file;
1266 }
1267 else
1268 {
1269 file = spec.substr(src_pos + 1, line_pos - src_pos - 1);
1270
1271 // classify the line spec
1272 spec_type = function_file_and_line;
1273 if (spec[line_pos] == '+')
4a81c0d9 1274 lineno_type = RELATIVE;
7d6d0afc
JS
1275 else if (spec[line_pos + 1] == '*' &&
1276 spec.length() == line_pos + 2)
4a81c0d9 1277 lineno_type = WILDCARD;
7d6d0afc 1278 else
4a81c0d9 1279 lineno_type = ABSOLUTE;
7d6d0afc 1280
4a81c0d9 1281 if (lineno_type != WILDCARD)
7d6d0afc
JS
1282 try
1283 {
f01d4ffb
BC
1284 // try to parse N, N-M, or N,M,O,P, or combination thereof...
1285 if (spec.find_first_of(",-", line_pos + 1) != string::npos)
1286 {
1287 lineno_type = ENUMERATED;
1288 vector<string> sub_specs;
1289 tokenize(spec.substr(line_pos + 1), sub_specs, ",");
b4662f6b
JS
1290 for (auto line_spec = sub_specs.cbegin();
1291 line_spec != sub_specs.cend(); ++line_spec)
f01d4ffb
BC
1292 {
1293 vector<string> ranges;
1294 tokenize(*line_spec, ranges, "-");
1295 if (ranges.size() > 1)
c92d3b42
FCE
1296 {
1297 int low = lex_cast<int>(ranges.front());
1298 int high = lex_cast<int>(ranges.back());
1299 for (int i = low; i <= high; i++)
f01d4ffb 1300 linenos.push_back(i);
c92d3b42 1301 }
f01d4ffb
BC
1302 else
1303 linenos.push_back(lex_cast<int>(ranges.at(0)));
1304 }
1305 sort(linenos.begin(), linenos.end());
1306 }
7d6d0afc
JS
1307 else
1308 {
f01d4ffb
BC
1309 linenos.push_back(lex_cast<int>(spec.substr(line_pos + 1)));
1310 linenos.push_back(lex_cast<int>(spec.substr(line_pos + 1)));
7d6d0afc
JS
1311 }
1312 }
1313 catch (runtime_error & exn)
1314 {
1315 goto bad;
1316 }
1317 }
bd2b1e68
GH
1318 }
1319
7d6d0afc
JS
1320 if (function.empty() ||
1321 (spec_type != function_alone && file.empty()))
bd2b1e68
GH
1322 goto bad;
1323
7d6d0afc 1324 if (sess.verbose > 2)
bd2b1e68 1325 {
b530b5b3
LB
1326 //clog << "parsed '" << spec << "'";
1327 clog << _F("parse '%s'", spec.c_str());
41c262f3 1328
7d6d0afc
JS
1329 if (!scopes.empty())
1330 clog << ", scope '" << scopes[0] << "'";
1331 for (unsigned i = 1; i < scopes.size(); ++i)
1332 clog << "::'" << scopes[i] << "'";
41c262f3 1333
7d6d0afc
JS
1334 clog << ", func '" << function << "'";
1335
1336 if (spec_type != function_alone)
1337 clog << ", file '" << file << "'";
1338
1339 if (spec_type == function_file_and_line)
1340 {
1341 clog << ", line ";
4a81c0d9 1342 switch (lineno_type)
7d6d0afc
JS
1343 {
1344 case ABSOLUTE:
4a81c0d9 1345 clog << linenos[0];
7d6d0afc
JS
1346 break;
1347
1348 case RELATIVE:
4a81c0d9 1349 clog << "+" << linenos[0];
7d6d0afc
JS
1350 break;
1351
f01d4ffb
BC
1352 case ENUMERATED:
1353 {
b4662f6b
JS
1354 for (auto linenos_it = linenos.cbegin();
1355 linenos_it != linenos.cend(); ++linenos_it)
f01d4ffb 1356 {
b4662f6b 1357 auto range_it = linenos_it;
f01d4ffb
BC
1358 while ((range_it+1) != linenos.end() && *range_it + 1 == *(range_it+1))
1359 ++range_it;
1360 if (linenos_it == range_it)
1361 clog << *linenos_it;
1362 else
1363 clog << *linenos_it << "-" << *range_it;
1364 if (range_it + 1 != linenos.end())
1365 clog << ",";
1366 linenos_it = range_it;
1367 }
1368 }
7d6d0afc
JS
1369 break;
1370
1371 case WILDCARD:
1372 clog << "*";
1373 break;
1374 }
1375 }
1376
1377 clog << endl;
bd2b1e68
GH
1378 }
1379
7d6d0afc
JS
1380 return;
1381
1382bad:
dc09353a 1383 throw SEMANTIC_ERROR(_F("malformed specification '%s'", spec.c_str()),
7d6d0afc 1384 base_probe->tok);
bd2b1e68
GH
1385}
1386
05fb3e0c
WF
1387string path_remove_sysroot(const systemtap_session& sess, const string& path)
1388{
1389 size_t pos;
1390 string retval = path;
1391 if (!sess.sysroot.empty() &&
1392 (pos = retval.find(sess.sysroot)) != string::npos)
a7146587 1393 retval.replace(pos, sess.sysroot.length(),
7a517c1f 1394 (*(sess.sysroot.end() - 1) == '/' ? "/": ""));
05fb3e0c
WF
1395 return retval;
1396}
bd2b1e68 1397
c3d8c09c
RB
1398/*
1399 * Convert 'Global Entry Point' to 'Local Entry Point'.
1400 *
1401 * if @gep contains next address after prologue, don't change it.
1402 *
1403 * For ELF ABI v2 on PPC64 LE, we need to adjust sym.st_value corresponding
1404 * to the bits of sym.st_other. These bits will tell us what's the offset
1405 * of the local entry point from the global entry point.
1406 *
1407 * st_other field is currently only used with ABIv2 on ppc64
1408 */
1409static Dwarf_Addr
1410get_lep(dwarf_query *q, Dwarf_Addr gep)
1411{
1412 Dwarf_Addr bias;
1413 Dwfl_Module *mod = q->dw.module;
1414 Elf* elf = (dwarf_getelf (dwfl_module_getdwarf (mod, &bias))
1415 ?: dwfl_module_getelf (mod, &bias));
1416
1417 GElf_Ehdr ehdr_mem;
1418 GElf_Ehdr* em = gelf_getehdr (elf, &ehdr_mem);
1419 if (em == NULL)
1420 throw SEMANTIC_ERROR (_("Couldn't get elf header"));
1421
1422 if (!(em->e_machine == EM_PPC64) || !((em->e_flags & EF_PPC64_ABI) == 2))
1423 return gep;
1424
1425 int syments = dwfl_module_getsymtab(mod);
1426 for (int i = 1; i < syments; ++i)
1427 {
1428 GElf_Sym sym;
1429 GElf_Word section;
1430 GElf_Addr addr;
1431
1432#if _ELFUTILS_PREREQ (0, 158)
1433 dwfl_module_getsym_info (mod, i, &sym, &addr, &section, NULL, NULL);
1434#else
1435 dwfl_module_getsym (mod, i, &sym, &section);
1436 addr = sym.st_value;
1437#endif
1438
1439 /*
1440 * Symbol table contains module_bias + offset. Substract module_bias
1441 * to compare offset with gep.
1442 */
1443 if ((addr - bias) == gep && (GELF_ST_TYPE(sym.st_info) == STT_FUNC)
1444 && sym.st_other)
1445 return gep + PPC64_LOCAL_ENTRY_OFFSET(sym.st_other);
1446 }
1447
1448 return gep;
1449}
1450
36f9dd1d 1451void
45a63356
FCE
1452dwarf_query::add_probe_point(interned_string dw_funcname,
1453 interned_string filename,
36f9dd1d 1454 int line,
b20febf3 1455 Dwarf_Die* scope_die,
36f9dd1d
FCE
1456 Dwarf_Addr addr)
1457{
45a63356 1458 interned_string reloc_section; // base section for relocation purposes
c3d8c09c 1459 Dwarf_Addr orig_addr = addr;
27646582 1460 Dwarf_Addr reloc_addr; // relocated
45a63356
FCE
1461 interned_string module = dw.module_name; // "kernel" or other
1462 interned_string funcname = dw_funcname;
36f9dd1d 1463
37ebca01
FCE
1464 assert (! has_absolute); // already handled in dwarf_builder::build()
1465
c3d8c09c 1466 addr = get_lep(this, addr);
f4faaf86 1467 reloc_addr = dw.relocate_address(addr, reloc_section);
2930abc7 1468
1ffb8bd1 1469 // If we originally used the linkage name, then let's call it that way
1ffb8bd1 1470 const char* linkage_name;
91bb9081 1471 if (!null_die(scope_die) && startswith (this->function, "_Z")
f450a7e3 1472 && (linkage_name = dwarf_linkage_name (scope_die)))
1ffb8bd1
JS
1473 funcname = linkage_name;
1474
7f9f3386
FCE
1475 if (sess.verbose > 1)
1476 {
b530b5b3 1477 clog << _("probe ") << funcname << "@" << filename << ":" << line;
b20febf3 1478 if (string(module) == TOK_KERNEL)
b530b5b3 1479 clog << _(" kernel");
91af0778 1480 else if (has_module)
b530b5b3 1481 clog << _(" module=") << module;
91af0778 1482 else if (has_process)
b530b5b3 1483 clog << _(" process=") << module;
b20febf3 1484 if (reloc_section != "") clog << " reloc=" << reloc_section;
b20febf3 1485 clog << " pc=0x" << hex << addr << dec;
7f9f3386 1486 }
4baf0e53 1487
2f18e69b
JL
1488 dwflpp::blacklisted_type blacklisted = dw.blacklisted_p (funcname, filename,
1489 line, module, addr,
1490 has_return);
b20febf3
FCE
1491 if (sess.verbose > 1)
1492 clog << endl;
7f9f3386 1493
84048984
FCE
1494 if (module == TOK_KERNEL)
1495 {
1496 // PR 4224: adapt to relocatable kernel by subtracting the _stext address here.
1497 reloc_addr = addr - sess.sym_stext;
37ebca01 1498 reloc_section = "_stext"; // a message to runtime's _stp_module_relocate
84048984
FCE
1499 }
1500
2f18e69b 1501 if (!blacklisted)
b20febf3 1502 {
1a0dbc5a 1503 sess.unwindsym_modules.insert (module);
6d0f3f0c
FCE
1504
1505 if (has_process)
1506 {
05fb3e0c 1507 string module_tgt = path_remove_sysroot(sess, module);
6d0f3f0c 1508 results.push_back (new uprobe_derived_probe(funcname, filename, line,
05fb3e0c 1509 module_tgt, reloc_section, addr, reloc_addr,
6d0f3f0c
FCE
1510 *this, scope_die));
1511 }
1512 else
1513 {
1514 assert (has_kernel || has_module);
24897818
DS
1515
1516 // We could only convert probes in the module's .init
1517 // section to symbol+offset probes. However, the module
1518 // refresh code only expects to be called once on a module
1519 // load, so we'll go ahead and convert them all.
1520 if (has_module)
1521 {
1522 module_info *mi = dw.mod_info;
1523
1524 if (mi->symtab_status == info_unknown)
1525 mi->get_symtab();
1526 if (mi->symtab_status == info_absent)
1527 throw SEMANTIC_ERROR(_F("can't retrieve symbol table for function %s",
1528 module_val.to_string().c_str()));
1529
1530 symbol_table *sym_table = mi->sym_table;
1531 func_info *symbol = sym_table->get_func_containing_address(addr);
c3d8c09c
RB
1532
1533 // Do not use LEP to find offset here. When 'symbol_name'
1534 // is used to register probe, kernel itself will find LEP.
1535 Dwarf_Addr offset = orig_addr - symbol->addr;
24897818
DS
1536 results.push_back (new dwarf_derived_probe(funcname, filename,
1537 line, module,
1538 reloc_section, addr,
1539 reloc_addr,
1540 *this, scope_die,
1541 symbol->name,
1542 offset));
1543 }
1544 else
1545 results.push_back (new dwarf_derived_probe(funcname, filename,
1546 line, module,
1547 reloc_section, addr,
1548 reloc_addr,
1549 *this, scope_die));
6d0f3f0c 1550 }
b20febf3 1551 }
2f18e69b
JL
1552 else
1553 {
1554 switch (blacklisted)
1555 {
1556 case dwflpp::blacklisted_section:
1557 sess.print_warning(_F("function %s is in blacklisted section",
7371cd19 1558 funcname.to_string().c_str()), base_probe->tok);
2f18e69b
JL
1559 break;
1560 case dwflpp::blacklisted_kprobes:
1561 sess.print_warning(_F("kprobes function %s is blacklisted",
7371cd19 1562 funcname.to_string().c_str()), base_probe->tok);
2f18e69b
JL
1563 break;
1564 case dwflpp::blacklisted_function_return:
1565 sess.print_warning(_F("function %s return probe is blacklisted",
7371cd19 1566 funcname.to_string().c_str()), base_probe->tok);
2f18e69b
JL
1567 break;
1568 case dwflpp::blacklisted_file:
1569 sess.print_warning(_F("function %s is in blacklisted file",
7371cd19 1570 funcname.to_string().c_str()), base_probe->tok);
2f18e69b
JL
1571 break;
1572 case dwflpp::blacklisted_function:
1573 default:
1574 sess.print_warning(_F("function %s is blacklisted",
7371cd19 1575 funcname.to_string().c_str()), base_probe->tok);
2f18e69b
JL
1576 break;
1577 }
1578 }
2930abc7
FCE
1579}
1580
613c8675
JL
1581void
1582dwarf_query::mount_well_formed_probe_point()
1583{
45a63356 1584 interned_string module = dw.module_name;
613c8675
JL
1585 if (has_process)
1586 module = path_remove_sysroot(sess, module);
1587
1588 vector<probe_point::component*> comps;
b4662f6b 1589 for (auto it = base_loc->components.begin();
613c8675
JL
1590 it != base_loc->components.end(); ++it)
1591 {
1592 if ((*it)->functor == TOK_PROCESS || (*it)->functor == TOK_MODULE)
1593 comps.push_back(new probe_point::component((*it)->functor,
1594 new literal_string(has_library ? path : module)));
1595 else
1596 comps.push_back(*it);
1597 }
1598
1599 probe_point *pp = new probe_point(*base_loc);
1600 pp->well_formed = true;
1601 pp->components = comps;
1602
1603 previous_bases.push(make_pair(base_loc, base_probe));
1604
1605 base_loc = pp;
1606 base_probe = new probe(base_probe, pp);
1607}
1608
1609void
1610dwarf_query::unmount_well_formed_probe_point()
1611{
1612 assert(!previous_bases.empty());
1613
1614 base_loc = previous_bases.top().first;
1615 base_probe = previous_bases.top().second;
1616
1617 previous_bases.pop();
1618}
1619
71dfce11 1620void
45a63356
FCE
1621dwarf_query::replace_probe_point_component_arg(interned_string functor,
1622 interned_string new_functor,
71dfce11
JL
1623 int64_t new_arg,
1624 bool hex)
1625{
1626 // only allow these operations if we're editing the well-formed loc
1627 assert(!previous_bases.empty());
1628
b4662f6b 1629 for (auto it = base_loc->components.begin();
71dfce11
JL
1630 it != base_loc->components.end(); ++it)
1631 if ((*it)->functor == functor)
1632 *it = new probe_point::component(new_functor,
1633 new literal_number(new_arg, hex));
1634}
1635
1636void
45a63356 1637dwarf_query::replace_probe_point_component_arg(interned_string functor,
71dfce11
JL
1638 int64_t new_arg,
1639 bool hex)
1640{
1641 replace_probe_point_component_arg(functor, functor, new_arg, hex);
1642}
1643
1644void
45a63356
FCE
1645dwarf_query::replace_probe_point_component_arg(interned_string functor,
1646 interned_string new_functor,
1647 interned_string new_arg)
71dfce11
JL
1648{
1649 // only allow these operations if we're editing the well-formed loc
1650 assert(!previous_bases.empty());
1651
b4662f6b 1652 for (auto it = base_loc->components.begin();
71dfce11
JL
1653 it != base_loc->components.end(); ++it)
1654 if ((*it)->functor == functor)
1655 *it = new probe_point::component(new_functor,
1656 new literal_string(new_arg));
1657}
1658
1659void
45a63356
FCE
1660dwarf_query::replace_probe_point_component_arg(interned_string functor,
1661 interned_string new_arg)
71dfce11
JL
1662{
1663 replace_probe_point_component_arg(functor, functor, new_arg);
1664}
1665
1666void
45a63356 1667dwarf_query::remove_probe_point_component(interned_string functor)
71dfce11
JL
1668{
1669 // only allow these operations if we're editing the well-formed loc
1670 assert(!previous_bases.empty());
1671
1672 vector<probe_point::component*> new_comps;
b4662f6b 1673 for (auto it = base_loc->components.begin();
71dfce11
JL
1674 it != base_loc->components.end(); ++it)
1675 if ((*it)->functor != functor)
1676 new_comps.push_back(*it);
1677
1678 base_loc->components = new_comps;
2930abc7
FCE
1679}
1680
5f0a03a6
JK
1681enum dbinfo_reqt
1682dwarf_query::assess_dbinfo_reqt()
1683{
1684 if (has_absolute)
1685 {
1686 // kernel.statement(NUM).absolute
1687 return dbr_none;
1688 }
c31add50 1689 if (has_inline || has_label || has_callee || has_callees_num)
5f0a03a6
JK
1690 {
1691 // kernel.function("f").inline or module("m").function("f").inline
1692 return dbr_need_dwarf;
1693 }
1694 if (has_function_str && spec_type == function_alone)
1695 {
1696 // kernel.function("f") or module("m").function("f")
1697 return dbr_need_symtab;
1698 }
1699 if (has_statement_num)
1700 {
1701 // kernel.statement(NUM) or module("m").statement(NUM)
1702 // Technically, all we need is the module offset (or _stext, for
1703 // the kernel). But for that we need either the ELF file or (for
1704 // _stext) the symbol table. In either case, the symbol table
1705 // is available, and that allows us to map the NUM (address)
1706 // to a function, which is goodness.
1707 return dbr_need_symtab;
1708 }
1709 if (has_function_num)
1710 {
1711 // kernel.function(NUM) or module("m").function(NUM)
1712 // Need the symbol table so we can back up from NUM to the
1713 // start of the function.
1714 return dbr_need_symtab;
1715 }
1716 // Symbol table tells us nothing about source files or line numbers.
1717 return dbr_need_dwarf;
1718}
2930abc7 1719
45a63356
FCE
1720interned_string
1721dwarf_query::final_function_name(interned_string final_func,
1722 interned_string final_file,
e772a6e7
JL
1723 int final_line)
1724{
1725 string final_name = final_func;
a3e980f9 1726 if (final_file != "")
e772a6e7 1727 {
79954c0f 1728 final_name += ("@" + string(final_file));
e772a6e7
JL
1729 if (final_line > 0)
1730 final_name += (":" + lex_cast(final_line));
1731 }
1732 return final_name;
1733}
2930abc7 1734
c5142c66
JL
1735bool
1736dwarf_query::is_fully_specified_function()
1737{
1738 // A fully specified function is one that was given using a .function() probe
1739 // by full name (no wildcards), and specific srcfile and decl_line.
1740 return (has_function_str
1741 && spec_type == function_file_and_line
1742 && !dw.name_has_wildcard(function)
1743 && filtered_srcfiles.size() == 1
1744 && !filtered_functions.empty()
1745 && lineno_type == ABSOLUTE
1746 && filtered_functions[0].decl_line == linenos[0]);
1747}
1748
959ccfed
JL
1749base_func_info_map_t
1750dwarf_query::filtered_all(void)
1751{
c5142c66 1752 base_func_info_map_t r;
b4662f6b
JS
1753 for (auto f = filtered_functions.cbegin();
1754 f != filtered_functions.cend(); ++f)
959ccfed 1755 r.push_back(*f);
b4662f6b
JS
1756 for (auto i = filtered_inlines.cbegin();
1757 i != filtered_inlines.cend(); ++i)
959ccfed
JL
1758 r.push_back(*i);
1759 return r;
1760}
1761
b8da0ad1
FCE
1762// The critical determining factor when interpreting a pattern
1763// string is, perhaps surprisingly: "presence of a lineno". The
1764// presence of a lineno changes the search strategy completely.
1765//
1766// Compare the two cases:
1767//
1768// 1. {statement,function}(foo@file.c:lineno)
1769// - find the files matching file.c
1770// - in each file, find the functions matching foo
1771// - query the file for line records matching lineno
1772// - iterate over the line records,
1773// - and iterate over the functions,
1774// - if(haspc(function.DIE, line.addr))
1775// - if looking for statements: probe(lineno.addr)
1776// - if looking for functions: probe(function.{entrypc,return,etc.})
1777//
1778// 2. {statement,function}(foo@file.c)
1779// - find the files matching file.c
1780// - in each file, find the functions matching foo
1781// - probe(function.{entrypc,return,etc.})
1782//
1783// Thus the first decision we make is based on the presence of a
1784// lineno, and we enter entirely different sets of callbacks
1785// depending on that decision.
1786//
1787// Note that the first case is a generalization fo the second, in that
1788// we could theoretically search through line records for matching
1789// file names (a "table scan" in rdbms lingo). Luckily, file names
1790// are already cached elsewhere, so we can do an "index scan" as an
1791// optimization.
7e1279ea 1792
bd2b1e68 1793static void
45a63356
FCE
1794query_statement (interned_string func,
1795 interned_string file,
4cd232e4 1796 int line,
bcc12710 1797 Dwarf_Die *scope_die,
20e4a32c 1798 Dwarf_Addr stmt_addr,
4cd232e4 1799 dwarf_query * q)
bd2b1e68 1800{
39bcd429
FCE
1801 try
1802 {
a3e980f9 1803 q->add_probe_point(func, file,
a9b2f3a5 1804 line, scope_die, stmt_addr);
39bcd429
FCE
1805 }
1806 catch (const semantic_error& e)
1807 {
1808 q->sess.print_error (e);
1809 }
bd2b1e68
GH
1810}
1811
6b517475
JS
1812static void
1813query_addr(Dwarf_Addr addr, dwarf_query *q)
1814{
7a71819c
JL
1815 assert(q->has_function_num || q->has_statement_num);
1816
6b517475
JS
1817 dwflpp &dw = q->dw;
1818
08d1d520
MW
1819 if (q->sess.verbose > 2)
1820 clog << "query_addr 0x" << hex << addr << dec << endl;
6b517475
JS
1821
1822 // First pick which CU contains this address
1823 Dwarf_Die* cudie = dw.query_cu_containing_address(addr);
1824 if (!cudie) // address could be wildly out of range
1825 return;
1826 dw.focus_on_cu(cudie);
1827
1828 // Now compensate for the dw bias
1829 addr -= dw.module_bias;
1830
1831 // Per PR5787, we look up the scope die even for
1832 // statement_num's, for blacklist sensitivity and $var
1833 // resolution purposes.
1834
1835 // Find the scopes containing this address
1836 vector<Dwarf_Die> scopes = dw.getscopes(addr);
1837 if (scopes.empty())
1838 return;
1839
1840 // Look for the innermost containing function
1841 Dwarf_Die *fnscope = NULL;
1842 for (size_t i = 0; i < scopes.size(); ++i)
1843 {
1844 int tag = dwarf_tag(&scopes[i]);
1845 if ((tag == DW_TAG_subprogram && !q->has_inline) ||
1846 (tag == DW_TAG_inlined_subroutine &&
4bda987e 1847 !q->has_call && !q->has_return && !q->has_exported))
6b517475
JS
1848 {
1849 fnscope = &scopes[i];
1850 break;
1851 }
1852 }
1853 if (!fnscope)
1854 return;
1855 dw.focus_on_function(fnscope);
1856
1857 Dwarf_Die *scope = q->has_function_num ? fnscope : &scopes[0];
1858
a3e980f9 1859 const char *file = dwarf_decl_file(fnscope) ?: "";
6b517475
JS
1860 int line;
1861 dwarf_decl_line(fnscope, &line);
1862
1863 // Function probes should reset the addr to the function entry
1864 // and possibly perform prologue searching
1865 if (q->has_function_num)
1866 {
1bbf3f90
JS
1867 if (!dw.die_entrypc(fnscope, &addr))
1868 return;
6b517475 1869 if (dwarf_tag(fnscope) == DW_TAG_subprogram &&
8c1cf686
MC
1870 q->sess.prologue_searching_mode != systemtap_session::prologue_searching_never &&
1871 (q->sess.prologue_searching_mode == systemtap_session::prologue_searching_always ||
b5b9879f 1872 (q->has_process && !q->dw.has_valid_locs()))) // PR 6871 && PR 6941
6b517475
JS
1873 {
1874 func_info func;
1875 func.die = *fnscope;
47d349b1
FCE
1876 func.name = dw.function_name;
1877 func.decl_file = file;
6b517475
JS
1878 func.decl_line = line;
1879 func.entrypc = addr;
1880
1881 func_info_map_t funcs(1, func);
1882 dw.resolve_prologue_endings (funcs);
5a617dc6
JL
1883 q->prologue_end = funcs[0].prologue_end;
1884
1885 // PR13200: if it's a .return probe, we need to emit a *retprobe based
1886 // on the entrypc so here we only use prologue_end for non .return
1887 // probes (note however that .return probes still take advantage of
1888 // prologue_end: PR14436)
1889 if (!q->has_return)
1890 addr = funcs[0].prologue_end;
6b517475
JS
1891 }
1892 }
1893 else
1894 {
de797211
JL
1895 Dwarf_Line *address_line = dwarf_getsrc_die(cudie, addr);
1896 Dwarf_Addr address_line_addr = addr;
6b517475
JS
1897 if (address_line)
1898 {
bf5974d6
JL
1899 file = DWARF_LINESRC(address_line);
1900 line = DWARF_LINENO(address_line);
1901 address_line_addr = DWARF_LINEADDR(address_line);
6b517475
JS
1902 }
1903
1904 // Verify that a raw address matches the beginning of a
1905 // statement. This is a somewhat lame check that the address
1906 // is at the start of an assembly instruction. Mark probes are in the
1907 // middle of a macro and thus not strictly at a statement beginning.
1908 // Guru mode may override this check.
de797211 1909 if (!q->has_mark && (!address_line || address_line_addr != addr))
6b517475
JS
1910 {
1911 stringstream msg;
2a97f50b 1912 msg << _F("address %#" PRIx64 " does not match the beginning of a statement",
b530b5b3 1913 addr);
6b517475 1914 if (address_line)
de797211 1915 msg << _F(" (try %#" PRIx64 ")", address_line_addr);
6b517475 1916 else
b530b5b3
LB
1917 msg << _F(" (no line info found for '%s', in module '%s')",
1918 dw.cu_name().c_str(), dw.module_name.c_str());
6b517475 1919 if (! q->sess.guru_mode)
dc09353a 1920 throw SEMANTIC_ERROR(msg.str());
2713ea24 1921 else
6b517475
JS
1922 q->sess.print_warning(msg.str());
1923 }
1924 }
1925
7a71819c
JL
1926 // We're ready to build a probe, but before, we need to create the final,
1927 // well-formed version of this location with all the components filled in
1928 q->mount_well_formed_probe_point();
1929 q->replace_probe_point_component_arg(TOK_FUNCTION, addr, true /* hex */ );
1930 q->replace_probe_point_component_arg(TOK_STATEMENT, addr, true /* hex */ );
1931
6b517475
JS
1932 // Build a probe at this point
1933 query_statement(dw.function_name, file, line, scope, addr, q);
7a71819c
JL
1934
1935 q->unmount_well_formed_probe_point();
6b517475
JS
1936}
1937
f4faaf86
JS
1938static void
1939query_plt_statement(dwarf_query *q)
1940{
1941 assert (q->has_plt && q->has_statement_num);
1942
1943 Dwarf_Addr addr = q->statement_num_val;
1944 if (q->sess.verbose > 2)
1945 clog << "query_plt_statement 0x" << hex << addr << dec << endl;
1946
1947 // First adjust the raw address to dwfl's elf bias.
1948 Dwarf_Addr elf_bias;
1949 Elf *elf = dwfl_module_getelf (q->dw.module, &elf_bias);
1950 assert(elf);
1951 addr += elf_bias;
1952
1953 // Now compensate for the dw bias
1954 q->dw.get_module_dwarf(false, false);
1955 addr -= q->dw.module_bias;
1956
93b44c2a
JL
1957 // Create the final well-formed probe point
1958 q->mount_well_formed_probe_point();
1959 q->replace_probe_point_component_arg(TOK_STATEMENT, q->statement_num_val, true /* hex */ );
1960
1961 // We remove the .plt part here, since if the user provided a .plt probe, then
1962 // the higher-level probe point is already well-formed. On the other hand, if
1963 // the user provides a .plt(PATTERN).statement(0xABCD), the PATTERN is
1964 // irrelevant (we won't iterate over plts) so just take it out.
1965 q->remove_probe_point_component(TOK_PLT);
1966
f4faaf86
JS
1967 // Build a probe at this point
1968 query_statement(q->plt_val, NULL, -1, NULL, addr, q);
93b44c2a
JL
1969
1970 q->unmount_well_formed_probe_point();
f4faaf86
JS
1971}
1972
8096dd7d 1973static void
600d39f1 1974query_label (const base_func_info& func,
8096dd7d
JS
1975 char const * label,
1976 char const * file,
1977 int line,
1978 Dwarf_Die *scope_die,
1979 Dwarf_Addr stmt_addr,
1980 dwarf_query * q)
1981{
6b517475
JS
1982 assert (q->has_statement_str || q->has_function_str);
1983
8096dd7d
JS
1984 // weed out functions whose decl_file isn't one of
1985 // the source files that we actually care about
6b517475 1986 if (q->spec_type != function_alone &&
8096dd7d
JS
1987 q->filtered_srcfiles.count(file) == 0)
1988 return;
1989
cdabbe86 1990 // Create the final well-formed probe
45a63356 1991 interned_string canon_func = q->final_function_name(func.name, file ?: "", line);
9891c543 1992
cdabbe86
JL
1993 q->mount_well_formed_probe_point();
1994 q->replace_probe_point_component_arg(TOK_FUNCTION, canon_func);
1995 q->replace_probe_point_component_arg(TOK_LABEL, label);
8096dd7d 1996
47d349b1 1997 query_statement(func.name, file, line, scope_die, stmt_addr, q);
9891c543 1998
cdabbe86 1999 q->unmount_well_formed_probe_point();
8096dd7d
JS
2000}
2001
67959c62 2002static void
e772a6e7
JL
2003query_callee (base_func_info& callee,
2004 base_func_info& caller,
67959c62
JL
2005 stack<Dwarf_Addr> *callers,
2006 dwarf_query * q)
2007{
2008 assert (q->has_function_str);
e772a6e7
JL
2009 assert (q->has_callee || q->has_callees_num);
2010
2011 // OK, we found a callee for a targeted caller. To help users see the
04a89a2a 2012 // derivation, we add the well-formed form .function(caller).callee(callee).
e772a6e7 2013
45a63356
FCE
2014 interned_string canon_caller = q->final_function_name(caller.name, caller.decl_file,
2015 caller.decl_line);
2016 interned_string canon_callee = q->final_function_name(callee.name, callee.decl_file,
2017 callee.decl_line);
e772a6e7 2018
04a89a2a
JL
2019 q->mount_well_formed_probe_point();
2020 q->replace_probe_point_component_arg(TOK_FUNCTION, canon_caller);
2021 q->replace_probe_point_component_arg(TOK_CALLEES, TOK_CALLEE, canon_callee);
2022 q->replace_probe_point_component_arg(TOK_CALLEE, canon_callee);
e772a6e7
JL
2023
2024 // Pass on the callers we'll need to add checks for
04a89a2a 2025 q->callers = callers;
e772a6e7 2026
47d349b1 2027 query_statement(callee.name, callee.decl_file,
a3e980f9 2028 callee.decl_line,
04a89a2a
JL
2029 &callee.die, callee.entrypc, q);
2030
2031 q->unmount_well_formed_probe_point();
67959c62
JL
2032}
2033
7e1279ea 2034static void
3e961ba6 2035query_inline_instance_info (inline_instance_info & ii,
7e1279ea
FCE
2036 dwarf_query * q)
2037{
b6581717 2038 try
7e1279ea 2039 {
8f14e444 2040 assert (! q->has_return); // checked by caller already
8debd4e7 2041 assert (q->has_function_str || q->has_statement_str);
0dbac951 2042
8f14e444 2043 if (q->sess.verbose>2)
b530b5b3 2044 clog << _F("querying entrypc %#" PRIx64 " of instance of inline '%s'\n",
7371cd19 2045 ii.entrypc, ii.name.to_string().c_str());
8debd4e7 2046
45a63356
FCE
2047 interned_string canon_func = q->final_function_name(ii.name, ii.decl_file,
2048 ii.decl_line);
8debd4e7
JL
2049
2050 q->mount_well_formed_probe_point();
2051 q->replace_probe_point_component_arg(TOK_FUNCTION, canon_func);
2052 q->replace_probe_point_component_arg(TOK_STATEMENT, canon_func);
2053
47d349b1 2054 query_statement (ii.name, ii.decl_file, ii.decl_line,
8f14e444 2055 &ii.die, ii.entrypc, q);
8debd4e7
JL
2056
2057 q->unmount_well_formed_probe_point();
7e1279ea 2058 }
b6581717 2059 catch (semantic_error &e)
7e1279ea 2060 {
b6581717 2061 q->sess.print_error (e);
7e1279ea
FCE
2062 }
2063}
2064
2065static void
2066query_func_info (Dwarf_Addr entrypc,
bcc12710 2067 func_info & fi,
7e1279ea
FCE
2068 dwarf_query * q)
2069{
e5745975
JL
2070 assert(q->has_function_str || q->has_statement_str);
2071
b6581717 2072 try
7e1279ea 2073 {
45a63356
FCE
2074 interned_string canon_func = q->final_function_name(fi.name, fi.decl_file,
2075 fi.decl_line);
e5745975
JL
2076
2077 q->mount_well_formed_probe_point();
2078 q->replace_probe_point_component_arg(TOK_FUNCTION, canon_func);
2079 q->replace_probe_point_component_arg(TOK_STATEMENT, canon_func);
2080
5a617dc6
JL
2081 // If it's a .return probe, we need to emit a *retprobe based on the
2082 // entrypc (PR13200). Note however that if prologue_end is valid,
2083 // dwarf_derived_probe will still take advantage of it by creating a new
2084 // probe there if necessary to pick up target vars (PR14436).
2085 if (fi.prologue_end == 0 || q->has_return)
2086 {
170ccf10 2087 q->prologue_end = fi.prologue_end;
47d349b1 2088 query_statement (fi.name, fi.decl_file, fi.decl_line,
5a617dc6
JL
2089 &fi.die, entrypc, q);
2090 }
b6581717 2091 else
5a617dc6 2092 {
47d349b1 2093 query_statement (fi.name, fi.decl_file, fi.decl_line,
5a617dc6
JL
2094 &fi.die, fi.prologue_end, q);
2095 }
e5745975
JL
2096
2097 q->unmount_well_formed_probe_point();
7e1279ea 2098 }
b6581717 2099 catch (semantic_error &e)
7e1279ea 2100 {
b6581717 2101 q->sess.print_error (e);
7e1279ea
FCE
2102 }
2103}
2104
7e1279ea 2105static void
de797211 2106query_srcfile_line (Dwarf_Addr addr, int lineno, dwarf_query * q)
7e1279ea 2107{
8fb810d7
JL
2108 assert (q->has_statement_str || q->has_function_str);
2109 assert (q->spec_type == function_file_and_line);
20e4a32c 2110
b4662f6b
JS
2111 auto bfis = q->filtered_all();
2112 for (auto i = bfis.begin(); i != bfis.end(); ++i)
897820ca 2113 {
3e961ba6 2114 if (q->dw.die_has_pc (i->die, addr))
959ccfed
JL
2115 {
2116 if (q->sess.verbose>3)
2117 clog << _("filtered DIE lands on srcfile\n");
2118 Dwarf_Die scope;
2119 q->dw.inner_die_containing_pc(i->die, addr, scope);
e5bc2e52 2120
45a63356
FCE
2121 interned_string canon_func = q->final_function_name(i->name, i->decl_file,
2122 lineno /* NB: not i->decl_line */ );
e5bc2e52 2123
3a5c85b8
JL
2124 if (q->has_nearest && (q->lineno_type == ABSOLUTE ||
2125 q->lineno_type == RELATIVE))
5702442a
JL
2126 {
2127 int lineno_nearest = q->linenos[0];
2128 if (q->lineno_type == RELATIVE)
2129 lineno_nearest += i->decl_line;
45a63356
FCE
2130 interned_string canon_func_nearest = q->final_function_name(i->name,
2131 i->decl_file,
2132 lineno_nearest);
5702442a
JL
2133 q->mount_well_formed_probe_point();
2134 q->replace_probe_point_component_arg(TOK_STATEMENT, canon_func_nearest);
2135 }
2136
e5bc2e52
JL
2137 q->mount_well_formed_probe_point();
2138 q->replace_probe_point_component_arg(TOK_FUNCTION, canon_func);
2139 q->replace_probe_point_component_arg(TOK_STATEMENT, canon_func);
2140
47d349b1 2141 query_statement (i->name, i->decl_file,
959ccfed
JL
2142 lineno, // NB: not q->line !
2143 &scope, addr, q);
e5bc2e52
JL
2144
2145 q->unmount_well_formed_probe_point();
3a5c85b8
JL
2146 if (q->has_nearest && (q->lineno_type == ABSOLUTE ||
2147 q->lineno_type == RELATIVE))
5702442a 2148 q->unmount_well_formed_probe_point();
959ccfed 2149 }
20e4a32c 2150 }
7e1279ea
FCE
2151}
2152
7fdd3e2c
JS
2153bool
2154inline_instance_info::operator<(const inline_instance_info& other) const
2155{
2156 if (entrypc != other.entrypc)
2157 return entrypc < other.entrypc;
2158
2159 if (decl_line != other.decl_line)
2160 return decl_line < other.decl_line;
2161
2162 int cmp = name.compare(other.name);
a3e980f9
FCE
2163 if (!cmp) // tiebreaker
2164 cmp = decl_file.compare(other.decl_file);
c60517ca 2165
7fdd3e2c
JS
2166 return cmp < 0;
2167}
2168
2169
4fa7b22b 2170static int
3956fccb 2171query_dwarf_inline_instance (Dwarf_Die * die, dwarf_query * q)
4fa7b22b 2172{
6b517475 2173 assert (q->has_statement_str || q->has_function_str);
4bda987e 2174 assert (!q->has_call && !q->has_return && !q->has_exported);
bd2b1e68 2175
39bcd429 2176 try
7a053d3b 2177 {
b0ee93c4 2178 if (q->sess.verbose>2)
b530b5b3 2179 clog << _F("selected inline instance of %s\n", q->dw.function_name.c_str());
7e1279ea 2180
6b517475
JS
2181 Dwarf_Addr entrypc;
2182 if (q->dw.die_entrypc (die, &entrypc))
2183 {
2184 inline_instance_info inl;
2185 inl.die = *die;
47d349b1 2186 inl.name = q->dw.function_name;
6b517475 2187 inl.entrypc = entrypc;
a3e980f9
FCE
2188 const char* df;
2189 q->dw.function_file (&df);
47d349b1 2190 inl.decl_file = df ?: "";
6b517475
JS
2191 q->dw.function_line (&inl.decl_line);
2192
2193 // make sure that this inline hasn't already
2194 // been matched from a different CU
2195 if (q->inline_dupes.insert(inl).second)
2196 q->filtered_inlines.push_back(inl);
2197 }
7e1279ea
FCE
2198 return DWARF_CB_OK;
2199 }
2200 catch (const semantic_error& e)
2201 {
2202 q->sess.print_error (e);
2203 return DWARF_CB_ABORT;
2204 }
2205}
bb788f9f 2206
7e1279ea 2207static int
7d007451 2208query_dwarf_func (Dwarf_Die * func, dwarf_query * q)
7e1279ea 2209{
6b517475 2210 assert (q->has_statement_str || q->has_function_str);
bb788f9f 2211
bd25380d
JS
2212 // weed out functions whose decl_file isn't one of
2213 // the source files that we actually care about
6b517475 2214 if (q->spec_type != function_alone &&
bd25380d 2215 q->filtered_srcfiles.count(dwarf_decl_file(func)?:"") == 0)
8096dd7d 2216 return DWARF_CB_OK;
bd25380d 2217
7e1279ea
FCE
2218 try
2219 {
7e1279ea
FCE
2220 q->dw.focus_on_function (func);
2221
7d6d0afc
JS
2222 if (!q->dw.function_scope_matches(q->scopes))
2223 return DWARF_CB_OK;
2224
857bdfd1
JS
2225 // make sure that this function address hasn't
2226 // already been matched under an aliased name
2227 Dwarf_Addr addr;
2228 if (!q->dw.func_is_inline() &&
2229 dwarf_entrypc(func, &addr) == 0 &&
2230 !q->alias_dupes.insert(addr).second)
2231 return DWARF_CB_OK;
2232
4bda987e 2233 if (q->dw.func_is_inline () && (! q->has_call) && (! q->has_return) && (! q->has_exported))
7e1279ea 2234 {
4bda987e 2235 if (q->sess.verbose>3)
b530b5b3 2236 clog << _F("checking instances of inline %s\n", q->dw.function_name.c_str());
4bda987e 2237 q->dw.iterate_over_inline_instances (query_dwarf_inline_instance, q);
7e1279ea 2238 }
8f14e444
FCE
2239 else if (q->dw.func_is_inline () && (q->has_return)) // PR 11553
2240 {
2241 q->inlined_non_returnable.insert (q->dw.function_name);
2242 }
396afcee 2243 else if (!q->dw.func_is_inline () && (! q->has_inline))
20e4a32c 2244 {
4bda987e
SC
2245 if (q->has_exported && !q->dw.func_is_exported ())
2246 return DWARF_CB_OK;
6b517475 2247 if (q->sess.verbose>2)
b530b5b3 2248 clog << _F("selected function %s\n", q->dw.function_name.c_str());
6b517475
JS
2249
2250 func_info func;
2251 q->dw.function_die (&func.die);
47d349b1 2252 func.name = q->dw.function_name;
a3e980f9
FCE
2253 const char *df;
2254 q->dw.function_file (&df);
47d349b1 2255 func.decl_file = df ?: "";
6b517475
JS
2256 q->dw.function_line (&func.decl_line);
2257
2258 Dwarf_Addr entrypc;
b0cfcdb4 2259 if (q->dw.function_entrypc (&entrypc))
6b517475
JS
2260 {
2261 func.entrypc = entrypc;
2262 q->filtered_functions.push_back (func);
2263 }
2264 /* else this function is fully inlined, just ignore it */
7e1279ea 2265 }
39bcd429 2266 return DWARF_CB_OK;
bd2b1e68 2267 }
39bcd429 2268 catch (const semantic_error& e)
bd2b1e68 2269 {
39bcd429
FCE
2270 q->sess.print_error (e);
2271 return DWARF_CB_ABORT;
bd2b1e68 2272 }
bd2b1e68
GH
2273}
2274
2275static int
5c378838 2276query_cu (Dwarf_Die * cudie, dwarf_query * q)
bd2b1e68 2277{
6b517475
JS
2278 assert (q->has_statement_str || q->has_function_str);
2279
85007c04 2280 if (pending_interrupts) return DWARF_CB_ABORT;
7a053d3b 2281
39bcd429 2282 try
bd2b1e68 2283 {
7e1279ea 2284 q->dw.focus_on_cu (cudie);
b5d77020 2285
b0ee93c4 2286 if (false && q->sess.verbose>2)
b530b5b3
LB
2287 clog << _F("focused on CU '%s', in module '%s'\n",
2288 q->dw.cu_name().c_str(), q->dw.module_name.c_str());
d9b516ca 2289
6b517475
JS
2290 q->filtered_srcfiles.clear();
2291 q->filtered_functions.clear();
2292 q->filtered_inlines.clear();
2293
2294 // In this path, we find "abstract functions", record
2295 // information about them, and then (depending on lineno
2296 // matching) possibly emit one or more of the function's
2297 // associated addresses. Unfortunately the control of this
2298 // cannot easily be turned inside out.
2299
2300 if (q->spec_type != function_alone)
39bcd429 2301 {
6b517475
JS
2302 // If we have a pattern string with a filename, we need
2303 // to elaborate the srcfile mask in question first.
2304 q->dw.collect_srcfiles_matching (q->file, q->filtered_srcfiles);
2305
2306 // If we have a file pattern and *no* srcfile matches, there's
2307 // no need to look further into this CU, so skip.
2308 if (q->filtered_srcfiles.empty())
2309 return DWARF_CB_OK;
2310 }
e4c58386 2311
6b517475
JS
2312 // Pick up [entrypc, name, DIE] tuples for all the functions
2313 // matching the query, and fill in the prologue endings of them
2314 // all in a single pass.
5898b6e1 2315 int rc = q->dw.iterate_over_functions (query_dwarf_func, q, q->function);
6b517475
JS
2316 if (rc != DWARF_CB_OK)
2317 q->query_done = true;
2318
277a2b96
JL
2319 if (!q->filtered_functions.empty() &&
2320 !q->has_statement_str && // PR 2608
8c1cf686
MC
2321 q->sess.prologue_searching_mode != systemtap_session::prologue_searching_never &&
2322 (q->sess.prologue_searching_mode == systemtap_session::prologue_searching_always ||
277a2b96
JL
2323 (q->has_process && !q->dw.has_valid_locs()))) // PR 6871 && PR 6941
2324 q->dw.resolve_prologue_endings (q->filtered_functions);
6b517475 2325
600d39f1 2326 if (q->has_label)
6b517475 2327 {
600d39f1
JL
2328 enum lineno_t lineno_type = WILDCARD;
2329 if (q->spec_type == function_file_and_line)
2330 lineno_type = q->lineno_type;
b4662f6b
JS
2331 auto bfis = q->filtered_all();
2332 for (auto i = bfis.begin(); i != bfis.end(); ++i)
600d39f1
JL
2333 q->dw.iterate_over_labels (&i->die, q->label_val, *i, q->linenos,
2334 lineno_type, q, query_label);
39bcd429 2335 }
67959c62
JL
2336 else if (q->has_callee || q->has_callees_num)
2337 {
2338 // .callee(str) --> str, .callees[(N)] --> "*"
2339 string callee_val = q->has_callee ? q->callee_val : "*";
b3fbdd3e 2340 int64_t callees_num_val = q->has_callees_num ? q->callees_num_val : 1;
67959c62
JL
2341
2342 // NB: We filter functions that do not match the file here rather than
2343 // in query_callee because we only want the filtering to apply to the
2344 // first level, not to callees that are recursed into if
2345 // callees_num_val > 1.
b4662f6b
JS
2346 auto bfis = q->filtered_all();
2347 for (auto i = bfis.begin(); i != bfis.end(); ++i)
67959c62
JL
2348 {
2349 if (q->spec_type != function_alone &&
47d349b1 2350 q->filtered_srcfiles.count(i->decl_file) == 0)
67959c62
JL
2351 continue;
2352 q->dw.iterate_over_callees (&i->die, callee_val,
2353 callees_num_val,
e772a6e7 2354 q, query_callee, *i);
67959c62
JL
2355 }
2356 }
c5142c66
JL
2357 else if (q->spec_type == function_file_and_line
2358 // User specified function, file and lineno, but if they match
2359 // exactly a specific function in a specific line at a specific
2360 // decl_line, the user doesn't actually want to probe a lineno,
2361 // but rather the function itself. So let fall through to
2362 // query_func_info/query_inline_instance_info in final else.
131d3efd
FL
2363 && !q->is_fully_specified_function()
2364 && !q->has_function_str)
600d39f1 2365 {
b4662f6b 2366 auto bfis = q->filtered_all();
600d39f1 2367
b4662f6b
JS
2368 for (auto srcfile = q->filtered_srcfiles.cbegin();
2369 srcfile != q->filtered_srcfiles.cend(); ++srcfile)
600d39f1
JL
2370 q->dw.iterate_over_srcfile_lines(srcfile->c_str(), q->linenos,
2371 q->lineno_type, bfis,
83eeb3ac
HK
2372 query_srcfile_line,
2373 q->has_nearest, q);
600d39f1 2374 }
6b517475
JS
2375 else
2376 {
131d3efd
FL
2377 // .statement(...:NN) often gets mixed up with .function(...:NN)
2378 if (q->spec_type == function_file_and_line
2379 && !q->is_fully_specified_function()
2380 && q->has_function_str)
2381 q->sess.print_warning (_("For probing a particular line, use a "
2382 ".statement() probe, not .function()"),
2383 q->base_probe->tok);
2384
6b517475 2385 // Otherwise, simply probe all resolved functions.
b4662f6b 2386 for (auto i = q->filtered_functions.begin();
6b517475
JS
2387 i != q->filtered_functions.end(); ++i)
2388 query_func_info (i->entrypc, *i, q);
2389
2390 // And all inline instances (if we're not excluding inlines with ".call")
2391 if (! q->has_call)
b4662f6b
JS
2392 for (auto i = q->filtered_inlines.begin();
2393 i != q->filtered_inlines.end(); ++i)
6b517475 2394 query_inline_instance_info (*i, q);
e772a6e7 2395 }
39bcd429 2396 return DWARF_CB_OK;
bd2b1e68 2397 }
39bcd429 2398 catch (const semantic_error& e)
bd2b1e68 2399 {
39bcd429
FCE
2400 q->sess.print_error (e);
2401 return DWARF_CB_ABORT;
bd2b1e68 2402 }
bd2b1e68
GH
2403}
2404
0ce64fb8 2405
4df79aaf
JS
2406void
2407dwarf_query::query_module_functions ()
2408{
2409 try
2410 {
2411 filtered_srcfiles.clear();
2412 filtered_functions.clear();
2413 filtered_inlines.clear();
2414
2415 // Collect all module functions so we know which CUs are interesting
2416 int rc = dw.iterate_single_function(query_dwarf_func, this, function);
2417 if (rc != DWARF_CB_OK)
2418 {
2419 query_done = true;
2420 return;
2421 }
2422
2423 set<void*> used_cus; // by cu->addr
2424 vector<Dwarf_Die> cus;
2425 Dwarf_Die cu_mem;
2426
b4662f6b
JS
2427 auto bfis = filtered_all();
2428 for (auto i = bfis.begin(); i != bfis.end(); ++i)
4df79aaf
JS
2429 if (dwarf_diecu(&i->die, &cu_mem, NULL, NULL) &&
2430 used_cus.insert(cu_mem.addr).second)
2431 cus.push_back(cu_mem);
2432
2433 // Reset the dupes since we didn't actually collect them the first time
2434 alias_dupes.clear();
2435 inline_dupes.clear();
2436
2437 // Run the query again on the individual CUs
b4662f6b 2438 for (auto i = cus.begin(); i != cus.end(); ++i){
91bb9081
LB
2439 rc = query_cu(&*i, this);
2440 if (rc != DWARF_CB_OK)
2441 {
2442 query_done = true;
2443 return;
2444 }
2445 }
4df79aaf
JS
2446 }
2447 catch (const semantic_error& e)
2448 {
2449 sess.print_error (e);
2450 }
2451}
2452
2453
5f0a03a6
JK
2454static void
2455validate_module_elf (Dwfl_Module *mod, const char *name, base_query *q)
2456{
2457 // Validate the machine code in this elf file against the
2458 // session machine. This is important, in case the wrong kind
2459 // of debuginfo is being automagically processed by elfutils.
2460 // While we can tell i686 apart from x86-64, unfortunately
2461 // we can't help confusing i586 vs i686 (both EM_386).
2462
2463 Dwarf_Addr bias;
2464 // We prefer dwfl_module_getdwarf to dwfl_module_getelf here,
2465 // because dwfl_module_getelf can force costly section relocations
2466 // we don't really need, while either will do for this purpose.
2467 Elf* elf = (dwarf_getelf (dwfl_module_getdwarf (mod, &bias))
2468 ?: dwfl_module_getelf (mod, &bias));
2469
2470 GElf_Ehdr ehdr_mem;
2471 GElf_Ehdr* em = gelf_getehdr (elf, &ehdr_mem);
e1c3b13a 2472 if (em == 0) { DWFL_ASSERT ("dwfl_getehdr", dwfl_errno()); }
d8f31d0a 2473 assert(em);
5f0a03a6
JK
2474 int elf_machine = em->e_machine;
2475 const char* debug_filename = "";
2476 const char* main_filename = "";
2477 (void) dwfl_module_info (mod, NULL, NULL,
2478 NULL, NULL, NULL,
2479 & main_filename,
2480 & debug_filename);
2481 const string& sess_machine = q->sess.architecture;
756c9462
FCE
2482
2483 string expect_machine; // to match sess.machine (i.e., kernel machine)
2484 string expect_machine2;
5f0a03a6 2485
d27e6fd5 2486 // NB: See also the 'uname -m' squashing done in main.cxx.
5f0a03a6
JK
2487 switch (elf_machine)
2488 {
756c9462
FCE
2489 // x86 and ppc are bi-architecture; a 64-bit kernel
2490 // can normally run either 32-bit or 64-bit *userspace*.
2491 case EM_386:
2492 expect_machine = "i?86";
2493 if (! q->has_process) break; // 32-bit kernel/module
575fe913 2494 /* Fallthrough */
756c9462
FCE
2495 case EM_X86_64:
2496 expect_machine2 = "x86_64";
2497 break;
2498 case EM_PPC:
756c9462 2499 case EM_PPC64:
5a1c472e 2500 expect_machine = "powerpc";
756c9462 2501 break;
3fe7d888 2502 case EM_S390: expect_machine = "s390"; break;
5f0a03a6 2503 case EM_IA_64: expect_machine = "ia64"; break;
d27e6fd5 2504 case EM_ARM: expect_machine = "arm*"; break;
86aae990 2505 case EM_AARCH64: expect_machine = "arm64"; break;
5b0a3499 2506 case EM_MIPS: expect_machine = "mips"; break;
5f0a03a6
JK
2507 // XXX: fill in some more of these
2508 default: expect_machine = "?"; break;
2509 }
2510
2511 if (! debug_filename) debug_filename = main_filename;
2512 if (! debug_filename) debug_filename = name;
2513
756c9462
FCE
2514 if (fnmatch (expect_machine.c_str(), sess_machine.c_str(), 0) != 0 &&
2515 fnmatch (expect_machine2.c_str(), sess_machine.c_str(), 0) != 0)
5f0a03a6
JK
2516 {
2517 stringstream msg;
b530b5b3
LB
2518 msg << _F("ELF machine %s|%s (code %d) mismatch with target %s in '%s'",
2519 expect_machine.c_str(), expect_machine2.c_str(), elf_machine,
2520 sess_machine.c_str(), debug_filename);
dc09353a 2521 throw SEMANTIC_ERROR(msg.str ());
5f0a03a6
JK
2522 }
2523
323a662a 2524 if (q->sess.verbose>2)
b78a0fbb 2525 clog << _F("focused on module '%s' = [%#" PRIx64 "-%#" PRIx64 ", bias %#" PRIx64
b530b5b3
LB
2526 " file %s ELF machine %s|%s (code %d)\n",
2527 q->dw.module_name.c_str(), q->dw.module_start, q->dw.module_end,
2528 q->dw.module_bias, debug_filename, expect_machine.c_str(),
2529 expect_machine2.c_str(), elf_machine);
5f0a03a6 2530}
1d3a40b6 2531
91af0778
FCE
2532
2533
2534static Dwarf_Addr
2535lookup_symbol_address (Dwfl_Module *m, const char* wanted)
2536{
2537 int syments = dwfl_module_getsymtab(m);
2538 assert(syments);
2539 for (int i = 1; i < syments; ++i)
2540 {
2541 GElf_Sym sym;
2542 const char *name = dwfl_module_getsym(m, i, &sym, NULL);
2543 if (name != NULL && strcmp(name, wanted) == 0)
2544 return sym.st_value;
2545 }
2546
2547 return 0;
2548}
2549
2550
2551
bd2b1e68 2552static int
b8da0ad1 2553query_module (Dwfl_Module *mod,
91af0778 2554 void **,
b8da0ad1 2555 const char *name,
6f4c1275 2556 Dwarf_Addr addr,
06de3a04 2557 base_query *q)
bd2b1e68 2558{
39bcd429 2559 try
e38d6504 2560 {
91af0778
FCE
2561 module_info* mi = q->sess.module_cache->cache[name];
2562 if (mi == 0)
2563 {
2564 mi = q->sess.module_cache->cache[name] = new module_info(name);
2565
6f4c1275
FCE
2566 mi->mod = mod;
2567 mi->addr = addr;
91af0778 2568
6f4c1275
FCE
2569 const char* debug_filename = "";
2570 const char* main_filename = "";
2571 (void) dwfl_module_info (mod, NULL, NULL,
2572 NULL, NULL, NULL,
2573 & main_filename,
2574 & debug_filename);
2575
ab3ed72d 2576 if (debug_filename || main_filename)
91af0778 2577 {
6f4c1275
FCE
2578 mi->elf_path = debug_filename ?: main_filename;
2579 }
2580 else if (name == TOK_KERNEL)
2581 {
2582 mi->dwarf_status = info_absent;
91af0778 2583 }
91af0778
FCE
2584 }
2585 // OK, enough of that module_info caching business.
2586
5f0a03a6 2587 q->dw.focus_on_module(mod, mi);
d9b516ca 2588
39bcd429
FCE
2589 // If we have enough information in the pattern to skip a module and
2590 // the module does not match that information, return early.
b8da0ad1 2591 if (!q->dw.module_name_matches(q->module_val))
85007c04 2592 return pending_interrupts ? DWARF_CB_ABORT : DWARF_CB_OK;
0cbbf9d1
FCE
2593
2594 // Don't allow module("*kernel*") type expressions to match the
2595 // elfutils module "kernel", which we refer to in the probe
2596 // point syntax exclusively as "kernel.*".
2597 if (q->dw.module_name == TOK_KERNEL && ! q->has_kernel)
85007c04 2598 return pending_interrupts ? DWARF_CB_ABORT : DWARF_CB_OK;
b5d77020 2599
5f0a03a6
JK
2600 if (mod)
2601 validate_module_elf(mod, name, q);
2602 else
91af0778
FCE
2603 assert(q->has_kernel); // and no vmlinux to examine
2604
2605 if (q->sess.verbose>2)
b530b5b3 2606 cerr << _F("focused on module '%s'\n", q->dw.module_name.c_str());
91af0778
FCE
2607
2608
2609 // Collect a few kernel addresses. XXX: these belong better
2610 // to the sess.module_info["kernel"] struct.
2611 if (q->dw.module_name == TOK_KERNEL)
c931ec8a 2612 {
91af0778
FCE
2613 if (! q->sess.sym_kprobes_text_start)
2614 q->sess.sym_kprobes_text_start = lookup_symbol_address (mod, "__kprobes_text_start");
2615 if (! q->sess.sym_kprobes_text_end)
2616 q->sess.sym_kprobes_text_end = lookup_symbol_address (mod, "__kprobes_text_end");
2617 if (! q->sess.sym_stext)
2618 q->sess.sym_stext = lookup_symbol_address (mod, "_stext");
c931ec8a
FCE
2619 }
2620
dbe9d133
JL
2621 // If there is a .library component, then q->path will hold the path to
2622 // the executable if the library was fully resolved. If not (e.g. not
2623 // absolute, or globby), resort to iterate_over_libraries().
2624 if (q->has_library && q->path.empty())
84c84ac4 2625 q->dw.iterate_over_libraries (&q->query_library_callback, q);
576eaefe
SC
2626 // .plt is translated to .plt.statement(N). We only want to iterate for the
2627 // .plt case
2628 else if (q->has_plt && ! q->has_statement)
070764c0
JL
2629 {
2630 q->dw.iterate_over_plt (q, &q->query_plt_callback);
2631 q->visited_modules.insert(name);
2632 }
84c84ac4 2633 else
070764c0
JL
2634 {
2635 // search the module for matches of the probe point.
2636 q->handle_query_module();
2637 q->visited_modules.insert(name);
2638 }
bb788f9f 2639
b8da0ad1 2640 // If we know that there will be no more matches, abort early.
85007c04 2641 if (q->dw.module_name_final_match(q->module_val) || pending_interrupts)
b8da0ad1
FCE
2642 return DWARF_CB_ABORT;
2643 else
2644 return DWARF_CB_OK;
7a053d3b 2645 }
39bcd429 2646 catch (const semantic_error& e)
bd2b1e68 2647 {
39bcd429
FCE
2648 q->sess.print_error (e);
2649 return DWARF_CB_ABORT;
bd2b1e68 2650 }
bd2b1e68
GH
2651}
2652
35d4ab18 2653
84c84ac4 2654void
bbbc7241 2655base_query::query_library_callback (base_query *me, const char *data)
84c84ac4 2656{
5d5bd369 2657 me->query_library (data);
84c84ac4
SC
2658}
2659
2660
614f0fcf
JL
2661probe*
2662build_library_probe(dwflpp& dw,
2663 const string& library,
2664 probe *base_probe,
2665 probe_point *base_loc)
2666{
2667 probe_point* specific_loc = new probe_point(*base_loc);
614f0fcf
JL
2668 vector<probe_point::component*> derived_comps;
2669
2670 // Create new probe point for the matching library. This is what will be
2671 // shown in listing mode. Also replace the process(str) with the real
2672 // absolute path rather than keeping what the user typed in.
b4662f6b
JS
2673 for (auto it = specific_loc->components.begin();
2674 it != specific_loc->components.end(); ++it)
614f0fcf
JL
2675 if ((*it)->functor == TOK_PROCESS)
2676 derived_comps.push_back(new probe_point::component(TOK_PROCESS,
2677 new literal_string(path_remove_sysroot(dw.sess, dw.module_name))));
2678 else if ((*it)->functor == TOK_LIBRARY)
2679 derived_comps.push_back(new probe_point::component(TOK_LIBRARY,
d2eaa03b
JL
2680 new literal_string(path_remove_sysroot(dw.sess, library)),
2681 true /* from_glob */ ));
614f0fcf
JL
2682 else
2683 derived_comps.push_back(*it);
2684 probe_point* derived_loc = new probe_point(*specific_loc);
2685 derived_loc->components = derived_comps;
2686 return new probe (new probe (base_probe, specific_loc), derived_loc);
2687}
2688
2689bool
51d6bda3
SC
2690query_one_library (const char *library, dwflpp & dw,
2691 const string user_lib, probe * base_probe, probe_point *base_loc,
2692 vector<derived_probe *> & results)
84c84ac4 2693{
47e226ed 2694 if (dw.function_name_matches_pattern(library, "*" + user_lib))
84c84ac4 2695 {
05fb3e0c
WF
2696 string library_path = find_executable (library, "", dw.sess.sysenv,
2697 "LD_LIBRARY_PATH");
168e2ef5
JL
2698 probe *new_base = build_library_probe(dw, library_path,
2699 base_probe, base_loc);
2700
2701 // We pass true for the optional parameter of derive_probes() here to
2702 // indicate that we don't mind if the probe doesn't resolve. This is
2703 // because users expect wildcarded probe points to only apply to a subset
2704 // of matching libraries, in the sense of "any", rather than "all", just
2705 // like module("*") and process("*"). See also dwarf_builder::build().
2706 derive_probes(dw.sess, new_base, results, true /* optional */ );
84c84ac4 2707
51d6bda3 2708 if (dw.sess.verbose > 2)
2cbcfa9c 2709 clog << _("module=") << library_path << endl;
614f0fcf 2710 return true;
84c84ac4 2711 }
614f0fcf 2712 return false;
84c84ac4
SC
2713}
2714
2715
51d6bda3
SC
2716void
2717dwarf_query::query_library (const char *library)
2718{
614f0fcf
JL
2719 visited_libraries.insert(library);
2720 if (query_one_library (library, dw, user_lib, base_probe, base_loc, results))
2721 resolved_library = true;
51d6bda3
SC
2722}
2723
576eaefe
SC
2724struct plt_expanding_visitor: public var_expanding_visitor
2725{
16aa72d9
FCE
2726 plt_expanding_visitor(systemtap_session&s, const string & entry):
2727 var_expanding_visitor (s),
576eaefe
SC
2728 entry (entry)
2729 {
2730 }
2731 const string & entry;
2732
2733 void visit_target_symbol (target_symbol* e);
2734};
2735
2736
2737void
45cdb40e 2738base_query::query_plt_callback (base_query *me, const char *entry, size_t address)
576eaefe 2739{
576eaefe
SC
2740 if (me->dw.function_name_matches_pattern (entry, me->plt_val))
2741 me->query_plt (entry, address);
7f4964f1 2742 me->dw.mod_info->plt_funcs.insert(entry);
576eaefe
SC
2743}
2744
2745
2746void
2747query_one_plt (const char *entry, long addr, dwflpp & dw,
2748 probe * base_probe, probe_point *base_loc,
93b44c2a 2749 vector<derived_probe *> & results, base_query *q)
576eaefe 2750{
45a63356 2751 interned_string module = dw.module_name;
93b44c2a
JL
2752 if (q->has_process)
2753 module = path_remove_sysroot(dw.sess, module);
2754
576eaefe 2755 probe_point* specific_loc = new probe_point(*base_loc);
93b44c2a
JL
2756 specific_loc->well_formed = true;
2757
576eaefe
SC
2758 vector<probe_point::component*> derived_comps;
2759
2760 if (dw.sess.verbose > 2)
2761 clog << _F("plt entry=%s\n", entry);
2762
b4662f6b
JS
2763 for (auto it = specific_loc->components.begin();
2764 it != specific_loc->components.end(); ++it)
93b44c2a
JL
2765 if ((*it)->functor == TOK_PROCESS)
2766 {
2767 // Replace with fully resolved path
2768 *it = new probe_point::component(TOK_PROCESS,
2769 new literal_string(q->has_library ? q->path : module));
2770 derived_comps.push_back(*it);
2771 }
2772 else if ((*it)->functor == TOK_PLT)
3d372d6b 2773 {
93b44c2a 2774 // Replace possibly globby component
8159bf55 2775 *it = new probe_point::component(TOK_PLT,
a3e980f9 2776 new literal_string(string(entry)));
3d372d6b
SC
2777 derived_comps.push_back(*it);
2778 derived_comps.push_back(new probe_point::component(TOK_STATEMENT,
ac7c8669 2779 new literal_number(addr, true)));
3d372d6b 2780 }
576eaefe
SC
2781 else
2782 derived_comps.push_back(*it);
2783 probe_point* derived_loc = new probe_point(*specific_loc);
2784 derived_loc->components = derived_comps;
02c34e38
FCE
2785 probe *new_base = new probe (new probe (base_probe, specific_loc),
2786 derived_loc);
576eaefe 2787 string e = string(entry);
16aa72d9
FCE
2788 plt_expanding_visitor pltv (dw.sess, e);
2789 var_expand_const_fold_loop (dw.sess, new_base->body, pltv);
2e96714f
SC
2790
2791 literal_map_t params;
2792 for (unsigned i = 0; i < derived_loc->components.size(); ++i)
2793 {
2794 probe_point::component *c = derived_loc->components[i];
47d349b1 2795 params[c->functor] = c->arg;
2e96714f
SC
2796 }
2797 dwarf_query derived_q(new_base, derived_loc, dw, params, results, "", "");
2798 dw.iterate_over_modules<base_query>(&query_module, &derived_q);
576eaefe
SC
2799}
2800
2801
2802void
2803dwarf_query::query_plt (const char *entry, size_t address)
2804{
93b44c2a 2805 query_one_plt (entry, address, dw, base_probe, base_loc, results, this);
576eaefe 2806}
51d6bda3 2807
435f53a7
FCE
2808// This would more naturally fit into elaborate.cxx:semantic_pass_symbols,
2809// but the needed declaration for module_cache is not available there.
2810// Nor for that matter in session.cxx. Only in this CU is that field ever
2811// set (in query_module() above), so we clean it up here too.
2812static void
2813delete_session_module_cache (systemtap_session& s)
2814{
2815 if (s.module_cache) {
2816 if (s.verbose > 3)
b530b5b3 2817 clog << _("deleting module_cache") << endl;
435f53a7
FCE
2818 delete s.module_cache;
2819 s.module_cache = 0;
2820 }
2821}
2822
2823
de688825 2824struct dwarf_var_expanding_visitor: public var_expanding_visitor
35d4ab18 2825{
77de5e9e 2826 dwarf_query & q;
bcc12710 2827 Dwarf_Die *scope_die;
77de5e9e 2828 Dwarf_Addr addr;
8c819921 2829 block *add_block;
2260f4e3 2830 block *add_call_probe; // synthesized from .return probes with saved $vars
1630966a
JS
2831 // NB: tids are not always collected in add_block & add_call_probe, because
2832 // gen_kretprobe_saved_return doesn't need them. Thus we need these extra
2833 // *_tid bools for gen_mapped_saved_return to tell what's there.
8cc799a5 2834 bool add_block_tid, add_call_probe_tid;
af234c40 2835 unsigned saved_longs, saved_strings; // data saved within kretprobes
b3627d9a 2836 unordered_map<Dwarf_Addr, block *> entry_probes;
45a63356 2837 unordered_map<std::string, expression *> return_ts_map;
729455a7 2838 vector<Dwarf_Die> scopes;
3689db05 2839 // probe counter name -> pointer of associated probe
aa2c662f 2840 std::set<std::string> perf_counter_refs;
b95e2b79 2841 bool visited;
77de5e9e 2842
de688825 2843 dwarf_var_expanding_visitor(dwarf_query & q, Dwarf_Die *sd, Dwarf_Addr a):
16aa72d9 2844 var_expanding_visitor(q.sess),
b3627d9a 2845 q(q), scope_die(sd), addr(a), add_block(NULL), add_call_probe(NULL),
8cc799a5 2846 add_block_tid(false), add_call_probe_tid(false),
af234c40 2847 saved_longs(0), saved_strings(0), visited(false) {}
277c21bc 2848 expression* gen_mapped_saved_return(expression* e, const string& name);
140be17a 2849 expression* gen_kretprobe_saved_return(expression* e);
a7999c82
JS
2850 void visit_target_symbol_saved_return (target_symbol* e);
2851 void visit_target_symbol_context (target_symbol* e);
d7f3e0c5 2852 void visit_target_symbol (target_symbol* e);
bd1fcbad 2853 void visit_atvar_op (atvar_op* e);
c24447be 2854 void visit_cast_op (cast_op* e);
8cc799a5 2855 void visit_entry_op (entry_op* e);
3689db05 2856 void visit_perf_op (perf_op* e);
16aa72d9 2857
729455a7
JS
2858private:
2859 vector<Dwarf_Die>& getscopes(target_symbol *e);
77de5e9e
GH
2860};
2861
2862
de688825 2863unsigned var_expanding_visitor::tick = 0;
77de5e9e 2864
a50de939 2865
16aa72d9 2866var_expanding_visitor::var_expanding_visitor (systemtap_session& s):
fac438d7 2867 update_visitor(s.verbose), sess(s), op()
a50de939
DS
2868{
2869 // FIXME: for the time being, by default we only support plain '$foo
2870 // = bar', not '+=' or any other op= variant. This is fixable, but a
2871 // bit ugly.
2872 //
2873 // If derived classes desire to add additional operator support, add
2874 // new operators to this list in the derived class constructor.
2875 valid_ops.insert ("=");
2876}
2877
2878
f55efafe
JS
2879void
2880var_expanding_visitor::provide_lvalue_call(functioncall* fcall)
2881{
2882 // Provide the functioncall to our parent, so that it can be used to
2883 // substitute for the assignment node immediately above us.
2884 assert(!target_symbol_setter_functioncalls.empty());
2885 *(target_symbol_setter_functioncalls.top()) = fcall;
2886}
2887
2888
87214add 2889bool
47d349b1 2890var_expanding_visitor::rewrite_lvalue(const token* tok, interned_string& eop,
87214add 2891 expression*& lvalue, expression*& rvalue)
77de5e9e 2892{
e57b735a
GH
2893 // Our job would normally be to require() the left and right sides
2894 // into a new assignment. What we're doing is slightly trickier:
2895 // we're pushing a functioncall** onto a stack, and if our left
2896 // child sets the functioncall* for that value, we're going to
2897 // assume our left child was a target symbol -- transformed into a
2898 // set_target_foo(value) call, and it wants to take our right child
2899 // as the argument "value".
2900 //
2901 // This is why some people claim that languages with
2902 // constructor-decomposing case expressions have a leg up on
2903 // visitors.
2904
2905 functioncall *fcall = NULL;
d9b516ca 2906
a50de939 2907 // Let visit_target_symbol know what operator it should handle.
47d349b1 2908 interned_string* old_op = op;
a3e980f9 2909 op = & eop;
a50de939 2910
e57b735a 2911 target_symbol_setter_functioncalls.push (&fcall);
87214add 2912 replace (lvalue);
e57b735a 2913 target_symbol_setter_functioncalls.pop ();
87214add
JS
2914 replace (rvalue);
2915
2916 op = old_op;
e57b735a
GH
2917
2918 if (fcall != NULL)
77de5e9e 2919 {
e57b735a
GH
2920 // Our left child is informing us that it was a target variable
2921 // and it has been replaced with a set_target_foo() function
2922 // call; we are going to provide that function call -- with the
2923 // right child spliced in as sole argument -- in place of
de688825 2924 // ourselves, in the var expansion we're in the middle of making.
e57b735a 2925
47d349b1 2926 if (valid_ops.find (eop) == valid_ops.end ())
a50de939
DS
2927 {
2928 // Build up a list of supported operators.
2929 string ops;
b530b5b3 2930 int valid_ops_size = 0;
b4662f6b 2931 for (auto i = valid_ops.begin(); i != valid_ops.end(); i++)
b530b5b3 2932 {
a50de939 2933 ops += " " + *i + ",";
b530b5b3
LB
2934 valid_ops_size++;
2935 }
a50de939
DS
2936 ops.resize(ops.size() - 1); // chop off the last ','
2937
2938 // Throw the error.
dc09353a 2939 throw SEMANTIC_ERROR (_NF("Only the following assign operator is implemented on target variables: %s",
1e41115c 2940 "Only the following assign operators are implemented on target variables: %s",
52c2652f 2941 valid_ops_size, ops.c_str()), tok);
b530b5b3 2942
a50de939 2943 }
e57b735a 2944
87214add
JS
2945 assert (lvalue == fcall);
2946 if (rvalue)
2947 fcall->args.push_back (rvalue);
4ed05b15 2948 provide (fcall);
87214add 2949 return true;
77de5e9e 2950 }
e57b735a 2951 else
87214add
JS
2952 return false;
2953}
2954
2955
2956void
2957var_expanding_visitor::visit_assignment (assignment* e)
2958{
2959 if (!rewrite_lvalue (e->tok, e->op, e->left, e->right))
2960 provide (e);
2961}
2962
2963
2964void
2965var_expanding_visitor::visit_pre_crement (pre_crement* e)
2966{
2967 expression *dummy = NULL;
2968 if (!rewrite_lvalue (e->tok, e->op, e->operand, dummy))
2969 provide (e);
2970}
2971
2972
2973void
2974var_expanding_visitor::visit_post_crement (post_crement* e)
2975{
2976 expression *dummy = NULL;
2977 if (!rewrite_lvalue (e->tok, e->op, e->operand, dummy))
2978 provide (e);
2979}
2980
2981
2982void
2983var_expanding_visitor::visit_delete_statement (delete_statement* s)
2984{
2985 string fakeop = "delete";
47d349b1 2986 interned_string fopr = fakeop;
87214add 2987 expression *dummy = NULL;
a3e980f9 2988 if (!rewrite_lvalue (s->tok, fopr, s->value, dummy))
87214add 2989 provide (s);
e57b735a 2990}
d9b516ca 2991
d7f3e0c5 2992
30263a73
FCE
2993void
2994var_expanding_visitor::visit_defined_op (defined_op* e)
2995{
048b546d 2996 expression * const old_operand = e->operand;
30263a73
FCE
2997 bool resolved = true;
2998
2999 defined_ops.push (e);
3000 try {
0fb0cac9 3001 replace (e->operand);
30263a73 3002
c69a87e0 3003 // NB: Formerly, we had some curious cases to consider here, depending on what
30263a73 3004 // various visit_target_symbol() implementations do for successful or
c69a87e0
FCE
3005 // erroneous resolutions. Some would signal a visit_target_symbol failure
3006 // with an exception, with a set flag within the target_symbol, or nothing
3007 // at all.
30263a73 3008 //
c69a87e0
FCE
3009 // Now, failures always have to be signalled with a
3010 // saved_conversion_error being chained to the target_symbol.
3011 // Successes have to result in an attempted rewrite of the
850bfddd 3012 // target_symbol (via provide()).
780f11ff 3013 //
09dd8f07 3014 // Edna Mode: "no capes". fche: "no exceptions". reality: not that simple
c69a87e0 3015
30263a73
FCE
3016 // dwarf stuff: success: rewrites to a function; failure: retains target_symbol, sets saved_conversion_error
3017 //
3018 // sdt-kprobes sdt.h: success: string or functioncall; failure: semantic_error
3019 //
3020 // sdt-uprobes: success: string or no op; failure: no op; expect derived/synthetic
3021 // dwarf probe to take care of it.
3022 // But this is rather unhelpful. So we rig the sdt_var_expanding_visitor
3023 // to pass through @defined() to the synthetic dwarf probe.
780f11ff 3024 //
30263a73
FCE
3025 // utrace: success: rewrites to function; failure: semantic_error
3026 //
850bfddd 3027 // procfs: success: rewrites to function; failure: semantic_error
09dd8f07
FCE
3028 //
3029 // ... but @defined() can nest other types of expressions too, for better or for worse,
3030 // which can result in semantic_error.
30263a73 3031
0fb0cac9
JS
3032 target_symbol* tsym = dynamic_cast<target_symbol*> (e->operand);
3033 if (tsym && tsym->saved_conversion_error) // failing
30263a73 3034 resolved = false;
048b546d 3035 else if (e->operand == old_operand) // unresolved but not marked failing
b7aedf26 3036 {
780f11ff
JS
3037 // There are some visitors that won't touch certain target_symbols,
3038 // e.g. dwarf_var_expanding_visitor won't resolve @cast. We should
3039 // leave it for now so some other visitor can have a chance.
048b546d 3040 defined_ops.pop ();
b7aedf26
JS
3041 provide (e);
3042 return;
3043 }
30263a73
FCE
3044 else // resolved, rewritten to some other expression type
3045 resolved = true;
780f11ff 3046 } catch (const semantic_error& e) {
09dd8f07
FCE
3047 // some uncooperative value like @perf("NO_SUCH_VALUE")
3048 resolved = false;
30263a73
FCE
3049 }
3050 defined_ops.pop ();
3051
16aa72d9
FCE
3052 if (sess.verbose>2)
3053 clog << _("Resolving ") << *e << ": " << resolved << endl;
3054
30263a73
FCE
3055 literal_number* ln = new literal_number (resolved ? 1 : 0);
3056 ln->tok = e->tok;
16aa72d9 3057 abort_provide (ln); // PR20672; stop updating visitor
30263a73
FCE
3058}
3059
3060
5f36109e
JS
3061struct dwarf_pretty_print
3062{
3063 dwarf_pretty_print (dwflpp& dw, vector<Dwarf_Die>& scopes, Dwarf_Addr pc,
3064 const string& local, bool userspace_p,
9c141eca 3065 const target_symbol& e, bool lvalue):
45a1aa33
JS
3066 dw(dw), local(local), scopes(scopes), pc(pc),
3067 pointer(NULL), pointer_type(),
d19a9a82 3068 userspace_p(userspace_p), deref_p(true)
5f36109e
JS
3069 {
3070 init_ts (e);
9c141eca 3071 dw.type_die_for_local (scopes, pc, local, ts, &base_type, lvalue);
5f36109e
JS
3072 }
3073
3074 dwarf_pretty_print (dwflpp& dw, Dwarf_Die *scope_die, Dwarf_Addr pc,
9c141eca 3075 bool userspace_p, const target_symbol& e, bool lvalue):
45a1aa33
JS
3076 dw(dw), scopes(1, *scope_die), pc(pc),
3077 pointer(NULL), pointer_type(),
d19a9a82 3078 userspace_p(userspace_p), deref_p(true)
5f36109e
JS
3079 {
3080 init_ts (e);
9c141eca 3081 dw.type_die_for_return (&scopes[0], pc, ts, &base_type, lvalue);
5f36109e
JS
3082 }
3083
3084 dwarf_pretty_print (dwflpp& dw, Dwarf_Die *type_die, expression* pointer,
9c141eca
DS
3085 bool deref_p, bool userspace_p, const target_symbol& e,
3086 bool lvalue):
5f36109e 3087 dw(dw), pc(0), pointer(pointer), pointer_type(*type_die),
d19a9a82 3088 userspace_p(userspace_p), deref_p(deref_p)
5f36109e
JS
3089 {
3090 init_ts (e);
9c141eca 3091 dw.type_die_for_pointer (type_die, ts, &base_type, lvalue);
5f36109e
JS
3092 }
3093
3094 functioncall* expand ();
ce83ff57 3095 ~dwarf_pretty_print () { delete ts; }
5f36109e
JS
3096
3097private:
3098 dwflpp& dw;
3099 target_symbol* ts;
7d11d8c9 3100 bool print_full;
5f36109e
JS
3101 Dwarf_Die base_type;
3102
3103 string local;
3104 vector<Dwarf_Die> scopes;
3105 Dwarf_Addr pc;
3106
3107 expression* pointer;
3108 Dwarf_Die pointer_type;
3109
d19a9a82 3110 const bool userspace_p, deref_p;
5f36109e
JS
3111
3112 void recurse (Dwarf_Die* type, target_symbol* e,
7d11d8c9 3113 print_format* pf, bool top=false);
600551ca
JS
3114 void recurse_bitfield (Dwarf_Die* type, target_symbol* e,
3115 print_format* pf);
5f36109e 3116 void recurse_base (Dwarf_Die* type, target_symbol* e,
7d11d8c9 3117 print_format* pf);
5f36109e 3118 void recurse_array (Dwarf_Die* type, target_symbol* e,
7d11d8c9 3119 print_format* pf, bool top);
5f36109e 3120 void recurse_pointer (Dwarf_Die* type, target_symbol* e,
7d11d8c9 3121 print_format* pf, bool top);
5f36109e 3122 void recurse_struct (Dwarf_Die* type, target_symbol* e,
7d11d8c9 3123 print_format* pf, bool top);
5f36109e 3124 void recurse_struct_members (Dwarf_Die* type, target_symbol* e,
7d11d8c9 3125 print_format* pf, int& count);
bbee5bb8 3126 bool print_chars (Dwarf_Die* type, target_symbol* e, print_format* pf);
5f36109e
JS
3127
3128 void init_ts (const target_symbol& e);
3129 expression* deref (target_symbol* e);
c55ea10d 3130 bool push_deref (print_format* pf, const string& fmt, target_symbol* e);
5f36109e
JS
3131};
3132
3133
3134void
3135dwarf_pretty_print::init_ts (const target_symbol& e)
3136{
3137 // Work with a new target_symbol so we can modify arguments
3138 ts = new target_symbol (e);
3139
3140 if (ts->addressof)
dc09353a 3141 throw SEMANTIC_ERROR(_("cannot take address of pretty-printed variable"), ts->tok);
5f36109e 3142
c4965ad9
JS
3143 size_t depth = ts->pretty_print_depth ();
3144 if (depth == 0)
dc09353a 3145 throw SEMANTIC_ERROR(_("invalid target_symbol for pretty-print"), ts->tok);
c4965ad9 3146 print_full = depth > 1;
5f36109e
JS
3147 ts->components.pop_back();
3148}
3149
3150
3151functioncall*
3152dwarf_pretty_print::expand ()
3153{
3154 static unsigned tick = 0;
3155
3156 // function pretty_print_X([pointer], [arg1, arg2, ...]) {
7d11d8c9
JS
3157 // try {
3158 // return sprintf("{.foo=...}", (ts)->foo, ...)
3159 // } catch {
3160 // return "ERROR"
3161 // }
5f36109e
JS
3162 // }
3163
3164 // Create the function decl and call.
3165
8ebc4425 3166 string fhash = detox_path(string(ts->tok->location.file->name));
5f36109e
JS
3167 functiondecl *fdecl = new functiondecl;
3168 fdecl->tok = ts->tok;
3169 fdecl->synthetic = true;
8ebc4425
DS
3170 fdecl->unmangled_name = fdecl->name = "__private_" + fhash
3171 + "_dwarf_pretty_print_" + lex_cast(tick++);
5f36109e
JS
3172 fdecl->type = pe_string;
3173
3174 functioncall* fcall = new functioncall;
5d08eac6 3175 fcall->referents.push_back(fdecl); // may be needed for post-pass2a sym resolution; autocast08.stp
5f36109e
JS
3176 fcall->tok = ts->tok;
3177 fcall->function = fdecl->name;
140be17a 3178 fcall->type = pe_string;
5f36109e
JS
3179
3180 // If there's a <pointer>, replace it with a new var and make that
3181 // the first function argument.
3182 if (pointer)
3183 {
3184 vardecl *v = new vardecl;
3185 v->type = pe_long;
9d3a1cd6 3186 v->name = v->unmangled_name = "pointer";
5f36109e 3187 v->tok = ts->tok;
9d3a1cd6 3188 v->synthetic = true;
5f36109e
JS
3189 fdecl->formal_args.push_back (v);
3190 fcall->args.push_back (pointer);
3191
3192 symbol* sym = new symbol;
3193 sym->tok = ts->tok;
3194 sym->name = v->name;
5f36109e
JS
3195 pointer = sym;
3196 }
3197
3198 // For each expression argument, replace it with a function argument.
3199 for (unsigned i = 0; i < ts->components.size(); ++i)
3200 if (ts->components[i].type == target_symbol::comp_expression_array_index)
3201 {
3202 vardecl *v = new vardecl;
3203 v->type = pe_long;
9d3a1cd6 3204 v->unmangled_name = v->name = "index" + lex_cast(i);
5f36109e
JS
3205 v->tok = ts->tok;
3206 fdecl->formal_args.push_back (v);
3207 fcall->args.push_back (ts->components[i].expr_index);
3208
3209 symbol* sym = new symbol;
3210 sym->tok = ts->tok;
3211 sym->name = v->name;
5f36109e
JS
3212 ts->components[i].expr_index = sym;
3213 }
3214
3215 // Create the return sprintf.
1c922ad7 3216 print_format* pf = print_format::create(ts->tok, "sprintf");
5f36109e
JS
3217 return_statement* rs = new return_statement;
3218 rs->tok = ts->tok;
3219 rs->value = pf;
5f36109e
JS
3220
3221 // Recurse into the actual values.
7d11d8c9 3222 recurse (&base_type, ts, pf, true);
5f36109e
JS
3223 pf->components = print_format::string_to_components(pf->raw_components);
3224
7d11d8c9
JS
3225 // Create the try-catch net
3226 try_block* tb = new try_block;
3227 tb->tok = ts->tok;
3228 tb->try_block = rs;
3229 tb->catch_error_var = 0;
3230 return_statement* rs2 = new return_statement;
3231 rs2->tok = ts->tok;
a3e980f9 3232 rs2->value = new literal_string (string("ERROR"));
7d11d8c9
JS
3233 rs2->value->tok = ts->tok;
3234 tb->catch_block = rs2;
3235 fdecl->body = tb;
3236
f8809d54 3237 fdecl->join (dw.sess);
5f36109e
JS
3238 return fcall;
3239}
3240
3241
3242void
3243dwarf_pretty_print::recurse (Dwarf_Die* start_type, target_symbol* e,
7d11d8c9 3244 print_format* pf, bool top)
5f36109e 3245{
611a848e
JS
3246 // deal with initial void* pointers
3247 if (!deref_p && null_die(start_type))
3248 {
3249 push_deref (pf, "%p", e);
3250 return;
3251 }
3252
5f36109e
JS
3253 Dwarf_Die type;
3254 dw.resolve_unqualified_inner_typedie (start_type, &type, e);
3255
3256 switch (dwarf_tag(&type))
3257 {
3258 default:
3259 // XXX need a warning?
3260 // throw semantic_error ("unsupported type (tag " + lex_cast(dwarf_tag(&type))
3261 // + ") for " + dwarf_type_name(&type), e->tok);
3262 pf->raw_components.append("?");
3263 break;
3264
3265 case DW_TAG_enumeration_type:
3266 case DW_TAG_base_type:
7d11d8c9 3267 recurse_base (&type, e, pf);
5f36109e
JS
3268 break;
3269
3270 case DW_TAG_array_type:
7d11d8c9 3271 recurse_array (&type, e, pf, top);
5f36109e
JS
3272 break;
3273
3274 case DW_TAG_pointer_type:
3275 case DW_TAG_reference_type:
3276 case DW_TAG_rvalue_reference_type:
7d11d8c9 3277 recurse_pointer (&type, e, pf, top);
5f36109e
JS
3278 break;
3279
3280 case DW_TAG_subroutine_type:
c55ea10d 3281 push_deref (pf, "<function>:%p", e);
5f36109e
JS
3282 break;
3283
3284 case DW_TAG_union_type:
5f36109e
JS
3285 case DW_TAG_structure_type:
3286 case DW_TAG_class_type:
7d11d8c9 3287 recurse_struct (&type, e, pf, top);
5f36109e
JS
3288 break;
3289 }
3290}
3291
3292
600551ca
JS
3293// Bit fields are handled as a special-case combination of recurse() and
3294// recurse_base(), only called from recurse_struct_members(). The main
3295// difference is that the value is always printed numerically, even if the
3296// underlying type is a char.
3297void
3298dwarf_pretty_print::recurse_bitfield (Dwarf_Die* start_type, target_symbol* e,
3299 print_format* pf)
3300{
3301 Dwarf_Die type;
3302 dw.resolve_unqualified_inner_typedie (start_type, &type, e);
3303
3304 int tag = dwarf_tag(&type);
3305 if (tag != DW_TAG_base_type && tag != DW_TAG_enumeration_type)
3306 {
3307 // XXX need a warning?
3308 // throw semantic_error ("unsupported bitfield type (tag " + lex_cast(tag)
3309 // + ") for " + dwarf_type_name(&type), e->tok);
3310 pf->raw_components.append("?");
3311 return;
3312 }
3313
3314 Dwarf_Attribute attr;
3315 Dwarf_Word encoding = (Dwarf_Word) -1;
3316 dwarf_formudata (dwarf_attr_integrate (&type, DW_AT_encoding, &attr),
3317 &encoding);
3318 switch (encoding)
3319 {
3320 case DW_ATE_float:
3321 case DW_ATE_complex_float:
3322 // XXX need a warning?
3323 // throw semantic_error ("unsupported bitfield type (encoding " + lex_cast(encoding)
3324 // + ") for " + dwarf_type_name(&type), e->tok);
3325 pf->raw_components.append("?");
3326 break;
3327
3328 case DW_ATE_unsigned:
3329 case DW_ATE_unsigned_char:
3330 push_deref (pf, "%u", e);
3331 break;
3332
3333 case DW_ATE_signed:
3334 case DW_ATE_signed_char:
3335 default:
3336 push_deref (pf, "%i", e);
3337 break;
3338 }
3339}
3340
3341
5f36109e
JS
3342void
3343dwarf_pretty_print::recurse_base (Dwarf_Die* type, target_symbol* e,
7d11d8c9 3344 print_format* pf)
5f36109e
JS
3345{
3346 Dwarf_Attribute attr;
3347 Dwarf_Word encoding = (Dwarf_Word) -1;
3348 dwarf_formudata (dwarf_attr_integrate (type, DW_AT_encoding, &attr),
3349 &encoding);
5f36109e
JS
3350 switch (encoding)
3351 {
3352 case DW_ATE_float:
3353 case DW_ATE_complex_float:
3354 // XXX need a warning?
3355 // throw semantic_error ("unsupported type (encoding " + lex_cast(encoding)
3356 // + ") for " + dwarf_type_name(type), e->tok);
3357 pf->raw_components.append("?");
5f36109e
JS
3358 break;
3359
6561d8d1 3360 case DW_ATE_UTF: // XXX need to add unicode to _stp_vsprint_char
5f36109e
JS
3361 case DW_ATE_signed_char:
3362 case DW_ATE_unsigned_char:
941101c1
JS
3363 // Use escapes to make sure that non-printable characters
3364 // don't interrupt our stream (especially '\0' values).
3365 push_deref (pf, "'%#c'", e);
5f36109e
JS
3366 break;
3367
3368 case DW_ATE_unsigned:
c55ea10d 3369 push_deref (pf, "%u", e);
5f36109e
JS
3370 break;
3371
600551ca 3372 case DW_ATE_signed:
5f36109e 3373 default:
c55ea10d 3374 push_deref (pf, "%i", e);
5f36109e
JS
3375 break;
3376 }
5f36109e
JS
3377}
3378
3379
3380void
3381dwarf_pretty_print::recurse_array (Dwarf_Die* type, target_symbol* e,
7d11d8c9 3382 print_format* pf, bool top)
5f36109e 3383{
7d11d8c9
JS
3384 if (!top && !print_full)
3385 {
3386 pf->raw_components.append("[...]");
3387 return;
3388 }
3389
5f36109e
JS
3390 Dwarf_Die childtype;
3391 dwarf_attr_die (type, DW_AT_type, &childtype);
bbee5bb8
JS
3392
3393 if (print_chars (&childtype, e, pf))
3394 return;
3395
5f36109e
JS
3396 pf->raw_components.append("[");
3397
3398 // We print the array up to the first 5 elements.
3399 // XXX how can we determine the array size?
3400 // ... for now, just print the first element
64cddf39 3401 // NB: limit to 32 args; see PR10750 and c_unparser::visit_print_format.
5f36109e 3402 unsigned i, size = 1;
64cddf39 3403 for (i=0; i < size && i < 5 && pf->args.size() < 32; ++i)
5f36109e
JS
3404 {
3405 if (i > 0)
3406 pf->raw_components.append(", ");
3407 target_symbol* e2 = new target_symbol(*e);
3408 e2->components.push_back (target_symbol::component(e->tok, i));
7d11d8c9 3409 recurse (&childtype, e2, pf);
5f36109e
JS
3410 }
3411 if (i < size || 1/*XXX until real size is known */)
3412 pf->raw_components.append(", ...");
3413 pf->raw_components.append("]");
3414}
3415
3416
3417void
3418dwarf_pretty_print::recurse_pointer (Dwarf_Die* type, target_symbol* e,
7d11d8c9 3419 print_format* pf, bool top)
5f36109e 3420{
7d11d8c9 3421 // We chase to top-level pointers, but leave the rest alone
d19a9a82 3422 bool void_p = true;
7d11d8c9 3423 Dwarf_Die pointee;
bbee5bb8 3424 if (dwarf_attr_die (type, DW_AT_type, &pointee))
d19a9a82
JS
3425 {
3426 try
3427 {
3428 dw.resolve_unqualified_inner_typedie (&pointee, &pointee, e);
3429 void_p = false;
3430 }
3431 catch (const semantic_error&) {}
3432 }
3433
3434 if (!void_p)
5f36109e 3435 {
bbee5bb8
JS
3436 if (print_chars (&pointee, e, pf))
3437 return;
3438
3439 if (top)
3440 {
3441 recurse (&pointee, e, pf, top);
3442 return;
3443 }
5f36109e 3444 }
bbee5bb8 3445
c55ea10d 3446 push_deref (pf, "%p", e);
5f36109e
JS
3447}
3448
3449
3450void
3451dwarf_pretty_print::recurse_struct (Dwarf_Die* type, target_symbol* e,
7d11d8c9 3452 print_format* pf, bool top)
5f36109e 3453{
bdec0e18
JS
3454 if (dwarf_hasattr(type, DW_AT_declaration))
3455 {
a44a7cb5 3456 Dwarf_Die *resolved = dw.declaration_resolve(type);
bdec0e18
JS
3457 if (!resolved)
3458 {
3459 // could be an error, but for now just stub it
3460 // throw semantic_error ("unresolved " + dwarf_type_name(type), e->tok);
3461 pf->raw_components.append("{...}");
3462 return;
3463 }
3464 type = resolved;
3465 }
3466
5f36109e
JS
3467 int count = 0;
3468 pf->raw_components.append("{");
7d11d8c9
JS
3469 if (top || print_full)
3470 recurse_struct_members (type, e, pf, count);
3471 else
3472 pf->raw_components.append("...");
5f36109e
JS
3473 pf->raw_components.append("}");
3474}
3475
3476
3477void
3478dwarf_pretty_print::recurse_struct_members (Dwarf_Die* type, target_symbol* e,
7d11d8c9 3479 print_format* pf, int& count)
5f36109e 3480{
a80f28d8
JS
3481 /* With inheritance, a subclass may mask member names of parent classes, so
3482 * our search among the inheritance tree must be breadth-first rather than
3483 * depth-first (recursive). The type die is still our starting point. When
3484 * we encounter a masked name, just skip it. */
3485 set<string> dupes;
3486 deque<Dwarf_Die> inheritees(1, *type);
3487 for (; !inheritees.empty(); inheritees.pop_front())
3488 {
dee830d9 3489 Dwarf_Die child, childtype, import;
a80f28d8
JS
3490 if (dwarf_child (&inheritees.front(), &child) == 0)
3491 do
3492 {
3493 target_symbol* e2 = e;
5f36109e 3494
a80f28d8
JS
3495 // skip static members
3496 if (dwarf_hasattr(&child, DW_AT_declaration))
3497 continue;
5f36109e 3498
a80f28d8 3499 int tag = dwarf_tag (&child);
5f36109e 3500
dee830d9
MW
3501 /* Pretend imported units contain members by recursing into
3502 struct_member printing with the same count. */
3503 if (tag == DW_TAG_imported_unit
3504 && dwarf_attr_die (&child, DW_AT_import, &import))
3505 recurse_struct_members (&import, e2, pf, count);
3506
a80f28d8
JS
3507 if (tag != DW_TAG_member && tag != DW_TAG_inheritance)
3508 continue;
5f36109e 3509
a80f28d8 3510 dwarf_attr_die (&child, DW_AT_type, &childtype);
5f36109e 3511
a80f28d8
JS
3512 if (tag == DW_TAG_inheritance)
3513 {
3514 inheritees.push_back(childtype);
3515 continue;
3516 }
5f36109e 3517
a80f28d8
JS
3518 int childtag = dwarf_tag (&childtype);
3519 const char *member = dwarf_diename (&child);
3a147004 3520
a80f28d8
JS
3521 // "_vptr.foo" members are C++ virtual function tables,
3522 // which (generally?) aren't interesting for users.
3523 if (member && startswith(member, "_vptr."))
3524 continue;
3a147004 3525
a80f28d8
JS
3526 // skip inheritance-masked duplicates
3527 if (member && !dupes.insert(member).second)
3528 continue;
64cddf39 3529
a80f28d8
JS
3530 if (++count > 1)
3531 pf->raw_components.append(", ");
64cddf39 3532
a80f28d8
JS
3533 // NB: limit to 32 args; see PR10750 and c_unparser::visit_print_format.
3534 if (pf->args.size() >= 32)
3535 {
3536 pf->raw_components.append("...");
3537 break;
3538 }
3539
3540 if (member)
3541 {
3542 pf->raw_components.append(".");
3543 pf->raw_components.append(member);
5f36109e 3544
a80f28d8
JS
3545 e2 = new target_symbol(*e);
3546 e2->components.push_back (target_symbol::component(e->tok, member));
3547 }
3548 else if (childtag == DW_TAG_union_type)
3549 pf->raw_components.append("<union>");
3550 else if (childtag == DW_TAG_structure_type)
3551 pf->raw_components.append("<class>");
3552 else if (childtag == DW_TAG_class_type)
3553 pf->raw_components.append("<struct>");
3554 pf->raw_components.append("=");
600551ca
JS
3555
3556 if (dwarf_hasattr_integrate (&child, DW_AT_bit_offset))
3557 recurse_bitfield (&childtype, e2, pf);
3558 else
3559 recurse (&childtype, e2, pf);
5f36109e 3560 }
a80f28d8
JS
3561 while (dwarf_siblingof (&child, &child) == 0);
3562 }
5f36109e
JS
3563}
3564
3565
bbee5bb8
JS
3566bool
3567dwarf_pretty_print::print_chars (Dwarf_Die* start_type, target_symbol* e,
3568 print_format* pf)
3569{
3570 Dwarf_Die type;
3571 dw.resolve_unqualified_inner_typedie (start_type, &type, e);
6561d8d1
JS
3572
3573 Dwarf_Attribute attr;
3574 Dwarf_Word encoding = (Dwarf_Word) -1;
3575 dwarf_formudata (dwarf_attr_integrate (&type, DW_AT_encoding, &attr),
3576 &encoding);
3577 switch (encoding)
bbee5bb8 3578 {
6561d8d1
JS
3579 case DW_ATE_UTF:
3580 case DW_ATE_signed_char:
3581 case DW_ATE_unsigned_char:
3582 break;
3583 default:
3584 return false;
3585 }
3586
9fcf867f 3587 string function = userspace_p ? "user_string_quoted" : "kernel_string_quoted";
6561d8d1
JS
3588 Dwarf_Word size = (Dwarf_Word) -1;
3589 dwarf_formudata (dwarf_attr_integrate (&type, DW_AT_byte_size, &attr), &size);
3590 switch (size)
3591 {
3592 case 1:
3593 break;
3594 case 2:
3595 function += "_utf16";
3596 break;
3597 case 4:
3598 function += "_utf32";
3599 break;
3600 default:
3601 return false;
3602 }
3603
9fcf867f 3604 if (push_deref (pf, "%s", e))
6561d8d1
JS
3605 {
3606 // steal the last arg for a string access
3607 assert (!pf->args.empty());
3608 functioncall* fcall = new functioncall;
3609 fcall->tok = e->tok;
3610 fcall->function = function;
3611 fcall->args.push_back (pf->args.back());
6561d8d1 3612 pf->args.back() = fcall;
bbee5bb8 3613 }
6561d8d1 3614 return true;
bbee5bb8
JS
3615}
3616
e870b754
RH
3617struct target_bitfield_remover: public update_visitor
3618{
3619 void visit_target_bitfield(target_bitfield *);
3620};
3621
3622void target_bitfield_remover::visit_target_bitfield(target_bitfield *e)
3623{
3624 replace (e->base);
3625
3626 expression *ret;
3627 if (e->signed_p)
3628 {
3629 binary_expression *ls = new binary_expression;
3630 ls->tok = e->tok;
3631 ls->op = "<<";
3632 ls->left = e->base;
3633 ls->right = new literal_number(64 - e->offset - e->size);
3634
3635 binary_expression *rs = new binary_expression;
3636 rs->tok = e->tok;
3637 rs->op = ">>";
3638 rs->left = ls;
3639 rs->right = new literal_number(64 - e->size);
3640
3641 ret = rs;
3642 }
3643 else
3644 {
3645 binary_expression *rs = new binary_expression;
3646 rs->tok = e->tok;
3647 rs->op = ">>";
3648 rs->left = e->base;
3649 rs->right = new literal_number(e->offset);
3650
3651 uint64_t field = ((uint64_t)2 << (e->size - 1)) - 1;
3652 binary_expression *msk = new binary_expression;
3653 msk->tok = e->tok;
3654 msk->op = "&";
3655 msk->left = rs;
3656 msk->right = new literal_number(field);
3657
3658 ret = msk;
3659 }
3660 provide (ret);
3661}
3662
a5ce5211
MW
3663// PR10601: adapt to kernel-vs-userspace loc2c-runtime
3664static const string EMBEDDED_FETCH_DEREF_KERNEL = string("\n")
f1e8e7e0
MW
3665 + "#define fetch_register k_fetch_register\n"
3666 + "#define store_register k_store_register\n"
3667 + "#define deref kderef\n"
3668 + "#define store_deref store_kderef\n";
a5ce5211
MW
3669
3670static const string EMBEDDED_FETCH_DEREF_USER = string("\n")
f1e8e7e0
MW
3671 + "#define fetch_register u_fetch_register\n"
3672 + "#define store_register u_store_register\n"
3673 + "#define deref uderef\n"
3674 + "#define store_deref store_uderef\n";
a5ce5211
MW
3675
3676#define EMBEDDED_FETCH_DEREF(U) \
f1e8e7e0 3677 (U ? EMBEDDED_FETCH_DEREF_USER : EMBEDDED_FETCH_DEREF_KERNEL)
a5ce5211
MW
3678
3679static const string EMBEDDED_FETCH_DEREF_DONE = string("\n")
f1e8e7e0
MW
3680 + "#undef fetch_register\n"
3681 + "#undef store_register\n"
3682 + "#undef deref\n"
3683 + "#undef store_deref\n";
bbee5bb8 3684
1c0be8c7 3685static functioncall*
0dbac951
RH
3686synthetic_embedded_deref_call(dwflpp& dw, location_context &ctx,
3687 const std::string &function_name,
3688 Dwarf_Die *function_type,
3689 bool userspace_p, bool lvalue_p,
3690 expression *pointer = NULL)
3691{
3692 target_symbol *e = ctx.e;
5552db3a 3693 const target_symbol *e_orig = ctx.e_orig;
0dbac951
RH
3694 const token *tok = e->tok;
3695
5552db3a
FCE
3696 assert (e != NULL);
3697 assert (e_orig != NULL);
3698
0dbac951
RH
3699 // Synthesize a functiondecl to contain an expression.
3700 string fhash = detox_path(string(tok->location.file->name));
5f36109e
JS
3701 functiondecl *fdecl = new functiondecl;
3702 fdecl->synthetic = true;
0dbac951 3703 fdecl->tok = tok;
8ebc4425 3704 fdecl->unmangled_name = fdecl->name = "__private_" + fhash + function_name;
40a393cd
JS
3705 // The fdecl type is generic, but we'll be detailed on the fcall below.
3706 fdecl->type = pe_long;
c939be69
JS
3707 fdecl->type_details = make_shared<exp_type_dwarf>(&dw, function_type,
3708 userspace_p, e->addressof);
5f36109e
JS
3709 // Synthesize a functioncall.
3710 functioncall* fcall = new functioncall;
0dbac951 3711 fcall->tok = tok;
5d08eac6 3712 fcall->referents.push_back(fdecl); // may be needed for post-pass2a sym resolution; autocast08.stp
5f36109e 3713 fcall->function = fdecl->name;
1c0be8c7 3714 fcall->type = fdecl->type;
c3ed664e 3715 fcall->type_details = fdecl->type_details;
5f36109e 3716
0dbac951
RH
3717 // ??? Once upon a time we explicitly marked functions with
3718 // /* unprivileged */, /* pure */, and /* stable */. Now that we
3719 // have the // function body as staptree nodes, we simply deduce
3720 // the properties from the nodes.
3721
1c0be8c7
JS
3722 // If this code snippet uses a precomputed pointer,
3723 // pass that as the first argument.
66373b2e 3724 if (pointer)
5f36109e 3725 {
0dbac951
RH
3726 assert(ctx.pointer);
3727 fdecl->formal_args.push_back(ctx.pointer);
5f36109e
JS
3728 fcall->args.push_back(pointer);
3729 }
5f36109e 3730
1c0be8c7 3731 // Any non-literal indexes need to be passed as arguments too.
0dbac951
RH
3732 if (!e->components.empty())
3733 {
3734 fdecl->formal_args.insert(fdecl->formal_args.end(),
3735 ctx.indicies.begin(),
5552db3a 3736 ctx.indicies.end()); // indexN..M
0dbac951 3737
5552db3a 3738 assert (e->components.size() == e_orig->components.size());
0dbac951
RH
3739 for (unsigned i = 0; i < e->components.size(); ++i)
3740 if (e->components[i].type == target_symbol::comp_expression_array_index)
5552db3a 3741 fcall->args.push_back(e_orig->components[i].expr_index); // the original index expression
0dbac951 3742 }
5f36109e 3743
1c0be8c7
JS
3744 // If this code snippet is assigning to an lvalue,
3745 // add a final argument for the rvalue.
5552db3a 3746 expression *ref_exp = ctx.locations.back()->program; // contains rewritten
84b71514
FCE
3747 if (ref_exp == 0) // e.g. if saw ->type == loc_noncontinguous
3748 throw SEMANTIC_ERROR(_("no usable location for symbol [error::dwarf]"), e->tok);
1c0be8c7
JS
3749 if (lvalue_p)
3750 {
3414d598
RH
3751 // NB: We don't know the value for fcall argument yet.
3752 // (see target_symbol_setter_functioncalls)
0288a588
RH
3753
3754 vardecl *rvalue = new vardecl;
3755 rvalue->type = pe_long;
9d3a1cd6 3756 rvalue->name = rvalue->unmangled_name = "rvalue";
0288a588
RH
3757 rvalue->tok = tok;
3758
3759 fdecl->formal_args.push_back(rvalue);
3760
3761 symbol *sym = new symbol;
3762 sym->name = rvalue->name;
3763 sym->tok = rvalue->tok;
3764 sym->type = pe_long;
19806444 3765 // sym->referent = rvalue;
e870b754
RH
3766 expression *rhs = sym;
3767
3768 // Expand bitfield writes.
3769 if (target_bitfield *bf = dynamic_cast<target_bitfield *>(ref_exp))
3770 {
3771 uint64_t field = ((uint64_t)2 << (bf->size - 1)) - 1;
3772
3773 ref_exp = bf->base;
3774 if (target_deref *dr = dynamic_cast<target_deref *>(ref_exp))
3775 {
3776 // Compute the address for a deref only once. This is
3777 // particularly important when the address itself is a deref.
3778 expression *addr = ctx.save_expression (dr->addr);
3779 dr->addr = addr;
3780 }
3781
3782 binary_expression *msk = new binary_expression;
3783 msk->tok = tok;
3784 msk->op = "&";
3785 msk->left = sym;
3786 msk->right = new literal_number(field);
3787
3788 binary_expression *sft = new binary_expression;
3789 sft->tok = tok;
3790 sft->op = "<<";
3791 sft->left = msk;
3792 sft->right = new literal_number(bf->offset);
3793
3794 binary_expression *clr = new binary_expression;
3795 clr->tok = tok;
3796 clr->op = "&";
3797 clr->left = deep_copy_visitor::deep_copy(ref_exp);
3798 clr->right = new literal_number(~(field << bf->offset));
3799
3800 binary_expression *ior = new binary_expression;
3801 ior->tok = tok;
3802 ior->op = "|";
3803 ior->left = clr;
3804 ior->right = sft;
3805
3806 rhs = ior;
3807 }
0288a588
RH
3808
3809 assignment *a = new assignment;
3810 a->tok = tok;
3811 a->op = "=";
3812 a->left = ref_exp;
e870b754 3813 a->right = rhs;
0288a588
RH
3814
3815 ref_exp = a;
1c0be8c7
JS
3816 }
3817
e870b754
RH
3818 // Expand bitfield reads.
3819 target_bitfield_remover().replace(ref_exp);
1aad7ed4 3820
0dbac951
RH
3821 fdecl->locals = ctx.locals;
3822
3823 block *blk = new block;
3824 blk->tok = tok;
3825 fdecl->body = blk;
3826
3827 for (auto i = ctx.evals.begin(); i != ctx.evals.end(); ++i)
9da6c217 3828 blk->statements.push_back(*i);
1c0be8c7 3829
0dbac951
RH
3830 return_statement *ret = new return_statement;
3831 ret->tok = tok;
0288a588 3832 ret->value = ref_exp;
0dbac951 3833 blk->statements.push_back(ret);
1aad7ed4 3834
0dbac951 3835 // Add the synthesized decl to the session now.
40a393cd 3836 fdecl->join (dw.sess);
0dbac951 3837
5f36109e
JS
3838 return fcall;
3839}
3840
1c0be8c7
JS
3841expression*
3842dwarf_pretty_print::deref (target_symbol* e)
3843{
3844 static unsigned tick = 0;
3845
3846 if (!deref_p)
3847 {
3848 assert (pointer && e->components.empty());
3849 return pointer;
3850 }
3851
3852 bool lvalue_p = false;
1c0be8c7 3853
0dbac951
RH
3854 location_context ctx(e, pointer);
3855 ctx.pc = pc;
3856 ctx.userspace_p = userspace_p;
3857
40a393cd 3858 Dwarf_Die endtype;
1c0be8c7 3859 if (pointer)
5552db3a 3860 dw.literal_stmt_for_pointer (ctx, &pointer_type, ctx.e, lvalue_p, &endtype);
1c0be8c7 3861 else if (!local.empty())
5552db3a 3862 dw.literal_stmt_for_local (ctx, scopes, local, ctx.e, lvalue_p, &endtype);
1c0be8c7 3863 else
5552db3a 3864 dw.literal_stmt_for_return (ctx, &scopes[0], ctx.e, lvalue_p, &endtype);
1c0be8c7 3865
0dbac951
RH
3866 string name = "_dwarf_pretty_print_deref_" + lex_cast(tick++);
3867 return synthetic_embedded_deref_call(dw, ctx, name, &endtype, userspace_p,
3868 lvalue_p, pointer);
1c0be8c7
JS
3869}
3870
5f36109e 3871
c55ea10d
JS
3872bool
3873dwarf_pretty_print::push_deref (print_format* pf, const string& fmt,
3874 target_symbol* e)
3875{
3876 expression* e2 = NULL;
3877 try
3878 {
3879 e2 = deref (e);
3880 }
3881 catch (const semantic_error&)
3882 {
3883 pf->raw_components.append ("?");
3884 return false;
3885 }
3886 pf->raw_components.append (fmt);
3887 pf->args.push_back (e2);
3888 return true;
3889}
3890
3891
e57b735a 3892void
a7999c82 3893dwarf_var_expanding_visitor::visit_target_symbol_saved_return (target_symbol* e)
e57b735a 3894{
a7999c82
JS
3895 // Get the full name of the target symbol.
3896 stringstream ts_name_stream;
3897 e->print(ts_name_stream);
3898 string ts_name = ts_name_stream.str();
3899
3900 // Check and make sure we haven't already seen this target
3901 // variable in this return probe. If we have, just return our
3902 // last replacement.
b4662f6b 3903 auto i = return_ts_map.find(ts_name);
a7999c82 3904 if (i != return_ts_map.end())
85ecf79a 3905 {
a7999c82
JS
3906 provide (i->second);
3907 return;
3908 }
85ecf79a 3909
70208613
JS
3910 // Attempt the expansion directly first, so if there's a problem with the
3911 // variable we won't have a bogus entry probe lying around. Like in
3912 // saveargs(), we pretend for a moment that we're not in a .return.
70208613 3913 expression *repl = e;
09dd8f07
FCE
3914 {
3915 save_and_restore<bool> temp_return (& q.has_return, false);
3916 replace (repl);
3917 }
3918
ff8550f2
JS
3919 // If it's still a target_symbol, then it couldn't be resolved. It may
3920 // not have a saved_conversion_error yet, e.g. for null_die(scope_die),
3921 // but we know it's not worth making that bogus entry anyway.
3922 if (dynamic_cast<target_symbol*>(repl))
70208613
JS
3923 {
3924 provide (repl);
3925 return;
3926 }
3927
af234c40
JS
3928 expression *exp;
3929 if (!q.has_process &&
3930 strverscmp(q.sess.kernel_base_release.c_str(), "2.6.25") >= 0)
140be17a 3931 exp = gen_kretprobe_saved_return(repl);
af234c40 3932 else
cc9001af 3933 exp = gen_mapped_saved_return(repl, e->sym_name());
af234c40 3934
6bed2a76
JS
3935 // Propagate the DWARF type to the expression in the return probe.
3936 if (repl->type_details && !exp->type_details)
3937 exp->type_details = repl->type_details;
3938
af234c40
JS
3939 // Provide the variable to our parent so it can be used as a
3940 // substitute for the target symbol.
3941 provide (exp);
3942
3943 // Remember this replacement since we might be able to reuse
3944 // it later if the same return probe references this target
3945 // symbol again.
3946 return_ts_map[ts_name] = exp;
3947}
3948
4a2970a3 3949static expression*
23dc94f6
DS
3950gen_mapped_saved_return(systemtap_session &sess, expression* e,
3951 const string& name,
3952 block *& add_block, bool& add_block_tid,
3953 block *& add_call_probe, bool& add_call_probe_tid)
af234c40 3954{
23dc94f6
DS
3955 static unsigned tick = 0;
3956
a7999c82
JS
3957 // We've got to do several things here to handle target
3958 // variables in return probes.
85ecf79a 3959
a7999c82
JS
3960 // (1) Synthesize two global arrays. One is the cache of the
3961 // target variable and the other contains a thread specific
3962 // nesting level counter. The arrays will look like
3963 // this:
3964 //
23dc94f6
DS
3965 // _entry_tvar_{name}_{num}
3966 // _entry_tvar_{name}_{num}_ctr
a7999c82 3967
38bf68a8 3968 string aname = (string("__global_entry_tvar_")
cc9001af 3969 + name
aca66a36 3970 + "_" + lex_cast(tick++));
a7999c82 3971 vardecl* vd = new vardecl;
9d3a1cd6
FCE
3972 vd->name = vd->unmangled_name = aname;
3973 vd->synthetic = true;
a7999c82 3974 vd->tok = e->tok;
23dc94f6 3975 sess.globals.push_back (vd);
a7999c82
JS
3976
3977 string ctrname = aname + "_ctr";
3978 vd = new vardecl;
9d3a1cd6 3979 vd->name = vd->unmangled_name = ctrname;
a7999c82 3980 vd->tok = e->tok;
9d3a1cd6 3981 vd->synthetic = true;
23dc94f6 3982 sess.globals.push_back (vd);
a7999c82
JS
3983
3984 // (2) Create a new code block we're going to insert at the
3985 // beginning of this probe to get the cached value into a
3986 // temporary variable. We'll replace the target variable
3987 // reference with the temporary variable reference. The code
3988 // will look like this:
3989 //
23dc94f6
DS
3990 // _entry_tvar_tid = tid()
3991 // _entry_tvar_{name}_{num}_tmp
3992 // = _entry_tvar_{name}_{num}[_entry_tvar_tid,
3993 // _entry_tvar_{name}_{num}_ctr[_entry_tvar_tid]]
3994 // delete _entry_tvar_{name}_{num}[_entry_tvar_tid,
3995 // _entry_tvar_{name}_{num}_ctr[_entry_tvar_tid]--]
3996 // if (! _entry_tvar_{name}_{num}_ctr[_entry_tvar_tid])
3997 // delete _entry_tvar_{name}_{num}_ctr[_entry_tvar_tid]
a7999c82
JS
3998
3999 // (2a) Synthesize the tid temporary expression, which will look
4000 // like this:
4001 //
23dc94f6 4002 // _entry_tvar_tid = tid()
a7999c82 4003 symbol* tidsym = new symbol;
23dc94f6 4004 tidsym->name = string("_entry_tvar_tid");
a7999c82 4005 tidsym->tok = e->tok;
85ecf79a 4006
a7999c82
JS
4007 if (add_block == NULL)
4008 {
4009 add_block = new block;
4010 add_block->tok = e->tok;
8cc799a5 4011 }
8c819921 4012
8cc799a5
JS
4013 if (!add_block_tid)
4014 {
a7999c82
JS
4015 // Synthesize a functioncall to grab the thread id.
4016 functioncall* fc = new functioncall;
4017 fc->tok = e->tok;
4018 fc->function = string("tid");
8c819921 4019
23dc94f6 4020 // Assign the tid to '_entry_tvar_tid'.
8c819921
DS
4021 assignment* a = new assignment;
4022 a->tok = e->tok;
4023 a->op = "=";
a7999c82
JS
4024 a->left = tidsym;
4025 a->right = fc;
8c819921
DS
4026
4027 expr_statement* es = new expr_statement;
4028 es->tok = e->tok;
4029 es->value = a;
8c819921 4030 add_block->statements.push_back (es);
8cc799a5 4031 add_block_tid = true;
a7999c82 4032 }
8c819921 4033
a7999c82
JS
4034 // (2b) Synthesize an array reference and assign it to a
4035 // temporary variable (that we'll use as replacement for the
4036 // target variable reference). It will look like this:
4037 //
23dc94f6
DS
4038 // _entry_tvar_{name}_{num}_tmp
4039 // = _entry_tvar_{name}_{num}[_entry_tvar_tid,
4040 // _entry_tvar_{name}_{num}_ctr[_entry_tvar_tid]]
a7999c82
JS
4041
4042 arrayindex* ai_tvar_base = new arrayindex;
4043 ai_tvar_base->tok = e->tok;
4044
4045 symbol* sym = new symbol;
4046 sym->name = aname;
4047 sym->tok = e->tok;
4048 ai_tvar_base->base = sym;
4049
4050 ai_tvar_base->indexes.push_back(tidsym);
4051
4052 // We need to create a copy of the array index in its current
4053 // state so we can have 2 variants of it (the original and one
4054 // that post-decrements the second index).
4055 arrayindex* ai_tvar = new arrayindex;
4056 arrayindex* ai_tvar_postdec = new arrayindex;
4057 *ai_tvar = *ai_tvar_base;
4058 *ai_tvar_postdec = *ai_tvar_base;
4059
4060 // Synthesize the
23dc94f6 4061 // "_entry_tvar_{name}_{num}_ctr[_entry_tvar_tid]" used as the
a7999c82
JS
4062 // second index into the array.
4063 arrayindex* ai_ctr = new arrayindex;
4064 ai_ctr->tok = e->tok;
4065
4066 sym = new symbol;
4067 sym->name = ctrname;
4068 sym->tok = e->tok;
4069 ai_ctr->base = sym;
4070 ai_ctr->indexes.push_back(tidsym);
4071 ai_tvar->indexes.push_back(ai_ctr);
4072
4073 symbol* tmpsym = new symbol;
4074 tmpsym->name = aname + "_tmp";
4075 tmpsym->tok = e->tok;
4076
4077 assignment* a = new assignment;
4078 a->tok = e->tok;
4079 a->op = "=";
4080 a->left = tmpsym;
4081 a->right = ai_tvar;
4082
4083 expr_statement* es = new expr_statement;
4084 es->tok = e->tok;
4085 es->value = a;
4086
4087 add_block->statements.push_back (es);
4088
4089 // (2c) Add a post-decrement to the second array index and
4090 // delete the array value. It will look like this:
4091 //
23dc94f6
DS
4092 // delete _entry_tvar_{name}_{num}[_entry_tvar_tid,
4093 // _entry_tvar_{name}_{num}_ctr[_entry_tvar_tid]--]
a7999c82
JS
4094
4095 post_crement* pc = new post_crement;
4096 pc->tok = e->tok;
4097 pc->op = "--";
4098 pc->operand = ai_ctr;
4099 ai_tvar_postdec->indexes.push_back(pc);
4100
4101 delete_statement* ds = new delete_statement;
4102 ds->tok = e->tok;
4103 ds->value = ai_tvar_postdec;
4104
4105 add_block->statements.push_back (ds);
4106
4107 // (2d) Delete the counter value if it is 0. It will look like
4108 // this:
23dc94f6
DS
4109 // if (! _entry_tvar_{name}_{num}_ctr[_entry_tvar_tid])
4110 // delete _entry_tvar_{name}_{num}_ctr[_entry_tvar_tid]
a7999c82
JS
4111
4112 ds = new delete_statement;
4113 ds->tok = e->tok;
4114 ds->value = ai_ctr;
4115
4116 unary_expression *ue = new unary_expression;
4117 ue->tok = e->tok;
4118 ue->op = "!";
4119 ue->operand = ai_ctr;
4120
4121 if_statement *ifs = new if_statement;
4122 ifs->tok = e->tok;
4123 ifs->condition = ue;
4124 ifs->thenblock = ds;
4125 ifs->elseblock = NULL;
4126
4127 add_block->statements.push_back (ifs);
4128
4129 // (3) We need an entry probe that saves the value for us in the
4130 // global array we created. Create the entry probe, which will
4131 // look like this:
4132 //
2260f4e3 4133 // probe kernel.function("{function}").call {
23dc94f6
DS
4134 // _entry_tvar_tid = tid()
4135 // _entry_tvar_{name}_{num}[_entry_tvar_tid,
4136 // ++_entry_tvar_{name}_{num}_ctr[_entry_tvar_tid]]
a7999c82
JS
4137 // = ${param}
4138 // }
4139
2260f4e3 4140 if (add_call_probe == NULL)
a7999c82 4141 {
2260f4e3
FCE
4142 add_call_probe = new block;
4143 add_call_probe->tok = e->tok;
8cc799a5 4144 }
4baf0e53 4145
8cc799a5
JS
4146 if (!add_call_probe_tid)
4147 {
a7999c82
JS
4148 // Synthesize a functioncall to grab the thread id.
4149 functioncall* fc = new functioncall;
4150 fc->tok = e->tok;
4151 fc->function = string("tid");
4baf0e53 4152
23dc94f6 4153 // Assign the tid to '_entry_tvar_tid'.
a7999c82 4154 assignment* a = new assignment;
8fc05e57
DS
4155 a->tok = e->tok;
4156 a->op = "=";
a7999c82
JS
4157 a->left = tidsym;
4158 a->right = fc;
8fc05e57 4159
a7999c82 4160 expr_statement* es = new expr_statement;
8fc05e57
DS
4161 es->tok = e->tok;
4162 es->value = a;
2260f4e3 4163 add_call_probe = new block(add_call_probe, es);
8cc799a5 4164 add_call_probe_tid = true;
85ecf79a 4165 }
cf2a1f85 4166
a7999c82 4167 // Save the value, like this:
23dc94f6
DS
4168 // _entry_tvar_{name}_{num}[_entry_tvar_tid,
4169 // ++_entry_tvar_{name}_{num}_ctr[_entry_tvar_tid]]
a7999c82
JS
4170 // = ${param}
4171 arrayindex* ai_tvar_preinc = new arrayindex;
4172 *ai_tvar_preinc = *ai_tvar_base;
4173
4174 pre_crement* preinc = new pre_crement;
4175 preinc->tok = e->tok;
4176 preinc->op = "++";
4177 preinc->operand = ai_ctr;
4178 ai_tvar_preinc->indexes.push_back(preinc);
4179
4180 a = new assignment;
4181 a->tok = e->tok;
4182 a->op = "=";
4183 a->left = ai_tvar_preinc;
4184 a->right = e;
4185
4186 es = new expr_statement;
4187 es->tok = e->tok;
4188 es->value = a;
4189
2260f4e3 4190 add_call_probe = new block(add_call_probe, es);
a7999c82 4191
23dc94f6 4192 // (4) Provide the '_entry_tvar_{name}_{num}_tmp' variable to
a7999c82
JS
4193 // our parent so it can be used as a substitute for the target
4194 // symbol.
3f803f9e 4195 delete ai_tvar_base;
af234c40
JS
4196 return tmpsym;
4197}
a7999c82 4198
af234c40 4199
23dc94f6
DS
4200expression*
4201dwarf_var_expanding_visitor::gen_mapped_saved_return(expression* e,
4202 const string& name)
4203{
4204 return ::gen_mapped_saved_return(q.sess, e, name, add_block,
4205 add_block_tid, add_call_probe,
4206 add_call_probe_tid);
4207}
4208
4209
af234c40 4210expression*
140be17a 4211dwarf_var_expanding_visitor::gen_kretprobe_saved_return(expression* e)
af234c40
JS
4212{
4213 // The code for this is simple.
4214 //
4215 // .call:
4216 // _set_kretprobe_long(index, $value)
4217 //
4218 // .return:
4219 // _get_kretprobe_long(index)
4220 //
4221 // (or s/long/string/ for things like $$parms)
4222
4223 unsigned index;
4224 string setfn, getfn;
4225
140be17a
JS
4226 // We need the caller to predetermine the type of the expression!
4227 switch (e->type)
af234c40 4228 {
140be17a 4229 case pe_string:
af234c40
JS
4230 index = saved_strings++;
4231 setfn = "_set_kretprobe_string";
4232 getfn = "_get_kretprobe_string";
140be17a
JS
4233 break;
4234 case pe_long:
af234c40
JS
4235 index = saved_longs++;
4236 setfn = "_set_kretprobe_long";
4237 getfn = "_get_kretprobe_long";
140be17a
JS
4238 break;
4239 default:
dc09353a 4240 throw SEMANTIC_ERROR(_("unknown type to save in kretprobe"), e->tok);
af234c40
JS
4241 }
4242
4243 // Create the entry code
4244 // _set_kretprobe_{long|string}(index, $value)
4245
4246 if (add_call_probe == NULL)
4247 {
4248 add_call_probe = new block;
4249 add_call_probe->tok = e->tok;
4250 }
4251
4252 functioncall* set_fc = new functioncall;
4253 set_fc->tok = e->tok;
4254 set_fc->function = setfn;
4255 set_fc->args.push_back(new literal_number(index));
4256 set_fc->args.back()->tok = e->tok;
4257 set_fc->args.push_back(e);
4258
4259 expr_statement* set_es = new expr_statement;
4260 set_es->tok = e->tok;
4261 set_es->value = set_fc;
4262
4263 add_call_probe->statements.push_back(set_es);
4264
4265 // Create the return code
4266 // _get_kretprobe_{long|string}(index)
4267
4268 functioncall* get_fc = new functioncall;
4269 get_fc->tok = e->tok;
4270 get_fc->function = getfn;
4271 get_fc->args.push_back(new literal_number(index));
4272 get_fc->args.back()->tok = e->tok;
4273
4274 return get_fc;
a7999c82 4275}
a43ba433 4276
a7999c82
JS
4277void
4278dwarf_var_expanding_visitor::visit_target_symbol_context (target_symbol* e)
4279{
7503f18e
FCE
4280 if (pending_interrupts) {
4281 provide(e);
4282 return;
4283 }
4284
b78a0fbb 4285 if (null_die(scope_die)) {
a3e980f9 4286 literal_string *empty = new literal_string(string(""));
04c8e51b
JL
4287 empty->tok = e->tok;
4288 provide(empty);
a7999c82 4289 return;
b78a0fbb 4290 }
2cb3fe26 4291
5f36109e
JS
4292 target_symbol *tsym = new target_symbol(*e);
4293
c4965ad9 4294 bool pretty = e->check_pretty_print ();
fde50242 4295 string format = pretty ? "=%s" : "=%#x";
a43ba433 4296
a7999c82
JS
4297 // Convert $$parms to sprintf of a list of parms and active local vars
4298 // which we recursively evaluate
a43ba433 4299
1c922ad7 4300 print_format* pf = print_format::create(e->tok, "sprintf");
a7999c82 4301
277c21bc 4302 if (q.has_return && (e->name == "$$return"))
a7999c82 4303 {
277c21bc 4304 tsym->name = "$return";
a7999c82
JS
4305
4306 // Ignore any variable that isn't accessible.
4307 tsym->saved_conversion_error = 0;
4308 expression *texp = tsym;
8b095b45 4309 replace (texp); // NB: throws nothing ...
a7999c82 4310 if (tsym->saved_conversion_error) // ... but this is how we know it happened.
a43ba433 4311 {
2cb3fe26 4312
a43ba433
FCE
4313 }
4314 else
4315 {
a7999c82 4316 pf->raw_components += "return";
5f36109e 4317 pf->raw_components += format;
a7999c82
JS
4318 pf->args.push_back(texp);
4319 }
4320 }
4321 else
4322 {
4323 // non-.return probe: support $$parms, $$vars, $$locals
345bbb3d 4324 bool first = true;
a7999c82 4325 Dwarf_Die result;
d48bc7eb
JS
4326 vector<Dwarf_Die> scopes = q.dw.getscopes(scope_die);
4327 for (unsigned i = 0; i < scopes.size(); ++i)
4328 {
4329 if (dwarf_tag(&scopes[i]) == DW_TAG_compile_unit)
4330 break; // we don't want file-level variables
4331 if (dwarf_child (&scopes[i], &result) == 0)
4332 do
00cf3709 4333 {
d48bc7eb
JS
4334 switch (dwarf_tag (&result))
4335 {
4336 case DW_TAG_variable:
4337 if (e->name == "$$parms")
4338 continue;
4339 break;
4340 case DW_TAG_formal_parameter:
4341 if (e->name == "$$locals")
4342 continue;
4343 break;
4344
4345 default:
4346 continue;
4347 }
41c262f3 4348
d48bc7eb
JS
4349 const char *diename = dwarf_diename (&result);
4350 if (! diename) continue;
f76427a2 4351
d48bc7eb
JS
4352 if (! first)
4353 pf->raw_components += " ";
4354 pf->raw_components += diename;
fde50242
JS
4355 first = false;
4356
4357 // Write a placeholder for ugly aggregates
4358 Dwarf_Die type;
4359 if (!pretty && dwarf_attr_die(&result, DW_AT_type, &type))
4360 {
4361 q.dw.resolve_unqualified_inner_typedie(&type, &type, e);
4362 switch (dwarf_tag(&type))
4363 {
4364 case DW_TAG_union_type:
4365 case DW_TAG_structure_type:
4366 case DW_TAG_class_type:
4367 pf->raw_components += "={...}";
4368 continue;
4369
4370 case DW_TAG_array_type:
4371 pf->raw_components += "=[...]";
4372 continue;
4373 }
4374 }
345bbb3d 4375
47d349b1 4376 tsym->name = string("$") + diename;
41c262f3 4377
d48bc7eb
JS
4378 // Ignore any variable that isn't accessible.
4379 tsym->saved_conversion_error = 0;
4380 expression *texp = tsym;
4381 replace (texp); // NB: throws nothing ...
4382 if (tsym->saved_conversion_error) // ... but this is how we know it happened.
a43ba433 4383 {
d48bc7eb
JS
4384 if (q.sess.verbose>2)
4385 {
e26c2f83 4386 for (const semantic_error *c = tsym->saved_conversion_error;
d48bc7eb 4387 c != 0;
c081af73 4388 c = c->get_chain()) {
4c5d9906 4389 clog << _("variable location problem [man error::dwarf]: ") << c->what() << endl;
d48bc7eb
JS
4390 }
4391 }
4392
4393 pf->raw_components += "=?";
a43ba433 4394 }
d48bc7eb
JS
4395 else
4396 {
4397 pf->raw_components += format;
4398 pf->args.push_back(texp);
4399 }
a7999c82 4400 }
d48bc7eb
JS
4401 while (dwarf_siblingof (&result, &result) == 0);
4402 }
a7999c82 4403 }
2cb3fe26 4404
a7999c82 4405 pf->components = print_format::string_to_components(pf->raw_components);
140be17a 4406 pf->type = pe_string;
a7999c82
JS
4407 provide (pf);
4408}
4409
2cb3fe26 4410
bd1fcbad
YZ
4411void
4412dwarf_var_expanding_visitor::visit_atvar_op (atvar_op *e)
4413{
4414 // Fill in our current module context if needed
4415 if (e->module.empty())
4416 e->module = q.dw.module_name;
4417
4418 if (e->module == q.dw.module_name && e->cu_name.empty())
4419 {
4420 // process like any other local
4421 // e->sym_name() will do the right thing
4422 visit_target_symbol(e);
4423 return;
4424 }
4425
4426 var_expanding_visitor::visit_atvar_op(e);
4427}
4428
4429
a7999c82
JS
4430void
4431dwarf_var_expanding_visitor::visit_target_symbol (target_symbol *e)
4432{
bd1fcbad 4433 assert(e->name.size() > 0 && (e->name[0] == '$' || e->name == "@var"));
a7999c82 4434 visited = true;
30263a73
FCE
4435 bool defined_being_checked = (defined_ops.size() > 0 && (defined_ops.top()->operand == e));
4436 // In this mode, we avoid hiding errors or generating extra code such as for .return saved $vars
a7999c82 4437
70208613 4438 try
a7999c82 4439 {
c69a87e0
FCE
4440 bool lvalue = is_active_lvalue(e);
4441 if (lvalue && !q.sess.guru_mode)
dc09353a 4442 throw SEMANTIC_ERROR(_("write to target variable not permitted; need stap -g"), e->tok);
2cb3fe26 4443
100a540e 4444 // XXX: process $context vars should be writable
70208613 4445
c69a87e0
FCE
4446 // See if we need to generate a new probe to save/access function
4447 // parameters from a return probe. PR 1382.
4448 if (q.has_return
4449 && !defined_being_checked
4a865280 4450 && (strverscmp(sess.compatible.c_str(), "4.1") < 0 || e->name != "@var")
277c21bc
JS
4451 && e->name != "$return" // not the special return-value variable handled below
4452 && e->name != "$$return") // nor the other special variable handled below
c69a87e0
FCE
4453 {
4454 if (lvalue)
dc09353a 4455 throw SEMANTIC_ERROR(_("write to target variable not permitted in .return probes"), e->tok);
b3b87c60
MC
4456 // PR14924: discourage this syntax
4457 stringstream expr;
4458 e->print(expr);
4459 q.sess.print_warning(_F("confusing usage, consider @entry(%s) in .return probe", expr.str().c_str()), e->tok);
c69a87e0
FCE
4460 visit_target_symbol_saved_return(e);
4461 return;
4462 }
e57b735a 4463
277c21bc
JS
4464 if (e->name == "$$vars" || e->name == "$$parms" || e->name == "$$locals"
4465 || (q.has_return && (e->name == "$$return")))
c69a87e0
FCE
4466 {
4467 if (lvalue)
dc09353a 4468 throw SEMANTIC_ERROR(_("cannot write to context variable"), e->tok);
70208613 4469
c69a87e0 4470 if (e->addressof)
dc09353a 4471 throw SEMANTIC_ERROR(_("cannot take address of context variable"), e->tok);
70208613 4472
5f36109e
JS
4473 e->assert_no_components("dwarf", true);
4474
c69a87e0
FCE
4475 visit_target_symbol_context(e);
4476 return;
4477 }
70208613 4478
04c8e51b 4479 // Everything else (pretty-printed vars, and context vars) require a
1c753c3a 4480 // scope_die in which to search for them. If produce an error.
04c8e51b 4481 if (null_die(scope_die))
1c753c3a
FCE
4482 throw SEMANTIC_ERROR(_F("debuginfo scope not found for module '%s', cannot resolve context variable [man error::dwarf]",
4483 q.dw.module_name.c_str()), e->tok);
04c8e51b 4484
c4965ad9 4485 if (e->check_pretty_print (lvalue))
5f36109e 4486 {
277c21bc 4487 if (q.has_return && (e->name == "$return"))
5f36109e
JS
4488 {
4489 dwarf_pretty_print dpp (q.dw, scope_die, addr,
9c141eca 4490 q.has_process, *e, lvalue);
5f36109e
JS
4491 dpp.expand()->visit(this);
4492 }
4493 else
4494 {
4495 dwarf_pretty_print dpp (q.dw, getscopes(e), addr,
cc9001af 4496 e->sym_name(),
9c141eca 4497 q.has_process, *e, lvalue);
5f36109e
JS
4498 dpp.expand()->visit(this);
4499 }
4500 return;
4501 }
4502
1c0be8c7 4503 bool userspace_p = q.has_process;
0dbac951
RH
4504 location_context ctx(e);
4505 ctx.pc = addr;
4506 ctx.userspace_p = userspace_p;
70208613 4507
5552db3a
FCE
4508 // NB: pass the ctx.e (copied/rewritten veraion e, not orig_e),
4509 // so [x] index expressions have their intra-synthetic-function names
40a393cd 4510 Dwarf_Die endtype;
277c21bc 4511 if (q.has_return && (e->name == "$return"))
5552db3a 4512 q.dw.literal_stmt_for_return (ctx, scope_die, ctx.e, lvalue, &endtype);
e19fda4e 4513 else
0dbac951 4514 q.dw.literal_stmt_for_local (ctx, getscopes(e), e->sym_name(),
5552db3a 4515 ctx.e, lvalue, &endtype);
70208613 4516
b3627d9a
JU
4517 q.dw.sess.globals.insert(q.dw.sess.globals.end(),
4518 ctx.globals.begin(),
4519 ctx.globals.end());
4520
4521 for (auto it = ctx.entry_probes.begin(); it != ctx.entry_probes.end(); ++it)
68bd23fd 4522 {
b3627d9a
JU
4523 auto res = entry_probes.find(it->first);
4524 if (res == entry_probes.end())
4525 entry_probes.insert(std::pair<Dwarf_Addr, block *>(it->first, it->second));
4526 else
4527 res->second = new block(res->second, it->second);
68bd23fd
JU
4528 }
4529
0dbac951 4530 string fname = (string(lvalue ? "_dwarf_tvar_set" : "_dwarf_tvar_get")
dd8cb30b 4531 + "_" + escaped_identifier_string (e->sym_name())
0dbac951
RH
4532 + "_" + lex_cast(tick++));
4533
4534 functioncall* n = synthetic_embedded_deref_call(q.dw, ctx, fname,
4535 &endtype, userspace_p,
4536 lvalue);
70208613 4537
c69a87e0 4538 if (lvalue)
f55efafe 4539 provide_lvalue_call (n);
70208613 4540
8444e5eb 4541 provide(n); // allow recursion to $var1[$var2] subexpressions
66d284f4
FCE
4542 }
4543 catch (const semantic_error& er)
4544 {
9fab2262
JS
4545 // We suppress this error message, and pass the unresolved
4546 // target_symbol to the next pass. We hope that this value ends
4547 // up not being referenced after all, so it can be optimized out
4548 // quietly.
5e50524f
FCE
4549 if (sess.verbose > 3)
4550 clog << "chaining to " << *e->tok << endl
4551 << sess.build_error_msg(er) << endl;
1af1e62d 4552 e->chain (er);
9fab2262 4553 provide (e);
66d284f4 4554 }
77de5e9e
GH
4555}
4556
4557
c24447be
JS
4558void
4559dwarf_var_expanding_visitor::visit_cast_op (cast_op *e)
4560{
4561 // Fill in our current module context if needed
4562 if (e->module.empty())
4563 e->module = q.dw.module_name;
4564
4565 var_expanding_visitor::visit_cast_op(e);
4566}
4567
4568
8cc799a5
JS
4569void
4570dwarf_var_expanding_visitor::visit_entry_op (entry_op *e)
4571{
4572 expression *repl = e;
09dd8f07
FCE
4573 bool defined_being_checked = (defined_ops.size() > 0 && (defined_ops.top()->operand == e));
4574 // In this mode, we avoid hiding errors or generating extra code such as for .return saved $vars
4575
8cc799a5
JS
4576 if (q.has_return)
4577 {
13ffabda
FCE
4578 // NB: don't expand the operand here, as if it weren't a return
4579 // probe. The original operand expression is transcribed into
4580 // the synthetic .call probe that gen_mapped_saved_return calls.
4581 // If we were to expand it here, we may e.g. map @perf("...") to
4582 // __perf_read_... prematurely & incorrectly. PR20416
8cc799a5 4583
09dd8f07
FCE
4584 // NB: but ... we sort of want to do a trial-expansion, just to
4585 // see if the contents are rejected, e.g. with a $var-undefined
4586 // error, so that the failure can propagate back up to a containing
4587 // @defined(). PR20821
4588
4589 if (defined_being_checked)
b3b87c60 4590 {
09dd8f07
FCE
4591 save_and_restore<bool> temp_return (& q.has_return, false);
4592 replace (e->operand); // don't generate any @entry machinery!
4593
4594 // propagate the replaced operand upward; it may be a
4595 // target_symbol and have a saved_conversion_error; we
4596 // also don't want to expand @defined(@entry(...)) into
4597 // a full synthetic probe goo.
4598 repl = e->operand;
4599 }
b3b87c60
MC
4600 else
4601 {
09dd8f07
FCE
4602 // XXX it would be nice to use gen_kretprobe_saved_return when available,
4603 // but it requires knowing the types already, which is problematic for
4604 // arbitrary expressons.
4605
4606 repl = gen_mapped_saved_return (e->operand, "entry");
b3b87c60 4607 }
8cc799a5
JS
4608 }
4609 provide (repl);
4610}
4611
09dd8f07 4612
3689db05
SC
4613void
4614dwarf_var_expanding_visitor::visit_perf_op (perf_op *e)
4615{
47d349b1 4616 string e_lit_val = e->operand->value;
b78a0fbb 4617
3689db05 4618 add_block = new block;
9eaaceaa 4619 add_block->tok = e->tok;
3689db05
SC
4620
4621 systemtap_session &s = this->q.sess;
3689db05 4622 // Find the associated perf.counter probe
b4662f6b
JS
4623 auto it = s.perf_counters.begin();
4624 for (; it != s.perf_counters.end(); it++)
3689db05
SC
4625 if ((*it).first == e_lit_val)
4626 {
aa2c662f
SC
4627 // if perf .process("name") omitted, then set it to this process name
4628 if ((*it).second.length() == 0)
4629 (*it).second = this->q.user_path;
4630 if ((*it).second == this->q.user_path)
3689db05
SC
4631 break;
4632 }
4633
4634 if (it != s.perf_counters.end())
4635 {
aa2c662f 4636 perf_counter_refs.insert((*it).first);
3689db05
SC
4637 // __perf_read_N is assigned in the probe prologue
4638 symbol* sym = new symbol;
4653caf1 4639 sym->tok = e->tok;
3689db05
SC
4640 sym->name = "__perf_read_" + (*it).first;
4641 provide (sym);
4642 }
4643 else
dc09353a 4644 throw SEMANTIC_ERROR(_F("perf counter '%s' not defined", e_lit_val.c_str()));
3689db05
SC
4645}
4646
8cc799a5 4647
729455a7
JS
4648vector<Dwarf_Die>&
4649dwarf_var_expanding_visitor::getscopes(target_symbol *e)
4650{
4651 if (scopes.empty())
4652 {
04c8e51b 4653 if(!null_die(scope_die))
f25a9197 4654 scopes = q.dw.getscopes(scope_die);
729455a7 4655 if (scopes.empty())
b530b5b3
LB
4656 //throw semantic_error (_F("unable to find any scopes containing %d", addr), e->tok);
4657 // ((scope_die == NULL) ? "" : (string (" in ") + (dwarf_diename(scope_die) ?: "<unknown>") + "(" + (dwarf_diename(q.dw.cu) ?: "<unknown>") ")" ))
dc09353a 4658 throw SEMANTIC_ERROR ("unable to find any scopes containing "
729455a7 4659 + lex_cast_hex(addr)
04c8e51b 4660 + (null_die(scope_die) ? ""
729455a7
JS
4661 : (string (" in ")
4662 + (dwarf_diename(scope_die) ?: "<unknown>")
4663 + "(" + (dwarf_diename(q.dw.cu) ?: "<unknown>")
4664 + ")"))
4665 + " while searching for local '"
cc9001af 4666 + e->sym_name() + "'",
729455a7
JS
4667 e->tok);
4668 }
4669 return scopes;
4670}
4671
4672
5f36109e
JS
4673struct dwarf_cast_expanding_visitor: public var_expanding_visitor
4674{
5f36109e 4675 dwarf_builder& db;
d77be314 4676 map<string,string> compiled_headers;
5f36109e
JS
4677
4678 dwarf_cast_expanding_visitor(systemtap_session& s, dwarf_builder& db):
16aa72d9 4679 var_expanding_visitor(s), db(db) {}
5f36109e
JS
4680 void visit_cast_op (cast_op* e);
4681 void filter_special_modules(string& module);
4682};
4683
4684
c4ce66a1
JS
4685struct dwarf_cast_query : public base_query
4686{
946e1a48 4687 cast_op& e;
c4ce66a1 4688 const bool lvalue;
5f36109e
JS
4689 const bool userspace_p;
4690 functioncall*& result;
c4ce66a1 4691
5f36109e
JS
4692 dwarf_cast_query(dwflpp& dw, const string& module, cast_op& e, bool lvalue,
4693 const bool userspace_p, functioncall*& result):
abb41d92 4694 base_query(dw, module), e(e), lvalue(lvalue),
5f36109e 4695 userspace_p(userspace_p), result(result) {}
c4ce66a1
JS
4696
4697 void handle_query_module();
822a6a3d 4698 void query_library (const char *) {}
dabd71bb 4699 void query_plt (const char *, size_t) {}
c4ce66a1
JS
4700};
4701
4702
c4ce66a1
JS
4703void
4704dwarf_cast_query::handle_query_module()
4705{
5f36109e
JS
4706 static unsigned tick = 0;
4707
4708 if (result)
c4ce66a1
JS
4709 return;
4710
ea1e477a 4711 // look for the type in any CU
a44a7cb5 4712 Dwarf_Die* type_die = NULL;
47d349b1 4713 string tns = e.type_name;
a3e980f9
FCE
4714
4715 if (startswith(tns, "class "))
a44a7cb5
JS
4716 {
4717 // normalize to match dwflpp::global_alias_caching_callback
47d349b1 4718 string struct_name = "struct " + (string)e.type_name.substr(6);
a44a7cb5
JS
4719 type_die = dw.declaration_resolve_other_cus(struct_name);
4720 }
4721 else
a3e980f9 4722 type_die = dw.declaration_resolve_other_cus(tns);
a44a7cb5
JS
4723
4724 // NB: We now index the types as "struct name"/"union name"/etc. instead of
4725 // just "name". But since we didn't require users to be explicit before, and
4726 // actually sort of discouraged it, we must be flexible now. So if a lookup
4727 // fails with a bare name, try augmenting it.
4728 if (!type_die &&
a3e980f9
FCE
4729 !startswith(tns, "class ") &&
4730 !startswith(tns, "struct ") &&
4731 !startswith(tns, "union ") &&
4732 !startswith(tns, "enum "))
a44a7cb5 4733 {
a3e980f9 4734 type_die = dw.declaration_resolve_other_cus("struct " + tns);
a44a7cb5 4735 if (!type_die)
a3e980f9 4736 type_die = dw.declaration_resolve_other_cus("union " + tns);
a44a7cb5 4737 if (!type_die)
a3e980f9 4738 type_die = dw.declaration_resolve_other_cus("enum " + tns);
a44a7cb5
JS
4739 }
4740
ea1e477a
JS
4741 if (!type_die)
4742 return;
c4ce66a1 4743
0dbac951
RH
4744 location_context ctx(&e, e.operand);
4745 ctx.userspace_p = userspace_p;
4746
66373b2e
AM
4747 // ctx may require extra information for --runtime=bpf
4748 symbol *s;
4749 bpf_context_vardecl *v;
4750 if ((s = dynamic_cast<symbol *>(e.operand))
4751 && (v = dynamic_cast<bpf_context_vardecl *>(s->referent)))
4752 ctx.adapt_pointer_to_bpf(v->size, v->offset, v->is_signed);
4753
40a393cd 4754 Dwarf_Die endtype;
0dbac951 4755 bool ok = false;
5f36109e 4756
ea1e477a 4757 try
c4ce66a1 4758 {
ea1e477a
JS
4759 Dwarf_Die cu_mem;
4760 dw.focus_on_cu(dwarf_diecu(type_die, &cu_mem, NULL, NULL));
5f36109e 4761
c4965ad9 4762 if (e.check_pretty_print (lvalue))
5f36109e 4763 {
9c141eca
DS
4764 dwarf_pretty_print dpp(dw, type_die, e.operand, true, userspace_p,
4765 e, lvalue);
5f36109e
JS
4766 result = dpp.expand();
4767 return;
4768 }
4769
5552db3a 4770 ok = dw.literal_stmt_for_pointer (ctx, type_die, ctx.e, lvalue, &endtype);
ea1e477a
JS
4771 }
4772 catch (const semantic_error& er)
4773 {
4774 // NB: we can have multiple errors, since a @cast
1af1e62d
JS
4775 // may be attempted using several different modules:
4776 // @cast(ptr, "type", "module1:module2:...")
4777 e.chain (er);
c4ce66a1 4778 }
c4ce66a1 4779
0dbac951 4780 if (!ok)
5f36109e 4781 return;
c4ce66a1 4782
5f36109e 4783 string fname = (string(lvalue ? "_dwarf_cast_set" : "_dwarf_cast_get")
cc9001af 4784 + "_" + e.sym_name()
5f36109e 4785 + "_" + lex_cast(tick++));
0dbac951
RH
4786 result = synthetic_embedded_deref_call(dw, ctx, fname, &endtype,
4787 userspace_p, lvalue, e.operand);
5f36109e 4788}
c4ce66a1
JS
4789
4790
fb0274bc
JS
4791void dwarf_cast_expanding_visitor::filter_special_modules(string& module)
4792{
d90053e7 4793 // look for "<path/to/header>" or "kernel<path/to/header>"
fb0274bc 4794 // for those cases, build a module including that header
d90053e7 4795 if (module[module.size() - 1] == '>' &&
60d98537 4796 (module[0] == '<' || startswith(module, "kernel<")))
fb0274bc 4797 {
d77be314 4798 string header = module;
b4662f6b 4799 auto it = compiled_headers.find(header);
d77be314
JS
4800 if (it != compiled_headers.end())
4801 {
4802 module = it->second;
4803 return;
4804 }
4805
fb0274bc 4806 string cached_module;
16aa72d9 4807 if (sess.use_cache)
fb0274bc
JS
4808 {
4809 // see if the cached module exists
16aa72d9
FCE
4810 cached_module = find_typequery_hash(sess, module);
4811 if (!cached_module.empty() && !sess.poison_cache)
fb0274bc
JS
4812 {
4813 int fd = open(cached_module.c_str(), O_RDONLY);
4814 if (fd != -1)
4815 {
16aa72d9 4816 if (sess.verbose > 2)
b530b5b3
LB
4817 //TRANSLATORS: Here we're using a cached module.
4818 clog << _("Pass 2: using cached ") << cached_module << endl;
d77be314 4819 compiled_headers[header] = module = cached_module;
fb0274bc
JS
4820 close(fd);
4821 return;
4822 }
4823 }
4824 }
4825
4826 // no cached module, time to make it
16aa72d9 4827 if (make_typequery(sess, module) == 0)
fb0274bc 4828 {
e16dc041 4829 // try to save typequery in the cache
16aa72d9
FCE
4830 if (sess.use_cache)
4831 copy_file(module, cached_module, sess.verbose > 2);
d77be314 4832 compiled_headers[header] = module;
fb0274bc
JS
4833 }
4834 }
4835}
4836
4837
c4ce66a1
JS
4838void dwarf_cast_expanding_visitor::visit_cast_op (cast_op* e)
4839{
4840 bool lvalue = is_active_lvalue(e);
16aa72d9 4841 if (lvalue && !sess.guru_mode)
dc09353a 4842 throw SEMANTIC_ERROR(_("write to @cast context variable not permitted; need stap -g"), e->tok);
c4ce66a1
JS
4843
4844 if (e->module.empty())
4845 e->module = "kernel"; // "*" may also be reasonable to search all kernel modules
4846
5f36109e 4847 functioncall* result = NULL;
8b31197b
JS
4848
4849 // split the module string by ':' for alternatives
4850 vector<string> modules;
47d349b1 4851 tokenize(e->module, modules, ":");
b5a0dd41 4852 bool userspace_p=false; // PR10601
5f36109e 4853 for (unsigned i = 0; !result && i < modules.size(); ++i)
c4ce66a1 4854 {
8b31197b 4855 string& module = modules[i];
fb0274bc 4856 filter_special_modules(module);
abb41d92 4857
c4ce66a1
JS
4858 // NB: This uses '/' to distinguish between kernel modules and userspace,
4859 // which means that userspace modules won't get any PATH searching.
4860 dwflpp* dw;
707bf35e
JS
4861 try
4862 {
b5a0dd41
FCE
4863 userspace_p=is_user_module (module);
4864 if (! userspace_p)
707bf35e
JS
4865 {
4866 // kernel or kernel module target
16aa72d9 4867 dw = db.get_kern_dw(sess, module);
707bf35e
JS
4868 }
4869 else
4870 {
16aa72d9
FCE
4871 module = find_executable (module, "", sess.sysenv); // canonicalize it
4872 dw = db.get_user_dw(sess, module);
707bf35e
JS
4873 }
4874 }
4875 catch (const semantic_error& er)
4876 {
4877 /* ignore and go to the next module */
4878 continue;
4879 }
c4ce66a1 4880
5f36109e 4881 dwarf_cast_query q (*dw, module, *e, lvalue, userspace_p, result);
06de3a04 4882 dw->iterate_over_modules<base_query>(&query_module, &q);
c4ce66a1 4883 }
abb41d92 4884
5f36109e 4885 if (!result)
c4ce66a1 4886 {
946e1a48
JS
4887 // We pass the unresolved cast_op to the next pass, and hope
4888 // that this value ends up not being referenced after all, so
4889 // it can be optimized out quietly.
c4ce66a1
JS
4890 provide (e);
4891 return;
4892 }
4893
c4ce66a1 4894 if (lvalue)
f55efafe 4895 provide_lvalue_call (result);
c4ce66a1 4896
5f36109e 4897 result->visit (this);
77de5e9e
GH
4898}
4899
4900
37efef73
JS
4901static bool resolve_pointer_type(Dwarf_Die& die, bool& isptr);
4902
4903exp_type_dwarf::exp_type_dwarf(dwflpp* dw, Dwarf_Die* die,
4904 bool userspace_p, bool addressof):
4905 dw(dw), die(*die), userspace_p(userspace_p), is_pointer(false)
4906{
4907 // is_pointer tells us whether a value is a pointer to the given type, so we
4908 // can dereference it; otherwise it will be treated as an end point.
4909 if (addressof)
4910 // we're already looking at the pointed-to type
4911 is_pointer = true;
4912 else
4913 // use the same test as tracepoints to see what we have
4914 resolve_pointer_type(this->die, is_pointer);
4915}
4916
4917
66facfa3
JS
4918functioncall *
4919exp_type_dwarf::expand(autocast_op* e, bool lvalue)
251707c8 4920{
66facfa3 4921 static unsigned tick = 0;
251707c8 4922
66facfa3
JS
4923 try
4924 {
37efef73
JS
4925 // make sure we're not dereferencing base types or void
4926 bool deref_p = is_pointer && !null_die(&die);
4927 if (!deref_p)
4928 e->assert_no_components("autocast", true);
4929
fb437f4e
JS
4930 if (lvalue && !dw->sess.guru_mode)
4931 throw SEMANTIC_ERROR(_("write not permitted; need stap -g"), e->tok);
4932
37efef73
JS
4933 if (e->components.empty())
4934 {
4935 if (e->addressof)
4936 throw SEMANTIC_ERROR(_("cannot take address of tracepoint variable"), e->tok);
4937
4938 // no components and no addressof? how did this autocast come to be?
4939 throw SEMANTIC_ERROR(_("internal error: no-op autocast encountered"), e->tok);
4940 }
4941
66facfa3 4942 Dwarf_Die cu_mem;
611a848e
JS
4943 if (!null_die(&die))
4944 dw->focus_on_cu(dwarf_diecu(&die, &cu_mem, NULL, NULL));
251707c8 4945
c4965ad9 4946 if (e->check_pretty_print (lvalue))
66facfa3 4947 {
9c141eca
DS
4948 dwarf_pretty_print dpp(*dw, &die, e->operand, deref_p, userspace_p,
4949 *e, lvalue);
66facfa3
JS
4950 return dpp.expand();
4951 }
4952
0dbac951
RH
4953 location_context ctx(e, e->operand);
4954 ctx.userspace_p = userspace_p;
66facfa3 4955 Dwarf_Die endtype;
251707c8 4956
13b3a538 4957 dw->literal_stmt_for_pointer (ctx, &die, ctx.e, lvalue, &endtype);
0dbac951
RH
4958
4959 string fname = (string(lvalue ? "_dwarf_autocast_set"
4960 : "_dwarf_autocast_get")
66facfa3
JS
4961 + "_" + lex_cast(tick++));
4962
0dbac951
RH
4963 return synthetic_embedded_deref_call(*dw, ctx, fname, &endtype,
4964 userspace_p, lvalue, e->operand);
66facfa3
JS
4965 }
4966 catch (const semantic_error &er)
4967 {
5e50524f
FCE
4968 if (dw->sess.verbose > 3)
4969 clog << "chaining to " << *e->tok << endl
4970 << dw->sess.build_error_msg(er) << endl;
66facfa3
JS
4971 e->chain (er);
4972 return NULL;
4973 }
251707c8
JS
4974}
4975
4976
bd1fcbad
YZ
4977struct dwarf_atvar_expanding_visitor: public var_expanding_visitor
4978{
bd1fcbad
YZ
4979 dwarf_builder& db;
4980
4981 dwarf_atvar_expanding_visitor(systemtap_session& s, dwarf_builder& db):
16aa72d9 4982 var_expanding_visitor(s), db(db) {}
bd1fcbad
YZ
4983 void visit_atvar_op (atvar_op* e);
4984};
4985
4986
4987struct dwarf_atvar_query: public base_query
4988{
4989 atvar_op& e;
4990 const bool userspace_p, lvalue;
4991 functioncall*& result;
4992 unsigned& tick;
4993 const string cu_name_pattern;
4994
4995 dwarf_atvar_query(dwflpp& dw, const string& module, atvar_op& e,
4996 const bool userspace_p, const bool lvalue,
4997 functioncall*& result,
4998 unsigned& tick):
4999 base_query(dw, module), e(e),
5000 userspace_p(userspace_p), lvalue(lvalue), result(result),
47d349b1 5001 tick(tick), cu_name_pattern(string("*/") + (string)e.cu_name) {}
bd1fcbad
YZ
5002
5003 void handle_query_module ();
5004 void query_library (const char *) {}
dabd71bb 5005 void query_plt (const char *, size_t) {}
5c378838 5006 static int atvar_query_cu (Dwarf_Die *cudie, dwarf_atvar_query *q);
bd1fcbad
YZ
5007};
5008
5009
5010int
5c378838 5011dwarf_atvar_query::atvar_query_cu (Dwarf_Die * cudie, dwarf_atvar_query *q)
bd1fcbad 5012{
bd1fcbad
YZ
5013 if (! q->e.cu_name.empty())
5014 {
c60517ca 5015 const char *die_name = dwarf_diename(cudie) ?: "";
47d349b1 5016 string cns = q->e.cu_name;
a3e980f9 5017 if (strcmp(die_name, cns.c_str()) != 0 // Perfect match
bd1fcbad
YZ
5018 && fnmatch(q->cu_name_pattern.c_str(), die_name, 0) != 0)
5019 {
5020 return DWARF_CB_OK;
5021 }
5022 }
5023
5024 try
5025 {
5026 vector<Dwarf_Die> scopes(1, *cudie);
5027
5028 q->dw.focus_on_cu (cudie);
5029
c4965ad9 5030 if (q->e.check_pretty_print (q->lvalue))
bd1fcbad
YZ
5031 {
5032 dwarf_pretty_print dpp (q->dw, scopes, 0, q->e.sym_name(),
9c141eca 5033 q->userspace_p, q->e, q->lvalue);
bd1fcbad
YZ
5034 q->result = dpp.expand();
5035 return DWARF_CB_ABORT;
5036 }
5037
0dbac951
RH
5038 location_context ctx(&q->e);
5039 ctx.userspace_p = q->userspace_p;
40a393cd 5040 Dwarf_Die endtype;
bd1fcbad 5041
0dbac951 5042 bool ok = q->dw.literal_stmt_for_local (ctx, scopes, q->e.sym_name(),
5552db3a 5043 ctx.e, q->lvalue, &endtype);
0dbac951
RH
5044
5045 if (!ok)
bd1fcbad
YZ
5046 return DWARF_CB_OK;
5047
5048 string fname = (string(q->lvalue ? "_dwarf_tvar_set"
5049 : "_dwarf_tvar_get")
5050 + "_" + q->e.sym_name()
5051 + "_" + lex_cast(q->tick++));
5052
0dbac951
RH
5053 q->result = synthetic_embedded_deref_call (q->dw, ctx, fname, &endtype,
5054 q->userspace_p, q->lvalue);
bd1fcbad
YZ
5055 }
5056 catch (const semantic_error& er)
5057 {
5058 // Here we suppress the error because we often just have too many
5059 // when scanning all the CUs.
5060 return DWARF_CB_OK;
5061 }
5062
5063 if (q->result) {
5064 return DWARF_CB_ABORT;
5065 }
5066
5067 return DWARF_CB_OK;
5068}
5069
5070
5071void
5072dwarf_atvar_query::handle_query_module ()
5073{
5074
5075 dw.iterate_over_cus(atvar_query_cu, this, false);
5076}
5077
5078
5079void
5080dwarf_atvar_expanding_visitor::visit_atvar_op (atvar_op* e)
5081{
5082 const bool lvalue = is_active_lvalue(e);
16aa72d9 5083 if (lvalue && !sess.guru_mode)
dc09353a 5084 throw SEMANTIC_ERROR(_("write to @var variable not permitted; "
bd1fcbad
YZ
5085 "need stap -g"), e->tok);
5086
5087 if (e->module.empty())
5088 e->module = "kernel";
5089
5090 functioncall* result = NULL;
5091
5092 // split the module string by ':' for alternatives
5093 vector<string> modules;
47d349b1 5094 tokenize(e->module, modules, ":");
bd1fcbad
YZ
5095 bool userspace_p = false;
5096 for (unsigned i = 0; !result && i < modules.size(); ++i)
5097 {
5098 string& module = modules[i];
5099
5100 dwflpp* dw;
5101 try
5102 {
5103 userspace_p = is_user_module(module);
5104 if (!userspace_p)
5105 {
5106 // kernel or kernel module target
16aa72d9 5107 dw = db.get_kern_dw(sess, module);
bd1fcbad
YZ
5108 }
5109 else
5110 {
16aa72d9
FCE
5111 module = find_executable(module, "", sess.sysenv);
5112 dw = db.get_user_dw(sess, module);
bd1fcbad
YZ
5113 }
5114 }
5115 catch (const semantic_error& er)
5116 {
5117 /* ignore and go to the next module */
5118 continue;
5119 }
5120
5121 dwarf_atvar_query q (*dw, module, *e, userspace_p, lvalue, result, tick);
06de3a04 5122 dw->iterate_over_modules<base_query>(&query_module, &q);
bd1fcbad
YZ
5123
5124 if (result)
5125 {
16aa72d9 5126 sess.unwindsym_modules.insert(module);
bd1fcbad
YZ
5127
5128 if (lvalue)
f55efafe 5129 provide_lvalue_call (result);
bd1fcbad
YZ
5130
5131 result->visit(this);
5132 return;
5133 }
5134
5135 /* Unable to find the variable in the current module, so we chain
5136 * an error in atvar_op */
a3e980f9
FCE
5137 string esn = e->sym_name();
5138 string mn = module;
47d349b1 5139 string cun = e->cu_name;
dc09353a 5140 semantic_error er(ERR_SRC, _F("unable to find global '%s' in %s%s%s",
a3e980f9
FCE
5141 esn.c_str(), mn.c_str(),
5142 cun.empty() ? "" : _(", in "),
5143 cun.c_str()));
5e50524f
FCE
5144 if (sess.verbose > 3)
5145 clog << "chaining to " << *e->tok << endl
5146 << sess.build_error_msg(er) << endl;
bd1fcbad
YZ
5147 e->chain (er);
5148 }
5149
5150 provide(e);
5151}
5152
5153
b8da0ad1 5154void
da1fa82f 5155dwarf_derived_probe::printsig (ostream& o, bool nest) const
b8da0ad1
FCE
5156{
5157 // Instead of just printing the plain locations, we add a PC value
5158 // as a comment as a way of telling e.g. apart multiple inlined
5159 // function instances. This is distinct from the verbose/clog
5160 // output, since this part goes into the cache hash calculations.
5161 sole_location()->print (o);
24897818
DS
5162 if (symbol_name != "")
5163 o << " /* pc=<" << symbol_name << "+" << offset << "> */";
5164 else
5165 o << " /* pc=" << section << "+0x" << hex << addr << dec << " */";
da1fa82f
SP
5166
5167 if (nest)
5168 printsig_nested (o);
b8da0ad1
FCE
5169}
5170
5171
5172
dc38c0ae 5173void
b20febf3
FCE
5174dwarf_derived_probe::join_group (systemtap_session& s)
5175{
af234c40
JS
5176 // skip probes which are paired entry-handlers
5177 if (!has_return && (saved_longs || saved_strings))
5178 return;
5179
a7fbce1f
DS
5180 if (! s.generic_kprobe_derived_probes)
5181 s.generic_kprobe_derived_probes = new generic_kprobe_derived_probe_group ();
5182 s.generic_kprobe_derived_probes->enroll (this);
5183 this->group = s.generic_kprobe_derived_probes;
ca6d3b0f 5184 if (has_return && entry_handler)
a7fbce1f 5185 entry_handler->group = s.generic_kprobe_derived_probes;
b20febf3
FCE
5186}
5187
5188
2b69faaf
JS
5189static bool
5190kernel_supports_inode_uprobes(systemtap_session& s)
5191{
5192 // The arch-supports is new to the builtin inode-uprobes, so it makes a
5193 // reasonable indicator of the new API. Else we'll need an autoconf...
64e807c2 5194 // see also buildrun.cxx:kernel_built_uprobs()
2b69faaf
JS
5195 return (s.kernel_config["CONFIG_ARCH_SUPPORTS_UPROBES"] == "y"
5196 && s.kernel_config["CONFIG_UPROBES"] == "y");
5197}
5198
5199
3667d615
JS
5200static bool
5201kernel_supports_inode_uretprobes(systemtap_session& s)
5202{
766013af
JS
5203 // We need inode-uprobes first, then look for a sign of uretprobes. The only
5204 // non-static function at present is arch_uretprobe_hijack_return_addr.
3667d615 5205 return kernel_supports_inode_uprobes(s) &&
af9e147f 5206 (s.kernel_functions.count("arch_uretprobe_hijack_return_addr") > 0);
3667d615
JS
5207}
5208
5209
5261f7ab
DS
5210void
5211check_process_probe_kernel_support(systemtap_session& s)
5212{
5213 // If we've got utrace, we're good to go.
5214 if (s.kernel_config["CONFIG_UTRACE"] == "y")
5215 return;
5216
8c021542
DS
5217 // We don't have utrace. For process probes that aren't
5218 // uprobes-based, we just need the task_finder. The task_finder
b266d318
DS
5219 // needs CONFIG_TRACEPOINTS and specific tracepoints. There is a
5220 // specific autoconf test for its needs.
8c021542
DS
5221 //
5222 // We'll just require CONFIG_TRACEPOINTS here as a quick-and-dirty
5223 // approximation.
5224 if (! s.need_uprobes && s.kernel_config["CONFIG_TRACEPOINTS"] == "y")
5225 return;
5226
d3e959b0
DS
5227 // For uprobes-based process probes, we need the task_finder plus
5228 // the builtin inode-uprobes.
8c021542
DS
5229 if (s.need_uprobes
5230 && s.kernel_config["CONFIG_TRACEPOINTS"] == "y"
d3e959b0 5231 && kernel_supports_inode_uprobes(s))
8c021542
DS
5232 return;
5233
dc09353a 5234 throw SEMANTIC_ERROR (_("process probes not available without kernel CONFIG_UTRACE or CONFIG_TRACEPOINTS/CONFIG_ARCH_SUPPORTS_UPROBES/CONFIG_UPROBES"));
5261f7ab
DS
5235}
5236
5237
45a63356
FCE
5238dwarf_derived_probe::dwarf_derived_probe(interned_string funcname,
5239 interned_string filename,
b20febf3 5240 int line,
91af0778 5241 // module & section specify a relocation
b20febf3
FCE
5242 // base for <addr>, unless section==""
5243 // (equivalently module=="kernel")
4766b1e6
JL
5244 // for userspace, it's a full path, for
5245 // modules, it's either a full path, or
5246 // the basename (e.g. 'btrfs')
45a63356
FCE
5247 interned_string module,
5248 interned_string section,
b20febf3
FCE
5249 // NB: dwfl_addr is the virtualized
5250 // address for this symbol.
5251 Dwarf_Addr dwfl_addr,
5252 // addr is the section-offset for
5253 // actual relocation.
5254 Dwarf_Addr addr,
5255 dwarf_query& q,
24897818
DS
5256 Dwarf_Die* scope_die /* may be null */,
5257 interned_string symbol_name,
5258 Dwarf_Addr offset)
a7fbce1f
DS
5259 : generic_kprobe_derived_probe (q.base_probe, q.base_loc, module, section,
5260 addr, q.has_return,
5261 q.has_maxactive, q.maxactive_val, "", offset),
63b4fd14 5262 path (q.path),
27dc09b1 5263 has_process (q.has_process),
c57ea854 5264 has_library (q.has_library),
b642c901
SC
5265 user_path (q.user_path),
5266 user_lib (q.user_lib),
a7fbce1f 5267 access_vars(false)
bd2b1e68 5268{
4766b1e6
JL
5269 // If we were given a fullpath to a kernel module, then get the simple name
5270 if (q.has_module && is_fully_resolved(module, q.dw.sess.sysroot, q.dw.sess.sysenv))
5271 this->module = modname_from_path(module);
5272
24897818
DS
5273 if (q.has_module && symbol_name != "")
5274 this->symbol_name = lex_cast(this->module) + ":" + lex_cast(symbol_name);
5275
cbc16973
AM
5276 if (q.sess.runtime_mode == systemtap_session::bpf_runtime && q.has_return)
5277 this->sym_name_for_bpf = funcname;
5278
b642c901
SC
5279 if (user_lib.size() != 0)
5280 has_library = true;
5281
6b66b9f7
JS
5282 if (q.has_process)
5283 {
5284 // We may receive probes on two types of ELF objects: ET_EXEC or ET_DYN.
5285 // ET_EXEC ones need no further relocation on the addr(==dwfl_addr), whereas
5286 // ET_DYN ones do (addr += run-time mmap base address). We tell these apart
5287 // by the incoming section value (".absolute" vs. ".dynamic").
5288 // XXX Assert invariants here too?
2b69faaf
JS
5289
5290 // inode-uprobes needs an offset rather than an absolute VM address.
4441e344 5291 // ditto for userspace runtimes (dyninst)
ac3af990 5292 if ((kernel_supports_inode_uprobes(q.dw.sess) || q.dw.sess.runtime_usermode_p()) &&
2b69faaf
JS
5293 section == ".absolute" && addr == dwfl_addr &&
5294 addr >= q.dw.module_start && addr < q.dw.module_end)
5295 this->addr = addr - q.dw.module_start;
6b66b9f7
JS
5296 }
5297 else
5298 {
5299 // Assert kernel relocation invariants
5300 if (section == "" && dwfl_addr != addr) // addr should be absolute
dc09353a 5301 throw SEMANTIC_ERROR (_("missing relocation basis"), tok);
6b66b9f7 5302 if (section != "" && dwfl_addr == addr) // addr should be an offset
dc09353a 5303 throw SEMANTIC_ERROR (_("inconsistent relocation address"), tok);
6b66b9f7 5304 }
2930abc7 5305
21beacc9
FCE
5306 // XXX: hack for strange g++/gcc's
5307#ifndef USHRT_MAX
5308#define USHRT_MAX 32767
5309#endif
5310
606fd9c8 5311 // Range limit maxactive() value
6b66b9f7 5312 if (has_maxactive && (maxactive_val < 0 || maxactive_val > USHRT_MAX))
dc09353a 5313 throw SEMANTIC_ERROR (_F("maxactive value out of range [0,%s]",
b530b5b3 5314 lex_cast(USHRT_MAX).c_str()), q.base_loc->components.front()->tok);
606fd9c8 5315
a7696882
JL
5316 // Expand target variables in the probe body. Even if the scope_die is
5317 // invalid, we still want to expand things such as $$vars/$$parms/etc...
5318 // (PR15999, PR16473). Access to specific context vars e.g. $argc will not be
5319 // expanded and will produce an error during the typeresolution_info pass.
5fa1d0a2 5320 {
5a617dc6
JL
5321 // PR14436: if we're expanding target variables in the probe body of a
5322 // .return probe, we need to make the expansion at the postprologue addr
5323 // instead (if any), which is then also the spot where the entry handler
5324 // probe is placed. (Note that at this point, a nonzero prologue_end
5325 // implies that it should be used, i.e. code is unoptimized).
5326 Dwarf_Addr handler_dwfl_addr = dwfl_addr;
5327 if (q.prologue_end != 0 && q.has_return)
5328 {
5329 handler_dwfl_addr = q.prologue_end;
5330 if (q.sess.verbose > 2)
5331 clog << _F("expanding .return vars at prologue_end (0x%s) "
5332 "rather than entrypc (0x%s)\n",
5333 lex_cast_hex(handler_dwfl_addr).c_str(),
5334 lex_cast_hex(dwfl_addr).c_str());
5335 }
3689db05 5336
16aa72d9
FCE
5337 // PR20672, there may be @defined()-guarded @entry() expressions
5338 // in the tree. If any @defined() maps to false, the visitor
5339 // needs to abort so that subsequent @entry()'s are not
5340 // processed (to generate synthetic .call etc. probes). We do a
5341 // a mini relaxation loop here.
5342 dwarf_var_expanding_visitor v (q, scope_die, handler_dwfl_addr);
5343 var_expand_const_fold_loop (q.sess, this->body, v);
5344
3689db05
SC
5345 // Propagate perf.counters so we can emit later
5346 this->perf_counter_refs = v.perf_counter_refs;
5347 // Emit local var used to save the perf counter read value
b4662f6b 5348 for (auto pcii = v.perf_counter_refs.begin();
3689db05
SC
5349 pcii != v.perf_counter_refs.end(); pcii++)
5350 {
3689db05 5351 // Find the associated perf counter probe
b4662f6b 5352 for (auto it = q.sess.perf_counters.begin();
3689db05
SC
5353 it != q.sess.perf_counters.end();
5354 it++)
aa2c662f 5355 if ((*it).first == (*pcii))
c92d3b42
FCE
5356 {
5357 vardecl* vd = new vardecl;
9d3a1cd6 5358 vd->name = vd->unmangled_name = "__perf_read_" + (*it).first;
c92d3b42
FCE
5359 vd->tok = this->tok;
5360 vd->set_arity(0, this->tok);
5361 vd->type = pe_long;
5362 vd->synthetic = true;
5363 this->locals.push_back (vd);
5364 break;
5365 }
3689db05
SC
5366 }
5367
6b66b9f7
JS
5368 if (!q.has_process)
5369 access_vars = v.visited;
37ebca01
FCE
5370
5371 // If during target-variable-expanding the probe, we added a new block
5372 // of code, add it to the start of the probe.
5373 if (v.add_block)
ba6f838d 5374 this->body = new block(v.add_block, this->body);
2260f4e3
FCE
5375
5376 // If when target-variable-expanding the probe, we need to synthesize a
5377 // sibling function-entry probe. We don't go through the whole probe derivation
5378 // business (PR10642) that could lead to wildcard/alias resolution, or for that
5379 // dwarf-induced duplication.
5380 if (v.add_call_probe)
37ebca01 5381 {
2260f4e3
FCE
5382 assert (q.has_return && !q.has_call);
5383
5384 // We temporarily replace q.base_probe.
09dd8f07
FCE
5385 save_and_restore<statement*> tmp_body (&q.base_probe->body, v.add_call_probe);
5386 save_and_restore<bool> tmp_return (&q.has_return, false);
5387 save_and_restore<bool> tmp_call (&q.has_call, true);
af234c40 5388
13ffabda
FCE
5389 // NB: any moved @entry(EXPR) bits will be expanded during this
5390 // nested *derived_probe ctor for the synthetic .call probe.
5391 // PR20416
da23eceb 5392 if (q.has_process)
5a617dc6
JL
5393 {
5394 // Place handler probe at the same addr as where the vars were
5395 // expanded (which may not be the same addr as the one for the
5396 // main retprobe, PR14436).
5397 Dwarf_Addr handler_addr = addr;
5398 if (handler_dwfl_addr != dwfl_addr)
5399 // adjust section offset by prologue_end-entrypc
5400 handler_addr += handler_dwfl_addr - dwfl_addr;
5401 entry_handler = new uprobe_derived_probe (funcname, filename,
5402 line, module, section,
5403 handler_dwfl_addr,
5404 handler_addr, q,
5405 scope_die);
5406 }
da23eceb 5407 else
af234c40
JS
5408 entry_handler = new dwarf_derived_probe (funcname, filename, line,
5409 module, section, dwfl_addr,
5410 addr, q, scope_die);
5411
4f4f173a
SP
5412 entry_handler->synthetic = true;
5413
af234c40
JS
5414 saved_longs = entry_handler->saved_longs = v.saved_longs;
5415 saved_strings = entry_handler->saved_strings = v.saved_strings;
5416
5417 q.results.push_back (entry_handler);
37ebca01 5418 }
68bd23fd 5419
b3627d9a 5420 for (auto it = v.entry_probes.begin(); it != v.entry_probes.end(); ++it)
68bd23fd 5421 {
b3627d9a
JU
5422 save_and_restore<statement*> tmp_body (&q.base_probe->body, it->second);
5423 save_and_restore<bool> tmp_function_num (&q.has_function_num, true);
5424 query_addr (it->first, &q);
68bd23fd
JU
5425 }
5426
a7696882
JL
5427 // Save the local variables for listing mode. If the scope_die is null,
5428 // local vars aren't accessible, so no need to invoke saveargs (PR10820).
bba368c5
JL
5429 if (!null_die(scope_die) &&
5430 q.sess.dump_mode == systemtap_session::dump_matched_probes_vars)
5431 saveargs(q, scope_die, dwfl_addr);
5fa1d0a2 5432 }
0a98fd42 5433
5d23847d 5434 // Reset the sole element of the "locations" vector as a
b20febf3
FCE
5435 // "reverse-engineered" form of the incoming (q.base_loc) probe
5436 // point. This allows a user to see what function / file / line
5437 // number any particular match of the wildcards.
919debfc
JL
5438
5439 vector<probe_point::component*> comps;
5440 if (q.has_kernel)
5441 comps.push_back (new probe_point::component(TOK_KERNEL));
5442 else if(q.has_module)
5443 comps.push_back (new probe_point::component(TOK_MODULE, new literal_string(module)));
5444 else if(q.has_process)
5445 comps.push_back (new probe_point::component(TOK_PROCESS, new literal_string(module)));
5446 else
5447 assert (0);
5448
5449 string fn_or_stmt;
5450 if (q.has_function_str || q.has_function_num)
e772a6e7 5451 fn_or_stmt = TOK_FUNCTION;
919debfc 5452 else
e772a6e7 5453 fn_or_stmt = TOK_STATEMENT;
919debfc
JL
5454
5455 if (q.has_function_str || q.has_statement_str)
5456 {
45a63356 5457 interned_string retro_name = q.final_function_name(funcname, filename, line);
919debfc
JL
5458 comps.push_back
5459 (new probe_point::component
5460 (fn_or_stmt, new literal_string (retro_name)));
5461 }
5462 else if (q.has_function_num || q.has_statement_num)
5463 {
5464 Dwarf_Addr retro_addr;
5465 if (q.has_function_num)
5466 retro_addr = q.function_num_val;
5467 else
5468 retro_addr = q.statement_num_val;
5469 comps.push_back (new probe_point::component
5470 (fn_or_stmt,
5471 new literal_number(retro_addr, true)));
5472
5473 if (q.has_absolute)
5474 comps.push_back (new probe_point::component (TOK_ABSOLUTE));
5475 }
5476
5477 if (q.has_call)
5478 comps.push_back (new probe_point::component(TOK_CALL));
5479 if (q.has_exported)
5480 comps.push_back (new probe_point::component(TOK_EXPORTED));
5481 if (q.has_inline)
5482 comps.push_back (new probe_point::component(TOK_INLINE));
5483 if (has_return)
5484 comps.push_back (new probe_point::component(TOK_RETURN));
5485 if (has_maxactive)
5486 comps.push_back (new probe_point::component
5487 (TOK_MAXACTIVE, new literal_number(maxactive_val)));
5488
5489 // Overwrite it.
5490 this->sole_location()->components = comps;
e2941743
JL
5491
5492 // if it's a .callee[s[(N)]] call, add checks to the probe body so that the
5493 // user body is only 'triggered' when called from q.callers[N-1], which
5494 // itself is called from q.callers[N-2], etc... I.E.
5495 // callees(N) --> N elements in q.callers --> N checks against [u]stack(0..N-1)
5496 if ((q.has_callee || q.has_callees_num) && q.callers && !q.callers->empty())
5497 {
5498 if (q.sess.verbose > 2)
7371cd19
JS
5499 clog << _F("adding caller checks for callee %s\n",
5500 funcname.to_string().c_str());
e2941743
JL
5501
5502 // Copy the stack and empty it out
5503 stack<Dwarf_Addr> callers(*q.callers);
5504 for (unsigned level = 1; !callers.empty(); level++,
5505 callers.pop())
5506 {
5507 Dwarf_Addr caller = callers.top();
5508
5509 // We first need to make the caller addr relocatable
45a63356 5510 interned_string caller_section;
e2941743
JL
5511 Dwarf_Addr caller_reloc;
5512 if (module == TOK_KERNEL)
5513 { // allow for relocatable kernel (see also add_probe_point())
5514 caller_reloc = caller - q.sess.sym_stext;
5515 caller_section = "_stext";
5516 }
5517 else
5518 caller_reloc = q.dw.relocate_address(caller,
5519 caller_section);
5520
5521 if (q.sess.verbose > 2)
7761da8b
JL
5522 clog << _F("adding caller check [u]stack(%d) == reloc(0x%s)\n",
5523 level, lex_cast_hex(caller_reloc).c_str());
e2941743
JL
5524
5525 // We want to add a statement like this:
6211aed9 5526 // if (!_caller_match(user, mod, sec, addr)) next;
e2941743
JL
5527 // Something similar is done in semantic_pass_conditions()
5528
5529 functioncall* check = new functioncall();
5530 check->tok = this->tok;
6211aed9 5531 check->function = "_caller_match";
e2941743
JL
5532 check->args.push_back(new literal_number(q.has_process));
5533 check->args[0]->tok = this->tok;
80e50fe7
FL
5534 // For callee .return probes, the callee is popped off stack
5535 // so we don't want to match the frame below the caller
5536 if (q.has_return)
5537 check->args.push_back(new literal_number(level-1));
5538 else
5539 check->args.push_back(new literal_number(level));
e2941743 5540 check->args[1]->tok = this->tok;
4766b1e6 5541 check->args.push_back(new literal_string(this->module));
e2941743
JL
5542 check->args[2]->tok = this->tok;
5543 check->args.push_back(new literal_string(caller_section));
5544 check->args[3]->tok = this->tok;
5545 check->args.push_back(new literal_number(caller_reloc, true /* hex */));
5546 check->args[4]->tok = this->tok;
5547
5548 unary_expression* notexp = new unary_expression();
5549 notexp->tok = this->tok;
5550 notexp->op = "!";
5551 notexp->operand = check;
5552
5553 if_statement* ifs = new if_statement();
5554 ifs->tok = this->tok;
5555 ifs->thenblock = new next_statement();
5556 ifs->thenblock->tok = this->tok;
5557 ifs->elseblock = NULL;
5558 ifs->condition = notexp;
5559
5560 this->body = new block(ifs, this->body);
5561 }
5562 }
2930abc7
FCE
5563}
5564
bd2b1e68 5565
0a98fd42 5566void
8c67c337
JS
5567dwarf_derived_probe::saveargs(dwarf_query& q, Dwarf_Die* scope_die,
5568 Dwarf_Addr dwfl_addr)
0a98fd42 5569{
9aa8ffce 5570 if (null_die(scope_die))
0a98fd42 5571 return;
0a98fd42 5572
8c67c337 5573 bool verbose = q.sess.verbose > 2;
0a98fd42 5574
8c67c337 5575 if (verbose)
b530b5b3 5576 clog << _F("saveargs: examining '%s' (dieoffset: %#" PRIx64 ")\n", (dwarf_diename(scope_die)?: "unknown"), dwarf_dieoffset(scope_die));
0a98fd42 5577
8c67c337
JS
5578 if (has_return)
5579 {
5580 /* Only save the return value if it has a type. */
5581 string type_name;
5582 Dwarf_Die type_die;
5583 if (dwarf_attr_die (scope_die, DW_AT_type, &type_die) &&
5584 dwarf_type_name(&type_die, type_name))
5585 args.push_back("$return:"+type_name);
5586
5587 else if (verbose)
b530b5b3
LB
5588 clog << _F("saveargs: failed to retrieve type name for return value (dieoffset: %s)\n",
5589 lex_cast_hex(dwarf_dieoffset(scope_die)).c_str());
8c67c337 5590 }
d87623a1 5591
0a98fd42 5592 Dwarf_Die arg;
4ef35696
JS
5593 vector<Dwarf_Die> scopes = q.dw.getscopes(scope_die);
5594 for (unsigned i = 0; i < scopes.size(); ++i)
5595 {
5596 if (dwarf_tag(&scopes[i]) == DW_TAG_compile_unit)
5597 break; // we don't want file-level variables
5598 if (dwarf_child (&scopes[i], &arg) == 0)
5599 do
0a98fd42 5600 {
4ef35696
JS
5601 switch (dwarf_tag (&arg))
5602 {
5603 case DW_TAG_variable:
5604 case DW_TAG_formal_parameter:
5605 break;
0a98fd42 5606
4ef35696
JS
5607 default:
5608 continue;
5609 }
0a98fd42 5610
4ef35696
JS
5611 /* Ignore this local if it has no name. */
5612 const char *arg_name = dwarf_diename (&arg);
5613 if (!arg_name)
8c67c337
JS
5614 {
5615 if (verbose)
b530b5b3
LB
5616 clog << _F("saveargs: failed to retrieve name for local (dieoffset: %s)\n",
5617 lex_cast_hex(dwarf_dieoffset(&arg)).c_str());
8c67c337
JS
5618 continue;
5619 }
4ef35696
JS
5620
5621 if (verbose)
b530b5b3
LB
5622 clog << _F("saveargs: finding location for local '%s' (dieoffset: %s)\n",
5623 arg_name, lex_cast_hex(dwarf_dieoffset(&arg)).c_str());
4ef35696
JS
5624
5625 /* Ignore this local if it has no location (or not at this PC). */
5626 /* NB: It still may not be directly accessible, e.g. if it is an
5627 * aggregate type, implicit_pointer, etc., but the user can later
5628 * figure out how to access the interesting parts. */
45b02a36
FCE
5629
5630 /* XXX: Perhaps saveargs() / listings-mode should work by synthesizing
5631 * several synthetic
5632 * probe foo { $var }
5633 * probes, testing them for overall resolvability.
5634 */
5635
4ef35696
JS
5636 Dwarf_Attribute attr_mem;
5637 if (!dwarf_attr_integrate (&arg, DW_AT_const_value, &attr_mem))
5638 {
5639 Dwarf_Op *expr;
5640 size_t len;
5641 if (!dwarf_attr_integrate (&arg, DW_AT_location, &attr_mem))
5642 {
5643 if (verbose)
b530b5b3
LB
5644 clog << _F("saveargs: failed to resolve the location for local '%s' (dieoffset: %s)\n",
5645 arg_name, lex_cast_hex(dwarf_dieoffset(&arg)).c_str());
4ef35696
JS
5646 continue;
5647 }
5648 else if (!(dwarf_getlocation_addr(&attr_mem, dwfl_addr, &expr,
5649 &len, 1) == 1 && len > 0))
5650 {
45b02a36
FCE
5651 Dwarf_Addr dwfl_addr2 = q.dw.pr15123_retry_addr (dwfl_addr, & arg);
5652 if (!dwfl_addr2 || (!(dwarf_getlocation_addr(&attr_mem, dwfl_addr2, &expr,
5653 &len, 1) == 1 && len > 0))) {
5654 if (verbose)
5655 clog << _F("saveargs: local '%s' (dieoffset: %s) is not available at this address (%s)\n",
5656 arg_name, lex_cast_hex(dwarf_dieoffset(&arg)).c_str(), lex_cast_hex(dwfl_addr).c_str());
5657 continue;
5658 }
4ef35696
JS
5659 }
5660 }
5661
5662 /* Ignore this local if it has no type. */
5663 string type_name;
5664 Dwarf_Die type_die;
5665 if (!dwarf_attr_die (&arg, DW_AT_type, &type_die) ||
5666 !dwarf_type_name(&type_die, type_name))
8c67c337
JS
5667 {
5668 if (verbose)
b530b5b3
LB
5669 clog << _F("saveargs: failed to retrieve type name for local '%s' (dieoffset: %s)\n",
5670 arg_name, lex_cast_hex(dwarf_dieoffset(&arg)).c_str());
8c67c337
JS
5671 continue;
5672 }
8c67c337 5673
4ef35696
JS
5674 /* This local looks good -- save it! */
5675 args.push_back("$"+string(arg_name)+":"+type_name);
8c67c337 5676 }
4ef35696
JS
5677 while (dwarf_siblingof (&arg, &arg) == 0);
5678 }
0a98fd42
JS
5679}
5680
5681
5682void
d0bfd2ac 5683dwarf_derived_probe::getargs(std::list<std::string> &arg_set) const
0a98fd42 5684{
d0bfd2ac 5685 arg_set.insert(arg_set.end(), args.begin(), args.end());
0a98fd42
JS
5686}
5687
5688
27dc09b1 5689void
42e38653 5690dwarf_derived_probe::emit_privilege_assertion (translator_output* o)
27dc09b1
DB
5691{
5692 if (has_process)
5693 {
5694 // These probes are allowed for unprivileged users, but only in the
5695 // context of processes which they own.
5696 emit_process_owner_assertion (o);
5697 return;
5698 }
5699
5700 // Other probes must contain the default assertion which aborts
5701 // if executed by an unprivileged user.
42e38653 5702 derived_probe::emit_privilege_assertion (o);
27dc09b1
DB
5703}
5704
5705
5706void
5707dwarf_derived_probe::print_dupe_stamp(ostream& o)
5708{
5709 if (has_process)
5710 {
5711 // These probes are allowed for unprivileged users, but only in the
5712 // context of processes which they own.
5713 print_dupe_stamp_unprivileged_process_owner (o);
5714 return;
5715 }
5716
5717 // Other probes must contain the default dupe stamp
5718 derived_probe::print_dupe_stamp (o);
5719}
5720
64211010 5721
7a053d3b 5722void
20c6c071 5723dwarf_derived_probe::register_statement_variants(match_node * root,
27dc09b1 5724 dwarf_builder * dw,
42e38653 5725 privilege_t privilege)
bd2b1e68 5726{
27dc09b1 5727 root
42e38653 5728 ->bind_privilege(privilege)
27dc09b1 5729 ->bind(dw);
83eeb3ac
HK
5730 root->bind(TOK_NEAREST)
5731 ->bind_privilege(privilege)
5732 ->bind(dw);
54efe513
GH
5733}
5734
7a053d3b 5735void
fd6602a0 5736dwarf_derived_probe::register_function_variants(match_node * root,
27dc09b1 5737 dwarf_builder * dw,
42e38653 5738 privilege_t privilege)
2865d17a 5739{
27dc09b1 5740 root
42e38653 5741 ->bind_privilege(privilege)
27dc09b1 5742 ->bind(dw);
27dc09b1 5743 root->bind(TOK_CALL)
42e38653 5744 ->bind_privilege(privilege)
27dc09b1 5745 ->bind(dw);
4bda987e
SC
5746 root->bind(TOK_EXPORTED)
5747 ->bind_privilege(privilege)
5748 ->bind(dw);
27dc09b1 5749 root->bind(TOK_RETURN)
42e38653 5750 ->bind_privilege(privilege)
27dc09b1 5751 ->bind(dw);
1e035395 5752
f6be7c06
DB
5753 // For process probes / uprobes, .maxactive() is unused.
5754 if (! pr_contains (privilege, pr_stapusr))
1e035395
FCE
5755 {
5756 root->bind(TOK_RETURN)
1e035395
FCE
5757 ->bind_num(TOK_MAXACTIVE)->bind(dw);
5758 }
bd2b1e68
GH
5759}
5760
7a053d3b 5761void
27dc09b1 5762dwarf_derived_probe::register_function_and_statement_variants(
440d9b00 5763 systemtap_session& s,
27dc09b1
DB
5764 match_node * root,
5765 dwarf_builder * dw,
42e38653 5766 privilege_t privilege
27dc09b1 5767)
bd2b1e68
GH
5768{
5769 // Here we match 4 forms:
5770 //
5771 // .function("foo")
5772 // .function(0xdeadbeef)
5773 // .statement("foo")
5774 // .statement(0xdeadbeef)
5775
440d9b00 5776 match_node *fv_root = root->bind_str(TOK_FUNCTION);
42e38653 5777 register_function_variants(fv_root, dw, privilege);
7f02ca94 5778 // ROOT.function("STRING") always gets the .inline and .label variants.
440d9b00 5779 fv_root->bind(TOK_INLINE)
42e38653 5780 ->bind_privilege(privilege)
440d9b00 5781 ->bind(dw);
7f02ca94
JS
5782 fv_root->bind_str(TOK_LABEL)
5783 ->bind_privilege(privilege)
440d9b00 5784 ->bind(dw);
c31add50
JL
5785 fv_root->bind_str(TOK_CALLEE)
5786 ->bind_privilege(privilege)
5787 ->bind(dw);
80e50fe7
FL
5788 fv_root->bind_str(TOK_CALLEE)
5789 ->bind(TOK_RETURN)
5790 ->bind_privilege(privilege)
5791 ->bind(dw);
5792 fv_root->bind_str(TOK_CALLEE)
5793 ->bind(TOK_CALL)
5794 ->bind_privilege(privilege)
5795 ->bind(dw);
c31add50
JL
5796 fv_root->bind(TOK_CALLEES)
5797 ->bind_privilege(privilege)
5798 ->bind(dw);
5799 fv_root->bind_num(TOK_CALLEES)
5800 ->bind_privilege(privilege)
5801 ->bind(dw);
440d9b00
DB
5802
5803 fv_root = root->bind_num(TOK_FUNCTION);
42e38653 5804 register_function_variants(fv_root, dw, privilege);
440d9b00
DB
5805 // ROOT.function(NUMBER).inline is deprecated in release 1.7 and removed thereafter.
5806 if (strverscmp(s.compatible.c_str(), "1.7") <= 0)
5807 {
5808 fv_root->bind(TOK_INLINE)
42e38653 5809 ->bind_privilege(privilege)
440d9b00
DB
5810 ->bind(dw);
5811 }
5812
42e38653
DB
5813 register_statement_variants(root->bind_str(TOK_STATEMENT), dw, privilege);
5814 register_statement_variants(root->bind_num(TOK_STATEMENT), dw, privilege);
bd2b1e68
GH
5815}
5816
b1615c74 5817void
dabd71bb 5818dwarf_derived_probe::register_sdt_variants(systemtap_session&,
b1615c74
JS
5819 match_node * root,
5820 dwarf_builder * dw)
5821{
5822 root->bind_str(TOK_MARK)
f66bb29a 5823 ->bind_privilege(pr_all)
b1615c74
JS
5824 ->bind(dw);
5825 root->bind_str(TOK_PROVIDER)->bind_str(TOK_MARK)
f66bb29a 5826 ->bind_privilege(pr_all)
b1615c74
JS
5827 ->bind(dw);
5828}
5829
5830void
dabd71bb 5831dwarf_derived_probe::register_plt_variants(systemtap_session&,
b1615c74
JS
5832 match_node * root,
5833 dwarf_builder * dw)
5834{
5835 root->bind(TOK_PLT)
f66bb29a 5836 ->bind_privilege(pr_all)
b1615c74
JS
5837 ->bind(dw);
5838 root->bind_str(TOK_PLT)
f66bb29a 5839 ->bind_privilege(pr_all)
b1615c74 5840 ->bind(dw);
2e96714f
SC
5841
5842 root->bind(TOK_PLT)
5843 ->bind(TOK_RETURN)
f66bb29a 5844 ->bind_privilege(pr_all)
b1615c74 5845 ->bind(dw);
2e96714f
SC
5846 root->bind_str(TOK_PLT)
5847 ->bind(TOK_RETURN)
f66bb29a 5848 ->bind_privilege(pr_all)
b1615c74 5849 ->bind(dw);
bd2b1e68
GH
5850}
5851
5852void
c4ce66a1 5853dwarf_derived_probe::register_patterns(systemtap_session& s)
bd2b1e68 5854{
c4ce66a1 5855 match_node* root = s.pattern_root;
bd2b1e68
GH
5856 dwarf_builder *dw = new dwarf_builder();
5857
c4ce66a1
JS
5858 update_visitor *filter = new dwarf_cast_expanding_visitor(s, *dw);
5859 s.code_filters.push_back(filter);
5860
bd1fcbad
YZ
5861 filter = new dwarf_atvar_expanding_visitor(s, *dw);
5862 s.code_filters.push_back(filter);
5863
73f52eb4
DB
5864 register_function_and_statement_variants(s, root->bind(TOK_KERNEL), dw, pr_privileged);
5865 register_function_and_statement_variants(s, root->bind_str(TOK_MODULE), dw, pr_privileged);
27dc09b1
DB
5866 root->bind(TOK_KERNEL)->bind_num(TOK_STATEMENT)->bind(TOK_ABSOLUTE)
5867 ->bind(dw);
2cab6244 5868
7f02ca94
JS
5869 match_node* uprobes[] = {
5870 root->bind(TOK_PROCESS),
5871 root->bind_str(TOK_PROCESS),
5c6f9e92 5872 root->bind_num(TOK_PROCESS),
7f02ca94
JS
5873 root->bind(TOK_PROCESS)->bind_str(TOK_LIBRARY),
5874 root->bind_str(TOK_PROCESS)->bind_str(TOK_LIBRARY),
5875 };
5876 for (size_t i = 0; i < sizeof(uprobes) / sizeof(*uprobes); ++i)
5877 {
f66bb29a 5878 register_function_and_statement_variants(s, uprobes[i], dw, pr_all);
7f02ca94
JS
5879 register_sdt_variants(s, uprobes[i], dw);
5880 register_plt_variants(s, uprobes[i], dw);
5881 }
bd2b1e68
GH
5882}
5883
9020300d 5884void
3689db05
SC
5885dwarf_derived_probe::emit_probe_local_init(systemtap_session& s, translator_output * o)
5886{
285ca427 5887 if (perf_counter_refs.size())
3689db05 5888 {
285ca427
DS
5889 o->newline() << "{";
5890 o->indent(1);
5891 unsigned ref_idx = 0;
5892 for (auto pcii = perf_counter_refs.begin();
5893 pcii != perf_counter_refs.end();
5894 pcii++)
5895 {
5896 // Find the associated perf.counter probe
5897 unsigned i = 0;
5898
5899 for (auto it=s.perf_counters.begin() ;
5900 it != s.perf_counters.end();
5901 it++, i++)
5902 {
5903 if ((*it).first == (*pcii))
5904 {
5905 // copy the perf counter values over
5906 //
5907 // NB: We'd like to simplify here. Right now we read
5908 // the perf counters into "values", then copy that
5909 // into the locals. We should be able to remove the
5910 // locals, but the 'symbol' class isn't designed to
5911 // point to the context structure itself, but the
5912 // locals inside the context structure.
5913 o->newline() << "l->l___perf_read_" + (*it).first
5914 + " = (int64_t)c->perf_read_values["
5915 + lex_cast(ref_idx) + "];";
5916 ref_idx++;
5917 break;
5918 }
5919 }
5920 }
5921 o->newline(-1) << "}";
3689db05 5922 }
b78a0fbb 5923
b95e2b79
MH
5924 if (access_vars)
5925 {
5926 // if accessing $variables, emit bsp cache setup for speeding up
d4670309 5927 o->newline() << "#if defined __ia64__";
d9aed31e 5928 o->newline() << "bspcache(c->unwaddr, c->kregs);";
d4670309 5929 o->newline() << "#endif";
b95e2b79 5930 }
9020300d 5931}
2930abc7 5932
b20febf3 5933// ------------------------------------------------------------------------
46b84a80
DS
5934
5935void
a7fbce1f 5936generic_kprobe_derived_probe_group::enroll (generic_kprobe_derived_probe* p)
46b84a80 5937{
b20febf3 5938 probes_by_module.insert (make_pair (p->module, p));
b8da0ad1 5939
a7fbce1f
DS
5940 // XXX: probes put at the same address (or symbol_name+offset)
5941 // should all share a single kprobe/kretprobe, and have their
5942 // handlers executed sequentially.
b55bc428
FCE
5943}
5944
a7fbce1f 5945
0dbac951 5946void
a7fbce1f 5947generic_kprobe_derived_probe_group::emit_module_decls (systemtap_session& s)
ec4373ff 5948{
b20febf3 5949 if (probes_by_module.empty()) return;
2930abc7 5950
a7fbce1f 5951 s.op->newline() << "/* ---- dwarf and non-dwarf kprobe-based probes ---- */";
775d51e5 5952
24897818
DS
5953 // FIXME: we could do the same thing (finding stats for the embedded
5954 // strings) for 'symbol_name'...
5955
faea5e16 5956 // Let's find some stats for the embedded strings. Maybe they
606fd9c8
FCE
5957 // are small and uniform enough to justify putting char[MAX]'s into
5958 // the array instead of relocated char*'s.
faea5e16
JS
5959 size_t module_name_max = 0, section_name_max = 0;
5960 size_t module_name_tot = 0, section_name_tot = 0;
606fd9c8 5961 size_t all_name_cnt = probes_by_module.size(); // for average
b4662f6b 5962 for (auto it = probes_by_module.begin(); it != probes_by_module.end(); it++)
606fd9c8 5963 {
a7fbce1f 5964 generic_kprobe_derived_probe* p = it->second;
606fd9c8
FCE
5965#define DOIT(var,expr) do { \
5966 size_t var##_size = (expr) + 1; \
5967 var##_max = max (var##_max, var##_size); \
5968 var##_tot += var##_size; } while (0)
5969 DOIT(module_name, p->module.size());
5970 DOIT(section_name, p->section.size());
606fd9c8
FCE
5971#undef DOIT
5972 }
5973
5974 // Decide whether it's worthwhile to use char[] or char* by comparing
5975 // the amount of average waste (max - avg) to the relocation data size
5976 // (3 native long words).
5977#define CALCIT(var) \
5978 if ((var##_name_max-(var##_name_tot/all_name_cnt)) < (3 * sizeof(void*))) \
5979 { \
4210989f 5980 s.op->newline() << "#define STAP_KPROBE_PROBE_STR_" << #var << " " \
2d9ec661
JL
5981 << "const char " << #var \
5982 << "[" << var##_name_max << "]"; \
4210989f 5983 if (s.verbose > 2) clog << "stap_kprobe_probe " << #var \
606fd9c8
FCE
5984 << "[" << var##_name_max << "]" << endl; \
5985 } \
5986 else \
5987 { \
4210989f 5988 s.op->newline() << "#define STAP_KPROBE_PROBE_STR_" << #var << " " \
2d9ec661 5989 << "const char * const " << #var << ""; \
4210989f 5990 if (s.verbose > 2) clog << "stap_kprobe_probe *" << #var << endl; \
606fd9c8
FCE
5991 }
5992
5993 CALCIT(module);
5994 CALCIT(section);
2d9ec661 5995
e6fe60e7 5996#undef CALCIT
606fd9c8 5997
2d9ec661
JL
5998 s.op->newline() << "#include \"linux/kprobes.c\"";
5999
4210989f 6000#define UNDEFIT(var) s.op->newline() << "#undef STAP_KPROBE_PROBE_STR_" << #var
2d9ec661
JL
6001 UNDEFIT(module);
6002 UNDEFIT(section);
6003#undef UNDEFIT
6004
6005 // Emit an array of kprobe/kretprobe pointers
6006 s.op->newline() << "#if defined(STAPCONF_UNREGISTER_KPROBES)";
6007 s.op->newline() << "static void * stap_unreg_kprobes[" << probes_by_module.size() << "];";
6008 s.op->newline() << "#endif";
6009
6010 // Emit the actual probe list.
6011
6012 // NB: we used to plop a union { struct kprobe; struct kretprobe } into
4210989f
DS
6013 // struct stap_kprobe_probe, but it being initialized data makes it add
6014 // hundreds of bytes of padding per stap_kprobe_probe. (PR5673)
6015 s.op->newline() << "static struct stap_kprobe stap_kprobes[" << probes_by_module.size() << "];";
2d9ec661
JL
6016 // NB: bss!
6017
4210989f 6018 s.op->newline() << "static struct stap_kprobe_probe stap_kprobe_probes[] = {";
b20febf3
FCE
6019 s.op->indent(1);
6020
4210989f 6021 size_t stap_kprobe_idx = 0;
b4662f6b 6022 for (auto it = probes_by_module.begin(); it != probes_by_module.end(); it++)
2930abc7 6023 {
a7fbce1f 6024 generic_kprobe_derived_probe* p = it->second;
b20febf3
FCE
6025 s.op->newline() << "{";
6026 if (p->has_return)
6027 s.op->line() << " .return_p=1,";
c9bad430 6028 if (p->has_maxactive)
606fd9c8
FCE
6029 {
6030 s.op->line() << " .maxactive_p=1,";
6031 assert (p->maxactive_val >= 0 && p->maxactive_val <= USHRT_MAX);
6032 s.op->line() << " .maxactive_val=" << p->maxactive_val << ",";
6033 }
af234c40
JS
6034 if (p->saved_longs || p->saved_strings)
6035 {
6036 if (p->saved_longs)
6037 s.op->line() << " .saved_longs=" << p->saved_longs << ",";
6038 if (p->saved_strings)
6039 s.op->line() << " .saved_strings=" << p->saved_strings << ",";
6040 if (p->entry_handler)
c87ae2c1 6041 s.op->line() << " .entry_probe=" << common_probe_init (p->entry_handler) << ",";
af234c40 6042 }
b350f56b
JS
6043 if (p->locations[0]->optional)
6044 s.op->line() << " .optional_p=1,";
dc38c256 6045 s.op->line() << " .address=(unsigned long)0x" << hex << p->addr << dec << "ULL,";
84048984
FCE
6046 s.op->line() << " .module=\"" << p->module << "\",";
6047 s.op->line() << " .section=\"" << p->section << "\",";
faea5e16 6048 s.op->line() << " .probe=" << common_probe_init (p) << ",";
4210989f 6049 s.op->line() << " .kprobe=&stap_kprobes[" << stap_kprobe_idx++ << "],";
a7fbce1f 6050 if (!p->symbol_name.empty())
24897818
DS
6051 {
6052 // After kernel commit 4982223e51, module notifiers are
6053 // being called too early. So, we have to switch to using
6054 // symbol+offset probing for modules.
a7fbce1f
DS
6055 if (! p->section.empty())
6056 s.op->newline(-1) << "#if LINUX_VERSION_CODE >= KERNEL_VERSION(3,10,0)";
6057 else
6058 s.op->indent(-1);
24897818
DS
6059 s.op->newline() << " .symbol_name=\"" << p->symbol_name << "\",";
6060 s.op->line() << " .offset=(unsigned int)" << p->offset << ",";
a7fbce1f
DS
6061 if (! p->section.empty())
6062 s.op->newline() << "#endif";
24897818 6063 s.op->newline(1);
0dbac951 6064 }
b20febf3 6065 s.op->line() << " },";
2930abc7 6066 }
2930abc7 6067
b20febf3
FCE
6068 s.op->newline(-1) << "};";
6069
6070 // Emit the kprobes callback function
6071 s.op->newline();
6072 s.op->newline() << "static int enter_kprobe_probe (struct kprobe *inst,";
6073 s.op->line() << " struct pt_regs *regs) {";
606fd9c8 6074 // NB: as of PR5673, the kprobe|kretprobe union struct is in BSS
4210989f 6075 s.op->newline(1) << "int kprobe_idx = ((uintptr_t)inst-(uintptr_t)stap_kprobes)/sizeof(struct stap_kprobe);";
606fd9c8 6076 // Check that the index is plausible
4210989f 6077 s.op->newline() << "struct stap_kprobe_probe *skp = &stap_kprobe_probes[";
606fd9c8
FCE
6078 s.op->line() << "((kprobe_idx >= 0 && kprobe_idx < " << probes_by_module.size() << ")?";
6079 s.op->line() << "kprobe_idx:0)"; // NB: at least we avoid memory corruption
6080 // XXX: it would be nice to give a more verbose error though; BUG_ON later?
6081 s.op->line() << "];";
4210989f 6082 common_probe_entryfn_prologue (s, "STAP_SESSION_RUNNING", "skp->probe",
cda141c2 6083 "stp_probe_type_kprobe");
d9aed31e 6084 s.op->newline() << "c->kregs = regs;";
6415ddde
MW
6085
6086 // Make it look like the IP is set as it wouldn't have been replaced
6087 // by a breakpoint instruction when calling real probe handler. Reset
6088 // IP regs on return, so we don't confuse kprobes. PR10458
6089 s.op->newline() << "{";
6090 s.op->indent(1);
d9aed31e 6091 s.op->newline() << "unsigned long kprobes_ip = REG_IP(c->kregs);";
259d54c0 6092 s.op->newline() << "SET_REG_IP(regs, (unsigned long) inst->addr);";
4210989f 6093 s.op->newline() << "(*skp->probe->ph) (c);";
259d54c0 6094 s.op->newline() << "SET_REG_IP(regs, kprobes_ip);";
6415ddde
MW
6095 s.op->newline(-1) << "}";
6096
ef1337ee 6097 common_probe_entryfn_epilogue (s, true, otf_safe_context(s));
b20febf3
FCE
6098 s.op->newline() << "return 0;";
6099 s.op->newline(-1) << "}";
6100
6101 // Same for kretprobes
6102 s.op->newline();
af234c40
JS
6103 s.op->newline() << "static int enter_kretprobe_common (struct kretprobe_instance *inst,";
6104 s.op->line() << " struct pt_regs *regs, int entry) {";
b20febf3 6105 s.op->newline(1) << "struct kretprobe *krp = inst->rp;";
606fd9c8
FCE
6106
6107 // NB: as of PR5673, the kprobe|kretprobe union struct is in BSS
4210989f 6108 s.op->newline() << "int kprobe_idx = ((uintptr_t)krp-(uintptr_t)stap_kprobes)/sizeof(struct stap_kprobe);";
606fd9c8 6109 // Check that the index is plausible
4210989f 6110 s.op->newline() << "struct stap_kprobe_probe *skp = &stap_kprobe_probes[";
606fd9c8
FCE
6111 s.op->line() << "((kprobe_idx >= 0 && kprobe_idx < " << probes_by_module.size() << ")?";
6112 s.op->line() << "kprobe_idx:0)"; // NB: at least we avoid memory corruption
6113 // XXX: it would be nice to give a more verbose error though; BUG_ON later?
6114 s.op->line() << "];";
6115
4210989f 6116 s.op->newline() << "const struct stap_probe *sp = entry ? skp->entry_probe : skp->probe;";
c87ae2c1
JS
6117 s.op->newline() << "if (sp) {";
6118 s.op->indent(1);
71db462b 6119 common_probe_entryfn_prologue (s, "STAP_SESSION_RUNNING", "sp",
cda141c2 6120 "stp_probe_type_kretprobe");
d9aed31e 6121 s.op->newline() << "c->kregs = regs;";
af234c40
JS
6122
6123 // for assisting runtime's backtrace logic and accessing kretprobe data packets
6dceb5c9 6124 s.op->newline() << "c->ips.krp.pi = inst;";
4210989f 6125 s.op->newline() << "c->ips.krp.pi_longs = skp->saved_longs;";
6415ddde
MW
6126
6127 // Make it look like the IP is set as it wouldn't have been replaced
6128 // by a breakpoint instruction when calling real probe handler. Reset
6129 // IP regs on return, so we don't confuse kprobes. PR10458
6130 s.op->newline() << "{";
d9aed31e 6131 s.op->newline(1) << "unsigned long kprobes_ip = REG_IP(c->kregs);";
c87ae2c1
JS
6132 s.op->newline() << "if (entry)";
6133 s.op->newline(1) << "SET_REG_IP(regs, (unsigned long) inst->rp->kp.addr);";
6134 s.op->newline(-1) << "else";
6135 s.op->newline(1) << "SET_REG_IP(regs, (unsigned long)inst->ret_addr);";
6136 s.op->newline(-1) << "(sp->ph) (c);";
259d54c0 6137 s.op->newline() << "SET_REG_IP(regs, kprobes_ip);";
6415ddde
MW
6138 s.op->newline(-1) << "}";
6139
ef1337ee 6140 common_probe_entryfn_epilogue (s, true, otf_safe_context(s));
c87ae2c1 6141 s.op->newline(-1) << "}";
b20febf3
FCE
6142 s.op->newline() << "return 0;";
6143 s.op->newline(-1) << "}";
af234c40 6144
14cf7e42 6145 s.op->newline();
20c6c071 6146}
ec4373ff 6147
20c6c071 6148
0dbac951 6149void
a7fbce1f 6150generic_kprobe_derived_probe_group::emit_module_init (systemtap_session& s)
b20febf3 6151{
66cab35f 6152 if (probes_by_module.empty()) return;
195dfadc 6153
a7fbce1f 6154 s.op->newline() << "/* ---- dwarf and non-dwarf kprobe-based probes ---- */";
54ff4cd7 6155
66cab35f
JL
6156 // We'll let stapkp_init() handle reporting errors by setting probe_point to
6157 // NULL.
6158 s.op->newline() << "probe_point = NULL;";
54ff4cd7 6159
66cab35f 6160 s.op->newline() << "rc = stapkp_init( "
4210989f
DS
6161 << "stap_kprobe_probes, "
6162 << "ARRAY_SIZE(stap_kprobe_probes));";
dc38c0ae
DS
6163}
6164
0dbac951
RH
6165std::string
6166generic_kprobe_derived_probe::args_for_bpf() const
6167{
6168 std::stringstream o;
6169
cbc16973
AM
6170 if (has_return)
6171 o << "kretprobe/" << sym_name_for_bpf;
0dbac951 6172 else
cbc16973 6173 o << "kprobe/" << "0x" << std::hex << addr;
0dbac951
RH
6174
6175 return o.str();
6176}
6177
6178bool
fcdd71ba
WC
6179sort_for_bpf(systemtap_session& s __attribute__ ((unused)),
6180 generic_kprobe_derived_probe_group *ge,
0dbac951
RH
6181 sort_for_bpf_probe_arg_vector &v)
6182{
2e757497 6183 if (!ge || ge->probes_by_module.empty())
0dbac951
RH
6184 return false;
6185
6186 for (auto i = ge->probes_by_module.begin();
6187 i != ge->probes_by_module.end(); ++i)
6188 {
6189 generic_kprobe_derived_probe *p = i->second;
6190 v.push_back(std::pair<derived_probe *, std::string>
6191 (p, p->args_for_bpf()));
6192 }
6193
6194 return true;
6195}
6196
6197void
a7fbce1f 6198generic_kprobe_derived_probe_group::emit_module_refresh (systemtap_session& s)
b4be7cbc 6199{
97ab978d 6200 if (probes_by_module.empty()) return;
b4be7cbc 6201
a7fbce1f 6202 s.op->newline() << "/* ---- dwarf and non-dwarf kprobe-based probes ---- */";
b4be7cbc 6203
97ab978d 6204 s.op->newline() << "stapkp_refresh( "
19d62b5b 6205 << "modname, "
4210989f
DS
6206 << "stap_kprobe_probes, "
6207 << "ARRAY_SIZE(stap_kprobe_probes));";
97ab978d 6208}
b4be7cbc 6209
0dbac951 6210void
a7fbce1f 6211generic_kprobe_derived_probe_group::emit_module_exit (systemtap_session& s)
46b84a80 6212{
c29d98ec 6213 if (probes_by_module.empty()) return;
42cb22bd 6214
a7fbce1f 6215 s.op->newline() << "/* ---- dwarf and non-dwarf kprobe-based probes ---- */";
c29d98ec
JL
6216
6217 s.op->newline() << "stapkp_exit( "
4210989f
DS
6218 << "stap_kprobe_probes, "
6219 << "ARRAY_SIZE(stap_kprobe_probes));";
46b84a80
DS
6220}
6221
a7fbce1f
DS
6222// ------------------------------------------------------------------------
6223
272c9036
WF
6224static void sdt_v3_tokenize(const string& str, vector<string>& tokens)
6225{
6226 string::size_type pos;
6227 string::size_type lastPos = str.find_first_not_of(" ", 0);
6228 string::size_type nextAt = str.find("@", lastPos);
e9a90eee
JS
6229
6230 if (nextAt == string::npos)
6231 {
6232 // PR13934: Assembly probes are not forced to use the N@OP form.
6233 // In this case, N is inferred to be the native word size. Since we
6234 // don't have a nice delimiter, just split it on spaces. SDT-asm authors
6235 // then must not put any spaces in arguments, to avoid ambiguity.
6236 tokenize(str, tokens, " ");
6237 return;
6238 }
6239
272c9036
WF
6240 while (lastPos != string::npos)
6241 {
6242 pos = nextAt + 1;
6243 nextAt = str.find("@", pos);
6244 if (nextAt == string::npos)
6245 pos = string::npos;
6246 else
6247 pos = str.rfind(" ", nextAt);
6248
6249 tokens.push_back(str.substr(lastPos, pos - lastPos));
6250 lastPos = str.find_first_not_of(" ", pos);
6251 }
6252}
8aabf152 6253
8aabf152 6254
aff5d390 6255struct sdt_uprobe_var_expanding_visitor: public var_expanding_visitor
7a05f484 6256{
ae1418f0 6257 enum regwidths {QI, QIh, HI, SI, DI};
f83336a5 6258 sdt_uprobe_var_expanding_visitor(systemtap_session& s,
b8688ee6 6259 dwflpp& dw,
f83336a5 6260 int elf_machine,
46ccda7c
FCE
6261 interned_string process_name,
6262 interned_string provider_name,
6263 interned_string probe_name,
71e5e13d 6264 stap_sdt_probe_type probe_type,
46ccda7c 6265 interned_string arg_string,
8aabf152 6266 int ac):
16aa72d9 6267 var_expanding_visitor (s), dw (dw), elf_machine (elf_machine),
b8688ee6
JL
6268 process_name (process_name), provider_name (provider_name),
6269 probe_name (probe_name), probe_type (probe_type), arg_count ((unsigned) ac)
a8ec7719 6270 {
a57b7534
JL
6271 // sanity check that we're not somehow here for a kernel probe
6272 assert(is_user_module(process_name));
ae1418f0 6273
2e6d617a 6274 build_dwarf_registers();
f83336a5 6275
ebbd2b45 6276 need_debug_info = false;
88e39987 6277 if (probe_type == uprobe3_type)
272c9036
WF
6278 {
6279 sdt_v3_tokenize(arg_string, arg_tokens);
6280 assert(arg_count <= 12);
6281 }
88e39987 6282 else
272c9036
WF
6283 {
6284 tokenize(arg_string, arg_tokens, " ");
6285 assert(arg_count <= 10);
6286 }
a8ec7719 6287 }
8aabf152 6288
b8688ee6 6289 dwflpp& dw;
332ba7e7 6290 int elf_machine;
46ccda7c
FCE
6291 interned_string process_name;
6292 interned_string provider_name;
6293 interned_string probe_name;
71e5e13d 6294 stap_sdt_probe_type probe_type;
8aabf152 6295 unsigned arg_count;
aff5d390 6296 vector<string> arg_tokens;
2e6d617a 6297
46a94997 6298 map<string, pair<unsigned,int> > dwarf_regs;
2e6d617a
JL
6299 string regnames;
6300 string percent_regnames;
6301
ebbd2b45 6302 bool need_debug_info;
aff5d390 6303
2e6d617a 6304 void build_dwarf_registers();
aff5d390 6305 void visit_target_symbol (target_symbol* e);
03c45fcb 6306 unsigned get_target_symbol_argno_and_validate (target_symbol* e);
ac074daf 6307 long parse_out_arg_precision(string& asmarg);
0fbf6fb8
JL
6308 expression* try_parse_arg_literal (target_symbol *e,
6309 const string& asmarg,
6310 long precision);
95b78bc9
JL
6311 expression* try_parse_arg_register (target_symbol *e,
6312 const string& asmarg,
6313 long precision);
8da56b62
JL
6314 expression* try_parse_arg_offset_register (target_symbol *e,
6315 const string& asmarg,
6316 long precision);
c826ad73
FCE
6317 expression* try_parse_arg_register_pair (target_symbol *e,
6318 const string& asmarg,
6319 long precision);
10fcca15
JL
6320 expression* try_parse_arg_effective_addr (target_symbol *e,
6321 const string& asmarg,
6322 long precision);
451c0c25
JL
6323 expression* try_parse_arg_varname (target_symbol *e,
6324 const string& asmarg,
6325 long precision);
6ef331c8
SC
6326 void visit_target_symbol_arg (target_symbol* e);
6327 void visit_target_symbol_context (target_symbol* e);
bd1fcbad 6328 void visit_atvar_op (atvar_op* e);
40a0c64e 6329 void visit_cast_op (cast_op* e);
aff5d390
SC
6330};
6331
2e6d617a
JL
6332void
6333sdt_uprobe_var_expanding_visitor::build_dwarf_registers ()
6334{
6335 /* Register name mapping table depends on the elf machine of this particular
6336 probe target process/file, not upon the host. So we can't just
6337 #ifdef _i686_ etc. */
6338
6339#define DRI(name,num,width) dwarf_regs[name]=make_pair(num,width)
6340 if (elf_machine == EM_X86_64) {
6341 DRI ("%rax", 0, DI); DRI ("%eax", 0, SI); DRI ("%ax", 0, HI);
6342 DRI ("%al", 0, QI); DRI ("%ah", 0, QIh);
6343 DRI ("%rdx", 1, DI); DRI ("%edx", 1, SI); DRI ("%dx", 1, HI);
6344 DRI ("%dl", 1, QI); DRI ("%dh", 1, QIh);
6345 DRI ("%rcx", 2, DI); DRI ("%ecx", 2, SI); DRI ("%cx", 2, HI);
6346 DRI ("%cl", 2, QI); DRI ("%ch", 2, QIh);
6347 DRI ("%rbx", 3, DI); DRI ("%ebx", 3, SI); DRI ("%bx", 3, HI);
6348 DRI ("%bl", 3, QI); DRI ("%bh", 3, QIh);
6349 DRI ("%rsi", 4, DI); DRI ("%esi", 4, SI); DRI ("%si", 4, HI);
6350 DRI ("%sil", 4, QI);
6351 DRI ("%rdi", 5, DI); DRI ("%edi", 5, SI); DRI ("%di", 5, HI);
6352 DRI ("%dil", 5, QI);
6353 DRI ("%rbp", 6, DI); DRI ("%ebp", 6, SI); DRI ("%bp", 6, HI);
6354 DRI ("%bpl", 6, QI);
6355 DRI ("%rsp", 7, DI); DRI ("%esp", 7, SI); DRI ("%sp", 7, HI);
6356 DRI ("%spl", 7, QI);
6357 DRI ("%r8", 8, DI); DRI ("%r8d", 8, SI); DRI ("%r8w", 8, HI);
6358 DRI ("%r8b", 8, QI);
6359 DRI ("%r9", 9, DI); DRI ("%r9d", 9, SI); DRI ("%r9w", 9, HI);
6360 DRI ("%r9b", 9, QI);
6361 DRI ("%r10", 10, DI); DRI ("%r10d", 10, SI); DRI ("%r10w", 10, HI);
6362 DRI ("%r10b", 10, QI);
6363 DRI ("%r11", 11, DI); DRI ("%r11d", 11, SI); DRI ("%r11w", 11, HI);
6364 DRI ("%r11b", 11, QI);
6365 DRI ("%r12", 12, DI); DRI ("%r12d", 12, SI); DRI ("%r12w", 12, HI);
6366 DRI ("%r12b", 12, QI);
6367 DRI ("%r13", 13, DI); DRI ("%r13d", 13, SI); DRI ("%r13w", 13, HI);
6368 DRI ("%r13b", 13, QI);
6369 DRI ("%r14", 14, DI); DRI ("%r14d", 14, SI); DRI ("%r14w", 14, HI);
6370 DRI ("%r14b", 14, QI);
6371 DRI ("%r15", 15, DI); DRI ("%r15d", 15, SI); DRI ("%r15w", 15, HI);
6372 DRI ("%r15b", 15, QI);
6373 DRI ("%rip", 16, DI); DRI ("%eip", 16, SI); DRI ("%ip", 16, HI);
6374 } else if (elf_machine == EM_386) {
6375 DRI ("%eax", 0, SI); DRI ("%ax", 0, HI); DRI ("%al", 0, QI);
6376 DRI ("%ah", 0, QIh);
6377 DRI ("%ecx", 1, SI); DRI ("%cx", 1, HI); DRI ("%cl", 1, QI);
6378 DRI ("%ch", 1, QIh);
6379 DRI ("%edx", 2, SI); DRI ("%dx", 2, HI); DRI ("%dl", 2, QI);
6380 DRI ("%dh", 2, QIh);
6381 DRI ("%ebx", 3, SI); DRI ("%bx", 3, HI); DRI ("%bl", 3, QI);
6382 DRI ("%bh", 3, QIh);
6383 DRI ("%esp", 4, SI); DRI ("%sp", 4, HI);
6384 DRI ("%ebp", 5, SI); DRI ("%bp", 5, HI);
6385 DRI ("%esi", 6, SI); DRI ("%si", 6, HI); DRI ("%sil", 6, QI);
6386 DRI ("%edi", 7, SI); DRI ("%di", 7, HI); DRI ("%dil", 7, QI);
6387 } else if (elf_machine == EM_PPC || elf_machine == EM_PPC64) {
6388 DRI ("%r0", 0, DI);
6389 DRI ("%r1", 1, DI);
6390 DRI ("%r2", 2, DI);
6391 DRI ("%r3", 3, DI);
6392 DRI ("%r4", 4, DI);
6393 DRI ("%r5", 5, DI);
6394 DRI ("%r6", 6, DI);
6395 DRI ("%r7", 7, DI);
6396 DRI ("%r8", 8, DI);
6397 DRI ("%r9", 9, DI);
6398 DRI ("%r10", 10, DI);
6399 DRI ("%r11", 11, DI);
6400 DRI ("%r12", 12, DI);
6401 DRI ("%r13", 13, DI);
6402 DRI ("%r14", 14, DI);
6403 DRI ("%r15", 15, DI);
6404 DRI ("%r16", 16, DI);
6405 DRI ("%r17", 17, DI);
6406 DRI ("%r18", 18, DI);
6407 DRI ("%r19", 19, DI);
6408 DRI ("%r20", 20, DI);
6409 DRI ("%r21", 21, DI);
6410 DRI ("%r22", 22, DI);
6411 DRI ("%r23", 23, DI);
6412 DRI ("%r24", 24, DI);
6413 DRI ("%r25", 25, DI);
6414 DRI ("%r26", 26, DI);
6415 DRI ("%r27", 27, DI);
6416 DRI ("%r28", 28, DI);
6417 DRI ("%r29", 29, DI);
6418 DRI ("%r30", 30, DI);
6419 DRI ("%r31", 31, DI);
6420 // PR11821: unadorned register "names" without -mregnames
6421 DRI ("0", 0, DI);
6422 DRI ("1", 1, DI);
6423 DRI ("2", 2, DI);
6424 DRI ("3", 3, DI);
6425 DRI ("4", 4, DI);
6426 DRI ("5", 5, DI);
6427 DRI ("6", 6, DI);
6428 DRI ("7", 7, DI);
6429 DRI ("8", 8, DI);
6430 DRI ("9", 9, DI);
6431 DRI ("10", 10, DI);
6432 DRI ("11", 11, DI);
6433 DRI ("12", 12, DI);
6434 DRI ("13", 13, DI);
6435 DRI ("14", 14, DI);
6436 DRI ("15", 15, DI);
6437 DRI ("16", 16, DI);
6438 DRI ("17", 17, DI);
6439 DRI ("18", 18, DI);
6440 DRI ("19", 19, DI);
6441 DRI ("20", 20, DI);
6442 DRI ("21", 21, DI);
6443 DRI ("22", 22, DI);
6444 DRI ("23", 23, DI);
6445 DRI ("24", 24, DI);
6446 DRI ("25", 25, DI);
6447 DRI ("26", 26, DI);
6448 DRI ("27", 27, DI);
6449 DRI ("28", 28, DI);
6450 DRI ("29", 29, DI);
6451 DRI ("30", 30, DI);
6452 DRI ("31", 31, DI);
6453 } else if (elf_machine == EM_S390) {
6454 DRI ("%r0", 0, DI);
6455 DRI ("%r1", 1, DI);
6456 DRI ("%r2", 2, DI);
6457 DRI ("%r3", 3, DI);
6458 DRI ("%r4", 4, DI);
6459 DRI ("%r5", 5, DI);
6460 DRI ("%r6", 6, DI);
6461 DRI ("%r7", 7, DI);
6462 DRI ("%r8", 8, DI);
6463 DRI ("%r9", 9, DI);
6464 DRI ("%r10", 10, DI);
6465 DRI ("%r11", 11, DI);
6466 DRI ("%r12", 12, DI);
6467 DRI ("%r13", 13, DI);
6468 DRI ("%r14", 14, DI);
6469 DRI ("%r15", 15, DI);
6470 } else if (elf_machine == EM_ARM) {
6471 DRI ("r0", 0, SI);
6472 DRI ("r1", 1, SI);
6473 DRI ("r2", 2, SI);
6474 DRI ("r3", 3, SI);
6475 DRI ("r4", 4, SI);
6476 DRI ("r5", 5, SI);
6477 DRI ("r6", 6, SI);
6478 DRI ("r7", 7, SI);
6479 DRI ("r8", 8, SI);
6480 DRI ("r9", 9, SI);
6481 DRI ("r10", 10, SI); DRI ("sl", 10, SI);
6482 DRI ("fp", 11, SI);
6483 DRI ("ip", 12, SI);
6484 DRI ("sp", 13, SI);
6485 DRI ("lr", 14, SI);
6486 DRI ("pc", 15, SI);
6487 } else if (elf_machine == EM_AARCH64) {
6488 DRI ("x0", 0, DI); DRI ("w0", 0, SI);
6489 DRI ("x1", 1, DI); DRI ("w1", 1, SI);
6490 DRI ("x2", 2, DI); DRI ("w2", 2, SI);
6491 DRI ("x3", 3, DI); DRI ("w3", 3, SI);
6492 DRI ("x4", 4, DI); DRI ("w4", 4, SI);
6493 DRI ("x5", 5, DI); DRI ("w5", 5, SI);
6494 DRI ("x6", 6, DI); DRI ("w6", 6, SI);
6495 DRI ("x7", 7, DI); DRI ("w7", 7, SI);
6496 DRI ("x8", 8, DI); DRI ("w8", 8, SI);
6497 DRI ("x9", 9, DI); DRI ("w9", 9, SI);
6498 DRI ("x10", 10, DI); DRI ("w10", 10, SI);
6499 DRI ("x11", 11, DI); DRI ("w11", 11, SI);
6500 DRI ("x12", 12, DI); DRI ("w12", 12, SI);
6501 DRI ("x13", 13, DI); DRI ("w13", 13, SI);
6502 DRI ("x14", 14, DI); DRI ("w14", 14, SI);
6503 DRI ("x15", 15, DI); DRI ("w15", 15, SI);
6504 DRI ("x16", 16, DI); DRI ("w16", 16, SI);
6505 DRI ("x17", 17, DI); DRI ("w17", 17, SI);
6506 DRI ("x18", 18, DI); DRI ("w18", 18, SI);
6507 DRI ("x19", 19, DI); DRI ("w19", 19, SI);
6508 DRI ("x20", 20, DI); DRI ("w20", 20, SI);
6509 DRI ("x21", 21, DI); DRI ("w21", 21, SI);
6510 DRI ("x22", 22, DI); DRI ("w22", 22, SI);
6511 DRI ("x23", 23, DI); DRI ("w23", 23, SI);
6512 DRI ("x24", 24, DI); DRI ("w24", 24, SI);
6513 DRI ("x25", 25, DI); DRI ("w25", 25, SI);
6514 DRI ("x26", 26, DI); DRI ("w26", 26, SI);
6515 DRI ("x27", 27, DI); DRI ("w27", 27, SI);
6516 DRI ("x28", 28, DI); DRI ("w28", 28, SI);
6517 DRI ("x29", 29, DI); DRI ("w29", 29, SI);
6518 DRI ("x30", 30, DI); DRI ("w30", 30, SI);
6519 DRI ("sp", 31, DI);
85356a4f
MN
6520 } else if (elf_machine == EM_MIPS) {
6521 Dwarf_Addr bias;
6522 Elf* elf = (dwfl_module_getelf (dw.mod_info->mod, &bias));
6523 enum regwidths mips_reg_width =
6524 (gelf_getclass (elf) == ELFCLASS32) ? SI : DI;
6525 DRI ("$zero", 0, mips_reg_width);
6526 DRI ("$at", 1, mips_reg_width);
6527 DRI ("$v0", 2, mips_reg_width);
6528 DRI ("$v1", 3, mips_reg_width);
6529 DRI ("$a0", 4, mips_reg_width);
6530 DRI ("$a1", 5, mips_reg_width);
6531 DRI ("$a2", 6, mips_reg_width);
6532 DRI ("$a3", 7, mips_reg_width);
6533 DRI ("$a4", 8, mips_reg_width);
6534 DRI ("$a5", 9, mips_reg_width);
6535 DRI ("$a6", 10, mips_reg_width);
6536 DRI ("$a7", 11, mips_reg_width);
6537 DRI ("$t0", 12, mips_reg_width);
6538 DRI ("$t1", 13, mips_reg_width);
6539 DRI ("$t2", 14, mips_reg_width);
6540 DRI ("$t3", 15, mips_reg_width);
6541 DRI ("$s0", 16, mips_reg_width);
6542 DRI ("$s1", 17, mips_reg_width);
6543 DRI ("$s2", 18, mips_reg_width);
6544 DRI ("$s3", 19, mips_reg_width);
6545 DRI ("$s4", 20, mips_reg_width);
6546 DRI ("$s5", 21, mips_reg_width);
6547 DRI ("$s6", 22, mips_reg_width);
6548 DRI ("$s7", 23, mips_reg_width);
6549 DRI ("$t8", 24, mips_reg_width);
6550 DRI ("$t9", 25, mips_reg_width);
6551 DRI ("$k0", 26, mips_reg_width);
6552 DRI ("$k1", 27, mips_reg_width);
6553 DRI ("$gp", 28, mips_reg_width);
6554 DRI ("$sp", 29, mips_reg_width);
6555 DRI ("$s8", 30, mips_reg_width);
6556 DRI ("$fp", 30, mips_reg_width);
6557 DRI ("$ra", 31, mips_reg_width);
6558
6559 DRI ("$0", 0, mips_reg_width);
6560 DRI ("$1", 1, mips_reg_width);
6561 DRI ("$2", 2, mips_reg_width);
6562 DRI ("$3", 3, mips_reg_width);
6563 DRI ("$4", 4, mips_reg_width);
6564 DRI ("$5", 5, mips_reg_width);
6565 DRI ("$6", 6, mips_reg_width);
6566 DRI ("$7", 7, mips_reg_width);
6567 DRI ("$8", 8, mips_reg_width);
6568 DRI ("$9", 9, mips_reg_width);
6569 DRI ("$10", 10, mips_reg_width);
6570 DRI ("$11", 11, mips_reg_width);
6571 DRI ("$12", 12, mips_reg_width);
6572 DRI ("$13", 13, mips_reg_width);
6573 DRI ("$14", 14, mips_reg_width);
6574 DRI ("$15", 15, mips_reg_width);
6575 DRI ("$16", 16, mips_reg_width);
6576 DRI ("$17", 17, mips_reg_width);
6577 DRI ("$18", 18, mips_reg_width);
6578 DRI ("$19", 19, mips_reg_width);
6579 DRI ("$20", 20, mips_reg_width);
6580 DRI ("$21", 21, mips_reg_width);
6581 DRI ("$22", 22, mips_reg_width);
6582 DRI ("$23", 23, mips_reg_width);
6583 DRI ("$24", 24, mips_reg_width);
6584 DRI ("$25", 25, mips_reg_width);
6585 DRI ("$26", 26, mips_reg_width);
6586 DRI ("$27", 27, mips_reg_width);
6587 DRI ("$28", 28, mips_reg_width);
6588 DRI ("$29", 29, mips_reg_width);
6589 DRI ("$30", 30, mips_reg_width);
6590 DRI ("$31", 31, mips_reg_width);
2e6d617a
JL
6591 } else if (arg_count) {
6592 /* permit this case; just fall back to dwarf */
6593 }
6594#undef DRI
6595
6596 // Build regex pieces out of the known dwarf_regs. We keep two separate
6597 // lists: ones with the % prefix (and thus unambigiuous even despite PR11821),
6598 // and ones with no prefix (and thus only usable in unambiguous contexts).
b4662f6b 6599 for (auto ri = dwarf_regs.cbegin(); ri != dwarf_regs.cend(); ri++)
2e6d617a
JL
6600 {
6601 string regname = ri->first;
6602 assert (regname != "");
85356a4f
MN
6603 // for register names starting with '$' convert the dollar to a
6604 // '\$' as otherwise the regexp tries to match end-of-line
6605 if (regname[0]=='$')
6606 regname = string("\\")+regname;
2e6d617a
JL
6607 regnames += string("|")+regname;
6608 if (regname[0]=='%')
6609 percent_regnames += string("|")+regname;
6610 }
6611
6612 // clip off leading |
6613 if (regnames != "")
6614 regnames = regnames.substr(1);
6615 if (percent_regnames != "")
6616 percent_regnames = percent_regnames.substr(1);
6617}
7a05f484
SC
6618
6619void
6ef331c8 6620sdt_uprobe_var_expanding_visitor::visit_target_symbol_context (target_symbol* e)
7a05f484 6621{
6ef331c8 6622 if (e->addressof)
dc09353a 6623 throw SEMANTIC_ERROR(_("cannot take address of context variable"), e->tok);
6ef331c8
SC
6624
6625 if (e->name == "$$name")
7a05f484 6626 {
6ef331c8
SC
6627 literal_string *myname = new literal_string (probe_name);
6628 myname->tok = e->tok;
6629 provide(myname);
6630 return;
6631 }
7a05f484 6632
6ef331c8
SC
6633 else if (e->name == "$$provider")
6634 {
6635 literal_string *myname = new literal_string (provider_name);
6636 myname->tok = e->tok;
6637 provide(myname);
6638 return;
6639 }
a794dbeb 6640
6ef331c8
SC
6641 else if (e->name == "$$vars" || e->name == "$$parms")
6642 {
6643 e->assert_no_components("sdt", true);
6ef331c8
SC
6644
6645 // Convert $$vars to sprintf of a list of vars which we recursively evaluate
63ea4244 6646
1c922ad7 6647 print_format* pf = print_format::create(e->tok, "sprintf");
6ef331c8
SC
6648
6649 for (unsigned i = 1; i <= arg_count; ++i)
6650 {
6651 if (i > 1)
6652 pf->raw_components += " ";
6653 target_symbol *tsym = new target_symbol;
6654 tsym->tok = e->tok;
6655 tsym->name = "$arg" + lex_cast(i);
47d349b1 6656 pf->raw_components += tsym->name;
6ef331c8
SC
6657 tsym->components = e->components;
6658
8c2f50c0 6659 expression *texp = require<expression> (tsym);
c4965ad9 6660 if (e->check_pretty_print ())
6ef331c8
SC
6661 pf->raw_components += "=%s";
6662 else
6663 pf->raw_components += "=%#x";
6664 pf->args.push_back(texp);
6665 }
6666
6667 pf->components = print_format::string_to_components(pf->raw_components);
6668 provide (pf);
6669 }
6670 else
6671 assert(0); // shouldn't get here
6672}
6673
03c45fcb
JL
6674unsigned
6675sdt_uprobe_var_expanding_visitor::get_target_symbol_argno_and_validate (target_symbol *e)
6ef331c8 6676{
03c45fcb
JL
6677 // parsing
6678 unsigned argno = 0;
47d349b1 6679 if (startswith(e->name, "$arg"))
6ef331c8 6680 {
c69a87e0 6681 try
03c45fcb 6682 {
a3e980f9 6683 argno = lex_cast<unsigned>(e->name.substr(4).to_string());
03c45fcb
JL
6684 }
6685 catch (const runtime_error& f)
6686 {
6687 // non-integral $arg suffix: e.g. $argKKKSDF
8aabf152 6688 argno = 0;
03c45fcb
JL
6689 }
6690 }
8aabf152 6691
03c45fcb
JL
6692 // validation
6693 if (arg_count == 0 || // a sdt.h variant without .probe-stored arg_count
6694 argno < 1 || argno > arg_count) // a $argN with out-of-range N
6695 {
6696 // NB: Either
6697 // 1) uprobe1_type $argN or $FOO (we don't know the arg_count)
6698 // 2) uprobe2_type $FOO (no probe args)
6699 // both of which get resolved later.
6700 // Throw it now, and it might be resolved by DWARF later.
6701 need_debug_info = true;
6702 throw SEMANTIC_ERROR(_("target-symbol requires debuginfo"), e->tok);
6703 }
6704 assert (arg_tokens.size() >= argno);
6705 return argno;
6706}
40fe32e0 6707
ac074daf
JL
6708long
6709sdt_uprobe_var_expanding_visitor::parse_out_arg_precision(string& asmarg)
6710{
6711 long precision;
6712 if (asmarg.find('@') != string::npos)
6713 {
6714 precision = lex_cast<int>(asmarg.substr(0, asmarg.find('@')));
6715 asmarg = asmarg.substr(asmarg.find('@')+1);
6716 }
6717 else
6718 {
6719 // V1/V2 do not have precision field so default to signed long
6720 // V3 asm does not have precision field so default to unsigned long
6721 if (probe_type == uprobe3_type)
6722 precision = sizeof(long); // this is an asm probe
71e5e13d 6723 else
ac074daf
JL
6724 precision = -sizeof(long);
6725 }
6726 return precision;
6727}
40fe32e0 6728
0fbf6fb8
JL
6729expression*
6730sdt_uprobe_var_expanding_visitor::try_parse_arg_literal (target_symbol *e,
6731 const string& asmarg,
6732 long precision)
6733{
6734 expression *argexpr = NULL;
6735
6736 // Here, we test for a numeric literal.
6737 // Only accept (signed) decimals throughout. XXX
6738
6739 // PR11821. NB: on powerpc, literals are not prefixed with $,
6740 // so this regex does not match. But that's OK, since without
6741 // -mregnames, we can't tell them apart from register numbers
6742 // anyway. With -mregnames, we could, if gcc somehow
6743 // communicated to us the presence of that option, but alas it
6744 // doesn't. http://gcc.gnu.org/PR44995.
6745 vector<string> matches;
7be1b19f
WC
6746 string regexp;
6747
85356a4f 6748 if (elf_machine == EM_AARCH64 || elf_machine == EM_MIPS) {
7be1b19f
WC
6749 regexp = "^([-]?[0-9][0-9]*)$";
6750 } else {
6751 regexp = "^[i\\$#]([-]?[0-9][0-9]*)$";
6752 }
6753
6754 if (!regexp_match (asmarg, regexp, matches)) {
6755 string sn =matches[1];
0fbf6fb8
JL
6756 int64_t n;
6757
6758 // We have to pay attention to the size & sign, as gcc sometimes
6759 // propagates constants that don't quite match, like a negative
6760 // value to fill an unsigned type.
6761 // NB: let it throw if something happens
6762 switch (precision)
8aabf152 6763 {
0fbf6fb8
JL
6764 case -1: n = lex_cast< int8_t>(sn); break;
6765 case 1: n = lex_cast< uint8_t>(sn); break;
6766 case -2: n = lex_cast< int16_t>(sn); break;
6767 case 2: n = lex_cast<uint16_t>(sn); break;
6768 case -4: n = lex_cast< int32_t>(sn); break;
6769 case 4: n = lex_cast<uint32_t>(sn); break;
6770 default:
6771 case -8: n = lex_cast< int64_t>(sn); break;
6772 case 8: n = lex_cast<uint64_t>(sn); break;
8aabf152
FCE
6773 }
6774
0fbf6fb8
JL
6775 literal_number* ln = new literal_number(n);
6776 ln->tok = e->tok;
6777 argexpr = ln;
6778 }
b78a0fbb 6779
0fbf6fb8
JL
6780 return argexpr;
6781}
6782
95b78bc9
JL
6783expression*
6784sdt_uprobe_var_expanding_visitor::try_parse_arg_register (target_symbol *e,
6785 const string& asmarg,
6786 long precision)
6787{
6788 expression *argexpr = NULL;
6789
6790 // test for REGISTER
6791 // NB: Because PR11821, we must use percent_regnames here.
6792 string regexp;
4380196b
WC
6793 if (elf_machine == EM_PPC || elf_machine == EM_PPC64
6794 || elf_machine == EM_ARM || elf_machine == EM_AARCH64)
95b78bc9
JL
6795 regexp = "^(" + regnames + ")$";
6796 else
6797 regexp = "^(" + percent_regnames + ")$";
6798
6799 vector<string> matches;
6800 if (!regexp_match(asmarg, regexp, matches))
6801 {
6802 string regname = matches[1];
b4662f6b 6803 auto ri = dwarf_regs.find (regname);
95b78bc9 6804 if (ri != dwarf_regs.end()) // known register
8aabf152 6805 {
95b78bc9
JL
6806 embedded_expr *get_arg1 = new embedded_expr;
6807 string width_adjust;
6808 switch (ri->second.second)
8aabf152 6809 {
95b78bc9
JL
6810 case QI: width_adjust = ") & 0xff)"; break;
6811 case QIh: width_adjust = ">>8) & 0xff)"; break;
6812 case HI:
6813 // preserve 16 bit register signness
6814 width_adjust = ") & 0xffff)";
6815 if (precision < 0)
6816 width_adjust += " << 48 >> 48";
6817 break;
6818 case SI:
6819 // preserve 32 bit register signness
6820 width_adjust = ") & 0xffffffff)";
6821 if (precision < 0)
6822 width_adjust += " << 32 >> 32";
6823 break;
6824 default: width_adjust = "))";
8aabf152 6825 }
95b78bc9
JL
6826 string type = "";
6827 if (probe_type == uprobe3_type)
6828 type = (precision < 0
6829 ? "(int" : "(uint") + lex_cast(abs(precision) * 8) + "_t)";
6830 type = type + "((";
6831 get_arg1->tok = e->tok;
6832 get_arg1->code = string("/* unprivileged */ /* pure */")
6833 + string(" ((int64_t)") + type
a57b7534 6834 + string("u_fetch_register(")
95b78bc9
JL
6835 + lex_cast(dwarf_regs[regname].first) + string("))")
6836 + width_adjust;
6837 argexpr = get_arg1;
8aabf152 6838 }
95b78bc9
JL
6839 }
6840 return argexpr;
6841}
40fe32e0 6842
b15f98b2
JL
6843static string
6844precision_to_function(long precision)
6845{
6846 switch (precision)
6847 {
6848 case 1: case -1:
6849 return "user_int8";
6850 case 2:
6851 return "user_uint16";
6852 case -2:
6853 return "user_int16";
6854 case 4:
6855 return "user_uint32";
6856 case -4:
6857 return "user_int32";
6858 case 8: case -8:
6859 return "user_int64";
6860 default:
6861 return "user_long";
6862 }
6863}
8aabf152 6864
8da56b62
JL
6865expression*
6866sdt_uprobe_var_expanding_visitor::try_parse_arg_offset_register (target_symbol *e,
6867 const string& asmarg,
6868 long precision)
6869{
6870 expression *argexpr = NULL;
8095a157 6871
8da56b62
JL
6872 // test for OFFSET(REGISTER) where OFFSET is +-N+-N+-N
6873 // NB: Despite PR11821, we can use regnames here, since the parentheses
6874 // make things unambiguous. (Note: gdb/stap-probe.c also parses this)
6875 // On ARM test for [REGISTER, OFFSET]
8095a157 6876
8da56b62
JL
6877 string regexp;
6878 int reg, offset1;
4380196b 6879 if (elf_machine == EM_ARM || elf_machine == EM_AARCH64)
8da56b62 6880 {
297745ba 6881 regexp = "^\\[(" + regnames + ")(,[ ]*[#]?([+-]?[0-9]+)([+-][0-9]*)?([+-][0-9]*)?)?\\]$";
8da56b62
JL
6882 reg = 1;
6883 offset1 = 3;
6884 }
6885 else
6886 {
6887 regexp = "^([+-]?[0-9]*)([+-][0-9]*)?([+-][0-9]*)?[(](" + regnames + ")[)]$";
6888 reg = 4;
6889 offset1 = 1;
6890 }
6891
6892 vector<string> matches;
6893 if (!regexp_match(asmarg, regexp, matches))
6894 {
6895 string regname;
6896 int64_t disp = 0;
6897 if (matches[reg].length())
6898 regname = matches[reg];
6899 if (dwarf_regs.find (regname) == dwarf_regs.end())
6900 throw SEMANTIC_ERROR(_F("unrecognized register '%s'", regname.c_str()));
6901
6902 for (int i=offset1; i <= (offset1 + 2); i++)
6903 if (matches[i].length())
6904 // should decode positive/negative hex/decimal
6905 // NB: let it throw if something happens
6906 disp += lex_cast<int64_t>(matches[i]);
6907
6908 // synthesize user_long(%{fetch_register(R)%} + D)
6909 embedded_expr *get_arg1 = new embedded_expr;
6910 get_arg1->tok = e->tok;
6911 get_arg1->code = string("/* unprivileged */ /* pure */")
a57b7534 6912 + string("u_fetch_register(")
8da56b62
JL
6913 + lex_cast(dwarf_regs[regname].first) + string(")");
6914 // XXX: may we ever need to cast that to a narrower type?
6915
6916 literal_number* inc = new literal_number(disp);
6917 inc->tok = e->tok;
6918
6919 binary_expression *be = new binary_expression;
6920 be->tok = e->tok;
6921 be->left = get_arg1;
6922 be->op = "+";
6923 be->right = inc;
6924
6925 functioncall *fc = new functioncall;
b15f98b2 6926 fc->function = precision_to_function(precision);
8da56b62
JL
6927 fc->tok = e->tok;
6928 fc->args.push_back(be);
8095a157 6929
8da56b62
JL
6930 argexpr = fc;
6931 }
8095a157 6932
8da56b62
JL
6933 return argexpr;
6934}
8095a157 6935
c826ad73
FCE
6936expression*
6937sdt_uprobe_var_expanding_visitor::try_parse_arg_register_pair (target_symbol *e,
6938 const string& asmarg,
6939 long precision)
6940{
6941 // BZ1613157: for powerpc, accept "R,R", as an alias of "(Ra,Rb)"
6942 if (sess.architecture.substr(0,7) != "powerpc")
6943 return NULL;
6944
6945 // test for BASE_REGISTER,INDEX_REGISTER
6946 string regexp = "^(" + regnames + "),(" + regnames + ")$";
6947 vector<string> matches;
6948 if (!regexp_match(asmarg, regexp, matches))
6949 {
6950 // delegate to parenthetic syntax
6951 return try_parse_arg_effective_addr (e, string("(")+asmarg+string(")"), precision);
6952 }
6953
6954 return NULL;
6955}
6956
10fcca15
JL
6957expression*
6958sdt_uprobe_var_expanding_visitor::try_parse_arg_effective_addr (target_symbol *e,
6959 const string& asmarg,
6960 long precision)
6961{
6962 expression *argexpr = NULL;
6963
6964 // test for OFFSET(BASE_REGISTER,INDEX_REGISTER[,SCALE]) where OFFSET is +-N+-N+-N
6965 // NB: Despite PR11821, we can use regnames here, since the parentheses
6966 // make things unambiguous. (Note: gdb/stap-probe.c also parses this)
6967 string regexp = "^([+-]?[0-9]*)([+-][0-9]*)?([+-][0-9]*)?[(](" + regnames + "),(" +
6968 regnames + ")(,[1248])?[)]$";
6969 vector<string> matches;
6970 if (!regexp_match(asmarg, regexp, matches))
6971 {
6972 string baseregname;
6973 string indexregname;
6974 int64_t disp = 0;
6975 short scale = 1;
6976
6977 if (matches[6].length())
6978 // NB: let it throw if we can't cast
6979 scale = lex_cast<short>(matches[6].substr(1)); // NB: skip the comma!
6980
6981 if (matches[4].length())
6982 baseregname = matches[4];
6983 if (dwarf_regs.find (baseregname) == dwarf_regs.end())
6984 throw SEMANTIC_ERROR(_F("unrecognized base register '%s'", baseregname.c_str()));
6985
6986 if (matches[5].length())
6987 indexregname = matches[5];
6988 if (dwarf_regs.find (indexregname) == dwarf_regs.end())
6989 throw SEMANTIC_ERROR(_F("unrecognized index register '%s'", indexregname.c_str()));
6990
6991 for (int i = 1; i <= 3; i++) // up to three OFFSET terms
6992 if (matches[i].length())
6993 // should decode positive/negative hex/decimal
6994 // NB: let it throw if something happens
6995 disp += lex_cast<int64_t>(matches[i]);
6996
6997 // synthesize user_long(%{fetch_register(R1)+fetch_register(R2)*N%} + D)
6998
6999 embedded_expr *get_arg1 = new embedded_expr;
a57b7534 7000 string regfn = "u_fetch_register";
10fcca15
JL
7001
7002 get_arg1->tok = e->tok;
7003 get_arg1->code = string("/* unprivileged */ /* pure */")
7004 + regfn + string("(")+lex_cast(dwarf_regs[baseregname].first)+string(")")
7005 + string("+(")
7006 + regfn + string("(")+lex_cast(dwarf_regs[indexregname].first)+string(")")
7007 + string("*")
7008 + lex_cast(scale)
7009 + string(")");
7010
7011 // NB: could plop this +DISPLACEMENT bit into the embedded-c expression too
7012 literal_number* inc = new literal_number(disp);
7013 inc->tok = e->tok;
7014
7015 binary_expression *be = new binary_expression;
7016 be->tok = e->tok;
7017 be->left = get_arg1;
7018 be->op = "+";
7019 be->right = inc;
7020
7021 functioncall *fc = new functioncall;
b15f98b2 7022 fc->function = precision_to_function(precision);
10fcca15
JL
7023 fc->tok = e->tok;
7024 fc->args.push_back(be);
8095a157 7025
10fcca15
JL
7026 argexpr = fc;
7027 }
8095a157 7028
10fcca15
JL
7029 return argexpr;
7030}
8095a157 7031
c826ad73 7032
451c0c25
JL
7033expression*
7034sdt_uprobe_var_expanding_visitor::try_parse_arg_varname (target_symbol *e,
7035 const string& asmarg,
7036 long precision)
6ef331c8 7037{
b8688ee6 7038 static unsigned tick = 0;
451c0c25
JL
7039 expression *argexpr = NULL;
7040
7041 // test for [OFF+]VARNAME[+OFF][(REGISTER)], where VARNAME is a variable
7042 // name. NB: Despite PR11821, we can use regnames here, since the parentheses
7043 // make things unambiguous.
7044 string regex = "^(([0-9]+)[+])?([a-zA-Z_][a-zA-Z0-9_]*)([+][0-9]+)?([(]("
7045 + regnames + ")[)])?$";
7046 vector<string> matches;
7047 if (!regexp_match(asmarg, regex, matches))
7048 {
7049 assert(matches.size() >= 4);
47d349b1 7050 interned_string varname = matches[3];
451c0c25
JL
7051
7052 // OFF can be before VARNAME (put in matches[2]) or after (put in
7053 // matches[4]) (or both?). Seems like in most cases it comes after,
7054 // unless the code was compiled with -fPIC.
7055 int64_t offset = 0;
7056 if (!matches[2].empty())
7057 offset += lex_cast<int64_t>(matches[2]);
7058 if (matches.size() >= 5 && !matches[4].empty())
7059 offset += lex_cast<int64_t>(matches[4]);
7060
7061 string regname;
7062 if (matches.size() >= 7)
7063 regname = matches[6];
7064
7065 // If it's just VARNAME, then proceed. If it's VARNAME(REGISTER), then
7066 // only proceed if it's RIP-relative addressing on x86_64.
7067 if (regname.empty() || (regname == "%rip" && elf_machine == EM_X86_64))
7068 {
7069 dw.mod_info->get_symtab();
7070 if (dw.mod_info->symtab_status != info_present)
7071 throw SEMANTIC_ERROR(_("can't retrieve symbol table"));
7072
7073 assert(dw.mod_info->sym_table);
45a63356
FCE
7074 unordered_map<interned_string, Dwarf_Addr>& globals = dw.mod_info->sym_table->globals;
7075 unordered_map<interned_string, Dwarf_Addr>& locals = dw.mod_info->sym_table->locals;
451c0c25
JL
7076 Dwarf_Addr addr = 0;
7077
7078 // check symtab locals then globals
7079 if (locals.count(varname))
7080 addr = locals[varname];
7081 if (globals.count(varname))
7082 addr = globals[varname];
7083
7084 if (addr)
8095a157 7085 {
451c0c25
JL
7086 // add whatever offset is in the operand
7087 addr += offset;
7088
7089 // adjust for dw bias because relocate_address() expects a
7090 // libdw address and this addr is from the symtab
7091 dw.get_module_dwarf(false, false);
7092 addr -= dw.module_bias;
7093
45a63356 7094 interned_string reloc_section;
451c0c25
JL
7095 Dwarf_Addr reloc_addr = dw.relocate_address(addr, reloc_section);
7096
7097 // OK, we have an address for the variable. Let's create a
7098 // function that will just relocate it at runtime, and then
7099 // call user_[u]int*() on the address it returns.
7100
7101 functioncall *user_int_call = new functioncall;
b15f98b2 7102 user_int_call->function = precision_to_function(precision);
451c0c25
JL
7103 user_int_call->tok = e->tok;
7104
8f54215e 7105 string fhash = detox_path(string(e->tok->location.file->name));
451c0c25
JL
7106 functiondecl *get_addr_decl = new functiondecl;
7107 get_addr_decl->tok = e->tok;
7108 get_addr_decl->synthetic = true;
8ebc4425
DS
7109 get_addr_decl->unmangled_name = get_addr_decl->name =
7110 "__private_" + fhash + "_sdt_arg_get_addr_" + lex_cast(tick++);
451c0c25
JL
7111 get_addr_decl->type = pe_long;
7112
6bc53f02 7113 // build _stp_umodule_relocate(module, addr, current)
451c0c25
JL
7114 stringstream ss;
7115 ss << " /* unprivileged */ /* pure */ /* pragma:vma */" << endl;
a57b7534 7116 ss << "STAP_RETURN(_stp_umodule_relocate(";
16aa72d9 7117 ss << "\"" << path_remove_sysroot(sess, process_name) << "\", ";
a57b7534
JL
7118 ss << "0x" << hex << reloc_addr << dec << ", ";
7119 ss << "current";
7120 ss << "));" << endl;
451c0c25
JL
7121
7122 embeddedcode *ec = new embeddedcode;
7123 ec->tok = e->tok;
7124 ec->code = ss.str();
7125 get_addr_decl->body = ec;
16aa72d9 7126 get_addr_decl->join(sess);
451c0c25
JL
7127
7128 functioncall *get_addr_call = new functioncall;
7129 get_addr_call->tok = e->tok;
7130 get_addr_call->function = get_addr_decl->name;
7131 user_int_call->args.push_back(get_addr_call);
b8688ee6 7132
451c0c25 7133 argexpr = user_int_call;
8095a157 7134 }
8aabf152 7135 }
451c0c25
JL
7136 }
7137
7138 return argexpr;
7139}
7140
7141void
7142sdt_uprobe_var_expanding_visitor::visit_target_symbol_arg (target_symbol *e)
7143{
6ef331c8
SC
7144 try
7145 {
03c45fcb 7146 unsigned argno = get_target_symbol_argno_and_validate(e); // the N in $argN
8aabf152 7147 string asmarg = arg_tokens[argno-1]; // $arg1 => arg_tokens[0]
8aabf152 7148
8aabf152
FCE
7149 // Now we try to parse this thing, which is an assembler operand
7150 // expression. If we can't, we warn, back down to need_debug_info
b874bd52 7151 // and hope for the best. Here is the syntax for a few architectures.
9859b766 7152 // Note that the power iN syntax is only for V3 sdt.h; gcc emits the i.
8095a157 7153 //
b6aaaf21
JL
7154 // literal reg reg reg+ base+index*size+ VAR VAR+off RIP-relative
7155 // indirect offset offset VAR+off
7156 // x86 $N %rR (%rR) N(%rR) O(%bR,%iR,S) var var+off var+off(%rip)
7157 // x86_64 $N %rR (%rR) N(%rR) O(%bR,%iR,S) var var+off var+off(%rip)
c826ad73 7158 // power iN R (R) N(R) R,R
b6aaaf21
JL
7159 // ia64 N rR [r16]
7160 // s390 N %rR 0(rR) N(r15)
7161 // arm #N rR [rR] [rR, #N]
7be1b19f 7162 // arm64 N rR [rR] [rR, N]
85356a4f 7163 // mips N $r N($r)
b874bd52 7164
8aabf152
FCE
7165 expression* argexpr = 0; // filled in in case of successful parse
7166
ac074daf
JL
7167 // Parse (and remove from asmarg) the leading length
7168 long precision = parse_out_arg_precision(asmarg);
40fe32e0 7169
0fbf6fb8 7170 try
8aabf152 7171 {
0fbf6fb8
JL
7172 if ((argexpr = try_parse_arg_literal(e, asmarg, precision)) != NULL)
7173 goto matched;
2e6d617a
JL
7174
7175 // all other matches require registers
7176 if (regnames == "")
7177 throw SEMANTIC_ERROR("no registers to use for parsing");
95b78bc9
JL
7178
7179 if ((argexpr = try_parse_arg_register(e, asmarg, precision)) != NULL)
7180 goto matched;
8da56b62 7181 if ((argexpr = try_parse_arg_offset_register(e, asmarg, precision)) != NULL)
c826ad73
FCE
7182 goto matched;
7183 if ((argexpr = try_parse_arg_register_pair(e, asmarg, precision)) != NULL)
8da56b62 7184 goto matched;
10fcca15
JL
7185 if ((argexpr = try_parse_arg_effective_addr(e, asmarg, precision)) != NULL)
7186 goto matched;
451c0c25
JL
7187 if ((argexpr = try_parse_arg_varname(e, asmarg, precision)) != NULL)
7188 goto matched;
0fbf6fb8 7189 }
5e50524f 7190 catch (const semantic_error& er)
0fbf6fb8 7191 {
5e50524f
FCE
7192 if (sess.verbose > 3)
7193 clog << "chaining to " << *e->tok << endl
7194 << sess.build_error_msg(er) << endl;
7195 e->chain(er);
b8688ee6 7196 }
8aabf152 7197
8aabf152 7198 // The asmarg operand was not recognized. Back down to dwarf.
16aa72d9 7199 if (! sess.suppress_warnings)
84fef8ee
FCE
7200 {
7201 if (probe_type == UPROBE3_TYPE)
16aa72d9
FCE
7202 sess.print_warning (_F("Can't parse SDT_V3 operand '%s' "
7203 "[man error::sdt]", asmarg.c_str()),
7204 e->tok);
84fef8ee 7205 else // must be *PROBE2; others don't get asm operands
16aa72d9
FCE
7206 sess.print_warning (_F("Downgrading SDT_V2 probe argument to "
7207 "dwarf, can't parse '%s' [man error::sdt]",
7208 asmarg.c_str()),
7209 e->tok);
84fef8ee 7210 }
451c0c25 7211
8aabf152 7212 need_debug_info = true;
451c0c25
JL
7213 throw SEMANTIC_ERROR(_("SDT asm not understood, requires debuginfo "
7214 "[man error::sdt]"), e->tok);
7215
7216 /* NOTREACHED */
366af4e7 7217
8aabf152
FCE
7218 matched:
7219 assert (argexpr != 0);
366af4e7 7220
16aa72d9 7221 if (sess.verbose > 2)
1e41115c 7222 //TRANSLATORS: We're mapping the operand to a new expression*.
b530b5b3 7223 clog << _F("mapped asm operand %s to ", asmarg.c_str()) << *argexpr << endl;
366af4e7 7224
aff5d390 7225 if (e->components.empty()) // We have a scalar
8aabf152
FCE
7226 {
7227 if (e->addressof)
dc09353a 7228 throw SEMANTIC_ERROR(_("cannot take address of sdt variable"), e->tok);
8aabf152 7229 provide (argexpr);
8aabf152
FCE
7230 }
7231 else // $var->foo
7232 {
7233 cast_op *cast = new cast_op;
7234 cast->name = "@cast";
7235 cast->tok = e->tok;
7236 cast->operand = argexpr;
7237 cast->components = e->components;
46ccda7c 7238 cast->type_name = (string)probe_name + "_arg" + lex_cast(argno);
8aabf152
FCE
7239 cast->module = process_name;
7240 cast->visit(this);
8aabf152 7241 }
aff5d390
SC
7242 }
7243 catch (const semantic_error &er)
7244 {
5e50524f
FCE
7245 if (sess.verbose > 3)
7246 clog << "chaining to " << *e->tok << endl
7247 << sess.build_error_msg(er) << endl;
aff5d390
SC
7248 e->chain (er);
7249 provide (e);
7250 }
7251}
7252
7253
6ef331c8
SC
7254void
7255sdt_uprobe_var_expanding_visitor::visit_target_symbol (target_symbol* e)
7256{
7257 try
7258 {
49131a6d 7259 assert(e->name.size() > 0
bd1fcbad 7260 && (e->name[0] == '$' || e->name == "@var"));
6ef331c8
SC
7261
7262 if (e->name == "$$name" || e->name == "$$provider" || e->name == "$$parms" || e->name == "$$vars")
7263 visit_target_symbol_context (e);
7264 else
7265 visit_target_symbol_arg (e);
7266 }
7267 catch (const semantic_error &er)
7268 {
5e50524f
FCE
7269 if (sess.verbose > 3)
7270 clog << "chaining to " << *e->tok << endl
7271 << sess.build_error_msg(er) << endl;
6ef331c8
SC
7272 e->chain (er);
7273 provide (e);
7274 }
7275}
7276
7277
bd1fcbad
YZ
7278void
7279sdt_uprobe_var_expanding_visitor::visit_atvar_op (atvar_op* e)
7280{
7281 need_debug_info = true;
7282
7283 // Fill in our current module context if needed
7284 if (e->module.empty())
7285 e->module = process_name;
7286
7287 var_expanding_visitor::visit_atvar_op(e);
7288}
7289
7290
40a0c64e
JS
7291void
7292sdt_uprobe_var_expanding_visitor::visit_cast_op (cast_op* e)
7293{
7294 // Fill in our current module context if needed
7295 if (e->module.empty())
7296 e->module = process_name;
7297
7298 var_expanding_visitor::visit_cast_op(e);
7299}
7300
7301
576eaefe
SC
7302void
7303plt_expanding_visitor::visit_target_symbol (target_symbol *e)
7304{
7305 try
7306 {
7307 if (e->name == "$$name")
7308 {
7309 literal_string *myname = new literal_string (entry);
7310 myname->tok = e->tok;
7311 provide(myname);
7312 return;
7313 }
3d69c03f
JS
7314
7315 // variable not found -> throw a semantic error
7316 // (only to be caught right away, but this may be more complex later...)
7317 string alternatives = "$$name";
dc09353a 7318 throw SEMANTIC_ERROR(_F("unable to find plt variable '%s' (alternatives: %s)",
7371cd19 7319 e->name.to_string().c_str(), alternatives.c_str()), e->tok);
576eaefe
SC
7320 }
7321 catch (const semantic_error &er)
7322 {
5e50524f
FCE
7323 if (sess.verbose > 3)
7324 clog << "chaining to " << *e->tok << endl
7325 << sess.build_error_msg(er) << endl;
576eaefe
SC
7326 e->chain (er);
7327 provide (e);
7328 }
7329}
7330
7331
edce5b67
JS
7332struct sdt_query : public base_query
7333{
7334 sdt_query(probe * base_probe, probe_point * base_loc,
7335 dwflpp & dw, literal_map_t const & params,
51d6bda3 7336 vector<derived_probe *> & results, const string user_lib);
edce5b67 7337
51d6bda3 7338 void query_library (const char *data);
614f0fcf
JL
7339 set<string> visited_libraries;
7340 bool resolved_library;
7341
dabd71bb 7342 void query_plt (const char *, size_t) {}
edce5b67
JS
7343 void handle_query_module();
7344
7345private:
15284963 7346 stap_sdt_probe_type probe_type;
d61ea602 7347 enum { probe_section=0, note_section=1, unknown_section=-1 } probe_loc;
edce5b67
JS
7348 probe * base_probe;
7349 probe_point * base_loc;
6846cfc8 7350 literal_map_t const & params;
edce5b67 7351 vector<derived_probe *> & results;
45a63356
FCE
7352 interned_string pp_mark;
7353 interned_string pp_provider;
51d6bda3 7354 string user_lib;
edce5b67
JS
7355
7356 set<string> probes_handled;
7357
7358 Elf_Data *pdata;
7359 size_t probe_scn_offset;
7360 size_t probe_scn_addr;
aff5d390 7361 uint64_t arg_count;
40fe32e0 7362 GElf_Addr base;
c57ea854 7363 GElf_Addr pc;
aff5d390 7364 string arg_string;
edce5b67 7365 string probe_name;
a794dbeb 7366 string provider_name;
909ab234 7367 GElf_Addr semaphore_load_offset;
79a0ca08 7368 Dwarf_Addr semaphore;
edce5b67
JS
7369
7370 bool init_probe_scn();
6b51ee12 7371 bool get_next_probe();
c57ea854
SC
7372 void iterate_over_probe_entries();
7373 void handle_probe_entry();
edce5b67 7374
488d3c1b 7375 static void setup_note_probe_entry_callback (sdt_query *me,
dbd24034
JL
7376 const string& scn_name,
7377 const string& note_name,
488d3c1b
JL
7378 int type,
7379 const char *data,
7380 size_t len);
dbd24034
JL
7381 void setup_note_probe_entry (const string& scn_name,
7382 const string& note_name, int type,
7383 const char *data, size_t len);
40fe32e0 7384
edce5b67 7385 void convert_probe(probe *base);
4ddb6dd0 7386 void record_semaphore(vector<derived_probe *> & results, unsigned start);
c72aa911 7387 probe* convert_location();
40fe32e0 7388 bool have_uprobe() {return probe_type == uprobe1_type || probe_type == uprobe2_type || probe_type == uprobe3_type;}
c57ea854
SC
7389 bool have_debuginfo_uprobe(bool need_debug_info)
7390 {return probe_type == uprobe1_type
40fe32e0 7391 || ((probe_type == uprobe2_type || probe_type == uprobe3_type)
c57ea854 7392 && need_debug_info);}
40fe32e0 7393 bool have_debuginfoless_uprobe() {return probe_type == uprobe2_type || probe_type == uprobe3_type;}
edce5b67
JS
7394};
7395
7396
7397sdt_query::sdt_query(probe * base_probe, probe_point * base_loc,
7398 dwflpp & dw, literal_map_t const & params,
51d6bda3 7399 vector<derived_probe *> & results, const string user_lib):
614f0fcf
JL
7400 base_query(dw, params), resolved_library(false),
7401 probe_type(unknown_probe_type), probe_loc(unknown_section),
7402 base_probe(base_probe), base_loc(base_loc), params(params), results(results),
45a1aa33 7403 user_lib(user_lib), pdata(0), probe_scn_offset(0), probe_scn_addr(0), arg_count(0),
614f0fcf 7404 base(0), pc(0), semaphore_load_offset(0), semaphore(0)
edce5b67 7405{
a794dbeb
FCE
7406 assert(get_string_param(params, TOK_MARK, pp_mark));
7407 get_string_param(params, TOK_PROVIDER, pp_provider); // pp_provider == "" -> unspecified
7408
ef428667
FCE
7409 // PR10245: permit usage of dtrace-y "-" separator in marker name;
7410 // map it to double-underscores.
7411 size_t pos = 0;
45a63356 7412 string pp_mark2 = pp_mark; // copy for string replacement processing
ef428667
FCE
7413 while (1) // there may be more than one
7414 {
45a63356 7415 size_t i = pp_mark2.find("-", pos);
ef428667 7416 if (i == string::npos) break;
45a63356 7417 pp_mark2.replace (i, 1, "__");
ef428667
FCE
7418 pos = i+1; // resume searching after the inserted __
7419 }
45a63356 7420 pp_mark = pp_mark2;
a794dbeb
FCE
7421
7422 // XXX: same for pp_provider?
edce5b67
JS
7423}
7424
7425
7426void
c57ea854 7427sdt_query::handle_probe_entry()
edce5b67 7428{
c57ea854
SC
7429 if (! have_uprobe()
7430 && !probes_handled.insert(probe_name).second)
edce5b67
JS
7431 return;
7432
7433 if (sess.verbose > 3)
c57ea854 7434 {
b78a0fbb 7435 //TRANSLATORS: Describing what probe type (kprobe or uprobe) the probe
b530b5b3
LB
7436 //TRANSLATORS: is matched to.
7437 clog << _F("matched probe_name %s probe type ", probe_name.c_str());
c57ea854
SC
7438 switch (probe_type)
7439 {
7440 case uprobe1_type:
7441 clog << "uprobe1 at 0x" << hex << pc << dec << endl;
7442 break;
7443 case uprobe2_type:
7444 clog << "uprobe2 at 0x" << hex << pc << dec << endl;
7445 break;
40fe32e0
SC
7446 case uprobe3_type:
7447 clog << "uprobe3 at 0x" << hex << pc << dec << endl;
7448 break;
d61ea602
JS
7449 default:
7450 clog << "unknown!" << endl;
7451 break;
c57ea854
SC
7452 }
7453 }
edce5b67 7454
c57ea854
SC
7455 // Extend the derivation chain
7456 probe *new_base = convert_location();
7457 probe_point *new_location = new_base->locations[0];
7458
c57ea854
SC
7459 bool need_debug_info = false;
7460
7d395255
JS
7461 // We could get the Elf* from either dwarf_getelf(dwfl_module_getdwarf(...))
7462 // or dwfl_module_getelf(...). We only need it for the machine type, which
7463 // should be the same. The bias is used for relocating debuginfoless probes,
7464 // though, so that must come from the possibly-prelinked ELF file, not DWARF.
c57ea854 7465 Dwarf_Addr bias;
7d395255 7466 Elf* elf = dwfl_module_getelf (dw.mod_info->mod, &bias);
c57ea854 7467
1cc41cd6
DS
7468 /* Figure out the architecture of this particular ELF file. The
7469 dwarfless register-name mappings depend on it. */
7470 GElf_Ehdr ehdr_mem;
7471 GElf_Ehdr* em = gelf_getehdr (elf, &ehdr_mem);
e1c3b13a 7472 if (em == 0) { DWFL_ASSERT ("dwfl_getehdr", dwfl_errno()); }
d8f31d0a 7473 assert(em);
1cc41cd6 7474 int elf_machine = em->e_machine;
b8688ee6
JL
7475 sdt_uprobe_var_expanding_visitor svv (sess, dw, elf_machine, module_val,
7476 provider_name, probe_name, probe_type,
7477 arg_string, arg_count);
16aa72d9
FCE
7478 var_expand_const_fold_loop (sess, new_base->body, svv);
7479
1cc41cd6 7480 need_debug_info = svv.need_debug_info;
c57ea854 7481
1cc41cd6
DS
7482 // XXX: why not derive_probes() in the uprobes case too?
7483 literal_map_t params;
7484 for (unsigned i = 0; i < new_location->components.size(); ++i)
7485 {
7486 probe_point::component *c = new_location->components[i];
47d349b1 7487 params[c->functor] = c->arg;
1cc41cd6 7488 }
c57ea854 7489
73d53dd2 7490 unsigned prior_results_size = results.size();
1cc41cd6
DS
7491 dwarf_query q(new_base, new_location, dw, params, results, "", "");
7492 q.has_mark = true; // enables mid-statement probing
30263a73 7493
73d53dd2
JS
7494 // V1 probes always need dwarf info
7495 // V2+ probes need dwarf info in case of a variable reference
1cc41cd6 7496 if (have_debuginfo_uprobe(need_debug_info))
06de3a04 7497 dw.iterate_over_modules<base_query>(&query_module, &q);
73d53dd2
JS
7498
7499 // For V2+ probes, if variable references weren't used or failed (PR14369),
7500 // then try with the more direct approach. Unresolved $vars might still
7501 // cause their own error, but this gives them a chance to be optimized out.
7502 if (have_debuginfoless_uprobe() && results.size() == prior_results_size)
1cc41cd6
DS
7503 {
7504 string section;
7505 Dwarf_Addr reloc_addr = q.statement_num_val + bias;
7506 if (dwfl_module_relocations (q.dw.mod_info->mod) > 0)
7507 {
7508 dwfl_module_relocate_address (q.dw.mod_info->mod, &reloc_addr);
7509 section = ".dynamic";
7510 }
7511 else
7512 section = ".absolute";
edce5b67 7513
1cc41cd6
DS
7514 uprobe_derived_probe* p =
7515 new uprobe_derived_probe ("", "", 0,
7516 path_remove_sysroot(sess,q.module_val),
7517 section,
7518 q.statement_num_val, reloc_addr, q, 0);
7519 p->saveargs (arg_count);
7520 results.push_back (p);
c57ea854 7521 }
487bf4e2 7522 sess.unwindsym_modules.insert (dw.module_name);
73d53dd2 7523 record_semaphore(results, prior_results_size);
c57ea854 7524}
edce5b67 7525
4ddb6dd0 7526
c57ea854
SC
7527void
7528sdt_query::handle_query_module()
7529{
7530 if (!init_probe_scn())
7531 return;
edce5b67 7532
c57ea854
SC
7533 if (sess.verbose > 3)
7534 clog << "TOK_MARK: " << pp_mark << " TOK_PROVIDER: " << pp_provider << endl;
edce5b67 7535
40fe32e0
SC
7536 if (probe_loc == note_section)
7537 {
7538 GElf_Shdr shdr_mem;
7539 GElf_Shdr *shdr = dw.get_section (".stapsdt.base", &shdr_mem);
7540
909ab234 7541 // The 'base' lets us adjust the hardcoded addresses in notes for prelink
58597eb9
SC
7542 // effects. The 'semaphore_load_offset' is the load address of the .probes
7543 // section so the semaphore can be converted to a section offset if needed.
40fe32e0 7544 if (shdr)
909ab234
JS
7545 {
7546 base = shdr->sh_addr;
909ab234
JS
7547 shdr = dw.get_section (".probes", &shdr_mem);
7548 if (shdr)
58597eb9 7549 semaphore_load_offset = shdr->sh_addr - shdr->sh_offset;
909ab234 7550 }
40fe32e0 7551 else
909ab234
JS
7552 base = semaphore_load_offset = 0;
7553
488d3c1b 7554 dw.iterate_over_notes (this, &sdt_query::setup_note_probe_entry_callback);
40fe32e0 7555 }
d61ea602 7556 else if (probe_loc == probe_section)
40fe32e0 7557 iterate_over_probe_entries ();
edce5b67
JS
7558}
7559
7560
7561bool
7562sdt_query::init_probe_scn()
7563{
448a86b7 7564 Elf* elf;
edce5b67 7565 GElf_Shdr shdr_mem;
40fe32e0
SC
7566
7567 GElf_Shdr *shdr = dw.get_section (".note.stapsdt", &shdr_mem);
7568 if (shdr)
7569 {
7570 probe_loc = note_section;
7571 return true;
7572 }
edce5b67 7573
448a86b7 7574 shdr = dw.get_section (".probes", &shdr_mem, &elf);
fea74777 7575 if (shdr)
edce5b67 7576 {
fea74777
SC
7577 pdata = elf_getdata_rawchunk (elf, shdr->sh_offset, shdr->sh_size, ELF_T_BYTE);
7578 probe_scn_offset = 0;
7579 probe_scn_addr = shdr->sh_addr;
7580 assert (pdata != NULL);
7581 if (sess.verbose > 4)
ce0f6648
LB
7582 clog << "got .probes elf scn_addr@0x" << probe_scn_addr << ", size: "
7583 << pdata->d_size << endl;
40fe32e0 7584 probe_loc = probe_section;
fea74777 7585 return true;
edce5b67 7586 }
fea74777 7587 else
edce5b67 7588 return false;
edce5b67
JS
7589}
7590
40fe32e0 7591void
dbd24034
JL
7592sdt_query::setup_note_probe_entry_callback (sdt_query *me,
7593 const string& scn_name,
7594 const string& note_name, int type,
7595 const char *data, size_t len)
40fe32e0 7596{
dbd24034 7597 me->setup_note_probe_entry (scn_name, note_name, type, data, len);
40fe32e0
SC
7598}
7599
7600
7601void
dbd24034
JL
7602sdt_query::setup_note_probe_entry (const string& scn_name,
7603 const string& note_name, int type,
7604 const char *data, size_t len)
40fe32e0 7605{
dbd24034
JL
7606 if (scn_name.compare(".note.stapsdt"))
7607 return;
7608#define _SDT_NOTE_NAME "stapsdt"
7609 if (note_name.compare(_SDT_NOTE_NAME))
7610 return;
40fe32e0
SC
7611#define _SDT_NOTE_TYPE 3
7612 if (type != _SDT_NOTE_TYPE)
7613 return;
7614
dbd24034 7615 // we found a probe entry
40fe32e0
SC
7616 union
7617 {
7618 Elf64_Addr a64[3];
7619 Elf32_Addr a32[3];
7620 } buf;
7621 Dwarf_Addr bias;
7622 Elf* elf = (dwfl_module_getelf (dw.mod_info->mod, &bias));
7623 Elf_Data dst =
7624 {
7625 &buf, ELF_T_ADDR, EV_CURRENT,
7626 gelf_fsize (elf, ELF_T_ADDR, 3, EV_CURRENT), 0, 0
7627 };
7628 assert (dst.d_size <= sizeof buf);
7629
7630 if (len < dst.d_size + 3)
7631 return;
7632
7633 Elf_Data src =
7634 {
7635 (void *) data, ELF_T_ADDR, EV_CURRENT,
7636 dst.d_size, 0, 0
7637 };
7638
7639 if (gelf_xlatetom (elf, &dst, &src,
7640 elf_getident (elf, NULL)[EI_DATA]) == NULL)
7641 printf ("gelf_xlatetom: %s", elf_errmsg (-1));
7642
7643 probe_type = uprobe3_type;
7644 const char * provider = data + dst.d_size;
3f803f9e 7645
40fe32e0 7646 const char *name = (const char*)memchr (provider, '\0', data + len - provider);
3f803f9e
CM
7647 if(name++ == NULL)
7648 return;
7649
7650 const char *args = (const char*)memchr (name, '\0', data + len - name);
7651 if (args++ == NULL || memchr (args, '\0', data + len - name) != data + len - 1)
7652 return;
7653
7654 provider_name = provider;
7655 probe_name = name;
7656 arg_string = args;
40fe32e0 7657
e90006d7
JL
7658 dw.mod_info->marks.insert(make_pair(provider, name));
7659
40fe32e0
SC
7660 // Did we find a matching probe?
7661 if (! (dw.function_name_matches_pattern (probe_name, pp_mark)
7662 && ((pp_provider == "")
7663 || dw.function_name_matches_pattern (provider_name, pp_provider))))
7664 return;
7665
e9a90eee
JS
7666 // PR13934: Assembly probes are not forced to use the N@OP form.
7667 // If we have '@' then great, else count based on space-delimiters.
7668 arg_count = count(arg_string.begin(), arg_string.end(), '@');
7669 if (!arg_count && !arg_string.empty())
7670 arg_count = 1 + count(arg_string.begin(), arg_string.end(), ' ');
7671
40fe32e0
SC
7672 GElf_Addr base_ref;
7673 if (gelf_getclass (elf) == ELFCLASS32)
7674 {
7675 pc = buf.a32[0];
7676 base_ref = buf.a32[1];
7677 semaphore = buf.a32[2];
7678 }
7679 else
7680 {
7681 pc = buf.a64[0];
7682 base_ref = buf.a64[1];
7683 semaphore = buf.a64[2];
7684 }
7685
7686 semaphore += base - base_ref;
7687 pc += base - base_ref;
7688
7d395255
JS
7689 // The semaphore also needs the ELF bias added now, so
7690 // record_semaphore can properly relocate it later.
7691 semaphore += bias;
7692
40fe32e0 7693 if (sess.verbose > 4)
b530b5b3 7694 clog << _F(" saw .note.stapsdt %s%s ", probe_name.c_str(), (provider_name != "" ? _(" (provider ")+provider_name+") " : "").c_str()) << "@0x" << hex << pc << dec << endl;
40fe32e0
SC
7695
7696 handle_probe_entry();
7697}
7698
7699
c57ea854
SC
7700void
7701sdt_query::iterate_over_probe_entries()
edce5b67 7702{
c57ea854 7703 // probes are in the .probe section
edce5b67
JS
7704 while (probe_scn_offset < pdata->d_size)
7705 {
aff5d390
SC
7706 stap_sdt_probe_entry_v1 *pbe_v1 = (stap_sdt_probe_entry_v1 *) ((char*)pdata->d_buf + probe_scn_offset);
7707 stap_sdt_probe_entry_v2 *pbe_v2 = (stap_sdt_probe_entry_v2 *) ((char*)pdata->d_buf + probe_scn_offset);
15284963 7708 probe_type = (stap_sdt_probe_type)(pbe_v1->type_a);
1cc41cd6 7709 if (! have_uprobe())
edce5b67
JS
7710 {
7711 // Unless this is a mangled .probes section, this happens
7712 // because the name of the probe comes first, followed by
7713 // the sentinel.
7714 if (sess.verbose > 5)
b530b5b3 7715 clog << _F("got unknown probe_type : 0x%x", probe_type) << endl;
edce5b67
JS
7716 probe_scn_offset += sizeof(__uint32_t);
7717 continue;
7718 }
aff5d390
SC
7719 if ((long)pbe_v1 % sizeof(__uint64_t)) // we have stap_sdt_probe_entry_v1.type_b
7720 {
7721 pbe_v1 = (stap_sdt_probe_entry_v1*)((char*)pbe_v1 - sizeof(__uint32_t));
1cc41cd6 7722 if (pbe_v1->type_b != uprobe1_type)
aff5d390
SC
7723 continue;
7724 }
7725
1cc41cd6 7726 if (probe_type == uprobe1_type)
aff5d390 7727 {
79a0ca08 7728 if (pbe_v1->name == 0) // No name possibly means we have a .so with a relocation
c57ea854 7729 return;
79a0ca08 7730 semaphore = 0;
aff5d390 7731 probe_name = (char*)((char*)pdata->d_buf + pbe_v1->name - (char*)probe_scn_addr);
a794dbeb 7732 provider_name = ""; // unknown
1cc41cd6
DS
7733 pc = pbe_v1->arg;
7734 arg_count = 0;
aff5d390
SC
7735 probe_scn_offset += sizeof (stap_sdt_probe_entry_v1);
7736 }
08b5a50c 7737 else if (probe_type == uprobe2_type)
aff5d390 7738 {
79a0ca08 7739 if (pbe_v2->name == 0) // No name possibly means we have a .so with a relocation
c57ea854 7740 return;
79a0ca08 7741 semaphore = pbe_v2->semaphore;
aff5d390 7742 probe_name = (char*)((char*)pdata->d_buf + pbe_v2->name - (char*)probe_scn_addr);
a794dbeb 7743 provider_name = (char*)((char*)pdata->d_buf + pbe_v2->provider - (char*)probe_scn_addr);
aff5d390
SC
7744 arg_count = pbe_v2->arg_count;
7745 pc = pbe_v2->pc;
7746 if (pbe_v2->arg_string)
7747 arg_string = (char*)((char*)pdata->d_buf + pbe_v2->arg_string - (char*)probe_scn_addr);
79a0ca08
SC
7748 // skip over pbe_v2, probe_name text and provider text
7749 probe_scn_offset = ((long)(pbe_v2->name) - (long)(probe_scn_addr)) + probe_name.length();
7750 probe_scn_offset += sizeof (__uint32_t) - probe_scn_offset % sizeof (__uint32_t);
aff5d390 7751 }
edce5b67 7752 if (sess.verbose > 4)
b530b5b3 7753 clog << _("saw .probes ") << probe_name << (provider_name != "" ? _(" (provider ")+provider_name+") " : "")
aff5d390 7754 << "@0x" << hex << pc << dec << endl;
edce5b67 7755
e90006d7
JL
7756 dw.mod_info->marks.insert(make_pair(provider_name, probe_name));
7757
a794dbeb
FCE
7758 if (dw.function_name_matches_pattern (probe_name, pp_mark)
7759 && ((pp_provider == "") || dw.function_name_matches_pattern (provider_name, pp_provider)))
c57ea854 7760 handle_probe_entry ();
edce5b67 7761 }
edce5b67
JS
7762}
7763
7764
6846cfc8 7765void
4ddb6dd0 7766sdt_query::record_semaphore (vector<derived_probe *> & results, unsigned start)
6846cfc8 7767{
a794dbeb
FCE
7768 for (unsigned i=0; i<2; i++) {
7769 // prefer with-provider symbol; look without provider prefix for backward compatibility only
7770 string semaphore = (i==0 ? (provider_name+"_") : "") + probe_name + "_semaphore";
7771 // XXX: multiple addresses?
7772 if (sess.verbose > 2)
b530b5b3 7773 clog << _F("looking for semaphore symbol %s ", semaphore.c_str());
a794dbeb 7774
79a0ca08
SC
7775 Dwarf_Addr addr;
7776 if (this->semaphore)
7777 addr = this->semaphore;
7778 else
7779 addr = lookup_symbol_address(dw.module, semaphore.c_str());
a794dbeb
FCE
7780 if (addr)
7781 {
7d395255 7782 if (dwfl_module_relocations (dw.module) > 0)
a794dbeb
FCE
7783 dwfl_module_relocate_address (dw.module, &addr);
7784 // XXX: relocation basis?
909ab234
JS
7785
7786 // Dyninst needs the *file*-based offset for semaphores,
7787 // so subtract the difference in load addresses between .text and .probes
7788 if (dw.sess.runtime_usermode_p())
7789 addr -= semaphore_load_offset;
7790
a794dbeb
FCE
7791 for (unsigned i = start; i < results.size(); ++i)
7792 results[i]->sdt_semaphore_addr = addr;
7793 if (sess.verbose > 2)
b530b5b3 7794 clog << _(", found at 0x") << hex << addr << dec << endl;
a794dbeb
FCE
7795 return;
7796 }
7797 else
7798 if (sess.verbose > 2)
b530b5b3 7799 clog << _(", not found") << endl;
a794dbeb 7800 }
6846cfc8
SC
7801}
7802
7803
edce5b67
JS
7804void
7805sdt_query::convert_probe (probe *base)
7806{
7807 block *b = new block;
7808 b->tok = base->body->tok;
7809
edce5b67
JS
7810 // Generate: if (arg1 != mark("label")) next;
7811 functioncall *fc = new functioncall;
bbafcb1e 7812 fc->function = "ulong_arg";
edce5b67 7813 fc->tok = b->tok;
bbafcb1e 7814 literal_number* num = new literal_number(1);
edce5b67
JS
7815 num->tok = b->tok;
7816 fc->args.push_back(num);
7817
7818 functioncall *fcus = new functioncall;
7819 fcus->function = "user_string";
7820 fcus->type = pe_string;
7821 fcus->tok = b->tok;
7822 fcus->args.push_back(fc);
7823
7824 if_statement *is = new if_statement;
7825 is->thenblock = new next_statement;
7826 is->elseblock = NULL;
7827 is->tok = b->tok;
63ea4244 7828 is->thenblock->tok = b->tok;
edce5b67
JS
7829 comparison *be = new comparison;
7830 be->op = "!=";
7831 be->tok = b->tok;
7832 be->left = fcus;
7833 be->right = new literal_string(probe_name);
63ea4244 7834 be->right->tok = b->tok;
edce5b67
JS
7835 is->condition = be;
7836 b->statements.push_back(is);
7837
7838 // Now replace the body
7839 b->statements.push_back(base->body);
7840 base->body = b;
7841}
7842
7843
c72aa911
JS
7844probe*
7845sdt_query::convert_location ()
edce5b67 7846{
45a63356 7847 interned_string module = dw.module_name;
684b8718
JL
7848 if (has_process)
7849 module = path_remove_sysroot(sess, module);
7850
c72aa911 7851 probe_point* specific_loc = new probe_point(*base_loc);
684b8718
JL
7852 specific_loc->well_formed = true;
7853
662539d9 7854 vector<probe_point::component*> derived_comps;
edce5b67 7855
b4662f6b 7856 for (auto it = specific_loc->components.begin();
662539d9
JS
7857 it != specific_loc->components.end(); ++it)
7858 if ((*it)->functor == TOK_PROCESS)
7859 {
684b8718
JL
7860 // replace the possibly incomplete path to process
7861 *it = new probe_point::component(TOK_PROCESS,
7862 new literal_string(has_library ? path : module));
7863
1cc41cd6
DS
7864 // copy the process name
7865 derived_comps.push_back(*it);
662539d9
JS
7866 }
7867 else if ((*it)->functor == TOK_LIBRARY)
7868 {
1cc41cd6
DS
7869 // copy the library name for process probes
7870 derived_comps.push_back(*it);
662539d9
JS
7871 }
7872 else if ((*it)->functor == TOK_PROVIDER)
7873 {
7874 // replace the possibly wildcarded arg with the specific provider name
7875 *it = new probe_point::component(TOK_PROVIDER,
7876 new literal_string(provider_name));
7877 }
7878 else if ((*it)->functor == TOK_MARK)
c72aa911
JS
7879 {
7880 // replace the possibly wildcarded arg with the specific marker name
662539d9
JS
7881 *it = new probe_point::component(TOK_MARK,
7882 new literal_string(probe_name));
a794dbeb 7883
aff5d390
SC
7884 if (sess.verbose > 3)
7885 switch (probe_type)
7886 {
7887 case uprobe1_type:
b530b5b3 7888 clog << _("probe_type == uprobe1, use statement addr: 0x")
aff5d390
SC
7889 << hex << pc << dec << endl;
7890 break;
7891 case uprobe2_type:
b530b5b3 7892 clog << _("probe_type == uprobe2, use statement addr: 0x")
aff5d390
SC
7893 << hex << pc << dec << endl;
7894 break;
40fe32e0 7895 case uprobe3_type:
b530b5b3 7896 clog << _("probe_type == uprobe3, use statement addr: 0x")
40fe32e0
SC
7897 << hex << pc << dec << endl;
7898 break;
aff5d390 7899 default:
b530b5b3 7900 clog << _F("probe_type == use_uprobe_no_dwarf, use label name: _stapprobe1_%s",
7371cd19 7901 pp_mark.to_string().c_str()) << endl;
aff5d390
SC
7902 }
7903
c72aa911
JS
7904 switch (probe_type)
7905 {
aff5d390
SC
7906 case uprobe1_type:
7907 case uprobe2_type:
40fe32e0 7908 case uprobe3_type:
c72aa911 7909 // process("executable").statement(probe_arg)
662539d9
JS
7910 derived_comps.push_back
7911 (new probe_point::component(TOK_STATEMENT,
7912 new literal_number(pc, true)));
c72aa911
JS
7913 break;
7914
a794dbeb 7915 default: // deprecated
c72aa911 7916 // process("executable").function("*").label("_stapprobe1_MARK_NAME")
662539d9
JS
7917 derived_comps.push_back
7918 (new probe_point::component(TOK_FUNCTION,
a3e980f9 7919 new literal_string(string("*"))));
662539d9 7920 derived_comps.push_back
c72aa911 7921 (new probe_point::component(TOK_LABEL,
45a63356 7922 new literal_string(string("_stapprobe1_") + (string)pp_mark)));
c72aa911
JS
7923 break;
7924 }
7925 }
edce5b67 7926
662539d9
JS
7927 probe_point* derived_loc = new probe_point(*specific_loc);
7928 derived_loc->components = derived_comps;
8159bf55 7929 return new probe (new probe (base_probe, specific_loc), derived_loc);
edce5b67
JS
7930}
7931
7932
51d6bda3
SC
7933void
7934sdt_query::query_library (const char *library)
7935{
614f0fcf
JL
7936 visited_libraries.insert(library);
7937 if (query_one_library (library, dw, user_lib, base_probe, base_loc, results))
7938 resolved_library = true;
51d6bda3
SC
7939}
7940
e90006d7
JL
7941string
7942suggest_marks(systemtap_session& sess,
7943 const set<string>& modules,
7944 const string& mark,
7945 const string& provider)
7946{
323a662a 7947 if (mark.empty() || modules.empty() || sess.module_cache == NULL || sess.suppress_costly_diagnostics)
e90006d7
JL
7948 return "";
7949
cff6336b
DS
7950 // PR18577: There isn't any point in generating a suggestion list if
7951 // we're not going to display it.
7952 if ((sess.dump_mode == systemtap_session::dump_matched_probes
7953 || sess.dump_mode == systemtap_session::dump_matched_probes_vars)
7954 && sess.verbose < 2)
7955 return "";
7956
e90006d7 7957 set<string> marks;
b4662f6b 7958 const auto &cache = sess.module_cache->cache;
e90006d7
JL
7959 bool dash_suggestions = (mark.find("-") != string::npos);
7960
b4662f6b 7961 for (auto itmod = modules.begin();
e90006d7
JL
7962 itmod != modules.end(); ++itmod)
7963 {
b4662f6b
JS
7964 auto itcache = cache.find(*itmod);
7965 if (itcache != cache.end())
e90006d7 7966 {
b4662f6b
JS
7967 for (auto itmarks = itcache->second->marks.cbegin();
7968 itmarks != itcache->second->marks.cend(); ++itmarks)
e90006d7
JL
7969 {
7970 if (provider.empty()
7971 // simulating dw.function_name_matches_pattern()
7972 || (fnmatch(provider.c_str(), itmarks->first.c_str(), 0) == 0))
7973 {
7974 string marksug = itmarks->second;
7975 if (dash_suggestions)
7976 {
7977 size_t pos = 0;
7978 while (1) // there may be more than one
7979 {
7980 size_t i = marksug.find("__", pos);
7981 if (i == string::npos) break;
7982 marksug.replace (i, 2, "-");
7983 pos = i+1; // resume searching after the inserted -
7984 }
7985 }
7986 marks.insert(marksug);
7987 }
7988 }
7989 }
7990 }
7991
7992 if (sess.verbose > 2)
7993 {
7994 clog << "suggesting " << marks.size() << " marks "
7995 << "from modules:" << endl;
b4662f6b
JS
7996 for (auto itmod = modules.begin();
7997 itmod != modules.end(); ++itmod)
e90006d7
JL
7998 clog << *itmod << endl;
7999 }
8000
8001 if (marks.empty())
8002 return "";
8003
8004 return levenshtein_suggest(mark, marks, 5); // print top 5 marks only
8005}
8006
40a70f52
JL
8007string
8008suggest_plt_functions(systemtap_session& sess,
8009 const set<string>& modules,
8010 const string& func)
8011{
323a662a 8012 if (func.empty() || modules.empty() || sess.module_cache == NULL || sess.suppress_costly_diagnostics)
40a70f52
JL
8013 return "";
8014
cff6336b
DS
8015 // PR18577: There isn't any point in generating a suggestion list if
8016 // we're not going to display it.
8017 if ((sess.dump_mode == systemtap_session::dump_matched_probes
8018 || sess.dump_mode == systemtap_session::dump_matched_probes_vars)
8019 && sess.verbose < 2)
8020 return "";
8021
47d349b1 8022 set<interned_string> funcs;
b4662f6b 8023 const auto &cache = sess.module_cache->cache;
40a70f52 8024
b4662f6b 8025 for (auto itmod = modules.begin();
40a70f52
JL
8026 itmod != modules.end(); ++itmod)
8027 {
b4662f6b
JS
8028 auto itcache = cache.find(*itmod);
8029 if (itcache != cache.end())
40a70f52
JL
8030 funcs.insert(itcache->second->plt_funcs.begin(),
8031 itcache->second->plt_funcs.end());
8032 }
8033
8034 if (sess.verbose > 2)
d82dd71f
JL
8035 {
8036 clog << "suggesting " << funcs.size() << " plt functions "
8037 << "from modules:" << endl;
b4662f6b
JS
8038 for (auto itmod = modules.begin();
8039 itmod != modules.end(); ++itmod)
d82dd71f
JL
8040 clog << *itmod << endl;
8041 }
40a70f52
JL
8042
8043 if (funcs.empty())
8044 return "";
8045
8046 return levenshtein_suggest(func, funcs, 5); // print top 5 funcs only
8047}
51d6bda3 8048
0508c45f
JL
8049string
8050suggest_dwarf_functions(systemtap_session& sess,
8051 const set<string>& modules,
8052 string func)
44ffe90c
JL
8053{
8054 // Trim any @ component
8055 size_t pos = func.find('@');
8056 if (pos != string::npos)
8057 func.erase(pos);
8058
323a662a 8059 if (func.empty() || modules.empty() || sess.module_cache == NULL || sess.suppress_costly_diagnostics)
44ffe90c
JL
8060 return "";
8061
cff6336b
DS
8062 // PR18577: There isn't any point in generating a suggestion list if
8063 // we're not going to display it.
8064 if ((sess.dump_mode == systemtap_session::dump_matched_probes
8065 || sess.dump_mode == systemtap_session::dump_matched_probes_vars)
8066 && sess.verbose < 2)
8067 return "";
8068
44ffe90c 8069 // We must first aggregate all the functions from the cache
47d349b1 8070 set<interned_string> funcs;
b4662f6b 8071 const auto &cache = sess.module_cache->cache;
44ffe90c 8072
b4662f6b
JS
8073 for (auto itmod = modules.begin();
8074 itmod != modules.end(); ++itmod)
44ffe90c 8075 {
6e6d9a3b
JL
8076 module_info *module;
8077
8078 // retrieve module_info from cache
b4662f6b
JS
8079 auto itcache = cache.find(*itmod);
8080 if (itcache != cache.end())
6e6d9a3b
JL
8081 module = itcache->second;
8082 else // module not found
8083 continue;
8084
8085 // add inlines
8086 funcs.insert(module->inlined_funcs.begin(),
8087 module->inlined_funcs.end());
8088
8089 // add all function symbols in cache
8090 if (module->symtab_status != info_present || module->sym_table == NULL)
8091 continue;
b4662f6b
JS
8092 const auto& modfuncs = module->sym_table->map_by_name;
8093 for (auto itfuncs = modfuncs.begin();
6e6d9a3b
JL
8094 itfuncs != modfuncs.end(); ++itfuncs)
8095 funcs.insert(itfuncs->first);
44ffe90c
JL
8096 }
8097
8098 if (sess.verbose > 2)
d82dd71f
JL
8099 {
8100 clog << "suggesting " << funcs.size() << " dwarf functions "
8101 << "from modules:" << endl;
b4662f6b
JS
8102 for (auto itmod = modules.begin();
8103 itmod != modules.end(); ++itmod)
d82dd71f
JL
8104 clog << *itmod << endl;
8105 }
44ffe90c
JL
8106
8107 if (funcs.empty())
8108 return "";
8109
593f09eb 8110 return levenshtein_suggest(func, funcs, 5); // print top 5 funcs only
44ffe90c
JL
8111}
8112
6ca3ea7b
JS
8113
8114// Use a glob pattern to find executables or shared libraries
8115static set<string>
8116glob_executable(const string& pattern)
8117{
8118 glob_t the_blob;
8119 set<string> globs;
8120
8121 int rc = glob (pattern.c_str(), 0, NULL, & the_blob);
8122 if (rc)
8123 throw SEMANTIC_ERROR (_F("glob %s error (%d)", pattern.c_str(), rc));
8124
8125 for (unsigned i = 0; i < the_blob.gl_pathc; ++i)
8126 {
8127 const char* globbed = the_blob.gl_pathv[i];
8128 struct stat st;
8129
c539cb3d 8130 if (stat (globbed, &st) == 0
6ca3ea7b
JS
8131 && S_ISREG (st.st_mode)) // see find_executable()
8132 {
8133 // Need to call resolve_path here, in order to path-expand
8134 // patterns like process("stap*"). Otherwise it may go through
8135 // to the next round of expansion as ("stap"), leading to a $PATH
8136 // search that's not consistent with the glob search already done.
8137 string canononicalized = resolve_path (globbed);
8138
8139 // The canonical names can result in duplication, for example
8140 // having followed symlinks that are common with shared libraries,
8141 // so we use a set for unique results.
8142 globs.insert(canononicalized);
8143 }
8144 }
8145
8146 globfree (& the_blob);
8147 return globs;
8148}
8149
8150static bool
8151resolve_library_by_path(base_query & q,
8152 set<string> const & visited_libraries,
8153 probe * base,
8154 probe_point * location,
8155 literal_map_t const & parameters,
8156 vector<derived_probe *> & finished_results)
8157{
8158 size_t results_pre = finished_results.size();
8159 systemtap_session & sess = q.sess;
8160 dwflpp & dw = q.dw;
8161
45a63356 8162 interned_string lib;
d2eaa03b
JL
8163 if (!location->from_globby_comp(TOK_LIBRARY) && q.has_library
8164 && !visited_libraries.empty()
6ca3ea7b
JS
8165 && q.get_string_param(parameters, TOK_LIBRARY, lib))
8166 {
8167 // The library didn't fit any DT_NEEDED libraries. As a last effort,
8168 // let's try to look for the library directly.
8169
8170 if (contains_glob_chars (lib))
8171 {
8172 // Evaluate glob here, and call derive_probes recursively with each match.
b4662f6b
JS
8173 const auto& globs = glob_executable (lib);
8174 for (auto it = globs.begin(); it != globs.end(); ++it)
6ca3ea7b
JS
8175 {
8176 assert_no_interrupts();
8177
8178 const string& globbed = *it;
8179 if (sess.verbose > 1)
8180 clog << _F("Expanded library(\"%s\") to library(\"%s\")",
7371cd19 8181 lib.to_string().c_str(), globbed.c_str()) << endl;
6ca3ea7b
JS
8182
8183 probe *new_base = build_library_probe(dw, globbed,
8184 base, location);
8185
8186 // We override "optional = true" here, as if the
8187 // wildcarded probe point was given a "?" suffix.
8188
8189 // This is because wildcard probes will be expected
8190 // by users to apply only to some subset of the
8191 // matching binaries, in the sense of "any", rather
8192 // than "all", sort of similarly how
8193 // module("*").function("...") patterns work.
8194
8195 derive_probes (sess, new_base, finished_results,
8196 true /* NB: not location->optional */ );
8197 }
8198 }
8199 else
8200 {
8201 string resolved_lib = find_executable(lib, sess.sysroot, sess.sysenv,
8202 "LD_LIBRARY_PATH");
8203 if (resolved_lib.find('/') != string::npos)
8204 {
8205 probe *new_base = build_library_probe(dw, resolved_lib,
8206 base, location);
8207 derive_probes(sess, new_base, finished_results);
8208 if (lib.find('/') == string::npos)
8209 sess.print_warning(_F("'%s' is not a needed library of '%s'. "
8210 "Specify the full path to squelch this warning.",
8211 resolved_lib.c_str(), dw.module_name.c_str()));
8212 }
8213 else
8214 {
8215 // Otherwise, let's suggest from the DT_NEEDED libraries
8216 string sugs = levenshtein_suggest(lib, visited_libraries, 5);
8217 if (!sugs.empty())
8218 throw SEMANTIC_ERROR (_NF("no match (similar library: %s)",
8219 "no match (similar libraries: %s)",
8220 sugs.find(',') == string::npos,
8221 sugs.c_str()));
8222 }
8223 }
8224 }
8225
8226 return results_pre != finished_results.size();
8227}
8228
51f0b234
DS
8229static void
8230handle_module_token(systemtap_session &sess, interned_string &module_token_val)
8231{
8232 // Do we have a fully resolved path to the module?
8233 if (!is_fully_resolved(module_token_val, sess.sysroot, sess.sysenv))
8234 {
8235 // If the path isn't fully resolved, it might be a in-tree
8236 // module name or a relative path. If it is a relative path,
8237 // convert it to a full path.
8238 if (module_token_val.find('/') != string::npos)
8239 {
8240 string module_token_val2 = find_executable(module_token_val,
8241 sess.sysroot,
8242 sess.sysenv);
8243 module_token_val = module_token_val2;
0dbac951 8244 }
51f0b234
DS
8245 // If we're here, then it's an in-tree module. Replace any
8246 // dashes with underscores.
8247 else
8248 {
8249 size_t dash_pos = 0;
8250 // copy out for replace operations
8251 string module_token_val2 = module_token_val;
8252 while ((dash_pos = module_token_val2.find('-')) != string::npos)
8253 module_token_val2.replace(int(dash_pos), 1, "_");
8254 module_token_val = module_token_val2;
8255 }
8256 }
8257}
8258
20c6c071 8259void
5227f1ea 8260dwarf_builder::build(systemtap_session & sess,
7a053d3b 8261 probe * base,
20c6c071 8262 probe_point * location,
86bf665e 8263 literal_map_t const & parameters,
20c6c071
GH
8264 vector<derived_probe *> & finished_results)
8265{
b20febf3
FCE
8266 // NB: the kernel/user dwlfpp objects are long-lived.
8267 // XXX: but they should be per-session, as this builder object
8268 // may be reused if we try to cross-instrument multiple targets.
84048984 8269
7a24d422 8270 dwflpp* dw = 0;
6d5d594e 8271 literal_map_t filled_parameters = parameters;
7a24d422 8272
45a63356 8273 interned_string module_name;
5c6f9e92 8274 int64_t proc_pid;
ae2552da
FCE
8275 if (has_null_param (parameters, TOK_KERNEL))
8276 {
8277 dw = get_kern_dw(sess, "kernel");
8278 }
8279 else if (get_param (parameters, TOK_MODULE, module_name))
b8da0ad1 8280 {
51f0b234
DS
8281 handle_module_token(sess, module_name);
8282 filled_parameters[TOK_MODULE] = new literal_string(module_name);
8e30940d 8283
37001baa
FCE
8284 // NB: glob patterns get expanded later, during the offline
8285 // elfutils module listing.
ae2552da 8286 dw = get_kern_dw(sess, module_name);
b8da0ad1 8287 }
5c6f9e92 8288 else if (has_param(filled_parameters, TOK_PROCESS))
84c84ac4 8289 {
0ce4c32f
FCE
8290 // NB: module_name is not yet set!
8291
6d5d594e
LB
8292 if(has_null_param(filled_parameters, TOK_PROCESS))
8293 {
c68fbc62 8294 if (!location->auto_path.empty())
fc18e6c4 8295 {
05f925e9
FL
8296 if (location->components[0]->functor == TOK_PROCESS &&
8297 location->components[0]->arg == 0)
8298 {
8299 // PATH expansion of process component without argument.
8300 // The filename without the .stp extension is used.
c68fbc62 8301 string full_path = location->auto_path;
05f925e9
FL
8302 string::size_type start = full_path.find("PATH/") + 4;
8303 string::size_type end = full_path.rfind(".stp");
8304 module_name = full_path.substr(start, end - start);
8305 location->components[0]->arg = new literal_string(module_name);
8306 filled_parameters[TOK_PROCESS] = new literal_string(module_name);
8307 }
fc18e6c4 8308 }
05f925e9 8309 else
30b865ce 8310 {
05f925e9
FL
8311 string file;
8312 try
8313 {
8314 file = sess.cmd_file();
8315 }
8316 catch (const semantic_error& e)
8317 {
8318 if(sess.target_pid)
8319 throw SEMANTIC_ERROR(_("invalid -x pid for unspecified process"
8320 " probe [man stapprobes]"), NULL, NULL, &e);
8321 else
8322 throw SEMANTIC_ERROR(_("invalid -c command for unspecified process"
8323 " probe [man stapprobes]"), NULL, NULL, &e);
8324 }
8325 if(file.empty())
8326 throw SEMANTIC_ERROR(_("unspecified process probe is invalid without"
8327 " a -c COMMAND or -x PID [man stapprobes]"));
8328 module_name = sess.sysroot + file;
8329 filled_parameters[TOK_PROCESS] = new literal_string(module_name);// this needs to be used in place of the blank map
8330 // in the case of TOK_MARK we need to modify locations as well // XXX why?
8331 if(location->components[0]->functor==TOK_PROCESS &&
8332 location->components[0]->arg == 0)
8333 {
8334 if (sess.target_pid)
8335 location->components[0]->arg = new literal_number(sess.target_pid);
8336 else
8337 location->components[0]->arg = new literal_string(module_name);
8338 }
30b865ce 8339 }
5c6f9e92
AJ
8340 }
8341
0ce4c32f
FCE
8342 // NB: must specifically handle the classical ("string") form here, to make sure
8343 // we get the module_name out.
8344 else if (get_param (parameters, TOK_PROCESS, module_name))
8345 {
c68fbc62 8346 if (!location->auto_path.empty())
05f925e9
FL
8347 {
8348 if (!module_name.empty() && module_name[0] != '/')
8349 {
8350 // prefix argument with file location from PATH directory
c68fbc62 8351 string full_path = location->auto_path;
05f925e9
FL
8352 string::size_type start = full_path.find("PATH/") + 4;
8353 string::size_type end = full_path.rfind("/");
8354 string arg = module_name;
8355 module_name = full_path.substr(start, end-start+1) + arg;
8356 location->components[0]->arg = new literal_string(module_name);
8357 filled_parameters[TOK_PROCESS] = new literal_string(module_name);
8358 }
8359 }
8360 else
8361 {
05f925e9
FL
8362 filled_parameters[TOK_PROCESS] = new literal_string(module_name);
8363 }
0ce4c32f
FCE
8364 }
8365
5c6f9e92
AJ
8366 else if (get_param (parameters, TOK_PROCESS, proc_pid))
8367 {
6c484277 8368 // check that the pid given corresponds to a running process
30b865ce 8369 string pid_err_msg;
fb9ae721 8370 if (!is_valid_pid(proc_pid, pid_err_msg))
30b865ce 8371 throw SEMANTIC_ERROR(pid_err_msg);
6c484277 8372
5c6f9e92
AJ
8373 string pid_path = string("/proc/") + lex_cast(proc_pid) + "/exe";
8374 module_name = sess.sysroot + pid_path;
37635b45 8375
0ce4c32f 8376 // in the case of TOK_MARK we need to modify locations as well // XXX why?
6d5d594e 8377 if(location->components[0]->functor==TOK_PROCESS &&
0ce4c32f 8378 location->components[0]->arg == 0)
30b865ce 8379 location->components[0]->arg = new literal_number(sess.target_pid);
b78a0fbb 8380 }
5750ecc6 8381
37001baa
FCE
8382 // PR6456 process("/bin/*") glob handling
8383 if (contains_glob_chars (module_name))
8384 {
8385 // Expand glob via rewriting the probe-point process("....")
8386 // parameter, asserted to be the first one.
8387
8388 assert (location->components.size() > 0);
8389 assert (location->components[0]->functor == TOK_PROCESS);
8390 assert (location->components[0]->arg);
8391 literal_string* lit = dynamic_cast<literal_string*>(location->components[0]->arg);
8392 assert (lit);
8393
8394 // Evaluate glob here, and call derive_probes recursively with each match.
4ffecddf
VK
8395 const auto& globs = glob_executable (sess.sysroot
8396 + string(module_name));
44ffe90c 8397 unsigned results_pre = finished_results.size();
b4662f6b 8398 for (auto it = globs.begin(); it != globs.end(); ++it)
37001baa 8399 {
e19ebcf7 8400 assert_no_interrupts();
37001baa 8401
6ca3ea7b 8402 const string& globbed = *it;
88f8e2c0 8403
6ca3ea7b
JS
8404 // synthesize a new probe_point, with the glob-expanded string
8405 probe_point *pp = new probe_point (*location);
ef0943df 8406
6ca3ea7b
JS
8407 // PR13338: quote results to prevent recursion
8408 string eglobbed = escape_glob_chars (globbed);
5750ecc6 8409
6ca3ea7b
JS
8410 if (sess.verbose > 1)
8411 clog << _F("Expanded process(\"%s\") to process(\"%s\")",
7371cd19 8412 module_name.to_string().c_str(), eglobbed.c_str()) << endl;
6ca3ea7b 8413 string eglobbed_tgt = path_remove_sysroot(sess, eglobbed);
5750ecc6 8414
d2eaa03b
JL
8415 probe_point::component* ppc
8416 = new probe_point::component (TOK_PROCESS,
8417 new literal_string (eglobbed_tgt),
8418 true /* from_glob */ );
6ca3ea7b
JS
8419 ppc->tok = location->components[0]->tok; // overwrite [0] slot, pattern matched above
8420 pp->components[0] = ppc;
37001baa 8421
6ca3ea7b 8422 probe* new_probe = new probe (base, pp);
7977a734 8423
6ca3ea7b
JS
8424 // We override "optional = true" here, as if the
8425 // wildcarded probe point was given a "?" suffix.
7977a734 8426
6ca3ea7b
JS
8427 // This is because wildcard probes will be expected
8428 // by users to apply only to some subset of the
8429 // matching binaries, in the sense of "any", rather
8430 // than "all", sort of similarly how
8431 // module("*").function("...") patterns work.
7977a734 8432
6ca3ea7b
JS
8433 derive_probes (sess, new_probe, finished_results,
8434 true /* NB: not location->optional */ );
37001baa
FCE
8435 }
8436
44ffe90c
JL
8437 unsigned results_post = finished_results.size();
8438
e90006d7 8439 // Did we fail to find a function/plt/mark by name? Let's suggest
44ffe90c 8440 // something!
45a63356 8441 interned_string func;
44ffe90c
JL
8442 if (results_pre == results_post
8443 && get_param(filled_parameters, TOK_FUNCTION, func)
8444 && !func.empty())
8445 {
0508c45f 8446 string sugs = suggest_dwarf_functions(sess, modules_seen, func);
44ffe90c
JL
8447 modules_seen.clear();
8448 if (!sugs.empty())
dc09353a 8449 throw SEMANTIC_ERROR (_NF("no match (similar function: %s)",
44ffe90c
JL
8450 "no match (similar functions: %s)",
8451 sugs.find(',') == string::npos,
8452 sugs.c_str()));
8453 }
40a70f52
JL
8454 else if (results_pre == results_post
8455 && get_param(filled_parameters, TOK_PLT, func)
8456 && !func.empty())
8457 {
8458 string sugs = suggest_plt_functions(sess, modules_seen, func);
8459 modules_seen.clear();
8460 if (!sugs.empty())
8461 throw SEMANTIC_ERROR (_NF("no match (similar function: %s)",
8462 "no match (similar functions: %s)",
8463 sugs.find(',') == string::npos,
8464 sugs.c_str()));
8465 }
e90006d7
JL
8466 else if (results_pre == results_post
8467 && get_param(filled_parameters, TOK_MARK, func)
8468 && !func.empty())
8469 {
45a63356 8470 interned_string provider;
e90006d7
JL
8471 get_param(filled_parameters, TOK_PROVIDER, provider);
8472
8473 string sugs = suggest_marks(sess, modules_seen, func, provider);
8474 modules_seen.clear();
8475 if (!sugs.empty())
8476 throw SEMANTIC_ERROR (_NF("no match (similar mark: %s)",
8477 "no match (similar marks: %s)",
8478 sugs.find(',') == string::npos,
8479 sugs.c_str()));
8480 }
44ffe90c 8481
37001baa
FCE
8482 return; // avoid falling through
8483 }
8484
5750ecc6
FCE
8485 // PR13338: unquote glob results
8486 module_name = unescape_glob_chars (module_name);
a7146587
DS
8487 user_path = find_executable (module_name, sess.sysroot, sess.sysenv); // canonicalize it
8488 // Note we don't need to pass the sysroot to
8489 // is_fully_resolved(), since we just passed it to
8490 // find_executable().
8491 if (!is_fully_resolved(user_path, "", sess.sysenv))
5fa0d811 8492 throw SEMANTIC_ERROR(_F("cannot find executable '%s'",
7371cd19 8493 user_path.to_string().c_str()));
d1bcbe71
RH
8494
8495 // if the executable starts with "#!", we look for the interpreter of the script
8496 {
7371cd19 8497 ifstream script_file (user_path.to_string().c_str());
d1bcbe71
RH
8498
8499 if (script_file.good ())
8500 {
8501 string line;
8502
8503 getline (script_file, line);
8504
8505 if (line.compare (0, 2, "#!") == 0)
8506 {
1f4b9e55 8507 string path = line.substr(2);
d1bcbe71 8508
1f4b9e55
DS
8509 // trim white space
8510 trim(path);
d1bcbe71 8511
1f4b9e55 8512 if (! path.empty())
d1bcbe71 8513 {
8e13c1a1
RH
8514 // handle "#!/usr/bin/env" redirect
8515 size_t offset = 0;
8516 if (path.compare(0, sizeof("/bin/env")-1, "/bin/env") == 0)
8517 {
8518 offset = sizeof("/bin/env")-1;
8519 }
8520 else if (path.compare(0, sizeof("/usr/bin/env")-1, "/usr/bin/env") == 0)
8521 {
8522 offset = sizeof("/usr/bin/env")-1;
8523 }
8524
8525 if (offset != 0)
8526 {
8527 size_t p3 = path.find_first_not_of(" \t", offset);
8528
8529 if (p3 != string::npos)
8530 {
8531 string env_path = path.substr(p3);
05fb3e0c
WF
8532 user_path = find_executable (env_path, sess.sysroot,
8533 sess.sysenv);
8e13c1a1
RH
8534 }
8535 }
8536 else
8537 {
05fb3e0c 8538 user_path = find_executable (path, sess.sysroot, sess.sysenv);
8e13c1a1 8539 }
d1bcbe71
RH
8540
8541 struct stat st;
8542
7371cd19
JS
8543 const string& new_path = user_path;
8544 if (access (new_path.c_str(), X_OK) == 0
8545 && stat (new_path.c_str(), &st) == 0
d1bcbe71
RH
8546 && S_ISREG (st.st_mode)) // see find_executable()
8547 {
8548 if (sess.verbose > 1)
b530b5b3 8549 clog << _F("Expanded process(\"%s\") to process(\"%s\")",
7371cd19 8550 module_name.to_string().c_str(), new_path.c_str()) << endl;
d1bcbe71
RH
8551
8552 assert (location->components.size() > 0);
8553 assert (location->components[0]->functor == TOK_PROCESS);
8554 assert (location->components[0]->arg);
8555 literal_string* lit = dynamic_cast<literal_string*>(location->components[0]->arg);
8556 assert (lit);
8557
8558 // synthesize a new probe_point, with the expanded string
8559 probe_point *pp = new probe_point (*location);
7371cd19 8560 string user_path_tgt = path_remove_sysroot(sess, new_path);
d1bcbe71 8561 probe_point::component* ppc = new probe_point::component (TOK_PROCESS,
a3e980f9 8562 new literal_string (user_path_tgt));
d1bcbe71
RH
8563 ppc->tok = location->components[0]->tok; // overwrite [0] slot, pattern matched above
8564 pp->components[0] = ppc;
8565
d885563b 8566 probe* new_probe = new probe (base, pp);
d1bcbe71
RH
8567
8568 derive_probes (sess, new_probe, finished_results);
8569
8570 script_file.close();
8571 return;
8572 }
8573 }
8574 }
8575 }
8576 script_file.close();
8577 }
8578
dbe9d133
JL
8579 // If this is a library probe, then target the library module instead. We
8580 // do this only if the library path is already fully resolved (such as
8581 // what query_one_library() would have done for us). Otherwise, we resort
8582 // to iterate_over_libraries.
c30ff776
TP
8583 if (get_param (parameters, TOK_LIBRARY, user_lib) && !user_lib.empty())
8584 {
8585 string library = find_executable (user_lib, sess.sysroot,
8586 sess.sysenv, "LD_LIBRARY_PATH");
8587 if (is_fully_resolved(library, "", sess.sysenv, "LD_LIBRARY_PATH"))
4e4b08eb
DS
8588 module_name = library;
8589 else
8590 module_name = user_path; // canonicalize it
c30ff776 8591 }
63b4fd14 8592 else
dbe9d133 8593 module_name = user_path; // canonicalize it
d0a7f5a9 8594
3667d615 8595 // uretprobes aren't available everywhere
536f1261 8596 if (has_null_param(parameters, TOK_RETURN) && !sess.runtime_usermode_p())
2b69faaf 8597 {
3667d615
JS
8598 if (kernel_supports_inode_uprobes(sess) &&
8599 !kernel_supports_inode_uretprobes(sess))
dc09353a 8600 throw SEMANTIC_ERROR
3ffeaf3c 8601 (_("process return probes not available [man error::inode-uprobes]"));
2b69faaf 8602 }
3667d615 8603
f4000852
MW
8604 // There is a similar check in pass 4 (buildrun), but it is
8605 // needed here too to make sure alternatives for optional
8606 // (? or !) process probes are disposed and/or alternatives
8607 // are selected.
3667d615
JS
8608 if (!sess.runtime_usermode_p())
8609 check_process_probe_kernel_support(sess);
e34d5d13 8610
7a24d422
FCE
8611 // user-space target; we use one dwflpp instance per module name
8612 // (= program or shared library)
707bf35e 8613 dw = get_user_dw(sess, module_name);
c8959a29 8614 }
20c6c071 8615
1acfc030
JS
8616 assert(dw);
8617
e90006d7
JL
8618 unsigned results_pre = finished_results.size();
8619
5896cd05 8620 if (sess.verbose > 3)
7371cd19
JS
8621 clog << _F("dwarf_builder::build for %s",
8622 module_name.to_string().c_str()) << endl;
5896cd05 8623
45a63356 8624 interned_string dummy_mark_name; // NB: PR10245: dummy value, need not substitute - => __
a794dbeb 8625 if (get_param(parameters, TOK_MARK, dummy_mark_name))
f28a8c28 8626 {
51d6bda3 8627 sdt_query sdtq(base, location, *dw, filled_parameters, finished_results, user_lib);
06de3a04 8628 dw->iterate_over_modules<base_query>(&query_module, &sdtq);
e90006d7
JL
8629
8630 // We need to update modules_seen with the modules we've visited
8631 modules_seen.insert(sdtq.visited_modules.begin(),
8632 sdtq.visited_modules.end());
8633
6ca3ea7b
JS
8634 if (results_pre == finished_results.size()
8635 && sdtq.has_library && !sdtq.resolved_library
8636 && resolve_library_by_path (sdtq, sdtq.visited_libraries,
8637 base, location, filled_parameters,
8638 finished_results))
8639 return;
614f0fcf 8640
e90006d7 8641 // Did we fail to find a mark?
d2eaa03b
JL
8642 if (results_pre == finished_results.size()
8643 && !location->from_globby_comp(TOK_MARK))
e90006d7 8644 {
45a63356 8645 interned_string provider;
c92d3b42 8646 (void) get_param(filled_parameters, TOK_PROVIDER, provider);
e90006d7
JL
8647
8648 string sugs = suggest_marks(sess, modules_seen, dummy_mark_name, provider);
8649 modules_seen.clear();
8650 if (!sugs.empty())
8651 throw SEMANTIC_ERROR (_NF("no match (similar mark: %s)",
8652 "no match (similar marks: %s)",
8653 sugs.find(',') == string::npos,
8654 sugs.c_str()));
8655 }
8656
edce5b67 8657 return;
7a05f484 8658 }
20c6c071 8659
6d5d594e 8660 dwarf_query q(base, location, *dw, filled_parameters, finished_results, user_path, user_lib);
7a24d422
FCE
8661
8662 // XXX: kernel.statement.absolute is a special case that requires no
8663 // dwfl processing. This code should be in a separate builder.
7a24d422 8664 if (q.has_kernel && q.has_absolute)
37ebca01 8665 {
4baf0e53 8666 // assert guru mode for absolute probes
37ebca01
FCE
8667 if (! q.base_probe->privileged)
8668 {
dc09353a 8669 throw SEMANTIC_ERROR (_("absolute statement probe in unprivileged script; need stap -g"),
edce5b67 8670 q.base_probe->tok);
37ebca01
FCE
8671 }
8672
8673 // For kernel.statement(NUM).absolute probe points, we bypass
8674 // all the debuginfo stuff: We just wire up a
8675 // dwarf_derived_probe right here and now.
4baf0e53 8676 dwarf_derived_probe* p =
b8da0ad1
FCE
8677 new dwarf_derived_probe ("", "", 0, "kernel", "",
8678 q.statement_num_val, q.statement_num_val,
8679 q, 0);
37ebca01 8680 finished_results.push_back (p);
1a0dbc5a 8681 sess.unwindsym_modules.insert ("kernel");
37ebca01
FCE
8682 return;
8683 }
8684
06de3a04 8685 dw->iterate_over_modules<base_query>(&query_module, &q);
8f14e444 8686
44ffe90c
JL
8687 // We need to update modules_seen with the modules we've visited
8688 modules_seen.insert(q.visited_modules.begin(),
8689 q.visited_modules.end());
8f14e444
FCE
8690
8691 // PR11553 special processing: .return probes requested, but
8692 // some inlined function instances matched.
8693 unsigned i_n_r = q.inlined_non_returnable.size();
8694 unsigned results_post = finished_results.size();
8695 if (i_n_r > 0)
8696 {
8697 if ((results_pre == results_post) && (! sess.suppress_warnings)) // no matches; issue warning
8698 {
8699 string quicklist;
b4662f6b 8700 for (auto it = q.inlined_non_returnable.begin();
8f14e444
FCE
8701 it != q.inlined_non_returnable.end();
8702 it++)
8703 {
45a63356 8704 quicklist += " " + (string)(*it);
8f14e444
FCE
8705 if (quicklist.size() > 80) // heuristic, don't make an overlong report line
8706 {
8707 quicklist += " ...";
8708 break;
8709 }
8710 }
c57ea854 8711
52c2652f 8712 sess.print_warning (_NF("cannot probe .return of %u inlined function %s",
b530b5b3 8713 "cannot probe .return of %u inlined functions %s",
52c2652f 8714 quicklist.size(), i_n_r, quicklist.c_str()));
8f14e444
FCE
8715 // There will be also a "no matches" semantic error generated.
8716 }
8717 if (sess.verbose > 1)
52c2652f
NMA
8718 clog << _NF("skipped .return probe of %u inlined function",
8719 "skipped .return probe of %u inlined functions", i_n_r, i_n_r) << endl;
8f14e444
FCE
8720 if ((sess.verbose > 3) || (sess.verbose > 2 && results_pre == results_post)) // issue details with high verbosity
8721 {
b4662f6b 8722 for (auto it = q.inlined_non_returnable.begin();
8f14e444
FCE
8723 it != q.inlined_non_returnable.end();
8724 it++)
8725 clog << (*it) << " ";
8726 clog << endl;
8727 }
8728 } // i_n_r > 0
44ffe90c 8729
6ca3ea7b
JS
8730 if (results_pre == finished_results.size()
8731 && q.has_library && !q.resolved_library
8732 && resolve_library_by_path (q, q.visited_libraries,
8733 base, location, filled_parameters,
8734 finished_results))
8735 return;
614f0fcf 8736
e90006d7 8737 // If we just failed to resolve a function/plt by name, we can suggest
ef0943df
JL
8738 // something. We only suggest things for probe points that were not
8739 // synthesized from a glob, i.e. only for 'real' probes. This is also
8740 // required because modules_seen needs to accumulate across recursive
8741 // calls for process(glob)[.library(glob)] probes.
45a63356 8742 interned_string func;
d2eaa03b 8743 if (results_pre == results_post && !location->from_globby_comp(TOK_FUNCTION)
44ffe90c
JL
8744 && get_param(filled_parameters, TOK_FUNCTION, func)
8745 && !func.empty())
8746 {
0508c45f 8747 string sugs = suggest_dwarf_functions(sess, modules_seen, func);
44ffe90c
JL
8748 modules_seen.clear();
8749 if (!sugs.empty())
dc09353a 8750 throw SEMANTIC_ERROR (_NF("no match (similar function: %s)",
44ffe90c
JL
8751 "no match (similar functions: %s)",
8752 sugs.find(',') == string::npos,
8753 sugs.c_str()));
8754 }
d2eaa03b 8755 else if (results_pre == results_post && !location->from_globby_comp(TOK_PLT)
40a70f52
JL
8756 && get_param(filled_parameters, TOK_PLT, func)
8757 && !func.empty())
8758 {
8759 string sugs = suggest_plt_functions(sess, modules_seen, func);
8760 modules_seen.clear();
8761 if (!sugs.empty())
8762 throw SEMANTIC_ERROR (_NF("no match (similar function: %s)",
8763 "no match (similar functions: %s)",
8764 sugs.find(',') == string::npos,
8765 sugs.c_str()));
8766 }
44ffe90c
JL
8767 else if (results_pre != results_post)
8768 // Something was derived so we won't need to suggest something
8769 modules_seen.clear();
5f0a03a6
JK
8770}
8771
8772symbol_table::~symbol_table()
8773{
c9efa5c9 8774 delete_map(map_by_addr);
5f0a03a6
JK
8775}
8776
8777void
45a63356 8778symbol_table::add_symbol(interned_string name, bool weak, bool descriptor,
61218615 8779 Dwarf_Addr addr, Dwarf_Addr entrypc)
5f0a03a6 8780{
064a90a9
MW
8781 /* Does the target architecture have function descriptors?
8782 Then we want to filter them out. When seeing a symbol with a name
8783 starting with '.' we assume it is a regular function pointer and
8784 not a pointer to a function descriptor. Note that this might create
8785 duplicates if we also found the function descriptor symbol itself.
8786 dwfl_module_getsym_info () will have resolved the actual function
8787 address for us. But we won't know if we see either or both. */
8788 if (opd_section != SHN_UNDEF)
8789 {
8790 // Map ".sys_foo" to "sys_foo".
8791 if (name[0] == '.')
45a63356 8792 name.remove_prefix(1);
064a90a9
MW
8793
8794 // Make sure we don't create duplicate func_info's
b4662f6b
JS
8795 auto er = map_by_addr.equal_range(addr);
8796 for (auto it = er.first; it != er.second; ++it)
2aa93775 8797 if (it->second->name == name)
064a90a9
MW
8798 return;
8799 }
8800
5f0a03a6 8801 func_info *fi = new func_info();
61218615 8802 fi->entrypc = entrypc;
5f0a03a6 8803 fi->addr = addr;
47d349b1 8804 fi->name = name;
ab91b232 8805 fi->weak = weak;
2867a2a1 8806 fi->descriptor = descriptor;
d16f315f
HK
8807
8808 map_by_name.insert(make_pair(fi->name, fi));
1c6b77e5 8809 map_by_addr.insert(make_pair(addr, fi));
5f0a03a6
JK
8810}
8811
46f7b6be 8812void
f98c6346 8813symbol_table::prepare_section_rejection(Dwfl_Module *mod __attribute__ ((unused)))
46f7b6be 8814{
064a90a9
MW
8815 Dwarf_Addr bias;
8816 Elf* elf = (dwarf_getelf (dwfl_module_getdwarf (mod, &bias))
8817 ?: dwfl_module_getelf (mod, &bias));
8818
8819 GElf_Ehdr ehdr_mem;
8820 GElf_Ehdr* em = gelf_getehdr (elf, &ehdr_mem);
8821 if (em == NULL) throw SEMANTIC_ERROR (_("Couldn't get elf header"));
8822
8823 /* Only old ELFv1 PPC64 ABI have function descriptors. */
8824 opd_section = SHN_UNDEF;
8825 if (em->e_machine != EM_PPC64 || (em->e_flags & EF_PPC64_ABI) == 2)
8826 return;
8827
46f7b6be
JK
8828 /*
8829 * The .opd section contains function descriptors that can look
8830 * just like function entry points. For example, there's a function
8831 * descriptor called "do_exit" that links to the entry point ".do_exit".
8832 * Reject all symbols in .opd.
8833 */
46f7b6be
JK
8834 Elf_Scn* scn = 0;
8835 size_t shstrndx;
8836
8837 if (!elf)
8838 return;
fcc30d6d 8839 if (elf_getshdrstrndx (elf, &shstrndx) != 0)
46f7b6be
JK
8840 return;
8841 while ((scn = elf_nextscn(elf, scn)) != NULL)
8842 {
8843 GElf_Shdr shdr_mem;
8844 GElf_Shdr *shdr = gelf_getshdr(scn, &shdr_mem);
8845 if (!shdr)
8846 continue;
8847 const char *name = elf_strptr(elf, shstrndx, shdr->sh_name);
8848 if (!strcmp(name, ".opd"))
8849 {
8850 opd_section = elf_ndxscn(scn);
8851 return;
8852 }
8853 }
46f7b6be
JK
8854}
8855
8856bool
8857symbol_table::reject_section(GElf_Word section)
8858{
064a90a9 8859 if (section == SHN_UNDEF || section == opd_section)
46f7b6be 8860 return true;
46f7b6be
JK
8861 return false;
8862}
8863
5f0a03a6
JK
8864enum info_status
8865symbol_table::get_from_elf()
8866{
5f0a03a6
JK
8867 Dwfl_Module *mod = mod_info->mod;
8868 int syments = dwfl_module_getsymtab(mod);
8869 assert(syments);
46f7b6be 8870 prepare_section_rejection(mod);
b4c6a4b1 8871
5f0a03a6
JK
8872 for (int i = 1; i < syments; ++i)
8873 {
8874 GElf_Sym sym;
ab91b232 8875 GElf_Word section;
cba67d8f
MW
8876 GElf_Addr addr;
8877 bool reject;
8878
8879/* Note that dwfl_module_getsym does adjust the sym.st_value but doesn't
8880 try to resolve it to a function address. dwfl_module_getsym_info leaves
8881 the st_value in tact (no adjustment applied) and returns the fully
8882 resolved address separately. In that case we can simply reject the
8883 symbol if it is SHN_UNDEF and don't need to call reject_section which
8884 does extra checks to see whether the address fall in an architecture
8885 specific descriptor table (which will never be the case when using the
8886 new dwfl_module_getsym_info). dwfl_module_getsym will only provide us
8887 with the (adjusted) st_value of the symbol, which might point into a
8888 function descriptor table. So in that case we still have to call
8889 reject_section. */
8890#if _ELFUTILS_PREREQ (0, 158)
45a63356 8891 const char* n = dwfl_module_getsym_info (mod, i, &sym, &addr, &section,
cba67d8f
MW
8892 NULL, NULL);
8893 reject = section == SHN_UNDEF;
8894#else
45a63356 8895 const char* n = dwfl_module_getsym (mod, i, &sym, &section);
b23098cd 8896 addr = sym.st_value;
cba67d8f
MW
8897 reject = reject_section(section);
8898#endif
45a63356
FCE
8899 if (! n)
8900 continue;
8901 interned_string name = n;
8902
61218615 8903 Dwarf_Addr entrypc = addr;
45a63356 8904 if (GELF_ST_TYPE(sym.st_info) == STT_FUNC)
ab91b232 8905 add_symbol(name, (GELF_ST_BIND(sym.st_info) == STB_WEAK),
61218615 8906 reject, addr, entrypc);
45a63356
FCE
8907 if (GELF_ST_TYPE(sym.st_info) == STT_OBJECT
8908 && GELF_ST_BIND(sym.st_info) == STB_GLOBAL)
2a4acb09 8909 globals[name] = addr;
45a63356
FCE
8910 if (GELF_ST_TYPE(sym.st_info) == STT_OBJECT
8911 && GELF_ST_BIND(sym.st_info) == STB_LOCAL)
2a4acb09 8912 locals[name] = addr;
5f0a03a6
JK
8913 }
8914 return info_present;
8915}
8916
5f0a03a6
JK
8917func_info *
8918symbol_table::get_func_containing_address(Dwarf_Addr addr)
8919{
b4662f6b 8920 auto iter = map_by_addr.upper_bound(addr);
1c6b77e5 8921 if (iter == map_by_addr.begin())
5f0a03a6 8922 return NULL;
2e67a43b 8923 else
1c6b77e5 8924 return (--iter)->second;
5f0a03a6
JK
8925}
8926
3d372d6b
SC
8927func_info *
8928symbol_table::get_first_func()
8929{
b4662f6b 8930 auto iter = map_by_addr.begin();
3d372d6b
SC
8931 return (iter)->second;
8932}
8933
f80d9cdb
MW
8934/* Note this function filters out any symbols that are "rejected" because
8935 they are "descriptor" function symbols or SHN_UNDEF symbols. */
fb5b4841 8936set <func_info*>
45a63356 8937symbol_table::lookup_symbol(interned_string name)
5f0a03a6 8938{
fb5b4841 8939 set<func_info*> fis;
b4662f6b
JS
8940 auto ret = map_by_name.equal_range(name);
8941 for (auto it = ret.first; it != ret.second; ++it)
f80d9cdb
MW
8942 if (! it->second->descriptor)
8943 fis.insert(it->second);
d16f315f 8944 return fis;
5f0a03a6
JK
8945}
8946
f80d9cdb
MW
8947/* Filters out the same "descriptor" or SHN_UNDEF symbols as
8948 symbol_table::lookup_symbol. */
fb5b4841 8949set <Dwarf_Addr>
45a63356 8950symbol_table::lookup_symbol_address(interned_string name)
5f0a03a6 8951{
fb5b4841
FCE
8952 set <Dwarf_Addr> addrs;
8953 set <func_info*> fis = lookup_symbol(name);
d16f315f 8954
b4662f6b 8955 for (auto it=fis.begin(); it!=fis.end(); ++it)
fb5b4841 8956 addrs.insert((*it)->addr);
d16f315f
HK
8957
8958 return addrs;
5f0a03a6
JK
8959}
8960
ab91b232
JK
8961// This is the kernel symbol table. The kernel macro cond_syscall creates
8962// a weak symbol for each system call and maps it to sys_ni_syscall.
8963// For system calls not implemented elsewhere, this weak symbol shows up
8964// in the kernel symbol table. Following the precedent of dwarfful stap,
8965// we refuse to consider such symbols. Here we delete them from our
8966// symbol table.
8967// TODO: Consider generalizing this and/or making it part of blacklist
8968// processing.
8969void
8970symbol_table::purge_syscall_stubs()
8971{
fb5b4841
FCE
8972 set<Dwarf_Addr> addrs = lookup_symbol_address("sys_ni_syscall");
8973 if (addrs.empty())
ab91b232 8974 return;
fb5b4841 8975
d16f315f 8976 /* Highly unlikely that multiple symbols named "sys_ni_syscall" may exist */
fb5b4841 8977 if (addrs.size() > 1)
064a90a9 8978 cerr << _("Multiple 'sys_ni_syscall' symbols found.\n");
fb5b4841 8979 Dwarf_Addr stub_addr = * addrs.begin();
d16f315f 8980
b4662f6b
JS
8981 auto purge_range = map_by_addr.equal_range(stub_addr);
8982 for (auto iter = purge_range.first;
2e67a43b 8983 iter != purge_range.second;
1c6b77e5 8984 )
ab91b232 8985 {
1c6b77e5 8986 func_info *fi = iter->second;
2e67a43b 8987 if (fi->weak && fi->name != "sys_ni_syscall")
ab91b232 8988 {
2e67a43b 8989 map_by_name.erase(fi->name);
1c6b77e5 8990 map_by_addr.erase(iter++);
2e67a43b 8991 delete fi;
2e67a43b 8992 }
1c6b77e5
JS
8993 else
8994 iter++;
ab91b232
JK
8995 }
8996}
8997
5f0a03a6 8998void
5f52fafe 8999module_info::get_symtab()
5f0a03a6 9000{
1c6b77e5
JS
9001 if (symtab_status != info_unknown)
9002 return;
9003
5f0a03a6
JK
9004 sym_table = new symbol_table(this);
9005 if (!elf_path.empty())
9006 {
5f0a03a6
JK
9007 symtab_status = sym_table->get_from_elf();
9008 }
9009 else
9010 {
9011 assert(name == TOK_KERNEL);
ab3ed72d
DS
9012 symtab_status = info_absent;
9013 cerr << _("Error: Cannot find vmlinux.") << endl;;
5f0a03a6
JK
9014 }
9015 if (symtab_status == info_absent)
9016 {
9017 delete sym_table;
9018 sym_table = NULL;
9019 return;
9020 }
9021
ab91b232
JK
9022 if (name == TOK_KERNEL)
9023 sym_table->purge_syscall_stubs();
5f0a03a6
JK
9024}
9025
1c6b77e5
JS
9026// update_symtab reconciles data between the elf symbol table and the dwarf
9027// function enumeration. It updates the symbol table entries with the dwarf
9028// die that describes the function, which also signals to query_module_symtab
9029// that a statement probe isn't needed. In return, it also adds aliases to the
9030// function table for names that share the same addr/die.
9031void
9032module_info::update_symtab(cu_function_cache_t *funcs)
9033{
9034 if (!sym_table)
9035 return;
9036
9037 cu_function_cache_t new_funcs;
9038
b4662f6b 9039 for (auto func = funcs->begin();
1c6b77e5
JS
9040 func != funcs->end(); func++)
9041 {
9042 // optimization: inlines will never be in the symbol table
9043 if (dwarf_func_inline(&func->second) != 0)
6e6d9a3b
JL
9044 {
9045 inlined_funcs.insert(func->first);
9046 continue;
9047 }
1c6b77e5 9048
61218615
JS
9049 // We need to make additional efforts to match mangled elf names to dwarf
9050 // too. DW_AT_linkage_name (or w/ MIPS) can help, but that's sometimes
1ffb8bd1
JS
9051 // missing, so we may also need to try matching by address. See also the
9052 // notes about _Z in dwflpp::iterate_over_functions().
45a63356 9053 interned_string name = dwarf_linkage_name(&func->second) ?: func->first;
1ffb8bd1 9054
61218615 9055 set<func_info*> fis = sym_table->lookup_symbol(name);
fb5b4841 9056 if (fis.empty())
1c6b77e5
JS
9057 continue;
9058
b4662f6b 9059 for (auto fi = fis.begin(); fi!=fis.end(); ++fi)
1c6b77e5 9060 {
d16f315f 9061 // iterate over all functions at the same address
b4662f6b
JS
9062 auto er = sym_table->map_by_addr.equal_range((*fi)->addr);
9063 for (auto it = er.first; it != er.second; ++it)
d16f315f
HK
9064 {
9065 // update this function with the dwarf die
9066 it->second->die = func->second;
1c6b77e5 9067
d16f315f
HK
9068 // if this function is a new alias, then
9069 // save it to merge into the function cache
9070 if (it->second != *fi)
9071 new_funcs.insert(make_pair(it->second->name, it->second->die));
9072 }
1c6b77e5
JS
9073 }
9074 }
9075
9076 // add all discovered aliases back into the function cache
9077 // NB: this won't replace any names that dwarf may have already found
9078 funcs->insert(new_funcs.begin(), new_funcs.end());
9079}
9080
5f0a03a6
JK
9081module_info::~module_info()
9082{
9083 if (sym_table)
9084 delete sym_table;
b55bc428
FCE
9085}
9086
935447c8 9087// ------------------------------------------------------------------------
888af770 9088// user-space probes
935447c8
DS
9089// ------------------------------------------------------------------------
9090
935447c8 9091
888af770 9092struct uprobe_derived_probe_group: public generic_dpg<uprobe_derived_probe>
935447c8 9093{
89ba3085
FCE
9094private:
9095 string make_pbm_key (uprobe_derived_probe* p) {
45a63356 9096 return (string)p->path + "|" + (string)p->module + "|" + (string)p->section + "|" + (string)lex_cast(p->pid);
89ba3085
FCE
9097 }
9098
cfcab6c7
JS
9099 void emit_module_maxuprobes (systemtap_session& s);
9100
2b69faaf
JS
9101 // Using our own utrace-based uprobes
9102 void emit_module_utrace_decls (systemtap_session& s);
9103 void emit_module_utrace_init (systemtap_session& s);
9104 void emit_module_utrace_exit (systemtap_session& s);
9105
9106 // Using the upstream inode-based uprobes
9107 void emit_module_inode_decls (systemtap_session& s);
9108 void emit_module_inode_init (systemtap_session& s);
3642eb17 9109 void emit_module_inode_refresh (systemtap_session& s);
2b69faaf
JS
9110 void emit_module_inode_exit (systemtap_session& s);
9111
3a894f7e
JS
9112 // Using the dyninst backend (via stapdyn)
9113 void emit_module_dyninst_decls (systemtap_session& s);
9114 void emit_module_dyninst_init (systemtap_session& s);
9115 void emit_module_dyninst_exit (systemtap_session& s);
9116
285ca427
DS
9117 // Perf support
9118 unsigned max_perf_counters;
9119 void emit_module_perf_read_handlers (systemtap_session& s);
9120
935447c8 9121public:
29a5287d
DS
9122 uprobe_derived_probe_group(): max_perf_counters(0) {}
9123
888af770 9124 void emit_module_decls (systemtap_session& s);
935447c8 9125 void emit_module_init (systemtap_session& s);
3642eb17 9126 void emit_module_refresh (systemtap_session& s);
935447c8 9127 void emit_module_exit (systemtap_session& s);
ca6d3b0f
JL
9128
9129 // on-the-fly only supported for inode-uprobes
9130 bool otf_supported (systemtap_session& s)
9131 { return !s.runtime_usermode_p()
9132 && kernel_supports_inode_uprobes(s); }
9133
9134 // workqueue manipulation is safe in uprobes
9135 bool otf_safe_context (systemtap_session& s)
9136 { return otf_supported(s); }
3530e7a7 9137
fcdd71ba
WC
9138 friend bool sort_for_bpf(systemtap_session& s,
9139 uprobe_derived_probe_group *upg,
3530e7a7 9140 sort_for_bpf_probe_arg_vector &v);
935447c8
DS
9141};
9142
9143
888af770
FCE
9144void
9145uprobe_derived_probe::join_group (systemtap_session& s)
9146{
9147 if (! s.uprobe_derived_probes)
9148 s.uprobe_derived_probes = new uprobe_derived_probe_group ();
9149 s.uprobe_derived_probes->enroll (this);
ca6d3b0f
JL
9150 this->group = s.uprobe_derived_probes;
9151
f31a77f5
DS
9152 if (s.runtime_usermode_p())
9153 enable_dynprobes(s);
9154 else
4441e344 9155 enable_task_finder(s);
a96d1db0 9156
8a03658e 9157 // Ask buildrun.cxx to build extra module if needed, and
d3e959b0
DS
9158 // signal staprun to load that module. If we're using the builtin
9159 // inode-uprobes, we still need to know that it is required.
8a03658e 9160 s.need_uprobes = true;
a96d1db0
DN
9161}
9162
888af770 9163
c0f84e7b
SC
9164void
9165uprobe_derived_probe::getargs(std::list<std::string> &arg_set) const
9166{
9167 dwarf_derived_probe::getargs(arg_set);
9168 arg_set.insert(arg_set.end(), args.begin(), args.end());
9169}
9170
9171
9172void
9173uprobe_derived_probe::saveargs(int nargs)
9174{
9175 for (int i = 1; i <= nargs; i++)
9176 args.push_back("$arg" + lex_cast (i) + ":long");
9177}
9178
9179
2865d17a 9180void
42e38653 9181uprobe_derived_probe::emit_privilege_assertion (translator_output* o)
2865d17a
DB
9182{
9183 // These probes are allowed for unprivileged users, but only in the
9184 // context of processes which they own.
9185 emit_process_owner_assertion (o);
9186}
9187
9188
285ca427
DS
9189void
9190uprobe_derived_probe::emit_perf_read_handler (systemtap_session &s,
9191 unsigned idx)
9192{
9193 if (perf_counter_refs.size())
9194 {
9195 unsigned ref_idx = 0;
9196 s.op->newline() << "static void stap_perf_read_handler_" << idx
9197 << "(long *values) {";
9198 s.op->indent(1);
9199
9200 for (auto pcii = perf_counter_refs.begin();
9201 pcii != perf_counter_refs.end();
9202 pcii++)
9203 {
9204 // Find the associated perf.counter probe
9205 unsigned i = 0;
9206 for (auto it=s.perf_counters.begin() ;
9207 it != s.perf_counters.end();
9208 it++, i++)
9209 {
9210 if ((*it).first == (*pcii))
9211 {
9212 s.op->newline() << "values[" << ref_idx
9213 << "] = _stp_perf_read(smp_processor_id(),"
9214 << i << ");";
9215 ref_idx++;
9216 break;
9217 }
9218 }
9219 }
9220 s.op->newline() << "return;";
9221 s.op->newline(-1) << "}";
9222 }
9223}
9224
888af770 9225struct uprobe_builder: public derived_probe_builder
a96d1db0 9226{
888af770 9227 uprobe_builder() {}
2b69faaf 9228 virtual void build(systemtap_session & sess,
a96d1db0
DN
9229 probe * base,
9230 probe_point * location,
86bf665e 9231 literal_map_t const & parameters,
a96d1db0
DN
9232 vector<derived_probe *> & finished_results)
9233 {
888af770 9234 int64_t process, address;
a96d1db0 9235
2b69faaf 9236 if (kernel_supports_inode_uprobes(sess))
dc09353a 9237 throw SEMANTIC_ERROR (_("absolute process probes not available [man error::inode-uprobes]"));
2b69faaf 9238
888af770 9239 bool b1 = get_param (parameters, TOK_PROCESS, process);
ced347a9 9240 (void) b1;
888af770 9241 bool b2 = get_param (parameters, TOK_STATEMENT, address);
ced347a9 9242 (void) b2;
888af770
FCE
9243 bool rr = has_null_param (parameters, TOK_RETURN);
9244 assert (b1 && b2); // by pattern_root construction
a96d1db0 9245
0973d815 9246 finished_results.push_back(new uprobe_derived_probe(base, location, process, address, rr));
a96d1db0 9247 }
352c84fe
FL
9248
9249 virtual string name() { return "uprobe builder"; }
a96d1db0
DN
9250};
9251
9252
9253void
cfcab6c7 9254uprobe_derived_probe_group::emit_module_maxuprobes (systemtap_session& s)
a96d1db0 9255{
43241c44
FCE
9256 // We'll probably need at least this many:
9257 unsigned minuprobes = probes.size();
9258 // .. but we don't want so many that .bss is inflated (PR10507):
9259 unsigned uprobesize = 64;
9260 unsigned maxuprobesmem = 10*1024*1024; // 10 MB
9261 unsigned maxuprobes = maxuprobesmem / uprobesize;
9262
aaf7ffe8
FCE
9263 // Let's choose a value on the geometric middle. This should end up
9264 // between minuprobes and maxuprobes. It's OK if this number turns
9265 // out to be < minuprobes or > maxuprobes. At worst, we get a
9266 // run-time error of one kind (too few: missed uprobe registrations)
9267 // or another (too many: vmalloc errors at module load time).
9268 unsigned default_maxuprobes = (unsigned)sqrt((double)minuprobes * (double)maxuprobes);
43241c44 9269
6d0f3f0c 9270 s.op->newline() << "#ifndef MAXUPROBES";
43241c44 9271 s.op->newline() << "#define MAXUPROBES " << default_maxuprobes;
6d0f3f0c 9272 s.op->newline() << "#endif";
cfcab6c7
JS
9273}
9274
9275
285ca427
DS
9276void
9277uprobe_derived_probe_group::emit_module_perf_read_handlers (systemtap_session& s)
9278{
9279 // If we're using perf counters, output the handler function(s)
9280 // before the actual uprobe probe handler function.
9281 for (unsigned i=0; i<probes.size(); i++)
9282 {
9283 uprobe_derived_probe *p = probes[i];
9284 p->emit_perf_read_handler(s, i);
9285 }
9286}
9287
9288
9289void
9290udpg_entryfn_prologue_declaration_callback (systemtap_session& s, void* data)
9291{
9292 unsigned nvalues = (unsigned)(unsigned long)data;
9293 if (nvalues > 0)
9294 {
9295 // Note that only gurus can exceed the maximum number of perf
9296 // values used in 1 probe. Since we store the perf values on
9297 // the stack, we can't have too many.
9298 if (!s.guru_mode && nvalues > 16)
9299 throw SEMANTIC_ERROR(_F("Too many simultaneous uses of perf values (%d is greater than 16)",
9300 nvalues));
9301 s.op->newline() << "long perf_read_values[" << nvalues << "];";
9302 }
9303}
9304
9305
9306void
9307udpg_entryfn_prologue_pre_context_callback (systemtap_session& s, void* data)
9308{
9309 unsigned nvalues = (unsigned)(unsigned long)data;
9310 if (nvalues == 0 || s.runtime_usermode_p())
9311 return;
9312
9313 if (kernel_supports_inode_uprobes (s))
9314 {
9315 s.op->newline() << "if (sup->perf_read_handler)";
9316 s.op->newline(1) << "sup->perf_read_handler(perf_read_values);";
9317 s.op->indent(-1);
9318 }
9319 else
9320 {
9321 s.op->newline() << "if (sups->perf_read_handler)";
9322 s.op->newline(1) << "sups->perf_read_handler(perf_read_values);";
9323 s.op->indent(-1);
9324 }
9325}
9326
9327
cfcab6c7
JS
9328void
9329uprobe_derived_probe_group::emit_module_utrace_decls (systemtap_session& s)
9330{
9331 if (probes.empty()) return;
9332 s.op->newline() << "/* ---- utrace uprobes ---- */";
9333 // If uprobes isn't in the kernel, pull it in from the runtime.
9334
9335 s.op->newline() << "#if defined(CONFIG_UPROBES) || defined(CONFIG_UPROBES_MODULE)";
9336 s.op->newline() << "#include <linux/uprobes.h>";
9337 s.op->newline() << "#else";
2ba1736a 9338 s.op->newline() << "#include \"linux/uprobes/uprobes.h\"";
cfcab6c7
JS
9339 s.op->newline() << "#endif";
9340 s.op->newline() << "#ifndef UPROBES_API_VERSION";
9341 s.op->newline() << "#define UPROBES_API_VERSION 1";
9342 s.op->newline() << "#endif";
9343
9344 emit_module_maxuprobes (s);
285ca427 9345 emit_module_perf_read_handlers(s);
a96d1db0 9346
cc52276b 9347 // Forward decls
2ba1736a 9348 s.op->newline() << "#include \"linux/uprobes-common.h\"";
cc52276b 9349
5e112f92
FCE
9350 // In .bss, the shared pool of uprobe/uretprobe structs. These are
9351 // too big to embed in the initialized .data stap_uprobe_spec array.
cc52276b
WC
9352 // XXX: consider a slab cache or somesuch for stap_uprobes
9353 s.op->newline() << "static struct stap_uprobe stap_uprobes [MAXUPROBES];";
6b378b7c 9354 s.op->newline() << "static DEFINE_MUTEX(stap_uprobes_lock);"; // protects against concurrent registration/unregistration
a96d1db0 9355
89ba3085
FCE
9356 s.op->assert_0_indent();
9357
89ba3085
FCE
9358 // Assign task-finder numbers as we build up the stap_uprobe_tf table.
9359 // This means we process probes[] in two passes.
9360 map <string,unsigned> module_index;
9361 unsigned module_index_ctr = 0;
9362
cc52276b
WC
9363 // not const since embedded task_finder_target struct changes
9364 s.op->newline() << "static struct stap_uprobe_tf stap_uprobe_finders[] = {";
89ba3085
FCE
9365 s.op->indent(1);
9366 for (unsigned i=0; i<probes.size(); i++)
9367 {
9368 uprobe_derived_probe *p = probes[i];
9369 string pbmkey = make_pbm_key (p);
9370 if (module_index.find (pbmkey) == module_index.end())
9371 {
9372 module_index[pbmkey] = module_index_ctr++;
9373
9374 s.op->newline() << "{";
9375 // NB: it's essential that make_pbm_key() use all of and
9376 // only the same fields as we're about to emit.
9377 s.op->line() << " .finder={";
1af100fc 9378 s.op->line() << " .purpose=\"uprobes\",";
89ba3085 9379 if (p->pid != 0)
68910c97
JK
9380 s.op->line() << " .pid=" << p->pid << ",";
9381
9382 if (p->section == "") // .statement(addr).absolute
9383 s.op->line() << " .callback=&stap_uprobe_process_found,";
89ba3085
FCE
9384 else if (p->section == ".absolute") // proxy for ET_EXEC -> exec()'d program
9385 {
9386 s.op->line() << " .procname=" << lex_cast_qstring(p->module) << ",";
9387 s.op->line() << " .callback=&stap_uprobe_process_found,";
9388 }
68910c97 9389 else if (p->section != ".absolute") // ET_DYN
89ba3085 9390 {
4ad95bbc
SC
9391 if (p->has_library)
9392 s.op->line() << " .procname=\"" << p->path << "\", ";
89ba3085
FCE
9393 s.op->line() << " .mmap_callback=&stap_uprobe_mmap_found, ";
9394 s.op->line() << " .munmap_callback=&stap_uprobe_munmap_found, ";
19d91f6c 9395 s.op->line() << " .callback=&stap_uprobe_process_munmap,";
89ba3085 9396 }
89ba3085 9397 s.op->line() << " },";
68910c97
JK
9398 if (p->module != "")
9399 s.op->line() << " .pathname=" << lex_cast_qstring(p->module) << ", ";
89ba3085
FCE
9400 s.op->line() << " },";
9401 }
c57ea854 9402 else
822a6a3d 9403 { } // skip it in this pass, already have a suitable stap_uprobe_tf slot for it.
89ba3085
FCE
9404 }
9405 s.op->newline(-1) << "};";
9406
9407 s.op->assert_0_indent();
9408
3689db05
SC
9409 unsigned pci;
9410 for (pci=0; pci<probes.size(); pci++)
9411 {
9412 // List of perf counters used by each probe
9413 // This list is an index into struct stap_perf_probe,
9414 uprobe_derived_probe *p = probes[pci];
3689db05 9415 s.op->newline() << "long perf_counters_" + lex_cast(pci) + "[] = {";
b4662f6b 9416 for (auto pcii = p->perf_counter_refs.begin();
3689db05
SC
9417 pcii != p->perf_counter_refs.end(); pcii++)
9418 {
3689db05
SC
9419 unsigned i = 0;
9420 // Find the associated perf.counter probe
b4662f6b 9421 for (auto it = s.perf_counters.begin();
3689db05 9422 it != s.perf_counters.end(); it++, i++)
aa2c662f 9423 if ((*it).first == (*pcii))
3689db05
SC
9424 break;
9425 s.op->line() << lex_cast(i) << ", ";
9426 }
9427 s.op->newline() << "};";
9428 }
9429
cc52276b
WC
9430 // NB: read-only structure
9431 s.op->newline() << "static const struct stap_uprobe_spec stap_uprobe_specs [] = {";
a96d1db0 9432 s.op->indent(1);
888af770
FCE
9433 for (unsigned i =0; i<probes.size(); i++)
9434 {
9435 uprobe_derived_probe* p = probes[i];
9436 s.op->newline() << "{";
89ba3085
FCE
9437 string key = make_pbm_key (p);
9438 unsigned value = module_index[key];
759e1d76
FCE
9439 if (value != 0)
9440 s.op->line() << " .tfi=" << value << ",";
6b66b9f7 9441 s.op->line() << " .address=(unsigned long)0x" << hex << p->addr << dec << "ULL,";
faea5e16 9442 s.op->line() << " .probe=" << common_probe_init (p) << ",";
4ddb6dd0 9443
038c38c6 9444 if (p->sdt_semaphore_addr != 0)
63b4fd14 9445 s.op->line() << " .sdt_sem_offset=(unsigned long)0x"
038c38c6 9446 << hex << p->sdt_semaphore_addr << dec << "ULL,";
4ddb6dd0 9447
285ca427
DS
9448 // Don't bother emit if array is empty.
9449 if (p->perf_counter_refs.size())
9450 {
9451 s.op->line() << " .perf_counters_dim=ARRAY_SIZE(perf_counters_" << lex_cast(i) << "),";
9452 // List of perf counters used by a probe from above
9453 s.op->line() << " .perf_counters=perf_counters_" + lex_cast(i) + ",";
9454 s.op->line() << " .perf_read_handler=&stap_perf_read_handler_"
9455 + lex_cast(i) + ",";
9456 }
4ddb6dd0
JS
9457 if (p->has_return)
9458 s.op->line() << " .return_p=1,";
888af770
FCE
9459 s.op->line() << " },";
9460 }
9461 s.op->newline(-1) << "};";
a96d1db0 9462
89ba3085
FCE
9463 s.op->assert_0_indent();
9464
48e685da 9465 s.op->newline() << "static void enter_uprobe_probe (struct uprobe *inst, struct pt_regs *regs) {";
888af770 9466 s.op->newline(1) << "struct stap_uprobe *sup = container_of(inst, struct stap_uprobe, up);";
89ba3085 9467 s.op->newline() << "const struct stap_uprobe_spec *sups = &stap_uprobe_specs [sup->spec_index];";
71db462b 9468 common_probe_entryfn_prologue (s, "STAP_SESSION_RUNNING", "sups->probe",
285ca427
DS
9469 "stp_probe_type_uprobe", true,
9470 udpg_entryfn_prologue_declaration_callback,
9471 udpg_entryfn_prologue_pre_context_callback,
9472 (void *)(unsigned long)max_perf_counters);
0e090c74 9473 s.op->newline() << "if (sup->spec_index < 0 || "
6e895029
DS
9474 << "sup->spec_index >= " << probes.size() << ") {";
9475 s.op->newline(1) << "_stp_error (\"bad spec_index %d (max " << probes.size()
0e090c74 9476 << "): %s\", sup->spec_index, c->probe_point);";
6e895029
DS
9477 s.op->newline() << "goto probe_epilogue;";
9478 s.op->newline(-1) << "}";
d9aed31e 9479 s.op->newline() << "c->uregs = regs;";
e04b5d74 9480 s.op->newline() << "c->user_mode_p = 1;";
6415ddde 9481
285ca427
DS
9482 // assign values to something in context
9483 if (s.perf_counters.size())
9484 s.op->newline() << "c->perf_read_values = perf_read_values;";
9485
6415ddde
MW
9486 // Make it look like the IP is set as it would in the actual user
9487 // task when calling real probe handler. Reset IP regs on return, so
9488 // we don't confuse uprobes. PR10458
9489 s.op->newline() << "{";
9490 s.op->indent(1);
d9aed31e 9491 s.op->newline() << "unsigned long uprobes_ip = REG_IP(c->uregs);";
259d54c0 9492 s.op->newline() << "SET_REG_IP(regs, inst->vaddr);";
26e63673 9493 s.op->newline() << "(*sups->probe->ph) (c);";
259d54c0 9494 s.op->newline() << "SET_REG_IP(regs, uprobes_ip);";
6415ddde
MW
9495 s.op->newline(-1) << "}";
9496
ef1337ee 9497 common_probe_entryfn_epilogue (s, true, otf_safe_context(s));
888af770 9498 s.op->newline(-1) << "}";
a96d1db0 9499
48e685da 9500 s.op->newline() << "static void enter_uretprobe_probe (struct uretprobe_instance *inst, struct pt_regs *regs) {";
888af770 9501 s.op->newline(1) << "struct stap_uprobe *sup = container_of(inst->rp, struct stap_uprobe, urp);";
89ba3085 9502 s.op->newline() << "const struct stap_uprobe_spec *sups = &stap_uprobe_specs [sup->spec_index];";
71db462b 9503 common_probe_entryfn_prologue (s, "STAP_SESSION_RUNNING", "sups->probe",
958a33db
DS
9504 "stp_probe_type_uretprobe", true,
9505 udpg_entryfn_prologue_declaration_callback,
9506 udpg_entryfn_prologue_pre_context_callback,
9507 (void *)(unsigned long)max_perf_counters);
6dceb5c9 9508 s.op->newline() << "c->ips.ri = inst;";
0e090c74 9509 s.op->newline() << "if (sup->spec_index < 0 || "
0d5561a5
DS
9510 << "sup->spec_index >= " << probes.size() << ") {";
9511 s.op->newline(1) << "_stp_error (\"bad spec_index %d (max " << probes.size()
0e090c74 9512 << "): %s\", sup->spec_index, c->probe_point);";
0d5561a5
DS
9513 s.op->newline() << "goto probe_epilogue;";
9514 s.op->newline(-1) << "}";
9515
d9aed31e 9516 s.op->newline() << "c->uregs = regs;";
e04b5d74 9517 s.op->newline() << "c->user_mode_p = 1;";
6415ddde 9518
958a33db
DS
9519 // assign values to something in context
9520 if (s.perf_counters.size())
9521 s.op->newline() << "c->perf_read_values = perf_read_values;";
9522
6415ddde
MW
9523 // Make it look like the IP is set as it would in the actual user
9524 // task when calling real probe handler. Reset IP regs on return, so
9525 // we don't confuse uprobes. PR10458
9526 s.op->newline() << "{";
9527 s.op->indent(1);
d9aed31e 9528 s.op->newline() << "unsigned long uprobes_ip = REG_IP(c->uregs);";
5e562a69 9529 s.op->newline() << "SET_REG_IP(regs, inst->ret_addr);";
26e63673 9530 s.op->newline() << "(*sups->probe->ph) (c);";
259d54c0 9531 s.op->newline() << "SET_REG_IP(regs, uprobes_ip);";
6415ddde
MW
9532 s.op->newline(-1) << "}";
9533
ef1337ee 9534 common_probe_entryfn_epilogue (s, true, otf_safe_context(s));
a96d1db0
DN
9535 s.op->newline(-1) << "}";
9536
89ba3085 9537 s.op->newline();
2ba1736a 9538 s.op->newline() << "#include \"linux/uprobes-common.c\"";
6d0f3f0c 9539 s.op->newline();
888af770 9540}
935447c8
DS
9541
9542
888af770 9543void
2b69faaf 9544uprobe_derived_probe_group::emit_module_utrace_init (systemtap_session& s)
935447c8 9545{
888af770 9546 if (probes.empty()) return;
935447c8 9547
2b69faaf 9548 s.op->newline() << "/* ---- utrace uprobes ---- */";
935447c8 9549
01b05e2e 9550 s.op->newline() << "for (j=0; j<MAXUPROBES; j++) {";
5e112f92
FCE
9551 s.op->newline(1) << "struct stap_uprobe *sup = & stap_uprobes[j];";
9552 s.op->newline() << "sup->spec_index = -1;"; // free slot
80b4ad8b
FCE
9553 // NB: we assume the rest of the struct (specificaly, sup->up) is
9554 // initialized to zero. This is so that we can use
9555 // sup->up->kdata = NULL for "really free!" PR 6829.
5e112f92
FCE
9556 s.op->newline(-1) << "}";
9557 s.op->newline() << "mutex_init (& stap_uprobes_lock);";
935447c8 9558
89ba3085
FCE
9559 // Set up the task_finders
9560 s.op->newline() << "for (i=0; i<sizeof(stap_uprobe_finders)/sizeof(stap_uprobe_finders[0]); i++) {";
9561 s.op->newline(1) << "struct stap_uprobe_tf *stf = & stap_uprobe_finders[i];";
c57ea854 9562 s.op->newline() << "probe_point = stf->pathname;"; // for error messages; XXX: would prefer pp() or something better
89ba3085 9563 s.op->newline() << "rc = stap_register_task_finder_target (& stf->finder);";
935447c8 9564
5e112f92
FCE
9565 // NB: if (rc), there is no need (XXX: nor any way) to clean up any
9566 // finders already registered, since mere registration does not
9567 // cause any utrace or memory allocation actions. That happens only
9568 // later, once the task finder engine starts running. So, for a
9569 // partial initialization requiring unwind, we need do nothing.
9570 s.op->newline() << "if (rc) break;";
a7a68293 9571
888af770
FCE
9572 s.op->newline(-1) << "}";
9573}
d0ea46ce 9574
d0a7f5a9 9575
888af770 9576void
2b69faaf 9577uprobe_derived_probe_group::emit_module_utrace_exit (systemtap_session& s)
888af770
FCE
9578{
9579 if (probes.empty()) return;
2b69faaf 9580 s.op->newline() << "/* ---- utrace uprobes ---- */";
e56e51c9 9581
6d0f3f0c
FCE
9582 // NB: there is no stap_unregister_task_finder_target call;
9583 // important stuff like utrace cleanups are done by
d41d451c
FCE
9584 // __stp_task_finder_cleanup() via stap_stop_task_finder().
9585 //
9586 // This function blocks until all callbacks are completed, so there
9587 // is supposed to be no possibility of any registration-related code starting
9588 // to run in parallel with our shutdown here. So we don't need to protect the
9589 // stap_uprobes[] array with the mutex.
d0a7f5a9 9590
01b05e2e 9591 s.op->newline() << "for (j=0; j<MAXUPROBES; j++) {";
5e112f92 9592 s.op->newline(1) << "struct stap_uprobe *sup = & stap_uprobes[j];";
89ba3085 9593 s.op->newline() << "const struct stap_uprobe_spec *sups = &stap_uprobe_specs [sup->spec_index];";
6d0f3f0c 9594 s.op->newline() << "if (sup->spec_index < 0) continue;"; // free slot
3568f1dd 9595
8faa1fc5 9596 // PR10655: decrement that ENABLED semaphore
c116c31b 9597 s.op->newline() << "if (sup->sdt_sem_address) {";
8faa1fc5
FCE
9598 s.op->newline(1) << "unsigned short sdt_semaphore;"; // NB: fixed size
9599 s.op->newline() << "pid_t pid = (sups->return_p ? sup->urp.u.pid : sup->up.pid);";
9600 s.op->newline() << "struct task_struct *tsk;";
9601 s.op->newline() << "rcu_read_lock();";
6846cfc8 9602
86229a55
DS
9603 // Do a pid->task_struct* lookup. For 2.6.24+, this code assumes
9604 // that the pid is always in the global namespace, not in any
9605 // private namespace.
8faa1fc5 9606 s.op->newline() << "#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,24)";
86229a55
DS
9607 // We'd like to call find_task_by_pid_ns() here, but it isn't
9608 // exported. So, we call what it calls...
9609 s.op->newline() << " tsk = pid_task(find_pid_ns(pid, &init_pid_ns), PIDTYPE_PID);";
8faa1fc5
FCE
9610 s.op->newline() << "#else";
9611 s.op->newline() << " tsk = find_task_by_pid (pid);";
9612 s.op->newline() << "#endif /* 2.6.24 */";
8faa1fc5
FCE
9613
9614 s.op->newline() << "if (tsk) {"; // just in case the thing exited while we weren't watching
3c5b8e2b 9615 s.op->newline(1) << "if (__access_process_vm_noflush(tsk, sup->sdt_sem_address, &sdt_semaphore, sizeof(sdt_semaphore), 0)) {";
63b4fd14 9616 s.op->newline(1) << "sdt_semaphore --;";
903b9fcd 9617 s.op->newline() << "#ifdef DEBUG_UPROBES";
c116c31b 9618 s.op->newline() << "_stp_dbug (__FUNCTION__,__LINE__, \"-semaphore %#x @ %#lx\\n\", sdt_semaphore, sup->sdt_sem_address);";
903b9fcd 9619 s.op->newline() << "#endif";
3c5b8e2b 9620 s.op->newline() << "__access_process_vm_noflush(tsk, sup->sdt_sem_address, &sdt_semaphore, sizeof(sdt_semaphore), 1);";
93c84191 9621 s.op->newline(-1) << "}";
8faa1fc5
FCE
9622 // XXX: need to analyze possibility of race condition
9623 s.op->newline(-1) << "}";
9624 s.op->newline() << "rcu_read_unlock();";
9625 s.op->newline(-1) << "}";
6846cfc8 9626
3568f1dd
FCE
9627 s.op->newline() << "if (sups->return_p) {";
9628 s.op->newline(1) << "#ifdef DEBUG_UPROBES";
89ba3085 9629 s.op->newline() << "_stp_dbug (__FUNCTION__,__LINE__, \"-uretprobe spec %d index %d pid %d addr %p\\n\", sup->spec_index, j, sup->up.pid, (void*) sup->up.vaddr);";
3568f1dd 9630 s.op->newline() << "#endif";
80b4ad8b
FCE
9631 // NB: PR6829 does not change that we still need to unregister at
9632 // *this* time -- when the script as a whole exits.
3568f1dd
FCE
9633 s.op->newline() << "unregister_uretprobe (& sup->urp);";
9634 s.op->newline(-1) << "} else {";
9635 s.op->newline(1) << "#ifdef DEBUG_UPROBES";
8faa1fc5 9636 s.op->newline() << "_stp_dbug (__FUNCTION__,__LINE__, \"-uprobe spec %d index %d pid %d addr %p\\n\", sup->spec_index, j, sup->up.pid, (void*) sup->up.vaddr);";
3568f1dd
FCE
9637 s.op->newline() << "#endif";
9638 s.op->newline() << "unregister_uprobe (& sup->up);";
9639 s.op->newline(-1) << "}";
935447c8 9640
6d0f3f0c 9641 s.op->newline() << "sup->spec_index = -1;";
935447c8 9642
3568f1dd
FCE
9643 // XXX: uprobe missed counts?
9644
6d0f3f0c 9645 s.op->newline(-1) << "}";
935447c8 9646
5e112f92 9647 s.op->newline() << "mutex_destroy (& stap_uprobes_lock);";
935447c8
DS
9648}
9649
2b69faaf
JS
9650
9651void
9652uprobe_derived_probe_group::emit_module_inode_decls (systemtap_session& s)
9653{
9654 if (probes.empty()) return;
9655 s.op->newline() << "/* ---- inode uprobes ---- */";
cfcab6c7 9656 emit_module_maxuprobes (s);
2ba1736a 9657 s.op->newline() << "#include \"linux/uprobes-inode.c\"";
285ca427 9658 emit_module_perf_read_handlers(s);
2b69faaf
JS
9659
9660 // Write the probe handler.
79af55c3
JS
9661 s.op->newline() << "static int stapiu_probe_handler "
9662 << "(struct stapiu_consumer *sup, struct pt_regs *regs) {";
9663 s.op->newline(1);
2dbbd473
JS
9664
9665 // Since we're sharing the entry function, we have to dynamically choose the probe_type
9666 string probe_type = "(sup->return_p ? stp_probe_type_uretprobe : stp_probe_type_uprobe)";
71db462b 9667 common_probe_entryfn_prologue (s, "STAP_SESSION_RUNNING", "sup->probe",
285ca427
DS
9668 probe_type, true,
9669 udpg_entryfn_prologue_declaration_callback,
9670 udpg_entryfn_prologue_pre_context_callback,
9671 (void *)(unsigned long)max_perf_counters);
2dbbd473 9672
3bff6634 9673 s.op->newline() << "c->uregs = regs;";
e04b5d74 9674 s.op->newline() << "c->user_mode_p = 1;";
285ca427
DS
9675
9676 // assign values to something in context
9677 if (s.perf_counters.size())
9678 s.op->newline() << "c->perf_read_values = perf_read_values;";
9679
497cfcb2 9680 // NB: IP is already set by stapiu_probe_prehandler in uprobes-inode.c
2b69faaf 9681 s.op->newline() << "(*sup->probe->ph) (c);";
39e63bae 9682
ef1337ee 9683 common_probe_entryfn_epilogue (s, true, otf_safe_context(s));
2b69faaf
JS
9684 s.op->newline() << "return 0;";
9685 s.op->newline(-1) << "}";
9686 s.op->assert_0_indent();
9687
9688 // Index of all the modules for which we need inodes.
9689 map<string, unsigned> module_index;
9690 unsigned module_index_ctr = 0;
9691
9692 // Discover and declare targets for each unique path.
cfcab6c7 9693 s.op->newline() << "static struct stapiu_target "
2b69faaf
JS
9694 << "stap_inode_uprobe_targets[] = {";
9695 s.op->indent(1);
9696 for (unsigned i=0; i<probes.size(); i++)
9697 {
9698 uprobe_derived_probe *p = probes[i];
cfcab6c7
JS
9699 const string key = make_pbm_key(p);
9700 if (module_index.find (key) == module_index.end())
2b69faaf 9701 {
cfcab6c7
JS
9702 module_index[key] = module_index_ctr++;
9703 s.op->newline() << "{";
9704 s.op->line() << " .finder={";
b78a0fbb 9705 s.op->line() << " .purpose=\"inode-uprobes\",";
cfcab6c7
JS
9706 if (p->pid != 0)
9707 s.op->line() << " .pid=" << p->pid << ",";
9708
9709 if (p->section == "") // .statement(addr).absolute XXX?
9710 s.op->line() << " .callback=&stapiu_process_found,";
9711 else if (p->section == ".absolute") // proxy for ET_EXEC -> exec()'d program
9712 {
9713 s.op->line() << " .procname=" << lex_cast_qstring(p->module) << ",";
9714 s.op->line() << " .callback=&stapiu_process_found,";
9715 }
9716 else if (p->section != ".absolute") // ET_DYN
9717 {
9718 if (p->has_library)
9719 s.op->line() << " .procname=\"" << p->path << "\", ";
9720 s.op->line() << " .mmap_callback=&stapiu_mmap_found, ";
9721 s.op->line() << " .munmap_callback=&stapiu_munmap_found, ";
9722 s.op->line() << " .callback=&stapiu_process_munmap,";
9723 }
9724 s.op->line() << " },";
9725 s.op->line() << " .filename=" << lex_cast_qstring(p->module) << ",";
9726 s.op->line() << " },";
2b69faaf
JS
9727 }
9728 }
9729 s.op->newline(-1) << "};";
9730 s.op->assert_0_indent();
9731
9732 // Declare the actual probes.
3689db05
SC
9733 unsigned pci;
9734 for (pci=0; pci<probes.size(); pci++)
9735 {
9736 // List of perf counters used by each probe
9737 // This list is an index into struct stap_perf_probe,
9738 uprobe_derived_probe *p = probes[pci];
3689db05 9739 s.op->newline() << "long perf_counters_" + lex_cast(pci) + "[] = {";
b4662f6b 9740 for (auto pcii = p->perf_counter_refs.begin();
3689db05
SC
9741 pcii != p->perf_counter_refs.end(); pcii++)
9742 {
3689db05
SC
9743 unsigned i = 0;
9744 // Find the associated perf.counter probe
b4662f6b 9745 for (auto it = s.perf_counters.begin();
4fa83377 9746 it != s.perf_counters.end(); it++, i++)
aa2c662f 9747 if ((*it).first == (*pcii))
3689db05
SC
9748 break;
9749 s.op->line() << lex_cast(i) << ", ";
9750 }
9751 s.op->newline() << "};";
9752 }
9753
cfcab6c7 9754 s.op->newline() << "static struct stapiu_consumer "
2b69faaf
JS
9755 << "stap_inode_uprobe_consumers[] = {";
9756 s.op->indent(1);
9757 for (unsigned i=0; i<probes.size(); i++)
9758 {
9759 uprobe_derived_probe *p = probes[i];
cfcab6c7
JS
9760 unsigned index = module_index[make_pbm_key(p)];
9761 s.op->newline() << "{";
79af55c3
JS
9762 if (p->has_return)
9763 s.op->line() << " .return_p=1,";
cfcab6c7
JS
9764 s.op->line() << " .target=&stap_inode_uprobe_targets[" << index << "],";
9765 s.op->line() << " .offset=(loff_t)0x" << hex << p->addr << dec << "ULL,";
9766 if (p->sdt_semaphore_addr)
9767 s.op->line() << " .sdt_sem_offset=(loff_t)0x"
9768 << hex << p->sdt_semaphore_addr << dec << "ULL,";
285ca427
DS
9769
9770 // Don't bother emit if array is empty.
9771 if (p->perf_counter_refs.size())
9772 {
9773 s.op->line() << " .perf_counters_dim=ARRAY_SIZE(perf_counters_" << lex_cast(i) << "),";
9774 // List of perf counters used by a probe from above
9775 s.op->line() << " .perf_counters=perf_counters_" + lex_cast(i) + ",";
9776 s.op->line() << " .perf_read_handler=&stap_perf_read_handler_"
9777 + lex_cast(i) + ",";
9778 }
9779
cfcab6c7
JS
9780 s.op->line() << " .probe=" << common_probe_init (p) << ",";
9781 s.op->line() << " },";
2b69faaf
JS
9782 }
9783 s.op->newline(-1) << "};";
9784 s.op->assert_0_indent();
9785}
9786
9787
9788void
9789uprobe_derived_probe_group::emit_module_inode_init (systemtap_session& s)
9790{
9791 if (probes.empty()) return;
9792 s.op->newline() << "/* ---- inode uprobes ---- */";
b0f614a0
DS
9793 // Let stapiu_init() handle reporting errors by setting probe_point
9794 // to NULL.
9795 s.op->newline() << "probe_point = NULL;";
cfcab6c7 9796 s.op->newline() << "rc = stapiu_init ("
2b69faaf
JS
9797 << "stap_inode_uprobe_targets, "
9798 << "ARRAY_SIZE(stap_inode_uprobe_targets), "
9799 << "stap_inode_uprobe_consumers, "
9800 << "ARRAY_SIZE(stap_inode_uprobe_consumers));";
9801}
9802
9803
3642eb17
JL
9804void
9805uprobe_derived_probe_group::emit_module_inode_refresh (systemtap_session& s)
9806{
9807 if (probes.empty()) return;
9808 s.op->newline() << "/* ---- inode uprobes ---- */";
3642eb17
JL
9809 s.op->newline() << "stapiu_refresh ("
9810 << "stap_inode_uprobe_targets, "
9811 << "ARRAY_SIZE(stap_inode_uprobe_targets));";
3642eb17
JL
9812}
9813
9814
2b69faaf
JS
9815void
9816uprobe_derived_probe_group::emit_module_inode_exit (systemtap_session& s)
9817{
9818 if (probes.empty()) return;
9819 s.op->newline() << "/* ---- inode uprobes ---- */";
cfcab6c7 9820 s.op->newline() << "stapiu_exit ("
2b69faaf
JS
9821 << "stap_inode_uprobe_targets, "
9822 << "ARRAY_SIZE(stap_inode_uprobe_targets), "
9823 << "stap_inode_uprobe_consumers, "
9824 << "ARRAY_SIZE(stap_inode_uprobe_consumers));";
9825}
9826
9827
3a894f7e
JS
9828void
9829uprobe_derived_probe_group::emit_module_dyninst_decls (systemtap_session& s)
9830{
9831 if (probes.empty()) return;
9832 s.op->newline() << "/* ---- dyninst uprobes ---- */";
9833 emit_module_maxuprobes (s);
e00f3fb7 9834 s.op->newline() << "#include \"dyninst/uprobes.h\"";
3a894f7e 9835
f31a77f5
DS
9836 // Let the dynprobe_derived_probe_group handle outputting targets
9837 // and probes. This allows us to merge different types of probes.
9838 s.op->newline() << "static struct stapdu_probe stapdu_probes[];";
9839 for (unsigned i = 0; i < probes.size(); i++)
3a894f7e
JS
9840 {
9841 uprobe_derived_probe *p = probes[i];
e00f3fb7 9842
f31a77f5
DS
9843 dynprobe_add_uprobe(s, p->module, p->addr, p->sdt_semaphore_addr,
9844 (p->has_return ? "STAPDYN_PROBE_FLAG_RETURN" : "0"),
9845 common_probe_init(p));
3a894f7e 9846 }
874d38bf
JS
9847 // loc2c-generated code assumes pt_regs are available, so use this to make
9848 // sure we always have *something* for it to dereference...
f31a77f5 9849 s.op->newline() << "static struct pt_regs stapdu_dummy_uregs;";
e00f3fb7 9850
3a894f7e
JS
9851 // Write the probe handler.
9852 // NB: not static, so dyninst can find it
9853 s.op->newline() << "int enter_dyninst_uprobe "
9854 << "(uint64_t index, struct pt_regs *regs) {";
e00f3fb7 9855 s.op->newline(1) << "struct stapdu_probe *sup = &stapdu_probes[index];";
92b97104
JS
9856
9857 // Since we're sharing the entry function, we have to dynamically choose the probe_type
9858 string probe_type = "((sup->flags & STAPDYN_PROBE_FLAG_RETURN) ?"
9859 " stp_probe_type_uretprobe : stp_probe_type_uprobe)";
71db462b 9860 common_probe_entryfn_prologue (s, "STAP_SESSION_RUNNING", "sup->probe",
92b97104
JS
9861 probe_type);
9862
874d38bf 9863 s.op->newline() << "c->uregs = regs ?: &stapdu_dummy_uregs;";
e04b5d74 9864 s.op->newline() << "c->user_mode_p = 1;";
3a894f7e
JS
9865 // XXX: once we have regs, check how dyninst sets the IP
9866 // XXX: the way that dyninst rewrites stuff is probably going to be
9867 // ... very confusing to our backtracer (at least if we stay in process)
9868 s.op->newline() << "(*sup->probe->ph) (c);";
ef1337ee 9869 common_probe_entryfn_epilogue (s, true, otf_safe_context(s));
3a894f7e
JS
9870 s.op->newline() << "return 0;";
9871 s.op->newline(-1) << "}";
3debb935 9872 s.op->newline() << "#include \"dyninst/uprobes-regs.c\"";
3a894f7e
JS
9873 s.op->assert_0_indent();
9874}
9875
9876
9877void
9878uprobe_derived_probe_group::emit_module_dyninst_init (systemtap_session& s)
9879{
9880 if (probes.empty()) return;
9881
9882 /* stapdyn handles the dirty work via dyninst */
9883 s.op->newline() << "/* ---- dyninst uprobes ---- */";
9884 s.op->newline() << "/* this section left intentionally blank */";
9885}
9886
9887
9888void
9889uprobe_derived_probe_group::emit_module_dyninst_exit (systemtap_session& s)
9890{
9891 if (probes.empty()) return;
9892
9893 /* stapdyn handles the dirty work via dyninst */
9894 s.op->newline() << "/* ---- dyninst uprobes ---- */";
9895 s.op->newline() << "/* this section left intentionally blank */";
9896}
9897
9898
2b69faaf
JS
9899void
9900uprobe_derived_probe_group::emit_module_decls (systemtap_session& s)
9901{
285ca427
DS
9902 // Here we need to figure out the max number of perf counters used
9903 // per probe.
9904 for (unsigned i=0; i<probes.size(); i++)
9905 {
9906 uprobe_derived_probe *p = probes[i];
9907 if (max_perf_counters < p->perf_counter_refs.size())
9908 max_perf_counters = p->perf_counter_refs.size();
9909 }
9910
ac3af990 9911 if (s.runtime_usermode_p())
4441e344
JS
9912 emit_module_dyninst_decls (s);
9913 else if (kernel_supports_inode_uprobes (s))
2b69faaf
JS
9914 emit_module_inode_decls (s);
9915 else
9916 emit_module_utrace_decls (s);
9917}
9918
9919
9920void
9921uprobe_derived_probe_group::emit_module_init (systemtap_session& s)
9922{
ac3af990 9923 if (s.runtime_usermode_p())
4441e344
JS
9924 emit_module_dyninst_init (s);
9925 else if (kernel_supports_inode_uprobes (s))
2b69faaf
JS
9926 emit_module_inode_init (s);
9927 else
9928 emit_module_utrace_init (s);
9929}
9930
9931
3642eb17
JL
9932void
9933uprobe_derived_probe_group::emit_module_refresh (systemtap_session& s)
9934{
9935 if (!s.runtime_usermode_p() && kernel_supports_inode_uprobes (s))
9936 emit_module_inode_refresh (s);
9937}
9938
9939
2b69faaf
JS
9940void
9941uprobe_derived_probe_group::emit_module_exit (systemtap_session& s)
9942{
ac3af990 9943 if (s.runtime_usermode_p())
4441e344
JS
9944 emit_module_dyninst_exit (s);
9945 else if (kernel_supports_inode_uprobes (s))
2b69faaf
JS
9946 emit_module_inode_exit (s);
9947 else
9948 emit_module_utrace_exit (s);
9949}
9950
3530e7a7 9951bool
fcdd71ba
WC
9952sort_for_bpf(systemtap_session& s __attribute__ ((unused)),
9953 uprobe_derived_probe_group *upg, sort_for_bpf_probe_arg_vector &v)
3530e7a7
AM
9954{
9955 if (!upg)
9956 return false;
9957
9958 for (auto i = upg->probes.begin(); i != upg->probes.end(); ++i)
9959 {
9960 uprobe_derived_probe *p = *i;
9961
9962 if (p->module.empty())
9963 throw SEMANTIC_ERROR(_("binary path required for BPF runtime"), p->tok);
9964
9965 if (p->has_library)
9966 throw SEMANTIC_ERROR(_("probe not compatible with BPF runtime"), p->tok);
9967
9968 std::stringstream o;
9969
9970 // format of section name: uprobe/<type>/<pid>/<offset><binary path>
9971 o << "uprobe/"
9972 << (p->has_return ? "r" : "p") << "/"
9973 << p->pid << "/"
9974 << p->addr
9975 << p->module;
9976
9977 v.push_back(std::pair<derived_probe *, std::string>(p, o.str()));
9978 }
9979
9980 return true;
9981}
2b69faaf 9982
e6fe60e7 9983// ------------------------------------------------------------------------
a7fbce1f 9984// Dwarfless kprobe derived probes
e6fe60e7
AM
9985// ------------------------------------------------------------------------
9986
4627ed58 9987static const string TOK_KPROBE("kprobe");
935447c8 9988
a7fbce1f 9989struct kprobe_derived_probe: public generic_kprobe_derived_probe
d0ea46ce 9990{
23dc94f6
DS
9991 kprobe_derived_probe (systemtap_session& sess,
9992 vector<derived_probe *> & results,
9993 probe *base,
bae55db9 9994 probe_point *location,
a7fbce1f 9995 interned_string module,
45a63356 9996 interned_string name,
bae55db9 9997 int64_t stmt_addr,
e6afb07b 9998 bool has_call,
bae55db9
JS
9999 bool has_return,
10000 bool has_statement,
10001 bool has_maxactive,
b642c901
SC
10002 bool has_path,
10003 bool has_library,
b3fbdd3e 10004 int64_t maxactive_val,
b642c901
SC
10005 const string& path,
10006 const string& library
bae55db9 10007 );
e6afb07b 10008 bool has_call;
bae55db9 10009 bool has_statement;
b642c901
SC
10010 bool has_path;
10011 bool has_library;
b642c901
SC
10012 string path;
10013 string library;
bae55db9 10014 bool access_var;
da1fa82f 10015 void printsig (std::ostream &o, bool nest=true) const;
bae55db9
JS
10016 void join_group (systemtap_session& s);
10017};
d0ea46ce 10018
23dc94f6
DS
10019struct kprobe_var_expanding_visitor: public var_expanding_visitor
10020{
23dc94f6
DS
10021 block *add_block;
10022 block *add_call_probe; // synthesized from .return probes with saved $vars
10023 bool add_block_tid, add_call_probe_tid;
bd5b25e1 10024 bool has_return;
23dc94f6 10025
bd5b25e1 10026 kprobe_var_expanding_visitor(systemtap_session& sess, bool has_return):
16aa72d9 10027 var_expanding_visitor(sess), add_block(NULL), add_call_probe(NULL),
bd5b25e1
JS
10028 add_block_tid(false), add_call_probe_tid(false),
10029 has_return(has_return) {}
23dc94f6
DS
10030
10031 void visit_entry_op (entry_op* e);
10032};
10033
10034
10035kprobe_derived_probe::kprobe_derived_probe (systemtap_session& sess,
10036 vector<derived_probe *> & results,
10037 probe *base,
e6fe60e7 10038 probe_point *location,
a7fbce1f 10039 interned_string module,
45a63356 10040 interned_string name,
e6fe60e7 10041 int64_t stmt_addr,
e6afb07b 10042 bool has_call,
b6371390
JS
10043 bool has_return,
10044 bool has_statement,
10045 bool has_maxactive,
b642c901
SC
10046 bool has_path,
10047 bool has_library,
b3fbdd3e 10048 int64_t maxactive_val,
b642c901
SC
10049 const string& path,
10050 const string& library
b6371390 10051 ):
0dbac951 10052 generic_kprobe_derived_probe (base, location,
a7fbce1f
DS
10053 module, "" /* FIXME: * section */,
10054 stmt_addr, has_return,
10055 has_maxactive, maxactive_val,
10056 name),
10057 has_call (has_call), has_statement (has_statement),
10058 has_path (has_path), has_library (has_library),
b642c901 10059 path (path), library (library)
e6fe60e7
AM
10060{
10061 this->tok = base->tok;
10062 this->access_var = false;
d0ea46ce 10063
e6fe60e7
AM
10064#ifndef USHRT_MAX
10065#define USHRT_MAX 32767
10066#endif
d0ea46ce 10067
46856d8d
JS
10068 // Expansion of $target variables in the probe body produces an error during
10069 // translate phase, since we're not using debuginfo
d0ea46ce 10070
e6fe60e7 10071 vector<probe_point::component*> comps;
46856d8d 10072 comps.push_back (new probe_point::component(TOK_KPROBE));
e6fe60e7 10073
46856d8d
JS
10074 if (has_statement)
10075 {
9ea68eb9
JS
10076 comps.push_back (new probe_point::component(TOK_STATEMENT,
10077 new literal_number(addr, true)));
46856d8d
JS
10078 comps.push_back (new probe_point::component(TOK_ABSOLUTE));
10079 }
10080 else
10081 {
10082 size_t pos = name.find(':');
10083 if (pos != string::npos)
d0ea46ce 10084 {
45a63356
FCE
10085 interned_string module = name.substr(0, pos);
10086 interned_string function = name.substr(pos + 1);
46856d8d
JS
10087 comps.push_back (new probe_point::component(TOK_MODULE, new literal_string(module)));
10088 comps.push_back (new probe_point::component(TOK_FUNCTION, new literal_string(function)));
10089 }
10090 else
10091 comps.push_back (new probe_point::component(TOK_FUNCTION, new literal_string(name)));
46856d8d 10092 }
d0ea46ce 10093
e6afb07b
JL
10094 if (has_call)
10095 comps.push_back (new probe_point::component(TOK_CALL));
b6371390
JS
10096 if (has_return)
10097 comps.push_back (new probe_point::component(TOK_RETURN));
10098 if (has_maxactive)
10099 comps.push_back (new probe_point::component(TOK_MAXACTIVE, new literal_number(maxactive_val)));
d0ea46ce 10100
bd5b25e1 10101 kprobe_var_expanding_visitor v (sess, has_return);
16aa72d9 10102 var_expand_const_fold_loop (sess, this->body, v);
23dc94f6
DS
10103
10104 // If during target-variable-expanding the probe, we added a new block
10105 // of code, add it to the start of the probe.
10106 if (v.add_block)
10107 this->body = new block(v.add_block, this->body);
10108
10109 // If when target-variable-expanding the probe, we need to
10110 // synthesize a sibling function-entry probe. We don't go through
10111 // the whole probe derivation business (PR10642) that could lead to
10112 // wildcard/alias resolution, or for that dwarf-induced duplication.
a7fbce1f
DS
10113 //
10114 // XXX: The dwarf_kprobe_derived_probe class has a different method
10115 // to handle these synthesized probes. It might be possible to use
10116 // the same method.
23dc94f6
DS
10117 if (v.add_call_probe)
10118 {
10119 assert (has_return);
10120
10121 // We temporarily replace base.
10122 statement* old_body = base->body;
10123 base->body = v.add_call_probe;
10124
10125 derived_probe *entry_handler
a7fbce1f
DS
10126 = new kprobe_derived_probe (sess, results, base, location,
10127 module, name, 0, true /* has_call */,
10128 false /* has_return */,
e6afb07b
JL
10129 has_statement, has_maxactive, has_path,
10130 has_library, maxactive_val, path, library);
4f4f173a
SP
10131
10132 entry_handler->synthetic = true;
23dc94f6
DS
10133 results.push_back (entry_handler);
10134
10135 base->body = old_body;
10136 }
10137
e6fe60e7
AM
10138 this->sole_location()->components = comps;
10139}
d0ea46ce 10140
da1fa82f 10141void kprobe_derived_probe::printsig (ostream& o, bool nest) const
e6fe60e7
AM
10142{
10143 sole_location()->print (o);
10144 o << " /* " << " name = " << symbol_name << "*/";
da1fa82f
SP
10145
10146 if (nest)
10147 printsig_nested (o);
e6fe60e7 10148}
d0ea46ce 10149
e6fe60e7
AM
10150void kprobe_derived_probe::join_group (systemtap_session& s)
10151{
a7fbce1f
DS
10152 if (! s.generic_kprobe_derived_probes)
10153 s.generic_kprobe_derived_probes = new generic_kprobe_derived_probe_group ();
10154 s.generic_kprobe_derived_probes->enroll (this);
10155 this->group = s.generic_kprobe_derived_probes;
f8a968bc
JS
10156}
10157
e6fe60e7 10158struct kprobe_builder: public derived_probe_builder
3c1b3d06 10159{
9fdf787d 10160public:
2a639817 10161 kprobe_builder() {}
9fdf787d 10162
dabd71bb 10163 void build_no_more (systemtap_session &) {}
9fdf787d 10164
e6fe60e7
AM
10165 virtual void build(systemtap_session & sess,
10166 probe * base,
10167 probe_point * location,
10168 literal_map_t const & parameters,
10169 vector<derived_probe *> & finished_results);
352c84fe 10170 virtual string name() { return "kprobe builder"; }
e6fe60e7 10171};
3c1b3d06
FCE
10172
10173
37a38927
JS
10174string
10175suggest_kernel_functions(const systemtap_session& session, interned_string function)
10176{
10177 const set<interned_string>& kernel_functions = session.kernel_functions;
323a662a 10178 if (function.empty() || kernel_functions.empty() || session.suppress_costly_diagnostics)
37a38927
JS
10179 return "";
10180
10181 // PR18577: There isn't any point in generating a suggestion list if
10182 // we're not going to display it.
10183 if ((session.dump_mode == systemtap_session::dump_matched_probes
10184 || session.dump_mode == systemtap_session::dump_matched_probes_vars)
10185 && session.verbose < 2)
10186 return "";
10187
10188 if (session.verbose > 2)
10189 clog << "suggesting " << kernel_functions.size() << " kernel functions" << endl;
10190
10191 return levenshtein_suggest(function, kernel_functions, 5); // print top 5 only
10192}
10193
79189b84 10194void
05fb3e0c 10195kprobe_builder::build(systemtap_session & sess,
e6fe60e7
AM
10196 probe * base,
10197 probe_point * location,
10198 literal_map_t const & parameters,
10199 vector<derived_probe *> & finished_results)
79189b84 10200{
45a63356
FCE
10201 interned_string function_string_val, module_string_val;
10202 interned_string path, library, path_tgt, library_tgt;
b6371390
JS
10203 int64_t statement_num_val = 0, maxactive_val = 0;
10204 bool has_function_str, has_module_str, has_statement_num;
e6afb07b 10205 bool has_absolute, has_call, has_return, has_maxactive;
b642c901 10206 bool has_path, has_library;
79189b84 10207
b6371390
JS
10208 has_function_str = get_param(parameters, TOK_FUNCTION, function_string_val);
10209 has_module_str = get_param(parameters, TOK_MODULE, module_string_val);
e6afb07b 10210 has_call = has_null_param (parameters, TOK_CALL);
b6371390
JS
10211 has_return = has_null_param (parameters, TOK_RETURN);
10212 has_maxactive = get_param(parameters, TOK_MAXACTIVE, maxactive_val);
10213 has_statement_num = get_param(parameters, TOK_STATEMENT, statement_num_val);
10214 has_absolute = has_null_param (parameters, TOK_ABSOLUTE);
b642c901
SC
10215 has_path = get_param (parameters, TOK_PROCESS, path);
10216 has_library = get_param (parameters, TOK_LIBRARY, library);
10217
51f0b234
DS
10218 if (has_module_str)
10219 {
10220 // The TOK_MODULE value can be a module name, relative path to a
10221 // module filename, or an absolute path to a module
10222 // filename. Handle all those details.
10223 handle_module_token(sess, module_string_val);
10224
10225 // If we've got a fullpath to the kernel module, then get the
10226 // simple name.
10227 if (module_string_val[0] == '/')
10228 module_string_val = modname_from_path(module_string_val);
10229 }
b642c901 10230 if (has_path)
05fb3e0c
WF
10231 {
10232 path = find_executable (path, sess.sysroot, sess.sysenv);
10233 path_tgt = path_remove_sysroot(sess, path);
10234 }
b642c901 10235 if (has_library)
05fb3e0c
WF
10236 {
10237 library = find_executable (library, sess.sysroot, sess.sysenv,
10238 "LD_LIBRARY_PATH");
10239 library_tgt = path_remove_sysroot(sess, library);
10240 }
c57ea854 10241
b6371390 10242 if (has_function_str)
6fb70fb7 10243 {
2a639817 10244 if (has_module_str)
9fdf787d 10245 {
45a63356 10246 function_string_val = (string)module_string_val + ":" + (string)function_string_val;
9fdf787d 10247 derived_probe *dp
23dc94f6 10248 = new kprobe_derived_probe (sess, finished_results, base,
a7fbce1f
DS
10249 location, module_string_val,
10250 function_string_val,
e6afb07b
JL
10251 0, has_call, has_return,
10252 has_statement_num, has_maxactive,
10253 has_path, has_library, maxactive_val,
10254 path_tgt, library_tgt);
9fdf787d
DS
10255 finished_results.push_back (dp);
10256 }
10257 else
10258 {
47d349b1 10259 vector<interned_string> matches;
2a639817
JS
10260
10261 // Simple names can be found directly
a81fb5d4 10262 if (function_string_val.find_first_of("*?[{") == string::npos)
2a639817
JS
10263 {
10264 if (sess.kernel_functions.count(function_string_val))
10265 matches.push_back(function_string_val);
10266 }
10267 else // Search function name list for matching names
10268 {
a81fb5d4 10269 const string& val = csh_to_ksh(function_string_val);
b4662f6b
JS
10270 for (auto it = sess.kernel_functions.cbegin();
10271 it != sess.kernel_functions.cend(); it++)
a3e980f9
FCE
10272 {
10273 // fnmatch returns zero for matching.
a81fb5d4 10274 if (fnmatch(val.c_str(), it->to_string().c_str(), FNM_EXTMATCH) == 0)
a3e980f9
FCE
10275 matches.push_back(*it);
10276 }
2a639817 10277 }
86758d5f 10278
37a38927
JS
10279 if (matches.empty())
10280 {
10281 string sugs = suggest_kernel_functions(sess, function_string_val);
10282 if (!sugs.empty())
10283 throw SEMANTIC_ERROR (_NF("no match (similar function: %s)",
10284 "no match (similar functions: %s)",
10285 sugs.find(',') == string::npos,
10286 sugs.c_str()));
10287 }
10288
b4662f6b 10289 for (auto it = matches.cbegin(); it != matches.cend(); it++)
9fdf787d 10290 {
2a639817
JS
10291 derived_probe *dp
10292 = new kprobe_derived_probe (sess, finished_results, base,
a7fbce1f 10293 location, "", *it, 0, has_call,
e6afb07b 10294 has_return, has_statement_num,
2a639817
JS
10295 has_maxactive, has_path,
10296 has_library, maxactive_val,
10297 path_tgt, library_tgt);
10298 finished_results.push_back (dp);
9fdf787d
DS
10299 }
10300 }
6fb70fb7 10301 }
e6fe60e7 10302 else
b6371390
JS
10303 {
10304 // assert guru mode for absolute probes
10305 if ( has_statement_num && has_absolute && !base->privileged )
dc09353a 10306 throw SEMANTIC_ERROR (_("absolute statement probe in unprivileged script; need stap -g"), base->tok);
b6371390 10307
23dc94f6
DS
10308 finished_results.push_back (new kprobe_derived_probe (sess,
10309 finished_results,
10310 base,
a7fbce1f
DS
10311 location,
10312 module_string_val,
10313 "",
b6371390 10314 statement_num_val,
e6afb07b 10315 has_call,
b6371390
JS
10316 has_return,
10317 has_statement_num,
10318 has_maxactive,
b642c901
SC
10319 has_path,
10320 has_library,
10321 maxactive_val,
05fb3e0c
WF
10322 path_tgt,
10323 library_tgt));
96b030fe 10324 }
79189b84
JS
10325}
10326
23dc94f6
DS
10327
10328void
10329kprobe_var_expanding_visitor::visit_entry_op (entry_op *e)
10330{
10331 expression *repl = e;
10332
bd5b25e1
JS
10333 if (has_return)
10334 {
05d8da72 10335 // see also PR20416
bd5b25e1
JS
10336 // XXX it would be nice to use gen_kretprobe_saved_return when
10337 // available, but it requires knowing the types already, which is
10338 // problematic for arbitrary expressons.
10339 repl = gen_mapped_saved_return (sess, e->operand, "entry",
10340 add_block, add_block_tid,
10341 add_call_probe, add_call_probe_tid);
10342 }
23dc94f6
DS
10343 provide (repl);
10344}
10345
10346
dd225250
PS
10347// ------------------------------------------------------------------------
10348// Hardware breakpoint based probes.
10349// ------------------------------------------------------------------------
10350
10351static const string TOK_HWBKPT("data");
10352static const string TOK_HWBKPT_WRITE("write");
10353static const string TOK_HWBKPT_RW("rw");
10354static const string TOK_LENGTH("length");
10355
10356#define HWBKPT_READ 0
10357#define HWBKPT_WRITE 1
10358#define HWBKPT_RW 2
10359struct hwbkpt_derived_probe: public derived_probe
10360{
10361 hwbkpt_derived_probe (probe *base,
10362 probe_point *location,
10363 uint64_t addr,
10364 string symname,
10365 unsigned int len,
10366 bool has_only_read_access,
10367 bool has_only_write_access,
10368 bool has_rw_access
10369 );
10370 Dwarf_Addr hwbkpt_addr;
10371 string symbol_name;
10372 unsigned int hwbkpt_access,hwbkpt_len;
10373
da1fa82f 10374 void printsig (std::ostream &o, bool nest) const;
dd225250
PS
10375 void join_group (systemtap_session& s);
10376};
10377
10378struct hwbkpt_derived_probe_group: public derived_probe_group
10379{
dd225250 10380private:
dac77b80 10381 vector<hwbkpt_derived_probe*> hwbkpt_probes;
dd225250
PS
10382
10383public:
10384 void enroll (hwbkpt_derived_probe* probe, systemtap_session& s);
10385 void emit_module_decls (systemtap_session& s);
10386 void emit_module_init (systemtap_session& s);
10387 void emit_module_exit (systemtap_session& s);
10388};
10389
10390hwbkpt_derived_probe::hwbkpt_derived_probe (probe *base,
9ea68eb9
JS
10391 probe_point *location,
10392 uint64_t addr,
10393 string symname,
10394 unsigned int len,
10395 bool has_only_read_access,
10396 bool has_only_write_access,
822a6a3d 10397 bool):
4c5d1300 10398 derived_probe (base, location, true /* .components soon rewritten */ ),
dd225250
PS
10399 hwbkpt_addr (addr),
10400 symbol_name (symname),
10401 hwbkpt_len (len)
10402{
10403 this->tok = base->tok;
10404
10405 vector<probe_point::component*> comps;
10406 comps.push_back (new probe_point::component(TOK_KERNEL));
10407
10408 if (hwbkpt_addr)
9ea68eb9
JS
10409 comps.push_back (new probe_point::component (TOK_HWBKPT,
10410 new literal_number(hwbkpt_addr, true)));
10411 else if (symbol_name.size())
10412 comps.push_back (new probe_point::component (TOK_HWBKPT, new literal_string(symbol_name)));
dd225250
PS
10413
10414 comps.push_back (new probe_point::component (TOK_LENGTH, new literal_number(hwbkpt_len)));
10415
10416 if (has_only_read_access)
9ea68eb9 10417 this->hwbkpt_access = HWBKPT_READ ;
dd225250
PS
10418//TODO add code for comps.push_back for read, since this flag is not for x86
10419
10420 else
9ea68eb9
JS
10421 {
10422 if (has_only_write_access)
10423 {
10424 this->hwbkpt_access = HWBKPT_WRITE ;
10425 comps.push_back (new probe_point::component(TOK_HWBKPT_WRITE));
10426 }
10427 else
10428 {
10429 this->hwbkpt_access = HWBKPT_RW ;
10430 comps.push_back (new probe_point::component(TOK_HWBKPT_RW));
10431 }
10432 }
dd225250
PS
10433
10434 this->sole_location()->components = comps;
10435}
10436
da1fa82f 10437void hwbkpt_derived_probe::printsig (ostream& o, bool nest) const
dd225250
PS
10438{
10439 sole_location()->print (o);
da1fa82f
SP
10440
10441 if (nest)
10442 printsig_nested (o);
dd225250
PS
10443}
10444
10445void hwbkpt_derived_probe::join_group (systemtap_session& s)
10446{
dac77b80
FCE
10447 if (! s.hwbkpt_derived_probes)
10448 s.hwbkpt_derived_probes = new hwbkpt_derived_probe_group ();
dd225250 10449 s.hwbkpt_derived_probes->enroll (this, s);
ca6d3b0f 10450 this->group = s.hwbkpt_derived_probes;
dd225250
PS
10451}
10452
5177a6c8 10453void hwbkpt_derived_probe_group::enroll (hwbkpt_derived_probe* p, systemtap_session&)
dd225250 10454{
dac77b80 10455 hwbkpt_probes.push_back (p);
dd225250
PS
10456}
10457
10458void
10459hwbkpt_derived_probe_group::emit_module_decls (systemtap_session& s)
10460{
dac77b80 10461 if (hwbkpt_probes.empty()) return;
dd225250
PS
10462
10463 s.op->newline() << "/* ---- hwbkpt-based probes ---- */";
10464
10465 s.op->newline() << "#include <linux/perf_event.h>";
10466 s.op->newline() << "#include <linux/hw_breakpoint.h>";
10467 s.op->newline();
10468
10469 // Forward declare the master entry functions
23063de1 10470 s.op->newline() << "#ifdef STAPCONF_PERF_HANDLER_NMI";
d0b322ed 10471 s.op->newline() << "static void enter_hwbkpt_probe (struct perf_event *bp,";
dd225250
PS
10472 s.op->line() << " int nmi,";
10473 s.op->line() << " struct perf_sample_data *data,";
10474 s.op->line() << " struct pt_regs *regs);";
23063de1 10475 s.op->newline() << "#else";
d0b322ed 10476 s.op->newline() << "static void enter_hwbkpt_probe (struct perf_event *bp,";
23063de1
FCE
10477 s.op->line() << " struct perf_sample_data *data,";
10478 s.op->line() << " struct pt_regs *regs);";
10479 s.op->newline() << "#endif";
79189b84 10480
dd225250
PS
10481 // Emit the actual probe list.
10482
10483 s.op->newline() << "static struct perf_event_attr ";
dac77b80 10484 s.op->newline() << "stap_hwbkpt_probe_array[" << hwbkpt_probes.size() << "];";
dd225250
PS
10485
10486 s.op->newline() << "static struct perf_event **";
dac77b80 10487 s.op->newline() << "stap_hwbkpt_ret_array[" << hwbkpt_probes.size() << "];";
dd225250
PS
10488 s.op->newline() << "static struct stap_hwbkpt_probe {";
10489 s.op->newline() << "int registered_p:1;";
43650b10 10490// registered_p = 0 signifies a probe that is unregistered (or failed)
dd225250
PS
10491// registered_p = 1 signifies a probe that got registered successfully
10492
faea5e16 10493 // Symbol Names are mostly small and uniform enough
dd225250 10494 // to justify putting const char*.
dac77b80 10495 s.op->newline() << "const char * const symbol;";
dd225250
PS
10496
10497 s.op->newline() << "const unsigned long address;";
10498 s.op->newline() << "uint8_t atype;";
bb0a4e12 10499 s.op->newline() << "unsigned int len;";
7c3e97f4 10500 s.op->newline() << "const struct stap_probe * const probe;";
dd225250
PS
10501 s.op->newline() << "} stap_hwbkpt_probes[] = {";
10502 s.op->indent(1);
10503
dac77b80 10504 for (unsigned int it = 0; it < hwbkpt_probes.size(); it++)
dd225250 10505 {
dac77b80 10506 hwbkpt_derived_probe* p = hwbkpt_probes.at(it);
dd225250 10507 s.op->newline() << "{";
dd225250
PS
10508 if (p->symbol_name.size())
10509 s.op->line() << " .address=(unsigned long)0x0" << "ULL,";
10510 else
10511 s.op->line() << " .address=(unsigned long)0x" << hex << p->hwbkpt_addr << dec << "ULL,";
10512 switch(p->hwbkpt_access){
10513 case HWBKPT_READ:
10514 s.op->line() << " .atype=HW_BREAKPOINT_R ,";
bb0a4e12 10515 break;
dd225250
PS
10516 case HWBKPT_WRITE:
10517 s.op->line() << " .atype=HW_BREAKPOINT_W ,";
bb0a4e12 10518 break;
dd225250
PS
10519 case HWBKPT_RW:
10520 s.op->line() << " .atype=HW_BREAKPOINT_R|HW_BREAKPOINT_W ,";
bb0a4e12 10521 break;
dd225250
PS
10522 };
10523 s.op->line() << " .len=" << p->hwbkpt_len << ",";
faea5e16 10524 s.op->line() << " .probe=" << common_probe_init (p) << ",";
dd225250 10525 s.op->line() << " .symbol=\"" << p->symbol_name << "\",";
dd225250
PS
10526 s.op->line() << " },";
10527 }
dac77b80 10528 s.op->newline(-1) << "};";
dd225250
PS
10529
10530 // Emit the hwbkpt callback function
10531 s.op->newline() ;
23063de1 10532 s.op->newline() << "#ifdef STAPCONF_PERF_HANDLER_NMI";
d0b322ed 10533 s.op->newline() << "static void enter_hwbkpt_probe (struct perf_event *bp,";
dd225250
PS
10534 s.op->line() << " int nmi,";
10535 s.op->line() << " struct perf_sample_data *data,";
10536 s.op->line() << " struct pt_regs *regs) {";
23063de1 10537 s.op->newline() << "#else";
d0b322ed 10538 s.op->newline() << "static void enter_hwbkpt_probe (struct perf_event *bp,";
23063de1
FCE
10539 s.op->line() << " struct perf_sample_data *data,";
10540 s.op->line() << " struct pt_regs *regs) {";
10541 s.op->newline() << "#endif";
dac77b80 10542 s.op->newline(1) << "unsigned int i;";
d0b322ed 10543 s.op->newline() << "if (bp->attr.type != PERF_TYPE_BREAKPOINT) return;";
dac77b80
FCE
10544 s.op->newline() << "for (i=0; i<" << hwbkpt_probes.size() << "; i++) {";
10545 s.op->newline(1) << "struct perf_event_attr *hp = & stap_hwbkpt_probe_array[i];";
10546 // XXX: why not match stap_hwbkpt_ret_array[i] against bp instead?
10547 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) {";
4210989f
DS
10548 s.op->newline(1) << "struct stap_hwbkpt_probe *skp = &stap_hwbkpt_probes[i];";
10549 common_probe_entryfn_prologue (s, "STAP_SESSION_RUNNING", "skp->probe",
cda141c2 10550 "stp_probe_type_hwbkpt");
d9aed31e 10551 s.op->newline() << "if (user_mode(regs)) {";
e04b5d74 10552 s.op->newline(1)<< "c->user_mode_p = 1;";
d9aed31e
MW
10553 s.op->newline() << "c->uregs = regs;";
10554 s.op->newline(-1) << "} else {";
10555 s.op->newline(1) << "c->kregs = regs;";
10556 s.op->newline(-1) << "}";
4210989f 10557 s.op->newline() << "(*skp->probe->ph) (c);";
ef1337ee 10558 common_probe_entryfn_epilogue (s, true, otf_safe_context(s));
dac77b80 10559 s.op->newline(-1) << "}";
dd225250 10560 s.op->newline(-1) << "}";
d0b322ed 10561 s.op->newline() << "return;";
dac77b80 10562 s.op->newline(-1) << "}";
dd225250
PS
10563}
10564
10565void
10566hwbkpt_derived_probe_group::emit_module_init (systemtap_session& s)
10567{
dac77b80 10568 s.op->newline() << "for (i=0; i<" << hwbkpt_probes.size() << "; i++) {";
4210989f 10569 s.op->newline(1) << "struct stap_hwbkpt_probe *skp = & stap_hwbkpt_probes[i];";
dd225250 10570 s.op->newline() << "struct perf_event_attr *hp = & stap_hwbkpt_probe_array[i];";
4210989f
DS
10571 s.op->newline() << "void *addr = (void *) skp->address;";
10572 s.op->newline() << "const char *hwbkpt_symbol_name = addr ? NULL : skp->symbol;";
dac77b80
FCE
10573 s.op->newline() << "hw_breakpoint_init(hp);";
10574 s.op->newline() << "if (addr)";
10575 s.op->newline(1) << "hp->bp_addr = (unsigned long) addr;";
10576 s.op->newline(-1) << "else { ";
10577 s.op->newline(1) << "hp->bp_addr = kallsyms_lookup_name(hwbkpt_symbol_name);";
10578 s.op->newline() << "if (!hp->bp_addr) { ";
4210989f 10579 s.op->newline(1) << "_stp_warn(\"Probe %s registration skipped: invalid symbol %s \",skp->probe->pp,hwbkpt_symbol_name);";
dac77b80
FCE
10580 s.op->newline() << "continue;";
10581 s.op->newline(-1) << "}";
10582 s.op->newline(-1) << "}";
4210989f 10583 s.op->newline() << "hp->bp_type = skp->atype;";
dac77b80 10584
5a540e9f
DS
10585 // Convert actual len to bp len.
10586 s.op->newline() << "switch(skp->len) {";
10587 s.op->newline() << "case 1:";
10588 s.op->newline(1) << "hp->bp_len = HW_BREAKPOINT_LEN_1;";
10589 s.op->newline() << "break;";
10590 s.op->newline(-1) << "case 2:";
10591 s.op->newline(1) << "hp->bp_len = HW_BREAKPOINT_LEN_2;";
10592 s.op->newline() << "break;";
10593 s.op->newline(-1) << "case 3:";
10594 s.op->newline() << "case 4:";
10595 s.op->newline(1) << "hp->bp_len = HW_BREAKPOINT_LEN_4;";
10596 s.op->newline() << "break;";
10597 s.op->newline(-1) << "case 5:";
10598 s.op->newline() << "case 6:";
10599 s.op->newline() << "case 7:";
10600 s.op->newline() << "case 8:";
10601 s.op->newline() << "default:"; // XXX: could instead reject
10602 s.op->newline(1) << "hp->bp_len = HW_BREAKPOINT_LEN_8;";
10603 s.op->newline() << "break;";
10604 s.op->newline(-1) << "}";
dac77b80 10605
4210989f 10606 s.op->newline() << "probe_point = skp->probe->pp;"; // for error messages
b273669e 10607 s.op->newline() << "#ifdef STAPCONF_HW_BREAKPOINT_CONTEXT";
d0b322ed 10608 s.op->newline() << "stap_hwbkpt_ret_array[i] = register_wide_hw_breakpoint(hp, &enter_hwbkpt_probe, NULL);";
b273669e 10609 s.op->newline() << "#else";
d0b322ed 10610 s.op->newline() << "stap_hwbkpt_ret_array[i] = register_wide_hw_breakpoint(hp, &enter_hwbkpt_probe);";
b273669e 10611 s.op->newline() << "#endif";
43650b10 10612 s.op->newline() << "rc = 0;";
dac77b80 10613 s.op->newline() << "if (IS_ERR(stap_hwbkpt_ret_array[i])) {";
43650b10
WC
10614 s.op->newline(1) << "rc = PTR_ERR(stap_hwbkpt_ret_array[i]);";
10615 s.op->newline() << "stap_hwbkpt_ret_array[i] = 0;";
10616 s.op->newline(-1) << "}";
217ef1f4
WC
10617 s.op->newline() << "if (rc) {";
10618 s.op->newline(1) << "_stp_warn(\"Hwbkpt probe %s: registration error %d, addr %p, name %s\", probe_point, rc, addr, hwbkpt_symbol_name);";
4210989f 10619 s.op->newline() << "skp->registered_p = 0;";
dac77b80 10620 s.op->newline(-1) << "}";
4210989f 10621 s.op->newline() << " else skp->registered_p = 1;";
dd225250
PS
10622 s.op->newline(-1) << "}"; // for loop
10623}
10624
10625void
10626hwbkpt_derived_probe_group::emit_module_exit (systemtap_session& s)
10627{
10628 //Unregister hwbkpt probes.
dac77b80 10629 s.op->newline() << "for (i=0; i<" << hwbkpt_probes.size() << "; i++) {";
4210989f
DS
10630 s.op->newline(1) << "struct stap_hwbkpt_probe *skp = & stap_hwbkpt_probes[i];";
10631 s.op->newline() << "if (skp->registered_p == 0) continue;";
dac77b80 10632 s.op->newline() << "unregister_wide_hw_breakpoint(stap_hwbkpt_ret_array[i]);";
4210989f 10633 s.op->newline() << "skp->registered_p = 0;";
dd225250
PS
10634 s.op->newline(-1) << "}";
10635}
10636
10637struct hwbkpt_builder: public derived_probe_builder
10638{
10639 hwbkpt_builder() {}
10640 virtual void build(systemtap_session & sess,
10641 probe * base,
10642 probe_point * location,
10643 literal_map_t const & parameters,
10644 vector<derived_probe *> & finished_results);
352c84fe
FL
10645
10646 virtual string name() { return "hwbkpt builder"; }
dd225250
PS
10647};
10648
10649void
10650hwbkpt_builder::build(systemtap_session & sess,
10651 probe * base,
10652 probe_point * location,
10653 literal_map_t const & parameters,
10654 vector<derived_probe *> & finished_results)
10655{
45a63356 10656 interned_string symbol_str_val;
dd225250
PS
10657 int64_t hwbkpt_address, len;
10658 bool has_addr, has_symbol_str, has_write, has_rw, has_len;
10659
b47f3a55 10660 if (! (sess.kernel_config["CONFIG_PERF_EVENTS"] == string("y")))
dc09353a 10661 throw SEMANTIC_ERROR (_("CONFIG_PERF_EVENTS not available on this kernel"),
b47f3a55
FCE
10662 location->components[0]->tok);
10663 if (! (sess.kernel_config["CONFIG_HAVE_HW_BREAKPOINT"] == string("y")))
dc09353a 10664 throw SEMANTIC_ERROR (_("CONFIG_HAVE_HW_BREAKPOINT not available on this kernel"),
b47f3a55
FCE
10665 location->components[0]->tok);
10666
26f262a7
DS
10667 // See BZ1431263 (on aarch64, running the hw_watch_addr.stp
10668 // systemtap examples cause a stuck CPU).
10669 if (sess.architecture == string("arm64"))
10670 throw SEMANTIC_ERROR (_("kernel.data probes are not supported on arm64 kernels"),
10671 location->components[0]->tok);
10672
dd225250
PS
10673 has_addr = get_param (parameters, TOK_HWBKPT, hwbkpt_address);
10674 has_symbol_str = get_param (parameters, TOK_HWBKPT, symbol_str_val);
10675 has_len = get_param (parameters, TOK_LENGTH, len);
10676 has_write = (parameters.find(TOK_HWBKPT_WRITE) != parameters.end());
10677 has_rw = (parameters.find(TOK_HWBKPT_RW) != parameters.end());
10678
b31695eb
JL
10679 // Make an intermediate pp that is well-formed. It's pretty much the same as
10680 // the user-provided one, except that the addr literal is well-typed.
10681 probe_point* well_formed_loc = new probe_point(*location);
10682 well_formed_loc->well_formed = true;
10683
10684 vector<probe_point::component*> well_formed_comps;
b4662f6b 10685 for (auto it = location->components.begin();
b31695eb
JL
10686 it != location->components.end(); ++it)
10687 if ((*it)->functor == TOK_HWBKPT && has_addr)
10688 well_formed_comps.push_back(new probe_point::component(TOK_HWBKPT,
10689 new literal_number(hwbkpt_address, true /* hex */ )));
10690 else
10691 well_formed_comps.push_back(*it);
10692 well_formed_loc->components = well_formed_comps;
10693 probe *new_base = new probe (base, well_formed_loc);
10694
dd225250
PS
10695 if (!has_len)
10696 len = 1;
10697
10698 if (has_addr)
b31695eb 10699 finished_results.push_back (new hwbkpt_derived_probe (new_base,
dd225250
PS
10700 location,
10701 hwbkpt_address,
10702 "",len,0,
10703 has_write,
10704 has_rw));
5d8a0aea 10705 else if (has_symbol_str)
b31695eb 10706 finished_results.push_back (new hwbkpt_derived_probe (new_base,
dd225250
PS
10707 location,
10708 0,
10709 symbol_str_val,len,0,
10710 has_write,
10711 has_rw));
5d8a0aea
FCE
10712 else
10713 assert (0);
dd225250 10714}
342d3f96 10715
0a6f5a3f
JS
10716// ------------------------------------------------------------------------
10717// statically inserted kernel-tracepoint derived probes
10718// ------------------------------------------------------------------------
10719
6fb70fb7 10720struct tracepoint_arg
79189b84 10721{
b6e49c97 10722 string name, c_type, c_decl, typecast;
dcaa1a65 10723 bool usable, used, isptr;
f8a968bc 10724 Dwarf_Die type_die;
b6e49c97 10725 tracepoint_arg(const string& tracepoint_name, Dwarf_Die *arg);
16b2359a
AM
10726
10727 // used with --runtime=bpf
10728 int size;
10729 int offset;
10730 bool is_signed;
6fb70fb7 10731};
79189b84 10732
0a6f5a3f
JS
10733struct tracepoint_derived_probe: public derived_probe
10734{
79189b84
JS
10735 tracepoint_derived_probe (systemtap_session& s,
10736 dwflpp& dw, Dwarf_Die& func_die,
968b8188 10737 const string& tracepoint_system,
79189b84
JS
10738 const string& tracepoint_name,
10739 probe* base_probe, probe_point* location);
bc9a523d 10740
79189b84 10741 systemtap_session& sess;
4f58db62 10742 string tracepoint_system, tracepoint_name, header;
6fb70fb7 10743 vector <struct tracepoint_arg> args;
bc9a523d 10744
6fb70fb7 10745 void build_args(dwflpp& dw, Dwarf_Die& func_die);
16b2359a 10746 void build_args_for_bpf(dwflpp& dw, Dwarf_Die& struct_die);
d0bfd2ac 10747 void getargs (std::list<std::string> &arg_set) const;
79189b84 10748 void join_group (systemtap_session& s);
3e3bd7b6 10749 void print_dupe_stamp(ostream& o);
0a6f5a3f 10750};
79189b84
JS
10751
10752
0a6f5a3f 10753struct tracepoint_derived_probe_group: public generic_dpg<tracepoint_derived_probe>
79189b84 10754{
fcdd71ba
WC
10755 friend bool sort_for_bpf(systemtap_session& s,
10756 tracepoint_derived_probe_group *t,
16b2359a
AM
10757 sort_for_bpf_probe_arg_vector &v);
10758
79189b84
JS
10759 void emit_module_decls (systemtap_session& s);
10760 void emit_module_init (systemtap_session& s);
10761 void emit_module_exit (systemtap_session& s);
0a6f5a3f 10762};
79189b84 10763
bc9a523d 10764
f8a968bc
JS
10765struct tracepoint_var_expanding_visitor: public var_expanding_visitor
10766{
70719cbf 10767 tracepoint_var_expanding_visitor(dwflpp& dw,
f8a968bc 10768 vector <struct tracepoint_arg>& args):
16aa72d9 10769 var_expanding_visitor (dw.sess),
70719cbf 10770 dw (dw), args (args) {}
f8a968bc 10771 dwflpp& dw;
f8a968bc 10772 vector <struct tracepoint_arg>& args;
bc9a523d 10773
f8a968bc
JS
10774 void visit_target_symbol (target_symbol* e);
10775 void visit_target_symbol_arg (target_symbol* e);
10776 void visit_target_symbol_context (target_symbol* e);
10777};
79189b84
JS
10778
10779
f8a968bc
JS
10780void
10781tracepoint_var_expanding_visitor::visit_target_symbol_arg (target_symbol* e)
75ead1f7 10782{
cc9001af 10783 string argname = e->sym_name();
47d349b1 10784 string en = e->name;
75ead1f7 10785
f8a968bc
JS
10786 // search for a tracepoint parameter matching this name
10787 tracepoint_arg *arg = NULL;
10788 for (unsigned i = 0; i < args.size(); ++i)
dcaa1a65 10789 if (args[i].usable && args[i].name == argname)
f8a968bc
JS
10790 {
10791 arg = &args[i];
10792 arg->used = true;
10793 break;
10794 }
75ead1f7 10795
f8a968bc
JS
10796 if (arg == NULL)
10797 {
1d0499c2 10798 set<string> vars;
f8a968bc 10799 for (unsigned i = 0; i < args.size(); ++i)
1d0499c2
JL
10800 vars.insert("$" + args[i].name);
10801 vars.insert("$$name");
10802 vars.insert("$$parms");
10803 vars.insert("$$vars");
a3e980f9 10804 string sugs = levenshtein_suggest(en, vars); // no need to limit, there's not that many
75ead1f7 10805
f8a968bc
JS
10806 // We hope that this value ends up not being referenced after all, so it
10807 // can be optimized out quietly.
1d0499c2 10808 throw SEMANTIC_ERROR(_F("unable to find tracepoint variable '%s'%s",
a3e980f9 10809 en.c_str(), sugs.empty() ? "" :
1d0499c2
JL
10810 (_(" (alternatives: ") + sugs + ")").c_str()), e->tok);
10811 // NB: we use 'alternatives' because we list all
f8a968bc
JS
10812 // NB: we can have multiple errors, since a target variable
10813 // may be expanded in several different contexts:
10814 // trace ("*") { $foo->bar }
f8a968bc 10815 }
75ead1f7 10816
37efef73
JS
10817 // make sure we're not dereferencing base types or void
10818 bool deref_p = arg->isptr && !null_die(&arg->type_die);
10819 if (!deref_p)
d19a9a82 10820 e->assert_no_components("tracepoint", true);
75ead1f7 10821
f8a968bc
JS
10822 // we can only write to dereferenced fields, and only if guru mode is on
10823 bool lvalue = is_active_lvalue(e);
10824 if (lvalue && (!dw.sess.guru_mode || e->components.empty()))
a3e980f9 10825 throw SEMANTIC_ERROR(_F("write to tracepoint variable '%s' not permitted; need stap -g", en.c_str()), e->tok);
c69a87e0 10826
ad370dcc
JS
10827 // XXX: if a struct/union arg is passed by value, then writing to its fields
10828 // is also meaningless until you dereference past a pointer member. It's
10829 // harder to detect and prevent that though...
75ead1f7 10830
f8a968bc
JS
10831 if (e->components.empty())
10832 {
03c75a4a 10833 if (e->addressof)
dc09353a 10834 throw SEMANTIC_ERROR(_("cannot take address of tracepoint variable"), e->tok);
a45664f4 10835
3e3bd7b6 10836 // Just grab the value from the probe locals
a45664f4
JS
10837 symbol* sym = new symbol;
10838 sym->tok = e->tok;
10839 sym->name = "__tracepoint_arg_" + arg->name;
c939be69 10840 sym->type_details = make_shared<exp_type_dwarf>(&dw, &arg->type_die, false, false);
66373b2e
AM
10841
10842 if (sess.runtime_mode == systemtap_session::bpf_runtime)
10843 {
10844 bpf_context_vardecl *v = new bpf_context_vardecl;
10845
10846 v->size = arg->size;
10847 v->offset = arg->offset;
10848 v->is_signed = arg->is_signed;
10849 sym->referent = v;
10850 }
10851
a45664f4 10852 provide (sym);
f8a968bc
JS
10853 }
10854 else
10855 {
5f36109e
JS
10856 // make a copy of the original as a bare target symbol for the tracepoint
10857 // value, which will be passed into the dwarf dereferencing code
10858 target_symbol* e2 = deep_copy_visitor::deep_copy(e);
10859 e2->components.clear();
10860
c4965ad9 10861 if (e->check_pretty_print (lvalue))
5f36109e 10862 {
9c141eca
DS
10863 dwarf_pretty_print dpp(dw, &arg->type_die, e2, deref_p, false,
10864 *e, lvalue);
5f36109e
JS
10865 dpp.expand()->visit (this);
10866 return;
10867 }
10868
1c0be8c7 10869 bool userspace_p = false;
0dbac951
RH
10870 location_context ctx(e, e2);
10871 ctx.userspace_p = userspace_p;
7e2cd9f5 10872
16b2359a
AM
10873 if (dw.sess.runtime_mode == systemtap_session::bpf_runtime)
10874 ctx.adapt_pointer_to_bpf(arg->size, arg->offset, arg->is_signed);
10875
3414d598 10876 Dwarf_Die endtype;
5552db3a 10877 dw.literal_stmt_for_pointer (ctx, &arg->type_die, ctx.e, lvalue, &endtype);
3414d598 10878
0dbac951
RH
10879 string fname = (string(lvalue ? "_tracepoint_tvar_set"
10880 : "_tracepoint_tvar_get")
10881 + "_" + e->sym_name()
10882 + "_" + lex_cast(tick++));
3414d598 10883
0dbac951
RH
10884 functioncall* n = synthetic_embedded_deref_call(dw, ctx, fname, &endtype,
10885 userspace_p, lvalue, e2);
75ead1f7 10886
f8a968bc 10887 if (lvalue)
f55efafe 10888 provide_lvalue_call (n);
75ead1f7 10889
8444e5eb 10890 provide(n); // allow recursion to $var1[$var2] subexpressions
f8a968bc 10891 }
75ead1f7
JS
10892}
10893
10894
f8a968bc
JS
10895void
10896tracepoint_var_expanding_visitor::visit_target_symbol_context (target_symbol* e)
0a6f5a3f 10897{
03c75a4a 10898 if (e->addressof)
dc09353a 10899 throw SEMANTIC_ERROR(_("cannot take address of context variable"), e->tok);
03c75a4a 10900
f8a968bc 10901 if (is_active_lvalue (e))
7371cd19
JS
10902 throw SEMANTIC_ERROR(_F("write to tracepoint '%s' not permitted",
10903 e->name.to_string().c_str()), e->tok);
0a6f5a3f 10904
4f58db62 10905 if (e->name == "$$name" || e->name == "$$system")
f8a968bc 10906 {
5f36109e
JS
10907 e->assert_no_components("tracepoint");
10908
4f58db62
JL
10909 string member = (e->name == "$$name") ? "c->ips.tp.tracepoint_name"
10910 : "c->ips.tp.tracepoint_system";
10911
bfdaad1e
DS
10912 // Synthesize an embedded expression.
10913 embedded_expr *expr = new embedded_expr;
10914 expr->tok = e->tok;
4f58db62
JL
10915 expr->code = string("/* string */ /* pure */ " +
10916 member + " ? " + member + " : \"\"");
bfdaad1e 10917 provide (expr);
f8a968bc 10918 }
277c21bc 10919 else if (e->name == "$$vars" || e->name == "$$parms")
f8a968bc 10920 {
5f36109e
JS
10921 e->assert_no_components("tracepoint", true);
10922
1c922ad7 10923 print_format* pf = print_format::create(e->tok, "sprintf");
0a6f5a3f 10924
f8a968bc 10925 for (unsigned i = 0; i < args.size(); ++i)
b278033a 10926 {
dcaa1a65
JS
10927 if (!args[i].usable)
10928 continue;
f8a968bc
JS
10929 if (i > 0)
10930 pf->raw_components += " ";
10931 pf->raw_components += args[i].name;
3e3bd7b6 10932 target_symbol *tsym = new target_symbol;
f8a968bc 10933 tsym->tok = e->tok;
277c21bc 10934 tsym->name = "$" + args[i].name;
5f36109e 10935 tsym->components = e->components;
b278033a 10936
f8a968bc
JS
10937 // every variable should always be accessible!
10938 tsym->saved_conversion_error = 0;
8c2f50c0 10939 expression *texp = require<expression> (tsym); // NB: throws nothing ...
14a97852
JS
10940 if (tsym->saved_conversion_error) // ... but this is how we know it happened.
10941 {
10942 if (dw.sess.verbose>2)
e26c2f83 10943 for (const semantic_error *c = tsym->saved_conversion_error;
c081af73 10944 c != 0; c = c->get_chain())
4c5d9906 10945 clog << _("variable location problem [man error::dwarf]: ") << c->what() << endl;
14a97852
JS
10946 pf->raw_components += "=?";
10947 continue;
10948 }
b278033a 10949
c4965ad9 10950 if (e->check_pretty_print ())
5f36109e
JS
10951 pf->raw_components += "=%s";
10952 else
10953 pf->raw_components += args[i].isptr ? "=%p" : "=%#x";
f8a968bc
JS
10954 pf->args.push_back(texp);
10955 }
0a6f5a3f 10956
f8a968bc
JS
10957 pf->components = print_format::string_to_components(pf->raw_components);
10958 provide (pf);
b278033a 10959 }
f8a968bc
JS
10960 else
10961 assert(0); // shouldn't get here
0a6f5a3f
JS
10962}
10963
0a6f5a3f 10964void
f8a968bc 10965tracepoint_var_expanding_visitor::visit_target_symbol (target_symbol* e)
0a6f5a3f 10966{
aff5d390 10967 try
c69a87e0 10968 {
bd1fcbad 10969 assert(e->name.size() > 0 && e->name[0] == '$');
aff5d390 10970
4f58db62
JL
10971 if (e->name == "$$name" || e->name == "$$system"
10972 || e->name == "$$parms" || e->name == "$$vars")
c69a87e0
FCE
10973 visit_target_symbol_context (e);
10974 else
10975 visit_target_symbol_arg (e);
10976 }
10977 catch (const semantic_error &er)
10978 {
5e50524f
FCE
10979 if (sess.verbose > 3)
10980 clog << "chaining to " << *e->tok << endl
10981 << sess.build_error_msg(er) << endl;
1af1e62d 10982 e->chain (er);
c69a87e0
FCE
10983 provide (e);
10984 }
0a6f5a3f
JS
10985}
10986
10987
79189b84
JS
10988tracepoint_derived_probe::tracepoint_derived_probe (systemtap_session& s,
10989 dwflpp& dw, Dwarf_Die& func_die,
968b8188 10990 const string& tracepoint_system,
79189b84
JS
10991 const string& tracepoint_name,
10992 probe* base, probe_point* loc):
4f58db62
JL
10993 derived_probe (base, loc, true /* .components soon rewritten */), sess (s),
10994 tracepoint_system (tracepoint_system), tracepoint_name (tracepoint_name)
56894e91 10995{
79189b84
JS
10996 // create synthetic probe point name; preserve condition
10997 vector<probe_point::component*> comps;
10998 comps.push_back (new probe_point::component (TOK_KERNEL));
81e0a46a
JL
10999
11000 // tag on system to the final name unless we're in compatibility mode so that
11001 // e.g. pn() returns just the name as before
968b8188 11002 string final_name = tracepoint_name;
81e0a46a
JL
11003 if (!tracepoint_system.empty()
11004 && strverscmp(s.compatible.c_str(), "2.6") > 0)
968b8188 11005 final_name = tracepoint_system + ":" + final_name;
81e0a46a 11006
968b8188
JL
11007 comps.push_back (new probe_point::component (TOK_TRACE,
11008 new literal_string(final_name)));
79189b84
JS
11009 this->sole_location()->components = comps;
11010
6fb70fb7 11011 // fill out the available arguments in this tracepoint
16b2359a
AM
11012 if (s.runtime_mode == systemtap_session::bpf_runtime)
11013 build_args_for_bpf(dw, func_die);
11014 else
11015 build_args(dw, func_die);
56894e91 11016
6fb70fb7
JS
11017 // determine which header defined this tracepoint
11018 string decl_file = dwarf_decl_file(&func_die);
b78a0fbb 11019 header = decl_file;
d4393459 11020
6fb70fb7
JS
11021 // tracepoints from FOO_event_types.h should really be included from FOO.h
11022 // XXX can dwarf tell us the include hierarchy? it would be better to
11023 // ... walk up to see which one was directly included by tracequery.c
3c1b3d06 11024 // XXX: see also PR9993.
d4393459 11025 size_t header_pos = header.find("_event_types");
6fb70fb7
JS
11026 if (header_pos != string::npos)
11027 header.erase(header_pos, 12);
56894e91 11028
f8a968bc 11029 // Now expand the local variables in the probe body
70719cbf 11030 tracepoint_var_expanding_visitor v (dw, args);
16aa72d9
FCE
11031 var_expand_const_fold_loop (sess, this->body, v);
11032
a45664f4 11033 for (unsigned i = 0; i < args.size(); i++)
16b2359a
AM
11034 {
11035 if (!args[i].used)
11036 continue;
11037
11038 if (s.runtime_mode == systemtap_session::bpf_runtime)
11039 {
11040 bpf_context_vardecl* v = new bpf_context_vardecl;
11041 v->name = "__tracepoint_arg_" + args[i].name;
11042 v->tok = this->tok;
11043 v->set_arity(0, this->tok);
11044 v->type = pe_long;
11045 v->synthetic = true;
11046 v->size = args[i].size;
11047 v->offset = args[i].offset;
11048
11049 this->locals.push_back(v);
11050 }
11051 else
11052 {
11053 vardecl* v = new vardecl;
9d3a1cd6 11054 v->name = v->unmangled_name = "__tracepoint_arg_" + args[i].name;
16b2359a
AM
11055 v->tok = this->tok;
11056 v->set_arity(0, this->tok);
11057 v->type = pe_long;
11058 v->synthetic = true;
11059
11060 this->locals.push_back (v);
11061 }
11062 }
56894e91 11063
79189b84 11064 if (sess.verbose > 2)
1341a03c 11065 clog << "tracepoint-based " << name() << " tracepoint='" << tracepoint_name << "'" << endl;
79189b84 11066}
dc38c0ae 11067
56894e91 11068
f8a968bc 11069static bool
37efef73 11070resolve_pointer_type(Dwarf_Die& die, bool& isptr)
46b84a80 11071{
611a848e
JS
11072 if (null_die(&die))
11073 {
11074 isptr = true;
11075 return true;
11076 }
11077
d19a9a82 11078 Dwarf_Die type;
37efef73 11079 switch (dwarf_tag(&die))
b20febf3 11080 {
f8a968bc
JS
11081 case DW_TAG_typedef:
11082 case DW_TAG_const_type:
11083 case DW_TAG_volatile_type:
c69af88f 11084 case DW_TAG_restrict_type:
f8a968bc 11085 // iterate on the referent type
37efef73
JS
11086 return (dwarf_attr_die(&die, DW_AT_type, &die)
11087 && resolve_pointer_type(die, isptr));
11088
f8a968bc 11089 case DW_TAG_base_type:
a52d2ac0 11090 case DW_TAG_enumeration_type:
37efef73
JS
11091 case DW_TAG_structure_type:
11092 case DW_TAG_union_type:
f8a968bc 11093 // base types will simply be treated as script longs
37efef73
JS
11094 // structs/unions must be referenced by pointer elsewhere
11095 isptr = false;
f8a968bc 11096 return true;
37efef73
JS
11097
11098 case DW_TAG_array_type:
f8a968bc 11099 case DW_TAG_pointer_type:
37efef73
JS
11100 case DW_TAG_reference_type:
11101 case DW_TAG_rvalue_reference_type:
11102 // pointer-like types can be treated as script longs,
dcaa1a65 11103 // and if we know their type, they can also be dereferenced
37efef73
JS
11104 isptr = true;
11105 type = die;
11106 while (dwarf_attr_die(&type, DW_AT_type, &type))
d19a9a82
JS
11107 {
11108 // It still might be a non-type, e.g. const void,
11109 // so we need to strip away all qualifiers.
37efef73 11110 int tag = dwarf_tag(&type);
d19a9a82
JS
11111 if (tag != DW_TAG_typedef &&
11112 tag != DW_TAG_const_type &&
c69af88f
MW
11113 tag != DW_TAG_volatile_type &&
11114 tag != DW_TAG_restrict_type)
d19a9a82 11115 {
37efef73
JS
11116 die = type;
11117 return true;
d19a9a82
JS
11118 }
11119 }
37efef73
JS
11120 // otherwise use a null_die to indicate void
11121 std::memset(&die, 0, sizeof(die));
ad370dcc 11122 return true;
37efef73
JS
11123
11124 default:
11125 // should we consider other types too?
11126 return false;
11127 }
11128}
11129
16b2359a
AM
11130static bool
11131is_signed_type(Dwarf_Die *die)
11132{
11133 switch (dwarf_tag(die))
11134 {
11135 case DW_TAG_base_type:
11136 {
11137 Dwarf_Attribute attr;
11138 Dwarf_Word encoding = (Dwarf_Word) -1;
11139 dwarf_formudata (dwarf_attr_integrate (die, DW_AT_encoding, &attr),
11140 &encoding);
11141 return encoding == DW_ATE_signed || encoding == DW_ATE_signed_char;
11142 }
11143 case DW_TAG_typedef:
11144 case DW_TAG_const_type:
11145 case DW_TAG_volatile_type:
11146 case DW_TAG_restrict_type:
11147 // iterate on the referent type
11148 return (dwarf_attr_die(die, DW_AT_type, die)
11149 && is_signed_type(die));
11150
11151 default:
11152 // should we consider other types too?
11153 return false;
11154 }
11155}
11156
11157static int
11158get_byte_size(Dwarf_Die *die, const char *probe_name)
11159{
11160 Dwarf_Attribute attr;
11161 Dwarf_Word size;
11162
11163 if (dwarf_attr(die, DW_AT_byte_size, &attr) == NULL)
11164 {
11165 Dwarf_Word count = 1;
11166 Dwarf_Die type;
11167 Dwarf_Die child;
11168
11169 if (dwarf_tag(die) == DW_TAG_array_type)
11170 {
11171 count = 0;
11172
11173 if (dwarf_child(die, &child) != 0)
11174 throw SEMANTIC_ERROR(_F("cannot resolve size of array %s for probe %s",
11175 dwarf_diename(die), probe_name));
11176
11177 do
11178 if (dwarf_tag(&child) == DW_TAG_subrange_type)
11179 {
11180 if (dwarf_attr(&child, DW_AT_upper_bound, &attr) != NULL)
11181 {
11182 dwarf_formudata(&attr, &count);
11183 count++;
11184 }
11185 else if (dwarf_attr(&child, DW_AT_count, &attr) != NULL)
11186 dwarf_formudata(&attr, &count);
11187 else
11188 SEMANTIC_ERROR(_F("array %s for probe %s has unknown size",
11189 dwarf_diename(die), probe_name));
11190 }
11191 while (dwarf_siblingof(&child, &child) == 0);
11192 }
11193 // Do any other types require special handling?
11194
11195 if (dwarf_attr_die(die, DW_AT_type, &type) == NULL)
11196 throw (SEMANTIC_ERROR(
11197 _F("cannot get byte size of type '%s' for tracepoint '%s'",
11198 dwarf_diename(die), probe_name)));
11199
11200 return count * get_byte_size(&type, probe_name);
11201 }
11202
11203 dwarf_formudata(&attr, &size);
11204 return size;
11205
11206}
37efef73
JS
11207
11208static bool
11209resolve_tracepoint_arg_type(tracepoint_arg& arg)
11210{
11211 if (!resolve_pointer_type(arg.type_die, arg.isptr))
11212 return false;
11213
11214 if (arg.isptr)
11215 arg.typecast = "(intptr_t)";
11216 else if (dwarf_tag(&arg.type_die) == DW_TAG_structure_type ||
11217 dwarf_tag(&arg.type_die) == DW_TAG_union_type)
11218 {
ad370dcc
JS
11219 // for structs/unions which are passed by value, we turn it into
11220 // a pointer that can be dereferenced.
11221 arg.isptr = true;
11222 arg.typecast = "(intptr_t)&";
b20febf3 11223 }
37efef73 11224 return true;
56894e91
JS
11225}
11226
11227
b6e49c97 11228tracepoint_arg::tracepoint_arg(const string& tracepoint_name, Dwarf_Die *arg)
16b2359a
AM
11229: usable(false), used(false), isptr(false), type_die(), size(-1),
11230 offset(-1), is_signed(false)
b6e49c97
JS
11231{
11232 name = dwarf_diename(arg) ?: "";
11233
11234 // read the type of this parameter
11235 if (!dwarf_attr_die (arg, DW_AT_type, &type_die)
11236 || !dwarf_type_name(&type_die, c_type))
11237 throw SEMANTIC_ERROR (_F("cannot get type of parameter '%s' of tracepoint '%s'",
11238 name.c_str(), tracepoint_name.c_str()));
11239
11240 // build the C declaration
11241 if (!dwarf_type_decl(&type_die, "__tracepoint_arg_" + name, c_decl))
11242 throw SEMANTIC_ERROR (_F("cannot get declaration of parameter '%s' of tracepoint '%s'",
11243 name.c_str(), tracepoint_name.c_str()));
11244
11245 usable = resolve_tracepoint_arg_type(*this);
11246}
11247
11248
16b2359a 11249
56894e91 11250void
822a6a3d 11251tracepoint_derived_probe::build_args(dwflpp&, Dwarf_Die& func_die)
56894e91 11252{
6fb70fb7
JS
11253 Dwarf_Die arg;
11254 if (dwarf_child(&func_die, &arg) == 0)
11255 do
11256 if (dwarf_tag(&arg) == DW_TAG_formal_parameter)
11257 {
11258 // build a tracepoint_arg for this parameter
b6e49c97 11259 args.emplace_back(tracepoint_name, &arg);
6fb70fb7 11260 if (sess.verbose > 4)
b6e49c97
JS
11261 {
11262 auto& tparg = args.back();
11263 clog << _F("found parameter for tracepoint '%s': type:'%s' name:'%s' decl:'%s' %s",
11264 tracepoint_name.c_str(), tparg.c_type.c_str(), tparg.name.c_str(),
11265 tparg.c_decl.c_str(), tparg.usable ? "ok" : "unavailable") << endl;
11266 }
6fb70fb7
JS
11267 }
11268 while (dwarf_siblingof(&arg, &arg) == 0);
56894e91
JS
11269}
11270
16b2359a
AM
11271void
11272tracepoint_derived_probe::build_args_for_bpf(dwflpp&, Dwarf_Die& struct_die)
11273{
11274 Dwarf_Die member;
fcdd71ba
WC
11275 int data_start = 0;
11276 bool struct_found = false, more_members = true;
16b2359a 11277
fcdd71ba
WC
11278 if (dwarf_child(&struct_die, &member) != 0) return;
11279
11280 // find the member struct inside the struct that actually has the information about the bpf arguments
11281 while (!struct_found && more_members)
11282 {
11283 Dwarf_Die type;
11284 Dwarf_Attribute attr;
11285 Dwarf_Word off;
11286
11287 dwarf_attr_die(&member, DW_AT_type, &type);
11288 if ((dwarf_tag(&type) == DW_TAG_structure_type)) {
11289 if (dwarf_attr(&member, DW_AT_data_member_location, &attr) == NULL
11290 || dwarf_formudata(&attr, &off) != 0)
11291 throw (SEMANTIC_ERROR
11292 (_F("cannot get offset attribute for variable '%s' of tracepoint '%s'",
11293 dwarf_diename(&member), tracepoint_name.c_str())));
11294 data_start = off;
11295 member = type;
11296 struct_found = true;
11297 } else {
11298 more_members = (dwarf_siblingof(&member, &member) == 0);
11299 }
11300 }
11301
11302 if (dwarf_child(&member, &member) == 0)
11303 do
16b2359a
AM
11304 if (dwarf_tag(&member) == DW_TAG_member)
11305 {
11306 Dwarf_Die type;
11307 Dwarf_Attribute attr;
11308 Dwarf_Word off;
11309 tracepoint_arg arg(dwarf_diename(&member), &member);
11310
11311 if (dwarf_attr(&member, DW_AT_data_member_location, &attr) == NULL
11312 || dwarf_formudata(&attr, &off) != 0)
11313 throw (SEMANTIC_ERROR
11314 (_F("cannot get offset attribute for variable '%s' of tracepoint '%s'",
11315 dwarf_diename(&member), tracepoint_name.c_str())));
11316
11317 dwarf_attr_die(&member, DW_AT_type, &type);
11318 arg.is_signed = is_signed_type(&type);
11319 arg.size = get_byte_size(&type, tracepoint_name.c_str());
fcdd71ba 11320 arg.offset = off + data_start;
16b2359a
AM
11321
11322 args.push_back(arg);
11323 }
fcdd71ba 11324 while (dwarf_siblingof(&member, &member) == 0);
16b2359a
AM
11325}
11326
dc38c0ae 11327void
d0bfd2ac 11328tracepoint_derived_probe::getargs(std::list<std::string> &arg_set) const
dc38c0ae 11329{
dcaa1a65
JS
11330 for (unsigned i = 0; i < args.size(); ++i)
11331 if (args[i].usable)
d0bfd2ac 11332 arg_set.push_back("$"+args[i].name+":"+args[i].c_type);
dc38c0ae
DS
11333}
11334
79189b84
JS
11335void
11336tracepoint_derived_probe::join_group (systemtap_session& s)
197a4d62 11337{
79189b84
JS
11338 if (! s.tracepoint_derived_probes)
11339 s.tracepoint_derived_probes = new tracepoint_derived_probe_group ();
11340 s.tracepoint_derived_probes->enroll (this);
ca6d3b0f 11341 this->group = s.tracepoint_derived_probes;
79189b84 11342}
e38d6504 11343
56894e91 11344
197a4d62 11345void
3e3bd7b6 11346tracepoint_derived_probe::print_dupe_stamp(ostream& o)
56894e91 11347{
3e3bd7b6
JS
11348 for (unsigned i = 0; i < args.size(); i++)
11349 if (args[i].used)
11350 o << "__tracepoint_arg_" << args[i].name << endl;
197a4d62 11351}
56894e91 11352
3e3bd7b6 11353
18b13994
DS
11354// Look for a particular header file in the build directory and the
11355// source directory (if it exists). Return true if the header file was
11356// found.
11357static bool header_exists(systemtap_session& s, const string& header)
11358{
11359 if (file_exists(s.kernel_build_tree + header)
11360 || (!s.kernel_source_tree.empty()
11361 && file_exists(s.kernel_source_tree + header)))
11362 return true;
11363 return false;
11364}
11365
11366
ac4f2837
DS
11367static vector<string> tracepoint_extra_decls (systemtap_session& s,
11368 const string& header,
11369 const bool tracequery)
47dd066d 11370{
3c1b3d06 11371 vector<string> they_live;
ac4f2837
DS
11372
11373 // Several headers end up including events/irq.h, events/kmem.h, and
11374 // events/module.h on RHEL6 (since they include headers that include
11375 // those headers). This causes stap to think the tracepoints from
11376 // those files belong in multiple tracepoint subsystems. To get
11377 // around this, we'll define the header guard macros for those
11378 // tracepoints headers, troublesome header file, then undefine the
11379 // macro. Then, later when a header includes linux/interrupt.h (for
11380 // example), the events/irq.h file doesn't get included because of
11381 // the header guard macro on linux/interrupt.h.
11382 //
11383 // Note that we only do this when building a tracequery module (to
11384 // find all the tracepoints).
11385 if (tracequery)
11386 {
11387 they_live.push_back ("#define _TRACE_KMEM_H");
11388 they_live.push_back ("#define _TRACE_IRQ_H");
11389 they_live.push_back ("#include <linux/interrupt.h>");
11390 they_live.push_back ("#undef _TRACE_IRQ_H");
11391 they_live.push_back ("#undef _TRACE_KMEM_H");
11392
11393 they_live.push_back ("#define _TRACE_MODULE_H");
11394 they_live.push_back ("#include <linux/module.h>");
11395 they_live.push_back ("#undef _TRACE_MODULE_H");
11396 }
11397
3c1b3d06
FCE
11398 // PR 9993
11399 // XXX: may need this to be configurable
d4393459 11400 they_live.push_back ("#include <linux/skbuff.h>");
9e0cd21a
FCE
11401
11402 // PR11649: conditional extra header
11403 // for kvm tracepoints in 2.6.33ish
11404 if (s.kernel_config["CONFIG_KVM"] != string("")) {
d4393459
FCE
11405 they_live.push_back ("#include <linux/kvm_host.h>");
11406 }
11407
7649bb84
DS
11408 if (header.find("xfs") != string::npos
11409 && s.kernel_config["CONFIG_XFS_FS"] != string("")) {
d4393459 11410 they_live.push_back ("#define XFS_BIG_BLKNOS 1");
7649bb84
DS
11411
11412 // The xfs_types.h include file got moved from fs/xfs/xfs_types.h
11413 // to fs/xfs/libxfs/xfs_types.h in upstream kernel 4.4, but that
11414 // patch has gotten backported to RHEL7's 3.10, so we can't really
11415 // depend on kernel version to know where that file is. We could
11416 // add lots of typedefs here to get things to compile (like for
11417 // xfs_agblock_t, xfs_agino_t, etc.), but the upstream kernel
11418 // could change the types being mapped and we'd get a compile
11419 // error when the types don't match. So, we'll try to find the
11420 // xfs_types.h file in the kernel source tree.
18b13994
DS
11421 if (header_exists(s, "/fs/xfs/xfs_linux.h"))
11422 they_live.push_back ("#include \"fs/xfs/xfs_linux.h\"");
11423 if (header_exists(s, "/fs/xfs/libxfs/xfs_types.h"))
11424 they_live.push_back ("#include \"fs/xfs/libxfs/xfs_types.h\"");
11425 else if (header_exists(s, "/fs/xfs/xfs_types.h"))
11426 they_live.push_back ("#include \"fs/xfs/xfs_types.h\"");
11427
b2fa3be2
DS
11428 // Kernel 4.7 needs xfs_format.h.
11429 if (header_exists(s, "/fs/xfs/libxfs/xfs_format.h"))
11430 they_live.push_back ("#include \"fs/xfs/libxfs/xfs_format.h\"");
11431
35c1420d
DS
11432 // Kernel 4.10 needs several headers.
11433 if (header_exists(s, "/fs/xfs/libxfs/xfs_trans_resv.h"))
11434 they_live.push_back ("#include \"fs/xfs/libxfs/xfs_trans_resv.h\"");
11435 if (header_exists(s, "/fs/xfs/xfs_mount.h"))
11436 they_live.push_back ("#include \"fs/xfs/xfs_mount.h\"");
11437 if (header_exists(s, "/fs/xfs/libxfs/xfs_log_format.h"))
11438 they_live.push_back ("#include \"fs/xfs/libxfs/xfs_log_format.h\"");
11439
18b13994
DS
11440 // Sigh. xfs_types.h (no matter where it is), also needs
11441 // xfs_linux.h. But, on newer kernels, xfs_linux.h includes
11442 // xfs_types.h, but really needs a '-I' command to do so. So,
11443 // we'll have to add a custom '-I' command.
11444 if (file_exists(s.kernel_build_tree + "/fs/xfs/libxfs"))
11445 s.kernel_extra_cflags.push_back ("-I" + s.kernel_build_tree
11446 + "/fs/xfs/libxfs");
11447 else if (!s.kernel_source_tree.empty()
11448 && file_exists(s.kernel_source_tree + "/fs/xfs/libxfs"))
ac4f2837
DS
11449 s.kernel_extra_cflags.push_back ("-I" + s.kernel_source_tree
11450 + "/fs/xfs/libxfs");
7649bb84 11451
d4393459
FCE
11452 they_live.push_back ("struct xfs_mount;");
11453 they_live.push_back ("struct xfs_inode;");
11454 they_live.push_back ("struct xfs_buf;");
11455 they_live.push_back ("struct xfs_bmbt_irec;");
c2cf1b87 11456 they_live.push_back ("struct xfs_trans;");
7649bb84 11457 they_live.push_back ("struct xfs_name;");
82343d77 11458 they_live.push_back ("struct xfs_icreate_log;");
9e0cd21a 11459 }
d4393459 11460
7649bb84
DS
11461 if (header.find("nfs") != string::npos
11462 && s.kernel_config["CONFIG_NFSD"] != string("")) {
d4393459 11463 they_live.push_back ("struct rpc_task;");
7649bb84
DS
11464 they_live.push_back ("struct nfs_open_context;");
11465 they_live.push_back ("struct nfs_client;");
11466 they_live.push_back ("struct nfs_fattr;");
11467 they_live.push_back ("struct nfs_fh;");
11468 they_live.push_back ("struct nfs_server;");
11469 they_live.push_back ("struct nfs_pgio_header;");
11470 they_live.push_back ("struct nfs_commit_data;");
11471 they_live.push_back ("struct nfs_closeres;");
11472 they_live.push_back ("struct nfs_closeargs;");
11473 they_live.push_back ("struct nfs_unlinkdata;");
8307aea5 11474 they_live.push_back ("struct nfs_writeverf;");
7649bb84
DS
11475 they_live.push_back ("struct nfs4_sequence_args;");
11476 they_live.push_back ("struct nfs4_sequence_res;");
11477 they_live.push_back ("struct nfs4_session;");
11478 they_live.push_back ("struct nfs4_state;");
11479 they_live.push_back ("struct nfs4_delegreturnres;");
11480 they_live.push_back ("struct nfs4_delegreturnargs;");
b2335020 11481 they_live.push_back ("struct pnfs_layout_hdr;");
7649bb84 11482 they_live.push_back ("struct pnfs_layout_range;");
b2fa3be2 11483 they_live.push_back ("struct pnfs_layout_segment;");
7649bb84
DS
11484
11485 // We need a definition of a 'stateid_t', which is a typedef of an
11486 // anonymous struct. So, we'll have to include the right kernel
11487 // header file.
18b13994 11488 if (header_exists(s, "/fs/nfsd/state.h"))
7649bb84 11489 they_live.push_back ("#include \"fs/nfsd/state.h\"");
b2335020
DS
11490
11491 // We need a definition of the pnfs_update_layout_reason enum, so
11492 // we'll need the right kernel header file.
18b13994
DS
11493 if (s.kernel_config["CONFIG_NFS_V4"] != string("")
11494 && header_exists(s, "/include/linux/nfs4.h"))
b2335020 11495 they_live.push_back ("#include \"linux/nfs4.h\"");
d4393459 11496 }
7649bb84 11497
b64d65e2
FCE
11498 // RHEL6.3
11499 if (header.find("rpc") != string::npos && s.kernel_config["CONFIG_NFSD"] != string("")) {
11500 they_live.push_back ("struct rpc_clnt;");
11501 they_live.push_back ("struct rpc_wait_queue;");
11502 }
d4393459 11503
ac4f2837 11504 if (header.find("timer") != string::npos)
b474fde4
DS
11505 {
11506 // Before including asm/cputime.h, we need to make sure it
11507 // exists, which is tricky since we need the arch specific
11508 // include directory.
11509 string karch = s.architecture;
11510 if (karch == "i386" || karch == "x86_64")
11511 karch = "x86";
11512 if (file_exists(s.kernel_build_tree + "/arch/" + karch
11513 + "/include/asm/cputime.h"))
11514 they_live.push_back ("#include <asm/cputime.h>");
11515 else if (!s.kernel_source_tree.empty()
11516 && file_exists(s.kernel_source_tree + "/arch/" + karch
11517 + "/include/asm/cputime.h"))
11518 they_live.push_back ("#include <asm/cputime.h>");
11519 }
d4393459 11520
c2cf1b87
FCE
11521 // linux 3.0
11522 they_live.push_back ("struct cpu_workqueue_struct;");
11523
18b13994
DS
11524 if (header.find("ext4") != string::npos
11525 && s.kernel_config["CONFIG_EXT4_FS"] != string("")
11526 && header_exists(s, "/fs/ext4/ext4.h"))
11527 they_live.push_back ("#include \"fs/ext4/ext4.h\"");
c2cf1b87 11528
d97d428e 11529 if (header.find("ext3") != string::npos)
ac4f2837
DS
11530 {
11531 they_live.push_back ("struct ext3_reserve_window_node;");
11532 they_live.push_back ("struct super_block;");
11533 they_live.push_back ("struct dentry;");
11534 }
50b72692 11535
d97d428e
FCE
11536 if (header.find("workqueue") != string::npos)
11537 {
11538 they_live.push_back ("struct pool_workqueue;");
11539 they_live.push_back ("struct work_struct;");
11540 }
11541
7649bb84
DS
11542 // Here we need the header file, since we need the snd_soc_dapm_path
11543 // struct declared and the snd_soc_dapm_direction enum.
d97d428e 11544 if (header.find("asoc") != string::npos)
7649bb84 11545 {
18b13994 11546 if (header_exists(s, "/include/sound/soc.h"))
7649bb84
DS
11547 they_live.push_back ("#include \"sound/soc.h\"");
11548 }
d97d428e
FCE
11549
11550 if (header.find("9p") != string::npos)
11551 {
11552 they_live.push_back ("struct p9_client;");
11553 they_live.push_back ("struct p9_fcall;");
11554 }
11555
11556 if (header.find("bcache") != string::npos)
11557 {
11558 they_live.push_back ("struct bkey;");
11559 they_live.push_back ("struct btree;");
11560 they_live.push_back ("struct cache_set;");
11561 they_live.push_back ("struct cache;");
7649bb84 11562 they_live.push_back ("struct bcache_device;");
d97d428e
FCE
11563 }
11564
11565 if (header.find("f2fs") != string::npos)
11566 {
11567 // cannot get fs/f2fs/f2fs.h #included
11568 they_live.push_back ("typedef u32 block_t;");
11569 they_live.push_back ("typedef u32 nid_t;");
7649bb84
DS
11570 they_live.push_back ("struct f2fs_io_info;");
11571 they_live.push_back ("struct f2fs_sb_info;");
11572 they_live.push_back ("struct extent_info;");
11573 they_live.push_back ("struct extent_node;");
b9ede742
DS
11574 they_live.push_back ("struct super_block;");
11575 they_live.push_back ("struct buffer_head;");
11576 they_live.push_back ("struct bio;");
d97d428e
FCE
11577 }
11578
11579 if (header.find("radeon") != string::npos)
ac4f2837
DS
11580 {
11581 they_live.push_back ("struct radeon_bo;");
11582 they_live.push_back ("struct radeon_bo_va;");
11583 they_live.push_back ("struct radeon_cs_parser;");
11584 they_live.push_back ("struct radeon_semaphore;");
11585 }
d97d428e 11586
ac4f2837
DS
11587 // Argh, 3.11, i915_trace.h -> i915_drv.h -> i915_reg.h without
11588 // -I. So, we have to add a custom -I flag.
18b13994
DS
11589 if (header.find("i915_trace") != string::npos)
11590 {
11591 if (file_exists(s.kernel_build_tree + "/drivers/gpu/drm/i915"))
11592 s.kernel_extra_cflags.push_back ("-I" + s.kernel_build_tree
11593 + "/drivers/gpu/drm/i915");
11594 else if (!s.kernel_source_tree.empty()
11595 && file_exists(s.kernel_source_tree + "/drivers/gpu/drm/i915"))
11596 s.kernel_extra_cflags.push_back ("-I" + s.kernel_source_tree
11597 + "/drivers/gpu/drm/i915");
c50798b2
FCE
11598
11599 if (file_exists(s.kernel_build_tree + "/drivers/gpu/drm/i915/gt"))
11600 s.kernel_extra_cflags.push_back ("-I" + s.kernel_build_tree
11601 + "/drivers/gpu/drm/i915/gt");
11602 else if (!s.kernel_source_tree.empty()
11603 && file_exists(s.kernel_source_tree + "/drivers/gpu/drm/i915/gt"))
11604 s.kernel_extra_cflags.push_back ("-I" + s.kernel_source_tree
11605 + "/drivers/gpu/drm/i915/gt");
18b13994 11606 }
d97d428e
FCE
11607
11608 if (header.find("/ath/") != string::npos)
11609 they_live.push_back ("struct ath5k_hw;");
11610
7649bb84
DS
11611 if (header.find("nilfs2") != string::npos)
11612 they_live.push_back ("struct nilfs_transaction_info;");
11613
11614 if (header.find("spi") != string::npos)
11615 {
11616 they_live.push_back ("struct spi_master;");
11617 they_live.push_back ("struct spi_message;");
11618 they_live.push_back ("struct spi_transfer;");
541daad6 11619 they_live.push_back ("struct spi_controller;");
7649bb84
DS
11620 }
11621
11622 if (header.find("thermal_power_allocator") != string::npos)
11623 they_live.push_back ("struct thermal_zone_device;");
11624
7649bb84
DS
11625 if (header.find("brcms_trace_brcmsmac") != string::npos)
11626 they_live.push_back ("struct brcms_timer;");
11627
11628 if (header.find("hda_intel_trace") != string::npos)
11629 they_live.push_back ("struct azx;");
11630
11631 if (header.find("v4l2") != string::npos)
11632 they_live.push_back ("struct v4l2_buffer;");
d97d428e 11633
8307aea5
DS
11634 if (header.find("pcm_trace") != string::npos
11635 || header.find("pcm_param_trace") != string::npos)
ac4f2837
DS
11636 {
11637 they_live.push_back ("struct snd_pcm_substream;");
11638 they_live.push_back ("#include <sound/asound.h>");
11639 }
11640
11641 // Here we need the header file, since we need the migrate_mode enum.
82343d77
DS
11642 if (header.find("migrate") != string::npos
11643 || header.find("compaction") != string::npos)
ac4f2837 11644 {
18b13994 11645 if (header_exists(s, "/include/linux/migrate_mode.h"))
ac4f2837
DS
11646 they_live.push_back ("#include <linux/migrate_mode.h>");
11647 }
11648
11649 // include/trace/events/module.h is odd. If CREATE_TRACE_POINTS
11650 // isn't defined, it doesn't define TRACE_SYSTEM, which means we
11651 // we'll find the module tracepoints (like 'module_load'), but not
11652 // realize they belong in the module subsystem (like
11653 // 'module:module_load'). We'd like to define CREATE_TRACE_POINTS,
11654 // but that causes compilation errors. So, we'll just define
11655 // TRACE_SYSTEM ourselves.
11656 if (header.find("events/module.h") != string::npos)
11657 they_live.push_back ("#define TRACE_SYSTEM module");
11658
11659 if (header.find("events/net.h") != string::npos)
11660 they_live.push_back ("struct ndmsg;");
11661
b2335020
DS
11662 if (header.find("iwl") != string::npos)
11663 {
11664 they_live.push_back ("struct iwl_cmd_header_wide;");
11665 they_live.push_back ("struct iwl_host_cmd;");
11666 they_live.push_back ("struct iwl_trans;");
11667 they_live.push_back ("struct iwl_rx_packet;");
11668 }
11669
35c1420d
DS
11670 if (header.find("mdio") != string::npos)
11671 {
11672 if (header_exists(s, "/include/linux/phy.h"))
11673 they_live.push_back ("#include <linux/phy.h>");
11674 }
11675
11676 if (header.find("wbt") != string::npos)
11677 {
b474fde4
DS
11678 // blk-wbt.h gets included as "../../../block/blk-wbt.h", so we
11679 // need an include path that is 3 levels deep. Note we can't use
11680 // "include/linux/events", since its headers conflict with ours.
11681 if (file_exists(s.kernel_build_tree + "/block/blk-wbt.h")
11682 && file_exists(s.kernel_build_tree + "/fs/xfs/libxfs"))
11683 s.kernel_extra_cflags.push_back ("-I" + s.kernel_build_tree
11684 + "/fs/xfs/libxfs");
11685 else if (!s.kernel_source_tree.empty()
11686 && file_exists(s.kernel_source_tree + "/block/blk-wbt.h")
11687 && file_exists(s.kernel_source_tree + "/fs/xfs/libxfs"))
11688 s.kernel_extra_cflags.push_back ("-I" + s.kernel_source_tree
11689 + "/fs/xfs/libxfs");
11690
35c1420d
DS
11691 if (header_exists(s, "/include/linux/blk_types.h"))
11692 they_live.push_back ("#include <linux/blk_types.h>");
11693 if (header_exists(s, "/include/linux/blkdev.h"))
11694 they_live.push_back ("#include <linux/blkdev.h>");
11695 }
11696
83d186dc
DS
11697 if (header.find("swiotlb") != string::npos)
11698 {
11699 if (header_exists(s, "/include/linux/swiotlb.h"))
11700 they_live.push_back ("#include <linux/swiotlb.h>");
11701 }
11702
c50798b2
FCE
11703
11704 if (header.find("afs") != string::npos)
b474fde4 11705 {
c50798b2
FCE
11706 if (header_exists (s, "/fs/afs/internal.h"))
11707 they_live.push_back ("#include \"fs/afs/internal.h\"");
11708
8307aea5 11709 they_live.push_back ("struct afs_call;");
c50798b2
FCE
11710 }
11711
11712 if (header.find("rxrpc") != string::npos)
11713 {
b474fde4
DS
11714 they_live.push_back ("struct rxrpc_call;");
11715 they_live.push_back ("struct rxrpc_connection;");
11716 they_live.push_back ("struct rxrpc_seq_t;");
11717 they_live.push_back ("struct rxrpc_serial_t;");
8307aea5
DS
11718 they_live.push_back ("struct rxrpc_skb_priv;");
11719
11720 // We need a definition of a 'rxrpc_seq_t', which is a typedef.
11721 // So, we'll have to include the right kernel header file.
11722 if (header_exists(s, "/net/rxrpc/protocol.h"))
11723 they_live.push_back ("#include \"net/rxrpc/protocol.h\"");
c50798b2
FCE
11724
11725 if (header_exists (s, "/net/rxrpc/ar-internal.h"))
11726 they_live.push_back ("#include \"net/rxrpc/ar-internal.h\"");
b474fde4
DS
11727 }
11728
d146c706
DS
11729 if (header.find("xdp") != string::npos)
11730 {
11731 they_live.push_back ("struct bpf_map;");
11732 }
11733
11734 if (header.find("bridge") != string::npos)
11735 {
11736 // br_private.h gets included as
11737 // "../../../net/bridge/br_private.h", so we need an include
11738 // path that is 3 levels deep.
11739 if (file_exists(s.kernel_build_tree + "/net/bridge/br_private.h")
11740 && file_exists(s.kernel_build_tree + "/fs/xfs/libxfs"))
11741 s.kernel_extra_cflags.push_back ("-I" + s.kernel_build_tree
11742 + "/fs/xfs/libxfs");
11743 else if (!s.kernel_source_tree.empty()
11744 && file_exists(s.kernel_source_tree + "/net/bridge/br_private.h")
11745 && file_exists(s.kernel_source_tree + "/fs/xfs/libxfs"))
11746 s.kernel_extra_cflags.push_back ("-I" + s.kernel_source_tree
11747 + "/fs/xfs/libxfs");
11748 }
8307aea5
DS
11749
11750 if (header.find("fsi") != string::npos)
11751 {
11752 they_live.push_back ("struct fsi_master;");
11753 they_live.push_back ("struct fsi_master_gpio;");
11754 }
11755
11756 if (header.find("drm") != string::npos)
11757 {
11758 they_live.push_back ("struct drm_file;");
11759 }
d1e1ba5f
FCE
11760
11761 if (header.find("cachefiles") != string::npos ||
11762 header.find("fscache") != string::npos)
11763 {
11764 they_live.push_back ("#include <linux/fscache.h>");
11765 they_live.push_back ("#include <linux/fscache-cache.h>");
11766 they_live.push_back ("struct cachefiles_object;"); // fs/cachefiles/internal.h
11767 }
11768
11769 #if 0
11770 /* This doesn't work as of 4.17ish, because it interferes with subsequent tracepoints
11771 coming in from other trace headers. e.g. module:module_put vs mei:module_put. */
11772 if (header_exists(s, "/drivers/misc/mei/mei-trace.h"))
11773 they_live.push_back ("#include \"drivers/misc/mei/mei-trace.h\"");
11774 #endif
11775
c50798b2
FCE
11776 if (header.find("gpu_scheduler") != string::npos)
11777 {
11778 they_live.push_back("#include <drm/gpu_scheduler.h>");
11779 }
11780
11781 if (header.find("siox.h") != string::npos)
11782 {
11783 they_live.push_back ("struct siox_device;"); // #include "drivers/siox/siox.h"
11784 they_live.push_back ("struct siox_master;"); // #include "drivers/siox/siox.h"
11785 they_live.push_back ("struct rxrpc_local;"); // #include "drivers/siox/siox.h"
11786 }
11787
3c1b3d06
FCE
11788 return they_live;
11789}
47dd066d
WC
11790
11791
11792void
79189b84 11793tracepoint_derived_probe_group::emit_module_decls (systemtap_session& s)
47dd066d 11794{
79189b84
JS
11795 if (probes.empty())
11796 return;
47dd066d 11797
96b030fe 11798 s.op->newline() << "/* ---- tracepoint probes ---- */";
3ef9830a 11799 s.op->newline() << "#include <linux/stp_tracepoint.h>" << endl;
96b030fe 11800 s.op->newline();
79189b84 11801
47dd066d 11802
a4b9c3b3
FCE
11803 // We create a MODULE_aux_N.c file for each tracepoint header, to allow them
11804 // to be separately compiled. That's because kernel tracepoint headers sometimes
11805 // conflict. PR13155.
11806
11807 map<string,translator_output*> per_header_aux;
11808 // GC NB: the translator_output* structs are owned/retained by the systemtap_session.
47dd066d 11809
6fb70fb7
JS
11810 for (unsigned i = 0; i < probes.size(); ++i)
11811 {
11812 tracepoint_derived_probe *p = probes[i];
75ae2ec9 11813 string header = p->header;
5f73a260 11814
a4b9c3b3
FCE
11815 // We cache the auxiliary output files on a per-header basis. We don't
11816 // need one aux file per tracepoint, only one per tracepoint-header.
11817 translator_output *tpop = per_header_aux[header];
11818 if (tpop == 0)
11819 {
11820 tpop = s.op_create_auxiliary();
11821 per_header_aux[header] = tpop;
11822
11823 // PR9993: Add extra headers to work around undeclared types in individual
11824 // include/trace/foo.h files
ac4f2837
DS
11825 const vector<string>& extra_decls = tracepoint_extra_decls (s, header,
11826 false);
a4b9c3b3
FCE
11827 for (unsigned z=0; z<extra_decls.size(); z++)
11828 tpop->newline() << extra_decls[z] << "\n";
720c435f 11829
a4b9c3b3
FCE
11830 // strip include/ substring, the same way as done in get_tracequery_module()
11831 size_t root_pos = header.rfind("include/");
11832 header = ((root_pos != string::npos) ? header.substr(root_pos + 8) : header);
bfffa443 11833
3ef9830a 11834 tpop->newline() << "#include <linux/stp_tracepoint.h>" << endl;
a4b9c3b3 11835 tpop->newline() << "#include <" << header << ">";
a4b9c3b3
FCE
11836 }
11837
720c435f
JS
11838 // collect the args that are actually in use
11839 vector<const tracepoint_arg*> used_args;
6fb70fb7 11840 for (unsigned j = 0; j < p->args.size(); ++j)
720c435f
JS
11841 if (p->args[j].used)
11842 used_args.push_back(&p->args[j]);
11843
3ef9830a
JS
11844 // forward-declare the generated-side tracepoint callback, and define the
11845 // generated-side tracepoint callback in the main translator-output
11846 string enter_real_fn = "enter_real_tracepoint_probe_" + lex_cast(i);
720c435f 11847 if (used_args.empty())
6fb70fb7 11848 {
3ef9830a
JS
11849 tpop->newline() << "STP_TRACE_ENTER_REAL_NOARGS(" << enter_real_fn << ");";
11850 s.op->newline() << "STP_TRACE_ENTER_REAL_NOARGS(" << enter_real_fn << ")";
6fb70fb7 11851 }
3ef9830a 11852 else
a4b9c3b3 11853 {
3ef9830a
JS
11854 tpop->newline() << "STP_TRACE_ENTER_REAL(" << enter_real_fn;
11855 s.op->newline() << "STP_TRACE_ENTER_REAL(" << enter_real_fn;
11856 s.op->indent(2);
11857 for (unsigned j = 0; j < used_args.size(); ++j)
11858 {
11859 tpop->line() << ", int64_t";
11860 s.op->newline() << ", int64_t __tracepoint_arg_" << used_args[j]->name;
11861 }
11862 tpop->line() << ");";
11863 s.op->newline() << ")";
11864 s.op->indent(-2);
6fb70fb7 11865 }
3ef9830a 11866 s.op->newline() << "{";
7c3e97f4 11867 s.op->newline(1) << "const struct stap_probe * const probe = "
faea5e16 11868 << common_probe_init (p) << ";";
71db462b 11869 common_probe_entryfn_prologue (s, "STAP_SESSION_RUNNING", "probe",
cda141c2 11870 "stp_probe_type_tracepoint");
4f58db62
JL
11871 s.op->newline() << "c->ips.tp.tracepoint_system = "
11872 << lex_cast_qstring (p->tracepoint_system)
11873 << ";";
11874 s.op->newline() << "c->ips.tp.tracepoint_name = "
c12d974f
FCE
11875 << lex_cast_qstring (p->tracepoint_name)
11876 << ";";
720c435f
JS
11877 for (unsigned j = 0; j < used_args.size(); ++j)
11878 {
1341a03c 11879 s.op->newline() << "c->probe_locals." << p->name()
0386bbcf 11880 << "." + s.up->c_localname("__tracepoint_arg_" + used_args[j]->name)
720c435f
JS
11881 << " = __tracepoint_arg_" << used_args[j]->name << ";";
11882 }
26e63673 11883 s.op->newline() << "(*probe->ph) (c);";
ef1337ee 11884 common_probe_entryfn_epilogue (s, true, otf_safe_context(s));
6fb70fb7 11885 s.op->newline(-1) << "}";
47dd066d 11886
a4b9c3b3 11887 // define the real tracepoint callback function
3ef9830a
JS
11888 string enter_fn = "enter_tracepoint_probe_" + lex_cast(i);
11889 if (p->args.empty())
11890 tpop->newline() << "static STP_TRACE_ENTER_NOARGS(" << enter_fn << ")";
11891 else
a4b9c3b3 11892 {
3ef9830a
JS
11893 tpop->newline() << "static STP_TRACE_ENTER(" << enter_fn;
11894 s.op->indent(2);
11895 for (unsigned j = 0; j < p->args.size(); ++j)
b6e49c97 11896 tpop->newline() << ", " << p->args[j].c_decl;
3ef9830a
JS
11897 tpop->newline() << ")";
11898 s.op->indent(-2);
a4b9c3b3 11899 }
3ef9830a
JS
11900 tpop->newline() << "{";
11901 tpop->newline(1) << enter_real_fn << "(";
720c435f
JS
11902 tpop->indent(2);
11903 for (unsigned j = 0; j < used_args.size(); ++j)
11904 {
11905 if (j > 0)
11906 tpop->line() << ", ";
11907 tpop->newline() << "(int64_t)" << used_args[j]->typecast
11908 << "__tracepoint_arg_" << used_args[j]->name;
11909 }
11910 tpop->newline() << ");";
11911 tpop->newline(-3) << "}";
a4b9c3b3
FCE
11912
11913
96b030fe 11914 // emit normalized registration functions
720c435f 11915 tpop->newline() << "int register_tracepoint_probe_" << i << "(void) {";
3ef9830a
JS
11916 tpop->newline(1) << "return STP_TRACE_REGISTER(" << p->tracepoint_name
11917 << ", " << enter_fn << ");";
a4b9c3b3 11918 tpop->newline(-1) << "}";
47dd066d 11919
86758d5f
JS
11920 // NB: we're not prepared to deal with unreg failures. However, failures
11921 // can only occur if the tracepoint doesn't exist (yet?), or if we
11922 // weren't even registered. The former should be OKed by the initial
11923 // registration call, and the latter is safe to ignore.
720c435f 11924 tpop->newline() << "void unregister_tracepoint_probe_" << i << "(void) {";
3ef9830a
JS
11925 tpop->newline(1) << "(void) STP_TRACE_UNREGISTER(" << p->tracepoint_name
11926 << ", " << enter_fn << ");";
a4b9c3b3
FCE
11927 tpop->newline(-1) << "}";
11928 tpop->newline();
5f73a260 11929
720c435f
JS
11930 // declare normalized registration functions
11931 s.op->newline() << "int register_tracepoint_probe_" << i << "(void);";
11932 s.op->newline() << "void unregister_tracepoint_probe_" << i << "(void);";
5f73a260 11933
a4b9c3b3 11934 tpop->assert_0_indent();
af304783
DS
11935 }
11936
96b030fe
JS
11937 // emit an array of registration functions for easy init/shutdown
11938 s.op->newline() << "static struct stap_tracepoint_probe {";
11939 s.op->newline(1) << "int (*reg)(void);";
86758d5f 11940 s.op->newline(0) << "void (*unreg)(void);";
96b030fe
JS
11941 s.op->newline(-1) << "} stap_tracepoint_probes[] = {";
11942 s.op->indent(1);
11943 for (unsigned i = 0; i < probes.size(); ++i)
11944 {
11945 s.op->newline () << "{";
11946 s.op->line() << " .reg=&register_tracepoint_probe_" << i << ",";
11947 s.op->line() << " .unreg=&unregister_tracepoint_probe_" << i;
11948 s.op->line() << " },";
11949 }
11950 s.op->newline(-1) << "};";
11951 s.op->newline();
47dd066d
WC
11952}
11953
11954
79189b84
JS
11955void
11956tracepoint_derived_probe_group::emit_module_init (systemtap_session &s)
47dd066d 11957{
79189b84
JS
11958 if (probes.size () == 0)
11959 return;
47dd066d 11960
79189b84 11961 s.op->newline() << "/* init tracepoint probes */";
96b030fe
JS
11962 s.op->newline() << "for (i=0; i<" << probes.size() << "; i++) {";
11963 s.op->newline(1) << "rc = stap_tracepoint_probes[i].reg();";
11964 s.op->newline() << "if (rc) {";
11965 s.op->newline(1) << "for (j=i-1; j>=0; j--)"; // partial rollback
11966 s.op->newline(1) << "stap_tracepoint_probes[j].unreg();";
11967 s.op->newline(-1) << "break;"; // don't attempt to register any more probes
11968 s.op->newline(-1) << "}";
11969 s.op->newline(-1) << "}";
47dd066d 11970
bc9a523d
FCE
11971 // This would be technically proper (on those autoconf-detectable
11972 // kernels that include this function in tracepoint.h), however we
11973 // already make several calls to synchronze_sched() during our
11974 // shutdown processes.
47dd066d 11975
bc9a523d
FCE
11976 // s.op->newline() << "if (rc)";
11977 // s.op->newline(1) << "tracepoint_synchronize_unregister();";
11978 // s.op->indent(-1);
79189b84 11979}
47dd066d
WC
11980
11981
79189b84
JS
11982void
11983tracepoint_derived_probe_group::emit_module_exit (systemtap_session& s)
47dd066d 11984{
79189b84
JS
11985 if (probes.empty())
11986 return;
47dd066d 11987
96b030fe
JS
11988 s.op->newline() << "/* deregister tracepoint probes */";
11989 s.op->newline() << "for (i=0; i<" << probes.size() << "; i++)";
11990 s.op->newline(1) << "stap_tracepoint_probes[i].unreg();";
11991 s.op->indent(-1);
47dd066d 11992
bc9a523d 11993 // Not necessary: see above.
47dd066d 11994
bc9a523d 11995 // s.op->newline() << "tracepoint_synchronize_unregister();";
79189b84 11996}
b20febf3 11997
47dd066d 11998
75ead1f7 11999struct tracepoint_query : public base_query
47dd066d 12000{
75ead1f7
JS
12001 probe * base_probe;
12002 probe_point * base_loc;
12003 vector<derived_probe *> & results;
f982c59b 12004 set<string> probed_names;
47dd066d 12005
75ead1f7
JS
12006 void handle_query_module();
12007 int handle_query_cu(Dwarf_Die * cudie);
12008 int handle_query_func(Dwarf_Die * func);
16b2359a 12009 int handle_query_type(Dwarf_Die * type);
266c72bd 12010 int handle_query_type_syscall_events(Dwarf_Die * cudie);
822a6a3d 12011 void query_library (const char *) {}
dabd71bb 12012 void query_plt (const char *, size_t) {}
b20febf3 12013
5c378838 12014 static int tracepoint_query_cu (Dwarf_Die * cudie, tracepoint_query * q);
7d007451 12015 static int tracepoint_query_func (Dwarf_Die * func, tracepoint_query * q);
16b2359a
AM
12016 static int tracepoint_query_type (Dwarf_Die * type,
12017 bool has_inner_types,
12018 const std::string& prefix,
12019 tracepoint_query * q);
e6d3a5bd
JL
12020
12021 tracepoint_query(dwflpp & dw, const string & tracepoint,
12022 probe * base_probe, probe_point * base_loc,
12023 vector<derived_probe *> & results):
12024 base_query(dw, "*"), base_probe(base_probe),
12025 base_loc(base_loc), results(results)
12026 {
12027 // The user may have specified the system to probe, e.g. all of the
12028 // following are possible:
12029 //
12030 // sched_switch --> tracepoint named sched_switch
12031 // sched:sched_switch --> tracepoint named sched_switch in the sched system
12032 // sch*:sched_* --> system starts with sch and tracepoint starts with sched_
12033 // sched:* --> all tracepoints in system sched
12034 // *:sched_switch --> same as just sched_switch
12035
12036 size_t sys_pos = tracepoint.find(':');
12037 if (sys_pos == string::npos)
12038 {
12039 this->system = "";
12040 this->tracepoint = tracepoint;
12041 }
12042 else
12043 {
81e0a46a
JL
12044 if (strverscmp(sess.compatible.c_str(), "2.6") <= 0)
12045 throw SEMANTIC_ERROR (_("SYSTEM:TRACEPOINT syntax not supported "
12046 "with --compatible <= 2.6"));
12047
e6d3a5bd
JL
12048 this->system = tracepoint.substr(0, sys_pos);
12049 this->tracepoint = tracepoint.substr(sys_pos+1);
12050 }
7bed8bec
JL
12051
12052 // make sure we have something to query (filters out e.g. "" and ":")
12053 if (this->tracepoint.empty())
12054 throw SEMANTIC_ERROR (_("invalid tracepoint string provided"));
e6d3a5bd 12055 }
99c2b408
JL
12056
12057private:
12058 string system; // target subsystem(s) to query
12059 string tracepoint; // target tracepoint(s) to query
12060 string current_system; // subsystem of module currently being visited
12061
12062 string retrieve_trace_system();
75ead1f7 12063};
47dd066d 12064
befcb049
JL
12065// name of section where TRACE_SYSTEM is stored
12066// (see tracepoint_builder::get_tracequery_modules())
12067#define STAP_TRACE_SYSTEM ".stap_trace_system"
47dd066d 12068
968b8188
JL
12069string
12070tracepoint_query::retrieve_trace_system()
12071{
12072 Dwarf_Addr bias;
12073 Elf* elf = dwfl_module_getelf(dw.module, &bias);
12074 if (!elf)
12075 return "";
12076
12077 size_t shstrndx;
12078 if (elf_getshdrstrndx(elf, &shstrndx))
12079 return "";
12080
12081 Elf_Scn *scn = NULL;
12082 GElf_Shdr shdr_mem;
12083
12084 while ((scn = elf_nextscn(elf, scn)))
12085 {
12086 if (gelf_getshdr(scn, &shdr_mem) == NULL)
12087 return "";
12088
12089 const char *name = elf_strptr(elf, shstrndx, shdr_mem.sh_name);
12090 if (name == NULL)
12091 return "";
12092
befcb049 12093 if (strcmp(name, STAP_TRACE_SYSTEM) == 0)
968b8188
JL
12094 break;
12095 }
12096
12097 if (scn == NULL)
12098 return "";
12099
12100 // Extract saved TRACE_SYSTEM in section
12101 Elf_Data *data = elf_getdata(scn, NULL);
12102 if (!data)
12103 return "";
12104
12105 return string((char*)data->d_buf);
12106}
12107
12108
47dd066d 12109void
75ead1f7 12110tracepoint_query::handle_query_module()
47dd066d 12111{
99c2b408 12112 // Get the TRACE_SYSTEM for this module, if any. It will be found in the
befcb049 12113 // STAP_TRACE_SYSTEM section if it exists.
968b8188
JL
12114 current_system = retrieve_trace_system();
12115
12116 // check if user wants a specific system
12117 if (!system.empty())
12118 {
12119 // don't need to go further if module has no system or doesn't
12120 // match the one we want
12121 if (current_system.empty()
12122 || !dw.function_name_matches_pattern(current_system, system))
12123 return;
12124 }
12125
75ead1f7 12126 // look for the tracepoints in each CU
337b7c44 12127 dw.iterate_over_cus(tracepoint_query_cu, this, false);
47dd066d
WC
12128}
12129
12130
75ead1f7
JS
12131int
12132tracepoint_query::handle_query_cu(Dwarf_Die * cudie)
47dd066d 12133{
75ead1f7 12134 dw.focus_on_cu (cudie);
5f52fafe 12135 dw.mod_info->get_symtab();
47dd066d 12136
16b2359a
AM
12137 // look at each type to see if it's a tracepoint
12138 if (dw.sess.runtime_mode == dw.sess.systemtap_session::bpf_runtime)
266c72bd
FCE
12139 {
12140 if (0 && current_system == "raw_syscalls")
12141 // In BPF / trace_events world, syscalls are abstracted from
12142 // the TRACE_EVENT_FN() (pure callbacks), via
12143 // kernel/trace/trace_syscalls.stp into a family of trace
12144 // events (demultiplexed by syscall id#). There is a
12145 // standardized event-field structure that does -not- show up
12146 // in these header files, nor in the vmlinux file, but are
12147 // synthesized/registered at kernel boot time.
12148 return handle_query_type_syscall_events (cudie);
12149 else
12150 return dwflpp::iterate_over_globals (cudie, tracepoint_query_type, this);
12151 }
16b2359a 12152
75ead1f7
JS
12153 // look at each function to see if it's a tracepoint
12154 string function = "stapprobe_" + tracepoint;
12155 return dw.iterate_over_functions (tracepoint_query_func, this, function);
47dd066d
WC
12156}
12157
12158
75ead1f7
JS
12159int
12160tracepoint_query::handle_query_func(Dwarf_Die * func)
47dd066d 12161{
75ead1f7 12162 dw.focus_on_function (func);
47dd066d 12163
60d98537 12164 assert(startswith(dw.function_name, "stapprobe_"));
75ead1f7 12165 string tracepoint_instance = dw.function_name.substr(10);
f982c59b
JS
12166
12167 // check for duplicates -- sometimes tracepoint headers may be indirectly
12168 // included in more than one of our tracequery modules.
12169 if (!probed_names.insert(tracepoint_instance).second)
12170 return DWARF_CB_OK;
12171
c4a04833
FCE
12172 // PR17126: blacklist
12173 if (!sess.guru_mode)
12174 {
12175 if ((sess.architecture.substr(0,3) == "ppc" ||
12176 sess.architecture.substr(0,7) == "powerpc") &&
12177 (tracepoint_instance == "hcall_entry" ||
90918315
HK
12178 tracepoint_instance == "hcall_exit" ||
12179 tracepoint_instance == "hash_fault"))
c4a04833
FCE
12180 {
12181 sess.print_warning(_F("tracepoint %s is blacklisted on architecture %s",
12182 tracepoint_instance.c_str(), sess.architecture.c_str()));
12183 return DWARF_CB_OK;
12184 }
12185 }
12186
79189b84 12187 derived_probe *dp = new tracepoint_derived_probe (dw.sess, dw, *func,
968b8188 12188 current_system,
79189b84
JS
12189 tracepoint_instance,
12190 base_probe, base_loc);
12191 results.push_back (dp);
75ead1f7 12192 return DWARF_CB_OK;
47dd066d
WC
12193}
12194
16b2359a
AM
12195int
12196tracepoint_query::handle_query_type(Dwarf_Die * type)
12197{
12198 Dwarf_Die struct_die = *type;
12199
12200 if (!dwarf_hasattr(type, DW_AT_name))
12201 return DWARF_CB_OK;
12202
12203 std::string name(dwarf_diename(type));
12204
12205 if (!dw.function_name_matches_pattern(name, "stapprobe_" + tracepoint)
12206 || startswith(name, "stapprobe_template_"))
12207 return DWARF_CB_OK;
12208
12209 name = name.substr(10);
12210
12211 // get the corresponding structure die
12212 while (dwarf_tag(&struct_die) == DW_TAG_typedef)
12213 {
12214 if (dwarf_attr_die(&struct_die, DW_AT_type, &struct_die) == NULL)
12215 throw SEMANTIC_ERROR(_F("Unable to resolve base type of %s for probe %s\n",
12216 name.c_str(), tracepoint.c_str()));
12217 }
12218
12219 assert(dwarf_tag(&struct_die) == DW_TAG_structure_type);
12220
12221 // check for duplicates -- sometimes tracepoint headers may be indirectly
12222 // included in more than one of our tracequery modules.
12223 if (!probed_names.insert(name).second)
12224 return DWARF_CB_OK;
12225
12226 derived_probe *dp = new tracepoint_derived_probe(dw.sess, dw, struct_die,
12227 current_system, name,
12228 base_probe, base_loc);
12229 results.push_back(dp);
12230 return DWARF_CB_OK;
12231}
47dd066d 12232
266c72bd
FCE
12233
12234int
12235tracepoint_query::handle_query_type_syscall_events(Dwarf_Die * cudie)
12236{
12237 (void) cudie;
12238
12239 return DWARF_CB_OK;
12240}
12241
12242
12243
75ead1f7 12244int
5c378838 12245tracepoint_query::tracepoint_query_cu (Dwarf_Die * cudie, tracepoint_query * q)
47dd066d 12246{
85007c04 12247 if (pending_interrupts) return DWARF_CB_ABORT;
75ead1f7 12248 return q->handle_query_cu(cudie);
47dd066d
WC
12249}
12250
12251
75ead1f7 12252int
7d007451 12253tracepoint_query::tracepoint_query_func (Dwarf_Die * func, tracepoint_query * q)
47dd066d 12254{
85007c04 12255 if (pending_interrupts) return DWARF_CB_ABORT;
75ead1f7 12256 return q->handle_query_func(func);
47dd066d
WC
12257}
12258
16b2359a
AM
12259int
12260tracepoint_query::tracepoint_query_type (Dwarf_Die *type, bool has_inner_types,
12261 const std::string& prefix, tracepoint_query *q)
12262{
12263 // needed to match signature of dwflpp::iterate_over_globals callback
12264 (void) has_inner_types;
12265 (void) prefix;
12266
12267 if (pending_interrupts) return DWARF_CB_ABORT;
12268 return q->handle_query_type(type);
12269}
12270
47dd066d 12271
0a6f5a3f 12272struct tracepoint_builder: public derived_probe_builder
47dd066d 12273{
0a6f5a3f
JS
12274private:
12275 dwflpp *dw;
12276 bool init_dw(systemtap_session& s);
c9ccb642
FCE
12277 void get_tracequery_modules(systemtap_session& s,
12278 const vector<string>& headers,
12279 vector<string>& modules);
47dd066d 12280
0a6f5a3f 12281public:
47dd066d 12282
0a6f5a3f
JS
12283 tracepoint_builder(): dw(0) {}
12284 ~tracepoint_builder() { delete dw; }
47dd066d 12285
0a6f5a3f
JS
12286 void build_no_more (systemtap_session& s)
12287 {
12288 if (dw && s.verbose > 3)
b530b5b3 12289 clog << _("tracepoint_builder releasing dwflpp") << endl;
0a6f5a3f
JS
12290 delete dw;
12291 dw = NULL;
435f53a7
FCE
12292
12293 delete_session_module_cache (s);
0a6f5a3f 12294 }
47dd066d 12295
0a6f5a3f
JS
12296 void build(systemtap_session& s,
12297 probe *base, probe_point *location,
12298 literal_map_t const& parameters,
12299 vector<derived_probe*>& finished_results);
352c84fe
FL
12300
12301 virtual string name() { return "tracepoint builder"; }
0a6f5a3f 12302};
47dd066d 12303
47dd066d 12304
c9ccb642 12305
2a0e62a8 12306// Create (or cache) one or more tracequery .o modules, based upon the
c9ccb642
FCE
12307// tracepoint-related header files given. Return the generated or cached
12308// modules[].
12309
12310void
12311tracepoint_builder::get_tracequery_modules(systemtap_session& s,
12312 const vector<string>& headers,
12313 vector<string>& modules)
0a6f5a3f 12314{
c95eddf7 12315 if (s.verbose > 2)
55e50c24 12316 {
ce0f6648 12317 clog << _F("Pass 2: getting a tracepoint query for %zu headers: ", headers.size()) << endl;
55e50c24
JS
12318 for (size_t i = 0; i < headers.size(); ++i)
12319 clog << " " << headers[i] << endl;
12320 }
c95eddf7 12321
2a0e62a8
JS
12322 map<string,string> headers_cache_obj; // header name -> cache/.../tracequery_hash.o file name
12323 // Map the headers to cache .o names. Note that this has side-effects of
c9ccb642
FCE
12324 // creating the $SYSTEMTAP_DIR/.cache/XX/... directory and the hash-log file,
12325 // so we prefer not to repeat this.
12326 vector<string> uncached_headers;
12327 for (size_t i=0; i<headers.size(); i++)
2a0e62a8 12328 headers_cache_obj[headers[i]] = find_tracequery_hash(s, headers[i]);
c9ccb642
FCE
12329
12330 // They may be in the cache already.
12331 if (s.use_cache && !s.poison_cache)
12332 for (size_t i=0; i<headers.size(); i++)
12333 {
12334 // see if the cached module exists
2a0e62a8 12335 const string& tracequery_path = headers_cache_obj[headers[i]];
c9ccb642
FCE
12336 if (!tracequery_path.empty() && file_exists(tracequery_path))
12337 {
12338 if (s.verbose > 2)
12339 clog << _F("Pass 2: using cached %s", tracequery_path.c_str()) << endl;
47dd066d 12340
c252fca2
JS
12341 // an empty file is a cached failure
12342 if (get_file_size(tracequery_path) > 0)
12343 modules.push_back (tracequery_path);
c9ccb642
FCE
12344 }
12345 else
12346 uncached_headers.push_back(headers[i]);
12347 }
12348 else
12349 uncached_headers = headers;
f982c59b 12350
c9ccb642
FCE
12351 // If we have nothing left to search for, quit
12352 if (uncached_headers.empty()) return;
55e50c24 12353
c9ccb642 12354 map<string,string> headers_tracequery_src; // header -> C-source code mapping
55e50c24 12355
c9ccb642
FCE
12356 // We could query several subsets of headers[] to make this go
12357 // faster, but let's KISS and do one at a time.
12358 for (size_t i=0; i<uncached_headers.size(); i++)
55e50c24 12359 {
c9ccb642
FCE
12360 const string& header = uncached_headers[i];
12361
12362 // create a tracequery source file
12363 ostringstream osrc;
12364
12365 // PR9993: Add extra headers to work around undeclared types in individual
12366 // include/trace/foo.h files
ac4f2837 12367 vector<string> short_decls = tracepoint_extra_decls(s, header, true);
c9ccb642
FCE
12368
12369 // add each requested tracepoint header
75ae2ec9 12370 size_t root_pos = header.rfind("include/");
832f100d 12371 short_decls.push_back(string("#include <") +
75ae2ec9 12372 ((root_pos != string::npos) ? header.substr(root_pos + 8) : header) +
d4393459 12373 string(">"));
f982c59b 12374
c9ccb642
FCE
12375 osrc << "#ifdef CONFIG_TRACEPOINTS" << endl;
12376 osrc << "#include <linux/tracepoint.h>" << endl;
832f100d 12377
fcdd71ba
WC
12378 // BPF raw tracepoint macros for creating the multiple fields
12379 // of the data struct that describes the raw tracepoint.
12380 // These macros counts up to 12. Any more, it will return 13th argument.
12381 // These macros will likely have issues with raw tracepoints with more than 12 arguments.
12382 osrc << "#define __COUNT_ARGS(_0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _n, X...) _n" << endl;
12383 osrc << "#define COUNT_ARGS(X...) __COUNT_ARGS(, ##X, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0)" << endl;
12384 osrc << "#define __CONCAT(a, b) a ## b" << endl;
12385 osrc << "#define CONCATENATE(a, b) __CONCAT(a, b)" << endl;
12386 osrc << "#define __FIELD_ENTRY(x) x __attribute__ ((aligned (8)))" << endl;
12387 osrc << "#define __FIELD1(a,...) __FIELD_ENTRY(a);" << endl;
12388 osrc << "#define __FIELD2(a,...) __FIELD_ENTRY(a); __FIELD1(__VA_ARGS__)" << endl;
12389 osrc << "#define __FIELD3(a,...) __FIELD_ENTRY(a); __FIELD2(__VA_ARGS__)" << endl;
12390 osrc << "#define __FIELD4(a,...) __FIELD_ENTRY(a); __FIELD3(__VA_ARGS__)" << endl;
12391 osrc << "#define __FIELD5(a,...) __FIELD_ENTRY(a); __FIELD4(__VA_ARGS__)" << endl;
12392 osrc << "#define __FIELD6(a,...) __FIELD_ENTRY(a); __FIELD5(__VA_ARGS__)" << endl;
12393 osrc << "#define __FIELD7(a,...) __FIELD_ENTRY(a); __FIELD6(__VA_ARGS__)" << endl;
12394 osrc << "#define __FIELD8(a,...) __FIELD_ENTRY(a); __FIELD7(__VA_ARGS__)" << endl;
12395 osrc << "#define __FIELD9(a,...) __FIELD_ENTRY(a); __FIELD8(__VA_ARGS__)" << endl;
12396 osrc << "#define __FIELD10(a,...) __FIELD_ENTRY(a); __FIELD9(__VA_ARGS__)" << endl;
12397 osrc << "#define __FIELD11(a,...) __FIELD_ENTRY(a); __FIELD10(__VA_ARGS__)" << endl;
12398 osrc << "#define __FIELD12(a,...) __FIELD_ENTRY(a); __FIELD11(__VA_ARGS__)" << endl;
12399 osrc << "#define FIELDS(...) CONCATENATE(__FIELD, COUNT_ARGS(__VA_ARGS__))(__VA_ARGS__)" << endl;
12400
12401 // The following PARAMS and DECLARE_TRACE_* macros are used
12402 // by both linux kernel module and bpf raw tracepoints.
12403
12404 // The kernel has changed this naming a few times, previously TPPROTO,
12405 // TP_PROTO, TPARGS, TP_ARGS, etc. so let's just dupe the latest.
12406 osrc << "#ifndef PARAMS" << endl;
12407 osrc << "#define PARAMS(args...) args" << endl;
12408 osrc << "#endif" << endl;
266c72bd 12409
fcdd71ba
WC
12410 // 2.6.35 added the NOARGS variant, but it's the same for us
12411 osrc << "#undef DECLARE_TRACE_NOARGS" << endl;
12412 osrc << "#define DECLARE_TRACE_NOARGS(name) \\" << endl;
12413 osrc << " DECLARE_TRACE(name, void, )" << endl;
12414
12415 // 2.6.38 added the CONDITION variant, which can also just redirect
12416 osrc << "#undef DECLARE_TRACE_CONDITION" << endl;
12417 osrc << "#define DECLARE_TRACE_CONDITION(name, proto, args, cond) \\" << endl;
12418 osrc << " DECLARE_TRACE(name, PARAMS(proto), PARAMS(args))" << endl;
12419
12420 // older tracepoints used DEFINE_TRACE, so redirect that too
12421 osrc << "#undef DEFINE_TRACE" << endl;
12422 osrc << "#define DEFINE_TRACE(name, proto, args) \\" << endl;
12423 osrc << " DECLARE_TRACE(name, PARAMS(proto), PARAMS(args))" << endl;
12424
12425 // Macros to help build the struct describing the older cooked bpf tracepoints
12426 osrc << "#undef __field" << endl;
12427 osrc << "#define __field(type, item) type item;" << endl;
12428
12429 osrc << "#undef __field_desc" << endl;
12430 osrc << "#define __field_desc(type, container, item) type item;" << endl;
12431
12432 osrc << "#undef __array" << endl;
12433 osrc << "#define __array(type, item, size) type item[size];" << endl;
12434
12435 osrc << "#undef __array_desc" << endl;
12436 osrc << "#define __array_desc(type, container, item, size) type item[size];" << endl;
12437
12438 osrc << "#undef __dynamic_array" << endl;
12439 osrc << "#define __dynamic_array(type, item, len) u32 item;" << endl;
12440
12441 osrc << "#undef __string" << endl;
12442 osrc << "#define __string(item, src) __dynamic_array(char, item, -1)" << endl;
12443
12444 osrc << "#undef __bitmask" << endl;
12445 osrc << "#define __bitmask(item, nr_bits) __dynamic_array(char, item, -1)" << endl;
12446
12447 osrc << "#undef TP_STRUCT__entry" << endl;
12448 osrc << "#define TP_STRUCT__entry(args...) args" << endl;
12449
12450 if (s.runtime_mode != systemtap_session::bpf_runtime) {
12451 // override DECLARE_TRACE to synthesize probe functions for us
12452 osrc << "#undef DECLARE_TRACE" << endl;
12453 osrc << "#define DECLARE_TRACE(name, proto, args) \\" << endl;
12454 osrc << " void stapprobe_##name(proto) {}" << endl;
12455 } else {
12456 if (s.use_bpf_raw_tracepoint) {
12457 // override DECLARE_TRACE to synthesize struct for the bpf raw tracepoint
12458 osrc << "#undef DECLARE_TRACE" << endl;
12459 osrc << "#define DECLARE_TRACE(name, proto, args) \\" << endl;
12460 osrc << " struct stapprobe_##name { struct { FIELDS(proto) } data; } stapprobe_##name;" << endl;
12461 } else {
12462 // Macros to create structure for older cooked bpf tracepoints
12463 // Similar to above, but instantiates structs instead of functions.
12464 // The members will become tracepoint args.
12465 osrc << "#undef DECLARE_EVENT_CLASS" << endl;
12466 osrc << "#define DECLARE_EVENT_CLASS(name, proto, args, tstruct, assign, print) \\" << endl;
12467 osrc << " struct stapprobe_template_##name { unsigned long long pad; struct { tstruct } data; };" << endl;
12468
12469 // typedef helps us access template's debuginfo when given name's debuginfo
12470 osrc << "#undef DEFINE_EVENT" << endl;
12471 osrc << "#define DEFINE_EVENT(template, name, proto, args) \\" << endl;
12472 osrc << " typedef struct stapprobe_template_##template stapprobe_##name; \\" << endl;
12473 osrc << " stapprobe_##name stapprobe_inst_##name;" << endl;
12474
12475 osrc << "#undef TRACE_EVENT" << endl;
12476 osrc << "#define TRACE_EVENT(name, proto, args, tstruct, assign, print) \\" << endl;
12477 osrc << " struct stapprobe_##name { unsigned long long pad; struct { tstruct } data; } stapprobe_##name;" << endl;
12478
12479 osrc << "#undef TRACE_EVENT_FN" << endl;
12480 osrc << "#define TRACE_EVENT_FN(name, proto, args, tstruct, assign, print, reg, unreg) \\" << endl;
12481 osrc << " struct stapprobe_##name { unsigned long long pad; struct { tstruct } data; } stapprobe_##name;" << endl;
12482
12483 osrc << "#undef TRACE_EVENT_CONDITION" << endl;
12484 osrc << "#define TRACE_EVENT_CONDITION(name, proto, args, cond, tstruct, assign, print) \\" << endl;
12485 osrc << " struct stapprobe_##name { unsigned long long pad; struct { tstruct } data; } stapprobe_##name;" << endl;
12486 }
12487 }
832f100d 12488
c9ccb642
FCE
12489 // add the specified decls/#includes
12490 for (unsigned z=0; z<short_decls.size(); z++)
12491 osrc << "#undef TRACE_INCLUDE_FILE\n"
12492 << "#undef TRACE_INCLUDE_PATH\n"
12493 << short_decls[z] << "\n";
12494
7d46f26e
JL
12495 // create a section that will hold the TRACE_SYSTEM for this header
12496 osrc << "#ifdef TRACE_SYSTEM" << endl;
12497 osrc << "const char stap_trace_system[]" << endl;
befcb049 12498 osrc << " __attribute__((section(\"" STAP_TRACE_SYSTEM "\")))" << endl;
7d46f26e
JL
12499 osrc << " = __stringify(TRACE_SYSTEM);" << endl;
12500 osrc << "#endif" << endl;
12501
c9ccb642
FCE
12502 // finish up the module source
12503 osrc << "#endif /* CONFIG_TRACEPOINTS */" << endl;
47dd066d 12504
c9ccb642
FCE
12505 // save the source file away
12506 headers_tracequery_src[header] = osrc.str();
55e50c24 12507 }
f982c59b 12508
c9ccb642 12509 // now build them all together
2a0e62a8 12510 map<string,string> tracequery_objs = make_tracequeries(s, headers_tracequery_src);
47dd066d 12511
40d78ace
JS
12512 // now extend the modules list, and maybe plop them into the cache
12513 for (size_t i=0; i<uncached_headers.size(); i++)
12514 {
12515 const string& header = uncached_headers[i];
12516 const string& tracequery_obj = tracequery_objs[header];
12517 const string& tracequery_path = headers_cache_obj[header];
12518 if (tracequery_obj !="" && file_exists(tracequery_obj))
12519 {
12520 modules.push_back (tracequery_obj);
12521 if (s.use_cache)
2a0e62a8 12522 copy_file(tracequery_obj, tracequery_path, s.verbose > 2);
40d78ace
JS
12523 }
12524 else if (s.use_cache)
12525 // cache an empty file for failures
12526 copy_file("/dev/null", tracequery_path, s.verbose > 2);
12527 }
f982c59b
JS
12528}
12529
12530
d4393459 12531
f982c59b
JS
12532bool
12533tracepoint_builder::init_dw(systemtap_session& s)
12534{
12535 if (dw != NULL)
12536 return true;
12537
12538 vector<string> tracequery_modules;
55e50c24 12539 vector<string> system_headers;
f982c59b
JS
12540
12541 glob_t trace_glob;
d4393459 12542
b19a4376 12543 // find kernel_source_tree from DW_AT_comp_dir
d4393459 12544 if (s.kernel_source_tree == "")
f982c59b 12545 {
d4393459 12546 unsigned found;
ccf2c922 12547 Dwfl *dwfl = setup_dwfl_kernel ("kernel", &found, s);
d4393459
FCE
12548 if (found)
12549 {
12550 Dwarf_Die *cudie = 0;
12551 Dwarf_Addr bias;
12552 while ((cudie = dwfl_nextcu (dwfl, cudie, &bias)) != NULL)
12553 {
e19ebcf7 12554 assert_no_interrupts();
d4393459
FCE
12555 Dwarf_Attribute attr;
12556 const char* name = dwarf_formstring (dwarf_attr (cudie, DW_AT_comp_dir, &attr));
36d65b45 12557 if (name)
d4393459 12558 {
f27496f3
JS
12559 // Before we try to use it, check that the path actually
12560 // exists locally and is distinct from the build tree.
12561 if (!file_exists(name))
36d65b45
JS
12562 {
12563 if (s.verbose > 2)
f27496f3
JS
12564 clog << _F("Ignoring inaccessible kernel source tree (DW_AT_comp_dir) at '%s'", name) << endl;
12565 }
12566 else if (resolve_path(name) == resolve_path(s.kernel_build_tree))
12567 {
12568 if (s.verbose > 2)
12569 clog << _F("Ignoring duplicate kernel source tree (DW_AT_comp_dir) at '%s'", name) << endl;
36d65b45
JS
12570 }
12571 else
12572 {
12573 if (s.verbose > 2)
f27496f3
JS
12574 clog << _F("Located kernel source tree (DW_AT_comp_dir) at '%s'", name) << endl;
12575 s.kernel_source_tree = name;
36d65b45 12576 }
61f1a63b 12577
d4393459
FCE
12578 break; // skip others; modern Kbuild uses same comp_dir for them all
12579 }
12580 }
12581 }
44392d73 12582 dwfl_end (dwfl);
d4393459
FCE
12583 }
12584
b19a4376 12585 // find kernel_source_tree from a source link, when different from build
f3fdcc93 12586 if (s.kernel_source_tree == "")
b19a4376 12587 {
f3fdcc93
HT
12588 vector<string> source_trees;
12589
12590 // vendor kernel (e.g. Fedora): the source link is in the same dir
12591 // as the build tree
12592 if (endswith(s.kernel_build_tree, "/build"))
12593 {
12594 string source_tree = s.kernel_build_tree;
12595 source_tree.replace(source_tree.length() - 5, 5, "source");
12596 source_trees.push_back(source_tree);
12597 }
12598
12599 // vanilla kernel: the source link is in the build tree
12600 source_trees.push_back(s.kernel_build_tree + "/source");
12601
12602 for (unsigned i = 0; i < source_trees.size(); i++)
b19a4376 12603 {
f3fdcc93
HT
12604 string source_tree = source_trees[i];
12605
12606 if (dir_exists(source_tree) &&
12607 resolve_path(source_tree) != resolve_path(s.kernel_build_tree))
12608 {
12609 if (s.verbose > 2)
12610 clog << _F("Located kernel source tree at '%s'", source_tree.c_str()) << endl;
12611 s.kernel_source_tree = source_tree;
12612 break;
12613 }
b19a4376
JS
12614 }
12615 }
12616
d4393459
FCE
12617 // prefixes
12618 vector<string> glob_prefixes;
12619 glob_prefixes.push_back (s.kernel_build_tree);
12620 if (s.kernel_source_tree != "")
12621 glob_prefixes.push_back (s.kernel_source_tree);
12622
12623 // suffixes
12624 vector<string> glob_suffixes;
12625 glob_suffixes.push_back("include/trace/events/*.h");
12626 glob_suffixes.push_back("include/trace/*.h");
d97d428e 12627 glob_suffixes.push_back("include/ras/*_event.h");
ac4f2837 12628 glob_suffixes.push_back("arch/x86/entry/vsyscall/*trace.h");
d97d428e 12629 glob_suffixes.push_back("arch/x86/kernel/*trace.h");
b2335020 12630 glob_suffixes.push_back("arch/*/include/asm/*trace*.h");
d97d428e 12631 glob_suffixes.push_back("arch/*/include/asm/trace/*.h");
ac4f2837 12632 glob_suffixes.push_back("arch/*/kvm/*trace.h");
f89ff3e2 12633 glob_suffixes.push_back("fs/xfs/linux-*/xfs_tr*.h");
d97d428e
FCE
12634 glob_suffixes.push_back("fs/*/*trace*.h");
12635 glob_suffixes.push_back("net/*/*trace*.h");
ac4f2837
DS
12636 glob_suffixes.push_back("sound/core/*_trace.h");
12637 glob_suffixes.push_back("sound/hda/*trace*.h");
d97d428e 12638 glob_suffixes.push_back("sound/pci/hda/*_trace.h");
ac4f2837 12639 glob_suffixes.push_back("drivers/base/regmap/*trace*.h");
d97d428e
FCE
12640 glob_suffixes.push_back("drivers/gpu/drm/*_trace.h");
12641 glob_suffixes.push_back("drivers/gpu/drm/*/*_trace.h");
12642 glob_suffixes.push_back("drivers/net/wireless/*/*/*trace*.h");
12643 glob_suffixes.push_back("drivers/usb/host/*trace*.h");
82343d77 12644 glob_suffixes.push_back("virt/kvm/*/*trace*.h");
d97d428e
FCE
12645
12646 // see also tracepoint_extra_decls above
d4393459
FCE
12647
12648 // compute cartesian product
12649 vector<string> globs;
12650 for (unsigned i=0; i<glob_prefixes.size(); i++)
12651 for (unsigned j=0; j<glob_suffixes.size(); j++)
12652 globs.push_back (glob_prefixes[i]+string("/")+glob_suffixes[j]);
12653
8aa43b8d 12654 set<string> duped_headers;
d4393459
FCE
12655 for (unsigned z = 0; z < globs.size(); z++)
12656 {
12657 string glob_str = globs[z];
12658 if (s.verbose > 3)
b530b5b3 12659 clog << _("Checking tracepoint glob ") << glob_str << endl;
d4393459 12660
067cc66f
CM
12661 int r = glob(glob_str.c_str(), 0, NULL, &trace_glob);
12662 if (r == GLOB_NOSPACE || r == GLOB_ABORTED)
12663 throw runtime_error("Error globbing tracepoint");
12664
f982c59b
JS
12665 for (unsigned i = 0; i < trace_glob.gl_pathc; ++i)
12666 {
12667 string header(trace_glob.gl_pathv[i]);
12668
12669 // filter out a few known "internal-only" headers
60d98537
JS
12670 if (endswith(header, "/define_trace.h") ||
12671 endswith(header, "/ftrace.h") ||
12672 endswith(header, "/trace_events.h") ||
ac4f2837 12673 endswith(header, "/perf.h") ||
60d98537 12674 endswith(header, "_event_types.h"))
f982c59b
JS
12675 continue;
12676
9369d397
JS
12677 // Skip identical headers from the build and source trees.
12678 // NB: For the moment these are only compared by reduced path, since
12679 // get_tracequery_modules and emit_module_decls also reduce the path
12680 // like this for their #includes. If we want to get fancier, like
12681 // comparing file contents, then those functions will also have to be
12682 // more precise in how they #include.
8aa43b8d
JS
12683 size_t root_pos = header.rfind("include/");
12684 if (root_pos != string::npos &&
12685 !duped_headers.insert(header.substr(root_pos + 8)).second)
12686 continue;
12687
55e50c24 12688 system_headers.push_back(header);
f982c59b
JS
12689 }
12690 globfree(&trace_glob);
12691 }
12692
c9ccb642
FCE
12693 // Build tracequery modules
12694 get_tracequery_modules(s, system_headers, tracequery_modules);
55e50c24 12695
f982c59b
JS
12696 // TODO: consider other sources of tracepoint headers too, like from
12697 // a command-line parameter or some environment or .systemtaprc
47dd066d 12698
59c11f91 12699 dw = new dwflpp(s, tracequery_modules, true);
0a6f5a3f
JS
12700 return true;
12701}
47dd066d 12702
0a6f5a3f
JS
12703void
12704tracepoint_builder::build(systemtap_session& s,
12705 probe *base, probe_point *location,
12706 literal_map_t const& parameters,
12707 vector<derived_probe*>& finished_results)
12708{
fcdd71ba
WC
12709 if (s.runtime_mode == systemtap_session::bpf_runtime &&
12710 strverscmp(s.compatible.c_str(), "4.2") >= 0) {
12711 s.use_bpf_raw_tracepoint = (s.kernel_functions.count("bpf_raw_tracepoint_release") > 0);
12712 if (!s.use_bpf_raw_tracepoint)
12713 throw SEMANTIC_ERROR (_("SYSTEM: new BPF TRACEPOINT behavior not supported "
12714 "by target kernel (or use --compatible=4.1 option)"));
12715 }
12716
0a6f5a3f
JS
12717 if (!init_dw(s))
12718 return;
47dd066d 12719
45a63356 12720 interned_string tracepoint;
75ead1f7 12721 assert(get_param (parameters, TOK_TRACE, tracepoint));
47dd066d 12722
75ead1f7 12723 tracepoint_query q(*dw, tracepoint, base, location, finished_results);
d906ab9d 12724 unsigned results_pre = finished_results.size();
06de3a04 12725 dw->iterate_over_modules<base_query>(&query_module, &q);
d906ab9d
JL
12726 unsigned results_post = finished_results.size();
12727
12728 // Did we fail to find a match? Let's suggest something!
12729 if (results_pre == results_post)
12730 {
12731 size_t pos;
12732 string sugs = suggest_dwarf_functions(s, q.visited_modules, tracepoint);
12733 while ((pos = sugs.find("stapprobe_")) != string::npos)
12734 sugs.erase(pos, string("stapprobe_").size());
12735 if (!sugs.empty())
ece93f53
JL
12736 throw SEMANTIC_ERROR (_NF("no match (similar tracepoint: %s)",
12737 "no match (similar tracepoints: %s)",
d906ab9d
JL
12738 sugs.find(',') == string::npos,
12739 sugs.c_str()));
12740 }
47dd066d 12741}
47dd066d 12742
16b2359a 12743bool
fcdd71ba
WC
12744sort_for_bpf(systemtap_session& s,
12745 tracepoint_derived_probe_group *t,
16b2359a
AM
12746 sort_for_bpf_probe_arg_vector &v)
12747{
fcdd71ba 12748 string tracepoint_flavor = (s.runtime_mode == systemtap_session::bpf_runtime && s.use_bpf_raw_tracepoint) ? "raw_trace/" : "trace/";
16b2359a
AM
12749 if (!t)
12750 return false;
12751
12752 for (auto i = t->probes.begin(); i != t->probes.end(); ++i)
12753 {
12754 tracepoint_derived_probe *p = *i;
12755 v.push_back(std::pair<derived_probe *, std::string>
fcdd71ba 12756 (p, tracepoint_flavor + p->tracepoint_system + "/" + p->tracepoint_name));
16b2359a
AM
12757 }
12758
12759 return true;
12760}
e6fe60e7 12761
b55bc428 12762// ------------------------------------------------------------------------
bd2b1e68 12763// Standard tapset registry.
b55bc428
FCE
12764// ------------------------------------------------------------------------
12765
7a053d3b 12766void
f8220a7b 12767register_standard_tapsets(systemtap_session & s)
b55bc428 12768{
47e0478e 12769 register_tapset_been(s);
93646f4d 12770 register_tapset_itrace(s);
dd0e4fa7 12771 register_tapset_mark(s);
7a212aa8 12772 register_tapset_procfs(s);
912e8c59 12773 register_tapset_timers(s);
8d9609f5 12774 register_tapset_netfilter(s);
b84779a5 12775 register_tapset_utrace(s);
b98a8d73 12776
7a24d422 12777 // dwarf-based kprobe/uprobe parts
c4ce66a1 12778 dwarf_derived_probe::register_patterns(s);
30a279be 12779
888af770
FCE
12780 // XXX: user-space starter set
12781 s.pattern_root->bind_num(TOK_PROCESS)
12782 ->bind_num(TOK_STATEMENT)->bind(TOK_ABSOLUTE)
f66bb29a 12783 ->bind_privilege(pr_all)
888af770
FCE
12784 ->bind(new uprobe_builder ());
12785 s.pattern_root->bind_num(TOK_PROCESS)
12786 ->bind_num(TOK_STATEMENT)->bind(TOK_ABSOLUTE)->bind(TOK_RETURN)
f66bb29a 12787 ->bind_privilege(pr_all)
888af770
FCE
12788 ->bind(new uprobe_builder ());
12789
0a6f5a3f
JS
12790 // kernel tracepoint probes
12791 s.pattern_root->bind(TOK_KERNEL)->bind_str(TOK_TRACE)
12792 ->bind(new tracepoint_builder());
12793
e6fe60e7
AM
12794 // Kprobe based probe
12795 s.pattern_root->bind(TOK_KPROBE)->bind_str(TOK_FUNCTION)
12796 ->bind(new kprobe_builder());
3c57fe1f
JS
12797 s.pattern_root->bind(TOK_KPROBE)->bind_str(TOK_FUNCTION)->bind(TOK_CALL)
12798 ->bind(new kprobe_builder());
e6fe60e7
AM
12799 s.pattern_root->bind(TOK_KPROBE)->bind_str(TOK_MODULE)
12800 ->bind_str(TOK_FUNCTION)->bind(new kprobe_builder());
3c57fe1f
JS
12801 s.pattern_root->bind(TOK_KPROBE)->bind_str(TOK_MODULE)
12802 ->bind_str(TOK_FUNCTION)->bind(TOK_CALL)->bind(new kprobe_builder());
e6fe60e7
AM
12803 s.pattern_root->bind(TOK_KPROBE)->bind_str(TOK_FUNCTION)->bind(TOK_RETURN)
12804 ->bind(new kprobe_builder());
b6371390
JS
12805 s.pattern_root->bind(TOK_KPROBE)->bind_str(TOK_FUNCTION)->bind(TOK_RETURN)
12806 ->bind_num(TOK_MAXACTIVE)->bind(new kprobe_builder());
e6fe60e7
AM
12807 s.pattern_root->bind(TOK_KPROBE)->bind_str(TOK_MODULE)
12808 ->bind_str(TOK_FUNCTION)->bind(TOK_RETURN)->bind(new kprobe_builder());
b6371390
JS
12809 s.pattern_root->bind(TOK_KPROBE)->bind_str(TOK_MODULE)
12810 ->bind_str(TOK_FUNCTION)->bind(TOK_RETURN)
12811 ->bind_num(TOK_MAXACTIVE)->bind(new kprobe_builder());
e6fe60e7
AM
12812 s.pattern_root->bind(TOK_KPROBE)->bind_num(TOK_STATEMENT)
12813 ->bind(TOK_ABSOLUTE)->bind(new kprobe_builder());
dd225250
PS
12814
12815 //Hwbkpt based probe
b47f3a55
FCE
12816 // NB: we formerly registered the probe point types only if the kernel configuration
12817 // allowed it. However, we get better error messages if we allow probes to resolve.
12818 s.pattern_root->bind(TOK_KERNEL)->bind_num(TOK_HWBKPT)
12819 ->bind(TOK_HWBKPT_WRITE)->bind(new hwbkpt_builder());
12820 s.pattern_root->bind(TOK_KERNEL)->bind_str(TOK_HWBKPT)
12821 ->bind(TOK_HWBKPT_WRITE)->bind(new hwbkpt_builder());
12822 s.pattern_root->bind(TOK_KERNEL)->bind_num(TOK_HWBKPT)
12823 ->bind(TOK_HWBKPT_RW)->bind(new hwbkpt_builder());
12824 s.pattern_root->bind(TOK_KERNEL)->bind_str(TOK_HWBKPT)
12825 ->bind(TOK_HWBKPT_RW)->bind(new hwbkpt_builder());
12826 s.pattern_root->bind(TOK_KERNEL)->bind_num(TOK_HWBKPT)
12827 ->bind_num(TOK_LENGTH)->bind(TOK_HWBKPT_WRITE)->bind(new hwbkpt_builder());
12828 s.pattern_root->bind(TOK_KERNEL)->bind_num(TOK_HWBKPT)
12829 ->bind_num(TOK_LENGTH)->bind(TOK_HWBKPT_RW)->bind(new hwbkpt_builder());
12830 // length supported with address only, not symbol names
83ea76b1
WC
12831
12832 //perf event based probe
4763f713 12833 register_tapset_perf(s);
a29858ef 12834 register_tapset_java(s);
25138447 12835 register_tapset_python(s);
b55bc428 12836}
dc38c0ae
DS
12837
12838
b20febf3
FCE
12839vector<derived_probe_group*>
12840all_session_groups(systemtap_session& s)
dc38c0ae 12841{
b20febf3 12842 vector<derived_probe_group*> g;
912e8c59
JS
12843
12844#define DOONE(x) \
12845 if (s. x##_derived_probes) \
12846 g.push_back ((derived_probe_group*)(s. x##_derived_probes))
ab655cf8
DS
12847
12848 // Note that order *is* important here. We want to make sure we
12849 // register (actually run) begin probes before any other probe type
12850 // is run. Similarly, when unregistering probes, we want to
12851 // unregister (actually run) end probes after every other probe type
12852 // has be unregistered. To do the latter,
12853 // c_unparser::emit_module_exit() will run this list backwards.
b20febf3 12854 DOONE(be);
a7fbce1f 12855 DOONE(generic_kprobe);
888af770 12856 DOONE(uprobe);
b20febf3
FCE
12857 DOONE(timer);
12858 DOONE(profile);
12859 DOONE(mark);
0a6f5a3f 12860 DOONE(tracepoint);
dd225250 12861 DOONE(hwbkpt);
83ea76b1 12862 DOONE(perf);
b20febf3 12863 DOONE(hrtimer);
92687f61
DS
12864
12865 // Another "order is important" item. Python probes create synthetic
12866 // procfs probes and the python probes' emit_module_decls() needs to
12867 // be called first.
12868 DOONE(python);
ce82316f 12869 DOONE(procfs);
92687f61 12870
8d9609f5 12871 DOONE(netfilter);
935447c8
DS
12872
12873 // Another "order is important" item. We want to make sure we
12874 // "register" the dummy task_finder probe group after all probe
12875 // groups that use the task_finder.
12876 DOONE(utrace);
a96d1db0 12877 DOONE(itrace);
f31a77f5 12878 DOONE(dynprobe);
935447c8 12879 DOONE(task_finder);
b20febf3
FCE
12880#undef DOONE
12881 return g;
46b84a80 12882}
73267b89
JS
12883
12884/* vim: set sw=2 ts=8 cino=>4,n-2,{2,^-2,t0,(0,u0,w1,M1 : */
This page took 3.745447 seconds and 5 git commands to generate.