]> sourceware.org Git - systemtap.git/blob - session.h
regenerate examples index in testsuite/systemtap.examples
[systemtap.git] / session.h
1 // -*- C++ -*-
2 // Copyright (C) 2005-2019 Red Hat Inc.
3 //
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
7 // later version.
8
9 #ifndef SESSION_H
10 #define SESSION_H
11
12 #include "config.h"
13 #if ENABLE_NLS
14 #include <libintl.h>
15 #include <locale.h>
16 #endif
17
18 #include <list>
19 #include <string>
20 #include <vector>
21 #include <iostream>
22 #include <sstream>
23 #include <map>
24 #include <set>
25 #include <stdexcept>
26
27 extern "C" {
28 #include <signal.h>
29 #include <elfutils/libdw.h>
30 }
31
32 #include "privilege.h"
33 #include "util.h"
34 #include "stringtable.h"
35
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
44
45 // forward decls for all referenced systemtap types
46 class stap_hash;
47 class match_node;
48 struct stapfile;
49 struct vardecl;
50 struct token;
51 struct functiondecl;
52 struct derived_probe;
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;
70 struct embeddedcode;
71 struct stapdfa;
72 class translator_output;
73 struct unparser;
74 struct semantic_error;
75 struct module_cache;
76 struct update_visitor;
77 struct compile_server_cache;
78
79 // XXX: a generalized form of this descriptor could be associated with
80 // a vardecl instead of out here at the systemtap_session level.
81 struct statistic_decl
82 {
83 statistic_decl(int _stat_ops = 0)
84 : type(none),
85 linear_low(0), linear_high(0), linear_step(0), bit_shift(0),
86 stat_ops(_stat_ops)
87 {}
88 enum { none, linear, logarithmic } type;
89 int64_t linear_low;
90 int64_t linear_high;
91 int64_t linear_step;
92 int bit_shift;
93 int stat_ops;
94 bool operator==(statistic_decl const & other)
95 {
96 return type == other.type
97 && linear_low == other.linear_low
98 && linear_high == other.linear_high
99 && linear_step == other.linear_step;
100 }
101 };
102
103 struct macrodecl; // defined in parse.h
104
105 struct parse_error: public std::runtime_error
106 {
107 const token* tok;
108 bool skip_some;
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) {}
118
119 std::string errsrc_chain(void) const
120 {
121 return errsrc + (chain ? "|" + chain->errsrc_chain() : "");
122 }
123 };
124
125 struct systemtap_session
126 {
127 private:
128 // disable implicit constructors by not implementing these
129 systemtap_session (const systemtap_session& other);
130 systemtap_session& operator= (const systemtap_session& other);
131
132 // copy constructor used by ::clone()
133 systemtap_session (const systemtap_session& other,
134 const std::string& arch,
135 const std::string& kern);
136
137 public:
138 systemtap_session ();
139 ~systemtap_session ();
140
141 // To reset the tmp_dir
142 void create_tmp_dir();
143 void remove_tmp_dir();
144 void reset_tmp_dir();
145
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 ();
150
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 ();
155 void version ();
156 void usage (int exitcode);
157 void check_options (int argc, char * const argv []);
158 static const char* morehelp;
159
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).
162
163 // command line args
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 -)
168 bool have_script;
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
177
178 std::string release;
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 ();
190
191 std::string sysroot;
192 std::map<std::string,std::string> sysenv;
193 bool update_release_sysroot;
194 std::string machine;
195 std::string architecture;
196 bool native_build;
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;
205 std::string cmd;
206 std::string cmd_file();
207 std::string compatible; // use (strverscmp(s.compatible.c_str(), "N.M") >= 0)
208 int target_pid;
209 int last_pass;
210 unsigned perpass_verbose[5];
211 unsigned verbose;
212 bool timing;
213 bool save_module;
214 bool save_uprobes;
215 bool modname_given;
216 bool keep_tmpdir;
217 bool guru_mode;
218 bool bulk_mode;
219 bool unoptimized;
220 bool suppress_warnings;
221 bool panic_warnings;
222 int buffer_size;
223 bool tapset_compile_coverage;
224 bool need_uprobes;
225 bool need_unwind;
226 bool need_symbols;
227 bool need_lines;
228 std::string uprobes_path;
229 std::string uprobes_hash;
230 bool load_only; // flight recorder mode
231 privilege_t privilege;
232 bool privilege_set;
233 bool systemtap_v_check;
234 bool tmpdir_opt_set;
235 bool read_stdin;
236 bool monitor;
237 int monitor_interval;
238 int timeout; // in ms
239
240 enum
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)
247 } dump_mode;
248
249 // Pattern to match against in listing mode (-l/-L)
250 std::string dump_matched_pattern;
251
252 int download_dbinfo;
253 bool suppress_handler_errors;
254 bool suppress_time_limits;
255 bool color_errors;
256 bool interactive_mode;
257 bool run_example;
258 bool no_global_var_display;
259 bool pass_1a_complete;
260
261 enum { color_never, color_auto, color_always } color_mode;
262 enum { prologue_searching_never, prologue_searching_auto, prologue_searching_always } prologue_searching_mode;
263
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;
267
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).
270
271 // Client/server
272 #if HAVE_NSS
273 static bool NSPR_Initialized; // only once for all sessions
274 void NSPR_init ();
275 #endif
276 bool client_options;
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;
287
288 bool modules_must_be_signed();
289 void get_mok_info();
290
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).
293
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;
300
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; }
305
306 // HTTP client/server
307 std::vector<std::string> http_servers;
308
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).
311
312 // Remote execution
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);
318
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).
321
322 // Cache data
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
330
331 // Skip bad $ vars
332 bool skip_badvars;
333
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).
336
337 // temporary directory for module builds etc.
338 // hazardous - it is "rm -rf"'d at exit
339 std::string tmpdir;
340 std::string translated_source; // C source code
341
342 match_node* pattern_root;
343 void register_library_aliases();
344
345 // data for various preprocessor library macros
346 std::map<std::string, macrodecl*> library_macros;
347
348 // parse trees for the various script files
349 std::vector<stapfile*> user_files;
350 std::vector<stapfile*> library_files;
351
352 std::string script_name(); // usually user_files[0]->name
353 std::string script_basename(); // basename of script_name()
354
355 // filters to run over all code before symbol resolution
356 // e.g. @cast expansion
357 std::vector<update_visitor*> code_filters;
358
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;
374
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 */
381
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;
401
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).
404
405 // unparser data
406 translator_output* op;
407 std::vector<translator_output*> auxiliary_outputs;
408 unparser* up;
409
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;
415
416 // List of libdwfl module names to extract symbol/unwind data for.
417 std::set<std::string> unwindsym_modules;
418 bool unwindsym_ldd;
419 struct module_cache* module_cache;
420 std::vector<std::string> build_ids;
421
422 // Secret benchmarking options
423 unsigned long benchmark_sdt_loops;
424 unsigned long benchmark_sdt_threads;
425
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).
428
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
434
435 // Returns number of critical errors (not counting those part of warnings)
436 unsigned num_errors ()
437 {
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
441 }
442
443 std::set<std::string> rpms_checked; // enlisted by filename+rpm_type
444 std::set<std::string> rpms_to_install; // resulting rpms
445
446 translator_output* op_create_auxiliary(bool trailer_p = false);
447
448 int target_namespaces_pid;
449
450 unsigned suppress_costly_diagnostics; /* set during processing of optional probes */
451
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,
459 const token* tok,
460 const std::string &input_name,
461 bool is_warningerr = false);
462 std::string build_error_msg (const parse_error &pe,
463 const token* tok,
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();
468
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).
471
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);
475
476 // Some automatic options settings require explanation.
477 void enable_auto_server (const std::string &message);
478 void explain_auto_options();
479
480 bool is_user_file (const std::string& name);
481 bool is_primary_probe (derived_probe *dp);
482
483 void clear_script_data();
484
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).
487 };
488
489 struct exit_exception: public std::runtime_error
490 {
491 int rc;
492 exit_exception (int rc):
493 runtime_error (_F("early exit requested, rc=%d", rc)), rc(rc) {}
494 };
495
496
497 // global counter of SIGINT/SIGTERM's received
498 extern int pending_interrupts;
499
500 // Interrupt exception subclass for catching
501 // interrupts (i.e. ctrl-c).
502 struct interrupt_exception: public std::runtime_error
503 {
504 interrupt_exception ():
505 runtime_error (_("interrupt received")){}
506 };
507
508 void assert_no_interrupts();
509
510 #endif // SESSION_H
511
512 /* vim: set sw=2 ts=8 cino=>4,n-2,{2,^-2,t0,(0,u0,w1,M1 : */
This page took 0.056817 seconds and 5 git commands to generate.