xref: /linux/tools/perf/util/parse-events.c (revision b27c4ece725a7f2225f76ad05dc6f3f5463fe893)
1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
2d2709c7cSDavid Howells #include <linux/hw_breakpoint.h>
38dd2a131SJiri Olsa #include <linux/err.h>
47f7c536fSArnaldo Carvalho de Melo #include <linux/zalloc.h>
576b31a29SArnaldo Carvalho de Melo #include <dirent.h>
6a43783aeSArnaldo Carvalho de Melo #include <errno.h>
786a5e0c2SArnaldo Carvalho de Melo #include <sys/ioctl.h>
8bafae98eSArnaldo Carvalho de Melo #include <sys/types.h>
9bafae98eSArnaldo Carvalho de Melo #include <sys/stat.h>
10bafae98eSArnaldo Carvalho de Melo #include <fcntl.h>
11391e4206SArnaldo Carvalho de Melo #include <sys/param.h>
12b0742e90SArnaldo Carvalho de Melo #include "term.h"
136b58e7f1SUlrich Drepper #include "../perf.h"
14361c99a6SArnaldo Carvalho de Melo #include "evlist.h"
1569aad6f1SArnaldo Carvalho de Melo #include "evsel.h"
164b6ab94eSJosh Poimboeuf #include <subcmd/parse-options.h>
1786470930SIngo Molnar #include "parse-events.h"
184b6ab94eSJosh Poimboeuf #include <subcmd/exec-cmd.h>
19a067558eSArnaldo Carvalho de Melo #include "string2.h"
208ec20b17SArnaldo Carvalho de Melo #include "strlist.h"
215aab621bSArnaldo Carvalho de Melo #include "symbol.h"
225beeded1SJason Baron #include "cache.h"
238755a8f2SArjan van de Ven #include "header.h"
2484c86ca1SWang Nan #include "bpf-loader.h"
256e81c74cSMasami Hiramatsu #include "debug.h"
26592d5a6bSJiri Olsa #include <api/fs/tracing_path.h>
27ac20de6fSZheng Yan #include "parse-events-bison.h"
2890e2b22dSJiri Olsa #define YY_EXTRA_TYPE int
2989812fc8SJiri Olsa #include "parse-events-flex.h"
305f537a26SJiri Olsa #include "pmu.h"
31b41f1cecSNamhyung Kim #include "thread_map.h"
32f30a79b0SJiri Olsa #include "cpumap.h"
3340218daeSMasami Hiramatsu #include "probe-file.h"
34b39b8393SJiri Olsa #include "asm/bug.h"
35ac12f676SAndi Kleen #include "util/parse-branch-options.h"
3671b0acceSAndi Kleen #include "metricgroup.h"
3789812fc8SJiri Olsa 
3889812fc8SJiri Olsa #define MAX_NAME_LEN 100
3986470930SIngo Molnar 
4082ba1f2fSJiri Olsa #ifdef PARSER_DEBUG
4182ba1f2fSJiri Olsa extern int parse_events_debug;
4282ba1f2fSJiri Olsa #endif
435d9cdc11SArnaldo Carvalho de Melo int parse_events_parse(void *parse_state, void *scanner);
44e637d177SHe Kuang static int get_config_terms(struct list_head *head_config,
45e637d177SHe Kuang 			    struct list_head *head_terms __maybe_unused);
46bcd3279fSFrederic Weisbecker 
47dcb4e102SKan Liang static struct perf_pmu_event_symbol *perf_pmu_events_list;
48dcb4e102SKan Liang /*
49dcb4e102SKan Liang  * The variable indicates the number of supported pmu event symbols.
50dcb4e102SKan Liang  * 0 means not initialized and ready to init
51dcb4e102SKan Liang  * -1 means failed to init, don't try anymore
52dcb4e102SKan Liang  * >0 is the number of supported pmu event symbols
53dcb4e102SKan Liang  */
54dcb4e102SKan Liang static int perf_pmu_events_list_num;
55dcb4e102SKan Liang 
56705750f2SYunlong Song struct event_symbol event_symbols_hw[PERF_COUNT_HW_MAX] = {
571dc12760SJiri Olsa 	[PERF_COUNT_HW_CPU_CYCLES] = {
581dc12760SJiri Olsa 		.symbol = "cpu-cycles",
591dc12760SJiri Olsa 		.alias  = "cycles",
601dc12760SJiri Olsa 	},
611dc12760SJiri Olsa 	[PERF_COUNT_HW_INSTRUCTIONS] = {
621dc12760SJiri Olsa 		.symbol = "instructions",
631dc12760SJiri Olsa 		.alias  = "",
641dc12760SJiri Olsa 	},
651dc12760SJiri Olsa 	[PERF_COUNT_HW_CACHE_REFERENCES] = {
661dc12760SJiri Olsa 		.symbol = "cache-references",
671dc12760SJiri Olsa 		.alias  = "",
681dc12760SJiri Olsa 	},
691dc12760SJiri Olsa 	[PERF_COUNT_HW_CACHE_MISSES] = {
701dc12760SJiri Olsa 		.symbol = "cache-misses",
711dc12760SJiri Olsa 		.alias  = "",
721dc12760SJiri Olsa 	},
731dc12760SJiri Olsa 	[PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = {
741dc12760SJiri Olsa 		.symbol = "branch-instructions",
751dc12760SJiri Olsa 		.alias  = "branches",
761dc12760SJiri Olsa 	},
771dc12760SJiri Olsa 	[PERF_COUNT_HW_BRANCH_MISSES] = {
781dc12760SJiri Olsa 		.symbol = "branch-misses",
791dc12760SJiri Olsa 		.alias  = "",
801dc12760SJiri Olsa 	},
811dc12760SJiri Olsa 	[PERF_COUNT_HW_BUS_CYCLES] = {
821dc12760SJiri Olsa 		.symbol = "bus-cycles",
831dc12760SJiri Olsa 		.alias  = "",
841dc12760SJiri Olsa 	},
851dc12760SJiri Olsa 	[PERF_COUNT_HW_STALLED_CYCLES_FRONTEND] = {
861dc12760SJiri Olsa 		.symbol = "stalled-cycles-frontend",
871dc12760SJiri Olsa 		.alias  = "idle-cycles-frontend",
881dc12760SJiri Olsa 	},
891dc12760SJiri Olsa 	[PERF_COUNT_HW_STALLED_CYCLES_BACKEND] = {
901dc12760SJiri Olsa 		.symbol = "stalled-cycles-backend",
911dc12760SJiri Olsa 		.alias  = "idle-cycles-backend",
921dc12760SJiri Olsa 	},
931dc12760SJiri Olsa 	[PERF_COUNT_HW_REF_CPU_CYCLES] = {
941dc12760SJiri Olsa 		.symbol = "ref-cycles",
951dc12760SJiri Olsa 		.alias  = "",
961dc12760SJiri Olsa 	},
971dc12760SJiri Olsa };
9886470930SIngo Molnar 
99705750f2SYunlong Song struct event_symbol event_symbols_sw[PERF_COUNT_SW_MAX] = {
1001dc12760SJiri Olsa 	[PERF_COUNT_SW_CPU_CLOCK] = {
1011dc12760SJiri Olsa 		.symbol = "cpu-clock",
1021dc12760SJiri Olsa 		.alias  = "",
1031dc12760SJiri Olsa 	},
1041dc12760SJiri Olsa 	[PERF_COUNT_SW_TASK_CLOCK] = {
1051dc12760SJiri Olsa 		.symbol = "task-clock",
1061dc12760SJiri Olsa 		.alias  = "",
1071dc12760SJiri Olsa 	},
1081dc12760SJiri Olsa 	[PERF_COUNT_SW_PAGE_FAULTS] = {
1091dc12760SJiri Olsa 		.symbol = "page-faults",
1101dc12760SJiri Olsa 		.alias  = "faults",
1111dc12760SJiri Olsa 	},
1121dc12760SJiri Olsa 	[PERF_COUNT_SW_CONTEXT_SWITCHES] = {
1131dc12760SJiri Olsa 		.symbol = "context-switches",
1141dc12760SJiri Olsa 		.alias  = "cs",
1151dc12760SJiri Olsa 	},
1161dc12760SJiri Olsa 	[PERF_COUNT_SW_CPU_MIGRATIONS] = {
1171dc12760SJiri Olsa 		.symbol = "cpu-migrations",
1181dc12760SJiri Olsa 		.alias  = "migrations",
1191dc12760SJiri Olsa 	},
1201dc12760SJiri Olsa 	[PERF_COUNT_SW_PAGE_FAULTS_MIN] = {
1211dc12760SJiri Olsa 		.symbol = "minor-faults",
1221dc12760SJiri Olsa 		.alias  = "",
1231dc12760SJiri Olsa 	},
1241dc12760SJiri Olsa 	[PERF_COUNT_SW_PAGE_FAULTS_MAJ] = {
1251dc12760SJiri Olsa 		.symbol = "major-faults",
1261dc12760SJiri Olsa 		.alias  = "",
1271dc12760SJiri Olsa 	},
1281dc12760SJiri Olsa 	[PERF_COUNT_SW_ALIGNMENT_FAULTS] = {
1291dc12760SJiri Olsa 		.symbol = "alignment-faults",
1301dc12760SJiri Olsa 		.alias  = "",
1311dc12760SJiri Olsa 	},
1321dc12760SJiri Olsa 	[PERF_COUNT_SW_EMULATION_FAULTS] = {
1331dc12760SJiri Olsa 		.symbol = "emulation-faults",
1341dc12760SJiri Olsa 		.alias  = "",
1351dc12760SJiri Olsa 	},
136d22d1a2aSAdrian Hunter 	[PERF_COUNT_SW_DUMMY] = {
137d22d1a2aSAdrian Hunter 		.symbol = "dummy",
138d22d1a2aSAdrian Hunter 		.alias  = "",
139d22d1a2aSAdrian Hunter 	},
140bae9cc41SArnaldo Carvalho de Melo 	[PERF_COUNT_SW_BPF_OUTPUT] = {
141bae9cc41SArnaldo Carvalho de Melo 		.symbol = "bpf-output",
142bae9cc41SArnaldo Carvalho de Melo 		.alias  = "",
143bae9cc41SArnaldo Carvalho de Melo 	},
14486470930SIngo Molnar };
14586470930SIngo Molnar 
146cdd6c482SIngo Molnar #define __PERF_EVENT_FIELD(config, name) \
147cdd6c482SIngo Molnar 	((config & PERF_EVENT_##name##_MASK) >> PERF_EVENT_##name##_SHIFT)
14886470930SIngo Molnar 
149cdd6c482SIngo Molnar #define PERF_EVENT_RAW(config)		__PERF_EVENT_FIELD(config, RAW)
150cdd6c482SIngo Molnar #define PERF_EVENT_CONFIG(config)	__PERF_EVENT_FIELD(config, CONFIG)
151cdd6c482SIngo Molnar #define PERF_EVENT_TYPE(config)		__PERF_EVENT_FIELD(config, TYPE)
152cdd6c482SIngo Molnar #define PERF_EVENT_ID(config)		__PERF_EVENT_FIELD(config, EVENT)
15386470930SIngo Molnar 
15422a9f41bSArnaldo Carvalho de Melo #define for_each_subsystem(sys_dir, sys_dirent)			\
15522a9f41bSArnaldo Carvalho de Melo 	while ((sys_dirent = readdir(sys_dir)) != NULL)		\
15622a9f41bSArnaldo Carvalho de Melo 		if (sys_dirent->d_type == DT_DIR &&		\
15722a9f41bSArnaldo Carvalho de Melo 		    (strcmp(sys_dirent->d_name, ".")) &&	\
15822a9f41bSArnaldo Carvalho de Melo 		    (strcmp(sys_dirent->d_name, "..")))
159f6bdafefSJason Baron 
160c02cab22SArnaldo Carvalho de Melo static int tp_event_has_id(const char *dir_path, struct dirent *evt_dir)
161ae07b63fSPeter Zijlstra {
162ae07b63fSPeter Zijlstra 	char evt_path[MAXPATHLEN];
163ae07b63fSPeter Zijlstra 	int fd;
164ae07b63fSPeter Zijlstra 
165c02cab22SArnaldo Carvalho de Melo 	snprintf(evt_path, MAXPATHLEN, "%s/%s/id", dir_path, evt_dir->d_name);
166ae07b63fSPeter Zijlstra 	fd = open(evt_path, O_RDONLY);
167ae07b63fSPeter Zijlstra 	if (fd < 0)
168ae07b63fSPeter Zijlstra 		return -EINVAL;
169ae07b63fSPeter Zijlstra 	close(fd);
170ae07b63fSPeter Zijlstra 
171ae07b63fSPeter Zijlstra 	return 0;
172ae07b63fSPeter Zijlstra }
173ae07b63fSPeter Zijlstra 
174c02cab22SArnaldo Carvalho de Melo #define for_each_event(dir_path, evt_dir, evt_dirent)		\
17522a9f41bSArnaldo Carvalho de Melo 	while ((evt_dirent = readdir(evt_dir)) != NULL)		\
17622a9f41bSArnaldo Carvalho de Melo 		if (evt_dirent->d_type == DT_DIR &&		\
17722a9f41bSArnaldo Carvalho de Melo 		    (strcmp(evt_dirent->d_name, ".")) &&	\
17822a9f41bSArnaldo Carvalho de Melo 		    (strcmp(evt_dirent->d_name, "..")) &&	\
179c02cab22SArnaldo Carvalho de Melo 		    (!tp_event_has_id(dir_path, evt_dirent)))
180f6bdafefSJason Baron 
181270bbbe8SLi Zefan #define MAX_EVENT_LENGTH 512
182f6bdafefSJason Baron 
183f6bdafefSJason Baron 
1841ef2ed10SFrederic Weisbecker struct tracepoint_path *tracepoint_id_to_path(u64 config)
185f6bdafefSJason Baron {
1861ef2ed10SFrederic Weisbecker 	struct tracepoint_path *path = NULL;
187f6bdafefSJason Baron 	DIR *sys_dir, *evt_dir;
18822a9f41bSArnaldo Carvalho de Melo 	struct dirent *sys_dirent, *evt_dirent;
1898aa8a7c8SStephane Eranian 	char id_buf[24];
190725b1368SEric Dumazet 	int fd;
191f6bdafefSJason Baron 	u64 id;
192f6bdafefSJason Baron 	char evt_path[MAXPATHLEN];
19325a7d914SArnaldo Carvalho de Melo 	char *dir_path;
194f6bdafefSJason Baron 
1957014e0e3SArnaldo Carvalho de Melo 	sys_dir = tracing_events__opendir();
196f6bdafefSJason Baron 	if (!sys_dir)
197725b1368SEric Dumazet 		return NULL;
198f6bdafefSJason Baron 
19922a9f41bSArnaldo Carvalho de Melo 	for_each_subsystem(sys_dir, sys_dirent) {
20025a7d914SArnaldo Carvalho de Melo 		dir_path = get_events_file(sys_dirent->d_name);
20125a7d914SArnaldo Carvalho de Melo 		if (!dir_path)
20225a7d914SArnaldo Carvalho de Melo 			continue;
203725b1368SEric Dumazet 		evt_dir = opendir(dir_path);
204725b1368SEric Dumazet 		if (!evt_dir)
20525a7d914SArnaldo Carvalho de Melo 			goto next;
206725b1368SEric Dumazet 
207c02cab22SArnaldo Carvalho de Melo 		for_each_event(dir_path, evt_dir, evt_dirent) {
208725b1368SEric Dumazet 
20977f18153SJiri Olsa 			scnprintf(evt_path, MAXPATHLEN, "%s/%s/id", dir_path,
21022a9f41bSArnaldo Carvalho de Melo 				  evt_dirent->d_name);
211725b1368SEric Dumazet 			fd = open(evt_path, O_RDONLY);
212f6bdafefSJason Baron 			if (fd < 0)
213f6bdafefSJason Baron 				continue;
214f6bdafefSJason Baron 			if (read(fd, id_buf, sizeof(id_buf)) < 0) {
215f6bdafefSJason Baron 				close(fd);
216f6bdafefSJason Baron 				continue;
217f6bdafefSJason Baron 			}
218f6bdafefSJason Baron 			close(fd);
219f6bdafefSJason Baron 			id = atoll(id_buf);
220f6bdafefSJason Baron 			if (id == config) {
22125a7d914SArnaldo Carvalho de Melo 				put_events_file(dir_path);
222f6bdafefSJason Baron 				closedir(evt_dir);
223f6bdafefSJason Baron 				closedir(sys_dir);
22459b4caebSJulia Lawall 				path = zalloc(sizeof(*path));
2255aa365f2STaeung Song 				if (!path)
2265aa365f2STaeung Song 					return NULL;
2271ef2ed10SFrederic Weisbecker 				path->system = malloc(MAX_EVENT_LENGTH);
2281ef2ed10SFrederic Weisbecker 				if (!path->system) {
2291ef2ed10SFrederic Weisbecker 					free(path);
2301ef2ed10SFrederic Weisbecker 					return NULL;
2311ef2ed10SFrederic Weisbecker 				}
2321ef2ed10SFrederic Weisbecker 				path->name = malloc(MAX_EVENT_LENGTH);
2331ef2ed10SFrederic Weisbecker 				if (!path->name) {
23474cf249dSArnaldo Carvalho de Melo 					zfree(&path->system);
2351ef2ed10SFrederic Weisbecker 					free(path);
2361ef2ed10SFrederic Weisbecker 					return NULL;
2371ef2ed10SFrederic Weisbecker 				}
23822a9f41bSArnaldo Carvalho de Melo 				strncpy(path->system, sys_dirent->d_name,
2391ef2ed10SFrederic Weisbecker 					MAX_EVENT_LENGTH);
24022a9f41bSArnaldo Carvalho de Melo 				strncpy(path->name, evt_dirent->d_name,
2411ef2ed10SFrederic Weisbecker 					MAX_EVENT_LENGTH);
2421ef2ed10SFrederic Weisbecker 				return path;
243f6bdafefSJason Baron 			}
244f6bdafefSJason Baron 		}
245f6bdafefSJason Baron 		closedir(evt_dir);
24625a7d914SArnaldo Carvalho de Melo next:
24725a7d914SArnaldo Carvalho de Melo 		put_events_file(dir_path);
248f6bdafefSJason Baron 	}
249f6bdafefSJason Baron 
250f6bdafefSJason Baron 	closedir(sys_dir);
2511ef2ed10SFrederic Weisbecker 	return NULL;
2521ef2ed10SFrederic Weisbecker }
2531ef2ed10SFrederic Weisbecker 
254e7c93f09SNamhyung Kim struct tracepoint_path *tracepoint_name_to_path(const char *name)
255e7c93f09SNamhyung Kim {
256e7c93f09SNamhyung Kim 	struct tracepoint_path *path = zalloc(sizeof(*path));
257e7c93f09SNamhyung Kim 	char *str = strchr(name, ':');
258e7c93f09SNamhyung Kim 
259e7c93f09SNamhyung Kim 	if (path == NULL || str == NULL) {
260e7c93f09SNamhyung Kim 		free(path);
261e7c93f09SNamhyung Kim 		return NULL;
262e7c93f09SNamhyung Kim 	}
263e7c93f09SNamhyung Kim 
264e7c93f09SNamhyung Kim 	path->system = strndup(name, str - name);
265e7c93f09SNamhyung Kim 	path->name = strdup(str+1);
266e7c93f09SNamhyung Kim 
267e7c93f09SNamhyung Kim 	if (path->system == NULL || path->name == NULL) {
26874cf249dSArnaldo Carvalho de Melo 		zfree(&path->system);
26974cf249dSArnaldo Carvalho de Melo 		zfree(&path->name);
270506fde11STaeung Song 		zfree(&path);
271e7c93f09SNamhyung Kim 	}
272e7c93f09SNamhyung Kim 
273e7c93f09SNamhyung Kim 	return path;
274e7c93f09SNamhyung Kim }
275e7c93f09SNamhyung Kim 
2761424dc96SDavid Ahern const char *event_type(int type)
2771424dc96SDavid Ahern {
2781424dc96SDavid Ahern 	switch (type) {
2791424dc96SDavid Ahern 	case PERF_TYPE_HARDWARE:
2801424dc96SDavid Ahern 		return "hardware";
2811424dc96SDavid Ahern 
2821424dc96SDavid Ahern 	case PERF_TYPE_SOFTWARE:
2831424dc96SDavid Ahern 		return "software";
2841424dc96SDavid Ahern 
2851424dc96SDavid Ahern 	case PERF_TYPE_TRACEPOINT:
2861424dc96SDavid Ahern 		return "tracepoint";
2871424dc96SDavid Ahern 
2881424dc96SDavid Ahern 	case PERF_TYPE_HW_CACHE:
2891424dc96SDavid Ahern 		return "hardware-cache";
2901424dc96SDavid Ahern 
2911424dc96SDavid Ahern 	default:
2921424dc96SDavid Ahern 		break;
2931424dc96SDavid Ahern 	}
2941424dc96SDavid Ahern 
2951424dc96SDavid Ahern 	return "unknown";
2961424dc96SDavid Ahern }
2971424dc96SDavid Ahern 
298e814fdddSWang Nan static int parse_events__is_name_term(struct parse_events_term *term)
299e814fdddSWang Nan {
300e814fdddSWang Nan 	return term->type_term == PARSE_EVENTS__TERM_TYPE_NAME;
301e814fdddSWang Nan }
3027ae92e74SYan, Zheng 
303e814fdddSWang Nan static char *get_config_name(struct list_head *head_terms)
304e814fdddSWang Nan {
305e814fdddSWang Nan 	struct parse_events_term *term;
306e814fdddSWang Nan 
307e814fdddSWang Nan 	if (!head_terms)
308e814fdddSWang Nan 		return NULL;
309e814fdddSWang Nan 
310e814fdddSWang Nan 	list_for_each_entry(term, head_terms, list)
311e814fdddSWang Nan 		if (parse_events__is_name_term(term))
312e814fdddSWang Nan 			return term->val.str;
313e814fdddSWang Nan 
314e814fdddSWang Nan 	return NULL;
315e814fdddSWang Nan }
3167ae92e74SYan, Zheng 
31732dcd021SJiri Olsa static struct evsel *
318410136f5SStephane Eranian __add_event(struct list_head *list, int *idx,
3197ae92e74SYan, Zheng 	    struct perf_event_attr *attr,
32066ec1191SMark Rutland 	    char *name, struct perf_pmu *pmu,
321f0fbb114SAndi Kleen 	    struct list_head *config_terms, bool auto_merge_stats,
322f0fbb114SAndi Kleen 	    const char *cpu_list)
32389812fc8SJiri Olsa {
32432dcd021SJiri Olsa 	struct evsel *evsel;
325f854839bSJiri Olsa 	struct perf_cpu_map *cpus = pmu ? pmu->cpus :
326f0fbb114SAndi Kleen 			       cpu_list ? cpu_map__new(cpu_list) : NULL;
32789812fc8SJiri Olsa 
32889812fc8SJiri Olsa 	event_attr_init(attr);
32989812fc8SJiri Olsa 
33075fc5ae5STaeung Song 	evsel = perf_evsel__new_idx(attr, *idx);
331c5cd8ac0SDavid Ahern 	if (!evsel)
332410136f5SStephane Eranian 		return NULL;
33389812fc8SJiri Olsa 
33475fc5ae5STaeung Song 	(*idx)++;
33538f01d8dSJiri Olsa 	evsel->cpus        = perf_cpu_map__get(cpus);
33638f01d8dSJiri Olsa 	evsel->own_cpus    = perf_cpu_map__get(cpus);
33766ec1191SMark Rutland 	evsel->system_wide = pmu ? pmu->is_uncore : false;
33863ce8449SArnaldo Carvalho de Melo 	evsel->auto_merge_stats = auto_merge_stats;
339f30a79b0SJiri Olsa 
3409db1763cSArnaldo Carvalho de Melo 	if (name)
34189812fc8SJiri Olsa 		evsel->name = strdup(name);
342930a2e29SJiri Olsa 
343930a2e29SJiri Olsa 	if (config_terms)
344930a2e29SJiri Olsa 		list_splice(config_terms, &evsel->config_terms);
345930a2e29SJiri Olsa 
346*b27c4eceSJiri Olsa 	list_add_tail(&evsel->core.node, list);
347410136f5SStephane Eranian 	return evsel;
34889812fc8SJiri Olsa }
34989812fc8SJiri Olsa 
350c5cd8ac0SDavid Ahern static int add_event(struct list_head *list, int *idx,
351930a2e29SJiri Olsa 		     struct perf_event_attr *attr, char *name,
352930a2e29SJiri Olsa 		     struct list_head *config_terms)
3537ae92e74SYan, Zheng {
354f0fbb114SAndi Kleen 	return __add_event(list, idx, attr, name, NULL, config_terms, false, NULL) ? 0 : -ENOMEM;
355f0fbb114SAndi Kleen }
356f0fbb114SAndi Kleen 
357f0fbb114SAndi Kleen static int add_event_tool(struct list_head *list, int *idx,
358f0fbb114SAndi Kleen 			  enum perf_tool_event tool_event)
359f0fbb114SAndi Kleen {
36032dcd021SJiri Olsa 	struct evsel *evsel;
361f0fbb114SAndi Kleen 	struct perf_event_attr attr = {
362f0fbb114SAndi Kleen 		.type = PERF_TYPE_SOFTWARE,
363f0fbb114SAndi Kleen 		.config = PERF_COUNT_SW_DUMMY,
364f0fbb114SAndi Kleen 	};
365f0fbb114SAndi Kleen 
366f0fbb114SAndi Kleen 	evsel = __add_event(list, idx, &attr, NULL, NULL, NULL, false, "0");
367f0fbb114SAndi Kleen 	if (!evsel)
368f0fbb114SAndi Kleen 		return -ENOMEM;
369f0fbb114SAndi Kleen 	evsel->tool_event = tool_event;
370f0fbb114SAndi Kleen 	if (tool_event == PERF_TOOL_DURATION_TIME)
371f0fbb114SAndi Kleen 		evsel->unit = strdup("ns");
372f0fbb114SAndi Kleen 	return 0;
3737ae92e74SYan, Zheng }
3747ae92e74SYan, Zheng 
3750b668bc9SArnaldo Carvalho de Melo static int parse_aliases(char *str, const char *names[][PERF_EVSEL__MAX_ALIASES], int size)
37686470930SIngo Molnar {
37786470930SIngo Molnar 	int i, j;
37861c45981SPaul Mackerras 	int n, longest = -1;
37986470930SIngo Molnar 
38086470930SIngo Molnar 	for (i = 0; i < size; i++) {
3810b668bc9SArnaldo Carvalho de Melo 		for (j = 0; j < PERF_EVSEL__MAX_ALIASES && names[i][j]; j++) {
38261c45981SPaul Mackerras 			n = strlen(names[i][j]);
38389812fc8SJiri Olsa 			if (n > longest && !strncasecmp(str, names[i][j], n))
38461c45981SPaul Mackerras 				longest = n;
38561c45981SPaul Mackerras 		}
38689812fc8SJiri Olsa 		if (longest > 0)
38786470930SIngo Molnar 			return i;
38886470930SIngo Molnar 	}
38986470930SIngo Molnar 
3908953645fSIngo Molnar 	return -1;
39186470930SIngo Molnar }
39286470930SIngo Molnar 
39343d0b978SWang Nan typedef int config_term_func_t(struct perf_event_attr *attr,
39443d0b978SWang Nan 			       struct parse_events_term *term,
39543d0b978SWang Nan 			       struct parse_events_error *err);
39643d0b978SWang Nan static int config_term_common(struct perf_event_attr *attr,
39743d0b978SWang Nan 			      struct parse_events_term *term,
39843d0b978SWang Nan 			      struct parse_events_error *err);
39943d0b978SWang Nan static int config_attr(struct perf_event_attr *attr,
40043d0b978SWang Nan 		       struct list_head *head,
40143d0b978SWang Nan 		       struct parse_events_error *err,
40243d0b978SWang Nan 		       config_term_func_t config_term);
40343d0b978SWang Nan 
404c5cd8ac0SDavid Ahern int parse_events_add_cache(struct list_head *list, int *idx,
40543d0b978SWang Nan 			   char *type, char *op_result1, char *op_result2,
4062c97b0d4SArnaldo Carvalho de Melo 			   struct parse_events_error *err,
40743d0b978SWang Nan 			   struct list_head *head_config)
40886470930SIngo Molnar {
40989812fc8SJiri Olsa 	struct perf_event_attr attr;
41043d0b978SWang Nan 	LIST_HEAD(config_terms);
41143d0b978SWang Nan 	char name[MAX_NAME_LEN], *config_name;
41261c45981SPaul Mackerras 	int cache_type = -1, cache_op = -1, cache_result = -1;
41389812fc8SJiri Olsa 	char *op_result[2] = { op_result1, op_result2 };
41489812fc8SJiri Olsa 	int i, n;
41586470930SIngo Molnar 
41686470930SIngo Molnar 	/*
41786470930SIngo Molnar 	 * No fallback - if we cannot get a clear cache type
41886470930SIngo Molnar 	 * then bail out:
41986470930SIngo Molnar 	 */
4200b668bc9SArnaldo Carvalho de Melo 	cache_type = parse_aliases(type, perf_evsel__hw_cache,
42189812fc8SJiri Olsa 				   PERF_COUNT_HW_CACHE_MAX);
42286470930SIngo Molnar 	if (cache_type == -1)
42389812fc8SJiri Olsa 		return -EINVAL;
42486470930SIngo Molnar 
42543d0b978SWang Nan 	config_name = get_config_name(head_config);
42689812fc8SJiri Olsa 	n = snprintf(name, MAX_NAME_LEN, "%s", type);
42789812fc8SJiri Olsa 
42889812fc8SJiri Olsa 	for (i = 0; (i < 2) && (op_result[i]); i++) {
42989812fc8SJiri Olsa 		char *str = op_result[i];
43089812fc8SJiri Olsa 
431275ef387SJiri Olsa 		n += snprintf(name + n, MAX_NAME_LEN - n, "-%s", str);
43261c45981SPaul Mackerras 
43361c45981SPaul Mackerras 		if (cache_op == -1) {
4340b668bc9SArnaldo Carvalho de Melo 			cache_op = parse_aliases(str, perf_evsel__hw_cache_op,
43561c45981SPaul Mackerras 						 PERF_COUNT_HW_CACHE_OP_MAX);
43661c45981SPaul Mackerras 			if (cache_op >= 0) {
4370b668bc9SArnaldo Carvalho de Melo 				if (!perf_evsel__is_cache_op_valid(cache_type, cache_op))
43889812fc8SJiri Olsa 					return -EINVAL;
43961c45981SPaul Mackerras 				continue;
44061c45981SPaul Mackerras 			}
44161c45981SPaul Mackerras 		}
44261c45981SPaul Mackerras 
44361c45981SPaul Mackerras 		if (cache_result == -1) {
4440b668bc9SArnaldo Carvalho de Melo 			cache_result = parse_aliases(str, perf_evsel__hw_cache_result,
44561c45981SPaul Mackerras 						     PERF_COUNT_HW_CACHE_RESULT_MAX);
44661c45981SPaul Mackerras 			if (cache_result >= 0)
44761c45981SPaul Mackerras 				continue;
44861c45981SPaul Mackerras 		}
44961c45981SPaul Mackerras 	}
45061c45981SPaul Mackerras 
45186470930SIngo Molnar 	/*
45286470930SIngo Molnar 	 * Fall back to reads:
45386470930SIngo Molnar 	 */
4548953645fSIngo Molnar 	if (cache_op == -1)
4558953645fSIngo Molnar 		cache_op = PERF_COUNT_HW_CACHE_OP_READ;
45686470930SIngo Molnar 
45786470930SIngo Molnar 	/*
45886470930SIngo Molnar 	 * Fall back to accesses:
45986470930SIngo Molnar 	 */
46086470930SIngo Molnar 	if (cache_result == -1)
46186470930SIngo Molnar 		cache_result = PERF_COUNT_HW_CACHE_RESULT_ACCESS;
46286470930SIngo Molnar 
46389812fc8SJiri Olsa 	memset(&attr, 0, sizeof(attr));
46489812fc8SJiri Olsa 	attr.config = cache_type | (cache_op << 8) | (cache_result << 16);
46589812fc8SJiri Olsa 	attr.type = PERF_TYPE_HW_CACHE;
46643d0b978SWang Nan 
46743d0b978SWang Nan 	if (head_config) {
4682c97b0d4SArnaldo Carvalho de Melo 		if (config_attr(&attr, head_config, err,
46943d0b978SWang Nan 				config_term_common))
47043d0b978SWang Nan 			return -EINVAL;
47143d0b978SWang Nan 
47243d0b978SWang Nan 		if (get_config_terms(head_config, &config_terms))
47343d0b978SWang Nan 			return -ENOMEM;
47443d0b978SWang Nan 	}
47543d0b978SWang Nan 	return add_event(list, idx, &attr, config_name ? : name, &config_terms);
47686470930SIngo Molnar }
47786470930SIngo Molnar 
478272ed29aSJiri Olsa static void tracepoint_error(struct parse_events_error *e, int err,
4798c619d6aSWang Nan 			     const char *sys, const char *name)
48019658171SJiri Olsa {
48119658171SJiri Olsa 	char help[BUFSIZ];
48219658171SJiri Olsa 
483ec183d22SAdrian Hunter 	if (!e)
484ec183d22SAdrian Hunter 		return;
485ec183d22SAdrian Hunter 
48619658171SJiri Olsa 	/*
48719658171SJiri Olsa 	 * We get error directly from syscall errno ( > 0),
48819658171SJiri Olsa 	 * or from encoded pointer's error ( < 0).
48919658171SJiri Olsa 	 */
49019658171SJiri Olsa 	err = abs(err);
49119658171SJiri Olsa 
49219658171SJiri Olsa 	switch (err) {
49319658171SJiri Olsa 	case EACCES:
494272ed29aSJiri Olsa 		e->str = strdup("can't access trace events");
49519658171SJiri Olsa 		break;
49619658171SJiri Olsa 	case ENOENT:
497272ed29aSJiri Olsa 		e->str = strdup("unknown tracepoint");
49819658171SJiri Olsa 		break;
49919658171SJiri Olsa 	default:
500272ed29aSJiri Olsa 		e->str = strdup("failed to add tracepoint");
50119658171SJiri Olsa 		break;
50219658171SJiri Olsa 	}
50319658171SJiri Olsa 
50419658171SJiri Olsa 	tracing_path__strerror_open_tp(err, help, sizeof(help), sys, name);
505272ed29aSJiri Olsa 	e->help = strdup(help);
50619658171SJiri Olsa }
50719658171SJiri Olsa 
508c5cd8ac0SDavid Ahern static int add_tracepoint(struct list_head *list, int *idx,
5098c619d6aSWang Nan 			  const char *sys_name, const char *evt_name,
510272ed29aSJiri Olsa 			  struct parse_events_error *err,
511e637d177SHe Kuang 			  struct list_head *head_config)
512bcd3279fSFrederic Weisbecker {
51332dcd021SJiri Olsa 	struct evsel *evsel;
514bcd3279fSFrederic Weisbecker 
515ef503831SArnaldo Carvalho de Melo 	evsel = perf_evsel__newtp_idx(sys_name, evt_name, (*idx)++);
51619658171SJiri Olsa 	if (IS_ERR(evsel)) {
517272ed29aSJiri Olsa 		tracepoint_error(err, PTR_ERR(evsel), sys_name, evt_name);
5188dd2a131SJiri Olsa 		return PTR_ERR(evsel);
51919658171SJiri Olsa 	}
520bcd3279fSFrederic Weisbecker 
521e637d177SHe Kuang 	if (head_config) {
522e637d177SHe Kuang 		LIST_HEAD(config_terms);
523e637d177SHe Kuang 
524e637d177SHe Kuang 		if (get_config_terms(head_config, &config_terms))
525e637d177SHe Kuang 			return -ENOMEM;
526e637d177SHe Kuang 		list_splice(&config_terms, &evsel->config_terms);
527e637d177SHe Kuang 	}
528e637d177SHe Kuang 
529*b27c4eceSJiri Olsa 	list_add_tail(&evsel->core.node, list);
53082fe1c29SArnaldo Carvalho de Melo 	return 0;
531bcd3279fSFrederic Weisbecker }
532bcd3279fSFrederic Weisbecker 
533c5cd8ac0SDavid Ahern static int add_tracepoint_multi_event(struct list_head *list, int *idx,
5348c619d6aSWang Nan 				      const char *sys_name, const char *evt_name,
535272ed29aSJiri Olsa 				      struct parse_events_error *err,
536e637d177SHe Kuang 				      struct list_head *head_config)
537bcd3279fSFrederic Weisbecker {
53825a7d914SArnaldo Carvalho de Melo 	char *evt_path;
539bcd3279fSFrederic Weisbecker 	struct dirent *evt_ent;
540bcd3279fSFrederic Weisbecker 	DIR *evt_dir;
54127bf90bfSJiri Olsa 	int ret = 0, found = 0;
542bcd3279fSFrederic Weisbecker 
54325a7d914SArnaldo Carvalho de Melo 	evt_path = get_events_file(sys_name);
54425a7d914SArnaldo Carvalho de Melo 	if (!evt_path) {
54525a7d914SArnaldo Carvalho de Melo 		tracepoint_error(err, errno, sys_name, evt_name);
54625a7d914SArnaldo Carvalho de Melo 		return -1;
54725a7d914SArnaldo Carvalho de Melo 	}
548bcd3279fSFrederic Weisbecker 	evt_dir = opendir(evt_path);
549bcd3279fSFrederic Weisbecker 	if (!evt_dir) {
55025a7d914SArnaldo Carvalho de Melo 		put_events_file(evt_path);
551272ed29aSJiri Olsa 		tracepoint_error(err, errno, sys_name, evt_name);
55289812fc8SJiri Olsa 		return -1;
553bcd3279fSFrederic Weisbecker 	}
554bcd3279fSFrederic Weisbecker 
55589812fc8SJiri Olsa 	while (!ret && (evt_ent = readdir(evt_dir))) {
556bcd3279fSFrederic Weisbecker 		if (!strcmp(evt_ent->d_name, ".")
557bcd3279fSFrederic Weisbecker 		    || !strcmp(evt_ent->d_name, "..")
558bcd3279fSFrederic Weisbecker 		    || !strcmp(evt_ent->d_name, "enable")
559bcd3279fSFrederic Weisbecker 		    || !strcmp(evt_ent->d_name, "filter"))
560bcd3279fSFrederic Weisbecker 			continue;
561bcd3279fSFrederic Weisbecker 
56289812fc8SJiri Olsa 		if (!strglobmatch(evt_ent->d_name, evt_name))
563fb1d2edfSMasami Hiramatsu 			continue;
564fb1d2edfSMasami Hiramatsu 
56527bf90bfSJiri Olsa 		found++;
56627bf90bfSJiri Olsa 
567e637d177SHe Kuang 		ret = add_tracepoint(list, idx, sys_name, evt_ent->d_name,
568272ed29aSJiri Olsa 				     err, head_config);
569bcd3279fSFrederic Weisbecker 	}
570bcd3279fSFrederic Weisbecker 
57127bf90bfSJiri Olsa 	if (!found) {
57227bf90bfSJiri Olsa 		tracepoint_error(err, ENOENT, sys_name, evt_name);
57327bf90bfSJiri Olsa 		ret = -1;
57427bf90bfSJiri Olsa 	}
57527bf90bfSJiri Olsa 
57625a7d914SArnaldo Carvalho de Melo 	put_events_file(evt_path);
5770bd3f084SJiri Olsa 	closedir(evt_dir);
57889812fc8SJiri Olsa 	return ret;
579bcd3279fSFrederic Weisbecker }
580bcd3279fSFrederic Weisbecker 
581c5cd8ac0SDavid Ahern static int add_tracepoint_event(struct list_head *list, int *idx,
5828c619d6aSWang Nan 				const char *sys_name, const char *evt_name,
583272ed29aSJiri Olsa 				struct parse_events_error *err,
584e637d177SHe Kuang 				struct list_head *head_config)
585f35488f9SJiri Olsa {
586f35488f9SJiri Olsa 	return strpbrk(evt_name, "*?") ?
587e637d177SHe Kuang 	       add_tracepoint_multi_event(list, idx, sys_name, evt_name,
588272ed29aSJiri Olsa 					  err, head_config) :
589e637d177SHe Kuang 	       add_tracepoint(list, idx, sys_name, evt_name,
590272ed29aSJiri Olsa 			      err, head_config);
591f35488f9SJiri Olsa }
592f35488f9SJiri Olsa 
593c5cd8ac0SDavid Ahern static int add_tracepoint_multi_sys(struct list_head *list, int *idx,
5948c619d6aSWang Nan 				    const char *sys_name, const char *evt_name,
595272ed29aSJiri Olsa 				    struct parse_events_error *err,
596e637d177SHe Kuang 				    struct list_head *head_config)
597f35488f9SJiri Olsa {
598f35488f9SJiri Olsa 	struct dirent *events_ent;
599f35488f9SJiri Olsa 	DIR *events_dir;
600f35488f9SJiri Olsa 	int ret = 0;
601f35488f9SJiri Olsa 
6027014e0e3SArnaldo Carvalho de Melo 	events_dir = tracing_events__opendir();
603f35488f9SJiri Olsa 	if (!events_dir) {
604272ed29aSJiri Olsa 		tracepoint_error(err, errno, sys_name, evt_name);
605f35488f9SJiri Olsa 		return -1;
606f35488f9SJiri Olsa 	}
607f35488f9SJiri Olsa 
608f35488f9SJiri Olsa 	while (!ret && (events_ent = readdir(events_dir))) {
609f35488f9SJiri Olsa 		if (!strcmp(events_ent->d_name, ".")
610f35488f9SJiri Olsa 		    || !strcmp(events_ent->d_name, "..")
611f35488f9SJiri Olsa 		    || !strcmp(events_ent->d_name, "enable")
612f35488f9SJiri Olsa 		    || !strcmp(events_ent->d_name, "header_event")
613f35488f9SJiri Olsa 		    || !strcmp(events_ent->d_name, "header_page"))
614f35488f9SJiri Olsa 			continue;
615f35488f9SJiri Olsa 
616f35488f9SJiri Olsa 		if (!strglobmatch(events_ent->d_name, sys_name))
617f35488f9SJiri Olsa 			continue;
618f35488f9SJiri Olsa 
619f35488f9SJiri Olsa 		ret = add_tracepoint_event(list, idx, events_ent->d_name,
620272ed29aSJiri Olsa 					   evt_name, err, head_config);
621f35488f9SJiri Olsa 	}
622f35488f9SJiri Olsa 
623f35488f9SJiri Olsa 	closedir(events_dir);
624f35488f9SJiri Olsa 	return ret;
625f35488f9SJiri Olsa }
626f35488f9SJiri Olsa 
6274edf30e3SWang Nan struct __add_bpf_event_param {
6285d9cdc11SArnaldo Carvalho de Melo 	struct parse_events_state *parse_state;
6294edf30e3SWang Nan 	struct list_head *list;
63095088a59SWang Nan 	struct list_head *head_config;
6314edf30e3SWang Nan };
6324edf30e3SWang Nan 
633af4a0991SArnaldo Carvalho de Melo static int add_bpf_event(const char *group, const char *event, int fd, struct bpf_object *obj,
6344edf30e3SWang Nan 			 void *_param)
6354edf30e3SWang Nan {
6364edf30e3SWang Nan 	LIST_HEAD(new_evsels);
6374edf30e3SWang Nan 	struct __add_bpf_event_param *param = _param;
6385d9cdc11SArnaldo Carvalho de Melo 	struct parse_events_state *parse_state = param->parse_state;
6394edf30e3SWang Nan 	struct list_head *list = param->list;
64032dcd021SJiri Olsa 	struct evsel *pos;
6414edf30e3SWang Nan 	int err;
6422620b7e3SArnaldo Carvalho de Melo 	/*
6432620b7e3SArnaldo Carvalho de Melo 	 * Check if we should add the event, i.e. if it is a TP but starts with a '!',
6442620b7e3SArnaldo Carvalho de Melo 	 * then don't add the tracepoint, this will be used for something else, like
6452620b7e3SArnaldo Carvalho de Melo 	 * adding to a BPF_MAP_TYPE_PROG_ARRAY.
6462620b7e3SArnaldo Carvalho de Melo 	 *
6472620b7e3SArnaldo Carvalho de Melo 	 * See tools/perf/examples/bpf/augmented_raw_syscalls.c
6482620b7e3SArnaldo Carvalho de Melo 	 */
6492620b7e3SArnaldo Carvalho de Melo 	if (group[0] == '!')
6502620b7e3SArnaldo Carvalho de Melo 		return 0;
6514edf30e3SWang Nan 
6524edf30e3SWang Nan 	pr_debug("add bpf event %s:%s and attach bpf program %d\n",
653cd102d70SWang Nan 		 group, event, fd);
6544edf30e3SWang Nan 
6555d369a75SArnaldo Carvalho de Melo 	err = parse_events_add_tracepoint(&new_evsels, &parse_state->idx, group,
6565d369a75SArnaldo Carvalho de Melo 					  event, parse_state->error,
65795088a59SWang Nan 					  param->head_config);
6584edf30e3SWang Nan 	if (err) {
65932dcd021SJiri Olsa 		struct evsel *evsel, *tmp;
6604edf30e3SWang Nan 
6614edf30e3SWang Nan 		pr_debug("Failed to add BPF event %s:%s\n",
662cd102d70SWang Nan 			 group, event);
663*b27c4eceSJiri Olsa 		list_for_each_entry_safe(evsel, tmp, &new_evsels, core.node) {
664*b27c4eceSJiri Olsa 			list_del_init(&evsel->core.node);
6655eb2dd2aSJiri Olsa 			evsel__delete(evsel);
6664edf30e3SWang Nan 		}
6674edf30e3SWang Nan 		return err;
6684edf30e3SWang Nan 	}
669cd102d70SWang Nan 	pr_debug("adding %s:%s\n", group, event);
6704edf30e3SWang Nan 
671*b27c4eceSJiri Olsa 	list_for_each_entry(pos, &new_evsels, core.node) {
6721f45b1d4SWang Nan 		pr_debug("adding %s:%s to %p\n",
673cd102d70SWang Nan 			 group, event, pos);
6741f45b1d4SWang Nan 		pos->bpf_fd = fd;
675af4a0991SArnaldo Carvalho de Melo 		pos->bpf_obj = obj;
6761f45b1d4SWang Nan 	}
6774edf30e3SWang Nan 	list_splice(&new_evsels, list);
6784edf30e3SWang Nan 	return 0;
6794edf30e3SWang Nan }
6804edf30e3SWang Nan 
6815d9cdc11SArnaldo Carvalho de Melo int parse_events_load_bpf_obj(struct parse_events_state *parse_state,
68284c86ca1SWang Nan 			      struct list_head *list,
68395088a59SWang Nan 			      struct bpf_object *obj,
68495088a59SWang Nan 			      struct list_head *head_config)
68584c86ca1SWang Nan {
68684c86ca1SWang Nan 	int err;
68784c86ca1SWang Nan 	char errbuf[BUFSIZ];
6885d9cdc11SArnaldo Carvalho de Melo 	struct __add_bpf_event_param param = {parse_state, list, head_config};
689aa3abf30SWang Nan 	static bool registered_unprobe_atexit = false;
69084c86ca1SWang Nan 
69184c86ca1SWang Nan 	if (IS_ERR(obj) || !obj) {
69284c86ca1SWang Nan 		snprintf(errbuf, sizeof(errbuf),
69384c86ca1SWang Nan 			 "Internal error: load bpf obj with NULL");
69484c86ca1SWang Nan 		err = -EINVAL;
69584c86ca1SWang Nan 		goto errout;
69684c86ca1SWang Nan 	}
69784c86ca1SWang Nan 
69884c86ca1SWang Nan 	/*
699aa3abf30SWang Nan 	 * Register atexit handler before calling bpf__probe() so
700aa3abf30SWang Nan 	 * bpf__probe() don't need to unprobe probe points its already
701aa3abf30SWang Nan 	 * created when failure.
702aa3abf30SWang Nan 	 */
703aa3abf30SWang Nan 	if (!registered_unprobe_atexit) {
704aa3abf30SWang Nan 		atexit(bpf__clear);
705aa3abf30SWang Nan 		registered_unprobe_atexit = true;
706aa3abf30SWang Nan 	}
707aa3abf30SWang Nan 
708aa3abf30SWang Nan 	err = bpf__probe(obj);
709aa3abf30SWang Nan 	if (err) {
710aa3abf30SWang Nan 		bpf__strerror_probe(obj, err, errbuf, sizeof(errbuf));
711aa3abf30SWang Nan 		goto errout;
712aa3abf30SWang Nan 	}
713aa3abf30SWang Nan 
7141e5e3ee8SWang Nan 	err = bpf__load(obj);
7151e5e3ee8SWang Nan 	if (err) {
7161e5e3ee8SWang Nan 		bpf__strerror_load(obj, err, errbuf, sizeof(errbuf));
7171e5e3ee8SWang Nan 		goto errout;
7181e5e3ee8SWang Nan 	}
7191e5e3ee8SWang Nan 
720cd102d70SWang Nan 	err = bpf__foreach_event(obj, add_bpf_event, &param);
7214edf30e3SWang Nan 	if (err) {
7224edf30e3SWang Nan 		snprintf(errbuf, sizeof(errbuf),
7234edf30e3SWang Nan 			 "Attach events in BPF object failed");
7244edf30e3SWang Nan 		goto errout;
7254edf30e3SWang Nan 	}
7264edf30e3SWang Nan 
7274edf30e3SWang Nan 	return 0;
72884c86ca1SWang Nan errout:
7295d9cdc11SArnaldo Carvalho de Melo 	parse_state->error->help = strdup("(add -v to see detail)");
7305d9cdc11SArnaldo Carvalho de Melo 	parse_state->error->str = strdup(errbuf);
73184c86ca1SWang Nan 	return err;
73284c86ca1SWang Nan }
73384c86ca1SWang Nan 
734a34f3be7SWang Nan static int
7355d9cdc11SArnaldo Carvalho de Melo parse_events_config_bpf(struct parse_events_state *parse_state,
736a34f3be7SWang Nan 			struct bpf_object *obj,
737a34f3be7SWang Nan 			struct list_head *head_config)
738a34f3be7SWang Nan {
739a34f3be7SWang Nan 	struct parse_events_term *term;
740a34f3be7SWang Nan 	int error_pos;
741a34f3be7SWang Nan 
742a34f3be7SWang Nan 	if (!head_config || list_empty(head_config))
743a34f3be7SWang Nan 		return 0;
744a34f3be7SWang Nan 
745a34f3be7SWang Nan 	list_for_each_entry(term, head_config, list) {
746a34f3be7SWang Nan 		char errbuf[BUFSIZ];
747a34f3be7SWang Nan 		int err;
748a34f3be7SWang Nan 
749a34f3be7SWang Nan 		if (term->type_term != PARSE_EVENTS__TERM_TYPE_USER) {
750a34f3be7SWang Nan 			snprintf(errbuf, sizeof(errbuf),
751a34f3be7SWang Nan 				 "Invalid config term for BPF object");
752a34f3be7SWang Nan 			errbuf[BUFSIZ - 1] = '\0';
753a34f3be7SWang Nan 
7545d9cdc11SArnaldo Carvalho de Melo 			parse_state->error->idx = term->err_term;
7555d9cdc11SArnaldo Carvalho de Melo 			parse_state->error->str = strdup(errbuf);
756a34f3be7SWang Nan 			return -EINVAL;
757a34f3be7SWang Nan 		}
758a34f3be7SWang Nan 
7595d9cdc11SArnaldo Carvalho de Melo 		err = bpf__config_obj(obj, term, parse_state->evlist, &error_pos);
760a34f3be7SWang Nan 		if (err) {
7615d9cdc11SArnaldo Carvalho de Melo 			bpf__strerror_config_obj(obj, term, parse_state->evlist,
762a34f3be7SWang Nan 						 &error_pos, err, errbuf,
763a34f3be7SWang Nan 						 sizeof(errbuf));
7645d9cdc11SArnaldo Carvalho de Melo 			parse_state->error->help = strdup(
7657630b3e2SWang Nan "Hint:\tValid config terms:\n"
766e571e029SWang Nan "     \tmap:[<arraymap>].value<indices>=[value]\n"
767e571e029SWang Nan "     \tmap:[<eventmap>].event<indices>=[event]\n"
7687630b3e2SWang Nan "\n"
769e571e029SWang Nan "     \twhere <indices> is something like [0,3...5] or [all]\n"
770a34f3be7SWang Nan "     \t(add -v to see detail)");
7715d9cdc11SArnaldo Carvalho de Melo 			parse_state->error->str = strdup(errbuf);
772a34f3be7SWang Nan 			if (err == -BPF_LOADER_ERRNO__OBJCONF_MAP_VALUE)
7735d9cdc11SArnaldo Carvalho de Melo 				parse_state->error->idx = term->err_val;
774a34f3be7SWang Nan 			else
7755d9cdc11SArnaldo Carvalho de Melo 				parse_state->error->idx = term->err_term + error_pos;
776a34f3be7SWang Nan 			return err;
777a34f3be7SWang Nan 		}
778a34f3be7SWang Nan 	}
779a34f3be7SWang Nan 	return 0;
780a34f3be7SWang Nan }
781a34f3be7SWang Nan 
78295088a59SWang Nan /*
78395088a59SWang Nan  * Split config terms:
78495088a59SWang Nan  * perf record -e bpf.c/call-graph=fp,map:array.value[0]=1/ ...
78595088a59SWang Nan  *  'call-graph=fp' is 'evt config', should be applied to each
78695088a59SWang Nan  *  events in bpf.c.
78795088a59SWang Nan  * 'map:array.value[0]=1' is 'obj config', should be processed
78895088a59SWang Nan  * with parse_events_config_bpf.
78995088a59SWang Nan  *
79095088a59SWang Nan  * Move object config terms from the first list to obj_head_config.
79195088a59SWang Nan  */
79295088a59SWang Nan static void
79395088a59SWang Nan split_bpf_config_terms(struct list_head *evt_head_config,
79495088a59SWang Nan 		       struct list_head *obj_head_config)
79595088a59SWang Nan {
79695088a59SWang Nan 	struct parse_events_term *term, *temp;
79795088a59SWang Nan 
79895088a59SWang Nan 	/*
79995088a59SWang Nan 	 * Currectly, all possible user config term
80095088a59SWang Nan 	 * belong to bpf object. parse_events__is_hardcoded_term()
80195088a59SWang Nan 	 * happends to be a good flag.
80295088a59SWang Nan 	 *
80395088a59SWang Nan 	 * See parse_events_config_bpf() and
80495088a59SWang Nan 	 * config_term_tracepoint().
80595088a59SWang Nan 	 */
80695088a59SWang Nan 	list_for_each_entry_safe(term, temp, evt_head_config, list)
80795088a59SWang Nan 		if (!parse_events__is_hardcoded_term(term))
80895088a59SWang Nan 			list_move_tail(&term->list, obj_head_config);
80995088a59SWang Nan }
81095088a59SWang Nan 
8115d9cdc11SArnaldo Carvalho de Melo int parse_events_load_bpf(struct parse_events_state *parse_state,
81284c86ca1SWang Nan 			  struct list_head *list,
813d509db04SWang Nan 			  char *bpf_file_name,
814a34f3be7SWang Nan 			  bool source,
815a34f3be7SWang Nan 			  struct list_head *head_config)
81684c86ca1SWang Nan {
817a34f3be7SWang Nan 	int err;
81895088a59SWang Nan 	struct bpf_object *obj;
81995088a59SWang Nan 	LIST_HEAD(obj_head_config);
82095088a59SWang Nan 
82195088a59SWang Nan 	if (head_config)
82295088a59SWang Nan 		split_bpf_config_terms(head_config, &obj_head_config);
82384c86ca1SWang Nan 
824d509db04SWang Nan 	obj = bpf__prepare_load(bpf_file_name, source);
8256371ca3bSWang Nan 	if (IS_ERR(obj)) {
82684c86ca1SWang Nan 		char errbuf[BUFSIZ];
82784c86ca1SWang Nan 
8286371ca3bSWang Nan 		err = PTR_ERR(obj);
82984c86ca1SWang Nan 
83084c86ca1SWang Nan 		if (err == -ENOTSUP)
83184c86ca1SWang Nan 			snprintf(errbuf, sizeof(errbuf),
83284c86ca1SWang Nan 				 "BPF support is not compiled");
83384c86ca1SWang Nan 		else
834d3e0ce39SWang Nan 			bpf__strerror_prepare_load(bpf_file_name,
835d3e0ce39SWang Nan 						   source,
836d3e0ce39SWang Nan 						   -err, errbuf,
837d3e0ce39SWang Nan 						   sizeof(errbuf));
83884c86ca1SWang Nan 
8395d9cdc11SArnaldo Carvalho de Melo 		parse_state->error->help = strdup("(add -v to see detail)");
8405d9cdc11SArnaldo Carvalho de Melo 		parse_state->error->str = strdup(errbuf);
84184c86ca1SWang Nan 		return err;
84284c86ca1SWang Nan 	}
84384c86ca1SWang Nan 
8445d9cdc11SArnaldo Carvalho de Melo 	err = parse_events_load_bpf_obj(parse_state, list, obj, head_config);
845a34f3be7SWang Nan 	if (err)
846a34f3be7SWang Nan 		return err;
8475d9cdc11SArnaldo Carvalho de Melo 	err = parse_events_config_bpf(parse_state, obj, &obj_head_config);
84895088a59SWang Nan 
84995088a59SWang Nan 	/*
85095088a59SWang Nan 	 * Caller doesn't know anything about obj_head_config,
85195088a59SWang Nan 	 * so combine them together again before returnning.
85295088a59SWang Nan 	 */
85395088a59SWang Nan 	if (head_config)
85495088a59SWang Nan 		list_splice_tail(&obj_head_config, head_config);
85595088a59SWang Nan 	return err;
85684c86ca1SWang Nan }
85784c86ca1SWang Nan 
85889812fc8SJiri Olsa static int
85989812fc8SJiri Olsa parse_breakpoint_type(const char *type, struct perf_event_attr *attr)
8601b290d67SFrederic Weisbecker {
8611b290d67SFrederic Weisbecker 	int i;
8621b290d67SFrederic Weisbecker 
8631b290d67SFrederic Weisbecker 	for (i = 0; i < 3; i++) {
86489812fc8SJiri Olsa 		if (!type || !type[i])
8651b290d67SFrederic Weisbecker 			break;
8661b290d67SFrederic Weisbecker 
8677582732fSJiri Olsa #define CHECK_SET_TYPE(bit)		\
8687582732fSJiri Olsa do {					\
8697582732fSJiri Olsa 	if (attr->bp_type & bit)	\
8707582732fSJiri Olsa 		return -EINVAL;		\
8717582732fSJiri Olsa 	else				\
8727582732fSJiri Olsa 		attr->bp_type |= bit;	\
8737582732fSJiri Olsa } while (0)
8747582732fSJiri Olsa 
8751b290d67SFrederic Weisbecker 		switch (type[i]) {
8761b290d67SFrederic Weisbecker 		case 'r':
8777582732fSJiri Olsa 			CHECK_SET_TYPE(HW_BREAKPOINT_R);
8781b290d67SFrederic Weisbecker 			break;
8791b290d67SFrederic Weisbecker 		case 'w':
8807582732fSJiri Olsa 			CHECK_SET_TYPE(HW_BREAKPOINT_W);
8811b290d67SFrederic Weisbecker 			break;
8821b290d67SFrederic Weisbecker 		case 'x':
8837582732fSJiri Olsa 			CHECK_SET_TYPE(HW_BREAKPOINT_X);
8841b290d67SFrederic Weisbecker 			break;
8851b290d67SFrederic Weisbecker 		default:
88689812fc8SJiri Olsa 			return -EINVAL;
8871b290d67SFrederic Weisbecker 		}
8881b290d67SFrederic Weisbecker 	}
88989812fc8SJiri Olsa 
8907582732fSJiri Olsa #undef CHECK_SET_TYPE
8917582732fSJiri Olsa 
8921b290d67SFrederic Weisbecker 	if (!attr->bp_type) /* Default */
8931b290d67SFrederic Weisbecker 		attr->bp_type = HW_BREAKPOINT_R | HW_BREAKPOINT_W;
8941b290d67SFrederic Weisbecker 
89589812fc8SJiri Olsa 	return 0;
8961b290d67SFrederic Weisbecker }
8971b290d67SFrederic Weisbecker 
898c5cd8ac0SDavid Ahern int parse_events_add_breakpoint(struct list_head *list, int *idx,
8993741eb9fSJacob Shin 				void *ptr, char *type, u64 len)
9001b290d67SFrederic Weisbecker {
90189812fc8SJiri Olsa 	struct perf_event_attr attr;
9021b290d67SFrederic Weisbecker 
90389812fc8SJiri Olsa 	memset(&attr, 0, sizeof(attr));
9049fafd98fSJiri Olsa 	attr.bp_addr = (unsigned long) ptr;
9051b290d67SFrederic Weisbecker 
90689812fc8SJiri Olsa 	if (parse_breakpoint_type(type, &attr))
90789812fc8SJiri Olsa 		return -EINVAL;
9081b290d67SFrederic Weisbecker 
9093741eb9fSJacob Shin 	/* Provide some defaults if len is not specified */
9103741eb9fSJacob Shin 	if (!len) {
91189812fc8SJiri Olsa 		if (attr.bp_type == HW_BREAKPOINT_X)
9123741eb9fSJacob Shin 			len = sizeof(long);
913aa59a485SFrederic Weisbecker 		else
9143741eb9fSJacob Shin 			len = HW_BREAKPOINT_LEN_4;
9153741eb9fSJacob Shin 	}
9163741eb9fSJacob Shin 
9173741eb9fSJacob Shin 	attr.bp_len = len;
918aa59a485SFrederic Weisbecker 
91989812fc8SJiri Olsa 	attr.type = PERF_TYPE_BREAKPOINT;
9204a841d65SJovi Zhang 	attr.sample_period = 1;
9211b290d67SFrederic Weisbecker 
922930a2e29SJiri Olsa 	return add_event(list, idx, &attr, NULL, NULL);
9231b290d67SFrederic Weisbecker }
9241b290d67SFrederic Weisbecker 
9253b0e371cSJiri Olsa static int check_type_val(struct parse_events_term *term,
9263b0e371cSJiri Olsa 			  struct parse_events_error *err,
9273b0e371cSJiri Olsa 			  int type)
9283b0e371cSJiri Olsa {
9293b0e371cSJiri Olsa 	if (type == term->type_val)
9303b0e371cSJiri Olsa 		return 0;
9313b0e371cSJiri Olsa 
9323b0e371cSJiri Olsa 	if (err) {
9333b0e371cSJiri Olsa 		err->idx = term->err_val;
9343b0e371cSJiri Olsa 		if (type == PARSE_EVENTS__TERM_TYPE_NUM)
9353b0e371cSJiri Olsa 			err->str = strdup("expected numeric value");
9363b0e371cSJiri Olsa 		else
9373b0e371cSJiri Olsa 			err->str = strdup("expected string value");
9383b0e371cSJiri Olsa 	}
9393b0e371cSJiri Olsa 	return -EINVAL;
9403b0e371cSJiri Olsa }
9413b0e371cSJiri Olsa 
94217cb5f84SWang Nan /*
94317cb5f84SWang Nan  * Update according to parse-events.l
94417cb5f84SWang Nan  */
94517cb5f84SWang Nan static const char *config_term_names[__PARSE_EVENTS__TERM_TYPE_NR] = {
94617cb5f84SWang Nan 	[PARSE_EVENTS__TERM_TYPE_USER]			= "<sysfs term>",
94717cb5f84SWang Nan 	[PARSE_EVENTS__TERM_TYPE_CONFIG]		= "config",
94817cb5f84SWang Nan 	[PARSE_EVENTS__TERM_TYPE_CONFIG1]		= "config1",
94917cb5f84SWang Nan 	[PARSE_EVENTS__TERM_TYPE_CONFIG2]		= "config2",
95017cb5f84SWang Nan 	[PARSE_EVENTS__TERM_TYPE_NAME]			= "name",
95117cb5f84SWang Nan 	[PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD]		= "period",
95217cb5f84SWang Nan 	[PARSE_EVENTS__TERM_TYPE_SAMPLE_FREQ]		= "freq",
95317cb5f84SWang Nan 	[PARSE_EVENTS__TERM_TYPE_BRANCH_SAMPLE_TYPE]	= "branch_type",
95417cb5f84SWang Nan 	[PARSE_EVENTS__TERM_TYPE_TIME]			= "time",
95517cb5f84SWang Nan 	[PARSE_EVENTS__TERM_TYPE_CALLGRAPH]		= "call-graph",
95617cb5f84SWang Nan 	[PARSE_EVENTS__TERM_TYPE_STACKSIZE]		= "stack-size",
95717cb5f84SWang Nan 	[PARSE_EVENTS__TERM_TYPE_NOINHERIT]		= "no-inherit",
95817cb5f84SWang Nan 	[PARSE_EVENTS__TERM_TYPE_INHERIT]		= "inherit",
959792d48b4SArnaldo Carvalho de Melo 	[PARSE_EVENTS__TERM_TYPE_MAX_STACK]		= "max-stack",
9602fda5adaSArnaldo Carvalho de Melo 	[PARSE_EVENTS__TERM_TYPE_MAX_EVENTS]		= "nr",
961626a6b78SWang Nan 	[PARSE_EVENTS__TERM_TYPE_OVERWRITE]		= "overwrite",
962626a6b78SWang Nan 	[PARSE_EVENTS__TERM_TYPE_NOOVERWRITE]		= "no-overwrite",
963dd60fba7SMathieu Poirier 	[PARSE_EVENTS__TERM_TYPE_DRV_CFG]		= "driver-config",
964064b4e82SJin Yao 	[PARSE_EVENTS__TERM_TYPE_PERCORE]		= "percore",
96517cb5f84SWang Nan };
96617cb5f84SWang Nan 
9671669e509SWang Nan static bool config_term_shrinked;
9681669e509SWang Nan 
9691669e509SWang Nan static bool
9701669e509SWang Nan config_term_avail(int term_type, struct parse_events_error *err)
9711669e509SWang Nan {
9721669e509SWang Nan 	if (term_type < 0 || term_type >= __PARSE_EVENTS__TERM_TYPE_NR) {
9731669e509SWang Nan 		err->str = strdup("Invalid term_type");
9741669e509SWang Nan 		return false;
9751669e509SWang Nan 	}
9761669e509SWang Nan 	if (!config_term_shrinked)
9771669e509SWang Nan 		return true;
9781669e509SWang Nan 
9791669e509SWang Nan 	switch (term_type) {
9801669e509SWang Nan 	case PARSE_EVENTS__TERM_TYPE_CONFIG:
9811669e509SWang Nan 	case PARSE_EVENTS__TERM_TYPE_CONFIG1:
9821669e509SWang Nan 	case PARSE_EVENTS__TERM_TYPE_CONFIG2:
9831669e509SWang Nan 	case PARSE_EVENTS__TERM_TYPE_NAME:
98406835545SSukadev Bhattiprolu 	case PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD:
985064b4e82SJin Yao 	case PARSE_EVENTS__TERM_TYPE_PERCORE:
9861669e509SWang Nan 		return true;
9871669e509SWang Nan 	default:
9881669e509SWang Nan 		if (!err)
9891669e509SWang Nan 			return false;
9901669e509SWang Nan 
9911669e509SWang Nan 		/* term_type is validated so indexing is safe */
9921669e509SWang Nan 		if (asprintf(&err->str, "'%s' is not usable in 'perf stat'",
9931669e509SWang Nan 			     config_term_names[term_type]) < 0)
9941669e509SWang Nan 			err->str = NULL;
9951669e509SWang Nan 		return false;
9961669e509SWang Nan 	}
9971669e509SWang Nan }
9981669e509SWang Nan 
9991669e509SWang Nan void parse_events__shrink_config_terms(void)
10001669e509SWang Nan {
10011669e509SWang Nan 	config_term_shrinked = true;
10021669e509SWang Nan }
10031669e509SWang Nan 
10040b8891a8SHe Kuang static int config_term_common(struct perf_event_attr *attr,
10053b0e371cSJiri Olsa 			      struct parse_events_term *term,
10063b0e371cSJiri Olsa 			      struct parse_events_error *err)
10078f707d84SJiri Olsa {
100816fa7e82SJiri Olsa #define CHECK_TYPE_VAL(type)						   \
100916fa7e82SJiri Olsa do {									   \
10103b0e371cSJiri Olsa 	if (check_type_val(term, err, PARSE_EVENTS__TERM_TYPE_ ## type)) \
101116fa7e82SJiri Olsa 		return -EINVAL;						   \
101216fa7e82SJiri Olsa } while (0)
101316fa7e82SJiri Olsa 
101416fa7e82SJiri Olsa 	switch (term->type_term) {
10158f707d84SJiri Olsa 	case PARSE_EVENTS__TERM_TYPE_CONFIG:
101616fa7e82SJiri Olsa 		CHECK_TYPE_VAL(NUM);
10178f707d84SJiri Olsa 		attr->config = term->val.num;
10188f707d84SJiri Olsa 		break;
10198f707d84SJiri Olsa 	case PARSE_EVENTS__TERM_TYPE_CONFIG1:
102016fa7e82SJiri Olsa 		CHECK_TYPE_VAL(NUM);
10218f707d84SJiri Olsa 		attr->config1 = term->val.num;
10228f707d84SJiri Olsa 		break;
10238f707d84SJiri Olsa 	case PARSE_EVENTS__TERM_TYPE_CONFIG2:
102416fa7e82SJiri Olsa 		CHECK_TYPE_VAL(NUM);
10258f707d84SJiri Olsa 		attr->config2 = term->val.num;
10268f707d84SJiri Olsa 		break;
10278f707d84SJiri Olsa 	case PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD:
102816fa7e82SJiri Olsa 		CHECK_TYPE_VAL(NUM);
10298f707d84SJiri Olsa 		break;
103009af2a55SNamhyung Kim 	case PARSE_EVENTS__TERM_TYPE_SAMPLE_FREQ:
103109af2a55SNamhyung Kim 		CHECK_TYPE_VAL(NUM);
103209af2a55SNamhyung Kim 		break;
10338f707d84SJiri Olsa 	case PARSE_EVENTS__TERM_TYPE_BRANCH_SAMPLE_TYPE:
1034ac12f676SAndi Kleen 		CHECK_TYPE_VAL(STR);
1035ac12f676SAndi Kleen 		if (strcmp(term->val.str, "no") &&
1036ac12f676SAndi Kleen 		    parse_branch_str(term->val.str, &attr->branch_sample_type)) {
1037ac12f676SAndi Kleen 			err->str = strdup("invalid branch sample type");
1038ac12f676SAndi Kleen 			err->idx = term->err_val;
1039ac12f676SAndi Kleen 			return -EINVAL;
1040ac12f676SAndi Kleen 		}
10418f707d84SJiri Olsa 		break;
104232067712SKan Liang 	case PARSE_EVENTS__TERM_TYPE_TIME:
104332067712SKan Liang 		CHECK_TYPE_VAL(NUM);
104432067712SKan Liang 		if (term->val.num > 1) {
104532067712SKan Liang 			err->str = strdup("expected 0 or 1");
104632067712SKan Liang 			err->idx = term->err_val;
104732067712SKan Liang 			return -EINVAL;
104832067712SKan Liang 		}
104932067712SKan Liang 		break;
1050d457c963SKan Liang 	case PARSE_EVENTS__TERM_TYPE_CALLGRAPH:
1051d457c963SKan Liang 		CHECK_TYPE_VAL(STR);
1052d457c963SKan Liang 		break;
1053d457c963SKan Liang 	case PARSE_EVENTS__TERM_TYPE_STACKSIZE:
1054d457c963SKan Liang 		CHECK_TYPE_VAL(NUM);
1055d457c963SKan Liang 		break;
1056374ce938SWang Nan 	case PARSE_EVENTS__TERM_TYPE_INHERIT:
1057374ce938SWang Nan 		CHECK_TYPE_VAL(NUM);
1058374ce938SWang Nan 		break;
1059374ce938SWang Nan 	case PARSE_EVENTS__TERM_TYPE_NOINHERIT:
1060374ce938SWang Nan 		CHECK_TYPE_VAL(NUM);
1061374ce938SWang Nan 		break;
1062626a6b78SWang Nan 	case PARSE_EVENTS__TERM_TYPE_OVERWRITE:
1063626a6b78SWang Nan 		CHECK_TYPE_VAL(NUM);
1064626a6b78SWang Nan 		break;
1065626a6b78SWang Nan 	case PARSE_EVENTS__TERM_TYPE_NOOVERWRITE:
1066626a6b78SWang Nan 		CHECK_TYPE_VAL(NUM);
1067626a6b78SWang Nan 		break;
10686b5fc39bSJiri Olsa 	case PARSE_EVENTS__TERM_TYPE_NAME:
10696b5fc39bSJiri Olsa 		CHECK_TYPE_VAL(STR);
10706b5fc39bSJiri Olsa 		break;
1071792d48b4SArnaldo Carvalho de Melo 	case PARSE_EVENTS__TERM_TYPE_MAX_STACK:
1072792d48b4SArnaldo Carvalho de Melo 		CHECK_TYPE_VAL(NUM);
1073792d48b4SArnaldo Carvalho de Melo 		break;
10742fda5adaSArnaldo Carvalho de Melo 	case PARSE_EVENTS__TERM_TYPE_MAX_EVENTS:
10752fda5adaSArnaldo Carvalho de Melo 		CHECK_TYPE_VAL(NUM);
10762fda5adaSArnaldo Carvalho de Melo 		break;
1077064b4e82SJin Yao 	case PARSE_EVENTS__TERM_TYPE_PERCORE:
1078064b4e82SJin Yao 		CHECK_TYPE_VAL(NUM);
1079064b4e82SJin Yao 		if ((unsigned int)term->val.num > 1) {
1080064b4e82SJin Yao 			err->str = strdup("expected 0 or 1");
1081064b4e82SJin Yao 			err->idx = term->err_val;
1082064b4e82SJin Yao 			return -EINVAL;
1083064b4e82SJin Yao 		}
1084064b4e82SJin Yao 		break;
10858f707d84SJiri Olsa 	default:
1086ffeb883eSHe Kuang 		err->str = strdup("unknown term");
1087ffeb883eSHe Kuang 		err->idx = term->err_term;
1088ffeb883eSHe Kuang 		err->help = parse_events_formats_error_string(NULL);
10898f707d84SJiri Olsa 		return -EINVAL;
10908f707d84SJiri Olsa 	}
109116fa7e82SJiri Olsa 
10921669e509SWang Nan 	/*
10931669e509SWang Nan 	 * Check term availbility after basic checking so
10941669e509SWang Nan 	 * PARSE_EVENTS__TERM_TYPE_USER can be found and filtered.
10951669e509SWang Nan 	 *
10961669e509SWang Nan 	 * If check availbility at the entry of this function,
10971669e509SWang Nan 	 * user will see "'<sysfs term>' is not usable in 'perf stat'"
10981669e509SWang Nan 	 * if an invalid config term is provided for legacy events
10991669e509SWang Nan 	 * (for example, instructions/badterm/...), which is confusing.
11001669e509SWang Nan 	 */
11011669e509SWang Nan 	if (!config_term_avail(term->type_term, err))
11021669e509SWang Nan 		return -EINVAL;
11038f707d84SJiri Olsa 	return 0;
110416fa7e82SJiri Olsa #undef CHECK_TYPE_VAL
11058f707d84SJiri Olsa }
11068f707d84SJiri Olsa 
11070b8891a8SHe Kuang static int config_term_pmu(struct perf_event_attr *attr,
11080b8891a8SHe Kuang 			   struct parse_events_term *term,
11090b8891a8SHe Kuang 			   struct parse_events_error *err)
11100b8891a8SHe Kuang {
1111dd60fba7SMathieu Poirier 	if (term->type_term == PARSE_EVENTS__TERM_TYPE_USER ||
1112dd60fba7SMathieu Poirier 	    term->type_term == PARSE_EVENTS__TERM_TYPE_DRV_CFG)
11130b8891a8SHe Kuang 		/*
11140b8891a8SHe Kuang 		 * Always succeed for sysfs terms, as we dont know
11150b8891a8SHe Kuang 		 * at this point what type they need to have.
11160b8891a8SHe Kuang 		 */
11170b8891a8SHe Kuang 		return 0;
11180b8891a8SHe Kuang 	else
11190b8891a8SHe Kuang 		return config_term_common(attr, term, err);
11200b8891a8SHe Kuang }
11210b8891a8SHe Kuang 
1122e637d177SHe Kuang static int config_term_tracepoint(struct perf_event_attr *attr,
1123e637d177SHe Kuang 				  struct parse_events_term *term,
1124e637d177SHe Kuang 				  struct parse_events_error *err)
1125e637d177SHe Kuang {
1126e637d177SHe Kuang 	switch (term->type_term) {
1127e637d177SHe Kuang 	case PARSE_EVENTS__TERM_TYPE_CALLGRAPH:
1128e637d177SHe Kuang 	case PARSE_EVENTS__TERM_TYPE_STACKSIZE:
1129374ce938SWang Nan 	case PARSE_EVENTS__TERM_TYPE_INHERIT:
1130374ce938SWang Nan 	case PARSE_EVENTS__TERM_TYPE_NOINHERIT:
1131792d48b4SArnaldo Carvalho de Melo 	case PARSE_EVENTS__TERM_TYPE_MAX_STACK:
11322fda5adaSArnaldo Carvalho de Melo 	case PARSE_EVENTS__TERM_TYPE_MAX_EVENTS:
1133626a6b78SWang Nan 	case PARSE_EVENTS__TERM_TYPE_OVERWRITE:
1134626a6b78SWang Nan 	case PARSE_EVENTS__TERM_TYPE_NOOVERWRITE:
1135e637d177SHe Kuang 		return config_term_common(attr, term, err);
1136e637d177SHe Kuang 	default:
1137e637d177SHe Kuang 		if (err) {
1138e637d177SHe Kuang 			err->idx = term->err_term;
1139e637d177SHe Kuang 			err->str = strdup("unknown term");
1140e637d177SHe Kuang 			err->help = strdup("valid terms: call-graph,stack-size\n");
1141e637d177SHe Kuang 		}
1142e637d177SHe Kuang 		return -EINVAL;
1143e637d177SHe Kuang 	}
1144e637d177SHe Kuang 
1145e637d177SHe Kuang 	return 0;
1146e637d177SHe Kuang }
1147e637d177SHe Kuang 
11488f707d84SJiri Olsa static int config_attr(struct perf_event_attr *attr,
11493b0e371cSJiri Olsa 		       struct list_head *head,
11500b8891a8SHe Kuang 		       struct parse_events_error *err,
11510b8891a8SHe Kuang 		       config_term_func_t config_term)
11528f707d84SJiri Olsa {
11536cee6cd3SArnaldo Carvalho de Melo 	struct parse_events_term *term;
11548f707d84SJiri Olsa 
11558f707d84SJiri Olsa 	list_for_each_entry(term, head, list)
11563b0e371cSJiri Olsa 		if (config_term(attr, term, err))
11578f707d84SJiri Olsa 			return -EINVAL;
11588f707d84SJiri Olsa 
11598f707d84SJiri Olsa 	return 0;
11608f707d84SJiri Olsa }
11618f707d84SJiri Olsa 
1162930a2e29SJiri Olsa static int get_config_terms(struct list_head *head_config,
1163930a2e29SJiri Olsa 			    struct list_head *head_terms __maybe_unused)
1164930a2e29SJiri Olsa {
1165930a2e29SJiri Olsa #define ADD_CONFIG_TERM(__type, __name, __val)			\
1166930a2e29SJiri Olsa do {								\
1167930a2e29SJiri Olsa 	struct perf_evsel_config_term *__t;			\
1168930a2e29SJiri Olsa 								\
1169930a2e29SJiri Olsa 	__t = zalloc(sizeof(*__t));				\
1170930a2e29SJiri Olsa 	if (!__t)						\
1171930a2e29SJiri Olsa 		return -ENOMEM;					\
1172930a2e29SJiri Olsa 								\
1173930a2e29SJiri Olsa 	INIT_LIST_HEAD(&__t->list);				\
1174930a2e29SJiri Olsa 	__t->type       = PERF_EVSEL__CONFIG_TERM_ ## __type;	\
1175930a2e29SJiri Olsa 	__t->val.__name = __val;				\
1176c2f1ceadSAndi Kleen 	__t->weak	= term->weak;				\
1177930a2e29SJiri Olsa 	list_add_tail(&__t->list, head_terms);			\
1178930a2e29SJiri Olsa } while (0)
1179930a2e29SJiri Olsa 
1180930a2e29SJiri Olsa 	struct parse_events_term *term;
1181930a2e29SJiri Olsa 
1182930a2e29SJiri Olsa 	list_for_each_entry(term, head_config, list) {
1183930a2e29SJiri Olsa 		switch (term->type_term) {
1184ee4c7588SJiri Olsa 		case PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD:
1185ee4c7588SJiri Olsa 			ADD_CONFIG_TERM(PERIOD, period, term->val.num);
118632067712SKan Liang 			break;
118709af2a55SNamhyung Kim 		case PARSE_EVENTS__TERM_TYPE_SAMPLE_FREQ:
118809af2a55SNamhyung Kim 			ADD_CONFIG_TERM(FREQ, freq, term->val.num);
118909af2a55SNamhyung Kim 			break;
119032067712SKan Liang 		case PARSE_EVENTS__TERM_TYPE_TIME:
119132067712SKan Liang 			ADD_CONFIG_TERM(TIME, time, term->val.num);
119232067712SKan Liang 			break;
1193d457c963SKan Liang 		case PARSE_EVENTS__TERM_TYPE_CALLGRAPH:
1194d457c963SKan Liang 			ADD_CONFIG_TERM(CALLGRAPH, callgraph, term->val.str);
1195d457c963SKan Liang 			break;
1196ac12f676SAndi Kleen 		case PARSE_EVENTS__TERM_TYPE_BRANCH_SAMPLE_TYPE:
1197ac12f676SAndi Kleen 			ADD_CONFIG_TERM(BRANCH, branch, term->val.str);
1198ac12f676SAndi Kleen 			break;
1199d457c963SKan Liang 		case PARSE_EVENTS__TERM_TYPE_STACKSIZE:
1200d457c963SKan Liang 			ADD_CONFIG_TERM(STACK_USER, stack_user, term->val.num);
1201d457c963SKan Liang 			break;
1202374ce938SWang Nan 		case PARSE_EVENTS__TERM_TYPE_INHERIT:
1203374ce938SWang Nan 			ADD_CONFIG_TERM(INHERIT, inherit, term->val.num ? 1 : 0);
1204374ce938SWang Nan 			break;
1205374ce938SWang Nan 		case PARSE_EVENTS__TERM_TYPE_NOINHERIT:
1206374ce938SWang Nan 			ADD_CONFIG_TERM(INHERIT, inherit, term->val.num ? 0 : 1);
1207374ce938SWang Nan 			break;
1208792d48b4SArnaldo Carvalho de Melo 		case PARSE_EVENTS__TERM_TYPE_MAX_STACK:
1209792d48b4SArnaldo Carvalho de Melo 			ADD_CONFIG_TERM(MAX_STACK, max_stack, term->val.num);
1210792d48b4SArnaldo Carvalho de Melo 			break;
12112fda5adaSArnaldo Carvalho de Melo 		case PARSE_EVENTS__TERM_TYPE_MAX_EVENTS:
12122fda5adaSArnaldo Carvalho de Melo 			ADD_CONFIG_TERM(MAX_EVENTS, max_events, term->val.num);
12132fda5adaSArnaldo Carvalho de Melo 			break;
1214626a6b78SWang Nan 		case PARSE_EVENTS__TERM_TYPE_OVERWRITE:
1215626a6b78SWang Nan 			ADD_CONFIG_TERM(OVERWRITE, overwrite, term->val.num ? 1 : 0);
1216626a6b78SWang Nan 			break;
1217626a6b78SWang Nan 		case PARSE_EVENTS__TERM_TYPE_NOOVERWRITE:
1218626a6b78SWang Nan 			ADD_CONFIG_TERM(OVERWRITE, overwrite, term->val.num ? 0 : 1);
1219626a6b78SWang Nan 			break;
1220dd60fba7SMathieu Poirier 		case PARSE_EVENTS__TERM_TYPE_DRV_CFG:
1221dd60fba7SMathieu Poirier 			ADD_CONFIG_TERM(DRV_CFG, drv_cfg, term->val.str);
1222dd60fba7SMathieu Poirier 			break;
1223064b4e82SJin Yao 		case PARSE_EVENTS__TERM_TYPE_PERCORE:
1224064b4e82SJin Yao 			ADD_CONFIG_TERM(PERCORE, percore,
1225064b4e82SJin Yao 					term->val.num ? true : false);
1226064b4e82SJin Yao 			break;
1227930a2e29SJiri Olsa 		default:
1228930a2e29SJiri Olsa 			break;
1229930a2e29SJiri Olsa 		}
1230930a2e29SJiri Olsa 	}
1231930a2e29SJiri Olsa #undef ADD_EVSEL_CONFIG
1232930a2e29SJiri Olsa 	return 0;
1233930a2e29SJiri Olsa }
1234930a2e29SJiri Olsa 
1235e637d177SHe Kuang int parse_events_add_tracepoint(struct list_head *list, int *idx,
12368c619d6aSWang Nan 				const char *sys, const char *event,
1237272ed29aSJiri Olsa 				struct parse_events_error *err,
1238e637d177SHe Kuang 				struct list_head *head_config)
1239e637d177SHe Kuang {
1240e637d177SHe Kuang 	if (head_config) {
1241e637d177SHe Kuang 		struct perf_event_attr attr;
1242e637d177SHe Kuang 
1243272ed29aSJiri Olsa 		if (config_attr(&attr, head_config, err,
1244e637d177SHe Kuang 				config_term_tracepoint))
1245e637d177SHe Kuang 			return -EINVAL;
1246e637d177SHe Kuang 	}
1247e637d177SHe Kuang 
1248e637d177SHe Kuang 	if (strpbrk(sys, "*?"))
1249e637d177SHe Kuang 		return add_tracepoint_multi_sys(list, idx, sys, event,
1250272ed29aSJiri Olsa 						err, head_config);
1251e637d177SHe Kuang 	else
1252e637d177SHe Kuang 		return add_tracepoint_event(list, idx, sys, event,
1253272ed29aSJiri Olsa 					    err, head_config);
1254e637d177SHe Kuang }
1255e637d177SHe Kuang 
12565d9cdc11SArnaldo Carvalho de Melo int parse_events_add_numeric(struct parse_events_state *parse_state,
125787d650beSJiri Olsa 			     struct list_head *list,
1258b527bab5SRobert Richter 			     u32 type, u64 config,
12598f707d84SJiri Olsa 			     struct list_head *head_config)
126074d5b588SJaswinder Singh Rajput {
126189812fc8SJiri Olsa 	struct perf_event_attr attr;
1262930a2e29SJiri Olsa 	LIST_HEAD(config_terms);
126374d5b588SJaswinder Singh Rajput 
126489812fc8SJiri Olsa 	memset(&attr, 0, sizeof(attr));
126589812fc8SJiri Olsa 	attr.type = type;
126689812fc8SJiri Olsa 	attr.config = config;
12678f707d84SJiri Olsa 
1268930a2e29SJiri Olsa 	if (head_config) {
12695d9cdc11SArnaldo Carvalho de Melo 		if (config_attr(&attr, head_config, parse_state->error,
12700b8891a8SHe Kuang 				config_term_common))
12718f707d84SJiri Olsa 			return -EINVAL;
12728f707d84SJiri Olsa 
1273930a2e29SJiri Olsa 		if (get_config_terms(head_config, &config_terms))
1274930a2e29SJiri Olsa 			return -ENOMEM;
1275930a2e29SJiri Olsa 	}
1276930a2e29SJiri Olsa 
12775d9cdc11SArnaldo Carvalho de Melo 	return add_event(list, &parse_state->idx, &attr,
127810bf358aSWang Nan 			 get_config_name(head_config), &config_terms);
1279b908debdSIngo Molnar }
1280b908debdSIngo Molnar 
1281f0fbb114SAndi Kleen int parse_events_add_tool(struct parse_events_state *parse_state,
1282f0fbb114SAndi Kleen 			  struct list_head *list,
1283f0fbb114SAndi Kleen 			  enum perf_tool_event tool_event)
1284f0fbb114SAndi Kleen {
1285f0fbb114SAndi Kleen 	return add_event_tool(list, &parse_state->idx, tool_event);
1286f0fbb114SAndi Kleen }
1287f0fbb114SAndi Kleen 
1288064b4e82SJin Yao static bool config_term_percore(struct list_head *config_terms)
1289064b4e82SJin Yao {
1290064b4e82SJin Yao 	struct perf_evsel_config_term *term;
1291064b4e82SJin Yao 
1292064b4e82SJin Yao 	list_for_each_entry(term, config_terms, list) {
1293064b4e82SJin Yao 		if (term->type == PERF_EVSEL__CONFIG_TERM_PERCORE)
1294064b4e82SJin Yao 			return term->val.percore;
1295064b4e82SJin Yao 	}
1296064b4e82SJin Yao 
1297064b4e82SJin Yao 	return false;
1298064b4e82SJin Yao }
1299064b4e82SJin Yao 
1300c199c11dSAgustin Vega-Frias int parse_events_add_pmu(struct parse_events_state *parse_state,
130136adec85SJiri Olsa 			 struct list_head *list, char *name,
13023cdc5c2cSKan Liang 			 struct list_head *head_config,
13033cdc5c2cSKan Liang 			 bool auto_merge_stats,
13043cdc5c2cSKan Liang 			 bool use_alias)
13055f537a26SJiri Olsa {
13065f537a26SJiri Olsa 	struct perf_event_attr attr;
130746441bdcSMatt Fleming 	struct perf_pmu_info info;
13085f537a26SJiri Olsa 	struct perf_pmu *pmu;
130932dcd021SJiri Olsa 	struct evsel *evsel;
1310333b5665SAndi Kleen 	struct parse_events_error *err = parse_state->error;
13113cdc5c2cSKan Liang 	bool use_uncore_alias;
1312930a2e29SJiri Olsa 	LIST_HEAD(config_terms);
13135f537a26SJiri Olsa 
13145f537a26SJiri Olsa 	pmu = perf_pmu__find(name);
1315333b5665SAndi Kleen 	if (!pmu) {
1316333b5665SAndi Kleen 		if (asprintf(&err->str,
1317333b5665SAndi Kleen 				"Cannot find PMU `%s'. Missing kernel support?",
1318333b5665SAndi Kleen 				name) < 0)
1319333b5665SAndi Kleen 			err->str = NULL;
13205f537a26SJiri Olsa 		return -EINVAL;
1321333b5665SAndi Kleen 	}
13225f537a26SJiri Olsa 
1323dc0a6202SAdrian Hunter 	if (pmu->default_config) {
1324dc0a6202SAdrian Hunter 		memcpy(&attr, pmu->default_config,
1325dc0a6202SAdrian Hunter 		       sizeof(struct perf_event_attr));
1326dc0a6202SAdrian Hunter 	} else {
13275f537a26SJiri Olsa 		memset(&attr, 0, sizeof(attr));
1328dc0a6202SAdrian Hunter 	}
13295f537a26SJiri Olsa 
13303cdc5c2cSKan Liang 	use_uncore_alias = (pmu->is_uncore && use_alias);
13313cdc5c2cSKan Liang 
1332ad962273SAdrian Hunter 	if (!head_config) {
1333ad962273SAdrian Hunter 		attr.type = pmu->type;
1334f0fbb114SAndi Kleen 		evsel = __add_event(list, &parse_state->idx, &attr, NULL, pmu, NULL,
1335f0fbb114SAndi Kleen 				    auto_merge_stats, NULL);
13368c5421c0SAgustin Vega-Frias 		if (evsel) {
13378c5421c0SAgustin Vega-Frias 			evsel->pmu_name = name;
13383cdc5c2cSKan Liang 			evsel->use_uncore_alias = use_uncore_alias;
13398c5421c0SAgustin Vega-Frias 			return 0;
13408c5421c0SAgustin Vega-Frias 		} else {
13418c5421c0SAgustin Vega-Frias 			return -ENOMEM;
13428c5421c0SAgustin Vega-Frias 		}
1343ad962273SAdrian Hunter 	}
1344ad962273SAdrian Hunter 
134546441bdcSMatt Fleming 	if (perf_pmu__check_alias(pmu, head_config, &info))
1346a6146d50SZheng Yan 		return -EINVAL;
1347a6146d50SZheng Yan 
13485f537a26SJiri Olsa 	/*
13495f537a26SJiri Olsa 	 * Configure hardcoded terms first, no need to check
13505f537a26SJiri Olsa 	 * return value when called with fail == 0 ;)
13515f537a26SJiri Olsa 	 */
13525d9cdc11SArnaldo Carvalho de Melo 	if (config_attr(&attr, head_config, parse_state->error, config_term_pmu))
1353c056ba6aSJiri Olsa 		return -EINVAL;
13545f537a26SJiri Olsa 
1355930a2e29SJiri Olsa 	if (get_config_terms(head_config, &config_terms))
1356930a2e29SJiri Olsa 		return -ENOMEM;
1357930a2e29SJiri Olsa 
13585d9cdc11SArnaldo Carvalho de Melo 	if (perf_pmu__config(pmu, &attr, head_config, parse_state->error))
13595f537a26SJiri Olsa 		return -EINVAL;
13605f537a26SJiri Olsa 
13615d9cdc11SArnaldo Carvalho de Melo 	evsel = __add_event(list, &parse_state->idx, &attr,
136266ec1191SMark Rutland 			    get_config_name(head_config), pmu,
1363f0fbb114SAndi Kleen 			    &config_terms, auto_merge_stats, NULL);
1364410136f5SStephane Eranian 	if (evsel) {
136546441bdcSMatt Fleming 		evsel->unit = info.unit;
136646441bdcSMatt Fleming 		evsel->scale = info.scale;
1367044330c1SMatt Fleming 		evsel->per_pkg = info.per_pkg;
13681d9e446bSJiri Olsa 		evsel->snapshot = info.snapshot;
136937932c18SAndi Kleen 		evsel->metric_expr = info.metric_expr;
137096284814SAndi Kleen 		evsel->metric_name = info.metric_name;
13718c5421c0SAgustin Vega-Frias 		evsel->pmu_name = name;
13723cdc5c2cSKan Liang 		evsel->use_uncore_alias = use_uncore_alias;
1373064b4e82SJin Yao 		evsel->percore = config_term_percore(&evsel->config_terms);
1374410136f5SStephane Eranian 	}
1375410136f5SStephane Eranian 
1376410136f5SStephane Eranian 	return evsel ? 0 : -ENOMEM;
13775f537a26SJiri Olsa }
13785f537a26SJiri Olsa 
13795d9cdc11SArnaldo Carvalho de Melo int parse_events_multi_pmu_add(struct parse_events_state *parse_state,
13802073ad33SAndi Kleen 			       char *str, struct list_head **listp)
13812073ad33SAndi Kleen {
13822073ad33SAndi Kleen 	struct list_head *head;
13832073ad33SAndi Kleen 	struct parse_events_term *term;
13842073ad33SAndi Kleen 	struct list_head *list;
13852073ad33SAndi Kleen 	struct perf_pmu *pmu = NULL;
13862073ad33SAndi Kleen 	int ok = 0;
13872073ad33SAndi Kleen 
13882073ad33SAndi Kleen 	*listp = NULL;
13892073ad33SAndi Kleen 	/* Add it for all PMUs that support the alias */
13902073ad33SAndi Kleen 	list = malloc(sizeof(struct list_head));
13912073ad33SAndi Kleen 	if (!list)
13922073ad33SAndi Kleen 		return -1;
13932073ad33SAndi Kleen 	INIT_LIST_HEAD(list);
13942073ad33SAndi Kleen 	while ((pmu = perf_pmu__scan(pmu)) != NULL) {
13952073ad33SAndi Kleen 		struct perf_pmu_alias *alias;
13962073ad33SAndi Kleen 
13972073ad33SAndi Kleen 		list_for_each_entry(alias, &pmu->aliases, list) {
13982073ad33SAndi Kleen 			if (!strcasecmp(alias->name, str)) {
13992073ad33SAndi Kleen 				head = malloc(sizeof(struct list_head));
14002073ad33SAndi Kleen 				if (!head)
14012073ad33SAndi Kleen 					return -1;
14022073ad33SAndi Kleen 				INIT_LIST_HEAD(head);
14032073ad33SAndi Kleen 				if (parse_events_term__num(&term, PARSE_EVENTS__TERM_TYPE_USER,
14042073ad33SAndi Kleen 							   str, 1, false, &str, NULL) < 0)
14052073ad33SAndi Kleen 					return -1;
14062073ad33SAndi Kleen 				list_add_tail(&term->list, head);
14072073ad33SAndi Kleen 
1408c199c11dSAgustin Vega-Frias 				if (!parse_events_add_pmu(parse_state, list,
14093cdc5c2cSKan Liang 							  pmu->name, head,
14103cdc5c2cSKan Liang 							  true, true)) {
14112073ad33SAndi Kleen 					pr_debug("%s -> %s/%s/\n", str,
14122073ad33SAndi Kleen 						 pmu->name, alias->str);
14132073ad33SAndi Kleen 					ok++;
14142073ad33SAndi Kleen 				}
14152073ad33SAndi Kleen 
14162073ad33SAndi Kleen 				parse_events_terms__delete(head);
14172073ad33SAndi Kleen 			}
14182073ad33SAndi Kleen 		}
14192073ad33SAndi Kleen 	}
14202073ad33SAndi Kleen 	if (!ok)
14212073ad33SAndi Kleen 		return -1;
14222073ad33SAndi Kleen 	*listp = list;
14232073ad33SAndi Kleen 	return 0;
14242073ad33SAndi Kleen }
14252073ad33SAndi Kleen 
14266a4bb04cSJiri Olsa int parse_events__modifier_group(struct list_head *list,
14276a4bb04cSJiri Olsa 				 char *event_mod)
142889efb029SJiri Olsa {
14296a4bb04cSJiri Olsa 	return parse_events__modifier_event(list, event_mod, true);
14306a4bb04cSJiri Olsa }
14316a4bb04cSJiri Olsa 
14323cdc5c2cSKan Liang /*
14333cdc5c2cSKan Liang  * Check if the two uncore PMUs are from the same uncore block
14343cdc5c2cSKan Liang  * The format of the uncore PMU name is uncore_#blockname_#pmuidx
14353cdc5c2cSKan Liang  */
14363cdc5c2cSKan Liang static bool is_same_uncore_block(const char *pmu_name_a, const char *pmu_name_b)
14373cdc5c2cSKan Liang {
14383cdc5c2cSKan Liang 	char *end_a, *end_b;
14393cdc5c2cSKan Liang 
14403cdc5c2cSKan Liang 	end_a = strrchr(pmu_name_a, '_');
14413cdc5c2cSKan Liang 	end_b = strrchr(pmu_name_b, '_');
14423cdc5c2cSKan Liang 
14433cdc5c2cSKan Liang 	if (!end_a || !end_b)
14443cdc5c2cSKan Liang 		return false;
14453cdc5c2cSKan Liang 
14463cdc5c2cSKan Liang 	if ((end_a - pmu_name_a) != (end_b - pmu_name_b))
14473cdc5c2cSKan Liang 		return false;
14483cdc5c2cSKan Liang 
14493cdc5c2cSKan Liang 	return (strncmp(pmu_name_a, pmu_name_b, end_a - pmu_name_a) == 0);
14503cdc5c2cSKan Liang }
14513cdc5c2cSKan Liang 
14523cdc5c2cSKan Liang static int
14533cdc5c2cSKan Liang parse_events__set_leader_for_uncore_aliase(char *name, struct list_head *list,
14543cdc5c2cSKan Liang 					   struct parse_events_state *parse_state)
14553cdc5c2cSKan Liang {
145632dcd021SJiri Olsa 	struct evsel *evsel, *leader;
14573cdc5c2cSKan Liang 	uintptr_t *leaders;
14583cdc5c2cSKan Liang 	bool is_leader = true;
14593cdc5c2cSKan Liang 	int i, nr_pmu = 0, total_members, ret = 0;
14603cdc5c2cSKan Liang 
1461*b27c4eceSJiri Olsa 	leader = list_first_entry(list, struct evsel, core.node);
1462*b27c4eceSJiri Olsa 	evsel = list_last_entry(list, struct evsel, core.node);
14633cdc5c2cSKan Liang 	total_members = evsel->idx - leader->idx + 1;
14643cdc5c2cSKan Liang 
14653cdc5c2cSKan Liang 	leaders = calloc(total_members, sizeof(uintptr_t));
14663cdc5c2cSKan Liang 	if (WARN_ON(!leaders))
14673cdc5c2cSKan Liang 		return 0;
14683cdc5c2cSKan Liang 
14693cdc5c2cSKan Liang 	/*
14703cdc5c2cSKan Liang 	 * Going through the whole group and doing sanity check.
14713cdc5c2cSKan Liang 	 * All members must use alias, and be from the same uncore block.
14723cdc5c2cSKan Liang 	 * Also, storing the leader events in an array.
14733cdc5c2cSKan Liang 	 */
14743cdc5c2cSKan Liang 	__evlist__for_each_entry(list, evsel) {
14753cdc5c2cSKan Liang 
14763cdc5c2cSKan Liang 		/* Only split the uncore group which members use alias */
14773cdc5c2cSKan Liang 		if (!evsel->use_uncore_alias)
14783cdc5c2cSKan Liang 			goto out;
14793cdc5c2cSKan Liang 
14803cdc5c2cSKan Liang 		/* The events must be from the same uncore block */
14813cdc5c2cSKan Liang 		if (!is_same_uncore_block(leader->pmu_name, evsel->pmu_name))
14823cdc5c2cSKan Liang 			goto out;
14833cdc5c2cSKan Liang 
14843cdc5c2cSKan Liang 		if (!is_leader)
14853cdc5c2cSKan Liang 			continue;
14863cdc5c2cSKan Liang 		/*
14873cdc5c2cSKan Liang 		 * If the event's PMU name starts to repeat, it must be a new
14883cdc5c2cSKan Liang 		 * event. That can be used to distinguish the leader from
14893cdc5c2cSKan Liang 		 * other members, even they have the same event name.
14903cdc5c2cSKan Liang 		 */
14913cdc5c2cSKan Liang 		if ((leader != evsel) && (leader->pmu_name == evsel->pmu_name)) {
14923cdc5c2cSKan Liang 			is_leader = false;
14933cdc5c2cSKan Liang 			continue;
14943cdc5c2cSKan Liang 		}
14953cdc5c2cSKan Liang 		/* The name is always alias name */
14963cdc5c2cSKan Liang 		WARN_ON(strcmp(leader->name, evsel->name));
14973cdc5c2cSKan Liang 
14983cdc5c2cSKan Liang 		/* Store the leader event for each PMU */
14993cdc5c2cSKan Liang 		leaders[nr_pmu++] = (uintptr_t) evsel;
15003cdc5c2cSKan Liang 	}
15013cdc5c2cSKan Liang 
15023cdc5c2cSKan Liang 	/* only one event alias */
15033cdc5c2cSKan Liang 	if (nr_pmu == total_members) {
15043cdc5c2cSKan Liang 		parse_state->nr_groups--;
15053cdc5c2cSKan Liang 		goto handled;
15063cdc5c2cSKan Liang 	}
15073cdc5c2cSKan Liang 
15083cdc5c2cSKan Liang 	/*
15093cdc5c2cSKan Liang 	 * An uncore event alias is a joint name which means the same event
15103cdc5c2cSKan Liang 	 * runs on all PMUs of a block.
15113cdc5c2cSKan Liang 	 * Perf doesn't support mixed events from different PMUs in the same
15123cdc5c2cSKan Liang 	 * group. The big group has to be split into multiple small groups
15133cdc5c2cSKan Liang 	 * which only include the events from the same PMU.
15143cdc5c2cSKan Liang 	 *
15153cdc5c2cSKan Liang 	 * Here the uncore event aliases must be from the same uncore block.
15163cdc5c2cSKan Liang 	 * The number of PMUs must be same for each alias. The number of new
15173cdc5c2cSKan Liang 	 * small groups equals to the number of PMUs.
15183cdc5c2cSKan Liang 	 * Setting the leader event for corresponding members in each group.
15193cdc5c2cSKan Liang 	 */
15203cdc5c2cSKan Liang 	i = 0;
15213cdc5c2cSKan Liang 	__evlist__for_each_entry(list, evsel) {
15223cdc5c2cSKan Liang 		if (i >= nr_pmu)
15233cdc5c2cSKan Liang 			i = 0;
152432dcd021SJiri Olsa 		evsel->leader = (struct evsel *) leaders[i++];
15253cdc5c2cSKan Liang 	}
15263cdc5c2cSKan Liang 
15273cdc5c2cSKan Liang 	/* The number of members and group name are same for each group */
15283cdc5c2cSKan Liang 	for (i = 0; i < nr_pmu; i++) {
152932dcd021SJiri Olsa 		evsel = (struct evsel *) leaders[i];
15303cdc5c2cSKan Liang 		evsel->nr_members = total_members / nr_pmu;
15313cdc5c2cSKan Liang 		evsel->group_name = name ? strdup(name) : NULL;
15323cdc5c2cSKan Liang 	}
15333cdc5c2cSKan Liang 
15343cdc5c2cSKan Liang 	/* Take the new small groups into account */
15353cdc5c2cSKan Liang 	parse_state->nr_groups += nr_pmu - 1;
15363cdc5c2cSKan Liang 
15373cdc5c2cSKan Liang handled:
15383cdc5c2cSKan Liang 	ret = 1;
15393cdc5c2cSKan Liang out:
15403cdc5c2cSKan Liang 	free(leaders);
15413cdc5c2cSKan Liang 	return ret;
15423cdc5c2cSKan Liang }
15433cdc5c2cSKan Liang 
15443cdc5c2cSKan Liang void parse_events__set_leader(char *name, struct list_head *list,
15453cdc5c2cSKan Liang 			      struct parse_events_state *parse_state)
15466a4bb04cSJiri Olsa {
154732dcd021SJiri Olsa 	struct evsel *leader;
15486a4bb04cSJiri Olsa 
1549854f7363SWang Nan 	if (list_empty(list)) {
1550854f7363SWang Nan 		WARN_ONCE(true, "WARNING: failed to set leader: empty list");
1551854f7363SWang Nan 		return;
1552854f7363SWang Nan 	}
1553854f7363SWang Nan 
15543cdc5c2cSKan Liang 	if (parse_events__set_leader_for_uncore_aliase(name, list, parse_state))
15553cdc5c2cSKan Liang 		return;
15563cdc5c2cSKan Liang 
155763dab225SArnaldo Carvalho de Melo 	__perf_evlist__set_leader(list);
1558*b27c4eceSJiri Olsa 	leader = list_entry(list->next, struct evsel, core.node);
15596a4bb04cSJiri Olsa 	leader->group_name = name ? strdup(name) : NULL;
156089efb029SJiri Olsa }
156189efb029SJiri Olsa 
1562c5cd8ac0SDavid Ahern /* list_event is assumed to point to malloc'ed memory */
15635d7be90eSJiri Olsa void parse_events_update_lists(struct list_head *list_event,
15645d7be90eSJiri Olsa 			       struct list_head *list_all)
15655d7be90eSJiri Olsa {
15665d7be90eSJiri Olsa 	/*
15675d7be90eSJiri Olsa 	 * Called for single event definition. Update the
156889efb029SJiri Olsa 	 * 'all event' list, and reinit the 'single event'
15695d7be90eSJiri Olsa 	 * list, for next event definition.
15705d7be90eSJiri Olsa 	 */
15715d7be90eSJiri Olsa 	list_splice_tail(list_event, list_all);
1572b847cbdcSJiri Olsa 	free(list_event);
15735d7be90eSJiri Olsa }
15745d7be90eSJiri Olsa 
1575f5b1135bSJiri Olsa struct event_modifier {
1576f5b1135bSJiri Olsa 	int eu;
1577f5b1135bSJiri Olsa 	int ek;
1578f5b1135bSJiri Olsa 	int eh;
1579f5b1135bSJiri Olsa 	int eH;
1580f5b1135bSJiri Olsa 	int eG;
1581a1e12da4SJiri Olsa 	int eI;
1582f5b1135bSJiri Olsa 	int precise;
15837f94af7aSJiri Olsa 	int precise_max;
1584f5b1135bSJiri Olsa 	int exclude_GH;
15853c176311SJiri Olsa 	int sample_read;
1586e9a7c414SMichael Ellerman 	int pinned;
15875a5dfe4bSAndi Kleen 	int weak;
1588f5b1135bSJiri Olsa };
158961c45981SPaul Mackerras 
1590f5b1135bSJiri Olsa static int get_event_modifier(struct event_modifier *mod, char *str,
159132dcd021SJiri Olsa 			       struct evsel *evsel)
1592f5b1135bSJiri Olsa {
1593f5b1135bSJiri Olsa 	int eu = evsel ? evsel->attr.exclude_user : 0;
1594f5b1135bSJiri Olsa 	int ek = evsel ? evsel->attr.exclude_kernel : 0;
1595f5b1135bSJiri Olsa 	int eh = evsel ? evsel->attr.exclude_hv : 0;
1596f5b1135bSJiri Olsa 	int eH = evsel ? evsel->attr.exclude_host : 0;
1597f5b1135bSJiri Olsa 	int eG = evsel ? evsel->attr.exclude_guest : 0;
1598a1e12da4SJiri Olsa 	int eI = evsel ? evsel->attr.exclude_idle : 0;
1599f5b1135bSJiri Olsa 	int precise = evsel ? evsel->attr.precise_ip : 0;
16007f94af7aSJiri Olsa 	int precise_max = 0;
16013c176311SJiri Olsa 	int sample_read = 0;
1602e9a7c414SMichael Ellerman 	int pinned = evsel ? evsel->attr.pinned : 0;
1603f5b1135bSJiri Olsa 
1604f5b1135bSJiri Olsa 	int exclude = eu | ek | eh;
1605f5b1135bSJiri Olsa 	int exclude_GH = evsel ? evsel->exclude_GH : 0;
16065a5dfe4bSAndi Kleen 	int weak = 0;
1607f5b1135bSJiri Olsa 
1608f5b1135bSJiri Olsa 	memset(mod, 0, sizeof(*mod));
1609ceb53fbfSIngo Molnar 
161061c45981SPaul Mackerras 	while (*str) {
1611ab608344SPeter Zijlstra 		if (*str == 'u') {
1612ab608344SPeter Zijlstra 			if (!exclude)
1613ab608344SPeter Zijlstra 				exclude = eu = ek = eh = 1;
161461c45981SPaul Mackerras 			eu = 0;
1615ab608344SPeter Zijlstra 		} else if (*str == 'k') {
1616ab608344SPeter Zijlstra 			if (!exclude)
1617ab608344SPeter Zijlstra 				exclude = eu = ek = eh = 1;
161861c45981SPaul Mackerras 			ek = 0;
1619ab608344SPeter Zijlstra 		} else if (*str == 'h') {
1620ab608344SPeter Zijlstra 			if (!exclude)
1621ab608344SPeter Zijlstra 				exclude = eu = ek = eh = 1;
162261c45981SPaul Mackerras 			eh = 0;
162399320cc8SJoerg Roedel 		} else if (*str == 'G') {
162499320cc8SJoerg Roedel 			if (!exclude_GH)
162599320cc8SJoerg Roedel 				exclude_GH = eG = eH = 1;
162699320cc8SJoerg Roedel 			eG = 0;
162799320cc8SJoerg Roedel 		} else if (*str == 'H') {
162899320cc8SJoerg Roedel 			if (!exclude_GH)
162999320cc8SJoerg Roedel 				exclude_GH = eG = eH = 1;
163099320cc8SJoerg Roedel 			eH = 0;
1631a1e12da4SJiri Olsa 		} else if (*str == 'I') {
1632a1e12da4SJiri Olsa 			eI = 1;
1633ab608344SPeter Zijlstra 		} else if (*str == 'p') {
1634ab608344SPeter Zijlstra 			precise++;
16351342798cSDavid Ahern 			/* use of precise requires exclude_guest */
16361342798cSDavid Ahern 			if (!exclude_GH)
16371342798cSDavid Ahern 				eG = 1;
16387f94af7aSJiri Olsa 		} else if (*str == 'P') {
16397f94af7aSJiri Olsa 			precise_max = 1;
16403c176311SJiri Olsa 		} else if (*str == 'S') {
16413c176311SJiri Olsa 			sample_read = 1;
1642e9a7c414SMichael Ellerman 		} else if (*str == 'D') {
1643e9a7c414SMichael Ellerman 			pinned = 1;
16445a5dfe4bSAndi Kleen 		} else if (*str == 'W') {
16455a5dfe4bSAndi Kleen 			weak = 1;
1646ab608344SPeter Zijlstra 		} else
164761c45981SPaul Mackerras 			break;
1648ab608344SPeter Zijlstra 
164961c45981SPaul Mackerras 		++str;
165061c45981SPaul Mackerras 	}
165174d5b588SJaswinder Singh Rajput 
165286470930SIngo Molnar 	/*
165389812fc8SJiri Olsa 	 * precise ip:
165489812fc8SJiri Olsa 	 *
165589812fc8SJiri Olsa 	 *  0 - SAMPLE_IP can have arbitrary skid
165689812fc8SJiri Olsa 	 *  1 - SAMPLE_IP must have constant skid
165789812fc8SJiri Olsa 	 *  2 - SAMPLE_IP requested to have 0 skid
165889812fc8SJiri Olsa 	 *  3 - SAMPLE_IP must have 0 skid
165989812fc8SJiri Olsa 	 *
166089812fc8SJiri Olsa 	 *  See also PERF_RECORD_MISC_EXACT_IP
166186470930SIngo Molnar 	 */
166289812fc8SJiri Olsa 	if (precise > 3)
166389812fc8SJiri Olsa 		return -EINVAL;
166486470930SIngo Molnar 
1665f5b1135bSJiri Olsa 	mod->eu = eu;
1666f5b1135bSJiri Olsa 	mod->ek = ek;
1667f5b1135bSJiri Olsa 	mod->eh = eh;
1668f5b1135bSJiri Olsa 	mod->eH = eH;
1669f5b1135bSJiri Olsa 	mod->eG = eG;
1670a1e12da4SJiri Olsa 	mod->eI = eI;
1671f5b1135bSJiri Olsa 	mod->precise = precise;
16727f94af7aSJiri Olsa 	mod->precise_max = precise_max;
1673f5b1135bSJiri Olsa 	mod->exclude_GH = exclude_GH;
16743c176311SJiri Olsa 	mod->sample_read = sample_read;
1675e9a7c414SMichael Ellerman 	mod->pinned = pinned;
16765a5dfe4bSAndi Kleen 	mod->weak = weak;
1677e9a7c414SMichael Ellerman 
1678f5b1135bSJiri Olsa 	return 0;
1679f5b1135bSJiri Olsa }
1680f5b1135bSJiri Olsa 
1681534123f4SJiri Olsa /*
1682534123f4SJiri Olsa  * Basic modifier sanity check to validate it contains only one
1683534123f4SJiri Olsa  * instance of any modifier (apart from 'p') present.
1684534123f4SJiri Olsa  */
1685534123f4SJiri Olsa static int check_modifier(char *str)
1686534123f4SJiri Olsa {
1687534123f4SJiri Olsa 	char *p = str;
1688534123f4SJiri Olsa 
1689534123f4SJiri Olsa 	/* The sizeof includes 0 byte as well. */
16905a5dfe4bSAndi Kleen 	if (strlen(str) > (sizeof("ukhGHpppPSDIW") - 1))
1691534123f4SJiri Olsa 		return -1;
1692534123f4SJiri Olsa 
1693534123f4SJiri Olsa 	while (*p) {
1694534123f4SJiri Olsa 		if (*p != 'p' && strchr(p + 1, *p))
1695534123f4SJiri Olsa 			return -1;
1696534123f4SJiri Olsa 		p++;
1697534123f4SJiri Olsa 	}
1698534123f4SJiri Olsa 
1699534123f4SJiri Olsa 	return 0;
1700534123f4SJiri Olsa }
1701534123f4SJiri Olsa 
1702f5b1135bSJiri Olsa int parse_events__modifier_event(struct list_head *list, char *str, bool add)
1703f5b1135bSJiri Olsa {
170432dcd021SJiri Olsa 	struct evsel *evsel;
1705f5b1135bSJiri Olsa 	struct event_modifier mod;
1706f5b1135bSJiri Olsa 
1707f5b1135bSJiri Olsa 	if (str == NULL)
1708f5b1135bSJiri Olsa 		return 0;
1709f5b1135bSJiri Olsa 
1710534123f4SJiri Olsa 	if (check_modifier(str))
1711534123f4SJiri Olsa 		return -EINVAL;
1712534123f4SJiri Olsa 
1713f5b1135bSJiri Olsa 	if (!add && get_event_modifier(&mod, str, NULL))
1714f5b1135bSJiri Olsa 		return -EINVAL;
1715f5b1135bSJiri Olsa 
1716e5cadb93SArnaldo Carvalho de Melo 	__evlist__for_each_entry(list, evsel) {
1717f5b1135bSJiri Olsa 		if (add && get_event_modifier(&mod, str, evsel))
1718f5b1135bSJiri Olsa 			return -EINVAL;
1719f5b1135bSJiri Olsa 
1720f5b1135bSJiri Olsa 		evsel->attr.exclude_user   = mod.eu;
1721f5b1135bSJiri Olsa 		evsel->attr.exclude_kernel = mod.ek;
1722f5b1135bSJiri Olsa 		evsel->attr.exclude_hv     = mod.eh;
1723f5b1135bSJiri Olsa 		evsel->attr.precise_ip     = mod.precise;
1724f5b1135bSJiri Olsa 		evsel->attr.exclude_host   = mod.eH;
1725f5b1135bSJiri Olsa 		evsel->attr.exclude_guest  = mod.eG;
1726a1e12da4SJiri Olsa 		evsel->attr.exclude_idle   = mod.eI;
1727f5b1135bSJiri Olsa 		evsel->exclude_GH          = mod.exclude_GH;
17283c176311SJiri Olsa 		evsel->sample_read         = mod.sample_read;
17297f94af7aSJiri Olsa 		evsel->precise_max         = mod.precise_max;
17305a5dfe4bSAndi Kleen 		evsel->weak_group	   = mod.weak;
1731e9a7c414SMichael Ellerman 
1732e9a7c414SMichael Ellerman 		if (perf_evsel__is_group_leader(evsel))
1733e9a7c414SMichael Ellerman 			evsel->attr.pinned = mod.pinned;
1734ceb53fbfSIngo Molnar 	}
173586470930SIngo Molnar 
173689812fc8SJiri Olsa 	return 0;
173786470930SIngo Molnar }
173886470930SIngo Molnar 
1739ac2ba9f3SRobert Richter int parse_events_name(struct list_head *list, char *name)
1740ac2ba9f3SRobert Richter {
174132dcd021SJiri Olsa 	struct evsel *evsel;
1742ac2ba9f3SRobert Richter 
1743e5cadb93SArnaldo Carvalho de Melo 	__evlist__for_each_entry(list, evsel) {
1744ac2ba9f3SRobert Richter 		if (!evsel->name)
1745ac2ba9f3SRobert Richter 			evsel->name = strdup(name);
1746ac2ba9f3SRobert Richter 	}
1747ac2ba9f3SRobert Richter 
1748ac2ba9f3SRobert Richter 	return 0;
1749ac2ba9f3SRobert Richter }
1750ac2ba9f3SRobert Richter 
1751dcb4e102SKan Liang static int
1752dcb4e102SKan Liang comp_pmu(const void *p1, const void *p2)
1753dcb4e102SKan Liang {
1754dcb4e102SKan Liang 	struct perf_pmu_event_symbol *pmu1 = (struct perf_pmu_event_symbol *) p1;
1755dcb4e102SKan Liang 	struct perf_pmu_event_symbol *pmu2 = (struct perf_pmu_event_symbol *) p2;
1756dcb4e102SKan Liang 
1757e312bcf1SAndi Kleen 	return strcasecmp(pmu1->symbol, pmu2->symbol);
1758dcb4e102SKan Liang }
1759dcb4e102SKan Liang 
1760dcb4e102SKan Liang static void perf_pmu__parse_cleanup(void)
1761dcb4e102SKan Liang {
1762dcb4e102SKan Liang 	if (perf_pmu_events_list_num > 0) {
1763dcb4e102SKan Liang 		struct perf_pmu_event_symbol *p;
1764dcb4e102SKan Liang 		int i;
1765dcb4e102SKan Liang 
1766dcb4e102SKan Liang 		for (i = 0; i < perf_pmu_events_list_num; i++) {
1767dcb4e102SKan Liang 			p = perf_pmu_events_list + i;
1768360e071bSTaeung Song 			zfree(&p->symbol);
1769dcb4e102SKan Liang 		}
1770506fde11STaeung Song 		zfree(&perf_pmu_events_list);
1771dcb4e102SKan Liang 		perf_pmu_events_list_num = 0;
1772dcb4e102SKan Liang 	}
1773dcb4e102SKan Liang }
1774dcb4e102SKan Liang 
1775dcb4e102SKan Liang #define SET_SYMBOL(str, stype)		\
1776dcb4e102SKan Liang do {					\
1777dcb4e102SKan Liang 	p->symbol = str;		\
1778dcb4e102SKan Liang 	if (!p->symbol)			\
1779dcb4e102SKan Liang 		goto err;		\
1780dcb4e102SKan Liang 	p->type = stype;		\
1781dcb4e102SKan Liang } while (0)
1782dcb4e102SKan Liang 
1783dcb4e102SKan Liang /*
1784dcb4e102SKan Liang  * Read the pmu events list from sysfs
1785dcb4e102SKan Liang  * Save it into perf_pmu_events_list
1786dcb4e102SKan Liang  */
1787dcb4e102SKan Liang static void perf_pmu__parse_init(void)
1788dcb4e102SKan Liang {
1789dcb4e102SKan Liang 
1790dcb4e102SKan Liang 	struct perf_pmu *pmu = NULL;
1791dcb4e102SKan Liang 	struct perf_pmu_alias *alias;
1792dcb4e102SKan Liang 	int len = 0;
1793dcb4e102SKan Liang 
1794231bb2aaSAndi Kleen 	pmu = NULL;
1795231bb2aaSAndi Kleen 	while ((pmu = perf_pmu__scan(pmu)) != NULL) {
1796dcb4e102SKan Liang 		list_for_each_entry(alias, &pmu->aliases, list) {
1797dcb4e102SKan Liang 			if (strchr(alias->name, '-'))
1798dcb4e102SKan Liang 				len++;
1799dcb4e102SKan Liang 			len++;
1800dcb4e102SKan Liang 		}
1801231bb2aaSAndi Kleen 	}
1802231bb2aaSAndi Kleen 
1803231bb2aaSAndi Kleen 	if (len == 0) {
1804231bb2aaSAndi Kleen 		perf_pmu_events_list_num = -1;
1805231bb2aaSAndi Kleen 		return;
1806231bb2aaSAndi Kleen 	}
1807dcb4e102SKan Liang 	perf_pmu_events_list = malloc(sizeof(struct perf_pmu_event_symbol) * len);
1808dcb4e102SKan Liang 	if (!perf_pmu_events_list)
1809dcb4e102SKan Liang 		return;
1810dcb4e102SKan Liang 	perf_pmu_events_list_num = len;
1811dcb4e102SKan Liang 
1812dcb4e102SKan Liang 	len = 0;
1813231bb2aaSAndi Kleen 	pmu = NULL;
1814231bb2aaSAndi Kleen 	while ((pmu = perf_pmu__scan(pmu)) != NULL) {
1815dcb4e102SKan Liang 		list_for_each_entry(alias, &pmu->aliases, list) {
1816dcb4e102SKan Liang 			struct perf_pmu_event_symbol *p = perf_pmu_events_list + len;
1817dcb4e102SKan Liang 			char *tmp = strchr(alias->name, '-');
1818dcb4e102SKan Liang 
1819dcb4e102SKan Liang 			if (tmp != NULL) {
1820dcb4e102SKan Liang 				SET_SYMBOL(strndup(alias->name, tmp - alias->name),
1821dcb4e102SKan Liang 						PMU_EVENT_SYMBOL_PREFIX);
1822dcb4e102SKan Liang 				p++;
1823dcb4e102SKan Liang 				SET_SYMBOL(strdup(++tmp), PMU_EVENT_SYMBOL_SUFFIX);
1824dcb4e102SKan Liang 				len += 2;
1825dcb4e102SKan Liang 			} else {
1826dcb4e102SKan Liang 				SET_SYMBOL(strdup(alias->name), PMU_EVENT_SYMBOL);
1827dcb4e102SKan Liang 				len++;
1828dcb4e102SKan Liang 			}
1829dcb4e102SKan Liang 		}
1830231bb2aaSAndi Kleen 	}
1831dcb4e102SKan Liang 	qsort(perf_pmu_events_list, len,
1832dcb4e102SKan Liang 		sizeof(struct perf_pmu_event_symbol), comp_pmu);
1833dcb4e102SKan Liang 
1834dcb4e102SKan Liang 	return;
1835dcb4e102SKan Liang err:
1836dcb4e102SKan Liang 	perf_pmu__parse_cleanup();
1837dcb4e102SKan Liang }
1838dcb4e102SKan Liang 
1839dcb4e102SKan Liang enum perf_pmu_event_symbol_type
1840dcb4e102SKan Liang perf_pmu__parse_check(const char *name)
1841dcb4e102SKan Liang {
1842dcb4e102SKan Liang 	struct perf_pmu_event_symbol p, *r;
1843dcb4e102SKan Liang 
1844dcb4e102SKan Liang 	/* scan kernel pmu events from sysfs if needed */
1845dcb4e102SKan Liang 	if (perf_pmu_events_list_num == 0)
1846dcb4e102SKan Liang 		perf_pmu__parse_init();
1847dcb4e102SKan Liang 	/*
1848dcb4e102SKan Liang 	 * name "cpu" could be prefix of cpu-cycles or cpu// events.
1849dcb4e102SKan Liang 	 * cpu-cycles has been handled by hardcode.
1850dcb4e102SKan Liang 	 * So it must be cpu// events, not kernel pmu event.
1851dcb4e102SKan Liang 	 */
1852dcb4e102SKan Liang 	if ((perf_pmu_events_list_num <= 0) || !strcmp(name, "cpu"))
1853dcb4e102SKan Liang 		return PMU_EVENT_SYMBOL_ERR;
1854dcb4e102SKan Liang 
1855dcb4e102SKan Liang 	p.symbol = strdup(name);
1856dcb4e102SKan Liang 	r = bsearch(&p, perf_pmu_events_list,
1857dcb4e102SKan Liang 			(size_t) perf_pmu_events_list_num,
1858dcb4e102SKan Liang 			sizeof(struct perf_pmu_event_symbol), comp_pmu);
1859360e071bSTaeung Song 	zfree(&p.symbol);
1860dcb4e102SKan Liang 	return r ? r->type : PMU_EVENT_SYMBOL_ERR;
1861dcb4e102SKan Liang }
1862dcb4e102SKan Liang 
18635d9cdc11SArnaldo Carvalho de Melo static int parse_events__scanner(const char *str, void *parse_state, int start_token)
1864ac20de6fSZheng Yan {
1865ac20de6fSZheng Yan 	YY_BUFFER_STATE buffer;
1866ac20de6fSZheng Yan 	void *scanner;
1867ac20de6fSZheng Yan 	int ret;
1868ac20de6fSZheng Yan 
186990e2b22dSJiri Olsa 	ret = parse_events_lex_init_extra(start_token, &scanner);
1870ac20de6fSZheng Yan 	if (ret)
1871ac20de6fSZheng Yan 		return ret;
1872ac20de6fSZheng Yan 
1873ac20de6fSZheng Yan 	buffer = parse_events__scan_string(str, scanner);
1874ac20de6fSZheng Yan 
1875ac20de6fSZheng Yan #ifdef PARSER_DEBUG
1876ac20de6fSZheng Yan 	parse_events_debug = 1;
1877ac20de6fSZheng Yan #endif
18785d9cdc11SArnaldo Carvalho de Melo 	ret = parse_events_parse(parse_state, scanner);
1879ac20de6fSZheng Yan 
1880ac20de6fSZheng Yan 	parse_events__flush_buffer(buffer, scanner);
1881ac20de6fSZheng Yan 	parse_events__delete_buffer(buffer, scanner);
1882ac20de6fSZheng Yan 	parse_events_lex_destroy(scanner);
1883ac20de6fSZheng Yan 	return ret;
1884ac20de6fSZheng Yan }
1885ac20de6fSZheng Yan 
188690e2b22dSJiri Olsa /*
188790e2b22dSJiri Olsa  * parse event config string, return a list of event terms.
188890e2b22dSJiri Olsa  */
188990e2b22dSJiri Olsa int parse_events_terms(struct list_head *terms, const char *str)
189090e2b22dSJiri Olsa {
18915d9cdc11SArnaldo Carvalho de Melo 	struct parse_events_state parse_state = {
189290e2b22dSJiri Olsa 		.terms = NULL,
189390e2b22dSJiri Olsa 	};
189490e2b22dSJiri Olsa 	int ret;
189590e2b22dSJiri Olsa 
18965d9cdc11SArnaldo Carvalho de Melo 	ret = parse_events__scanner(str, &parse_state, PE_START_TERMS);
189790e2b22dSJiri Olsa 	if (!ret) {
18985d9cdc11SArnaldo Carvalho de Melo 		list_splice(parse_state.terms, terms);
18995d9cdc11SArnaldo Carvalho de Melo 		zfree(&parse_state.terms);
190090e2b22dSJiri Olsa 		return 0;
190190e2b22dSJiri Olsa 	}
190290e2b22dSJiri Olsa 
19035d9cdc11SArnaldo Carvalho de Melo 	parse_events_terms__delete(parse_state.terms);
190490e2b22dSJiri Olsa 	return ret;
190590e2b22dSJiri Olsa }
190690e2b22dSJiri Olsa 
190763503dbaSJiri Olsa int parse_events(struct evlist *evlist, const char *str,
1908b39b8393SJiri Olsa 		 struct parse_events_error *err)
190986470930SIngo Molnar {
19105d9cdc11SArnaldo Carvalho de Melo 	struct parse_events_state parse_state = {
19115d9cdc11SArnaldo Carvalho de Melo 		.list   = LIST_HEAD_INIT(parse_state.list),
191246010ab2SJiri Olsa 		.idx    = evlist->nr_entries,
1913b39b8393SJiri Olsa 		.error  = err,
19147630b3e2SWang Nan 		.evlist = evlist,
191546010ab2SJiri Olsa 	};
191646010ab2SJiri Olsa 	int ret;
191786470930SIngo Molnar 
19185d9cdc11SArnaldo Carvalho de Melo 	ret = parse_events__scanner(str, &parse_state, PE_START_EVENTS);
1919dcb4e102SKan Liang 	perf_pmu__parse_cleanup();
192089812fc8SJiri Olsa 	if (!ret) {
192132dcd021SJiri Olsa 		struct evsel *last;
192215bfd2ccSWang Nan 
19235d9cdc11SArnaldo Carvalho de Melo 		if (list_empty(&parse_state.list)) {
1924c23080a6SArnaldo Carvalho de Melo 			WARN_ONCE(true, "WARNING: event parser found nothing\n");
1925854f7363SWang Nan 			return -1;
1926854f7363SWang Nan 		}
1927854f7363SWang Nan 
19285d9cdc11SArnaldo Carvalho de Melo 		perf_evlist__splice_list_tail(evlist, &parse_state.list);
19295d9cdc11SArnaldo Carvalho de Melo 		evlist->nr_groups += parse_state.nr_groups;
193015bfd2ccSWang Nan 		last = perf_evlist__last(evlist);
193115bfd2ccSWang Nan 		last->cmdline_group_boundary = true;
193215bfd2ccSWang Nan 
193386470930SIngo Molnar 		return 0;
193486470930SIngo Molnar 	}
193586470930SIngo Molnar 
19365d7be90eSJiri Olsa 	/*
19375d7be90eSJiri Olsa 	 * There are 2 users - builtin-record and builtin-test objects.
1938c12995a5SJiri Olsa 	 * Both call evlist__delete in case of error, so we dont
19395d7be90eSJiri Olsa 	 * need to bother.
19405d7be90eSJiri Olsa 	 */
194189812fc8SJiri Olsa 	return ret;
194289812fc8SJiri Olsa }
194389812fc8SJiri Olsa 
1944b39b8393SJiri Olsa #define MAX_WIDTH 1000
1945b39b8393SJiri Olsa static int get_term_width(void)
1946b39b8393SJiri Olsa {
1947b39b8393SJiri Olsa 	struct winsize ws;
1948b39b8393SJiri Olsa 
1949b39b8393SJiri Olsa 	get_term_dimensions(&ws);
1950b39b8393SJiri Olsa 	return ws.ws_col > MAX_WIDTH ? MAX_WIDTH : ws.ws_col;
1951b39b8393SJiri Olsa }
1952b39b8393SJiri Olsa 
1953333b5665SAndi Kleen void parse_events_print_error(struct parse_events_error *err,
1954b39b8393SJiri Olsa 			      const char *event)
1955b39b8393SJiri Olsa {
1956b39b8393SJiri Olsa 	const char *str = "invalid or unsupported event: ";
1957b39b8393SJiri Olsa 	char _buf[MAX_WIDTH];
1958b39b8393SJiri Olsa 	char *buf = (char *) event;
1959b39b8393SJiri Olsa 	int idx = 0;
1960b39b8393SJiri Olsa 
1961b39b8393SJiri Olsa 	if (err->str) {
1962b39b8393SJiri Olsa 		/* -2 for extra '' in the final fprintf */
1963b39b8393SJiri Olsa 		int width       = get_term_width() - 2;
1964b39b8393SJiri Olsa 		int len_event   = strlen(event);
1965b39b8393SJiri Olsa 		int len_str, max_len, cut = 0;
1966b39b8393SJiri Olsa 
1967b39b8393SJiri Olsa 		/*
1968b39b8393SJiri Olsa 		 * Maximum error index indent, we will cut
1969b39b8393SJiri Olsa 		 * the event string if it's bigger.
1970b39b8393SJiri Olsa 		 */
1971141b2d31SAdrian Hunter 		int max_err_idx = 13;
1972b39b8393SJiri Olsa 
1973b39b8393SJiri Olsa 		/*
1974b39b8393SJiri Olsa 		 * Let's be specific with the message when
1975b39b8393SJiri Olsa 		 * we have the precise error.
1976b39b8393SJiri Olsa 		 */
1977b39b8393SJiri Olsa 		str     = "event syntax error: ";
1978b39b8393SJiri Olsa 		len_str = strlen(str);
1979b39b8393SJiri Olsa 		max_len = width - len_str;
1980b39b8393SJiri Olsa 
1981b39b8393SJiri Olsa 		buf = _buf;
1982b39b8393SJiri Olsa 
1983bd1a0be5SAdam Buchbinder 		/* We're cutting from the beginning. */
1984b39b8393SJiri Olsa 		if (err->idx > max_err_idx)
1985b39b8393SJiri Olsa 			cut = err->idx - max_err_idx;
1986b39b8393SJiri Olsa 
1987b39b8393SJiri Olsa 		strncpy(buf, event + cut, max_len);
1988b39b8393SJiri Olsa 
1989b39b8393SJiri Olsa 		/* Mark cut parts with '..' on both sides. */
1990b39b8393SJiri Olsa 		if (cut)
1991b39b8393SJiri Olsa 			buf[0] = buf[1] = '.';
1992b39b8393SJiri Olsa 
1993b39b8393SJiri Olsa 		if ((len_event - cut) > max_len) {
1994b39b8393SJiri Olsa 			buf[max_len - 1] = buf[max_len - 2] = '.';
1995b39b8393SJiri Olsa 			buf[max_len] = 0;
1996b39b8393SJiri Olsa 		}
1997b39b8393SJiri Olsa 
1998b39b8393SJiri Olsa 		idx = len_str + err->idx - cut;
1999b39b8393SJiri Olsa 	}
2000b39b8393SJiri Olsa 
2001b39b8393SJiri Olsa 	fprintf(stderr, "%s'%s'\n", str, buf);
2002b39b8393SJiri Olsa 	if (idx) {
2003b39b8393SJiri Olsa 		fprintf(stderr, "%*s\\___ %s\n", idx + 1, "", err->str);
2004b39b8393SJiri Olsa 		if (err->help)
2005b39b8393SJiri Olsa 			fprintf(stderr, "\n%s\n", err->help);
2006360e071bSTaeung Song 		zfree(&err->str);
2007360e071bSTaeung Song 		zfree(&err->help);
2008b39b8393SJiri Olsa 	}
2009b39b8393SJiri Olsa }
2010b39b8393SJiri Olsa 
2011b39b8393SJiri Olsa #undef MAX_WIDTH
2012b39b8393SJiri Olsa 
2013f120f9d5SJiri Olsa int parse_events_option(const struct option *opt, const char *str,
20141d037ca1SIrina Tirdea 			int unset __maybe_unused)
2015f120f9d5SJiri Olsa {
201663503dbaSJiri Olsa 	struct evlist *evlist = *(struct evlist **)opt->value;
2017b39b8393SJiri Olsa 	struct parse_events_error err = { .idx = 0, };
2018b39b8393SJiri Olsa 	int ret = parse_events(evlist, str, &err);
20199175ce1fSAndi Kleen 
2020333b5665SAndi Kleen 	if (ret) {
2021b39b8393SJiri Olsa 		parse_events_print_error(&err, str);
2022333b5665SAndi Kleen 		fprintf(stderr, "Run 'perf list' for a list of valid events\n");
2023333b5665SAndi Kleen 	}
2024b39b8393SJiri Olsa 
20259175ce1fSAndi Kleen 	return ret;
2026f120f9d5SJiri Olsa }
2027f120f9d5SJiri Olsa 
20284ba1faa1SWang Nan static int
202963503dbaSJiri Olsa foreach_evsel_in_last_glob(struct evlist *evlist,
203032dcd021SJiri Olsa 			   int (*func)(struct evsel *evsel,
20314ba1faa1SWang Nan 				       const void *arg),
20324ba1faa1SWang Nan 			   const void *arg)
2033c171b552SLi Zefan {
203432dcd021SJiri Olsa 	struct evsel *last = NULL;
20354ba1faa1SWang Nan 	int err;
2036c171b552SLi Zefan 
2037854f7363SWang Nan 	/*
2038854f7363SWang Nan 	 * Don't return when list_empty, give func a chance to report
2039854f7363SWang Nan 	 * error when it found last == NULL.
2040854f7363SWang Nan 	 *
2041854f7363SWang Nan 	 * So no need to WARN here, let *func do this.
2042854f7363SWang Nan 	 */
2043361c99a6SArnaldo Carvalho de Melo 	if (evlist->nr_entries > 0)
20440c21f736SArnaldo Carvalho de Melo 		last = perf_evlist__last(evlist);
204569aad6f1SArnaldo Carvalho de Melo 
204615bfd2ccSWang Nan 	do {
20474ba1faa1SWang Nan 		err = (*func)(last, arg);
20484ba1faa1SWang Nan 		if (err)
2049c171b552SLi Zefan 			return -1;
20504ba1faa1SWang Nan 		if (!last)
20514ba1faa1SWang Nan 			return 0;
2052c171b552SLi Zefan 
2053*b27c4eceSJiri Olsa 		if (last->core.node.prev == &evlist->entries)
205415bfd2ccSWang Nan 			return 0;
2055*b27c4eceSJiri Olsa 		last = list_entry(last->core.node.prev, struct evsel, core.node);
205615bfd2ccSWang Nan 	} while (!last->cmdline_group_boundary);
205715bfd2ccSWang Nan 
2058c171b552SLi Zefan 	return 0;
2059c171b552SLi Zefan }
2060c171b552SLi Zefan 
206132dcd021SJiri Olsa static int set_filter(struct evsel *evsel, const void *arg)
20624ba1faa1SWang Nan {
20634ba1faa1SWang Nan 	const char *str = arg;
20641e857484SMathieu Poirier 	bool found = false;
20651e857484SMathieu Poirier 	int nr_addr_filters = 0;
20661e857484SMathieu Poirier 	struct perf_pmu *pmu = NULL;
20674ba1faa1SWang Nan 
206849836f78SJack Henschel 	if (evsel == NULL) {
206949836f78SJack Henschel 		fprintf(stderr,
207049836f78SJack Henschel 			"--filter option should follow a -e tracepoint or HW tracer option\n");
207149836f78SJack Henschel 		return -1;
207249836f78SJack Henschel 	}
20734ba1faa1SWang Nan 
20741e857484SMathieu Poirier 	if (evsel->attr.type == PERF_TYPE_TRACEPOINT) {
20753541c034SMathieu Poirier 		if (perf_evsel__append_tp_filter(evsel, str) < 0) {
20764ba1faa1SWang Nan 			fprintf(stderr,
20774ba1faa1SWang Nan 				"not enough memory to hold filter string\n");
20784ba1faa1SWang Nan 			return -1;
20794ba1faa1SWang Nan 		}
20804ba1faa1SWang Nan 
20814ba1faa1SWang Nan 		return 0;
20824ba1faa1SWang Nan 	}
20834ba1faa1SWang Nan 
20841e857484SMathieu Poirier 	while ((pmu = perf_pmu__scan(pmu)) != NULL)
20851e857484SMathieu Poirier 		if (pmu->type == evsel->attr.type) {
20861e857484SMathieu Poirier 			found = true;
20871e857484SMathieu Poirier 			break;
20881e857484SMathieu Poirier 		}
20891e857484SMathieu Poirier 
20901e857484SMathieu Poirier 	if (found)
20911e857484SMathieu Poirier 		perf_pmu__scan_file(pmu, "nr_addr_filters",
20921e857484SMathieu Poirier 				    "%d", &nr_addr_filters);
20931e857484SMathieu Poirier 
209449836f78SJack Henschel 	if (!nr_addr_filters) {
209549836f78SJack Henschel 		fprintf(stderr,
209649836f78SJack Henschel 			"This CPU does not support address filtering\n");
209749836f78SJack Henschel 		return -1;
209849836f78SJack Henschel 	}
20991e857484SMathieu Poirier 
21001e857484SMathieu Poirier 	if (perf_evsel__append_addr_filter(evsel, str) < 0) {
21011e857484SMathieu Poirier 		fprintf(stderr,
21021e857484SMathieu Poirier 			"not enough memory to hold filter string\n");
21031e857484SMathieu Poirier 		return -1;
21041e857484SMathieu Poirier 	}
21051e857484SMathieu Poirier 
21061e857484SMathieu Poirier 	return 0;
21071e857484SMathieu Poirier }
21081e857484SMathieu Poirier 
21094ba1faa1SWang Nan int parse_filter(const struct option *opt, const char *str,
21104ba1faa1SWang Nan 		 int unset __maybe_unused)
21114ba1faa1SWang Nan {
211263503dbaSJiri Olsa 	struct evlist *evlist = *(struct evlist **)opt->value;
21134ba1faa1SWang Nan 
21144ba1faa1SWang Nan 	return foreach_evsel_in_last_glob(evlist, set_filter,
21154ba1faa1SWang Nan 					  (const void *)str);
21164ba1faa1SWang Nan }
21174ba1faa1SWang Nan 
211832dcd021SJiri Olsa static int add_exclude_perf_filter(struct evsel *evsel,
21194ba1faa1SWang Nan 				   const void *arg __maybe_unused)
21204ba1faa1SWang Nan {
21214ba1faa1SWang Nan 	char new_filter[64];
21224ba1faa1SWang Nan 
21234ba1faa1SWang Nan 	if (evsel == NULL || evsel->attr.type != PERF_TYPE_TRACEPOINT) {
21244ba1faa1SWang Nan 		fprintf(stderr,
21254ba1faa1SWang Nan 			"--exclude-perf option should follow a -e tracepoint option\n");
21264ba1faa1SWang Nan 		return -1;
21274ba1faa1SWang Nan 	}
21284ba1faa1SWang Nan 
21294ba1faa1SWang Nan 	snprintf(new_filter, sizeof(new_filter), "common_pid != %d", getpid());
21304ba1faa1SWang Nan 
21313541c034SMathieu Poirier 	if (perf_evsel__append_tp_filter(evsel, new_filter) < 0) {
21324ba1faa1SWang Nan 		fprintf(stderr,
21334ba1faa1SWang Nan 			"not enough memory to hold filter string\n");
21344ba1faa1SWang Nan 		return -1;
21354ba1faa1SWang Nan 	}
21364ba1faa1SWang Nan 
21374ba1faa1SWang Nan 	return 0;
21384ba1faa1SWang Nan }
21394ba1faa1SWang Nan 
21404ba1faa1SWang Nan int exclude_perf(const struct option *opt,
21414ba1faa1SWang Nan 		 const char *arg __maybe_unused,
21424ba1faa1SWang Nan 		 int unset __maybe_unused)
21434ba1faa1SWang Nan {
214463503dbaSJiri Olsa 	struct evlist *evlist = *(struct evlist **)opt->value;
21454ba1faa1SWang Nan 
21464ba1faa1SWang Nan 	return foreach_evsel_in_last_glob(evlist, add_exclude_perf_filter,
21474ba1faa1SWang Nan 					  NULL);
21484ba1faa1SWang Nan }
21494ba1faa1SWang Nan 
215086470930SIngo Molnar static const char * const event_type_descriptors[] = {
215186470930SIngo Molnar 	"Hardware event",
215286470930SIngo Molnar 	"Software event",
215386470930SIngo Molnar 	"Tracepoint event",
215486470930SIngo Molnar 	"Hardware cache event",
215541bdcb23SLiming Wang 	"Raw hardware event descriptor",
215641bdcb23SLiming Wang 	"Hardware breakpoint",
215786470930SIngo Molnar };
215886470930SIngo Molnar 
2159ab0e4800SYunlong Song static int cmp_string(const void *a, const void *b)
2160ab0e4800SYunlong Song {
2161ab0e4800SYunlong Song 	const char * const *as = a;
2162ab0e4800SYunlong Song 	const char * const *bs = b;
2163ab0e4800SYunlong Song 
2164ab0e4800SYunlong Song 	return strcmp(*as, *bs);
2165ab0e4800SYunlong Song }
2166ab0e4800SYunlong Song 
216786470930SIngo Molnar /*
2168f6bdafefSJason Baron  * Print the events from <debugfs_mount_point>/tracing/events
2169f6bdafefSJason Baron  */
2170f6bdafefSJason Baron 
2171a3277d2dSFrederic Weisbecker void print_tracepoint_events(const char *subsys_glob, const char *event_glob,
2172a3277d2dSFrederic Weisbecker 			     bool name_only)
2173f6bdafefSJason Baron {
2174f6bdafefSJason Baron 	DIR *sys_dir, *evt_dir;
217522a9f41bSArnaldo Carvalho de Melo 	struct dirent *sys_dirent, *evt_dirent;
2176f6bdafefSJason Baron 	char evt_path[MAXPATHLEN];
217725a7d914SArnaldo Carvalho de Melo 	char *dir_path;
2178ab0e4800SYunlong Song 	char **evt_list = NULL;
2179ab0e4800SYunlong Song 	unsigned int evt_i = 0, evt_num = 0;
2180ab0e4800SYunlong Song 	bool evt_num_known = false;
2181f6bdafefSJason Baron 
2182ab0e4800SYunlong Song restart:
21837014e0e3SArnaldo Carvalho de Melo 	sys_dir = tracing_events__opendir();
2184f6bdafefSJason Baron 	if (!sys_dir)
2185725b1368SEric Dumazet 		return;
2186f6bdafefSJason Baron 
2187ab0e4800SYunlong Song 	if (evt_num_known) {
2188ab0e4800SYunlong Song 		evt_list = zalloc(sizeof(char *) * evt_num);
2189ab0e4800SYunlong Song 		if (!evt_list)
2190ab0e4800SYunlong Song 			goto out_close_sys_dir;
2191ab0e4800SYunlong Song 	}
2192ab0e4800SYunlong Song 
219322a9f41bSArnaldo Carvalho de Melo 	for_each_subsystem(sys_dir, sys_dirent) {
2194668b8788SArnaldo Carvalho de Melo 		if (subsys_glob != NULL &&
219522a9f41bSArnaldo Carvalho de Melo 		    !strglobmatch(sys_dirent->d_name, subsys_glob))
2196668b8788SArnaldo Carvalho de Melo 			continue;
2197725b1368SEric Dumazet 
219825a7d914SArnaldo Carvalho de Melo 		dir_path = get_events_file(sys_dirent->d_name);
219925a7d914SArnaldo Carvalho de Melo 		if (!dir_path)
220025a7d914SArnaldo Carvalho de Melo 			continue;
2201725b1368SEric Dumazet 		evt_dir = opendir(dir_path);
2202725b1368SEric Dumazet 		if (!evt_dir)
220325a7d914SArnaldo Carvalho de Melo 			goto next;
2204725b1368SEric Dumazet 
2205c02cab22SArnaldo Carvalho de Melo 		for_each_event(dir_path, evt_dir, evt_dirent) {
2206668b8788SArnaldo Carvalho de Melo 			if (event_glob != NULL &&
220722a9f41bSArnaldo Carvalho de Melo 			    !strglobmatch(evt_dirent->d_name, event_glob))
2208668b8788SArnaldo Carvalho de Melo 				continue;
2209668b8788SArnaldo Carvalho de Melo 
2210ab0e4800SYunlong Song 			if (!evt_num_known) {
2211ab0e4800SYunlong Song 				evt_num++;
2212a3277d2dSFrederic Weisbecker 				continue;
2213a3277d2dSFrederic Weisbecker 			}
2214a3277d2dSFrederic Weisbecker 
2215f6bdafefSJason Baron 			snprintf(evt_path, MAXPATHLEN, "%s:%s",
221622a9f41bSArnaldo Carvalho de Melo 				 sys_dirent->d_name, evt_dirent->d_name);
2217ab0e4800SYunlong Song 
2218ab0e4800SYunlong Song 			evt_list[evt_i] = strdup(evt_path);
221925a7d914SArnaldo Carvalho de Melo 			if (evt_list[evt_i] == NULL) {
222025a7d914SArnaldo Carvalho de Melo 				put_events_file(dir_path);
2221ab0e4800SYunlong Song 				goto out_close_evt_dir;
222225a7d914SArnaldo Carvalho de Melo 			}
2223ab0e4800SYunlong Song 			evt_i++;
2224f6bdafefSJason Baron 		}
2225f6bdafefSJason Baron 		closedir(evt_dir);
222625a7d914SArnaldo Carvalho de Melo next:
222725a7d914SArnaldo Carvalho de Melo 		put_events_file(dir_path);
2228f6bdafefSJason Baron 	}
2229f6bdafefSJason Baron 	closedir(sys_dir);
2230ab0e4800SYunlong Song 
2231ab0e4800SYunlong Song 	if (!evt_num_known) {
2232ab0e4800SYunlong Song 		evt_num_known = true;
2233ab0e4800SYunlong Song 		goto restart;
2234ab0e4800SYunlong Song 	}
2235ab0e4800SYunlong Song 	qsort(evt_list, evt_num, sizeof(char *), cmp_string);
2236ab0e4800SYunlong Song 	evt_i = 0;
2237ab0e4800SYunlong Song 	while (evt_i < evt_num) {
2238ab0e4800SYunlong Song 		if (name_only) {
2239ab0e4800SYunlong Song 			printf("%s ", evt_list[evt_i++]);
2240ab0e4800SYunlong Song 			continue;
2241ab0e4800SYunlong Song 		}
2242ab0e4800SYunlong Song 		printf("  %-50s [%s]\n", evt_list[evt_i++],
2243ab0e4800SYunlong Song 				event_type_descriptors[PERF_TYPE_TRACEPOINT]);
2244ab0e4800SYunlong Song 	}
2245dfc431cbSArnaldo Carvalho de Melo 	if (evt_num && pager_in_use())
2246ab0e4800SYunlong Song 		printf("\n");
2247ab0e4800SYunlong Song 
2248ab0e4800SYunlong Song out_free:
2249ab0e4800SYunlong Song 	evt_num = evt_i;
2250ab0e4800SYunlong Song 	for (evt_i = 0; evt_i < evt_num; evt_i++)
2251ab0e4800SYunlong Song 		zfree(&evt_list[evt_i]);
2252ab0e4800SYunlong Song 	zfree(&evt_list);
2253ab0e4800SYunlong Song 	return;
2254ab0e4800SYunlong Song 
2255ab0e4800SYunlong Song out_close_evt_dir:
2256ab0e4800SYunlong Song 	closedir(evt_dir);
2257ab0e4800SYunlong Song out_close_sys_dir:
2258ab0e4800SYunlong Song 	closedir(sys_dir);
2259ab0e4800SYunlong Song 
2260ab0e4800SYunlong Song 	printf("FATAL: not enough memory to print %s\n",
2261ab0e4800SYunlong Song 			event_type_descriptors[PERF_TYPE_TRACEPOINT]);
2262ab0e4800SYunlong Song 	if (evt_list)
2263ab0e4800SYunlong Song 		goto out_free;
2264f6bdafefSJason Baron }
2265f6bdafefSJason Baron 
2266f6bdafefSJason Baron /*
226720c457b8SThomas Renninger  * Check whether event is in <debugfs_mount_point>/tracing/events
226820c457b8SThomas Renninger  */
226920c457b8SThomas Renninger 
227020c457b8SThomas Renninger int is_valid_tracepoint(const char *event_string)
227120c457b8SThomas Renninger {
227220c457b8SThomas Renninger 	DIR *sys_dir, *evt_dir;
227322a9f41bSArnaldo Carvalho de Melo 	struct dirent *sys_dirent, *evt_dirent;
227420c457b8SThomas Renninger 	char evt_path[MAXPATHLEN];
227525a7d914SArnaldo Carvalho de Melo 	char *dir_path;
227620c457b8SThomas Renninger 
22777014e0e3SArnaldo Carvalho de Melo 	sys_dir = tracing_events__opendir();
227820c457b8SThomas Renninger 	if (!sys_dir)
227920c457b8SThomas Renninger 		return 0;
228020c457b8SThomas Renninger 
228122a9f41bSArnaldo Carvalho de Melo 	for_each_subsystem(sys_dir, sys_dirent) {
228225a7d914SArnaldo Carvalho de Melo 		dir_path = get_events_file(sys_dirent->d_name);
228325a7d914SArnaldo Carvalho de Melo 		if (!dir_path)
228425a7d914SArnaldo Carvalho de Melo 			continue;
228520c457b8SThomas Renninger 		evt_dir = opendir(dir_path);
228620c457b8SThomas Renninger 		if (!evt_dir)
228725a7d914SArnaldo Carvalho de Melo 			goto next;
228820c457b8SThomas Renninger 
2289c02cab22SArnaldo Carvalho de Melo 		for_each_event(dir_path, evt_dir, evt_dirent) {
229020c457b8SThomas Renninger 			snprintf(evt_path, MAXPATHLEN, "%s:%s",
229122a9f41bSArnaldo Carvalho de Melo 				 sys_dirent->d_name, evt_dirent->d_name);
229220c457b8SThomas Renninger 			if (!strcmp(evt_path, event_string)) {
229320c457b8SThomas Renninger 				closedir(evt_dir);
229420c457b8SThomas Renninger 				closedir(sys_dir);
229520c457b8SThomas Renninger 				return 1;
229620c457b8SThomas Renninger 			}
229720c457b8SThomas Renninger 		}
229820c457b8SThomas Renninger 		closedir(evt_dir);
229925a7d914SArnaldo Carvalho de Melo next:
230025a7d914SArnaldo Carvalho de Melo 		put_events_file(dir_path);
230120c457b8SThomas Renninger 	}
230220c457b8SThomas Renninger 	closedir(sys_dir);
230320c457b8SThomas Renninger 	return 0;
230420c457b8SThomas Renninger }
230520c457b8SThomas Renninger 
2306b41f1cecSNamhyung Kim static bool is_event_supported(u8 type, unsigned config)
2307b41f1cecSNamhyung Kim {
2308b41f1cecSNamhyung Kim 	bool ret = true;
230988fee52eSVince Weaver 	int open_return;
231032dcd021SJiri Olsa 	struct evsel *evsel;
2311b41f1cecSNamhyung Kim 	struct perf_event_attr attr = {
2312b41f1cecSNamhyung Kim 		.type = type,
2313b41f1cecSNamhyung Kim 		.config = config,
2314b41f1cecSNamhyung Kim 		.disabled = 1,
2315b41f1cecSNamhyung Kim 	};
23169749b90eSJiri Olsa 	struct perf_thread_map *tmap = thread_map__new_by_tid(0);
231789896051SArnaldo Carvalho de Melo 
231889896051SArnaldo Carvalho de Melo 	if (tmap == NULL)
231989896051SArnaldo Carvalho de Melo 		return false;
2320b41f1cecSNamhyung Kim 
2321365c3ae7SJiri Olsa 	evsel = evsel__new(&attr);
2322b41f1cecSNamhyung Kim 	if (evsel) {
23235972d1e0SJiri Olsa 		open_return = evsel__open(evsel, NULL, tmap);
232488fee52eSVince Weaver 		ret = open_return >= 0;
232588fee52eSVince Weaver 
232688fee52eSVince Weaver 		if (open_return == -EACCES) {
232788fee52eSVince Weaver 			/*
232888fee52eSVince Weaver 			 * This happens if the paranoid value
232988fee52eSVince Weaver 			 * /proc/sys/kernel/perf_event_paranoid is set to 2
233088fee52eSVince Weaver 			 * Re-run with exclude_kernel set; we don't do that
233188fee52eSVince Weaver 			 * by default as some ARM machines do not support it.
233288fee52eSVince Weaver 			 *
233388fee52eSVince Weaver 			 */
233488fee52eSVince Weaver 			evsel->attr.exclude_kernel = 1;
23355972d1e0SJiri Olsa 			ret = evsel__open(evsel, NULL, tmap) >= 0;
233688fee52eSVince Weaver 		}
23375eb2dd2aSJiri Olsa 		evsel__delete(evsel);
2338b41f1cecSNamhyung Kim 	}
2339b41f1cecSNamhyung Kim 
23407836e52eSJiri Olsa 	perf_thread_map__put(tmap);
2341b41f1cecSNamhyung Kim 	return ret;
2342b41f1cecSNamhyung Kim }
2343b41f1cecSNamhyung Kim 
234440218daeSMasami Hiramatsu void print_sdt_events(const char *subsys_glob, const char *event_glob,
234540218daeSMasami Hiramatsu 		      bool name_only)
234640218daeSMasami Hiramatsu {
234740218daeSMasami Hiramatsu 	struct probe_cache *pcache;
234840218daeSMasami Hiramatsu 	struct probe_cache_entry *ent;
234940218daeSMasami Hiramatsu 	struct strlist *bidlist, *sdtlist;
235040218daeSMasami Hiramatsu 	struct strlist_config cfg = {.dont_dupstr = true};
235140218daeSMasami Hiramatsu 	struct str_node *nd, *nd2;
235240218daeSMasami Hiramatsu 	char *buf, *path, *ptr = NULL;
235340218daeSMasami Hiramatsu 	bool show_detail = false;
235440218daeSMasami Hiramatsu 	int ret;
235540218daeSMasami Hiramatsu 
235640218daeSMasami Hiramatsu 	sdtlist = strlist__new(NULL, &cfg);
235740218daeSMasami Hiramatsu 	if (!sdtlist) {
235840218daeSMasami Hiramatsu 		pr_debug("Failed to allocate new strlist for SDT\n");
235940218daeSMasami Hiramatsu 		return;
236040218daeSMasami Hiramatsu 	}
236140218daeSMasami Hiramatsu 	bidlist = build_id_cache__list_all(true);
236240218daeSMasami Hiramatsu 	if (!bidlist) {
236340218daeSMasami Hiramatsu 		pr_debug("Failed to get buildids: %d\n", errno);
236440218daeSMasami Hiramatsu 		return;
236540218daeSMasami Hiramatsu 	}
236640218daeSMasami Hiramatsu 	strlist__for_each_entry(nd, bidlist) {
2367f045b8c4SKrister Johansen 		pcache = probe_cache__new(nd->s, NULL);
236840218daeSMasami Hiramatsu 		if (!pcache)
236940218daeSMasami Hiramatsu 			continue;
237040218daeSMasami Hiramatsu 		list_for_each_entry(ent, &pcache->entries, node) {
237140218daeSMasami Hiramatsu 			if (!ent->sdt)
237240218daeSMasami Hiramatsu 				continue;
237340218daeSMasami Hiramatsu 			if (subsys_glob &&
237440218daeSMasami Hiramatsu 			    !strglobmatch(ent->pev.group, subsys_glob))
237540218daeSMasami Hiramatsu 				continue;
237640218daeSMasami Hiramatsu 			if (event_glob &&
237740218daeSMasami Hiramatsu 			    !strglobmatch(ent->pev.event, event_glob))
237840218daeSMasami Hiramatsu 				continue;
237940218daeSMasami Hiramatsu 			ret = asprintf(&buf, "%s:%s@%s", ent->pev.group,
238040218daeSMasami Hiramatsu 					ent->pev.event, nd->s);
238140218daeSMasami Hiramatsu 			if (ret > 0)
238240218daeSMasami Hiramatsu 				strlist__add(sdtlist, buf);
238340218daeSMasami Hiramatsu 		}
238440218daeSMasami Hiramatsu 		probe_cache__delete(pcache);
238540218daeSMasami Hiramatsu 	}
238640218daeSMasami Hiramatsu 	strlist__delete(bidlist);
238740218daeSMasami Hiramatsu 
238840218daeSMasami Hiramatsu 	strlist__for_each_entry(nd, sdtlist) {
238940218daeSMasami Hiramatsu 		buf = strchr(nd->s, '@');
239040218daeSMasami Hiramatsu 		if (buf)
239140218daeSMasami Hiramatsu 			*(buf++) = '\0';
239240218daeSMasami Hiramatsu 		if (name_only) {
239340218daeSMasami Hiramatsu 			printf("%s ", nd->s);
239440218daeSMasami Hiramatsu 			continue;
239540218daeSMasami Hiramatsu 		}
239640218daeSMasami Hiramatsu 		nd2 = strlist__next(nd);
239740218daeSMasami Hiramatsu 		if (nd2) {
239840218daeSMasami Hiramatsu 			ptr = strchr(nd2->s, '@');
239940218daeSMasami Hiramatsu 			if (ptr)
240040218daeSMasami Hiramatsu 				*ptr = '\0';
240140218daeSMasami Hiramatsu 			if (strcmp(nd->s, nd2->s) == 0)
240240218daeSMasami Hiramatsu 				show_detail = true;
240340218daeSMasami Hiramatsu 		}
240440218daeSMasami Hiramatsu 		if (show_detail) {
240540218daeSMasami Hiramatsu 			path = build_id_cache__origname(buf);
240640218daeSMasami Hiramatsu 			ret = asprintf(&buf, "%s@%s(%.12s)", nd->s, path, buf);
240740218daeSMasami Hiramatsu 			if (ret > 0) {
240840218daeSMasami Hiramatsu 				printf("  %-50s [%s]\n", buf, "SDT event");
240940218daeSMasami Hiramatsu 				free(buf);
241040218daeSMasami Hiramatsu 			}
24118bde8516SChangbin Du 			free(path);
241240218daeSMasami Hiramatsu 		} else
241340218daeSMasami Hiramatsu 			printf("  %-50s [%s]\n", nd->s, "SDT event");
241440218daeSMasami Hiramatsu 		if (nd2) {
241540218daeSMasami Hiramatsu 			if (strcmp(nd->s, nd2->s) != 0)
241640218daeSMasami Hiramatsu 				show_detail = false;
241740218daeSMasami Hiramatsu 			if (ptr)
241840218daeSMasami Hiramatsu 				*ptr = '@';
241940218daeSMasami Hiramatsu 		}
242040218daeSMasami Hiramatsu 	}
242140218daeSMasami Hiramatsu 	strlist__delete(sdtlist);
242240218daeSMasami Hiramatsu }
242340218daeSMasami Hiramatsu 
2424a3277d2dSFrederic Weisbecker int print_hwcache_events(const char *event_glob, bool name_only)
2425668b8788SArnaldo Carvalho de Melo {
2426ab0e4800SYunlong Song 	unsigned int type, op, i, evt_i = 0, evt_num = 0;
24270b668bc9SArnaldo Carvalho de Melo 	char name[64];
2428ab0e4800SYunlong Song 	char **evt_list = NULL;
2429ab0e4800SYunlong Song 	bool evt_num_known = false;
2430ab0e4800SYunlong Song 
2431ab0e4800SYunlong Song restart:
2432ab0e4800SYunlong Song 	if (evt_num_known) {
2433ab0e4800SYunlong Song 		evt_list = zalloc(sizeof(char *) * evt_num);
2434ab0e4800SYunlong Song 		if (!evt_list)
2435ab0e4800SYunlong Song 			goto out_enomem;
2436ab0e4800SYunlong Song 	}
2437668b8788SArnaldo Carvalho de Melo 
2438668b8788SArnaldo Carvalho de Melo 	for (type = 0; type < PERF_COUNT_HW_CACHE_MAX; type++) {
2439668b8788SArnaldo Carvalho de Melo 		for (op = 0; op < PERF_COUNT_HW_CACHE_OP_MAX; op++) {
2440668b8788SArnaldo Carvalho de Melo 			/* skip invalid cache type */
24410b668bc9SArnaldo Carvalho de Melo 			if (!perf_evsel__is_cache_op_valid(type, op))
2442668b8788SArnaldo Carvalho de Melo 				continue;
2443668b8788SArnaldo Carvalho de Melo 
2444668b8788SArnaldo Carvalho de Melo 			for (i = 0; i < PERF_COUNT_HW_CACHE_RESULT_MAX; i++) {
24450b668bc9SArnaldo Carvalho de Melo 				__perf_evsel__hw_cache_type_op_res_name(type, op, i,
24460b668bc9SArnaldo Carvalho de Melo 									name, sizeof(name));
2447947b4ad1SIngo Molnar 				if (event_glob != NULL && !strglobmatch(name, event_glob))
2448668b8788SArnaldo Carvalho de Melo 					continue;
2449668b8788SArnaldo Carvalho de Melo 
2450b41f1cecSNamhyung Kim 				if (!is_event_supported(PERF_TYPE_HW_CACHE,
2451b41f1cecSNamhyung Kim 							type | (op << 8) | (i << 16)))
2452b41f1cecSNamhyung Kim 					continue;
2453b41f1cecSNamhyung Kim 
2454ab0e4800SYunlong Song 				if (!evt_num_known) {
2455ab0e4800SYunlong Song 					evt_num++;
2456ab0e4800SYunlong Song 					continue;
2457ab0e4800SYunlong Song 				}
2458ab0e4800SYunlong Song 
2459ab0e4800SYunlong Song 				evt_list[evt_i] = strdup(name);
2460ab0e4800SYunlong Song 				if (evt_list[evt_i] == NULL)
2461ab0e4800SYunlong Song 					goto out_enomem;
2462ab0e4800SYunlong Song 				evt_i++;
2463668b8788SArnaldo Carvalho de Melo 			}
2464668b8788SArnaldo Carvalho de Melo 		}
2465668b8788SArnaldo Carvalho de Melo 	}
2466668b8788SArnaldo Carvalho de Melo 
2467ab0e4800SYunlong Song 	if (!evt_num_known) {
2468ab0e4800SYunlong Song 		evt_num_known = true;
2469ab0e4800SYunlong Song 		goto restart;
2470ab0e4800SYunlong Song 	}
2471ab0e4800SYunlong Song 	qsort(evt_list, evt_num, sizeof(char *), cmp_string);
2472ab0e4800SYunlong Song 	evt_i = 0;
2473ab0e4800SYunlong Song 	while (evt_i < evt_num) {
2474ab0e4800SYunlong Song 		if (name_only) {
2475ab0e4800SYunlong Song 			printf("%s ", evt_list[evt_i++]);
2476ab0e4800SYunlong Song 			continue;
2477ab0e4800SYunlong Song 		}
2478ab0e4800SYunlong Song 		printf("  %-50s [%s]\n", evt_list[evt_i++],
2479ab0e4800SYunlong Song 				event_type_descriptors[PERF_TYPE_HW_CACHE]);
2480ab0e4800SYunlong Song 	}
2481dfc431cbSArnaldo Carvalho de Melo 	if (evt_num && pager_in_use())
2482dc098b35SAndi Kleen 		printf("\n");
2483ab0e4800SYunlong Song 
2484ab0e4800SYunlong Song out_free:
2485ab0e4800SYunlong Song 	evt_num = evt_i;
2486ab0e4800SYunlong Song 	for (evt_i = 0; evt_i < evt_num; evt_i++)
2487ab0e4800SYunlong Song 		zfree(&evt_list[evt_i]);
2488ab0e4800SYunlong Song 	zfree(&evt_list);
2489ab0e4800SYunlong Song 	return evt_num;
2490ab0e4800SYunlong Song 
2491ab0e4800SYunlong Song out_enomem:
2492ab0e4800SYunlong Song 	printf("FATAL: not enough memory to print %s\n", event_type_descriptors[PERF_TYPE_HW_CACHE]);
2493ab0e4800SYunlong Song 	if (evt_list)
2494ab0e4800SYunlong Song 		goto out_free;
2495ab0e4800SYunlong Song 	return evt_num;
2496668b8788SArnaldo Carvalho de Melo }
2497668b8788SArnaldo Carvalho de Melo 
24985e0861baSAndi Kleen static void print_tool_event(const char *name, const char *event_glob,
24995e0861baSAndi Kleen 			     bool name_only)
25005e0861baSAndi Kleen {
25015e0861baSAndi Kleen 	if (event_glob && !strglobmatch(name, event_glob))
25025e0861baSAndi Kleen 		return;
25035e0861baSAndi Kleen 	if (name_only)
25045e0861baSAndi Kleen 		printf("%s ", name);
25055e0861baSAndi Kleen 	else
25065e0861baSAndi Kleen 		printf("  %-50s [%s]\n", name, "Tool event");
25075e0861baSAndi Kleen 
25085e0861baSAndi Kleen }
25095e0861baSAndi Kleen 
25105e0861baSAndi Kleen void print_tool_events(const char *event_glob, bool name_only)
25115e0861baSAndi Kleen {
25125e0861baSAndi Kleen 	print_tool_event("duration_time", event_glob, name_only);
25135e0861baSAndi Kleen 	if (pager_in_use())
25145e0861baSAndi Kleen 		printf("\n");
25155e0861baSAndi Kleen }
25165e0861baSAndi Kleen 
2517705750f2SYunlong Song void print_symbol_events(const char *event_glob, unsigned type,
2518a3277d2dSFrederic Weisbecker 				struct event_symbol *syms, unsigned max,
2519a3277d2dSFrederic Weisbecker 				bool name_only)
252086470930SIngo Molnar {
2521ab0e4800SYunlong Song 	unsigned int i, evt_i = 0, evt_num = 0;
2522947b4ad1SIngo Molnar 	char name[MAX_NAME_LEN];
2523ab0e4800SYunlong Song 	char **evt_list = NULL;
2524ab0e4800SYunlong Song 	bool evt_num_known = false;
2525ab0e4800SYunlong Song 
2526ab0e4800SYunlong Song restart:
2527ab0e4800SYunlong Song 	if (evt_num_known) {
2528ab0e4800SYunlong Song 		evt_list = zalloc(sizeof(char *) * evt_num);
2529ab0e4800SYunlong Song 		if (!evt_list)
2530ab0e4800SYunlong Song 			goto out_enomem;
2531ab0e4800SYunlong Song 		syms -= max;
2532ab0e4800SYunlong Song 	}
253386470930SIngo Molnar 
25341dc12760SJiri Olsa 	for (i = 0; i < max; i++, syms++) {
2535668b8788SArnaldo Carvalho de Melo 
2536e37df6c7SArnaldo Carvalho de Melo 		if (event_glob != NULL && syms->symbol != NULL &&
2537668b8788SArnaldo Carvalho de Melo 		    !(strglobmatch(syms->symbol, event_glob) ||
2538668b8788SArnaldo Carvalho de Melo 		      (syms->alias && strglobmatch(syms->alias, event_glob))))
2539668b8788SArnaldo Carvalho de Melo 			continue;
254086470930SIngo Molnar 
2541b41f1cecSNamhyung Kim 		if (!is_event_supported(type, i))
2542b41f1cecSNamhyung Kim 			continue;
2543b41f1cecSNamhyung Kim 
2544ab0e4800SYunlong Song 		if (!evt_num_known) {
2545ab0e4800SYunlong Song 			evt_num++;
2546a3277d2dSFrederic Weisbecker 			continue;
2547a3277d2dSFrederic Weisbecker 		}
2548a3277d2dSFrederic Weisbecker 
2549ab0e4800SYunlong Song 		if (!name_only && strlen(syms->alias))
2550947b4ad1SIngo Molnar 			snprintf(name, MAX_NAME_LEN, "%s OR %s", syms->symbol, syms->alias);
255174d5b588SJaswinder Singh Rajput 		else
2552bd8d57fbSArnaldo Carvalho de Melo 			strlcpy(name, syms->symbol, MAX_NAME_LEN);
255386470930SIngo Molnar 
2554ab0e4800SYunlong Song 		evt_list[evt_i] = strdup(name);
2555ab0e4800SYunlong Song 		if (evt_list[evt_i] == NULL)
2556ab0e4800SYunlong Song 			goto out_enomem;
2557ab0e4800SYunlong Song 		evt_i++;
255886470930SIngo Molnar 	}
255986470930SIngo Molnar 
2560ab0e4800SYunlong Song 	if (!evt_num_known) {
2561ab0e4800SYunlong Song 		evt_num_known = true;
2562ab0e4800SYunlong Song 		goto restart;
2563ab0e4800SYunlong Song 	}
2564ab0e4800SYunlong Song 	qsort(evt_list, evt_num, sizeof(char *), cmp_string);
2565ab0e4800SYunlong Song 	evt_i = 0;
2566ab0e4800SYunlong Song 	while (evt_i < evt_num) {
2567ab0e4800SYunlong Song 		if (name_only) {
2568ab0e4800SYunlong Song 			printf("%s ", evt_list[evt_i++]);
2569ab0e4800SYunlong Song 			continue;
2570ab0e4800SYunlong Song 		}
2571ab0e4800SYunlong Song 		printf("  %-50s [%s]\n", evt_list[evt_i++], event_type_descriptors[type]);
2572ab0e4800SYunlong Song 	}
2573dfc431cbSArnaldo Carvalho de Melo 	if (evt_num && pager_in_use())
2574689d3018SMarti Raudsepp 		printf("\n");
2575ab0e4800SYunlong Song 
2576ab0e4800SYunlong Song out_free:
2577ab0e4800SYunlong Song 	evt_num = evt_i;
2578ab0e4800SYunlong Song 	for (evt_i = 0; evt_i < evt_num; evt_i++)
2579ab0e4800SYunlong Song 		zfree(&evt_list[evt_i]);
2580ab0e4800SYunlong Song 	zfree(&evt_list);
2581ab0e4800SYunlong Song 	return;
2582ab0e4800SYunlong Song 
2583ab0e4800SYunlong Song out_enomem:
2584ab0e4800SYunlong Song 	printf("FATAL: not enough memory to print %s\n", event_type_descriptors[type]);
2585ab0e4800SYunlong Song 	if (evt_list)
2586ab0e4800SYunlong Song 		goto out_free;
2587668b8788SArnaldo Carvalho de Melo }
25881dc12760SJiri Olsa 
25891dc12760SJiri Olsa /*
25901dc12760SJiri Olsa  * Print the help text for the event symbols:
25911dc12760SJiri Olsa  */
2592c8d6828aSSukadev Bhattiprolu void print_events(const char *event_glob, bool name_only, bool quiet_flag,
2593bf874fcfSAndi Kleen 			bool long_desc, bool details_flag)
25941dc12760SJiri Olsa {
25951dc12760SJiri Olsa 	print_symbol_events(event_glob, PERF_TYPE_HARDWARE,
2596a3277d2dSFrederic Weisbecker 			    event_symbols_hw, PERF_COUNT_HW_MAX, name_only);
25971dc12760SJiri Olsa 
25981dc12760SJiri Olsa 	print_symbol_events(event_glob, PERF_TYPE_SOFTWARE,
2599a3277d2dSFrederic Weisbecker 			    event_symbols_sw, PERF_COUNT_SW_MAX, name_only);
26005e0861baSAndi Kleen 	print_tool_events(event_glob, name_only);
26011dc12760SJiri Olsa 
2602a3277d2dSFrederic Weisbecker 	print_hwcache_events(event_glob, name_only);
260373c24cb8SJaswinder Singh Rajput 
2604bf874fcfSAndi Kleen 	print_pmu_events(event_glob, name_only, quiet_flag, long_desc,
2605bf874fcfSAndi Kleen 			details_flag);
2606dc098b35SAndi Kleen 
2607668b8788SArnaldo Carvalho de Melo 	if (event_glob != NULL)
2608668b8788SArnaldo Carvalho de Melo 		return;
260973c24cb8SJaswinder Singh Rajput 
2610a3277d2dSFrederic Weisbecker 	if (!name_only) {
2611947b4ad1SIngo Molnar 		printf("  %-50s [%s]\n",
26125f537a26SJiri Olsa 		       "rNNN",
26131cf4a063SArnaldo Carvalho de Melo 		       event_type_descriptors[PERF_TYPE_RAW]);
26145f537a26SJiri Olsa 		printf("  %-50s [%s]\n",
26155f537a26SJiri Olsa 		       "cpu/t1=v1[,t2=v2,t3 ...]/modifier",
26165f537a26SJiri Olsa 		       event_type_descriptors[PERF_TYPE_RAW]);
2617dfc431cbSArnaldo Carvalho de Melo 		if (pager_in_use())
2618dfc431cbSArnaldo Carvalho de Melo 			printf("   (see 'man perf-list' on how to encode it)\n\n");
261986470930SIngo Molnar 
2620947b4ad1SIngo Molnar 		printf("  %-50s [%s]\n",
26213741eb9fSJacob Shin 		       "mem:<addr>[/len][:access]",
262241bdcb23SLiming Wang 			event_type_descriptors[PERF_TYPE_BREAKPOINT]);
2623dfc431cbSArnaldo Carvalho de Melo 		if (pager_in_use())
26241b290d67SFrederic Weisbecker 			printf("\n");
2625a3277d2dSFrederic Weisbecker 	}
26261b290d67SFrederic Weisbecker 
2627a3277d2dSFrederic Weisbecker 	print_tracepoint_events(NULL, NULL, name_only);
262840218daeSMasami Hiramatsu 
262940218daeSMasami Hiramatsu 	print_sdt_events(NULL, NULL, name_only);
263071b0acceSAndi Kleen 
263133bbc571SJiri Olsa 	metricgroup__print(true, true, NULL, name_only, details_flag);
263286470930SIngo Molnar }
26338f707d84SJiri Olsa 
26346cee6cd3SArnaldo Carvalho de Melo int parse_events__is_hardcoded_term(struct parse_events_term *term)
26358f707d84SJiri Olsa {
263616fa7e82SJiri Olsa 	return term->type_term != PARSE_EVENTS__TERM_TYPE_USER;
26378f707d84SJiri Olsa }
26388f707d84SJiri Olsa 
263967b49b38SJiri Olsa static int new_term(struct parse_events_term **_term,
264067b49b38SJiri Olsa 		    struct parse_events_term *temp,
264167b49b38SJiri Olsa 		    char *str, u64 num)
26428f707d84SJiri Olsa {
26436cee6cd3SArnaldo Carvalho de Melo 	struct parse_events_term *term;
26448f707d84SJiri Olsa 
264567b49b38SJiri Olsa 	term = malloc(sizeof(*term));
26468f707d84SJiri Olsa 	if (!term)
26478f707d84SJiri Olsa 		return -ENOMEM;
26488f707d84SJiri Olsa 
264967b49b38SJiri Olsa 	*term = *temp;
26508f707d84SJiri Olsa 	INIT_LIST_HEAD(&term->list);
2651c2f1ceadSAndi Kleen 	term->weak = false;
26528f707d84SJiri Olsa 
265367b49b38SJiri Olsa 	switch (term->type_val) {
26548f707d84SJiri Olsa 	case PARSE_EVENTS__TERM_TYPE_NUM:
26558f707d84SJiri Olsa 		term->val.num = num;
26568f707d84SJiri Olsa 		break;
26578f707d84SJiri Olsa 	case PARSE_EVENTS__TERM_TYPE_STR:
26588f707d84SJiri Olsa 		term->val.str = str;
26598f707d84SJiri Olsa 		break;
26608f707d84SJiri Olsa 	default:
26614be8be6bSAdrian Hunter 		free(term);
26628f707d84SJiri Olsa 		return -EINVAL;
26638f707d84SJiri Olsa 	}
26648f707d84SJiri Olsa 
26658f707d84SJiri Olsa 	*_term = term;
26668f707d84SJiri Olsa 	return 0;
26678f707d84SJiri Olsa }
26688f707d84SJiri Olsa 
26696cee6cd3SArnaldo Carvalho de Melo int parse_events_term__num(struct parse_events_term **term,
2670cecf3a2eSJiri Olsa 			   int type_term, char *config, u64 num,
267199e7138eSJiri Olsa 			   bool no_value,
2672bb78ce7dSAdrian Hunter 			   void *loc_term_, void *loc_val_)
267316fa7e82SJiri Olsa {
2674bb78ce7dSAdrian Hunter 	YYLTYPE *loc_term = loc_term_;
2675bb78ce7dSAdrian Hunter 	YYLTYPE *loc_val = loc_val_;
2676bb78ce7dSAdrian Hunter 
267767b49b38SJiri Olsa 	struct parse_events_term temp = {
267867b49b38SJiri Olsa 		.type_val  = PARSE_EVENTS__TERM_TYPE_NUM,
267967b49b38SJiri Olsa 		.type_term = type_term,
268067b49b38SJiri Olsa 		.config    = config,
268199e7138eSJiri Olsa 		.no_value  = no_value,
268267b49b38SJiri Olsa 		.err_term  = loc_term ? loc_term->first_column : 0,
268367b49b38SJiri Olsa 		.err_val   = loc_val  ? loc_val->first_column  : 0,
268467b49b38SJiri Olsa 	};
268567b49b38SJiri Olsa 
268667b49b38SJiri Olsa 	return new_term(term, &temp, NULL, num);
268716fa7e82SJiri Olsa }
268816fa7e82SJiri Olsa 
26896cee6cd3SArnaldo Carvalho de Melo int parse_events_term__str(struct parse_events_term **term,
2690cecf3a2eSJiri Olsa 			   int type_term, char *config, char *str,
2691bb78ce7dSAdrian Hunter 			   void *loc_term_, void *loc_val_)
269216fa7e82SJiri Olsa {
2693bb78ce7dSAdrian Hunter 	YYLTYPE *loc_term = loc_term_;
2694bb78ce7dSAdrian Hunter 	YYLTYPE *loc_val = loc_val_;
2695bb78ce7dSAdrian Hunter 
269667b49b38SJiri Olsa 	struct parse_events_term temp = {
269767b49b38SJiri Olsa 		.type_val  = PARSE_EVENTS__TERM_TYPE_STR,
269867b49b38SJiri Olsa 		.type_term = type_term,
269967b49b38SJiri Olsa 		.config    = config,
270067b49b38SJiri Olsa 		.err_term  = loc_term ? loc_term->first_column : 0,
270167b49b38SJiri Olsa 		.err_val   = loc_val  ? loc_val->first_column  : 0,
270267b49b38SJiri Olsa 	};
270367b49b38SJiri Olsa 
270467b49b38SJiri Olsa 	return new_term(term, &temp, str, 0);
270516fa7e82SJiri Olsa }
270616fa7e82SJiri Olsa 
27076cee6cd3SArnaldo Carvalho de Melo int parse_events_term__sym_hw(struct parse_events_term **term,
27081d33d6dcSJiri Olsa 			      char *config, unsigned idx)
27091d33d6dcSJiri Olsa {
27101d33d6dcSJiri Olsa 	struct event_symbol *sym;
271167b49b38SJiri Olsa 	struct parse_events_term temp = {
271267b49b38SJiri Olsa 		.type_val  = PARSE_EVENTS__TERM_TYPE_STR,
271367b49b38SJiri Olsa 		.type_term = PARSE_EVENTS__TERM_TYPE_USER,
271467b49b38SJiri Olsa 		.config    = config ?: (char *) "event",
271567b49b38SJiri Olsa 	};
27161d33d6dcSJiri Olsa 
27171d33d6dcSJiri Olsa 	BUG_ON(idx >= PERF_COUNT_HW_MAX);
27181d33d6dcSJiri Olsa 	sym = &event_symbols_hw[idx];
27191d33d6dcSJiri Olsa 
272067b49b38SJiri Olsa 	return new_term(term, &temp, (char *) sym->symbol, 0);
27211d33d6dcSJiri Olsa }
27221d33d6dcSJiri Olsa 
27236cee6cd3SArnaldo Carvalho de Melo int parse_events_term__clone(struct parse_events_term **new,
27246cee6cd3SArnaldo Carvalho de Melo 			     struct parse_events_term *term)
2725a6146d50SZheng Yan {
272667b49b38SJiri Olsa 	struct parse_events_term temp = {
272767b49b38SJiri Olsa 		.type_val  = term->type_val,
272867b49b38SJiri Olsa 		.type_term = term->type_term,
272967b49b38SJiri Olsa 		.config    = term->config,
273067b49b38SJiri Olsa 		.err_term  = term->err_term,
273167b49b38SJiri Olsa 		.err_val   = term->err_val,
273267b49b38SJiri Olsa 	};
273367b49b38SJiri Olsa 
273467b49b38SJiri Olsa 	return new_term(new, &temp, term->val.str, term->val.num);
2735a6146d50SZheng Yan }
2736a6146d50SZheng Yan 
27378255718fSAndi Kleen int parse_events_copy_term_list(struct list_head *old,
27388255718fSAndi Kleen 				 struct list_head **new)
27398255718fSAndi Kleen {
27408255718fSAndi Kleen 	struct parse_events_term *term, *n;
27418255718fSAndi Kleen 	int ret;
27428255718fSAndi Kleen 
27438255718fSAndi Kleen 	if (!old) {
27448255718fSAndi Kleen 		*new = NULL;
27458255718fSAndi Kleen 		return 0;
27468255718fSAndi Kleen 	}
27478255718fSAndi Kleen 
27488255718fSAndi Kleen 	*new = malloc(sizeof(struct list_head));
27498255718fSAndi Kleen 	if (!*new)
27508255718fSAndi Kleen 		return -ENOMEM;
27518255718fSAndi Kleen 	INIT_LIST_HEAD(*new);
27528255718fSAndi Kleen 
27538255718fSAndi Kleen 	list_for_each_entry (term, old, list) {
27548255718fSAndi Kleen 		ret = parse_events_term__clone(&n, term);
27558255718fSAndi Kleen 		if (ret)
27568255718fSAndi Kleen 			return ret;
27578255718fSAndi Kleen 		list_add_tail(&n->list, *new);
27588255718fSAndi Kleen 	}
27598255718fSAndi Kleen 	return 0;
27608255718fSAndi Kleen }
27618255718fSAndi Kleen 
2762fc0a2c1dSArnaldo Carvalho de Melo void parse_events_terms__purge(struct list_head *terms)
27638f707d84SJiri Olsa {
27646cee6cd3SArnaldo Carvalho de Melo 	struct parse_events_term *term, *h;
27658f707d84SJiri Olsa 
2766a8adfcebSWang Nan 	list_for_each_entry_safe(term, h, terms, list) {
27672d055bf2SWang Nan 		if (term->array.nr_ranges)
2768360e071bSTaeung Song 			zfree(&term->array.ranges);
2769a8adfcebSWang Nan 		list_del_init(&term->list);
27708f707d84SJiri Olsa 		free(term);
27718f707d84SJiri Olsa 	}
2772a8adfcebSWang Nan }
2773b39b8393SJiri Olsa 
27742146afc6SArnaldo Carvalho de Melo void parse_events_terms__delete(struct list_head *terms)
2775fc0a2c1dSArnaldo Carvalho de Melo {
27762146afc6SArnaldo Carvalho de Melo 	if (!terms)
27772146afc6SArnaldo Carvalho de Melo 		return;
2778fc0a2c1dSArnaldo Carvalho de Melo 	parse_events_terms__purge(terms);
2779d20a5f2bSWang Nan 	free(terms);
2780fc0a2c1dSArnaldo Carvalho de Melo }
2781fc0a2c1dSArnaldo Carvalho de Melo 
27822d055bf2SWang Nan void parse_events__clear_array(struct parse_events_array *a)
27832d055bf2SWang Nan {
2784360e071bSTaeung Song 	zfree(&a->ranges);
27852d055bf2SWang Nan }
27862d055bf2SWang Nan 
27875d9cdc11SArnaldo Carvalho de Melo void parse_events_evlist_error(struct parse_events_state *parse_state,
2788b39b8393SJiri Olsa 			       int idx, const char *str)
2789b39b8393SJiri Olsa {
27905d9cdc11SArnaldo Carvalho de Melo 	struct parse_events_error *err = parse_state->error;
2791b39b8393SJiri Olsa 
2792a6ced2beSAdrian Hunter 	if (!err)
2793a6ced2beSAdrian Hunter 		return;
2794b39b8393SJiri Olsa 	err->idx = idx;
2795b39b8393SJiri Olsa 	err->str = strdup(str);
2796b39b8393SJiri Olsa 	WARN_ONCE(!err->str, "WARNING: failed to allocate error string");
2797b39b8393SJiri Olsa }
2798ffeb883eSHe Kuang 
279917cb5f84SWang Nan static void config_terms_list(char *buf, size_t buf_sz)
280017cb5f84SWang Nan {
280117cb5f84SWang Nan 	int i;
280217cb5f84SWang Nan 	bool first = true;
280317cb5f84SWang Nan 
280417cb5f84SWang Nan 	buf[0] = '\0';
280517cb5f84SWang Nan 	for (i = 0; i < __PARSE_EVENTS__TERM_TYPE_NR; i++) {
280617cb5f84SWang Nan 		const char *name = config_term_names[i];
280717cb5f84SWang Nan 
28081669e509SWang Nan 		if (!config_term_avail(i, NULL))
28091669e509SWang Nan 			continue;
281017cb5f84SWang Nan 		if (!name)
281117cb5f84SWang Nan 			continue;
281217cb5f84SWang Nan 		if (name[0] == '<')
281317cb5f84SWang Nan 			continue;
281417cb5f84SWang Nan 
281517cb5f84SWang Nan 		if (strlen(buf) + strlen(name) + 2 >= buf_sz)
281617cb5f84SWang Nan 			return;
281717cb5f84SWang Nan 
281817cb5f84SWang Nan 		if (!first)
281917cb5f84SWang Nan 			strcat(buf, ",");
282017cb5f84SWang Nan 		else
282117cb5f84SWang Nan 			first = false;
282217cb5f84SWang Nan 		strcat(buf, name);
282317cb5f84SWang Nan 	}
282417cb5f84SWang Nan }
282517cb5f84SWang Nan 
2826ffeb883eSHe Kuang /*
2827ffeb883eSHe Kuang  * Return string contains valid config terms of an event.
2828ffeb883eSHe Kuang  * @additional_terms: For terms such as PMU sysfs terms.
2829ffeb883eSHe Kuang  */
2830ffeb883eSHe Kuang char *parse_events_formats_error_string(char *additional_terms)
2831ffeb883eSHe Kuang {
2832ffeb883eSHe Kuang 	char *str;
2833626a6b78SWang Nan 	/* "no-overwrite" is the longest name */
283417cb5f84SWang Nan 	char static_terms[__PARSE_EVENTS__TERM_TYPE_NR *
2835626a6b78SWang Nan 			  (sizeof("no-overwrite") - 1)];
2836ffeb883eSHe Kuang 
283717cb5f84SWang Nan 	config_terms_list(static_terms, sizeof(static_terms));
2838ffeb883eSHe Kuang 	/* valid terms */
2839ffeb883eSHe Kuang 	if (additional_terms) {
284026dee028SWang Nan 		if (asprintf(&str, "valid terms: %s,%s",
284126dee028SWang Nan 			     additional_terms, static_terms) < 0)
2842ffeb883eSHe Kuang 			goto fail;
2843ffeb883eSHe Kuang 	} else {
284426dee028SWang Nan 		if (asprintf(&str, "valid terms: %s", static_terms) < 0)
2845ffeb883eSHe Kuang 			goto fail;
2846ffeb883eSHe Kuang 	}
2847ffeb883eSHe Kuang 	return str;
2848ffeb883eSHe Kuang 
2849ffeb883eSHe Kuang fail:
2850ffeb883eSHe Kuang 	return NULL;
2851ffeb883eSHe Kuang }
2852