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