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"
22 #define MAX_NAME_LEN 100
25 extern int parse_events_debug;
27 int parse_events_parse(void *data, void *scanner);
29 static struct perf_pmu_event_symbol *perf_pmu_events_list;
31 * The variable indicates the number of supported pmu event symbols.
32 * 0 means not initialized and ready to init
33 * -1 means failed to init, don't try anymore
34 * >0 is the number of supported pmu event symbols
36 static int perf_pmu_events_list_num;
38 struct event_symbol event_symbols_hw[PERF_COUNT_HW_MAX] = {
39 [PERF_COUNT_HW_CPU_CYCLES] = {
40 .symbol = "cpu-cycles",
43 [PERF_COUNT_HW_INSTRUCTIONS] = {
44 .symbol = "instructions",
47 [PERF_COUNT_HW_CACHE_REFERENCES] = {
48 .symbol = "cache-references",
51 [PERF_COUNT_HW_CACHE_MISSES] = {
52 .symbol = "cache-misses",
55 [PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = {
56 .symbol = "branch-instructions",
59 [PERF_COUNT_HW_BRANCH_MISSES] = {
60 .symbol = "branch-misses",
63 [PERF_COUNT_HW_BUS_CYCLES] = {
64 .symbol = "bus-cycles",
67 [PERF_COUNT_HW_STALLED_CYCLES_FRONTEND] = {
68 .symbol = "stalled-cycles-frontend",
69 .alias = "idle-cycles-frontend",
71 [PERF_COUNT_HW_STALLED_CYCLES_BACKEND] = {
72 .symbol = "stalled-cycles-backend",
73 .alias = "idle-cycles-backend",
75 [PERF_COUNT_HW_REF_CPU_CYCLES] = {
76 .symbol = "ref-cycles",
81 struct event_symbol event_symbols_sw[PERF_COUNT_SW_MAX] = {
82 [PERF_COUNT_SW_CPU_CLOCK] = {
83 .symbol = "cpu-clock",
86 [PERF_COUNT_SW_TASK_CLOCK] = {
87 .symbol = "task-clock",
90 [PERF_COUNT_SW_PAGE_FAULTS] = {
91 .symbol = "page-faults",
94 [PERF_COUNT_SW_CONTEXT_SWITCHES] = {
95 .symbol = "context-switches",
98 [PERF_COUNT_SW_CPU_MIGRATIONS] = {
99 .symbol = "cpu-migrations",
100 .alias = "migrations",
102 [PERF_COUNT_SW_PAGE_FAULTS_MIN] = {
103 .symbol = "minor-faults",
106 [PERF_COUNT_SW_PAGE_FAULTS_MAJ] = {
107 .symbol = "major-faults",
110 [PERF_COUNT_SW_ALIGNMENT_FAULTS] = {
111 .symbol = "alignment-faults",
114 [PERF_COUNT_SW_EMULATION_FAULTS] = {
115 .symbol = "emulation-faults",
118 [PERF_COUNT_SW_DUMMY] = {
124 #define __PERF_EVENT_FIELD(config, name) \
125 ((config & PERF_EVENT_##name##_MASK) >> PERF_EVENT_##name##_SHIFT)
127 #define PERF_EVENT_RAW(config) __PERF_EVENT_FIELD(config, RAW)
128 #define PERF_EVENT_CONFIG(config) __PERF_EVENT_FIELD(config, CONFIG)
129 #define PERF_EVENT_TYPE(config) __PERF_EVENT_FIELD(config, TYPE)
130 #define PERF_EVENT_ID(config) __PERF_EVENT_FIELD(config, EVENT)
132 #define for_each_subsystem(sys_dir, sys_dirent, sys_next) \
133 while (!readdir_r(sys_dir, &sys_dirent, &sys_next) && sys_next) \
134 if (sys_dirent.d_type == DT_DIR && \
135 (strcmp(sys_dirent.d_name, ".")) && \
136 (strcmp(sys_dirent.d_name, "..")))
138 static int tp_event_has_id(struct dirent *sys_dir, struct dirent *evt_dir)
140 char evt_path[MAXPATHLEN];
143 snprintf(evt_path, MAXPATHLEN, "%s/%s/%s/id", tracing_events_path,
144 sys_dir->d_name, evt_dir->d_name);
145 fd = open(evt_path, O_RDONLY);
153 #define for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next) \
154 while (!readdir_r(evt_dir, &evt_dirent, &evt_next) && evt_next) \
155 if (evt_dirent.d_type == DT_DIR && \
156 (strcmp(evt_dirent.d_name, ".")) && \
157 (strcmp(evt_dirent.d_name, "..")) && \
158 (!tp_event_has_id(&sys_dirent, &evt_dirent)))
160 #define MAX_EVENT_LENGTH 512
163 struct tracepoint_path *tracepoint_id_to_path(u64 config)
165 struct tracepoint_path *path = NULL;
166 DIR *sys_dir, *evt_dir;
167 struct dirent *sys_next, *evt_next, sys_dirent, evt_dirent;
171 char evt_path[MAXPATHLEN];
172 char dir_path[MAXPATHLEN];
174 sys_dir = opendir(tracing_events_path);
178 for_each_subsystem(sys_dir, sys_dirent, sys_next) {
180 snprintf(dir_path, MAXPATHLEN, "%s/%s", tracing_events_path,
182 evt_dir = opendir(dir_path);
186 for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next) {
188 snprintf(evt_path, MAXPATHLEN, "%s/%s/id", dir_path,
190 fd = open(evt_path, O_RDONLY);
193 if (read(fd, id_buf, sizeof(id_buf)) < 0) {
202 path = zalloc(sizeof(*path));
203 path->system = malloc(MAX_EVENT_LENGTH);
208 path->name = malloc(MAX_EVENT_LENGTH);
210 zfree(&path->system);
214 strncpy(path->system, sys_dirent.d_name,
216 strncpy(path->name, evt_dirent.d_name,
228 struct tracepoint_path *tracepoint_name_to_path(const char *name)
230 struct tracepoint_path *path = zalloc(sizeof(*path));
231 char *str = strchr(name, ':');
233 if (path == NULL || str == NULL) {
238 path->system = strndup(name, str - name);
239 path->name = strdup(str+1);
241 if (path->system == NULL || path->name == NULL) {
242 zfree(&path->system);
251 const char *event_type(int type)
254 case PERF_TYPE_HARDWARE:
257 case PERF_TYPE_SOFTWARE:
260 case PERF_TYPE_TRACEPOINT:
263 case PERF_TYPE_HW_CACHE:
264 return "hardware-cache";
275 static struct perf_evsel *
276 __add_event(struct list_head *list, int *idx,
277 struct perf_event_attr *attr,
278 char *name, struct cpu_map *cpus)
280 struct perf_evsel *evsel;
282 event_attr_init(attr);
284 evsel = perf_evsel__new_idx(attr, (*idx)++);
290 evsel->name = strdup(name);
291 list_add_tail(&evsel->node, list);
295 static int add_event(struct list_head *list, int *idx,
296 struct perf_event_attr *attr, char *name)
298 return __add_event(list, idx, attr, name, NULL) ? 0 : -ENOMEM;
301 static int parse_aliases(char *str, const char *names[][PERF_EVSEL__MAX_ALIASES], int size)
306 for (i = 0; i < size; i++) {
307 for (j = 0; j < PERF_EVSEL__MAX_ALIASES && names[i][j]; j++) {
308 n = strlen(names[i][j]);
309 if (n > longest && !strncasecmp(str, names[i][j], n))
319 int parse_events_add_cache(struct list_head *list, int *idx,
320 char *type, char *op_result1, char *op_result2)
322 struct perf_event_attr attr;
323 char name[MAX_NAME_LEN];
324 int cache_type = -1, cache_op = -1, cache_result = -1;
325 char *op_result[2] = { op_result1, op_result2 };
329 * No fallback - if we cannot get a clear cache type
332 cache_type = parse_aliases(type, perf_evsel__hw_cache,
333 PERF_COUNT_HW_CACHE_MAX);
334 if (cache_type == -1)
337 n = snprintf(name, MAX_NAME_LEN, "%s", type);
339 for (i = 0; (i < 2) && (op_result[i]); i++) {
340 char *str = op_result[i];
342 n += snprintf(name + n, MAX_NAME_LEN - n, "-%s", str);
344 if (cache_op == -1) {
345 cache_op = parse_aliases(str, perf_evsel__hw_cache_op,
346 PERF_COUNT_HW_CACHE_OP_MAX);
348 if (!perf_evsel__is_cache_op_valid(cache_type, cache_op))
354 if (cache_result == -1) {
355 cache_result = parse_aliases(str, perf_evsel__hw_cache_result,
356 PERF_COUNT_HW_CACHE_RESULT_MAX);
357 if (cache_result >= 0)
363 * Fall back to reads:
366 cache_op = PERF_COUNT_HW_CACHE_OP_READ;
369 * Fall back to accesses:
371 if (cache_result == -1)
372 cache_result = PERF_COUNT_HW_CACHE_RESULT_ACCESS;
374 memset(&attr, 0, sizeof(attr));
375 attr.config = cache_type | (cache_op << 8) | (cache_result << 16);
376 attr.type = PERF_TYPE_HW_CACHE;
377 return add_event(list, idx, &attr, name);
380 static int add_tracepoint(struct list_head *list, int *idx,
381 char *sys_name, char *evt_name)
383 struct perf_evsel *evsel;
385 evsel = perf_evsel__newtp_idx(sys_name, evt_name, (*idx)++);
389 list_add_tail(&evsel->node, list);
394 static int add_tracepoint_multi_event(struct list_head *list, int *idx,
395 char *sys_name, char *evt_name)
397 char evt_path[MAXPATHLEN];
398 struct dirent *evt_ent;
402 snprintf(evt_path, MAXPATHLEN, "%s/%s", tracing_events_path, sys_name);
403 evt_dir = opendir(evt_path);
405 perror("Can't open event dir");
409 while (!ret && (evt_ent = readdir(evt_dir))) {
410 if (!strcmp(evt_ent->d_name, ".")
411 || !strcmp(evt_ent->d_name, "..")
412 || !strcmp(evt_ent->d_name, "enable")
413 || !strcmp(evt_ent->d_name, "filter"))
416 if (!strglobmatch(evt_ent->d_name, evt_name))
419 ret = add_tracepoint(list, idx, sys_name, evt_ent->d_name);
426 static int add_tracepoint_event(struct list_head *list, int *idx,
427 char *sys_name, char *evt_name)
429 return strpbrk(evt_name, "*?") ?
430 add_tracepoint_multi_event(list, idx, sys_name, evt_name) :
431 add_tracepoint(list, idx, sys_name, evt_name);
434 static int add_tracepoint_multi_sys(struct list_head *list, int *idx,
435 char *sys_name, char *evt_name)
437 struct dirent *events_ent;
441 events_dir = opendir(tracing_events_path);
443 perror("Can't open event dir");
447 while (!ret && (events_ent = readdir(events_dir))) {
448 if (!strcmp(events_ent->d_name, ".")
449 || !strcmp(events_ent->d_name, "..")
450 || !strcmp(events_ent->d_name, "enable")
451 || !strcmp(events_ent->d_name, "header_event")
452 || !strcmp(events_ent->d_name, "header_page"))
455 if (!strglobmatch(events_ent->d_name, sys_name))
458 ret = add_tracepoint_event(list, idx, events_ent->d_name,
462 closedir(events_dir);
466 int parse_events_add_tracepoint(struct list_head *list, int *idx,
467 char *sys, char *event)
469 if (strpbrk(sys, "*?"))
470 return add_tracepoint_multi_sys(list, idx, sys, event);
472 return add_tracepoint_event(list, idx, sys, event);
476 parse_breakpoint_type(const char *type, struct perf_event_attr *attr)
480 for (i = 0; i < 3; i++) {
481 if (!type || !type[i])
484 #define CHECK_SET_TYPE(bit) \
486 if (attr->bp_type & bit) \
489 attr->bp_type |= bit; \
494 CHECK_SET_TYPE(HW_BREAKPOINT_R);
497 CHECK_SET_TYPE(HW_BREAKPOINT_W);
500 CHECK_SET_TYPE(HW_BREAKPOINT_X);
507 #undef CHECK_SET_TYPE
509 if (!attr->bp_type) /* Default */
510 attr->bp_type = HW_BREAKPOINT_R | HW_BREAKPOINT_W;
515 int parse_events_add_breakpoint(struct list_head *list, int *idx,
516 void *ptr, char *type, u64 len)
518 struct perf_event_attr attr;
520 memset(&attr, 0, sizeof(attr));
521 attr.bp_addr = (unsigned long) ptr;
523 if (parse_breakpoint_type(type, &attr))
526 /* Provide some defaults if len is not specified */
528 if (attr.bp_type == HW_BREAKPOINT_X)
531 len = HW_BREAKPOINT_LEN_4;
536 attr.type = PERF_TYPE_BREAKPOINT;
537 attr.sample_period = 1;
539 return add_event(list, idx, &attr, NULL);
542 static int check_type_val(struct parse_events_term *term,
543 struct parse_events_error *err,
546 if (type == term->type_val)
550 err->idx = term->err_val;
551 if (type == PARSE_EVENTS__TERM_TYPE_NUM)
552 err->str = strdup("expected numeric value");
554 err->str = strdup("expected string value");
559 static int config_term(struct perf_event_attr *attr,
560 struct parse_events_term *term,
561 struct parse_events_error *err)
563 #define CHECK_TYPE_VAL(type) \
565 if (check_type_val(term, err, PARSE_EVENTS__TERM_TYPE_ ## type)) \
569 switch (term->type_term) {
570 case PARSE_EVENTS__TERM_TYPE_USER:
572 * Always succeed for sysfs terms, as we dont know
573 * at this point what type they need to have.
576 case PARSE_EVENTS__TERM_TYPE_CONFIG:
578 attr->config = term->val.num;
580 case PARSE_EVENTS__TERM_TYPE_CONFIG1:
582 attr->config1 = term->val.num;
584 case PARSE_EVENTS__TERM_TYPE_CONFIG2:
586 attr->config2 = term->val.num;
588 case PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD:
590 attr->sample_period = term->val.num;
592 case PARSE_EVENTS__TERM_TYPE_BRANCH_SAMPLE_TYPE:
594 * TODO uncomment when the field is available
595 * attr->branch_sample_type = term->val.num;
598 case PARSE_EVENTS__TERM_TYPE_NAME:
606 #undef CHECK_TYPE_VAL
609 static int config_attr(struct perf_event_attr *attr,
610 struct list_head *head,
611 struct parse_events_error *err)
613 struct parse_events_term *term;
615 list_for_each_entry(term, head, list)
616 if (config_term(attr, term, err))
622 int parse_events_add_numeric(struct parse_events_evlist *data,
623 struct list_head *list,
624 u32 type, u64 config,
625 struct list_head *head_config)
627 struct perf_event_attr attr;
629 memset(&attr, 0, sizeof(attr));
631 attr.config = config;
634 config_attr(&attr, head_config, data->error))
637 return add_event(list, &data->idx, &attr, NULL);
640 static int parse_events__is_name_term(struct parse_events_term *term)
642 return term->type_term == PARSE_EVENTS__TERM_TYPE_NAME;
645 static char *pmu_event_name(struct list_head *head_terms)
647 struct parse_events_term *term;
649 list_for_each_entry(term, head_terms, list)
650 if (parse_events__is_name_term(term))
651 return term->val.str;
656 int parse_events_add_pmu(struct parse_events_evlist *data,
657 struct list_head *list, char *name,
658 struct list_head *head_config)
660 struct perf_event_attr attr;
661 struct perf_pmu_info info;
662 struct perf_pmu *pmu;
663 struct perf_evsel *evsel;
665 pmu = perf_pmu__find(name);
669 if (pmu->default_config) {
670 memcpy(&attr, pmu->default_config,
671 sizeof(struct perf_event_attr));
673 memset(&attr, 0, sizeof(attr));
677 attr.type = pmu->type;
678 evsel = __add_event(list, &data->idx, &attr, NULL, pmu->cpus);
679 return evsel ? 0 : -ENOMEM;
682 if (perf_pmu__check_alias(pmu, head_config, &info))
686 * Configure hardcoded terms first, no need to check
687 * return value when called with fail == 0 ;)
689 if (config_attr(&attr, head_config, data->error))
692 if (perf_pmu__config(pmu, &attr, head_config, data->error))
695 evsel = __add_event(list, &data->idx, &attr,
696 pmu_event_name(head_config), pmu->cpus);
698 evsel->unit = info.unit;
699 evsel->scale = info.scale;
700 evsel->per_pkg = info.per_pkg;
701 evsel->snapshot = info.snapshot;
704 return evsel ? 0 : -ENOMEM;
707 int parse_events__modifier_group(struct list_head *list,
710 return parse_events__modifier_event(list, event_mod, true);
713 void parse_events__set_leader(char *name, struct list_head *list)
715 struct perf_evsel *leader;
717 __perf_evlist__set_leader(list);
718 leader = list_entry(list->next, struct perf_evsel, node);
719 leader->group_name = name ? strdup(name) : NULL;
722 /* list_event is assumed to point to malloc'ed memory */
723 void parse_events_update_lists(struct list_head *list_event,
724 struct list_head *list_all)
727 * Called for single event definition. Update the
728 * 'all event' list, and reinit the 'single event'
729 * list, for next event definition.
731 list_splice_tail(list_event, list_all);
735 struct event_modifier {
748 static int get_event_modifier(struct event_modifier *mod, char *str,
749 struct perf_evsel *evsel)
751 int eu = evsel ? evsel->attr.exclude_user : 0;
752 int ek = evsel ? evsel->attr.exclude_kernel : 0;
753 int eh = evsel ? evsel->attr.exclude_hv : 0;
754 int eH = evsel ? evsel->attr.exclude_host : 0;
755 int eG = evsel ? evsel->attr.exclude_guest : 0;
756 int eI = evsel ? evsel->attr.exclude_idle : 0;
757 int precise = evsel ? evsel->attr.precise_ip : 0;
759 int pinned = evsel ? evsel->attr.pinned : 0;
761 int exclude = eu | ek | eh;
762 int exclude_GH = evsel ? evsel->exclude_GH : 0;
764 memset(mod, 0, sizeof(*mod));
769 exclude = eu = ek = eh = 1;
771 } else if (*str == 'k') {
773 exclude = eu = ek = eh = 1;
775 } else if (*str == 'h') {
777 exclude = eu = ek = eh = 1;
779 } else if (*str == 'G') {
781 exclude_GH = eG = eH = 1;
783 } else if (*str == 'H') {
785 exclude_GH = eG = eH = 1;
787 } else if (*str == 'I') {
789 } else if (*str == 'p') {
791 /* use of precise requires exclude_guest */
794 } else if (*str == 'S') {
796 } else if (*str == 'D') {
807 * 0 - SAMPLE_IP can have arbitrary skid
808 * 1 - SAMPLE_IP must have constant skid
809 * 2 - SAMPLE_IP requested to have 0 skid
810 * 3 - SAMPLE_IP must have 0 skid
812 * See also PERF_RECORD_MISC_EXACT_IP
823 mod->precise = precise;
824 mod->exclude_GH = exclude_GH;
825 mod->sample_read = sample_read;
826 mod->pinned = pinned;
832 * Basic modifier sanity check to validate it contains only one
833 * instance of any modifier (apart from 'p') present.
835 static int check_modifier(char *str)
839 /* The sizeof includes 0 byte as well. */
840 if (strlen(str) > (sizeof("ukhGHpppSDI") - 1))
844 if (*p != 'p' && strchr(p + 1, *p))
852 int parse_events__modifier_event(struct list_head *list, char *str, bool add)
854 struct perf_evsel *evsel;
855 struct event_modifier mod;
860 if (check_modifier(str))
863 if (!add && get_event_modifier(&mod, str, NULL))
866 __evlist__for_each(list, evsel) {
867 if (add && get_event_modifier(&mod, str, evsel))
870 evsel->attr.exclude_user = mod.eu;
871 evsel->attr.exclude_kernel = mod.ek;
872 evsel->attr.exclude_hv = mod.eh;
873 evsel->attr.precise_ip = mod.precise;
874 evsel->attr.exclude_host = mod.eH;
875 evsel->attr.exclude_guest = mod.eG;
876 evsel->attr.exclude_idle = mod.eI;
877 evsel->exclude_GH = mod.exclude_GH;
878 evsel->sample_read = mod.sample_read;
880 if (perf_evsel__is_group_leader(evsel))
881 evsel->attr.pinned = mod.pinned;
887 int parse_events_name(struct list_head *list, char *name)
889 struct perf_evsel *evsel;
891 __evlist__for_each(list, evsel) {
893 evsel->name = strdup(name);
900 comp_pmu(const void *p1, const void *p2)
902 struct perf_pmu_event_symbol *pmu1 = (struct perf_pmu_event_symbol *) p1;
903 struct perf_pmu_event_symbol *pmu2 = (struct perf_pmu_event_symbol *) p2;
905 return strcmp(pmu1->symbol, pmu2->symbol);
908 static void perf_pmu__parse_cleanup(void)
910 if (perf_pmu_events_list_num > 0) {
911 struct perf_pmu_event_symbol *p;
914 for (i = 0; i < perf_pmu_events_list_num; i++) {
915 p = perf_pmu_events_list + i;
918 free(perf_pmu_events_list);
919 perf_pmu_events_list = NULL;
920 perf_pmu_events_list_num = 0;
924 #define SET_SYMBOL(str, stype) \
933 * Read the pmu events list from sysfs
934 * Save it into perf_pmu_events_list
936 static void perf_pmu__parse_init(void)
939 struct perf_pmu *pmu = NULL;
940 struct perf_pmu_alias *alias;
943 pmu = perf_pmu__find("cpu");
944 if ((pmu == NULL) || list_empty(&pmu->aliases)) {
945 perf_pmu_events_list_num = -1;
948 list_for_each_entry(alias, &pmu->aliases, list) {
949 if (strchr(alias->name, '-'))
953 perf_pmu_events_list = malloc(sizeof(struct perf_pmu_event_symbol) * len);
954 if (!perf_pmu_events_list)
956 perf_pmu_events_list_num = len;
959 list_for_each_entry(alias, &pmu->aliases, list) {
960 struct perf_pmu_event_symbol *p = perf_pmu_events_list + len;
961 char *tmp = strchr(alias->name, '-');
964 SET_SYMBOL(strndup(alias->name, tmp - alias->name),
965 PMU_EVENT_SYMBOL_PREFIX);
967 SET_SYMBOL(strdup(++tmp), PMU_EVENT_SYMBOL_SUFFIX);
970 SET_SYMBOL(strdup(alias->name), PMU_EVENT_SYMBOL);
974 qsort(perf_pmu_events_list, len,
975 sizeof(struct perf_pmu_event_symbol), comp_pmu);
979 perf_pmu__parse_cleanup();
982 enum perf_pmu_event_symbol_type
983 perf_pmu__parse_check(const char *name)
985 struct perf_pmu_event_symbol p, *r;
987 /* scan kernel pmu events from sysfs if needed */
988 if (perf_pmu_events_list_num == 0)
989 perf_pmu__parse_init();
991 * name "cpu" could be prefix of cpu-cycles or cpu// events.
992 * cpu-cycles has been handled by hardcode.
993 * So it must be cpu// events, not kernel pmu event.
995 if ((perf_pmu_events_list_num <= 0) || !strcmp(name, "cpu"))
996 return PMU_EVENT_SYMBOL_ERR;
998 p.symbol = strdup(name);
999 r = bsearch(&p, perf_pmu_events_list,
1000 (size_t) perf_pmu_events_list_num,
1001 sizeof(struct perf_pmu_event_symbol), comp_pmu);
1003 return r ? r->type : PMU_EVENT_SYMBOL_ERR;
1006 static int parse_events__scanner(const char *str, void *data, int start_token)
1008 YY_BUFFER_STATE buffer;
1012 ret = parse_events_lex_init_extra(start_token, &scanner);
1016 buffer = parse_events__scan_string(str, scanner);
1019 parse_events_debug = 1;
1021 ret = parse_events_parse(data, scanner);
1023 parse_events__flush_buffer(buffer, scanner);
1024 parse_events__delete_buffer(buffer, scanner);
1025 parse_events_lex_destroy(scanner);
1030 * parse event config string, return a list of event terms.
1032 int parse_events_terms(struct list_head *terms, const char *str)
1034 struct parse_events_terms data = {
1039 ret = parse_events__scanner(str, &data, PE_START_TERMS);
1041 list_splice(data.terms, terms);
1047 parse_events__free_terms(data.terms);
1051 int parse_events(struct perf_evlist *evlist, const char *str,
1052 struct parse_events_error *err)
1054 struct parse_events_evlist data = {
1055 .list = LIST_HEAD_INIT(data.list),
1056 .idx = evlist->nr_entries,
1061 ret = parse_events__scanner(str, &data, PE_START_EVENTS);
1062 perf_pmu__parse_cleanup();
1064 int entries = data.idx - evlist->nr_entries;
1065 perf_evlist__splice_list_tail(evlist, &data.list, entries);
1066 evlist->nr_groups += data.nr_groups;
1071 * There are 2 users - builtin-record and builtin-test objects.
1072 * Both call perf_evlist__delete in case of error, so we dont
1078 #define MAX_WIDTH 1000
1079 static int get_term_width(void)
1083 get_term_dimensions(&ws);
1084 return ws.ws_col > MAX_WIDTH ? MAX_WIDTH : ws.ws_col;
1087 static void parse_events_print_error(struct parse_events_error *err,
1090 const char *str = "invalid or unsupported event: ";
1091 char _buf[MAX_WIDTH];
1092 char *buf = (char *) event;
1096 /* -2 for extra '' in the final fprintf */
1097 int width = get_term_width() - 2;
1098 int len_event = strlen(event);
1099 int len_str, max_len, cut = 0;
1102 * Maximum error index indent, we will cut
1103 * the event string if it's bigger.
1105 int max_err_idx = 10;
1108 * Let's be specific with the message when
1109 * we have the precise error.
1111 str = "event syntax error: ";
1112 len_str = strlen(str);
1113 max_len = width - len_str;
1117 /* We're cutting from the beggining. */
1118 if (err->idx > max_err_idx)
1119 cut = err->idx - max_err_idx;
1121 strncpy(buf, event + cut, max_len);
1123 /* Mark cut parts with '..' on both sides. */
1125 buf[0] = buf[1] = '.';
1127 if ((len_event - cut) > max_len) {
1128 buf[max_len - 1] = buf[max_len - 2] = '.';
1132 idx = len_str + err->idx - cut;
1135 fprintf(stderr, "%s'%s'\n", str, buf);
1137 fprintf(stderr, "%*s\\___ %s\n", idx + 1, "", err->str);
1139 fprintf(stderr, "\n%s\n", err->help);
1144 fprintf(stderr, "Run 'perf list' for a list of valid events\n");
1149 int parse_events_option(const struct option *opt, const char *str,
1150 int unset __maybe_unused)
1152 struct perf_evlist *evlist = *(struct perf_evlist **)opt->value;
1153 struct parse_events_error err = { .idx = 0, };
1154 int ret = parse_events(evlist, str, &err);
1157 parse_events_print_error(&err, str);
1162 int parse_filter(const struct option *opt, const char *str,
1163 int unset __maybe_unused)
1165 struct perf_evlist *evlist = *(struct perf_evlist **)opt->value;
1166 struct perf_evsel *last = NULL;
1168 if (evlist->nr_entries > 0)
1169 last = perf_evlist__last(evlist);
1171 if (last == NULL || last->attr.type != PERF_TYPE_TRACEPOINT) {
1173 "--filter option should follow a -e tracepoint option\n");
1177 last->filter = strdup(str);
1178 if (last->filter == NULL) {
1179 fprintf(stderr, "not enough memory to hold filter string\n");
1186 static const char * const event_type_descriptors[] = {
1190 "Hardware cache event",
1191 "Raw hardware event descriptor",
1192 "Hardware breakpoint",
1195 static int cmp_string(const void *a, const void *b)
1197 const char * const *as = a;
1198 const char * const *bs = b;
1200 return strcmp(*as, *bs);
1204 * Print the events from <debugfs_mount_point>/tracing/events
1207 void print_tracepoint_events(const char *subsys_glob, const char *event_glob,
1210 DIR *sys_dir, *evt_dir;
1211 struct dirent *sys_next, *evt_next, sys_dirent, evt_dirent;
1212 char evt_path[MAXPATHLEN];
1213 char dir_path[MAXPATHLEN];
1214 char **evt_list = NULL;
1215 unsigned int evt_i = 0, evt_num = 0;
1216 bool evt_num_known = false;
1219 sys_dir = opendir(tracing_events_path);
1223 if (evt_num_known) {
1224 evt_list = zalloc(sizeof(char *) * evt_num);
1226 goto out_close_sys_dir;
1229 for_each_subsystem(sys_dir, sys_dirent, sys_next) {
1230 if (subsys_glob != NULL &&
1231 !strglobmatch(sys_dirent.d_name, subsys_glob))
1234 snprintf(dir_path, MAXPATHLEN, "%s/%s", tracing_events_path,
1236 evt_dir = opendir(dir_path);
1240 for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next) {
1241 if (event_glob != NULL &&
1242 !strglobmatch(evt_dirent.d_name, event_glob))
1245 if (!evt_num_known) {
1250 snprintf(evt_path, MAXPATHLEN, "%s:%s",
1251 sys_dirent.d_name, evt_dirent.d_name);
1253 evt_list[evt_i] = strdup(evt_path);
1254 if (evt_list[evt_i] == NULL)
1255 goto out_close_evt_dir;
1262 if (!evt_num_known) {
1263 evt_num_known = true;
1266 qsort(evt_list, evt_num, sizeof(char *), cmp_string);
1268 while (evt_i < evt_num) {
1270 printf("%s ", evt_list[evt_i++]);
1273 printf(" %-50s [%s]\n", evt_list[evt_i++],
1274 event_type_descriptors[PERF_TYPE_TRACEPOINT]);
1281 for (evt_i = 0; evt_i < evt_num; evt_i++)
1282 zfree(&evt_list[evt_i]);
1291 printf("FATAL: not enough memory to print %s\n",
1292 event_type_descriptors[PERF_TYPE_TRACEPOINT]);
1298 * Check whether event is in <debugfs_mount_point>/tracing/events
1301 int is_valid_tracepoint(const char *event_string)
1303 DIR *sys_dir, *evt_dir;
1304 struct dirent *sys_next, *evt_next, sys_dirent, evt_dirent;
1305 char evt_path[MAXPATHLEN];
1306 char dir_path[MAXPATHLEN];
1308 sys_dir = opendir(tracing_events_path);
1312 for_each_subsystem(sys_dir, sys_dirent, sys_next) {
1314 snprintf(dir_path, MAXPATHLEN, "%s/%s", tracing_events_path,
1316 evt_dir = opendir(dir_path);
1320 for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next) {
1321 snprintf(evt_path, MAXPATHLEN, "%s:%s",
1322 sys_dirent.d_name, evt_dirent.d_name);
1323 if (!strcmp(evt_path, event_string)) {
1335 static bool is_event_supported(u8 type, unsigned config)
1339 struct perf_evsel *evsel;
1340 struct perf_event_attr attr = {
1346 struct thread_map map;
1353 evsel = perf_evsel__new(&attr);
1355 open_return = perf_evsel__open(evsel, NULL, &tmap.map);
1356 ret = open_return >= 0;
1358 if (open_return == -EACCES) {
1360 * This happens if the paranoid value
1361 * /proc/sys/kernel/perf_event_paranoid is set to 2
1362 * Re-run with exclude_kernel set; we don't do that
1363 * by default as some ARM machines do not support it.
1366 evsel->attr.exclude_kernel = 1;
1367 ret = perf_evsel__open(evsel, NULL, &tmap.map) >= 0;
1369 perf_evsel__delete(evsel);
1375 int print_hwcache_events(const char *event_glob, bool name_only)
1377 unsigned int type, op, i, evt_i = 0, evt_num = 0;
1379 char **evt_list = NULL;
1380 bool evt_num_known = false;
1383 if (evt_num_known) {
1384 evt_list = zalloc(sizeof(char *) * evt_num);
1389 for (type = 0; type < PERF_COUNT_HW_CACHE_MAX; type++) {
1390 for (op = 0; op < PERF_COUNT_HW_CACHE_OP_MAX; op++) {
1391 /* skip invalid cache type */
1392 if (!perf_evsel__is_cache_op_valid(type, op))
1395 for (i = 0; i < PERF_COUNT_HW_CACHE_RESULT_MAX; i++) {
1396 __perf_evsel__hw_cache_type_op_res_name(type, op, i,
1397 name, sizeof(name));
1398 if (event_glob != NULL && !strglobmatch(name, event_glob))
1401 if (!is_event_supported(PERF_TYPE_HW_CACHE,
1402 type | (op << 8) | (i << 16)))
1405 if (!evt_num_known) {
1410 evt_list[evt_i] = strdup(name);
1411 if (evt_list[evt_i] == NULL)
1418 if (!evt_num_known) {
1419 evt_num_known = true;
1422 qsort(evt_list, evt_num, sizeof(char *), cmp_string);
1424 while (evt_i < evt_num) {
1426 printf("%s ", evt_list[evt_i++]);
1429 printf(" %-50s [%s]\n", evt_list[evt_i++],
1430 event_type_descriptors[PERF_TYPE_HW_CACHE]);
1437 for (evt_i = 0; evt_i < evt_num; evt_i++)
1438 zfree(&evt_list[evt_i]);
1443 printf("FATAL: not enough memory to print %s\n", event_type_descriptors[PERF_TYPE_HW_CACHE]);
1449 void print_symbol_events(const char *event_glob, unsigned type,
1450 struct event_symbol *syms, unsigned max,
1453 unsigned int i, evt_i = 0, evt_num = 0;
1454 char name[MAX_NAME_LEN];
1455 char **evt_list = NULL;
1456 bool evt_num_known = false;
1459 if (evt_num_known) {
1460 evt_list = zalloc(sizeof(char *) * evt_num);
1466 for (i = 0; i < max; i++, syms++) {
1468 if (event_glob != NULL &&
1469 !(strglobmatch(syms->symbol, event_glob) ||
1470 (syms->alias && strglobmatch(syms->alias, event_glob))))
1473 if (!is_event_supported(type, i))
1476 if (!evt_num_known) {
1481 if (!name_only && strlen(syms->alias))
1482 snprintf(name, MAX_NAME_LEN, "%s OR %s", syms->symbol, syms->alias);
1484 strncpy(name, syms->symbol, MAX_NAME_LEN);
1486 evt_list[evt_i] = strdup(name);
1487 if (evt_list[evt_i] == NULL)
1492 if (!evt_num_known) {
1493 evt_num_known = true;
1496 qsort(evt_list, evt_num, sizeof(char *), cmp_string);
1498 while (evt_i < evt_num) {
1500 printf("%s ", evt_list[evt_i++]);
1503 printf(" %-50s [%s]\n", evt_list[evt_i++], event_type_descriptors[type]);
1510 for (evt_i = 0; evt_i < evt_num; evt_i++)
1511 zfree(&evt_list[evt_i]);
1516 printf("FATAL: not enough memory to print %s\n", event_type_descriptors[type]);
1522 * Print the help text for the event symbols:
1524 void print_events(const char *event_glob, bool name_only)
1526 print_symbol_events(event_glob, PERF_TYPE_HARDWARE,
1527 event_symbols_hw, PERF_COUNT_HW_MAX, name_only);
1529 print_symbol_events(event_glob, PERF_TYPE_SOFTWARE,
1530 event_symbols_sw, PERF_COUNT_SW_MAX, name_only);
1532 print_hwcache_events(event_glob, name_only);
1534 print_pmu_events(event_glob, name_only);
1536 if (event_glob != NULL)
1540 printf(" %-50s [%s]\n",
1542 event_type_descriptors[PERF_TYPE_RAW]);
1543 printf(" %-50s [%s]\n",
1544 "cpu/t1=v1[,t2=v2,t3 ...]/modifier",
1545 event_type_descriptors[PERF_TYPE_RAW]);
1546 printf(" (see 'man perf-list' on how to encode it)\n");
1549 printf(" %-50s [%s]\n",
1550 "mem:<addr>[/len][:access]",
1551 event_type_descriptors[PERF_TYPE_BREAKPOINT]);
1555 print_tracepoint_events(NULL, NULL, name_only);
1558 int parse_events__is_hardcoded_term(struct parse_events_term *term)
1560 return term->type_term != PARSE_EVENTS__TERM_TYPE_USER;
1563 static int new_term(struct parse_events_term **_term, int type_val,
1564 int type_term, char *config,
1565 char *str, u64 num, int err_term, int err_val)
1567 struct parse_events_term *term;
1569 term = zalloc(sizeof(*term));
1573 INIT_LIST_HEAD(&term->list);
1574 term->type_val = type_val;
1575 term->type_term = type_term;
1576 term->config = config;
1577 term->err_term = err_term;
1578 term->err_val = err_val;
1581 case PARSE_EVENTS__TERM_TYPE_NUM:
1582 term->val.num = num;
1584 case PARSE_EVENTS__TERM_TYPE_STR:
1585 term->val.str = str;
1596 int parse_events_term__num(struct parse_events_term **term,
1597 int type_term, char *config, u64 num,
1598 void *loc_term_, void *loc_val_)
1600 YYLTYPE *loc_term = loc_term_;
1601 YYLTYPE *loc_val = loc_val_;
1603 return new_term(term, PARSE_EVENTS__TERM_TYPE_NUM, type_term,
1605 loc_term ? loc_term->first_column : 0,
1606 loc_val ? loc_val->first_column : 0);
1609 int parse_events_term__str(struct parse_events_term **term,
1610 int type_term, char *config, char *str,
1611 void *loc_term_, void *loc_val_)
1613 YYLTYPE *loc_term = loc_term_;
1614 YYLTYPE *loc_val = loc_val_;
1616 return new_term(term, PARSE_EVENTS__TERM_TYPE_STR, type_term,
1618 loc_term ? loc_term->first_column : 0,
1619 loc_val ? loc_val->first_column : 0);
1622 int parse_events_term__sym_hw(struct parse_events_term **term,
1623 char *config, unsigned idx)
1625 struct event_symbol *sym;
1627 BUG_ON(idx >= PERF_COUNT_HW_MAX);
1628 sym = &event_symbols_hw[idx];
1631 return new_term(term, PARSE_EVENTS__TERM_TYPE_STR,
1632 PARSE_EVENTS__TERM_TYPE_USER, config,
1633 (char *) sym->symbol, 0, 0, 0);
1635 return new_term(term, PARSE_EVENTS__TERM_TYPE_STR,
1636 PARSE_EVENTS__TERM_TYPE_USER,
1637 (char *) "event", (char *) sym->symbol,
1641 int parse_events_term__clone(struct parse_events_term **new,
1642 struct parse_events_term *term)
1644 return new_term(new, term->type_val, term->type_term, term->config,
1645 term->val.str, term->val.num,
1646 term->err_term, term->err_val);
1649 void parse_events__free_terms(struct list_head *terms)
1651 struct parse_events_term *term, *h;
1653 list_for_each_entry_safe(term, h, terms, list)
1657 void parse_events_evlist_error(struct parse_events_evlist *data,
1658 int idx, const char *str)
1660 struct parse_events_error *err = data->error;
1665 err->str = strdup(str);
1666 WARN_ONCE(!err->str, "WARNING: failed to allocate error string");