2 // Copyright (C) 2005-2019 Red Hat Inc.
4 // This file is part of systemtap, and is free software. You can
5 // redistribute it and/or modify it under the terms of the GNU General
6 // Public License (GPL); either version 2, or (at your option) any
29 #include <elfutils/libdw.h>
32 #include "privilege.h"
34 #include "stringtable.h"
36 /* statistical operations used with a global */
37 #define STAT_OP_NONE 1 << 0
38 #define STAT_OP_COUNT 1 << 1
39 #define STAT_OP_SUM 1 << 2
40 #define STAT_OP_MIN 1 << 3
41 #define STAT_OP_MAX 1 << 4
42 #define STAT_OP_AVG 1 << 5
43 #define STAT_OP_VARIANCE 1 << 6
45 // forward decls for all referenced systemtap types
53 struct be_derived_probe_group
;
54 struct generic_kprobe_derived_probe_group
;
55 struct hwbkpt_derived_probe_group
;
56 struct perf_derived_probe_group
;
57 struct uprobe_derived_probe_group
;
58 struct utrace_derived_probe_group
;
59 struct itrace_derived_probe_group
;
60 struct task_finder_derived_probe_group
;
61 struct timer_derived_probe_group
;
62 struct netfilter_derived_probe_group
;
63 struct profile_derived_probe_group
;
64 struct mark_derived_probe_group
;
65 struct tracepoint_derived_probe_group
;
66 struct hrtimer_derived_probe_group
;
67 struct procfs_derived_probe_group
;
68 struct dynprobe_derived_probe_group
;
69 struct python_derived_probe_group
;
72 class translator_output
;
74 struct semantic_error
;
76 struct update_visitor
;
77 struct compile_server_cache
;
79 // XXX: a generalized form of this descriptor could be associated with
80 // a vardecl instead of out here at the systemtap_session level.
83 statistic_decl(int _stat_ops
= 0)
85 linear_low(0), linear_high(0), linear_step(0), bit_shift(0),
88 enum { none
, linear
, logarithmic
} type
;
94 bool operator==(statistic_decl
const & other
)
96 return type
== other
.type
97 && linear_low
== other
.linear_low
98 && linear_high
== other
.linear_high
99 && linear_step
== other
.linear_step
;
103 struct macrodecl
; // defined in parse.h
105 struct parse_error
: public std::runtime_error
109 const parse_error
*chain
;
110 const std::string errsrc
;
111 ~parse_error () throw () {}
112 parse_error (const std::string
& src
, const std::string
& msg
):
113 runtime_error (msg
), tok (0), skip_some (true), chain(0), errsrc(src
) {}
114 parse_error (const std::string
& src
, const std::string
& msg
, const token
* t
):
115 runtime_error (msg
), tok (t
), skip_some (true), chain(0), errsrc(src
) {}
116 parse_error (const std::string
& src
, const std::string
& msg
, bool skip
):
117 runtime_error (msg
), tok (0), skip_some (skip
), chain(0), errsrc(src
) {}
119 std::string
errsrc_chain(void) const
121 return errsrc
+ (chain
? "|" + chain
->errsrc_chain() : "");
125 struct systemtap_session
128 // disable implicit constructors by not implementing these
129 systemtap_session (const systemtap_session
& other
);
130 systemtap_session
& operator= (const systemtap_session
& other
);
132 // copy constructor used by ::clone()
133 systemtap_session (const systemtap_session
& other
,
134 const std::string
& arch
,
135 const std::string
& kern
);
138 systemtap_session ();
139 ~systemtap_session ();
141 // To reset the tmp_dir
142 void create_tmp_dir();
143 void remove_tmp_dir();
144 void reset_tmp_dir();
146 // NB: It is very important for all of the above (and below) fields
147 // to be cleared in the systemtap_session ctor (session.cxx).
148 void setup_kernel_release (const std::string
& kstr
);
149 void insert_loaded_modules ();
151 // command line parsing
152 int parse_cmdline (int argc
, char * const argv
[]);
153 bool parse_cmdline_runtime (const std::string
& opt_runtime
);
154 std::string
version_string ();
156 void usage (int exitcode
);
157 void check_options (int argc
, char * const argv
[]);
158 static const char* morehelp
;
160 // NB: It is very important for all of the above (and below) fields
161 // to be cleared in the systemtap_session ctor (session.cxx).
164 std::string script_file
; // FILE
165 std::string cmdline_script
; // -e PROGRAM
166 std::vector
<std::string
> additional_scripts
; // -E SCRIPT
167 std::stringstream stdin_script
; // stdin script (stap -)
169 std::vector
<std::string
> include_path
;
170 int include_arg_start
;
171 std::vector
<std::string
> c_macros
;
172 std::vector
<std::string
> args
;
173 std::vector
<bool> used_args
;
174 std::vector
<std::string
> kbuildflags
; // -B var=val
175 std::vector
<std::string
> globalopts
; // -G var=val
176 std::vector
<std::string
> modinfos
; // --modinfo tag=value
179 std::string kernel_release
;
180 std::string kernel_base_release
;
181 std::string kernel_build_tree
;
182 std::string kernel_source_tree
;
183 std::vector
<std::string
> kernel_extra_cflags
;
184 std::map
<interned_string
,interned_string
> kernel_config
;
185 std::set
<interned_string
> kernel_exports
;
186 std::set
<interned_string
> kernel_functions
;
187 int parse_kernel_config ();
188 int parse_kernel_exports ();
189 int parse_kernel_functions ();
192 std::map
<std::string
,std::string
> sysenv
;
193 bool update_release_sysroot
;
195 std::string architecture
;
197 std::string runtime_path
;
198 bool runtime_specified
;
199 std::string data_path
;
200 std::string module_name
;
201 const std::string
module_filename() const;
202 std::string stapconf_name
;
203 std::string output_file
;
204 std::string size_option
;
206 std::string
cmd_file();
207 std::string compatible
; // use (strverscmp(s.compatible.c_str(), "N.M") >= 0)
210 unsigned perpass_verbose
[5];
220 bool suppress_warnings
;
223 bool tapset_compile_coverage
;
228 std::string uprobes_path
;
229 std::string uprobes_hash
;
230 bool load_only
; // flight recorder mode
231 privilege_t privilege
;
233 bool systemtap_v_check
;
237 int monitor_interval
;
238 int timeout
; // in ms
241 { dump_none
, // no dumping requested
242 dump_probe_types
, // dump standard tapset probes
243 dump_probe_aliases
, // dump tapset probe aliases
244 dump_functions
, // dump tapset functions
245 dump_matched_probes
, // dump matching probes (-l)
246 dump_matched_probes_vars
// dump matching probes and their variables (-L)
249 // Pattern to match against in listing mode (-l/-L)
250 std::string dump_matched_pattern
;
253 bool suppress_handler_errors
;
254 bool suppress_time_limits
;
256 bool interactive_mode
;
258 bool no_global_var_display
;
259 bool pass_1a_complete
;
261 enum { color_never
, color_auto
, color_always
} color_mode
;
262 enum { prologue_searching_never
, prologue_searching_auto
, prologue_searching_always
} prologue_searching_mode
;
264 enum { kernel_runtime
, dyninst_runtime
, bpf_runtime
} runtime_mode
;
265 bool runtime_usermode_p() const { return runtime_mode
== dyninst_runtime
; }
266 bool use_bpf_raw_tracepoint
;
268 // NB: It is very important for all of the above (and below) fields
269 // to be cleared in the systemtap_session ctor (session.cxx).
273 static bool NSPR_Initialized
; // only once for all sessions
277 std::string client_options_disallowed_for_unprivileged
;
278 std::vector
<std::string
> server_status_strings
;
279 std::vector
<std::string
> specified_servers
;
280 std::string server_trust_spec
;
281 std::vector
<std::string
> server_args
;
282 std::string winning_server
;
283 compile_server_cache
* server_cache
;
284 std::vector
<std::string
> mok_fingerprints
;
285 std::string auto_privilege_level_msg
;
286 std::vector
<std::string
> auto_server_msgs
;
288 bool modules_must_be_signed();
291 // NB: It is very important for all of the above (and below) fields
292 // to be cleared in the systemtap_session ctor (session.cxx).
294 // Mechanism for retrying compilation with a compile server should it fail due
295 // to lack of resources on the local host.
296 // Once it has been decided not to try the server (e.g. syntax error),
297 // that decision cannot be changed.
298 int try_server_status
;
299 bool use_server_on_error
;
301 enum { try_server_unset
, dont_try_server
, do_try_server
};
302 void init_try_server ();
303 void set_try_server (int t
= do_try_server
);
304 bool try_server () const { return try_server_status
== do_try_server
; }
306 // HTTP client/server
307 std::vector
<std::string
> http_servers
;
309 // NB: It is very important for all of the above (and below) fields
310 // to be cleared in the systemtap_session ctor (session.cxx).
313 std::vector
<std::string
> remote_uris
;
314 bool use_remote_prefix
;
315 typedef std::map
<std::pair
<std::string
, std::string
>, systemtap_session
*> session_map_t
;
316 session_map_t subsessions
;
317 systemtap_session
* clone(const std::string
& arch
, const std::string
& release
);
319 // NB: It is very important for all of the above (and below) fields
320 // to be cleared in the systemtap_session ctor (session.cxx).
323 bool use_cache
; // control all caching
324 bool use_script_cache
; // control caching of pass-3/4 output
325 bool poison_cache
; // consider the cache to be write-only
326 std::string cache_path
; // usually ~/.systemtap/cache
327 std::string hash_path
; // path to the cached script module
328 std::string stapconf_path
; // path to the cached stapconf
329 stap_hash
*base_hash
; // hash common to all caching
334 // NB: It is very important for all of the above (and below) fields
335 // to be cleared in the systemtap_session ctor (session.cxx).
337 // temporary directory for module builds etc.
338 // hazardous - it is "rm -rf"'d at exit
340 std::string translated_source
; // C source code
342 match_node
* pattern_root
;
343 void register_library_aliases();
345 // data for various preprocessor library macros
346 std::map
<std::string
, macrodecl
*> library_macros
;
348 // parse trees for the various script files
349 std::vector
<stapfile
*> user_files
;
350 std::vector
<stapfile
*> library_files
;
352 std::string
script_name(); // usually user_files[0]->name
353 std::string
script_basename(); // basename of script_name()
355 // filters to run over all code before symbol resolution
356 // e.g. @cast expansion
357 std::vector
<update_visitor
*> code_filters
;
359 // resolved globals/functions/probes for the run as a whole
360 std::vector
<stapfile
*> files
;
361 std::vector
<vardecl
*> globals
;
362 std::map
<std::string
,functiondecl
*> functions
;
363 std::map
<std::string
,unsigned> overload_count
;
364 // probe counter name -> probe associated with counter
365 std::vector
<std::pair
<std::string
,std::string
> > perf_counters
;
366 std::vector
<derived_probe
*> probes
; // see also *_probes groups below
367 std::vector
<embeddedcode
*> embeds
;
368 std::map
<interned_string
, statistic_decl
> stat_decls
;
369 // track things that are removed
370 std::vector
<vardecl
*> unused_globals
;
371 std::vector
<derived_probe
*> unused_probes
; // see also *_probes groups below
372 std::vector
<functiondecl
*> unused_functions
;
373 std::set
<derived_probe
*> empty_probes
;
375 // resolved/compiled regular expressions for the run
376 std::map
<std::string
, stapdfa
*> dfas
;
377 unsigned dfa_counter
; // used to give unique names
378 unsigned dfa_maxmap
; // used for subexpression-tracking data structure
379 unsigned dfa_maxtag
; // ditto
380 bool need_tagged_dfa
; // triggered by /* pragma:tagged_dfa */
382 // Every probe in these groups must also appear in the
383 // session.probes vector.
384 be_derived_probe_group
* be_derived_probes
;
385 generic_kprobe_derived_probe_group
* generic_kprobe_derived_probes
;
386 hwbkpt_derived_probe_group
* hwbkpt_derived_probes
;
387 perf_derived_probe_group
* perf_derived_probes
;
388 uprobe_derived_probe_group
* uprobe_derived_probes
;
389 utrace_derived_probe_group
* utrace_derived_probes
;
390 itrace_derived_probe_group
* itrace_derived_probes
;
391 task_finder_derived_probe_group
* task_finder_derived_probes
;
392 timer_derived_probe_group
* timer_derived_probes
;
393 netfilter_derived_probe_group
* netfilter_derived_probes
;
394 profile_derived_probe_group
* profile_derived_probes
;
395 mark_derived_probe_group
* mark_derived_probes
;
396 tracepoint_derived_probe_group
* tracepoint_derived_probes
;
397 hrtimer_derived_probe_group
* hrtimer_derived_probes
;
398 procfs_derived_probe_group
* procfs_derived_probes
;
399 dynprobe_derived_probe_group
* dynprobe_derived_probes
;
400 python_derived_probe_group
* python_derived_probes
;
402 // NB: It is very important for all of the above (and below) fields
403 // to be cleared in the systemtap_session ctor (session.cxx).
406 translator_output
* op
;
407 std::vector
<translator_output
*> auxiliary_outputs
;
410 // some symbol addresses
411 // XXX: these belong elsewhere; perhaps the dwflpp instance
412 Dwarf_Addr sym_kprobes_text_start
;
413 Dwarf_Addr sym_kprobes_text_end
;
414 Dwarf_Addr sym_stext
;
416 // List of libdwfl module names to extract symbol/unwind data for.
417 std::set
<std::string
> unwindsym_modules
;
419 struct module_cache
* module_cache
;
420 std::vector
<std::string
> build_ids
;
422 // Secret benchmarking options
423 unsigned long benchmark_sdt_loops
;
424 unsigned long benchmark_sdt_threads
;
426 // NB: It is very important for all of the above (and below) fields
427 // to be cleared in the systemtap_session ctor (session.cxx).
429 std::set
<std::string
> seen_warnings
;
430 int suppressed_warnings
;
431 std::map
<std::string
, int> seen_errors
; // NB: can change to a set if threshold is 1
432 int suppressed_errors
;
433 int warningerr_count
; // see comment in systemtap_session::print_error
435 // Returns number of critical errors (not counting those part of warnings)
436 unsigned num_errors ()
438 return (seen_errors
.size() // all the errors we've encountered
439 - warningerr_count
// except those considered warningerrs
440 + (panic_warnings
? seen_warnings
.size() : 0)); // plus warnings if -W given
443 std::set
<std::string
> rpms_checked
; // enlisted by filename+rpm_type
444 std::set
<std::string
> rpms_to_install
; // resulting rpms
446 translator_output
* op_create_auxiliary(bool trailer_p
= false);
448 int target_namespaces_pid
;
450 unsigned suppress_costly_diagnostics
; /* set during processing of optional probes */
452 const token
* last_token
;
453 void print_token (std::ostream
& o
, const token
* tok
);
454 void print_error (const semantic_error
& e
);
455 std::string
build_error_msg (const semantic_error
& e
);
456 void print_error_source (std::ostream
&, std::string
&, const token
* tok
);
457 void print_error_details (std::ostream
&, std::string
&, const semantic_error
&);
458 void print_error (const parse_error
&pe
,
460 const std::string
&input_name
,
461 bool is_warningerr
= false);
462 std::string
build_error_msg (const parse_error
&pe
,
464 const std::string
&input_name
);
465 void print_warning (const std::string
& w
, const token
* tok
= 0);
466 void printscript(std::ostream
& o
);
467 void report_suppression();
469 // NB: It is very important for all of the above (and below) fields
470 // to be cleared in the systemtap_session ctor (session.cxx).
472 std::string
colorize(const std::string
& str
, const std::string
& type
);
473 std::string
colorize(const token
* tok
);
474 std::string
parse_stap_color(const std::string
& type
);
476 // Some automatic options settings require explanation.
477 void enable_auto_server (const std::string
&message
);
478 void explain_auto_options();
480 bool is_user_file (const std::string
& name
);
481 bool is_primary_probe (derived_probe
*dp
);
483 void clear_script_data();
485 // NB: It is very important for all of the above (and below) fields
486 // to be cleared in the systemtap_session ctor (session.cxx).
489 struct exit_exception
: public std::runtime_error
492 exit_exception (int rc
):
493 runtime_error (_F("early exit requested, rc=%d", rc
)), rc(rc
) {}
497 // global counter of SIGINT/SIGTERM's received
498 extern int pending_interrupts
;
500 // Interrupt exception subclass for catching
501 // interrupts (i.e. ctrl-c).
502 struct interrupt_exception
: public std::runtime_error
504 interrupt_exception ():
505 runtime_error (_("interrupt received")){}
508 void assert_no_interrupts();
512 /* vim: set sw=2 ts=8 cino=>4,n-2,{2,^-2,t0,(0,u0,w1,M1 : */