1 #include <linux/hw_breakpoint.h>
6 #include "parse-options.h"
7 #include "parse-events.h"
9 #include "linux/string.h"
13 #include <lk/debugfs.h>
14 #include "parse-events-bison.h"
15 #define YY_EXTRA_TYPE int
16 #include "parse-events-flex.h"
19 #define MAX_NAME_LEN 100
27 extern int parse_events_debug;
29 int parse_events_parse(void *data, void *scanner);
31 static struct event_symbol event_symbols_hw[PERF_COUNT_HW_MAX] = {
32 [PERF_COUNT_HW_CPU_CYCLES] = {
33 .symbol = "cpu-cycles",
36 [PERF_COUNT_HW_INSTRUCTIONS] = {
37 .symbol = "instructions",
40 [PERF_COUNT_HW_CACHE_REFERENCES] = {
41 .symbol = "cache-references",
44 [PERF_COUNT_HW_CACHE_MISSES] = {
45 .symbol = "cache-misses",
48 [PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = {
49 .symbol = "branch-instructions",
52 [PERF_COUNT_HW_BRANCH_MISSES] = {
53 .symbol = "branch-misses",
56 [PERF_COUNT_HW_BUS_CYCLES] = {
57 .symbol = "bus-cycles",
60 [PERF_COUNT_HW_STALLED_CYCLES_FRONTEND] = {
61 .symbol = "stalled-cycles-frontend",
62 .alias = "idle-cycles-frontend",
64 [PERF_COUNT_HW_STALLED_CYCLES_BACKEND] = {
65 .symbol = "stalled-cycles-backend",
66 .alias = "idle-cycles-backend",
68 [PERF_COUNT_HW_REF_CPU_CYCLES] = {
69 .symbol = "ref-cycles",
74 static struct event_symbol event_symbols_sw[PERF_COUNT_SW_MAX] = {
75 [PERF_COUNT_SW_CPU_CLOCK] = {
76 .symbol = "cpu-clock",
79 [PERF_COUNT_SW_TASK_CLOCK] = {
80 .symbol = "task-clock",
83 [PERF_COUNT_SW_PAGE_FAULTS] = {
84 .symbol = "page-faults",
87 [PERF_COUNT_SW_CONTEXT_SWITCHES] = {
88 .symbol = "context-switches",
91 [PERF_COUNT_SW_CPU_MIGRATIONS] = {
92 .symbol = "cpu-migrations",
93 .alias = "migrations",
95 [PERF_COUNT_SW_PAGE_FAULTS_MIN] = {
96 .symbol = "minor-faults",
99 [PERF_COUNT_SW_PAGE_FAULTS_MAJ] = {
100 .symbol = "major-faults",
103 [PERF_COUNT_SW_ALIGNMENT_FAULTS] = {
104 .symbol = "alignment-faults",
107 [PERF_COUNT_SW_EMULATION_FAULTS] = {
108 .symbol = "emulation-faults",
113 #define __PERF_EVENT_FIELD(config, name) \
114 ((config & PERF_EVENT_##name##_MASK) >> PERF_EVENT_##name##_SHIFT)
116 #define PERF_EVENT_RAW(config) __PERF_EVENT_FIELD(config, RAW)
117 #define PERF_EVENT_CONFIG(config) __PERF_EVENT_FIELD(config, CONFIG)
118 #define PERF_EVENT_TYPE(config) __PERF_EVENT_FIELD(config, TYPE)
119 #define PERF_EVENT_ID(config) __PERF_EVENT_FIELD(config, EVENT)
121 #define for_each_subsystem(sys_dir, sys_dirent, sys_next) \
122 while (!readdir_r(sys_dir, &sys_dirent, &sys_next) && sys_next) \
123 if (sys_dirent.d_type == DT_DIR && \
124 (strcmp(sys_dirent.d_name, ".")) && \
125 (strcmp(sys_dirent.d_name, "..")))
127 static int tp_event_has_id(struct dirent *sys_dir, struct dirent *evt_dir)
129 char evt_path[MAXPATHLEN];
132 snprintf(evt_path, MAXPATHLEN, "%s/%s/%s/id", tracing_events_path,
133 sys_dir->d_name, evt_dir->d_name);
134 fd = open(evt_path, O_RDONLY);
142 #define for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next) \
143 while (!readdir_r(evt_dir, &evt_dirent, &evt_next) && evt_next) \
144 if (evt_dirent.d_type == DT_DIR && \
145 (strcmp(evt_dirent.d_name, ".")) && \
146 (strcmp(evt_dirent.d_name, "..")) && \
147 (!tp_event_has_id(&sys_dirent, &evt_dirent)))
149 #define MAX_EVENT_LENGTH 512
152 struct tracepoint_path *tracepoint_id_to_path(u64 config)
154 struct tracepoint_path *path = NULL;
155 DIR *sys_dir, *evt_dir;
156 struct dirent *sys_next, *evt_next, sys_dirent, evt_dirent;
160 char evt_path[MAXPATHLEN];
161 char dir_path[MAXPATHLEN];
163 if (debugfs_valid_mountpoint(tracing_events_path))
166 sys_dir = opendir(tracing_events_path);
170 for_each_subsystem(sys_dir, sys_dirent, sys_next) {
172 snprintf(dir_path, MAXPATHLEN, "%s/%s", tracing_events_path,
174 evt_dir = opendir(dir_path);
178 for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next) {
180 snprintf(evt_path, MAXPATHLEN, "%s/%s/id", dir_path,
182 fd = open(evt_path, O_RDONLY);
185 if (read(fd, id_buf, sizeof(id_buf)) < 0) {
194 path = zalloc(sizeof(*path));
195 path->system = malloc(MAX_EVENT_LENGTH);
200 path->name = malloc(MAX_EVENT_LENGTH);
206 strncpy(path->system, sys_dirent.d_name,
208 strncpy(path->name, evt_dirent.d_name,
220 struct tracepoint_path *tracepoint_name_to_path(const char *name)
222 struct tracepoint_path *path = zalloc(sizeof(*path));
223 char *str = strchr(name, ':');
225 if (path == NULL || str == NULL) {
230 path->system = strndup(name, str - name);
231 path->name = strdup(str+1);
233 if (path->system == NULL || path->name == NULL) {
243 const char *event_type(int type)
246 case PERF_TYPE_HARDWARE:
249 case PERF_TYPE_SOFTWARE:
252 case PERF_TYPE_TRACEPOINT:
255 case PERF_TYPE_HW_CACHE:
256 return "hardware-cache";
267 static int __add_event(struct list_head *list, int *idx,
268 struct perf_event_attr *attr,
269 char *name, struct cpu_map *cpus)
271 struct perf_evsel *evsel;
273 event_attr_init(attr);
275 evsel = perf_evsel__new(attr, (*idx)++);
281 evsel->name = strdup(name);
282 list_add_tail(&evsel->node, list);
286 static int add_event(struct list_head *list, int *idx,
287 struct perf_event_attr *attr, char *name)
289 return __add_event(list, idx, attr, name, NULL);
292 static int parse_aliases(char *str, const char *names[][PERF_EVSEL__MAX_ALIASES], int size)
297 for (i = 0; i < size; i++) {
298 for (j = 0; j < PERF_EVSEL__MAX_ALIASES && names[i][j]; j++) {
299 n = strlen(names[i][j]);
300 if (n > longest && !strncasecmp(str, names[i][j], n))
310 int parse_events_add_cache(struct list_head *list, int *idx,
311 char *type, char *op_result1, char *op_result2)
313 struct perf_event_attr attr;
314 char name[MAX_NAME_LEN];
315 int cache_type = -1, cache_op = -1, cache_result = -1;
316 char *op_result[2] = { op_result1, op_result2 };
320 * No fallback - if we cannot get a clear cache type
323 cache_type = parse_aliases(type, perf_evsel__hw_cache,
324 PERF_COUNT_HW_CACHE_MAX);
325 if (cache_type == -1)
328 n = snprintf(name, MAX_NAME_LEN, "%s", type);
330 for (i = 0; (i < 2) && (op_result[i]); i++) {
331 char *str = op_result[i];
333 n += snprintf(name + n, MAX_NAME_LEN - n, "-%s", str);
335 if (cache_op == -1) {
336 cache_op = parse_aliases(str, perf_evsel__hw_cache_op,
337 PERF_COUNT_HW_CACHE_OP_MAX);
339 if (!perf_evsel__is_cache_op_valid(cache_type, cache_op))
345 if (cache_result == -1) {
346 cache_result = parse_aliases(str, perf_evsel__hw_cache_result,
347 PERF_COUNT_HW_CACHE_RESULT_MAX);
348 if (cache_result >= 0)
354 * Fall back to reads:
357 cache_op = PERF_COUNT_HW_CACHE_OP_READ;
360 * Fall back to accesses:
362 if (cache_result == -1)
363 cache_result = PERF_COUNT_HW_CACHE_RESULT_ACCESS;
365 memset(&attr, 0, sizeof(attr));
366 attr.config = cache_type | (cache_op << 8) | (cache_result << 16);
367 attr.type = PERF_TYPE_HW_CACHE;
368 return add_event(list, idx, &attr, name);
371 static int add_tracepoint(struct list_head *list, int *idx,
372 char *sys_name, char *evt_name)
374 struct perf_evsel *evsel;
376 evsel = perf_evsel__newtp(sys_name, evt_name, (*idx)++);
380 list_add_tail(&evsel->node, list);
385 static int add_tracepoint_multi_event(struct list_head *list, int *idx,
386 char *sys_name, char *evt_name)
388 char evt_path[MAXPATHLEN];
389 struct dirent *evt_ent;
393 snprintf(evt_path, MAXPATHLEN, "%s/%s", tracing_events_path, sys_name);
394 evt_dir = opendir(evt_path);
396 perror("Can't open event dir");
400 while (!ret && (evt_ent = readdir(evt_dir))) {
401 if (!strcmp(evt_ent->d_name, ".")
402 || !strcmp(evt_ent->d_name, "..")
403 || !strcmp(evt_ent->d_name, "enable")
404 || !strcmp(evt_ent->d_name, "filter"))
407 if (!strglobmatch(evt_ent->d_name, evt_name))
410 ret = add_tracepoint(list, idx, sys_name, evt_ent->d_name);
417 static int add_tracepoint_event(struct list_head *list, int *idx,
418 char *sys_name, char *evt_name)
420 return strpbrk(evt_name, "*?") ?
421 add_tracepoint_multi_event(list, idx, sys_name, evt_name) :
422 add_tracepoint(list, idx, sys_name, evt_name);
425 static int add_tracepoint_multi_sys(struct list_head *list, int *idx,
426 char *sys_name, char *evt_name)
428 struct dirent *events_ent;
432 events_dir = opendir(tracing_events_path);
434 perror("Can't open event dir");
438 while (!ret && (events_ent = readdir(events_dir))) {
439 if (!strcmp(events_ent->d_name, ".")
440 || !strcmp(events_ent->d_name, "..")
441 || !strcmp(events_ent->d_name, "enable")
442 || !strcmp(events_ent->d_name, "header_event")
443 || !strcmp(events_ent->d_name, "header_page"))
446 if (!strglobmatch(events_ent->d_name, sys_name))
449 ret = add_tracepoint_event(list, idx, events_ent->d_name,
453 closedir(events_dir);
457 int parse_events_add_tracepoint(struct list_head *list, int *idx,
458 char *sys, char *event)
462 ret = debugfs_valid_mountpoint(tracing_events_path);
466 if (strpbrk(sys, "*?"))
467 return add_tracepoint_multi_sys(list, idx, sys, event);
469 return add_tracepoint_event(list, idx, sys, event);
473 parse_breakpoint_type(const char *type, struct perf_event_attr *attr)
477 for (i = 0; i < 3; i++) {
478 if (!type || !type[i])
481 #define CHECK_SET_TYPE(bit) \
483 if (attr->bp_type & bit) \
486 attr->bp_type |= bit; \
491 CHECK_SET_TYPE(HW_BREAKPOINT_R);
494 CHECK_SET_TYPE(HW_BREAKPOINT_W);
497 CHECK_SET_TYPE(HW_BREAKPOINT_X);
504 #undef CHECK_SET_TYPE
506 if (!attr->bp_type) /* Default */
507 attr->bp_type = HW_BREAKPOINT_R | HW_BREAKPOINT_W;
512 int parse_events_add_breakpoint(struct list_head *list, int *idx,
513 void *ptr, char *type)
515 struct perf_event_attr attr;
517 memset(&attr, 0, sizeof(attr));
518 attr.bp_addr = (unsigned long) ptr;
520 if (parse_breakpoint_type(type, &attr))
524 * We should find a nice way to override the access length
525 * Provide some defaults for now
527 if (attr.bp_type == HW_BREAKPOINT_X)
528 attr.bp_len = sizeof(long);
530 attr.bp_len = HW_BREAKPOINT_LEN_4;
532 attr.type = PERF_TYPE_BREAKPOINT;
533 attr.sample_period = 1;
535 return add_event(list, idx, &attr, NULL);
538 static int config_term(struct perf_event_attr *attr,
539 struct parse_events_term *term)
541 #define CHECK_TYPE_VAL(type) \
543 if (PARSE_EVENTS__TERM_TYPE_ ## type != term->type_val) \
547 switch (term->type_term) {
548 case PARSE_EVENTS__TERM_TYPE_CONFIG:
550 attr->config = term->val.num;
552 case PARSE_EVENTS__TERM_TYPE_CONFIG1:
554 attr->config1 = term->val.num;
556 case PARSE_EVENTS__TERM_TYPE_CONFIG2:
558 attr->config2 = term->val.num;
560 case PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD:
562 attr->sample_period = term->val.num;
564 case PARSE_EVENTS__TERM_TYPE_BRANCH_SAMPLE_TYPE:
566 * TODO uncomment when the field is available
567 * attr->branch_sample_type = term->val.num;
570 case PARSE_EVENTS__TERM_TYPE_NAME:
578 #undef CHECK_TYPE_VAL
581 static int config_attr(struct perf_event_attr *attr,
582 struct list_head *head, int fail)
584 struct parse_events_term *term;
586 list_for_each_entry(term, head, list)
587 if (config_term(attr, term) && fail)
593 int parse_events_add_numeric(struct list_head *list, int *idx,
594 u32 type, u64 config,
595 struct list_head *head_config)
597 struct perf_event_attr attr;
599 memset(&attr, 0, sizeof(attr));
601 attr.config = config;
604 config_attr(&attr, head_config, 1))
607 return add_event(list, idx, &attr, NULL);
610 static int parse_events__is_name_term(struct parse_events_term *term)
612 return term->type_term == PARSE_EVENTS__TERM_TYPE_NAME;
615 static char *pmu_event_name(struct list_head *head_terms)
617 struct parse_events_term *term;
619 list_for_each_entry(term, head_terms, list)
620 if (parse_events__is_name_term(term))
621 return term->val.str;
626 int parse_events_add_pmu(struct list_head *list, int *idx,
627 char *name, struct list_head *head_config)
629 struct perf_event_attr attr;
630 struct perf_pmu *pmu;
632 pmu = perf_pmu__find(name);
636 memset(&attr, 0, sizeof(attr));
638 if (perf_pmu__check_alias(pmu, head_config))
642 * Configure hardcoded terms first, no need to check
643 * return value when called with fail == 0 ;)
645 config_attr(&attr, head_config, 0);
647 if (perf_pmu__config(pmu, &attr, head_config))
650 return __add_event(list, idx, &attr, pmu_event_name(head_config),
654 int parse_events__modifier_group(struct list_head *list,
657 return parse_events__modifier_event(list, event_mod, true);
660 void parse_events__set_leader(char *name, struct list_head *list)
662 struct perf_evsel *leader;
664 __perf_evlist__set_leader(list);
665 leader = list_entry(list->next, struct perf_evsel, node);
666 leader->group_name = name ? strdup(name) : NULL;
669 /* list_event is assumed to point to malloc'ed memory */
670 void parse_events_update_lists(struct list_head *list_event,
671 struct list_head *list_all)
674 * Called for single event definition. Update the
675 * 'all event' list, and reinit the 'single event'
676 * list, for next event definition.
678 list_splice_tail(list_event, list_all);
682 struct event_modifier {
694 static int get_event_modifier(struct event_modifier *mod, char *str,
695 struct perf_evsel *evsel)
697 int eu = evsel ? evsel->attr.exclude_user : 0;
698 int ek = evsel ? evsel->attr.exclude_kernel : 0;
699 int eh = evsel ? evsel->attr.exclude_hv : 0;
700 int eH = evsel ? evsel->attr.exclude_host : 0;
701 int eG = evsel ? evsel->attr.exclude_guest : 0;
702 int precise = evsel ? evsel->attr.precise_ip : 0;
704 int pinned = evsel ? evsel->attr.pinned : 0;
706 int exclude = eu | ek | eh;
707 int exclude_GH = evsel ? evsel->exclude_GH : 0;
709 memset(mod, 0, sizeof(*mod));
714 exclude = eu = ek = eh = 1;
716 } else if (*str == 'k') {
718 exclude = eu = ek = eh = 1;
720 } else if (*str == 'h') {
722 exclude = eu = ek = eh = 1;
724 } else if (*str == 'G') {
726 exclude_GH = eG = eH = 1;
728 } else if (*str == 'H') {
730 exclude_GH = eG = eH = 1;
732 } else if (*str == 'p') {
734 /* use of precise requires exclude_guest */
737 } else if (*str == 'S') {
739 } else if (*str == 'D') {
750 * 0 - SAMPLE_IP can have arbitrary skid
751 * 1 - SAMPLE_IP must have constant skid
752 * 2 - SAMPLE_IP requested to have 0 skid
753 * 3 - SAMPLE_IP must have 0 skid
755 * See also PERF_RECORD_MISC_EXACT_IP
765 mod->precise = precise;
766 mod->exclude_GH = exclude_GH;
767 mod->sample_read = sample_read;
768 mod->pinned = pinned;
774 * Basic modifier sanity check to validate it contains only one
775 * instance of any modifier (apart from 'p') present.
777 static int check_modifier(char *str)
781 /* The sizeof includes 0 byte as well. */
782 if (strlen(str) > (sizeof("ukhGHpppSD") - 1))
786 if (*p != 'p' && strchr(p + 1, *p))
794 int parse_events__modifier_event(struct list_head *list, char *str, bool add)
796 struct perf_evsel *evsel;
797 struct event_modifier mod;
802 if (check_modifier(str))
805 if (!add && get_event_modifier(&mod, str, NULL))
808 list_for_each_entry(evsel, list, node) {
810 if (add && get_event_modifier(&mod, str, evsel))
813 evsel->attr.exclude_user = mod.eu;
814 evsel->attr.exclude_kernel = mod.ek;
815 evsel->attr.exclude_hv = mod.eh;
816 evsel->attr.precise_ip = mod.precise;
817 evsel->attr.exclude_host = mod.eH;
818 evsel->attr.exclude_guest = mod.eG;
819 evsel->exclude_GH = mod.exclude_GH;
820 evsel->sample_read = mod.sample_read;
822 if (perf_evsel__is_group_leader(evsel))
823 evsel->attr.pinned = mod.pinned;
829 int parse_events_name(struct list_head *list, char *name)
831 struct perf_evsel *evsel;
833 list_for_each_entry(evsel, list, node) {
835 evsel->name = strdup(name);
841 static int parse_events__scanner(const char *str, void *data, int start_token);
843 static int parse_events_fixup(int ret, const char *str, void *data,
846 char *o = strdup(str);
854 while ((p = strsep(&t, ",")) != NULL) {
856 str_append(&s, &len, ",");
857 str_append(&s, &len, "cpu/");
858 str_append(&s, &len, p);
859 str_append(&s, &len, "/");
864 return parse_events__scanner(s, data, start_token);
867 static int parse_events__scanner(const char *str, void *data, int start_token)
869 YY_BUFFER_STATE buffer;
873 ret = parse_events_lex_init_extra(start_token, &scanner);
877 buffer = parse_events__scan_string(str, scanner);
880 parse_events_debug = 1;
882 ret = parse_events_parse(data, scanner);
884 parse_events__flush_buffer(buffer, scanner);
885 parse_events__delete_buffer(buffer, scanner);
886 parse_events_lex_destroy(scanner);
887 if (ret && !strchr(str, '/'))
888 ret = parse_events_fixup(ret, str, data, start_token);
893 * parse event config string, return a list of event terms.
895 int parse_events_terms(struct list_head *terms, const char *str)
897 struct parse_events_terms data = {
902 ret = parse_events__scanner(str, &data, PE_START_TERMS);
904 list_splice(data.terms, terms);
910 parse_events__free_terms(data.terms);
914 int parse_events(struct perf_evlist *evlist, const char *str)
916 struct parse_events_evlist data = {
917 .list = LIST_HEAD_INIT(data.list),
918 .idx = evlist->nr_entries,
922 ret = parse_events__scanner(str, &data, PE_START_EVENTS);
924 int entries = data.idx - evlist->nr_entries;
925 perf_evlist__splice_list_tail(evlist, &data.list, entries);
926 evlist->nr_groups += data.nr_groups;
931 * There are 2 users - builtin-record and builtin-test objects.
932 * Both call perf_evlist__delete in case of error, so we dont
938 int parse_events_option(const struct option *opt, const char *str,
939 int unset __maybe_unused)
941 struct perf_evlist *evlist = *(struct perf_evlist **)opt->value;
942 int ret = parse_events(evlist, str);
945 fprintf(stderr, "invalid or unsupported event: '%s'\n", str);
946 fprintf(stderr, "Run 'perf list' for a list of valid events\n");
951 int parse_filter(const struct option *opt, const char *str,
952 int unset __maybe_unused)
954 struct perf_evlist *evlist = *(struct perf_evlist **)opt->value;
955 struct perf_evsel *last = NULL;
957 if (evlist->nr_entries > 0)
958 last = perf_evlist__last(evlist);
960 if (last == NULL || last->attr.type != PERF_TYPE_TRACEPOINT) {
962 "-F option should follow a -e tracepoint option\n");
966 last->filter = strdup(str);
967 if (last->filter == NULL) {
968 fprintf(stderr, "not enough memory to hold filter string\n");
975 static const char * const event_type_descriptors[] = {
979 "Hardware cache event",
980 "Raw hardware event descriptor",
981 "Hardware breakpoint",
985 * Print the events from <debugfs_mount_point>/tracing/events
988 void print_tracepoint_events(const char *subsys_glob, const char *event_glob,
991 DIR *sys_dir, *evt_dir;
992 struct dirent *sys_next, *evt_next, sys_dirent, evt_dirent;
993 char evt_path[MAXPATHLEN];
994 char dir_path[MAXPATHLEN];
996 if (debugfs_valid_mountpoint(tracing_events_path))
999 sys_dir = opendir(tracing_events_path);
1003 for_each_subsystem(sys_dir, sys_dirent, sys_next) {
1004 if (subsys_glob != NULL &&
1005 !strglobmatch(sys_dirent.d_name, subsys_glob))
1008 snprintf(dir_path, MAXPATHLEN, "%s/%s", tracing_events_path,
1010 evt_dir = opendir(dir_path);
1014 for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next) {
1015 if (event_glob != NULL &&
1016 !strglobmatch(evt_dirent.d_name, event_glob))
1020 printf("%s:%s ", sys_dirent.d_name, evt_dirent.d_name);
1024 snprintf(evt_path, MAXPATHLEN, "%s:%s",
1025 sys_dirent.d_name, evt_dirent.d_name);
1026 printf(" %-50s [%s]\n", evt_path,
1027 event_type_descriptors[PERF_TYPE_TRACEPOINT]);
1035 * Check whether event is in <debugfs_mount_point>/tracing/events
1038 int is_valid_tracepoint(const char *event_string)
1040 DIR *sys_dir, *evt_dir;
1041 struct dirent *sys_next, *evt_next, sys_dirent, evt_dirent;
1042 char evt_path[MAXPATHLEN];
1043 char dir_path[MAXPATHLEN];
1045 if (debugfs_valid_mountpoint(tracing_events_path))
1048 sys_dir = opendir(tracing_events_path);
1052 for_each_subsystem(sys_dir, sys_dirent, sys_next) {
1054 snprintf(dir_path, MAXPATHLEN, "%s/%s", tracing_events_path,
1056 evt_dir = opendir(dir_path);
1060 for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next) {
1061 snprintf(evt_path, MAXPATHLEN, "%s:%s",
1062 sys_dirent.d_name, evt_dirent.d_name);
1063 if (!strcmp(evt_path, event_string)) {
1075 static void __print_events_type(u8 type, struct event_symbol *syms,
1081 for (i = 0; i < max ; i++, syms++) {
1082 if (strlen(syms->alias))
1083 snprintf(name, sizeof(name), "%s OR %s",
1084 syms->symbol, syms->alias);
1086 snprintf(name, sizeof(name), "%s", syms->symbol);
1088 printf(" %-50s [%s]\n", name,
1089 event_type_descriptors[type]);
1093 void print_events_type(u8 type)
1095 if (type == PERF_TYPE_SOFTWARE)
1096 __print_events_type(type, event_symbols_sw, PERF_COUNT_SW_MAX);
1098 __print_events_type(type, event_symbols_hw, PERF_COUNT_HW_MAX);
1101 int print_hwcache_events(const char *event_glob, bool name_only)
1103 unsigned int type, op, i, printed = 0;
1106 for (type = 0; type < PERF_COUNT_HW_CACHE_MAX; type++) {
1107 for (op = 0; op < PERF_COUNT_HW_CACHE_OP_MAX; op++) {
1108 /* skip invalid cache type */
1109 if (!perf_evsel__is_cache_op_valid(type, op))
1112 for (i = 0; i < PERF_COUNT_HW_CACHE_RESULT_MAX; i++) {
1113 __perf_evsel__hw_cache_type_op_res_name(type, op, i,
1114 name, sizeof(name));
1115 if (event_glob != NULL && !strglobmatch(name, event_glob))
1119 printf("%s ", name);
1121 printf(" %-50s [%s]\n", name,
1122 event_type_descriptors[PERF_TYPE_HW_CACHE]);
1133 static void print_symbol_events(const char *event_glob, unsigned type,
1134 struct event_symbol *syms, unsigned max,
1137 unsigned i, printed = 0;
1138 char name[MAX_NAME_LEN];
1140 for (i = 0; i < max; i++, syms++) {
1142 if (event_glob != NULL &&
1143 !(strglobmatch(syms->symbol, event_glob) ||
1144 (syms->alias && strglobmatch(syms->alias, event_glob))))
1148 printf("%s ", syms->symbol);
1152 if (strlen(syms->alias))
1153 snprintf(name, MAX_NAME_LEN, "%s OR %s", syms->symbol, syms->alias);
1155 strncpy(name, syms->symbol, MAX_NAME_LEN);
1157 printf(" %-50s [%s]\n", name, event_type_descriptors[type]);
1167 * Print the help text for the event symbols:
1169 void print_events(const char *event_glob, bool name_only)
1173 printf("List of pre-defined events (to be used in -e):\n");
1176 print_symbol_events(event_glob, PERF_TYPE_HARDWARE,
1177 event_symbols_hw, PERF_COUNT_HW_MAX, name_only);
1179 print_symbol_events(event_glob, PERF_TYPE_SOFTWARE,
1180 event_symbols_sw, PERF_COUNT_SW_MAX, name_only);
1182 print_hwcache_events(event_glob, name_only);
1184 print_pmu_events(event_glob, name_only);
1186 if (event_glob != NULL)
1190 printf(" %-50s [%s]\n",
1192 event_type_descriptors[PERF_TYPE_RAW]);
1193 printf(" %-50s [%s]\n",
1194 "cpu/t1=v1[,t2=v2,t3 ...]/modifier",
1195 event_type_descriptors[PERF_TYPE_RAW]);
1196 printf(" (see 'man perf-list' on how to encode it)\n");
1199 printf(" %-50s [%s]\n",
1200 "mem:<addr>[:access]",
1201 event_type_descriptors[PERF_TYPE_BREAKPOINT]);
1205 print_tracepoint_events(NULL, NULL, name_only);
1208 int parse_events__is_hardcoded_term(struct parse_events_term *term)
1210 return term->type_term != PARSE_EVENTS__TERM_TYPE_USER;
1213 static int new_term(struct parse_events_term **_term, int type_val,
1214 int type_term, char *config,
1217 struct parse_events_term *term;
1219 term = zalloc(sizeof(*term));
1223 INIT_LIST_HEAD(&term->list);
1224 term->type_val = type_val;
1225 term->type_term = type_term;
1226 term->config = config;
1229 case PARSE_EVENTS__TERM_TYPE_NUM:
1230 term->val.num = num;
1232 case PARSE_EVENTS__TERM_TYPE_STR:
1233 term->val.str = str;
1244 int parse_events_term__num(struct parse_events_term **term,
1245 int type_term, char *config, u64 num)
1247 return new_term(term, PARSE_EVENTS__TERM_TYPE_NUM, type_term,
1251 int parse_events_term__str(struct parse_events_term **term,
1252 int type_term, char *config, char *str)
1254 return new_term(term, PARSE_EVENTS__TERM_TYPE_STR, type_term,
1258 int parse_events_term__sym_hw(struct parse_events_term **term,
1259 char *config, unsigned idx)
1261 struct event_symbol *sym;
1263 BUG_ON(idx >= PERF_COUNT_HW_MAX);
1264 sym = &event_symbols_hw[idx];
1267 return new_term(term, PARSE_EVENTS__TERM_TYPE_STR,
1268 PARSE_EVENTS__TERM_TYPE_USER, config,
1269 (char *) sym->symbol, 0);
1271 return new_term(term, PARSE_EVENTS__TERM_TYPE_STR,
1272 PARSE_EVENTS__TERM_TYPE_USER,
1273 (char *) "event", (char *) sym->symbol, 0);
1276 int parse_events_term__clone(struct parse_events_term **new,
1277 struct parse_events_term *term)
1279 return new_term(new, term->type_val, term->type_term, term->config,
1280 term->val.str, term->val.num);
1283 void parse_events__free_terms(struct list_head *terms)
1285 struct parse_events_term *term, *h;
1287 list_for_each_entry_safe(term, h, terms, list)