1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/hw_breakpoint.h>
4 #include <linux/zalloc.h>
11 #include <sys/param.h>
16 #include <subcmd/pager.h>
17 #include <subcmd/parse-options.h>
18 #include "parse-events.h"
19 #include <subcmd/exec-cmd.h>
24 #include "bpf-loader.h"
26 #include <api/fs/tracing_path.h>
27 #include <perf/cpumap.h>
28 #include "parse-events-bison.h"
29 #define YY_EXTRA_TYPE void*
30 #include "parse-events-flex.h"
32 #include "thread_map.h"
33 #include "probe-file.h"
35 #include "util/parse-branch-options.h"
36 #include "metricgroup.h"
37 #include "util/evsel_config.h"
38 #include "util/event.h"
40 #include "util/parse-events-hybrid.h"
43 #define MAX_NAME_LEN 100
46 extern int parse_events_debug;
48 int parse_events_parse(void *parse_state, void *scanner);
49 static int get_config_terms(struct list_head *head_config,
50 struct list_head *head_terms __maybe_unused);
52 static struct perf_pmu_event_symbol *perf_pmu_events_list;
54 * The variable indicates the number of supported pmu event symbols.
55 * 0 means not initialized and ready to init
56 * -1 means failed to init, don't try anymore
57 * >0 is the number of supported pmu event symbols
59 static int perf_pmu_events_list_num;
61 struct event_symbol event_symbols_hw[PERF_COUNT_HW_MAX] = {
62 [PERF_COUNT_HW_CPU_CYCLES] = {
63 .symbol = "cpu-cycles",
66 [PERF_COUNT_HW_INSTRUCTIONS] = {
67 .symbol = "instructions",
70 [PERF_COUNT_HW_CACHE_REFERENCES] = {
71 .symbol = "cache-references",
74 [PERF_COUNT_HW_CACHE_MISSES] = {
75 .symbol = "cache-misses",
78 [PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = {
79 .symbol = "branch-instructions",
82 [PERF_COUNT_HW_BRANCH_MISSES] = {
83 .symbol = "branch-misses",
86 [PERF_COUNT_HW_BUS_CYCLES] = {
87 .symbol = "bus-cycles",
90 [PERF_COUNT_HW_STALLED_CYCLES_FRONTEND] = {
91 .symbol = "stalled-cycles-frontend",
92 .alias = "idle-cycles-frontend",
94 [PERF_COUNT_HW_STALLED_CYCLES_BACKEND] = {
95 .symbol = "stalled-cycles-backend",
96 .alias = "idle-cycles-backend",
98 [PERF_COUNT_HW_REF_CPU_CYCLES] = {
99 .symbol = "ref-cycles",
104 struct event_symbol event_symbols_sw[PERF_COUNT_SW_MAX] = {
105 [PERF_COUNT_SW_CPU_CLOCK] = {
106 .symbol = "cpu-clock",
109 [PERF_COUNT_SW_TASK_CLOCK] = {
110 .symbol = "task-clock",
113 [PERF_COUNT_SW_PAGE_FAULTS] = {
114 .symbol = "page-faults",
117 [PERF_COUNT_SW_CONTEXT_SWITCHES] = {
118 .symbol = "context-switches",
121 [PERF_COUNT_SW_CPU_MIGRATIONS] = {
122 .symbol = "cpu-migrations",
123 .alias = "migrations",
125 [PERF_COUNT_SW_PAGE_FAULTS_MIN] = {
126 .symbol = "minor-faults",
129 [PERF_COUNT_SW_PAGE_FAULTS_MAJ] = {
130 .symbol = "major-faults",
133 [PERF_COUNT_SW_ALIGNMENT_FAULTS] = {
134 .symbol = "alignment-faults",
137 [PERF_COUNT_SW_EMULATION_FAULTS] = {
138 .symbol = "emulation-faults",
141 [PERF_COUNT_SW_DUMMY] = {
145 [PERF_COUNT_SW_BPF_OUTPUT] = {
146 .symbol = "bpf-output",
151 #define __PERF_EVENT_FIELD(config, name) \
152 ((config & PERF_EVENT_##name##_MASK) >> PERF_EVENT_##name##_SHIFT)
154 #define PERF_EVENT_RAW(config) __PERF_EVENT_FIELD(config, RAW)
155 #define PERF_EVENT_CONFIG(config) __PERF_EVENT_FIELD(config, CONFIG)
156 #define PERF_EVENT_TYPE(config) __PERF_EVENT_FIELD(config, TYPE)
157 #define PERF_EVENT_ID(config) __PERF_EVENT_FIELD(config, EVENT)
159 #define for_each_subsystem(sys_dir, sys_dirent) \
160 while ((sys_dirent = readdir(sys_dir)) != NULL) \
161 if (sys_dirent->d_type == DT_DIR && \
162 (strcmp(sys_dirent->d_name, ".")) && \
163 (strcmp(sys_dirent->d_name, "..")))
165 static int tp_event_has_id(const char *dir_path, struct dirent *evt_dir)
167 char evt_path[MAXPATHLEN];
170 snprintf(evt_path, MAXPATHLEN, "%s/%s/id", dir_path, evt_dir->d_name);
171 fd = open(evt_path, O_RDONLY);
179 #define for_each_event(dir_path, evt_dir, evt_dirent) \
180 while ((evt_dirent = readdir(evt_dir)) != NULL) \
181 if (evt_dirent->d_type == DT_DIR && \
182 (strcmp(evt_dirent->d_name, ".")) && \
183 (strcmp(evt_dirent->d_name, "..")) && \
184 (!tp_event_has_id(dir_path, evt_dirent)))
186 #define MAX_EVENT_LENGTH 512
188 void parse_events__handle_error(struct parse_events_error *err, int idx,
189 char *str, char *help)
191 if (WARN(!str, "WARNING: failed to provide error string\n")) {
195 switch (err->num_errors) {
202 err->first_idx = err->idx;
204 err->first_str = err->str;
206 err->first_help = err->help;
210 pr_debug("Multiple errors dropping message: %s (%s)\n",
211 err->str, err->help);
221 struct tracepoint_path *tracepoint_id_to_path(u64 config)
223 struct tracepoint_path *path = NULL;
224 DIR *sys_dir, *evt_dir;
225 struct dirent *sys_dirent, *evt_dirent;
229 char evt_path[MAXPATHLEN];
232 sys_dir = tracing_events__opendir();
236 for_each_subsystem(sys_dir, sys_dirent) {
237 dir_path = get_events_file(sys_dirent->d_name);
240 evt_dir = opendir(dir_path);
244 for_each_event(dir_path, evt_dir, evt_dirent) {
246 scnprintf(evt_path, MAXPATHLEN, "%s/%s/id", dir_path,
248 fd = open(evt_path, O_RDONLY);
251 if (read(fd, id_buf, sizeof(id_buf)) < 0) {
258 put_events_file(dir_path);
261 path = zalloc(sizeof(*path));
264 if (asprintf(&path->system, "%.*s", MAX_EVENT_LENGTH, sys_dirent->d_name) < 0) {
268 if (asprintf(&path->name, "%.*s", MAX_EVENT_LENGTH, evt_dirent->d_name) < 0) {
269 zfree(&path->system);
278 put_events_file(dir_path);
285 struct tracepoint_path *tracepoint_name_to_path(const char *name)
287 struct tracepoint_path *path = zalloc(sizeof(*path));
288 char *str = strchr(name, ':');
290 if (path == NULL || str == NULL) {
295 path->system = strndup(name, str - name);
296 path->name = strdup(str+1);
298 if (path->system == NULL || path->name == NULL) {
299 zfree(&path->system);
307 const char *event_type(int type)
310 case PERF_TYPE_HARDWARE:
313 case PERF_TYPE_SOFTWARE:
316 case PERF_TYPE_TRACEPOINT:
319 case PERF_TYPE_HW_CACHE:
320 return "hardware-cache";
329 static int parse_events__is_name_term(struct parse_events_term *term)
331 return term->type_term == PARSE_EVENTS__TERM_TYPE_NAME;
334 static char *get_config_name(struct list_head *head_terms)
336 struct parse_events_term *term;
341 list_for_each_entry(term, head_terms, list)
342 if (parse_events__is_name_term(term))
343 return term->val.str;
348 static struct evsel *
349 __add_event(struct list_head *list, int *idx,
350 struct perf_event_attr *attr,
352 char *name, struct perf_pmu *pmu,
353 struct list_head *config_terms, bool auto_merge_stats,
354 const char *cpu_list)
357 struct perf_cpu_map *cpus = pmu ? perf_cpu_map__get(pmu->cpus) :
358 cpu_list ? perf_cpu_map__new(cpu_list) : NULL;
360 if (pmu && attr->type == PERF_TYPE_RAW)
361 perf_pmu__warn_invalid_config(pmu, attr->config, name);
364 event_attr_init(attr);
366 evsel = evsel__new_idx(attr, *idx);
368 perf_cpu_map__put(cpus);
373 evsel->core.cpus = cpus;
374 evsel->core.own_cpus = perf_cpu_map__get(cpus);
375 evsel->core.system_wide = pmu ? pmu->is_uncore : false;
376 evsel->auto_merge_stats = auto_merge_stats;
379 evsel->name = strdup(name);
382 list_splice(config_terms, &evsel->config_terms);
385 list_add_tail(&evsel->core.node, list);
390 struct evsel *parse_events__add_event(int idx, struct perf_event_attr *attr,
391 char *name, struct perf_pmu *pmu)
393 return __add_event(NULL, &idx, attr, false, name, pmu, NULL, false,
397 static int add_event(struct list_head *list, int *idx,
398 struct perf_event_attr *attr, char *name,
399 struct list_head *config_terms)
401 return __add_event(list, idx, attr, true, name, NULL, config_terms,
402 false, NULL) ? 0 : -ENOMEM;
405 static int add_event_tool(struct list_head *list, int *idx,
406 enum perf_tool_event tool_event)
409 struct perf_event_attr attr = {
410 .type = PERF_TYPE_SOFTWARE,
411 .config = PERF_COUNT_SW_DUMMY,
414 evsel = __add_event(list, idx, &attr, true, NULL, NULL, NULL, false,
418 evsel->tool_event = tool_event;
419 if (tool_event == PERF_TOOL_DURATION_TIME)
424 static int parse_aliases(char *str, const char *names[][EVSEL__MAX_ALIASES], int size)
429 for (i = 0; i < size; i++) {
430 for (j = 0; j < EVSEL__MAX_ALIASES && names[i][j]; j++) {
431 n = strlen(names[i][j]);
432 if (n > longest && !strncasecmp(str, names[i][j], n))
442 typedef int config_term_func_t(struct perf_event_attr *attr,
443 struct parse_events_term *term,
444 struct parse_events_error *err);
445 static int config_term_common(struct perf_event_attr *attr,
446 struct parse_events_term *term,
447 struct parse_events_error *err);
448 static int config_attr(struct perf_event_attr *attr,
449 struct list_head *head,
450 struct parse_events_error *err,
451 config_term_func_t config_term);
453 int parse_events_add_cache(struct list_head *list, int *idx,
454 char *type, char *op_result1, char *op_result2,
455 struct parse_events_error *err,
456 struct list_head *head_config)
458 struct perf_event_attr attr;
459 LIST_HEAD(config_terms);
460 char name[MAX_NAME_LEN], *config_name;
461 int cache_type = -1, cache_op = -1, cache_result = -1;
462 char *op_result[2] = { op_result1, op_result2 };
467 * No fallback - if we cannot get a clear cache type
470 cache_type = parse_aliases(type, evsel__hw_cache, PERF_COUNT_HW_CACHE_MAX);
471 if (cache_type == -1)
474 config_name = get_config_name(head_config);
475 n = snprintf(name, MAX_NAME_LEN, "%s", type);
477 for (i = 0; (i < 2) && (op_result[i]); i++) {
478 char *str = op_result[i];
480 n += snprintf(name + n, MAX_NAME_LEN - n, "-%s", str);
482 if (cache_op == -1) {
483 cache_op = parse_aliases(str, evsel__hw_cache_op,
484 PERF_COUNT_HW_CACHE_OP_MAX);
486 if (!evsel__is_cache_op_valid(cache_type, cache_op))
492 if (cache_result == -1) {
493 cache_result = parse_aliases(str, evsel__hw_cache_result,
494 PERF_COUNT_HW_CACHE_RESULT_MAX);
495 if (cache_result >= 0)
501 * Fall back to reads:
504 cache_op = PERF_COUNT_HW_CACHE_OP_READ;
507 * Fall back to accesses:
509 if (cache_result == -1)
510 cache_result = PERF_COUNT_HW_CACHE_RESULT_ACCESS;
512 memset(&attr, 0, sizeof(attr));
513 attr.config = cache_type | (cache_op << 8) | (cache_result << 16);
514 attr.type = PERF_TYPE_HW_CACHE;
517 if (config_attr(&attr, head_config, err,
521 if (get_config_terms(head_config, &config_terms))
525 ret = parse_events__add_cache_hybrid(list, idx, &attr,
526 config_name ? : name, &config_terms,
531 return add_event(list, idx, &attr, config_name ? : name, &config_terms);
534 static void tracepoint_error(struct parse_events_error *e, int err,
535 const char *sys, const char *name)
544 * We get error directly from syscall errno ( > 0),
545 * or from encoded pointer's error ( < 0).
551 str = "can't access trace events";
554 str = "unknown tracepoint";
557 str = "failed to add tracepoint";
561 tracing_path__strerror_open_tp(err, help, sizeof(help), sys, name);
562 parse_events__handle_error(e, 0, strdup(str), strdup(help));
565 static int add_tracepoint(struct list_head *list, int *idx,
566 const char *sys_name, const char *evt_name,
567 struct parse_events_error *err,
568 struct list_head *head_config)
570 struct evsel *evsel = evsel__newtp_idx(sys_name, evt_name, (*idx)++);
573 tracepoint_error(err, PTR_ERR(evsel), sys_name, evt_name);
574 return PTR_ERR(evsel);
578 LIST_HEAD(config_terms);
580 if (get_config_terms(head_config, &config_terms))
582 list_splice(&config_terms, &evsel->config_terms);
585 list_add_tail(&evsel->core.node, list);
589 static int add_tracepoint_multi_event(struct list_head *list, int *idx,
590 const char *sys_name, const char *evt_name,
591 struct parse_events_error *err,
592 struct list_head *head_config)
595 struct dirent *evt_ent;
597 int ret = 0, found = 0;
599 evt_path = get_events_file(sys_name);
601 tracepoint_error(err, errno, sys_name, evt_name);
604 evt_dir = opendir(evt_path);
606 put_events_file(evt_path);
607 tracepoint_error(err, errno, sys_name, evt_name);
611 while (!ret && (evt_ent = readdir(evt_dir))) {
612 if (!strcmp(evt_ent->d_name, ".")
613 || !strcmp(evt_ent->d_name, "..")
614 || !strcmp(evt_ent->d_name, "enable")
615 || !strcmp(evt_ent->d_name, "filter"))
618 if (!strglobmatch(evt_ent->d_name, evt_name))
623 ret = add_tracepoint(list, idx, sys_name, evt_ent->d_name,
628 tracepoint_error(err, ENOENT, sys_name, evt_name);
632 put_events_file(evt_path);
637 static int add_tracepoint_event(struct list_head *list, int *idx,
638 const char *sys_name, const char *evt_name,
639 struct parse_events_error *err,
640 struct list_head *head_config)
642 return strpbrk(evt_name, "*?") ?
643 add_tracepoint_multi_event(list, idx, sys_name, evt_name,
645 add_tracepoint(list, idx, sys_name, evt_name,
649 static int add_tracepoint_multi_sys(struct list_head *list, int *idx,
650 const char *sys_name, const char *evt_name,
651 struct parse_events_error *err,
652 struct list_head *head_config)
654 struct dirent *events_ent;
658 events_dir = tracing_events__opendir();
660 tracepoint_error(err, errno, sys_name, evt_name);
664 while (!ret && (events_ent = readdir(events_dir))) {
665 if (!strcmp(events_ent->d_name, ".")
666 || !strcmp(events_ent->d_name, "..")
667 || !strcmp(events_ent->d_name, "enable")
668 || !strcmp(events_ent->d_name, "header_event")
669 || !strcmp(events_ent->d_name, "header_page"))
672 if (!strglobmatch(events_ent->d_name, sys_name))
675 ret = add_tracepoint_event(list, idx, events_ent->d_name,
676 evt_name, err, head_config);
679 closedir(events_dir);
683 #ifdef HAVE_LIBBPF_SUPPORT
684 struct __add_bpf_event_param {
685 struct parse_events_state *parse_state;
686 struct list_head *list;
687 struct list_head *head_config;
690 static int add_bpf_event(const char *group, const char *event, int fd, struct bpf_object *obj,
693 LIST_HEAD(new_evsels);
694 struct __add_bpf_event_param *param = _param;
695 struct parse_events_state *parse_state = param->parse_state;
696 struct list_head *list = param->list;
700 * Check if we should add the event, i.e. if it is a TP but starts with a '!',
701 * then don't add the tracepoint, this will be used for something else, like
702 * adding to a BPF_MAP_TYPE_PROG_ARRAY.
704 * See tools/perf/examples/bpf/augmented_raw_syscalls.c
709 pr_debug("add bpf event %s:%s and attach bpf program %d\n",
712 err = parse_events_add_tracepoint(&new_evsels, &parse_state->idx, group,
713 event, parse_state->error,
716 struct evsel *evsel, *tmp;
718 pr_debug("Failed to add BPF event %s:%s\n",
720 list_for_each_entry_safe(evsel, tmp, &new_evsels, core.node) {
721 list_del_init(&evsel->core.node);
722 evsel__delete(evsel);
726 pr_debug("adding %s:%s\n", group, event);
728 list_for_each_entry(pos, &new_evsels, core.node) {
729 pr_debug("adding %s:%s to %p\n",
734 list_splice(&new_evsels, list);
738 int parse_events_load_bpf_obj(struct parse_events_state *parse_state,
739 struct list_head *list,
740 struct bpf_object *obj,
741 struct list_head *head_config)
745 struct __add_bpf_event_param param = {parse_state, list, head_config};
746 static bool registered_unprobe_atexit = false;
748 if (IS_ERR(obj) || !obj) {
749 snprintf(errbuf, sizeof(errbuf),
750 "Internal error: load bpf obj with NULL");
756 * Register atexit handler before calling bpf__probe() so
757 * bpf__probe() don't need to unprobe probe points its already
758 * created when failure.
760 if (!registered_unprobe_atexit) {
762 registered_unprobe_atexit = true;
765 err = bpf__probe(obj);
767 bpf__strerror_probe(obj, err, errbuf, sizeof(errbuf));
771 err = bpf__load(obj);
773 bpf__strerror_load(obj, err, errbuf, sizeof(errbuf));
777 err = bpf__foreach_event(obj, add_bpf_event, ¶m);
779 snprintf(errbuf, sizeof(errbuf),
780 "Attach events in BPF object failed");
786 parse_events__handle_error(parse_state->error, 0,
787 strdup(errbuf), strdup("(add -v to see detail)"));
792 parse_events_config_bpf(struct parse_events_state *parse_state,
793 struct bpf_object *obj,
794 struct list_head *head_config)
796 struct parse_events_term *term;
799 if (!head_config || list_empty(head_config))
802 list_for_each_entry(term, head_config, list) {
805 if (term->type_term != PARSE_EVENTS__TERM_TYPE_USER) {
806 parse_events__handle_error(parse_state->error, term->err_term,
807 strdup("Invalid config term for BPF object"),
812 err = bpf__config_obj(obj, term, parse_state->evlist, &error_pos);
817 bpf__strerror_config_obj(obj, term, parse_state->evlist,
818 &error_pos, err, errbuf,
821 if (err == -BPF_LOADER_ERRNO__OBJCONF_MAP_VALUE)
824 idx = term->err_term + error_pos;
826 parse_events__handle_error(parse_state->error, idx,
829 "Hint:\tValid config terms:\n"
830 " \tmap:[<arraymap>].value<indices>=[value]\n"
831 " \tmap:[<eventmap>].event<indices>=[event]\n"
833 " \twhere <indices> is something like [0,3...5] or [all]\n"
834 " \t(add -v to see detail)"));
842 * Split config terms:
843 * perf record -e bpf.c/call-graph=fp,map:array.value[0]=1/ ...
844 * 'call-graph=fp' is 'evt config', should be applied to each
846 * 'map:array.value[0]=1' is 'obj config', should be processed
847 * with parse_events_config_bpf.
849 * Move object config terms from the first list to obj_head_config.
852 split_bpf_config_terms(struct list_head *evt_head_config,
853 struct list_head *obj_head_config)
855 struct parse_events_term *term, *temp;
858 * Currently, all possible user config term
859 * belong to bpf object. parse_events__is_hardcoded_term()
860 * happens to be a good flag.
862 * See parse_events_config_bpf() and
863 * config_term_tracepoint().
865 list_for_each_entry_safe(term, temp, evt_head_config, list)
866 if (!parse_events__is_hardcoded_term(term))
867 list_move_tail(&term->list, obj_head_config);
870 int parse_events_load_bpf(struct parse_events_state *parse_state,
871 struct list_head *list,
874 struct list_head *head_config)
877 struct bpf_object *obj;
878 LIST_HEAD(obj_head_config);
881 split_bpf_config_terms(head_config, &obj_head_config);
883 obj = bpf__prepare_load(bpf_file_name, source);
890 snprintf(errbuf, sizeof(errbuf),
891 "BPF support is not compiled");
893 bpf__strerror_prepare_load(bpf_file_name,
898 parse_events__handle_error(parse_state->error, 0,
899 strdup(errbuf), strdup("(add -v to see detail)"));
903 err = parse_events_load_bpf_obj(parse_state, list, obj, head_config);
906 err = parse_events_config_bpf(parse_state, obj, &obj_head_config);
909 * Caller doesn't know anything about obj_head_config,
910 * so combine them together again before returning.
913 list_splice_tail(&obj_head_config, head_config);
916 #else // HAVE_LIBBPF_SUPPORT
917 int parse_events_load_bpf_obj(struct parse_events_state *parse_state,
918 struct list_head *list __maybe_unused,
919 struct bpf_object *obj __maybe_unused,
920 struct list_head *head_config __maybe_unused)
922 parse_events__handle_error(parse_state->error, 0,
923 strdup("BPF support is not compiled"),
924 strdup("Make sure libbpf-devel is available at build time."));
928 int parse_events_load_bpf(struct parse_events_state *parse_state,
929 struct list_head *list __maybe_unused,
930 char *bpf_file_name __maybe_unused,
931 bool source __maybe_unused,
932 struct list_head *head_config __maybe_unused)
934 parse_events__handle_error(parse_state->error, 0,
935 strdup("BPF support is not compiled"),
936 strdup("Make sure libbpf-devel is available at build time."));
939 #endif // HAVE_LIBBPF_SUPPORT
942 parse_breakpoint_type(const char *type, struct perf_event_attr *attr)
946 for (i = 0; i < 3; i++) {
947 if (!type || !type[i])
950 #define CHECK_SET_TYPE(bit) \
952 if (attr->bp_type & bit) \
955 attr->bp_type |= bit; \
960 CHECK_SET_TYPE(HW_BREAKPOINT_R);
963 CHECK_SET_TYPE(HW_BREAKPOINT_W);
966 CHECK_SET_TYPE(HW_BREAKPOINT_X);
973 #undef CHECK_SET_TYPE
975 if (!attr->bp_type) /* Default */
976 attr->bp_type = HW_BREAKPOINT_R | HW_BREAKPOINT_W;
981 int parse_events_add_breakpoint(struct list_head *list, int *idx,
982 u64 addr, char *type, u64 len)
984 struct perf_event_attr attr;
986 memset(&attr, 0, sizeof(attr));
989 if (parse_breakpoint_type(type, &attr))
992 /* Provide some defaults if len is not specified */
994 if (attr.bp_type == HW_BREAKPOINT_X)
997 len = HW_BREAKPOINT_LEN_4;
1002 attr.type = PERF_TYPE_BREAKPOINT;
1003 attr.sample_period = 1;
1005 return add_event(list, idx, &attr, NULL, NULL);
1008 static int check_type_val(struct parse_events_term *term,
1009 struct parse_events_error *err,
1012 if (type == term->type_val)
1016 parse_events__handle_error(err, term->err_val,
1017 type == PARSE_EVENTS__TERM_TYPE_NUM
1018 ? strdup("expected numeric value")
1019 : strdup("expected string value"),
1026 * Update according to parse-events.l
1028 static const char *config_term_names[__PARSE_EVENTS__TERM_TYPE_NR] = {
1029 [PARSE_EVENTS__TERM_TYPE_USER] = "<sysfs term>",
1030 [PARSE_EVENTS__TERM_TYPE_CONFIG] = "config",
1031 [PARSE_EVENTS__TERM_TYPE_CONFIG1] = "config1",
1032 [PARSE_EVENTS__TERM_TYPE_CONFIG2] = "config2",
1033 [PARSE_EVENTS__TERM_TYPE_NAME] = "name",
1034 [PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD] = "period",
1035 [PARSE_EVENTS__TERM_TYPE_SAMPLE_FREQ] = "freq",
1036 [PARSE_EVENTS__TERM_TYPE_BRANCH_SAMPLE_TYPE] = "branch_type",
1037 [PARSE_EVENTS__TERM_TYPE_TIME] = "time",
1038 [PARSE_EVENTS__TERM_TYPE_CALLGRAPH] = "call-graph",
1039 [PARSE_EVENTS__TERM_TYPE_STACKSIZE] = "stack-size",
1040 [PARSE_EVENTS__TERM_TYPE_NOINHERIT] = "no-inherit",
1041 [PARSE_EVENTS__TERM_TYPE_INHERIT] = "inherit",
1042 [PARSE_EVENTS__TERM_TYPE_MAX_STACK] = "max-stack",
1043 [PARSE_EVENTS__TERM_TYPE_MAX_EVENTS] = "nr",
1044 [PARSE_EVENTS__TERM_TYPE_OVERWRITE] = "overwrite",
1045 [PARSE_EVENTS__TERM_TYPE_NOOVERWRITE] = "no-overwrite",
1046 [PARSE_EVENTS__TERM_TYPE_DRV_CFG] = "driver-config",
1047 [PARSE_EVENTS__TERM_TYPE_PERCORE] = "percore",
1048 [PARSE_EVENTS__TERM_TYPE_AUX_OUTPUT] = "aux-output",
1049 [PARSE_EVENTS__TERM_TYPE_AUX_SAMPLE_SIZE] = "aux-sample-size",
1052 static bool config_term_shrinked;
1055 config_term_avail(int term_type, struct parse_events_error *err)
1059 if (term_type < 0 || term_type >= __PARSE_EVENTS__TERM_TYPE_NR) {
1060 parse_events__handle_error(err, -1,
1061 strdup("Invalid term_type"), NULL);
1064 if (!config_term_shrinked)
1067 switch (term_type) {
1068 case PARSE_EVENTS__TERM_TYPE_CONFIG:
1069 case PARSE_EVENTS__TERM_TYPE_CONFIG1:
1070 case PARSE_EVENTS__TERM_TYPE_CONFIG2:
1071 case PARSE_EVENTS__TERM_TYPE_NAME:
1072 case PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD:
1073 case PARSE_EVENTS__TERM_TYPE_PERCORE:
1079 /* term_type is validated so indexing is safe */
1080 if (asprintf(&err_str, "'%s' is not usable in 'perf stat'",
1081 config_term_names[term_type]) >= 0)
1082 parse_events__handle_error(err, -1, err_str, NULL);
1087 void parse_events__shrink_config_terms(void)
1089 config_term_shrinked = true;
1092 static int config_term_common(struct perf_event_attr *attr,
1093 struct parse_events_term *term,
1094 struct parse_events_error *err)
1096 #define CHECK_TYPE_VAL(type) \
1098 if (check_type_val(term, err, PARSE_EVENTS__TERM_TYPE_ ## type)) \
1102 switch (term->type_term) {
1103 case PARSE_EVENTS__TERM_TYPE_CONFIG:
1104 CHECK_TYPE_VAL(NUM);
1105 attr->config = term->val.num;
1107 case PARSE_EVENTS__TERM_TYPE_CONFIG1:
1108 CHECK_TYPE_VAL(NUM);
1109 attr->config1 = term->val.num;
1111 case PARSE_EVENTS__TERM_TYPE_CONFIG2:
1112 CHECK_TYPE_VAL(NUM);
1113 attr->config2 = term->val.num;
1115 case PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD:
1116 CHECK_TYPE_VAL(NUM);
1118 case PARSE_EVENTS__TERM_TYPE_SAMPLE_FREQ:
1119 CHECK_TYPE_VAL(NUM);
1121 case PARSE_EVENTS__TERM_TYPE_BRANCH_SAMPLE_TYPE:
1122 CHECK_TYPE_VAL(STR);
1123 if (strcmp(term->val.str, "no") &&
1124 parse_branch_str(term->val.str,
1125 &attr->branch_sample_type)) {
1126 parse_events__handle_error(err, term->err_val,
1127 strdup("invalid branch sample type"),
1132 case PARSE_EVENTS__TERM_TYPE_TIME:
1133 CHECK_TYPE_VAL(NUM);
1134 if (term->val.num > 1) {
1135 parse_events__handle_error(err, term->err_val,
1136 strdup("expected 0 or 1"),
1141 case PARSE_EVENTS__TERM_TYPE_CALLGRAPH:
1142 CHECK_TYPE_VAL(STR);
1144 case PARSE_EVENTS__TERM_TYPE_STACKSIZE:
1145 CHECK_TYPE_VAL(NUM);
1147 case PARSE_EVENTS__TERM_TYPE_INHERIT:
1148 CHECK_TYPE_VAL(NUM);
1150 case PARSE_EVENTS__TERM_TYPE_NOINHERIT:
1151 CHECK_TYPE_VAL(NUM);
1153 case PARSE_EVENTS__TERM_TYPE_OVERWRITE:
1154 CHECK_TYPE_VAL(NUM);
1156 case PARSE_EVENTS__TERM_TYPE_NOOVERWRITE:
1157 CHECK_TYPE_VAL(NUM);
1159 case PARSE_EVENTS__TERM_TYPE_NAME:
1160 CHECK_TYPE_VAL(STR);
1162 case PARSE_EVENTS__TERM_TYPE_MAX_STACK:
1163 CHECK_TYPE_VAL(NUM);
1165 case PARSE_EVENTS__TERM_TYPE_MAX_EVENTS:
1166 CHECK_TYPE_VAL(NUM);
1168 case PARSE_EVENTS__TERM_TYPE_PERCORE:
1169 CHECK_TYPE_VAL(NUM);
1170 if ((unsigned int)term->val.num > 1) {
1171 parse_events__handle_error(err, term->err_val,
1172 strdup("expected 0 or 1"),
1177 case PARSE_EVENTS__TERM_TYPE_AUX_OUTPUT:
1178 CHECK_TYPE_VAL(NUM);
1180 case PARSE_EVENTS__TERM_TYPE_AUX_SAMPLE_SIZE:
1181 CHECK_TYPE_VAL(NUM);
1182 if (term->val.num > UINT_MAX) {
1183 parse_events__handle_error(err, term->err_val,
1190 parse_events__handle_error(err, term->err_term,
1191 strdup("unknown term"),
1192 parse_events_formats_error_string(NULL));
1197 * Check term availability after basic checking so
1198 * PARSE_EVENTS__TERM_TYPE_USER can be found and filtered.
1200 * If check availability at the entry of this function,
1201 * user will see "'<sysfs term>' is not usable in 'perf stat'"
1202 * if an invalid config term is provided for legacy events
1203 * (for example, instructions/badterm/...), which is confusing.
1205 if (!config_term_avail(term->type_term, err))
1208 #undef CHECK_TYPE_VAL
1211 static int config_term_pmu(struct perf_event_attr *attr,
1212 struct parse_events_term *term,
1213 struct parse_events_error *err)
1215 if (term->type_term == PARSE_EVENTS__TERM_TYPE_USER ||
1216 term->type_term == PARSE_EVENTS__TERM_TYPE_DRV_CFG)
1218 * Always succeed for sysfs terms, as we dont know
1219 * at this point what type they need to have.
1223 return config_term_common(attr, term, err);
1226 static int config_term_tracepoint(struct perf_event_attr *attr,
1227 struct parse_events_term *term,
1228 struct parse_events_error *err)
1230 switch (term->type_term) {
1231 case PARSE_EVENTS__TERM_TYPE_CALLGRAPH:
1232 case PARSE_EVENTS__TERM_TYPE_STACKSIZE:
1233 case PARSE_EVENTS__TERM_TYPE_INHERIT:
1234 case PARSE_EVENTS__TERM_TYPE_NOINHERIT:
1235 case PARSE_EVENTS__TERM_TYPE_MAX_STACK:
1236 case PARSE_EVENTS__TERM_TYPE_MAX_EVENTS:
1237 case PARSE_EVENTS__TERM_TYPE_OVERWRITE:
1238 case PARSE_EVENTS__TERM_TYPE_NOOVERWRITE:
1239 case PARSE_EVENTS__TERM_TYPE_AUX_OUTPUT:
1240 case PARSE_EVENTS__TERM_TYPE_AUX_SAMPLE_SIZE:
1241 return config_term_common(attr, term, err);
1244 parse_events__handle_error(err, term->err_term,
1245 strdup("unknown term"),
1246 strdup("valid terms: call-graph,stack-size\n"));
1254 static int config_attr(struct perf_event_attr *attr,
1255 struct list_head *head,
1256 struct parse_events_error *err,
1257 config_term_func_t config_term)
1259 struct parse_events_term *term;
1261 list_for_each_entry(term, head, list)
1262 if (config_term(attr, term, err))
1268 static int get_config_terms(struct list_head *head_config,
1269 struct list_head *head_terms __maybe_unused)
1271 #define ADD_CONFIG_TERM(__type, __weak) \
1272 struct evsel_config_term *__t; \
1274 __t = zalloc(sizeof(*__t)); \
1278 INIT_LIST_HEAD(&__t->list); \
1279 __t->type = EVSEL__CONFIG_TERM_ ## __type; \
1280 __t->weak = __weak; \
1281 list_add_tail(&__t->list, head_terms)
1283 #define ADD_CONFIG_TERM_VAL(__type, __name, __val, __weak) \
1285 ADD_CONFIG_TERM(__type, __weak); \
1286 __t->val.__name = __val; \
1289 #define ADD_CONFIG_TERM_STR(__type, __val, __weak) \
1291 ADD_CONFIG_TERM(__type, __weak); \
1292 __t->val.str = strdup(__val); \
1293 if (!__t->val.str) { \
1297 __t->free_str = true; \
1300 struct parse_events_term *term;
1302 list_for_each_entry(term, head_config, list) {
1303 switch (term->type_term) {
1304 case PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD:
1305 ADD_CONFIG_TERM_VAL(PERIOD, period, term->val.num, term->weak);
1307 case PARSE_EVENTS__TERM_TYPE_SAMPLE_FREQ:
1308 ADD_CONFIG_TERM_VAL(FREQ, freq, term->val.num, term->weak);
1310 case PARSE_EVENTS__TERM_TYPE_TIME:
1311 ADD_CONFIG_TERM_VAL(TIME, time, term->val.num, term->weak);
1313 case PARSE_EVENTS__TERM_TYPE_CALLGRAPH:
1314 ADD_CONFIG_TERM_STR(CALLGRAPH, term->val.str, term->weak);
1316 case PARSE_EVENTS__TERM_TYPE_BRANCH_SAMPLE_TYPE:
1317 ADD_CONFIG_TERM_STR(BRANCH, term->val.str, term->weak);
1319 case PARSE_EVENTS__TERM_TYPE_STACKSIZE:
1320 ADD_CONFIG_TERM_VAL(STACK_USER, stack_user,
1321 term->val.num, term->weak);
1323 case PARSE_EVENTS__TERM_TYPE_INHERIT:
1324 ADD_CONFIG_TERM_VAL(INHERIT, inherit,
1325 term->val.num ? 1 : 0, term->weak);
1327 case PARSE_EVENTS__TERM_TYPE_NOINHERIT:
1328 ADD_CONFIG_TERM_VAL(INHERIT, inherit,
1329 term->val.num ? 0 : 1, term->weak);
1331 case PARSE_EVENTS__TERM_TYPE_MAX_STACK:
1332 ADD_CONFIG_TERM_VAL(MAX_STACK, max_stack,
1333 term->val.num, term->weak);
1335 case PARSE_EVENTS__TERM_TYPE_MAX_EVENTS:
1336 ADD_CONFIG_TERM_VAL(MAX_EVENTS, max_events,
1337 term->val.num, term->weak);
1339 case PARSE_EVENTS__TERM_TYPE_OVERWRITE:
1340 ADD_CONFIG_TERM_VAL(OVERWRITE, overwrite,
1341 term->val.num ? 1 : 0, term->weak);
1343 case PARSE_EVENTS__TERM_TYPE_NOOVERWRITE:
1344 ADD_CONFIG_TERM_VAL(OVERWRITE, overwrite,
1345 term->val.num ? 0 : 1, term->weak);
1347 case PARSE_EVENTS__TERM_TYPE_DRV_CFG:
1348 ADD_CONFIG_TERM_STR(DRV_CFG, term->val.str, term->weak);
1350 case PARSE_EVENTS__TERM_TYPE_PERCORE:
1351 ADD_CONFIG_TERM_VAL(PERCORE, percore,
1352 term->val.num ? true : false, term->weak);
1354 case PARSE_EVENTS__TERM_TYPE_AUX_OUTPUT:
1355 ADD_CONFIG_TERM_VAL(AUX_OUTPUT, aux_output,
1356 term->val.num ? 1 : 0, term->weak);
1358 case PARSE_EVENTS__TERM_TYPE_AUX_SAMPLE_SIZE:
1359 ADD_CONFIG_TERM_VAL(AUX_SAMPLE_SIZE, aux_sample_size,
1360 term->val.num, term->weak);
1370 * Add EVSEL__CONFIG_TERM_CFG_CHG where cfg_chg will have a bit set for
1371 * each bit of attr->config that the user has changed.
1373 static int get_config_chgs(struct perf_pmu *pmu, struct list_head *head_config,
1374 struct list_head *head_terms)
1376 struct parse_events_term *term;
1380 list_for_each_entry(term, head_config, list) {
1381 switch (term->type_term) {
1382 case PARSE_EVENTS__TERM_TYPE_USER:
1383 type = perf_pmu__format_type(&pmu->format, term->config);
1384 if (type != PERF_PMU_FORMAT_VALUE_CONFIG)
1386 bits |= perf_pmu__format_bits(&pmu->format, term->config);
1388 case PARSE_EVENTS__TERM_TYPE_CONFIG:
1397 ADD_CONFIG_TERM_VAL(CFG_CHG, cfg_chg, bits, false);
1399 #undef ADD_CONFIG_TERM
1403 int parse_events_add_tracepoint(struct list_head *list, int *idx,
1404 const char *sys, const char *event,
1405 struct parse_events_error *err,
1406 struct list_head *head_config)
1409 struct perf_event_attr attr;
1411 if (config_attr(&attr, head_config, err,
1412 config_term_tracepoint))
1416 if (strpbrk(sys, "*?"))
1417 return add_tracepoint_multi_sys(list, idx, sys, event,
1420 return add_tracepoint_event(list, idx, sys, event,
1424 int parse_events_add_numeric(struct parse_events_state *parse_state,
1425 struct list_head *list,
1426 u32 type, u64 config,
1427 struct list_head *head_config)
1429 struct perf_event_attr attr;
1430 LIST_HEAD(config_terms);
1434 memset(&attr, 0, sizeof(attr));
1436 attr.config = config;
1439 if (config_attr(&attr, head_config, parse_state->error,
1440 config_term_common))
1443 if (get_config_terms(head_config, &config_terms))
1447 ret = parse_events__add_numeric_hybrid(parse_state, list, &attr,
1448 get_config_name(head_config),
1449 &config_terms, &hybrid);
1453 return add_event(list, &parse_state->idx, &attr,
1454 get_config_name(head_config), &config_terms);
1457 int parse_events_add_tool(struct parse_events_state *parse_state,
1458 struct list_head *list,
1459 enum perf_tool_event tool_event)
1461 return add_event_tool(list, &parse_state->idx, tool_event);
1464 static bool config_term_percore(struct list_head *config_terms)
1466 struct evsel_config_term *term;
1468 list_for_each_entry(term, config_terms, list) {
1469 if (term->type == EVSEL__CONFIG_TERM_PERCORE)
1470 return term->val.percore;
1476 int parse_events_add_pmu(struct parse_events_state *parse_state,
1477 struct list_head *list, char *name,
1478 struct list_head *head_config,
1479 bool auto_merge_stats,
1482 struct perf_event_attr attr;
1483 struct perf_pmu_info info;
1484 struct perf_pmu *pmu;
1485 struct evsel *evsel;
1486 struct parse_events_error *err = parse_state->error;
1487 bool use_uncore_alias;
1488 LIST_HEAD(config_terms);
1491 fprintf(stderr, "Attempting to add event pmu '%s' with '",
1494 struct parse_events_term *term;
1496 list_for_each_entry(term, head_config, list) {
1497 fprintf(stderr, "%s,", term->config);
1500 fprintf(stderr, "' that may result in non-fatal errors\n");
1503 pmu = parse_state->fake_pmu ?: perf_pmu__find(name);
1507 if (asprintf(&err_str,
1508 "Cannot find PMU `%s'. Missing kernel support?",
1510 parse_events__handle_error(err, 0, err_str, NULL);
1514 if (pmu->default_config) {
1515 memcpy(&attr, pmu->default_config,
1516 sizeof(struct perf_event_attr));
1518 memset(&attr, 0, sizeof(attr));
1521 use_uncore_alias = (pmu->is_uncore && use_alias);
1524 attr.type = pmu->type;
1525 evsel = __add_event(list, &parse_state->idx, &attr, true, NULL,
1526 pmu, NULL, auto_merge_stats, NULL);
1528 evsel->pmu_name = name ? strdup(name) : NULL;
1529 evsel->use_uncore_alias = use_uncore_alias;
1536 if (!parse_state->fake_pmu && perf_pmu__check_alias(pmu, head_config, &info))
1540 fprintf(stderr, "After aliases, add event pmu '%s' with '",
1543 struct parse_events_term *term;
1545 list_for_each_entry(term, head_config, list) {
1546 fprintf(stderr, "%s,", term->config);
1549 fprintf(stderr, "' that may result in non-fatal errors\n");
1553 * Configure hardcoded terms first, no need to check
1554 * return value when called with fail == 0 ;)
1556 if (config_attr(&attr, head_config, parse_state->error, config_term_pmu))
1559 if (get_config_terms(head_config, &config_terms))
1563 * When using default config, record which bits of attr->config were
1564 * changed by the user.
1566 if (pmu->default_config && get_config_chgs(pmu, head_config, &config_terms))
1569 if (!parse_state->fake_pmu && perf_pmu__config(pmu, &attr, head_config, parse_state->error)) {
1570 struct evsel_config_term *pos, *tmp;
1572 list_for_each_entry_safe(pos, tmp, &config_terms, list) {
1573 list_del_init(&pos->list);
1575 zfree(&pos->val.str);
1581 evsel = __add_event(list, &parse_state->idx, &attr, true,
1582 get_config_name(head_config), pmu,
1583 &config_terms, auto_merge_stats, NULL);
1588 evsel->use_config_name = true;
1590 evsel->pmu_name = name ? strdup(name) : NULL;
1591 evsel->use_uncore_alias = use_uncore_alias;
1592 evsel->percore = config_term_percore(&evsel->config_terms);
1594 if (parse_state->fake_pmu)
1597 evsel->unit = info.unit;
1598 evsel->scale = info.scale;
1599 evsel->per_pkg = info.per_pkg;
1600 evsel->snapshot = info.snapshot;
1601 evsel->metric_expr = info.metric_expr;
1602 evsel->metric_name = info.metric_name;
1606 int parse_events_multi_pmu_add(struct parse_events_state *parse_state,
1607 char *str, struct list_head **listp)
1609 struct parse_events_term *term;
1610 struct list_head *list;
1611 struct perf_pmu *pmu = NULL;
1615 /* Add it for all PMUs that support the alias */
1616 list = malloc(sizeof(struct list_head));
1619 INIT_LIST_HEAD(list);
1620 while ((pmu = perf_pmu__scan(pmu)) != NULL) {
1621 struct perf_pmu_alias *alias;
1623 list_for_each_entry(alias, &pmu->aliases, list) {
1624 if (!strcasecmp(alias->name, str)) {
1625 struct list_head *head;
1628 head = malloc(sizeof(struct list_head));
1631 INIT_LIST_HEAD(head);
1632 config = strdup(str);
1635 if (parse_events_term__num(&term,
1636 PARSE_EVENTS__TERM_TYPE_USER,
1637 config, 1, false, &config,
1643 list_add_tail(&term->list, head);
1645 if (!parse_events_add_pmu(parse_state, list,
1648 pr_debug("%s -> %s/%s/\n", str,
1649 pmu->name, alias->str);
1653 parse_events_terms__delete(head);
1665 int parse_events__modifier_group(struct list_head *list,
1668 return parse_events__modifier_event(list, event_mod, true);
1672 * Check if the two uncore PMUs are from the same uncore block
1673 * The format of the uncore PMU name is uncore_#blockname_#pmuidx
1675 static bool is_same_uncore_block(const char *pmu_name_a, const char *pmu_name_b)
1677 char *end_a, *end_b;
1679 end_a = strrchr(pmu_name_a, '_');
1680 end_b = strrchr(pmu_name_b, '_');
1682 if (!end_a || !end_b)
1685 if ((end_a - pmu_name_a) != (end_b - pmu_name_b))
1688 return (strncmp(pmu_name_a, pmu_name_b, end_a - pmu_name_a) == 0);
1692 parse_events__set_leader_for_uncore_aliase(char *name, struct list_head *list,
1693 struct parse_events_state *parse_state)
1695 struct evsel *evsel, *leader;
1697 bool is_leader = true;
1698 int i, nr_pmu = 0, total_members, ret = 0;
1700 leader = list_first_entry(list, struct evsel, core.node);
1701 evsel = list_last_entry(list, struct evsel, core.node);
1702 total_members = evsel->idx - leader->idx + 1;
1704 leaders = calloc(total_members, sizeof(uintptr_t));
1705 if (WARN_ON(!leaders))
1709 * Going through the whole group and doing sanity check.
1710 * All members must use alias, and be from the same uncore block.
1711 * Also, storing the leader events in an array.
1713 __evlist__for_each_entry(list, evsel) {
1715 /* Only split the uncore group which members use alias */
1716 if (!evsel->use_uncore_alias)
1719 /* The events must be from the same uncore block */
1720 if (!is_same_uncore_block(leader->pmu_name, evsel->pmu_name))
1726 * If the event's PMU name starts to repeat, it must be a new
1727 * event. That can be used to distinguish the leader from
1728 * other members, even they have the same event name.
1730 if ((leader != evsel) &&
1731 !strcmp(leader->pmu_name, evsel->pmu_name)) {
1736 /* Store the leader event for each PMU */
1737 leaders[nr_pmu++] = (uintptr_t) evsel;
1740 /* only one event alias */
1741 if (nr_pmu == total_members) {
1742 parse_state->nr_groups--;
1747 * An uncore event alias is a joint name which means the same event
1748 * runs on all PMUs of a block.
1749 * Perf doesn't support mixed events from different PMUs in the same
1750 * group. The big group has to be split into multiple small groups
1751 * which only include the events from the same PMU.
1753 * Here the uncore event aliases must be from the same uncore block.
1754 * The number of PMUs must be same for each alias. The number of new
1755 * small groups equals to the number of PMUs.
1756 * Setting the leader event for corresponding members in each group.
1759 __evlist__for_each_entry(list, evsel) {
1762 evsel->leader = (struct evsel *) leaders[i++];
1765 /* The number of members and group name are same for each group */
1766 for (i = 0; i < nr_pmu; i++) {
1767 evsel = (struct evsel *) leaders[i];
1768 evsel->core.nr_members = total_members / nr_pmu;
1769 evsel->group_name = name ? strdup(name) : NULL;
1772 /* Take the new small groups into account */
1773 parse_state->nr_groups += nr_pmu - 1;
1782 void parse_events__set_leader(char *name, struct list_head *list,
1783 struct parse_events_state *parse_state)
1785 struct evsel *leader;
1787 if (list_empty(list)) {
1788 WARN_ONCE(true, "WARNING: failed to set leader: empty list");
1792 if (parse_events__set_leader_for_uncore_aliase(name, list, parse_state))
1795 __evlist__set_leader(list);
1796 leader = list_entry(list->next, struct evsel, core.node);
1797 leader->group_name = name ? strdup(name) : NULL;
1800 /* list_event is assumed to point to malloc'ed memory */
1801 void parse_events_update_lists(struct list_head *list_event,
1802 struct list_head *list_all)
1805 * Called for single event definition. Update the
1806 * 'all event' list, and reinit the 'single event'
1807 * list, for next event definition.
1809 list_splice_tail(list_event, list_all);
1813 struct event_modifier {
1830 static int get_event_modifier(struct event_modifier *mod, char *str,
1831 struct evsel *evsel)
1833 int eu = evsel ? evsel->core.attr.exclude_user : 0;
1834 int ek = evsel ? evsel->core.attr.exclude_kernel : 0;
1835 int eh = evsel ? evsel->core.attr.exclude_hv : 0;
1836 int eH = evsel ? evsel->core.attr.exclude_host : 0;
1837 int eG = evsel ? evsel->core.attr.exclude_guest : 0;
1838 int eI = evsel ? evsel->core.attr.exclude_idle : 0;
1839 int precise = evsel ? evsel->core.attr.precise_ip : 0;
1840 int precise_max = 0;
1841 int sample_read = 0;
1842 int pinned = evsel ? evsel->core.attr.pinned : 0;
1843 int exclusive = evsel ? evsel->core.attr.exclusive : 0;
1845 int exclude = eu | ek | eh;
1846 int exclude_GH = evsel ? evsel->exclude_GH : 0;
1848 int bpf_counter = 0;
1850 memset(mod, 0, sizeof(*mod));
1855 exclude = eu = ek = eh = 1;
1856 if (!exclude_GH && !perf_guest)
1859 } else if (*str == 'k') {
1861 exclude = eu = ek = eh = 1;
1863 } else if (*str == 'h') {
1865 exclude = eu = ek = eh = 1;
1867 } else if (*str == 'G') {
1869 exclude_GH = eG = eH = 1;
1871 } else if (*str == 'H') {
1873 exclude_GH = eG = eH = 1;
1875 } else if (*str == 'I') {
1877 } else if (*str == 'p') {
1879 /* use of precise requires exclude_guest */
1882 } else if (*str == 'P') {
1884 } else if (*str == 'S') {
1886 } else if (*str == 'D') {
1888 } else if (*str == 'e') {
1890 } else if (*str == 'W') {
1892 } else if (*str == 'b') {
1903 * 0 - SAMPLE_IP can have arbitrary skid
1904 * 1 - SAMPLE_IP must have constant skid
1905 * 2 - SAMPLE_IP requested to have 0 skid
1906 * 3 - SAMPLE_IP must have 0 skid
1908 * See also PERF_RECORD_MISC_EXACT_IP
1919 mod->precise = precise;
1920 mod->precise_max = precise_max;
1921 mod->exclude_GH = exclude_GH;
1922 mod->sample_read = sample_read;
1923 mod->pinned = pinned;
1925 mod->bpf_counter = bpf_counter;
1926 mod->exclusive = exclusive;
1932 * Basic modifier sanity check to validate it contains only one
1933 * instance of any modifier (apart from 'p') present.
1935 static int check_modifier(char *str)
1939 /* The sizeof includes 0 byte as well. */
1940 if (strlen(str) > (sizeof("ukhGHpppPSDIWeb") - 1))
1944 if (*p != 'p' && strchr(p + 1, *p))
1952 int parse_events__modifier_event(struct list_head *list, char *str, bool add)
1954 struct evsel *evsel;
1955 struct event_modifier mod;
1960 if (check_modifier(str))
1963 if (!add && get_event_modifier(&mod, str, NULL))
1966 __evlist__for_each_entry(list, evsel) {
1967 if (add && get_event_modifier(&mod, str, evsel))
1970 evsel->core.attr.exclude_user = mod.eu;
1971 evsel->core.attr.exclude_kernel = mod.ek;
1972 evsel->core.attr.exclude_hv = mod.eh;
1973 evsel->core.attr.precise_ip = mod.precise;
1974 evsel->core.attr.exclude_host = mod.eH;
1975 evsel->core.attr.exclude_guest = mod.eG;
1976 evsel->core.attr.exclude_idle = mod.eI;
1977 evsel->exclude_GH = mod.exclude_GH;
1978 evsel->sample_read = mod.sample_read;
1979 evsel->precise_max = mod.precise_max;
1980 evsel->weak_group = mod.weak;
1981 evsel->bpf_counter = mod.bpf_counter;
1983 if (evsel__is_group_leader(evsel)) {
1984 evsel->core.attr.pinned = mod.pinned;
1985 evsel->core.attr.exclusive = mod.exclusive;
1992 int parse_events_name(struct list_head *list, char *name)
1994 struct evsel *evsel;
1996 __evlist__for_each_entry(list, evsel) {
1998 evsel->name = strdup(name);
2005 comp_pmu(const void *p1, const void *p2)
2007 struct perf_pmu_event_symbol *pmu1 = (struct perf_pmu_event_symbol *) p1;
2008 struct perf_pmu_event_symbol *pmu2 = (struct perf_pmu_event_symbol *) p2;
2010 return strcasecmp(pmu1->symbol, pmu2->symbol);
2013 static void perf_pmu__parse_cleanup(void)
2015 if (perf_pmu_events_list_num > 0) {
2016 struct perf_pmu_event_symbol *p;
2019 for (i = 0; i < perf_pmu_events_list_num; i++) {
2020 p = perf_pmu_events_list + i;
2023 zfree(&perf_pmu_events_list);
2024 perf_pmu_events_list_num = 0;
2028 #define SET_SYMBOL(str, stype) \
2037 * Read the pmu events list from sysfs
2038 * Save it into perf_pmu_events_list
2040 static void perf_pmu__parse_init(void)
2043 struct perf_pmu *pmu = NULL;
2044 struct perf_pmu_alias *alias;
2048 while ((pmu = perf_pmu__scan(pmu)) != NULL) {
2049 list_for_each_entry(alias, &pmu->aliases, list) {
2050 if (strchr(alias->name, '-'))
2057 perf_pmu_events_list_num = -1;
2060 perf_pmu_events_list = malloc(sizeof(struct perf_pmu_event_symbol) * len);
2061 if (!perf_pmu_events_list)
2063 perf_pmu_events_list_num = len;
2067 while ((pmu = perf_pmu__scan(pmu)) != NULL) {
2068 list_for_each_entry(alias, &pmu->aliases, list) {
2069 struct perf_pmu_event_symbol *p = perf_pmu_events_list + len;
2070 char *tmp = strchr(alias->name, '-');
2073 SET_SYMBOL(strndup(alias->name, tmp - alias->name),
2074 PMU_EVENT_SYMBOL_PREFIX);
2076 SET_SYMBOL(strdup(++tmp), PMU_EVENT_SYMBOL_SUFFIX);
2079 SET_SYMBOL(strdup(alias->name), PMU_EVENT_SYMBOL);
2084 qsort(perf_pmu_events_list, len,
2085 sizeof(struct perf_pmu_event_symbol), comp_pmu);
2089 perf_pmu__parse_cleanup();
2093 * This function injects special term in
2094 * perf_pmu_events_list so the test code
2095 * can check on this functionality.
2097 int perf_pmu__test_parse_init(void)
2099 struct perf_pmu_event_symbol *list;
2101 list = malloc(sizeof(*list) * 1);
2105 list->type = PMU_EVENT_SYMBOL;
2106 list->symbol = strdup("read");
2108 if (!list->symbol) {
2113 perf_pmu_events_list = list;
2114 perf_pmu_events_list_num = 1;
2118 enum perf_pmu_event_symbol_type
2119 perf_pmu__parse_check(const char *name)
2121 struct perf_pmu_event_symbol p, *r;
2123 /* scan kernel pmu events from sysfs if needed */
2124 if (perf_pmu_events_list_num == 0)
2125 perf_pmu__parse_init();
2127 * name "cpu" could be prefix of cpu-cycles or cpu// events.
2128 * cpu-cycles has been handled by hardcode.
2129 * So it must be cpu// events, not kernel pmu event.
2131 if ((perf_pmu_events_list_num <= 0) || !strcmp(name, "cpu"))
2132 return PMU_EVENT_SYMBOL_ERR;
2134 p.symbol = strdup(name);
2135 r = bsearch(&p, perf_pmu_events_list,
2136 (size_t) perf_pmu_events_list_num,
2137 sizeof(struct perf_pmu_event_symbol), comp_pmu);
2139 return r ? r->type : PMU_EVENT_SYMBOL_ERR;
2142 static int parse_events__scanner(const char *str,
2143 struct parse_events_state *parse_state)
2145 YY_BUFFER_STATE buffer;
2149 ret = parse_events_lex_init_extra(parse_state, &scanner);
2153 buffer = parse_events__scan_string(str, scanner);
2156 parse_events_debug = 1;
2157 parse_events_set_debug(1, scanner);
2159 ret = parse_events_parse(parse_state, scanner);
2161 parse_events__flush_buffer(buffer, scanner);
2162 parse_events__delete_buffer(buffer, scanner);
2163 parse_events_lex_destroy(scanner);
2168 * parse event config string, return a list of event terms.
2170 int parse_events_terms(struct list_head *terms, const char *str)
2172 struct parse_events_state parse_state = {
2174 .stoken = PE_START_TERMS,
2178 ret = parse_events__scanner(str, &parse_state);
2179 perf_pmu__parse_cleanup();
2182 list_splice(parse_state.terms, terms);
2183 zfree(&parse_state.terms);
2187 parse_events_terms__delete(parse_state.terms);
2191 int __parse_events(struct evlist *evlist, const char *str,
2192 struct parse_events_error *err, struct perf_pmu *fake_pmu)
2194 struct parse_events_state parse_state = {
2195 .list = LIST_HEAD_INIT(parse_state.list),
2196 .idx = evlist->core.nr_entries,
2199 .stoken = PE_START_EVENTS,
2200 .fake_pmu = fake_pmu,
2204 ret = parse_events__scanner(str, &parse_state);
2205 perf_pmu__parse_cleanup();
2207 if (!ret && list_empty(&parse_state.list)) {
2208 WARN_ONCE(true, "WARNING: event parser found nothing\n");
2213 * Add list to the evlist even with errors to allow callers to clean up.
2215 evlist__splice_list_tail(evlist, &parse_state.list);
2220 evlist->nr_groups += parse_state.nr_groups;
2221 last = evlist__last(evlist);
2222 last->cmdline_group_boundary = true;
2228 * There are 2 users - builtin-record and builtin-test objects.
2229 * Both call evlist__delete in case of error, so we dont
2235 #define MAX_WIDTH 1000
2236 static int get_term_width(void)
2240 get_term_dimensions(&ws);
2241 return ws.ws_col > MAX_WIDTH ? MAX_WIDTH : ws.ws_col;
2244 static void __parse_events_print_error(int err_idx, const char *err_str,
2245 const char *err_help, const char *event)
2247 const char *str = "invalid or unsupported event: ";
2248 char _buf[MAX_WIDTH];
2249 char *buf = (char *) event;
2252 /* -2 for extra '' in the final fprintf */
2253 int width = get_term_width() - 2;
2254 int len_event = strlen(event);
2255 int len_str, max_len, cut = 0;
2258 * Maximum error index indent, we will cut
2259 * the event string if it's bigger.
2261 int max_err_idx = 13;
2264 * Let's be specific with the message when
2265 * we have the precise error.
2267 str = "event syntax error: ";
2268 len_str = strlen(str);
2269 max_len = width - len_str;
2273 /* We're cutting from the beginning. */
2274 if (err_idx > max_err_idx)
2275 cut = err_idx - max_err_idx;
2277 strncpy(buf, event + cut, max_len);
2279 /* Mark cut parts with '..' on both sides. */
2281 buf[0] = buf[1] = '.';
2283 if ((len_event - cut) > max_len) {
2284 buf[max_len - 1] = buf[max_len - 2] = '.';
2288 idx = len_str + err_idx - cut;
2291 fprintf(stderr, "%s'%s'\n", str, buf);
2293 fprintf(stderr, "%*s\\___ %s\n", idx + 1, "", err_str);
2295 fprintf(stderr, "\n%s\n", err_help);
2299 void parse_events_print_error(struct parse_events_error *err,
2302 if (!err->num_errors)
2305 __parse_events_print_error(err->idx, err->str, err->help, event);
2309 if (err->num_errors > 1) {
2310 fputs("\nInitial error:\n", stderr);
2311 __parse_events_print_error(err->first_idx, err->first_str,
2312 err->first_help, event);
2313 zfree(&err->first_str);
2314 zfree(&err->first_help);
2320 int parse_events_option(const struct option *opt, const char *str,
2321 int unset __maybe_unused)
2323 struct evlist *evlist = *(struct evlist **)opt->value;
2324 struct parse_events_error err;
2327 bzero(&err, sizeof(err));
2328 ret = parse_events(evlist, str, &err);
2331 parse_events_print_error(&err, str);
2332 fprintf(stderr, "Run 'perf list' for a list of valid events\n");
2338 int parse_events_option_new_evlist(const struct option *opt, const char *str, int unset)
2340 struct evlist **evlistp = opt->value;
2343 if (*evlistp == NULL) {
2344 *evlistp = evlist__new();
2346 if (*evlistp == NULL) {
2347 fprintf(stderr, "Not enough memory to create evlist\n");
2352 ret = parse_events_option(opt, str, unset);
2354 evlist__delete(*evlistp);
2362 foreach_evsel_in_last_glob(struct evlist *evlist,
2363 int (*func)(struct evsel *evsel,
2367 struct evsel *last = NULL;
2371 * Don't return when list_empty, give func a chance to report
2372 * error when it found last == NULL.
2374 * So no need to WARN here, let *func do this.
2376 if (evlist->core.nr_entries > 0)
2377 last = evlist__last(evlist);
2380 err = (*func)(last, arg);
2386 if (last->core.node.prev == &evlist->core.entries)
2388 last = list_entry(last->core.node.prev, struct evsel, core.node);
2389 } while (!last->cmdline_group_boundary);
2394 static int set_filter(struct evsel *evsel, const void *arg)
2396 const char *str = arg;
2398 int nr_addr_filters = 0;
2399 struct perf_pmu *pmu = NULL;
2401 if (evsel == NULL) {
2403 "--filter option should follow a -e tracepoint or HW tracer option\n");
2407 if (evsel->core.attr.type == PERF_TYPE_TRACEPOINT) {
2408 if (evsel__append_tp_filter(evsel, str) < 0) {
2410 "not enough memory to hold filter string\n");
2417 while ((pmu = perf_pmu__scan(pmu)) != NULL)
2418 if (pmu->type == evsel->core.attr.type) {
2424 perf_pmu__scan_file(pmu, "nr_addr_filters",
2425 "%d", &nr_addr_filters);
2427 if (!nr_addr_filters) {
2429 "This CPU does not support address filtering\n");
2433 if (evsel__append_addr_filter(evsel, str) < 0) {
2435 "not enough memory to hold filter string\n");
2442 int parse_filter(const struct option *opt, const char *str,
2443 int unset __maybe_unused)
2445 struct evlist *evlist = *(struct evlist **)opt->value;
2447 return foreach_evsel_in_last_glob(evlist, set_filter,
2451 static int add_exclude_perf_filter(struct evsel *evsel,
2452 const void *arg __maybe_unused)
2454 char new_filter[64];
2456 if (evsel == NULL || evsel->core.attr.type != PERF_TYPE_TRACEPOINT) {
2458 "--exclude-perf option should follow a -e tracepoint option\n");
2462 snprintf(new_filter, sizeof(new_filter), "common_pid != %d", getpid());
2464 if (evsel__append_tp_filter(evsel, new_filter) < 0) {
2466 "not enough memory to hold filter string\n");
2473 int exclude_perf(const struct option *opt,
2474 const char *arg __maybe_unused,
2475 int unset __maybe_unused)
2477 struct evlist *evlist = *(struct evlist **)opt->value;
2479 return foreach_evsel_in_last_glob(evlist, add_exclude_perf_filter,
2483 static const char * const event_type_descriptors[] = {
2487 "Hardware cache event",
2488 "Raw hardware event descriptor",
2489 "Hardware breakpoint",
2492 static int cmp_string(const void *a, const void *b)
2494 const char * const *as = a;
2495 const char * const *bs = b;
2497 return strcmp(*as, *bs);
2501 * Print the events from <debugfs_mount_point>/tracing/events
2504 void print_tracepoint_events(const char *subsys_glob, const char *event_glob,
2507 DIR *sys_dir, *evt_dir;
2508 struct dirent *sys_dirent, *evt_dirent;
2509 char evt_path[MAXPATHLEN];
2511 char **evt_list = NULL;
2512 unsigned int evt_i = 0, evt_num = 0;
2513 bool evt_num_known = false;
2516 sys_dir = tracing_events__opendir();
2520 if (evt_num_known) {
2521 evt_list = zalloc(sizeof(char *) * evt_num);
2523 goto out_close_sys_dir;
2526 for_each_subsystem(sys_dir, sys_dirent) {
2527 if (subsys_glob != NULL &&
2528 !strglobmatch(sys_dirent->d_name, subsys_glob))
2531 dir_path = get_events_file(sys_dirent->d_name);
2534 evt_dir = opendir(dir_path);
2538 for_each_event(dir_path, evt_dir, evt_dirent) {
2539 if (event_glob != NULL &&
2540 !strglobmatch(evt_dirent->d_name, event_glob))
2543 if (!evt_num_known) {
2548 snprintf(evt_path, MAXPATHLEN, "%s:%s",
2549 sys_dirent->d_name, evt_dirent->d_name);
2551 evt_list[evt_i] = strdup(evt_path);
2552 if (evt_list[evt_i] == NULL) {
2553 put_events_file(dir_path);
2554 goto out_close_evt_dir;
2560 put_events_file(dir_path);
2564 if (!evt_num_known) {
2565 evt_num_known = true;
2568 qsort(evt_list, evt_num, sizeof(char *), cmp_string);
2570 while (evt_i < evt_num) {
2572 printf("%s ", evt_list[evt_i++]);
2575 printf(" %-50s [%s]\n", evt_list[evt_i++],
2576 event_type_descriptors[PERF_TYPE_TRACEPOINT]);
2578 if (evt_num && pager_in_use())
2583 for (evt_i = 0; evt_i < evt_num; evt_i++)
2584 zfree(&evt_list[evt_i]);
2593 printf("FATAL: not enough memory to print %s\n",
2594 event_type_descriptors[PERF_TYPE_TRACEPOINT]);
2600 * Check whether event is in <debugfs_mount_point>/tracing/events
2603 int is_valid_tracepoint(const char *event_string)
2605 DIR *sys_dir, *evt_dir;
2606 struct dirent *sys_dirent, *evt_dirent;
2607 char evt_path[MAXPATHLEN];
2610 sys_dir = tracing_events__opendir();
2614 for_each_subsystem(sys_dir, sys_dirent) {
2615 dir_path = get_events_file(sys_dirent->d_name);
2618 evt_dir = opendir(dir_path);
2622 for_each_event(dir_path, evt_dir, evt_dirent) {
2623 snprintf(evt_path, MAXPATHLEN, "%s:%s",
2624 sys_dirent->d_name, evt_dirent->d_name);
2625 if (!strcmp(evt_path, event_string)) {
2633 put_events_file(dir_path);
2639 static bool is_event_supported(u8 type, unsigned config)
2643 struct evsel *evsel;
2644 struct perf_event_attr attr = {
2649 struct perf_thread_map *tmap = thread_map__new_by_tid(0);
2654 evsel = evsel__new(&attr);
2656 open_return = evsel__open(evsel, NULL, tmap);
2657 ret = open_return >= 0;
2659 if (open_return == -EACCES) {
2661 * This happens if the paranoid value
2662 * /proc/sys/kernel/perf_event_paranoid is set to 2
2663 * Re-run with exclude_kernel set; we don't do that
2664 * by default as some ARM machines do not support it.
2667 evsel->core.attr.exclude_kernel = 1;
2668 ret = evsel__open(evsel, NULL, tmap) >= 0;
2670 evsel__delete(evsel);
2673 perf_thread_map__put(tmap);
2677 void print_sdt_events(const char *subsys_glob, const char *event_glob,
2680 struct probe_cache *pcache;
2681 struct probe_cache_entry *ent;
2682 struct strlist *bidlist, *sdtlist;
2683 struct strlist_config cfg = {.dont_dupstr = true};
2684 struct str_node *nd, *nd2;
2685 char *buf, *path, *ptr = NULL;
2686 bool show_detail = false;
2689 sdtlist = strlist__new(NULL, &cfg);
2691 pr_debug("Failed to allocate new strlist for SDT\n");
2694 bidlist = build_id_cache__list_all(true);
2696 pr_debug("Failed to get buildids: %d\n", errno);
2699 strlist__for_each_entry(nd, bidlist) {
2700 pcache = probe_cache__new(nd->s, NULL);
2703 list_for_each_entry(ent, &pcache->entries, node) {
2707 !strglobmatch(ent->pev.group, subsys_glob))
2710 !strglobmatch(ent->pev.event, event_glob))
2712 ret = asprintf(&buf, "%s:%s@%s", ent->pev.group,
2713 ent->pev.event, nd->s);
2715 strlist__add(sdtlist, buf);
2717 probe_cache__delete(pcache);
2719 strlist__delete(bidlist);
2721 strlist__for_each_entry(nd, sdtlist) {
2722 buf = strchr(nd->s, '@');
2726 printf("%s ", nd->s);
2729 nd2 = strlist__next(nd);
2731 ptr = strchr(nd2->s, '@');
2734 if (strcmp(nd->s, nd2->s) == 0)
2738 path = build_id_cache__origname(buf);
2739 ret = asprintf(&buf, "%s@%s(%.12s)", nd->s, path, buf);
2741 printf(" %-50s [%s]\n", buf, "SDT event");
2746 printf(" %-50s [%s]\n", nd->s, "SDT event");
2748 if (strcmp(nd->s, nd2->s) != 0)
2749 show_detail = false;
2754 strlist__delete(sdtlist);
2757 int print_hwcache_events(const char *event_glob, bool name_only)
2759 unsigned int type, op, i, evt_i = 0, evt_num = 0;
2761 char **evt_list = NULL;
2762 bool evt_num_known = false;
2765 if (evt_num_known) {
2766 evt_list = zalloc(sizeof(char *) * evt_num);
2771 for (type = 0; type < PERF_COUNT_HW_CACHE_MAX; type++) {
2772 for (op = 0; op < PERF_COUNT_HW_CACHE_OP_MAX; op++) {
2773 /* skip invalid cache type */
2774 if (!evsel__is_cache_op_valid(type, op))
2777 for (i = 0; i < PERF_COUNT_HW_CACHE_RESULT_MAX; i++) {
2778 __evsel__hw_cache_type_op_res_name(type, op, i, name, sizeof(name));
2779 if (event_glob != NULL && !strglobmatch(name, event_glob))
2782 if (!is_event_supported(PERF_TYPE_HW_CACHE,
2783 type | (op << 8) | (i << 16)))
2786 if (!evt_num_known) {
2791 evt_list[evt_i] = strdup(name);
2792 if (evt_list[evt_i] == NULL)
2799 if (!evt_num_known) {
2800 evt_num_known = true;
2803 qsort(evt_list, evt_num, sizeof(char *), cmp_string);
2805 while (evt_i < evt_num) {
2807 printf("%s ", evt_list[evt_i++]);
2810 printf(" %-50s [%s]\n", evt_list[evt_i++],
2811 event_type_descriptors[PERF_TYPE_HW_CACHE]);
2813 if (evt_num && pager_in_use())
2818 for (evt_i = 0; evt_i < evt_num; evt_i++)
2819 zfree(&evt_list[evt_i]);
2824 printf("FATAL: not enough memory to print %s\n", event_type_descriptors[PERF_TYPE_HW_CACHE]);
2830 static void print_tool_event(const char *name, const char *event_glob,
2833 if (event_glob && !strglobmatch(name, event_glob))
2836 printf("%s ", name);
2838 printf(" %-50s [%s]\n", name, "Tool event");
2842 void print_tool_events(const char *event_glob, bool name_only)
2844 print_tool_event("duration_time", event_glob, name_only);
2849 void print_symbol_events(const char *event_glob, unsigned type,
2850 struct event_symbol *syms, unsigned max,
2853 unsigned int i, evt_i = 0, evt_num = 0;
2854 char name[MAX_NAME_LEN];
2855 char **evt_list = NULL;
2856 bool evt_num_known = false;
2859 if (evt_num_known) {
2860 evt_list = zalloc(sizeof(char *) * evt_num);
2866 for (i = 0; i < max; i++, syms++) {
2868 if (event_glob != NULL && syms->symbol != NULL &&
2869 !(strglobmatch(syms->symbol, event_glob) ||
2870 (syms->alias && strglobmatch(syms->alias, event_glob))))
2873 if (!is_event_supported(type, i))
2876 if (!evt_num_known) {
2881 if (!name_only && strlen(syms->alias))
2882 snprintf(name, MAX_NAME_LEN, "%s OR %s", syms->symbol, syms->alias);
2884 strlcpy(name, syms->symbol, MAX_NAME_LEN);
2886 evt_list[evt_i] = strdup(name);
2887 if (evt_list[evt_i] == NULL)
2892 if (!evt_num_known) {
2893 evt_num_known = true;
2896 qsort(evt_list, evt_num, sizeof(char *), cmp_string);
2898 while (evt_i < evt_num) {
2900 printf("%s ", evt_list[evt_i++]);
2903 printf(" %-50s [%s]\n", evt_list[evt_i++], event_type_descriptors[type]);
2905 if (evt_num && pager_in_use())
2910 for (evt_i = 0; evt_i < evt_num; evt_i++)
2911 zfree(&evt_list[evt_i]);
2916 printf("FATAL: not enough memory to print %s\n", event_type_descriptors[type]);
2922 * Print the help text for the event symbols:
2924 void print_events(const char *event_glob, bool name_only, bool quiet_flag,
2925 bool long_desc, bool details_flag, bool deprecated)
2927 print_symbol_events(event_glob, PERF_TYPE_HARDWARE,
2928 event_symbols_hw, PERF_COUNT_HW_MAX, name_only);
2930 print_symbol_events(event_glob, PERF_TYPE_SOFTWARE,
2931 event_symbols_sw, PERF_COUNT_SW_MAX, name_only);
2932 print_tool_events(event_glob, name_only);
2934 print_hwcache_events(event_glob, name_only);
2936 print_pmu_events(event_glob, name_only, quiet_flag, long_desc,
2937 details_flag, deprecated);
2939 if (event_glob != NULL)
2943 printf(" %-50s [%s]\n",
2945 event_type_descriptors[PERF_TYPE_RAW]);
2946 printf(" %-50s [%s]\n",
2947 "cpu/t1=v1[,t2=v2,t3 ...]/modifier",
2948 event_type_descriptors[PERF_TYPE_RAW]);
2950 printf(" (see 'man perf-list' on how to encode it)\n\n");
2952 printf(" %-50s [%s]\n",
2953 "mem:<addr>[/len][:access]",
2954 event_type_descriptors[PERF_TYPE_BREAKPOINT]);
2959 print_tracepoint_events(NULL, NULL, name_only);
2961 print_sdt_events(NULL, NULL, name_only);
2963 metricgroup__print(true, true, NULL, name_only, details_flag);
2965 print_libpfm_events(name_only, long_desc);
2968 int parse_events__is_hardcoded_term(struct parse_events_term *term)
2970 return term->type_term != PARSE_EVENTS__TERM_TYPE_USER;
2973 static int new_term(struct parse_events_term **_term,
2974 struct parse_events_term *temp,
2977 struct parse_events_term *term;
2979 term = malloc(sizeof(*term));
2984 INIT_LIST_HEAD(&term->list);
2987 switch (term->type_val) {
2988 case PARSE_EVENTS__TERM_TYPE_NUM:
2989 term->val.num = num;
2991 case PARSE_EVENTS__TERM_TYPE_STR:
2992 term->val.str = str;
3003 int parse_events_term__num(struct parse_events_term **term,
3004 int type_term, char *config, u64 num,
3006 void *loc_term_, void *loc_val_)
3008 YYLTYPE *loc_term = loc_term_;
3009 YYLTYPE *loc_val = loc_val_;
3011 struct parse_events_term temp = {
3012 .type_val = PARSE_EVENTS__TERM_TYPE_NUM,
3013 .type_term = type_term,
3015 .no_value = no_value,
3016 .err_term = loc_term ? loc_term->first_column : 0,
3017 .err_val = loc_val ? loc_val->first_column : 0,
3020 return new_term(term, &temp, NULL, num);
3023 int parse_events_term__str(struct parse_events_term **term,
3024 int type_term, char *config, char *str,
3025 void *loc_term_, void *loc_val_)
3027 YYLTYPE *loc_term = loc_term_;
3028 YYLTYPE *loc_val = loc_val_;
3030 struct parse_events_term temp = {
3031 .type_val = PARSE_EVENTS__TERM_TYPE_STR,
3032 .type_term = type_term,
3034 .err_term = loc_term ? loc_term->first_column : 0,
3035 .err_val = loc_val ? loc_val->first_column : 0,
3038 return new_term(term, &temp, str, 0);
3041 int parse_events_term__sym_hw(struct parse_events_term **term,
3042 char *config, unsigned idx)
3044 struct event_symbol *sym;
3046 struct parse_events_term temp = {
3047 .type_val = PARSE_EVENTS__TERM_TYPE_STR,
3048 .type_term = PARSE_EVENTS__TERM_TYPE_USER,
3053 temp.config = strdup("event");
3057 BUG_ON(idx >= PERF_COUNT_HW_MAX);
3058 sym = &event_symbols_hw[idx];
3060 str = strdup(sym->symbol);
3063 return new_term(term, &temp, str, 0);
3066 int parse_events_term__clone(struct parse_events_term **new,
3067 struct parse_events_term *term)
3070 struct parse_events_term temp = {
3071 .type_val = term->type_val,
3072 .type_term = term->type_term,
3074 .err_term = term->err_term,
3075 .err_val = term->err_val,
3079 temp.config = strdup(term->config);
3083 if (term->type_val == PARSE_EVENTS__TERM_TYPE_NUM)
3084 return new_term(new, &temp, NULL, term->val.num);
3086 str = strdup(term->val.str);
3089 return new_term(new, &temp, str, 0);
3092 void parse_events_term__delete(struct parse_events_term *term)
3094 if (term->array.nr_ranges)
3095 zfree(&term->array.ranges);
3097 if (term->type_val != PARSE_EVENTS__TERM_TYPE_NUM)
3098 zfree(&term->val.str);
3100 zfree(&term->config);
3104 int parse_events_copy_term_list(struct list_head *old,
3105 struct list_head **new)
3107 struct parse_events_term *term, *n;
3115 *new = malloc(sizeof(struct list_head));
3118 INIT_LIST_HEAD(*new);
3120 list_for_each_entry (term, old, list) {
3121 ret = parse_events_term__clone(&n, term);
3124 list_add_tail(&n->list, *new);
3129 void parse_events_terms__purge(struct list_head *terms)
3131 struct parse_events_term *term, *h;
3133 list_for_each_entry_safe(term, h, terms, list) {
3134 list_del_init(&term->list);
3135 parse_events_term__delete(term);
3139 void parse_events_terms__delete(struct list_head *terms)
3143 parse_events_terms__purge(terms);
3147 void parse_events__clear_array(struct parse_events_array *a)
3152 void parse_events_evlist_error(struct parse_events_state *parse_state,
3153 int idx, const char *str)
3155 if (!parse_state->error)
3158 parse_events__handle_error(parse_state->error, idx, strdup(str), NULL);
3161 static void config_terms_list(char *buf, size_t buf_sz)
3167 for (i = 0; i < __PARSE_EVENTS__TERM_TYPE_NR; i++) {
3168 const char *name = config_term_names[i];
3170 if (!config_term_avail(i, NULL))
3177 if (strlen(buf) + strlen(name) + 2 >= buf_sz)
3189 * Return string contains valid config terms of an event.
3190 * @additional_terms: For terms such as PMU sysfs terms.
3192 char *parse_events_formats_error_string(char *additional_terms)
3195 /* "no-overwrite" is the longest name */
3196 char static_terms[__PARSE_EVENTS__TERM_TYPE_NR *
3197 (sizeof("no-overwrite") - 1)];
3199 config_terms_list(static_terms, sizeof(static_terms));
3201 if (additional_terms) {
3202 if (asprintf(&str, "valid terms: %s,%s",
3203 additional_terms, static_terms) < 0)
3206 if (asprintf(&str, "valid terms: %s", static_terms) < 0)
3215 struct evsel *parse_events__add_event_hybrid(struct list_head *list, int *idx,
3216 struct perf_event_attr *attr,
3217 char *name, struct perf_pmu *pmu,
3218 struct list_head *config_terms)
3220 return __add_event(list, idx, attr, true, name, pmu,
3221 config_terms, false, NULL);