xref: /linux/tools/perf/util/parse-events.c (revision 39df730b09774bd860e39ea208a48d15078236cb)
1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
2d2709c7cSDavid Howells #include <linux/hw_breakpoint.h>
38dd2a131SJiri Olsa #include <linux/err.h>
476b31a29SArnaldo Carvalho de Melo #include <dirent.h>
5a43783aeSArnaldo Carvalho de Melo #include <errno.h>
686a5e0c2SArnaldo Carvalho de Melo #include <sys/ioctl.h>
7bafae98eSArnaldo Carvalho de Melo #include <sys/types.h>
8bafae98eSArnaldo Carvalho de Melo #include <sys/stat.h>
9bafae98eSArnaldo Carvalho de Melo #include <fcntl.h>
10391e4206SArnaldo Carvalho de Melo #include <sys/param.h>
11b0742e90SArnaldo Carvalho de Melo #include "term.h"
126b58e7f1SUlrich Drepper #include "../perf.h"
13361c99a6SArnaldo Carvalho de Melo #include "evlist.h"
1469aad6f1SArnaldo Carvalho de Melo #include "evsel.h"
154b6ab94eSJosh Poimboeuf #include <subcmd/parse-options.h>
1686470930SIngo Molnar #include "parse-events.h"
174b6ab94eSJosh Poimboeuf #include <subcmd/exec-cmd.h>
18a067558eSArnaldo Carvalho de Melo #include "string2.h"
198ec20b17SArnaldo Carvalho de Melo #include "strlist.h"
205aab621bSArnaldo Carvalho de Melo #include "symbol.h"
215beeded1SJason Baron #include "cache.h"
228755a8f2SArjan van de Ven #include "header.h"
2384c86ca1SWang Nan #include "bpf-loader.h"
246e81c74cSMasami Hiramatsu #include "debug.h"
25592d5a6bSJiri Olsa #include <api/fs/tracing_path.h>
26ac20de6fSZheng Yan #include "parse-events-bison.h"
2790e2b22dSJiri Olsa #define YY_EXTRA_TYPE int
2889812fc8SJiri Olsa #include "parse-events-flex.h"
295f537a26SJiri Olsa #include "pmu.h"
30b41f1cecSNamhyung Kim #include "thread_map.h"
31f30a79b0SJiri Olsa #include "cpumap.h"
3240218daeSMasami Hiramatsu #include "probe-file.h"
33b39b8393SJiri Olsa #include "asm/bug.h"
34ac12f676SAndi Kleen #include "util/parse-branch-options.h"
3571b0acceSAndi Kleen #include "metricgroup.h"
3689812fc8SJiri Olsa 
3789812fc8SJiri Olsa #define MAX_NAME_LEN 100
3886470930SIngo Molnar 
3982ba1f2fSJiri Olsa #ifdef PARSER_DEBUG
4082ba1f2fSJiri Olsa extern int parse_events_debug;
4182ba1f2fSJiri Olsa #endif
425d9cdc11SArnaldo Carvalho de Melo int parse_events_parse(void *parse_state, void *scanner);
43e637d177SHe Kuang static int get_config_terms(struct list_head *head_config,
44e637d177SHe Kuang 			    struct list_head *head_terms __maybe_unused);
45bcd3279fSFrederic Weisbecker 
46dcb4e102SKan Liang static struct perf_pmu_event_symbol *perf_pmu_events_list;
47dcb4e102SKan Liang /*
48dcb4e102SKan Liang  * The variable indicates the number of supported pmu event symbols.
49dcb4e102SKan Liang  * 0 means not initialized and ready to init
50dcb4e102SKan Liang  * -1 means failed to init, don't try anymore
51dcb4e102SKan Liang  * >0 is the number of supported pmu event symbols
52dcb4e102SKan Liang  */
53dcb4e102SKan Liang static int perf_pmu_events_list_num;
54dcb4e102SKan Liang 
55705750f2SYunlong Song struct event_symbol event_symbols_hw[PERF_COUNT_HW_MAX] = {
561dc12760SJiri Olsa 	[PERF_COUNT_HW_CPU_CYCLES] = {
571dc12760SJiri Olsa 		.symbol = "cpu-cycles",
581dc12760SJiri Olsa 		.alias  = "cycles",
591dc12760SJiri Olsa 	},
601dc12760SJiri Olsa 	[PERF_COUNT_HW_INSTRUCTIONS] = {
611dc12760SJiri Olsa 		.symbol = "instructions",
621dc12760SJiri Olsa 		.alias  = "",
631dc12760SJiri Olsa 	},
641dc12760SJiri Olsa 	[PERF_COUNT_HW_CACHE_REFERENCES] = {
651dc12760SJiri Olsa 		.symbol = "cache-references",
661dc12760SJiri Olsa 		.alias  = "",
671dc12760SJiri Olsa 	},
681dc12760SJiri Olsa 	[PERF_COUNT_HW_CACHE_MISSES] = {
691dc12760SJiri Olsa 		.symbol = "cache-misses",
701dc12760SJiri Olsa 		.alias  = "",
711dc12760SJiri Olsa 	},
721dc12760SJiri Olsa 	[PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = {
731dc12760SJiri Olsa 		.symbol = "branch-instructions",
741dc12760SJiri Olsa 		.alias  = "branches",
751dc12760SJiri Olsa 	},
761dc12760SJiri Olsa 	[PERF_COUNT_HW_BRANCH_MISSES] = {
771dc12760SJiri Olsa 		.symbol = "branch-misses",
781dc12760SJiri Olsa 		.alias  = "",
791dc12760SJiri Olsa 	},
801dc12760SJiri Olsa 	[PERF_COUNT_HW_BUS_CYCLES] = {
811dc12760SJiri Olsa 		.symbol = "bus-cycles",
821dc12760SJiri Olsa 		.alias  = "",
831dc12760SJiri Olsa 	},
841dc12760SJiri Olsa 	[PERF_COUNT_HW_STALLED_CYCLES_FRONTEND] = {
851dc12760SJiri Olsa 		.symbol = "stalled-cycles-frontend",
861dc12760SJiri Olsa 		.alias  = "idle-cycles-frontend",
871dc12760SJiri Olsa 	},
881dc12760SJiri Olsa 	[PERF_COUNT_HW_STALLED_CYCLES_BACKEND] = {
891dc12760SJiri Olsa 		.symbol = "stalled-cycles-backend",
901dc12760SJiri Olsa 		.alias  = "idle-cycles-backend",
911dc12760SJiri Olsa 	},
921dc12760SJiri Olsa 	[PERF_COUNT_HW_REF_CPU_CYCLES] = {
931dc12760SJiri Olsa 		.symbol = "ref-cycles",
941dc12760SJiri Olsa 		.alias  = "",
951dc12760SJiri Olsa 	},
961dc12760SJiri Olsa };
9786470930SIngo Molnar 
98705750f2SYunlong Song struct event_symbol event_symbols_sw[PERF_COUNT_SW_MAX] = {
991dc12760SJiri Olsa 	[PERF_COUNT_SW_CPU_CLOCK] = {
1001dc12760SJiri Olsa 		.symbol = "cpu-clock",
1011dc12760SJiri Olsa 		.alias  = "",
1021dc12760SJiri Olsa 	},
1031dc12760SJiri Olsa 	[PERF_COUNT_SW_TASK_CLOCK] = {
1041dc12760SJiri Olsa 		.symbol = "task-clock",
1051dc12760SJiri Olsa 		.alias  = "",
1061dc12760SJiri Olsa 	},
1071dc12760SJiri Olsa 	[PERF_COUNT_SW_PAGE_FAULTS] = {
1081dc12760SJiri Olsa 		.symbol = "page-faults",
1091dc12760SJiri Olsa 		.alias  = "faults",
1101dc12760SJiri Olsa 	},
1111dc12760SJiri Olsa 	[PERF_COUNT_SW_CONTEXT_SWITCHES] = {
1121dc12760SJiri Olsa 		.symbol = "context-switches",
1131dc12760SJiri Olsa 		.alias  = "cs",
1141dc12760SJiri Olsa 	},
1151dc12760SJiri Olsa 	[PERF_COUNT_SW_CPU_MIGRATIONS] = {
1161dc12760SJiri Olsa 		.symbol = "cpu-migrations",
1171dc12760SJiri Olsa 		.alias  = "migrations",
1181dc12760SJiri Olsa 	},
1191dc12760SJiri Olsa 	[PERF_COUNT_SW_PAGE_FAULTS_MIN] = {
1201dc12760SJiri Olsa 		.symbol = "minor-faults",
1211dc12760SJiri Olsa 		.alias  = "",
1221dc12760SJiri Olsa 	},
1231dc12760SJiri Olsa 	[PERF_COUNT_SW_PAGE_FAULTS_MAJ] = {
1241dc12760SJiri Olsa 		.symbol = "major-faults",
1251dc12760SJiri Olsa 		.alias  = "",
1261dc12760SJiri Olsa 	},
1271dc12760SJiri Olsa 	[PERF_COUNT_SW_ALIGNMENT_FAULTS] = {
1281dc12760SJiri Olsa 		.symbol = "alignment-faults",
1291dc12760SJiri Olsa 		.alias  = "",
1301dc12760SJiri Olsa 	},
1311dc12760SJiri Olsa 	[PERF_COUNT_SW_EMULATION_FAULTS] = {
1321dc12760SJiri Olsa 		.symbol = "emulation-faults",
1331dc12760SJiri Olsa 		.alias  = "",
1341dc12760SJiri Olsa 	},
135d22d1a2aSAdrian Hunter 	[PERF_COUNT_SW_DUMMY] = {
136d22d1a2aSAdrian Hunter 		.symbol = "dummy",
137d22d1a2aSAdrian Hunter 		.alias  = "",
138d22d1a2aSAdrian Hunter 	},
139bae9cc41SArnaldo Carvalho de Melo 	[PERF_COUNT_SW_BPF_OUTPUT] = {
140bae9cc41SArnaldo Carvalho de Melo 		.symbol = "bpf-output",
141bae9cc41SArnaldo Carvalho de Melo 		.alias  = "",
142bae9cc41SArnaldo Carvalho de Melo 	},
14386470930SIngo Molnar };
14486470930SIngo Molnar 
145cdd6c482SIngo Molnar #define __PERF_EVENT_FIELD(config, name) \
146cdd6c482SIngo Molnar 	((config & PERF_EVENT_##name##_MASK) >> PERF_EVENT_##name##_SHIFT)
14786470930SIngo Molnar 
148cdd6c482SIngo Molnar #define PERF_EVENT_RAW(config)		__PERF_EVENT_FIELD(config, RAW)
149cdd6c482SIngo Molnar #define PERF_EVENT_CONFIG(config)	__PERF_EVENT_FIELD(config, CONFIG)
150cdd6c482SIngo Molnar #define PERF_EVENT_TYPE(config)		__PERF_EVENT_FIELD(config, TYPE)
151cdd6c482SIngo Molnar #define PERF_EVENT_ID(config)		__PERF_EVENT_FIELD(config, EVENT)
15286470930SIngo Molnar 
15322a9f41bSArnaldo Carvalho de Melo #define for_each_subsystem(sys_dir, sys_dirent)			\
15422a9f41bSArnaldo Carvalho de Melo 	while ((sys_dirent = readdir(sys_dir)) != NULL)		\
15522a9f41bSArnaldo Carvalho de Melo 		if (sys_dirent->d_type == DT_DIR &&		\
15622a9f41bSArnaldo Carvalho de Melo 		    (strcmp(sys_dirent->d_name, ".")) &&	\
15722a9f41bSArnaldo Carvalho de Melo 		    (strcmp(sys_dirent->d_name, "..")))
158f6bdafefSJason Baron 
159c02cab22SArnaldo Carvalho de Melo static int tp_event_has_id(const char *dir_path, struct dirent *evt_dir)
160ae07b63fSPeter Zijlstra {
161ae07b63fSPeter Zijlstra 	char evt_path[MAXPATHLEN];
162ae07b63fSPeter Zijlstra 	int fd;
163ae07b63fSPeter Zijlstra 
164c02cab22SArnaldo Carvalho de Melo 	snprintf(evt_path, MAXPATHLEN, "%s/%s/id", dir_path, evt_dir->d_name);
165ae07b63fSPeter Zijlstra 	fd = open(evt_path, O_RDONLY);
166ae07b63fSPeter Zijlstra 	if (fd < 0)
167ae07b63fSPeter Zijlstra 		return -EINVAL;
168ae07b63fSPeter Zijlstra 	close(fd);
169ae07b63fSPeter Zijlstra 
170ae07b63fSPeter Zijlstra 	return 0;
171ae07b63fSPeter Zijlstra }
172ae07b63fSPeter Zijlstra 
173c02cab22SArnaldo Carvalho de Melo #define for_each_event(dir_path, evt_dir, evt_dirent)		\
17422a9f41bSArnaldo Carvalho de Melo 	while ((evt_dirent = readdir(evt_dir)) != NULL)		\
17522a9f41bSArnaldo Carvalho de Melo 		if (evt_dirent->d_type == DT_DIR &&		\
17622a9f41bSArnaldo Carvalho de Melo 		    (strcmp(evt_dirent->d_name, ".")) &&	\
17722a9f41bSArnaldo Carvalho de Melo 		    (strcmp(evt_dirent->d_name, "..")) &&	\
178c02cab22SArnaldo Carvalho de Melo 		    (!tp_event_has_id(dir_path, evt_dirent)))
179f6bdafefSJason Baron 
180270bbbe8SLi Zefan #define MAX_EVENT_LENGTH 512
181f6bdafefSJason Baron 
182f6bdafefSJason Baron 
1831ef2ed10SFrederic Weisbecker struct tracepoint_path *tracepoint_id_to_path(u64 config)
184f6bdafefSJason Baron {
1851ef2ed10SFrederic Weisbecker 	struct tracepoint_path *path = NULL;
186f6bdafefSJason Baron 	DIR *sys_dir, *evt_dir;
18722a9f41bSArnaldo Carvalho de Melo 	struct dirent *sys_dirent, *evt_dirent;
1888aa8a7c8SStephane Eranian 	char id_buf[24];
189725b1368SEric Dumazet 	int fd;
190f6bdafefSJason Baron 	u64 id;
191f6bdafefSJason Baron 	char evt_path[MAXPATHLEN];
19225a7d914SArnaldo Carvalho de Melo 	char *dir_path;
193f6bdafefSJason Baron 
1947014e0e3SArnaldo Carvalho de Melo 	sys_dir = tracing_events__opendir();
195f6bdafefSJason Baron 	if (!sys_dir)
196725b1368SEric Dumazet 		return NULL;
197f6bdafefSJason Baron 
19822a9f41bSArnaldo Carvalho de Melo 	for_each_subsystem(sys_dir, sys_dirent) {
19925a7d914SArnaldo Carvalho de Melo 		dir_path = get_events_file(sys_dirent->d_name);
20025a7d914SArnaldo Carvalho de Melo 		if (!dir_path)
20125a7d914SArnaldo Carvalho de Melo 			continue;
202725b1368SEric Dumazet 		evt_dir = opendir(dir_path);
203725b1368SEric Dumazet 		if (!evt_dir)
20425a7d914SArnaldo Carvalho de Melo 			goto next;
205725b1368SEric Dumazet 
206c02cab22SArnaldo Carvalho de Melo 		for_each_event(dir_path, evt_dir, evt_dirent) {
207725b1368SEric Dumazet 
20877f18153SJiri Olsa 			scnprintf(evt_path, MAXPATHLEN, "%s/%s/id", dir_path,
20922a9f41bSArnaldo Carvalho de Melo 				  evt_dirent->d_name);
210725b1368SEric Dumazet 			fd = open(evt_path, O_RDONLY);
211f6bdafefSJason Baron 			if (fd < 0)
212f6bdafefSJason Baron 				continue;
213f6bdafefSJason Baron 			if (read(fd, id_buf, sizeof(id_buf)) < 0) {
214f6bdafefSJason Baron 				close(fd);
215f6bdafefSJason Baron 				continue;
216f6bdafefSJason Baron 			}
217f6bdafefSJason Baron 			close(fd);
218f6bdafefSJason Baron 			id = atoll(id_buf);
219f6bdafefSJason Baron 			if (id == config) {
22025a7d914SArnaldo Carvalho de Melo 				put_events_file(dir_path);
221f6bdafefSJason Baron 				closedir(evt_dir);
222f6bdafefSJason Baron 				closedir(sys_dir);
22359b4caebSJulia Lawall 				path = zalloc(sizeof(*path));
2245aa365f2STaeung Song 				if (!path)
2255aa365f2STaeung Song 					return NULL;
2261ef2ed10SFrederic Weisbecker 				path->system = malloc(MAX_EVENT_LENGTH);
2271ef2ed10SFrederic Weisbecker 				if (!path->system) {
2281ef2ed10SFrederic Weisbecker 					free(path);
2291ef2ed10SFrederic Weisbecker 					return NULL;
2301ef2ed10SFrederic Weisbecker 				}
2311ef2ed10SFrederic Weisbecker 				path->name = malloc(MAX_EVENT_LENGTH);
2321ef2ed10SFrederic Weisbecker 				if (!path->name) {
23374cf249dSArnaldo Carvalho de Melo 					zfree(&path->system);
2341ef2ed10SFrederic Weisbecker 					free(path);
2351ef2ed10SFrederic Weisbecker 					return NULL;
2361ef2ed10SFrederic Weisbecker 				}
23722a9f41bSArnaldo Carvalho de Melo 				strncpy(path->system, sys_dirent->d_name,
2381ef2ed10SFrederic Weisbecker 					MAX_EVENT_LENGTH);
23922a9f41bSArnaldo Carvalho de Melo 				strncpy(path->name, evt_dirent->d_name,
2401ef2ed10SFrederic Weisbecker 					MAX_EVENT_LENGTH);
2411ef2ed10SFrederic Weisbecker 				return path;
242f6bdafefSJason Baron 			}
243f6bdafefSJason Baron 		}
244f6bdafefSJason Baron 		closedir(evt_dir);
24525a7d914SArnaldo Carvalho de Melo next:
24625a7d914SArnaldo Carvalho de Melo 		put_events_file(dir_path);
247f6bdafefSJason Baron 	}
248f6bdafefSJason Baron 
249f6bdafefSJason Baron 	closedir(sys_dir);
2501ef2ed10SFrederic Weisbecker 	return NULL;
2511ef2ed10SFrederic Weisbecker }
2521ef2ed10SFrederic Weisbecker 
253e7c93f09SNamhyung Kim struct tracepoint_path *tracepoint_name_to_path(const char *name)
254e7c93f09SNamhyung Kim {
255e7c93f09SNamhyung Kim 	struct tracepoint_path *path = zalloc(sizeof(*path));
256e7c93f09SNamhyung Kim 	char *str = strchr(name, ':');
257e7c93f09SNamhyung Kim 
258e7c93f09SNamhyung Kim 	if (path == NULL || str == NULL) {
259e7c93f09SNamhyung Kim 		free(path);
260e7c93f09SNamhyung Kim 		return NULL;
261e7c93f09SNamhyung Kim 	}
262e7c93f09SNamhyung Kim 
263e7c93f09SNamhyung Kim 	path->system = strndup(name, str - name);
264e7c93f09SNamhyung Kim 	path->name = strdup(str+1);
265e7c93f09SNamhyung Kim 
266e7c93f09SNamhyung Kim 	if (path->system == NULL || path->name == NULL) {
26774cf249dSArnaldo Carvalho de Melo 		zfree(&path->system);
26874cf249dSArnaldo Carvalho de Melo 		zfree(&path->name);
269506fde11STaeung Song 		zfree(&path);
270e7c93f09SNamhyung Kim 	}
271e7c93f09SNamhyung Kim 
272e7c93f09SNamhyung Kim 	return path;
273e7c93f09SNamhyung Kim }
274e7c93f09SNamhyung Kim 
2751424dc96SDavid Ahern const char *event_type(int type)
2761424dc96SDavid Ahern {
2771424dc96SDavid Ahern 	switch (type) {
2781424dc96SDavid Ahern 	case PERF_TYPE_HARDWARE:
2791424dc96SDavid Ahern 		return "hardware";
2801424dc96SDavid Ahern 
2811424dc96SDavid Ahern 	case PERF_TYPE_SOFTWARE:
2821424dc96SDavid Ahern 		return "software";
2831424dc96SDavid Ahern 
2841424dc96SDavid Ahern 	case PERF_TYPE_TRACEPOINT:
2851424dc96SDavid Ahern 		return "tracepoint";
2861424dc96SDavid Ahern 
2871424dc96SDavid Ahern 	case PERF_TYPE_HW_CACHE:
2881424dc96SDavid Ahern 		return "hardware-cache";
2891424dc96SDavid Ahern 
2901424dc96SDavid Ahern 	default:
2911424dc96SDavid Ahern 		break;
2921424dc96SDavid Ahern 	}
2931424dc96SDavid Ahern 
2941424dc96SDavid Ahern 	return "unknown";
2951424dc96SDavid Ahern }
2961424dc96SDavid Ahern 
297e814fdddSWang Nan static int parse_events__is_name_term(struct parse_events_term *term)
298e814fdddSWang Nan {
299e814fdddSWang Nan 	return term->type_term == PARSE_EVENTS__TERM_TYPE_NAME;
300e814fdddSWang Nan }
3017ae92e74SYan, Zheng 
302e814fdddSWang Nan static char *get_config_name(struct list_head *head_terms)
303e814fdddSWang Nan {
304e814fdddSWang Nan 	struct parse_events_term *term;
305e814fdddSWang Nan 
306e814fdddSWang Nan 	if (!head_terms)
307e814fdddSWang Nan 		return NULL;
308e814fdddSWang Nan 
309e814fdddSWang Nan 	list_for_each_entry(term, head_terms, list)
310e814fdddSWang Nan 		if (parse_events__is_name_term(term))
311e814fdddSWang Nan 			return term->val.str;
312e814fdddSWang Nan 
313e814fdddSWang Nan 	return NULL;
314e814fdddSWang Nan }
3157ae92e74SYan, Zheng 
316410136f5SStephane Eranian static struct perf_evsel *
317410136f5SStephane Eranian __add_event(struct list_head *list, int *idx,
3187ae92e74SYan, Zheng 	    struct perf_event_attr *attr,
31966ec1191SMark Rutland 	    char *name, struct perf_pmu *pmu,
32063ce8449SArnaldo Carvalho de Melo 	    struct list_head *config_terms, bool auto_merge_stats)
32189812fc8SJiri Olsa {
32289812fc8SJiri Olsa 	struct perf_evsel *evsel;
32366ec1191SMark Rutland 	struct cpu_map *cpus = pmu ? pmu->cpus : NULL;
32489812fc8SJiri Olsa 
32589812fc8SJiri Olsa 	event_attr_init(attr);
32689812fc8SJiri Olsa 
32775fc5ae5STaeung Song 	evsel = perf_evsel__new_idx(attr, *idx);
328c5cd8ac0SDavid Ahern 	if (!evsel)
329410136f5SStephane Eranian 		return NULL;
33089812fc8SJiri Olsa 
33175fc5ae5STaeung Song 	(*idx)++;
332f30a79b0SJiri Olsa 	evsel->cpus        = cpu_map__get(cpus);
333fce4d296SAdrian Hunter 	evsel->own_cpus    = cpu_map__get(cpus);
33466ec1191SMark Rutland 	evsel->system_wide = pmu ? pmu->is_uncore : false;
33563ce8449SArnaldo Carvalho de Melo 	evsel->auto_merge_stats = auto_merge_stats;
336f30a79b0SJiri Olsa 
3379db1763cSArnaldo Carvalho de Melo 	if (name)
33889812fc8SJiri Olsa 		evsel->name = strdup(name);
339930a2e29SJiri Olsa 
340930a2e29SJiri Olsa 	if (config_terms)
341930a2e29SJiri Olsa 		list_splice(config_terms, &evsel->config_terms);
342930a2e29SJiri Olsa 
343b847cbdcSJiri Olsa 	list_add_tail(&evsel->node, list);
344410136f5SStephane Eranian 	return evsel;
34589812fc8SJiri Olsa }
34689812fc8SJiri Olsa 
347c5cd8ac0SDavid Ahern static int add_event(struct list_head *list, int *idx,
348930a2e29SJiri Olsa 		     struct perf_event_attr *attr, char *name,
349930a2e29SJiri Olsa 		     struct list_head *config_terms)
3507ae92e74SYan, Zheng {
35163ce8449SArnaldo Carvalho de Melo 	return __add_event(list, idx, attr, name, NULL, config_terms, false) ? 0 : -ENOMEM;
3527ae92e74SYan, Zheng }
3537ae92e74SYan, Zheng 
3540b668bc9SArnaldo Carvalho de Melo static int parse_aliases(char *str, const char *names[][PERF_EVSEL__MAX_ALIASES], int size)
35586470930SIngo Molnar {
35686470930SIngo Molnar 	int i, j;
35761c45981SPaul Mackerras 	int n, longest = -1;
35886470930SIngo Molnar 
35986470930SIngo Molnar 	for (i = 0; i < size; i++) {
3600b668bc9SArnaldo Carvalho de Melo 		for (j = 0; j < PERF_EVSEL__MAX_ALIASES && names[i][j]; j++) {
36161c45981SPaul Mackerras 			n = strlen(names[i][j]);
36289812fc8SJiri Olsa 			if (n > longest && !strncasecmp(str, names[i][j], n))
36361c45981SPaul Mackerras 				longest = n;
36461c45981SPaul Mackerras 		}
36589812fc8SJiri Olsa 		if (longest > 0)
36686470930SIngo Molnar 			return i;
36786470930SIngo Molnar 	}
36886470930SIngo Molnar 
3698953645fSIngo Molnar 	return -1;
37086470930SIngo Molnar }
37186470930SIngo Molnar 
37243d0b978SWang Nan typedef int config_term_func_t(struct perf_event_attr *attr,
37343d0b978SWang Nan 			       struct parse_events_term *term,
37443d0b978SWang Nan 			       struct parse_events_error *err);
37543d0b978SWang Nan static int config_term_common(struct perf_event_attr *attr,
37643d0b978SWang Nan 			      struct parse_events_term *term,
37743d0b978SWang Nan 			      struct parse_events_error *err);
37843d0b978SWang Nan static int config_attr(struct perf_event_attr *attr,
37943d0b978SWang Nan 		       struct list_head *head,
38043d0b978SWang Nan 		       struct parse_events_error *err,
38143d0b978SWang Nan 		       config_term_func_t config_term);
38243d0b978SWang Nan 
383c5cd8ac0SDavid Ahern int parse_events_add_cache(struct list_head *list, int *idx,
38443d0b978SWang Nan 			   char *type, char *op_result1, char *op_result2,
3852c97b0d4SArnaldo Carvalho de Melo 			   struct parse_events_error *err,
38643d0b978SWang Nan 			   struct list_head *head_config)
38786470930SIngo Molnar {
38889812fc8SJiri Olsa 	struct perf_event_attr attr;
38943d0b978SWang Nan 	LIST_HEAD(config_terms);
39043d0b978SWang Nan 	char name[MAX_NAME_LEN], *config_name;
39161c45981SPaul Mackerras 	int cache_type = -1, cache_op = -1, cache_result = -1;
39289812fc8SJiri Olsa 	char *op_result[2] = { op_result1, op_result2 };
39389812fc8SJiri Olsa 	int i, n;
39486470930SIngo Molnar 
39586470930SIngo Molnar 	/*
39686470930SIngo Molnar 	 * No fallback - if we cannot get a clear cache type
39786470930SIngo Molnar 	 * then bail out:
39886470930SIngo Molnar 	 */
3990b668bc9SArnaldo Carvalho de Melo 	cache_type = parse_aliases(type, perf_evsel__hw_cache,
40089812fc8SJiri Olsa 				   PERF_COUNT_HW_CACHE_MAX);
40186470930SIngo Molnar 	if (cache_type == -1)
40289812fc8SJiri Olsa 		return -EINVAL;
40386470930SIngo Molnar 
40443d0b978SWang Nan 	config_name = get_config_name(head_config);
40589812fc8SJiri Olsa 	n = snprintf(name, MAX_NAME_LEN, "%s", type);
40689812fc8SJiri Olsa 
40789812fc8SJiri Olsa 	for (i = 0; (i < 2) && (op_result[i]); i++) {
40889812fc8SJiri Olsa 		char *str = op_result[i];
40989812fc8SJiri Olsa 
410275ef387SJiri Olsa 		n += snprintf(name + n, MAX_NAME_LEN - n, "-%s", str);
41161c45981SPaul Mackerras 
41261c45981SPaul Mackerras 		if (cache_op == -1) {
4130b668bc9SArnaldo Carvalho de Melo 			cache_op = parse_aliases(str, perf_evsel__hw_cache_op,
41461c45981SPaul Mackerras 						 PERF_COUNT_HW_CACHE_OP_MAX);
41561c45981SPaul Mackerras 			if (cache_op >= 0) {
4160b668bc9SArnaldo Carvalho de Melo 				if (!perf_evsel__is_cache_op_valid(cache_type, cache_op))
41789812fc8SJiri Olsa 					return -EINVAL;
41861c45981SPaul Mackerras 				continue;
41961c45981SPaul Mackerras 			}
42061c45981SPaul Mackerras 		}
42161c45981SPaul Mackerras 
42261c45981SPaul Mackerras 		if (cache_result == -1) {
4230b668bc9SArnaldo Carvalho de Melo 			cache_result = parse_aliases(str, perf_evsel__hw_cache_result,
42461c45981SPaul Mackerras 						     PERF_COUNT_HW_CACHE_RESULT_MAX);
42561c45981SPaul Mackerras 			if (cache_result >= 0)
42661c45981SPaul Mackerras 				continue;
42761c45981SPaul Mackerras 		}
42861c45981SPaul Mackerras 	}
42961c45981SPaul Mackerras 
43086470930SIngo Molnar 	/*
43186470930SIngo Molnar 	 * Fall back to reads:
43286470930SIngo Molnar 	 */
4338953645fSIngo Molnar 	if (cache_op == -1)
4348953645fSIngo Molnar 		cache_op = PERF_COUNT_HW_CACHE_OP_READ;
43586470930SIngo Molnar 
43686470930SIngo Molnar 	/*
43786470930SIngo Molnar 	 * Fall back to accesses:
43886470930SIngo Molnar 	 */
43986470930SIngo Molnar 	if (cache_result == -1)
44086470930SIngo Molnar 		cache_result = PERF_COUNT_HW_CACHE_RESULT_ACCESS;
44186470930SIngo Molnar 
44289812fc8SJiri Olsa 	memset(&attr, 0, sizeof(attr));
44389812fc8SJiri Olsa 	attr.config = cache_type | (cache_op << 8) | (cache_result << 16);
44489812fc8SJiri Olsa 	attr.type = PERF_TYPE_HW_CACHE;
44543d0b978SWang Nan 
44643d0b978SWang Nan 	if (head_config) {
4472c97b0d4SArnaldo Carvalho de Melo 		if (config_attr(&attr, head_config, err,
44843d0b978SWang Nan 				config_term_common))
44943d0b978SWang Nan 			return -EINVAL;
45043d0b978SWang Nan 
45143d0b978SWang Nan 		if (get_config_terms(head_config, &config_terms))
45243d0b978SWang Nan 			return -ENOMEM;
45343d0b978SWang Nan 	}
45443d0b978SWang Nan 	return add_event(list, idx, &attr, config_name ? : name, &config_terms);
45586470930SIngo Molnar }
45686470930SIngo Molnar 
457272ed29aSJiri Olsa static void tracepoint_error(struct parse_events_error *e, int err,
4588c619d6aSWang Nan 			     const char *sys, const char *name)
45919658171SJiri Olsa {
46019658171SJiri Olsa 	char help[BUFSIZ];
46119658171SJiri Olsa 
462ec183d22SAdrian Hunter 	if (!e)
463ec183d22SAdrian Hunter 		return;
464ec183d22SAdrian Hunter 
46519658171SJiri Olsa 	/*
46619658171SJiri Olsa 	 * We get error directly from syscall errno ( > 0),
46719658171SJiri Olsa 	 * or from encoded pointer's error ( < 0).
46819658171SJiri Olsa 	 */
46919658171SJiri Olsa 	err = abs(err);
47019658171SJiri Olsa 
47119658171SJiri Olsa 	switch (err) {
47219658171SJiri Olsa 	case EACCES:
473272ed29aSJiri Olsa 		e->str = strdup("can't access trace events");
47419658171SJiri Olsa 		break;
47519658171SJiri Olsa 	case ENOENT:
476272ed29aSJiri Olsa 		e->str = strdup("unknown tracepoint");
47719658171SJiri Olsa 		break;
47819658171SJiri Olsa 	default:
479272ed29aSJiri Olsa 		e->str = strdup("failed to add tracepoint");
48019658171SJiri Olsa 		break;
48119658171SJiri Olsa 	}
48219658171SJiri Olsa 
48319658171SJiri Olsa 	tracing_path__strerror_open_tp(err, help, sizeof(help), sys, name);
484272ed29aSJiri Olsa 	e->help = strdup(help);
48519658171SJiri Olsa }
48619658171SJiri Olsa 
487c5cd8ac0SDavid Ahern static int add_tracepoint(struct list_head *list, int *idx,
4888c619d6aSWang Nan 			  const char *sys_name, const char *evt_name,
489272ed29aSJiri Olsa 			  struct parse_events_error *err,
490e637d177SHe Kuang 			  struct list_head *head_config)
491bcd3279fSFrederic Weisbecker {
49282fe1c29SArnaldo Carvalho de Melo 	struct perf_evsel *evsel;
493bcd3279fSFrederic Weisbecker 
494ef503831SArnaldo Carvalho de Melo 	evsel = perf_evsel__newtp_idx(sys_name, evt_name, (*idx)++);
49519658171SJiri Olsa 	if (IS_ERR(evsel)) {
496272ed29aSJiri Olsa 		tracepoint_error(err, PTR_ERR(evsel), sys_name, evt_name);
4978dd2a131SJiri Olsa 		return PTR_ERR(evsel);
49819658171SJiri Olsa 	}
499bcd3279fSFrederic Weisbecker 
500e637d177SHe Kuang 	if (head_config) {
501e637d177SHe Kuang 		LIST_HEAD(config_terms);
502e637d177SHe Kuang 
503e637d177SHe Kuang 		if (get_config_terms(head_config, &config_terms))
504e637d177SHe Kuang 			return -ENOMEM;
505e637d177SHe Kuang 		list_splice(&config_terms, &evsel->config_terms);
506e637d177SHe Kuang 	}
507e637d177SHe Kuang 
50882fe1c29SArnaldo Carvalho de Melo 	list_add_tail(&evsel->node, list);
50982fe1c29SArnaldo Carvalho de Melo 	return 0;
510bcd3279fSFrederic Weisbecker }
511bcd3279fSFrederic Weisbecker 
512c5cd8ac0SDavid Ahern static int add_tracepoint_multi_event(struct list_head *list, int *idx,
5138c619d6aSWang Nan 				      const char *sys_name, const char *evt_name,
514272ed29aSJiri Olsa 				      struct parse_events_error *err,
515e637d177SHe Kuang 				      struct list_head *head_config)
516bcd3279fSFrederic Weisbecker {
51725a7d914SArnaldo Carvalho de Melo 	char *evt_path;
518bcd3279fSFrederic Weisbecker 	struct dirent *evt_ent;
519bcd3279fSFrederic Weisbecker 	DIR *evt_dir;
52027bf90bfSJiri Olsa 	int ret = 0, found = 0;
521bcd3279fSFrederic Weisbecker 
52225a7d914SArnaldo Carvalho de Melo 	evt_path = get_events_file(sys_name);
52325a7d914SArnaldo Carvalho de Melo 	if (!evt_path) {
52425a7d914SArnaldo Carvalho de Melo 		tracepoint_error(err, errno, sys_name, evt_name);
52525a7d914SArnaldo Carvalho de Melo 		return -1;
52625a7d914SArnaldo Carvalho de Melo 	}
527bcd3279fSFrederic Weisbecker 	evt_dir = opendir(evt_path);
528bcd3279fSFrederic Weisbecker 	if (!evt_dir) {
52925a7d914SArnaldo Carvalho de Melo 		put_events_file(evt_path);
530272ed29aSJiri Olsa 		tracepoint_error(err, errno, sys_name, evt_name);
53189812fc8SJiri Olsa 		return -1;
532bcd3279fSFrederic Weisbecker 	}
533bcd3279fSFrederic Weisbecker 
53489812fc8SJiri Olsa 	while (!ret && (evt_ent = readdir(evt_dir))) {
535bcd3279fSFrederic Weisbecker 		if (!strcmp(evt_ent->d_name, ".")
536bcd3279fSFrederic Weisbecker 		    || !strcmp(evt_ent->d_name, "..")
537bcd3279fSFrederic Weisbecker 		    || !strcmp(evt_ent->d_name, "enable")
538bcd3279fSFrederic Weisbecker 		    || !strcmp(evt_ent->d_name, "filter"))
539bcd3279fSFrederic Weisbecker 			continue;
540bcd3279fSFrederic Weisbecker 
54189812fc8SJiri Olsa 		if (!strglobmatch(evt_ent->d_name, evt_name))
542fb1d2edfSMasami Hiramatsu 			continue;
543fb1d2edfSMasami Hiramatsu 
54427bf90bfSJiri Olsa 		found++;
54527bf90bfSJiri Olsa 
546e637d177SHe Kuang 		ret = add_tracepoint(list, idx, sys_name, evt_ent->d_name,
547272ed29aSJiri Olsa 				     err, head_config);
548bcd3279fSFrederic Weisbecker 	}
549bcd3279fSFrederic Weisbecker 
55027bf90bfSJiri Olsa 	if (!found) {
55127bf90bfSJiri Olsa 		tracepoint_error(err, ENOENT, sys_name, evt_name);
55227bf90bfSJiri Olsa 		ret = -1;
55327bf90bfSJiri Olsa 	}
55427bf90bfSJiri Olsa 
55525a7d914SArnaldo Carvalho de Melo 	put_events_file(evt_path);
5560bd3f084SJiri Olsa 	closedir(evt_dir);
55789812fc8SJiri Olsa 	return ret;
558bcd3279fSFrederic Weisbecker }
559bcd3279fSFrederic Weisbecker 
560c5cd8ac0SDavid Ahern static int add_tracepoint_event(struct list_head *list, int *idx,
5618c619d6aSWang Nan 				const char *sys_name, const char *evt_name,
562272ed29aSJiri Olsa 				struct parse_events_error *err,
563e637d177SHe Kuang 				struct list_head *head_config)
564f35488f9SJiri Olsa {
565f35488f9SJiri Olsa 	return strpbrk(evt_name, "*?") ?
566e637d177SHe Kuang 	       add_tracepoint_multi_event(list, idx, sys_name, evt_name,
567272ed29aSJiri Olsa 					  err, head_config) :
568e637d177SHe Kuang 	       add_tracepoint(list, idx, sys_name, evt_name,
569272ed29aSJiri Olsa 			      err, head_config);
570f35488f9SJiri Olsa }
571f35488f9SJiri Olsa 
572c5cd8ac0SDavid Ahern static int add_tracepoint_multi_sys(struct list_head *list, int *idx,
5738c619d6aSWang Nan 				    const char *sys_name, const char *evt_name,
574272ed29aSJiri Olsa 				    struct parse_events_error *err,
575e637d177SHe Kuang 				    struct list_head *head_config)
576f35488f9SJiri Olsa {
577f35488f9SJiri Olsa 	struct dirent *events_ent;
578f35488f9SJiri Olsa 	DIR *events_dir;
579f35488f9SJiri Olsa 	int ret = 0;
580f35488f9SJiri Olsa 
5817014e0e3SArnaldo Carvalho de Melo 	events_dir = tracing_events__opendir();
582f35488f9SJiri Olsa 	if (!events_dir) {
583272ed29aSJiri Olsa 		tracepoint_error(err, errno, sys_name, evt_name);
584f35488f9SJiri Olsa 		return -1;
585f35488f9SJiri Olsa 	}
586f35488f9SJiri Olsa 
587f35488f9SJiri Olsa 	while (!ret && (events_ent = readdir(events_dir))) {
588f35488f9SJiri Olsa 		if (!strcmp(events_ent->d_name, ".")
589f35488f9SJiri Olsa 		    || !strcmp(events_ent->d_name, "..")
590f35488f9SJiri Olsa 		    || !strcmp(events_ent->d_name, "enable")
591f35488f9SJiri Olsa 		    || !strcmp(events_ent->d_name, "header_event")
592f35488f9SJiri Olsa 		    || !strcmp(events_ent->d_name, "header_page"))
593f35488f9SJiri Olsa 			continue;
594f35488f9SJiri Olsa 
595f35488f9SJiri Olsa 		if (!strglobmatch(events_ent->d_name, sys_name))
596f35488f9SJiri Olsa 			continue;
597f35488f9SJiri Olsa 
598f35488f9SJiri Olsa 		ret = add_tracepoint_event(list, idx, events_ent->d_name,
599272ed29aSJiri Olsa 					   evt_name, err, head_config);
600f35488f9SJiri Olsa 	}
601f35488f9SJiri Olsa 
602f35488f9SJiri Olsa 	closedir(events_dir);
603f35488f9SJiri Olsa 	return ret;
604f35488f9SJiri Olsa }
605f35488f9SJiri Olsa 
6064edf30e3SWang Nan struct __add_bpf_event_param {
6075d9cdc11SArnaldo Carvalho de Melo 	struct parse_events_state *parse_state;
6084edf30e3SWang Nan 	struct list_head *list;
60995088a59SWang Nan 	struct list_head *head_config;
6104edf30e3SWang Nan };
6114edf30e3SWang Nan 
612cd102d70SWang Nan static int add_bpf_event(const char *group, const char *event, int fd,
6134edf30e3SWang Nan 			 void *_param)
6144edf30e3SWang Nan {
6154edf30e3SWang Nan 	LIST_HEAD(new_evsels);
6164edf30e3SWang Nan 	struct __add_bpf_event_param *param = _param;
6175d9cdc11SArnaldo Carvalho de Melo 	struct parse_events_state *parse_state = param->parse_state;
6184edf30e3SWang Nan 	struct list_head *list = param->list;
6191f45b1d4SWang Nan 	struct perf_evsel *pos;
6204edf30e3SWang Nan 	int err;
6214edf30e3SWang Nan 
6224edf30e3SWang Nan 	pr_debug("add bpf event %s:%s and attach bpf program %d\n",
623cd102d70SWang Nan 		 group, event, fd);
6244edf30e3SWang Nan 
6255d369a75SArnaldo Carvalho de Melo 	err = parse_events_add_tracepoint(&new_evsels, &parse_state->idx, group,
6265d369a75SArnaldo Carvalho de Melo 					  event, parse_state->error,
62795088a59SWang Nan 					  param->head_config);
6284edf30e3SWang Nan 	if (err) {
6294edf30e3SWang Nan 		struct perf_evsel *evsel, *tmp;
6304edf30e3SWang Nan 
6314edf30e3SWang Nan 		pr_debug("Failed to add BPF event %s:%s\n",
632cd102d70SWang Nan 			 group, event);
6334edf30e3SWang Nan 		list_for_each_entry_safe(evsel, tmp, &new_evsels, node) {
6344edf30e3SWang Nan 			list_del(&evsel->node);
6354edf30e3SWang Nan 			perf_evsel__delete(evsel);
6364edf30e3SWang Nan 		}
6374edf30e3SWang Nan 		return err;
6384edf30e3SWang Nan 	}
639cd102d70SWang Nan 	pr_debug("adding %s:%s\n", group, event);
6404edf30e3SWang Nan 
6411f45b1d4SWang Nan 	list_for_each_entry(pos, &new_evsels, node) {
6421f45b1d4SWang Nan 		pr_debug("adding %s:%s to %p\n",
643cd102d70SWang Nan 			 group, event, pos);
6441f45b1d4SWang Nan 		pos->bpf_fd = fd;
6451f45b1d4SWang Nan 	}
6464edf30e3SWang Nan 	list_splice(&new_evsels, list);
6474edf30e3SWang Nan 	return 0;
6484edf30e3SWang Nan }
6494edf30e3SWang Nan 
6505d9cdc11SArnaldo Carvalho de Melo int parse_events_load_bpf_obj(struct parse_events_state *parse_state,
65184c86ca1SWang Nan 			      struct list_head *list,
65295088a59SWang Nan 			      struct bpf_object *obj,
65395088a59SWang Nan 			      struct list_head *head_config)
65484c86ca1SWang Nan {
65584c86ca1SWang Nan 	int err;
65684c86ca1SWang Nan 	char errbuf[BUFSIZ];
6575d9cdc11SArnaldo Carvalho de Melo 	struct __add_bpf_event_param param = {parse_state, list, head_config};
658aa3abf30SWang Nan 	static bool registered_unprobe_atexit = false;
65984c86ca1SWang Nan 
66084c86ca1SWang Nan 	if (IS_ERR(obj) || !obj) {
66184c86ca1SWang Nan 		snprintf(errbuf, sizeof(errbuf),
66284c86ca1SWang Nan 			 "Internal error: load bpf obj with NULL");
66384c86ca1SWang Nan 		err = -EINVAL;
66484c86ca1SWang Nan 		goto errout;
66584c86ca1SWang Nan 	}
66684c86ca1SWang Nan 
66784c86ca1SWang Nan 	/*
668aa3abf30SWang Nan 	 * Register atexit handler before calling bpf__probe() so
669aa3abf30SWang Nan 	 * bpf__probe() don't need to unprobe probe points its already
670aa3abf30SWang Nan 	 * created when failure.
671aa3abf30SWang Nan 	 */
672aa3abf30SWang Nan 	if (!registered_unprobe_atexit) {
673aa3abf30SWang Nan 		atexit(bpf__clear);
674aa3abf30SWang Nan 		registered_unprobe_atexit = true;
675aa3abf30SWang Nan 	}
676aa3abf30SWang Nan 
677aa3abf30SWang Nan 	err = bpf__probe(obj);
678aa3abf30SWang Nan 	if (err) {
679aa3abf30SWang Nan 		bpf__strerror_probe(obj, err, errbuf, sizeof(errbuf));
680aa3abf30SWang Nan 		goto errout;
681aa3abf30SWang Nan 	}
682aa3abf30SWang Nan 
6831e5e3ee8SWang Nan 	err = bpf__load(obj);
6841e5e3ee8SWang Nan 	if (err) {
6851e5e3ee8SWang Nan 		bpf__strerror_load(obj, err, errbuf, sizeof(errbuf));
6861e5e3ee8SWang Nan 		goto errout;
6871e5e3ee8SWang Nan 	}
6881e5e3ee8SWang Nan 
689cd102d70SWang Nan 	err = bpf__foreach_event(obj, add_bpf_event, &param);
6904edf30e3SWang Nan 	if (err) {
6914edf30e3SWang Nan 		snprintf(errbuf, sizeof(errbuf),
6924edf30e3SWang Nan 			 "Attach events in BPF object failed");
6934edf30e3SWang Nan 		goto errout;
6944edf30e3SWang Nan 	}
6954edf30e3SWang Nan 
6964edf30e3SWang Nan 	return 0;
69784c86ca1SWang Nan errout:
6985d9cdc11SArnaldo Carvalho de Melo 	parse_state->error->help = strdup("(add -v to see detail)");
6995d9cdc11SArnaldo Carvalho de Melo 	parse_state->error->str = strdup(errbuf);
70084c86ca1SWang Nan 	return err;
70184c86ca1SWang Nan }
70284c86ca1SWang Nan 
703a34f3be7SWang Nan static int
7045d9cdc11SArnaldo Carvalho de Melo parse_events_config_bpf(struct parse_events_state *parse_state,
705a34f3be7SWang Nan 			struct bpf_object *obj,
706a34f3be7SWang Nan 			struct list_head *head_config)
707a34f3be7SWang Nan {
708a34f3be7SWang Nan 	struct parse_events_term *term;
709a34f3be7SWang Nan 	int error_pos;
710a34f3be7SWang Nan 
711a34f3be7SWang Nan 	if (!head_config || list_empty(head_config))
712a34f3be7SWang Nan 		return 0;
713a34f3be7SWang Nan 
714a34f3be7SWang Nan 	list_for_each_entry(term, head_config, list) {
715a34f3be7SWang Nan 		char errbuf[BUFSIZ];
716a34f3be7SWang Nan 		int err;
717a34f3be7SWang Nan 
718a34f3be7SWang Nan 		if (term->type_term != PARSE_EVENTS__TERM_TYPE_USER) {
719a34f3be7SWang Nan 			snprintf(errbuf, sizeof(errbuf),
720a34f3be7SWang Nan 				 "Invalid config term for BPF object");
721a34f3be7SWang Nan 			errbuf[BUFSIZ - 1] = '\0';
722a34f3be7SWang Nan 
7235d9cdc11SArnaldo Carvalho de Melo 			parse_state->error->idx = term->err_term;
7245d9cdc11SArnaldo Carvalho de Melo 			parse_state->error->str = strdup(errbuf);
725a34f3be7SWang Nan 			return -EINVAL;
726a34f3be7SWang Nan 		}
727a34f3be7SWang Nan 
7285d9cdc11SArnaldo Carvalho de Melo 		err = bpf__config_obj(obj, term, parse_state->evlist, &error_pos);
729a34f3be7SWang Nan 		if (err) {
7305d9cdc11SArnaldo Carvalho de Melo 			bpf__strerror_config_obj(obj, term, parse_state->evlist,
731a34f3be7SWang Nan 						 &error_pos, err, errbuf,
732a34f3be7SWang Nan 						 sizeof(errbuf));
7335d9cdc11SArnaldo Carvalho de Melo 			parse_state->error->help = strdup(
7347630b3e2SWang Nan "Hint:\tValid config terms:\n"
735e571e029SWang Nan "     \tmap:[<arraymap>].value<indices>=[value]\n"
736e571e029SWang Nan "     \tmap:[<eventmap>].event<indices>=[event]\n"
7377630b3e2SWang Nan "\n"
738e571e029SWang Nan "     \twhere <indices> is something like [0,3...5] or [all]\n"
739a34f3be7SWang Nan "     \t(add -v to see detail)");
7405d9cdc11SArnaldo Carvalho de Melo 			parse_state->error->str = strdup(errbuf);
741a34f3be7SWang Nan 			if (err == -BPF_LOADER_ERRNO__OBJCONF_MAP_VALUE)
7425d9cdc11SArnaldo Carvalho de Melo 				parse_state->error->idx = term->err_val;
743a34f3be7SWang Nan 			else
7445d9cdc11SArnaldo Carvalho de Melo 				parse_state->error->idx = term->err_term + error_pos;
745a34f3be7SWang Nan 			return err;
746a34f3be7SWang Nan 		}
747a34f3be7SWang Nan 	}
748a34f3be7SWang Nan 	return 0;
749a34f3be7SWang Nan }
750a34f3be7SWang Nan 
75195088a59SWang Nan /*
75295088a59SWang Nan  * Split config terms:
75395088a59SWang Nan  * perf record -e bpf.c/call-graph=fp,map:array.value[0]=1/ ...
75495088a59SWang Nan  *  'call-graph=fp' is 'evt config', should be applied to each
75595088a59SWang Nan  *  events in bpf.c.
75695088a59SWang Nan  * 'map:array.value[0]=1' is 'obj config', should be processed
75795088a59SWang Nan  * with parse_events_config_bpf.
75895088a59SWang Nan  *
75995088a59SWang Nan  * Move object config terms from the first list to obj_head_config.
76095088a59SWang Nan  */
76195088a59SWang Nan static void
76295088a59SWang Nan split_bpf_config_terms(struct list_head *evt_head_config,
76395088a59SWang Nan 		       struct list_head *obj_head_config)
76495088a59SWang Nan {
76595088a59SWang Nan 	struct parse_events_term *term, *temp;
76695088a59SWang Nan 
76795088a59SWang Nan 	/*
76895088a59SWang Nan 	 * Currectly, all possible user config term
76995088a59SWang Nan 	 * belong to bpf object. parse_events__is_hardcoded_term()
77095088a59SWang Nan 	 * happends to be a good flag.
77195088a59SWang Nan 	 *
77295088a59SWang Nan 	 * See parse_events_config_bpf() and
77395088a59SWang Nan 	 * config_term_tracepoint().
77495088a59SWang Nan 	 */
77595088a59SWang Nan 	list_for_each_entry_safe(term, temp, evt_head_config, list)
77695088a59SWang Nan 		if (!parse_events__is_hardcoded_term(term))
77795088a59SWang Nan 			list_move_tail(&term->list, obj_head_config);
77895088a59SWang Nan }
77995088a59SWang Nan 
7805d9cdc11SArnaldo Carvalho de Melo int parse_events_load_bpf(struct parse_events_state *parse_state,
78184c86ca1SWang Nan 			  struct list_head *list,
782d509db04SWang Nan 			  char *bpf_file_name,
783a34f3be7SWang Nan 			  bool source,
784a34f3be7SWang Nan 			  struct list_head *head_config)
78584c86ca1SWang Nan {
786a34f3be7SWang Nan 	int err;
78795088a59SWang Nan 	struct bpf_object *obj;
78895088a59SWang Nan 	LIST_HEAD(obj_head_config);
78995088a59SWang Nan 
79095088a59SWang Nan 	if (head_config)
79195088a59SWang Nan 		split_bpf_config_terms(head_config, &obj_head_config);
79284c86ca1SWang Nan 
793d509db04SWang Nan 	obj = bpf__prepare_load(bpf_file_name, source);
7946371ca3bSWang Nan 	if (IS_ERR(obj)) {
79584c86ca1SWang Nan 		char errbuf[BUFSIZ];
79684c86ca1SWang Nan 
7976371ca3bSWang Nan 		err = PTR_ERR(obj);
79884c86ca1SWang Nan 
79984c86ca1SWang Nan 		if (err == -ENOTSUP)
80084c86ca1SWang Nan 			snprintf(errbuf, sizeof(errbuf),
80184c86ca1SWang Nan 				 "BPF support is not compiled");
80284c86ca1SWang Nan 		else
803d3e0ce39SWang Nan 			bpf__strerror_prepare_load(bpf_file_name,
804d3e0ce39SWang Nan 						   source,
805d3e0ce39SWang Nan 						   -err, errbuf,
806d3e0ce39SWang Nan 						   sizeof(errbuf));
80784c86ca1SWang Nan 
8085d9cdc11SArnaldo Carvalho de Melo 		parse_state->error->help = strdup("(add -v to see detail)");
8095d9cdc11SArnaldo Carvalho de Melo 		parse_state->error->str = strdup(errbuf);
81084c86ca1SWang Nan 		return err;
81184c86ca1SWang Nan 	}
81284c86ca1SWang Nan 
8135d9cdc11SArnaldo Carvalho de Melo 	err = parse_events_load_bpf_obj(parse_state, list, obj, head_config);
814a34f3be7SWang Nan 	if (err)
815a34f3be7SWang Nan 		return err;
8165d9cdc11SArnaldo Carvalho de Melo 	err = parse_events_config_bpf(parse_state, obj, &obj_head_config);
81795088a59SWang Nan 
81895088a59SWang Nan 	/*
81995088a59SWang Nan 	 * Caller doesn't know anything about obj_head_config,
82095088a59SWang Nan 	 * so combine them together again before returnning.
82195088a59SWang Nan 	 */
82295088a59SWang Nan 	if (head_config)
82395088a59SWang Nan 		list_splice_tail(&obj_head_config, head_config);
82495088a59SWang Nan 	return err;
82584c86ca1SWang Nan }
82684c86ca1SWang Nan 
82789812fc8SJiri Olsa static int
82889812fc8SJiri Olsa parse_breakpoint_type(const char *type, struct perf_event_attr *attr)
8291b290d67SFrederic Weisbecker {
8301b290d67SFrederic Weisbecker 	int i;
8311b290d67SFrederic Weisbecker 
8321b290d67SFrederic Weisbecker 	for (i = 0; i < 3; i++) {
83389812fc8SJiri Olsa 		if (!type || !type[i])
8341b290d67SFrederic Weisbecker 			break;
8351b290d67SFrederic Weisbecker 
8367582732fSJiri Olsa #define CHECK_SET_TYPE(bit)		\
8377582732fSJiri Olsa do {					\
8387582732fSJiri Olsa 	if (attr->bp_type & bit)	\
8397582732fSJiri Olsa 		return -EINVAL;		\
8407582732fSJiri Olsa 	else				\
8417582732fSJiri Olsa 		attr->bp_type |= bit;	\
8427582732fSJiri Olsa } while (0)
8437582732fSJiri Olsa 
8441b290d67SFrederic Weisbecker 		switch (type[i]) {
8451b290d67SFrederic Weisbecker 		case 'r':
8467582732fSJiri Olsa 			CHECK_SET_TYPE(HW_BREAKPOINT_R);
8471b290d67SFrederic Weisbecker 			break;
8481b290d67SFrederic Weisbecker 		case 'w':
8497582732fSJiri Olsa 			CHECK_SET_TYPE(HW_BREAKPOINT_W);
8501b290d67SFrederic Weisbecker 			break;
8511b290d67SFrederic Weisbecker 		case 'x':
8527582732fSJiri Olsa 			CHECK_SET_TYPE(HW_BREAKPOINT_X);
8531b290d67SFrederic Weisbecker 			break;
8541b290d67SFrederic Weisbecker 		default:
85589812fc8SJiri Olsa 			return -EINVAL;
8561b290d67SFrederic Weisbecker 		}
8571b290d67SFrederic Weisbecker 	}
85889812fc8SJiri Olsa 
8597582732fSJiri Olsa #undef CHECK_SET_TYPE
8607582732fSJiri Olsa 
8611b290d67SFrederic Weisbecker 	if (!attr->bp_type) /* Default */
8621b290d67SFrederic Weisbecker 		attr->bp_type = HW_BREAKPOINT_R | HW_BREAKPOINT_W;
8631b290d67SFrederic Weisbecker 
86489812fc8SJiri Olsa 	return 0;
8651b290d67SFrederic Weisbecker }
8661b290d67SFrederic Weisbecker 
867c5cd8ac0SDavid Ahern int parse_events_add_breakpoint(struct list_head *list, int *idx,
8683741eb9fSJacob Shin 				void *ptr, char *type, u64 len)
8691b290d67SFrederic Weisbecker {
87089812fc8SJiri Olsa 	struct perf_event_attr attr;
8711b290d67SFrederic Weisbecker 
87289812fc8SJiri Olsa 	memset(&attr, 0, sizeof(attr));
8739fafd98fSJiri Olsa 	attr.bp_addr = (unsigned long) ptr;
8741b290d67SFrederic Weisbecker 
87589812fc8SJiri Olsa 	if (parse_breakpoint_type(type, &attr))
87689812fc8SJiri Olsa 		return -EINVAL;
8771b290d67SFrederic Weisbecker 
8783741eb9fSJacob Shin 	/* Provide some defaults if len is not specified */
8793741eb9fSJacob Shin 	if (!len) {
88089812fc8SJiri Olsa 		if (attr.bp_type == HW_BREAKPOINT_X)
8813741eb9fSJacob Shin 			len = sizeof(long);
882aa59a485SFrederic Weisbecker 		else
8833741eb9fSJacob Shin 			len = HW_BREAKPOINT_LEN_4;
8843741eb9fSJacob Shin 	}
8853741eb9fSJacob Shin 
8863741eb9fSJacob Shin 	attr.bp_len = len;
887aa59a485SFrederic Weisbecker 
88889812fc8SJiri Olsa 	attr.type = PERF_TYPE_BREAKPOINT;
8894a841d65SJovi Zhang 	attr.sample_period = 1;
8901b290d67SFrederic Weisbecker 
891930a2e29SJiri Olsa 	return add_event(list, idx, &attr, NULL, NULL);
8921b290d67SFrederic Weisbecker }
8931b290d67SFrederic Weisbecker 
8943b0e371cSJiri Olsa static int check_type_val(struct parse_events_term *term,
8953b0e371cSJiri Olsa 			  struct parse_events_error *err,
8963b0e371cSJiri Olsa 			  int type)
8973b0e371cSJiri Olsa {
8983b0e371cSJiri Olsa 	if (type == term->type_val)
8993b0e371cSJiri Olsa 		return 0;
9003b0e371cSJiri Olsa 
9013b0e371cSJiri Olsa 	if (err) {
9023b0e371cSJiri Olsa 		err->idx = term->err_val;
9033b0e371cSJiri Olsa 		if (type == PARSE_EVENTS__TERM_TYPE_NUM)
9043b0e371cSJiri Olsa 			err->str = strdup("expected numeric value");
9053b0e371cSJiri Olsa 		else
9063b0e371cSJiri Olsa 			err->str = strdup("expected string value");
9073b0e371cSJiri Olsa 	}
9083b0e371cSJiri Olsa 	return -EINVAL;
9093b0e371cSJiri Olsa }
9103b0e371cSJiri Olsa 
91117cb5f84SWang Nan /*
91217cb5f84SWang Nan  * Update according to parse-events.l
91317cb5f84SWang Nan  */
91417cb5f84SWang Nan static const char *config_term_names[__PARSE_EVENTS__TERM_TYPE_NR] = {
91517cb5f84SWang Nan 	[PARSE_EVENTS__TERM_TYPE_USER]			= "<sysfs term>",
91617cb5f84SWang Nan 	[PARSE_EVENTS__TERM_TYPE_CONFIG]		= "config",
91717cb5f84SWang Nan 	[PARSE_EVENTS__TERM_TYPE_CONFIG1]		= "config1",
91817cb5f84SWang Nan 	[PARSE_EVENTS__TERM_TYPE_CONFIG2]		= "config2",
91917cb5f84SWang Nan 	[PARSE_EVENTS__TERM_TYPE_NAME]			= "name",
92017cb5f84SWang Nan 	[PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD]		= "period",
92117cb5f84SWang Nan 	[PARSE_EVENTS__TERM_TYPE_SAMPLE_FREQ]		= "freq",
92217cb5f84SWang Nan 	[PARSE_EVENTS__TERM_TYPE_BRANCH_SAMPLE_TYPE]	= "branch_type",
92317cb5f84SWang Nan 	[PARSE_EVENTS__TERM_TYPE_TIME]			= "time",
92417cb5f84SWang Nan 	[PARSE_EVENTS__TERM_TYPE_CALLGRAPH]		= "call-graph",
92517cb5f84SWang Nan 	[PARSE_EVENTS__TERM_TYPE_STACKSIZE]		= "stack-size",
92617cb5f84SWang Nan 	[PARSE_EVENTS__TERM_TYPE_NOINHERIT]		= "no-inherit",
92717cb5f84SWang Nan 	[PARSE_EVENTS__TERM_TYPE_INHERIT]		= "inherit",
928792d48b4SArnaldo Carvalho de Melo 	[PARSE_EVENTS__TERM_TYPE_MAX_STACK]		= "max-stack",
9292fda5adaSArnaldo Carvalho de Melo 	[PARSE_EVENTS__TERM_TYPE_MAX_EVENTS]		= "nr",
930626a6b78SWang Nan 	[PARSE_EVENTS__TERM_TYPE_OVERWRITE]		= "overwrite",
931626a6b78SWang Nan 	[PARSE_EVENTS__TERM_TYPE_NOOVERWRITE]		= "no-overwrite",
932dd60fba7SMathieu Poirier 	[PARSE_EVENTS__TERM_TYPE_DRV_CFG]		= "driver-config",
93317cb5f84SWang Nan };
93417cb5f84SWang Nan 
9351669e509SWang Nan static bool config_term_shrinked;
9361669e509SWang Nan 
9371669e509SWang Nan static bool
9381669e509SWang Nan config_term_avail(int term_type, struct parse_events_error *err)
9391669e509SWang Nan {
9401669e509SWang Nan 	if (term_type < 0 || term_type >= __PARSE_EVENTS__TERM_TYPE_NR) {
9411669e509SWang Nan 		err->str = strdup("Invalid term_type");
9421669e509SWang Nan 		return false;
9431669e509SWang Nan 	}
9441669e509SWang Nan 	if (!config_term_shrinked)
9451669e509SWang Nan 		return true;
9461669e509SWang Nan 
9471669e509SWang Nan 	switch (term_type) {
9481669e509SWang Nan 	case PARSE_EVENTS__TERM_TYPE_CONFIG:
9491669e509SWang Nan 	case PARSE_EVENTS__TERM_TYPE_CONFIG1:
9501669e509SWang Nan 	case PARSE_EVENTS__TERM_TYPE_CONFIG2:
9511669e509SWang Nan 	case PARSE_EVENTS__TERM_TYPE_NAME:
95206835545SSukadev Bhattiprolu 	case PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD:
9531669e509SWang Nan 		return true;
9541669e509SWang Nan 	default:
9551669e509SWang Nan 		if (!err)
9561669e509SWang Nan 			return false;
9571669e509SWang Nan 
9581669e509SWang Nan 		/* term_type is validated so indexing is safe */
9591669e509SWang Nan 		if (asprintf(&err->str, "'%s' is not usable in 'perf stat'",
9601669e509SWang Nan 			     config_term_names[term_type]) < 0)
9611669e509SWang Nan 			err->str = NULL;
9621669e509SWang Nan 		return false;
9631669e509SWang Nan 	}
9641669e509SWang Nan }
9651669e509SWang Nan 
9661669e509SWang Nan void parse_events__shrink_config_terms(void)
9671669e509SWang Nan {
9681669e509SWang Nan 	config_term_shrinked = true;
9691669e509SWang Nan }
9701669e509SWang Nan 
9710b8891a8SHe Kuang static int config_term_common(struct perf_event_attr *attr,
9723b0e371cSJiri Olsa 			      struct parse_events_term *term,
9733b0e371cSJiri Olsa 			      struct parse_events_error *err)
9748f707d84SJiri Olsa {
97516fa7e82SJiri Olsa #define CHECK_TYPE_VAL(type)						   \
97616fa7e82SJiri Olsa do {									   \
9773b0e371cSJiri Olsa 	if (check_type_val(term, err, PARSE_EVENTS__TERM_TYPE_ ## type)) \
97816fa7e82SJiri Olsa 		return -EINVAL;						   \
97916fa7e82SJiri Olsa } while (0)
98016fa7e82SJiri Olsa 
98116fa7e82SJiri Olsa 	switch (term->type_term) {
9828f707d84SJiri Olsa 	case PARSE_EVENTS__TERM_TYPE_CONFIG:
98316fa7e82SJiri Olsa 		CHECK_TYPE_VAL(NUM);
9848f707d84SJiri Olsa 		attr->config = term->val.num;
9858f707d84SJiri Olsa 		break;
9868f707d84SJiri Olsa 	case PARSE_EVENTS__TERM_TYPE_CONFIG1:
98716fa7e82SJiri Olsa 		CHECK_TYPE_VAL(NUM);
9888f707d84SJiri Olsa 		attr->config1 = term->val.num;
9898f707d84SJiri Olsa 		break;
9908f707d84SJiri Olsa 	case PARSE_EVENTS__TERM_TYPE_CONFIG2:
99116fa7e82SJiri Olsa 		CHECK_TYPE_VAL(NUM);
9928f707d84SJiri Olsa 		attr->config2 = term->val.num;
9938f707d84SJiri Olsa 		break;
9948f707d84SJiri Olsa 	case PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD:
99516fa7e82SJiri Olsa 		CHECK_TYPE_VAL(NUM);
9968f707d84SJiri Olsa 		break;
99709af2a55SNamhyung Kim 	case PARSE_EVENTS__TERM_TYPE_SAMPLE_FREQ:
99809af2a55SNamhyung Kim 		CHECK_TYPE_VAL(NUM);
99909af2a55SNamhyung Kim 		break;
10008f707d84SJiri Olsa 	case PARSE_EVENTS__TERM_TYPE_BRANCH_SAMPLE_TYPE:
1001ac12f676SAndi Kleen 		CHECK_TYPE_VAL(STR);
1002ac12f676SAndi Kleen 		if (strcmp(term->val.str, "no") &&
1003ac12f676SAndi Kleen 		    parse_branch_str(term->val.str, &attr->branch_sample_type)) {
1004ac12f676SAndi Kleen 			err->str = strdup("invalid branch sample type");
1005ac12f676SAndi Kleen 			err->idx = term->err_val;
1006ac12f676SAndi Kleen 			return -EINVAL;
1007ac12f676SAndi Kleen 		}
10088f707d84SJiri Olsa 		break;
100932067712SKan Liang 	case PARSE_EVENTS__TERM_TYPE_TIME:
101032067712SKan Liang 		CHECK_TYPE_VAL(NUM);
101132067712SKan Liang 		if (term->val.num > 1) {
101232067712SKan Liang 			err->str = strdup("expected 0 or 1");
101332067712SKan Liang 			err->idx = term->err_val;
101432067712SKan Liang 			return -EINVAL;
101532067712SKan Liang 		}
101632067712SKan Liang 		break;
1017d457c963SKan Liang 	case PARSE_EVENTS__TERM_TYPE_CALLGRAPH:
1018d457c963SKan Liang 		CHECK_TYPE_VAL(STR);
1019d457c963SKan Liang 		break;
1020d457c963SKan Liang 	case PARSE_EVENTS__TERM_TYPE_STACKSIZE:
1021d457c963SKan Liang 		CHECK_TYPE_VAL(NUM);
1022d457c963SKan Liang 		break;
1023374ce938SWang Nan 	case PARSE_EVENTS__TERM_TYPE_INHERIT:
1024374ce938SWang Nan 		CHECK_TYPE_VAL(NUM);
1025374ce938SWang Nan 		break;
1026374ce938SWang Nan 	case PARSE_EVENTS__TERM_TYPE_NOINHERIT:
1027374ce938SWang Nan 		CHECK_TYPE_VAL(NUM);
1028374ce938SWang Nan 		break;
1029626a6b78SWang Nan 	case PARSE_EVENTS__TERM_TYPE_OVERWRITE:
1030626a6b78SWang Nan 		CHECK_TYPE_VAL(NUM);
1031626a6b78SWang Nan 		break;
1032626a6b78SWang Nan 	case PARSE_EVENTS__TERM_TYPE_NOOVERWRITE:
1033626a6b78SWang Nan 		CHECK_TYPE_VAL(NUM);
1034626a6b78SWang Nan 		break;
10356b5fc39bSJiri Olsa 	case PARSE_EVENTS__TERM_TYPE_NAME:
10366b5fc39bSJiri Olsa 		CHECK_TYPE_VAL(STR);
10376b5fc39bSJiri Olsa 		break;
1038792d48b4SArnaldo Carvalho de Melo 	case PARSE_EVENTS__TERM_TYPE_MAX_STACK:
1039792d48b4SArnaldo Carvalho de Melo 		CHECK_TYPE_VAL(NUM);
1040792d48b4SArnaldo Carvalho de Melo 		break;
10412fda5adaSArnaldo Carvalho de Melo 	case PARSE_EVENTS__TERM_TYPE_MAX_EVENTS:
10422fda5adaSArnaldo Carvalho de Melo 		CHECK_TYPE_VAL(NUM);
10432fda5adaSArnaldo Carvalho de Melo 		break;
10448f707d84SJiri Olsa 	default:
1045ffeb883eSHe Kuang 		err->str = strdup("unknown term");
1046ffeb883eSHe Kuang 		err->idx = term->err_term;
1047ffeb883eSHe Kuang 		err->help = parse_events_formats_error_string(NULL);
10488f707d84SJiri Olsa 		return -EINVAL;
10498f707d84SJiri Olsa 	}
105016fa7e82SJiri Olsa 
10511669e509SWang Nan 	/*
10521669e509SWang Nan 	 * Check term availbility after basic checking so
10531669e509SWang Nan 	 * PARSE_EVENTS__TERM_TYPE_USER can be found and filtered.
10541669e509SWang Nan 	 *
10551669e509SWang Nan 	 * If check availbility at the entry of this function,
10561669e509SWang Nan 	 * user will see "'<sysfs term>' is not usable in 'perf stat'"
10571669e509SWang Nan 	 * if an invalid config term is provided for legacy events
10581669e509SWang Nan 	 * (for example, instructions/badterm/...), which is confusing.
10591669e509SWang Nan 	 */
10601669e509SWang Nan 	if (!config_term_avail(term->type_term, err))
10611669e509SWang Nan 		return -EINVAL;
10628f707d84SJiri Olsa 	return 0;
106316fa7e82SJiri Olsa #undef CHECK_TYPE_VAL
10648f707d84SJiri Olsa }
10658f707d84SJiri Olsa 
10660b8891a8SHe Kuang static int config_term_pmu(struct perf_event_attr *attr,
10670b8891a8SHe Kuang 			   struct parse_events_term *term,
10680b8891a8SHe Kuang 			   struct parse_events_error *err)
10690b8891a8SHe Kuang {
1070dd60fba7SMathieu Poirier 	if (term->type_term == PARSE_EVENTS__TERM_TYPE_USER ||
1071dd60fba7SMathieu Poirier 	    term->type_term == PARSE_EVENTS__TERM_TYPE_DRV_CFG)
10720b8891a8SHe Kuang 		/*
10730b8891a8SHe Kuang 		 * Always succeed for sysfs terms, as we dont know
10740b8891a8SHe Kuang 		 * at this point what type they need to have.
10750b8891a8SHe Kuang 		 */
10760b8891a8SHe Kuang 		return 0;
10770b8891a8SHe Kuang 	else
10780b8891a8SHe Kuang 		return config_term_common(attr, term, err);
10790b8891a8SHe Kuang }
10800b8891a8SHe Kuang 
1081e637d177SHe Kuang static int config_term_tracepoint(struct perf_event_attr *attr,
1082e637d177SHe Kuang 				  struct parse_events_term *term,
1083e637d177SHe Kuang 				  struct parse_events_error *err)
1084e637d177SHe Kuang {
1085e637d177SHe Kuang 	switch (term->type_term) {
1086e637d177SHe Kuang 	case PARSE_EVENTS__TERM_TYPE_CALLGRAPH:
1087e637d177SHe Kuang 	case PARSE_EVENTS__TERM_TYPE_STACKSIZE:
1088374ce938SWang Nan 	case PARSE_EVENTS__TERM_TYPE_INHERIT:
1089374ce938SWang Nan 	case PARSE_EVENTS__TERM_TYPE_NOINHERIT:
1090792d48b4SArnaldo Carvalho de Melo 	case PARSE_EVENTS__TERM_TYPE_MAX_STACK:
10912fda5adaSArnaldo Carvalho de Melo 	case PARSE_EVENTS__TERM_TYPE_MAX_EVENTS:
1092626a6b78SWang Nan 	case PARSE_EVENTS__TERM_TYPE_OVERWRITE:
1093626a6b78SWang Nan 	case PARSE_EVENTS__TERM_TYPE_NOOVERWRITE:
1094e637d177SHe Kuang 		return config_term_common(attr, term, err);
1095e637d177SHe Kuang 	default:
1096e637d177SHe Kuang 		if (err) {
1097e637d177SHe Kuang 			err->idx = term->err_term;
1098e637d177SHe Kuang 			err->str = strdup("unknown term");
1099e637d177SHe Kuang 			err->help = strdup("valid terms: call-graph,stack-size\n");
1100e637d177SHe Kuang 		}
1101e637d177SHe Kuang 		return -EINVAL;
1102e637d177SHe Kuang 	}
1103e637d177SHe Kuang 
1104e637d177SHe Kuang 	return 0;
1105e637d177SHe Kuang }
1106e637d177SHe Kuang 
11078f707d84SJiri Olsa static int config_attr(struct perf_event_attr *attr,
11083b0e371cSJiri Olsa 		       struct list_head *head,
11090b8891a8SHe Kuang 		       struct parse_events_error *err,
11100b8891a8SHe Kuang 		       config_term_func_t config_term)
11118f707d84SJiri Olsa {
11126cee6cd3SArnaldo Carvalho de Melo 	struct parse_events_term *term;
11138f707d84SJiri Olsa 
11148f707d84SJiri Olsa 	list_for_each_entry(term, head, list)
11153b0e371cSJiri Olsa 		if (config_term(attr, term, err))
11168f707d84SJiri Olsa 			return -EINVAL;
11178f707d84SJiri Olsa 
11188f707d84SJiri Olsa 	return 0;
11198f707d84SJiri Olsa }
11208f707d84SJiri Olsa 
1121930a2e29SJiri Olsa static int get_config_terms(struct list_head *head_config,
1122930a2e29SJiri Olsa 			    struct list_head *head_terms __maybe_unused)
1123930a2e29SJiri Olsa {
1124930a2e29SJiri Olsa #define ADD_CONFIG_TERM(__type, __name, __val)			\
1125930a2e29SJiri Olsa do {								\
1126930a2e29SJiri Olsa 	struct perf_evsel_config_term *__t;			\
1127930a2e29SJiri Olsa 								\
1128930a2e29SJiri Olsa 	__t = zalloc(sizeof(*__t));				\
1129930a2e29SJiri Olsa 	if (!__t)						\
1130930a2e29SJiri Olsa 		return -ENOMEM;					\
1131930a2e29SJiri Olsa 								\
1132930a2e29SJiri Olsa 	INIT_LIST_HEAD(&__t->list);				\
1133930a2e29SJiri Olsa 	__t->type       = PERF_EVSEL__CONFIG_TERM_ ## __type;	\
1134930a2e29SJiri Olsa 	__t->val.__name = __val;				\
1135c2f1ceadSAndi Kleen 	__t->weak	= term->weak;				\
1136930a2e29SJiri Olsa 	list_add_tail(&__t->list, head_terms);			\
1137930a2e29SJiri Olsa } while (0)
1138930a2e29SJiri Olsa 
1139930a2e29SJiri Olsa 	struct parse_events_term *term;
1140930a2e29SJiri Olsa 
1141930a2e29SJiri Olsa 	list_for_each_entry(term, head_config, list) {
1142930a2e29SJiri Olsa 		switch (term->type_term) {
1143ee4c7588SJiri Olsa 		case PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD:
1144ee4c7588SJiri Olsa 			ADD_CONFIG_TERM(PERIOD, period, term->val.num);
114532067712SKan Liang 			break;
114609af2a55SNamhyung Kim 		case PARSE_EVENTS__TERM_TYPE_SAMPLE_FREQ:
114709af2a55SNamhyung Kim 			ADD_CONFIG_TERM(FREQ, freq, term->val.num);
114809af2a55SNamhyung Kim 			break;
114932067712SKan Liang 		case PARSE_EVENTS__TERM_TYPE_TIME:
115032067712SKan Liang 			ADD_CONFIG_TERM(TIME, time, term->val.num);
115132067712SKan Liang 			break;
1152d457c963SKan Liang 		case PARSE_EVENTS__TERM_TYPE_CALLGRAPH:
1153d457c963SKan Liang 			ADD_CONFIG_TERM(CALLGRAPH, callgraph, term->val.str);
1154d457c963SKan Liang 			break;
1155ac12f676SAndi Kleen 		case PARSE_EVENTS__TERM_TYPE_BRANCH_SAMPLE_TYPE:
1156ac12f676SAndi Kleen 			ADD_CONFIG_TERM(BRANCH, branch, term->val.str);
1157ac12f676SAndi Kleen 			break;
1158d457c963SKan Liang 		case PARSE_EVENTS__TERM_TYPE_STACKSIZE:
1159d457c963SKan Liang 			ADD_CONFIG_TERM(STACK_USER, stack_user, term->val.num);
1160d457c963SKan Liang 			break;
1161374ce938SWang Nan 		case PARSE_EVENTS__TERM_TYPE_INHERIT:
1162374ce938SWang Nan 			ADD_CONFIG_TERM(INHERIT, inherit, term->val.num ? 1 : 0);
1163374ce938SWang Nan 			break;
1164374ce938SWang Nan 		case PARSE_EVENTS__TERM_TYPE_NOINHERIT:
1165374ce938SWang Nan 			ADD_CONFIG_TERM(INHERIT, inherit, term->val.num ? 0 : 1);
1166374ce938SWang Nan 			break;
1167792d48b4SArnaldo Carvalho de Melo 		case PARSE_EVENTS__TERM_TYPE_MAX_STACK:
1168792d48b4SArnaldo Carvalho de Melo 			ADD_CONFIG_TERM(MAX_STACK, max_stack, term->val.num);
1169792d48b4SArnaldo Carvalho de Melo 			break;
11702fda5adaSArnaldo Carvalho de Melo 		case PARSE_EVENTS__TERM_TYPE_MAX_EVENTS:
11712fda5adaSArnaldo Carvalho de Melo 			ADD_CONFIG_TERM(MAX_EVENTS, max_events, term->val.num);
11722fda5adaSArnaldo Carvalho de Melo 			break;
1173626a6b78SWang Nan 		case PARSE_EVENTS__TERM_TYPE_OVERWRITE:
1174626a6b78SWang Nan 			ADD_CONFIG_TERM(OVERWRITE, overwrite, term->val.num ? 1 : 0);
1175626a6b78SWang Nan 			break;
1176626a6b78SWang Nan 		case PARSE_EVENTS__TERM_TYPE_NOOVERWRITE:
1177626a6b78SWang Nan 			ADD_CONFIG_TERM(OVERWRITE, overwrite, term->val.num ? 0 : 1);
1178626a6b78SWang Nan 			break;
1179dd60fba7SMathieu Poirier 		case PARSE_EVENTS__TERM_TYPE_DRV_CFG:
1180dd60fba7SMathieu Poirier 			ADD_CONFIG_TERM(DRV_CFG, drv_cfg, term->val.str);
1181dd60fba7SMathieu Poirier 			break;
1182930a2e29SJiri Olsa 		default:
1183930a2e29SJiri Olsa 			break;
1184930a2e29SJiri Olsa 		}
1185930a2e29SJiri Olsa 	}
1186930a2e29SJiri Olsa #undef ADD_EVSEL_CONFIG
1187930a2e29SJiri Olsa 	return 0;
1188930a2e29SJiri Olsa }
1189930a2e29SJiri Olsa 
1190e637d177SHe Kuang int parse_events_add_tracepoint(struct list_head *list, int *idx,
11918c619d6aSWang Nan 				const char *sys, const char *event,
1192272ed29aSJiri Olsa 				struct parse_events_error *err,
1193e637d177SHe Kuang 				struct list_head *head_config)
1194e637d177SHe Kuang {
1195e637d177SHe Kuang 	if (head_config) {
1196e637d177SHe Kuang 		struct perf_event_attr attr;
1197e637d177SHe Kuang 
1198272ed29aSJiri Olsa 		if (config_attr(&attr, head_config, err,
1199e637d177SHe Kuang 				config_term_tracepoint))
1200e637d177SHe Kuang 			return -EINVAL;
1201e637d177SHe Kuang 	}
1202e637d177SHe Kuang 
1203e637d177SHe Kuang 	if (strpbrk(sys, "*?"))
1204e637d177SHe Kuang 		return add_tracepoint_multi_sys(list, idx, sys, event,
1205272ed29aSJiri Olsa 						err, head_config);
1206e637d177SHe Kuang 	else
1207e637d177SHe Kuang 		return add_tracepoint_event(list, idx, sys, event,
1208272ed29aSJiri Olsa 					    err, head_config);
1209e637d177SHe Kuang }
1210e637d177SHe Kuang 
12115d9cdc11SArnaldo Carvalho de Melo int parse_events_add_numeric(struct parse_events_state *parse_state,
121287d650beSJiri Olsa 			     struct list_head *list,
1213b527bab5SRobert Richter 			     u32 type, u64 config,
12148f707d84SJiri Olsa 			     struct list_head *head_config)
121574d5b588SJaswinder Singh Rajput {
121689812fc8SJiri Olsa 	struct perf_event_attr attr;
1217930a2e29SJiri Olsa 	LIST_HEAD(config_terms);
121874d5b588SJaswinder Singh Rajput 
121989812fc8SJiri Olsa 	memset(&attr, 0, sizeof(attr));
122089812fc8SJiri Olsa 	attr.type = type;
122189812fc8SJiri Olsa 	attr.config = config;
12228f707d84SJiri Olsa 
1223930a2e29SJiri Olsa 	if (head_config) {
12245d9cdc11SArnaldo Carvalho de Melo 		if (config_attr(&attr, head_config, parse_state->error,
12250b8891a8SHe Kuang 				config_term_common))
12268f707d84SJiri Olsa 			return -EINVAL;
12278f707d84SJiri Olsa 
1228930a2e29SJiri Olsa 		if (get_config_terms(head_config, &config_terms))
1229930a2e29SJiri Olsa 			return -ENOMEM;
1230930a2e29SJiri Olsa 	}
1231930a2e29SJiri Olsa 
12325d9cdc11SArnaldo Carvalho de Melo 	return add_event(list, &parse_state->idx, &attr,
123310bf358aSWang Nan 			 get_config_name(head_config), &config_terms);
1234b908debdSIngo Molnar }
1235b908debdSIngo Molnar 
1236c199c11dSAgustin Vega-Frias int parse_events_add_pmu(struct parse_events_state *parse_state,
123736adec85SJiri Olsa 			 struct list_head *list, char *name,
12383cdc5c2cSKan Liang 			 struct list_head *head_config,
12393cdc5c2cSKan Liang 			 bool auto_merge_stats,
12403cdc5c2cSKan Liang 			 bool use_alias)
12415f537a26SJiri Olsa {
12425f537a26SJiri Olsa 	struct perf_event_attr attr;
124346441bdcSMatt Fleming 	struct perf_pmu_info info;
12445f537a26SJiri Olsa 	struct perf_pmu *pmu;
1245410136f5SStephane Eranian 	struct perf_evsel *evsel;
1246333b5665SAndi Kleen 	struct parse_events_error *err = parse_state->error;
12473cdc5c2cSKan Liang 	bool use_uncore_alias;
1248930a2e29SJiri Olsa 	LIST_HEAD(config_terms);
12495f537a26SJiri Olsa 
12505f537a26SJiri Olsa 	pmu = perf_pmu__find(name);
1251333b5665SAndi Kleen 	if (!pmu) {
1252333b5665SAndi Kleen 		if (asprintf(&err->str,
1253333b5665SAndi Kleen 				"Cannot find PMU `%s'. Missing kernel support?",
1254333b5665SAndi Kleen 				name) < 0)
1255333b5665SAndi Kleen 			err->str = NULL;
12565f537a26SJiri Olsa 		return -EINVAL;
1257333b5665SAndi Kleen 	}
12585f537a26SJiri Olsa 
1259dc0a6202SAdrian Hunter 	if (pmu->default_config) {
1260dc0a6202SAdrian Hunter 		memcpy(&attr, pmu->default_config,
1261dc0a6202SAdrian Hunter 		       sizeof(struct perf_event_attr));
1262dc0a6202SAdrian Hunter 	} else {
12635f537a26SJiri Olsa 		memset(&attr, 0, sizeof(attr));
1264dc0a6202SAdrian Hunter 	}
12655f537a26SJiri Olsa 
12663cdc5c2cSKan Liang 	use_uncore_alias = (pmu->is_uncore && use_alias);
12673cdc5c2cSKan Liang 
1268ad962273SAdrian Hunter 	if (!head_config) {
1269ad962273SAdrian Hunter 		attr.type = pmu->type;
127066ec1191SMark Rutland 		evsel = __add_event(list, &parse_state->idx, &attr, NULL, pmu, NULL, auto_merge_stats);
12718c5421c0SAgustin Vega-Frias 		if (evsel) {
12728c5421c0SAgustin Vega-Frias 			evsel->pmu_name = name;
12733cdc5c2cSKan Liang 			evsel->use_uncore_alias = use_uncore_alias;
12748c5421c0SAgustin Vega-Frias 			return 0;
12758c5421c0SAgustin Vega-Frias 		} else {
12768c5421c0SAgustin Vega-Frias 			return -ENOMEM;
12778c5421c0SAgustin Vega-Frias 		}
1278ad962273SAdrian Hunter 	}
1279ad962273SAdrian Hunter 
128046441bdcSMatt Fleming 	if (perf_pmu__check_alias(pmu, head_config, &info))
1281a6146d50SZheng Yan 		return -EINVAL;
1282a6146d50SZheng Yan 
12835f537a26SJiri Olsa 	/*
12845f537a26SJiri Olsa 	 * Configure hardcoded terms first, no need to check
12855f537a26SJiri Olsa 	 * return value when called with fail == 0 ;)
12865f537a26SJiri Olsa 	 */
12875d9cdc11SArnaldo Carvalho de Melo 	if (config_attr(&attr, head_config, parse_state->error, config_term_pmu))
1288c056ba6aSJiri Olsa 		return -EINVAL;
12895f537a26SJiri Olsa 
1290930a2e29SJiri Olsa 	if (get_config_terms(head_config, &config_terms))
1291930a2e29SJiri Olsa 		return -ENOMEM;
1292930a2e29SJiri Olsa 
12935d9cdc11SArnaldo Carvalho de Melo 	if (perf_pmu__config(pmu, &attr, head_config, parse_state->error))
12945f537a26SJiri Olsa 		return -EINVAL;
12955f537a26SJiri Olsa 
12965d9cdc11SArnaldo Carvalho de Melo 	evsel = __add_event(list, &parse_state->idx, &attr,
129766ec1191SMark Rutland 			    get_config_name(head_config), pmu,
129863ce8449SArnaldo Carvalho de Melo 			    &config_terms, auto_merge_stats);
1299410136f5SStephane Eranian 	if (evsel) {
130046441bdcSMatt Fleming 		evsel->unit = info.unit;
130146441bdcSMatt Fleming 		evsel->scale = info.scale;
1302044330c1SMatt Fleming 		evsel->per_pkg = info.per_pkg;
13031d9e446bSJiri Olsa 		evsel->snapshot = info.snapshot;
130437932c18SAndi Kleen 		evsel->metric_expr = info.metric_expr;
130596284814SAndi Kleen 		evsel->metric_name = info.metric_name;
13068c5421c0SAgustin Vega-Frias 		evsel->pmu_name = name;
13073cdc5c2cSKan Liang 		evsel->use_uncore_alias = use_uncore_alias;
1308410136f5SStephane Eranian 	}
1309410136f5SStephane Eranian 
1310410136f5SStephane Eranian 	return evsel ? 0 : -ENOMEM;
13115f537a26SJiri Olsa }
13125f537a26SJiri Olsa 
13135d9cdc11SArnaldo Carvalho de Melo int parse_events_multi_pmu_add(struct parse_events_state *parse_state,
13142073ad33SAndi Kleen 			       char *str, struct list_head **listp)
13152073ad33SAndi Kleen {
13162073ad33SAndi Kleen 	struct list_head *head;
13172073ad33SAndi Kleen 	struct parse_events_term *term;
13182073ad33SAndi Kleen 	struct list_head *list;
13192073ad33SAndi Kleen 	struct perf_pmu *pmu = NULL;
13202073ad33SAndi Kleen 	int ok = 0;
13212073ad33SAndi Kleen 
13222073ad33SAndi Kleen 	*listp = NULL;
13232073ad33SAndi Kleen 	/* Add it for all PMUs that support the alias */
13242073ad33SAndi Kleen 	list = malloc(sizeof(struct list_head));
13252073ad33SAndi Kleen 	if (!list)
13262073ad33SAndi Kleen 		return -1;
13272073ad33SAndi Kleen 	INIT_LIST_HEAD(list);
13282073ad33SAndi Kleen 	while ((pmu = perf_pmu__scan(pmu)) != NULL) {
13292073ad33SAndi Kleen 		struct perf_pmu_alias *alias;
13302073ad33SAndi Kleen 
13312073ad33SAndi Kleen 		list_for_each_entry(alias, &pmu->aliases, list) {
13322073ad33SAndi Kleen 			if (!strcasecmp(alias->name, str)) {
13332073ad33SAndi Kleen 				head = malloc(sizeof(struct list_head));
13342073ad33SAndi Kleen 				if (!head)
13352073ad33SAndi Kleen 					return -1;
13362073ad33SAndi Kleen 				INIT_LIST_HEAD(head);
13372073ad33SAndi Kleen 				if (parse_events_term__num(&term, PARSE_EVENTS__TERM_TYPE_USER,
13382073ad33SAndi Kleen 							   str, 1, false, &str, NULL) < 0)
13392073ad33SAndi Kleen 					return -1;
13402073ad33SAndi Kleen 				list_add_tail(&term->list, head);
13412073ad33SAndi Kleen 
1342c199c11dSAgustin Vega-Frias 				if (!parse_events_add_pmu(parse_state, list,
13433cdc5c2cSKan Liang 							  pmu->name, head,
13443cdc5c2cSKan Liang 							  true, true)) {
13452073ad33SAndi Kleen 					pr_debug("%s -> %s/%s/\n", str,
13462073ad33SAndi Kleen 						 pmu->name, alias->str);
13472073ad33SAndi Kleen 					ok++;
13482073ad33SAndi Kleen 				}
13492073ad33SAndi Kleen 
13502073ad33SAndi Kleen 				parse_events_terms__delete(head);
13512073ad33SAndi Kleen 			}
13522073ad33SAndi Kleen 		}
13532073ad33SAndi Kleen 	}
13542073ad33SAndi Kleen 	if (!ok)
13552073ad33SAndi Kleen 		return -1;
13562073ad33SAndi Kleen 	*listp = list;
13572073ad33SAndi Kleen 	return 0;
13582073ad33SAndi Kleen }
13592073ad33SAndi Kleen 
13606a4bb04cSJiri Olsa int parse_events__modifier_group(struct list_head *list,
13616a4bb04cSJiri Olsa 				 char *event_mod)
136289efb029SJiri Olsa {
13636a4bb04cSJiri Olsa 	return parse_events__modifier_event(list, event_mod, true);
13646a4bb04cSJiri Olsa }
13656a4bb04cSJiri Olsa 
13663cdc5c2cSKan Liang /*
13673cdc5c2cSKan Liang  * Check if the two uncore PMUs are from the same uncore block
13683cdc5c2cSKan Liang  * The format of the uncore PMU name is uncore_#blockname_#pmuidx
13693cdc5c2cSKan Liang  */
13703cdc5c2cSKan Liang static bool is_same_uncore_block(const char *pmu_name_a, const char *pmu_name_b)
13713cdc5c2cSKan Liang {
13723cdc5c2cSKan Liang 	char *end_a, *end_b;
13733cdc5c2cSKan Liang 
13743cdc5c2cSKan Liang 	end_a = strrchr(pmu_name_a, '_');
13753cdc5c2cSKan Liang 	end_b = strrchr(pmu_name_b, '_');
13763cdc5c2cSKan Liang 
13773cdc5c2cSKan Liang 	if (!end_a || !end_b)
13783cdc5c2cSKan Liang 		return false;
13793cdc5c2cSKan Liang 
13803cdc5c2cSKan Liang 	if ((end_a - pmu_name_a) != (end_b - pmu_name_b))
13813cdc5c2cSKan Liang 		return false;
13823cdc5c2cSKan Liang 
13833cdc5c2cSKan Liang 	return (strncmp(pmu_name_a, pmu_name_b, end_a - pmu_name_a) == 0);
13843cdc5c2cSKan Liang }
13853cdc5c2cSKan Liang 
13863cdc5c2cSKan Liang static int
13873cdc5c2cSKan Liang parse_events__set_leader_for_uncore_aliase(char *name, struct list_head *list,
13883cdc5c2cSKan Liang 					   struct parse_events_state *parse_state)
13893cdc5c2cSKan Liang {
13903cdc5c2cSKan Liang 	struct perf_evsel *evsel, *leader;
13913cdc5c2cSKan Liang 	uintptr_t *leaders;
13923cdc5c2cSKan Liang 	bool is_leader = true;
13933cdc5c2cSKan Liang 	int i, nr_pmu = 0, total_members, ret = 0;
13943cdc5c2cSKan Liang 
13953cdc5c2cSKan Liang 	leader = list_first_entry(list, struct perf_evsel, node);
13963cdc5c2cSKan Liang 	evsel = list_last_entry(list, struct perf_evsel, node);
13973cdc5c2cSKan Liang 	total_members = evsel->idx - leader->idx + 1;
13983cdc5c2cSKan Liang 
13993cdc5c2cSKan Liang 	leaders = calloc(total_members, sizeof(uintptr_t));
14003cdc5c2cSKan Liang 	if (WARN_ON(!leaders))
14013cdc5c2cSKan Liang 		return 0;
14023cdc5c2cSKan Liang 
14033cdc5c2cSKan Liang 	/*
14043cdc5c2cSKan Liang 	 * Going through the whole group and doing sanity check.
14053cdc5c2cSKan Liang 	 * All members must use alias, and be from the same uncore block.
14063cdc5c2cSKan Liang 	 * Also, storing the leader events in an array.
14073cdc5c2cSKan Liang 	 */
14083cdc5c2cSKan Liang 	__evlist__for_each_entry(list, evsel) {
14093cdc5c2cSKan Liang 
14103cdc5c2cSKan Liang 		/* Only split the uncore group which members use alias */
14113cdc5c2cSKan Liang 		if (!evsel->use_uncore_alias)
14123cdc5c2cSKan Liang 			goto out;
14133cdc5c2cSKan Liang 
14143cdc5c2cSKan Liang 		/* The events must be from the same uncore block */
14153cdc5c2cSKan Liang 		if (!is_same_uncore_block(leader->pmu_name, evsel->pmu_name))
14163cdc5c2cSKan Liang 			goto out;
14173cdc5c2cSKan Liang 
14183cdc5c2cSKan Liang 		if (!is_leader)
14193cdc5c2cSKan Liang 			continue;
14203cdc5c2cSKan Liang 		/*
14213cdc5c2cSKan Liang 		 * If the event's PMU name starts to repeat, it must be a new
14223cdc5c2cSKan Liang 		 * event. That can be used to distinguish the leader from
14233cdc5c2cSKan Liang 		 * other members, even they have the same event name.
14243cdc5c2cSKan Liang 		 */
14253cdc5c2cSKan Liang 		if ((leader != evsel) && (leader->pmu_name == evsel->pmu_name)) {
14263cdc5c2cSKan Liang 			is_leader = false;
14273cdc5c2cSKan Liang 			continue;
14283cdc5c2cSKan Liang 		}
14293cdc5c2cSKan Liang 		/* The name is always alias name */
14303cdc5c2cSKan Liang 		WARN_ON(strcmp(leader->name, evsel->name));
14313cdc5c2cSKan Liang 
14323cdc5c2cSKan Liang 		/* Store the leader event for each PMU */
14333cdc5c2cSKan Liang 		leaders[nr_pmu++] = (uintptr_t) evsel;
14343cdc5c2cSKan Liang 	}
14353cdc5c2cSKan Liang 
14363cdc5c2cSKan Liang 	/* only one event alias */
14373cdc5c2cSKan Liang 	if (nr_pmu == total_members) {
14383cdc5c2cSKan Liang 		parse_state->nr_groups--;
14393cdc5c2cSKan Liang 		goto handled;
14403cdc5c2cSKan Liang 	}
14413cdc5c2cSKan Liang 
14423cdc5c2cSKan Liang 	/*
14433cdc5c2cSKan Liang 	 * An uncore event alias is a joint name which means the same event
14443cdc5c2cSKan Liang 	 * runs on all PMUs of a block.
14453cdc5c2cSKan Liang 	 * Perf doesn't support mixed events from different PMUs in the same
14463cdc5c2cSKan Liang 	 * group. The big group has to be split into multiple small groups
14473cdc5c2cSKan Liang 	 * which only include the events from the same PMU.
14483cdc5c2cSKan Liang 	 *
14493cdc5c2cSKan Liang 	 * Here the uncore event aliases must be from the same uncore block.
14503cdc5c2cSKan Liang 	 * The number of PMUs must be same for each alias. The number of new
14513cdc5c2cSKan Liang 	 * small groups equals to the number of PMUs.
14523cdc5c2cSKan Liang 	 * Setting the leader event for corresponding members in each group.
14533cdc5c2cSKan Liang 	 */
14543cdc5c2cSKan Liang 	i = 0;
14553cdc5c2cSKan Liang 	__evlist__for_each_entry(list, evsel) {
14563cdc5c2cSKan Liang 		if (i >= nr_pmu)
14573cdc5c2cSKan Liang 			i = 0;
14583cdc5c2cSKan Liang 		evsel->leader = (struct perf_evsel *) leaders[i++];
14593cdc5c2cSKan Liang 	}
14603cdc5c2cSKan Liang 
14613cdc5c2cSKan Liang 	/* The number of members and group name are same for each group */
14623cdc5c2cSKan Liang 	for (i = 0; i < nr_pmu; i++) {
14633cdc5c2cSKan Liang 		evsel = (struct perf_evsel *) leaders[i];
14643cdc5c2cSKan Liang 		evsel->nr_members = total_members / nr_pmu;
14653cdc5c2cSKan Liang 		evsel->group_name = name ? strdup(name) : NULL;
14663cdc5c2cSKan Liang 	}
14673cdc5c2cSKan Liang 
14683cdc5c2cSKan Liang 	/* Take the new small groups into account */
14693cdc5c2cSKan Liang 	parse_state->nr_groups += nr_pmu - 1;
14703cdc5c2cSKan Liang 
14713cdc5c2cSKan Liang handled:
14723cdc5c2cSKan Liang 	ret = 1;
14733cdc5c2cSKan Liang out:
14743cdc5c2cSKan Liang 	free(leaders);
14753cdc5c2cSKan Liang 	return ret;
14763cdc5c2cSKan Liang }
14773cdc5c2cSKan Liang 
14783cdc5c2cSKan Liang void parse_events__set_leader(char *name, struct list_head *list,
14793cdc5c2cSKan Liang 			      struct parse_events_state *parse_state)
14806a4bb04cSJiri Olsa {
14816a4bb04cSJiri Olsa 	struct perf_evsel *leader;
14826a4bb04cSJiri Olsa 
1483854f7363SWang Nan 	if (list_empty(list)) {
1484854f7363SWang Nan 		WARN_ONCE(true, "WARNING: failed to set leader: empty list");
1485854f7363SWang Nan 		return;
1486854f7363SWang Nan 	}
1487854f7363SWang Nan 
14883cdc5c2cSKan Liang 	if (parse_events__set_leader_for_uncore_aliase(name, list, parse_state))
14893cdc5c2cSKan Liang 		return;
14903cdc5c2cSKan Liang 
149163dab225SArnaldo Carvalho de Melo 	__perf_evlist__set_leader(list);
149263dab225SArnaldo Carvalho de Melo 	leader = list_entry(list->next, struct perf_evsel, node);
14936a4bb04cSJiri Olsa 	leader->group_name = name ? strdup(name) : NULL;
149489efb029SJiri Olsa }
149589efb029SJiri Olsa 
1496c5cd8ac0SDavid Ahern /* list_event is assumed to point to malloc'ed memory */
14975d7be90eSJiri Olsa void parse_events_update_lists(struct list_head *list_event,
14985d7be90eSJiri Olsa 			       struct list_head *list_all)
14995d7be90eSJiri Olsa {
15005d7be90eSJiri Olsa 	/*
15015d7be90eSJiri Olsa 	 * Called for single event definition. Update the
150289efb029SJiri Olsa 	 * 'all event' list, and reinit the 'single event'
15035d7be90eSJiri Olsa 	 * list, for next event definition.
15045d7be90eSJiri Olsa 	 */
15055d7be90eSJiri Olsa 	list_splice_tail(list_event, list_all);
1506b847cbdcSJiri Olsa 	free(list_event);
15075d7be90eSJiri Olsa }
15085d7be90eSJiri Olsa 
1509f5b1135bSJiri Olsa struct event_modifier {
1510f5b1135bSJiri Olsa 	int eu;
1511f5b1135bSJiri Olsa 	int ek;
1512f5b1135bSJiri Olsa 	int eh;
1513f5b1135bSJiri Olsa 	int eH;
1514f5b1135bSJiri Olsa 	int eG;
1515a1e12da4SJiri Olsa 	int eI;
1516f5b1135bSJiri Olsa 	int precise;
15177f94af7aSJiri Olsa 	int precise_max;
1518f5b1135bSJiri Olsa 	int exclude_GH;
15193c176311SJiri Olsa 	int sample_read;
1520e9a7c414SMichael Ellerman 	int pinned;
15215a5dfe4bSAndi Kleen 	int weak;
1522f5b1135bSJiri Olsa };
152361c45981SPaul Mackerras 
1524f5b1135bSJiri Olsa static int get_event_modifier(struct event_modifier *mod, char *str,
1525f5b1135bSJiri Olsa 			       struct perf_evsel *evsel)
1526f5b1135bSJiri Olsa {
1527f5b1135bSJiri Olsa 	int eu = evsel ? evsel->attr.exclude_user : 0;
1528f5b1135bSJiri Olsa 	int ek = evsel ? evsel->attr.exclude_kernel : 0;
1529f5b1135bSJiri Olsa 	int eh = evsel ? evsel->attr.exclude_hv : 0;
1530f5b1135bSJiri Olsa 	int eH = evsel ? evsel->attr.exclude_host : 0;
1531f5b1135bSJiri Olsa 	int eG = evsel ? evsel->attr.exclude_guest : 0;
1532a1e12da4SJiri Olsa 	int eI = evsel ? evsel->attr.exclude_idle : 0;
1533f5b1135bSJiri Olsa 	int precise = evsel ? evsel->attr.precise_ip : 0;
15347f94af7aSJiri Olsa 	int precise_max = 0;
15353c176311SJiri Olsa 	int sample_read = 0;
1536e9a7c414SMichael Ellerman 	int pinned = evsel ? evsel->attr.pinned : 0;
1537f5b1135bSJiri Olsa 
1538f5b1135bSJiri Olsa 	int exclude = eu | ek | eh;
1539f5b1135bSJiri Olsa 	int exclude_GH = evsel ? evsel->exclude_GH : 0;
15405a5dfe4bSAndi Kleen 	int weak = 0;
1541f5b1135bSJiri Olsa 
1542f5b1135bSJiri Olsa 	memset(mod, 0, sizeof(*mod));
1543ceb53fbfSIngo Molnar 
154461c45981SPaul Mackerras 	while (*str) {
1545ab608344SPeter Zijlstra 		if (*str == 'u') {
1546ab608344SPeter Zijlstra 			if (!exclude)
1547ab608344SPeter Zijlstra 				exclude = eu = ek = eh = 1;
154861c45981SPaul Mackerras 			eu = 0;
1549ab608344SPeter Zijlstra 		} else if (*str == 'k') {
1550ab608344SPeter Zijlstra 			if (!exclude)
1551ab608344SPeter Zijlstra 				exclude = eu = ek = eh = 1;
155261c45981SPaul Mackerras 			ek = 0;
1553ab608344SPeter Zijlstra 		} else if (*str == 'h') {
1554ab608344SPeter Zijlstra 			if (!exclude)
1555ab608344SPeter Zijlstra 				exclude = eu = ek = eh = 1;
155661c45981SPaul Mackerras 			eh = 0;
155799320cc8SJoerg Roedel 		} else if (*str == 'G') {
155899320cc8SJoerg Roedel 			if (!exclude_GH)
155999320cc8SJoerg Roedel 				exclude_GH = eG = eH = 1;
156099320cc8SJoerg Roedel 			eG = 0;
156199320cc8SJoerg Roedel 		} else if (*str == 'H') {
156299320cc8SJoerg Roedel 			if (!exclude_GH)
156399320cc8SJoerg Roedel 				exclude_GH = eG = eH = 1;
156499320cc8SJoerg Roedel 			eH = 0;
1565a1e12da4SJiri Olsa 		} else if (*str == 'I') {
1566a1e12da4SJiri Olsa 			eI = 1;
1567ab608344SPeter Zijlstra 		} else if (*str == 'p') {
1568ab608344SPeter Zijlstra 			precise++;
15691342798cSDavid Ahern 			/* use of precise requires exclude_guest */
15701342798cSDavid Ahern 			if (!exclude_GH)
15711342798cSDavid Ahern 				eG = 1;
15727f94af7aSJiri Olsa 		} else if (*str == 'P') {
15737f94af7aSJiri Olsa 			precise_max = 1;
15743c176311SJiri Olsa 		} else if (*str == 'S') {
15753c176311SJiri Olsa 			sample_read = 1;
1576e9a7c414SMichael Ellerman 		} else if (*str == 'D') {
1577e9a7c414SMichael Ellerman 			pinned = 1;
15785a5dfe4bSAndi Kleen 		} else if (*str == 'W') {
15795a5dfe4bSAndi Kleen 			weak = 1;
1580ab608344SPeter Zijlstra 		} else
158161c45981SPaul Mackerras 			break;
1582ab608344SPeter Zijlstra 
158361c45981SPaul Mackerras 		++str;
158461c45981SPaul Mackerras 	}
158574d5b588SJaswinder Singh Rajput 
158686470930SIngo Molnar 	/*
158789812fc8SJiri Olsa 	 * precise ip:
158889812fc8SJiri Olsa 	 *
158989812fc8SJiri Olsa 	 *  0 - SAMPLE_IP can have arbitrary skid
159089812fc8SJiri Olsa 	 *  1 - SAMPLE_IP must have constant skid
159189812fc8SJiri Olsa 	 *  2 - SAMPLE_IP requested to have 0 skid
159289812fc8SJiri Olsa 	 *  3 - SAMPLE_IP must have 0 skid
159389812fc8SJiri Olsa 	 *
159489812fc8SJiri Olsa 	 *  See also PERF_RECORD_MISC_EXACT_IP
159586470930SIngo Molnar 	 */
159689812fc8SJiri Olsa 	if (precise > 3)
159789812fc8SJiri Olsa 		return -EINVAL;
159886470930SIngo Molnar 
1599f5b1135bSJiri Olsa 	mod->eu = eu;
1600f5b1135bSJiri Olsa 	mod->ek = ek;
1601f5b1135bSJiri Olsa 	mod->eh = eh;
1602f5b1135bSJiri Olsa 	mod->eH = eH;
1603f5b1135bSJiri Olsa 	mod->eG = eG;
1604a1e12da4SJiri Olsa 	mod->eI = eI;
1605f5b1135bSJiri Olsa 	mod->precise = precise;
16067f94af7aSJiri Olsa 	mod->precise_max = precise_max;
1607f5b1135bSJiri Olsa 	mod->exclude_GH = exclude_GH;
16083c176311SJiri Olsa 	mod->sample_read = sample_read;
1609e9a7c414SMichael Ellerman 	mod->pinned = pinned;
16105a5dfe4bSAndi Kleen 	mod->weak = weak;
1611e9a7c414SMichael Ellerman 
1612f5b1135bSJiri Olsa 	return 0;
1613f5b1135bSJiri Olsa }
1614f5b1135bSJiri Olsa 
1615534123f4SJiri Olsa /*
1616534123f4SJiri Olsa  * Basic modifier sanity check to validate it contains only one
1617534123f4SJiri Olsa  * instance of any modifier (apart from 'p') present.
1618534123f4SJiri Olsa  */
1619534123f4SJiri Olsa static int check_modifier(char *str)
1620534123f4SJiri Olsa {
1621534123f4SJiri Olsa 	char *p = str;
1622534123f4SJiri Olsa 
1623534123f4SJiri Olsa 	/* The sizeof includes 0 byte as well. */
16245a5dfe4bSAndi Kleen 	if (strlen(str) > (sizeof("ukhGHpppPSDIW") - 1))
1625534123f4SJiri Olsa 		return -1;
1626534123f4SJiri Olsa 
1627534123f4SJiri Olsa 	while (*p) {
1628534123f4SJiri Olsa 		if (*p != 'p' && strchr(p + 1, *p))
1629534123f4SJiri Olsa 			return -1;
1630534123f4SJiri Olsa 		p++;
1631534123f4SJiri Olsa 	}
1632534123f4SJiri Olsa 
1633534123f4SJiri Olsa 	return 0;
1634534123f4SJiri Olsa }
1635534123f4SJiri Olsa 
1636f5b1135bSJiri Olsa int parse_events__modifier_event(struct list_head *list, char *str, bool add)
1637f5b1135bSJiri Olsa {
1638f5b1135bSJiri Olsa 	struct perf_evsel *evsel;
1639f5b1135bSJiri Olsa 	struct event_modifier mod;
1640f5b1135bSJiri Olsa 
1641f5b1135bSJiri Olsa 	if (str == NULL)
1642f5b1135bSJiri Olsa 		return 0;
1643f5b1135bSJiri Olsa 
1644534123f4SJiri Olsa 	if (check_modifier(str))
1645534123f4SJiri Olsa 		return -EINVAL;
1646534123f4SJiri Olsa 
1647f5b1135bSJiri Olsa 	if (!add && get_event_modifier(&mod, str, NULL))
1648f5b1135bSJiri Olsa 		return -EINVAL;
1649f5b1135bSJiri Olsa 
1650e5cadb93SArnaldo Carvalho de Melo 	__evlist__for_each_entry(list, evsel) {
1651f5b1135bSJiri Olsa 		if (add && get_event_modifier(&mod, str, evsel))
1652f5b1135bSJiri Olsa 			return -EINVAL;
1653f5b1135bSJiri Olsa 
1654f5b1135bSJiri Olsa 		evsel->attr.exclude_user   = mod.eu;
1655f5b1135bSJiri Olsa 		evsel->attr.exclude_kernel = mod.ek;
1656f5b1135bSJiri Olsa 		evsel->attr.exclude_hv     = mod.eh;
1657f5b1135bSJiri Olsa 		evsel->attr.precise_ip     = mod.precise;
1658f5b1135bSJiri Olsa 		evsel->attr.exclude_host   = mod.eH;
1659f5b1135bSJiri Olsa 		evsel->attr.exclude_guest  = mod.eG;
1660a1e12da4SJiri Olsa 		evsel->attr.exclude_idle   = mod.eI;
1661f5b1135bSJiri Olsa 		evsel->exclude_GH          = mod.exclude_GH;
16623c176311SJiri Olsa 		evsel->sample_read         = mod.sample_read;
16637f94af7aSJiri Olsa 		evsel->precise_max         = mod.precise_max;
16645a5dfe4bSAndi Kleen 		evsel->weak_group	   = mod.weak;
1665e9a7c414SMichael Ellerman 
1666e9a7c414SMichael Ellerman 		if (perf_evsel__is_group_leader(evsel))
1667e9a7c414SMichael Ellerman 			evsel->attr.pinned = mod.pinned;
1668ceb53fbfSIngo Molnar 	}
166986470930SIngo Molnar 
167089812fc8SJiri Olsa 	return 0;
167186470930SIngo Molnar }
167286470930SIngo Molnar 
1673ac2ba9f3SRobert Richter int parse_events_name(struct list_head *list, char *name)
1674ac2ba9f3SRobert Richter {
1675ac2ba9f3SRobert Richter 	struct perf_evsel *evsel;
1676ac2ba9f3SRobert Richter 
1677e5cadb93SArnaldo Carvalho de Melo 	__evlist__for_each_entry(list, evsel) {
1678ac2ba9f3SRobert Richter 		if (!evsel->name)
1679ac2ba9f3SRobert Richter 			evsel->name = strdup(name);
1680ac2ba9f3SRobert Richter 	}
1681ac2ba9f3SRobert Richter 
1682ac2ba9f3SRobert Richter 	return 0;
1683ac2ba9f3SRobert Richter }
1684ac2ba9f3SRobert Richter 
1685dcb4e102SKan Liang static int
1686dcb4e102SKan Liang comp_pmu(const void *p1, const void *p2)
1687dcb4e102SKan Liang {
1688dcb4e102SKan Liang 	struct perf_pmu_event_symbol *pmu1 = (struct perf_pmu_event_symbol *) p1;
1689dcb4e102SKan Liang 	struct perf_pmu_event_symbol *pmu2 = (struct perf_pmu_event_symbol *) p2;
1690dcb4e102SKan Liang 
1691e312bcf1SAndi Kleen 	return strcasecmp(pmu1->symbol, pmu2->symbol);
1692dcb4e102SKan Liang }
1693dcb4e102SKan Liang 
1694dcb4e102SKan Liang static void perf_pmu__parse_cleanup(void)
1695dcb4e102SKan Liang {
1696dcb4e102SKan Liang 	if (perf_pmu_events_list_num > 0) {
1697dcb4e102SKan Liang 		struct perf_pmu_event_symbol *p;
1698dcb4e102SKan Liang 		int i;
1699dcb4e102SKan Liang 
1700dcb4e102SKan Liang 		for (i = 0; i < perf_pmu_events_list_num; i++) {
1701dcb4e102SKan Liang 			p = perf_pmu_events_list + i;
1702360e071bSTaeung Song 			zfree(&p->symbol);
1703dcb4e102SKan Liang 		}
1704506fde11STaeung Song 		zfree(&perf_pmu_events_list);
1705dcb4e102SKan Liang 		perf_pmu_events_list_num = 0;
1706dcb4e102SKan Liang 	}
1707dcb4e102SKan Liang }
1708dcb4e102SKan Liang 
1709dcb4e102SKan Liang #define SET_SYMBOL(str, stype)		\
1710dcb4e102SKan Liang do {					\
1711dcb4e102SKan Liang 	p->symbol = str;		\
1712dcb4e102SKan Liang 	if (!p->symbol)			\
1713dcb4e102SKan Liang 		goto err;		\
1714dcb4e102SKan Liang 	p->type = stype;		\
1715dcb4e102SKan Liang } while (0)
1716dcb4e102SKan Liang 
1717dcb4e102SKan Liang /*
1718dcb4e102SKan Liang  * Read the pmu events list from sysfs
1719dcb4e102SKan Liang  * Save it into perf_pmu_events_list
1720dcb4e102SKan Liang  */
1721dcb4e102SKan Liang static void perf_pmu__parse_init(void)
1722dcb4e102SKan Liang {
1723dcb4e102SKan Liang 
1724dcb4e102SKan Liang 	struct perf_pmu *pmu = NULL;
1725dcb4e102SKan Liang 	struct perf_pmu_alias *alias;
1726dcb4e102SKan Liang 	int len = 0;
1727dcb4e102SKan Liang 
1728231bb2aaSAndi Kleen 	pmu = NULL;
1729231bb2aaSAndi Kleen 	while ((pmu = perf_pmu__scan(pmu)) != NULL) {
1730dcb4e102SKan Liang 		list_for_each_entry(alias, &pmu->aliases, list) {
1731dcb4e102SKan Liang 			if (strchr(alias->name, '-'))
1732dcb4e102SKan Liang 				len++;
1733dcb4e102SKan Liang 			len++;
1734dcb4e102SKan Liang 		}
1735231bb2aaSAndi Kleen 	}
1736231bb2aaSAndi Kleen 
1737231bb2aaSAndi Kleen 	if (len == 0) {
1738231bb2aaSAndi Kleen 		perf_pmu_events_list_num = -1;
1739231bb2aaSAndi Kleen 		return;
1740231bb2aaSAndi Kleen 	}
1741dcb4e102SKan Liang 	perf_pmu_events_list = malloc(sizeof(struct perf_pmu_event_symbol) * len);
1742dcb4e102SKan Liang 	if (!perf_pmu_events_list)
1743dcb4e102SKan Liang 		return;
1744dcb4e102SKan Liang 	perf_pmu_events_list_num = len;
1745dcb4e102SKan Liang 
1746dcb4e102SKan Liang 	len = 0;
1747231bb2aaSAndi Kleen 	pmu = NULL;
1748231bb2aaSAndi Kleen 	while ((pmu = perf_pmu__scan(pmu)) != NULL) {
1749dcb4e102SKan Liang 		list_for_each_entry(alias, &pmu->aliases, list) {
1750dcb4e102SKan Liang 			struct perf_pmu_event_symbol *p = perf_pmu_events_list + len;
1751dcb4e102SKan Liang 			char *tmp = strchr(alias->name, '-');
1752dcb4e102SKan Liang 
1753dcb4e102SKan Liang 			if (tmp != NULL) {
1754dcb4e102SKan Liang 				SET_SYMBOL(strndup(alias->name, tmp - alias->name),
1755dcb4e102SKan Liang 						PMU_EVENT_SYMBOL_PREFIX);
1756dcb4e102SKan Liang 				p++;
1757dcb4e102SKan Liang 				SET_SYMBOL(strdup(++tmp), PMU_EVENT_SYMBOL_SUFFIX);
1758dcb4e102SKan Liang 				len += 2;
1759dcb4e102SKan Liang 			} else {
1760dcb4e102SKan Liang 				SET_SYMBOL(strdup(alias->name), PMU_EVENT_SYMBOL);
1761dcb4e102SKan Liang 				len++;
1762dcb4e102SKan Liang 			}
1763dcb4e102SKan Liang 		}
1764231bb2aaSAndi Kleen 	}
1765dcb4e102SKan Liang 	qsort(perf_pmu_events_list, len,
1766dcb4e102SKan Liang 		sizeof(struct perf_pmu_event_symbol), comp_pmu);
1767dcb4e102SKan Liang 
1768dcb4e102SKan Liang 	return;
1769dcb4e102SKan Liang err:
1770dcb4e102SKan Liang 	perf_pmu__parse_cleanup();
1771dcb4e102SKan Liang }
1772dcb4e102SKan Liang 
1773dcb4e102SKan Liang enum perf_pmu_event_symbol_type
1774dcb4e102SKan Liang perf_pmu__parse_check(const char *name)
1775dcb4e102SKan Liang {
1776dcb4e102SKan Liang 	struct perf_pmu_event_symbol p, *r;
1777dcb4e102SKan Liang 
1778dcb4e102SKan Liang 	/* scan kernel pmu events from sysfs if needed */
1779dcb4e102SKan Liang 	if (perf_pmu_events_list_num == 0)
1780dcb4e102SKan Liang 		perf_pmu__parse_init();
1781dcb4e102SKan Liang 	/*
1782dcb4e102SKan Liang 	 * name "cpu" could be prefix of cpu-cycles or cpu// events.
1783dcb4e102SKan Liang 	 * cpu-cycles has been handled by hardcode.
1784dcb4e102SKan Liang 	 * So it must be cpu// events, not kernel pmu event.
1785dcb4e102SKan Liang 	 */
1786dcb4e102SKan Liang 	if ((perf_pmu_events_list_num <= 0) || !strcmp(name, "cpu"))
1787dcb4e102SKan Liang 		return PMU_EVENT_SYMBOL_ERR;
1788dcb4e102SKan Liang 
1789dcb4e102SKan Liang 	p.symbol = strdup(name);
1790dcb4e102SKan Liang 	r = bsearch(&p, perf_pmu_events_list,
1791dcb4e102SKan Liang 			(size_t) perf_pmu_events_list_num,
1792dcb4e102SKan Liang 			sizeof(struct perf_pmu_event_symbol), comp_pmu);
1793360e071bSTaeung Song 	zfree(&p.symbol);
1794dcb4e102SKan Liang 	return r ? r->type : PMU_EVENT_SYMBOL_ERR;
1795dcb4e102SKan Liang }
1796dcb4e102SKan Liang 
17975d9cdc11SArnaldo Carvalho de Melo static int parse_events__scanner(const char *str, void *parse_state, int start_token)
1798ac20de6fSZheng Yan {
1799ac20de6fSZheng Yan 	YY_BUFFER_STATE buffer;
1800ac20de6fSZheng Yan 	void *scanner;
1801ac20de6fSZheng Yan 	int ret;
1802ac20de6fSZheng Yan 
180390e2b22dSJiri Olsa 	ret = parse_events_lex_init_extra(start_token, &scanner);
1804ac20de6fSZheng Yan 	if (ret)
1805ac20de6fSZheng Yan 		return ret;
1806ac20de6fSZheng Yan 
1807ac20de6fSZheng Yan 	buffer = parse_events__scan_string(str, scanner);
1808ac20de6fSZheng Yan 
1809ac20de6fSZheng Yan #ifdef PARSER_DEBUG
1810ac20de6fSZheng Yan 	parse_events_debug = 1;
1811ac20de6fSZheng Yan #endif
18125d9cdc11SArnaldo Carvalho de Melo 	ret = parse_events_parse(parse_state, scanner);
1813ac20de6fSZheng Yan 
1814ac20de6fSZheng Yan 	parse_events__flush_buffer(buffer, scanner);
1815ac20de6fSZheng Yan 	parse_events__delete_buffer(buffer, scanner);
1816ac20de6fSZheng Yan 	parse_events_lex_destroy(scanner);
1817ac20de6fSZheng Yan 	return ret;
1818ac20de6fSZheng Yan }
1819ac20de6fSZheng Yan 
182090e2b22dSJiri Olsa /*
182190e2b22dSJiri Olsa  * parse event config string, return a list of event terms.
182290e2b22dSJiri Olsa  */
182390e2b22dSJiri Olsa int parse_events_terms(struct list_head *terms, const char *str)
182490e2b22dSJiri Olsa {
18255d9cdc11SArnaldo Carvalho de Melo 	struct parse_events_state parse_state = {
182690e2b22dSJiri Olsa 		.terms = NULL,
182790e2b22dSJiri Olsa 	};
182890e2b22dSJiri Olsa 	int ret;
182990e2b22dSJiri Olsa 
18305d9cdc11SArnaldo Carvalho de Melo 	ret = parse_events__scanner(str, &parse_state, PE_START_TERMS);
183190e2b22dSJiri Olsa 	if (!ret) {
18325d9cdc11SArnaldo Carvalho de Melo 		list_splice(parse_state.terms, terms);
18335d9cdc11SArnaldo Carvalho de Melo 		zfree(&parse_state.terms);
183490e2b22dSJiri Olsa 		return 0;
183590e2b22dSJiri Olsa 	}
183690e2b22dSJiri Olsa 
18375d9cdc11SArnaldo Carvalho de Melo 	parse_events_terms__delete(parse_state.terms);
183890e2b22dSJiri Olsa 	return ret;
183990e2b22dSJiri Olsa }
184090e2b22dSJiri Olsa 
1841b39b8393SJiri Olsa int parse_events(struct perf_evlist *evlist, const char *str,
1842b39b8393SJiri Olsa 		 struct parse_events_error *err)
184386470930SIngo Molnar {
18445d9cdc11SArnaldo Carvalho de Melo 	struct parse_events_state parse_state = {
18455d9cdc11SArnaldo Carvalho de Melo 		.list   = LIST_HEAD_INIT(parse_state.list),
184646010ab2SJiri Olsa 		.idx    = evlist->nr_entries,
1847b39b8393SJiri Olsa 		.error  = err,
18487630b3e2SWang Nan 		.evlist = evlist,
184946010ab2SJiri Olsa 	};
185046010ab2SJiri Olsa 	int ret;
185186470930SIngo Molnar 
18525d9cdc11SArnaldo Carvalho de Melo 	ret = parse_events__scanner(str, &parse_state, PE_START_EVENTS);
1853dcb4e102SKan Liang 	perf_pmu__parse_cleanup();
185489812fc8SJiri Olsa 	if (!ret) {
185515bfd2ccSWang Nan 		struct perf_evsel *last;
185615bfd2ccSWang Nan 
18575d9cdc11SArnaldo Carvalho de Melo 		if (list_empty(&parse_state.list)) {
1858c23080a6SArnaldo Carvalho de Melo 			WARN_ONCE(true, "WARNING: event parser found nothing\n");
1859854f7363SWang Nan 			return -1;
1860854f7363SWang Nan 		}
1861854f7363SWang Nan 
18625d9cdc11SArnaldo Carvalho de Melo 		perf_evlist__splice_list_tail(evlist, &parse_state.list);
18635d9cdc11SArnaldo Carvalho de Melo 		evlist->nr_groups += parse_state.nr_groups;
186415bfd2ccSWang Nan 		last = perf_evlist__last(evlist);
186515bfd2ccSWang Nan 		last->cmdline_group_boundary = true;
186615bfd2ccSWang Nan 
186786470930SIngo Molnar 		return 0;
186886470930SIngo Molnar 	}
186986470930SIngo Molnar 
18705d7be90eSJiri Olsa 	/*
18715d7be90eSJiri Olsa 	 * There are 2 users - builtin-record and builtin-test objects.
18725d7be90eSJiri Olsa 	 * Both call perf_evlist__delete in case of error, so we dont
18735d7be90eSJiri Olsa 	 * need to bother.
18745d7be90eSJiri Olsa 	 */
187589812fc8SJiri Olsa 	return ret;
187689812fc8SJiri Olsa }
187789812fc8SJiri Olsa 
1878b39b8393SJiri Olsa #define MAX_WIDTH 1000
1879b39b8393SJiri Olsa static int get_term_width(void)
1880b39b8393SJiri Olsa {
1881b39b8393SJiri Olsa 	struct winsize ws;
1882b39b8393SJiri Olsa 
1883b39b8393SJiri Olsa 	get_term_dimensions(&ws);
1884b39b8393SJiri Olsa 	return ws.ws_col > MAX_WIDTH ? MAX_WIDTH : ws.ws_col;
1885b39b8393SJiri Olsa }
1886b39b8393SJiri Olsa 
1887333b5665SAndi Kleen void parse_events_print_error(struct parse_events_error *err,
1888b39b8393SJiri Olsa 			      const char *event)
1889b39b8393SJiri Olsa {
1890b39b8393SJiri Olsa 	const char *str = "invalid or unsupported event: ";
1891b39b8393SJiri Olsa 	char _buf[MAX_WIDTH];
1892b39b8393SJiri Olsa 	char *buf = (char *) event;
1893b39b8393SJiri Olsa 	int idx = 0;
1894b39b8393SJiri Olsa 
1895b39b8393SJiri Olsa 	if (err->str) {
1896b39b8393SJiri Olsa 		/* -2 for extra '' in the final fprintf */
1897b39b8393SJiri Olsa 		int width       = get_term_width() - 2;
1898b39b8393SJiri Olsa 		int len_event   = strlen(event);
1899b39b8393SJiri Olsa 		int len_str, max_len, cut = 0;
1900b39b8393SJiri Olsa 
1901b39b8393SJiri Olsa 		/*
1902b39b8393SJiri Olsa 		 * Maximum error index indent, we will cut
1903b39b8393SJiri Olsa 		 * the event string if it's bigger.
1904b39b8393SJiri Olsa 		 */
1905141b2d31SAdrian Hunter 		int max_err_idx = 13;
1906b39b8393SJiri Olsa 
1907b39b8393SJiri Olsa 		/*
1908b39b8393SJiri Olsa 		 * Let's be specific with the message when
1909b39b8393SJiri Olsa 		 * we have the precise error.
1910b39b8393SJiri Olsa 		 */
1911b39b8393SJiri Olsa 		str     = "event syntax error: ";
1912b39b8393SJiri Olsa 		len_str = strlen(str);
1913b39b8393SJiri Olsa 		max_len = width - len_str;
1914b39b8393SJiri Olsa 
1915b39b8393SJiri Olsa 		buf = _buf;
1916b39b8393SJiri Olsa 
1917bd1a0be5SAdam Buchbinder 		/* We're cutting from the beginning. */
1918b39b8393SJiri Olsa 		if (err->idx > max_err_idx)
1919b39b8393SJiri Olsa 			cut = err->idx - max_err_idx;
1920b39b8393SJiri Olsa 
1921b39b8393SJiri Olsa 		strncpy(buf, event + cut, max_len);
1922b39b8393SJiri Olsa 
1923b39b8393SJiri Olsa 		/* Mark cut parts with '..' on both sides. */
1924b39b8393SJiri Olsa 		if (cut)
1925b39b8393SJiri Olsa 			buf[0] = buf[1] = '.';
1926b39b8393SJiri Olsa 
1927b39b8393SJiri Olsa 		if ((len_event - cut) > max_len) {
1928b39b8393SJiri Olsa 			buf[max_len - 1] = buf[max_len - 2] = '.';
1929b39b8393SJiri Olsa 			buf[max_len] = 0;
1930b39b8393SJiri Olsa 		}
1931b39b8393SJiri Olsa 
1932b39b8393SJiri Olsa 		idx = len_str + err->idx - cut;
1933b39b8393SJiri Olsa 	}
1934b39b8393SJiri Olsa 
1935b39b8393SJiri Olsa 	fprintf(stderr, "%s'%s'\n", str, buf);
1936b39b8393SJiri Olsa 	if (idx) {
1937b39b8393SJiri Olsa 		fprintf(stderr, "%*s\\___ %s\n", idx + 1, "", err->str);
1938b39b8393SJiri Olsa 		if (err->help)
1939b39b8393SJiri Olsa 			fprintf(stderr, "\n%s\n", err->help);
1940360e071bSTaeung Song 		zfree(&err->str);
1941360e071bSTaeung Song 		zfree(&err->help);
1942b39b8393SJiri Olsa 	}
1943b39b8393SJiri Olsa }
1944b39b8393SJiri Olsa 
1945b39b8393SJiri Olsa #undef MAX_WIDTH
1946b39b8393SJiri Olsa 
1947f120f9d5SJiri Olsa int parse_events_option(const struct option *opt, const char *str,
19481d037ca1SIrina Tirdea 			int unset __maybe_unused)
1949f120f9d5SJiri Olsa {
1950f120f9d5SJiri Olsa 	struct perf_evlist *evlist = *(struct perf_evlist **)opt->value;
1951b39b8393SJiri Olsa 	struct parse_events_error err = { .idx = 0, };
1952b39b8393SJiri Olsa 	int ret = parse_events(evlist, str, &err);
19539175ce1fSAndi Kleen 
1954333b5665SAndi Kleen 	if (ret) {
1955b39b8393SJiri Olsa 		parse_events_print_error(&err, str);
1956333b5665SAndi Kleen 		fprintf(stderr, "Run 'perf list' for a list of valid events\n");
1957333b5665SAndi Kleen 	}
1958b39b8393SJiri Olsa 
19599175ce1fSAndi Kleen 	return ret;
1960f120f9d5SJiri Olsa }
1961f120f9d5SJiri Olsa 
19624ba1faa1SWang Nan static int
19634ba1faa1SWang Nan foreach_evsel_in_last_glob(struct perf_evlist *evlist,
19644ba1faa1SWang Nan 			   int (*func)(struct perf_evsel *evsel,
19654ba1faa1SWang Nan 				       const void *arg),
19664ba1faa1SWang Nan 			   const void *arg)
1967c171b552SLi Zefan {
196869aad6f1SArnaldo Carvalho de Melo 	struct perf_evsel *last = NULL;
19694ba1faa1SWang Nan 	int err;
1970c171b552SLi Zefan 
1971854f7363SWang Nan 	/*
1972854f7363SWang Nan 	 * Don't return when list_empty, give func a chance to report
1973854f7363SWang Nan 	 * error when it found last == NULL.
1974854f7363SWang Nan 	 *
1975854f7363SWang Nan 	 * So no need to WARN here, let *func do this.
1976854f7363SWang Nan 	 */
1977361c99a6SArnaldo Carvalho de Melo 	if (evlist->nr_entries > 0)
19780c21f736SArnaldo Carvalho de Melo 		last = perf_evlist__last(evlist);
197969aad6f1SArnaldo Carvalho de Melo 
198015bfd2ccSWang Nan 	do {
19814ba1faa1SWang Nan 		err = (*func)(last, arg);
19824ba1faa1SWang Nan 		if (err)
1983c171b552SLi Zefan 			return -1;
19844ba1faa1SWang Nan 		if (!last)
19854ba1faa1SWang Nan 			return 0;
1986c171b552SLi Zefan 
198715bfd2ccSWang Nan 		if (last->node.prev == &evlist->entries)
198815bfd2ccSWang Nan 			return 0;
198915bfd2ccSWang Nan 		last = list_entry(last->node.prev, struct perf_evsel, node);
199015bfd2ccSWang Nan 	} while (!last->cmdline_group_boundary);
199115bfd2ccSWang Nan 
1992c171b552SLi Zefan 	return 0;
1993c171b552SLi Zefan }
1994c171b552SLi Zefan 
19954ba1faa1SWang Nan static int set_filter(struct perf_evsel *evsel, const void *arg)
19964ba1faa1SWang Nan {
19974ba1faa1SWang Nan 	const char *str = arg;
19981e857484SMathieu Poirier 	bool found = false;
19991e857484SMathieu Poirier 	int nr_addr_filters = 0;
20001e857484SMathieu Poirier 	struct perf_pmu *pmu = NULL;
20014ba1faa1SWang Nan 
200249836f78SJack Henschel 	if (evsel == NULL) {
200349836f78SJack Henschel 		fprintf(stderr,
200449836f78SJack Henschel 			"--filter option should follow a -e tracepoint or HW tracer option\n");
200549836f78SJack Henschel 		return -1;
200649836f78SJack Henschel 	}
20074ba1faa1SWang Nan 
20081e857484SMathieu Poirier 	if (evsel->attr.type == PERF_TYPE_TRACEPOINT) {
20093541c034SMathieu Poirier 		if (perf_evsel__append_tp_filter(evsel, str) < 0) {
20104ba1faa1SWang Nan 			fprintf(stderr,
20114ba1faa1SWang Nan 				"not enough memory to hold filter string\n");
20124ba1faa1SWang Nan 			return -1;
20134ba1faa1SWang Nan 		}
20144ba1faa1SWang Nan 
20154ba1faa1SWang Nan 		return 0;
20164ba1faa1SWang Nan 	}
20174ba1faa1SWang Nan 
20181e857484SMathieu Poirier 	while ((pmu = perf_pmu__scan(pmu)) != NULL)
20191e857484SMathieu Poirier 		if (pmu->type == evsel->attr.type) {
20201e857484SMathieu Poirier 			found = true;
20211e857484SMathieu Poirier 			break;
20221e857484SMathieu Poirier 		}
20231e857484SMathieu Poirier 
20241e857484SMathieu Poirier 	if (found)
20251e857484SMathieu Poirier 		perf_pmu__scan_file(pmu, "nr_addr_filters",
20261e857484SMathieu Poirier 				    "%d", &nr_addr_filters);
20271e857484SMathieu Poirier 
202849836f78SJack Henschel 	if (!nr_addr_filters) {
202949836f78SJack Henschel 		fprintf(stderr,
203049836f78SJack Henschel 			"This CPU does not support address filtering\n");
203149836f78SJack Henschel 		return -1;
203249836f78SJack Henschel 	}
20331e857484SMathieu Poirier 
20341e857484SMathieu Poirier 	if (perf_evsel__append_addr_filter(evsel, str) < 0) {
20351e857484SMathieu Poirier 		fprintf(stderr,
20361e857484SMathieu Poirier 			"not enough memory to hold filter string\n");
20371e857484SMathieu Poirier 		return -1;
20381e857484SMathieu Poirier 	}
20391e857484SMathieu Poirier 
20401e857484SMathieu Poirier 	return 0;
20411e857484SMathieu Poirier }
20421e857484SMathieu Poirier 
20434ba1faa1SWang Nan int parse_filter(const struct option *opt, const char *str,
20444ba1faa1SWang Nan 		 int unset __maybe_unused)
20454ba1faa1SWang Nan {
20464ba1faa1SWang Nan 	struct perf_evlist *evlist = *(struct perf_evlist **)opt->value;
20474ba1faa1SWang Nan 
20484ba1faa1SWang Nan 	return foreach_evsel_in_last_glob(evlist, set_filter,
20494ba1faa1SWang Nan 					  (const void *)str);
20504ba1faa1SWang Nan }
20514ba1faa1SWang Nan 
20524ba1faa1SWang Nan static int add_exclude_perf_filter(struct perf_evsel *evsel,
20534ba1faa1SWang Nan 				   const void *arg __maybe_unused)
20544ba1faa1SWang Nan {
20554ba1faa1SWang Nan 	char new_filter[64];
20564ba1faa1SWang Nan 
20574ba1faa1SWang Nan 	if (evsel == NULL || evsel->attr.type != PERF_TYPE_TRACEPOINT) {
20584ba1faa1SWang Nan 		fprintf(stderr,
20594ba1faa1SWang Nan 			"--exclude-perf option should follow a -e tracepoint option\n");
20604ba1faa1SWang Nan 		return -1;
20614ba1faa1SWang Nan 	}
20624ba1faa1SWang Nan 
20634ba1faa1SWang Nan 	snprintf(new_filter, sizeof(new_filter), "common_pid != %d", getpid());
20644ba1faa1SWang Nan 
20653541c034SMathieu Poirier 	if (perf_evsel__append_tp_filter(evsel, new_filter) < 0) {
20664ba1faa1SWang Nan 		fprintf(stderr,
20674ba1faa1SWang Nan 			"not enough memory to hold filter string\n");
20684ba1faa1SWang Nan 		return -1;
20694ba1faa1SWang Nan 	}
20704ba1faa1SWang Nan 
20714ba1faa1SWang Nan 	return 0;
20724ba1faa1SWang Nan }
20734ba1faa1SWang Nan 
20744ba1faa1SWang Nan int exclude_perf(const struct option *opt,
20754ba1faa1SWang Nan 		 const char *arg __maybe_unused,
20764ba1faa1SWang Nan 		 int unset __maybe_unused)
20774ba1faa1SWang Nan {
20784ba1faa1SWang Nan 	struct perf_evlist *evlist = *(struct perf_evlist **)opt->value;
20794ba1faa1SWang Nan 
20804ba1faa1SWang Nan 	return foreach_evsel_in_last_glob(evlist, add_exclude_perf_filter,
20814ba1faa1SWang Nan 					  NULL);
20824ba1faa1SWang Nan }
20834ba1faa1SWang Nan 
208486470930SIngo Molnar static const char * const event_type_descriptors[] = {
208586470930SIngo Molnar 	"Hardware event",
208686470930SIngo Molnar 	"Software event",
208786470930SIngo Molnar 	"Tracepoint event",
208886470930SIngo Molnar 	"Hardware cache event",
208941bdcb23SLiming Wang 	"Raw hardware event descriptor",
209041bdcb23SLiming Wang 	"Hardware breakpoint",
209186470930SIngo Molnar };
209286470930SIngo Molnar 
2093ab0e4800SYunlong Song static int cmp_string(const void *a, const void *b)
2094ab0e4800SYunlong Song {
2095ab0e4800SYunlong Song 	const char * const *as = a;
2096ab0e4800SYunlong Song 	const char * const *bs = b;
2097ab0e4800SYunlong Song 
2098ab0e4800SYunlong Song 	return strcmp(*as, *bs);
2099ab0e4800SYunlong Song }
2100ab0e4800SYunlong Song 
210186470930SIngo Molnar /*
2102f6bdafefSJason Baron  * Print the events from <debugfs_mount_point>/tracing/events
2103f6bdafefSJason Baron  */
2104f6bdafefSJason Baron 
2105a3277d2dSFrederic Weisbecker void print_tracepoint_events(const char *subsys_glob, const char *event_glob,
2106a3277d2dSFrederic Weisbecker 			     bool name_only)
2107f6bdafefSJason Baron {
2108f6bdafefSJason Baron 	DIR *sys_dir, *evt_dir;
210922a9f41bSArnaldo Carvalho de Melo 	struct dirent *sys_dirent, *evt_dirent;
2110f6bdafefSJason Baron 	char evt_path[MAXPATHLEN];
211125a7d914SArnaldo Carvalho de Melo 	char *dir_path;
2112ab0e4800SYunlong Song 	char **evt_list = NULL;
2113ab0e4800SYunlong Song 	unsigned int evt_i = 0, evt_num = 0;
2114ab0e4800SYunlong Song 	bool evt_num_known = false;
2115f6bdafefSJason Baron 
2116ab0e4800SYunlong Song restart:
21177014e0e3SArnaldo Carvalho de Melo 	sys_dir = tracing_events__opendir();
2118f6bdafefSJason Baron 	if (!sys_dir)
2119725b1368SEric Dumazet 		return;
2120f6bdafefSJason Baron 
2121ab0e4800SYunlong Song 	if (evt_num_known) {
2122ab0e4800SYunlong Song 		evt_list = zalloc(sizeof(char *) * evt_num);
2123ab0e4800SYunlong Song 		if (!evt_list)
2124ab0e4800SYunlong Song 			goto out_close_sys_dir;
2125ab0e4800SYunlong Song 	}
2126ab0e4800SYunlong Song 
212722a9f41bSArnaldo Carvalho de Melo 	for_each_subsystem(sys_dir, sys_dirent) {
2128668b8788SArnaldo Carvalho de Melo 		if (subsys_glob != NULL &&
212922a9f41bSArnaldo Carvalho de Melo 		    !strglobmatch(sys_dirent->d_name, subsys_glob))
2130668b8788SArnaldo Carvalho de Melo 			continue;
2131725b1368SEric Dumazet 
213225a7d914SArnaldo Carvalho de Melo 		dir_path = get_events_file(sys_dirent->d_name);
213325a7d914SArnaldo Carvalho de Melo 		if (!dir_path)
213425a7d914SArnaldo Carvalho de Melo 			continue;
2135725b1368SEric Dumazet 		evt_dir = opendir(dir_path);
2136725b1368SEric Dumazet 		if (!evt_dir)
213725a7d914SArnaldo Carvalho de Melo 			goto next;
2138725b1368SEric Dumazet 
2139c02cab22SArnaldo Carvalho de Melo 		for_each_event(dir_path, evt_dir, evt_dirent) {
2140668b8788SArnaldo Carvalho de Melo 			if (event_glob != NULL &&
214122a9f41bSArnaldo Carvalho de Melo 			    !strglobmatch(evt_dirent->d_name, event_glob))
2142668b8788SArnaldo Carvalho de Melo 				continue;
2143668b8788SArnaldo Carvalho de Melo 
2144ab0e4800SYunlong Song 			if (!evt_num_known) {
2145ab0e4800SYunlong Song 				evt_num++;
2146a3277d2dSFrederic Weisbecker 				continue;
2147a3277d2dSFrederic Weisbecker 			}
2148a3277d2dSFrederic Weisbecker 
2149f6bdafefSJason Baron 			snprintf(evt_path, MAXPATHLEN, "%s:%s",
215022a9f41bSArnaldo Carvalho de Melo 				 sys_dirent->d_name, evt_dirent->d_name);
2151ab0e4800SYunlong Song 
2152ab0e4800SYunlong Song 			evt_list[evt_i] = strdup(evt_path);
215325a7d914SArnaldo Carvalho de Melo 			if (evt_list[evt_i] == NULL) {
215425a7d914SArnaldo Carvalho de Melo 				put_events_file(dir_path);
2155ab0e4800SYunlong Song 				goto out_close_evt_dir;
215625a7d914SArnaldo Carvalho de Melo 			}
2157ab0e4800SYunlong Song 			evt_i++;
2158f6bdafefSJason Baron 		}
2159f6bdafefSJason Baron 		closedir(evt_dir);
216025a7d914SArnaldo Carvalho de Melo next:
216125a7d914SArnaldo Carvalho de Melo 		put_events_file(dir_path);
2162f6bdafefSJason Baron 	}
2163f6bdafefSJason Baron 	closedir(sys_dir);
2164ab0e4800SYunlong Song 
2165ab0e4800SYunlong Song 	if (!evt_num_known) {
2166ab0e4800SYunlong Song 		evt_num_known = true;
2167ab0e4800SYunlong Song 		goto restart;
2168ab0e4800SYunlong Song 	}
2169ab0e4800SYunlong Song 	qsort(evt_list, evt_num, sizeof(char *), cmp_string);
2170ab0e4800SYunlong Song 	evt_i = 0;
2171ab0e4800SYunlong Song 	while (evt_i < evt_num) {
2172ab0e4800SYunlong Song 		if (name_only) {
2173ab0e4800SYunlong Song 			printf("%s ", evt_list[evt_i++]);
2174ab0e4800SYunlong Song 			continue;
2175ab0e4800SYunlong Song 		}
2176ab0e4800SYunlong Song 		printf("  %-50s [%s]\n", evt_list[evt_i++],
2177ab0e4800SYunlong Song 				event_type_descriptors[PERF_TYPE_TRACEPOINT]);
2178ab0e4800SYunlong Song 	}
2179dfc431cbSArnaldo Carvalho de Melo 	if (evt_num && pager_in_use())
2180ab0e4800SYunlong Song 		printf("\n");
2181ab0e4800SYunlong Song 
2182ab0e4800SYunlong Song out_free:
2183ab0e4800SYunlong Song 	evt_num = evt_i;
2184ab0e4800SYunlong Song 	for (evt_i = 0; evt_i < evt_num; evt_i++)
2185ab0e4800SYunlong Song 		zfree(&evt_list[evt_i]);
2186ab0e4800SYunlong Song 	zfree(&evt_list);
2187ab0e4800SYunlong Song 	return;
2188ab0e4800SYunlong Song 
2189ab0e4800SYunlong Song out_close_evt_dir:
2190ab0e4800SYunlong Song 	closedir(evt_dir);
2191ab0e4800SYunlong Song out_close_sys_dir:
2192ab0e4800SYunlong Song 	closedir(sys_dir);
2193ab0e4800SYunlong Song 
2194ab0e4800SYunlong Song 	printf("FATAL: not enough memory to print %s\n",
2195ab0e4800SYunlong Song 			event_type_descriptors[PERF_TYPE_TRACEPOINT]);
2196ab0e4800SYunlong Song 	if (evt_list)
2197ab0e4800SYunlong Song 		goto out_free;
2198f6bdafefSJason Baron }
2199f6bdafefSJason Baron 
2200f6bdafefSJason Baron /*
220120c457b8SThomas Renninger  * Check whether event is in <debugfs_mount_point>/tracing/events
220220c457b8SThomas Renninger  */
220320c457b8SThomas Renninger 
220420c457b8SThomas Renninger int is_valid_tracepoint(const char *event_string)
220520c457b8SThomas Renninger {
220620c457b8SThomas Renninger 	DIR *sys_dir, *evt_dir;
220722a9f41bSArnaldo Carvalho de Melo 	struct dirent *sys_dirent, *evt_dirent;
220820c457b8SThomas Renninger 	char evt_path[MAXPATHLEN];
220925a7d914SArnaldo Carvalho de Melo 	char *dir_path;
221020c457b8SThomas Renninger 
22117014e0e3SArnaldo Carvalho de Melo 	sys_dir = tracing_events__opendir();
221220c457b8SThomas Renninger 	if (!sys_dir)
221320c457b8SThomas Renninger 		return 0;
221420c457b8SThomas Renninger 
221522a9f41bSArnaldo Carvalho de Melo 	for_each_subsystem(sys_dir, sys_dirent) {
221625a7d914SArnaldo Carvalho de Melo 		dir_path = get_events_file(sys_dirent->d_name);
221725a7d914SArnaldo Carvalho de Melo 		if (!dir_path)
221825a7d914SArnaldo Carvalho de Melo 			continue;
221920c457b8SThomas Renninger 		evt_dir = opendir(dir_path);
222020c457b8SThomas Renninger 		if (!evt_dir)
222125a7d914SArnaldo Carvalho de Melo 			goto next;
222220c457b8SThomas Renninger 
2223c02cab22SArnaldo Carvalho de Melo 		for_each_event(dir_path, evt_dir, evt_dirent) {
222420c457b8SThomas Renninger 			snprintf(evt_path, MAXPATHLEN, "%s:%s",
222522a9f41bSArnaldo Carvalho de Melo 				 sys_dirent->d_name, evt_dirent->d_name);
222620c457b8SThomas Renninger 			if (!strcmp(evt_path, event_string)) {
222720c457b8SThomas Renninger 				closedir(evt_dir);
222820c457b8SThomas Renninger 				closedir(sys_dir);
222920c457b8SThomas Renninger 				return 1;
223020c457b8SThomas Renninger 			}
223120c457b8SThomas Renninger 		}
223220c457b8SThomas Renninger 		closedir(evt_dir);
223325a7d914SArnaldo Carvalho de Melo next:
223425a7d914SArnaldo Carvalho de Melo 		put_events_file(dir_path);
223520c457b8SThomas Renninger 	}
223620c457b8SThomas Renninger 	closedir(sys_dir);
223720c457b8SThomas Renninger 	return 0;
223820c457b8SThomas Renninger }
223920c457b8SThomas Renninger 
2240b41f1cecSNamhyung Kim static bool is_event_supported(u8 type, unsigned config)
2241b41f1cecSNamhyung Kim {
2242b41f1cecSNamhyung Kim 	bool ret = true;
224388fee52eSVince Weaver 	int open_return;
2244b41f1cecSNamhyung Kim 	struct perf_evsel *evsel;
2245b41f1cecSNamhyung Kim 	struct perf_event_attr attr = {
2246b41f1cecSNamhyung Kim 		.type = type,
2247b41f1cecSNamhyung Kim 		.config = config,
2248b41f1cecSNamhyung Kim 		.disabled = 1,
2249b41f1cecSNamhyung Kim 	};
225089896051SArnaldo Carvalho de Melo 	struct thread_map *tmap = thread_map__new_by_tid(0);
225189896051SArnaldo Carvalho de Melo 
225289896051SArnaldo Carvalho de Melo 	if (tmap == NULL)
225389896051SArnaldo Carvalho de Melo 		return false;
2254b41f1cecSNamhyung Kim 
2255ef503831SArnaldo Carvalho de Melo 	evsel = perf_evsel__new(&attr);
2256b41f1cecSNamhyung Kim 	if (evsel) {
225789896051SArnaldo Carvalho de Melo 		open_return = perf_evsel__open(evsel, NULL, tmap);
225888fee52eSVince Weaver 		ret = open_return >= 0;
225988fee52eSVince Weaver 
226088fee52eSVince Weaver 		if (open_return == -EACCES) {
226188fee52eSVince Weaver 			/*
226288fee52eSVince Weaver 			 * This happens if the paranoid value
226388fee52eSVince Weaver 			 * /proc/sys/kernel/perf_event_paranoid is set to 2
226488fee52eSVince Weaver 			 * Re-run with exclude_kernel set; we don't do that
226588fee52eSVince Weaver 			 * by default as some ARM machines do not support it.
226688fee52eSVince Weaver 			 *
226788fee52eSVince Weaver 			 */
226888fee52eSVince Weaver 			evsel->attr.exclude_kernel = 1;
226989896051SArnaldo Carvalho de Melo 			ret = perf_evsel__open(evsel, NULL, tmap) >= 0;
227088fee52eSVince Weaver 		}
2271b41f1cecSNamhyung Kim 		perf_evsel__delete(evsel);
2272b41f1cecSNamhyung Kim 	}
2273b41f1cecSNamhyung Kim 
2274*39df730bSChangbin Du 	thread_map__put(tmap);
2275b41f1cecSNamhyung Kim 	return ret;
2276b41f1cecSNamhyung Kim }
2277b41f1cecSNamhyung Kim 
227840218daeSMasami Hiramatsu void print_sdt_events(const char *subsys_glob, const char *event_glob,
227940218daeSMasami Hiramatsu 		      bool name_only)
228040218daeSMasami Hiramatsu {
228140218daeSMasami Hiramatsu 	struct probe_cache *pcache;
228240218daeSMasami Hiramatsu 	struct probe_cache_entry *ent;
228340218daeSMasami Hiramatsu 	struct strlist *bidlist, *sdtlist;
228440218daeSMasami Hiramatsu 	struct strlist_config cfg = {.dont_dupstr = true};
228540218daeSMasami Hiramatsu 	struct str_node *nd, *nd2;
228640218daeSMasami Hiramatsu 	char *buf, *path, *ptr = NULL;
228740218daeSMasami Hiramatsu 	bool show_detail = false;
228840218daeSMasami Hiramatsu 	int ret;
228940218daeSMasami Hiramatsu 
229040218daeSMasami Hiramatsu 	sdtlist = strlist__new(NULL, &cfg);
229140218daeSMasami Hiramatsu 	if (!sdtlist) {
229240218daeSMasami Hiramatsu 		pr_debug("Failed to allocate new strlist for SDT\n");
229340218daeSMasami Hiramatsu 		return;
229440218daeSMasami Hiramatsu 	}
229540218daeSMasami Hiramatsu 	bidlist = build_id_cache__list_all(true);
229640218daeSMasami Hiramatsu 	if (!bidlist) {
229740218daeSMasami Hiramatsu 		pr_debug("Failed to get buildids: %d\n", errno);
229840218daeSMasami Hiramatsu 		return;
229940218daeSMasami Hiramatsu 	}
230040218daeSMasami Hiramatsu 	strlist__for_each_entry(nd, bidlist) {
2301f045b8c4SKrister Johansen 		pcache = probe_cache__new(nd->s, NULL);
230240218daeSMasami Hiramatsu 		if (!pcache)
230340218daeSMasami Hiramatsu 			continue;
230440218daeSMasami Hiramatsu 		list_for_each_entry(ent, &pcache->entries, node) {
230540218daeSMasami Hiramatsu 			if (!ent->sdt)
230640218daeSMasami Hiramatsu 				continue;
230740218daeSMasami Hiramatsu 			if (subsys_glob &&
230840218daeSMasami Hiramatsu 			    !strglobmatch(ent->pev.group, subsys_glob))
230940218daeSMasami Hiramatsu 				continue;
231040218daeSMasami Hiramatsu 			if (event_glob &&
231140218daeSMasami Hiramatsu 			    !strglobmatch(ent->pev.event, event_glob))
231240218daeSMasami Hiramatsu 				continue;
231340218daeSMasami Hiramatsu 			ret = asprintf(&buf, "%s:%s@%s", ent->pev.group,
231440218daeSMasami Hiramatsu 					ent->pev.event, nd->s);
231540218daeSMasami Hiramatsu 			if (ret > 0)
231640218daeSMasami Hiramatsu 				strlist__add(sdtlist, buf);
231740218daeSMasami Hiramatsu 		}
231840218daeSMasami Hiramatsu 		probe_cache__delete(pcache);
231940218daeSMasami Hiramatsu 	}
232040218daeSMasami Hiramatsu 	strlist__delete(bidlist);
232140218daeSMasami Hiramatsu 
232240218daeSMasami Hiramatsu 	strlist__for_each_entry(nd, sdtlist) {
232340218daeSMasami Hiramatsu 		buf = strchr(nd->s, '@');
232440218daeSMasami Hiramatsu 		if (buf)
232540218daeSMasami Hiramatsu 			*(buf++) = '\0';
232640218daeSMasami Hiramatsu 		if (name_only) {
232740218daeSMasami Hiramatsu 			printf("%s ", nd->s);
232840218daeSMasami Hiramatsu 			continue;
232940218daeSMasami Hiramatsu 		}
233040218daeSMasami Hiramatsu 		nd2 = strlist__next(nd);
233140218daeSMasami Hiramatsu 		if (nd2) {
233240218daeSMasami Hiramatsu 			ptr = strchr(nd2->s, '@');
233340218daeSMasami Hiramatsu 			if (ptr)
233440218daeSMasami Hiramatsu 				*ptr = '\0';
233540218daeSMasami Hiramatsu 			if (strcmp(nd->s, nd2->s) == 0)
233640218daeSMasami Hiramatsu 				show_detail = true;
233740218daeSMasami Hiramatsu 		}
233840218daeSMasami Hiramatsu 		if (show_detail) {
233940218daeSMasami Hiramatsu 			path = build_id_cache__origname(buf);
234040218daeSMasami Hiramatsu 			ret = asprintf(&buf, "%s@%s(%.12s)", nd->s, path, buf);
234140218daeSMasami Hiramatsu 			if (ret > 0) {
234240218daeSMasami Hiramatsu 				printf("  %-50s [%s]\n", buf, "SDT event");
234340218daeSMasami Hiramatsu 				free(buf);
234440218daeSMasami Hiramatsu 			}
234540218daeSMasami Hiramatsu 		} else
234640218daeSMasami Hiramatsu 			printf("  %-50s [%s]\n", nd->s, "SDT event");
234740218daeSMasami Hiramatsu 		if (nd2) {
234840218daeSMasami Hiramatsu 			if (strcmp(nd->s, nd2->s) != 0)
234940218daeSMasami Hiramatsu 				show_detail = false;
235040218daeSMasami Hiramatsu 			if (ptr)
235140218daeSMasami Hiramatsu 				*ptr = '@';
235240218daeSMasami Hiramatsu 		}
235340218daeSMasami Hiramatsu 	}
235440218daeSMasami Hiramatsu 	strlist__delete(sdtlist);
235540218daeSMasami Hiramatsu }
235640218daeSMasami Hiramatsu 
2357a3277d2dSFrederic Weisbecker int print_hwcache_events(const char *event_glob, bool name_only)
2358668b8788SArnaldo Carvalho de Melo {
2359ab0e4800SYunlong Song 	unsigned int type, op, i, evt_i = 0, evt_num = 0;
23600b668bc9SArnaldo Carvalho de Melo 	char name[64];
2361ab0e4800SYunlong Song 	char **evt_list = NULL;
2362ab0e4800SYunlong Song 	bool evt_num_known = false;
2363ab0e4800SYunlong Song 
2364ab0e4800SYunlong Song restart:
2365ab0e4800SYunlong Song 	if (evt_num_known) {
2366ab0e4800SYunlong Song 		evt_list = zalloc(sizeof(char *) * evt_num);
2367ab0e4800SYunlong Song 		if (!evt_list)
2368ab0e4800SYunlong Song 			goto out_enomem;
2369ab0e4800SYunlong Song 	}
2370668b8788SArnaldo Carvalho de Melo 
2371668b8788SArnaldo Carvalho de Melo 	for (type = 0; type < PERF_COUNT_HW_CACHE_MAX; type++) {
2372668b8788SArnaldo Carvalho de Melo 		for (op = 0; op < PERF_COUNT_HW_CACHE_OP_MAX; op++) {
2373668b8788SArnaldo Carvalho de Melo 			/* skip invalid cache type */
23740b668bc9SArnaldo Carvalho de Melo 			if (!perf_evsel__is_cache_op_valid(type, op))
2375668b8788SArnaldo Carvalho de Melo 				continue;
2376668b8788SArnaldo Carvalho de Melo 
2377668b8788SArnaldo Carvalho de Melo 			for (i = 0; i < PERF_COUNT_HW_CACHE_RESULT_MAX; i++) {
23780b668bc9SArnaldo Carvalho de Melo 				__perf_evsel__hw_cache_type_op_res_name(type, op, i,
23790b668bc9SArnaldo Carvalho de Melo 									name, sizeof(name));
2380947b4ad1SIngo Molnar 				if (event_glob != NULL && !strglobmatch(name, event_glob))
2381668b8788SArnaldo Carvalho de Melo 					continue;
2382668b8788SArnaldo Carvalho de Melo 
2383b41f1cecSNamhyung Kim 				if (!is_event_supported(PERF_TYPE_HW_CACHE,
2384b41f1cecSNamhyung Kim 							type | (op << 8) | (i << 16)))
2385b41f1cecSNamhyung Kim 					continue;
2386b41f1cecSNamhyung Kim 
2387ab0e4800SYunlong Song 				if (!evt_num_known) {
2388ab0e4800SYunlong Song 					evt_num++;
2389ab0e4800SYunlong Song 					continue;
2390ab0e4800SYunlong Song 				}
2391ab0e4800SYunlong Song 
2392ab0e4800SYunlong Song 				evt_list[evt_i] = strdup(name);
2393ab0e4800SYunlong Song 				if (evt_list[evt_i] == NULL)
2394ab0e4800SYunlong Song 					goto out_enomem;
2395ab0e4800SYunlong Song 				evt_i++;
2396668b8788SArnaldo Carvalho de Melo 			}
2397668b8788SArnaldo Carvalho de Melo 		}
2398668b8788SArnaldo Carvalho de Melo 	}
2399668b8788SArnaldo Carvalho de Melo 
2400ab0e4800SYunlong Song 	if (!evt_num_known) {
2401ab0e4800SYunlong Song 		evt_num_known = true;
2402ab0e4800SYunlong Song 		goto restart;
2403ab0e4800SYunlong Song 	}
2404ab0e4800SYunlong Song 	qsort(evt_list, evt_num, sizeof(char *), cmp_string);
2405ab0e4800SYunlong Song 	evt_i = 0;
2406ab0e4800SYunlong Song 	while (evt_i < evt_num) {
2407ab0e4800SYunlong Song 		if (name_only) {
2408ab0e4800SYunlong Song 			printf("%s ", evt_list[evt_i++]);
2409ab0e4800SYunlong Song 			continue;
2410ab0e4800SYunlong Song 		}
2411ab0e4800SYunlong Song 		printf("  %-50s [%s]\n", evt_list[evt_i++],
2412ab0e4800SYunlong Song 				event_type_descriptors[PERF_TYPE_HW_CACHE]);
2413ab0e4800SYunlong Song 	}
2414dfc431cbSArnaldo Carvalho de Melo 	if (evt_num && pager_in_use())
2415dc098b35SAndi Kleen 		printf("\n");
2416ab0e4800SYunlong Song 
2417ab0e4800SYunlong Song out_free:
2418ab0e4800SYunlong Song 	evt_num = evt_i;
2419ab0e4800SYunlong Song 	for (evt_i = 0; evt_i < evt_num; evt_i++)
2420ab0e4800SYunlong Song 		zfree(&evt_list[evt_i]);
2421ab0e4800SYunlong Song 	zfree(&evt_list);
2422ab0e4800SYunlong Song 	return evt_num;
2423ab0e4800SYunlong Song 
2424ab0e4800SYunlong Song out_enomem:
2425ab0e4800SYunlong Song 	printf("FATAL: not enough memory to print %s\n", event_type_descriptors[PERF_TYPE_HW_CACHE]);
2426ab0e4800SYunlong Song 	if (evt_list)
2427ab0e4800SYunlong Song 		goto out_free;
2428ab0e4800SYunlong Song 	return evt_num;
2429668b8788SArnaldo Carvalho de Melo }
2430668b8788SArnaldo Carvalho de Melo 
2431705750f2SYunlong Song void print_symbol_events(const char *event_glob, unsigned type,
2432a3277d2dSFrederic Weisbecker 				struct event_symbol *syms, unsigned max,
2433a3277d2dSFrederic Weisbecker 				bool name_only)
243486470930SIngo Molnar {
2435ab0e4800SYunlong Song 	unsigned int i, evt_i = 0, evt_num = 0;
2436947b4ad1SIngo Molnar 	char name[MAX_NAME_LEN];
2437ab0e4800SYunlong Song 	char **evt_list = NULL;
2438ab0e4800SYunlong Song 	bool evt_num_known = false;
2439ab0e4800SYunlong Song 
2440ab0e4800SYunlong Song restart:
2441ab0e4800SYunlong Song 	if (evt_num_known) {
2442ab0e4800SYunlong Song 		evt_list = zalloc(sizeof(char *) * evt_num);
2443ab0e4800SYunlong Song 		if (!evt_list)
2444ab0e4800SYunlong Song 			goto out_enomem;
2445ab0e4800SYunlong Song 		syms -= max;
2446ab0e4800SYunlong Song 	}
244786470930SIngo Molnar 
24481dc12760SJiri Olsa 	for (i = 0; i < max; i++, syms++) {
2449668b8788SArnaldo Carvalho de Melo 
2450e37df6c7SArnaldo Carvalho de Melo 		if (event_glob != NULL && syms->symbol != NULL &&
2451668b8788SArnaldo Carvalho de Melo 		    !(strglobmatch(syms->symbol, event_glob) ||
2452668b8788SArnaldo Carvalho de Melo 		      (syms->alias && strglobmatch(syms->alias, event_glob))))
2453668b8788SArnaldo Carvalho de Melo 			continue;
245486470930SIngo Molnar 
2455b41f1cecSNamhyung Kim 		if (!is_event_supported(type, i))
2456b41f1cecSNamhyung Kim 			continue;
2457b41f1cecSNamhyung Kim 
2458ab0e4800SYunlong Song 		if (!evt_num_known) {
2459ab0e4800SYunlong Song 			evt_num++;
2460a3277d2dSFrederic Weisbecker 			continue;
2461a3277d2dSFrederic Weisbecker 		}
2462a3277d2dSFrederic Weisbecker 
2463ab0e4800SYunlong Song 		if (!name_only && strlen(syms->alias))
2464947b4ad1SIngo Molnar 			snprintf(name, MAX_NAME_LEN, "%s OR %s", syms->symbol, syms->alias);
246574d5b588SJaswinder Singh Rajput 		else
2466bd8d57fbSArnaldo Carvalho de Melo 			strlcpy(name, syms->symbol, MAX_NAME_LEN);
246786470930SIngo Molnar 
2468ab0e4800SYunlong Song 		evt_list[evt_i] = strdup(name);
2469ab0e4800SYunlong Song 		if (evt_list[evt_i] == NULL)
2470ab0e4800SYunlong Song 			goto out_enomem;
2471ab0e4800SYunlong Song 		evt_i++;
247286470930SIngo Molnar 	}
247386470930SIngo Molnar 
2474ab0e4800SYunlong Song 	if (!evt_num_known) {
2475ab0e4800SYunlong Song 		evt_num_known = true;
2476ab0e4800SYunlong Song 		goto restart;
2477ab0e4800SYunlong Song 	}
2478ab0e4800SYunlong Song 	qsort(evt_list, evt_num, sizeof(char *), cmp_string);
2479ab0e4800SYunlong Song 	evt_i = 0;
2480ab0e4800SYunlong Song 	while (evt_i < evt_num) {
2481ab0e4800SYunlong Song 		if (name_only) {
2482ab0e4800SYunlong Song 			printf("%s ", evt_list[evt_i++]);
2483ab0e4800SYunlong Song 			continue;
2484ab0e4800SYunlong Song 		}
2485ab0e4800SYunlong Song 		printf("  %-50s [%s]\n", evt_list[evt_i++], event_type_descriptors[type]);
2486ab0e4800SYunlong Song 	}
2487dfc431cbSArnaldo Carvalho de Melo 	if (evt_num && pager_in_use())
2488689d3018SMarti Raudsepp 		printf("\n");
2489ab0e4800SYunlong Song 
2490ab0e4800SYunlong Song out_free:
2491ab0e4800SYunlong Song 	evt_num = evt_i;
2492ab0e4800SYunlong Song 	for (evt_i = 0; evt_i < evt_num; evt_i++)
2493ab0e4800SYunlong Song 		zfree(&evt_list[evt_i]);
2494ab0e4800SYunlong Song 	zfree(&evt_list);
2495ab0e4800SYunlong Song 	return;
2496ab0e4800SYunlong Song 
2497ab0e4800SYunlong Song out_enomem:
2498ab0e4800SYunlong Song 	printf("FATAL: not enough memory to print %s\n", event_type_descriptors[type]);
2499ab0e4800SYunlong Song 	if (evt_list)
2500ab0e4800SYunlong Song 		goto out_free;
2501668b8788SArnaldo Carvalho de Melo }
25021dc12760SJiri Olsa 
25031dc12760SJiri Olsa /*
25041dc12760SJiri Olsa  * Print the help text for the event symbols:
25051dc12760SJiri Olsa  */
2506c8d6828aSSukadev Bhattiprolu void print_events(const char *event_glob, bool name_only, bool quiet_flag,
2507bf874fcfSAndi Kleen 			bool long_desc, bool details_flag)
25081dc12760SJiri Olsa {
25091dc12760SJiri Olsa 	print_symbol_events(event_glob, PERF_TYPE_HARDWARE,
2510a3277d2dSFrederic Weisbecker 			    event_symbols_hw, PERF_COUNT_HW_MAX, name_only);
25111dc12760SJiri Olsa 
25121dc12760SJiri Olsa 	print_symbol_events(event_glob, PERF_TYPE_SOFTWARE,
2513a3277d2dSFrederic Weisbecker 			    event_symbols_sw, PERF_COUNT_SW_MAX, name_only);
25141dc12760SJiri Olsa 
2515a3277d2dSFrederic Weisbecker 	print_hwcache_events(event_glob, name_only);
251673c24cb8SJaswinder Singh Rajput 
2517bf874fcfSAndi Kleen 	print_pmu_events(event_glob, name_only, quiet_flag, long_desc,
2518bf874fcfSAndi Kleen 			details_flag);
2519dc098b35SAndi Kleen 
2520668b8788SArnaldo Carvalho de Melo 	if (event_glob != NULL)
2521668b8788SArnaldo Carvalho de Melo 		return;
252273c24cb8SJaswinder Singh Rajput 
2523a3277d2dSFrederic Weisbecker 	if (!name_only) {
2524947b4ad1SIngo Molnar 		printf("  %-50s [%s]\n",
25255f537a26SJiri Olsa 		       "rNNN",
25261cf4a063SArnaldo Carvalho de Melo 		       event_type_descriptors[PERF_TYPE_RAW]);
25275f537a26SJiri Olsa 		printf("  %-50s [%s]\n",
25285f537a26SJiri Olsa 		       "cpu/t1=v1[,t2=v2,t3 ...]/modifier",
25295f537a26SJiri Olsa 		       event_type_descriptors[PERF_TYPE_RAW]);
2530dfc431cbSArnaldo Carvalho de Melo 		if (pager_in_use())
2531dfc431cbSArnaldo Carvalho de Melo 			printf("   (see 'man perf-list' on how to encode it)\n\n");
253286470930SIngo Molnar 
2533947b4ad1SIngo Molnar 		printf("  %-50s [%s]\n",
25343741eb9fSJacob Shin 		       "mem:<addr>[/len][:access]",
253541bdcb23SLiming Wang 			event_type_descriptors[PERF_TYPE_BREAKPOINT]);
2536dfc431cbSArnaldo Carvalho de Melo 		if (pager_in_use())
25371b290d67SFrederic Weisbecker 			printf("\n");
2538a3277d2dSFrederic Weisbecker 	}
25391b290d67SFrederic Weisbecker 
2540a3277d2dSFrederic Weisbecker 	print_tracepoint_events(NULL, NULL, name_only);
254140218daeSMasami Hiramatsu 
254240218daeSMasami Hiramatsu 	print_sdt_events(NULL, NULL, name_only);
254371b0acceSAndi Kleen 
254433bbc571SJiri Olsa 	metricgroup__print(true, true, NULL, name_only, details_flag);
254586470930SIngo Molnar }
25468f707d84SJiri Olsa 
25476cee6cd3SArnaldo Carvalho de Melo int parse_events__is_hardcoded_term(struct parse_events_term *term)
25488f707d84SJiri Olsa {
254916fa7e82SJiri Olsa 	return term->type_term != PARSE_EVENTS__TERM_TYPE_USER;
25508f707d84SJiri Olsa }
25518f707d84SJiri Olsa 
255267b49b38SJiri Olsa static int new_term(struct parse_events_term **_term,
255367b49b38SJiri Olsa 		    struct parse_events_term *temp,
255467b49b38SJiri Olsa 		    char *str, u64 num)
25558f707d84SJiri Olsa {
25566cee6cd3SArnaldo Carvalho de Melo 	struct parse_events_term *term;
25578f707d84SJiri Olsa 
255867b49b38SJiri Olsa 	term = malloc(sizeof(*term));
25598f707d84SJiri Olsa 	if (!term)
25608f707d84SJiri Olsa 		return -ENOMEM;
25618f707d84SJiri Olsa 
256267b49b38SJiri Olsa 	*term = *temp;
25638f707d84SJiri Olsa 	INIT_LIST_HEAD(&term->list);
2564c2f1ceadSAndi Kleen 	term->weak = false;
25658f707d84SJiri Olsa 
256667b49b38SJiri Olsa 	switch (term->type_val) {
25678f707d84SJiri Olsa 	case PARSE_EVENTS__TERM_TYPE_NUM:
25688f707d84SJiri Olsa 		term->val.num = num;
25698f707d84SJiri Olsa 		break;
25708f707d84SJiri Olsa 	case PARSE_EVENTS__TERM_TYPE_STR:
25718f707d84SJiri Olsa 		term->val.str = str;
25728f707d84SJiri Olsa 		break;
25738f707d84SJiri Olsa 	default:
25744be8be6bSAdrian Hunter 		free(term);
25758f707d84SJiri Olsa 		return -EINVAL;
25768f707d84SJiri Olsa 	}
25778f707d84SJiri Olsa 
25788f707d84SJiri Olsa 	*_term = term;
25798f707d84SJiri Olsa 	return 0;
25808f707d84SJiri Olsa }
25818f707d84SJiri Olsa 
25826cee6cd3SArnaldo Carvalho de Melo int parse_events_term__num(struct parse_events_term **term,
2583cecf3a2eSJiri Olsa 			   int type_term, char *config, u64 num,
258499e7138eSJiri Olsa 			   bool no_value,
2585bb78ce7dSAdrian Hunter 			   void *loc_term_, void *loc_val_)
258616fa7e82SJiri Olsa {
2587bb78ce7dSAdrian Hunter 	YYLTYPE *loc_term = loc_term_;
2588bb78ce7dSAdrian Hunter 	YYLTYPE *loc_val = loc_val_;
2589bb78ce7dSAdrian Hunter 
259067b49b38SJiri Olsa 	struct parse_events_term temp = {
259167b49b38SJiri Olsa 		.type_val  = PARSE_EVENTS__TERM_TYPE_NUM,
259267b49b38SJiri Olsa 		.type_term = type_term,
259367b49b38SJiri Olsa 		.config    = config,
259499e7138eSJiri Olsa 		.no_value  = no_value,
259567b49b38SJiri Olsa 		.err_term  = loc_term ? loc_term->first_column : 0,
259667b49b38SJiri Olsa 		.err_val   = loc_val  ? loc_val->first_column  : 0,
259767b49b38SJiri Olsa 	};
259867b49b38SJiri Olsa 
259967b49b38SJiri Olsa 	return new_term(term, &temp, NULL, num);
260016fa7e82SJiri Olsa }
260116fa7e82SJiri Olsa 
26026cee6cd3SArnaldo Carvalho de Melo int parse_events_term__str(struct parse_events_term **term,
2603cecf3a2eSJiri Olsa 			   int type_term, char *config, char *str,
2604bb78ce7dSAdrian Hunter 			   void *loc_term_, void *loc_val_)
260516fa7e82SJiri Olsa {
2606bb78ce7dSAdrian Hunter 	YYLTYPE *loc_term = loc_term_;
2607bb78ce7dSAdrian Hunter 	YYLTYPE *loc_val = loc_val_;
2608bb78ce7dSAdrian Hunter 
260967b49b38SJiri Olsa 	struct parse_events_term temp = {
261067b49b38SJiri Olsa 		.type_val  = PARSE_EVENTS__TERM_TYPE_STR,
261167b49b38SJiri Olsa 		.type_term = type_term,
261267b49b38SJiri Olsa 		.config    = config,
261367b49b38SJiri Olsa 		.err_term  = loc_term ? loc_term->first_column : 0,
261467b49b38SJiri Olsa 		.err_val   = loc_val  ? loc_val->first_column  : 0,
261567b49b38SJiri Olsa 	};
261667b49b38SJiri Olsa 
261767b49b38SJiri Olsa 	return new_term(term, &temp, str, 0);
261816fa7e82SJiri Olsa }
261916fa7e82SJiri Olsa 
26206cee6cd3SArnaldo Carvalho de Melo int parse_events_term__sym_hw(struct parse_events_term **term,
26211d33d6dcSJiri Olsa 			      char *config, unsigned idx)
26221d33d6dcSJiri Olsa {
26231d33d6dcSJiri Olsa 	struct event_symbol *sym;
262467b49b38SJiri Olsa 	struct parse_events_term temp = {
262567b49b38SJiri Olsa 		.type_val  = PARSE_EVENTS__TERM_TYPE_STR,
262667b49b38SJiri Olsa 		.type_term = PARSE_EVENTS__TERM_TYPE_USER,
262767b49b38SJiri Olsa 		.config    = config ?: (char *) "event",
262867b49b38SJiri Olsa 	};
26291d33d6dcSJiri Olsa 
26301d33d6dcSJiri Olsa 	BUG_ON(idx >= PERF_COUNT_HW_MAX);
26311d33d6dcSJiri Olsa 	sym = &event_symbols_hw[idx];
26321d33d6dcSJiri Olsa 
263367b49b38SJiri Olsa 	return new_term(term, &temp, (char *) sym->symbol, 0);
26341d33d6dcSJiri Olsa }
26351d33d6dcSJiri Olsa 
26366cee6cd3SArnaldo Carvalho de Melo int parse_events_term__clone(struct parse_events_term **new,
26376cee6cd3SArnaldo Carvalho de Melo 			     struct parse_events_term *term)
2638a6146d50SZheng Yan {
263967b49b38SJiri Olsa 	struct parse_events_term temp = {
264067b49b38SJiri Olsa 		.type_val  = term->type_val,
264167b49b38SJiri Olsa 		.type_term = term->type_term,
264267b49b38SJiri Olsa 		.config    = term->config,
264367b49b38SJiri Olsa 		.err_term  = term->err_term,
264467b49b38SJiri Olsa 		.err_val   = term->err_val,
264567b49b38SJiri Olsa 	};
264667b49b38SJiri Olsa 
264767b49b38SJiri Olsa 	return new_term(new, &temp, term->val.str, term->val.num);
2648a6146d50SZheng Yan }
2649a6146d50SZheng Yan 
26508255718fSAndi Kleen int parse_events_copy_term_list(struct list_head *old,
26518255718fSAndi Kleen 				 struct list_head **new)
26528255718fSAndi Kleen {
26538255718fSAndi Kleen 	struct parse_events_term *term, *n;
26548255718fSAndi Kleen 	int ret;
26558255718fSAndi Kleen 
26568255718fSAndi Kleen 	if (!old) {
26578255718fSAndi Kleen 		*new = NULL;
26588255718fSAndi Kleen 		return 0;
26598255718fSAndi Kleen 	}
26608255718fSAndi Kleen 
26618255718fSAndi Kleen 	*new = malloc(sizeof(struct list_head));
26628255718fSAndi Kleen 	if (!*new)
26638255718fSAndi Kleen 		return -ENOMEM;
26648255718fSAndi Kleen 	INIT_LIST_HEAD(*new);
26658255718fSAndi Kleen 
26668255718fSAndi Kleen 	list_for_each_entry (term, old, list) {
26678255718fSAndi Kleen 		ret = parse_events_term__clone(&n, term);
26688255718fSAndi Kleen 		if (ret)
26698255718fSAndi Kleen 			return ret;
26708255718fSAndi Kleen 		list_add_tail(&n->list, *new);
26718255718fSAndi Kleen 	}
26728255718fSAndi Kleen 	return 0;
26738255718fSAndi Kleen }
26748255718fSAndi Kleen 
2675fc0a2c1dSArnaldo Carvalho de Melo void parse_events_terms__purge(struct list_head *terms)
26768f707d84SJiri Olsa {
26776cee6cd3SArnaldo Carvalho de Melo 	struct parse_events_term *term, *h;
26788f707d84SJiri Olsa 
2679a8adfcebSWang Nan 	list_for_each_entry_safe(term, h, terms, list) {
26802d055bf2SWang Nan 		if (term->array.nr_ranges)
2681360e071bSTaeung Song 			zfree(&term->array.ranges);
2682a8adfcebSWang Nan 		list_del_init(&term->list);
26838f707d84SJiri Olsa 		free(term);
26848f707d84SJiri Olsa 	}
2685a8adfcebSWang Nan }
2686b39b8393SJiri Olsa 
26872146afc6SArnaldo Carvalho de Melo void parse_events_terms__delete(struct list_head *terms)
2688fc0a2c1dSArnaldo Carvalho de Melo {
26892146afc6SArnaldo Carvalho de Melo 	if (!terms)
26902146afc6SArnaldo Carvalho de Melo 		return;
2691fc0a2c1dSArnaldo Carvalho de Melo 	parse_events_terms__purge(terms);
2692d20a5f2bSWang Nan 	free(terms);
2693fc0a2c1dSArnaldo Carvalho de Melo }
2694fc0a2c1dSArnaldo Carvalho de Melo 
26952d055bf2SWang Nan void parse_events__clear_array(struct parse_events_array *a)
26962d055bf2SWang Nan {
2697360e071bSTaeung Song 	zfree(&a->ranges);
26982d055bf2SWang Nan }
26992d055bf2SWang Nan 
27005d9cdc11SArnaldo Carvalho de Melo void parse_events_evlist_error(struct parse_events_state *parse_state,
2701b39b8393SJiri Olsa 			       int idx, const char *str)
2702b39b8393SJiri Olsa {
27035d9cdc11SArnaldo Carvalho de Melo 	struct parse_events_error *err = parse_state->error;
2704b39b8393SJiri Olsa 
2705a6ced2beSAdrian Hunter 	if (!err)
2706a6ced2beSAdrian Hunter 		return;
2707b39b8393SJiri Olsa 	err->idx = idx;
2708b39b8393SJiri Olsa 	err->str = strdup(str);
2709b39b8393SJiri Olsa 	WARN_ONCE(!err->str, "WARNING: failed to allocate error string");
2710b39b8393SJiri Olsa }
2711ffeb883eSHe Kuang 
271217cb5f84SWang Nan static void config_terms_list(char *buf, size_t buf_sz)
271317cb5f84SWang Nan {
271417cb5f84SWang Nan 	int i;
271517cb5f84SWang Nan 	bool first = true;
271617cb5f84SWang Nan 
271717cb5f84SWang Nan 	buf[0] = '\0';
271817cb5f84SWang Nan 	for (i = 0; i < __PARSE_EVENTS__TERM_TYPE_NR; i++) {
271917cb5f84SWang Nan 		const char *name = config_term_names[i];
272017cb5f84SWang Nan 
27211669e509SWang Nan 		if (!config_term_avail(i, NULL))
27221669e509SWang Nan 			continue;
272317cb5f84SWang Nan 		if (!name)
272417cb5f84SWang Nan 			continue;
272517cb5f84SWang Nan 		if (name[0] == '<')
272617cb5f84SWang Nan 			continue;
272717cb5f84SWang Nan 
272817cb5f84SWang Nan 		if (strlen(buf) + strlen(name) + 2 >= buf_sz)
272917cb5f84SWang Nan 			return;
273017cb5f84SWang Nan 
273117cb5f84SWang Nan 		if (!first)
273217cb5f84SWang Nan 			strcat(buf, ",");
273317cb5f84SWang Nan 		else
273417cb5f84SWang Nan 			first = false;
273517cb5f84SWang Nan 		strcat(buf, name);
273617cb5f84SWang Nan 	}
273717cb5f84SWang Nan }
273817cb5f84SWang Nan 
2739ffeb883eSHe Kuang /*
2740ffeb883eSHe Kuang  * Return string contains valid config terms of an event.
2741ffeb883eSHe Kuang  * @additional_terms: For terms such as PMU sysfs terms.
2742ffeb883eSHe Kuang  */
2743ffeb883eSHe Kuang char *parse_events_formats_error_string(char *additional_terms)
2744ffeb883eSHe Kuang {
2745ffeb883eSHe Kuang 	char *str;
2746626a6b78SWang Nan 	/* "no-overwrite" is the longest name */
274717cb5f84SWang Nan 	char static_terms[__PARSE_EVENTS__TERM_TYPE_NR *
2748626a6b78SWang Nan 			  (sizeof("no-overwrite") - 1)];
2749ffeb883eSHe Kuang 
275017cb5f84SWang Nan 	config_terms_list(static_terms, sizeof(static_terms));
2751ffeb883eSHe Kuang 	/* valid terms */
2752ffeb883eSHe Kuang 	if (additional_terms) {
275326dee028SWang Nan 		if (asprintf(&str, "valid terms: %s,%s",
275426dee028SWang Nan 			     additional_terms, static_terms) < 0)
2755ffeb883eSHe Kuang 			goto fail;
2756ffeb883eSHe Kuang 	} else {
275726dee028SWang Nan 		if (asprintf(&str, "valid terms: %s", static_terms) < 0)
2758ffeb883eSHe Kuang 			goto fail;
2759ffeb883eSHe Kuang 	}
2760ffeb883eSHe Kuang 	return str;
2761ffeb883eSHe Kuang 
2762ffeb883eSHe Kuang fail:
2763ffeb883eSHe Kuang 	return NULL;
2764ffeb883eSHe Kuang }
2765