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