summaryrefslogtreecommitdiffstats
path: root/elaborate.h
blob: d22518020f50d2ab2038a095b0a49175ab7fab10 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
// -*- C++ -*-
// Copyright (C) 2005, 2006 Red Hat Inc.
//
// This file is part of systemtap, and is free software.  You can
// redistribute it and/or modify it under the terms of the GNU General
// Public License (GPL); either version 2, or (at your option) any
// later version.

#ifndef ELABORATE_H
#define ELABORATE_H

#include "staptree.h"
#include "parse.h"
#include <string>
#include <vector>
#include <iostream>
#include <sstream>
#include <map>

// ------------------------------------------------------------------------

struct derived_probe;
struct match_node;

struct symresolution_info: public traversing_visitor 
{
protected:
  systemtap_session& session;

public:
  functiondecl* current_function;
  derived_probe* current_probe;
  symresolution_info (systemtap_session& s);

  vardecl* find_var (const std::string& name, int arity);
  functiondecl* find_function (const std::string& name, unsigned arity);

  void visit_block (block *s);
  void visit_symbol (symbol* e);
  void visit_foreach_loop (foreach_loop* e);
  void visit_arrayindex (arrayindex* e);
  void visit_functioncall (functioncall* e);
  void visit_delete_statement (delete_statement* s);
};


struct typeresolution_info: public visitor
{
  typeresolution_info (systemtap_session& s);
  systemtap_session& session;
  unsigned num_newly_resolved;
  unsigned num_still_unresolved;
  bool assert_resolvability;
  functiondecl* current_function;
  derived_probe* current_probe;

  void mismatch (const token* tok, exp_type t1, exp_type t2);
  void unresolved (const token* tok);
  void resolved (const token* tok, exp_type t);
  void invalid (const token* tok, exp_type t);

  exp_type t; // implicit parameter for nested visit call; may clobber

  void visit_block (block* s);
  void visit_embeddedcode (embeddedcode* s);
  void visit_null_statement (null_statement* s);
  void visit_expr_statement (expr_statement* s);
  void visit_if_statement (if_statement* s);
  void visit_for_loop (for_loop* s);
  void visit_foreach_loop (foreach_loop* s);
  void visit_return_statement (return_statement* s);
  void visit_delete_statement (delete_statement* s);
  void visit_next_statement (next_statement* s);
  void visit_break_statement (break_statement* s);
  void visit_continue_statement (continue_statement* s);
  void visit_literal_string (literal_string* e);
  void visit_literal_number (literal_number* e);
  void visit_binary_expression (binary_expression* e);
  void visit_unary_expression (unary_expression* e);
  void visit_pre_crement (pre_crement* e);
  void visit_post_crement (post_crement* e);
  void visit_logical_or_expr (logical_or_expr* e);
  void visit_logical_and_expr (logical_and_expr* e);
  void visit_array_in (array_in* e);
  void visit_comparison (comparison* e);
  void visit_concatenation (concatenation* e);
  void visit_ternary_expression (ternary_expression* e);
  void visit_assignment (assignment* e);
  void visit_symbol (symbol* e);
  void visit_target_symbol (target_symbol* e);
  void visit_arrayindex (arrayindex* e);
  void visit_functioncall (functioncall* e);
  void visit_print_format (print_format* e);
  void visit_stat_op (stat_op* e);
  void visit_hist_op (hist_op* e);
};


// ------------------------------------------------------------------------


// A derived_probe is a probe that has been elaborated by
// binding to a matching provider.  The locations std::vector
// may be smaller or larger than the base probe, since a
// provider may transform it.

class translator_output;

struct derived_probe: public probe
{
  derived_probe (probe* b);
  derived_probe (probe* b, probe_point* l);
  probe* base; // the original parsed probe
  virtual probe* basest () { return base->basest(); }

  virtual ~derived_probe () {}

  virtual void register_probe (systemtap_session& s) = 0;

  virtual void emit_registrations_start (translator_output* o,
					 unsigned index) = 0;
  virtual void emit_registrations_end (translator_output* o,
				       unsigned index) = 0;
  // (from within module_init):
  // rc = ..... register_or_whatever (ENTRYFN);

  virtual void emit_deregistrations (translator_output* o) = 0;
  // (from within module_exit):
  // (void) ..... unregister_or_whatever (ENTRYFN);

  virtual void emit_probe_entries (translator_output* o) = 0;
  // ... for all probe-points:
  // ELABORATE_SPECIFIC_SIGNATURE ENTRYFN {
  //   /* allocate context - probe_prologue */
  //   /* copy parameters, initial state into context */
  //   probe_NUMBER (context);
  //   /* deallocate context - probe_epilogue */
  // }

