-
Notifications
You must be signed in to change notification settings - Fork 3
/
parse.cxx
3772 lines (3262 loc) · 101 KB
/
parse.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
// recursive descent parser for systemtap scripts
// Copyright (C) 2005-2013 Red Hat Inc.
// Copyright (C) 2006 Intel Corporation.
// Copyright (C) 2007 Bull S.A.S
//
// 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 "parse.h"
#include "session.h"
#include "util.h"
#include <iostream>
#include <fstream>
#include <cctype>
#include <cstdlib>
#include <cassert>
#include <cerrno>
#include <climits>
#include <sstream>
#include <cstring>
#include <cctype>
#include <iterator>
extern "C" {
#include <fnmatch.h>
}
using namespace std;
class lexer
{
public:
bool ate_comment; // current token follows a comment
bool ate_whitespace; // the most recent token followed whitespace
bool saw_tokens; // the lexer found tokens (before preprocessing occurred)
token* scan ();
lexer (istream&, const string&, systemtap_session&);
void set_current_file (stapfile* f);
static set<string> keywords;
static set<string> atwords;
private:
inline int input_get ();
inline int input_peek (unsigned n=0);
void input_put (const string&, const token*);
string input_name;
string input_contents;
const char *input_pointer; // index into input_contents
const char *input_end;
unsigned cursor_suspend_count;
unsigned cursor_suspend_line;
unsigned cursor_suspend_column;
unsigned cursor_line;
unsigned cursor_column;
systemtap_session& session;
stapfile* current_file;
};
class parser
{
public:
parser (systemtap_session& s, const string& n, istream& i, bool p);
~parser ();
stapfile* parse (bool errs_as_warnings);
stapfile* parse_library_macros (bool errs_as_warnings);
private:
typedef enum {
PP_NONE,
PP_KEEP_THEN,
PP_SKIP_THEN,
PP_KEEP_ELSE,
PP_SKIP_ELSE,
} pp_state_t;
struct pp1_activation;
struct pp_macrodecl : public macrodecl {
pp1_activation* parent_act; // used for param bindings
virtual bool is_closure() { return parent_act != 0; }
pp_macrodecl () : macrodecl(), parent_act(0) { }
};
systemtap_session& session;
string input_name;
lexer input;
bool privileged;
parse_context context;
// preprocessing subordinate, first pass (macros)
struct pp1_activation {
const token* tok;
unsigned cursor; // position within macro body
map<string, pp_macrodecl*> params;
macrodecl* curr_macro;
pp1_activation (const token* tok, macrodecl* curr_macro)
: tok(tok), cursor(0), curr_macro(curr_macro) { }
~pp1_activation ();
};
map<string, macrodecl*> pp1_namespace;
vector<pp1_activation*> pp1_state;
const token* next_pp1 ();
const token* scan_pp1 ();
const token* slurp_pp1_param (vector<const token*>& param);
const token* slurp_pp1_body (vector<const token*>& body);
// preprocessing subordinate, final pass (conditionals)
vector<pair<const token*, pp_state_t> > pp_state;
const token* scan_pp ();
const token* skip_pp ();
// scanning state
const token* next ();
const token* peek ();
// Advance past and throw away current token after peek () or next ().
void swallow ();
const token* systemtap_v_seen;
const token* last_t; // the last value returned by peek() or next()
const token* next_t; // lookahead token
// expectations, these swallow the token
void expect_known (token_type tt, string const & expected);
void expect_unknown (token_type tt, string & target);
void expect_unknown2 (token_type tt1, token_type tt2, string & target);
// convenience forms, these also swallow the token
void expect_op (string const & expected);
void expect_kw (string const & expected);
void expect_number (int64_t & expected);
void expect_ident_or_keyword (string & target);
// convenience forms, which return true or false, these don't swallow token
bool peek_op (string const & op);
bool peek_kw (string const & kw);
// convenience forms, which return the token
const token* expect_kw_token (string const & expected);
const token* expect_ident_or_atword (string & target);
void print_error (const parse_error& pe, bool errs_as_warnings = false);
unsigned num_errors;
private: // nonterminals
void parse_probe (vector<probe*>&, vector<probe_alias*>&);
void parse_global (vector<vardecl*>&, vector<probe*>&);
void parse_functiondecl (vector<functiondecl*>&);
embeddedcode* parse_embeddedcode ();
probe_point* parse_probe_point ();
literal_string* consume_string_literals (const token*);
literal_string* parse_literal_string ();
literal* parse_literal ();
block* parse_stmt_block ();
try_block* parse_try_block ();
statement* parse_statement ();
if_statement* parse_if_statement ();
for_loop* parse_for_loop ();
for_loop* parse_while_loop ();
foreach_loop* parse_foreach_loop ();
expr_statement* parse_expr_statement ();
return_statement* parse_return_statement ();
delete_statement* parse_delete_statement ();
next_statement* parse_next_statement ();
break_statement* parse_break_statement ();
continue_statement* parse_continue_statement ();
indexable* parse_indexable ();
const token *parse_hist_op_or_bare_name (hist_op *&hop, string &name);
target_symbol *parse_target_symbol (const token* t);
expression* parse_entry_op (const token* t);
expression* parse_defined_op (const token* t);
expression* parse_perf_op (const token* t);
expression* parse_expression ();
expression* parse_assignment ();
expression* parse_ternary ();
expression* parse_logical_or ();
expression* parse_logical_and ();
expression* parse_boolean_or ();
expression* parse_boolean_xor ();
expression* parse_boolean_and ();
expression* parse_array_in ();
expression* parse_comparison_or_regex_query ();
expression* parse_shift ();
expression* parse_concatenation ();
expression* parse_additive ();
expression* parse_multiplicative ();
expression* parse_unary ();
expression* parse_crement ();
expression* parse_value ();
expression* parse_symbol ();
void parse_target_symbol_components (target_symbol* e);
};
// ------------------------------------------------------------------------
stapfile*
parse (systemtap_session& s, istream& i, bool pr, bool errs_as_warnings)
{
parser p (s, "<input>", i, pr);
return p.parse (errs_as_warnings);
}
stapfile*
parse (systemtap_session& s, const string& name, bool pr, bool errs_as_warnings)
{
ifstream i(name.c_str(), ios::in);
if (i.fail())
{
cerr << (file_exists(name)
? _F("Input file '%s' can't be opened for reading.", name.c_str())
: _F("Input file '%s' is missing.", name.c_str()))
<< endl;
return 0;
}
parser p (s, name, i, pr);
return p.parse (errs_as_warnings);
}
stapfile*
parse_library_macros (systemtap_session& s, const string& name, bool errs_as_warnings)
{
ifstream i(name.c_str(), ios::in);
if (i.fail())
{
cerr << (file_exists(name)
? _F("Input file '%s' can't be opened for reading.", name.c_str())
: _F("Input file '%s' is missing.", name.c_str()))
<< endl;
return 0;
}
parser p (s, name, i, false); // TODOXX pr is ...? should path be full??
return p.parse_library_macros (errs_as_warnings);
}
// ------------------------------------------------------------------------
parser::parser (systemtap_session& s, const string &n, istream& i, bool p):
session (s), input_name (n), input (i, input_name, s), privileged (p),
context(con_unknown), systemtap_v_seen(0), last_t (0), next_t (0), num_errors (0)
{
}
parser::~parser()
{
}
static string
tt2str(token_type tt)
{
switch (tt)
{
case tok_junk: return "junk";
case tok_identifier: return "identifier";
case tok_operator: return "operator";
case tok_string: return "string";
case tok_number: return "number";
case tok_embedded: return "embedded-code";
case tok_keyword: return "keyword";
}
return "unknown token";
}
ostream&
operator << (ostream& o, const source_loc& loc)
{
o << loc.file->name << ":"
<< loc.line << ":"
<< loc.column;
return o;
}
ostream&
operator << (ostream& o, const token& t)
{
o << tt2str(t.type);
if (t.type != tok_embedded && t.type != tok_keyword) // XXX: other types?
{
o << " '";
for (unsigned i=0; i<t.content.length(); i++)
{
char c = t.content[i];
o << (isprint (c) ? c : '?');
}
o << "'";
}
o << " at "
<< t.location;
return o;
}
void
parser::print_error (const parse_error &pe, bool errs_as_warnings)
{
const token *tok = pe.tok ? pe.tok : last_t;
session.print_error(pe, tok, input_name, errs_as_warnings);
num_errors ++;
}
template <typename OPERAND>
bool eval_comparison (const OPERAND& lhs, const token* op, const OPERAND& rhs)
{
if (op->type == tok_operator && op->content == "<=")
{ return lhs <= rhs; }
else if (op->type == tok_operator && op->content == ">=")
{ return lhs >= rhs; }
else if (op->type == tok_operator && op->content == "<")
{ return lhs < rhs; }
else if (op->type == tok_operator && op->content == ">")
{ return lhs > rhs; }
else if (op->type == tok_operator && op->content == "==")
{ return lhs == rhs; }
else if (op->type == tok_operator && op->content == "!=")
{ return lhs != rhs; }
else
throw PARSE_ERROR (_("expected comparison operator"), op);
}
// Here, we perform on-the-fly preprocessing in two passes.
// First pass - macro declaration and expansion.
//
// The basic form of a declaration is @define SIGNATURE %( BODY %)
// where SIGNATURE is of the form macro_name (a, b, c, ...)
// and BODY can obtain the parameter contents as @a, @b, @c, ....
// Note that parameterless macros can also be declared.
//
// Macro definitions may not be nested.
// A macro is available textually after it has been defined.
//
// The basic form of a macro invocation
// for a parameterless macro is @macro_name,
// for a macro with parameters is @macro_name(param_1, param_2, ...).
//
// NB: this means that a parameterless macro @foo called as @foo(a, b, c)
// leaves its 'parameters' alone, rather than consuming them to result
// in a "too many parameters error". This may be useful in the unusual
// case of wanting @foo to expand to the name of a function.
//
// Invocations of unknown macros are left unexpanded, to allow
// the continued use of constructs such as @cast, @var, etc.
macrodecl::~macrodecl ()
{
delete tok;
for (vector<const token*>::iterator it = body.begin();
it != body.end(); it++)
delete *it;
}
parser::pp1_activation::~pp1_activation ()
{
delete tok;
if (curr_macro->is_closure()) return; // body is shared with an earlier declaration
for (map<string, pp_macrodecl*>::iterator it = params.begin();
it != params.end(); it++)
delete it->second;
}
// Grab a token from the current input source (main file or macro body):
const token*
parser::next_pp1 ()
{
if (pp1_state.empty())
return input.scan ();
// otherwise, we're inside a macro
pp1_activation* act = pp1_state.back();
unsigned& cursor = act->cursor;
if (cursor < act->curr_macro->body.size())
{
token* t = new token(*act->curr_macro->body[cursor]);
t->chain = new token(*act->tok); // mark chained token
cursor++;
return t;
}
else
return 0; // reached end of macro body
}
const token*
parser::scan_pp1 ()
{
while (true)
{
const token* t = next_pp1 ();
if (t == 0) // EOF or end of macro body
{
if (pp1_state.empty()) // actual EOF
return 0;
// Exit macro and loop around to look for the next token.
pp1_activation* act = pp1_state.back();
pp1_state.pop_back(); delete act;
continue;
}
// macro definition
if (t->type == tok_operator && t->content == "@define")
{
if (!pp1_state.empty())
throw PARSE_ERROR (_("'@define' forbidden inside macro body"), t);
delete t;
// handle macro definition
// (1) consume macro signature
t = input.scan();
if (! (t && t->type == tok_identifier))
throw PARSE_ERROR (_("expected identifier"), t);
string name = t->content;
// check for redefinition of existing macro
if (pp1_namespace.find(name) != pp1_namespace.end())
{
parse_error er (ERR_SRC, _F("attempt to redefine macro '@%s' in the same file", name.c_str ()), t);
// Also point to pp1_namespace[name]->tok, the site of
// the original definition:
er.chain = new PARSE_ERROR (_F("macro '@%s' first defined here",
name.c_str()), pp1_namespace[name]->tok);
throw er;
}
// XXX: the above restriction was mostly necessary due to
// wanting to leave open the possibility of
// statically-scoped semantics in the future.
// XXX: this cascades into further parse errors as the
// parser tries to parse the remaining definition... (e.g.
// it can't tell that the macro body isn't a conditional,
// that the uses of parameters aren't nonexistent
// macros.....)
if (name == "define")
throw PARSE_ERROR (_("attempt to redefine '@define'"), t);
if (input.atwords.count("@" + name))
session.print_warning (_F("macro redefines built-in operator '@%s'", name.c_str()), t);
macrodecl* decl = (pp1_namespace[name] = new macrodecl);
decl->tok = t;
// determine if the macro takes parameters
bool saw_params = false;
t = input.scan();
if (t && t->type == tok_operator && t->content == "(")
{
saw_params = true;
do
{
delete t;
t = input.scan ();
if (! (t && t->type == tok_identifier))
throw PARSE_ERROR(_("expected identifier"), t);
decl->formal_args.push_back(t->content);
delete t;
t = input.scan ();
if (t && t->type == tok_operator && t->content == ",")
{
continue;
}
else if (t && t->type == tok_operator && t->content == ")")
{
delete t;
t = input.scan();
break;
}
else
{
throw PARSE_ERROR (_("expected ',' or ')'"), t);
}
}
while (true);
}
// (2) identify & consume macro body
if (! (t && t->type == tok_operator && t->content == "%("))
{
if (saw_params)
throw PARSE_ERROR (_("expected '%('"), t);
else
throw PARSE_ERROR (_("expected '%(' or '('"), t);
}
delete t;
t = slurp_pp1_body (decl->body);
if (!t)
throw PARSE_ERROR (_("incomplete macro definition - missing '%)'"), decl->tok);
delete t;
// Now loop around to look for a real token.
continue;
}
// (potential) macro invocation
if (t->type == tok_operator && t->content[0] == '@')
{
string name = t->content.substr(1); // strip initial '@'
// check if name refers to a real parameter or macro
macrodecl* decl;
pp1_activation* act = pp1_state.empty() ? 0 : pp1_state.back();
if (act && act->params.find(name) != act->params.end())
decl = act->params[name];
else if (!(act && act->curr_macro->context == ctx_library)
&& pp1_namespace.find(name) != pp1_namespace.end())
decl = pp1_namespace[name];
else if (session.library_macros.find(name)
!= session.library_macros.end())
decl = session.library_macros[name];
else // this is an ordinary @operator
return t;
// handle macro invocation, taking ownership of t
pp1_activation *new_act = new pp1_activation(t, decl);
unsigned num_params = decl->formal_args.size();
// (1a) restore parameter invocation closure
if (num_params == 0 && decl->is_closure())
{
// NB: decl->parent_act is always safe since the
// parameter decl (if any) comes from an activation
// record which deeper in the stack than new_act.
// decl is a macro parameter which must be evaluated in
// the context of the original point of invocation:
new_act->params = ((pp_macrodecl*)decl)->parent_act->params;
goto expand;
}
// (1b) consume macro parameters (if any)
if (num_params == 0)
goto expand;
// for simplicity, we do not allow macro constructs here
// -- if we did, we'd have to recursively call scan_pp1()
t = next_pp1 ();
if (! (t && t->type == tok_operator && t->content == "("))
{
delete new_act;
throw PARSE_ERROR (_NF
("expected '(' in invocation of macro '@%s'"
" taking %d parameter",
"expected '(' in invocation of macro '@%s'"
" taking %d parameters",
num_params, name.c_str(), num_params), t);
}
// XXX perhaps parse/count the full number of params,
// so we can say "expected x, found y params" on error?
for (unsigned i = 0; i < num_params; i++)
{
delete t;
// create parameter closure
string param_name = decl->formal_args[i];
pp_macrodecl* p = (new_act->params[param_name]
= new pp_macrodecl);
p->tok = new token(*new_act->tok);
p->parent_act = act;
// NB: *new_act->tok points to invocation, act is NULL at top level
t = slurp_pp1_param (p->body);
// check correct usage of ',' or ')'
if (t == 0) // hit unexpected EOF or end of macro
{
// XXX could we pop the stack and continue parsing
// the invocation, allowing macros to construct new
// invocations in piecemeal fashion??
const token* orig_t = new token(*new_act->tok);
delete new_act;
throw PARSE_ERROR (_("could not find end of macro invocation"), orig_t);
}
if (t->type == tok_operator && t->content == ",")
{
if (i + 1 == num_params)
{
delete new_act;
throw PARSE_ERROR (_F("too many parameters for macro '@%s' (expected %d)", name.c_str(), num_params), t);
}
}
else if (t->type == tok_operator && t->content == ")")
{
if (i + 1 != num_params)
{
delete new_act;
throw PARSE_ERROR (_F("too few parameters for macro '@%s' (expected %d)", name.c_str(), num_params), t);
}
}
else
{
// XXX this is, incidentally, impossible
delete new_act;
throw PARSE_ERROR(_("expected ',' or ')' after macro parameter"), t);
}
}
delete t;
// (2) set up macro expansion
expand:
pp1_state.push_back (new_act);
// Now loop around to look for a real token.
continue;
}
// Otherwise, we have an ordinary token.
return t;
}
}
// Consume a single macro invocation's parameters, heeding nested ( )
// brackets and stopping on an unbalanced ')' or an unbracketed ','
// (and returning the final separator token).
const token*
parser::slurp_pp1_param (vector<const token*>& param)
{
const token* t = 0;
unsigned nesting = 0;
do
{
t = next_pp1 ();
if (!t)
break;
if (t->type == tok_operator && t->content == "(")
++nesting;
else if (nesting && t->type == tok_operator && t->content == ")")
--nesting;
else if (!nesting && t->type == tok_operator
&& (t->content == ")" || t->content == ","))
break;
param.push_back(t);
}
while (true);
return t; // report ")" or "," or NULL
}
// Consume a macro declaration's body, heeding nested %( %) brackets.
const token*
parser::slurp_pp1_body (vector<const token*>& body)
{
const token* t = 0;
unsigned nesting = 0;
do
{
t = next_pp1 ();
if (!t)
break;
if (t->type == tok_operator && t->content == "%(")
++nesting;
else if (nesting && t->type == tok_operator && t->content == "%)")
--nesting;
else if (!nesting && t->type == tok_operator && t->content == "%)")
break;
body.push_back(t);
}
while (true);
return t; // report final "%)" or NULL
}
// Used for parsing .stpm files.
stapfile*
parser::parse_library_macros (bool errs_as_warnings)
{
stapfile* f = new stapfile;
input.set_current_file (f);
try
{
const token* t = scan_pp1 ();
// Currently we only take objection to macro invocations if they
// produce a non-whitespace token after being expanded.
// XXX should we prevent macro invocations even if they expand to empty??
if (t != 0)
throw PARSE_ERROR (_F("library macro file '%s' contains non-@define construct", input_name.c_str()), t);
// We need to first check whether *any* of the macros are duplicates,
// then commit to including the entire file in the global namespace
// (or not). Yuck.
for (map<string, macrodecl*>::iterator it = pp1_namespace.begin();
it != pp1_namespace.end(); it++)
{
string name = it->first;
if (session.library_macros.find(name) != session.library_macros.end())
{
parse_error er(ERR_SRC, _F("duplicate definition of library macro '@%s'", name.c_str()), it->second->tok);
er.chain = new PARSE_ERROR (_F("macro '@%s' first defined here", name.c_str()), session.library_macros[name]->tok);
print_error (er);
delete er.chain;
delete f;
return 0;
}
}
}
catch (const parse_error& pe)
{
print_error (pe, errs_as_warnings);
delete f;
return 0;
}
// If no errors, include the entire file. Note how this is outside
// of the try-catch block -- no errors possible.
for (map<string, macrodecl*>::iterator it = pp1_namespace.begin();
it != pp1_namespace.end(); it++)
{
string name = it->first;
session.library_macros[name] = it->second;
session.library_macros[name]->context = ctx_library;
}
return f;
}
// Second pass - preprocessor conditional expansion.
//
// The basic form is %( CONDITION %? THEN-TOKENS %: ELSE-TOKENS %)
// where CONDITION is: kernel_v[r] COMPARISON-OP "version-string"
// or: arch COMPARISON-OP "arch-string"
// or: systemtap_v COMPARISON-OP "version-string"
// or: systemtap_privilege COMPARISON-OP "privilege-string"
// or: CONFIG_foo COMPARISON-OP "config-string"
// or: CONFIG_foo COMPARISON-OP number
// or: CONFIG_foo COMPARISON-OP CONFIG_bar
// or: "string1" COMPARISON-OP "string2"
// or: number1 COMPARISON-OP number2
// The %: ELSE-TOKENS part is optional.
//
// e.g. %( kernel_v > "2.5" %? "foo" %: "baz" %)
// e.g. %( arch != "i?86" %? "foo" %: "baz" %)
// e.g. %( CONFIG_foo %? "foo" %: "baz" %)
//
// Up to an entire %( ... %) expression is processed by a single call
// to this function. Tokens included by any nested conditions are
// enqueued in a private vector.
bool eval_pp_conditional (systemtap_session& s,
const token* l, const token* op, const token* r)
{
if (l->type == tok_identifier && (l->content == "kernel_v" ||
l->content == "kernel_vr" ||
l->content == "systemtap_v"))
{
if (! (r->type == tok_string))
throw PARSE_ERROR (_("expected string literal"), r);
string target_kernel_vr = s.kernel_release;
string target_kernel_v = s.kernel_base_release;
string target;
if (l->content == "kernel_v") target = target_kernel_v;
else if (l->content == "kernel_vr") target = target_kernel_vr;
else if (l->content == "systemtap_v") target = s.compatible;
else assert (0);
string query = r->content;
bool rhs_wildcard = (strpbrk (query.c_str(), "*?[") != 0);
// collect acceptable strverscmp results.
int rvc_ok1, rvc_ok2;
bool wc_ok = false;
if (op->type == tok_operator && op->content == "<=")
{ rvc_ok1 = -1; rvc_ok2 = 0; }
else if (op->type == tok_operator && op->content == ">=")
{ rvc_ok1 = 1; rvc_ok2 = 0; }
else if (op->type == tok_operator && op->content == "<")
{ rvc_ok1 = -1; rvc_ok2 = -1; }
else if (op->type == tok_operator && op->content == ">")
{ rvc_ok1 = 1; rvc_ok2 = 1; }
else if (op->type == tok_operator && op->content == "==")
{ rvc_ok1 = 0; rvc_ok2 = 0; wc_ok = true; }
else if (op->type == tok_operator && op->content == "!=")
{ rvc_ok1 = -1; rvc_ok2 = 1; wc_ok = true; }
else
throw PARSE_ERROR (_("expected comparison operator"), op);
if ((!wc_ok) && rhs_wildcard)
throw PARSE_ERROR (_("wildcard not allowed with order comparison operators"), op);
if (rhs_wildcard)
{
int rvc_result = fnmatch (query.c_str(), target.c_str(),
FNM_NOESCAPE); // spooky
bool badness = (rvc_result == 0) ^ (op->content == "==");
return !badness;
}
else
{
int rvc_result = strverscmp (target.c_str(), query.c_str());
// normalize rvc_result
if (rvc_result < 0) rvc_result = -1;
if (rvc_result > 0) rvc_result = 1;
return (rvc_result == rvc_ok1 || rvc_result == rvc_ok2);
}
}
else if (l->type == tok_identifier && l->content == "systemtap_privilege")
{
string target_privilege =
/* XXX perhaps include a "guru" state */
pr_contains(s.privilege, pr_stapdev) ? "stapdev"
: pr_contains(s.privilege, pr_stapsys) ? "stapsys"
: pr_contains(s.privilege, pr_stapusr) ? "stapusr"
: "none"; /* should be impossible -- s.privilege always one of above */
assert(target_privilege != "none");
if (! (r->type == tok_string))
throw PARSE_ERROR (_("expected string literal"), r);
string query_privilege = r->content;
bool nomatch = (target_privilege != query_privilege);
bool result;
if (op->type == tok_operator && op->content == "==")
result = !nomatch;
else if (op->type == tok_operator && op->content == "!=")
result = nomatch;
else
throw PARSE_ERROR (_("expected '==' or '!='"), op);
/* XXX perhaps allow <= >= and similar comparisons */
return result;
}
else if (l->type == tok_identifier && l->content == "arch")
{
string target_architecture = s.architecture;
if (! (r->type == tok_string))
throw PARSE_ERROR (_("expected string literal"), r);
string query_architecture = r->content;
int nomatch = fnmatch (query_architecture.c_str(),
target_architecture.c_str(),
FNM_NOESCAPE); // still spooky
bool result;
if (op->type == tok_operator && op->content == "==")
result = !nomatch;
else if (op->type == tok_operator && op->content == "!=")
result = nomatch;
else
throw PARSE_ERROR (_("expected '==' or '!='"), op);
return result;
}
else if (l->type == tok_identifier && l->content == "runtime")
{
if (! (r->type == tok_string))
throw PARSE_ERROR (_("expected string literal"), r);
string query_runtime = r->content;
string target_runtime;
target_runtime = (s.runtime_mode == systemtap_session::dyninst_runtime
? "dyninst" : "kernel");
int nomatch = fnmatch (query_runtime.c_str(),
target_runtime.c_str(),
FNM_NOESCAPE); // still spooky
bool result;
if (op->type == tok_operator && op->content == "==")
result = !nomatch;
else if (op->type == tok_operator && op->content == "!=")
result = nomatch;
else
throw PARSE_ERROR (_("expected '==' or '!='"), op);
return result;
}
else if (l->type == tok_identifier && startswith(l->content, "CONFIG_"))
{
if (r->type == tok_string)
{
string lhs = s.kernel_config[l->content]; // may be empty
string rhs = r->content;
int nomatch = fnmatch (rhs.c_str(), lhs.c_str(), FNM_NOESCAPE); // still spooky
bool result;
if (op->type == tok_operator && op->content == "==")
result = !nomatch;
else if (op->type == tok_operator && op->content == "!=")
result = nomatch;
else
throw PARSE_ERROR (_("expected '==' or '!='"), op);
return result;
}
else if (r->type == tok_number)
{
const char* startp = s.kernel_config[l->content].c_str ();
char* endp = (char*) startp;
errno = 0;
int64_t lhs = (int64_t) strtoll (startp, & endp, 0);
if (errno == ERANGE || errno == EINVAL || *endp != '\0')
throw PARSE_ERROR ("Config option value not a number", l);
int64_t rhs = lex_cast<int64_t>(r->content);
return eval_comparison (lhs, op, rhs);
}
else if (r->type == tok_identifier
&& startswith(r->content, "CONFIG_"))
{
// First try to convert both to numbers,
// otherwise threat both as strings.
const char* startp = s.kernel_config[l->content].c_str ();
char* endp = (char*) startp;
errno = 0;
int64_t val = (int64_t) strtoll (startp, & endp, 0);
if (errno != ERANGE && errno != EINVAL && *endp == '\0')
{
int64_t lhs = val;
startp = s.kernel_config[r->content].c_str ();
endp = (char*) startp;
errno = 0;
int64_t rhs = (int64_t) strtoll (startp, & endp, 0);
if (errno != ERANGE && errno != EINVAL && *endp == '\0')
return eval_comparison (lhs, op, rhs);
}
string lhs = s.kernel_config[l->content];
string rhs = s.kernel_config[r->content];
return eval_comparison (lhs, op, rhs);
}
else
throw PARSE_ERROR (_("expected string, number literal or other CONFIG_... as right side operand"), r);
}
else if (l->type == tok_string && r->type == tok_string)
{
string lhs = l->content;
string rhs = r->content;
return eval_comparison (lhs, op, rhs);
// NB: no wildcarding option here
}
else if (l->type == tok_number && r->type == tok_number)
{
int64_t lhs = lex_cast<int64_t>(l->content);
int64_t rhs = lex_cast<int64_t>(r->content);
return eval_comparison (lhs, op, rhs);
// NB: no wildcarding option here
}
else if (l->type == tok_string && r->type == tok_number
&& op->type == tok_operator)
throw PARSE_ERROR (_("expected string literal as right value"), r);
else if (l->type == tok_number && r->type == tok_string
&& op->type == tok_operator)
throw PARSE_ERROR (_("expected number literal as right value"), r);
else
throw PARSE_ERROR (_("expected 'arch', 'kernel_v', 'kernel_vr', 'systemtap_v',\n"
" 'runtime', 'systemtap_privilege', 'CONFIG_...', or\n"
" comparison between strings or integers"), l);
}
// Only tokens corresponding to the TRUE statement must be expanded
const token*
parser::scan_pp ()
{
while (true)
{
pp_state_t pp = PP_NONE;
if (!pp_state.empty())