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
382
383
384
385
386
387
388
389
390
391
392
|
// tapset resolution
// Copyright (C) 2005 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.
#include "config.h"
#include "staptree.h"
#include "elaborate.h"
#include "tapsets.h"
#include "translate.h"
#include <iostream>
#include <sstream>
#include <deque>
#include <vector>
#include <map>
using namespace std;
// match_key
match_key::match_key(string const & n)
: name(n),
have_parameter(false),
parameter_type(tok_junk)
{
}
match_key::match_key(probe_point::component const & c)
: name(c.functor),
have_parameter(c.arg != NULL),
parameter_type(c.arg ? c.arg->tok->type : tok_junk)
{
}
match_key &
match_key::with_number()
{
have_parameter = true;
parameter_type = tok_number;
return *this;
}
match_key &
match_key::with_string()
{
have_parameter = true;
parameter_type = tok_string;
return *this;
}
string
match_key::str() const
{
if (have_parameter)
switch (parameter_type)
{
case tok_string: return name + "(string)";
case tok_number: return name + "(number)";
default: return name + "(...)";
}
return name;
}
bool
match_key::operator<(match_key const & other) const
{
return ((name < other.name)
|| (name == name
&& have_parameter < other.have_parameter)
|| (name == name
&& have_parameter == other.have_parameter
&& parameter_type < other.parameter_type));
}
// match_node
match_node::match_node()
: end(NULL)
{}
match_node &
match_node::bind(match_key const & k)
{
map<match_key, match_node *>::const_iterator i = sub.find(k);
if (i != sub.end())
return *i->second;
match_node * n = new match_node();
sub.insert(make_pair(k, n));
return *n;
}
void
match_node::bind(derived_probe_builder * e)
{
if (end)
throw semantic_error("already have a pattern ending");
end = e;
}
match_node &
match_node::bind(string const & k)
{
return bind(match_key(k));
}
match_node &
match_node::bind_str(string const & k)
{
return bind(match_key(k).with_string());
}
match_node &
match_node::bind_num(string const & k)
{
return bind(match_key(k).with_number());
}
derived_probe_builder *
match_node::find_builder(vector<probe_point::component *> const & components,
unsigned pos,
vector< pair<string, literal *> > & parameters)
{
assert(pos <= components.size());
if (pos == components.size())
{
// probe_point ends here. we match iff we have
// an "end" entry here. if we don't, it'll be null.
return end;
}
else
{
// probe_point contains a component here. we match iff there's
// an entry in the sub table, and its value matches the rest
// of the probe_point.
match_key k(*components[pos]);
map<match_key, match_node *>::const_iterator i = sub.find(k);
if (i == sub.end())
return NULL;
else
{
derived_probe_builder * builder = NULL;
if (k.have_parameter)
{
assert(components[pos]->arg);
parameters.push_back(make_pair(components[pos]->functor,
components[pos]->arg));
}
builder = i->second->find_builder(components, pos+1, parameters);
if (k.have_parameter && !builder)
parameters.pop_back();
return builder;
}
}
}
static void
param_vec_to_map(vector< pair<string, literal *> > const & param_vec,
map<string, literal *> & param_map)
{
for (vector< pair<string, literal *> >::const_iterator i = param_vec.begin();
i != param_vec.end(); ++i)
{
param_map[i->first] = i->second;
}
}
// XXX: bind patterns for probe aliases found in AST
struct
alias_derived_probe
{
alias_derived_probe(probe_point * expansion)
: alias_expansion(expansion) {}
probe_point * alias_expansion;
void emit_registrations (translator_output* o, unsigned i) {}
void emit_deregistrations (translator_output* o, unsigned i) {}
void emit_probe_entries (translator_output* o, unsigned i) {}
};
// the root of the global pattern-matching tree
static match_node * root_node;
// the match-and-expand loop
void
symresolution_info::derive_probes (probe *p, vector<derived_probe*>& dps)
{
if (!root_node)
{
root_node = new match_node();
register_standard_tapsets(*root_node);
}
assert(root_node);
deque<probe_point *> work(p->locations.begin(), p->locations.end());
while(!work.empty())
{
probe_point *loc = work.front();
work.pop_front();
vector< pair<string, literal *> > param_vec;
map<string, literal *> param_map;
derived_probe_builder * builder =
root_node->find_builder(loc->components, 0, param_vec);
if (!builder)
throw semantic_error ("no match for probe point", loc->tok);
param_vec_to_map(param_vec, param_map);
derived_probe *derived = builder->build(p, loc, param_map);
assert(derived);
// append to worklist if it's an alias; append to result otherwise
alias_derived_probe *as_alias = dynamic_cast<alias_derived_probe *>(derived);
if (as_alias)
{
work.push_back(as_alias->alias_expansion);
delete derived;
}
else
dps.push_back (derived);
}
}
// ------------------------------------------------------------------------
// begin/end probes are run right during registration / deregistration
// ------------------------------------------------------------------------
struct be_derived_probe: public derived_probe
{
bool begin;
be_derived_probe (probe* p, bool b): derived_probe (p), begin (b) {}
be_derived_probe (probe* p, probe_point* l, bool b):
derived_probe (p, l), begin (b) {}
void emit_registrations (translator_output* o, unsigned i);
void emit_deregistrations (translator_output* o, unsigned i);
void emit_probe_entries (translator_output* o, unsigned i);
};
struct
be_builder
: public derived_probe_builder
{
bool begin;
be_builder(bool b) : begin(b) {}
virtual derived_probe * build(probe * base,
probe_point * location,
map<string, literal *> const & parameters)
{
return new be_derived_probe(base, location, begin);
}
virtual ~be_builder() {}
};
void
be_derived_probe::emit_registrations (translator_output* o, unsigned j)
{
if (begin)
for (unsigned i=0; i<locations.size(); i++)
{
o->newline() << "enter_" << j << "_" << i << " ()";
o->newline() << "rc = errorcount;";
}
else
o->newline() << "rc = 0;";
}
void
be_derived_probe::emit_deregistrations (translator_output* o, unsigned j)
{
if (begin)
o->newline() << "rc = 0;";
else
for (unsigned i=0; i<locations.size(); i++)
{
o->newline() << "enter_" << j << "_" << i << " ()";
o->newline() << "rc = errorcount;";
}
}
void
be_derived_probe::emit_probe_entries (translator_output* o, unsigned j)
{
for (unsigned i=0; i<locations.size(); i++)
{
probe_point *l = locations[i];
o->newline() << "/* location " << i << ": " << *l << " */";
o->newline() << "static void enter_" << j << "_" << i << " ()";
o->newline() << "{";
o->newline(1) << "struct context* c = & contexts [0];";
// XXX: assert #0 is free; need locked search instead
o->newline() << "if (c->busy) { errorcount ++; return; }";
o->newline() << "c->busy ++;";
o->newline() << "c->actioncount = 0;";
o->newline() << "c->nesting = 0;";
// NB: locals are initialized by probe function itself
o->newline() << "probe_" << j << " (c);";
o->newline() << "c->busy --;";
o->newline(-1) << "}" << endl;
}
}
// ------------------------------------------------------------------------
// dwarf derived probes XXX: todo dwfl integration
// ------------------------------------------------------------------------
struct dwarf_derived_probe: public derived_probe
{
bool kernel;
map<string, literal *> params;
dwarf_derived_probe (probe* p, probe_point* l, bool kernel,
map<string, literal *> const & params):
derived_probe (p, l),
kernel (kernel) {}
virtual void emit_registrations (translator_output* o, unsigned i);
virtual void emit_deregistrations (translator_output* o, unsigned i);
virtual void emit_probe_entries (translator_output* o, unsigned i);
virtual ~dwarf_derived_probe() {}
};
struct
dwarf_builder
: public derived_probe_builder
{
bool begin;
dwarf_builder(bool b) : begin(b) {}
virtual derived_probe * build(probe * base,
probe_point * location,
map<string, literal *> const & parameters)
{
return new dwarf_derived_probe(base, location, begin, parameters);
}
virtual ~dwarf_builder() {}
};
void
dwarf_derived_probe::emit_registrations (translator_output* o, unsigned i)
{
}
void
dwarf_derived_probe::emit_deregistrations (translator_output* o, unsigned i)
{
}
void
dwarf_derived_probe::emit_probe_entries (translator_output* o, unsigned i)
{
}
// ------------------------------------------------------------------------
// standard tapset registry
// ------------------------------------------------------------------------
void
register_standard_tapsets(match_node & root)
{
// rudimentary binders for begin and end targets
root.bind("begin").bind(new be_builder(true));
root.bind("end").bind(new be_builder(false));
// various flavours of dwarf lookup (on the kernel)
dwarf_builder *kern = new dwarf_builder(true);
root.bind("kernel").bind_str("function").bind(kern);
root.bind("kernel").bind_str("function").bind_num("line").bind(kern);
root.bind_str("module").bind(kern);
root.bind_str("module").bind_str("function").bind(kern);
}
|