]> git.ipfire.org Git - people/ms/linux.git/blob - tools/perf/util/parse-events.c
Merge branch 'perf/hw_breakpoints' into perf/core
[people/ms/linux.git] / tools / perf / util / parse-events.c
1 #include <linux/hw_breakpoint.h>
2 #include "util.h"
3 #include "../perf.h"
4 #include "evlist.h"
5 #include "evsel.h"
6 #include "parse-options.h"
7 #include "parse-events.h"
8 #include "exec_cmd.h"
9 #include "string.h"
10 #include "symbol.h"
11 #include "cache.h"
12 #include "header.h"
13 #include "debug.h"
14 #include <api/fs/debugfs.h>
15 #include "parse-events-bison.h"
16 #define YY_EXTRA_TYPE int
17 #include "parse-events-flex.h"
18 #include "pmu.h"
19 #include "thread_map.h"
20
21 #define MAX_NAME_LEN 100
22
23 struct event_symbol {
24 const char *symbol;
25 const char *alias;
26 };
27
28 #ifdef PARSER_DEBUG
29 extern int parse_events_debug;
30 #endif
31 int parse_events_parse(void *data, void *scanner);
32
33 static struct perf_pmu_event_symbol *perf_pmu_events_list;
34 /*
35 * The variable indicates the number of supported pmu event symbols.
36 * 0 means not initialized and ready to init
37 * -1 means failed to init, don't try anymore
38 * >0 is the number of supported pmu event symbols
39 */
40 static int perf_pmu_events_list_num;
41
42 static struct event_symbol event_symbols_hw[PERF_COUNT_HW_MAX] = {
43 [PERF_COUNT_HW_CPU_CYCLES] = {
44 .symbol = "cpu-cycles",
45 .alias = "cycles",
46 },
47 [PERF_COUNT_HW_INSTRUCTIONS] = {
48 .symbol = "instructions",
49 .alias = "",
50 },
51 [PERF_COUNT_HW_CACHE_REFERENCES] = {
52 .symbol = "cache-references",
53 .alias = "",
54 },
55 [PERF_COUNT_HW_CACHE_MISSES] = {
56 .symbol = "cache-misses",
57 .alias = "",
58 },
59 [PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = {
60 .symbol = "branch-instructions",
61 .alias = "branches",
62 },
63 [PERF_COUNT_HW_BRANCH_MISSES] = {
64 .symbol = "branch-misses",
65 .alias = "",
66 },
67 [PERF_COUNT_HW_BUS_CYCLES] = {
68 .symbol = "bus-cycles",
69 .alias = "",
70 },
71 [PERF_COUNT_HW_STALLED_CYCLES_FRONTEND] = {
72 .symbol = "stalled-cycles-frontend",
73 .alias = "idle-cycles-frontend",
74 },
75 [PERF_COUNT_HW_STALLED_CYCLES_BACKEND] = {
76 .symbol = "stalled-cycles-backend",
77 .alias = "idle-cycles-backend",
78 },
79 [PERF_COUNT_HW_REF_CPU_CYCLES] = {
80 .symbol = "ref-cycles",
81 .alias = "",
82 },
83 };
84
85 static struct event_symbol event_symbols_sw[PERF_COUNT_SW_MAX] = {
86 [PERF_COUNT_SW_CPU_CLOCK] = {
87 .symbol = "cpu-clock",
88 .alias = "",
89 },
90 [PERF_COUNT_SW_TASK_CLOCK] = {
91 .symbol = "task-clock",
92 .alias = "",
93 },
94 [PERF_COUNT_SW_PAGE_FAULTS] = {
95 .symbol = "page-faults",
96 .alias = "faults",
97 },
98 [PERF_COUNT_SW_CONTEXT_SWITCHES] = {
99 .symbol = "context-switches",
100 .alias = "cs",
101 },
102 [PERF_COUNT_SW_CPU_MIGRATIONS] = {
103 .symbol = "cpu-migrations",
104 .alias = "migrations",
105 },
106 [PERF_COUNT_SW_PAGE_FAULTS_MIN] = {
107 .symbol = "minor-faults",
108 .alias = "",
109 },
110 [PERF_COUNT_SW_PAGE_FAULTS_MAJ] = {
111 .symbol = "major-faults",
112 .alias = "",
113 },
114 [PERF_COUNT_SW_ALIGNMENT_FAULTS] = {
115 .symbol = "alignment-faults",
116 .alias = "",
117 },
118 [PERF_COUNT_SW_EMULATION_FAULTS] = {
119 .symbol = "emulation-faults",
120 .alias = "",
121 },
122 [PERF_COUNT_SW_DUMMY] = {
123 .symbol = "dummy",
124 .alias = "",
125 },
126 };
127
128 #define __PERF_EVENT_FIELD(config, name) \
129 ((config & PERF_EVENT_##name##_MASK) >> PERF_EVENT_##name##_SHIFT)
130
131 #define PERF_EVENT_RAW(config) __PERF_EVENT_FIELD(config, RAW)
132 #define PERF_EVENT_CONFIG(config) __PERF_EVENT_FIELD(config, CONFIG)
133 #define PERF_EVENT_TYPE(config) __PERF_EVENT_FIELD(config, TYPE)
134 #define PERF_EVENT_ID(config) __PERF_EVENT_FIELD(config, EVENT)
135
136 #define for_each_subsystem(sys_dir, sys_dirent, sys_next) \
137 while (!readdir_r(sys_dir, &sys_dirent, &sys_next) && sys_next) \
138 if (sys_dirent.d_type == DT_DIR && \
139 (strcmp(sys_dirent.d_name, ".")) && \
140 (strcmp(sys_dirent.d_name, "..")))
141
142 static int tp_event_has_id(struct dirent *sys_dir, struct dirent *evt_dir)
143 {
144 char evt_path[MAXPATHLEN];
145 int fd;
146
147 snprintf(evt_path, MAXPATHLEN, "%s/%s/%s/id", tracing_events_path,
148 sys_dir->d_name, evt_dir->d_name);
149 fd = open(evt_path, O_RDONLY);
150 if (fd < 0)
151 return -EINVAL;
152 close(fd);
153
154 return 0;
155 }
156
157 #define for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next) \
158 while (!readdir_r(evt_dir, &evt_dirent, &evt_next) && evt_next) \
159 if (evt_dirent.d_type == DT_DIR && \
160 (strcmp(evt_dirent.d_name, ".")) && \
161 (strcmp(evt_dirent.d_name, "..")) && \
162 (!tp_event_has_id(&sys_dirent, &evt_dirent)))
163
164 #define MAX_EVENT_LENGTH 512
165
166
167 struct tracepoint_path *tracepoint_id_to_path(u64 config)
168 {
169 struct tracepoint_path *path = NULL;
170 DIR *sys_dir, *evt_dir;
171 struct dirent *sys_next, *evt_next, sys_dirent, evt_dirent;
172 char id_buf[24];
173 int fd;
174 u64 id;
175 char evt_path[MAXPATHLEN];
176 char dir_path[MAXPATHLEN];
177
178 if (debugfs_valid_mountpoint(tracing_events_path))
179 return NULL;
180
181 sys_dir = opendir(tracing_events_path);
182 if (!sys_dir)
183 return NULL;
184
185 for_each_subsystem(sys_dir, sys_dirent, sys_next) {
186
187 snprintf(dir_path, MAXPATHLEN, "%s/%s", tracing_events_path,
188 sys_dirent.d_name);
189 evt_dir = opendir(dir_path);
190 if (!evt_dir)
191 continue;
192
193 for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next) {
194
195 snprintf(evt_path, MAXPATHLEN, "%s/%s/id", dir_path,
196 evt_dirent.d_name);
197 fd = open(evt_path, O_RDONLY);
198 if (fd < 0)
199 continue;
200 if (read(fd, id_buf, sizeof(id_buf)) < 0) {
201 close(fd);
202 continue;
203 }
204 close(fd);
205 id = atoll(id_buf);
206 if (id == config) {
207 closedir(evt_dir);
208 closedir(sys_dir);
209 path = zalloc(sizeof(*path));
210 path->system = malloc(MAX_EVENT_LENGTH);
211 if (!path->system) {
212 free(path);
213 return NULL;
214 }
215 path->name = malloc(MAX_EVENT_LENGTH);
216 if (!path->name) {
217 zfree(&path->system);
218 free(path);
219 return NULL;
220 }
221 strncpy(path->system, sys_dirent.d_name,
222 MAX_EVENT_LENGTH);
223 strncpy(path->name, evt_dirent.d_name,
224 MAX_EVENT_LENGTH);
225 return path;
226 }
227 }
228 closedir(evt_dir);
229 }
230
231 closedir(sys_dir);
232 return NULL;
233 }
234
235 struct tracepoint_path *tracepoint_name_to_path(const char *name)
236 {
237 struct tracepoint_path *path = zalloc(sizeof(*path));
238 char *str = strchr(name, ':');
239
240 if (path == NULL || str == NULL) {
241 free(path);
242 return NULL;
243 }
244
245 path->system = strndup(name, str - name);
246 path->name = strdup(str+1);
247
248 if (path->system == NULL || path->name == NULL) {
249 zfree(&path->system);
250 zfree(&path->name);
251 free(path);
252 path = NULL;
253 }
254
255 return path;
256 }
257
258 const char *event_type(int type)
259 {
260 switch (type) {
261 case PERF_TYPE_HARDWARE:
262 return "hardware";
263
264 case PERF_TYPE_SOFTWARE:
265 return "software";
266
267 case PERF_TYPE_TRACEPOINT:
268 return "tracepoint";
269
270 case PERF_TYPE_HW_CACHE:
271 return "hardware-cache";
272
273 default:
274 break;
275 }
276
277 return "unknown";
278 }
279
280
281
282 static struct perf_evsel *
283 __add_event(struct list_head *list, int *idx,
284 struct perf_event_attr *attr,
285 char *name, struct cpu_map *cpus)
286 {
287 struct perf_evsel *evsel;
288
289 event_attr_init(attr);
290
291 evsel = perf_evsel__new_idx(attr, (*idx)++);
292 if (!evsel)
293 return NULL;
294
295 evsel->cpus = cpus;
296 if (name)
297 evsel->name = strdup(name);
298 list_add_tail(&evsel->node, list);
299 return evsel;
300 }
301
302 static int add_event(struct list_head *list, int *idx,
303 struct perf_event_attr *attr, char *name)
304 {
305 return __add_event(list, idx, attr, name, NULL) ? 0 : -ENOMEM;
306 }
307
308 static int parse_aliases(char *str, const char *names[][PERF_EVSEL__MAX_ALIASES], int size)
309 {
310 int i, j;
311 int n, longest = -1;
312
313 for (i = 0; i < size; i++) {
314 for (j = 0; j < PERF_EVSEL__MAX_ALIASES && names[i][j]; j++) {
315 n = strlen(names[i][j]);
316 if (n > longest && !strncasecmp(str, names[i][j], n))
317 longest = n;
318 }
319 if (longest > 0)
320 return i;
321 }
322
323 return -1;
324 }
325
326 int parse_events_add_cache(struct list_head *list, int *idx,
327 char *type, char *op_result1, char *op_result2)
328 {
329 struct perf_event_attr attr;
330 char name[MAX_NAME_LEN];
331 int cache_type = -1, cache_op = -1, cache_result = -1;
332 char *op_result[2] = { op_result1, op_result2 };
333 int i, n;
334
335 /*
336 * No fallback - if we cannot get a clear cache type
337 * then bail out:
338 */
339 cache_type = parse_aliases(type, perf_evsel__hw_cache,
340 PERF_COUNT_HW_CACHE_MAX);
341 if (cache_type == -1)
342 return -EINVAL;
343
344 n = snprintf(name, MAX_NAME_LEN, "%s", type);
345
346 for (i = 0; (i < 2) && (op_result[i]); i++) {
347 char *str = op_result[i];
348
349 n += snprintf(name + n, MAX_NAME_LEN - n, "-%s", str);
350
351 if (cache_op == -1) {
352 cache_op = parse_aliases(str, perf_evsel__hw_cache_op,
353 PERF_COUNT_HW_CACHE_OP_MAX);
354 if (cache_op >= 0) {
355 if (!perf_evsel__is_cache_op_valid(cache_type, cache_op))
356 return -EINVAL;
357 continue;
358 }
359 }
360
361 if (cache_result == -1) {
362 cache_result = parse_aliases(str, perf_evsel__hw_cache_result,
363 PERF_COUNT_HW_CACHE_RESULT_MAX);
364 if (cache_result >= 0)
365 continue;
366 }
367 }
368
369 /*
370 * Fall back to reads:
371 */
372 if (cache_op == -1)
373 cache_op = PERF_COUNT_HW_CACHE_OP_READ;
374
375 /*
376 * Fall back to accesses:
377 */
378 if (cache_result == -1)
379 cache_result = PERF_COUNT_HW_CACHE_RESULT_ACCESS;
380
381 memset(&attr, 0, sizeof(attr));
382 attr.config = cache_type | (cache_op << 8) | (cache_result << 16);
383 attr.type = PERF_TYPE_HW_CACHE;
384 return add_event(list, idx, &attr, name);
385 }
386
387 static int add_tracepoint(struct list_head *list, int *idx,
388 char *sys_name, char *evt_name)
389 {
390 struct perf_evsel *evsel;
391
392 evsel = perf_evsel__newtp_idx(sys_name, evt_name, (*idx)++);
393 if (!evsel)
394 return -ENOMEM;
395
396 list_add_tail(&evsel->node, list);
397
398 return 0;
399 }
400
401 static int add_tracepoint_multi_event(struct list_head *list, int *idx,
402 char *sys_name, char *evt_name)
403 {
404 char evt_path[MAXPATHLEN];
405 struct dirent *evt_ent;
406 DIR *evt_dir;
407 int ret = 0;
408
409 snprintf(evt_path, MAXPATHLEN, "%s/%s", tracing_events_path, sys_name);
410 evt_dir = opendir(evt_path);
411 if (!evt_dir) {
412 perror("Can't open event dir");
413 return -1;
414 }
415
416 while (!ret && (evt_ent = readdir(evt_dir))) {
417 if (!strcmp(evt_ent->d_name, ".")
418 || !strcmp(evt_ent->d_name, "..")
419 || !strcmp(evt_ent->d_name, "enable")
420 || !strcmp(evt_ent->d_name, "filter"))
421 continue;
422
423 if (!strglobmatch(evt_ent->d_name, evt_name))
424 continue;
425
426 ret = add_tracepoint(list, idx, sys_name, evt_ent->d_name);
427 }
428
429 closedir(evt_dir);
430 return ret;
431 }
432
433 static int add_tracepoint_event(struct list_head *list, int *idx,
434 char *sys_name, char *evt_name)
435 {
436 return strpbrk(evt_name, "*?") ?
437 add_tracepoint_multi_event(list, idx, sys_name, evt_name) :
438 add_tracepoint(list, idx, sys_name, evt_name);
439 }
440
441 static int add_tracepoint_multi_sys(struct list_head *list, int *idx,
442 char *sys_name, char *evt_name)
443 {
444 struct dirent *events_ent;
445 DIR *events_dir;
446 int ret = 0;
447
448 events_dir = opendir(tracing_events_path);
449 if (!events_dir) {
450 perror("Can't open event dir");
451 return -1;
452 }
453
454 while (!ret && (events_ent = readdir(events_dir))) {
455 if (!strcmp(events_ent->d_name, ".")
456 || !strcmp(events_ent->d_name, "..")
457 || !strcmp(events_ent->d_name, "enable")
458 || !strcmp(events_ent->d_name, "header_event")
459 || !strcmp(events_ent->d_name, "header_page"))
460 continue;
461
462 if (!strglobmatch(events_ent->d_name, sys_name))
463 continue;
464
465 ret = add_tracepoint_event(list, idx, events_ent->d_name,
466 evt_name);
467 }
468
469 closedir(events_dir);
470 return ret;
471 }
472
473 int parse_events_add_tracepoint(struct list_head *list, int *idx,
474 char *sys, char *event)
475 {
476 int ret;
477
478 ret = debugfs_valid_mountpoint(tracing_events_path);
479 if (ret)
480 return ret;
481
482 if (strpbrk(sys, "*?"))
483 return add_tracepoint_multi_sys(list, idx, sys, event);
484 else
485 return add_tracepoint_event(list, idx, sys, event);
486 }
487
488 static int
489 parse_breakpoint_type(const char *type, struct perf_event_attr *attr)
490 {
491 int i;
492
493 for (i = 0; i < 3; i++) {
494 if (!type || !type[i])
495 break;
496
497 #define CHECK_SET_TYPE(bit) \
498 do { \
499 if (attr->bp_type & bit) \
500 return -EINVAL; \
501 else \
502 attr->bp_type |= bit; \
503 } while (0)
504
505 switch (type[i]) {
506 case 'r':
507 CHECK_SET_TYPE(HW_BREAKPOINT_R);
508 break;
509 case 'w':
510 CHECK_SET_TYPE(HW_BREAKPOINT_W);
511 break;
512 case 'x':
513 CHECK_SET_TYPE(HW_BREAKPOINT_X);
514 break;
515 default:
516 return -EINVAL;
517 }
518 }
519
520 #undef CHECK_SET_TYPE
521
522 if (!attr->bp_type) /* Default */
523 attr->bp_type = HW_BREAKPOINT_R | HW_BREAKPOINT_W;
524
525 return 0;
526 }
527
528 int parse_events_add_breakpoint(struct list_head *list, int *idx,
529 void *ptr, char *type, u64 len)
530 {
531 struct perf_event_attr attr;
532
533 memset(&attr, 0, sizeof(attr));
534 attr.bp_addr = (unsigned long) ptr;
535
536 if (parse_breakpoint_type(type, &attr))
537 return -EINVAL;
538
539 /* Provide some defaults if len is not specified */
540 if (!len) {
541 if (attr.bp_type == HW_BREAKPOINT_X)
542 len = sizeof(long);
543 else
544 len = HW_BREAKPOINT_LEN_4;
545 }
546
547 attr.bp_len = len;
548
549 attr.type = PERF_TYPE_BREAKPOINT;
550 attr.sample_period = 1;
551
552 return add_event(list, idx, &attr, NULL);
553 }
554
555 static int config_term(struct perf_event_attr *attr,
556 struct parse_events_term *term)
557 {
558 #define CHECK_TYPE_VAL(type) \
559 do { \
560 if (PARSE_EVENTS__TERM_TYPE_ ## type != term->type_val) \
561 return -EINVAL; \
562 } while (0)
563
564 switch (term->type_term) {
565 case PARSE_EVENTS__TERM_TYPE_CONFIG:
566 CHECK_TYPE_VAL(NUM);
567 attr->config = term->val.num;
568 break;
569 case PARSE_EVENTS__TERM_TYPE_CONFIG1:
570 CHECK_TYPE_VAL(NUM);
571 attr->config1 = term->val.num;
572 break;
573 case PARSE_EVENTS__TERM_TYPE_CONFIG2:
574 CHECK_TYPE_VAL(NUM);
575 attr->config2 = term->val.num;
576 break;
577 case PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD:
578 CHECK_TYPE_VAL(NUM);
579 attr->sample_period = term->val.num;
580 break;
581 case PARSE_EVENTS__TERM_TYPE_BRANCH_SAMPLE_TYPE:
582 /*
583 * TODO uncomment when the field is available
584 * attr->branch_sample_type = term->val.num;
585 */
586 break;
587 case PARSE_EVENTS__TERM_TYPE_NAME:
588 CHECK_TYPE_VAL(STR);
589 break;
590 default:
591 return -EINVAL;
592 }
593
594 return 0;
595 #undef CHECK_TYPE_VAL
596 }
597
598 static int config_attr(struct perf_event_attr *attr,
599 struct list_head *head, int fail)
600 {
601 struct parse_events_term *term;
602
603 list_for_each_entry(term, head, list)
604 if (config_term(attr, term) && fail)
605 return -EINVAL;
606
607 return 0;
608 }
609
610 int parse_events_add_numeric(struct list_head *list, int *idx,
611 u32 type, u64 config,
612 struct list_head *head_config)
613 {
614 struct perf_event_attr attr;
615
616 memset(&attr, 0, sizeof(attr));
617 attr.type = type;
618 attr.config = config;
619
620 if (head_config &&
621 config_attr(&attr, head_config, 1))
622 return -EINVAL;
623
624 return add_event(list, idx, &attr, NULL);
625 }
626
627 static int parse_events__is_name_term(struct parse_events_term *term)
628 {
629 return term->type_term == PARSE_EVENTS__TERM_TYPE_NAME;
630 }
631
632 static char *pmu_event_name(struct list_head *head_terms)
633 {
634 struct parse_events_term *term;
635
636 list_for_each_entry(term, head_terms, list)
637 if (parse_events__is_name_term(term))
638 return term->val.str;
639
640 return NULL;
641 }
642
643 int parse_events_add_pmu(struct list_head *list, int *idx,
644 char *name, struct list_head *head_config)
645 {
646 struct perf_event_attr attr;
647 struct perf_pmu_info info;
648 struct perf_pmu *pmu;
649 struct perf_evsel *evsel;
650
651 pmu = perf_pmu__find(name);
652 if (!pmu)
653 return -EINVAL;
654
655 if (pmu->default_config) {
656 memcpy(&attr, pmu->default_config,
657 sizeof(struct perf_event_attr));
658 } else {
659 memset(&attr, 0, sizeof(attr));
660 }
661
662 if (!head_config) {
663 attr.type = pmu->type;
664 evsel = __add_event(list, idx, &attr, NULL, pmu->cpus);
665 return evsel ? 0 : -ENOMEM;
666 }
667
668 if (perf_pmu__check_alias(pmu, head_config, &info))
669 return -EINVAL;
670
671 /*
672 * Configure hardcoded terms first, no need to check
673 * return value when called with fail == 0 ;)
674 */
675 config_attr(&attr, head_config, 0);
676
677 if (perf_pmu__config(pmu, &attr, head_config))
678 return -EINVAL;
679
680 evsel = __add_event(list, idx, &attr, pmu_event_name(head_config),
681 pmu->cpus);
682 if (evsel) {
683 evsel->unit = info.unit;
684 evsel->scale = info.scale;
685 evsel->per_pkg = info.per_pkg;
686 evsel->snapshot = info.snapshot;
687 }
688
689 return evsel ? 0 : -ENOMEM;
690 }
691
692 int parse_events__modifier_group(struct list_head *list,
693 char *event_mod)
694 {
695 return parse_events__modifier_event(list, event_mod, true);
696 }
697
698 void parse_events__set_leader(char *name, struct list_head *list)
699 {
700 struct perf_evsel *leader;
701
702 __perf_evlist__set_leader(list);
703 leader = list_entry(list->next, struct perf_evsel, node);
704 leader->group_name = name ? strdup(name) : NULL;
705 }
706
707 /* list_event is assumed to point to malloc'ed memory */
708 void parse_events_update_lists(struct list_head *list_event,
709 struct list_head *list_all)
710 {
711 /*
712 * Called for single event definition. Update the
713 * 'all event' list, and reinit the 'single event'
714 * list, for next event definition.
715 */
716 list_splice_tail(list_event, list_all);
717 free(list_event);
718 }
719
720 struct event_modifier {
721 int eu;
722 int ek;
723 int eh;
724 int eH;
725 int eG;
726 int precise;
727 int exclude_GH;
728 int sample_read;
729 int pinned;
730 };
731
732 static int get_event_modifier(struct event_modifier *mod, char *str,
733 struct perf_evsel *evsel)
734 {
735 int eu = evsel ? evsel->attr.exclude_user : 0;
736 int ek = evsel ? evsel->attr.exclude_kernel : 0;
737 int eh = evsel ? evsel->attr.exclude_hv : 0;
738 int eH = evsel ? evsel->attr.exclude_host : 0;
739 int eG = evsel ? evsel->attr.exclude_guest : 0;
740 int precise = evsel ? evsel->attr.precise_ip : 0;
741 int sample_read = 0;
742 int pinned = evsel ? evsel->attr.pinned : 0;
743
744 int exclude = eu | ek | eh;
745 int exclude_GH = evsel ? evsel->exclude_GH : 0;
746
747 memset(mod, 0, sizeof(*mod));
748
749 while (*str) {
750 if (*str == 'u') {
751 if (!exclude)
752 exclude = eu = ek = eh = 1;
753 eu = 0;
754 } else if (*str == 'k') {
755 if (!exclude)
756 exclude = eu = ek = eh = 1;
757 ek = 0;
758 } else if (*str == 'h') {
759 if (!exclude)
760 exclude = eu = ek = eh = 1;
761 eh = 0;
762 } else if (*str == 'G') {
763 if (!exclude_GH)
764 exclude_GH = eG = eH = 1;
765 eG = 0;
766 } else if (*str == 'H') {
767 if (!exclude_GH)
768 exclude_GH = eG = eH = 1;
769 eH = 0;
770 } else if (*str == 'p') {
771 precise++;
772 /* use of precise requires exclude_guest */
773 if (!exclude_GH)
774 eG = 1;
775 } else if (*str == 'S') {
776 sample_read = 1;
777 } else if (*str == 'D') {
778 pinned = 1;
779 } else
780 break;
781
782 ++str;
783 }
784
785 /*
786 * precise ip:
787 *
788 * 0 - SAMPLE_IP can have arbitrary skid
789 * 1 - SAMPLE_IP must have constant skid
790 * 2 - SAMPLE_IP requested to have 0 skid
791 * 3 - SAMPLE_IP must have 0 skid
792 *
793 * See also PERF_RECORD_MISC_EXACT_IP
794 */
795 if (precise > 3)
796 return -EINVAL;
797
798 mod->eu = eu;
799 mod->ek = ek;
800 mod->eh = eh;
801 mod->eH = eH;
802 mod->eG = eG;
803 mod->precise = precise;
804 mod->exclude_GH = exclude_GH;
805 mod->sample_read = sample_read;
806 mod->pinned = pinned;
807
808 return 0;
809 }
810
811 /*
812 * Basic modifier sanity check to validate it contains only one
813 * instance of any modifier (apart from 'p') present.
814 */
815 static int check_modifier(char *str)
816 {
817 char *p = str;
818
819 /* The sizeof includes 0 byte as well. */
820 if (strlen(str) > (sizeof("ukhGHpppSD") - 1))
821 return -1;
822
823 while (*p) {
824 if (*p != 'p' && strchr(p + 1, *p))
825 return -1;
826 p++;
827 }
828
829 return 0;
830 }
831
832 int parse_events__modifier_event(struct list_head *list, char *str, bool add)
833 {
834 struct perf_evsel *evsel;
835 struct event_modifier mod;
836
837 if (str == NULL)
838 return 0;
839
840 if (check_modifier(str))
841 return -EINVAL;
842
843 if (!add && get_event_modifier(&mod, str, NULL))
844 return -EINVAL;
845
846 __evlist__for_each(list, evsel) {
847 if (add && get_event_modifier(&mod, str, evsel))
848 return -EINVAL;
849
850 evsel->attr.exclude_user = mod.eu;
851 evsel->attr.exclude_kernel = mod.ek;
852 evsel->attr.exclude_hv = mod.eh;
853 evsel->attr.precise_ip = mod.precise;
854 evsel->attr.exclude_host = mod.eH;
855 evsel->attr.exclude_guest = mod.eG;
856 evsel->exclude_GH = mod.exclude_GH;
857 evsel->sample_read = mod.sample_read;
858
859 if (perf_evsel__is_group_leader(evsel))
860 evsel->attr.pinned = mod.pinned;
861 }
862
863 return 0;
864 }
865
866 int parse_events_name(struct list_head *list, char *name)
867 {
868 struct perf_evsel *evsel;
869
870 __evlist__for_each(list, evsel) {
871 if (!evsel->name)
872 evsel->name = strdup(name);
873 }
874
875 return 0;
876 }
877
878 static int
879 comp_pmu(const void *p1, const void *p2)
880 {
881 struct perf_pmu_event_symbol *pmu1 = (struct perf_pmu_event_symbol *) p1;
882 struct perf_pmu_event_symbol *pmu2 = (struct perf_pmu_event_symbol *) p2;
883
884 return strcmp(pmu1->symbol, pmu2->symbol);
885 }
886
887 static void perf_pmu__parse_cleanup(void)
888 {
889 if (perf_pmu_events_list_num > 0) {
890 struct perf_pmu_event_symbol *p;
891 int i;
892
893 for (i = 0; i < perf_pmu_events_list_num; i++) {
894 p = perf_pmu_events_list + i;
895 free(p->symbol);
896 }
897 free(perf_pmu_events_list);
898 perf_pmu_events_list = NULL;
899 perf_pmu_events_list_num = 0;
900 }
901 }
902
903 #define SET_SYMBOL(str, stype) \
904 do { \
905 p->symbol = str; \
906 if (!p->symbol) \
907 goto err; \
908 p->type = stype; \
909 } while (0)
910
911 /*
912 * Read the pmu events list from sysfs
913 * Save it into perf_pmu_events_list
914 */
915 static void perf_pmu__parse_init(void)
916 {
917
918 struct perf_pmu *pmu = NULL;
919 struct perf_pmu_alias *alias;
920 int len = 0;
921
922 pmu = perf_pmu__find("cpu");
923 if ((pmu == NULL) || list_empty(&pmu->aliases)) {
924 perf_pmu_events_list_num = -1;
925 return;
926 }
927 list_for_each_entry(alias, &pmu->aliases, list) {
928 if (strchr(alias->name, '-'))
929 len++;
930 len++;
931 }
932 perf_pmu_events_list = malloc(sizeof(struct perf_pmu_event_symbol) * len);
933 if (!perf_pmu_events_list)
934 return;
935 perf_pmu_events_list_num = len;
936
937 len = 0;
938 list_for_each_entry(alias, &pmu->aliases, list) {
939 struct perf_pmu_event_symbol *p = perf_pmu_events_list + len;
940 char *tmp = strchr(alias->name, '-');
941
942 if (tmp != NULL) {
943 SET_SYMBOL(strndup(alias->name, tmp - alias->name),
944 PMU_EVENT_SYMBOL_PREFIX);
945 p++;
946 SET_SYMBOL(strdup(++tmp), PMU_EVENT_SYMBOL_SUFFIX);
947 len += 2;
948 } else {
949 SET_SYMBOL(strdup(alias->name), PMU_EVENT_SYMBOL);
950 len++;
951 }
952 }
953 qsort(perf_pmu_events_list, len,
954 sizeof(struct perf_pmu_event_symbol), comp_pmu);
955
956 return;
957 err:
958 perf_pmu__parse_cleanup();
959 }
960
961 enum perf_pmu_event_symbol_type
962 perf_pmu__parse_check(const char *name)
963 {
964 struct perf_pmu_event_symbol p, *r;
965
966 /* scan kernel pmu events from sysfs if needed */
967 if (perf_pmu_events_list_num == 0)
968 perf_pmu__parse_init();
969 /*
970 * name "cpu" could be prefix of cpu-cycles or cpu// events.
971 * cpu-cycles has been handled by hardcode.
972 * So it must be cpu// events, not kernel pmu event.
973 */
974 if ((perf_pmu_events_list_num <= 0) || !strcmp(name, "cpu"))
975 return PMU_EVENT_SYMBOL_ERR;
976
977 p.symbol = strdup(name);
978 r = bsearch(&p, perf_pmu_events_list,
979 (size_t) perf_pmu_events_list_num,
980 sizeof(struct perf_pmu_event_symbol), comp_pmu);
981 free(p.symbol);
982 return r ? r->type : PMU_EVENT_SYMBOL_ERR;
983 }
984
985 static int parse_events__scanner(const char *str, void *data, int start_token)
986 {
987 YY_BUFFER_STATE buffer;
988 void *scanner;
989 int ret;
990
991 ret = parse_events_lex_init_extra(start_token, &scanner);
992 if (ret)
993 return ret;
994
995 buffer = parse_events__scan_string(str, scanner);
996
997 #ifdef PARSER_DEBUG
998 parse_events_debug = 1;
999 #endif
1000 ret = parse_events_parse(data, scanner);
1001
1002 parse_events__flush_buffer(buffer, scanner);
1003 parse_events__delete_buffer(buffer, scanner);
1004 parse_events_lex_destroy(scanner);
1005 return ret;
1006 }
1007
1008 /*
1009 * parse event config string, return a list of event terms.
1010 */
1011 int parse_events_terms(struct list_head *terms, const char *str)
1012 {
1013 struct parse_events_terms data = {
1014 .terms = NULL,
1015 };
1016 int ret;
1017
1018 ret = parse_events__scanner(str, &data, PE_START_TERMS);
1019 if (!ret) {
1020 list_splice(data.terms, terms);
1021 zfree(&data.terms);
1022 return 0;
1023 }
1024
1025 if (data.terms)
1026 parse_events__free_terms(data.terms);
1027 return ret;
1028 }
1029
1030 int parse_events(struct perf_evlist *evlist, const char *str)
1031 {
1032 struct parse_events_evlist data = {
1033 .list = LIST_HEAD_INIT(data.list),
1034 .idx = evlist->nr_entries,
1035 };
1036 int ret;
1037
1038 ret = parse_events__scanner(str, &data, PE_START_EVENTS);
1039 perf_pmu__parse_cleanup();
1040 if (!ret) {
1041 int entries = data.idx - evlist->nr_entries;
1042 perf_evlist__splice_list_tail(evlist, &data.list, entries);
1043 evlist->nr_groups += data.nr_groups;
1044 return 0;
1045 }
1046
1047 /*
1048 * There are 2 users - builtin-record and builtin-test objects.
1049 * Both call perf_evlist__delete in case of error, so we dont
1050 * need to bother.
1051 */
1052 return ret;
1053 }
1054
1055 int parse_events_option(const struct option *opt, const char *str,
1056 int unset __maybe_unused)
1057 {
1058 struct perf_evlist *evlist = *(struct perf_evlist **)opt->value;
1059 int ret = parse_events(evlist, str);
1060
1061 if (ret) {
1062 fprintf(stderr, "invalid or unsupported event: '%s'\n", str);
1063 fprintf(stderr, "Run 'perf list' for a list of valid events\n");
1064 }
1065 return ret;
1066 }
1067
1068 int parse_filter(const struct option *opt, const char *str,
1069 int unset __maybe_unused)
1070 {
1071 struct perf_evlist *evlist = *(struct perf_evlist **)opt->value;
1072 struct perf_evsel *last = NULL;
1073
1074 if (evlist->nr_entries > 0)
1075 last = perf_evlist__last(evlist);
1076
1077 if (last == NULL || last->attr.type != PERF_TYPE_TRACEPOINT) {
1078 fprintf(stderr,
1079 "--filter option should follow a -e tracepoint option\n");
1080 return -1;
1081 }
1082
1083 last->filter = strdup(str);
1084 if (last->filter == NULL) {
1085 fprintf(stderr, "not enough memory to hold filter string\n");
1086 return -1;
1087 }
1088
1089 return 0;
1090 }
1091
1092 static const char * const event_type_descriptors[] = {
1093 "Hardware event",
1094 "Software event",
1095 "Tracepoint event",
1096 "Hardware cache event",
1097 "Raw hardware event descriptor",
1098 "Hardware breakpoint",
1099 };
1100
1101 /*
1102 * Print the events from <debugfs_mount_point>/tracing/events
1103 */
1104
1105 void print_tracepoint_events(const char *subsys_glob, const char *event_glob,
1106 bool name_only)
1107 {
1108 DIR *sys_dir, *evt_dir;
1109 struct dirent *sys_next, *evt_next, sys_dirent, evt_dirent;
1110 char evt_path[MAXPATHLEN];
1111 char dir_path[MAXPATHLEN];
1112 char sbuf[STRERR_BUFSIZE];
1113
1114 if (debugfs_valid_mountpoint(tracing_events_path)) {
1115 printf(" [ Tracepoints not available: %s ]\n",
1116 strerror_r(errno, sbuf, sizeof(sbuf)));
1117 return;
1118 }
1119
1120 sys_dir = opendir(tracing_events_path);
1121 if (!sys_dir)
1122 return;
1123
1124 for_each_subsystem(sys_dir, sys_dirent, sys_next) {
1125 if (subsys_glob != NULL &&
1126 !strglobmatch(sys_dirent.d_name, subsys_glob))
1127 continue;
1128
1129 snprintf(dir_path, MAXPATHLEN, "%s/%s", tracing_events_path,
1130 sys_dirent.d_name);
1131 evt_dir = opendir(dir_path);
1132 if (!evt_dir)
1133 continue;
1134
1135 for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next) {
1136 if (event_glob != NULL &&
1137 !strglobmatch(evt_dirent.d_name, event_glob))
1138 continue;
1139
1140 if (name_only) {
1141 printf("%s:%s ", sys_dirent.d_name, evt_dirent.d_name);
1142 continue;
1143 }
1144
1145 snprintf(evt_path, MAXPATHLEN, "%s:%s",
1146 sys_dirent.d_name, evt_dirent.d_name);
1147 printf(" %-50s [%s]\n", evt_path,
1148 event_type_descriptors[PERF_TYPE_TRACEPOINT]);
1149 }
1150 closedir(evt_dir);
1151 }
1152 closedir(sys_dir);
1153 }
1154
1155 /*
1156 * Check whether event is in <debugfs_mount_point>/tracing/events
1157 */
1158
1159 int is_valid_tracepoint(const char *event_string)
1160 {
1161 DIR *sys_dir, *evt_dir;
1162 struct dirent *sys_next, *evt_next, sys_dirent, evt_dirent;
1163 char evt_path[MAXPATHLEN];
1164 char dir_path[MAXPATHLEN];
1165
1166 if (debugfs_valid_mountpoint(tracing_events_path))
1167 return 0;
1168
1169 sys_dir = opendir(tracing_events_path);
1170 if (!sys_dir)
1171 return 0;
1172
1173 for_each_subsystem(sys_dir, sys_dirent, sys_next) {
1174
1175 snprintf(dir_path, MAXPATHLEN, "%s/%s", tracing_events_path,
1176 sys_dirent.d_name);
1177 evt_dir = opendir(dir_path);
1178 if (!evt_dir)
1179 continue;
1180
1181 for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next) {
1182 snprintf(evt_path, MAXPATHLEN, "%s:%s",
1183 sys_dirent.d_name, evt_dirent.d_name);
1184 if (!strcmp(evt_path, event_string)) {
1185 closedir(evt_dir);
1186 closedir(sys_dir);
1187 return 1;
1188 }
1189 }
1190 closedir(evt_dir);
1191 }
1192 closedir(sys_dir);
1193 return 0;
1194 }
1195
1196 static bool is_event_supported(u8 type, unsigned config)
1197 {
1198 bool ret = true;
1199 int open_return;
1200 struct perf_evsel *evsel;
1201 struct perf_event_attr attr = {
1202 .type = type,
1203 .config = config,
1204 .disabled = 1,
1205 };
1206 struct {
1207 struct thread_map map;
1208 int threads[1];
1209 } tmap = {
1210 .map.nr = 1,
1211 .threads = { 0 },
1212 };
1213
1214 evsel = perf_evsel__new(&attr);
1215 if (evsel) {
1216 open_return = perf_evsel__open(evsel, NULL, &tmap.map);
1217 ret = open_return >= 0;
1218
1219 if (open_return == -EACCES) {
1220 /*
1221 * This happens if the paranoid value
1222 * /proc/sys/kernel/perf_event_paranoid is set to 2
1223 * Re-run with exclude_kernel set; we don't do that
1224 * by default as some ARM machines do not support it.
1225 *
1226 */
1227 evsel->attr.exclude_kernel = 1;
1228 ret = perf_evsel__open(evsel, NULL, &tmap.map) >= 0;
1229 }
1230 perf_evsel__delete(evsel);
1231 }
1232
1233 return ret;
1234 }
1235
1236 static void __print_events_type(u8 type, struct event_symbol *syms,
1237 unsigned max)
1238 {
1239 char name[64];
1240 unsigned i;
1241
1242 for (i = 0; i < max ; i++, syms++) {
1243 if (!is_event_supported(type, i))
1244 continue;
1245
1246 if (strlen(syms->alias))
1247 snprintf(name, sizeof(name), "%s OR %s",
1248 syms->symbol, syms->alias);
1249 else
1250 snprintf(name, sizeof(name), "%s", syms->symbol);
1251
1252 printf(" %-50s [%s]\n", name, event_type_descriptors[type]);
1253 }
1254 }
1255
1256 void print_events_type(u8 type)
1257 {
1258 if (type == PERF_TYPE_SOFTWARE)
1259 __print_events_type(type, event_symbols_sw, PERF_COUNT_SW_MAX);
1260 else
1261 __print_events_type(type, event_symbols_hw, PERF_COUNT_HW_MAX);
1262 }
1263
1264 int print_hwcache_events(const char *event_glob, bool name_only)
1265 {
1266 unsigned int type, op, i, printed = 0;
1267 char name[64];
1268
1269 for (type = 0; type < PERF_COUNT_HW_CACHE_MAX; type++) {
1270 for (op = 0; op < PERF_COUNT_HW_CACHE_OP_MAX; op++) {
1271 /* skip invalid cache type */
1272 if (!perf_evsel__is_cache_op_valid(type, op))
1273 continue;
1274
1275 for (i = 0; i < PERF_COUNT_HW_CACHE_RESULT_MAX; i++) {
1276 __perf_evsel__hw_cache_type_op_res_name(type, op, i,
1277 name, sizeof(name));
1278 if (event_glob != NULL && !strglobmatch(name, event_glob))
1279 continue;
1280
1281 if (!is_event_supported(PERF_TYPE_HW_CACHE,
1282 type | (op << 8) | (i << 16)))
1283 continue;
1284
1285 if (name_only)
1286 printf("%s ", name);
1287 else
1288 printf(" %-50s [%s]\n", name,
1289 event_type_descriptors[PERF_TYPE_HW_CACHE]);
1290 ++printed;
1291 }
1292 }
1293 }
1294
1295 if (printed)
1296 printf("\n");
1297 return printed;
1298 }
1299
1300 static void print_symbol_events(const char *event_glob, unsigned type,
1301 struct event_symbol *syms, unsigned max,
1302 bool name_only)
1303 {
1304 unsigned i, printed = 0;
1305 char name[MAX_NAME_LEN];
1306
1307 for (i = 0; i < max; i++, syms++) {
1308
1309 if (event_glob != NULL &&
1310 !(strglobmatch(syms->symbol, event_glob) ||
1311 (syms->alias && strglobmatch(syms->alias, event_glob))))
1312 continue;
1313
1314 if (!is_event_supported(type, i))
1315 continue;
1316
1317 if (name_only) {
1318 printf("%s ", syms->symbol);
1319 continue;
1320 }
1321
1322 if (strlen(syms->alias))
1323 snprintf(name, MAX_NAME_LEN, "%s OR %s", syms->symbol, syms->alias);
1324 else
1325 strncpy(name, syms->symbol, MAX_NAME_LEN);
1326
1327 printf(" %-50s [%s]\n", name, event_type_descriptors[type]);
1328
1329 printed++;
1330 }
1331
1332 if (printed)
1333 printf("\n");
1334 }
1335
1336 /*
1337 * Print the help text for the event symbols:
1338 */
1339 void print_events(const char *event_glob, bool name_only)
1340 {
1341 if (!name_only) {
1342 printf("\n");
1343 printf("List of pre-defined events (to be used in -e):\n");
1344 }
1345
1346 print_symbol_events(event_glob, PERF_TYPE_HARDWARE,
1347 event_symbols_hw, PERF_COUNT_HW_MAX, name_only);
1348
1349 print_symbol_events(event_glob, PERF_TYPE_SOFTWARE,
1350 event_symbols_sw, PERF_COUNT_SW_MAX, name_only);
1351
1352 print_hwcache_events(event_glob, name_only);
1353
1354 print_pmu_events(event_glob, name_only);
1355
1356 if (event_glob != NULL)
1357 return;
1358
1359 if (!name_only) {
1360 printf(" %-50s [%s]\n",
1361 "rNNN",
1362 event_type_descriptors[PERF_TYPE_RAW]);
1363 printf(" %-50s [%s]\n",
1364 "cpu/t1=v1[,t2=v2,t3 ...]/modifier",
1365 event_type_descriptors[PERF_TYPE_RAW]);
1366 printf(" (see 'man perf-list' on how to encode it)\n");
1367 printf("\n");
1368
1369 printf(" %-50s [%s]\n",
1370 "mem:<addr>[/len][:access]",
1371 event_type_descriptors[PERF_TYPE_BREAKPOINT]);
1372 printf("\n");
1373 }
1374
1375 print_tracepoint_events(NULL, NULL, name_only);
1376 }
1377
1378 int parse_events__is_hardcoded_term(struct parse_events_term *term)
1379 {
1380 return term->type_term != PARSE_EVENTS__TERM_TYPE_USER;
1381 }
1382
1383 static int new_term(struct parse_events_term **_term, int type_val,
1384 int type_term, char *config,
1385 char *str, u64 num)
1386 {
1387 struct parse_events_term *term;
1388
1389 term = zalloc(sizeof(*term));
1390 if (!term)
1391 return -ENOMEM;
1392
1393 INIT_LIST_HEAD(&term->list);
1394 term->type_val = type_val;
1395 term->type_term = type_term;
1396 term->config = config;
1397
1398 switch (type_val) {
1399 case PARSE_EVENTS__TERM_TYPE_NUM:
1400 term->val.num = num;
1401 break;
1402 case PARSE_EVENTS__TERM_TYPE_STR:
1403 term->val.str = str;
1404 break;
1405 default:
1406 free(term);
1407 return -EINVAL;
1408 }
1409
1410 *_term = term;
1411 return 0;
1412 }
1413
1414 int parse_events_term__num(struct parse_events_term **term,
1415 int type_term, char *config, u64 num)
1416 {
1417 return new_term(term, PARSE_EVENTS__TERM_TYPE_NUM, type_term,
1418 config, NULL, num);
1419 }
1420
1421 int parse_events_term__str(struct parse_events_term **term,
1422 int type_term, char *config, char *str)
1423 {
1424 return new_term(term, PARSE_EVENTS__TERM_TYPE_STR, type_term,
1425 config, str, 0);
1426 }
1427
1428 int parse_events_term__sym_hw(struct parse_events_term **term,
1429 char *config, unsigned idx)
1430 {
1431 struct event_symbol *sym;
1432
1433 BUG_ON(idx >= PERF_COUNT_HW_MAX);
1434 sym = &event_symbols_hw[idx];
1435
1436 if (config)
1437 return new_term(term, PARSE_EVENTS__TERM_TYPE_STR,
1438 PARSE_EVENTS__TERM_TYPE_USER, config,
1439 (char *) sym->symbol, 0);
1440 else
1441 return new_term(term, PARSE_EVENTS__TERM_TYPE_STR,
1442 PARSE_EVENTS__TERM_TYPE_USER,
1443 (char *) "event", (char *) sym->symbol, 0);
1444 }
1445
1446 int parse_events_term__clone(struct parse_events_term **new,
1447 struct parse_events_term *term)
1448 {
1449 return new_term(new, term->type_val, term->type_term, term->config,
1450 term->val.str, term->val.num);
1451 }
1452
1453 void parse_events__free_terms(struct list_head *terms)
1454 {
1455 struct parse_events_term *term, *h;
1456
1457 list_for_each_entry_safe(term, h, terms, list)
1458 free(term);
1459 }