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