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