xref: /linux/tools/perf/util/parse-events.c (revision 8e8714c3d157568b7a769917a5e05573bbaf5af0)
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"
134a3cec84SArnaldo Carvalho de Melo #include "build-id.h"
14361c99a6SArnaldo Carvalho de Melo #include "evlist.h"
1569aad6f1SArnaldo Carvalho de Melo #include "evsel.h"
16fa0d9846SArnaldo Carvalho de Melo #include <subcmd/pager.h>
174b6ab94eSJosh Poimboeuf #include <subcmd/parse-options.h>
1886470930SIngo Molnar #include "parse-events.h"
194b6ab94eSJosh Poimboeuf #include <subcmd/exec-cmd.h>
20a067558eSArnaldo Carvalho de Melo #include "string2.h"
218ec20b17SArnaldo Carvalho de Melo #include "strlist.h"
225aab621bSArnaldo Carvalho de Melo #include "symbol.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>
279c3516d1SJiri Olsa #include <perf/cpumap.h>
28ac20de6fSZheng Yan #include "parse-events-bison.h"
2990e2b22dSJiri Olsa #define YY_EXTRA_TYPE int
3089812fc8SJiri Olsa #include "parse-events-flex.h"
315f537a26SJiri Olsa #include "pmu.h"
32b41f1cecSNamhyung Kim #include "thread_map.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"
3795be9d19SArnaldo Carvalho de Melo #include "util/evsel_config.h"
38252a2fdcSArnaldo Carvalho de Melo #include "util/event.h"
3989812fc8SJiri Olsa 
4089812fc8SJiri Olsa #define MAX_NAME_LEN 100
4186470930SIngo Molnar 
4282ba1f2fSJiri Olsa #ifdef PARSER_DEBUG
4382ba1f2fSJiri Olsa extern int parse_events_debug;
4482ba1f2fSJiri Olsa #endif
455d9cdc11SArnaldo Carvalho de Melo int parse_events_parse(void *parse_state, void *scanner);
46e637d177SHe Kuang static int get_config_terms(struct list_head *head_config,
47e637d177SHe Kuang 			    struct list_head *head_terms __maybe_unused);
48bcd3279fSFrederic Weisbecker 
49dcb4e102SKan Liang static struct perf_pmu_event_symbol *perf_pmu_events_list;
50dcb4e102SKan Liang /*
51dcb4e102SKan Liang  * The variable indicates the number of supported pmu event symbols.
52dcb4e102SKan Liang  * 0 means not initialized and ready to init
53dcb4e102SKan Liang  * -1 means failed to init, don't try anymore
54dcb4e102SKan Liang  * >0 is the number of supported pmu event symbols
55dcb4e102SKan Liang  */
56dcb4e102SKan Liang static int perf_pmu_events_list_num;
57dcb4e102SKan Liang 
58705750f2SYunlong Song struct event_symbol event_symbols_hw[PERF_COUNT_HW_MAX] = {
591dc12760SJiri Olsa 	[PERF_COUNT_HW_CPU_CYCLES] = {
601dc12760SJiri Olsa 		.symbol = "cpu-cycles",
611dc12760SJiri Olsa 		.alias  = "cycles",
621dc12760SJiri Olsa 	},
631dc12760SJiri Olsa 	[PERF_COUNT_HW_INSTRUCTIONS] = {
641dc12760SJiri Olsa 		.symbol = "instructions",
651dc12760SJiri Olsa 		.alias  = "",
661dc12760SJiri Olsa 	},
671dc12760SJiri Olsa 	[PERF_COUNT_HW_CACHE_REFERENCES] = {
681dc12760SJiri Olsa 		.symbol = "cache-references",
691dc12760SJiri Olsa 		.alias  = "",
701dc12760SJiri Olsa 	},
711dc12760SJiri Olsa 	[PERF_COUNT_HW_CACHE_MISSES] = {
721dc12760SJiri Olsa 		.symbol = "cache-misses",
731dc12760SJiri Olsa 		.alias  = "",
741dc12760SJiri Olsa 	},
751dc12760SJiri Olsa 	[PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = {
761dc12760SJiri Olsa 		.symbol = "branch-instructions",
771dc12760SJiri Olsa 		.alias  = "branches",
781dc12760SJiri Olsa 	},
791dc12760SJiri Olsa 	[PERF_COUNT_HW_BRANCH_MISSES] = {
801dc12760SJiri Olsa 		.symbol = "branch-misses",
811dc12760SJiri Olsa 		.alias  = "",
821dc12760SJiri Olsa 	},
831dc12760SJiri Olsa 	[PERF_COUNT_HW_BUS_CYCLES] = {
841dc12760SJiri Olsa 		.symbol = "bus-cycles",
851dc12760SJiri Olsa 		.alias  = "",
861dc12760SJiri Olsa 	},
871dc12760SJiri Olsa 	[PERF_COUNT_HW_STALLED_CYCLES_FRONTEND] = {
881dc12760SJiri Olsa 		.symbol = "stalled-cycles-frontend",
891dc12760SJiri Olsa 		.alias  = "idle-cycles-frontend",
901dc12760SJiri Olsa 	},
911dc12760SJiri Olsa 	[PERF_COUNT_HW_STALLED_CYCLES_BACKEND] = {
921dc12760SJiri Olsa 		.symbol = "stalled-cycles-backend",
931dc12760SJiri Olsa 		.alias  = "idle-cycles-backend",
941dc12760SJiri Olsa 	},
951dc12760SJiri Olsa 	[PERF_COUNT_HW_REF_CPU_CYCLES] = {
961dc12760SJiri Olsa 		.symbol = "ref-cycles",
971dc12760SJiri Olsa 		.alias  = "",
981dc12760SJiri Olsa 	},
991dc12760SJiri Olsa };
10086470930SIngo Molnar 
101705750f2SYunlong Song struct event_symbol event_symbols_sw[PERF_COUNT_SW_MAX] = {
1021dc12760SJiri Olsa 	[PERF_COUNT_SW_CPU_CLOCK] = {
1031dc12760SJiri Olsa 		.symbol = "cpu-clock",
1041dc12760SJiri Olsa 		.alias  = "",
1051dc12760SJiri Olsa 	},
1061dc12760SJiri Olsa 	[PERF_COUNT_SW_TASK_CLOCK] = {
1071dc12760SJiri Olsa 		.symbol = "task-clock",
1081dc12760SJiri Olsa 		.alias  = "",
1091dc12760SJiri Olsa 	},
1101dc12760SJiri Olsa 	[PERF_COUNT_SW_PAGE_FAULTS] = {
1111dc12760SJiri Olsa 		.symbol = "page-faults",
1121dc12760SJiri Olsa 		.alias  = "faults",
1131dc12760SJiri Olsa 	},
1141dc12760SJiri Olsa 	[PERF_COUNT_SW_CONTEXT_SWITCHES] = {
1151dc12760SJiri Olsa 		.symbol = "context-switches",
1161dc12760SJiri Olsa 		.alias  = "cs",
1171dc12760SJiri Olsa 	},
1181dc12760SJiri Olsa 	[PERF_COUNT_SW_CPU_MIGRATIONS] = {
1191dc12760SJiri Olsa 		.symbol = "cpu-migrations",
1201dc12760SJiri Olsa 		.alias  = "migrations",
1211dc12760SJiri Olsa 	},
1221dc12760SJiri Olsa 	[PERF_COUNT_SW_PAGE_FAULTS_MIN] = {
1231dc12760SJiri Olsa 		.symbol = "minor-faults",
1241dc12760SJiri Olsa 		.alias  = "",
1251dc12760SJiri Olsa 	},
1261dc12760SJiri Olsa 	[PERF_COUNT_SW_PAGE_FAULTS_MAJ] = {
1271dc12760SJiri Olsa 		.symbol = "major-faults",
1281dc12760SJiri Olsa 		.alias  = "",
1291dc12760SJiri Olsa 	},
1301dc12760SJiri Olsa 	[PERF_COUNT_SW_ALIGNMENT_FAULTS] = {
1311dc12760SJiri Olsa 		.symbol = "alignment-faults",
1321dc12760SJiri Olsa 		.alias  = "",
1331dc12760SJiri Olsa 	},
1341dc12760SJiri Olsa 	[PERF_COUNT_SW_EMULATION_FAULTS] = {
1351dc12760SJiri Olsa 		.symbol = "emulation-faults",
1361dc12760SJiri Olsa 		.alias  = "",
1371dc12760SJiri Olsa 	},
138d22d1a2aSAdrian Hunter 	[PERF_COUNT_SW_DUMMY] = {
139d22d1a2aSAdrian Hunter 		.symbol = "dummy",
140d22d1a2aSAdrian Hunter 		.alias  = "",
141d22d1a2aSAdrian Hunter 	},
142bae9cc41SArnaldo Carvalho de Melo 	[PERF_COUNT_SW_BPF_OUTPUT] = {
143bae9cc41SArnaldo Carvalho de Melo 		.symbol = "bpf-output",
144bae9cc41SArnaldo Carvalho de Melo 		.alias  = "",
145bae9cc41SArnaldo Carvalho de Melo 	},
14686470930SIngo Molnar };
14786470930SIngo Molnar 
148cdd6c482SIngo Molnar #define __PERF_EVENT_FIELD(config, name) \
149cdd6c482SIngo Molnar 	((config & PERF_EVENT_##name##_MASK) >> PERF_EVENT_##name##_SHIFT)
15086470930SIngo Molnar 
151cdd6c482SIngo Molnar #define PERF_EVENT_RAW(config)		__PERF_EVENT_FIELD(config, RAW)
152cdd6c482SIngo Molnar #define PERF_EVENT_CONFIG(config)	__PERF_EVENT_FIELD(config, CONFIG)
153cdd6c482SIngo Molnar #define PERF_EVENT_TYPE(config)		__PERF_EVENT_FIELD(config, TYPE)
154cdd6c482SIngo Molnar #define PERF_EVENT_ID(config)		__PERF_EVENT_FIELD(config, EVENT)
15586470930SIngo Molnar 
15622a9f41bSArnaldo Carvalho de Melo #define for_each_subsystem(sys_dir, sys_dirent)			\
15722a9f41bSArnaldo Carvalho de Melo 	while ((sys_dirent = readdir(sys_dir)) != NULL)		\
15822a9f41bSArnaldo Carvalho de Melo 		if (sys_dirent->d_type == DT_DIR &&		\
15922a9f41bSArnaldo Carvalho de Melo 		    (strcmp(sys_dirent->d_name, ".")) &&	\
16022a9f41bSArnaldo Carvalho de Melo 		    (strcmp(sys_dirent->d_name, "..")))
161f6bdafefSJason Baron 
162c02cab22SArnaldo Carvalho de Melo static int tp_event_has_id(const char *dir_path, struct dirent *evt_dir)
163ae07b63fSPeter Zijlstra {
164ae07b63fSPeter Zijlstra 	char evt_path[MAXPATHLEN];
165ae07b63fSPeter Zijlstra 	int fd;
166ae07b63fSPeter Zijlstra 
167c02cab22SArnaldo Carvalho de Melo 	snprintf(evt_path, MAXPATHLEN, "%s/%s/id", dir_path, evt_dir->d_name);
168ae07b63fSPeter Zijlstra 	fd = open(evt_path, O_RDONLY);
169ae07b63fSPeter Zijlstra 	if (fd < 0)
170ae07b63fSPeter Zijlstra 		return -EINVAL;
171ae07b63fSPeter Zijlstra 	close(fd);
172ae07b63fSPeter Zijlstra 
173ae07b63fSPeter Zijlstra 	return 0;
174ae07b63fSPeter Zijlstra }
175ae07b63fSPeter Zijlstra 
176c02cab22SArnaldo Carvalho de Melo #define for_each_event(dir_path, evt_dir, evt_dirent)		\
17722a9f41bSArnaldo Carvalho de Melo 	while ((evt_dirent = readdir(evt_dir)) != NULL)		\
17822a9f41bSArnaldo Carvalho de Melo 		if (evt_dirent->d_type == DT_DIR &&		\
17922a9f41bSArnaldo Carvalho de Melo 		    (strcmp(evt_dirent->d_name, ".")) &&	\
18022a9f41bSArnaldo Carvalho de Melo 		    (strcmp(evt_dirent->d_name, "..")) &&	\
181c02cab22SArnaldo Carvalho de Melo 		    (!tp_event_has_id(dir_path, evt_dirent)))
182f6bdafefSJason Baron 
183270bbbe8SLi Zefan #define MAX_EVENT_LENGTH 512
184f6bdafefSJason Baron 
185f6bdafefSJason Baron 
1861ef2ed10SFrederic Weisbecker struct tracepoint_path *tracepoint_id_to_path(u64 config)
187f6bdafefSJason Baron {
1881ef2ed10SFrederic Weisbecker 	struct tracepoint_path *path = NULL;
189f6bdafefSJason Baron 	DIR *sys_dir, *evt_dir;
19022a9f41bSArnaldo Carvalho de Melo 	struct dirent *sys_dirent, *evt_dirent;
1918aa8a7c8SStephane Eranian 	char id_buf[24];
192725b1368SEric Dumazet 	int fd;
193f6bdafefSJason Baron 	u64 id;
194f6bdafefSJason Baron 	char evt_path[MAXPATHLEN];
19525a7d914SArnaldo Carvalho de Melo 	char *dir_path;
196f6bdafefSJason Baron 
1977014e0e3SArnaldo Carvalho de Melo 	sys_dir = tracing_events__opendir();
198f6bdafefSJason Baron 	if (!sys_dir)
199725b1368SEric Dumazet 		return NULL;
200f6bdafefSJason Baron 
20122a9f41bSArnaldo Carvalho de Melo 	for_each_subsystem(sys_dir, sys_dirent) {
20225a7d914SArnaldo Carvalho de Melo 		dir_path = get_events_file(sys_dirent->d_name);
20325a7d914SArnaldo Carvalho de Melo 		if (!dir_path)
20425a7d914SArnaldo Carvalho de Melo 			continue;
205725b1368SEric Dumazet 		evt_dir = opendir(dir_path);
206725b1368SEric Dumazet 		if (!evt_dir)
20725a7d914SArnaldo Carvalho de Melo 			goto next;
208725b1368SEric Dumazet 
209c02cab22SArnaldo Carvalho de Melo 		for_each_event(dir_path, evt_dir, evt_dirent) {
210725b1368SEric Dumazet 
21177f18153SJiri Olsa 			scnprintf(evt_path, MAXPATHLEN, "%s/%s/id", dir_path,
21222a9f41bSArnaldo Carvalho de Melo 				  evt_dirent->d_name);
213725b1368SEric Dumazet 			fd = open(evt_path, O_RDONLY);
214f6bdafefSJason Baron 			if (fd < 0)
215f6bdafefSJason Baron 				continue;
216f6bdafefSJason Baron 			if (read(fd, id_buf, sizeof(id_buf)) < 0) {
217f6bdafefSJason Baron 				close(fd);
218f6bdafefSJason Baron 				continue;
219f6bdafefSJason Baron 			}
220f6bdafefSJason Baron 			close(fd);
221f6bdafefSJason Baron 			id = atoll(id_buf);
222f6bdafefSJason Baron 			if (id == config) {
22325a7d914SArnaldo Carvalho de Melo 				put_events_file(dir_path);
224f6bdafefSJason Baron 				closedir(evt_dir);
225f6bdafefSJason Baron 				closedir(sys_dir);
22659b4caebSJulia Lawall 				path = zalloc(sizeof(*path));
2275aa365f2STaeung Song 				if (!path)
2285aa365f2STaeung Song 					return NULL;
2291ef2ed10SFrederic Weisbecker 				path->system = malloc(MAX_EVENT_LENGTH);
2301ef2ed10SFrederic Weisbecker 				if (!path->system) {
2311ef2ed10SFrederic Weisbecker 					free(path);
2321ef2ed10SFrederic Weisbecker 					return NULL;
2331ef2ed10SFrederic Weisbecker 				}
2341ef2ed10SFrederic Weisbecker 				path->name = malloc(MAX_EVENT_LENGTH);
2351ef2ed10SFrederic Weisbecker 				if (!path->name) {
23674cf249dSArnaldo Carvalho de Melo 					zfree(&path->system);
2371ef2ed10SFrederic Weisbecker 					free(path);
2381ef2ed10SFrederic Weisbecker 					return NULL;
2391ef2ed10SFrederic Weisbecker 				}
24022a9f41bSArnaldo Carvalho de Melo 				strncpy(path->system, sys_dirent->d_name,
2411ef2ed10SFrederic Weisbecker 					MAX_EVENT_LENGTH);
24222a9f41bSArnaldo Carvalho de Melo 				strncpy(path->name, evt_dirent->d_name,
2431ef2ed10SFrederic Weisbecker 					MAX_EVENT_LENGTH);
2441ef2ed10SFrederic Weisbecker 				return path;
245f6bdafefSJason Baron 			}
246f6bdafefSJason Baron 		}
247f6bdafefSJason Baron 		closedir(evt_dir);
24825a7d914SArnaldo Carvalho de Melo next:
24925a7d914SArnaldo Carvalho de Melo 		put_events_file(dir_path);
250f6bdafefSJason Baron 	}
251f6bdafefSJason Baron 
252f6bdafefSJason Baron 	closedir(sys_dir);
2531ef2ed10SFrederic Weisbecker 	return NULL;
2541ef2ed10SFrederic Weisbecker }
2551ef2ed10SFrederic Weisbecker 
256e7c93f09SNamhyung Kim struct tracepoint_path *tracepoint_name_to_path(const char *name)
257e7c93f09SNamhyung Kim {
258e7c93f09SNamhyung Kim 	struct tracepoint_path *path = zalloc(sizeof(*path));
259e7c93f09SNamhyung Kim 	char *str = strchr(name, ':');
260e7c93f09SNamhyung Kim 
261e7c93f09SNamhyung Kim 	if (path == NULL || str == NULL) {
262e7c93f09SNamhyung Kim 		free(path);
263e7c93f09SNamhyung Kim 		return NULL;
264e7c93f09SNamhyung Kim 	}
265e7c93f09SNamhyung Kim 
266e7c93f09SNamhyung Kim 	path->system = strndup(name, str - name);
267e7c93f09SNamhyung Kim 	path->name = strdup(str+1);
268e7c93f09SNamhyung Kim 
269e7c93f09SNamhyung Kim 	if (path->system == NULL || path->name == NULL) {
27074cf249dSArnaldo Carvalho de Melo 		zfree(&path->system);
27174cf249dSArnaldo Carvalho de Melo 		zfree(&path->name);
272506fde11STaeung Song 		zfree(&path);
273e7c93f09SNamhyung Kim 	}
274e7c93f09SNamhyung Kim 
275e7c93f09SNamhyung Kim 	return path;
276e7c93f09SNamhyung Kim }
277e7c93f09SNamhyung Kim 
2781424dc96SDavid Ahern const char *event_type(int type)
2791424dc96SDavid Ahern {
2801424dc96SDavid Ahern 	switch (type) {
2811424dc96SDavid Ahern 	case PERF_TYPE_HARDWARE:
2821424dc96SDavid Ahern 		return "hardware";
2831424dc96SDavid Ahern 
2841424dc96SDavid Ahern 	case PERF_TYPE_SOFTWARE:
2851424dc96SDavid Ahern 		return "software";
2861424dc96SDavid Ahern 
2871424dc96SDavid Ahern 	case PERF_TYPE_TRACEPOINT:
2881424dc96SDavid Ahern 		return "tracepoint";
2891424dc96SDavid Ahern 
2901424dc96SDavid Ahern 	case PERF_TYPE_HW_CACHE:
2911424dc96SDavid Ahern 		return "hardware-cache";
2921424dc96SDavid Ahern 
2931424dc96SDavid Ahern 	default:
2941424dc96SDavid Ahern 		break;
2951424dc96SDavid Ahern 	}
2961424dc96SDavid Ahern 
2971424dc96SDavid Ahern 	return "unknown";
2981424dc96SDavid Ahern }
2991424dc96SDavid Ahern 
300e814fdddSWang Nan static int parse_events__is_name_term(struct parse_events_term *term)
301e814fdddSWang Nan {
302e814fdddSWang Nan 	return term->type_term == PARSE_EVENTS__TERM_TYPE_NAME;
303e814fdddSWang Nan }
3047ae92e74SYan, Zheng 
305e814fdddSWang Nan static char *get_config_name(struct list_head *head_terms)
306e814fdddSWang Nan {
307e814fdddSWang Nan 	struct parse_events_term *term;
308e814fdddSWang Nan 
309e814fdddSWang Nan 	if (!head_terms)
310e814fdddSWang Nan 		return NULL;
311e814fdddSWang Nan 
312e814fdddSWang Nan 	list_for_each_entry(term, head_terms, list)
313e814fdddSWang Nan 		if (parse_events__is_name_term(term))
314e814fdddSWang Nan 			return term->val.str;
315e814fdddSWang Nan 
316e814fdddSWang Nan 	return NULL;
317e814fdddSWang Nan }
3187ae92e74SYan, Zheng 
31932dcd021SJiri Olsa static struct evsel *
320410136f5SStephane Eranian __add_event(struct list_head *list, int *idx,
3217ae92e74SYan, Zheng 	    struct perf_event_attr *attr,
32266ec1191SMark Rutland 	    char *name, struct perf_pmu *pmu,
323f0fbb114SAndi Kleen 	    struct list_head *config_terms, bool auto_merge_stats,
324f0fbb114SAndi Kleen 	    const char *cpu_list)
32589812fc8SJiri Olsa {
32632dcd021SJiri Olsa 	struct evsel *evsel;
327f854839bSJiri Olsa 	struct perf_cpu_map *cpus = pmu ? pmu->cpus :
3289c3516d1SJiri Olsa 			       cpu_list ? perf_cpu_map__new(cpu_list) : NULL;
32989812fc8SJiri Olsa 
33089812fc8SJiri Olsa 	event_attr_init(attr);
33189812fc8SJiri Olsa 
33275fc5ae5STaeung Song 	evsel = perf_evsel__new_idx(attr, *idx);
333c5cd8ac0SDavid Ahern 	if (!evsel)
334410136f5SStephane Eranian 		return NULL;
33589812fc8SJiri Olsa 
33675fc5ae5STaeung Song 	(*idx)++;
337d400bd3aSJiri Olsa 	evsel->core.cpus   = perf_cpu_map__get(cpus);
338fe1f61b3SJiri Olsa 	evsel->core.own_cpus = perf_cpu_map__get(cpus);
339648b5af3SJiri Olsa 	evsel->core.system_wide = pmu ? pmu->is_uncore : false;
34063ce8449SArnaldo Carvalho de Melo 	evsel->auto_merge_stats = auto_merge_stats;
341f30a79b0SJiri Olsa 
3429db1763cSArnaldo Carvalho de Melo 	if (name)
34389812fc8SJiri Olsa 		evsel->name = strdup(name);
344930a2e29SJiri Olsa 
345930a2e29SJiri Olsa 	if (config_terms)
346930a2e29SJiri Olsa 		list_splice(config_terms, &evsel->config_terms);
347930a2e29SJiri Olsa 
348b27c4eceSJiri Olsa 	list_add_tail(&evsel->core.node, list);
349410136f5SStephane Eranian 	return evsel;
35089812fc8SJiri Olsa }
35189812fc8SJiri Olsa 
352c5cd8ac0SDavid Ahern static int add_event(struct list_head *list, int *idx,
353930a2e29SJiri Olsa 		     struct perf_event_attr *attr, char *name,
354930a2e29SJiri Olsa 		     struct list_head *config_terms)
3557ae92e74SYan, Zheng {
356f0fbb114SAndi Kleen 	return __add_event(list, idx, attr, name, NULL, config_terms, false, NULL) ? 0 : -ENOMEM;
357f0fbb114SAndi Kleen }
358f0fbb114SAndi Kleen 
359f0fbb114SAndi Kleen static int add_event_tool(struct list_head *list, int *idx,
360f0fbb114SAndi Kleen 			  enum perf_tool_event tool_event)
361f0fbb114SAndi Kleen {
36232dcd021SJiri Olsa 	struct evsel *evsel;
363f0fbb114SAndi Kleen 	struct perf_event_attr attr = {
364f0fbb114SAndi Kleen 		.type = PERF_TYPE_SOFTWARE,
365f0fbb114SAndi Kleen 		.config = PERF_COUNT_SW_DUMMY,
366f0fbb114SAndi Kleen 	};
367f0fbb114SAndi Kleen 
368f0fbb114SAndi Kleen 	evsel = __add_event(list, idx, &attr, NULL, NULL, NULL, false, "0");
369f0fbb114SAndi Kleen 	if (!evsel)
370f0fbb114SAndi Kleen 		return -ENOMEM;
371f0fbb114SAndi Kleen 	evsel->tool_event = tool_event;
372f0fbb114SAndi Kleen 	if (tool_event == PERF_TOOL_DURATION_TIME)
373f0fbb114SAndi Kleen 		evsel->unit = strdup("ns");
374f0fbb114SAndi Kleen 	return 0;
3757ae92e74SYan, Zheng }
3767ae92e74SYan, Zheng 
3770b668bc9SArnaldo Carvalho de Melo static int parse_aliases(char *str, const char *names[][PERF_EVSEL__MAX_ALIASES], int size)
37886470930SIngo Molnar {
37986470930SIngo Molnar 	int i, j;
38061c45981SPaul Mackerras 	int n, longest = -1;
38186470930SIngo Molnar 
38286470930SIngo Molnar 	for (i = 0; i < size; i++) {
3830b668bc9SArnaldo Carvalho de Melo 		for (j = 0; j < PERF_EVSEL__MAX_ALIASES && names[i][j]; j++) {
38461c45981SPaul Mackerras 			n = strlen(names[i][j]);
38589812fc8SJiri Olsa 			if (n > longest && !strncasecmp(str, names[i][j], n))
38661c45981SPaul Mackerras 				longest = n;
38761c45981SPaul Mackerras 		}
38889812fc8SJiri Olsa 		if (longest > 0)
38986470930SIngo Molnar 			return i;
39086470930SIngo Molnar 	}
39186470930SIngo Molnar 
3928953645fSIngo Molnar 	return -1;
39386470930SIngo Molnar }
39486470930SIngo Molnar 
39543d0b978SWang Nan typedef int config_term_func_t(struct perf_event_attr *attr,
39643d0b978SWang Nan 			       struct parse_events_term *term,
39743d0b978SWang Nan 			       struct parse_events_error *err);
39843d0b978SWang Nan static int config_term_common(struct perf_event_attr *attr,
39943d0b978SWang Nan 			      struct parse_events_term *term,
40043d0b978SWang Nan 			      struct parse_events_error *err);
40143d0b978SWang Nan static int config_attr(struct perf_event_attr *attr,
40243d0b978SWang Nan 		       struct list_head *head,
40343d0b978SWang Nan 		       struct parse_events_error *err,
40443d0b978SWang Nan 		       config_term_func_t config_term);
40543d0b978SWang Nan 
406c5cd8ac0SDavid Ahern int parse_events_add_cache(struct list_head *list, int *idx,
40743d0b978SWang Nan 			   char *type, char *op_result1, char *op_result2,
4082c97b0d4SArnaldo Carvalho de Melo 			   struct parse_events_error *err,
40943d0b978SWang Nan 			   struct list_head *head_config)
41086470930SIngo Molnar {
41189812fc8SJiri Olsa 	struct perf_event_attr attr;
41243d0b978SWang Nan 	LIST_HEAD(config_terms);
41343d0b978SWang Nan 	char name[MAX_NAME_LEN], *config_name;
41461c45981SPaul Mackerras 	int cache_type = -1, cache_op = -1, cache_result = -1;
41589812fc8SJiri Olsa 	char *op_result[2] = { op_result1, op_result2 };
41689812fc8SJiri Olsa 	int i, n;
41786470930SIngo Molnar 
41886470930SIngo Molnar 	/*
41986470930SIngo Molnar 	 * No fallback - if we cannot get a clear cache type
42086470930SIngo Molnar 	 * then bail out:
42186470930SIngo Molnar 	 */
4220b668bc9SArnaldo Carvalho de Melo 	cache_type = parse_aliases(type, perf_evsel__hw_cache,
42389812fc8SJiri Olsa 				   PERF_COUNT_HW_CACHE_MAX);
42486470930SIngo Molnar 	if (cache_type == -1)
42589812fc8SJiri Olsa 		return -EINVAL;
42686470930SIngo Molnar 
42743d0b978SWang Nan 	config_name = get_config_name(head_config);
42889812fc8SJiri Olsa 	n = snprintf(name, MAX_NAME_LEN, "%s", type);
42989812fc8SJiri Olsa 
43089812fc8SJiri Olsa 	for (i = 0; (i < 2) && (op_result[i]); i++) {
43189812fc8SJiri Olsa 		char *str = op_result[i];
43289812fc8SJiri Olsa 
433275ef387SJiri Olsa 		n += snprintf(name + n, MAX_NAME_LEN - n, "-%s", str);
43461c45981SPaul Mackerras 
43561c45981SPaul Mackerras 		if (cache_op == -1) {
4360b668bc9SArnaldo Carvalho de Melo 			cache_op = parse_aliases(str, perf_evsel__hw_cache_op,
43761c45981SPaul Mackerras 						 PERF_COUNT_HW_CACHE_OP_MAX);
43861c45981SPaul Mackerras 			if (cache_op >= 0) {
4390b668bc9SArnaldo Carvalho de Melo 				if (!perf_evsel__is_cache_op_valid(cache_type, cache_op))
44089812fc8SJiri Olsa 					return -EINVAL;
44161c45981SPaul Mackerras 				continue;
44261c45981SPaul Mackerras 			}
44361c45981SPaul Mackerras 		}
44461c45981SPaul Mackerras 
44561c45981SPaul Mackerras 		if (cache_result == -1) {
4460b668bc9SArnaldo Carvalho de Melo 			cache_result = parse_aliases(str, perf_evsel__hw_cache_result,
44761c45981SPaul Mackerras 						     PERF_COUNT_HW_CACHE_RESULT_MAX);
44861c45981SPaul Mackerras 			if (cache_result >= 0)
44961c45981SPaul Mackerras 				continue;
45061c45981SPaul Mackerras 		}
45161c45981SPaul Mackerras 	}
45261c45981SPaul Mackerras 
45386470930SIngo Molnar 	/*
45486470930SIngo Molnar 	 * Fall back to reads:
45586470930SIngo Molnar 	 */
4568953645fSIngo Molnar 	if (cache_op == -1)
4578953645fSIngo Molnar 		cache_op = PERF_COUNT_HW_CACHE_OP_READ;
45886470930SIngo Molnar 
45986470930SIngo Molnar 	/*
46086470930SIngo Molnar 	 * Fall back to accesses:
46186470930SIngo Molnar 	 */
46286470930SIngo Molnar 	if (cache_result == -1)
46386470930SIngo Molnar 		cache_result = PERF_COUNT_HW_CACHE_RESULT_ACCESS;
46486470930SIngo Molnar 
46589812fc8SJiri Olsa 	memset(&attr, 0, sizeof(attr));
46689812fc8SJiri Olsa 	attr.config = cache_type | (cache_op << 8) | (cache_result << 16);
46789812fc8SJiri Olsa 	attr.type = PERF_TYPE_HW_CACHE;
46843d0b978SWang Nan 
46943d0b978SWang Nan 	if (head_config) {
4702c97b0d4SArnaldo Carvalho de Melo 		if (config_attr(&attr, head_config, err,
47143d0b978SWang Nan 				config_term_common))
47243d0b978SWang Nan 			return -EINVAL;
47343d0b978SWang Nan 
47443d0b978SWang Nan 		if (get_config_terms(head_config, &config_terms))
47543d0b978SWang Nan 			return -ENOMEM;
47643d0b978SWang Nan 	}
47743d0b978SWang Nan 	return add_event(list, idx, &attr, config_name ? : name, &config_terms);
47886470930SIngo Molnar }
47986470930SIngo Molnar 
480272ed29aSJiri Olsa static void tracepoint_error(struct parse_events_error *e, int err,
4818c619d6aSWang Nan 			     const char *sys, const char *name)
48219658171SJiri Olsa {
48319658171SJiri Olsa 	char help[BUFSIZ];
48419658171SJiri Olsa 
485ec183d22SAdrian Hunter 	if (!e)
486ec183d22SAdrian Hunter 		return;
487ec183d22SAdrian Hunter 
48819658171SJiri Olsa 	/*
48919658171SJiri Olsa 	 * We get error directly from syscall errno ( > 0),
49019658171SJiri Olsa 	 * or from encoded pointer's error ( < 0).
49119658171SJiri Olsa 	 */
49219658171SJiri Olsa 	err = abs(err);
49319658171SJiri Olsa 
49419658171SJiri Olsa 	switch (err) {
49519658171SJiri Olsa 	case EACCES:
496272ed29aSJiri Olsa 		e->str = strdup("can't access trace events");
49719658171SJiri Olsa 		break;
49819658171SJiri Olsa 	case ENOENT:
499272ed29aSJiri Olsa 		e->str = strdup("unknown tracepoint");
50019658171SJiri Olsa 		break;
50119658171SJiri Olsa 	default:
502272ed29aSJiri Olsa 		e->str = strdup("failed to add tracepoint");
50319658171SJiri Olsa 		break;
50419658171SJiri Olsa 	}
50519658171SJiri Olsa 
50619658171SJiri Olsa 	tracing_path__strerror_open_tp(err, help, sizeof(help), sys, name);
507272ed29aSJiri Olsa 	e->help = strdup(help);
50819658171SJiri Olsa }
50919658171SJiri Olsa 
510c5cd8ac0SDavid Ahern static int add_tracepoint(struct list_head *list, int *idx,
5118c619d6aSWang Nan 			  const char *sys_name, const char *evt_name,
512272ed29aSJiri Olsa 			  struct parse_events_error *err,
513e637d177SHe Kuang 			  struct list_head *head_config)
514bcd3279fSFrederic Weisbecker {
51532dcd021SJiri Olsa 	struct evsel *evsel;
516bcd3279fSFrederic Weisbecker 
517ef503831SArnaldo Carvalho de Melo 	evsel = perf_evsel__newtp_idx(sys_name, evt_name, (*idx)++);
51819658171SJiri Olsa 	if (IS_ERR(evsel)) {
519272ed29aSJiri Olsa 		tracepoint_error(err, PTR_ERR(evsel), sys_name, evt_name);
5208dd2a131SJiri Olsa 		return PTR_ERR(evsel);
52119658171SJiri Olsa 	}
522bcd3279fSFrederic Weisbecker 
523e637d177SHe Kuang 	if (head_config) {
524e637d177SHe Kuang 		LIST_HEAD(config_terms);
525e637d177SHe Kuang 
526e637d177SHe Kuang 		if (get_config_terms(head_config, &config_terms))
527e637d177SHe Kuang 			return -ENOMEM;
528e637d177SHe Kuang 		list_splice(&config_terms, &evsel->config_terms);
529e637d177SHe Kuang 	}
530e637d177SHe Kuang 
531b27c4eceSJiri Olsa 	list_add_tail(&evsel->core.node, list);
53282fe1c29SArnaldo Carvalho de Melo 	return 0;
533bcd3279fSFrederic Weisbecker }
534bcd3279fSFrederic Weisbecker 
535c5cd8ac0SDavid Ahern static int add_tracepoint_multi_event(struct list_head *list, int *idx,
5368c619d6aSWang Nan 				      const char *sys_name, const char *evt_name,
537272ed29aSJiri Olsa 				      struct parse_events_error *err,
538e637d177SHe Kuang 				      struct list_head *head_config)
539bcd3279fSFrederic Weisbecker {
54025a7d914SArnaldo Carvalho de Melo 	char *evt_path;
541bcd3279fSFrederic Weisbecker 	struct dirent *evt_ent;
542bcd3279fSFrederic Weisbecker 	DIR *evt_dir;
54327bf90bfSJiri Olsa 	int ret = 0, found = 0;
544bcd3279fSFrederic Weisbecker 
54525a7d914SArnaldo Carvalho de Melo 	evt_path = get_events_file(sys_name);
54625a7d914SArnaldo Carvalho de Melo 	if (!evt_path) {
54725a7d914SArnaldo Carvalho de Melo 		tracepoint_error(err, errno, sys_name, evt_name);
54825a7d914SArnaldo Carvalho de Melo 		return -1;
54925a7d914SArnaldo Carvalho de Melo 	}
550bcd3279fSFrederic Weisbecker 	evt_dir = opendir(evt_path);
551bcd3279fSFrederic Weisbecker 	if (!evt_dir) {
55225a7d914SArnaldo Carvalho de Melo 		put_events_file(evt_path);
553272ed29aSJiri Olsa 		tracepoint_error(err, errno, sys_name, evt_name);
55489812fc8SJiri Olsa 		return -1;
555bcd3279fSFrederic Weisbecker 	}
556bcd3279fSFrederic Weisbecker 
55789812fc8SJiri Olsa 	while (!ret && (evt_ent = readdir(evt_dir))) {
558bcd3279fSFrederic Weisbecker 		if (!strcmp(evt_ent->d_name, ".")
559bcd3279fSFrederic Weisbecker 		    || !strcmp(evt_ent->d_name, "..")
560bcd3279fSFrederic Weisbecker 		    || !strcmp(evt_ent->d_name, "enable")
561bcd3279fSFrederic Weisbecker 		    || !strcmp(evt_ent->d_name, "filter"))
562bcd3279fSFrederic Weisbecker 			continue;
563bcd3279fSFrederic Weisbecker 
56489812fc8SJiri Olsa 		if (!strglobmatch(evt_ent->d_name, evt_name))
565fb1d2edfSMasami Hiramatsu 			continue;
566fb1d2edfSMasami Hiramatsu 
56727bf90bfSJiri Olsa 		found++;
56827bf90bfSJiri Olsa 
569e637d177SHe Kuang 		ret = add_tracepoint(list, idx, sys_name, evt_ent->d_name,
570272ed29aSJiri Olsa 				     err, head_config);
571bcd3279fSFrederic Weisbecker 	}
572bcd3279fSFrederic Weisbecker 
57327bf90bfSJiri Olsa 	if (!found) {
57427bf90bfSJiri Olsa 		tracepoint_error(err, ENOENT, sys_name, evt_name);
57527bf90bfSJiri Olsa 		ret = -1;
57627bf90bfSJiri Olsa 	}
57727bf90bfSJiri Olsa 
57825a7d914SArnaldo Carvalho de Melo 	put_events_file(evt_path);
5790bd3f084SJiri Olsa 	closedir(evt_dir);
58089812fc8SJiri Olsa 	return ret;
581bcd3279fSFrederic Weisbecker }
582bcd3279fSFrederic Weisbecker 
583c5cd8ac0SDavid Ahern static int add_tracepoint_event(struct list_head *list, int *idx,
5848c619d6aSWang Nan 				const char *sys_name, const char *evt_name,
585272ed29aSJiri Olsa 				struct parse_events_error *err,
586e637d177SHe Kuang 				struct list_head *head_config)
587f35488f9SJiri Olsa {
588f35488f9SJiri Olsa 	return strpbrk(evt_name, "*?") ?
589e637d177SHe Kuang 	       add_tracepoint_multi_event(list, idx, sys_name, evt_name,
590272ed29aSJiri Olsa 					  err, head_config) :
591e637d177SHe Kuang 	       add_tracepoint(list, idx, sys_name, evt_name,
592272ed29aSJiri Olsa 			      err, head_config);
593f35488f9SJiri Olsa }
594f35488f9SJiri Olsa 
595c5cd8ac0SDavid Ahern static int add_tracepoint_multi_sys(struct list_head *list, int *idx,
5968c619d6aSWang Nan 				    const char *sys_name, const char *evt_name,
597272ed29aSJiri Olsa 				    struct parse_events_error *err,
598e637d177SHe Kuang 				    struct list_head *head_config)
599f35488f9SJiri Olsa {
600f35488f9SJiri Olsa 	struct dirent *events_ent;
601f35488f9SJiri Olsa 	DIR *events_dir;
602f35488f9SJiri Olsa 	int ret = 0;
603f35488f9SJiri Olsa 
6047014e0e3SArnaldo Carvalho de Melo 	events_dir = tracing_events__opendir();
605f35488f9SJiri Olsa 	if (!events_dir) {
606272ed29aSJiri Olsa 		tracepoint_error(err, errno, sys_name, evt_name);
607f35488f9SJiri Olsa 		return -1;
608f35488f9SJiri Olsa 	}
609f35488f9SJiri Olsa 
610f35488f9SJiri Olsa 	while (!ret && (events_ent = readdir(events_dir))) {
611f35488f9SJiri Olsa 		if (!strcmp(events_ent->d_name, ".")
612f35488f9SJiri Olsa 		    || !strcmp(events_ent->d_name, "..")
613f35488f9SJiri Olsa 		    || !strcmp(events_ent->d_name, "enable")
614f35488f9SJiri Olsa 		    || !strcmp(events_ent->d_name, "header_event")
615f35488f9SJiri Olsa 		    || !strcmp(events_ent->d_name, "header_page"))
616f35488f9SJiri Olsa 			continue;
617f35488f9SJiri Olsa 
618f35488f9SJiri Olsa 		if (!strglobmatch(events_ent->d_name, sys_name))
619f35488f9SJiri Olsa 			continue;
620f35488f9SJiri Olsa 
621f35488f9SJiri Olsa 		ret = add_tracepoint_event(list, idx, events_ent->d_name,
622272ed29aSJiri Olsa 					   evt_name, err, head_config);
623f35488f9SJiri Olsa 	}
624f35488f9SJiri Olsa 
625f35488f9SJiri Olsa 	closedir(events_dir);
626f35488f9SJiri Olsa 	return ret;
627f35488f9SJiri Olsa }
628f35488f9SJiri Olsa 
6294edf30e3SWang Nan struct __add_bpf_event_param {
6305d9cdc11SArnaldo Carvalho de Melo 	struct parse_events_state *parse_state;
6314edf30e3SWang Nan 	struct list_head *list;
63295088a59SWang Nan 	struct list_head *head_config;
6334edf30e3SWang Nan };
6344edf30e3SWang Nan 
635af4a0991SArnaldo Carvalho de Melo static int add_bpf_event(const char *group, const char *event, int fd, struct bpf_object *obj,
6364edf30e3SWang Nan 			 void *_param)
6374edf30e3SWang Nan {
6384edf30e3SWang Nan 	LIST_HEAD(new_evsels);
6394edf30e3SWang Nan 	struct __add_bpf_event_param *param = _param;
6405d9cdc11SArnaldo Carvalho de Melo 	struct parse_events_state *parse_state = param->parse_state;
6414edf30e3SWang Nan 	struct list_head *list = param->list;
64232dcd021SJiri Olsa 	struct evsel *pos;
6434edf30e3SWang Nan 	int err;
6442620b7e3SArnaldo Carvalho de Melo 	/*
6452620b7e3SArnaldo Carvalho de Melo 	 * Check if we should add the event, i.e. if it is a TP but starts with a '!',
6462620b7e3SArnaldo Carvalho de Melo 	 * then don't add the tracepoint, this will be used for something else, like
6472620b7e3SArnaldo Carvalho de Melo 	 * adding to a BPF_MAP_TYPE_PROG_ARRAY.
6482620b7e3SArnaldo Carvalho de Melo 	 *
6492620b7e3SArnaldo Carvalho de Melo 	 * See tools/perf/examples/bpf/augmented_raw_syscalls.c
6502620b7e3SArnaldo Carvalho de Melo 	 */
6512620b7e3SArnaldo Carvalho de Melo 	if (group[0] == '!')
6522620b7e3SArnaldo Carvalho de Melo 		return 0;
6534edf30e3SWang Nan 
6544edf30e3SWang Nan 	pr_debug("add bpf event %s:%s and attach bpf program %d\n",
655cd102d70SWang Nan 		 group, event, fd);
6564edf30e3SWang Nan 
6575d369a75SArnaldo Carvalho de Melo 	err = parse_events_add_tracepoint(&new_evsels, &parse_state->idx, group,
6585d369a75SArnaldo Carvalho de Melo 					  event, parse_state->error,
65995088a59SWang Nan 					  param->head_config);
6604edf30e3SWang Nan 	if (err) {
66132dcd021SJiri Olsa 		struct evsel *evsel, *tmp;
6624edf30e3SWang Nan 
6634edf30e3SWang Nan 		pr_debug("Failed to add BPF event %s:%s\n",
664cd102d70SWang Nan 			 group, event);
665b27c4eceSJiri Olsa 		list_for_each_entry_safe(evsel, tmp, &new_evsels, core.node) {
666b27c4eceSJiri Olsa 			list_del_init(&evsel->core.node);
6675eb2dd2aSJiri Olsa 			evsel__delete(evsel);
6684edf30e3SWang Nan 		}
6694edf30e3SWang Nan 		return err;
6704edf30e3SWang Nan 	}
671cd102d70SWang Nan 	pr_debug("adding %s:%s\n", group, event);
6724edf30e3SWang Nan 
673b27c4eceSJiri Olsa 	list_for_each_entry(pos, &new_evsels, core.node) {
6741f45b1d4SWang Nan 		pr_debug("adding %s:%s to %p\n",
675cd102d70SWang Nan 			 group, event, pos);
6761f45b1d4SWang Nan 		pos->bpf_fd = fd;
677af4a0991SArnaldo Carvalho de Melo 		pos->bpf_obj = obj;
6781f45b1d4SWang Nan 	}
6794edf30e3SWang Nan 	list_splice(&new_evsels, list);
6804edf30e3SWang Nan 	return 0;
6814edf30e3SWang Nan }
6824edf30e3SWang Nan 
6835d9cdc11SArnaldo Carvalho de Melo int parse_events_load_bpf_obj(struct parse_events_state *parse_state,
68484c86ca1SWang Nan 			      struct list_head *list,
68595088a59SWang Nan 			      struct bpf_object *obj,
68695088a59SWang Nan 			      struct list_head *head_config)
68784c86ca1SWang Nan {
68884c86ca1SWang Nan 	int err;
68984c86ca1SWang Nan 	char errbuf[BUFSIZ];
6905d9cdc11SArnaldo Carvalho de Melo 	struct __add_bpf_event_param param = {parse_state, list, head_config};
691aa3abf30SWang Nan 	static bool registered_unprobe_atexit = false;
69284c86ca1SWang Nan 
69384c86ca1SWang Nan 	if (IS_ERR(obj) || !obj) {
69484c86ca1SWang Nan 		snprintf(errbuf, sizeof(errbuf),
69584c86ca1SWang Nan 			 "Internal error: load bpf obj with NULL");
69684c86ca1SWang Nan 		err = -EINVAL;
69784c86ca1SWang Nan 		goto errout;
69884c86ca1SWang Nan 	}
69984c86ca1SWang Nan 
70084c86ca1SWang Nan 	/*
701aa3abf30SWang Nan 	 * Register atexit handler before calling bpf__probe() so
702aa3abf30SWang Nan 	 * bpf__probe() don't need to unprobe probe points its already
703aa3abf30SWang Nan 	 * created when failure.
704aa3abf30SWang Nan 	 */
705aa3abf30SWang Nan 	if (!registered_unprobe_atexit) {
706aa3abf30SWang Nan 		atexit(bpf__clear);
707aa3abf30SWang Nan 		registered_unprobe_atexit = true;
708aa3abf30SWang Nan 	}
709aa3abf30SWang Nan 
710aa3abf30SWang Nan 	err = bpf__probe(obj);
711aa3abf30SWang Nan 	if (err) {
712aa3abf30SWang Nan 		bpf__strerror_probe(obj, err, errbuf, sizeof(errbuf));
713aa3abf30SWang Nan 		goto errout;
714aa3abf30SWang Nan 	}
715aa3abf30SWang Nan 
7161e5e3ee8SWang Nan 	err = bpf__load(obj);
7171e5e3ee8SWang Nan 	if (err) {
7181e5e3ee8SWang Nan 		bpf__strerror_load(obj, err, errbuf, sizeof(errbuf));
7191e5e3ee8SWang Nan 		goto errout;
7201e5e3ee8SWang Nan 	}
7211e5e3ee8SWang Nan 
722cd102d70SWang Nan 	err = bpf__foreach_event(obj, add_bpf_event, &param);
7234edf30e3SWang Nan 	if (err) {
7244edf30e3SWang Nan 		snprintf(errbuf, sizeof(errbuf),
7254edf30e3SWang Nan 			 "Attach events in BPF object failed");
7264edf30e3SWang Nan 		goto errout;
7274edf30e3SWang Nan 	}
7284edf30e3SWang Nan 
7294edf30e3SWang Nan 	return 0;
73084c86ca1SWang Nan errout:
7315d9cdc11SArnaldo Carvalho de Melo 	parse_state->error->help = strdup("(add -v to see detail)");
7325d9cdc11SArnaldo Carvalho de Melo 	parse_state->error->str = strdup(errbuf);
73384c86ca1SWang Nan 	return err;
73484c86ca1SWang Nan }
73584c86ca1SWang Nan 
736a34f3be7SWang Nan static int
7375d9cdc11SArnaldo Carvalho de Melo parse_events_config_bpf(struct parse_events_state *parse_state,
738a34f3be7SWang Nan 			struct bpf_object *obj,
739a34f3be7SWang Nan 			struct list_head *head_config)
740a34f3be7SWang Nan {
741a34f3be7SWang Nan 	struct parse_events_term *term;
742a34f3be7SWang Nan 	int error_pos;
743a34f3be7SWang Nan 
744a34f3be7SWang Nan 	if (!head_config || list_empty(head_config))
745a34f3be7SWang Nan 		return 0;
746a34f3be7SWang Nan 
747a34f3be7SWang Nan 	list_for_each_entry(term, head_config, list) {
748a34f3be7SWang Nan 		char errbuf[BUFSIZ];
749a34f3be7SWang Nan 		int err;
750a34f3be7SWang Nan 
751a34f3be7SWang Nan 		if (term->type_term != PARSE_EVENTS__TERM_TYPE_USER) {
752a34f3be7SWang Nan 			snprintf(errbuf, sizeof(errbuf),
753a34f3be7SWang Nan 				 "Invalid config term for BPF object");
754a34f3be7SWang Nan 			errbuf[BUFSIZ - 1] = '\0';
755a34f3be7SWang Nan 
7565d9cdc11SArnaldo Carvalho de Melo 			parse_state->error->idx = term->err_term;
7575d9cdc11SArnaldo Carvalho de Melo 			parse_state->error->str = strdup(errbuf);
758a34f3be7SWang Nan 			return -EINVAL;
759a34f3be7SWang Nan 		}
760a34f3be7SWang Nan 
7615d9cdc11SArnaldo Carvalho de Melo 		err = bpf__config_obj(obj, term, parse_state->evlist, &error_pos);
762a34f3be7SWang Nan 		if (err) {
7635d9cdc11SArnaldo Carvalho de Melo 			bpf__strerror_config_obj(obj, term, parse_state->evlist,
764a34f3be7SWang Nan 						 &error_pos, err, errbuf,
765a34f3be7SWang Nan 						 sizeof(errbuf));
7665d9cdc11SArnaldo Carvalho de Melo 			parse_state->error->help = strdup(
7677630b3e2SWang Nan "Hint:\tValid config terms:\n"
768e571e029SWang Nan "     \tmap:[<arraymap>].value<indices>=[value]\n"
769e571e029SWang Nan "     \tmap:[<eventmap>].event<indices>=[event]\n"
7707630b3e2SWang Nan "\n"
771e571e029SWang Nan "     \twhere <indices> is something like [0,3...5] or [all]\n"
772a34f3be7SWang Nan "     \t(add -v to see detail)");
7735d9cdc11SArnaldo Carvalho de Melo 			parse_state->error->str = strdup(errbuf);
774a34f3be7SWang Nan 			if (err == -BPF_LOADER_ERRNO__OBJCONF_MAP_VALUE)
7755d9cdc11SArnaldo Carvalho de Melo 				parse_state->error->idx = term->err_val;
776a34f3be7SWang Nan 			else
7775d9cdc11SArnaldo Carvalho de Melo 				parse_state->error->idx = term->err_term + error_pos;
778a34f3be7SWang Nan 			return err;
779a34f3be7SWang Nan 		}
780a34f3be7SWang Nan 	}
781a34f3be7SWang Nan 	return 0;
782a34f3be7SWang Nan }
783a34f3be7SWang Nan 
78495088a59SWang Nan /*
78595088a59SWang Nan  * Split config terms:
78695088a59SWang Nan  * perf record -e bpf.c/call-graph=fp,map:array.value[0]=1/ ...
78795088a59SWang Nan  *  'call-graph=fp' is 'evt config', should be applied to each
78895088a59SWang Nan  *  events in bpf.c.
78995088a59SWang Nan  * 'map:array.value[0]=1' is 'obj config', should be processed
79095088a59SWang Nan  * with parse_events_config_bpf.
79195088a59SWang Nan  *
79295088a59SWang Nan  * Move object config terms from the first list to obj_head_config.
79395088a59SWang Nan  */
79495088a59SWang Nan static void
79595088a59SWang Nan split_bpf_config_terms(struct list_head *evt_head_config,
79695088a59SWang Nan 		       struct list_head *obj_head_config)
79795088a59SWang Nan {
79895088a59SWang Nan 	struct parse_events_term *term, *temp;
79995088a59SWang Nan 
80095088a59SWang Nan 	/*
80195088a59SWang Nan 	 * Currectly, all possible user config term
80295088a59SWang Nan 	 * belong to bpf object. parse_events__is_hardcoded_term()
80395088a59SWang Nan 	 * happends to be a good flag.
80495088a59SWang Nan 	 *
80595088a59SWang Nan 	 * See parse_events_config_bpf() and
80695088a59SWang Nan 	 * config_term_tracepoint().
80795088a59SWang Nan 	 */
80895088a59SWang Nan 	list_for_each_entry_safe(term, temp, evt_head_config, list)
80995088a59SWang Nan 		if (!parse_events__is_hardcoded_term(term))
81095088a59SWang Nan 			list_move_tail(&term->list, obj_head_config);
81195088a59SWang Nan }
81295088a59SWang Nan 
8135d9cdc11SArnaldo Carvalho de Melo int parse_events_load_bpf(struct parse_events_state *parse_state,
81484c86ca1SWang Nan 			  struct list_head *list,
815d509db04SWang Nan 			  char *bpf_file_name,
816a34f3be7SWang Nan 			  bool source,
817a34f3be7SWang Nan 			  struct list_head *head_config)
81884c86ca1SWang Nan {
819a34f3be7SWang Nan 	int err;
82095088a59SWang Nan 	struct bpf_object *obj;
82195088a59SWang Nan 	LIST_HEAD(obj_head_config);
82295088a59SWang Nan 
82395088a59SWang Nan 	if (head_config)
82495088a59SWang Nan 		split_bpf_config_terms(head_config, &obj_head_config);
82584c86ca1SWang Nan 
826d509db04SWang Nan 	obj = bpf__prepare_load(bpf_file_name, source);
8276371ca3bSWang Nan 	if (IS_ERR(obj)) {
82884c86ca1SWang Nan 		char errbuf[BUFSIZ];
82984c86ca1SWang Nan 
8306371ca3bSWang Nan 		err = PTR_ERR(obj);
83184c86ca1SWang Nan 
83284c86ca1SWang Nan 		if (err == -ENOTSUP)
83384c86ca1SWang Nan 			snprintf(errbuf, sizeof(errbuf),
83484c86ca1SWang Nan 				 "BPF support is not compiled");
83584c86ca1SWang Nan 		else
836d3e0ce39SWang Nan 			bpf__strerror_prepare_load(bpf_file_name,
837d3e0ce39SWang Nan 						   source,
838d3e0ce39SWang Nan 						   -err, errbuf,
839d3e0ce39SWang Nan 						   sizeof(errbuf));
84084c86ca1SWang Nan 
8415d9cdc11SArnaldo Carvalho de Melo 		parse_state->error->help = strdup("(add -v to see detail)");
8425d9cdc11SArnaldo Carvalho de Melo 		parse_state->error->str = strdup(errbuf);
84384c86ca1SWang Nan 		return err;
84484c86ca1SWang Nan 	}
84584c86ca1SWang Nan 
8465d9cdc11SArnaldo Carvalho de Melo 	err = parse_events_load_bpf_obj(parse_state, list, obj, head_config);
847a34f3be7SWang Nan 	if (err)
848a34f3be7SWang Nan 		return err;
8495d9cdc11SArnaldo Carvalho de Melo 	err = parse_events_config_bpf(parse_state, obj, &obj_head_config);
85095088a59SWang Nan 
85195088a59SWang Nan 	/*
85295088a59SWang Nan 	 * Caller doesn't know anything about obj_head_config,
85395088a59SWang Nan 	 * so combine them together again before returnning.
85495088a59SWang Nan 	 */
85595088a59SWang Nan 	if (head_config)
85695088a59SWang Nan 		list_splice_tail(&obj_head_config, head_config);
85795088a59SWang Nan 	return err;
85884c86ca1SWang Nan }
85984c86ca1SWang Nan 
86089812fc8SJiri Olsa static int
86189812fc8SJiri Olsa parse_breakpoint_type(const char *type, struct perf_event_attr *attr)
8621b290d67SFrederic Weisbecker {
8631b290d67SFrederic Weisbecker 	int i;
8641b290d67SFrederic Weisbecker 
8651b290d67SFrederic Weisbecker 	for (i = 0; i < 3; i++) {
86689812fc8SJiri Olsa 		if (!type || !type[i])
8671b290d67SFrederic Weisbecker 			break;
8681b290d67SFrederic Weisbecker 
8697582732fSJiri Olsa #define CHECK_SET_TYPE(bit)		\
8707582732fSJiri Olsa do {					\
8717582732fSJiri Olsa 	if (attr->bp_type & bit)	\
8727582732fSJiri Olsa 		return -EINVAL;		\
8737582732fSJiri Olsa 	else				\
8747582732fSJiri Olsa 		attr->bp_type |= bit;	\
8757582732fSJiri Olsa } while (0)
8767582732fSJiri Olsa 
8771b290d67SFrederic Weisbecker 		switch (type[i]) {
8781b290d67SFrederic Weisbecker 		case 'r':
8797582732fSJiri Olsa 			CHECK_SET_TYPE(HW_BREAKPOINT_R);
8801b290d67SFrederic Weisbecker 			break;
8811b290d67SFrederic Weisbecker 		case 'w':
8827582732fSJiri Olsa 			CHECK_SET_TYPE(HW_BREAKPOINT_W);
8831b290d67SFrederic Weisbecker 			break;
8841b290d67SFrederic Weisbecker 		case 'x':
8857582732fSJiri Olsa 			CHECK_SET_TYPE(HW_BREAKPOINT_X);
8861b290d67SFrederic Weisbecker 			break;
8871b290d67SFrederic Weisbecker 		default:
88889812fc8SJiri Olsa 			return -EINVAL;
8891b290d67SFrederic Weisbecker 		}
8901b290d67SFrederic Weisbecker 	}
89189812fc8SJiri Olsa 
8927582732fSJiri Olsa #undef CHECK_SET_TYPE
8937582732fSJiri Olsa 
8941b290d67SFrederic Weisbecker 	if (!attr->bp_type) /* Default */
8951b290d67SFrederic Weisbecker 		attr->bp_type = HW_BREAKPOINT_R | HW_BREAKPOINT_W;
8961b290d67SFrederic Weisbecker 
89789812fc8SJiri Olsa 	return 0;
8981b290d67SFrederic Weisbecker }
8991b290d67SFrederic Weisbecker 
900c5cd8ac0SDavid Ahern int parse_events_add_breakpoint(struct list_head *list, int *idx,
9013741eb9fSJacob Shin 				void *ptr, char *type, u64 len)
9021b290d67SFrederic Weisbecker {
90389812fc8SJiri Olsa 	struct perf_event_attr attr;
9041b290d67SFrederic Weisbecker 
90589812fc8SJiri Olsa 	memset(&attr, 0, sizeof(attr));
9069fafd98fSJiri Olsa 	attr.bp_addr = (unsigned long) ptr;
9071b290d67SFrederic Weisbecker 
90889812fc8SJiri Olsa 	if (parse_breakpoint_type(type, &attr))
90989812fc8SJiri Olsa 		return -EINVAL;
9101b290d67SFrederic Weisbecker 
9113741eb9fSJacob Shin 	/* Provide some defaults if len is not specified */
9123741eb9fSJacob Shin 	if (!len) {
91389812fc8SJiri Olsa 		if (attr.bp_type == HW_BREAKPOINT_X)
9143741eb9fSJacob Shin 			len = sizeof(long);
915aa59a485SFrederic Weisbecker 		else
9163741eb9fSJacob Shin 			len = HW_BREAKPOINT_LEN_4;
9173741eb9fSJacob Shin 	}
9183741eb9fSJacob Shin 
9193741eb9fSJacob Shin 	attr.bp_len = len;
920aa59a485SFrederic Weisbecker 
92189812fc8SJiri Olsa 	attr.type = PERF_TYPE_BREAKPOINT;
9224a841d65SJovi Zhang 	attr.sample_period = 1;
9231b290d67SFrederic Weisbecker 
924930a2e29SJiri Olsa 	return add_event(list, idx, &attr, NULL, NULL);
9251b290d67SFrederic Weisbecker }
9261b290d67SFrederic Weisbecker 
9273b0e371cSJiri Olsa static int check_type_val(struct parse_events_term *term,
9283b0e371cSJiri Olsa 			  struct parse_events_error *err,
9293b0e371cSJiri Olsa 			  int type)
9303b0e371cSJiri Olsa {
9313b0e371cSJiri Olsa 	if (type == term->type_val)
9323b0e371cSJiri Olsa 		return 0;
9333b0e371cSJiri Olsa 
9343b0e371cSJiri Olsa 	if (err) {
9353b0e371cSJiri Olsa 		err->idx = term->err_val;
9363b0e371cSJiri Olsa 		if (type == PARSE_EVENTS__TERM_TYPE_NUM)
9373b0e371cSJiri Olsa 			err->str = strdup("expected numeric value");
9383b0e371cSJiri Olsa 		else
9393b0e371cSJiri Olsa 			err->str = strdup("expected string value");
9403b0e371cSJiri Olsa 	}
9413b0e371cSJiri Olsa 	return -EINVAL;
9423b0e371cSJiri Olsa }
9433b0e371cSJiri Olsa 
94417cb5f84SWang Nan /*
94517cb5f84SWang Nan  * Update according to parse-events.l
94617cb5f84SWang Nan  */
94717cb5f84SWang Nan static const char *config_term_names[__PARSE_EVENTS__TERM_TYPE_NR] = {
94817cb5f84SWang Nan 	[PARSE_EVENTS__TERM_TYPE_USER]			= "<sysfs term>",
94917cb5f84SWang Nan 	[PARSE_EVENTS__TERM_TYPE_CONFIG]		= "config",
95017cb5f84SWang Nan 	[PARSE_EVENTS__TERM_TYPE_CONFIG1]		= "config1",
95117cb5f84SWang Nan 	[PARSE_EVENTS__TERM_TYPE_CONFIG2]		= "config2",
95217cb5f84SWang Nan 	[PARSE_EVENTS__TERM_TYPE_NAME]			= "name",
95317cb5f84SWang Nan 	[PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD]		= "period",
95417cb5f84SWang Nan 	[PARSE_EVENTS__TERM_TYPE_SAMPLE_FREQ]		= "freq",
95517cb5f84SWang Nan 	[PARSE_EVENTS__TERM_TYPE_BRANCH_SAMPLE_TYPE]	= "branch_type",
95617cb5f84SWang Nan 	[PARSE_EVENTS__TERM_TYPE_TIME]			= "time",
95717cb5f84SWang Nan 	[PARSE_EVENTS__TERM_TYPE_CALLGRAPH]		= "call-graph",
95817cb5f84SWang Nan 	[PARSE_EVENTS__TERM_TYPE_STACKSIZE]		= "stack-size",
95917cb5f84SWang Nan 	[PARSE_EVENTS__TERM_TYPE_NOINHERIT]		= "no-inherit",
96017cb5f84SWang Nan 	[PARSE_EVENTS__TERM_TYPE_INHERIT]		= "inherit",
961792d48b4SArnaldo Carvalho de Melo 	[PARSE_EVENTS__TERM_TYPE_MAX_STACK]		= "max-stack",
9622fda5adaSArnaldo Carvalho de Melo 	[PARSE_EVENTS__TERM_TYPE_MAX_EVENTS]		= "nr",
963626a6b78SWang Nan 	[PARSE_EVENTS__TERM_TYPE_OVERWRITE]		= "overwrite",
964626a6b78SWang Nan 	[PARSE_EVENTS__TERM_TYPE_NOOVERWRITE]		= "no-overwrite",
965dd60fba7SMathieu Poirier 	[PARSE_EVENTS__TERM_TYPE_DRV_CFG]		= "driver-config",
966064b4e82SJin Yao 	[PARSE_EVENTS__TERM_TYPE_PERCORE]		= "percore",
9671b992154SAdrian Hunter 	[PARSE_EVENTS__TERM_TYPE_AUX_OUTPUT]		= "aux-output",
96817cb5f84SWang Nan };
96917cb5f84SWang Nan 
9701669e509SWang Nan static bool config_term_shrinked;
9711669e509SWang Nan 
9721669e509SWang Nan static bool
9731669e509SWang Nan config_term_avail(int term_type, struct parse_events_error *err)
9741669e509SWang Nan {
9751669e509SWang Nan 	if (term_type < 0 || term_type >= __PARSE_EVENTS__TERM_TYPE_NR) {
9761669e509SWang Nan 		err->str = strdup("Invalid term_type");
9771669e509SWang Nan 		return false;
9781669e509SWang Nan 	}
9791669e509SWang Nan 	if (!config_term_shrinked)
9801669e509SWang Nan 		return true;
9811669e509SWang Nan 
9821669e509SWang Nan 	switch (term_type) {
9831669e509SWang Nan 	case PARSE_EVENTS__TERM_TYPE_CONFIG:
9841669e509SWang Nan 	case PARSE_EVENTS__TERM_TYPE_CONFIG1:
9851669e509SWang Nan 	case PARSE_EVENTS__TERM_TYPE_CONFIG2:
9861669e509SWang Nan 	case PARSE_EVENTS__TERM_TYPE_NAME:
98706835545SSukadev Bhattiprolu 	case PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD:
988064b4e82SJin Yao 	case PARSE_EVENTS__TERM_TYPE_PERCORE:
9891669e509SWang Nan 		return true;
9901669e509SWang Nan 	default:
9911669e509SWang Nan 		if (!err)
9921669e509SWang Nan 			return false;
9931669e509SWang Nan 
9941669e509SWang Nan 		/* term_type is validated so indexing is safe */
9951669e509SWang Nan 		if (asprintf(&err->str, "'%s' is not usable in 'perf stat'",
9961669e509SWang Nan 			     config_term_names[term_type]) < 0)
9971669e509SWang Nan 			err->str = NULL;
9981669e509SWang Nan 		return false;
9991669e509SWang Nan 	}
10001669e509SWang Nan }
10011669e509SWang Nan 
10021669e509SWang Nan void parse_events__shrink_config_terms(void)
10031669e509SWang Nan {
10041669e509SWang Nan 	config_term_shrinked = true;
10051669e509SWang Nan }
10061669e509SWang Nan 
10070b8891a8SHe Kuang static int config_term_common(struct perf_event_attr *attr,
10083b0e371cSJiri Olsa 			      struct parse_events_term *term,
10093b0e371cSJiri Olsa 			      struct parse_events_error *err)
10108f707d84SJiri Olsa {
101116fa7e82SJiri Olsa #define CHECK_TYPE_VAL(type)						   \
101216fa7e82SJiri Olsa do {									   \
10133b0e371cSJiri Olsa 	if (check_type_val(term, err, PARSE_EVENTS__TERM_TYPE_ ## type)) \
101416fa7e82SJiri Olsa 		return -EINVAL;						   \
101516fa7e82SJiri Olsa } while (0)
101616fa7e82SJiri Olsa 
101716fa7e82SJiri Olsa 	switch (term->type_term) {
10188f707d84SJiri Olsa 	case PARSE_EVENTS__TERM_TYPE_CONFIG:
101916fa7e82SJiri Olsa 		CHECK_TYPE_VAL(NUM);
10208f707d84SJiri Olsa 		attr->config = term->val.num;
10218f707d84SJiri Olsa 		break;
10228f707d84SJiri Olsa 	case PARSE_EVENTS__TERM_TYPE_CONFIG1:
102316fa7e82SJiri Olsa 		CHECK_TYPE_VAL(NUM);
10248f707d84SJiri Olsa 		attr->config1 = term->val.num;
10258f707d84SJiri Olsa 		break;
10268f707d84SJiri Olsa 	case PARSE_EVENTS__TERM_TYPE_CONFIG2:
102716fa7e82SJiri Olsa 		CHECK_TYPE_VAL(NUM);
10288f707d84SJiri Olsa 		attr->config2 = term->val.num;
10298f707d84SJiri Olsa 		break;
10308f707d84SJiri Olsa 	case PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD:
103116fa7e82SJiri Olsa 		CHECK_TYPE_VAL(NUM);
10328f707d84SJiri Olsa 		break;
103309af2a55SNamhyung Kim 	case PARSE_EVENTS__TERM_TYPE_SAMPLE_FREQ:
103409af2a55SNamhyung Kim 		CHECK_TYPE_VAL(NUM);
103509af2a55SNamhyung Kim 		break;
10368f707d84SJiri Olsa 	case PARSE_EVENTS__TERM_TYPE_BRANCH_SAMPLE_TYPE:
1037ac12f676SAndi Kleen 		CHECK_TYPE_VAL(STR);
1038ac12f676SAndi Kleen 		if (strcmp(term->val.str, "no") &&
1039ac12f676SAndi Kleen 		    parse_branch_str(term->val.str, &attr->branch_sample_type)) {
1040ac12f676SAndi Kleen 			err->str = strdup("invalid branch sample type");
1041ac12f676SAndi Kleen 			err->idx = term->err_val;
1042ac12f676SAndi Kleen 			return -EINVAL;
1043ac12f676SAndi Kleen 		}
10448f707d84SJiri Olsa 		break;
104532067712SKan Liang 	case PARSE_EVENTS__TERM_TYPE_TIME:
104632067712SKan Liang 		CHECK_TYPE_VAL(NUM);
104732067712SKan Liang 		if (term->val.num > 1) {
104832067712SKan Liang 			err->str = strdup("expected 0 or 1");
104932067712SKan Liang 			err->idx = term->err_val;
105032067712SKan Liang 			return -EINVAL;
105132067712SKan Liang 		}
105232067712SKan Liang 		break;
1053d457c963SKan Liang 	case PARSE_EVENTS__TERM_TYPE_CALLGRAPH:
1054d457c963SKan Liang 		CHECK_TYPE_VAL(STR);
1055d457c963SKan Liang 		break;
1056d457c963SKan Liang 	case PARSE_EVENTS__TERM_TYPE_STACKSIZE:
1057d457c963SKan Liang 		CHECK_TYPE_VAL(NUM);
1058d457c963SKan Liang 		break;
1059374ce938SWang Nan 	case PARSE_EVENTS__TERM_TYPE_INHERIT:
1060374ce938SWang Nan 		CHECK_TYPE_VAL(NUM);
1061374ce938SWang Nan 		break;
1062374ce938SWang Nan 	case PARSE_EVENTS__TERM_TYPE_NOINHERIT:
1063374ce938SWang Nan 		CHECK_TYPE_VAL(NUM);
1064374ce938SWang Nan 		break;
1065626a6b78SWang Nan 	case PARSE_EVENTS__TERM_TYPE_OVERWRITE:
1066626a6b78SWang Nan 		CHECK_TYPE_VAL(NUM);
1067626a6b78SWang Nan 		break;
1068626a6b78SWang Nan 	case PARSE_EVENTS__TERM_TYPE_NOOVERWRITE:
1069626a6b78SWang Nan 		CHECK_TYPE_VAL(NUM);
1070626a6b78SWang Nan 		break;
10716b5fc39bSJiri Olsa 	case PARSE_EVENTS__TERM_TYPE_NAME:
10726b5fc39bSJiri Olsa 		CHECK_TYPE_VAL(STR);
10736b5fc39bSJiri Olsa 		break;
1074792d48b4SArnaldo Carvalho de Melo 	case PARSE_EVENTS__TERM_TYPE_MAX_STACK:
1075792d48b4SArnaldo Carvalho de Melo 		CHECK_TYPE_VAL(NUM);
1076792d48b4SArnaldo Carvalho de Melo 		break;
10772fda5adaSArnaldo Carvalho de Melo 	case PARSE_EVENTS__TERM_TYPE_MAX_EVENTS:
10782fda5adaSArnaldo Carvalho de Melo 		CHECK_TYPE_VAL(NUM);
10792fda5adaSArnaldo Carvalho de Melo 		break;
1080064b4e82SJin Yao 	case PARSE_EVENTS__TERM_TYPE_PERCORE:
1081064b4e82SJin Yao 		CHECK_TYPE_VAL(NUM);
1082064b4e82SJin Yao 		if ((unsigned int)term->val.num > 1) {
1083064b4e82SJin Yao 			err->str = strdup("expected 0 or 1");
1084064b4e82SJin Yao 			err->idx = term->err_val;
1085064b4e82SJin Yao 			return -EINVAL;
1086064b4e82SJin Yao 		}
1087064b4e82SJin Yao 		break;
10881b992154SAdrian Hunter 	case PARSE_EVENTS__TERM_TYPE_AUX_OUTPUT:
10891b992154SAdrian Hunter 		CHECK_TYPE_VAL(NUM);
10901b992154SAdrian Hunter 		break;
10918f707d84SJiri Olsa 	default:
1092ffeb883eSHe Kuang 		err->str = strdup("unknown term");
1093ffeb883eSHe Kuang 		err->idx = term->err_term;
1094ffeb883eSHe Kuang 		err->help = parse_events_formats_error_string(NULL);
10958f707d84SJiri Olsa 		return -EINVAL;
10968f707d84SJiri Olsa 	}
109716fa7e82SJiri Olsa 
10981669e509SWang Nan 	/*
10991669e509SWang Nan 	 * Check term availbility after basic checking so
11001669e509SWang Nan 	 * PARSE_EVENTS__TERM_TYPE_USER can be found and filtered.
11011669e509SWang Nan 	 *
11021669e509SWang Nan 	 * If check availbility at the entry of this function,
11031669e509SWang Nan 	 * user will see "'<sysfs term>' is not usable in 'perf stat'"
11041669e509SWang Nan 	 * if an invalid config term is provided for legacy events
11051669e509SWang Nan 	 * (for example, instructions/badterm/...), which is confusing.
11061669e509SWang Nan 	 */
11071669e509SWang Nan 	if (!config_term_avail(term->type_term, err))
11081669e509SWang Nan 		return -EINVAL;
11098f707d84SJiri Olsa 	return 0;
111016fa7e82SJiri Olsa #undef CHECK_TYPE_VAL
11118f707d84SJiri Olsa }
11128f707d84SJiri Olsa 
11130b8891a8SHe Kuang static int config_term_pmu(struct perf_event_attr *attr,
11140b8891a8SHe Kuang 			   struct parse_events_term *term,
11150b8891a8SHe Kuang 			   struct parse_events_error *err)
11160b8891a8SHe Kuang {
1117dd60fba7SMathieu Poirier 	if (term->type_term == PARSE_EVENTS__TERM_TYPE_USER ||
1118dd60fba7SMathieu Poirier 	    term->type_term == PARSE_EVENTS__TERM_TYPE_DRV_CFG)
11190b8891a8SHe Kuang 		/*
11200b8891a8SHe Kuang 		 * Always succeed for sysfs terms, as we dont know
11210b8891a8SHe Kuang 		 * at this point what type they need to have.
11220b8891a8SHe Kuang 		 */
11230b8891a8SHe Kuang 		return 0;
11240b8891a8SHe Kuang 	else
11250b8891a8SHe Kuang 		return config_term_common(attr, term, err);
11260b8891a8SHe Kuang }
11270b8891a8SHe Kuang 
1128e637d177SHe Kuang static int config_term_tracepoint(struct perf_event_attr *attr,
1129e637d177SHe Kuang 				  struct parse_events_term *term,
1130e637d177SHe Kuang 				  struct parse_events_error *err)
1131e637d177SHe Kuang {
1132e637d177SHe Kuang 	switch (term->type_term) {
1133e637d177SHe Kuang 	case PARSE_EVENTS__TERM_TYPE_CALLGRAPH:
1134e637d177SHe Kuang 	case PARSE_EVENTS__TERM_TYPE_STACKSIZE:
1135374ce938SWang Nan 	case PARSE_EVENTS__TERM_TYPE_INHERIT:
1136374ce938SWang Nan 	case PARSE_EVENTS__TERM_TYPE_NOINHERIT:
1137792d48b4SArnaldo Carvalho de Melo 	case PARSE_EVENTS__TERM_TYPE_MAX_STACK:
11382fda5adaSArnaldo Carvalho de Melo 	case PARSE_EVENTS__TERM_TYPE_MAX_EVENTS:
1139626a6b78SWang Nan 	case PARSE_EVENTS__TERM_TYPE_OVERWRITE:
1140626a6b78SWang Nan 	case PARSE_EVENTS__TERM_TYPE_NOOVERWRITE:
11411b992154SAdrian Hunter 	case PARSE_EVENTS__TERM_TYPE_AUX_OUTPUT:
1142e637d177SHe Kuang 		return config_term_common(attr, term, err);
1143e637d177SHe Kuang 	default:
1144e637d177SHe Kuang 		if (err) {
1145e637d177SHe Kuang 			err->idx = term->err_term;
1146e637d177SHe Kuang 			err->str = strdup("unknown term");
1147e637d177SHe Kuang 			err->help = strdup("valid terms: call-graph,stack-size\n");
1148e637d177SHe Kuang 		}
1149e637d177SHe Kuang 		return -EINVAL;
1150e637d177SHe Kuang 	}
1151e637d177SHe Kuang 
1152e637d177SHe Kuang 	return 0;
1153e637d177SHe Kuang }
1154e637d177SHe Kuang 
11558f707d84SJiri Olsa static int config_attr(struct perf_event_attr *attr,
11563b0e371cSJiri Olsa 		       struct list_head *head,
11570b8891a8SHe Kuang 		       struct parse_events_error *err,
11580b8891a8SHe Kuang 		       config_term_func_t config_term)
11598f707d84SJiri Olsa {
11606cee6cd3SArnaldo Carvalho de Melo 	struct parse_events_term *term;
11618f707d84SJiri Olsa 
11628f707d84SJiri Olsa 	list_for_each_entry(term, head, list)
11633b0e371cSJiri Olsa 		if (config_term(attr, term, err))
11648f707d84SJiri Olsa 			return -EINVAL;
11658f707d84SJiri Olsa 
11668f707d84SJiri Olsa 	return 0;
11678f707d84SJiri Olsa }
11688f707d84SJiri Olsa 
1169930a2e29SJiri Olsa static int get_config_terms(struct list_head *head_config,
1170930a2e29SJiri Olsa 			    struct list_head *head_terms __maybe_unused)
1171930a2e29SJiri Olsa {
1172930a2e29SJiri Olsa #define ADD_CONFIG_TERM(__type, __name, __val)			\
1173930a2e29SJiri Olsa do {								\
1174930a2e29SJiri Olsa 	struct perf_evsel_config_term *__t;			\
1175930a2e29SJiri Olsa 								\
1176930a2e29SJiri Olsa 	__t = zalloc(sizeof(*__t));				\
1177930a2e29SJiri Olsa 	if (!__t)						\
1178930a2e29SJiri Olsa 		return -ENOMEM;					\
1179930a2e29SJiri Olsa 								\
1180930a2e29SJiri Olsa 	INIT_LIST_HEAD(&__t->list);				\
1181930a2e29SJiri Olsa 	__t->type       = PERF_EVSEL__CONFIG_TERM_ ## __type;	\
1182930a2e29SJiri Olsa 	__t->val.__name = __val;				\
1183c2f1ceadSAndi Kleen 	__t->weak	= term->weak;				\
1184930a2e29SJiri Olsa 	list_add_tail(&__t->list, head_terms);			\
1185930a2e29SJiri Olsa } while (0)
1186930a2e29SJiri Olsa 
1187930a2e29SJiri Olsa 	struct parse_events_term *term;
1188930a2e29SJiri Olsa 
1189930a2e29SJiri Olsa 	list_for_each_entry(term, head_config, list) {
1190930a2e29SJiri Olsa 		switch (term->type_term) {
1191ee4c7588SJiri Olsa 		case PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD:
1192ee4c7588SJiri Olsa 			ADD_CONFIG_TERM(PERIOD, period, term->val.num);
119332067712SKan Liang 			break;
119409af2a55SNamhyung Kim 		case PARSE_EVENTS__TERM_TYPE_SAMPLE_FREQ:
119509af2a55SNamhyung Kim 			ADD_CONFIG_TERM(FREQ, freq, term->val.num);
119609af2a55SNamhyung Kim 			break;
119732067712SKan Liang 		case PARSE_EVENTS__TERM_TYPE_TIME:
119832067712SKan Liang 			ADD_CONFIG_TERM(TIME, time, term->val.num);
119932067712SKan Liang 			break;
1200d457c963SKan Liang 		case PARSE_EVENTS__TERM_TYPE_CALLGRAPH:
1201d457c963SKan Liang 			ADD_CONFIG_TERM(CALLGRAPH, callgraph, term->val.str);
1202d457c963SKan Liang 			break;
1203ac12f676SAndi Kleen 		case PARSE_EVENTS__TERM_TYPE_BRANCH_SAMPLE_TYPE:
1204ac12f676SAndi Kleen 			ADD_CONFIG_TERM(BRANCH, branch, term->val.str);
1205ac12f676SAndi Kleen 			break;
1206d457c963SKan Liang 		case PARSE_EVENTS__TERM_TYPE_STACKSIZE:
1207d457c963SKan Liang 			ADD_CONFIG_TERM(STACK_USER, stack_user, term->val.num);
1208d457c963SKan Liang 			break;
1209374ce938SWang Nan 		case PARSE_EVENTS__TERM_TYPE_INHERIT:
1210374ce938SWang Nan 			ADD_CONFIG_TERM(INHERIT, inherit, term->val.num ? 1 : 0);
1211374ce938SWang Nan 			break;
1212374ce938SWang Nan 		case PARSE_EVENTS__TERM_TYPE_NOINHERIT:
1213374ce938SWang Nan 			ADD_CONFIG_TERM(INHERIT, inherit, term->val.num ? 0 : 1);
1214374ce938SWang Nan 			break;
1215792d48b4SArnaldo Carvalho de Melo 		case PARSE_EVENTS__TERM_TYPE_MAX_STACK:
1216792d48b4SArnaldo Carvalho de Melo 			ADD_CONFIG_TERM(MAX_STACK, max_stack, term->val.num);
1217792d48b4SArnaldo Carvalho de Melo 			break;
12182fda5adaSArnaldo Carvalho de Melo 		case PARSE_EVENTS__TERM_TYPE_MAX_EVENTS:
12192fda5adaSArnaldo Carvalho de Melo 			ADD_CONFIG_TERM(MAX_EVENTS, max_events, term->val.num);
12202fda5adaSArnaldo Carvalho de Melo 			break;
1221626a6b78SWang Nan 		case PARSE_EVENTS__TERM_TYPE_OVERWRITE:
1222626a6b78SWang Nan 			ADD_CONFIG_TERM(OVERWRITE, overwrite, term->val.num ? 1 : 0);
1223626a6b78SWang Nan 			break;
1224626a6b78SWang Nan 		case PARSE_EVENTS__TERM_TYPE_NOOVERWRITE:
1225626a6b78SWang Nan 			ADD_CONFIG_TERM(OVERWRITE, overwrite, term->val.num ? 0 : 1);
1226626a6b78SWang Nan 			break;
1227dd60fba7SMathieu Poirier 		case PARSE_EVENTS__TERM_TYPE_DRV_CFG:
1228dd60fba7SMathieu Poirier 			ADD_CONFIG_TERM(DRV_CFG, drv_cfg, term->val.str);
1229dd60fba7SMathieu Poirier 			break;
1230064b4e82SJin Yao 		case PARSE_EVENTS__TERM_TYPE_PERCORE:
1231064b4e82SJin Yao 			ADD_CONFIG_TERM(PERCORE, percore,
1232064b4e82SJin Yao 					term->val.num ? true : false);
1233064b4e82SJin Yao 			break;
12341b992154SAdrian Hunter 		case PARSE_EVENTS__TERM_TYPE_AUX_OUTPUT:
12351b992154SAdrian Hunter 			ADD_CONFIG_TERM(AUX_OUTPUT, aux_output, term->val.num ? 1 : 0);
12361b992154SAdrian Hunter 			break;
1237930a2e29SJiri Olsa 		default:
1238930a2e29SJiri Olsa 			break;
1239930a2e29SJiri Olsa 		}
1240930a2e29SJiri Olsa 	}
1241930a2e29SJiri Olsa #undef ADD_EVSEL_CONFIG
1242930a2e29SJiri Olsa 	return 0;
1243930a2e29SJiri Olsa }
1244930a2e29SJiri Olsa 
1245e637d177SHe Kuang int parse_events_add_tracepoint(struct list_head *list, int *idx,
12468c619d6aSWang Nan 				const char *sys, const char *event,
1247272ed29aSJiri Olsa 				struct parse_events_error *err,
1248e637d177SHe Kuang 				struct list_head *head_config)
1249e637d177SHe Kuang {
1250e637d177SHe Kuang 	if (head_config) {
1251e637d177SHe Kuang 		struct perf_event_attr attr;
1252e637d177SHe Kuang 
1253272ed29aSJiri Olsa 		if (config_attr(&attr, head_config, err,
1254e637d177SHe Kuang 				config_term_tracepoint))
1255e637d177SHe Kuang 			return -EINVAL;
1256e637d177SHe Kuang 	}
1257e637d177SHe Kuang 
1258e637d177SHe Kuang 	if (strpbrk(sys, "*?"))
1259e637d177SHe Kuang 		return add_tracepoint_multi_sys(list, idx, sys, event,
1260272ed29aSJiri Olsa 						err, head_config);
1261e637d177SHe Kuang 	else
1262e637d177SHe Kuang 		return add_tracepoint_event(list, idx, sys, event,
1263272ed29aSJiri Olsa 					    err, head_config);
1264e637d177SHe Kuang }
1265e637d177SHe Kuang 
12665d9cdc11SArnaldo Carvalho de Melo int parse_events_add_numeric(struct parse_events_state *parse_state,
126787d650beSJiri Olsa 			     struct list_head *list,
1268b527bab5SRobert Richter 			     u32 type, u64 config,
12698f707d84SJiri Olsa 			     struct list_head *head_config)
127074d5b588SJaswinder Singh Rajput {
127189812fc8SJiri Olsa 	struct perf_event_attr attr;
1272930a2e29SJiri Olsa 	LIST_HEAD(config_terms);
127374d5b588SJaswinder Singh Rajput 
127489812fc8SJiri Olsa 	memset(&attr, 0, sizeof(attr));
127589812fc8SJiri Olsa 	attr.type = type;
127689812fc8SJiri Olsa 	attr.config = config;
12778f707d84SJiri Olsa 
1278930a2e29SJiri Olsa 	if (head_config) {
12795d9cdc11SArnaldo Carvalho de Melo 		if (config_attr(&attr, head_config, parse_state->error,
12800b8891a8SHe Kuang 				config_term_common))
12818f707d84SJiri Olsa 			return -EINVAL;
12828f707d84SJiri Olsa 
1283930a2e29SJiri Olsa 		if (get_config_terms(head_config, &config_terms))
1284930a2e29SJiri Olsa 			return -ENOMEM;
1285930a2e29SJiri Olsa 	}
1286930a2e29SJiri Olsa 
12875d9cdc11SArnaldo Carvalho de Melo 	return add_event(list, &parse_state->idx, &attr,
128810bf358aSWang Nan 			 get_config_name(head_config), &config_terms);
1289b908debdSIngo Molnar }
1290b908debdSIngo Molnar 
1291f0fbb114SAndi Kleen int parse_events_add_tool(struct parse_events_state *parse_state,
1292f0fbb114SAndi Kleen 			  struct list_head *list,
1293f0fbb114SAndi Kleen 			  enum perf_tool_event tool_event)
1294f0fbb114SAndi Kleen {
1295f0fbb114SAndi Kleen 	return add_event_tool(list, &parse_state->idx, tool_event);
1296f0fbb114SAndi Kleen }
1297f0fbb114SAndi Kleen 
1298064b4e82SJin Yao static bool config_term_percore(struct list_head *config_terms)
1299064b4e82SJin Yao {
1300064b4e82SJin Yao 	struct perf_evsel_config_term *term;
1301064b4e82SJin Yao 
1302064b4e82SJin Yao 	list_for_each_entry(term, config_terms, list) {
1303064b4e82SJin Yao 		if (term->type == PERF_EVSEL__CONFIG_TERM_PERCORE)
1304064b4e82SJin Yao 			return term->val.percore;
1305064b4e82SJin Yao 	}
1306064b4e82SJin Yao 
1307064b4e82SJin Yao 	return false;
1308064b4e82SJin Yao }
1309064b4e82SJin Yao 
1310c199c11dSAgustin Vega-Frias int parse_events_add_pmu(struct parse_events_state *parse_state,
131136adec85SJiri Olsa 			 struct list_head *list, char *name,
13123cdc5c2cSKan Liang 			 struct list_head *head_config,
13133cdc5c2cSKan Liang 			 bool auto_merge_stats,
13143cdc5c2cSKan Liang 			 bool use_alias)
13155f537a26SJiri Olsa {
13165f537a26SJiri Olsa 	struct perf_event_attr attr;
131746441bdcSMatt Fleming 	struct perf_pmu_info info;
13185f537a26SJiri Olsa 	struct perf_pmu *pmu;
131932dcd021SJiri Olsa 	struct evsel *evsel;
1320333b5665SAndi Kleen 	struct parse_events_error *err = parse_state->error;
13213cdc5c2cSKan Liang 	bool use_uncore_alias;
1322930a2e29SJiri Olsa 	LIST_HEAD(config_terms);
13235f537a26SJiri Olsa 
13245f537a26SJiri Olsa 	pmu = perf_pmu__find(name);
1325333b5665SAndi Kleen 	if (!pmu) {
1326333b5665SAndi Kleen 		if (asprintf(&err->str,
1327333b5665SAndi Kleen 				"Cannot find PMU `%s'. Missing kernel support?",
1328333b5665SAndi Kleen 				name) < 0)
1329333b5665SAndi Kleen 			err->str = NULL;
13305f537a26SJiri Olsa 		return -EINVAL;
1331333b5665SAndi Kleen 	}
13325f537a26SJiri Olsa 
1333dc0a6202SAdrian Hunter 	if (pmu->default_config) {
1334dc0a6202SAdrian Hunter 		memcpy(&attr, pmu->default_config,
1335dc0a6202SAdrian Hunter 		       sizeof(struct perf_event_attr));
1336dc0a6202SAdrian Hunter 	} else {
13375f537a26SJiri Olsa 		memset(&attr, 0, sizeof(attr));
1338dc0a6202SAdrian Hunter 	}
13395f537a26SJiri Olsa 
13403cdc5c2cSKan Liang 	use_uncore_alias = (pmu->is_uncore && use_alias);
13413cdc5c2cSKan Liang 
1342ad962273SAdrian Hunter 	if (!head_config) {
1343ad962273SAdrian Hunter 		attr.type = pmu->type;
1344f0fbb114SAndi Kleen 		evsel = __add_event(list, &parse_state->idx, &attr, NULL, pmu, NULL,
1345f0fbb114SAndi Kleen 				    auto_merge_stats, NULL);
13468c5421c0SAgustin Vega-Frias 		if (evsel) {
13478c5421c0SAgustin Vega-Frias 			evsel->pmu_name = name;
13483cdc5c2cSKan Liang 			evsel->use_uncore_alias = use_uncore_alias;
13498c5421c0SAgustin Vega-Frias 			return 0;
13508c5421c0SAgustin Vega-Frias 		} else {
13518c5421c0SAgustin Vega-Frias 			return -ENOMEM;
13528c5421c0SAgustin Vega-Frias 		}
1353ad962273SAdrian Hunter 	}
1354ad962273SAdrian Hunter 
135546441bdcSMatt Fleming 	if (perf_pmu__check_alias(pmu, head_config, &info))
1356a6146d50SZheng Yan 		return -EINVAL;
1357a6146d50SZheng Yan 
13585f537a26SJiri Olsa 	/*
13595f537a26SJiri Olsa 	 * Configure hardcoded terms first, no need to check
13605f537a26SJiri Olsa 	 * return value when called with fail == 0 ;)
13615f537a26SJiri Olsa 	 */
13625d9cdc11SArnaldo Carvalho de Melo 	if (config_attr(&attr, head_config, parse_state->error, config_term_pmu))
1363c056ba6aSJiri Olsa 		return -EINVAL;
13645f537a26SJiri Olsa 
1365930a2e29SJiri Olsa 	if (get_config_terms(head_config, &config_terms))
1366930a2e29SJiri Olsa 		return -ENOMEM;
1367930a2e29SJiri Olsa 
13685d9cdc11SArnaldo Carvalho de Melo 	if (perf_pmu__config(pmu, &attr, head_config, parse_state->error))
13695f537a26SJiri Olsa 		return -EINVAL;
13705f537a26SJiri Olsa 
13715d9cdc11SArnaldo Carvalho de Melo 	evsel = __add_event(list, &parse_state->idx, &attr,
137266ec1191SMark Rutland 			    get_config_name(head_config), pmu,
1373f0fbb114SAndi Kleen 			    &config_terms, auto_merge_stats, NULL);
1374410136f5SStephane Eranian 	if (evsel) {
137546441bdcSMatt Fleming 		evsel->unit = info.unit;
137646441bdcSMatt Fleming 		evsel->scale = info.scale;
1377044330c1SMatt Fleming 		evsel->per_pkg = info.per_pkg;
13781d9e446bSJiri Olsa 		evsel->snapshot = info.snapshot;
137937932c18SAndi Kleen 		evsel->metric_expr = info.metric_expr;
138096284814SAndi Kleen 		evsel->metric_name = info.metric_name;
13818c5421c0SAgustin Vega-Frias 		evsel->pmu_name = name;
13823cdc5c2cSKan Liang 		evsel->use_uncore_alias = use_uncore_alias;
1383064b4e82SJin Yao 		evsel->percore = config_term_percore(&evsel->config_terms);
1384410136f5SStephane Eranian 	}
1385410136f5SStephane Eranian 
1386410136f5SStephane Eranian 	return evsel ? 0 : -ENOMEM;
13875f537a26SJiri Olsa }
13885f537a26SJiri Olsa 
13895d9cdc11SArnaldo Carvalho de Melo int parse_events_multi_pmu_add(struct parse_events_state *parse_state,
13902073ad33SAndi Kleen 			       char *str, struct list_head **listp)
13912073ad33SAndi Kleen {
13922073ad33SAndi Kleen 	struct list_head *head;
13932073ad33SAndi Kleen 	struct parse_events_term *term;
13942073ad33SAndi Kleen 	struct list_head *list;
13952073ad33SAndi Kleen 	struct perf_pmu *pmu = NULL;
13962073ad33SAndi Kleen 	int ok = 0;
13972073ad33SAndi Kleen 
13982073ad33SAndi Kleen 	*listp = NULL;
13992073ad33SAndi Kleen 	/* Add it for all PMUs that support the alias */
14002073ad33SAndi Kleen 	list = malloc(sizeof(struct list_head));
14012073ad33SAndi Kleen 	if (!list)
14022073ad33SAndi Kleen 		return -1;
14032073ad33SAndi Kleen 	INIT_LIST_HEAD(list);
14042073ad33SAndi Kleen 	while ((pmu = perf_pmu__scan(pmu)) != NULL) {
14052073ad33SAndi Kleen 		struct perf_pmu_alias *alias;
14062073ad33SAndi Kleen 
14072073ad33SAndi Kleen 		list_for_each_entry(alias, &pmu->aliases, list) {
14082073ad33SAndi Kleen 			if (!strcasecmp(alias->name, str)) {
14092073ad33SAndi Kleen 				head = malloc(sizeof(struct list_head));
14102073ad33SAndi Kleen 				if (!head)
14112073ad33SAndi Kleen 					return -1;
14122073ad33SAndi Kleen 				INIT_LIST_HEAD(head);
14132073ad33SAndi Kleen 				if (parse_events_term__num(&term, PARSE_EVENTS__TERM_TYPE_USER,
14142073ad33SAndi Kleen 							   str, 1, false, &str, NULL) < 0)
14152073ad33SAndi Kleen 					return -1;
14162073ad33SAndi Kleen 				list_add_tail(&term->list, head);
14172073ad33SAndi Kleen 
1418c199c11dSAgustin Vega-Frias 				if (!parse_events_add_pmu(parse_state, list,
14193cdc5c2cSKan Liang 							  pmu->name, head,
14203cdc5c2cSKan Liang 							  true, true)) {
14212073ad33SAndi Kleen 					pr_debug("%s -> %s/%s/\n", str,
14222073ad33SAndi Kleen 						 pmu->name, alias->str);
14232073ad33SAndi Kleen 					ok++;
14242073ad33SAndi Kleen 				}
14252073ad33SAndi Kleen 
14262073ad33SAndi Kleen 				parse_events_terms__delete(head);
14272073ad33SAndi Kleen 			}
14282073ad33SAndi Kleen 		}
14292073ad33SAndi Kleen 	}
14302073ad33SAndi Kleen 	if (!ok)
14312073ad33SAndi Kleen 		return -1;
14322073ad33SAndi Kleen 	*listp = list;
14332073ad33SAndi Kleen 	return 0;
14342073ad33SAndi Kleen }
14352073ad33SAndi Kleen 
14366a4bb04cSJiri Olsa int parse_events__modifier_group(struct list_head *list,
14376a4bb04cSJiri Olsa 				 char *event_mod)
143889efb029SJiri Olsa {
14396a4bb04cSJiri Olsa 	return parse_events__modifier_event(list, event_mod, true);
14406a4bb04cSJiri Olsa }
14416a4bb04cSJiri Olsa 
14423cdc5c2cSKan Liang /*
14433cdc5c2cSKan Liang  * Check if the two uncore PMUs are from the same uncore block
14443cdc5c2cSKan Liang  * The format of the uncore PMU name is uncore_#blockname_#pmuidx
14453cdc5c2cSKan Liang  */
14463cdc5c2cSKan Liang static bool is_same_uncore_block(const char *pmu_name_a, const char *pmu_name_b)
14473cdc5c2cSKan Liang {
14483cdc5c2cSKan Liang 	char *end_a, *end_b;
14493cdc5c2cSKan Liang 
14503cdc5c2cSKan Liang 	end_a = strrchr(pmu_name_a, '_');
14513cdc5c2cSKan Liang 	end_b = strrchr(pmu_name_b, '_');
14523cdc5c2cSKan Liang 
14533cdc5c2cSKan Liang 	if (!end_a || !end_b)
14543cdc5c2cSKan Liang 		return false;
14553cdc5c2cSKan Liang 
14563cdc5c2cSKan Liang 	if ((end_a - pmu_name_a) != (end_b - pmu_name_b))
14573cdc5c2cSKan Liang 		return false;
14583cdc5c2cSKan Liang 
14593cdc5c2cSKan Liang 	return (strncmp(pmu_name_a, pmu_name_b, end_a - pmu_name_a) == 0);
14603cdc5c2cSKan Liang }
14613cdc5c2cSKan Liang 
14623cdc5c2cSKan Liang static int
14633cdc5c2cSKan Liang parse_events__set_leader_for_uncore_aliase(char *name, struct list_head *list,
14643cdc5c2cSKan Liang 					   struct parse_events_state *parse_state)
14653cdc5c2cSKan Liang {
146632dcd021SJiri Olsa 	struct evsel *evsel, *leader;
14673cdc5c2cSKan Liang 	uintptr_t *leaders;
14683cdc5c2cSKan Liang 	bool is_leader = true;
14693cdc5c2cSKan Liang 	int i, nr_pmu = 0, total_members, ret = 0;
14703cdc5c2cSKan Liang 
1471b27c4eceSJiri Olsa 	leader = list_first_entry(list, struct evsel, core.node);
1472b27c4eceSJiri Olsa 	evsel = list_last_entry(list, struct evsel, core.node);
14733cdc5c2cSKan Liang 	total_members = evsel->idx - leader->idx + 1;
14743cdc5c2cSKan Liang 
14753cdc5c2cSKan Liang 	leaders = calloc(total_members, sizeof(uintptr_t));
14763cdc5c2cSKan Liang 	if (WARN_ON(!leaders))
14773cdc5c2cSKan Liang 		return 0;
14783cdc5c2cSKan Liang 
14793cdc5c2cSKan Liang 	/*
14803cdc5c2cSKan Liang 	 * Going through the whole group and doing sanity check.
14813cdc5c2cSKan Liang 	 * All members must use alias, and be from the same uncore block.
14823cdc5c2cSKan Liang 	 * Also, storing the leader events in an array.
14833cdc5c2cSKan Liang 	 */
14843cdc5c2cSKan Liang 	__evlist__for_each_entry(list, evsel) {
14853cdc5c2cSKan Liang 
14863cdc5c2cSKan Liang 		/* Only split the uncore group which members use alias */
14873cdc5c2cSKan Liang 		if (!evsel->use_uncore_alias)
14883cdc5c2cSKan Liang 			goto out;
14893cdc5c2cSKan Liang 
14903cdc5c2cSKan Liang 		/* The events must be from the same uncore block */
14913cdc5c2cSKan Liang 		if (!is_same_uncore_block(leader->pmu_name, evsel->pmu_name))
14923cdc5c2cSKan Liang 			goto out;
14933cdc5c2cSKan Liang 
14943cdc5c2cSKan Liang 		if (!is_leader)
14953cdc5c2cSKan Liang 			continue;
14963cdc5c2cSKan Liang 		/*
14973cdc5c2cSKan Liang 		 * If the event's PMU name starts to repeat, it must be a new
14983cdc5c2cSKan Liang 		 * event. That can be used to distinguish the leader from
14993cdc5c2cSKan Liang 		 * other members, even they have the same event name.
15003cdc5c2cSKan Liang 		 */
15013cdc5c2cSKan Liang 		if ((leader != evsel) && (leader->pmu_name == evsel->pmu_name)) {
15023cdc5c2cSKan Liang 			is_leader = false;
15033cdc5c2cSKan Liang 			continue;
15043cdc5c2cSKan Liang 		}
15053cdc5c2cSKan Liang 		/* The name is always alias name */
15063cdc5c2cSKan Liang 		WARN_ON(strcmp(leader->name, evsel->name));
15073cdc5c2cSKan Liang 
15083cdc5c2cSKan Liang 		/* Store the leader event for each PMU */
15093cdc5c2cSKan Liang 		leaders[nr_pmu++] = (uintptr_t) evsel;
15103cdc5c2cSKan Liang 	}
15113cdc5c2cSKan Liang 
15123cdc5c2cSKan Liang 	/* only one event alias */
15133cdc5c2cSKan Liang 	if (nr_pmu == total_members) {
15143cdc5c2cSKan Liang 		parse_state->nr_groups--;
15153cdc5c2cSKan Liang 		goto handled;
15163cdc5c2cSKan Liang 	}
15173cdc5c2cSKan Liang 
15183cdc5c2cSKan Liang 	/*
15193cdc5c2cSKan Liang 	 * An uncore event alias is a joint name which means the same event
15203cdc5c2cSKan Liang 	 * runs on all PMUs of a block.
15213cdc5c2cSKan Liang 	 * Perf doesn't support mixed events from different PMUs in the same
15223cdc5c2cSKan Liang 	 * group. The big group has to be split into multiple small groups
15233cdc5c2cSKan Liang 	 * which only include the events from the same PMU.
15243cdc5c2cSKan Liang 	 *
15253cdc5c2cSKan Liang 	 * Here the uncore event aliases must be from the same uncore block.
15263cdc5c2cSKan Liang 	 * The number of PMUs must be same for each alias. The number of new
15273cdc5c2cSKan Liang 	 * small groups equals to the number of PMUs.
15283cdc5c2cSKan Liang 	 * Setting the leader event for corresponding members in each group.
15293cdc5c2cSKan Liang 	 */
15303cdc5c2cSKan Liang 	i = 0;
15313cdc5c2cSKan Liang 	__evlist__for_each_entry(list, evsel) {
15323cdc5c2cSKan Liang 		if (i >= nr_pmu)
15333cdc5c2cSKan Liang 			i = 0;
153432dcd021SJiri Olsa 		evsel->leader = (struct evsel *) leaders[i++];
15353cdc5c2cSKan Liang 	}
15363cdc5c2cSKan Liang 
15373cdc5c2cSKan Liang 	/* The number of members and group name are same for each group */
15383cdc5c2cSKan Liang 	for (i = 0; i < nr_pmu; i++) {
153932dcd021SJiri Olsa 		evsel = (struct evsel *) leaders[i];
15405643b1a5SJiri Olsa 		evsel->core.nr_members = total_members / nr_pmu;
15413cdc5c2cSKan Liang 		evsel->group_name = name ? strdup(name) : NULL;
15423cdc5c2cSKan Liang 	}
15433cdc5c2cSKan Liang 
15443cdc5c2cSKan Liang 	/* Take the new small groups into account */
15453cdc5c2cSKan Liang 	parse_state->nr_groups += nr_pmu - 1;
15463cdc5c2cSKan Liang 
15473cdc5c2cSKan Liang handled:
15483cdc5c2cSKan Liang 	ret = 1;
15493cdc5c2cSKan Liang out:
15503cdc5c2cSKan Liang 	free(leaders);
15513cdc5c2cSKan Liang 	return ret;
15523cdc5c2cSKan Liang }
15533cdc5c2cSKan Liang 
15543cdc5c2cSKan Liang void parse_events__set_leader(char *name, struct list_head *list,
15553cdc5c2cSKan Liang 			      struct parse_events_state *parse_state)
15566a4bb04cSJiri Olsa {
155732dcd021SJiri Olsa 	struct evsel *leader;
15586a4bb04cSJiri Olsa 
1559854f7363SWang Nan 	if (list_empty(list)) {
1560854f7363SWang Nan 		WARN_ONCE(true, "WARNING: failed to set leader: empty list");
1561854f7363SWang Nan 		return;
1562854f7363SWang Nan 	}
1563854f7363SWang Nan 
15643cdc5c2cSKan Liang 	if (parse_events__set_leader_for_uncore_aliase(name, list, parse_state))
15653cdc5c2cSKan Liang 		return;
15663cdc5c2cSKan Liang 
156763dab225SArnaldo Carvalho de Melo 	__perf_evlist__set_leader(list);
1568b27c4eceSJiri Olsa 	leader = list_entry(list->next, struct evsel, core.node);
15696a4bb04cSJiri Olsa 	leader->group_name = name ? strdup(name) : NULL;
157089efb029SJiri Olsa }
157189efb029SJiri Olsa 
1572c5cd8ac0SDavid Ahern /* list_event is assumed to point to malloc'ed memory */
15735d7be90eSJiri Olsa void parse_events_update_lists(struct list_head *list_event,
15745d7be90eSJiri Olsa 			       struct list_head *list_all)
15755d7be90eSJiri Olsa {
15765d7be90eSJiri Olsa 	/*
15775d7be90eSJiri Olsa 	 * Called for single event definition. Update the
157889efb029SJiri Olsa 	 * 'all event' list, and reinit the 'single event'
15795d7be90eSJiri Olsa 	 * list, for next event definition.
15805d7be90eSJiri Olsa 	 */
15815d7be90eSJiri Olsa 	list_splice_tail(list_event, list_all);
1582b847cbdcSJiri Olsa 	free(list_event);
15835d7be90eSJiri Olsa }
15845d7be90eSJiri Olsa 
1585f5b1135bSJiri Olsa struct event_modifier {
1586f5b1135bSJiri Olsa 	int eu;
1587f5b1135bSJiri Olsa 	int ek;
1588f5b1135bSJiri Olsa 	int eh;
1589f5b1135bSJiri Olsa 	int eH;
1590f5b1135bSJiri Olsa 	int eG;
1591a1e12da4SJiri Olsa 	int eI;
1592f5b1135bSJiri Olsa 	int precise;
15937f94af7aSJiri Olsa 	int precise_max;
1594f5b1135bSJiri Olsa 	int exclude_GH;
15953c176311SJiri Olsa 	int sample_read;
1596e9a7c414SMichael Ellerman 	int pinned;
15975a5dfe4bSAndi Kleen 	int weak;
1598f5b1135bSJiri Olsa };
159961c45981SPaul Mackerras 
1600f5b1135bSJiri Olsa static int get_event_modifier(struct event_modifier *mod, char *str,
160132dcd021SJiri Olsa 			       struct evsel *evsel)
1602f5b1135bSJiri Olsa {
16031fc632ceSJiri Olsa 	int eu = evsel ? evsel->core.attr.exclude_user : 0;
16041fc632ceSJiri Olsa 	int ek = evsel ? evsel->core.attr.exclude_kernel : 0;
16051fc632ceSJiri Olsa 	int eh = evsel ? evsel->core.attr.exclude_hv : 0;
16061fc632ceSJiri Olsa 	int eH = evsel ? evsel->core.attr.exclude_host : 0;
16071fc632ceSJiri Olsa 	int eG = evsel ? evsel->core.attr.exclude_guest : 0;
16081fc632ceSJiri Olsa 	int eI = evsel ? evsel->core.attr.exclude_idle : 0;
16091fc632ceSJiri Olsa 	int precise = evsel ? evsel->core.attr.precise_ip : 0;
16107f94af7aSJiri Olsa 	int precise_max = 0;
16113c176311SJiri Olsa 	int sample_read = 0;
16121fc632ceSJiri Olsa 	int pinned = evsel ? evsel->core.attr.pinned : 0;
1613f5b1135bSJiri Olsa 
1614f5b1135bSJiri Olsa 	int exclude = eu | ek | eh;
1615f5b1135bSJiri Olsa 	int exclude_GH = evsel ? evsel->exclude_GH : 0;
16165a5dfe4bSAndi Kleen 	int weak = 0;
1617f5b1135bSJiri Olsa 
1618f5b1135bSJiri Olsa 	memset(mod, 0, sizeof(*mod));
1619ceb53fbfSIngo Molnar 
162061c45981SPaul Mackerras 	while (*str) {
1621ab608344SPeter Zijlstra 		if (*str == 'u') {
1622ab608344SPeter Zijlstra 			if (!exclude)
1623ab608344SPeter Zijlstra 				exclude = eu = ek = eh = 1;
162461c45981SPaul Mackerras 			eu = 0;
1625ab608344SPeter Zijlstra 		} else if (*str == 'k') {
1626ab608344SPeter Zijlstra 			if (!exclude)
1627ab608344SPeter Zijlstra 				exclude = eu = ek = eh = 1;
162861c45981SPaul Mackerras 			ek = 0;
1629ab608344SPeter Zijlstra 		} else if (*str == 'h') {
1630ab608344SPeter Zijlstra 			if (!exclude)
1631ab608344SPeter Zijlstra 				exclude = eu = ek = eh = 1;
163261c45981SPaul Mackerras 			eh = 0;
163399320cc8SJoerg Roedel 		} else if (*str == 'G') {
163499320cc8SJoerg Roedel 			if (!exclude_GH)
163599320cc8SJoerg Roedel 				exclude_GH = eG = eH = 1;
163699320cc8SJoerg Roedel 			eG = 0;
163799320cc8SJoerg Roedel 		} else if (*str == 'H') {
163899320cc8SJoerg Roedel 			if (!exclude_GH)
163999320cc8SJoerg Roedel 				exclude_GH = eG = eH = 1;
164099320cc8SJoerg Roedel 			eH = 0;
1641a1e12da4SJiri Olsa 		} else if (*str == 'I') {
1642a1e12da4SJiri Olsa 			eI = 1;
1643ab608344SPeter Zijlstra 		} else if (*str == 'p') {
1644ab608344SPeter Zijlstra 			precise++;
16451342798cSDavid Ahern 			/* use of precise requires exclude_guest */
16461342798cSDavid Ahern 			if (!exclude_GH)
16471342798cSDavid Ahern 				eG = 1;
16487f94af7aSJiri Olsa 		} else if (*str == 'P') {
16497f94af7aSJiri Olsa 			precise_max = 1;
16503c176311SJiri Olsa 		} else if (*str == 'S') {
16513c176311SJiri Olsa 			sample_read = 1;
1652e9a7c414SMichael Ellerman 		} else if (*str == 'D') {
1653e9a7c414SMichael Ellerman 			pinned = 1;
16545a5dfe4bSAndi Kleen 		} else if (*str == 'W') {
16555a5dfe4bSAndi Kleen 			weak = 1;
1656ab608344SPeter Zijlstra 		} else
165761c45981SPaul Mackerras 			break;
1658ab608344SPeter Zijlstra 
165961c45981SPaul Mackerras 		++str;
166061c45981SPaul Mackerras 	}
166174d5b588SJaswinder Singh Rajput 
166286470930SIngo Molnar 	/*
166389812fc8SJiri Olsa 	 * precise ip:
166489812fc8SJiri Olsa 	 *
166589812fc8SJiri Olsa 	 *  0 - SAMPLE_IP can have arbitrary skid
166689812fc8SJiri Olsa 	 *  1 - SAMPLE_IP must have constant skid
166789812fc8SJiri Olsa 	 *  2 - SAMPLE_IP requested to have 0 skid
166889812fc8SJiri Olsa 	 *  3 - SAMPLE_IP must have 0 skid
166989812fc8SJiri Olsa 	 *
167089812fc8SJiri Olsa 	 *  See also PERF_RECORD_MISC_EXACT_IP
167186470930SIngo Molnar 	 */
167289812fc8SJiri Olsa 	if (precise > 3)
167389812fc8SJiri Olsa 		return -EINVAL;
167486470930SIngo Molnar 
1675f5b1135bSJiri Olsa 	mod->eu = eu;
1676f5b1135bSJiri Olsa 	mod->ek = ek;
1677f5b1135bSJiri Olsa 	mod->eh = eh;
1678f5b1135bSJiri Olsa 	mod->eH = eH;
1679f5b1135bSJiri Olsa 	mod->eG = eG;
1680a1e12da4SJiri Olsa 	mod->eI = eI;
1681f5b1135bSJiri Olsa 	mod->precise = precise;
16827f94af7aSJiri Olsa 	mod->precise_max = precise_max;
1683f5b1135bSJiri Olsa 	mod->exclude_GH = exclude_GH;
16843c176311SJiri Olsa 	mod->sample_read = sample_read;
1685e9a7c414SMichael Ellerman 	mod->pinned = pinned;
16865a5dfe4bSAndi Kleen 	mod->weak = weak;
1687e9a7c414SMichael Ellerman 
1688f5b1135bSJiri Olsa 	return 0;
1689f5b1135bSJiri Olsa }
1690f5b1135bSJiri Olsa 
1691534123f4SJiri Olsa /*
1692534123f4SJiri Olsa  * Basic modifier sanity check to validate it contains only one
1693534123f4SJiri Olsa  * instance of any modifier (apart from 'p') present.
1694534123f4SJiri Olsa  */
1695534123f4SJiri Olsa static int check_modifier(char *str)
1696534123f4SJiri Olsa {
1697534123f4SJiri Olsa 	char *p = str;
1698534123f4SJiri Olsa 
1699534123f4SJiri Olsa 	/* The sizeof includes 0 byte as well. */
17005a5dfe4bSAndi Kleen 	if (strlen(str) > (sizeof("ukhGHpppPSDIW") - 1))
1701534123f4SJiri Olsa 		return -1;
1702534123f4SJiri Olsa 
1703534123f4SJiri Olsa 	while (*p) {
1704534123f4SJiri Olsa 		if (*p != 'p' && strchr(p + 1, *p))
1705534123f4SJiri Olsa 			return -1;
1706534123f4SJiri Olsa 		p++;
1707534123f4SJiri Olsa 	}
1708534123f4SJiri Olsa 
1709534123f4SJiri Olsa 	return 0;
1710534123f4SJiri Olsa }
1711534123f4SJiri Olsa 
1712f5b1135bSJiri Olsa int parse_events__modifier_event(struct list_head *list, char *str, bool add)
1713f5b1135bSJiri Olsa {
171432dcd021SJiri Olsa 	struct evsel *evsel;
1715f5b1135bSJiri Olsa 	struct event_modifier mod;
1716f5b1135bSJiri Olsa 
1717f5b1135bSJiri Olsa 	if (str == NULL)
1718f5b1135bSJiri Olsa 		return 0;
1719f5b1135bSJiri Olsa 
1720534123f4SJiri Olsa 	if (check_modifier(str))
1721534123f4SJiri Olsa 		return -EINVAL;
1722534123f4SJiri Olsa 
1723f5b1135bSJiri Olsa 	if (!add && get_event_modifier(&mod, str, NULL))
1724f5b1135bSJiri Olsa 		return -EINVAL;
1725f5b1135bSJiri Olsa 
1726e5cadb93SArnaldo Carvalho de Melo 	__evlist__for_each_entry(list, evsel) {
1727f5b1135bSJiri Olsa 		if (add && get_event_modifier(&mod, str, evsel))
1728f5b1135bSJiri Olsa 			return -EINVAL;
1729f5b1135bSJiri Olsa 
17301fc632ceSJiri Olsa 		evsel->core.attr.exclude_user   = mod.eu;
17311fc632ceSJiri Olsa 		evsel->core.attr.exclude_kernel = mod.ek;
17321fc632ceSJiri Olsa 		evsel->core.attr.exclude_hv     = mod.eh;
17331fc632ceSJiri Olsa 		evsel->core.attr.precise_ip     = mod.precise;
17341fc632ceSJiri Olsa 		evsel->core.attr.exclude_host   = mod.eH;
17351fc632ceSJiri Olsa 		evsel->core.attr.exclude_guest  = mod.eG;
17361fc632ceSJiri Olsa 		evsel->core.attr.exclude_idle   = mod.eI;
1737f5b1135bSJiri Olsa 		evsel->exclude_GH          = mod.exclude_GH;
17383c176311SJiri Olsa 		evsel->sample_read         = mod.sample_read;
17397f94af7aSJiri Olsa 		evsel->precise_max         = mod.precise_max;
17405a5dfe4bSAndi Kleen 		evsel->weak_group	   = mod.weak;
1741e9a7c414SMichael Ellerman 
1742e9a7c414SMichael Ellerman 		if (perf_evsel__is_group_leader(evsel))
17431fc632ceSJiri Olsa 			evsel->core.attr.pinned = mod.pinned;
1744ceb53fbfSIngo Molnar 	}
174586470930SIngo Molnar 
174689812fc8SJiri Olsa 	return 0;
174786470930SIngo Molnar }
174886470930SIngo Molnar 
1749ac2ba9f3SRobert Richter int parse_events_name(struct list_head *list, char *name)
1750ac2ba9f3SRobert Richter {
175132dcd021SJiri Olsa 	struct evsel *evsel;
1752ac2ba9f3SRobert Richter 
1753e5cadb93SArnaldo Carvalho de Melo 	__evlist__for_each_entry(list, evsel) {
1754ac2ba9f3SRobert Richter 		if (!evsel->name)
1755ac2ba9f3SRobert Richter 			evsel->name = strdup(name);
1756ac2ba9f3SRobert Richter 	}
1757ac2ba9f3SRobert Richter 
1758ac2ba9f3SRobert Richter 	return 0;
1759ac2ba9f3SRobert Richter }
1760ac2ba9f3SRobert Richter 
1761dcb4e102SKan Liang static int
1762dcb4e102SKan Liang comp_pmu(const void *p1, const void *p2)
1763dcb4e102SKan Liang {
1764dcb4e102SKan Liang 	struct perf_pmu_event_symbol *pmu1 = (struct perf_pmu_event_symbol *) p1;
1765dcb4e102SKan Liang 	struct perf_pmu_event_symbol *pmu2 = (struct perf_pmu_event_symbol *) p2;
1766dcb4e102SKan Liang 
1767e312bcf1SAndi Kleen 	return strcasecmp(pmu1->symbol, pmu2->symbol);
1768dcb4e102SKan Liang }
1769dcb4e102SKan Liang 
1770dcb4e102SKan Liang static void perf_pmu__parse_cleanup(void)
1771dcb4e102SKan Liang {
1772dcb4e102SKan Liang 	if (perf_pmu_events_list_num > 0) {
1773dcb4e102SKan Liang 		struct perf_pmu_event_symbol *p;
1774dcb4e102SKan Liang 		int i;
1775dcb4e102SKan Liang 
1776dcb4e102SKan Liang 		for (i = 0; i < perf_pmu_events_list_num; i++) {
1777dcb4e102SKan Liang 			p = perf_pmu_events_list + i;
1778360e071bSTaeung Song 			zfree(&p->symbol);
1779dcb4e102SKan Liang 		}
1780506fde11STaeung Song 		zfree(&perf_pmu_events_list);
1781dcb4e102SKan Liang 		perf_pmu_events_list_num = 0;
1782dcb4e102SKan Liang 	}
1783dcb4e102SKan Liang }
1784dcb4e102SKan Liang 
1785dcb4e102SKan Liang #define SET_SYMBOL(str, stype)		\
1786dcb4e102SKan Liang do {					\
1787dcb4e102SKan Liang 	p->symbol = str;		\
1788dcb4e102SKan Liang 	if (!p->symbol)			\
1789dcb4e102SKan Liang 		goto err;		\
1790dcb4e102SKan Liang 	p->type = stype;		\
1791dcb4e102SKan Liang } while (0)
1792dcb4e102SKan Liang 
1793dcb4e102SKan Liang /*
1794dcb4e102SKan Liang  * Read the pmu events list from sysfs
1795dcb4e102SKan Liang  * Save it into perf_pmu_events_list
1796dcb4e102SKan Liang  */
1797dcb4e102SKan Liang static void perf_pmu__parse_init(void)
1798dcb4e102SKan Liang {
1799dcb4e102SKan Liang 
1800dcb4e102SKan Liang 	struct perf_pmu *pmu = NULL;
1801dcb4e102SKan Liang 	struct perf_pmu_alias *alias;
1802dcb4e102SKan Liang 	int len = 0;
1803dcb4e102SKan Liang 
1804231bb2aaSAndi Kleen 	pmu = NULL;
1805231bb2aaSAndi Kleen 	while ((pmu = perf_pmu__scan(pmu)) != NULL) {
1806dcb4e102SKan Liang 		list_for_each_entry(alias, &pmu->aliases, list) {
1807dcb4e102SKan Liang 			if (strchr(alias->name, '-'))
1808dcb4e102SKan Liang 				len++;
1809dcb4e102SKan Liang 			len++;
1810dcb4e102SKan Liang 		}
1811231bb2aaSAndi Kleen 	}
1812231bb2aaSAndi Kleen 
1813231bb2aaSAndi Kleen 	if (len == 0) {
1814231bb2aaSAndi Kleen 		perf_pmu_events_list_num = -1;
1815231bb2aaSAndi Kleen 		return;
1816231bb2aaSAndi Kleen 	}
1817dcb4e102SKan Liang 	perf_pmu_events_list = malloc(sizeof(struct perf_pmu_event_symbol) * len);
1818dcb4e102SKan Liang 	if (!perf_pmu_events_list)
1819dcb4e102SKan Liang 		return;
1820dcb4e102SKan Liang 	perf_pmu_events_list_num = len;
1821dcb4e102SKan Liang 
1822dcb4e102SKan Liang 	len = 0;
1823231bb2aaSAndi Kleen 	pmu = NULL;
1824231bb2aaSAndi Kleen 	while ((pmu = perf_pmu__scan(pmu)) != NULL) {
1825dcb4e102SKan Liang 		list_for_each_entry(alias, &pmu->aliases, list) {
1826dcb4e102SKan Liang 			struct perf_pmu_event_symbol *p = perf_pmu_events_list + len;
1827dcb4e102SKan Liang 			char *tmp = strchr(alias->name, '-');
1828dcb4e102SKan Liang 
1829dcb4e102SKan Liang 			if (tmp != NULL) {
1830dcb4e102SKan Liang 				SET_SYMBOL(strndup(alias->name, tmp - alias->name),
1831dcb4e102SKan Liang 						PMU_EVENT_SYMBOL_PREFIX);
1832dcb4e102SKan Liang 				p++;
1833dcb4e102SKan Liang 				SET_SYMBOL(strdup(++tmp), PMU_EVENT_SYMBOL_SUFFIX);
1834dcb4e102SKan Liang 				len += 2;
1835dcb4e102SKan Liang 			} else {
1836dcb4e102SKan Liang 				SET_SYMBOL(strdup(alias->name), PMU_EVENT_SYMBOL);
1837dcb4e102SKan Liang 				len++;
1838dcb4e102SKan Liang 			}
1839dcb4e102SKan Liang 		}
1840231bb2aaSAndi Kleen 	}
1841dcb4e102SKan Liang 	qsort(perf_pmu_events_list, len,
1842dcb4e102SKan Liang 		sizeof(struct perf_pmu_event_symbol), comp_pmu);
1843dcb4e102SKan Liang 
1844dcb4e102SKan Liang 	return;
1845dcb4e102SKan Liang err:
1846dcb4e102SKan Liang 	perf_pmu__parse_cleanup();
1847dcb4e102SKan Liang }
1848dcb4e102SKan Liang 
1849dcb4e102SKan Liang enum perf_pmu_event_symbol_type
1850dcb4e102SKan Liang perf_pmu__parse_check(const char *name)
1851dcb4e102SKan Liang {
1852dcb4e102SKan Liang 	struct perf_pmu_event_symbol p, *r;
1853dcb4e102SKan Liang 
1854dcb4e102SKan Liang 	/* scan kernel pmu events from sysfs if needed */
1855dcb4e102SKan Liang 	if (perf_pmu_events_list_num == 0)
1856dcb4e102SKan Liang 		perf_pmu__parse_init();
1857dcb4e102SKan Liang 	/*
1858dcb4e102SKan Liang 	 * name "cpu" could be prefix of cpu-cycles or cpu// events.
1859dcb4e102SKan Liang 	 * cpu-cycles has been handled by hardcode.
1860dcb4e102SKan Liang 	 * So it must be cpu// events, not kernel pmu event.
1861dcb4e102SKan Liang 	 */
1862dcb4e102SKan Liang 	if ((perf_pmu_events_list_num <= 0) || !strcmp(name, "cpu"))
1863dcb4e102SKan Liang 		return PMU_EVENT_SYMBOL_ERR;
1864dcb4e102SKan Liang 
1865dcb4e102SKan Liang 	p.symbol = strdup(name);
1866dcb4e102SKan Liang 	r = bsearch(&p, perf_pmu_events_list,
1867dcb4e102SKan Liang 			(size_t) perf_pmu_events_list_num,
1868dcb4e102SKan Liang 			sizeof(struct perf_pmu_event_symbol), comp_pmu);
1869360e071bSTaeung Song 	zfree(&p.symbol);
1870dcb4e102SKan Liang 	return r ? r->type : PMU_EVENT_SYMBOL_ERR;
1871dcb4e102SKan Liang }
1872dcb4e102SKan Liang 
18735d9cdc11SArnaldo Carvalho de Melo static int parse_events__scanner(const char *str, void *parse_state, int start_token)
1874ac20de6fSZheng Yan {
1875ac20de6fSZheng Yan 	YY_BUFFER_STATE buffer;
1876ac20de6fSZheng Yan 	void *scanner;
1877ac20de6fSZheng Yan 	int ret;
1878ac20de6fSZheng Yan 
187990e2b22dSJiri Olsa 	ret = parse_events_lex_init_extra(start_token, &scanner);
1880ac20de6fSZheng Yan 	if (ret)
1881ac20de6fSZheng Yan 		return ret;
1882ac20de6fSZheng Yan 
1883ac20de6fSZheng Yan 	buffer = parse_events__scan_string(str, scanner);
1884ac20de6fSZheng Yan 
1885ac20de6fSZheng Yan #ifdef PARSER_DEBUG
1886ac20de6fSZheng Yan 	parse_events_debug = 1;
1887ac20de6fSZheng Yan #endif
18885d9cdc11SArnaldo Carvalho de Melo 	ret = parse_events_parse(parse_state, scanner);
1889ac20de6fSZheng Yan 
1890ac20de6fSZheng Yan 	parse_events__flush_buffer(buffer, scanner);
1891ac20de6fSZheng Yan 	parse_events__delete_buffer(buffer, scanner);
1892ac20de6fSZheng Yan 	parse_events_lex_destroy(scanner);
1893ac20de6fSZheng Yan 	return ret;
1894ac20de6fSZheng Yan }
1895ac20de6fSZheng Yan 
189690e2b22dSJiri Olsa /*
189790e2b22dSJiri Olsa  * parse event config string, return a list of event terms.
189890e2b22dSJiri Olsa  */
189990e2b22dSJiri Olsa int parse_events_terms(struct list_head *terms, const char *str)
190090e2b22dSJiri Olsa {
19015d9cdc11SArnaldo Carvalho de Melo 	struct parse_events_state parse_state = {
190290e2b22dSJiri Olsa 		.terms = NULL,
190390e2b22dSJiri Olsa 	};
190490e2b22dSJiri Olsa 	int ret;
190590e2b22dSJiri Olsa 
19065d9cdc11SArnaldo Carvalho de Melo 	ret = parse_events__scanner(str, &parse_state, PE_START_TERMS);
190790e2b22dSJiri Olsa 	if (!ret) {
19085d9cdc11SArnaldo Carvalho de Melo 		list_splice(parse_state.terms, terms);
19095d9cdc11SArnaldo Carvalho de Melo 		zfree(&parse_state.terms);
191090e2b22dSJiri Olsa 		return 0;
191190e2b22dSJiri Olsa 	}
191290e2b22dSJiri Olsa 
19135d9cdc11SArnaldo Carvalho de Melo 	parse_events_terms__delete(parse_state.terms);
191490e2b22dSJiri Olsa 	return ret;
191590e2b22dSJiri Olsa }
191690e2b22dSJiri Olsa 
191763503dbaSJiri Olsa int parse_events(struct evlist *evlist, const char *str,
1918b39b8393SJiri Olsa 		 struct parse_events_error *err)
191986470930SIngo Molnar {
19205d9cdc11SArnaldo Carvalho de Melo 	struct parse_events_state parse_state = {
19215d9cdc11SArnaldo Carvalho de Melo 		.list   = LIST_HEAD_INIT(parse_state.list),
19226484d2f9SJiri Olsa 		.idx    = evlist->core.nr_entries,
1923b39b8393SJiri Olsa 		.error  = err,
19247630b3e2SWang Nan 		.evlist = evlist,
192546010ab2SJiri Olsa 	};
192646010ab2SJiri Olsa 	int ret;
192786470930SIngo Molnar 
19285d9cdc11SArnaldo Carvalho de Melo 	ret = parse_events__scanner(str, &parse_state, PE_START_EVENTS);
1929dcb4e102SKan Liang 	perf_pmu__parse_cleanup();
193015bfd2ccSWang Nan 
1931*8e8714c3SIan Rogers 	if (!ret && list_empty(&parse_state.list)) {
1932c23080a6SArnaldo Carvalho de Melo 		WARN_ONCE(true, "WARNING: event parser found nothing\n");
1933854f7363SWang Nan 		return -1;
1934854f7363SWang Nan 	}
1935854f7363SWang Nan 
1936*8e8714c3SIan Rogers 	/*
1937*8e8714c3SIan Rogers 	 * Add list to the evlist even with errors to allow callers to clean up.
1938*8e8714c3SIan Rogers 	 */
19395d9cdc11SArnaldo Carvalho de Melo 	perf_evlist__splice_list_tail(evlist, &parse_state.list);
1940*8e8714c3SIan Rogers 
1941*8e8714c3SIan Rogers 	if (!ret) {
1942*8e8714c3SIan Rogers 		struct evsel *last;
1943*8e8714c3SIan Rogers 
19445d9cdc11SArnaldo Carvalho de Melo 		evlist->nr_groups += parse_state.nr_groups;
1945515dbe48SJiri Olsa 		last = evlist__last(evlist);
194615bfd2ccSWang Nan 		last->cmdline_group_boundary = true;
194715bfd2ccSWang Nan 
194886470930SIngo Molnar 		return 0;
194986470930SIngo Molnar 	}
195086470930SIngo Molnar 
19515d7be90eSJiri Olsa 	/*
19525d7be90eSJiri Olsa 	 * There are 2 users - builtin-record and builtin-test objects.
1953c12995a5SJiri Olsa 	 * Both call evlist__delete in case of error, so we dont
19545d7be90eSJiri Olsa 	 * need to bother.
19555d7be90eSJiri Olsa 	 */
195689812fc8SJiri Olsa 	return ret;
195789812fc8SJiri Olsa }
195889812fc8SJiri Olsa 
1959b39b8393SJiri Olsa #define MAX_WIDTH 1000
1960b39b8393SJiri Olsa static int get_term_width(void)
1961b39b8393SJiri Olsa {
1962b39b8393SJiri Olsa 	struct winsize ws;
1963b39b8393SJiri Olsa 
1964b39b8393SJiri Olsa 	get_term_dimensions(&ws);
1965b39b8393SJiri Olsa 	return ws.ws_col > MAX_WIDTH ? MAX_WIDTH : ws.ws_col;
1966b39b8393SJiri Olsa }
1967b39b8393SJiri Olsa 
1968333b5665SAndi Kleen void parse_events_print_error(struct parse_events_error *err,
1969b39b8393SJiri Olsa 			      const char *event)
1970b39b8393SJiri Olsa {
1971b39b8393SJiri Olsa 	const char *str = "invalid or unsupported event: ";
1972b39b8393SJiri Olsa 	char _buf[MAX_WIDTH];
1973b39b8393SJiri Olsa 	char *buf = (char *) event;
1974b39b8393SJiri Olsa 	int idx = 0;
1975b39b8393SJiri Olsa 
1976b39b8393SJiri Olsa 	if (err->str) {
1977b39b8393SJiri Olsa 		/* -2 for extra '' in the final fprintf */
1978b39b8393SJiri Olsa 		int width       = get_term_width() - 2;
1979b39b8393SJiri Olsa 		int len_event   = strlen(event);
1980b39b8393SJiri Olsa 		int len_str, max_len, cut = 0;
1981b39b8393SJiri Olsa 
1982b39b8393SJiri Olsa 		/*
1983b39b8393SJiri Olsa 		 * Maximum error index indent, we will cut
1984b39b8393SJiri Olsa 		 * the event string if it's bigger.
1985b39b8393SJiri Olsa 		 */
1986141b2d31SAdrian Hunter 		int max_err_idx = 13;
1987b39b8393SJiri Olsa 
1988b39b8393SJiri Olsa 		/*
1989b39b8393SJiri Olsa 		 * Let's be specific with the message when
1990b39b8393SJiri Olsa 		 * we have the precise error.
1991b39b8393SJiri Olsa 		 */
1992b39b8393SJiri Olsa 		str     = "event syntax error: ";
1993b39b8393SJiri Olsa 		len_str = strlen(str);
1994b39b8393SJiri Olsa 		max_len = width - len_str;
1995b39b8393SJiri Olsa 
1996b39b8393SJiri Olsa 		buf = _buf;
1997b39b8393SJiri Olsa 
1998bd1a0be5SAdam Buchbinder 		/* We're cutting from the beginning. */
1999b39b8393SJiri Olsa 		if (err->idx > max_err_idx)
2000b39b8393SJiri Olsa 			cut = err->idx - max_err_idx;
2001b39b8393SJiri Olsa 
2002b39b8393SJiri Olsa 		strncpy(buf, event + cut, max_len);
2003b39b8393SJiri Olsa 
2004b39b8393SJiri Olsa 		/* Mark cut parts with '..' on both sides. */
2005b39b8393SJiri Olsa 		if (cut)
2006b39b8393SJiri Olsa 			buf[0] = buf[1] = '.';
2007b39b8393SJiri Olsa 
2008b39b8393SJiri Olsa 		if ((len_event - cut) > max_len) {
2009b39b8393SJiri Olsa 			buf[max_len - 1] = buf[max_len - 2] = '.';
2010b39b8393SJiri Olsa 			buf[max_len] = 0;
2011b39b8393SJiri Olsa 		}
2012b39b8393SJiri Olsa 
2013b39b8393SJiri Olsa 		idx = len_str + err->idx - cut;
2014b39b8393SJiri Olsa 	}
2015b39b8393SJiri Olsa 
2016b39b8393SJiri Olsa 	fprintf(stderr, "%s'%s'\n", str, buf);
2017b39b8393SJiri Olsa 	if (idx) {
2018b39b8393SJiri Olsa 		fprintf(stderr, "%*s\\___ %s\n", idx + 1, "", err->str);
2019b39b8393SJiri Olsa 		if (err->help)
2020b39b8393SJiri Olsa 			fprintf(stderr, "\n%s\n", err->help);
2021360e071bSTaeung Song 		zfree(&err->str);
2022360e071bSTaeung Song 		zfree(&err->help);
2023b39b8393SJiri Olsa 	}
2024b39b8393SJiri Olsa }
2025b39b8393SJiri Olsa 
2026b39b8393SJiri Olsa #undef MAX_WIDTH
2027b39b8393SJiri Olsa 
2028f120f9d5SJiri Olsa int parse_events_option(const struct option *opt, const char *str,
20291d037ca1SIrina Tirdea 			int unset __maybe_unused)
2030f120f9d5SJiri Olsa {
203163503dbaSJiri Olsa 	struct evlist *evlist = *(struct evlist **)opt->value;
2032b39b8393SJiri Olsa 	struct parse_events_error err = { .idx = 0, };
2033b39b8393SJiri Olsa 	int ret = parse_events(evlist, str, &err);
20349175ce1fSAndi Kleen 
2035333b5665SAndi Kleen 	if (ret) {
2036b39b8393SJiri Olsa 		parse_events_print_error(&err, str);
2037333b5665SAndi Kleen 		fprintf(stderr, "Run 'perf list' for a list of valid events\n");
2038333b5665SAndi Kleen 	}
2039b39b8393SJiri Olsa 
20409175ce1fSAndi Kleen 	return ret;
2041f120f9d5SJiri Olsa }
2042f120f9d5SJiri Olsa 
20434ba1faa1SWang Nan static int
204463503dbaSJiri Olsa foreach_evsel_in_last_glob(struct evlist *evlist,
204532dcd021SJiri Olsa 			   int (*func)(struct evsel *evsel,
20464ba1faa1SWang Nan 				       const void *arg),
20474ba1faa1SWang Nan 			   const void *arg)
2048c171b552SLi Zefan {
204932dcd021SJiri Olsa 	struct evsel *last = NULL;
20504ba1faa1SWang Nan 	int err;
2051c171b552SLi Zefan 
2052854f7363SWang Nan 	/*
2053854f7363SWang Nan 	 * Don't return when list_empty, give func a chance to report
2054854f7363SWang Nan 	 * error when it found last == NULL.
2055854f7363SWang Nan 	 *
2056854f7363SWang Nan 	 * So no need to WARN here, let *func do this.
2057854f7363SWang Nan 	 */
20586484d2f9SJiri Olsa 	if (evlist->core.nr_entries > 0)
2059515dbe48SJiri Olsa 		last = evlist__last(evlist);
206069aad6f1SArnaldo Carvalho de Melo 
206115bfd2ccSWang Nan 	do {
20624ba1faa1SWang Nan 		err = (*func)(last, arg);
20634ba1faa1SWang Nan 		if (err)
2064c171b552SLi Zefan 			return -1;
20654ba1faa1SWang Nan 		if (!last)
20664ba1faa1SWang Nan 			return 0;
2067c171b552SLi Zefan 
2068ce9036a6SJiri Olsa 		if (last->core.node.prev == &evlist->core.entries)
206915bfd2ccSWang Nan 			return 0;
2070b27c4eceSJiri Olsa 		last = list_entry(last->core.node.prev, struct evsel, core.node);
207115bfd2ccSWang Nan 	} while (!last->cmdline_group_boundary);
207215bfd2ccSWang Nan 
2073c171b552SLi Zefan 	return 0;
2074c171b552SLi Zefan }
2075c171b552SLi Zefan 
207632dcd021SJiri Olsa static int set_filter(struct evsel *evsel, const void *arg)
20774ba1faa1SWang Nan {
20784ba1faa1SWang Nan 	const char *str = arg;
20791e857484SMathieu Poirier 	bool found = false;
20801e857484SMathieu Poirier 	int nr_addr_filters = 0;
20811e857484SMathieu Poirier 	struct perf_pmu *pmu = NULL;
20824ba1faa1SWang Nan 
208349836f78SJack Henschel 	if (evsel == NULL) {
208449836f78SJack Henschel 		fprintf(stderr,
208549836f78SJack Henschel 			"--filter option should follow a -e tracepoint or HW tracer option\n");
208649836f78SJack Henschel 		return -1;
208749836f78SJack Henschel 	}
20884ba1faa1SWang Nan 
20891fc632ceSJiri Olsa 	if (evsel->core.attr.type == PERF_TYPE_TRACEPOINT) {
20903541c034SMathieu Poirier 		if (perf_evsel__append_tp_filter(evsel, str) < 0) {
20914ba1faa1SWang Nan 			fprintf(stderr,
20924ba1faa1SWang Nan 				"not enough memory to hold filter string\n");
20934ba1faa1SWang Nan 			return -1;
20944ba1faa1SWang Nan 		}
20954ba1faa1SWang Nan 
20964ba1faa1SWang Nan 		return 0;
20974ba1faa1SWang Nan 	}
20984ba1faa1SWang Nan 
20991e857484SMathieu Poirier 	while ((pmu = perf_pmu__scan(pmu)) != NULL)
21001fc632ceSJiri Olsa 		if (pmu->type == evsel->core.attr.type) {
21011e857484SMathieu Poirier 			found = true;
21021e857484SMathieu Poirier 			break;
21031e857484SMathieu Poirier 		}
21041e857484SMathieu Poirier 
21051e857484SMathieu Poirier 	if (found)
21061e857484SMathieu Poirier 		perf_pmu__scan_file(pmu, "nr_addr_filters",
21071e857484SMathieu Poirier 				    "%d", &nr_addr_filters);
21081e857484SMathieu Poirier 
210949836f78SJack Henschel 	if (!nr_addr_filters) {
211049836f78SJack Henschel 		fprintf(stderr,
211149836f78SJack Henschel 			"This CPU does not support address filtering\n");
211249836f78SJack Henschel 		return -1;
211349836f78SJack Henschel 	}
21141e857484SMathieu Poirier 
21151e857484SMathieu Poirier 	if (perf_evsel__append_addr_filter(evsel, str) < 0) {
21161e857484SMathieu Poirier 		fprintf(stderr,
21171e857484SMathieu Poirier 			"not enough memory to hold filter string\n");
21181e857484SMathieu Poirier 		return -1;
21191e857484SMathieu Poirier 	}
21201e857484SMathieu Poirier 
21211e857484SMathieu Poirier 	return 0;
21221e857484SMathieu Poirier }
21231e857484SMathieu Poirier 
21244ba1faa1SWang Nan int parse_filter(const struct option *opt, const char *str,
21254ba1faa1SWang Nan 		 int unset __maybe_unused)
21264ba1faa1SWang Nan {
212763503dbaSJiri Olsa 	struct evlist *evlist = *(struct evlist **)opt->value;
21284ba1faa1SWang Nan 
21294ba1faa1SWang Nan 	return foreach_evsel_in_last_glob(evlist, set_filter,
21304ba1faa1SWang Nan 					  (const void *)str);
21314ba1faa1SWang Nan }
21324ba1faa1SWang Nan 
213332dcd021SJiri Olsa static int add_exclude_perf_filter(struct evsel *evsel,
21344ba1faa1SWang Nan 				   const void *arg __maybe_unused)
21354ba1faa1SWang Nan {
21364ba1faa1SWang Nan 	char new_filter[64];
21374ba1faa1SWang Nan 
21381fc632ceSJiri Olsa 	if (evsel == NULL || evsel->core.attr.type != PERF_TYPE_TRACEPOINT) {
21394ba1faa1SWang Nan 		fprintf(stderr,
21404ba1faa1SWang Nan 			"--exclude-perf option should follow a -e tracepoint option\n");
21414ba1faa1SWang Nan 		return -1;
21424ba1faa1SWang Nan 	}
21434ba1faa1SWang Nan 
21444ba1faa1SWang Nan 	snprintf(new_filter, sizeof(new_filter), "common_pid != %d", getpid());
21454ba1faa1SWang Nan 
21463541c034SMathieu Poirier 	if (perf_evsel__append_tp_filter(evsel, new_filter) < 0) {
21474ba1faa1SWang Nan 		fprintf(stderr,
21484ba1faa1SWang Nan 			"not enough memory to hold filter string\n");
21494ba1faa1SWang Nan 		return -1;
21504ba1faa1SWang Nan 	}
21514ba1faa1SWang Nan 
21524ba1faa1SWang Nan 	return 0;
21534ba1faa1SWang Nan }
21544ba1faa1SWang Nan 
21554ba1faa1SWang Nan int exclude_perf(const struct option *opt,
21564ba1faa1SWang Nan 		 const char *arg __maybe_unused,
21574ba1faa1SWang Nan 		 int unset __maybe_unused)
21584ba1faa1SWang Nan {
215963503dbaSJiri Olsa 	struct evlist *evlist = *(struct evlist **)opt->value;
21604ba1faa1SWang Nan 
21614ba1faa1SWang Nan 	return foreach_evsel_in_last_glob(evlist, add_exclude_perf_filter,
21624ba1faa1SWang Nan 					  NULL);
21634ba1faa1SWang Nan }
21644ba1faa1SWang Nan 
216586470930SIngo Molnar static const char * const event_type_descriptors[] = {
216686470930SIngo Molnar 	"Hardware event",
216786470930SIngo Molnar 	"Software event",
216886470930SIngo Molnar 	"Tracepoint event",
216986470930SIngo Molnar 	"Hardware cache event",
217041bdcb23SLiming Wang 	"Raw hardware event descriptor",
217141bdcb23SLiming Wang 	"Hardware breakpoint",
217286470930SIngo Molnar };
217386470930SIngo Molnar 
2174ab0e4800SYunlong Song static int cmp_string(const void *a, const void *b)
2175ab0e4800SYunlong Song {
2176ab0e4800SYunlong Song 	const char * const *as = a;
2177ab0e4800SYunlong Song 	const char * const *bs = b;
2178ab0e4800SYunlong Song 
2179ab0e4800SYunlong Song 	return strcmp(*as, *bs);
2180ab0e4800SYunlong Song }
2181ab0e4800SYunlong Song 
218286470930SIngo Molnar /*
2183f6bdafefSJason Baron  * Print the events from <debugfs_mount_point>/tracing/events
2184f6bdafefSJason Baron  */
2185f6bdafefSJason Baron 
2186a3277d2dSFrederic Weisbecker void print_tracepoint_events(const char *subsys_glob, const char *event_glob,
2187a3277d2dSFrederic Weisbecker 			     bool name_only)
2188f6bdafefSJason Baron {
2189f6bdafefSJason Baron 	DIR *sys_dir, *evt_dir;
219022a9f41bSArnaldo Carvalho de Melo 	struct dirent *sys_dirent, *evt_dirent;
2191f6bdafefSJason Baron 	char evt_path[MAXPATHLEN];
219225a7d914SArnaldo Carvalho de Melo 	char *dir_path;
2193ab0e4800SYunlong Song 	char **evt_list = NULL;
2194ab0e4800SYunlong Song 	unsigned int evt_i = 0, evt_num = 0;
2195ab0e4800SYunlong Song 	bool evt_num_known = false;
2196f6bdafefSJason Baron 
2197ab0e4800SYunlong Song restart:
21987014e0e3SArnaldo Carvalho de Melo 	sys_dir = tracing_events__opendir();
2199f6bdafefSJason Baron 	if (!sys_dir)
2200725b1368SEric Dumazet 		return;
2201f6bdafefSJason Baron 
2202ab0e4800SYunlong Song 	if (evt_num_known) {
2203ab0e4800SYunlong Song 		evt_list = zalloc(sizeof(char *) * evt_num);
2204ab0e4800SYunlong Song 		if (!evt_list)
2205ab0e4800SYunlong Song 			goto out_close_sys_dir;
2206ab0e4800SYunlong Song 	}
2207ab0e4800SYunlong Song 
220822a9f41bSArnaldo Carvalho de Melo 	for_each_subsystem(sys_dir, sys_dirent) {
2209668b8788SArnaldo Carvalho de Melo 		if (subsys_glob != NULL &&
221022a9f41bSArnaldo Carvalho de Melo 		    !strglobmatch(sys_dirent->d_name, subsys_glob))
2211668b8788SArnaldo Carvalho de Melo 			continue;
2212725b1368SEric Dumazet 
221325a7d914SArnaldo Carvalho de Melo 		dir_path = get_events_file(sys_dirent->d_name);
221425a7d914SArnaldo Carvalho de Melo 		if (!dir_path)
221525a7d914SArnaldo Carvalho de Melo 			continue;
2216725b1368SEric Dumazet 		evt_dir = opendir(dir_path);
2217725b1368SEric Dumazet 		if (!evt_dir)
221825a7d914SArnaldo Carvalho de Melo 			goto next;
2219725b1368SEric Dumazet 
2220c02cab22SArnaldo Carvalho de Melo 		for_each_event(dir_path, evt_dir, evt_dirent) {
2221668b8788SArnaldo Carvalho de Melo 			if (event_glob != NULL &&
222222a9f41bSArnaldo Carvalho de Melo 			    !strglobmatch(evt_dirent->d_name, event_glob))
2223668b8788SArnaldo Carvalho de Melo 				continue;
2224668b8788SArnaldo Carvalho de Melo 
2225ab0e4800SYunlong Song 			if (!evt_num_known) {
2226ab0e4800SYunlong Song 				evt_num++;
2227a3277d2dSFrederic Weisbecker 				continue;
2228a3277d2dSFrederic Weisbecker 			}
2229a3277d2dSFrederic Weisbecker 
2230f6bdafefSJason Baron 			snprintf(evt_path, MAXPATHLEN, "%s:%s",
223122a9f41bSArnaldo Carvalho de Melo 				 sys_dirent->d_name, evt_dirent->d_name);
2232ab0e4800SYunlong Song 
2233ab0e4800SYunlong Song 			evt_list[evt_i] = strdup(evt_path);
223425a7d914SArnaldo Carvalho de Melo 			if (evt_list[evt_i] == NULL) {
223525a7d914SArnaldo Carvalho de Melo 				put_events_file(dir_path);
2236ab0e4800SYunlong Song 				goto out_close_evt_dir;
223725a7d914SArnaldo Carvalho de Melo 			}
2238ab0e4800SYunlong Song 			evt_i++;
2239f6bdafefSJason Baron 		}
2240f6bdafefSJason Baron 		closedir(evt_dir);
224125a7d914SArnaldo Carvalho de Melo next:
224225a7d914SArnaldo Carvalho de Melo 		put_events_file(dir_path);
2243f6bdafefSJason Baron 	}
2244f6bdafefSJason Baron 	closedir(sys_dir);
2245ab0e4800SYunlong Song 
2246ab0e4800SYunlong Song 	if (!evt_num_known) {
2247ab0e4800SYunlong Song 		evt_num_known = true;
2248ab0e4800SYunlong Song 		goto restart;
2249ab0e4800SYunlong Song 	}
2250ab0e4800SYunlong Song 	qsort(evt_list, evt_num, sizeof(char *), cmp_string);
2251ab0e4800SYunlong Song 	evt_i = 0;
2252ab0e4800SYunlong Song 	while (evt_i < evt_num) {
2253ab0e4800SYunlong Song 		if (name_only) {
2254ab0e4800SYunlong Song 			printf("%s ", evt_list[evt_i++]);
2255ab0e4800SYunlong Song 			continue;
2256ab0e4800SYunlong Song 		}
2257ab0e4800SYunlong Song 		printf("  %-50s [%s]\n", evt_list[evt_i++],
2258ab0e4800SYunlong Song 				event_type_descriptors[PERF_TYPE_TRACEPOINT]);
2259ab0e4800SYunlong Song 	}
2260dfc431cbSArnaldo Carvalho de Melo 	if (evt_num && pager_in_use())
2261ab0e4800SYunlong Song 		printf("\n");
2262ab0e4800SYunlong Song 
2263ab0e4800SYunlong Song out_free:
2264ab0e4800SYunlong Song 	evt_num = evt_i;
2265ab0e4800SYunlong Song 	for (evt_i = 0; evt_i < evt_num; evt_i++)
2266ab0e4800SYunlong Song 		zfree(&evt_list[evt_i]);
2267ab0e4800SYunlong Song 	zfree(&evt_list);
2268ab0e4800SYunlong Song 	return;
2269ab0e4800SYunlong Song 
2270ab0e4800SYunlong Song out_close_evt_dir:
2271ab0e4800SYunlong Song 	closedir(evt_dir);
2272ab0e4800SYunlong Song out_close_sys_dir:
2273ab0e4800SYunlong Song 	closedir(sys_dir);
2274ab0e4800SYunlong Song 
2275ab0e4800SYunlong Song 	printf("FATAL: not enough memory to print %s\n",
2276ab0e4800SYunlong Song 			event_type_descriptors[PERF_TYPE_TRACEPOINT]);
2277ab0e4800SYunlong Song 	if (evt_list)
2278ab0e4800SYunlong Song 		goto out_free;
2279f6bdafefSJason Baron }
2280f6bdafefSJason Baron 
2281f6bdafefSJason Baron /*
228220c457b8SThomas Renninger  * Check whether event is in <debugfs_mount_point>/tracing/events
228320c457b8SThomas Renninger  */
228420c457b8SThomas Renninger 
228520c457b8SThomas Renninger int is_valid_tracepoint(const char *event_string)
228620c457b8SThomas Renninger {
228720c457b8SThomas Renninger 	DIR *sys_dir, *evt_dir;
228822a9f41bSArnaldo Carvalho de Melo 	struct dirent *sys_dirent, *evt_dirent;
228920c457b8SThomas Renninger 	char evt_path[MAXPATHLEN];
229025a7d914SArnaldo Carvalho de Melo 	char *dir_path;
229120c457b8SThomas Renninger 
22927014e0e3SArnaldo Carvalho de Melo 	sys_dir = tracing_events__opendir();
229320c457b8SThomas Renninger 	if (!sys_dir)
229420c457b8SThomas Renninger 		return 0;
229520c457b8SThomas Renninger 
229622a9f41bSArnaldo Carvalho de Melo 	for_each_subsystem(sys_dir, sys_dirent) {
229725a7d914SArnaldo Carvalho de Melo 		dir_path = get_events_file(sys_dirent->d_name);
229825a7d914SArnaldo Carvalho de Melo 		if (!dir_path)
229925a7d914SArnaldo Carvalho de Melo 			continue;
230020c457b8SThomas Renninger 		evt_dir = opendir(dir_path);
230120c457b8SThomas Renninger 		if (!evt_dir)
230225a7d914SArnaldo Carvalho de Melo 			goto next;
230320c457b8SThomas Renninger 
2304c02cab22SArnaldo Carvalho de Melo 		for_each_event(dir_path, evt_dir, evt_dirent) {
230520c457b8SThomas Renninger 			snprintf(evt_path, MAXPATHLEN, "%s:%s",
230622a9f41bSArnaldo Carvalho de Melo 				 sys_dirent->d_name, evt_dirent->d_name);
230720c457b8SThomas Renninger 			if (!strcmp(evt_path, event_string)) {
230820c457b8SThomas Renninger 				closedir(evt_dir);
230920c457b8SThomas Renninger 				closedir(sys_dir);
231020c457b8SThomas Renninger 				return 1;
231120c457b8SThomas Renninger 			}
231220c457b8SThomas Renninger 		}
231320c457b8SThomas Renninger 		closedir(evt_dir);
231425a7d914SArnaldo Carvalho de Melo next:
231525a7d914SArnaldo Carvalho de Melo 		put_events_file(dir_path);
231620c457b8SThomas Renninger 	}
231720c457b8SThomas Renninger 	closedir(sys_dir);
231820c457b8SThomas Renninger 	return 0;
231920c457b8SThomas Renninger }
232020c457b8SThomas Renninger 
2321b41f1cecSNamhyung Kim static bool is_event_supported(u8 type, unsigned config)
2322b41f1cecSNamhyung Kim {
2323b41f1cecSNamhyung Kim 	bool ret = true;
232488fee52eSVince Weaver 	int open_return;
232532dcd021SJiri Olsa 	struct evsel *evsel;
2326b41f1cecSNamhyung Kim 	struct perf_event_attr attr = {
2327b41f1cecSNamhyung Kim 		.type = type,
2328b41f1cecSNamhyung Kim 		.config = config,
2329b41f1cecSNamhyung Kim 		.disabled = 1,
2330b41f1cecSNamhyung Kim 	};
23319749b90eSJiri Olsa 	struct perf_thread_map *tmap = thread_map__new_by_tid(0);
233289896051SArnaldo Carvalho de Melo 
233389896051SArnaldo Carvalho de Melo 	if (tmap == NULL)
233489896051SArnaldo Carvalho de Melo 		return false;
2335b41f1cecSNamhyung Kim 
2336365c3ae7SJiri Olsa 	evsel = evsel__new(&attr);
2337b41f1cecSNamhyung Kim 	if (evsel) {
23385972d1e0SJiri Olsa 		open_return = evsel__open(evsel, NULL, tmap);
233988fee52eSVince Weaver 		ret = open_return >= 0;
234088fee52eSVince Weaver 
234188fee52eSVince Weaver 		if (open_return == -EACCES) {
234288fee52eSVince Weaver 			/*
234388fee52eSVince Weaver 			 * This happens if the paranoid value
234488fee52eSVince Weaver 			 * /proc/sys/kernel/perf_event_paranoid is set to 2
234588fee52eSVince Weaver 			 * Re-run with exclude_kernel set; we don't do that
234688fee52eSVince Weaver 			 * by default as some ARM machines do not support it.
234788fee52eSVince Weaver 			 *
234888fee52eSVince Weaver 			 */
23491fc632ceSJiri Olsa 			evsel->core.attr.exclude_kernel = 1;
23505972d1e0SJiri Olsa 			ret = evsel__open(evsel, NULL, tmap) >= 0;
235188fee52eSVince Weaver 		}
23525eb2dd2aSJiri Olsa 		evsel__delete(evsel);
2353b41f1cecSNamhyung Kim 	}
2354b41f1cecSNamhyung Kim 
23557836e52eSJiri Olsa 	perf_thread_map__put(tmap);
2356b41f1cecSNamhyung Kim 	return ret;
2357b41f1cecSNamhyung Kim }
2358b41f1cecSNamhyung Kim 
235940218daeSMasami Hiramatsu void print_sdt_events(const char *subsys_glob, const char *event_glob,
236040218daeSMasami Hiramatsu 		      bool name_only)
236140218daeSMasami Hiramatsu {
236240218daeSMasami Hiramatsu 	struct probe_cache *pcache;
236340218daeSMasami Hiramatsu 	struct probe_cache_entry *ent;
236440218daeSMasami Hiramatsu 	struct strlist *bidlist, *sdtlist;
236540218daeSMasami Hiramatsu 	struct strlist_config cfg = {.dont_dupstr = true};
236640218daeSMasami Hiramatsu 	struct str_node *nd, *nd2;
236740218daeSMasami Hiramatsu 	char *buf, *path, *ptr = NULL;
236840218daeSMasami Hiramatsu 	bool show_detail = false;
236940218daeSMasami Hiramatsu 	int ret;
237040218daeSMasami Hiramatsu 
237140218daeSMasami Hiramatsu 	sdtlist = strlist__new(NULL, &cfg);
237240218daeSMasami Hiramatsu 	if (!sdtlist) {
237340218daeSMasami Hiramatsu 		pr_debug("Failed to allocate new strlist for SDT\n");
237440218daeSMasami Hiramatsu 		return;
237540218daeSMasami Hiramatsu 	}
237640218daeSMasami Hiramatsu 	bidlist = build_id_cache__list_all(true);
237740218daeSMasami Hiramatsu 	if (!bidlist) {
237840218daeSMasami Hiramatsu 		pr_debug("Failed to get buildids: %d\n", errno);
237940218daeSMasami Hiramatsu 		return;
238040218daeSMasami Hiramatsu 	}
238140218daeSMasami Hiramatsu 	strlist__for_each_entry(nd, bidlist) {
2382f045b8c4SKrister Johansen 		pcache = probe_cache__new(nd->s, NULL);
238340218daeSMasami Hiramatsu 		if (!pcache)
238440218daeSMasami Hiramatsu 			continue;
238540218daeSMasami Hiramatsu 		list_for_each_entry(ent, &pcache->entries, node) {
238640218daeSMasami Hiramatsu 			if (!ent->sdt)
238740218daeSMasami Hiramatsu 				continue;
238840218daeSMasami Hiramatsu 			if (subsys_glob &&
238940218daeSMasami Hiramatsu 			    !strglobmatch(ent->pev.group, subsys_glob))
239040218daeSMasami Hiramatsu 				continue;
239140218daeSMasami Hiramatsu 			if (event_glob &&
239240218daeSMasami Hiramatsu 			    !strglobmatch(ent->pev.event, event_glob))
239340218daeSMasami Hiramatsu 				continue;
239440218daeSMasami Hiramatsu 			ret = asprintf(&buf, "%s:%s@%s", ent->pev.group,
239540218daeSMasami Hiramatsu 					ent->pev.event, nd->s);
239640218daeSMasami Hiramatsu 			if (ret > 0)
239740218daeSMasami Hiramatsu 				strlist__add(sdtlist, buf);
239840218daeSMasami Hiramatsu 		}
239940218daeSMasami Hiramatsu 		probe_cache__delete(pcache);
240040218daeSMasami Hiramatsu 	}
240140218daeSMasami Hiramatsu 	strlist__delete(bidlist);
240240218daeSMasami Hiramatsu 
240340218daeSMasami Hiramatsu 	strlist__for_each_entry(nd, sdtlist) {
240440218daeSMasami Hiramatsu 		buf = strchr(nd->s, '@');
240540218daeSMasami Hiramatsu 		if (buf)
240640218daeSMasami Hiramatsu 			*(buf++) = '\0';
240740218daeSMasami Hiramatsu 		if (name_only) {
240840218daeSMasami Hiramatsu 			printf("%s ", nd->s);
240940218daeSMasami Hiramatsu 			continue;
241040218daeSMasami Hiramatsu 		}
241140218daeSMasami Hiramatsu 		nd2 = strlist__next(nd);
241240218daeSMasami Hiramatsu 		if (nd2) {
241340218daeSMasami Hiramatsu 			ptr = strchr(nd2->s, '@');
241440218daeSMasami Hiramatsu 			if (ptr)
241540218daeSMasami Hiramatsu 				*ptr = '\0';
241640218daeSMasami Hiramatsu 			if (strcmp(nd->s, nd2->s) == 0)
241740218daeSMasami Hiramatsu 				show_detail = true;
241840218daeSMasami Hiramatsu 		}
241940218daeSMasami Hiramatsu 		if (show_detail) {
242040218daeSMasami Hiramatsu 			path = build_id_cache__origname(buf);
242140218daeSMasami Hiramatsu 			ret = asprintf(&buf, "%s@%s(%.12s)", nd->s, path, buf);
242240218daeSMasami Hiramatsu 			if (ret > 0) {
242340218daeSMasami Hiramatsu 				printf("  %-50s [%s]\n", buf, "SDT event");
242440218daeSMasami Hiramatsu 				free(buf);
242540218daeSMasami Hiramatsu 			}
24268bde8516SChangbin Du 			free(path);
242740218daeSMasami Hiramatsu 		} else
242840218daeSMasami Hiramatsu 			printf("  %-50s [%s]\n", nd->s, "SDT event");
242940218daeSMasami Hiramatsu 		if (nd2) {
243040218daeSMasami Hiramatsu 			if (strcmp(nd->s, nd2->s) != 0)
243140218daeSMasami Hiramatsu 				show_detail = false;
243240218daeSMasami Hiramatsu 			if (ptr)
243340218daeSMasami Hiramatsu 				*ptr = '@';
243440218daeSMasami Hiramatsu 		}
243540218daeSMasami Hiramatsu 	}
243640218daeSMasami Hiramatsu 	strlist__delete(sdtlist);
243740218daeSMasami Hiramatsu }
243840218daeSMasami Hiramatsu 
2439a3277d2dSFrederic Weisbecker int print_hwcache_events(const char *event_glob, bool name_only)
2440668b8788SArnaldo Carvalho de Melo {
2441ab0e4800SYunlong Song 	unsigned int type, op, i, evt_i = 0, evt_num = 0;
24420b668bc9SArnaldo Carvalho de Melo 	char name[64];
2443ab0e4800SYunlong Song 	char **evt_list = NULL;
2444ab0e4800SYunlong Song 	bool evt_num_known = false;
2445ab0e4800SYunlong Song 
2446ab0e4800SYunlong Song restart:
2447ab0e4800SYunlong Song 	if (evt_num_known) {
2448ab0e4800SYunlong Song 		evt_list = zalloc(sizeof(char *) * evt_num);
2449ab0e4800SYunlong Song 		if (!evt_list)
2450ab0e4800SYunlong Song 			goto out_enomem;
2451ab0e4800SYunlong Song 	}
2452668b8788SArnaldo Carvalho de Melo 
2453668b8788SArnaldo Carvalho de Melo 	for (type = 0; type < PERF_COUNT_HW_CACHE_MAX; type++) {
2454668b8788SArnaldo Carvalho de Melo 		for (op = 0; op < PERF_COUNT_HW_CACHE_OP_MAX; op++) {
2455668b8788SArnaldo Carvalho de Melo 			/* skip invalid cache type */
24560b668bc9SArnaldo Carvalho de Melo 			if (!perf_evsel__is_cache_op_valid(type, op))
2457668b8788SArnaldo Carvalho de Melo 				continue;
2458668b8788SArnaldo Carvalho de Melo 
2459668b8788SArnaldo Carvalho de Melo 			for (i = 0; i < PERF_COUNT_HW_CACHE_RESULT_MAX; i++) {
24600b668bc9SArnaldo Carvalho de Melo 				__perf_evsel__hw_cache_type_op_res_name(type, op, i,
24610b668bc9SArnaldo Carvalho de Melo 									name, sizeof(name));
2462947b4ad1SIngo Molnar 				if (event_glob != NULL && !strglobmatch(name, event_glob))
2463668b8788SArnaldo Carvalho de Melo 					continue;
2464668b8788SArnaldo Carvalho de Melo 
2465b41f1cecSNamhyung Kim 				if (!is_event_supported(PERF_TYPE_HW_CACHE,
2466b41f1cecSNamhyung Kim 							type | (op << 8) | (i << 16)))
2467b41f1cecSNamhyung Kim 					continue;
2468b41f1cecSNamhyung Kim 
2469ab0e4800SYunlong Song 				if (!evt_num_known) {
2470ab0e4800SYunlong Song 					evt_num++;
2471ab0e4800SYunlong Song 					continue;
2472ab0e4800SYunlong Song 				}
2473ab0e4800SYunlong Song 
2474ab0e4800SYunlong Song 				evt_list[evt_i] = strdup(name);
2475ab0e4800SYunlong Song 				if (evt_list[evt_i] == NULL)
2476ab0e4800SYunlong Song 					goto out_enomem;
2477ab0e4800SYunlong Song 				evt_i++;
2478668b8788SArnaldo Carvalho de Melo 			}
2479668b8788SArnaldo Carvalho de Melo 		}
2480668b8788SArnaldo Carvalho de Melo 	}
2481668b8788SArnaldo Carvalho de Melo 
2482ab0e4800SYunlong Song 	if (!evt_num_known) {
2483ab0e4800SYunlong Song 		evt_num_known = true;
2484ab0e4800SYunlong Song 		goto restart;
2485ab0e4800SYunlong Song 	}
2486ab0e4800SYunlong Song 	qsort(evt_list, evt_num, sizeof(char *), cmp_string);
2487ab0e4800SYunlong Song 	evt_i = 0;
2488ab0e4800SYunlong Song 	while (evt_i < evt_num) {
2489ab0e4800SYunlong Song 		if (name_only) {
2490ab0e4800SYunlong Song 			printf("%s ", evt_list[evt_i++]);
2491ab0e4800SYunlong Song 			continue;
2492ab0e4800SYunlong Song 		}
2493ab0e4800SYunlong Song 		printf("  %-50s [%s]\n", evt_list[evt_i++],
2494ab0e4800SYunlong Song 				event_type_descriptors[PERF_TYPE_HW_CACHE]);
2495ab0e4800SYunlong Song 	}
2496dfc431cbSArnaldo Carvalho de Melo 	if (evt_num && pager_in_use())
2497dc098b35SAndi Kleen 		printf("\n");
2498ab0e4800SYunlong Song 
2499ab0e4800SYunlong Song out_free:
2500ab0e4800SYunlong Song 	evt_num = evt_i;
2501ab0e4800SYunlong Song 	for (evt_i = 0; evt_i < evt_num; evt_i++)
2502ab0e4800SYunlong Song 		zfree(&evt_list[evt_i]);
2503ab0e4800SYunlong Song 	zfree(&evt_list);
2504ab0e4800SYunlong Song 	return evt_num;
2505ab0e4800SYunlong Song 
2506ab0e4800SYunlong Song out_enomem:
2507ab0e4800SYunlong Song 	printf("FATAL: not enough memory to print %s\n", event_type_descriptors[PERF_TYPE_HW_CACHE]);
2508ab0e4800SYunlong Song 	if (evt_list)
2509ab0e4800SYunlong Song 		goto out_free;
2510ab0e4800SYunlong Song 	return evt_num;
2511668b8788SArnaldo Carvalho de Melo }
2512668b8788SArnaldo Carvalho de Melo 
25135e0861baSAndi Kleen static void print_tool_event(const char *name, const char *event_glob,
25145e0861baSAndi Kleen 			     bool name_only)
25155e0861baSAndi Kleen {
25165e0861baSAndi Kleen 	if (event_glob && !strglobmatch(name, event_glob))
25175e0861baSAndi Kleen 		return;
25185e0861baSAndi Kleen 	if (name_only)
25195e0861baSAndi Kleen 		printf("%s ", name);
25205e0861baSAndi Kleen 	else
25215e0861baSAndi Kleen 		printf("  %-50s [%s]\n", name, "Tool event");
25225e0861baSAndi Kleen 
25235e0861baSAndi Kleen }
25245e0861baSAndi Kleen 
25255e0861baSAndi Kleen void print_tool_events(const char *event_glob, bool name_only)
25265e0861baSAndi Kleen {
25275e0861baSAndi Kleen 	print_tool_event("duration_time", event_glob, name_only);
25285e0861baSAndi Kleen 	if (pager_in_use())
25295e0861baSAndi Kleen 		printf("\n");
25305e0861baSAndi Kleen }
25315e0861baSAndi Kleen 
2532705750f2SYunlong Song void print_symbol_events(const char *event_glob, unsigned type,
2533a3277d2dSFrederic Weisbecker 				struct event_symbol *syms, unsigned max,
2534a3277d2dSFrederic Weisbecker 				bool name_only)
253586470930SIngo Molnar {
2536ab0e4800SYunlong Song 	unsigned int i, evt_i = 0, evt_num = 0;
2537947b4ad1SIngo Molnar 	char name[MAX_NAME_LEN];
2538ab0e4800SYunlong Song 	char **evt_list = NULL;
2539ab0e4800SYunlong Song 	bool evt_num_known = false;
2540ab0e4800SYunlong Song 
2541ab0e4800SYunlong Song restart:
2542ab0e4800SYunlong Song 	if (evt_num_known) {
2543ab0e4800SYunlong Song 		evt_list = zalloc(sizeof(char *) * evt_num);
2544ab0e4800SYunlong Song 		if (!evt_list)
2545ab0e4800SYunlong Song 			goto out_enomem;
2546ab0e4800SYunlong Song 		syms -= max;
2547ab0e4800SYunlong Song 	}
254886470930SIngo Molnar 
25491dc12760SJiri Olsa 	for (i = 0; i < max; i++, syms++) {
2550668b8788SArnaldo Carvalho de Melo 
2551e37df6c7SArnaldo Carvalho de Melo 		if (event_glob != NULL && syms->symbol != NULL &&
2552668b8788SArnaldo Carvalho de Melo 		    !(strglobmatch(syms->symbol, event_glob) ||
2553668b8788SArnaldo Carvalho de Melo 		      (syms->alias && strglobmatch(syms->alias, event_glob))))
2554668b8788SArnaldo Carvalho de Melo 			continue;
255586470930SIngo Molnar 
2556b41f1cecSNamhyung Kim 		if (!is_event_supported(type, i))
2557b41f1cecSNamhyung Kim 			continue;
2558b41f1cecSNamhyung Kim 
2559ab0e4800SYunlong Song 		if (!evt_num_known) {
2560ab0e4800SYunlong Song 			evt_num++;
2561a3277d2dSFrederic Weisbecker 			continue;
2562a3277d2dSFrederic Weisbecker 		}
2563a3277d2dSFrederic Weisbecker 
2564ab0e4800SYunlong Song 		if (!name_only && strlen(syms->alias))
2565947b4ad1SIngo Molnar 			snprintf(name, MAX_NAME_LEN, "%s OR %s", syms->symbol, syms->alias);
256674d5b588SJaswinder Singh Rajput 		else
2567bd8d57fbSArnaldo Carvalho de Melo 			strlcpy(name, syms->symbol, MAX_NAME_LEN);
256886470930SIngo Molnar 
2569ab0e4800SYunlong Song 		evt_list[evt_i] = strdup(name);
2570ab0e4800SYunlong Song 		if (evt_list[evt_i] == NULL)
2571ab0e4800SYunlong Song 			goto out_enomem;
2572ab0e4800SYunlong Song 		evt_i++;
257386470930SIngo Molnar 	}
257486470930SIngo Molnar 
2575ab0e4800SYunlong Song 	if (!evt_num_known) {
2576ab0e4800SYunlong Song 		evt_num_known = true;
2577ab0e4800SYunlong Song 		goto restart;
2578ab0e4800SYunlong Song 	}
2579ab0e4800SYunlong Song 	qsort(evt_list, evt_num, sizeof(char *), cmp_string);
2580ab0e4800SYunlong Song 	evt_i = 0;
2581ab0e4800SYunlong Song 	while (evt_i < evt_num) {
2582ab0e4800SYunlong Song 		if (name_only) {
2583ab0e4800SYunlong Song 			printf("%s ", evt_list[evt_i++]);
2584ab0e4800SYunlong Song 			continue;
2585ab0e4800SYunlong Song 		}
2586ab0e4800SYunlong Song 		printf("  %-50s [%s]\n", evt_list[evt_i++], event_type_descriptors[type]);
2587ab0e4800SYunlong Song 	}
2588dfc431cbSArnaldo Carvalho de Melo 	if (evt_num && pager_in_use())
2589689d3018SMarti Raudsepp 		printf("\n");
2590ab0e4800SYunlong Song 
2591ab0e4800SYunlong Song out_free:
2592ab0e4800SYunlong Song 	evt_num = evt_i;
2593ab0e4800SYunlong Song 	for (evt_i = 0; evt_i < evt_num; evt_i++)
2594ab0e4800SYunlong Song 		zfree(&evt_list[evt_i]);
2595ab0e4800SYunlong Song 	zfree(&evt_list);
2596ab0e4800SYunlong Song 	return;
2597ab0e4800SYunlong Song 
2598ab0e4800SYunlong Song out_enomem:
2599ab0e4800SYunlong Song 	printf("FATAL: not enough memory to print %s\n", event_type_descriptors[type]);
2600ab0e4800SYunlong Song 	if (evt_list)
2601ab0e4800SYunlong Song 		goto out_free;
2602668b8788SArnaldo Carvalho de Melo }
26031dc12760SJiri Olsa 
26041dc12760SJiri Olsa /*
26051dc12760SJiri Olsa  * Print the help text for the event symbols:
26061dc12760SJiri Olsa  */
2607c8d6828aSSukadev Bhattiprolu void print_events(const char *event_glob, bool name_only, bool quiet_flag,
2608a7f6c8c8SJin Yao 			bool long_desc, bool details_flag, bool deprecated)
26091dc12760SJiri Olsa {
26101dc12760SJiri Olsa 	print_symbol_events(event_glob, PERF_TYPE_HARDWARE,
2611a3277d2dSFrederic Weisbecker 			    event_symbols_hw, PERF_COUNT_HW_MAX, name_only);
26121dc12760SJiri Olsa 
26131dc12760SJiri Olsa 	print_symbol_events(event_glob, PERF_TYPE_SOFTWARE,
2614a3277d2dSFrederic Weisbecker 			    event_symbols_sw, PERF_COUNT_SW_MAX, name_only);
26155e0861baSAndi Kleen 	print_tool_events(event_glob, name_only);
26161dc12760SJiri Olsa 
2617a3277d2dSFrederic Weisbecker 	print_hwcache_events(event_glob, name_only);
261873c24cb8SJaswinder Singh Rajput 
2619bf874fcfSAndi Kleen 	print_pmu_events(event_glob, name_only, quiet_flag, long_desc,
2620a7f6c8c8SJin Yao 			details_flag, deprecated);
2621dc098b35SAndi Kleen 
2622668b8788SArnaldo Carvalho de Melo 	if (event_glob != NULL)
2623668b8788SArnaldo Carvalho de Melo 		return;
262473c24cb8SJaswinder Singh Rajput 
2625a3277d2dSFrederic Weisbecker 	if (!name_only) {
2626947b4ad1SIngo Molnar 		printf("  %-50s [%s]\n",
26275f537a26SJiri Olsa 		       "rNNN",
26281cf4a063SArnaldo Carvalho de Melo 		       event_type_descriptors[PERF_TYPE_RAW]);
26295f537a26SJiri Olsa 		printf("  %-50s [%s]\n",
26305f537a26SJiri Olsa 		       "cpu/t1=v1[,t2=v2,t3 ...]/modifier",
26315f537a26SJiri Olsa 		       event_type_descriptors[PERF_TYPE_RAW]);
2632dfc431cbSArnaldo Carvalho de Melo 		if (pager_in_use())
2633dfc431cbSArnaldo Carvalho de Melo 			printf("   (see 'man perf-list' on how to encode it)\n\n");
263486470930SIngo Molnar 
2635947b4ad1SIngo Molnar 		printf("  %-50s [%s]\n",
26363741eb9fSJacob Shin 		       "mem:<addr>[/len][:access]",
263741bdcb23SLiming Wang 			event_type_descriptors[PERF_TYPE_BREAKPOINT]);
2638dfc431cbSArnaldo Carvalho de Melo 		if (pager_in_use())
26391b290d67SFrederic Weisbecker 			printf("\n");
2640a3277d2dSFrederic Weisbecker 	}
26411b290d67SFrederic Weisbecker 
2642a3277d2dSFrederic Weisbecker 	print_tracepoint_events(NULL, NULL, name_only);
264340218daeSMasami Hiramatsu 
264440218daeSMasami Hiramatsu 	print_sdt_events(NULL, NULL, name_only);
264571b0acceSAndi Kleen 
264633bbc571SJiri Olsa 	metricgroup__print(true, true, NULL, name_only, details_flag);
264786470930SIngo Molnar }
26488f707d84SJiri Olsa 
26496cee6cd3SArnaldo Carvalho de Melo int parse_events__is_hardcoded_term(struct parse_events_term *term)
26508f707d84SJiri Olsa {
265116fa7e82SJiri Olsa 	return term->type_term != PARSE_EVENTS__TERM_TYPE_USER;
26528f707d84SJiri Olsa }
26538f707d84SJiri Olsa 
265467b49b38SJiri Olsa static int new_term(struct parse_events_term **_term,
265567b49b38SJiri Olsa 		    struct parse_events_term *temp,
265667b49b38SJiri Olsa 		    char *str, u64 num)
26578f707d84SJiri Olsa {
26586cee6cd3SArnaldo Carvalho de Melo 	struct parse_events_term *term;
26598f707d84SJiri Olsa 
266067b49b38SJiri Olsa 	term = malloc(sizeof(*term));
26618f707d84SJiri Olsa 	if (!term)
26628f707d84SJiri Olsa 		return -ENOMEM;
26638f707d84SJiri Olsa 
266467b49b38SJiri Olsa 	*term = *temp;
26658f707d84SJiri Olsa 	INIT_LIST_HEAD(&term->list);
2666c2f1ceadSAndi Kleen 	term->weak = false;
26678f707d84SJiri Olsa 
266867b49b38SJiri Olsa 	switch (term->type_val) {
26698f707d84SJiri Olsa 	case PARSE_EVENTS__TERM_TYPE_NUM:
26708f707d84SJiri Olsa 		term->val.num = num;
26718f707d84SJiri Olsa 		break;
26728f707d84SJiri Olsa 	case PARSE_EVENTS__TERM_TYPE_STR:
26738f707d84SJiri Olsa 		term->val.str = str;
26748f707d84SJiri Olsa 		break;
26758f707d84SJiri Olsa 	default:
26764be8be6bSAdrian Hunter 		free(term);
26778f707d84SJiri Olsa 		return -EINVAL;
26788f707d84SJiri Olsa 	}
26798f707d84SJiri Olsa 
26808f707d84SJiri Olsa 	*_term = term;
26818f707d84SJiri Olsa 	return 0;
26828f707d84SJiri Olsa }
26838f707d84SJiri Olsa 
26846cee6cd3SArnaldo Carvalho de Melo int parse_events_term__num(struct parse_events_term **term,
2685cecf3a2eSJiri Olsa 			   int type_term, char *config, u64 num,
268699e7138eSJiri Olsa 			   bool no_value,
2687bb78ce7dSAdrian Hunter 			   void *loc_term_, void *loc_val_)
268816fa7e82SJiri Olsa {
2689bb78ce7dSAdrian Hunter 	YYLTYPE *loc_term = loc_term_;
2690bb78ce7dSAdrian Hunter 	YYLTYPE *loc_val = loc_val_;
2691bb78ce7dSAdrian Hunter 
269267b49b38SJiri Olsa 	struct parse_events_term temp = {
269367b49b38SJiri Olsa 		.type_val  = PARSE_EVENTS__TERM_TYPE_NUM,
269467b49b38SJiri Olsa 		.type_term = type_term,
269567b49b38SJiri Olsa 		.config    = config,
269699e7138eSJiri Olsa 		.no_value  = no_value,
269767b49b38SJiri Olsa 		.err_term  = loc_term ? loc_term->first_column : 0,
269867b49b38SJiri Olsa 		.err_val   = loc_val  ? loc_val->first_column  : 0,
269967b49b38SJiri Olsa 	};
270067b49b38SJiri Olsa 
270167b49b38SJiri Olsa 	return new_term(term, &temp, NULL, num);
270216fa7e82SJiri Olsa }
270316fa7e82SJiri Olsa 
27046cee6cd3SArnaldo Carvalho de Melo int parse_events_term__str(struct parse_events_term **term,
2705cecf3a2eSJiri Olsa 			   int type_term, char *config, char *str,
2706bb78ce7dSAdrian Hunter 			   void *loc_term_, void *loc_val_)
270716fa7e82SJiri Olsa {
2708bb78ce7dSAdrian Hunter 	YYLTYPE *loc_term = loc_term_;
2709bb78ce7dSAdrian Hunter 	YYLTYPE *loc_val = loc_val_;
2710bb78ce7dSAdrian Hunter 
271167b49b38SJiri Olsa 	struct parse_events_term temp = {
271267b49b38SJiri Olsa 		.type_val  = PARSE_EVENTS__TERM_TYPE_STR,
271367b49b38SJiri Olsa 		.type_term = type_term,
271467b49b38SJiri Olsa 		.config    = config,
271567b49b38SJiri Olsa 		.err_term  = loc_term ? loc_term->first_column : 0,
271667b49b38SJiri Olsa 		.err_val   = loc_val  ? loc_val->first_column  : 0,
271767b49b38SJiri Olsa 	};
271867b49b38SJiri Olsa 
271967b49b38SJiri Olsa 	return new_term(term, &temp, str, 0);
272016fa7e82SJiri Olsa }
272116fa7e82SJiri Olsa 
27226cee6cd3SArnaldo Carvalho de Melo int parse_events_term__sym_hw(struct parse_events_term **term,
27231d33d6dcSJiri Olsa 			      char *config, unsigned idx)
27241d33d6dcSJiri Olsa {
27251d33d6dcSJiri Olsa 	struct event_symbol *sym;
272667b49b38SJiri Olsa 	struct parse_events_term temp = {
272767b49b38SJiri Olsa 		.type_val  = PARSE_EVENTS__TERM_TYPE_STR,
272867b49b38SJiri Olsa 		.type_term = PARSE_EVENTS__TERM_TYPE_USER,
272967b49b38SJiri Olsa 		.config    = config ?: (char *) "event",
273067b49b38SJiri Olsa 	};
27311d33d6dcSJiri Olsa 
27321d33d6dcSJiri Olsa 	BUG_ON(idx >= PERF_COUNT_HW_MAX);
27331d33d6dcSJiri Olsa 	sym = &event_symbols_hw[idx];
27341d33d6dcSJiri Olsa 
273567b49b38SJiri Olsa 	return new_term(term, &temp, (char *) sym->symbol, 0);
27361d33d6dcSJiri Olsa }
27371d33d6dcSJiri Olsa 
27386cee6cd3SArnaldo Carvalho de Melo int parse_events_term__clone(struct parse_events_term **new,
27396cee6cd3SArnaldo Carvalho de Melo 			     struct parse_events_term *term)
2740a6146d50SZheng Yan {
274167b49b38SJiri Olsa 	struct parse_events_term temp = {
274267b49b38SJiri Olsa 		.type_val  = term->type_val,
274367b49b38SJiri Olsa 		.type_term = term->type_term,
274467b49b38SJiri Olsa 		.config    = term->config,
274567b49b38SJiri Olsa 		.err_term  = term->err_term,
274667b49b38SJiri Olsa 		.err_val   = term->err_val,
274767b49b38SJiri Olsa 	};
274867b49b38SJiri Olsa 
274967b49b38SJiri Olsa 	return new_term(new, &temp, term->val.str, term->val.num);
2750a6146d50SZheng Yan }
2751a6146d50SZheng Yan 
27528255718fSAndi Kleen int parse_events_copy_term_list(struct list_head *old,
27538255718fSAndi Kleen 				 struct list_head **new)
27548255718fSAndi Kleen {
27558255718fSAndi Kleen 	struct parse_events_term *term, *n;
27568255718fSAndi Kleen 	int ret;
27578255718fSAndi Kleen 
27588255718fSAndi Kleen 	if (!old) {
27598255718fSAndi Kleen 		*new = NULL;
27608255718fSAndi Kleen 		return 0;
27618255718fSAndi Kleen 	}
27628255718fSAndi Kleen 
27638255718fSAndi Kleen 	*new = malloc(sizeof(struct list_head));
27648255718fSAndi Kleen 	if (!*new)
27658255718fSAndi Kleen 		return -ENOMEM;
27668255718fSAndi Kleen 	INIT_LIST_HEAD(*new);
27678255718fSAndi Kleen 
27688255718fSAndi Kleen 	list_for_each_entry (term, old, list) {
27698255718fSAndi Kleen 		ret = parse_events_term__clone(&n, term);
27708255718fSAndi Kleen 		if (ret)
27718255718fSAndi Kleen 			return ret;
27728255718fSAndi Kleen 		list_add_tail(&n->list, *new);
27738255718fSAndi Kleen 	}
27748255718fSAndi Kleen 	return 0;
27758255718fSAndi Kleen }
27768255718fSAndi Kleen 
2777fc0a2c1dSArnaldo Carvalho de Melo void parse_events_terms__purge(struct list_head *terms)
27788f707d84SJiri Olsa {
27796cee6cd3SArnaldo Carvalho de Melo 	struct parse_events_term *term, *h;
27808f707d84SJiri Olsa 
2781a8adfcebSWang Nan 	list_for_each_entry_safe(term, h, terms, list) {
27822d055bf2SWang Nan 		if (term->array.nr_ranges)
2783360e071bSTaeung Song 			zfree(&term->array.ranges);
2784a8adfcebSWang Nan 		list_del_init(&term->list);
27858f707d84SJiri Olsa 		free(term);
27868f707d84SJiri Olsa 	}
2787a8adfcebSWang Nan }
2788b39b8393SJiri Olsa 
27892146afc6SArnaldo Carvalho de Melo void parse_events_terms__delete(struct list_head *terms)
2790fc0a2c1dSArnaldo Carvalho de Melo {
27912146afc6SArnaldo Carvalho de Melo 	if (!terms)
27922146afc6SArnaldo Carvalho de Melo 		return;
2793fc0a2c1dSArnaldo Carvalho de Melo 	parse_events_terms__purge(terms);
2794d20a5f2bSWang Nan 	free(terms);
2795fc0a2c1dSArnaldo Carvalho de Melo }
2796fc0a2c1dSArnaldo Carvalho de Melo 
27972d055bf2SWang Nan void parse_events__clear_array(struct parse_events_array *a)
27982d055bf2SWang Nan {
2799360e071bSTaeung Song 	zfree(&a->ranges);
28002d055bf2SWang Nan }
28012d055bf2SWang Nan 
28025d9cdc11SArnaldo Carvalho de Melo void parse_events_evlist_error(struct parse_events_state *parse_state,
2803b39b8393SJiri Olsa 			       int idx, const char *str)
2804b39b8393SJiri Olsa {
28055d9cdc11SArnaldo Carvalho de Melo 	struct parse_events_error *err = parse_state->error;
2806b39b8393SJiri Olsa 
2807a6ced2beSAdrian Hunter 	if (!err)
2808a6ced2beSAdrian Hunter 		return;
2809b39b8393SJiri Olsa 	err->idx = idx;
2810b39b8393SJiri Olsa 	err->str = strdup(str);
2811b39b8393SJiri Olsa 	WARN_ONCE(!err->str, "WARNING: failed to allocate error string");
2812b39b8393SJiri Olsa }
2813ffeb883eSHe Kuang 
281417cb5f84SWang Nan static void config_terms_list(char *buf, size_t buf_sz)
281517cb5f84SWang Nan {
281617cb5f84SWang Nan 	int i;
281717cb5f84SWang Nan 	bool first = true;
281817cb5f84SWang Nan 
281917cb5f84SWang Nan 	buf[0] = '\0';
282017cb5f84SWang Nan 	for (i = 0; i < __PARSE_EVENTS__TERM_TYPE_NR; i++) {
282117cb5f84SWang Nan 		const char *name = config_term_names[i];
282217cb5f84SWang Nan 
28231669e509SWang Nan 		if (!config_term_avail(i, NULL))
28241669e509SWang Nan 			continue;
282517cb5f84SWang Nan 		if (!name)
282617cb5f84SWang Nan 			continue;
282717cb5f84SWang Nan 		if (name[0] == '<')
282817cb5f84SWang Nan 			continue;
282917cb5f84SWang Nan 
283017cb5f84SWang Nan 		if (strlen(buf) + strlen(name) + 2 >= buf_sz)
283117cb5f84SWang Nan 			return;
283217cb5f84SWang Nan 
283317cb5f84SWang Nan 		if (!first)
283417cb5f84SWang Nan 			strcat(buf, ",");
283517cb5f84SWang Nan 		else
283617cb5f84SWang Nan 			first = false;
283717cb5f84SWang Nan 		strcat(buf, name);
283817cb5f84SWang Nan 	}
283917cb5f84SWang Nan }
284017cb5f84SWang Nan 
2841ffeb883eSHe Kuang /*
2842ffeb883eSHe Kuang  * Return string contains valid config terms of an event.
2843ffeb883eSHe Kuang  * @additional_terms: For terms such as PMU sysfs terms.
2844ffeb883eSHe Kuang  */
2845ffeb883eSHe Kuang char *parse_events_formats_error_string(char *additional_terms)
2846ffeb883eSHe Kuang {
2847ffeb883eSHe Kuang 	char *str;
2848626a6b78SWang Nan 	/* "no-overwrite" is the longest name */
284917cb5f84SWang Nan 	char static_terms[__PARSE_EVENTS__TERM_TYPE_NR *
2850626a6b78SWang Nan 			  (sizeof("no-overwrite") - 1)];
2851ffeb883eSHe Kuang 
285217cb5f84SWang Nan 	config_terms_list(static_terms, sizeof(static_terms));
2853ffeb883eSHe Kuang 	/* valid terms */
2854ffeb883eSHe Kuang 	if (additional_terms) {
285526dee028SWang Nan 		if (asprintf(&str, "valid terms: %s,%s",
285626dee028SWang Nan 			     additional_terms, static_terms) < 0)
2857ffeb883eSHe Kuang 			goto fail;
2858ffeb883eSHe Kuang 	} else {
285926dee028SWang Nan 		if (asprintf(&str, "valid terms: %s", static_terms) < 0)
2860ffeb883eSHe Kuang 			goto fail;
2861ffeb883eSHe Kuang 	}
2862ffeb883eSHe Kuang 	return str;
2863ffeb883eSHe Kuang 
2864ffeb883eSHe Kuang fail:
2865ffeb883eSHe Kuang 	return NULL;
2866ffeb883eSHe Kuang }
2867