Merge tag 'block-5.11-2021-01-10' of git://git.kernel.dk/linux-block
[linux/fpc-iii.git] / tools / perf / util / parse-events.c
blob42c84adeb2fbe1e65d82b9b069e024b36aa2cb62
1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/hw_breakpoint.h>
3 #include <linux/err.h>
4 #include <linux/zalloc.h>
5 #include <dirent.h>
6 #include <errno.h>
7 #include <sys/ioctl.h>
8 #include <sys/types.h>
9 #include <sys/stat.h>
10 #include <fcntl.h>
11 #include <sys/param.h>
12 #include "term.h"
13 #include "build-id.h"
14 #include "evlist.h"
15 #include "evsel.h"
16 #include <subcmd/pager.h>
17 #include <subcmd/parse-options.h>
18 #include "parse-events.h"
19 #include <subcmd/exec-cmd.h>
20 #include "string2.h"
21 #include "strlist.h"
22 #include "symbol.h"
23 #include "header.h"
24 #include "bpf-loader.h"
25 #include "debug.h"
26 #include <api/fs/tracing_path.h>
27 #include <perf/cpumap.h>
28 #include "parse-events-bison.h"
29 #define YY_EXTRA_TYPE void*
30 #include "parse-events-flex.h"
31 #include "pmu.h"
32 #include "thread_map.h"
33 #include "probe-file.h"
34 #include "asm/bug.h"
35 #include "util/parse-branch-options.h"
36 #include "metricgroup.h"
37 #include "util/evsel_config.h"
38 #include "util/event.h"
39 #include "util/pfm.h"
40 #include "perf.h"
42 #define MAX_NAME_LEN 100
44 #ifdef PARSER_DEBUG
45 extern int parse_events_debug;
46 #endif
47 int parse_events_parse(void *parse_state, void *scanner);
48 static int get_config_terms(struct list_head *head_config,
49 struct list_head *head_terms __maybe_unused);
51 static struct perf_pmu_event_symbol *perf_pmu_events_list;
53 * The variable indicates the number of supported pmu event symbols.
54 * 0 means not initialized and ready to init
55 * -1 means failed to init, don't try anymore
56 * >0 is the number of supported pmu event symbols
58 static int perf_pmu_events_list_num;
60 struct event_symbol event_symbols_hw[PERF_COUNT_HW_MAX] = {
61 [PERF_COUNT_HW_CPU_CYCLES] = {
62 .symbol = "cpu-cycles",
63 .alias = "cycles",
65 [PERF_COUNT_HW_INSTRUCTIONS] = {
66 .symbol = "instructions",
67 .alias = "",
69 [PERF_COUNT_HW_CACHE_REFERENCES] = {
70 .symbol = "cache-references",
71 .alias = "",
73 [PERF_COUNT_HW_CACHE_MISSES] = {
74 .symbol = "cache-misses",
75 .alias = "",
77 [PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = {
78 .symbol = "branch-instructions",
79 .alias = "branches",
81 [PERF_COUNT_HW_BRANCH_MISSES] = {
82 .symbol = "branch-misses",
83 .alias = "",
85 [PERF_COUNT_HW_BUS_CYCLES] = {
86 .symbol = "bus-cycles",
87 .alias = "",
89 [PERF_COUNT_HW_STALLED_CYCLES_FRONTEND] = {
90 .symbol = "stalled-cycles-frontend",
91 .alias = "idle-cycles-frontend",
93 [PERF_COUNT_HW_STALLED_CYCLES_BACKEND] = {
94 .symbol = "stalled-cycles-backend",
95 .alias = "idle-cycles-backend",
97 [PERF_COUNT_HW_REF_CPU_CYCLES] = {
98 .symbol = "ref-cycles",
99 .alias = "",
103 struct event_symbol event_symbols_sw[PERF_COUNT_SW_MAX] = {
104 [PERF_COUNT_SW_CPU_CLOCK] = {
105 .symbol = "cpu-clock",
106 .alias = "",
108 [PERF_COUNT_SW_TASK_CLOCK] = {
109 .symbol = "task-clock",
110 .alias = "",
112 [PERF_COUNT_SW_PAGE_FAULTS] = {
113 .symbol = "page-faults",
114 .alias = "faults",
116 [PERF_COUNT_SW_CONTEXT_SWITCHES] = {
117 .symbol = "context-switches",
118 .alias = "cs",
120 [PERF_COUNT_SW_CPU_MIGRATIONS] = {
121 .symbol = "cpu-migrations",
122 .alias = "migrations",
124 [PERF_COUNT_SW_PAGE_FAULTS_MIN] = {
125 .symbol = "minor-faults",
126 .alias = "",
128 [PERF_COUNT_SW_PAGE_FAULTS_MAJ] = {
129 .symbol = "major-faults",
130 .alias = "",
132 [PERF_COUNT_SW_ALIGNMENT_FAULTS] = {
133 .symbol = "alignment-faults",
134 .alias = "",
136 [PERF_COUNT_SW_EMULATION_FAULTS] = {
137 .symbol = "emulation-faults",
138 .alias = "",
140 [PERF_COUNT_SW_DUMMY] = {
141 .symbol = "dummy",
142 .alias = "",
144 [PERF_COUNT_SW_BPF_OUTPUT] = {
145 .symbol = "bpf-output",
146 .alias = "",
150 #define __PERF_EVENT_FIELD(config, name) \
151 ((config & PERF_EVENT_##name##_MASK) >> PERF_EVENT_##name##_SHIFT)
153 #define PERF_EVENT_RAW(config) __PERF_EVENT_FIELD(config, RAW)
154 #define PERF_EVENT_CONFIG(config) __PERF_EVENT_FIELD(config, CONFIG)
155 #define PERF_EVENT_TYPE(config) __PERF_EVENT_FIELD(config, TYPE)
156 #define PERF_EVENT_ID(config) __PERF_EVENT_FIELD(config, EVENT)
158 #define for_each_subsystem(sys_dir, sys_dirent) \
159 while ((sys_dirent = readdir(sys_dir)) != NULL) \
160 if (sys_dirent->d_type == DT_DIR && \
161 (strcmp(sys_dirent->d_name, ".")) && \
162 (strcmp(sys_dirent->d_name, "..")))
164 static int tp_event_has_id(const char *dir_path, struct dirent *evt_dir)
166 char evt_path[MAXPATHLEN];
167 int fd;
169 snprintf(evt_path, MAXPATHLEN, "%s/%s/id", dir_path, evt_dir->d_name);
170 fd = open(evt_path, O_RDONLY);
171 if (fd < 0)
172 return -EINVAL;
173 close(fd);
175 return 0;
178 #define for_each_event(dir_path, evt_dir, evt_dirent) \
179 while ((evt_dirent = readdir(evt_dir)) != NULL) \
180 if (evt_dirent->d_type == DT_DIR && \
181 (strcmp(evt_dirent->d_name, ".")) && \
182 (strcmp(evt_dirent->d_name, "..")) && \
183 (!tp_event_has_id(dir_path, evt_dirent)))
185 #define MAX_EVENT_LENGTH 512
187 void parse_events__handle_error(struct parse_events_error *err, int idx,
188 char *str, char *help)
190 if (WARN(!str, "WARNING: failed to provide error string\n")) {
191 free(help);
192 return;
194 switch (err->num_errors) {
195 case 0:
196 err->idx = idx;
197 err->str = str;
198 err->help = help;
199 break;
200 case 1:
201 err->first_idx = err->idx;
202 err->idx = idx;
203 err->first_str = err->str;
204 err->str = str;
205 err->first_help = err->help;
206 err->help = help;
207 break;
208 default:
209 pr_debug("Multiple errors dropping message: %s (%s)\n",
210 err->str, err->help);
211 free(err->str);
212 err->str = str;
213 free(err->help);
214 err->help = help;
215 break;
217 err->num_errors++;
220 struct tracepoint_path *tracepoint_id_to_path(u64 config)
222 struct tracepoint_path *path = NULL;
223 DIR *sys_dir, *evt_dir;
224 struct dirent *sys_dirent, *evt_dirent;
225 char id_buf[24];
226 int fd;
227 u64 id;
228 char evt_path[MAXPATHLEN];
229 char *dir_path;
231 sys_dir = tracing_events__opendir();
232 if (!sys_dir)
233 return NULL;
235 for_each_subsystem(sys_dir, sys_dirent) {
236 dir_path = get_events_file(sys_dirent->d_name);
237 if (!dir_path)
238 continue;
239 evt_dir = opendir(dir_path);
240 if (!evt_dir)
241 goto next;
243 for_each_event(dir_path, evt_dir, evt_dirent) {
245 scnprintf(evt_path, MAXPATHLEN, "%s/%s/id", dir_path,
246 evt_dirent->d_name);
247 fd = open(evt_path, O_RDONLY);
248 if (fd < 0)
249 continue;
250 if (read(fd, id_buf, sizeof(id_buf)) < 0) {
251 close(fd);
252 continue;
254 close(fd);
255 id = atoll(id_buf);
256 if (id == config) {
257 put_events_file(dir_path);
258 closedir(evt_dir);
259 closedir(sys_dir);
260 path = zalloc(sizeof(*path));
261 if (!path)
262 return NULL;
263 if (asprintf(&path->system, "%.*s", MAX_EVENT_LENGTH, sys_dirent->d_name) < 0) {
264 free(path);
265 return NULL;
267 if (asprintf(&path->name, "%.*s", MAX_EVENT_LENGTH, evt_dirent->d_name) < 0) {
268 zfree(&path->system);
269 free(path);
270 return NULL;
272 return path;
275 closedir(evt_dir);
276 next:
277 put_events_file(dir_path);
280 closedir(sys_dir);
281 return NULL;
284 struct tracepoint_path *tracepoint_name_to_path(const char *name)
286 struct tracepoint_path *path = zalloc(sizeof(*path));
287 char *str = strchr(name, ':');
289 if (path == NULL || str == NULL) {
290 free(path);
291 return NULL;
294 path->system = strndup(name, str - name);
295 path->name = strdup(str+1);
297 if (path->system == NULL || path->name == NULL) {
298 zfree(&path->system);
299 zfree(&path->name);
300 zfree(&path);
303 return path;
306 const char *event_type(int type)
308 switch (type) {
309 case PERF_TYPE_HARDWARE:
310 return "hardware";
312 case PERF_TYPE_SOFTWARE:
313 return "software";
315 case PERF_TYPE_TRACEPOINT:
316 return "tracepoint";
318 case PERF_TYPE_HW_CACHE:
319 return "hardware-cache";
321 default:
322 break;
325 return "unknown";
328 static int parse_events__is_name_term(struct parse_events_term *term)
330 return term->type_term == PARSE_EVENTS__TERM_TYPE_NAME;
333 static char *get_config_name(struct list_head *head_terms)
335 struct parse_events_term *term;
337 if (!head_terms)
338 return NULL;
340 list_for_each_entry(term, head_terms, list)
341 if (parse_events__is_name_term(term))
342 return term->val.str;
344 return NULL;
347 static struct evsel *
348 __add_event(struct list_head *list, int *idx,
349 struct perf_event_attr *attr,
350 bool init_attr,
351 char *name, struct perf_pmu *pmu,
352 struct list_head *config_terms, bool auto_merge_stats,
353 const char *cpu_list)
355 struct evsel *evsel;
356 struct perf_cpu_map *cpus = pmu ? perf_cpu_map__get(pmu->cpus) :
357 cpu_list ? perf_cpu_map__new(cpu_list) : NULL;
359 if (init_attr)
360 event_attr_init(attr);
362 evsel = evsel__new_idx(attr, *idx);
363 if (!evsel) {
364 perf_cpu_map__put(cpus);
365 return NULL;
368 (*idx)++;
369 evsel->core.cpus = cpus;
370 evsel->core.own_cpus = perf_cpu_map__get(cpus);
371 evsel->core.system_wide = pmu ? pmu->is_uncore : false;
372 evsel->auto_merge_stats = auto_merge_stats;
374 if (name)
375 evsel->name = strdup(name);
377 if (config_terms)
378 list_splice(config_terms, &evsel->config_terms);
380 if (list)
381 list_add_tail(&evsel->core.node, list);
383 return evsel;
386 struct evsel *parse_events__add_event(int idx, struct perf_event_attr *attr,
387 char *name, struct perf_pmu *pmu)
389 return __add_event(NULL, &idx, attr, false, name, pmu, NULL, false,
390 NULL);
393 static int add_event(struct list_head *list, int *idx,
394 struct perf_event_attr *attr, char *name,
395 struct list_head *config_terms)
397 return __add_event(list, idx, attr, true, name, NULL, config_terms,
398 false, NULL) ? 0 : -ENOMEM;
401 static int add_event_tool(struct list_head *list, int *idx,
402 enum perf_tool_event tool_event)
404 struct evsel *evsel;
405 struct perf_event_attr attr = {
406 .type = PERF_TYPE_SOFTWARE,
407 .config = PERF_COUNT_SW_DUMMY,
410 evsel = __add_event(list, idx, &attr, true, NULL, NULL, NULL, false,
411 "0");
412 if (!evsel)
413 return -ENOMEM;
414 evsel->tool_event = tool_event;
415 if (tool_event == PERF_TOOL_DURATION_TIME)
416 evsel->unit = "ns";
417 return 0;
420 static int parse_aliases(char *str, const char *names[][EVSEL__MAX_ALIASES], int size)
422 int i, j;
423 int n, longest = -1;
425 for (i = 0; i < size; i++) {
426 for (j = 0; j < EVSEL__MAX_ALIASES && names[i][j]; j++) {
427 n = strlen(names[i][j]);
428 if (n > longest && !strncasecmp(str, names[i][j], n))
429 longest = n;
431 if (longest > 0)
432 return i;
435 return -1;
438 typedef int config_term_func_t(struct perf_event_attr *attr,
439 struct parse_events_term *term,
440 struct parse_events_error *err);
441 static int config_term_common(struct perf_event_attr *attr,
442 struct parse_events_term *term,
443 struct parse_events_error *err);
444 static int config_attr(struct perf_event_attr *attr,
445 struct list_head *head,
446 struct parse_events_error *err,
447 config_term_func_t config_term);
449 int parse_events_add_cache(struct list_head *list, int *idx,
450 char *type, char *op_result1, char *op_result2,
451 struct parse_events_error *err,
452 struct list_head *head_config)
454 struct perf_event_attr attr;
455 LIST_HEAD(config_terms);
456 char name[MAX_NAME_LEN], *config_name;
457 int cache_type = -1, cache_op = -1, cache_result = -1;
458 char *op_result[2] = { op_result1, op_result2 };
459 int i, n;
462 * No fallback - if we cannot get a clear cache type
463 * then bail out:
465 cache_type = parse_aliases(type, evsel__hw_cache, PERF_COUNT_HW_CACHE_MAX);
466 if (cache_type == -1)
467 return -EINVAL;
469 config_name = get_config_name(head_config);
470 n = snprintf(name, MAX_NAME_LEN, "%s", type);
472 for (i = 0; (i < 2) && (op_result[i]); i++) {
473 char *str = op_result[i];
475 n += snprintf(name + n, MAX_NAME_LEN - n, "-%s", str);
477 if (cache_op == -1) {
478 cache_op = parse_aliases(str, evsel__hw_cache_op,
479 PERF_COUNT_HW_CACHE_OP_MAX);
480 if (cache_op >= 0) {
481 if (!evsel__is_cache_op_valid(cache_type, cache_op))
482 return -EINVAL;
483 continue;
487 if (cache_result == -1) {
488 cache_result = parse_aliases(str, evsel__hw_cache_result,
489 PERF_COUNT_HW_CACHE_RESULT_MAX);
490 if (cache_result >= 0)
491 continue;
496 * Fall back to reads:
498 if (cache_op == -1)
499 cache_op = PERF_COUNT_HW_CACHE_OP_READ;
502 * Fall back to accesses:
504 if (cache_result == -1)
505 cache_result = PERF_COUNT_HW_CACHE_RESULT_ACCESS;
507 memset(&attr, 0, sizeof(attr));
508 attr.config = cache_type | (cache_op << 8) | (cache_result << 16);
509 attr.type = PERF_TYPE_HW_CACHE;
511 if (head_config) {
512 if (config_attr(&attr, head_config, err,
513 config_term_common))
514 return -EINVAL;
516 if (get_config_terms(head_config, &config_terms))
517 return -ENOMEM;
519 return add_event(list, idx, &attr, config_name ? : name, &config_terms);
522 static void tracepoint_error(struct parse_events_error *e, int err,
523 const char *sys, const char *name)
525 const char *str;
526 char help[BUFSIZ];
528 if (!e)
529 return;
532 * We get error directly from syscall errno ( > 0),
533 * or from encoded pointer's error ( < 0).
535 err = abs(err);
537 switch (err) {
538 case EACCES:
539 str = "can't access trace events";
540 break;
541 case ENOENT:
542 str = "unknown tracepoint";
543 break;
544 default:
545 str = "failed to add tracepoint";
546 break;
549 tracing_path__strerror_open_tp(err, help, sizeof(help), sys, name);
550 parse_events__handle_error(e, 0, strdup(str), strdup(help));
553 static int add_tracepoint(struct list_head *list, int *idx,
554 const char *sys_name, const char *evt_name,
555 struct parse_events_error *err,
556 struct list_head *head_config)
558 struct evsel *evsel = evsel__newtp_idx(sys_name, evt_name, (*idx)++);
560 if (IS_ERR(evsel)) {
561 tracepoint_error(err, PTR_ERR(evsel), sys_name, evt_name);
562 return PTR_ERR(evsel);
565 if (head_config) {
566 LIST_HEAD(config_terms);
568 if (get_config_terms(head_config, &config_terms))
569 return -ENOMEM;
570 list_splice(&config_terms, &evsel->config_terms);
573 list_add_tail(&evsel->core.node, list);
574 return 0;
577 static int add_tracepoint_multi_event(struct list_head *list, int *idx,
578 const char *sys_name, const char *evt_name,
579 struct parse_events_error *err,
580 struct list_head *head_config)
582 char *evt_path;
583 struct dirent *evt_ent;
584 DIR *evt_dir;
585 int ret = 0, found = 0;
587 evt_path = get_events_file(sys_name);
588 if (!evt_path) {
589 tracepoint_error(err, errno, sys_name, evt_name);
590 return -1;
592 evt_dir = opendir(evt_path);
593 if (!evt_dir) {
594 put_events_file(evt_path);
595 tracepoint_error(err, errno, sys_name, evt_name);
596 return -1;
599 while (!ret && (evt_ent = readdir(evt_dir))) {
600 if (!strcmp(evt_ent->d_name, ".")
601 || !strcmp(evt_ent->d_name, "..")
602 || !strcmp(evt_ent->d_name, "enable")
603 || !strcmp(evt_ent->d_name, "filter"))
604 continue;
606 if (!strglobmatch(evt_ent->d_name, evt_name))
607 continue;
609 found++;
611 ret = add_tracepoint(list, idx, sys_name, evt_ent->d_name,
612 err, head_config);
615 if (!found) {
616 tracepoint_error(err, ENOENT, sys_name, evt_name);
617 ret = -1;
620 put_events_file(evt_path);
621 closedir(evt_dir);
622 return ret;
625 static int add_tracepoint_event(struct list_head *list, int *idx,
626 const char *sys_name, const char *evt_name,
627 struct parse_events_error *err,
628 struct list_head *head_config)
630 return strpbrk(evt_name, "*?") ?
631 add_tracepoint_multi_event(list, idx, sys_name, evt_name,
632 err, head_config) :
633 add_tracepoint(list, idx, sys_name, evt_name,
634 err, head_config);
637 static int add_tracepoint_multi_sys(struct list_head *list, int *idx,
638 const char *sys_name, const char *evt_name,
639 struct parse_events_error *err,
640 struct list_head *head_config)
642 struct dirent *events_ent;
643 DIR *events_dir;
644 int ret = 0;
646 events_dir = tracing_events__opendir();
647 if (!events_dir) {
648 tracepoint_error(err, errno, sys_name, evt_name);
649 return -1;
652 while (!ret && (events_ent = readdir(events_dir))) {
653 if (!strcmp(events_ent->d_name, ".")
654 || !strcmp(events_ent->d_name, "..")
655 || !strcmp(events_ent->d_name, "enable")
656 || !strcmp(events_ent->d_name, "header_event")
657 || !strcmp(events_ent->d_name, "header_page"))
658 continue;
660 if (!strglobmatch(events_ent->d_name, sys_name))
661 continue;
663 ret = add_tracepoint_event(list, idx, events_ent->d_name,
664 evt_name, err, head_config);
667 closedir(events_dir);
668 return ret;
671 #ifdef HAVE_LIBBPF_SUPPORT
672 struct __add_bpf_event_param {
673 struct parse_events_state *parse_state;
674 struct list_head *list;
675 struct list_head *head_config;
678 static int add_bpf_event(const char *group, const char *event, int fd, struct bpf_object *obj,
679 void *_param)
681 LIST_HEAD(new_evsels);
682 struct __add_bpf_event_param *param = _param;
683 struct parse_events_state *parse_state = param->parse_state;
684 struct list_head *list = param->list;
685 struct evsel *pos;
686 int err;
688 * Check if we should add the event, i.e. if it is a TP but starts with a '!',
689 * then don't add the tracepoint, this will be used for something else, like
690 * adding to a BPF_MAP_TYPE_PROG_ARRAY.
692 * See tools/perf/examples/bpf/augmented_raw_syscalls.c
694 if (group[0] == '!')
695 return 0;
697 pr_debug("add bpf event %s:%s and attach bpf program %d\n",
698 group, event, fd);
700 err = parse_events_add_tracepoint(&new_evsels, &parse_state->idx, group,
701 event, parse_state->error,
702 param->head_config);
703 if (err) {
704 struct evsel *evsel, *tmp;
706 pr_debug("Failed to add BPF event %s:%s\n",
707 group, event);
708 list_for_each_entry_safe(evsel, tmp, &new_evsels, core.node) {
709 list_del_init(&evsel->core.node);
710 evsel__delete(evsel);
712 return err;
714 pr_debug("adding %s:%s\n", group, event);
716 list_for_each_entry(pos, &new_evsels, core.node) {
717 pr_debug("adding %s:%s to %p\n",
718 group, event, pos);
719 pos->bpf_fd = fd;
720 pos->bpf_obj = obj;
722 list_splice(&new_evsels, list);
723 return 0;
726 int parse_events_load_bpf_obj(struct parse_events_state *parse_state,
727 struct list_head *list,
728 struct bpf_object *obj,
729 struct list_head *head_config)
731 int err;
732 char errbuf[BUFSIZ];
733 struct __add_bpf_event_param param = {parse_state, list, head_config};
734 static bool registered_unprobe_atexit = false;
736 if (IS_ERR(obj) || !obj) {
737 snprintf(errbuf, sizeof(errbuf),
738 "Internal error: load bpf obj with NULL");
739 err = -EINVAL;
740 goto errout;
744 * Register atexit handler before calling bpf__probe() so
745 * bpf__probe() don't need to unprobe probe points its already
746 * created when failure.
748 if (!registered_unprobe_atexit) {
749 atexit(bpf__clear);
750 registered_unprobe_atexit = true;
753 err = bpf__probe(obj);
754 if (err) {
755 bpf__strerror_probe(obj, err, errbuf, sizeof(errbuf));
756 goto errout;
759 err = bpf__load(obj);
760 if (err) {
761 bpf__strerror_load(obj, err, errbuf, sizeof(errbuf));
762 goto errout;
765 err = bpf__foreach_event(obj, add_bpf_event, &param);
766 if (err) {
767 snprintf(errbuf, sizeof(errbuf),
768 "Attach events in BPF object failed");
769 goto errout;
772 return 0;
773 errout:
774 parse_events__handle_error(parse_state->error, 0,
775 strdup(errbuf), strdup("(add -v to see detail)"));
776 return err;
779 static int
780 parse_events_config_bpf(struct parse_events_state *parse_state,
781 struct bpf_object *obj,
782 struct list_head *head_config)
784 struct parse_events_term *term;
785 int error_pos;
787 if (!head_config || list_empty(head_config))
788 return 0;
790 list_for_each_entry(term, head_config, list) {
791 int err;
793 if (term->type_term != PARSE_EVENTS__TERM_TYPE_USER) {
794 parse_events__handle_error(parse_state->error, term->err_term,
795 strdup("Invalid config term for BPF object"),
796 NULL);
797 return -EINVAL;
800 err = bpf__config_obj(obj, term, parse_state->evlist, &error_pos);
801 if (err) {
802 char errbuf[BUFSIZ];
803 int idx;
805 bpf__strerror_config_obj(obj, term, parse_state->evlist,
806 &error_pos, err, errbuf,
807 sizeof(errbuf));
809 if (err == -BPF_LOADER_ERRNO__OBJCONF_MAP_VALUE)
810 idx = term->err_val;
811 else
812 idx = term->err_term + error_pos;
814 parse_events__handle_error(parse_state->error, idx,
815 strdup(errbuf),
816 strdup(
817 "Hint:\tValid config terms:\n"
818 " \tmap:[<arraymap>].value<indices>=[value]\n"
819 " \tmap:[<eventmap>].event<indices>=[event]\n"
820 "\n"
821 " \twhere <indices> is something like [0,3...5] or [all]\n"
822 " \t(add -v to see detail)"));
823 return err;
826 return 0;
830 * Split config terms:
831 * perf record -e bpf.c/call-graph=fp,map:array.value[0]=1/ ...
832 * 'call-graph=fp' is 'evt config', should be applied to each
833 * events in bpf.c.
834 * 'map:array.value[0]=1' is 'obj config', should be processed
835 * with parse_events_config_bpf.
837 * Move object config terms from the first list to obj_head_config.
839 static void
840 split_bpf_config_terms(struct list_head *evt_head_config,
841 struct list_head *obj_head_config)
843 struct parse_events_term *term, *temp;
846 * Currectly, all possible user config term
847 * belong to bpf object. parse_events__is_hardcoded_term()
848 * happends to be a good flag.
850 * See parse_events_config_bpf() and
851 * config_term_tracepoint().
853 list_for_each_entry_safe(term, temp, evt_head_config, list)
854 if (!parse_events__is_hardcoded_term(term))
855 list_move_tail(&term->list, obj_head_config);
858 int parse_events_load_bpf(struct parse_events_state *parse_state,
859 struct list_head *list,
860 char *bpf_file_name,
861 bool source,
862 struct list_head *head_config)
864 int err;
865 struct bpf_object *obj;
866 LIST_HEAD(obj_head_config);
868 if (head_config)
869 split_bpf_config_terms(head_config, &obj_head_config);
871 obj = bpf__prepare_load(bpf_file_name, source);
872 if (IS_ERR(obj)) {
873 char errbuf[BUFSIZ];
875 err = PTR_ERR(obj);
877 if (err == -ENOTSUP)
878 snprintf(errbuf, sizeof(errbuf),
879 "BPF support is not compiled");
880 else
881 bpf__strerror_prepare_load(bpf_file_name,
882 source,
883 -err, errbuf,
884 sizeof(errbuf));
886 parse_events__handle_error(parse_state->error, 0,
887 strdup(errbuf), strdup("(add -v to see detail)"));
888 return err;
891 err = parse_events_load_bpf_obj(parse_state, list, obj, head_config);
892 if (err)
893 return err;
894 err = parse_events_config_bpf(parse_state, obj, &obj_head_config);
897 * Caller doesn't know anything about obj_head_config,
898 * so combine them together again before returnning.
900 if (head_config)
901 list_splice_tail(&obj_head_config, head_config);
902 return err;
904 #else // HAVE_LIBBPF_SUPPORT
905 int parse_events_load_bpf_obj(struct parse_events_state *parse_state,
906 struct list_head *list __maybe_unused,
907 struct bpf_object *obj __maybe_unused,
908 struct list_head *head_config __maybe_unused)
910 parse_events__handle_error(parse_state->error, 0,
911 strdup("BPF support is not compiled"),
912 strdup("Make sure libbpf-devel is available at build time."));
913 return -ENOTSUP;
916 int parse_events_load_bpf(struct parse_events_state *parse_state,
917 struct list_head *list __maybe_unused,
918 char *bpf_file_name __maybe_unused,
919 bool source __maybe_unused,
920 struct list_head *head_config __maybe_unused)
922 parse_events__handle_error(parse_state->error, 0,
923 strdup("BPF support is not compiled"),
924 strdup("Make sure libbpf-devel is available at build time."));
925 return -ENOTSUP;
927 #endif // HAVE_LIBBPF_SUPPORT
929 static int
930 parse_breakpoint_type(const char *type, struct perf_event_attr *attr)
932 int i;
934 for (i = 0; i < 3; i++) {
935 if (!type || !type[i])
936 break;
938 #define CHECK_SET_TYPE(bit) \
939 do { \
940 if (attr->bp_type & bit) \
941 return -EINVAL; \
942 else \
943 attr->bp_type |= bit; \
944 } while (0)
946 switch (type[i]) {
947 case 'r':
948 CHECK_SET_TYPE(HW_BREAKPOINT_R);
949 break;
950 case 'w':
951 CHECK_SET_TYPE(HW_BREAKPOINT_W);
952 break;
953 case 'x':
954 CHECK_SET_TYPE(HW_BREAKPOINT_X);
955 break;
956 default:
957 return -EINVAL;
961 #undef CHECK_SET_TYPE
963 if (!attr->bp_type) /* Default */
964 attr->bp_type = HW_BREAKPOINT_R | HW_BREAKPOINT_W;
966 return 0;
969 int parse_events_add_breakpoint(struct list_head *list, int *idx,
970 u64 addr, char *type, u64 len)
972 struct perf_event_attr attr;
974 memset(&attr, 0, sizeof(attr));
975 attr.bp_addr = addr;
977 if (parse_breakpoint_type(type, &attr))
978 return -EINVAL;
980 /* Provide some defaults if len is not specified */
981 if (!len) {
982 if (attr.bp_type == HW_BREAKPOINT_X)
983 len = sizeof(long);
984 else
985 len = HW_BREAKPOINT_LEN_4;
988 attr.bp_len = len;
990 attr.type = PERF_TYPE_BREAKPOINT;
991 attr.sample_period = 1;
993 return add_event(list, idx, &attr, NULL, NULL);
996 static int check_type_val(struct parse_events_term *term,
997 struct parse_events_error *err,
998 int type)
1000 if (type == term->type_val)
1001 return 0;
1003 if (err) {
1004 parse_events__handle_error(err, term->err_val,
1005 type == PARSE_EVENTS__TERM_TYPE_NUM
1006 ? strdup("expected numeric value")
1007 : strdup("expected string value"),
1008 NULL);
1010 return -EINVAL;
1014 * Update according to parse-events.l
1016 static const char *config_term_names[__PARSE_EVENTS__TERM_TYPE_NR] = {
1017 [PARSE_EVENTS__TERM_TYPE_USER] = "<sysfs term>",
1018 [PARSE_EVENTS__TERM_TYPE_CONFIG] = "config",
1019 [PARSE_EVENTS__TERM_TYPE_CONFIG1] = "config1",
1020 [PARSE_EVENTS__TERM_TYPE_CONFIG2] = "config2",
1021 [PARSE_EVENTS__TERM_TYPE_NAME] = "name",
1022 [PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD] = "period",
1023 [PARSE_EVENTS__TERM_TYPE_SAMPLE_FREQ] = "freq",
1024 [PARSE_EVENTS__TERM_TYPE_BRANCH_SAMPLE_TYPE] = "branch_type",
1025 [PARSE_EVENTS__TERM_TYPE_TIME] = "time",
1026 [PARSE_EVENTS__TERM_TYPE_CALLGRAPH] = "call-graph",
1027 [PARSE_EVENTS__TERM_TYPE_STACKSIZE] = "stack-size",
1028 [PARSE_EVENTS__TERM_TYPE_NOINHERIT] = "no-inherit",
1029 [PARSE_EVENTS__TERM_TYPE_INHERIT] = "inherit",
1030 [PARSE_EVENTS__TERM_TYPE_MAX_STACK] = "max-stack",
1031 [PARSE_EVENTS__TERM_TYPE_MAX_EVENTS] = "nr",
1032 [PARSE_EVENTS__TERM_TYPE_OVERWRITE] = "overwrite",
1033 [PARSE_EVENTS__TERM_TYPE_NOOVERWRITE] = "no-overwrite",
1034 [PARSE_EVENTS__TERM_TYPE_DRV_CFG] = "driver-config",
1035 [PARSE_EVENTS__TERM_TYPE_PERCORE] = "percore",
1036 [PARSE_EVENTS__TERM_TYPE_AUX_OUTPUT] = "aux-output",
1037 [PARSE_EVENTS__TERM_TYPE_AUX_SAMPLE_SIZE] = "aux-sample-size",
1040 static bool config_term_shrinked;
1042 static bool
1043 config_term_avail(int term_type, struct parse_events_error *err)
1045 char *err_str;
1047 if (term_type < 0 || term_type >= __PARSE_EVENTS__TERM_TYPE_NR) {
1048 parse_events__handle_error(err, -1,
1049 strdup("Invalid term_type"), NULL);
1050 return false;
1052 if (!config_term_shrinked)
1053 return true;
1055 switch (term_type) {
1056 case PARSE_EVENTS__TERM_TYPE_CONFIG:
1057 case PARSE_EVENTS__TERM_TYPE_CONFIG1:
1058 case PARSE_EVENTS__TERM_TYPE_CONFIG2:
1059 case PARSE_EVENTS__TERM_TYPE_NAME:
1060 case PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD:
1061 case PARSE_EVENTS__TERM_TYPE_PERCORE:
1062 return true;
1063 default:
1064 if (!err)
1065 return false;
1067 /* term_type is validated so indexing is safe */
1068 if (asprintf(&err_str, "'%s' is not usable in 'perf stat'",
1069 config_term_names[term_type]) >= 0)
1070 parse_events__handle_error(err, -1, err_str, NULL);
1071 return false;
1075 void parse_events__shrink_config_terms(void)
1077 config_term_shrinked = true;
1080 static int config_term_common(struct perf_event_attr *attr,
1081 struct parse_events_term *term,
1082 struct parse_events_error *err)
1084 #define CHECK_TYPE_VAL(type) \
1085 do { \
1086 if (check_type_val(term, err, PARSE_EVENTS__TERM_TYPE_ ## type)) \
1087 return -EINVAL; \
1088 } while (0)
1090 switch (term->type_term) {
1091 case PARSE_EVENTS__TERM_TYPE_CONFIG:
1092 CHECK_TYPE_VAL(NUM);
1093 attr->config = term->val.num;
1094 break;
1095 case PARSE_EVENTS__TERM_TYPE_CONFIG1:
1096 CHECK_TYPE_VAL(NUM);
1097 attr->config1 = term->val.num;
1098 break;
1099 case PARSE_EVENTS__TERM_TYPE_CONFIG2:
1100 CHECK_TYPE_VAL(NUM);
1101 attr->config2 = term->val.num;
1102 break;
1103 case PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD:
1104 CHECK_TYPE_VAL(NUM);
1105 break;
1106 case PARSE_EVENTS__TERM_TYPE_SAMPLE_FREQ:
1107 CHECK_TYPE_VAL(NUM);
1108 break;
1109 case PARSE_EVENTS__TERM_TYPE_BRANCH_SAMPLE_TYPE:
1110 CHECK_TYPE_VAL(STR);
1111 if (strcmp(term->val.str, "no") &&
1112 parse_branch_str(term->val.str,
1113 &attr->branch_sample_type)) {
1114 parse_events__handle_error(err, term->err_val,
1115 strdup("invalid branch sample type"),
1116 NULL);
1117 return -EINVAL;
1119 break;
1120 case PARSE_EVENTS__TERM_TYPE_TIME:
1121 CHECK_TYPE_VAL(NUM);
1122 if (term->val.num > 1) {
1123 parse_events__handle_error(err, term->err_val,
1124 strdup("expected 0 or 1"),
1125 NULL);
1126 return -EINVAL;
1128 break;
1129 case PARSE_EVENTS__TERM_TYPE_CALLGRAPH:
1130 CHECK_TYPE_VAL(STR);
1131 break;
1132 case PARSE_EVENTS__TERM_TYPE_STACKSIZE:
1133 CHECK_TYPE_VAL(NUM);
1134 break;
1135 case PARSE_EVENTS__TERM_TYPE_INHERIT:
1136 CHECK_TYPE_VAL(NUM);
1137 break;
1138 case PARSE_EVENTS__TERM_TYPE_NOINHERIT:
1139 CHECK_TYPE_VAL(NUM);
1140 break;
1141 case PARSE_EVENTS__TERM_TYPE_OVERWRITE:
1142 CHECK_TYPE_VAL(NUM);
1143 break;
1144 case PARSE_EVENTS__TERM_TYPE_NOOVERWRITE:
1145 CHECK_TYPE_VAL(NUM);
1146 break;
1147 case PARSE_EVENTS__TERM_TYPE_NAME:
1148 CHECK_TYPE_VAL(STR);
1149 break;
1150 case PARSE_EVENTS__TERM_TYPE_MAX_STACK:
1151 CHECK_TYPE_VAL(NUM);
1152 break;
1153 case PARSE_EVENTS__TERM_TYPE_MAX_EVENTS:
1154 CHECK_TYPE_VAL(NUM);
1155 break;
1156 case PARSE_EVENTS__TERM_TYPE_PERCORE:
1157 CHECK_TYPE_VAL(NUM);
1158 if ((unsigned int)term->val.num > 1) {
1159 parse_events__handle_error(err, term->err_val,
1160 strdup("expected 0 or 1"),
1161 NULL);
1162 return -EINVAL;
1164 break;
1165 case PARSE_EVENTS__TERM_TYPE_AUX_OUTPUT:
1166 CHECK_TYPE_VAL(NUM);
1167 break;
1168 case PARSE_EVENTS__TERM_TYPE_AUX_SAMPLE_SIZE:
1169 CHECK_TYPE_VAL(NUM);
1170 if (term->val.num > UINT_MAX) {
1171 parse_events__handle_error(err, term->err_val,
1172 strdup("too big"),
1173 NULL);
1174 return -EINVAL;
1176 break;
1177 default:
1178 parse_events__handle_error(err, term->err_term,
1179 strdup("unknown term"),
1180 parse_events_formats_error_string(NULL));
1181 return -EINVAL;
1185 * Check term availbility after basic checking so
1186 * PARSE_EVENTS__TERM_TYPE_USER can be found and filtered.
1188 * If check availbility at the entry of this function,
1189 * user will see "'<sysfs term>' is not usable in 'perf stat'"
1190 * if an invalid config term is provided for legacy events
1191 * (for example, instructions/badterm/...), which is confusing.
1193 if (!config_term_avail(term->type_term, err))
1194 return -EINVAL;
1195 return 0;
1196 #undef CHECK_TYPE_VAL
1199 static int config_term_pmu(struct perf_event_attr *attr,
1200 struct parse_events_term *term,
1201 struct parse_events_error *err)
1203 if (term->type_term == PARSE_EVENTS__TERM_TYPE_USER ||
1204 term->type_term == PARSE_EVENTS__TERM_TYPE_DRV_CFG)
1206 * Always succeed for sysfs terms, as we dont know
1207 * at this point what type they need to have.
1209 return 0;
1210 else
1211 return config_term_common(attr, term, err);
1214 static int config_term_tracepoint(struct perf_event_attr *attr,
1215 struct parse_events_term *term,
1216 struct parse_events_error *err)
1218 switch (term->type_term) {
1219 case PARSE_EVENTS__TERM_TYPE_CALLGRAPH:
1220 case PARSE_EVENTS__TERM_TYPE_STACKSIZE:
1221 case PARSE_EVENTS__TERM_TYPE_INHERIT:
1222 case PARSE_EVENTS__TERM_TYPE_NOINHERIT:
1223 case PARSE_EVENTS__TERM_TYPE_MAX_STACK:
1224 case PARSE_EVENTS__TERM_TYPE_MAX_EVENTS:
1225 case PARSE_EVENTS__TERM_TYPE_OVERWRITE:
1226 case PARSE_EVENTS__TERM_TYPE_NOOVERWRITE:
1227 case PARSE_EVENTS__TERM_TYPE_AUX_OUTPUT:
1228 case PARSE_EVENTS__TERM_TYPE_AUX_SAMPLE_SIZE:
1229 return config_term_common(attr, term, err);
1230 default:
1231 if (err) {
1232 parse_events__handle_error(err, term->err_term,
1233 strdup("unknown term"),
1234 strdup("valid terms: call-graph,stack-size\n"));
1236 return -EINVAL;
1239 return 0;
1242 static int config_attr(struct perf_event_attr *attr,
1243 struct list_head *head,
1244 struct parse_events_error *err,
1245 config_term_func_t config_term)
1247 struct parse_events_term *term;
1249 list_for_each_entry(term, head, list)
1250 if (config_term(attr, term, err))
1251 return -EINVAL;
1253 return 0;
1256 static int get_config_terms(struct list_head *head_config,
1257 struct list_head *head_terms __maybe_unused)
1259 #define ADD_CONFIG_TERM(__type, __weak) \
1260 struct evsel_config_term *__t; \
1262 __t = zalloc(sizeof(*__t)); \
1263 if (!__t) \
1264 return -ENOMEM; \
1266 INIT_LIST_HEAD(&__t->list); \
1267 __t->type = EVSEL__CONFIG_TERM_ ## __type; \
1268 __t->weak = __weak; \
1269 list_add_tail(&__t->list, head_terms)
1271 #define ADD_CONFIG_TERM_VAL(__type, __name, __val, __weak) \
1272 do { \
1273 ADD_CONFIG_TERM(__type, __weak); \
1274 __t->val.__name = __val; \
1275 } while (0)
1277 #define ADD_CONFIG_TERM_STR(__type, __val, __weak) \
1278 do { \
1279 ADD_CONFIG_TERM(__type, __weak); \
1280 __t->val.str = strdup(__val); \
1281 if (!__t->val.str) { \
1282 zfree(&__t); \
1283 return -ENOMEM; \
1285 __t->free_str = true; \
1286 } while (0)
1288 struct parse_events_term *term;
1290 list_for_each_entry(term, head_config, list) {
1291 switch (term->type_term) {
1292 case PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD:
1293 ADD_CONFIG_TERM_VAL(PERIOD, period, term->val.num, term->weak);
1294 break;
1295 case PARSE_EVENTS__TERM_TYPE_SAMPLE_FREQ:
1296 ADD_CONFIG_TERM_VAL(FREQ, freq, term->val.num, term->weak);
1297 break;
1298 case PARSE_EVENTS__TERM_TYPE_TIME:
1299 ADD_CONFIG_TERM_VAL(TIME, time, term->val.num, term->weak);
1300 break;
1301 case PARSE_EVENTS__TERM_TYPE_CALLGRAPH:
1302 ADD_CONFIG_TERM_STR(CALLGRAPH, term->val.str, term->weak);
1303 break;
1304 case PARSE_EVENTS__TERM_TYPE_BRANCH_SAMPLE_TYPE:
1305 ADD_CONFIG_TERM_STR(BRANCH, term->val.str, term->weak);
1306 break;
1307 case PARSE_EVENTS__TERM_TYPE_STACKSIZE:
1308 ADD_CONFIG_TERM_VAL(STACK_USER, stack_user,
1309 term->val.num, term->weak);
1310 break;
1311 case PARSE_EVENTS__TERM_TYPE_INHERIT:
1312 ADD_CONFIG_TERM_VAL(INHERIT, inherit,
1313 term->val.num ? 1 : 0, term->weak);
1314 break;
1315 case PARSE_EVENTS__TERM_TYPE_NOINHERIT:
1316 ADD_CONFIG_TERM_VAL(INHERIT, inherit,
1317 term->val.num ? 0 : 1, term->weak);
1318 break;
1319 case PARSE_EVENTS__TERM_TYPE_MAX_STACK:
1320 ADD_CONFIG_TERM_VAL(MAX_STACK, max_stack,
1321 term->val.num, term->weak);
1322 break;
1323 case PARSE_EVENTS__TERM_TYPE_MAX_EVENTS:
1324 ADD_CONFIG_TERM_VAL(MAX_EVENTS, max_events,
1325 term->val.num, term->weak);
1326 break;
1327 case PARSE_EVENTS__TERM_TYPE_OVERWRITE:
1328 ADD_CONFIG_TERM_VAL(OVERWRITE, overwrite,
1329 term->val.num ? 1 : 0, term->weak);
1330 break;
1331 case PARSE_EVENTS__TERM_TYPE_NOOVERWRITE:
1332 ADD_CONFIG_TERM_VAL(OVERWRITE, overwrite,
1333 term->val.num ? 0 : 1, term->weak);
1334 break;
1335 case PARSE_EVENTS__TERM_TYPE_DRV_CFG:
1336 ADD_CONFIG_TERM_STR(DRV_CFG, term->val.str, term->weak);
1337 break;
1338 case PARSE_EVENTS__TERM_TYPE_PERCORE:
1339 ADD_CONFIG_TERM_VAL(PERCORE, percore,
1340 term->val.num ? true : false, term->weak);
1341 break;
1342 case PARSE_EVENTS__TERM_TYPE_AUX_OUTPUT:
1343 ADD_CONFIG_TERM_VAL(AUX_OUTPUT, aux_output,
1344 term->val.num ? 1 : 0, term->weak);
1345 break;
1346 case PARSE_EVENTS__TERM_TYPE_AUX_SAMPLE_SIZE:
1347 ADD_CONFIG_TERM_VAL(AUX_SAMPLE_SIZE, aux_sample_size,
1348 term->val.num, term->weak);
1349 break;
1350 default:
1351 break;
1354 return 0;
1358 * Add EVSEL__CONFIG_TERM_CFG_CHG where cfg_chg will have a bit set for
1359 * each bit of attr->config that the user has changed.
1361 static int get_config_chgs(struct perf_pmu *pmu, struct list_head *head_config,
1362 struct list_head *head_terms)
1364 struct parse_events_term *term;
1365 u64 bits = 0;
1366 int type;
1368 list_for_each_entry(term, head_config, list) {
1369 switch (term->type_term) {
1370 case PARSE_EVENTS__TERM_TYPE_USER:
1371 type = perf_pmu__format_type(&pmu->format, term->config);
1372 if (type != PERF_PMU_FORMAT_VALUE_CONFIG)
1373 continue;
1374 bits |= perf_pmu__format_bits(&pmu->format, term->config);
1375 break;
1376 case PARSE_EVENTS__TERM_TYPE_CONFIG:
1377 bits = ~(u64)0;
1378 break;
1379 default:
1380 break;
1384 if (bits)
1385 ADD_CONFIG_TERM_VAL(CFG_CHG, cfg_chg, bits, false);
1387 #undef ADD_CONFIG_TERM
1388 return 0;
1391 int parse_events_add_tracepoint(struct list_head *list, int *idx,
1392 const char *sys, const char *event,
1393 struct parse_events_error *err,
1394 struct list_head *head_config)
1396 if (head_config) {
1397 struct perf_event_attr attr;
1399 if (config_attr(&attr, head_config, err,
1400 config_term_tracepoint))
1401 return -EINVAL;
1404 if (strpbrk(sys, "*?"))
1405 return add_tracepoint_multi_sys(list, idx, sys, event,
1406 err, head_config);
1407 else
1408 return add_tracepoint_event(list, idx, sys, event,
1409 err, head_config);
1412 int parse_events_add_numeric(struct parse_events_state *parse_state,
1413 struct list_head *list,
1414 u32 type, u64 config,
1415 struct list_head *head_config)
1417 struct perf_event_attr attr;
1418 LIST_HEAD(config_terms);
1420 memset(&attr, 0, sizeof(attr));
1421 attr.type = type;
1422 attr.config = config;
1424 if (head_config) {
1425 if (config_attr(&attr, head_config, parse_state->error,
1426 config_term_common))
1427 return -EINVAL;
1429 if (get_config_terms(head_config, &config_terms))
1430 return -ENOMEM;
1433 return add_event(list, &parse_state->idx, &attr,
1434 get_config_name(head_config), &config_terms);
1437 int parse_events_add_tool(struct parse_events_state *parse_state,
1438 struct list_head *list,
1439 enum perf_tool_event tool_event)
1441 return add_event_tool(list, &parse_state->idx, tool_event);
1444 static bool config_term_percore(struct list_head *config_terms)
1446 struct evsel_config_term *term;
1448 list_for_each_entry(term, config_terms, list) {
1449 if (term->type == EVSEL__CONFIG_TERM_PERCORE)
1450 return term->val.percore;
1453 return false;
1456 int parse_events_add_pmu(struct parse_events_state *parse_state,
1457 struct list_head *list, char *name,
1458 struct list_head *head_config,
1459 bool auto_merge_stats,
1460 bool use_alias)
1462 struct perf_event_attr attr;
1463 struct perf_pmu_info info;
1464 struct perf_pmu *pmu;
1465 struct evsel *evsel;
1466 struct parse_events_error *err = parse_state->error;
1467 bool use_uncore_alias;
1468 LIST_HEAD(config_terms);
1470 if (verbose > 1) {
1471 fprintf(stderr, "Attempting to add event pmu '%s' with '",
1472 name);
1473 if (head_config) {
1474 struct parse_events_term *term;
1476 list_for_each_entry(term, head_config, list) {
1477 fprintf(stderr, "%s,", term->config);
1480 fprintf(stderr, "' that may result in non-fatal errors\n");
1483 pmu = parse_state->fake_pmu ?: perf_pmu__find(name);
1484 if (!pmu) {
1485 char *err_str;
1487 if (asprintf(&err_str,
1488 "Cannot find PMU `%s'. Missing kernel support?",
1489 name) >= 0)
1490 parse_events__handle_error(err, 0, err_str, NULL);
1491 return -EINVAL;
1494 if (pmu->default_config) {
1495 memcpy(&attr, pmu->default_config,
1496 sizeof(struct perf_event_attr));
1497 } else {
1498 memset(&attr, 0, sizeof(attr));
1501 use_uncore_alias = (pmu->is_uncore && use_alias);
1503 if (!head_config) {
1504 attr.type = pmu->type;
1505 evsel = __add_event(list, &parse_state->idx, &attr, true, NULL,
1506 pmu, NULL, auto_merge_stats, NULL);
1507 if (evsel) {
1508 evsel->pmu_name = name ? strdup(name) : NULL;
1509 evsel->use_uncore_alias = use_uncore_alias;
1510 return 0;
1511 } else {
1512 return -ENOMEM;
1516 if (!parse_state->fake_pmu && perf_pmu__check_alias(pmu, head_config, &info))
1517 return -EINVAL;
1519 if (verbose > 1) {
1520 fprintf(stderr, "After aliases, add event pmu '%s' with '",
1521 name);
1522 if (head_config) {
1523 struct parse_events_term *term;
1525 list_for_each_entry(term, head_config, list) {
1526 fprintf(stderr, "%s,", term->config);
1529 fprintf(stderr, "' that may result in non-fatal errors\n");
1533 * Configure hardcoded terms first, no need to check
1534 * return value when called with fail == 0 ;)
1536 if (config_attr(&attr, head_config, parse_state->error, config_term_pmu))
1537 return -EINVAL;
1539 if (get_config_terms(head_config, &config_terms))
1540 return -ENOMEM;
1543 * When using default config, record which bits of attr->config were
1544 * changed by the user.
1546 if (pmu->default_config && get_config_chgs(pmu, head_config, &config_terms))
1547 return -ENOMEM;
1549 if (!parse_state->fake_pmu && perf_pmu__config(pmu, &attr, head_config, parse_state->error)) {
1550 struct evsel_config_term *pos, *tmp;
1552 list_for_each_entry_safe(pos, tmp, &config_terms, list) {
1553 list_del_init(&pos->list);
1554 if (pos->free_str)
1555 zfree(&pos->val.str);
1556 free(pos);
1558 return -EINVAL;
1561 evsel = __add_event(list, &parse_state->idx, &attr, true,
1562 get_config_name(head_config), pmu,
1563 &config_terms, auto_merge_stats, NULL);
1564 if (!evsel)
1565 return -ENOMEM;
1567 evsel->pmu_name = name ? strdup(name) : NULL;
1568 evsel->use_uncore_alias = use_uncore_alias;
1569 evsel->percore = config_term_percore(&evsel->config_terms);
1571 if (parse_state->fake_pmu)
1572 return 0;
1574 evsel->unit = info.unit;
1575 evsel->scale = info.scale;
1576 evsel->per_pkg = info.per_pkg;
1577 evsel->snapshot = info.snapshot;
1578 evsel->metric_expr = info.metric_expr;
1579 evsel->metric_name = info.metric_name;
1580 return 0;
1583 int parse_events_multi_pmu_add(struct parse_events_state *parse_state,
1584 char *str, struct list_head **listp)
1586 struct parse_events_term *term;
1587 struct list_head *list;
1588 struct perf_pmu *pmu = NULL;
1589 int ok = 0;
1591 *listp = NULL;
1592 /* Add it for all PMUs that support the alias */
1593 list = malloc(sizeof(struct list_head));
1594 if (!list)
1595 return -1;
1596 INIT_LIST_HEAD(list);
1597 while ((pmu = perf_pmu__scan(pmu)) != NULL) {
1598 struct perf_pmu_alias *alias;
1600 list_for_each_entry(alias, &pmu->aliases, list) {
1601 if (!strcasecmp(alias->name, str)) {
1602 struct list_head *head;
1603 char *config;
1605 head = malloc(sizeof(struct list_head));
1606 if (!head)
1607 return -1;
1608 INIT_LIST_HEAD(head);
1609 config = strdup(str);
1610 if (!config)
1611 return -1;
1612 if (parse_events_term__num(&term,
1613 PARSE_EVENTS__TERM_TYPE_USER,
1614 config, 1, false, &config,
1615 NULL) < 0) {
1616 free(list);
1617 free(config);
1618 return -1;
1620 list_add_tail(&term->list, head);
1622 if (!parse_events_add_pmu(parse_state, list,
1623 pmu->name, head,
1624 true, true)) {
1625 pr_debug("%s -> %s/%s/\n", str,
1626 pmu->name, alias->str);
1627 ok++;
1630 parse_events_terms__delete(head);
1634 if (!ok) {
1635 free(list);
1636 return -1;
1638 *listp = list;
1639 return 0;
1642 int parse_events__modifier_group(struct list_head *list,
1643 char *event_mod)
1645 return parse_events__modifier_event(list, event_mod, true);
1649 * Check if the two uncore PMUs are from the same uncore block
1650 * The format of the uncore PMU name is uncore_#blockname_#pmuidx
1652 static bool is_same_uncore_block(const char *pmu_name_a, const char *pmu_name_b)
1654 char *end_a, *end_b;
1656 end_a = strrchr(pmu_name_a, '_');
1657 end_b = strrchr(pmu_name_b, '_');
1659 if (!end_a || !end_b)
1660 return false;
1662 if ((end_a - pmu_name_a) != (end_b - pmu_name_b))
1663 return false;
1665 return (strncmp(pmu_name_a, pmu_name_b, end_a - pmu_name_a) == 0);
1668 static int
1669 parse_events__set_leader_for_uncore_aliase(char *name, struct list_head *list,
1670 struct parse_events_state *parse_state)
1672 struct evsel *evsel, *leader;
1673 uintptr_t *leaders;
1674 bool is_leader = true;
1675 int i, nr_pmu = 0, total_members, ret = 0;
1677 leader = list_first_entry(list, struct evsel, core.node);
1678 evsel = list_last_entry(list, struct evsel, core.node);
1679 total_members = evsel->idx - leader->idx + 1;
1681 leaders = calloc(total_members, sizeof(uintptr_t));
1682 if (WARN_ON(!leaders))
1683 return 0;
1686 * Going through the whole group and doing sanity check.
1687 * All members must use alias, and be from the same uncore block.
1688 * Also, storing the leader events in an array.
1690 __evlist__for_each_entry(list, evsel) {
1692 /* Only split the uncore group which members use alias */
1693 if (!evsel->use_uncore_alias)
1694 goto out;
1696 /* The events must be from the same uncore block */
1697 if (!is_same_uncore_block(leader->pmu_name, evsel->pmu_name))
1698 goto out;
1700 if (!is_leader)
1701 continue;
1703 * If the event's PMU name starts to repeat, it must be a new
1704 * event. That can be used to distinguish the leader from
1705 * other members, even they have the same event name.
1707 if ((leader != evsel) &&
1708 !strcmp(leader->pmu_name, evsel->pmu_name)) {
1709 is_leader = false;
1710 continue;
1713 /* Store the leader event for each PMU */
1714 leaders[nr_pmu++] = (uintptr_t) evsel;
1717 /* only one event alias */
1718 if (nr_pmu == total_members) {
1719 parse_state->nr_groups--;
1720 goto handled;
1724 * An uncore event alias is a joint name which means the same event
1725 * runs on all PMUs of a block.
1726 * Perf doesn't support mixed events from different PMUs in the same
1727 * group. The big group has to be split into multiple small groups
1728 * which only include the events from the same PMU.
1730 * Here the uncore event aliases must be from the same uncore block.
1731 * The number of PMUs must be same for each alias. The number of new
1732 * small groups equals to the number of PMUs.
1733 * Setting the leader event for corresponding members in each group.
1735 i = 0;
1736 __evlist__for_each_entry(list, evsel) {
1737 if (i >= nr_pmu)
1738 i = 0;
1739 evsel->leader = (struct evsel *) leaders[i++];
1742 /* The number of members and group name are same for each group */
1743 for (i = 0; i < nr_pmu; i++) {
1744 evsel = (struct evsel *) leaders[i];
1745 evsel->core.nr_members = total_members / nr_pmu;
1746 evsel->group_name = name ? strdup(name) : NULL;
1749 /* Take the new small groups into account */
1750 parse_state->nr_groups += nr_pmu - 1;
1752 handled:
1753 ret = 1;
1754 out:
1755 free(leaders);
1756 return ret;
1759 void parse_events__set_leader(char *name, struct list_head *list,
1760 struct parse_events_state *parse_state)
1762 struct evsel *leader;
1764 if (list_empty(list)) {
1765 WARN_ONCE(true, "WARNING: failed to set leader: empty list");
1766 return;
1769 if (parse_events__set_leader_for_uncore_aliase(name, list, parse_state))
1770 return;
1772 __evlist__set_leader(list);
1773 leader = list_entry(list->next, struct evsel, core.node);
1774 leader->group_name = name ? strdup(name) : NULL;
1777 /* list_event is assumed to point to malloc'ed memory */
1778 void parse_events_update_lists(struct list_head *list_event,
1779 struct list_head *list_all)
1782 * Called for single event definition. Update the
1783 * 'all event' list, and reinit the 'single event'
1784 * list, for next event definition.
1786 list_splice_tail(list_event, list_all);
1787 free(list_event);
1790 struct event_modifier {
1791 int eu;
1792 int ek;
1793 int eh;
1794 int eH;
1795 int eG;
1796 int eI;
1797 int precise;
1798 int precise_max;
1799 int exclude_GH;
1800 int sample_read;
1801 int pinned;
1802 int weak;
1803 int exclusive;
1806 static int get_event_modifier(struct event_modifier *mod, char *str,
1807 struct evsel *evsel)
1809 int eu = evsel ? evsel->core.attr.exclude_user : 0;
1810 int ek = evsel ? evsel->core.attr.exclude_kernel : 0;
1811 int eh = evsel ? evsel->core.attr.exclude_hv : 0;
1812 int eH = evsel ? evsel->core.attr.exclude_host : 0;
1813 int eG = evsel ? evsel->core.attr.exclude_guest : 0;
1814 int eI = evsel ? evsel->core.attr.exclude_idle : 0;
1815 int precise = evsel ? evsel->core.attr.precise_ip : 0;
1816 int precise_max = 0;
1817 int sample_read = 0;
1818 int pinned = evsel ? evsel->core.attr.pinned : 0;
1819 int exclusive = evsel ? evsel->core.attr.exclusive : 0;
1821 int exclude = eu | ek | eh;
1822 int exclude_GH = evsel ? evsel->exclude_GH : 0;
1823 int weak = 0;
1825 memset(mod, 0, sizeof(*mod));
1827 while (*str) {
1828 if (*str == 'u') {
1829 if (!exclude)
1830 exclude = eu = ek = eh = 1;
1831 if (!exclude_GH && !perf_guest)
1832 eG = 1;
1833 eu = 0;
1834 } else if (*str == 'k') {
1835 if (!exclude)
1836 exclude = eu = ek = eh = 1;
1837 ek = 0;
1838 } else if (*str == 'h') {
1839 if (!exclude)
1840 exclude = eu = ek = eh = 1;
1841 eh = 0;
1842 } else if (*str == 'G') {
1843 if (!exclude_GH)
1844 exclude_GH = eG = eH = 1;
1845 eG = 0;
1846 } else if (*str == 'H') {
1847 if (!exclude_GH)
1848 exclude_GH = eG = eH = 1;
1849 eH = 0;
1850 } else if (*str == 'I') {
1851 eI = 1;
1852 } else if (*str == 'p') {
1853 precise++;
1854 /* use of precise requires exclude_guest */
1855 if (!exclude_GH)
1856 eG = 1;
1857 } else if (*str == 'P') {
1858 precise_max = 1;
1859 } else if (*str == 'S') {
1860 sample_read = 1;
1861 } else if (*str == 'D') {
1862 pinned = 1;
1863 } else if (*str == 'e') {
1864 exclusive = 1;
1865 } else if (*str == 'W') {
1866 weak = 1;
1867 } else
1868 break;
1870 ++str;
1874 * precise ip:
1876 * 0 - SAMPLE_IP can have arbitrary skid
1877 * 1 - SAMPLE_IP must have constant skid
1878 * 2 - SAMPLE_IP requested to have 0 skid
1879 * 3 - SAMPLE_IP must have 0 skid
1881 * See also PERF_RECORD_MISC_EXACT_IP
1883 if (precise > 3)
1884 return -EINVAL;
1886 mod->eu = eu;
1887 mod->ek = ek;
1888 mod->eh = eh;
1889 mod->eH = eH;
1890 mod->eG = eG;
1891 mod->eI = eI;
1892 mod->precise = precise;
1893 mod->precise_max = precise_max;
1894 mod->exclude_GH = exclude_GH;
1895 mod->sample_read = sample_read;
1896 mod->pinned = pinned;
1897 mod->weak = weak;
1898 mod->exclusive = exclusive;
1900 return 0;
1904 * Basic modifier sanity check to validate it contains only one
1905 * instance of any modifier (apart from 'p') present.
1907 static int check_modifier(char *str)
1909 char *p = str;
1911 /* The sizeof includes 0 byte as well. */
1912 if (strlen(str) > (sizeof("ukhGHpppPSDIWe") - 1))
1913 return -1;
1915 while (*p) {
1916 if (*p != 'p' && strchr(p + 1, *p))
1917 return -1;
1918 p++;
1921 return 0;
1924 int parse_events__modifier_event(struct list_head *list, char *str, bool add)
1926 struct evsel *evsel;
1927 struct event_modifier mod;
1929 if (str == NULL)
1930 return 0;
1932 if (check_modifier(str))
1933 return -EINVAL;
1935 if (!add && get_event_modifier(&mod, str, NULL))
1936 return -EINVAL;
1938 __evlist__for_each_entry(list, evsel) {
1939 if (add && get_event_modifier(&mod, str, evsel))
1940 return -EINVAL;
1942 evsel->core.attr.exclude_user = mod.eu;
1943 evsel->core.attr.exclude_kernel = mod.ek;
1944 evsel->core.attr.exclude_hv = mod.eh;
1945 evsel->core.attr.precise_ip = mod.precise;
1946 evsel->core.attr.exclude_host = mod.eH;
1947 evsel->core.attr.exclude_guest = mod.eG;
1948 evsel->core.attr.exclude_idle = mod.eI;
1949 evsel->exclude_GH = mod.exclude_GH;
1950 evsel->sample_read = mod.sample_read;
1951 evsel->precise_max = mod.precise_max;
1952 evsel->weak_group = mod.weak;
1954 if (evsel__is_group_leader(evsel)) {
1955 evsel->core.attr.pinned = mod.pinned;
1956 evsel->core.attr.exclusive = mod.exclusive;
1960 return 0;
1963 int parse_events_name(struct list_head *list, char *name)
1965 struct evsel *evsel;
1967 __evlist__for_each_entry(list, evsel) {
1968 if (!evsel->name)
1969 evsel->name = strdup(name);
1972 return 0;
1975 static int
1976 comp_pmu(const void *p1, const void *p2)
1978 struct perf_pmu_event_symbol *pmu1 = (struct perf_pmu_event_symbol *) p1;
1979 struct perf_pmu_event_symbol *pmu2 = (struct perf_pmu_event_symbol *) p2;
1981 return strcasecmp(pmu1->symbol, pmu2->symbol);
1984 static void perf_pmu__parse_cleanup(void)
1986 if (perf_pmu_events_list_num > 0) {
1987 struct perf_pmu_event_symbol *p;
1988 int i;
1990 for (i = 0; i < perf_pmu_events_list_num; i++) {
1991 p = perf_pmu_events_list + i;
1992 zfree(&p->symbol);
1994 zfree(&perf_pmu_events_list);
1995 perf_pmu_events_list_num = 0;
1999 #define SET_SYMBOL(str, stype) \
2000 do { \
2001 p->symbol = str; \
2002 if (!p->symbol) \
2003 goto err; \
2004 p->type = stype; \
2005 } while (0)
2008 * Read the pmu events list from sysfs
2009 * Save it into perf_pmu_events_list
2011 static void perf_pmu__parse_init(void)
2014 struct perf_pmu *pmu = NULL;
2015 struct perf_pmu_alias *alias;
2016 int len = 0;
2018 pmu = NULL;
2019 while ((pmu = perf_pmu__scan(pmu)) != NULL) {
2020 list_for_each_entry(alias, &pmu->aliases, list) {
2021 if (strchr(alias->name, '-'))
2022 len++;
2023 len++;
2027 if (len == 0) {
2028 perf_pmu_events_list_num = -1;
2029 return;
2031 perf_pmu_events_list = malloc(sizeof(struct perf_pmu_event_symbol) * len);
2032 if (!perf_pmu_events_list)
2033 return;
2034 perf_pmu_events_list_num = len;
2036 len = 0;
2037 pmu = NULL;
2038 while ((pmu = perf_pmu__scan(pmu)) != NULL) {
2039 list_for_each_entry(alias, &pmu->aliases, list) {
2040 struct perf_pmu_event_symbol *p = perf_pmu_events_list + len;
2041 char *tmp = strchr(alias->name, '-');
2043 if (tmp != NULL) {
2044 SET_SYMBOL(strndup(alias->name, tmp - alias->name),
2045 PMU_EVENT_SYMBOL_PREFIX);
2046 p++;
2047 SET_SYMBOL(strdup(++tmp), PMU_EVENT_SYMBOL_SUFFIX);
2048 len += 2;
2049 } else {
2050 SET_SYMBOL(strdup(alias->name), PMU_EVENT_SYMBOL);
2051 len++;
2055 qsort(perf_pmu_events_list, len,
2056 sizeof(struct perf_pmu_event_symbol), comp_pmu);
2058 return;
2059 err:
2060 perf_pmu__parse_cleanup();
2064 * This function injects special term in
2065 * perf_pmu_events_list so the test code
2066 * can check on this functionality.
2068 int perf_pmu__test_parse_init(void)
2070 struct perf_pmu_event_symbol *list;
2072 list = malloc(sizeof(*list) * 1);
2073 if (!list)
2074 return -ENOMEM;
2076 list->type = PMU_EVENT_SYMBOL;
2077 list->symbol = strdup("read");
2079 if (!list->symbol) {
2080 free(list);
2081 return -ENOMEM;
2084 perf_pmu_events_list = list;
2085 perf_pmu_events_list_num = 1;
2086 return 0;
2089 enum perf_pmu_event_symbol_type
2090 perf_pmu__parse_check(const char *name)
2092 struct perf_pmu_event_symbol p, *r;
2094 /* scan kernel pmu events from sysfs if needed */
2095 if (perf_pmu_events_list_num == 0)
2096 perf_pmu__parse_init();
2098 * name "cpu" could be prefix of cpu-cycles or cpu// events.
2099 * cpu-cycles has been handled by hardcode.
2100 * So it must be cpu// events, not kernel pmu event.
2102 if ((perf_pmu_events_list_num <= 0) || !strcmp(name, "cpu"))
2103 return PMU_EVENT_SYMBOL_ERR;
2105 p.symbol = strdup(name);
2106 r = bsearch(&p, perf_pmu_events_list,
2107 (size_t) perf_pmu_events_list_num,
2108 sizeof(struct perf_pmu_event_symbol), comp_pmu);
2109 zfree(&p.symbol);
2110 return r ? r->type : PMU_EVENT_SYMBOL_ERR;
2113 static int parse_events__scanner(const char *str,
2114 struct parse_events_state *parse_state)
2116 YY_BUFFER_STATE buffer;
2117 void *scanner;
2118 int ret;
2120 ret = parse_events_lex_init_extra(parse_state, &scanner);
2121 if (ret)
2122 return ret;
2124 buffer = parse_events__scan_string(str, scanner);
2126 #ifdef PARSER_DEBUG
2127 parse_events_debug = 1;
2128 parse_events_set_debug(1, scanner);
2129 #endif
2130 ret = parse_events_parse(parse_state, scanner);
2132 parse_events__flush_buffer(buffer, scanner);
2133 parse_events__delete_buffer(buffer, scanner);
2134 parse_events_lex_destroy(scanner);
2135 return ret;
2139 * parse event config string, return a list of event terms.
2141 int parse_events_terms(struct list_head *terms, const char *str)
2143 struct parse_events_state parse_state = {
2144 .terms = NULL,
2145 .stoken = PE_START_TERMS,
2147 int ret;
2149 ret = parse_events__scanner(str, &parse_state);
2150 perf_pmu__parse_cleanup();
2152 if (!ret) {
2153 list_splice(parse_state.terms, terms);
2154 zfree(&parse_state.terms);
2155 return 0;
2158 parse_events_terms__delete(parse_state.terms);
2159 return ret;
2162 int __parse_events(struct evlist *evlist, const char *str,
2163 struct parse_events_error *err, struct perf_pmu *fake_pmu)
2165 struct parse_events_state parse_state = {
2166 .list = LIST_HEAD_INIT(parse_state.list),
2167 .idx = evlist->core.nr_entries,
2168 .error = err,
2169 .evlist = evlist,
2170 .stoken = PE_START_EVENTS,
2171 .fake_pmu = fake_pmu,
2173 int ret;
2175 ret = parse_events__scanner(str, &parse_state);
2176 perf_pmu__parse_cleanup();
2178 if (!ret && list_empty(&parse_state.list)) {
2179 WARN_ONCE(true, "WARNING: event parser found nothing\n");
2180 return -1;
2184 * Add list to the evlist even with errors to allow callers to clean up.
2186 evlist__splice_list_tail(evlist, &parse_state.list);
2188 if (!ret) {
2189 struct evsel *last;
2191 evlist->nr_groups += parse_state.nr_groups;
2192 last = evlist__last(evlist);
2193 last->cmdline_group_boundary = true;
2195 return 0;
2199 * There are 2 users - builtin-record and builtin-test objects.
2200 * Both call evlist__delete in case of error, so we dont
2201 * need to bother.
2203 return ret;
2206 #define MAX_WIDTH 1000
2207 static int get_term_width(void)
2209 struct winsize ws;
2211 get_term_dimensions(&ws);
2212 return ws.ws_col > MAX_WIDTH ? MAX_WIDTH : ws.ws_col;
2215 static void __parse_events_print_error(int err_idx, const char *err_str,
2216 const char *err_help, const char *event)
2218 const char *str = "invalid or unsupported event: ";
2219 char _buf[MAX_WIDTH];
2220 char *buf = (char *) event;
2221 int idx = 0;
2222 if (err_str) {
2223 /* -2 for extra '' in the final fprintf */
2224 int width = get_term_width() - 2;
2225 int len_event = strlen(event);
2226 int len_str, max_len, cut = 0;
2229 * Maximum error index indent, we will cut
2230 * the event string if it's bigger.
2232 int max_err_idx = 13;
2235 * Let's be specific with the message when
2236 * we have the precise error.
2238 str = "event syntax error: ";
2239 len_str = strlen(str);
2240 max_len = width - len_str;
2242 buf = _buf;
2244 /* We're cutting from the beginning. */
2245 if (err_idx > max_err_idx)
2246 cut = err_idx - max_err_idx;
2248 strncpy(buf, event + cut, max_len);
2250 /* Mark cut parts with '..' on both sides. */
2251 if (cut)
2252 buf[0] = buf[1] = '.';
2254 if ((len_event - cut) > max_len) {
2255 buf[max_len - 1] = buf[max_len - 2] = '.';
2256 buf[max_len] = 0;
2259 idx = len_str + err_idx - cut;
2262 fprintf(stderr, "%s'%s'\n", str, buf);
2263 if (idx) {
2264 fprintf(stderr, "%*s\\___ %s\n", idx + 1, "", err_str);
2265 if (err_help)
2266 fprintf(stderr, "\n%s\n", err_help);
2270 void parse_events_print_error(struct parse_events_error *err,
2271 const char *event)
2273 if (!err->num_errors)
2274 return;
2276 __parse_events_print_error(err->idx, err->str, err->help, event);
2277 zfree(&err->str);
2278 zfree(&err->help);
2280 if (err->num_errors > 1) {
2281 fputs("\nInitial error:\n", stderr);
2282 __parse_events_print_error(err->first_idx, err->first_str,
2283 err->first_help, event);
2284 zfree(&err->first_str);
2285 zfree(&err->first_help);
2289 #undef MAX_WIDTH
2291 int parse_events_option(const struct option *opt, const char *str,
2292 int unset __maybe_unused)
2294 struct evlist *evlist = *(struct evlist **)opt->value;
2295 struct parse_events_error err;
2296 int ret;
2298 bzero(&err, sizeof(err));
2299 ret = parse_events(evlist, str, &err);
2301 if (ret) {
2302 parse_events_print_error(&err, str);
2303 fprintf(stderr, "Run 'perf list' for a list of valid events\n");
2306 return ret;
2309 int parse_events_option_new_evlist(const struct option *opt, const char *str, int unset)
2311 struct evlist **evlistp = opt->value;
2312 int ret;
2314 if (*evlistp == NULL) {
2315 *evlistp = evlist__new();
2317 if (*evlistp == NULL) {
2318 fprintf(stderr, "Not enough memory to create evlist\n");
2319 return -1;
2323 ret = parse_events_option(opt, str, unset);
2324 if (ret) {
2325 evlist__delete(*evlistp);
2326 *evlistp = NULL;
2329 return ret;
2332 static int
2333 foreach_evsel_in_last_glob(struct evlist *evlist,
2334 int (*func)(struct evsel *evsel,
2335 const void *arg),
2336 const void *arg)
2338 struct evsel *last = NULL;
2339 int err;
2342 * Don't return when list_empty, give func a chance to report
2343 * error when it found last == NULL.
2345 * So no need to WARN here, let *func do this.
2347 if (evlist->core.nr_entries > 0)
2348 last = evlist__last(evlist);
2350 do {
2351 err = (*func)(last, arg);
2352 if (err)
2353 return -1;
2354 if (!last)
2355 return 0;
2357 if (last->core.node.prev == &evlist->core.entries)
2358 return 0;
2359 last = list_entry(last->core.node.prev, struct evsel, core.node);
2360 } while (!last->cmdline_group_boundary);
2362 return 0;
2365 static int set_filter(struct evsel *evsel, const void *arg)
2367 const char *str = arg;
2368 bool found = false;
2369 int nr_addr_filters = 0;
2370 struct perf_pmu *pmu = NULL;
2372 if (evsel == NULL) {
2373 fprintf(stderr,
2374 "--filter option should follow a -e tracepoint or HW tracer option\n");
2375 return -1;
2378 if (evsel->core.attr.type == PERF_TYPE_TRACEPOINT) {
2379 if (evsel__append_tp_filter(evsel, str) < 0) {
2380 fprintf(stderr,
2381 "not enough memory to hold filter string\n");
2382 return -1;
2385 return 0;
2388 while ((pmu = perf_pmu__scan(pmu)) != NULL)
2389 if (pmu->type == evsel->core.attr.type) {
2390 found = true;
2391 break;
2394 if (found)
2395 perf_pmu__scan_file(pmu, "nr_addr_filters",
2396 "%d", &nr_addr_filters);
2398 if (!nr_addr_filters) {
2399 fprintf(stderr,
2400 "This CPU does not support address filtering\n");
2401 return -1;
2404 if (evsel__append_addr_filter(evsel, str) < 0) {
2405 fprintf(stderr,
2406 "not enough memory to hold filter string\n");
2407 return -1;
2410 return 0;
2413 int parse_filter(const struct option *opt, const char *str,
2414 int unset __maybe_unused)
2416 struct evlist *evlist = *(struct evlist **)opt->value;
2418 return foreach_evsel_in_last_glob(evlist, set_filter,
2419 (const void *)str);
2422 static int add_exclude_perf_filter(struct evsel *evsel,
2423 const void *arg __maybe_unused)
2425 char new_filter[64];
2427 if (evsel == NULL || evsel->core.attr.type != PERF_TYPE_TRACEPOINT) {
2428 fprintf(stderr,
2429 "--exclude-perf option should follow a -e tracepoint option\n");
2430 return -1;
2433 snprintf(new_filter, sizeof(new_filter), "common_pid != %d", getpid());
2435 if (evsel__append_tp_filter(evsel, new_filter) < 0) {
2436 fprintf(stderr,
2437 "not enough memory to hold filter string\n");
2438 return -1;
2441 return 0;
2444 int exclude_perf(const struct option *opt,
2445 const char *arg __maybe_unused,
2446 int unset __maybe_unused)
2448 struct evlist *evlist = *(struct evlist **)opt->value;
2450 return foreach_evsel_in_last_glob(evlist, add_exclude_perf_filter,
2451 NULL);
2454 static const char * const event_type_descriptors[] = {
2455 "Hardware event",
2456 "Software event",
2457 "Tracepoint event",
2458 "Hardware cache event",
2459 "Raw hardware event descriptor",
2460 "Hardware breakpoint",
2463 static int cmp_string(const void *a, const void *b)
2465 const char * const *as = a;
2466 const char * const *bs = b;
2468 return strcmp(*as, *bs);
2472 * Print the events from <debugfs_mount_point>/tracing/events
2475 void print_tracepoint_events(const char *subsys_glob, const char *event_glob,
2476 bool name_only)
2478 DIR *sys_dir, *evt_dir;
2479 struct dirent *sys_dirent, *evt_dirent;
2480 char evt_path[MAXPATHLEN];
2481 char *dir_path;
2482 char **evt_list = NULL;
2483 unsigned int evt_i = 0, evt_num = 0;
2484 bool evt_num_known = false;
2486 restart:
2487 sys_dir = tracing_events__opendir();
2488 if (!sys_dir)
2489 return;
2491 if (evt_num_known) {
2492 evt_list = zalloc(sizeof(char *) * evt_num);
2493 if (!evt_list)
2494 goto out_close_sys_dir;
2497 for_each_subsystem(sys_dir, sys_dirent) {
2498 if (subsys_glob != NULL &&
2499 !strglobmatch(sys_dirent->d_name, subsys_glob))
2500 continue;
2502 dir_path = get_events_file(sys_dirent->d_name);
2503 if (!dir_path)
2504 continue;
2505 evt_dir = opendir(dir_path);
2506 if (!evt_dir)
2507 goto next;
2509 for_each_event(dir_path, evt_dir, evt_dirent) {
2510 if (event_glob != NULL &&
2511 !strglobmatch(evt_dirent->d_name, event_glob))
2512 continue;
2514 if (!evt_num_known) {
2515 evt_num++;
2516 continue;
2519 snprintf(evt_path, MAXPATHLEN, "%s:%s",
2520 sys_dirent->d_name, evt_dirent->d_name);
2522 evt_list[evt_i] = strdup(evt_path);
2523 if (evt_list[evt_i] == NULL) {
2524 put_events_file(dir_path);
2525 goto out_close_evt_dir;
2527 evt_i++;
2529 closedir(evt_dir);
2530 next:
2531 put_events_file(dir_path);
2533 closedir(sys_dir);
2535 if (!evt_num_known) {
2536 evt_num_known = true;
2537 goto restart;
2539 qsort(evt_list, evt_num, sizeof(char *), cmp_string);
2540 evt_i = 0;
2541 while (evt_i < evt_num) {
2542 if (name_only) {
2543 printf("%s ", evt_list[evt_i++]);
2544 continue;
2546 printf(" %-50s [%s]\n", evt_list[evt_i++],
2547 event_type_descriptors[PERF_TYPE_TRACEPOINT]);
2549 if (evt_num && pager_in_use())
2550 printf("\n");
2552 out_free:
2553 evt_num = evt_i;
2554 for (evt_i = 0; evt_i < evt_num; evt_i++)
2555 zfree(&evt_list[evt_i]);
2556 zfree(&evt_list);
2557 return;
2559 out_close_evt_dir:
2560 closedir(evt_dir);
2561 out_close_sys_dir:
2562 closedir(sys_dir);
2564 printf("FATAL: not enough memory to print %s\n",
2565 event_type_descriptors[PERF_TYPE_TRACEPOINT]);
2566 if (evt_list)
2567 goto out_free;
2571 * Check whether event is in <debugfs_mount_point>/tracing/events
2574 int is_valid_tracepoint(const char *event_string)
2576 DIR *sys_dir, *evt_dir;
2577 struct dirent *sys_dirent, *evt_dirent;
2578 char evt_path[MAXPATHLEN];
2579 char *dir_path;
2581 sys_dir = tracing_events__opendir();
2582 if (!sys_dir)
2583 return 0;
2585 for_each_subsystem(sys_dir, sys_dirent) {
2586 dir_path = get_events_file(sys_dirent->d_name);
2587 if (!dir_path)
2588 continue;
2589 evt_dir = opendir(dir_path);
2590 if (!evt_dir)
2591 goto next;
2593 for_each_event(dir_path, evt_dir, evt_dirent) {
2594 snprintf(evt_path, MAXPATHLEN, "%s:%s",
2595 sys_dirent->d_name, evt_dirent->d_name);
2596 if (!strcmp(evt_path, event_string)) {
2597 closedir(evt_dir);
2598 closedir(sys_dir);
2599 return 1;
2602 closedir(evt_dir);
2603 next:
2604 put_events_file(dir_path);
2606 closedir(sys_dir);
2607 return 0;
2610 static bool is_event_supported(u8 type, unsigned config)
2612 bool ret = true;
2613 int open_return;
2614 struct evsel *evsel;
2615 struct perf_event_attr attr = {
2616 .type = type,
2617 .config = config,
2618 .disabled = 1,
2620 struct perf_thread_map *tmap = thread_map__new_by_tid(0);
2622 if (tmap == NULL)
2623 return false;
2625 evsel = evsel__new(&attr);
2626 if (evsel) {
2627 open_return = evsel__open(evsel, NULL, tmap);
2628 ret = open_return >= 0;
2630 if (open_return == -EACCES) {
2632 * This happens if the paranoid value
2633 * /proc/sys/kernel/perf_event_paranoid is set to 2
2634 * Re-run with exclude_kernel set; we don't do that
2635 * by default as some ARM machines do not support it.
2638 evsel->core.attr.exclude_kernel = 1;
2639 ret = evsel__open(evsel, NULL, tmap) >= 0;
2641 evsel__delete(evsel);
2644 perf_thread_map__put(tmap);
2645 return ret;
2648 void print_sdt_events(const char *subsys_glob, const char *event_glob,
2649 bool name_only)
2651 struct probe_cache *pcache;
2652 struct probe_cache_entry *ent;
2653 struct strlist *bidlist, *sdtlist;
2654 struct strlist_config cfg = {.dont_dupstr = true};
2655 struct str_node *nd, *nd2;
2656 char *buf, *path, *ptr = NULL;
2657 bool show_detail = false;
2658 int ret;
2660 sdtlist = strlist__new(NULL, &cfg);
2661 if (!sdtlist) {
2662 pr_debug("Failed to allocate new strlist for SDT\n");
2663 return;
2665 bidlist = build_id_cache__list_all(true);
2666 if (!bidlist) {
2667 pr_debug("Failed to get buildids: %d\n", errno);
2668 return;
2670 strlist__for_each_entry(nd, bidlist) {
2671 pcache = probe_cache__new(nd->s, NULL);
2672 if (!pcache)
2673 continue;
2674 list_for_each_entry(ent, &pcache->entries, node) {
2675 if (!ent->sdt)
2676 continue;
2677 if (subsys_glob &&
2678 !strglobmatch(ent->pev.group, subsys_glob))
2679 continue;
2680 if (event_glob &&
2681 !strglobmatch(ent->pev.event, event_glob))
2682 continue;
2683 ret = asprintf(&buf, "%s:%s@%s", ent->pev.group,
2684 ent->pev.event, nd->s);
2685 if (ret > 0)
2686 strlist__add(sdtlist, buf);
2688 probe_cache__delete(pcache);
2690 strlist__delete(bidlist);
2692 strlist__for_each_entry(nd, sdtlist) {
2693 buf = strchr(nd->s, '@');
2694 if (buf)
2695 *(buf++) = '\0';
2696 if (name_only) {
2697 printf("%s ", nd->s);
2698 continue;
2700 nd2 = strlist__next(nd);
2701 if (nd2) {
2702 ptr = strchr(nd2->s, '@');
2703 if (ptr)
2704 *ptr = '\0';
2705 if (strcmp(nd->s, nd2->s) == 0)
2706 show_detail = true;
2708 if (show_detail) {
2709 path = build_id_cache__origname(buf);
2710 ret = asprintf(&buf, "%s@%s(%.12s)", nd->s, path, buf);
2711 if (ret > 0) {
2712 printf(" %-50s [%s]\n", buf, "SDT event");
2713 free(buf);
2715 free(path);
2716 } else
2717 printf(" %-50s [%s]\n", nd->s, "SDT event");
2718 if (nd2) {
2719 if (strcmp(nd->s, nd2->s) != 0)
2720 show_detail = false;
2721 if (ptr)
2722 *ptr = '@';
2725 strlist__delete(sdtlist);
2728 int print_hwcache_events(const char *event_glob, bool name_only)
2730 unsigned int type, op, i, evt_i = 0, evt_num = 0;
2731 char name[64];
2732 char **evt_list = NULL;
2733 bool evt_num_known = false;
2735 restart:
2736 if (evt_num_known) {
2737 evt_list = zalloc(sizeof(char *) * evt_num);
2738 if (!evt_list)
2739 goto out_enomem;
2742 for (type = 0; type < PERF_COUNT_HW_CACHE_MAX; type++) {
2743 for (op = 0; op < PERF_COUNT_HW_CACHE_OP_MAX; op++) {
2744 /* skip invalid cache type */
2745 if (!evsel__is_cache_op_valid(type, op))
2746 continue;
2748 for (i = 0; i < PERF_COUNT_HW_CACHE_RESULT_MAX; i++) {
2749 __evsel__hw_cache_type_op_res_name(type, op, i, name, sizeof(name));
2750 if (event_glob != NULL && !strglobmatch(name, event_glob))
2751 continue;
2753 if (!is_event_supported(PERF_TYPE_HW_CACHE,
2754 type | (op << 8) | (i << 16)))
2755 continue;
2757 if (!evt_num_known) {
2758 evt_num++;
2759 continue;
2762 evt_list[evt_i] = strdup(name);
2763 if (evt_list[evt_i] == NULL)
2764 goto out_enomem;
2765 evt_i++;
2770 if (!evt_num_known) {
2771 evt_num_known = true;
2772 goto restart;
2774 qsort(evt_list, evt_num, sizeof(char *), cmp_string);
2775 evt_i = 0;
2776 while (evt_i < evt_num) {
2777 if (name_only) {
2778 printf("%s ", evt_list[evt_i++]);
2779 continue;
2781 printf(" %-50s [%s]\n", evt_list[evt_i++],
2782 event_type_descriptors[PERF_TYPE_HW_CACHE]);
2784 if (evt_num && pager_in_use())
2785 printf("\n");
2787 out_free:
2788 evt_num = evt_i;
2789 for (evt_i = 0; evt_i < evt_num; evt_i++)
2790 zfree(&evt_list[evt_i]);
2791 zfree(&evt_list);
2792 return evt_num;
2794 out_enomem:
2795 printf("FATAL: not enough memory to print %s\n", event_type_descriptors[PERF_TYPE_HW_CACHE]);
2796 if (evt_list)
2797 goto out_free;
2798 return evt_num;
2801 static void print_tool_event(const char *name, const char *event_glob,
2802 bool name_only)
2804 if (event_glob && !strglobmatch(name, event_glob))
2805 return;
2806 if (name_only)
2807 printf("%s ", name);
2808 else
2809 printf(" %-50s [%s]\n", name, "Tool event");
2813 void print_tool_events(const char *event_glob, bool name_only)
2815 print_tool_event("duration_time", event_glob, name_only);
2816 if (pager_in_use())
2817 printf("\n");
2820 void print_symbol_events(const char *event_glob, unsigned type,
2821 struct event_symbol *syms, unsigned max,
2822 bool name_only)
2824 unsigned int i, evt_i = 0, evt_num = 0;
2825 char name[MAX_NAME_LEN];
2826 char **evt_list = NULL;
2827 bool evt_num_known = false;
2829 restart:
2830 if (evt_num_known) {
2831 evt_list = zalloc(sizeof(char *) * evt_num);
2832 if (!evt_list)
2833 goto out_enomem;
2834 syms -= max;
2837 for (i = 0; i < max; i++, syms++) {
2839 if (event_glob != NULL && syms->symbol != NULL &&
2840 !(strglobmatch(syms->symbol, event_glob) ||
2841 (syms->alias && strglobmatch(syms->alias, event_glob))))
2842 continue;
2844 if (!is_event_supported(type, i))
2845 continue;
2847 if (!evt_num_known) {
2848 evt_num++;
2849 continue;
2852 if (!name_only && strlen(syms->alias))
2853 snprintf(name, MAX_NAME_LEN, "%s OR %s", syms->symbol, syms->alias);
2854 else
2855 strlcpy(name, syms->symbol, MAX_NAME_LEN);
2857 evt_list[evt_i] = strdup(name);
2858 if (evt_list[evt_i] == NULL)
2859 goto out_enomem;
2860 evt_i++;
2863 if (!evt_num_known) {
2864 evt_num_known = true;
2865 goto restart;
2867 qsort(evt_list, evt_num, sizeof(char *), cmp_string);
2868 evt_i = 0;
2869 while (evt_i < evt_num) {
2870 if (name_only) {
2871 printf("%s ", evt_list[evt_i++]);
2872 continue;
2874 printf(" %-50s [%s]\n", evt_list[evt_i++], event_type_descriptors[type]);
2876 if (evt_num && pager_in_use())
2877 printf("\n");
2879 out_free:
2880 evt_num = evt_i;
2881 for (evt_i = 0; evt_i < evt_num; evt_i++)
2882 zfree(&evt_list[evt_i]);
2883 zfree(&evt_list);
2884 return;
2886 out_enomem:
2887 printf("FATAL: not enough memory to print %s\n", event_type_descriptors[type]);
2888 if (evt_list)
2889 goto out_free;
2893 * Print the help text for the event symbols:
2895 void print_events(const char *event_glob, bool name_only, bool quiet_flag,
2896 bool long_desc, bool details_flag, bool deprecated)
2898 print_symbol_events(event_glob, PERF_TYPE_HARDWARE,
2899 event_symbols_hw, PERF_COUNT_HW_MAX, name_only);
2901 print_symbol_events(event_glob, PERF_TYPE_SOFTWARE,
2902 event_symbols_sw, PERF_COUNT_SW_MAX, name_only);
2903 print_tool_events(event_glob, name_only);
2905 print_hwcache_events(event_glob, name_only);
2907 print_pmu_events(event_glob, name_only, quiet_flag, long_desc,
2908 details_flag, deprecated);
2910 if (event_glob != NULL)
2911 return;
2913 if (!name_only) {
2914 printf(" %-50s [%s]\n",
2915 "rNNN",
2916 event_type_descriptors[PERF_TYPE_RAW]);
2917 printf(" %-50s [%s]\n",
2918 "cpu/t1=v1[,t2=v2,t3 ...]/modifier",
2919 event_type_descriptors[PERF_TYPE_RAW]);
2920 if (pager_in_use())
2921 printf(" (see 'man perf-list' on how to encode it)\n\n");
2923 printf(" %-50s [%s]\n",
2924 "mem:<addr>[/len][:access]",
2925 event_type_descriptors[PERF_TYPE_BREAKPOINT]);
2926 if (pager_in_use())
2927 printf("\n");
2930 print_tracepoint_events(NULL, NULL, name_only);
2932 print_sdt_events(NULL, NULL, name_only);
2934 metricgroup__print(true, true, NULL, name_only, details_flag);
2936 print_libpfm_events(name_only, long_desc);
2939 int parse_events__is_hardcoded_term(struct parse_events_term *term)
2941 return term->type_term != PARSE_EVENTS__TERM_TYPE_USER;
2944 static int new_term(struct parse_events_term **_term,
2945 struct parse_events_term *temp,
2946 char *str, u64 num)
2948 struct parse_events_term *term;
2950 term = malloc(sizeof(*term));
2951 if (!term)
2952 return -ENOMEM;
2954 *term = *temp;
2955 INIT_LIST_HEAD(&term->list);
2956 term->weak = false;
2958 switch (term->type_val) {
2959 case PARSE_EVENTS__TERM_TYPE_NUM:
2960 term->val.num = num;
2961 break;
2962 case PARSE_EVENTS__TERM_TYPE_STR:
2963 term->val.str = str;
2964 break;
2965 default:
2966 free(term);
2967 return -EINVAL;
2970 *_term = term;
2971 return 0;
2974 int parse_events_term__num(struct parse_events_term **term,
2975 int type_term, char *config, u64 num,
2976 bool no_value,
2977 void *loc_term_, void *loc_val_)
2979 YYLTYPE *loc_term = loc_term_;
2980 YYLTYPE *loc_val = loc_val_;
2982 struct parse_events_term temp = {
2983 .type_val = PARSE_EVENTS__TERM_TYPE_NUM,
2984 .type_term = type_term,
2985 .config = config,
2986 .no_value = no_value,
2987 .err_term = loc_term ? loc_term->first_column : 0,
2988 .err_val = loc_val ? loc_val->first_column : 0,
2991 return new_term(term, &temp, NULL, num);
2994 int parse_events_term__str(struct parse_events_term **term,
2995 int type_term, char *config, char *str,
2996 void *loc_term_, void *loc_val_)
2998 YYLTYPE *loc_term = loc_term_;
2999 YYLTYPE *loc_val = loc_val_;
3001 struct parse_events_term temp = {
3002 .type_val = PARSE_EVENTS__TERM_TYPE_STR,
3003 .type_term = type_term,
3004 .config = config,
3005 .err_term = loc_term ? loc_term->first_column : 0,
3006 .err_val = loc_val ? loc_val->first_column : 0,
3009 return new_term(term, &temp, str, 0);
3012 int parse_events_term__sym_hw(struct parse_events_term **term,
3013 char *config, unsigned idx)
3015 struct event_symbol *sym;
3016 char *str;
3017 struct parse_events_term temp = {
3018 .type_val = PARSE_EVENTS__TERM_TYPE_STR,
3019 .type_term = PARSE_EVENTS__TERM_TYPE_USER,
3020 .config = config,
3023 if (!temp.config) {
3024 temp.config = strdup("event");
3025 if (!temp.config)
3026 return -ENOMEM;
3028 BUG_ON(idx >= PERF_COUNT_HW_MAX);
3029 sym = &event_symbols_hw[idx];
3031 str = strdup(sym->symbol);
3032 if (!str)
3033 return -ENOMEM;
3034 return new_term(term, &temp, str, 0);
3037 int parse_events_term__clone(struct parse_events_term **new,
3038 struct parse_events_term *term)
3040 char *str;
3041 struct parse_events_term temp = {
3042 .type_val = term->type_val,
3043 .type_term = term->type_term,
3044 .config = NULL,
3045 .err_term = term->err_term,
3046 .err_val = term->err_val,
3049 if (term->config) {
3050 temp.config = strdup(term->config);
3051 if (!temp.config)
3052 return -ENOMEM;
3054 if (term->type_val == PARSE_EVENTS__TERM_TYPE_NUM)
3055 return new_term(new, &temp, NULL, term->val.num);
3057 str = strdup(term->val.str);
3058 if (!str)
3059 return -ENOMEM;
3060 return new_term(new, &temp, str, 0);
3063 void parse_events_term__delete(struct parse_events_term *term)
3065 if (term->array.nr_ranges)
3066 zfree(&term->array.ranges);
3068 if (term->type_val != PARSE_EVENTS__TERM_TYPE_NUM)
3069 zfree(&term->val.str);
3071 zfree(&term->config);
3072 free(term);
3075 int parse_events_copy_term_list(struct list_head *old,
3076 struct list_head **new)
3078 struct parse_events_term *term, *n;
3079 int ret;
3081 if (!old) {
3082 *new = NULL;
3083 return 0;
3086 *new = malloc(sizeof(struct list_head));
3087 if (!*new)
3088 return -ENOMEM;
3089 INIT_LIST_HEAD(*new);
3091 list_for_each_entry (term, old, list) {
3092 ret = parse_events_term__clone(&n, term);
3093 if (ret)
3094 return ret;
3095 list_add_tail(&n->list, *new);
3097 return 0;
3100 void parse_events_terms__purge(struct list_head *terms)
3102 struct parse_events_term *term, *h;
3104 list_for_each_entry_safe(term, h, terms, list) {
3105 list_del_init(&term->list);
3106 parse_events_term__delete(term);
3110 void parse_events_terms__delete(struct list_head *terms)
3112 if (!terms)
3113 return;
3114 parse_events_terms__purge(terms);
3115 free(terms);
3118 void parse_events__clear_array(struct parse_events_array *a)
3120 zfree(&a->ranges);
3123 void parse_events_evlist_error(struct parse_events_state *parse_state,
3124 int idx, const char *str)
3126 if (!parse_state->error)
3127 return;
3129 parse_events__handle_error(parse_state->error, idx, strdup(str), NULL);
3132 static void config_terms_list(char *buf, size_t buf_sz)
3134 int i;
3135 bool first = true;
3137 buf[0] = '\0';
3138 for (i = 0; i < __PARSE_EVENTS__TERM_TYPE_NR; i++) {
3139 const char *name = config_term_names[i];
3141 if (!config_term_avail(i, NULL))
3142 continue;
3143 if (!name)
3144 continue;
3145 if (name[0] == '<')
3146 continue;
3148 if (strlen(buf) + strlen(name) + 2 >= buf_sz)
3149 return;
3151 if (!first)
3152 strcat(buf, ",");
3153 else
3154 first = false;
3155 strcat(buf, name);
3160 * Return string contains valid config terms of an event.
3161 * @additional_terms: For terms such as PMU sysfs terms.
3163 char *parse_events_formats_error_string(char *additional_terms)
3165 char *str;
3166 /* "no-overwrite" is the longest name */
3167 char static_terms[__PARSE_EVENTS__TERM_TYPE_NR *
3168 (sizeof("no-overwrite") - 1)];
3170 config_terms_list(static_terms, sizeof(static_terms));
3171 /* valid terms */
3172 if (additional_terms) {
3173 if (asprintf(&str, "valid terms: %s,%s",
3174 additional_terms, static_terms) < 0)
3175 goto fail;
3176 } else {
3177 if (asprintf(&str, "valid terms: %s", static_terms) < 0)
3178 goto fail;
3180 return str;
3182 fail:
3183 return NULL;