xref: /linux/tools/perf/util/parse-events.c (revision baec60800dd4a379546f3ba311574d907a01a77d)
1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/hw_breakpoint.h>
3 #include <linux/err.h>
4 #include <linux/list_sort.h>
5 #include <linux/zalloc.h>
6 #include <dirent.h>
7 #include <errno.h>
8 #include <sys/ioctl.h>
9 #include <sys/param.h>
10 #include "term.h"
11 #include "evlist.h"
12 #include "evsel.h"
13 #include <subcmd/parse-options.h>
14 #include "parse-events.h"
15 #include "string2.h"
16 #include "strlist.h"
17 #include "debug.h"
18 #include <api/fs/tracing_path.h>
19 #include <perf/cpumap.h>
20 #include <util/parse-events-bison.h>
21 #include <util/parse-events-flex.h>
22 #include "pmu.h"
23 #include "pmus.h"
24 #include "asm/bug.h"
25 #include "util/parse-branch-options.h"
26 #include "util/evsel_config.h"
27 #include "util/event.h"
28 #include "util/bpf-filter.h"
29 #include "util/util.h"
30 #include "tracepoint.h"
31 
32 #define MAX_NAME_LEN 100
33 
34 #ifdef PARSER_DEBUG
35 extern int parse_events_debug;
36 #endif
37 static int get_config_terms(struct list_head *head_config,
38 			    struct list_head *head_terms __maybe_unused);
39 
40 struct event_symbol event_symbols_hw[PERF_COUNT_HW_MAX] = {
41 	[PERF_COUNT_HW_CPU_CYCLES] = {
42 		.symbol = "cpu-cycles",
43 		.alias  = "cycles",
44 	},
45 	[PERF_COUNT_HW_INSTRUCTIONS] = {
46 		.symbol = "instructions",
47 		.alias  = "",
48 	},
49 	[PERF_COUNT_HW_CACHE_REFERENCES] = {
50 		.symbol = "cache-references",
51 		.alias  = "",
52 	},
53 	[PERF_COUNT_HW_CACHE_MISSES] = {
54 		.symbol = "cache-misses",
55 		.alias  = "",
56 	},
57 	[PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = {
58 		.symbol = "branch-instructions",
59 		.alias  = "branches",
60 	},
61 	[PERF_COUNT_HW_BRANCH_MISSES] = {
62 		.symbol = "branch-misses",
63 		.alias  = "",
64 	},
65 	[PERF_COUNT_HW_BUS_CYCLES] = {
66 		.symbol = "bus-cycles",
67 		.alias  = "",
68 	},
69 	[PERF_COUNT_HW_STALLED_CYCLES_FRONTEND] = {
70 		.symbol = "stalled-cycles-frontend",
71 		.alias  = "idle-cycles-frontend",
72 	},
73 	[PERF_COUNT_HW_STALLED_CYCLES_BACKEND] = {
74 		.symbol = "stalled-cycles-backend",
75 		.alias  = "idle-cycles-backend",
76 	},
77 	[PERF_COUNT_HW_REF_CPU_CYCLES] = {
78 		.symbol = "ref-cycles",
79 		.alias  = "",
80 	},
81 };
82 
83 struct event_symbol event_symbols_sw[PERF_COUNT_SW_MAX] = {
84 	[PERF_COUNT_SW_CPU_CLOCK] = {
85 		.symbol = "cpu-clock",
86 		.alias  = "",
87 	},
88 	[PERF_COUNT_SW_TASK_CLOCK] = {
89 		.symbol = "task-clock",
90 		.alias  = "",
91 	},
92 	[PERF_COUNT_SW_PAGE_FAULTS] = {
93 		.symbol = "page-faults",
94 		.alias  = "faults",
95 	},
96 	[PERF_COUNT_SW_CONTEXT_SWITCHES] = {
97 		.symbol = "context-switches",
98 		.alias  = "cs",
99 	},
100 	[PERF_COUNT_SW_CPU_MIGRATIONS] = {
101 		.symbol = "cpu-migrations",
102 		.alias  = "migrations",
103 	},
104 	[PERF_COUNT_SW_PAGE_FAULTS_MIN] = {
105 		.symbol = "minor-faults",
106 		.alias  = "",
107 	},
108 	[PERF_COUNT_SW_PAGE_FAULTS_MAJ] = {
109 		.symbol = "major-faults",
110 		.alias  = "",
111 	},
112 	[PERF_COUNT_SW_ALIGNMENT_FAULTS] = {
113 		.symbol = "alignment-faults",
114 		.alias  = "",
115 	},
116 	[PERF_COUNT_SW_EMULATION_FAULTS] = {
117 		.symbol = "emulation-faults",
118 		.alias  = "",
119 	},
120 	[PERF_COUNT_SW_DUMMY] = {
121 		.symbol = "dummy",
122 		.alias  = "",
123 	},
124 	[PERF_COUNT_SW_BPF_OUTPUT] = {
125 		.symbol = "bpf-output",
126 		.alias  = "",
127 	},
128 	[PERF_COUNT_SW_CGROUP_SWITCHES] = {
129 		.symbol = "cgroup-switches",
130 		.alias  = "",
131 	},
132 };
133 
134 const char *event_type(int type)
135 {
136 	switch (type) {
137 	case PERF_TYPE_HARDWARE:
138 		return "hardware";
139 
140 	case PERF_TYPE_SOFTWARE:
141 		return "software";
142 
143 	case PERF_TYPE_TRACEPOINT:
144 		return "tracepoint";
145 
146 	case PERF_TYPE_HW_CACHE:
147 		return "hardware-cache";
148 
149 	default:
150 		break;
151 	}
152 
153 	return "unknown";
154 }
155 
156 static char *get_config_str(struct list_head *head_terms, int type_term)
157 {
158 	struct parse_events_term *term;
159 
160 	if (!head_terms)
161 		return NULL;
162 
163 	list_for_each_entry(term, head_terms, list)
164 		if (term->type_term == type_term)
165 			return term->val.str;
166 
167 	return NULL;
168 }
169 
170 static char *get_config_metric_id(struct list_head *head_terms)
171 {
172 	return get_config_str(head_terms, PARSE_EVENTS__TERM_TYPE_METRIC_ID);
173 }
174 
175 static char *get_config_name(struct list_head *head_terms)
176 {
177 	return get_config_str(head_terms, PARSE_EVENTS__TERM_TYPE_NAME);
178 }
179 
180 /**
181  * fix_raw - For each raw term see if there is an event (aka alias) in pmu that
182  *           matches the raw's string value. If the string value matches an
183  *           event then change the term to be an event, if not then change it to
184  *           be a config term. For example, "read" may be an event of the PMU or
185  *           a raw hex encoding of 0xead. The fix-up is done late so the PMU of
186  *           the event can be determined and we don't need to scan all PMUs
187  *           ahead-of-time.
188  * @config_terms: the list of terms that may contain a raw term.
189  * @pmu: the PMU to scan for events from.
190  */
191 static void fix_raw(struct list_head *config_terms, struct perf_pmu *pmu)
192 {
193 	struct parse_events_term *term;
194 
195 	list_for_each_entry(term, config_terms, list) {
196 		u64 num;
197 
198 		if (term->type_term != PARSE_EVENTS__TERM_TYPE_RAW)
199 			continue;
200 
201 		if (perf_pmu__have_event(pmu, term->val.str)) {
202 			zfree(&term->config);
203 			term->config = term->val.str;
204 			term->type_val = PARSE_EVENTS__TERM_TYPE_NUM;
205 			term->type_term = PARSE_EVENTS__TERM_TYPE_USER;
206 			term->val.num = 1;
207 			term->no_value = true;
208 			continue;
209 		}
210 
211 		zfree(&term->config);
212 		term->config = strdup("config");
213 		errno = 0;
214 		num = strtoull(term->val.str + 1, NULL, 16);
215 		assert(errno == 0);
216 		free(term->val.str);
217 		term->type_val = PARSE_EVENTS__TERM_TYPE_NUM;
218 		term->type_term = PARSE_EVENTS__TERM_TYPE_CONFIG;
219 		term->val.num = num;
220 		term->no_value = false;
221 	}
222 }
223 
224 static struct evsel *
225 __add_event(struct list_head *list, int *idx,
226 	    struct perf_event_attr *attr,
227 	    bool init_attr,
228 	    const char *name, const char *metric_id, struct perf_pmu *pmu,
229 	    struct list_head *config_terms, bool auto_merge_stats,
230 	    const char *cpu_list)
231 {
232 	struct evsel *evsel;
233 	struct perf_cpu_map *cpus = pmu ? perf_cpu_map__get(pmu->cpus) :
234 			       cpu_list ? perf_cpu_map__new(cpu_list) : NULL;
235 
236 	if (pmu)
237 		perf_pmu__warn_invalid_formats(pmu);
238 
239 	if (pmu && (attr->type == PERF_TYPE_RAW || attr->type >= PERF_TYPE_MAX)) {
240 		perf_pmu__warn_invalid_config(pmu, attr->config, name,
241 					      PERF_PMU_FORMAT_VALUE_CONFIG, "config");
242 		perf_pmu__warn_invalid_config(pmu, attr->config1, name,
243 					      PERF_PMU_FORMAT_VALUE_CONFIG1, "config1");
244 		perf_pmu__warn_invalid_config(pmu, attr->config2, name,
245 					      PERF_PMU_FORMAT_VALUE_CONFIG2, "config2");
246 		perf_pmu__warn_invalid_config(pmu, attr->config3, name,
247 					      PERF_PMU_FORMAT_VALUE_CONFIG3, "config3");
248 	}
249 	if (init_attr)
250 		event_attr_init(attr);
251 
252 	evsel = evsel__new_idx(attr, *idx);
253 	if (!evsel) {
254 		perf_cpu_map__put(cpus);
255 		return NULL;
256 	}
257 
258 	(*idx)++;
259 	evsel->core.cpus = cpus;
260 	evsel->core.own_cpus = perf_cpu_map__get(cpus);
261 	evsel->core.requires_cpu = pmu ? pmu->is_uncore : false;
262 	evsel->core.is_pmu_core = pmu ? pmu->is_core : false;
263 	evsel->auto_merge_stats = auto_merge_stats;
264 	evsel->pmu = pmu;
265 	evsel->pmu_name = pmu ? strdup(pmu->name) : NULL;
266 
267 	if (name)
268 		evsel->name = strdup(name);
269 
270 	if (metric_id)
271 		evsel->metric_id = strdup(metric_id);
272 
273 	if (config_terms)
274 		list_splice_init(config_terms, &evsel->config_terms);
275 
276 	if (list)
277 		list_add_tail(&evsel->core.node, list);
278 
279 	return evsel;
280 }
281 
282 struct evsel *parse_events__add_event(int idx, struct perf_event_attr *attr,
283 				      const char *name, const char *metric_id,
284 				      struct perf_pmu *pmu)
285 {
286 	return __add_event(/*list=*/NULL, &idx, attr, /*init_attr=*/false, name,
287 			   metric_id, pmu, /*config_terms=*/NULL,
288 			   /*auto_merge_stats=*/false, /*cpu_list=*/NULL);
289 }
290 
291 static int add_event(struct list_head *list, int *idx,
292 		     struct perf_event_attr *attr, const char *name,
293 		     const char *metric_id, struct list_head *config_terms)
294 {
295 	return __add_event(list, idx, attr, /*init_attr*/true, name, metric_id,
296 			   /*pmu=*/NULL, config_terms,
297 			   /*auto_merge_stats=*/false, /*cpu_list=*/NULL) ? 0 : -ENOMEM;
298 }
299 
300 static int add_event_tool(struct list_head *list, int *idx,
301 			  enum perf_tool_event tool_event)
302 {
303 	struct evsel *evsel;
304 	struct perf_event_attr attr = {
305 		.type = PERF_TYPE_SOFTWARE,
306 		.config = PERF_COUNT_SW_DUMMY,
307 	};
308 
309 	evsel = __add_event(list, idx, &attr, /*init_attr=*/true, /*name=*/NULL,
310 			    /*metric_id=*/NULL, /*pmu=*/NULL,
311 			    /*config_terms=*/NULL, /*auto_merge_stats=*/false,
312 			    /*cpu_list=*/"0");
313 	if (!evsel)
314 		return -ENOMEM;
315 	evsel->tool_event = tool_event;
316 	if (tool_event == PERF_TOOL_DURATION_TIME
317 	    || tool_event == PERF_TOOL_USER_TIME
318 	    || tool_event == PERF_TOOL_SYSTEM_TIME) {
319 		free((char *)evsel->unit);
320 		evsel->unit = strdup("ns");
321 	}
322 	return 0;
323 }
324 
325 /**
326  * parse_aliases - search names for entries beginning or equalling str ignoring
327  *                 case. If mutliple entries in names match str then the longest
328  *                 is chosen.
329  * @str: The needle to look for.
330  * @names: The haystack to search.
331  * @size: The size of the haystack.
332  * @longest: Out argument giving the length of the matching entry.
333  */
334 static int parse_aliases(const char *str, const char *const names[][EVSEL__MAX_ALIASES], int size,
335 			 int *longest)
336 {
337 	*longest = -1;
338 	for (int i = 0; i < size; i++) {
339 		for (int j = 0; j < EVSEL__MAX_ALIASES && names[i][j]; j++) {
340 			int n = strlen(names[i][j]);
341 
342 			if (n > *longest && !strncasecmp(str, names[i][j], n))
343 				*longest = n;
344 		}
345 		if (*longest > 0)
346 			return i;
347 	}
348 
349 	return -1;
350 }
351 
352 typedef int config_term_func_t(struct perf_event_attr *attr,
353 			       struct parse_events_term *term,
354 			       struct parse_events_error *err);
355 static int config_term_common(struct perf_event_attr *attr,
356 			      struct parse_events_term *term,
357 			      struct parse_events_error *err);
358 static int config_attr(struct perf_event_attr *attr,
359 		       struct list_head *head,
360 		       struct parse_events_error *err,
361 		       config_term_func_t config_term);
362 
363 /**
364  * parse_events__decode_legacy_cache - Search name for the legacy cache event
365  *                                     name composed of 1, 2 or 3 hyphen
366  *                                     separated sections. The first section is
367  *                                     the cache type while the others are the
368  *                                     optional op and optional result. To make
369  *                                     life hard the names in the table also
370  *                                     contain hyphens and the longest name
371  *                                     should always be selected.
372  */
373 int parse_events__decode_legacy_cache(const char *name, int extended_pmu_type, __u64 *config)
374 {
375 	int len, cache_type = -1, cache_op = -1, cache_result = -1;
376 	const char *name_end = &name[strlen(name) + 1];
377 	const char *str = name;
378 
379 	cache_type = parse_aliases(str, evsel__hw_cache, PERF_COUNT_HW_CACHE_MAX, &len);
380 	if (cache_type == -1)
381 		return -EINVAL;
382 	str += len + 1;
383 
384 	if (str < name_end) {
385 		cache_op = parse_aliases(str, evsel__hw_cache_op,
386 					PERF_COUNT_HW_CACHE_OP_MAX, &len);
387 		if (cache_op >= 0) {
388 			if (!evsel__is_cache_op_valid(cache_type, cache_op))
389 				return -EINVAL;
390 			str += len + 1;
391 		} else {
392 			cache_result = parse_aliases(str, evsel__hw_cache_result,
393 						PERF_COUNT_HW_CACHE_RESULT_MAX, &len);
394 			if (cache_result >= 0)
395 				str += len + 1;
396 		}
397 	}
398 	if (str < name_end) {
399 		if (cache_op < 0) {
400 			cache_op = parse_aliases(str, evsel__hw_cache_op,
401 						PERF_COUNT_HW_CACHE_OP_MAX, &len);
402 			if (cache_op >= 0) {
403 				if (!evsel__is_cache_op_valid(cache_type, cache_op))
404 					return -EINVAL;
405 			}
406 		} else if (cache_result < 0) {
407 			cache_result = parse_aliases(str, evsel__hw_cache_result,
408 						PERF_COUNT_HW_CACHE_RESULT_MAX, &len);
409 		}
410 	}
411 
412 	/*
413 	 * Fall back to reads:
414 	 */
415 	if (cache_op == -1)
416 		cache_op = PERF_COUNT_HW_CACHE_OP_READ;
417 
418 	/*
419 	 * Fall back to accesses:
420 	 */
421 	if (cache_result == -1)
422 		cache_result = PERF_COUNT_HW_CACHE_RESULT_ACCESS;
423 
424 	*config = cache_type | (cache_op << 8) | (cache_result << 16);
425 	if (perf_pmus__supports_extended_type())
426 		*config |= (__u64)extended_pmu_type << PERF_PMU_TYPE_SHIFT;
427 	return 0;
428 }
429 
430 /**
431  * parse_events__filter_pmu - returns false if a wildcard PMU should be
432  *                            considered, true if it should be filtered.
433  */
434 bool parse_events__filter_pmu(const struct parse_events_state *parse_state,
435 			      const struct perf_pmu *pmu)
436 {
437 	if (parse_state->pmu_filter == NULL)
438 		return false;
439 
440 	return strcmp(parse_state->pmu_filter, pmu->name) != 0;
441 }
442 
443 int parse_events_add_cache(struct list_head *list, int *idx, const char *name,
444 			   struct parse_events_state *parse_state,
445 			   struct list_head *head_config)
446 {
447 	struct perf_pmu *pmu = NULL;
448 	bool found_supported = false;
449 	const char *config_name = get_config_name(head_config);
450 	const char *metric_id = get_config_metric_id(head_config);
451 
452 	/* Legacy cache events are only supported by core PMUs. */
453 	while ((pmu = perf_pmus__scan_core(pmu)) != NULL) {
454 		LIST_HEAD(config_terms);
455 		struct perf_event_attr attr;
456 		int ret;
457 
458 		if (parse_events__filter_pmu(parse_state, pmu))
459 			continue;
460 
461 		memset(&attr, 0, sizeof(attr));
462 		attr.type = PERF_TYPE_HW_CACHE;
463 
464 		ret = parse_events__decode_legacy_cache(name, pmu->type, &attr.config);
465 		if (ret)
466 			return ret;
467 
468 		found_supported = true;
469 
470 		if (head_config) {
471 			if (config_attr(&attr, head_config, parse_state->error, config_term_common))
472 				return -EINVAL;
473 
474 			if (get_config_terms(head_config, &config_terms))
475 				return -ENOMEM;
476 		}
477 
478 		if (__add_event(list, idx, &attr, /*init_attr*/true, config_name ?: name,
479 				metric_id, pmu, &config_terms, /*auto_merge_stats=*/false,
480 				/*cpu_list=*/NULL) == NULL)
481 			return -ENOMEM;
482 
483 		free_config_terms(&config_terms);
484 	}
485 	return found_supported ? 0 : -EINVAL;
486 }
487 
488 #ifdef HAVE_LIBTRACEEVENT
489 static void tracepoint_error(struct parse_events_error *e, int err,
490 			     const char *sys, const char *name, int column)
491 {
492 	const char *str;
493 	char help[BUFSIZ];
494 
495 	if (!e)
496 		return;
497 
498 	/*
499 	 * We get error directly from syscall errno ( > 0),
500 	 * or from encoded pointer's error ( < 0).
501 	 */
502 	err = abs(err);
503 
504 	switch (err) {
505 	case EACCES:
506 		str = "can't access trace events";
507 		break;
508 	case ENOENT:
509 		str = "unknown tracepoint";
510 		break;
511 	default:
512 		str = "failed to add tracepoint";
513 		break;
514 	}
515 
516 	tracing_path__strerror_open_tp(err, help, sizeof(help), sys, name);
517 	parse_events_error__handle(e, column, strdup(str), strdup(help));
518 }
519 
520 static int add_tracepoint(struct list_head *list, int *idx,
521 			  const char *sys_name, const char *evt_name,
522 			  struct parse_events_error *err,
523 			  struct list_head *head_config, void *loc_)
524 {
525 	YYLTYPE *loc = loc_;
526 	struct evsel *evsel = evsel__newtp_idx(sys_name, evt_name, (*idx)++);
527 
528 	if (IS_ERR(evsel)) {
529 		tracepoint_error(err, PTR_ERR(evsel), sys_name, evt_name, loc->first_column);
530 		return PTR_ERR(evsel);
531 	}
532 
533 	if (head_config) {
534 		LIST_HEAD(config_terms);
535 
536 		if (get_config_terms(head_config, &config_terms))
537 			return -ENOMEM;
538 		list_splice(&config_terms, &evsel->config_terms);
539 	}
540 
541 	list_add_tail(&evsel->core.node, list);
542 	return 0;
543 }
544 
545 static int add_tracepoint_multi_event(struct list_head *list, int *idx,
546 				      const char *sys_name, const char *evt_name,
547 				      struct parse_events_error *err,
548 				      struct list_head *head_config, YYLTYPE *loc)
549 {
550 	char *evt_path;
551 	struct dirent *evt_ent;
552 	DIR *evt_dir;
553 	int ret = 0, found = 0;
554 
555 	evt_path = get_events_file(sys_name);
556 	if (!evt_path) {
557 		tracepoint_error(err, errno, sys_name, evt_name, loc->first_column);
558 		return -1;
559 	}
560 	evt_dir = opendir(evt_path);
561 	if (!evt_dir) {
562 		put_events_file(evt_path);
563 		tracepoint_error(err, errno, sys_name, evt_name, loc->first_column);
564 		return -1;
565 	}
566 
567 	while (!ret && (evt_ent = readdir(evt_dir))) {
568 		if (!strcmp(evt_ent->d_name, ".")
569 		    || !strcmp(evt_ent->d_name, "..")
570 		    || !strcmp(evt_ent->d_name, "enable")
571 		    || !strcmp(evt_ent->d_name, "filter"))
572 			continue;
573 
574 		if (!strglobmatch(evt_ent->d_name, evt_name))
575 			continue;
576 
577 		found++;
578 
579 		ret = add_tracepoint(list, idx, sys_name, evt_ent->d_name,
580 				     err, head_config, loc);
581 	}
582 
583 	if (!found) {
584 		tracepoint_error(err, ENOENT, sys_name, evt_name, loc->first_column);
585 		ret = -1;
586 	}
587 
588 	put_events_file(evt_path);
589 	closedir(evt_dir);
590 	return ret;
591 }
592 
593 static int add_tracepoint_event(struct list_head *list, int *idx,
594 				const char *sys_name, const char *evt_name,
595 				struct parse_events_error *err,
596 				struct list_head *head_config, YYLTYPE *loc)
597 {
598 	return strpbrk(evt_name, "*?") ?
599 		add_tracepoint_multi_event(list, idx, sys_name, evt_name,
600 					   err, head_config, loc) :
601 		add_tracepoint(list, idx, sys_name, evt_name,
602 			       err, head_config, loc);
603 }
604 
605 static int add_tracepoint_multi_sys(struct list_head *list, int *idx,
606 				    const char *sys_name, const char *evt_name,
607 				    struct parse_events_error *err,
608 				    struct list_head *head_config, YYLTYPE *loc)
609 {
610 	struct dirent *events_ent;
611 	DIR *events_dir;
612 	int ret = 0;
613 
614 	events_dir = tracing_events__opendir();
615 	if (!events_dir) {
616 		tracepoint_error(err, errno, sys_name, evt_name, loc->first_column);
617 		return -1;
618 	}
619 
620 	while (!ret && (events_ent = readdir(events_dir))) {
621 		if (!strcmp(events_ent->d_name, ".")
622 		    || !strcmp(events_ent->d_name, "..")
623 		    || !strcmp(events_ent->d_name, "enable")
624 		    || !strcmp(events_ent->d_name, "header_event")
625 		    || !strcmp(events_ent->d_name, "header_page"))
626 			continue;
627 
628 		if (!strglobmatch(events_ent->d_name, sys_name))
629 			continue;
630 
631 		ret = add_tracepoint_event(list, idx, events_ent->d_name,
632 					   evt_name, err, head_config, loc);
633 	}
634 
635 	closedir(events_dir);
636 	return ret;
637 }
638 #endif /* HAVE_LIBTRACEEVENT */
639 
640 static int
641 parse_breakpoint_type(const char *type, struct perf_event_attr *attr)
642 {
643 	int i;
644 
645 	for (i = 0; i < 3; i++) {
646 		if (!type || !type[i])
647 			break;
648 
649 #define CHECK_SET_TYPE(bit)		\
650 do {					\
651 	if (attr->bp_type & bit)	\
652 		return -EINVAL;		\
653 	else				\
654 		attr->bp_type |= bit;	\
655 } while (0)
656 
657 		switch (type[i]) {
658 		case 'r':
659 			CHECK_SET_TYPE(HW_BREAKPOINT_R);
660 			break;
661 		case 'w':
662 			CHECK_SET_TYPE(HW_BREAKPOINT_W);
663 			break;
664 		case 'x':
665 			CHECK_SET_TYPE(HW_BREAKPOINT_X);
666 			break;
667 		default:
668 			return -EINVAL;
669 		}
670 	}
671 
672 #undef CHECK_SET_TYPE
673 
674 	if (!attr->bp_type) /* Default */
675 		attr->bp_type = HW_BREAKPOINT_R | HW_BREAKPOINT_W;
676 
677 	return 0;
678 }
679 
680 int parse_events_add_breakpoint(struct parse_events_state *parse_state,
681 				struct list_head *list,
682 				u64 addr, char *type, u64 len,
683 				struct list_head *head_config __maybe_unused)
684 {
685 	struct perf_event_attr attr;
686 	LIST_HEAD(config_terms);
687 	const char *name;
688 
689 	memset(&attr, 0, sizeof(attr));
690 	attr.bp_addr = addr;
691 
692 	if (parse_breakpoint_type(type, &attr))
693 		return -EINVAL;
694 
695 	/* Provide some defaults if len is not specified */
696 	if (!len) {
697 		if (attr.bp_type == HW_BREAKPOINT_X)
698 			len = sizeof(long);
699 		else
700 			len = HW_BREAKPOINT_LEN_4;
701 	}
702 
703 	attr.bp_len = len;
704 
705 	attr.type = PERF_TYPE_BREAKPOINT;
706 	attr.sample_period = 1;
707 
708 	if (head_config) {
709 		if (config_attr(&attr, head_config, parse_state->error,
710 				config_term_common))
711 			return -EINVAL;
712 
713 		if (get_config_terms(head_config, &config_terms))
714 			return -ENOMEM;
715 	}
716 
717 	name = get_config_name(head_config);
718 
719 	return add_event(list, &parse_state->idx, &attr, name, /*mertic_id=*/NULL,
720 			 &config_terms);
721 }
722 
723 static int check_type_val(struct parse_events_term *term,
724 			  struct parse_events_error *err,
725 			  int type)
726 {
727 	if (type == term->type_val)
728 		return 0;
729 
730 	if (err) {
731 		parse_events_error__handle(err, term->err_val,
732 					type == PARSE_EVENTS__TERM_TYPE_NUM
733 					? strdup("expected numeric value")
734 					: strdup("expected string value"),
735 					NULL);
736 	}
737 	return -EINVAL;
738 }
739 
740 /*
741  * Update according to parse-events.l
742  */
743 static const char *config_term_names[__PARSE_EVENTS__TERM_TYPE_NR] = {
744 	[PARSE_EVENTS__TERM_TYPE_USER]			= "<sysfs term>",
745 	[PARSE_EVENTS__TERM_TYPE_CONFIG]		= "config",
746 	[PARSE_EVENTS__TERM_TYPE_CONFIG1]		= "config1",
747 	[PARSE_EVENTS__TERM_TYPE_CONFIG2]		= "config2",
748 	[PARSE_EVENTS__TERM_TYPE_CONFIG3]		= "config3",
749 	[PARSE_EVENTS__TERM_TYPE_NAME]			= "name",
750 	[PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD]		= "period",
751 	[PARSE_EVENTS__TERM_TYPE_SAMPLE_FREQ]		= "freq",
752 	[PARSE_EVENTS__TERM_TYPE_BRANCH_SAMPLE_TYPE]	= "branch_type",
753 	[PARSE_EVENTS__TERM_TYPE_TIME]			= "time",
754 	[PARSE_EVENTS__TERM_TYPE_CALLGRAPH]		= "call-graph",
755 	[PARSE_EVENTS__TERM_TYPE_STACKSIZE]		= "stack-size",
756 	[PARSE_EVENTS__TERM_TYPE_NOINHERIT]		= "no-inherit",
757 	[PARSE_EVENTS__TERM_TYPE_INHERIT]		= "inherit",
758 	[PARSE_EVENTS__TERM_TYPE_MAX_STACK]		= "max-stack",
759 	[PARSE_EVENTS__TERM_TYPE_MAX_EVENTS]		= "nr",
760 	[PARSE_EVENTS__TERM_TYPE_OVERWRITE]		= "overwrite",
761 	[PARSE_EVENTS__TERM_TYPE_NOOVERWRITE]		= "no-overwrite",
762 	[PARSE_EVENTS__TERM_TYPE_DRV_CFG]		= "driver-config",
763 	[PARSE_EVENTS__TERM_TYPE_PERCORE]		= "percore",
764 	[PARSE_EVENTS__TERM_TYPE_AUX_OUTPUT]		= "aux-output",
765 	[PARSE_EVENTS__TERM_TYPE_AUX_SAMPLE_SIZE]	= "aux-sample-size",
766 	[PARSE_EVENTS__TERM_TYPE_METRIC_ID]		= "metric-id",
767 	[PARSE_EVENTS__TERM_TYPE_RAW]                   = "raw",
768 	[PARSE_EVENTS__TERM_TYPE_LEGACY_CACHE]          = "legacy-cache",
769 	[PARSE_EVENTS__TERM_TYPE_HARDWARE]              = "hardware",
770 };
771 
772 static bool config_term_shrinked;
773 
774 static bool
775 config_term_avail(int term_type, struct parse_events_error *err)
776 {
777 	char *err_str;
778 
779 	if (term_type < 0 || term_type >= __PARSE_EVENTS__TERM_TYPE_NR) {
780 		parse_events_error__handle(err, -1,
781 					strdup("Invalid term_type"), NULL);
782 		return false;
783 	}
784 	if (!config_term_shrinked)
785 		return true;
786 
787 	switch (term_type) {
788 	case PARSE_EVENTS__TERM_TYPE_CONFIG:
789 	case PARSE_EVENTS__TERM_TYPE_CONFIG1:
790 	case PARSE_EVENTS__TERM_TYPE_CONFIG2:
791 	case PARSE_EVENTS__TERM_TYPE_CONFIG3:
792 	case PARSE_EVENTS__TERM_TYPE_NAME:
793 	case PARSE_EVENTS__TERM_TYPE_METRIC_ID:
794 	case PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD:
795 	case PARSE_EVENTS__TERM_TYPE_PERCORE:
796 		return true;
797 	default:
798 		if (!err)
799 			return false;
800 
801 		/* term_type is validated so indexing is safe */
802 		if (asprintf(&err_str, "'%s' is not usable in 'perf stat'",
803 				config_term_names[term_type]) >= 0)
804 			parse_events_error__handle(err, -1, err_str, NULL);
805 		return false;
806 	}
807 }
808 
809 void parse_events__shrink_config_terms(void)
810 {
811 	config_term_shrinked = true;
812 }
813 
814 static int config_term_common(struct perf_event_attr *attr,
815 			      struct parse_events_term *term,
816 			      struct parse_events_error *err)
817 {
818 #define CHECK_TYPE_VAL(type)						   \
819 do {									   \
820 	if (check_type_val(term, err, PARSE_EVENTS__TERM_TYPE_ ## type)) \
821 		return -EINVAL;						   \
822 } while (0)
823 
824 	switch (term->type_term) {
825 	case PARSE_EVENTS__TERM_TYPE_CONFIG:
826 		CHECK_TYPE_VAL(NUM);
827 		attr->config = term->val.num;
828 		break;
829 	case PARSE_EVENTS__TERM_TYPE_CONFIG1:
830 		CHECK_TYPE_VAL(NUM);
831 		attr->config1 = term->val.num;
832 		break;
833 	case PARSE_EVENTS__TERM_TYPE_CONFIG2:
834 		CHECK_TYPE_VAL(NUM);
835 		attr->config2 = term->val.num;
836 		break;
837 	case PARSE_EVENTS__TERM_TYPE_CONFIG3:
838 		CHECK_TYPE_VAL(NUM);
839 		attr->config3 = term->val.num;
840 		break;
841 	case PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD:
842 		CHECK_TYPE_VAL(NUM);
843 		break;
844 	case PARSE_EVENTS__TERM_TYPE_SAMPLE_FREQ:
845 		CHECK_TYPE_VAL(NUM);
846 		break;
847 	case PARSE_EVENTS__TERM_TYPE_BRANCH_SAMPLE_TYPE:
848 		CHECK_TYPE_VAL(STR);
849 		if (strcmp(term->val.str, "no") &&
850 		    parse_branch_str(term->val.str,
851 				    &attr->branch_sample_type)) {
852 			parse_events_error__handle(err, term->err_val,
853 					strdup("invalid branch sample type"),
854 					NULL);
855 			return -EINVAL;
856 		}
857 		break;
858 	case PARSE_EVENTS__TERM_TYPE_TIME:
859 		CHECK_TYPE_VAL(NUM);
860 		if (term->val.num > 1) {
861 			parse_events_error__handle(err, term->err_val,
862 						strdup("expected 0 or 1"),
863 						NULL);
864 			return -EINVAL;
865 		}
866 		break;
867 	case PARSE_EVENTS__TERM_TYPE_CALLGRAPH:
868 		CHECK_TYPE_VAL(STR);
869 		break;
870 	case PARSE_EVENTS__TERM_TYPE_STACKSIZE:
871 		CHECK_TYPE_VAL(NUM);
872 		break;
873 	case PARSE_EVENTS__TERM_TYPE_INHERIT:
874 		CHECK_TYPE_VAL(NUM);
875 		break;
876 	case PARSE_EVENTS__TERM_TYPE_NOINHERIT:
877 		CHECK_TYPE_VAL(NUM);
878 		break;
879 	case PARSE_EVENTS__TERM_TYPE_OVERWRITE:
880 		CHECK_TYPE_VAL(NUM);
881 		break;
882 	case PARSE_EVENTS__TERM_TYPE_NOOVERWRITE:
883 		CHECK_TYPE_VAL(NUM);
884 		break;
885 	case PARSE_EVENTS__TERM_TYPE_NAME:
886 		CHECK_TYPE_VAL(STR);
887 		break;
888 	case PARSE_EVENTS__TERM_TYPE_METRIC_ID:
889 		CHECK_TYPE_VAL(STR);
890 		break;
891 	case PARSE_EVENTS__TERM_TYPE_RAW:
892 		CHECK_TYPE_VAL(STR);
893 		break;
894 	case PARSE_EVENTS__TERM_TYPE_MAX_STACK:
895 		CHECK_TYPE_VAL(NUM);
896 		break;
897 	case PARSE_EVENTS__TERM_TYPE_MAX_EVENTS:
898 		CHECK_TYPE_VAL(NUM);
899 		break;
900 	case PARSE_EVENTS__TERM_TYPE_PERCORE:
901 		CHECK_TYPE_VAL(NUM);
902 		if ((unsigned int)term->val.num > 1) {
903 			parse_events_error__handle(err, term->err_val,
904 						strdup("expected 0 or 1"),
905 						NULL);
906 			return -EINVAL;
907 		}
908 		break;
909 	case PARSE_EVENTS__TERM_TYPE_AUX_OUTPUT:
910 		CHECK_TYPE_VAL(NUM);
911 		break;
912 	case PARSE_EVENTS__TERM_TYPE_AUX_SAMPLE_SIZE:
913 		CHECK_TYPE_VAL(NUM);
914 		if (term->val.num > UINT_MAX) {
915 			parse_events_error__handle(err, term->err_val,
916 						strdup("too big"),
917 						NULL);
918 			return -EINVAL;
919 		}
920 		break;
921 	default:
922 		parse_events_error__handle(err, term->err_term,
923 				strdup("unknown term"),
924 				parse_events_formats_error_string(NULL));
925 		return -EINVAL;
926 	}
927 
928 	/*
929 	 * Check term availability after basic checking so
930 	 * PARSE_EVENTS__TERM_TYPE_USER can be found and filtered.
931 	 *
932 	 * If check availability at the entry of this function,
933 	 * user will see "'<sysfs term>' is not usable in 'perf stat'"
934 	 * if an invalid config term is provided for legacy events
935 	 * (for example, instructions/badterm/...), which is confusing.
936 	 */
937 	if (!config_term_avail(term->type_term, err))
938 		return -EINVAL;
939 	return 0;
940 #undef CHECK_TYPE_VAL
941 }
942 
943 static int config_term_pmu(struct perf_event_attr *attr,
944 			   struct parse_events_term *term,
945 			   struct parse_events_error *err)
946 {
947 	if (term->type_term == PARSE_EVENTS__TERM_TYPE_LEGACY_CACHE) {
948 		const struct perf_pmu *pmu = perf_pmus__find_by_type(attr->type);
949 
950 		if (!pmu) {
951 			char *err_str;
952 
953 			if (asprintf(&err_str, "Failed to find PMU for type %d", attr->type) >= 0)
954 				parse_events_error__handle(err, term->err_term,
955 							   err_str, /*help=*/NULL);
956 			return -EINVAL;
957 		}
958 		if (perf_pmu__supports_legacy_cache(pmu)) {
959 			attr->type = PERF_TYPE_HW_CACHE;
960 			return parse_events__decode_legacy_cache(term->config, pmu->type,
961 								 &attr->config);
962 		} else
963 			term->type_term = PARSE_EVENTS__TERM_TYPE_USER;
964 	}
965 	if (term->type_term == PARSE_EVENTS__TERM_TYPE_HARDWARE) {
966 		const struct perf_pmu *pmu = perf_pmus__find_by_type(attr->type);
967 
968 		if (!pmu) {
969 			char *err_str;
970 
971 			if (asprintf(&err_str, "Failed to find PMU for type %d", attr->type) >= 0)
972 				parse_events_error__handle(err, term->err_term,
973 							   err_str, /*help=*/NULL);
974 			return -EINVAL;
975 		}
976 		attr->type = PERF_TYPE_HARDWARE;
977 		attr->config = term->val.num;
978 		if (perf_pmus__supports_extended_type())
979 			attr->config |= (__u64)pmu->type << PERF_PMU_TYPE_SHIFT;
980 		return 0;
981 	}
982 	if (term->type_term == PARSE_EVENTS__TERM_TYPE_USER ||
983 	    term->type_term == PARSE_EVENTS__TERM_TYPE_DRV_CFG) {
984 		/*
985 		 * Always succeed for sysfs terms, as we dont know
986 		 * at this point what type they need to have.
987 		 */
988 		return 0;
989 	}
990 	return config_term_common(attr, term, err);
991 }
992 
993 #ifdef HAVE_LIBTRACEEVENT
994 static int config_term_tracepoint(struct perf_event_attr *attr,
995 				  struct parse_events_term *term,
996 				  struct parse_events_error *err)
997 {
998 	switch (term->type_term) {
999 	case PARSE_EVENTS__TERM_TYPE_CALLGRAPH:
1000 	case PARSE_EVENTS__TERM_TYPE_STACKSIZE:
1001 	case PARSE_EVENTS__TERM_TYPE_INHERIT:
1002 	case PARSE_EVENTS__TERM_TYPE_NOINHERIT:
1003 	case PARSE_EVENTS__TERM_TYPE_MAX_STACK:
1004 	case PARSE_EVENTS__TERM_TYPE_MAX_EVENTS:
1005 	case PARSE_EVENTS__TERM_TYPE_OVERWRITE:
1006 	case PARSE_EVENTS__TERM_TYPE_NOOVERWRITE:
1007 	case PARSE_EVENTS__TERM_TYPE_AUX_OUTPUT:
1008 	case PARSE_EVENTS__TERM_TYPE_AUX_SAMPLE_SIZE:
1009 		return config_term_common(attr, term, err);
1010 	default:
1011 		if (err) {
1012 			parse_events_error__handle(err, term->err_term,
1013 				strdup("unknown term"),
1014 				strdup("valid terms: call-graph,stack-size\n"));
1015 		}
1016 		return -EINVAL;
1017 	}
1018 
1019 	return 0;
1020 }
1021 #endif
1022 
1023 static int config_attr(struct perf_event_attr *attr,
1024 		       struct list_head *head,
1025 		       struct parse_events_error *err,
1026 		       config_term_func_t config_term)
1027 {
1028 	struct parse_events_term *term;
1029 
1030 	list_for_each_entry(term, head, list)
1031 		if (config_term(attr, term, err))
1032 			return -EINVAL;
1033 
1034 	return 0;
1035 }
1036 
1037 static int get_config_terms(struct list_head *head_config,
1038 			    struct list_head *head_terms __maybe_unused)
1039 {
1040 #define ADD_CONFIG_TERM(__type, __weak)				\
1041 	struct evsel_config_term *__t;			\
1042 								\
1043 	__t = zalloc(sizeof(*__t));				\
1044 	if (!__t)						\
1045 		return -ENOMEM;					\
1046 								\
1047 	INIT_LIST_HEAD(&__t->list);				\
1048 	__t->type       = EVSEL__CONFIG_TERM_ ## __type;	\
1049 	__t->weak	= __weak;				\
1050 	list_add_tail(&__t->list, head_terms)
1051 
1052 #define ADD_CONFIG_TERM_VAL(__type, __name, __val, __weak)	\
1053 do {								\
1054 	ADD_CONFIG_TERM(__type, __weak);			\
1055 	__t->val.__name = __val;				\
1056 } while (0)
1057 
1058 #define ADD_CONFIG_TERM_STR(__type, __val, __weak)		\
1059 do {								\
1060 	ADD_CONFIG_TERM(__type, __weak);			\
1061 	__t->val.str = strdup(__val);				\
1062 	if (!__t->val.str) {					\
1063 		zfree(&__t);					\
1064 		return -ENOMEM;					\
1065 	}							\
1066 	__t->free_str = true;					\
1067 } while (0)
1068 
1069 	struct parse_events_term *term;
1070 
1071 	list_for_each_entry(term, head_config, list) {
1072 		switch (term->type_term) {
1073 		case PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD:
1074 			ADD_CONFIG_TERM_VAL(PERIOD, period, term->val.num, term->weak);
1075 			break;
1076 		case PARSE_EVENTS__TERM_TYPE_SAMPLE_FREQ:
1077 			ADD_CONFIG_TERM_VAL(FREQ, freq, term->val.num, term->weak);
1078 			break;
1079 		case PARSE_EVENTS__TERM_TYPE_TIME:
1080 			ADD_CONFIG_TERM_VAL(TIME, time, term->val.num, term->weak);
1081 			break;
1082 		case PARSE_EVENTS__TERM_TYPE_CALLGRAPH:
1083 			ADD_CONFIG_TERM_STR(CALLGRAPH, term->val.str, term->weak);
1084 			break;
1085 		case PARSE_EVENTS__TERM_TYPE_BRANCH_SAMPLE_TYPE:
1086 			ADD_CONFIG_TERM_STR(BRANCH, term->val.str, term->weak);
1087 			break;
1088 		case PARSE_EVENTS__TERM_TYPE_STACKSIZE:
1089 			ADD_CONFIG_TERM_VAL(STACK_USER, stack_user,
1090 					    term->val.num, term->weak);
1091 			break;
1092 		case PARSE_EVENTS__TERM_TYPE_INHERIT:
1093 			ADD_CONFIG_TERM_VAL(INHERIT, inherit,
1094 					    term->val.num ? 1 : 0, term->weak);
1095 			break;
1096 		case PARSE_EVENTS__TERM_TYPE_NOINHERIT:
1097 			ADD_CONFIG_TERM_VAL(INHERIT, inherit,
1098 					    term->val.num ? 0 : 1, term->weak);
1099 			break;
1100 		case PARSE_EVENTS__TERM_TYPE_MAX_STACK:
1101 			ADD_CONFIG_TERM_VAL(MAX_STACK, max_stack,
1102 					    term->val.num, term->weak);
1103 			break;
1104 		case PARSE_EVENTS__TERM_TYPE_MAX_EVENTS:
1105 			ADD_CONFIG_TERM_VAL(MAX_EVENTS, max_events,
1106 					    term->val.num, term->weak);
1107 			break;
1108 		case PARSE_EVENTS__TERM_TYPE_OVERWRITE:
1109 			ADD_CONFIG_TERM_VAL(OVERWRITE, overwrite,
1110 					    term->val.num ? 1 : 0, term->weak);
1111 			break;
1112 		case PARSE_EVENTS__TERM_TYPE_NOOVERWRITE:
1113 			ADD_CONFIG_TERM_VAL(OVERWRITE, overwrite,
1114 					    term->val.num ? 0 : 1, term->weak);
1115 			break;
1116 		case PARSE_EVENTS__TERM_TYPE_DRV_CFG:
1117 			ADD_CONFIG_TERM_STR(DRV_CFG, term->val.str, term->weak);
1118 			break;
1119 		case PARSE_EVENTS__TERM_TYPE_PERCORE:
1120 			ADD_CONFIG_TERM_VAL(PERCORE, percore,
1121 					    term->val.num ? true : false, term->weak);
1122 			break;
1123 		case PARSE_EVENTS__TERM_TYPE_AUX_OUTPUT:
1124 			ADD_CONFIG_TERM_VAL(AUX_OUTPUT, aux_output,
1125 					    term->val.num ? 1 : 0, term->weak);
1126 			break;
1127 		case PARSE_EVENTS__TERM_TYPE_AUX_SAMPLE_SIZE:
1128 			ADD_CONFIG_TERM_VAL(AUX_SAMPLE_SIZE, aux_sample_size,
1129 					    term->val.num, term->weak);
1130 			break;
1131 		default:
1132 			break;
1133 		}
1134 	}
1135 	return 0;
1136 }
1137 
1138 /*
1139  * Add EVSEL__CONFIG_TERM_CFG_CHG where cfg_chg will have a bit set for
1140  * each bit of attr->config that the user has changed.
1141  */
1142 static int get_config_chgs(struct perf_pmu *pmu, struct list_head *head_config,
1143 			   struct list_head *head_terms)
1144 {
1145 	struct parse_events_term *term;
1146 	u64 bits = 0;
1147 	int type;
1148 
1149 	list_for_each_entry(term, head_config, list) {
1150 		switch (term->type_term) {
1151 		case PARSE_EVENTS__TERM_TYPE_USER:
1152 			type = perf_pmu__format_type(pmu, term->config);
1153 			if (type != PERF_PMU_FORMAT_VALUE_CONFIG)
1154 				continue;
1155 			bits |= perf_pmu__format_bits(pmu, term->config);
1156 			break;
1157 		case PARSE_EVENTS__TERM_TYPE_CONFIG:
1158 			bits = ~(u64)0;
1159 			break;
1160 		default:
1161 			break;
1162 		}
1163 	}
1164 
1165 	if (bits)
1166 		ADD_CONFIG_TERM_VAL(CFG_CHG, cfg_chg, bits, false);
1167 
1168 #undef ADD_CONFIG_TERM
1169 	return 0;
1170 }
1171 
1172 int parse_events_add_tracepoint(struct list_head *list, int *idx,
1173 				const char *sys, const char *event,
1174 				struct parse_events_error *err,
1175 				struct list_head *head_config, void *loc_)
1176 {
1177 	YYLTYPE *loc = loc_;
1178 #ifdef HAVE_LIBTRACEEVENT
1179 	if (head_config) {
1180 		struct perf_event_attr attr;
1181 
1182 		if (config_attr(&attr, head_config, err,
1183 				config_term_tracepoint))
1184 			return -EINVAL;
1185 	}
1186 
1187 	if (strpbrk(sys, "*?"))
1188 		return add_tracepoint_multi_sys(list, idx, sys, event,
1189 						err, head_config, loc);
1190 	else
1191 		return add_tracepoint_event(list, idx, sys, event,
1192 					    err, head_config, loc);
1193 #else
1194 	(void)list;
1195 	(void)idx;
1196 	(void)sys;
1197 	(void)event;
1198 	(void)head_config;
1199 	parse_events_error__handle(err, loc->first_column, strdup("unsupported tracepoint"),
1200 				strdup("libtraceevent is necessary for tracepoint support"));
1201 	return -1;
1202 #endif
1203 }
1204 
1205 static int __parse_events_add_numeric(struct parse_events_state *parse_state,
1206 				struct list_head *list,
1207 				struct perf_pmu *pmu, u32 type, u32 extended_type,
1208 				u64 config, struct list_head *head_config)
1209 {
1210 	struct perf_event_attr attr;
1211 	LIST_HEAD(config_terms);
1212 	const char *name, *metric_id;
1213 	int ret;
1214 
1215 	memset(&attr, 0, sizeof(attr));
1216 	attr.type = type;
1217 	attr.config = config;
1218 	if (extended_type && (type == PERF_TYPE_HARDWARE || type == PERF_TYPE_HW_CACHE)) {
1219 		assert(perf_pmus__supports_extended_type());
1220 		attr.config |= (u64)extended_type << PERF_PMU_TYPE_SHIFT;
1221 	}
1222 
1223 	if (head_config) {
1224 		if (config_attr(&attr, head_config, parse_state->error,
1225 				config_term_common))
1226 			return -EINVAL;
1227 
1228 		if (get_config_terms(head_config, &config_terms))
1229 			return -ENOMEM;
1230 	}
1231 
1232 	name = get_config_name(head_config);
1233 	metric_id = get_config_metric_id(head_config);
1234 	ret = __add_event(list, &parse_state->idx, &attr, /*init_attr*/true, name,
1235 			metric_id, pmu, &config_terms, /*auto_merge_stats=*/false,
1236 			/*cpu_list=*/NULL) ? 0 : -ENOMEM;
1237 	free_config_terms(&config_terms);
1238 	return ret;
1239 }
1240 
1241 int parse_events_add_numeric(struct parse_events_state *parse_state,
1242 			     struct list_head *list,
1243 			     u32 type, u64 config,
1244 			     struct list_head *head_config,
1245 			     bool wildcard)
1246 {
1247 	struct perf_pmu *pmu = NULL;
1248 	bool found_supported = false;
1249 
1250 	/* Wildcards on numeric values are only supported by core PMUs. */
1251 	if (wildcard && perf_pmus__supports_extended_type()) {
1252 		while ((pmu = perf_pmus__scan_core(pmu)) != NULL) {
1253 			int ret;
1254 
1255 			found_supported = true;
1256 			if (parse_events__filter_pmu(parse_state, pmu))
1257 				continue;
1258 
1259 			ret = __parse_events_add_numeric(parse_state, list, pmu,
1260 							 type, pmu->type,
1261 							 config, head_config);
1262 			if (ret)
1263 				return ret;
1264 		}
1265 		if (found_supported)
1266 			return 0;
1267 	}
1268 	return __parse_events_add_numeric(parse_state, list, perf_pmus__find_by_type(type),
1269 					type, /*extended_type=*/0, config, head_config);
1270 }
1271 
1272 int parse_events_add_tool(struct parse_events_state *parse_state,
1273 			  struct list_head *list,
1274 			  int tool_event)
1275 {
1276 	return add_event_tool(list, &parse_state->idx, tool_event);
1277 }
1278 
1279 static bool config_term_percore(struct list_head *config_terms)
1280 {
1281 	struct evsel_config_term *term;
1282 
1283 	list_for_each_entry(term, config_terms, list) {
1284 		if (term->type == EVSEL__CONFIG_TERM_PERCORE)
1285 			return term->val.percore;
1286 	}
1287 
1288 	return false;
1289 }
1290 
1291 int parse_events_add_pmu(struct parse_events_state *parse_state,
1292 			 struct list_head *list, const char *name,
1293 			 struct list_head *head_config,
1294 			 bool auto_merge_stats, void *loc_)
1295 {
1296 	struct perf_event_attr attr;
1297 	struct perf_pmu_info info;
1298 	struct perf_pmu *pmu;
1299 	struct evsel *evsel;
1300 	struct parse_events_error *err = parse_state->error;
1301 	YYLTYPE *loc = loc_;
1302 	LIST_HEAD(config_terms);
1303 
1304 	pmu = parse_state->fake_pmu ?: perf_pmus__find(name);
1305 
1306 	if (verbose > 1 && !(pmu && pmu->selectable)) {
1307 		fprintf(stderr, "Attempting to add event pmu '%s' with '",
1308 			name);
1309 		if (head_config) {
1310 			struct parse_events_term *term;
1311 
1312 			list_for_each_entry(term, head_config, list) {
1313 				fprintf(stderr, "%s,", term->config);
1314 			}
1315 		}
1316 		fprintf(stderr, "' that may result in non-fatal errors\n");
1317 	}
1318 
1319 	if (!pmu) {
1320 		char *err_str;
1321 
1322 		if (asprintf(&err_str,
1323 				"Cannot find PMU `%s'. Missing kernel support?",
1324 				name) >= 0)
1325 			parse_events_error__handle(err, loc->first_column, err_str, NULL);
1326 		return -EINVAL;
1327 	}
1328 	if (head_config)
1329 		fix_raw(head_config, pmu);
1330 
1331 	if (pmu->default_config) {
1332 		memcpy(&attr, pmu->default_config,
1333 		       sizeof(struct perf_event_attr));
1334 	} else {
1335 		memset(&attr, 0, sizeof(attr));
1336 	}
1337 	attr.type = pmu->type;
1338 
1339 	if (!head_config) {
1340 		evsel = __add_event(list, &parse_state->idx, &attr,
1341 				    /*init_attr=*/true, /*name=*/NULL,
1342 				    /*metric_id=*/NULL, pmu,
1343 				    /*config_terms=*/NULL, auto_merge_stats,
1344 				    /*cpu_list=*/NULL);
1345 		return evsel ? 0 : -ENOMEM;
1346 	}
1347 
1348 	if (!parse_state->fake_pmu && perf_pmu__check_alias(pmu, head_config, &info, err))
1349 		return -EINVAL;
1350 
1351 	if (verbose > 1) {
1352 		fprintf(stderr, "After aliases, add event pmu '%s' with '",
1353 			name);
1354 		if (head_config) {
1355 			struct parse_events_term *term;
1356 
1357 			list_for_each_entry(term, head_config, list) {
1358 				fprintf(stderr, "%s,", term->config);
1359 			}
1360 		}
1361 		fprintf(stderr, "' that may result in non-fatal errors\n");
1362 	}
1363 
1364 	/*
1365 	 * Configure hardcoded terms first, no need to check
1366 	 * return value when called with fail == 0 ;)
1367 	 */
1368 	if (config_attr(&attr, head_config, parse_state->error, config_term_pmu))
1369 		return -EINVAL;
1370 
1371 	if (get_config_terms(head_config, &config_terms))
1372 		return -ENOMEM;
1373 
1374 	/*
1375 	 * When using default config, record which bits of attr->config were
1376 	 * changed by the user.
1377 	 */
1378 	if (pmu->default_config && get_config_chgs(pmu, head_config, &config_terms))
1379 		return -ENOMEM;
1380 
1381 	if (!parse_state->fake_pmu && perf_pmu__config(pmu, &attr, head_config, parse_state->error)) {
1382 		free_config_terms(&config_terms);
1383 		return -EINVAL;
1384 	}
1385 
1386 	evsel = __add_event(list, &parse_state->idx, &attr, /*init_attr=*/true,
1387 			    get_config_name(head_config),
1388 			    get_config_metric_id(head_config), pmu,
1389 			    &config_terms, auto_merge_stats, /*cpu_list=*/NULL);
1390 	if (!evsel)
1391 		return -ENOMEM;
1392 
1393 	if (evsel->name)
1394 		evsel->use_config_name = true;
1395 
1396 	evsel->percore = config_term_percore(&evsel->config_terms);
1397 
1398 	if (parse_state->fake_pmu)
1399 		return 0;
1400 
1401 	free((char *)evsel->unit);
1402 	evsel->unit = strdup(info.unit);
1403 	evsel->scale = info.scale;
1404 	evsel->per_pkg = info.per_pkg;
1405 	evsel->snapshot = info.snapshot;
1406 	return 0;
1407 }
1408 
1409 int parse_events_multi_pmu_add(struct parse_events_state *parse_state,
1410 			       char *str, struct list_head *head,
1411 			       struct list_head **listp, void *loc_)
1412 {
1413 	struct parse_events_term *term;
1414 	struct list_head *list = NULL;
1415 	struct list_head *orig_head = NULL;
1416 	struct perf_pmu *pmu = NULL;
1417 	YYLTYPE *loc = loc_;
1418 	int ok = 0;
1419 	const char *config;
1420 
1421 	*listp = NULL;
1422 
1423 	if (!head) {
1424 		head = malloc(sizeof(struct list_head));
1425 		if (!head)
1426 			goto out_err;
1427 
1428 		INIT_LIST_HEAD(head);
1429 	}
1430 	config = strdup(str);
1431 	if (!config)
1432 		goto out_err;
1433 
1434 	if (parse_events_term__num(&term,
1435 				   PARSE_EVENTS__TERM_TYPE_USER,
1436 				   config, 1, false, NULL,
1437 					NULL) < 0) {
1438 		zfree(&config);
1439 		goto out_err;
1440 	}
1441 	list_add_tail(&term->list, head);
1442 
1443 	/* Add it for all PMUs that support the alias */
1444 	list = malloc(sizeof(struct list_head));
1445 	if (!list)
1446 		goto out_err;
1447 
1448 	INIT_LIST_HEAD(list);
1449 
1450 	while ((pmu = perf_pmus__scan(pmu)) != NULL) {
1451 		bool auto_merge_stats;
1452 
1453 		if (parse_events__filter_pmu(parse_state, pmu))
1454 			continue;
1455 
1456 		if (!perf_pmu__have_event(pmu, str))
1457 			continue;
1458 
1459 		auto_merge_stats = perf_pmu__auto_merge_stats(pmu);
1460 		parse_events_copy_term_list(head, &orig_head);
1461 		if (!parse_events_add_pmu(parse_state, list, pmu->name,
1462 					  orig_head, auto_merge_stats, loc)) {
1463 			pr_debug("%s -> %s/%s/\n", str, pmu->name, str);
1464 			ok++;
1465 		}
1466 		parse_events_terms__delete(orig_head);
1467 	}
1468 
1469 	if (parse_state->fake_pmu) {
1470 		if (!parse_events_add_pmu(parse_state, list, str, head,
1471 					  /*auto_merge_stats=*/true, loc)) {
1472 			pr_debug("%s -> %s/%s/\n", str, "fake_pmu", str);
1473 			ok++;
1474 		}
1475 	}
1476 
1477 out_err:
1478 	if (ok)
1479 		*listp = list;
1480 	else
1481 		free(list);
1482 
1483 	parse_events_terms__delete(head);
1484 	return ok ? 0 : -1;
1485 }
1486 
1487 int parse_events__modifier_group(struct list_head *list,
1488 				 char *event_mod)
1489 {
1490 	return parse_events__modifier_event(list, event_mod, true);
1491 }
1492 
1493 void parse_events__set_leader(char *name, struct list_head *list)
1494 {
1495 	struct evsel *leader;
1496 
1497 	if (list_empty(list)) {
1498 		WARN_ONCE(true, "WARNING: failed to set leader: empty list");
1499 		return;
1500 	}
1501 
1502 	leader = list_first_entry(list, struct evsel, core.node);
1503 	__perf_evlist__set_leader(list, &leader->core);
1504 	leader->group_name = name;
1505 }
1506 
1507 /* list_event is assumed to point to malloc'ed memory */
1508 void parse_events_update_lists(struct list_head *list_event,
1509 			       struct list_head *list_all)
1510 {
1511 	/*
1512 	 * Called for single event definition. Update the
1513 	 * 'all event' list, and reinit the 'single event'
1514 	 * list, for next event definition.
1515 	 */
1516 	list_splice_tail(list_event, list_all);
1517 	free(list_event);
1518 }
1519 
1520 struct event_modifier {
1521 	int eu;
1522 	int ek;
1523 	int eh;
1524 	int eH;
1525 	int eG;
1526 	int eI;
1527 	int precise;
1528 	int precise_max;
1529 	int exclude_GH;
1530 	int sample_read;
1531 	int pinned;
1532 	int weak;
1533 	int exclusive;
1534 	int bpf_counter;
1535 };
1536 
1537 static int get_event_modifier(struct event_modifier *mod, char *str,
1538 			       struct evsel *evsel)
1539 {
1540 	int eu = evsel ? evsel->core.attr.exclude_user : 0;
1541 	int ek = evsel ? evsel->core.attr.exclude_kernel : 0;
1542 	int eh = evsel ? evsel->core.attr.exclude_hv : 0;
1543 	int eH = evsel ? evsel->core.attr.exclude_host : 0;
1544 	int eG = evsel ? evsel->core.attr.exclude_guest : 0;
1545 	int eI = evsel ? evsel->core.attr.exclude_idle : 0;
1546 	int precise = evsel ? evsel->core.attr.precise_ip : 0;
1547 	int precise_max = 0;
1548 	int sample_read = 0;
1549 	int pinned = evsel ? evsel->core.attr.pinned : 0;
1550 	int exclusive = evsel ? evsel->core.attr.exclusive : 0;
1551 
1552 	int exclude = eu | ek | eh;
1553 	int exclude_GH = evsel ? evsel->exclude_GH : 0;
1554 	int weak = 0;
1555 	int bpf_counter = 0;
1556 
1557 	memset(mod, 0, sizeof(*mod));
1558 
1559 	while (*str) {
1560 		if (*str == 'u') {
1561 			if (!exclude)
1562 				exclude = eu = ek = eh = 1;
1563 			if (!exclude_GH && !perf_guest)
1564 				eG = 1;
1565 			eu = 0;
1566 		} else if (*str == 'k') {
1567 			if (!exclude)
1568 				exclude = eu = ek = eh = 1;
1569 			ek = 0;
1570 		} else if (*str == 'h') {
1571 			if (!exclude)
1572 				exclude = eu = ek = eh = 1;
1573 			eh = 0;
1574 		} else if (*str == 'G') {
1575 			if (!exclude_GH)
1576 				exclude_GH = eG = eH = 1;
1577 			eG = 0;
1578 		} else if (*str == 'H') {
1579 			if (!exclude_GH)
1580 				exclude_GH = eG = eH = 1;
1581 			eH = 0;
1582 		} else if (*str == 'I') {
1583 			eI = 1;
1584 		} else if (*str == 'p') {
1585 			precise++;
1586 			/* use of precise requires exclude_guest */
1587 			if (!exclude_GH)
1588 				eG = 1;
1589 		} else if (*str == 'P') {
1590 			precise_max = 1;
1591 		} else if (*str == 'S') {
1592 			sample_read = 1;
1593 		} else if (*str == 'D') {
1594 			pinned = 1;
1595 		} else if (*str == 'e') {
1596 			exclusive = 1;
1597 		} else if (*str == 'W') {
1598 			weak = 1;
1599 		} else if (*str == 'b') {
1600 			bpf_counter = 1;
1601 		} else
1602 			break;
1603 
1604 		++str;
1605 	}
1606 
1607 	/*
1608 	 * precise ip:
1609 	 *
1610 	 *  0 - SAMPLE_IP can have arbitrary skid
1611 	 *  1 - SAMPLE_IP must have constant skid
1612 	 *  2 - SAMPLE_IP requested to have 0 skid
1613 	 *  3 - SAMPLE_IP must have 0 skid
1614 	 *
1615 	 *  See also PERF_RECORD_MISC_EXACT_IP
1616 	 */
1617 	if (precise > 3)
1618 		return -EINVAL;
1619 
1620 	mod->eu = eu;
1621 	mod->ek = ek;
1622 	mod->eh = eh;
1623 	mod->eH = eH;
1624 	mod->eG = eG;
1625 	mod->eI = eI;
1626 	mod->precise = precise;
1627 	mod->precise_max = precise_max;
1628 	mod->exclude_GH = exclude_GH;
1629 	mod->sample_read = sample_read;
1630 	mod->pinned = pinned;
1631 	mod->weak = weak;
1632 	mod->bpf_counter = bpf_counter;
1633 	mod->exclusive = exclusive;
1634 
1635 	return 0;
1636 }
1637 
1638 /*
1639  * Basic modifier sanity check to validate it contains only one
1640  * instance of any modifier (apart from 'p') present.
1641  */
1642 static int check_modifier(char *str)
1643 {
1644 	char *p = str;
1645 
1646 	/* The sizeof includes 0 byte as well. */
1647 	if (strlen(str) > (sizeof("ukhGHpppPSDIWeb") - 1))
1648 		return -1;
1649 
1650 	while (*p) {
1651 		if (*p != 'p' && strchr(p + 1, *p))
1652 			return -1;
1653 		p++;
1654 	}
1655 
1656 	return 0;
1657 }
1658 
1659 int parse_events__modifier_event(struct list_head *list, char *str, bool add)
1660 {
1661 	struct evsel *evsel;
1662 	struct event_modifier mod;
1663 
1664 	if (str == NULL)
1665 		return 0;
1666 
1667 	if (check_modifier(str))
1668 		return -EINVAL;
1669 
1670 	if (!add && get_event_modifier(&mod, str, NULL))
1671 		return -EINVAL;
1672 
1673 	__evlist__for_each_entry(list, evsel) {
1674 		if (add && get_event_modifier(&mod, str, evsel))
1675 			return -EINVAL;
1676 
1677 		evsel->core.attr.exclude_user   = mod.eu;
1678 		evsel->core.attr.exclude_kernel = mod.ek;
1679 		evsel->core.attr.exclude_hv     = mod.eh;
1680 		evsel->core.attr.precise_ip     = mod.precise;
1681 		evsel->core.attr.exclude_host   = mod.eH;
1682 		evsel->core.attr.exclude_guest  = mod.eG;
1683 		evsel->core.attr.exclude_idle   = mod.eI;
1684 		evsel->exclude_GH          = mod.exclude_GH;
1685 		evsel->sample_read         = mod.sample_read;
1686 		evsel->precise_max         = mod.precise_max;
1687 		evsel->weak_group	   = mod.weak;
1688 		evsel->bpf_counter	   = mod.bpf_counter;
1689 
1690 		if (evsel__is_group_leader(evsel)) {
1691 			evsel->core.attr.pinned = mod.pinned;
1692 			evsel->core.attr.exclusive = mod.exclusive;
1693 		}
1694 	}
1695 
1696 	return 0;
1697 }
1698 
1699 int parse_events_name(struct list_head *list, const char *name)
1700 {
1701 	struct evsel *evsel;
1702 
1703 	__evlist__for_each_entry(list, evsel) {
1704 		if (!evsel->name) {
1705 			evsel->name = strdup(name);
1706 			if (!evsel->name)
1707 				return -ENOMEM;
1708 		}
1709 	}
1710 
1711 	return 0;
1712 }
1713 
1714 static int parse_events__scanner(const char *str,
1715 				 FILE *input,
1716 				 struct parse_events_state *parse_state)
1717 {
1718 	YY_BUFFER_STATE buffer;
1719 	void *scanner;
1720 	int ret;
1721 
1722 	ret = parse_events_lex_init_extra(parse_state, &scanner);
1723 	if (ret)
1724 		return ret;
1725 
1726 	if (str)
1727 		buffer = parse_events__scan_string(str, scanner);
1728 	else
1729 	        parse_events_set_in(input, scanner);
1730 
1731 #ifdef PARSER_DEBUG
1732 	parse_events_debug = 1;
1733 	parse_events_set_debug(1, scanner);
1734 #endif
1735 	ret = parse_events_parse(parse_state, scanner);
1736 
1737 	if (str) {
1738 		parse_events__flush_buffer(buffer, scanner);
1739 		parse_events__delete_buffer(buffer, scanner);
1740 	}
1741 	parse_events_lex_destroy(scanner);
1742 	return ret;
1743 }
1744 
1745 /*
1746  * parse event config string, return a list of event terms.
1747  */
1748 int parse_events_terms(struct list_head *terms, const char *str, FILE *input)
1749 {
1750 	struct parse_events_state parse_state = {
1751 		.terms  = NULL,
1752 		.stoken = PE_START_TERMS,
1753 	};
1754 	int ret;
1755 
1756 	ret = parse_events__scanner(str, input, &parse_state);
1757 
1758 	if (!ret) {
1759 		list_splice(parse_state.terms, terms);
1760 		zfree(&parse_state.terms);
1761 		return 0;
1762 	}
1763 
1764 	parse_events_terms__delete(parse_state.terms);
1765 	return ret;
1766 }
1767 
1768 static int evsel__compute_group_pmu_name(struct evsel *evsel,
1769 					  const struct list_head *head)
1770 {
1771 	struct evsel *leader = evsel__leader(evsel);
1772 	struct evsel *pos;
1773 	const char *group_pmu_name;
1774 	struct perf_pmu *pmu = evsel__find_pmu(evsel);
1775 
1776 	if (!pmu) {
1777 		/*
1778 		 * For PERF_TYPE_HARDWARE and PERF_TYPE_HW_CACHE types the PMU
1779 		 * is a core PMU, but in heterogeneous systems this is
1780 		 * unknown. For now pick the first core PMU.
1781 		 */
1782 		pmu = perf_pmus__scan_core(NULL);
1783 	}
1784 	if (!pmu) {
1785 		pr_debug("No PMU found for '%s'\n", evsel__name(evsel));
1786 		return -EINVAL;
1787 	}
1788 	group_pmu_name = pmu->name;
1789 	/*
1790 	 * Software events may be in a group with other uncore PMU events. Use
1791 	 * the pmu_name of the first non-software event to avoid breaking the
1792 	 * software event out of the group.
1793 	 *
1794 	 * Aux event leaders, like intel_pt, expect a group with events from
1795 	 * other PMUs, so substitute the AUX event's PMU in this case.
1796 	 */
1797 	if (perf_pmu__is_software(pmu) || evsel__is_aux_event(leader)) {
1798 		struct perf_pmu *leader_pmu = evsel__find_pmu(leader);
1799 
1800 		if (!leader_pmu) {
1801 			/* As with determining pmu above. */
1802 			leader_pmu = perf_pmus__scan_core(NULL);
1803 		}
1804 		/*
1805 		 * Starting with the leader, find the first event with a named
1806 		 * non-software PMU. for_each_group_(member|evsel) isn't used as
1807 		 * the list isn't yet sorted putting evsel's in the same group
1808 		 * together.
1809 		 */
1810 		if (leader_pmu && !perf_pmu__is_software(leader_pmu)) {
1811 			group_pmu_name = leader_pmu->name;
1812 		} else if (leader->core.nr_members > 1) {
1813 			list_for_each_entry(pos, head, core.node) {
1814 				struct perf_pmu *pos_pmu;
1815 
1816 				if (pos == leader || evsel__leader(pos) != leader)
1817 					continue;
1818 				pos_pmu = evsel__find_pmu(pos);
1819 				if (!pos_pmu) {
1820 					/* As with determining pmu above. */
1821 					pos_pmu = perf_pmus__scan_core(NULL);
1822 				}
1823 				if (pos_pmu && !perf_pmu__is_software(pos_pmu)) {
1824 					group_pmu_name = pos_pmu->name;
1825 					break;
1826 				}
1827 			}
1828 		}
1829 	}
1830 	/* Assign the actual name taking care that the fake PMU lacks a name. */
1831 	evsel->group_pmu_name = strdup(group_pmu_name ?: "fake");
1832 	return evsel->group_pmu_name ? 0 : -ENOMEM;
1833 }
1834 
1835 __weak int arch_evlist__cmp(const struct evsel *lhs, const struct evsel *rhs)
1836 {
1837 	/* Order by insertion index. */
1838 	return lhs->core.idx - rhs->core.idx;
1839 }
1840 
1841 static int evlist__cmp(void *_fg_idx, const struct list_head *l, const struct list_head *r)
1842 {
1843 	const struct perf_evsel *lhs_core = container_of(l, struct perf_evsel, node);
1844 	const struct evsel *lhs = container_of(lhs_core, struct evsel, core);
1845 	const struct perf_evsel *rhs_core = container_of(r, struct perf_evsel, node);
1846 	const struct evsel *rhs = container_of(rhs_core, struct evsel, core);
1847 	int *force_grouped_idx = _fg_idx;
1848 	int lhs_sort_idx, rhs_sort_idx, ret;
1849 	const char *lhs_pmu_name, *rhs_pmu_name;
1850 	bool lhs_has_group, rhs_has_group;
1851 
1852 	/*
1853 	 * First sort by grouping/leader. Read the leader idx only if the evsel
1854 	 * is part of a group, by default ungrouped events will be sorted
1855 	 * relative to grouped events based on where the first ungrouped event
1856 	 * occurs. If both events don't have a group we want to fall-through to
1857 	 * the arch specific sorting, that can reorder and fix things like
1858 	 * Intel's topdown events.
1859 	 */
1860 	if (lhs_core->leader != lhs_core || lhs_core->nr_members > 1) {
1861 		lhs_has_group = true;
1862 		lhs_sort_idx = lhs_core->leader->idx;
1863 	} else {
1864 		lhs_has_group = false;
1865 		lhs_sort_idx = *force_grouped_idx != -1 && arch_evsel__must_be_in_group(lhs)
1866 			? *force_grouped_idx
1867 			: lhs_core->idx;
1868 	}
1869 	if (rhs_core->leader != rhs_core || rhs_core->nr_members > 1) {
1870 		rhs_has_group = true;
1871 		rhs_sort_idx = rhs_core->leader->idx;
1872 	} else {
1873 		rhs_has_group = false;
1874 		rhs_sort_idx = *force_grouped_idx != -1 && arch_evsel__must_be_in_group(rhs)
1875 			? *force_grouped_idx
1876 			: rhs_core->idx;
1877 	}
1878 
1879 	if (lhs_sort_idx != rhs_sort_idx)
1880 		return lhs_sort_idx - rhs_sort_idx;
1881 
1882 	/* Group by PMU if there is a group. Groups can't span PMUs. */
1883 	if (lhs_has_group && rhs_has_group) {
1884 		lhs_pmu_name = lhs->group_pmu_name;
1885 		rhs_pmu_name = rhs->group_pmu_name;
1886 		ret = strcmp(lhs_pmu_name, rhs_pmu_name);
1887 		if (ret)
1888 			return ret;
1889 	}
1890 
1891 	/* Architecture specific sorting. */
1892 	return arch_evlist__cmp(lhs, rhs);
1893 }
1894 
1895 static int parse_events__sort_events_and_fix_groups(struct list_head *list)
1896 {
1897 	int idx = 0, force_grouped_idx = -1;
1898 	struct evsel *pos, *cur_leader = NULL;
1899 	struct perf_evsel *cur_leaders_grp = NULL;
1900 	bool idx_changed = false, cur_leader_force_grouped = false;
1901 	int orig_num_leaders = 0, num_leaders = 0;
1902 	int ret;
1903 
1904 	/*
1905 	 * Compute index to insert ungrouped events at. Place them where the
1906 	 * first ungrouped event appears.
1907 	 */
1908 	list_for_each_entry(pos, list, core.node) {
1909 		const struct evsel *pos_leader = evsel__leader(pos);
1910 
1911 		ret = evsel__compute_group_pmu_name(pos, list);
1912 		if (ret)
1913 			return ret;
1914 
1915 		if (pos == pos_leader)
1916 			orig_num_leaders++;
1917 
1918 		/*
1919 		 * Ensure indexes are sequential, in particular for multiple
1920 		 * event lists being merged. The indexes are used to detect when
1921 		 * the user order is modified.
1922 		 */
1923 		pos->core.idx = idx++;
1924 
1925 		/* Remember an index to sort all forced grouped events together to. */
1926 		if (force_grouped_idx == -1 && pos == pos_leader && pos->core.nr_members < 2 &&
1927 		    arch_evsel__must_be_in_group(pos))
1928 			force_grouped_idx = pos->core.idx;
1929 	}
1930 
1931 	/* Sort events. */
1932 	list_sort(&force_grouped_idx, list, evlist__cmp);
1933 
1934 	/*
1935 	 * Recompute groups, splitting for PMUs and adding groups for events
1936 	 * that require them.
1937 	 */
1938 	idx = 0;
1939 	list_for_each_entry(pos, list, core.node) {
1940 		const struct evsel *pos_leader = evsel__leader(pos);
1941 		const char *pos_pmu_name = pos->group_pmu_name;
1942 		const char *cur_leader_pmu_name;
1943 		bool pos_force_grouped = force_grouped_idx != -1 &&
1944 			arch_evsel__must_be_in_group(pos);
1945 
1946 		/* Reset index and nr_members. */
1947 		if (pos->core.idx != idx)
1948 			idx_changed = true;
1949 		pos->core.idx = idx++;
1950 		pos->core.nr_members = 0;
1951 
1952 		/*
1953 		 * Set the group leader respecting the given groupings and that
1954 		 * groups can't span PMUs.
1955 		 */
1956 		if (!cur_leader)
1957 			cur_leader = pos;
1958 
1959 		cur_leader_pmu_name = cur_leader->group_pmu_name;
1960 		if ((cur_leaders_grp != pos->core.leader &&
1961 		     (!pos_force_grouped || !cur_leader_force_grouped)) ||
1962 		    strcmp(cur_leader_pmu_name, pos_pmu_name)) {
1963 			/* Event is for a different group/PMU than last. */
1964 			cur_leader = pos;
1965 			/*
1966 			 * Remember the leader's group before it is overwritten,
1967 			 * so that later events match as being in the same
1968 			 * group.
1969 			 */
1970 			cur_leaders_grp = pos->core.leader;
1971 			/*
1972 			 * Avoid forcing events into groups with events that
1973 			 * don't need to be in the group.
1974 			 */
1975 			cur_leader_force_grouped = pos_force_grouped;
1976 		}
1977 		if (pos_leader != cur_leader) {
1978 			/* The leader changed so update it. */
1979 			evsel__set_leader(pos, cur_leader);
1980 		}
1981 	}
1982 	list_for_each_entry(pos, list, core.node) {
1983 		struct evsel *pos_leader = evsel__leader(pos);
1984 
1985 		if (pos == pos_leader)
1986 			num_leaders++;
1987 		pos_leader->core.nr_members++;
1988 	}
1989 	return (idx_changed || num_leaders != orig_num_leaders) ? 1 : 0;
1990 }
1991 
1992 int __parse_events(struct evlist *evlist, const char *str, const char *pmu_filter,
1993 		   struct parse_events_error *err, struct perf_pmu *fake_pmu,
1994 		   bool warn_if_reordered)
1995 {
1996 	struct parse_events_state parse_state = {
1997 		.list	  = LIST_HEAD_INIT(parse_state.list),
1998 		.idx	  = evlist->core.nr_entries,
1999 		.error	  = err,
2000 		.stoken	  = PE_START_EVENTS,
2001 		.fake_pmu = fake_pmu,
2002 		.pmu_filter = pmu_filter,
2003 		.match_legacy_cache_terms = true,
2004 	};
2005 	int ret, ret2;
2006 
2007 	ret = parse_events__scanner(str, /*input=*/ NULL, &parse_state);
2008 
2009 	if (!ret && list_empty(&parse_state.list)) {
2010 		WARN_ONCE(true, "WARNING: event parser found nothing\n");
2011 		return -1;
2012 	}
2013 
2014 	ret2 = parse_events__sort_events_and_fix_groups(&parse_state.list);
2015 	if (ret2 < 0)
2016 		return ret;
2017 
2018 	if (ret2 && warn_if_reordered && !parse_state.wild_card_pmus)
2019 		pr_warning("WARNING: events were regrouped to match PMUs\n");
2020 
2021 	/*
2022 	 * Add list to the evlist even with errors to allow callers to clean up.
2023 	 */
2024 	evlist__splice_list_tail(evlist, &parse_state.list);
2025 
2026 	if (!ret) {
2027 		struct evsel *last;
2028 
2029 		last = evlist__last(evlist);
2030 		last->cmdline_group_boundary = true;
2031 
2032 		return 0;
2033 	}
2034 
2035 	/*
2036 	 * There are 2 users - builtin-record and builtin-test objects.
2037 	 * Both call evlist__delete in case of error, so we dont
2038 	 * need to bother.
2039 	 */
2040 	return ret;
2041 }
2042 
2043 int parse_event(struct evlist *evlist, const char *str)
2044 {
2045 	struct parse_events_error err;
2046 	int ret;
2047 
2048 	parse_events_error__init(&err);
2049 	ret = parse_events(evlist, str, &err);
2050 	parse_events_error__exit(&err);
2051 	return ret;
2052 }
2053 
2054 void parse_events_error__init(struct parse_events_error *err)
2055 {
2056 	bzero(err, sizeof(*err));
2057 }
2058 
2059 void parse_events_error__exit(struct parse_events_error *err)
2060 {
2061 	zfree(&err->str);
2062 	zfree(&err->help);
2063 	zfree(&err->first_str);
2064 	zfree(&err->first_help);
2065 }
2066 
2067 void parse_events_error__handle(struct parse_events_error *err, int idx,
2068 				char *str, char *help)
2069 {
2070 	if (WARN(!str || !err, "WARNING: failed to provide error string or struct\n"))
2071 		goto out_free;
2072 	switch (err->num_errors) {
2073 	case 0:
2074 		err->idx = idx;
2075 		err->str = str;
2076 		err->help = help;
2077 		break;
2078 	case 1:
2079 		err->first_idx = err->idx;
2080 		err->idx = idx;
2081 		err->first_str = err->str;
2082 		err->str = str;
2083 		err->first_help = err->help;
2084 		err->help = help;
2085 		break;
2086 	default:
2087 		pr_debug("Multiple errors dropping message: %s (%s)\n",
2088 			err->str, err->help);
2089 		free(err->str);
2090 		err->str = str;
2091 		free(err->help);
2092 		err->help = help;
2093 		break;
2094 	}
2095 	err->num_errors++;
2096 	return;
2097 
2098 out_free:
2099 	free(str);
2100 	free(help);
2101 }
2102 
2103 #define MAX_WIDTH 1000
2104 static int get_term_width(void)
2105 {
2106 	struct winsize ws;
2107 
2108 	get_term_dimensions(&ws);
2109 	return ws.ws_col > MAX_WIDTH ? MAX_WIDTH : ws.ws_col;
2110 }
2111 
2112 static void __parse_events_error__print(int err_idx, const char *err_str,
2113 					const char *err_help, const char *event)
2114 {
2115 	const char *str = "invalid or unsupported event: ";
2116 	char _buf[MAX_WIDTH];
2117 	char *buf = (char *) event;
2118 	int idx = 0;
2119 	if (err_str) {
2120 		/* -2 for extra '' in the final fprintf */
2121 		int width       = get_term_width() - 2;
2122 		int len_event   = strlen(event);
2123 		int len_str, max_len, cut = 0;
2124 
2125 		/*
2126 		 * Maximum error index indent, we will cut
2127 		 * the event string if it's bigger.
2128 		 */
2129 		int max_err_idx = 13;
2130 
2131 		/*
2132 		 * Let's be specific with the message when
2133 		 * we have the precise error.
2134 		 */
2135 		str     = "event syntax error: ";
2136 		len_str = strlen(str);
2137 		max_len = width - len_str;
2138 
2139 		buf = _buf;
2140 
2141 		/* We're cutting from the beginning. */
2142 		if (err_idx > max_err_idx)
2143 			cut = err_idx - max_err_idx;
2144 
2145 		strncpy(buf, event + cut, max_len);
2146 
2147 		/* Mark cut parts with '..' on both sides. */
2148 		if (cut)
2149 			buf[0] = buf[1] = '.';
2150 
2151 		if ((len_event - cut) > max_len) {
2152 			buf[max_len - 1] = buf[max_len - 2] = '.';
2153 			buf[max_len] = 0;
2154 		}
2155 
2156 		idx = len_str + err_idx - cut;
2157 	}
2158 
2159 	fprintf(stderr, "%s'%s'\n", str, buf);
2160 	if (idx) {
2161 		fprintf(stderr, "%*s\\___ %s\n", idx + 1, "", err_str);
2162 		if (err_help)
2163 			fprintf(stderr, "\n%s\n", err_help);
2164 	}
2165 }
2166 
2167 void parse_events_error__print(struct parse_events_error *err,
2168 			       const char *event)
2169 {
2170 	if (!err->num_errors)
2171 		return;
2172 
2173 	__parse_events_error__print(err->idx, err->str, err->help, event);
2174 
2175 	if (err->num_errors > 1) {
2176 		fputs("\nInitial error:\n", stderr);
2177 		__parse_events_error__print(err->first_idx, err->first_str,
2178 					err->first_help, event);
2179 	}
2180 }
2181 
2182 #undef MAX_WIDTH
2183 
2184 int parse_events_option(const struct option *opt, const char *str,
2185 			int unset __maybe_unused)
2186 {
2187 	struct parse_events_option_args *args = opt->value;
2188 	struct parse_events_error err;
2189 	int ret;
2190 
2191 	parse_events_error__init(&err);
2192 	ret = __parse_events(*args->evlistp, str, args->pmu_filter, &err,
2193 			     /*fake_pmu=*/NULL, /*warn_if_reordered=*/true);
2194 
2195 	if (ret) {
2196 		parse_events_error__print(&err, str);
2197 		fprintf(stderr, "Run 'perf list' for a list of valid events\n");
2198 	}
2199 	parse_events_error__exit(&err);
2200 
2201 	return ret;
2202 }
2203 
2204 int parse_events_option_new_evlist(const struct option *opt, const char *str, int unset)
2205 {
2206 	struct parse_events_option_args *args = opt->value;
2207 	int ret;
2208 
2209 	if (*args->evlistp == NULL) {
2210 		*args->evlistp = evlist__new();
2211 
2212 		if (*args->evlistp == NULL) {
2213 			fprintf(stderr, "Not enough memory to create evlist\n");
2214 			return -1;
2215 		}
2216 	}
2217 	ret = parse_events_option(opt, str, unset);
2218 	if (ret) {
2219 		evlist__delete(*args->evlistp);
2220 		*args->evlistp = NULL;
2221 	}
2222 
2223 	return ret;
2224 }
2225 
2226 static int
2227 foreach_evsel_in_last_glob(struct evlist *evlist,
2228 			   int (*func)(struct evsel *evsel,
2229 				       const void *arg),
2230 			   const void *arg)
2231 {
2232 	struct evsel *last = NULL;
2233 	int err;
2234 
2235 	/*
2236 	 * Don't return when list_empty, give func a chance to report
2237 	 * error when it found last == NULL.
2238 	 *
2239 	 * So no need to WARN here, let *func do this.
2240 	 */
2241 	if (evlist->core.nr_entries > 0)
2242 		last = evlist__last(evlist);
2243 
2244 	do {
2245 		err = (*func)(last, arg);
2246 		if (err)
2247 			return -1;
2248 		if (!last)
2249 			return 0;
2250 
2251 		if (last->core.node.prev == &evlist->core.entries)
2252 			return 0;
2253 		last = list_entry(last->core.node.prev, struct evsel, core.node);
2254 	} while (!last->cmdline_group_boundary);
2255 
2256 	return 0;
2257 }
2258 
2259 static int set_filter(struct evsel *evsel, const void *arg)
2260 {
2261 	const char *str = arg;
2262 	bool found = false;
2263 	int nr_addr_filters = 0;
2264 	struct perf_pmu *pmu = NULL;
2265 
2266 	if (evsel == NULL) {
2267 		fprintf(stderr,
2268 			"--filter option should follow a -e tracepoint or HW tracer option\n");
2269 		return -1;
2270 	}
2271 
2272 	if (evsel->core.attr.type == PERF_TYPE_TRACEPOINT) {
2273 		if (evsel__append_tp_filter(evsel, str) < 0) {
2274 			fprintf(stderr,
2275 				"not enough memory to hold filter string\n");
2276 			return -1;
2277 		}
2278 
2279 		return 0;
2280 	}
2281 
2282 	while ((pmu = perf_pmus__scan(pmu)) != NULL)
2283 		if (pmu->type == evsel->core.attr.type) {
2284 			found = true;
2285 			break;
2286 		}
2287 
2288 	if (found)
2289 		perf_pmu__scan_file(pmu, "nr_addr_filters",
2290 				    "%d", &nr_addr_filters);
2291 
2292 	if (!nr_addr_filters)
2293 		return perf_bpf_filter__parse(&evsel->bpf_filters, str);
2294 
2295 	if (evsel__append_addr_filter(evsel, str) < 0) {
2296 		fprintf(stderr,
2297 			"not enough memory to hold filter string\n");
2298 		return -1;
2299 	}
2300 
2301 	return 0;
2302 }
2303 
2304 int parse_filter(const struct option *opt, const char *str,
2305 		 int unset __maybe_unused)
2306 {
2307 	struct evlist *evlist = *(struct evlist **)opt->value;
2308 
2309 	return foreach_evsel_in_last_glob(evlist, set_filter,
2310 					  (const void *)str);
2311 }
2312 
2313 static int add_exclude_perf_filter(struct evsel *evsel,
2314 				   const void *arg __maybe_unused)
2315 {
2316 	char new_filter[64];
2317 
2318 	if (evsel == NULL || evsel->core.attr.type != PERF_TYPE_TRACEPOINT) {
2319 		fprintf(stderr,
2320 			"--exclude-perf option should follow a -e tracepoint option\n");
2321 		return -1;
2322 	}
2323 
2324 	snprintf(new_filter, sizeof(new_filter), "common_pid != %d", getpid());
2325 
2326 	if (evsel__append_tp_filter(evsel, new_filter) < 0) {
2327 		fprintf(stderr,
2328 			"not enough memory to hold filter string\n");
2329 		return -1;
2330 	}
2331 
2332 	return 0;
2333 }
2334 
2335 int exclude_perf(const struct option *opt,
2336 		 const char *arg __maybe_unused,
2337 		 int unset __maybe_unused)
2338 {
2339 	struct evlist *evlist = *(struct evlist **)opt->value;
2340 
2341 	return foreach_evsel_in_last_glob(evlist, add_exclude_perf_filter,
2342 					  NULL);
2343 }
2344 
2345 int parse_events__is_hardcoded_term(struct parse_events_term *term)
2346 {
2347 	return term->type_term != PARSE_EVENTS__TERM_TYPE_USER;
2348 }
2349 
2350 static int new_term(struct parse_events_term **_term,
2351 		    struct parse_events_term *temp,
2352 		    char *str, u64 num)
2353 {
2354 	struct parse_events_term *term;
2355 
2356 	term = malloc(sizeof(*term));
2357 	if (!term)
2358 		return -ENOMEM;
2359 
2360 	*term = *temp;
2361 	INIT_LIST_HEAD(&term->list);
2362 	term->weak = false;
2363 
2364 	switch (term->type_val) {
2365 	case PARSE_EVENTS__TERM_TYPE_NUM:
2366 		term->val.num = num;
2367 		break;
2368 	case PARSE_EVENTS__TERM_TYPE_STR:
2369 		term->val.str = str;
2370 		break;
2371 	default:
2372 		free(term);
2373 		return -EINVAL;
2374 	}
2375 
2376 	*_term = term;
2377 	return 0;
2378 }
2379 
2380 int parse_events_term__num(struct parse_events_term **term,
2381 			   int type_term, const char *config, u64 num,
2382 			   bool no_value,
2383 			   void *loc_term_, void *loc_val_)
2384 {
2385 	YYLTYPE *loc_term = loc_term_;
2386 	YYLTYPE *loc_val = loc_val_;
2387 
2388 	struct parse_events_term temp = {
2389 		.type_val  = PARSE_EVENTS__TERM_TYPE_NUM,
2390 		.type_term = type_term,
2391 		.config    = config ? : strdup(config_term_names[type_term]),
2392 		.no_value  = no_value,
2393 		.err_term  = loc_term ? loc_term->first_column : 0,
2394 		.err_val   = loc_val  ? loc_val->first_column  : 0,
2395 	};
2396 
2397 	return new_term(term, &temp, NULL, num);
2398 }
2399 
2400 int parse_events_term__str(struct parse_events_term **term,
2401 			   int type_term, char *config, char *str,
2402 			   void *loc_term_, void *loc_val_)
2403 {
2404 	YYLTYPE *loc_term = loc_term_;
2405 	YYLTYPE *loc_val = loc_val_;
2406 
2407 	struct parse_events_term temp = {
2408 		.type_val  = PARSE_EVENTS__TERM_TYPE_STR,
2409 		.type_term = type_term,
2410 		.config    = config,
2411 		.err_term  = loc_term ? loc_term->first_column : 0,
2412 		.err_val   = loc_val  ? loc_val->first_column  : 0,
2413 	};
2414 
2415 	return new_term(term, &temp, str, 0);
2416 }
2417 
2418 int parse_events_term__term(struct parse_events_term **term,
2419 			    int term_lhs, int term_rhs,
2420 			    void *loc_term, void *loc_val)
2421 {
2422 	return parse_events_term__str(term, term_lhs, NULL,
2423 				      strdup(config_term_names[term_rhs]),
2424 				      loc_term, loc_val);
2425 }
2426 
2427 int parse_events_term__clone(struct parse_events_term **new,
2428 			     struct parse_events_term *term)
2429 {
2430 	char *str;
2431 	struct parse_events_term temp = {
2432 		.type_val  = term->type_val,
2433 		.type_term = term->type_term,
2434 		.config    = NULL,
2435 		.err_term  = term->err_term,
2436 		.err_val   = term->err_val,
2437 	};
2438 
2439 	if (term->config) {
2440 		temp.config = strdup(term->config);
2441 		if (!temp.config)
2442 			return -ENOMEM;
2443 	}
2444 	if (term->type_val == PARSE_EVENTS__TERM_TYPE_NUM)
2445 		return new_term(new, &temp, NULL, term->val.num);
2446 
2447 	str = strdup(term->val.str);
2448 	if (!str)
2449 		return -ENOMEM;
2450 	return new_term(new, &temp, str, 0);
2451 }
2452 
2453 void parse_events_term__delete(struct parse_events_term *term)
2454 {
2455 	if (term->type_val != PARSE_EVENTS__TERM_TYPE_NUM)
2456 		zfree(&term->val.str);
2457 
2458 	zfree(&term->config);
2459 	free(term);
2460 }
2461 
2462 int parse_events_copy_term_list(struct list_head *old,
2463 				 struct list_head **new)
2464 {
2465 	struct parse_events_term *term, *n;
2466 	int ret;
2467 
2468 	if (!old) {
2469 		*new = NULL;
2470 		return 0;
2471 	}
2472 
2473 	*new = malloc(sizeof(struct list_head));
2474 	if (!*new)
2475 		return -ENOMEM;
2476 	INIT_LIST_HEAD(*new);
2477 
2478 	list_for_each_entry (term, old, list) {
2479 		ret = parse_events_term__clone(&n, term);
2480 		if (ret)
2481 			return ret;
2482 		list_add_tail(&n->list, *new);
2483 	}
2484 	return 0;
2485 }
2486 
2487 void parse_events_terms__purge(struct list_head *terms)
2488 {
2489 	struct parse_events_term *term, *h;
2490 
2491 	list_for_each_entry_safe(term, h, terms, list) {
2492 		list_del_init(&term->list);
2493 		parse_events_term__delete(term);
2494 	}
2495 }
2496 
2497 void parse_events_terms__delete(struct list_head *terms)
2498 {
2499 	if (!terms)
2500 		return;
2501 	parse_events_terms__purge(terms);
2502 	free(terms);
2503 }
2504 
2505 void parse_events_evlist_error(struct parse_events_state *parse_state,
2506 			       int idx, const char *str)
2507 {
2508 	if (!parse_state->error)
2509 		return;
2510 
2511 	parse_events_error__handle(parse_state->error, idx, strdup(str), NULL);
2512 }
2513 
2514 static void config_terms_list(char *buf, size_t buf_sz)
2515 {
2516 	int i;
2517 	bool first = true;
2518 
2519 	buf[0] = '\0';
2520 	for (i = 0; i < __PARSE_EVENTS__TERM_TYPE_NR; i++) {
2521 		const char *name = config_term_names[i];
2522 
2523 		if (!config_term_avail(i, NULL))
2524 			continue;
2525 		if (!name)
2526 			continue;
2527 		if (name[0] == '<')
2528 			continue;
2529 
2530 		if (strlen(buf) + strlen(name) + 2 >= buf_sz)
2531 			return;
2532 
2533 		if (!first)
2534 			strcat(buf, ",");
2535 		else
2536 			first = false;
2537 		strcat(buf, name);
2538 	}
2539 }
2540 
2541 /*
2542  * Return string contains valid config terms of an event.
2543  * @additional_terms: For terms such as PMU sysfs terms.
2544  */
2545 char *parse_events_formats_error_string(char *additional_terms)
2546 {
2547 	char *str;
2548 	/* "no-overwrite" is the longest name */
2549 	char static_terms[__PARSE_EVENTS__TERM_TYPE_NR *
2550 			  (sizeof("no-overwrite") - 1)];
2551 
2552 	config_terms_list(static_terms, sizeof(static_terms));
2553 	/* valid terms */
2554 	if (additional_terms) {
2555 		if (asprintf(&str, "valid terms: %s,%s",
2556 			     additional_terms, static_terms) < 0)
2557 			goto fail;
2558 	} else {
2559 		if (asprintf(&str, "valid terms: %s", static_terms) < 0)
2560 			goto fail;
2561 	}
2562 	return str;
2563 
2564 fail:
2565 	return NULL;
2566 }
2567