1 #include <linux/hw_breakpoint.h>
6 #include "parse-options.h"
7 #include "parse-events.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"
19 #include "thread_map.h"
23 #define MAX_NAME_LEN 100
26 extern int parse_events_debug;
28 int parse_events_parse(void *data, void *scanner);
30 static struct perf_pmu_event_symbol *perf_pmu_events_list;
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
37 static int perf_pmu_events_list_num;
39 struct event_symbol event_symbols_hw[PERF_COUNT_HW_MAX] = {
40 [PERF_COUNT_HW_CPU_CYCLES] = {
41 .symbol = "cpu-cycles",
44 [PERF_COUNT_HW_INSTRUCTIONS] = {
45 .symbol = "instructions",
48 [PERF_COUNT_HW_CACHE_REFERENCES] = {
49 .symbol = "cache-references",
52 [PERF_COUNT_HW_CACHE_MISSES] = {
53 .symbol = "cache-misses",
56 [PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = {
57 .symbol = "branch-instructions",
60 [PERF_COUNT_HW_BRANCH_MISSES] = {
61 .symbol = "branch-misses",
64 [PERF_COUNT_HW_BUS_CYCLES] = {
65 .symbol = "bus-cycles",
68 [PERF_COUNT_HW_STALLED_CYCLES_FRONTEND] = {
69 .symbol = "stalled-cycles-frontend",
70 .alias = "idle-cycles-frontend",
72 [PERF_COUNT_HW_STALLED_CYCLES_BACKEND] = {
73 .symbol = "stalled-cycles-backend",
74 .alias = "idle-cycles-backend",
76 [PERF_COUNT_HW_REF_CPU_CYCLES] = {
77 .symbol = "ref-cycles",
82 struct event_symbol event_symbols_sw[PERF_COUNT_SW_MAX] = {
83 [PERF_COUNT_SW_CPU_CLOCK] = {
84 .symbol = "cpu-clock",
87 [PERF_COUNT_SW_TASK_CLOCK] = {
88 .symbol = "task-clock",
91 [PERF_COUNT_SW_PAGE_FAULTS] = {
92 .symbol = "page-faults",
95 [PERF_COUNT_SW_CONTEXT_SWITCHES] = {
96 .symbol = "context-switches",
99 [PERF_COUNT_SW_CPU_MIGRATIONS] = {
100 .symbol = "cpu-migrations",
101 .alias = "migrations",
103 [PERF_COUNT_SW_PAGE_FAULTS_MIN] = {
104 .symbol = "minor-faults",
107 [PERF_COUNT_SW_PAGE_FAULTS_MAJ] = {
108 .symbol = "major-faults",
111 [PERF_COUNT_SW_ALIGNMENT_FAULTS] = {
112 .symbol = "alignment-faults",
115 [PERF_COUNT_SW_EMULATION_FAULTS] = {
116 .symbol = "emulation-faults",
119 [PERF_COUNT_SW_DUMMY] = {
125 #define __PERF_EVENT_FIELD(config, name) \
126 ((config & PERF_EVENT_##name##_MASK) >> PERF_EVENT_##name##_SHIFT)
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)
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, "..")))
139 static int tp_event_has_id(struct dirent *sys_dir, struct dirent *evt_dir)
141 char evt_path[MAXPATHLEN];
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);
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)))
161 #define MAX_EVENT_LENGTH 512
164 struct tracepoint_path *tracepoint_id_to_path(u64 config)
166 struct tracepoint_path *path = NULL;
167 DIR *sys_dir, *evt_dir;
168 struct dirent *sys_next, *evt_next, sys_dirent, evt_dirent;
172 char evt_path[MAXPATHLEN];
173 char dir_path[MAXPATHLEN];
175 sys_dir = opendir(tracing_events_path);
179 for_each_subsystem(sys_dir, sys_dirent, sys_next) {
181 snprintf(dir_path, MAXPATHLEN, "%s/%s", tracing_events_path,
183 evt_dir = opendir(dir_path);
187 for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next) {
189 snprintf(evt_path, MAXPATHLEN, "%s/%s/id", dir_path,
191 fd = open(evt_path, O_RDONLY);
194 if (read(fd, id_buf, sizeof(id_buf)) < 0) {
203 path = zalloc(sizeof(*path));
204 path->system = malloc(MAX_EVENT_LENGTH);
209 path->name = malloc(MAX_EVENT_LENGTH);
211 zfree(&path->system);
215 strncpy(path->system, sys_dirent.d_name,
217 strncpy(path->name, evt_dirent.d_name,
229 struct tracepoint_path *tracepoint_name_to_path(const char *name)
231 struct tracepoint_path *path = zalloc(sizeof(*path));
232 char *str = strchr(name, ':');
234 if (path == NULL || str == NULL) {
239 path->system = strndup(name, str - name);
240 path->name = strdup(str+1);
242 if (path->system == NULL || path->name == NULL) {
243 zfree(&path->system);
252 const char *event_type(int type)
255 case PERF_TYPE_HARDWARE:
258 case PERF_TYPE_SOFTWARE:
261 case PERF_TYPE_TRACEPOINT:
264 case PERF_TYPE_HW_CACHE:
265 return "hardware-cache";
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)
282 struct perf_evsel *evsel;
284 event_attr_init(attr);
286 evsel = perf_evsel__new_idx(attr, (*idx)++);
291 evsel->cpus = cpu_map__get(cpus);
294 evsel->name = strdup(name);
297 list_splice(config_terms, &evsel->config_terms);
299 list_add_tail(&evsel->node, list);
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)
307 return __add_event(list, idx, attr, name, NULL, config_terms) ? 0 : -ENOMEM;
310 static int parse_aliases(char *str, const char *names[][PERF_EVSEL__MAX_ALIASES], int size)
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))
328 int parse_events_add_cache(struct list_head *list, int *idx,
329 char *type, char *op_result1, char *op_result2)
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 };
338 * No fallback - if we cannot get a clear cache type
341 cache_type = parse_aliases(type, perf_evsel__hw_cache,
342 PERF_COUNT_HW_CACHE_MAX);
343 if (cache_type == -1)
346 n = snprintf(name, MAX_NAME_LEN, "%s", type);
348 for (i = 0; (i < 2) && (op_result[i]); i++) {
349 char *str = op_result[i];
351 n += snprintf(name + n, MAX_NAME_LEN - n, "-%s", str);
353 if (cache_op == -1) {
354 cache_op = parse_aliases(str, perf_evsel__hw_cache_op,
355 PERF_COUNT_HW_CACHE_OP_MAX);
357 if (!perf_evsel__is_cache_op_valid(cache_type, cache_op))
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)
372 * Fall back to reads:
375 cache_op = PERF_COUNT_HW_CACHE_OP_READ;
378 * Fall back to accesses:
380 if (cache_result == -1)
381 cache_result = PERF_COUNT_HW_CACHE_RESULT_ACCESS;
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);
389 static int add_tracepoint(struct list_head *list, int *idx,
390 char *sys_name, char *evt_name)
392 struct perf_evsel *evsel;
394 evsel = perf_evsel__newtp_idx(sys_name, evt_name, (*idx)++);
398 list_add_tail(&evsel->node, list);
403 static int add_tracepoint_multi_event(struct list_head *list, int *idx,
404 char *sys_name, char *evt_name)
406 char evt_path[MAXPATHLEN];
407 struct dirent *evt_ent;
411 snprintf(evt_path, MAXPATHLEN, "%s/%s", tracing_events_path, sys_name);
412 evt_dir = opendir(evt_path);
414 perror("Can't open event dir");
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"))
425 if (!strglobmatch(evt_ent->d_name, evt_name))
428 ret = add_tracepoint(list, idx, sys_name, evt_ent->d_name);
435 static int add_tracepoint_event(struct list_head *list, int *idx,
436 char *sys_name, char *evt_name)
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);
443 static int add_tracepoint_multi_sys(struct list_head *list, int *idx,
444 char *sys_name, char *evt_name)
446 struct dirent *events_ent;
450 events_dir = opendir(tracing_events_path);
452 perror("Can't open event dir");
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"))
464 if (!strglobmatch(events_ent->d_name, sys_name))
467 ret = add_tracepoint_event(list, idx, events_ent->d_name,
471 closedir(events_dir);
475 int parse_events_add_tracepoint(struct list_head *list, int *idx,
476 char *sys, char *event)
478 if (strpbrk(sys, "*?"))
479 return add_tracepoint_multi_sys(list, idx, sys, event);
481 return add_tracepoint_event(list, idx, sys, event);
485 parse_breakpoint_type(const char *type, struct perf_event_attr *attr)
489 for (i = 0; i < 3; i++) {
490 if (!type || !type[i])
493 #define CHECK_SET_TYPE(bit) \
495 if (attr->bp_type & bit) \
498 attr->bp_type |= bit; \
503 CHECK_SET_TYPE(HW_BREAKPOINT_R);
506 CHECK_SET_TYPE(HW_BREAKPOINT_W);
509 CHECK_SET_TYPE(HW_BREAKPOINT_X);
516 #undef CHECK_SET_TYPE
518 if (!attr->bp_type) /* Default */
519 attr->bp_type = HW_BREAKPOINT_R | HW_BREAKPOINT_W;
524 int parse_events_add_breakpoint(struct list_head *list, int *idx,
525 void *ptr, char *type, u64 len)
527 struct perf_event_attr attr;
529 memset(&attr, 0, sizeof(attr));
530 attr.bp_addr = (unsigned long) ptr;
532 if (parse_breakpoint_type(type, &attr))
535 /* Provide some defaults if len is not specified */
537 if (attr.bp_type == HW_BREAKPOINT_X)
540 len = HW_BREAKPOINT_LEN_4;
545 attr.type = PERF_TYPE_BREAKPOINT;
546 attr.sample_period = 1;
548 return add_event(list, idx, &attr, NULL, NULL);
551 static int check_type_val(struct parse_events_term *term,
552 struct parse_events_error *err,
555 if (type == term->type_val)
559 err->idx = term->err_val;
560 if (type == PARSE_EVENTS__TERM_TYPE_NUM)
561 err->str = strdup("expected numeric value");
563 err->str = strdup("expected string value");
568 static int config_term(struct perf_event_attr *attr,
569 struct parse_events_term *term,
570 struct parse_events_error *err)
572 #define CHECK_TYPE_VAL(type) \
574 if (check_type_val(term, err, PARSE_EVENTS__TERM_TYPE_ ## type)) \
578 switch (term->type_term) {
579 case PARSE_EVENTS__TERM_TYPE_USER:
581 * Always succeed for sysfs terms, as we dont know
582 * at this point what type they need to have.
585 case PARSE_EVENTS__TERM_TYPE_CONFIG:
587 attr->config = term->val.num;
589 case PARSE_EVENTS__TERM_TYPE_CONFIG1:
591 attr->config1 = term->val.num;
593 case PARSE_EVENTS__TERM_TYPE_CONFIG2:
595 attr->config2 = term->val.num;
597 case PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD:
600 case PARSE_EVENTS__TERM_TYPE_SAMPLE_FREQ:
603 case PARSE_EVENTS__TERM_TYPE_BRANCH_SAMPLE_TYPE:
605 * TODO uncomment when the field is available
606 * attr->branch_sample_type = term->val.num;
609 case PARSE_EVENTS__TERM_TYPE_TIME:
611 if (term->val.num > 1) {
612 err->str = strdup("expected 0 or 1");
613 err->idx = term->err_val;
617 case PARSE_EVENTS__TERM_TYPE_CALLGRAPH:
620 case PARSE_EVENTS__TERM_TYPE_STACKSIZE:
623 case PARSE_EVENTS__TERM_TYPE_NAME:
631 #undef CHECK_TYPE_VAL
634 static int config_attr(struct perf_event_attr *attr,
635 struct list_head *head,
636 struct parse_events_error *err)
638 struct parse_events_term *term;
640 list_for_each_entry(term, head, list)
641 if (config_term(attr, term, err))
647 static int get_config_terms(struct list_head *head_config,
648 struct list_head *head_terms __maybe_unused)
650 #define ADD_CONFIG_TERM(__type, __name, __val) \
652 struct perf_evsel_config_term *__t; \
654 __t = zalloc(sizeof(*__t)); \
658 INIT_LIST_HEAD(&__t->list); \
659 __t->type = PERF_EVSEL__CONFIG_TERM_ ## __type; \
660 __t->val.__name = __val; \
661 list_add_tail(&__t->list, head_terms); \
664 struct parse_events_term *term;
666 list_for_each_entry(term, head_config, list) {
667 switch (term->type_term) {
668 case PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD:
669 ADD_CONFIG_TERM(PERIOD, period, term->val.num);
671 case PARSE_EVENTS__TERM_TYPE_SAMPLE_FREQ:
672 ADD_CONFIG_TERM(FREQ, freq, term->val.num);
674 case PARSE_EVENTS__TERM_TYPE_TIME:
675 ADD_CONFIG_TERM(TIME, time, term->val.num);
677 case PARSE_EVENTS__TERM_TYPE_CALLGRAPH:
678 ADD_CONFIG_TERM(CALLGRAPH, callgraph, term->val.str);
680 case PARSE_EVENTS__TERM_TYPE_STACKSIZE:
681 ADD_CONFIG_TERM(STACK_USER, stack_user, term->val.num);
687 #undef ADD_EVSEL_CONFIG
691 int parse_events_add_numeric(struct parse_events_evlist *data,
692 struct list_head *list,
693 u32 type, u64 config,
694 struct list_head *head_config)
696 struct perf_event_attr attr;
697 LIST_HEAD(config_terms);
699 memset(&attr, 0, sizeof(attr));
701 attr.config = config;
704 if (config_attr(&attr, head_config, data->error))
707 if (get_config_terms(head_config, &config_terms))
711 return add_event(list, &data->idx, &attr, NULL, &config_terms);
714 static int parse_events__is_name_term(struct parse_events_term *term)
716 return term->type_term == PARSE_EVENTS__TERM_TYPE_NAME;
719 static char *pmu_event_name(struct list_head *head_terms)
721 struct parse_events_term *term;
723 list_for_each_entry(term, head_terms, list)
724 if (parse_events__is_name_term(term))
725 return term->val.str;
730 int parse_events_add_pmu(struct parse_events_evlist *data,
731 struct list_head *list, char *name,
732 struct list_head *head_config)
734 struct perf_event_attr attr;
735 struct perf_pmu_info info;
736 struct perf_pmu *pmu;
737 struct perf_evsel *evsel;
738 LIST_HEAD(config_terms);
740 pmu = perf_pmu__find(name);
744 if (pmu->default_config) {
745 memcpy(&attr, pmu->default_config,
746 sizeof(struct perf_event_attr));
748 memset(&attr, 0, sizeof(attr));
752 attr.type = pmu->type;
753 evsel = __add_event(list, &data->idx, &attr, NULL, pmu->cpus, NULL);
754 return evsel ? 0 : -ENOMEM;
757 if (perf_pmu__check_alias(pmu, head_config, &info))
761 * Configure hardcoded terms first, no need to check
762 * return value when called with fail == 0 ;)
764 if (config_attr(&attr, head_config, data->error))
767 if (get_config_terms(head_config, &config_terms))
770 if (perf_pmu__config(pmu, &attr, head_config, data->error))
773 evsel = __add_event(list, &data->idx, &attr,
774 pmu_event_name(head_config), pmu->cpus,
777 evsel->unit = info.unit;
778 evsel->scale = info.scale;
779 evsel->per_pkg = info.per_pkg;
780 evsel->snapshot = info.snapshot;
783 return evsel ? 0 : -ENOMEM;
786 int parse_events__modifier_group(struct list_head *list,
789 return parse_events__modifier_event(list, event_mod, true);
792 void parse_events__set_leader(char *name, struct list_head *list)
794 struct perf_evsel *leader;
796 __perf_evlist__set_leader(list);
797 leader = list_entry(list->next, struct perf_evsel, node);
798 leader->group_name = name ? strdup(name) : NULL;
801 /* list_event is assumed to point to malloc'ed memory */
802 void parse_events_update_lists(struct list_head *list_event,
803 struct list_head *list_all)
806 * Called for single event definition. Update the
807 * 'all event' list, and reinit the 'single event'
808 * list, for next event definition.
810 list_splice_tail(list_event, list_all);
814 struct event_modifier {
827 static int get_event_modifier(struct event_modifier *mod, char *str,
828 struct perf_evsel *evsel)
830 int eu = evsel ? evsel->attr.exclude_user : 0;
831 int ek = evsel ? evsel->attr.exclude_kernel : 0;
832 int eh = evsel ? evsel->attr.exclude_hv : 0;
833 int eH = evsel ? evsel->attr.exclude_host : 0;
834 int eG = evsel ? evsel->attr.exclude_guest : 0;
835 int eI = evsel ? evsel->attr.exclude_idle : 0;
836 int precise = evsel ? evsel->attr.precise_ip : 0;
838 int pinned = evsel ? evsel->attr.pinned : 0;
840 int exclude = eu | ek | eh;
841 int exclude_GH = evsel ? evsel->exclude_GH : 0;
843 memset(mod, 0, sizeof(*mod));
848 exclude = eu = ek = eh = 1;
850 } else if (*str == 'k') {
852 exclude = eu = ek = eh = 1;
854 } else if (*str == 'h') {
856 exclude = eu = ek = eh = 1;
858 } else if (*str == 'G') {
860 exclude_GH = eG = eH = 1;
862 } else if (*str == 'H') {
864 exclude_GH = eG = eH = 1;
866 } else if (*str == 'I') {
868 } else if (*str == 'p') {
870 /* use of precise requires exclude_guest */
873 } else if (*str == 'S') {
875 } else if (*str == 'D') {
886 * 0 - SAMPLE_IP can have arbitrary skid
887 * 1 - SAMPLE_IP must have constant skid
888 * 2 - SAMPLE_IP requested to have 0 skid
889 * 3 - SAMPLE_IP must have 0 skid
891 * See also PERF_RECORD_MISC_EXACT_IP
902 mod->precise = precise;
903 mod->exclude_GH = exclude_GH;
904 mod->sample_read = sample_read;
905 mod->pinned = pinned;
911 * Basic modifier sanity check to validate it contains only one
912 * instance of any modifier (apart from 'p') present.
914 static int check_modifier(char *str)
918 /* The sizeof includes 0 byte as well. */
919 if (strlen(str) > (sizeof("ukhGHpppSDI") - 1))
923 if (*p != 'p' && strchr(p + 1, *p))
931 int parse_events__modifier_event(struct list_head *list, char *str, bool add)
933 struct perf_evsel *evsel;
934 struct event_modifier mod;
939 if (check_modifier(str))
942 if (!add && get_event_modifier(&mod, str, NULL))
945 __evlist__for_each(list, evsel) {
946 if (add && get_event_modifier(&mod, str, evsel))
949 evsel->attr.exclude_user = mod.eu;
950 evsel->attr.exclude_kernel = mod.ek;
951 evsel->attr.exclude_hv = mod.eh;
952 evsel->attr.precise_ip = mod.precise;
953 evsel->attr.exclude_host = mod.eH;
954 evsel->attr.exclude_guest = mod.eG;
955 evsel->attr.exclude_idle = mod.eI;
956 evsel->exclude_GH = mod.exclude_GH;
957 evsel->sample_read = mod.sample_read;
959 if (perf_evsel__is_group_leader(evsel))
960 evsel->attr.pinned = mod.pinned;
966 int parse_events_name(struct list_head *list, char *name)
968 struct perf_evsel *evsel;
970 __evlist__for_each(list, evsel) {
972 evsel->name = strdup(name);
979 comp_pmu(const void *p1, const void *p2)
981 struct perf_pmu_event_symbol *pmu1 = (struct perf_pmu_event_symbol *) p1;
982 struct perf_pmu_event_symbol *pmu2 = (struct perf_pmu_event_symbol *) p2;
984 return strcmp(pmu1->symbol, pmu2->symbol);
987 static void perf_pmu__parse_cleanup(void)
989 if (perf_pmu_events_list_num > 0) {
990 struct perf_pmu_event_symbol *p;
993 for (i = 0; i < perf_pmu_events_list_num; i++) {
994 p = perf_pmu_events_list + i;
997 free(perf_pmu_events_list);
998 perf_pmu_events_list = NULL;
999 perf_pmu_events_list_num = 0;
1003 #define SET_SYMBOL(str, stype) \
1012 * Read the pmu events list from sysfs
1013 * Save it into perf_pmu_events_list
1015 static void perf_pmu__parse_init(void)
1018 struct perf_pmu *pmu = NULL;
1019 struct perf_pmu_alias *alias;
1022 pmu = perf_pmu__find("cpu");
1023 if ((pmu == NULL) || list_empty(&pmu->aliases)) {
1024 perf_pmu_events_list_num = -1;
1027 list_for_each_entry(alias, &pmu->aliases, list) {
1028 if (strchr(alias->name, '-'))
1032 perf_pmu_events_list = malloc(sizeof(struct perf_pmu_event_symbol) * len);
1033 if (!perf_pmu_events_list)
1035 perf_pmu_events_list_num = len;
1038 list_for_each_entry(alias, &pmu->aliases, list) {
1039 struct perf_pmu_event_symbol *p = perf_pmu_events_list + len;
1040 char *tmp = strchr(alias->name, '-');
1043 SET_SYMBOL(strndup(alias->name, tmp - alias->name),
1044 PMU_EVENT_SYMBOL_PREFIX);
1046 SET_SYMBOL(strdup(++tmp), PMU_EVENT_SYMBOL_SUFFIX);
1049 SET_SYMBOL(strdup(alias->name), PMU_EVENT_SYMBOL);
1053 qsort(perf_pmu_events_list, len,
1054 sizeof(struct perf_pmu_event_symbol), comp_pmu);
1058 perf_pmu__parse_cleanup();
1061 enum perf_pmu_event_symbol_type
1062 perf_pmu__parse_check(const char *name)
1064 struct perf_pmu_event_symbol p, *r;
1066 /* scan kernel pmu events from sysfs if needed */
1067 if (perf_pmu_events_list_num == 0)
1068 perf_pmu__parse_init();
1070 * name "cpu" could be prefix of cpu-cycles or cpu// events.
1071 * cpu-cycles has been handled by hardcode.
1072 * So it must be cpu// events, not kernel pmu event.
1074 if ((perf_pmu_events_list_num <= 0) || !strcmp(name, "cpu"))
1075 return PMU_EVENT_SYMBOL_ERR;
1077 p.symbol = strdup(name);
1078 r = bsearch(&p, perf_pmu_events_list,
1079 (size_t) perf_pmu_events_list_num,
1080 sizeof(struct perf_pmu_event_symbol), comp_pmu);
1082 return r ? r->type : PMU_EVENT_SYMBOL_ERR;
1085 static int parse_events__scanner(const char *str, void *data, int start_token)
1087 YY_BUFFER_STATE buffer;
1091 ret = parse_events_lex_init_extra(start_token, &scanner);
1095 buffer = parse_events__scan_string(str, scanner);
1098 parse_events_debug = 1;
1100 ret = parse_events_parse(data, scanner);
1102 parse_events__flush_buffer(buffer, scanner);
1103 parse_events__delete_buffer(buffer, scanner);
1104 parse_events_lex_destroy(scanner);
1109 * parse event config string, return a list of event terms.
1111 int parse_events_terms(struct list_head *terms, const char *str)
1113 struct parse_events_terms data = {
1118 ret = parse_events__scanner(str, &data, PE_START_TERMS);
1120 list_splice(data.terms, terms);
1126 parse_events__free_terms(data.terms);
1130 int parse_events(struct perf_evlist *evlist, const char *str,
1131 struct parse_events_error *err)
1133 struct parse_events_evlist data = {
1134 .list = LIST_HEAD_INIT(data.list),
1135 .idx = evlist->nr_entries,
1140 ret = parse_events__scanner(str, &data, PE_START_EVENTS);
1141 perf_pmu__parse_cleanup();
1143 int entries = data.idx - evlist->nr_entries;
1144 struct perf_evsel *last;
1146 perf_evlist__splice_list_tail(evlist, &data.list, entries);
1147 evlist->nr_groups += data.nr_groups;
1148 last = perf_evlist__last(evlist);
1149 last->cmdline_group_boundary = true;
1155 * There are 2 users - builtin-record and builtin-test objects.
1156 * Both call perf_evlist__delete in case of error, so we dont
1162 #define MAX_WIDTH 1000
1163 static int get_term_width(void)
1167 get_term_dimensions(&ws);
1168 return ws.ws_col > MAX_WIDTH ? MAX_WIDTH : ws.ws_col;
1171 static void parse_events_print_error(struct parse_events_error *err,
1174 const char *str = "invalid or unsupported event: ";
1175 char _buf[MAX_WIDTH];
1176 char *buf = (char *) event;
1180 /* -2 for extra '' in the final fprintf */
1181 int width = get_term_width() - 2;
1182 int len_event = strlen(event);
1183 int len_str, max_len, cut = 0;
1186 * Maximum error index indent, we will cut
1187 * the event string if it's bigger.
1189 int max_err_idx = 13;
1192 * Let's be specific with the message when
1193 * we have the precise error.
1195 str = "event syntax error: ";
1196 len_str = strlen(str);
1197 max_len = width - len_str;
1201 /* We're cutting from the beggining. */
1202 if (err->idx > max_err_idx)
1203 cut = err->idx - max_err_idx;
1205 strncpy(buf, event + cut, max_len);
1207 /* Mark cut parts with '..' on both sides. */
1209 buf[0] = buf[1] = '.';
1211 if ((len_event - cut) > max_len) {
1212 buf[max_len - 1] = buf[max_len - 2] = '.';
1216 idx = len_str + err->idx - cut;
1219 fprintf(stderr, "%s'%s'\n", str, buf);
1221 fprintf(stderr, "%*s\\___ %s\n", idx + 1, "", err->str);
1223 fprintf(stderr, "\n%s\n", err->help);
1228 fprintf(stderr, "Run 'perf list' for a list of valid events\n");
1233 int parse_events_option(const struct option *opt, const char *str,
1234 int unset __maybe_unused)
1236 struct perf_evlist *evlist = *(struct perf_evlist **)opt->value;
1237 struct parse_events_error err = { .idx = 0, };
1238 int ret = parse_events(evlist, str, &err);
1241 parse_events_print_error(&err, str);
1247 foreach_evsel_in_last_glob(struct perf_evlist *evlist,
1248 int (*func)(struct perf_evsel *evsel,
1252 struct perf_evsel *last = NULL;
1255 if (evlist->nr_entries > 0)
1256 last = perf_evlist__last(evlist);
1259 err = (*func)(last, arg);
1265 if (last->node.prev == &evlist->entries)
1267 last = list_entry(last->node.prev, struct perf_evsel, node);
1268 } while (!last->cmdline_group_boundary);
1273 static int set_filter(struct perf_evsel *evsel, const void *arg)
1275 const char *str = arg;
1277 if (evsel == NULL || evsel->attr.type != PERF_TYPE_TRACEPOINT) {
1279 "--filter option should follow a -e tracepoint option\n");
1283 if (perf_evsel__append_filter(evsel, "&&", str) < 0) {
1285 "not enough memory to hold filter string\n");
1292 int parse_filter(const struct option *opt, const char *str,
1293 int unset __maybe_unused)
1295 struct perf_evlist *evlist = *(struct perf_evlist **)opt->value;
1297 return foreach_evsel_in_last_glob(evlist, set_filter,
1301 static int add_exclude_perf_filter(struct perf_evsel *evsel,
1302 const void *arg __maybe_unused)
1304 char new_filter[64];
1306 if (evsel == NULL || evsel->attr.type != PERF_TYPE_TRACEPOINT) {
1308 "--exclude-perf option should follow a -e tracepoint option\n");
1312 snprintf(new_filter, sizeof(new_filter), "common_pid != %d", getpid());
1314 if (perf_evsel__append_filter(evsel, "&&", new_filter) < 0) {
1316 "not enough memory to hold filter string\n");
1323 int exclude_perf(const struct option *opt,
1324 const char *arg __maybe_unused,
1325 int unset __maybe_unused)
1327 struct perf_evlist *evlist = *(struct perf_evlist **)opt->value;
1329 return foreach_evsel_in_last_glob(evlist, add_exclude_perf_filter,
1333 static const char * const event_type_descriptors[] = {
1337 "Hardware cache event",
1338 "Raw hardware event descriptor",
1339 "Hardware breakpoint",
1342 static int cmp_string(const void *a, const void *b)
1344 const char * const *as = a;
1345 const char * const *bs = b;
1347 return strcmp(*as, *bs);
1351 * Print the events from <debugfs_mount_point>/tracing/events
1354 void print_tracepoint_events(const char *subsys_glob, const char *event_glob,
1357 DIR *sys_dir, *evt_dir;
1358 struct dirent *sys_next, *evt_next, sys_dirent, evt_dirent;
1359 char evt_path[MAXPATHLEN];
1360 char dir_path[MAXPATHLEN];
1361 char **evt_list = NULL;
1362 unsigned int evt_i = 0, evt_num = 0;
1363 bool evt_num_known = false;
1366 sys_dir = opendir(tracing_events_path);
1370 if (evt_num_known) {
1371 evt_list = zalloc(sizeof(char *) * evt_num);
1373 goto out_close_sys_dir;
1376 for_each_subsystem(sys_dir, sys_dirent, sys_next) {
1377 if (subsys_glob != NULL &&
1378 !strglobmatch(sys_dirent.d_name, subsys_glob))
1381 snprintf(dir_path, MAXPATHLEN, "%s/%s", tracing_events_path,
1383 evt_dir = opendir(dir_path);
1387 for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next) {
1388 if (event_glob != NULL &&
1389 !strglobmatch(evt_dirent.d_name, event_glob))
1392 if (!evt_num_known) {
1397 snprintf(evt_path, MAXPATHLEN, "%s:%s",
1398 sys_dirent.d_name, evt_dirent.d_name);
1400 evt_list[evt_i] = strdup(evt_path);
1401 if (evt_list[evt_i] == NULL)
1402 goto out_close_evt_dir;
1409 if (!evt_num_known) {
1410 evt_num_known = true;
1413 qsort(evt_list, evt_num, sizeof(char *), cmp_string);
1415 while (evt_i < evt_num) {
1417 printf("%s ", evt_list[evt_i++]);
1420 printf(" %-50s [%s]\n", evt_list[evt_i++],
1421 event_type_descriptors[PERF_TYPE_TRACEPOINT]);
1428 for (evt_i = 0; evt_i < evt_num; evt_i++)
1429 zfree(&evt_list[evt_i]);
1438 printf("FATAL: not enough memory to print %s\n",
1439 event_type_descriptors[PERF_TYPE_TRACEPOINT]);
1445 * Check whether event is in <debugfs_mount_point>/tracing/events
1448 int is_valid_tracepoint(const char *event_string)
1450 DIR *sys_dir, *evt_dir;
1451 struct dirent *sys_next, *evt_next, sys_dirent, evt_dirent;
1452 char evt_path[MAXPATHLEN];
1453 char dir_path[MAXPATHLEN];
1455 sys_dir = opendir(tracing_events_path);
1459 for_each_subsystem(sys_dir, sys_dirent, sys_next) {
1461 snprintf(dir_path, MAXPATHLEN, "%s/%s", tracing_events_path,
1463 evt_dir = opendir(dir_path);
1467 for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next) {
1468 snprintf(evt_path, MAXPATHLEN, "%s:%s",
1469 sys_dirent.d_name, evt_dirent.d_name);
1470 if (!strcmp(evt_path, event_string)) {
1482 static bool is_event_supported(u8 type, unsigned config)
1486 struct perf_evsel *evsel;
1487 struct perf_event_attr attr = {
1493 struct thread_map map;
1500 evsel = perf_evsel__new(&attr);
1502 open_return = perf_evsel__open(evsel, NULL, &tmap.map);
1503 ret = open_return >= 0;
1505 if (open_return == -EACCES) {
1507 * This happens if the paranoid value
1508 * /proc/sys/kernel/perf_event_paranoid is set to 2
1509 * Re-run with exclude_kernel set; we don't do that
1510 * by default as some ARM machines do not support it.
1513 evsel->attr.exclude_kernel = 1;
1514 ret = perf_evsel__open(evsel, NULL, &tmap.map) >= 0;
1516 perf_evsel__delete(evsel);
1522 int print_hwcache_events(const char *event_glob, bool name_only)
1524 unsigned int type, op, i, evt_i = 0, evt_num = 0;
1526 char **evt_list = NULL;
1527 bool evt_num_known = false;
1530 if (evt_num_known) {
1531 evt_list = zalloc(sizeof(char *) * evt_num);
1536 for (type = 0; type < PERF_COUNT_HW_CACHE_MAX; type++) {
1537 for (op = 0; op < PERF_COUNT_HW_CACHE_OP_MAX; op++) {
1538 /* skip invalid cache type */
1539 if (!perf_evsel__is_cache_op_valid(type, op))
1542 for (i = 0; i < PERF_COUNT_HW_CACHE_RESULT_MAX; i++) {
1543 __perf_evsel__hw_cache_type_op_res_name(type, op, i,
1544 name, sizeof(name));
1545 if (event_glob != NULL && !strglobmatch(name, event_glob))
1548 if (!is_event_supported(PERF_TYPE_HW_CACHE,
1549 type | (op << 8) | (i << 16)))
1552 if (!evt_num_known) {
1557 evt_list[evt_i] = strdup(name);
1558 if (evt_list[evt_i] == NULL)
1565 if (!evt_num_known) {
1566 evt_num_known = true;
1569 qsort(evt_list, evt_num, sizeof(char *), cmp_string);
1571 while (evt_i < evt_num) {
1573 printf("%s ", evt_list[evt_i++]);
1576 printf(" %-50s [%s]\n", evt_list[evt_i++],
1577 event_type_descriptors[PERF_TYPE_HW_CACHE]);
1584 for (evt_i = 0; evt_i < evt_num; evt_i++)
1585 zfree(&evt_list[evt_i]);
1590 printf("FATAL: not enough memory to print %s\n", event_type_descriptors[PERF_TYPE_HW_CACHE]);
1596 void print_symbol_events(const char *event_glob, unsigned type,
1597 struct event_symbol *syms, unsigned max,
1600 unsigned int i, evt_i = 0, evt_num = 0;
1601 char name[MAX_NAME_LEN];
1602 char **evt_list = NULL;
1603 bool evt_num_known = false;
1606 if (evt_num_known) {
1607 evt_list = zalloc(sizeof(char *) * evt_num);
1613 for (i = 0; i < max; i++, syms++) {
1615 if (event_glob != NULL &&
1616 !(strglobmatch(syms->symbol, event_glob) ||
1617 (syms->alias && strglobmatch(syms->alias, event_glob))))
1620 if (!is_event_supported(type, i))
1623 if (!evt_num_known) {
1628 if (!name_only && strlen(syms->alias))
1629 snprintf(name, MAX_NAME_LEN, "%s OR %s", syms->symbol, syms->alias);
1631 strncpy(name, syms->symbol, MAX_NAME_LEN);
1633 evt_list[evt_i] = strdup(name);
1634 if (evt_list[evt_i] == NULL)
1639 if (!evt_num_known) {
1640 evt_num_known = true;
1643 qsort(evt_list, evt_num, sizeof(char *), cmp_string);
1645 while (evt_i < evt_num) {
1647 printf("%s ", evt_list[evt_i++]);
1650 printf(" %-50s [%s]\n", evt_list[evt_i++], event_type_descriptors[type]);
1657 for (evt_i = 0; evt_i < evt_num; evt_i++)
1658 zfree(&evt_list[evt_i]);
1663 printf("FATAL: not enough memory to print %s\n", event_type_descriptors[type]);
1669 * Print the help text for the event symbols:
1671 void print_events(const char *event_glob, bool name_only)
1673 print_symbol_events(event_glob, PERF_TYPE_HARDWARE,
1674 event_symbols_hw, PERF_COUNT_HW_MAX, name_only);
1676 print_symbol_events(event_glob, PERF_TYPE_SOFTWARE,
1677 event_symbols_sw, PERF_COUNT_SW_MAX, name_only);
1679 print_hwcache_events(event_glob, name_only);
1681 print_pmu_events(event_glob, name_only);
1683 if (event_glob != NULL)
1687 printf(" %-50s [%s]\n",
1689 event_type_descriptors[PERF_TYPE_RAW]);
1690 printf(" %-50s [%s]\n",
1691 "cpu/t1=v1[,t2=v2,t3 ...]/modifier",
1692 event_type_descriptors[PERF_TYPE_RAW]);
1693 printf(" (see 'man perf-list' on how to encode it)\n");
1696 printf(" %-50s [%s]\n",
1697 "mem:<addr>[/len][:access]",
1698 event_type_descriptors[PERF_TYPE_BREAKPOINT]);
1702 print_tracepoint_events(NULL, NULL, name_only);
1705 int parse_events__is_hardcoded_term(struct parse_events_term *term)
1707 return term->type_term != PARSE_EVENTS__TERM_TYPE_USER;
1710 static int new_term(struct parse_events_term **_term, int type_val,
1711 int type_term, char *config,
1712 char *str, u64 num, int err_term, int err_val)
1714 struct parse_events_term *term;
1716 term = zalloc(sizeof(*term));
1720 INIT_LIST_HEAD(&term->list);
1721 term->type_val = type_val;
1722 term->type_term = type_term;
1723 term->config = config;
1724 term->err_term = err_term;
1725 term->err_val = err_val;
1728 case PARSE_EVENTS__TERM_TYPE_NUM:
1729 term->val.num = num;
1731 case PARSE_EVENTS__TERM_TYPE_STR:
1732 term->val.str = str;
1743 int parse_events_term__num(struct parse_events_term **term,
1744 int type_term, char *config, u64 num,
1745 void *loc_term_, void *loc_val_)
1747 YYLTYPE *loc_term = loc_term_;
1748 YYLTYPE *loc_val = loc_val_;
1750 return new_term(term, PARSE_EVENTS__TERM_TYPE_NUM, type_term,
1752 loc_term ? loc_term->first_column : 0,
1753 loc_val ? loc_val->first_column : 0);
1756 int parse_events_term__str(struct parse_events_term **term,
1757 int type_term, char *config, char *str,
1758 void *loc_term_, void *loc_val_)
1760 YYLTYPE *loc_term = loc_term_;
1761 YYLTYPE *loc_val = loc_val_;
1763 return new_term(term, PARSE_EVENTS__TERM_TYPE_STR, type_term,
1765 loc_term ? loc_term->first_column : 0,
1766 loc_val ? loc_val->first_column : 0);
1769 int parse_events_term__sym_hw(struct parse_events_term **term,
1770 char *config, unsigned idx)
1772 struct event_symbol *sym;
1774 BUG_ON(idx >= PERF_COUNT_HW_MAX);
1775 sym = &event_symbols_hw[idx];
1778 return new_term(term, PARSE_EVENTS__TERM_TYPE_STR,
1779 PARSE_EVENTS__TERM_TYPE_USER, config,
1780 (char *) sym->symbol, 0, 0, 0);
1782 return new_term(term, PARSE_EVENTS__TERM_TYPE_STR,
1783 PARSE_EVENTS__TERM_TYPE_USER,
1784 (char *) "event", (char *) sym->symbol,
1788 int parse_events_term__clone(struct parse_events_term **new,
1789 struct parse_events_term *term)
1791 return new_term(new, term->type_val, term->type_term, term->config,
1792 term->val.str, term->val.num,
1793 term->err_term, term->err_val);
1796 void parse_events__free_terms(struct list_head *terms)
1798 struct parse_events_term *term, *h;
1800 list_for_each_entry_safe(term, h, terms, list)
1804 void parse_events_evlist_error(struct parse_events_evlist *data,
1805 int idx, const char *str)
1807 struct parse_events_error *err = data->error;
1812 err->str = strdup(str);
1813 WARN_ONCE(!err->str, "WARNING: failed to allocate error string");