xref: /openbmc/linux/tools/perf/util/parse-events.y (revision 5d0e4d78)
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, *orig_terms, *terms;
230 
231 	if (parse_events_copy_term_list($2, &orig_terms))
232 		YYABORT;
233 
234 	ALLOC_LIST(list);
235 	if (parse_events_add_pmu(data, list, $1, $2)) {
236 		struct perf_pmu *pmu = NULL;
237 		int ok = 0;
238 
239 		while ((pmu = perf_pmu__scan(pmu)) != NULL) {
240 			char *name = pmu->name;
241 
242 			if (!strncmp(name, "uncore_", 7) &&
243 			    strncmp($1, "uncore_", 7))
244 				name += 7;
245 			if (!strncmp($1, name, strlen($1))) {
246 				if (parse_events_copy_term_list(orig_terms, &terms))
247 					YYABORT;
248 				if (!parse_events_add_pmu(data, list, pmu->name, terms))
249 					ok++;
250 				parse_events_terms__delete(terms);
251 			}
252 		}
253 		if (!ok)
254 			YYABORT;
255 	}
256 	parse_events_terms__delete($2);
257 	parse_events_terms__delete(orig_terms);
258 	$$ = list;
259 }
260 |
261 PE_KERNEL_PMU_EVENT sep_dc
262 {
263 	struct list_head *list;
264 
265 	if (parse_events_multi_pmu_add(_data, $1, &list) < 0)
266 		YYABORT;
267 	$$ = list;
268 }
269 |
270 PE_PMU_EVENT_PRE '-' PE_PMU_EVENT_SUF sep_dc
271 {
272 	struct list_head *list;
273 	char pmu_name[128];
274 
275 	snprintf(&pmu_name, 128, "%s-%s", $1, $3);
276 	if (parse_events_multi_pmu_add(_data, pmu_name, &list) < 0)
277 		YYABORT;
278 	$$ = list;
279 }
280 
281 value_sym:
282 PE_VALUE_SYM_HW
283 |
284 PE_VALUE_SYM_SW
285 
286 event_legacy_symbol:
287 value_sym '/' event_config '/'
288 {
289 	struct parse_events_evlist *data = _data;
290 	struct list_head *list;
291 	int type = $1 >> 16;
292 	int config = $1 & 255;
293 
294 	ALLOC_LIST(list);
295 	ABORT_ON(parse_events_add_numeric(data, list, type, config, $3));
296 	parse_events_terms__delete($3);
297 	$$ = list;
298 }
299 |
300 value_sym sep_slash_dc
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, NULL));
309 	$$ = list;
310 }
311 
312 event_legacy_cache:
313 PE_NAME_CACHE_TYPE '-' PE_NAME_CACHE_OP_RESULT '-' PE_NAME_CACHE_OP_RESULT opt_event_config
314 {
315 	struct parse_events_evlist *data = _data;
316 	struct parse_events_error *error = data->error;
317 	struct list_head *list;
318 
319 	ALLOC_LIST(list);
320 	ABORT_ON(parse_events_add_cache(list, &data->idx, $1, $3, $5, error, $6));
321 	parse_events_terms__delete($6);
322 	$$ = list;
323 }
324 |
325 PE_NAME_CACHE_TYPE '-' PE_NAME_CACHE_OP_RESULT opt_event_config
326 {
327 	struct parse_events_evlist *data = _data;
328 	struct parse_events_error *error = data->error;
329 	struct list_head *list;
330 
331 	ALLOC_LIST(list);
332 	ABORT_ON(parse_events_add_cache(list, &data->idx, $1, $3, NULL, error, $4));
333 	parse_events_terms__delete($4);
334 	$$ = list;
335 }
336 |
337 PE_NAME_CACHE_TYPE opt_event_config
338 {
339 	struct parse_events_evlist *data = _data;
340 	struct parse_events_error *error = data->error;
341 	struct list_head *list;
342 
343 	ALLOC_LIST(list);
344 	ABORT_ON(parse_events_add_cache(list, &data->idx, $1, NULL, NULL, error, $2));
345 	parse_events_terms__delete($2);
346 	$$ = list;
347 }
348 
349 event_legacy_mem:
350 PE_PREFIX_MEM PE_VALUE '/' PE_VALUE ':' PE_MODIFIER_BP sep_dc
351 {
352 	struct parse_events_evlist *data = _data;
353 	struct list_head *list;
354 
355 	ALLOC_LIST(list);
356 	ABORT_ON(parse_events_add_breakpoint(list, &data->idx,
357 					     (void *) $2, $6, $4));
358 	$$ = list;
359 }
360 |
361 PE_PREFIX_MEM PE_VALUE '/' PE_VALUE sep_dc
362 {
363 	struct parse_events_evlist *data = _data;
364 	struct list_head *list;
365 
366 	ALLOC_LIST(list);
367 	ABORT_ON(parse_events_add_breakpoint(list, &data->idx,
368 					     (void *) $2, NULL, $4));
369 	$$ = list;
370 }
371 |
372 PE_PREFIX_MEM PE_VALUE ':' PE_MODIFIER_BP sep_dc
373 {
374 	struct parse_events_evlist *data = _data;
375 	struct list_head *list;
376 
377 	ALLOC_LIST(list);
378 	ABORT_ON(parse_events_add_breakpoint(list, &data->idx,
379 					     (void *) $2, $4, 0));
380 	$$ = list;
381 }
382 |
383 PE_PREFIX_MEM PE_VALUE sep_dc
384 {
385 	struct parse_events_evlist *data = _data;
386 	struct list_head *list;
387 
388 	ALLOC_LIST(list);
389 	ABORT_ON(parse_events_add_breakpoint(list, &data->idx,
390 					     (void *) $2, NULL, 0));
391 	$$ = list;
392 }
393 
394 event_legacy_tracepoint:
395 tracepoint_name opt_event_config
396 {
397 	struct parse_events_evlist *data = _data;
398 	struct parse_events_error *error = data->error;
399 	struct list_head *list;
400 
401 	ALLOC_LIST(list);
402 	if (error)
403 		error->idx = @1.first_column;
404 
405 	if (parse_events_add_tracepoint(list, &data->idx, $1.sys, $1.event,
406 					error, $2))
407 		return -1;
408 
409 	$$ = list;
410 }
411 
412 tracepoint_name:
413 PE_NAME '-' PE_NAME ':' PE_NAME
414 {
415 	char sys_name[128];
416 	struct tracepoint_name tracepoint;
417 
418 	snprintf(&sys_name, 128, "%s-%s", $1, $3);
419 	tracepoint.sys = &sys_name;
420 	tracepoint.event = $5;
421 
422 	$$ = tracepoint;
423 }
424 |
425 PE_NAME ':' PE_NAME
426 {
427 	struct tracepoint_name tracepoint = {$1, $3};
428 
429 	$$ = tracepoint;
430 }
431 
432 event_legacy_numeric:
433 PE_VALUE ':' PE_VALUE opt_event_config
434 {
435 	struct parse_events_evlist *data = _data;
436 	struct list_head *list;
437 
438 	ALLOC_LIST(list);
439 	ABORT_ON(parse_events_add_numeric(data, list, (u32)$1, $3, $4));
440 	parse_events_terms__delete($4);
441 	$$ = list;
442 }
443 
444 event_legacy_raw:
445 PE_RAW opt_event_config
446 {
447 	struct parse_events_evlist *data = _data;
448 	struct list_head *list;
449 
450 	ALLOC_LIST(list);
451 	ABORT_ON(parse_events_add_numeric(data, list, PERF_TYPE_RAW, $1, $2));
452 	parse_events_terms__delete($2);
453 	$$ = list;
454 }
455 
456 event_bpf_file:
457 PE_BPF_OBJECT opt_event_config
458 {
459 	struct parse_events_evlist *data = _data;
460 	struct parse_events_error *error = data->error;
461 	struct list_head *list;
462 
463 	ALLOC_LIST(list);
464 	ABORT_ON(parse_events_load_bpf(data, list, $1, false, $2));
465 	parse_events_terms__delete($2);
466 	$$ = list;
467 }
468 |
469 PE_BPF_SOURCE opt_event_config
470 {
471 	struct parse_events_evlist *data = _data;
472 	struct list_head *list;
473 
474 	ALLOC_LIST(list);
475 	ABORT_ON(parse_events_load_bpf(data, list, $1, true, $2));
476 	parse_events_terms__delete($2);
477 	$$ = list;
478 }
479 
480 opt_event_config:
481 '/' event_config '/'
482 {
483 	$$ = $2;
484 }
485 |
486 '/' '/'
487 {
488 	$$ = NULL;
489 }
490 |
491 {
492 	$$ = NULL;
493 }
494 
495 start_terms: event_config
496 {
497 	struct parse_events_terms *data = _data;
498 	data->terms = $1;
499 }
500 
501 event_config:
502 event_config ',' event_term
503 {
504 	struct list_head *head = $1;
505 	struct parse_events_term *term = $3;
506 
507 	ABORT_ON(!head);
508 	list_add_tail(&term->list, head);
509 	$$ = $1;
510 }
511 |
512 event_term
513 {
514 	struct list_head *head = malloc(sizeof(*head));
515 	struct parse_events_term *term = $1;
516 
517 	ABORT_ON(!head);
518 	INIT_LIST_HEAD(head);
519 	list_add_tail(&term->list, head);
520 	$$ = head;
521 }
522 
523 event_term:
524 PE_NAME '=' PE_NAME
525 {
526 	struct parse_events_term *term;
527 
528 	ABORT_ON(parse_events_term__str(&term, PARSE_EVENTS__TERM_TYPE_USER,
529 					$1, $3, &@1, &@3));
530 	$$ = term;
531 }
532 |
533 PE_NAME '=' PE_VALUE
534 {
535 	struct parse_events_term *term;
536 
537 	ABORT_ON(parse_events_term__num(&term, PARSE_EVENTS__TERM_TYPE_USER,
538 					$1, $3, false, &@1, &@3));
539 	$$ = term;
540 }
541 |
542 PE_NAME '=' PE_VALUE_SYM_HW
543 {
544 	struct parse_events_term *term;
545 	int config = $3 & 255;
546 
547 	ABORT_ON(parse_events_term__sym_hw(&term, $1, config));
548 	$$ = term;
549 }
550 |
551 PE_NAME
552 {
553 	struct parse_events_term *term;
554 
555 	ABORT_ON(parse_events_term__num(&term, PARSE_EVENTS__TERM_TYPE_USER,
556 					$1, 1, true, &@1, NULL));
557 	$$ = term;
558 }
559 |
560 PE_VALUE_SYM_HW
561 {
562 	struct parse_events_term *term;
563 	int config = $1 & 255;
564 
565 	ABORT_ON(parse_events_term__sym_hw(&term, NULL, config));
566 	$$ = term;
567 }
568 |
569 PE_TERM '=' PE_NAME
570 {
571 	struct parse_events_term *term;
572 
573 	ABORT_ON(parse_events_term__str(&term, (int)$1, NULL, $3, &@1, &@3));
574 	$$ = term;
575 }
576 |
577 PE_TERM '=' PE_VALUE
578 {
579 	struct parse_events_term *term;
580 
581 	ABORT_ON(parse_events_term__num(&term, (int)$1, NULL, $3, false, &@1, &@3));
582 	$$ = term;
583 }
584 |
585 PE_TERM
586 {
587 	struct parse_events_term *term;
588 
589 	ABORT_ON(parse_events_term__num(&term, (int)$1, NULL, 1, true, &@1, NULL));
590 	$$ = term;
591 }
592 |
593 PE_NAME array '=' PE_NAME
594 {
595 	struct parse_events_term *term;
596 	int i;
597 
598 	ABORT_ON(parse_events_term__str(&term, PARSE_EVENTS__TERM_TYPE_USER,
599 					$1, $4, &@1, &@4));
600 
601 	term->array = $2;
602 	$$ = term;
603 }
604 |
605 PE_NAME array '=' PE_VALUE
606 {
607 	struct parse_events_term *term;
608 
609 	ABORT_ON(parse_events_term__num(&term, PARSE_EVENTS__TERM_TYPE_USER,
610 					$1, $4, false, &@1, &@4));
611 	term->array = $2;
612 	$$ = term;
613 }
614 |
615 PE_DRV_CFG_TERM
616 {
617 	struct parse_events_term *term;
618 
619 	ABORT_ON(parse_events_term__str(&term, PARSE_EVENTS__TERM_TYPE_DRV_CFG,
620 					$1, $1, &@1, NULL));
621 	$$ = term;
622 }
623 
624 array:
625 '[' array_terms ']'
626 {
627 	$$ = $2;
628 }
629 |
630 PE_ARRAY_ALL
631 {
632 	$$.nr_ranges = 0;
633 	$$.ranges = NULL;
634 }
635 
636 array_terms:
637 array_terms ',' array_term
638 {
639 	struct parse_events_array new_array;
640 
641 	new_array.nr_ranges = $1.nr_ranges + $3.nr_ranges;
642 	new_array.ranges = malloc(sizeof(new_array.ranges[0]) *
643 				  new_array.nr_ranges);
644 	ABORT_ON(!new_array.ranges);
645 	memcpy(&new_array.ranges[0], $1.ranges,
646 	       $1.nr_ranges * sizeof(new_array.ranges[0]));
647 	memcpy(&new_array.ranges[$1.nr_ranges], $3.ranges,
648 	       $3.nr_ranges * sizeof(new_array.ranges[0]));
649 	free($1.ranges);
650 	free($3.ranges);
651 	$$ = new_array;
652 }
653 |
654 array_term
655 
656 array_term:
657 PE_VALUE
658 {
659 	struct parse_events_array array;
660 
661 	array.nr_ranges = 1;
662 	array.ranges = malloc(sizeof(array.ranges[0]));
663 	ABORT_ON(!array.ranges);
664 	array.ranges[0].start = $1;
665 	array.ranges[0].length = 1;
666 	$$ = array;
667 }
668 |
669 PE_VALUE PE_ARRAY_RANGE PE_VALUE
670 {
671 	struct parse_events_array array;
672 
673 	ABORT_ON($3 < $1);
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 = $3 - $1 + 1;
679 	$$ = array;
680 }
681 
682 sep_dc: ':' |
683 
684 sep_slash_dc: '/' | ':' |
685 
686 %%
687 
688 void parse_events_error(YYLTYPE *loc, void *data,
689 			void *scanner __maybe_unused,
690 			char const *msg __maybe_unused)
691 {
692 	parse_events_evlist_error(data, loc->last_column, "parser error");
693 }
694