Repository navigation
Expand file tree
/
Copy pathast.h
More file actions
381 lines (319 loc) · 9.75 KB
/
Copy pathast.h
File metadata and controls
381 lines (319 loc) · 9.75 KB
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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
#pragma once
#include "location.hh"
#include "utils.h"
#include <map>
#include <string>
#include <vector>
#include "types.h"
namespace bpftrace {
namespace ast {
class Visitor;
class Node {
public:
Node();
Node(location loc);
virtual ~Node() = default;
virtual void accept(Visitor &v) = 0;
location loc;
};
class Map;
class Variable;
class Expression : public Node {
public:
Expression();
Expression(location loc);
SizedType type;
Map *key_for_map = nullptr;
Map *map = nullptr; // Only set when this expression is assigned to a map
Variable *var = nullptr; // Set when this expression is assigned to a variable
bool is_literal = false;
bool is_variable = false;
bool is_map = false;
};
using ExpressionList = std::vector<Expression *>;
class Integer : public Expression {
public:
explicit Integer(long n);
explicit Integer(long n, location loc);
long n;
void accept(Visitor &v) override;
};
class PositionalParameter : public Expression {
public:
explicit PositionalParameter(PositionalParameterType ptype, long n);
explicit PositionalParameter(PositionalParameterType ptype,
long n,
location loc);
PositionalParameterType ptype;
long n;
bool is_in_str = false;
void accept(Visitor &v) override;
};
class String : public Expression {
public:
explicit String(const std::string &str);
explicit String(const std::string &str, location loc);
std::string str;
void accept(Visitor &v) override;
};
class StackMode : public Expression {
public:
explicit StackMode(const std::string &mode);
explicit StackMode(const std::string &mode, location loc);
std::string mode;
void accept(Visitor &v) override;
};
class Identifier : public Expression {
public:
explicit Identifier(const std::string &ident);
explicit Identifier(const std::string &ident, location loc);
std::string ident;
void accept(Visitor &v) override;
};
class Builtin : public Expression {
public:
explicit Builtin(const std::string &ident);
explicit Builtin(const std::string &ident, location loc);
std::string ident;
int probe_id;
void accept(Visitor &v) override;
};
class Call : public Expression {
public:
explicit Call(const std::string &func);
explicit Call(const std::string &func, location loc);
Call(const std::string &func, ExpressionList *vargs);
Call(const std::string &func, ExpressionList *vargs, location loc);
std::string func;
ExpressionList *vargs;
void accept(Visitor &v) override;
};
class Map : public Expression {
public:
explicit Map(const std::string &ident, location loc);
Map(const std::string &ident, ExpressionList *vargs);
Map(const std::string &ident, ExpressionList *vargs, location loc);
std::string ident;
ExpressionList *vargs;
bool skip_key_validation = false;
void accept(Visitor &v) override;
};
class Variable : public Expression {
public:
explicit Variable(const std::string &ident);
explicit Variable(const std::string &ident, location loc);
std::string ident;
void accept(Visitor &v) override;
};
class Binop : public Expression {
public:
Binop(Expression *left, int op, Expression *right, location loc);
Expression *left, *right;
int op;
void accept(Visitor &v) override;
};
class Unop : public Expression {
public:
Unop(int op, Expression *expr, location loc = location());
Unop(int op,
Expression *expr,
bool is_post_op = false,
location loc = location());
Expression *expr;
int op;
bool is_post_op;
void accept(Visitor &v) override;
};
class FieldAccess : public Expression {
public:
FieldAccess(Expression *expr, const std::string &field);
FieldAccess(Expression *expr, const std::string &field, location loc);
Expression *expr;
std::string field;
void accept(Visitor &v) override;
};
class ArrayAccess : public Expression {
public:
ArrayAccess(Expression *expr, Expression *indexpr);
ArrayAccess(Expression *expr, Expression *indexpr, location loc);
Expression *expr;
Expression *indexpr;
void accept(Visitor &v) override;
};
class Cast : public Expression {
public:
Cast(const std::string &type, bool is_pointer, Expression *expr);
Cast(const std::string &type,
bool is_pointer,
Expression *expr,
location loc);
std::string cast_type;
bool is_pointer;
Expression *expr;
void accept(Visitor &v) override;
};
class Statement : public Node {
public:
Statement() = default;
Statement(location loc);
};
using StatementList = std::vector<Statement *>;
class ExprStatement : public Statement {
public:
explicit ExprStatement(Expression *expr);
explicit ExprStatement(Expression *expr, location loc);
Expression *expr;
void accept(Visitor &v) override;
};
class AssignMapStatement : public Statement {
public:
AssignMapStatement(Map *map, Expression *expr, location loc = location());
Map *map;
Expression *expr;
void accept(Visitor &v) override;
};
class AssignVarStatement : public Statement {
public:
AssignVarStatement(Variable *var, Expression *expr);
AssignVarStatement(Variable *var, Expression *expr, location loc);
Variable *var;
Expression *expr;
void accept(Visitor &v) override;
};
class If : public Statement {
public:
If(Expression *cond, StatementList *stmts);
If(Expression *cond, StatementList *stmts, StatementList *else_stmts);
Expression *cond;
StatementList *stmts = nullptr;
StatementList *else_stmts = nullptr;
void accept(Visitor &v) override;
};
class Unroll : public Statement {
public:
Unroll(long int var, StatementList *stmts);
long int var = 0;
StatementList *stmts;
void accept(Visitor &v) override;
};
class Predicate : public Node {
public:
explicit Predicate(Expression *expr);
explicit Predicate(Expression *expr, location loc);
Expression *expr;
void accept(Visitor &v) override;
};
class Ternary : public Expression {
public:
Ternary(Expression *cond, Expression *left, Expression *right);
Ternary(Expression *cond, Expression *left, Expression *right, location loc);
Expression *cond, *left, *right;
void accept(Visitor &v) override;
};
class AttachPoint : public Node {
public:
explicit AttachPoint(const std::string &provider, location loc = location());
AttachPoint(const std::string &provider,
const std::string &func,
location loc = location());
AttachPoint(const std::string &provider,
const std::string &target,
const std::string &func,
bool need_expansion,
location loc = location());
AttachPoint(const std::string &provider,
const std::string &target,
const std::string &ns,
const std::string &func,
bool need_expansion,
location loc = location());
AttachPoint(const std::string &provider,
const std::string &str,
uint64_t val,
location loc = location());
AttachPoint(const std::string &provider,
const std::string &target,
uint64_t addr,
uint64_t len,
const std::string &mode,
location loc = location());
AttachPoint(const std::string &provider,
const std::string &target,
const std::string &func,
uint64_t offset,
location loc = location());
std::string provider;
std::string target;
std::string ns;
std::string func;
usdt_probe_entry usdt; // resolved USDT entry, used to support arguments with wildcard matches
int freq = 0;
uint64_t addr = 0;
uint64_t len = 0;
std::string mode;
bool need_expansion = false;
uint64_t address = 0;
uint64_t func_offset = 0;
void accept(Visitor &v) override;
std::string name(const std::string &attach_point) const;
int index(std::string name);
void set_index(std::string name, int index);
private:
std::map<std::string, int> index_;
};
using AttachPointList = std::vector<AttachPoint *>;
class Probe : public Node {
public:
Probe(AttachPointList *attach_points, Predicate *pred, StatementList *stmts);
AttachPointList *attach_points;
Predicate *pred;
StatementList *stmts;
void accept(Visitor &v) override;
std::string name() const;
bool need_expansion = false; // must build a BPF program per wildcard match
bool need_tp_args_structs = false; // must import struct for tracepoints
int index();
void set_index(int index);
private:
int index_ = 0;
};
using ProbeList = std::vector<Probe *>;
class Program : public Node {
public:
Program(const std::string &c_definitions, ProbeList *probes);
std::string c_definitions;
ProbeList *probes;
void accept(Visitor &v) override;
};
class Visitor {
public:
virtual ~Visitor() = default;
virtual void visit(Integer &integer) = 0;
virtual void visit(PositionalParameter &integer) = 0;
virtual void visit(String &string) = 0;
virtual void visit(Builtin &builtin) = 0;
virtual void visit(Identifier &identifier) = 0;
virtual void visit(StackMode &mode) = 0;
virtual void visit(Call &call) = 0;
virtual void visit(Map &map) = 0;
virtual void visit(Variable &var) = 0;
virtual void visit(Binop &binop) = 0;
virtual void visit(Unop &unop) = 0;
virtual void visit(Ternary &ternary) = 0;
virtual void visit(FieldAccess &acc) = 0;
virtual void visit(ArrayAccess &arr) = 0;
virtual void visit(Cast &cast) = 0;
virtual void visit(ExprStatement &expr) = 0;
virtual void visit(AssignMapStatement &assignment) = 0;
virtual void visit(AssignVarStatement &assignment) = 0;
virtual void visit(If &if_block) = 0;
virtual void visit(Unroll &unroll) = 0;
virtual void visit(Predicate &pred) = 0;
virtual void visit(AttachPoint &ap) = 0;
virtual void visit(Probe &probe) = 0;
virtual void visit(Program &program) = 0;
};
std::string opstr(Binop &binop);
std::string opstr(Unop &unop);
} // namespace ast
} // namespace bpftrace