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