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 int
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"
38 #define MAX_NAME_LEN 100
41 extern int parse_events_debug
;
43 int parse_events_parse(void *parse_state
, void *scanner
);
44 static int get_config_terms(struct list_head
*head_config
,
45 struct list_head
*head_terms __maybe_unused
);
47 static struct perf_pmu_event_symbol
*perf_pmu_events_list
;
49 * The variable indicates the number of supported pmu event symbols.
50 * 0 means not initialized and ready to init
51 * -1 means failed to init, don't try anymore
52 * >0 is the number of supported pmu event symbols
54 static int perf_pmu_events_list_num
;
56 struct event_symbol event_symbols_hw
[PERF_COUNT_HW_MAX
] = {
57 [PERF_COUNT_HW_CPU_CYCLES
] = {
58 .symbol
= "cpu-cycles",
61 [PERF_COUNT_HW_INSTRUCTIONS
] = {
62 .symbol
= "instructions",
65 [PERF_COUNT_HW_CACHE_REFERENCES
] = {
66 .symbol
= "cache-references",
69 [PERF_COUNT_HW_CACHE_MISSES
] = {
70 .symbol
= "cache-misses",
73 [PERF_COUNT_HW_BRANCH_INSTRUCTIONS
] = {
74 .symbol
= "branch-instructions",
77 [PERF_COUNT_HW_BRANCH_MISSES
] = {
78 .symbol
= "branch-misses",
81 [PERF_COUNT_HW_BUS_CYCLES
] = {
82 .symbol
= "bus-cycles",
85 [PERF_COUNT_HW_STALLED_CYCLES_FRONTEND
] = {
86 .symbol
= "stalled-cycles-frontend",
87 .alias
= "idle-cycles-frontend",
89 [PERF_COUNT_HW_STALLED_CYCLES_BACKEND
] = {
90 .symbol
= "stalled-cycles-backend",
91 .alias
= "idle-cycles-backend",
93 [PERF_COUNT_HW_REF_CPU_CYCLES
] = {
94 .symbol
= "ref-cycles",
99 struct event_symbol event_symbols_sw
[PERF_COUNT_SW_MAX
] = {
100 [PERF_COUNT_SW_CPU_CLOCK
] = {
101 .symbol
= "cpu-clock",
104 [PERF_COUNT_SW_TASK_CLOCK
] = {
105 .symbol
= "task-clock",
108 [PERF_COUNT_SW_PAGE_FAULTS
] = {
109 .symbol
= "page-faults",
112 [PERF_COUNT_SW_CONTEXT_SWITCHES
] = {
113 .symbol
= "context-switches",
116 [PERF_COUNT_SW_CPU_MIGRATIONS
] = {
117 .symbol
= "cpu-migrations",
118 .alias
= "migrations",
120 [PERF_COUNT_SW_PAGE_FAULTS_MIN
] = {
121 .symbol
= "minor-faults",
124 [PERF_COUNT_SW_PAGE_FAULTS_MAJ
] = {
125 .symbol
= "major-faults",
128 [PERF_COUNT_SW_ALIGNMENT_FAULTS
] = {
129 .symbol
= "alignment-faults",
132 [PERF_COUNT_SW_EMULATION_FAULTS
] = {
133 .symbol
= "emulation-faults",
136 [PERF_COUNT_SW_DUMMY
] = {
140 [PERF_COUNT_SW_BPF_OUTPUT
] = {
141 .symbol
= "bpf-output",
146 #define __PERF_EVENT_FIELD(config, name) \
147 ((config & PERF_EVENT_##name##_MASK) >> PERF_EVENT_##name##_SHIFT)
149 #define PERF_EVENT_RAW(config) __PERF_EVENT_FIELD(config, RAW)
150 #define PERF_EVENT_CONFIG(config) __PERF_EVENT_FIELD(config, CONFIG)
151 #define PERF_EVENT_TYPE(config) __PERF_EVENT_FIELD(config, TYPE)
152 #define PERF_EVENT_ID(config) __PERF_EVENT_FIELD(config, EVENT)
154 #define for_each_subsystem(sys_dir, sys_dirent) \
155 while ((sys_dirent = readdir(sys_dir)) != NULL) \
156 if (sys_dirent->d_type == DT_DIR && \
157 (strcmp(sys_dirent->d_name, ".")) && \
158 (strcmp(sys_dirent->d_name, "..")))
160 static int tp_event_has_id(const char *dir_path
, struct dirent
*evt_dir
)
162 char evt_path
[MAXPATHLEN
];
165 snprintf(evt_path
, MAXPATHLEN
, "%s/%s/id", dir_path
, evt_dir
->d_name
);
166 fd
= open(evt_path
, O_RDONLY
);
174 #define for_each_event(dir_path, evt_dir, evt_dirent) \
175 while ((evt_dirent = readdir(evt_dir)) != NULL) \
176 if (evt_dirent->d_type == DT_DIR && \
177 (strcmp(evt_dirent->d_name, ".")) && \
178 (strcmp(evt_dirent->d_name, "..")) && \
179 (!tp_event_has_id(dir_path, evt_dirent)))
181 #define MAX_EVENT_LENGTH 512
184 struct tracepoint_path
*tracepoint_id_to_path(u64 config
)
186 struct tracepoint_path
*path
= NULL
;
187 DIR *sys_dir
, *evt_dir
;
188 struct dirent
*sys_dirent
, *evt_dirent
;
192 char evt_path
[MAXPATHLEN
];
195 sys_dir
= tracing_events__opendir();
199 for_each_subsystem(sys_dir
, sys_dirent
) {
200 dir_path
= get_events_file(sys_dirent
->d_name
);
203 evt_dir
= opendir(dir_path
);
207 for_each_event(dir_path
, evt_dir
, evt_dirent
) {
209 scnprintf(evt_path
, MAXPATHLEN
, "%s/%s/id", dir_path
,
211 fd
= open(evt_path
, O_RDONLY
);
214 if (read(fd
, id_buf
, sizeof(id_buf
)) < 0) {
221 put_events_file(dir_path
);
224 path
= zalloc(sizeof(*path
));
227 path
->system
= malloc(MAX_EVENT_LENGTH
);
232 path
->name
= malloc(MAX_EVENT_LENGTH
);
234 zfree(&path
->system
);
238 strncpy(path
->system
, sys_dirent
->d_name
,
240 strncpy(path
->name
, evt_dirent
->d_name
,
247 put_events_file(dir_path
);
254 struct tracepoint_path
*tracepoint_name_to_path(const char *name
)
256 struct tracepoint_path
*path
= zalloc(sizeof(*path
));
257 char *str
= strchr(name
, ':');
259 if (path
== NULL
|| str
== NULL
) {
264 path
->system
= strndup(name
, str
- name
);
265 path
->name
= strdup(str
+1);
267 if (path
->system
== NULL
|| path
->name
== NULL
) {
268 zfree(&path
->system
);
276 const char *event_type(int type
)
279 case PERF_TYPE_HARDWARE
:
282 case PERF_TYPE_SOFTWARE
:
285 case PERF_TYPE_TRACEPOINT
:
288 case PERF_TYPE_HW_CACHE
:
289 return "hardware-cache";
298 static int parse_events__is_name_term(struct parse_events_term
*term
)
300 return term
->type_term
== PARSE_EVENTS__TERM_TYPE_NAME
;
303 static char *get_config_name(struct list_head
*head_terms
)
305 struct parse_events_term
*term
;
310 list_for_each_entry(term
, head_terms
, list
)
311 if (parse_events__is_name_term(term
))
312 return term
->val
.str
;
317 static struct evsel
*
318 __add_event(struct list_head
*list
, int *idx
,
319 struct perf_event_attr
*attr
,
320 char *name
, struct perf_pmu
*pmu
,
321 struct list_head
*config_terms
, bool auto_merge_stats
,
322 const char *cpu_list
)
325 struct perf_cpu_map
*cpus
= pmu
? pmu
->cpus
:
326 cpu_list
? perf_cpu_map__new(cpu_list
) : NULL
;
328 event_attr_init(attr
);
330 evsel
= perf_evsel__new_idx(attr
, *idx
);
335 evsel
->core
.cpus
= perf_cpu_map__get(cpus
);
336 evsel
->core
.own_cpus
= perf_cpu_map__get(cpus
);
337 evsel
->system_wide
= pmu
? pmu
->is_uncore
: false;
338 evsel
->auto_merge_stats
= auto_merge_stats
;
341 evsel
->name
= strdup(name
);
344 list_splice(config_terms
, &evsel
->config_terms
);
346 list_add_tail(&evsel
->core
.node
, list
);
350 static int add_event(struct list_head
*list
, int *idx
,
351 struct perf_event_attr
*attr
, char *name
,
352 struct list_head
*config_terms
)
354 return __add_event(list
, idx
, attr
, name
, NULL
, config_terms
, false, NULL
) ? 0 : -ENOMEM
;
357 static int add_event_tool(struct list_head
*list
, int *idx
,
358 enum perf_tool_event tool_event
)
361 struct perf_event_attr attr
= {
362 .type
= PERF_TYPE_SOFTWARE
,
363 .config
= PERF_COUNT_SW_DUMMY
,
366 evsel
= __add_event(list
, idx
, &attr
, NULL
, NULL
, NULL
, false, "0");
369 evsel
->tool_event
= tool_event
;
370 if (tool_event
== PERF_TOOL_DURATION_TIME
)
371 evsel
->unit
= strdup("ns");
375 static int parse_aliases(char *str
, const char *names
[][PERF_EVSEL__MAX_ALIASES
], int size
)
380 for (i
= 0; i
< size
; i
++) {
381 for (j
= 0; j
< PERF_EVSEL__MAX_ALIASES
&& names
[i
][j
]; j
++) {
382 n
= strlen(names
[i
][j
]);
383 if (n
> longest
&& !strncasecmp(str
, names
[i
][j
], n
))
393 typedef int config_term_func_t(struct perf_event_attr
*attr
,
394 struct parse_events_term
*term
,
395 struct parse_events_error
*err
);
396 static int config_term_common(struct perf_event_attr
*attr
,
397 struct parse_events_term
*term
,
398 struct parse_events_error
*err
);
399 static int config_attr(struct perf_event_attr
*attr
,
400 struct list_head
*head
,
401 struct parse_events_error
*err
,
402 config_term_func_t config_term
);
404 int parse_events_add_cache(struct list_head
*list
, int *idx
,
405 char *type
, char *op_result1
, char *op_result2
,
406 struct parse_events_error
*err
,
407 struct list_head
*head_config
)
409 struct perf_event_attr attr
;
410 LIST_HEAD(config_terms
);
411 char name
[MAX_NAME_LEN
], *config_name
;
412 int cache_type
= -1, cache_op
= -1, cache_result
= -1;
413 char *op_result
[2] = { op_result1
, op_result2
};
417 * No fallback - if we cannot get a clear cache type
420 cache_type
= parse_aliases(type
, perf_evsel__hw_cache
,
421 PERF_COUNT_HW_CACHE_MAX
);
422 if (cache_type
== -1)
425 config_name
= get_config_name(head_config
);
426 n
= snprintf(name
, MAX_NAME_LEN
, "%s", type
);
428 for (i
= 0; (i
< 2) && (op_result
[i
]); i
++) {
429 char *str
= op_result
[i
];
431 n
+= snprintf(name
+ n
, MAX_NAME_LEN
- n
, "-%s", str
);
433 if (cache_op
== -1) {
434 cache_op
= parse_aliases(str
, perf_evsel__hw_cache_op
,
435 PERF_COUNT_HW_CACHE_OP_MAX
);
437 if (!perf_evsel__is_cache_op_valid(cache_type
, cache_op
))
443 if (cache_result
== -1) {
444 cache_result
= parse_aliases(str
, perf_evsel__hw_cache_result
,
445 PERF_COUNT_HW_CACHE_RESULT_MAX
);
446 if (cache_result
>= 0)
452 * Fall back to reads:
455 cache_op
= PERF_COUNT_HW_CACHE_OP_READ
;
458 * Fall back to accesses:
460 if (cache_result
== -1)
461 cache_result
= PERF_COUNT_HW_CACHE_RESULT_ACCESS
;
463 memset(&attr
, 0, sizeof(attr
));
464 attr
.config
= cache_type
| (cache_op
<< 8) | (cache_result
<< 16);
465 attr
.type
= PERF_TYPE_HW_CACHE
;
468 if (config_attr(&attr
, head_config
, err
,
472 if (get_config_terms(head_config
, &config_terms
))
475 return add_event(list
, idx
, &attr
, config_name
? : name
, &config_terms
);
478 static void tracepoint_error(struct parse_events_error
*e
, int err
,
479 const char *sys
, const char *name
)
487 * We get error directly from syscall errno ( > 0),
488 * or from encoded pointer's error ( < 0).
494 e
->str
= strdup("can't access trace events");
497 e
->str
= strdup("unknown tracepoint");
500 e
->str
= strdup("failed to add tracepoint");
504 tracing_path__strerror_open_tp(err
, help
, sizeof(help
), sys
, name
);
505 e
->help
= strdup(help
);
508 static int add_tracepoint(struct list_head
*list
, int *idx
,
509 const char *sys_name
, const char *evt_name
,
510 struct parse_events_error
*err
,
511 struct list_head
*head_config
)
515 evsel
= perf_evsel__newtp_idx(sys_name
, evt_name
, (*idx
)++);
517 tracepoint_error(err
, PTR_ERR(evsel
), sys_name
, evt_name
);
518 return PTR_ERR(evsel
);
522 LIST_HEAD(config_terms
);
524 if (get_config_terms(head_config
, &config_terms
))
526 list_splice(&config_terms
, &evsel
->config_terms
);
529 list_add_tail(&evsel
->core
.node
, list
);
533 static int add_tracepoint_multi_event(struct list_head
*list
, int *idx
,
534 const char *sys_name
, const char *evt_name
,
535 struct parse_events_error
*err
,
536 struct list_head
*head_config
)
539 struct dirent
*evt_ent
;
541 int ret
= 0, found
= 0;
543 evt_path
= get_events_file(sys_name
);
545 tracepoint_error(err
, errno
, sys_name
, evt_name
);
548 evt_dir
= opendir(evt_path
);
550 put_events_file(evt_path
);
551 tracepoint_error(err
, errno
, sys_name
, evt_name
);
555 while (!ret
&& (evt_ent
= readdir(evt_dir
))) {
556 if (!strcmp(evt_ent
->d_name
, ".")
557 || !strcmp(evt_ent
->d_name
, "..")
558 || !strcmp(evt_ent
->d_name
, "enable")
559 || !strcmp(evt_ent
->d_name
, "filter"))
562 if (!strglobmatch(evt_ent
->d_name
, evt_name
))
567 ret
= add_tracepoint(list
, idx
, sys_name
, evt_ent
->d_name
,
572 tracepoint_error(err
, ENOENT
, sys_name
, evt_name
);
576 put_events_file(evt_path
);
581 static int add_tracepoint_event(struct list_head
*list
, int *idx
,
582 const char *sys_name
, const char *evt_name
,
583 struct parse_events_error
*err
,
584 struct list_head
*head_config
)
586 return strpbrk(evt_name
, "*?") ?
587 add_tracepoint_multi_event(list
, idx
, sys_name
, evt_name
,
589 add_tracepoint(list
, idx
, sys_name
, evt_name
,
593 static int add_tracepoint_multi_sys(struct list_head
*list
, int *idx
,
594 const char *sys_name
, const char *evt_name
,
595 struct parse_events_error
*err
,
596 struct list_head
*head_config
)
598 struct dirent
*events_ent
;
602 events_dir
= tracing_events__opendir();
604 tracepoint_error(err
, errno
, sys_name
, evt_name
);
608 while (!ret
&& (events_ent
= readdir(events_dir
))) {
609 if (!strcmp(events_ent
->d_name
, ".")
610 || !strcmp(events_ent
->d_name
, "..")
611 || !strcmp(events_ent
->d_name
, "enable")
612 || !strcmp(events_ent
->d_name
, "header_event")
613 || !strcmp(events_ent
->d_name
, "header_page"))
616 if (!strglobmatch(events_ent
->d_name
, sys_name
))
619 ret
= add_tracepoint_event(list
, idx
, events_ent
->d_name
,
620 evt_name
, err
, head_config
);
623 closedir(events_dir
);
627 struct __add_bpf_event_param
{
628 struct parse_events_state
*parse_state
;
629 struct list_head
*list
;
630 struct list_head
*head_config
;
633 static int add_bpf_event(const char *group
, const char *event
, int fd
, struct bpf_object
*obj
,
636 LIST_HEAD(new_evsels
);
637 struct __add_bpf_event_param
*param
= _param
;
638 struct parse_events_state
*parse_state
= param
->parse_state
;
639 struct list_head
*list
= param
->list
;
643 * Check if we should add the event, i.e. if it is a TP but starts with a '!',
644 * then don't add the tracepoint, this will be used for something else, like
645 * adding to a BPF_MAP_TYPE_PROG_ARRAY.
647 * See tools/perf/examples/bpf/augmented_raw_syscalls.c
652 pr_debug("add bpf event %s:%s and attach bpf program %d\n",
655 err
= parse_events_add_tracepoint(&new_evsels
, &parse_state
->idx
, group
,
656 event
, parse_state
->error
,
659 struct evsel
*evsel
, *tmp
;
661 pr_debug("Failed to add BPF event %s:%s\n",
663 list_for_each_entry_safe(evsel
, tmp
, &new_evsels
, core
.node
) {
664 list_del_init(&evsel
->core
.node
);
665 evsel__delete(evsel
);
669 pr_debug("adding %s:%s\n", group
, event
);
671 list_for_each_entry(pos
, &new_evsels
, core
.node
) {
672 pr_debug("adding %s:%s to %p\n",
677 list_splice(&new_evsels
, list
);
681 int parse_events_load_bpf_obj(struct parse_events_state
*parse_state
,
682 struct list_head
*list
,
683 struct bpf_object
*obj
,
684 struct list_head
*head_config
)
688 struct __add_bpf_event_param param
= {parse_state
, list
, head_config
};
689 static bool registered_unprobe_atexit
= false;
691 if (IS_ERR(obj
) || !obj
) {
692 snprintf(errbuf
, sizeof(errbuf
),
693 "Internal error: load bpf obj with NULL");
699 * Register atexit handler before calling bpf__probe() so
700 * bpf__probe() don't need to unprobe probe points its already
701 * created when failure.
703 if (!registered_unprobe_atexit
) {
705 registered_unprobe_atexit
= true;
708 err
= bpf__probe(obj
);
710 bpf__strerror_probe(obj
, err
, errbuf
, sizeof(errbuf
));
714 err
= bpf__load(obj
);
716 bpf__strerror_load(obj
, err
, errbuf
, sizeof(errbuf
));
720 err
= bpf__foreach_event(obj
, add_bpf_event
, ¶m
);
722 snprintf(errbuf
, sizeof(errbuf
),
723 "Attach events in BPF object failed");
729 parse_state
->error
->help
= strdup("(add -v to see detail)");
730 parse_state
->error
->str
= strdup(errbuf
);
735 parse_events_config_bpf(struct parse_events_state
*parse_state
,
736 struct bpf_object
*obj
,
737 struct list_head
*head_config
)
739 struct parse_events_term
*term
;
742 if (!head_config
|| list_empty(head_config
))
745 list_for_each_entry(term
, head_config
, list
) {
749 if (term
->type_term
!= PARSE_EVENTS__TERM_TYPE_USER
) {
750 snprintf(errbuf
, sizeof(errbuf
),
751 "Invalid config term for BPF object");
752 errbuf
[BUFSIZ
- 1] = '\0';
754 parse_state
->error
->idx
= term
->err_term
;
755 parse_state
->error
->str
= strdup(errbuf
);
759 err
= bpf__config_obj(obj
, term
, parse_state
->evlist
, &error_pos
);
761 bpf__strerror_config_obj(obj
, term
, parse_state
->evlist
,
762 &error_pos
, err
, errbuf
,
764 parse_state
->error
->help
= strdup(
765 "Hint:\tValid config terms:\n"
766 " \tmap:[<arraymap>].value<indices>=[value]\n"
767 " \tmap:[<eventmap>].event<indices>=[event]\n"
769 " \twhere <indices> is something like [0,3...5] or [all]\n"
770 " \t(add -v to see detail)");
771 parse_state
->error
->str
= strdup(errbuf
);
772 if (err
== -BPF_LOADER_ERRNO__OBJCONF_MAP_VALUE
)
773 parse_state
->error
->idx
= term
->err_val
;
775 parse_state
->error
->idx
= term
->err_term
+ error_pos
;
783 * Split config terms:
784 * perf record -e bpf.c/call-graph=fp,map:array.value[0]=1/ ...
785 * 'call-graph=fp' is 'evt config', should be applied to each
787 * 'map:array.value[0]=1' is 'obj config', should be processed
788 * with parse_events_config_bpf.
790 * Move object config terms from the first list to obj_head_config.
793 split_bpf_config_terms(struct list_head
*evt_head_config
,
794 struct list_head
*obj_head_config
)
796 struct parse_events_term
*term
, *temp
;
799 * Currectly, all possible user config term
800 * belong to bpf object. parse_events__is_hardcoded_term()
801 * happends to be a good flag.
803 * See parse_events_config_bpf() and
804 * config_term_tracepoint().
806 list_for_each_entry_safe(term
, temp
, evt_head_config
, list
)
807 if (!parse_events__is_hardcoded_term(term
))
808 list_move_tail(&term
->list
, obj_head_config
);
811 int parse_events_load_bpf(struct parse_events_state
*parse_state
,
812 struct list_head
*list
,
815 struct list_head
*head_config
)
818 struct bpf_object
*obj
;
819 LIST_HEAD(obj_head_config
);
822 split_bpf_config_terms(head_config
, &obj_head_config
);
824 obj
= bpf__prepare_load(bpf_file_name
, source
);
831 snprintf(errbuf
, sizeof(errbuf
),
832 "BPF support is not compiled");
834 bpf__strerror_prepare_load(bpf_file_name
,
839 parse_state
->error
->help
= strdup("(add -v to see detail)");
840 parse_state
->error
->str
= strdup(errbuf
);
844 err
= parse_events_load_bpf_obj(parse_state
, list
, obj
, head_config
);
847 err
= parse_events_config_bpf(parse_state
, obj
, &obj_head_config
);
850 * Caller doesn't know anything about obj_head_config,
851 * so combine them together again before returnning.
854 list_splice_tail(&obj_head_config
, head_config
);
859 parse_breakpoint_type(const char *type
, struct perf_event_attr
*attr
)
863 for (i
= 0; i
< 3; i
++) {
864 if (!type
|| !type
[i
])
867 #define CHECK_SET_TYPE(bit) \
869 if (attr->bp_type & bit) \
872 attr->bp_type |= bit; \
877 CHECK_SET_TYPE(HW_BREAKPOINT_R
);
880 CHECK_SET_TYPE(HW_BREAKPOINT_W
);
883 CHECK_SET_TYPE(HW_BREAKPOINT_X
);
890 #undef CHECK_SET_TYPE
892 if (!attr
->bp_type
) /* Default */
893 attr
->bp_type
= HW_BREAKPOINT_R
| HW_BREAKPOINT_W
;
898 int parse_events_add_breakpoint(struct list_head
*list
, int *idx
,
899 void *ptr
, char *type
, u64 len
)
901 struct perf_event_attr attr
;
903 memset(&attr
, 0, sizeof(attr
));
904 attr
.bp_addr
= (unsigned long) ptr
;
906 if (parse_breakpoint_type(type
, &attr
))
909 /* Provide some defaults if len is not specified */
911 if (attr
.bp_type
== HW_BREAKPOINT_X
)
914 len
= HW_BREAKPOINT_LEN_4
;
919 attr
.type
= PERF_TYPE_BREAKPOINT
;
920 attr
.sample_period
= 1;
922 return add_event(list
, idx
, &attr
, NULL
, NULL
);
925 static int check_type_val(struct parse_events_term
*term
,
926 struct parse_events_error
*err
,
929 if (type
== term
->type_val
)
933 err
->idx
= term
->err_val
;
934 if (type
== PARSE_EVENTS__TERM_TYPE_NUM
)
935 err
->str
= strdup("expected numeric value");
937 err
->str
= strdup("expected string value");
943 * Update according to parse-events.l
945 static const char *config_term_names
[__PARSE_EVENTS__TERM_TYPE_NR
] = {
946 [PARSE_EVENTS__TERM_TYPE_USER
] = "<sysfs term>",
947 [PARSE_EVENTS__TERM_TYPE_CONFIG
] = "config",
948 [PARSE_EVENTS__TERM_TYPE_CONFIG1
] = "config1",
949 [PARSE_EVENTS__TERM_TYPE_CONFIG2
] = "config2",
950 [PARSE_EVENTS__TERM_TYPE_NAME
] = "name",
951 [PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD
] = "period",
952 [PARSE_EVENTS__TERM_TYPE_SAMPLE_FREQ
] = "freq",
953 [PARSE_EVENTS__TERM_TYPE_BRANCH_SAMPLE_TYPE
] = "branch_type",
954 [PARSE_EVENTS__TERM_TYPE_TIME
] = "time",
955 [PARSE_EVENTS__TERM_TYPE_CALLGRAPH
] = "call-graph",
956 [PARSE_EVENTS__TERM_TYPE_STACKSIZE
] = "stack-size",
957 [PARSE_EVENTS__TERM_TYPE_NOINHERIT
] = "no-inherit",
958 [PARSE_EVENTS__TERM_TYPE_INHERIT
] = "inherit",
959 [PARSE_EVENTS__TERM_TYPE_MAX_STACK
] = "max-stack",
960 [PARSE_EVENTS__TERM_TYPE_MAX_EVENTS
] = "nr",
961 [PARSE_EVENTS__TERM_TYPE_OVERWRITE
] = "overwrite",
962 [PARSE_EVENTS__TERM_TYPE_NOOVERWRITE
] = "no-overwrite",
963 [PARSE_EVENTS__TERM_TYPE_DRV_CFG
] = "driver-config",
964 [PARSE_EVENTS__TERM_TYPE_PERCORE
] = "percore",
965 [PARSE_EVENTS__TERM_TYPE_AUX_OUTPUT
] = "aux-output",
968 static bool config_term_shrinked
;
971 config_term_avail(int term_type
, struct parse_events_error
*err
)
973 if (term_type
< 0 || term_type
>= __PARSE_EVENTS__TERM_TYPE_NR
) {
974 err
->str
= strdup("Invalid term_type");
977 if (!config_term_shrinked
)
981 case PARSE_EVENTS__TERM_TYPE_CONFIG
:
982 case PARSE_EVENTS__TERM_TYPE_CONFIG1
:
983 case PARSE_EVENTS__TERM_TYPE_CONFIG2
:
984 case PARSE_EVENTS__TERM_TYPE_NAME
:
985 case PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD
:
986 case PARSE_EVENTS__TERM_TYPE_PERCORE
:
992 /* term_type is validated so indexing is safe */
993 if (asprintf(&err
->str
, "'%s' is not usable in 'perf stat'",
994 config_term_names
[term_type
]) < 0)
1000 void parse_events__shrink_config_terms(void)
1002 config_term_shrinked
= true;
1005 static int config_term_common(struct perf_event_attr
*attr
,
1006 struct parse_events_term
*term
,
1007 struct parse_events_error
*err
)
1009 #define CHECK_TYPE_VAL(type) \
1011 if (check_type_val(term, err, PARSE_EVENTS__TERM_TYPE_ ## type)) \
1015 switch (term
->type_term
) {
1016 case PARSE_EVENTS__TERM_TYPE_CONFIG
:
1017 CHECK_TYPE_VAL(NUM
);
1018 attr
->config
= term
->val
.num
;
1020 case PARSE_EVENTS__TERM_TYPE_CONFIG1
:
1021 CHECK_TYPE_VAL(NUM
);
1022 attr
->config1
= term
->val
.num
;
1024 case PARSE_EVENTS__TERM_TYPE_CONFIG2
:
1025 CHECK_TYPE_VAL(NUM
);
1026 attr
->config2
= term
->val
.num
;
1028 case PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD
:
1029 CHECK_TYPE_VAL(NUM
);
1031 case PARSE_EVENTS__TERM_TYPE_SAMPLE_FREQ
:
1032 CHECK_TYPE_VAL(NUM
);
1034 case PARSE_EVENTS__TERM_TYPE_BRANCH_SAMPLE_TYPE
:
1035 CHECK_TYPE_VAL(STR
);
1036 if (strcmp(term
->val
.str
, "no") &&
1037 parse_branch_str(term
->val
.str
, &attr
->branch_sample_type
)) {
1038 err
->str
= strdup("invalid branch sample type");
1039 err
->idx
= term
->err_val
;
1043 case PARSE_EVENTS__TERM_TYPE_TIME
:
1044 CHECK_TYPE_VAL(NUM
);
1045 if (term
->val
.num
> 1) {
1046 err
->str
= strdup("expected 0 or 1");
1047 err
->idx
= term
->err_val
;
1051 case PARSE_EVENTS__TERM_TYPE_CALLGRAPH
:
1052 CHECK_TYPE_VAL(STR
);
1054 case PARSE_EVENTS__TERM_TYPE_STACKSIZE
:
1055 CHECK_TYPE_VAL(NUM
);
1057 case PARSE_EVENTS__TERM_TYPE_INHERIT
:
1058 CHECK_TYPE_VAL(NUM
);
1060 case PARSE_EVENTS__TERM_TYPE_NOINHERIT
:
1061 CHECK_TYPE_VAL(NUM
);
1063 case PARSE_EVENTS__TERM_TYPE_OVERWRITE
:
1064 CHECK_TYPE_VAL(NUM
);
1066 case PARSE_EVENTS__TERM_TYPE_NOOVERWRITE
:
1067 CHECK_TYPE_VAL(NUM
);
1069 case PARSE_EVENTS__TERM_TYPE_NAME
:
1070 CHECK_TYPE_VAL(STR
);
1072 case PARSE_EVENTS__TERM_TYPE_MAX_STACK
:
1073 CHECK_TYPE_VAL(NUM
);
1075 case PARSE_EVENTS__TERM_TYPE_MAX_EVENTS
:
1076 CHECK_TYPE_VAL(NUM
);
1078 case PARSE_EVENTS__TERM_TYPE_PERCORE
:
1079 CHECK_TYPE_VAL(NUM
);
1080 if ((unsigned int)term
->val
.num
> 1) {
1081 err
->str
= strdup("expected 0 or 1");
1082 err
->idx
= term
->err_val
;
1086 case PARSE_EVENTS__TERM_TYPE_AUX_OUTPUT
:
1087 CHECK_TYPE_VAL(NUM
);
1090 err
->str
= strdup("unknown term");
1091 err
->idx
= term
->err_term
;
1092 err
->help
= parse_events_formats_error_string(NULL
);
1097 * Check term availbility after basic checking so
1098 * PARSE_EVENTS__TERM_TYPE_USER can be found and filtered.
1100 * If check availbility at the entry of this function,
1101 * user will see "'<sysfs term>' is not usable in 'perf stat'"
1102 * if an invalid config term is provided for legacy events
1103 * (for example, instructions/badterm/...), which is confusing.
1105 if (!config_term_avail(term
->type_term
, err
))
1108 #undef CHECK_TYPE_VAL
1111 static int config_term_pmu(struct perf_event_attr
*attr
,
1112 struct parse_events_term
*term
,
1113 struct parse_events_error
*err
)
1115 if (term
->type_term
== PARSE_EVENTS__TERM_TYPE_USER
||
1116 term
->type_term
== PARSE_EVENTS__TERM_TYPE_DRV_CFG
)
1118 * Always succeed for sysfs terms, as we dont know
1119 * at this point what type they need to have.
1123 return config_term_common(attr
, term
, err
);
1126 static int config_term_tracepoint(struct perf_event_attr
*attr
,
1127 struct parse_events_term
*term
,
1128 struct parse_events_error
*err
)
1130 switch (term
->type_term
) {
1131 case PARSE_EVENTS__TERM_TYPE_CALLGRAPH
:
1132 case PARSE_EVENTS__TERM_TYPE_STACKSIZE
:
1133 case PARSE_EVENTS__TERM_TYPE_INHERIT
:
1134 case PARSE_EVENTS__TERM_TYPE_NOINHERIT
:
1135 case PARSE_EVENTS__TERM_TYPE_MAX_STACK
:
1136 case PARSE_EVENTS__TERM_TYPE_MAX_EVENTS
:
1137 case PARSE_EVENTS__TERM_TYPE_OVERWRITE
:
1138 case PARSE_EVENTS__TERM_TYPE_NOOVERWRITE
:
1139 case PARSE_EVENTS__TERM_TYPE_AUX_OUTPUT
:
1140 return config_term_common(attr
, term
, err
);
1143 err
->idx
= term
->err_term
;
1144 err
->str
= strdup("unknown term");
1145 err
->help
= strdup("valid terms: call-graph,stack-size\n");
1153 static int config_attr(struct perf_event_attr
*attr
,
1154 struct list_head
*head
,
1155 struct parse_events_error
*err
,
1156 config_term_func_t config_term
)
1158 struct parse_events_term
*term
;
1160 list_for_each_entry(term
, head
, list
)
1161 if (config_term(attr
, term
, err
))
1167 static int get_config_terms(struct list_head
*head_config
,
1168 struct list_head
*head_terms __maybe_unused
)
1170 #define ADD_CONFIG_TERM(__type, __name, __val) \
1172 struct perf_evsel_config_term *__t; \
1174 __t = zalloc(sizeof(*__t)); \
1178 INIT_LIST_HEAD(&__t->list); \
1179 __t->type = PERF_EVSEL__CONFIG_TERM_ ## __type; \
1180 __t->val.__name = __val; \
1181 __t->weak = term->weak; \
1182 list_add_tail(&__t->list, head_terms); \
1185 struct parse_events_term
*term
;
1187 list_for_each_entry(term
, head_config
, list
) {
1188 switch (term
->type_term
) {
1189 case PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD
:
1190 ADD_CONFIG_TERM(PERIOD
, period
, term
->val
.num
);
1192 case PARSE_EVENTS__TERM_TYPE_SAMPLE_FREQ
:
1193 ADD_CONFIG_TERM(FREQ
, freq
, term
->val
.num
);
1195 case PARSE_EVENTS__TERM_TYPE_TIME
:
1196 ADD_CONFIG_TERM(TIME
, time
, term
->val
.num
);
1198 case PARSE_EVENTS__TERM_TYPE_CALLGRAPH
:
1199 ADD_CONFIG_TERM(CALLGRAPH
, callgraph
, term
->val
.str
);
1201 case PARSE_EVENTS__TERM_TYPE_BRANCH_SAMPLE_TYPE
:
1202 ADD_CONFIG_TERM(BRANCH
, branch
, term
->val
.str
);
1204 case PARSE_EVENTS__TERM_TYPE_STACKSIZE
:
1205 ADD_CONFIG_TERM(STACK_USER
, stack_user
, term
->val
.num
);
1207 case PARSE_EVENTS__TERM_TYPE_INHERIT
:
1208 ADD_CONFIG_TERM(INHERIT
, inherit
, term
->val
.num
? 1 : 0);
1210 case PARSE_EVENTS__TERM_TYPE_NOINHERIT
:
1211 ADD_CONFIG_TERM(INHERIT
, inherit
, term
->val
.num
? 0 : 1);
1213 case PARSE_EVENTS__TERM_TYPE_MAX_STACK
:
1214 ADD_CONFIG_TERM(MAX_STACK
, max_stack
, term
->val
.num
);
1216 case PARSE_EVENTS__TERM_TYPE_MAX_EVENTS
:
1217 ADD_CONFIG_TERM(MAX_EVENTS
, max_events
, term
->val
.num
);
1219 case PARSE_EVENTS__TERM_TYPE_OVERWRITE
:
1220 ADD_CONFIG_TERM(OVERWRITE
, overwrite
, term
->val
.num
? 1 : 0);
1222 case PARSE_EVENTS__TERM_TYPE_NOOVERWRITE
:
1223 ADD_CONFIG_TERM(OVERWRITE
, overwrite
, term
->val
.num
? 0 : 1);
1225 case PARSE_EVENTS__TERM_TYPE_DRV_CFG
:
1226 ADD_CONFIG_TERM(DRV_CFG
, drv_cfg
, term
->val
.str
);
1228 case PARSE_EVENTS__TERM_TYPE_PERCORE
:
1229 ADD_CONFIG_TERM(PERCORE
, percore
,
1230 term
->val
.num
? true : false);
1232 case PARSE_EVENTS__TERM_TYPE_AUX_OUTPUT
:
1233 ADD_CONFIG_TERM(AUX_OUTPUT
, aux_output
, term
->val
.num
? 1 : 0);
1239 #undef ADD_EVSEL_CONFIG
1243 int parse_events_add_tracepoint(struct list_head
*list
, int *idx
,
1244 const char *sys
, const char *event
,
1245 struct parse_events_error
*err
,
1246 struct list_head
*head_config
)
1249 struct perf_event_attr attr
;
1251 if (config_attr(&attr
, head_config
, err
,
1252 config_term_tracepoint
))
1256 if (strpbrk(sys
, "*?"))
1257 return add_tracepoint_multi_sys(list
, idx
, sys
, event
,
1260 return add_tracepoint_event(list
, idx
, sys
, event
,
1264 int parse_events_add_numeric(struct parse_events_state
*parse_state
,
1265 struct list_head
*list
,
1266 u32 type
, u64 config
,
1267 struct list_head
*head_config
)
1269 struct perf_event_attr attr
;
1270 LIST_HEAD(config_terms
);
1272 memset(&attr
, 0, sizeof(attr
));
1274 attr
.config
= config
;
1277 if (config_attr(&attr
, head_config
, parse_state
->error
,
1278 config_term_common
))
1281 if (get_config_terms(head_config
, &config_terms
))
1285 return add_event(list
, &parse_state
->idx
, &attr
,
1286 get_config_name(head_config
), &config_terms
);
1289 int parse_events_add_tool(struct parse_events_state
*parse_state
,
1290 struct list_head
*list
,
1291 enum perf_tool_event tool_event
)
1293 return add_event_tool(list
, &parse_state
->idx
, tool_event
);
1296 static bool config_term_percore(struct list_head
*config_terms
)
1298 struct perf_evsel_config_term
*term
;
1300 list_for_each_entry(term
, config_terms
, list
) {
1301 if (term
->type
== PERF_EVSEL__CONFIG_TERM_PERCORE
)
1302 return term
->val
.percore
;
1308 int parse_events_add_pmu(struct parse_events_state
*parse_state
,
1309 struct list_head
*list
, char *name
,
1310 struct list_head
*head_config
,
1311 bool auto_merge_stats
,
1314 struct perf_event_attr attr
;
1315 struct perf_pmu_info info
;
1316 struct perf_pmu
*pmu
;
1317 struct evsel
*evsel
;
1318 struct parse_events_error
*err
= parse_state
->error
;
1319 bool use_uncore_alias
;
1320 LIST_HEAD(config_terms
);
1322 pmu
= perf_pmu__find(name
);
1324 if (asprintf(&err
->str
,
1325 "Cannot find PMU `%s'. Missing kernel support?",
1331 if (pmu
->default_config
) {
1332 memcpy(&attr
, pmu
->default_config
,
1333 sizeof(struct perf_event_attr
));
1335 memset(&attr
, 0, sizeof(attr
));
1338 use_uncore_alias
= (pmu
->is_uncore
&& use_alias
);
1341 attr
.type
= pmu
->type
;
1342 evsel
= __add_event(list
, &parse_state
->idx
, &attr
, NULL
, pmu
, NULL
,
1343 auto_merge_stats
, NULL
);
1345 evsel
->pmu_name
= name
;
1346 evsel
->use_uncore_alias
= use_uncore_alias
;
1353 if (perf_pmu__check_alias(pmu
, head_config
, &info
))
1357 * Configure hardcoded terms first, no need to check
1358 * return value when called with fail == 0 ;)
1360 if (config_attr(&attr
, head_config
, parse_state
->error
, config_term_pmu
))
1363 if (get_config_terms(head_config
, &config_terms
))
1366 if (perf_pmu__config(pmu
, &attr
, head_config
, parse_state
->error
))
1369 evsel
= __add_event(list
, &parse_state
->idx
, &attr
,
1370 get_config_name(head_config
), pmu
,
1371 &config_terms
, auto_merge_stats
, NULL
);
1373 evsel
->unit
= info
.unit
;
1374 evsel
->scale
= info
.scale
;
1375 evsel
->per_pkg
= info
.per_pkg
;
1376 evsel
->snapshot
= info
.snapshot
;
1377 evsel
->metric_expr
= info
.metric_expr
;
1378 evsel
->metric_name
= info
.metric_name
;
1379 evsel
->pmu_name
= name
;
1380 evsel
->use_uncore_alias
= use_uncore_alias
;
1381 evsel
->percore
= config_term_percore(&evsel
->config_terms
);
1384 return evsel
? 0 : -ENOMEM
;
1387 int parse_events_multi_pmu_add(struct parse_events_state
*parse_state
,
1388 char *str
, struct list_head
**listp
)
1390 struct list_head
*head
;
1391 struct parse_events_term
*term
;
1392 struct list_head
*list
;
1393 struct perf_pmu
*pmu
= NULL
;
1397 /* Add it for all PMUs that support the alias */
1398 list
= malloc(sizeof(struct list_head
));
1401 INIT_LIST_HEAD(list
);
1402 while ((pmu
= perf_pmu__scan(pmu
)) != NULL
) {
1403 struct perf_pmu_alias
*alias
;
1405 list_for_each_entry(alias
, &pmu
->aliases
, list
) {
1406 if (!strcasecmp(alias
->name
, str
)) {
1407 head
= malloc(sizeof(struct list_head
));
1410 INIT_LIST_HEAD(head
);
1411 if (parse_events_term__num(&term
, PARSE_EVENTS__TERM_TYPE_USER
,
1412 str
, 1, false, &str
, NULL
) < 0)
1414 list_add_tail(&term
->list
, head
);
1416 if (!parse_events_add_pmu(parse_state
, list
,
1419 pr_debug("%s -> %s/%s/\n", str
,
1420 pmu
->name
, alias
->str
);
1424 parse_events_terms__delete(head
);
1434 int parse_events__modifier_group(struct list_head
*list
,
1437 return parse_events__modifier_event(list
, event_mod
, true);
1441 * Check if the two uncore PMUs are from the same uncore block
1442 * The format of the uncore PMU name is uncore_#blockname_#pmuidx
1444 static bool is_same_uncore_block(const char *pmu_name_a
, const char *pmu_name_b
)
1446 char *end_a
, *end_b
;
1448 end_a
= strrchr(pmu_name_a
, '_');
1449 end_b
= strrchr(pmu_name_b
, '_');
1451 if (!end_a
|| !end_b
)
1454 if ((end_a
- pmu_name_a
) != (end_b
- pmu_name_b
))
1457 return (strncmp(pmu_name_a
, pmu_name_b
, end_a
- pmu_name_a
) == 0);
1461 parse_events__set_leader_for_uncore_aliase(char *name
, struct list_head
*list
,
1462 struct parse_events_state
*parse_state
)
1464 struct evsel
*evsel
, *leader
;
1466 bool is_leader
= true;
1467 int i
, nr_pmu
= 0, total_members
, ret
= 0;
1469 leader
= list_first_entry(list
, struct evsel
, core
.node
);
1470 evsel
= list_last_entry(list
, struct evsel
, core
.node
);
1471 total_members
= evsel
->idx
- leader
->idx
+ 1;
1473 leaders
= calloc(total_members
, sizeof(uintptr_t));
1474 if (WARN_ON(!leaders
))
1478 * Going through the whole group and doing sanity check.
1479 * All members must use alias, and be from the same uncore block.
1480 * Also, storing the leader events in an array.
1482 __evlist__for_each_entry(list
, evsel
) {
1484 /* Only split the uncore group which members use alias */
1485 if (!evsel
->use_uncore_alias
)
1488 /* The events must be from the same uncore block */
1489 if (!is_same_uncore_block(leader
->pmu_name
, evsel
->pmu_name
))
1495 * If the event's PMU name starts to repeat, it must be a new
1496 * event. That can be used to distinguish the leader from
1497 * other members, even they have the same event name.
1499 if ((leader
!= evsel
) && (leader
->pmu_name
== evsel
->pmu_name
)) {
1503 /* The name is always alias name */
1504 WARN_ON(strcmp(leader
->name
, evsel
->name
));
1506 /* Store the leader event for each PMU */
1507 leaders
[nr_pmu
++] = (uintptr_t) evsel
;
1510 /* only one event alias */
1511 if (nr_pmu
== total_members
) {
1512 parse_state
->nr_groups
--;
1517 * An uncore event alias is a joint name which means the same event
1518 * runs on all PMUs of a block.
1519 * Perf doesn't support mixed events from different PMUs in the same
1520 * group. The big group has to be split into multiple small groups
1521 * which only include the events from the same PMU.
1523 * Here the uncore event aliases must be from the same uncore block.
1524 * The number of PMUs must be same for each alias. The number of new
1525 * small groups equals to the number of PMUs.
1526 * Setting the leader event for corresponding members in each group.
1529 __evlist__for_each_entry(list
, evsel
) {
1532 evsel
->leader
= (struct evsel
*) leaders
[i
++];
1535 /* The number of members and group name are same for each group */
1536 for (i
= 0; i
< nr_pmu
; i
++) {
1537 evsel
= (struct evsel
*) leaders
[i
];
1538 evsel
->core
.nr_members
= total_members
/ nr_pmu
;
1539 evsel
->group_name
= name
? strdup(name
) : NULL
;
1542 /* Take the new small groups into account */
1543 parse_state
->nr_groups
+= nr_pmu
- 1;
1552 void parse_events__set_leader(char *name
, struct list_head
*list
,
1553 struct parse_events_state
*parse_state
)
1555 struct evsel
*leader
;
1557 if (list_empty(list
)) {
1558 WARN_ONCE(true, "WARNING: failed to set leader: empty list");
1562 if (parse_events__set_leader_for_uncore_aliase(name
, list
, parse_state
))
1565 __perf_evlist__set_leader(list
);
1566 leader
= list_entry(list
->next
, struct evsel
, core
.node
);
1567 leader
->group_name
= name
? strdup(name
) : NULL
;
1570 /* list_event is assumed to point to malloc'ed memory */
1571 void parse_events_update_lists(struct list_head
*list_event
,
1572 struct list_head
*list_all
)
1575 * Called for single event definition. Update the
1576 * 'all event' list, and reinit the 'single event'
1577 * list, for next event definition.
1579 list_splice_tail(list_event
, list_all
);
1583 struct event_modifier
{
1598 static int get_event_modifier(struct event_modifier
*mod
, char *str
,
1599 struct evsel
*evsel
)
1601 int eu
= evsel
? evsel
->core
.attr
.exclude_user
: 0;
1602 int ek
= evsel
? evsel
->core
.attr
.exclude_kernel
: 0;
1603 int eh
= evsel
? evsel
->core
.attr
.exclude_hv
: 0;
1604 int eH
= evsel
? evsel
->core
.attr
.exclude_host
: 0;
1605 int eG
= evsel
? evsel
->core
.attr
.exclude_guest
: 0;
1606 int eI
= evsel
? evsel
->core
.attr
.exclude_idle
: 0;
1607 int precise
= evsel
? evsel
->core
.attr
.precise_ip
: 0;
1608 int precise_max
= 0;
1609 int sample_read
= 0;
1610 int pinned
= evsel
? evsel
->core
.attr
.pinned
: 0;
1612 int exclude
= eu
| ek
| eh
;
1613 int exclude_GH
= evsel
? evsel
->exclude_GH
: 0;
1616 memset(mod
, 0, sizeof(*mod
));
1621 exclude
= eu
= ek
= eh
= 1;
1623 } else if (*str
== 'k') {
1625 exclude
= eu
= ek
= eh
= 1;
1627 } else if (*str
== 'h') {
1629 exclude
= eu
= ek
= eh
= 1;
1631 } else if (*str
== 'G') {
1633 exclude_GH
= eG
= eH
= 1;
1635 } else if (*str
== 'H') {
1637 exclude_GH
= eG
= eH
= 1;
1639 } else if (*str
== 'I') {
1641 } else if (*str
== 'p') {
1643 /* use of precise requires exclude_guest */
1646 } else if (*str
== 'P') {
1648 } else if (*str
== 'S') {
1650 } else if (*str
== 'D') {
1652 } else if (*str
== 'W') {
1663 * 0 - SAMPLE_IP can have arbitrary skid
1664 * 1 - SAMPLE_IP must have constant skid
1665 * 2 - SAMPLE_IP requested to have 0 skid
1666 * 3 - SAMPLE_IP must have 0 skid
1668 * See also PERF_RECORD_MISC_EXACT_IP
1679 mod
->precise
= precise
;
1680 mod
->precise_max
= precise_max
;
1681 mod
->exclude_GH
= exclude_GH
;
1682 mod
->sample_read
= sample_read
;
1683 mod
->pinned
= pinned
;
1690 * Basic modifier sanity check to validate it contains only one
1691 * instance of any modifier (apart from 'p') present.
1693 static int check_modifier(char *str
)
1697 /* The sizeof includes 0 byte as well. */
1698 if (strlen(str
) > (sizeof("ukhGHpppPSDIW") - 1))
1702 if (*p
!= 'p' && strchr(p
+ 1, *p
))
1710 int parse_events__modifier_event(struct list_head
*list
, char *str
, bool add
)
1712 struct evsel
*evsel
;
1713 struct event_modifier mod
;
1718 if (check_modifier(str
))
1721 if (!add
&& get_event_modifier(&mod
, str
, NULL
))
1724 __evlist__for_each_entry(list
, evsel
) {
1725 if (add
&& get_event_modifier(&mod
, str
, evsel
))
1728 evsel
->core
.attr
.exclude_user
= mod
.eu
;
1729 evsel
->core
.attr
.exclude_kernel
= mod
.ek
;
1730 evsel
->core
.attr
.exclude_hv
= mod
.eh
;
1731 evsel
->core
.attr
.precise_ip
= mod
.precise
;
1732 evsel
->core
.attr
.exclude_host
= mod
.eH
;
1733 evsel
->core
.attr
.exclude_guest
= mod
.eG
;
1734 evsel
->core
.attr
.exclude_idle
= mod
.eI
;
1735 evsel
->exclude_GH
= mod
.exclude_GH
;
1736 evsel
->sample_read
= mod
.sample_read
;
1737 evsel
->precise_max
= mod
.precise_max
;
1738 evsel
->weak_group
= mod
.weak
;
1740 if (perf_evsel__is_group_leader(evsel
))
1741 evsel
->core
.attr
.pinned
= mod
.pinned
;
1747 int parse_events_name(struct list_head
*list
, char *name
)
1749 struct evsel
*evsel
;
1751 __evlist__for_each_entry(list
, evsel
) {
1753 evsel
->name
= strdup(name
);
1760 comp_pmu(const void *p1
, const void *p2
)
1762 struct perf_pmu_event_symbol
*pmu1
= (struct perf_pmu_event_symbol
*) p1
;
1763 struct perf_pmu_event_symbol
*pmu2
= (struct perf_pmu_event_symbol
*) p2
;
1765 return strcasecmp(pmu1
->symbol
, pmu2
->symbol
);
1768 static void perf_pmu__parse_cleanup(void)
1770 if (perf_pmu_events_list_num
> 0) {
1771 struct perf_pmu_event_symbol
*p
;
1774 for (i
= 0; i
< perf_pmu_events_list_num
; i
++) {
1775 p
= perf_pmu_events_list
+ i
;
1778 zfree(&perf_pmu_events_list
);
1779 perf_pmu_events_list_num
= 0;
1783 #define SET_SYMBOL(str, stype) \
1792 * Read the pmu events list from sysfs
1793 * Save it into perf_pmu_events_list
1795 static void perf_pmu__parse_init(void)
1798 struct perf_pmu
*pmu
= NULL
;
1799 struct perf_pmu_alias
*alias
;
1803 while ((pmu
= perf_pmu__scan(pmu
)) != NULL
) {
1804 list_for_each_entry(alias
, &pmu
->aliases
, list
) {
1805 if (strchr(alias
->name
, '-'))
1812 perf_pmu_events_list_num
= -1;
1815 perf_pmu_events_list
= malloc(sizeof(struct perf_pmu_event_symbol
) * len
);
1816 if (!perf_pmu_events_list
)
1818 perf_pmu_events_list_num
= len
;
1822 while ((pmu
= perf_pmu__scan(pmu
)) != NULL
) {
1823 list_for_each_entry(alias
, &pmu
->aliases
, list
) {
1824 struct perf_pmu_event_symbol
*p
= perf_pmu_events_list
+ len
;
1825 char *tmp
= strchr(alias
->name
, '-');
1828 SET_SYMBOL(strndup(alias
->name
, tmp
- alias
->name
),
1829 PMU_EVENT_SYMBOL_PREFIX
);
1831 SET_SYMBOL(strdup(++tmp
), PMU_EVENT_SYMBOL_SUFFIX
);
1834 SET_SYMBOL(strdup(alias
->name
), PMU_EVENT_SYMBOL
);
1839 qsort(perf_pmu_events_list
, len
,
1840 sizeof(struct perf_pmu_event_symbol
), comp_pmu
);
1844 perf_pmu__parse_cleanup();
1847 enum perf_pmu_event_symbol_type
1848 perf_pmu__parse_check(const char *name
)
1850 struct perf_pmu_event_symbol p
, *r
;
1852 /* scan kernel pmu events from sysfs if needed */
1853 if (perf_pmu_events_list_num
== 0)
1854 perf_pmu__parse_init();
1856 * name "cpu" could be prefix of cpu-cycles or cpu// events.
1857 * cpu-cycles has been handled by hardcode.
1858 * So it must be cpu// events, not kernel pmu event.
1860 if ((perf_pmu_events_list_num
<= 0) || !strcmp(name
, "cpu"))
1861 return PMU_EVENT_SYMBOL_ERR
;
1863 p
.symbol
= strdup(name
);
1864 r
= bsearch(&p
, perf_pmu_events_list
,
1865 (size_t) perf_pmu_events_list_num
,
1866 sizeof(struct perf_pmu_event_symbol
), comp_pmu
);
1868 return r
? r
->type
: PMU_EVENT_SYMBOL_ERR
;
1871 static int parse_events__scanner(const char *str
, void *parse_state
, int start_token
)
1873 YY_BUFFER_STATE buffer
;
1877 ret
= parse_events_lex_init_extra(start_token
, &scanner
);
1881 buffer
= parse_events__scan_string(str
, scanner
);
1884 parse_events_debug
= 1;
1886 ret
= parse_events_parse(parse_state
, scanner
);
1888 parse_events__flush_buffer(buffer
, scanner
);
1889 parse_events__delete_buffer(buffer
, scanner
);
1890 parse_events_lex_destroy(scanner
);
1895 * parse event config string, return a list of event terms.
1897 int parse_events_terms(struct list_head
*terms
, const char *str
)
1899 struct parse_events_state parse_state
= {
1904 ret
= parse_events__scanner(str
, &parse_state
, PE_START_TERMS
);
1906 list_splice(parse_state
.terms
, terms
);
1907 zfree(&parse_state
.terms
);
1911 parse_events_terms__delete(parse_state
.terms
);
1915 int parse_events(struct evlist
*evlist
, const char *str
,
1916 struct parse_events_error
*err
)
1918 struct parse_events_state parse_state
= {
1919 .list
= LIST_HEAD_INIT(parse_state
.list
),
1920 .idx
= evlist
->core
.nr_entries
,
1926 ret
= parse_events__scanner(str
, &parse_state
, PE_START_EVENTS
);
1927 perf_pmu__parse_cleanup();
1931 if (list_empty(&parse_state
.list
)) {
1932 WARN_ONCE(true, "WARNING: event parser found nothing\n");
1936 perf_evlist__splice_list_tail(evlist
, &parse_state
.list
);
1937 evlist
->nr_groups
+= parse_state
.nr_groups
;
1938 last
= perf_evlist__last(evlist
);
1939 last
->cmdline_group_boundary
= true;
1945 * There are 2 users - builtin-record and builtin-test objects.
1946 * Both call evlist__delete in case of error, so we dont
1952 #define MAX_WIDTH 1000
1953 static int get_term_width(void)
1957 get_term_dimensions(&ws
);
1958 return ws
.ws_col
> MAX_WIDTH
? MAX_WIDTH
: ws
.ws_col
;
1961 void parse_events_print_error(struct parse_events_error
*err
,
1964 const char *str
= "invalid or unsupported event: ";
1965 char _buf
[MAX_WIDTH
];
1966 char *buf
= (char *) event
;
1970 /* -2 for extra '' in the final fprintf */
1971 int width
= get_term_width() - 2;
1972 int len_event
= strlen(event
);
1973 int len_str
, max_len
, cut
= 0;
1976 * Maximum error index indent, we will cut
1977 * the event string if it's bigger.
1979 int max_err_idx
= 13;
1982 * Let's be specific with the message when
1983 * we have the precise error.
1985 str
= "event syntax error: ";
1986 len_str
= strlen(str
);
1987 max_len
= width
- len_str
;
1991 /* We're cutting from the beginning. */
1992 if (err
->idx
> max_err_idx
)
1993 cut
= err
->idx
- max_err_idx
;
1995 strncpy(buf
, event
+ cut
, max_len
);
1997 /* Mark cut parts with '..' on both sides. */
1999 buf
[0] = buf
[1] = '.';
2001 if ((len_event
- cut
) > max_len
) {
2002 buf
[max_len
- 1] = buf
[max_len
- 2] = '.';
2006 idx
= len_str
+ err
->idx
- cut
;
2009 fprintf(stderr
, "%s'%s'\n", str
, buf
);
2011 fprintf(stderr
, "%*s\\___ %s\n", idx
+ 1, "", err
->str
);
2013 fprintf(stderr
, "\n%s\n", err
->help
);
2021 int parse_events_option(const struct option
*opt
, const char *str
,
2022 int unset __maybe_unused
)
2024 struct evlist
*evlist
= *(struct evlist
**)opt
->value
;
2025 struct parse_events_error err
= { .idx
= 0, };
2026 int ret
= parse_events(evlist
, str
, &err
);
2029 parse_events_print_error(&err
, str
);
2030 fprintf(stderr
, "Run 'perf list' for a list of valid events\n");
2037 foreach_evsel_in_last_glob(struct evlist
*evlist
,
2038 int (*func
)(struct evsel
*evsel
,
2042 struct evsel
*last
= NULL
;
2046 * Don't return when list_empty, give func a chance to report
2047 * error when it found last == NULL.
2049 * So no need to WARN here, let *func do this.
2051 if (evlist
->core
.nr_entries
> 0)
2052 last
= perf_evlist__last(evlist
);
2055 err
= (*func
)(last
, arg
);
2061 if (last
->core
.node
.prev
== &evlist
->core
.entries
)
2063 last
= list_entry(last
->core
.node
.prev
, struct evsel
, core
.node
);
2064 } while (!last
->cmdline_group_boundary
);
2069 static int set_filter(struct evsel
*evsel
, const void *arg
)
2071 const char *str
= arg
;
2073 int nr_addr_filters
= 0;
2074 struct perf_pmu
*pmu
= NULL
;
2076 if (evsel
== NULL
) {
2078 "--filter option should follow a -e tracepoint or HW tracer option\n");
2082 if (evsel
->core
.attr
.type
== PERF_TYPE_TRACEPOINT
) {
2083 if (perf_evsel__append_tp_filter(evsel
, str
) < 0) {
2085 "not enough memory to hold filter string\n");
2092 while ((pmu
= perf_pmu__scan(pmu
)) != NULL
)
2093 if (pmu
->type
== evsel
->core
.attr
.type
) {
2099 perf_pmu__scan_file(pmu
, "nr_addr_filters",
2100 "%d", &nr_addr_filters
);
2102 if (!nr_addr_filters
) {
2104 "This CPU does not support address filtering\n");
2108 if (perf_evsel__append_addr_filter(evsel
, str
) < 0) {
2110 "not enough memory to hold filter string\n");
2117 int parse_filter(const struct option
*opt
, const char *str
,
2118 int unset __maybe_unused
)
2120 struct evlist
*evlist
= *(struct evlist
**)opt
->value
;
2122 return foreach_evsel_in_last_glob(evlist
, set_filter
,
2126 static int add_exclude_perf_filter(struct evsel
*evsel
,
2127 const void *arg __maybe_unused
)
2129 char new_filter
[64];
2131 if (evsel
== NULL
|| evsel
->core
.attr
.type
!= PERF_TYPE_TRACEPOINT
) {
2133 "--exclude-perf option should follow a -e tracepoint option\n");
2137 snprintf(new_filter
, sizeof(new_filter
), "common_pid != %d", getpid());
2139 if (perf_evsel__append_tp_filter(evsel
, new_filter
) < 0) {
2141 "not enough memory to hold filter string\n");
2148 int exclude_perf(const struct option
*opt
,
2149 const char *arg __maybe_unused
,
2150 int unset __maybe_unused
)
2152 struct evlist
*evlist
= *(struct evlist
**)opt
->value
;
2154 return foreach_evsel_in_last_glob(evlist
, add_exclude_perf_filter
,
2158 static const char * const event_type_descriptors
[] = {
2162 "Hardware cache event",
2163 "Raw hardware event descriptor",
2164 "Hardware breakpoint",
2167 static int cmp_string(const void *a
, const void *b
)
2169 const char * const *as
= a
;
2170 const char * const *bs
= b
;
2172 return strcmp(*as
, *bs
);
2176 * Print the events from <debugfs_mount_point>/tracing/events
2179 void print_tracepoint_events(const char *subsys_glob
, const char *event_glob
,
2182 DIR *sys_dir
, *evt_dir
;
2183 struct dirent
*sys_dirent
, *evt_dirent
;
2184 char evt_path
[MAXPATHLEN
];
2186 char **evt_list
= NULL
;
2187 unsigned int evt_i
= 0, evt_num
= 0;
2188 bool evt_num_known
= false;
2191 sys_dir
= tracing_events__opendir();
2195 if (evt_num_known
) {
2196 evt_list
= zalloc(sizeof(char *) * evt_num
);
2198 goto out_close_sys_dir
;
2201 for_each_subsystem(sys_dir
, sys_dirent
) {
2202 if (subsys_glob
!= NULL
&&
2203 !strglobmatch(sys_dirent
->d_name
, subsys_glob
))
2206 dir_path
= get_events_file(sys_dirent
->d_name
);
2209 evt_dir
= opendir(dir_path
);
2213 for_each_event(dir_path
, evt_dir
, evt_dirent
) {
2214 if (event_glob
!= NULL
&&
2215 !strglobmatch(evt_dirent
->d_name
, event_glob
))
2218 if (!evt_num_known
) {
2223 snprintf(evt_path
, MAXPATHLEN
, "%s:%s",
2224 sys_dirent
->d_name
, evt_dirent
->d_name
);
2226 evt_list
[evt_i
] = strdup(evt_path
);
2227 if (evt_list
[evt_i
] == NULL
) {
2228 put_events_file(dir_path
);
2229 goto out_close_evt_dir
;
2235 put_events_file(dir_path
);
2239 if (!evt_num_known
) {
2240 evt_num_known
= true;
2243 qsort(evt_list
, evt_num
, sizeof(char *), cmp_string
);
2245 while (evt_i
< evt_num
) {
2247 printf("%s ", evt_list
[evt_i
++]);
2250 printf(" %-50s [%s]\n", evt_list
[evt_i
++],
2251 event_type_descriptors
[PERF_TYPE_TRACEPOINT
]);
2253 if (evt_num
&& pager_in_use())
2258 for (evt_i
= 0; evt_i
< evt_num
; evt_i
++)
2259 zfree(&evt_list
[evt_i
]);
2268 printf("FATAL: not enough memory to print %s\n",
2269 event_type_descriptors
[PERF_TYPE_TRACEPOINT
]);
2275 * Check whether event is in <debugfs_mount_point>/tracing/events
2278 int is_valid_tracepoint(const char *event_string
)
2280 DIR *sys_dir
, *evt_dir
;
2281 struct dirent
*sys_dirent
, *evt_dirent
;
2282 char evt_path
[MAXPATHLEN
];
2285 sys_dir
= tracing_events__opendir();
2289 for_each_subsystem(sys_dir
, sys_dirent
) {
2290 dir_path
= get_events_file(sys_dirent
->d_name
);
2293 evt_dir
= opendir(dir_path
);
2297 for_each_event(dir_path
, evt_dir
, evt_dirent
) {
2298 snprintf(evt_path
, MAXPATHLEN
, "%s:%s",
2299 sys_dirent
->d_name
, evt_dirent
->d_name
);
2300 if (!strcmp(evt_path
, event_string
)) {
2308 put_events_file(dir_path
);
2314 static bool is_event_supported(u8 type
, unsigned config
)
2318 struct evsel
*evsel
;
2319 struct perf_event_attr attr
= {
2324 struct perf_thread_map
*tmap
= thread_map__new_by_tid(0);
2329 evsel
= evsel__new(&attr
);
2331 open_return
= evsel__open(evsel
, NULL
, tmap
);
2332 ret
= open_return
>= 0;
2334 if (open_return
== -EACCES
) {
2336 * This happens if the paranoid value
2337 * /proc/sys/kernel/perf_event_paranoid is set to 2
2338 * Re-run with exclude_kernel set; we don't do that
2339 * by default as some ARM machines do not support it.
2342 evsel
->core
.attr
.exclude_kernel
= 1;
2343 ret
= evsel__open(evsel
, NULL
, tmap
) >= 0;
2345 evsel__delete(evsel
);
2348 perf_thread_map__put(tmap
);
2352 void print_sdt_events(const char *subsys_glob
, const char *event_glob
,
2355 struct probe_cache
*pcache
;
2356 struct probe_cache_entry
*ent
;
2357 struct strlist
*bidlist
, *sdtlist
;
2358 struct strlist_config cfg
= {.dont_dupstr
= true};
2359 struct str_node
*nd
, *nd2
;
2360 char *buf
, *path
, *ptr
= NULL
;
2361 bool show_detail
= false;
2364 sdtlist
= strlist__new(NULL
, &cfg
);
2366 pr_debug("Failed to allocate new strlist for SDT\n");
2369 bidlist
= build_id_cache__list_all(true);
2371 pr_debug("Failed to get buildids: %d\n", errno
);
2374 strlist__for_each_entry(nd
, bidlist
) {
2375 pcache
= probe_cache__new(nd
->s
, NULL
);
2378 list_for_each_entry(ent
, &pcache
->entries
, node
) {
2382 !strglobmatch(ent
->pev
.group
, subsys_glob
))
2385 !strglobmatch(ent
->pev
.event
, event_glob
))
2387 ret
= asprintf(&buf
, "%s:%s@%s", ent
->pev
.group
,
2388 ent
->pev
.event
, nd
->s
);
2390 strlist__add(sdtlist
, buf
);
2392 probe_cache__delete(pcache
);
2394 strlist__delete(bidlist
);
2396 strlist__for_each_entry(nd
, sdtlist
) {
2397 buf
= strchr(nd
->s
, '@');
2401 printf("%s ", nd
->s
);
2404 nd2
= strlist__next(nd
);
2406 ptr
= strchr(nd2
->s
, '@');
2409 if (strcmp(nd
->s
, nd2
->s
) == 0)
2413 path
= build_id_cache__origname(buf
);
2414 ret
= asprintf(&buf
, "%s@%s(%.12s)", nd
->s
, path
, buf
);
2416 printf(" %-50s [%s]\n", buf
, "SDT event");
2421 printf(" %-50s [%s]\n", nd
->s
, "SDT event");
2423 if (strcmp(nd
->s
, nd2
->s
) != 0)
2424 show_detail
= false;
2429 strlist__delete(sdtlist
);
2432 int print_hwcache_events(const char *event_glob
, bool name_only
)
2434 unsigned int type
, op
, i
, evt_i
= 0, evt_num
= 0;
2436 char **evt_list
= NULL
;
2437 bool evt_num_known
= false;
2440 if (evt_num_known
) {
2441 evt_list
= zalloc(sizeof(char *) * evt_num
);
2446 for (type
= 0; type
< PERF_COUNT_HW_CACHE_MAX
; type
++) {
2447 for (op
= 0; op
< PERF_COUNT_HW_CACHE_OP_MAX
; op
++) {
2448 /* skip invalid cache type */
2449 if (!perf_evsel__is_cache_op_valid(type
, op
))
2452 for (i
= 0; i
< PERF_COUNT_HW_CACHE_RESULT_MAX
; i
++) {
2453 __perf_evsel__hw_cache_type_op_res_name(type
, op
, i
,
2454 name
, sizeof(name
));
2455 if (event_glob
!= NULL
&& !strglobmatch(name
, event_glob
))
2458 if (!is_event_supported(PERF_TYPE_HW_CACHE
,
2459 type
| (op
<< 8) | (i
<< 16)))
2462 if (!evt_num_known
) {
2467 evt_list
[evt_i
] = strdup(name
);
2468 if (evt_list
[evt_i
] == NULL
)
2475 if (!evt_num_known
) {
2476 evt_num_known
= true;
2479 qsort(evt_list
, evt_num
, sizeof(char *), cmp_string
);
2481 while (evt_i
< evt_num
) {
2483 printf("%s ", evt_list
[evt_i
++]);
2486 printf(" %-50s [%s]\n", evt_list
[evt_i
++],
2487 event_type_descriptors
[PERF_TYPE_HW_CACHE
]);
2489 if (evt_num
&& pager_in_use())
2494 for (evt_i
= 0; evt_i
< evt_num
; evt_i
++)
2495 zfree(&evt_list
[evt_i
]);
2500 printf("FATAL: not enough memory to print %s\n", event_type_descriptors
[PERF_TYPE_HW_CACHE
]);
2506 static void print_tool_event(const char *name
, const char *event_glob
,
2509 if (event_glob
&& !strglobmatch(name
, event_glob
))
2512 printf("%s ", name
);
2514 printf(" %-50s [%s]\n", name
, "Tool event");
2518 void print_tool_events(const char *event_glob
, bool name_only
)
2520 print_tool_event("duration_time", event_glob
, name_only
);
2525 void print_symbol_events(const char *event_glob
, unsigned type
,
2526 struct event_symbol
*syms
, unsigned max
,
2529 unsigned int i
, evt_i
= 0, evt_num
= 0;
2530 char name
[MAX_NAME_LEN
];
2531 char **evt_list
= NULL
;
2532 bool evt_num_known
= false;
2535 if (evt_num_known
) {
2536 evt_list
= zalloc(sizeof(char *) * evt_num
);
2542 for (i
= 0; i
< max
; i
++, syms
++) {
2544 if (event_glob
!= NULL
&& syms
->symbol
!= NULL
&&
2545 !(strglobmatch(syms
->symbol
, event_glob
) ||
2546 (syms
->alias
&& strglobmatch(syms
->alias
, event_glob
))))
2549 if (!is_event_supported(type
, i
))
2552 if (!evt_num_known
) {
2557 if (!name_only
&& strlen(syms
->alias
))
2558 snprintf(name
, MAX_NAME_LEN
, "%s OR %s", syms
->symbol
, syms
->alias
);
2560 strlcpy(name
, syms
->symbol
, MAX_NAME_LEN
);
2562 evt_list
[evt_i
] = strdup(name
);
2563 if (evt_list
[evt_i
] == NULL
)
2568 if (!evt_num_known
) {
2569 evt_num_known
= true;
2572 qsort(evt_list
, evt_num
, sizeof(char *), cmp_string
);
2574 while (evt_i
< evt_num
) {
2576 printf("%s ", evt_list
[evt_i
++]);
2579 printf(" %-50s [%s]\n", evt_list
[evt_i
++], event_type_descriptors
[type
]);
2581 if (evt_num
&& pager_in_use())
2586 for (evt_i
= 0; evt_i
< evt_num
; evt_i
++)
2587 zfree(&evt_list
[evt_i
]);
2592 printf("FATAL: not enough memory to print %s\n", event_type_descriptors
[type
]);
2598 * Print the help text for the event symbols:
2600 void print_events(const char *event_glob
, bool name_only
, bool quiet_flag
,
2601 bool long_desc
, bool details_flag
)
2603 print_symbol_events(event_glob
, PERF_TYPE_HARDWARE
,
2604 event_symbols_hw
, PERF_COUNT_HW_MAX
, name_only
);
2606 print_symbol_events(event_glob
, PERF_TYPE_SOFTWARE
,
2607 event_symbols_sw
, PERF_COUNT_SW_MAX
, name_only
);
2608 print_tool_events(event_glob
, name_only
);
2610 print_hwcache_events(event_glob
, name_only
);
2612 print_pmu_events(event_glob
, name_only
, quiet_flag
, long_desc
,
2615 if (event_glob
!= NULL
)
2619 printf(" %-50s [%s]\n",
2621 event_type_descriptors
[PERF_TYPE_RAW
]);
2622 printf(" %-50s [%s]\n",
2623 "cpu/t1=v1[,t2=v2,t3 ...]/modifier",
2624 event_type_descriptors
[PERF_TYPE_RAW
]);
2626 printf(" (see 'man perf-list' on how to encode it)\n\n");
2628 printf(" %-50s [%s]\n",
2629 "mem:<addr>[/len][:access]",
2630 event_type_descriptors
[PERF_TYPE_BREAKPOINT
]);
2635 print_tracepoint_events(NULL
, NULL
, name_only
);
2637 print_sdt_events(NULL
, NULL
, name_only
);
2639 metricgroup__print(true, true, NULL
, name_only
, details_flag
);
2642 int parse_events__is_hardcoded_term(struct parse_events_term
*term
)
2644 return term
->type_term
!= PARSE_EVENTS__TERM_TYPE_USER
;
2647 static int new_term(struct parse_events_term
**_term
,
2648 struct parse_events_term
*temp
,
2651 struct parse_events_term
*term
;
2653 term
= malloc(sizeof(*term
));
2658 INIT_LIST_HEAD(&term
->list
);
2661 switch (term
->type_val
) {
2662 case PARSE_EVENTS__TERM_TYPE_NUM
:
2663 term
->val
.num
= num
;
2665 case PARSE_EVENTS__TERM_TYPE_STR
:
2666 term
->val
.str
= str
;
2677 int parse_events_term__num(struct parse_events_term
**term
,
2678 int type_term
, char *config
, u64 num
,
2680 void *loc_term_
, void *loc_val_
)
2682 YYLTYPE
*loc_term
= loc_term_
;
2683 YYLTYPE
*loc_val
= loc_val_
;
2685 struct parse_events_term temp
= {
2686 .type_val
= PARSE_EVENTS__TERM_TYPE_NUM
,
2687 .type_term
= type_term
,
2689 .no_value
= no_value
,
2690 .err_term
= loc_term
? loc_term
->first_column
: 0,
2691 .err_val
= loc_val
? loc_val
->first_column
: 0,
2694 return new_term(term
, &temp
, NULL
, num
);
2697 int parse_events_term__str(struct parse_events_term
**term
,
2698 int type_term
, char *config
, char *str
,
2699 void *loc_term_
, void *loc_val_
)
2701 YYLTYPE
*loc_term
= loc_term_
;
2702 YYLTYPE
*loc_val
= loc_val_
;
2704 struct parse_events_term temp
= {
2705 .type_val
= PARSE_EVENTS__TERM_TYPE_STR
,
2706 .type_term
= type_term
,
2708 .err_term
= loc_term
? loc_term
->first_column
: 0,
2709 .err_val
= loc_val
? loc_val
->first_column
: 0,
2712 return new_term(term
, &temp
, str
, 0);
2715 int parse_events_term__sym_hw(struct parse_events_term
**term
,
2716 char *config
, unsigned idx
)
2718 struct event_symbol
*sym
;
2719 struct parse_events_term temp
= {
2720 .type_val
= PARSE_EVENTS__TERM_TYPE_STR
,
2721 .type_term
= PARSE_EVENTS__TERM_TYPE_USER
,
2722 .config
= config
?: (char *) "event",
2725 BUG_ON(idx
>= PERF_COUNT_HW_MAX
);
2726 sym
= &event_symbols_hw
[idx
];
2728 return new_term(term
, &temp
, (char *) sym
->symbol
, 0);
2731 int parse_events_term__clone(struct parse_events_term
**new,
2732 struct parse_events_term
*term
)
2734 struct parse_events_term temp
= {
2735 .type_val
= term
->type_val
,
2736 .type_term
= term
->type_term
,
2737 .config
= term
->config
,
2738 .err_term
= term
->err_term
,
2739 .err_val
= term
->err_val
,
2742 return new_term(new, &temp
, term
->val
.str
, term
->val
.num
);
2745 int parse_events_copy_term_list(struct list_head
*old
,
2746 struct list_head
**new)
2748 struct parse_events_term
*term
, *n
;
2756 *new = malloc(sizeof(struct list_head
));
2759 INIT_LIST_HEAD(*new);
2761 list_for_each_entry (term
, old
, list
) {
2762 ret
= parse_events_term__clone(&n
, term
);
2765 list_add_tail(&n
->list
, *new);
2770 void parse_events_terms__purge(struct list_head
*terms
)
2772 struct parse_events_term
*term
, *h
;
2774 list_for_each_entry_safe(term
, h
, terms
, list
) {
2775 if (term
->array
.nr_ranges
)
2776 zfree(&term
->array
.ranges
);
2777 list_del_init(&term
->list
);
2782 void parse_events_terms__delete(struct list_head
*terms
)
2786 parse_events_terms__purge(terms
);
2790 void parse_events__clear_array(struct parse_events_array
*a
)
2795 void parse_events_evlist_error(struct parse_events_state
*parse_state
,
2796 int idx
, const char *str
)
2798 struct parse_events_error
*err
= parse_state
->error
;
2803 err
->str
= strdup(str
);
2804 WARN_ONCE(!err
->str
, "WARNING: failed to allocate error string");
2807 static void config_terms_list(char *buf
, size_t buf_sz
)
2813 for (i
= 0; i
< __PARSE_EVENTS__TERM_TYPE_NR
; i
++) {
2814 const char *name
= config_term_names
[i
];
2816 if (!config_term_avail(i
, NULL
))
2823 if (strlen(buf
) + strlen(name
) + 2 >= buf_sz
)
2835 * Return string contains valid config terms of an event.
2836 * @additional_terms: For terms such as PMU sysfs terms.
2838 char *parse_events_formats_error_string(char *additional_terms
)
2841 /* "no-overwrite" is the longest name */
2842 char static_terms
[__PARSE_EVENTS__TERM_TYPE_NR
*
2843 (sizeof("no-overwrite") - 1)];
2845 config_terms_list(static_terms
, sizeof(static_terms
));
2847 if (additional_terms
) {
2848 if (asprintf(&str
, "valid terms: %s,%s",
2849 additional_terms
, static_terms
) < 0)
2852 if (asprintf(&str
, "valid terms: %s", static_terms
) < 0)