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)
281 struct perf_evsel *evsel;
283 event_attr_init(attr);
285 evsel = perf_evsel__new_idx(attr, (*idx)++);
290 evsel->cpus = cpu_map__get(cpus);
293 evsel->name = strdup(name);
294 list_add_tail(&evsel->node, list);
298 static int add_event(struct list_head *list, int *idx,
299 struct perf_event_attr *attr, char *name)
301 return __add_event(list, idx, attr, name, NULL) ? 0 : -ENOMEM;
304 static int parse_aliases(char *str, const char *names[][PERF_EVSEL__MAX_ALIASES], int size)
309 for (i = 0; i < size; i++) {
310 for (j = 0; j < PERF_EVSEL__MAX_ALIASES && names[i][j]; j++) {
311 n = strlen(names[i][j]);
312 if (n > longest && !strncasecmp(str, names[i][j], n))
322 int parse_events_add_cache(struct list_head *list, int *idx,
323 char *type, char *op_result1, char *op_result2)
325 struct perf_event_attr attr;
326 char name[MAX_NAME_LEN];
327 int cache_type = -1, cache_op = -1, cache_result = -1;
328 char *op_result[2] = { op_result1, op_result2 };
332 * No fallback - if we cannot get a clear cache type
335 cache_type = parse_aliases(type, perf_evsel__hw_cache,
336 PERF_COUNT_HW_CACHE_MAX);
337 if (cache_type == -1)
340 n = snprintf(name, MAX_NAME_LEN, "%s", type);
342 for (i = 0; (i < 2) && (op_result[i]); i++) {
343 char *str = op_result[i];
345 n += snprintf(name + n, MAX_NAME_LEN - n, "-%s", str);
347 if (cache_op == -1) {
348 cache_op = parse_aliases(str, perf_evsel__hw_cache_op,
349 PERF_COUNT_HW_CACHE_OP_MAX);
351 if (!perf_evsel__is_cache_op_valid(cache_type, cache_op))
357 if (cache_result == -1) {
358 cache_result = parse_aliases(str, perf_evsel__hw_cache_result,
359 PERF_COUNT_HW_CACHE_RESULT_MAX);
360 if (cache_result >= 0)
366 * Fall back to reads:
369 cache_op = PERF_COUNT_HW_CACHE_OP_READ;
372 * Fall back to accesses:
374 if (cache_result == -1)
375 cache_result = PERF_COUNT_HW_CACHE_RESULT_ACCESS;
377 memset(&attr, 0, sizeof(attr));
378 attr.config = cache_type | (cache_op << 8) | (cache_result << 16);
379 attr.type = PERF_TYPE_HW_CACHE;
380 return add_event(list, idx, &attr, name);
383 static int add_tracepoint(struct list_head *list, int *idx,
384 char *sys_name, char *evt_name)
386 struct perf_evsel *evsel;
388 evsel = perf_evsel__newtp_idx(sys_name, evt_name, (*idx)++);
392 list_add_tail(&evsel->node, list);
397 static int add_tracepoint_multi_event(struct list_head *list, int *idx,
398 char *sys_name, char *evt_name)
400 char evt_path[MAXPATHLEN];
401 struct dirent *evt_ent;
405 snprintf(evt_path, MAXPATHLEN, "%s/%s", tracing_events_path, sys_name);
406 evt_dir = opendir(evt_path);
408 perror("Can't open event dir");
412 while (!ret && (evt_ent = readdir(evt_dir))) {
413 if (!strcmp(evt_ent->d_name, ".")
414 || !strcmp(evt_ent->d_name, "..")
415 || !strcmp(evt_ent->d_name, "enable")
416 || !strcmp(evt_ent->d_name, "filter"))
419 if (!strglobmatch(evt_ent->d_name, evt_name))
422 ret = add_tracepoint(list, idx, sys_name, evt_ent->d_name);
429 static int add_tracepoint_event(struct list_head *list, int *idx,
430 char *sys_name, char *evt_name)
432 return strpbrk(evt_name, "*?") ?
433 add_tracepoint_multi_event(list, idx, sys_name, evt_name) :
434 add_tracepoint(list, idx, sys_name, evt_name);
437 static int add_tracepoint_multi_sys(struct list_head *list, int *idx,
438 char *sys_name, char *evt_name)
440 struct dirent *events_ent;
444 events_dir = opendir(tracing_events_path);
446 perror("Can't open event dir");
450 while (!ret && (events_ent = readdir(events_dir))) {
451 if (!strcmp(events_ent->d_name, ".")
452 || !strcmp(events_ent->d_name, "..")
453 || !strcmp(events_ent->d_name, "enable")
454 || !strcmp(events_ent->d_name, "header_event")
455 || !strcmp(events_ent->d_name, "header_page"))
458 if (!strglobmatch(events_ent->d_name, sys_name))
461 ret = add_tracepoint_event(list, idx, events_ent->d_name,
465 closedir(events_dir);
469 int parse_events_add_tracepoint(struct list_head *list, int *idx,
470 char *sys, char *event)
472 if (strpbrk(sys, "*?"))
473 return add_tracepoint_multi_sys(list, idx, sys, event);
475 return add_tracepoint_event(list, idx, sys, event);
479 parse_breakpoint_type(const char *type, struct perf_event_attr *attr)
483 for (i = 0; i < 3; i++) {
484 if (!type || !type[i])
487 #define CHECK_SET_TYPE(bit) \
489 if (attr->bp_type & bit) \
492 attr->bp_type |= bit; \
497 CHECK_SET_TYPE(HW_BREAKPOINT_R);
500 CHECK_SET_TYPE(HW_BREAKPOINT_W);
503 CHECK_SET_TYPE(HW_BREAKPOINT_X);
510 #undef CHECK_SET_TYPE
512 if (!attr->bp_type) /* Default */
513 attr->bp_type = HW_BREAKPOINT_R | HW_BREAKPOINT_W;
518 int parse_events_add_breakpoint(struct list_head *list, int *idx,
519 void *ptr, char *type, u64 len)
521 struct perf_event_attr attr;
523 memset(&attr, 0, sizeof(attr));
524 attr.bp_addr = (unsigned long) ptr;
526 if (parse_breakpoint_type(type, &attr))
529 /* Provide some defaults if len is not specified */
531 if (attr.bp_type == HW_BREAKPOINT_X)
534 len = HW_BREAKPOINT_LEN_4;
539 attr.type = PERF_TYPE_BREAKPOINT;
540 attr.sample_period = 1;
542 return add_event(list, idx, &attr, NULL);
545 static int check_type_val(struct parse_events_term *term,
546 struct parse_events_error *err,
549 if (type == term->type_val)
553 err->idx = term->err_val;
554 if (type == PARSE_EVENTS__TERM_TYPE_NUM)
555 err->str = strdup("expected numeric value");
557 err->str = strdup("expected string value");
562 static int config_term(struct perf_event_attr *attr,
563 struct parse_events_term *term,
564 struct parse_events_error *err)
566 #define CHECK_TYPE_VAL(type) \
568 if (check_type_val(term, err, PARSE_EVENTS__TERM_TYPE_ ## type)) \
572 switch (term->type_term) {
573 case PARSE_EVENTS__TERM_TYPE_USER:
575 * Always succeed for sysfs terms, as we dont know
576 * at this point what type they need to have.
579 case PARSE_EVENTS__TERM_TYPE_CONFIG:
581 attr->config = term->val.num;
583 case PARSE_EVENTS__TERM_TYPE_CONFIG1:
585 attr->config1 = term->val.num;
587 case PARSE_EVENTS__TERM_TYPE_CONFIG2:
589 attr->config2 = term->val.num;
591 case PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD:
593 attr->sample_period = term->val.num;
595 case PARSE_EVENTS__TERM_TYPE_BRANCH_SAMPLE_TYPE:
597 * TODO uncomment when the field is available
598 * attr->branch_sample_type = term->val.num;
601 case PARSE_EVENTS__TERM_TYPE_NAME:
609 #undef CHECK_TYPE_VAL
612 static int config_attr(struct perf_event_attr *attr,
613 struct list_head *head,
614 struct parse_events_error *err)
616 struct parse_events_term *term;
618 list_for_each_entry(term, head, list)
619 if (config_term(attr, term, err))
625 int parse_events_add_numeric(struct parse_events_evlist *data,
626 struct list_head *list,
627 u32 type, u64 config,
628 struct list_head *head_config)
630 struct perf_event_attr attr;
632 memset(&attr, 0, sizeof(attr));
634 attr.config = config;
637 config_attr(&attr, head_config, data->error))
640 return add_event(list, &data->idx, &attr, NULL);
643 static int parse_events__is_name_term(struct parse_events_term *term)
645 return term->type_term == PARSE_EVENTS__TERM_TYPE_NAME;
648 static char *pmu_event_name(struct list_head *head_terms)
650 struct parse_events_term *term;
652 list_for_each_entry(term, head_terms, list)
653 if (parse_events__is_name_term(term))
654 return term->val.str;
659 int parse_events_add_pmu(struct parse_events_evlist *data,
660 struct list_head *list, char *name,
661 struct list_head *head_config)
663 struct perf_event_attr attr;
664 struct perf_pmu_info info;
665 struct perf_pmu *pmu;
666 struct perf_evsel *evsel;
668 pmu = perf_pmu__find(name);
672 if (pmu->default_config) {
673 memcpy(&attr, pmu->default_config,
674 sizeof(struct perf_event_attr));
676 memset(&attr, 0, sizeof(attr));
680 attr.type = pmu->type;
681 evsel = __add_event(list, &data->idx, &attr, NULL, pmu->cpus);
682 return evsel ? 0 : -ENOMEM;
685 if (perf_pmu__check_alias(pmu, head_config, &info))
689 * Configure hardcoded terms first, no need to check
690 * return value when called with fail == 0 ;)
692 if (config_attr(&attr, head_config, data->error))
695 if (perf_pmu__config(pmu, &attr, head_config, data->error))
698 evsel = __add_event(list, &data->idx, &attr,
699 pmu_event_name(head_config), pmu->cpus);
701 evsel->unit = info.unit;
702 evsel->scale = info.scale;
703 evsel->per_pkg = info.per_pkg;
704 evsel->snapshot = info.snapshot;
707 return evsel ? 0 : -ENOMEM;
710 int parse_events__modifier_group(struct list_head *list,
713 return parse_events__modifier_event(list, event_mod, true);
716 void parse_events__set_leader(char *name, struct list_head *list)
718 struct perf_evsel *leader;
720 __perf_evlist__set_leader(list);
721 leader = list_entry(list->next, struct perf_evsel, node);
722 leader->group_name = name ? strdup(name) : NULL;
725 /* list_event is assumed to point to malloc'ed memory */
726 void parse_events_update_lists(struct list_head *list_event,
727 struct list_head *list_all)
730 * Called for single event definition. Update the
731 * 'all event' list, and reinit the 'single event'
732 * list, for next event definition.
734 list_splice_tail(list_event, list_all);
738 struct event_modifier {
751 static int get_event_modifier(struct event_modifier *mod, char *str,
752 struct perf_evsel *evsel)
754 int eu = evsel ? evsel->attr.exclude_user : 0;
755 int ek = evsel ? evsel->attr.exclude_kernel : 0;
756 int eh = evsel ? evsel->attr.exclude_hv : 0;
757 int eH = evsel ? evsel->attr.exclude_host : 0;
758 int eG = evsel ? evsel->attr.exclude_guest : 0;
759 int eI = evsel ? evsel->attr.exclude_idle : 0;
760 int precise = evsel ? evsel->attr.precise_ip : 0;
762 int pinned = evsel ? evsel->attr.pinned : 0;
764 int exclude = eu | ek | eh;
765 int exclude_GH = evsel ? evsel->exclude_GH : 0;
767 memset(mod, 0, sizeof(*mod));
772 exclude = eu = ek = eh = 1;
774 } else if (*str == 'k') {
776 exclude = eu = ek = eh = 1;
778 } else if (*str == 'h') {
780 exclude = eu = ek = eh = 1;
782 } else if (*str == 'G') {
784 exclude_GH = eG = eH = 1;
786 } else if (*str == 'H') {
788 exclude_GH = eG = eH = 1;
790 } else if (*str == 'I') {
792 } else if (*str == 'p') {
794 /* use of precise requires exclude_guest */
797 } else if (*str == 'S') {
799 } else if (*str == 'D') {
810 * 0 - SAMPLE_IP can have arbitrary skid
811 * 1 - SAMPLE_IP must have constant skid
812 * 2 - SAMPLE_IP requested to have 0 skid
813 * 3 - SAMPLE_IP must have 0 skid
815 * See also PERF_RECORD_MISC_EXACT_IP
826 mod->precise = precise;
827 mod->exclude_GH = exclude_GH;
828 mod->sample_read = sample_read;
829 mod->pinned = pinned;
835 * Basic modifier sanity check to validate it contains only one
836 * instance of any modifier (apart from 'p') present.
838 static int check_modifier(char *str)
842 /* The sizeof includes 0 byte as well. */
843 if (strlen(str) > (sizeof("ukhGHpppSDI") - 1))
847 if (*p != 'p' && strchr(p + 1, *p))
855 int parse_events__modifier_event(struct list_head *list, char *str, bool add)
857 struct perf_evsel *evsel;
858 struct event_modifier mod;
863 if (check_modifier(str))
866 if (!add && get_event_modifier(&mod, str, NULL))
869 __evlist__for_each(list, evsel) {
870 if (add && get_event_modifier(&mod, str, evsel))
873 evsel->attr.exclude_user = mod.eu;
874 evsel->attr.exclude_kernel = mod.ek;
875 evsel->attr.exclude_hv = mod.eh;
876 evsel->attr.precise_ip = mod.precise;
877 evsel->attr.exclude_host = mod.eH;
878 evsel->attr.exclude_guest = mod.eG;
879 evsel->attr.exclude_idle = mod.eI;
880 evsel->exclude_GH = mod.exclude_GH;
881 evsel->sample_read = mod.sample_read;
883 if (perf_evsel__is_group_leader(evsel))
884 evsel->attr.pinned = mod.pinned;
890 int parse_events_name(struct list_head *list, char *name)
892 struct perf_evsel *evsel;
894 __evlist__for_each(list, evsel) {
896 evsel->name = strdup(name);
903 comp_pmu(const void *p1, const void *p2)
905 struct perf_pmu_event_symbol *pmu1 = (struct perf_pmu_event_symbol *) p1;
906 struct perf_pmu_event_symbol *pmu2 = (struct perf_pmu_event_symbol *) p2;
908 return strcmp(pmu1->symbol, pmu2->symbol);
911 static void perf_pmu__parse_cleanup(void)
913 if (perf_pmu_events_list_num > 0) {
914 struct perf_pmu_event_symbol *p;
917 for (i = 0; i < perf_pmu_events_list_num; i++) {
918 p = perf_pmu_events_list + i;
921 free(perf_pmu_events_list);
922 perf_pmu_events_list = NULL;
923 perf_pmu_events_list_num = 0;
927 #define SET_SYMBOL(str, stype) \
936 * Read the pmu events list from sysfs
937 * Save it into perf_pmu_events_list
939 static void perf_pmu__parse_init(void)
942 struct perf_pmu *pmu = NULL;
943 struct perf_pmu_alias *alias;
946 pmu = perf_pmu__find("cpu");
947 if ((pmu == NULL) || list_empty(&pmu->aliases)) {
948 perf_pmu_events_list_num = -1;
951 list_for_each_entry(alias, &pmu->aliases, list) {
952 if (strchr(alias->name, '-'))
956 perf_pmu_events_list = malloc(sizeof(struct perf_pmu_event_symbol) * len);
957 if (!perf_pmu_events_list)
959 perf_pmu_events_list_num = len;
962 list_for_each_entry(alias, &pmu->aliases, list) {
963 struct perf_pmu_event_symbol *p = perf_pmu_events_list + len;
964 char *tmp = strchr(alias->name, '-');
967 SET_SYMBOL(strndup(alias->name, tmp - alias->name),
968 PMU_EVENT_SYMBOL_PREFIX);
970 SET_SYMBOL(strdup(++tmp), PMU_EVENT_SYMBOL_SUFFIX);
973 SET_SYMBOL(strdup(alias->name), PMU_EVENT_SYMBOL);
977 qsort(perf_pmu_events_list, len,
978 sizeof(struct perf_pmu_event_symbol), comp_pmu);
982 perf_pmu__parse_cleanup();
985 enum perf_pmu_event_symbol_type
986 perf_pmu__parse_check(const char *name)
988 struct perf_pmu_event_symbol p, *r;
990 /* scan kernel pmu events from sysfs if needed */
991 if (perf_pmu_events_list_num == 0)
992 perf_pmu__parse_init();
994 * name "cpu" could be prefix of cpu-cycles or cpu// events.
995 * cpu-cycles has been handled by hardcode.
996 * So it must be cpu// events, not kernel pmu event.
998 if ((perf_pmu_events_list_num <= 0) || !strcmp(name, "cpu"))
999 return PMU_EVENT_SYMBOL_ERR;
1001 p.symbol = strdup(name);
1002 r = bsearch(&p, perf_pmu_events_list,
1003 (size_t) perf_pmu_events_list_num,
1004 sizeof(struct perf_pmu_event_symbol), comp_pmu);
1006 return r ? r->type : PMU_EVENT_SYMBOL_ERR;
1009 static int parse_events__scanner(const char *str, void *data, int start_token)
1011 YY_BUFFER_STATE buffer;
1015 ret = parse_events_lex_init_extra(start_token, &scanner);
1019 buffer = parse_events__scan_string(str, scanner);
1022 parse_events_debug = 1;
1024 ret = parse_events_parse(data, scanner);
1026 parse_events__flush_buffer(buffer, scanner);
1027 parse_events__delete_buffer(buffer, scanner);
1028 parse_events_lex_destroy(scanner);
1033 * parse event config string, return a list of event terms.
1035 int parse_events_terms(struct list_head *terms, const char *str)
1037 struct parse_events_terms data = {
1042 ret = parse_events__scanner(str, &data, PE_START_TERMS);
1044 list_splice(data.terms, terms);
1050 parse_events__free_terms(data.terms);
1054 int parse_events(struct perf_evlist *evlist, const char *str,
1055 struct parse_events_error *err)
1057 struct parse_events_evlist data = {
1058 .list = LIST_HEAD_INIT(data.list),
1059 .idx = evlist->nr_entries,
1064 ret = parse_events__scanner(str, &data, PE_START_EVENTS);
1065 perf_pmu__parse_cleanup();
1067 int entries = data.idx - evlist->nr_entries;
1068 perf_evlist__splice_list_tail(evlist, &data.list, entries);
1069 evlist->nr_groups += data.nr_groups;
1074 * There are 2 users - builtin-record and builtin-test objects.
1075 * Both call perf_evlist__delete in case of error, so we dont
1081 #define MAX_WIDTH 1000
1082 static int get_term_width(void)
1086 get_term_dimensions(&ws);
1087 return ws.ws_col > MAX_WIDTH ? MAX_WIDTH : ws.ws_col;
1090 static void parse_events_print_error(struct parse_events_error *err,
1093 const char *str = "invalid or unsupported event: ";
1094 char _buf[MAX_WIDTH];
1095 char *buf = (char *) event;
1099 /* -2 for extra '' in the final fprintf */
1100 int width = get_term_width() - 2;
1101 int len_event = strlen(event);
1102 int len_str, max_len, cut = 0;
1105 * Maximum error index indent, we will cut
1106 * the event string if it's bigger.
1108 int max_err_idx = 10;
1111 * Let's be specific with the message when
1112 * we have the precise error.
1114 str = "event syntax error: ";
1115 len_str = strlen(str);
1116 max_len = width - len_str;
1120 /* We're cutting from the beggining. */
1121 if (err->idx > max_err_idx)
1122 cut = err->idx - max_err_idx;
1124 strncpy(buf, event + cut, max_len);
1126 /* Mark cut parts with '..' on both sides. */
1128 buf[0] = buf[1] = '.';
1130 if ((len_event - cut) > max_len) {
1131 buf[max_len - 1] = buf[max_len - 2] = '.';
1135 idx = len_str + err->idx - cut;
1138 fprintf(stderr, "%s'%s'\n", str, buf);
1140 fprintf(stderr, "%*s\\___ %s\n", idx + 1, "", err->str);
1142 fprintf(stderr, "\n%s\n", err->help);
1147 fprintf(stderr, "Run 'perf list' for a list of valid events\n");
1152 int parse_events_option(const struct option *opt, const char *str,
1153 int unset __maybe_unused)
1155 struct perf_evlist *evlist = *(struct perf_evlist **)opt->value;
1156 struct parse_events_error err = { .idx = 0, };
1157 int ret = parse_events(evlist, str, &err);
1160 parse_events_print_error(&err, str);
1165 int parse_filter(const struct option *opt, const char *str,
1166 int unset __maybe_unused)
1168 struct perf_evlist *evlist = *(struct perf_evlist **)opt->value;
1169 struct perf_evsel *last = NULL;
1171 if (evlist->nr_entries > 0)
1172 last = perf_evlist__last(evlist);
1174 if (last == NULL || last->attr.type != PERF_TYPE_TRACEPOINT) {
1176 "--filter option should follow a -e tracepoint option\n");
1180 last->filter = strdup(str);
1181 if (last->filter == NULL) {
1182 fprintf(stderr, "not enough memory to hold filter string\n");
1189 static const char * const event_type_descriptors[] = {
1193 "Hardware cache event",
1194 "Raw hardware event descriptor",
1195 "Hardware breakpoint",
1198 static int cmp_string(const void *a, const void *b)
1200 const char * const *as = a;
1201 const char * const *bs = b;
1203 return strcmp(*as, *bs);
1207 * Print the events from <debugfs_mount_point>/tracing/events
1210 void print_tracepoint_events(const char *subsys_glob, const char *event_glob,
1213 DIR *sys_dir, *evt_dir;
1214 struct dirent *sys_next, *evt_next, sys_dirent, evt_dirent;
1215 char evt_path[MAXPATHLEN];
1216 char dir_path[MAXPATHLEN];
1217 char **evt_list = NULL;
1218 unsigned int evt_i = 0, evt_num = 0;
1219 bool evt_num_known = false;
1222 sys_dir = opendir(tracing_events_path);
1226 if (evt_num_known) {
1227 evt_list = zalloc(sizeof(char *) * evt_num);
1229 goto out_close_sys_dir;
1232 for_each_subsystem(sys_dir, sys_dirent, sys_next) {
1233 if (subsys_glob != NULL &&
1234 !strglobmatch(sys_dirent.d_name, subsys_glob))
1237 snprintf(dir_path, MAXPATHLEN, "%s/%s", tracing_events_path,
1239 evt_dir = opendir(dir_path);
1243 for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next) {
1244 if (event_glob != NULL &&
1245 !strglobmatch(evt_dirent.d_name, event_glob))
1248 if (!evt_num_known) {
1253 snprintf(evt_path, MAXPATHLEN, "%s:%s",
1254 sys_dirent.d_name, evt_dirent.d_name);
1256 evt_list[evt_i] = strdup(evt_path);
1257 if (evt_list[evt_i] == NULL)
1258 goto out_close_evt_dir;
1265 if (!evt_num_known) {
1266 evt_num_known = true;
1269 qsort(evt_list, evt_num, sizeof(char *), cmp_string);
1271 while (evt_i < evt_num) {
1273 printf("%s ", evt_list[evt_i++]);
1276 printf(" %-50s [%s]\n", evt_list[evt_i++],
1277 event_type_descriptors[PERF_TYPE_TRACEPOINT]);
1284 for (evt_i = 0; evt_i < evt_num; evt_i++)
1285 zfree(&evt_list[evt_i]);
1294 printf("FATAL: not enough memory to print %s\n",
1295 event_type_descriptors[PERF_TYPE_TRACEPOINT]);
1301 * Check whether event is in <debugfs_mount_point>/tracing/events
1304 int is_valid_tracepoint(const char *event_string)
1306 DIR *sys_dir, *evt_dir;
1307 struct dirent *sys_next, *evt_next, sys_dirent, evt_dirent;
1308 char evt_path[MAXPATHLEN];
1309 char dir_path[MAXPATHLEN];
1311 sys_dir = opendir(tracing_events_path);
1315 for_each_subsystem(sys_dir, sys_dirent, sys_next) {
1317 snprintf(dir_path, MAXPATHLEN, "%s/%s", tracing_events_path,
1319 evt_dir = opendir(dir_path);
1323 for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next) {
1324 snprintf(evt_path, MAXPATHLEN, "%s:%s",
1325 sys_dirent.d_name, evt_dirent.d_name);
1326 if (!strcmp(evt_path, event_string)) {
1338 static bool is_event_supported(u8 type, unsigned config)
1342 struct perf_evsel *evsel;
1343 struct perf_event_attr attr = {
1349 struct thread_map map;
1356 evsel = perf_evsel__new(&attr);
1358 open_return = perf_evsel__open(evsel, NULL, &tmap.map);
1359 ret = open_return >= 0;
1361 if (open_return == -EACCES) {
1363 * This happens if the paranoid value
1364 * /proc/sys/kernel/perf_event_paranoid is set to 2
1365 * Re-run with exclude_kernel set; we don't do that
1366 * by default as some ARM machines do not support it.
1369 evsel->attr.exclude_kernel = 1;
1370 ret = perf_evsel__open(evsel, NULL, &tmap.map) >= 0;
1372 perf_evsel__delete(evsel);
1378 int print_hwcache_events(const char *event_glob, bool name_only)
1380 unsigned int type, op, i, evt_i = 0, evt_num = 0;
1382 char **evt_list = NULL;
1383 bool evt_num_known = false;
1386 if (evt_num_known) {
1387 evt_list = zalloc(sizeof(char *) * evt_num);
1392 for (type = 0; type < PERF_COUNT_HW_CACHE_MAX; type++) {
1393 for (op = 0; op < PERF_COUNT_HW_CACHE_OP_MAX; op++) {
1394 /* skip invalid cache type */
1395 if (!perf_evsel__is_cache_op_valid(type, op))
1398 for (i = 0; i < PERF_COUNT_HW_CACHE_RESULT_MAX; i++) {
1399 __perf_evsel__hw_cache_type_op_res_name(type, op, i,
1400 name, sizeof(name));
1401 if (event_glob != NULL && !strglobmatch(name, event_glob))
1404 if (!is_event_supported(PERF_TYPE_HW_CACHE,
1405 type | (op << 8) | (i << 16)))
1408 if (!evt_num_known) {
1413 evt_list[evt_i] = strdup(name);
1414 if (evt_list[evt_i] == NULL)
1421 if (!evt_num_known) {
1422 evt_num_known = true;
1425 qsort(evt_list, evt_num, sizeof(char *), cmp_string);
1427 while (evt_i < evt_num) {
1429 printf("%s ", evt_list[evt_i++]);
1432 printf(" %-50s [%s]\n", evt_list[evt_i++],
1433 event_type_descriptors[PERF_TYPE_HW_CACHE]);
1440 for (evt_i = 0; evt_i < evt_num; evt_i++)
1441 zfree(&evt_list[evt_i]);
1446 printf("FATAL: not enough memory to print %s\n", event_type_descriptors[PERF_TYPE_HW_CACHE]);
1452 void print_symbol_events(const char *event_glob, unsigned type,
1453 struct event_symbol *syms, unsigned max,
1456 unsigned int i, evt_i = 0, evt_num = 0;
1457 char name[MAX_NAME_LEN];
1458 char **evt_list = NULL;
1459 bool evt_num_known = false;
1462 if (evt_num_known) {
1463 evt_list = zalloc(sizeof(char *) * evt_num);
1469 for (i = 0; i < max; i++, syms++) {
1471 if (event_glob != NULL &&
1472 !(strglobmatch(syms->symbol, event_glob) ||
1473 (syms->alias && strglobmatch(syms->alias, event_glob))))
1476 if (!is_event_supported(type, i))
1479 if (!evt_num_known) {
1484 if (!name_only && strlen(syms->alias))
1485 snprintf(name, MAX_NAME_LEN, "%s OR %s", syms->symbol, syms->alias);
1487 strncpy(name, syms->symbol, MAX_NAME_LEN);
1489 evt_list[evt_i] = strdup(name);
1490 if (evt_list[evt_i] == NULL)
1495 if (!evt_num_known) {
1496 evt_num_known = true;
1499 qsort(evt_list, evt_num, sizeof(char *), cmp_string);
1501 while (evt_i < evt_num) {
1503 printf("%s ", evt_list[evt_i++]);
1506 printf(" %-50s [%s]\n", evt_list[evt_i++], event_type_descriptors[type]);
1513 for (evt_i = 0; evt_i < evt_num; evt_i++)
1514 zfree(&evt_list[evt_i]);
1519 printf("FATAL: not enough memory to print %s\n", event_type_descriptors[type]);
1525 * Print the help text for the event symbols:
1527 void print_events(const char *event_glob, bool name_only)
1529 print_symbol_events(event_glob, PERF_TYPE_HARDWARE,
1530 event_symbols_hw, PERF_COUNT_HW_MAX, name_only);
1532 print_symbol_events(event_glob, PERF_TYPE_SOFTWARE,
1533 event_symbols_sw, PERF_COUNT_SW_MAX, name_only);
1535 print_hwcache_events(event_glob, name_only);
1537 print_pmu_events(event_glob, name_only);
1539 if (event_glob != NULL)
1543 printf(" %-50s [%s]\n",
1545 event_type_descriptors[PERF_TYPE_RAW]);
1546 printf(" %-50s [%s]\n",
1547 "cpu/t1=v1[,t2=v2,t3 ...]/modifier",
1548 event_type_descriptors[PERF_TYPE_RAW]);
1549 printf(" (see 'man perf-list' on how to encode it)\n");
1552 printf(" %-50s [%s]\n",
1553 "mem:<addr>[/len][:access]",
1554 event_type_descriptors[PERF_TYPE_BREAKPOINT]);
1558 print_tracepoint_events(NULL, NULL, name_only);
1561 int parse_events__is_hardcoded_term(struct parse_events_term *term)
1563 return term->type_term != PARSE_EVENTS__TERM_TYPE_USER;
1566 static int new_term(struct parse_events_term **_term, int type_val,
1567 int type_term, char *config,
1568 char *str, u64 num, int err_term, int err_val)
1570 struct parse_events_term *term;
1572 term = zalloc(sizeof(*term));
1576 INIT_LIST_HEAD(&term->list);
1577 term->type_val = type_val;
1578 term->type_term = type_term;
1579 term->config = config;
1580 term->err_term = err_term;
1581 term->err_val = err_val;
1584 case PARSE_EVENTS__TERM_TYPE_NUM:
1585 term->val.num = num;
1587 case PARSE_EVENTS__TERM_TYPE_STR:
1588 term->val.str = str;
1599 int parse_events_term__num(struct parse_events_term **term,
1600 int type_term, char *config, u64 num,
1601 void *loc_term_, void *loc_val_)
1603 YYLTYPE *loc_term = loc_term_;
1604 YYLTYPE *loc_val = loc_val_;
1606 return new_term(term, PARSE_EVENTS__TERM_TYPE_NUM, type_term,
1608 loc_term ? loc_term->first_column : 0,
1609 loc_val ? loc_val->first_column : 0);
1612 int parse_events_term__str(struct parse_events_term **term,
1613 int type_term, char *config, char *str,
1614 void *loc_term_, void *loc_val_)
1616 YYLTYPE *loc_term = loc_term_;
1617 YYLTYPE *loc_val = loc_val_;
1619 return new_term(term, PARSE_EVENTS__TERM_TYPE_STR, type_term,
1621 loc_term ? loc_term->first_column : 0,
1622 loc_val ? loc_val->first_column : 0);
1625 int parse_events_term__sym_hw(struct parse_events_term **term,
1626 char *config, unsigned idx)
1628 struct event_symbol *sym;
1630 BUG_ON(idx >= PERF_COUNT_HW_MAX);
1631 sym = &event_symbols_hw[idx];
1634 return new_term(term, PARSE_EVENTS__TERM_TYPE_STR,
1635 PARSE_EVENTS__TERM_TYPE_USER, config,
1636 (char *) sym->symbol, 0, 0, 0);
1638 return new_term(term, PARSE_EVENTS__TERM_TYPE_STR,
1639 PARSE_EVENTS__TERM_TYPE_USER,
1640 (char *) "event", (char *) sym->symbol,
1644 int parse_events_term__clone(struct parse_events_term **new,
1645 struct parse_events_term *term)
1647 return new_term(new, term->type_val, term->type_term, term->config,
1648 term->val.str, term->val.num,
1649 term->err_term, term->err_val);
1652 void parse_events__free_terms(struct list_head *terms)
1654 struct parse_events_term *term, *h;
1656 list_for_each_entry_safe(term, h, terms, list)
1660 void parse_events_evlist_error(struct parse_events_evlist *data,
1661 int idx, const char *str)
1663 struct parse_events_error *err = data->error;
1668 err->str = strdup(str);
1669 WARN_ONCE(!err->str, "WARNING: failed to allocate error string");