-
Notifications
You must be signed in to change notification settings - Fork 0
/
elaborate.cxx
6453 lines (5499 loc) · 184 KB
/
elaborate.cxx
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
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// elaboration functions
// Copyright (C) 2005-2015 Red Hat Inc.
// Copyright (C) 2008 Intel Corporation
//
// 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 "elaborate.h"
#include "translate.h"
#include "parse.h"
#include "tapsets.h"
#include "session.h"
#include "util.h"
#include "task_finder.h"
#include "stapregex.h"
#include "stringtable.h"
extern "C" {
#include <sys/utsname.h>
#include <fnmatch.h>
#define __STDC_FORMAT_MACROS
#include <inttypes.h>
}
#include <algorithm>
#include <fstream>
#include <map>
#include <cassert>
#include <set>
#include <vector>
#include <algorithm>
#include <iterator>
#include <climits>
using namespace std;
// ------------------------------------------------------------------------
// Used in probe_point condition construction. Either argument may be
// NULL; if both, return NULL too. Resulting expression is a deep
// copy for symbol resolution purposes.
expression* add_condition (expression* a, expression* b)
{
if (!a && !b) return 0;
if (! a) return deep_copy_visitor::deep_copy(b);
if (! b) return deep_copy_visitor::deep_copy(a);
logical_and_expr la;
la.op = "&&";
la.left = a;
la.right = b;
la.tok = a->tok; // or could be b->tok
return deep_copy_visitor::deep_copy(& la);
}
// ------------------------------------------------------------------------
derived_probe::derived_probe (probe *p, probe_point *l, bool rewrite_loc):
base (p), base_pp(l), group(NULL), sdt_semaphore_addr(0),
session_index((unsigned)-1)
{
assert (p);
this->tok = p->tok;
this->privileged = p->privileged;
this->body = deep_copy_visitor::deep_copy(p->body);
assert (l);
// make a copy for subclasses which want to rewrite the location
if (rewrite_loc)
l = new probe_point(*l);
this->locations.push_back (l);
}
void
derived_probe::printsig (ostream& o) const
{
probe::printsig (o);
printsig_nested (o);
}
void
derived_probe::printsig_nested (ostream& o) const
{
// We'd like to enclose the probe derivation chain in a /* */
// comment delimiter. But just printing /* base->printsig() */ is
// not enough, since base might itself be a derived_probe. So we,
// er, "cleverly" encode our nesting state as a formatting flag for
// the ostream.
ios::fmtflags f = o.flags (ios::internal);
if (f & ios::internal)
{
// already nested
o << " <- ";
base->printsig (o);
}
else
{
// outermost nesting
o << " /* <- ";
base->printsig (o);
o << " */";
}
// restore flags
(void) o.flags (f);
}
void
derived_probe::collect_derivation_chain (std::vector<probe*> &probes_list) const
{
probes_list.push_back(const_cast<derived_probe*>(this));
base->collect_derivation_chain(probes_list);
}
void
derived_probe::collect_derivation_pp_chain (std::vector<probe_point*> &pp_list) const
{
pp_list.push_back(const_cast<probe_point*>(this->sole_location()));
base->collect_derivation_pp_chain(pp_list);
}
string
derived_probe::derived_locations (bool firstFrom)
{
ostringstream o;
vector<probe_point*> reference_point;
collect_derivation_pp_chain(reference_point);
if (reference_point.size() > 0)
for(unsigned i=1; i<reference_point.size(); ++i)
{
if (firstFrom || i>1)
o << " from: ";
o << reference_point[i]->str(false); // no ?,!,etc
}
return o.str();
}
probe_point*
derived_probe::sole_location () const
{
if (locations.size() == 0 || locations.size() > 1)
throw SEMANTIC_ERROR (_N("derived_probe with no locations",
"derived_probe with too many locations",
locations.size()), this->tok);
else
return locations[0];
}
probe_point*
derived_probe::script_location () const
{
// This feeds function::pn() in the tapset, which is documented as the
// script-level probe point expression, *after wildcard expansion*.
vector<probe_point*> chain;
collect_derivation_pp_chain (chain);
// Go backwards until we hit the first well-formed probe point
for (int i=chain.size()-1; i>=0; i--)
if (chain[i]->well_formed)
return chain[i];
// If that didn't work, just fallback to -something-.
return sole_location();
}
void
derived_probe::emit_privilege_assertion (translator_output* o)
{
// Emit code which will cause compilation to fail if it is compiled in
// unprivileged mode.
o->newline() << "#if ! STP_PRIVILEGE_CONTAINS (STP_PRIVILEGE, STP_PR_STAPDEV) && \\";
o->newline() << " ! STP_PRIVILEGE_CONTAINS (STP_PRIVILEGE, STP_PR_STAPSYS)";
o->newline() << "#error Internal Error: Probe ";
probe::printsig (o->line());
o->line() << " generated in --unprivileged mode";
o->newline() << "#endif";
}
void
derived_probe::emit_process_owner_assertion (translator_output* o)
{
// Emit code which will abort should the current target not belong to the
// user in unprivileged mode.
o->newline() << "#if ! STP_PRIVILEGE_CONTAINS (STP_PRIVILEGE, STP_PR_STAPDEV) && \\";
o->newline() << " ! STP_PRIVILEGE_CONTAINS (STP_PRIVILEGE, STP_PR_STAPSYS)";
o->newline(1) << "if (! is_myproc ()) {";
o->newline(1) << "snprintf(c->error_buffer, sizeof(c->error_buffer),";
o->newline() << " \"Internal Error: Process %d does not belong to user %d in probe %s in --unprivileged mode\",";
o->newline() << " current->tgid, _stp_uid, c->probe_point);";
o->newline() << "c->last_error = c->error_buffer;";
// NB: since this check occurs before probe locking, its exit should
// not be a "goto out", which would attempt unlocking.
o->newline() << "return;";
o->newline(-1) << "}";
o->newline(-1) << "#endif";
}
void
derived_probe::print_dupe_stamp_unprivileged(ostream& o)
{
o << _("unprivileged users: authorized") << endl;
}
void
derived_probe::print_dupe_stamp_unprivileged_process_owner(ostream& o)
{
o << _("unprivileged users: authorized for process owner") << endl;
}
// ------------------------------------------------------------------------
// Members of derived_probe_builder
void
derived_probe_builder::build_with_suffix(systemtap_session & sess,
probe * use,
probe_point * location,
literal_map_t const & parameters,
std::vector<derived_probe *>
& finished_results,
std::vector<probe_point::component *>
const & suffix) {
// XXX perhaps build the probe if suffix is empty?
// if (suffix.empty()) {
// build (sess, use, location, parameters, finished_results);
// return;
// }
throw SEMANTIC_ERROR (_("invalid suffix for probe"));
}
bool
derived_probe_builder::get_param (literal_map_t const & params,
interned_string key,
interned_string& value)
{
literal_map_t::const_iterator i = params.find (key);
if (i == params.end())
return false;
literal_string * ls = dynamic_cast<literal_string *>(i->second);
if (!ls)
return false;
value = ls->value;
return true;
}
bool
derived_probe_builder::get_param (literal_map_t const & params,
interned_string key,
int64_t& value)
{
literal_map_t::const_iterator i = params.find (key);
if (i == params.end())
return false;
if (i->second == NULL)
return false;
literal_number * ln = dynamic_cast<literal_number *>(i->second);
if (!ln)
return false;
value = ln->value;
return true;
}
bool
derived_probe_builder::has_null_param (literal_map_t const & params,
interned_string key)
{
literal_map_t::const_iterator i = params.find(key);
return (i != params.end() && i->second == NULL);
}
bool
derived_probe_builder::has_param (literal_map_t const & params,
interned_string key)
{
return (params.find(key) != params.end());
}
// ------------------------------------------------------------------------
// Members of match_key.
match_key::match_key(interned_string n)
: name(n),
have_parameter(false),
parameter_type(pe_unknown)
{
}
match_key::match_key(probe_point::component const & c)
: name(c.functor),
have_parameter(c.arg != NULL),
parameter_type(c.arg ? c.arg->type : pe_unknown)
{
}
match_key &
match_key::with_number()
{
have_parameter = true;
parameter_type = pe_long;
return *this;
}
match_key &
match_key::with_string()
{
have_parameter = true;
parameter_type = pe_string;
return *this;
}
string
match_key::str() const
{
string n = name;
if (have_parameter)
switch (parameter_type)
{
case pe_string: return n + "(string)";
case pe_long: return n + "(number)";
default: return n + "(...)";
}
return n;
}
bool
match_key::operator<(match_key const & other) const
{
return ((name < other.name)
|| (name == other.name
&& have_parameter < other.have_parameter)
|| (name == other.name
&& have_parameter == other.have_parameter
&& parameter_type < other.parameter_type));
}
// NB: these are only used in the probe point name components, where
// only "*" is permitted.
//
// Within module("bar"), function("foo"), process("baz") strings, real
// wildcards are permitted too. See also util.h:contains_glob_chars
static bool
isglob(interned_string str)
{
return(str.find('*') != str.npos);
}
static bool
isdoubleglob(interned_string str)
{
return(str.find("**") != str.npos);
}
bool
match_key::globmatch(match_key const & other) const
{
const char *other_str = other.name.c_str();
const char *name_str = name.c_str();
return ((fnmatch(name_str, other_str, FNM_NOESCAPE) == 0)
&& have_parameter == other.have_parameter
&& parameter_type == other.parameter_type);
}
// ------------------------------------------------------------------------
// Members of match_node
// ------------------------------------------------------------------------
match_node::match_node() :
privilege(privilege_t (pr_stapdev | pr_stapsys))
{
}
match_node *
match_node::bind(match_key const & k)
{
if (k.name == "*")
throw SEMANTIC_ERROR(_("invalid use of wildcard probe point component"));
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)
{
ends.push_back (e);
}
match_node *
match_node::bind(interned_string 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());
}
match_node *
match_node::bind_privilege(privilege_t p)
{
privilege = p;
return this;
}
void
match_node::find_and_build (systemtap_session& s,
probe* p, probe_point *loc, unsigned pos,
vector<derived_probe *>& results)
{
assert (pos <= loc->components.size());
if (pos == loc->components.size()) // matched all probe point components so far
{
if (ends.empty())
{
string alternatives;
for (sub_map_iterator_t i = sub.begin(); i != sub.end(); i++)
alternatives += string(" ") + i->first.str();
throw SEMANTIC_ERROR (_F("probe point truncated (follow: %s)",
alternatives.c_str()),
loc->components.back()->tok);
}
if (! pr_contains (privilege, s.privilege))
{
throw SEMANTIC_ERROR (_F("probe point is not allowed for --privilege=%s",
pr_name (s.privilege)),
loc->components.back()->tok);
}
literal_map_t param_map;
for (unsigned i=0; i<pos; i++)
param_map[loc->components[i]->functor] = loc->components[i]->arg;
// maybe 0
// Iterate over all bound builders
for (unsigned k=0; k<ends.size(); k++)
{
derived_probe_builder *b = ends[k];
b->build (s, p, loc, param_map, results);
}
}
else if (isdoubleglob(loc->components[pos]->functor)) // ** wildcard?
{
unsigned int num_results = results.size();
// When faced with "foo**bar", we try "foo*bar" and "foo*.**bar"
const probe_point::component *comp = loc->components[pos];
string functor = comp->functor;
size_t glob_start = functor.find("**");
size_t glob_end = functor.find_first_not_of('*', glob_start);
string prefix = functor.substr(0, glob_start);
string suffix = ((glob_end != string::npos) ?
functor.substr(glob_end) : "");
// Synthesize "foo*bar"
probe_point *simple_pp = new probe_point(*loc);
probe_point::component *simple_comp = new probe_point::component(*comp);
simple_comp->functor = prefix + "*" + suffix;
simple_comp->from_glob = true;
simple_pp->components[pos] = simple_comp;
try
{
find_and_build (s, p, simple_pp, pos, results);
}
catch (const semantic_error& e)
{
// Ignore semantic_errors.
}
// Cleanup if we didn't find anything
if (results.size() == num_results)
{
delete simple_pp;
delete simple_comp;
}
num_results = results.size();
// Synthesize "foo*.**bar"
// NB: any component arg should attach to the latter part only
probe_point *expanded_pp = new probe_point(*loc);
probe_point::component *expanded_comp_pre = new probe_point::component(*comp);
expanded_comp_pre->functor = prefix + "*";
expanded_comp_pre->from_glob = true;
expanded_comp_pre->arg = NULL;
probe_point::component *expanded_comp_post = new probe_point::component(*comp);
expanded_comp_post->functor = string("**") + suffix;
expanded_pp->components[pos] = expanded_comp_pre;
expanded_pp->components.insert(expanded_pp->components.begin() + pos + 1,
expanded_comp_post);
try
{
find_and_build (s, p, expanded_pp, pos, results);
}
catch (const semantic_error& e)
{
// Ignore semantic_errors.
}
// Cleanup if we didn't find anything
if (results.size() == num_results)
{
delete expanded_pp;
delete expanded_comp_pre;
delete expanded_comp_post;
}
// Try suffix expansion only if no matches found:
if (num_results == results.size())
this->try_suffix_expansion (s, p, loc, pos, results);
if (! loc->optional && num_results == results.size())
{
// We didn't find any wildcard matches (since the size of
// the result vector didn't change). Throw an error.
string sugs = suggest_functors(s, functor);
throw SEMANTIC_ERROR (_F("probe point mismatch: didn't find any wildcard matches%s",
sugs.empty() ? "" : (" (similar: " + sugs + ")").c_str()),
comp->tok);
}
}
else if (isglob(loc->components[pos]->functor)) // wildcard?
{
match_key match (* loc->components[pos]);
// Call find_and_build for each possible match. Ignore errors -
// unless we don't find any match.
unsigned int num_results = results.size();
for (sub_map_iterator_t i = sub.begin(); i != sub.end(); i++)
{
const match_key& subkey = i->first;
match_node* subnode = i->second;
assert_no_interrupts();
if (match.globmatch(subkey))
{
if (s.verbose > 2)
clog << _F("wildcard '%s' matched '%s'",
loc->components[pos]->functor.c_str(),
subkey.name.c_str()) << endl;
// When we have a wildcard, we need to create a copy of
// the probe point. Then we'll create a copy of the
// wildcard component, and substitute the non-wildcard
// functor.
probe_point *non_wildcard_pp = new probe_point(*loc);
probe_point::component *non_wildcard_component
= new probe_point::component(*loc->components[pos]);
non_wildcard_component->functor = subkey.name;
non_wildcard_component->from_glob = true;
non_wildcard_pp->components[pos] = non_wildcard_component;
// NB: probe conditions are not attached at the wildcard
// (component/functor) level, but at the overall
// probe_point level.
unsigned int inner_results = results.size();
// recurse (with the non-wildcard probe point)
try
{
subnode->find_and_build (s, p, non_wildcard_pp, pos+1,
results);
}
catch (const semantic_error& e)
{
// Ignore semantic_errors while expanding wildcards.
// If we get done and nothing was expanded, the code
// following the loop will complain.
}
if (results.size() == inner_results)
{
// If this wildcard didn't match, cleanup.
delete non_wildcard_pp;
delete non_wildcard_component;
}
}
}
// Try suffix expansion only if no matches found:
if (num_results == results.size())
this->try_suffix_expansion (s, p, loc, pos, results);
if (! loc->optional && num_results == results.size())
{
// We didn't find any wildcard matches (since the size of
// the result vector didn't change). Throw an error.
string sugs = suggest_functors(s, loc->components[pos]->functor);
throw SEMANTIC_ERROR (_F("probe point mismatch: didn't find any wildcard matches%s",
sugs.empty() ? "" : (" (similar: " + sugs + ")").c_str()),
loc->components[pos]->tok);
}
}
else
{
match_key match (* loc->components[pos]);
sub_map_iterator_t i = sub.find (match);
if (i != sub.end()) // match found
{
match_node* subnode = i->second;
// recurse
subnode->find_and_build (s, p, loc, pos+1, results);
return;
}
unsigned int num_results = results.size();
this->try_suffix_expansion (s, p, loc, pos, results);
// XXX: how to correctly report alternatives + position numbers
// for alias suffixes? file a separate PR to address the issue
if (! loc->optional && num_results == results.size())
{
// We didn't find any alias suffixes (since the size of the
// result vector didn't change). Throw an error.
string sugs = suggest_functors(s, loc->components[pos]->functor);
throw SEMANTIC_ERROR (_F("probe point mismatch%s",
sugs.empty() ? "" : (" (similar: " + sugs + ")").c_str()),
loc->components[pos]->tok);
}
}
}
string
match_node::suggest_functors(systemtap_session& s, string functor)
{
// only use prefix if globby (and prefix is non-empty)
size_t glob = functor.find('*');
if (glob != string::npos && glob != 0)
functor.erase(glob);
if (functor.empty())
return "";
// PR18577: There isn't any point in generating a suggestion list if
// we're not going to display it.
if ((s.dump_mode == systemtap_session::dump_matched_probes
|| s.dump_mode == systemtap_session::dump_matched_probes_vars)
&& s.verbose < 2)
return "";
set<string> functors;
for (sub_map_iterator_t i = sub.begin(); i != sub.end(); i++)
{
string ftor = i->first.str();
if (ftor.find('(') != string::npos) // trim any parameter
ftor.erase(ftor.find('('));
functors.insert(ftor);
}
return levenshtein_suggest(functor, functors, 5); // print top 5
}
void
match_node::try_suffix_expansion (systemtap_session& s,
probe *p, probe_point *loc, unsigned pos,
vector<derived_probe *>& results)
{
// PR12210: match alias suffixes. If the components thus far
// have been matched, but there is an additional unknown
// suffix, we have a potential alias suffix on our hands. We
// need to expand the preceding components as probe aliases,
// reattach the suffix, and re-run derive_probes() on the
// resulting expansion. This is done by the routine
// build_with_suffix().
if (strverscmp(s.compatible.c_str(), "2.0") >= 0)
{
// XXX: technically, param_map isn't used here. So don't
// bother actually assembling it unless some
// derived_probe_builder appears that actually takes
// suffixes *and* consults parameters (currently no such
// builders exist).
literal_map_t param_map;
// for (unsigned i=0; i<pos; i++)
// param_map[loc->components[i]->functor] = loc->components[i]->arg;
// maybe 0
vector<probe_point::component *> suffix (loc->components.begin()+pos,
loc->components.end());
// Multiple derived_probe_builders may be bound at a
// match_node due to the possibility of multiply defined
// aliases.
for (unsigned k=0; k < ends.size(); k++)
{
derived_probe_builder *b = ends[k];
try
{
b->build_with_suffix (s, p, loc, param_map, results, suffix);
}
catch (const recursive_expansion_error &e)
{
// Re-throw:
throw semantic_error(e);
}
catch (const semantic_error &e)
{
// Adjust source coordinate and re-throw:
if (! loc->optional)
throw semantic_error(e.errsrc, e.what(), loc->components[pos]->tok);
}
}
}
}
void
match_node::build_no_more (systemtap_session& s)
{
for (sub_map_iterator_t i = sub.begin(); i != sub.end(); i++)
i->second->build_no_more (s);
for (unsigned k=0; k<ends.size(); k++)
{
derived_probe_builder *b = ends[k];
b->build_no_more (s);
}
}
void
match_node::dump (systemtap_session &s, const string &name)
{
// Dump this node, if it is complete.
for (unsigned k=0; k<ends.size(); k++)
{
// Don't print aliases at all (for now) until we can figure out how to determine whether
// the probes they resolve to are ok in unprivileged mode.
if (ends[k]->is_alias ())
continue;
// In unprivileged mode, don't show the probes which are not allowed for unprivileged
// users.
if (pr_contains (privilege, s.privilege))
{
cout << name << endl;
break; // we need only print one instance.
}
}
// Recursively dump the children of this node
string dot;
if (! name.empty ())
dot = ".";
for (sub_map_iterator_t i = sub.begin(); i != sub.end(); i++)
{
i->second->dump (s, name + dot + i->first.str());
}
}
// ------------------------------------------------------------------------
// Alias probes
// ------------------------------------------------------------------------
struct alias_derived_probe: public derived_probe
{
alias_derived_probe (probe* base, probe_point *l, const probe_alias *a,
const vector<probe_point::component *> *suffix = 0);
~alias_derived_probe();
void upchuck () { throw SEMANTIC_ERROR (_("inappropriate"), this->tok); }
// Alias probes are immediately expanded to other derived_probe
// types, and are not themselves emitted or listed in
// systemtap_session.probes
void join_group (systemtap_session&) { upchuck (); }
virtual const probe_alias *get_alias () const { return alias; }
virtual probe_point *get_alias_loc () const { return alias_loc; }
virtual probe_point *sole_location () const;
private:
const probe_alias *alias; // Used to check for recursion
probe_point *alias_loc; // Hack to recover full probe name
};
alias_derived_probe::alias_derived_probe(probe *base, probe_point *l,
const probe_alias *a,
const vector<probe_point::component *>
*suffix):
derived_probe (base, l), alias(a)
{
// XXX pretty nasty -- this was cribbed from printscript() in main.cxx
assert (alias->alias_names.size() >= 1);
alias_loc = new probe_point(*alias->alias_names[0]); // XXX: [0] is arbitrary; it would make just as much sense to collect all of the names
alias_loc->well_formed = true;
vector<probe_point::component*>::const_iterator it;
for (it = suffix->begin(); it != suffix->end(); ++it)
{
alias_loc->components.push_back(*it);
if (isglob((*it)->functor))
alias_loc->well_formed = false; // needs further derivation
}
}
alias_derived_probe::~alias_derived_probe ()
{
delete alias_loc;
}
probe_point*
alias_derived_probe::sole_location () const
{
return const_cast<probe_point*>(alias_loc);
}
void
alias_expansion_builder::build(systemtap_session & sess,
probe * use,
probe_point * location,
literal_map_t const & parameters,
vector<derived_probe *> & finished_results)
{
vector<probe_point::component *> empty_suffix;
build_with_suffix (sess, use, location, parameters,
finished_results, empty_suffix);
}
void
alias_expansion_builder::build_with_suffix(systemtap_session & sess,
probe * use,
probe_point * location,
literal_map_t const &,
vector<derived_probe *>
& finished_results,
vector<probe_point::component *>
const & suffix)
{
// Don't build the alias expansion if infinite recursion is detected.
if (checkForRecursiveExpansion (use)) {
stringstream msg;
msg << _F("recursive loop in alias expansion of %s at %s",
lex_cast(*location).c_str(), lex_cast(location->components.front()->tok->location).c_str());
// semantic_errors thrown here might be ignored, so we need a special class:
throw recursive_expansion_error (msg.str());
// XXX The point of throwing this custom error is to suppress a
// cascade of "probe mismatch" messages that appear in addition to
// the error. The current approach suppresses most of the error
// cascade, but leaves one spurious error; in any case, the way
// this particular error is reported could be improved.
}
// We're going to build a new probe and wrap it up in an
// alias_expansion_probe so that the expansion loop recognizes it as
// such and re-expands its expansion.
alias_derived_probe * n = new alias_derived_probe (use, location /* soon overwritten */, this->alias, &suffix);
n->body = new block();
// The new probe gets a deep copy of the location list of the alias
// (with incoming condition joined) plus the suffix (if any),
n->locations.clear();
for (unsigned i=0; i<alias->locations.size(); i++)
{
probe_point *pp = new probe_point(*alias->locations[i]);
pp->components.insert(pp->components.end(), suffix.begin(), suffix.end());
pp->condition = add_condition (pp->condition, location->condition);
n->locations.push_back(pp);
}
// the token location of the alias,
n->tok = location->components.front()->tok;
// and statements representing the concatenation of the alias'
// body with the use's.
//
// NB: locals are *not* copied forward, from either alias or
// use. The expansion should have its locals re-inferred since
// there's concatenated code here and we only want one vardecl per
// resulting variable.
if (alias->epilogue_style)
n->body = new block (use->body, alias->body);
else
n->body = new block (alias->body, use->body);
unsigned old_num_results = finished_results.size();
// If expanding for an alias suffix, be sure to pass on any errors
// to the caller instead of printing them in derive_probes():
derive_probes (sess, n, finished_results, location->optional, !suffix.empty());
// Check whether we resolved something. If so, put the
// whole library into the queue if not already there.
if (finished_results.size() > old_num_results)
{
stapfile *f = alias->tok->location.file;
if (find (sess.files.begin(), sess.files.end(), f)
== sess.files.end())
sess.files.push_back (f);
}
}
bool
alias_expansion_builder::checkForRecursiveExpansion (probe *use)
{
// Collect the derivation chain of this probe.
vector<probe*>derivations;
use->collect_derivation_chain (derivations);
// Check all probe points in the alias expansion against the currently-being-expanded probe point
// of each of the probes in the derivation chain, looking for a match. This
// indicates infinite recursion.
// The first element of the derivation chain will be the derived_probe representing 'use', so
// start the search with the second element.
assert (derivations.size() > 0);
assert (derivations[0] == use);
for (unsigned d = 1; d < derivations.size(); ++d) {
if (use->get_alias() == derivations[d]->get_alias())
return true; // recursion detected
}
return false;
}
// ------------------------------------------------------------------------
// Pattern matching
// ------------------------------------------------------------------------
static unsigned max_recursion = 100;
struct
recursion_guard
{
unsigned & i;
recursion_guard(unsigned & i) : i(i)
{
if (i > max_recursion)
throw SEMANTIC_ERROR(_("recursion limit reached"));
++i;
}
~recursion_guard()
{
--i;
}
};
// The match-and-expand loop.
void
derive_probes (systemtap_session& s,
probe *p, vector<derived_probe*>& dps,
bool optional,
bool rethrow_errors)
{
// We need a static to track whether the current probe is optional so that
// even if we recurse into derive_probes with optional = false, errors will
// still be ignored. The undo_parent_optional bool ensures we reset the
// static at the same level we had it set.
static bool parent_optional = false;
bool undo_parent_optional = false;
if (optional && !parent_optional)
{
parent_optional = true;
undo_parent_optional = true;
}
vector <semantic_error> optional_errs;
for (unsigned i = 0; i < p->locations.size(); ++i)
{
assert_no_interrupts();
probe_point *loc = p->locations[i];