• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #include <linux/hw_breakpoint.h>
2 #include <linux/err.h>
3 #include "util.h"
4 #include "../perf.h"
5 #include "evlist.h"
6 #include "evsel.h"
7 #include "parse-options.h"
8 #include "parse-events.h"
9 #include "exec_cmd.h"
10 #include "string.h"
11 #include "symbol.h"
12 #include "cache.h"
13 #include "header.h"
14 #include "bpf-loader.h"
15 #include "debug.h"
16 #include <api/fs/tracing_path.h>
17 #include "parse-events-bison.h"
18 #define YY_EXTRA_TYPE int
19 #include "parse-events-flex.h"
20 #include "pmu.h"
21 #include "thread_map.h"
22 #include "cpumap.h"
23 #include "asm/bug.h"
24 
25 #define MAX_NAME_LEN 100
26 
27 #ifdef PARSER_DEBUG
28 extern int parse_events_debug;
29 #endif
30 int parse_events_parse(void *data, void *scanner);
31 static int get_config_terms(struct list_head *head_config,
32 			    struct list_head *head_terms __maybe_unused);
33 
34 static struct perf_pmu_event_symbol *perf_pmu_events_list;
35 /*
36  * The variable indicates the number of supported pmu event symbols.
37  * 0 means not initialized and ready to init
38  * -1 means failed to init, don't try anymore
39  * >0 is the number of supported pmu event symbols
40  */
41 static int perf_pmu_events_list_num;
42 
43 struct event_symbol event_symbols_hw[PERF_COUNT_HW_MAX] = {
44 	[PERF_COUNT_HW_CPU_CYCLES] = {
45 		.symbol = "cpu-cycles",
46 		.alias  = "cycles",
47 	},
48 	[PERF_COUNT_HW_INSTRUCTIONS] = {
49 		.symbol = "instructions",
50 		.alias  = "",
51 	},
52 	[PERF_COUNT_HW_CACHE_REFERENCES] = {
53 		.symbol = "cache-references",
54 		.alias  = "",
55 	},
56 	[PERF_COUNT_HW_CACHE_MISSES] = {
57 		.symbol = "cache-misses",
58 		.alias  = "",
59 	},
60 	[PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = {
61 		.symbol = "branch-instructions",
62 		.alias  = "branches",
63 	},
64 	[PERF_COUNT_HW_BRANCH_MISSES] = {
65 		.symbol = "branch-misses",
66 		.alias  = "",
67 	},
68 	[PERF_COUNT_HW_BUS_CYCLES] = {
69 		.symbol = "bus-cycles",
70 		.alias  = "",
71 	},
72 	[PERF_COUNT_HW_STALLED_CYCLES_FRONTEND] = {
73 		.symbol = "stalled-cycles-frontend",
74 		.alias  = "idle-cycles-frontend",
75 	},
76 	[PERF_COUNT_HW_STALLED_CYCLES_BACKEND] = {
77 		.symbol = "stalled-cycles-backend",
78 		.alias  = "idle-cycles-backend",
79 	},
80 	[PERF_COUNT_HW_REF_CPU_CYCLES] = {
81 		.symbol = "ref-cycles",
82 		.alias  = "",
83 	},
84 };
85 
86 struct event_symbol event_symbols_sw[PERF_COUNT_SW_MAX] = {
87 	[PERF_COUNT_SW_CPU_CLOCK] = {
88 		.symbol = "cpu-clock",
89 		.alias  = "",
90 	},
91 	[PERF_COUNT_SW_TASK_CLOCK] = {
92 		.symbol = "task-clock",
93 		.alias  = "",
94 	},
95 	[PERF_COUNT_SW_PAGE_FAULTS] = {
96 		.symbol = "page-faults",
97 		.alias  = "faults",
98 	},
99 	[PERF_COUNT_SW_CONTEXT_SWITCHES] = {
100 		.symbol = "context-switches",
101 		.alias  = "cs",
102 	},
103 	[PERF_COUNT_SW_CPU_MIGRATIONS] = {
104 		.symbol = "cpu-migrations",
105 		.alias  = "migrations",
106 	},
107 	[PERF_COUNT_SW_PAGE_FAULTS_MIN] = {
108 		.symbol = "minor-faults",
109 		.alias  = "",
110 	},
111 	[PERF_COUNT_SW_PAGE_FAULTS_MAJ] = {
112 		.symbol = "major-faults",
113 		.alias  = "",
114 	},
115 	[PERF_COUNT_SW_ALIGNMENT_FAULTS] = {
116 		.symbol = "alignment-faults",
117 		.alias  = "",
118 	},
119 	[PERF_COUNT_SW_EMULATION_FAULTS] = {
120 		.symbol = "emulation-faults",
121 		.alias  = "",
122 	},
123 	[PERF_COUNT_SW_DUMMY] = {
124 		.symbol = "dummy",
125 		.alias  = "",
126 	},
127 	[PERF_COUNT_SW_BPF_OUTPUT] = {
128 		.symbol = "bpf-output",
129 		.alias  = "",
130 	},
131 };
132 
133 #define __PERF_EVENT_FIELD(config, name) \
134 	((config & PERF_EVENT_##name##_MASK) >> PERF_EVENT_##name##_SHIFT)
135 
136 #define PERF_EVENT_RAW(config)		__PERF_EVENT_FIELD(config, RAW)
137 #define PERF_EVENT_CONFIG(config)	__PERF_EVENT_FIELD(config, CONFIG)
138 #define PERF_EVENT_TYPE(config)		__PERF_EVENT_FIELD(config, TYPE)
139 #define PERF_EVENT_ID(config)		__PERF_EVENT_FIELD(config, EVENT)
140 
141 #define for_each_subsystem(sys_dir, sys_dirent)			\
142 	while ((sys_dirent = readdir(sys_dir)) != NULL)		\
143 		if (sys_dirent->d_type == DT_DIR &&		\
144 		    (strcmp(sys_dirent->d_name, ".")) &&	\
145 		    (strcmp(sys_dirent->d_name, "..")))
146 
tp_event_has_id(struct dirent * sys_dir,struct dirent * evt_dir)147 static int tp_event_has_id(struct dirent *sys_dir, struct dirent *evt_dir)
148 {
149 	char evt_path[MAXPATHLEN];
150 	int fd;
151 
152 	snprintf(evt_path, MAXPATHLEN, "%s/%s/%s/id", tracing_events_path,
153 			sys_dir->d_name, evt_dir->d_name);
154 	fd = open(evt_path, O_RDONLY);
155 	if (fd < 0)
156 		return -EINVAL;
157 	close(fd);
158 
159 	return 0;
160 }
161 
162 #define for_each_event(sys_dirent, evt_dir, evt_dirent)		\
163 	while ((evt_dirent = readdir(evt_dir)) != NULL)		\
164 		if (evt_dirent->d_type == DT_DIR &&		\
165 		    (strcmp(evt_dirent->d_name, ".")) &&	\
166 		    (strcmp(evt_dirent->d_name, "..")) &&	\
167 		    (!tp_event_has_id(sys_dirent, evt_dirent)))
168 
169 #define MAX_EVENT_LENGTH 512
170 
171 
tracepoint_id_to_path(u64 config)172 struct tracepoint_path *tracepoint_id_to_path(u64 config)
173 {
174 	struct tracepoint_path *path = NULL;
175 	DIR *sys_dir, *evt_dir;
176 	struct dirent *sys_dirent, *evt_dirent;
177 	char id_buf[24];
178 	int fd;
179 	u64 id;
180 	char evt_path[MAXPATHLEN];
181 	char dir_path[MAXPATHLEN];
182 
183 	sys_dir = opendir(tracing_events_path);
184 	if (!sys_dir)
185 		return NULL;
186 
187 	for_each_subsystem(sys_dir, sys_dirent) {
188 
189 		snprintf(dir_path, MAXPATHLEN, "%s/%s", tracing_events_path,
190 			 sys_dirent->d_name);
191 		evt_dir = opendir(dir_path);
192 		if (!evt_dir)
193 			continue;
194 
195 		for_each_event(sys_dirent, evt_dir, evt_dirent) {
196 
197 			scnprintf(evt_path, MAXPATHLEN, "%s/%s/id", dir_path,
198 				  evt_dirent->d_name);
199 			fd = open(evt_path, O_RDONLY);
200 			if (fd < 0)
201 				continue;
202 			if (read(fd, id_buf, sizeof(id_buf)) < 0) {
203 				close(fd);
204 				continue;
205 			}
206 			close(fd);
207 			id = atoll(id_buf);
208 			if (id == config) {
209 				closedir(evt_dir);
210 				closedir(sys_dir);
211 				path = zalloc(sizeof(*path));
212 				path->system = malloc(MAX_EVENT_LENGTH);
213 				if (!path->system) {
214 					free(path);
215 					return NULL;
216 				}
217 				path->name = malloc(MAX_EVENT_LENGTH);
218 				if (!path->name) {
219 					zfree(&path->system);
220 					free(path);
221 					return NULL;
222 				}
223 				strncpy(path->system, sys_dirent->d_name,
224 					MAX_EVENT_LENGTH);
225 				strncpy(path->name, evt_dirent->d_name,
226 					MAX_EVENT_LENGTH);
227 				return path;
228 			}
229 		}
230 		closedir(evt_dir);
231 	}
232 
233 	closedir(sys_dir);
234 	return NULL;
235 }
236 
tracepoint_name_to_path(const char * name)237 struct tracepoint_path *tracepoint_name_to_path(const char *name)
238 {
239 	struct tracepoint_path *path = zalloc(sizeof(*path));
240 	char *str = strchr(name, ':');
241 
242 	if (path == NULL || str == NULL) {
243 		free(path);
244 		return NULL;
245 	}
246 
247 	path->system = strndup(name, str - name);
248 	path->name = strdup(str+1);
249 
250 	if (path->system == NULL || path->name == NULL) {
251 		zfree(&path->system);
252 		zfree(&path->name);
253 		free(path);
254 		path = NULL;
255 	}
256 
257 	return path;
258 }
259 
event_type(int type)260 const char *event_type(int type)
261 {
262 	switch (type) {
263 	case PERF_TYPE_HARDWARE:
264 		return "hardware";
265 
266 	case PERF_TYPE_SOFTWARE:
267 		return "software";
268 
269 	case PERF_TYPE_TRACEPOINT:
270 		return "tracepoint";
271 
272 	case PERF_TYPE_HW_CACHE:
273 		return "hardware-cache";
274 
275 	default:
276 		break;
277 	}
278 
279 	return "unknown";
280 }
281 
282 
283 
284 static struct perf_evsel *
__add_event(struct list_head * list,int * idx,struct perf_event_attr * attr,char * name,struct cpu_map * cpus,struct list_head * config_terms)285 __add_event(struct list_head *list, int *idx,
286 	    struct perf_event_attr *attr,
287 	    char *name, struct cpu_map *cpus,
288 	    struct list_head *config_terms)
289 {
290 	struct perf_evsel *evsel;
291 
292 	event_attr_init(attr);
293 
294 	evsel = perf_evsel__new_idx(attr, *idx);
295 	if (!evsel)
296 		return NULL;
297 
298 	(*idx)++;
299 	evsel->cpus     = cpu_map__get(cpus);
300 	evsel->own_cpus = cpu_map__get(cpus);
301 
302 	if (name)
303 		evsel->name = strdup(name);
304 
305 	if (config_terms)
306 		list_splice(config_terms, &evsel->config_terms);
307 
308 	list_add_tail(&evsel->node, list);
309 	return evsel;
310 }
311 
add_event(struct list_head * list,int * idx,struct perf_event_attr * attr,char * name,struct list_head * config_terms)312 static int add_event(struct list_head *list, int *idx,
313 		     struct perf_event_attr *attr, char *name,
314 		     struct list_head *config_terms)
315 {
316 	return __add_event(list, idx, attr, name, NULL, config_terms) ? 0 : -ENOMEM;
317 }
318 
parse_aliases(char * str,const char * names[][PERF_EVSEL__MAX_ALIASES],int size)319 static int parse_aliases(char *str, const char *names[][PERF_EVSEL__MAX_ALIASES], int size)
320 {
321 	int i, j;
322 	int n, longest = -1;
323 
324 	for (i = 0; i < size; i++) {
325 		for (j = 0; j < PERF_EVSEL__MAX_ALIASES && names[i][j]; j++) {
326 			n = strlen(names[i][j]);
327 			if (n > longest && !strncasecmp(str, names[i][j], n))
328 				longest = n;
329 		}
330 		if (longest > 0)
331 			return i;
332 	}
333 
334 	return -1;
335 }
336 
parse_events_add_cache(struct list_head * list,int * idx,char * type,char * op_result1,char * op_result2)337 int parse_events_add_cache(struct list_head *list, int *idx,
338 			   char *type, char *op_result1, char *op_result2)
339 {
340 	struct perf_event_attr attr;
341 	char name[MAX_NAME_LEN];
342 	int cache_type = -1, cache_op = -1, cache_result = -1;
343 	char *op_result[2] = { op_result1, op_result2 };
344 	int i, n;
345 
346 	/*
347 	 * No fallback - if we cannot get a clear cache type
348 	 * then bail out:
349 	 */
350 	cache_type = parse_aliases(type, perf_evsel__hw_cache,
351 				   PERF_COUNT_HW_CACHE_MAX);
352 	if (cache_type == -1)
353 		return -EINVAL;
354 
355 	n = snprintf(name, MAX_NAME_LEN, "%s", type);
356 
357 	for (i = 0; (i < 2) && (op_result[i]); i++) {
358 		char *str = op_result[i];
359 
360 		n += snprintf(name + n, MAX_NAME_LEN - n, "-%s", str);
361 
362 		if (cache_op == -1) {
363 			cache_op = parse_aliases(str, perf_evsel__hw_cache_op,
364 						 PERF_COUNT_HW_CACHE_OP_MAX);
365 			if (cache_op >= 0) {
366 				if (!perf_evsel__is_cache_op_valid(cache_type, cache_op))
367 					return -EINVAL;
368 				continue;
369 			}
370 		}
371 
372 		if (cache_result == -1) {
373 			cache_result = parse_aliases(str, perf_evsel__hw_cache_result,
374 						     PERF_COUNT_HW_CACHE_RESULT_MAX);
375 			if (cache_result >= 0)
376 				continue;
377 		}
378 	}
379 
380 	/*
381 	 * Fall back to reads:
382 	 */
383 	if (cache_op == -1)
384 		cache_op = PERF_COUNT_HW_CACHE_OP_READ;
385 
386 	/*
387 	 * Fall back to accesses:
388 	 */
389 	if (cache_result == -1)
390 		cache_result = PERF_COUNT_HW_CACHE_RESULT_ACCESS;
391 
392 	memset(&attr, 0, sizeof(attr));
393 	attr.config = cache_type | (cache_op << 8) | (cache_result << 16);
394 	attr.type = PERF_TYPE_HW_CACHE;
395 	return add_event(list, idx, &attr, name, NULL);
396 }
397 
tracepoint_error(struct parse_events_error * e,int err,char * sys,char * name)398 static void tracepoint_error(struct parse_events_error *e, int err,
399 			     char *sys, char *name)
400 {
401 	char help[BUFSIZ];
402 
403 	if (!e)
404 		return;
405 
406 	/*
407 	 * We get error directly from syscall errno ( > 0),
408 	 * or from encoded pointer's error ( < 0).
409 	 */
410 	err = abs(err);
411 
412 	switch (err) {
413 	case EACCES:
414 		e->str = strdup("can't access trace events");
415 		break;
416 	case ENOENT:
417 		e->str = strdup("unknown tracepoint");
418 		break;
419 	default:
420 		e->str = strdup("failed to add tracepoint");
421 		break;
422 	}
423 
424 	tracing_path__strerror_open_tp(err, help, sizeof(help), sys, name);
425 	e->help = strdup(help);
426 }
427 
add_tracepoint(struct list_head * list,int * idx,char * sys_name,char * evt_name,struct parse_events_error * err,struct list_head * head_config)428 static int add_tracepoint(struct list_head *list, int *idx,
429 			  char *sys_name, char *evt_name,
430 			  struct parse_events_error *err,
431 			  struct list_head *head_config)
432 {
433 	struct perf_evsel *evsel;
434 
435 	evsel = perf_evsel__newtp_idx(sys_name, evt_name, (*idx)++);
436 	if (IS_ERR(evsel)) {
437 		tracepoint_error(err, PTR_ERR(evsel), sys_name, evt_name);
438 		return PTR_ERR(evsel);
439 	}
440 
441 	if (head_config) {
442 		LIST_HEAD(config_terms);
443 
444 		if (get_config_terms(head_config, &config_terms))
445 			return -ENOMEM;
446 		list_splice(&config_terms, &evsel->config_terms);
447 	}
448 
449 	list_add_tail(&evsel->node, list);
450 	return 0;
451 }
452 
add_tracepoint_multi_event(struct list_head * list,int * idx,char * sys_name,char * evt_name,struct parse_events_error * err,struct list_head * head_config)453 static int add_tracepoint_multi_event(struct list_head *list, int *idx,
454 				      char *sys_name, char *evt_name,
455 				      struct parse_events_error *err,
456 				      struct list_head *head_config)
457 {
458 	char evt_path[MAXPATHLEN];
459 	struct dirent *evt_ent;
460 	DIR *evt_dir;
461 	int ret = 0, found = 0;
462 
463 	snprintf(evt_path, MAXPATHLEN, "%s/%s", tracing_events_path, sys_name);
464 	evt_dir = opendir(evt_path);
465 	if (!evt_dir) {
466 		tracepoint_error(err, errno, sys_name, evt_name);
467 		return -1;
468 	}
469 
470 	while (!ret && (evt_ent = readdir(evt_dir))) {
471 		if (!strcmp(evt_ent->d_name, ".")
472 		    || !strcmp(evt_ent->d_name, "..")
473 		    || !strcmp(evt_ent->d_name, "enable")
474 		    || !strcmp(evt_ent->d_name, "filter"))
475 			continue;
476 
477 		if (!strglobmatch(evt_ent->d_name, evt_name))
478 			continue;
479 
480 		found++;
481 
482 		ret = add_tracepoint(list, idx, sys_name, evt_ent->d_name,
483 				     err, head_config);
484 	}
485 
486 	if (!found) {
487 		tracepoint_error(err, ENOENT, sys_name, evt_name);
488 		ret = -1;
489 	}
490 
491 	closedir(evt_dir);
492 	return ret;
493 }
494 
add_tracepoint_event(struct list_head * list,int * idx,char * sys_name,char * evt_name,struct parse_events_error * err,struct list_head * head_config)495 static int add_tracepoint_event(struct list_head *list, int *idx,
496 				char *sys_name, char *evt_name,
497 				struct parse_events_error *err,
498 				struct list_head *head_config)
499 {
500 	return strpbrk(evt_name, "*?") ?
501 	       add_tracepoint_multi_event(list, idx, sys_name, evt_name,
502 					  err, head_config) :
503 	       add_tracepoint(list, idx, sys_name, evt_name,
504 			      err, head_config);
505 }
506 
add_tracepoint_multi_sys(struct list_head * list,int * idx,char * sys_name,char * evt_name,struct parse_events_error * err,struct list_head * head_config)507 static int add_tracepoint_multi_sys(struct list_head *list, int *idx,
508 				    char *sys_name, char *evt_name,
509 				    struct parse_events_error *err,
510 				    struct list_head *head_config)
511 {
512 	struct dirent *events_ent;
513 	DIR *events_dir;
514 	int ret = 0;
515 
516 	events_dir = opendir(tracing_events_path);
517 	if (!events_dir) {
518 		tracepoint_error(err, errno, sys_name, evt_name);
519 		return -1;
520 	}
521 
522 	while (!ret && (events_ent = readdir(events_dir))) {
523 		if (!strcmp(events_ent->d_name, ".")
524 		    || !strcmp(events_ent->d_name, "..")
525 		    || !strcmp(events_ent->d_name, "enable")
526 		    || !strcmp(events_ent->d_name, "header_event")
527 		    || !strcmp(events_ent->d_name, "header_page"))
528 			continue;
529 
530 		if (!strglobmatch(events_ent->d_name, sys_name))
531 			continue;
532 
533 		ret = add_tracepoint_event(list, idx, events_ent->d_name,
534 					   evt_name, err, head_config);
535 	}
536 
537 	closedir(events_dir);
538 	return ret;
539 }
540 
541 struct __add_bpf_event_param {
542 	struct parse_events_evlist *data;
543 	struct list_head *list;
544 };
545 
add_bpf_event(struct probe_trace_event * tev,int fd,void * _param)546 static int add_bpf_event(struct probe_trace_event *tev, int fd,
547 			 void *_param)
548 {
549 	LIST_HEAD(new_evsels);
550 	struct __add_bpf_event_param *param = _param;
551 	struct parse_events_evlist *evlist = param->data;
552 	struct list_head *list = param->list;
553 	struct perf_evsel *pos;
554 	int err;
555 
556 	pr_debug("add bpf event %s:%s and attach bpf program %d\n",
557 		 tev->group, tev->event, fd);
558 
559 	err = parse_events_add_tracepoint(&new_evsels, &evlist->idx, tev->group,
560 					  tev->event, evlist->error, NULL);
561 	if (err) {
562 		struct perf_evsel *evsel, *tmp;
563 
564 		pr_debug("Failed to add BPF event %s:%s\n",
565 			 tev->group, tev->event);
566 		list_for_each_entry_safe(evsel, tmp, &new_evsels, node) {
567 			list_del(&evsel->node);
568 			perf_evsel__delete(evsel);
569 		}
570 		return err;
571 	}
572 	pr_debug("adding %s:%s\n", tev->group, tev->event);
573 
574 	list_for_each_entry(pos, &new_evsels, node) {
575 		pr_debug("adding %s:%s to %p\n",
576 			 tev->group, tev->event, pos);
577 		pos->bpf_fd = fd;
578 	}
579 	list_splice(&new_evsels, list);
580 	return 0;
581 }
582 
parse_events_load_bpf_obj(struct parse_events_evlist * data,struct list_head * list,struct bpf_object * obj)583 int parse_events_load_bpf_obj(struct parse_events_evlist *data,
584 			      struct list_head *list,
585 			      struct bpf_object *obj)
586 {
587 	int err;
588 	char errbuf[BUFSIZ];
589 	struct __add_bpf_event_param param = {data, list};
590 	static bool registered_unprobe_atexit = false;
591 
592 	if (IS_ERR(obj) || !obj) {
593 		snprintf(errbuf, sizeof(errbuf),
594 			 "Internal error: load bpf obj with NULL");
595 		err = -EINVAL;
596 		goto errout;
597 	}
598 
599 	/*
600 	 * Register atexit handler before calling bpf__probe() so
601 	 * bpf__probe() don't need to unprobe probe points its already
602 	 * created when failure.
603 	 */
604 	if (!registered_unprobe_atexit) {
605 		atexit(bpf__clear);
606 		registered_unprobe_atexit = true;
607 	}
608 
609 	err = bpf__probe(obj);
610 	if (err) {
611 		bpf__strerror_probe(obj, err, errbuf, sizeof(errbuf));
612 		goto errout;
613 	}
614 
615 	err = bpf__load(obj);
616 	if (err) {
617 		bpf__strerror_load(obj, err, errbuf, sizeof(errbuf));
618 		goto errout;
619 	}
620 
621 	err = bpf__foreach_tev(obj, add_bpf_event, &param);
622 	if (err) {
623 		snprintf(errbuf, sizeof(errbuf),
624 			 "Attach events in BPF object failed");
625 		goto errout;
626 	}
627 
628 	return 0;
629 errout:
630 	data->error->help = strdup("(add -v to see detail)");
631 	data->error->str = strdup(errbuf);
632 	return err;
633 }
634 
parse_events_load_bpf(struct parse_events_evlist * data,struct list_head * list,char * bpf_file_name,bool source)635 int parse_events_load_bpf(struct parse_events_evlist *data,
636 			  struct list_head *list,
637 			  char *bpf_file_name,
638 			  bool source)
639 {
640 	struct bpf_object *obj;
641 
642 	obj = bpf__prepare_load(bpf_file_name, source);
643 	if (IS_ERR(obj)) {
644 		char errbuf[BUFSIZ];
645 		int err;
646 
647 		err = PTR_ERR(obj);
648 
649 		if (err == -ENOTSUP)
650 			snprintf(errbuf, sizeof(errbuf),
651 				 "BPF support is not compiled");
652 		else
653 			bpf__strerror_prepare_load(bpf_file_name,
654 						   source,
655 						   -err, errbuf,
656 						   sizeof(errbuf));
657 
658 		data->error->help = strdup("(add -v to see detail)");
659 		data->error->str = strdup(errbuf);
660 		return err;
661 	}
662 
663 	return parse_events_load_bpf_obj(data, list, obj);
664 }
665 
666 static int
parse_breakpoint_type(const char * type,struct perf_event_attr * attr)667 parse_breakpoint_type(const char *type, struct perf_event_attr *attr)
668 {
669 	int i;
670 
671 	for (i = 0; i < 3; i++) {
672 		if (!type || !type[i])
673 			break;
674 
675 #define CHECK_SET_TYPE(bit)		\
676 do {					\
677 	if (attr->bp_type & bit)	\
678 		return -EINVAL;		\
679 	else				\
680 		attr->bp_type |= bit;	\
681 } while (0)
682 
683 		switch (type[i]) {
684 		case 'r':
685 			CHECK_SET_TYPE(HW_BREAKPOINT_R);
686 			break;
687 		case 'w':
688 			CHECK_SET_TYPE(HW_BREAKPOINT_W);
689 			break;
690 		case 'x':
691 			CHECK_SET_TYPE(HW_BREAKPOINT_X);
692 			break;
693 		default:
694 			return -EINVAL;
695 		}
696 	}
697 
698 #undef CHECK_SET_TYPE
699 
700 	if (!attr->bp_type) /* Default */
701 		attr->bp_type = HW_BREAKPOINT_R | HW_BREAKPOINT_W;
702 
703 	return 0;
704 }
705 
parse_events_add_breakpoint(struct list_head * list,int * idx,void * ptr,char * type,u64 len)706 int parse_events_add_breakpoint(struct list_head *list, int *idx,
707 				void *ptr, char *type, u64 len)
708 {
709 	struct perf_event_attr attr;
710 
711 	memset(&attr, 0, sizeof(attr));
712 	attr.bp_addr = (unsigned long) ptr;
713 
714 	if (parse_breakpoint_type(type, &attr))
715 		return -EINVAL;
716 
717 	/* Provide some defaults if len is not specified */
718 	if (!len) {
719 		if (attr.bp_type == HW_BREAKPOINT_X)
720 			len = sizeof(long);
721 		else
722 			len = HW_BREAKPOINT_LEN_4;
723 	}
724 
725 	attr.bp_len = len;
726 
727 	attr.type = PERF_TYPE_BREAKPOINT;
728 	attr.sample_period = 1;
729 
730 	return add_event(list, idx, &attr, NULL, NULL);
731 }
732 
check_type_val(struct parse_events_term * term,struct parse_events_error * err,int type)733 static int check_type_val(struct parse_events_term *term,
734 			  struct parse_events_error *err,
735 			  int type)
736 {
737 	if (type == term->type_val)
738 		return 0;
739 
740 	if (err) {
741 		err->idx = term->err_val;
742 		if (type == PARSE_EVENTS__TERM_TYPE_NUM)
743 			err->str = strdup("expected numeric value");
744 		else
745 			err->str = strdup("expected string value");
746 	}
747 	return -EINVAL;
748 }
749 
750 typedef int config_term_func_t(struct perf_event_attr *attr,
751 			       struct parse_events_term *term,
752 			       struct parse_events_error *err);
753 
config_term_common(struct perf_event_attr * attr,struct parse_events_term * term,struct parse_events_error * err)754 static int config_term_common(struct perf_event_attr *attr,
755 			      struct parse_events_term *term,
756 			      struct parse_events_error *err)
757 {
758 #define CHECK_TYPE_VAL(type)						   \
759 do {									   \
760 	if (check_type_val(term, err, PARSE_EVENTS__TERM_TYPE_ ## type)) \
761 		return -EINVAL;						   \
762 } while (0)
763 
764 	switch (term->type_term) {
765 	case PARSE_EVENTS__TERM_TYPE_CONFIG:
766 		CHECK_TYPE_VAL(NUM);
767 		attr->config = term->val.num;
768 		break;
769 	case PARSE_EVENTS__TERM_TYPE_CONFIG1:
770 		CHECK_TYPE_VAL(NUM);
771 		attr->config1 = term->val.num;
772 		break;
773 	case PARSE_EVENTS__TERM_TYPE_CONFIG2:
774 		CHECK_TYPE_VAL(NUM);
775 		attr->config2 = term->val.num;
776 		break;
777 	case PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD:
778 		CHECK_TYPE_VAL(NUM);
779 		break;
780 	case PARSE_EVENTS__TERM_TYPE_SAMPLE_FREQ:
781 		CHECK_TYPE_VAL(NUM);
782 		break;
783 	case PARSE_EVENTS__TERM_TYPE_BRANCH_SAMPLE_TYPE:
784 		/*
785 		 * TODO uncomment when the field is available
786 		 * attr->branch_sample_type = term->val.num;
787 		 */
788 		break;
789 	case PARSE_EVENTS__TERM_TYPE_TIME:
790 		CHECK_TYPE_VAL(NUM);
791 		if (term->val.num > 1) {
792 			err->str = strdup("expected 0 or 1");
793 			err->idx = term->err_val;
794 			return -EINVAL;
795 		}
796 		break;
797 	case PARSE_EVENTS__TERM_TYPE_CALLGRAPH:
798 		CHECK_TYPE_VAL(STR);
799 		break;
800 	case PARSE_EVENTS__TERM_TYPE_STACKSIZE:
801 		CHECK_TYPE_VAL(NUM);
802 		break;
803 	case PARSE_EVENTS__TERM_TYPE_INHERIT:
804 		CHECK_TYPE_VAL(NUM);
805 		break;
806 	case PARSE_EVENTS__TERM_TYPE_NOINHERIT:
807 		CHECK_TYPE_VAL(NUM);
808 		break;
809 	case PARSE_EVENTS__TERM_TYPE_NAME:
810 		CHECK_TYPE_VAL(STR);
811 		break;
812 	default:
813 		err->str = strdup("unknown term");
814 		err->idx = term->err_term;
815 		err->help = parse_events_formats_error_string(NULL);
816 		return -EINVAL;
817 	}
818 
819 	return 0;
820 #undef CHECK_TYPE_VAL
821 }
822 
config_term_pmu(struct perf_event_attr * attr,struct parse_events_term * term,struct parse_events_error * err)823 static int config_term_pmu(struct perf_event_attr *attr,
824 			   struct parse_events_term *term,
825 			   struct parse_events_error *err)
826 {
827 	if (term->type_term == PARSE_EVENTS__TERM_TYPE_USER)
828 		/*
829 		 * Always succeed for sysfs terms, as we dont know
830 		 * at this point what type they need to have.
831 		 */
832 		return 0;
833 	else
834 		return config_term_common(attr, term, err);
835 }
836 
config_term_tracepoint(struct perf_event_attr * attr,struct parse_events_term * term,struct parse_events_error * err)837 static int config_term_tracepoint(struct perf_event_attr *attr,
838 				  struct parse_events_term *term,
839 				  struct parse_events_error *err)
840 {
841 	switch (term->type_term) {
842 	case PARSE_EVENTS__TERM_TYPE_CALLGRAPH:
843 	case PARSE_EVENTS__TERM_TYPE_STACKSIZE:
844 	case PARSE_EVENTS__TERM_TYPE_INHERIT:
845 	case PARSE_EVENTS__TERM_TYPE_NOINHERIT:
846 		return config_term_common(attr, term, err);
847 	default:
848 		if (err) {
849 			err->idx = term->err_term;
850 			err->str = strdup("unknown term");
851 			err->help = strdup("valid terms: call-graph,stack-size\n");
852 		}
853 		return -EINVAL;
854 	}
855 
856 	return 0;
857 }
858 
config_attr(struct perf_event_attr * attr,struct list_head * head,struct parse_events_error * err,config_term_func_t config_term)859 static int config_attr(struct perf_event_attr *attr,
860 		       struct list_head *head,
861 		       struct parse_events_error *err,
862 		       config_term_func_t config_term)
863 {
864 	struct parse_events_term *term;
865 
866 	list_for_each_entry(term, head, list)
867 		if (config_term(attr, term, err))
868 			return -EINVAL;
869 
870 	return 0;
871 }
872 
get_config_terms(struct list_head * head_config,struct list_head * head_terms __maybe_unused)873 static int get_config_terms(struct list_head *head_config,
874 			    struct list_head *head_terms __maybe_unused)
875 {
876 #define ADD_CONFIG_TERM(__type, __name, __val)			\
877 do {								\
878 	struct perf_evsel_config_term *__t;			\
879 								\
880 	__t = zalloc(sizeof(*__t));				\
881 	if (!__t)						\
882 		return -ENOMEM;					\
883 								\
884 	INIT_LIST_HEAD(&__t->list);				\
885 	__t->type       = PERF_EVSEL__CONFIG_TERM_ ## __type;	\
886 	__t->val.__name = __val;				\
887 	list_add_tail(&__t->list, head_terms);			\
888 } while (0)
889 
890 	struct parse_events_term *term;
891 
892 	list_for_each_entry(term, head_config, list) {
893 		switch (term->type_term) {
894 		case PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD:
895 			ADD_CONFIG_TERM(PERIOD, period, term->val.num);
896 			break;
897 		case PARSE_EVENTS__TERM_TYPE_SAMPLE_FREQ:
898 			ADD_CONFIG_TERM(FREQ, freq, term->val.num);
899 			break;
900 		case PARSE_EVENTS__TERM_TYPE_TIME:
901 			ADD_CONFIG_TERM(TIME, time, term->val.num);
902 			break;
903 		case PARSE_EVENTS__TERM_TYPE_CALLGRAPH:
904 			ADD_CONFIG_TERM(CALLGRAPH, callgraph, term->val.str);
905 			break;
906 		case PARSE_EVENTS__TERM_TYPE_STACKSIZE:
907 			ADD_CONFIG_TERM(STACK_USER, stack_user, term->val.num);
908 			break;
909 		case PARSE_EVENTS__TERM_TYPE_INHERIT:
910 			ADD_CONFIG_TERM(INHERIT, inherit, term->val.num ? 1 : 0);
911 			break;
912 		case PARSE_EVENTS__TERM_TYPE_NOINHERIT:
913 			ADD_CONFIG_TERM(INHERIT, inherit, term->val.num ? 0 : 1);
914 			break;
915 		default:
916 			break;
917 		}
918 	}
919 #undef ADD_EVSEL_CONFIG
920 	return 0;
921 }
922 
parse_events_add_tracepoint(struct list_head * list,int * idx,char * sys,char * event,struct parse_events_error * err,struct list_head * head_config)923 int parse_events_add_tracepoint(struct list_head *list, int *idx,
924 				char *sys, char *event,
925 				struct parse_events_error *err,
926 				struct list_head *head_config)
927 {
928 	if (head_config) {
929 		struct perf_event_attr attr;
930 
931 		if (config_attr(&attr, head_config, err,
932 				config_term_tracepoint))
933 			return -EINVAL;
934 	}
935 
936 	if (strpbrk(sys, "*?"))
937 		return add_tracepoint_multi_sys(list, idx, sys, event,
938 						err, head_config);
939 	else
940 		return add_tracepoint_event(list, idx, sys, event,
941 					    err, head_config);
942 }
943 
parse_events_add_numeric(struct parse_events_evlist * data,struct list_head * list,u32 type,u64 config,struct list_head * head_config)944 int parse_events_add_numeric(struct parse_events_evlist *data,
945 			     struct list_head *list,
946 			     u32 type, u64 config,
947 			     struct list_head *head_config)
948 {
949 	struct perf_event_attr attr;
950 	LIST_HEAD(config_terms);
951 
952 	memset(&attr, 0, sizeof(attr));
953 	attr.type = type;
954 	attr.config = config;
955 
956 	if (head_config) {
957 		if (config_attr(&attr, head_config, data->error,
958 				config_term_common))
959 			return -EINVAL;
960 
961 		if (get_config_terms(head_config, &config_terms))
962 			return -ENOMEM;
963 	}
964 
965 	return add_event(list, &data->idx, &attr, NULL, &config_terms);
966 }
967 
parse_events__is_name_term(struct parse_events_term * term)968 static int parse_events__is_name_term(struct parse_events_term *term)
969 {
970 	return term->type_term == PARSE_EVENTS__TERM_TYPE_NAME;
971 }
972 
pmu_event_name(struct list_head * head_terms)973 static char *pmu_event_name(struct list_head *head_terms)
974 {
975 	struct parse_events_term *term;
976 
977 	list_for_each_entry(term, head_terms, list)
978 		if (parse_events__is_name_term(term))
979 			return term->val.str;
980 
981 	return NULL;
982 }
983 
parse_events_add_pmu(struct parse_events_evlist * data,struct list_head * list,char * name,struct list_head * head_config)984 int parse_events_add_pmu(struct parse_events_evlist *data,
985 			 struct list_head *list, char *name,
986 			 struct list_head *head_config)
987 {
988 	struct perf_event_attr attr;
989 	struct perf_pmu_info info;
990 	struct perf_pmu *pmu;
991 	struct perf_evsel *evsel;
992 	LIST_HEAD(config_terms);
993 
994 	pmu = perf_pmu__find(name);
995 	if (!pmu)
996 		return -EINVAL;
997 
998 	if (pmu->default_config) {
999 		memcpy(&attr, pmu->default_config,
1000 		       sizeof(struct perf_event_attr));
1001 	} else {
1002 		memset(&attr, 0, sizeof(attr));
1003 	}
1004 
1005 	if (!head_config) {
1006 		attr.type = pmu->type;
1007 		evsel = __add_event(list, &data->idx, &attr, NULL, pmu->cpus, NULL);
1008 		return evsel ? 0 : -ENOMEM;
1009 	}
1010 
1011 	if (perf_pmu__check_alias(pmu, head_config, &info))
1012 		return -EINVAL;
1013 
1014 	/*
1015 	 * Configure hardcoded terms first, no need to check
1016 	 * return value when called with fail == 0 ;)
1017 	 */
1018 	if (config_attr(&attr, head_config, data->error, config_term_pmu))
1019 		return -EINVAL;
1020 
1021 	if (get_config_terms(head_config, &config_terms))
1022 		return -ENOMEM;
1023 
1024 	if (perf_pmu__config(pmu, &attr, head_config, data->error))
1025 		return -EINVAL;
1026 
1027 	evsel = __add_event(list, &data->idx, &attr,
1028 			    pmu_event_name(head_config), pmu->cpus,
1029 			    &config_terms);
1030 	if (evsel) {
1031 		evsel->unit = info.unit;
1032 		evsel->scale = info.scale;
1033 		evsel->per_pkg = info.per_pkg;
1034 		evsel->snapshot = info.snapshot;
1035 	}
1036 
1037 	return evsel ? 0 : -ENOMEM;
1038 }
1039 
parse_events__modifier_group(struct list_head * list,char * event_mod)1040 int parse_events__modifier_group(struct list_head *list,
1041 				 char *event_mod)
1042 {
1043 	return parse_events__modifier_event(list, event_mod, true);
1044 }
1045 
parse_events__set_leader(char * name,struct list_head * list)1046 void parse_events__set_leader(char *name, struct list_head *list)
1047 {
1048 	struct perf_evsel *leader;
1049 
1050 	if (list_empty(list)) {
1051 		WARN_ONCE(true, "WARNING: failed to set leader: empty list");
1052 		return;
1053 	}
1054 
1055 	__perf_evlist__set_leader(list);
1056 	leader = list_entry(list->next, struct perf_evsel, node);
1057 	leader->group_name = name ? strdup(name) : NULL;
1058 }
1059 
1060 /* list_event is assumed to point to malloc'ed memory */
parse_events_update_lists(struct list_head * list_event,struct list_head * list_all)1061 void parse_events_update_lists(struct list_head *list_event,
1062 			       struct list_head *list_all)
1063 {
1064 	/*
1065 	 * Called for single event definition. Update the
1066 	 * 'all event' list, and reinit the 'single event'
1067 	 * list, for next event definition.
1068 	 */
1069 	list_splice_tail(list_event, list_all);
1070 	free(list_event);
1071 }
1072 
1073 struct event_modifier {
1074 	int eu;
1075 	int ek;
1076 	int eh;
1077 	int eH;
1078 	int eG;
1079 	int eI;
1080 	int precise;
1081 	int precise_max;
1082 	int exclude_GH;
1083 	int sample_read;
1084 	int pinned;
1085 };
1086 
get_event_modifier(struct event_modifier * mod,char * str,struct perf_evsel * evsel)1087 static int get_event_modifier(struct event_modifier *mod, char *str,
1088 			       struct perf_evsel *evsel)
1089 {
1090 	int eu = evsel ? evsel->attr.exclude_user : 0;
1091 	int ek = evsel ? evsel->attr.exclude_kernel : 0;
1092 	int eh = evsel ? evsel->attr.exclude_hv : 0;
1093 	int eH = evsel ? evsel->attr.exclude_host : 0;
1094 	int eG = evsel ? evsel->attr.exclude_guest : 0;
1095 	int eI = evsel ? evsel->attr.exclude_idle : 0;
1096 	int precise = evsel ? evsel->attr.precise_ip : 0;
1097 	int precise_max = 0;
1098 	int sample_read = 0;
1099 	int pinned = evsel ? evsel->attr.pinned : 0;
1100 
1101 	int exclude = eu | ek | eh;
1102 	int exclude_GH = evsel ? evsel->exclude_GH : 0;
1103 
1104 	memset(mod, 0, sizeof(*mod));
1105 
1106 	while (*str) {
1107 		if (*str == 'u') {
1108 			if (!exclude)
1109 				exclude = eu = ek = eh = 1;
1110 			eu = 0;
1111 		} else if (*str == 'k') {
1112 			if (!exclude)
1113 				exclude = eu = ek = eh = 1;
1114 			ek = 0;
1115 		} else if (*str == 'h') {
1116 			if (!exclude)
1117 				exclude = eu = ek = eh = 1;
1118 			eh = 0;
1119 		} else if (*str == 'G') {
1120 			if (!exclude_GH)
1121 				exclude_GH = eG = eH = 1;
1122 			eG = 0;
1123 		} else if (*str == 'H') {
1124 			if (!exclude_GH)
1125 				exclude_GH = eG = eH = 1;
1126 			eH = 0;
1127 		} else if (*str == 'I') {
1128 			eI = 1;
1129 		} else if (*str == 'p') {
1130 			precise++;
1131 			/* use of precise requires exclude_guest */
1132 			if (!exclude_GH)
1133 				eG = 1;
1134 		} else if (*str == 'P') {
1135 			precise_max = 1;
1136 		} else if (*str == 'S') {
1137 			sample_read = 1;
1138 		} else if (*str == 'D') {
1139 			pinned = 1;
1140 		} else
1141 			break;
1142 
1143 		++str;
1144 	}
1145 
1146 	/*
1147 	 * precise ip:
1148 	 *
1149 	 *  0 - SAMPLE_IP can have arbitrary skid
1150 	 *  1 - SAMPLE_IP must have constant skid
1151 	 *  2 - SAMPLE_IP requested to have 0 skid
1152 	 *  3 - SAMPLE_IP must have 0 skid
1153 	 *
1154 	 *  See also PERF_RECORD_MISC_EXACT_IP
1155 	 */
1156 	if (precise > 3)
1157 		return -EINVAL;
1158 
1159 	mod->eu = eu;
1160 	mod->ek = ek;
1161 	mod->eh = eh;
1162 	mod->eH = eH;
1163 	mod->eG = eG;
1164 	mod->eI = eI;
1165 	mod->precise = precise;
1166 	mod->precise_max = precise_max;
1167 	mod->exclude_GH = exclude_GH;
1168 	mod->sample_read = sample_read;
1169 	mod->pinned = pinned;
1170 
1171 	return 0;
1172 }
1173 
1174 /*
1175  * Basic modifier sanity check to validate it contains only one
1176  * instance of any modifier (apart from 'p') present.
1177  */
check_modifier(char * str)1178 static int check_modifier(char *str)
1179 {
1180 	char *p = str;
1181 
1182 	/* The sizeof includes 0 byte as well. */
1183 	if (strlen(str) > (sizeof("ukhGHpppPSDI") - 1))
1184 		return -1;
1185 
1186 	while (*p) {
1187 		if (*p != 'p' && strchr(p + 1, *p))
1188 			return -1;
1189 		p++;
1190 	}
1191 
1192 	return 0;
1193 }
1194 
parse_events__modifier_event(struct list_head * list,char * str,bool add)1195 int parse_events__modifier_event(struct list_head *list, char *str, bool add)
1196 {
1197 	struct perf_evsel *evsel;
1198 	struct event_modifier mod;
1199 
1200 	if (str == NULL)
1201 		return 0;
1202 
1203 	if (check_modifier(str))
1204 		return -EINVAL;
1205 
1206 	if (!add && get_event_modifier(&mod, str, NULL))
1207 		return -EINVAL;
1208 
1209 	__evlist__for_each(list, evsel) {
1210 		if (add && get_event_modifier(&mod, str, evsel))
1211 			return -EINVAL;
1212 
1213 		evsel->attr.exclude_user   = mod.eu;
1214 		evsel->attr.exclude_kernel = mod.ek;
1215 		evsel->attr.exclude_hv     = mod.eh;
1216 		evsel->attr.precise_ip     = mod.precise;
1217 		evsel->attr.exclude_host   = mod.eH;
1218 		evsel->attr.exclude_guest  = mod.eG;
1219 		evsel->attr.exclude_idle   = mod.eI;
1220 		evsel->exclude_GH          = mod.exclude_GH;
1221 		evsel->sample_read         = mod.sample_read;
1222 		evsel->precise_max         = mod.precise_max;
1223 
1224 		if (perf_evsel__is_group_leader(evsel))
1225 			evsel->attr.pinned = mod.pinned;
1226 	}
1227 
1228 	return 0;
1229 }
1230 
parse_events_name(struct list_head * list,char * name)1231 int parse_events_name(struct list_head *list, char *name)
1232 {
1233 	struct perf_evsel *evsel;
1234 
1235 	__evlist__for_each(list, evsel) {
1236 		if (!evsel->name)
1237 			evsel->name = strdup(name);
1238 	}
1239 
1240 	return 0;
1241 }
1242 
1243 static int
comp_pmu(const void * p1,const void * p2)1244 comp_pmu(const void *p1, const void *p2)
1245 {
1246 	struct perf_pmu_event_symbol *pmu1 = (struct perf_pmu_event_symbol *) p1;
1247 	struct perf_pmu_event_symbol *pmu2 = (struct perf_pmu_event_symbol *) p2;
1248 
1249 	return strcmp(pmu1->symbol, pmu2->symbol);
1250 }
1251 
perf_pmu__parse_cleanup(void)1252 static void perf_pmu__parse_cleanup(void)
1253 {
1254 	if (perf_pmu_events_list_num > 0) {
1255 		struct perf_pmu_event_symbol *p;
1256 		int i;
1257 
1258 		for (i = 0; i < perf_pmu_events_list_num; i++) {
1259 			p = perf_pmu_events_list + i;
1260 			free(p->symbol);
1261 		}
1262 		free(perf_pmu_events_list);
1263 		perf_pmu_events_list = NULL;
1264 		perf_pmu_events_list_num = 0;
1265 	}
1266 }
1267 
1268 #define SET_SYMBOL(str, stype)		\
1269 do {					\
1270 	p->symbol = str;		\
1271 	if (!p->symbol)			\
1272 		goto err;		\
1273 	p->type = stype;		\
1274 } while (0)
1275 
1276 /*
1277  * Read the pmu events list from sysfs
1278  * Save it into perf_pmu_events_list
1279  */
perf_pmu__parse_init(void)1280 static void perf_pmu__parse_init(void)
1281 {
1282 
1283 	struct perf_pmu *pmu = NULL;
1284 	struct perf_pmu_alias *alias;
1285 	int len = 0;
1286 
1287 	pmu = perf_pmu__find("cpu");
1288 	if ((pmu == NULL) || list_empty(&pmu->aliases)) {
1289 		perf_pmu_events_list_num = -1;
1290 		return;
1291 	}
1292 	list_for_each_entry(alias, &pmu->aliases, list) {
1293 		if (strchr(alias->name, '-'))
1294 			len++;
1295 		len++;
1296 	}
1297 	perf_pmu_events_list = malloc(sizeof(struct perf_pmu_event_symbol) * len);
1298 	if (!perf_pmu_events_list)
1299 		return;
1300 	perf_pmu_events_list_num = len;
1301 
1302 	len = 0;
1303 	list_for_each_entry(alias, &pmu->aliases, list) {
1304 		struct perf_pmu_event_symbol *p = perf_pmu_events_list + len;
1305 		char *tmp = strchr(alias->name, '-');
1306 
1307 		if (tmp != NULL) {
1308 			SET_SYMBOL(strndup(alias->name, tmp - alias->name),
1309 					PMU_EVENT_SYMBOL_PREFIX);
1310 			p++;
1311 			SET_SYMBOL(strdup(++tmp), PMU_EVENT_SYMBOL_SUFFIX);
1312 			len += 2;
1313 		} else {
1314 			SET_SYMBOL(strdup(alias->name), PMU_EVENT_SYMBOL);
1315 			len++;
1316 		}
1317 	}
1318 	qsort(perf_pmu_events_list, len,
1319 		sizeof(struct perf_pmu_event_symbol), comp_pmu);
1320 
1321 	return;
1322 err:
1323 	perf_pmu__parse_cleanup();
1324 }
1325 
1326 enum perf_pmu_event_symbol_type
perf_pmu__parse_check(const char * name)1327 perf_pmu__parse_check(const char *name)
1328 {
1329 	struct perf_pmu_event_symbol p, *r;
1330 
1331 	/* scan kernel pmu events from sysfs if needed */
1332 	if (perf_pmu_events_list_num == 0)
1333 		perf_pmu__parse_init();
1334 	/*
1335 	 * name "cpu" could be prefix of cpu-cycles or cpu// events.
1336 	 * cpu-cycles has been handled by hardcode.
1337 	 * So it must be cpu// events, not kernel pmu event.
1338 	 */
1339 	if ((perf_pmu_events_list_num <= 0) || !strcmp(name, "cpu"))
1340 		return PMU_EVENT_SYMBOL_ERR;
1341 
1342 	p.symbol = strdup(name);
1343 	r = bsearch(&p, perf_pmu_events_list,
1344 			(size_t) perf_pmu_events_list_num,
1345 			sizeof(struct perf_pmu_event_symbol), comp_pmu);
1346 	free(p.symbol);
1347 	return r ? r->type : PMU_EVENT_SYMBOL_ERR;
1348 }
1349 
parse_events__scanner(const char * str,void * data,int start_token)1350 static int parse_events__scanner(const char *str, void *data, int start_token)
1351 {
1352 	YY_BUFFER_STATE buffer;
1353 	void *scanner;
1354 	int ret;
1355 
1356 	ret = parse_events_lex_init_extra(start_token, &scanner);
1357 	if (ret)
1358 		return ret;
1359 
1360 	buffer = parse_events__scan_string(str, scanner);
1361 
1362 #ifdef PARSER_DEBUG
1363 	parse_events_debug = 1;
1364 #endif
1365 	ret = parse_events_parse(data, scanner);
1366 
1367 	parse_events__flush_buffer(buffer, scanner);
1368 	parse_events__delete_buffer(buffer, scanner);
1369 	parse_events_lex_destroy(scanner);
1370 	return ret;
1371 }
1372 
1373 /*
1374  * parse event config string, return a list of event terms.
1375  */
parse_events_terms(struct list_head * terms,const char * str)1376 int parse_events_terms(struct list_head *terms, const char *str)
1377 {
1378 	struct parse_events_terms data = {
1379 		.terms = NULL,
1380 	};
1381 	int ret;
1382 
1383 	ret = parse_events__scanner(str, &data, PE_START_TERMS);
1384 	if (!ret) {
1385 		list_splice(data.terms, terms);
1386 		zfree(&data.terms);
1387 		return 0;
1388 	}
1389 
1390 	if (data.terms)
1391 		parse_events__free_terms(data.terms);
1392 	return ret;
1393 }
1394 
parse_events(struct perf_evlist * evlist,const char * str,struct parse_events_error * err)1395 int parse_events(struct perf_evlist *evlist, const char *str,
1396 		 struct parse_events_error *err)
1397 {
1398 	struct parse_events_evlist data = {
1399 		.list  = LIST_HEAD_INIT(data.list),
1400 		.idx   = evlist->nr_entries,
1401 		.error = err,
1402 	};
1403 	int ret;
1404 
1405 	ret = parse_events__scanner(str, &data, PE_START_EVENTS);
1406 	perf_pmu__parse_cleanup();
1407 	if (!ret) {
1408 		struct perf_evsel *last;
1409 
1410 		if (list_empty(&data.list)) {
1411 			WARN_ONCE(true, "WARNING: event parser found nothing");
1412 			return -1;
1413 		}
1414 
1415 		perf_evlist__splice_list_tail(evlist, &data.list);
1416 		evlist->nr_groups += data.nr_groups;
1417 		last = perf_evlist__last(evlist);
1418 		last->cmdline_group_boundary = true;
1419 
1420 		return 0;
1421 	}
1422 
1423 	/*
1424 	 * There are 2 users - builtin-record and builtin-test objects.
1425 	 * Both call perf_evlist__delete in case of error, so we dont
1426 	 * need to bother.
1427 	 */
1428 	return ret;
1429 }
1430 
1431 #define MAX_WIDTH 1000
get_term_width(void)1432 static int get_term_width(void)
1433 {
1434 	struct winsize ws;
1435 
1436 	get_term_dimensions(&ws);
1437 	return ws.ws_col > MAX_WIDTH ? MAX_WIDTH : ws.ws_col;
1438 }
1439 
parse_events_print_error(struct parse_events_error * err,const char * event)1440 static void parse_events_print_error(struct parse_events_error *err,
1441 				     const char *event)
1442 {
1443 	const char *str = "invalid or unsupported event: ";
1444 	char _buf[MAX_WIDTH];
1445 	char *buf = (char *) event;
1446 	int idx = 0;
1447 
1448 	if (err->str) {
1449 		/* -2 for extra '' in the final fprintf */
1450 		int width       = get_term_width() - 2;
1451 		int len_event   = strlen(event);
1452 		int len_str, max_len, cut = 0;
1453 
1454 		/*
1455 		 * Maximum error index indent, we will cut
1456 		 * the event string if it's bigger.
1457 		 */
1458 		int max_err_idx = 13;
1459 
1460 		/*
1461 		 * Let's be specific with the message when
1462 		 * we have the precise error.
1463 		 */
1464 		str     = "event syntax error: ";
1465 		len_str = strlen(str);
1466 		max_len = width - len_str;
1467 
1468 		buf = _buf;
1469 
1470 		/* We're cutting from the beggining. */
1471 		if (err->idx > max_err_idx)
1472 			cut = err->idx - max_err_idx;
1473 
1474 		strncpy(buf, event + cut, max_len);
1475 
1476 		/* Mark cut parts with '..' on both sides. */
1477 		if (cut)
1478 			buf[0] = buf[1] = '.';
1479 
1480 		if ((len_event - cut) > max_len) {
1481 			buf[max_len - 1] = buf[max_len - 2] = '.';
1482 			buf[max_len] = 0;
1483 		}
1484 
1485 		idx = len_str + err->idx - cut;
1486 	}
1487 
1488 	fprintf(stderr, "%s'%s'\n", str, buf);
1489 	if (idx) {
1490 		fprintf(stderr, "%*s\\___ %s\n", idx + 1, "", err->str);
1491 		if (err->help)
1492 			fprintf(stderr, "\n%s\n", err->help);
1493 		free(err->str);
1494 		free(err->help);
1495 	}
1496 
1497 	fprintf(stderr, "Run 'perf list' for a list of valid events\n");
1498 }
1499 
1500 #undef MAX_WIDTH
1501 
parse_events_option(const struct option * opt,const char * str,int unset __maybe_unused)1502 int parse_events_option(const struct option *opt, const char *str,
1503 			int unset __maybe_unused)
1504 {
1505 	struct perf_evlist *evlist = *(struct perf_evlist **)opt->value;
1506 	struct parse_events_error err = { .idx = 0, };
1507 	int ret = parse_events(evlist, str, &err);
1508 
1509 	if (ret)
1510 		parse_events_print_error(&err, str);
1511 
1512 	return ret;
1513 }
1514 
1515 static int
foreach_evsel_in_last_glob(struct perf_evlist * evlist,int (* func)(struct perf_evsel * evsel,const void * arg),const void * arg)1516 foreach_evsel_in_last_glob(struct perf_evlist *evlist,
1517 			   int (*func)(struct perf_evsel *evsel,
1518 				       const void *arg),
1519 			   const void *arg)
1520 {
1521 	struct perf_evsel *last = NULL;
1522 	int err;
1523 
1524 	/*
1525 	 * Don't return when list_empty, give func a chance to report
1526 	 * error when it found last == NULL.
1527 	 *
1528 	 * So no need to WARN here, let *func do this.
1529 	 */
1530 	if (evlist->nr_entries > 0)
1531 		last = perf_evlist__last(evlist);
1532 
1533 	do {
1534 		err = (*func)(last, arg);
1535 		if (err)
1536 			return -1;
1537 		if (!last)
1538 			return 0;
1539 
1540 		if (last->node.prev == &evlist->entries)
1541 			return 0;
1542 		last = list_entry(last->node.prev, struct perf_evsel, node);
1543 	} while (!last->cmdline_group_boundary);
1544 
1545 	return 0;
1546 }
1547 
set_filter(struct perf_evsel * evsel,const void * arg)1548 static int set_filter(struct perf_evsel *evsel, const void *arg)
1549 {
1550 	const char *str = arg;
1551 
1552 	if (evsel == NULL || evsel->attr.type != PERF_TYPE_TRACEPOINT) {
1553 		fprintf(stderr,
1554 			"--filter option should follow a -e tracepoint option\n");
1555 		return -1;
1556 	}
1557 
1558 	if (perf_evsel__append_filter(evsel, "&&", str) < 0) {
1559 		fprintf(stderr,
1560 			"not enough memory to hold filter string\n");
1561 		return -1;
1562 	}
1563 
1564 	return 0;
1565 }
1566 
parse_filter(const struct option * opt,const char * str,int unset __maybe_unused)1567 int parse_filter(const struct option *opt, const char *str,
1568 		 int unset __maybe_unused)
1569 {
1570 	struct perf_evlist *evlist = *(struct perf_evlist **)opt->value;
1571 
1572 	return foreach_evsel_in_last_glob(evlist, set_filter,
1573 					  (const void *)str);
1574 }
1575 
add_exclude_perf_filter(struct perf_evsel * evsel,const void * arg __maybe_unused)1576 static int add_exclude_perf_filter(struct perf_evsel *evsel,
1577 				   const void *arg __maybe_unused)
1578 {
1579 	char new_filter[64];
1580 
1581 	if (evsel == NULL || evsel->attr.type != PERF_TYPE_TRACEPOINT) {
1582 		fprintf(stderr,
1583 			"--exclude-perf option should follow a -e tracepoint option\n");
1584 		return -1;
1585 	}
1586 
1587 	snprintf(new_filter, sizeof(new_filter), "common_pid != %d", getpid());
1588 
1589 	if (perf_evsel__append_filter(evsel, "&&", new_filter) < 0) {
1590 		fprintf(stderr,
1591 			"not enough memory to hold filter string\n");
1592 		return -1;
1593 	}
1594 
1595 	return 0;
1596 }
1597 
exclude_perf(const struct option * opt,const char * arg __maybe_unused,int unset __maybe_unused)1598 int exclude_perf(const struct option *opt,
1599 		 const char *arg __maybe_unused,
1600 		 int unset __maybe_unused)
1601 {
1602 	struct perf_evlist *evlist = *(struct perf_evlist **)opt->value;
1603 
1604 	return foreach_evsel_in_last_glob(evlist, add_exclude_perf_filter,
1605 					  NULL);
1606 }
1607 
1608 static const char * const event_type_descriptors[] = {
1609 	"Hardware event",
1610 	"Software event",
1611 	"Tracepoint event",
1612 	"Hardware cache event",
1613 	"Raw hardware event descriptor",
1614 	"Hardware breakpoint",
1615 };
1616 
cmp_string(const void * a,const void * b)1617 static int cmp_string(const void *a, const void *b)
1618 {
1619 	const char * const *as = a;
1620 	const char * const *bs = b;
1621 
1622 	return strcmp(*as, *bs);
1623 }
1624 
1625 /*
1626  * Print the events from <debugfs_mount_point>/tracing/events
1627  */
1628 
print_tracepoint_events(const char * subsys_glob,const char * event_glob,bool name_only)1629 void print_tracepoint_events(const char *subsys_glob, const char *event_glob,
1630 			     bool name_only)
1631 {
1632 	DIR *sys_dir, *evt_dir;
1633 	struct dirent *sys_dirent, *evt_dirent;
1634 	char evt_path[MAXPATHLEN];
1635 	char dir_path[MAXPATHLEN];
1636 	char **evt_list = NULL;
1637 	unsigned int evt_i = 0, evt_num = 0;
1638 	bool evt_num_known = false;
1639 
1640 restart:
1641 	sys_dir = opendir(tracing_events_path);
1642 	if (!sys_dir)
1643 		return;
1644 
1645 	if (evt_num_known) {
1646 		evt_list = zalloc(sizeof(char *) * evt_num);
1647 		if (!evt_list)
1648 			goto out_close_sys_dir;
1649 	}
1650 
1651 	for_each_subsystem(sys_dir, sys_dirent) {
1652 		if (subsys_glob != NULL &&
1653 		    !strglobmatch(sys_dirent->d_name, subsys_glob))
1654 			continue;
1655 
1656 		snprintf(dir_path, MAXPATHLEN, "%s/%s", tracing_events_path,
1657 			 sys_dirent->d_name);
1658 		evt_dir = opendir(dir_path);
1659 		if (!evt_dir)
1660 			continue;
1661 
1662 		for_each_event(sys_dirent, evt_dir, evt_dirent) {
1663 			if (event_glob != NULL &&
1664 			    !strglobmatch(evt_dirent->d_name, event_glob))
1665 				continue;
1666 
1667 			if (!evt_num_known) {
1668 				evt_num++;
1669 				continue;
1670 			}
1671 
1672 			snprintf(evt_path, MAXPATHLEN, "%s:%s",
1673 				 sys_dirent->d_name, evt_dirent->d_name);
1674 
1675 			evt_list[evt_i] = strdup(evt_path);
1676 			if (evt_list[evt_i] == NULL)
1677 				goto out_close_evt_dir;
1678 			evt_i++;
1679 		}
1680 		closedir(evt_dir);
1681 	}
1682 	closedir(sys_dir);
1683 
1684 	if (!evt_num_known) {
1685 		evt_num_known = true;
1686 		goto restart;
1687 	}
1688 	qsort(evt_list, evt_num, sizeof(char *), cmp_string);
1689 	evt_i = 0;
1690 	while (evt_i < evt_num) {
1691 		if (name_only) {
1692 			printf("%s ", evt_list[evt_i++]);
1693 			continue;
1694 		}
1695 		printf("  %-50s [%s]\n", evt_list[evt_i++],
1696 				event_type_descriptors[PERF_TYPE_TRACEPOINT]);
1697 	}
1698 	if (evt_num && pager_in_use())
1699 		printf("\n");
1700 
1701 out_free:
1702 	evt_num = evt_i;
1703 	for (evt_i = 0; evt_i < evt_num; evt_i++)
1704 		zfree(&evt_list[evt_i]);
1705 	zfree(&evt_list);
1706 	return;
1707 
1708 out_close_evt_dir:
1709 	closedir(evt_dir);
1710 out_close_sys_dir:
1711 	closedir(sys_dir);
1712 
1713 	printf("FATAL: not enough memory to print %s\n",
1714 			event_type_descriptors[PERF_TYPE_TRACEPOINT]);
1715 	if (evt_list)
1716 		goto out_free;
1717 }
1718 
1719 /*
1720  * Check whether event is in <debugfs_mount_point>/tracing/events
1721  */
1722 
is_valid_tracepoint(const char * event_string)1723 int is_valid_tracepoint(const char *event_string)
1724 {
1725 	DIR *sys_dir, *evt_dir;
1726 	struct dirent *sys_dirent, *evt_dirent;
1727 	char evt_path[MAXPATHLEN];
1728 	char dir_path[MAXPATHLEN];
1729 
1730 	sys_dir = opendir(tracing_events_path);
1731 	if (!sys_dir)
1732 		return 0;
1733 
1734 	for_each_subsystem(sys_dir, sys_dirent) {
1735 
1736 		snprintf(dir_path, MAXPATHLEN, "%s/%s", tracing_events_path,
1737 			 sys_dirent->d_name);
1738 		evt_dir = opendir(dir_path);
1739 		if (!evt_dir)
1740 			continue;
1741 
1742 		for_each_event(sys_dirent, evt_dir, evt_dirent) {
1743 			snprintf(evt_path, MAXPATHLEN, "%s:%s",
1744 				 sys_dirent->d_name, evt_dirent->d_name);
1745 			if (!strcmp(evt_path, event_string)) {
1746 				closedir(evt_dir);
1747 				closedir(sys_dir);
1748 				return 1;
1749 			}
1750 		}
1751 		closedir(evt_dir);
1752 	}
1753 	closedir(sys_dir);
1754 	return 0;
1755 }
1756 
is_event_supported(u8 type,unsigned config)1757 static bool is_event_supported(u8 type, unsigned config)
1758 {
1759 	bool ret = true;
1760 	int open_return;
1761 	struct perf_evsel *evsel;
1762 	struct perf_event_attr attr = {
1763 		.type = type,
1764 		.config = config,
1765 		.disabled = 1,
1766 	};
1767 	struct {
1768 		struct thread_map map;
1769 		int threads[1];
1770 	} tmap = {
1771 		.map.nr	 = 1,
1772 		.threads = { 0 },
1773 	};
1774 
1775 	evsel = perf_evsel__new(&attr);
1776 	if (evsel) {
1777 		open_return = perf_evsel__open(evsel, NULL, &tmap.map);
1778 		ret = open_return >= 0;
1779 
1780 		if (open_return == -EACCES) {
1781 			/*
1782 			 * This happens if the paranoid value
1783 			 * /proc/sys/kernel/perf_event_paranoid is set to 2
1784 			 * Re-run with exclude_kernel set; we don't do that
1785 			 * by default as some ARM machines do not support it.
1786 			 *
1787 			 */
1788 			evsel->attr.exclude_kernel = 1;
1789 			ret = perf_evsel__open(evsel, NULL, &tmap.map) >= 0;
1790 		}
1791 		perf_evsel__delete(evsel);
1792 	}
1793 
1794 	return ret;
1795 }
1796 
print_hwcache_events(const char * event_glob,bool name_only)1797 int print_hwcache_events(const char *event_glob, bool name_only)
1798 {
1799 	unsigned int type, op, i, evt_i = 0, evt_num = 0;
1800 	char name[64];
1801 	char **evt_list = NULL;
1802 	bool evt_num_known = false;
1803 
1804 restart:
1805 	if (evt_num_known) {
1806 		evt_list = zalloc(sizeof(char *) * evt_num);
1807 		if (!evt_list)
1808 			goto out_enomem;
1809 	}
1810 
1811 	for (type = 0; type < PERF_COUNT_HW_CACHE_MAX; type++) {
1812 		for (op = 0; op < PERF_COUNT_HW_CACHE_OP_MAX; op++) {
1813 			/* skip invalid cache type */
1814 			if (!perf_evsel__is_cache_op_valid(type, op))
1815 				continue;
1816 
1817 			for (i = 0; i < PERF_COUNT_HW_CACHE_RESULT_MAX; i++) {
1818 				__perf_evsel__hw_cache_type_op_res_name(type, op, i,
1819 									name, sizeof(name));
1820 				if (event_glob != NULL && !strglobmatch(name, event_glob))
1821 					continue;
1822 
1823 				if (!is_event_supported(PERF_TYPE_HW_CACHE,
1824 							type | (op << 8) | (i << 16)))
1825 					continue;
1826 
1827 				if (!evt_num_known) {
1828 					evt_num++;
1829 					continue;
1830 				}
1831 
1832 				evt_list[evt_i] = strdup(name);
1833 				if (evt_list[evt_i] == NULL)
1834 					goto out_enomem;
1835 				evt_i++;
1836 			}
1837 		}
1838 	}
1839 
1840 	if (!evt_num_known) {
1841 		evt_num_known = true;
1842 		goto restart;
1843 	}
1844 	qsort(evt_list, evt_num, sizeof(char *), cmp_string);
1845 	evt_i = 0;
1846 	while (evt_i < evt_num) {
1847 		if (name_only) {
1848 			printf("%s ", evt_list[evt_i++]);
1849 			continue;
1850 		}
1851 		printf("  %-50s [%s]\n", evt_list[evt_i++],
1852 				event_type_descriptors[PERF_TYPE_HW_CACHE]);
1853 	}
1854 	if (evt_num && pager_in_use())
1855 		printf("\n");
1856 
1857 out_free:
1858 	evt_num = evt_i;
1859 	for (evt_i = 0; evt_i < evt_num; evt_i++)
1860 		zfree(&evt_list[evt_i]);
1861 	zfree(&evt_list);
1862 	return evt_num;
1863 
1864 out_enomem:
1865 	printf("FATAL: not enough memory to print %s\n", event_type_descriptors[PERF_TYPE_HW_CACHE]);
1866 	if (evt_list)
1867 		goto out_free;
1868 	return evt_num;
1869 }
1870 
print_symbol_events(const char * event_glob,unsigned type,struct event_symbol * syms,unsigned max,bool name_only)1871 void print_symbol_events(const char *event_glob, unsigned type,
1872 				struct event_symbol *syms, unsigned max,
1873 				bool name_only)
1874 {
1875 	unsigned int i, evt_i = 0, evt_num = 0;
1876 	char name[MAX_NAME_LEN];
1877 	char **evt_list = NULL;
1878 	bool evt_num_known = false;
1879 
1880 restart:
1881 	if (evt_num_known) {
1882 		evt_list = zalloc(sizeof(char *) * evt_num);
1883 		if (!evt_list)
1884 			goto out_enomem;
1885 		syms -= max;
1886 	}
1887 
1888 	for (i = 0; i < max; i++, syms++) {
1889 
1890 		if (event_glob != NULL && syms->symbol != NULL &&
1891 		    !(strglobmatch(syms->symbol, event_glob) ||
1892 		      (syms->alias && strglobmatch(syms->alias, event_glob))))
1893 			continue;
1894 
1895 		if (!is_event_supported(type, i))
1896 			continue;
1897 
1898 		if (!evt_num_known) {
1899 			evt_num++;
1900 			continue;
1901 		}
1902 
1903 		if (!name_only && strlen(syms->alias))
1904 			snprintf(name, MAX_NAME_LEN, "%s OR %s", syms->symbol, syms->alias);
1905 		else
1906 			strlcpy(name, syms->symbol, MAX_NAME_LEN);
1907 
1908 		evt_list[evt_i] = strdup(name);
1909 		if (evt_list[evt_i] == NULL)
1910 			goto out_enomem;
1911 		evt_i++;
1912 	}
1913 
1914 	if (!evt_num_known) {
1915 		evt_num_known = true;
1916 		goto restart;
1917 	}
1918 	qsort(evt_list, evt_num, sizeof(char *), cmp_string);
1919 	evt_i = 0;
1920 	while (evt_i < evt_num) {
1921 		if (name_only) {
1922 			printf("%s ", evt_list[evt_i++]);
1923 			continue;
1924 		}
1925 		printf("  %-50s [%s]\n", evt_list[evt_i++], event_type_descriptors[type]);
1926 	}
1927 	if (evt_num && pager_in_use())
1928 		printf("\n");
1929 
1930 out_free:
1931 	evt_num = evt_i;
1932 	for (evt_i = 0; evt_i < evt_num; evt_i++)
1933 		zfree(&evt_list[evt_i]);
1934 	zfree(&evt_list);
1935 	return;
1936 
1937 out_enomem:
1938 	printf("FATAL: not enough memory to print %s\n", event_type_descriptors[type]);
1939 	if (evt_list)
1940 		goto out_free;
1941 }
1942 
1943 /*
1944  * Print the help text for the event symbols:
1945  */
print_events(const char * event_glob,bool name_only)1946 void print_events(const char *event_glob, bool name_only)
1947 {
1948 	print_symbol_events(event_glob, PERF_TYPE_HARDWARE,
1949 			    event_symbols_hw, PERF_COUNT_HW_MAX, name_only);
1950 
1951 	print_symbol_events(event_glob, PERF_TYPE_SOFTWARE,
1952 			    event_symbols_sw, PERF_COUNT_SW_MAX, name_only);
1953 
1954 	print_hwcache_events(event_glob, name_only);
1955 
1956 	print_pmu_events(event_glob, name_only);
1957 
1958 	if (event_glob != NULL)
1959 		return;
1960 
1961 	if (!name_only) {
1962 		printf("  %-50s [%s]\n",
1963 		       "rNNN",
1964 		       event_type_descriptors[PERF_TYPE_RAW]);
1965 		printf("  %-50s [%s]\n",
1966 		       "cpu/t1=v1[,t2=v2,t3 ...]/modifier",
1967 		       event_type_descriptors[PERF_TYPE_RAW]);
1968 		if (pager_in_use())
1969 			printf("   (see 'man perf-list' on how to encode it)\n\n");
1970 
1971 		printf("  %-50s [%s]\n",
1972 		       "mem:<addr>[/len][:access]",
1973 			event_type_descriptors[PERF_TYPE_BREAKPOINT]);
1974 		if (pager_in_use())
1975 			printf("\n");
1976 	}
1977 
1978 	print_tracepoint_events(NULL, NULL, name_only);
1979 }
1980 
parse_events__is_hardcoded_term(struct parse_events_term * term)1981 int parse_events__is_hardcoded_term(struct parse_events_term *term)
1982 {
1983 	return term->type_term != PARSE_EVENTS__TERM_TYPE_USER;
1984 }
1985 
new_term(struct parse_events_term ** _term,int type_val,int type_term,char * config,char * str,u64 num,int err_term,int err_val)1986 static int new_term(struct parse_events_term **_term, int type_val,
1987 		    int type_term, char *config,
1988 		    char *str, u64 num, int err_term, int err_val)
1989 {
1990 	struct parse_events_term *term;
1991 
1992 	term = zalloc(sizeof(*term));
1993 	if (!term)
1994 		return -ENOMEM;
1995 
1996 	INIT_LIST_HEAD(&term->list);
1997 	term->type_val  = type_val;
1998 	term->type_term = type_term;
1999 	term->config = config;
2000 	term->err_term = err_term;
2001 	term->err_val  = err_val;
2002 
2003 	switch (type_val) {
2004 	case PARSE_EVENTS__TERM_TYPE_NUM:
2005 		term->val.num = num;
2006 		break;
2007 	case PARSE_EVENTS__TERM_TYPE_STR:
2008 		term->val.str = str;
2009 		break;
2010 	default:
2011 		free(term);
2012 		return -EINVAL;
2013 	}
2014 
2015 	*_term = term;
2016 	return 0;
2017 }
2018 
parse_events_term__num(struct parse_events_term ** term,int type_term,char * config,u64 num,void * loc_term_,void * loc_val_)2019 int parse_events_term__num(struct parse_events_term **term,
2020 			   int type_term, char *config, u64 num,
2021 			   void *loc_term_, void *loc_val_)
2022 {
2023 	YYLTYPE *loc_term = loc_term_;
2024 	YYLTYPE *loc_val = loc_val_;
2025 
2026 	return new_term(term, PARSE_EVENTS__TERM_TYPE_NUM, type_term,
2027 			config, NULL, num,
2028 			loc_term ? loc_term->first_column : 0,
2029 			loc_val ? loc_val->first_column : 0);
2030 }
2031 
parse_events_term__str(struct parse_events_term ** term,int type_term,char * config,char * str,void * loc_term_,void * loc_val_)2032 int parse_events_term__str(struct parse_events_term **term,
2033 			   int type_term, char *config, char *str,
2034 			   void *loc_term_, void *loc_val_)
2035 {
2036 	YYLTYPE *loc_term = loc_term_;
2037 	YYLTYPE *loc_val = loc_val_;
2038 
2039 	return new_term(term, PARSE_EVENTS__TERM_TYPE_STR, type_term,
2040 			config, str, 0,
2041 			loc_term ? loc_term->first_column : 0,
2042 			loc_val ? loc_val->first_column : 0);
2043 }
2044 
parse_events_term__sym_hw(struct parse_events_term ** term,char * config,unsigned idx)2045 int parse_events_term__sym_hw(struct parse_events_term **term,
2046 			      char *config, unsigned idx)
2047 {
2048 	struct event_symbol *sym;
2049 
2050 	BUG_ON(idx >= PERF_COUNT_HW_MAX);
2051 	sym = &event_symbols_hw[idx];
2052 
2053 	if (config)
2054 		return new_term(term, PARSE_EVENTS__TERM_TYPE_STR,
2055 				PARSE_EVENTS__TERM_TYPE_USER, config,
2056 				(char *) sym->symbol, 0, 0, 0);
2057 	else
2058 		return new_term(term, PARSE_EVENTS__TERM_TYPE_STR,
2059 				PARSE_EVENTS__TERM_TYPE_USER,
2060 				(char *) "event", (char *) sym->symbol,
2061 				0, 0, 0);
2062 }
2063 
parse_events_term__clone(struct parse_events_term ** new,struct parse_events_term * term)2064 int parse_events_term__clone(struct parse_events_term **new,
2065 			     struct parse_events_term *term)
2066 {
2067 	return new_term(new, term->type_val, term->type_term, term->config,
2068 			term->val.str, term->val.num,
2069 			term->err_term, term->err_val);
2070 }
2071 
parse_events__free_terms(struct list_head * terms)2072 void parse_events__free_terms(struct list_head *terms)
2073 {
2074 	struct parse_events_term *term, *h;
2075 
2076 	list_for_each_entry_safe(term, h, terms, list)
2077 		free(term);
2078 }
2079 
parse_events_evlist_error(struct parse_events_evlist * data,int idx,const char * str)2080 void parse_events_evlist_error(struct parse_events_evlist *data,
2081 			       int idx, const char *str)
2082 {
2083 	struct parse_events_error *err = data->error;
2084 
2085 	if (!err)
2086 		return;
2087 	err->idx = idx;
2088 	err->str = strdup(str);
2089 	WARN_ONCE(!err->str, "WARNING: failed to allocate error string");
2090 }
2091 
2092 /*
2093  * Return string contains valid config terms of an event.
2094  * @additional_terms: For terms such as PMU sysfs terms.
2095  */
parse_events_formats_error_string(char * additional_terms)2096 char *parse_events_formats_error_string(char *additional_terms)
2097 {
2098 	char *str;
2099 	static const char *static_terms = "config,config1,config2,name,"
2100 					  "period,freq,branch_type,time,"
2101 					  "call-graph,stack-size\n";
2102 
2103 	/* valid terms */
2104 	if (additional_terms) {
2105 		if (asprintf(&str, "valid terms: %s,%s",
2106 			     additional_terms, static_terms) < 0)
2107 			goto fail;
2108 	} else {
2109 		if (asprintf(&str, "valid terms: %s", static_terms) < 0)
2110 			goto fail;
2111 	}
2112 	return str;
2113 
2114 fail:
2115 	return NULL;
2116 }
2117