1 #include <linux/hw_breakpoint.h>
11 #include <subcmd/parse-options.h>
12 #include "parse-events.h"
13 #include <subcmd/exec-cmd.h>
19 #include "bpf-loader.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"
26 #include "thread_map.h"
28 #include "probe-file.h"
30 #include "util/parse-branch-options.h"
32 #define MAX_NAME_LEN 100
35 extern int parse_events_debug;
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);
41 static struct perf_pmu_event_symbol *perf_pmu_events_list;
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
48 static int perf_pmu_events_list_num;
50 struct event_symbol event_symbols_hw[PERF_COUNT_HW_MAX] = {
51 [PERF_COUNT_HW_CPU_CYCLES] = {
52 .symbol = "cpu-cycles",
55 [PERF_COUNT_HW_INSTRUCTIONS] = {
56 .symbol = "instructions",
59 [PERF_COUNT_HW_CACHE_REFERENCES] = {
60 .symbol = "cache-references",
63 [PERF_COUNT_HW_CACHE_MISSES] = {
64 .symbol = "cache-misses",
67 [PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = {
68 .symbol = "branch-instructions",
71 [PERF_COUNT_HW_BRANCH_MISSES] = {
72 .symbol = "branch-misses",
75 [PERF_COUNT_HW_BUS_CYCLES] = {
76 .symbol = "bus-cycles",
79 [PERF_COUNT_HW_STALLED_CYCLES_FRONTEND] = {
80 .symbol = "stalled-cycles-frontend",
81 .alias = "idle-cycles-frontend",
83 [PERF_COUNT_HW_STALLED_CYCLES_BACKEND] = {
84 .symbol = "stalled-cycles-backend",
85 .alias = "idle-cycles-backend",
87 [PERF_COUNT_HW_REF_CPU_CYCLES] = {
88 .symbol = "ref-cycles",
93 struct event_symbol event_symbols_sw[PERF_COUNT_SW_MAX] = {
94 [PERF_COUNT_SW_CPU_CLOCK] = {
95 .symbol = "cpu-clock",
98 [PERF_COUNT_SW_TASK_CLOCK] = {
99 .symbol = "task-clock",
102 [PERF_COUNT_SW_PAGE_FAULTS] = {
103 .symbol = "page-faults",
106 [PERF_COUNT_SW_CONTEXT_SWITCHES] = {
107 .symbol = "context-switches",
110 [PERF_COUNT_SW_CPU_MIGRATIONS] = {
111 .symbol = "cpu-migrations",
112 .alias = "migrations",
114 [PERF_COUNT_SW_PAGE_FAULTS_MIN] = {
115 .symbol = "minor-faults",
118 [PERF_COUNT_SW_PAGE_FAULTS_MAJ] = {
119 .symbol = "major-faults",
122 [PERF_COUNT_SW_ALIGNMENT_FAULTS] = {
123 .symbol = "alignment-faults",
126 [PERF_COUNT_SW_EMULATION_FAULTS] = {
127 .symbol = "emulation-faults",
130 [PERF_COUNT_SW_DUMMY] = {
134 [PERF_COUNT_SW_BPF_OUTPUT] = {
135 .symbol = "bpf-output",
140 #define __PERF_EVENT_FIELD(config, name) \
141 ((config & PERF_EVENT_##name##_MASK) >> PERF_EVENT_##name##_SHIFT)
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)
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, "..")))
154 static int tp_event_has_id(struct dirent *sys_dir, struct dirent *evt_dir)
156 char evt_path[MAXPATHLEN];
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);
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)))
176 #define MAX_EVENT_LENGTH 512
179 struct tracepoint_path *tracepoint_id_to_path(u64 config)
181 struct tracepoint_path *path = NULL;
182 DIR *sys_dir, *evt_dir;
183 struct dirent *sys_dirent, *evt_dirent;
187 char evt_path[MAXPATHLEN];
188 char dir_path[MAXPATHLEN];
190 sys_dir = opendir(tracing_events_path);
194 for_each_subsystem(sys_dir, sys_dirent) {
196 snprintf(dir_path, MAXPATHLEN, "%s/%s", tracing_events_path,
198 evt_dir = opendir(dir_path);
202 for_each_event(sys_dirent, evt_dir, evt_dirent) {
204 snprintf(evt_path, MAXPATHLEN, "%s/%s/id", dir_path,
206 fd = open(evt_path, O_RDONLY);
209 if (read(fd, id_buf, sizeof(id_buf)) < 0) {
218 path = zalloc(sizeof(*path));
221 path->system = malloc(MAX_EVENT_LENGTH);
226 path->name = malloc(MAX_EVENT_LENGTH);
228 zfree(&path->system);
232 strncpy(path->system, sys_dirent->d_name,
234 strncpy(path->name, evt_dirent->d_name,
246 struct tracepoint_path *tracepoint_name_to_path(const char *name)
248 struct tracepoint_path *path = zalloc(sizeof(*path));
249 char *str = strchr(name, ':');
251 if (path == NULL || str == NULL) {
256 path->system = strndup(name, str - name);
257 path->name = strdup(str+1);
259 if (path->system == NULL || path->name == NULL) {
260 zfree(&path->system);
268 const char *event_type(int type)
271 case PERF_TYPE_HARDWARE:
274 case PERF_TYPE_SOFTWARE:
277 case PERF_TYPE_TRACEPOINT:
280 case PERF_TYPE_HW_CACHE:
281 return "hardware-cache";
290 static int parse_events__is_name_term(struct parse_events_term *term)
292 return term->type_term == PARSE_EVENTS__TERM_TYPE_NAME;
295 static char *get_config_name(struct list_head *head_terms)
297 struct parse_events_term *term;
302 list_for_each_entry(term, head_terms, list)
303 if (parse_events__is_name_term(term))
304 return term->val.str;
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)
315 struct perf_evsel *evsel;
317 event_attr_init(attr);
319 evsel = perf_evsel__new_idx(attr, *idx);
324 evsel->cpus = cpu_map__get(cpus);
325 evsel->own_cpus = cpu_map__get(cpus);
326 evsel->system_wide = !!cpus;
329 evsel->name = strdup(name);
332 list_splice(config_terms, &evsel->config_terms);
334 list_add_tail(&evsel->node, list);
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)
342 return __add_event(list, idx, attr, name, NULL, config_terms) ? 0 : -ENOMEM;
345 static int parse_aliases(char *str, const char *names[][PERF_EVSEL__MAX_ALIASES], int size)
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))
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);
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)
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 };
387 * No fallback - if we cannot get a clear cache type
390 cache_type = parse_aliases(type, perf_evsel__hw_cache,
391 PERF_COUNT_HW_CACHE_MAX);
392 if (cache_type == -1)
395 config_name = get_config_name(head_config);
396 n = snprintf(name, MAX_NAME_LEN, "%s", type);
398 for (i = 0; (i < 2) && (op_result[i]); i++) {
399 char *str = op_result[i];
401 n += snprintf(name + n, MAX_NAME_LEN - n, "-%s", str);
403 if (cache_op == -1) {
404 cache_op = parse_aliases(str, perf_evsel__hw_cache_op,
405 PERF_COUNT_HW_CACHE_OP_MAX);
407 if (!perf_evsel__is_cache_op_valid(cache_type, cache_op))
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)
422 * Fall back to reads:
425 cache_op = PERF_COUNT_HW_CACHE_OP_READ;
428 * Fall back to accesses:
430 if (cache_result == -1)
431 cache_result = PERF_COUNT_HW_CACHE_RESULT_ACCESS;
433 memset(&attr, 0, sizeof(attr));
434 attr.config = cache_type | (cache_op << 8) | (cache_result << 16);
435 attr.type = PERF_TYPE_HW_CACHE;
438 if (config_attr(&attr, head_config, err,
442 if (get_config_terms(head_config, &config_terms))
445 return add_event(list, idx, &attr, config_name ? : name, &config_terms);
448 static void tracepoint_error(struct parse_events_error *e, int err,
449 const char *sys, const char *name)
457 * We get error directly from syscall errno ( > 0),
458 * or from encoded pointer's error ( < 0).
464 e->str = strdup("can't access trace events");
467 e->str = strdup("unknown tracepoint");
470 e->str = strdup("failed to add tracepoint");
474 tracing_path__strerror_open_tp(err, help, sizeof(help), sys, name);
475 e->help = strdup(help);
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)
483 struct perf_evsel *evsel;
485 evsel = perf_evsel__newtp_idx(sys_name, evt_name, (*idx)++);
487 tracepoint_error(err, PTR_ERR(evsel), sys_name, evt_name);
488 return PTR_ERR(evsel);
492 LIST_HEAD(config_terms);
494 if (get_config_terms(head_config, &config_terms))
496 list_splice(&config_terms, &evsel->config_terms);
499 list_add_tail(&evsel->node, list);
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)
508 char evt_path[MAXPATHLEN];
509 struct dirent *evt_ent;
511 int ret = 0, found = 0;
513 snprintf(evt_path, MAXPATHLEN, "%s/%s", tracing_events_path, sys_name);
514 evt_dir = opendir(evt_path);
516 tracepoint_error(err, errno, sys_name, evt_name);
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"))
527 if (!strglobmatch(evt_ent->d_name, evt_name))
532 ret = add_tracepoint(list, idx, sys_name, evt_ent->d_name,
537 tracepoint_error(err, ENOENT, sys_name, evt_name);
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)
550 return strpbrk(evt_name, "*?") ?
551 add_tracepoint_multi_event(list, idx, sys_name, evt_name,
553 add_tracepoint(list, idx, sys_name, evt_name,
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)
562 struct dirent *events_ent;
566 events_dir = opendir(tracing_events_path);
568 tracepoint_error(err, errno, sys_name, evt_name);
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"))
580 if (!strglobmatch(events_ent->d_name, sys_name))
583 ret = add_tracepoint_event(list, idx, events_ent->d_name,
584 evt_name, err, head_config);
587 closedir(events_dir);
591 struct __add_bpf_event_param {
592 struct parse_events_evlist *data;
593 struct list_head *list;
594 struct list_head *head_config;
597 static int add_bpf_event(const char *group, const char *event, int fd,
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;
607 pr_debug("add bpf event %s:%s and attach bpf program %d\n",
610 err = parse_events_add_tracepoint(&new_evsels, &evlist->idx, group,
611 event, evlist->error,
614 struct perf_evsel *evsel, *tmp;
616 pr_debug("Failed to add BPF event %s:%s\n",
618 list_for_each_entry_safe(evsel, tmp, &new_evsels, node) {
619 list_del(&evsel->node);
620 perf_evsel__delete(evsel);
624 pr_debug("adding %s:%s\n", group, event);
626 list_for_each_entry(pos, &new_evsels, node) {
627 pr_debug("adding %s:%s to %p\n",
631 list_splice(&new_evsels, list);
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)
642 struct __add_bpf_event_param param = {data, list, head_config};
643 static bool registered_unprobe_atexit = false;
645 if (IS_ERR(obj) || !obj) {
646 snprintf(errbuf, sizeof(errbuf),
647 "Internal error: load bpf obj with NULL");
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.
657 if (!registered_unprobe_atexit) {
659 registered_unprobe_atexit = true;
662 err = bpf__probe(obj);
664 bpf__strerror_probe(obj, err, errbuf, sizeof(errbuf));
668 err = bpf__load(obj);
670 bpf__strerror_load(obj, err, errbuf, sizeof(errbuf));
674 err = bpf__foreach_event(obj, add_bpf_event, ¶m);
676 snprintf(errbuf, sizeof(errbuf),
677 "Attach events in BPF object failed");
683 data->error->help = strdup("(add -v to see detail)");
684 data->error->str = strdup(errbuf);
689 parse_events_config_bpf(struct parse_events_evlist *data,
690 struct bpf_object *obj,
691 struct list_head *head_config)
693 struct parse_events_term *term;
696 if (!head_config || list_empty(head_config))
699 list_for_each_entry(term, head_config, list) {
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';
708 data->error->idx = term->err_term;
709 data->error->str = strdup(errbuf);
713 err = bpf__config_obj(obj, term, data->evlist, &error_pos);
715 bpf__strerror_config_obj(obj, term, data->evlist,
716 &error_pos, err, 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"
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;
729 data->error->idx = term->err_term + error_pos;
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
741 * 'map:array.value[0]=1' is 'obj config', should be processed
742 * with parse_events_config_bpf.
744 * Move object config terms from the first list to obj_head_config.
747 split_bpf_config_terms(struct list_head *evt_head_config,
748 struct list_head *obj_head_config)
750 struct parse_events_term *term, *temp;
753 * Currectly, all possible user config term
754 * belong to bpf object. parse_events__is_hardcoded_term()
755 * happends to be a good flag.
757 * See parse_events_config_bpf() and
758 * config_term_tracepoint().
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);
765 int parse_events_load_bpf(struct parse_events_evlist *data,
766 struct list_head *list,
769 struct list_head *head_config)
772 struct bpf_object *obj;
773 LIST_HEAD(obj_head_config);
776 split_bpf_config_terms(head_config, &obj_head_config);
778 obj = bpf__prepare_load(bpf_file_name, source);
785 snprintf(errbuf, sizeof(errbuf),
786 "BPF support is not compiled");
788 bpf__strerror_prepare_load(bpf_file_name,
793 data->error->help = strdup("(add -v to see detail)");
794 data->error->str = strdup(errbuf);
798 err = parse_events_load_bpf_obj(data, list, obj, head_config);
801 err = parse_events_config_bpf(data, obj, &obj_head_config);
804 * Caller doesn't know anything about obj_head_config,
805 * so combine them together again before returnning.
808 list_splice_tail(&obj_head_config, head_config);
813 parse_breakpoint_type(const char *type, struct perf_event_attr *attr)
817 for (i = 0; i < 3; i++) {
818 if (!type || !type[i])
821 #define CHECK_SET_TYPE(bit) \
823 if (attr->bp_type & bit) \
826 attr->bp_type |= bit; \
831 CHECK_SET_TYPE(HW_BREAKPOINT_R);
834 CHECK_SET_TYPE(HW_BREAKPOINT_W);
837 CHECK_SET_TYPE(HW_BREAKPOINT_X);
844 #undef CHECK_SET_TYPE
846 if (!attr->bp_type) /* Default */
847 attr->bp_type = HW_BREAKPOINT_R | HW_BREAKPOINT_W;
852 int parse_events_add_breakpoint(struct list_head *list, int *idx,
853 void *ptr, char *type, u64 len)
855 struct perf_event_attr attr;
857 memset(&attr, 0, sizeof(attr));
858 attr.bp_addr = (unsigned long) ptr;
860 if (parse_breakpoint_type(type, &attr))
863 /* Provide some defaults if len is not specified */
865 if (attr.bp_type == HW_BREAKPOINT_X)
868 len = HW_BREAKPOINT_LEN_4;
873 attr.type = PERF_TYPE_BREAKPOINT;
874 attr.sample_period = 1;
876 return add_event(list, idx, &attr, NULL, NULL);
879 static int check_type_val(struct parse_events_term *term,
880 struct parse_events_error *err,
883 if (type == term->type_val)
887 err->idx = term->err_val;
888 if (type == PARSE_EVENTS__TERM_TYPE_NUM)
889 err->str = strdup("expected numeric value");
891 err->str = strdup("expected string value");
897 * Update according to parse-events.l
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",
919 static bool config_term_shrinked;
922 config_term_avail(int term_type, struct parse_events_error *err)
924 if (term_type < 0 || term_type >= __PARSE_EVENTS__TERM_TYPE_NR) {
925 err->str = strdup("Invalid term_type");
928 if (!config_term_shrinked)
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:
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)
950 void parse_events__shrink_config_terms(void)
952 config_term_shrinked = true;
955 static int config_term_common(struct perf_event_attr *attr,
956 struct parse_events_term *term,
957 struct parse_events_error *err)
959 #define CHECK_TYPE_VAL(type) \
961 if (check_type_val(term, err, PARSE_EVENTS__TERM_TYPE_ ## type)) \
965 switch (term->type_term) {
966 case PARSE_EVENTS__TERM_TYPE_CONFIG:
968 attr->config = term->val.num;
970 case PARSE_EVENTS__TERM_TYPE_CONFIG1:
972 attr->config1 = term->val.num;
974 case PARSE_EVENTS__TERM_TYPE_CONFIG2:
976 attr->config2 = term->val.num;
978 case PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD:
981 case PARSE_EVENTS__TERM_TYPE_SAMPLE_FREQ:
984 case PARSE_EVENTS__TERM_TYPE_BRANCH_SAMPLE_TYPE:
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;
993 case PARSE_EVENTS__TERM_TYPE_TIME:
995 if (term->val.num > 1) {
996 err->str = strdup("expected 0 or 1");
997 err->idx = term->err_val;
1001 case PARSE_EVENTS__TERM_TYPE_CALLGRAPH:
1002 CHECK_TYPE_VAL(STR);
1004 case PARSE_EVENTS__TERM_TYPE_STACKSIZE:
1005 CHECK_TYPE_VAL(NUM);
1007 case PARSE_EVENTS__TERM_TYPE_INHERIT:
1008 CHECK_TYPE_VAL(NUM);
1010 case PARSE_EVENTS__TERM_TYPE_NOINHERIT:
1011 CHECK_TYPE_VAL(NUM);
1013 case PARSE_EVENTS__TERM_TYPE_OVERWRITE:
1014 CHECK_TYPE_VAL(NUM);
1016 case PARSE_EVENTS__TERM_TYPE_NOOVERWRITE:
1017 CHECK_TYPE_VAL(NUM);
1019 case PARSE_EVENTS__TERM_TYPE_NAME:
1020 CHECK_TYPE_VAL(STR);
1022 case PARSE_EVENTS__TERM_TYPE_MAX_STACK:
1023 CHECK_TYPE_VAL(NUM);
1026 err->str = strdup("unknown term");
1027 err->idx = term->err_term;
1028 err->help = parse_events_formats_error_string(NULL);
1033 * Check term availbility after basic checking so
1034 * PARSE_EVENTS__TERM_TYPE_USER can be found and filtered.
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.
1041 if (!config_term_avail(term->type_term, err))
1044 #undef CHECK_TYPE_VAL
1047 static int config_term_pmu(struct perf_event_attr *attr,
1048 struct parse_events_term *term,
1049 struct parse_events_error *err)
1051 if (term->type_term == PARSE_EVENTS__TERM_TYPE_USER ||
1052 term->type_term == PARSE_EVENTS__TERM_TYPE_DRV_CFG)
1054 * Always succeed for sysfs terms, as we dont know
1055 * at this point what type they need to have.
1059 return config_term_common(attr, term, err);
1062 static int config_term_tracepoint(struct perf_event_attr *attr,
1063 struct parse_events_term *term,
1064 struct parse_events_error *err)
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);
1077 err->idx = term->err_term;
1078 err->str = strdup("unknown term");
1079 err->help = strdup("valid terms: call-graph,stack-size\n");
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)
1092 struct parse_events_term *term;
1094 list_for_each_entry(term, head, list)
1095 if (config_term(attr, term, err))
1101 static int get_config_terms(struct list_head *head_config,
1102 struct list_head *head_terms __maybe_unused)
1104 #define ADD_CONFIG_TERM(__type, __name, __val) \
1106 struct perf_evsel_config_term *__t; \
1108 __t = zalloc(sizeof(*__t)); \
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); \
1118 struct parse_events_term *term;
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);
1125 case PARSE_EVENTS__TERM_TYPE_SAMPLE_FREQ:
1126 ADD_CONFIG_TERM(FREQ, freq, term->val.num);
1128 case PARSE_EVENTS__TERM_TYPE_TIME:
1129 ADD_CONFIG_TERM(TIME, time, term->val.num);
1131 case PARSE_EVENTS__TERM_TYPE_CALLGRAPH:
1132 ADD_CONFIG_TERM(CALLGRAPH, callgraph, term->val.str);
1134 case PARSE_EVENTS__TERM_TYPE_BRANCH_SAMPLE_TYPE:
1135 ADD_CONFIG_TERM(BRANCH, branch, term->val.str);
1137 case PARSE_EVENTS__TERM_TYPE_STACKSIZE:
1138 ADD_CONFIG_TERM(STACK_USER, stack_user, term->val.num);
1140 case PARSE_EVENTS__TERM_TYPE_INHERIT:
1141 ADD_CONFIG_TERM(INHERIT, inherit, term->val.num ? 1 : 0);
1143 case PARSE_EVENTS__TERM_TYPE_NOINHERIT:
1144 ADD_CONFIG_TERM(INHERIT, inherit, term->val.num ? 0 : 1);
1146 case PARSE_EVENTS__TERM_TYPE_MAX_STACK:
1147 ADD_CONFIG_TERM(MAX_STACK, max_stack, term->val.num);
1149 case PARSE_EVENTS__TERM_TYPE_OVERWRITE:
1150 ADD_CONFIG_TERM(OVERWRITE, overwrite, term->val.num ? 1 : 0);
1152 case PARSE_EVENTS__TERM_TYPE_NOOVERWRITE:
1153 ADD_CONFIG_TERM(OVERWRITE, overwrite, term->val.num ? 0 : 1);
1155 case PARSE_EVENTS__TERM_TYPE_DRV_CFG:
1156 ADD_CONFIG_TERM(DRV_CFG, drv_cfg, term->val.str);
1162 #undef ADD_EVSEL_CONFIG
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)
1172 struct perf_event_attr attr;
1174 if (config_attr(&attr, head_config, err,
1175 config_term_tracepoint))
1179 if (strpbrk(sys, "*?"))
1180 return add_tracepoint_multi_sys(list, idx, sys, event,
1183 return add_tracepoint_event(list, idx, sys, event,
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)
1192 struct perf_event_attr attr;
1193 LIST_HEAD(config_terms);
1195 memset(&attr, 0, sizeof(attr));
1197 attr.config = config;
1200 if (config_attr(&attr, head_config, data->error,
1201 config_term_common))
1204 if (get_config_terms(head_config, &config_terms))
1208 return add_event(list, &data->idx, &attr,
1209 get_config_name(head_config), &config_terms);
1212 int parse_events_add_pmu(struct parse_events_evlist *data,
1213 struct list_head *list, char *name,
1214 struct list_head *head_config)
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);
1222 pmu = perf_pmu__find(name);
1226 if (pmu->default_config) {
1227 memcpy(&attr, pmu->default_config,
1228 sizeof(struct perf_event_attr));
1230 memset(&attr, 0, sizeof(attr));
1234 attr.type = pmu->type;
1235 evsel = __add_event(list, &data->idx, &attr, NULL, pmu->cpus, NULL);
1236 return evsel ? 0 : -ENOMEM;
1239 if (perf_pmu__check_alias(pmu, head_config, &info))
1243 * Configure hardcoded terms first, no need to check
1244 * return value when called with fail == 0 ;)
1246 if (config_attr(&attr, head_config, data->error, config_term_pmu))
1249 if (get_config_terms(head_config, &config_terms))
1252 if (perf_pmu__config(pmu, &attr, head_config, data->error))
1255 evsel = __add_event(list, &data->idx, &attr,
1256 get_config_name(head_config), pmu->cpus,
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;
1267 return evsel ? 0 : -ENOMEM;
1270 int parse_events_multi_pmu_add(struct parse_events_evlist *data,
1271 char *str, struct list_head **listp)
1273 struct list_head *head;
1274 struct parse_events_term *term;
1275 struct list_head *list;
1276 struct perf_pmu *pmu = NULL;
1280 /* Add it for all PMUs that support the alias */
1281 list = malloc(sizeof(struct list_head));
1284 INIT_LIST_HEAD(list);
1285 while ((pmu = perf_pmu__scan(pmu)) != NULL) {
1286 struct perf_pmu_alias *alias;
1288 list_for_each_entry(alias, &pmu->aliases, list) {
1289 if (!strcasecmp(alias->name, str)) {
1290 head = malloc(sizeof(struct list_head));
1293 INIT_LIST_HEAD(head);
1294 if (parse_events_term__num(&term, PARSE_EVENTS__TERM_TYPE_USER,
1295 str, 1, false, &str, NULL) < 0)
1297 list_add_tail(&term->list, head);
1299 if (!parse_events_add_pmu(data, list,
1301 pr_debug("%s -> %s/%s/\n", str,
1302 pmu->name, alias->str);
1306 parse_events_terms__delete(head);
1316 int parse_events__modifier_group(struct list_head *list,
1319 return parse_events__modifier_event(list, event_mod, true);
1322 void parse_events__set_leader(char *name, struct list_head *list)
1324 struct perf_evsel *leader;
1326 if (list_empty(list)) {
1327 WARN_ONCE(true, "WARNING: failed to set leader: empty list");
1331 __perf_evlist__set_leader(list);
1332 leader = list_entry(list->next, struct perf_evsel, node);
1333 leader->group_name = name ? strdup(name) : NULL;
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)
1341 * Called for single event definition. Update the
1342 * 'all event' list, and reinit the 'single event'
1343 * list, for next event definition.
1345 list_splice_tail(list_event, list_all);
1349 struct event_modifier {
1363 static int get_event_modifier(struct event_modifier *mod, char *str,
1364 struct perf_evsel *evsel)
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;
1377 int exclude = eu | ek | eh;
1378 int exclude_GH = evsel ? evsel->exclude_GH : 0;
1380 memset(mod, 0, sizeof(*mod));
1385 exclude = eu = ek = eh = 1;
1387 } else if (*str == 'k') {
1389 exclude = eu = ek = eh = 1;
1391 } else if (*str == 'h') {
1393 exclude = eu = ek = eh = 1;
1395 } else if (*str == 'G') {
1397 exclude_GH = eG = eH = 1;
1399 } else if (*str == 'H') {
1401 exclude_GH = eG = eH = 1;
1403 } else if (*str == 'I') {
1405 } else if (*str == 'p') {
1407 /* use of precise requires exclude_guest */
1410 } else if (*str == 'P') {
1412 } else if (*str == 'S') {
1414 } else if (*str == 'D') {
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
1430 * See also PERF_RECORD_MISC_EXACT_IP
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;
1451 * Basic modifier sanity check to validate it contains only one
1452 * instance of any modifier (apart from 'p') present.
1454 static int check_modifier(char *str)
1458 /* The sizeof includes 0 byte as well. */
1459 if (strlen(str) > (sizeof("ukhGHpppPSDI") - 1))
1463 if (*p != 'p' && strchr(p + 1, *p))
1471 int parse_events__modifier_event(struct list_head *list, char *str, bool add)
1473 struct perf_evsel *evsel;
1474 struct event_modifier mod;
1479 if (check_modifier(str))
1482 if (!add && get_event_modifier(&mod, str, NULL))
1485 __evlist__for_each_entry(list, evsel) {
1486 if (add && get_event_modifier(&mod, str, evsel))
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;
1500 if (perf_evsel__is_group_leader(evsel))
1501 evsel->attr.pinned = mod.pinned;
1507 int parse_events_name(struct list_head *list, char *name)
1509 struct perf_evsel *evsel;
1511 __evlist__for_each_entry(list, evsel) {
1513 evsel->name = strdup(name);
1520 comp_pmu(const void *p1, const void *p2)
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;
1525 return strcasecmp(pmu1->symbol, pmu2->symbol);
1528 static void perf_pmu__parse_cleanup(void)
1530 if (perf_pmu_events_list_num > 0) {
1531 struct perf_pmu_event_symbol *p;
1534 for (i = 0; i < perf_pmu_events_list_num; i++) {
1535 p = perf_pmu_events_list + i;
1538 zfree(&perf_pmu_events_list);
1539 perf_pmu_events_list_num = 0;
1543 #define SET_SYMBOL(str, stype) \
1552 * Read the pmu events list from sysfs
1553 * Save it into perf_pmu_events_list
1555 static void perf_pmu__parse_init(void)
1558 struct perf_pmu *pmu = NULL;
1559 struct perf_pmu_alias *alias;
1563 while ((pmu = perf_pmu__scan(pmu)) != NULL) {
1564 list_for_each_entry(alias, &pmu->aliases, list) {
1565 if (strchr(alias->name, '-'))
1572 perf_pmu_events_list_num = -1;
1575 perf_pmu_events_list = malloc(sizeof(struct perf_pmu_event_symbol) * len);
1576 if (!perf_pmu_events_list)
1578 perf_pmu_events_list_num = len;
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, '-');
1588 SET_SYMBOL(strndup(alias->name, tmp - alias->name),
1589 PMU_EVENT_SYMBOL_PREFIX);
1591 SET_SYMBOL(strdup(++tmp), PMU_EVENT_SYMBOL_SUFFIX);
1594 SET_SYMBOL(strdup(alias->name), PMU_EVENT_SYMBOL);
1599 qsort(perf_pmu_events_list, len,
1600 sizeof(struct perf_pmu_event_symbol), comp_pmu);
1604 perf_pmu__parse_cleanup();
1607 enum perf_pmu_event_symbol_type
1608 perf_pmu__parse_check(const char *name)
1610 struct perf_pmu_event_symbol p, *r;
1612 /* scan kernel pmu events from sysfs if needed */
1613 if (perf_pmu_events_list_num == 0)
1614 perf_pmu__parse_init();
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.
1620 if ((perf_pmu_events_list_num <= 0) || !strcmp(name, "cpu"))
1621 return PMU_EVENT_SYMBOL_ERR;
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);
1628 return r ? r->type : PMU_EVENT_SYMBOL_ERR;
1631 static int parse_events__scanner(const char *str, void *data, int start_token)
1633 YY_BUFFER_STATE buffer;
1637 ret = parse_events_lex_init_extra(start_token, &scanner);
1641 buffer = parse_events__scan_string(str, scanner);
1644 parse_events_debug = 1;
1646 ret = parse_events_parse(data, scanner);
1648 parse_events__flush_buffer(buffer, scanner);
1649 parse_events__delete_buffer(buffer, scanner);
1650 parse_events_lex_destroy(scanner);
1655 * parse event config string, return a list of event terms.
1657 int parse_events_terms(struct list_head *terms, const char *str)
1659 struct parse_events_terms data = {
1664 ret = parse_events__scanner(str, &data, PE_START_TERMS);
1666 list_splice(data.terms, terms);
1671 parse_events_terms__delete(data.terms);
1675 int parse_events(struct perf_evlist *evlist, const char *str,
1676 struct parse_events_error *err)
1678 struct parse_events_evlist data = {
1679 .list = LIST_HEAD_INIT(data.list),
1680 .idx = evlist->nr_entries,
1686 ret = parse_events__scanner(str, &data, PE_START_EVENTS);
1687 perf_pmu__parse_cleanup();
1689 struct perf_evsel *last;
1691 if (list_empty(&data.list)) {
1692 WARN_ONCE(true, "WARNING: event parser found nothing");
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;
1705 * There are 2 users - builtin-record and builtin-test objects.
1706 * Both call perf_evlist__delete in case of error, so we dont
1712 #define MAX_WIDTH 1000
1713 static int get_term_width(void)
1717 get_term_dimensions(&ws);
1718 return ws.ws_col > MAX_WIDTH ? MAX_WIDTH : ws.ws_col;
1721 static void parse_events_print_error(struct parse_events_error *err,
1724 const char *str = "invalid or unsupported event: ";
1725 char _buf[MAX_WIDTH];
1726 char *buf = (char *) event;
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;
1736 * Maximum error index indent, we will cut
1737 * the event string if it's bigger.
1739 int max_err_idx = 13;
1742 * Let's be specific with the message when
1743 * we have the precise error.
1745 str = "event syntax error: ";
1746 len_str = strlen(str);
1747 max_len = width - len_str;
1751 /* We're cutting from the beginning. */
1752 if (err->idx > max_err_idx)
1753 cut = err->idx - max_err_idx;
1755 strncpy(buf, event + cut, max_len);
1757 /* Mark cut parts with '..' on both sides. */
1759 buf[0] = buf[1] = '.';
1761 if ((len_event - cut) > max_len) {
1762 buf[max_len - 1] = buf[max_len - 2] = '.';
1766 idx = len_str + err->idx - cut;
1769 fprintf(stderr, "%s'%s'\n", str, buf);
1771 fprintf(stderr, "%*s\\___ %s\n", idx + 1, "", err->str);
1773 fprintf(stderr, "\n%s\n", err->help);
1778 fprintf(stderr, "Run 'perf list' for a list of valid events\n");
1783 int parse_events_option(const struct option *opt, const char *str,
1784 int unset __maybe_unused)
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);
1791 parse_events_print_error(&err, str);
1797 foreach_evsel_in_last_glob(struct perf_evlist *evlist,
1798 int (*func)(struct perf_evsel *evsel,
1802 struct perf_evsel *last = NULL;
1806 * Don't return when list_empty, give func a chance to report
1807 * error when it found last == NULL.
1809 * So no need to WARN here, let *func do this.
1811 if (evlist->nr_entries > 0)
1812 last = perf_evlist__last(evlist);
1815 err = (*func)(last, arg);
1821 if (last->node.prev == &evlist->entries)
1823 last = list_entry(last->node.prev, struct perf_evsel, node);
1824 } while (!last->cmdline_group_boundary);
1829 static int set_filter(struct perf_evsel *evsel, const void *arg)
1831 const char *str = arg;
1833 int nr_addr_filters = 0;
1834 struct perf_pmu *pmu = NULL;
1839 if (evsel->attr.type == PERF_TYPE_TRACEPOINT) {
1840 if (perf_evsel__append_tp_filter(evsel, str) < 0) {
1842 "not enough memory to hold filter string\n");
1849 while ((pmu = perf_pmu__scan(pmu)) != NULL)
1850 if (pmu->type == evsel->attr.type) {
1856 perf_pmu__scan_file(pmu, "nr_addr_filters",
1857 "%d", &nr_addr_filters);
1859 if (!nr_addr_filters)
1862 if (perf_evsel__append_addr_filter(evsel, str) < 0) {
1864 "not enough memory to hold filter string\n");
1872 "--filter option should follow a -e tracepoint or HW tracer option\n");
1877 int parse_filter(const struct option *opt, const char *str,
1878 int unset __maybe_unused)
1880 struct perf_evlist *evlist = *(struct perf_evlist **)opt->value;
1882 return foreach_evsel_in_last_glob(evlist, set_filter,
1886 static int add_exclude_perf_filter(struct perf_evsel *evsel,
1887 const void *arg __maybe_unused)
1889 char new_filter[64];
1891 if (evsel == NULL || evsel->attr.type != PERF_TYPE_TRACEPOINT) {
1893 "--exclude-perf option should follow a -e tracepoint option\n");
1897 snprintf(new_filter, sizeof(new_filter), "common_pid != %d", getpid());
1899 if (perf_evsel__append_tp_filter(evsel, new_filter) < 0) {
1901 "not enough memory to hold filter string\n");
1908 int exclude_perf(const struct option *opt,
1909 const char *arg __maybe_unused,
1910 int unset __maybe_unused)
1912 struct perf_evlist *evlist = *(struct perf_evlist **)opt->value;
1914 return foreach_evsel_in_last_glob(evlist, add_exclude_perf_filter,
1918 static const char * const event_type_descriptors[] = {
1922 "Hardware cache event",
1923 "Raw hardware event descriptor",
1924 "Hardware breakpoint",
1927 static int cmp_string(const void *a, const void *b)
1929 const char * const *as = a;
1930 const char * const *bs = b;
1932 return strcmp(*as, *bs);
1936 * Print the events from <debugfs_mount_point>/tracing/events
1939 void print_tracepoint_events(const char *subsys_glob, const char *event_glob,
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;
1951 sys_dir = opendir(tracing_events_path);
1955 if (evt_num_known) {
1956 evt_list = zalloc(sizeof(char *) * evt_num);
1958 goto out_close_sys_dir;
1961 for_each_subsystem(sys_dir, sys_dirent) {
1962 if (subsys_glob != NULL &&
1963 !strglobmatch(sys_dirent->d_name, subsys_glob))
1966 snprintf(dir_path, MAXPATHLEN, "%s/%s", tracing_events_path,
1967 sys_dirent->d_name);
1968 evt_dir = opendir(dir_path);
1972 for_each_event(sys_dirent, evt_dir, evt_dirent) {
1973 if (event_glob != NULL &&
1974 !strglobmatch(evt_dirent->d_name, event_glob))
1977 if (!evt_num_known) {
1982 snprintf(evt_path, MAXPATHLEN, "%s:%s",
1983 sys_dirent->d_name, evt_dirent->d_name);
1985 evt_list[evt_i] = strdup(evt_path);
1986 if (evt_list[evt_i] == NULL)
1987 goto out_close_evt_dir;
1994 if (!evt_num_known) {
1995 evt_num_known = true;
1998 qsort(evt_list, evt_num, sizeof(char *), cmp_string);
2000 while (evt_i < evt_num) {
2002 printf("%s ", evt_list[evt_i++]);
2005 printf(" %-50s [%s]\n", evt_list[evt_i++],
2006 event_type_descriptors[PERF_TYPE_TRACEPOINT]);
2008 if (evt_num && pager_in_use())
2013 for (evt_i = 0; evt_i < evt_num; evt_i++)
2014 zfree(&evt_list[evt_i]);
2023 printf("FATAL: not enough memory to print %s\n",
2024 event_type_descriptors[PERF_TYPE_TRACEPOINT]);
2030 * Check whether event is in <debugfs_mount_point>/tracing/events
2033 int is_valid_tracepoint(const char *event_string)
2035 DIR *sys_dir, *evt_dir;
2036 struct dirent *sys_dirent, *evt_dirent;
2037 char evt_path[MAXPATHLEN];
2038 char dir_path[MAXPATHLEN];
2040 sys_dir = opendir(tracing_events_path);
2044 for_each_subsystem(sys_dir, sys_dirent) {
2046 snprintf(dir_path, MAXPATHLEN, "%s/%s", tracing_events_path,
2047 sys_dirent->d_name);
2048 evt_dir = opendir(dir_path);
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)) {
2067 static bool is_event_supported(u8 type, unsigned config)
2071 struct perf_evsel *evsel;
2072 struct perf_event_attr attr = {
2077 struct thread_map *tmap = thread_map__new_by_tid(0);
2082 evsel = perf_evsel__new(&attr);
2084 open_return = perf_evsel__open(evsel, NULL, tmap);
2085 ret = open_return >= 0;
2087 if (open_return == -EACCES) {
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.
2095 evsel->attr.exclude_kernel = 1;
2096 ret = perf_evsel__open(evsel, NULL, tmap) >= 0;
2098 perf_evsel__delete(evsel);
2104 void print_sdt_events(const char *subsys_glob, const char *event_glob,
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;
2116 sdtlist = strlist__new(NULL, &cfg);
2118 pr_debug("Failed to allocate new strlist for SDT\n");
2121 bidlist = build_id_cache__list_all(true);
2123 pr_debug("Failed to get buildids: %d\n", errno);
2126 strlist__for_each_entry(nd, bidlist) {
2127 pcache = probe_cache__new(nd->s);
2130 list_for_each_entry(ent, &pcache->entries, node) {
2134 !strglobmatch(ent->pev.group, subsys_glob))
2137 !strglobmatch(ent->pev.event, event_glob))
2139 ret = asprintf(&buf, "%s:%s@%s", ent->pev.group,
2140 ent->pev.event, nd->s);
2142 strlist__add(sdtlist, buf);
2144 probe_cache__delete(pcache);
2146 strlist__delete(bidlist);
2148 strlist__for_each_entry(nd, sdtlist) {
2149 buf = strchr(nd->s, '@');
2153 printf("%s ", nd->s);
2156 nd2 = strlist__next(nd);
2158 ptr = strchr(nd2->s, '@');
2161 if (strcmp(nd->s, nd2->s) == 0)
2165 path = build_id_cache__origname(buf);
2166 ret = asprintf(&buf, "%s@%s(%.12s)", nd->s, path, buf);
2168 printf(" %-50s [%s]\n", buf, "SDT event");
2172 printf(" %-50s [%s]\n", nd->s, "SDT event");
2174 if (strcmp(nd->s, nd2->s) != 0)
2175 show_detail = false;
2180 strlist__delete(sdtlist);
2183 int print_hwcache_events(const char *event_glob, bool name_only)
2185 unsigned int type, op, i, evt_i = 0, evt_num = 0;
2187 char **evt_list = NULL;
2188 bool evt_num_known = false;
2191 if (evt_num_known) {
2192 evt_list = zalloc(sizeof(char *) * evt_num);
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))
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))
2209 if (!is_event_supported(PERF_TYPE_HW_CACHE,
2210 type | (op << 8) | (i << 16)))
2213 if (!evt_num_known) {
2218 evt_list[evt_i] = strdup(name);
2219 if (evt_list[evt_i] == NULL)
2226 if (!evt_num_known) {
2227 evt_num_known = true;
2230 qsort(evt_list, evt_num, sizeof(char *), cmp_string);
2232 while (evt_i < evt_num) {
2234 printf("%s ", evt_list[evt_i++]);
2237 printf(" %-50s [%s]\n", evt_list[evt_i++],
2238 event_type_descriptors[PERF_TYPE_HW_CACHE]);
2240 if (evt_num && pager_in_use())
2245 for (evt_i = 0; evt_i < evt_num; evt_i++)
2246 zfree(&evt_list[evt_i]);
2251 printf("FATAL: not enough memory to print %s\n", event_type_descriptors[PERF_TYPE_HW_CACHE]);
2257 void print_symbol_events(const char *event_glob, unsigned type,
2258 struct event_symbol *syms, unsigned max,
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;
2267 if (evt_num_known) {
2268 evt_list = zalloc(sizeof(char *) * evt_num);
2274 for (i = 0; i < max; i++, syms++) {
2276 if (event_glob != NULL && syms->symbol != NULL &&
2277 !(strglobmatch(syms->symbol, event_glob) ||
2278 (syms->alias && strglobmatch(syms->alias, event_glob))))
2281 if (!is_event_supported(type, i))
2284 if (!evt_num_known) {
2289 if (!name_only && strlen(syms->alias))
2290 snprintf(name, MAX_NAME_LEN, "%s OR %s", syms->symbol, syms->alias);
2292 strncpy(name, syms->symbol, MAX_NAME_LEN);
2294 evt_list[evt_i] = strdup(name);
2295 if (evt_list[evt_i] == NULL)
2300 if (!evt_num_known) {
2301 evt_num_known = true;
2304 qsort(evt_list, evt_num, sizeof(char *), cmp_string);
2306 while (evt_i < evt_num) {
2308 printf("%s ", evt_list[evt_i++]);
2311 printf(" %-50s [%s]\n", evt_list[evt_i++], event_type_descriptors[type]);
2313 if (evt_num && pager_in_use())
2318 for (evt_i = 0; evt_i < evt_num; evt_i++)
2319 zfree(&evt_list[evt_i]);
2324 printf("FATAL: not enough memory to print %s\n", event_type_descriptors[type]);
2330 * Print the help text for the event symbols:
2332 void print_events(const char *event_glob, bool name_only, bool quiet_flag,
2333 bool long_desc, bool details_flag)
2335 print_symbol_events(event_glob, PERF_TYPE_HARDWARE,
2336 event_symbols_hw, PERF_COUNT_HW_MAX, name_only);
2338 print_symbol_events(event_glob, PERF_TYPE_SOFTWARE,
2339 event_symbols_sw, PERF_COUNT_SW_MAX, name_only);
2341 print_hwcache_events(event_glob, name_only);
2343 print_pmu_events(event_glob, name_only, quiet_flag, long_desc,
2346 if (event_glob != NULL)
2350 printf(" %-50s [%s]\n",
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]);
2357 printf(" (see 'man perf-list' on how to encode it)\n\n");
2359 printf(" %-50s [%s]\n",
2360 "mem:<addr>[/len][:access]",
2361 event_type_descriptors[PERF_TYPE_BREAKPOINT]);
2366 print_tracepoint_events(NULL, NULL, name_only);
2368 print_sdt_events(NULL, NULL, name_only);
2371 int parse_events__is_hardcoded_term(struct parse_events_term *term)
2373 return term->type_term != PARSE_EVENTS__TERM_TYPE_USER;
2376 static int new_term(struct parse_events_term **_term,
2377 struct parse_events_term *temp,
2380 struct parse_events_term *term;
2382 term = malloc(sizeof(*term));
2387 INIT_LIST_HEAD(&term->list);
2389 switch (term->type_val) {
2390 case PARSE_EVENTS__TERM_TYPE_NUM:
2391 term->val.num = num;
2393 case PARSE_EVENTS__TERM_TYPE_STR:
2394 term->val.str = str;
2405 int parse_events_term__num(struct parse_events_term **term,
2406 int type_term, char *config, u64 num,
2408 void *loc_term_, void *loc_val_)
2410 YYLTYPE *loc_term = loc_term_;
2411 YYLTYPE *loc_val = loc_val_;
2413 struct parse_events_term temp = {
2414 .type_val = PARSE_EVENTS__TERM_TYPE_NUM,
2415 .type_term = type_term,
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,
2422 return new_term(term, &temp, NULL, num);
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_)
2429 YYLTYPE *loc_term = loc_term_;
2430 YYLTYPE *loc_val = loc_val_;
2432 struct parse_events_term temp = {
2433 .type_val = PARSE_EVENTS__TERM_TYPE_STR,
2434 .type_term = type_term,
2436 .err_term = loc_term ? loc_term->first_column : 0,
2437 .err_val = loc_val ? loc_val->first_column : 0,
2440 return new_term(term, &temp, str, 0);
2443 int parse_events_term__sym_hw(struct parse_events_term **term,
2444 char *config, unsigned idx)
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",
2453 BUG_ON(idx >= PERF_COUNT_HW_MAX);
2454 sym = &event_symbols_hw[idx];
2456 return new_term(term, &temp, (char *) sym->symbol, 0);
2459 int parse_events_term__clone(struct parse_events_term **new,
2460 struct parse_events_term *term)
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,
2470 return new_term(new, &temp, term->val.str, term->val.num);
2473 int parse_events_copy_term_list(struct list_head *old,
2474 struct list_head **new)
2476 struct parse_events_term *term, *n;
2484 *new = malloc(sizeof(struct list_head));
2487 INIT_LIST_HEAD(*new);
2489 list_for_each_entry (term, old, list) {
2490 ret = parse_events_term__clone(&n, term);
2493 list_add_tail(&n->list, *new);
2498 void parse_events_terms__purge(struct list_head *terms)
2500 struct parse_events_term *term, *h;
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);
2510 void parse_events_terms__delete(struct list_head *terms)
2514 parse_events_terms__purge(terms);
2518 void parse_events__clear_array(struct parse_events_array *a)
2523 void parse_events_evlist_error(struct parse_events_evlist *data,
2524 int idx, const char *str)
2526 struct parse_events_error *err = data->error;
2531 err->str = strdup(str);
2532 WARN_ONCE(!err->str, "WARNING: failed to allocate error string");
2535 static void config_terms_list(char *buf, size_t buf_sz)
2541 for (i = 0; i < __PARSE_EVENTS__TERM_TYPE_NR; i++) {
2542 const char *name = config_term_names[i];
2544 if (!config_term_avail(i, NULL))
2551 if (strlen(buf) + strlen(name) + 2 >= buf_sz)
2563 * Return string contains valid config terms of an event.
2564 * @additional_terms: For terms such as PMU sysfs terms.
2566 char *parse_events_formats_error_string(char *additional_terms)
2569 /* "no-overwrite" is the longest name */
2570 char static_terms[__PARSE_EVENTS__TERM_TYPE_NR *
2571 (sizeof("no-overwrite") - 1)];
2573 config_terms_list(static_terms, sizeof(static_terms));
2575 if (additional_terms) {
2576 if (asprintf(&str, "valid terms: %s,%s",
2577 additional_terms, static_terms) < 0)
2580 if (asprintf(&str, "valid terms: %s", static_terms) < 0)