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"
42 #define MAX_NAME_LEN 100
45 extern int parse_events_debug
;
47 int parse_events_parse(void *parse_state
, void *scanner
);
48 static int get_config_terms(struct list_head
*head_config
,
49 struct list_head
*head_terms __maybe_unused
);
51 static struct perf_pmu_event_symbol
*perf_pmu_events_list
;
53 * The variable indicates the number of supported pmu event symbols.
54 * 0 means not initialized and ready to init
55 * -1 means failed to init, don't try anymore
56 * >0 is the number of supported pmu event symbols
58 static int perf_pmu_events_list_num
;
60 struct event_symbol event_symbols_hw
[PERF_COUNT_HW_MAX
] = {
61 [PERF_COUNT_HW_CPU_CYCLES
] = {
62 .symbol
= "cpu-cycles",
65 [PERF_COUNT_HW_INSTRUCTIONS
] = {
66 .symbol
= "instructions",
69 [PERF_COUNT_HW_CACHE_REFERENCES
] = {
70 .symbol
= "cache-references",
73 [PERF_COUNT_HW_CACHE_MISSES
] = {
74 .symbol
= "cache-misses",
77 [PERF_COUNT_HW_BRANCH_INSTRUCTIONS
] = {
78 .symbol
= "branch-instructions",
81 [PERF_COUNT_HW_BRANCH_MISSES
] = {
82 .symbol
= "branch-misses",
85 [PERF_COUNT_HW_BUS_CYCLES
] = {
86 .symbol
= "bus-cycles",
89 [PERF_COUNT_HW_STALLED_CYCLES_FRONTEND
] = {
90 .symbol
= "stalled-cycles-frontend",
91 .alias
= "idle-cycles-frontend",
93 [PERF_COUNT_HW_STALLED_CYCLES_BACKEND
] = {
94 .symbol
= "stalled-cycles-backend",
95 .alias
= "idle-cycles-backend",
97 [PERF_COUNT_HW_REF_CPU_CYCLES
] = {
98 .symbol
= "ref-cycles",
103 struct event_symbol event_symbols_sw
[PERF_COUNT_SW_MAX
] = {
104 [PERF_COUNT_SW_CPU_CLOCK
] = {
105 .symbol
= "cpu-clock",
108 [PERF_COUNT_SW_TASK_CLOCK
] = {
109 .symbol
= "task-clock",
112 [PERF_COUNT_SW_PAGE_FAULTS
] = {
113 .symbol
= "page-faults",
116 [PERF_COUNT_SW_CONTEXT_SWITCHES
] = {
117 .symbol
= "context-switches",
120 [PERF_COUNT_SW_CPU_MIGRATIONS
] = {
121 .symbol
= "cpu-migrations",
122 .alias
= "migrations",
124 [PERF_COUNT_SW_PAGE_FAULTS_MIN
] = {
125 .symbol
= "minor-faults",
128 [PERF_COUNT_SW_PAGE_FAULTS_MAJ
] = {
129 .symbol
= "major-faults",
132 [PERF_COUNT_SW_ALIGNMENT_FAULTS
] = {
133 .symbol
= "alignment-faults",
136 [PERF_COUNT_SW_EMULATION_FAULTS
] = {
137 .symbol
= "emulation-faults",
140 [PERF_COUNT_SW_DUMMY
] = {
144 [PERF_COUNT_SW_BPF_OUTPUT
] = {
145 .symbol
= "bpf-output",
150 #define __PERF_EVENT_FIELD(config, name) \
151 ((config & PERF_EVENT_##name##_MASK) >> PERF_EVENT_##name##_SHIFT)
153 #define PERF_EVENT_RAW(config) __PERF_EVENT_FIELD(config, RAW)
154 #define PERF_EVENT_CONFIG(config) __PERF_EVENT_FIELD(config, CONFIG)
155 #define PERF_EVENT_TYPE(config) __PERF_EVENT_FIELD(config, TYPE)
156 #define PERF_EVENT_ID(config) __PERF_EVENT_FIELD(config, EVENT)
158 #define for_each_subsystem(sys_dir, sys_dirent) \
159 while ((sys_dirent = readdir(sys_dir)) != NULL) \
160 if (sys_dirent->d_type == DT_DIR && \
161 (strcmp(sys_dirent->d_name, ".")) && \
162 (strcmp(sys_dirent->d_name, "..")))
164 static int tp_event_has_id(const char *dir_path
, struct dirent
*evt_dir
)
166 char evt_path
[MAXPATHLEN
];
169 snprintf(evt_path
, MAXPATHLEN
, "%s/%s/id", dir_path
, evt_dir
->d_name
);
170 fd
= open(evt_path
, O_RDONLY
);
178 #define for_each_event(dir_path, evt_dir, evt_dirent) \
179 while ((evt_dirent = readdir(evt_dir)) != NULL) \
180 if (evt_dirent->d_type == DT_DIR && \
181 (strcmp(evt_dirent->d_name, ".")) && \
182 (strcmp(evt_dirent->d_name, "..")) && \
183 (!tp_event_has_id(dir_path, evt_dirent)))
185 #define MAX_EVENT_LENGTH 512
187 void parse_events__handle_error(struct parse_events_error
*err
, int idx
,
188 char *str
, char *help
)
190 if (WARN(!str
, "WARNING: failed to provide error string\n")) {
194 switch (err
->num_errors
) {
201 err
->first_idx
= err
->idx
;
203 err
->first_str
= err
->str
;
205 err
->first_help
= err
->help
;
209 pr_debug("Multiple errors dropping message: %s (%s)\n",
210 err
->str
, err
->help
);
220 struct tracepoint_path
*tracepoint_id_to_path(u64 config
)
222 struct tracepoint_path
*path
= NULL
;
223 DIR *sys_dir
, *evt_dir
;
224 struct dirent
*sys_dirent
, *evt_dirent
;
228 char evt_path
[MAXPATHLEN
];
231 sys_dir
= tracing_events__opendir();
235 for_each_subsystem(sys_dir
, sys_dirent
) {
236 dir_path
= get_events_file(sys_dirent
->d_name
);
239 evt_dir
= opendir(dir_path
);
243 for_each_event(dir_path
, evt_dir
, evt_dirent
) {
245 scnprintf(evt_path
, MAXPATHLEN
, "%s/%s/id", dir_path
,
247 fd
= open(evt_path
, O_RDONLY
);
250 if (read(fd
, id_buf
, sizeof(id_buf
)) < 0) {
257 put_events_file(dir_path
);
260 path
= zalloc(sizeof(*path
));
263 if (asprintf(&path
->system
, "%.*s", MAX_EVENT_LENGTH
, sys_dirent
->d_name
) < 0) {
267 if (asprintf(&path
->name
, "%.*s", MAX_EVENT_LENGTH
, evt_dirent
->d_name
) < 0) {
268 zfree(&path
->system
);
277 put_events_file(dir_path
);
284 struct tracepoint_path
*tracepoint_name_to_path(const char *name
)
286 struct tracepoint_path
*path
= zalloc(sizeof(*path
));
287 char *str
= strchr(name
, ':');
289 if (path
== NULL
|| str
== NULL
) {
294 path
->system
= strndup(name
, str
- name
);
295 path
->name
= strdup(str
+1);
297 if (path
->system
== NULL
|| path
->name
== NULL
) {
298 zfree(&path
->system
);
306 const char *event_type(int type
)
309 case PERF_TYPE_HARDWARE
:
312 case PERF_TYPE_SOFTWARE
:
315 case PERF_TYPE_TRACEPOINT
:
318 case PERF_TYPE_HW_CACHE
:
319 return "hardware-cache";
328 static int parse_events__is_name_term(struct parse_events_term
*term
)
330 return term
->type_term
== PARSE_EVENTS__TERM_TYPE_NAME
;
333 static char *get_config_name(struct list_head
*head_terms
)
335 struct parse_events_term
*term
;
340 list_for_each_entry(term
, head_terms
, list
)
341 if (parse_events__is_name_term(term
))
342 return term
->val
.str
;
347 static struct evsel
*
348 __add_event(struct list_head
*list
, int *idx
,
349 struct perf_event_attr
*attr
,
351 char *name
, struct perf_pmu
*pmu
,
352 struct list_head
*config_terms
, bool auto_merge_stats
,
353 const char *cpu_list
)
356 struct perf_cpu_map
*cpus
= pmu
? perf_cpu_map__get(pmu
->cpus
) :
357 cpu_list
? perf_cpu_map__new(cpu_list
) : NULL
;
360 event_attr_init(attr
);
362 evsel
= evsel__new_idx(attr
, *idx
);
364 perf_cpu_map__put(cpus
);
369 evsel
->core
.cpus
= cpus
;
370 evsel
->core
.own_cpus
= perf_cpu_map__get(cpus
);
371 evsel
->core
.system_wide
= pmu
? pmu
->is_uncore
: false;
372 evsel
->auto_merge_stats
= auto_merge_stats
;
375 evsel
->name
= strdup(name
);
378 list_splice(config_terms
, &evsel
->config_terms
);
381 list_add_tail(&evsel
->core
.node
, list
);
386 struct evsel
*parse_events__add_event(int idx
, struct perf_event_attr
*attr
,
387 char *name
, struct perf_pmu
*pmu
)
389 return __add_event(NULL
, &idx
, attr
, false, name
, pmu
, NULL
, false,
393 static int add_event(struct list_head
*list
, int *idx
,
394 struct perf_event_attr
*attr
, char *name
,
395 struct list_head
*config_terms
)
397 return __add_event(list
, idx
, attr
, true, name
, NULL
, config_terms
,
398 false, NULL
) ? 0 : -ENOMEM
;
401 static int add_event_tool(struct list_head
*list
, int *idx
,
402 enum perf_tool_event tool_event
)
405 struct perf_event_attr attr
= {
406 .type
= PERF_TYPE_SOFTWARE
,
407 .config
= PERF_COUNT_SW_DUMMY
,
410 evsel
= __add_event(list
, idx
, &attr
, true, NULL
, NULL
, NULL
, false,
414 evsel
->tool_event
= tool_event
;
415 if (tool_event
== PERF_TOOL_DURATION_TIME
)
420 static int parse_aliases(char *str
, const char *names
[][EVSEL__MAX_ALIASES
], int size
)
425 for (i
= 0; i
< size
; i
++) {
426 for (j
= 0; j
< EVSEL__MAX_ALIASES
&& names
[i
][j
]; j
++) {
427 n
= strlen(names
[i
][j
]);
428 if (n
> longest
&& !strncasecmp(str
, names
[i
][j
], n
))
438 typedef int config_term_func_t(struct perf_event_attr
*attr
,
439 struct parse_events_term
*term
,
440 struct parse_events_error
*err
);
441 static int config_term_common(struct perf_event_attr
*attr
,
442 struct parse_events_term
*term
,
443 struct parse_events_error
*err
);
444 static int config_attr(struct perf_event_attr
*attr
,
445 struct list_head
*head
,
446 struct parse_events_error
*err
,
447 config_term_func_t config_term
);
449 int parse_events_add_cache(struct list_head
*list
, int *idx
,
450 char *type
, char *op_result1
, char *op_result2
,
451 struct parse_events_error
*err
,
452 struct list_head
*head_config
)
454 struct perf_event_attr attr
;
455 LIST_HEAD(config_terms
);
456 char name
[MAX_NAME_LEN
], *config_name
;
457 int cache_type
= -1, cache_op
= -1, cache_result
= -1;
458 char *op_result
[2] = { op_result1
, op_result2
};
462 * No fallback - if we cannot get a clear cache type
465 cache_type
= parse_aliases(type
, evsel__hw_cache
, PERF_COUNT_HW_CACHE_MAX
);
466 if (cache_type
== -1)
469 config_name
= get_config_name(head_config
);
470 n
= snprintf(name
, MAX_NAME_LEN
, "%s", type
);
472 for (i
= 0; (i
< 2) && (op_result
[i
]); i
++) {
473 char *str
= op_result
[i
];
475 n
+= snprintf(name
+ n
, MAX_NAME_LEN
- n
, "-%s", str
);
477 if (cache_op
== -1) {
478 cache_op
= parse_aliases(str
, evsel__hw_cache_op
,
479 PERF_COUNT_HW_CACHE_OP_MAX
);
481 if (!evsel__is_cache_op_valid(cache_type
, cache_op
))
487 if (cache_result
== -1) {
488 cache_result
= parse_aliases(str
, evsel__hw_cache_result
,
489 PERF_COUNT_HW_CACHE_RESULT_MAX
);
490 if (cache_result
>= 0)
496 * Fall back to reads:
499 cache_op
= PERF_COUNT_HW_CACHE_OP_READ
;
502 * Fall back to accesses:
504 if (cache_result
== -1)
505 cache_result
= PERF_COUNT_HW_CACHE_RESULT_ACCESS
;
507 memset(&attr
, 0, sizeof(attr
));
508 attr
.config
= cache_type
| (cache_op
<< 8) | (cache_result
<< 16);
509 attr
.type
= PERF_TYPE_HW_CACHE
;
512 if (config_attr(&attr
, head_config
, err
,
516 if (get_config_terms(head_config
, &config_terms
))
519 return add_event(list
, idx
, &attr
, config_name
? : name
, &config_terms
);
522 static void tracepoint_error(struct parse_events_error
*e
, int err
,
523 const char *sys
, const char *name
)
532 * We get error directly from syscall errno ( > 0),
533 * or from encoded pointer's error ( < 0).
539 str
= "can't access trace events";
542 str
= "unknown tracepoint";
545 str
= "failed to add tracepoint";
549 tracing_path__strerror_open_tp(err
, help
, sizeof(help
), sys
, name
);
550 parse_events__handle_error(e
, 0, strdup(str
), strdup(help
));
553 static int add_tracepoint(struct list_head
*list
, int *idx
,
554 const char *sys_name
, const char *evt_name
,
555 struct parse_events_error
*err
,
556 struct list_head
*head_config
)
558 struct evsel
*evsel
= evsel__newtp_idx(sys_name
, evt_name
, (*idx
)++);
561 tracepoint_error(err
, PTR_ERR(evsel
), sys_name
, evt_name
);
562 return PTR_ERR(evsel
);
566 LIST_HEAD(config_terms
);
568 if (get_config_terms(head_config
, &config_terms
))
570 list_splice(&config_terms
, &evsel
->config_terms
);
573 list_add_tail(&evsel
->core
.node
, list
);
577 static int add_tracepoint_multi_event(struct list_head
*list
, int *idx
,
578 const char *sys_name
, const char *evt_name
,
579 struct parse_events_error
*err
,
580 struct list_head
*head_config
)
583 struct dirent
*evt_ent
;
585 int ret
= 0, found
= 0;
587 evt_path
= get_events_file(sys_name
);
589 tracepoint_error(err
, errno
, sys_name
, evt_name
);
592 evt_dir
= opendir(evt_path
);
594 put_events_file(evt_path
);
595 tracepoint_error(err
, errno
, sys_name
, evt_name
);
599 while (!ret
&& (evt_ent
= readdir(evt_dir
))) {
600 if (!strcmp(evt_ent
->d_name
, ".")
601 || !strcmp(evt_ent
->d_name
, "..")
602 || !strcmp(evt_ent
->d_name
, "enable")
603 || !strcmp(evt_ent
->d_name
, "filter"))
606 if (!strglobmatch(evt_ent
->d_name
, evt_name
))
611 ret
= add_tracepoint(list
, idx
, sys_name
, evt_ent
->d_name
,
616 tracepoint_error(err
, ENOENT
, sys_name
, evt_name
);
620 put_events_file(evt_path
);
625 static int add_tracepoint_event(struct list_head
*list
, int *idx
,
626 const char *sys_name
, const char *evt_name
,
627 struct parse_events_error
*err
,
628 struct list_head
*head_config
)
630 return strpbrk(evt_name
, "*?") ?
631 add_tracepoint_multi_event(list
, idx
, sys_name
, evt_name
,
633 add_tracepoint(list
, idx
, sys_name
, evt_name
,
637 static int add_tracepoint_multi_sys(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 struct dirent
*events_ent
;
646 events_dir
= tracing_events__opendir();
648 tracepoint_error(err
, errno
, sys_name
, evt_name
);
652 while (!ret
&& (events_ent
= readdir(events_dir
))) {
653 if (!strcmp(events_ent
->d_name
, ".")
654 || !strcmp(events_ent
->d_name
, "..")
655 || !strcmp(events_ent
->d_name
, "enable")
656 || !strcmp(events_ent
->d_name
, "header_event")
657 || !strcmp(events_ent
->d_name
, "header_page"))
660 if (!strglobmatch(events_ent
->d_name
, sys_name
))
663 ret
= add_tracepoint_event(list
, idx
, events_ent
->d_name
,
664 evt_name
, err
, head_config
);
667 closedir(events_dir
);
671 #ifdef HAVE_LIBBPF_SUPPORT
672 struct __add_bpf_event_param
{
673 struct parse_events_state
*parse_state
;
674 struct list_head
*list
;
675 struct list_head
*head_config
;
678 static int add_bpf_event(const char *group
, const char *event
, int fd
, struct bpf_object
*obj
,
681 LIST_HEAD(new_evsels
);
682 struct __add_bpf_event_param
*param
= _param
;
683 struct parse_events_state
*parse_state
= param
->parse_state
;
684 struct list_head
*list
= param
->list
;
688 * Check if we should add the event, i.e. if it is a TP but starts with a '!',
689 * then don't add the tracepoint, this will be used for something else, like
690 * adding to a BPF_MAP_TYPE_PROG_ARRAY.
692 * See tools/perf/examples/bpf/augmented_raw_syscalls.c
697 pr_debug("add bpf event %s:%s and attach bpf program %d\n",
700 err
= parse_events_add_tracepoint(&new_evsels
, &parse_state
->idx
, group
,
701 event
, parse_state
->error
,
704 struct evsel
*evsel
, *tmp
;
706 pr_debug("Failed to add BPF event %s:%s\n",
708 list_for_each_entry_safe(evsel
, tmp
, &new_evsels
, core
.node
) {
709 list_del_init(&evsel
->core
.node
);
710 evsel__delete(evsel
);
714 pr_debug("adding %s:%s\n", group
, event
);
716 list_for_each_entry(pos
, &new_evsels
, core
.node
) {
717 pr_debug("adding %s:%s to %p\n",
722 list_splice(&new_evsels
, list
);
726 int parse_events_load_bpf_obj(struct parse_events_state
*parse_state
,
727 struct list_head
*list
,
728 struct bpf_object
*obj
,
729 struct list_head
*head_config
)
733 struct __add_bpf_event_param param
= {parse_state
, list
, head_config
};
734 static bool registered_unprobe_atexit
= false;
736 if (IS_ERR(obj
) || !obj
) {
737 snprintf(errbuf
, sizeof(errbuf
),
738 "Internal error: load bpf obj with NULL");
744 * Register atexit handler before calling bpf__probe() so
745 * bpf__probe() don't need to unprobe probe points its already
746 * created when failure.
748 if (!registered_unprobe_atexit
) {
750 registered_unprobe_atexit
= true;
753 err
= bpf__probe(obj
);
755 bpf__strerror_probe(obj
, err
, errbuf
, sizeof(errbuf
));
759 err
= bpf__load(obj
);
761 bpf__strerror_load(obj
, err
, errbuf
, sizeof(errbuf
));
765 err
= bpf__foreach_event(obj
, add_bpf_event
, ¶m
);
767 snprintf(errbuf
, sizeof(errbuf
),
768 "Attach events in BPF object failed");
774 parse_events__handle_error(parse_state
->error
, 0,
775 strdup(errbuf
), strdup("(add -v to see detail)"));
780 parse_events_config_bpf(struct parse_events_state
*parse_state
,
781 struct bpf_object
*obj
,
782 struct list_head
*head_config
)
784 struct parse_events_term
*term
;
787 if (!head_config
|| list_empty(head_config
))
790 list_for_each_entry(term
, head_config
, list
) {
793 if (term
->type_term
!= PARSE_EVENTS__TERM_TYPE_USER
) {
794 parse_events__handle_error(parse_state
->error
, term
->err_term
,
795 strdup("Invalid config term for BPF object"),
800 err
= bpf__config_obj(obj
, term
, parse_state
->evlist
, &error_pos
);
805 bpf__strerror_config_obj(obj
, term
, parse_state
->evlist
,
806 &error_pos
, err
, errbuf
,
809 if (err
== -BPF_LOADER_ERRNO__OBJCONF_MAP_VALUE
)
812 idx
= term
->err_term
+ error_pos
;
814 parse_events__handle_error(parse_state
->error
, idx
,
817 "Hint:\tValid config terms:\n"
818 " \tmap:[<arraymap>].value<indices>=[value]\n"
819 " \tmap:[<eventmap>].event<indices>=[event]\n"
821 " \twhere <indices> is something like [0,3...5] or [all]\n"
822 " \t(add -v to see detail)"));
830 * Split config terms:
831 * perf record -e bpf.c/call-graph=fp,map:array.value[0]=1/ ...
832 * 'call-graph=fp' is 'evt config', should be applied to each
834 * 'map:array.value[0]=1' is 'obj config', should be processed
835 * with parse_events_config_bpf.
837 * Move object config terms from the first list to obj_head_config.
840 split_bpf_config_terms(struct list_head
*evt_head_config
,
841 struct list_head
*obj_head_config
)
843 struct parse_events_term
*term
, *temp
;
846 * Currectly, all possible user config term
847 * belong to bpf object. parse_events__is_hardcoded_term()
848 * happends to be a good flag.
850 * See parse_events_config_bpf() and
851 * config_term_tracepoint().
853 list_for_each_entry_safe(term
, temp
, evt_head_config
, list
)
854 if (!parse_events__is_hardcoded_term(term
))
855 list_move_tail(&term
->list
, obj_head_config
);
858 int parse_events_load_bpf(struct parse_events_state
*parse_state
,
859 struct list_head
*list
,
862 struct list_head
*head_config
)
865 struct bpf_object
*obj
;
866 LIST_HEAD(obj_head_config
);
869 split_bpf_config_terms(head_config
, &obj_head_config
);
871 obj
= bpf__prepare_load(bpf_file_name
, source
);
878 snprintf(errbuf
, sizeof(errbuf
),
879 "BPF support is not compiled");
881 bpf__strerror_prepare_load(bpf_file_name
,
886 parse_events__handle_error(parse_state
->error
, 0,
887 strdup(errbuf
), strdup("(add -v to see detail)"));
891 err
= parse_events_load_bpf_obj(parse_state
, list
, obj
, head_config
);
894 err
= parse_events_config_bpf(parse_state
, obj
, &obj_head_config
);
897 * Caller doesn't know anything about obj_head_config,
898 * so combine them together again before returnning.
901 list_splice_tail(&obj_head_config
, head_config
);
904 #else // HAVE_LIBBPF_SUPPORT
905 int parse_events_load_bpf_obj(struct parse_events_state
*parse_state
,
906 struct list_head
*list __maybe_unused
,
907 struct bpf_object
*obj __maybe_unused
,
908 struct list_head
*head_config __maybe_unused
)
910 parse_events__handle_error(parse_state
->error
, 0,
911 strdup("BPF support is not compiled"),
912 strdup("Make sure libbpf-devel is available at build time."));
916 int parse_events_load_bpf(struct parse_events_state
*parse_state
,
917 struct list_head
*list __maybe_unused
,
918 char *bpf_file_name __maybe_unused
,
919 bool source __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."));
927 #endif // HAVE_LIBBPF_SUPPORT
930 parse_breakpoint_type(const char *type
, struct perf_event_attr
*attr
)
934 for (i
= 0; i
< 3; i
++) {
935 if (!type
|| !type
[i
])
938 #define CHECK_SET_TYPE(bit) \
940 if (attr->bp_type & bit) \
943 attr->bp_type |= bit; \
948 CHECK_SET_TYPE(HW_BREAKPOINT_R
);
951 CHECK_SET_TYPE(HW_BREAKPOINT_W
);
954 CHECK_SET_TYPE(HW_BREAKPOINT_X
);
961 #undef CHECK_SET_TYPE
963 if (!attr
->bp_type
) /* Default */
964 attr
->bp_type
= HW_BREAKPOINT_R
| HW_BREAKPOINT_W
;
969 int parse_events_add_breakpoint(struct list_head
*list
, int *idx
,
970 u64 addr
, char *type
, u64 len
)
972 struct perf_event_attr attr
;
974 memset(&attr
, 0, sizeof(attr
));
977 if (parse_breakpoint_type(type
, &attr
))
980 /* Provide some defaults if len is not specified */
982 if (attr
.bp_type
== HW_BREAKPOINT_X
)
985 len
= HW_BREAKPOINT_LEN_4
;
990 attr
.type
= PERF_TYPE_BREAKPOINT
;
991 attr
.sample_period
= 1;
993 return add_event(list
, idx
, &attr
, NULL
, NULL
);
996 static int check_type_val(struct parse_events_term
*term
,
997 struct parse_events_error
*err
,
1000 if (type
== term
->type_val
)
1004 parse_events__handle_error(err
, term
->err_val
,
1005 type
== PARSE_EVENTS__TERM_TYPE_NUM
1006 ? strdup("expected numeric value")
1007 : strdup("expected string value"),
1014 * Update according to parse-events.l
1016 static const char *config_term_names
[__PARSE_EVENTS__TERM_TYPE_NR
] = {
1017 [PARSE_EVENTS__TERM_TYPE_USER
] = "<sysfs term>",
1018 [PARSE_EVENTS__TERM_TYPE_CONFIG
] = "config",
1019 [PARSE_EVENTS__TERM_TYPE_CONFIG1
] = "config1",
1020 [PARSE_EVENTS__TERM_TYPE_CONFIG2
] = "config2",
1021 [PARSE_EVENTS__TERM_TYPE_NAME
] = "name",
1022 [PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD
] = "period",
1023 [PARSE_EVENTS__TERM_TYPE_SAMPLE_FREQ
] = "freq",
1024 [PARSE_EVENTS__TERM_TYPE_BRANCH_SAMPLE_TYPE
] = "branch_type",
1025 [PARSE_EVENTS__TERM_TYPE_TIME
] = "time",
1026 [PARSE_EVENTS__TERM_TYPE_CALLGRAPH
] = "call-graph",
1027 [PARSE_EVENTS__TERM_TYPE_STACKSIZE
] = "stack-size",
1028 [PARSE_EVENTS__TERM_TYPE_NOINHERIT
] = "no-inherit",
1029 [PARSE_EVENTS__TERM_TYPE_INHERIT
] = "inherit",
1030 [PARSE_EVENTS__TERM_TYPE_MAX_STACK
] = "max-stack",
1031 [PARSE_EVENTS__TERM_TYPE_MAX_EVENTS
] = "nr",
1032 [PARSE_EVENTS__TERM_TYPE_OVERWRITE
] = "overwrite",
1033 [PARSE_EVENTS__TERM_TYPE_NOOVERWRITE
] = "no-overwrite",
1034 [PARSE_EVENTS__TERM_TYPE_DRV_CFG
] = "driver-config",
1035 [PARSE_EVENTS__TERM_TYPE_PERCORE
] = "percore",
1036 [PARSE_EVENTS__TERM_TYPE_AUX_OUTPUT
] = "aux-output",
1037 [PARSE_EVENTS__TERM_TYPE_AUX_SAMPLE_SIZE
] = "aux-sample-size",
1040 static bool config_term_shrinked
;
1043 config_term_avail(int term_type
, struct parse_events_error
*err
)
1047 if (term_type
< 0 || term_type
>= __PARSE_EVENTS__TERM_TYPE_NR
) {
1048 parse_events__handle_error(err
, -1,
1049 strdup("Invalid term_type"), NULL
);
1052 if (!config_term_shrinked
)
1055 switch (term_type
) {
1056 case PARSE_EVENTS__TERM_TYPE_CONFIG
:
1057 case PARSE_EVENTS__TERM_TYPE_CONFIG1
:
1058 case PARSE_EVENTS__TERM_TYPE_CONFIG2
:
1059 case PARSE_EVENTS__TERM_TYPE_NAME
:
1060 case PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD
:
1061 case PARSE_EVENTS__TERM_TYPE_PERCORE
:
1067 /* term_type is validated so indexing is safe */
1068 if (asprintf(&err_str
, "'%s' is not usable in 'perf stat'",
1069 config_term_names
[term_type
]) >= 0)
1070 parse_events__handle_error(err
, -1, err_str
, NULL
);
1075 void parse_events__shrink_config_terms(void)
1077 config_term_shrinked
= true;
1080 static int config_term_common(struct perf_event_attr
*attr
,
1081 struct parse_events_term
*term
,
1082 struct parse_events_error
*err
)
1084 #define CHECK_TYPE_VAL(type) \
1086 if (check_type_val(term, err, PARSE_EVENTS__TERM_TYPE_ ## type)) \
1090 switch (term
->type_term
) {
1091 case PARSE_EVENTS__TERM_TYPE_CONFIG
:
1092 CHECK_TYPE_VAL(NUM
);
1093 attr
->config
= term
->val
.num
;
1095 case PARSE_EVENTS__TERM_TYPE_CONFIG1
:
1096 CHECK_TYPE_VAL(NUM
);
1097 attr
->config1
= term
->val
.num
;
1099 case PARSE_EVENTS__TERM_TYPE_CONFIG2
:
1100 CHECK_TYPE_VAL(NUM
);
1101 attr
->config2
= term
->val
.num
;
1103 case PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD
:
1104 CHECK_TYPE_VAL(NUM
);
1106 case PARSE_EVENTS__TERM_TYPE_SAMPLE_FREQ
:
1107 CHECK_TYPE_VAL(NUM
);
1109 case PARSE_EVENTS__TERM_TYPE_BRANCH_SAMPLE_TYPE
:
1110 CHECK_TYPE_VAL(STR
);
1111 if (strcmp(term
->val
.str
, "no") &&
1112 parse_branch_str(term
->val
.str
,
1113 &attr
->branch_sample_type
)) {
1114 parse_events__handle_error(err
, term
->err_val
,
1115 strdup("invalid branch sample type"),
1120 case PARSE_EVENTS__TERM_TYPE_TIME
:
1121 CHECK_TYPE_VAL(NUM
);
1122 if (term
->val
.num
> 1) {
1123 parse_events__handle_error(err
, term
->err_val
,
1124 strdup("expected 0 or 1"),
1129 case PARSE_EVENTS__TERM_TYPE_CALLGRAPH
:
1130 CHECK_TYPE_VAL(STR
);
1132 case PARSE_EVENTS__TERM_TYPE_STACKSIZE
:
1133 CHECK_TYPE_VAL(NUM
);
1135 case PARSE_EVENTS__TERM_TYPE_INHERIT
:
1136 CHECK_TYPE_VAL(NUM
);
1138 case PARSE_EVENTS__TERM_TYPE_NOINHERIT
:
1139 CHECK_TYPE_VAL(NUM
);
1141 case PARSE_EVENTS__TERM_TYPE_OVERWRITE
:
1142 CHECK_TYPE_VAL(NUM
);
1144 case PARSE_EVENTS__TERM_TYPE_NOOVERWRITE
:
1145 CHECK_TYPE_VAL(NUM
);
1147 case PARSE_EVENTS__TERM_TYPE_NAME
:
1148 CHECK_TYPE_VAL(STR
);
1150 case PARSE_EVENTS__TERM_TYPE_MAX_STACK
:
1151 CHECK_TYPE_VAL(NUM
);
1153 case PARSE_EVENTS__TERM_TYPE_MAX_EVENTS
:
1154 CHECK_TYPE_VAL(NUM
);
1156 case PARSE_EVENTS__TERM_TYPE_PERCORE
:
1157 CHECK_TYPE_VAL(NUM
);
1158 if ((unsigned int)term
->val
.num
> 1) {
1159 parse_events__handle_error(err
, term
->err_val
,
1160 strdup("expected 0 or 1"),
1165 case PARSE_EVENTS__TERM_TYPE_AUX_OUTPUT
:
1166 CHECK_TYPE_VAL(NUM
);
1168 case PARSE_EVENTS__TERM_TYPE_AUX_SAMPLE_SIZE
:
1169 CHECK_TYPE_VAL(NUM
);
1170 if (term
->val
.num
> UINT_MAX
) {
1171 parse_events__handle_error(err
, term
->err_val
,
1178 parse_events__handle_error(err
, term
->err_term
,
1179 strdup("unknown term"),
1180 parse_events_formats_error_string(NULL
));
1185 * Check term availbility after basic checking so
1186 * PARSE_EVENTS__TERM_TYPE_USER can be found and filtered.
1188 * If check availbility at the entry of this function,
1189 * user will see "'<sysfs term>' is not usable in 'perf stat'"
1190 * if an invalid config term is provided for legacy events
1191 * (for example, instructions/badterm/...), which is confusing.
1193 if (!config_term_avail(term
->type_term
, err
))
1196 #undef CHECK_TYPE_VAL
1199 static int config_term_pmu(struct perf_event_attr
*attr
,
1200 struct parse_events_term
*term
,
1201 struct parse_events_error
*err
)
1203 if (term
->type_term
== PARSE_EVENTS__TERM_TYPE_USER
||
1204 term
->type_term
== PARSE_EVENTS__TERM_TYPE_DRV_CFG
)
1206 * Always succeed for sysfs terms, as we dont know
1207 * at this point what type they need to have.
1211 return config_term_common(attr
, term
, err
);
1214 static int config_term_tracepoint(struct perf_event_attr
*attr
,
1215 struct parse_events_term
*term
,
1216 struct parse_events_error
*err
)
1218 switch (term
->type_term
) {
1219 case PARSE_EVENTS__TERM_TYPE_CALLGRAPH
:
1220 case PARSE_EVENTS__TERM_TYPE_STACKSIZE
:
1221 case PARSE_EVENTS__TERM_TYPE_INHERIT
:
1222 case PARSE_EVENTS__TERM_TYPE_NOINHERIT
:
1223 case PARSE_EVENTS__TERM_TYPE_MAX_STACK
:
1224 case PARSE_EVENTS__TERM_TYPE_MAX_EVENTS
:
1225 case PARSE_EVENTS__TERM_TYPE_OVERWRITE
:
1226 case PARSE_EVENTS__TERM_TYPE_NOOVERWRITE
:
1227 case PARSE_EVENTS__TERM_TYPE_AUX_OUTPUT
:
1228 case PARSE_EVENTS__TERM_TYPE_AUX_SAMPLE_SIZE
:
1229 return config_term_common(attr
, term
, err
);
1232 parse_events__handle_error(err
, term
->err_term
,
1233 strdup("unknown term"),
1234 strdup("valid terms: call-graph,stack-size\n"));
1242 static int config_attr(struct perf_event_attr
*attr
,
1243 struct list_head
*head
,
1244 struct parse_events_error
*err
,
1245 config_term_func_t config_term
)
1247 struct parse_events_term
*term
;
1249 list_for_each_entry(term
, head
, list
)
1250 if (config_term(attr
, term
, err
))
1256 static int get_config_terms(struct list_head
*head_config
,
1257 struct list_head
*head_terms __maybe_unused
)
1259 #define ADD_CONFIG_TERM(__type, __weak) \
1260 struct evsel_config_term *__t; \
1262 __t = zalloc(sizeof(*__t)); \
1266 INIT_LIST_HEAD(&__t->list); \
1267 __t->type = EVSEL__CONFIG_TERM_ ## __type; \
1268 __t->weak = __weak; \
1269 list_add_tail(&__t->list, head_terms)
1271 #define ADD_CONFIG_TERM_VAL(__type, __name, __val, __weak) \
1273 ADD_CONFIG_TERM(__type, __weak); \
1274 __t->val.__name = __val; \
1277 #define ADD_CONFIG_TERM_STR(__type, __val, __weak) \
1279 ADD_CONFIG_TERM(__type, __weak); \
1280 __t->val.str = strdup(__val); \
1281 if (!__t->val.str) { \
1285 __t->free_str = true; \
1288 struct parse_events_term
*term
;
1290 list_for_each_entry(term
, head_config
, list
) {
1291 switch (term
->type_term
) {
1292 case PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD
:
1293 ADD_CONFIG_TERM_VAL(PERIOD
, period
, term
->val
.num
, term
->weak
);
1295 case PARSE_EVENTS__TERM_TYPE_SAMPLE_FREQ
:
1296 ADD_CONFIG_TERM_VAL(FREQ
, freq
, term
->val
.num
, term
->weak
);
1298 case PARSE_EVENTS__TERM_TYPE_TIME
:
1299 ADD_CONFIG_TERM_VAL(TIME
, time
, term
->val
.num
, term
->weak
);
1301 case PARSE_EVENTS__TERM_TYPE_CALLGRAPH
:
1302 ADD_CONFIG_TERM_STR(CALLGRAPH
, term
->val
.str
, term
->weak
);
1304 case PARSE_EVENTS__TERM_TYPE_BRANCH_SAMPLE_TYPE
:
1305 ADD_CONFIG_TERM_STR(BRANCH
, term
->val
.str
, term
->weak
);
1307 case PARSE_EVENTS__TERM_TYPE_STACKSIZE
:
1308 ADD_CONFIG_TERM_VAL(STACK_USER
, stack_user
,
1309 term
->val
.num
, term
->weak
);
1311 case PARSE_EVENTS__TERM_TYPE_INHERIT
:
1312 ADD_CONFIG_TERM_VAL(INHERIT
, inherit
,
1313 term
->val
.num
? 1 : 0, term
->weak
);
1315 case PARSE_EVENTS__TERM_TYPE_NOINHERIT
:
1316 ADD_CONFIG_TERM_VAL(INHERIT
, inherit
,
1317 term
->val
.num
? 0 : 1, term
->weak
);
1319 case PARSE_EVENTS__TERM_TYPE_MAX_STACK
:
1320 ADD_CONFIG_TERM_VAL(MAX_STACK
, max_stack
,
1321 term
->val
.num
, term
->weak
);
1323 case PARSE_EVENTS__TERM_TYPE_MAX_EVENTS
:
1324 ADD_CONFIG_TERM_VAL(MAX_EVENTS
, max_events
,
1325 term
->val
.num
, term
->weak
);
1327 case PARSE_EVENTS__TERM_TYPE_OVERWRITE
:
1328 ADD_CONFIG_TERM_VAL(OVERWRITE
, overwrite
,
1329 term
->val
.num
? 1 : 0, term
->weak
);
1331 case PARSE_EVENTS__TERM_TYPE_NOOVERWRITE
:
1332 ADD_CONFIG_TERM_VAL(OVERWRITE
, overwrite
,
1333 term
->val
.num
? 0 : 1, term
->weak
);
1335 case PARSE_EVENTS__TERM_TYPE_DRV_CFG
:
1336 ADD_CONFIG_TERM_STR(DRV_CFG
, term
->val
.str
, term
->weak
);
1338 case PARSE_EVENTS__TERM_TYPE_PERCORE
:
1339 ADD_CONFIG_TERM_VAL(PERCORE
, percore
,
1340 term
->val
.num
? true : false, term
->weak
);
1342 case PARSE_EVENTS__TERM_TYPE_AUX_OUTPUT
:
1343 ADD_CONFIG_TERM_VAL(AUX_OUTPUT
, aux_output
,
1344 term
->val
.num
? 1 : 0, term
->weak
);
1346 case PARSE_EVENTS__TERM_TYPE_AUX_SAMPLE_SIZE
:
1347 ADD_CONFIG_TERM_VAL(AUX_SAMPLE_SIZE
, aux_sample_size
,
1348 term
->val
.num
, term
->weak
);
1358 * Add EVSEL__CONFIG_TERM_CFG_CHG where cfg_chg will have a bit set for
1359 * each bit of attr->config that the user has changed.
1361 static int get_config_chgs(struct perf_pmu
*pmu
, struct list_head
*head_config
,
1362 struct list_head
*head_terms
)
1364 struct parse_events_term
*term
;
1368 list_for_each_entry(term
, head_config
, list
) {
1369 switch (term
->type_term
) {
1370 case PARSE_EVENTS__TERM_TYPE_USER
:
1371 type
= perf_pmu__format_type(&pmu
->format
, term
->config
);
1372 if (type
!= PERF_PMU_FORMAT_VALUE_CONFIG
)
1374 bits
|= perf_pmu__format_bits(&pmu
->format
, term
->config
);
1376 case PARSE_EVENTS__TERM_TYPE_CONFIG
:
1385 ADD_CONFIG_TERM_VAL(CFG_CHG
, cfg_chg
, bits
, false);
1387 #undef ADD_CONFIG_TERM
1391 int parse_events_add_tracepoint(struct list_head
*list
, int *idx
,
1392 const char *sys
, const char *event
,
1393 struct parse_events_error
*err
,
1394 struct list_head
*head_config
)
1397 struct perf_event_attr attr
;
1399 if (config_attr(&attr
, head_config
, err
,
1400 config_term_tracepoint
))
1404 if (strpbrk(sys
, "*?"))
1405 return add_tracepoint_multi_sys(list
, idx
, sys
, event
,
1408 return add_tracepoint_event(list
, idx
, sys
, event
,
1412 int parse_events_add_numeric(struct parse_events_state
*parse_state
,
1413 struct list_head
*list
,
1414 u32 type
, u64 config
,
1415 struct list_head
*head_config
)
1417 struct perf_event_attr attr
;
1418 LIST_HEAD(config_terms
);
1420 memset(&attr
, 0, sizeof(attr
));
1422 attr
.config
= config
;
1425 if (config_attr(&attr
, head_config
, parse_state
->error
,
1426 config_term_common
))
1429 if (get_config_terms(head_config
, &config_terms
))
1433 return add_event(list
, &parse_state
->idx
, &attr
,
1434 get_config_name(head_config
), &config_terms
);
1437 int parse_events_add_tool(struct parse_events_state
*parse_state
,
1438 struct list_head
*list
,
1439 enum perf_tool_event tool_event
)
1441 return add_event_tool(list
, &parse_state
->idx
, tool_event
);
1444 static bool config_term_percore(struct list_head
*config_terms
)
1446 struct evsel_config_term
*term
;
1448 list_for_each_entry(term
, config_terms
, list
) {
1449 if (term
->type
== EVSEL__CONFIG_TERM_PERCORE
)
1450 return term
->val
.percore
;
1456 int parse_events_add_pmu(struct parse_events_state
*parse_state
,
1457 struct list_head
*list
, char *name
,
1458 struct list_head
*head_config
,
1459 bool auto_merge_stats
,
1462 struct perf_event_attr attr
;
1463 struct perf_pmu_info info
;
1464 struct perf_pmu
*pmu
;
1465 struct evsel
*evsel
;
1466 struct parse_events_error
*err
= parse_state
->error
;
1467 bool use_uncore_alias
;
1468 LIST_HEAD(config_terms
);
1471 fprintf(stderr
, "Attempting to add event pmu '%s' with '",
1474 struct parse_events_term
*term
;
1476 list_for_each_entry(term
, head_config
, list
) {
1477 fprintf(stderr
, "%s,", term
->config
);
1480 fprintf(stderr
, "' that may result in non-fatal errors\n");
1483 pmu
= parse_state
->fake_pmu
?: perf_pmu__find(name
);
1487 if (asprintf(&err_str
,
1488 "Cannot find PMU `%s'. Missing kernel support?",
1490 parse_events__handle_error(err
, 0, err_str
, NULL
);
1494 if (pmu
->default_config
) {
1495 memcpy(&attr
, pmu
->default_config
,
1496 sizeof(struct perf_event_attr
));
1498 memset(&attr
, 0, sizeof(attr
));
1501 use_uncore_alias
= (pmu
->is_uncore
&& use_alias
);
1504 attr
.type
= pmu
->type
;
1505 evsel
= __add_event(list
, &parse_state
->idx
, &attr
, true, NULL
,
1506 pmu
, NULL
, auto_merge_stats
, NULL
);
1508 evsel
->pmu_name
= name
? strdup(name
) : NULL
;
1509 evsel
->use_uncore_alias
= use_uncore_alias
;
1516 if (!parse_state
->fake_pmu
&& perf_pmu__check_alias(pmu
, head_config
, &info
))
1520 fprintf(stderr
, "After aliases, add event pmu '%s' with '",
1523 struct parse_events_term
*term
;
1525 list_for_each_entry(term
, head_config
, list
) {
1526 fprintf(stderr
, "%s,", term
->config
);
1529 fprintf(stderr
, "' that may result in non-fatal errors\n");
1533 * Configure hardcoded terms first, no need to check
1534 * return value when called with fail == 0 ;)
1536 if (config_attr(&attr
, head_config
, parse_state
->error
, config_term_pmu
))
1539 if (get_config_terms(head_config
, &config_terms
))
1543 * When using default config, record which bits of attr->config were
1544 * changed by the user.
1546 if (pmu
->default_config
&& get_config_chgs(pmu
, head_config
, &config_terms
))
1549 if (!parse_state
->fake_pmu
&& perf_pmu__config(pmu
, &attr
, head_config
, parse_state
->error
)) {
1550 struct evsel_config_term
*pos
, *tmp
;
1552 list_for_each_entry_safe(pos
, tmp
, &config_terms
, list
) {
1553 list_del_init(&pos
->list
);
1555 zfree(&pos
->val
.str
);
1561 evsel
= __add_event(list
, &parse_state
->idx
, &attr
, true,
1562 get_config_name(head_config
), pmu
,
1563 &config_terms
, auto_merge_stats
, NULL
);
1567 evsel
->pmu_name
= name
? strdup(name
) : NULL
;
1568 evsel
->use_uncore_alias
= use_uncore_alias
;
1569 evsel
->percore
= config_term_percore(&evsel
->config_terms
);
1571 if (parse_state
->fake_pmu
)
1574 evsel
->unit
= info
.unit
;
1575 evsel
->scale
= info
.scale
;
1576 evsel
->per_pkg
= info
.per_pkg
;
1577 evsel
->snapshot
= info
.snapshot
;
1578 evsel
->metric_expr
= info
.metric_expr
;
1579 evsel
->metric_name
= info
.metric_name
;
1583 int parse_events_multi_pmu_add(struct parse_events_state
*parse_state
,
1584 char *str
, struct list_head
**listp
)
1586 struct parse_events_term
*term
;
1587 struct list_head
*list
;
1588 struct perf_pmu
*pmu
= NULL
;
1592 /* Add it for all PMUs that support the alias */
1593 list
= malloc(sizeof(struct list_head
));
1596 INIT_LIST_HEAD(list
);
1597 while ((pmu
= perf_pmu__scan(pmu
)) != NULL
) {
1598 struct perf_pmu_alias
*alias
;
1600 list_for_each_entry(alias
, &pmu
->aliases
, list
) {
1601 if (!strcasecmp(alias
->name
, str
)) {
1602 struct list_head
*head
;
1605 head
= malloc(sizeof(struct list_head
));
1608 INIT_LIST_HEAD(head
);
1609 config
= strdup(str
);
1612 if (parse_events_term__num(&term
,
1613 PARSE_EVENTS__TERM_TYPE_USER
,
1614 config
, 1, false, &config
,
1620 list_add_tail(&term
->list
, head
);
1622 if (!parse_events_add_pmu(parse_state
, list
,
1625 pr_debug("%s -> %s/%s/\n", str
,
1626 pmu
->name
, alias
->str
);
1630 parse_events_terms__delete(head
);
1642 int parse_events__modifier_group(struct list_head
*list
,
1645 return parse_events__modifier_event(list
, event_mod
, true);
1649 * Check if the two uncore PMUs are from the same uncore block
1650 * The format of the uncore PMU name is uncore_#blockname_#pmuidx
1652 static bool is_same_uncore_block(const char *pmu_name_a
, const char *pmu_name_b
)
1654 char *end_a
, *end_b
;
1656 end_a
= strrchr(pmu_name_a
, '_');
1657 end_b
= strrchr(pmu_name_b
, '_');
1659 if (!end_a
|| !end_b
)
1662 if ((end_a
- pmu_name_a
) != (end_b
- pmu_name_b
))
1665 return (strncmp(pmu_name_a
, pmu_name_b
, end_a
- pmu_name_a
) == 0);
1669 parse_events__set_leader_for_uncore_aliase(char *name
, struct list_head
*list
,
1670 struct parse_events_state
*parse_state
)
1672 struct evsel
*evsel
, *leader
;
1674 bool is_leader
= true;
1675 int i
, nr_pmu
= 0, total_members
, ret
= 0;
1677 leader
= list_first_entry(list
, struct evsel
, core
.node
);
1678 evsel
= list_last_entry(list
, struct evsel
, core
.node
);
1679 total_members
= evsel
->idx
- leader
->idx
+ 1;
1681 leaders
= calloc(total_members
, sizeof(uintptr_t));
1682 if (WARN_ON(!leaders
))
1686 * Going through the whole group and doing sanity check.
1687 * All members must use alias, and be from the same uncore block.
1688 * Also, storing the leader events in an array.
1690 __evlist__for_each_entry(list
, evsel
) {
1692 /* Only split the uncore group which members use alias */
1693 if (!evsel
->use_uncore_alias
)
1696 /* The events must be from the same uncore block */
1697 if (!is_same_uncore_block(leader
->pmu_name
, evsel
->pmu_name
))
1703 * If the event's PMU name starts to repeat, it must be a new
1704 * event. That can be used to distinguish the leader from
1705 * other members, even they have the same event name.
1707 if ((leader
!= evsel
) &&
1708 !strcmp(leader
->pmu_name
, evsel
->pmu_name
)) {
1713 /* Store the leader event for each PMU */
1714 leaders
[nr_pmu
++] = (uintptr_t) evsel
;
1717 /* only one event alias */
1718 if (nr_pmu
== total_members
) {
1719 parse_state
->nr_groups
--;
1724 * An uncore event alias is a joint name which means the same event
1725 * runs on all PMUs of a block.
1726 * Perf doesn't support mixed events from different PMUs in the same
1727 * group. The big group has to be split into multiple small groups
1728 * which only include the events from the same PMU.
1730 * Here the uncore event aliases must be from the same uncore block.
1731 * The number of PMUs must be same for each alias. The number of new
1732 * small groups equals to the number of PMUs.
1733 * Setting the leader event for corresponding members in each group.
1736 __evlist__for_each_entry(list
, evsel
) {
1739 evsel
->leader
= (struct evsel
*) leaders
[i
++];
1742 /* The number of members and group name are same for each group */
1743 for (i
= 0; i
< nr_pmu
; i
++) {
1744 evsel
= (struct evsel
*) leaders
[i
];
1745 evsel
->core
.nr_members
= total_members
/ nr_pmu
;
1746 evsel
->group_name
= name
? strdup(name
) : NULL
;
1749 /* Take the new small groups into account */
1750 parse_state
->nr_groups
+= nr_pmu
- 1;
1759 void parse_events__set_leader(char *name
, struct list_head
*list
,
1760 struct parse_events_state
*parse_state
)
1762 struct evsel
*leader
;
1764 if (list_empty(list
)) {
1765 WARN_ONCE(true, "WARNING: failed to set leader: empty list");
1769 if (parse_events__set_leader_for_uncore_aliase(name
, list
, parse_state
))
1772 __evlist__set_leader(list
);
1773 leader
= list_entry(list
->next
, struct evsel
, core
.node
);
1774 leader
->group_name
= name
? strdup(name
) : NULL
;
1777 /* list_event is assumed to point to malloc'ed memory */
1778 void parse_events_update_lists(struct list_head
*list_event
,
1779 struct list_head
*list_all
)
1782 * Called for single event definition. Update the
1783 * 'all event' list, and reinit the 'single event'
1784 * list, for next event definition.
1786 list_splice_tail(list_event
, list_all
);
1790 struct event_modifier
{
1806 static int get_event_modifier(struct event_modifier
*mod
, char *str
,
1807 struct evsel
*evsel
)
1809 int eu
= evsel
? evsel
->core
.attr
.exclude_user
: 0;
1810 int ek
= evsel
? evsel
->core
.attr
.exclude_kernel
: 0;
1811 int eh
= evsel
? evsel
->core
.attr
.exclude_hv
: 0;
1812 int eH
= evsel
? evsel
->core
.attr
.exclude_host
: 0;
1813 int eG
= evsel
? evsel
->core
.attr
.exclude_guest
: 0;
1814 int eI
= evsel
? evsel
->core
.attr
.exclude_idle
: 0;
1815 int precise
= evsel
? evsel
->core
.attr
.precise_ip
: 0;
1816 int precise_max
= 0;
1817 int sample_read
= 0;
1818 int pinned
= evsel
? evsel
->core
.attr
.pinned
: 0;
1819 int exclusive
= evsel
? evsel
->core
.attr
.exclusive
: 0;
1821 int exclude
= eu
| ek
| eh
;
1822 int exclude_GH
= evsel
? evsel
->exclude_GH
: 0;
1825 memset(mod
, 0, sizeof(*mod
));
1830 exclude
= eu
= ek
= eh
= 1;
1831 if (!exclude_GH
&& !perf_guest
)
1834 } else if (*str
== 'k') {
1836 exclude
= eu
= ek
= eh
= 1;
1838 } else if (*str
== 'h') {
1840 exclude
= eu
= ek
= eh
= 1;
1842 } else if (*str
== 'G') {
1844 exclude_GH
= eG
= eH
= 1;
1846 } else if (*str
== 'H') {
1848 exclude_GH
= eG
= eH
= 1;
1850 } else if (*str
== 'I') {
1852 } else if (*str
== 'p') {
1854 /* use of precise requires exclude_guest */
1857 } else if (*str
== 'P') {
1859 } else if (*str
== 'S') {
1861 } else if (*str
== 'D') {
1863 } else if (*str
== 'e') {
1865 } else if (*str
== 'W') {
1876 * 0 - SAMPLE_IP can have arbitrary skid
1877 * 1 - SAMPLE_IP must have constant skid
1878 * 2 - SAMPLE_IP requested to have 0 skid
1879 * 3 - SAMPLE_IP must have 0 skid
1881 * See also PERF_RECORD_MISC_EXACT_IP
1892 mod
->precise
= precise
;
1893 mod
->precise_max
= precise_max
;
1894 mod
->exclude_GH
= exclude_GH
;
1895 mod
->sample_read
= sample_read
;
1896 mod
->pinned
= pinned
;
1898 mod
->exclusive
= exclusive
;
1904 * Basic modifier sanity check to validate it contains only one
1905 * instance of any modifier (apart from 'p') present.
1907 static int check_modifier(char *str
)
1911 /* The sizeof includes 0 byte as well. */
1912 if (strlen(str
) > (sizeof("ukhGHpppPSDIWe") - 1))
1916 if (*p
!= 'p' && strchr(p
+ 1, *p
))
1924 int parse_events__modifier_event(struct list_head
*list
, char *str
, bool add
)
1926 struct evsel
*evsel
;
1927 struct event_modifier mod
;
1932 if (check_modifier(str
))
1935 if (!add
&& get_event_modifier(&mod
, str
, NULL
))
1938 __evlist__for_each_entry(list
, evsel
) {
1939 if (add
&& get_event_modifier(&mod
, str
, evsel
))
1942 evsel
->core
.attr
.exclude_user
= mod
.eu
;
1943 evsel
->core
.attr
.exclude_kernel
= mod
.ek
;
1944 evsel
->core
.attr
.exclude_hv
= mod
.eh
;
1945 evsel
->core
.attr
.precise_ip
= mod
.precise
;
1946 evsel
->core
.attr
.exclude_host
= mod
.eH
;
1947 evsel
->core
.attr
.exclude_guest
= mod
.eG
;
1948 evsel
->core
.attr
.exclude_idle
= mod
.eI
;
1949 evsel
->exclude_GH
= mod
.exclude_GH
;
1950 evsel
->sample_read
= mod
.sample_read
;
1951 evsel
->precise_max
= mod
.precise_max
;
1952 evsel
->weak_group
= mod
.weak
;
1954 if (evsel__is_group_leader(evsel
)) {
1955 evsel
->core
.attr
.pinned
= mod
.pinned
;
1956 evsel
->core
.attr
.exclusive
= mod
.exclusive
;
1963 int parse_events_name(struct list_head
*list
, char *name
)
1965 struct evsel
*evsel
;
1967 __evlist__for_each_entry(list
, evsel
) {
1969 evsel
->name
= strdup(name
);
1976 comp_pmu(const void *p1
, const void *p2
)
1978 struct perf_pmu_event_symbol
*pmu1
= (struct perf_pmu_event_symbol
*) p1
;
1979 struct perf_pmu_event_symbol
*pmu2
= (struct perf_pmu_event_symbol
*) p2
;
1981 return strcasecmp(pmu1
->symbol
, pmu2
->symbol
);
1984 static void perf_pmu__parse_cleanup(void)
1986 if (perf_pmu_events_list_num
> 0) {
1987 struct perf_pmu_event_symbol
*p
;
1990 for (i
= 0; i
< perf_pmu_events_list_num
; i
++) {
1991 p
= perf_pmu_events_list
+ i
;
1994 zfree(&perf_pmu_events_list
);
1995 perf_pmu_events_list_num
= 0;
1999 #define SET_SYMBOL(str, stype) \
2008 * Read the pmu events list from sysfs
2009 * Save it into perf_pmu_events_list
2011 static void perf_pmu__parse_init(void)
2014 struct perf_pmu
*pmu
= NULL
;
2015 struct perf_pmu_alias
*alias
;
2019 while ((pmu
= perf_pmu__scan(pmu
)) != NULL
) {
2020 list_for_each_entry(alias
, &pmu
->aliases
, list
) {
2021 if (strchr(alias
->name
, '-'))
2028 perf_pmu_events_list_num
= -1;
2031 perf_pmu_events_list
= malloc(sizeof(struct perf_pmu_event_symbol
) * len
);
2032 if (!perf_pmu_events_list
)
2034 perf_pmu_events_list_num
= len
;
2038 while ((pmu
= perf_pmu__scan(pmu
)) != NULL
) {
2039 list_for_each_entry(alias
, &pmu
->aliases
, list
) {
2040 struct perf_pmu_event_symbol
*p
= perf_pmu_events_list
+ len
;
2041 char *tmp
= strchr(alias
->name
, '-');
2044 SET_SYMBOL(strndup(alias
->name
, tmp
- alias
->name
),
2045 PMU_EVENT_SYMBOL_PREFIX
);
2047 SET_SYMBOL(strdup(++tmp
), PMU_EVENT_SYMBOL_SUFFIX
);
2050 SET_SYMBOL(strdup(alias
->name
), PMU_EVENT_SYMBOL
);
2055 qsort(perf_pmu_events_list
, len
,
2056 sizeof(struct perf_pmu_event_symbol
), comp_pmu
);
2060 perf_pmu__parse_cleanup();
2064 * This function injects special term in
2065 * perf_pmu_events_list so the test code
2066 * can check on this functionality.
2068 int perf_pmu__test_parse_init(void)
2070 struct perf_pmu_event_symbol
*list
;
2072 list
= malloc(sizeof(*list
) * 1);
2076 list
->type
= PMU_EVENT_SYMBOL
;
2077 list
->symbol
= strdup("read");
2079 if (!list
->symbol
) {
2084 perf_pmu_events_list
= list
;
2085 perf_pmu_events_list_num
= 1;
2089 enum perf_pmu_event_symbol_type
2090 perf_pmu__parse_check(const char *name
)
2092 struct perf_pmu_event_symbol p
, *r
;
2094 /* scan kernel pmu events from sysfs if needed */
2095 if (perf_pmu_events_list_num
== 0)
2096 perf_pmu__parse_init();
2098 * name "cpu" could be prefix of cpu-cycles or cpu// events.
2099 * cpu-cycles has been handled by hardcode.
2100 * So it must be cpu// events, not kernel pmu event.
2102 if ((perf_pmu_events_list_num
<= 0) || !strcmp(name
, "cpu"))
2103 return PMU_EVENT_SYMBOL_ERR
;
2105 p
.symbol
= strdup(name
);
2106 r
= bsearch(&p
, perf_pmu_events_list
,
2107 (size_t) perf_pmu_events_list_num
,
2108 sizeof(struct perf_pmu_event_symbol
), comp_pmu
);
2110 return r
? r
->type
: PMU_EVENT_SYMBOL_ERR
;
2113 static int parse_events__scanner(const char *str
,
2114 struct parse_events_state
*parse_state
)
2116 YY_BUFFER_STATE buffer
;
2120 ret
= parse_events_lex_init_extra(parse_state
, &scanner
);
2124 buffer
= parse_events__scan_string(str
, scanner
);
2127 parse_events_debug
= 1;
2128 parse_events_set_debug(1, scanner
);
2130 ret
= parse_events_parse(parse_state
, scanner
);
2132 parse_events__flush_buffer(buffer
, scanner
);
2133 parse_events__delete_buffer(buffer
, scanner
);
2134 parse_events_lex_destroy(scanner
);
2139 * parse event config string, return a list of event terms.
2141 int parse_events_terms(struct list_head
*terms
, const char *str
)
2143 struct parse_events_state parse_state
= {
2145 .stoken
= PE_START_TERMS
,
2149 ret
= parse_events__scanner(str
, &parse_state
);
2150 perf_pmu__parse_cleanup();
2153 list_splice(parse_state
.terms
, terms
);
2154 zfree(&parse_state
.terms
);
2158 parse_events_terms__delete(parse_state
.terms
);
2162 int __parse_events(struct evlist
*evlist
, const char *str
,
2163 struct parse_events_error
*err
, struct perf_pmu
*fake_pmu
)
2165 struct parse_events_state parse_state
= {
2166 .list
= LIST_HEAD_INIT(parse_state
.list
),
2167 .idx
= evlist
->core
.nr_entries
,
2170 .stoken
= PE_START_EVENTS
,
2171 .fake_pmu
= fake_pmu
,
2175 ret
= parse_events__scanner(str
, &parse_state
);
2176 perf_pmu__parse_cleanup();
2178 if (!ret
&& list_empty(&parse_state
.list
)) {
2179 WARN_ONCE(true, "WARNING: event parser found nothing\n");
2184 * Add list to the evlist even with errors to allow callers to clean up.
2186 evlist__splice_list_tail(evlist
, &parse_state
.list
);
2191 evlist
->nr_groups
+= parse_state
.nr_groups
;
2192 last
= evlist__last(evlist
);
2193 last
->cmdline_group_boundary
= true;
2199 * There are 2 users - builtin-record and builtin-test objects.
2200 * Both call evlist__delete in case of error, so we dont
2206 #define MAX_WIDTH 1000
2207 static int get_term_width(void)
2211 get_term_dimensions(&ws
);
2212 return ws
.ws_col
> MAX_WIDTH
? MAX_WIDTH
: ws
.ws_col
;
2215 static void __parse_events_print_error(int err_idx
, const char *err_str
,
2216 const char *err_help
, const char *event
)
2218 const char *str
= "invalid or unsupported event: ";
2219 char _buf
[MAX_WIDTH
];
2220 char *buf
= (char *) event
;
2223 /* -2 for extra '' in the final fprintf */
2224 int width
= get_term_width() - 2;
2225 int len_event
= strlen(event
);
2226 int len_str
, max_len
, cut
= 0;
2229 * Maximum error index indent, we will cut
2230 * the event string if it's bigger.
2232 int max_err_idx
= 13;
2235 * Let's be specific with the message when
2236 * we have the precise error.
2238 str
= "event syntax error: ";
2239 len_str
= strlen(str
);
2240 max_len
= width
- len_str
;
2244 /* We're cutting from the beginning. */
2245 if (err_idx
> max_err_idx
)
2246 cut
= err_idx
- max_err_idx
;
2248 strncpy(buf
, event
+ cut
, max_len
);
2250 /* Mark cut parts with '..' on both sides. */
2252 buf
[0] = buf
[1] = '.';
2254 if ((len_event
- cut
) > max_len
) {
2255 buf
[max_len
- 1] = buf
[max_len
- 2] = '.';
2259 idx
= len_str
+ err_idx
- cut
;
2262 fprintf(stderr
, "%s'%s'\n", str
, buf
);
2264 fprintf(stderr
, "%*s\\___ %s\n", idx
+ 1, "", err_str
);
2266 fprintf(stderr
, "\n%s\n", err_help
);
2270 void parse_events_print_error(struct parse_events_error
*err
,
2273 if (!err
->num_errors
)
2276 __parse_events_print_error(err
->idx
, err
->str
, err
->help
, event
);
2280 if (err
->num_errors
> 1) {
2281 fputs("\nInitial error:\n", stderr
);
2282 __parse_events_print_error(err
->first_idx
, err
->first_str
,
2283 err
->first_help
, event
);
2284 zfree(&err
->first_str
);
2285 zfree(&err
->first_help
);
2291 int parse_events_option(const struct option
*opt
, const char *str
,
2292 int unset __maybe_unused
)
2294 struct evlist
*evlist
= *(struct evlist
**)opt
->value
;
2295 struct parse_events_error err
;
2298 bzero(&err
, sizeof(err
));
2299 ret
= parse_events(evlist
, str
, &err
);
2302 parse_events_print_error(&err
, str
);
2303 fprintf(stderr
, "Run 'perf list' for a list of valid events\n");
2309 int parse_events_option_new_evlist(const struct option
*opt
, const char *str
, int unset
)
2311 struct evlist
**evlistp
= opt
->value
;
2314 if (*evlistp
== NULL
) {
2315 *evlistp
= evlist__new();
2317 if (*evlistp
== NULL
) {
2318 fprintf(stderr
, "Not enough memory to create evlist\n");
2323 ret
= parse_events_option(opt
, str
, unset
);
2325 evlist__delete(*evlistp
);
2333 foreach_evsel_in_last_glob(struct evlist
*evlist
,
2334 int (*func
)(struct evsel
*evsel
,
2338 struct evsel
*last
= NULL
;
2342 * Don't return when list_empty, give func a chance to report
2343 * error when it found last == NULL.
2345 * So no need to WARN here, let *func do this.
2347 if (evlist
->core
.nr_entries
> 0)
2348 last
= evlist__last(evlist
);
2351 err
= (*func
)(last
, arg
);
2357 if (last
->core
.node
.prev
== &evlist
->core
.entries
)
2359 last
= list_entry(last
->core
.node
.prev
, struct evsel
, core
.node
);
2360 } while (!last
->cmdline_group_boundary
);
2365 static int set_filter(struct evsel
*evsel
, const void *arg
)
2367 const char *str
= arg
;
2369 int nr_addr_filters
= 0;
2370 struct perf_pmu
*pmu
= NULL
;
2372 if (evsel
== NULL
) {
2374 "--filter option should follow a -e tracepoint or HW tracer option\n");
2378 if (evsel
->core
.attr
.type
== PERF_TYPE_TRACEPOINT
) {
2379 if (evsel__append_tp_filter(evsel
, str
) < 0) {
2381 "not enough memory to hold filter string\n");
2388 while ((pmu
= perf_pmu__scan(pmu
)) != NULL
)
2389 if (pmu
->type
== evsel
->core
.attr
.type
) {
2395 perf_pmu__scan_file(pmu
, "nr_addr_filters",
2396 "%d", &nr_addr_filters
);
2398 if (!nr_addr_filters
) {
2400 "This CPU does not support address filtering\n");
2404 if (evsel__append_addr_filter(evsel
, str
) < 0) {
2406 "not enough memory to hold filter string\n");
2413 int parse_filter(const struct option
*opt
, const char *str
,
2414 int unset __maybe_unused
)
2416 struct evlist
*evlist
= *(struct evlist
**)opt
->value
;
2418 return foreach_evsel_in_last_glob(evlist
, set_filter
,
2422 static int add_exclude_perf_filter(struct evsel
*evsel
,
2423 const void *arg __maybe_unused
)
2425 char new_filter
[64];
2427 if (evsel
== NULL
|| evsel
->core
.attr
.type
!= PERF_TYPE_TRACEPOINT
) {
2429 "--exclude-perf option should follow a -e tracepoint option\n");
2433 snprintf(new_filter
, sizeof(new_filter
), "common_pid != %d", getpid());
2435 if (evsel__append_tp_filter(evsel
, new_filter
) < 0) {
2437 "not enough memory to hold filter string\n");
2444 int exclude_perf(const struct option
*opt
,
2445 const char *arg __maybe_unused
,
2446 int unset __maybe_unused
)
2448 struct evlist
*evlist
= *(struct evlist
**)opt
->value
;
2450 return foreach_evsel_in_last_glob(evlist
, add_exclude_perf_filter
,
2454 static const char * const event_type_descriptors
[] = {
2458 "Hardware cache event",
2459 "Raw hardware event descriptor",
2460 "Hardware breakpoint",
2463 static int cmp_string(const void *a
, const void *b
)
2465 const char * const *as
= a
;
2466 const char * const *bs
= b
;
2468 return strcmp(*as
, *bs
);
2472 * Print the events from <debugfs_mount_point>/tracing/events
2475 void print_tracepoint_events(const char *subsys_glob
, const char *event_glob
,
2478 DIR *sys_dir
, *evt_dir
;
2479 struct dirent
*sys_dirent
, *evt_dirent
;
2480 char evt_path
[MAXPATHLEN
];
2482 char **evt_list
= NULL
;
2483 unsigned int evt_i
= 0, evt_num
= 0;
2484 bool evt_num_known
= false;
2487 sys_dir
= tracing_events__opendir();
2491 if (evt_num_known
) {
2492 evt_list
= zalloc(sizeof(char *) * evt_num
);
2494 goto out_close_sys_dir
;
2497 for_each_subsystem(sys_dir
, sys_dirent
) {
2498 if (subsys_glob
!= NULL
&&
2499 !strglobmatch(sys_dirent
->d_name
, subsys_glob
))
2502 dir_path
= get_events_file(sys_dirent
->d_name
);
2505 evt_dir
= opendir(dir_path
);
2509 for_each_event(dir_path
, evt_dir
, evt_dirent
) {
2510 if (event_glob
!= NULL
&&
2511 !strglobmatch(evt_dirent
->d_name
, event_glob
))
2514 if (!evt_num_known
) {
2519 snprintf(evt_path
, MAXPATHLEN
, "%s:%s",
2520 sys_dirent
->d_name
, evt_dirent
->d_name
);
2522 evt_list
[evt_i
] = strdup(evt_path
);
2523 if (evt_list
[evt_i
] == NULL
) {
2524 put_events_file(dir_path
);
2525 goto out_close_evt_dir
;
2531 put_events_file(dir_path
);
2535 if (!evt_num_known
) {
2536 evt_num_known
= true;
2539 qsort(evt_list
, evt_num
, sizeof(char *), cmp_string
);
2541 while (evt_i
< evt_num
) {
2543 printf("%s ", evt_list
[evt_i
++]);
2546 printf(" %-50s [%s]\n", evt_list
[evt_i
++],
2547 event_type_descriptors
[PERF_TYPE_TRACEPOINT
]);
2549 if (evt_num
&& pager_in_use())
2554 for (evt_i
= 0; evt_i
< evt_num
; evt_i
++)
2555 zfree(&evt_list
[evt_i
]);
2564 printf("FATAL: not enough memory to print %s\n",
2565 event_type_descriptors
[PERF_TYPE_TRACEPOINT
]);
2571 * Check whether event is in <debugfs_mount_point>/tracing/events
2574 int is_valid_tracepoint(const char *event_string
)
2576 DIR *sys_dir
, *evt_dir
;
2577 struct dirent
*sys_dirent
, *evt_dirent
;
2578 char evt_path
[MAXPATHLEN
];
2581 sys_dir
= tracing_events__opendir();
2585 for_each_subsystem(sys_dir
, sys_dirent
) {
2586 dir_path
= get_events_file(sys_dirent
->d_name
);
2589 evt_dir
= opendir(dir_path
);
2593 for_each_event(dir_path
, evt_dir
, evt_dirent
) {
2594 snprintf(evt_path
, MAXPATHLEN
, "%s:%s",
2595 sys_dirent
->d_name
, evt_dirent
->d_name
);
2596 if (!strcmp(evt_path
, event_string
)) {
2604 put_events_file(dir_path
);
2610 static bool is_event_supported(u8 type
, unsigned config
)
2614 struct evsel
*evsel
;
2615 struct perf_event_attr attr
= {
2620 struct perf_thread_map
*tmap
= thread_map__new_by_tid(0);
2625 evsel
= evsel__new(&attr
);
2627 open_return
= evsel__open(evsel
, NULL
, tmap
);
2628 ret
= open_return
>= 0;
2630 if (open_return
== -EACCES
) {
2632 * This happens if the paranoid value
2633 * /proc/sys/kernel/perf_event_paranoid is set to 2
2634 * Re-run with exclude_kernel set; we don't do that
2635 * by default as some ARM machines do not support it.
2638 evsel
->core
.attr
.exclude_kernel
= 1;
2639 ret
= evsel__open(evsel
, NULL
, tmap
) >= 0;
2641 evsel__delete(evsel
);
2644 perf_thread_map__put(tmap
);
2648 void print_sdt_events(const char *subsys_glob
, const char *event_glob
,
2651 struct probe_cache
*pcache
;
2652 struct probe_cache_entry
*ent
;
2653 struct strlist
*bidlist
, *sdtlist
;
2654 struct strlist_config cfg
= {.dont_dupstr
= true};
2655 struct str_node
*nd
, *nd2
;
2656 char *buf
, *path
, *ptr
= NULL
;
2657 bool show_detail
= false;
2660 sdtlist
= strlist__new(NULL
, &cfg
);
2662 pr_debug("Failed to allocate new strlist for SDT\n");
2665 bidlist
= build_id_cache__list_all(true);
2667 pr_debug("Failed to get buildids: %d\n", errno
);
2670 strlist__for_each_entry(nd
, bidlist
) {
2671 pcache
= probe_cache__new(nd
->s
, NULL
);
2674 list_for_each_entry(ent
, &pcache
->entries
, node
) {
2678 !strglobmatch(ent
->pev
.group
, subsys_glob
))
2681 !strglobmatch(ent
->pev
.event
, event_glob
))
2683 ret
= asprintf(&buf
, "%s:%s@%s", ent
->pev
.group
,
2684 ent
->pev
.event
, nd
->s
);
2686 strlist__add(sdtlist
, buf
);
2688 probe_cache__delete(pcache
);
2690 strlist__delete(bidlist
);
2692 strlist__for_each_entry(nd
, sdtlist
) {
2693 buf
= strchr(nd
->s
, '@');
2697 printf("%s ", nd
->s
);
2700 nd2
= strlist__next(nd
);
2702 ptr
= strchr(nd2
->s
, '@');
2705 if (strcmp(nd
->s
, nd2
->s
) == 0)
2709 path
= build_id_cache__origname(buf
);
2710 ret
= asprintf(&buf
, "%s@%s(%.12s)", nd
->s
, path
, buf
);
2712 printf(" %-50s [%s]\n", buf
, "SDT event");
2717 printf(" %-50s [%s]\n", nd
->s
, "SDT event");
2719 if (strcmp(nd
->s
, nd2
->s
) != 0)
2720 show_detail
= false;
2725 strlist__delete(sdtlist
);
2728 int print_hwcache_events(const char *event_glob
, bool name_only
)
2730 unsigned int type
, op
, i
, evt_i
= 0, evt_num
= 0;
2732 char **evt_list
= NULL
;
2733 bool evt_num_known
= false;
2736 if (evt_num_known
) {
2737 evt_list
= zalloc(sizeof(char *) * evt_num
);
2742 for (type
= 0; type
< PERF_COUNT_HW_CACHE_MAX
; type
++) {
2743 for (op
= 0; op
< PERF_COUNT_HW_CACHE_OP_MAX
; op
++) {
2744 /* skip invalid cache type */
2745 if (!evsel__is_cache_op_valid(type
, op
))
2748 for (i
= 0; i
< PERF_COUNT_HW_CACHE_RESULT_MAX
; i
++) {
2749 __evsel__hw_cache_type_op_res_name(type
, op
, i
, name
, sizeof(name
));
2750 if (event_glob
!= NULL
&& !strglobmatch(name
, event_glob
))
2753 if (!is_event_supported(PERF_TYPE_HW_CACHE
,
2754 type
| (op
<< 8) | (i
<< 16)))
2757 if (!evt_num_known
) {
2762 evt_list
[evt_i
] = strdup(name
);
2763 if (evt_list
[evt_i
] == NULL
)
2770 if (!evt_num_known
) {
2771 evt_num_known
= true;
2774 qsort(evt_list
, evt_num
, sizeof(char *), cmp_string
);
2776 while (evt_i
< evt_num
) {
2778 printf("%s ", evt_list
[evt_i
++]);
2781 printf(" %-50s [%s]\n", evt_list
[evt_i
++],
2782 event_type_descriptors
[PERF_TYPE_HW_CACHE
]);
2784 if (evt_num
&& pager_in_use())
2789 for (evt_i
= 0; evt_i
< evt_num
; evt_i
++)
2790 zfree(&evt_list
[evt_i
]);
2795 printf("FATAL: not enough memory to print %s\n", event_type_descriptors
[PERF_TYPE_HW_CACHE
]);
2801 static void print_tool_event(const char *name
, const char *event_glob
,
2804 if (event_glob
&& !strglobmatch(name
, event_glob
))
2807 printf("%s ", name
);
2809 printf(" %-50s [%s]\n", name
, "Tool event");
2813 void print_tool_events(const char *event_glob
, bool name_only
)
2815 print_tool_event("duration_time", event_glob
, name_only
);
2820 void print_symbol_events(const char *event_glob
, unsigned type
,
2821 struct event_symbol
*syms
, unsigned max
,
2824 unsigned int i
, evt_i
= 0, evt_num
= 0;
2825 char name
[MAX_NAME_LEN
];
2826 char **evt_list
= NULL
;
2827 bool evt_num_known
= false;
2830 if (evt_num_known
) {
2831 evt_list
= zalloc(sizeof(char *) * evt_num
);
2837 for (i
= 0; i
< max
; i
++, syms
++) {
2839 if (event_glob
!= NULL
&& syms
->symbol
!= NULL
&&
2840 !(strglobmatch(syms
->symbol
, event_glob
) ||
2841 (syms
->alias
&& strglobmatch(syms
->alias
, event_glob
))))
2844 if (!is_event_supported(type
, i
))
2847 if (!evt_num_known
) {
2852 if (!name_only
&& strlen(syms
->alias
))
2853 snprintf(name
, MAX_NAME_LEN
, "%s OR %s", syms
->symbol
, syms
->alias
);
2855 strlcpy(name
, syms
->symbol
, MAX_NAME_LEN
);
2857 evt_list
[evt_i
] = strdup(name
);
2858 if (evt_list
[evt_i
] == NULL
)
2863 if (!evt_num_known
) {
2864 evt_num_known
= true;
2867 qsort(evt_list
, evt_num
, sizeof(char *), cmp_string
);
2869 while (evt_i
< evt_num
) {
2871 printf("%s ", evt_list
[evt_i
++]);
2874 printf(" %-50s [%s]\n", evt_list
[evt_i
++], event_type_descriptors
[type
]);
2876 if (evt_num
&& pager_in_use())
2881 for (evt_i
= 0; evt_i
< evt_num
; evt_i
++)
2882 zfree(&evt_list
[evt_i
]);
2887 printf("FATAL: not enough memory to print %s\n", event_type_descriptors
[type
]);
2893 * Print the help text for the event symbols:
2895 void print_events(const char *event_glob
, bool name_only
, bool quiet_flag
,
2896 bool long_desc
, bool details_flag
, bool deprecated
)
2898 print_symbol_events(event_glob
, PERF_TYPE_HARDWARE
,
2899 event_symbols_hw
, PERF_COUNT_HW_MAX
, name_only
);
2901 print_symbol_events(event_glob
, PERF_TYPE_SOFTWARE
,
2902 event_symbols_sw
, PERF_COUNT_SW_MAX
, name_only
);
2903 print_tool_events(event_glob
, name_only
);
2905 print_hwcache_events(event_glob
, name_only
);
2907 print_pmu_events(event_glob
, name_only
, quiet_flag
, long_desc
,
2908 details_flag
, deprecated
);
2910 if (event_glob
!= NULL
)
2914 printf(" %-50s [%s]\n",
2916 event_type_descriptors
[PERF_TYPE_RAW
]);
2917 printf(" %-50s [%s]\n",
2918 "cpu/t1=v1[,t2=v2,t3 ...]/modifier",
2919 event_type_descriptors
[PERF_TYPE_RAW
]);
2921 printf(" (see 'man perf-list' on how to encode it)\n\n");
2923 printf(" %-50s [%s]\n",
2924 "mem:<addr>[/len][:access]",
2925 event_type_descriptors
[PERF_TYPE_BREAKPOINT
]);
2930 print_tracepoint_events(NULL
, NULL
, name_only
);
2932 print_sdt_events(NULL
, NULL
, name_only
);
2934 metricgroup__print(true, true, NULL
, name_only
, details_flag
);
2936 print_libpfm_events(name_only
, long_desc
);
2939 int parse_events__is_hardcoded_term(struct parse_events_term
*term
)
2941 return term
->type_term
!= PARSE_EVENTS__TERM_TYPE_USER
;
2944 static int new_term(struct parse_events_term
**_term
,
2945 struct parse_events_term
*temp
,
2948 struct parse_events_term
*term
;
2950 term
= malloc(sizeof(*term
));
2955 INIT_LIST_HEAD(&term
->list
);
2958 switch (term
->type_val
) {
2959 case PARSE_EVENTS__TERM_TYPE_NUM
:
2960 term
->val
.num
= num
;
2962 case PARSE_EVENTS__TERM_TYPE_STR
:
2963 term
->val
.str
= str
;
2974 int parse_events_term__num(struct parse_events_term
**term
,
2975 int type_term
, char *config
, u64 num
,
2977 void *loc_term_
, void *loc_val_
)
2979 YYLTYPE
*loc_term
= loc_term_
;
2980 YYLTYPE
*loc_val
= loc_val_
;
2982 struct parse_events_term temp
= {
2983 .type_val
= PARSE_EVENTS__TERM_TYPE_NUM
,
2984 .type_term
= type_term
,
2986 .no_value
= no_value
,
2987 .err_term
= loc_term
? loc_term
->first_column
: 0,
2988 .err_val
= loc_val
? loc_val
->first_column
: 0,
2991 return new_term(term
, &temp
, NULL
, num
);
2994 int parse_events_term__str(struct parse_events_term
**term
,
2995 int type_term
, char *config
, char *str
,
2996 void *loc_term_
, void *loc_val_
)
2998 YYLTYPE
*loc_term
= loc_term_
;
2999 YYLTYPE
*loc_val
= loc_val_
;
3001 struct parse_events_term temp
= {
3002 .type_val
= PARSE_EVENTS__TERM_TYPE_STR
,
3003 .type_term
= type_term
,
3005 .err_term
= loc_term
? loc_term
->first_column
: 0,
3006 .err_val
= loc_val
? loc_val
->first_column
: 0,
3009 return new_term(term
, &temp
, str
, 0);
3012 int parse_events_term__sym_hw(struct parse_events_term
**term
,
3013 char *config
, unsigned idx
)
3015 struct event_symbol
*sym
;
3017 struct parse_events_term temp
= {
3018 .type_val
= PARSE_EVENTS__TERM_TYPE_STR
,
3019 .type_term
= PARSE_EVENTS__TERM_TYPE_USER
,
3024 temp
.config
= strdup("event");
3028 BUG_ON(idx
>= PERF_COUNT_HW_MAX
);
3029 sym
= &event_symbols_hw
[idx
];
3031 str
= strdup(sym
->symbol
);
3034 return new_term(term
, &temp
, str
, 0);
3037 int parse_events_term__clone(struct parse_events_term
**new,
3038 struct parse_events_term
*term
)
3041 struct parse_events_term temp
= {
3042 .type_val
= term
->type_val
,
3043 .type_term
= term
->type_term
,
3045 .err_term
= term
->err_term
,
3046 .err_val
= term
->err_val
,
3050 temp
.config
= strdup(term
->config
);
3054 if (term
->type_val
== PARSE_EVENTS__TERM_TYPE_NUM
)
3055 return new_term(new, &temp
, NULL
, term
->val
.num
);
3057 str
= strdup(term
->val
.str
);
3060 return new_term(new, &temp
, str
, 0);
3063 void parse_events_term__delete(struct parse_events_term
*term
)
3065 if (term
->array
.nr_ranges
)
3066 zfree(&term
->array
.ranges
);
3068 if (term
->type_val
!= PARSE_EVENTS__TERM_TYPE_NUM
)
3069 zfree(&term
->val
.str
);
3071 zfree(&term
->config
);
3075 int parse_events_copy_term_list(struct list_head
*old
,
3076 struct list_head
**new)
3078 struct parse_events_term
*term
, *n
;
3086 *new = malloc(sizeof(struct list_head
));
3089 INIT_LIST_HEAD(*new);
3091 list_for_each_entry (term
, old
, list
) {
3092 ret
= parse_events_term__clone(&n
, term
);
3095 list_add_tail(&n
->list
, *new);
3100 void parse_events_terms__purge(struct list_head
*terms
)
3102 struct parse_events_term
*term
, *h
;
3104 list_for_each_entry_safe(term
, h
, terms
, list
) {
3105 list_del_init(&term
->list
);
3106 parse_events_term__delete(term
);
3110 void parse_events_terms__delete(struct list_head
*terms
)
3114 parse_events_terms__purge(terms
);
3118 void parse_events__clear_array(struct parse_events_array
*a
)
3123 void parse_events_evlist_error(struct parse_events_state
*parse_state
,
3124 int idx
, const char *str
)
3126 if (!parse_state
->error
)
3129 parse_events__handle_error(parse_state
->error
, idx
, strdup(str
), NULL
);
3132 static void config_terms_list(char *buf
, size_t buf_sz
)
3138 for (i
= 0; i
< __PARSE_EVENTS__TERM_TYPE_NR
; i
++) {
3139 const char *name
= config_term_names
[i
];
3141 if (!config_term_avail(i
, NULL
))
3148 if (strlen(buf
) + strlen(name
) + 2 >= buf_sz
)
3160 * Return string contains valid config terms of an event.
3161 * @additional_terms: For terms such as PMU sysfs terms.
3163 char *parse_events_formats_error_string(char *additional_terms
)
3166 /* "no-overwrite" is the longest name */
3167 char static_terms
[__PARSE_EVENTS__TERM_TYPE_NR
*
3168 (sizeof("no-overwrite") - 1)];
3170 config_terms_list(static_terms
, sizeof(static_terms
));
3172 if (additional_terms
) {
3173 if (asprintf(&str
, "valid terms: %s,%s",
3174 additional_terms
, static_terms
) < 0)
3177 if (asprintf(&str
, "valid terms: %s", static_terms
) < 0)