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