1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/hw_breakpoint.h>
10 #include <sys/param.h>
15 #include <subcmd/parse-options.h>
16 #include "parse-events.h"
17 #include <subcmd/exec-cmd.h>
23 #include "bpf-loader.h"
25 #include <api/fs/tracing_path.h>
26 #include "parse-events-bison.h"
27 #define YY_EXTRA_TYPE int
28 #include "parse-events-flex.h"
30 #include "thread_map.h"
32 #include "probe-file.h"
34 #include "util/parse-branch-options.h"
35 #include "metricgroup.h"
37 #define MAX_NAME_LEN 100
40 extern int parse_events_debug
;
42 int parse_events_parse(void *parse_state
, void *scanner
);
43 static int get_config_terms(struct list_head
*head_config
,
44 struct list_head
*head_terms __maybe_unused
);
46 static struct perf_pmu_event_symbol
*perf_pmu_events_list
;
48 * The variable indicates the number of supported pmu event symbols.
49 * 0 means not initialized and ready to init
50 * -1 means failed to init, don't try anymore
51 * >0 is the number of supported pmu event symbols
53 static int perf_pmu_events_list_num
;
55 struct event_symbol event_symbols_hw
[PERF_COUNT_HW_MAX
] = {
56 [PERF_COUNT_HW_CPU_CYCLES
] = {
57 .symbol
= "cpu-cycles",
60 [PERF_COUNT_HW_INSTRUCTIONS
] = {
61 .symbol
= "instructions",
64 [PERF_COUNT_HW_CACHE_REFERENCES
] = {
65 .symbol
= "cache-references",
68 [PERF_COUNT_HW_CACHE_MISSES
] = {
69 .symbol
= "cache-misses",
72 [PERF_COUNT_HW_BRANCH_INSTRUCTIONS
] = {
73 .symbol
= "branch-instructions",
76 [PERF_COUNT_HW_BRANCH_MISSES
] = {
77 .symbol
= "branch-misses",
80 [PERF_COUNT_HW_BUS_CYCLES
] = {
81 .symbol
= "bus-cycles",
84 [PERF_COUNT_HW_STALLED_CYCLES_FRONTEND
] = {
85 .symbol
= "stalled-cycles-frontend",
86 .alias
= "idle-cycles-frontend",
88 [PERF_COUNT_HW_STALLED_CYCLES_BACKEND
] = {
89 .symbol
= "stalled-cycles-backend",
90 .alias
= "idle-cycles-backend",
92 [PERF_COUNT_HW_REF_CPU_CYCLES
] = {
93 .symbol
= "ref-cycles",
98 struct event_symbol event_symbols_sw
[PERF_COUNT_SW_MAX
] = {
99 [PERF_COUNT_SW_CPU_CLOCK
] = {
100 .symbol
= "cpu-clock",
103 [PERF_COUNT_SW_TASK_CLOCK
] = {
104 .symbol
= "task-clock",
107 [PERF_COUNT_SW_PAGE_FAULTS
] = {
108 .symbol
= "page-faults",
111 [PERF_COUNT_SW_CONTEXT_SWITCHES
] = {
112 .symbol
= "context-switches",
115 [PERF_COUNT_SW_CPU_MIGRATIONS
] = {
116 .symbol
= "cpu-migrations",
117 .alias
= "migrations",
119 [PERF_COUNT_SW_PAGE_FAULTS_MIN
] = {
120 .symbol
= "minor-faults",
123 [PERF_COUNT_SW_PAGE_FAULTS_MAJ
] = {
124 .symbol
= "major-faults",
127 [PERF_COUNT_SW_ALIGNMENT_FAULTS
] = {
128 .symbol
= "alignment-faults",
131 [PERF_COUNT_SW_EMULATION_FAULTS
] = {
132 .symbol
= "emulation-faults",
135 [PERF_COUNT_SW_DUMMY
] = {
139 [PERF_COUNT_SW_BPF_OUTPUT
] = {
140 .symbol
= "bpf-output",
145 #define __PERF_EVENT_FIELD(config, name) \
146 ((config & PERF_EVENT_##name##_MASK) >> PERF_EVENT_##name##_SHIFT)
148 #define PERF_EVENT_RAW(config) __PERF_EVENT_FIELD(config, RAW)
149 #define PERF_EVENT_CONFIG(config) __PERF_EVENT_FIELD(config, CONFIG)
150 #define PERF_EVENT_TYPE(config) __PERF_EVENT_FIELD(config, TYPE)
151 #define PERF_EVENT_ID(config) __PERF_EVENT_FIELD(config, EVENT)
153 #define for_each_subsystem(sys_dir, sys_dirent) \
154 while ((sys_dirent = readdir(sys_dir)) != NULL) \
155 if (sys_dirent->d_type == DT_DIR && \
156 (strcmp(sys_dirent->d_name, ".")) && \
157 (strcmp(sys_dirent->d_name, "..")))
159 static int tp_event_has_id(const char *dir_path
, struct dirent
*evt_dir
)
161 char evt_path
[MAXPATHLEN
];
164 snprintf(evt_path
, MAXPATHLEN
, "%s/%s/id", dir_path
, evt_dir
->d_name
);
165 fd
= open(evt_path
, O_RDONLY
);
173 #define for_each_event(dir_path, evt_dir, evt_dirent) \
174 while ((evt_dirent = readdir(evt_dir)) != NULL) \
175 if (evt_dirent->d_type == DT_DIR && \
176 (strcmp(evt_dirent->d_name, ".")) && \
177 (strcmp(evt_dirent->d_name, "..")) && \
178 (!tp_event_has_id(dir_path, evt_dirent)))
180 #define MAX_EVENT_LENGTH 512
183 struct tracepoint_path
*tracepoint_id_to_path(u64 config
)
185 struct tracepoint_path
*path
= NULL
;
186 DIR *sys_dir
, *evt_dir
;
187 struct dirent
*sys_dirent
, *evt_dirent
;
191 char evt_path
[MAXPATHLEN
];
194 sys_dir
= tracing_events__opendir();
198 for_each_subsystem(sys_dir
, sys_dirent
) {
199 dir_path
= get_events_file(sys_dirent
->d_name
);
202 evt_dir
= opendir(dir_path
);
206 for_each_event(dir_path
, evt_dir
, evt_dirent
) {
208 scnprintf(evt_path
, MAXPATHLEN
, "%s/%s/id", dir_path
,
210 fd
= open(evt_path
, O_RDONLY
);
213 if (read(fd
, id_buf
, sizeof(id_buf
)) < 0) {
220 put_events_file(dir_path
);
223 path
= zalloc(sizeof(*path
));
226 path
->system
= malloc(MAX_EVENT_LENGTH
);
231 path
->name
= malloc(MAX_EVENT_LENGTH
);
233 zfree(&path
->system
);
237 strncpy(path
->system
, sys_dirent
->d_name
,
239 strncpy(path
->name
, evt_dirent
->d_name
,
246 put_events_file(dir_path
);
253 struct tracepoint_path
*tracepoint_name_to_path(const char *name
)
255 struct tracepoint_path
*path
= zalloc(sizeof(*path
));
256 char *str
= strchr(name
, ':');
258 if (path
== NULL
|| str
== NULL
) {
263 path
->system
= strndup(name
, str
- name
);
264 path
->name
= strdup(str
+1);
266 if (path
->system
== NULL
|| path
->name
== NULL
) {
267 zfree(&path
->system
);
275 const char *event_type(int type
)
278 case PERF_TYPE_HARDWARE
:
281 case PERF_TYPE_SOFTWARE
:
284 case PERF_TYPE_TRACEPOINT
:
287 case PERF_TYPE_HW_CACHE
:
288 return "hardware-cache";
297 static int parse_events__is_name_term(struct parse_events_term
*term
)
299 return term
->type_term
== PARSE_EVENTS__TERM_TYPE_NAME
;
302 static char *get_config_name(struct list_head
*head_terms
)
304 struct parse_events_term
*term
;
309 list_for_each_entry(term
, head_terms
, list
)
310 if (parse_events__is_name_term(term
))
311 return term
->val
.str
;
316 static struct perf_evsel
*
317 __add_event(struct list_head
*list
, int *idx
,
318 struct perf_event_attr
*attr
,
319 char *name
, struct perf_pmu
*pmu
,
320 struct list_head
*config_terms
, bool auto_merge_stats
,
321 const char *cpu_list
)
323 struct perf_evsel
*evsel
;
324 struct cpu_map
*cpus
= pmu
? pmu
->cpus
:
325 cpu_list
? cpu_map__new(cpu_list
) : NULL
;
327 event_attr_init(attr
);
329 evsel
= perf_evsel__new_idx(attr
, *idx
);
334 evsel
->cpus
= cpu_map__get(cpus
);
335 evsel
->own_cpus
= cpu_map__get(cpus
);
336 evsel
->system_wide
= pmu
? pmu
->is_uncore
: false;
337 evsel
->auto_merge_stats
= auto_merge_stats
;
340 evsel
->name
= strdup(name
);
343 list_splice(config_terms
, &evsel
->config_terms
);
345 list_add_tail(&evsel
->node
, list
);
349 static int add_event(struct list_head
*list
, int *idx
,
350 struct perf_event_attr
*attr
, char *name
,
351 struct list_head
*config_terms
)
353 return __add_event(list
, idx
, attr
, name
, NULL
, config_terms
, false, NULL
) ? 0 : -ENOMEM
;
356 static int add_event_tool(struct list_head
*list
, int *idx
,
357 enum perf_tool_event tool_event
)
359 struct perf_evsel
*evsel
;
360 struct perf_event_attr attr
= {
361 .type
= PERF_TYPE_SOFTWARE
,
362 .config
= PERF_COUNT_SW_DUMMY
,
365 evsel
= __add_event(list
, idx
, &attr
, NULL
, NULL
, NULL
, false, "0");
368 evsel
->tool_event
= tool_event
;
369 if (tool_event
== PERF_TOOL_DURATION_TIME
)
370 evsel
->unit
= strdup("ns");
374 static int parse_aliases(char *str
, const char *names
[][PERF_EVSEL__MAX_ALIASES
], int size
)
379 for (i
= 0; i
< size
; i
++) {
380 for (j
= 0; j
< PERF_EVSEL__MAX_ALIASES
&& names
[i
][j
]; j
++) {
381 n
= strlen(names
[i
][j
]);
382 if (n
> longest
&& !strncasecmp(str
, names
[i
][j
], n
))
392 typedef int config_term_func_t(struct perf_event_attr
*attr
,
393 struct parse_events_term
*term
,
394 struct parse_events_error
*err
);
395 static int config_term_common(struct perf_event_attr
*attr
,
396 struct parse_events_term
*term
,
397 struct parse_events_error
*err
);
398 static int config_attr(struct perf_event_attr
*attr
,
399 struct list_head
*head
,
400 struct parse_events_error
*err
,
401 config_term_func_t config_term
);
403 int parse_events_add_cache(struct list_head
*list
, int *idx
,
404 char *type
, char *op_result1
, char *op_result2
,
405 struct parse_events_error
*err
,
406 struct list_head
*head_config
)
408 struct perf_event_attr attr
;
409 LIST_HEAD(config_terms
);
410 char name
[MAX_NAME_LEN
], *config_name
;
411 int cache_type
= -1, cache_op
= -1, cache_result
= -1;
412 char *op_result
[2] = { op_result1
, op_result2
};
416 * No fallback - if we cannot get a clear cache type
419 cache_type
= parse_aliases(type
, perf_evsel__hw_cache
,
420 PERF_COUNT_HW_CACHE_MAX
);
421 if (cache_type
== -1)
424 config_name
= get_config_name(head_config
);
425 n
= snprintf(name
, MAX_NAME_LEN
, "%s", type
);
427 for (i
= 0; (i
< 2) && (op_result
[i
]); i
++) {
428 char *str
= op_result
[i
];
430 n
+= snprintf(name
+ n
, MAX_NAME_LEN
- n
, "-%s", str
);
432 if (cache_op
== -1) {
433 cache_op
= parse_aliases(str
, perf_evsel__hw_cache_op
,
434 PERF_COUNT_HW_CACHE_OP_MAX
);
436 if (!perf_evsel__is_cache_op_valid(cache_type
, cache_op
))
442 if (cache_result
== -1) {
443 cache_result
= parse_aliases(str
, perf_evsel__hw_cache_result
,
444 PERF_COUNT_HW_CACHE_RESULT_MAX
);
445 if (cache_result
>= 0)
451 * Fall back to reads:
454 cache_op
= PERF_COUNT_HW_CACHE_OP_READ
;
457 * Fall back to accesses:
459 if (cache_result
== -1)
460 cache_result
= PERF_COUNT_HW_CACHE_RESULT_ACCESS
;
462 memset(&attr
, 0, sizeof(attr
));
463 attr
.config
= cache_type
| (cache_op
<< 8) | (cache_result
<< 16);
464 attr
.type
= PERF_TYPE_HW_CACHE
;
467 if (config_attr(&attr
, head_config
, err
,
471 if (get_config_terms(head_config
, &config_terms
))
474 return add_event(list
, idx
, &attr
, config_name
? : name
, &config_terms
);
477 static void tracepoint_error(struct parse_events_error
*e
, int err
,
478 const char *sys
, const char *name
)
486 * We get error directly from syscall errno ( > 0),
487 * or from encoded pointer's error ( < 0).
493 e
->str
= strdup("can't access trace events");
496 e
->str
= strdup("unknown tracepoint");
499 e
->str
= strdup("failed to add tracepoint");
503 tracing_path__strerror_open_tp(err
, help
, sizeof(help
), sys
, name
);
504 e
->help
= strdup(help
);
507 static int add_tracepoint(struct list_head
*list
, int *idx
,
508 const char *sys_name
, const char *evt_name
,
509 struct parse_events_error
*err
,
510 struct list_head
*head_config
)
512 struct perf_evsel
*evsel
;
514 evsel
= perf_evsel__newtp_idx(sys_name
, evt_name
, (*idx
)++);
516 tracepoint_error(err
, PTR_ERR(evsel
), sys_name
, evt_name
);
517 return PTR_ERR(evsel
);
521 LIST_HEAD(config_terms
);
523 if (get_config_terms(head_config
, &config_terms
))
525 list_splice(&config_terms
, &evsel
->config_terms
);
528 list_add_tail(&evsel
->node
, list
);
532 static int add_tracepoint_multi_event(struct list_head
*list
, int *idx
,
533 const char *sys_name
, const char *evt_name
,
534 struct parse_events_error
*err
,
535 struct list_head
*head_config
)
538 struct dirent
*evt_ent
;
540 int ret
= 0, found
= 0;
542 evt_path
= get_events_file(sys_name
);
544 tracepoint_error(err
, errno
, sys_name
, evt_name
);
547 evt_dir
= opendir(evt_path
);
549 put_events_file(evt_path
);
550 tracepoint_error(err
, errno
, sys_name
, evt_name
);
554 while (!ret
&& (evt_ent
= readdir(evt_dir
))) {
555 if (!strcmp(evt_ent
->d_name
, ".")
556 || !strcmp(evt_ent
->d_name
, "..")
557 || !strcmp(evt_ent
->d_name
, "enable")
558 || !strcmp(evt_ent
->d_name
, "filter"))
561 if (!strglobmatch(evt_ent
->d_name
, evt_name
))
566 ret
= add_tracepoint(list
, idx
, sys_name
, evt_ent
->d_name
,
571 tracepoint_error(err
, ENOENT
, sys_name
, evt_name
);
575 put_events_file(evt_path
);
580 static int add_tracepoint_event(struct list_head
*list
, int *idx
,
581 const char *sys_name
, const char *evt_name
,
582 struct parse_events_error
*err
,
583 struct list_head
*head_config
)
585 return strpbrk(evt_name
, "*?") ?
586 add_tracepoint_multi_event(list
, idx
, sys_name
, evt_name
,
588 add_tracepoint(list
, idx
, sys_name
, evt_name
,
592 static int add_tracepoint_multi_sys(struct list_head
*list
, int *idx
,
593 const char *sys_name
, const char *evt_name
,
594 struct parse_events_error
*err
,
595 struct list_head
*head_config
)
597 struct dirent
*events_ent
;
601 events_dir
= tracing_events__opendir();
603 tracepoint_error(err
, errno
, sys_name
, evt_name
);
607 while (!ret
&& (events_ent
= readdir(events_dir
))) {
608 if (!strcmp(events_ent
->d_name
, ".")
609 || !strcmp(events_ent
->d_name
, "..")
610 || !strcmp(events_ent
->d_name
, "enable")
611 || !strcmp(events_ent
->d_name
, "header_event")
612 || !strcmp(events_ent
->d_name
, "header_page"))
615 if (!strglobmatch(events_ent
->d_name
, sys_name
))
618 ret
= add_tracepoint_event(list
, idx
, events_ent
->d_name
,
619 evt_name
, err
, head_config
);
622 closedir(events_dir
);
626 struct __add_bpf_event_param
{
627 struct parse_events_state
*parse_state
;
628 struct list_head
*list
;
629 struct list_head
*head_config
;
632 static int add_bpf_event(const char *group
, const char *event
, int fd
,
635 LIST_HEAD(new_evsels
);
636 struct __add_bpf_event_param
*param
= _param
;
637 struct parse_events_state
*parse_state
= param
->parse_state
;
638 struct list_head
*list
= param
->list
;
639 struct perf_evsel
*pos
;
642 pr_debug("add bpf event %s:%s and attach bpf program %d\n",
645 err
= parse_events_add_tracepoint(&new_evsels
, &parse_state
->idx
, group
,
646 event
, parse_state
->error
,
649 struct perf_evsel
*evsel
, *tmp
;
651 pr_debug("Failed to add BPF event %s:%s\n",
653 list_for_each_entry_safe(evsel
, tmp
, &new_evsels
, node
) {
654 list_del(&evsel
->node
);
655 perf_evsel__delete(evsel
);
659 pr_debug("adding %s:%s\n", group
, event
);
661 list_for_each_entry(pos
, &new_evsels
, node
) {
662 pr_debug("adding %s:%s to %p\n",
666 list_splice(&new_evsels
, list
);
670 int parse_events_load_bpf_obj(struct parse_events_state
*parse_state
,
671 struct list_head
*list
,
672 struct bpf_object
*obj
,
673 struct list_head
*head_config
)
677 struct __add_bpf_event_param param
= {parse_state
, list
, head_config
};
678 static bool registered_unprobe_atexit
= false;
680 if (IS_ERR(obj
) || !obj
) {
681 snprintf(errbuf
, sizeof(errbuf
),
682 "Internal error: load bpf obj with NULL");
688 * Register atexit handler before calling bpf__probe() so
689 * bpf__probe() don't need to unprobe probe points its already
690 * created when failure.
692 if (!registered_unprobe_atexit
) {
694 registered_unprobe_atexit
= true;
697 err
= bpf__probe(obj
);
699 bpf__strerror_probe(obj
, err
, errbuf
, sizeof(errbuf
));
703 err
= bpf__load(obj
);
705 bpf__strerror_load(obj
, err
, errbuf
, sizeof(errbuf
));
709 err
= bpf__foreach_event(obj
, add_bpf_event
, ¶m
);
711 snprintf(errbuf
, sizeof(errbuf
),
712 "Attach events in BPF object failed");
718 parse_state
->error
->help
= strdup("(add -v to see detail)");
719 parse_state
->error
->str
= strdup(errbuf
);
724 parse_events_config_bpf(struct parse_events_state
*parse_state
,
725 struct bpf_object
*obj
,
726 struct list_head
*head_config
)
728 struct parse_events_term
*term
;
731 if (!head_config
|| list_empty(head_config
))
734 list_for_each_entry(term
, head_config
, list
) {
738 if (term
->type_term
!= PARSE_EVENTS__TERM_TYPE_USER
) {
739 snprintf(errbuf
, sizeof(errbuf
),
740 "Invalid config term for BPF object");
741 errbuf
[BUFSIZ
- 1] = '\0';
743 parse_state
->error
->idx
= term
->err_term
;
744 parse_state
->error
->str
= strdup(errbuf
);
748 err
= bpf__config_obj(obj
, term
, parse_state
->evlist
, &error_pos
);
750 bpf__strerror_config_obj(obj
, term
, parse_state
->evlist
,
751 &error_pos
, err
, errbuf
,
753 parse_state
->error
->help
= strdup(
754 "Hint:\tValid config terms:\n"
755 " \tmap:[<arraymap>].value<indices>=[value]\n"
756 " \tmap:[<eventmap>].event<indices>=[event]\n"
758 " \twhere <indices> is something like [0,3...5] or [all]\n"
759 " \t(add -v to see detail)");
760 parse_state
->error
->str
= strdup(errbuf
);
761 if (err
== -BPF_LOADER_ERRNO__OBJCONF_MAP_VALUE
)
762 parse_state
->error
->idx
= term
->err_val
;
764 parse_state
->error
->idx
= term
->err_term
+ error_pos
;
772 * Split config terms:
773 * perf record -e bpf.c/call-graph=fp,map:array.value[0]=1/ ...
774 * 'call-graph=fp' is 'evt config', should be applied to each
776 * 'map:array.value[0]=1' is 'obj config', should be processed
777 * with parse_events_config_bpf.
779 * Move object config terms from the first list to obj_head_config.
782 split_bpf_config_terms(struct list_head
*evt_head_config
,
783 struct list_head
*obj_head_config
)
785 struct parse_events_term
*term
, *temp
;
788 * Currectly, all possible user config term
789 * belong to bpf object. parse_events__is_hardcoded_term()
790 * happends to be a good flag.
792 * See parse_events_config_bpf() and
793 * config_term_tracepoint().
795 list_for_each_entry_safe(term
, temp
, evt_head_config
, list
)
796 if (!parse_events__is_hardcoded_term(term
))
797 list_move_tail(&term
->list
, obj_head_config
);
800 int parse_events_load_bpf(struct parse_events_state
*parse_state
,
801 struct list_head
*list
,
804 struct list_head
*head_config
)
807 struct bpf_object
*obj
;
808 LIST_HEAD(obj_head_config
);
811 split_bpf_config_terms(head_config
, &obj_head_config
);
813 obj
= bpf__prepare_load(bpf_file_name
, source
);
820 snprintf(errbuf
, sizeof(errbuf
),
821 "BPF support is not compiled");
823 bpf__strerror_prepare_load(bpf_file_name
,
828 parse_state
->error
->help
= strdup("(add -v to see detail)");
829 parse_state
->error
->str
= strdup(errbuf
);
833 err
= parse_events_load_bpf_obj(parse_state
, list
, obj
, head_config
);
836 err
= parse_events_config_bpf(parse_state
, obj
, &obj_head_config
);
839 * Caller doesn't know anything about obj_head_config,
840 * so combine them together again before returnning.
843 list_splice_tail(&obj_head_config
, head_config
);
848 parse_breakpoint_type(const char *type
, struct perf_event_attr
*attr
)
852 for (i
= 0; i
< 3; i
++) {
853 if (!type
|| !type
[i
])
856 #define CHECK_SET_TYPE(bit) \
858 if (attr->bp_type & bit) \
861 attr->bp_type |= bit; \
866 CHECK_SET_TYPE(HW_BREAKPOINT_R
);
869 CHECK_SET_TYPE(HW_BREAKPOINT_W
);
872 CHECK_SET_TYPE(HW_BREAKPOINT_X
);
879 #undef CHECK_SET_TYPE
881 if (!attr
->bp_type
) /* Default */
882 attr
->bp_type
= HW_BREAKPOINT_R
| HW_BREAKPOINT_W
;
887 int parse_events_add_breakpoint(struct list_head
*list
, int *idx
,
888 void *ptr
, char *type
, u64 len
)
890 struct perf_event_attr attr
;
892 memset(&attr
, 0, sizeof(attr
));
893 attr
.bp_addr
= (unsigned long) ptr
;
895 if (parse_breakpoint_type(type
, &attr
))
898 /* Provide some defaults if len is not specified */
900 if (attr
.bp_type
== HW_BREAKPOINT_X
)
903 len
= HW_BREAKPOINT_LEN_4
;
908 attr
.type
= PERF_TYPE_BREAKPOINT
;
909 attr
.sample_period
= 1;
911 return add_event(list
, idx
, &attr
, NULL
, NULL
);
914 static int check_type_val(struct parse_events_term
*term
,
915 struct parse_events_error
*err
,
918 if (type
== term
->type_val
)
922 err
->idx
= term
->err_val
;
923 if (type
== PARSE_EVENTS__TERM_TYPE_NUM
)
924 err
->str
= strdup("expected numeric value");
926 err
->str
= strdup("expected string value");
932 * Update according to parse-events.l
934 static const char *config_term_names
[__PARSE_EVENTS__TERM_TYPE_NR
] = {
935 [PARSE_EVENTS__TERM_TYPE_USER
] = "<sysfs term>",
936 [PARSE_EVENTS__TERM_TYPE_CONFIG
] = "config",
937 [PARSE_EVENTS__TERM_TYPE_CONFIG1
] = "config1",
938 [PARSE_EVENTS__TERM_TYPE_CONFIG2
] = "config2",
939 [PARSE_EVENTS__TERM_TYPE_NAME
] = "name",
940 [PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD
] = "period",
941 [PARSE_EVENTS__TERM_TYPE_SAMPLE_FREQ
] = "freq",
942 [PARSE_EVENTS__TERM_TYPE_BRANCH_SAMPLE_TYPE
] = "branch_type",
943 [PARSE_EVENTS__TERM_TYPE_TIME
] = "time",
944 [PARSE_EVENTS__TERM_TYPE_CALLGRAPH
] = "call-graph",
945 [PARSE_EVENTS__TERM_TYPE_STACKSIZE
] = "stack-size",
946 [PARSE_EVENTS__TERM_TYPE_NOINHERIT
] = "no-inherit",
947 [PARSE_EVENTS__TERM_TYPE_INHERIT
] = "inherit",
948 [PARSE_EVENTS__TERM_TYPE_MAX_STACK
] = "max-stack",
949 [PARSE_EVENTS__TERM_TYPE_MAX_EVENTS
] = "nr",
950 [PARSE_EVENTS__TERM_TYPE_OVERWRITE
] = "overwrite",
951 [PARSE_EVENTS__TERM_TYPE_NOOVERWRITE
] = "no-overwrite",
952 [PARSE_EVENTS__TERM_TYPE_DRV_CFG
] = "driver-config",
953 [PARSE_EVENTS__TERM_TYPE_PERCORE
] = "percore",
956 static bool config_term_shrinked
;
959 config_term_avail(int term_type
, struct parse_events_error
*err
)
961 if (term_type
< 0 || term_type
>= __PARSE_EVENTS__TERM_TYPE_NR
) {
962 err
->str
= strdup("Invalid term_type");
965 if (!config_term_shrinked
)
969 case PARSE_EVENTS__TERM_TYPE_CONFIG
:
970 case PARSE_EVENTS__TERM_TYPE_CONFIG1
:
971 case PARSE_EVENTS__TERM_TYPE_CONFIG2
:
972 case PARSE_EVENTS__TERM_TYPE_NAME
:
973 case PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD
:
974 case PARSE_EVENTS__TERM_TYPE_PERCORE
:
980 /* term_type is validated so indexing is safe */
981 if (asprintf(&err
->str
, "'%s' is not usable in 'perf stat'",
982 config_term_names
[term_type
]) < 0)
988 void parse_events__shrink_config_terms(void)
990 config_term_shrinked
= true;
993 static int config_term_common(struct perf_event_attr
*attr
,
994 struct parse_events_term
*term
,
995 struct parse_events_error
*err
)
997 #define CHECK_TYPE_VAL(type) \
999 if (check_type_val(term, err, PARSE_EVENTS__TERM_TYPE_ ## type)) \
1003 switch (term
->type_term
) {
1004 case PARSE_EVENTS__TERM_TYPE_CONFIG
:
1005 CHECK_TYPE_VAL(NUM
);
1006 attr
->config
= term
->val
.num
;
1008 case PARSE_EVENTS__TERM_TYPE_CONFIG1
:
1009 CHECK_TYPE_VAL(NUM
);
1010 attr
->config1
= term
->val
.num
;
1012 case PARSE_EVENTS__TERM_TYPE_CONFIG2
:
1013 CHECK_TYPE_VAL(NUM
);
1014 attr
->config2
= term
->val
.num
;
1016 case PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD
:
1017 CHECK_TYPE_VAL(NUM
);
1019 case PARSE_EVENTS__TERM_TYPE_SAMPLE_FREQ
:
1020 CHECK_TYPE_VAL(NUM
);
1022 case PARSE_EVENTS__TERM_TYPE_BRANCH_SAMPLE_TYPE
:
1023 CHECK_TYPE_VAL(STR
);
1024 if (strcmp(term
->val
.str
, "no") &&
1025 parse_branch_str(term
->val
.str
, &attr
->branch_sample_type
)) {
1026 err
->str
= strdup("invalid branch sample type");
1027 err
->idx
= term
->err_val
;
1031 case PARSE_EVENTS__TERM_TYPE_TIME
:
1032 CHECK_TYPE_VAL(NUM
);
1033 if (term
->val
.num
> 1) {
1034 err
->str
= strdup("expected 0 or 1");
1035 err
->idx
= term
->err_val
;
1039 case PARSE_EVENTS__TERM_TYPE_CALLGRAPH
:
1040 CHECK_TYPE_VAL(STR
);
1042 case PARSE_EVENTS__TERM_TYPE_STACKSIZE
:
1043 CHECK_TYPE_VAL(NUM
);
1045 case PARSE_EVENTS__TERM_TYPE_INHERIT
:
1046 CHECK_TYPE_VAL(NUM
);
1048 case PARSE_EVENTS__TERM_TYPE_NOINHERIT
:
1049 CHECK_TYPE_VAL(NUM
);
1051 case PARSE_EVENTS__TERM_TYPE_OVERWRITE
:
1052 CHECK_TYPE_VAL(NUM
);
1054 case PARSE_EVENTS__TERM_TYPE_NOOVERWRITE
:
1055 CHECK_TYPE_VAL(NUM
);
1057 case PARSE_EVENTS__TERM_TYPE_NAME
:
1058 CHECK_TYPE_VAL(STR
);
1060 case PARSE_EVENTS__TERM_TYPE_MAX_STACK
:
1061 CHECK_TYPE_VAL(NUM
);
1063 case PARSE_EVENTS__TERM_TYPE_MAX_EVENTS
:
1064 CHECK_TYPE_VAL(NUM
);
1066 case PARSE_EVENTS__TERM_TYPE_PERCORE
:
1067 CHECK_TYPE_VAL(NUM
);
1068 if ((unsigned int)term
->val
.num
> 1) {
1069 err
->str
= strdup("expected 0 or 1");
1070 err
->idx
= term
->err_val
;
1075 err
->str
= strdup("unknown term");
1076 err
->idx
= term
->err_term
;
1077 err
->help
= parse_events_formats_error_string(NULL
);
1082 * Check term availbility after basic checking so
1083 * PARSE_EVENTS__TERM_TYPE_USER can be found and filtered.
1085 * If check availbility at the entry of this function,
1086 * user will see "'<sysfs term>' is not usable in 'perf stat'"
1087 * if an invalid config term is provided for legacy events
1088 * (for example, instructions/badterm/...), which is confusing.
1090 if (!config_term_avail(term
->type_term
, err
))
1093 #undef CHECK_TYPE_VAL
1096 static int config_term_pmu(struct perf_event_attr
*attr
,
1097 struct parse_events_term
*term
,
1098 struct parse_events_error
*err
)
1100 if (term
->type_term
== PARSE_EVENTS__TERM_TYPE_USER
||
1101 term
->type_term
== PARSE_EVENTS__TERM_TYPE_DRV_CFG
)
1103 * Always succeed for sysfs terms, as we dont know
1104 * at this point what type they need to have.
1108 return config_term_common(attr
, term
, err
);
1111 static int config_term_tracepoint(struct perf_event_attr
*attr
,
1112 struct parse_events_term
*term
,
1113 struct parse_events_error
*err
)
1115 switch (term
->type_term
) {
1116 case PARSE_EVENTS__TERM_TYPE_CALLGRAPH
:
1117 case PARSE_EVENTS__TERM_TYPE_STACKSIZE
:
1118 case PARSE_EVENTS__TERM_TYPE_INHERIT
:
1119 case PARSE_EVENTS__TERM_TYPE_NOINHERIT
:
1120 case PARSE_EVENTS__TERM_TYPE_MAX_STACK
:
1121 case PARSE_EVENTS__TERM_TYPE_MAX_EVENTS
:
1122 case PARSE_EVENTS__TERM_TYPE_OVERWRITE
:
1123 case PARSE_EVENTS__TERM_TYPE_NOOVERWRITE
:
1124 return config_term_common(attr
, term
, err
);
1127 err
->idx
= term
->err_term
;
1128 err
->str
= strdup("unknown term");
1129 err
->help
= strdup("valid terms: call-graph,stack-size\n");
1137 static int config_attr(struct perf_event_attr
*attr
,
1138 struct list_head
*head
,
1139 struct parse_events_error
*err
,
1140 config_term_func_t config_term
)
1142 struct parse_events_term
*term
;
1144 list_for_each_entry(term
, head
, list
)
1145 if (config_term(attr
, term
, err
))
1151 static int get_config_terms(struct list_head
*head_config
,
1152 struct list_head
*head_terms __maybe_unused
)
1154 #define ADD_CONFIG_TERM(__type, __name, __val) \
1156 struct perf_evsel_config_term *__t; \
1158 __t = zalloc(sizeof(*__t)); \
1162 INIT_LIST_HEAD(&__t->list); \
1163 __t->type = PERF_EVSEL__CONFIG_TERM_ ## __type; \
1164 __t->val.__name = __val; \
1165 __t->weak = term->weak; \
1166 list_add_tail(&__t->list, head_terms); \
1169 struct parse_events_term
*term
;
1171 list_for_each_entry(term
, head_config
, list
) {
1172 switch (term
->type_term
) {
1173 case PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD
:
1174 ADD_CONFIG_TERM(PERIOD
, period
, term
->val
.num
);
1176 case PARSE_EVENTS__TERM_TYPE_SAMPLE_FREQ
:
1177 ADD_CONFIG_TERM(FREQ
, freq
, term
->val
.num
);
1179 case PARSE_EVENTS__TERM_TYPE_TIME
:
1180 ADD_CONFIG_TERM(TIME
, time
, term
->val
.num
);
1182 case PARSE_EVENTS__TERM_TYPE_CALLGRAPH
:
1183 ADD_CONFIG_TERM(CALLGRAPH
, callgraph
, term
->val
.str
);
1185 case PARSE_EVENTS__TERM_TYPE_BRANCH_SAMPLE_TYPE
:
1186 ADD_CONFIG_TERM(BRANCH
, branch
, term
->val
.str
);
1188 case PARSE_EVENTS__TERM_TYPE_STACKSIZE
:
1189 ADD_CONFIG_TERM(STACK_USER
, stack_user
, term
->val
.num
);
1191 case PARSE_EVENTS__TERM_TYPE_INHERIT
:
1192 ADD_CONFIG_TERM(INHERIT
, inherit
, term
->val
.num
? 1 : 0);
1194 case PARSE_EVENTS__TERM_TYPE_NOINHERIT
:
1195 ADD_CONFIG_TERM(INHERIT
, inherit
, term
->val
.num
? 0 : 1);
1197 case PARSE_EVENTS__TERM_TYPE_MAX_STACK
:
1198 ADD_CONFIG_TERM(MAX_STACK
, max_stack
, term
->val
.num
);
1200 case PARSE_EVENTS__TERM_TYPE_MAX_EVENTS
:
1201 ADD_CONFIG_TERM(MAX_EVENTS
, max_events
, term
->val
.num
);
1203 case PARSE_EVENTS__TERM_TYPE_OVERWRITE
:
1204 ADD_CONFIG_TERM(OVERWRITE
, overwrite
, term
->val
.num
? 1 : 0);
1206 case PARSE_EVENTS__TERM_TYPE_NOOVERWRITE
:
1207 ADD_CONFIG_TERM(OVERWRITE
, overwrite
, term
->val
.num
? 0 : 1);
1209 case PARSE_EVENTS__TERM_TYPE_DRV_CFG
:
1210 ADD_CONFIG_TERM(DRV_CFG
, drv_cfg
, term
->val
.str
);
1212 case PARSE_EVENTS__TERM_TYPE_PERCORE
:
1213 ADD_CONFIG_TERM(PERCORE
, percore
,
1214 term
->val
.num
? true : false);
1220 #undef ADD_EVSEL_CONFIG
1224 int parse_events_add_tracepoint(struct list_head
*list
, int *idx
,
1225 const char *sys
, const char *event
,
1226 struct parse_events_error
*err
,
1227 struct list_head
*head_config
)
1230 struct perf_event_attr attr
;
1232 if (config_attr(&attr
, head_config
, err
,
1233 config_term_tracepoint
))
1237 if (strpbrk(sys
, "*?"))
1238 return add_tracepoint_multi_sys(list
, idx
, sys
, event
,
1241 return add_tracepoint_event(list
, idx
, sys
, event
,
1245 int parse_events_add_numeric(struct parse_events_state
*parse_state
,
1246 struct list_head
*list
,
1247 u32 type
, u64 config
,
1248 struct list_head
*head_config
)
1250 struct perf_event_attr attr
;
1251 LIST_HEAD(config_terms
);
1253 memset(&attr
, 0, sizeof(attr
));
1255 attr
.config
= config
;
1258 if (config_attr(&attr
, head_config
, parse_state
->error
,
1259 config_term_common
))
1262 if (get_config_terms(head_config
, &config_terms
))
1266 return add_event(list
, &parse_state
->idx
, &attr
,
1267 get_config_name(head_config
), &config_terms
);
1270 int parse_events_add_tool(struct parse_events_state
*parse_state
,
1271 struct list_head
*list
,
1272 enum perf_tool_event tool_event
)
1274 return add_event_tool(list
, &parse_state
->idx
, tool_event
);
1277 static bool config_term_percore(struct list_head
*config_terms
)
1279 struct perf_evsel_config_term
*term
;
1281 list_for_each_entry(term
, config_terms
, list
) {
1282 if (term
->type
== PERF_EVSEL__CONFIG_TERM_PERCORE
)
1283 return term
->val
.percore
;
1289 int parse_events_add_pmu(struct parse_events_state
*parse_state
,
1290 struct list_head
*list
, char *name
,
1291 struct list_head
*head_config
,
1292 bool auto_merge_stats
,
1295 struct perf_event_attr attr
;
1296 struct perf_pmu_info info
;
1297 struct perf_pmu
*pmu
;
1298 struct perf_evsel
*evsel
;
1299 struct parse_events_error
*err
= parse_state
->error
;
1300 bool use_uncore_alias
;
1301 LIST_HEAD(config_terms
);
1303 pmu
= perf_pmu__find(name
);
1305 if (asprintf(&err
->str
,
1306 "Cannot find PMU `%s'. Missing kernel support?",
1312 if (pmu
->default_config
) {
1313 memcpy(&attr
, pmu
->default_config
,
1314 sizeof(struct perf_event_attr
));
1316 memset(&attr
, 0, sizeof(attr
));
1319 use_uncore_alias
= (pmu
->is_uncore
&& use_alias
);
1322 attr
.type
= pmu
->type
;
1323 evsel
= __add_event(list
, &parse_state
->idx
, &attr
, NULL
, pmu
, NULL
,
1324 auto_merge_stats
, NULL
);
1326 evsel
->pmu_name
= name
;
1327 evsel
->use_uncore_alias
= use_uncore_alias
;
1334 if (perf_pmu__check_alias(pmu
, head_config
, &info
))
1338 * Configure hardcoded terms first, no need to check
1339 * return value when called with fail == 0 ;)
1341 if (config_attr(&attr
, head_config
, parse_state
->error
, config_term_pmu
))
1344 if (get_config_terms(head_config
, &config_terms
))
1347 if (perf_pmu__config(pmu
, &attr
, head_config
, parse_state
->error
))
1350 evsel
= __add_event(list
, &parse_state
->idx
, &attr
,
1351 get_config_name(head_config
), pmu
,
1352 &config_terms
, auto_merge_stats
, NULL
);
1354 evsel
->unit
= info
.unit
;
1355 evsel
->scale
= info
.scale
;
1356 evsel
->per_pkg
= info
.per_pkg
;
1357 evsel
->snapshot
= info
.snapshot
;
1358 evsel
->metric_expr
= info
.metric_expr
;
1359 evsel
->metric_name
= info
.metric_name
;
1360 evsel
->pmu_name
= name
;
1361 evsel
->use_uncore_alias
= use_uncore_alias
;
1362 evsel
->percore
= config_term_percore(&evsel
->config_terms
);
1365 return evsel
? 0 : -ENOMEM
;
1368 int parse_events_multi_pmu_add(struct parse_events_state
*parse_state
,
1369 char *str
, struct list_head
**listp
)
1371 struct list_head
*head
;
1372 struct parse_events_term
*term
;
1373 struct list_head
*list
;
1374 struct perf_pmu
*pmu
= NULL
;
1378 /* Add it for all PMUs that support the alias */
1379 list
= malloc(sizeof(struct list_head
));
1382 INIT_LIST_HEAD(list
);
1383 while ((pmu
= perf_pmu__scan(pmu
)) != NULL
) {
1384 struct perf_pmu_alias
*alias
;
1386 list_for_each_entry(alias
, &pmu
->aliases
, list
) {
1387 if (!strcasecmp(alias
->name
, str
)) {
1388 head
= malloc(sizeof(struct list_head
));
1391 INIT_LIST_HEAD(head
);
1392 if (parse_events_term__num(&term
, PARSE_EVENTS__TERM_TYPE_USER
,
1393 str
, 1, false, &str
, NULL
) < 0)
1395 list_add_tail(&term
->list
, head
);
1397 if (!parse_events_add_pmu(parse_state
, list
,
1400 pr_debug("%s -> %s/%s/\n", str
,
1401 pmu
->name
, alias
->str
);
1405 parse_events_terms__delete(head
);
1415 int parse_events__modifier_group(struct list_head
*list
,
1418 return parse_events__modifier_event(list
, event_mod
, true);
1422 * Check if the two uncore PMUs are from the same uncore block
1423 * The format of the uncore PMU name is uncore_#blockname_#pmuidx
1425 static bool is_same_uncore_block(const char *pmu_name_a
, const char *pmu_name_b
)
1427 char *end_a
, *end_b
;
1429 end_a
= strrchr(pmu_name_a
, '_');
1430 end_b
= strrchr(pmu_name_b
, '_');
1432 if (!end_a
|| !end_b
)
1435 if ((end_a
- pmu_name_a
) != (end_b
- pmu_name_b
))
1438 return (strncmp(pmu_name_a
, pmu_name_b
, end_a
- pmu_name_a
) == 0);
1442 parse_events__set_leader_for_uncore_aliase(char *name
, struct list_head
*list
,
1443 struct parse_events_state
*parse_state
)
1445 struct perf_evsel
*evsel
, *leader
;
1447 bool is_leader
= true;
1448 int i
, nr_pmu
= 0, total_members
, ret
= 0;
1450 leader
= list_first_entry(list
, struct perf_evsel
, node
);
1451 evsel
= list_last_entry(list
, struct perf_evsel
, node
);
1452 total_members
= evsel
->idx
- leader
->idx
+ 1;
1454 leaders
= calloc(total_members
, sizeof(uintptr_t));
1455 if (WARN_ON(!leaders
))
1459 * Going through the whole group and doing sanity check.
1460 * All members must use alias, and be from the same uncore block.
1461 * Also, storing the leader events in an array.
1463 __evlist__for_each_entry(list
, evsel
) {
1465 /* Only split the uncore group which members use alias */
1466 if (!evsel
->use_uncore_alias
)
1469 /* The events must be from the same uncore block */
1470 if (!is_same_uncore_block(leader
->pmu_name
, evsel
->pmu_name
))
1476 * If the event's PMU name starts to repeat, it must be a new
1477 * event. That can be used to distinguish the leader from
1478 * other members, even they have the same event name.
1480 if ((leader
!= evsel
) && (leader
->pmu_name
== evsel
->pmu_name
)) {
1484 /* The name is always alias name */
1485 WARN_ON(strcmp(leader
->name
, evsel
->name
));
1487 /* Store the leader event for each PMU */
1488 leaders
[nr_pmu
++] = (uintptr_t) evsel
;
1491 /* only one event alias */
1492 if (nr_pmu
== total_members
) {
1493 parse_state
->nr_groups
--;
1498 * An uncore event alias is a joint name which means the same event
1499 * runs on all PMUs of a block.
1500 * Perf doesn't support mixed events from different PMUs in the same
1501 * group. The big group has to be split into multiple small groups
1502 * which only include the events from the same PMU.
1504 * Here the uncore event aliases must be from the same uncore block.
1505 * The number of PMUs must be same for each alias. The number of new
1506 * small groups equals to the number of PMUs.
1507 * Setting the leader event for corresponding members in each group.
1510 __evlist__for_each_entry(list
, evsel
) {
1513 evsel
->leader
= (struct perf_evsel
*) leaders
[i
++];
1516 /* The number of members and group name are same for each group */
1517 for (i
= 0; i
< nr_pmu
; i
++) {
1518 evsel
= (struct perf_evsel
*) leaders
[i
];
1519 evsel
->nr_members
= total_members
/ nr_pmu
;
1520 evsel
->group_name
= name
? strdup(name
) : NULL
;
1523 /* Take the new small groups into account */
1524 parse_state
->nr_groups
+= nr_pmu
- 1;
1533 void parse_events__set_leader(char *name
, struct list_head
*list
,
1534 struct parse_events_state
*parse_state
)
1536 struct perf_evsel
*leader
;
1538 if (list_empty(list
)) {
1539 WARN_ONCE(true, "WARNING: failed to set leader: empty list");
1543 if (parse_events__set_leader_for_uncore_aliase(name
, list
, parse_state
))
1546 __perf_evlist__set_leader(list
);
1547 leader
= list_entry(list
->next
, struct perf_evsel
, node
);
1548 leader
->group_name
= name
? strdup(name
) : NULL
;
1551 /* list_event is assumed to point to malloc'ed memory */
1552 void parse_events_update_lists(struct list_head
*list_event
,
1553 struct list_head
*list_all
)
1556 * Called for single event definition. Update the
1557 * 'all event' list, and reinit the 'single event'
1558 * list, for next event definition.
1560 list_splice_tail(list_event
, list_all
);
1564 struct event_modifier
{
1579 static int get_event_modifier(struct event_modifier
*mod
, char *str
,
1580 struct perf_evsel
*evsel
)
1582 int eu
= evsel
? evsel
->attr
.exclude_user
: 0;
1583 int ek
= evsel
? evsel
->attr
.exclude_kernel
: 0;
1584 int eh
= evsel
? evsel
->attr
.exclude_hv
: 0;
1585 int eH
= evsel
? evsel
->attr
.exclude_host
: 0;
1586 int eG
= evsel
? evsel
->attr
.exclude_guest
: 0;
1587 int eI
= evsel
? evsel
->attr
.exclude_idle
: 0;
1588 int precise
= evsel
? evsel
->attr
.precise_ip
: 0;
1589 int precise_max
= 0;
1590 int sample_read
= 0;
1591 int pinned
= evsel
? evsel
->attr
.pinned
: 0;
1593 int exclude
= eu
| ek
| eh
;
1594 int exclude_GH
= evsel
? evsel
->exclude_GH
: 0;
1597 memset(mod
, 0, sizeof(*mod
));
1602 exclude
= eu
= ek
= eh
= 1;
1604 } else if (*str
== 'k') {
1606 exclude
= eu
= ek
= eh
= 1;
1608 } else if (*str
== 'h') {
1610 exclude
= eu
= ek
= eh
= 1;
1612 } else if (*str
== 'G') {
1614 exclude_GH
= eG
= eH
= 1;
1616 } else if (*str
== 'H') {
1618 exclude_GH
= eG
= eH
= 1;
1620 } else if (*str
== 'I') {
1622 } else if (*str
== 'p') {
1624 /* use of precise requires exclude_guest */
1627 } else if (*str
== 'P') {
1629 } else if (*str
== 'S') {
1631 } else if (*str
== 'D') {
1633 } else if (*str
== 'W') {
1644 * 0 - SAMPLE_IP can have arbitrary skid
1645 * 1 - SAMPLE_IP must have constant skid
1646 * 2 - SAMPLE_IP requested to have 0 skid
1647 * 3 - SAMPLE_IP must have 0 skid
1649 * See also PERF_RECORD_MISC_EXACT_IP
1660 mod
->precise
= precise
;
1661 mod
->precise_max
= precise_max
;
1662 mod
->exclude_GH
= exclude_GH
;
1663 mod
->sample_read
= sample_read
;
1664 mod
->pinned
= pinned
;
1671 * Basic modifier sanity check to validate it contains only one
1672 * instance of any modifier (apart from 'p') present.
1674 static int check_modifier(char *str
)
1678 /* The sizeof includes 0 byte as well. */
1679 if (strlen(str
) > (sizeof("ukhGHpppPSDIW") - 1))
1683 if (*p
!= 'p' && strchr(p
+ 1, *p
))
1691 int parse_events__modifier_event(struct list_head
*list
, char *str
, bool add
)
1693 struct perf_evsel
*evsel
;
1694 struct event_modifier mod
;
1699 if (check_modifier(str
))
1702 if (!add
&& get_event_modifier(&mod
, str
, NULL
))
1705 __evlist__for_each_entry(list
, evsel
) {
1706 if (add
&& get_event_modifier(&mod
, str
, evsel
))
1709 evsel
->attr
.exclude_user
= mod
.eu
;
1710 evsel
->attr
.exclude_kernel
= mod
.ek
;
1711 evsel
->attr
.exclude_hv
= mod
.eh
;
1712 evsel
->attr
.precise_ip
= mod
.precise
;
1713 evsel
->attr
.exclude_host
= mod
.eH
;
1714 evsel
->attr
.exclude_guest
= mod
.eG
;
1715 evsel
->attr
.exclude_idle
= mod
.eI
;
1716 evsel
->exclude_GH
= mod
.exclude_GH
;
1717 evsel
->sample_read
= mod
.sample_read
;
1718 evsel
->precise_max
= mod
.precise_max
;
1719 evsel
->weak_group
= mod
.weak
;
1721 if (perf_evsel__is_group_leader(evsel
))
1722 evsel
->attr
.pinned
= mod
.pinned
;
1728 int parse_events_name(struct list_head
*list
, char *name
)
1730 struct perf_evsel
*evsel
;
1732 __evlist__for_each_entry(list
, evsel
) {
1734 evsel
->name
= strdup(name
);
1741 comp_pmu(const void *p1
, const void *p2
)
1743 struct perf_pmu_event_symbol
*pmu1
= (struct perf_pmu_event_symbol
*) p1
;
1744 struct perf_pmu_event_symbol
*pmu2
= (struct perf_pmu_event_symbol
*) p2
;
1746 return strcasecmp(pmu1
->symbol
, pmu2
->symbol
);
1749 static void perf_pmu__parse_cleanup(void)
1751 if (perf_pmu_events_list_num
> 0) {
1752 struct perf_pmu_event_symbol
*p
;
1755 for (i
= 0; i
< perf_pmu_events_list_num
; i
++) {
1756 p
= perf_pmu_events_list
+ i
;
1759 zfree(&perf_pmu_events_list
);
1760 perf_pmu_events_list_num
= 0;
1764 #define SET_SYMBOL(str, stype) \
1773 * Read the pmu events list from sysfs
1774 * Save it into perf_pmu_events_list
1776 static void perf_pmu__parse_init(void)
1779 struct perf_pmu
*pmu
= NULL
;
1780 struct perf_pmu_alias
*alias
;
1784 while ((pmu
= perf_pmu__scan(pmu
)) != NULL
) {
1785 list_for_each_entry(alias
, &pmu
->aliases
, list
) {
1786 if (strchr(alias
->name
, '-'))
1793 perf_pmu_events_list_num
= -1;
1796 perf_pmu_events_list
= malloc(sizeof(struct perf_pmu_event_symbol
) * len
);
1797 if (!perf_pmu_events_list
)
1799 perf_pmu_events_list_num
= len
;
1803 while ((pmu
= perf_pmu__scan(pmu
)) != NULL
) {
1804 list_for_each_entry(alias
, &pmu
->aliases
, list
) {
1805 struct perf_pmu_event_symbol
*p
= perf_pmu_events_list
+ len
;
1806 char *tmp
= strchr(alias
->name
, '-');
1809 SET_SYMBOL(strndup(alias
->name
, tmp
- alias
->name
),
1810 PMU_EVENT_SYMBOL_PREFIX
);
1812 SET_SYMBOL(strdup(++tmp
), PMU_EVENT_SYMBOL_SUFFIX
);
1815 SET_SYMBOL(strdup(alias
->name
), PMU_EVENT_SYMBOL
);
1820 qsort(perf_pmu_events_list
, len
,
1821 sizeof(struct perf_pmu_event_symbol
), comp_pmu
);
1825 perf_pmu__parse_cleanup();
1828 enum perf_pmu_event_symbol_type
1829 perf_pmu__parse_check(const char *name
)
1831 struct perf_pmu_event_symbol p
, *r
;
1833 /* scan kernel pmu events from sysfs if needed */
1834 if (perf_pmu_events_list_num
== 0)
1835 perf_pmu__parse_init();
1837 * name "cpu" could be prefix of cpu-cycles or cpu// events.
1838 * cpu-cycles has been handled by hardcode.
1839 * So it must be cpu// events, not kernel pmu event.
1841 if ((perf_pmu_events_list_num
<= 0) || !strcmp(name
, "cpu"))
1842 return PMU_EVENT_SYMBOL_ERR
;
1844 p
.symbol
= strdup(name
);
1845 r
= bsearch(&p
, perf_pmu_events_list
,
1846 (size_t) perf_pmu_events_list_num
,
1847 sizeof(struct perf_pmu_event_symbol
), comp_pmu
);
1849 return r
? r
->type
: PMU_EVENT_SYMBOL_ERR
;
1852 static int parse_events__scanner(const char *str
, void *parse_state
, int start_token
)
1854 YY_BUFFER_STATE buffer
;
1858 ret
= parse_events_lex_init_extra(start_token
, &scanner
);
1862 buffer
= parse_events__scan_string(str
, scanner
);
1865 parse_events_debug
= 1;
1867 ret
= parse_events_parse(parse_state
, scanner
);
1869 parse_events__flush_buffer(buffer
, scanner
);
1870 parse_events__delete_buffer(buffer
, scanner
);
1871 parse_events_lex_destroy(scanner
);
1876 * parse event config string, return a list of event terms.
1878 int parse_events_terms(struct list_head
*terms
, const char *str
)
1880 struct parse_events_state parse_state
= {
1885 ret
= parse_events__scanner(str
, &parse_state
, PE_START_TERMS
);
1887 list_splice(parse_state
.terms
, terms
);
1888 zfree(&parse_state
.terms
);
1892 parse_events_terms__delete(parse_state
.terms
);
1896 int parse_events(struct perf_evlist
*evlist
, const char *str
,
1897 struct parse_events_error
*err
)
1899 struct parse_events_state parse_state
= {
1900 .list
= LIST_HEAD_INIT(parse_state
.list
),
1901 .idx
= evlist
->nr_entries
,
1907 ret
= parse_events__scanner(str
, &parse_state
, PE_START_EVENTS
);
1908 perf_pmu__parse_cleanup();
1910 struct perf_evsel
*last
;
1912 if (list_empty(&parse_state
.list
)) {
1913 WARN_ONCE(true, "WARNING: event parser found nothing\n");
1917 perf_evlist__splice_list_tail(evlist
, &parse_state
.list
);
1918 evlist
->nr_groups
+= parse_state
.nr_groups
;
1919 last
= perf_evlist__last(evlist
);
1920 last
->cmdline_group_boundary
= true;
1926 * There are 2 users - builtin-record and builtin-test objects.
1927 * Both call perf_evlist__delete in case of error, so we dont
1933 #define MAX_WIDTH 1000
1934 static int get_term_width(void)
1938 get_term_dimensions(&ws
);
1939 return ws
.ws_col
> MAX_WIDTH
? MAX_WIDTH
: ws
.ws_col
;
1942 void parse_events_print_error(struct parse_events_error
*err
,
1945 const char *str
= "invalid or unsupported event: ";
1946 char _buf
[MAX_WIDTH
];
1947 char *buf
= (char *) event
;
1951 /* -2 for extra '' in the final fprintf */
1952 int width
= get_term_width() - 2;
1953 int len_event
= strlen(event
);
1954 int len_str
, max_len
, cut
= 0;
1957 * Maximum error index indent, we will cut
1958 * the event string if it's bigger.
1960 int max_err_idx
= 13;
1963 * Let's be specific with the message when
1964 * we have the precise error.
1966 str
= "event syntax error: ";
1967 len_str
= strlen(str
);
1968 max_len
= width
- len_str
;
1972 /* We're cutting from the beginning. */
1973 if (err
->idx
> max_err_idx
)
1974 cut
= err
->idx
- max_err_idx
;
1976 strncpy(buf
, event
+ cut
, max_len
);
1978 /* Mark cut parts with '..' on both sides. */
1980 buf
[0] = buf
[1] = '.';
1982 if ((len_event
- cut
) > max_len
) {
1983 buf
[max_len
- 1] = buf
[max_len
- 2] = '.';
1987 idx
= len_str
+ err
->idx
- cut
;
1990 fprintf(stderr
, "%s'%s'\n", str
, buf
);
1992 fprintf(stderr
, "%*s\\___ %s\n", idx
+ 1, "", err
->str
);
1994 fprintf(stderr
, "\n%s\n", err
->help
);
2002 int parse_events_option(const struct option
*opt
, const char *str
,
2003 int unset __maybe_unused
)
2005 struct perf_evlist
*evlist
= *(struct perf_evlist
**)opt
->value
;
2006 struct parse_events_error err
= { .idx
= 0, };
2007 int ret
= parse_events(evlist
, str
, &err
);
2010 parse_events_print_error(&err
, str
);
2011 fprintf(stderr
, "Run 'perf list' for a list of valid events\n");
2018 foreach_evsel_in_last_glob(struct perf_evlist
*evlist
,
2019 int (*func
)(struct perf_evsel
*evsel
,
2023 struct perf_evsel
*last
= NULL
;
2027 * Don't return when list_empty, give func a chance to report
2028 * error when it found last == NULL.
2030 * So no need to WARN here, let *func do this.
2032 if (evlist
->nr_entries
> 0)
2033 last
= perf_evlist__last(evlist
);
2036 err
= (*func
)(last
, arg
);
2042 if (last
->node
.prev
== &evlist
->entries
)
2044 last
= list_entry(last
->node
.prev
, struct perf_evsel
, node
);
2045 } while (!last
->cmdline_group_boundary
);
2050 static int set_filter(struct perf_evsel
*evsel
, const void *arg
)
2052 const char *str
= arg
;
2054 int nr_addr_filters
= 0;
2055 struct perf_pmu
*pmu
= NULL
;
2057 if (evsel
== NULL
) {
2059 "--filter option should follow a -e tracepoint or HW tracer option\n");
2063 if (evsel
->attr
.type
== PERF_TYPE_TRACEPOINT
) {
2064 if (perf_evsel__append_tp_filter(evsel
, str
) < 0) {
2066 "not enough memory to hold filter string\n");
2073 while ((pmu
= perf_pmu__scan(pmu
)) != NULL
)
2074 if (pmu
->type
== evsel
->attr
.type
) {
2080 perf_pmu__scan_file(pmu
, "nr_addr_filters",
2081 "%d", &nr_addr_filters
);
2083 if (!nr_addr_filters
) {
2085 "This CPU does not support address filtering\n");
2089 if (perf_evsel__append_addr_filter(evsel
, str
) < 0) {
2091 "not enough memory to hold filter string\n");
2098 int parse_filter(const struct option
*opt
, const char *str
,
2099 int unset __maybe_unused
)
2101 struct perf_evlist
*evlist
= *(struct perf_evlist
**)opt
->value
;
2103 return foreach_evsel_in_last_glob(evlist
, set_filter
,
2107 static int add_exclude_perf_filter(struct perf_evsel
*evsel
,
2108 const void *arg __maybe_unused
)
2110 char new_filter
[64];
2112 if (evsel
== NULL
|| evsel
->attr
.type
!= PERF_TYPE_TRACEPOINT
) {
2114 "--exclude-perf option should follow a -e tracepoint option\n");
2118 snprintf(new_filter
, sizeof(new_filter
), "common_pid != %d", getpid());
2120 if (perf_evsel__append_tp_filter(evsel
, new_filter
) < 0) {
2122 "not enough memory to hold filter string\n");
2129 int exclude_perf(const struct option
*opt
,
2130 const char *arg __maybe_unused
,
2131 int unset __maybe_unused
)
2133 struct perf_evlist
*evlist
= *(struct perf_evlist
**)opt
->value
;
2135 return foreach_evsel_in_last_glob(evlist
, add_exclude_perf_filter
,
2139 static const char * const event_type_descriptors
[] = {
2143 "Hardware cache event",
2144 "Raw hardware event descriptor",
2145 "Hardware breakpoint",
2148 static int cmp_string(const void *a
, const void *b
)
2150 const char * const *as
= a
;
2151 const char * const *bs
= b
;
2153 return strcmp(*as
, *bs
);
2157 * Print the events from <debugfs_mount_point>/tracing/events
2160 void print_tracepoint_events(const char *subsys_glob
, const char *event_glob
,
2163 DIR *sys_dir
, *evt_dir
;
2164 struct dirent
*sys_dirent
, *evt_dirent
;
2165 char evt_path
[MAXPATHLEN
];
2167 char **evt_list
= NULL
;
2168 unsigned int evt_i
= 0, evt_num
= 0;
2169 bool evt_num_known
= false;
2172 sys_dir
= tracing_events__opendir();
2176 if (evt_num_known
) {
2177 evt_list
= zalloc(sizeof(char *) * evt_num
);
2179 goto out_close_sys_dir
;
2182 for_each_subsystem(sys_dir
, sys_dirent
) {
2183 if (subsys_glob
!= NULL
&&
2184 !strglobmatch(sys_dirent
->d_name
, subsys_glob
))
2187 dir_path
= get_events_file(sys_dirent
->d_name
);
2190 evt_dir
= opendir(dir_path
);
2194 for_each_event(dir_path
, evt_dir
, evt_dirent
) {
2195 if (event_glob
!= NULL
&&
2196 !strglobmatch(evt_dirent
->d_name
, event_glob
))
2199 if (!evt_num_known
) {
2204 snprintf(evt_path
, MAXPATHLEN
, "%s:%s",
2205 sys_dirent
->d_name
, evt_dirent
->d_name
);
2207 evt_list
[evt_i
] = strdup(evt_path
);
2208 if (evt_list
[evt_i
] == NULL
) {
2209 put_events_file(dir_path
);
2210 goto out_close_evt_dir
;
2216 put_events_file(dir_path
);
2220 if (!evt_num_known
) {
2221 evt_num_known
= true;
2224 qsort(evt_list
, evt_num
, sizeof(char *), cmp_string
);
2226 while (evt_i
< evt_num
) {
2228 printf("%s ", evt_list
[evt_i
++]);
2231 printf(" %-50s [%s]\n", evt_list
[evt_i
++],
2232 event_type_descriptors
[PERF_TYPE_TRACEPOINT
]);
2234 if (evt_num
&& pager_in_use())
2239 for (evt_i
= 0; evt_i
< evt_num
; evt_i
++)
2240 zfree(&evt_list
[evt_i
]);
2249 printf("FATAL: not enough memory to print %s\n",
2250 event_type_descriptors
[PERF_TYPE_TRACEPOINT
]);
2256 * Check whether event is in <debugfs_mount_point>/tracing/events
2259 int is_valid_tracepoint(const char *event_string
)
2261 DIR *sys_dir
, *evt_dir
;
2262 struct dirent
*sys_dirent
, *evt_dirent
;
2263 char evt_path
[MAXPATHLEN
];
2266 sys_dir
= tracing_events__opendir();
2270 for_each_subsystem(sys_dir
, sys_dirent
) {
2271 dir_path
= get_events_file(sys_dirent
->d_name
);
2274 evt_dir
= opendir(dir_path
);
2278 for_each_event(dir_path
, evt_dir
, evt_dirent
) {
2279 snprintf(evt_path
, MAXPATHLEN
, "%s:%s",
2280 sys_dirent
->d_name
, evt_dirent
->d_name
);
2281 if (!strcmp(evt_path
, event_string
)) {
2289 put_events_file(dir_path
);
2295 static bool is_event_supported(u8 type
, unsigned config
)
2299 struct perf_evsel
*evsel
;
2300 struct perf_event_attr attr
= {
2305 struct thread_map
*tmap
= thread_map__new_by_tid(0);
2310 evsel
= perf_evsel__new(&attr
);
2312 open_return
= perf_evsel__open(evsel
, NULL
, tmap
);
2313 ret
= open_return
>= 0;
2315 if (open_return
== -EACCES
) {
2317 * This happens if the paranoid value
2318 * /proc/sys/kernel/perf_event_paranoid is set to 2
2319 * Re-run with exclude_kernel set; we don't do that
2320 * by default as some ARM machines do not support it.
2323 evsel
->attr
.exclude_kernel
= 1;
2324 ret
= perf_evsel__open(evsel
, NULL
, tmap
) >= 0;
2326 perf_evsel__delete(evsel
);
2329 thread_map__put(tmap
);
2333 void print_sdt_events(const char *subsys_glob
, const char *event_glob
,
2336 struct probe_cache
*pcache
;
2337 struct probe_cache_entry
*ent
;
2338 struct strlist
*bidlist
, *sdtlist
;
2339 struct strlist_config cfg
= {.dont_dupstr
= true};
2340 struct str_node
*nd
, *nd2
;
2341 char *buf
, *path
, *ptr
= NULL
;
2342 bool show_detail
= false;
2345 sdtlist
= strlist__new(NULL
, &cfg
);
2347 pr_debug("Failed to allocate new strlist for SDT\n");
2350 bidlist
= build_id_cache__list_all(true);
2352 pr_debug("Failed to get buildids: %d\n", errno
);
2355 strlist__for_each_entry(nd
, bidlist
) {
2356 pcache
= probe_cache__new(nd
->s
, NULL
);
2359 list_for_each_entry(ent
, &pcache
->entries
, node
) {
2363 !strglobmatch(ent
->pev
.group
, subsys_glob
))
2366 !strglobmatch(ent
->pev
.event
, event_glob
))
2368 ret
= asprintf(&buf
, "%s:%s@%s", ent
->pev
.group
,
2369 ent
->pev
.event
, nd
->s
);
2371 strlist__add(sdtlist
, buf
);
2373 probe_cache__delete(pcache
);
2375 strlist__delete(bidlist
);
2377 strlist__for_each_entry(nd
, sdtlist
) {
2378 buf
= strchr(nd
->s
, '@');
2382 printf("%s ", nd
->s
);
2385 nd2
= strlist__next(nd
);
2387 ptr
= strchr(nd2
->s
, '@');
2390 if (strcmp(nd
->s
, nd2
->s
) == 0)
2394 path
= build_id_cache__origname(buf
);
2395 ret
= asprintf(&buf
, "%s@%s(%.12s)", nd
->s
, path
, buf
);
2397 printf(" %-50s [%s]\n", buf
, "SDT event");
2402 printf(" %-50s [%s]\n", nd
->s
, "SDT event");
2404 if (strcmp(nd
->s
, nd2
->s
) != 0)
2405 show_detail
= false;
2410 strlist__delete(sdtlist
);
2413 int print_hwcache_events(const char *event_glob
, bool name_only
)
2415 unsigned int type
, op
, i
, evt_i
= 0, evt_num
= 0;
2417 char **evt_list
= NULL
;
2418 bool evt_num_known
= false;
2421 if (evt_num_known
) {
2422 evt_list
= zalloc(sizeof(char *) * evt_num
);
2427 for (type
= 0; type
< PERF_COUNT_HW_CACHE_MAX
; type
++) {
2428 for (op
= 0; op
< PERF_COUNT_HW_CACHE_OP_MAX
; op
++) {
2429 /* skip invalid cache type */
2430 if (!perf_evsel__is_cache_op_valid(type
, op
))
2433 for (i
= 0; i
< PERF_COUNT_HW_CACHE_RESULT_MAX
; i
++) {
2434 __perf_evsel__hw_cache_type_op_res_name(type
, op
, i
,
2435 name
, sizeof(name
));
2436 if (event_glob
!= NULL
&& !strglobmatch(name
, event_glob
))
2439 if (!is_event_supported(PERF_TYPE_HW_CACHE
,
2440 type
| (op
<< 8) | (i
<< 16)))
2443 if (!evt_num_known
) {
2448 evt_list
[evt_i
] = strdup(name
);
2449 if (evt_list
[evt_i
] == NULL
)
2456 if (!evt_num_known
) {
2457 evt_num_known
= true;
2460 qsort(evt_list
, evt_num
, sizeof(char *), cmp_string
);
2462 while (evt_i
< evt_num
) {
2464 printf("%s ", evt_list
[evt_i
++]);
2467 printf(" %-50s [%s]\n", evt_list
[evt_i
++],
2468 event_type_descriptors
[PERF_TYPE_HW_CACHE
]);
2470 if (evt_num
&& pager_in_use())
2475 for (evt_i
= 0; evt_i
< evt_num
; evt_i
++)
2476 zfree(&evt_list
[evt_i
]);
2481 printf("FATAL: not enough memory to print %s\n", event_type_descriptors
[PERF_TYPE_HW_CACHE
]);
2487 static void print_tool_event(const char *name
, const char *event_glob
,
2490 if (event_glob
&& !strglobmatch(name
, event_glob
))
2493 printf("%s ", name
);
2495 printf(" %-50s [%s]\n", name
, "Tool event");
2499 void print_tool_events(const char *event_glob
, bool name_only
)
2501 print_tool_event("duration_time", event_glob
, name_only
);
2506 void print_symbol_events(const char *event_glob
, unsigned type
,
2507 struct event_symbol
*syms
, unsigned max
,
2510 unsigned int i
, evt_i
= 0, evt_num
= 0;
2511 char name
[MAX_NAME_LEN
];
2512 char **evt_list
= NULL
;
2513 bool evt_num_known
= false;
2516 if (evt_num_known
) {
2517 evt_list
= zalloc(sizeof(char *) * evt_num
);
2523 for (i
= 0; i
< max
; i
++, syms
++) {
2525 if (event_glob
!= NULL
&& syms
->symbol
!= NULL
&&
2526 !(strglobmatch(syms
->symbol
, event_glob
) ||
2527 (syms
->alias
&& strglobmatch(syms
->alias
, event_glob
))))
2530 if (!is_event_supported(type
, i
))
2533 if (!evt_num_known
) {
2538 if (!name_only
&& strlen(syms
->alias
))
2539 snprintf(name
, MAX_NAME_LEN
, "%s OR %s", syms
->symbol
, syms
->alias
);
2541 strlcpy(name
, syms
->symbol
, MAX_NAME_LEN
);
2543 evt_list
[evt_i
] = strdup(name
);
2544 if (evt_list
[evt_i
] == NULL
)
2549 if (!evt_num_known
) {
2550 evt_num_known
= true;
2553 qsort(evt_list
, evt_num
, sizeof(char *), cmp_string
);
2555 while (evt_i
< evt_num
) {
2557 printf("%s ", evt_list
[evt_i
++]);
2560 printf(" %-50s [%s]\n", evt_list
[evt_i
++], event_type_descriptors
[type
]);
2562 if (evt_num
&& pager_in_use())
2567 for (evt_i
= 0; evt_i
< evt_num
; evt_i
++)
2568 zfree(&evt_list
[evt_i
]);
2573 printf("FATAL: not enough memory to print %s\n", event_type_descriptors
[type
]);
2579 * Print the help text for the event symbols:
2581 void print_events(const char *event_glob
, bool name_only
, bool quiet_flag
,
2582 bool long_desc
, bool details_flag
)
2584 print_symbol_events(event_glob
, PERF_TYPE_HARDWARE
,
2585 event_symbols_hw
, PERF_COUNT_HW_MAX
, name_only
);
2587 print_symbol_events(event_glob
, PERF_TYPE_SOFTWARE
,
2588 event_symbols_sw
, PERF_COUNT_SW_MAX
, name_only
);
2589 print_tool_events(event_glob
, name_only
);
2591 print_hwcache_events(event_glob
, name_only
);
2593 print_pmu_events(event_glob
, name_only
, quiet_flag
, long_desc
,
2596 if (event_glob
!= NULL
)
2600 printf(" %-50s [%s]\n",
2602 event_type_descriptors
[PERF_TYPE_RAW
]);
2603 printf(" %-50s [%s]\n",
2604 "cpu/t1=v1[,t2=v2,t3 ...]/modifier",
2605 event_type_descriptors
[PERF_TYPE_RAW
]);
2607 printf(" (see 'man perf-list' on how to encode it)\n\n");
2609 printf(" %-50s [%s]\n",
2610 "mem:<addr>[/len][:access]",
2611 event_type_descriptors
[PERF_TYPE_BREAKPOINT
]);
2616 print_tracepoint_events(NULL
, NULL
, name_only
);
2618 print_sdt_events(NULL
, NULL
, name_only
);
2620 metricgroup__print(true, true, NULL
, name_only
, details_flag
);
2623 int parse_events__is_hardcoded_term(struct parse_events_term
*term
)
2625 return term
->type_term
!= PARSE_EVENTS__TERM_TYPE_USER
;
2628 static int new_term(struct parse_events_term
**_term
,
2629 struct parse_events_term
*temp
,
2632 struct parse_events_term
*term
;
2634 term
= malloc(sizeof(*term
));
2639 INIT_LIST_HEAD(&term
->list
);
2642 switch (term
->type_val
) {
2643 case PARSE_EVENTS__TERM_TYPE_NUM
:
2644 term
->val
.num
= num
;
2646 case PARSE_EVENTS__TERM_TYPE_STR
:
2647 term
->val
.str
= str
;
2658 int parse_events_term__num(struct parse_events_term
**term
,
2659 int type_term
, char *config
, u64 num
,
2661 void *loc_term_
, void *loc_val_
)
2663 YYLTYPE
*loc_term
= loc_term_
;
2664 YYLTYPE
*loc_val
= loc_val_
;
2666 struct parse_events_term temp
= {
2667 .type_val
= PARSE_EVENTS__TERM_TYPE_NUM
,
2668 .type_term
= type_term
,
2670 .no_value
= no_value
,
2671 .err_term
= loc_term
? loc_term
->first_column
: 0,
2672 .err_val
= loc_val
? loc_val
->first_column
: 0,
2675 return new_term(term
, &temp
, NULL
, num
);
2678 int parse_events_term__str(struct parse_events_term
**term
,
2679 int type_term
, char *config
, char *str
,
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_STR
,
2687 .type_term
= type_term
,
2689 .err_term
= loc_term
? loc_term
->first_column
: 0,
2690 .err_val
= loc_val
? loc_val
->first_column
: 0,
2693 return new_term(term
, &temp
, str
, 0);
2696 int parse_events_term__sym_hw(struct parse_events_term
**term
,
2697 char *config
, unsigned idx
)
2699 struct event_symbol
*sym
;
2700 struct parse_events_term temp
= {
2701 .type_val
= PARSE_EVENTS__TERM_TYPE_STR
,
2702 .type_term
= PARSE_EVENTS__TERM_TYPE_USER
,
2703 .config
= config
?: (char *) "event",
2706 BUG_ON(idx
>= PERF_COUNT_HW_MAX
);
2707 sym
= &event_symbols_hw
[idx
];
2709 return new_term(term
, &temp
, (char *) sym
->symbol
, 0);
2712 int parse_events_term__clone(struct parse_events_term
**new,
2713 struct parse_events_term
*term
)
2715 struct parse_events_term temp
= {
2716 .type_val
= term
->type_val
,
2717 .type_term
= term
->type_term
,
2718 .config
= term
->config
,
2719 .err_term
= term
->err_term
,
2720 .err_val
= term
->err_val
,
2723 return new_term(new, &temp
, term
->val
.str
, term
->val
.num
);
2726 int parse_events_copy_term_list(struct list_head
*old
,
2727 struct list_head
**new)
2729 struct parse_events_term
*term
, *n
;
2737 *new = malloc(sizeof(struct list_head
));
2740 INIT_LIST_HEAD(*new);
2742 list_for_each_entry (term
, old
, list
) {
2743 ret
= parse_events_term__clone(&n
, term
);
2746 list_add_tail(&n
->list
, *new);
2751 void parse_events_terms__purge(struct list_head
*terms
)
2753 struct parse_events_term
*term
, *h
;
2755 list_for_each_entry_safe(term
, h
, terms
, list
) {
2756 if (term
->array
.nr_ranges
)
2757 zfree(&term
->array
.ranges
);
2758 list_del_init(&term
->list
);
2763 void parse_events_terms__delete(struct list_head
*terms
)
2767 parse_events_terms__purge(terms
);
2771 void parse_events__clear_array(struct parse_events_array
*a
)
2776 void parse_events_evlist_error(struct parse_events_state
*parse_state
,
2777 int idx
, const char *str
)
2779 struct parse_events_error
*err
= parse_state
->error
;
2784 err
->str
= strdup(str
);
2785 WARN_ONCE(!err
->str
, "WARNING: failed to allocate error string");
2788 static void config_terms_list(char *buf
, size_t buf_sz
)
2794 for (i
= 0; i
< __PARSE_EVENTS__TERM_TYPE_NR
; i
++) {
2795 const char *name
= config_term_names
[i
];
2797 if (!config_term_avail(i
, NULL
))
2804 if (strlen(buf
) + strlen(name
) + 2 >= buf_sz
)
2816 * Return string contains valid config terms of an event.
2817 * @additional_terms: For terms such as PMU sysfs terms.
2819 char *parse_events_formats_error_string(char *additional_terms
)
2822 /* "no-overwrite" is the longest name */
2823 char static_terms
[__PARSE_EVENTS__TERM_TYPE_NR
*
2824 (sizeof("no-overwrite") - 1)];
2826 config_terms_list(static_terms
, sizeof(static_terms
));
2828 if (additional_terms
) {
2829 if (asprintf(&str
, "valid terms: %s,%s",
2830 additional_terms
, static_terms
) < 0)
2833 if (asprintf(&str
, "valid terms: %s", static_terms
) < 0)