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