xref: /linux/tools/perf/util/parse-events.c (revision 4949009eb8d40a441dcddcd96e101e77d31cf1b2)
1 #include <linux/hw_breakpoint.h>
2 #include "util.h"
3 #include "../perf.h"
4 #include "evlist.h"
5 #include "evsel.h"
6 #include "parse-options.h"
7 #include "parse-events.h"
8 #include "exec_cmd.h"
9 #include "string.h"
10 #include "symbol.h"
11 #include "cache.h"
12 #include "header.h"
13 #include "debug.h"
14 #include <api/fs/debugfs.h>
15 #include "parse-events-bison.h"
16 #define YY_EXTRA_TYPE int
17 #include "parse-events-flex.h"
18 #include "pmu.h"
19 #include "thread_map.h"
20 
21 #define MAX_NAME_LEN 100
22 
23 struct event_symbol {
24 	const char	*symbol;
25 	const char	*alias;
26 };
27 
28 #ifdef PARSER_DEBUG
29 extern int parse_events_debug;
30 #endif
31 int parse_events_parse(void *data, void *scanner);
32 
33 static struct perf_pmu_event_symbol *perf_pmu_events_list;
34 /*
35  * The variable indicates the number of supported pmu event symbols.
36  * 0 means not initialized and ready to init
37  * -1 means failed to init, don't try anymore
38  * >0 is the number of supported pmu event symbols
39  */
40 static int perf_pmu_events_list_num;
41 
42 static struct event_symbol event_symbols_hw[PERF_COUNT_HW_MAX] = {
43 	[PERF_COUNT_HW_CPU_CYCLES] = {
44 		.symbol = "cpu-cycles",
45 		.alias  = "cycles",
46 	},
47 	[PERF_COUNT_HW_INSTRUCTIONS] = {
48 		.symbol = "instructions",
49 		.alias  = "",
50 	},
51 	[PERF_COUNT_HW_CACHE_REFERENCES] = {
52 		.symbol = "cache-references",
53 		.alias  = "",
54 	},
55 	[PERF_COUNT_HW_CACHE_MISSES] = {
56 		.symbol = "cache-misses",
57 		.alias  = "",
58 	},
59 	[PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = {
60 		.symbol = "branch-instructions",
61 		.alias  = "branches",
62 	},
63 	[PERF_COUNT_HW_BRANCH_MISSES] = {
64 		.symbol = "branch-misses",
65 		.alias  = "",
66 	},
67 	[PERF_COUNT_HW_BUS_CYCLES] = {
68 		.symbol = "bus-cycles",
69 		.alias  = "",
70 	},
71 	[PERF_COUNT_HW_STALLED_CYCLES_FRONTEND] = {
72 		.symbol = "stalled-cycles-frontend",
73 		.alias  = "idle-cycles-frontend",
74 	},
75 	[PERF_COUNT_HW_STALLED_CYCLES_BACKEND] = {
76 		.symbol = "stalled-cycles-backend",
77 		.alias  = "idle-cycles-backend",
78 	},
79 	[PERF_COUNT_HW_REF_CPU_CYCLES] = {
80 		.symbol = "ref-cycles",
81 		.alias  = "",
82 	},
83 };
84 
85 static struct event_symbol event_symbols_sw[PERF_COUNT_SW_MAX] = {
86 	[PERF_COUNT_SW_CPU_CLOCK] = {
87 		.symbol = "cpu-clock",
88 		.alias  = "",
89 	},
90 	[PERF_COUNT_SW_TASK_CLOCK] = {
91 		.symbol = "task-clock",
92 		.alias  = "",
93 	},
94 	[PERF_COUNT_SW_PAGE_FAULTS] = {
95 		.symbol = "page-faults",
96 		.alias  = "faults",
97 	},
98 	[PERF_COUNT_SW_CONTEXT_SWITCHES] = {
99 		.symbol = "context-switches",
100 		.alias  = "cs",
101 	},
102 	[PERF_COUNT_SW_CPU_MIGRATIONS] = {
103 		.symbol = "cpu-migrations",
104 		.alias  = "migrations",
105 	},
106 	[PERF_COUNT_SW_PAGE_FAULTS_MIN] = {
107 		.symbol = "minor-faults",
108 		.alias  = "",
109 	},
110 	[PERF_COUNT_SW_PAGE_FAULTS_MAJ] = {
111 		.symbol = "major-faults",
112 		.alias  = "",
113 	},
114 	[PERF_COUNT_SW_ALIGNMENT_FAULTS] = {
115 		.symbol = "alignment-faults",
116 		.alias  = "",
117 	},
118 	[PERF_COUNT_SW_EMULATION_FAULTS] = {
119 		.symbol = "emulation-faults",
120 		.alias  = "",
121 	},
122 	[PERF_COUNT_SW_DUMMY] = {
123 		.symbol = "dummy",
124 		.alias  = "",
125 	},
126 };
127 
128 #define __PERF_EVENT_FIELD(config, name) \
129 	((config & PERF_EVENT_##name##_MASK) >> PERF_EVENT_##name##_SHIFT)
130 
131 #define PERF_EVENT_RAW(config)		__PERF_EVENT_FIELD(config, RAW)
132 #define PERF_EVENT_CONFIG(config)	__PERF_EVENT_FIELD(config, CONFIG)
133 #define PERF_EVENT_TYPE(config)		__PERF_EVENT_FIELD(config, TYPE)
134 #define PERF_EVENT_ID(config)		__PERF_EVENT_FIELD(config, EVENT)
135 
136 #define for_each_subsystem(sys_dir, sys_dirent, sys_next)	       \
137 	while (!readdir_r(sys_dir, &sys_dirent, &sys_next) && sys_next)	       \
138 	if (sys_dirent.d_type == DT_DIR &&				       \
139 	   (strcmp(sys_dirent.d_name, ".")) &&				       \
140 	   (strcmp(sys_dirent.d_name, "..")))
141 
142 static int tp_event_has_id(struct dirent *sys_dir, struct dirent *evt_dir)
143 {
144 	char evt_path[MAXPATHLEN];
145 	int fd;
146 
147 	snprintf(evt_path, MAXPATHLEN, "%s/%s/%s/id", tracing_events_path,
148 			sys_dir->d_name, evt_dir->d_name);
149 	fd = open(evt_path, O_RDONLY);
150 	if (fd < 0)
151 		return -EINVAL;
152 	close(fd);
153 
154 	return 0;
155 }
156 
157 #define for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next)	       \
158 	while (!readdir_r(evt_dir, &evt_dirent, &evt_next) && evt_next)        \
159 	if (evt_dirent.d_type == DT_DIR &&				       \
160 	   (strcmp(evt_dirent.d_name, ".")) &&				       \
161 	   (strcmp(evt_dirent.d_name, "..")) &&				       \
162 	   (!tp_event_has_id(&sys_dirent, &evt_dirent)))
163 
164 #define MAX_EVENT_LENGTH 512
165 
166 
167 struct tracepoint_path *tracepoint_id_to_path(u64 config)
168 {
169 	struct tracepoint_path *path = NULL;
170 	DIR *sys_dir, *evt_dir;
171 	struct dirent *sys_next, *evt_next, sys_dirent, evt_dirent;
172 	char id_buf[24];
173 	int fd;
174 	u64 id;
175 	char evt_path[MAXPATHLEN];
176 	char dir_path[MAXPATHLEN];
177 
178 	if (debugfs_valid_mountpoint(tracing_events_path))
179 		return NULL;
180 
181 	sys_dir = opendir(tracing_events_path);
182 	if (!sys_dir)
183 		return NULL;
184 
185 	for_each_subsystem(sys_dir, sys_dirent, sys_next) {
186 
187 		snprintf(dir_path, MAXPATHLEN, "%s/%s", tracing_events_path,
188 			 sys_dirent.d_name);
189 		evt_dir = opendir(dir_path);
190 		if (!evt_dir)
191 			continue;
192 
193 		for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next) {
194 
195 			snprintf(evt_path, MAXPATHLEN, "%s/%s/id", dir_path,
196 				 evt_dirent.d_name);
197 			fd = open(evt_path, O_RDONLY);
198 			if (fd < 0)
199 				continue;
200 			if (read(fd, id_buf, sizeof(id_buf)) < 0) {
201 				close(fd);
202 				continue;
203 			}
204 			close(fd);
205 			id = atoll(id_buf);
206 			if (id == config) {
207 				closedir(evt_dir);
208 				closedir(sys_dir);
209 				path = zalloc(sizeof(*path));
210 				path->system = malloc(MAX_EVENT_LENGTH);
211 				if (!path->system) {
212 					free(path);
213 					return NULL;
214 				}
215 				path->name = malloc(MAX_EVENT_LENGTH);
216 				if (!path->name) {
217 					zfree(&path->system);
218 					free(path);
219 					return NULL;
220 				}
221 				strncpy(path->system, sys_dirent.d_name,
222 					MAX_EVENT_LENGTH);
223 				strncpy(path->name, evt_dirent.d_name,
224 					MAX_EVENT_LENGTH);
225 				return path;
226 			}
227 		}
228 		closedir(evt_dir);
229 	}
230 
231 	closedir(sys_dir);
232 	return NULL;
233 }
234 
235 struct tracepoint_path *tracepoint_name_to_path(const char *name)
236 {
237 	struct tracepoint_path *path = zalloc(sizeof(*path));
238 	char *str = strchr(name, ':');
239 
240 	if (path == NULL || str == NULL) {
241 		free(path);
242 		return NULL;
243 	}
244 
245 	path->system = strndup(name, str - name);
246 	path->name = strdup(str+1);
247 
248 	if (path->system == NULL || path->name == NULL) {
249 		zfree(&path->system);
250 		zfree(&path->name);
251 		free(path);
252 		path = NULL;
253 	}
254 
255 	return path;
256 }
257 
258 const char *event_type(int type)
259 {
260 	switch (type) {
261 	case PERF_TYPE_HARDWARE:
262 		return "hardware";
263 
264 	case PERF_TYPE_SOFTWARE:
265 		return "software";
266 
267 	case PERF_TYPE_TRACEPOINT:
268 		return "tracepoint";
269 
270 	case PERF_TYPE_HW_CACHE:
271 		return "hardware-cache";
272 
273 	default:
274 		break;
275 	}
276 
277 	return "unknown";
278 }
279 
280 
281 
282 static struct perf_evsel *
283 __add_event(struct list_head *list, int *idx,
284 	    struct perf_event_attr *attr,
285 	    char *name, struct cpu_map *cpus)
286 {
287 	struct perf_evsel *evsel;
288 
289 	event_attr_init(attr);
290 
291 	evsel = perf_evsel__new_idx(attr, (*idx)++);
292 	if (!evsel)
293 		return NULL;
294 
295 	evsel->cpus = cpus;
296 	if (name)
297 		evsel->name = strdup(name);
298 	list_add_tail(&evsel->node, list);
299 	return evsel;
300 }
301 
302 static int add_event(struct list_head *list, int *idx,
303 		     struct perf_event_attr *attr, char *name)
304 {
305 	return __add_event(list, idx, attr, name, NULL) ? 0 : -ENOMEM;
306 }
307 
308 static int parse_aliases(char *str, const char *names[][PERF_EVSEL__MAX_ALIASES], int size)
309 {
310 	int i, j;
311 	int n, longest = -1;
312 
313 	for (i = 0; i < size; i++) {
314 		for (j = 0; j < PERF_EVSEL__MAX_ALIASES && names[i][j]; j++) {
315 			n = strlen(names[i][j]);
316 			if (n > longest && !strncasecmp(str, names[i][j], n))
317 				longest = n;
318 		}
319 		if (longest > 0)
320 			return i;
321 	}
322 
323 	return -1;
324 }
325 
326 int parse_events_add_cache(struct list_head *list, int *idx,
327 			   char *type, char *op_result1, char *op_result2)
328 {
329 	struct perf_event_attr attr;
330 	char name[MAX_NAME_LEN];
331 	int cache_type = -1, cache_op = -1, cache_result = -1;
332 	char *op_result[2] = { op_result1, op_result2 };
333 	int i, n;
334 
335 	/*
336 	 * No fallback - if we cannot get a clear cache type
337 	 * then bail out:
338 	 */
339 	cache_type = parse_aliases(type, perf_evsel__hw_cache,
340 				   PERF_COUNT_HW_CACHE_MAX);
341 	if (cache_type == -1)
342 		return -EINVAL;
343 
344 	n = snprintf(name, MAX_NAME_LEN, "%s", type);
345 
346 	for (i = 0; (i < 2) && (op_result[i]); i++) {
347 		char *str = op_result[i];
348 
349 		n += snprintf(name + n, MAX_NAME_LEN - n, "-%s", str);
350 
351 		if (cache_op == -1) {
352 			cache_op = parse_aliases(str, perf_evsel__hw_cache_op,
353 						 PERF_COUNT_HW_CACHE_OP_MAX);
354 			if (cache_op >= 0) {
355 				if (!perf_evsel__is_cache_op_valid(cache_type, cache_op))
356 					return -EINVAL;
357 				continue;
358 			}
359 		}
360 
361 		if (cache_result == -1) {
362 			cache_result = parse_aliases(str, perf_evsel__hw_cache_result,
363 						     PERF_COUNT_HW_CACHE_RESULT_MAX);
364 			if (cache_result >= 0)
365 				continue;
366 		}
367 	}
368 
369 	/*
370 	 * Fall back to reads:
371 	 */
372 	if (cache_op == -1)
373 		cache_op = PERF_COUNT_HW_CACHE_OP_READ;
374 
375 	/*
376 	 * Fall back to accesses:
377 	 */
378 	if (cache_result == -1)
379 		cache_result = PERF_COUNT_HW_CACHE_RESULT_ACCESS;
380 
381 	memset(&attr, 0, sizeof(attr));
382 	attr.config = cache_type | (cache_op << 8) | (cache_result << 16);
383 	attr.type = PERF_TYPE_HW_CACHE;
384 	return add_event(list, idx, &attr, name);
385 }
386 
387 static int add_tracepoint(struct list_head *list, int *idx,
388 			  char *sys_name, char *evt_name)
389 {
390 	struct perf_evsel *evsel;
391 
392 	evsel = perf_evsel__newtp_idx(sys_name, evt_name, (*idx)++);
393 	if (!evsel)
394 		return -ENOMEM;
395 
396 	list_add_tail(&evsel->node, list);
397 
398 	return 0;
399 }
400 
401 static int add_tracepoint_multi_event(struct list_head *list, int *idx,
402 				      char *sys_name, char *evt_name)
403 {
404 	char evt_path[MAXPATHLEN];
405 	struct dirent *evt_ent;
406 	DIR *evt_dir;
407 	int ret = 0;
408 
409 	snprintf(evt_path, MAXPATHLEN, "%s/%s", tracing_events_path, sys_name);
410 	evt_dir = opendir(evt_path);
411 	if (!evt_dir) {
412 		perror("Can't open event dir");
413 		return -1;
414 	}
415 
416 	while (!ret && (evt_ent = readdir(evt_dir))) {
417 		if (!strcmp(evt_ent->d_name, ".")
418 		    || !strcmp(evt_ent->d_name, "..")
419 		    || !strcmp(evt_ent->d_name, "enable")
420 		    || !strcmp(evt_ent->d_name, "filter"))
421 			continue;
422 
423 		if (!strglobmatch(evt_ent->d_name, evt_name))
424 			continue;
425 
426 		ret = add_tracepoint(list, idx, sys_name, evt_ent->d_name);
427 	}
428 
429 	closedir(evt_dir);
430 	return ret;
431 }
432 
433 static int add_tracepoint_event(struct list_head *list, int *idx,
434 				char *sys_name, char *evt_name)
435 {
436 	return strpbrk(evt_name, "*?") ?
437 	       add_tracepoint_multi_event(list, idx, sys_name, evt_name) :
438 	       add_tracepoint(list, idx, sys_name, evt_name);
439 }
440 
441 static int add_tracepoint_multi_sys(struct list_head *list, int *idx,
442 				    char *sys_name, char *evt_name)
443 {
444 	struct dirent *events_ent;
445 	DIR *events_dir;
446 	int ret = 0;
447 
448 	events_dir = opendir(tracing_events_path);
449 	if (!events_dir) {
450 		perror("Can't open event dir");
451 		return -1;
452 	}
453 
454 	while (!ret && (events_ent = readdir(events_dir))) {
455 		if (!strcmp(events_ent->d_name, ".")
456 		    || !strcmp(events_ent->d_name, "..")
457 		    || !strcmp(events_ent->d_name, "enable")
458 		    || !strcmp(events_ent->d_name, "header_event")
459 		    || !strcmp(events_ent->d_name, "header_page"))
460 			continue;
461 
462 		if (!strglobmatch(events_ent->d_name, sys_name))
463 			continue;
464 
465 		ret = add_tracepoint_event(list, idx, events_ent->d_name,
466 					   evt_name);
467 	}
468 
469 	closedir(events_dir);
470 	return ret;
471 }
472 
473 int parse_events_add_tracepoint(struct list_head *list, int *idx,
474 				char *sys, char *event)
475 {
476 	int ret;
477 
478 	ret = debugfs_valid_mountpoint(tracing_events_path);
479 	if (ret)
480 		return ret;
481 
482 	if (strpbrk(sys, "*?"))
483 		return add_tracepoint_multi_sys(list, idx, sys, event);
484 	else
485 		return add_tracepoint_event(list, idx, sys, event);
486 }
487 
488 static int
489 parse_breakpoint_type(const char *type, struct perf_event_attr *attr)
490 {
491 	int i;
492 
493 	for (i = 0; i < 3; i++) {
494 		if (!type || !type[i])
495 			break;
496 
497 #define CHECK_SET_TYPE(bit)		\
498 do {					\
499 	if (attr->bp_type & bit)	\
500 		return -EINVAL;		\
501 	else				\
502 		attr->bp_type |= bit;	\
503 } while (0)
504 
505 		switch (type[i]) {
506 		case 'r':
507 			CHECK_SET_TYPE(HW_BREAKPOINT_R);
508 			break;
509 		case 'w':
510 			CHECK_SET_TYPE(HW_BREAKPOINT_W);
511 			break;
512 		case 'x':
513 			CHECK_SET_TYPE(HW_BREAKPOINT_X);
514 			break;
515 		default:
516 			return -EINVAL;
517 		}
518 	}
519 
520 #undef CHECK_SET_TYPE
521 
522 	if (!attr->bp_type) /* Default */
523 		attr->bp_type = HW_BREAKPOINT_R | HW_BREAKPOINT_W;
524 
525 	return 0;
526 }
527 
528 int parse_events_add_breakpoint(struct list_head *list, int *idx,
529 				void *ptr, char *type)
530 {
531 	struct perf_event_attr attr;
532 
533 	memset(&attr, 0, sizeof(attr));
534 	attr.bp_addr = (unsigned long) ptr;
535 
536 	if (parse_breakpoint_type(type, &attr))
537 		return -EINVAL;
538 
539 	/*
540 	 * We should find a nice way to override the access length
541 	 * Provide some defaults for now
542 	 */
543 	if (attr.bp_type == HW_BREAKPOINT_X)
544 		attr.bp_len = sizeof(long);
545 	else
546 		attr.bp_len = HW_BREAKPOINT_LEN_4;
547 
548 	attr.type = PERF_TYPE_BREAKPOINT;
549 	attr.sample_period = 1;
550 
551 	return add_event(list, idx, &attr, NULL);
552 }
553 
554 static int config_term(struct perf_event_attr *attr,
555 		       struct parse_events_term *term)
556 {
557 #define CHECK_TYPE_VAL(type)					\
558 do {								\
559 	if (PARSE_EVENTS__TERM_TYPE_ ## type != term->type_val)	\
560 		return -EINVAL;					\
561 } while (0)
562 
563 	switch (term->type_term) {
564 	case PARSE_EVENTS__TERM_TYPE_CONFIG:
565 		CHECK_TYPE_VAL(NUM);
566 		attr->config = term->val.num;
567 		break;
568 	case PARSE_EVENTS__TERM_TYPE_CONFIG1:
569 		CHECK_TYPE_VAL(NUM);
570 		attr->config1 = term->val.num;
571 		break;
572 	case PARSE_EVENTS__TERM_TYPE_CONFIG2:
573 		CHECK_TYPE_VAL(NUM);
574 		attr->config2 = term->val.num;
575 		break;
576 	case PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD:
577 		CHECK_TYPE_VAL(NUM);
578 		attr->sample_period = term->val.num;
579 		break;
580 	case PARSE_EVENTS__TERM_TYPE_BRANCH_SAMPLE_TYPE:
581 		/*
582 		 * TODO uncomment when the field is available
583 		 * attr->branch_sample_type = term->val.num;
584 		 */
585 		break;
586 	case PARSE_EVENTS__TERM_TYPE_NAME:
587 		CHECK_TYPE_VAL(STR);
588 		break;
589 	default:
590 		return -EINVAL;
591 	}
592 
593 	return 0;
594 #undef CHECK_TYPE_VAL
595 }
596 
597 static int config_attr(struct perf_event_attr *attr,
598 		       struct list_head *head, int fail)
599 {
600 	struct parse_events_term *term;
601 
602 	list_for_each_entry(term, head, list)
603 		if (config_term(attr, term) && fail)
604 			return -EINVAL;
605 
606 	return 0;
607 }
608 
609 int parse_events_add_numeric(struct list_head *list, int *idx,
610 			     u32 type, u64 config,
611 			     struct list_head *head_config)
612 {
613 	struct perf_event_attr attr;
614 
615 	memset(&attr, 0, sizeof(attr));
616 	attr.type = type;
617 	attr.config = config;
618 
619 	if (head_config &&
620 	    config_attr(&attr, head_config, 1))
621 		return -EINVAL;
622 
623 	return add_event(list, idx, &attr, NULL);
624 }
625 
626 static int parse_events__is_name_term(struct parse_events_term *term)
627 {
628 	return term->type_term == PARSE_EVENTS__TERM_TYPE_NAME;
629 }
630 
631 static char *pmu_event_name(struct list_head *head_terms)
632 {
633 	struct parse_events_term *term;
634 
635 	list_for_each_entry(term, head_terms, list)
636 		if (parse_events__is_name_term(term))
637 			return term->val.str;
638 
639 	return NULL;
640 }
641 
642 int parse_events_add_pmu(struct list_head *list, int *idx,
643 			 char *name, struct list_head *head_config)
644 {
645 	struct perf_event_attr attr;
646 	struct perf_pmu_info info;
647 	struct perf_pmu *pmu;
648 	struct perf_evsel *evsel;
649 
650 	pmu = perf_pmu__find(name);
651 	if (!pmu)
652 		return -EINVAL;
653 
654 	if (pmu->default_config) {
655 		memcpy(&attr, pmu->default_config,
656 		       sizeof(struct perf_event_attr));
657 	} else {
658 		memset(&attr, 0, sizeof(attr));
659 	}
660 
661 	if (!head_config) {
662 		attr.type = pmu->type;
663 		evsel = __add_event(list, idx, &attr, NULL, pmu->cpus);
664 		return evsel ? 0 : -ENOMEM;
665 	}
666 
667 	if (perf_pmu__check_alias(pmu, head_config, &info))
668 		return -EINVAL;
669 
670 	/*
671 	 * Configure hardcoded terms first, no need to check
672 	 * return value when called with fail == 0 ;)
673 	 */
674 	config_attr(&attr, head_config, 0);
675 
676 	if (perf_pmu__config(pmu, &attr, head_config))
677 		return -EINVAL;
678 
679 	evsel = __add_event(list, idx, &attr, pmu_event_name(head_config),
680 			    pmu->cpus);
681 	if (evsel) {
682 		evsel->unit = info.unit;
683 		evsel->scale = info.scale;
684 		evsel->per_pkg = info.per_pkg;
685 		evsel->snapshot = info.snapshot;
686 	}
687 
688 	return evsel ? 0 : -ENOMEM;
689 }
690 
691 int parse_events__modifier_group(struct list_head *list,
692 				 char *event_mod)
693 {
694 	return parse_events__modifier_event(list, event_mod, true);
695 }
696 
697 void parse_events__set_leader(char *name, struct list_head *list)
698 {
699 	struct perf_evsel *leader;
700 
701 	__perf_evlist__set_leader(list);
702 	leader = list_entry(list->next, struct perf_evsel, node);
703 	leader->group_name = name ? strdup(name) : NULL;
704 }
705 
706 /* list_event is assumed to point to malloc'ed memory */
707 void parse_events_update_lists(struct list_head *list_event,
708 			       struct list_head *list_all)
709 {
710 	/*
711 	 * Called for single event definition. Update the
712 	 * 'all event' list, and reinit the 'single event'
713 	 * list, for next event definition.
714 	 */
715 	list_splice_tail(list_event, list_all);
716 	free(list_event);
717 }
718 
719 struct event_modifier {
720 	int eu;
721 	int ek;
722 	int eh;
723 	int eH;
724 	int eG;
725 	int precise;
726 	int exclude_GH;
727 	int sample_read;
728 	int pinned;
729 };
730 
731 static int get_event_modifier(struct event_modifier *mod, char *str,
732 			       struct perf_evsel *evsel)
733 {
734 	int eu = evsel ? evsel->attr.exclude_user : 0;
735 	int ek = evsel ? evsel->attr.exclude_kernel : 0;
736 	int eh = evsel ? evsel->attr.exclude_hv : 0;
737 	int eH = evsel ? evsel->attr.exclude_host : 0;
738 	int eG = evsel ? evsel->attr.exclude_guest : 0;
739 	int precise = evsel ? evsel->attr.precise_ip : 0;
740 	int sample_read = 0;
741 	int pinned = evsel ? evsel->attr.pinned : 0;
742 
743 	int exclude = eu | ek | eh;
744 	int exclude_GH = evsel ? evsel->exclude_GH : 0;
745 
746 	memset(mod, 0, sizeof(*mod));
747 
748 	while (*str) {
749 		if (*str == 'u') {
750 			if (!exclude)
751 				exclude = eu = ek = eh = 1;
752 			eu = 0;
753 		} else if (*str == 'k') {
754 			if (!exclude)
755 				exclude = eu = ek = eh = 1;
756 			ek = 0;
757 		} else if (*str == 'h') {
758 			if (!exclude)
759 				exclude = eu = ek = eh = 1;
760 			eh = 0;
761 		} else if (*str == 'G') {
762 			if (!exclude_GH)
763 				exclude_GH = eG = eH = 1;
764 			eG = 0;
765 		} else if (*str == 'H') {
766 			if (!exclude_GH)
767 				exclude_GH = eG = eH = 1;
768 			eH = 0;
769 		} else if (*str == 'p') {
770 			precise++;
771 			/* use of precise requires exclude_guest */
772 			if (!exclude_GH)
773 				eG = 1;
774 		} else if (*str == 'S') {
775 			sample_read = 1;
776 		} else if (*str == 'D') {
777 			pinned = 1;
778 		} else
779 			break;
780 
781 		++str;
782 	}
783 
784 	/*
785 	 * precise ip:
786 	 *
787 	 *  0 - SAMPLE_IP can have arbitrary skid
788 	 *  1 - SAMPLE_IP must have constant skid
789 	 *  2 - SAMPLE_IP requested to have 0 skid
790 	 *  3 - SAMPLE_IP must have 0 skid
791 	 *
792 	 *  See also PERF_RECORD_MISC_EXACT_IP
793 	 */
794 	if (precise > 3)
795 		return -EINVAL;
796 
797 	mod->eu = eu;
798 	mod->ek = ek;
799 	mod->eh = eh;
800 	mod->eH = eH;
801 	mod->eG = eG;
802 	mod->precise = precise;
803 	mod->exclude_GH = exclude_GH;
804 	mod->sample_read = sample_read;
805 	mod->pinned = pinned;
806 
807 	return 0;
808 }
809 
810 /*
811  * Basic modifier sanity check to validate it contains only one
812  * instance of any modifier (apart from 'p') present.
813  */
814 static int check_modifier(char *str)
815 {
816 	char *p = str;
817 
818 	/* The sizeof includes 0 byte as well. */
819 	if (strlen(str) > (sizeof("ukhGHpppSD") - 1))
820 		return -1;
821 
822 	while (*p) {
823 		if (*p != 'p' && strchr(p + 1, *p))
824 			return -1;
825 		p++;
826 	}
827 
828 	return 0;
829 }
830 
831 int parse_events__modifier_event(struct list_head *list, char *str, bool add)
832 {
833 	struct perf_evsel *evsel;
834 	struct event_modifier mod;
835 
836 	if (str == NULL)
837 		return 0;
838 
839 	if (check_modifier(str))
840 		return -EINVAL;
841 
842 	if (!add && get_event_modifier(&mod, str, NULL))
843 		return -EINVAL;
844 
845 	__evlist__for_each(list, evsel) {
846 		if (add && get_event_modifier(&mod, str, evsel))
847 			return -EINVAL;
848 
849 		evsel->attr.exclude_user   = mod.eu;
850 		evsel->attr.exclude_kernel = mod.ek;
851 		evsel->attr.exclude_hv     = mod.eh;
852 		evsel->attr.precise_ip     = mod.precise;
853 		evsel->attr.exclude_host   = mod.eH;
854 		evsel->attr.exclude_guest  = mod.eG;
855 		evsel->exclude_GH          = mod.exclude_GH;
856 		evsel->sample_read         = mod.sample_read;
857 
858 		if (perf_evsel__is_group_leader(evsel))
859 			evsel->attr.pinned = mod.pinned;
860 	}
861 
862 	return 0;
863 }
864 
865 int parse_events_name(struct list_head *list, char *name)
866 {
867 	struct perf_evsel *evsel;
868 
869 	__evlist__for_each(list, evsel) {
870 		if (!evsel->name)
871 			evsel->name = strdup(name);
872 	}
873 
874 	return 0;
875 }
876 
877 static int
878 comp_pmu(const void *p1, const void *p2)
879 {
880 	struct perf_pmu_event_symbol *pmu1 = (struct perf_pmu_event_symbol *) p1;
881 	struct perf_pmu_event_symbol *pmu2 = (struct perf_pmu_event_symbol *) p2;
882 
883 	return strcmp(pmu1->symbol, pmu2->symbol);
884 }
885 
886 static void perf_pmu__parse_cleanup(void)
887 {
888 	if (perf_pmu_events_list_num > 0) {
889 		struct perf_pmu_event_symbol *p;
890 		int i;
891 
892 		for (i = 0; i < perf_pmu_events_list_num; i++) {
893 			p = perf_pmu_events_list + i;
894 			free(p->symbol);
895 		}
896 		free(perf_pmu_events_list);
897 		perf_pmu_events_list = NULL;
898 		perf_pmu_events_list_num = 0;
899 	}
900 }
901 
902 #define SET_SYMBOL(str, stype)		\
903 do {					\
904 	p->symbol = str;		\
905 	if (!p->symbol)			\
906 		goto err;		\
907 	p->type = stype;		\
908 } while (0)
909 
910 /*
911  * Read the pmu events list from sysfs
912  * Save it into perf_pmu_events_list
913  */
914 static void perf_pmu__parse_init(void)
915 {
916 
917 	struct perf_pmu *pmu = NULL;
918 	struct perf_pmu_alias *alias;
919 	int len = 0;
920 
921 	pmu = perf_pmu__find("cpu");
922 	if ((pmu == NULL) || list_empty(&pmu->aliases)) {
923 		perf_pmu_events_list_num = -1;
924 		return;
925 	}
926 	list_for_each_entry(alias, &pmu->aliases, list) {
927 		if (strchr(alias->name, '-'))
928 			len++;
929 		len++;
930 	}
931 	perf_pmu_events_list = malloc(sizeof(struct perf_pmu_event_symbol) * len);
932 	if (!perf_pmu_events_list)
933 		return;
934 	perf_pmu_events_list_num = len;
935 
936 	len = 0;
937 	list_for_each_entry(alias, &pmu->aliases, list) {
938 		struct perf_pmu_event_symbol *p = perf_pmu_events_list + len;
939 		char *tmp = strchr(alias->name, '-');
940 
941 		if (tmp != NULL) {
942 			SET_SYMBOL(strndup(alias->name, tmp - alias->name),
943 					PMU_EVENT_SYMBOL_PREFIX);
944 			p++;
945 			SET_SYMBOL(strdup(++tmp), PMU_EVENT_SYMBOL_SUFFIX);
946 			len += 2;
947 		} else {
948 			SET_SYMBOL(strdup(alias->name), PMU_EVENT_SYMBOL);
949 			len++;
950 		}
951 	}
952 	qsort(perf_pmu_events_list, len,
953 		sizeof(struct perf_pmu_event_symbol), comp_pmu);
954 
955 	return;
956 err:
957 	perf_pmu__parse_cleanup();
958 }
959 
960 enum perf_pmu_event_symbol_type
961 perf_pmu__parse_check(const char *name)
962 {
963 	struct perf_pmu_event_symbol p, *r;
964 
965 	/* scan kernel pmu events from sysfs if needed */
966 	if (perf_pmu_events_list_num == 0)
967 		perf_pmu__parse_init();
968 	/*
969 	 * name "cpu" could be prefix of cpu-cycles or cpu// events.
970 	 * cpu-cycles has been handled by hardcode.
971 	 * So it must be cpu// events, not kernel pmu event.
972 	 */
973 	if ((perf_pmu_events_list_num <= 0) || !strcmp(name, "cpu"))
974 		return PMU_EVENT_SYMBOL_ERR;
975 
976 	p.symbol = strdup(name);
977 	r = bsearch(&p, perf_pmu_events_list,
978 			(size_t) perf_pmu_events_list_num,
979 			sizeof(struct perf_pmu_event_symbol), comp_pmu);
980 	free(p.symbol);
981 	return r ? r->type : PMU_EVENT_SYMBOL_ERR;
982 }
983 
984 static int parse_events__scanner(const char *str, void *data, int start_token)
985 {
986 	YY_BUFFER_STATE buffer;
987 	void *scanner;
988 	int ret;
989 
990 	ret = parse_events_lex_init_extra(start_token, &scanner);
991 	if (ret)
992 		return ret;
993 
994 	buffer = parse_events__scan_string(str, scanner);
995 
996 #ifdef PARSER_DEBUG
997 	parse_events_debug = 1;
998 #endif
999 	ret = parse_events_parse(data, scanner);
1000 
1001 	parse_events__flush_buffer(buffer, scanner);
1002 	parse_events__delete_buffer(buffer, scanner);
1003 	parse_events_lex_destroy(scanner);
1004 	return ret;
1005 }
1006 
1007 /*
1008  * parse event config string, return a list of event terms.
1009  */
1010 int parse_events_terms(struct list_head *terms, const char *str)
1011 {
1012 	struct parse_events_terms data = {
1013 		.terms = NULL,
1014 	};
1015 	int ret;
1016 
1017 	ret = parse_events__scanner(str, &data, PE_START_TERMS);
1018 	if (!ret) {
1019 		list_splice(data.terms, terms);
1020 		zfree(&data.terms);
1021 		return 0;
1022 	}
1023 
1024 	if (data.terms)
1025 		parse_events__free_terms(data.terms);
1026 	return ret;
1027 }
1028 
1029 int parse_events(struct perf_evlist *evlist, const char *str)
1030 {
1031 	struct parse_events_evlist data = {
1032 		.list = LIST_HEAD_INIT(data.list),
1033 		.idx  = evlist->nr_entries,
1034 	};
1035 	int ret;
1036 
1037 	ret = parse_events__scanner(str, &data, PE_START_EVENTS);
1038 	perf_pmu__parse_cleanup();
1039 	if (!ret) {
1040 		int entries = data.idx - evlist->nr_entries;
1041 		perf_evlist__splice_list_tail(evlist, &data.list, entries);
1042 		evlist->nr_groups += data.nr_groups;
1043 		return 0;
1044 	}
1045 
1046 	/*
1047 	 * There are 2 users - builtin-record and builtin-test objects.
1048 	 * Both call perf_evlist__delete in case of error, so we dont
1049 	 * need to bother.
1050 	 */
1051 	return ret;
1052 }
1053 
1054 int parse_events_option(const struct option *opt, const char *str,
1055 			int unset __maybe_unused)
1056 {
1057 	struct perf_evlist *evlist = *(struct perf_evlist **)opt->value;
1058 	int ret = parse_events(evlist, str);
1059 
1060 	if (ret) {
1061 		fprintf(stderr, "invalid or unsupported event: '%s'\n", str);
1062 		fprintf(stderr, "Run 'perf list' for a list of valid events\n");
1063 	}
1064 	return ret;
1065 }
1066 
1067 int parse_filter(const struct option *opt, const char *str,
1068 		 int unset __maybe_unused)
1069 {
1070 	struct perf_evlist *evlist = *(struct perf_evlist **)opt->value;
1071 	struct perf_evsel *last = NULL;
1072 
1073 	if (evlist->nr_entries > 0)
1074 		last = perf_evlist__last(evlist);
1075 
1076 	if (last == NULL || last->attr.type != PERF_TYPE_TRACEPOINT) {
1077 		fprintf(stderr,
1078 			"--filter option should follow a -e tracepoint option\n");
1079 		return -1;
1080 	}
1081 
1082 	last->filter = strdup(str);
1083 	if (last->filter == NULL) {
1084 		fprintf(stderr, "not enough memory to hold filter string\n");
1085 		return -1;
1086 	}
1087 
1088 	return 0;
1089 }
1090 
1091 static const char * const event_type_descriptors[] = {
1092 	"Hardware event",
1093 	"Software event",
1094 	"Tracepoint event",
1095 	"Hardware cache event",
1096 	"Raw hardware event descriptor",
1097 	"Hardware breakpoint",
1098 };
1099 
1100 /*
1101  * Print the events from <debugfs_mount_point>/tracing/events
1102  */
1103 
1104 void print_tracepoint_events(const char *subsys_glob, const char *event_glob,
1105 			     bool name_only)
1106 {
1107 	DIR *sys_dir, *evt_dir;
1108 	struct dirent *sys_next, *evt_next, sys_dirent, evt_dirent;
1109 	char evt_path[MAXPATHLEN];
1110 	char dir_path[MAXPATHLEN];
1111 	char sbuf[STRERR_BUFSIZE];
1112 
1113 	if (debugfs_valid_mountpoint(tracing_events_path)) {
1114 		printf("  [ Tracepoints not available: %s ]\n",
1115 			strerror_r(errno, sbuf, sizeof(sbuf)));
1116 		return;
1117 	}
1118 
1119 	sys_dir = opendir(tracing_events_path);
1120 	if (!sys_dir)
1121 		return;
1122 
1123 	for_each_subsystem(sys_dir, sys_dirent, sys_next) {
1124 		if (subsys_glob != NULL &&
1125 		    !strglobmatch(sys_dirent.d_name, subsys_glob))
1126 			continue;
1127 
1128 		snprintf(dir_path, MAXPATHLEN, "%s/%s", tracing_events_path,
1129 			 sys_dirent.d_name);
1130 		evt_dir = opendir(dir_path);
1131 		if (!evt_dir)
1132 			continue;
1133 
1134 		for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next) {
1135 			if (event_glob != NULL &&
1136 			    !strglobmatch(evt_dirent.d_name, event_glob))
1137 				continue;
1138 
1139 			if (name_only) {
1140 				printf("%s:%s ", sys_dirent.d_name, evt_dirent.d_name);
1141 				continue;
1142 			}
1143 
1144 			snprintf(evt_path, MAXPATHLEN, "%s:%s",
1145 				 sys_dirent.d_name, evt_dirent.d_name);
1146 			printf("  %-50s [%s]\n", evt_path,
1147 				event_type_descriptors[PERF_TYPE_TRACEPOINT]);
1148 		}
1149 		closedir(evt_dir);
1150 	}
1151 	closedir(sys_dir);
1152 }
1153 
1154 /*
1155  * Check whether event is in <debugfs_mount_point>/tracing/events
1156  */
1157 
1158 int is_valid_tracepoint(const char *event_string)
1159 {
1160 	DIR *sys_dir, *evt_dir;
1161 	struct dirent *sys_next, *evt_next, sys_dirent, evt_dirent;
1162 	char evt_path[MAXPATHLEN];
1163 	char dir_path[MAXPATHLEN];
1164 
1165 	if (debugfs_valid_mountpoint(tracing_events_path))
1166 		return 0;
1167 
1168 	sys_dir = opendir(tracing_events_path);
1169 	if (!sys_dir)
1170 		return 0;
1171 
1172 	for_each_subsystem(sys_dir, sys_dirent, sys_next) {
1173 
1174 		snprintf(dir_path, MAXPATHLEN, "%s/%s", tracing_events_path,
1175 			 sys_dirent.d_name);
1176 		evt_dir = opendir(dir_path);
1177 		if (!evt_dir)
1178 			continue;
1179 
1180 		for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next) {
1181 			snprintf(evt_path, MAXPATHLEN, "%s:%s",
1182 				 sys_dirent.d_name, evt_dirent.d_name);
1183 			if (!strcmp(evt_path, event_string)) {
1184 				closedir(evt_dir);
1185 				closedir(sys_dir);
1186 				return 1;
1187 			}
1188 		}
1189 		closedir(evt_dir);
1190 	}
1191 	closedir(sys_dir);
1192 	return 0;
1193 }
1194 
1195 static bool is_event_supported(u8 type, unsigned config)
1196 {
1197 	bool ret = true;
1198 	int open_return;
1199 	struct perf_evsel *evsel;
1200 	struct perf_event_attr attr = {
1201 		.type = type,
1202 		.config = config,
1203 		.disabled = 1,
1204 	};
1205 	struct {
1206 		struct thread_map map;
1207 		int threads[1];
1208 	} tmap = {
1209 		.map.nr	 = 1,
1210 		.threads = { 0 },
1211 	};
1212 
1213 	evsel = perf_evsel__new(&attr);
1214 	if (evsel) {
1215 		open_return = perf_evsel__open(evsel, NULL, &tmap.map);
1216 		ret = open_return >= 0;
1217 
1218 		if (open_return == -EACCES) {
1219 			/*
1220 			 * This happens if the paranoid value
1221 			 * /proc/sys/kernel/perf_event_paranoid is set to 2
1222 			 * Re-run with exclude_kernel set; we don't do that
1223 			 * by default as some ARM machines do not support it.
1224 			 *
1225 			 */
1226 			evsel->attr.exclude_kernel = 1;
1227 			ret = perf_evsel__open(evsel, NULL, &tmap.map) >= 0;
1228 		}
1229 		perf_evsel__delete(evsel);
1230 	}
1231 
1232 	return ret;
1233 }
1234 
1235 static void __print_events_type(u8 type, struct event_symbol *syms,
1236 				unsigned max)
1237 {
1238 	char name[64];
1239 	unsigned i;
1240 
1241 	for (i = 0; i < max ; i++, syms++) {
1242 		if (!is_event_supported(type, i))
1243 			continue;
1244 
1245 		if (strlen(syms->alias))
1246 			snprintf(name, sizeof(name),  "%s OR %s",
1247 				 syms->symbol, syms->alias);
1248 		else
1249 			snprintf(name, sizeof(name), "%s", syms->symbol);
1250 
1251 		printf("  %-50s [%s]\n", name, event_type_descriptors[type]);
1252 	}
1253 }
1254 
1255 void print_events_type(u8 type)
1256 {
1257 	if (type == PERF_TYPE_SOFTWARE)
1258 		__print_events_type(type, event_symbols_sw, PERF_COUNT_SW_MAX);
1259 	else
1260 		__print_events_type(type, event_symbols_hw, PERF_COUNT_HW_MAX);
1261 }
1262 
1263 int print_hwcache_events(const char *event_glob, bool name_only)
1264 {
1265 	unsigned int type, op, i, printed = 0;
1266 	char name[64];
1267 
1268 	for (type = 0; type < PERF_COUNT_HW_CACHE_MAX; type++) {
1269 		for (op = 0; op < PERF_COUNT_HW_CACHE_OP_MAX; op++) {
1270 			/* skip invalid cache type */
1271 			if (!perf_evsel__is_cache_op_valid(type, op))
1272 				continue;
1273 
1274 			for (i = 0; i < PERF_COUNT_HW_CACHE_RESULT_MAX; i++) {
1275 				__perf_evsel__hw_cache_type_op_res_name(type, op, i,
1276 									name, sizeof(name));
1277 				if (event_glob != NULL && !strglobmatch(name, event_glob))
1278 					continue;
1279 
1280 				if (!is_event_supported(PERF_TYPE_HW_CACHE,
1281 							type | (op << 8) | (i << 16)))
1282 					continue;
1283 
1284 				if (name_only)
1285 					printf("%s ", name);
1286 				else
1287 					printf("  %-50s [%s]\n", name,
1288 					       event_type_descriptors[PERF_TYPE_HW_CACHE]);
1289 				++printed;
1290 			}
1291 		}
1292 	}
1293 
1294 	if (printed)
1295 		printf("\n");
1296 	return printed;
1297 }
1298 
1299 static void print_symbol_events(const char *event_glob, unsigned type,
1300 				struct event_symbol *syms, unsigned max,
1301 				bool name_only)
1302 {
1303 	unsigned i, printed = 0;
1304 	char name[MAX_NAME_LEN];
1305 
1306 	for (i = 0; i < max; i++, syms++) {
1307 
1308 		if (event_glob != NULL &&
1309 		    !(strglobmatch(syms->symbol, event_glob) ||
1310 		      (syms->alias && strglobmatch(syms->alias, event_glob))))
1311 			continue;
1312 
1313 		if (!is_event_supported(type, i))
1314 			continue;
1315 
1316 		if (name_only) {
1317 			printf("%s ", syms->symbol);
1318 			continue;
1319 		}
1320 
1321 		if (strlen(syms->alias))
1322 			snprintf(name, MAX_NAME_LEN, "%s OR %s", syms->symbol, syms->alias);
1323 		else
1324 			strncpy(name, syms->symbol, MAX_NAME_LEN);
1325 
1326 		printf("  %-50s [%s]\n", name, event_type_descriptors[type]);
1327 
1328 		printed++;
1329 	}
1330 
1331 	if (printed)
1332 		printf("\n");
1333 }
1334 
1335 /*
1336  * Print the help text for the event symbols:
1337  */
1338 void print_events(const char *event_glob, bool name_only)
1339 {
1340 	if (!name_only) {
1341 		printf("\n");
1342 		printf("List of pre-defined events (to be used in -e):\n");
1343 	}
1344 
1345 	print_symbol_events(event_glob, PERF_TYPE_HARDWARE,
1346 			    event_symbols_hw, PERF_COUNT_HW_MAX, name_only);
1347 
1348 	print_symbol_events(event_glob, PERF_TYPE_SOFTWARE,
1349 			    event_symbols_sw, PERF_COUNT_SW_MAX, name_only);
1350 
1351 	print_hwcache_events(event_glob, name_only);
1352 
1353 	print_pmu_events(event_glob, name_only);
1354 
1355 	if (event_glob != NULL)
1356 		return;
1357 
1358 	if (!name_only) {
1359 		printf("  %-50s [%s]\n",
1360 		       "rNNN",
1361 		       event_type_descriptors[PERF_TYPE_RAW]);
1362 		printf("  %-50s [%s]\n",
1363 		       "cpu/t1=v1[,t2=v2,t3 ...]/modifier",
1364 		       event_type_descriptors[PERF_TYPE_RAW]);
1365 		printf("   (see 'man perf-list' on how to encode it)\n");
1366 		printf("\n");
1367 
1368 		printf("  %-50s [%s]\n",
1369 		       "mem:<addr>[:access]",
1370 			event_type_descriptors[PERF_TYPE_BREAKPOINT]);
1371 		printf("\n");
1372 	}
1373 
1374 	print_tracepoint_events(NULL, NULL, name_only);
1375 }
1376 
1377 int parse_events__is_hardcoded_term(struct parse_events_term *term)
1378 {
1379 	return term->type_term != PARSE_EVENTS__TERM_TYPE_USER;
1380 }
1381 
1382 static int new_term(struct parse_events_term **_term, int type_val,
1383 		    int type_term, char *config,
1384 		    char *str, u64 num)
1385 {
1386 	struct parse_events_term *term;
1387 
1388 	term = zalloc(sizeof(*term));
1389 	if (!term)
1390 		return -ENOMEM;
1391 
1392 	INIT_LIST_HEAD(&term->list);
1393 	term->type_val  = type_val;
1394 	term->type_term = type_term;
1395 	term->config = config;
1396 
1397 	switch (type_val) {
1398 	case PARSE_EVENTS__TERM_TYPE_NUM:
1399 		term->val.num = num;
1400 		break;
1401 	case PARSE_EVENTS__TERM_TYPE_STR:
1402 		term->val.str = str;
1403 		break;
1404 	default:
1405 		free(term);
1406 		return -EINVAL;
1407 	}
1408 
1409 	*_term = term;
1410 	return 0;
1411 }
1412 
1413 int parse_events_term__num(struct parse_events_term **term,
1414 			   int type_term, char *config, u64 num)
1415 {
1416 	return new_term(term, PARSE_EVENTS__TERM_TYPE_NUM, type_term,
1417 			config, NULL, num);
1418 }
1419 
1420 int parse_events_term__str(struct parse_events_term **term,
1421 			   int type_term, char *config, char *str)
1422 {
1423 	return new_term(term, PARSE_EVENTS__TERM_TYPE_STR, type_term,
1424 			config, str, 0);
1425 }
1426 
1427 int parse_events_term__sym_hw(struct parse_events_term **term,
1428 			      char *config, unsigned idx)
1429 {
1430 	struct event_symbol *sym;
1431 
1432 	BUG_ON(idx >= PERF_COUNT_HW_MAX);
1433 	sym = &event_symbols_hw[idx];
1434 
1435 	if (config)
1436 		return new_term(term, PARSE_EVENTS__TERM_TYPE_STR,
1437 				PARSE_EVENTS__TERM_TYPE_USER, config,
1438 				(char *) sym->symbol, 0);
1439 	else
1440 		return new_term(term, PARSE_EVENTS__TERM_TYPE_STR,
1441 				PARSE_EVENTS__TERM_TYPE_USER,
1442 				(char *) "event", (char *) sym->symbol, 0);
1443 }
1444 
1445 int parse_events_term__clone(struct parse_events_term **new,
1446 			     struct parse_events_term *term)
1447 {
1448 	return new_term(new, term->type_val, term->type_term, term->config,
1449 			term->val.str, term->val.num);
1450 }
1451 
1452 void parse_events__free_terms(struct list_head *terms)
1453 {
1454 	struct parse_events_term *term, *h;
1455 
1456 	list_for_each_entry_safe(term, h, terms, list)
1457 		free(term);
1458 }
1459