1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/hw_breakpoint.h>
10 #include <sys/param.h>
15 #include <subcmd/parse-options.h>
16 #include "parse-events.h"
17 #include <subcmd/exec-cmd.h>
23 #include "bpf-loader.h"
25 #include <api/fs/tracing_path.h>
26 #include "parse-events-bison.h"
27 #define YY_EXTRA_TYPE int
28 #include "parse-events-flex.h"
30 #include "thread_map.h"
32 #include "probe-file.h"
34 #include "util/parse-branch-options.h"
35 #include "metricgroup.h"
37 #define MAX_NAME_LEN 100
40 extern int parse_events_debug;
42 int parse_events_parse(void *parse_state, void *scanner);
43 static int get_config_terms(struct list_head *head_config,
44 struct list_head *head_terms __maybe_unused);
46 static struct perf_pmu_event_symbol *perf_pmu_events_list;
48 * The variable indicates the number of supported pmu event symbols.
49 * 0 means not initialized and ready to init
50 * -1 means failed to init, don't try anymore
51 * >0 is the number of supported pmu event symbols
53 static int perf_pmu_events_list_num;
55 struct event_symbol event_symbols_hw[PERF_COUNT_HW_MAX] = {
56 [PERF_COUNT_HW_CPU_CYCLES] = {
57 .symbol = "cpu-cycles",
60 [PERF_COUNT_HW_INSTRUCTIONS] = {
61 .symbol = "instructions",
64 [PERF_COUNT_HW_CACHE_REFERENCES] = {
65 .symbol = "cache-references",
68 [PERF_COUNT_HW_CACHE_MISSES] = {
69 .symbol = "cache-misses",
72 [PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = {
73 .symbol = "branch-instructions",
76 [PERF_COUNT_HW_BRANCH_MISSES] = {
77 .symbol = "branch-misses",
80 [PERF_COUNT_HW_BUS_CYCLES] = {
81 .symbol = "bus-cycles",
84 [PERF_COUNT_HW_STALLED_CYCLES_FRONTEND] = {
85 .symbol = "stalled-cycles-frontend",
86 .alias = "idle-cycles-frontend",
88 [PERF_COUNT_HW_STALLED_CYCLES_BACKEND] = {
89 .symbol = "stalled-cycles-backend",
90 .alias = "idle-cycles-backend",
92 [PERF_COUNT_HW_REF_CPU_CYCLES] = {
93 .symbol = "ref-cycles",
98 struct event_symbol event_symbols_sw[PERF_COUNT_SW_MAX] = {
99 [PERF_COUNT_SW_CPU_CLOCK] = {
100 .symbol = "cpu-clock",
103 [PERF_COUNT_SW_TASK_CLOCK] = {
104 .symbol = "task-clock",
107 [PERF_COUNT_SW_PAGE_FAULTS] = {
108 .symbol = "page-faults",
111 [PERF_COUNT_SW_CONTEXT_SWITCHES] = {
112 .symbol = "context-switches",
115 [PERF_COUNT_SW_CPU_MIGRATIONS] = {
116 .symbol = "cpu-migrations",
117 .alias = "migrations",
119 [PERF_COUNT_SW_PAGE_FAULTS_MIN] = {
120 .symbol = "minor-faults",
123 [PERF_COUNT_SW_PAGE_FAULTS_MAJ] = {
124 .symbol = "major-faults",
127 [PERF_COUNT_SW_ALIGNMENT_FAULTS] = {
128 .symbol = "alignment-faults",
131 [PERF_COUNT_SW_EMULATION_FAULTS] = {
132 .symbol = "emulation-faults",
135 [PERF_COUNT_SW_DUMMY] = {
139 [PERF_COUNT_SW_BPF_OUTPUT] = {
140 .symbol = "bpf-output",
145 #define __PERF_EVENT_FIELD(config, name) \
146 ((config & PERF_EVENT_##name##_MASK) >> PERF_EVENT_##name##_SHIFT)
148 #define PERF_EVENT_RAW(config) __PERF_EVENT_FIELD(config, RAW)
149 #define PERF_EVENT_CONFIG(config) __PERF_EVENT_FIELD(config, CONFIG)
150 #define PERF_EVENT_TYPE(config) __PERF_EVENT_FIELD(config, TYPE)
151 #define PERF_EVENT_ID(config) __PERF_EVENT_FIELD(config, EVENT)
153 #define for_each_subsystem(sys_dir, sys_dirent) \
154 while ((sys_dirent = readdir(sys_dir)) != NULL) \
155 if (sys_dirent->d_type == DT_DIR && \
156 (strcmp(sys_dirent->d_name, ".")) && \
157 (strcmp(sys_dirent->d_name, "..")))
159 static int tp_event_has_id(const char *dir_path, struct dirent *evt_dir)
161 char evt_path[MAXPATHLEN];
164 snprintf(evt_path, MAXPATHLEN, "%s/%s/id", dir_path, evt_dir->d_name);
165 fd = open(evt_path, O_RDONLY);
173 #define for_each_event(dir_path, evt_dir, evt_dirent) \
174 while ((evt_dirent = readdir(evt_dir)) != NULL) \
175 if (evt_dirent->d_type == DT_DIR && \
176 (strcmp(evt_dirent->d_name, ".")) && \
177 (strcmp(evt_dirent->d_name, "..")) && \
178 (!tp_event_has_id(dir_path, evt_dirent)))
180 #define MAX_EVENT_LENGTH 512
183 struct tracepoint_path *tracepoint_id_to_path(u64 config)
185 struct tracepoint_path *path = NULL;
186 DIR *sys_dir, *evt_dir;
187 struct dirent *sys_dirent, *evt_dirent;
191 char evt_path[MAXPATHLEN];
194 sys_dir = tracing_events__opendir();
198 for_each_subsystem(sys_dir, sys_dirent) {
199 dir_path = get_events_file(sys_dirent->d_name);
202 evt_dir = opendir(dir_path);
206 for_each_event(dir_path, evt_dir, evt_dirent) {
208 scnprintf(evt_path, MAXPATHLEN, "%s/%s/id", dir_path,
210 fd = open(evt_path, O_RDONLY);
213 if (read(fd, id_buf, sizeof(id_buf)) < 0) {
220 put_events_file(dir_path);
223 path = zalloc(sizeof(*path));
226 path->system = malloc(MAX_EVENT_LENGTH);
231 path->name = malloc(MAX_EVENT_LENGTH);
233 zfree(&path->system);
237 strncpy(path->system, sys_dirent->d_name,
239 strncpy(path->name, evt_dirent->d_name,
246 put_events_file(dir_path);
253 struct tracepoint_path *tracepoint_name_to_path(const char *name)
255 struct tracepoint_path *path = zalloc(sizeof(*path));
256 char *str = strchr(name, ':');
258 if (path == NULL || str == NULL) {
263 path->system = strndup(name, str - name);
264 path->name = strdup(str+1);
266 if (path->system == NULL || path->name == NULL) {
267 zfree(&path->system);
275 const char *event_type(int type)
278 case PERF_TYPE_HARDWARE:
281 case PERF_TYPE_SOFTWARE:
284 case PERF_TYPE_TRACEPOINT:
287 case PERF_TYPE_HW_CACHE:
288 return "hardware-cache";
297 static int parse_events__is_name_term(struct parse_events_term *term)
299 return term->type_term == PARSE_EVENTS__TERM_TYPE_NAME;
302 static char *get_config_name(struct list_head *head_terms)
304 struct parse_events_term *term;
309 list_for_each_entry(term, head_terms, list)
310 if (parse_events__is_name_term(term))
311 return term->val.str;
316 static struct perf_evsel *
317 __add_event(struct list_head *list, int *idx,
318 struct perf_event_attr *attr,
319 char *name, struct perf_pmu *pmu,
320 struct list_head *config_terms, bool auto_merge_stats)
322 struct perf_evsel *evsel;
323 struct cpu_map *cpus = pmu ? pmu->cpus : NULL;
325 event_attr_init(attr);
327 evsel = perf_evsel__new_idx(attr, *idx);
332 evsel->cpus = cpu_map__get(cpus);
333 evsel->own_cpus = cpu_map__get(cpus);
334 evsel->system_wide = pmu ? pmu->is_uncore : false;
335 evsel->auto_merge_stats = auto_merge_stats;
338 evsel->name = strdup(name);
341 list_splice(config_terms, &evsel->config_terms);
343 list_add_tail(&evsel->node, list);
347 static int add_event(struct list_head *list, int *idx,
348 struct perf_event_attr *attr, char *name,
349 struct list_head *config_terms)
351 return __add_event(list, idx, attr, name, NULL, config_terms, false) ? 0 : -ENOMEM;
354 static int parse_aliases(char *str, const char *names[][PERF_EVSEL__MAX_ALIASES], int size)
359 for (i = 0; i < size; i++) {
360 for (j = 0; j < PERF_EVSEL__MAX_ALIASES && names[i][j]; j++) {
361 n = strlen(names[i][j]);
362 if (n > longest && !strncasecmp(str, names[i][j], n))
372 typedef int config_term_func_t(struct perf_event_attr *attr,
373 struct parse_events_term *term,
374 struct parse_events_error *err);
375 static int config_term_common(struct perf_event_attr *attr,
376 struct parse_events_term *term,
377 struct parse_events_error *err);
378 static int config_attr(struct perf_event_attr *attr,
379 struct list_head *head,
380 struct parse_events_error *err,
381 config_term_func_t config_term);
383 int parse_events_add_cache(struct list_head *list, int *idx,
384 char *type, char *op_result1, char *op_result2,
385 struct parse_events_error *err,
386 struct list_head *head_config)
388 struct perf_event_attr attr;
389 LIST_HEAD(config_terms);
390 char name[MAX_NAME_LEN], *config_name;
391 int cache_type = -1, cache_op = -1, cache_result = -1;
392 char *op_result[2] = { op_result1, op_result2 };
396 * No fallback - if we cannot get a clear cache type
399 cache_type = parse_aliases(type, perf_evsel__hw_cache,
400 PERF_COUNT_HW_CACHE_MAX);
401 if (cache_type == -1)
404 config_name = get_config_name(head_config);
405 n = snprintf(name, MAX_NAME_LEN, "%s", type);
407 for (i = 0; (i < 2) && (op_result[i]); i++) {
408 char *str = op_result[i];
410 n += snprintf(name + n, MAX_NAME_LEN - n, "-%s", str);
412 if (cache_op == -1) {
413 cache_op = parse_aliases(str, perf_evsel__hw_cache_op,
414 PERF_COUNT_HW_CACHE_OP_MAX);
416 if (!perf_evsel__is_cache_op_valid(cache_type, cache_op))
422 if (cache_result == -1) {
423 cache_result = parse_aliases(str, perf_evsel__hw_cache_result,
424 PERF_COUNT_HW_CACHE_RESULT_MAX);
425 if (cache_result >= 0)
431 * Fall back to reads:
434 cache_op = PERF_COUNT_HW_CACHE_OP_READ;
437 * Fall back to accesses:
439 if (cache_result == -1)
440 cache_result = PERF_COUNT_HW_CACHE_RESULT_ACCESS;
442 memset(&attr, 0, sizeof(attr));
443 attr.config = cache_type | (cache_op << 8) | (cache_result << 16);
444 attr.type = PERF_TYPE_HW_CACHE;
447 if (config_attr(&attr, head_config, err,
451 if (get_config_terms(head_config, &config_terms))
454 return add_event(list, idx, &attr, config_name ? : name, &config_terms);
457 static void tracepoint_error(struct parse_events_error *e, int err,
458 const char *sys, const char *name)
466 * We get error directly from syscall errno ( > 0),
467 * or from encoded pointer's error ( < 0).
473 e->str = strdup("can't access trace events");
476 e->str = strdup("unknown tracepoint");
479 e->str = strdup("failed to add tracepoint");
483 tracing_path__strerror_open_tp(err, help, sizeof(help), sys, name);
484 e->help = strdup(help);
487 static int add_tracepoint(struct list_head *list, int *idx,
488 const char *sys_name, const char *evt_name,
489 struct parse_events_error *err,
490 struct list_head *head_config)
492 struct perf_evsel *evsel;
494 evsel = perf_evsel__newtp_idx(sys_name, evt_name, (*idx)++);
496 tracepoint_error(err, PTR_ERR(evsel), sys_name, evt_name);
497 return PTR_ERR(evsel);
501 LIST_HEAD(config_terms);
503 if (get_config_terms(head_config, &config_terms))
505 list_splice(&config_terms, &evsel->config_terms);
508 list_add_tail(&evsel->node, list);
512 static int add_tracepoint_multi_event(struct list_head *list, int *idx,
513 const char *sys_name, const char *evt_name,
514 struct parse_events_error *err,
515 struct list_head *head_config)
518 struct dirent *evt_ent;
520 int ret = 0, found = 0;
522 evt_path = get_events_file(sys_name);
524 tracepoint_error(err, errno, sys_name, evt_name);
527 evt_dir = opendir(evt_path);
529 put_events_file(evt_path);
530 tracepoint_error(err, errno, sys_name, evt_name);
534 while (!ret && (evt_ent = readdir(evt_dir))) {
535 if (!strcmp(evt_ent->d_name, ".")
536 || !strcmp(evt_ent->d_name, "..")
537 || !strcmp(evt_ent->d_name, "enable")
538 || !strcmp(evt_ent->d_name, "filter"))
541 if (!strglobmatch(evt_ent->d_name, evt_name))
546 ret = add_tracepoint(list, idx, sys_name, evt_ent->d_name,
551 tracepoint_error(err, ENOENT, sys_name, evt_name);
555 put_events_file(evt_path);
560 static int add_tracepoint_event(struct list_head *list, int *idx,
561 const char *sys_name, const char *evt_name,
562 struct parse_events_error *err,
563 struct list_head *head_config)
565 return strpbrk(evt_name, "*?") ?
566 add_tracepoint_multi_event(list, idx, sys_name, evt_name,
568 add_tracepoint(list, idx, sys_name, evt_name,
572 static int add_tracepoint_multi_sys(struct list_head *list, int *idx,
573 const char *sys_name, const char *evt_name,
574 struct parse_events_error *err,
575 struct list_head *head_config)
577 struct dirent *events_ent;
581 events_dir = tracing_events__opendir();
583 tracepoint_error(err, errno, sys_name, evt_name);
587 while (!ret && (events_ent = readdir(events_dir))) {
588 if (!strcmp(events_ent->d_name, ".")
589 || !strcmp(events_ent->d_name, "..")
590 || !strcmp(events_ent->d_name, "enable")
591 || !strcmp(events_ent->d_name, "header_event")
592 || !strcmp(events_ent->d_name, "header_page"))
595 if (!strglobmatch(events_ent->d_name, sys_name))
598 ret = add_tracepoint_event(list, idx, events_ent->d_name,
599 evt_name, err, head_config);
602 closedir(events_dir);
606 struct __add_bpf_event_param {
607 struct parse_events_state *parse_state;
608 struct list_head *list;
609 struct list_head *head_config;
612 static int add_bpf_event(const char *group, const char *event, int fd,
615 LIST_HEAD(new_evsels);
616 struct __add_bpf_event_param *param = _param;
617 struct parse_events_state *parse_state = param->parse_state;
618 struct list_head *list = param->list;
619 struct perf_evsel *pos;
622 pr_debug("add bpf event %s:%s and attach bpf program %d\n",
625 err = parse_events_add_tracepoint(&new_evsels, &parse_state->idx, group,
626 event, parse_state->error,
629 struct perf_evsel *evsel, *tmp;
631 pr_debug("Failed to add BPF event %s:%s\n",
633 list_for_each_entry_safe(evsel, tmp, &new_evsels, node) {
634 list_del(&evsel->node);
635 perf_evsel__delete(evsel);
639 pr_debug("adding %s:%s\n", group, event);
641 list_for_each_entry(pos, &new_evsels, node) {
642 pr_debug("adding %s:%s to %p\n",
646 list_splice(&new_evsels, list);
650 int parse_events_load_bpf_obj(struct parse_events_state *parse_state,
651 struct list_head *list,
652 struct bpf_object *obj,
653 struct list_head *head_config)
657 struct __add_bpf_event_param param = {parse_state, list, head_config};
658 static bool registered_unprobe_atexit = false;
660 if (IS_ERR(obj) || !obj) {
661 snprintf(errbuf, sizeof(errbuf),
662 "Internal error: load bpf obj with NULL");
668 * Register atexit handler before calling bpf__probe() so
669 * bpf__probe() don't need to unprobe probe points its already
670 * created when failure.
672 if (!registered_unprobe_atexit) {
674 registered_unprobe_atexit = true;
677 err = bpf__probe(obj);
679 bpf__strerror_probe(obj, err, errbuf, sizeof(errbuf));
683 err = bpf__load(obj);
685 bpf__strerror_load(obj, err, errbuf, sizeof(errbuf));
689 err = bpf__foreach_event(obj, add_bpf_event, ¶m);
691 snprintf(errbuf, sizeof(errbuf),
692 "Attach events in BPF object failed");
698 parse_state->error->help = strdup("(add -v to see detail)");
699 parse_state->error->str = strdup(errbuf);
704 parse_events_config_bpf(struct parse_events_state *parse_state,
705 struct bpf_object *obj,
706 struct list_head *head_config)
708 struct parse_events_term *term;
711 if (!head_config || list_empty(head_config))
714 list_for_each_entry(term, head_config, list) {
718 if (term->type_term != PARSE_EVENTS__TERM_TYPE_USER) {
719 snprintf(errbuf, sizeof(errbuf),
720 "Invalid config term for BPF object");
721 errbuf[BUFSIZ - 1] = '\0';
723 parse_state->error->idx = term->err_term;
724 parse_state->error->str = strdup(errbuf);
728 err = bpf__config_obj(obj, term, parse_state->evlist, &error_pos);
730 bpf__strerror_config_obj(obj, term, parse_state->evlist,
731 &error_pos, err, errbuf,
733 parse_state->error->help = strdup(
734 "Hint:\tValid config terms:\n"
735 " \tmap:[<arraymap>].value<indices>=[value]\n"
736 " \tmap:[<eventmap>].event<indices>=[event]\n"
738 " \twhere <indices> is something like [0,3...5] or [all]\n"
739 " \t(add -v to see detail)");
740 parse_state->error->str = strdup(errbuf);
741 if (err == -BPF_LOADER_ERRNO__OBJCONF_MAP_VALUE)
742 parse_state->error->idx = term->err_val;
744 parse_state->error->idx = term->err_term + error_pos;
752 * Split config terms:
753 * perf record -e bpf.c/call-graph=fp,map:array.value[0]=1/ ...
754 * 'call-graph=fp' is 'evt config', should be applied to each
756 * 'map:array.value[0]=1' is 'obj config', should be processed
757 * with parse_events_config_bpf.
759 * Move object config terms from the first list to obj_head_config.
762 split_bpf_config_terms(struct list_head *evt_head_config,
763 struct list_head *obj_head_config)
765 struct parse_events_term *term, *temp;
768 * Currectly, all possible user config term
769 * belong to bpf object. parse_events__is_hardcoded_term()
770 * happends to be a good flag.
772 * See parse_events_config_bpf() and
773 * config_term_tracepoint().
775 list_for_each_entry_safe(term, temp, evt_head_config, list)
776 if (!parse_events__is_hardcoded_term(term))
777 list_move_tail(&term->list, obj_head_config);
780 int parse_events_load_bpf(struct parse_events_state *parse_state,
781 struct list_head *list,
784 struct list_head *head_config)
787 struct bpf_object *obj;
788 LIST_HEAD(obj_head_config);
791 split_bpf_config_terms(head_config, &obj_head_config);
793 obj = bpf__prepare_load(bpf_file_name, source);
800 snprintf(errbuf, sizeof(errbuf),
801 "BPF support is not compiled");
803 bpf__strerror_prepare_load(bpf_file_name,
808 parse_state->error->help = strdup("(add -v to see detail)");
809 parse_state->error->str = strdup(errbuf);
813 err = parse_events_load_bpf_obj(parse_state, list, obj, head_config);
816 err = parse_events_config_bpf(parse_state, obj, &obj_head_config);
819 * Caller doesn't know anything about obj_head_config,
820 * so combine them together again before returnning.
823 list_splice_tail(&obj_head_config, head_config);
828 parse_breakpoint_type(const char *type, struct perf_event_attr *attr)
832 for (i = 0; i < 3; i++) {
833 if (!type || !type[i])
836 #define CHECK_SET_TYPE(bit) \
838 if (attr->bp_type & bit) \
841 attr->bp_type |= bit; \
846 CHECK_SET_TYPE(HW_BREAKPOINT_R);
849 CHECK_SET_TYPE(HW_BREAKPOINT_W);
852 CHECK_SET_TYPE(HW_BREAKPOINT_X);
859 #undef CHECK_SET_TYPE
861 if (!attr->bp_type) /* Default */
862 attr->bp_type = HW_BREAKPOINT_R | HW_BREAKPOINT_W;
867 int parse_events_add_breakpoint(struct list_head *list, int *idx,
868 void *ptr, char *type, u64 len)
870 struct perf_event_attr attr;
872 memset(&attr, 0, sizeof(attr));
873 attr.bp_addr = (unsigned long) ptr;
875 if (parse_breakpoint_type(type, &attr))
878 /* Provide some defaults if len is not specified */
880 if (attr.bp_type == HW_BREAKPOINT_X)
883 len = HW_BREAKPOINT_LEN_4;
888 attr.type = PERF_TYPE_BREAKPOINT;
889 attr.sample_period = 1;
891 return add_event(list, idx, &attr, NULL, NULL);
894 static int check_type_val(struct parse_events_term *term,
895 struct parse_events_error *err,
898 if (type == term->type_val)
902 err->idx = term->err_val;
903 if (type == PARSE_EVENTS__TERM_TYPE_NUM)
904 err->str = strdup("expected numeric value");
906 err->str = strdup("expected string value");
912 * Update according to parse-events.l
914 static const char *config_term_names[__PARSE_EVENTS__TERM_TYPE_NR] = {
915 [PARSE_EVENTS__TERM_TYPE_USER] = "<sysfs term>",
916 [PARSE_EVENTS__TERM_TYPE_CONFIG] = "config",
917 [PARSE_EVENTS__TERM_TYPE_CONFIG1] = "config1",
918 [PARSE_EVENTS__TERM_TYPE_CONFIG2] = "config2",
919 [PARSE_EVENTS__TERM_TYPE_NAME] = "name",
920 [PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD] = "period",
921 [PARSE_EVENTS__TERM_TYPE_SAMPLE_FREQ] = "freq",
922 [PARSE_EVENTS__TERM_TYPE_BRANCH_SAMPLE_TYPE] = "branch_type",
923 [PARSE_EVENTS__TERM_TYPE_TIME] = "time",
924 [PARSE_EVENTS__TERM_TYPE_CALLGRAPH] = "call-graph",
925 [PARSE_EVENTS__TERM_TYPE_STACKSIZE] = "stack-size",
926 [PARSE_EVENTS__TERM_TYPE_NOINHERIT] = "no-inherit",
927 [PARSE_EVENTS__TERM_TYPE_INHERIT] = "inherit",
928 [PARSE_EVENTS__TERM_TYPE_MAX_STACK] = "max-stack",
929 [PARSE_EVENTS__TERM_TYPE_MAX_EVENTS] = "nr",
930 [PARSE_EVENTS__TERM_TYPE_OVERWRITE] = "overwrite",
931 [PARSE_EVENTS__TERM_TYPE_NOOVERWRITE] = "no-overwrite",
932 [PARSE_EVENTS__TERM_TYPE_DRV_CFG] = "driver-config",
935 static bool config_term_shrinked;
938 config_term_avail(int term_type, struct parse_events_error *err)
940 if (term_type < 0 || term_type >= __PARSE_EVENTS__TERM_TYPE_NR) {
941 err->str = strdup("Invalid term_type");
944 if (!config_term_shrinked)
948 case PARSE_EVENTS__TERM_TYPE_CONFIG:
949 case PARSE_EVENTS__TERM_TYPE_CONFIG1:
950 case PARSE_EVENTS__TERM_TYPE_CONFIG2:
951 case PARSE_EVENTS__TERM_TYPE_NAME:
952 case PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD:
958 /* term_type is validated so indexing is safe */
959 if (asprintf(&err->str, "'%s' is not usable in 'perf stat'",
960 config_term_names[term_type]) < 0)
966 void parse_events__shrink_config_terms(void)
968 config_term_shrinked = true;
971 static int config_term_common(struct perf_event_attr *attr,
972 struct parse_events_term *term,
973 struct parse_events_error *err)
975 #define CHECK_TYPE_VAL(type) \
977 if (check_type_val(term, err, PARSE_EVENTS__TERM_TYPE_ ## type)) \
981 switch (term->type_term) {
982 case PARSE_EVENTS__TERM_TYPE_CONFIG:
984 attr->config = term->val.num;
986 case PARSE_EVENTS__TERM_TYPE_CONFIG1:
988 attr->config1 = term->val.num;
990 case PARSE_EVENTS__TERM_TYPE_CONFIG2:
992 attr->config2 = term->val.num;
994 case PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD:
997 case PARSE_EVENTS__TERM_TYPE_SAMPLE_FREQ:
1000 case PARSE_EVENTS__TERM_TYPE_BRANCH_SAMPLE_TYPE:
1001 CHECK_TYPE_VAL(STR);
1002 if (strcmp(term->val.str, "no") &&
1003 parse_branch_str(term->val.str, &attr->branch_sample_type)) {
1004 err->str = strdup("invalid branch sample type");
1005 err->idx = term->err_val;
1009 case PARSE_EVENTS__TERM_TYPE_TIME:
1010 CHECK_TYPE_VAL(NUM);
1011 if (term->val.num > 1) {
1012 err->str = strdup("expected 0 or 1");
1013 err->idx = term->err_val;
1017 case PARSE_EVENTS__TERM_TYPE_CALLGRAPH:
1018 CHECK_TYPE_VAL(STR);
1020 case PARSE_EVENTS__TERM_TYPE_STACKSIZE:
1021 CHECK_TYPE_VAL(NUM);
1023 case PARSE_EVENTS__TERM_TYPE_INHERIT:
1024 CHECK_TYPE_VAL(NUM);
1026 case PARSE_EVENTS__TERM_TYPE_NOINHERIT:
1027 CHECK_TYPE_VAL(NUM);
1029 case PARSE_EVENTS__TERM_TYPE_OVERWRITE:
1030 CHECK_TYPE_VAL(NUM);
1032 case PARSE_EVENTS__TERM_TYPE_NOOVERWRITE:
1033 CHECK_TYPE_VAL(NUM);
1035 case PARSE_EVENTS__TERM_TYPE_NAME:
1036 CHECK_TYPE_VAL(STR);
1038 case PARSE_EVENTS__TERM_TYPE_MAX_STACK:
1039 CHECK_TYPE_VAL(NUM);
1041 case PARSE_EVENTS__TERM_TYPE_MAX_EVENTS:
1042 CHECK_TYPE_VAL(NUM);
1045 err->str = strdup("unknown term");
1046 err->idx = term->err_term;
1047 err->help = parse_events_formats_error_string(NULL);
1052 * Check term availbility after basic checking so
1053 * PARSE_EVENTS__TERM_TYPE_USER can be found and filtered.
1055 * If check availbility at the entry of this function,
1056 * user will see "'<sysfs term>' is not usable in 'perf stat'"
1057 * if an invalid config term is provided for legacy events
1058 * (for example, instructions/badterm/...), which is confusing.
1060 if (!config_term_avail(term->type_term, err))
1063 #undef CHECK_TYPE_VAL
1066 static int config_term_pmu(struct perf_event_attr *attr,
1067 struct parse_events_term *term,
1068 struct parse_events_error *err)
1070 if (term->type_term == PARSE_EVENTS__TERM_TYPE_USER ||
1071 term->type_term == PARSE_EVENTS__TERM_TYPE_DRV_CFG)
1073 * Always succeed for sysfs terms, as we dont know
1074 * at this point what type they need to have.
1078 return config_term_common(attr, term, err);
1081 static int config_term_tracepoint(struct perf_event_attr *attr,
1082 struct parse_events_term *term,
1083 struct parse_events_error *err)
1085 switch (term->type_term) {
1086 case PARSE_EVENTS__TERM_TYPE_CALLGRAPH:
1087 case PARSE_EVENTS__TERM_TYPE_STACKSIZE:
1088 case PARSE_EVENTS__TERM_TYPE_INHERIT:
1089 case PARSE_EVENTS__TERM_TYPE_NOINHERIT:
1090 case PARSE_EVENTS__TERM_TYPE_MAX_STACK:
1091 case PARSE_EVENTS__TERM_TYPE_MAX_EVENTS:
1092 case PARSE_EVENTS__TERM_TYPE_OVERWRITE:
1093 case PARSE_EVENTS__TERM_TYPE_NOOVERWRITE:
1094 return config_term_common(attr, term, err);
1097 err->idx = term->err_term;
1098 err->str = strdup("unknown term");
1099 err->help = strdup("valid terms: call-graph,stack-size\n");
1107 static int config_attr(struct perf_event_attr *attr,
1108 struct list_head *head,
1109 struct parse_events_error *err,
1110 config_term_func_t config_term)
1112 struct parse_events_term *term;
1114 list_for_each_entry(term, head, list)
1115 if (config_term(attr, term, err))
1121 static int get_config_terms(struct list_head *head_config,
1122 struct list_head *head_terms __maybe_unused)
1124 #define ADD_CONFIG_TERM(__type, __name, __val) \
1126 struct perf_evsel_config_term *__t; \
1128 __t = zalloc(sizeof(*__t)); \
1132 INIT_LIST_HEAD(&__t->list); \
1133 __t->type = PERF_EVSEL__CONFIG_TERM_ ## __type; \
1134 __t->val.__name = __val; \
1135 __t->weak = term->weak; \
1136 list_add_tail(&__t->list, head_terms); \
1139 struct parse_events_term *term;
1141 list_for_each_entry(term, head_config, list) {
1142 switch (term->type_term) {
1143 case PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD:
1144 ADD_CONFIG_TERM(PERIOD, period, term->val.num);
1146 case PARSE_EVENTS__TERM_TYPE_SAMPLE_FREQ:
1147 ADD_CONFIG_TERM(FREQ, freq, term->val.num);
1149 case PARSE_EVENTS__TERM_TYPE_TIME:
1150 ADD_CONFIG_TERM(TIME, time, term->val.num);
1152 case PARSE_EVENTS__TERM_TYPE_CALLGRAPH:
1153 ADD_CONFIG_TERM(CALLGRAPH, callgraph, term->val.str);
1155 case PARSE_EVENTS__TERM_TYPE_BRANCH_SAMPLE_TYPE:
1156 ADD_CONFIG_TERM(BRANCH, branch, term->val.str);
1158 case PARSE_EVENTS__TERM_TYPE_STACKSIZE:
1159 ADD_CONFIG_TERM(STACK_USER, stack_user, term->val.num);
1161 case PARSE_EVENTS__TERM_TYPE_INHERIT:
1162 ADD_CONFIG_TERM(INHERIT, inherit, term->val.num ? 1 : 0);
1164 case PARSE_EVENTS__TERM_TYPE_NOINHERIT:
1165 ADD_CONFIG_TERM(INHERIT, inherit, term->val.num ? 0 : 1);
1167 case PARSE_EVENTS__TERM_TYPE_MAX_STACK:
1168 ADD_CONFIG_TERM(MAX_STACK, max_stack, term->val.num);
1170 case PARSE_EVENTS__TERM_TYPE_MAX_EVENTS:
1171 ADD_CONFIG_TERM(MAX_EVENTS, max_events, term->val.num);
1173 case PARSE_EVENTS__TERM_TYPE_OVERWRITE:
1174 ADD_CONFIG_TERM(OVERWRITE, overwrite, term->val.num ? 1 : 0);
1176 case PARSE_EVENTS__TERM_TYPE_NOOVERWRITE:
1177 ADD_CONFIG_TERM(OVERWRITE, overwrite, term->val.num ? 0 : 1);
1179 case PARSE_EVENTS__TERM_TYPE_DRV_CFG:
1180 ADD_CONFIG_TERM(DRV_CFG, drv_cfg, term->val.str);
1186 #undef ADD_EVSEL_CONFIG
1190 int parse_events_add_tracepoint(struct list_head *list, int *idx,
1191 const char *sys, const char *event,
1192 struct parse_events_error *err,
1193 struct list_head *head_config)
1196 struct perf_event_attr attr;
1198 if (config_attr(&attr, head_config, err,
1199 config_term_tracepoint))
1203 if (strpbrk(sys, "*?"))
1204 return add_tracepoint_multi_sys(list, idx, sys, event,
1207 return add_tracepoint_event(list, idx, sys, event,
1211 int parse_events_add_numeric(struct parse_events_state *parse_state,
1212 struct list_head *list,
1213 u32 type, u64 config,
1214 struct list_head *head_config)
1216 struct perf_event_attr attr;
1217 LIST_HEAD(config_terms);
1219 memset(&attr, 0, sizeof(attr));
1221 attr.config = config;
1224 if (config_attr(&attr, head_config, parse_state->error,
1225 config_term_common))
1228 if (get_config_terms(head_config, &config_terms))
1232 return add_event(list, &parse_state->idx, &attr,
1233 get_config_name(head_config), &config_terms);
1236 int parse_events_add_pmu(struct parse_events_state *parse_state,
1237 struct list_head *list, char *name,
1238 struct list_head *head_config,
1239 bool auto_merge_stats,
1242 struct perf_event_attr attr;
1243 struct perf_pmu_info info;
1244 struct perf_pmu *pmu;
1245 struct perf_evsel *evsel;
1246 struct parse_events_error *err = parse_state->error;
1247 bool use_uncore_alias;
1248 LIST_HEAD(config_terms);
1250 pmu = perf_pmu__find(name);
1252 if (asprintf(&err->str,
1253 "Cannot find PMU `%s'. Missing kernel support?",
1259 if (pmu->default_config) {
1260 memcpy(&attr, pmu->default_config,
1261 sizeof(struct perf_event_attr));
1263 memset(&attr, 0, sizeof(attr));
1266 use_uncore_alias = (pmu->is_uncore && use_alias);
1269 attr.type = pmu->type;
1270 evsel = __add_event(list, &parse_state->idx, &attr, NULL, pmu, NULL, auto_merge_stats);
1272 evsel->pmu_name = name;
1273 evsel->use_uncore_alias = use_uncore_alias;
1280 if (perf_pmu__check_alias(pmu, head_config, &info))
1284 * Configure hardcoded terms first, no need to check
1285 * return value when called with fail == 0 ;)
1287 if (config_attr(&attr, head_config, parse_state->error, config_term_pmu))
1290 if (get_config_terms(head_config, &config_terms))
1293 if (perf_pmu__config(pmu, &attr, head_config, parse_state->error))
1296 evsel = __add_event(list, &parse_state->idx, &attr,
1297 get_config_name(head_config), pmu,
1298 &config_terms, auto_merge_stats);
1300 evsel->unit = info.unit;
1301 evsel->scale = info.scale;
1302 evsel->per_pkg = info.per_pkg;
1303 evsel->snapshot = info.snapshot;
1304 evsel->metric_expr = info.metric_expr;
1305 evsel->metric_name = info.metric_name;
1306 evsel->pmu_name = name;
1307 evsel->use_uncore_alias = use_uncore_alias;
1310 return evsel ? 0 : -ENOMEM;
1313 int parse_events_multi_pmu_add(struct parse_events_state *parse_state,
1314 char *str, struct list_head **listp)
1316 struct list_head *head;
1317 struct parse_events_term *term;
1318 struct list_head *list;
1319 struct perf_pmu *pmu = NULL;
1323 /* Add it for all PMUs that support the alias */
1324 list = malloc(sizeof(struct list_head));
1327 INIT_LIST_HEAD(list);
1328 while ((pmu = perf_pmu__scan(pmu)) != NULL) {
1329 struct perf_pmu_alias *alias;
1331 list_for_each_entry(alias, &pmu->aliases, list) {
1332 if (!strcasecmp(alias->name, str)) {
1333 head = malloc(sizeof(struct list_head));
1336 INIT_LIST_HEAD(head);
1337 if (parse_events_term__num(&term, PARSE_EVENTS__TERM_TYPE_USER,
1338 str, 1, false, &str, NULL) < 0)
1340 list_add_tail(&term->list, head);
1342 if (!parse_events_add_pmu(parse_state, list,
1345 pr_debug("%s -> %s/%s/\n", str,
1346 pmu->name, alias->str);
1350 parse_events_terms__delete(head);
1360 int parse_events__modifier_group(struct list_head *list,
1363 return parse_events__modifier_event(list, event_mod, true);
1367 * Check if the two uncore PMUs are from the same uncore block
1368 * The format of the uncore PMU name is uncore_#blockname_#pmuidx
1370 static bool is_same_uncore_block(const char *pmu_name_a, const char *pmu_name_b)
1372 char *end_a, *end_b;
1374 end_a = strrchr(pmu_name_a, '_');
1375 end_b = strrchr(pmu_name_b, '_');
1377 if (!end_a || !end_b)
1380 if ((end_a - pmu_name_a) != (end_b - pmu_name_b))
1383 return (strncmp(pmu_name_a, pmu_name_b, end_a - pmu_name_a) == 0);
1387 parse_events__set_leader_for_uncore_aliase(char *name, struct list_head *list,
1388 struct parse_events_state *parse_state)
1390 struct perf_evsel *evsel, *leader;
1392 bool is_leader = true;
1393 int i, nr_pmu = 0, total_members, ret = 0;
1395 leader = list_first_entry(list, struct perf_evsel, node);
1396 evsel = list_last_entry(list, struct perf_evsel, node);
1397 total_members = evsel->idx - leader->idx + 1;
1399 leaders = calloc(total_members, sizeof(uintptr_t));
1400 if (WARN_ON(!leaders))
1404 * Going through the whole group and doing sanity check.
1405 * All members must use alias, and be from the same uncore block.
1406 * Also, storing the leader events in an array.
1408 __evlist__for_each_entry(list, evsel) {
1410 /* Only split the uncore group which members use alias */
1411 if (!evsel->use_uncore_alias)
1414 /* The events must be from the same uncore block */
1415 if (!is_same_uncore_block(leader->pmu_name, evsel->pmu_name))
1421 * If the event's PMU name starts to repeat, it must be a new
1422 * event. That can be used to distinguish the leader from
1423 * other members, even they have the same event name.
1425 if ((leader != evsel) && (leader->pmu_name == evsel->pmu_name)) {
1429 /* The name is always alias name */
1430 WARN_ON(strcmp(leader->name, evsel->name));
1432 /* Store the leader event for each PMU */
1433 leaders[nr_pmu++] = (uintptr_t) evsel;
1436 /* only one event alias */
1437 if (nr_pmu == total_members) {
1438 parse_state->nr_groups--;
1443 * An uncore event alias is a joint name which means the same event
1444 * runs on all PMUs of a block.
1445 * Perf doesn't support mixed events from different PMUs in the same
1446 * group. The big group has to be split into multiple small groups
1447 * which only include the events from the same PMU.
1449 * Here the uncore event aliases must be from the same uncore block.
1450 * The number of PMUs must be same for each alias. The number of new
1451 * small groups equals to the number of PMUs.
1452 * Setting the leader event for corresponding members in each group.
1455 __evlist__for_each_entry(list, evsel) {
1458 evsel->leader = (struct perf_evsel *) leaders[i++];
1461 /* The number of members and group name are same for each group */
1462 for (i = 0; i < nr_pmu; i++) {
1463 evsel = (struct perf_evsel *) leaders[i];
1464 evsel->nr_members = total_members / nr_pmu;
1465 evsel->group_name = name ? strdup(name) : NULL;
1468 /* Take the new small groups into account */
1469 parse_state->nr_groups += nr_pmu - 1;
1478 void parse_events__set_leader(char *name, struct list_head *list,
1479 struct parse_events_state *parse_state)
1481 struct perf_evsel *leader;
1483 if (list_empty(list)) {
1484 WARN_ONCE(true, "WARNING: failed to set leader: empty list");
1488 if (parse_events__set_leader_for_uncore_aliase(name, list, parse_state))
1491 __perf_evlist__set_leader(list);
1492 leader = list_entry(list->next, struct perf_evsel, node);
1493 leader->group_name = name ? strdup(name) : NULL;
1496 /* list_event is assumed to point to malloc'ed memory */
1497 void parse_events_update_lists(struct list_head *list_event,
1498 struct list_head *list_all)
1501 * Called for single event definition. Update the
1502 * 'all event' list, and reinit the 'single event'
1503 * list, for next event definition.
1505 list_splice_tail(list_event, list_all);
1509 struct event_modifier {
1524 static int get_event_modifier(struct event_modifier *mod, char *str,
1525 struct perf_evsel *evsel)
1527 int eu = evsel ? evsel->attr.exclude_user : 0;
1528 int ek = evsel ? evsel->attr.exclude_kernel : 0;
1529 int eh = evsel ? evsel->attr.exclude_hv : 0;
1530 int eH = evsel ? evsel->attr.exclude_host : 0;
1531 int eG = evsel ? evsel->attr.exclude_guest : 0;
1532 int eI = evsel ? evsel->attr.exclude_idle : 0;
1533 int precise = evsel ? evsel->attr.precise_ip : 0;
1534 int precise_max = 0;
1535 int sample_read = 0;
1536 int pinned = evsel ? evsel->attr.pinned : 0;
1538 int exclude = eu | ek | eh;
1539 int exclude_GH = evsel ? evsel->exclude_GH : 0;
1542 memset(mod, 0, sizeof(*mod));
1547 exclude = eu = ek = eh = 1;
1549 } else if (*str == 'k') {
1551 exclude = eu = ek = eh = 1;
1553 } else if (*str == 'h') {
1555 exclude = eu = ek = eh = 1;
1557 } else if (*str == 'G') {
1559 exclude_GH = eG = eH = 1;
1561 } else if (*str == 'H') {
1563 exclude_GH = eG = eH = 1;
1565 } else if (*str == 'I') {
1567 } else if (*str == 'p') {
1569 /* use of precise requires exclude_guest */
1572 } else if (*str == 'P') {
1574 } else if (*str == 'S') {
1576 } else if (*str == 'D') {
1578 } else if (*str == 'W') {
1589 * 0 - SAMPLE_IP can have arbitrary skid
1590 * 1 - SAMPLE_IP must have constant skid
1591 * 2 - SAMPLE_IP requested to have 0 skid
1592 * 3 - SAMPLE_IP must have 0 skid
1594 * See also PERF_RECORD_MISC_EXACT_IP
1605 mod->precise = precise;
1606 mod->precise_max = precise_max;
1607 mod->exclude_GH = exclude_GH;
1608 mod->sample_read = sample_read;
1609 mod->pinned = pinned;
1616 * Basic modifier sanity check to validate it contains only one
1617 * instance of any modifier (apart from 'p') present.
1619 static int check_modifier(char *str)
1623 /* The sizeof includes 0 byte as well. */
1624 if (strlen(str) > (sizeof("ukhGHpppPSDIW") - 1))
1628 if (*p != 'p' && strchr(p + 1, *p))
1636 int parse_events__modifier_event(struct list_head *list, char *str, bool add)
1638 struct perf_evsel *evsel;
1639 struct event_modifier mod;
1644 if (check_modifier(str))
1647 if (!add && get_event_modifier(&mod, str, NULL))
1650 __evlist__for_each_entry(list, evsel) {
1651 if (add && get_event_modifier(&mod, str, evsel))
1654 evsel->attr.exclude_user = mod.eu;
1655 evsel->attr.exclude_kernel = mod.ek;
1656 evsel->attr.exclude_hv = mod.eh;
1657 evsel->attr.precise_ip = mod.precise;
1658 evsel->attr.exclude_host = mod.eH;
1659 evsel->attr.exclude_guest = mod.eG;
1660 evsel->attr.exclude_idle = mod.eI;
1661 evsel->exclude_GH = mod.exclude_GH;
1662 evsel->sample_read = mod.sample_read;
1663 evsel->precise_max = mod.precise_max;
1664 evsel->weak_group = mod.weak;
1666 if (perf_evsel__is_group_leader(evsel))
1667 evsel->attr.pinned = mod.pinned;
1673 int parse_events_name(struct list_head *list, char *name)
1675 struct perf_evsel *evsel;
1677 __evlist__for_each_entry(list, evsel) {
1679 evsel->name = strdup(name);
1686 comp_pmu(const void *p1, const void *p2)
1688 struct perf_pmu_event_symbol *pmu1 = (struct perf_pmu_event_symbol *) p1;
1689 struct perf_pmu_event_symbol *pmu2 = (struct perf_pmu_event_symbol *) p2;
1691 return strcasecmp(pmu1->symbol, pmu2->symbol);
1694 static void perf_pmu__parse_cleanup(void)
1696 if (perf_pmu_events_list_num > 0) {
1697 struct perf_pmu_event_symbol *p;
1700 for (i = 0; i < perf_pmu_events_list_num; i++) {
1701 p = perf_pmu_events_list + i;
1704 zfree(&perf_pmu_events_list);
1705 perf_pmu_events_list_num = 0;
1709 #define SET_SYMBOL(str, stype) \
1718 * Read the pmu events list from sysfs
1719 * Save it into perf_pmu_events_list
1721 static void perf_pmu__parse_init(void)
1724 struct perf_pmu *pmu = NULL;
1725 struct perf_pmu_alias *alias;
1729 while ((pmu = perf_pmu__scan(pmu)) != NULL) {
1730 list_for_each_entry(alias, &pmu->aliases, list) {
1731 if (strchr(alias->name, '-'))
1738 perf_pmu_events_list_num = -1;
1741 perf_pmu_events_list = malloc(sizeof(struct perf_pmu_event_symbol) * len);
1742 if (!perf_pmu_events_list)
1744 perf_pmu_events_list_num = len;
1748 while ((pmu = perf_pmu__scan(pmu)) != NULL) {
1749 list_for_each_entry(alias, &pmu->aliases, list) {
1750 struct perf_pmu_event_symbol *p = perf_pmu_events_list + len;
1751 char *tmp = strchr(alias->name, '-');
1754 SET_SYMBOL(strndup(alias->name, tmp - alias->name),
1755 PMU_EVENT_SYMBOL_PREFIX);
1757 SET_SYMBOL(strdup(++tmp), PMU_EVENT_SYMBOL_SUFFIX);
1760 SET_SYMBOL(strdup(alias->name), PMU_EVENT_SYMBOL);
1765 qsort(perf_pmu_events_list, len,
1766 sizeof(struct perf_pmu_event_symbol), comp_pmu);
1770 perf_pmu__parse_cleanup();
1773 enum perf_pmu_event_symbol_type
1774 perf_pmu__parse_check(const char *name)
1776 struct perf_pmu_event_symbol p, *r;
1778 /* scan kernel pmu events from sysfs if needed */
1779 if (perf_pmu_events_list_num == 0)
1780 perf_pmu__parse_init();
1782 * name "cpu" could be prefix of cpu-cycles or cpu// events.
1783 * cpu-cycles has been handled by hardcode.
1784 * So it must be cpu// events, not kernel pmu event.
1786 if ((perf_pmu_events_list_num <= 0) || !strcmp(name, "cpu"))
1787 return PMU_EVENT_SYMBOL_ERR;
1789 p.symbol = strdup(name);
1790 r = bsearch(&p, perf_pmu_events_list,
1791 (size_t) perf_pmu_events_list_num,
1792 sizeof(struct perf_pmu_event_symbol), comp_pmu);
1794 return r ? r->type : PMU_EVENT_SYMBOL_ERR;
1797 static int parse_events__scanner(const char *str, void *parse_state, int start_token)
1799 YY_BUFFER_STATE buffer;
1803 ret = parse_events_lex_init_extra(start_token, &scanner);
1807 buffer = parse_events__scan_string(str, scanner);
1810 parse_events_debug = 1;
1812 ret = parse_events_parse(parse_state, scanner);
1814 parse_events__flush_buffer(buffer, scanner);
1815 parse_events__delete_buffer(buffer, scanner);
1816 parse_events_lex_destroy(scanner);
1821 * parse event config string, return a list of event terms.
1823 int parse_events_terms(struct list_head *terms, const char *str)
1825 struct parse_events_state parse_state = {
1830 ret = parse_events__scanner(str, &parse_state, PE_START_TERMS);
1832 list_splice(parse_state.terms, terms);
1833 zfree(&parse_state.terms);
1837 parse_events_terms__delete(parse_state.terms);
1841 int parse_events(struct perf_evlist *evlist, const char *str,
1842 struct parse_events_error *err)
1844 struct parse_events_state parse_state = {
1845 .list = LIST_HEAD_INIT(parse_state.list),
1846 .idx = evlist->nr_entries,
1852 ret = parse_events__scanner(str, &parse_state, PE_START_EVENTS);
1853 perf_pmu__parse_cleanup();
1855 struct perf_evsel *last;
1857 if (list_empty(&parse_state.list)) {
1858 WARN_ONCE(true, "WARNING: event parser found nothing\n");
1862 perf_evlist__splice_list_tail(evlist, &parse_state.list);
1863 evlist->nr_groups += parse_state.nr_groups;
1864 last = perf_evlist__last(evlist);
1865 last->cmdline_group_boundary = true;
1871 * There are 2 users - builtin-record and builtin-test objects.
1872 * Both call perf_evlist__delete in case of error, so we dont
1878 #define MAX_WIDTH 1000
1879 static int get_term_width(void)
1883 get_term_dimensions(&ws);
1884 return ws.ws_col > MAX_WIDTH ? MAX_WIDTH : ws.ws_col;
1887 void parse_events_print_error(struct parse_events_error *err,
1890 const char *str = "invalid or unsupported event: ";
1891 char _buf[MAX_WIDTH];
1892 char *buf = (char *) event;
1896 /* -2 for extra '' in the final fprintf */
1897 int width = get_term_width() - 2;
1898 int len_event = strlen(event);
1899 int len_str, max_len, cut = 0;
1902 * Maximum error index indent, we will cut
1903 * the event string if it's bigger.
1905 int max_err_idx = 13;
1908 * Let's be specific with the message when
1909 * we have the precise error.
1911 str = "event syntax error: ";
1912 len_str = strlen(str);
1913 max_len = width - len_str;
1917 /* We're cutting from the beginning. */
1918 if (err->idx > max_err_idx)
1919 cut = err->idx - max_err_idx;
1921 strncpy(buf, event + cut, max_len);
1923 /* Mark cut parts with '..' on both sides. */
1925 buf[0] = buf[1] = '.';
1927 if ((len_event - cut) > max_len) {
1928 buf[max_len - 1] = buf[max_len - 2] = '.';
1932 idx = len_str + err->idx - cut;
1935 fprintf(stderr, "%s'%s'\n", str, buf);
1937 fprintf(stderr, "%*s\\___ %s\n", idx + 1, "", err->str);
1939 fprintf(stderr, "\n%s\n", err->help);
1947 int parse_events_option(const struct option *opt, const char *str,
1948 int unset __maybe_unused)
1950 struct perf_evlist *evlist = *(struct perf_evlist **)opt->value;
1951 struct parse_events_error err = { .idx = 0, };
1952 int ret = parse_events(evlist, str, &err);
1955 parse_events_print_error(&err, str);
1956 fprintf(stderr, "Run 'perf list' for a list of valid events\n");
1963 foreach_evsel_in_last_glob(struct perf_evlist *evlist,
1964 int (*func)(struct perf_evsel *evsel,
1968 struct perf_evsel *last = NULL;
1972 * Don't return when list_empty, give func a chance to report
1973 * error when it found last == NULL.
1975 * So no need to WARN here, let *func do this.
1977 if (evlist->nr_entries > 0)
1978 last = perf_evlist__last(evlist);
1981 err = (*func)(last, arg);
1987 if (last->node.prev == &evlist->entries)
1989 last = list_entry(last->node.prev, struct perf_evsel, node);
1990 } while (!last->cmdline_group_boundary);
1995 static int set_filter(struct perf_evsel *evsel, const void *arg)
1997 const char *str = arg;
1999 int nr_addr_filters = 0;
2000 struct perf_pmu *pmu = NULL;
2002 if (evsel == NULL) {
2004 "--filter option should follow a -e tracepoint or HW tracer option\n");
2008 if (evsel->attr.type == PERF_TYPE_TRACEPOINT) {
2009 if (perf_evsel__append_tp_filter(evsel, str) < 0) {
2011 "not enough memory to hold filter string\n");
2018 while ((pmu = perf_pmu__scan(pmu)) != NULL)
2019 if (pmu->type == evsel->attr.type) {
2025 perf_pmu__scan_file(pmu, "nr_addr_filters",
2026 "%d", &nr_addr_filters);
2028 if (!nr_addr_filters) {
2030 "This CPU does not support address filtering\n");
2034 if (perf_evsel__append_addr_filter(evsel, str) < 0) {
2036 "not enough memory to hold filter string\n");
2043 int parse_filter(const struct option *opt, const char *str,
2044 int unset __maybe_unused)
2046 struct perf_evlist *evlist = *(struct perf_evlist **)opt->value;
2048 return foreach_evsel_in_last_glob(evlist, set_filter,
2052 static int add_exclude_perf_filter(struct perf_evsel *evsel,
2053 const void *arg __maybe_unused)
2055 char new_filter[64];
2057 if (evsel == NULL || evsel->attr.type != PERF_TYPE_TRACEPOINT) {
2059 "--exclude-perf option should follow a -e tracepoint option\n");
2063 snprintf(new_filter, sizeof(new_filter), "common_pid != %d", getpid());
2065 if (perf_evsel__append_tp_filter(evsel, new_filter) < 0) {
2067 "not enough memory to hold filter string\n");
2074 int exclude_perf(const struct option *opt,
2075 const char *arg __maybe_unused,
2076 int unset __maybe_unused)
2078 struct perf_evlist *evlist = *(struct perf_evlist **)opt->value;
2080 return foreach_evsel_in_last_glob(evlist, add_exclude_perf_filter,
2084 static const char * const event_type_descriptors[] = {
2088 "Hardware cache event",
2089 "Raw hardware event descriptor",
2090 "Hardware breakpoint",
2093 static int cmp_string(const void *a, const void *b)
2095 const char * const *as = a;
2096 const char * const *bs = b;
2098 return strcmp(*as, *bs);
2102 * Print the events from <debugfs_mount_point>/tracing/events
2105 void print_tracepoint_events(const char *subsys_glob, const char *event_glob,
2108 DIR *sys_dir, *evt_dir;
2109 struct dirent *sys_dirent, *evt_dirent;
2110 char evt_path[MAXPATHLEN];
2112 char **evt_list = NULL;
2113 unsigned int evt_i = 0, evt_num = 0;
2114 bool evt_num_known = false;
2117 sys_dir = tracing_events__opendir();
2121 if (evt_num_known) {
2122 evt_list = zalloc(sizeof(char *) * evt_num);
2124 goto out_close_sys_dir;
2127 for_each_subsystem(sys_dir, sys_dirent) {
2128 if (subsys_glob != NULL &&
2129 !strglobmatch(sys_dirent->d_name, subsys_glob))
2132 dir_path = get_events_file(sys_dirent->d_name);
2135 evt_dir = opendir(dir_path);
2139 for_each_event(dir_path, evt_dir, evt_dirent) {
2140 if (event_glob != NULL &&
2141 !strglobmatch(evt_dirent->d_name, event_glob))
2144 if (!evt_num_known) {
2149 snprintf(evt_path, MAXPATHLEN, "%s:%s",
2150 sys_dirent->d_name, evt_dirent->d_name);
2152 evt_list[evt_i] = strdup(evt_path);
2153 if (evt_list[evt_i] == NULL) {
2154 put_events_file(dir_path);
2155 goto out_close_evt_dir;
2161 put_events_file(dir_path);
2165 if (!evt_num_known) {
2166 evt_num_known = true;
2169 qsort(evt_list, evt_num, sizeof(char *), cmp_string);
2171 while (evt_i < evt_num) {
2173 printf("%s ", evt_list[evt_i++]);
2176 printf(" %-50s [%s]\n", evt_list[evt_i++],
2177 event_type_descriptors[PERF_TYPE_TRACEPOINT]);
2179 if (evt_num && pager_in_use())
2184 for (evt_i = 0; evt_i < evt_num; evt_i++)
2185 zfree(&evt_list[evt_i]);
2194 printf("FATAL: not enough memory to print %s\n",
2195 event_type_descriptors[PERF_TYPE_TRACEPOINT]);
2201 * Check whether event is in <debugfs_mount_point>/tracing/events
2204 int is_valid_tracepoint(const char *event_string)
2206 DIR *sys_dir, *evt_dir;
2207 struct dirent *sys_dirent, *evt_dirent;
2208 char evt_path[MAXPATHLEN];
2211 sys_dir = tracing_events__opendir();
2215 for_each_subsystem(sys_dir, sys_dirent) {
2216 dir_path = get_events_file(sys_dirent->d_name);
2219 evt_dir = opendir(dir_path);
2223 for_each_event(dir_path, evt_dir, evt_dirent) {
2224 snprintf(evt_path, MAXPATHLEN, "%s:%s",
2225 sys_dirent->d_name, evt_dirent->d_name);
2226 if (!strcmp(evt_path, event_string)) {
2234 put_events_file(dir_path);
2240 static bool is_event_supported(u8 type, unsigned config)
2244 struct perf_evsel *evsel;
2245 struct perf_event_attr attr = {
2250 struct thread_map *tmap = thread_map__new_by_tid(0);
2255 evsel = perf_evsel__new(&attr);
2257 open_return = perf_evsel__open(evsel, NULL, tmap);
2258 ret = open_return >= 0;
2260 if (open_return == -EACCES) {
2262 * This happens if the paranoid value
2263 * /proc/sys/kernel/perf_event_paranoid is set to 2
2264 * Re-run with exclude_kernel set; we don't do that
2265 * by default as some ARM machines do not support it.
2268 evsel->attr.exclude_kernel = 1;
2269 ret = perf_evsel__open(evsel, NULL, tmap) >= 0;
2271 perf_evsel__delete(evsel);
2274 thread_map__put(tmap);
2278 void print_sdt_events(const char *subsys_glob, const char *event_glob,
2281 struct probe_cache *pcache;
2282 struct probe_cache_entry *ent;
2283 struct strlist *bidlist, *sdtlist;
2284 struct strlist_config cfg = {.dont_dupstr = true};
2285 struct str_node *nd, *nd2;
2286 char *buf, *path, *ptr = NULL;
2287 bool show_detail = false;
2290 sdtlist = strlist__new(NULL, &cfg);
2292 pr_debug("Failed to allocate new strlist for SDT\n");
2295 bidlist = build_id_cache__list_all(true);
2297 pr_debug("Failed to get buildids: %d\n", errno);
2300 strlist__for_each_entry(nd, bidlist) {
2301 pcache = probe_cache__new(nd->s, NULL);
2304 list_for_each_entry(ent, &pcache->entries, node) {
2308 !strglobmatch(ent->pev.group, subsys_glob))
2311 !strglobmatch(ent->pev.event, event_glob))
2313 ret = asprintf(&buf, "%s:%s@%s", ent->pev.group,
2314 ent->pev.event, nd->s);
2316 strlist__add(sdtlist, buf);
2318 probe_cache__delete(pcache);
2320 strlist__delete(bidlist);
2322 strlist__for_each_entry(nd, sdtlist) {
2323 buf = strchr(nd->s, '@');
2327 printf("%s ", nd->s);
2330 nd2 = strlist__next(nd);
2332 ptr = strchr(nd2->s, '@');
2335 if (strcmp(nd->s, nd2->s) == 0)
2339 path = build_id_cache__origname(buf);
2340 ret = asprintf(&buf, "%s@%s(%.12s)", nd->s, path, buf);
2342 printf(" %-50s [%s]\n", buf, "SDT event");
2346 printf(" %-50s [%s]\n", nd->s, "SDT event");
2348 if (strcmp(nd->s, nd2->s) != 0)
2349 show_detail = false;
2354 strlist__delete(sdtlist);
2357 int print_hwcache_events(const char *event_glob, bool name_only)
2359 unsigned int type, op, i, evt_i = 0, evt_num = 0;
2361 char **evt_list = NULL;
2362 bool evt_num_known = false;
2365 if (evt_num_known) {
2366 evt_list = zalloc(sizeof(char *) * evt_num);
2371 for (type = 0; type < PERF_COUNT_HW_CACHE_MAX; type++) {
2372 for (op = 0; op < PERF_COUNT_HW_CACHE_OP_MAX; op++) {
2373 /* skip invalid cache type */
2374 if (!perf_evsel__is_cache_op_valid(type, op))
2377 for (i = 0; i < PERF_COUNT_HW_CACHE_RESULT_MAX; i++) {
2378 __perf_evsel__hw_cache_type_op_res_name(type, op, i,
2379 name, sizeof(name));
2380 if (event_glob != NULL && !strglobmatch(name, event_glob))
2383 if (!is_event_supported(PERF_TYPE_HW_CACHE,
2384 type | (op << 8) | (i << 16)))
2387 if (!evt_num_known) {
2392 evt_list[evt_i] = strdup(name);
2393 if (evt_list[evt_i] == NULL)
2400 if (!evt_num_known) {
2401 evt_num_known = true;
2404 qsort(evt_list, evt_num, sizeof(char *), cmp_string);
2406 while (evt_i < evt_num) {
2408 printf("%s ", evt_list[evt_i++]);
2411 printf(" %-50s [%s]\n", evt_list[evt_i++],
2412 event_type_descriptors[PERF_TYPE_HW_CACHE]);
2414 if (evt_num && pager_in_use())
2419 for (evt_i = 0; evt_i < evt_num; evt_i++)
2420 zfree(&evt_list[evt_i]);
2425 printf("FATAL: not enough memory to print %s\n", event_type_descriptors[PERF_TYPE_HW_CACHE]);
2431 void print_symbol_events(const char *event_glob, unsigned type,
2432 struct event_symbol *syms, unsigned max,
2435 unsigned int i, evt_i = 0, evt_num = 0;
2436 char name[MAX_NAME_LEN];
2437 char **evt_list = NULL;
2438 bool evt_num_known = false;
2441 if (evt_num_known) {
2442 evt_list = zalloc(sizeof(char *) * evt_num);
2448 for (i = 0; i < max; i++, syms++) {
2450 if (event_glob != NULL && syms->symbol != NULL &&
2451 !(strglobmatch(syms->symbol, event_glob) ||
2452 (syms->alias && strglobmatch(syms->alias, event_glob))))
2455 if (!is_event_supported(type, i))
2458 if (!evt_num_known) {
2463 if (!name_only && strlen(syms->alias))
2464 snprintf(name, MAX_NAME_LEN, "%s OR %s", syms->symbol, syms->alias);
2466 strlcpy(name, syms->symbol, MAX_NAME_LEN);
2468 evt_list[evt_i] = strdup(name);
2469 if (evt_list[evt_i] == NULL)
2474 if (!evt_num_known) {
2475 evt_num_known = true;
2478 qsort(evt_list, evt_num, sizeof(char *), cmp_string);
2480 while (evt_i < evt_num) {
2482 printf("%s ", evt_list[evt_i++]);
2485 printf(" %-50s [%s]\n", evt_list[evt_i++], event_type_descriptors[type]);
2487 if (evt_num && pager_in_use())
2492 for (evt_i = 0; evt_i < evt_num; evt_i++)
2493 zfree(&evt_list[evt_i]);
2498 printf("FATAL: not enough memory to print %s\n", event_type_descriptors[type]);
2504 * Print the help text for the event symbols:
2506 void print_events(const char *event_glob, bool name_only, bool quiet_flag,
2507 bool long_desc, bool details_flag)
2509 print_symbol_events(event_glob, PERF_TYPE_HARDWARE,
2510 event_symbols_hw, PERF_COUNT_HW_MAX, name_only);
2512 print_symbol_events(event_glob, PERF_TYPE_SOFTWARE,
2513 event_symbols_sw, PERF_COUNT_SW_MAX, name_only);
2515 print_hwcache_events(event_glob, name_only);
2517 print_pmu_events(event_glob, name_only, quiet_flag, long_desc,
2520 if (event_glob != NULL)
2524 printf(" %-50s [%s]\n",
2526 event_type_descriptors[PERF_TYPE_RAW]);
2527 printf(" %-50s [%s]\n",
2528 "cpu/t1=v1[,t2=v2,t3 ...]/modifier",
2529 event_type_descriptors[PERF_TYPE_RAW]);
2531 printf(" (see 'man perf-list' on how to encode it)\n\n");
2533 printf(" %-50s [%s]\n",
2534 "mem:<addr>[/len][:access]",
2535 event_type_descriptors[PERF_TYPE_BREAKPOINT]);
2540 print_tracepoint_events(NULL, NULL, name_only);
2542 print_sdt_events(NULL, NULL, name_only);
2544 metricgroup__print(true, true, NULL, name_only, details_flag);
2547 int parse_events__is_hardcoded_term(struct parse_events_term *term)
2549 return term->type_term != PARSE_EVENTS__TERM_TYPE_USER;
2552 static int new_term(struct parse_events_term **_term,
2553 struct parse_events_term *temp,
2556 struct parse_events_term *term;
2558 term = malloc(sizeof(*term));
2563 INIT_LIST_HEAD(&term->list);
2566 switch (term->type_val) {
2567 case PARSE_EVENTS__TERM_TYPE_NUM:
2568 term->val.num = num;
2570 case PARSE_EVENTS__TERM_TYPE_STR:
2571 term->val.str = str;
2582 int parse_events_term__num(struct parse_events_term **term,
2583 int type_term, char *config, u64 num,
2585 void *loc_term_, void *loc_val_)
2587 YYLTYPE *loc_term = loc_term_;
2588 YYLTYPE *loc_val = loc_val_;
2590 struct parse_events_term temp = {
2591 .type_val = PARSE_EVENTS__TERM_TYPE_NUM,
2592 .type_term = type_term,
2594 .no_value = no_value,
2595 .err_term = loc_term ? loc_term->first_column : 0,
2596 .err_val = loc_val ? loc_val->first_column : 0,
2599 return new_term(term, &temp, NULL, num);
2602 int parse_events_term__str(struct parse_events_term **term,
2603 int type_term, char *config, char *str,
2604 void *loc_term_, void *loc_val_)
2606 YYLTYPE *loc_term = loc_term_;
2607 YYLTYPE *loc_val = loc_val_;
2609 struct parse_events_term temp = {
2610 .type_val = PARSE_EVENTS__TERM_TYPE_STR,
2611 .type_term = type_term,
2613 .err_term = loc_term ? loc_term->first_column : 0,
2614 .err_val = loc_val ? loc_val->first_column : 0,
2617 return new_term(term, &temp, str, 0);
2620 int parse_events_term__sym_hw(struct parse_events_term **term,
2621 char *config, unsigned idx)
2623 struct event_symbol *sym;
2624 struct parse_events_term temp = {
2625 .type_val = PARSE_EVENTS__TERM_TYPE_STR,
2626 .type_term = PARSE_EVENTS__TERM_TYPE_USER,
2627 .config = config ?: (char *) "event",
2630 BUG_ON(idx >= PERF_COUNT_HW_MAX);
2631 sym = &event_symbols_hw[idx];
2633 return new_term(term, &temp, (char *) sym->symbol, 0);
2636 int parse_events_term__clone(struct parse_events_term **new,
2637 struct parse_events_term *term)
2639 struct parse_events_term temp = {
2640 .type_val = term->type_val,
2641 .type_term = term->type_term,
2642 .config = term->config,
2643 .err_term = term->err_term,
2644 .err_val = term->err_val,
2647 return new_term(new, &temp, term->val.str, term->val.num);
2650 int parse_events_copy_term_list(struct list_head *old,
2651 struct list_head **new)
2653 struct parse_events_term *term, *n;
2661 *new = malloc(sizeof(struct list_head));
2664 INIT_LIST_HEAD(*new);
2666 list_for_each_entry (term, old, list) {
2667 ret = parse_events_term__clone(&n, term);
2670 list_add_tail(&n->list, *new);
2675 void parse_events_terms__purge(struct list_head *terms)
2677 struct parse_events_term *term, *h;
2679 list_for_each_entry_safe(term, h, terms, list) {
2680 if (term->array.nr_ranges)
2681 zfree(&term->array.ranges);
2682 list_del_init(&term->list);
2687 void parse_events_terms__delete(struct list_head *terms)
2691 parse_events_terms__purge(terms);
2695 void parse_events__clear_array(struct parse_events_array *a)
2700 void parse_events_evlist_error(struct parse_events_state *parse_state,
2701 int idx, const char *str)
2703 struct parse_events_error *err = parse_state->error;
2708 err->str = strdup(str);
2709 WARN_ONCE(!err->str, "WARNING: failed to allocate error string");
2712 static void config_terms_list(char *buf, size_t buf_sz)
2718 for (i = 0; i < __PARSE_EVENTS__TERM_TYPE_NR; i++) {
2719 const char *name = config_term_names[i];
2721 if (!config_term_avail(i, NULL))
2728 if (strlen(buf) + strlen(name) + 2 >= buf_sz)
2740 * Return string contains valid config terms of an event.
2741 * @additional_terms: For terms such as PMU sysfs terms.
2743 char *parse_events_formats_error_string(char *additional_terms)
2746 /* "no-overwrite" is the longest name */
2747 char static_terms[__PARSE_EVENTS__TERM_TYPE_NR *
2748 (sizeof("no-overwrite") - 1)];
2750 config_terms_list(static_terms, sizeof(static_terms));
2752 if (additional_terms) {
2753 if (asprintf(&str, "valid terms: %s,%s",
2754 additional_terms, static_terms) < 0)
2757 if (asprintf(&str, "valid terms: %s", static_terms) < 0)