1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/hw_breakpoint.h>
4 #include <linux/zalloc.h>
11 #include <sys/param.h>
16 #include <subcmd/parse-options.h>
17 #include "parse-events.h"
18 #include <subcmd/exec-cmd.h>
24 #include "bpf-loader.h"
26 #include <api/fs/tracing_path.h>
27 #include "parse-events-bison.h"
28 #define YY_EXTRA_TYPE int
29 #include "parse-events-flex.h"
31 #include "thread_map.h"
33 #include "probe-file.h"
35 #include "util/parse-branch-options.h"
36 #include "metricgroup.h"
38 #define MAX_NAME_LEN 100
41 extern int parse_events_debug;
43 int parse_events_parse(void *parse_state, void *scanner);
44 static int get_config_terms(struct list_head *head_config,
45 struct list_head *head_terms __maybe_unused);
47 static struct perf_pmu_event_symbol *perf_pmu_events_list;
49 * The variable indicates the number of supported pmu event symbols.
50 * 0 means not initialized and ready to init
51 * -1 means failed to init, don't try anymore
52 * >0 is the number of supported pmu event symbols
54 static int perf_pmu_events_list_num;
56 struct event_symbol event_symbols_hw[PERF_COUNT_HW_MAX] = {
57 [PERF_COUNT_HW_CPU_CYCLES] = {
58 .symbol = "cpu-cycles",
61 [PERF_COUNT_HW_INSTRUCTIONS] = {
62 .symbol = "instructions",
65 [PERF_COUNT_HW_CACHE_REFERENCES] = {
66 .symbol = "cache-references",
69 [PERF_COUNT_HW_CACHE_MISSES] = {
70 .symbol = "cache-misses",
73 [PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = {
74 .symbol = "branch-instructions",
77 [PERF_COUNT_HW_BRANCH_MISSES] = {
78 .symbol = "branch-misses",
81 [PERF_COUNT_HW_BUS_CYCLES] = {
82 .symbol = "bus-cycles",
85 [PERF_COUNT_HW_STALLED_CYCLES_FRONTEND] = {
86 .symbol = "stalled-cycles-frontend",
87 .alias = "idle-cycles-frontend",
89 [PERF_COUNT_HW_STALLED_CYCLES_BACKEND] = {
90 .symbol = "stalled-cycles-backend",
91 .alias = "idle-cycles-backend",
93 [PERF_COUNT_HW_REF_CPU_CYCLES] = {
94 .symbol = "ref-cycles",
99 struct event_symbol event_symbols_sw[PERF_COUNT_SW_MAX] = {
100 [PERF_COUNT_SW_CPU_CLOCK] = {
101 .symbol = "cpu-clock",
104 [PERF_COUNT_SW_TASK_CLOCK] = {
105 .symbol = "task-clock",
108 [PERF_COUNT_SW_PAGE_FAULTS] = {
109 .symbol = "page-faults",
112 [PERF_COUNT_SW_CONTEXT_SWITCHES] = {
113 .symbol = "context-switches",
116 [PERF_COUNT_SW_CPU_MIGRATIONS] = {
117 .symbol = "cpu-migrations",
118 .alias = "migrations",
120 [PERF_COUNT_SW_PAGE_FAULTS_MIN] = {
121 .symbol = "minor-faults",
124 [PERF_COUNT_SW_PAGE_FAULTS_MAJ] = {
125 .symbol = "major-faults",
128 [PERF_COUNT_SW_ALIGNMENT_FAULTS] = {
129 .symbol = "alignment-faults",
132 [PERF_COUNT_SW_EMULATION_FAULTS] = {
133 .symbol = "emulation-faults",
136 [PERF_COUNT_SW_DUMMY] = {
140 [PERF_COUNT_SW_BPF_OUTPUT] = {
141 .symbol = "bpf-output",
146 #define __PERF_EVENT_FIELD(config, name) \
147 ((config & PERF_EVENT_##name##_MASK) >> PERF_EVENT_##name##_SHIFT)
149 #define PERF_EVENT_RAW(config) __PERF_EVENT_FIELD(config, RAW)
150 #define PERF_EVENT_CONFIG(config) __PERF_EVENT_FIELD(config, CONFIG)
151 #define PERF_EVENT_TYPE(config) __PERF_EVENT_FIELD(config, TYPE)
152 #define PERF_EVENT_ID(config) __PERF_EVENT_FIELD(config, EVENT)
154 #define for_each_subsystem(sys_dir, sys_dirent) \
155 while ((sys_dirent = readdir(sys_dir)) != NULL) \
156 if (sys_dirent->d_type == DT_DIR && \
157 (strcmp(sys_dirent->d_name, ".")) && \
158 (strcmp(sys_dirent->d_name, "..")))
160 static int tp_event_has_id(const char *dir_path, struct dirent *evt_dir)
162 char evt_path[MAXPATHLEN];
165 snprintf(evt_path, MAXPATHLEN, "%s/%s/id", dir_path, evt_dir->d_name);
166 fd = open(evt_path, O_RDONLY);
174 #define for_each_event(dir_path, evt_dir, evt_dirent) \
175 while ((evt_dirent = readdir(evt_dir)) != NULL) \
176 if (evt_dirent->d_type == DT_DIR && \
177 (strcmp(evt_dirent->d_name, ".")) && \
178 (strcmp(evt_dirent->d_name, "..")) && \
179 (!tp_event_has_id(dir_path, evt_dirent)))
181 #define MAX_EVENT_LENGTH 512
184 struct tracepoint_path *tracepoint_id_to_path(u64 config)
186 struct tracepoint_path *path = NULL;
187 DIR *sys_dir, *evt_dir;
188 struct dirent *sys_dirent, *evt_dirent;
192 char evt_path[MAXPATHLEN];
195 sys_dir = tracing_events__opendir();
199 for_each_subsystem(sys_dir, sys_dirent) {
200 dir_path = get_events_file(sys_dirent->d_name);
203 evt_dir = opendir(dir_path);
207 for_each_event(dir_path, evt_dir, evt_dirent) {
209 scnprintf(evt_path, MAXPATHLEN, "%s/%s/id", dir_path,
211 fd = open(evt_path, O_RDONLY);
214 if (read(fd, id_buf, sizeof(id_buf)) < 0) {
221 put_events_file(dir_path);
224 path = zalloc(sizeof(*path));
227 path->system = malloc(MAX_EVENT_LENGTH);
232 path->name = malloc(MAX_EVENT_LENGTH);
234 zfree(&path->system);
238 strncpy(path->system, sys_dirent->d_name,
240 strncpy(path->name, evt_dirent->d_name,
247 put_events_file(dir_path);
254 struct tracepoint_path *tracepoint_name_to_path(const char *name)
256 struct tracepoint_path *path = zalloc(sizeof(*path));
257 char *str = strchr(name, ':');
259 if (path == NULL || str == NULL) {
264 path->system = strndup(name, str - name);
265 path->name = strdup(str+1);
267 if (path->system == NULL || path->name == NULL) {
268 zfree(&path->system);
276 const char *event_type(int type)
279 case PERF_TYPE_HARDWARE:
282 case PERF_TYPE_SOFTWARE:
285 case PERF_TYPE_TRACEPOINT:
288 case PERF_TYPE_HW_CACHE:
289 return "hardware-cache";
298 static int parse_events__is_name_term(struct parse_events_term *term)
300 return term->type_term == PARSE_EVENTS__TERM_TYPE_NAME;
303 static char *get_config_name(struct list_head *head_terms)
305 struct parse_events_term *term;
310 list_for_each_entry(term, head_terms, list)
311 if (parse_events__is_name_term(term))
312 return term->val.str;
317 static struct evsel *
318 __add_event(struct list_head *list, int *idx,
319 struct perf_event_attr *attr,
320 char *name, struct perf_pmu *pmu,
321 struct list_head *config_terms, bool auto_merge_stats,
322 const char *cpu_list)
325 struct perf_cpu_map *cpus = pmu ? pmu->cpus :
326 cpu_list ? cpu_map__new(cpu_list) : NULL;
328 event_attr_init(attr);
330 evsel = perf_evsel__new_idx(attr, *idx);
335 evsel->cpus = cpu_map__get(cpus);
336 evsel->own_cpus = cpu_map__get(cpus);
337 evsel->system_wide = pmu ? pmu->is_uncore : false;
338 evsel->auto_merge_stats = auto_merge_stats;
341 evsel->name = strdup(name);
344 list_splice(config_terms, &evsel->config_terms);
346 list_add_tail(&evsel->node, list);
350 static int add_event(struct list_head *list, int *idx,
351 struct perf_event_attr *attr, char *name,
352 struct list_head *config_terms)
354 return __add_event(list, idx, attr, name, NULL, config_terms, false, NULL) ? 0 : -ENOMEM;
357 static int add_event_tool(struct list_head *list, int *idx,
358 enum perf_tool_event tool_event)
361 struct perf_event_attr attr = {
362 .type = PERF_TYPE_SOFTWARE,
363 .config = PERF_COUNT_SW_DUMMY,
366 evsel = __add_event(list, idx, &attr, NULL, NULL, NULL, false, "0");
369 evsel->tool_event = tool_event;
370 if (tool_event == PERF_TOOL_DURATION_TIME)
371 evsel->unit = strdup("ns");
375 static int parse_aliases(char *str, const char *names[][PERF_EVSEL__MAX_ALIASES], int size)
380 for (i = 0; i < size; i++) {
381 for (j = 0; j < PERF_EVSEL__MAX_ALIASES && names[i][j]; j++) {
382 n = strlen(names[i][j]);
383 if (n > longest && !strncasecmp(str, names[i][j], n))
393 typedef int config_term_func_t(struct perf_event_attr *attr,
394 struct parse_events_term *term,
395 struct parse_events_error *err);
396 static int config_term_common(struct perf_event_attr *attr,
397 struct parse_events_term *term,
398 struct parse_events_error *err);
399 static int config_attr(struct perf_event_attr *attr,
400 struct list_head *head,
401 struct parse_events_error *err,
402 config_term_func_t config_term);
404 int parse_events_add_cache(struct list_head *list, int *idx,
405 char *type, char *op_result1, char *op_result2,
406 struct parse_events_error *err,
407 struct list_head *head_config)
409 struct perf_event_attr attr;
410 LIST_HEAD(config_terms);
411 char name[MAX_NAME_LEN], *config_name;
412 int cache_type = -1, cache_op = -1, cache_result = -1;
413 char *op_result[2] = { op_result1, op_result2 };
417 * No fallback - if we cannot get a clear cache type
420 cache_type = parse_aliases(type, perf_evsel__hw_cache,
421 PERF_COUNT_HW_CACHE_MAX);
422 if (cache_type == -1)
425 config_name = get_config_name(head_config);
426 n = snprintf(name, MAX_NAME_LEN, "%s", type);
428 for (i = 0; (i < 2) && (op_result[i]); i++) {
429 char *str = op_result[i];
431 n += snprintf(name + n, MAX_NAME_LEN - n, "-%s", str);
433 if (cache_op == -1) {
434 cache_op = parse_aliases(str, perf_evsel__hw_cache_op,
435 PERF_COUNT_HW_CACHE_OP_MAX);
437 if (!perf_evsel__is_cache_op_valid(cache_type, cache_op))
443 if (cache_result == -1) {
444 cache_result = parse_aliases(str, perf_evsel__hw_cache_result,
445 PERF_COUNT_HW_CACHE_RESULT_MAX);
446 if (cache_result >= 0)
452 * Fall back to reads:
455 cache_op = PERF_COUNT_HW_CACHE_OP_READ;
458 * Fall back to accesses:
460 if (cache_result == -1)
461 cache_result = PERF_COUNT_HW_CACHE_RESULT_ACCESS;
463 memset(&attr, 0, sizeof(attr));
464 attr.config = cache_type | (cache_op << 8) | (cache_result << 16);
465 attr.type = PERF_TYPE_HW_CACHE;
468 if (config_attr(&attr, head_config, err,
472 if (get_config_terms(head_config, &config_terms))
475 return add_event(list, idx, &attr, config_name ? : name, &config_terms);
478 static void tracepoint_error(struct parse_events_error *e, int err,
479 const char *sys, const char *name)
487 * We get error directly from syscall errno ( > 0),
488 * or from encoded pointer's error ( < 0).
494 e->str = strdup("can't access trace events");
497 e->str = strdup("unknown tracepoint");
500 e->str = strdup("failed to add tracepoint");
504 tracing_path__strerror_open_tp(err, help, sizeof(help), sys, name);
505 e->help = strdup(help);
508 static int add_tracepoint(struct list_head *list, int *idx,
509 const char *sys_name, const char *evt_name,
510 struct parse_events_error *err,
511 struct list_head *head_config)
515 evsel = perf_evsel__newtp_idx(sys_name, evt_name, (*idx)++);
517 tracepoint_error(err, PTR_ERR(evsel), sys_name, evt_name);
518 return PTR_ERR(evsel);
522 LIST_HEAD(config_terms);
524 if (get_config_terms(head_config, &config_terms))
526 list_splice(&config_terms, &evsel->config_terms);
529 list_add_tail(&evsel->node, list);
533 static int add_tracepoint_multi_event(struct list_head *list, int *idx,
534 const char *sys_name, const char *evt_name,
535 struct parse_events_error *err,
536 struct list_head *head_config)
539 struct dirent *evt_ent;
541 int ret = 0, found = 0;
543 evt_path = get_events_file(sys_name);
545 tracepoint_error(err, errno, sys_name, evt_name);
548 evt_dir = opendir(evt_path);
550 put_events_file(evt_path);
551 tracepoint_error(err, errno, sys_name, evt_name);
555 while (!ret && (evt_ent = readdir(evt_dir))) {
556 if (!strcmp(evt_ent->d_name, ".")
557 || !strcmp(evt_ent->d_name, "..")
558 || !strcmp(evt_ent->d_name, "enable")
559 || !strcmp(evt_ent->d_name, "filter"))
562 if (!strglobmatch(evt_ent->d_name, evt_name))
567 ret = add_tracepoint(list, idx, sys_name, evt_ent->d_name,
572 tracepoint_error(err, ENOENT, sys_name, evt_name);
576 put_events_file(evt_path);
581 static int add_tracepoint_event(struct list_head *list, int *idx,
582 const char *sys_name, const char *evt_name,
583 struct parse_events_error *err,
584 struct list_head *head_config)
586 return strpbrk(evt_name, "*?") ?
587 add_tracepoint_multi_event(list, idx, sys_name, evt_name,
589 add_tracepoint(list, idx, sys_name, evt_name,
593 static int add_tracepoint_multi_sys(struct list_head *list, int *idx,
594 const char *sys_name, const char *evt_name,
595 struct parse_events_error *err,
596 struct list_head *head_config)
598 struct dirent *events_ent;
602 events_dir = tracing_events__opendir();
604 tracepoint_error(err, errno, sys_name, evt_name);
608 while (!ret && (events_ent = readdir(events_dir))) {
609 if (!strcmp(events_ent->d_name, ".")
610 || !strcmp(events_ent->d_name, "..")
611 || !strcmp(events_ent->d_name, "enable")
612 || !strcmp(events_ent->d_name, "header_event")
613 || !strcmp(events_ent->d_name, "header_page"))
616 if (!strglobmatch(events_ent->d_name, sys_name))
619 ret = add_tracepoint_event(list, idx, events_ent->d_name,
620 evt_name, err, head_config);
623 closedir(events_dir);
627 struct __add_bpf_event_param {
628 struct parse_events_state *parse_state;
629 struct list_head *list;
630 struct list_head *head_config;
633 static int add_bpf_event(const char *group, const char *event, int fd, struct bpf_object *obj,
636 LIST_HEAD(new_evsels);
637 struct __add_bpf_event_param *param = _param;
638 struct parse_events_state *parse_state = param->parse_state;
639 struct list_head *list = param->list;
643 * Check if we should add the event, i.e. if it is a TP but starts with a '!',
644 * then don't add the tracepoint, this will be used for something else, like
645 * adding to a BPF_MAP_TYPE_PROG_ARRAY.
647 * See tools/perf/examples/bpf/augmented_raw_syscalls.c
652 pr_debug("add bpf event %s:%s and attach bpf program %d\n",
655 err = parse_events_add_tracepoint(&new_evsels, &parse_state->idx, group,
656 event, parse_state->error,
659 struct evsel *evsel, *tmp;
661 pr_debug("Failed to add BPF event %s:%s\n",
663 list_for_each_entry_safe(evsel, tmp, &new_evsels, node) {
664 list_del_init(&evsel->node);
665 perf_evsel__delete(evsel);
669 pr_debug("adding %s:%s\n", group, event);
671 list_for_each_entry(pos, &new_evsels, node) {
672 pr_debug("adding %s:%s to %p\n",
677 list_splice(&new_evsels, list);
681 int parse_events_load_bpf_obj(struct parse_events_state *parse_state,
682 struct list_head *list,
683 struct bpf_object *obj,
684 struct list_head *head_config)
688 struct __add_bpf_event_param param = {parse_state, list, head_config};
689 static bool registered_unprobe_atexit = false;
691 if (IS_ERR(obj) || !obj) {
692 snprintf(errbuf, sizeof(errbuf),
693 "Internal error: load bpf obj with NULL");
699 * Register atexit handler before calling bpf__probe() so
700 * bpf__probe() don't need to unprobe probe points its already
701 * created when failure.
703 if (!registered_unprobe_atexit) {
705 registered_unprobe_atexit = true;
708 err = bpf__probe(obj);
710 bpf__strerror_probe(obj, err, errbuf, sizeof(errbuf));
714 err = bpf__load(obj);
716 bpf__strerror_load(obj, err, errbuf, sizeof(errbuf));
720 err = bpf__foreach_event(obj, add_bpf_event, ¶m);
722 snprintf(errbuf, sizeof(errbuf),
723 "Attach events in BPF object failed");
729 parse_state->error->help = strdup("(add -v to see detail)");
730 parse_state->error->str = strdup(errbuf);
735 parse_events_config_bpf(struct parse_events_state *parse_state,
736 struct bpf_object *obj,
737 struct list_head *head_config)
739 struct parse_events_term *term;
742 if (!head_config || list_empty(head_config))
745 list_for_each_entry(term, head_config, list) {
749 if (term->type_term != PARSE_EVENTS__TERM_TYPE_USER) {
750 snprintf(errbuf, sizeof(errbuf),
751 "Invalid config term for BPF object");
752 errbuf[BUFSIZ - 1] = '\0';
754 parse_state->error->idx = term->err_term;
755 parse_state->error->str = strdup(errbuf);
759 err = bpf__config_obj(obj, term, parse_state->evlist, &error_pos);
761 bpf__strerror_config_obj(obj, term, parse_state->evlist,
762 &error_pos, err, errbuf,
764 parse_state->error->help = strdup(
765 "Hint:\tValid config terms:\n"
766 " \tmap:[<arraymap>].value<indices>=[value]\n"
767 " \tmap:[<eventmap>].event<indices>=[event]\n"
769 " \twhere <indices> is something like [0,3...5] or [all]\n"
770 " \t(add -v to see detail)");
771 parse_state->error->str = strdup(errbuf);
772 if (err == -BPF_LOADER_ERRNO__OBJCONF_MAP_VALUE)
773 parse_state->error->idx = term->err_val;
775 parse_state->error->idx = term->err_term + error_pos;
783 * Split config terms:
784 * perf record -e bpf.c/call-graph=fp,map:array.value[0]=1/ ...
785 * 'call-graph=fp' is 'evt config', should be applied to each
787 * 'map:array.value[0]=1' is 'obj config', should be processed
788 * with parse_events_config_bpf.
790 * Move object config terms from the first list to obj_head_config.
793 split_bpf_config_terms(struct list_head *evt_head_config,
794 struct list_head *obj_head_config)
796 struct parse_events_term *term, *temp;
799 * Currectly, all possible user config term
800 * belong to bpf object. parse_events__is_hardcoded_term()
801 * happends to be a good flag.
803 * See parse_events_config_bpf() and
804 * config_term_tracepoint().
806 list_for_each_entry_safe(term, temp, evt_head_config, list)
807 if (!parse_events__is_hardcoded_term(term))
808 list_move_tail(&term->list, obj_head_config);
811 int parse_events_load_bpf(struct parse_events_state *parse_state,
812 struct list_head *list,
815 struct list_head *head_config)
818 struct bpf_object *obj;
819 LIST_HEAD(obj_head_config);
822 split_bpf_config_terms(head_config, &obj_head_config);
824 obj = bpf__prepare_load(bpf_file_name, source);
831 snprintf(errbuf, sizeof(errbuf),
832 "BPF support is not compiled");
834 bpf__strerror_prepare_load(bpf_file_name,
839 parse_state->error->help = strdup("(add -v to see detail)");
840 parse_state->error->str = strdup(errbuf);
844 err = parse_events_load_bpf_obj(parse_state, list, obj, head_config);
847 err = parse_events_config_bpf(parse_state, obj, &obj_head_config);
850 * Caller doesn't know anything about obj_head_config,
851 * so combine them together again before returnning.
854 list_splice_tail(&obj_head_config, head_config);
859 parse_breakpoint_type(const char *type, struct perf_event_attr *attr)
863 for (i = 0; i < 3; i++) {
864 if (!type || !type[i])
867 #define CHECK_SET_TYPE(bit) \
869 if (attr->bp_type & bit) \
872 attr->bp_type |= bit; \
877 CHECK_SET_TYPE(HW_BREAKPOINT_R);
880 CHECK_SET_TYPE(HW_BREAKPOINT_W);
883 CHECK_SET_TYPE(HW_BREAKPOINT_X);
890 #undef CHECK_SET_TYPE
892 if (!attr->bp_type) /* Default */
893 attr->bp_type = HW_BREAKPOINT_R | HW_BREAKPOINT_W;
898 int parse_events_add_breakpoint(struct list_head *list, int *idx,
899 void *ptr, char *type, u64 len)
901 struct perf_event_attr attr;
903 memset(&attr, 0, sizeof(attr));
904 attr.bp_addr = (unsigned long) ptr;
906 if (parse_breakpoint_type(type, &attr))
909 /* Provide some defaults if len is not specified */
911 if (attr.bp_type == HW_BREAKPOINT_X)
914 len = HW_BREAKPOINT_LEN_4;
919 attr.type = PERF_TYPE_BREAKPOINT;
920 attr.sample_period = 1;
922 return add_event(list, idx, &attr, NULL, NULL);
925 static int check_type_val(struct parse_events_term *term,
926 struct parse_events_error *err,
929 if (type == term->type_val)
933 err->idx = term->err_val;
934 if (type == PARSE_EVENTS__TERM_TYPE_NUM)
935 err->str = strdup("expected numeric value");
937 err->str = strdup("expected string value");
943 * Update according to parse-events.l
945 static const char *config_term_names[__PARSE_EVENTS__TERM_TYPE_NR] = {
946 [PARSE_EVENTS__TERM_TYPE_USER] = "<sysfs term>",
947 [PARSE_EVENTS__TERM_TYPE_CONFIG] = "config",
948 [PARSE_EVENTS__TERM_TYPE_CONFIG1] = "config1",
949 [PARSE_EVENTS__TERM_TYPE_CONFIG2] = "config2",
950 [PARSE_EVENTS__TERM_TYPE_NAME] = "name",
951 [PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD] = "period",
952 [PARSE_EVENTS__TERM_TYPE_SAMPLE_FREQ] = "freq",
953 [PARSE_EVENTS__TERM_TYPE_BRANCH_SAMPLE_TYPE] = "branch_type",
954 [PARSE_EVENTS__TERM_TYPE_TIME] = "time",
955 [PARSE_EVENTS__TERM_TYPE_CALLGRAPH] = "call-graph",
956 [PARSE_EVENTS__TERM_TYPE_STACKSIZE] = "stack-size",
957 [PARSE_EVENTS__TERM_TYPE_NOINHERIT] = "no-inherit",
958 [PARSE_EVENTS__TERM_TYPE_INHERIT] = "inherit",
959 [PARSE_EVENTS__TERM_TYPE_MAX_STACK] = "max-stack",
960 [PARSE_EVENTS__TERM_TYPE_MAX_EVENTS] = "nr",
961 [PARSE_EVENTS__TERM_TYPE_OVERWRITE] = "overwrite",
962 [PARSE_EVENTS__TERM_TYPE_NOOVERWRITE] = "no-overwrite",
963 [PARSE_EVENTS__TERM_TYPE_DRV_CFG] = "driver-config",
964 [PARSE_EVENTS__TERM_TYPE_PERCORE] = "percore",
967 static bool config_term_shrinked;
970 config_term_avail(int term_type, struct parse_events_error *err)
972 if (term_type < 0 || term_type >= __PARSE_EVENTS__TERM_TYPE_NR) {
973 err->str = strdup("Invalid term_type");
976 if (!config_term_shrinked)
980 case PARSE_EVENTS__TERM_TYPE_CONFIG:
981 case PARSE_EVENTS__TERM_TYPE_CONFIG1:
982 case PARSE_EVENTS__TERM_TYPE_CONFIG2:
983 case PARSE_EVENTS__TERM_TYPE_NAME:
984 case PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD:
985 case PARSE_EVENTS__TERM_TYPE_PERCORE:
991 /* term_type is validated so indexing is safe */
992 if (asprintf(&err->str, "'%s' is not usable in 'perf stat'",
993 config_term_names[term_type]) < 0)
999 void parse_events__shrink_config_terms(void)
1001 config_term_shrinked = true;
1004 static int config_term_common(struct perf_event_attr *attr,
1005 struct parse_events_term *term,
1006 struct parse_events_error *err)
1008 #define CHECK_TYPE_VAL(type) \
1010 if (check_type_val(term, err, PARSE_EVENTS__TERM_TYPE_ ## type)) \
1014 switch (term->type_term) {
1015 case PARSE_EVENTS__TERM_TYPE_CONFIG:
1016 CHECK_TYPE_VAL(NUM);
1017 attr->config = term->val.num;
1019 case PARSE_EVENTS__TERM_TYPE_CONFIG1:
1020 CHECK_TYPE_VAL(NUM);
1021 attr->config1 = term->val.num;
1023 case PARSE_EVENTS__TERM_TYPE_CONFIG2:
1024 CHECK_TYPE_VAL(NUM);
1025 attr->config2 = term->val.num;
1027 case PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD:
1028 CHECK_TYPE_VAL(NUM);
1030 case PARSE_EVENTS__TERM_TYPE_SAMPLE_FREQ:
1031 CHECK_TYPE_VAL(NUM);
1033 case PARSE_EVENTS__TERM_TYPE_BRANCH_SAMPLE_TYPE:
1034 CHECK_TYPE_VAL(STR);
1035 if (strcmp(term->val.str, "no") &&
1036 parse_branch_str(term->val.str, &attr->branch_sample_type)) {
1037 err->str = strdup("invalid branch sample type");
1038 err->idx = term->err_val;
1042 case PARSE_EVENTS__TERM_TYPE_TIME:
1043 CHECK_TYPE_VAL(NUM);
1044 if (term->val.num > 1) {
1045 err->str = strdup("expected 0 or 1");
1046 err->idx = term->err_val;
1050 case PARSE_EVENTS__TERM_TYPE_CALLGRAPH:
1051 CHECK_TYPE_VAL(STR);
1053 case PARSE_EVENTS__TERM_TYPE_STACKSIZE:
1054 CHECK_TYPE_VAL(NUM);
1056 case PARSE_EVENTS__TERM_TYPE_INHERIT:
1057 CHECK_TYPE_VAL(NUM);
1059 case PARSE_EVENTS__TERM_TYPE_NOINHERIT:
1060 CHECK_TYPE_VAL(NUM);
1062 case PARSE_EVENTS__TERM_TYPE_OVERWRITE:
1063 CHECK_TYPE_VAL(NUM);
1065 case PARSE_EVENTS__TERM_TYPE_NOOVERWRITE:
1066 CHECK_TYPE_VAL(NUM);
1068 case PARSE_EVENTS__TERM_TYPE_NAME:
1069 CHECK_TYPE_VAL(STR);
1071 case PARSE_EVENTS__TERM_TYPE_MAX_STACK:
1072 CHECK_TYPE_VAL(NUM);
1074 case PARSE_EVENTS__TERM_TYPE_MAX_EVENTS:
1075 CHECK_TYPE_VAL(NUM);
1077 case PARSE_EVENTS__TERM_TYPE_PERCORE:
1078 CHECK_TYPE_VAL(NUM);
1079 if ((unsigned int)term->val.num > 1) {
1080 err->str = strdup("expected 0 or 1");
1081 err->idx = term->err_val;
1086 err->str = strdup("unknown term");
1087 err->idx = term->err_term;
1088 err->help = parse_events_formats_error_string(NULL);
1093 * Check term availbility after basic checking so
1094 * PARSE_EVENTS__TERM_TYPE_USER can be found and filtered.
1096 * If check availbility at the entry of this function,
1097 * user will see "'<sysfs term>' is not usable in 'perf stat'"
1098 * if an invalid config term is provided for legacy events
1099 * (for example, instructions/badterm/...), which is confusing.
1101 if (!config_term_avail(term->type_term, err))
1104 #undef CHECK_TYPE_VAL
1107 static int config_term_pmu(struct perf_event_attr *attr,
1108 struct parse_events_term *term,
1109 struct parse_events_error *err)
1111 if (term->type_term == PARSE_EVENTS__TERM_TYPE_USER ||
1112 term->type_term == PARSE_EVENTS__TERM_TYPE_DRV_CFG)
1114 * Always succeed for sysfs terms, as we dont know
1115 * at this point what type they need to have.
1119 return config_term_common(attr, term, err);
1122 static int config_term_tracepoint(struct perf_event_attr *attr,
1123 struct parse_events_term *term,
1124 struct parse_events_error *err)
1126 switch (term->type_term) {
1127 case PARSE_EVENTS__TERM_TYPE_CALLGRAPH:
1128 case PARSE_EVENTS__TERM_TYPE_STACKSIZE:
1129 case PARSE_EVENTS__TERM_TYPE_INHERIT:
1130 case PARSE_EVENTS__TERM_TYPE_NOINHERIT:
1131 case PARSE_EVENTS__TERM_TYPE_MAX_STACK:
1132 case PARSE_EVENTS__TERM_TYPE_MAX_EVENTS:
1133 case PARSE_EVENTS__TERM_TYPE_OVERWRITE:
1134 case PARSE_EVENTS__TERM_TYPE_NOOVERWRITE:
1135 return config_term_common(attr, term, err);
1138 err->idx = term->err_term;
1139 err->str = strdup("unknown term");
1140 err->help = strdup("valid terms: call-graph,stack-size\n");
1148 static int config_attr(struct perf_event_attr *attr,
1149 struct list_head *head,
1150 struct parse_events_error *err,
1151 config_term_func_t config_term)
1153 struct parse_events_term *term;
1155 list_for_each_entry(term, head, list)
1156 if (config_term(attr, term, err))
1162 static int get_config_terms(struct list_head *head_config,
1163 struct list_head *head_terms __maybe_unused)
1165 #define ADD_CONFIG_TERM(__type, __name, __val) \
1167 struct perf_evsel_config_term *__t; \
1169 __t = zalloc(sizeof(*__t)); \
1173 INIT_LIST_HEAD(&__t->list); \
1174 __t->type = PERF_EVSEL__CONFIG_TERM_ ## __type; \
1175 __t->val.__name = __val; \
1176 __t->weak = term->weak; \
1177 list_add_tail(&__t->list, head_terms); \
1180 struct parse_events_term *term;
1182 list_for_each_entry(term, head_config, list) {
1183 switch (term->type_term) {
1184 case PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD:
1185 ADD_CONFIG_TERM(PERIOD, period, term->val.num);
1187 case PARSE_EVENTS__TERM_TYPE_SAMPLE_FREQ:
1188 ADD_CONFIG_TERM(FREQ, freq, term->val.num);
1190 case PARSE_EVENTS__TERM_TYPE_TIME:
1191 ADD_CONFIG_TERM(TIME, time, term->val.num);
1193 case PARSE_EVENTS__TERM_TYPE_CALLGRAPH:
1194 ADD_CONFIG_TERM(CALLGRAPH, callgraph, term->val.str);
1196 case PARSE_EVENTS__TERM_TYPE_BRANCH_SAMPLE_TYPE:
1197 ADD_CONFIG_TERM(BRANCH, branch, term->val.str);
1199 case PARSE_EVENTS__TERM_TYPE_STACKSIZE:
1200 ADD_CONFIG_TERM(STACK_USER, stack_user, term->val.num);
1202 case PARSE_EVENTS__TERM_TYPE_INHERIT:
1203 ADD_CONFIG_TERM(INHERIT, inherit, term->val.num ? 1 : 0);
1205 case PARSE_EVENTS__TERM_TYPE_NOINHERIT:
1206 ADD_CONFIG_TERM(INHERIT, inherit, term->val.num ? 0 : 1);
1208 case PARSE_EVENTS__TERM_TYPE_MAX_STACK:
1209 ADD_CONFIG_TERM(MAX_STACK, max_stack, term->val.num);
1211 case PARSE_EVENTS__TERM_TYPE_MAX_EVENTS:
1212 ADD_CONFIG_TERM(MAX_EVENTS, max_events, term->val.num);
1214 case PARSE_EVENTS__TERM_TYPE_OVERWRITE:
1215 ADD_CONFIG_TERM(OVERWRITE, overwrite, term->val.num ? 1 : 0);
1217 case PARSE_EVENTS__TERM_TYPE_NOOVERWRITE:
1218 ADD_CONFIG_TERM(OVERWRITE, overwrite, term->val.num ? 0 : 1);
1220 case PARSE_EVENTS__TERM_TYPE_DRV_CFG:
1221 ADD_CONFIG_TERM(DRV_CFG, drv_cfg, term->val.str);
1223 case PARSE_EVENTS__TERM_TYPE_PERCORE:
1224 ADD_CONFIG_TERM(PERCORE, percore,
1225 term->val.num ? true : false);
1231 #undef ADD_EVSEL_CONFIG
1235 int parse_events_add_tracepoint(struct list_head *list, int *idx,
1236 const char *sys, const char *event,
1237 struct parse_events_error *err,
1238 struct list_head *head_config)
1241 struct perf_event_attr attr;
1243 if (config_attr(&attr, head_config, err,
1244 config_term_tracepoint))
1248 if (strpbrk(sys, "*?"))
1249 return add_tracepoint_multi_sys(list, idx, sys, event,
1252 return add_tracepoint_event(list, idx, sys, event,
1256 int parse_events_add_numeric(struct parse_events_state *parse_state,
1257 struct list_head *list,
1258 u32 type, u64 config,
1259 struct list_head *head_config)
1261 struct perf_event_attr attr;
1262 LIST_HEAD(config_terms);
1264 memset(&attr, 0, sizeof(attr));
1266 attr.config = config;
1269 if (config_attr(&attr, head_config, parse_state->error,
1270 config_term_common))
1273 if (get_config_terms(head_config, &config_terms))
1277 return add_event(list, &parse_state->idx, &attr,
1278 get_config_name(head_config), &config_terms);
1281 int parse_events_add_tool(struct parse_events_state *parse_state,
1282 struct list_head *list,
1283 enum perf_tool_event tool_event)
1285 return add_event_tool(list, &parse_state->idx, tool_event);
1288 static bool config_term_percore(struct list_head *config_terms)
1290 struct perf_evsel_config_term *term;
1292 list_for_each_entry(term, config_terms, list) {
1293 if (term->type == PERF_EVSEL__CONFIG_TERM_PERCORE)
1294 return term->val.percore;
1300 int parse_events_add_pmu(struct parse_events_state *parse_state,
1301 struct list_head *list, char *name,
1302 struct list_head *head_config,
1303 bool auto_merge_stats,
1306 struct perf_event_attr attr;
1307 struct perf_pmu_info info;
1308 struct perf_pmu *pmu;
1309 struct evsel *evsel;
1310 struct parse_events_error *err = parse_state->error;
1311 bool use_uncore_alias;
1312 LIST_HEAD(config_terms);
1314 pmu = perf_pmu__find(name);
1316 if (asprintf(&err->str,
1317 "Cannot find PMU `%s'. Missing kernel support?",
1323 if (pmu->default_config) {
1324 memcpy(&attr, pmu->default_config,
1325 sizeof(struct perf_event_attr));
1327 memset(&attr, 0, sizeof(attr));
1330 use_uncore_alias = (pmu->is_uncore && use_alias);
1333 attr.type = pmu->type;
1334 evsel = __add_event(list, &parse_state->idx, &attr, NULL, pmu, NULL,
1335 auto_merge_stats, NULL);
1337 evsel->pmu_name = name;
1338 evsel->use_uncore_alias = use_uncore_alias;
1345 if (perf_pmu__check_alias(pmu, head_config, &info))
1349 * Configure hardcoded terms first, no need to check
1350 * return value when called with fail == 0 ;)
1352 if (config_attr(&attr, head_config, parse_state->error, config_term_pmu))
1355 if (get_config_terms(head_config, &config_terms))
1358 if (perf_pmu__config(pmu, &attr, head_config, parse_state->error))
1361 evsel = __add_event(list, &parse_state->idx, &attr,
1362 get_config_name(head_config), pmu,
1363 &config_terms, auto_merge_stats, NULL);
1365 evsel->unit = info.unit;
1366 evsel->scale = info.scale;
1367 evsel->per_pkg = info.per_pkg;
1368 evsel->snapshot = info.snapshot;
1369 evsel->metric_expr = info.metric_expr;
1370 evsel->metric_name = info.metric_name;
1371 evsel->pmu_name = name;
1372 evsel->use_uncore_alias = use_uncore_alias;
1373 evsel->percore = config_term_percore(&evsel->config_terms);
1376 return evsel ? 0 : -ENOMEM;
1379 int parse_events_multi_pmu_add(struct parse_events_state *parse_state,
1380 char *str, struct list_head **listp)
1382 struct list_head *head;
1383 struct parse_events_term *term;
1384 struct list_head *list;
1385 struct perf_pmu *pmu = NULL;
1389 /* Add it for all PMUs that support the alias */
1390 list = malloc(sizeof(struct list_head));
1393 INIT_LIST_HEAD(list);
1394 while ((pmu = perf_pmu__scan(pmu)) != NULL) {
1395 struct perf_pmu_alias *alias;
1397 list_for_each_entry(alias, &pmu->aliases, list) {
1398 if (!strcasecmp(alias->name, str)) {
1399 head = malloc(sizeof(struct list_head));
1402 INIT_LIST_HEAD(head);
1403 if (parse_events_term__num(&term, PARSE_EVENTS__TERM_TYPE_USER,
1404 str, 1, false, &str, NULL) < 0)
1406 list_add_tail(&term->list, head);
1408 if (!parse_events_add_pmu(parse_state, list,
1411 pr_debug("%s -> %s/%s/\n", str,
1412 pmu->name, alias->str);
1416 parse_events_terms__delete(head);
1426 int parse_events__modifier_group(struct list_head *list,
1429 return parse_events__modifier_event(list, event_mod, true);
1433 * Check if the two uncore PMUs are from the same uncore block
1434 * The format of the uncore PMU name is uncore_#blockname_#pmuidx
1436 static bool is_same_uncore_block(const char *pmu_name_a, const char *pmu_name_b)
1438 char *end_a, *end_b;
1440 end_a = strrchr(pmu_name_a, '_');
1441 end_b = strrchr(pmu_name_b, '_');
1443 if (!end_a || !end_b)
1446 if ((end_a - pmu_name_a) != (end_b - pmu_name_b))
1449 return (strncmp(pmu_name_a, pmu_name_b, end_a - pmu_name_a) == 0);
1453 parse_events__set_leader_for_uncore_aliase(char *name, struct list_head *list,
1454 struct parse_events_state *parse_state)
1456 struct evsel *evsel, *leader;
1458 bool is_leader = true;
1459 int i, nr_pmu = 0, total_members, ret = 0;
1461 leader = list_first_entry(list, struct evsel, node);
1462 evsel = list_last_entry(list, struct evsel, node);
1463 total_members = evsel->idx - leader->idx + 1;
1465 leaders = calloc(total_members, sizeof(uintptr_t));
1466 if (WARN_ON(!leaders))
1470 * Going through the whole group and doing sanity check.
1471 * All members must use alias, and be from the same uncore block.
1472 * Also, storing the leader events in an array.
1474 __evlist__for_each_entry(list, evsel) {
1476 /* Only split the uncore group which members use alias */
1477 if (!evsel->use_uncore_alias)
1480 /* The events must be from the same uncore block */
1481 if (!is_same_uncore_block(leader->pmu_name, evsel->pmu_name))
1487 * If the event's PMU name starts to repeat, it must be a new
1488 * event. That can be used to distinguish the leader from
1489 * other members, even they have the same event name.
1491 if ((leader != evsel) && (leader->pmu_name == evsel->pmu_name)) {
1495 /* The name is always alias name */
1496 WARN_ON(strcmp(leader->name, evsel->name));
1498 /* Store the leader event for each PMU */
1499 leaders[nr_pmu++] = (uintptr_t) evsel;
1502 /* only one event alias */
1503 if (nr_pmu == total_members) {
1504 parse_state->nr_groups--;
1509 * An uncore event alias is a joint name which means the same event
1510 * runs on all PMUs of a block.
1511 * Perf doesn't support mixed events from different PMUs in the same
1512 * group. The big group has to be split into multiple small groups
1513 * which only include the events from the same PMU.
1515 * Here the uncore event aliases must be from the same uncore block.
1516 * The number of PMUs must be same for each alias. The number of new
1517 * small groups equals to the number of PMUs.
1518 * Setting the leader event for corresponding members in each group.
1521 __evlist__for_each_entry(list, evsel) {
1524 evsel->leader = (struct evsel *) leaders[i++];
1527 /* The number of members and group name are same for each group */
1528 for (i = 0; i < nr_pmu; i++) {
1529 evsel = (struct evsel *) leaders[i];
1530 evsel->nr_members = total_members / nr_pmu;
1531 evsel->group_name = name ? strdup(name) : NULL;
1534 /* Take the new small groups into account */
1535 parse_state->nr_groups += nr_pmu - 1;
1544 void parse_events__set_leader(char *name, struct list_head *list,
1545 struct parse_events_state *parse_state)
1547 struct evsel *leader;
1549 if (list_empty(list)) {
1550 WARN_ONCE(true, "WARNING: failed to set leader: empty list");
1554 if (parse_events__set_leader_for_uncore_aliase(name, list, parse_state))
1557 __perf_evlist__set_leader(list);
1558 leader = list_entry(list->next, struct evsel, node);
1559 leader->group_name = name ? strdup(name) : NULL;
1562 /* list_event is assumed to point to malloc'ed memory */
1563 void parse_events_update_lists(struct list_head *list_event,
1564 struct list_head *list_all)
1567 * Called for single event definition. Update the
1568 * 'all event' list, and reinit the 'single event'
1569 * list, for next event definition.
1571 list_splice_tail(list_event, list_all);
1575 struct event_modifier {
1590 static int get_event_modifier(struct event_modifier *mod, char *str,
1591 struct evsel *evsel)
1593 int eu = evsel ? evsel->attr.exclude_user : 0;
1594 int ek = evsel ? evsel->attr.exclude_kernel : 0;
1595 int eh = evsel ? evsel->attr.exclude_hv : 0;
1596 int eH = evsel ? evsel->attr.exclude_host : 0;
1597 int eG = evsel ? evsel->attr.exclude_guest : 0;
1598 int eI = evsel ? evsel->attr.exclude_idle : 0;
1599 int precise = evsel ? evsel->attr.precise_ip : 0;
1600 int precise_max = 0;
1601 int sample_read = 0;
1602 int pinned = evsel ? evsel->attr.pinned : 0;
1604 int exclude = eu | ek | eh;
1605 int exclude_GH = evsel ? evsel->exclude_GH : 0;
1608 memset(mod, 0, sizeof(*mod));
1613 exclude = eu = ek = eh = 1;
1615 } else if (*str == 'k') {
1617 exclude = eu = ek = eh = 1;
1619 } else if (*str == 'h') {
1621 exclude = eu = ek = eh = 1;
1623 } else if (*str == 'G') {
1625 exclude_GH = eG = eH = 1;
1627 } else if (*str == 'H') {
1629 exclude_GH = eG = eH = 1;
1631 } else if (*str == 'I') {
1633 } else if (*str == 'p') {
1635 /* use of precise requires exclude_guest */
1638 } else if (*str == 'P') {
1640 } else if (*str == 'S') {
1642 } else if (*str == 'D') {
1644 } else if (*str == 'W') {
1655 * 0 - SAMPLE_IP can have arbitrary skid
1656 * 1 - SAMPLE_IP must have constant skid
1657 * 2 - SAMPLE_IP requested to have 0 skid
1658 * 3 - SAMPLE_IP must have 0 skid
1660 * See also PERF_RECORD_MISC_EXACT_IP
1671 mod->precise = precise;
1672 mod->precise_max = precise_max;
1673 mod->exclude_GH = exclude_GH;
1674 mod->sample_read = sample_read;
1675 mod->pinned = pinned;
1682 * Basic modifier sanity check to validate it contains only one
1683 * instance of any modifier (apart from 'p') present.
1685 static int check_modifier(char *str)
1689 /* The sizeof includes 0 byte as well. */
1690 if (strlen(str) > (sizeof("ukhGHpppPSDIW") - 1))
1694 if (*p != 'p' && strchr(p + 1, *p))
1702 int parse_events__modifier_event(struct list_head *list, char *str, bool add)
1704 struct evsel *evsel;
1705 struct event_modifier mod;
1710 if (check_modifier(str))
1713 if (!add && get_event_modifier(&mod, str, NULL))
1716 __evlist__for_each_entry(list, evsel) {
1717 if (add && get_event_modifier(&mod, str, evsel))
1720 evsel->attr.exclude_user = mod.eu;
1721 evsel->attr.exclude_kernel = mod.ek;
1722 evsel->attr.exclude_hv = mod.eh;
1723 evsel->attr.precise_ip = mod.precise;
1724 evsel->attr.exclude_host = mod.eH;
1725 evsel->attr.exclude_guest = mod.eG;
1726 evsel->attr.exclude_idle = mod.eI;
1727 evsel->exclude_GH = mod.exclude_GH;
1728 evsel->sample_read = mod.sample_read;
1729 evsel->precise_max = mod.precise_max;
1730 evsel->weak_group = mod.weak;
1732 if (perf_evsel__is_group_leader(evsel))
1733 evsel->attr.pinned = mod.pinned;
1739 int parse_events_name(struct list_head *list, char *name)
1741 struct evsel *evsel;
1743 __evlist__for_each_entry(list, evsel) {
1745 evsel->name = strdup(name);
1752 comp_pmu(const void *p1, const void *p2)
1754 struct perf_pmu_event_symbol *pmu1 = (struct perf_pmu_event_symbol *) p1;
1755 struct perf_pmu_event_symbol *pmu2 = (struct perf_pmu_event_symbol *) p2;
1757 return strcasecmp(pmu1->symbol, pmu2->symbol);
1760 static void perf_pmu__parse_cleanup(void)
1762 if (perf_pmu_events_list_num > 0) {
1763 struct perf_pmu_event_symbol *p;
1766 for (i = 0; i < perf_pmu_events_list_num; i++) {
1767 p = perf_pmu_events_list + i;
1770 zfree(&perf_pmu_events_list);
1771 perf_pmu_events_list_num = 0;
1775 #define SET_SYMBOL(str, stype) \
1784 * Read the pmu events list from sysfs
1785 * Save it into perf_pmu_events_list
1787 static void perf_pmu__parse_init(void)
1790 struct perf_pmu *pmu = NULL;
1791 struct perf_pmu_alias *alias;
1795 while ((pmu = perf_pmu__scan(pmu)) != NULL) {
1796 list_for_each_entry(alias, &pmu->aliases, list) {
1797 if (strchr(alias->name, '-'))
1804 perf_pmu_events_list_num = -1;
1807 perf_pmu_events_list = malloc(sizeof(struct perf_pmu_event_symbol) * len);
1808 if (!perf_pmu_events_list)
1810 perf_pmu_events_list_num = len;
1814 while ((pmu = perf_pmu__scan(pmu)) != NULL) {
1815 list_for_each_entry(alias, &pmu->aliases, list) {
1816 struct perf_pmu_event_symbol *p = perf_pmu_events_list + len;
1817 char *tmp = strchr(alias->name, '-');
1820 SET_SYMBOL(strndup(alias->name, tmp - alias->name),
1821 PMU_EVENT_SYMBOL_PREFIX);
1823 SET_SYMBOL(strdup(++tmp), PMU_EVENT_SYMBOL_SUFFIX);
1826 SET_SYMBOL(strdup(alias->name), PMU_EVENT_SYMBOL);
1831 qsort(perf_pmu_events_list, len,
1832 sizeof(struct perf_pmu_event_symbol), comp_pmu);
1836 perf_pmu__parse_cleanup();
1839 enum perf_pmu_event_symbol_type
1840 perf_pmu__parse_check(const char *name)
1842 struct perf_pmu_event_symbol p, *r;
1844 /* scan kernel pmu events from sysfs if needed */
1845 if (perf_pmu_events_list_num == 0)
1846 perf_pmu__parse_init();
1848 * name "cpu" could be prefix of cpu-cycles or cpu// events.
1849 * cpu-cycles has been handled by hardcode.
1850 * So it must be cpu// events, not kernel pmu event.
1852 if ((perf_pmu_events_list_num <= 0) || !strcmp(name, "cpu"))
1853 return PMU_EVENT_SYMBOL_ERR;
1855 p.symbol = strdup(name);
1856 r = bsearch(&p, perf_pmu_events_list,
1857 (size_t) perf_pmu_events_list_num,
1858 sizeof(struct perf_pmu_event_symbol), comp_pmu);
1860 return r ? r->type : PMU_EVENT_SYMBOL_ERR;
1863 static int parse_events__scanner(const char *str, void *parse_state, int start_token)
1865 YY_BUFFER_STATE buffer;
1869 ret = parse_events_lex_init_extra(start_token, &scanner);
1873 buffer = parse_events__scan_string(str, scanner);
1876 parse_events_debug = 1;
1878 ret = parse_events_parse(parse_state, scanner);
1880 parse_events__flush_buffer(buffer, scanner);
1881 parse_events__delete_buffer(buffer, scanner);
1882 parse_events_lex_destroy(scanner);
1887 * parse event config string, return a list of event terms.
1889 int parse_events_terms(struct list_head *terms, const char *str)
1891 struct parse_events_state parse_state = {
1896 ret = parse_events__scanner(str, &parse_state, PE_START_TERMS);
1898 list_splice(parse_state.terms, terms);
1899 zfree(&parse_state.terms);
1903 parse_events_terms__delete(parse_state.terms);
1907 int parse_events(struct evlist *evlist, const char *str,
1908 struct parse_events_error *err)
1910 struct parse_events_state parse_state = {
1911 .list = LIST_HEAD_INIT(parse_state.list),
1912 .idx = evlist->nr_entries,
1918 ret = parse_events__scanner(str, &parse_state, PE_START_EVENTS);
1919 perf_pmu__parse_cleanup();
1923 if (list_empty(&parse_state.list)) {
1924 WARN_ONCE(true, "WARNING: event parser found nothing\n");
1928 perf_evlist__splice_list_tail(evlist, &parse_state.list);
1929 evlist->nr_groups += parse_state.nr_groups;
1930 last = perf_evlist__last(evlist);
1931 last->cmdline_group_boundary = true;
1937 * There are 2 users - builtin-record and builtin-test objects.
1938 * Both call evlist__delete in case of error, so we dont
1944 #define MAX_WIDTH 1000
1945 static int get_term_width(void)
1949 get_term_dimensions(&ws);
1950 return ws.ws_col > MAX_WIDTH ? MAX_WIDTH : ws.ws_col;
1953 void parse_events_print_error(struct parse_events_error *err,
1956 const char *str = "invalid or unsupported event: ";
1957 char _buf[MAX_WIDTH];
1958 char *buf = (char *) event;
1962 /* -2 for extra '' in the final fprintf */
1963 int width = get_term_width() - 2;
1964 int len_event = strlen(event);
1965 int len_str, max_len, cut = 0;
1968 * Maximum error index indent, we will cut
1969 * the event string if it's bigger.
1971 int max_err_idx = 13;
1974 * Let's be specific with the message when
1975 * we have the precise error.
1977 str = "event syntax error: ";
1978 len_str = strlen(str);
1979 max_len = width - len_str;
1983 /* We're cutting from the beginning. */
1984 if (err->idx > max_err_idx)
1985 cut = err->idx - max_err_idx;
1987 strncpy(buf, event + cut, max_len);
1989 /* Mark cut parts with '..' on both sides. */
1991 buf[0] = buf[1] = '.';
1993 if ((len_event - cut) > max_len) {
1994 buf[max_len - 1] = buf[max_len - 2] = '.';
1998 idx = len_str + err->idx - cut;
2001 fprintf(stderr, "%s'%s'\n", str, buf);
2003 fprintf(stderr, "%*s\\___ %s\n", idx + 1, "", err->str);
2005 fprintf(stderr, "\n%s\n", err->help);
2013 int parse_events_option(const struct option *opt, const char *str,
2014 int unset __maybe_unused)
2016 struct evlist *evlist = *(struct evlist **)opt->value;
2017 struct parse_events_error err = { .idx = 0, };
2018 int ret = parse_events(evlist, str, &err);
2021 parse_events_print_error(&err, str);
2022 fprintf(stderr, "Run 'perf list' for a list of valid events\n");
2029 foreach_evsel_in_last_glob(struct evlist *evlist,
2030 int (*func)(struct evsel *evsel,
2034 struct evsel *last = NULL;
2038 * Don't return when list_empty, give func a chance to report
2039 * error when it found last == NULL.
2041 * So no need to WARN here, let *func do this.
2043 if (evlist->nr_entries > 0)
2044 last = perf_evlist__last(evlist);
2047 err = (*func)(last, arg);
2053 if (last->node.prev == &evlist->entries)
2055 last = list_entry(last->node.prev, struct evsel, node);
2056 } while (!last->cmdline_group_boundary);
2061 static int set_filter(struct evsel *evsel, const void *arg)
2063 const char *str = arg;
2065 int nr_addr_filters = 0;
2066 struct perf_pmu *pmu = NULL;
2068 if (evsel == NULL) {
2070 "--filter option should follow a -e tracepoint or HW tracer option\n");
2074 if (evsel->attr.type == PERF_TYPE_TRACEPOINT) {
2075 if (perf_evsel__append_tp_filter(evsel, str) < 0) {
2077 "not enough memory to hold filter string\n");
2084 while ((pmu = perf_pmu__scan(pmu)) != NULL)
2085 if (pmu->type == evsel->attr.type) {
2091 perf_pmu__scan_file(pmu, "nr_addr_filters",
2092 "%d", &nr_addr_filters);
2094 if (!nr_addr_filters) {
2096 "This CPU does not support address filtering\n");
2100 if (perf_evsel__append_addr_filter(evsel, str) < 0) {
2102 "not enough memory to hold filter string\n");
2109 int parse_filter(const struct option *opt, const char *str,
2110 int unset __maybe_unused)
2112 struct evlist *evlist = *(struct evlist **)opt->value;
2114 return foreach_evsel_in_last_glob(evlist, set_filter,
2118 static int add_exclude_perf_filter(struct evsel *evsel,
2119 const void *arg __maybe_unused)
2121 char new_filter[64];
2123 if (evsel == NULL || evsel->attr.type != PERF_TYPE_TRACEPOINT) {
2125 "--exclude-perf option should follow a -e tracepoint option\n");
2129 snprintf(new_filter, sizeof(new_filter), "common_pid != %d", getpid());
2131 if (perf_evsel__append_tp_filter(evsel, new_filter) < 0) {
2133 "not enough memory to hold filter string\n");
2140 int exclude_perf(const struct option *opt,
2141 const char *arg __maybe_unused,
2142 int unset __maybe_unused)
2144 struct evlist *evlist = *(struct evlist **)opt->value;
2146 return foreach_evsel_in_last_glob(evlist, add_exclude_perf_filter,
2150 static const char * const event_type_descriptors[] = {
2154 "Hardware cache event",
2155 "Raw hardware event descriptor",
2156 "Hardware breakpoint",
2159 static int cmp_string(const void *a, const void *b)
2161 const char * const *as = a;
2162 const char * const *bs = b;
2164 return strcmp(*as, *bs);
2168 * Print the events from <debugfs_mount_point>/tracing/events
2171 void print_tracepoint_events(const char *subsys_glob, const char *event_glob,
2174 DIR *sys_dir, *evt_dir;
2175 struct dirent *sys_dirent, *evt_dirent;
2176 char evt_path[MAXPATHLEN];
2178 char **evt_list = NULL;
2179 unsigned int evt_i = 0, evt_num = 0;
2180 bool evt_num_known = false;
2183 sys_dir = tracing_events__opendir();
2187 if (evt_num_known) {
2188 evt_list = zalloc(sizeof(char *) * evt_num);
2190 goto out_close_sys_dir;
2193 for_each_subsystem(sys_dir, sys_dirent) {
2194 if (subsys_glob != NULL &&
2195 !strglobmatch(sys_dirent->d_name, subsys_glob))
2198 dir_path = get_events_file(sys_dirent->d_name);
2201 evt_dir = opendir(dir_path);
2205 for_each_event(dir_path, evt_dir, evt_dirent) {
2206 if (event_glob != NULL &&
2207 !strglobmatch(evt_dirent->d_name, event_glob))
2210 if (!evt_num_known) {
2215 snprintf(evt_path, MAXPATHLEN, "%s:%s",
2216 sys_dirent->d_name, evt_dirent->d_name);
2218 evt_list[evt_i] = strdup(evt_path);
2219 if (evt_list[evt_i] == NULL) {
2220 put_events_file(dir_path);
2221 goto out_close_evt_dir;
2227 put_events_file(dir_path);
2231 if (!evt_num_known) {
2232 evt_num_known = true;
2235 qsort(evt_list, evt_num, sizeof(char *), cmp_string);
2237 while (evt_i < evt_num) {
2239 printf("%s ", evt_list[evt_i++]);
2242 printf(" %-50s [%s]\n", evt_list[evt_i++],
2243 event_type_descriptors[PERF_TYPE_TRACEPOINT]);
2245 if (evt_num && pager_in_use())
2250 for (evt_i = 0; evt_i < evt_num; evt_i++)
2251 zfree(&evt_list[evt_i]);
2260 printf("FATAL: not enough memory to print %s\n",
2261 event_type_descriptors[PERF_TYPE_TRACEPOINT]);
2267 * Check whether event is in <debugfs_mount_point>/tracing/events
2270 int is_valid_tracepoint(const char *event_string)
2272 DIR *sys_dir, *evt_dir;
2273 struct dirent *sys_dirent, *evt_dirent;
2274 char evt_path[MAXPATHLEN];
2277 sys_dir = tracing_events__opendir();
2281 for_each_subsystem(sys_dir, sys_dirent) {
2282 dir_path = get_events_file(sys_dirent->d_name);
2285 evt_dir = opendir(dir_path);
2289 for_each_event(dir_path, evt_dir, evt_dirent) {
2290 snprintf(evt_path, MAXPATHLEN, "%s:%s",
2291 sys_dirent->d_name, evt_dirent->d_name);
2292 if (!strcmp(evt_path, event_string)) {
2300 put_events_file(dir_path);
2306 static bool is_event_supported(u8 type, unsigned config)
2310 struct evsel *evsel;
2311 struct perf_event_attr attr = {
2316 struct perf_thread_map *tmap = thread_map__new_by_tid(0);
2321 evsel = perf_evsel__new(&attr);
2323 open_return = perf_evsel__open(evsel, NULL, tmap);
2324 ret = open_return >= 0;
2326 if (open_return == -EACCES) {
2328 * This happens if the paranoid value
2329 * /proc/sys/kernel/perf_event_paranoid is set to 2
2330 * Re-run with exclude_kernel set; we don't do that
2331 * by default as some ARM machines do not support it.
2334 evsel->attr.exclude_kernel = 1;
2335 ret = perf_evsel__open(evsel, NULL, tmap) >= 0;
2337 perf_evsel__delete(evsel);
2340 thread_map__put(tmap);
2344 void print_sdt_events(const char *subsys_glob, const char *event_glob,
2347 struct probe_cache *pcache;
2348 struct probe_cache_entry *ent;
2349 struct strlist *bidlist, *sdtlist;
2350 struct strlist_config cfg = {.dont_dupstr = true};
2351 struct str_node *nd, *nd2;
2352 char *buf, *path, *ptr = NULL;
2353 bool show_detail = false;
2356 sdtlist = strlist__new(NULL, &cfg);
2358 pr_debug("Failed to allocate new strlist for SDT\n");
2361 bidlist = build_id_cache__list_all(true);
2363 pr_debug("Failed to get buildids: %d\n", errno);
2366 strlist__for_each_entry(nd, bidlist) {
2367 pcache = probe_cache__new(nd->s, NULL);
2370 list_for_each_entry(ent, &pcache->entries, node) {
2374 !strglobmatch(ent->pev.group, subsys_glob))
2377 !strglobmatch(ent->pev.event, event_glob))
2379 ret = asprintf(&buf, "%s:%s@%s", ent->pev.group,
2380 ent->pev.event, nd->s);
2382 strlist__add(sdtlist, buf);
2384 probe_cache__delete(pcache);
2386 strlist__delete(bidlist);
2388 strlist__for_each_entry(nd, sdtlist) {
2389 buf = strchr(nd->s, '@');
2393 printf("%s ", nd->s);
2396 nd2 = strlist__next(nd);
2398 ptr = strchr(nd2->s, '@');
2401 if (strcmp(nd->s, nd2->s) == 0)
2405 path = build_id_cache__origname(buf);
2406 ret = asprintf(&buf, "%s@%s(%.12s)", nd->s, path, buf);
2408 printf(" %-50s [%s]\n", buf, "SDT event");
2413 printf(" %-50s [%s]\n", nd->s, "SDT event");
2415 if (strcmp(nd->s, nd2->s) != 0)
2416 show_detail = false;
2421 strlist__delete(sdtlist);
2424 int print_hwcache_events(const char *event_glob, bool name_only)
2426 unsigned int type, op, i, evt_i = 0, evt_num = 0;
2428 char **evt_list = NULL;
2429 bool evt_num_known = false;
2432 if (evt_num_known) {
2433 evt_list = zalloc(sizeof(char *) * evt_num);
2438 for (type = 0; type < PERF_COUNT_HW_CACHE_MAX; type++) {
2439 for (op = 0; op < PERF_COUNT_HW_CACHE_OP_MAX; op++) {
2440 /* skip invalid cache type */
2441 if (!perf_evsel__is_cache_op_valid(type, op))
2444 for (i = 0; i < PERF_COUNT_HW_CACHE_RESULT_MAX; i++) {
2445 __perf_evsel__hw_cache_type_op_res_name(type, op, i,
2446 name, sizeof(name));
2447 if (event_glob != NULL && !strglobmatch(name, event_glob))
2450 if (!is_event_supported(PERF_TYPE_HW_CACHE,
2451 type | (op << 8) | (i << 16)))
2454 if (!evt_num_known) {
2459 evt_list[evt_i] = strdup(name);
2460 if (evt_list[evt_i] == NULL)
2467 if (!evt_num_known) {
2468 evt_num_known = true;
2471 qsort(evt_list, evt_num, sizeof(char *), cmp_string);
2473 while (evt_i < evt_num) {
2475 printf("%s ", evt_list[evt_i++]);
2478 printf(" %-50s [%s]\n", evt_list[evt_i++],
2479 event_type_descriptors[PERF_TYPE_HW_CACHE]);
2481 if (evt_num && pager_in_use())
2486 for (evt_i = 0; evt_i < evt_num; evt_i++)
2487 zfree(&evt_list[evt_i]);
2492 printf("FATAL: not enough memory to print %s\n", event_type_descriptors[PERF_TYPE_HW_CACHE]);
2498 static void print_tool_event(const char *name, const char *event_glob,
2501 if (event_glob && !strglobmatch(name, event_glob))
2504 printf("%s ", name);
2506 printf(" %-50s [%s]\n", name, "Tool event");
2510 void print_tool_events(const char *event_glob, bool name_only)
2512 print_tool_event("duration_time", event_glob, name_only);
2517 void print_symbol_events(const char *event_glob, unsigned type,
2518 struct event_symbol *syms, unsigned max,
2521 unsigned int i, evt_i = 0, evt_num = 0;
2522 char name[MAX_NAME_LEN];
2523 char **evt_list = NULL;
2524 bool evt_num_known = false;
2527 if (evt_num_known) {
2528 evt_list = zalloc(sizeof(char *) * evt_num);
2534 for (i = 0; i < max; i++, syms++) {
2536 if (event_glob != NULL && syms->symbol != NULL &&
2537 !(strglobmatch(syms->symbol, event_glob) ||
2538 (syms->alias && strglobmatch(syms->alias, event_glob))))
2541 if (!is_event_supported(type, i))
2544 if (!evt_num_known) {
2549 if (!name_only && strlen(syms->alias))
2550 snprintf(name, MAX_NAME_LEN, "%s OR %s", syms->symbol, syms->alias);
2552 strlcpy(name, syms->symbol, MAX_NAME_LEN);
2554 evt_list[evt_i] = strdup(name);
2555 if (evt_list[evt_i] == NULL)
2560 if (!evt_num_known) {
2561 evt_num_known = true;
2564 qsort(evt_list, evt_num, sizeof(char *), cmp_string);
2566 while (evt_i < evt_num) {
2568 printf("%s ", evt_list[evt_i++]);
2571 printf(" %-50s [%s]\n", evt_list[evt_i++], event_type_descriptors[type]);
2573 if (evt_num && pager_in_use())
2578 for (evt_i = 0; evt_i < evt_num; evt_i++)
2579 zfree(&evt_list[evt_i]);
2584 printf("FATAL: not enough memory to print %s\n", event_type_descriptors[type]);
2590 * Print the help text for the event symbols:
2592 void print_events(const char *event_glob, bool name_only, bool quiet_flag,
2593 bool long_desc, bool details_flag)
2595 print_symbol_events(event_glob, PERF_TYPE_HARDWARE,
2596 event_symbols_hw, PERF_COUNT_HW_MAX, name_only);
2598 print_symbol_events(event_glob, PERF_TYPE_SOFTWARE,
2599 event_symbols_sw, PERF_COUNT_SW_MAX, name_only);
2600 print_tool_events(event_glob, name_only);
2602 print_hwcache_events(event_glob, name_only);
2604 print_pmu_events(event_glob, name_only, quiet_flag, long_desc,
2607 if (event_glob != NULL)
2611 printf(" %-50s [%s]\n",
2613 event_type_descriptors[PERF_TYPE_RAW]);
2614 printf(" %-50s [%s]\n",
2615 "cpu/t1=v1[,t2=v2,t3 ...]/modifier",
2616 event_type_descriptors[PERF_TYPE_RAW]);
2618 printf(" (see 'man perf-list' on how to encode it)\n\n");
2620 printf(" %-50s [%s]\n",
2621 "mem:<addr>[/len][:access]",
2622 event_type_descriptors[PERF_TYPE_BREAKPOINT]);
2627 print_tracepoint_events(NULL, NULL, name_only);
2629 print_sdt_events(NULL, NULL, name_only);
2631 metricgroup__print(true, true, NULL, name_only, details_flag);
2634 int parse_events__is_hardcoded_term(struct parse_events_term *term)
2636 return term->type_term != PARSE_EVENTS__TERM_TYPE_USER;
2639 static int new_term(struct parse_events_term **_term,
2640 struct parse_events_term *temp,
2643 struct parse_events_term *term;
2645 term = malloc(sizeof(*term));
2650 INIT_LIST_HEAD(&term->list);
2653 switch (term->type_val) {
2654 case PARSE_EVENTS__TERM_TYPE_NUM:
2655 term->val.num = num;
2657 case PARSE_EVENTS__TERM_TYPE_STR:
2658 term->val.str = str;
2669 int parse_events_term__num(struct parse_events_term **term,
2670 int type_term, char *config, u64 num,
2672 void *loc_term_, void *loc_val_)
2674 YYLTYPE *loc_term = loc_term_;
2675 YYLTYPE *loc_val = loc_val_;
2677 struct parse_events_term temp = {
2678 .type_val = PARSE_EVENTS__TERM_TYPE_NUM,
2679 .type_term = type_term,
2681 .no_value = no_value,
2682 .err_term = loc_term ? loc_term->first_column : 0,
2683 .err_val = loc_val ? loc_val->first_column : 0,
2686 return new_term(term, &temp, NULL, num);
2689 int parse_events_term__str(struct parse_events_term **term,
2690 int type_term, char *config, char *str,
2691 void *loc_term_, void *loc_val_)
2693 YYLTYPE *loc_term = loc_term_;
2694 YYLTYPE *loc_val = loc_val_;
2696 struct parse_events_term temp = {
2697 .type_val = PARSE_EVENTS__TERM_TYPE_STR,
2698 .type_term = type_term,
2700 .err_term = loc_term ? loc_term->first_column : 0,
2701 .err_val = loc_val ? loc_val->first_column : 0,
2704 return new_term(term, &temp, str, 0);
2707 int parse_events_term__sym_hw(struct parse_events_term **term,
2708 char *config, unsigned idx)
2710 struct event_symbol *sym;
2711 struct parse_events_term temp = {
2712 .type_val = PARSE_EVENTS__TERM_TYPE_STR,
2713 .type_term = PARSE_EVENTS__TERM_TYPE_USER,
2714 .config = config ?: (char *) "event",
2717 BUG_ON(idx >= PERF_COUNT_HW_MAX);
2718 sym = &event_symbols_hw[idx];
2720 return new_term(term, &temp, (char *) sym->symbol, 0);
2723 int parse_events_term__clone(struct parse_events_term **new,
2724 struct parse_events_term *term)
2726 struct parse_events_term temp = {
2727 .type_val = term->type_val,
2728 .type_term = term->type_term,
2729 .config = term->config,
2730 .err_term = term->err_term,
2731 .err_val = term->err_val,
2734 return new_term(new, &temp, term->val.str, term->val.num);
2737 int parse_events_copy_term_list(struct list_head *old,
2738 struct list_head **new)
2740 struct parse_events_term *term, *n;
2748 *new = malloc(sizeof(struct list_head));
2751 INIT_LIST_HEAD(*new);
2753 list_for_each_entry (term, old, list) {
2754 ret = parse_events_term__clone(&n, term);
2757 list_add_tail(&n->list, *new);
2762 void parse_events_terms__purge(struct list_head *terms)
2764 struct parse_events_term *term, *h;
2766 list_for_each_entry_safe(term, h, terms, list) {
2767 if (term->array.nr_ranges)
2768 zfree(&term->array.ranges);
2769 list_del_init(&term->list);
2774 void parse_events_terms__delete(struct list_head *terms)
2778 parse_events_terms__purge(terms);
2782 void parse_events__clear_array(struct parse_events_array *a)
2787 void parse_events_evlist_error(struct parse_events_state *parse_state,
2788 int idx, const char *str)
2790 struct parse_events_error *err = parse_state->error;
2795 err->str = strdup(str);
2796 WARN_ONCE(!err->str, "WARNING: failed to allocate error string");
2799 static void config_terms_list(char *buf, size_t buf_sz)
2805 for (i = 0; i < __PARSE_EVENTS__TERM_TYPE_NR; i++) {
2806 const char *name = config_term_names[i];
2808 if (!config_term_avail(i, NULL))
2815 if (strlen(buf) + strlen(name) + 2 >= buf_sz)
2827 * Return string contains valid config terms of an event.
2828 * @additional_terms: For terms such as PMU sysfs terms.
2830 char *parse_events_formats_error_string(char *additional_terms)
2833 /* "no-overwrite" is the longest name */
2834 char static_terms[__PARSE_EVENTS__TERM_TYPE_NR *
2835 (sizeof("no-overwrite") - 1)];
2837 config_terms_list(static_terms, sizeof(static_terms));
2839 if (additional_terms) {
2840 if (asprintf(&str, "valid terms: %s,%s",
2841 additional_terms, static_terms) < 0)
2844 if (asprintf(&str, "valid terms: %s", static_terms) < 0)