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