blob: b6115cbdf84295b09a75fb07da77da9ee622e667 [file] [log] [blame]
rjw1f884582022-01-06 17:20:42 +08001%pure-parser
2%parse-param {void *_parse_state}
3%parse-param {void *scanner}
4%lex-param {void* scanner}
5%locations
6
7%{
8
9#define YYDEBUG 1
10
11#include <linux/compiler.h>
12#include <linux/list.h>
13#include <linux/types.h>
14#include "util.h"
15#include "pmu.h"
16#include "debug.h"
17#include "parse-events.h"
18#include "parse-events-bison.h"
19
20void parse_events_error(YYLTYPE *loc, void *parse_state, void *scanner, char const *msg);
21
22#define ABORT_ON(val) \
23do { \
24 if (val) \
25 YYABORT; \
26} while (0)
27
28#define ALLOC_LIST(list) \
29do { \
30 list = malloc(sizeof(*list)); \
31 ABORT_ON(!list); \
32 INIT_LIST_HEAD(list); \
33} while (0)
34
35static void inc_group_count(struct list_head *list,
36 struct parse_events_state *parse_state)
37{
38 /* Count groups only have more than 1 members */
39 if (!list_is_last(list->next, list))
40 parse_state->nr_groups++;
41}
42
43%}
44
45%token PE_START_EVENTS PE_START_TERMS
46%token PE_VALUE PE_VALUE_SYM_HW PE_VALUE_SYM_SW PE_RAW PE_TERM
47%token PE_EVENT_NAME
48%token PE_NAME
49%token PE_BPF_OBJECT PE_BPF_SOURCE
50%token PE_MODIFIER_EVENT PE_MODIFIER_BP
51%token PE_NAME_CACHE_TYPE PE_NAME_CACHE_OP_RESULT
52%token PE_PREFIX_MEM PE_PREFIX_RAW PE_PREFIX_GROUP
53%token PE_ERROR
54%token PE_PMU_EVENT_PRE PE_PMU_EVENT_SUF PE_KERNEL_PMU_EVENT
55%token PE_ARRAY_ALL PE_ARRAY_RANGE
56%token PE_DRV_CFG_TERM
57%type <num> PE_VALUE
58%type <num> PE_VALUE_SYM_HW
59%type <num> PE_VALUE_SYM_SW
60%type <num> PE_RAW
61%type <num> PE_TERM
62%type <str> PE_NAME
63%type <str> PE_BPF_OBJECT
64%type <str> PE_BPF_SOURCE
65%type <str> PE_NAME_CACHE_TYPE
66%type <str> PE_NAME_CACHE_OP_RESULT
67%type <str> PE_MODIFIER_EVENT
68%type <str> PE_MODIFIER_BP
69%type <str> PE_EVENT_NAME
70%type <str> PE_PMU_EVENT_PRE PE_PMU_EVENT_SUF PE_KERNEL_PMU_EVENT
71%type <str> PE_DRV_CFG_TERM
72%type <num> value_sym
73%type <head> event_config
74%type <head> opt_event_config
75%type <head> opt_pmu_config
76%type <term> event_term
77%type <head> event_pmu
78%type <head> event_legacy_symbol
79%type <head> event_legacy_cache
80%type <head> event_legacy_mem
81%type <head> event_legacy_tracepoint
82%type <tracepoint_name> tracepoint_name
83%type <head> event_legacy_numeric
84%type <head> event_legacy_raw
85%type <head> event_bpf_file
86%type <head> event_def
87%type <head> event_mod
88%type <head> event_name
89%type <head> event
90%type <head> events
91%type <head> group_def
92%type <head> group
93%type <head> groups
94%type <array> array
95%type <array> array_term
96%type <array> array_terms
97
98%union
99{
100 char *str;
101 u64 num;
102 struct list_head *head;
103 struct parse_events_term *term;
104 struct tracepoint_name {
105 char *sys;
106 char *event;
107 } tracepoint_name;
108 struct parse_events_array array;
109}
110%%
111
112start:
113PE_START_EVENTS start_events
114|
115PE_START_TERMS start_terms
116
117start_events: groups
118{
119 struct parse_events_state *parse_state = _parse_state;
120
121 parse_events_update_lists($1, &parse_state->list);
122}
123
124groups:
125groups ',' group
126{
127 struct list_head *list = $1;
128 struct list_head *group = $3;
129
130 parse_events_update_lists(group, list);
131 $$ = list;
132}
133|
134groups ',' event
135{
136 struct list_head *list = $1;
137 struct list_head *event = $3;
138
139 parse_events_update_lists(event, list);
140 $$ = list;
141}
142|
143group
144|
145event
146
147group:
148group_def ':' PE_MODIFIER_EVENT
149{
150 struct list_head *list = $1;
151
152 ABORT_ON(parse_events__modifier_group(list, $3));
153 $$ = list;
154}
155|
156group_def
157
158group_def:
159PE_NAME '{' events '}'
160{
161 struct list_head *list = $3;
162
163 inc_group_count(list, _parse_state);
164 parse_events__set_leader($1, list);
165 $$ = list;
166}
167|
168'{' events '}'
169{
170 struct list_head *list = $2;
171
172 inc_group_count(list, _parse_state);
173 parse_events__set_leader(NULL, list);
174 $$ = list;
175}
176
177events:
178events ',' event
179{
180 struct list_head *event = $3;
181 struct list_head *list = $1;
182
183 parse_events_update_lists(event, list);
184 $$ = list;
185}
186|
187event
188
189event: event_mod
190
191event_mod:
192event_name PE_MODIFIER_EVENT
193{
194 struct list_head *list = $1;
195
196 /*
197 * Apply modifier on all events added by single event definition
198 * (there could be more events added for multiple tracepoint
199 * definitions via '*?'.
200 */
201 ABORT_ON(parse_events__modifier_event(list, $2, false));
202 $$ = list;
203}
204|
205event_name
206
207event_name:
208PE_EVENT_NAME event_def
209{
210 ABORT_ON(parse_events_name($2, $1));
211 free($1);
212 $$ = $2;
213}
214|
215event_def
216
217event_def: event_pmu |
218 event_legacy_symbol |
219 event_legacy_cache sep_dc |
220 event_legacy_mem |
221 event_legacy_tracepoint sep_dc |
222 event_legacy_numeric sep_dc |
223 event_legacy_raw sep_dc |
224 event_bpf_file
225
226event_pmu:
227PE_NAME opt_pmu_config
228{
229 struct parse_events_state *parse_state = _parse_state;
230 struct parse_events_error *error = parse_state->error;
231 struct list_head *list, *orig_terms, *terms;
232
233 if (parse_events_copy_term_list($2, &orig_terms))
234 YYABORT;
235
236 if (error)
237 error->idx = @1.first_column;
238
239 ALLOC_LIST(list);
240 if (parse_events_add_pmu(_parse_state, list, $1, $2)) {
241 struct perf_pmu *pmu = NULL;
242 int ok = 0;
243
244 while ((pmu = perf_pmu__scan(pmu)) != NULL) {
245 char *name = pmu->name;
246
247 if (!strncmp(name, "uncore_", 7) &&
248 strncmp($1, "uncore_", 7))
249 name += 7;
250 if (!strncmp($1, name, strlen($1))) {
251 if (parse_events_copy_term_list(orig_terms, &terms))
252 YYABORT;
253 if (!parse_events_add_pmu(_parse_state, list, pmu->name, terms))
254 ok++;
255 parse_events_terms__delete(terms);
256 }
257 }
258 if (!ok)
259 YYABORT;
260 }
261 parse_events_terms__delete($2);
262 parse_events_terms__delete(orig_terms);
263 $$ = list;
264}
265|
266PE_KERNEL_PMU_EVENT sep_dc
267{
268 struct list_head *list;
269
270 if (parse_events_multi_pmu_add(_parse_state, $1, &list) < 0)
271 YYABORT;
272 $$ = list;
273}
274|
275PE_PMU_EVENT_PRE '-' PE_PMU_EVENT_SUF sep_dc
276{
277 struct list_head *list;
278 char pmu_name[128];
279
280 snprintf(&pmu_name, 128, "%s-%s", $1, $3);
281 if (parse_events_multi_pmu_add(_parse_state, pmu_name, &list) < 0)
282 YYABORT;
283 $$ = list;
284}
285
286value_sym:
287PE_VALUE_SYM_HW
288|
289PE_VALUE_SYM_SW
290
291event_legacy_symbol:
292value_sym '/' event_config '/'
293{
294 struct list_head *list;
295 int type = $1 >> 16;
296 int config = $1 & 255;
297
298 ALLOC_LIST(list);
299 ABORT_ON(parse_events_add_numeric(_parse_state, list, type, config, $3));
300 parse_events_terms__delete($3);
301 $$ = list;
302}
303|
304value_sym sep_slash_dc
305{
306 struct list_head *list;
307 int type = $1 >> 16;
308 int config = $1 & 255;
309
310 ALLOC_LIST(list);
311 ABORT_ON(parse_events_add_numeric(_parse_state, list, type, config, NULL));
312 $$ = list;
313}
314
315event_legacy_cache:
316PE_NAME_CACHE_TYPE '-' PE_NAME_CACHE_OP_RESULT '-' PE_NAME_CACHE_OP_RESULT opt_event_config
317{
318 struct parse_events_state *parse_state = _parse_state;
319 struct parse_events_error *error = parse_state->error;
320 struct list_head *list;
321
322 ALLOC_LIST(list);
323 ABORT_ON(parse_events_add_cache(list, &parse_state->idx, $1, $3, $5, error, $6));
324 parse_events_terms__delete($6);
325 $$ = list;
326}
327|
328PE_NAME_CACHE_TYPE '-' PE_NAME_CACHE_OP_RESULT opt_event_config
329{
330 struct parse_events_state *parse_state = _parse_state;
331 struct parse_events_error *error = parse_state->error;
332 struct list_head *list;
333
334 ALLOC_LIST(list);
335 ABORT_ON(parse_events_add_cache(list, &parse_state->idx, $1, $3, NULL, error, $4));
336 parse_events_terms__delete($4);
337 $$ = list;
338}
339|
340PE_NAME_CACHE_TYPE opt_event_config
341{
342 struct parse_events_state *parse_state = _parse_state;
343 struct parse_events_error *error = parse_state->error;
344 struct list_head *list;
345
346 ALLOC_LIST(list);
347 ABORT_ON(parse_events_add_cache(list, &parse_state->idx, $1, NULL, NULL, error, $2));
348 parse_events_terms__delete($2);
349 $$ = list;
350}
351
352event_legacy_mem:
353PE_PREFIX_MEM PE_VALUE '/' PE_VALUE ':' PE_MODIFIER_BP sep_dc
354{
355 struct parse_events_state *parse_state = _parse_state;
356 struct list_head *list;
357
358 ALLOC_LIST(list);
359 ABORT_ON(parse_events_add_breakpoint(list, &parse_state->idx,
360 (void *) $2, $6, $4));
361 $$ = list;
362}
363|
364PE_PREFIX_MEM PE_VALUE '/' PE_VALUE sep_dc
365{
366 struct parse_events_state *parse_state = _parse_state;
367 struct list_head *list;
368
369 ALLOC_LIST(list);
370 ABORT_ON(parse_events_add_breakpoint(list, &parse_state->idx,
371 (void *) $2, NULL, $4));
372 $$ = list;
373}
374|
375PE_PREFIX_MEM PE_VALUE ':' PE_MODIFIER_BP sep_dc
376{
377 struct parse_events_state *parse_state = _parse_state;
378 struct list_head *list;
379
380 ALLOC_LIST(list);
381 ABORT_ON(parse_events_add_breakpoint(list, &parse_state->idx,
382 (void *) $2, $4, 0));
383 $$ = list;
384}
385|
386PE_PREFIX_MEM PE_VALUE sep_dc
387{
388 struct parse_events_state *parse_state = _parse_state;
389 struct list_head *list;
390
391 ALLOC_LIST(list);
392 ABORT_ON(parse_events_add_breakpoint(list, &parse_state->idx,
393 (void *) $2, NULL, 0));
394 $$ = list;
395}
396
397event_legacy_tracepoint:
398tracepoint_name opt_event_config
399{
400 struct parse_events_state *parse_state = _parse_state;
401 struct parse_events_error *error = parse_state->error;
402 struct list_head *list;
403
404 ALLOC_LIST(list);
405 if (error)
406 error->idx = @1.first_column;
407
408 if (parse_events_add_tracepoint(list, &parse_state->idx, $1.sys, $1.event,
409 error, $2))
410 return -1;
411
412 $$ = list;
413}
414
415tracepoint_name:
416PE_NAME '-' PE_NAME ':' PE_NAME
417{
418 char sys_name[128];
419 struct tracepoint_name tracepoint;
420
421 snprintf(&sys_name, 128, "%s-%s", $1, $3);
422 tracepoint.sys = &sys_name;
423 tracepoint.event = $5;
424
425 $$ = tracepoint;
426}
427|
428PE_NAME ':' PE_NAME
429{
430 struct tracepoint_name tracepoint = {$1, $3};
431
432 $$ = tracepoint;
433}
434
435event_legacy_numeric:
436PE_VALUE ':' PE_VALUE opt_event_config
437{
438 struct list_head *list;
439
440 ALLOC_LIST(list);
441 ABORT_ON(parse_events_add_numeric(_parse_state, list, (u32)$1, $3, $4));
442 parse_events_terms__delete($4);
443 $$ = list;
444}
445
446event_legacy_raw:
447PE_RAW opt_event_config
448{
449 struct list_head *list;
450
451 ALLOC_LIST(list);
452 ABORT_ON(parse_events_add_numeric(_parse_state, list, PERF_TYPE_RAW, $1, $2));
453 parse_events_terms__delete($2);
454 $$ = list;
455}
456
457event_bpf_file:
458PE_BPF_OBJECT opt_event_config
459{
460 struct parse_events_state *parse_state = _parse_state;
461 struct parse_events_error *error = parse_state->error;
462 struct list_head *list;
463
464 ALLOC_LIST(list);
465 ABORT_ON(parse_events_load_bpf(parse_state, list, $1, false, $2));
466 parse_events_terms__delete($2);
467 $$ = list;
468}
469|
470PE_BPF_SOURCE opt_event_config
471{
472 struct list_head *list;
473
474 ALLOC_LIST(list);
475 ABORT_ON(parse_events_load_bpf(_parse_state, list, $1, true, $2));
476 parse_events_terms__delete($2);
477 $$ = list;
478}
479
480opt_event_config:
481'/' event_config '/'
482{
483 $$ = $2;
484}
485|
486'/' '/'
487{
488 $$ = NULL;
489}
490|
491{
492 $$ = NULL;
493}
494
495opt_pmu_config:
496'/' event_config '/'
497{
498 $$ = $2;
499}
500|
501'/' '/'
502{
503 $$ = NULL;
504}
505
506start_terms: event_config
507{
508 struct parse_events_state *parse_state = _parse_state;
509 parse_state->terms = $1;
510}
511
512event_config:
513event_config ',' event_term
514{
515 struct list_head *head = $1;
516 struct parse_events_term *term = $3;
517
518 ABORT_ON(!head);
519 list_add_tail(&term->list, head);
520 $$ = $1;
521}
522|
523event_term
524{
525 struct list_head *head = malloc(sizeof(*head));
526 struct parse_events_term *term = $1;
527
528 ABORT_ON(!head);
529 INIT_LIST_HEAD(head);
530 list_add_tail(&term->list, head);
531 $$ = head;
532}
533
534event_term:
535PE_NAME '=' PE_NAME
536{
537 struct parse_events_term *term;
538
539 ABORT_ON(parse_events_term__str(&term, PARSE_EVENTS__TERM_TYPE_USER,
540 $1, $3, &@1, &@3));
541 $$ = term;
542}
543|
544PE_NAME '=' PE_VALUE
545{
546 struct parse_events_term *term;
547
548 ABORT_ON(parse_events_term__num(&term, PARSE_EVENTS__TERM_TYPE_USER,
549 $1, $3, false, &@1, &@3));
550 $$ = term;
551}
552|
553PE_NAME '=' PE_VALUE_SYM_HW
554{
555 struct parse_events_term *term;
556 int config = $3 & 255;
557
558 ABORT_ON(parse_events_term__sym_hw(&term, $1, config));
559 $$ = term;
560}
561|
562PE_NAME
563{
564 struct parse_events_term *term;
565
566 ABORT_ON(parse_events_term__num(&term, PARSE_EVENTS__TERM_TYPE_USER,
567 $1, 1, true, &@1, NULL));
568 $$ = term;
569}
570|
571PE_VALUE_SYM_HW
572{
573 struct parse_events_term *term;
574 int config = $1 & 255;
575
576 ABORT_ON(parse_events_term__sym_hw(&term, NULL, config));
577 $$ = term;
578}
579|
580PE_TERM '=' PE_NAME
581{
582 struct parse_events_term *term;
583
584 ABORT_ON(parse_events_term__str(&term, (int)$1, NULL, $3, &@1, &@3));
585 $$ = term;
586}
587|
588PE_TERM '=' PE_VALUE
589{
590 struct parse_events_term *term;
591
592 ABORT_ON(parse_events_term__num(&term, (int)$1, NULL, $3, false, &@1, &@3));
593 $$ = term;
594}
595|
596PE_TERM
597{
598 struct parse_events_term *term;
599
600 ABORT_ON(parse_events_term__num(&term, (int)$1, NULL, 1, true, &@1, NULL));
601 $$ = term;
602}
603|
604PE_NAME array '=' PE_NAME
605{
606 struct parse_events_term *term;
607 int i;
608
609 ABORT_ON(parse_events_term__str(&term, PARSE_EVENTS__TERM_TYPE_USER,
610 $1, $4, &@1, &@4));
611
612 term->array = $2;
613 $$ = term;
614}
615|
616PE_NAME array '=' PE_VALUE
617{
618 struct parse_events_term *term;
619
620 ABORT_ON(parse_events_term__num(&term, PARSE_EVENTS__TERM_TYPE_USER,
621 $1, $4, false, &@1, &@4));
622 term->array = $2;
623 $$ = term;
624}
625|
626PE_DRV_CFG_TERM
627{
628 struct parse_events_term *term;
629
630 ABORT_ON(parse_events_term__str(&term, PARSE_EVENTS__TERM_TYPE_DRV_CFG,
631 $1, $1, &@1, NULL));
632 $$ = term;
633}
634
635array:
636'[' array_terms ']'
637{
638 $$ = $2;
639}
640|
641PE_ARRAY_ALL
642{
643 $$.nr_ranges = 0;
644 $$.ranges = NULL;
645}
646
647array_terms:
648array_terms ',' array_term
649{
650 struct parse_events_array new_array;
651
652 new_array.nr_ranges = $1.nr_ranges + $3.nr_ranges;
653 new_array.ranges = malloc(sizeof(new_array.ranges[0]) *
654 new_array.nr_ranges);
655 ABORT_ON(!new_array.ranges);
656 memcpy(&new_array.ranges[0], $1.ranges,
657 $1.nr_ranges * sizeof(new_array.ranges[0]));
658 memcpy(&new_array.ranges[$1.nr_ranges], $3.ranges,
659 $3.nr_ranges * sizeof(new_array.ranges[0]));
660 free($1.ranges);
661 free($3.ranges);
662 $$ = new_array;
663}
664|
665array_term
666
667array_term:
668PE_VALUE
669{
670 struct parse_events_array array;
671
672 array.nr_ranges = 1;
673 array.ranges = malloc(sizeof(array.ranges[0]));
674 ABORT_ON(!array.ranges);
675 array.ranges[0].start = $1;
676 array.ranges[0].length = 1;
677 $$ = array;
678}
679|
680PE_VALUE PE_ARRAY_RANGE PE_VALUE
681{
682 struct parse_events_array array;
683
684 ABORT_ON($3 < $1);
685 array.nr_ranges = 1;
686 array.ranges = malloc(sizeof(array.ranges[0]));
687 ABORT_ON(!array.ranges);
688 array.ranges[0].start = $1;
689 array.ranges[0].length = $3 - $1 + 1;
690 $$ = array;
691}
692
693sep_dc: ':' |
694
695sep_slash_dc: '/' | ':' |
696
697%%
698
699void parse_events_error(YYLTYPE *loc, void *parse_state,
700 void *scanner __maybe_unused,
701 char const *msg __maybe_unused)
702{
703 parse_events_evlist_error(parse_state, loc->last_column, "parser error");
704}