]> git.proxmox.com Git - mirror_ubuntu-focal-kernel.git/blob - tools/perf/util/parse-events.c
a33a1d5059a27ddf341aa496881a458ed9a78491
[mirror_ubuntu-focal-kernel.git] / tools / perf / util / parse-events.c
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 int
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
38 #define MAX_NAME_LEN 100
39
40 #ifdef PARSER_DEBUG
41 extern int parse_events_debug;
42 #endif
43 int parse_events_parse(void *parse_state, void *scanner);
44 static int get_config_terms(struct list_head *head_config,
45 struct list_head *head_terms __maybe_unused);
46
47 static struct perf_pmu_event_symbol *perf_pmu_events_list;
48 /*
49 * The variable indicates the number of supported pmu event symbols.
50 * 0 means not initialized and ready to init
51 * -1 means failed to init, don't try anymore
52 * >0 is the number of supported pmu event symbols
53 */
54 static int perf_pmu_events_list_num;
55
56 struct event_symbol event_symbols_hw[PERF_COUNT_HW_MAX] = {
57 [PERF_COUNT_HW_CPU_CYCLES] = {
58 .symbol = "cpu-cycles",
59 .alias = "cycles",
60 },
61 [PERF_COUNT_HW_INSTRUCTIONS] = {
62 .symbol = "instructions",
63 .alias = "",
64 },
65 [PERF_COUNT_HW_CACHE_REFERENCES] = {
66 .symbol = "cache-references",
67 .alias = "",
68 },
69 [PERF_COUNT_HW_CACHE_MISSES] = {
70 .symbol = "cache-misses",
71 .alias = "",
72 },
73 [PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = {
74 .symbol = "branch-instructions",
75 .alias = "branches",
76 },
77 [PERF_COUNT_HW_BRANCH_MISSES] = {
78 .symbol = "branch-misses",
79 .alias = "",
80 },
81 [PERF_COUNT_HW_BUS_CYCLES] = {
82 .symbol = "bus-cycles",
83 .alias = "",
84 },
85 [PERF_COUNT_HW_STALLED_CYCLES_FRONTEND] = {
86 .symbol = "stalled-cycles-frontend",
87 .alias = "idle-cycles-frontend",
88 },
89 [PERF_COUNT_HW_STALLED_CYCLES_BACKEND] = {
90 .symbol = "stalled-cycles-backend",
91 .alias = "idle-cycles-backend",
92 },
93 [PERF_COUNT_HW_REF_CPU_CYCLES] = {
94 .symbol = "ref-cycles",
95 .alias = "",
96 },
97 };
98
99 struct event_symbol event_symbols_sw[PERF_COUNT_SW_MAX] = {
100 [PERF_COUNT_SW_CPU_CLOCK] = {
101 .symbol = "cpu-clock",
102 .alias = "",
103 },
104 [PERF_COUNT_SW_TASK_CLOCK] = {
105 .symbol = "task-clock",
106 .alias = "",
107 },
108 [PERF_COUNT_SW_PAGE_FAULTS] = {
109 .symbol = "page-faults",
110 .alias = "faults",
111 },
112 [PERF_COUNT_SW_CONTEXT_SWITCHES] = {
113 .symbol = "context-switches",
114 .alias = "cs",
115 },
116 [PERF_COUNT_SW_CPU_MIGRATIONS] = {
117 .symbol = "cpu-migrations",
118 .alias = "migrations",
119 },
120 [PERF_COUNT_SW_PAGE_FAULTS_MIN] = {
121 .symbol = "minor-faults",
122 .alias = "",
123 },
124 [PERF_COUNT_SW_PAGE_FAULTS_MAJ] = {
125 .symbol = "major-faults",
126 .alias = "",
127 },
128 [PERF_COUNT_SW_ALIGNMENT_FAULTS] = {
129 .symbol = "alignment-faults",
130 .alias = "",
131 },
132 [PERF_COUNT_SW_EMULATION_FAULTS] = {
133 .symbol = "emulation-faults",
134 .alias = "",
135 },
136 [PERF_COUNT_SW_DUMMY] = {
137 .symbol = "dummy",
138 .alias = "",
139 },
140 [PERF_COUNT_SW_BPF_OUTPUT] = {
141 .symbol = "bpf-output",
142 .alias = "",
143 },
144 };
145
146 #define __PERF_EVENT_FIELD(config, name) \
147 ((config & PERF_EVENT_##name##_MASK) >> PERF_EVENT_##name##_SHIFT)
148
149 #define PERF_EVENT_RAW(config) __PERF_EVENT_FIELD(config, RAW)
150 #define PERF_EVENT_CONFIG(config) __PERF_EVENT_FIELD(config, CONFIG)
151 #define PERF_EVENT_TYPE(config) __PERF_EVENT_FIELD(config, TYPE)
152 #define PERF_EVENT_ID(config) __PERF_EVENT_FIELD(config, EVENT)
153
154 #define for_each_subsystem(sys_dir, sys_dirent) \
155 while ((sys_dirent = readdir(sys_dir)) != NULL) \
156 if (sys_dirent->d_type == DT_DIR && \
157 (strcmp(sys_dirent->d_name, ".")) && \
158 (strcmp(sys_dirent->d_name, "..")))
159
160 static int tp_event_has_id(const char *dir_path, struct dirent *evt_dir)
161 {
162 char evt_path[MAXPATHLEN];
163 int fd;
164
165 snprintf(evt_path, MAXPATHLEN, "%s/%s/id", dir_path, evt_dir->d_name);
166 fd = open(evt_path, O_RDONLY);
167 if (fd < 0)
168 return -EINVAL;
169 close(fd);
170
171 return 0;
172 }
173
174 #define for_each_event(dir_path, evt_dir, evt_dirent) \
175 while ((evt_dirent = readdir(evt_dir)) != NULL) \
176 if (evt_dirent->d_type == DT_DIR && \
177 (strcmp(evt_dirent->d_name, ".")) && \
178 (strcmp(evt_dirent->d_name, "..")) && \
179 (!tp_event_has_id(dir_path, evt_dirent)))
180
181 #define MAX_EVENT_LENGTH 512
182
183
184 struct tracepoint_path *tracepoint_id_to_path(u64 config)
185 {
186 struct tracepoint_path *path = NULL;
187 DIR *sys_dir, *evt_dir;
188 struct dirent *sys_dirent, *evt_dirent;
189 char id_buf[24];
190 int fd;
191 u64 id;
192 char evt_path[MAXPATHLEN];
193 char *dir_path;
194
195 sys_dir = tracing_events__opendir();
196 if (!sys_dir)
197 return NULL;
198
199 for_each_subsystem(sys_dir, sys_dirent) {
200 dir_path = get_events_file(sys_dirent->d_name);
201 if (!dir_path)
202 continue;
203 evt_dir = opendir(dir_path);
204 if (!evt_dir)
205 goto next;
206
207 for_each_event(dir_path, evt_dir, evt_dirent) {
208
209 scnprintf(evt_path, MAXPATHLEN, "%s/%s/id", dir_path,
210 evt_dirent->d_name);
211 fd = open(evt_path, O_RDONLY);
212 if (fd < 0)
213 continue;
214 if (read(fd, id_buf, sizeof(id_buf)) < 0) {
215 close(fd);
216 continue;
217 }
218 close(fd);
219 id = atoll(id_buf);
220 if (id == config) {
221 put_events_file(dir_path);
222 closedir(evt_dir);
223 closedir(sys_dir);
224 path = zalloc(sizeof(*path));
225 if (!path)
226 return NULL;
227 path->system = malloc(MAX_EVENT_LENGTH);
228 if (!path->system) {
229 free(path);
230 return NULL;
231 }
232 path->name = malloc(MAX_EVENT_LENGTH);
233 if (!path->name) {
234 zfree(&path->system);
235 free(path);
236 return NULL;
237 }
238 strncpy(path->system, sys_dirent->d_name,
239 MAX_EVENT_LENGTH);
240 strncpy(path->name, evt_dirent->d_name,
241 MAX_EVENT_LENGTH);
242 return path;
243 }
244 }
245 closedir(evt_dir);
246 next:
247 put_events_file(dir_path);
248 }
249
250 closedir(sys_dir);
251 return NULL;
252 }
253
254 struct tracepoint_path *tracepoint_name_to_path(const char *name)
255 {
256 struct tracepoint_path *path = zalloc(sizeof(*path));
257 char *str = strchr(name, ':');
258
259 if (path == NULL || str == NULL) {
260 free(path);
261 return NULL;
262 }
263
264 path->system = strndup(name, str - name);
265 path->name = strdup(str+1);
266
267 if (path->system == NULL || path->name == NULL) {
268 zfree(&path->system);
269 zfree(&path->name);
270 zfree(&path);
271 }
272
273 return path;
274 }
275
276 const char *event_type(int type)
277 {
278 switch (type) {
279 case PERF_TYPE_HARDWARE:
280 return "hardware";
281
282 case PERF_TYPE_SOFTWARE:
283 return "software";
284
285 case PERF_TYPE_TRACEPOINT:
286 return "tracepoint";
287
288 case PERF_TYPE_HW_CACHE:
289 return "hardware-cache";
290
291 default:
292 break;
293 }
294
295 return "unknown";
296 }
297
298 static int parse_events__is_name_term(struct parse_events_term *term)
299 {
300 return term->type_term == PARSE_EVENTS__TERM_TYPE_NAME;
301 }
302
303 static char *get_config_name(struct list_head *head_terms)
304 {
305 struct parse_events_term *term;
306
307 if (!head_terms)
308 return NULL;
309
310 list_for_each_entry(term, head_terms, list)
311 if (parse_events__is_name_term(term))
312 return term->val.str;
313
314 return NULL;
315 }
316
317 static struct evsel *
318 __add_event(struct list_head *list, int *idx,
319 struct perf_event_attr *attr,
320 char *name, struct perf_pmu *pmu,
321 struct list_head *config_terms, bool auto_merge_stats,
322 const char *cpu_list)
323 {
324 struct evsel *evsel;
325 struct perf_cpu_map *cpus = pmu ? pmu->cpus :
326 cpu_list ? perf_cpu_map__new(cpu_list) : NULL;
327
328 event_attr_init(attr);
329
330 evsel = perf_evsel__new_idx(attr, *idx);
331 if (!evsel)
332 return NULL;
333
334 (*idx)++;
335 evsel->core.cpus = perf_cpu_map__get(cpus);
336 evsel->core.own_cpus = perf_cpu_map__get(cpus);
337 evsel->system_wide = pmu ? pmu->is_uncore : false;
338 evsel->auto_merge_stats = auto_merge_stats;
339
340 if (name)
341 evsel->name = strdup(name);
342
343 if (config_terms)
344 list_splice(config_terms, &evsel->config_terms);
345
346 list_add_tail(&evsel->core.node, list);
347 return evsel;
348 }
349
350 static int add_event(struct list_head *list, int *idx,
351 struct perf_event_attr *attr, char *name,
352 struct list_head *config_terms)
353 {
354 return __add_event(list, idx, attr, name, NULL, config_terms, false, NULL) ? 0 : -ENOMEM;
355 }
356
357 static int add_event_tool(struct list_head *list, int *idx,
358 enum perf_tool_event tool_event)
359 {
360 struct evsel *evsel;
361 struct perf_event_attr attr = {
362 .type = PERF_TYPE_SOFTWARE,
363 .config = PERF_COUNT_SW_DUMMY,
364 };
365
366 evsel = __add_event(list, idx, &attr, NULL, NULL, NULL, false, "0");
367 if (!evsel)
368 return -ENOMEM;
369 evsel->tool_event = tool_event;
370 if (tool_event == PERF_TOOL_DURATION_TIME)
371 evsel->unit = strdup("ns");
372 return 0;
373 }
374
375 static int parse_aliases(char *str, const char *names[][PERF_EVSEL__MAX_ALIASES], int size)
376 {
377 int i, j;
378 int n, longest = -1;
379
380 for (i = 0; i < size; i++) {
381 for (j = 0; j < PERF_EVSEL__MAX_ALIASES && names[i][j]; j++) {
382 n = strlen(names[i][j]);
383 if (n > longest && !strncasecmp(str, names[i][j], n))
384 longest = n;
385 }
386 if (longest > 0)
387 return i;
388 }
389
390 return -1;
391 }
392
393 typedef int config_term_func_t(struct perf_event_attr *attr,
394 struct parse_events_term *term,
395 struct parse_events_error *err);
396 static int config_term_common(struct perf_event_attr *attr,
397 struct parse_events_term *term,
398 struct parse_events_error *err);
399 static int config_attr(struct perf_event_attr *attr,
400 struct list_head *head,
401 struct parse_events_error *err,
402 config_term_func_t config_term);
403
404 int parse_events_add_cache(struct list_head *list, int *idx,
405 char *type, char *op_result1, char *op_result2,
406 struct parse_events_error *err,
407 struct list_head *head_config)
408 {
409 struct perf_event_attr attr;
410 LIST_HEAD(config_terms);
411 char name[MAX_NAME_LEN], *config_name;
412 int cache_type = -1, cache_op = -1, cache_result = -1;
413 char *op_result[2] = { op_result1, op_result2 };
414 int i, n;
415
416 /*
417 * No fallback - if we cannot get a clear cache type
418 * then bail out:
419 */
420 cache_type = parse_aliases(type, perf_evsel__hw_cache,
421 PERF_COUNT_HW_CACHE_MAX);
422 if (cache_type == -1)
423 return -EINVAL;
424
425 config_name = get_config_name(head_config);
426 n = snprintf(name, MAX_NAME_LEN, "%s", type);
427
428 for (i = 0; (i < 2) && (op_result[i]); i++) {
429 char *str = op_result[i];
430
431 n += snprintf(name + n, MAX_NAME_LEN - n, "-%s", str);
432
433 if (cache_op == -1) {
434 cache_op = parse_aliases(str, perf_evsel__hw_cache_op,
435 PERF_COUNT_HW_CACHE_OP_MAX);
436 if (cache_op >= 0) {
437 if (!perf_evsel__is_cache_op_valid(cache_type, cache_op))
438 return -EINVAL;
439 continue;
440 }
441 }
442
443 if (cache_result == -1) {
444 cache_result = parse_aliases(str, perf_evsel__hw_cache_result,
445 PERF_COUNT_HW_CACHE_RESULT_MAX);
446 if (cache_result >= 0)
447 continue;
448 }
449 }
450
451 /*
452 * Fall back to reads:
453 */
454 if (cache_op == -1)
455 cache_op = PERF_COUNT_HW_CACHE_OP_READ;
456
457 /*
458 * Fall back to accesses:
459 */
460 if (cache_result == -1)
461 cache_result = PERF_COUNT_HW_CACHE_RESULT_ACCESS;
462
463 memset(&attr, 0, sizeof(attr));
464 attr.config = cache_type | (cache_op << 8) | (cache_result << 16);
465 attr.type = PERF_TYPE_HW_CACHE;
466
467 if (head_config) {
468 if (config_attr(&attr, head_config, err,
469 config_term_common))
470 return -EINVAL;
471
472 if (get_config_terms(head_config, &config_terms))
473 return -ENOMEM;
474 }
475 return add_event(list, idx, &attr, config_name ? : name, &config_terms);
476 }
477
478 static void tracepoint_error(struct parse_events_error *e, int err,
479 const char *sys, const char *name)
480 {
481 char help[BUFSIZ];
482
483 if (!e)
484 return;
485
486 /*
487 * We get error directly from syscall errno ( > 0),
488 * or from encoded pointer's error ( < 0).
489 */
490 err = abs(err);
491
492 switch (err) {
493 case EACCES:
494 e->str = strdup("can't access trace events");
495 break;
496 case ENOENT:
497 e->str = strdup("unknown tracepoint");
498 break;
499 default:
500 e->str = strdup("failed to add tracepoint");
501 break;
502 }
503
504 tracing_path__strerror_open_tp(err, help, sizeof(help), sys, name);
505 e->help = strdup(help);
506 }
507
508 static int add_tracepoint(struct list_head *list, int *idx,
509 const char *sys_name, const char *evt_name,
510 struct parse_events_error *err,
511 struct list_head *head_config)
512 {
513 struct evsel *evsel;
514
515 evsel = perf_evsel__newtp_idx(sys_name, evt_name, (*idx)++);
516 if (IS_ERR(evsel)) {
517 tracepoint_error(err, PTR_ERR(evsel), sys_name, evt_name);
518 return PTR_ERR(evsel);
519 }
520
521 if (head_config) {
522 LIST_HEAD(config_terms);
523
524 if (get_config_terms(head_config, &config_terms))
525 return -ENOMEM;
526 list_splice(&config_terms, &evsel->config_terms);
527 }
528
529 list_add_tail(&evsel->core.node, list);
530 return 0;
531 }
532
533 static int add_tracepoint_multi_event(struct list_head *list, int *idx,
534 const char *sys_name, const char *evt_name,
535 struct parse_events_error *err,
536 struct list_head *head_config)
537 {
538 char *evt_path;
539 struct dirent *evt_ent;
540 DIR *evt_dir;
541 int ret = 0, found = 0;
542
543 evt_path = get_events_file(sys_name);
544 if (!evt_path) {
545 tracepoint_error(err, errno, sys_name, evt_name);
546 return -1;
547 }
548 evt_dir = opendir(evt_path);
549 if (!evt_dir) {
550 put_events_file(evt_path);
551 tracepoint_error(err, errno, sys_name, evt_name);
552 return -1;
553 }
554
555 while (!ret && (evt_ent = readdir(evt_dir))) {
556 if (!strcmp(evt_ent->d_name, ".")
557 || !strcmp(evt_ent->d_name, "..")
558 || !strcmp(evt_ent->d_name, "enable")
559 || !strcmp(evt_ent->d_name, "filter"))
560 continue;
561
562 if (!strglobmatch(evt_ent->d_name, evt_name))
563 continue;
564
565 found++;
566
567 ret = add_tracepoint(list, idx, sys_name, evt_ent->d_name,
568 err, head_config);
569 }
570
571 if (!found) {
572 tracepoint_error(err, ENOENT, sys_name, evt_name);
573 ret = -1;
574 }
575
576 put_events_file(evt_path);
577 closedir(evt_dir);
578 return ret;
579 }
580
581 static int add_tracepoint_event(struct list_head *list, int *idx,
582 const char *sys_name, const char *evt_name,
583 struct parse_events_error *err,
584 struct list_head *head_config)
585 {
586 return strpbrk(evt_name, "*?") ?
587 add_tracepoint_multi_event(list, idx, sys_name, evt_name,
588 err, head_config) :
589 add_tracepoint(list, idx, sys_name, evt_name,
590 err, head_config);
591 }
592
593 static int add_tracepoint_multi_sys(struct list_head *list, int *idx,
594 const char *sys_name, const char *evt_name,
595 struct parse_events_error *err,
596 struct list_head *head_config)
597 {
598 struct dirent *events_ent;
599 DIR *events_dir;
600 int ret = 0;
601
602 events_dir = tracing_events__opendir();
603 if (!events_dir) {
604 tracepoint_error(err, errno, sys_name, evt_name);
605 return -1;
606 }
607
608 while (!ret && (events_ent = readdir(events_dir))) {
609 if (!strcmp(events_ent->d_name, ".")
610 || !strcmp(events_ent->d_name, "..")
611 || !strcmp(events_ent->d_name, "enable")
612 || !strcmp(events_ent->d_name, "header_event")
613 || !strcmp(events_ent->d_name, "header_page"))
614 continue;
615
616 if (!strglobmatch(events_ent->d_name, sys_name))
617 continue;
618
619 ret = add_tracepoint_event(list, idx, events_ent->d_name,
620 evt_name, err, head_config);
621 }
622
623 closedir(events_dir);
624 return ret;
625 }
626
627 struct __add_bpf_event_param {
628 struct parse_events_state *parse_state;
629 struct list_head *list;
630 struct list_head *head_config;
631 };
632
633 static int add_bpf_event(const char *group, const char *event, int fd, struct bpf_object *obj,
634 void *_param)
635 {
636 LIST_HEAD(new_evsels);
637 struct __add_bpf_event_param *param = _param;
638 struct parse_events_state *parse_state = param->parse_state;
639 struct list_head *list = param->list;
640 struct evsel *pos;
641 int err;
642 /*
643 * Check if we should add the event, i.e. if it is a TP but starts with a '!',
644 * then don't add the tracepoint, this will be used for something else, like
645 * adding to a BPF_MAP_TYPE_PROG_ARRAY.
646 *
647 * See tools/perf/examples/bpf/augmented_raw_syscalls.c
648 */
649 if (group[0] == '!')
650 return 0;
651
652 pr_debug("add bpf event %s:%s and attach bpf program %d\n",
653 group, event, fd);
654
655 err = parse_events_add_tracepoint(&new_evsels, &parse_state->idx, group,
656 event, parse_state->error,
657 param->head_config);
658 if (err) {
659 struct evsel *evsel, *tmp;
660
661 pr_debug("Failed to add BPF event %s:%s\n",
662 group, event);
663 list_for_each_entry_safe(evsel, tmp, &new_evsels, core.node) {
664 list_del_init(&evsel->core.node);
665 evsel__delete(evsel);
666 }
667 return err;
668 }
669 pr_debug("adding %s:%s\n", group, event);
670
671 list_for_each_entry(pos, &new_evsels, core.node) {
672 pr_debug("adding %s:%s to %p\n",
673 group, event, pos);
674 pos->bpf_fd = fd;
675 pos->bpf_obj = obj;
676 }
677 list_splice(&new_evsels, list);
678 return 0;
679 }
680
681 int parse_events_load_bpf_obj(struct parse_events_state *parse_state,
682 struct list_head *list,
683 struct bpf_object *obj,
684 struct list_head *head_config)
685 {
686 int err;
687 char errbuf[BUFSIZ];
688 struct __add_bpf_event_param param = {parse_state, list, head_config};
689 static bool registered_unprobe_atexit = false;
690
691 if (IS_ERR(obj) || !obj) {
692 snprintf(errbuf, sizeof(errbuf),
693 "Internal error: load bpf obj with NULL");
694 err = -EINVAL;
695 goto errout;
696 }
697
698 /*
699 * Register atexit handler before calling bpf__probe() so
700 * bpf__probe() don't need to unprobe probe points its already
701 * created when failure.
702 */
703 if (!registered_unprobe_atexit) {
704 atexit(bpf__clear);
705 registered_unprobe_atexit = true;
706 }
707
708 err = bpf__probe(obj);
709 if (err) {
710 bpf__strerror_probe(obj, err, errbuf, sizeof(errbuf));
711 goto errout;
712 }
713
714 err = bpf__load(obj);
715 if (err) {
716 bpf__strerror_load(obj, err, errbuf, sizeof(errbuf));
717 goto errout;
718 }
719
720 err = bpf__foreach_event(obj, add_bpf_event, &param);
721 if (err) {
722 snprintf(errbuf, sizeof(errbuf),
723 "Attach events in BPF object failed");
724 goto errout;
725 }
726
727 return 0;
728 errout:
729 parse_state->error->help = strdup("(add -v to see detail)");
730 parse_state->error->str = strdup(errbuf);
731 return err;
732 }
733
734 static int
735 parse_events_config_bpf(struct parse_events_state *parse_state,
736 struct bpf_object *obj,
737 struct list_head *head_config)
738 {
739 struct parse_events_term *term;
740 int error_pos;
741
742 if (!head_config || list_empty(head_config))
743 return 0;
744
745 list_for_each_entry(term, head_config, list) {
746 char errbuf[BUFSIZ];
747 int err;
748
749 if (term->type_term != PARSE_EVENTS__TERM_TYPE_USER) {
750 snprintf(errbuf, sizeof(errbuf),
751 "Invalid config term for BPF object");
752 errbuf[BUFSIZ - 1] = '\0';
753
754 parse_state->error->idx = term->err_term;
755 parse_state->error->str = strdup(errbuf);
756 return -EINVAL;
757 }
758
759 err = bpf__config_obj(obj, term, parse_state->evlist, &error_pos);
760 if (err) {
761 bpf__strerror_config_obj(obj, term, parse_state->evlist,
762 &error_pos, err, errbuf,
763 sizeof(errbuf));
764 parse_state->error->help = strdup(
765 "Hint:\tValid config terms:\n"
766 " \tmap:[<arraymap>].value<indices>=[value]\n"
767 " \tmap:[<eventmap>].event<indices>=[event]\n"
768 "\n"
769 " \twhere <indices> is something like [0,3...5] or [all]\n"
770 " \t(add -v to see detail)");
771 parse_state->error->str = strdup(errbuf);
772 if (err == -BPF_LOADER_ERRNO__OBJCONF_MAP_VALUE)
773 parse_state->error->idx = term->err_val;
774 else
775 parse_state->error->idx = term->err_term + error_pos;
776 return err;
777 }
778 }
779 return 0;
780 }
781
782 /*
783 * Split config terms:
784 * perf record -e bpf.c/call-graph=fp,map:array.value[0]=1/ ...
785 * 'call-graph=fp' is 'evt config', should be applied to each
786 * events in bpf.c.
787 * 'map:array.value[0]=1' is 'obj config', should be processed
788 * with parse_events_config_bpf.
789 *
790 * Move object config terms from the first list to obj_head_config.
791 */
792 static void
793 split_bpf_config_terms(struct list_head *evt_head_config,
794 struct list_head *obj_head_config)
795 {
796 struct parse_events_term *term, *temp;
797
798 /*
799 * Currectly, all possible user config term
800 * belong to bpf object. parse_events__is_hardcoded_term()
801 * happends to be a good flag.
802 *
803 * See parse_events_config_bpf() and
804 * config_term_tracepoint().
805 */
806 list_for_each_entry_safe(term, temp, evt_head_config, list)
807 if (!parse_events__is_hardcoded_term(term))
808 list_move_tail(&term->list, obj_head_config);
809 }
810
811 int parse_events_load_bpf(struct parse_events_state *parse_state,
812 struct list_head *list,
813 char *bpf_file_name,
814 bool source,
815 struct list_head *head_config)
816 {
817 int err;
818 struct bpf_object *obj;
819 LIST_HEAD(obj_head_config);
820
821 if (head_config)
822 split_bpf_config_terms(head_config, &obj_head_config);
823
824 obj = bpf__prepare_load(bpf_file_name, source);
825 if (IS_ERR(obj)) {
826 char errbuf[BUFSIZ];
827
828 err = PTR_ERR(obj);
829
830 if (err == -ENOTSUP)
831 snprintf(errbuf, sizeof(errbuf),
832 "BPF support is not compiled");
833 else
834 bpf__strerror_prepare_load(bpf_file_name,
835 source,
836 -err, errbuf,
837 sizeof(errbuf));
838
839 parse_state->error->help = strdup("(add -v to see detail)");
840 parse_state->error->str = strdup(errbuf);
841 return err;
842 }
843
844 err = parse_events_load_bpf_obj(parse_state, list, obj, head_config);
845 if (err)
846 return err;
847 err = parse_events_config_bpf(parse_state, obj, &obj_head_config);
848
849 /*
850 * Caller doesn't know anything about obj_head_config,
851 * so combine them together again before returnning.
852 */
853 if (head_config)
854 list_splice_tail(&obj_head_config, head_config);
855 return err;
856 }
857
858 static int
859 parse_breakpoint_type(const char *type, struct perf_event_attr *attr)
860 {
861 int i;
862
863 for (i = 0; i < 3; i++) {
864 if (!type || !type[i])
865 break;
866
867 #define CHECK_SET_TYPE(bit) \
868 do { \
869 if (attr->bp_type & bit) \
870 return -EINVAL; \
871 else \
872 attr->bp_type |= bit; \
873 } while (0)
874
875 switch (type[i]) {
876 case 'r':
877 CHECK_SET_TYPE(HW_BREAKPOINT_R);
878 break;
879 case 'w':
880 CHECK_SET_TYPE(HW_BREAKPOINT_W);
881 break;
882 case 'x':
883 CHECK_SET_TYPE(HW_BREAKPOINT_X);
884 break;
885 default:
886 return -EINVAL;
887 }
888 }
889
890 #undef CHECK_SET_TYPE
891
892 if (!attr->bp_type) /* Default */
893 attr->bp_type = HW_BREAKPOINT_R | HW_BREAKPOINT_W;
894
895 return 0;
896 }
897
898 int parse_events_add_breakpoint(struct list_head *list, int *idx,
899 void *ptr, char *type, u64 len)
900 {
901 struct perf_event_attr attr;
902
903 memset(&attr, 0, sizeof(attr));
904 attr.bp_addr = (unsigned long) ptr;
905
906 if (parse_breakpoint_type(type, &attr))
907 return -EINVAL;
908
909 /* Provide some defaults if len is not specified */
910 if (!len) {
911 if (attr.bp_type == HW_BREAKPOINT_X)
912 len = sizeof(long);
913 else
914 len = HW_BREAKPOINT_LEN_4;
915 }
916
917 attr.bp_len = len;
918
919 attr.type = PERF_TYPE_BREAKPOINT;
920 attr.sample_period = 1;
921
922 return add_event(list, idx, &attr, NULL, NULL);
923 }
924
925 static int check_type_val(struct parse_events_term *term,
926 struct parse_events_error *err,
927 int type)
928 {
929 if (type == term->type_val)
930 return 0;
931
932 if (err) {
933 err->idx = term->err_val;
934 if (type == PARSE_EVENTS__TERM_TYPE_NUM)
935 err->str = strdup("expected numeric value");
936 else
937 err->str = strdup("expected string value");
938 }
939 return -EINVAL;
940 }
941
942 /*
943 * Update according to parse-events.l
944 */
945 static const char *config_term_names[__PARSE_EVENTS__TERM_TYPE_NR] = {
946 [PARSE_EVENTS__TERM_TYPE_USER] = "<sysfs term>",
947 [PARSE_EVENTS__TERM_TYPE_CONFIG] = "config",
948 [PARSE_EVENTS__TERM_TYPE_CONFIG1] = "config1",
949 [PARSE_EVENTS__TERM_TYPE_CONFIG2] = "config2",
950 [PARSE_EVENTS__TERM_TYPE_NAME] = "name",
951 [PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD] = "period",
952 [PARSE_EVENTS__TERM_TYPE_SAMPLE_FREQ] = "freq",
953 [PARSE_EVENTS__TERM_TYPE_BRANCH_SAMPLE_TYPE] = "branch_type",
954 [PARSE_EVENTS__TERM_TYPE_TIME] = "time",
955 [PARSE_EVENTS__TERM_TYPE_CALLGRAPH] = "call-graph",
956 [PARSE_EVENTS__TERM_TYPE_STACKSIZE] = "stack-size",
957 [PARSE_EVENTS__TERM_TYPE_NOINHERIT] = "no-inherit",
958 [PARSE_EVENTS__TERM_TYPE_INHERIT] = "inherit",
959 [PARSE_EVENTS__TERM_TYPE_MAX_STACK] = "max-stack",
960 [PARSE_EVENTS__TERM_TYPE_MAX_EVENTS] = "nr",
961 [PARSE_EVENTS__TERM_TYPE_OVERWRITE] = "overwrite",
962 [PARSE_EVENTS__TERM_TYPE_NOOVERWRITE] = "no-overwrite",
963 [PARSE_EVENTS__TERM_TYPE_DRV_CFG] = "driver-config",
964 [PARSE_EVENTS__TERM_TYPE_PERCORE] = "percore",
965 [PARSE_EVENTS__TERM_TYPE_AUX_OUTPUT] = "aux-output",
966 };
967
968 static bool config_term_shrinked;
969
970 static bool
971 config_term_avail(int term_type, struct parse_events_error *err)
972 {
973 if (term_type < 0 || term_type >= __PARSE_EVENTS__TERM_TYPE_NR) {
974 err->str = strdup("Invalid term_type");
975 return false;
976 }
977 if (!config_term_shrinked)
978 return true;
979
980 switch (term_type) {
981 case PARSE_EVENTS__TERM_TYPE_CONFIG:
982 case PARSE_EVENTS__TERM_TYPE_CONFIG1:
983 case PARSE_EVENTS__TERM_TYPE_CONFIG2:
984 case PARSE_EVENTS__TERM_TYPE_NAME:
985 case PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD:
986 case PARSE_EVENTS__TERM_TYPE_PERCORE:
987 return true;
988 default:
989 if (!err)
990 return false;
991
992 /* term_type is validated so indexing is safe */
993 if (asprintf(&err->str, "'%s' is not usable in 'perf stat'",
994 config_term_names[term_type]) < 0)
995 err->str = NULL;
996 return false;
997 }
998 }
999
1000 void parse_events__shrink_config_terms(void)
1001 {
1002 config_term_shrinked = true;
1003 }
1004
1005 static int config_term_common(struct perf_event_attr *attr,
1006 struct parse_events_term *term,
1007 struct parse_events_error *err)
1008 {
1009 #define CHECK_TYPE_VAL(type) \
1010 do { \
1011 if (check_type_val(term, err, PARSE_EVENTS__TERM_TYPE_ ## type)) \
1012 return -EINVAL; \
1013 } while (0)
1014
1015 switch (term->type_term) {
1016 case PARSE_EVENTS__TERM_TYPE_CONFIG:
1017 CHECK_TYPE_VAL(NUM);
1018 attr->config = term->val.num;
1019 break;
1020 case PARSE_EVENTS__TERM_TYPE_CONFIG1:
1021 CHECK_TYPE_VAL(NUM);
1022 attr->config1 = term->val.num;
1023 break;
1024 case PARSE_EVENTS__TERM_TYPE_CONFIG2:
1025 CHECK_TYPE_VAL(NUM);
1026 attr->config2 = term->val.num;
1027 break;
1028 case PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD:
1029 CHECK_TYPE_VAL(NUM);
1030 break;
1031 case PARSE_EVENTS__TERM_TYPE_SAMPLE_FREQ:
1032 CHECK_TYPE_VAL(NUM);
1033 break;
1034 case PARSE_EVENTS__TERM_TYPE_BRANCH_SAMPLE_TYPE:
1035 CHECK_TYPE_VAL(STR);
1036 if (strcmp(term->val.str, "no") &&
1037 parse_branch_str(term->val.str, &attr->branch_sample_type)) {
1038 err->str = strdup("invalid branch sample type");
1039 err->idx = term->err_val;
1040 return -EINVAL;
1041 }
1042 break;
1043 case PARSE_EVENTS__TERM_TYPE_TIME:
1044 CHECK_TYPE_VAL(NUM);
1045 if (term->val.num > 1) {
1046 err->str = strdup("expected 0 or 1");
1047 err->idx = term->err_val;
1048 return -EINVAL;
1049 }
1050 break;
1051 case PARSE_EVENTS__TERM_TYPE_CALLGRAPH:
1052 CHECK_TYPE_VAL(STR);
1053 break;
1054 case PARSE_EVENTS__TERM_TYPE_STACKSIZE:
1055 CHECK_TYPE_VAL(NUM);
1056 break;
1057 case PARSE_EVENTS__TERM_TYPE_INHERIT:
1058 CHECK_TYPE_VAL(NUM);
1059 break;
1060 case PARSE_EVENTS__TERM_TYPE_NOINHERIT:
1061 CHECK_TYPE_VAL(NUM);
1062 break;
1063 case PARSE_EVENTS__TERM_TYPE_OVERWRITE:
1064 CHECK_TYPE_VAL(NUM);
1065 break;
1066 case PARSE_EVENTS__TERM_TYPE_NOOVERWRITE:
1067 CHECK_TYPE_VAL(NUM);
1068 break;
1069 case PARSE_EVENTS__TERM_TYPE_NAME:
1070 CHECK_TYPE_VAL(STR);
1071 break;
1072 case PARSE_EVENTS__TERM_TYPE_MAX_STACK:
1073 CHECK_TYPE_VAL(NUM);
1074 break;
1075 case PARSE_EVENTS__TERM_TYPE_MAX_EVENTS:
1076 CHECK_TYPE_VAL(NUM);
1077 break;
1078 case PARSE_EVENTS__TERM_TYPE_PERCORE:
1079 CHECK_TYPE_VAL(NUM);
1080 if ((unsigned int)term->val.num > 1) {
1081 err->str = strdup("expected 0 or 1");
1082 err->idx = term->err_val;
1083 return -EINVAL;
1084 }
1085 break;
1086 case PARSE_EVENTS__TERM_TYPE_AUX_OUTPUT:
1087 CHECK_TYPE_VAL(NUM);
1088 break;
1089 default:
1090 err->str = strdup("unknown term");
1091 err->idx = term->err_term;
1092 err->help = parse_events_formats_error_string(NULL);
1093 return -EINVAL;
1094 }
1095
1096 /*
1097 * Check term availbility after basic checking so
1098 * PARSE_EVENTS__TERM_TYPE_USER can be found and filtered.
1099 *
1100 * If check availbility at the entry of this function,
1101 * user will see "'<sysfs term>' is not usable in 'perf stat'"
1102 * if an invalid config term is provided for legacy events
1103 * (for example, instructions/badterm/...), which is confusing.
1104 */
1105 if (!config_term_avail(term->type_term, err))
1106 return -EINVAL;
1107 return 0;
1108 #undef CHECK_TYPE_VAL
1109 }
1110
1111 static int config_term_pmu(struct perf_event_attr *attr,
1112 struct parse_events_term *term,
1113 struct parse_events_error *err)
1114 {
1115 if (term->type_term == PARSE_EVENTS__TERM_TYPE_USER ||
1116 term->type_term == PARSE_EVENTS__TERM_TYPE_DRV_CFG)
1117 /*
1118 * Always succeed for sysfs terms, as we dont know
1119 * at this point what type they need to have.
1120 */
1121 return 0;
1122 else
1123 return config_term_common(attr, term, err);
1124 }
1125
1126 static int config_term_tracepoint(struct perf_event_attr *attr,
1127 struct parse_events_term *term,
1128 struct parse_events_error *err)
1129 {
1130 switch (term->type_term) {
1131 case PARSE_EVENTS__TERM_TYPE_CALLGRAPH:
1132 case PARSE_EVENTS__TERM_TYPE_STACKSIZE:
1133 case PARSE_EVENTS__TERM_TYPE_INHERIT:
1134 case PARSE_EVENTS__TERM_TYPE_NOINHERIT:
1135 case PARSE_EVENTS__TERM_TYPE_MAX_STACK:
1136 case PARSE_EVENTS__TERM_TYPE_MAX_EVENTS:
1137 case PARSE_EVENTS__TERM_TYPE_OVERWRITE:
1138 case PARSE_EVENTS__TERM_TYPE_NOOVERWRITE:
1139 case PARSE_EVENTS__TERM_TYPE_AUX_OUTPUT:
1140 return config_term_common(attr, term, err);
1141 default:
1142 if (err) {
1143 err->idx = term->err_term;
1144 err->str = strdup("unknown term");
1145 err->help = strdup("valid terms: call-graph,stack-size\n");
1146 }
1147 return -EINVAL;
1148 }
1149
1150 return 0;
1151 }
1152
1153 static int config_attr(struct perf_event_attr *attr,
1154 struct list_head *head,
1155 struct parse_events_error *err,
1156 config_term_func_t config_term)
1157 {
1158 struct parse_events_term *term;
1159
1160 list_for_each_entry(term, head, list)
1161 if (config_term(attr, term, err))
1162 return -EINVAL;
1163
1164 return 0;
1165 }
1166
1167 static int get_config_terms(struct list_head *head_config,
1168 struct list_head *head_terms __maybe_unused)
1169 {
1170 #define ADD_CONFIG_TERM(__type, __name, __val) \
1171 do { \
1172 struct perf_evsel_config_term *__t; \
1173 \
1174 __t = zalloc(sizeof(*__t)); \
1175 if (!__t) \
1176 return -ENOMEM; \
1177 \
1178 INIT_LIST_HEAD(&__t->list); \
1179 __t->type = PERF_EVSEL__CONFIG_TERM_ ## __type; \
1180 __t->val.__name = __val; \
1181 __t->weak = term->weak; \
1182 list_add_tail(&__t->list, head_terms); \
1183 } while (0)
1184
1185 struct parse_events_term *term;
1186
1187 list_for_each_entry(term, head_config, list) {
1188 switch (term->type_term) {
1189 case PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD:
1190 ADD_CONFIG_TERM(PERIOD, period, term->val.num);
1191 break;
1192 case PARSE_EVENTS__TERM_TYPE_SAMPLE_FREQ:
1193 ADD_CONFIG_TERM(FREQ, freq, term->val.num);
1194 break;
1195 case PARSE_EVENTS__TERM_TYPE_TIME:
1196 ADD_CONFIG_TERM(TIME, time, term->val.num);
1197 break;
1198 case PARSE_EVENTS__TERM_TYPE_CALLGRAPH:
1199 ADD_CONFIG_TERM(CALLGRAPH, callgraph, term->val.str);
1200 break;
1201 case PARSE_EVENTS__TERM_TYPE_BRANCH_SAMPLE_TYPE:
1202 ADD_CONFIG_TERM(BRANCH, branch, term->val.str);
1203 break;
1204 case PARSE_EVENTS__TERM_TYPE_STACKSIZE:
1205 ADD_CONFIG_TERM(STACK_USER, stack_user, term->val.num);
1206 break;
1207 case PARSE_EVENTS__TERM_TYPE_INHERIT:
1208 ADD_CONFIG_TERM(INHERIT, inherit, term->val.num ? 1 : 0);
1209 break;
1210 case PARSE_EVENTS__TERM_TYPE_NOINHERIT:
1211 ADD_CONFIG_TERM(INHERIT, inherit, term->val.num ? 0 : 1);
1212 break;
1213 case PARSE_EVENTS__TERM_TYPE_MAX_STACK:
1214 ADD_CONFIG_TERM(MAX_STACK, max_stack, term->val.num);
1215 break;
1216 case PARSE_EVENTS__TERM_TYPE_MAX_EVENTS:
1217 ADD_CONFIG_TERM(MAX_EVENTS, max_events, term->val.num);
1218 break;
1219 case PARSE_EVENTS__TERM_TYPE_OVERWRITE:
1220 ADD_CONFIG_TERM(OVERWRITE, overwrite, term->val.num ? 1 : 0);
1221 break;
1222 case PARSE_EVENTS__TERM_TYPE_NOOVERWRITE:
1223 ADD_CONFIG_TERM(OVERWRITE, overwrite, term->val.num ? 0 : 1);
1224 break;
1225 case PARSE_EVENTS__TERM_TYPE_DRV_CFG:
1226 ADD_CONFIG_TERM(DRV_CFG, drv_cfg, term->val.str);
1227 break;
1228 case PARSE_EVENTS__TERM_TYPE_PERCORE:
1229 ADD_CONFIG_TERM(PERCORE, percore,
1230 term->val.num ? true : false);
1231 break;
1232 case PARSE_EVENTS__TERM_TYPE_AUX_OUTPUT:
1233 ADD_CONFIG_TERM(AUX_OUTPUT, aux_output, term->val.num ? 1 : 0);
1234 break;
1235 default:
1236 break;
1237 }
1238 }
1239 #undef ADD_EVSEL_CONFIG
1240 return 0;
1241 }
1242
1243 int parse_events_add_tracepoint(struct list_head *list, int *idx,
1244 const char *sys, const char *event,
1245 struct parse_events_error *err,
1246 struct list_head *head_config)
1247 {
1248 if (head_config) {
1249 struct perf_event_attr attr;
1250
1251 if (config_attr(&attr, head_config, err,
1252 config_term_tracepoint))
1253 return -EINVAL;
1254 }
1255
1256 if (strpbrk(sys, "*?"))
1257 return add_tracepoint_multi_sys(list, idx, sys, event,
1258 err, head_config);
1259 else
1260 return add_tracepoint_event(list, idx, sys, event,
1261 err, head_config);
1262 }
1263
1264 int parse_events_add_numeric(struct parse_events_state *parse_state,
1265 struct list_head *list,
1266 u32 type, u64 config,
1267 struct list_head *head_config)
1268 {
1269 struct perf_event_attr attr;
1270 LIST_HEAD(config_terms);
1271
1272 memset(&attr, 0, sizeof(attr));
1273 attr.type = type;
1274 attr.config = config;
1275
1276 if (head_config) {
1277 if (config_attr(&attr, head_config, parse_state->error,
1278 config_term_common))
1279 return -EINVAL;
1280
1281 if (get_config_terms(head_config, &config_terms))
1282 return -ENOMEM;
1283 }
1284
1285 return add_event(list, &parse_state->idx, &attr,
1286 get_config_name(head_config), &config_terms);
1287 }
1288
1289 int parse_events_add_tool(struct parse_events_state *parse_state,
1290 struct list_head *list,
1291 enum perf_tool_event tool_event)
1292 {
1293 return add_event_tool(list, &parse_state->idx, tool_event);
1294 }
1295
1296 static bool config_term_percore(struct list_head *config_terms)
1297 {
1298 struct perf_evsel_config_term *term;
1299
1300 list_for_each_entry(term, config_terms, list) {
1301 if (term->type == PERF_EVSEL__CONFIG_TERM_PERCORE)
1302 return term->val.percore;
1303 }
1304
1305 return false;
1306 }
1307
1308 int parse_events_add_pmu(struct parse_events_state *parse_state,
1309 struct list_head *list, char *name,
1310 struct list_head *head_config,
1311 bool auto_merge_stats,
1312 bool use_alias)
1313 {
1314 struct perf_event_attr attr;
1315 struct perf_pmu_info info;
1316 struct perf_pmu *pmu;
1317 struct evsel *evsel;
1318 struct parse_events_error *err = parse_state->error;
1319 bool use_uncore_alias;
1320 LIST_HEAD(config_terms);
1321
1322 pmu = perf_pmu__find(name);
1323 if (!pmu) {
1324 if (asprintf(&err->str,
1325 "Cannot find PMU `%s'. Missing kernel support?",
1326 name) < 0)
1327 err->str = NULL;
1328 return -EINVAL;
1329 }
1330
1331 if (pmu->default_config) {
1332 memcpy(&attr, pmu->default_config,
1333 sizeof(struct perf_event_attr));
1334 } else {
1335 memset(&attr, 0, sizeof(attr));
1336 }
1337
1338 use_uncore_alias = (pmu->is_uncore && use_alias);
1339
1340 if (!head_config) {
1341 attr.type = pmu->type;
1342 evsel = __add_event(list, &parse_state->idx, &attr, NULL, pmu, NULL,
1343 auto_merge_stats, NULL);
1344 if (evsel) {
1345 evsel->pmu_name = name;
1346 evsel->use_uncore_alias = use_uncore_alias;
1347 return 0;
1348 } else {
1349 return -ENOMEM;
1350 }
1351 }
1352
1353 if (perf_pmu__check_alias(pmu, head_config, &info))
1354 return -EINVAL;
1355
1356 /*
1357 * Configure hardcoded terms first, no need to check
1358 * return value when called with fail == 0 ;)
1359 */
1360 if (config_attr(&attr, head_config, parse_state->error, config_term_pmu))
1361 return -EINVAL;
1362
1363 if (get_config_terms(head_config, &config_terms))
1364 return -ENOMEM;
1365
1366 if (perf_pmu__config(pmu, &attr, head_config, parse_state->error))
1367 return -EINVAL;
1368
1369 evsel = __add_event(list, &parse_state->idx, &attr,
1370 get_config_name(head_config), pmu,
1371 &config_terms, auto_merge_stats, NULL);
1372 if (evsel) {
1373 evsel->unit = info.unit;
1374 evsel->scale = info.scale;
1375 evsel->per_pkg = info.per_pkg;
1376 evsel->snapshot = info.snapshot;
1377 evsel->metric_expr = info.metric_expr;
1378 evsel->metric_name = info.metric_name;
1379 evsel->pmu_name = name;
1380 evsel->use_uncore_alias = use_uncore_alias;
1381 evsel->percore = config_term_percore(&evsel->config_terms);
1382 }
1383
1384 return evsel ? 0 : -ENOMEM;
1385 }
1386
1387 int parse_events_multi_pmu_add(struct parse_events_state *parse_state,
1388 char *str, struct list_head **listp)
1389 {
1390 struct list_head *head;
1391 struct parse_events_term *term;
1392 struct list_head *list;
1393 struct perf_pmu *pmu = NULL;
1394 int ok = 0;
1395
1396 *listp = NULL;
1397 /* Add it for all PMUs that support the alias */
1398 list = malloc(sizeof(struct list_head));
1399 if (!list)
1400 return -1;
1401 INIT_LIST_HEAD(list);
1402 while ((pmu = perf_pmu__scan(pmu)) != NULL) {
1403 struct perf_pmu_alias *alias;
1404
1405 list_for_each_entry(alias, &pmu->aliases, list) {
1406 if (!strcasecmp(alias->name, str)) {
1407 head = malloc(sizeof(struct list_head));
1408 if (!head)
1409 return -1;
1410 INIT_LIST_HEAD(head);
1411 if (parse_events_term__num(&term, PARSE_EVENTS__TERM_TYPE_USER,
1412 str, 1, false, &str, NULL) < 0)
1413 return -1;
1414 list_add_tail(&term->list, head);
1415
1416 if (!parse_events_add_pmu(parse_state, list,
1417 pmu->name, head,
1418 true, true)) {
1419 pr_debug("%s -> %s/%s/\n", str,
1420 pmu->name, alias->str);
1421 ok++;
1422 }
1423
1424 parse_events_terms__delete(head);
1425 }
1426 }
1427 }
1428 if (!ok)
1429 return -1;
1430 *listp = list;
1431 return 0;
1432 }
1433
1434 int parse_events__modifier_group(struct list_head *list,
1435 char *event_mod)
1436 {
1437 return parse_events__modifier_event(list, event_mod, true);
1438 }
1439
1440 /*
1441 * Check if the two uncore PMUs are from the same uncore block
1442 * The format of the uncore PMU name is uncore_#blockname_#pmuidx
1443 */
1444 static bool is_same_uncore_block(const char *pmu_name_a, const char *pmu_name_b)
1445 {
1446 char *end_a, *end_b;
1447
1448 end_a = strrchr(pmu_name_a, '_');
1449 end_b = strrchr(pmu_name_b, '_');
1450
1451 if (!end_a || !end_b)
1452 return false;
1453
1454 if ((end_a - pmu_name_a) != (end_b - pmu_name_b))
1455 return false;
1456
1457 return (strncmp(pmu_name_a, pmu_name_b, end_a - pmu_name_a) == 0);
1458 }
1459
1460 static int
1461 parse_events__set_leader_for_uncore_aliase(char *name, struct list_head *list,
1462 struct parse_events_state *parse_state)
1463 {
1464 struct evsel *evsel, *leader;
1465 uintptr_t *leaders;
1466 bool is_leader = true;
1467 int i, nr_pmu = 0, total_members, ret = 0;
1468
1469 leader = list_first_entry(list, struct evsel, core.node);
1470 evsel = list_last_entry(list, struct evsel, core.node);
1471 total_members = evsel->idx - leader->idx + 1;
1472
1473 leaders = calloc(total_members, sizeof(uintptr_t));
1474 if (WARN_ON(!leaders))
1475 return 0;
1476
1477 /*
1478 * Going through the whole group and doing sanity check.
1479 * All members must use alias, and be from the same uncore block.
1480 * Also, storing the leader events in an array.
1481 */
1482 __evlist__for_each_entry(list, evsel) {
1483
1484 /* Only split the uncore group which members use alias */
1485 if (!evsel->use_uncore_alias)
1486 goto out;
1487
1488 /* The events must be from the same uncore block */
1489 if (!is_same_uncore_block(leader->pmu_name, evsel->pmu_name))
1490 goto out;
1491
1492 if (!is_leader)
1493 continue;
1494 /*
1495 * If the event's PMU name starts to repeat, it must be a new
1496 * event. That can be used to distinguish the leader from
1497 * other members, even they have the same event name.
1498 */
1499 if ((leader != evsel) && (leader->pmu_name == evsel->pmu_name)) {
1500 is_leader = false;
1501 continue;
1502 }
1503 /* The name is always alias name */
1504 WARN_ON(strcmp(leader->name, evsel->name));
1505
1506 /* Store the leader event for each PMU */
1507 leaders[nr_pmu++] = (uintptr_t) evsel;
1508 }
1509
1510 /* only one event alias */
1511 if (nr_pmu == total_members) {
1512 parse_state->nr_groups--;
1513 goto handled;
1514 }
1515
1516 /*
1517 * An uncore event alias is a joint name which means the same event
1518 * runs on all PMUs of a block.
1519 * Perf doesn't support mixed events from different PMUs in the same
1520 * group. The big group has to be split into multiple small groups
1521 * which only include the events from the same PMU.
1522 *
1523 * Here the uncore event aliases must be from the same uncore block.
1524 * The number of PMUs must be same for each alias. The number of new
1525 * small groups equals to the number of PMUs.
1526 * Setting the leader event for corresponding members in each group.
1527 */
1528 i = 0;
1529 __evlist__for_each_entry(list, evsel) {
1530 if (i >= nr_pmu)
1531 i = 0;
1532 evsel->leader = (struct evsel *) leaders[i++];
1533 }
1534
1535 /* The number of members and group name are same for each group */
1536 for (i = 0; i < nr_pmu; i++) {
1537 evsel = (struct evsel *) leaders[i];
1538 evsel->core.nr_members = total_members / nr_pmu;
1539 evsel->group_name = name ? strdup(name) : NULL;
1540 }
1541
1542 /* Take the new small groups into account */
1543 parse_state->nr_groups += nr_pmu - 1;
1544
1545 handled:
1546 ret = 1;
1547 out:
1548 free(leaders);
1549 return ret;
1550 }
1551
1552 void parse_events__set_leader(char *name, struct list_head *list,
1553 struct parse_events_state *parse_state)
1554 {
1555 struct evsel *leader;
1556
1557 if (list_empty(list)) {
1558 WARN_ONCE(true, "WARNING: failed to set leader: empty list");
1559 return;
1560 }
1561
1562 if (parse_events__set_leader_for_uncore_aliase(name, list, parse_state))
1563 return;
1564
1565 __perf_evlist__set_leader(list);
1566 leader = list_entry(list->next, struct evsel, core.node);
1567 leader->group_name = name ? strdup(name) : NULL;
1568 }
1569
1570 /* list_event is assumed to point to malloc'ed memory */
1571 void parse_events_update_lists(struct list_head *list_event,
1572 struct list_head *list_all)
1573 {
1574 /*
1575 * Called for single event definition. Update the
1576 * 'all event' list, and reinit the 'single event'
1577 * list, for next event definition.
1578 */
1579 list_splice_tail(list_event, list_all);
1580 free(list_event);
1581 }
1582
1583 struct event_modifier {
1584 int eu;
1585 int ek;
1586 int eh;
1587 int eH;
1588 int eG;
1589 int eI;
1590 int precise;
1591 int precise_max;
1592 int exclude_GH;
1593 int sample_read;
1594 int pinned;
1595 int weak;
1596 };
1597
1598 static int get_event_modifier(struct event_modifier *mod, char *str,
1599 struct evsel *evsel)
1600 {
1601 int eu = evsel ? evsel->core.attr.exclude_user : 0;
1602 int ek = evsel ? evsel->core.attr.exclude_kernel : 0;
1603 int eh = evsel ? evsel->core.attr.exclude_hv : 0;
1604 int eH = evsel ? evsel->core.attr.exclude_host : 0;
1605 int eG = evsel ? evsel->core.attr.exclude_guest : 0;
1606 int eI = evsel ? evsel->core.attr.exclude_idle : 0;
1607 int precise = evsel ? evsel->core.attr.precise_ip : 0;
1608 int precise_max = 0;
1609 int sample_read = 0;
1610 int pinned = evsel ? evsel->core.attr.pinned : 0;
1611
1612 int exclude = eu | ek | eh;
1613 int exclude_GH = evsel ? evsel->exclude_GH : 0;
1614 int weak = 0;
1615
1616 memset(mod, 0, sizeof(*mod));
1617
1618 while (*str) {
1619 if (*str == 'u') {
1620 if (!exclude)
1621 exclude = eu = ek = eh = 1;
1622 eu = 0;
1623 } else if (*str == 'k') {
1624 if (!exclude)
1625 exclude = eu = ek = eh = 1;
1626 ek = 0;
1627 } else if (*str == 'h') {
1628 if (!exclude)
1629 exclude = eu = ek = eh = 1;
1630 eh = 0;
1631 } else if (*str == 'G') {
1632 if (!exclude_GH)
1633 exclude_GH = eG = eH = 1;
1634 eG = 0;
1635 } else if (*str == 'H') {
1636 if (!exclude_GH)
1637 exclude_GH = eG = eH = 1;
1638 eH = 0;
1639 } else if (*str == 'I') {
1640 eI = 1;
1641 } else if (*str == 'p') {
1642 precise++;
1643 /* use of precise requires exclude_guest */
1644 if (!exclude_GH)
1645 eG = 1;
1646 } else if (*str == 'P') {
1647 precise_max = 1;
1648 } else if (*str == 'S') {
1649 sample_read = 1;
1650 } else if (*str == 'D') {
1651 pinned = 1;
1652 } else if (*str == 'W') {
1653 weak = 1;
1654 } else
1655 break;
1656
1657 ++str;
1658 }
1659
1660 /*
1661 * precise ip:
1662 *
1663 * 0 - SAMPLE_IP can have arbitrary skid
1664 * 1 - SAMPLE_IP must have constant skid
1665 * 2 - SAMPLE_IP requested to have 0 skid
1666 * 3 - SAMPLE_IP must have 0 skid
1667 *
1668 * See also PERF_RECORD_MISC_EXACT_IP
1669 */
1670 if (precise > 3)
1671 return -EINVAL;
1672
1673 mod->eu = eu;
1674 mod->ek = ek;
1675 mod->eh = eh;
1676 mod->eH = eH;
1677 mod->eG = eG;
1678 mod->eI = eI;
1679 mod->precise = precise;
1680 mod->precise_max = precise_max;
1681 mod->exclude_GH = exclude_GH;
1682 mod->sample_read = sample_read;
1683 mod->pinned = pinned;
1684 mod->weak = weak;
1685
1686 return 0;
1687 }
1688
1689 /*
1690 * Basic modifier sanity check to validate it contains only one
1691 * instance of any modifier (apart from 'p') present.
1692 */
1693 static int check_modifier(char *str)
1694 {
1695 char *p = str;
1696
1697 /* The sizeof includes 0 byte as well. */
1698 if (strlen(str) > (sizeof("ukhGHpppPSDIW") - 1))
1699 return -1;
1700
1701 while (*p) {
1702 if (*p != 'p' && strchr(p + 1, *p))
1703 return -1;
1704 p++;
1705 }
1706
1707 return 0;
1708 }
1709
1710 int parse_events__modifier_event(struct list_head *list, char *str, bool add)
1711 {
1712 struct evsel *evsel;
1713 struct event_modifier mod;
1714
1715 if (str == NULL)
1716 return 0;
1717
1718 if (check_modifier(str))
1719 return -EINVAL;
1720
1721 if (!add && get_event_modifier(&mod, str, NULL))
1722 return -EINVAL;
1723
1724 __evlist__for_each_entry(list, evsel) {
1725 if (add && get_event_modifier(&mod, str, evsel))
1726 return -EINVAL;
1727
1728 evsel->core.attr.exclude_user = mod.eu;
1729 evsel->core.attr.exclude_kernel = mod.ek;
1730 evsel->core.attr.exclude_hv = mod.eh;
1731 evsel->core.attr.precise_ip = mod.precise;
1732 evsel->core.attr.exclude_host = mod.eH;
1733 evsel->core.attr.exclude_guest = mod.eG;
1734 evsel->core.attr.exclude_idle = mod.eI;
1735 evsel->exclude_GH = mod.exclude_GH;
1736 evsel->sample_read = mod.sample_read;
1737 evsel->precise_max = mod.precise_max;
1738 evsel->weak_group = mod.weak;
1739
1740 if (perf_evsel__is_group_leader(evsel))
1741 evsel->core.attr.pinned = mod.pinned;
1742 }
1743
1744 return 0;
1745 }
1746
1747 int parse_events_name(struct list_head *list, char *name)
1748 {
1749 struct evsel *evsel;
1750
1751 __evlist__for_each_entry(list, evsel) {
1752 if (!evsel->name)
1753 evsel->name = strdup(name);
1754 }
1755
1756 return 0;
1757 }
1758
1759 static int
1760 comp_pmu(const void *p1, const void *p2)
1761 {
1762 struct perf_pmu_event_symbol *pmu1 = (struct perf_pmu_event_symbol *) p1;
1763 struct perf_pmu_event_symbol *pmu2 = (struct perf_pmu_event_symbol *) p2;
1764
1765 return strcasecmp(pmu1->symbol, pmu2->symbol);
1766 }
1767
1768 static void perf_pmu__parse_cleanup(void)
1769 {
1770 if (perf_pmu_events_list_num > 0) {
1771 struct perf_pmu_event_symbol *p;
1772 int i;
1773
1774 for (i = 0; i < perf_pmu_events_list_num; i++) {
1775 p = perf_pmu_events_list + i;
1776 zfree(&p->symbol);
1777 }
1778 zfree(&perf_pmu_events_list);
1779 perf_pmu_events_list_num = 0;
1780 }
1781 }
1782
1783 #define SET_SYMBOL(str, stype) \
1784 do { \
1785 p->symbol = str; \
1786 if (!p->symbol) \
1787 goto err; \
1788 p->type = stype; \
1789 } while (0)
1790
1791 /*
1792 * Read the pmu events list from sysfs
1793 * Save it into perf_pmu_events_list
1794 */
1795 static void perf_pmu__parse_init(void)
1796 {
1797
1798 struct perf_pmu *pmu = NULL;
1799 struct perf_pmu_alias *alias;
1800 int len = 0;
1801
1802 pmu = NULL;
1803 while ((pmu = perf_pmu__scan(pmu)) != NULL) {
1804 list_for_each_entry(alias, &pmu->aliases, list) {
1805 if (strchr(alias->name, '-'))
1806 len++;
1807 len++;
1808 }
1809 }
1810
1811 if (len == 0) {
1812 perf_pmu_events_list_num = -1;
1813 return;
1814 }
1815 perf_pmu_events_list = malloc(sizeof(struct perf_pmu_event_symbol) * len);
1816 if (!perf_pmu_events_list)
1817 return;
1818 perf_pmu_events_list_num = len;
1819
1820 len = 0;
1821 pmu = NULL;
1822 while ((pmu = perf_pmu__scan(pmu)) != NULL) {
1823 list_for_each_entry(alias, &pmu->aliases, list) {
1824 struct perf_pmu_event_symbol *p = perf_pmu_events_list + len;
1825 char *tmp = strchr(alias->name, '-');
1826
1827 if (tmp != NULL) {
1828 SET_SYMBOL(strndup(alias->name, tmp - alias->name),
1829 PMU_EVENT_SYMBOL_PREFIX);
1830 p++;
1831 SET_SYMBOL(strdup(++tmp), PMU_EVENT_SYMBOL_SUFFIX);
1832 len += 2;
1833 } else {
1834 SET_SYMBOL(strdup(alias->name), PMU_EVENT_SYMBOL);
1835 len++;
1836 }
1837 }
1838 }
1839 qsort(perf_pmu_events_list, len,
1840 sizeof(struct perf_pmu_event_symbol), comp_pmu);
1841
1842 return;
1843 err:
1844 perf_pmu__parse_cleanup();
1845 }
1846
1847 enum perf_pmu_event_symbol_type
1848 perf_pmu__parse_check(const char *name)
1849 {
1850 struct perf_pmu_event_symbol p, *r;
1851
1852 /* scan kernel pmu events from sysfs if needed */
1853 if (perf_pmu_events_list_num == 0)
1854 perf_pmu__parse_init();
1855 /*
1856 * name "cpu" could be prefix of cpu-cycles or cpu// events.
1857 * cpu-cycles has been handled by hardcode.
1858 * So it must be cpu// events, not kernel pmu event.
1859 */
1860 if ((perf_pmu_events_list_num <= 0) || !strcmp(name, "cpu"))
1861 return PMU_EVENT_SYMBOL_ERR;
1862
1863 p.symbol = strdup(name);
1864 r = bsearch(&p, perf_pmu_events_list,
1865 (size_t) perf_pmu_events_list_num,
1866 sizeof(struct perf_pmu_event_symbol), comp_pmu);
1867 zfree(&p.symbol);
1868 return r ? r->type : PMU_EVENT_SYMBOL_ERR;
1869 }
1870
1871 static int parse_events__scanner(const char *str, void *parse_state, int start_token)
1872 {
1873 YY_BUFFER_STATE buffer;
1874 void *scanner;
1875 int ret;
1876
1877 ret = parse_events_lex_init_extra(start_token, &scanner);
1878 if (ret)
1879 return ret;
1880
1881 buffer = parse_events__scan_string(str, scanner);
1882
1883 #ifdef PARSER_DEBUG
1884 parse_events_debug = 1;
1885 #endif
1886 ret = parse_events_parse(parse_state, scanner);
1887
1888 parse_events__flush_buffer(buffer, scanner);
1889 parse_events__delete_buffer(buffer, scanner);
1890 parse_events_lex_destroy(scanner);
1891 return ret;
1892 }
1893
1894 /*
1895 * parse event config string, return a list of event terms.
1896 */
1897 int parse_events_terms(struct list_head *terms, const char *str)
1898 {
1899 struct parse_events_state parse_state = {
1900 .terms = NULL,
1901 };
1902 int ret;
1903
1904 ret = parse_events__scanner(str, &parse_state, PE_START_TERMS);
1905 if (!ret) {
1906 list_splice(parse_state.terms, terms);
1907 zfree(&parse_state.terms);
1908 return 0;
1909 }
1910
1911 parse_events_terms__delete(parse_state.terms);
1912 return ret;
1913 }
1914
1915 int parse_events(struct evlist *evlist, const char *str,
1916 struct parse_events_error *err)
1917 {
1918 struct parse_events_state parse_state = {
1919 .list = LIST_HEAD_INIT(parse_state.list),
1920 .idx = evlist->core.nr_entries,
1921 .error = err,
1922 .evlist = evlist,
1923 };
1924 int ret;
1925
1926 ret = parse_events__scanner(str, &parse_state, PE_START_EVENTS);
1927 perf_pmu__parse_cleanup();
1928 if (!ret) {
1929 struct evsel *last;
1930
1931 if (list_empty(&parse_state.list)) {
1932 WARN_ONCE(true, "WARNING: event parser found nothing\n");
1933 return -1;
1934 }
1935
1936 perf_evlist__splice_list_tail(evlist, &parse_state.list);
1937 evlist->nr_groups += parse_state.nr_groups;
1938 last = perf_evlist__last(evlist);
1939 last->cmdline_group_boundary = true;
1940
1941 return 0;
1942 }
1943
1944 /*
1945 * There are 2 users - builtin-record and builtin-test objects.
1946 * Both call evlist__delete in case of error, so we dont
1947 * need to bother.
1948 */
1949 return ret;
1950 }
1951
1952 #define MAX_WIDTH 1000
1953 static int get_term_width(void)
1954 {
1955 struct winsize ws;
1956
1957 get_term_dimensions(&ws);
1958 return ws.ws_col > MAX_WIDTH ? MAX_WIDTH : ws.ws_col;
1959 }
1960
1961 void parse_events_print_error(struct parse_events_error *err,
1962 const char *event)
1963 {
1964 const char *str = "invalid or unsupported event: ";
1965 char _buf[MAX_WIDTH];
1966 char *buf = (char *) event;
1967 int idx = 0;
1968
1969 if (err->str) {
1970 /* -2 for extra '' in the final fprintf */
1971 int width = get_term_width() - 2;
1972 int len_event = strlen(event);
1973 int len_str, max_len, cut = 0;
1974
1975 /*
1976 * Maximum error index indent, we will cut
1977 * the event string if it's bigger.
1978 */
1979 int max_err_idx = 13;
1980
1981 /*
1982 * Let's be specific with the message when
1983 * we have the precise error.
1984 */
1985 str = "event syntax error: ";
1986 len_str = strlen(str);
1987 max_len = width - len_str;
1988
1989 buf = _buf;
1990
1991 /* We're cutting from the beginning. */
1992 if (err->idx > max_err_idx)
1993 cut = err->idx - max_err_idx;
1994
1995 strncpy(buf, event + cut, max_len);
1996
1997 /* Mark cut parts with '..' on both sides. */
1998 if (cut)
1999 buf[0] = buf[1] = '.';
2000
2001 if ((len_event - cut) > max_len) {
2002 buf[max_len - 1] = buf[max_len - 2] = '.';
2003 buf[max_len] = 0;
2004 }
2005
2006 idx = len_str + err->idx - cut;
2007 }
2008
2009 fprintf(stderr, "%s'%s'\n", str, buf);
2010 if (idx) {
2011 fprintf(stderr, "%*s\\___ %s\n", idx + 1, "", err->str);
2012 if (err->help)
2013 fprintf(stderr, "\n%s\n", err->help);
2014 zfree(&err->str);
2015 zfree(&err->help);
2016 }
2017 }
2018
2019 #undef MAX_WIDTH
2020
2021 int parse_events_option(const struct option *opt, const char *str,
2022 int unset __maybe_unused)
2023 {
2024 struct evlist *evlist = *(struct evlist **)opt->value;
2025 struct parse_events_error err = { .idx = 0, };
2026 int ret = parse_events(evlist, str, &err);
2027
2028 if (ret) {
2029 parse_events_print_error(&err, str);
2030 fprintf(stderr, "Run 'perf list' for a list of valid events\n");
2031 }
2032
2033 return ret;
2034 }
2035
2036 static int
2037 foreach_evsel_in_last_glob(struct evlist *evlist,
2038 int (*func)(struct evsel *evsel,
2039 const void *arg),
2040 const void *arg)
2041 {
2042 struct evsel *last = NULL;
2043 int err;
2044
2045 /*
2046 * Don't return when list_empty, give func a chance to report
2047 * error when it found last == NULL.
2048 *
2049 * So no need to WARN here, let *func do this.
2050 */
2051 if (evlist->core.nr_entries > 0)
2052 last = perf_evlist__last(evlist);
2053
2054 do {
2055 err = (*func)(last, arg);
2056 if (err)
2057 return -1;
2058 if (!last)
2059 return 0;
2060
2061 if (last->core.node.prev == &evlist->core.entries)
2062 return 0;
2063 last = list_entry(last->core.node.prev, struct evsel, core.node);
2064 } while (!last->cmdline_group_boundary);
2065
2066 return 0;
2067 }
2068
2069 static int set_filter(struct evsel *evsel, const void *arg)
2070 {
2071 const char *str = arg;
2072 bool found = false;
2073 int nr_addr_filters = 0;
2074 struct perf_pmu *pmu = NULL;
2075
2076 if (evsel == NULL) {
2077 fprintf(stderr,
2078 "--filter option should follow a -e tracepoint or HW tracer option\n");
2079 return -1;
2080 }
2081
2082 if (evsel->core.attr.type == PERF_TYPE_TRACEPOINT) {
2083 if (perf_evsel__append_tp_filter(evsel, str) < 0) {
2084 fprintf(stderr,
2085 "not enough memory to hold filter string\n");
2086 return -1;
2087 }
2088
2089 return 0;
2090 }
2091
2092 while ((pmu = perf_pmu__scan(pmu)) != NULL)
2093 if (pmu->type == evsel->core.attr.type) {
2094 found = true;
2095 break;
2096 }
2097
2098 if (found)
2099 perf_pmu__scan_file(pmu, "nr_addr_filters",
2100 "%d", &nr_addr_filters);
2101
2102 if (!nr_addr_filters) {
2103 fprintf(stderr,
2104 "This CPU does not support address filtering\n");
2105 return -1;
2106 }
2107
2108 if (perf_evsel__append_addr_filter(evsel, str) < 0) {
2109 fprintf(stderr,
2110 "not enough memory to hold filter string\n");
2111 return -1;
2112 }
2113
2114 return 0;
2115 }
2116
2117 int parse_filter(const struct option *opt, const char *str,
2118 int unset __maybe_unused)
2119 {
2120 struct evlist *evlist = *(struct evlist **)opt->value;
2121
2122 return foreach_evsel_in_last_glob(evlist, set_filter,
2123 (const void *)str);
2124 }
2125
2126 static int add_exclude_perf_filter(struct evsel *evsel,
2127 const void *arg __maybe_unused)
2128 {
2129 char new_filter[64];
2130
2131 if (evsel == NULL || evsel->core.attr.type != PERF_TYPE_TRACEPOINT) {
2132 fprintf(stderr,
2133 "--exclude-perf option should follow a -e tracepoint option\n");
2134 return -1;
2135 }
2136
2137 snprintf(new_filter, sizeof(new_filter), "common_pid != %d", getpid());
2138
2139 if (perf_evsel__append_tp_filter(evsel, new_filter) < 0) {
2140 fprintf(stderr,
2141 "not enough memory to hold filter string\n");
2142 return -1;
2143 }
2144
2145 return 0;
2146 }
2147
2148 int exclude_perf(const struct option *opt,
2149 const char *arg __maybe_unused,
2150 int unset __maybe_unused)
2151 {
2152 struct evlist *evlist = *(struct evlist **)opt->value;
2153
2154 return foreach_evsel_in_last_glob(evlist, add_exclude_perf_filter,
2155 NULL);
2156 }
2157
2158 static const char * const event_type_descriptors[] = {
2159 "Hardware event",
2160 "Software event",
2161 "Tracepoint event",
2162 "Hardware cache event",
2163 "Raw hardware event descriptor",
2164 "Hardware breakpoint",
2165 };
2166
2167 static int cmp_string(const void *a, const void *b)
2168 {
2169 const char * const *as = a;
2170 const char * const *bs = b;
2171
2172 return strcmp(*as, *bs);
2173 }
2174
2175 /*
2176 * Print the events from <debugfs_mount_point>/tracing/events
2177 */
2178
2179 void print_tracepoint_events(const char *subsys_glob, const char *event_glob,
2180 bool name_only)
2181 {
2182 DIR *sys_dir, *evt_dir;
2183 struct dirent *sys_dirent, *evt_dirent;
2184 char evt_path[MAXPATHLEN];
2185 char *dir_path;
2186 char **evt_list = NULL;
2187 unsigned int evt_i = 0, evt_num = 0;
2188 bool evt_num_known = false;
2189
2190 restart:
2191 sys_dir = tracing_events__opendir();
2192 if (!sys_dir)
2193 return;
2194
2195 if (evt_num_known) {
2196 evt_list = zalloc(sizeof(char *) * evt_num);
2197 if (!evt_list)
2198 goto out_close_sys_dir;
2199 }
2200
2201 for_each_subsystem(sys_dir, sys_dirent) {
2202 if (subsys_glob != NULL &&
2203 !strglobmatch(sys_dirent->d_name, subsys_glob))
2204 continue;
2205
2206 dir_path = get_events_file(sys_dirent->d_name);
2207 if (!dir_path)
2208 continue;
2209 evt_dir = opendir(dir_path);
2210 if (!evt_dir)
2211 goto next;
2212
2213 for_each_event(dir_path, evt_dir, evt_dirent) {
2214 if (event_glob != NULL &&
2215 !strglobmatch(evt_dirent->d_name, event_glob))
2216 continue;
2217
2218 if (!evt_num_known) {
2219 evt_num++;
2220 continue;
2221 }
2222
2223 snprintf(evt_path, MAXPATHLEN, "%s:%s",
2224 sys_dirent->d_name, evt_dirent->d_name);
2225
2226 evt_list[evt_i] = strdup(evt_path);
2227 if (evt_list[evt_i] == NULL) {
2228 put_events_file(dir_path);
2229 goto out_close_evt_dir;
2230 }
2231 evt_i++;
2232 }
2233 closedir(evt_dir);
2234 next:
2235 put_events_file(dir_path);
2236 }
2237 closedir(sys_dir);
2238
2239 if (!evt_num_known) {
2240 evt_num_known = true;
2241 goto restart;
2242 }
2243 qsort(evt_list, evt_num, sizeof(char *), cmp_string);
2244 evt_i = 0;
2245 while (evt_i < evt_num) {
2246 if (name_only) {
2247 printf("%s ", evt_list[evt_i++]);
2248 continue;
2249 }
2250 printf(" %-50s [%s]\n", evt_list[evt_i++],
2251 event_type_descriptors[PERF_TYPE_TRACEPOINT]);
2252 }
2253 if (evt_num && pager_in_use())
2254 printf("\n");
2255
2256 out_free:
2257 evt_num = evt_i;
2258 for (evt_i = 0; evt_i < evt_num; evt_i++)
2259 zfree(&evt_list[evt_i]);
2260 zfree(&evt_list);
2261 return;
2262
2263 out_close_evt_dir:
2264 closedir(evt_dir);
2265 out_close_sys_dir:
2266 closedir(sys_dir);
2267
2268 printf("FATAL: not enough memory to print %s\n",
2269 event_type_descriptors[PERF_TYPE_TRACEPOINT]);
2270 if (evt_list)
2271 goto out_free;
2272 }
2273
2274 /*
2275 * Check whether event is in <debugfs_mount_point>/tracing/events
2276 */
2277
2278 int is_valid_tracepoint(const char *event_string)
2279 {
2280 DIR *sys_dir, *evt_dir;
2281 struct dirent *sys_dirent, *evt_dirent;
2282 char evt_path[MAXPATHLEN];
2283 char *dir_path;
2284
2285 sys_dir = tracing_events__opendir();
2286 if (!sys_dir)
2287 return 0;
2288
2289 for_each_subsystem(sys_dir, sys_dirent) {
2290 dir_path = get_events_file(sys_dirent->d_name);
2291 if (!dir_path)
2292 continue;
2293 evt_dir = opendir(dir_path);
2294 if (!evt_dir)
2295 goto next;
2296
2297 for_each_event(dir_path, evt_dir, evt_dirent) {
2298 snprintf(evt_path, MAXPATHLEN, "%s:%s",
2299 sys_dirent->d_name, evt_dirent->d_name);
2300 if (!strcmp(evt_path, event_string)) {
2301 closedir(evt_dir);
2302 closedir(sys_dir);
2303 return 1;
2304 }
2305 }
2306 closedir(evt_dir);
2307 next:
2308 put_events_file(dir_path);
2309 }
2310 closedir(sys_dir);
2311 return 0;
2312 }
2313
2314 static bool is_event_supported(u8 type, unsigned config)
2315 {
2316 bool ret = true;
2317 int open_return;
2318 struct evsel *evsel;
2319 struct perf_event_attr attr = {
2320 .type = type,
2321 .config = config,
2322 .disabled = 1,
2323 };
2324 struct perf_thread_map *tmap = thread_map__new_by_tid(0);
2325
2326 if (tmap == NULL)
2327 return false;
2328
2329 evsel = evsel__new(&attr);
2330 if (evsel) {
2331 open_return = evsel__open(evsel, NULL, tmap);
2332 ret = open_return >= 0;
2333
2334 if (open_return == -EACCES) {
2335 /*
2336 * This happens if the paranoid value
2337 * /proc/sys/kernel/perf_event_paranoid is set to 2
2338 * Re-run with exclude_kernel set; we don't do that
2339 * by default as some ARM machines do not support it.
2340 *
2341 */
2342 evsel->core.attr.exclude_kernel = 1;
2343 ret = evsel__open(evsel, NULL, tmap) >= 0;
2344 }
2345 evsel__delete(evsel);
2346 }
2347
2348 perf_thread_map__put(tmap);
2349 return ret;
2350 }
2351
2352 void print_sdt_events(const char *subsys_glob, const char *event_glob,
2353 bool name_only)
2354 {
2355 struct probe_cache *pcache;
2356 struct probe_cache_entry *ent;
2357 struct strlist *bidlist, *sdtlist;
2358 struct strlist_config cfg = {.dont_dupstr = true};
2359 struct str_node *nd, *nd2;
2360 char *buf, *path, *ptr = NULL;
2361 bool show_detail = false;
2362 int ret;
2363
2364 sdtlist = strlist__new(NULL, &cfg);
2365 if (!sdtlist) {
2366 pr_debug("Failed to allocate new strlist for SDT\n");
2367 return;
2368 }
2369 bidlist = build_id_cache__list_all(true);
2370 if (!bidlist) {
2371 pr_debug("Failed to get buildids: %d\n", errno);
2372 return;
2373 }
2374 strlist__for_each_entry(nd, bidlist) {
2375 pcache = probe_cache__new(nd->s, NULL);
2376 if (!pcache)
2377 continue;
2378 list_for_each_entry(ent, &pcache->entries, node) {
2379 if (!ent->sdt)
2380 continue;
2381 if (subsys_glob &&
2382 !strglobmatch(ent->pev.group, subsys_glob))
2383 continue;
2384 if (event_glob &&
2385 !strglobmatch(ent->pev.event, event_glob))
2386 continue;
2387 ret = asprintf(&buf, "%s:%s@%s", ent->pev.group,
2388 ent->pev.event, nd->s);
2389 if (ret > 0)
2390 strlist__add(sdtlist, buf);
2391 }
2392 probe_cache__delete(pcache);
2393 }
2394 strlist__delete(bidlist);
2395
2396 strlist__for_each_entry(nd, sdtlist) {
2397 buf = strchr(nd->s, '@');
2398 if (buf)
2399 *(buf++) = '\0';
2400 if (name_only) {
2401 printf("%s ", nd->s);
2402 continue;
2403 }
2404 nd2 = strlist__next(nd);
2405 if (nd2) {
2406 ptr = strchr(nd2->s, '@');
2407 if (ptr)
2408 *ptr = '\0';
2409 if (strcmp(nd->s, nd2->s) == 0)
2410 show_detail = true;
2411 }
2412 if (show_detail) {
2413 path = build_id_cache__origname(buf);
2414 ret = asprintf(&buf, "%s@%s(%.12s)", nd->s, path, buf);
2415 if (ret > 0) {
2416 printf(" %-50s [%s]\n", buf, "SDT event");
2417 free(buf);
2418 }
2419 free(path);
2420 } else
2421 printf(" %-50s [%s]\n", nd->s, "SDT event");
2422 if (nd2) {
2423 if (strcmp(nd->s, nd2->s) != 0)
2424 show_detail = false;
2425 if (ptr)
2426 *ptr = '@';
2427 }
2428 }
2429 strlist__delete(sdtlist);
2430 }
2431
2432 int print_hwcache_events(const char *event_glob, bool name_only)
2433 {
2434 unsigned int type, op, i, evt_i = 0, evt_num = 0;
2435 char name[64];
2436 char **evt_list = NULL;
2437 bool evt_num_known = false;
2438
2439 restart:
2440 if (evt_num_known) {
2441 evt_list = zalloc(sizeof(char *) * evt_num);
2442 if (!evt_list)
2443 goto out_enomem;
2444 }
2445
2446 for (type = 0; type < PERF_COUNT_HW_CACHE_MAX; type++) {
2447 for (op = 0; op < PERF_COUNT_HW_CACHE_OP_MAX; op++) {
2448 /* skip invalid cache type */
2449 if (!perf_evsel__is_cache_op_valid(type, op))
2450 continue;
2451
2452 for (i = 0; i < PERF_COUNT_HW_CACHE_RESULT_MAX; i++) {
2453 __perf_evsel__hw_cache_type_op_res_name(type, op, i,
2454 name, sizeof(name));
2455 if (event_glob != NULL && !strglobmatch(name, event_glob))
2456 continue;
2457
2458 if (!is_event_supported(PERF_TYPE_HW_CACHE,
2459 type | (op << 8) | (i << 16)))
2460 continue;
2461
2462 if (!evt_num_known) {
2463 evt_num++;
2464 continue;
2465 }
2466
2467 evt_list[evt_i] = strdup(name);
2468 if (evt_list[evt_i] == NULL)
2469 goto out_enomem;
2470 evt_i++;
2471 }
2472 }
2473 }
2474
2475 if (!evt_num_known) {
2476 evt_num_known = true;
2477 goto restart;
2478 }
2479 qsort(evt_list, evt_num, sizeof(char *), cmp_string);
2480 evt_i = 0;
2481 while (evt_i < evt_num) {
2482 if (name_only) {
2483 printf("%s ", evt_list[evt_i++]);
2484 continue;
2485 }
2486 printf(" %-50s [%s]\n", evt_list[evt_i++],
2487 event_type_descriptors[PERF_TYPE_HW_CACHE]);
2488 }
2489 if (evt_num && pager_in_use())
2490 printf("\n");
2491
2492 out_free:
2493 evt_num = evt_i;
2494 for (evt_i = 0; evt_i < evt_num; evt_i++)
2495 zfree(&evt_list[evt_i]);
2496 zfree(&evt_list);
2497 return evt_num;
2498
2499 out_enomem:
2500 printf("FATAL: not enough memory to print %s\n", event_type_descriptors[PERF_TYPE_HW_CACHE]);
2501 if (evt_list)
2502 goto out_free;
2503 return evt_num;
2504 }
2505
2506 static void print_tool_event(const char *name, const char *event_glob,
2507 bool name_only)
2508 {
2509 if (event_glob && !strglobmatch(name, event_glob))
2510 return;
2511 if (name_only)
2512 printf("%s ", name);
2513 else
2514 printf(" %-50s [%s]\n", name, "Tool event");
2515
2516 }
2517
2518 void print_tool_events(const char *event_glob, bool name_only)
2519 {
2520 print_tool_event("duration_time", event_glob, name_only);
2521 if (pager_in_use())
2522 printf("\n");
2523 }
2524
2525 void print_symbol_events(const char *event_glob, unsigned type,
2526 struct event_symbol *syms, unsigned max,
2527 bool name_only)
2528 {
2529 unsigned int i, evt_i = 0, evt_num = 0;
2530 char name[MAX_NAME_LEN];
2531 char **evt_list = NULL;
2532 bool evt_num_known = false;
2533
2534 restart:
2535 if (evt_num_known) {
2536 evt_list = zalloc(sizeof(char *) * evt_num);
2537 if (!evt_list)
2538 goto out_enomem;
2539 syms -= max;
2540 }
2541
2542 for (i = 0; i < max; i++, syms++) {
2543
2544 if (event_glob != NULL && syms->symbol != NULL &&
2545 !(strglobmatch(syms->symbol, event_glob) ||
2546 (syms->alias && strglobmatch(syms->alias, event_glob))))
2547 continue;
2548
2549 if (!is_event_supported(type, i))
2550 continue;
2551
2552 if (!evt_num_known) {
2553 evt_num++;
2554 continue;
2555 }
2556
2557 if (!name_only && strlen(syms->alias))
2558 snprintf(name, MAX_NAME_LEN, "%s OR %s", syms->symbol, syms->alias);
2559 else
2560 strlcpy(name, syms->symbol, MAX_NAME_LEN);
2561
2562 evt_list[evt_i] = strdup(name);
2563 if (evt_list[evt_i] == NULL)
2564 goto out_enomem;
2565 evt_i++;
2566 }
2567
2568 if (!evt_num_known) {
2569 evt_num_known = true;
2570 goto restart;
2571 }
2572 qsort(evt_list, evt_num, sizeof(char *), cmp_string);
2573 evt_i = 0;
2574 while (evt_i < evt_num) {
2575 if (name_only) {
2576 printf("%s ", evt_list[evt_i++]);
2577 continue;
2578 }
2579 printf(" %-50s [%s]\n", evt_list[evt_i++], event_type_descriptors[type]);
2580 }
2581 if (evt_num && pager_in_use())
2582 printf("\n");
2583
2584 out_free:
2585 evt_num = evt_i;
2586 for (evt_i = 0; evt_i < evt_num; evt_i++)
2587 zfree(&evt_list[evt_i]);
2588 zfree(&evt_list);
2589 return;
2590
2591 out_enomem:
2592 printf("FATAL: not enough memory to print %s\n", event_type_descriptors[type]);
2593 if (evt_list)
2594 goto out_free;
2595 }
2596
2597 /*
2598 * Print the help text for the event symbols:
2599 */
2600 void print_events(const char *event_glob, bool name_only, bool quiet_flag,
2601 bool long_desc, bool details_flag)
2602 {
2603 print_symbol_events(event_glob, PERF_TYPE_HARDWARE,
2604 event_symbols_hw, PERF_COUNT_HW_MAX, name_only);
2605
2606 print_symbol_events(event_glob, PERF_TYPE_SOFTWARE,
2607 event_symbols_sw, PERF_COUNT_SW_MAX, name_only);
2608 print_tool_events(event_glob, name_only);
2609
2610 print_hwcache_events(event_glob, name_only);
2611
2612 print_pmu_events(event_glob, name_only, quiet_flag, long_desc,
2613 details_flag);
2614
2615 if (event_glob != NULL)
2616 return;
2617
2618 if (!name_only) {
2619 printf(" %-50s [%s]\n",
2620 "rNNN",
2621 event_type_descriptors[PERF_TYPE_RAW]);
2622 printf(" %-50s [%s]\n",
2623 "cpu/t1=v1[,t2=v2,t3 ...]/modifier",
2624 event_type_descriptors[PERF_TYPE_RAW]);
2625 if (pager_in_use())
2626 printf(" (see 'man perf-list' on how to encode it)\n\n");
2627
2628 printf(" %-50s [%s]\n",
2629 "mem:<addr>[/len][:access]",
2630 event_type_descriptors[PERF_TYPE_BREAKPOINT]);
2631 if (pager_in_use())
2632 printf("\n");
2633 }
2634
2635 print_tracepoint_events(NULL, NULL, name_only);
2636
2637 print_sdt_events(NULL, NULL, name_only);
2638
2639 metricgroup__print(true, true, NULL, name_only, details_flag);
2640 }
2641
2642 int parse_events__is_hardcoded_term(struct parse_events_term *term)
2643 {
2644 return term->type_term != PARSE_EVENTS__TERM_TYPE_USER;
2645 }
2646
2647 static int new_term(struct parse_events_term **_term,
2648 struct parse_events_term *temp,
2649 char *str, u64 num)
2650 {
2651 struct parse_events_term *term;
2652
2653 term = malloc(sizeof(*term));
2654 if (!term)
2655 return -ENOMEM;
2656
2657 *term = *temp;
2658 INIT_LIST_HEAD(&term->list);
2659 term->weak = false;
2660
2661 switch (term->type_val) {
2662 case PARSE_EVENTS__TERM_TYPE_NUM:
2663 term->val.num = num;
2664 break;
2665 case PARSE_EVENTS__TERM_TYPE_STR:
2666 term->val.str = str;
2667 break;
2668 default:
2669 free(term);
2670 return -EINVAL;
2671 }
2672
2673 *_term = term;
2674 return 0;
2675 }
2676
2677 int parse_events_term__num(struct parse_events_term **term,
2678 int type_term, char *config, u64 num,
2679 bool no_value,
2680 void *loc_term_, void *loc_val_)
2681 {
2682 YYLTYPE *loc_term = loc_term_;
2683 YYLTYPE *loc_val = loc_val_;
2684
2685 struct parse_events_term temp = {
2686 .type_val = PARSE_EVENTS__TERM_TYPE_NUM,
2687 .type_term = type_term,
2688 .config = config,
2689 .no_value = no_value,
2690 .err_term = loc_term ? loc_term->first_column : 0,
2691 .err_val = loc_val ? loc_val->first_column : 0,
2692 };
2693
2694 return new_term(term, &temp, NULL, num);
2695 }
2696
2697 int parse_events_term__str(struct parse_events_term **term,
2698 int type_term, char *config, char *str,
2699 void *loc_term_, void *loc_val_)
2700 {
2701 YYLTYPE *loc_term = loc_term_;
2702 YYLTYPE *loc_val = loc_val_;
2703
2704 struct parse_events_term temp = {
2705 .type_val = PARSE_EVENTS__TERM_TYPE_STR,
2706 .type_term = type_term,
2707 .config = config,
2708 .err_term = loc_term ? loc_term->first_column : 0,
2709 .err_val = loc_val ? loc_val->first_column : 0,
2710 };
2711
2712 return new_term(term, &temp, str, 0);
2713 }
2714
2715 int parse_events_term__sym_hw(struct parse_events_term **term,
2716 char *config, unsigned idx)
2717 {
2718 struct event_symbol *sym;
2719 struct parse_events_term temp = {
2720 .type_val = PARSE_EVENTS__TERM_TYPE_STR,
2721 .type_term = PARSE_EVENTS__TERM_TYPE_USER,
2722 .config = config ?: (char *) "event",
2723 };
2724
2725 BUG_ON(idx >= PERF_COUNT_HW_MAX);
2726 sym = &event_symbols_hw[idx];
2727
2728 return new_term(term, &temp, (char *) sym->symbol, 0);
2729 }
2730
2731 int parse_events_term__clone(struct parse_events_term **new,
2732 struct parse_events_term *term)
2733 {
2734 struct parse_events_term temp = {
2735 .type_val = term->type_val,
2736 .type_term = term->type_term,
2737 .config = term->config,
2738 .err_term = term->err_term,
2739 .err_val = term->err_val,
2740 };
2741
2742 return new_term(new, &temp, term->val.str, term->val.num);
2743 }
2744
2745 int parse_events_copy_term_list(struct list_head *old,
2746 struct list_head **new)
2747 {
2748 struct parse_events_term *term, *n;
2749 int ret;
2750
2751 if (!old) {
2752 *new = NULL;
2753 return 0;
2754 }
2755
2756 *new = malloc(sizeof(struct list_head));
2757 if (!*new)
2758 return -ENOMEM;
2759 INIT_LIST_HEAD(*new);
2760
2761 list_for_each_entry (term, old, list) {
2762 ret = parse_events_term__clone(&n, term);
2763 if (ret)
2764 return ret;
2765 list_add_tail(&n->list, *new);
2766 }
2767 return 0;
2768 }
2769
2770 void parse_events_terms__purge(struct list_head *terms)
2771 {
2772 struct parse_events_term *term, *h;
2773
2774 list_for_each_entry_safe(term, h, terms, list) {
2775 if (term->array.nr_ranges)
2776 zfree(&term->array.ranges);
2777 list_del_init(&term->list);
2778 free(term);
2779 }
2780 }
2781
2782 void parse_events_terms__delete(struct list_head *terms)
2783 {
2784 if (!terms)
2785 return;
2786 parse_events_terms__purge(terms);
2787 free(terms);
2788 }
2789
2790 void parse_events__clear_array(struct parse_events_array *a)
2791 {
2792 zfree(&a->ranges);
2793 }
2794
2795 void parse_events_evlist_error(struct parse_events_state *parse_state,
2796 int idx, const char *str)
2797 {
2798 struct parse_events_error *err = parse_state->error;
2799
2800 if (!err)
2801 return;
2802 err->idx = idx;
2803 err->str = strdup(str);
2804 WARN_ONCE(!err->str, "WARNING: failed to allocate error string");
2805 }
2806
2807 static void config_terms_list(char *buf, size_t buf_sz)
2808 {
2809 int i;
2810 bool first = true;
2811
2812 buf[0] = '\0';
2813 for (i = 0; i < __PARSE_EVENTS__TERM_TYPE_NR; i++) {
2814 const char *name = config_term_names[i];
2815
2816 if (!config_term_avail(i, NULL))
2817 continue;
2818 if (!name)
2819 continue;
2820 if (name[0] == '<')
2821 continue;
2822
2823 if (strlen(buf) + strlen(name) + 2 >= buf_sz)
2824 return;
2825
2826 if (!first)
2827 strcat(buf, ",");
2828 else
2829 first = false;
2830 strcat(buf, name);
2831 }
2832 }
2833
2834 /*
2835 * Return string contains valid config terms of an event.
2836 * @additional_terms: For terms such as PMU sysfs terms.
2837 */
2838 char *parse_events_formats_error_string(char *additional_terms)
2839 {
2840 char *str;
2841 /* "no-overwrite" is the longest name */
2842 char static_terms[__PARSE_EVENTS__TERM_TYPE_NR *
2843 (sizeof("no-overwrite") - 1)];
2844
2845 config_terms_list(static_terms, sizeof(static_terms));
2846 /* valid terms */
2847 if (additional_terms) {
2848 if (asprintf(&str, "valid terms: %s,%s",
2849 additional_terms, static_terms) < 0)
2850 goto fail;
2851 } else {
2852 if (asprintf(&str, "valid terms: %s", static_terms) < 0)
2853 goto fail;
2854 }
2855 return str;
2856
2857 fail:
2858 return NULL;
2859 }