perf list: Don't print Unit for "default_core"
[platform/kernel/linux-starfive.git] / tools / perf / util / parse-events.c
1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/hw_breakpoint.h>
3 #include <linux/err.h>
4 #include <linux/list_sort.h>
5 #include <linux/zalloc.h>
6 #include <dirent.h>
7 #include <errno.h>
8 #include <sys/ioctl.h>
9 #include <sys/param.h>
10 #include "term.h"
11 #include "evlist.h"
12 #include "evsel.h"
13 #include <subcmd/parse-options.h>
14 #include "parse-events.h"
15 #include "string2.h"
16 #include "strbuf.h"
17 #include "debug.h"
18 #include <api/fs/tracing_path.h>
19 #include <perf/cpumap.h>
20 #include <util/parse-events-bison.h>
21 #include <util/parse-events-flex.h>
22 #include "pmu.h"
23 #include "pmus.h"
24 #include "asm/bug.h"
25 #include "util/parse-branch-options.h"
26 #include "util/evsel_config.h"
27 #include "util/event.h"
28 #include "util/bpf-filter.h"
29 #include "util/util.h"
30 #include "tracepoint.h"
31
32 #define MAX_NAME_LEN 100
33
34 #ifdef PARSER_DEBUG
35 extern int parse_events_debug;
36 #endif
37 static int get_config_terms(struct list_head *head_config,
38                             struct list_head *head_terms __maybe_unused);
39
40 struct event_symbol event_symbols_hw[PERF_COUNT_HW_MAX] = {
41         [PERF_COUNT_HW_CPU_CYCLES] = {
42                 .symbol = "cpu-cycles",
43                 .alias  = "cycles",
44         },
45         [PERF_COUNT_HW_INSTRUCTIONS] = {
46                 .symbol = "instructions",
47                 .alias  = "",
48         },
49         [PERF_COUNT_HW_CACHE_REFERENCES] = {
50                 .symbol = "cache-references",
51                 .alias  = "",
52         },
53         [PERF_COUNT_HW_CACHE_MISSES] = {
54                 .symbol = "cache-misses",
55                 .alias  = "",
56         },
57         [PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = {
58                 .symbol = "branch-instructions",
59                 .alias  = "branches",
60         },
61         [PERF_COUNT_HW_BRANCH_MISSES] = {
62                 .symbol = "branch-misses",
63                 .alias  = "",
64         },
65         [PERF_COUNT_HW_BUS_CYCLES] = {
66                 .symbol = "bus-cycles",
67                 .alias  = "",
68         },
69         [PERF_COUNT_HW_STALLED_CYCLES_FRONTEND] = {
70                 .symbol = "stalled-cycles-frontend",
71                 .alias  = "idle-cycles-frontend",
72         },
73         [PERF_COUNT_HW_STALLED_CYCLES_BACKEND] = {
74                 .symbol = "stalled-cycles-backend",
75                 .alias  = "idle-cycles-backend",
76         },
77         [PERF_COUNT_HW_REF_CPU_CYCLES] = {
78                 .symbol = "ref-cycles",
79                 .alias  = "",
80         },
81 };
82
83 struct event_symbol event_symbols_sw[PERF_COUNT_SW_MAX] = {
84         [PERF_COUNT_SW_CPU_CLOCK] = {
85                 .symbol = "cpu-clock",
86                 .alias  = "",
87         },
88         [PERF_COUNT_SW_TASK_CLOCK] = {
89                 .symbol = "task-clock",
90                 .alias  = "",
91         },
92         [PERF_COUNT_SW_PAGE_FAULTS] = {
93                 .symbol = "page-faults",
94                 .alias  = "faults",
95         },
96         [PERF_COUNT_SW_CONTEXT_SWITCHES] = {
97                 .symbol = "context-switches",
98                 .alias  = "cs",
99         },
100         [PERF_COUNT_SW_CPU_MIGRATIONS] = {
101                 .symbol = "cpu-migrations",
102                 .alias  = "migrations",
103         },
104         [PERF_COUNT_SW_PAGE_FAULTS_MIN] = {
105                 .symbol = "minor-faults",
106                 .alias  = "",
107         },
108         [PERF_COUNT_SW_PAGE_FAULTS_MAJ] = {
109                 .symbol = "major-faults",
110                 .alias  = "",
111         },
112         [PERF_COUNT_SW_ALIGNMENT_FAULTS] = {
113                 .symbol = "alignment-faults",
114                 .alias  = "",
115         },
116         [PERF_COUNT_SW_EMULATION_FAULTS] = {
117                 .symbol = "emulation-faults",
118                 .alias  = "",
119         },
120         [PERF_COUNT_SW_DUMMY] = {
121                 .symbol = "dummy",
122                 .alias  = "",
123         },
124         [PERF_COUNT_SW_BPF_OUTPUT] = {
125                 .symbol = "bpf-output",
126                 .alias  = "",
127         },
128         [PERF_COUNT_SW_CGROUP_SWITCHES] = {
129                 .symbol = "cgroup-switches",
130                 .alias  = "",
131         },
132 };
133
134 const char *event_type(int type)
135 {
136         switch (type) {
137         case PERF_TYPE_HARDWARE:
138                 return "hardware";
139
140         case PERF_TYPE_SOFTWARE:
141                 return "software";
142
143         case PERF_TYPE_TRACEPOINT:
144                 return "tracepoint";
145
146         case PERF_TYPE_HW_CACHE:
147                 return "hardware-cache";
148
149         default:
150                 break;
151         }
152
153         return "unknown";
154 }
155
156 static char *get_config_str(struct list_head *head_terms, int type_term)
157 {
158         struct parse_events_term *term;
159
160         if (!head_terms)
161                 return NULL;
162
163         list_for_each_entry(term, head_terms, list)
164                 if (term->type_term == type_term)
165                         return term->val.str;
166
167         return NULL;
168 }
169
170 static char *get_config_metric_id(struct list_head *head_terms)
171 {
172         return get_config_str(head_terms, PARSE_EVENTS__TERM_TYPE_METRIC_ID);
173 }
174
175 static char *get_config_name(struct list_head *head_terms)
176 {
177         return get_config_str(head_terms, PARSE_EVENTS__TERM_TYPE_NAME);
178 }
179
180 /**
181  * fix_raw - For each raw term see if there is an event (aka alias) in pmu that
182  *           matches the raw's string value. If the string value matches an
183  *           event then change the term to be an event, if not then change it to
184  *           be a config term. For example, "read" may be an event of the PMU or
185  *           a raw hex encoding of 0xead. The fix-up is done late so the PMU of
186  *           the event can be determined and we don't need to scan all PMUs
187  *           ahead-of-time.
188  * @config_terms: the list of terms that may contain a raw term.
189  * @pmu: the PMU to scan for events from.
190  */
191 static void fix_raw(struct list_head *config_terms, struct perf_pmu *pmu)
192 {
193         struct parse_events_term *term;
194
195         list_for_each_entry(term, config_terms, list) {
196                 u64 num;
197
198                 if (term->type_term != PARSE_EVENTS__TERM_TYPE_RAW)
199                         continue;
200
201                 if (perf_pmu__have_event(pmu, term->val.str)) {
202                         zfree(&term->config);
203                         term->config = term->val.str;
204                         term->type_val = PARSE_EVENTS__TERM_TYPE_NUM;
205                         term->type_term = PARSE_EVENTS__TERM_TYPE_USER;
206                         term->val.num = 1;
207                         term->no_value = true;
208                         continue;
209                 }
210
211                 zfree(&term->config);
212                 term->config = strdup("config");
213                 errno = 0;
214                 num = strtoull(term->val.str + 1, NULL, 16);
215                 assert(errno == 0);
216                 free(term->val.str);
217                 term->type_val = PARSE_EVENTS__TERM_TYPE_NUM;
218                 term->type_term = PARSE_EVENTS__TERM_TYPE_CONFIG;
219                 term->val.num = num;
220                 term->no_value = false;
221         }
222 }
223
224 static struct evsel *
225 __add_event(struct list_head *list, int *idx,
226             struct perf_event_attr *attr,
227             bool init_attr,
228             const char *name, const char *metric_id, struct perf_pmu *pmu,
229             struct list_head *config_terms, bool auto_merge_stats,
230             const char *cpu_list)
231 {
232         struct evsel *evsel;
233         struct perf_cpu_map *cpus = pmu ? perf_cpu_map__get(pmu->cpus) :
234                                cpu_list ? perf_cpu_map__new(cpu_list) : NULL;
235
236         if (pmu)
237                 perf_pmu__warn_invalid_formats(pmu);
238
239         if (pmu && (attr->type == PERF_TYPE_RAW || attr->type >= PERF_TYPE_MAX)) {
240                 perf_pmu__warn_invalid_config(pmu, attr->config, name,
241                                               PERF_PMU_FORMAT_VALUE_CONFIG, "config");
242                 perf_pmu__warn_invalid_config(pmu, attr->config1, name,
243                                               PERF_PMU_FORMAT_VALUE_CONFIG1, "config1");
244                 perf_pmu__warn_invalid_config(pmu, attr->config2, name,
245                                               PERF_PMU_FORMAT_VALUE_CONFIG2, "config2");
246                 perf_pmu__warn_invalid_config(pmu, attr->config3, name,
247                                               PERF_PMU_FORMAT_VALUE_CONFIG3, "config3");
248         }
249         if (init_attr)
250                 event_attr_init(attr);
251
252         evsel = evsel__new_idx(attr, *idx);
253         if (!evsel) {
254                 perf_cpu_map__put(cpus);
255                 return NULL;
256         }
257
258         (*idx)++;
259         evsel->core.cpus = cpus;
260         evsel->core.own_cpus = perf_cpu_map__get(cpus);
261         evsel->core.requires_cpu = pmu ? pmu->is_uncore : false;
262         evsel->core.is_pmu_core = pmu ? pmu->is_core : false;
263         evsel->auto_merge_stats = auto_merge_stats;
264         evsel->pmu = pmu;
265         evsel->pmu_name = pmu ? strdup(pmu->name) : NULL;
266
267         if (name)
268                 evsel->name = strdup(name);
269
270         if (metric_id)
271                 evsel->metric_id = strdup(metric_id);
272
273         if (config_terms)
274                 list_splice_init(config_terms, &evsel->config_terms);
275
276         if (list)
277                 list_add_tail(&evsel->core.node, list);
278
279         return evsel;
280 }
281
282 struct evsel *parse_events__add_event(int idx, struct perf_event_attr *attr,
283                                       const char *name, const char *metric_id,
284                                       struct perf_pmu *pmu)
285 {
286         return __add_event(/*list=*/NULL, &idx, attr, /*init_attr=*/false, name,
287                            metric_id, pmu, /*config_terms=*/NULL,
288                            /*auto_merge_stats=*/false, /*cpu_list=*/NULL);
289 }
290
291 static int add_event(struct list_head *list, int *idx,
292                      struct perf_event_attr *attr, const char *name,
293                      const char *metric_id, struct list_head *config_terms)
294 {
295         return __add_event(list, idx, attr, /*init_attr*/true, name, metric_id,
296                            /*pmu=*/NULL, config_terms,
297                            /*auto_merge_stats=*/false, /*cpu_list=*/NULL) ? 0 : -ENOMEM;
298 }
299
300 static int add_event_tool(struct list_head *list, int *idx,
301                           enum perf_tool_event tool_event)
302 {
303         struct evsel *evsel;
304         struct perf_event_attr attr = {
305                 .type = PERF_TYPE_SOFTWARE,
306                 .config = PERF_COUNT_SW_DUMMY,
307         };
308
309         evsel = __add_event(list, idx, &attr, /*init_attr=*/true, /*name=*/NULL,
310                             /*metric_id=*/NULL, /*pmu=*/NULL,
311                             /*config_terms=*/NULL, /*auto_merge_stats=*/false,
312                             /*cpu_list=*/"0");
313         if (!evsel)
314                 return -ENOMEM;
315         evsel->tool_event = tool_event;
316         if (tool_event == PERF_TOOL_DURATION_TIME
317             || tool_event == PERF_TOOL_USER_TIME
318             || tool_event == PERF_TOOL_SYSTEM_TIME) {
319                 free((char *)evsel->unit);
320                 evsel->unit = strdup("ns");
321         }
322         return 0;
323 }
324
325 /**
326  * parse_aliases - search names for entries beginning or equalling str ignoring
327  *                 case. If mutliple entries in names match str then the longest
328  *                 is chosen.
329  * @str: The needle to look for.
330  * @names: The haystack to search.
331  * @size: The size of the haystack.
332  * @longest: Out argument giving the length of the matching entry.
333  */
334 static int parse_aliases(const char *str, const char *const names[][EVSEL__MAX_ALIASES], int size,
335                          int *longest)
336 {
337         *longest = -1;
338         for (int i = 0; i < size; i++) {
339                 for (int j = 0; j < EVSEL__MAX_ALIASES && names[i][j]; j++) {
340                         int n = strlen(names[i][j]);
341
342                         if (n > *longest && !strncasecmp(str, names[i][j], n))
343                                 *longest = n;
344                 }
345                 if (*longest > 0)
346                         return i;
347         }
348
349         return -1;
350 }
351
352 typedef int config_term_func_t(struct perf_event_attr *attr,
353                                struct parse_events_term *term,
354                                struct parse_events_error *err);
355 static int config_term_common(struct perf_event_attr *attr,
356                               struct parse_events_term *term,
357                               struct parse_events_error *err);
358 static int config_attr(struct perf_event_attr *attr,
359                        struct list_head *head,
360                        struct parse_events_error *err,
361                        config_term_func_t config_term);
362
363 /**
364  * parse_events__decode_legacy_cache - Search name for the legacy cache event
365  *                                     name composed of 1, 2 or 3 hyphen
366  *                                     separated sections. The first section is
367  *                                     the cache type while the others are the
368  *                                     optional op and optional result. To make
369  *                                     life hard the names in the table also
370  *                                     contain hyphens and the longest name
371  *                                     should always be selected.
372  */
373 int parse_events__decode_legacy_cache(const char *name, int extended_pmu_type, __u64 *config)
374 {
375         int len, cache_type = -1, cache_op = -1, cache_result = -1;
376         const char *name_end = &name[strlen(name) + 1];
377         const char *str = name;
378
379         cache_type = parse_aliases(str, evsel__hw_cache, PERF_COUNT_HW_CACHE_MAX, &len);
380         if (cache_type == -1)
381                 return -EINVAL;
382         str += len + 1;
383
384         if (str < name_end) {
385                 cache_op = parse_aliases(str, evsel__hw_cache_op,
386                                         PERF_COUNT_HW_CACHE_OP_MAX, &len);
387                 if (cache_op >= 0) {
388                         if (!evsel__is_cache_op_valid(cache_type, cache_op))
389                                 return -EINVAL;
390                         str += len + 1;
391                 } else {
392                         cache_result = parse_aliases(str, evsel__hw_cache_result,
393                                                 PERF_COUNT_HW_CACHE_RESULT_MAX, &len);
394                         if (cache_result >= 0)
395                                 str += len + 1;
396                 }
397         }
398         if (str < name_end) {
399                 if (cache_op < 0) {
400                         cache_op = parse_aliases(str, evsel__hw_cache_op,
401                                                 PERF_COUNT_HW_CACHE_OP_MAX, &len);
402                         if (cache_op >= 0) {
403                                 if (!evsel__is_cache_op_valid(cache_type, cache_op))
404                                         return -EINVAL;
405                         }
406                 } else if (cache_result < 0) {
407                         cache_result = parse_aliases(str, evsel__hw_cache_result,
408                                                 PERF_COUNT_HW_CACHE_RESULT_MAX, &len);
409                 }
410         }
411
412         /*
413          * Fall back to reads:
414          */
415         if (cache_op == -1)
416                 cache_op = PERF_COUNT_HW_CACHE_OP_READ;
417
418         /*
419          * Fall back to accesses:
420          */
421         if (cache_result == -1)
422                 cache_result = PERF_COUNT_HW_CACHE_RESULT_ACCESS;
423
424         *config = cache_type | (cache_op << 8) | (cache_result << 16);
425         if (perf_pmus__supports_extended_type())
426                 *config |= (__u64)extended_pmu_type << PERF_PMU_TYPE_SHIFT;
427         return 0;
428 }
429
430 /**
431  * parse_events__filter_pmu - returns false if a wildcard PMU should be
432  *                            considered, true if it should be filtered.
433  */
434 bool parse_events__filter_pmu(const struct parse_events_state *parse_state,
435                               const struct perf_pmu *pmu)
436 {
437         if (parse_state->pmu_filter == NULL)
438                 return false;
439
440         return strcmp(parse_state->pmu_filter, pmu->name) != 0;
441 }
442
443 int parse_events_add_cache(struct list_head *list, int *idx, const char *name,
444                            struct parse_events_state *parse_state,
445                            struct list_head *head_config)
446 {
447         struct perf_pmu *pmu = NULL;
448         bool found_supported = false;
449         const char *config_name = get_config_name(head_config);
450         const char *metric_id = get_config_metric_id(head_config);
451
452         /* Legacy cache events are only supported by core PMUs. */
453         while ((pmu = perf_pmus__scan_core(pmu)) != NULL) {
454                 LIST_HEAD(config_terms);
455                 struct perf_event_attr attr;
456                 int ret;
457
458                 if (parse_events__filter_pmu(parse_state, pmu))
459                         continue;
460
461                 memset(&attr, 0, sizeof(attr));
462                 attr.type = PERF_TYPE_HW_CACHE;
463
464                 ret = parse_events__decode_legacy_cache(name, pmu->type, &attr.config);
465                 if (ret)
466                         return ret;
467
468                 found_supported = true;
469
470                 if (head_config) {
471                         if (config_attr(&attr, head_config, parse_state->error, config_term_common))
472                                 return -EINVAL;
473
474                         if (get_config_terms(head_config, &config_terms))
475                                 return -ENOMEM;
476                 }
477
478                 if (__add_event(list, idx, &attr, /*init_attr*/true, config_name ?: name,
479                                 metric_id, pmu, &config_terms, /*auto_merge_stats=*/false,
480                                 /*cpu_list=*/NULL) == NULL)
481                         return -ENOMEM;
482
483                 free_config_terms(&config_terms);
484         }
485         return found_supported ? 0 : -EINVAL;
486 }
487
488 #ifdef HAVE_LIBTRACEEVENT
489 static void tracepoint_error(struct parse_events_error *e, int err,
490                              const char *sys, const char *name, int column)
491 {
492         const char *str;
493         char help[BUFSIZ];
494
495         if (!e)
496                 return;
497
498         /*
499          * We get error directly from syscall errno ( > 0),
500          * or from encoded pointer's error ( < 0).
501          */
502         err = abs(err);
503
504         switch (err) {
505         case EACCES:
506                 str = "can't access trace events";
507                 break;
508         case ENOENT:
509                 str = "unknown tracepoint";
510                 break;
511         default:
512                 str = "failed to add tracepoint";
513                 break;
514         }
515
516         tracing_path__strerror_open_tp(err, help, sizeof(help), sys, name);
517         parse_events_error__handle(e, column, strdup(str), strdup(help));
518 }
519
520 static int add_tracepoint(struct list_head *list, int *idx,
521                           const char *sys_name, const char *evt_name,
522                           struct parse_events_error *err,
523                           struct list_head *head_config, void *loc_)
524 {
525         YYLTYPE *loc = loc_;
526         struct evsel *evsel = evsel__newtp_idx(sys_name, evt_name, (*idx)++);
527
528         if (IS_ERR(evsel)) {
529                 tracepoint_error(err, PTR_ERR(evsel), sys_name, evt_name, loc->first_column);
530                 return PTR_ERR(evsel);
531         }
532
533         if (head_config) {
534                 LIST_HEAD(config_terms);
535
536                 if (get_config_terms(head_config, &config_terms))
537                         return -ENOMEM;
538                 list_splice(&config_terms, &evsel->config_terms);
539         }
540
541         list_add_tail(&evsel->core.node, list);
542         return 0;
543 }
544
545 static int add_tracepoint_multi_event(struct list_head *list, int *idx,
546                                       const char *sys_name, const char *evt_name,
547                                       struct parse_events_error *err,
548                                       struct list_head *head_config, YYLTYPE *loc)
549 {
550         char *evt_path;
551         struct dirent *evt_ent;
552         DIR *evt_dir;
553         int ret = 0, found = 0;
554
555         evt_path = get_events_file(sys_name);
556         if (!evt_path) {
557                 tracepoint_error(err, errno, sys_name, evt_name, loc->first_column);
558                 return -1;
559         }
560         evt_dir = opendir(evt_path);
561         if (!evt_dir) {
562                 put_events_file(evt_path);
563                 tracepoint_error(err, errno, sys_name, evt_name, loc->first_column);
564                 return -1;
565         }
566
567         while (!ret && (evt_ent = readdir(evt_dir))) {
568                 if (!strcmp(evt_ent->d_name, ".")
569                     || !strcmp(evt_ent->d_name, "..")
570                     || !strcmp(evt_ent->d_name, "enable")
571                     || !strcmp(evt_ent->d_name, "filter"))
572                         continue;
573
574                 if (!strglobmatch(evt_ent->d_name, evt_name))
575                         continue;
576
577                 found++;
578
579                 ret = add_tracepoint(list, idx, sys_name, evt_ent->d_name,
580                                      err, head_config, loc);
581         }
582
583         if (!found) {
584                 tracepoint_error(err, ENOENT, sys_name, evt_name, loc->first_column);
585                 ret = -1;
586         }
587
588         put_events_file(evt_path);
589         closedir(evt_dir);
590         return ret;
591 }
592
593 static int add_tracepoint_event(struct list_head *list, int *idx,
594                                 const char *sys_name, const char *evt_name,
595                                 struct parse_events_error *err,
596                                 struct list_head *head_config, YYLTYPE *loc)
597 {
598         return strpbrk(evt_name, "*?") ?
599                 add_tracepoint_multi_event(list, idx, sys_name, evt_name,
600                                            err, head_config, loc) :
601                 add_tracepoint(list, idx, sys_name, evt_name,
602                                err, head_config, loc);
603 }
604
605 static int add_tracepoint_multi_sys(struct list_head *list, int *idx,
606                                     const char *sys_name, const char *evt_name,
607                                     struct parse_events_error *err,
608                                     struct list_head *head_config, YYLTYPE *loc)
609 {
610         struct dirent *events_ent;
611         DIR *events_dir;
612         int ret = 0;
613
614         events_dir = tracing_events__opendir();
615         if (!events_dir) {
616                 tracepoint_error(err, errno, sys_name, evt_name, loc->first_column);
617                 return -1;
618         }
619
620         while (!ret && (events_ent = readdir(events_dir))) {
621                 if (!strcmp(events_ent->d_name, ".")
622                     || !strcmp(events_ent->d_name, "..")
623                     || !strcmp(events_ent->d_name, "enable")
624                     || !strcmp(events_ent->d_name, "header_event")
625                     || !strcmp(events_ent->d_name, "header_page"))
626                         continue;
627
628                 if (!strglobmatch(events_ent->d_name, sys_name))
629                         continue;
630
631                 ret = add_tracepoint_event(list, idx, events_ent->d_name,
632                                            evt_name, err, head_config, loc);
633         }
634
635         closedir(events_dir);
636         return ret;
637 }
638 #endif /* HAVE_LIBTRACEEVENT */
639
640 static int
641 parse_breakpoint_type(const char *type, struct perf_event_attr *attr)
642 {
643         int i;
644
645         for (i = 0; i < 3; i++) {
646                 if (!type || !type[i])
647                         break;
648
649 #define CHECK_SET_TYPE(bit)             \
650 do {                                    \
651         if (attr->bp_type & bit)        \
652                 return -EINVAL;         \
653         else                            \
654                 attr->bp_type |= bit;   \
655 } while (0)
656
657                 switch (type[i]) {
658                 case 'r':
659                         CHECK_SET_TYPE(HW_BREAKPOINT_R);
660                         break;
661                 case 'w':
662                         CHECK_SET_TYPE(HW_BREAKPOINT_W);
663                         break;
664                 case 'x':
665                         CHECK_SET_TYPE(HW_BREAKPOINT_X);
666                         break;
667                 default:
668                         return -EINVAL;
669                 }
670         }
671
672 #undef CHECK_SET_TYPE
673
674         if (!attr->bp_type) /* Default */
675                 attr->bp_type = HW_BREAKPOINT_R | HW_BREAKPOINT_W;
676
677         return 0;
678 }
679
680 int parse_events_add_breakpoint(struct parse_events_state *parse_state,
681                                 struct list_head *list,
682                                 u64 addr, char *type, u64 len,
683                                 struct list_head *head_config __maybe_unused)
684 {
685         struct perf_event_attr attr;
686         LIST_HEAD(config_terms);
687         const char *name;
688
689         memset(&attr, 0, sizeof(attr));
690         attr.bp_addr = addr;
691
692         if (parse_breakpoint_type(type, &attr))
693                 return -EINVAL;
694
695         /* Provide some defaults if len is not specified */
696         if (!len) {
697                 if (attr.bp_type == HW_BREAKPOINT_X)
698                         len = sizeof(long);
699                 else
700                         len = HW_BREAKPOINT_LEN_4;
701         }
702
703         attr.bp_len = len;
704
705         attr.type = PERF_TYPE_BREAKPOINT;
706         attr.sample_period = 1;
707
708         if (head_config) {
709                 if (config_attr(&attr, head_config, parse_state->error,
710                                 config_term_common))
711                         return -EINVAL;
712
713                 if (get_config_terms(head_config, &config_terms))
714                         return -ENOMEM;
715         }
716
717         name = get_config_name(head_config);
718
719         return add_event(list, &parse_state->idx, &attr, name, /*mertic_id=*/NULL,
720                          &config_terms);
721 }
722
723 static int check_type_val(struct parse_events_term *term,
724                           struct parse_events_error *err,
725                           int type)
726 {
727         if (type == term->type_val)
728                 return 0;
729
730         if (err) {
731                 parse_events_error__handle(err, term->err_val,
732                                         type == PARSE_EVENTS__TERM_TYPE_NUM
733                                         ? strdup("expected numeric value")
734                                         : strdup("expected string value"),
735                                         NULL);
736         }
737         return -EINVAL;
738 }
739
740 /*
741  * Update according to parse-events.l
742  */
743 static const char *config_term_names[__PARSE_EVENTS__TERM_TYPE_NR] = {
744         [PARSE_EVENTS__TERM_TYPE_USER]                  = "<sysfs term>",
745         [PARSE_EVENTS__TERM_TYPE_CONFIG]                = "config",
746         [PARSE_EVENTS__TERM_TYPE_CONFIG1]               = "config1",
747         [PARSE_EVENTS__TERM_TYPE_CONFIG2]               = "config2",
748         [PARSE_EVENTS__TERM_TYPE_CONFIG3]               = "config3",
749         [PARSE_EVENTS__TERM_TYPE_NAME]                  = "name",
750         [PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD]         = "period",
751         [PARSE_EVENTS__TERM_TYPE_SAMPLE_FREQ]           = "freq",
752         [PARSE_EVENTS__TERM_TYPE_BRANCH_SAMPLE_TYPE]    = "branch_type",
753         [PARSE_EVENTS__TERM_TYPE_TIME]                  = "time",
754         [PARSE_EVENTS__TERM_TYPE_CALLGRAPH]             = "call-graph",
755         [PARSE_EVENTS__TERM_TYPE_STACKSIZE]             = "stack-size",
756         [PARSE_EVENTS__TERM_TYPE_NOINHERIT]             = "no-inherit",
757         [PARSE_EVENTS__TERM_TYPE_INHERIT]               = "inherit",
758         [PARSE_EVENTS__TERM_TYPE_MAX_STACK]             = "max-stack",
759         [PARSE_EVENTS__TERM_TYPE_MAX_EVENTS]            = "nr",
760         [PARSE_EVENTS__TERM_TYPE_OVERWRITE]             = "overwrite",
761         [PARSE_EVENTS__TERM_TYPE_NOOVERWRITE]           = "no-overwrite",
762         [PARSE_EVENTS__TERM_TYPE_DRV_CFG]               = "driver-config",
763         [PARSE_EVENTS__TERM_TYPE_PERCORE]               = "percore",
764         [PARSE_EVENTS__TERM_TYPE_AUX_OUTPUT]            = "aux-output",
765         [PARSE_EVENTS__TERM_TYPE_AUX_SAMPLE_SIZE]       = "aux-sample-size",
766         [PARSE_EVENTS__TERM_TYPE_METRIC_ID]             = "metric-id",
767         [PARSE_EVENTS__TERM_TYPE_RAW]                   = "raw",
768         [PARSE_EVENTS__TERM_TYPE_LEGACY_CACHE]          = "legacy-cache",
769         [PARSE_EVENTS__TERM_TYPE_HARDWARE]              = "hardware",
770 };
771
772 static bool config_term_shrinked;
773
774 static bool
775 config_term_avail(int term_type, struct parse_events_error *err)
776 {
777         char *err_str;
778
779         if (term_type < 0 || term_type >= __PARSE_EVENTS__TERM_TYPE_NR) {
780                 parse_events_error__handle(err, -1,
781                                         strdup("Invalid term_type"), NULL);
782                 return false;
783         }
784         if (!config_term_shrinked)
785                 return true;
786
787         switch (term_type) {
788         case PARSE_EVENTS__TERM_TYPE_CONFIG:
789         case PARSE_EVENTS__TERM_TYPE_CONFIG1:
790         case PARSE_EVENTS__TERM_TYPE_CONFIG2:
791         case PARSE_EVENTS__TERM_TYPE_CONFIG3:
792         case PARSE_EVENTS__TERM_TYPE_NAME:
793         case PARSE_EVENTS__TERM_TYPE_METRIC_ID:
794         case PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD:
795         case PARSE_EVENTS__TERM_TYPE_PERCORE:
796                 return true;
797         default:
798                 if (!err)
799                         return false;
800
801                 /* term_type is validated so indexing is safe */
802                 if (asprintf(&err_str, "'%s' is not usable in 'perf stat'",
803                                 config_term_names[term_type]) >= 0)
804                         parse_events_error__handle(err, -1, err_str, NULL);
805                 return false;
806         }
807 }
808
809 void parse_events__shrink_config_terms(void)
810 {
811         config_term_shrinked = true;
812 }
813
814 static int config_term_common(struct perf_event_attr *attr,
815                               struct parse_events_term *term,
816                               struct parse_events_error *err)
817 {
818 #define CHECK_TYPE_VAL(type)                                               \
819 do {                                                                       \
820         if (check_type_val(term, err, PARSE_EVENTS__TERM_TYPE_ ## type)) \
821                 return -EINVAL;                                            \
822 } while (0)
823
824         switch (term->type_term) {
825         case PARSE_EVENTS__TERM_TYPE_CONFIG:
826                 CHECK_TYPE_VAL(NUM);
827                 attr->config = term->val.num;
828                 break;
829         case PARSE_EVENTS__TERM_TYPE_CONFIG1:
830                 CHECK_TYPE_VAL(NUM);
831                 attr->config1 = term->val.num;
832                 break;
833         case PARSE_EVENTS__TERM_TYPE_CONFIG2:
834                 CHECK_TYPE_VAL(NUM);
835                 attr->config2 = term->val.num;
836                 break;
837         case PARSE_EVENTS__TERM_TYPE_CONFIG3:
838                 CHECK_TYPE_VAL(NUM);
839                 attr->config3 = term->val.num;
840                 break;
841         case PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD:
842                 CHECK_TYPE_VAL(NUM);
843                 break;
844         case PARSE_EVENTS__TERM_TYPE_SAMPLE_FREQ:
845                 CHECK_TYPE_VAL(NUM);
846                 break;
847         case PARSE_EVENTS__TERM_TYPE_BRANCH_SAMPLE_TYPE:
848                 CHECK_TYPE_VAL(STR);
849                 if (strcmp(term->val.str, "no") &&
850                     parse_branch_str(term->val.str,
851                                     &attr->branch_sample_type)) {
852                         parse_events_error__handle(err, term->err_val,
853                                         strdup("invalid branch sample type"),
854                                         NULL);
855                         return -EINVAL;
856                 }
857                 break;
858         case PARSE_EVENTS__TERM_TYPE_TIME:
859                 CHECK_TYPE_VAL(NUM);
860                 if (term->val.num > 1) {
861                         parse_events_error__handle(err, term->err_val,
862                                                 strdup("expected 0 or 1"),
863                                                 NULL);
864                         return -EINVAL;
865                 }
866                 break;
867         case PARSE_EVENTS__TERM_TYPE_CALLGRAPH:
868                 CHECK_TYPE_VAL(STR);
869                 break;
870         case PARSE_EVENTS__TERM_TYPE_STACKSIZE:
871                 CHECK_TYPE_VAL(NUM);
872                 break;
873         case PARSE_EVENTS__TERM_TYPE_INHERIT:
874                 CHECK_TYPE_VAL(NUM);
875                 break;
876         case PARSE_EVENTS__TERM_TYPE_NOINHERIT:
877                 CHECK_TYPE_VAL(NUM);
878                 break;
879         case PARSE_EVENTS__TERM_TYPE_OVERWRITE:
880                 CHECK_TYPE_VAL(NUM);
881                 break;
882         case PARSE_EVENTS__TERM_TYPE_NOOVERWRITE:
883                 CHECK_TYPE_VAL(NUM);
884                 break;
885         case PARSE_EVENTS__TERM_TYPE_NAME:
886                 CHECK_TYPE_VAL(STR);
887                 break;
888         case PARSE_EVENTS__TERM_TYPE_METRIC_ID:
889                 CHECK_TYPE_VAL(STR);
890                 break;
891         case PARSE_EVENTS__TERM_TYPE_RAW:
892                 CHECK_TYPE_VAL(STR);
893                 break;
894         case PARSE_EVENTS__TERM_TYPE_MAX_STACK:
895                 CHECK_TYPE_VAL(NUM);
896                 break;
897         case PARSE_EVENTS__TERM_TYPE_MAX_EVENTS:
898                 CHECK_TYPE_VAL(NUM);
899                 break;
900         case PARSE_EVENTS__TERM_TYPE_PERCORE:
901                 CHECK_TYPE_VAL(NUM);
902                 if ((unsigned int)term->val.num > 1) {
903                         parse_events_error__handle(err, term->err_val,
904                                                 strdup("expected 0 or 1"),
905                                                 NULL);
906                         return -EINVAL;
907                 }
908                 break;
909         case PARSE_EVENTS__TERM_TYPE_AUX_OUTPUT:
910                 CHECK_TYPE_VAL(NUM);
911                 break;
912         case PARSE_EVENTS__TERM_TYPE_AUX_SAMPLE_SIZE:
913                 CHECK_TYPE_VAL(NUM);
914                 if (term->val.num > UINT_MAX) {
915                         parse_events_error__handle(err, term->err_val,
916                                                 strdup("too big"),
917                                                 NULL);
918                         return -EINVAL;
919                 }
920                 break;
921         default:
922                 parse_events_error__handle(err, term->err_term,
923                                 strdup("unknown term"),
924                                 parse_events_formats_error_string(NULL));
925                 return -EINVAL;
926         }
927
928         /*
929          * Check term availability after basic checking so
930          * PARSE_EVENTS__TERM_TYPE_USER can be found and filtered.
931          *
932          * If check availability at the entry of this function,
933          * user will see "'<sysfs term>' is not usable in 'perf stat'"
934          * if an invalid config term is provided for legacy events
935          * (for example, instructions/badterm/...), which is confusing.
936          */
937         if (!config_term_avail(term->type_term, err))
938                 return -EINVAL;
939         return 0;
940 #undef CHECK_TYPE_VAL
941 }
942
943 static int config_term_pmu(struct perf_event_attr *attr,
944                            struct parse_events_term *term,
945                            struct parse_events_error *err)
946 {
947         if (term->type_term == PARSE_EVENTS__TERM_TYPE_LEGACY_CACHE) {
948                 const struct perf_pmu *pmu = perf_pmus__find_by_type(attr->type);
949
950                 if (!pmu) {
951                         char *err_str;
952
953                         if (asprintf(&err_str, "Failed to find PMU for type %d", attr->type) >= 0)
954                                 parse_events_error__handle(err, term->err_term,
955                                                            err_str, /*help=*/NULL);
956                         return -EINVAL;
957                 }
958                 if (perf_pmu__supports_legacy_cache(pmu)) {
959                         attr->type = PERF_TYPE_HW_CACHE;
960                         return parse_events__decode_legacy_cache(term->config, pmu->type,
961                                                                  &attr->config);
962                 } else
963                         term->type_term = PARSE_EVENTS__TERM_TYPE_USER;
964         }
965         if (term->type_term == PARSE_EVENTS__TERM_TYPE_HARDWARE) {
966                 const struct perf_pmu *pmu = perf_pmus__find_by_type(attr->type);
967
968                 if (!pmu) {
969                         char *err_str;
970
971                         if (asprintf(&err_str, "Failed to find PMU for type %d", attr->type) >= 0)
972                                 parse_events_error__handle(err, term->err_term,
973                                                            err_str, /*help=*/NULL);
974                         return -EINVAL;
975                 }
976                 attr->type = PERF_TYPE_HARDWARE;
977                 attr->config = term->val.num;
978                 if (perf_pmus__supports_extended_type())
979                         attr->config |= (__u64)pmu->type << PERF_PMU_TYPE_SHIFT;
980                 return 0;
981         }
982         if (term->type_term == PARSE_EVENTS__TERM_TYPE_USER ||
983             term->type_term == PARSE_EVENTS__TERM_TYPE_DRV_CFG) {
984                 /*
985                  * Always succeed for sysfs terms, as we dont know
986                  * at this point what type they need to have.
987                  */
988                 return 0;
989         }
990         return config_term_common(attr, term, err);
991 }
992
993 #ifdef HAVE_LIBTRACEEVENT
994 static int config_term_tracepoint(struct perf_event_attr *attr,
995                                   struct parse_events_term *term,
996                                   struct parse_events_error *err)
997 {
998         switch (term->type_term) {
999         case PARSE_EVENTS__TERM_TYPE_CALLGRAPH:
1000         case PARSE_EVENTS__TERM_TYPE_STACKSIZE:
1001         case PARSE_EVENTS__TERM_TYPE_INHERIT:
1002         case PARSE_EVENTS__TERM_TYPE_NOINHERIT:
1003         case PARSE_EVENTS__TERM_TYPE_MAX_STACK:
1004         case PARSE_EVENTS__TERM_TYPE_MAX_EVENTS:
1005         case PARSE_EVENTS__TERM_TYPE_OVERWRITE:
1006         case PARSE_EVENTS__TERM_TYPE_NOOVERWRITE:
1007         case PARSE_EVENTS__TERM_TYPE_AUX_OUTPUT:
1008         case PARSE_EVENTS__TERM_TYPE_AUX_SAMPLE_SIZE:
1009                 return config_term_common(attr, term, err);
1010         default:
1011                 if (err) {
1012                         parse_events_error__handle(err, term->err_term,
1013                                 strdup("unknown term"),
1014                                 strdup("valid terms: call-graph,stack-size\n"));
1015                 }
1016                 return -EINVAL;
1017         }
1018
1019         return 0;
1020 }
1021 #endif
1022
1023 static int config_attr(struct perf_event_attr *attr,
1024                        struct list_head *head,
1025                        struct parse_events_error *err,
1026                        config_term_func_t config_term)
1027 {
1028         struct parse_events_term *term;
1029
1030         list_for_each_entry(term, head, list)
1031                 if (config_term(attr, term, err))
1032                         return -EINVAL;
1033
1034         return 0;
1035 }
1036
1037 static int get_config_terms(struct list_head *head_config,
1038                             struct list_head *head_terms __maybe_unused)
1039 {
1040 #define ADD_CONFIG_TERM(__type, __weak)                         \
1041         struct evsel_config_term *__t;                  \
1042                                                                 \
1043         __t = zalloc(sizeof(*__t));                             \
1044         if (!__t)                                               \
1045                 return -ENOMEM;                                 \
1046                                                                 \
1047         INIT_LIST_HEAD(&__t->list);                             \
1048         __t->type       = EVSEL__CONFIG_TERM_ ## __type;        \
1049         __t->weak       = __weak;                               \
1050         list_add_tail(&__t->list, head_terms)
1051
1052 #define ADD_CONFIG_TERM_VAL(__type, __name, __val, __weak)      \
1053 do {                                                            \
1054         ADD_CONFIG_TERM(__type, __weak);                        \
1055         __t->val.__name = __val;                                \
1056 } while (0)
1057
1058 #define ADD_CONFIG_TERM_STR(__type, __val, __weak)              \
1059 do {                                                            \
1060         ADD_CONFIG_TERM(__type, __weak);                        \
1061         __t->val.str = strdup(__val);                           \
1062         if (!__t->val.str) {                                    \
1063                 zfree(&__t);                                    \
1064                 return -ENOMEM;                                 \
1065         }                                                       \
1066         __t->free_str = true;                                   \
1067 } while (0)
1068
1069         struct parse_events_term *term;
1070
1071         list_for_each_entry(term, head_config, list) {
1072                 switch (term->type_term) {
1073                 case PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD:
1074                         ADD_CONFIG_TERM_VAL(PERIOD, period, term->val.num, term->weak);
1075                         break;
1076                 case PARSE_EVENTS__TERM_TYPE_SAMPLE_FREQ:
1077                         ADD_CONFIG_TERM_VAL(FREQ, freq, term->val.num, term->weak);
1078                         break;
1079                 case PARSE_EVENTS__TERM_TYPE_TIME:
1080                         ADD_CONFIG_TERM_VAL(TIME, time, term->val.num, term->weak);
1081                         break;
1082                 case PARSE_EVENTS__TERM_TYPE_CALLGRAPH:
1083                         ADD_CONFIG_TERM_STR(CALLGRAPH, term->val.str, term->weak);
1084                         break;
1085                 case PARSE_EVENTS__TERM_TYPE_BRANCH_SAMPLE_TYPE:
1086                         ADD_CONFIG_TERM_STR(BRANCH, term->val.str, term->weak);
1087                         break;
1088                 case PARSE_EVENTS__TERM_TYPE_STACKSIZE:
1089                         ADD_CONFIG_TERM_VAL(STACK_USER, stack_user,
1090                                             term->val.num, term->weak);
1091                         break;
1092                 case PARSE_EVENTS__TERM_TYPE_INHERIT:
1093                         ADD_CONFIG_TERM_VAL(INHERIT, inherit,
1094                                             term->val.num ? 1 : 0, term->weak);
1095                         break;
1096                 case PARSE_EVENTS__TERM_TYPE_NOINHERIT:
1097                         ADD_CONFIG_TERM_VAL(INHERIT, inherit,
1098                                             term->val.num ? 0 : 1, term->weak);
1099                         break;
1100                 case PARSE_EVENTS__TERM_TYPE_MAX_STACK:
1101                         ADD_CONFIG_TERM_VAL(MAX_STACK, max_stack,
1102                                             term->val.num, term->weak);
1103                         break;
1104                 case PARSE_EVENTS__TERM_TYPE_MAX_EVENTS:
1105                         ADD_CONFIG_TERM_VAL(MAX_EVENTS, max_events,
1106                                             term->val.num, term->weak);
1107                         break;
1108                 case PARSE_EVENTS__TERM_TYPE_OVERWRITE:
1109                         ADD_CONFIG_TERM_VAL(OVERWRITE, overwrite,
1110                                             term->val.num ? 1 : 0, term->weak);
1111                         break;
1112                 case PARSE_EVENTS__TERM_TYPE_NOOVERWRITE:
1113                         ADD_CONFIG_TERM_VAL(OVERWRITE, overwrite,
1114                                             term->val.num ? 0 : 1, term->weak);
1115                         break;
1116                 case PARSE_EVENTS__TERM_TYPE_DRV_CFG:
1117                         ADD_CONFIG_TERM_STR(DRV_CFG, term->val.str, term->weak);
1118                         break;
1119                 case PARSE_EVENTS__TERM_TYPE_PERCORE:
1120                         ADD_CONFIG_TERM_VAL(PERCORE, percore,
1121                                             term->val.num ? true : false, term->weak);
1122                         break;
1123                 case PARSE_EVENTS__TERM_TYPE_AUX_OUTPUT:
1124                         ADD_CONFIG_TERM_VAL(AUX_OUTPUT, aux_output,
1125                                             term->val.num ? 1 : 0, term->weak);
1126                         break;
1127                 case PARSE_EVENTS__TERM_TYPE_AUX_SAMPLE_SIZE:
1128                         ADD_CONFIG_TERM_VAL(AUX_SAMPLE_SIZE, aux_sample_size,
1129                                             term->val.num, term->weak);
1130                         break;
1131                 default:
1132                         break;
1133                 }
1134         }
1135         return 0;
1136 }
1137
1138 /*
1139  * Add EVSEL__CONFIG_TERM_CFG_CHG where cfg_chg will have a bit set for
1140  * each bit of attr->config that the user has changed.
1141  */
1142 static int get_config_chgs(struct perf_pmu *pmu, struct list_head *head_config,
1143                            struct list_head *head_terms)
1144 {
1145         struct parse_events_term *term;
1146         u64 bits = 0;
1147         int type;
1148
1149         list_for_each_entry(term, head_config, list) {
1150                 switch (term->type_term) {
1151                 case PARSE_EVENTS__TERM_TYPE_USER:
1152                         type = perf_pmu__format_type(pmu, term->config);
1153                         if (type != PERF_PMU_FORMAT_VALUE_CONFIG)
1154                                 continue;
1155                         bits |= perf_pmu__format_bits(pmu, term->config);
1156                         break;
1157                 case PARSE_EVENTS__TERM_TYPE_CONFIG:
1158                         bits = ~(u64)0;
1159                         break;
1160                 default:
1161                         break;
1162                 }
1163         }
1164
1165         if (bits)
1166                 ADD_CONFIG_TERM_VAL(CFG_CHG, cfg_chg, bits, false);
1167
1168 #undef ADD_CONFIG_TERM
1169         return 0;
1170 }
1171
1172 int parse_events_add_tracepoint(struct list_head *list, int *idx,
1173                                 const char *sys, const char *event,
1174                                 struct parse_events_error *err,
1175                                 struct list_head *head_config, void *loc_)
1176 {
1177         YYLTYPE *loc = loc_;
1178 #ifdef HAVE_LIBTRACEEVENT
1179         if (head_config) {
1180                 struct perf_event_attr attr;
1181
1182                 if (config_attr(&attr, head_config, err,
1183                                 config_term_tracepoint))
1184                         return -EINVAL;
1185         }
1186
1187         if (strpbrk(sys, "*?"))
1188                 return add_tracepoint_multi_sys(list, idx, sys, event,
1189                                                 err, head_config, loc);
1190         else
1191                 return add_tracepoint_event(list, idx, sys, event,
1192                                             err, head_config, loc);
1193 #else
1194         (void)list;
1195         (void)idx;
1196         (void)sys;
1197         (void)event;
1198         (void)head_config;
1199         parse_events_error__handle(err, loc->first_column, strdup("unsupported tracepoint"),
1200                                 strdup("libtraceevent is necessary for tracepoint support"));
1201         return -1;
1202 #endif
1203 }
1204
1205 static int __parse_events_add_numeric(struct parse_events_state *parse_state,
1206                                 struct list_head *list,
1207                                 struct perf_pmu *pmu, u32 type, u32 extended_type,
1208                                 u64 config, struct list_head *head_config)
1209 {
1210         struct perf_event_attr attr;
1211         LIST_HEAD(config_terms);
1212         const char *name, *metric_id;
1213         int ret;
1214
1215         memset(&attr, 0, sizeof(attr));
1216         attr.type = type;
1217         attr.config = config;
1218         if (extended_type && (type == PERF_TYPE_HARDWARE || type == PERF_TYPE_HW_CACHE)) {
1219                 assert(perf_pmus__supports_extended_type());
1220                 attr.config |= (u64)extended_type << PERF_PMU_TYPE_SHIFT;
1221         }
1222
1223         if (head_config) {
1224                 if (config_attr(&attr, head_config, parse_state->error,
1225                                 config_term_common))
1226                         return -EINVAL;
1227
1228                 if (get_config_terms(head_config, &config_terms))
1229                         return -ENOMEM;
1230         }
1231
1232         name = get_config_name(head_config);
1233         metric_id = get_config_metric_id(head_config);
1234         ret = __add_event(list, &parse_state->idx, &attr, /*init_attr*/true, name,
1235                         metric_id, pmu, &config_terms, /*auto_merge_stats=*/false,
1236                         /*cpu_list=*/NULL) ? 0 : -ENOMEM;
1237         free_config_terms(&config_terms);
1238         return ret;
1239 }
1240
1241 int parse_events_add_numeric(struct parse_events_state *parse_state,
1242                              struct list_head *list,
1243                              u32 type, u64 config,
1244                              struct list_head *head_config,
1245                              bool wildcard)
1246 {
1247         struct perf_pmu *pmu = NULL;
1248         bool found_supported = false;
1249
1250         /* Wildcards on numeric values are only supported by core PMUs. */
1251         if (wildcard && perf_pmus__supports_extended_type()) {
1252                 while ((pmu = perf_pmus__scan_core(pmu)) != NULL) {
1253                         int ret;
1254
1255                         found_supported = true;
1256                         if (parse_events__filter_pmu(parse_state, pmu))
1257                                 continue;
1258
1259                         ret = __parse_events_add_numeric(parse_state, list, pmu,
1260                                                          type, pmu->type,
1261                                                          config, head_config);
1262                         if (ret)
1263                                 return ret;
1264                 }
1265                 if (found_supported)
1266                         return 0;
1267         }
1268         return __parse_events_add_numeric(parse_state, list, perf_pmus__find_by_type(type),
1269                                         type, /*extended_type=*/0, config, head_config);
1270 }
1271
1272 int parse_events_add_tool(struct parse_events_state *parse_state,
1273                           struct list_head *list,
1274                           int tool_event)
1275 {
1276         return add_event_tool(list, &parse_state->idx, tool_event);
1277 }
1278
1279 static bool config_term_percore(struct list_head *config_terms)
1280 {
1281         struct evsel_config_term *term;
1282
1283         list_for_each_entry(term, config_terms, list) {
1284                 if (term->type == EVSEL__CONFIG_TERM_PERCORE)
1285                         return term->val.percore;
1286         }
1287
1288         return false;
1289 }
1290
1291 int parse_events_add_pmu(struct parse_events_state *parse_state,
1292                          struct list_head *list, const char *name,
1293                          struct list_head *head_config,
1294                          bool auto_merge_stats, void *loc_)
1295 {
1296         struct perf_event_attr attr;
1297         struct perf_pmu_info info;
1298         struct perf_pmu *pmu;
1299         struct evsel *evsel;
1300         struct parse_events_error *err = parse_state->error;
1301         YYLTYPE *loc = loc_;
1302         LIST_HEAD(config_terms);
1303
1304         pmu = parse_state->fake_pmu ?: perf_pmus__find(name);
1305
1306         if (!pmu) {
1307                 char *err_str;
1308
1309                 if (asprintf(&err_str,
1310                                 "Cannot find PMU `%s'. Missing kernel support?",
1311                                 name) >= 0)
1312                         parse_events_error__handle(err, loc->first_column, err_str, NULL);
1313                 return -EINVAL;
1314         }
1315
1316         if (verbose > 1) {
1317                 struct strbuf sb;
1318
1319                 strbuf_init(&sb, /*hint=*/ 0);
1320                 if (pmu->selectable && !head_config) {
1321                         strbuf_addf(&sb, "%s//", name);
1322                 } else {
1323                         strbuf_addf(&sb, "%s/", name);
1324                         parse_events_term__to_strbuf(head_config, &sb);
1325                         strbuf_addch(&sb, '/');
1326                 }
1327                 fprintf(stderr, "Attempt to add: %s\n", sb.buf);
1328                 strbuf_release(&sb);
1329         }
1330         if (head_config)
1331                 fix_raw(head_config, pmu);
1332
1333         if (pmu->default_config) {
1334                 memcpy(&attr, pmu->default_config,
1335                        sizeof(struct perf_event_attr));
1336         } else {
1337                 memset(&attr, 0, sizeof(attr));
1338         }
1339         attr.type = pmu->type;
1340
1341         if (!head_config) {
1342                 evsel = __add_event(list, &parse_state->idx, &attr,
1343                                     /*init_attr=*/true, /*name=*/NULL,
1344                                     /*metric_id=*/NULL, pmu,
1345                                     /*config_terms=*/NULL, auto_merge_stats,
1346                                     /*cpu_list=*/NULL);
1347                 return evsel ? 0 : -ENOMEM;
1348         }
1349
1350         if (!parse_state->fake_pmu && perf_pmu__check_alias(pmu, head_config, &info, err))
1351                 return -EINVAL;
1352
1353         if (verbose > 1) {
1354                 struct strbuf sb;
1355
1356                 strbuf_init(&sb, /*hint=*/ 0);
1357                 parse_events_term__to_strbuf(head_config, &sb);
1358                 fprintf(stderr, "..after resolving event: %s/%s/\n", name, sb.buf);
1359                 strbuf_release(&sb);
1360         }
1361
1362         /*
1363          * Configure hardcoded terms first, no need to check
1364          * return value when called with fail == 0 ;)
1365          */
1366         if (config_attr(&attr, head_config, parse_state->error, config_term_pmu))
1367                 return -EINVAL;
1368
1369         if (get_config_terms(head_config, &config_terms))
1370                 return -ENOMEM;
1371
1372         /*
1373          * When using default config, record which bits of attr->config were
1374          * changed by the user.
1375          */
1376         if (pmu->default_config && get_config_chgs(pmu, head_config, &config_terms))
1377                 return -ENOMEM;
1378
1379         if (!parse_state->fake_pmu && perf_pmu__config(pmu, &attr, head_config, parse_state->error)) {
1380                 free_config_terms(&config_terms);
1381                 return -EINVAL;
1382         }
1383
1384         evsel = __add_event(list, &parse_state->idx, &attr, /*init_attr=*/true,
1385                             get_config_name(head_config),
1386                             get_config_metric_id(head_config), pmu,
1387                             &config_terms, auto_merge_stats, /*cpu_list=*/NULL);
1388         if (!evsel)
1389                 return -ENOMEM;
1390
1391         if (evsel->name)
1392                 evsel->use_config_name = true;
1393
1394         evsel->percore = config_term_percore(&evsel->config_terms);
1395
1396         if (parse_state->fake_pmu)
1397                 return 0;
1398
1399         free((char *)evsel->unit);
1400         evsel->unit = strdup(info.unit);
1401         evsel->scale = info.scale;
1402         evsel->per_pkg = info.per_pkg;
1403         evsel->snapshot = info.snapshot;
1404         return 0;
1405 }
1406
1407 int parse_events_multi_pmu_add(struct parse_events_state *parse_state,
1408                                char *str, struct list_head *head,
1409                                struct list_head **listp, void *loc_)
1410 {
1411         struct parse_events_term *term;
1412         struct list_head *list = NULL;
1413         struct list_head *orig_head = NULL;
1414         struct perf_pmu *pmu = NULL;
1415         YYLTYPE *loc = loc_;
1416         int ok = 0;
1417         const char *config;
1418
1419         *listp = NULL;
1420
1421         if (!head) {
1422                 head = malloc(sizeof(struct list_head));
1423                 if (!head)
1424                         goto out_err;
1425
1426                 INIT_LIST_HEAD(head);
1427         }
1428         config = strdup(str);
1429         if (!config)
1430                 goto out_err;
1431
1432         if (parse_events_term__num(&term,
1433                                    PARSE_EVENTS__TERM_TYPE_USER,
1434                                    config, 1, false, NULL,
1435                                         NULL) < 0) {
1436                 zfree(&config);
1437                 goto out_err;
1438         }
1439         list_add_tail(&term->list, head);
1440
1441         /* Add it for all PMUs that support the alias */
1442         list = malloc(sizeof(struct list_head));
1443         if (!list)
1444                 goto out_err;
1445
1446         INIT_LIST_HEAD(list);
1447
1448         while ((pmu = perf_pmus__scan(pmu)) != NULL) {
1449                 bool auto_merge_stats;
1450
1451                 if (parse_events__filter_pmu(parse_state, pmu))
1452                         continue;
1453
1454                 if (!perf_pmu__have_event(pmu, str))
1455                         continue;
1456
1457                 auto_merge_stats = perf_pmu__auto_merge_stats(pmu);
1458                 parse_events_copy_term_list(head, &orig_head);
1459                 if (!parse_events_add_pmu(parse_state, list, pmu->name,
1460                                           orig_head, auto_merge_stats, loc)) {
1461                         struct strbuf sb;
1462
1463                         strbuf_init(&sb, /*hint=*/ 0);
1464                         parse_events_term__to_strbuf(orig_head, &sb);
1465                         pr_debug("%s -> %s/%s/\n", str, pmu->name, sb.buf);
1466                         strbuf_release(&sb);
1467                         ok++;
1468                 }
1469                 parse_events_terms__delete(orig_head);
1470         }
1471
1472         if (parse_state->fake_pmu) {
1473                 if (!parse_events_add_pmu(parse_state, list, str, head,
1474                                           /*auto_merge_stats=*/true, loc)) {
1475                         struct strbuf sb;
1476
1477                         strbuf_init(&sb, /*hint=*/ 0);
1478                         parse_events_term__to_strbuf(head, &sb);
1479                         pr_debug("%s -> %s/%s/\n", str, "fake_pmu", sb.buf);
1480                         strbuf_release(&sb);
1481                         ok++;
1482                 }
1483         }
1484
1485 out_err:
1486         if (ok)
1487                 *listp = list;
1488         else
1489                 free(list);
1490
1491         parse_events_terms__delete(head);
1492         return ok ? 0 : -1;
1493 }
1494
1495 int parse_events__modifier_group(struct list_head *list,
1496                                  char *event_mod)
1497 {
1498         return parse_events__modifier_event(list, event_mod, true);
1499 }
1500
1501 void parse_events__set_leader(char *name, struct list_head *list)
1502 {
1503         struct evsel *leader;
1504
1505         if (list_empty(list)) {
1506                 WARN_ONCE(true, "WARNING: failed to set leader: empty list");
1507                 return;
1508         }
1509
1510         leader = list_first_entry(list, struct evsel, core.node);
1511         __perf_evlist__set_leader(list, &leader->core);
1512         leader->group_name = name;
1513 }
1514
1515 /* list_event is assumed to point to malloc'ed memory */
1516 void parse_events_update_lists(struct list_head *list_event,
1517                                struct list_head *list_all)
1518 {
1519         /*
1520          * Called for single event definition. Update the
1521          * 'all event' list, and reinit the 'single event'
1522          * list, for next event definition.
1523          */
1524         list_splice_tail(list_event, list_all);
1525         free(list_event);
1526 }
1527
1528 struct event_modifier {
1529         int eu;
1530         int ek;
1531         int eh;
1532         int eH;
1533         int eG;
1534         int eI;
1535         int precise;
1536         int precise_max;
1537         int exclude_GH;
1538         int sample_read;
1539         int pinned;
1540         int weak;
1541         int exclusive;
1542         int bpf_counter;
1543 };
1544
1545 static int get_event_modifier(struct event_modifier *mod, char *str,
1546                                struct evsel *evsel)
1547 {
1548         int eu = evsel ? evsel->core.attr.exclude_user : 0;
1549         int ek = evsel ? evsel->core.attr.exclude_kernel : 0;
1550         int eh = evsel ? evsel->core.attr.exclude_hv : 0;
1551         int eH = evsel ? evsel->core.attr.exclude_host : 0;
1552         int eG = evsel ? evsel->core.attr.exclude_guest : 0;
1553         int eI = evsel ? evsel->core.attr.exclude_idle : 0;
1554         int precise = evsel ? evsel->core.attr.precise_ip : 0;
1555         int precise_max = 0;
1556         int sample_read = 0;
1557         int pinned = evsel ? evsel->core.attr.pinned : 0;
1558         int exclusive = evsel ? evsel->core.attr.exclusive : 0;
1559
1560         int exclude = eu | ek | eh;
1561         int exclude_GH = evsel ? evsel->exclude_GH : 0;
1562         int weak = 0;
1563         int bpf_counter = 0;
1564
1565         memset(mod, 0, sizeof(*mod));
1566
1567         while (*str) {
1568                 if (*str == 'u') {
1569                         if (!exclude)
1570                                 exclude = eu = ek = eh = 1;
1571                         if (!exclude_GH && !perf_guest)
1572                                 eG = 1;
1573                         eu = 0;
1574                 } else if (*str == 'k') {
1575                         if (!exclude)
1576                                 exclude = eu = ek = eh = 1;
1577                         ek = 0;
1578                 } else if (*str == 'h') {
1579                         if (!exclude)
1580                                 exclude = eu = ek = eh = 1;
1581                         eh = 0;
1582                 } else if (*str == 'G') {
1583                         if (!exclude_GH)
1584                                 exclude_GH = eG = eH = 1;
1585                         eG = 0;
1586                 } else if (*str == 'H') {
1587                         if (!exclude_GH)
1588                                 exclude_GH = eG = eH = 1;
1589                         eH = 0;
1590                 } else if (*str == 'I') {
1591                         eI = 1;
1592                 } else if (*str == 'p') {
1593                         precise++;
1594                         /* use of precise requires exclude_guest */
1595                         if (!exclude_GH)
1596                                 eG = 1;
1597                 } else if (*str == 'P') {
1598                         precise_max = 1;
1599                 } else if (*str == 'S') {
1600                         sample_read = 1;
1601                 } else if (*str == 'D') {
1602                         pinned = 1;
1603                 } else if (*str == 'e') {
1604                         exclusive = 1;
1605                 } else if (*str == 'W') {
1606                         weak = 1;
1607                 } else if (*str == 'b') {
1608                         bpf_counter = 1;
1609                 } else
1610                         break;
1611
1612                 ++str;
1613         }
1614
1615         /*
1616          * precise ip:
1617          *
1618          *  0 - SAMPLE_IP can have arbitrary skid
1619          *  1 - SAMPLE_IP must have constant skid
1620          *  2 - SAMPLE_IP requested to have 0 skid
1621          *  3 - SAMPLE_IP must have 0 skid
1622          *
1623          *  See also PERF_RECORD_MISC_EXACT_IP
1624          */
1625         if (precise > 3)
1626                 return -EINVAL;
1627
1628         mod->eu = eu;
1629         mod->ek = ek;
1630         mod->eh = eh;
1631         mod->eH = eH;
1632         mod->eG = eG;
1633         mod->eI = eI;
1634         mod->precise = precise;
1635         mod->precise_max = precise_max;
1636         mod->exclude_GH = exclude_GH;
1637         mod->sample_read = sample_read;
1638         mod->pinned = pinned;
1639         mod->weak = weak;
1640         mod->bpf_counter = bpf_counter;
1641         mod->exclusive = exclusive;
1642
1643         return 0;
1644 }
1645
1646 /*
1647  * Basic modifier sanity check to validate it contains only one
1648  * instance of any modifier (apart from 'p') present.
1649  */
1650 static int check_modifier(char *str)
1651 {
1652         char *p = str;
1653
1654         /* The sizeof includes 0 byte as well. */
1655         if (strlen(str) > (sizeof("ukhGHpppPSDIWeb") - 1))
1656                 return -1;
1657
1658         while (*p) {
1659                 if (*p != 'p' && strchr(p + 1, *p))
1660                         return -1;
1661                 p++;
1662         }
1663
1664         return 0;
1665 }
1666
1667 int parse_events__modifier_event(struct list_head *list, char *str, bool add)
1668 {
1669         struct evsel *evsel;
1670         struct event_modifier mod;
1671
1672         if (str == NULL)
1673                 return 0;
1674
1675         if (check_modifier(str))
1676                 return -EINVAL;
1677
1678         if (!add && get_event_modifier(&mod, str, NULL))
1679                 return -EINVAL;
1680
1681         __evlist__for_each_entry(list, evsel) {
1682                 if (add && get_event_modifier(&mod, str, evsel))
1683                         return -EINVAL;
1684
1685                 evsel->core.attr.exclude_user   = mod.eu;
1686                 evsel->core.attr.exclude_kernel = mod.ek;
1687                 evsel->core.attr.exclude_hv     = mod.eh;
1688                 evsel->core.attr.precise_ip     = mod.precise;
1689                 evsel->core.attr.exclude_host   = mod.eH;
1690                 evsel->core.attr.exclude_guest  = mod.eG;
1691                 evsel->core.attr.exclude_idle   = mod.eI;
1692                 evsel->exclude_GH          = mod.exclude_GH;
1693                 evsel->sample_read         = mod.sample_read;
1694                 evsel->precise_max         = mod.precise_max;
1695                 evsel->weak_group          = mod.weak;
1696                 evsel->bpf_counter         = mod.bpf_counter;
1697
1698                 if (evsel__is_group_leader(evsel)) {
1699                         evsel->core.attr.pinned = mod.pinned;
1700                         evsel->core.attr.exclusive = mod.exclusive;
1701                 }
1702         }
1703
1704         return 0;
1705 }
1706
1707 int parse_events_name(struct list_head *list, const char *name)
1708 {
1709         struct evsel *evsel;
1710
1711         __evlist__for_each_entry(list, evsel) {
1712                 if (!evsel->name) {
1713                         evsel->name = strdup(name);
1714                         if (!evsel->name)
1715                                 return -ENOMEM;
1716                 }
1717         }
1718
1719         return 0;
1720 }
1721
1722 static int parse_events__scanner(const char *str,
1723                                  FILE *input,
1724                                  struct parse_events_state *parse_state)
1725 {
1726         YY_BUFFER_STATE buffer;
1727         void *scanner;
1728         int ret;
1729
1730         ret = parse_events_lex_init_extra(parse_state, &scanner);
1731         if (ret)
1732                 return ret;
1733
1734         if (str)
1735                 buffer = parse_events__scan_string(str, scanner);
1736         else
1737                 parse_events_set_in(input, scanner);
1738
1739 #ifdef PARSER_DEBUG
1740         parse_events_debug = 1;
1741         parse_events_set_debug(1, scanner);
1742 #endif
1743         ret = parse_events_parse(parse_state, scanner);
1744
1745         if (str) {
1746                 parse_events__flush_buffer(buffer, scanner);
1747                 parse_events__delete_buffer(buffer, scanner);
1748         }
1749         parse_events_lex_destroy(scanner);
1750         return ret;
1751 }
1752
1753 /*
1754  * parse event config string, return a list of event terms.
1755  */
1756 int parse_events_terms(struct list_head *terms, const char *str, FILE *input)
1757 {
1758         struct parse_events_state parse_state = {
1759                 .terms  = NULL,
1760                 .stoken = PE_START_TERMS,
1761         };
1762         int ret;
1763
1764         ret = parse_events__scanner(str, input, &parse_state);
1765
1766         if (!ret) {
1767                 list_splice(parse_state.terms, terms);
1768                 zfree(&parse_state.terms);
1769                 return 0;
1770         }
1771
1772         parse_events_terms__delete(parse_state.terms);
1773         return ret;
1774 }
1775
1776 static int evsel__compute_group_pmu_name(struct evsel *evsel,
1777                                           const struct list_head *head)
1778 {
1779         struct evsel *leader = evsel__leader(evsel);
1780         struct evsel *pos;
1781         const char *group_pmu_name;
1782         struct perf_pmu *pmu = evsel__find_pmu(evsel);
1783
1784         if (!pmu) {
1785                 /*
1786                  * For PERF_TYPE_HARDWARE and PERF_TYPE_HW_CACHE types the PMU
1787                  * is a core PMU, but in heterogeneous systems this is
1788                  * unknown. For now pick the first core PMU.
1789                  */
1790                 pmu = perf_pmus__scan_core(NULL);
1791         }
1792         if (!pmu) {
1793                 pr_debug("No PMU found for '%s'\n", evsel__name(evsel));
1794                 return -EINVAL;
1795         }
1796         group_pmu_name = pmu->name;
1797         /*
1798          * Software events may be in a group with other uncore PMU events. Use
1799          * the pmu_name of the first non-software event to avoid breaking the
1800          * software event out of the group.
1801          *
1802          * Aux event leaders, like intel_pt, expect a group with events from
1803          * other PMUs, so substitute the AUX event's PMU in this case.
1804          */
1805         if (perf_pmu__is_software(pmu) || evsel__is_aux_event(leader)) {
1806                 struct perf_pmu *leader_pmu = evsel__find_pmu(leader);
1807
1808                 if (!leader_pmu) {
1809                         /* As with determining pmu above. */
1810                         leader_pmu = perf_pmus__scan_core(NULL);
1811                 }
1812                 /*
1813                  * Starting with the leader, find the first event with a named
1814                  * non-software PMU. for_each_group_(member|evsel) isn't used as
1815                  * the list isn't yet sorted putting evsel's in the same group
1816                  * together.
1817                  */
1818                 if (leader_pmu && !perf_pmu__is_software(leader_pmu)) {
1819                         group_pmu_name = leader_pmu->name;
1820                 } else if (leader->core.nr_members > 1) {
1821                         list_for_each_entry(pos, head, core.node) {
1822                                 struct perf_pmu *pos_pmu;
1823
1824                                 if (pos == leader || evsel__leader(pos) != leader)
1825                                         continue;
1826                                 pos_pmu = evsel__find_pmu(pos);
1827                                 if (!pos_pmu) {
1828                                         /* As with determining pmu above. */
1829                                         pos_pmu = perf_pmus__scan_core(NULL);
1830                                 }
1831                                 if (pos_pmu && !perf_pmu__is_software(pos_pmu)) {
1832                                         group_pmu_name = pos_pmu->name;
1833                                         break;
1834                                 }
1835                         }
1836                 }
1837         }
1838         /* Assign the actual name taking care that the fake PMU lacks a name. */
1839         evsel->group_pmu_name = strdup(group_pmu_name ?: "fake");
1840         return evsel->group_pmu_name ? 0 : -ENOMEM;
1841 }
1842
1843 __weak int arch_evlist__cmp(const struct evsel *lhs, const struct evsel *rhs)
1844 {
1845         /* Order by insertion index. */
1846         return lhs->core.idx - rhs->core.idx;
1847 }
1848
1849 static int evlist__cmp(void *_fg_idx, const struct list_head *l, const struct list_head *r)
1850 {
1851         const struct perf_evsel *lhs_core = container_of(l, struct perf_evsel, node);
1852         const struct evsel *lhs = container_of(lhs_core, struct evsel, core);
1853         const struct perf_evsel *rhs_core = container_of(r, struct perf_evsel, node);
1854         const struct evsel *rhs = container_of(rhs_core, struct evsel, core);
1855         int *force_grouped_idx = _fg_idx;
1856         int lhs_sort_idx, rhs_sort_idx, ret;
1857         const char *lhs_pmu_name, *rhs_pmu_name;
1858         bool lhs_has_group, rhs_has_group;
1859
1860         /*
1861          * First sort by grouping/leader. Read the leader idx only if the evsel
1862          * is part of a group, by default ungrouped events will be sorted
1863          * relative to grouped events based on where the first ungrouped event
1864          * occurs. If both events don't have a group we want to fall-through to
1865          * the arch specific sorting, that can reorder and fix things like
1866          * Intel's topdown events.
1867          */
1868         if (lhs_core->leader != lhs_core || lhs_core->nr_members > 1) {
1869                 lhs_has_group = true;
1870                 lhs_sort_idx = lhs_core->leader->idx;
1871         } else {
1872                 lhs_has_group = false;
1873                 lhs_sort_idx = *force_grouped_idx != -1 && arch_evsel__must_be_in_group(lhs)
1874                         ? *force_grouped_idx
1875                         : lhs_core->idx;
1876         }
1877         if (rhs_core->leader != rhs_core || rhs_core->nr_members > 1) {
1878                 rhs_has_group = true;
1879                 rhs_sort_idx = rhs_core->leader->idx;
1880         } else {
1881                 rhs_has_group = false;
1882                 rhs_sort_idx = *force_grouped_idx != -1 && arch_evsel__must_be_in_group(rhs)
1883                         ? *force_grouped_idx
1884                         : rhs_core->idx;
1885         }
1886
1887         if (lhs_sort_idx != rhs_sort_idx)
1888                 return lhs_sort_idx - rhs_sort_idx;
1889
1890         /* Group by PMU if there is a group. Groups can't span PMUs. */
1891         if (lhs_has_group && rhs_has_group) {
1892                 lhs_pmu_name = lhs->group_pmu_name;
1893                 rhs_pmu_name = rhs->group_pmu_name;
1894                 ret = strcmp(lhs_pmu_name, rhs_pmu_name);
1895                 if (ret)
1896                         return ret;
1897         }
1898
1899         /* Architecture specific sorting. */
1900         return arch_evlist__cmp(lhs, rhs);
1901 }
1902
1903 static int parse_events__sort_events_and_fix_groups(struct list_head *list)
1904 {
1905         int idx = 0, force_grouped_idx = -1;
1906         struct evsel *pos, *cur_leader = NULL;
1907         struct perf_evsel *cur_leaders_grp = NULL;
1908         bool idx_changed = false, cur_leader_force_grouped = false;
1909         int orig_num_leaders = 0, num_leaders = 0;
1910         int ret;
1911
1912         /*
1913          * Compute index to insert ungrouped events at. Place them where the
1914          * first ungrouped event appears.
1915          */
1916         list_for_each_entry(pos, list, core.node) {
1917                 const struct evsel *pos_leader = evsel__leader(pos);
1918
1919                 ret = evsel__compute_group_pmu_name(pos, list);
1920                 if (ret)
1921                         return ret;
1922
1923                 if (pos == pos_leader)
1924                         orig_num_leaders++;
1925
1926                 /*
1927                  * Ensure indexes are sequential, in particular for multiple
1928                  * event lists being merged. The indexes are used to detect when
1929                  * the user order is modified.
1930                  */
1931                 pos->core.idx = idx++;
1932
1933                 /* Remember an index to sort all forced grouped events together to. */
1934                 if (force_grouped_idx == -1 && pos == pos_leader && pos->core.nr_members < 2 &&
1935                     arch_evsel__must_be_in_group(pos))
1936                         force_grouped_idx = pos->core.idx;
1937         }
1938
1939         /* Sort events. */
1940         list_sort(&force_grouped_idx, list, evlist__cmp);
1941
1942         /*
1943          * Recompute groups, splitting for PMUs and adding groups for events
1944          * that require them.
1945          */
1946         idx = 0;
1947         list_for_each_entry(pos, list, core.node) {
1948                 const struct evsel *pos_leader = evsel__leader(pos);
1949                 const char *pos_pmu_name = pos->group_pmu_name;
1950                 const char *cur_leader_pmu_name;
1951                 bool pos_force_grouped = force_grouped_idx != -1 &&
1952                         arch_evsel__must_be_in_group(pos);
1953
1954                 /* Reset index and nr_members. */
1955                 if (pos->core.idx != idx)
1956                         idx_changed = true;
1957                 pos->core.idx = idx++;
1958                 pos->core.nr_members = 0;
1959
1960                 /*
1961                  * Set the group leader respecting the given groupings and that
1962                  * groups can't span PMUs.
1963                  */
1964                 if (!cur_leader)
1965                         cur_leader = pos;
1966
1967                 cur_leader_pmu_name = cur_leader->group_pmu_name;
1968                 if ((cur_leaders_grp != pos->core.leader &&
1969                      (!pos_force_grouped || !cur_leader_force_grouped)) ||
1970                     strcmp(cur_leader_pmu_name, pos_pmu_name)) {
1971                         /* Event is for a different group/PMU than last. */
1972                         cur_leader = pos;
1973                         /*
1974                          * Remember the leader's group before it is overwritten,
1975                          * so that later events match as being in the same
1976                          * group.
1977                          */
1978                         cur_leaders_grp = pos->core.leader;
1979                         /*
1980                          * Avoid forcing events into groups with events that
1981                          * don't need to be in the group.
1982                          */
1983                         cur_leader_force_grouped = pos_force_grouped;
1984                 }
1985                 if (pos_leader != cur_leader) {
1986                         /* The leader changed so update it. */
1987                         evsel__set_leader(pos, cur_leader);
1988                 }
1989         }
1990         list_for_each_entry(pos, list, core.node) {
1991                 struct evsel *pos_leader = evsel__leader(pos);
1992
1993                 if (pos == pos_leader)
1994                         num_leaders++;
1995                 pos_leader->core.nr_members++;
1996         }
1997         return (idx_changed || num_leaders != orig_num_leaders) ? 1 : 0;
1998 }
1999
2000 int __parse_events(struct evlist *evlist, const char *str, const char *pmu_filter,
2001                    struct parse_events_error *err, struct perf_pmu *fake_pmu,
2002                    bool warn_if_reordered)
2003 {
2004         struct parse_events_state parse_state = {
2005                 .list     = LIST_HEAD_INIT(parse_state.list),
2006                 .idx      = evlist->core.nr_entries,
2007                 .error    = err,
2008                 .stoken   = PE_START_EVENTS,
2009                 .fake_pmu = fake_pmu,
2010                 .pmu_filter = pmu_filter,
2011                 .match_legacy_cache_terms = true,
2012         };
2013         int ret, ret2;
2014
2015         ret = parse_events__scanner(str, /*input=*/ NULL, &parse_state);
2016
2017         if (!ret && list_empty(&parse_state.list)) {
2018                 WARN_ONCE(true, "WARNING: event parser found nothing\n");
2019                 return -1;
2020         }
2021
2022         ret2 = parse_events__sort_events_and_fix_groups(&parse_state.list);
2023         if (ret2 < 0)
2024                 return ret;
2025
2026         if (ret2 && warn_if_reordered && !parse_state.wild_card_pmus)
2027                 pr_warning("WARNING: events were regrouped to match PMUs\n");
2028
2029         /*
2030          * Add list to the evlist even with errors to allow callers to clean up.
2031          */
2032         evlist__splice_list_tail(evlist, &parse_state.list);
2033
2034         if (!ret) {
2035                 struct evsel *last;
2036
2037                 last = evlist__last(evlist);
2038                 last->cmdline_group_boundary = true;
2039
2040                 return 0;
2041         }
2042
2043         /*
2044          * There are 2 users - builtin-record and builtin-test objects.
2045          * Both call evlist__delete in case of error, so we dont
2046          * need to bother.
2047          */
2048         return ret;
2049 }
2050
2051 int parse_event(struct evlist *evlist, const char *str)
2052 {
2053         struct parse_events_error err;
2054         int ret;
2055
2056         parse_events_error__init(&err);
2057         ret = parse_events(evlist, str, &err);
2058         parse_events_error__exit(&err);
2059         return ret;
2060 }
2061
2062 void parse_events_error__init(struct parse_events_error *err)
2063 {
2064         bzero(err, sizeof(*err));
2065 }
2066
2067 void parse_events_error__exit(struct parse_events_error *err)
2068 {
2069         zfree(&err->str);
2070         zfree(&err->help);
2071         zfree(&err->first_str);
2072         zfree(&err->first_help);
2073 }
2074
2075 void parse_events_error__handle(struct parse_events_error *err, int idx,
2076                                 char *str, char *help)
2077 {
2078         if (WARN(!str || !err, "WARNING: failed to provide error string or struct\n"))
2079                 goto out_free;
2080         switch (err->num_errors) {
2081         case 0:
2082                 err->idx = idx;
2083                 err->str = str;
2084                 err->help = help;
2085                 break;
2086         case 1:
2087                 err->first_idx = err->idx;
2088                 err->idx = idx;
2089                 err->first_str = err->str;
2090                 err->str = str;
2091                 err->first_help = err->help;
2092                 err->help = help;
2093                 break;
2094         default:
2095                 pr_debug("Multiple errors dropping message: %s (%s)\n",
2096                         err->str, err->help ?: "<no help>");
2097                 free(err->str);
2098                 err->str = str;
2099                 free(err->help);
2100                 err->help = help;
2101                 break;
2102         }
2103         err->num_errors++;
2104         return;
2105
2106 out_free:
2107         free(str);
2108         free(help);
2109 }
2110
2111 #define MAX_WIDTH 1000
2112 static int get_term_width(void)
2113 {
2114         struct winsize ws;
2115
2116         get_term_dimensions(&ws);
2117         return ws.ws_col > MAX_WIDTH ? MAX_WIDTH : ws.ws_col;
2118 }
2119
2120 static void __parse_events_error__print(int err_idx, const char *err_str,
2121                                         const char *err_help, const char *event)
2122 {
2123         const char *str = "invalid or unsupported event: ";
2124         char _buf[MAX_WIDTH];
2125         char *buf = (char *) event;
2126         int idx = 0;
2127         if (err_str) {
2128                 /* -2 for extra '' in the final fprintf */
2129                 int width       = get_term_width() - 2;
2130                 int len_event   = strlen(event);
2131                 int len_str, max_len, cut = 0;
2132
2133                 /*
2134                  * Maximum error index indent, we will cut
2135                  * the event string if it's bigger.
2136                  */
2137                 int max_err_idx = 13;
2138
2139                 /*
2140                  * Let's be specific with the message when
2141                  * we have the precise error.
2142                  */
2143                 str     = "event syntax error: ";
2144                 len_str = strlen(str);
2145                 max_len = width - len_str;
2146
2147                 buf = _buf;
2148
2149                 /* We're cutting from the beginning. */
2150                 if (err_idx > max_err_idx)
2151                         cut = err_idx - max_err_idx;
2152
2153                 strncpy(buf, event + cut, max_len);
2154
2155                 /* Mark cut parts with '..' on both sides. */
2156                 if (cut)
2157                         buf[0] = buf[1] = '.';
2158
2159                 if ((len_event - cut) > max_len) {
2160                         buf[max_len - 1] = buf[max_len - 2] = '.';
2161                         buf[max_len] = 0;
2162                 }
2163
2164                 idx = len_str + err_idx - cut;
2165         }
2166
2167         fprintf(stderr, "%s'%s'\n", str, buf);
2168         if (idx) {
2169                 fprintf(stderr, "%*s\\___ %s\n", idx + 1, "", err_str);
2170                 if (err_help)
2171                         fprintf(stderr, "\n%s\n", err_help);
2172         }
2173 }
2174
2175 void parse_events_error__print(struct parse_events_error *err,
2176                                const char *event)
2177 {
2178         if (!err->num_errors)
2179                 return;
2180
2181         __parse_events_error__print(err->idx, err->str, err->help, event);
2182
2183         if (err->num_errors > 1) {
2184                 fputs("\nInitial error:\n", stderr);
2185                 __parse_events_error__print(err->first_idx, err->first_str,
2186                                         err->first_help, event);
2187         }
2188 }
2189
2190 #undef MAX_WIDTH
2191
2192 int parse_events_option(const struct option *opt, const char *str,
2193                         int unset __maybe_unused)
2194 {
2195         struct parse_events_option_args *args = opt->value;
2196         struct parse_events_error err;
2197         int ret;
2198
2199         parse_events_error__init(&err);
2200         ret = __parse_events(*args->evlistp, str, args->pmu_filter, &err,
2201                              /*fake_pmu=*/NULL, /*warn_if_reordered=*/true);
2202
2203         if (ret) {
2204                 parse_events_error__print(&err, str);
2205                 fprintf(stderr, "Run 'perf list' for a list of valid events\n");
2206         }
2207         parse_events_error__exit(&err);
2208
2209         return ret;
2210 }
2211
2212 int parse_events_option_new_evlist(const struct option *opt, const char *str, int unset)
2213 {
2214         struct parse_events_option_args *args = opt->value;
2215         int ret;
2216
2217         if (*args->evlistp == NULL) {
2218                 *args->evlistp = evlist__new();
2219
2220                 if (*args->evlistp == NULL) {
2221                         fprintf(stderr, "Not enough memory to create evlist\n");
2222                         return -1;
2223                 }
2224         }
2225         ret = parse_events_option(opt, str, unset);
2226         if (ret) {
2227                 evlist__delete(*args->evlistp);
2228                 *args->evlistp = NULL;
2229         }
2230
2231         return ret;
2232 }
2233
2234 static int
2235 foreach_evsel_in_last_glob(struct evlist *evlist,
2236                            int (*func)(struct evsel *evsel,
2237                                        const void *arg),
2238                            const void *arg)
2239 {
2240         struct evsel *last = NULL;
2241         int err;
2242
2243         /*
2244          * Don't return when list_empty, give func a chance to report
2245          * error when it found last == NULL.
2246          *
2247          * So no need to WARN here, let *func do this.
2248          */
2249         if (evlist->core.nr_entries > 0)
2250                 last = evlist__last(evlist);
2251
2252         do {
2253                 err = (*func)(last, arg);
2254                 if (err)
2255                         return -1;
2256                 if (!last)
2257                         return 0;
2258
2259                 if (last->core.node.prev == &evlist->core.entries)
2260                         return 0;
2261                 last = list_entry(last->core.node.prev, struct evsel, core.node);
2262         } while (!last->cmdline_group_boundary);
2263
2264         return 0;
2265 }
2266
2267 static int set_filter(struct evsel *evsel, const void *arg)
2268 {
2269         const char *str = arg;
2270         bool found = false;
2271         int nr_addr_filters = 0;
2272         struct perf_pmu *pmu = NULL;
2273
2274         if (evsel == NULL) {
2275                 fprintf(stderr,
2276                         "--filter option should follow a -e tracepoint or HW tracer option\n");
2277                 return -1;
2278         }
2279
2280         if (evsel->core.attr.type == PERF_TYPE_TRACEPOINT) {
2281                 if (evsel__append_tp_filter(evsel, str) < 0) {
2282                         fprintf(stderr,
2283                                 "not enough memory to hold filter string\n");
2284                         return -1;
2285                 }
2286
2287                 return 0;
2288         }
2289
2290         while ((pmu = perf_pmus__scan(pmu)) != NULL)
2291                 if (pmu->type == evsel->core.attr.type) {
2292                         found = true;
2293                         break;
2294                 }
2295
2296         if (found)
2297                 perf_pmu__scan_file(pmu, "nr_addr_filters",
2298                                     "%d", &nr_addr_filters);
2299
2300         if (!nr_addr_filters)
2301                 return perf_bpf_filter__parse(&evsel->bpf_filters, str);
2302
2303         if (evsel__append_addr_filter(evsel, str) < 0) {
2304                 fprintf(stderr,
2305                         "not enough memory to hold filter string\n");
2306                 return -1;
2307         }
2308
2309         return 0;
2310 }
2311
2312 int parse_filter(const struct option *opt, const char *str,
2313                  int unset __maybe_unused)
2314 {
2315         struct evlist *evlist = *(struct evlist **)opt->value;
2316
2317         return foreach_evsel_in_last_glob(evlist, set_filter,
2318                                           (const void *)str);
2319 }
2320
2321 static int add_exclude_perf_filter(struct evsel *evsel,
2322                                    const void *arg __maybe_unused)
2323 {
2324         char new_filter[64];
2325
2326         if (evsel == NULL || evsel->core.attr.type != PERF_TYPE_TRACEPOINT) {
2327                 fprintf(stderr,
2328                         "--exclude-perf option should follow a -e tracepoint option\n");
2329                 return -1;
2330         }
2331
2332         snprintf(new_filter, sizeof(new_filter), "common_pid != %d", getpid());
2333
2334         if (evsel__append_tp_filter(evsel, new_filter) < 0) {
2335                 fprintf(stderr,
2336                         "not enough memory to hold filter string\n");
2337                 return -1;
2338         }
2339
2340         return 0;
2341 }
2342
2343 int exclude_perf(const struct option *opt,
2344                  const char *arg __maybe_unused,
2345                  int unset __maybe_unused)
2346 {
2347         struct evlist *evlist = *(struct evlist **)opt->value;
2348
2349         return foreach_evsel_in_last_glob(evlist, add_exclude_perf_filter,
2350                                           NULL);
2351 }
2352
2353 int parse_events__is_hardcoded_term(struct parse_events_term *term)
2354 {
2355         return term->type_term != PARSE_EVENTS__TERM_TYPE_USER;
2356 }
2357
2358 static int new_term(struct parse_events_term **_term,
2359                     struct parse_events_term *temp,
2360                     char *str, u64 num)
2361 {
2362         struct parse_events_term *term;
2363
2364         term = malloc(sizeof(*term));
2365         if (!term)
2366                 return -ENOMEM;
2367
2368         *term = *temp;
2369         INIT_LIST_HEAD(&term->list);
2370         term->weak = false;
2371
2372         switch (term->type_val) {
2373         case PARSE_EVENTS__TERM_TYPE_NUM:
2374                 term->val.num = num;
2375                 break;
2376         case PARSE_EVENTS__TERM_TYPE_STR:
2377                 term->val.str = str;
2378                 break;
2379         default:
2380                 free(term);
2381                 return -EINVAL;
2382         }
2383
2384         *_term = term;
2385         return 0;
2386 }
2387
2388 int parse_events_term__num(struct parse_events_term **term,
2389                            int type_term, const char *config, u64 num,
2390                            bool no_value,
2391                            void *loc_term_, void *loc_val_)
2392 {
2393         YYLTYPE *loc_term = loc_term_;
2394         YYLTYPE *loc_val = loc_val_;
2395
2396         struct parse_events_term temp = {
2397                 .type_val  = PARSE_EVENTS__TERM_TYPE_NUM,
2398                 .type_term = type_term,
2399                 .config    = config ? : strdup(config_term_names[type_term]),
2400                 .no_value  = no_value,
2401                 .err_term  = loc_term ? loc_term->first_column : 0,
2402                 .err_val   = loc_val  ? loc_val->first_column  : 0,
2403         };
2404
2405         return new_term(term, &temp, NULL, num);
2406 }
2407
2408 int parse_events_term__str(struct parse_events_term **term,
2409                            int type_term, char *config, char *str,
2410                            void *loc_term_, void *loc_val_)
2411 {
2412         YYLTYPE *loc_term = loc_term_;
2413         YYLTYPE *loc_val = loc_val_;
2414
2415         struct parse_events_term temp = {
2416                 .type_val  = PARSE_EVENTS__TERM_TYPE_STR,
2417                 .type_term = type_term,
2418                 .config    = config,
2419                 .err_term  = loc_term ? loc_term->first_column : 0,
2420                 .err_val   = loc_val  ? loc_val->first_column  : 0,
2421         };
2422
2423         return new_term(term, &temp, str, 0);
2424 }
2425
2426 int parse_events_term__term(struct parse_events_term **term,
2427                             int term_lhs, int term_rhs,
2428                             void *loc_term, void *loc_val)
2429 {
2430         return parse_events_term__str(term, term_lhs, NULL,
2431                                       strdup(config_term_names[term_rhs]),
2432                                       loc_term, loc_val);
2433 }
2434
2435 int parse_events_term__clone(struct parse_events_term **new,
2436                              struct parse_events_term *term)
2437 {
2438         char *str;
2439         struct parse_events_term temp = {
2440                 .type_val  = term->type_val,
2441                 .type_term = term->type_term,
2442                 .config    = NULL,
2443                 .err_term  = term->err_term,
2444                 .err_val   = term->err_val,
2445         };
2446
2447         if (term->config) {
2448                 temp.config = strdup(term->config);
2449                 if (!temp.config)
2450                         return -ENOMEM;
2451         }
2452         if (term->type_val == PARSE_EVENTS__TERM_TYPE_NUM)
2453                 return new_term(new, &temp, NULL, term->val.num);
2454
2455         str = strdup(term->val.str);
2456         if (!str)
2457                 return -ENOMEM;
2458         return new_term(new, &temp, str, 0);
2459 }
2460
2461 void parse_events_term__delete(struct parse_events_term *term)
2462 {
2463         if (term->type_val != PARSE_EVENTS__TERM_TYPE_NUM)
2464                 zfree(&term->val.str);
2465
2466         zfree(&term->config);
2467         free(term);
2468 }
2469
2470 int parse_events_copy_term_list(struct list_head *old,
2471                                  struct list_head **new)
2472 {
2473         struct parse_events_term *term, *n;
2474         int ret;
2475
2476         if (!old) {
2477                 *new = NULL;
2478                 return 0;
2479         }
2480
2481         *new = malloc(sizeof(struct list_head));
2482         if (!*new)
2483                 return -ENOMEM;
2484         INIT_LIST_HEAD(*new);
2485
2486         list_for_each_entry (term, old, list) {
2487                 ret = parse_events_term__clone(&n, term);
2488                 if (ret)
2489                         return ret;
2490                 list_add_tail(&n->list, *new);
2491         }
2492         return 0;
2493 }
2494
2495 void parse_events_terms__purge(struct list_head *terms)
2496 {
2497         struct parse_events_term *term, *h;
2498
2499         list_for_each_entry_safe(term, h, terms, list) {
2500                 list_del_init(&term->list);
2501                 parse_events_term__delete(term);
2502         }
2503 }
2504
2505 void parse_events_terms__delete(struct list_head *terms)
2506 {
2507         if (!terms)
2508                 return;
2509         parse_events_terms__purge(terms);
2510         free(terms);
2511 }
2512
2513 int parse_events_term__to_strbuf(struct list_head *term_list, struct strbuf *sb)
2514 {
2515         struct parse_events_term *term;
2516         bool first = true;
2517
2518         if (!term_list)
2519                 return 0;
2520
2521         list_for_each_entry(term, term_list, list) {
2522                 int ret;
2523
2524                 if (!first) {
2525                         ret = strbuf_addch(sb, ',');
2526                         if (ret < 0)
2527                                 return ret;
2528                 }
2529                 first = false;
2530
2531                 if (term->type_val == PARSE_EVENTS__TERM_TYPE_NUM)
2532                         if (term->type_term == PARSE_EVENTS__TERM_TYPE_USER && term->val.num == 1)
2533                                 ret = strbuf_addf(sb, "%s", term->config);
2534                         else
2535                                 ret = strbuf_addf(sb, "%s=%#"PRIx64, term->config, term->val.num);
2536                 else if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR) {
2537                         if (term->config) {
2538                                 ret = strbuf_addf(sb, "%s=", term->config);
2539                                 if (ret < 0)
2540                                         return ret;
2541                         } else if (term->type_term < __PARSE_EVENTS__TERM_TYPE_NR) {
2542                                 ret = strbuf_addf(sb, "%s=", config_term_names[term->type_term]);
2543                                 if (ret < 0)
2544                                         return ret;
2545                         }
2546                         ret = strbuf_addf(sb, "%s", term->val.str);
2547                 }
2548                 if (ret < 0)
2549                         return ret;
2550         }
2551         return 0;
2552 }
2553
2554 void parse_events_evlist_error(struct parse_events_state *parse_state,
2555                                int idx, const char *str)
2556 {
2557         if (!parse_state->error)
2558                 return;
2559
2560         parse_events_error__handle(parse_state->error, idx, strdup(str), NULL);
2561 }
2562
2563 static void config_terms_list(char *buf, size_t buf_sz)
2564 {
2565         int i;
2566         bool first = true;
2567
2568         buf[0] = '\0';
2569         for (i = 0; i < __PARSE_EVENTS__TERM_TYPE_NR; i++) {
2570                 const char *name = config_term_names[i];
2571
2572                 if (!config_term_avail(i, NULL))
2573                         continue;
2574                 if (!name)
2575                         continue;
2576                 if (name[0] == '<')
2577                         continue;
2578
2579                 if (strlen(buf) + strlen(name) + 2 >= buf_sz)
2580                         return;
2581
2582                 if (!first)
2583                         strcat(buf, ",");
2584                 else
2585                         first = false;
2586                 strcat(buf, name);
2587         }
2588 }
2589
2590 /*
2591  * Return string contains valid config terms of an event.
2592  * @additional_terms: For terms such as PMU sysfs terms.
2593  */
2594 char *parse_events_formats_error_string(char *additional_terms)
2595 {
2596         char *str;
2597         /* "no-overwrite" is the longest name */
2598         char static_terms[__PARSE_EVENTS__TERM_TYPE_NR *
2599                           (sizeof("no-overwrite") - 1)];
2600
2601         config_terms_list(static_terms, sizeof(static_terms));
2602         /* valid terms */
2603         if (additional_terms) {
2604                 if (asprintf(&str, "valid terms: %s,%s",
2605                              additional_terms, static_terms) < 0)
2606                         goto fail;
2607         } else {
2608                 if (asprintf(&str, "valid terms: %s", static_terms) < 0)
2609                         goto fail;
2610         }
2611         return str;
2612
2613 fail:
2614         return NULL;
2615 }