xref: /openbmc/linux/tools/perf/util/parse-events.c (revision afb46f79)
1 #include <linux/hw_breakpoint.h>
2 #include "util.h"
3 #include "../perf.h"
4 #include "evlist.h"
5 #include "evsel.h"
6 #include "parse-options.h"
7 #include "parse-events.h"
8 #include "exec_cmd.h"
9 #include "linux/string.h"
10 #include "symbol.h"
11 #include "cache.h"
12 #include "header.h"
13 #include <api/fs/debugfs.h>
14 #include "parse-events-bison.h"
15 #define YY_EXTRA_TYPE int
16 #include "parse-events-flex.h"
17 #include "pmu.h"
18 #include "thread_map.h"
19 
20 #define MAX_NAME_LEN 100
21 
22 struct event_symbol {
23 	const char	*symbol;
24 	const char	*alias;
25 };
26 
27 #ifdef PARSER_DEBUG
28 extern int parse_events_debug;
29 #endif
30 int parse_events_parse(void *data, void *scanner);
31 
32 static struct event_symbol event_symbols_hw[PERF_COUNT_HW_MAX] = {
33 	[PERF_COUNT_HW_CPU_CYCLES] = {
34 		.symbol = "cpu-cycles",
35 		.alias  = "cycles",
36 	},
37 	[PERF_COUNT_HW_INSTRUCTIONS] = {
38 		.symbol = "instructions",
39 		.alias  = "",
40 	},
41 	[PERF_COUNT_HW_CACHE_REFERENCES] = {
42 		.symbol = "cache-references",
43 		.alias  = "",
44 	},
45 	[PERF_COUNT_HW_CACHE_MISSES] = {
46 		.symbol = "cache-misses",
47 		.alias  = "",
48 	},
49 	[PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = {
50 		.symbol = "branch-instructions",
51 		.alias  = "branches",
52 	},
53 	[PERF_COUNT_HW_BRANCH_MISSES] = {
54 		.symbol = "branch-misses",
55 		.alias  = "",
56 	},
57 	[PERF_COUNT_HW_BUS_CYCLES] = {
58 		.symbol = "bus-cycles",
59 		.alias  = "",
60 	},
61 	[PERF_COUNT_HW_STALLED_CYCLES_FRONTEND] = {
62 		.symbol = "stalled-cycles-frontend",
63 		.alias  = "idle-cycles-frontend",
64 	},
65 	[PERF_COUNT_HW_STALLED_CYCLES_BACKEND] = {
66 		.symbol = "stalled-cycles-backend",
67 		.alias  = "idle-cycles-backend",
68 	},
69 	[PERF_COUNT_HW_REF_CPU_CYCLES] = {
70 		.symbol = "ref-cycles",
71 		.alias  = "",
72 	},
73 };
74 
75 static struct event_symbol event_symbols_sw[PERF_COUNT_SW_MAX] = {
76 	[PERF_COUNT_SW_CPU_CLOCK] = {
77 		.symbol = "cpu-clock",
78 		.alias  = "",
79 	},
80 	[PERF_COUNT_SW_TASK_CLOCK] = {
81 		.symbol = "task-clock",
82 		.alias  = "",
83 	},
84 	[PERF_COUNT_SW_PAGE_FAULTS] = {
85 		.symbol = "page-faults",
86 		.alias  = "faults",
87 	},
88 	[PERF_COUNT_SW_CONTEXT_SWITCHES] = {
89 		.symbol = "context-switches",
90 		.alias  = "cs",
91 	},
92 	[PERF_COUNT_SW_CPU_MIGRATIONS] = {
93 		.symbol = "cpu-migrations",
94 		.alias  = "migrations",
95 	},
96 	[PERF_COUNT_SW_PAGE_FAULTS_MIN] = {
97 		.symbol = "minor-faults",
98 		.alias  = "",
99 	},
100 	[PERF_COUNT_SW_PAGE_FAULTS_MAJ] = {
101 		.symbol = "major-faults",
102 		.alias  = "",
103 	},
104 	[PERF_COUNT_SW_ALIGNMENT_FAULTS] = {
105 		.symbol = "alignment-faults",
106 		.alias  = "",
107 	},
108 	[PERF_COUNT_SW_EMULATION_FAULTS] = {
109 		.symbol = "emulation-faults",
110 		.alias  = "",
111 	},
112 	[PERF_COUNT_SW_DUMMY] = {
113 		.symbol = "dummy",
114 		.alias  = "",
115 	},
116 };
117 
118 #define __PERF_EVENT_FIELD(config, name) \
119 	((config & PERF_EVENT_##name##_MASK) >> PERF_EVENT_##name##_SHIFT)
120 
121 #define PERF_EVENT_RAW(config)		__PERF_EVENT_FIELD(config, RAW)
122 #define PERF_EVENT_CONFIG(config)	__PERF_EVENT_FIELD(config, CONFIG)
123 #define PERF_EVENT_TYPE(config)		__PERF_EVENT_FIELD(config, TYPE)
124 #define PERF_EVENT_ID(config)		__PERF_EVENT_FIELD(config, EVENT)
125 
126 #define for_each_subsystem(sys_dir, sys_dirent, sys_next)	       \
127 	while (!readdir_r(sys_dir, &sys_dirent, &sys_next) && sys_next)	       \
128 	if (sys_dirent.d_type == DT_DIR &&				       \
129 	   (strcmp(sys_dirent.d_name, ".")) &&				       \
130 	   (strcmp(sys_dirent.d_name, "..")))
131 
132 static int tp_event_has_id(struct dirent *sys_dir, struct dirent *evt_dir)
133 {
134 	char evt_path[MAXPATHLEN];
135 	int fd;
136 
137 	snprintf(evt_path, MAXPATHLEN, "%s/%s/%s/id", tracing_events_path,
138 			sys_dir->d_name, evt_dir->d_name);
139 	fd = open(evt_path, O_RDONLY);
140 	if (fd < 0)
141 		return -EINVAL;
142 	close(fd);
143 
144 	return 0;
145 }
146 
147 #define for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next)	       \
148 	while (!readdir_r(evt_dir, &evt_dirent, &evt_next) && evt_next)        \
149 	if (evt_dirent.d_type == DT_DIR &&				       \
150 	   (strcmp(evt_dirent.d_name, ".")) &&				       \
151 	   (strcmp(evt_dirent.d_name, "..")) &&				       \
152 	   (!tp_event_has_id(&sys_dirent, &evt_dirent)))
153 
154 #define MAX_EVENT_LENGTH 512
155 
156 
157 struct tracepoint_path *tracepoint_id_to_path(u64 config)
158 {
159 	struct tracepoint_path *path = NULL;
160 	DIR *sys_dir, *evt_dir;
161 	struct dirent *sys_next, *evt_next, sys_dirent, evt_dirent;
162 	char id_buf[24];
163 	int fd;
164 	u64 id;
165 	char evt_path[MAXPATHLEN];
166 	char dir_path[MAXPATHLEN];
167 
168 	if (debugfs_valid_mountpoint(tracing_events_path))
169 		return NULL;
170 
171 	sys_dir = opendir(tracing_events_path);
172 	if (!sys_dir)
173 		return NULL;
174 
175 	for_each_subsystem(sys_dir, sys_dirent, sys_next) {
176 
177 		snprintf(dir_path, MAXPATHLEN, "%s/%s", tracing_events_path,
178 			 sys_dirent.d_name);
179 		evt_dir = opendir(dir_path);
180 		if (!evt_dir)
181 			continue;
182 
183 		for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next) {
184 
185 			snprintf(evt_path, MAXPATHLEN, "%s/%s/id", dir_path,
186 				 evt_dirent.d_name);
187 			fd = open(evt_path, O_RDONLY);
188 			if (fd < 0)
189 				continue;
190 			if (read(fd, id_buf, sizeof(id_buf)) < 0) {
191 				close(fd);
192 				continue;
193 			}
194 			close(fd);
195 			id = atoll(id_buf);
196 			if (id == config) {
197 				closedir(evt_dir);
198 				closedir(sys_dir);
199 				path = zalloc(sizeof(*path));
200 				path->system = malloc(MAX_EVENT_LENGTH);
201 				if (!path->system) {
202 					free(path);
203 					return NULL;
204 				}
205 				path->name = malloc(MAX_EVENT_LENGTH);
206 				if (!path->name) {
207 					zfree(&path->system);
208 					free(path);
209 					return NULL;
210 				}
211 				strncpy(path->system, sys_dirent.d_name,
212 					MAX_EVENT_LENGTH);
213 				strncpy(path->name, evt_dirent.d_name,
214 					MAX_EVENT_LENGTH);
215 				return path;
216 			}
217 		}
218 		closedir(evt_dir);
219 	}
220 
221 	closedir(sys_dir);
222 	return NULL;
223 }
224 
225 struct tracepoint_path *tracepoint_name_to_path(const char *name)
226 {
227 	struct tracepoint_path *path = zalloc(sizeof(*path));
228 	char *str = strchr(name, ':');
229 
230 	if (path == NULL || str == NULL) {
231 		free(path);
232 		return NULL;
233 	}
234 
235 	path->system = strndup(name, str - name);
236 	path->name = strdup(str+1);
237 
238 	if (path->system == NULL || path->name == NULL) {
239 		zfree(&path->system);
240 		zfree(&path->name);
241 		free(path);
242 		path = NULL;
243 	}
244 
245 	return path;
246 }
247 
248 const char *event_type(int type)
249 {
250 	switch (type) {
251 	case PERF_TYPE_HARDWARE:
252 		return "hardware";
253 
254 	case PERF_TYPE_SOFTWARE:
255 		return "software";
256 
257 	case PERF_TYPE_TRACEPOINT:
258 		return "tracepoint";
259 
260 	case PERF_TYPE_HW_CACHE:
261 		return "hardware-cache";
262 
263 	default:
264 		break;
265 	}
266 
267 	return "unknown";
268 }
269 
270 
271 
272 static struct perf_evsel *
273 __add_event(struct list_head *list, int *idx,
274 	    struct perf_event_attr *attr,
275 	    char *name, struct cpu_map *cpus)
276 {
277 	struct perf_evsel *evsel;
278 
279 	event_attr_init(attr);
280 
281 	evsel = perf_evsel__new_idx(attr, (*idx)++);
282 	if (!evsel)
283 		return NULL;
284 
285 	evsel->cpus = cpus;
286 	if (name)
287 		evsel->name = strdup(name);
288 	list_add_tail(&evsel->node, list);
289 	return evsel;
290 }
291 
292 static int add_event(struct list_head *list, int *idx,
293 		     struct perf_event_attr *attr, char *name)
294 {
295 	return __add_event(list, idx, attr, name, NULL) ? 0 : -ENOMEM;
296 }
297 
298 static int parse_aliases(char *str, const char *names[][PERF_EVSEL__MAX_ALIASES], int size)
299 {
300 	int i, j;
301 	int n, longest = -1;
302 
303 	for (i = 0; i < size; i++) {
304 		for (j = 0; j < PERF_EVSEL__MAX_ALIASES && names[i][j]; j++) {
305 			n = strlen(names[i][j]);
306 			if (n > longest && !strncasecmp(str, names[i][j], n))
307 				longest = n;
308 		}
309 		if (longest > 0)
310 			return i;
311 	}
312 
313 	return -1;
314 }
315 
316 int parse_events_add_cache(struct list_head *list, int *idx,
317 			   char *type, char *op_result1, char *op_result2)
318 {
319 	struct perf_event_attr attr;
320 	char name[MAX_NAME_LEN];
321 	int cache_type = -1, cache_op = -1, cache_result = -1;
322 	char *op_result[2] = { op_result1, op_result2 };
323 	int i, n;
324 
325 	/*
326 	 * No fallback - if we cannot get a clear cache type
327 	 * then bail out:
328 	 */
329 	cache_type = parse_aliases(type, perf_evsel__hw_cache,
330 				   PERF_COUNT_HW_CACHE_MAX);
331 	if (cache_type == -1)
332 		return -EINVAL;
333 
334 	n = snprintf(name, MAX_NAME_LEN, "%s", type);
335 
336 	for (i = 0; (i < 2) && (op_result[i]); i++) {
337 		char *str = op_result[i];
338 
339 		n += snprintf(name + n, MAX_NAME_LEN - n, "-%s", str);
340 
341 		if (cache_op == -1) {
342 			cache_op = parse_aliases(str, perf_evsel__hw_cache_op,
343 						 PERF_COUNT_HW_CACHE_OP_MAX);
344 			if (cache_op >= 0) {
345 				if (!perf_evsel__is_cache_op_valid(cache_type, cache_op))
346 					return -EINVAL;
347 				continue;
348 			}
349 		}
350 
351 		if (cache_result == -1) {
352 			cache_result = parse_aliases(str, perf_evsel__hw_cache_result,
353 						     PERF_COUNT_HW_CACHE_RESULT_MAX);
354 			if (cache_result >= 0)
355 				continue;
356 		}
357 	}
358 
359 	/*
360 	 * Fall back to reads:
361 	 */
362 	if (cache_op == -1)
363 		cache_op = PERF_COUNT_HW_CACHE_OP_READ;
364 
365 	/*
366 	 * Fall back to accesses:
367 	 */
368 	if (cache_result == -1)
369 		cache_result = PERF_COUNT_HW_CACHE_RESULT_ACCESS;
370 
371 	memset(&attr, 0, sizeof(attr));
372 	attr.config = cache_type | (cache_op << 8) | (cache_result << 16);
373 	attr.type = PERF_TYPE_HW_CACHE;
374 	return add_event(list, idx, &attr, name);
375 }
376 
377 static int add_tracepoint(struct list_head *list, int *idx,
378 			  char *sys_name, char *evt_name)
379 {
380 	struct perf_evsel *evsel;
381 
382 	evsel = perf_evsel__newtp_idx(sys_name, evt_name, (*idx)++);
383 	if (!evsel)
384 		return -ENOMEM;
385 
386 	list_add_tail(&evsel->node, list);
387 
388 	return 0;
389 }
390 
391 static int add_tracepoint_multi_event(struct list_head *list, int *idx,
392 				      char *sys_name, char *evt_name)
393 {
394 	char evt_path[MAXPATHLEN];
395 	struct dirent *evt_ent;
396 	DIR *evt_dir;
397 	int ret = 0;
398 
399 	snprintf(evt_path, MAXPATHLEN, "%s/%s", tracing_events_path, sys_name);
400 	evt_dir = opendir(evt_path);
401 	if (!evt_dir) {
402 		perror("Can't open event dir");
403 		return -1;
404 	}
405 
406 	while (!ret && (evt_ent = readdir(evt_dir))) {
407 		if (!strcmp(evt_ent->d_name, ".")
408 		    || !strcmp(evt_ent->d_name, "..")
409 		    || !strcmp(evt_ent->d_name, "enable")
410 		    || !strcmp(evt_ent->d_name, "filter"))
411 			continue;
412 
413 		if (!strglobmatch(evt_ent->d_name, evt_name))
414 			continue;
415 
416 		ret = add_tracepoint(list, idx, sys_name, evt_ent->d_name);
417 	}
418 
419 	closedir(evt_dir);
420 	return ret;
421 }
422 
423 static int add_tracepoint_event(struct list_head *list, int *idx,
424 				char *sys_name, char *evt_name)
425 {
426 	return strpbrk(evt_name, "*?") ?
427 	       add_tracepoint_multi_event(list, idx, sys_name, evt_name) :
428 	       add_tracepoint(list, idx, sys_name, evt_name);
429 }
430 
431 static int add_tracepoint_multi_sys(struct list_head *list, int *idx,
432 				    char *sys_name, char *evt_name)
433 {
434 	struct dirent *events_ent;
435 	DIR *events_dir;
436 	int ret = 0;
437 
438 	events_dir = opendir(tracing_events_path);
439 	if (!events_dir) {
440 		perror("Can't open event dir");
441 		return -1;
442 	}
443 
444 	while (!ret && (events_ent = readdir(events_dir))) {
445 		if (!strcmp(events_ent->d_name, ".")
446 		    || !strcmp(events_ent->d_name, "..")
447 		    || !strcmp(events_ent->d_name, "enable")
448 		    || !strcmp(events_ent->d_name, "header_event")
449 		    || !strcmp(events_ent->d_name, "header_page"))
450 			continue;
451 
452 		if (!strglobmatch(events_ent->d_name, sys_name))
453 			continue;
454 
455 		ret = add_tracepoint_event(list, idx, events_ent->d_name,
456 					   evt_name);
457 	}
458 
459 	closedir(events_dir);
460 	return ret;
461 }
462 
463 int parse_events_add_tracepoint(struct list_head *list, int *idx,
464 				char *sys, char *event)
465 {
466 	int ret;
467 
468 	ret = debugfs_valid_mountpoint(tracing_events_path);
469 	if (ret)
470 		return ret;
471 
472 	if (strpbrk(sys, "*?"))
473 		return add_tracepoint_multi_sys(list, idx, sys, event);
474 	else
475 		return add_tracepoint_event(list, idx, sys, event);
476 }
477 
478 static int
479 parse_breakpoint_type(const char *type, struct perf_event_attr *attr)
480 {
481 	int i;
482 
483 	for (i = 0; i < 3; i++) {
484 		if (!type || !type[i])
485 			break;
486 
487 #define CHECK_SET_TYPE(bit)		\
488 do {					\
489 	if (attr->bp_type & bit)	\
490 		return -EINVAL;		\
491 	else				\
492 		attr->bp_type |= bit;	\
493 } while (0)
494 
495 		switch (type[i]) {
496 		case 'r':
497 			CHECK_SET_TYPE(HW_BREAKPOINT_R);
498 			break;
499 		case 'w':
500 			CHECK_SET_TYPE(HW_BREAKPOINT_W);
501 			break;
502 		case 'x':
503 			CHECK_SET_TYPE(HW_BREAKPOINT_X);
504 			break;
505 		default:
506 			return -EINVAL;
507 		}
508 	}
509 
510 #undef CHECK_SET_TYPE
511 
512 	if (!attr->bp_type) /* Default */
513 		attr->bp_type = HW_BREAKPOINT_R | HW_BREAKPOINT_W;
514 
515 	return 0;
516 }
517 
518 int parse_events_add_breakpoint(struct list_head *list, int *idx,
519 				void *ptr, char *type)
520 {
521 	struct perf_event_attr attr;
522 
523 	memset(&attr, 0, sizeof(attr));
524 	attr.bp_addr = (unsigned long) ptr;
525 
526 	if (parse_breakpoint_type(type, &attr))
527 		return -EINVAL;
528 
529 	/*
530 	 * We should find a nice way to override the access length
531 	 * Provide some defaults for now
532 	 */
533 	if (attr.bp_type == HW_BREAKPOINT_X)
534 		attr.bp_len = sizeof(long);
535 	else
536 		attr.bp_len = HW_BREAKPOINT_LEN_4;
537 
538 	attr.type = PERF_TYPE_BREAKPOINT;
539 	attr.sample_period = 1;
540 
541 	return add_event(list, idx, &attr, NULL);
542 }
543 
544 static int config_term(struct perf_event_attr *attr,
545 		       struct parse_events_term *term)
546 {
547 #define CHECK_TYPE_VAL(type)					\
548 do {								\
549 	if (PARSE_EVENTS__TERM_TYPE_ ## type != term->type_val)	\
550 		return -EINVAL;					\
551 } while (0)
552 
553 	switch (term->type_term) {
554 	case PARSE_EVENTS__TERM_TYPE_CONFIG:
555 		CHECK_TYPE_VAL(NUM);
556 		attr->config = term->val.num;
557 		break;
558 	case PARSE_EVENTS__TERM_TYPE_CONFIG1:
559 		CHECK_TYPE_VAL(NUM);
560 		attr->config1 = term->val.num;
561 		break;
562 	case PARSE_EVENTS__TERM_TYPE_CONFIG2:
563 		CHECK_TYPE_VAL(NUM);
564 		attr->config2 = term->val.num;
565 		break;
566 	case PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD:
567 		CHECK_TYPE_VAL(NUM);
568 		attr->sample_period = term->val.num;
569 		break;
570 	case PARSE_EVENTS__TERM_TYPE_BRANCH_SAMPLE_TYPE:
571 		/*
572 		 * TODO uncomment when the field is available
573 		 * attr->branch_sample_type = term->val.num;
574 		 */
575 		break;
576 	case PARSE_EVENTS__TERM_TYPE_NAME:
577 		CHECK_TYPE_VAL(STR);
578 		break;
579 	default:
580 		return -EINVAL;
581 	}
582 
583 	return 0;
584 #undef CHECK_TYPE_VAL
585 }
586 
587 static int config_attr(struct perf_event_attr *attr,
588 		       struct list_head *head, int fail)
589 {
590 	struct parse_events_term *term;
591 
592 	list_for_each_entry(term, head, list)
593 		if (config_term(attr, term) && fail)
594 			return -EINVAL;
595 
596 	return 0;
597 }
598 
599 int parse_events_add_numeric(struct list_head *list, int *idx,
600 			     u32 type, u64 config,
601 			     struct list_head *head_config)
602 {
603 	struct perf_event_attr attr;
604 
605 	memset(&attr, 0, sizeof(attr));
606 	attr.type = type;
607 	attr.config = config;
608 
609 	if (head_config &&
610 	    config_attr(&attr, head_config, 1))
611 		return -EINVAL;
612 
613 	return add_event(list, idx, &attr, NULL);
614 }
615 
616 static int parse_events__is_name_term(struct parse_events_term *term)
617 {
618 	return term->type_term == PARSE_EVENTS__TERM_TYPE_NAME;
619 }
620 
621 static char *pmu_event_name(struct list_head *head_terms)
622 {
623 	struct parse_events_term *term;
624 
625 	list_for_each_entry(term, head_terms, list)
626 		if (parse_events__is_name_term(term))
627 			return term->val.str;
628 
629 	return NULL;
630 }
631 
632 int parse_events_add_pmu(struct list_head *list, int *idx,
633 			 char *name, struct list_head *head_config)
634 {
635 	struct perf_event_attr attr;
636 	struct perf_pmu *pmu;
637 	struct perf_evsel *evsel;
638 	const char *unit;
639 	double scale;
640 
641 	pmu = perf_pmu__find(name);
642 	if (!pmu)
643 		return -EINVAL;
644 
645 	memset(&attr, 0, sizeof(attr));
646 
647 	if (perf_pmu__check_alias(pmu, head_config, &unit, &scale))
648 		return -EINVAL;
649 
650 	/*
651 	 * Configure hardcoded terms first, no need to check
652 	 * return value when called with fail == 0 ;)
653 	 */
654 	config_attr(&attr, head_config, 0);
655 
656 	if (perf_pmu__config(pmu, &attr, head_config))
657 		return -EINVAL;
658 
659 	evsel = __add_event(list, idx, &attr, pmu_event_name(head_config),
660 			    pmu->cpus);
661 	if (evsel) {
662 		evsel->unit = unit;
663 		evsel->scale = scale;
664 	}
665 
666 	return evsel ? 0 : -ENOMEM;
667 }
668 
669 int parse_events__modifier_group(struct list_head *list,
670 				 char *event_mod)
671 {
672 	return parse_events__modifier_event(list, event_mod, true);
673 }
674 
675 void parse_events__set_leader(char *name, struct list_head *list)
676 {
677 	struct perf_evsel *leader;
678 
679 	__perf_evlist__set_leader(list);
680 	leader = list_entry(list->next, struct perf_evsel, node);
681 	leader->group_name = name ? strdup(name) : NULL;
682 }
683 
684 /* list_event is assumed to point to malloc'ed memory */
685 void parse_events_update_lists(struct list_head *list_event,
686 			       struct list_head *list_all)
687 {
688 	/*
689 	 * Called for single event definition. Update the
690 	 * 'all event' list, and reinit the 'single event'
691 	 * list, for next event definition.
692 	 */
693 	list_splice_tail(list_event, list_all);
694 	free(list_event);
695 }
696 
697 struct event_modifier {
698 	int eu;
699 	int ek;
700 	int eh;
701 	int eH;
702 	int eG;
703 	int precise;
704 	int exclude_GH;
705 	int sample_read;
706 	int pinned;
707 };
708 
709 static int get_event_modifier(struct event_modifier *mod, char *str,
710 			       struct perf_evsel *evsel)
711 {
712 	int eu = evsel ? evsel->attr.exclude_user : 0;
713 	int ek = evsel ? evsel->attr.exclude_kernel : 0;
714 	int eh = evsel ? evsel->attr.exclude_hv : 0;
715 	int eH = evsel ? evsel->attr.exclude_host : 0;
716 	int eG = evsel ? evsel->attr.exclude_guest : 0;
717 	int precise = evsel ? evsel->attr.precise_ip : 0;
718 	int sample_read = 0;
719 	int pinned = evsel ? evsel->attr.pinned : 0;
720 
721 	int exclude = eu | ek | eh;
722 	int exclude_GH = evsel ? evsel->exclude_GH : 0;
723 
724 	memset(mod, 0, sizeof(*mod));
725 
726 	while (*str) {
727 		if (*str == 'u') {
728 			if (!exclude)
729 				exclude = eu = ek = eh = 1;
730 			eu = 0;
731 		} else if (*str == 'k') {
732 			if (!exclude)
733 				exclude = eu = ek = eh = 1;
734 			ek = 0;
735 		} else if (*str == 'h') {
736 			if (!exclude)
737 				exclude = eu = ek = eh = 1;
738 			eh = 0;
739 		} else if (*str == 'G') {
740 			if (!exclude_GH)
741 				exclude_GH = eG = eH = 1;
742 			eG = 0;
743 		} else if (*str == 'H') {
744 			if (!exclude_GH)
745 				exclude_GH = eG = eH = 1;
746 			eH = 0;
747 		} else if (*str == 'p') {
748 			precise++;
749 			/* use of precise requires exclude_guest */
750 			if (!exclude_GH)
751 				eG = 1;
752 		} else if (*str == 'S') {
753 			sample_read = 1;
754 		} else if (*str == 'D') {
755 			pinned = 1;
756 		} else
757 			break;
758 
759 		++str;
760 	}
761 
762 	/*
763 	 * precise ip:
764 	 *
765 	 *  0 - SAMPLE_IP can have arbitrary skid
766 	 *  1 - SAMPLE_IP must have constant skid
767 	 *  2 - SAMPLE_IP requested to have 0 skid
768 	 *  3 - SAMPLE_IP must have 0 skid
769 	 *
770 	 *  See also PERF_RECORD_MISC_EXACT_IP
771 	 */
772 	if (precise > 3)
773 		return -EINVAL;
774 
775 	mod->eu = eu;
776 	mod->ek = ek;
777 	mod->eh = eh;
778 	mod->eH = eH;
779 	mod->eG = eG;
780 	mod->precise = precise;
781 	mod->exclude_GH = exclude_GH;
782 	mod->sample_read = sample_read;
783 	mod->pinned = pinned;
784 
785 	return 0;
786 }
787 
788 /*
789  * Basic modifier sanity check to validate it contains only one
790  * instance of any modifier (apart from 'p') present.
791  */
792 static int check_modifier(char *str)
793 {
794 	char *p = str;
795 
796 	/* The sizeof includes 0 byte as well. */
797 	if (strlen(str) > (sizeof("ukhGHpppSD") - 1))
798 		return -1;
799 
800 	while (*p) {
801 		if (*p != 'p' && strchr(p + 1, *p))
802 			return -1;
803 		p++;
804 	}
805 
806 	return 0;
807 }
808 
809 int parse_events__modifier_event(struct list_head *list, char *str, bool add)
810 {
811 	struct perf_evsel *evsel;
812 	struct event_modifier mod;
813 
814 	if (str == NULL)
815 		return 0;
816 
817 	if (check_modifier(str))
818 		return -EINVAL;
819 
820 	if (!add && get_event_modifier(&mod, str, NULL))
821 		return -EINVAL;
822 
823 	__evlist__for_each(list, evsel) {
824 		if (add && get_event_modifier(&mod, str, evsel))
825 			return -EINVAL;
826 
827 		evsel->attr.exclude_user   = mod.eu;
828 		evsel->attr.exclude_kernel = mod.ek;
829 		evsel->attr.exclude_hv     = mod.eh;
830 		evsel->attr.precise_ip     = mod.precise;
831 		evsel->attr.exclude_host   = mod.eH;
832 		evsel->attr.exclude_guest  = mod.eG;
833 		evsel->exclude_GH          = mod.exclude_GH;
834 		evsel->sample_read         = mod.sample_read;
835 
836 		if (perf_evsel__is_group_leader(evsel))
837 			evsel->attr.pinned = mod.pinned;
838 	}
839 
840 	return 0;
841 }
842 
843 int parse_events_name(struct list_head *list, char *name)
844 {
845 	struct perf_evsel *evsel;
846 
847 	__evlist__for_each(list, evsel) {
848 		if (!evsel->name)
849 			evsel->name = strdup(name);
850 	}
851 
852 	return 0;
853 }
854 
855 static int parse_events__scanner(const char *str, void *data, int start_token);
856 
857 static int parse_events_fixup(int ret, const char *str, void *data,
858 			      int start_token)
859 {
860 	char *o = strdup(str);
861 	char *s = NULL;
862 	char *t = o;
863 	char *p;
864 	int len = 0;
865 
866 	if (!o)
867 		return ret;
868 	while ((p = strsep(&t, ",")) != NULL) {
869 		if (s)
870 			str_append(&s, &len, ",");
871 		str_append(&s, &len, "cpu/");
872 		str_append(&s, &len, p);
873 		str_append(&s, &len, "/");
874 	}
875 	free(o);
876 	if (!s)
877 		return -ENOMEM;
878 	return parse_events__scanner(s, data, start_token);
879 }
880 
881 static int parse_events__scanner(const char *str, void *data, int start_token)
882 {
883 	YY_BUFFER_STATE buffer;
884 	void *scanner;
885 	int ret;
886 
887 	ret = parse_events_lex_init_extra(start_token, &scanner);
888 	if (ret)
889 		return ret;
890 
891 	buffer = parse_events__scan_string(str, scanner);
892 
893 #ifdef PARSER_DEBUG
894 	parse_events_debug = 1;
895 #endif
896 	ret = parse_events_parse(data, scanner);
897 
898 	parse_events__flush_buffer(buffer, scanner);
899 	parse_events__delete_buffer(buffer, scanner);
900 	parse_events_lex_destroy(scanner);
901 	if (ret && !strchr(str, '/'))
902 		ret = parse_events_fixup(ret, str, data, start_token);
903 	return ret;
904 }
905 
906 /*
907  * parse event config string, return a list of event terms.
908  */
909 int parse_events_terms(struct list_head *terms, const char *str)
910 {
911 	struct parse_events_terms data = {
912 		.terms = NULL,
913 	};
914 	int ret;
915 
916 	ret = parse_events__scanner(str, &data, PE_START_TERMS);
917 	if (!ret) {
918 		list_splice(data.terms, terms);
919 		zfree(&data.terms);
920 		return 0;
921 	}
922 
923 	if (data.terms)
924 		parse_events__free_terms(data.terms);
925 	return ret;
926 }
927 
928 int parse_events(struct perf_evlist *evlist, const char *str)
929 {
930 	struct parse_events_evlist data = {
931 		.list = LIST_HEAD_INIT(data.list),
932 		.idx  = evlist->nr_entries,
933 	};
934 	int ret;
935 
936 	ret = parse_events__scanner(str, &data, PE_START_EVENTS);
937 	if (!ret) {
938 		int entries = data.idx - evlist->nr_entries;
939 		perf_evlist__splice_list_tail(evlist, &data.list, entries);
940 		evlist->nr_groups += data.nr_groups;
941 		return 0;
942 	}
943 
944 	/*
945 	 * There are 2 users - builtin-record and builtin-test objects.
946 	 * Both call perf_evlist__delete in case of error, so we dont
947 	 * need to bother.
948 	 */
949 	return ret;
950 }
951 
952 int parse_events_option(const struct option *opt, const char *str,
953 			int unset __maybe_unused)
954 {
955 	struct perf_evlist *evlist = *(struct perf_evlist **)opt->value;
956 	int ret = parse_events(evlist, str);
957 
958 	if (ret) {
959 		fprintf(stderr, "invalid or unsupported event: '%s'\n", str);
960 		fprintf(stderr, "Run 'perf list' for a list of valid events\n");
961 	}
962 	return ret;
963 }
964 
965 int parse_filter(const struct option *opt, const char *str,
966 		 int unset __maybe_unused)
967 {
968 	struct perf_evlist *evlist = *(struct perf_evlist **)opt->value;
969 	struct perf_evsel *last = NULL;
970 
971 	if (evlist->nr_entries > 0)
972 		last = perf_evlist__last(evlist);
973 
974 	if (last == NULL || last->attr.type != PERF_TYPE_TRACEPOINT) {
975 		fprintf(stderr,
976 			"-F option should follow a -e tracepoint option\n");
977 		return -1;
978 	}
979 
980 	last->filter = strdup(str);
981 	if (last->filter == NULL) {
982 		fprintf(stderr, "not enough memory to hold filter string\n");
983 		return -1;
984 	}
985 
986 	return 0;
987 }
988 
989 static const char * const event_type_descriptors[] = {
990 	"Hardware event",
991 	"Software event",
992 	"Tracepoint event",
993 	"Hardware cache event",
994 	"Raw hardware event descriptor",
995 	"Hardware breakpoint",
996 };
997 
998 /*
999  * Print the events from <debugfs_mount_point>/tracing/events
1000  */
1001 
1002 void print_tracepoint_events(const char *subsys_glob, const char *event_glob,
1003 			     bool name_only)
1004 {
1005 	DIR *sys_dir, *evt_dir;
1006 	struct dirent *sys_next, *evt_next, sys_dirent, evt_dirent;
1007 	char evt_path[MAXPATHLEN];
1008 	char dir_path[MAXPATHLEN];
1009 
1010 	if (debugfs_valid_mountpoint(tracing_events_path)) {
1011 		printf("  [ Tracepoints not available: %s ]\n", strerror(errno));
1012 		return;
1013 	}
1014 
1015 	sys_dir = opendir(tracing_events_path);
1016 	if (!sys_dir)
1017 		return;
1018 
1019 	for_each_subsystem(sys_dir, sys_dirent, sys_next) {
1020 		if (subsys_glob != NULL &&
1021 		    !strglobmatch(sys_dirent.d_name, subsys_glob))
1022 			continue;
1023 
1024 		snprintf(dir_path, MAXPATHLEN, "%s/%s", tracing_events_path,
1025 			 sys_dirent.d_name);
1026 		evt_dir = opendir(dir_path);
1027 		if (!evt_dir)
1028 			continue;
1029 
1030 		for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next) {
1031 			if (event_glob != NULL &&
1032 			    !strglobmatch(evt_dirent.d_name, event_glob))
1033 				continue;
1034 
1035 			if (name_only) {
1036 				printf("%s:%s ", sys_dirent.d_name, evt_dirent.d_name);
1037 				continue;
1038 			}
1039 
1040 			snprintf(evt_path, MAXPATHLEN, "%s:%s",
1041 				 sys_dirent.d_name, evt_dirent.d_name);
1042 			printf("  %-50s [%s]\n", evt_path,
1043 				event_type_descriptors[PERF_TYPE_TRACEPOINT]);
1044 		}
1045 		closedir(evt_dir);
1046 	}
1047 	closedir(sys_dir);
1048 }
1049 
1050 /*
1051  * Check whether event is in <debugfs_mount_point>/tracing/events
1052  */
1053 
1054 int is_valid_tracepoint(const char *event_string)
1055 {
1056 	DIR *sys_dir, *evt_dir;
1057 	struct dirent *sys_next, *evt_next, sys_dirent, evt_dirent;
1058 	char evt_path[MAXPATHLEN];
1059 	char dir_path[MAXPATHLEN];
1060 
1061 	if (debugfs_valid_mountpoint(tracing_events_path))
1062 		return 0;
1063 
1064 	sys_dir = opendir(tracing_events_path);
1065 	if (!sys_dir)
1066 		return 0;
1067 
1068 	for_each_subsystem(sys_dir, sys_dirent, sys_next) {
1069 
1070 		snprintf(dir_path, MAXPATHLEN, "%s/%s", tracing_events_path,
1071 			 sys_dirent.d_name);
1072 		evt_dir = opendir(dir_path);
1073 		if (!evt_dir)
1074 			continue;
1075 
1076 		for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next) {
1077 			snprintf(evt_path, MAXPATHLEN, "%s:%s",
1078 				 sys_dirent.d_name, evt_dirent.d_name);
1079 			if (!strcmp(evt_path, event_string)) {
1080 				closedir(evt_dir);
1081 				closedir(sys_dir);
1082 				return 1;
1083 			}
1084 		}
1085 		closedir(evt_dir);
1086 	}
1087 	closedir(sys_dir);
1088 	return 0;
1089 }
1090 
1091 static bool is_event_supported(u8 type, unsigned config)
1092 {
1093 	bool ret = true;
1094 	int open_return;
1095 	struct perf_evsel *evsel;
1096 	struct perf_event_attr attr = {
1097 		.type = type,
1098 		.config = config,
1099 		.disabled = 1,
1100 	};
1101 	struct {
1102 		struct thread_map map;
1103 		int threads[1];
1104 	} tmap = {
1105 		.map.nr	 = 1,
1106 		.threads = { 0 },
1107 	};
1108 
1109 	evsel = perf_evsel__new(&attr);
1110 	if (evsel) {
1111 		open_return = perf_evsel__open(evsel, NULL, &tmap.map);
1112 		ret = open_return >= 0;
1113 
1114 		if (open_return == -EACCES) {
1115 			/*
1116 			 * This happens if the paranoid value
1117 			 * /proc/sys/kernel/perf_event_paranoid is set to 2
1118 			 * Re-run with exclude_kernel set; we don't do that
1119 			 * by default as some ARM machines do not support it.
1120 			 *
1121 			 */
1122 			evsel->attr.exclude_kernel = 1;
1123 			ret = perf_evsel__open(evsel, NULL, &tmap.map) >= 0;
1124 		}
1125 		perf_evsel__delete(evsel);
1126 	}
1127 
1128 	return ret;
1129 }
1130 
1131 static void __print_events_type(u8 type, struct event_symbol *syms,
1132 				unsigned max)
1133 {
1134 	char name[64];
1135 	unsigned i;
1136 
1137 	for (i = 0; i < max ; i++, syms++) {
1138 		if (!is_event_supported(type, i))
1139 			continue;
1140 
1141 		if (strlen(syms->alias))
1142 			snprintf(name, sizeof(name),  "%s OR %s",
1143 				 syms->symbol, syms->alias);
1144 		else
1145 			snprintf(name, sizeof(name), "%s", syms->symbol);
1146 
1147 		printf("  %-50s [%s]\n", name, event_type_descriptors[type]);
1148 	}
1149 }
1150 
1151 void print_events_type(u8 type)
1152 {
1153 	if (type == PERF_TYPE_SOFTWARE)
1154 		__print_events_type(type, event_symbols_sw, PERF_COUNT_SW_MAX);
1155 	else
1156 		__print_events_type(type, event_symbols_hw, PERF_COUNT_HW_MAX);
1157 }
1158 
1159 int print_hwcache_events(const char *event_glob, bool name_only)
1160 {
1161 	unsigned int type, op, i, printed = 0;
1162 	char name[64];
1163 
1164 	for (type = 0; type < PERF_COUNT_HW_CACHE_MAX; type++) {
1165 		for (op = 0; op < PERF_COUNT_HW_CACHE_OP_MAX; op++) {
1166 			/* skip invalid cache type */
1167 			if (!perf_evsel__is_cache_op_valid(type, op))
1168 				continue;
1169 
1170 			for (i = 0; i < PERF_COUNT_HW_CACHE_RESULT_MAX; i++) {
1171 				__perf_evsel__hw_cache_type_op_res_name(type, op, i,
1172 									name, sizeof(name));
1173 				if (event_glob != NULL && !strglobmatch(name, event_glob))
1174 					continue;
1175 
1176 				if (!is_event_supported(PERF_TYPE_HW_CACHE,
1177 							type | (op << 8) | (i << 16)))
1178 					continue;
1179 
1180 				if (name_only)
1181 					printf("%s ", name);
1182 				else
1183 					printf("  %-50s [%s]\n", name,
1184 					       event_type_descriptors[PERF_TYPE_HW_CACHE]);
1185 				++printed;
1186 			}
1187 		}
1188 	}
1189 
1190 	if (printed)
1191 		printf("\n");
1192 	return printed;
1193 }
1194 
1195 static void print_symbol_events(const char *event_glob, unsigned type,
1196 				struct event_symbol *syms, unsigned max,
1197 				bool name_only)
1198 {
1199 	unsigned i, printed = 0;
1200 	char name[MAX_NAME_LEN];
1201 
1202 	for (i = 0; i < max; i++, syms++) {
1203 
1204 		if (event_glob != NULL &&
1205 		    !(strglobmatch(syms->symbol, event_glob) ||
1206 		      (syms->alias && strglobmatch(syms->alias, event_glob))))
1207 			continue;
1208 
1209 		if (!is_event_supported(type, i))
1210 			continue;
1211 
1212 		if (name_only) {
1213 			printf("%s ", syms->symbol);
1214 			continue;
1215 		}
1216 
1217 		if (strlen(syms->alias))
1218 			snprintf(name, MAX_NAME_LEN, "%s OR %s", syms->symbol, syms->alias);
1219 		else
1220 			strncpy(name, syms->symbol, MAX_NAME_LEN);
1221 
1222 		printf("  %-50s [%s]\n", name, event_type_descriptors[type]);
1223 
1224 		printed++;
1225 	}
1226 
1227 	if (printed)
1228 		printf("\n");
1229 }
1230 
1231 /*
1232  * Print the help text for the event symbols:
1233  */
1234 void print_events(const char *event_glob, bool name_only)
1235 {
1236 	if (!name_only) {
1237 		printf("\n");
1238 		printf("List of pre-defined events (to be used in -e):\n");
1239 	}
1240 
1241 	print_symbol_events(event_glob, PERF_TYPE_HARDWARE,
1242 			    event_symbols_hw, PERF_COUNT_HW_MAX, name_only);
1243 
1244 	print_symbol_events(event_glob, PERF_TYPE_SOFTWARE,
1245 			    event_symbols_sw, PERF_COUNT_SW_MAX, name_only);
1246 
1247 	print_hwcache_events(event_glob, name_only);
1248 
1249 	print_pmu_events(event_glob, name_only);
1250 
1251 	if (event_glob != NULL)
1252 		return;
1253 
1254 	if (!name_only) {
1255 		printf("  %-50s [%s]\n",
1256 		       "rNNN",
1257 		       event_type_descriptors[PERF_TYPE_RAW]);
1258 		printf("  %-50s [%s]\n",
1259 		       "cpu/t1=v1[,t2=v2,t3 ...]/modifier",
1260 		       event_type_descriptors[PERF_TYPE_RAW]);
1261 		printf("   (see 'man perf-list' on how to encode it)\n");
1262 		printf("\n");
1263 
1264 		printf("  %-50s [%s]\n",
1265 		       "mem:<addr>[:access]",
1266 			event_type_descriptors[PERF_TYPE_BREAKPOINT]);
1267 		printf("\n");
1268 	}
1269 
1270 	print_tracepoint_events(NULL, NULL, name_only);
1271 }
1272 
1273 int parse_events__is_hardcoded_term(struct parse_events_term *term)
1274 {
1275 	return term->type_term != PARSE_EVENTS__TERM_TYPE_USER;
1276 }
1277 
1278 static int new_term(struct parse_events_term **_term, int type_val,
1279 		    int type_term, char *config,
1280 		    char *str, u64 num)
1281 {
1282 	struct parse_events_term *term;
1283 
1284 	term = zalloc(sizeof(*term));
1285 	if (!term)
1286 		return -ENOMEM;
1287 
1288 	INIT_LIST_HEAD(&term->list);
1289 	term->type_val  = type_val;
1290 	term->type_term = type_term;
1291 	term->config = config;
1292 
1293 	switch (type_val) {
1294 	case PARSE_EVENTS__TERM_TYPE_NUM:
1295 		term->val.num = num;
1296 		break;
1297 	case PARSE_EVENTS__TERM_TYPE_STR:
1298 		term->val.str = str;
1299 		break;
1300 	default:
1301 		free(term);
1302 		return -EINVAL;
1303 	}
1304 
1305 	*_term = term;
1306 	return 0;
1307 }
1308 
1309 int parse_events_term__num(struct parse_events_term **term,
1310 			   int type_term, char *config, u64 num)
1311 {
1312 	return new_term(term, PARSE_EVENTS__TERM_TYPE_NUM, type_term,
1313 			config, NULL, num);
1314 }
1315 
1316 int parse_events_term__str(struct parse_events_term **term,
1317 			   int type_term, char *config, char *str)
1318 {
1319 	return new_term(term, PARSE_EVENTS__TERM_TYPE_STR, type_term,
1320 			config, str, 0);
1321 }
1322 
1323 int parse_events_term__sym_hw(struct parse_events_term **term,
1324 			      char *config, unsigned idx)
1325 {
1326 	struct event_symbol *sym;
1327 
1328 	BUG_ON(idx >= PERF_COUNT_HW_MAX);
1329 	sym = &event_symbols_hw[idx];
1330 
1331 	if (config)
1332 		return new_term(term, PARSE_EVENTS__TERM_TYPE_STR,
1333 				PARSE_EVENTS__TERM_TYPE_USER, config,
1334 				(char *) sym->symbol, 0);
1335 	else
1336 		return new_term(term, PARSE_EVENTS__TERM_TYPE_STR,
1337 				PARSE_EVENTS__TERM_TYPE_USER,
1338 				(char *) "event", (char *) sym->symbol, 0);
1339 }
1340 
1341 int parse_events_term__clone(struct parse_events_term **new,
1342 			     struct parse_events_term *term)
1343 {
1344 	return new_term(new, term->type_val, term->type_term, term->config,
1345 			term->val.str, term->val.num);
1346 }
1347 
1348 void parse_events__free_terms(struct list_head *terms)
1349 {
1350 	struct parse_events_term *term, *h;
1351 
1352 	list_for_each_entry_safe(term, h, terms, list)
1353 		free(term);
1354 }
1355