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