1 #include <linux/hw_breakpoint.h>
7 #include <subcmd/parse-options.h>
8 #include "parse-events.h"
9 #include <subcmd/exec-cmd.h>
14 #include "bpf-loader.h"
16 #include <api/fs/tracing_path.h>
17 #include "parse-events-bison.h"
18 #define YY_EXTRA_TYPE int
19 #include "parse-events-flex.h"
21 #include "thread_map.h"
23 #include "probe-file.h"
25 #include "util/parse-branch-options.h"
27 #define MAX_NAME_LEN 100
30 extern int parse_events_debug;
32 int parse_events_parse(void *data, void *scanner);
33 static int get_config_terms(struct list_head *head_config,
34 struct list_head *head_terms __maybe_unused);
36 static struct perf_pmu_event_symbol *perf_pmu_events_list;
38 * The variable indicates the number of supported pmu event symbols.
39 * 0 means not initialized and ready to init
40 * -1 means failed to init, don't try anymore
41 * >0 is the number of supported pmu event symbols
43 static int perf_pmu_events_list_num;
45 struct event_symbol event_symbols_hw[PERF_COUNT_HW_MAX] = {
46 [PERF_COUNT_HW_CPU_CYCLES] = {
47 .symbol = "cpu-cycles",
50 [PERF_COUNT_HW_INSTRUCTIONS] = {
51 .symbol = "instructions",
54 [PERF_COUNT_HW_CACHE_REFERENCES] = {
55 .symbol = "cache-references",
58 [PERF_COUNT_HW_CACHE_MISSES] = {
59 .symbol = "cache-misses",
62 [PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = {
63 .symbol = "branch-instructions",
66 [PERF_COUNT_HW_BRANCH_MISSES] = {
67 .symbol = "branch-misses",
70 [PERF_COUNT_HW_BUS_CYCLES] = {
71 .symbol = "bus-cycles",
74 [PERF_COUNT_HW_STALLED_CYCLES_FRONTEND] = {
75 .symbol = "stalled-cycles-frontend",
76 .alias = "idle-cycles-frontend",
78 [PERF_COUNT_HW_STALLED_CYCLES_BACKEND] = {
79 .symbol = "stalled-cycles-backend",
80 .alias = "idle-cycles-backend",
82 [PERF_COUNT_HW_REF_CPU_CYCLES] = {
83 .symbol = "ref-cycles",
88 struct event_symbol event_symbols_sw[PERF_COUNT_SW_MAX] = {
89 [PERF_COUNT_SW_CPU_CLOCK] = {
90 .symbol = "cpu-clock",
93 [PERF_COUNT_SW_TASK_CLOCK] = {
94 .symbol = "task-clock",
97 [PERF_COUNT_SW_PAGE_FAULTS] = {
98 .symbol = "page-faults",
101 [PERF_COUNT_SW_CONTEXT_SWITCHES] = {
102 .symbol = "context-switches",
105 [PERF_COUNT_SW_CPU_MIGRATIONS] = {
106 .symbol = "cpu-migrations",
107 .alias = "migrations",
109 [PERF_COUNT_SW_PAGE_FAULTS_MIN] = {
110 .symbol = "minor-faults",
113 [PERF_COUNT_SW_PAGE_FAULTS_MAJ] = {
114 .symbol = "major-faults",
117 [PERF_COUNT_SW_ALIGNMENT_FAULTS] = {
118 .symbol = "alignment-faults",
121 [PERF_COUNT_SW_EMULATION_FAULTS] = {
122 .symbol = "emulation-faults",
125 [PERF_COUNT_SW_DUMMY] = {
129 [PERF_COUNT_SW_BPF_OUTPUT] = {
130 .symbol = "bpf-output",
135 #define __PERF_EVENT_FIELD(config, name) \
136 ((config & PERF_EVENT_##name##_MASK) >> PERF_EVENT_##name##_SHIFT)
138 #define PERF_EVENT_RAW(config) __PERF_EVENT_FIELD(config, RAW)
139 #define PERF_EVENT_CONFIG(config) __PERF_EVENT_FIELD(config, CONFIG)
140 #define PERF_EVENT_TYPE(config) __PERF_EVENT_FIELD(config, TYPE)
141 #define PERF_EVENT_ID(config) __PERF_EVENT_FIELD(config, EVENT)
143 #define for_each_subsystem(sys_dir, sys_dirent) \
144 while ((sys_dirent = readdir(sys_dir)) != NULL) \
145 if (sys_dirent->d_type == DT_DIR && \
146 (strcmp(sys_dirent->d_name, ".")) && \
147 (strcmp(sys_dirent->d_name, "..")))
149 static int tp_event_has_id(struct dirent *sys_dir, struct dirent *evt_dir)
151 char evt_path[MAXPATHLEN];
154 snprintf(evt_path, MAXPATHLEN, "%s/%s/%s/id", tracing_events_path,
155 sys_dir->d_name, evt_dir->d_name);
156 fd = open(evt_path, O_RDONLY);
164 #define for_each_event(sys_dirent, evt_dir, evt_dirent) \
165 while ((evt_dirent = readdir(evt_dir)) != NULL) \
166 if (evt_dirent->d_type == DT_DIR && \
167 (strcmp(evt_dirent->d_name, ".")) && \
168 (strcmp(evt_dirent->d_name, "..")) && \
169 (!tp_event_has_id(sys_dirent, evt_dirent)))
171 #define MAX_EVENT_LENGTH 512
174 struct tracepoint_path *tracepoint_id_to_path(u64 config)
176 struct tracepoint_path *path = NULL;
177 DIR *sys_dir, *evt_dir;
178 struct dirent *sys_dirent, *evt_dirent;
182 char evt_path[MAXPATHLEN];
183 char dir_path[MAXPATHLEN];
185 sys_dir = opendir(tracing_events_path);
189 for_each_subsystem(sys_dir, sys_dirent) {
191 snprintf(dir_path, MAXPATHLEN, "%s/%s", tracing_events_path,
193 evt_dir = opendir(dir_path);
197 for_each_event(sys_dirent, evt_dir, evt_dirent) {
199 snprintf(evt_path, MAXPATHLEN, "%s/%s/id", dir_path,
201 fd = open(evt_path, O_RDONLY);
204 if (read(fd, id_buf, sizeof(id_buf)) < 0) {
213 path = zalloc(sizeof(*path));
216 path->system = malloc(MAX_EVENT_LENGTH);
221 path->name = malloc(MAX_EVENT_LENGTH);
223 zfree(&path->system);
227 strncpy(path->system, sys_dirent->d_name,
229 strncpy(path->name, evt_dirent->d_name,
241 struct tracepoint_path *tracepoint_name_to_path(const char *name)
243 struct tracepoint_path *path = zalloc(sizeof(*path));
244 char *str = strchr(name, ':');
246 if (path == NULL || str == NULL) {
251 path->system = strndup(name, str - name);
252 path->name = strdup(str+1);
254 if (path->system == NULL || path->name == NULL) {
255 zfree(&path->system);
263 const char *event_type(int type)
266 case PERF_TYPE_HARDWARE:
269 case PERF_TYPE_SOFTWARE:
272 case PERF_TYPE_TRACEPOINT:
275 case PERF_TYPE_HW_CACHE:
276 return "hardware-cache";
285 static int parse_events__is_name_term(struct parse_events_term *term)
287 return term->type_term == PARSE_EVENTS__TERM_TYPE_NAME;
290 static char *get_config_name(struct list_head *head_terms)
292 struct parse_events_term *term;
297 list_for_each_entry(term, head_terms, list)
298 if (parse_events__is_name_term(term))
299 return term->val.str;
304 static struct perf_evsel *
305 __add_event(struct list_head *list, int *idx,
306 struct perf_event_attr *attr,
307 char *name, struct cpu_map *cpus,
308 struct list_head *config_terms)
310 struct perf_evsel *evsel;
312 event_attr_init(attr);
314 evsel = perf_evsel__new_idx(attr, *idx);
319 evsel->cpus = cpu_map__get(cpus);
320 evsel->own_cpus = cpu_map__get(cpus);
323 evsel->name = strdup(name);
326 list_splice(config_terms, &evsel->config_terms);
328 list_add_tail(&evsel->node, list);
332 static int add_event(struct list_head *list, int *idx,
333 struct perf_event_attr *attr, char *name,
334 struct list_head *config_terms)
336 return __add_event(list, idx, attr, name, NULL, config_terms) ? 0 : -ENOMEM;
339 static int parse_aliases(char *str, const char *names[][PERF_EVSEL__MAX_ALIASES], int size)
344 for (i = 0; i < size; i++) {
345 for (j = 0; j < PERF_EVSEL__MAX_ALIASES && names[i][j]; j++) {
346 n = strlen(names[i][j]);
347 if (n > longest && !strncasecmp(str, names[i][j], n))
357 typedef int config_term_func_t(struct perf_event_attr *attr,
358 struct parse_events_term *term,
359 struct parse_events_error *err);
360 static int config_term_common(struct perf_event_attr *attr,
361 struct parse_events_term *term,
362 struct parse_events_error *err);
363 static int config_attr(struct perf_event_attr *attr,
364 struct list_head *head,
365 struct parse_events_error *err,
366 config_term_func_t config_term);
368 int parse_events_add_cache(struct list_head *list, int *idx,
369 char *type, char *op_result1, char *op_result2,
370 struct parse_events_error *err,
371 struct list_head *head_config)
373 struct perf_event_attr attr;
374 LIST_HEAD(config_terms);
375 char name[MAX_NAME_LEN], *config_name;
376 int cache_type = -1, cache_op = -1, cache_result = -1;
377 char *op_result[2] = { op_result1, op_result2 };
381 * No fallback - if we cannot get a clear cache type
384 cache_type = parse_aliases(type, perf_evsel__hw_cache,
385 PERF_COUNT_HW_CACHE_MAX);
386 if (cache_type == -1)
389 config_name = get_config_name(head_config);
390 n = snprintf(name, MAX_NAME_LEN, "%s", type);
392 for (i = 0; (i < 2) && (op_result[i]); i++) {
393 char *str = op_result[i];
395 n += snprintf(name + n, MAX_NAME_LEN - n, "-%s", str);
397 if (cache_op == -1) {
398 cache_op = parse_aliases(str, perf_evsel__hw_cache_op,
399 PERF_COUNT_HW_CACHE_OP_MAX);
401 if (!perf_evsel__is_cache_op_valid(cache_type, cache_op))
407 if (cache_result == -1) {
408 cache_result = parse_aliases(str, perf_evsel__hw_cache_result,
409 PERF_COUNT_HW_CACHE_RESULT_MAX);
410 if (cache_result >= 0)
416 * Fall back to reads:
419 cache_op = PERF_COUNT_HW_CACHE_OP_READ;
422 * Fall back to accesses:
424 if (cache_result == -1)
425 cache_result = PERF_COUNT_HW_CACHE_RESULT_ACCESS;
427 memset(&attr, 0, sizeof(attr));
428 attr.config = cache_type | (cache_op << 8) | (cache_result << 16);
429 attr.type = PERF_TYPE_HW_CACHE;
432 if (config_attr(&attr, head_config, err,
436 if (get_config_terms(head_config, &config_terms))
439 return add_event(list, idx, &attr, config_name ? : name, &config_terms);
442 static void tracepoint_error(struct parse_events_error *e, int err,
443 const char *sys, const char *name)
451 * We get error directly from syscall errno ( > 0),
452 * or from encoded pointer's error ( < 0).
458 e->str = strdup("can't access trace events");
461 e->str = strdup("unknown tracepoint");
464 e->str = strdup("failed to add tracepoint");
468 tracing_path__strerror_open_tp(err, help, sizeof(help), sys, name);
469 e->help = strdup(help);
472 static int add_tracepoint(struct list_head *list, int *idx,
473 const char *sys_name, const char *evt_name,
474 struct parse_events_error *err,
475 struct list_head *head_config)
477 struct perf_evsel *evsel;
479 evsel = perf_evsel__newtp_idx(sys_name, evt_name, (*idx)++);
481 tracepoint_error(err, PTR_ERR(evsel), sys_name, evt_name);
482 return PTR_ERR(evsel);
486 LIST_HEAD(config_terms);
488 if (get_config_terms(head_config, &config_terms))
490 list_splice(&config_terms, &evsel->config_terms);
493 list_add_tail(&evsel->node, list);
497 static int add_tracepoint_multi_event(struct list_head *list, int *idx,
498 const char *sys_name, const char *evt_name,
499 struct parse_events_error *err,
500 struct list_head *head_config)
502 char evt_path[MAXPATHLEN];
503 struct dirent *evt_ent;
505 int ret = 0, found = 0;
507 snprintf(evt_path, MAXPATHLEN, "%s/%s", tracing_events_path, sys_name);
508 evt_dir = opendir(evt_path);
510 tracepoint_error(err, errno, sys_name, evt_name);
514 while (!ret && (evt_ent = readdir(evt_dir))) {
515 if (!strcmp(evt_ent->d_name, ".")
516 || !strcmp(evt_ent->d_name, "..")
517 || !strcmp(evt_ent->d_name, "enable")
518 || !strcmp(evt_ent->d_name, "filter"))
521 if (!strglobmatch(evt_ent->d_name, evt_name))
526 ret = add_tracepoint(list, idx, sys_name, evt_ent->d_name,
531 tracepoint_error(err, ENOENT, sys_name, evt_name);
539 static int add_tracepoint_event(struct list_head *list, int *idx,
540 const char *sys_name, const char *evt_name,
541 struct parse_events_error *err,
542 struct list_head *head_config)
544 return strpbrk(evt_name, "*?") ?
545 add_tracepoint_multi_event(list, idx, sys_name, evt_name,
547 add_tracepoint(list, idx, sys_name, evt_name,
551 static int add_tracepoint_multi_sys(struct list_head *list, int *idx,
552 const char *sys_name, const char *evt_name,
553 struct parse_events_error *err,
554 struct list_head *head_config)
556 struct dirent *events_ent;
560 events_dir = opendir(tracing_events_path);
562 tracepoint_error(err, errno, sys_name, evt_name);
566 while (!ret && (events_ent = readdir(events_dir))) {
567 if (!strcmp(events_ent->d_name, ".")
568 || !strcmp(events_ent->d_name, "..")
569 || !strcmp(events_ent->d_name, "enable")
570 || !strcmp(events_ent->d_name, "header_event")
571 || !strcmp(events_ent->d_name, "header_page"))
574 if (!strglobmatch(events_ent->d_name, sys_name))
577 ret = add_tracepoint_event(list, idx, events_ent->d_name,
578 evt_name, err, head_config);
581 closedir(events_dir);
585 struct __add_bpf_event_param {
586 struct parse_events_evlist *data;
587 struct list_head *list;
588 struct list_head *head_config;
591 static int add_bpf_event(const char *group, const char *event, int fd,
594 LIST_HEAD(new_evsels);
595 struct __add_bpf_event_param *param = _param;
596 struct parse_events_evlist *evlist = param->data;
597 struct list_head *list = param->list;
598 struct perf_evsel *pos;
601 pr_debug("add bpf event %s:%s and attach bpf program %d\n",
604 err = parse_events_add_tracepoint(&new_evsels, &evlist->idx, group,
605 event, evlist->error,
608 struct perf_evsel *evsel, *tmp;
610 pr_debug("Failed to add BPF event %s:%s\n",
612 list_for_each_entry_safe(evsel, tmp, &new_evsels, node) {
613 list_del(&evsel->node);
614 perf_evsel__delete(evsel);
618 pr_debug("adding %s:%s\n", group, event);
620 list_for_each_entry(pos, &new_evsels, node) {
621 pr_debug("adding %s:%s to %p\n",
625 list_splice(&new_evsels, list);
629 int parse_events_load_bpf_obj(struct parse_events_evlist *data,
630 struct list_head *list,
631 struct bpf_object *obj,
632 struct list_head *head_config)
636 struct __add_bpf_event_param param = {data, list, head_config};
637 static bool registered_unprobe_atexit = false;
639 if (IS_ERR(obj) || !obj) {
640 snprintf(errbuf, sizeof(errbuf),
641 "Internal error: load bpf obj with NULL");
647 * Register atexit handler before calling bpf__probe() so
648 * bpf__probe() don't need to unprobe probe points its already
649 * created when failure.
651 if (!registered_unprobe_atexit) {
653 registered_unprobe_atexit = true;
656 err = bpf__probe(obj);
658 bpf__strerror_probe(obj, err, errbuf, sizeof(errbuf));
662 err = bpf__load(obj);
664 bpf__strerror_load(obj, err, errbuf, sizeof(errbuf));
668 err = bpf__foreach_event(obj, add_bpf_event, ¶m);
670 snprintf(errbuf, sizeof(errbuf),
671 "Attach events in BPF object failed");
677 data->error->help = strdup("(add -v to see detail)");
678 data->error->str = strdup(errbuf);
683 parse_events_config_bpf(struct parse_events_evlist *data,
684 struct bpf_object *obj,
685 struct list_head *head_config)
687 struct parse_events_term *term;
690 if (!head_config || list_empty(head_config))
693 list_for_each_entry(term, head_config, list) {
697 if (term->type_term != PARSE_EVENTS__TERM_TYPE_USER) {
698 snprintf(errbuf, sizeof(errbuf),
699 "Invalid config term for BPF object");
700 errbuf[BUFSIZ - 1] = '\0';
702 data->error->idx = term->err_term;
703 data->error->str = strdup(errbuf);
707 err = bpf__config_obj(obj, term, data->evlist, &error_pos);
709 bpf__strerror_config_obj(obj, term, data->evlist,
710 &error_pos, err, errbuf,
712 data->error->help = strdup(
713 "Hint:\tValid config terms:\n"
714 " \tmap:[<arraymap>].value<indices>=[value]\n"
715 " \tmap:[<eventmap>].event<indices>=[event]\n"
717 " \twhere <indices> is something like [0,3...5] or [all]\n"
718 " \t(add -v to see detail)");
719 data->error->str = strdup(errbuf);
720 if (err == -BPF_LOADER_ERRNO__OBJCONF_MAP_VALUE)
721 data->error->idx = term->err_val;
723 data->error->idx = term->err_term + error_pos;
731 * Split config terms:
732 * perf record -e bpf.c/call-graph=fp,map:array.value[0]=1/ ...
733 * 'call-graph=fp' is 'evt config', should be applied to each
735 * 'map:array.value[0]=1' is 'obj config', should be processed
736 * with parse_events_config_bpf.
738 * Move object config terms from the first list to obj_head_config.
741 split_bpf_config_terms(struct list_head *evt_head_config,
742 struct list_head *obj_head_config)
744 struct parse_events_term *term, *temp;
747 * Currectly, all possible user config term
748 * belong to bpf object. parse_events__is_hardcoded_term()
749 * happends to be a good flag.
751 * See parse_events_config_bpf() and
752 * config_term_tracepoint().
754 list_for_each_entry_safe(term, temp, evt_head_config, list)
755 if (!parse_events__is_hardcoded_term(term))
756 list_move_tail(&term->list, obj_head_config);
759 int parse_events_load_bpf(struct parse_events_evlist *data,
760 struct list_head *list,
763 struct list_head *head_config)
766 struct bpf_object *obj;
767 LIST_HEAD(obj_head_config);
770 split_bpf_config_terms(head_config, &obj_head_config);
772 obj = bpf__prepare_load(bpf_file_name, source);
779 snprintf(errbuf, sizeof(errbuf),
780 "BPF support is not compiled");
782 bpf__strerror_prepare_load(bpf_file_name,
787 data->error->help = strdup("(add -v to see detail)");
788 data->error->str = strdup(errbuf);
792 err = parse_events_load_bpf_obj(data, list, obj, head_config);
795 err = parse_events_config_bpf(data, obj, &obj_head_config);
798 * Caller doesn't know anything about obj_head_config,
799 * so combine them together again before returnning.
802 list_splice_tail(&obj_head_config, head_config);
807 parse_breakpoint_type(const char *type, struct perf_event_attr *attr)
811 for (i = 0; i < 3; i++) {
812 if (!type || !type[i])
815 #define CHECK_SET_TYPE(bit) \
817 if (attr->bp_type & bit) \
820 attr->bp_type |= bit; \
825 CHECK_SET_TYPE(HW_BREAKPOINT_R);
828 CHECK_SET_TYPE(HW_BREAKPOINT_W);
831 CHECK_SET_TYPE(HW_BREAKPOINT_X);
838 #undef CHECK_SET_TYPE
840 if (!attr->bp_type) /* Default */
841 attr->bp_type = HW_BREAKPOINT_R | HW_BREAKPOINT_W;
846 int parse_events_add_breakpoint(struct list_head *list, int *idx,
847 void *ptr, char *type, u64 len)
849 struct perf_event_attr attr;
851 memset(&attr, 0, sizeof(attr));
852 attr.bp_addr = (unsigned long) ptr;
854 if (parse_breakpoint_type(type, &attr))
857 /* Provide some defaults if len is not specified */
859 if (attr.bp_type == HW_BREAKPOINT_X)
862 len = HW_BREAKPOINT_LEN_4;
867 attr.type = PERF_TYPE_BREAKPOINT;
868 attr.sample_period = 1;
870 return add_event(list, idx, &attr, NULL, NULL);
873 static int check_type_val(struct parse_events_term *term,
874 struct parse_events_error *err,
877 if (type == term->type_val)
881 err->idx = term->err_val;
882 if (type == PARSE_EVENTS__TERM_TYPE_NUM)
883 err->str = strdup("expected numeric value");
885 err->str = strdup("expected string value");
891 * Update according to parse-events.l
893 static const char *config_term_names[__PARSE_EVENTS__TERM_TYPE_NR] = {
894 [PARSE_EVENTS__TERM_TYPE_USER] = "<sysfs term>",
895 [PARSE_EVENTS__TERM_TYPE_CONFIG] = "config",
896 [PARSE_EVENTS__TERM_TYPE_CONFIG1] = "config1",
897 [PARSE_EVENTS__TERM_TYPE_CONFIG2] = "config2",
898 [PARSE_EVENTS__TERM_TYPE_NAME] = "name",
899 [PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD] = "period",
900 [PARSE_EVENTS__TERM_TYPE_SAMPLE_FREQ] = "freq",
901 [PARSE_EVENTS__TERM_TYPE_BRANCH_SAMPLE_TYPE] = "branch_type",
902 [PARSE_EVENTS__TERM_TYPE_TIME] = "time",
903 [PARSE_EVENTS__TERM_TYPE_CALLGRAPH] = "call-graph",
904 [PARSE_EVENTS__TERM_TYPE_STACKSIZE] = "stack-size",
905 [PARSE_EVENTS__TERM_TYPE_NOINHERIT] = "no-inherit",
906 [PARSE_EVENTS__TERM_TYPE_INHERIT] = "inherit",
907 [PARSE_EVENTS__TERM_TYPE_MAX_STACK] = "max-stack",
908 [PARSE_EVENTS__TERM_TYPE_OVERWRITE] = "overwrite",
909 [PARSE_EVENTS__TERM_TYPE_NOOVERWRITE] = "no-overwrite",
910 [PARSE_EVENTS__TERM_TYPE_DRV_CFG] = "driver-config",
913 static bool config_term_shrinked;
916 config_term_avail(int term_type, struct parse_events_error *err)
918 if (term_type < 0 || term_type >= __PARSE_EVENTS__TERM_TYPE_NR) {
919 err->str = strdup("Invalid term_type");
922 if (!config_term_shrinked)
926 case PARSE_EVENTS__TERM_TYPE_CONFIG:
927 case PARSE_EVENTS__TERM_TYPE_CONFIG1:
928 case PARSE_EVENTS__TERM_TYPE_CONFIG2:
929 case PARSE_EVENTS__TERM_TYPE_NAME:
930 case PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD:
936 /* term_type is validated so indexing is safe */
937 if (asprintf(&err->str, "'%s' is not usable in 'perf stat'",
938 config_term_names[term_type]) < 0)
944 void parse_events__shrink_config_terms(void)
946 config_term_shrinked = true;
949 static int config_term_common(struct perf_event_attr *attr,
950 struct parse_events_term *term,
951 struct parse_events_error *err)
953 #define CHECK_TYPE_VAL(type) \
955 if (check_type_val(term, err, PARSE_EVENTS__TERM_TYPE_ ## type)) \
959 switch (term->type_term) {
960 case PARSE_EVENTS__TERM_TYPE_CONFIG:
962 attr->config = term->val.num;
964 case PARSE_EVENTS__TERM_TYPE_CONFIG1:
966 attr->config1 = term->val.num;
968 case PARSE_EVENTS__TERM_TYPE_CONFIG2:
970 attr->config2 = term->val.num;
972 case PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD:
975 case PARSE_EVENTS__TERM_TYPE_SAMPLE_FREQ:
978 case PARSE_EVENTS__TERM_TYPE_BRANCH_SAMPLE_TYPE:
980 if (strcmp(term->val.str, "no") &&
981 parse_branch_str(term->val.str, &attr->branch_sample_type)) {
982 err->str = strdup("invalid branch sample type");
983 err->idx = term->err_val;
987 case PARSE_EVENTS__TERM_TYPE_TIME:
989 if (term->val.num > 1) {
990 err->str = strdup("expected 0 or 1");
991 err->idx = term->err_val;
995 case PARSE_EVENTS__TERM_TYPE_CALLGRAPH:
998 case PARSE_EVENTS__TERM_TYPE_STACKSIZE:
1001 case PARSE_EVENTS__TERM_TYPE_INHERIT:
1002 CHECK_TYPE_VAL(NUM);
1004 case PARSE_EVENTS__TERM_TYPE_NOINHERIT:
1005 CHECK_TYPE_VAL(NUM);
1007 case PARSE_EVENTS__TERM_TYPE_OVERWRITE:
1008 CHECK_TYPE_VAL(NUM);
1010 case PARSE_EVENTS__TERM_TYPE_NOOVERWRITE:
1011 CHECK_TYPE_VAL(NUM);
1013 case PARSE_EVENTS__TERM_TYPE_NAME:
1014 CHECK_TYPE_VAL(STR);
1016 case PARSE_EVENTS__TERM_TYPE_MAX_STACK:
1017 CHECK_TYPE_VAL(NUM);
1020 err->str = strdup("unknown term");
1021 err->idx = term->err_term;
1022 err->help = parse_events_formats_error_string(NULL);
1027 * Check term availbility after basic checking so
1028 * PARSE_EVENTS__TERM_TYPE_USER can be found and filtered.
1030 * If check availbility at the entry of this function,
1031 * user will see "'<sysfs term>' is not usable in 'perf stat'"
1032 * if an invalid config term is provided for legacy events
1033 * (for example, instructions/badterm/...), which is confusing.
1035 if (!config_term_avail(term->type_term, err))
1038 #undef CHECK_TYPE_VAL
1041 static int config_term_pmu(struct perf_event_attr *attr,
1042 struct parse_events_term *term,
1043 struct parse_events_error *err)
1045 if (term->type_term == PARSE_EVENTS__TERM_TYPE_USER ||
1046 term->type_term == PARSE_EVENTS__TERM_TYPE_DRV_CFG)
1048 * Always succeed for sysfs terms, as we dont know
1049 * at this point what type they need to have.
1053 return config_term_common(attr, term, err);
1056 static int config_term_tracepoint(struct perf_event_attr *attr,
1057 struct parse_events_term *term,
1058 struct parse_events_error *err)
1060 switch (term->type_term) {
1061 case PARSE_EVENTS__TERM_TYPE_CALLGRAPH:
1062 case PARSE_EVENTS__TERM_TYPE_STACKSIZE:
1063 case PARSE_EVENTS__TERM_TYPE_INHERIT:
1064 case PARSE_EVENTS__TERM_TYPE_NOINHERIT:
1065 case PARSE_EVENTS__TERM_TYPE_MAX_STACK:
1066 case PARSE_EVENTS__TERM_TYPE_OVERWRITE:
1067 case PARSE_EVENTS__TERM_TYPE_NOOVERWRITE:
1068 return config_term_common(attr, term, err);
1071 err->idx = term->err_term;
1072 err->str = strdup("unknown term");
1073 err->help = strdup("valid terms: call-graph,stack-size\n");
1081 static int config_attr(struct perf_event_attr *attr,
1082 struct list_head *head,
1083 struct parse_events_error *err,
1084 config_term_func_t config_term)
1086 struct parse_events_term *term;
1088 list_for_each_entry(term, head, list)
1089 if (config_term(attr, term, err))
1095 static int get_config_terms(struct list_head *head_config,
1096 struct list_head *head_terms __maybe_unused)
1098 #define ADD_CONFIG_TERM(__type, __name, __val) \
1100 struct perf_evsel_config_term *__t; \
1102 __t = zalloc(sizeof(*__t)); \
1106 INIT_LIST_HEAD(&__t->list); \
1107 __t->type = PERF_EVSEL__CONFIG_TERM_ ## __type; \
1108 __t->val.__name = __val; \
1109 list_add_tail(&__t->list, head_terms); \
1112 struct parse_events_term *term;
1114 list_for_each_entry(term, head_config, list) {
1115 switch (term->type_term) {
1116 case PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD:
1117 ADD_CONFIG_TERM(PERIOD, period, term->val.num);
1119 case PARSE_EVENTS__TERM_TYPE_SAMPLE_FREQ:
1120 ADD_CONFIG_TERM(FREQ, freq, term->val.num);
1122 case PARSE_EVENTS__TERM_TYPE_TIME:
1123 ADD_CONFIG_TERM(TIME, time, term->val.num);
1125 case PARSE_EVENTS__TERM_TYPE_CALLGRAPH:
1126 ADD_CONFIG_TERM(CALLGRAPH, callgraph, term->val.str);
1128 case PARSE_EVENTS__TERM_TYPE_BRANCH_SAMPLE_TYPE:
1129 ADD_CONFIG_TERM(BRANCH, branch, term->val.str);
1131 case PARSE_EVENTS__TERM_TYPE_STACKSIZE:
1132 ADD_CONFIG_TERM(STACK_USER, stack_user, term->val.num);
1134 case PARSE_EVENTS__TERM_TYPE_INHERIT:
1135 ADD_CONFIG_TERM(INHERIT, inherit, term->val.num ? 1 : 0);
1137 case PARSE_EVENTS__TERM_TYPE_NOINHERIT:
1138 ADD_CONFIG_TERM(INHERIT, inherit, term->val.num ? 0 : 1);
1140 case PARSE_EVENTS__TERM_TYPE_MAX_STACK:
1141 ADD_CONFIG_TERM(MAX_STACK, max_stack, term->val.num);
1143 case PARSE_EVENTS__TERM_TYPE_OVERWRITE:
1144 ADD_CONFIG_TERM(OVERWRITE, overwrite, term->val.num ? 1 : 0);
1146 case PARSE_EVENTS__TERM_TYPE_NOOVERWRITE:
1147 ADD_CONFIG_TERM(OVERWRITE, overwrite, term->val.num ? 0 : 1);
1149 case PARSE_EVENTS__TERM_TYPE_DRV_CFG:
1150 ADD_CONFIG_TERM(DRV_CFG, drv_cfg, term->val.str);
1156 #undef ADD_EVSEL_CONFIG
1160 int parse_events_add_tracepoint(struct list_head *list, int *idx,
1161 const char *sys, const char *event,
1162 struct parse_events_error *err,
1163 struct list_head *head_config)
1166 struct perf_event_attr attr;
1168 if (config_attr(&attr, head_config, err,
1169 config_term_tracepoint))
1173 if (strpbrk(sys, "*?"))
1174 return add_tracepoint_multi_sys(list, idx, sys, event,
1177 return add_tracepoint_event(list, idx, sys, event,
1181 int parse_events_add_numeric(struct parse_events_evlist *data,
1182 struct list_head *list,
1183 u32 type, u64 config,
1184 struct list_head *head_config)
1186 struct perf_event_attr attr;
1187 LIST_HEAD(config_terms);
1189 memset(&attr, 0, sizeof(attr));
1191 attr.config = config;
1194 if (config_attr(&attr, head_config, data->error,
1195 config_term_common))
1198 if (get_config_terms(head_config, &config_terms))
1202 return add_event(list, &data->idx, &attr,
1203 get_config_name(head_config), &config_terms);
1206 int parse_events_add_pmu(struct parse_events_evlist *data,
1207 struct list_head *list, char *name,
1208 struct list_head *head_config)
1210 struct perf_event_attr attr;
1211 struct perf_pmu_info info;
1212 struct perf_pmu *pmu;
1213 struct perf_evsel *evsel;
1214 LIST_HEAD(config_terms);
1216 pmu = perf_pmu__find(name);
1220 if (pmu->default_config) {
1221 memcpy(&attr, pmu->default_config,
1222 sizeof(struct perf_event_attr));
1224 memset(&attr, 0, sizeof(attr));
1228 attr.type = pmu->type;
1229 evsel = __add_event(list, &data->idx, &attr, NULL, pmu->cpus, NULL);
1230 return evsel ? 0 : -ENOMEM;
1233 if (perf_pmu__check_alias(pmu, head_config, &info))
1237 * Configure hardcoded terms first, no need to check
1238 * return value when called with fail == 0 ;)
1240 if (config_attr(&attr, head_config, data->error, config_term_pmu))
1243 if (get_config_terms(head_config, &config_terms))
1246 if (perf_pmu__config(pmu, &attr, head_config, data->error))
1249 evsel = __add_event(list, &data->idx, &attr,
1250 get_config_name(head_config), pmu->cpus,
1253 evsel->unit = info.unit;
1254 evsel->scale = info.scale;
1255 evsel->per_pkg = info.per_pkg;
1256 evsel->snapshot = info.snapshot;
1259 return evsel ? 0 : -ENOMEM;
1262 int parse_events__modifier_group(struct list_head *list,
1265 return parse_events__modifier_event(list, event_mod, true);
1268 void parse_events__set_leader(char *name, struct list_head *list)
1270 struct perf_evsel *leader;
1272 if (list_empty(list)) {
1273 WARN_ONCE(true, "WARNING: failed to set leader: empty list");
1277 __perf_evlist__set_leader(list);
1278 leader = list_entry(list->next, struct perf_evsel, node);
1279 leader->group_name = name ? strdup(name) : NULL;
1282 /* list_event is assumed to point to malloc'ed memory */
1283 void parse_events_update_lists(struct list_head *list_event,
1284 struct list_head *list_all)
1287 * Called for single event definition. Update the
1288 * 'all event' list, and reinit the 'single event'
1289 * list, for next event definition.
1291 list_splice_tail(list_event, list_all);
1295 struct event_modifier {
1309 static int get_event_modifier(struct event_modifier *mod, char *str,
1310 struct perf_evsel *evsel)
1312 int eu = evsel ? evsel->attr.exclude_user : 0;
1313 int ek = evsel ? evsel->attr.exclude_kernel : 0;
1314 int eh = evsel ? evsel->attr.exclude_hv : 0;
1315 int eH = evsel ? evsel->attr.exclude_host : 0;
1316 int eG = evsel ? evsel->attr.exclude_guest : 0;
1317 int eI = evsel ? evsel->attr.exclude_idle : 0;
1318 int precise = evsel ? evsel->attr.precise_ip : 0;
1319 int precise_max = 0;
1320 int sample_read = 0;
1321 int pinned = evsel ? evsel->attr.pinned : 0;
1323 int exclude = eu | ek | eh;
1324 int exclude_GH = evsel ? evsel->exclude_GH : 0;
1326 memset(mod, 0, sizeof(*mod));
1331 exclude = eu = ek = eh = 1;
1333 } else if (*str == 'k') {
1335 exclude = eu = ek = eh = 1;
1337 } else if (*str == 'h') {
1339 exclude = eu = ek = eh = 1;
1341 } else if (*str == 'G') {
1343 exclude_GH = eG = eH = 1;
1345 } else if (*str == 'H') {
1347 exclude_GH = eG = eH = 1;
1349 } else if (*str == 'I') {
1351 } else if (*str == 'p') {
1353 /* use of precise requires exclude_guest */
1356 } else if (*str == 'P') {
1358 } else if (*str == 'S') {
1360 } else if (*str == 'D') {
1371 * 0 - SAMPLE_IP can have arbitrary skid
1372 * 1 - SAMPLE_IP must have constant skid
1373 * 2 - SAMPLE_IP requested to have 0 skid
1374 * 3 - SAMPLE_IP must have 0 skid
1376 * See also PERF_RECORD_MISC_EXACT_IP
1387 mod->precise = precise;
1388 mod->precise_max = precise_max;
1389 mod->exclude_GH = exclude_GH;
1390 mod->sample_read = sample_read;
1391 mod->pinned = pinned;
1397 * Basic modifier sanity check to validate it contains only one
1398 * instance of any modifier (apart from 'p') present.
1400 static int check_modifier(char *str)
1404 /* The sizeof includes 0 byte as well. */
1405 if (strlen(str) > (sizeof("ukhGHpppPSDI") - 1))
1409 if (*p != 'p' && strchr(p + 1, *p))
1417 int parse_events__modifier_event(struct list_head *list, char *str, bool add)
1419 struct perf_evsel *evsel;
1420 struct event_modifier mod;
1425 if (check_modifier(str))
1428 if (!add && get_event_modifier(&mod, str, NULL))
1431 __evlist__for_each_entry(list, evsel) {
1432 if (add && get_event_modifier(&mod, str, evsel))
1435 evsel->attr.exclude_user = mod.eu;
1436 evsel->attr.exclude_kernel = mod.ek;
1437 evsel->attr.exclude_hv = mod.eh;
1438 evsel->attr.precise_ip = mod.precise;
1439 evsel->attr.exclude_host = mod.eH;
1440 evsel->attr.exclude_guest = mod.eG;
1441 evsel->attr.exclude_idle = mod.eI;
1442 evsel->exclude_GH = mod.exclude_GH;
1443 evsel->sample_read = mod.sample_read;
1444 evsel->precise_max = mod.precise_max;
1446 if (perf_evsel__is_group_leader(evsel))
1447 evsel->attr.pinned = mod.pinned;
1453 int parse_events_name(struct list_head *list, char *name)
1455 struct perf_evsel *evsel;
1457 __evlist__for_each_entry(list, evsel) {
1459 evsel->name = strdup(name);
1466 comp_pmu(const void *p1, const void *p2)
1468 struct perf_pmu_event_symbol *pmu1 = (struct perf_pmu_event_symbol *) p1;
1469 struct perf_pmu_event_symbol *pmu2 = (struct perf_pmu_event_symbol *) p2;
1471 return strcasecmp(pmu1->symbol, pmu2->symbol);
1474 static void perf_pmu__parse_cleanup(void)
1476 if (perf_pmu_events_list_num > 0) {
1477 struct perf_pmu_event_symbol *p;
1480 for (i = 0; i < perf_pmu_events_list_num; i++) {
1481 p = perf_pmu_events_list + i;
1484 zfree(&perf_pmu_events_list);
1485 perf_pmu_events_list_num = 0;
1489 #define SET_SYMBOL(str, stype) \
1498 * Read the pmu events list from sysfs
1499 * Save it into perf_pmu_events_list
1501 static void perf_pmu__parse_init(void)
1504 struct perf_pmu *pmu = NULL;
1505 struct perf_pmu_alias *alias;
1509 while ((pmu = perf_pmu__scan(pmu)) != NULL) {
1510 list_for_each_entry(alias, &pmu->aliases, list) {
1511 if (strchr(alias->name, '-'))
1518 perf_pmu_events_list_num = -1;
1521 perf_pmu_events_list = malloc(sizeof(struct perf_pmu_event_symbol) * len);
1522 if (!perf_pmu_events_list)
1524 perf_pmu_events_list_num = len;
1528 while ((pmu = perf_pmu__scan(pmu)) != NULL) {
1529 list_for_each_entry(alias, &pmu->aliases, list) {
1530 struct perf_pmu_event_symbol *p = perf_pmu_events_list + len;
1531 char *tmp = strchr(alias->name, '-');
1534 SET_SYMBOL(strndup(alias->name, tmp - alias->name),
1535 PMU_EVENT_SYMBOL_PREFIX);
1537 SET_SYMBOL(strdup(++tmp), PMU_EVENT_SYMBOL_SUFFIX);
1540 SET_SYMBOL(strdup(alias->name), PMU_EVENT_SYMBOL);
1545 qsort(perf_pmu_events_list, len,
1546 sizeof(struct perf_pmu_event_symbol), comp_pmu);
1550 perf_pmu__parse_cleanup();
1553 enum perf_pmu_event_symbol_type
1554 perf_pmu__parse_check(const char *name)
1556 struct perf_pmu_event_symbol p, *r;
1558 /* scan kernel pmu events from sysfs if needed */
1559 if (perf_pmu_events_list_num == 0)
1560 perf_pmu__parse_init();
1562 * name "cpu" could be prefix of cpu-cycles or cpu// events.
1563 * cpu-cycles has been handled by hardcode.
1564 * So it must be cpu// events, not kernel pmu event.
1566 if ((perf_pmu_events_list_num <= 0) || !strcmp(name, "cpu"))
1567 return PMU_EVENT_SYMBOL_ERR;
1569 p.symbol = strdup(name);
1570 r = bsearch(&p, perf_pmu_events_list,
1571 (size_t) perf_pmu_events_list_num,
1572 sizeof(struct perf_pmu_event_symbol), comp_pmu);
1574 return r ? r->type : PMU_EVENT_SYMBOL_ERR;
1577 static int parse_events__scanner(const char *str, void *data, int start_token)
1579 YY_BUFFER_STATE buffer;
1583 ret = parse_events_lex_init_extra(start_token, &scanner);
1587 buffer = parse_events__scan_string(str, scanner);
1590 parse_events_debug = 1;
1592 ret = parse_events_parse(data, scanner);
1594 parse_events__flush_buffer(buffer, scanner);
1595 parse_events__delete_buffer(buffer, scanner);
1596 parse_events_lex_destroy(scanner);
1601 * parse event config string, return a list of event terms.
1603 int parse_events_terms(struct list_head *terms, const char *str)
1605 struct parse_events_terms data = {
1610 ret = parse_events__scanner(str, &data, PE_START_TERMS);
1612 list_splice(data.terms, terms);
1617 parse_events_terms__delete(data.terms);
1621 int parse_events(struct perf_evlist *evlist, const char *str,
1622 struct parse_events_error *err)
1624 struct parse_events_evlist data = {
1625 .list = LIST_HEAD_INIT(data.list),
1626 .idx = evlist->nr_entries,
1632 ret = parse_events__scanner(str, &data, PE_START_EVENTS);
1633 perf_pmu__parse_cleanup();
1635 struct perf_evsel *last;
1637 if (list_empty(&data.list)) {
1638 WARN_ONCE(true, "WARNING: event parser found nothing");
1642 perf_evlist__splice_list_tail(evlist, &data.list);
1643 evlist->nr_groups += data.nr_groups;
1644 last = perf_evlist__last(evlist);
1645 last->cmdline_group_boundary = true;
1651 * There are 2 users - builtin-record and builtin-test objects.
1652 * Both call perf_evlist__delete in case of error, so we dont
1658 #define MAX_WIDTH 1000
1659 static int get_term_width(void)
1663 get_term_dimensions(&ws);
1664 return ws.ws_col > MAX_WIDTH ? MAX_WIDTH : ws.ws_col;
1667 static void parse_events_print_error(struct parse_events_error *err,
1670 const char *str = "invalid or unsupported event: ";
1671 char _buf[MAX_WIDTH];
1672 char *buf = (char *) event;
1676 /* -2 for extra '' in the final fprintf */
1677 int width = get_term_width() - 2;
1678 int len_event = strlen(event);
1679 int len_str, max_len, cut = 0;
1682 * Maximum error index indent, we will cut
1683 * the event string if it's bigger.
1685 int max_err_idx = 13;
1688 * Let's be specific with the message when
1689 * we have the precise error.
1691 str = "event syntax error: ";
1692 len_str = strlen(str);
1693 max_len = width - len_str;
1697 /* We're cutting from the beginning. */
1698 if (err->idx > max_err_idx)
1699 cut = err->idx - max_err_idx;
1701 strncpy(buf, event + cut, max_len);
1703 /* Mark cut parts with '..' on both sides. */
1705 buf[0] = buf[1] = '.';
1707 if ((len_event - cut) > max_len) {
1708 buf[max_len - 1] = buf[max_len - 2] = '.';
1712 idx = len_str + err->idx - cut;
1715 fprintf(stderr, "%s'%s'\n", str, buf);
1717 fprintf(stderr, "%*s\\___ %s\n", idx + 1, "", err->str);
1719 fprintf(stderr, "\n%s\n", err->help);
1724 fprintf(stderr, "Run 'perf list' for a list of valid events\n");
1729 int parse_events_option(const struct option *opt, const char *str,
1730 int unset __maybe_unused)
1732 struct perf_evlist *evlist = *(struct perf_evlist **)opt->value;
1733 struct parse_events_error err = { .idx = 0, };
1734 int ret = parse_events(evlist, str, &err);
1737 parse_events_print_error(&err, str);
1743 foreach_evsel_in_last_glob(struct perf_evlist *evlist,
1744 int (*func)(struct perf_evsel *evsel,
1748 struct perf_evsel *last = NULL;
1752 * Don't return when list_empty, give func a chance to report
1753 * error when it found last == NULL.
1755 * So no need to WARN here, let *func do this.
1757 if (evlist->nr_entries > 0)
1758 last = perf_evlist__last(evlist);
1761 err = (*func)(last, arg);
1767 if (last->node.prev == &evlist->entries)
1769 last = list_entry(last->node.prev, struct perf_evsel, node);
1770 } while (!last->cmdline_group_boundary);
1775 static int set_filter(struct perf_evsel *evsel, const void *arg)
1777 const char *str = arg;
1779 int nr_addr_filters = 0;
1780 struct perf_pmu *pmu = NULL;
1785 if (evsel->attr.type == PERF_TYPE_TRACEPOINT) {
1786 if (perf_evsel__append_tp_filter(evsel, str) < 0) {
1788 "not enough memory to hold filter string\n");
1795 while ((pmu = perf_pmu__scan(pmu)) != NULL)
1796 if (pmu->type == evsel->attr.type) {
1802 perf_pmu__scan_file(pmu, "nr_addr_filters",
1803 "%d", &nr_addr_filters);
1805 if (!nr_addr_filters)
1808 if (perf_evsel__append_addr_filter(evsel, str) < 0) {
1810 "not enough memory to hold filter string\n");
1818 "--filter option should follow a -e tracepoint or HW tracer option\n");
1823 int parse_filter(const struct option *opt, const char *str,
1824 int unset __maybe_unused)
1826 struct perf_evlist *evlist = *(struct perf_evlist **)opt->value;
1828 return foreach_evsel_in_last_glob(evlist, set_filter,
1832 static int add_exclude_perf_filter(struct perf_evsel *evsel,
1833 const void *arg __maybe_unused)
1835 char new_filter[64];
1837 if (evsel == NULL || evsel->attr.type != PERF_TYPE_TRACEPOINT) {
1839 "--exclude-perf option should follow a -e tracepoint option\n");
1843 snprintf(new_filter, sizeof(new_filter), "common_pid != %d", getpid());
1845 if (perf_evsel__append_tp_filter(evsel, new_filter) < 0) {
1847 "not enough memory to hold filter string\n");
1854 int exclude_perf(const struct option *opt,
1855 const char *arg __maybe_unused,
1856 int unset __maybe_unused)
1858 struct perf_evlist *evlist = *(struct perf_evlist **)opt->value;
1860 return foreach_evsel_in_last_glob(evlist, add_exclude_perf_filter,
1864 static const char * const event_type_descriptors[] = {
1868 "Hardware cache event",
1869 "Raw hardware event descriptor",
1870 "Hardware breakpoint",
1873 static int cmp_string(const void *a, const void *b)
1875 const char * const *as = a;
1876 const char * const *bs = b;
1878 return strcmp(*as, *bs);
1882 * Print the events from <debugfs_mount_point>/tracing/events
1885 void print_tracepoint_events(const char *subsys_glob, const char *event_glob,
1888 DIR *sys_dir, *evt_dir;
1889 struct dirent *sys_dirent, *evt_dirent;
1890 char evt_path[MAXPATHLEN];
1891 char dir_path[MAXPATHLEN];
1892 char **evt_list = NULL;
1893 unsigned int evt_i = 0, evt_num = 0;
1894 bool evt_num_known = false;
1897 sys_dir = opendir(tracing_events_path);
1901 if (evt_num_known) {
1902 evt_list = zalloc(sizeof(char *) * evt_num);
1904 goto out_close_sys_dir;
1907 for_each_subsystem(sys_dir, sys_dirent) {
1908 if (subsys_glob != NULL &&
1909 !strglobmatch(sys_dirent->d_name, subsys_glob))
1912 snprintf(dir_path, MAXPATHLEN, "%s/%s", tracing_events_path,
1913 sys_dirent->d_name);
1914 evt_dir = opendir(dir_path);
1918 for_each_event(sys_dirent, evt_dir, evt_dirent) {
1919 if (event_glob != NULL &&
1920 !strglobmatch(evt_dirent->d_name, event_glob))
1923 if (!evt_num_known) {
1928 snprintf(evt_path, MAXPATHLEN, "%s:%s",
1929 sys_dirent->d_name, evt_dirent->d_name);
1931 evt_list[evt_i] = strdup(evt_path);
1932 if (evt_list[evt_i] == NULL)
1933 goto out_close_evt_dir;
1940 if (!evt_num_known) {
1941 evt_num_known = true;
1944 qsort(evt_list, evt_num, sizeof(char *), cmp_string);
1946 while (evt_i < evt_num) {
1948 printf("%s ", evt_list[evt_i++]);
1951 printf(" %-50s [%s]\n", evt_list[evt_i++],
1952 event_type_descriptors[PERF_TYPE_TRACEPOINT]);
1954 if (evt_num && pager_in_use())
1959 for (evt_i = 0; evt_i < evt_num; evt_i++)
1960 zfree(&evt_list[evt_i]);
1969 printf("FATAL: not enough memory to print %s\n",
1970 event_type_descriptors[PERF_TYPE_TRACEPOINT]);
1976 * Check whether event is in <debugfs_mount_point>/tracing/events
1979 int is_valid_tracepoint(const char *event_string)
1981 DIR *sys_dir, *evt_dir;
1982 struct dirent *sys_dirent, *evt_dirent;
1983 char evt_path[MAXPATHLEN];
1984 char dir_path[MAXPATHLEN];
1986 sys_dir = opendir(tracing_events_path);
1990 for_each_subsystem(sys_dir, sys_dirent) {
1992 snprintf(dir_path, MAXPATHLEN, "%s/%s", tracing_events_path,
1993 sys_dirent->d_name);
1994 evt_dir = opendir(dir_path);
1998 for_each_event(sys_dirent, evt_dir, evt_dirent) {
1999 snprintf(evt_path, MAXPATHLEN, "%s:%s",
2000 sys_dirent->d_name, evt_dirent->d_name);
2001 if (!strcmp(evt_path, event_string)) {
2013 static bool is_event_supported(u8 type, unsigned config)
2017 struct perf_evsel *evsel;
2018 struct perf_event_attr attr = {
2023 struct thread_map *tmap = thread_map__new_by_tid(0);
2028 evsel = perf_evsel__new(&attr);
2030 open_return = perf_evsel__open(evsel, NULL, tmap);
2031 ret = open_return >= 0;
2033 if (open_return == -EACCES) {
2035 * This happens if the paranoid value
2036 * /proc/sys/kernel/perf_event_paranoid is set to 2
2037 * Re-run with exclude_kernel set; we don't do that
2038 * by default as some ARM machines do not support it.
2041 evsel->attr.exclude_kernel = 1;
2042 ret = perf_evsel__open(evsel, NULL, tmap) >= 0;
2044 perf_evsel__delete(evsel);
2050 void print_sdt_events(const char *subsys_glob, const char *event_glob,
2053 struct probe_cache *pcache;
2054 struct probe_cache_entry *ent;
2055 struct strlist *bidlist, *sdtlist;
2056 struct strlist_config cfg = {.dont_dupstr = true};
2057 struct str_node *nd, *nd2;
2058 char *buf, *path, *ptr = NULL;
2059 bool show_detail = false;
2062 sdtlist = strlist__new(NULL, &cfg);
2064 pr_debug("Failed to allocate new strlist for SDT\n");
2067 bidlist = build_id_cache__list_all(true);
2069 pr_debug("Failed to get buildids: %d\n", errno);
2072 strlist__for_each_entry(nd, bidlist) {
2073 pcache = probe_cache__new(nd->s);
2076 list_for_each_entry(ent, &pcache->entries, node) {
2080 !strglobmatch(ent->pev.group, subsys_glob))
2083 !strglobmatch(ent->pev.event, event_glob))
2085 ret = asprintf(&buf, "%s:%s@%s", ent->pev.group,
2086 ent->pev.event, nd->s);
2088 strlist__add(sdtlist, buf);
2090 probe_cache__delete(pcache);
2092 strlist__delete(bidlist);
2094 strlist__for_each_entry(nd, sdtlist) {
2095 buf = strchr(nd->s, '@');
2099 printf("%s ", nd->s);
2102 nd2 = strlist__next(nd);
2104 ptr = strchr(nd2->s, '@');
2107 if (strcmp(nd->s, nd2->s) == 0)
2111 path = build_id_cache__origname(buf);
2112 ret = asprintf(&buf, "%s@%s(%.12s)", nd->s, path, buf);
2114 printf(" %-50s [%s]\n", buf, "SDT event");
2118 printf(" %-50s [%s]\n", nd->s, "SDT event");
2120 if (strcmp(nd->s, nd2->s) != 0)
2121 show_detail = false;
2126 strlist__delete(sdtlist);
2129 int print_hwcache_events(const char *event_glob, bool name_only)
2131 unsigned int type, op, i, evt_i = 0, evt_num = 0;
2133 char **evt_list = NULL;
2134 bool evt_num_known = false;
2137 if (evt_num_known) {
2138 evt_list = zalloc(sizeof(char *) * evt_num);
2143 for (type = 0; type < PERF_COUNT_HW_CACHE_MAX; type++) {
2144 for (op = 0; op < PERF_COUNT_HW_CACHE_OP_MAX; op++) {
2145 /* skip invalid cache type */
2146 if (!perf_evsel__is_cache_op_valid(type, op))
2149 for (i = 0; i < PERF_COUNT_HW_CACHE_RESULT_MAX; i++) {
2150 __perf_evsel__hw_cache_type_op_res_name(type, op, i,
2151 name, sizeof(name));
2152 if (event_glob != NULL && !strglobmatch(name, event_glob))
2155 if (!is_event_supported(PERF_TYPE_HW_CACHE,
2156 type | (op << 8) | (i << 16)))
2159 if (!evt_num_known) {
2164 evt_list[evt_i] = strdup(name);
2165 if (evt_list[evt_i] == NULL)
2172 if (!evt_num_known) {
2173 evt_num_known = true;
2176 qsort(evt_list, evt_num, sizeof(char *), cmp_string);
2178 while (evt_i < evt_num) {
2180 printf("%s ", evt_list[evt_i++]);
2183 printf(" %-50s [%s]\n", evt_list[evt_i++],
2184 event_type_descriptors[PERF_TYPE_HW_CACHE]);
2186 if (evt_num && pager_in_use())
2191 for (evt_i = 0; evt_i < evt_num; evt_i++)
2192 zfree(&evt_list[evt_i]);
2197 printf("FATAL: not enough memory to print %s\n", event_type_descriptors[PERF_TYPE_HW_CACHE]);
2203 void print_symbol_events(const char *event_glob, unsigned type,
2204 struct event_symbol *syms, unsigned max,
2207 unsigned int i, evt_i = 0, evt_num = 0;
2208 char name[MAX_NAME_LEN];
2209 char **evt_list = NULL;
2210 bool evt_num_known = false;
2213 if (evt_num_known) {
2214 evt_list = zalloc(sizeof(char *) * evt_num);
2220 for (i = 0; i < max; i++, syms++) {
2222 if (event_glob != NULL && syms->symbol != NULL &&
2223 !(strglobmatch(syms->symbol, event_glob) ||
2224 (syms->alias && strglobmatch(syms->alias, event_glob))))
2227 if (!is_event_supported(type, i))
2230 if (!evt_num_known) {
2235 if (!name_only && strlen(syms->alias))
2236 snprintf(name, MAX_NAME_LEN, "%s OR %s", syms->symbol, syms->alias);
2238 strncpy(name, syms->symbol, MAX_NAME_LEN);
2240 evt_list[evt_i] = strdup(name);
2241 if (evt_list[evt_i] == NULL)
2246 if (!evt_num_known) {
2247 evt_num_known = true;
2250 qsort(evt_list, evt_num, sizeof(char *), cmp_string);
2252 while (evt_i < evt_num) {
2254 printf("%s ", evt_list[evt_i++]);
2257 printf(" %-50s [%s]\n", evt_list[evt_i++], event_type_descriptors[type]);
2259 if (evt_num && pager_in_use())
2264 for (evt_i = 0; evt_i < evt_num; evt_i++)
2265 zfree(&evt_list[evt_i]);
2270 printf("FATAL: not enough memory to print %s\n", event_type_descriptors[type]);
2276 * Print the help text for the event symbols:
2278 void print_events(const char *event_glob, bool name_only, bool quiet_flag,
2281 print_symbol_events(event_glob, PERF_TYPE_HARDWARE,
2282 event_symbols_hw, PERF_COUNT_HW_MAX, name_only);
2284 print_symbol_events(event_glob, PERF_TYPE_SOFTWARE,
2285 event_symbols_sw, PERF_COUNT_SW_MAX, name_only);
2287 print_hwcache_events(event_glob, name_only);
2289 print_pmu_events(event_glob, name_only, quiet_flag, long_desc);
2291 if (event_glob != NULL)
2295 printf(" %-50s [%s]\n",
2297 event_type_descriptors[PERF_TYPE_RAW]);
2298 printf(" %-50s [%s]\n",
2299 "cpu/t1=v1[,t2=v2,t3 ...]/modifier",
2300 event_type_descriptors[PERF_TYPE_RAW]);
2302 printf(" (see 'man perf-list' on how to encode it)\n\n");
2304 printf(" %-50s [%s]\n",
2305 "mem:<addr>[/len][:access]",
2306 event_type_descriptors[PERF_TYPE_BREAKPOINT]);
2311 print_tracepoint_events(NULL, NULL, name_only);
2313 print_sdt_events(NULL, NULL, name_only);
2316 int parse_events__is_hardcoded_term(struct parse_events_term *term)
2318 return term->type_term != PARSE_EVENTS__TERM_TYPE_USER;
2321 static int new_term(struct parse_events_term **_term,
2322 struct parse_events_term *temp,
2325 struct parse_events_term *term;
2327 term = malloc(sizeof(*term));
2332 INIT_LIST_HEAD(&term->list);
2334 switch (term->type_val) {
2335 case PARSE_EVENTS__TERM_TYPE_NUM:
2336 term->val.num = num;
2338 case PARSE_EVENTS__TERM_TYPE_STR:
2339 term->val.str = str;
2350 int parse_events_term__num(struct parse_events_term **term,
2351 int type_term, char *config, u64 num,
2353 void *loc_term_, void *loc_val_)
2355 YYLTYPE *loc_term = loc_term_;
2356 YYLTYPE *loc_val = loc_val_;
2358 struct parse_events_term temp = {
2359 .type_val = PARSE_EVENTS__TERM_TYPE_NUM,
2360 .type_term = type_term,
2362 .no_value = no_value,
2363 .err_term = loc_term ? loc_term->first_column : 0,
2364 .err_val = loc_val ? loc_val->first_column : 0,
2367 return new_term(term, &temp, NULL, num);
2370 int parse_events_term__str(struct parse_events_term **term,
2371 int type_term, char *config, char *str,
2372 void *loc_term_, void *loc_val_)
2374 YYLTYPE *loc_term = loc_term_;
2375 YYLTYPE *loc_val = loc_val_;
2377 struct parse_events_term temp = {
2378 .type_val = PARSE_EVENTS__TERM_TYPE_STR,
2379 .type_term = type_term,
2381 .err_term = loc_term ? loc_term->first_column : 0,
2382 .err_val = loc_val ? loc_val->first_column : 0,
2385 return new_term(term, &temp, str, 0);
2388 int parse_events_term__sym_hw(struct parse_events_term **term,
2389 char *config, unsigned idx)
2391 struct event_symbol *sym;
2392 struct parse_events_term temp = {
2393 .type_val = PARSE_EVENTS__TERM_TYPE_STR,
2394 .type_term = PARSE_EVENTS__TERM_TYPE_USER,
2395 .config = config ?: (char *) "event",
2398 BUG_ON(idx >= PERF_COUNT_HW_MAX);
2399 sym = &event_symbols_hw[idx];
2401 return new_term(term, &temp, (char *) sym->symbol, 0);
2404 int parse_events_term__clone(struct parse_events_term **new,
2405 struct parse_events_term *term)
2407 struct parse_events_term temp = {
2408 .type_val = term->type_val,
2409 .type_term = term->type_term,
2410 .config = term->config,
2411 .err_term = term->err_term,
2412 .err_val = term->err_val,
2415 return new_term(new, &temp, term->val.str, term->val.num);
2418 void parse_events_terms__purge(struct list_head *terms)
2420 struct parse_events_term *term, *h;
2422 list_for_each_entry_safe(term, h, terms, list) {
2423 if (term->array.nr_ranges)
2424 zfree(&term->array.ranges);
2425 list_del_init(&term->list);
2430 void parse_events_terms__delete(struct list_head *terms)
2434 parse_events_terms__purge(terms);
2438 void parse_events__clear_array(struct parse_events_array *a)
2443 void parse_events_evlist_error(struct parse_events_evlist *data,
2444 int idx, const char *str)
2446 struct parse_events_error *err = data->error;
2451 err->str = strdup(str);
2452 WARN_ONCE(!err->str, "WARNING: failed to allocate error string");
2455 static void config_terms_list(char *buf, size_t buf_sz)
2461 for (i = 0; i < __PARSE_EVENTS__TERM_TYPE_NR; i++) {
2462 const char *name = config_term_names[i];
2464 if (!config_term_avail(i, NULL))
2471 if (strlen(buf) + strlen(name) + 2 >= buf_sz)
2483 * Return string contains valid config terms of an event.
2484 * @additional_terms: For terms such as PMU sysfs terms.
2486 char *parse_events_formats_error_string(char *additional_terms)
2489 /* "no-overwrite" is the longest name */
2490 char static_terms[__PARSE_EVENTS__TERM_TYPE_NR *
2491 (sizeof("no-overwrite") - 1)];
2493 config_terms_list(static_terms, sizeof(static_terms));
2495 if (additional_terms) {
2496 if (asprintf(&str, "valid terms: %s,%s",
2497 additional_terms, static_terms) < 0)
2500 if (asprintf(&str, "valid terms: %s", static_terms) < 0)