  virtual void emit_probe_context_vars (translator_output* o) {}
  // From within unparser::emit_common_header, add any extra variables
  // to this probe's context locals.

protected:
  void emit_probe_prologue (translator_output* o, const std::string&);
  void emit_probe_epilogue (translator_output* o);

public:
  static void emit_common_header (translator_output* o);
  // from c_unparser::emit_common_header
};

// ------------------------------------------------------------------------

struct be_derived_probe;
struct dwarf_derived_probe;
struct hrtimer_derived_probe;
struct mark_derived_probe;
struct never_derived_probe;
struct profile_derived_probe;
struct timer_derived_probe;
struct perfmon_derived_probe;
struct unparser;

struct derived_probe_group
{
  virtual ~derived_probe_group () {}

  virtual void register_probe(be_derived_probe* p);
  virtual void register_probe(dwarf_derived_probe* p);
  virtual void register_probe(hrtimer_derived_probe* p);
  virtual void register_probe(mark_derived_probe* p);
  virtual void register_probe(never_derived_probe* p);
  virtual void register_probe(profile_derived_probe* p);
  virtual void register_probe(timer_derived_probe* p);
  virtual void register_probe(perfmon_derived_probe* p);
  virtual size_t size () = 0;

  virtual void emit_probes (translator_output* op, unparser* up) = 0;

  virtual void emit_module_init (translator_output* o) = 0;
};


// ------------------------------------------------------------------------

struct derived_probe_builder
{
  virtual void build(systemtap_session & sess,
		     probe* base, 
		     probe_point* location,
		     std::map<std::string, literal*> const & parameters,
		     std::vector<derived_probe*> & finished_results) = 0;
  virtual ~derived_probe_builder() {}

  static bool get_param (std::map<std::string, literal*> const & parameters,
                         const std::string& key, std::string& value);
  static bool get_param (std::map<std::string, literal*> const & parameters,
                         const std::string& key, int64_t& value);
};


struct
match_key
{
  std::string name;
  bool have_parameter;
  token_type parameter_type;

  match_key(std::string const & n);
  match_key(probe_point::component const & c);

  match_key & with_number();
  match_key & with_string();
  std::string str() const;
  bool operator<(match_key const & other) const;
  bool globmatch(match_key const & other) const;
};


class
match_node
{
  typedef std::map<match_key, match_node*> sub_map_t;
  typedef std::map<match_key, match_node*>::iterator sub_map_iterator_t;
  sub_map_t sub;
  derived_probe_builder* end;

 public:
  match_node();

  void find_and_build (systemtap_session& s,
                       probe* p, probe_point *loc, unsigned pos,
                       std::vector<derived_probe *>& results);

  match_node* bind(match_key const & k);
  match_node* bind(std::string const & k);
  match_node* bind_str(std::string const & k);
  match_node* bind_num(std::string const & k);
  void bind(derived_probe_builder* e);
};

// ------------------------------------------------------------------------

/* struct systemtap_session moved to session.h */

int semantic_pass (systemtap_session& s);
void derive_probes (systemtap_session& s,
                    probe *p, std::vector<derived_probe*>& dps,
                    bool optional = false);

// A helper we use here and in translate, for pulling symbols out of lvalue
// expressions.
symbol * get_symbol_within_expression (expression *e);


struct unparser;

struct derived_probe_group_container: public derived_probe_group
{
private:
  std::vector<derived_probe*> probes;
  derived_probe_group* be_probe_group;
  derived_probe_group* dwarf_probe_group;
  derived_probe_group* hrtimer_probe_group;
  derived_probe_group* mark_probe_group;
  derived_probe_group* never_probe_group;
  derived_probe_group* profile_probe_group;
  derived_probe_group* timer_probe_group;
  derived_probe_group* perfmon_probe_group;

public:
  derived_probe_group_container ();
  ~derived_probe_group_container ();

  void register_probe (be_derived_probe* p);
  void register_probe (dwarf_derived_probe* p);
  void register_probe (hrtimer_derived_probe* p);
  void register_probe (mark_derived_probe* p);
  void register_probe (never_derived_probe* p);
  void register_probe (profile_derived_probe* p);
  void register_probe (timer_derived_probe* p);
  void register_probe (perfmon_derived_probe* p);
  size_t size () { return (probes.size ()); }

  derived_probe* operator[] (size_t n) { return (probes[n]); }

  void emit_probes (translator_output* op, unparser* up);
  void emit_module_init (translator_output* o);
  void emit_module_init_call (translator_output* o);
  void emit_module_exit (translator_output* o);
};


#endif // ELABORATE_H