xref: /openbmc/linux/tools/perf/util/parse-events.c (revision cb325ddd)
1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/hw_breakpoint.h>
3 #include <linux/err.h>
4 #include <linux/zalloc.h>
5 #include <dirent.h>
6 #include <errno.h>
7 #include <sys/ioctl.h>
8 #include <sys/types.h>
9 #include <sys/stat.h>
10 #include <fcntl.h>
11 #include <sys/param.h>
12 #include "term.h"
13 #include "build-id.h"
14 #include "evlist.h"
15 #include "evsel.h"
16 #include <subcmd/pager.h>
17 #include <subcmd/parse-options.h>
18 #include "parse-events.h"
19 #include <subcmd/exec-cmd.h>
20 #include "string2.h"
21 #include "strlist.h"
22 #include "bpf-loader.h"
23 #include "debug.h"
24 #include <api/fs/tracing_path.h>
25 #include <perf/cpumap.h>
26 #include "parse-events-bison.h"
27 #define YY_EXTRA_TYPE void*
28 #include "parse-events-flex.h"
29 #include "pmu.h"
30 #include "thread_map.h"
31 #include "probe-file.h"
32 #include "asm/bug.h"
33 #include "util/parse-branch-options.h"
34 #include "metricgroup.h"
35 #include "util/evsel_config.h"
36 #include "util/event.h"
37 #include "util/pfm.h"
38 #include "util/parse-events-hybrid.h"
39 #include "util/pmu-hybrid.h"
40 #include "perf.h"
41 
42 #define MAX_NAME_LEN 100
43 
44 #ifdef PARSER_DEBUG
45 extern int parse_events_debug;
46 #endif
47 int parse_events_parse(void *parse_state, void *scanner);
48 static int get_config_terms(struct list_head *head_config,
49 			    struct list_head *head_terms __maybe_unused);
50 static int parse_events__with_hybrid_pmu(struct parse_events_state *parse_state,
51 					 const char *str, char *pmu_name,
52 					 struct list_head *list);
53 
54 static struct perf_pmu_event_symbol *perf_pmu_events_list;
55 /*
56  * The variable indicates the number of supported pmu event symbols.
57  * 0 means not initialized and ready to init
58  * -1 means failed to init, don't try anymore
59  * >0 is the number of supported pmu event symbols
60  */
61 static int perf_pmu_events_list_num;
62 
63 struct event_symbol event_symbols_hw[PERF_COUNT_HW_MAX] = {
64 	[PERF_COUNT_HW_CPU_CYCLES] = {
65 		.symbol = "cpu-cycles",
66 		.alias  = "cycles",
67 	},
68 	[PERF_COUNT_HW_INSTRUCTIONS] = {
69 		.symbol = "instructions",
70 		.alias  = "",
71 	},
72 	[PERF_COUNT_HW_CACHE_REFERENCES] = {
73 		.symbol = "cache-references",
74 		.alias  = "",
75 	},
76 	[PERF_COUNT_HW_CACHE_MISSES] = {
77 		.symbol = "cache-misses",
78 		.alias  = "",
79 	},
80 	[PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = {
81 		.symbol = "branch-instructions",
82 		.alias  = "branches",
83 	},
84 	[PERF_COUNT_HW_BRANCH_MISSES] = {
85 		.symbol = "branch-misses",
86 		.alias  = "",
87 	},
88 	[PERF_COUNT_HW_BUS_CYCLES] = {
89 		.symbol = "bus-cycles",
90 		.alias  = "",
91 	},
92 	[PERF_COUNT_HW_STALLED_CYCLES_FRONTEND] = {
93 		.symbol = "stalled-cycles-frontend",
94 		.alias  = "idle-cycles-frontend",
95 	},
96 	[PERF_COUNT_HW_STALLED_CYCLES_BACKEND] = {
97 		.symbol = "stalled-cycles-backend",
98 		.alias  = "idle-cycles-backend",
99 	},
100 	[PERF_COUNT_HW_REF_CPU_CYCLES] = {
101 		.symbol = "ref-cycles",
102 		.alias  = "",
103 	},
104 };
105 
106 struct event_symbol event_symbols_sw[PERF_COUNT_SW_MAX] = {
107 	[PERF_COUNT_SW_CPU_CLOCK] = {
108 		.symbol = "cpu-clock",
109 		.alias  = "",
110 	},
111 	[PERF_COUNT_SW_TASK_CLOCK] = {
112 		.symbol = "task-clock",
113 		.alias  = "",
114 	},
115 	[PERF_COUNT_SW_PAGE_FAULTS] = {
116 		.symbol = "page-faults",
117 		.alias  = "faults",
118 	},
119 	[PERF_COUNT_SW_CONTEXT_SWITCHES] = {
120 		.symbol = "context-switches",
121 		.alias  = "cs",
122 	},
123 	[PERF_COUNT_SW_CPU_MIGRATIONS] = {
124 		.symbol = "cpu-migrations",
125 		.alias  = "migrations",
126 	},
127 	[PERF_COUNT_SW_PAGE_FAULTS_MIN] = {
128 		.symbol = "minor-faults",
129 		.alias  = "",
130 	},
131 	[PERF_COUNT_SW_PAGE_FAULTS_MAJ] = {
132 		.symbol = "major-faults",
133 		.alias  = "",
134 	},
135 	[PERF_COUNT_SW_ALIGNMENT_FAULTS] = {
136 		.symbol = "alignment-faults",
137 		.alias  = "",
138 	},
139 	[PERF_COUNT_SW_EMULATION_FAULTS] = {
140 		.symbol = "emulation-faults",
141 		.alias  = "",
142 	},
143 	[PERF_COUNT_SW_DUMMY] = {
144 		.symbol = "dummy",
145 		.alias  = "",
146 	},
147 	[PERF_COUNT_SW_BPF_OUTPUT] = {
148 		.symbol = "bpf-output",
149 		.alias  = "",
150 	},
151 	[PERF_COUNT_SW_CGROUP_SWITCHES] = {
152 		.symbol = "cgroup-switches",
153 		.alias  = "",
154 	},
155 };
156 
157 #define __PERF_EVENT_FIELD(config, name) \
158 	((config & PERF_EVENT_##name##_MASK) >> PERF_EVENT_##name##_SHIFT)
159 
160 #define PERF_EVENT_RAW(config)		__PERF_EVENT_FIELD(config, RAW)
161 #define PERF_EVENT_CONFIG(config)	__PERF_EVENT_FIELD(config, CONFIG)
162 #define PERF_EVENT_TYPE(config)		__PERF_EVENT_FIELD(config, TYPE)
163 #define PERF_EVENT_ID(config)		__PERF_EVENT_FIELD(config, EVENT)
164 
165 #define for_each_subsystem(sys_dir, sys_dirent)			\
166 	while ((sys_dirent = readdir(sys_dir)) != NULL)		\
167 		if (sys_dirent->d_type == DT_DIR &&		\
168 		    (strcmp(sys_dirent->d_name, ".")) &&	\
169 		    (strcmp(sys_dirent->d_name, "..")))
170 
171 static int tp_event_has_id(const char *dir_path, struct dirent *evt_dir)
172 {
173 	char evt_path[MAXPATHLEN];
174 	int fd;
175 
176 	snprintf(evt_path, MAXPATHLEN, "%s/%s/id", dir_path, evt_dir->d_name);
177 	fd = open(evt_path, O_RDONLY);
178 	if (fd < 0)
179 		return -EINVAL;
180 	close(fd);
181 
182 	return 0;
183 }
184 
185 #define for_each_event(dir_path, evt_dir, evt_dirent)		\
186 	while ((evt_dirent = readdir(evt_dir)) != NULL)		\
187 		if (evt_dirent->d_type == DT_DIR &&		\
188 		    (strcmp(evt_dirent->d_name, ".")) &&	\
189 		    (strcmp(evt_dirent->d_name, "..")) &&	\
190 		    (!tp_event_has_id(dir_path, evt_dirent)))
191 
192 #define MAX_EVENT_LENGTH 512
193 
194 struct tracepoint_path *tracepoint_id_to_path(u64 config)
195 {
196 	struct tracepoint_path *path = NULL;
197 	DIR *sys_dir, *evt_dir;
198 	struct dirent *sys_dirent, *evt_dirent;
199 	char id_buf[24];
200 	int fd;
201 	u64 id;
202 	char evt_path[MAXPATHLEN];
203 	char *dir_path;
204 
205 	sys_dir = tracing_events__opendir();
206 	if (!sys_dir)
207 		return NULL;
208 
209 	for_each_subsystem(sys_dir, sys_dirent) {
210 		dir_path = get_events_file(sys_dirent->d_name);
211 		if (!dir_path)
212 			continue;
213 		evt_dir = opendir(dir_path);
214 		if (!evt_dir)
215 			goto next;
216 
217 		for_each_event(dir_path, evt_dir, evt_dirent) {
218 
219 			scnprintf(evt_path, MAXPATHLEN, "%s/%s/id", dir_path,
220 				  evt_dirent->d_name);
221 			fd = open(evt_path, O_RDONLY);
222 			if (fd < 0)
223 				continue;
224 			if (read(fd, id_buf, sizeof(id_buf)) < 0) {
225 				close(fd);
226 				continue;
227 			}
228 			close(fd);
229 			id = atoll(id_buf);
230 			if (id == config) {
231 				put_events_file(dir_path);
232 				closedir(evt_dir);
233 				closedir(sys_dir);
234 				path = zalloc(sizeof(*path));
235 				if (!path)
236 					return NULL;
237 				if (asprintf(&path->system, "%.*s", MAX_EVENT_LENGTH, sys_dirent->d_name) < 0) {
238 					free(path);
239 					return NULL;
240 				}
241 				if (asprintf(&path->name, "%.*s", MAX_EVENT_LENGTH, evt_dirent->d_name) < 0) {
242 					zfree(&path->system);
243 					free(path);
244 					return NULL;
245 				}
246 				return path;
247 			}
248 		}
249 		closedir(evt_dir);
250 next:
251 		put_events_file(dir_path);
252 	}
253 
254 	closedir(sys_dir);
255 	return NULL;
256 }
257 
258 struct tracepoint_path *tracepoint_name_to_path(const char *name)
259 {
260 	struct tracepoint_path *path = zalloc(sizeof(*path));
261 	char *str = strchr(name, ':');
262 
263 	if (path == NULL || str == NULL) {
264 		free(path);
265 		return NULL;
266 	}
267 
268 	path->system = strndup(name, str - name);
269 	path->name = strdup(str+1);
270 
271 	if (path->system == NULL || path->name == NULL) {
272 		zfree(&path->system);
273 		zfree(&path->name);
274 		zfree(&path);
275 	}
276 
277 	return path;
278 }
279 
280 const char *event_type(int type)
281 {
282 	switch (type) {
283 	case PERF_TYPE_HARDWARE:
284 		return "hardware";
285 
286 	case PERF_TYPE_SOFTWARE:
287 		return "software";
288 
289 	case PERF_TYPE_TRACEPOINT:
290 		return "tracepoint";
291 
292 	case PERF_TYPE_HW_CACHE:
293 		return "hardware-cache";
294 
295 	default:
296 		break;
297 	}
298 
299 	return "unknown";
300 }
301 
302 static char *get_config_str(struct list_head *head_terms, int type_term)
303 {
304 	struct parse_events_term *term;
305 
306 	if (!head_terms)
307 		return NULL;
308 
309 	list_for_each_entry(term, head_terms, list)
310 		if (term->type_term == type_term)
311 			return term->val.str;
312 
313 	return NULL;
314 }
315 
316 static char *get_config_metric_id(struct list_head *head_terms)
317 {
318 	return get_config_str(head_terms, PARSE_EVENTS__TERM_TYPE_METRIC_ID);
319 }
320 
321 static char *get_config_name(struct list_head *head_terms)
322 {
323 	return get_config_str(head_terms, PARSE_EVENTS__TERM_TYPE_NAME);
324 }
325 
326 static struct evsel *
327 __add_event(struct list_head *list, int *idx,
328 	    struct perf_event_attr *attr,
329 	    bool init_attr,
330 	    const char *name, const char *metric_id, struct perf_pmu *pmu,
331 	    struct list_head *config_terms, bool auto_merge_stats,
332 	    const char *cpu_list)
333 {
334 	struct evsel *evsel;
335 	struct perf_cpu_map *cpus = pmu ? perf_cpu_map__get(pmu->cpus) :
336 			       cpu_list ? perf_cpu_map__new(cpu_list) : NULL;
337 
338 	if (pmu && attr->type == PERF_TYPE_RAW)
339 		perf_pmu__warn_invalid_config(pmu, attr->config, name);
340 
341 	if (init_attr)
342 		event_attr_init(attr);
343 
344 	evsel = evsel__new_idx(attr, *idx);
345 	if (!evsel) {
346 		perf_cpu_map__put(cpus);
347 		return NULL;
348 	}
349 
350 	(*idx)++;
351 	evsel->core.cpus = cpus;
352 	evsel->core.own_cpus = perf_cpu_map__get(cpus);
353 	evsel->core.system_wide = pmu ? pmu->is_uncore : false;
354 	evsel->auto_merge_stats = auto_merge_stats;
355 
356 	if (name)
357 		evsel->name = strdup(name);
358 
359 	if (metric_id)
360 		evsel->metric_id = strdup(metric_id);
361 
362 	if (config_terms)
363 		list_splice_init(config_terms, &evsel->config_terms);
364 
365 	if (list)
366 		list_add_tail(&evsel->core.node, list);
367 
368 	return evsel;
369 }
370 
371 struct evsel *parse_events__add_event(int idx, struct perf_event_attr *attr,
372 				      const char *name, const char *metric_id,
373 				      struct perf_pmu *pmu)
374 {
375 	return __add_event(/*list=*/NULL, &idx, attr, /*init_attr=*/false, name,
376 			   metric_id, pmu, /*config_terms=*/NULL,
377 			   /*auto_merge_stats=*/false, /*cpu_list=*/NULL);
378 }
379 
380 static int add_event(struct list_head *list, int *idx,
381 		     struct perf_event_attr *attr, const char *name,
382 		     const char *metric_id, struct list_head *config_terms)
383 {
384 	return __add_event(list, idx, attr, /*init_attr*/true, name, metric_id,
385 			   /*pmu=*/NULL, config_terms,
386 			   /*auto_merge_stats=*/false, /*cpu_list=*/NULL) ? 0 : -ENOMEM;
387 }
388 
389 static int add_event_tool(struct list_head *list, int *idx,
390 			  enum perf_tool_event tool_event)
391 {
392 	struct evsel *evsel;
393 	struct perf_event_attr attr = {
394 		.type = PERF_TYPE_SOFTWARE,
395 		.config = PERF_COUNT_SW_DUMMY,
396 	};
397 
398 	evsel = __add_event(list, idx, &attr, /*init_attr=*/true, /*name=*/NULL,
399 			    /*metric_id=*/NULL, /*pmu=*/NULL,
400 			    /*config_terms=*/NULL, /*auto_merge_stats=*/false,
401 			    /*cpu_list=*/"0");
402 	if (!evsel)
403 		return -ENOMEM;
404 	evsel->tool_event = tool_event;
405 	if (tool_event == PERF_TOOL_DURATION_TIME) {
406 		free((char *)evsel->unit);
407 		evsel->unit = strdup("ns");
408 	}
409 	return 0;
410 }
411 
412 static int parse_aliases(char *str, const char *names[][EVSEL__MAX_ALIASES], int size)
413 {
414 	int i, j;
415 	int n, longest = -1;
416 
417 	for (i = 0; i < size; i++) {
418 		for (j = 0; j < EVSEL__MAX_ALIASES && names[i][j]; j++) {
419 			n = strlen(names[i][j]);
420 			if (n > longest && !strncasecmp(str, names[i][j], n))
421 				longest = n;
422 		}
423 		if (longest > 0)
424 			return i;
425 	}
426 
427 	return -1;
428 }
429 
430 typedef int config_term_func_t(struct perf_event_attr *attr,
431 			       struct parse_events_term *term,
432 			       struct parse_events_error *err);
433 static int config_term_common(struct perf_event_attr *attr,
434 			      struct parse_events_term *term,
435 			      struct parse_events_error *err);
436 static int config_attr(struct perf_event_attr *attr,
437 		       struct list_head *head,
438 		       struct parse_events_error *err,
439 		       config_term_func_t config_term);
440 
441 int parse_events_add_cache(struct list_head *list, int *idx,
442 			   char *type, char *op_result1, char *op_result2,
443 			   struct parse_events_error *err,
444 			   struct list_head *head_config,
445 			   struct parse_events_state *parse_state)
446 {
447 	struct perf_event_attr attr;
448 	LIST_HEAD(config_terms);
449 	char name[MAX_NAME_LEN];
450 	const char *config_name, *metric_id;
451 	int cache_type = -1, cache_op = -1, cache_result = -1;
452 	char *op_result[2] = { op_result1, op_result2 };
453 	int i, n, ret;
454 	bool hybrid;
455 
456 	/*
457 	 * No fallback - if we cannot get a clear cache type
458 	 * then bail out:
459 	 */
460 	cache_type = parse_aliases(type, evsel__hw_cache, PERF_COUNT_HW_CACHE_MAX);
461 	if (cache_type == -1)
462 		return -EINVAL;
463 
464 	config_name = get_config_name(head_config);
465 	n = snprintf(name, MAX_NAME_LEN, "%s", type);
466 
467 	for (i = 0; (i < 2) && (op_result[i]); i++) {
468 		char *str = op_result[i];
469 
470 		n += snprintf(name + n, MAX_NAME_LEN - n, "-%s", str);
471 
472 		if (cache_op == -1) {
473 			cache_op = parse_aliases(str, evsel__hw_cache_op,
474 						 PERF_COUNT_HW_CACHE_OP_MAX);
475 			if (cache_op >= 0) {
476 				if (!evsel__is_cache_op_valid(cache_type, cache_op))
477 					return -EINVAL;
478 				continue;
479 			}
480 		}
481 
482 		if (cache_result == -1) {
483 			cache_result = parse_aliases(str, evsel__hw_cache_result,
484 						     PERF_COUNT_HW_CACHE_RESULT_MAX);
485 			if (cache_result >= 0)
486 				continue;
487 		}
488 	}
489 
490 	/*
491 	 * Fall back to reads:
492 	 */
493 	if (cache_op == -1)
494 		cache_op = PERF_COUNT_HW_CACHE_OP_READ;
495 
496 	/*
497 	 * Fall back to accesses:
498 	 */
499 	if (cache_result == -1)
500 		cache_result = PERF_COUNT_HW_CACHE_RESULT_ACCESS;
501 
502 	memset(&attr, 0, sizeof(attr));
503 	attr.config = cache_type | (cache_op << 8) | (cache_result << 16);
504 	attr.type = PERF_TYPE_HW_CACHE;
505 
506 	if (head_config) {
507 		if (config_attr(&attr, head_config, err,
508 				config_term_common))
509 			return -EINVAL;
510 
511 		if (get_config_terms(head_config, &config_terms))
512 			return -ENOMEM;
513 	}
514 
515 	metric_id = get_config_metric_id(head_config);
516 	ret = parse_events__add_cache_hybrid(list, idx, &attr,
517 					     config_name ? : name,
518 					     metric_id,
519 					     &config_terms,
520 					     &hybrid, parse_state);
521 	if (hybrid)
522 		goto out_free_terms;
523 
524 	ret = add_event(list, idx, &attr, config_name ? : name, metric_id,
525 			&config_terms);
526 out_free_terms:
527 	free_config_terms(&config_terms);
528 	return ret;
529 }
530 
531 static void tracepoint_error(struct parse_events_error *e, int err,
532 			     const char *sys, const char *name)
533 {
534 	const char *str;
535 	char help[BUFSIZ];
536 
537 	if (!e)
538 		return;
539 
540 	/*
541 	 * We get error directly from syscall errno ( > 0),
542 	 * or from encoded pointer's error ( < 0).
543 	 */
544 	err = abs(err);
545 
546 	switch (err) {
547 	case EACCES:
548 		str = "can't access trace events";
549 		break;
550 	case ENOENT:
551 		str = "unknown tracepoint";
552 		break;
553 	default:
554 		str = "failed to add tracepoint";
555 		break;
556 	}
557 
558 	tracing_path__strerror_open_tp(err, help, sizeof(help), sys, name);
559 	parse_events_error__handle(e, 0, strdup(str), strdup(help));
560 }
561 
562 static int add_tracepoint(struct list_head *list, int *idx,
563 			  const char *sys_name, const char *evt_name,
564 			  struct parse_events_error *err,
565 			  struct list_head *head_config)
566 {
567 	struct evsel *evsel = evsel__newtp_idx(sys_name, evt_name, (*idx)++);
568 
569 	if (IS_ERR(evsel)) {
570 		tracepoint_error(err, PTR_ERR(evsel), sys_name, evt_name);
571 		return PTR_ERR(evsel);
572 	}
573 
574 	if (head_config) {
575 		LIST_HEAD(config_terms);
576 
577 		if (get_config_terms(head_config, &config_terms))
578 			return -ENOMEM;
579 		list_splice(&config_terms, &evsel->config_terms);
580 	}
581 
582 	list_add_tail(&evsel->core.node, list);
583 	return 0;
584 }
585 
586 static int add_tracepoint_multi_event(struct list_head *list, int *idx,
587 				      const char *sys_name, const char *evt_name,
588 				      struct parse_events_error *err,
589 				      struct list_head *head_config)
590 {
591 	char *evt_path;
592 	struct dirent *evt_ent;
593 	DIR *evt_dir;
594 	int ret = 0, found = 0;
595 
596 	evt_path = get_events_file(sys_name);
597 	if (!evt_path) {
598 		tracepoint_error(err, errno, sys_name, evt_name);
599 		return -1;
600 	}
601 	evt_dir = opendir(evt_path);
602 	if (!evt_dir) {
603 		put_events_file(evt_path);
604 		tracepoint_error(err, errno, sys_name, evt_name);
605 		return -1;
606 	}
607 
608 	while (!ret && (evt_ent = readdir(evt_dir))) {
609 		if (!strcmp(evt_ent->d_name, ".")
610 		    || !strcmp(evt_ent->d_name, "..")
611 		    || !strcmp(evt_ent->d_name, "enable")
612 		    || !strcmp(evt_ent->d_name, "filter"))
613 			continue;
614 
615 		if (!strglobmatch(evt_ent->d_name, evt_name))
616 			continue;
617 
618 		found++;
619 
620 		ret = add_tracepoint(list, idx, sys_name, evt_ent->d_name,
621 				     err, head_config);
622 	}
623 
624 	if (!found) {
625 		tracepoint_error(err, ENOENT, sys_name, evt_name);
626 		ret = -1;
627 	}
628 
629 	put_events_file(evt_path);
630 	closedir(evt_dir);
631 	return ret;
632 }
633 
634 static int add_tracepoint_event(struct list_head *list, int *idx,
635 				const char *sys_name, const char *evt_name,
636 				struct parse_events_error *err,
637 				struct list_head *head_config)
638 {
639 	return strpbrk(evt_name, "*?") ?
640 	       add_tracepoint_multi_event(list, idx, sys_name, evt_name,
641 					  err, head_config) :
642 	       add_tracepoint(list, idx, sys_name, evt_name,
643 			      err, head_config);
644 }
645 
646 static int add_tracepoint_multi_sys(struct list_head *list, int *idx,
647 				    const char *sys_name, const char *evt_name,
648 				    struct parse_events_error *err,
649 				    struct list_head *head_config)
650 {
651 	struct dirent *events_ent;
652 	DIR *events_dir;
653 	int ret = 0;
654 
655 	events_dir = tracing_events__opendir();
656 	if (!events_dir) {
657 		tracepoint_error(err, errno, sys_name, evt_name);
658 		return -1;
659 	}
660 
661 	while (!ret && (events_ent = readdir(events_dir))) {
662 		if (!strcmp(events_ent->d_name, ".")
663 		    || !strcmp(events_ent->d_name, "..")
664 		    || !strcmp(events_ent->d_name, "enable")
665 		    || !strcmp(events_ent->d_name, "header_event")
666 		    || !strcmp(events_ent->d_name, "header_page"))
667 			continue;
668 
669 		if (!strglobmatch(events_ent->d_name, sys_name))
670 			continue;
671 
672 		ret = add_tracepoint_event(list, idx, events_ent->d_name,
673 					   evt_name, err, head_config);
674 	}
675 
676 	closedir(events_dir);
677 	return ret;
678 }
679 
680 #ifdef HAVE_LIBBPF_SUPPORT
681 struct __add_bpf_event_param {
682 	struct parse_events_state *parse_state;
683 	struct list_head *list;
684 	struct list_head *head_config;
685 };
686 
687 static int add_bpf_event(const char *group, const char *event, int fd, struct bpf_object *obj,
688 			 void *_param)
689 {
690 	LIST_HEAD(new_evsels);
691 	struct __add_bpf_event_param *param = _param;
692 	struct parse_events_state *parse_state = param->parse_state;
693 	struct list_head *list = param->list;
694 	struct evsel *pos;
695 	int err;
696 	/*
697 	 * Check if we should add the event, i.e. if it is a TP but starts with a '!',
698 	 * then don't add the tracepoint, this will be used for something else, like
699 	 * adding to a BPF_MAP_TYPE_PROG_ARRAY.
700 	 *
701 	 * See tools/perf/examples/bpf/augmented_raw_syscalls.c
702 	 */
703 	if (group[0] == '!')
704 		return 0;
705 
706 	pr_debug("add bpf event %s:%s and attach bpf program %d\n",
707 		 group, event, fd);
708 
709 	err = parse_events_add_tracepoint(&new_evsels, &parse_state->idx, group,
710 					  event, parse_state->error,
711 					  param->head_config);
712 	if (err) {
713 		struct evsel *evsel, *tmp;
714 
715 		pr_debug("Failed to add BPF event %s:%s\n",
716 			 group, event);
717 		list_for_each_entry_safe(evsel, tmp, &new_evsels, core.node) {
718 			list_del_init(&evsel->core.node);
719 			evsel__delete(evsel);
720 		}
721 		return err;
722 	}
723 	pr_debug("adding %s:%s\n", group, event);
724 
725 	list_for_each_entry(pos, &new_evsels, core.node) {
726 		pr_debug("adding %s:%s to %p\n",
727 			 group, event, pos);
728 		pos->bpf_fd = fd;
729 		pos->bpf_obj = obj;
730 	}
731 	list_splice(&new_evsels, list);
732 	return 0;
733 }
734 
735 int parse_events_load_bpf_obj(struct parse_events_state *parse_state,
736 			      struct list_head *list,
737 			      struct bpf_object *obj,
738 			      struct list_head *head_config)
739 {
740 	int err;
741 	char errbuf[BUFSIZ];
742 	struct __add_bpf_event_param param = {parse_state, list, head_config};
743 	static bool registered_unprobe_atexit = false;
744 
745 	if (IS_ERR(obj) || !obj) {
746 		snprintf(errbuf, sizeof(errbuf),
747 			 "Internal error: load bpf obj with NULL");
748 		err = -EINVAL;
749 		goto errout;
750 	}
751 
752 	/*
753 	 * Register atexit handler before calling bpf__probe() so
754 	 * bpf__probe() don't need to unprobe probe points its already
755 	 * created when failure.
756 	 */
757 	if (!registered_unprobe_atexit) {
758 		atexit(bpf__clear);
759 		registered_unprobe_atexit = true;
760 	}
761 
762 	err = bpf__probe(obj);
763 	if (err) {
764 		bpf__strerror_probe(obj, err, errbuf, sizeof(errbuf));
765 		goto errout;
766 	}
767 
768 	err = bpf__load(obj);
769 	if (err) {
770 		bpf__strerror_load(obj, err, errbuf, sizeof(errbuf));
771 		goto errout;
772 	}
773 
774 	err = bpf__foreach_event(obj, add_bpf_event, &param);
775 	if (err) {
776 		snprintf(errbuf, sizeof(errbuf),
777 			 "Attach events in BPF object failed");
778 		goto errout;
779 	}
780 
781 	return 0;
782 errout:
783 	parse_events_error__handle(parse_state->error, 0,
784 				strdup(errbuf), strdup("(add -v to see detail)"));
785 	return err;
786 }
787 
788 static int
789 parse_events_config_bpf(struct parse_events_state *parse_state,
790 			struct bpf_object *obj,
791 			struct list_head *head_config)
792 {
793 	struct parse_events_term *term;
794 	int error_pos;
795 
796 	if (!head_config || list_empty(head_config))
797 		return 0;
798 
799 	list_for_each_entry(term, head_config, list) {
800 		int err;
801 
802 		if (term->type_term != PARSE_EVENTS__TERM_TYPE_USER) {
803 			parse_events_error__handle(parse_state->error, term->err_term,
804 						strdup("Invalid config term for BPF object"),
805 						NULL);
806 			return -EINVAL;
807 		}
808 
809 		err = bpf__config_obj(obj, term, parse_state->evlist, &error_pos);
810 		if (err) {
811 			char errbuf[BUFSIZ];
812 			int idx;
813 
814 			bpf__strerror_config_obj(obj, term, parse_state->evlist,
815 						 &error_pos, err, errbuf,
816 						 sizeof(errbuf));
817 
818 			if (err == -BPF_LOADER_ERRNO__OBJCONF_MAP_VALUE)
819 				idx = term->err_val;
820 			else
821 				idx = term->err_term + error_pos;
822 
823 			parse_events_error__handle(parse_state->error, idx,
824 						strdup(errbuf),
825 						strdup(
826 "Hint:\tValid config terms:\n"
827 "     \tmap:[<arraymap>].value<indices>=[value]\n"
828 "     \tmap:[<eventmap>].event<indices>=[event]\n"
829 "\n"
830 "     \twhere <indices> is something like [0,3...5] or [all]\n"
831 "     \t(add -v to see detail)"));
832 			return err;
833 		}
834 	}
835 	return 0;
836 }
837 
838 /*
839  * Split config terms:
840  * perf record -e bpf.c/call-graph=fp,map:array.value[0]=1/ ...
841  *  'call-graph=fp' is 'evt config', should be applied to each
842  *  events in bpf.c.
843  * 'map:array.value[0]=1' is 'obj config', should be processed
844  * with parse_events_config_bpf.
845  *
846  * Move object config terms from the first list to obj_head_config.
847  */
848 static void
849 split_bpf_config_terms(struct list_head *evt_head_config,
850 		       struct list_head *obj_head_config)
851 {
852 	struct parse_events_term *term, *temp;
853 
854 	/*
855 	 * Currently, all possible user config term
856 	 * belong to bpf object. parse_events__is_hardcoded_term()
857 	 * happens to be a good flag.
858 	 *
859 	 * See parse_events_config_bpf() and
860 	 * config_term_tracepoint().
861 	 */
862 	list_for_each_entry_safe(term, temp, evt_head_config, list)
863 		if (!parse_events__is_hardcoded_term(term))
864 			list_move_tail(&term->list, obj_head_config);
865 }
866 
867 int parse_events_load_bpf(struct parse_events_state *parse_state,
868 			  struct list_head *list,
869 			  char *bpf_file_name,
870 			  bool source,
871 			  struct list_head *head_config)
872 {
873 	int err;
874 	struct bpf_object *obj;
875 	LIST_HEAD(obj_head_config);
876 
877 	if (head_config)
878 		split_bpf_config_terms(head_config, &obj_head_config);
879 
880 	obj = bpf__prepare_load(bpf_file_name, source);
881 	if (IS_ERR(obj)) {
882 		char errbuf[BUFSIZ];
883 
884 		err = PTR_ERR(obj);
885 
886 		if (err == -ENOTSUP)
887 			snprintf(errbuf, sizeof(errbuf),
888 				 "BPF support is not compiled");
889 		else
890 			bpf__strerror_prepare_load(bpf_file_name,
891 						   source,
892 						   -err, errbuf,
893 						   sizeof(errbuf));
894 
895 		parse_events_error__handle(parse_state->error, 0,
896 					strdup(errbuf), strdup("(add -v to see detail)"));
897 		return err;
898 	}
899 
900 	err = parse_events_load_bpf_obj(parse_state, list, obj, head_config);
901 	if (err)
902 		return err;
903 	err = parse_events_config_bpf(parse_state, obj, &obj_head_config);
904 
905 	/*
906 	 * Caller doesn't know anything about obj_head_config,
907 	 * so combine them together again before returning.
908 	 */
909 	if (head_config)
910 		list_splice_tail(&obj_head_config, head_config);
911 	return err;
912 }
913 #else // HAVE_LIBBPF_SUPPORT
914 int parse_events_load_bpf_obj(struct parse_events_state *parse_state,
915 			      struct list_head *list __maybe_unused,
916 			      struct bpf_object *obj __maybe_unused,
917 			      struct list_head *head_config __maybe_unused)
918 {
919 	parse_events_error__handle(parse_state->error, 0,
920 				   strdup("BPF support is not compiled"),
921 				   strdup("Make sure libbpf-devel is available at build time."));
922 	return -ENOTSUP;
923 }
924 
925 int parse_events_load_bpf(struct parse_events_state *parse_state,
926 			  struct list_head *list __maybe_unused,
927 			  char *bpf_file_name __maybe_unused,
928 			  bool source __maybe_unused,
929 			  struct list_head *head_config __maybe_unused)
930 {
931 	parse_events_error__handle(parse_state->error, 0,
932 				   strdup("BPF support is not compiled"),
933 				   strdup("Make sure libbpf-devel is available at build time."));
934 	return -ENOTSUP;
935 }
936 #endif // HAVE_LIBBPF_SUPPORT
937 
938 static int
939 parse_breakpoint_type(const char *type, struct perf_event_attr *attr)
940 {
941 	int i;
942 
943 	for (i = 0; i < 3; i++) {
944 		if (!type || !type[i])
945 			break;
946 
947 #define CHECK_SET_TYPE(bit)		\
948 do {					\
949 	if (attr->bp_type & bit)	\
950 		return -EINVAL;		\
951 	else				\
952 		attr->bp_type |= bit;	\
953 } while (0)
954 
955 		switch (type[i]) {
956 		case 'r':
957 			CHECK_SET_TYPE(HW_BREAKPOINT_R);
958 			break;
959 		case 'w':
960 			CHECK_SET_TYPE(HW_BREAKPOINT_W);
961 			break;
962 		case 'x':
963 			CHECK_SET_TYPE(HW_BREAKPOINT_X);
964 			break;
965 		default:
966 			return -EINVAL;
967 		}
968 	}
969 
970 #undef CHECK_SET_TYPE
971 
972 	if (!attr->bp_type) /* Default */
973 		attr->bp_type = HW_BREAKPOINT_R | HW_BREAKPOINT_W;
974 
975 	return 0;
976 }
977 
978 int parse_events_add_breakpoint(struct list_head *list, int *idx,
979 				u64 addr, char *type, u64 len)
980 {
981 	struct perf_event_attr attr;
982 
983 	memset(&attr, 0, sizeof(attr));
984 	attr.bp_addr = addr;
985 
986 	if (parse_breakpoint_type(type, &attr))
987 		return -EINVAL;
988 
989 	/* Provide some defaults if len is not specified */
990 	if (!len) {
991 		if (attr.bp_type == HW_BREAKPOINT_X)
992 			len = sizeof(long);
993 		else
994 			len = HW_BREAKPOINT_LEN_4;
995 	}
996 
997 	attr.bp_len = len;
998 
999 	attr.type = PERF_TYPE_BREAKPOINT;
1000 	attr.sample_period = 1;
1001 
1002 	return add_event(list, idx, &attr, /*name=*/NULL, /*mertic_id=*/NULL,
1003 			 /*config_terms=*/NULL);
1004 }
1005 
1006 static int check_type_val(struct parse_events_term *term,
1007 			  struct parse_events_error *err,
1008 			  int type)
1009 {
1010 	if (type == term->type_val)
1011 		return 0;
1012 
1013 	if (err) {
1014 		parse_events_error__handle(err, term->err_val,
1015 					type == PARSE_EVENTS__TERM_TYPE_NUM
1016 					? strdup("expected numeric value")
1017 					: strdup("expected string value"),
1018 					NULL);
1019 	}
1020 	return -EINVAL;
1021 }
1022 
1023 /*
1024  * Update according to parse-events.l
1025  */
1026 static const char *config_term_names[__PARSE_EVENTS__TERM_TYPE_NR] = {
1027 	[PARSE_EVENTS__TERM_TYPE_USER]			= "<sysfs term>",
1028 	[PARSE_EVENTS__TERM_TYPE_CONFIG]		= "config",
1029 	[PARSE_EVENTS__TERM_TYPE_CONFIG1]		= "config1",
1030 	[PARSE_EVENTS__TERM_TYPE_CONFIG2]		= "config2",
1031 	[PARSE_EVENTS__TERM_TYPE_NAME]			= "name",
1032 	[PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD]		= "period",
1033 	[PARSE_EVENTS__TERM_TYPE_SAMPLE_FREQ]		= "freq",
1034 	[PARSE_EVENTS__TERM_TYPE_BRANCH_SAMPLE_TYPE]	= "branch_type",
1035 	[PARSE_EVENTS__TERM_TYPE_TIME]			= "time",
1036 	[PARSE_EVENTS__TERM_TYPE_CALLGRAPH]		= "call-graph",
1037 	[PARSE_EVENTS__TERM_TYPE_STACKSIZE]		= "stack-size",
1038 	[PARSE_EVENTS__TERM_TYPE_NOINHERIT]		= "no-inherit",
1039 	[PARSE_EVENTS__TERM_TYPE_INHERIT]		= "inherit",
1040 	[PARSE_EVENTS__TERM_TYPE_MAX_STACK]		= "max-stack",
1041 	[PARSE_EVENTS__TERM_TYPE_MAX_EVENTS]		= "nr",
1042 	[PARSE_EVENTS__TERM_TYPE_OVERWRITE]		= "overwrite",
1043 	[PARSE_EVENTS__TERM_TYPE_NOOVERWRITE]		= "no-overwrite",
1044 	[PARSE_EVENTS__TERM_TYPE_DRV_CFG]		= "driver-config",
1045 	[PARSE_EVENTS__TERM_TYPE_PERCORE]		= "percore",
1046 	[PARSE_EVENTS__TERM_TYPE_AUX_OUTPUT]		= "aux-output",
1047 	[PARSE_EVENTS__TERM_TYPE_AUX_SAMPLE_SIZE]	= "aux-sample-size",
1048 	[PARSE_EVENTS__TERM_TYPE_METRIC_ID]		= "metric-id",
1049 };
1050 
1051 static bool config_term_shrinked;
1052 
1053 static bool
1054 config_term_avail(int term_type, struct parse_events_error *err)
1055 {
1056 	char *err_str;
1057 
1058 	if (term_type < 0 || term_type >= __PARSE_EVENTS__TERM_TYPE_NR) {
1059 		parse_events_error__handle(err, -1,
1060 					strdup("Invalid term_type"), NULL);
1061 		return false;
1062 	}
1063 	if (!config_term_shrinked)
1064 		return true;
1065 
1066 	switch (term_type) {
1067 	case PARSE_EVENTS__TERM_TYPE_CONFIG:
1068 	case PARSE_EVENTS__TERM_TYPE_CONFIG1:
1069 	case PARSE_EVENTS__TERM_TYPE_CONFIG2:
1070 	case PARSE_EVENTS__TERM_TYPE_NAME:
1071 	case PARSE_EVENTS__TERM_TYPE_METRIC_ID:
1072 	case PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD:
1073 	case PARSE_EVENTS__TERM_TYPE_PERCORE:
1074 		return true;
1075 	default:
1076 		if (!err)
1077 			return false;
1078 
1079 		/* term_type is validated so indexing is safe */
1080 		if (asprintf(&err_str, "'%s' is not usable in 'perf stat'",
1081 				config_term_names[term_type]) >= 0)
1082 			parse_events_error__handle(err, -1, err_str, NULL);
1083 		return false;
1084 	}
1085 }
1086 
1087 void parse_events__shrink_config_terms(void)
1088 {
1089 	config_term_shrinked = true;
1090 }
1091 
1092 static int config_term_common(struct perf_event_attr *attr,
1093 			      struct parse_events_term *term,
1094 			      struct parse_events_error *err)
1095 {
1096 #define CHECK_TYPE_VAL(type)						   \
1097 do {									   \
1098 	if (check_type_val(term, err, PARSE_EVENTS__TERM_TYPE_ ## type)) \
1099 		return -EINVAL;						   \
1100 } while (0)
1101 
1102 	switch (term->type_term) {
1103 	case PARSE_EVENTS__TERM_TYPE_CONFIG:
1104 		CHECK_TYPE_VAL(NUM);
1105 		attr->config = term->val.num;
1106 		break;
1107 	case PARSE_EVENTS__TERM_TYPE_CONFIG1:
1108 		CHECK_TYPE_VAL(NUM);
1109 		attr->config1 = term->val.num;
1110 		break;
1111 	case PARSE_EVENTS__TERM_TYPE_CONFIG2:
1112 		CHECK_TYPE_VAL(NUM);
1113 		attr->config2 = term->val.num;
1114 		break;
1115 	case PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD:
1116 		CHECK_TYPE_VAL(NUM);
1117 		break;
1118 	case PARSE_EVENTS__TERM_TYPE_SAMPLE_FREQ:
1119 		CHECK_TYPE_VAL(NUM);
1120 		break;
1121 	case PARSE_EVENTS__TERM_TYPE_BRANCH_SAMPLE_TYPE:
1122 		CHECK_TYPE_VAL(STR);
1123 		if (strcmp(term->val.str, "no") &&
1124 		    parse_branch_str(term->val.str,
1125 				    &attr->branch_sample_type)) {
1126 			parse_events_error__handle(err, term->err_val,
1127 					strdup("invalid branch sample type"),
1128 					NULL);
1129 			return -EINVAL;
1130 		}
1131 		break;
1132 	case PARSE_EVENTS__TERM_TYPE_TIME:
1133 		CHECK_TYPE_VAL(NUM);
1134 		if (term->val.num > 1) {
1135 			parse_events_error__handle(err, term->err_val,
1136 						strdup("expected 0 or 1"),
1137 						NULL);
1138 			return -EINVAL;
1139 		}
1140 		break;
1141 	case PARSE_EVENTS__TERM_TYPE_CALLGRAPH:
1142 		CHECK_TYPE_VAL(STR);
1143 		break;
1144 	case PARSE_EVENTS__TERM_TYPE_STACKSIZE:
1145 		CHECK_TYPE_VAL(NUM);
1146 		break;
1147 	case PARSE_EVENTS__TERM_TYPE_INHERIT:
1148 		CHECK_TYPE_VAL(NUM);
1149 		break;
1150 	case PARSE_EVENTS__TERM_TYPE_NOINHERIT:
1151 		CHECK_TYPE_VAL(NUM);
1152 		break;
1153 	case PARSE_EVENTS__TERM_TYPE_OVERWRITE:
1154 		CHECK_TYPE_VAL(NUM);
1155 		break;
1156 	case PARSE_EVENTS__TERM_TYPE_NOOVERWRITE:
1157 		CHECK_TYPE_VAL(NUM);
1158 		break;
1159 	case PARSE_EVENTS__TERM_TYPE_NAME:
1160 		CHECK_TYPE_VAL(STR);
1161 		break;
1162 	case PARSE_EVENTS__TERM_TYPE_METRIC_ID:
1163 		CHECK_TYPE_VAL(STR);
1164 		break;
1165 	case PARSE_EVENTS__TERM_TYPE_MAX_STACK:
1166 		CHECK_TYPE_VAL(NUM);
1167 		break;
1168 	case PARSE_EVENTS__TERM_TYPE_MAX_EVENTS:
1169 		CHECK_TYPE_VAL(NUM);
1170 		break;
1171 	case PARSE_EVENTS__TERM_TYPE_PERCORE:
1172 		CHECK_TYPE_VAL(NUM);
1173 		if ((unsigned int)term->val.num > 1) {
1174 			parse_events_error__handle(err, term->err_val,
1175 						strdup("expected 0 or 1"),
1176 						NULL);
1177 			return -EINVAL;
1178 		}
1179 		break;
1180 	case PARSE_EVENTS__TERM_TYPE_AUX_OUTPUT:
1181 		CHECK_TYPE_VAL(NUM);
1182 		break;
1183 	case PARSE_EVENTS__TERM_TYPE_AUX_SAMPLE_SIZE:
1184 		CHECK_TYPE_VAL(NUM);
1185 		if (term->val.num > UINT_MAX) {
1186 			parse_events_error__handle(err, term->err_val,
1187 						strdup("too big"),
1188 						NULL);
1189 			return -EINVAL;
1190 		}
1191 		break;
1192 	default:
1193 		parse_events_error__handle(err, term->err_term,
1194 				strdup("unknown term"),
1195 				parse_events_formats_error_string(NULL));
1196 		return -EINVAL;
1197 	}
1198 
1199 	/*
1200 	 * Check term availability after basic checking so
1201 	 * PARSE_EVENTS__TERM_TYPE_USER can be found and filtered.
1202 	 *
1203 	 * If check availability at the entry of this function,
1204 	 * user will see "'<sysfs term>' is not usable in 'perf stat'"
1205 	 * if an invalid config term is provided for legacy events
1206 	 * (for example, instructions/badterm/...), which is confusing.
1207 	 */
1208 	if (!config_term_avail(term->type_term, err))
1209 		return -EINVAL;
1210 	return 0;
1211 #undef CHECK_TYPE_VAL
1212 }
1213 
1214 static int config_term_pmu(struct perf_event_attr *attr,
1215 			   struct parse_events_term *term,
1216 			   struct parse_events_error *err)
1217 {
1218 	if (term->type_term == PARSE_EVENTS__TERM_TYPE_USER ||
1219 	    term->type_term == PARSE_EVENTS__TERM_TYPE_DRV_CFG)
1220 		/*
1221 		 * Always succeed for sysfs terms, as we dont know
1222 		 * at this point what type they need to have.
1223 		 */
1224 		return 0;
1225 	else
1226 		return config_term_common(attr, term, err);
1227 }
1228 
1229 static int config_term_tracepoint(struct perf_event_attr *attr,
1230 				  struct parse_events_term *term,
1231 				  struct parse_events_error *err)
1232 {
1233 	switch (term->type_term) {
1234 	case PARSE_EVENTS__TERM_TYPE_CALLGRAPH:
1235 	case PARSE_EVENTS__TERM_TYPE_STACKSIZE:
1236 	case PARSE_EVENTS__TERM_TYPE_INHERIT:
1237 	case PARSE_EVENTS__TERM_TYPE_NOINHERIT:
1238 	case PARSE_EVENTS__TERM_TYPE_MAX_STACK:
1239 	case PARSE_EVENTS__TERM_TYPE_MAX_EVENTS:
1240 	case PARSE_EVENTS__TERM_TYPE_OVERWRITE:
1241 	case PARSE_EVENTS__TERM_TYPE_NOOVERWRITE:
1242 	case PARSE_EVENTS__TERM_TYPE_AUX_OUTPUT:
1243 	case PARSE_EVENTS__TERM_TYPE_AUX_SAMPLE_SIZE:
1244 		return config_term_common(attr, term, err);
1245 	default:
1246 		if (err) {
1247 			parse_events_error__handle(err, term->err_term,
1248 				strdup("unknown term"),
1249 				strdup("valid terms: call-graph,stack-size\n"));
1250 		}
1251 		return -EINVAL;
1252 	}
1253 
1254 	return 0;
1255 }
1256 
1257 static int config_attr(struct perf_event_attr *attr,
1258 		       struct list_head *head,
1259 		       struct parse_events_error *err,
1260 		       config_term_func_t config_term)
1261 {
1262 	struct parse_events_term *term;
1263 
1264 	list_for_each_entry(term, head, list)
1265 		if (config_term(attr, term, err))
1266 			return -EINVAL;
1267 
1268 	return 0;
1269 }
1270 
1271 static int get_config_terms(struct list_head *head_config,
1272 			    struct list_head *head_terms __maybe_unused)
1273 {
1274 #define ADD_CONFIG_TERM(__type, __weak)				\
1275 	struct evsel_config_term *__t;			\
1276 								\
1277 	__t = zalloc(sizeof(*__t));				\
1278 	if (!__t)						\
1279 		return -ENOMEM;					\
1280 								\
1281 	INIT_LIST_HEAD(&__t->list);				\
1282 	__t->type       = EVSEL__CONFIG_TERM_ ## __type;	\
1283 	__t->weak	= __weak;				\
1284 	list_add_tail(&__t->list, head_terms)
1285 
1286 #define ADD_CONFIG_TERM_VAL(__type, __name, __val, __weak)	\
1287 do {								\
1288 	ADD_CONFIG_TERM(__type, __weak);			\
1289 	__t->val.__name = __val;				\
1290 } while (0)
1291 
1292 #define ADD_CONFIG_TERM_STR(__type, __val, __weak)		\
1293 do {								\
1294 	ADD_CONFIG_TERM(__type, __weak);			\
1295 	__t->val.str = strdup(__val);				\
1296 	if (!__t->val.str) {					\
1297 		zfree(&__t);					\
1298 		return -ENOMEM;					\
1299 	}							\
1300 	__t->free_str = true;					\
1301 } while (0)
1302 
1303 	struct parse_events_term *term;
1304 
1305 	list_for_each_entry(term, head_config, list) {
1306 		switch (term->type_term) {
1307 		case PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD:
1308 			ADD_CONFIG_TERM_VAL(PERIOD, period, term->val.num, term->weak);
1309 			break;
1310 		case PARSE_EVENTS__TERM_TYPE_SAMPLE_FREQ:
1311 			ADD_CONFIG_TERM_VAL(FREQ, freq, term->val.num, term->weak);
1312 			break;
1313 		case PARSE_EVENTS__TERM_TYPE_TIME:
1314 			ADD_CONFIG_TERM_VAL(TIME, time, term->val.num, term->weak);
1315 			break;
1316 		case PARSE_EVENTS__TERM_TYPE_CALLGRAPH:
1317 			ADD_CONFIG_TERM_STR(CALLGRAPH, term->val.str, term->weak);
1318 			break;
1319 		case PARSE_EVENTS__TERM_TYPE_BRANCH_SAMPLE_TYPE:
1320 			ADD_CONFIG_TERM_STR(BRANCH, term->val.str, term->weak);
1321 			break;
1322 		case PARSE_EVENTS__TERM_TYPE_STACKSIZE:
1323 			ADD_CONFIG_TERM_VAL(STACK_USER, stack_user,
1324 					    term->val.num, term->weak);
1325 			break;
1326 		case PARSE_EVENTS__TERM_TYPE_INHERIT:
1327 			ADD_CONFIG_TERM_VAL(INHERIT, inherit,
1328 					    term->val.num ? 1 : 0, term->weak);
1329 			break;
1330 		case PARSE_EVENTS__TERM_TYPE_NOINHERIT:
1331 			ADD_CONFIG_TERM_VAL(INHERIT, inherit,
1332 					    term->val.num ? 0 : 1, term->weak);
1333 			break;
1334 		case PARSE_EVENTS__TERM_TYPE_MAX_STACK:
1335 			ADD_CONFIG_TERM_VAL(MAX_STACK, max_stack,
1336 					    term->val.num, term->weak);
1337 			break;
1338 		case PARSE_EVENTS__TERM_TYPE_MAX_EVENTS:
1339 			ADD_CONFIG_TERM_VAL(MAX_EVENTS, max_events,
1340 					    term->val.num, term->weak);
1341 			break;
1342 		case PARSE_EVENTS__TERM_TYPE_OVERWRITE:
1343 			ADD_CONFIG_TERM_VAL(OVERWRITE, overwrite,
1344 					    term->val.num ? 1 : 0, term->weak);
1345 			break;
1346 		case PARSE_EVENTS__TERM_TYPE_NOOVERWRITE:
1347 			ADD_CONFIG_TERM_VAL(OVERWRITE, overwrite,
1348 					    term->val.num ? 0 : 1, term->weak);
1349 			break;
1350 		case PARSE_EVENTS__TERM_TYPE_DRV_CFG:
1351 			ADD_CONFIG_TERM_STR(DRV_CFG, term->val.str, term->weak);
1352 			break;
1353 		case PARSE_EVENTS__TERM_TYPE_PERCORE:
1354 			ADD_CONFIG_TERM_VAL(PERCORE, percore,
1355 					    term->val.num ? true : false, term->weak);
1356 			break;
1357 		case PARSE_EVENTS__TERM_TYPE_AUX_OUTPUT:
1358 			ADD_CONFIG_TERM_VAL(AUX_OUTPUT, aux_output,
1359 					    term->val.num ? 1 : 0, term->weak);
1360 			break;
1361 		case PARSE_EVENTS__TERM_TYPE_AUX_SAMPLE_SIZE:
1362 			ADD_CONFIG_TERM_VAL(AUX_SAMPLE_SIZE, aux_sample_size,
1363 					    term->val.num, term->weak);
1364 			break;
1365 		default:
1366 			break;
1367 		}
1368 	}
1369 	return 0;
1370 }
1371 
1372 /*
1373  * Add EVSEL__CONFIG_TERM_CFG_CHG where cfg_chg will have a bit set for
1374  * each bit of attr->config that the user has changed.
1375  */
1376 static int get_config_chgs(struct perf_pmu *pmu, struct list_head *head_config,
1377 			   struct list_head *head_terms)
1378 {
1379 	struct parse_events_term *term;
1380 	u64 bits = 0;
1381 	int type;
1382 
1383 	list_for_each_entry(term, head_config, list) {
1384 		switch (term->type_term) {
1385 		case PARSE_EVENTS__TERM_TYPE_USER:
1386 			type = perf_pmu__format_type(&pmu->format, term->config);
1387 			if (type != PERF_PMU_FORMAT_VALUE_CONFIG)
1388 				continue;
1389 			bits |= perf_pmu__format_bits(&pmu->format, term->config);
1390 			break;
1391 		case PARSE_EVENTS__TERM_TYPE_CONFIG:
1392 			bits = ~(u64)0;
1393 			break;
1394 		default:
1395 			break;
1396 		}
1397 	}
1398 
1399 	if (bits)
1400 		ADD_CONFIG_TERM_VAL(CFG_CHG, cfg_chg, bits, false);
1401 
1402 #undef ADD_CONFIG_TERM
1403 	return 0;
1404 }
1405 
1406 int parse_events_add_tracepoint(struct list_head *list, int *idx,
1407 				const char *sys, const char *event,
1408 				struct parse_events_error *err,
1409 				struct list_head *head_config)
1410 {
1411 	if (head_config) {
1412 		struct perf_event_attr attr;
1413 
1414 		if (config_attr(&attr, head_config, err,
1415 				config_term_tracepoint))
1416 			return -EINVAL;
1417 	}
1418 
1419 	if (strpbrk(sys, "*?"))
1420 		return add_tracepoint_multi_sys(list, idx, sys, event,
1421 						err, head_config);
1422 	else
1423 		return add_tracepoint_event(list, idx, sys, event,
1424 					    err, head_config);
1425 }
1426 
1427 int parse_events_add_numeric(struct parse_events_state *parse_state,
1428 			     struct list_head *list,
1429 			     u32 type, u64 config,
1430 			     struct list_head *head_config)
1431 {
1432 	struct perf_event_attr attr;
1433 	LIST_HEAD(config_terms);
1434 	const char *name, *metric_id;
1435 	bool hybrid;
1436 	int ret;
1437 
1438 	memset(&attr, 0, sizeof(attr));
1439 	attr.type = type;
1440 	attr.config = config;
1441 
1442 	if (head_config) {
1443 		if (config_attr(&attr, head_config, parse_state->error,
1444 				config_term_common))
1445 			return -EINVAL;
1446 
1447 		if (get_config_terms(head_config, &config_terms))
1448 			return -ENOMEM;
1449 	}
1450 
1451 	name = get_config_name(head_config);
1452 	metric_id = get_config_metric_id(head_config);
1453 	ret = parse_events__add_numeric_hybrid(parse_state, list, &attr,
1454 					       name, metric_id,
1455 					       &config_terms, &hybrid);
1456 	if (hybrid)
1457 		goto out_free_terms;
1458 
1459 	ret = add_event(list, &parse_state->idx, &attr, name, metric_id,
1460 			&config_terms);
1461 out_free_terms:
1462 	free_config_terms(&config_terms);
1463 	return ret;
1464 }
1465 
1466 int parse_events_add_tool(struct parse_events_state *parse_state,
1467 			  struct list_head *list,
1468 			  int tool_event)
1469 {
1470 	return add_event_tool(list, &parse_state->idx, tool_event);
1471 }
1472 
1473 static bool config_term_percore(struct list_head *config_terms)
1474 {
1475 	struct evsel_config_term *term;
1476 
1477 	list_for_each_entry(term, config_terms, list) {
1478 		if (term->type == EVSEL__CONFIG_TERM_PERCORE)
1479 			return term->val.percore;
1480 	}
1481 
1482 	return false;
1483 }
1484 
1485 static int parse_events__inside_hybrid_pmu(struct parse_events_state *parse_state,
1486 					   struct list_head *list, char *name,
1487 					   struct list_head *head_config)
1488 {
1489 	struct parse_events_term *term;
1490 	int ret = -1;
1491 
1492 	if (parse_state->fake_pmu || !head_config || list_empty(head_config) ||
1493 	    !perf_pmu__is_hybrid(name)) {
1494 		return -1;
1495 	}
1496 
1497 	/*
1498 	 * More than one term in list.
1499 	 */
1500 	if (head_config->next && head_config->next->next != head_config)
1501 		return -1;
1502 
1503 	term = list_first_entry(head_config, struct parse_events_term, list);
1504 	if (term && term->config && strcmp(term->config, "event")) {
1505 		ret = parse_events__with_hybrid_pmu(parse_state, term->config,
1506 						    name, list);
1507 	}
1508 
1509 	return ret;
1510 }
1511 
1512 int parse_events_add_pmu(struct parse_events_state *parse_state,
1513 			 struct list_head *list, char *name,
1514 			 struct list_head *head_config,
1515 			 bool auto_merge_stats,
1516 			 bool use_alias)
1517 {
1518 	struct perf_event_attr attr;
1519 	struct perf_pmu_info info;
1520 	struct perf_pmu *pmu;
1521 	struct evsel *evsel;
1522 	struct parse_events_error *err = parse_state->error;
1523 	bool use_uncore_alias;
1524 	LIST_HEAD(config_terms);
1525 
1526 	if (verbose > 1) {
1527 		fprintf(stderr, "Attempting to add event pmu '%s' with '",
1528 			name);
1529 		if (head_config) {
1530 			struct parse_events_term *term;
1531 
1532 			list_for_each_entry(term, head_config, list) {
1533 				fprintf(stderr, "%s,", term->config);
1534 			}
1535 		}
1536 		fprintf(stderr, "' that may result in non-fatal errors\n");
1537 	}
1538 
1539 	pmu = parse_state->fake_pmu ?: perf_pmu__find(name);
1540 	if (!pmu) {
1541 		char *err_str;
1542 
1543 		if (asprintf(&err_str,
1544 				"Cannot find PMU `%s'. Missing kernel support?",
1545 				name) >= 0)
1546 			parse_events_error__handle(err, 0, err_str, NULL);
1547 		return -EINVAL;
1548 	}
1549 
1550 	if (pmu->default_config) {
1551 		memcpy(&attr, pmu->default_config,
1552 		       sizeof(struct perf_event_attr));
1553 	} else {
1554 		memset(&attr, 0, sizeof(attr));
1555 	}
1556 
1557 	use_uncore_alias = (pmu->is_uncore && use_alias);
1558 
1559 	if (!head_config) {
1560 		attr.type = pmu->type;
1561 		evsel = __add_event(list, &parse_state->idx, &attr,
1562 				    /*init_attr=*/true, /*name=*/NULL,
1563 				    /*metric_id=*/NULL, pmu,
1564 				    /*config_terms=*/NULL, auto_merge_stats,
1565 				    /*cpu_list=*/NULL);
1566 		if (evsel) {
1567 			evsel->pmu_name = name ? strdup(name) : NULL;
1568 			evsel->use_uncore_alias = use_uncore_alias;
1569 			return 0;
1570 		} else {
1571 			return -ENOMEM;
1572 		}
1573 	}
1574 
1575 	if (!parse_state->fake_pmu && perf_pmu__check_alias(pmu, head_config, &info))
1576 		return -EINVAL;
1577 
1578 	if (verbose > 1) {
1579 		fprintf(stderr, "After aliases, add event pmu '%s' with '",
1580 			name);
1581 		if (head_config) {
1582 			struct parse_events_term *term;
1583 
1584 			list_for_each_entry(term, head_config, list) {
1585 				fprintf(stderr, "%s,", term->config);
1586 			}
1587 		}
1588 		fprintf(stderr, "' that may result in non-fatal errors\n");
1589 	}
1590 
1591 	/*
1592 	 * Configure hardcoded terms first, no need to check
1593 	 * return value when called with fail == 0 ;)
1594 	 */
1595 	if (config_attr(&attr, head_config, parse_state->error, config_term_pmu))
1596 		return -EINVAL;
1597 
1598 	if (get_config_terms(head_config, &config_terms))
1599 		return -ENOMEM;
1600 
1601 	/*
1602 	 * When using default config, record which bits of attr->config were
1603 	 * changed by the user.
1604 	 */
1605 	if (pmu->default_config && get_config_chgs(pmu, head_config, &config_terms))
1606 		return -ENOMEM;
1607 
1608 	if (!parse_events__inside_hybrid_pmu(parse_state, list, name,
1609 					     head_config)) {
1610 		return 0;
1611 	}
1612 
1613 	if (!parse_state->fake_pmu && perf_pmu__config(pmu, &attr, head_config, parse_state->error)) {
1614 		free_config_terms(&config_terms);
1615 		return -EINVAL;
1616 	}
1617 
1618 	evsel = __add_event(list, &parse_state->idx, &attr, /*init_attr=*/true,
1619 			    get_config_name(head_config),
1620 			    get_config_metric_id(head_config), pmu,
1621 			    &config_terms, auto_merge_stats, /*cpu_list=*/NULL);
1622 	if (!evsel)
1623 		return -ENOMEM;
1624 
1625 	if (evsel->name)
1626 		evsel->use_config_name = true;
1627 
1628 	evsel->pmu_name = name ? strdup(name) : NULL;
1629 	evsel->use_uncore_alias = use_uncore_alias;
1630 	evsel->percore = config_term_percore(&evsel->config_terms);
1631 
1632 	if (parse_state->fake_pmu)
1633 		return 0;
1634 
1635 	free((char *)evsel->unit);
1636 	evsel->unit = strdup(info.unit);
1637 	evsel->scale = info.scale;
1638 	evsel->per_pkg = info.per_pkg;
1639 	evsel->snapshot = info.snapshot;
1640 	evsel->metric_expr = info.metric_expr;
1641 	evsel->metric_name = info.metric_name;
1642 	return 0;
1643 }
1644 
1645 int parse_events_multi_pmu_add(struct parse_events_state *parse_state,
1646 			       char *str, struct list_head *head,
1647 			       struct list_head **listp)
1648 {
1649 	struct parse_events_term *term;
1650 	struct list_head *list = NULL;
1651 	struct list_head *orig_head = NULL;
1652 	struct perf_pmu *pmu = NULL;
1653 	int ok = 0;
1654 	char *config;
1655 
1656 	*listp = NULL;
1657 
1658 	if (!head) {
1659 		head = malloc(sizeof(struct list_head));
1660 		if (!head)
1661 			goto out_err;
1662 
1663 		INIT_LIST_HEAD(head);
1664 	}
1665 	config = strdup(str);
1666 	if (!config)
1667 		goto out_err;
1668 
1669 	if (parse_events_term__num(&term,
1670 				   PARSE_EVENTS__TERM_TYPE_USER,
1671 				   config, 1, false, &config,
1672 					NULL) < 0) {
1673 		free(config);
1674 		goto out_err;
1675 	}
1676 	list_add_tail(&term->list, head);
1677 
1678 	/* Add it for all PMUs that support the alias */
1679 	list = malloc(sizeof(struct list_head));
1680 	if (!list)
1681 		goto out_err;
1682 
1683 	INIT_LIST_HEAD(list);
1684 
1685 	while ((pmu = perf_pmu__scan(pmu)) != NULL) {
1686 		struct perf_pmu_alias *alias;
1687 
1688 		list_for_each_entry(alias, &pmu->aliases, list) {
1689 			if (!strcasecmp(alias->name, str)) {
1690 				parse_events_copy_term_list(head, &orig_head);
1691 				if (!parse_events_add_pmu(parse_state, list,
1692 							  pmu->name, orig_head,
1693 							  true, true)) {
1694 					pr_debug("%s -> %s/%s/\n", str,
1695 						 pmu->name, alias->str);
1696 					ok++;
1697 				}
1698 				parse_events_terms__delete(orig_head);
1699 			}
1700 		}
1701 	}
1702 
1703 	if (parse_state->fake_pmu) {
1704 		if (!parse_events_add_pmu(parse_state, list, str, head,
1705 					  true, true)) {
1706 			pr_debug("%s -> %s/%s/\n", str, "fake_pmu", str);
1707 			ok++;
1708 		}
1709 	}
1710 
1711 out_err:
1712 	if (ok)
1713 		*listp = list;
1714 	else
1715 		free(list);
1716 
1717 	parse_events_terms__delete(head);
1718 	return ok ? 0 : -1;
1719 }
1720 
1721 int parse_events__modifier_group(struct list_head *list,
1722 				 char *event_mod)
1723 {
1724 	return parse_events__modifier_event(list, event_mod, true);
1725 }
1726 
1727 /*
1728  * Check if the two uncore PMUs are from the same uncore block
1729  * The format of the uncore PMU name is uncore_#blockname_#pmuidx
1730  */
1731 static bool is_same_uncore_block(const char *pmu_name_a, const char *pmu_name_b)
1732 {
1733 	char *end_a, *end_b;
1734 
1735 	end_a = strrchr(pmu_name_a, '_');
1736 	end_b = strrchr(pmu_name_b, '_');
1737 
1738 	if (!end_a || !end_b)
1739 		return false;
1740 
1741 	if ((end_a - pmu_name_a) != (end_b - pmu_name_b))
1742 		return false;
1743 
1744 	return (strncmp(pmu_name_a, pmu_name_b, end_a - pmu_name_a) == 0);
1745 }
1746 
1747 static int
1748 parse_events__set_leader_for_uncore_aliase(char *name, struct list_head *list,
1749 					   struct parse_events_state *parse_state)
1750 {
1751 	struct evsel *evsel, *leader;
1752 	uintptr_t *leaders;
1753 	bool is_leader = true;
1754 	int i, nr_pmu = 0, total_members, ret = 0;
1755 
1756 	leader = list_first_entry(list, struct evsel, core.node);
1757 	evsel = list_last_entry(list, struct evsel, core.node);
1758 	total_members = evsel->core.idx - leader->core.idx + 1;
1759 
1760 	leaders = calloc(total_members, sizeof(uintptr_t));
1761 	if (WARN_ON(!leaders))
1762 		return 0;
1763 
1764 	/*
1765 	 * Going through the whole group and doing sanity check.
1766 	 * All members must use alias, and be from the same uncore block.
1767 	 * Also, storing the leader events in an array.
1768 	 */
1769 	__evlist__for_each_entry(list, evsel) {
1770 
1771 		/* Only split the uncore group which members use alias */
1772 		if (!evsel->use_uncore_alias)
1773 			goto out;
1774 
1775 		/* The events must be from the same uncore block */
1776 		if (!is_same_uncore_block(leader->pmu_name, evsel->pmu_name))
1777 			goto out;
1778 
1779 		if (!is_leader)
1780 			continue;
1781 		/*
1782 		 * If the event's PMU name starts to repeat, it must be a new
1783 		 * event. That can be used to distinguish the leader from
1784 		 * other members, even they have the same event name.
1785 		 */
1786 		if ((leader != evsel) &&
1787 		    !strcmp(leader->pmu_name, evsel->pmu_name)) {
1788 			is_leader = false;
1789 			continue;
1790 		}
1791 
1792 		/* Store the leader event for each PMU */
1793 		leaders[nr_pmu++] = (uintptr_t) evsel;
1794 	}
1795 
1796 	/* only one event alias */
1797 	if (nr_pmu == total_members) {
1798 		parse_state->nr_groups--;
1799 		goto handled;
1800 	}
1801 
1802 	/*
1803 	 * An uncore event alias is a joint name which means the same event
1804 	 * runs on all PMUs of a block.
1805 	 * Perf doesn't support mixed events from different PMUs in the same
1806 	 * group. The big group has to be split into multiple small groups
1807 	 * which only include the events from the same PMU.
1808 	 *
1809 	 * Here the uncore event aliases must be from the same uncore block.
1810 	 * The number of PMUs must be same for each alias. The number of new
1811 	 * small groups equals to the number of PMUs.
1812 	 * Setting the leader event for corresponding members in each group.
1813 	 */
1814 	i = 0;
1815 	__evlist__for_each_entry(list, evsel) {
1816 		if (i >= nr_pmu)
1817 			i = 0;
1818 		evsel__set_leader(evsel, (struct evsel *) leaders[i++]);
1819 	}
1820 
1821 	/* The number of members and group name are same for each group */
1822 	for (i = 0; i < nr_pmu; i++) {
1823 		evsel = (struct evsel *) leaders[i];
1824 		evsel->core.nr_members = total_members / nr_pmu;
1825 		evsel->group_name = name ? strdup(name) : NULL;
1826 	}
1827 
1828 	/* Take the new small groups into account */
1829 	parse_state->nr_groups += nr_pmu - 1;
1830 
1831 handled:
1832 	ret = 1;
1833 out:
1834 	free(leaders);
1835 	return ret;
1836 }
1837 
1838 __weak struct evsel *arch_evlist__leader(struct list_head *list)
1839 {
1840 	return list_first_entry(list, struct evsel, core.node);
1841 }
1842 
1843 void parse_events__set_leader(char *name, struct list_head *list,
1844 			      struct parse_events_state *parse_state)
1845 {
1846 	struct evsel *leader;
1847 
1848 	if (list_empty(list)) {
1849 		WARN_ONCE(true, "WARNING: failed to set leader: empty list");
1850 		return;
1851 	}
1852 
1853 	if (parse_events__set_leader_for_uncore_aliase(name, list, parse_state))
1854 		return;
1855 
1856 	leader = arch_evlist__leader(list);
1857 	__perf_evlist__set_leader(list, &leader->core);
1858 	leader->group_name = name ? strdup(name) : NULL;
1859 	list_move(&leader->core.node, list);
1860 }
1861 
1862 /* list_event is assumed to point to malloc'ed memory */
1863 void parse_events_update_lists(struct list_head *list_event,
1864 			       struct list_head *list_all)
1865 {
1866 	/*
1867 	 * Called for single event definition. Update the
1868 	 * 'all event' list, and reinit the 'single event'
1869 	 * list, for next event definition.
1870 	 */
1871 	list_splice_tail(list_event, list_all);
1872 	free(list_event);
1873 }
1874 
1875 struct event_modifier {
1876 	int eu;
1877 	int ek;
1878 	int eh;
1879 	int eH;
1880 	int eG;
1881 	int eI;
1882 	int precise;
1883 	int precise_max;
1884 	int exclude_GH;
1885 	int sample_read;
1886 	int pinned;
1887 	int weak;
1888 	int exclusive;
1889 	int bpf_counter;
1890 };
1891 
1892 static int get_event_modifier(struct event_modifier *mod, char *str,
1893 			       struct evsel *evsel)
1894 {
1895 	int eu = evsel ? evsel->core.attr.exclude_user : 0;
1896 	int ek = evsel ? evsel->core.attr.exclude_kernel : 0;
1897 	int eh = evsel ? evsel->core.attr.exclude_hv : 0;
1898 	int eH = evsel ? evsel->core.attr.exclude_host : 0;
1899 	int eG = evsel ? evsel->core.attr.exclude_guest : 0;
1900 	int eI = evsel ? evsel->core.attr.exclude_idle : 0;
1901 	int precise = evsel ? evsel->core.attr.precise_ip : 0;
1902 	int precise_max = 0;
1903 	int sample_read = 0;
1904 	int pinned = evsel ? evsel->core.attr.pinned : 0;
1905 	int exclusive = evsel ? evsel->core.attr.exclusive : 0;
1906 
1907 	int exclude = eu | ek | eh;
1908 	int exclude_GH = evsel ? evsel->exclude_GH : 0;
1909 	int weak = 0;
1910 	int bpf_counter = 0;
1911 
1912 	memset(mod, 0, sizeof(*mod));
1913 
1914 	while (*str) {
1915 		if (*str == 'u') {
1916 			if (!exclude)
1917 				exclude = eu = ek = eh = 1;
1918 			if (!exclude_GH && !perf_guest)
1919 				eG = 1;
1920 			eu = 0;
1921 		} else if (*str == 'k') {
1922 			if (!exclude)
1923 				exclude = eu = ek = eh = 1;
1924 			ek = 0;
1925 		} else if (*str == 'h') {
1926 			if (!exclude)
1927 				exclude = eu = ek = eh = 1;
1928 			eh = 0;
1929 		} else if (*str == 'G') {
1930 			if (!exclude_GH)
1931 				exclude_GH = eG = eH = 1;
1932 			eG = 0;
1933 		} else if (*str == 'H') {
1934 			if (!exclude_GH)
1935 				exclude_GH = eG = eH = 1;
1936 			eH = 0;
1937 		} else if (*str == 'I') {
1938 			eI = 1;
1939 		} else if (*str == 'p') {
1940 			precise++;
1941 			/* use of precise requires exclude_guest */
1942 			if (!exclude_GH)
1943 				eG = 1;
1944 		} else if (*str == 'P') {
1945 			precise_max = 1;
1946 		} else if (*str == 'S') {
1947 			sample_read = 1;
1948 		} else if (*str == 'D') {
1949 			pinned = 1;
1950 		} else if (*str == 'e') {
1951 			exclusive = 1;
1952 		} else if (*str == 'W') {
1953 			weak = 1;
1954 		} else if (*str == 'b') {
1955 			bpf_counter = 1;
1956 		} else
1957 			break;
1958 
1959 		++str;
1960 	}
1961 
1962 	/*
1963 	 * precise ip:
1964 	 *
1965 	 *  0 - SAMPLE_IP can have arbitrary skid
1966 	 *  1 - SAMPLE_IP must have constant skid
1967 	 *  2 - SAMPLE_IP requested to have 0 skid
1968 	 *  3 - SAMPLE_IP must have 0 skid
1969 	 *
1970 	 *  See also PERF_RECORD_MISC_EXACT_IP
1971 	 */
1972 	if (precise > 3)
1973 		return -EINVAL;
1974 
1975 	mod->eu = eu;
1976 	mod->ek = ek;
1977 	mod->eh = eh;
1978 	mod->eH = eH;
1979 	mod->eG = eG;
1980 	mod->eI = eI;
1981 	mod->precise = precise;
1982 	mod->precise_max = precise_max;
1983 	mod->exclude_GH = exclude_GH;
1984 	mod->sample_read = sample_read;
1985 	mod->pinned = pinned;
1986 	mod->weak = weak;
1987 	mod->bpf_counter = bpf_counter;
1988 	mod->exclusive = exclusive;
1989 
1990 	return 0;
1991 }
1992 
1993 /*
1994  * Basic modifier sanity check to validate it contains only one
1995  * instance of any modifier (apart from 'p') present.
1996  */
1997 static int check_modifier(char *str)
1998 {
1999 	char *p = str;
2000 
2001 	/* The sizeof includes 0 byte as well. */
2002 	if (strlen(str) > (sizeof("ukhGHpppPSDIWeb") - 1))
2003 		return -1;
2004 
2005 	while (*p) {
2006 		if (*p != 'p' && strchr(p + 1, *p))
2007 			return -1;
2008 		p++;
2009 	}
2010 
2011 	return 0;
2012 }
2013 
2014 int parse_events__modifier_event(struct list_head *list, char *str, bool add)
2015 {
2016 	struct evsel *evsel;
2017 	struct event_modifier mod;
2018 
2019 	if (str == NULL)
2020 		return 0;
2021 
2022 	if (check_modifier(str))
2023 		return -EINVAL;
2024 
2025 	if (!add && get_event_modifier(&mod, str, NULL))
2026 		return -EINVAL;
2027 
2028 	__evlist__for_each_entry(list, evsel) {
2029 		if (add && get_event_modifier(&mod, str, evsel))
2030 			return -EINVAL;
2031 
2032 		evsel->core.attr.exclude_user   = mod.eu;
2033 		evsel->core.attr.exclude_kernel = mod.ek;
2034 		evsel->core.attr.exclude_hv     = mod.eh;
2035 		evsel->core.attr.precise_ip     = mod.precise;
2036 		evsel->core.attr.exclude_host   = mod.eH;
2037 		evsel->core.attr.exclude_guest  = mod.eG;
2038 		evsel->core.attr.exclude_idle   = mod.eI;
2039 		evsel->exclude_GH          = mod.exclude_GH;
2040 		evsel->sample_read         = mod.sample_read;
2041 		evsel->precise_max         = mod.precise_max;
2042 		evsel->weak_group	   = mod.weak;
2043 		evsel->bpf_counter	   = mod.bpf_counter;
2044 
2045 		if (evsel__is_group_leader(evsel)) {
2046 			evsel->core.attr.pinned = mod.pinned;
2047 			evsel->core.attr.exclusive = mod.exclusive;
2048 		}
2049 	}
2050 
2051 	return 0;
2052 }
2053 
2054 int parse_events_name(struct list_head *list, const char *name)
2055 {
2056 	struct evsel *evsel;
2057 
2058 	__evlist__for_each_entry(list, evsel) {
2059 		if (!evsel->name)
2060 			evsel->name = strdup(name);
2061 	}
2062 
2063 	return 0;
2064 }
2065 
2066 static int
2067 comp_pmu(const void *p1, const void *p2)
2068 {
2069 	struct perf_pmu_event_symbol *pmu1 = (struct perf_pmu_event_symbol *) p1;
2070 	struct perf_pmu_event_symbol *pmu2 = (struct perf_pmu_event_symbol *) p2;
2071 
2072 	return strcasecmp(pmu1->symbol, pmu2->symbol);
2073 }
2074 
2075 static void perf_pmu__parse_cleanup(void)
2076 {
2077 	if (perf_pmu_events_list_num > 0) {
2078 		struct perf_pmu_event_symbol *p;
2079 		int i;
2080 
2081 		for (i = 0; i < perf_pmu_events_list_num; i++) {
2082 			p = perf_pmu_events_list + i;
2083 			zfree(&p->symbol);
2084 		}
2085 		zfree(&perf_pmu_events_list);
2086 		perf_pmu_events_list_num = 0;
2087 	}
2088 }
2089 
2090 #define SET_SYMBOL(str, stype)		\
2091 do {					\
2092 	p->symbol = str;		\
2093 	if (!p->symbol)			\
2094 		goto err;		\
2095 	p->type = stype;		\
2096 } while (0)
2097 
2098 /*
2099  * Read the pmu events list from sysfs
2100  * Save it into perf_pmu_events_list
2101  */
2102 static void perf_pmu__parse_init(void)
2103 {
2104 
2105 	struct perf_pmu *pmu = NULL;
2106 	struct perf_pmu_alias *alias;
2107 	int len = 0;
2108 
2109 	pmu = NULL;
2110 	while ((pmu = perf_pmu__scan(pmu)) != NULL) {
2111 		list_for_each_entry(alias, &pmu->aliases, list) {
2112 			char *tmp = strchr(alias->name, '-');
2113 
2114 			if (tmp) {
2115 				char *tmp2 = NULL;
2116 
2117 				tmp2 = strchr(tmp + 1, '-');
2118 				len++;
2119 				if (tmp2)
2120 					len++;
2121 			}
2122 
2123 			len++;
2124 		}
2125 	}
2126 
2127 	if (len == 0) {
2128 		perf_pmu_events_list_num = -1;
2129 		return;
2130 	}
2131 	perf_pmu_events_list = malloc(sizeof(struct perf_pmu_event_symbol) * len);
2132 	if (!perf_pmu_events_list)
2133 		return;
2134 	perf_pmu_events_list_num = len;
2135 
2136 	len = 0;
2137 	pmu = NULL;
2138 	while ((pmu = perf_pmu__scan(pmu)) != NULL) {
2139 		list_for_each_entry(alias, &pmu->aliases, list) {
2140 			struct perf_pmu_event_symbol *p = perf_pmu_events_list + len;
2141 			char *tmp = strchr(alias->name, '-');
2142 			char *tmp2 = NULL;
2143 
2144 			if (tmp)
2145 				tmp2 = strchr(tmp + 1, '-');
2146 			if (tmp2) {
2147 				SET_SYMBOL(strndup(alias->name, tmp - alias->name),
2148 						PMU_EVENT_SYMBOL_PREFIX);
2149 				p++;
2150 				tmp++;
2151 				SET_SYMBOL(strndup(tmp, tmp2 - tmp), PMU_EVENT_SYMBOL_SUFFIX);
2152 				p++;
2153 				SET_SYMBOL(strdup(++tmp2), PMU_EVENT_SYMBOL_SUFFIX2);
2154 				len += 3;
2155 			} else if (tmp) {
2156 				SET_SYMBOL(strndup(alias->name, tmp - alias->name),
2157 						PMU_EVENT_SYMBOL_PREFIX);
2158 				p++;
2159 				SET_SYMBOL(strdup(++tmp), PMU_EVENT_SYMBOL_SUFFIX);
2160 				len += 2;
2161 			} else {
2162 				SET_SYMBOL(strdup(alias->name), PMU_EVENT_SYMBOL);
2163 				len++;
2164 			}
2165 		}
2166 	}
2167 	qsort(perf_pmu_events_list, len,
2168 		sizeof(struct perf_pmu_event_symbol), comp_pmu);
2169 
2170 	return;
2171 err:
2172 	perf_pmu__parse_cleanup();
2173 }
2174 
2175 /*
2176  * This function injects special term in
2177  * perf_pmu_events_list so the test code
2178  * can check on this functionality.
2179  */
2180 int perf_pmu__test_parse_init(void)
2181 {
2182 	struct perf_pmu_event_symbol *list, *tmp, symbols[] = {
2183 		{(char *)"read", PMU_EVENT_SYMBOL},
2184 		{(char *)"event", PMU_EVENT_SYMBOL_PREFIX},
2185 		{(char *)"two", PMU_EVENT_SYMBOL_SUFFIX},
2186 		{(char *)"hyphen", PMU_EVENT_SYMBOL_SUFFIX},
2187 		{(char *)"hyph", PMU_EVENT_SYMBOL_SUFFIX2},
2188 	};
2189 	unsigned long i, j;
2190 
2191 	tmp = list = malloc(sizeof(*list) * ARRAY_SIZE(symbols));
2192 	if (!list)
2193 		return -ENOMEM;
2194 
2195 	for (i = 0; i < ARRAY_SIZE(symbols); i++, tmp++) {
2196 		tmp->type = symbols[i].type;
2197 		tmp->symbol = strdup(symbols[i].symbol);
2198 		if (!tmp->symbol)
2199 			goto err_free;
2200 	}
2201 
2202 	perf_pmu_events_list = list;
2203 	perf_pmu_events_list_num = ARRAY_SIZE(symbols);
2204 
2205 	qsort(perf_pmu_events_list, ARRAY_SIZE(symbols),
2206 	      sizeof(struct perf_pmu_event_symbol), comp_pmu);
2207 	return 0;
2208 
2209 err_free:
2210 	for (j = 0, tmp = list; j < i; j++, tmp++)
2211 		free(tmp->symbol);
2212 	free(list);
2213 	return -ENOMEM;
2214 }
2215 
2216 enum perf_pmu_event_symbol_type
2217 perf_pmu__parse_check(const char *name)
2218 {
2219 	struct perf_pmu_event_symbol p, *r;
2220 
2221 	/* scan kernel pmu events from sysfs if needed */
2222 	if (perf_pmu_events_list_num == 0)
2223 		perf_pmu__parse_init();
2224 	/*
2225 	 * name "cpu" could be prefix of cpu-cycles or cpu// events.
2226 	 * cpu-cycles has been handled by hardcode.
2227 	 * So it must be cpu// events, not kernel pmu event.
2228 	 */
2229 	if ((perf_pmu_events_list_num <= 0) || !strcmp(name, "cpu"))
2230 		return PMU_EVENT_SYMBOL_ERR;
2231 
2232 	p.symbol = strdup(name);
2233 	r = bsearch(&p, perf_pmu_events_list,
2234 			(size_t) perf_pmu_events_list_num,
2235 			sizeof(struct perf_pmu_event_symbol), comp_pmu);
2236 	zfree(&p.symbol);
2237 	return r ? r->type : PMU_EVENT_SYMBOL_ERR;
2238 }
2239 
2240 static int parse_events__scanner(const char *str,
2241 				 struct parse_events_state *parse_state)
2242 {
2243 	YY_BUFFER_STATE buffer;
2244 	void *scanner;
2245 	int ret;
2246 
2247 	ret = parse_events_lex_init_extra(parse_state, &scanner);
2248 	if (ret)
2249 		return ret;
2250 
2251 	buffer = parse_events__scan_string(str, scanner);
2252 
2253 #ifdef PARSER_DEBUG
2254 	parse_events_debug = 1;
2255 	parse_events_set_debug(1, scanner);
2256 #endif
2257 	ret = parse_events_parse(parse_state, scanner);
2258 
2259 	parse_events__flush_buffer(buffer, scanner);
2260 	parse_events__delete_buffer(buffer, scanner);
2261 	parse_events_lex_destroy(scanner);
2262 	return ret;
2263 }
2264 
2265 /*
2266  * parse event config string, return a list of event terms.
2267  */
2268 int parse_events_terms(struct list_head *terms, const char *str)
2269 {
2270 	struct parse_events_state parse_state = {
2271 		.terms  = NULL,
2272 		.stoken = PE_START_TERMS,
2273 	};
2274 	int ret;
2275 
2276 	ret = parse_events__scanner(str, &parse_state);
2277 	perf_pmu__parse_cleanup();
2278 
2279 	if (!ret) {
2280 		list_splice(parse_state.terms, terms);
2281 		zfree(&parse_state.terms);
2282 		return 0;
2283 	}
2284 
2285 	parse_events_terms__delete(parse_state.terms);
2286 	return ret;
2287 }
2288 
2289 static int parse_events__with_hybrid_pmu(struct parse_events_state *parse_state,
2290 					 const char *str, char *pmu_name,
2291 					 struct list_head *list)
2292 {
2293 	struct parse_events_state ps = {
2294 		.list            = LIST_HEAD_INIT(ps.list),
2295 		.stoken          = PE_START_EVENTS,
2296 		.hybrid_pmu_name = pmu_name,
2297 		.idx             = parse_state->idx,
2298 	};
2299 	int ret;
2300 
2301 	ret = parse_events__scanner(str, &ps);
2302 	perf_pmu__parse_cleanup();
2303 
2304 	if (!ret) {
2305 		if (!list_empty(&ps.list)) {
2306 			list_splice(&ps.list, list);
2307 			parse_state->idx = ps.idx;
2308 			return 0;
2309 		} else
2310 			return -1;
2311 	}
2312 
2313 	return ret;
2314 }
2315 
2316 int __parse_events(struct evlist *evlist, const char *str,
2317 		   struct parse_events_error *err, struct perf_pmu *fake_pmu)
2318 {
2319 	struct parse_events_state parse_state = {
2320 		.list	  = LIST_HEAD_INIT(parse_state.list),
2321 		.idx	  = evlist->core.nr_entries,
2322 		.error	  = err,
2323 		.evlist	  = evlist,
2324 		.stoken	  = PE_START_EVENTS,
2325 		.fake_pmu = fake_pmu,
2326 	};
2327 	int ret;
2328 
2329 	ret = parse_events__scanner(str, &parse_state);
2330 	perf_pmu__parse_cleanup();
2331 
2332 	if (!ret && list_empty(&parse_state.list)) {
2333 		WARN_ONCE(true, "WARNING: event parser found nothing\n");
2334 		return -1;
2335 	}
2336 
2337 	/*
2338 	 * Add list to the evlist even with errors to allow callers to clean up.
2339 	 */
2340 	evlist__splice_list_tail(evlist, &parse_state.list);
2341 
2342 	if (!ret) {
2343 		struct evsel *last;
2344 
2345 		evlist->core.nr_groups += parse_state.nr_groups;
2346 		last = evlist__last(evlist);
2347 		last->cmdline_group_boundary = true;
2348 
2349 		return 0;
2350 	}
2351 
2352 	/*
2353 	 * There are 2 users - builtin-record and builtin-test objects.
2354 	 * Both call evlist__delete in case of error, so we dont
2355 	 * need to bother.
2356 	 */
2357 	return ret;
2358 }
2359 
2360 void parse_events_error__init(struct parse_events_error *err)
2361 {
2362 	bzero(err, sizeof(*err));
2363 }
2364 
2365 void parse_events_error__exit(struct parse_events_error *err)
2366 {
2367 	zfree(&err->str);
2368 	zfree(&err->help);
2369 	zfree(&err->first_str);
2370 	zfree(&err->first_help);
2371 }
2372 
2373 void parse_events_error__handle(struct parse_events_error *err, int idx,
2374 				char *str, char *help)
2375 {
2376 	if (WARN(!str, "WARNING: failed to provide error string\n")) {
2377 		free(help);
2378 		return;
2379 	}
2380 	switch (err->num_errors) {
2381 	case 0:
2382 		err->idx = idx;
2383 		err->str = str;
2384 		err->help = help;
2385 		break;
2386 	case 1:
2387 		err->first_idx = err->idx;
2388 		err->idx = idx;
2389 		err->first_str = err->str;
2390 		err->str = str;
2391 		err->first_help = err->help;
2392 		err->help = help;
2393 		break;
2394 	default:
2395 		pr_debug("Multiple errors dropping message: %s (%s)\n",
2396 			err->str, err->help);
2397 		free(err->str);
2398 		err->str = str;
2399 		free(err->help);
2400 		err->help = help;
2401 		break;
2402 	}
2403 	err->num_errors++;
2404 }
2405 
2406 #define MAX_WIDTH 1000
2407 static int get_term_width(void)
2408 {
2409 	struct winsize ws;
2410 
2411 	get_term_dimensions(&ws);
2412 	return ws.ws_col > MAX_WIDTH ? MAX_WIDTH : ws.ws_col;
2413 }
2414 
2415 static void __parse_events_error__print(int err_idx, const char *err_str,
2416 					const char *err_help, const char *event)
2417 {
2418 	const char *str = "invalid or unsupported event: ";
2419 	char _buf[MAX_WIDTH];
2420 	char *buf = (char *) event;
2421 	int idx = 0;
2422 	if (err_str) {
2423 		/* -2 for extra '' in the final fprintf */
2424 		int width       = get_term_width() - 2;
2425 		int len_event   = strlen(event);
2426 		int len_str, max_len, cut = 0;
2427 
2428 		/*
2429 		 * Maximum error index indent, we will cut
2430 		 * the event string if it's bigger.
2431 		 */
2432 		int max_err_idx = 13;
2433 
2434 		/*
2435 		 * Let's be specific with the message when
2436 		 * we have the precise error.
2437 		 */
2438 		str     = "event syntax error: ";
2439 		len_str = strlen(str);
2440 		max_len = width - len_str;
2441 
2442 		buf = _buf;
2443 
2444 		/* We're cutting from the beginning. */
2445 		if (err_idx > max_err_idx)
2446 			cut = err_idx - max_err_idx;
2447 
2448 		strncpy(buf, event + cut, max_len);
2449 
2450 		/* Mark cut parts with '..' on both sides. */
2451 		if (cut)
2452 			buf[0] = buf[1] = '.';
2453 
2454 		if ((len_event - cut) > max_len) {
2455 			buf[max_len - 1] = buf[max_len - 2] = '.';
2456 			buf[max_len] = 0;
2457 		}
2458 
2459 		idx = len_str + err_idx - cut;
2460 	}
2461 
2462 	fprintf(stderr, "%s'%s'\n", str, buf);
2463 	if (idx) {
2464 		fprintf(stderr, "%*s\\___ %s\n", idx + 1, "", err_str);
2465 		if (err_help)
2466 			fprintf(stderr, "\n%s\n", err_help);
2467 	}
2468 }
2469 
2470 void parse_events_error__print(struct parse_events_error *err,
2471 			       const char *event)
2472 {
2473 	if (!err->num_errors)
2474 		return;
2475 
2476 	__parse_events_error__print(err->idx, err->str, err->help, event);
2477 
2478 	if (err->num_errors > 1) {
2479 		fputs("\nInitial error:\n", stderr);
2480 		__parse_events_error__print(err->first_idx, err->first_str,
2481 					err->first_help, event);
2482 	}
2483 }
2484 
2485 #undef MAX_WIDTH
2486 
2487 int parse_events_option(const struct option *opt, const char *str,
2488 			int unset __maybe_unused)
2489 {
2490 	struct evlist *evlist = *(struct evlist **)opt->value;
2491 	struct parse_events_error err;
2492 	int ret;
2493 
2494 	parse_events_error__init(&err);
2495 	ret = parse_events(evlist, str, &err);
2496 
2497 	if (ret) {
2498 		parse_events_error__print(&err, str);
2499 		fprintf(stderr, "Run 'perf list' for a list of valid events\n");
2500 	}
2501 	parse_events_error__exit(&err);
2502 
2503 	return ret;
2504 }
2505 
2506 int parse_events_option_new_evlist(const struct option *opt, const char *str, int unset)
2507 {
2508 	struct evlist **evlistp = opt->value;
2509 	int ret;
2510 
2511 	if (*evlistp == NULL) {
2512 		*evlistp = evlist__new();
2513 
2514 		if (*evlistp == NULL) {
2515 			fprintf(stderr, "Not enough memory to create evlist\n");
2516 			return -1;
2517 		}
2518 	}
2519 
2520 	ret = parse_events_option(opt, str, unset);
2521 	if (ret) {
2522 		evlist__delete(*evlistp);
2523 		*evlistp = NULL;
2524 	}
2525 
2526 	return ret;
2527 }
2528 
2529 static int
2530 foreach_evsel_in_last_glob(struct evlist *evlist,
2531 			   int (*func)(struct evsel *evsel,
2532 				       const void *arg),
2533 			   const void *arg)
2534 {
2535 	struct evsel *last = NULL;
2536 	int err;
2537 
2538 	/*
2539 	 * Don't return when list_empty, give func a chance to report
2540 	 * error when it found last == NULL.
2541 	 *
2542 	 * So no need to WARN here, let *func do this.
2543 	 */
2544 	if (evlist->core.nr_entries > 0)
2545 		last = evlist__last(evlist);
2546 
2547 	do {
2548 		err = (*func)(last, arg);
2549 		if (err)
2550 			return -1;
2551 		if (!last)
2552 			return 0;
2553 
2554 		if (last->core.node.prev == &evlist->core.entries)
2555 			return 0;
2556 		last = list_entry(last->core.node.prev, struct evsel, core.node);
2557 	} while (!last->cmdline_group_boundary);
2558 
2559 	return 0;
2560 }
2561 
2562 static int set_filter(struct evsel *evsel, const void *arg)
2563 {
2564 	const char *str = arg;
2565 	bool found = false;
2566 	int nr_addr_filters = 0;
2567 	struct perf_pmu *pmu = NULL;
2568 
2569 	if (evsel == NULL) {
2570 		fprintf(stderr,
2571 			"--filter option should follow a -e tracepoint or HW tracer option\n");
2572 		return -1;
2573 	}
2574 
2575 	if (evsel->core.attr.type == PERF_TYPE_TRACEPOINT) {
2576 		if (evsel__append_tp_filter(evsel, str) < 0) {
2577 			fprintf(stderr,
2578 				"not enough memory to hold filter string\n");
2579 			return -1;
2580 		}
2581 
2582 		return 0;
2583 	}
2584 
2585 	while ((pmu = perf_pmu__scan(pmu)) != NULL)
2586 		if (pmu->type == evsel->core.attr.type) {
2587 			found = true;
2588 			break;
2589 		}
2590 
2591 	if (found)
2592 		perf_pmu__scan_file(pmu, "nr_addr_filters",
2593 				    "%d", &nr_addr_filters);
2594 
2595 	if (!nr_addr_filters) {
2596 		fprintf(stderr,
2597 			"This CPU does not support address filtering\n");
2598 		return -1;
2599 	}
2600 
2601 	if (evsel__append_addr_filter(evsel, str) < 0) {
2602 		fprintf(stderr,
2603 			"not enough memory to hold filter string\n");
2604 		return -1;
2605 	}
2606 
2607 	return 0;
2608 }
2609 
2610 int parse_filter(const struct option *opt, const char *str,
2611 		 int unset __maybe_unused)
2612 {
2613 	struct evlist *evlist = *(struct evlist **)opt->value;
2614 
2615 	return foreach_evsel_in_last_glob(evlist, set_filter,
2616 					  (const void *)str);
2617 }
2618 
2619 static int add_exclude_perf_filter(struct evsel *evsel,
2620 				   const void *arg __maybe_unused)
2621 {
2622 	char new_filter[64];
2623 
2624 	if (evsel == NULL || evsel->core.attr.type != PERF_TYPE_TRACEPOINT) {
2625 		fprintf(stderr,
2626 			"--exclude-perf option should follow a -e tracepoint option\n");
2627 		return -1;
2628 	}
2629 
2630 	snprintf(new_filter, sizeof(new_filter), "common_pid != %d", getpid());
2631 
2632 	if (evsel__append_tp_filter(evsel, new_filter) < 0) {
2633 		fprintf(stderr,
2634 			"not enough memory to hold filter string\n");
2635 		return -1;
2636 	}
2637 
2638 	return 0;
2639 }
2640 
2641 int exclude_perf(const struct option *opt,
2642 		 const char *arg __maybe_unused,
2643 		 int unset __maybe_unused)
2644 {
2645 	struct evlist *evlist = *(struct evlist **)opt->value;
2646 
2647 	return foreach_evsel_in_last_glob(evlist, add_exclude_perf_filter,
2648 					  NULL);
2649 }
2650 
2651 static const char * const event_type_descriptors[] = {
2652 	"Hardware event",
2653 	"Software event",
2654 	"Tracepoint event",
2655 	"Hardware cache event",
2656 	"Raw hardware event descriptor",
2657 	"Hardware breakpoint",
2658 };
2659 
2660 static int cmp_string(const void *a, const void *b)
2661 {
2662 	const char * const *as = a;
2663 	const char * const *bs = b;
2664 
2665 	return strcmp(*as, *bs);
2666 }
2667 
2668 /*
2669  * Print the events from <debugfs_mount_point>/tracing/events
2670  */
2671 
2672 void print_tracepoint_events(const char *subsys_glob, const char *event_glob,
2673 			     bool name_only)
2674 {
2675 	DIR *sys_dir, *evt_dir;
2676 	struct dirent *sys_dirent, *evt_dirent;
2677 	char evt_path[MAXPATHLEN];
2678 	char *dir_path;
2679 	char **evt_list = NULL;
2680 	unsigned int evt_i = 0, evt_num = 0;
2681 	bool evt_num_known = false;
2682 
2683 restart:
2684 	sys_dir = tracing_events__opendir();
2685 	if (!sys_dir)
2686 		return;
2687 
2688 	if (evt_num_known) {
2689 		evt_list = zalloc(sizeof(char *) * evt_num);
2690 		if (!evt_list)
2691 			goto out_close_sys_dir;
2692 	}
2693 
2694 	for_each_subsystem(sys_dir, sys_dirent) {
2695 		if (subsys_glob != NULL &&
2696 		    !strglobmatch(sys_dirent->d_name, subsys_glob))
2697 			continue;
2698 
2699 		dir_path = get_events_file(sys_dirent->d_name);
2700 		if (!dir_path)
2701 			continue;
2702 		evt_dir = opendir(dir_path);
2703 		if (!evt_dir)
2704 			goto next;
2705 
2706 		for_each_event(dir_path, evt_dir, evt_dirent) {
2707 			if (event_glob != NULL &&
2708 			    !strglobmatch(evt_dirent->d_name, event_glob))
2709 				continue;
2710 
2711 			if (!evt_num_known) {
2712 				evt_num++;
2713 				continue;
2714 			}
2715 
2716 			snprintf(evt_path, MAXPATHLEN, "%s:%s",
2717 				 sys_dirent->d_name, evt_dirent->d_name);
2718 
2719 			evt_list[evt_i] = strdup(evt_path);
2720 			if (evt_list[evt_i] == NULL) {
2721 				put_events_file(dir_path);
2722 				goto out_close_evt_dir;
2723 			}
2724 			evt_i++;
2725 		}
2726 		closedir(evt_dir);
2727 next:
2728 		put_events_file(dir_path);
2729 	}
2730 	closedir(sys_dir);
2731 
2732 	if (!evt_num_known) {
2733 		evt_num_known = true;
2734 		goto restart;
2735 	}
2736 	qsort(evt_list, evt_num, sizeof(char *), cmp_string);
2737 	evt_i = 0;
2738 	while (evt_i < evt_num) {
2739 		if (name_only) {
2740 			printf("%s ", evt_list[evt_i++]);
2741 			continue;
2742 		}
2743 		printf("  %-50s [%s]\n", evt_list[evt_i++],
2744 				event_type_descriptors[PERF_TYPE_TRACEPOINT]);
2745 	}
2746 	if (evt_num && pager_in_use())
2747 		printf("\n");
2748 
2749 out_free:
2750 	evt_num = evt_i;
2751 	for (evt_i = 0; evt_i < evt_num; evt_i++)
2752 		zfree(&evt_list[evt_i]);
2753 	zfree(&evt_list);
2754 	return;
2755 
2756 out_close_evt_dir:
2757 	closedir(evt_dir);
2758 out_close_sys_dir:
2759 	closedir(sys_dir);
2760 
2761 	printf("FATAL: not enough memory to print %s\n",
2762 			event_type_descriptors[PERF_TYPE_TRACEPOINT]);
2763 	if (evt_list)
2764 		goto out_free;
2765 }
2766 
2767 /*
2768  * Check whether event is in <debugfs_mount_point>/tracing/events
2769  */
2770 
2771 int is_valid_tracepoint(const char *event_string)
2772 {
2773 	DIR *sys_dir, *evt_dir;
2774 	struct dirent *sys_dirent, *evt_dirent;
2775 	char evt_path[MAXPATHLEN];
2776 	char *dir_path;
2777 
2778 	sys_dir = tracing_events__opendir();
2779 	if (!sys_dir)
2780 		return 0;
2781 
2782 	for_each_subsystem(sys_dir, sys_dirent) {
2783 		dir_path = get_events_file(sys_dirent->d_name);
2784 		if (!dir_path)
2785 			continue;
2786 		evt_dir = opendir(dir_path);
2787 		if (!evt_dir)
2788 			goto next;
2789 
2790 		for_each_event(dir_path, evt_dir, evt_dirent) {
2791 			snprintf(evt_path, MAXPATHLEN, "%s:%s",
2792 				 sys_dirent->d_name, evt_dirent->d_name);
2793 			if (!strcmp(evt_path, event_string)) {
2794 				closedir(evt_dir);
2795 				closedir(sys_dir);
2796 				return 1;
2797 			}
2798 		}
2799 		closedir(evt_dir);
2800 next:
2801 		put_events_file(dir_path);
2802 	}
2803 	closedir(sys_dir);
2804 	return 0;
2805 }
2806 
2807 static bool is_event_supported(u8 type, u64 config)
2808 {
2809 	bool ret = true;
2810 	int open_return;
2811 	struct evsel *evsel;
2812 	struct perf_event_attr attr = {
2813 		.type = type,
2814 		.config = config,
2815 		.disabled = 1,
2816 	};
2817 	struct perf_thread_map *tmap = thread_map__new_by_tid(0);
2818 
2819 	if (tmap == NULL)
2820 		return false;
2821 
2822 	evsel = evsel__new(&attr);
2823 	if (evsel) {
2824 		open_return = evsel__open(evsel, NULL, tmap);
2825 		ret = open_return >= 0;
2826 
2827 		if (open_return == -EACCES) {
2828 			/*
2829 			 * This happens if the paranoid value
2830 			 * /proc/sys/kernel/perf_event_paranoid is set to 2
2831 			 * Re-run with exclude_kernel set; we don't do that
2832 			 * by default as some ARM machines do not support it.
2833 			 *
2834 			 */
2835 			evsel->core.attr.exclude_kernel = 1;
2836 			ret = evsel__open(evsel, NULL, tmap) >= 0;
2837 		}
2838 		evsel__delete(evsel);
2839 	}
2840 
2841 	perf_thread_map__put(tmap);
2842 	return ret;
2843 }
2844 
2845 void print_sdt_events(const char *subsys_glob, const char *event_glob,
2846 		      bool name_only)
2847 {
2848 	struct probe_cache *pcache;
2849 	struct probe_cache_entry *ent;
2850 	struct strlist *bidlist, *sdtlist;
2851 	struct strlist_config cfg = {.dont_dupstr = true};
2852 	struct str_node *nd, *nd2;
2853 	char *buf, *path, *ptr = NULL;
2854 	bool show_detail = false;
2855 	int ret;
2856 
2857 	sdtlist = strlist__new(NULL, &cfg);
2858 	if (!sdtlist) {
2859 		pr_debug("Failed to allocate new strlist for SDT\n");
2860 		return;
2861 	}
2862 	bidlist = build_id_cache__list_all(true);
2863 	if (!bidlist) {
2864 		pr_debug("Failed to get buildids: %d\n", errno);
2865 		return;
2866 	}
2867 	strlist__for_each_entry(nd, bidlist) {
2868 		pcache = probe_cache__new(nd->s, NULL);
2869 		if (!pcache)
2870 			continue;
2871 		list_for_each_entry(ent, &pcache->entries, node) {
2872 			if (!ent->sdt)
2873 				continue;
2874 			if (subsys_glob &&
2875 			    !strglobmatch(ent->pev.group, subsys_glob))
2876 				continue;
2877 			if (event_glob &&
2878 			    !strglobmatch(ent->pev.event, event_glob))
2879 				continue;
2880 			ret = asprintf(&buf, "%s:%s@%s", ent->pev.group,
2881 					ent->pev.event, nd->s);
2882 			if (ret > 0)
2883 				strlist__add(sdtlist, buf);
2884 		}
2885 		probe_cache__delete(pcache);
2886 	}
2887 	strlist__delete(bidlist);
2888 
2889 	strlist__for_each_entry(nd, sdtlist) {
2890 		buf = strchr(nd->s, '@');
2891 		if (buf)
2892 			*(buf++) = '\0';
2893 		if (name_only) {
2894 			printf("%s ", nd->s);
2895 			continue;
2896 		}
2897 		nd2 = strlist__next(nd);
2898 		if (nd2) {
2899 			ptr = strchr(nd2->s, '@');
2900 			if (ptr)
2901 				*ptr = '\0';
2902 			if (strcmp(nd->s, nd2->s) == 0)
2903 				show_detail = true;
2904 		}
2905 		if (show_detail) {
2906 			path = build_id_cache__origname(buf);
2907 			ret = asprintf(&buf, "%s@%s(%.12s)", nd->s, path, buf);
2908 			if (ret > 0) {
2909 				printf("  %-50s [%s]\n", buf, "SDT event");
2910 				free(buf);
2911 			}
2912 			free(path);
2913 		} else
2914 			printf("  %-50s [%s]\n", nd->s, "SDT event");
2915 		if (nd2) {
2916 			if (strcmp(nd->s, nd2->s) != 0)
2917 				show_detail = false;
2918 			if (ptr)
2919 				*ptr = '@';
2920 		}
2921 	}
2922 	strlist__delete(sdtlist);
2923 }
2924 
2925 int print_hwcache_events(const char *event_glob, bool name_only)
2926 {
2927 	unsigned int type, op, i, evt_i = 0, evt_num = 0, npmus = 0;
2928 	char name[64], new_name[128];
2929 	char **evt_list = NULL, **evt_pmus = NULL;
2930 	bool evt_num_known = false;
2931 	struct perf_pmu *pmu = NULL;
2932 
2933 	if (perf_pmu__has_hybrid()) {
2934 		npmus = perf_pmu__hybrid_pmu_num();
2935 		evt_pmus = zalloc(sizeof(char *) * npmus);
2936 		if (!evt_pmus)
2937 			goto out_enomem;
2938 	}
2939 
2940 restart:
2941 	if (evt_num_known) {
2942 		evt_list = zalloc(sizeof(char *) * evt_num);
2943 		if (!evt_list)
2944 			goto out_enomem;
2945 	}
2946 
2947 	for (type = 0; type < PERF_COUNT_HW_CACHE_MAX; type++) {
2948 		for (op = 0; op < PERF_COUNT_HW_CACHE_OP_MAX; op++) {
2949 			/* skip invalid cache type */
2950 			if (!evsel__is_cache_op_valid(type, op))
2951 				continue;
2952 
2953 			for (i = 0; i < PERF_COUNT_HW_CACHE_RESULT_MAX; i++) {
2954 				unsigned int hybrid_supported = 0, j;
2955 				bool supported;
2956 
2957 				__evsel__hw_cache_type_op_res_name(type, op, i, name, sizeof(name));
2958 				if (event_glob != NULL && !strglobmatch(name, event_glob))
2959 					continue;
2960 
2961 				if (!perf_pmu__has_hybrid()) {
2962 					if (!is_event_supported(PERF_TYPE_HW_CACHE,
2963 								type | (op << 8) | (i << 16))) {
2964 						continue;
2965 					}
2966 				} else {
2967 					perf_pmu__for_each_hybrid_pmu(pmu) {
2968 						if (!evt_num_known) {
2969 							evt_num++;
2970 							continue;
2971 						}
2972 
2973 						supported = is_event_supported(
2974 									PERF_TYPE_HW_CACHE,
2975 									type | (op << 8) | (i << 16) |
2976 									((__u64)pmu->type << PERF_PMU_TYPE_SHIFT));
2977 						if (supported) {
2978 							snprintf(new_name, sizeof(new_name), "%s/%s/",
2979 								 pmu->name, name);
2980 							evt_pmus[hybrid_supported] = strdup(new_name);
2981 							hybrid_supported++;
2982 						}
2983 					}
2984 
2985 					if (hybrid_supported == 0)
2986 						continue;
2987 				}
2988 
2989 				if (!evt_num_known) {
2990 					evt_num++;
2991 					continue;
2992 				}
2993 
2994 				if ((hybrid_supported == 0) ||
2995 				    (hybrid_supported == npmus)) {
2996 					evt_list[evt_i] = strdup(name);
2997 					if (npmus > 0) {
2998 						for (j = 0; j < npmus; j++)
2999 							zfree(&evt_pmus[j]);
3000 					}
3001 				} else {
3002 					for (j = 0; j < hybrid_supported; j++) {
3003 						evt_list[evt_i++] = evt_pmus[j];
3004 						evt_pmus[j] = NULL;
3005 					}
3006 					continue;
3007 				}
3008 
3009 				if (evt_list[evt_i] == NULL)
3010 					goto out_enomem;
3011 				evt_i++;
3012 			}
3013 		}
3014 	}
3015 
3016 	if (!evt_num_known) {
3017 		evt_num_known = true;
3018 		goto restart;
3019 	}
3020 
3021 	for (evt_i = 0; evt_i < evt_num; evt_i++) {
3022 		if (!evt_list[evt_i])
3023 			break;
3024 	}
3025 
3026 	evt_num = evt_i;
3027 	qsort(evt_list, evt_num, sizeof(char *), cmp_string);
3028 	evt_i = 0;
3029 	while (evt_i < evt_num) {
3030 		if (name_only) {
3031 			printf("%s ", evt_list[evt_i++]);
3032 			continue;
3033 		}
3034 		printf("  %-50s [%s]\n", evt_list[evt_i++],
3035 				event_type_descriptors[PERF_TYPE_HW_CACHE]);
3036 	}
3037 	if (evt_num && pager_in_use())
3038 		printf("\n");
3039 
3040 out_free:
3041 	evt_num = evt_i;
3042 	for (evt_i = 0; evt_i < evt_num; evt_i++)
3043 		zfree(&evt_list[evt_i]);
3044 	zfree(&evt_list);
3045 
3046 	for (evt_i = 0; evt_i < npmus; evt_i++)
3047 		zfree(&evt_pmus[evt_i]);
3048 	zfree(&evt_pmus);
3049 	return evt_num;
3050 
3051 out_enomem:
3052 	printf("FATAL: not enough memory to print %s\n", event_type_descriptors[PERF_TYPE_HW_CACHE]);
3053 	if (evt_list)
3054 		goto out_free;
3055 	return evt_num;
3056 }
3057 
3058 static void print_tool_event(const char *name, const char *event_glob,
3059 			     bool name_only)
3060 {
3061 	if (event_glob && !strglobmatch(name, event_glob))
3062 		return;
3063 	if (name_only)
3064 		printf("%s ", name);
3065 	else
3066 		printf("  %-50s [%s]\n", name, "Tool event");
3067 
3068 }
3069 
3070 void print_tool_events(const char *event_glob, bool name_only)
3071 {
3072 	print_tool_event("duration_time", event_glob, name_only);
3073 	if (pager_in_use())
3074 		printf("\n");
3075 }
3076 
3077 void print_symbol_events(const char *event_glob, unsigned type,
3078 				struct event_symbol *syms, unsigned max,
3079 				bool name_only)
3080 {
3081 	unsigned int i, evt_i = 0, evt_num = 0;
3082 	char name[MAX_NAME_LEN];
3083 	char **evt_list = NULL;
3084 	bool evt_num_known = false;
3085 
3086 restart:
3087 	if (evt_num_known) {
3088 		evt_list = zalloc(sizeof(char *) * evt_num);
3089 		if (!evt_list)
3090 			goto out_enomem;
3091 		syms -= max;
3092 	}
3093 
3094 	for (i = 0; i < max; i++, syms++) {
3095 		/*
3096 		 * New attr.config still not supported here, the latest
3097 		 * example was PERF_COUNT_SW_CGROUP_SWITCHES
3098 		 */
3099 		if (syms->symbol == NULL)
3100 			continue;
3101 
3102 		if (event_glob != NULL && !(strglobmatch(syms->symbol, event_glob) ||
3103 		      (syms->alias && strglobmatch(syms->alias, event_glob))))
3104 			continue;
3105 
3106 		if (!is_event_supported(type, i))
3107 			continue;
3108 
3109 		if (!evt_num_known) {
3110 			evt_num++;
3111 			continue;
3112 		}
3113 
3114 		if (!name_only && strlen(syms->alias))
3115 			snprintf(name, MAX_NAME_LEN, "%s OR %s", syms->symbol, syms->alias);
3116 		else
3117 			strlcpy(name, syms->symbol, MAX_NAME_LEN);
3118 
3119 		evt_list[evt_i] = strdup(name);
3120 		if (evt_list[evt_i] == NULL)
3121 			goto out_enomem;
3122 		evt_i++;
3123 	}
3124 
3125 	if (!evt_num_known) {
3126 		evt_num_known = true;
3127 		goto restart;
3128 	}
3129 	qsort(evt_list, evt_num, sizeof(char *), cmp_string);
3130 	evt_i = 0;
3131 	while (evt_i < evt_num) {
3132 		if (name_only) {
3133 			printf("%s ", evt_list[evt_i++]);
3134 			continue;
3135 		}
3136 		printf("  %-50s [%s]\n", evt_list[evt_i++], event_type_descriptors[type]);
3137 	}
3138 	if (evt_num && pager_in_use())
3139 		printf("\n");
3140 
3141 out_free:
3142 	evt_num = evt_i;
3143 	for (evt_i = 0; evt_i < evt_num; evt_i++)
3144 		zfree(&evt_list[evt_i]);
3145 	zfree(&evt_list);
3146 	return;
3147 
3148 out_enomem:
3149 	printf("FATAL: not enough memory to print %s\n", event_type_descriptors[type]);
3150 	if (evt_list)
3151 		goto out_free;
3152 }
3153 
3154 /*
3155  * Print the help text for the event symbols:
3156  */
3157 void print_events(const char *event_glob, bool name_only, bool quiet_flag,
3158 			bool long_desc, bool details_flag, bool deprecated,
3159 			const char *pmu_name)
3160 {
3161 	print_symbol_events(event_glob, PERF_TYPE_HARDWARE,
3162 			    event_symbols_hw, PERF_COUNT_HW_MAX, name_only);
3163 
3164 	print_symbol_events(event_glob, PERF_TYPE_SOFTWARE,
3165 			    event_symbols_sw, PERF_COUNT_SW_MAX, name_only);
3166 	print_tool_events(event_glob, name_only);
3167 
3168 	print_hwcache_events(event_glob, name_only);
3169 
3170 	print_pmu_events(event_glob, name_only, quiet_flag, long_desc,
3171 			details_flag, deprecated, pmu_name);
3172 
3173 	if (event_glob != NULL)
3174 		return;
3175 
3176 	if (!name_only) {
3177 		printf("  %-50s [%s]\n",
3178 		       "rNNN",
3179 		       event_type_descriptors[PERF_TYPE_RAW]);
3180 		printf("  %-50s [%s]\n",
3181 		       "cpu/t1=v1[,t2=v2,t3 ...]/modifier",
3182 		       event_type_descriptors[PERF_TYPE_RAW]);
3183 		if (pager_in_use())
3184 			printf("   (see 'man perf-list' on how to encode it)\n\n");
3185 
3186 		printf("  %-50s [%s]\n",
3187 		       "mem:<addr>[/len][:access]",
3188 			event_type_descriptors[PERF_TYPE_BREAKPOINT]);
3189 		if (pager_in_use())
3190 			printf("\n");
3191 	}
3192 
3193 	print_tracepoint_events(NULL, NULL, name_only);
3194 
3195 	print_sdt_events(NULL, NULL, name_only);
3196 
3197 	metricgroup__print(true, true, NULL, name_only, details_flag,
3198 			   pmu_name);
3199 
3200 	print_libpfm_events(name_only, long_desc);
3201 }
3202 
3203 int parse_events__is_hardcoded_term(struct parse_events_term *term)
3204 {
3205 	return term->type_term != PARSE_EVENTS__TERM_TYPE_USER;
3206 }
3207 
3208 static int new_term(struct parse_events_term **_term,
3209 		    struct parse_events_term *temp,
3210 		    char *str, u64 num)
3211 {
3212 	struct parse_events_term *term;
3213 
3214 	term = malloc(sizeof(*term));
3215 	if (!term)
3216 		return -ENOMEM;
3217 
3218 	*term = *temp;
3219 	INIT_LIST_HEAD(&term->list);
3220 	term->weak = false;
3221 
3222 	switch (term->type_val) {
3223 	case PARSE_EVENTS__TERM_TYPE_NUM:
3224 		term->val.num = num;
3225 		break;
3226 	case PARSE_EVENTS__TERM_TYPE_STR:
3227 		term->val.str = str;
3228 		break;
3229 	default:
3230 		free(term);
3231 		return -EINVAL;
3232 	}
3233 
3234 	*_term = term;
3235 	return 0;
3236 }
3237 
3238 int parse_events_term__num(struct parse_events_term **term,
3239 			   int type_term, char *config, u64 num,
3240 			   bool no_value,
3241 			   void *loc_term_, void *loc_val_)
3242 {
3243 	YYLTYPE *loc_term = loc_term_;
3244 	YYLTYPE *loc_val = loc_val_;
3245 
3246 	struct parse_events_term temp = {
3247 		.type_val  = PARSE_EVENTS__TERM_TYPE_NUM,
3248 		.type_term = type_term,
3249 		.config    = config ? : strdup(config_term_names[type_term]),
3250 		.no_value  = no_value,
3251 		.err_term  = loc_term ? loc_term->first_column : 0,
3252 		.err_val   = loc_val  ? loc_val->first_column  : 0,
3253 	};
3254 
3255 	return new_term(term, &temp, NULL, num);
3256 }
3257 
3258 int parse_events_term__str(struct parse_events_term **term,
3259 			   int type_term, char *config, char *str,
3260 			   void *loc_term_, void *loc_val_)
3261 {
3262 	YYLTYPE *loc_term = loc_term_;
3263 	YYLTYPE *loc_val = loc_val_;
3264 
3265 	struct parse_events_term temp = {
3266 		.type_val  = PARSE_EVENTS__TERM_TYPE_STR,
3267 		.type_term = type_term,
3268 		.config    = config,
3269 		.err_term  = loc_term ? loc_term->first_column : 0,
3270 		.err_val   = loc_val  ? loc_val->first_column  : 0,
3271 	};
3272 
3273 	return new_term(term, &temp, str, 0);
3274 }
3275 
3276 int parse_events_term__sym_hw(struct parse_events_term **term,
3277 			      char *config, unsigned idx)
3278 {
3279 	struct event_symbol *sym;
3280 	char *str;
3281 	struct parse_events_term temp = {
3282 		.type_val  = PARSE_EVENTS__TERM_TYPE_STR,
3283 		.type_term = PARSE_EVENTS__TERM_TYPE_USER,
3284 		.config    = config,
3285 	};
3286 
3287 	if (!temp.config) {
3288 		temp.config = strdup("event");
3289 		if (!temp.config)
3290 			return -ENOMEM;
3291 	}
3292 	BUG_ON(idx >= PERF_COUNT_HW_MAX);
3293 	sym = &event_symbols_hw[idx];
3294 
3295 	str = strdup(sym->symbol);
3296 	if (!str)
3297 		return -ENOMEM;
3298 	return new_term(term, &temp, str, 0);
3299 }
3300 
3301 int parse_events_term__clone(struct parse_events_term **new,
3302 			     struct parse_events_term *term)
3303 {
3304 	char *str;
3305 	struct parse_events_term temp = {
3306 		.type_val  = term->type_val,
3307 		.type_term = term->type_term,
3308 		.config    = NULL,
3309 		.err_term  = term->err_term,
3310 		.err_val   = term->err_val,
3311 	};
3312 
3313 	if (term->config) {
3314 		temp.config = strdup(term->config);
3315 		if (!temp.config)
3316 			return -ENOMEM;
3317 	}
3318 	if (term->type_val == PARSE_EVENTS__TERM_TYPE_NUM)
3319 		return new_term(new, &temp, NULL, term->val.num);
3320 
3321 	str = strdup(term->val.str);
3322 	if (!str)
3323 		return -ENOMEM;
3324 	return new_term(new, &temp, str, 0);
3325 }
3326 
3327 void parse_events_term__delete(struct parse_events_term *term)
3328 {
3329 	if (term->array.nr_ranges)
3330 		zfree(&term->array.ranges);
3331 
3332 	if (term->type_val != PARSE_EVENTS__TERM_TYPE_NUM)
3333 		zfree(&term->val.str);
3334 
3335 	zfree(&term->config);
3336 	free(term);
3337 }
3338 
3339 int parse_events_copy_term_list(struct list_head *old,
3340 				 struct list_head **new)
3341 {
3342 	struct parse_events_term *term, *n;
3343 	int ret;
3344 
3345 	if (!old) {
3346 		*new = NULL;
3347 		return 0;
3348 	}
3349 
3350 	*new = malloc(sizeof(struct list_head));
3351 	if (!*new)
3352 		return -ENOMEM;
3353 	INIT_LIST_HEAD(*new);
3354 
3355 	list_for_each_entry (term, old, list) {
3356 		ret = parse_events_term__clone(&n, term);
3357 		if (ret)
3358 			return ret;
3359 		list_add_tail(&n->list, *new);
3360 	}
3361 	return 0;
3362 }
3363 
3364 void parse_events_terms__purge(struct list_head *terms)
3365 {
3366 	struct parse_events_term *term, *h;
3367 
3368 	list_for_each_entry_safe(term, h, terms, list) {
3369 		list_del_init(&term->list);
3370 		parse_events_term__delete(term);
3371 	}
3372 }
3373 
3374 void parse_events_terms__delete(struct list_head *terms)
3375 {
3376 	if (!terms)
3377 		return;
3378 	parse_events_terms__purge(terms);
3379 	free(terms);
3380 }
3381 
3382 void parse_events__clear_array(struct parse_events_array *a)
3383 {
3384 	zfree(&a->ranges);
3385 }
3386 
3387 void parse_events_evlist_error(struct parse_events_state *parse_state,
3388 			       int idx, const char *str)
3389 {
3390 	if (!parse_state->error)
3391 		return;
3392 
3393 	parse_events_error__handle(parse_state->error, idx, strdup(str), NULL);
3394 }
3395 
3396 static void config_terms_list(char *buf, size_t buf_sz)
3397 {
3398 	int i;
3399 	bool first = true;
3400 
3401 	buf[0] = '\0';
3402 	for (i = 0; i < __PARSE_EVENTS__TERM_TYPE_NR; i++) {
3403 		const char *name = config_term_names[i];
3404 
3405 		if (!config_term_avail(i, NULL))
3406 			continue;
3407 		if (!name)
3408 			continue;
3409 		if (name[0] == '<')
3410 			continue;
3411 
3412 		if (strlen(buf) + strlen(name) + 2 >= buf_sz)
3413 			return;
3414 
3415 		if (!first)
3416 			strcat(buf, ",");
3417 		else
3418 			first = false;
3419 		strcat(buf, name);
3420 	}
3421 }
3422 
3423 /*
3424  * Return string contains valid config terms of an event.
3425  * @additional_terms: For terms such as PMU sysfs terms.
3426  */
3427 char *parse_events_formats_error_string(char *additional_terms)
3428 {
3429 	char *str;
3430 	/* "no-overwrite" is the longest name */
3431 	char static_terms[__PARSE_EVENTS__TERM_TYPE_NR *
3432 			  (sizeof("no-overwrite") - 1)];
3433 
3434 	config_terms_list(static_terms, sizeof(static_terms));
3435 	/* valid terms */
3436 	if (additional_terms) {
3437 		if (asprintf(&str, "valid terms: %s,%s",
3438 			     additional_terms, static_terms) < 0)
3439 			goto fail;
3440 	} else {
3441 		if (asprintf(&str, "valid terms: %s", static_terms) < 0)
3442 			goto fail;
3443 	}
3444 	return str;
3445 
3446 fail:
3447 	return NULL;
3448 }
3449 
3450 struct evsel *parse_events__add_event_hybrid(struct list_head *list, int *idx,
3451 					     struct perf_event_attr *attr,
3452 					     const char *name,
3453 					     const char *metric_id,
3454 					     struct perf_pmu *pmu,
3455 					     struct list_head *config_terms)
3456 {
3457 	return __add_event(list, idx, attr, /*init_attr=*/true, name, metric_id,
3458 			   pmu, config_terms, /*auto_merge_stats=*/false,
3459 			   /*cpu_list=*/NULL);
3460 }
3461