1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/hw_breakpoint.h>
12 #include <subcmd/parse-options.h>
13 #include "parse-events.h"
14 #include <subcmd/exec-cmd.h>
20 #include "bpf-loader.h"
22 #include <api/fs/tracing_path.h>
23 #include "parse-events-bison.h"
24 #define YY_EXTRA_TYPE int
25 #include "parse-events-flex.h"
27 #include "thread_map.h"
29 #include "probe-file.h"
31 #include "util/parse-branch-options.h"
33 #define MAX_NAME_LEN 100
36 extern int parse_events_debug;
38 int parse_events_parse(void *parse_state, void *scanner);
39 static int get_config_terms(struct list_head *head_config,
40 struct list_head *head_terms __maybe_unused);
42 static struct perf_pmu_event_symbol *perf_pmu_events_list;
44 * The variable indicates the number of supported pmu event symbols.
45 * 0 means not initialized and ready to init
46 * -1 means failed to init, don't try anymore
47 * >0 is the number of supported pmu event symbols
49 static int perf_pmu_events_list_num;
51 struct event_symbol event_symbols_hw[PERF_COUNT_HW_MAX] = {
52 [PERF_COUNT_HW_CPU_CYCLES] = {
53 .symbol = "cpu-cycles",
56 [PERF_COUNT_HW_INSTRUCTIONS] = {
57 .symbol = "instructions",
60 [PERF_COUNT_HW_CACHE_REFERENCES] = {
61 .symbol = "cache-references",
64 [PERF_COUNT_HW_CACHE_MISSES] = {
65 .symbol = "cache-misses",
68 [PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = {
69 .symbol = "branch-instructions",
72 [PERF_COUNT_HW_BRANCH_MISSES] = {
73 .symbol = "branch-misses",
76 [PERF_COUNT_HW_BUS_CYCLES] = {
77 .symbol = "bus-cycles",
80 [PERF_COUNT_HW_STALLED_CYCLES_FRONTEND] = {
81 .symbol = "stalled-cycles-frontend",
82 .alias = "idle-cycles-frontend",
84 [PERF_COUNT_HW_STALLED_CYCLES_BACKEND] = {
85 .symbol = "stalled-cycles-backend",
86 .alias = "idle-cycles-backend",
88 [PERF_COUNT_HW_REF_CPU_CYCLES] = {
89 .symbol = "ref-cycles",
94 struct event_symbol event_symbols_sw[PERF_COUNT_SW_MAX] = {
95 [PERF_COUNT_SW_CPU_CLOCK] = {
96 .symbol = "cpu-clock",
99 [PERF_COUNT_SW_TASK_CLOCK] = {
100 .symbol = "task-clock",
103 [PERF_COUNT_SW_PAGE_FAULTS] = {
104 .symbol = "page-faults",
107 [PERF_COUNT_SW_CONTEXT_SWITCHES] = {
108 .symbol = "context-switches",
111 [PERF_COUNT_SW_CPU_MIGRATIONS] = {
112 .symbol = "cpu-migrations",
113 .alias = "migrations",
115 [PERF_COUNT_SW_PAGE_FAULTS_MIN] = {
116 .symbol = "minor-faults",
119 [PERF_COUNT_SW_PAGE_FAULTS_MAJ] = {
120 .symbol = "major-faults",
123 [PERF_COUNT_SW_ALIGNMENT_FAULTS] = {
124 .symbol = "alignment-faults",
127 [PERF_COUNT_SW_EMULATION_FAULTS] = {
128 .symbol = "emulation-faults",
131 [PERF_COUNT_SW_DUMMY] = {
135 [PERF_COUNT_SW_BPF_OUTPUT] = {
136 .symbol = "bpf-output",
141 #define __PERF_EVENT_FIELD(config, name) \
142 ((config & PERF_EVENT_##name##_MASK) >> PERF_EVENT_##name##_SHIFT)
144 #define PERF_EVENT_RAW(config) __PERF_EVENT_FIELD(config, RAW)
145 #define PERF_EVENT_CONFIG(config) __PERF_EVENT_FIELD(config, CONFIG)
146 #define PERF_EVENT_TYPE(config) __PERF_EVENT_FIELD(config, TYPE)
147 #define PERF_EVENT_ID(config) __PERF_EVENT_FIELD(config, EVENT)
149 #define for_each_subsystem(sys_dir, sys_dirent) \
150 while ((sys_dirent = readdir(sys_dir)) != NULL) \
151 if (sys_dirent->d_type == DT_DIR && \
152 (strcmp(sys_dirent->d_name, ".")) && \
153 (strcmp(sys_dirent->d_name, "..")))
155 static int tp_event_has_id(struct dirent *sys_dir, struct dirent *evt_dir)
157 char evt_path[MAXPATHLEN];
160 snprintf(evt_path, MAXPATHLEN, "%s/%s/%s/id", tracing_events_path,
161 sys_dir->d_name, evt_dir->d_name);
162 fd = open(evt_path, O_RDONLY);
170 #define for_each_event(sys_dirent, evt_dir, evt_dirent) \
171 while ((evt_dirent = readdir(evt_dir)) != NULL) \
172 if (evt_dirent->d_type == DT_DIR && \
173 (strcmp(evt_dirent->d_name, ".")) && \
174 (strcmp(evt_dirent->d_name, "..")) && \
175 (!tp_event_has_id(sys_dirent, evt_dirent)))
177 #define MAX_EVENT_LENGTH 512
180 struct tracepoint_path *tracepoint_id_to_path(u64 config)
182 struct tracepoint_path *path = NULL;
183 DIR *sys_dir, *evt_dir;
184 struct dirent *sys_dirent, *evt_dirent;
188 char evt_path[MAXPATHLEN];
189 char dir_path[MAXPATHLEN];
191 sys_dir = opendir(tracing_events_path);
195 for_each_subsystem(sys_dir, sys_dirent) {
197 snprintf(dir_path, MAXPATHLEN, "%s/%s", tracing_events_path,
199 evt_dir = opendir(dir_path);
203 for_each_event(sys_dirent, evt_dir, evt_dirent) {
205 scnprintf(evt_path, MAXPATHLEN, "%s/%s/id", dir_path,
207 fd = open(evt_path, O_RDONLY);
210 if (read(fd, id_buf, sizeof(id_buf)) < 0) {
219 path = zalloc(sizeof(*path));
222 path->system = malloc(MAX_EVENT_LENGTH);
227 path->name = malloc(MAX_EVENT_LENGTH);
229 zfree(&path->system);
233 strncpy(path->system, sys_dirent->d_name,
235 strncpy(path->name, evt_dirent->d_name,
247 struct tracepoint_path *tracepoint_name_to_path(const char *name)
249 struct tracepoint_path *path = zalloc(sizeof(*path));
250 char *str = strchr(name, ':');
252 if (path == NULL || str == NULL) {
257 path->system = strndup(name, str - name);
258 path->name = strdup(str+1);
260 if (path->system == NULL || path->name == NULL) {
261 zfree(&path->system);
269 const char *event_type(int type)
272 case PERF_TYPE_HARDWARE:
275 case PERF_TYPE_SOFTWARE:
278 case PERF_TYPE_TRACEPOINT:
281 case PERF_TYPE_HW_CACHE:
282 return "hardware-cache";
291 static int parse_events__is_name_term(struct parse_events_term *term)
293 return term->type_term == PARSE_EVENTS__TERM_TYPE_NAME;
296 static char *get_config_name(struct list_head *head_terms)
298 struct parse_events_term *term;
303 list_for_each_entry(term, head_terms, list)
304 if (parse_events__is_name_term(term))
305 return term->val.str;
310 static struct perf_evsel *
311 __add_event(struct list_head *list, int *idx,
312 struct perf_event_attr *attr,
313 char *name, struct perf_pmu *pmu,
314 struct list_head *config_terms, bool auto_merge_stats)
316 struct perf_evsel *evsel;
317 struct cpu_map *cpus = pmu ? pmu->cpus : NULL;
319 event_attr_init(attr);
321 evsel = perf_evsel__new_idx(attr, *idx);
326 evsel->cpus = cpu_map__get(cpus);
327 evsel->own_cpus = cpu_map__get(cpus);
328 evsel->system_wide = pmu ? pmu->is_uncore : false;
329 evsel->auto_merge_stats = auto_merge_stats;
332 evsel->name = strdup(name);
335 list_splice(config_terms, &evsel->config_terms);
337 list_add_tail(&evsel->node, list);
341 static int add_event(struct list_head *list, int *idx,
342 struct perf_event_attr *attr, char *name,
343 struct list_head *config_terms)
345 return __add_event(list, idx, attr, name, NULL, config_terms, false) ? 0 : -ENOMEM;
348 static int parse_aliases(char *str, const char *names[][PERF_EVSEL__MAX_ALIASES], int size)
353 for (i = 0; i < size; i++) {
354 for (j = 0; j < PERF_EVSEL__MAX_ALIASES && names[i][j]; j++) {
355 n = strlen(names[i][j]);
356 if (n > longest && !strncasecmp(str, names[i][j], n))
366 typedef int config_term_func_t(struct perf_event_attr *attr,
367 struct parse_events_term *term,
368 struct parse_events_error *err);
369 static int config_term_common(struct perf_event_attr *attr,
370 struct parse_events_term *term,
371 struct parse_events_error *err);
372 static int config_attr(struct perf_event_attr *attr,
373 struct list_head *head,
374 struct parse_events_error *err,
375 config_term_func_t config_term);
377 int parse_events_add_cache(struct list_head *list, int *idx,
378 char *type, char *op_result1, char *op_result2,
379 struct parse_events_error *err,
380 struct list_head *head_config)
382 struct perf_event_attr attr;
383 LIST_HEAD(config_terms);
384 char name[MAX_NAME_LEN], *config_name;
385 int cache_type = -1, cache_op = -1, cache_result = -1;
386 char *op_result[2] = { op_result1, op_result2 };
390 * No fallback - if we cannot get a clear cache type
393 cache_type = parse_aliases(type, perf_evsel__hw_cache,
394 PERF_COUNT_HW_CACHE_MAX);
395 if (cache_type == -1)
398 config_name = get_config_name(head_config);
399 n = snprintf(name, MAX_NAME_LEN, "%s", type);
401 for (i = 0; (i < 2) && (op_result[i]); i++) {
402 char *str = op_result[i];
404 n += snprintf(name + n, MAX_NAME_LEN - n, "-%s", str);
406 if (cache_op == -1) {
407 cache_op = parse_aliases(str, perf_evsel__hw_cache_op,
408 PERF_COUNT_HW_CACHE_OP_MAX);
410 if (!perf_evsel__is_cache_op_valid(cache_type, cache_op))
416 if (cache_result == -1) {
417 cache_result = parse_aliases(str, perf_evsel__hw_cache_result,
418 PERF_COUNT_HW_CACHE_RESULT_MAX);
419 if (cache_result >= 0)
425 * Fall back to reads:
428 cache_op = PERF_COUNT_HW_CACHE_OP_READ;
431 * Fall back to accesses:
433 if (cache_result == -1)
434 cache_result = PERF_COUNT_HW_CACHE_RESULT_ACCESS;
436 memset(&attr, 0, sizeof(attr));
437 attr.config = cache_type | (cache_op << 8) | (cache_result << 16);
438 attr.type = PERF_TYPE_HW_CACHE;
441 if (config_attr(&attr, head_config, err,
445 if (get_config_terms(head_config, &config_terms))
448 return add_event(list, idx, &attr, config_name ? : name, &config_terms);
451 static void tracepoint_error(struct parse_events_error *e, int err,
452 const char *sys, const char *name)
460 * We get error directly from syscall errno ( > 0),
461 * or from encoded pointer's error ( < 0).
467 e->str = strdup("can't access trace events");
470 e->str = strdup("unknown tracepoint");
473 e->str = strdup("failed to add tracepoint");
477 tracing_path__strerror_open_tp(err, help, sizeof(help), sys, name);
478 e->help = strdup(help);
481 static int add_tracepoint(struct list_head *list, int *idx,
482 const char *sys_name, const char *evt_name,
483 struct parse_events_error *err,
484 struct list_head *head_config)
486 struct perf_evsel *evsel;
488 evsel = perf_evsel__newtp_idx(sys_name, evt_name, (*idx)++);
490 tracepoint_error(err, PTR_ERR(evsel), sys_name, evt_name);
491 return PTR_ERR(evsel);
495 LIST_HEAD(config_terms);
497 if (get_config_terms(head_config, &config_terms))
499 list_splice(&config_terms, &evsel->config_terms);
502 list_add_tail(&evsel->node, list);
506 static int add_tracepoint_multi_event(struct list_head *list, int *idx,
507 const char *sys_name, const char *evt_name,
508 struct parse_events_error *err,
509 struct list_head *head_config)
511 char evt_path[MAXPATHLEN];
512 struct dirent *evt_ent;
514 int ret = 0, found = 0;
516 snprintf(evt_path, MAXPATHLEN, "%s/%s", tracing_events_path, sys_name);
517 evt_dir = opendir(evt_path);
519 tracepoint_error(err, errno, sys_name, evt_name);
523 while (!ret && (evt_ent = readdir(evt_dir))) {
524 if (!strcmp(evt_ent->d_name, ".")
525 || !strcmp(evt_ent->d_name, "..")
526 || !strcmp(evt_ent->d_name, "enable")
527 || !strcmp(evt_ent->d_name, "filter"))
530 if (!strglobmatch(evt_ent->d_name, evt_name))
535 ret = add_tracepoint(list, idx, sys_name, evt_ent->d_name,
540 tracepoint_error(err, ENOENT, sys_name, evt_name);
548 static int add_tracepoint_event(struct list_head *list, int *idx,
549 const char *sys_name, const char *evt_name,
550 struct parse_events_error *err,
551 struct list_head *head_config)
553 return strpbrk(evt_name, "*?") ?
554 add_tracepoint_multi_event(list, idx, sys_name, evt_name,
556 add_tracepoint(list, idx, sys_name, evt_name,
560 static int add_tracepoint_multi_sys(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 struct dirent *events_ent;
569 events_dir = opendir(tracing_events_path);
571 tracepoint_error(err, errno, sys_name, evt_name);
575 while (!ret && (events_ent = readdir(events_dir))) {
576 if (!strcmp(events_ent->d_name, ".")
577 || !strcmp(events_ent->d_name, "..")
578 || !strcmp(events_ent->d_name, "enable")
579 || !strcmp(events_ent->d_name, "header_event")
580 || !strcmp(events_ent->d_name, "header_page"))
583 if (!strglobmatch(events_ent->d_name, sys_name))
586 ret = add_tracepoint_event(list, idx, events_ent->d_name,
587 evt_name, err, head_config);
590 closedir(events_dir);
594 struct __add_bpf_event_param {
595 struct parse_events_state *parse_state;
596 struct list_head *list;
597 struct list_head *head_config;
600 static int add_bpf_event(const char *group, const char *event, int fd,
603 LIST_HEAD(new_evsels);
604 struct __add_bpf_event_param *param = _param;
605 struct parse_events_state *parse_state = param->parse_state;
606 struct list_head *list = param->list;
607 struct perf_evsel *pos;
610 pr_debug("add bpf event %s:%s and attach bpf program %d\n",
613 err = parse_events_add_tracepoint(&new_evsels, &parse_state->idx, group,
614 event, parse_state->error,
617 struct perf_evsel *evsel, *tmp;
619 pr_debug("Failed to add BPF event %s:%s\n",
621 list_for_each_entry_safe(evsel, tmp, &new_evsels, node) {
622 list_del(&evsel->node);
623 perf_evsel__delete(evsel);
627 pr_debug("adding %s:%s\n", group, event);
629 list_for_each_entry(pos, &new_evsels, node) {
630 pr_debug("adding %s:%s to %p\n",
634 list_splice(&new_evsels, list);
638 int parse_events_load_bpf_obj(struct parse_events_state *parse_state,
639 struct list_head *list,
640 struct bpf_object *obj,
641 struct list_head *head_config)
645 struct __add_bpf_event_param param = {parse_state, list, head_config};
646 static bool registered_unprobe_atexit = false;
648 if (IS_ERR(obj) || !obj) {
649 snprintf(errbuf, sizeof(errbuf),
650 "Internal error: load bpf obj with NULL");
656 * Register atexit handler before calling bpf__probe() so
657 * bpf__probe() don't need to unprobe probe points its already
658 * created when failure.
660 if (!registered_unprobe_atexit) {
662 registered_unprobe_atexit = true;
665 err = bpf__probe(obj);
667 bpf__strerror_probe(obj, err, errbuf, sizeof(errbuf));
671 err = bpf__load(obj);
673 bpf__strerror_load(obj, err, errbuf, sizeof(errbuf));
677 err = bpf__foreach_event(obj, add_bpf_event, ¶m);
679 snprintf(errbuf, sizeof(errbuf),
680 "Attach events in BPF object failed");
686 parse_state->error->help = strdup("(add -v to see detail)");
687 parse_state->error->str = strdup(errbuf);
692 parse_events_config_bpf(struct parse_events_state *parse_state,
693 struct bpf_object *obj,
694 struct list_head *head_config)
696 struct parse_events_term *term;
699 if (!head_config || list_empty(head_config))
702 list_for_each_entry(term, head_config, list) {
706 if (term->type_term != PARSE_EVENTS__TERM_TYPE_USER) {
707 snprintf(errbuf, sizeof(errbuf),
708 "Invalid config term for BPF object");
709 errbuf[BUFSIZ - 1] = '\0';
711 parse_state->error->idx = term->err_term;
712 parse_state->error->str = strdup(errbuf);
716 err = bpf__config_obj(obj, term, parse_state->evlist, &error_pos);
718 bpf__strerror_config_obj(obj, term, parse_state->evlist,
719 &error_pos, err, errbuf,
721 parse_state->error->help = strdup(
722 "Hint:\tValid config terms:\n"
723 " \tmap:[<arraymap>].value<indices>=[value]\n"
724 " \tmap:[<eventmap>].event<indices>=[event]\n"
726 " \twhere <indices> is something like [0,3...5] or [all]\n"
727 " \t(add -v to see detail)");
728 parse_state->error->str = strdup(errbuf);
729 if (err == -BPF_LOADER_ERRNO__OBJCONF_MAP_VALUE)
730 parse_state->error->idx = term->err_val;
732 parse_state->error->idx = term->err_term + error_pos;
740 * Split config terms:
741 * perf record -e bpf.c/call-graph=fp,map:array.value[0]=1/ ...
742 * 'call-graph=fp' is 'evt config', should be applied to each
744 * 'map:array.value[0]=1' is 'obj config', should be processed
745 * with parse_events_config_bpf.
747 * Move object config terms from the first list to obj_head_config.
750 split_bpf_config_terms(struct list_head *evt_head_config,
751 struct list_head *obj_head_config)
753 struct parse_events_term *term, *temp;
756 * Currectly, all possible user config term
757 * belong to bpf object. parse_events__is_hardcoded_term()
758 * happends to be a good flag.
760 * See parse_events_config_bpf() and
761 * config_term_tracepoint().
763 list_for_each_entry_safe(term, temp, evt_head_config, list)
764 if (!parse_events__is_hardcoded_term(term))
765 list_move_tail(&term->list, obj_head_config);
768 int parse_events_load_bpf(struct parse_events_state *parse_state,
769 struct list_head *list,
772 struct list_head *head_config)
775 struct bpf_object *obj;
776 LIST_HEAD(obj_head_config);
779 split_bpf_config_terms(head_config, &obj_head_config);
781 obj = bpf__prepare_load(bpf_file_name, source);
788 snprintf(errbuf, sizeof(errbuf),
789 "BPF support is not compiled");
791 bpf__strerror_prepare_load(bpf_file_name,
796 parse_state->error->help = strdup("(add -v to see detail)");
797 parse_state->error->str = strdup(errbuf);
801 err = parse_events_load_bpf_obj(parse_state, list, obj, head_config);
804 err = parse_events_config_bpf(parse_state, obj, &obj_head_config);
807 * Caller doesn't know anything about obj_head_config,
808 * so combine them together again before returnning.
811 list_splice_tail(&obj_head_config, head_config);
816 parse_breakpoint_type(const char *type, struct perf_event_attr *attr)
820 for (i = 0; i < 3; i++) {
821 if (!type || !type[i])
824 #define CHECK_SET_TYPE(bit) \
826 if (attr->bp_type & bit) \
829 attr->bp_type |= bit; \
834 CHECK_SET_TYPE(HW_BREAKPOINT_R);
837 CHECK_SET_TYPE(HW_BREAKPOINT_W);
840 CHECK_SET_TYPE(HW_BREAKPOINT_X);
847 #undef CHECK_SET_TYPE
849 if (!attr->bp_type) /* Default */
850 attr->bp_type = HW_BREAKPOINT_R | HW_BREAKPOINT_W;
855 int parse_events_add_breakpoint(struct list_head *list, int *idx,
856 void *ptr, char *type, u64 len)
858 struct perf_event_attr attr;
860 memset(&attr, 0, sizeof(attr));
861 attr.bp_addr = (unsigned long) ptr;
863 if (parse_breakpoint_type(type, &attr))
866 /* Provide some defaults if len is not specified */
868 if (attr.bp_type == HW_BREAKPOINT_X)
871 len = HW_BREAKPOINT_LEN_4;
876 attr.type = PERF_TYPE_BREAKPOINT;
877 attr.sample_period = 1;
879 return add_event(list, idx, &attr, NULL, NULL);
882 static int check_type_val(struct parse_events_term *term,
883 struct parse_events_error *err,
886 if (type == term->type_val)
890 err->idx = term->err_val;
891 if (type == PARSE_EVENTS__TERM_TYPE_NUM)
892 err->str = strdup("expected numeric value");
894 err->str = strdup("expected string value");
900 * Update according to parse-events.l
902 static const char *config_term_names[__PARSE_EVENTS__TERM_TYPE_NR] = {
903 [PARSE_EVENTS__TERM_TYPE_USER] = "<sysfs term>",
904 [PARSE_EVENTS__TERM_TYPE_CONFIG] = "config",
905 [PARSE_EVENTS__TERM_TYPE_CONFIG1] = "config1",
906 [PARSE_EVENTS__TERM_TYPE_CONFIG2] = "config2",
907 [PARSE_EVENTS__TERM_TYPE_NAME] = "name",
908 [PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD] = "period",
909 [PARSE_EVENTS__TERM_TYPE_SAMPLE_FREQ] = "freq",
910 [PARSE_EVENTS__TERM_TYPE_BRANCH_SAMPLE_TYPE] = "branch_type",
911 [PARSE_EVENTS__TERM_TYPE_TIME] = "time",
912 [PARSE_EVENTS__TERM_TYPE_CALLGRAPH] = "call-graph",
913 [PARSE_EVENTS__TERM_TYPE_STACKSIZE] = "stack-size",
914 [PARSE_EVENTS__TERM_TYPE_NOINHERIT] = "no-inherit",
915 [PARSE_EVENTS__TERM_TYPE_INHERIT] = "inherit",
916 [PARSE_EVENTS__TERM_TYPE_MAX_STACK] = "max-stack",
917 [PARSE_EVENTS__TERM_TYPE_OVERWRITE] = "overwrite",
918 [PARSE_EVENTS__TERM_TYPE_NOOVERWRITE] = "no-overwrite",
919 [PARSE_EVENTS__TERM_TYPE_DRV_CFG] = "driver-config",
922 static bool config_term_shrinked;
925 config_term_avail(int term_type, struct parse_events_error *err)
927 if (term_type < 0 || term_type >= __PARSE_EVENTS__TERM_TYPE_NR) {
928 err->str = strdup("Invalid term_type");
931 if (!config_term_shrinked)
935 case PARSE_EVENTS__TERM_TYPE_CONFIG:
936 case PARSE_EVENTS__TERM_TYPE_CONFIG1:
937 case PARSE_EVENTS__TERM_TYPE_CONFIG2:
938 case PARSE_EVENTS__TERM_TYPE_NAME:
939 case PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD:
945 /* term_type is validated so indexing is safe */
946 if (asprintf(&err->str, "'%s' is not usable in 'perf stat'",
947 config_term_names[term_type]) < 0)
953 void parse_events__shrink_config_terms(void)
955 config_term_shrinked = true;
958 static int config_term_common(struct perf_event_attr *attr,
959 struct parse_events_term *term,
960 struct parse_events_error *err)
962 #define CHECK_TYPE_VAL(type) \
964 if (check_type_val(term, err, PARSE_EVENTS__TERM_TYPE_ ## type)) \
968 switch (term->type_term) {
969 case PARSE_EVENTS__TERM_TYPE_CONFIG:
971 attr->config = term->val.num;
973 case PARSE_EVENTS__TERM_TYPE_CONFIG1:
975 attr->config1 = term->val.num;
977 case PARSE_EVENTS__TERM_TYPE_CONFIG2:
979 attr->config2 = term->val.num;
981 case PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD:
984 case PARSE_EVENTS__TERM_TYPE_SAMPLE_FREQ:
987 case PARSE_EVENTS__TERM_TYPE_BRANCH_SAMPLE_TYPE:
989 if (strcmp(term->val.str, "no") &&
990 parse_branch_str(term->val.str, &attr->branch_sample_type)) {
991 err->str = strdup("invalid branch sample type");
992 err->idx = term->err_val;
996 case PARSE_EVENTS__TERM_TYPE_TIME:
998 if (term->val.num > 1) {
999 err->str = strdup("expected 0 or 1");
1000 err->idx = term->err_val;
1004 case PARSE_EVENTS__TERM_TYPE_CALLGRAPH:
1005 CHECK_TYPE_VAL(STR);
1007 case PARSE_EVENTS__TERM_TYPE_STACKSIZE:
1008 CHECK_TYPE_VAL(NUM);
1010 case PARSE_EVENTS__TERM_TYPE_INHERIT:
1011 CHECK_TYPE_VAL(NUM);
1013 case PARSE_EVENTS__TERM_TYPE_NOINHERIT:
1014 CHECK_TYPE_VAL(NUM);
1016 case PARSE_EVENTS__TERM_TYPE_OVERWRITE:
1017 CHECK_TYPE_VAL(NUM);
1019 case PARSE_EVENTS__TERM_TYPE_NOOVERWRITE:
1020 CHECK_TYPE_VAL(NUM);
1022 case PARSE_EVENTS__TERM_TYPE_NAME:
1023 CHECK_TYPE_VAL(STR);
1025 case PARSE_EVENTS__TERM_TYPE_MAX_STACK:
1026 CHECK_TYPE_VAL(NUM);
1029 err->str = strdup("unknown term");
1030 err->idx = term->err_term;
1031 err->help = parse_events_formats_error_string(NULL);
1036 * Check term availbility after basic checking so
1037 * PARSE_EVENTS__TERM_TYPE_USER can be found and filtered.
1039 * If check availbility at the entry of this function,
1040 * user will see "'<sysfs term>' is not usable in 'perf stat'"
1041 * if an invalid config term is provided for legacy events
1042 * (for example, instructions/badterm/...), which is confusing.
1044 if (!config_term_avail(term->type_term, err))
1047 #undef CHECK_TYPE_VAL
1050 static int config_term_pmu(struct perf_event_attr *attr,
1051 struct parse_events_term *term,
1052 struct parse_events_error *err)
1054 if (term->type_term == PARSE_EVENTS__TERM_TYPE_USER ||
1055 term->type_term == PARSE_EVENTS__TERM_TYPE_DRV_CFG)
1057 * Always succeed for sysfs terms, as we dont know
1058 * at this point what type they need to have.
1062 return config_term_common(attr, term, err);
1065 static int config_term_tracepoint(struct perf_event_attr *attr,
1066 struct parse_events_term *term,
1067 struct parse_events_error *err)
1069 switch (term->type_term) {
1070 case PARSE_EVENTS__TERM_TYPE_CALLGRAPH:
1071 case PARSE_EVENTS__TERM_TYPE_STACKSIZE:
1072 case PARSE_EVENTS__TERM_TYPE_INHERIT:
1073 case PARSE_EVENTS__TERM_TYPE_NOINHERIT:
1074 case PARSE_EVENTS__TERM_TYPE_MAX_STACK:
1075 case PARSE_EVENTS__TERM_TYPE_OVERWRITE:
1076 case PARSE_EVENTS__TERM_TYPE_NOOVERWRITE:
1077 return config_term_common(attr, term, err);
1080 err->idx = term->err_term;
1081 err->str = strdup("unknown term");
1082 err->help = strdup("valid terms: call-graph,stack-size\n");
1090 static int config_attr(struct perf_event_attr *attr,
1091 struct list_head *head,
1092 struct parse_events_error *err,
1093 config_term_func_t config_term)
1095 struct parse_events_term *term;
1097 list_for_each_entry(term, head, list)
1098 if (config_term(attr, term, err))
1104 static int get_config_terms(struct list_head *head_config,
1105 struct list_head *head_terms __maybe_unused)
1107 #define ADD_CONFIG_TERM(__type, __name, __val) \
1109 struct perf_evsel_config_term *__t; \
1111 __t = zalloc(sizeof(*__t)); \
1115 INIT_LIST_HEAD(&__t->list); \
1116 __t->type = PERF_EVSEL__CONFIG_TERM_ ## __type; \
1117 __t->val.__name = __val; \
1118 __t->weak = term->weak; \
1119 list_add_tail(&__t->list, head_terms); \
1122 struct parse_events_term *term;
1124 list_for_each_entry(term, head_config, list) {
1125 switch (term->type_term) {
1126 case PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD:
1127 ADD_CONFIG_TERM(PERIOD, period, term->val.num);
1129 case PARSE_EVENTS__TERM_TYPE_SAMPLE_FREQ:
1130 ADD_CONFIG_TERM(FREQ, freq, term->val.num);
1132 case PARSE_EVENTS__TERM_TYPE_TIME:
1133 ADD_CONFIG_TERM(TIME, time, term->val.num);
1135 case PARSE_EVENTS__TERM_TYPE_CALLGRAPH:
1136 ADD_CONFIG_TERM(CALLGRAPH, callgraph, term->val.str);
1138 case PARSE_EVENTS__TERM_TYPE_BRANCH_SAMPLE_TYPE:
1139 ADD_CONFIG_TERM(BRANCH, branch, term->val.str);
1141 case PARSE_EVENTS__TERM_TYPE_STACKSIZE:
1142 ADD_CONFIG_TERM(STACK_USER, stack_user, term->val.num);
1144 case PARSE_EVENTS__TERM_TYPE_INHERIT:
1145 ADD_CONFIG_TERM(INHERIT, inherit, term->val.num ? 1 : 0);
1147 case PARSE_EVENTS__TERM_TYPE_NOINHERIT:
1148 ADD_CONFIG_TERM(INHERIT, inherit, term->val.num ? 0 : 1);
1150 case PARSE_EVENTS__TERM_TYPE_MAX_STACK:
1151 ADD_CONFIG_TERM(MAX_STACK, max_stack, term->val.num);
1153 case PARSE_EVENTS__TERM_TYPE_OVERWRITE:
1154 ADD_CONFIG_TERM(OVERWRITE, overwrite, term->val.num ? 1 : 0);
1156 case PARSE_EVENTS__TERM_TYPE_NOOVERWRITE:
1157 ADD_CONFIG_TERM(OVERWRITE, overwrite, term->val.num ? 0 : 1);
1159 case PARSE_EVENTS__TERM_TYPE_DRV_CFG:
1160 ADD_CONFIG_TERM(DRV_CFG, drv_cfg, term->val.str);
1166 #undef ADD_EVSEL_CONFIG
1170 int parse_events_add_tracepoint(struct list_head *list, int *idx,
1171 const char *sys, const char *event,
1172 struct parse_events_error *err,
1173 struct list_head *head_config)
1176 struct perf_event_attr attr;
1178 if (config_attr(&attr, head_config, err,
1179 config_term_tracepoint))
1183 if (strpbrk(sys, "*?"))
1184 return add_tracepoint_multi_sys(list, idx, sys, event,
1187 return add_tracepoint_event(list, idx, sys, event,
1191 int parse_events_add_numeric(struct parse_events_state *parse_state,
1192 struct list_head *list,
1193 u32 type, u64 config,
1194 struct list_head *head_config)
1196 struct perf_event_attr attr;
1197 LIST_HEAD(config_terms);
1199 memset(&attr, 0, sizeof(attr));
1201 attr.config = config;
1204 if (config_attr(&attr, head_config, parse_state->error,
1205 config_term_common))
1208 if (get_config_terms(head_config, &config_terms))
1212 return add_event(list, &parse_state->idx, &attr,
1213 get_config_name(head_config), &config_terms);
1216 static int __parse_events_add_pmu(struct parse_events_state *parse_state,
1217 struct list_head *list, char *name,
1218 struct list_head *head_config, bool auto_merge_stats)
1220 struct perf_event_attr attr;
1221 struct perf_pmu_info info;
1222 struct perf_pmu *pmu;
1223 struct perf_evsel *evsel;
1224 LIST_HEAD(config_terms);
1226 pmu = perf_pmu__find(name);
1230 if (pmu->default_config) {
1231 memcpy(&attr, pmu->default_config,
1232 sizeof(struct perf_event_attr));
1234 memset(&attr, 0, sizeof(attr));
1238 attr.type = pmu->type;
1239 evsel = __add_event(list, &parse_state->idx, &attr, NULL, pmu, NULL, auto_merge_stats);
1240 return evsel ? 0 : -ENOMEM;
1243 if (perf_pmu__check_alias(pmu, head_config, &info))
1247 * Configure hardcoded terms first, no need to check
1248 * return value when called with fail == 0 ;)
1250 if (config_attr(&attr, head_config, parse_state->error, config_term_pmu))
1253 if (get_config_terms(head_config, &config_terms))
1256 if (perf_pmu__config(pmu, &attr, head_config, parse_state->error))
1259 evsel = __add_event(list, &parse_state->idx, &attr,
1260 get_config_name(head_config), pmu,
1261 &config_terms, auto_merge_stats);
1263 evsel->unit = info.unit;
1264 evsel->scale = info.scale;
1265 evsel->per_pkg = info.per_pkg;
1266 evsel->snapshot = info.snapshot;
1267 evsel->metric_expr = info.metric_expr;
1268 evsel->metric_name = info.metric_name;
1271 return evsel ? 0 : -ENOMEM;
1274 int parse_events_add_pmu(struct parse_events_state *parse_state,
1275 struct list_head *list, char *name,
1276 struct list_head *head_config)
1278 return __parse_events_add_pmu(parse_state, list, name, head_config, false);
1281 int parse_events_multi_pmu_add(struct parse_events_state *parse_state,
1282 char *str, struct list_head **listp)
1284 struct list_head *head;
1285 struct parse_events_term *term;
1286 struct list_head *list;
1287 struct perf_pmu *pmu = NULL;
1291 /* Add it for all PMUs that support the alias */
1292 list = malloc(sizeof(struct list_head));
1295 INIT_LIST_HEAD(list);
1296 while ((pmu = perf_pmu__scan(pmu)) != NULL) {
1297 struct perf_pmu_alias *alias;
1299 list_for_each_entry(alias, &pmu->aliases, list) {
1300 if (!strcasecmp(alias->name, str)) {
1301 head = malloc(sizeof(struct list_head));
1304 INIT_LIST_HEAD(head);
1305 if (parse_events_term__num(&term, PARSE_EVENTS__TERM_TYPE_USER,
1306 str, 1, false, &str, NULL) < 0)
1308 list_add_tail(&term->list, head);
1310 if (!__parse_events_add_pmu(parse_state, list,
1311 pmu->name, head, true)) {
1312 pr_debug("%s -> %s/%s/\n", str,
1313 pmu->name, alias->str);
1317 parse_events_terms__delete(head);
1327 int parse_events__modifier_group(struct list_head *list,
1330 return parse_events__modifier_event(list, event_mod, true);
1333 void parse_events__set_leader(char *name, struct list_head *list)
1335 struct perf_evsel *leader;
1337 if (list_empty(list)) {
1338 WARN_ONCE(true, "WARNING: failed to set leader: empty list");
1342 __perf_evlist__set_leader(list);
1343 leader = list_entry(list->next, struct perf_evsel, node);
1344 leader->group_name = name ? strdup(name) : NULL;
1347 /* list_event is assumed to point to malloc'ed memory */
1348 void parse_events_update_lists(struct list_head *list_event,
1349 struct list_head *list_all)
1352 * Called for single event definition. Update the
1353 * 'all event' list, and reinit the 'single event'
1354 * list, for next event definition.
1356 list_splice_tail(list_event, list_all);
1360 struct event_modifier {
1374 static int get_event_modifier(struct event_modifier *mod, char *str,
1375 struct perf_evsel *evsel)
1377 int eu = evsel ? evsel->attr.exclude_user : 0;
1378 int ek = evsel ? evsel->attr.exclude_kernel : 0;
1379 int eh = evsel ? evsel->attr.exclude_hv : 0;
1380 int eH = evsel ? evsel->attr.exclude_host : 0;
1381 int eG = evsel ? evsel->attr.exclude_guest : 0;
1382 int eI = evsel ? evsel->attr.exclude_idle : 0;
1383 int precise = evsel ? evsel->attr.precise_ip : 0;
1384 int precise_max = 0;
1385 int sample_read = 0;
1386 int pinned = evsel ? evsel->attr.pinned : 0;
1388 int exclude = eu | ek | eh;
1389 int exclude_GH = evsel ? evsel->exclude_GH : 0;
1391 memset(mod, 0, sizeof(*mod));
1396 exclude = eu = ek = eh = 1;
1398 } else if (*str == 'k') {
1400 exclude = eu = ek = eh = 1;
1402 } else if (*str == 'h') {
1404 exclude = eu = ek = eh = 1;
1406 } else if (*str == 'G') {
1408 exclude_GH = eG = eH = 1;
1410 } else if (*str == 'H') {
1412 exclude_GH = eG = eH = 1;
1414 } else if (*str == 'I') {
1416 } else if (*str == 'p') {
1418 /* use of precise requires exclude_guest */
1421 } else if (*str == 'P') {
1423 } else if (*str == 'S') {
1425 } else if (*str == 'D') {
1436 * 0 - SAMPLE_IP can have arbitrary skid
1437 * 1 - SAMPLE_IP must have constant skid
1438 * 2 - SAMPLE_IP requested to have 0 skid
1439 * 3 - SAMPLE_IP must have 0 skid
1441 * See also PERF_RECORD_MISC_EXACT_IP
1452 mod->precise = precise;
1453 mod->precise_max = precise_max;
1454 mod->exclude_GH = exclude_GH;
1455 mod->sample_read = sample_read;
1456 mod->pinned = pinned;
1462 * Basic modifier sanity check to validate it contains only one
1463 * instance of any modifier (apart from 'p') present.
1465 static int check_modifier(char *str)
1469 /* The sizeof includes 0 byte as well. */
1470 if (strlen(str) > (sizeof("ukhGHpppPSDI") - 1))
1474 if (*p != 'p' && strchr(p + 1, *p))
1482 int parse_events__modifier_event(struct list_head *list, char *str, bool add)
1484 struct perf_evsel *evsel;
1485 struct event_modifier mod;
1490 if (check_modifier(str))
1493 if (!add && get_event_modifier(&mod, str, NULL))
1496 __evlist__for_each_entry(list, evsel) {
1497 if (add && get_event_modifier(&mod, str, evsel))
1500 evsel->attr.exclude_user = mod.eu;
1501 evsel->attr.exclude_kernel = mod.ek;
1502 evsel->attr.exclude_hv = mod.eh;
1503 evsel->attr.precise_ip = mod.precise;
1504 evsel->attr.exclude_host = mod.eH;
1505 evsel->attr.exclude_guest = mod.eG;
1506 evsel->attr.exclude_idle = mod.eI;
1507 evsel->exclude_GH = mod.exclude_GH;
1508 evsel->sample_read = mod.sample_read;
1509 evsel->precise_max = mod.precise_max;
1511 if (perf_evsel__is_group_leader(evsel))
1512 evsel->attr.pinned = mod.pinned;
1518 int parse_events_name(struct list_head *list, char *name)
1520 struct perf_evsel *evsel;
1522 __evlist__for_each_entry(list, evsel) {
1524 evsel->name = strdup(name);
1531 comp_pmu(const void *p1, const void *p2)
1533 struct perf_pmu_event_symbol *pmu1 = (struct perf_pmu_event_symbol *) p1;
1534 struct perf_pmu_event_symbol *pmu2 = (struct perf_pmu_event_symbol *) p2;
1536 return strcasecmp(pmu1->symbol, pmu2->symbol);
1539 static void perf_pmu__parse_cleanup(void)
1541 if (perf_pmu_events_list_num > 0) {
1542 struct perf_pmu_event_symbol *p;
1545 for (i = 0; i < perf_pmu_events_list_num; i++) {
1546 p = perf_pmu_events_list + i;
1549 zfree(&perf_pmu_events_list);
1550 perf_pmu_events_list_num = 0;
1554 #define SET_SYMBOL(str, stype) \
1563 * Read the pmu events list from sysfs
1564 * Save it into perf_pmu_events_list
1566 static void perf_pmu__parse_init(void)
1569 struct perf_pmu *pmu = NULL;
1570 struct perf_pmu_alias *alias;
1574 while ((pmu = perf_pmu__scan(pmu)) != NULL) {
1575 list_for_each_entry(alias, &pmu->aliases, list) {
1576 if (strchr(alias->name, '-'))
1583 perf_pmu_events_list_num = -1;
1586 perf_pmu_events_list = malloc(sizeof(struct perf_pmu_event_symbol) * len);
1587 if (!perf_pmu_events_list)
1589 perf_pmu_events_list_num = len;
1593 while ((pmu = perf_pmu__scan(pmu)) != NULL) {
1594 list_for_each_entry(alias, &pmu->aliases, list) {
1595 struct perf_pmu_event_symbol *p = perf_pmu_events_list + len;
1596 char *tmp = strchr(alias->name, '-');
1599 SET_SYMBOL(strndup(alias->name, tmp - alias->name),
1600 PMU_EVENT_SYMBOL_PREFIX);
1602 SET_SYMBOL(strdup(++tmp), PMU_EVENT_SYMBOL_SUFFIX);
1605 SET_SYMBOL(strdup(alias->name), PMU_EVENT_SYMBOL);
1610 qsort(perf_pmu_events_list, len,
1611 sizeof(struct perf_pmu_event_symbol), comp_pmu);
1615 perf_pmu__parse_cleanup();
1618 enum perf_pmu_event_symbol_type
1619 perf_pmu__parse_check(const char *name)
1621 struct perf_pmu_event_symbol p, *r;
1623 /* scan kernel pmu events from sysfs if needed */
1624 if (perf_pmu_events_list_num == 0)
1625 perf_pmu__parse_init();
1627 * name "cpu" could be prefix of cpu-cycles or cpu// events.
1628 * cpu-cycles has been handled by hardcode.
1629 * So it must be cpu// events, not kernel pmu event.
1631 if ((perf_pmu_events_list_num <= 0) || !strcmp(name, "cpu"))
1632 return PMU_EVENT_SYMBOL_ERR;
1634 p.symbol = strdup(name);
1635 r = bsearch(&p, perf_pmu_events_list,
1636 (size_t) perf_pmu_events_list_num,
1637 sizeof(struct perf_pmu_event_symbol), comp_pmu);
1639 return r ? r->type : PMU_EVENT_SYMBOL_ERR;
1642 static int parse_events__scanner(const char *str, void *parse_state, int start_token)
1644 YY_BUFFER_STATE buffer;
1648 ret = parse_events_lex_init_extra(start_token, &scanner);
1652 buffer = parse_events__scan_string(str, scanner);
1655 parse_events_debug = 1;
1657 ret = parse_events_parse(parse_state, scanner);
1659 parse_events__flush_buffer(buffer, scanner);
1660 parse_events__delete_buffer(buffer, scanner);
1661 parse_events_lex_destroy(scanner);
1666 * parse event config string, return a list of event terms.
1668 int parse_events_terms(struct list_head *terms, const char *str)
1670 struct parse_events_state parse_state = {
1675 ret = parse_events__scanner(str, &parse_state, PE_START_TERMS);
1677 list_splice(parse_state.terms, terms);
1678 zfree(&parse_state.terms);
1682 parse_events_terms__delete(parse_state.terms);
1686 int parse_events(struct perf_evlist *evlist, const char *str,
1687 struct parse_events_error *err)
1689 struct parse_events_state parse_state = {
1690 .list = LIST_HEAD_INIT(parse_state.list),
1691 .idx = evlist->nr_entries,
1697 ret = parse_events__scanner(str, &parse_state, PE_START_EVENTS);
1698 perf_pmu__parse_cleanup();
1700 struct perf_evsel *last;
1702 if (list_empty(&parse_state.list)) {
1703 WARN_ONCE(true, "WARNING: event parser found nothing");
1707 perf_evlist__splice_list_tail(evlist, &parse_state.list);
1708 evlist->nr_groups += parse_state.nr_groups;
1709 last = perf_evlist__last(evlist);
1710 last->cmdline_group_boundary = true;
1716 * There are 2 users - builtin-record and builtin-test objects.
1717 * Both call perf_evlist__delete in case of error, so we dont
1723 #define MAX_WIDTH 1000
1724 static int get_term_width(void)
1728 get_term_dimensions(&ws);
1729 return ws.ws_col > MAX_WIDTH ? MAX_WIDTH : ws.ws_col;
1732 static void parse_events_print_error(struct parse_events_error *err,
1735 const char *str = "invalid or unsupported event: ";
1736 char _buf[MAX_WIDTH];
1737 char *buf = (char *) event;
1741 /* -2 for extra '' in the final fprintf */
1742 int width = get_term_width() - 2;
1743 int len_event = strlen(event);
1744 int len_str, max_len, cut = 0;
1747 * Maximum error index indent, we will cut
1748 * the event string if it's bigger.
1750 int max_err_idx = 13;
1753 * Let's be specific with the message when
1754 * we have the precise error.
1756 str = "event syntax error: ";
1757 len_str = strlen(str);
1758 max_len = width - len_str;
1762 /* We're cutting from the beginning. */
1763 if (err->idx > max_err_idx)
1764 cut = err->idx - max_err_idx;
1766 strncpy(buf, event + cut, max_len);
1768 /* Mark cut parts with '..' on both sides. */
1770 buf[0] = buf[1] = '.';
1772 if ((len_event - cut) > max_len) {
1773 buf[max_len - 1] = buf[max_len - 2] = '.';
1777 idx = len_str + err->idx - cut;
1780 fprintf(stderr, "%s'%s'\n", str, buf);
1782 fprintf(stderr, "%*s\\___ %s\n", idx + 1, "", err->str);
1784 fprintf(stderr, "\n%s\n", err->help);
1789 fprintf(stderr, "Run 'perf list' for a list of valid events\n");
1794 int parse_events_option(const struct option *opt, const char *str,
1795 int unset __maybe_unused)
1797 struct perf_evlist *evlist = *(struct perf_evlist **)opt->value;
1798 struct parse_events_error err = { .idx = 0, };
1799 int ret = parse_events(evlist, str, &err);
1802 parse_events_print_error(&err, str);
1808 foreach_evsel_in_last_glob(struct perf_evlist *evlist,
1809 int (*func)(struct perf_evsel *evsel,
1813 struct perf_evsel *last = NULL;
1817 * Don't return when list_empty, give func a chance to report
1818 * error when it found last == NULL.
1820 * So no need to WARN here, let *func do this.
1822 if (evlist->nr_entries > 0)
1823 last = perf_evlist__last(evlist);
1826 err = (*func)(last, arg);
1832 if (last->node.prev == &evlist->entries)
1834 last = list_entry(last->node.prev, struct perf_evsel, node);
1835 } while (!last->cmdline_group_boundary);
1840 static int set_filter(struct perf_evsel *evsel, const void *arg)
1842 const char *str = arg;
1844 int nr_addr_filters = 0;
1845 struct perf_pmu *pmu = NULL;
1850 if (evsel->attr.type == PERF_TYPE_TRACEPOINT) {
1851 if (perf_evsel__append_tp_filter(evsel, str) < 0) {
1853 "not enough memory to hold filter string\n");
1860 while ((pmu = perf_pmu__scan(pmu)) != NULL)
1861 if (pmu->type == evsel->attr.type) {
1867 perf_pmu__scan_file(pmu, "nr_addr_filters",
1868 "%d", &nr_addr_filters);
1870 if (!nr_addr_filters)
1873 if (perf_evsel__append_addr_filter(evsel, str) < 0) {
1875 "not enough memory to hold filter string\n");
1883 "--filter option should follow a -e tracepoint or HW tracer option\n");
1888 int parse_filter(const struct option *opt, const char *str,
1889 int unset __maybe_unused)
1891 struct perf_evlist *evlist = *(struct perf_evlist **)opt->value;
1893 return foreach_evsel_in_last_glob(evlist, set_filter,
1897 static int add_exclude_perf_filter(struct perf_evsel *evsel,
1898 const void *arg __maybe_unused)
1900 char new_filter[64];
1902 if (evsel == NULL || evsel->attr.type != PERF_TYPE_TRACEPOINT) {
1904 "--exclude-perf option should follow a -e tracepoint option\n");
1908 snprintf(new_filter, sizeof(new_filter), "common_pid != %d", getpid());
1910 if (perf_evsel__append_tp_filter(evsel, new_filter) < 0) {
1912 "not enough memory to hold filter string\n");
1919 int exclude_perf(const struct option *opt,
1920 const char *arg __maybe_unused,
1921 int unset __maybe_unused)
1923 struct perf_evlist *evlist = *(struct perf_evlist **)opt->value;
1925 return foreach_evsel_in_last_glob(evlist, add_exclude_perf_filter,
1929 static const char * const event_type_descriptors[] = {
1933 "Hardware cache event",
1934 "Raw hardware event descriptor",
1935 "Hardware breakpoint",
1938 static int cmp_string(const void *a, const void *b)
1940 const char * const *as = a;
1941 const char * const *bs = b;
1943 return strcmp(*as, *bs);
1947 * Print the events from <debugfs_mount_point>/tracing/events
1950 void print_tracepoint_events(const char *subsys_glob, const char *event_glob,
1953 DIR *sys_dir, *evt_dir;
1954 struct dirent *sys_dirent, *evt_dirent;
1955 char evt_path[MAXPATHLEN];
1956 char dir_path[MAXPATHLEN];
1957 char **evt_list = NULL;
1958 unsigned int evt_i = 0, evt_num = 0;
1959 bool evt_num_known = false;
1962 sys_dir = opendir(tracing_events_path);
1966 if (evt_num_known) {
1967 evt_list = zalloc(sizeof(char *) * evt_num);
1969 goto out_close_sys_dir;
1972 for_each_subsystem(sys_dir, sys_dirent) {
1973 if (subsys_glob != NULL &&
1974 !strglobmatch(sys_dirent->d_name, subsys_glob))
1977 snprintf(dir_path, MAXPATHLEN, "%s/%s", tracing_events_path,
1978 sys_dirent->d_name);
1979 evt_dir = opendir(dir_path);
1983 for_each_event(sys_dirent, evt_dir, evt_dirent) {
1984 if (event_glob != NULL &&
1985 !strglobmatch(evt_dirent->d_name, event_glob))
1988 if (!evt_num_known) {
1993 snprintf(evt_path, MAXPATHLEN, "%s:%s",
1994 sys_dirent->d_name, evt_dirent->d_name);
1996 evt_list[evt_i] = strdup(evt_path);
1997 if (evt_list[evt_i] == NULL)
1998 goto out_close_evt_dir;
2005 if (!evt_num_known) {
2006 evt_num_known = true;
2009 qsort(evt_list, evt_num, sizeof(char *), cmp_string);
2011 while (evt_i < evt_num) {
2013 printf("%s ", evt_list[evt_i++]);
2016 printf(" %-50s [%s]\n", evt_list[evt_i++],
2017 event_type_descriptors[PERF_TYPE_TRACEPOINT]);
2019 if (evt_num && pager_in_use())
2024 for (evt_i = 0; evt_i < evt_num; evt_i++)
2025 zfree(&evt_list[evt_i]);
2034 printf("FATAL: not enough memory to print %s\n",
2035 event_type_descriptors[PERF_TYPE_TRACEPOINT]);
2041 * Check whether event is in <debugfs_mount_point>/tracing/events
2044 int is_valid_tracepoint(const char *event_string)
2046 DIR *sys_dir, *evt_dir;
2047 struct dirent *sys_dirent, *evt_dirent;
2048 char evt_path[MAXPATHLEN];
2049 char dir_path[MAXPATHLEN];
2051 sys_dir = opendir(tracing_events_path);
2055 for_each_subsystem(sys_dir, sys_dirent) {
2057 snprintf(dir_path, MAXPATHLEN, "%s/%s", tracing_events_path,
2058 sys_dirent->d_name);
2059 evt_dir = opendir(dir_path);
2063 for_each_event(sys_dirent, evt_dir, evt_dirent) {
2064 snprintf(evt_path, MAXPATHLEN, "%s:%s",
2065 sys_dirent->d_name, evt_dirent->d_name);
2066 if (!strcmp(evt_path, event_string)) {
2078 static bool is_event_supported(u8 type, unsigned config)
2082 struct perf_evsel *evsel;
2083 struct perf_event_attr attr = {
2088 struct thread_map *tmap = thread_map__new_by_tid(0);
2093 evsel = perf_evsel__new(&attr);
2095 open_return = perf_evsel__open(evsel, NULL, tmap);
2096 ret = open_return >= 0;
2098 if (open_return == -EACCES) {
2100 * This happens if the paranoid value
2101 * /proc/sys/kernel/perf_event_paranoid is set to 2
2102 * Re-run with exclude_kernel set; we don't do that
2103 * by default as some ARM machines do not support it.
2106 evsel->attr.exclude_kernel = 1;
2107 ret = perf_evsel__open(evsel, NULL, tmap) >= 0;
2109 perf_evsel__delete(evsel);
2115 void print_sdt_events(const char *subsys_glob, const char *event_glob,
2118 struct probe_cache *pcache;
2119 struct probe_cache_entry *ent;
2120 struct strlist *bidlist, *sdtlist;
2121 struct strlist_config cfg = {.dont_dupstr = true};
2122 struct str_node *nd, *nd2;
2123 char *buf, *path, *ptr = NULL;
2124 bool show_detail = false;
2127 sdtlist = strlist__new(NULL, &cfg);
2129 pr_debug("Failed to allocate new strlist for SDT\n");
2132 bidlist = build_id_cache__list_all(true);
2134 pr_debug("Failed to get buildids: %d\n", errno);
2137 strlist__for_each_entry(nd, bidlist) {
2138 pcache = probe_cache__new(nd->s, NULL);
2141 list_for_each_entry(ent, &pcache->entries, node) {
2145 !strglobmatch(ent->pev.group, subsys_glob))
2148 !strglobmatch(ent->pev.event, event_glob))
2150 ret = asprintf(&buf, "%s:%s@%s", ent->pev.group,
2151 ent->pev.event, nd->s);
2153 strlist__add(sdtlist, buf);
2155 probe_cache__delete(pcache);
2157 strlist__delete(bidlist);
2159 strlist__for_each_entry(nd, sdtlist) {
2160 buf = strchr(nd->s, '@');
2164 printf("%s ", nd->s);
2167 nd2 = strlist__next(nd);
2169 ptr = strchr(nd2->s, '@');
2172 if (strcmp(nd->s, nd2->s) == 0)
2176 path = build_id_cache__origname(buf);
2177 ret = asprintf(&buf, "%s@%s(%.12s)", nd->s, path, buf);
2179 printf(" %-50s [%s]\n", buf, "SDT event");
2183 printf(" %-50s [%s]\n", nd->s, "SDT event");
2185 if (strcmp(nd->s, nd2->s) != 0)
2186 show_detail = false;
2191 strlist__delete(sdtlist);
2194 int print_hwcache_events(const char *event_glob, bool name_only)
2196 unsigned int type, op, i, evt_i = 0, evt_num = 0;
2198 char **evt_list = NULL;
2199 bool evt_num_known = false;
2202 if (evt_num_known) {
2203 evt_list = zalloc(sizeof(char *) * evt_num);
2208 for (type = 0; type < PERF_COUNT_HW_CACHE_MAX; type++) {
2209 for (op = 0; op < PERF_COUNT_HW_CACHE_OP_MAX; op++) {
2210 /* skip invalid cache type */
2211 if (!perf_evsel__is_cache_op_valid(type, op))
2214 for (i = 0; i < PERF_COUNT_HW_CACHE_RESULT_MAX; i++) {
2215 __perf_evsel__hw_cache_type_op_res_name(type, op, i,
2216 name, sizeof(name));
2217 if (event_glob != NULL && !strglobmatch(name, event_glob))
2220 if (!is_event_supported(PERF_TYPE_HW_CACHE,
2221 type | (op << 8) | (i << 16)))
2224 if (!evt_num_known) {
2229 evt_list[evt_i] = strdup(name);
2230 if (evt_list[evt_i] == NULL)
2237 if (!evt_num_known) {
2238 evt_num_known = true;
2241 qsort(evt_list, evt_num, sizeof(char *), cmp_string);
2243 while (evt_i < evt_num) {
2245 printf("%s ", evt_list[evt_i++]);
2248 printf(" %-50s [%s]\n", evt_list[evt_i++],
2249 event_type_descriptors[PERF_TYPE_HW_CACHE]);
2251 if (evt_num && pager_in_use())
2256 for (evt_i = 0; evt_i < evt_num; evt_i++)
2257 zfree(&evt_list[evt_i]);
2262 printf("FATAL: not enough memory to print %s\n", event_type_descriptors[PERF_TYPE_HW_CACHE]);
2268 void print_symbol_events(const char *event_glob, unsigned type,
2269 struct event_symbol *syms, unsigned max,
2272 unsigned int i, evt_i = 0, evt_num = 0;
2273 char name[MAX_NAME_LEN];
2274 char **evt_list = NULL;
2275 bool evt_num_known = false;
2278 if (evt_num_known) {
2279 evt_list = zalloc(sizeof(char *) * evt_num);
2285 for (i = 0; i < max; i++, syms++) {
2287 if (event_glob != NULL && syms->symbol != NULL &&
2288 !(strglobmatch(syms->symbol, event_glob) ||
2289 (syms->alias && strglobmatch(syms->alias, event_glob))))
2292 if (!is_event_supported(type, i))
2295 if (!evt_num_known) {
2300 if (!name_only && strlen(syms->alias))
2301 snprintf(name, MAX_NAME_LEN, "%s OR %s", syms->symbol, syms->alias);
2303 strlcpy(name, syms->symbol, MAX_NAME_LEN);
2305 evt_list[evt_i] = strdup(name);
2306 if (evt_list[evt_i] == NULL)
2311 if (!evt_num_known) {
2312 evt_num_known = true;
2315 qsort(evt_list, evt_num, sizeof(char *), cmp_string);
2317 while (evt_i < evt_num) {
2319 printf("%s ", evt_list[evt_i++]);
2322 printf(" %-50s [%s]\n", evt_list[evt_i++], event_type_descriptors[type]);
2324 if (evt_num && pager_in_use())
2329 for (evt_i = 0; evt_i < evt_num; evt_i++)
2330 zfree(&evt_list[evt_i]);
2335 printf("FATAL: not enough memory to print %s\n", event_type_descriptors[type]);
2341 * Print the help text for the event symbols:
2343 void print_events(const char *event_glob, bool name_only, bool quiet_flag,
2344 bool long_desc, bool details_flag)
2346 print_symbol_events(event_glob, PERF_TYPE_HARDWARE,
2347 event_symbols_hw, PERF_COUNT_HW_MAX, name_only);
2349 print_symbol_events(event_glob, PERF_TYPE_SOFTWARE,
2350 event_symbols_sw, PERF_COUNT_SW_MAX, name_only);
2352 print_hwcache_events(event_glob, name_only);
2354 print_pmu_events(event_glob, name_only, quiet_flag, long_desc,
2357 if (event_glob != NULL)
2361 printf(" %-50s [%s]\n",
2363 event_type_descriptors[PERF_TYPE_RAW]);
2364 printf(" %-50s [%s]\n",
2365 "cpu/t1=v1[,t2=v2,t3 ...]/modifier",
2366 event_type_descriptors[PERF_TYPE_RAW]);
2368 printf(" (see 'man perf-list' on how to encode it)\n\n");
2370 printf(" %-50s [%s]\n",
2371 "mem:<addr>[/len][:access]",
2372 event_type_descriptors[PERF_TYPE_BREAKPOINT]);
2377 print_tracepoint_events(NULL, NULL, name_only);
2379 print_sdt_events(NULL, NULL, name_only);
2382 int parse_events__is_hardcoded_term(struct parse_events_term *term)
2384 return term->type_term != PARSE_EVENTS__TERM_TYPE_USER;
2387 static int new_term(struct parse_events_term **_term,
2388 struct parse_events_term *temp,
2391 struct parse_events_term *term;
2393 term = malloc(sizeof(*term));
2398 INIT_LIST_HEAD(&term->list);
2401 switch (term->type_val) {
2402 case PARSE_EVENTS__TERM_TYPE_NUM:
2403 term->val.num = num;
2405 case PARSE_EVENTS__TERM_TYPE_STR:
2406 term->val.str = str;
2417 int parse_events_term__num(struct parse_events_term **term,
2418 int type_term, char *config, u64 num,
2420 void *loc_term_, void *loc_val_)
2422 YYLTYPE *loc_term = loc_term_;
2423 YYLTYPE *loc_val = loc_val_;
2425 struct parse_events_term temp = {
2426 .type_val = PARSE_EVENTS__TERM_TYPE_NUM,
2427 .type_term = type_term,
2429 .no_value = no_value,
2430 .err_term = loc_term ? loc_term->first_column : 0,
2431 .err_val = loc_val ? loc_val->first_column : 0,
2434 return new_term(term, &temp, NULL, num);
2437 int parse_events_term__str(struct parse_events_term **term,
2438 int type_term, char *config, char *str,
2439 void *loc_term_, void *loc_val_)
2441 YYLTYPE *loc_term = loc_term_;
2442 YYLTYPE *loc_val = loc_val_;
2444 struct parse_events_term temp = {
2445 .type_val = PARSE_EVENTS__TERM_TYPE_STR,
2446 .type_term = type_term,
2448 .err_term = loc_term ? loc_term->first_column : 0,
2449 .err_val = loc_val ? loc_val->first_column : 0,
2452 return new_term(term, &temp, str, 0);
2455 int parse_events_term__sym_hw(struct parse_events_term **term,
2456 char *config, unsigned idx)
2458 struct event_symbol *sym;
2459 struct parse_events_term temp = {
2460 .type_val = PARSE_EVENTS__TERM_TYPE_STR,
2461 .type_term = PARSE_EVENTS__TERM_TYPE_USER,
2462 .config = config ?: (char *) "event",
2465 BUG_ON(idx >= PERF_COUNT_HW_MAX);
2466 sym = &event_symbols_hw[idx];
2468 return new_term(term, &temp, (char *) sym->symbol, 0);
2471 int parse_events_term__clone(struct parse_events_term **new,
2472 struct parse_events_term *term)
2474 struct parse_events_term temp = {
2475 .type_val = term->type_val,
2476 .type_term = term->type_term,
2477 .config = term->config,
2478 .err_term = term->err_term,
2479 .err_val = term->err_val,
2482 return new_term(new, &temp, term->val.str, term->val.num);
2485 int parse_events_copy_term_list(struct list_head *old,
2486 struct list_head **new)
2488 struct parse_events_term *term, *n;
2496 *new = malloc(sizeof(struct list_head));
2499 INIT_LIST_HEAD(*new);
2501 list_for_each_entry (term, old, list) {
2502 ret = parse_events_term__clone(&n, term);
2505 list_add_tail(&n->list, *new);
2510 void parse_events_terms__purge(struct list_head *terms)
2512 struct parse_events_term *term, *h;
2514 list_for_each_entry_safe(term, h, terms, list) {
2515 if (term->array.nr_ranges)
2516 zfree(&term->array.ranges);
2517 list_del_init(&term->list);
2522 void parse_events_terms__delete(struct list_head *terms)
2526 parse_events_terms__purge(terms);
2530 void parse_events__clear_array(struct parse_events_array *a)
2535 void parse_events_evlist_error(struct parse_events_state *parse_state,
2536 int idx, const char *str)
2538 struct parse_events_error *err = parse_state->error;
2543 err->str = strdup(str);
2544 WARN_ONCE(!err->str, "WARNING: failed to allocate error string");
2547 static void config_terms_list(char *buf, size_t buf_sz)
2553 for (i = 0; i < __PARSE_EVENTS__TERM_TYPE_NR; i++) {
2554 const char *name = config_term_names[i];
2556 if (!config_term_avail(i, NULL))
2563 if (strlen(buf) + strlen(name) + 2 >= buf_sz)
2575 * Return string contains valid config terms of an event.
2576 * @additional_terms: For terms such as PMU sysfs terms.
2578 char *parse_events_formats_error_string(char *additional_terms)
2581 /* "no-overwrite" is the longest name */
2582 char static_terms[__PARSE_EVENTS__TERM_TYPE_NR *
2583 (sizeof("no-overwrite") - 1)];
2585 config_terms_list(static_terms, sizeof(static_terms));
2587 if (additional_terms) {
2588 if (asprintf(&str, "valid terms: %s,%s",
2589 additional_terms, static_terms) < 0)
2592 if (asprintf(&str, "valid terms: %s", static_terms) < 0)