treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / tools / perf / util / parse-events.y
blob94f8bcd835826de88b83c8df50141bafdffce7f2
1 %define api.pure full
2 %parse-param {void *_parse_state}
3 %parse-param {void *scanner}
4 %lex-param {void* scanner}
5 %locations
7 %{
9 #define YYDEBUG 1
11 #include <fnmatch.h>
12 #include <stdio.h>
13 #include <linux/compiler.h>
14 #include <linux/types.h>
15 #include <linux/zalloc.h>
16 #include "pmu.h"
17 #include "evsel.h"
18 #include "parse-events.h"
19 #include "parse-events-bison.h"
21 void parse_events_error(YYLTYPE *loc, void *parse_state, void *scanner, char const *msg);
23 #define ABORT_ON(val) \
24 do { \
25 if (val) \
26 YYABORT; \
27 } while (0)
29 static struct list_head* alloc_list()
31 struct list_head *list;
33 list = malloc(sizeof(*list));
34 if (!list)
35 return NULL;
37 INIT_LIST_HEAD(list);
38 return list;
41 static void free_list_evsel(struct list_head* list_evsel)
43 struct evsel *evsel, *tmp;
45 list_for_each_entry_safe(evsel, tmp, list_evsel, core.node) {
46 list_del_init(&evsel->core.node);
47 perf_evsel__delete(evsel);
49 free(list_evsel);
52 static void inc_group_count(struct list_head *list,
53 struct parse_events_state *parse_state)
55 /* Count groups only have more than 1 members */
56 if (!list_is_last(list->next, list))
57 parse_state->nr_groups++;
62 %token PE_START_EVENTS PE_START_TERMS
63 %token PE_VALUE PE_VALUE_SYM_HW PE_VALUE_SYM_SW PE_RAW PE_TERM
64 %token PE_VALUE_SYM_TOOL
65 %token PE_EVENT_NAME
66 %token PE_NAME
67 %token PE_BPF_OBJECT PE_BPF_SOURCE
68 %token PE_MODIFIER_EVENT PE_MODIFIER_BP
69 %token PE_NAME_CACHE_TYPE PE_NAME_CACHE_OP_RESULT
70 %token PE_PREFIX_MEM PE_PREFIX_RAW PE_PREFIX_GROUP
71 %token PE_ERROR
72 %token PE_PMU_EVENT_PRE PE_PMU_EVENT_SUF PE_KERNEL_PMU_EVENT
73 %token PE_ARRAY_ALL PE_ARRAY_RANGE
74 %token PE_DRV_CFG_TERM
75 %type <num> PE_VALUE
76 %type <num> PE_VALUE_SYM_HW
77 %type <num> PE_VALUE_SYM_SW
78 %type <num> PE_VALUE_SYM_TOOL
79 %type <num> PE_RAW
80 %type <num> PE_TERM
81 %type <num> value_sym
82 %type <str> PE_NAME
83 %type <str> PE_BPF_OBJECT
84 %type <str> PE_BPF_SOURCE
85 %type <str> PE_NAME_CACHE_TYPE
86 %type <str> PE_NAME_CACHE_OP_RESULT
87 %type <str> PE_MODIFIER_EVENT
88 %type <str> PE_MODIFIER_BP
89 %type <str> PE_EVENT_NAME
90 %type <str> PE_PMU_EVENT_PRE PE_PMU_EVENT_SUF PE_KERNEL_PMU_EVENT
91 %type <str> PE_DRV_CFG_TERM
92 %destructor { free ($$); } <str>
93 %type <term> event_term
94 %destructor { parse_events_term__delete ($$); } <term>
95 %type <list_terms> event_config
96 %type <list_terms> opt_event_config
97 %type <list_terms> opt_pmu_config
98 %destructor { parse_events_terms__delete ($$); } <list_terms>
99 %type <list_evsel> event_pmu
100 %type <list_evsel> event_legacy_symbol
101 %type <list_evsel> event_legacy_cache
102 %type <list_evsel> event_legacy_mem
103 %type <list_evsel> event_legacy_tracepoint
104 %type <list_evsel> event_legacy_numeric
105 %type <list_evsel> event_legacy_raw
106 %type <list_evsel> event_bpf_file
107 %type <list_evsel> event_def
108 %type <list_evsel> event_mod
109 %type <list_evsel> event_name
110 %type <list_evsel> event
111 %type <list_evsel> events
112 %type <list_evsel> group_def
113 %type <list_evsel> group
114 %type <list_evsel> groups
115 %destructor { free_list_evsel ($$); } <list_evsel>
116 %type <tracepoint_name> tracepoint_name
117 %destructor { free ($$.sys); free ($$.event); } <tracepoint_name>
118 %type <array> array
119 %type <array> array_term
120 %type <array> array_terms
121 %destructor { free ($$.ranges); } <array>
123 %union
125 char *str;
126 u64 num;
127 struct list_head *list_evsel;
128 struct list_head *list_terms;
129 struct parse_events_term *term;
130 struct tracepoint_name {
131 char *sys;
132 char *event;
133 } tracepoint_name;
134 struct parse_events_array array;
138 start:
139 PE_START_EVENTS start_events
141 PE_START_TERMS start_terms
143 start_events: groups
145 struct parse_events_state *parse_state = _parse_state;
147 /* frees $1 */
148 parse_events_update_lists($1, &parse_state->list);
151 groups:
152 groups ',' group
154 struct list_head *list = $1;
155 struct list_head *group = $3;
157 /* frees $3 */
158 parse_events_update_lists(group, list);
159 $$ = list;
162 groups ',' event
164 struct list_head *list = $1;
165 struct list_head *event = $3;
167 /* frees $3 */
168 parse_events_update_lists(event, list);
169 $$ = list;
172 group
174 event
176 group:
177 group_def ':' PE_MODIFIER_EVENT
179 struct list_head *list = $1;
180 int err;
182 err = parse_events__modifier_group(list, $3);
183 free($3);
184 if (err) {
185 free_list_evsel(list);
186 YYABORT;
188 $$ = list;
191 group_def
193 group_def:
194 PE_NAME '{' events '}'
196 struct list_head *list = $3;
198 inc_group_count(list, _parse_state);
199 parse_events__set_leader($1, list, _parse_state);
200 free($1);
201 $$ = list;
204 '{' events '}'
206 struct list_head *list = $2;
208 inc_group_count(list, _parse_state);
209 parse_events__set_leader(NULL, list, _parse_state);
210 $$ = list;
213 events:
214 events ',' event
216 struct list_head *event = $3;
217 struct list_head *list = $1;
219 /* frees $3 */
220 parse_events_update_lists(event, list);
221 $$ = list;
224 event
226 event: event_mod
228 event_mod:
229 event_name PE_MODIFIER_EVENT
231 struct list_head *list = $1;
232 int err;
235 * Apply modifier on all events added by single event definition
236 * (there could be more events added for multiple tracepoint
237 * definitions via '*?'.
239 err = parse_events__modifier_event(list, $2, false);
240 free($2);
241 if (err) {
242 free_list_evsel(list);
243 YYABORT;
245 $$ = list;
248 event_name
250 event_name:
251 PE_EVENT_NAME event_def
253 int err;
255 err = parse_events_name($2, $1);
256 free($1);
257 if (err) {
258 free_list_evsel($2);
259 YYABORT;
261 $$ = $2;
264 event_def
266 event_def: event_pmu |
267 event_legacy_symbol |
268 event_legacy_cache sep_dc |
269 event_legacy_mem |
270 event_legacy_tracepoint sep_dc |
271 event_legacy_numeric sep_dc |
272 event_legacy_raw sep_dc |
273 event_bpf_file
275 event_pmu:
276 PE_NAME opt_pmu_config
278 struct parse_events_state *parse_state = _parse_state;
279 struct parse_events_error *error = parse_state->error;
280 struct list_head *list = NULL, *orig_terms = NULL, *terms= NULL;
281 char *pattern = NULL;
283 #define CLEANUP_YYABORT \
284 do { \
285 parse_events_terms__delete($2); \
286 parse_events_terms__delete(orig_terms); \
287 free(list); \
288 free($1); \
289 free(pattern); \
290 YYABORT; \
291 } while(0)
293 if (parse_events_copy_term_list($2, &orig_terms))
294 CLEANUP_YYABORT;
296 if (error)
297 error->idx = @1.first_column;
299 list = alloc_list();
300 if (!list)
301 CLEANUP_YYABORT;
302 if (parse_events_add_pmu(_parse_state, list, $1, $2, false, false)) {
303 struct perf_pmu *pmu = NULL;
304 int ok = 0;
306 if (asprintf(&pattern, "%s*", $1) < 0)
307 CLEANUP_YYABORT;
309 while ((pmu = perf_pmu__scan(pmu)) != NULL) {
310 char *name = pmu->name;
312 if (!strncmp(name, "uncore_", 7) &&
313 strncmp($1, "uncore_", 7))
314 name += 7;
315 if (!fnmatch(pattern, name, 0)) {
316 if (parse_events_copy_term_list(orig_terms, &terms))
317 CLEANUP_YYABORT;
318 if (!parse_events_add_pmu(_parse_state, list, pmu->name, terms, true, false))
319 ok++;
320 parse_events_terms__delete(terms);
324 if (!ok)
325 CLEANUP_YYABORT;
327 parse_events_terms__delete($2);
328 parse_events_terms__delete(orig_terms);
329 free($1);
330 $$ = list;
331 #undef CLEANUP_YYABORT
334 PE_KERNEL_PMU_EVENT sep_dc
336 struct list_head *list;
337 int err;
339 err = parse_events_multi_pmu_add(_parse_state, $1, &list);
340 free($1);
341 if (err < 0)
342 YYABORT;
343 $$ = list;
346 PE_PMU_EVENT_PRE '-' PE_PMU_EVENT_SUF sep_dc
348 struct list_head *list;
349 char pmu_name[128];
351 snprintf(&pmu_name, 128, "%s-%s", $1, $3);
352 free($1);
353 free($3);
354 if (parse_events_multi_pmu_add(_parse_state, pmu_name, &list) < 0)
355 YYABORT;
356 $$ = list;
359 value_sym:
360 PE_VALUE_SYM_HW
362 PE_VALUE_SYM_SW
364 event_legacy_symbol:
365 value_sym '/' event_config '/'
367 struct list_head *list;
368 int type = $1 >> 16;
369 int config = $1 & 255;
370 int err;
372 list = alloc_list();
373 ABORT_ON(!list);
374 err = parse_events_add_numeric(_parse_state, list, type, config, $3);
375 parse_events_terms__delete($3);
376 if (err) {
377 free_list_evsel(list);
378 YYABORT;
380 $$ = list;
383 value_sym sep_slash_slash_dc
385 struct list_head *list;
386 int type = $1 >> 16;
387 int config = $1 & 255;
389 list = alloc_list();
390 ABORT_ON(!list);
391 ABORT_ON(parse_events_add_numeric(_parse_state, list, type, config, NULL));
392 $$ = list;
395 PE_VALUE_SYM_TOOL sep_slash_slash_dc
397 struct list_head *list;
399 list = alloc_list();
400 ABORT_ON(!list);
401 ABORT_ON(parse_events_add_tool(_parse_state, list, $1));
402 $$ = list;
405 event_legacy_cache:
406 PE_NAME_CACHE_TYPE '-' PE_NAME_CACHE_OP_RESULT '-' PE_NAME_CACHE_OP_RESULT opt_event_config
408 struct parse_events_state *parse_state = _parse_state;
409 struct parse_events_error *error = parse_state->error;
410 struct list_head *list;
411 int err;
413 list = alloc_list();
414 ABORT_ON(!list);
415 err = parse_events_add_cache(list, &parse_state->idx, $1, $3, $5, error, $6);
416 parse_events_terms__delete($6);
417 free($1);
418 free($3);
419 free($5);
420 if (err) {
421 free_list_evsel(list);
422 YYABORT;
424 $$ = list;
427 PE_NAME_CACHE_TYPE '-' PE_NAME_CACHE_OP_RESULT opt_event_config
429 struct parse_events_state *parse_state = _parse_state;
430 struct parse_events_error *error = parse_state->error;
431 struct list_head *list;
432 int err;
434 list = alloc_list();
435 ABORT_ON(!list);
436 err = parse_events_add_cache(list, &parse_state->idx, $1, $3, NULL, error, $4);
437 parse_events_terms__delete($4);
438 free($1);
439 free($3);
440 if (err) {
441 free_list_evsel(list);
442 YYABORT;
444 $$ = list;
447 PE_NAME_CACHE_TYPE opt_event_config
449 struct parse_events_state *parse_state = _parse_state;
450 struct parse_events_error *error = parse_state->error;
451 struct list_head *list;
452 int err;
454 list = alloc_list();
455 ABORT_ON(!list);
456 err = parse_events_add_cache(list, &parse_state->idx, $1, NULL, NULL, error, $2);
457 parse_events_terms__delete($2);
458 free($1);
459 if (err) {
460 free_list_evsel(list);
461 YYABORT;
463 $$ = list;
466 event_legacy_mem:
467 PE_PREFIX_MEM PE_VALUE '/' PE_VALUE ':' PE_MODIFIER_BP sep_dc
469 struct parse_events_state *parse_state = _parse_state;
470 struct list_head *list;
471 int err;
473 list = alloc_list();
474 ABORT_ON(!list);
475 err = parse_events_add_breakpoint(list, &parse_state->idx,
476 (void *) $2, $6, $4);
477 free($6);
478 if (err) {
479 free(list);
480 YYABORT;
482 $$ = list;
485 PE_PREFIX_MEM PE_VALUE '/' PE_VALUE sep_dc
487 struct parse_events_state *parse_state = _parse_state;
488 struct list_head *list;
490 list = alloc_list();
491 ABORT_ON(!list);
492 if (parse_events_add_breakpoint(list, &parse_state->idx,
493 (void *) $2, NULL, $4)) {
494 free(list);
495 YYABORT;
497 $$ = list;
500 PE_PREFIX_MEM PE_VALUE ':' PE_MODIFIER_BP sep_dc
502 struct parse_events_state *parse_state = _parse_state;
503 struct list_head *list;
504 int err;
506 list = alloc_list();
507 ABORT_ON(!list);
508 err = parse_events_add_breakpoint(list, &parse_state->idx,
509 (void *) $2, $4, 0);
510 free($4);
511 if (err) {
512 free(list);
513 YYABORT;
515 $$ = list;
518 PE_PREFIX_MEM PE_VALUE sep_dc
520 struct parse_events_state *parse_state = _parse_state;
521 struct list_head *list;
523 list = alloc_list();
524 ABORT_ON(!list);
525 if (parse_events_add_breakpoint(list, &parse_state->idx,
526 (void *) $2, NULL, 0)) {
527 free(list);
528 YYABORT;
530 $$ = list;
533 event_legacy_tracepoint:
534 tracepoint_name opt_event_config
536 struct parse_events_state *parse_state = _parse_state;
537 struct parse_events_error *error = parse_state->error;
538 struct list_head *list;
539 int err;
541 list = alloc_list();
542 ABORT_ON(!list);
543 if (error)
544 error->idx = @1.first_column;
546 err = parse_events_add_tracepoint(list, &parse_state->idx, $1.sys, $1.event,
547 error, $2);
549 parse_events_terms__delete($2);
550 free($1.sys);
551 free($1.event);
552 if (err) {
553 free(list);
554 YYABORT;
556 $$ = list;
559 tracepoint_name:
560 PE_NAME '-' PE_NAME ':' PE_NAME
562 struct tracepoint_name tracepoint;
564 ABORT_ON(asprintf(&tracepoint.sys, "%s-%s", $1, $3) < 0);
565 tracepoint.event = $5;
566 free($1);
567 free($3);
568 $$ = tracepoint;
571 PE_NAME ':' PE_NAME
573 struct tracepoint_name tracepoint = {$1, $3};
575 $$ = tracepoint;
578 event_legacy_numeric:
579 PE_VALUE ':' PE_VALUE opt_event_config
581 struct list_head *list;
582 int err;
584 list = alloc_list();
585 ABORT_ON(!list);
586 err = parse_events_add_numeric(_parse_state, list, (u32)$1, $3, $4);
587 parse_events_terms__delete($4);
588 if (err) {
589 free(list);
590 YYABORT;
592 $$ = list;
595 event_legacy_raw:
596 PE_RAW opt_event_config
598 struct list_head *list;
599 int err;
601 list = alloc_list();
602 ABORT_ON(!list);
603 err = parse_events_add_numeric(_parse_state, list, PERF_TYPE_RAW, $1, $2);
604 parse_events_terms__delete($2);
605 if (err) {
606 free(list);
607 YYABORT;
609 $$ = list;
612 event_bpf_file:
613 PE_BPF_OBJECT opt_event_config
615 struct parse_events_state *parse_state = _parse_state;
616 struct list_head *list;
617 int err;
619 list = alloc_list();
620 ABORT_ON(!list);
621 err = parse_events_load_bpf(parse_state, list, $1, false, $2);
622 parse_events_terms__delete($2);
623 free($1);
624 if (err) {
625 free(list);
626 YYABORT;
628 $$ = list;
631 PE_BPF_SOURCE opt_event_config
633 struct list_head *list;
634 int err;
636 list = alloc_list();
637 ABORT_ON(!list);
638 err = parse_events_load_bpf(_parse_state, list, $1, true, $2);
639 parse_events_terms__delete($2);
640 if (err) {
641 free(list);
642 YYABORT;
644 $$ = list;
647 opt_event_config:
648 '/' event_config '/'
650 $$ = $2;
653 '/' '/'
655 $$ = NULL;
659 $$ = NULL;
662 opt_pmu_config:
663 '/' event_config '/'
665 $$ = $2;
668 '/' '/'
670 $$ = NULL;
673 start_terms: event_config
675 struct parse_events_state *parse_state = _parse_state;
676 if (parse_state->terms) {
677 parse_events_terms__delete ($1);
678 YYABORT;
680 parse_state->terms = $1;
683 event_config:
684 event_config ',' event_term
686 struct list_head *head = $1;
687 struct parse_events_term *term = $3;
689 if (!head) {
690 parse_events_term__delete(term);
691 YYABORT;
693 list_add_tail(&term->list, head);
694 $$ = $1;
697 event_term
699 struct list_head *head = malloc(sizeof(*head));
700 struct parse_events_term *term = $1;
702 ABORT_ON(!head);
703 INIT_LIST_HEAD(head);
704 list_add_tail(&term->list, head);
705 $$ = head;
708 event_term:
709 PE_NAME '=' PE_NAME
711 struct parse_events_term *term;
713 if (parse_events_term__str(&term, PARSE_EVENTS__TERM_TYPE_USER,
714 $1, $3, &@1, &@3)) {
715 free($1);
716 free($3);
717 YYABORT;
719 $$ = term;
722 PE_NAME '=' PE_VALUE
724 struct parse_events_term *term;
726 if (parse_events_term__num(&term, PARSE_EVENTS__TERM_TYPE_USER,
727 $1, $3, false, &@1, &@3)) {
728 free($1);
729 YYABORT;
731 $$ = term;
734 PE_NAME '=' PE_VALUE_SYM_HW
736 struct parse_events_term *term;
737 int config = $3 & 255;
739 if (parse_events_term__sym_hw(&term, $1, config)) {
740 free($1);
741 YYABORT;
743 $$ = term;
746 PE_NAME
748 struct parse_events_term *term;
750 if (parse_events_term__num(&term, PARSE_EVENTS__TERM_TYPE_USER,
751 $1, 1, true, &@1, NULL)) {
752 free($1);
753 YYABORT;
755 $$ = term;
758 PE_VALUE_SYM_HW
760 struct parse_events_term *term;
761 int config = $1 & 255;
763 ABORT_ON(parse_events_term__sym_hw(&term, NULL, config));
764 $$ = term;
767 PE_TERM '=' PE_NAME
769 struct parse_events_term *term;
771 if (parse_events_term__str(&term, (int)$1, NULL, $3, &@1, &@3)) {
772 free($3);
773 YYABORT;
775 $$ = term;
778 PE_TERM '=' PE_VALUE
780 struct parse_events_term *term;
782 ABORT_ON(parse_events_term__num(&term, (int)$1, NULL, $3, false, &@1, &@3));
783 $$ = term;
786 PE_TERM
788 struct parse_events_term *term;
790 ABORT_ON(parse_events_term__num(&term, (int)$1, NULL, 1, true, &@1, NULL));
791 $$ = term;
794 PE_NAME array '=' PE_NAME
796 struct parse_events_term *term;
798 if (parse_events_term__str(&term, PARSE_EVENTS__TERM_TYPE_USER,
799 $1, $4, &@1, &@4)) {
800 free($1);
801 free($4);
802 free($2.ranges);
803 YYABORT;
805 term->array = $2;
806 $$ = term;
809 PE_NAME array '=' PE_VALUE
811 struct parse_events_term *term;
813 if (parse_events_term__num(&term, PARSE_EVENTS__TERM_TYPE_USER,
814 $1, $4, false, &@1, &@4)) {
815 free($1);
816 free($2.ranges);
817 YYABORT;
819 term->array = $2;
820 $$ = term;
823 PE_DRV_CFG_TERM
825 struct parse_events_term *term;
826 char *config = strdup($1);
828 ABORT_ON(!config);
829 if (parse_events_term__str(&term, PARSE_EVENTS__TERM_TYPE_DRV_CFG,
830 config, $1, &@1, NULL)) {
831 free($1);
832 free(config);
833 YYABORT;
835 $$ = term;
838 array:
839 '[' array_terms ']'
841 $$ = $2;
844 PE_ARRAY_ALL
846 $$.nr_ranges = 0;
847 $$.ranges = NULL;
850 array_terms:
851 array_terms ',' array_term
853 struct parse_events_array new_array;
855 new_array.nr_ranges = $1.nr_ranges + $3.nr_ranges;
856 new_array.ranges = realloc($1.ranges,
857 sizeof(new_array.ranges[0]) *
858 new_array.nr_ranges);
859 ABORT_ON(!new_array.ranges);
860 memcpy(&new_array.ranges[$1.nr_ranges], $3.ranges,
861 $3.nr_ranges * sizeof(new_array.ranges[0]));
862 free($3.ranges);
863 $$ = new_array;
866 array_term
868 array_term:
869 PE_VALUE
871 struct parse_events_array array;
873 array.nr_ranges = 1;
874 array.ranges = malloc(sizeof(array.ranges[0]));
875 ABORT_ON(!array.ranges);
876 array.ranges[0].start = $1;
877 array.ranges[0].length = 1;
878 $$ = array;
881 PE_VALUE PE_ARRAY_RANGE PE_VALUE
883 struct parse_events_array array;
885 ABORT_ON($3 < $1);
886 array.nr_ranges = 1;
887 array.ranges = malloc(sizeof(array.ranges[0]));
888 ABORT_ON(!array.ranges);
889 array.ranges[0].start = $1;
890 array.ranges[0].length = $3 - $1 + 1;
891 $$ = array;
894 sep_dc: ':' |
896 sep_slash_slash_dc: '/' '/' | ':' |
900 void parse_events_error(YYLTYPE *loc, void *parse_state,
901 void *scanner __maybe_unused,
902 char const *msg __maybe_unused)
904 parse_events_evlist_error(parse_state, loc->last_column, "parser error");