]> sourceware.org Git - systemtap.git/blob - elaborate.h
PR6503: introduce systemtap_module_refresh() entry point
[systemtap.git] / elaborate.h
1 // -*- C++ -*-
2 // Copyright (C) 2005-2011 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 ELABORATE_H
10 #define ELABORATE_H
11
12 #include "staptree.h"
13 #include "parse.h"
14 #include <string>
15 #include <vector>
16 //#include <iostream>
17 #include <iosfwd>
18 #include <sstream>
19 #include <map>
20 #include <list>
21
22 extern "C" {
23 #include <elfutils/libdw.h>
24 }
25
26 // ------------------------------------------------------------------------
27
28 struct derived_probe;
29 struct match_node;
30
31 struct symresolution_info: public traversing_visitor
32 {
33 protected:
34 systemtap_session& session;
35
36 public:
37 functiondecl* current_function;
38 derived_probe* current_probe;
39 symresolution_info (systemtap_session& s);
40
41 vardecl* find_var (const std::string& name, int arity, const token *tok);
42 functiondecl* find_function (const std::string& name, unsigned arity);
43
44 void visit_block (block *s);
45 void visit_symbol (symbol* e);
46 void visit_foreach_loop (foreach_loop* e);
47 void visit_arrayindex (arrayindex* e);
48 void visit_functioncall (functioncall* e);
49 void visit_delete_statement (delete_statement* s);
50 };
51
52
53 struct typeresolution_info: public visitor
54 {
55 typeresolution_info (systemtap_session& s);
56 systemtap_session& session;
57 unsigned num_newly_resolved;
58 unsigned num_still_unresolved;
59 bool assert_resolvability;
60 functiondecl* current_function;
61 derived_probe* current_probe;
62 std::vector <const token*> resolved_toks; // account for type mis-
63 std::vector <const token*> printed_toks; // matches (BZ 9719)
64
65 void check_arg_type (exp_type wanted, expression* arg);
66 void check_local (vardecl* v);
67 void mismatch (const token* tok, exp_type t1, exp_type t2);
68 void unresolved (const token* tok);
69 void resolved (const token* tok, exp_type t);
70 void invalid (const token* tok, exp_type t);
71
72 exp_type t; // implicit parameter for nested visit call; may clobber
73
74 void visit_block (block* s);
75 void visit_try_block (try_block* s);
76 void visit_embeddedcode (embeddedcode* s);
77 void visit_null_statement (null_statement* s);
78 void visit_expr_statement (expr_statement* s);
79 void visit_if_statement (if_statement* s);
80 void visit_for_loop (for_loop* s);
81 void visit_foreach_loop (foreach_loop* s);
82 void visit_return_statement (return_statement* s);
83 void visit_delete_statement (delete_statement* s);
84 void visit_next_statement (next_statement* s);
85 void visit_break_statement (break_statement* s);
86 void visit_continue_statement (continue_statement* s);
87 void visit_literal_string (literal_string* e);
88 void visit_literal_number (literal_number* e);
89 void visit_embedded_expr (embedded_expr* e);
90 void visit_binary_expression (binary_expression* e);
91 void visit_unary_expression (unary_expression* e);
92 void visit_pre_crement (pre_crement* e);
93 void visit_post_crement (post_crement* e);
94 void visit_logical_or_expr (logical_or_expr* e);
95 void visit_logical_and_expr (logical_and_expr* e);
96 void visit_array_in (array_in* e);
97 void visit_comparison (comparison* e);
98 void visit_concatenation (concatenation* e);
99 void visit_ternary_expression (ternary_expression* e);
100 void visit_assignment (assignment* e);
101 void visit_symbol (symbol* e);
102 void visit_target_symbol (target_symbol* e);
103 void visit_arrayindex (arrayindex* e);
104 void visit_functioncall (functioncall* e);
105 void visit_print_format (print_format* e);
106 void visit_stat_op (stat_op* e);
107 void visit_hist_op (hist_op* e);
108 void visit_cast_op (cast_op* e);
109 void visit_defined_op (defined_op* e);
110 void visit_entry_op (entry_op* e);
111 };
112
113
114 // ------------------------------------------------------------------------
115
116
117 // A derived_probe is a probe that has been elaborated by
118 // binding to a matching provider. The locations std::vector
119 // may be smaller or larger than the base probe, since a
120 // provider may transform it.
121
122 class translator_output;
123 class derived_probe_group;
124
125 struct derived_probe: public probe
126 {
127 derived_probe (probe* b, probe_point* l, bool rewrite_loc=false);
128 probe* base; // the original parsed probe
129 probe_point* base_pp; // the probe_point that led to this derivation
130 virtual const probe* basest () const { return base->basest(); }
131 virtual const probe* almost_basest () const { return base->almost_basest() ?: this; }
132 virtual ~derived_probe () {}
133 virtual void join_group (systemtap_session& s) = 0;
134 virtual probe_point* sole_location () const;
135 virtual probe_point* script_location () const;
136 virtual void printsig (std::ostream &o) const;
137 // return arguments of probe if there
138 virtual void getargs (std::list<std::string> &) const {}
139 void printsig_nested (std::ostream &o) const;
140 virtual void collect_derivation_chain (std::vector<probe*> &probes_list);
141 virtual void collect_derivation_pp_chain (std::vector<probe_point*> &pp_list);
142 std::string derived_locations ();
143
144 virtual void print_dupe_stamp(std::ostream&) {}
145 // To aid duplication elimination, print a stamp which uniquely identifies
146 // the code that will be added to the probe body. (Doesn't need to be the
147 // actual code...)
148
149 virtual void initialize_probe_context_vars (translator_output*) {}
150 // From within unparser::emit_probe, initialized any extra variables
151 // in this probe's context locals.
152
153 virtual void emit_probe_local_init (translator_output*) {}
154 // From within unparser::emit_probe, emit any extra processing block
155 // for this probe.
156
157 virtual void emit_unprivileged_assertion (translator_output*);
158 // From within unparser::emit_probe, emit any unprivileged mode
159 // checking for this probe.
160
161 public:
162 static void emit_common_header (translator_output* o);
163 // from c_unparser::emit_common_header
164 // XXX: probably can move this stuff to a probe_group::emit_module_decls
165
166 static void emit_process_owner_assertion (translator_output*);
167 // From within unparser::emit_probe, emit a check that the current
168 // process belongs to the user.
169
170 static void print_dupe_stamp_unprivileged(std::ostream& o);
171 static void print_dupe_stamp_unprivileged_process_owner(std::ostream& o);
172
173 virtual bool needs_global_locks () { return true; }
174 // by default, probes need locks around global variables
175
176 // Location of semaphores to activate sdt probes
177 Dwarf_Addr sdt_semaphore_addr;
178
179 // index into session.probes[], set and used during translation
180 unsigned session_index;
181 };
182
183 // ------------------------------------------------------------------------
184
185 struct unparser;
186
187 // Various derived classes derived_probe_group manage the
188 // registration/invocation/unregistration of sibling probes.
189 struct derived_probe_group
190 {
191 virtual ~derived_probe_group () {}
192
193 virtual void emit_module_decls (systemtap_session& s) = 0;
194 // The _decls-generated code may assume that declarations such as
195 // the context, embedded-C code, function and probe handler bodies
196 // are all already generated. That is, _decls is called near the
197 // end of the code generation process. It should minimize the
198 // number of separate variables (and to a lesser extent, their
199 // size).
200
201 virtual void emit_module_init (systemtap_session& s) = 0;
202 // The _init-generated code may assume that it is called only once.
203 // If that code fails at run time, it must set rc=1 and roll back
204 // any partial initializations, for its _exit friend will NOT be
205 // invoked. The generated code may use pre-declared "int i, j;"
206 // and set "const char* probe_point;".
207
208 virtual void emit_module_refresh (systemtap_session& s) {}
209 // The _refresh-generated code may be called multiple times during
210 // a session run, bracketed by _init and _exit calls.
211 // Upon failure, it must set enough state so that
212 // a subsequent _exit call will clean up everything.
213 // The generated code may use pre-declared "int i, j;".
214
215 virtual void emit_module_exit (systemtap_session& s) = 0;
216 // The _exit-generated code may assume that it is executed exactly
217 // zero times (if the _init-generated code failed) or once. (_exit
218 // itself may be called a few times, to generate the code in a few
219 // different places in the probe module.)
220 // The generated code may use pre-declared "int i, j;".
221 };
222
223
224 // ------------------------------------------------------------------------
225
226 typedef std::map<std::string, literal*> literal_map_t;
227
228 struct derived_probe_builder
229 {
230 virtual void build(systemtap_session & sess,
231 probe* base,
232 probe_point* location,
233 literal_map_t const & parameters,
234 std::vector<derived_probe*> & finished_results) = 0;
235 virtual ~derived_probe_builder() {}
236 virtual void build_no_more (systemtap_session &) {}
237 virtual bool is_alias () const { return false; }
238
239 static bool has_null_param (literal_map_t const & parameters,
240 const std::string& key);
241 static bool get_param (literal_map_t const & parameters,
242 const std::string& key, std::string& value);
243 static bool get_param (literal_map_t const & parameters,
244 const std::string& key, int64_t& value);
245 };
246
247
248 struct
249 match_key
250 {
251 std::string name;
252 bool have_parameter;
253 exp_type parameter_type;
254
255 match_key(std::string const & n);
256 match_key(probe_point::component const & c);
257
258 match_key & with_number();
259 match_key & with_string();
260 std::string str() const;
261 bool operator<(match_key const & other) const;
262 bool globmatch(match_key const & other) const;
263 };
264
265
266 class
267 match_node
268 {
269 typedef std::map<match_key, match_node*> sub_map_t;
270 typedef std::map<match_key, match_node*>::iterator sub_map_iterator_t;
271 sub_map_t sub;
272 std::vector<derived_probe_builder*> ends;
273
274 public:
275 match_node();
276
277 void find_and_build (systemtap_session& s,
278 probe* p, probe_point *loc, unsigned pos,
279 std::vector<derived_probe *>& results);
280 void build_no_more (systemtap_session &s);
281 void dump (systemtap_session &s, const std::string &name = "");
282
283 match_node* bind(match_key const & k);
284 match_node* bind(std::string const & k);
285 match_node* bind_str(std::string const & k);
286 match_node* bind_num(std::string const & k);
287 match_node* bind_unprivileged(bool b = true);
288 void bind(derived_probe_builder* e);
289
290 private:
291 bool unprivileged_ok;
292 };
293
294 // ------------------------------------------------------------------------
295
296 struct
297 alias_expansion_builder
298 : public derived_probe_builder
299 {
300 probe_alias * alias;
301
302 alias_expansion_builder(probe_alias * a)
303 : alias(a)
304 {}
305
306 virtual void build(systemtap_session & sess,
307 probe * use,
308 probe_point * location,
309 std::map<std::string, literal *> const &,
310 std::vector<derived_probe *> & finished_results);
311 virtual bool is_alias () const { return true; }
312
313 bool checkForRecursiveExpansion (probe *use);
314 };
315
316 // ------------------------------------------------------------------------
317
318 /* struct systemtap_session moved to session.h */
319
320 int semantic_pass (systemtap_session& s);
321 void derive_probes (systemtap_session& s,
322 probe *p, std::vector<derived_probe*>& dps,
323 bool optional = false);
324
325 // A helper we use here and in translate, for pulling symbols out of lvalue
326 // expressions.
327 symbol * get_symbol_within_expression (expression *e);
328
329
330 struct unparser;
331
332
333 #endif // ELABORATE_H
334
335 /* vim: set sw=2 ts=8 cino=>4,n-2,{2,^-2,t0,(0,u0,w1,M1 : */
This page took 0.056229 seconds and 6 git commands to generate.