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