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