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