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