xref: /linux/tools/perf/util/parse-events.c (revision e56fbc9dc79ce0fdc49ffadd062214ddd02f65b6)
1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
2d2709c7cSDavid Howells #include <linux/hw_breakpoint.h>
38dd2a131SJiri Olsa #include <linux/err.h>
47f7c536fSArnaldo Carvalho de Melo #include <linux/zalloc.h>
576b31a29SArnaldo Carvalho de Melo #include <dirent.h>
6a43783aeSArnaldo Carvalho de Melo #include <errno.h>
786a5e0c2SArnaldo Carvalho de Melo #include <sys/ioctl.h>
8bafae98eSArnaldo Carvalho de Melo #include <sys/types.h>
9bafae98eSArnaldo Carvalho de Melo #include <sys/stat.h>
10bafae98eSArnaldo Carvalho de Melo #include <fcntl.h>
11391e4206SArnaldo Carvalho de Melo #include <sys/param.h>
12b0742e90SArnaldo Carvalho de Melo #include "term.h"
136b58e7f1SUlrich Drepper #include "../perf.h"
14361c99a6SArnaldo Carvalho de Melo #include "evlist.h"
1569aad6f1SArnaldo Carvalho de Melo #include "evsel.h"
164b6ab94eSJosh Poimboeuf #include <subcmd/parse-options.h>
1786470930SIngo Molnar #include "parse-events.h"
184b6ab94eSJosh Poimboeuf #include <subcmd/exec-cmd.h>
19a067558eSArnaldo Carvalho de Melo #include "string2.h"
208ec20b17SArnaldo Carvalho de Melo #include "strlist.h"
215aab621bSArnaldo Carvalho de Melo #include "symbol.h"
225beeded1SJason Baron #include "cache.h"
238755a8f2SArjan van de Ven #include "header.h"
2484c86ca1SWang Nan #include "bpf-loader.h"
256e81c74cSMasami Hiramatsu #include "debug.h"
26592d5a6bSJiri Olsa #include <api/fs/tracing_path.h>
27ac20de6fSZheng Yan #include "parse-events-bison.h"
2890e2b22dSJiri Olsa #define YY_EXTRA_TYPE int
2989812fc8SJiri Olsa #include "parse-events-flex.h"
305f537a26SJiri Olsa #include "pmu.h"
31b41f1cecSNamhyung Kim #include "thread_map.h"
32f30a79b0SJiri Olsa #include "cpumap.h"
3340218daeSMasami Hiramatsu #include "probe-file.h"
34b39b8393SJiri Olsa #include "asm/bug.h"
35ac12f676SAndi Kleen #include "util/parse-branch-options.h"
3671b0acceSAndi Kleen #include "metricgroup.h"
3789812fc8SJiri Olsa 
3889812fc8SJiri Olsa #define MAX_NAME_LEN 100
3986470930SIngo Molnar 
4082ba1f2fSJiri Olsa #ifdef PARSER_DEBUG
4182ba1f2fSJiri Olsa extern int parse_events_debug;
4282ba1f2fSJiri Olsa #endif
435d9cdc11SArnaldo Carvalho de Melo int parse_events_parse(void *parse_state, void *scanner);
44e637d177SHe Kuang static int get_config_terms(struct list_head *head_config,
45e637d177SHe Kuang 			    struct list_head *head_terms __maybe_unused);
46bcd3279fSFrederic Weisbecker 
47dcb4e102SKan Liang static struct perf_pmu_event_symbol *perf_pmu_events_list;
48dcb4e102SKan Liang /*
49dcb4e102SKan Liang  * The variable indicates the number of supported pmu event symbols.
50dcb4e102SKan Liang  * 0 means not initialized and ready to init
51dcb4e102SKan Liang  * -1 means failed to init, don't try anymore
52dcb4e102SKan Liang  * >0 is the number of supported pmu event symbols
53dcb4e102SKan Liang  */
54dcb4e102SKan Liang static int perf_pmu_events_list_num;
55dcb4e102SKan Liang 
56705750f2SYunlong Song struct event_symbol event_symbols_hw[PERF_COUNT_HW_MAX] = {
571dc12760SJiri Olsa 	[PERF_COUNT_HW_CPU_CYCLES] = {
581dc12760SJiri Olsa 		.symbol = "cpu-cycles",
591dc12760SJiri Olsa 		.alias  = "cycles",
601dc12760SJiri Olsa 	},
611dc12760SJiri Olsa 	[PERF_COUNT_HW_INSTRUCTIONS] = {
621dc12760SJiri Olsa 		.symbol = "instructions",
631dc12760SJiri Olsa 		.alias  = "",
641dc12760SJiri Olsa 	},
651dc12760SJiri Olsa 	[PERF_COUNT_HW_CACHE_REFERENCES] = {
661dc12760SJiri Olsa 		.symbol = "cache-references",
671dc12760SJiri Olsa 		.alias  = "",
681dc12760SJiri Olsa 	},
691dc12760SJiri Olsa 	[PERF_COUNT_HW_CACHE_MISSES] = {
701dc12760SJiri Olsa 		.symbol = "cache-misses",
711dc12760SJiri Olsa 		.alias  = "",
721dc12760SJiri Olsa 	},
731dc12760SJiri Olsa 	[PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = {
741dc12760SJiri Olsa 		.symbol = "branch-instructions",
751dc12760SJiri Olsa 		.alias  = "branches",
761dc12760SJiri Olsa 	},
771dc12760SJiri Olsa 	[PERF_COUNT_HW_BRANCH_MISSES] = {
781dc12760SJiri Olsa 		.symbol = "branch-misses",
791dc12760SJiri Olsa 		.alias  = "",
801dc12760SJiri Olsa 	},
811dc12760SJiri Olsa 	[PERF_COUNT_HW_BUS_CYCLES] = {
821dc12760SJiri Olsa 		.symbol = "bus-cycles",
831dc12760SJiri Olsa 		.alias  = "",
841dc12760SJiri Olsa 	},
851dc12760SJiri Olsa 	[PERF_COUNT_HW_STALLED_CYCLES_FRONTEND] = {
861dc12760SJiri Olsa 		.symbol = "stalled-cycles-frontend",
871dc12760SJiri Olsa 		.alias  = "idle-cycles-frontend",
881dc12760SJiri Olsa 	},
891dc12760SJiri Olsa 	[PERF_COUNT_HW_STALLED_CYCLES_BACKEND] = {
901dc12760SJiri Olsa 		.symbol = "stalled-cycles-backend",
911dc12760SJiri Olsa 		.alias  = "idle-cycles-backend",
921dc12760SJiri Olsa 	},
931dc12760SJiri Olsa 	[PERF_COUNT_HW_REF_CPU_CYCLES] = {
941dc12760SJiri Olsa 		.symbol = "ref-cycles",
951dc12760SJiri Olsa 		.alias  = "",
961dc12760SJiri Olsa 	},
971dc12760SJiri Olsa };
9886470930SIngo Molnar 
99705750f2SYunlong Song struct event_symbol event_symbols_sw[PERF_COUNT_SW_MAX] = {
1001dc12760SJiri Olsa 	[PERF_COUNT_SW_CPU_CLOCK] = {
1011dc12760SJiri Olsa 		.symbol = "cpu-clock",
1021dc12760SJiri Olsa 		.alias  = "",
1031dc12760SJiri Olsa 	},
1041dc12760SJiri Olsa 	[PERF_COUNT_SW_TASK_CLOCK] = {
1051dc12760SJiri Olsa 		.symbol = "task-clock",
1061dc12760SJiri Olsa 		.alias  = "",
1071dc12760SJiri Olsa 	},
1081dc12760SJiri Olsa 	[PERF_COUNT_SW_PAGE_FAULTS] = {
1091dc12760SJiri Olsa 		.symbol = "page-faults",
1101dc12760SJiri Olsa 		.alias  = "faults",
1111dc12760SJiri Olsa 	},
1121dc12760SJiri Olsa 	[PERF_COUNT_SW_CONTEXT_SWITCHES] = {
1131dc12760SJiri Olsa 		.symbol = "context-switches",
1141dc12760SJiri Olsa 		.alias  = "cs",
1151dc12760SJiri Olsa 	},
1161dc12760SJiri Olsa 	[PERF_COUNT_SW_CPU_MIGRATIONS] = {
1171dc12760SJiri Olsa 		.symbol = "cpu-migrations",
1181dc12760SJiri Olsa 		.alias  = "migrations",
1191dc12760SJiri Olsa 	},
1201dc12760SJiri Olsa 	[PERF_COUNT_SW_PAGE_FAULTS_MIN] = {
1211dc12760SJiri Olsa 		.symbol = "minor-faults",
1221dc12760SJiri Olsa 		.alias  = "",
1231dc12760SJiri Olsa 	},
1241dc12760SJiri Olsa 	[PERF_COUNT_SW_PAGE_FAULTS_MAJ] = {
1251dc12760SJiri Olsa 		.symbol = "major-faults",
1261dc12760SJiri Olsa 		.alias  = "",
1271dc12760SJiri Olsa 	},
1281dc12760SJiri Olsa 	[PERF_COUNT_SW_ALIGNMENT_FAULTS] = {
1291dc12760SJiri Olsa 		.symbol = "alignment-faults",
1301dc12760SJiri Olsa 		.alias  = "",
1311dc12760SJiri Olsa 	},
1321dc12760SJiri Olsa 	[PERF_COUNT_SW_EMULATION_FAULTS] = {
1331dc12760SJiri Olsa 		.symbol = "emulation-faults",
1341dc12760SJiri Olsa 		.alias  = "",
1351dc12760SJiri Olsa 	},
136d22d1a2aSAdrian Hunter 	[PERF_COUNT_SW_DUMMY] = {
137d22d1a2aSAdrian Hunter 		.symbol = "dummy",
138d22d1a2aSAdrian Hunter 		.alias  = "",
139d22d1a2aSAdrian Hunter 	},
140bae9cc41SArnaldo Carvalho de Melo 	[PERF_COUNT_SW_BPF_OUTPUT] = {
141bae9cc41SArnaldo Carvalho de Melo 		.symbol = "bpf-output",
142bae9cc41SArnaldo Carvalho de Melo 		.alias  = "",
143bae9cc41SArnaldo Carvalho de Melo 	},
14486470930SIngo Molnar };
14586470930SIngo Molnar 
146cdd6c482SIngo Molnar #define __PERF_EVENT_FIELD(config, name) \
147cdd6c482SIngo Molnar 	((config & PERF_EVENT_##name##_MASK) >> PERF_EVENT_##name##_SHIFT)
14886470930SIngo Molnar 
149cdd6c482SIngo Molnar #define PERF_EVENT_RAW(config)		__PERF_EVENT_FIELD(config, RAW)
150cdd6c482SIngo Molnar #define PERF_EVENT_CONFIG(config)	__PERF_EVENT_FIELD(config, CONFIG)
151cdd6c482SIngo Molnar #define PERF_EVENT_TYPE(config)		__PERF_EVENT_FIELD(config, TYPE)
152cdd6c482SIngo Molnar #define PERF_EVENT_ID(config)		__PERF_EVENT_FIELD(config, EVENT)
15386470930SIngo Molnar 
15422a9f41bSArnaldo Carvalho de Melo #define for_each_subsystem(sys_dir, sys_dirent)			\
15522a9f41bSArnaldo Carvalho de Melo 	while ((sys_dirent = readdir(sys_dir)) != NULL)		\
15622a9f41bSArnaldo Carvalho de Melo 		if (sys_dirent->d_type == DT_DIR &&		\
15722a9f41bSArnaldo Carvalho de Melo 		    (strcmp(sys_dirent->d_name, ".")) &&	\
15822a9f41bSArnaldo Carvalho de Melo 		    (strcmp(sys_dirent->d_name, "..")))
159f6bdafefSJason Baron 
160c02cab22SArnaldo Carvalho de Melo static int tp_event_has_id(const char *dir_path, struct dirent *evt_dir)
161ae07b63fSPeter Zijlstra {
162ae07b63fSPeter Zijlstra 	char evt_path[MAXPATHLEN];
163ae07b63fSPeter Zijlstra 	int fd;
164ae07b63fSPeter Zijlstra 
165c02cab22SArnaldo Carvalho de Melo 	snprintf(evt_path, MAXPATHLEN, "%s/%s/id", dir_path, evt_dir->d_name);
166ae07b63fSPeter Zijlstra 	fd = open(evt_path, O_RDONLY);
167ae07b63fSPeter Zijlstra 	if (fd < 0)
168ae07b63fSPeter Zijlstra 		return -EINVAL;
169ae07b63fSPeter Zijlstra 	close(fd);
170ae07b63fSPeter Zijlstra 
171ae07b63fSPeter Zijlstra 	return 0;
172ae07b63fSPeter Zijlstra }
173ae07b63fSPeter Zijlstra 
174c02cab22SArnaldo Carvalho de Melo #define for_each_event(dir_path, evt_dir, evt_dirent)		\
17522a9f41bSArnaldo Carvalho de Melo 	while ((evt_dirent = readdir(evt_dir)) != NULL)		\
17622a9f41bSArnaldo Carvalho de Melo 		if (evt_dirent->d_type == DT_DIR &&		\
17722a9f41bSArnaldo Carvalho de Melo 		    (strcmp(evt_dirent->d_name, ".")) &&	\
17822a9f41bSArnaldo Carvalho de Melo 		    (strcmp(evt_dirent->d_name, "..")) &&	\
179c02cab22SArnaldo Carvalho de Melo 		    (!tp_event_has_id(dir_path, evt_dirent)))
180f6bdafefSJason Baron 
181270bbbe8SLi Zefan #define MAX_EVENT_LENGTH 512
182f6bdafefSJason Baron 
183f6bdafefSJason Baron 
1841ef2ed10SFrederic Weisbecker struct tracepoint_path *tracepoint_id_to_path(u64 config)
185f6bdafefSJason Baron {
1861ef2ed10SFrederic Weisbecker 	struct tracepoint_path *path = NULL;
187f6bdafefSJason Baron 	DIR *sys_dir, *evt_dir;
18822a9f41bSArnaldo Carvalho de Melo 	struct dirent *sys_dirent, *evt_dirent;
1898aa8a7c8SStephane Eranian 	char id_buf[24];
190725b1368SEric Dumazet 	int fd;
191f6bdafefSJason Baron 	u64 id;
192f6bdafefSJason Baron 	char evt_path[MAXPATHLEN];
19325a7d914SArnaldo Carvalho de Melo 	char *dir_path;
194f6bdafefSJason Baron 
1957014e0e3SArnaldo Carvalho de Melo 	sys_dir = tracing_events__opendir();
196f6bdafefSJason Baron 	if (!sys_dir)
197725b1368SEric Dumazet 		return NULL;
198f6bdafefSJason Baron 
19922a9f41bSArnaldo Carvalho de Melo 	for_each_subsystem(sys_dir, sys_dirent) {
20025a7d914SArnaldo Carvalho de Melo 		dir_path = get_events_file(sys_dirent->d_name);
20125a7d914SArnaldo Carvalho de Melo 		if (!dir_path)
20225a7d914SArnaldo Carvalho de Melo 			continue;
203725b1368SEric Dumazet 		evt_dir = opendir(dir_path);
204725b1368SEric Dumazet 		if (!evt_dir)
20525a7d914SArnaldo Carvalho de Melo 			goto next;
206725b1368SEric Dumazet 
207c02cab22SArnaldo Carvalho de Melo 		for_each_event(dir_path, evt_dir, evt_dirent) {
208725b1368SEric Dumazet 
20977f18153SJiri Olsa 			scnprintf(evt_path, MAXPATHLEN, "%s/%s/id", dir_path,
21022a9f41bSArnaldo Carvalho de Melo 				  evt_dirent->d_name);
211725b1368SEric Dumazet 			fd = open(evt_path, O_RDONLY);
212f6bdafefSJason Baron 			if (fd < 0)
213f6bdafefSJason Baron 				continue;
214f6bdafefSJason Baron 			if (read(fd, id_buf, sizeof(id_buf)) < 0) {
215f6bdafefSJason Baron 				close(fd);
216f6bdafefSJason Baron 				continue;
217f6bdafefSJason Baron 			}
218f6bdafefSJason Baron 			close(fd);
219f6bdafefSJason Baron 			id = atoll(id_buf);
220f6bdafefSJason Baron 			if (id == config) {
22125a7d914SArnaldo Carvalho de Melo 				put_events_file(dir_path);
222f6bdafefSJason Baron 				closedir(evt_dir);
223f6bdafefSJason Baron 				closedir(sys_dir);
22459b4caebSJulia Lawall 				path = zalloc(sizeof(*path));
2255aa365f2STaeung Song 				if (!path)
2265aa365f2STaeung Song 					return NULL;
2271ef2ed10SFrederic Weisbecker 				path->system = malloc(MAX_EVENT_LENGTH);
2281ef2ed10SFrederic Weisbecker 				if (!path->system) {
2291ef2ed10SFrederic Weisbecker 					free(path);
2301ef2ed10SFrederic Weisbecker 					return NULL;
2311ef2ed10SFrederic Weisbecker 				}
2321ef2ed10SFrederic Weisbecker 				path->name = malloc(MAX_EVENT_LENGTH);
2331ef2ed10SFrederic Weisbecker 				if (!path->name) {
23474cf249dSArnaldo Carvalho de Melo 					zfree(&path->system);
2351ef2ed10SFrederic Weisbecker 					free(path);
2361ef2ed10SFrederic Weisbecker 					return NULL;
2371ef2ed10SFrederic Weisbecker 				}
23822a9f41bSArnaldo Carvalho de Melo 				strncpy(path->system, sys_dirent->d_name,
2391ef2ed10SFrederic Weisbecker 					MAX_EVENT_LENGTH);
24022a9f41bSArnaldo Carvalho de Melo 				strncpy(path->name, evt_dirent->d_name,
2411ef2ed10SFrederic Weisbecker 					MAX_EVENT_LENGTH);
2421ef2ed10SFrederic Weisbecker 				return path;
243f6bdafefSJason Baron 			}
244f6bdafefSJason Baron 		}
245f6bdafefSJason Baron 		closedir(evt_dir);
24625a7d914SArnaldo Carvalho de Melo next:
24725a7d914SArnaldo Carvalho de Melo 		put_events_file(dir_path);
248f6bdafefSJason Baron 	}
249f6bdafefSJason Baron 
250f6bdafefSJason Baron 	closedir(sys_dir);
2511ef2ed10SFrederic Weisbecker 	return NULL;
2521ef2ed10SFrederic Weisbecker }
2531ef2ed10SFrederic Weisbecker 
254e7c93f09SNamhyung Kim struct tracepoint_path *tracepoint_name_to_path(const char *name)
255e7c93f09SNamhyung Kim {
256e7c93f09SNamhyung Kim 	struct tracepoint_path *path = zalloc(sizeof(*path));
257e7c93f09SNamhyung Kim 	char *str = strchr(name, ':');
258e7c93f09SNamhyung Kim 
259e7c93f09SNamhyung Kim 	if (path == NULL || str == NULL) {
260e7c93f09SNamhyung Kim 		free(path);
261e7c93f09SNamhyung Kim 		return NULL;
262e7c93f09SNamhyung Kim 	}
263e7c93f09SNamhyung Kim 
264e7c93f09SNamhyung Kim 	path->system = strndup(name, str - name);
265e7c93f09SNamhyung Kim 	path->name = strdup(str+1);
266e7c93f09SNamhyung Kim 
267e7c93f09SNamhyung Kim 	if (path->system == NULL || path->name == NULL) {
26874cf249dSArnaldo Carvalho de Melo 		zfree(&path->system);
26974cf249dSArnaldo Carvalho de Melo 		zfree(&path->name);
270506fde11STaeung Song 		zfree(&path);
271e7c93f09SNamhyung Kim 	}
272e7c93f09SNamhyung Kim 
273e7c93f09SNamhyung Kim 	return path;
274e7c93f09SNamhyung Kim }
275e7c93f09SNamhyung Kim 
2761424dc96SDavid Ahern const char *event_type(int type)
2771424dc96SDavid Ahern {
2781424dc96SDavid Ahern 	switch (type) {
2791424dc96SDavid Ahern 	case PERF_TYPE_HARDWARE:
2801424dc96SDavid Ahern 		return "hardware";
2811424dc96SDavid Ahern 
2821424dc96SDavid Ahern 	case PERF_TYPE_SOFTWARE:
2831424dc96SDavid Ahern 		return "software";
2841424dc96SDavid Ahern 
2851424dc96SDavid Ahern 	case PERF_TYPE_TRACEPOINT:
2861424dc96SDavid Ahern 		return "tracepoint";
2871424dc96SDavid Ahern 
2881424dc96SDavid Ahern 	case PERF_TYPE_HW_CACHE:
2891424dc96SDavid Ahern 		return "hardware-cache";
2901424dc96SDavid Ahern 
2911424dc96SDavid Ahern 	default:
2921424dc96SDavid Ahern 		break;
2931424dc96SDavid Ahern 	}
2941424dc96SDavid Ahern 
2951424dc96SDavid Ahern 	return "unknown";
2961424dc96SDavid Ahern }
2971424dc96SDavid Ahern 
298e814fdddSWang Nan static int parse_events__is_name_term(struct parse_events_term *term)
299e814fdddSWang Nan {
300e814fdddSWang Nan 	return term->type_term == PARSE_EVENTS__TERM_TYPE_NAME;
301e814fdddSWang Nan }
3027ae92e74SYan, Zheng 
303e814fdddSWang Nan static char *get_config_name(struct list_head *head_terms)
304e814fdddSWang Nan {
305e814fdddSWang Nan 	struct parse_events_term *term;
306e814fdddSWang Nan 
307e814fdddSWang Nan 	if (!head_terms)
308e814fdddSWang Nan 		return NULL;
309e814fdddSWang Nan 
310e814fdddSWang Nan 	list_for_each_entry(term, head_terms, list)
311e814fdddSWang Nan 		if (parse_events__is_name_term(term))
312e814fdddSWang Nan 			return term->val.str;
313e814fdddSWang Nan 
314e814fdddSWang Nan 	return NULL;
315e814fdddSWang Nan }
3167ae92e74SYan, Zheng 
317410136f5SStephane Eranian static struct perf_evsel *
318410136f5SStephane Eranian __add_event(struct list_head *list, int *idx,
3197ae92e74SYan, Zheng 	    struct perf_event_attr *attr,
32066ec1191SMark Rutland 	    char *name, struct perf_pmu *pmu,
321f0fbb114SAndi Kleen 	    struct list_head *config_terms, bool auto_merge_stats,
322f0fbb114SAndi Kleen 	    const char *cpu_list)
32389812fc8SJiri Olsa {
32489812fc8SJiri Olsa 	struct perf_evsel *evsel;
325f0fbb114SAndi Kleen 	struct cpu_map *cpus = pmu ? pmu->cpus :
326f0fbb114SAndi Kleen 			       cpu_list ? cpu_map__new(cpu_list) : NULL;
32789812fc8SJiri Olsa 
32889812fc8SJiri Olsa 	event_attr_init(attr);
32989812fc8SJiri Olsa 
33075fc5ae5STaeung Song 	evsel = perf_evsel__new_idx(attr, *idx);
331c5cd8ac0SDavid Ahern 	if (!evsel)
332410136f5SStephane Eranian 		return NULL;
33389812fc8SJiri Olsa 
33475fc5ae5STaeung Song 	(*idx)++;
335f30a79b0SJiri Olsa 	evsel->cpus        = cpu_map__get(cpus);
336fce4d296SAdrian Hunter 	evsel->own_cpus    = cpu_map__get(cpus);
33766ec1191SMark Rutland 	evsel->system_wide = pmu ? pmu->is_uncore : false;
33863ce8449SArnaldo Carvalho de Melo 	evsel->auto_merge_stats = auto_merge_stats;
339f30a79b0SJiri Olsa 
3409db1763cSArnaldo Carvalho de Melo 	if (name)
34189812fc8SJiri Olsa 		evsel->name = strdup(name);
342930a2e29SJiri Olsa 
343930a2e29SJiri Olsa 	if (config_terms)
344930a2e29SJiri Olsa 		list_splice(config_terms, &evsel->config_terms);
345930a2e29SJiri Olsa 
346b847cbdcSJiri Olsa 	list_add_tail(&evsel->node, list);
347410136f5SStephane Eranian 	return evsel;
34889812fc8SJiri Olsa }
34989812fc8SJiri Olsa 
350c5cd8ac0SDavid Ahern static int add_event(struct list_head *list, int *idx,
351930a2e29SJiri Olsa 		     struct perf_event_attr *attr, char *name,
352930a2e29SJiri Olsa 		     struct list_head *config_terms)
3537ae92e74SYan, Zheng {
354f0fbb114SAndi Kleen 	return __add_event(list, idx, attr, name, NULL, config_terms, false, NULL) ? 0 : -ENOMEM;
355f0fbb114SAndi Kleen }
356f0fbb114SAndi Kleen 
357f0fbb114SAndi Kleen static int add_event_tool(struct list_head *list, int *idx,
358f0fbb114SAndi Kleen 			  enum perf_tool_event tool_event)
359f0fbb114SAndi Kleen {
360f0fbb114SAndi Kleen 	struct perf_evsel *evsel;
361f0fbb114SAndi Kleen 	struct perf_event_attr attr = {
362f0fbb114SAndi Kleen 		.type = PERF_TYPE_SOFTWARE,
363f0fbb114SAndi Kleen 		.config = PERF_COUNT_SW_DUMMY,
364f0fbb114SAndi Kleen 	};
365f0fbb114SAndi Kleen 
366f0fbb114SAndi Kleen 	evsel = __add_event(list, idx, &attr, NULL, NULL, NULL, false, "0");
367f0fbb114SAndi Kleen 	if (!evsel)
368f0fbb114SAndi Kleen 		return -ENOMEM;
369f0fbb114SAndi Kleen 	evsel->tool_event = tool_event;
370f0fbb114SAndi Kleen 	if (tool_event == PERF_TOOL_DURATION_TIME)
371f0fbb114SAndi Kleen 		evsel->unit = strdup("ns");
372f0fbb114SAndi Kleen 	return 0;
3737ae92e74SYan, Zheng }
3747ae92e74SYan, Zheng 
3750b668bc9SArnaldo Carvalho de Melo static int parse_aliases(char *str, const char *names[][PERF_EVSEL__MAX_ALIASES], int size)
37686470930SIngo Molnar {
37786470930SIngo Molnar 	int i, j;
37861c45981SPaul Mackerras 	int n, longest = -1;
37986470930SIngo Molnar 
38086470930SIngo Molnar 	for (i = 0; i < size; i++) {
3810b668bc9SArnaldo Carvalho de Melo 		for (j = 0; j < PERF_EVSEL__MAX_ALIASES && names[i][j]; j++) {
38261c45981SPaul Mackerras 			n = strlen(names[i][j]);
38389812fc8SJiri Olsa 			if (n > longest && !strncasecmp(str, names[i][j], n))
38461c45981SPaul Mackerras 				longest = n;
38561c45981SPaul Mackerras 		}
38689812fc8SJiri Olsa 		if (longest > 0)
38786470930SIngo Molnar 			return i;
38886470930SIngo Molnar 	}
38986470930SIngo Molnar 
3908953645fSIngo Molnar 	return -1;
39186470930SIngo Molnar }
39286470930SIngo Molnar 
39343d0b978SWang Nan typedef int config_term_func_t(struct perf_event_attr *attr,
39443d0b978SWang Nan 			       struct parse_events_term *term,
39543d0b978SWang Nan 			       struct parse_events_error *err);
39643d0b978SWang Nan static int config_term_common(struct perf_event_attr *attr,
39743d0b978SWang Nan 			      struct parse_events_term *term,
39843d0b978SWang Nan 			      struct parse_events_error *err);
39943d0b978SWang Nan static int config_attr(struct perf_event_attr *attr,
40043d0b978SWang Nan 		       struct list_head *head,
40143d0b978SWang Nan 		       struct parse_events_error *err,
40243d0b978SWang Nan 		       config_term_func_t config_term);
40343d0b978SWang Nan 
404c5cd8ac0SDavid Ahern int parse_events_add_cache(struct list_head *list, int *idx,
40543d0b978SWang Nan 			   char *type, char *op_result1, char *op_result2,
4062c97b0d4SArnaldo Carvalho de Melo 			   struct parse_events_error *err,
40743d0b978SWang Nan 			   struct list_head *head_config)
40886470930SIngo Molnar {
40989812fc8SJiri Olsa 	struct perf_event_attr attr;
41043d0b978SWang Nan 	LIST_HEAD(config_terms);
41143d0b978SWang Nan 	char name[MAX_NAME_LEN], *config_name;
41261c45981SPaul Mackerras 	int cache_type = -1, cache_op = -1, cache_result = -1;
41389812fc8SJiri Olsa 	char *op_result[2] = { op_result1, op_result2 };
41489812fc8SJiri Olsa 	int i, n;
41586470930SIngo Molnar 
41686470930SIngo Molnar 	/*
41786470930SIngo Molnar 	 * No fallback - if we cannot get a clear cache type
41886470930SIngo Molnar 	 * then bail out:
41986470930SIngo Molnar 	 */
4200b668bc9SArnaldo Carvalho de Melo 	cache_type = parse_aliases(type, perf_evsel__hw_cache,
42189812fc8SJiri Olsa 				   PERF_COUNT_HW_CACHE_MAX);
42286470930SIngo Molnar 	if (cache_type == -1)
42389812fc8SJiri Olsa 		return -EINVAL;
42486470930SIngo Molnar 
42543d0b978SWang Nan 	config_name = get_config_name(head_config);
42689812fc8SJiri Olsa 	n = snprintf(name, MAX_NAME_LEN, "%s", type);
42789812fc8SJiri Olsa 
42889812fc8SJiri Olsa 	for (i = 0; (i < 2) && (op_result[i]); i++) {
42989812fc8SJiri Olsa 		char *str = op_result[i];
43089812fc8SJiri Olsa 
431275ef387SJiri Olsa 		n += snprintf(name + n, MAX_NAME_LEN - n, "-%s", str);
43261c45981SPaul Mackerras 
43361c45981SPaul Mackerras 		if (cache_op == -1) {
4340b668bc9SArnaldo Carvalho de Melo 			cache_op = parse_aliases(str, perf_evsel__hw_cache_op,
43561c45981SPaul Mackerras 						 PERF_COUNT_HW_CACHE_OP_MAX);
43661c45981SPaul Mackerras 			if (cache_op >= 0) {
4370b668bc9SArnaldo Carvalho de Melo 				if (!perf_evsel__is_cache_op_valid(cache_type, cache_op))
43889812fc8SJiri Olsa 					return -EINVAL;
43961c45981SPaul Mackerras 				continue;
44061c45981SPaul Mackerras 			}
44161c45981SPaul Mackerras 		}
44261c45981SPaul Mackerras 
44361c45981SPaul Mackerras 		if (cache_result == -1) {
4440b668bc9SArnaldo Carvalho de Melo 			cache_result = parse_aliases(str, perf_evsel__hw_cache_result,
44561c45981SPaul Mackerras 						     PERF_COUNT_HW_CACHE_RESULT_MAX);
44661c45981SPaul Mackerras 			if (cache_result >= 0)
44761c45981SPaul Mackerras 				continue;
44861c45981SPaul Mackerras 		}
44961c45981SPaul Mackerras 	}
45061c45981SPaul Mackerras 
45186470930SIngo Molnar 	/*
45286470930SIngo Molnar 	 * Fall back to reads:
45386470930SIngo Molnar 	 */
4548953645fSIngo Molnar 	if (cache_op == -1)
4558953645fSIngo Molnar 		cache_op = PERF_COUNT_HW_CACHE_OP_READ;
45686470930SIngo Molnar 
45786470930SIngo Molnar 	/*
45886470930SIngo Molnar 	 * Fall back to accesses:
45986470930SIngo Molnar 	 */
46086470930SIngo Molnar 	if (cache_result == -1)
46186470930SIngo Molnar 		cache_result = PERF_COUNT_HW_CACHE_RESULT_ACCESS;
46286470930SIngo Molnar 
46389812fc8SJiri Olsa 	memset(&attr, 0, sizeof(attr));
46489812fc8SJiri Olsa 	attr.config = cache_type | (cache_op << 8) | (cache_result << 16);
46589812fc8SJiri Olsa 	attr.type = PERF_TYPE_HW_CACHE;
46643d0b978SWang Nan 
46743d0b978SWang Nan 	if (head_config) {
4682c97b0d4SArnaldo Carvalho de Melo 		if (config_attr(&attr, head_config, err,
46943d0b978SWang Nan 				config_term_common))
47043d0b978SWang Nan 			return -EINVAL;
47143d0b978SWang Nan 
47243d0b978SWang Nan 		if (get_config_terms(head_config, &config_terms))
47343d0b978SWang Nan 			return -ENOMEM;
47443d0b978SWang Nan 	}
47543d0b978SWang Nan 	return add_event(list, idx, &attr, config_name ? : name, &config_terms);
47686470930SIngo Molnar }
47786470930SIngo Molnar 
478272ed29aSJiri Olsa static void tracepoint_error(struct parse_events_error *e, int err,
4798c619d6aSWang Nan 			     const char *sys, const char *name)
48019658171SJiri Olsa {
48119658171SJiri Olsa 	char help[BUFSIZ];
48219658171SJiri Olsa 
483ec183d22SAdrian Hunter 	if (!e)
484ec183d22SAdrian Hunter 		return;
485ec183d22SAdrian Hunter 
48619658171SJiri Olsa 	/*
48719658171SJiri Olsa 	 * We get error directly from syscall errno ( > 0),
48819658171SJiri Olsa 	 * or from encoded pointer's error ( < 0).
48919658171SJiri Olsa 	 */
49019658171SJiri Olsa 	err = abs(err);
49119658171SJiri Olsa 
49219658171SJiri Olsa 	switch (err) {
49319658171SJiri Olsa 	case EACCES:
494272ed29aSJiri Olsa 		e->str = strdup("can't access trace events");
49519658171SJiri Olsa 		break;
49619658171SJiri Olsa 	case ENOENT:
497272ed29aSJiri Olsa 		e->str = strdup("unknown tracepoint");
49819658171SJiri Olsa 		break;
49919658171SJiri Olsa 	default:
500272ed29aSJiri Olsa 		e->str = strdup("failed to add tracepoint");
50119658171SJiri Olsa 		break;
50219658171SJiri Olsa 	}
50319658171SJiri Olsa 
50419658171SJiri Olsa 	tracing_path__strerror_open_tp(err, help, sizeof(help), sys, name);
505272ed29aSJiri Olsa 	e->help = strdup(help);
50619658171SJiri Olsa }
50719658171SJiri Olsa 
508c5cd8ac0SDavid Ahern static int add_tracepoint(struct list_head *list, int *idx,
5098c619d6aSWang Nan 			  const char *sys_name, const char *evt_name,
510272ed29aSJiri Olsa 			  struct parse_events_error *err,
511e637d177SHe Kuang 			  struct list_head *head_config)
512bcd3279fSFrederic Weisbecker {
51382fe1c29SArnaldo Carvalho de Melo 	struct perf_evsel *evsel;
514bcd3279fSFrederic Weisbecker 
515ef503831SArnaldo Carvalho de Melo 	evsel = perf_evsel__newtp_idx(sys_name, evt_name, (*idx)++);
51619658171SJiri Olsa 	if (IS_ERR(evsel)) {
517272ed29aSJiri Olsa 		tracepoint_error(err, PTR_ERR(evsel), sys_name, evt_name);
5188dd2a131SJiri Olsa 		return PTR_ERR(evsel);
51919658171SJiri Olsa 	}
520bcd3279fSFrederic Weisbecker 
521e637d177SHe Kuang 	if (head_config) {
522e637d177SHe Kuang 		LIST_HEAD(config_terms);
523e637d177SHe Kuang 
524e637d177SHe Kuang 		if (get_config_terms(head_config, &config_terms))
525e637d177SHe Kuang 			return -ENOMEM;
526e637d177SHe Kuang 		list_splice(&config_terms, &evsel->config_terms);
527e637d177SHe Kuang 	}
528e637d177SHe Kuang 
52982fe1c29SArnaldo Carvalho de Melo 	list_add_tail(&evsel->node, list);
53082fe1c29SArnaldo Carvalho de Melo 	return 0;
531bcd3279fSFrederic Weisbecker }
532bcd3279fSFrederic Weisbecker 
533c5cd8ac0SDavid Ahern static int add_tracepoint_multi_event(struct list_head *list, int *idx,
5348c619d6aSWang Nan 				      const char *sys_name, const char *evt_name,
535272ed29aSJiri Olsa 				      struct parse_events_error *err,
536e637d177SHe Kuang 				      struct list_head *head_config)
537bcd3279fSFrederic Weisbecker {
53825a7d914SArnaldo Carvalho de Melo 	char *evt_path;
539bcd3279fSFrederic Weisbecker 	struct dirent *evt_ent;
540bcd3279fSFrederic Weisbecker 	DIR *evt_dir;
54127bf90bfSJiri Olsa 	int ret = 0, found = 0;
542bcd3279fSFrederic Weisbecker 
54325a7d914SArnaldo Carvalho de Melo 	evt_path = get_events_file(sys_name);
54425a7d914SArnaldo Carvalho de Melo 	if (!evt_path) {
54525a7d914SArnaldo Carvalho de Melo 		tracepoint_error(err, errno, sys_name, evt_name);
54625a7d914SArnaldo Carvalho de Melo 		return -1;
54725a7d914SArnaldo Carvalho de Melo 	}
548bcd3279fSFrederic Weisbecker 	evt_dir = opendir(evt_path);
549bcd3279fSFrederic Weisbecker 	if (!evt_dir) {
55025a7d914SArnaldo Carvalho de Melo 		put_events_file(evt_path);
551272ed29aSJiri Olsa 		tracepoint_error(err, errno, sys_name, evt_name);
55289812fc8SJiri Olsa 		return -1;
553bcd3279fSFrederic Weisbecker 	}
554bcd3279fSFrederic Weisbecker 
55589812fc8SJiri Olsa 	while (!ret && (evt_ent = readdir(evt_dir))) {
556bcd3279fSFrederic Weisbecker 		if (!strcmp(evt_ent->d_name, ".")
557bcd3279fSFrederic Weisbecker 		    || !strcmp(evt_ent->d_name, "..")
558bcd3279fSFrederic Weisbecker 		    || !strcmp(evt_ent->d_name, "enable")
559bcd3279fSFrederic Weisbecker 		    || !strcmp(evt_ent->d_name, "filter"))
560bcd3279fSFrederic Weisbecker 			continue;
561bcd3279fSFrederic Weisbecker 
56289812fc8SJiri Olsa 		if (!strglobmatch(evt_ent->d_name, evt_name))
563fb1d2edfSMasami Hiramatsu 			continue;
564fb1d2edfSMasami Hiramatsu 
56527bf90bfSJiri Olsa 		found++;
56627bf90bfSJiri Olsa 
567e637d177SHe Kuang 		ret = add_tracepoint(list, idx, sys_name, evt_ent->d_name,
568272ed29aSJiri Olsa 				     err, head_config);
569bcd3279fSFrederic Weisbecker 	}
570bcd3279fSFrederic Weisbecker 
57127bf90bfSJiri Olsa 	if (!found) {
57227bf90bfSJiri Olsa 		tracepoint_error(err, ENOENT, sys_name, evt_name);
57327bf90bfSJiri Olsa 		ret = -1;
57427bf90bfSJiri Olsa 	}
57527bf90bfSJiri Olsa 
57625a7d914SArnaldo Carvalho de Melo 	put_events_file(evt_path);
5770bd3f084SJiri Olsa 	closedir(evt_dir);
57889812fc8SJiri Olsa 	return ret;
579bcd3279fSFrederic Weisbecker }
580bcd3279fSFrederic Weisbecker 
581c5cd8ac0SDavid Ahern static int add_tracepoint_event(struct list_head *list, int *idx,
5828c619d6aSWang Nan 				const char *sys_name, const char *evt_name,
583272ed29aSJiri Olsa 				struct parse_events_error *err,
584e637d177SHe Kuang 				struct list_head *head_config)
585f35488f9SJiri Olsa {
586f35488f9SJiri Olsa 	return strpbrk(evt_name, "*?") ?
587e637d177SHe Kuang 	       add_tracepoint_multi_event(list, idx, sys_name, evt_name,
588272ed29aSJiri Olsa 					  err, head_config) :
589e637d177SHe Kuang 	       add_tracepoint(list, idx, sys_name, evt_name,
590272ed29aSJiri Olsa 			      err, head_config);
591f35488f9SJiri Olsa }
592f35488f9SJiri Olsa 
593c5cd8ac0SDavid Ahern static int add_tracepoint_multi_sys(struct list_head *list, int *idx,
5948c619d6aSWang Nan 				    const char *sys_name, const char *evt_name,
595272ed29aSJiri Olsa 				    struct parse_events_error *err,
596e637d177SHe Kuang 				    struct list_head *head_config)
597f35488f9SJiri Olsa {
598f35488f9SJiri Olsa 	struct dirent *events_ent;
599f35488f9SJiri Olsa 	DIR *events_dir;
600f35488f9SJiri Olsa 	int ret = 0;
601f35488f9SJiri Olsa 
6027014e0e3SArnaldo Carvalho de Melo 	events_dir = tracing_events__opendir();
603f35488f9SJiri Olsa 	if (!events_dir) {
604272ed29aSJiri Olsa 		tracepoint_error(err, errno, sys_name, evt_name);
605f35488f9SJiri Olsa 		return -1;
606f35488f9SJiri Olsa 	}
607f35488f9SJiri Olsa 
608f35488f9SJiri Olsa 	while (!ret && (events_ent = readdir(events_dir))) {
609f35488f9SJiri Olsa 		if (!strcmp(events_ent->d_name, ".")
610f35488f9SJiri Olsa 		    || !strcmp(events_ent->d_name, "..")
611f35488f9SJiri Olsa 		    || !strcmp(events_ent->d_name, "enable")
612f35488f9SJiri Olsa 		    || !strcmp(events_ent->d_name, "header_event")
613f35488f9SJiri Olsa 		    || !strcmp(events_ent->d_name, "header_page"))
614f35488f9SJiri Olsa 			continue;
615f35488f9SJiri Olsa 
616f35488f9SJiri Olsa 		if (!strglobmatch(events_ent->d_name, sys_name))
617f35488f9SJiri Olsa 			continue;
618f35488f9SJiri Olsa 
619f35488f9SJiri Olsa 		ret = add_tracepoint_event(list, idx, events_ent->d_name,
620272ed29aSJiri Olsa 					   evt_name, err, head_config);
621f35488f9SJiri Olsa 	}
622f35488f9SJiri Olsa 
623f35488f9SJiri Olsa 	closedir(events_dir);
624f35488f9SJiri Olsa 	return ret;
625f35488f9SJiri Olsa }
626f35488f9SJiri Olsa 
6274edf30e3SWang Nan struct __add_bpf_event_param {
6285d9cdc11SArnaldo Carvalho de Melo 	struct parse_events_state *parse_state;
6294edf30e3SWang Nan 	struct list_head *list;
63095088a59SWang Nan 	struct list_head *head_config;
6314edf30e3SWang Nan };
6324edf30e3SWang Nan 
633cd102d70SWang Nan static int add_bpf_event(const char *group, const char *event, int fd,
6344edf30e3SWang Nan 			 void *_param)
6354edf30e3SWang Nan {
6364edf30e3SWang Nan 	LIST_HEAD(new_evsels);
6374edf30e3SWang Nan 	struct __add_bpf_event_param *param = _param;
6385d9cdc11SArnaldo Carvalho de Melo 	struct parse_events_state *parse_state = param->parse_state;
6394edf30e3SWang Nan 	struct list_head *list = param->list;
6401f45b1d4SWang Nan 	struct perf_evsel *pos;
6414edf30e3SWang Nan 	int err;
6424edf30e3SWang Nan 
6434edf30e3SWang Nan 	pr_debug("add bpf event %s:%s and attach bpf program %d\n",
644cd102d70SWang Nan 		 group, event, fd);
6454edf30e3SWang Nan 
6465d369a75SArnaldo Carvalho de Melo 	err = parse_events_add_tracepoint(&new_evsels, &parse_state->idx, group,
6475d369a75SArnaldo Carvalho de Melo 					  event, parse_state->error,
64895088a59SWang Nan 					  param->head_config);
6494edf30e3SWang Nan 	if (err) {
6504edf30e3SWang Nan 		struct perf_evsel *evsel, *tmp;
6514edf30e3SWang Nan 
6524edf30e3SWang Nan 		pr_debug("Failed to add BPF event %s:%s\n",
653cd102d70SWang Nan 			 group, event);
6544edf30e3SWang Nan 		list_for_each_entry_safe(evsel, tmp, &new_evsels, node) {
655*e56fbc9dSArnaldo Carvalho de Melo 			list_del_init(&evsel->node);
6564edf30e3SWang Nan 			perf_evsel__delete(evsel);
6574edf30e3SWang Nan 		}
6584edf30e3SWang Nan 		return err;
6594edf30e3SWang Nan 	}
660cd102d70SWang Nan 	pr_debug("adding %s:%s\n", group, event);
6614edf30e3SWang Nan 
6621f45b1d4SWang Nan 	list_for_each_entry(pos, &new_evsels, node) {
6631f45b1d4SWang Nan 		pr_debug("adding %s:%s to %p\n",
664cd102d70SWang Nan 			 group, event, pos);
6651f45b1d4SWang Nan 		pos->bpf_fd = fd;
6661f45b1d4SWang Nan 	}
6674edf30e3SWang Nan 	list_splice(&new_evsels, list);
6684edf30e3SWang Nan 	return 0;
6694edf30e3SWang Nan }
6704edf30e3SWang Nan 
6715d9cdc11SArnaldo Carvalho de Melo int parse_events_load_bpf_obj(struct parse_events_state *parse_state,
67284c86ca1SWang Nan 			      struct list_head *list,
67395088a59SWang Nan 			      struct bpf_object *obj,
67495088a59SWang Nan 			      struct list_head *head_config)
67584c86ca1SWang Nan {
67684c86ca1SWang Nan 	int err;
67784c86ca1SWang Nan 	char errbuf[BUFSIZ];
6785d9cdc11SArnaldo Carvalho de Melo 	struct __add_bpf_event_param param = {parse_state, list, head_config};
679aa3abf30SWang Nan 	static bool registered_unprobe_atexit = false;
68084c86ca1SWang Nan 
68184c86ca1SWang Nan 	if (IS_ERR(obj) || !obj) {
68284c86ca1SWang Nan 		snprintf(errbuf, sizeof(errbuf),
68384c86ca1SWang Nan 			 "Internal error: load bpf obj with NULL");
68484c86ca1SWang Nan 		err = -EINVAL;
68584c86ca1SWang Nan 		goto errout;
68684c86ca1SWang Nan 	}
68784c86ca1SWang Nan 
68884c86ca1SWang Nan 	/*
689aa3abf30SWang Nan 	 * Register atexit handler before calling bpf__probe() so
690aa3abf30SWang Nan 	 * bpf__probe() don't need to unprobe probe points its already
691aa3abf30SWang Nan 	 * created when failure.
692aa3abf30SWang Nan 	 */
693aa3abf30SWang Nan 	if (!registered_unprobe_atexit) {
694aa3abf30SWang Nan 		atexit(bpf__clear);
695aa3abf30SWang Nan 		registered_unprobe_atexit = true;
696aa3abf30SWang Nan 	}
697aa3abf30SWang Nan 
698aa3abf30SWang Nan 	err = bpf__probe(obj);
699aa3abf30SWang Nan 	if (err) {
700aa3abf30SWang Nan 		bpf__strerror_probe(obj, err, errbuf, sizeof(errbuf));
701aa3abf30SWang Nan 		goto errout;
702aa3abf30SWang Nan 	}
703aa3abf30SWang Nan 
7041e5e3ee8SWang Nan 	err = bpf__load(obj);
7051e5e3ee8SWang Nan 	if (err) {
7061e5e3ee8SWang Nan 		bpf__strerror_load(obj, err, errbuf, sizeof(errbuf));
7071e5e3ee8SWang Nan 		goto errout;
7081e5e3ee8SWang Nan 	}
7091e5e3ee8SWang Nan 
710cd102d70SWang Nan 	err = bpf__foreach_event(obj, add_bpf_event, &param);
7114edf30e3SWang Nan 	if (err) {
7124edf30e3SWang Nan 		snprintf(errbuf, sizeof(errbuf),
7134edf30e3SWang Nan 			 "Attach events in BPF object failed");
7144edf30e3SWang Nan 		goto errout;
7154edf30e3SWang Nan 	}
7164edf30e3SWang Nan 
7174edf30e3SWang Nan 	return 0;
71884c86ca1SWang Nan errout:
7195d9cdc11SArnaldo Carvalho de Melo 	parse_state->error->help = strdup("(add -v to see detail)");
7205d9cdc11SArnaldo Carvalho de Melo 	parse_state->error->str = strdup(errbuf);
72184c86ca1SWang Nan 	return err;
72284c86ca1SWang Nan }
72384c86ca1SWang Nan 
724a34f3be7SWang Nan static int
7255d9cdc11SArnaldo Carvalho de Melo parse_events_config_bpf(struct parse_events_state *parse_state,
726a34f3be7SWang Nan 			struct bpf_object *obj,
727a34f3be7SWang Nan 			struct list_head *head_config)
728a34f3be7SWang Nan {
729a34f3be7SWang Nan 	struct parse_events_term *term;
730a34f3be7SWang Nan 	int error_pos;
731a34f3be7SWang Nan 
732a34f3be7SWang Nan 	if (!head_config || list_empty(head_config))
733a34f3be7SWang Nan 		return 0;
734a34f3be7SWang Nan 
735a34f3be7SWang Nan 	list_for_each_entry(term, head_config, list) {
736a34f3be7SWang Nan 		char errbuf[BUFSIZ];
737a34f3be7SWang Nan 		int err;
738a34f3be7SWang Nan 
739a34f3be7SWang Nan 		if (term->type_term != PARSE_EVENTS__TERM_TYPE_USER) {
740a34f3be7SWang Nan 			snprintf(errbuf, sizeof(errbuf),
741a34f3be7SWang Nan 				 "Invalid config term for BPF object");
742a34f3be7SWang Nan 			errbuf[BUFSIZ - 1] = '\0';
743a34f3be7SWang Nan 
7445d9cdc11SArnaldo Carvalho de Melo 			parse_state->error->idx = term->err_term;
7455d9cdc11SArnaldo Carvalho de Melo 			parse_state->error->str = strdup(errbuf);
746a34f3be7SWang Nan 			return -EINVAL;
747a34f3be7SWang Nan 		}
748a34f3be7SWang Nan 
7495d9cdc11SArnaldo Carvalho de Melo 		err = bpf__config_obj(obj, term, parse_state->evlist, &error_pos);
750a34f3be7SWang Nan 		if (err) {
7515d9cdc11SArnaldo Carvalho de Melo 			bpf__strerror_config_obj(obj, term, parse_state->evlist,
752a34f3be7SWang Nan 						 &error_pos, err, errbuf,
753a34f3be7SWang Nan 						 sizeof(errbuf));
7545d9cdc11SArnaldo Carvalho de Melo 			parse_state->error->help = strdup(
7557630b3e2SWang Nan "Hint:\tValid config terms:\n"
756e571e029SWang Nan "     \tmap:[<arraymap>].value<indices>=[value]\n"
757e571e029SWang Nan "     \tmap:[<eventmap>].event<indices>=[event]\n"
7587630b3e2SWang Nan "\n"
759e571e029SWang Nan "     \twhere <indices> is something like [0,3...5] or [all]\n"
760a34f3be7SWang Nan "     \t(add -v to see detail)");
7615d9cdc11SArnaldo Carvalho de Melo 			parse_state->error->str = strdup(errbuf);
762a34f3be7SWang Nan 			if (err == -BPF_LOADER_ERRNO__OBJCONF_MAP_VALUE)
7635d9cdc11SArnaldo Carvalho de Melo 				parse_state->error->idx = term->err_val;
764a34f3be7SWang Nan 			else
7655d9cdc11SArnaldo Carvalho de Melo 				parse_state->error->idx = term->err_term + error_pos;
766a34f3be7SWang Nan 			return err;
767a34f3be7SWang Nan 		}
768a34f3be7SWang Nan 	}
769a34f3be7SWang Nan 	return 0;
770a34f3be7SWang Nan }
771a34f3be7SWang Nan 
77295088a59SWang Nan /*
77395088a59SWang Nan  * Split config terms:
77495088a59SWang Nan  * perf record -e bpf.c/call-graph=fp,map:array.value[0]=1/ ...
77595088a59SWang Nan  *  'call-graph=fp' is 'evt config', should be applied to each
77695088a59SWang Nan  *  events in bpf.c.
77795088a59SWang Nan  * 'map:array.value[0]=1' is 'obj config', should be processed
77895088a59SWang Nan  * with parse_events_config_bpf.
77995088a59SWang Nan  *
78095088a59SWang Nan  * Move object config terms from the first list to obj_head_config.
78195088a59SWang Nan  */
78295088a59SWang Nan static void
78395088a59SWang Nan split_bpf_config_terms(struct list_head *evt_head_config,
78495088a59SWang Nan 		       struct list_head *obj_head_config)
78595088a59SWang Nan {
78695088a59SWang Nan 	struct parse_events_term *term, *temp;
78795088a59SWang Nan 
78895088a59SWang Nan 	/*
78995088a59SWang Nan 	 * Currectly, all possible user config term
79095088a59SWang Nan 	 * belong to bpf object. parse_events__is_hardcoded_term()
79195088a59SWang Nan 	 * happends to be a good flag.
79295088a59SWang Nan 	 *
79395088a59SWang Nan 	 * See parse_events_config_bpf() and
79495088a59SWang Nan 	 * config_term_tracepoint().
79595088a59SWang Nan 	 */
79695088a59SWang Nan 	list_for_each_entry_safe(term, temp, evt_head_config, list)
79795088a59SWang Nan 		if (!parse_events__is_hardcoded_term(term))
79895088a59SWang Nan 			list_move_tail(&term->list, obj_head_config);
79995088a59SWang Nan }
80095088a59SWang Nan 
8015d9cdc11SArnaldo Carvalho de Melo int parse_events_load_bpf(struct parse_events_state *parse_state,
80284c86ca1SWang Nan 			  struct list_head *list,
803d509db04SWang Nan 			  char *bpf_file_name,
804a34f3be7SWang Nan 			  bool source,
805a34f3be7SWang Nan 			  struct list_head *head_config)
80684c86ca1SWang Nan {
807a34f3be7SWang Nan 	int err;
80895088a59SWang Nan 	struct bpf_object *obj;
80995088a59SWang Nan 	LIST_HEAD(obj_head_config);
81095088a59SWang Nan 
81195088a59SWang Nan 	if (head_config)
81295088a59SWang Nan 		split_bpf_config_terms(head_config, &obj_head_config);
81384c86ca1SWang Nan 
814d509db04SWang Nan 	obj = bpf__prepare_load(bpf_file_name, source);
8156371ca3bSWang Nan 	if (IS_ERR(obj)) {
81684c86ca1SWang Nan 		char errbuf[BUFSIZ];
81784c86ca1SWang Nan 
8186371ca3bSWang Nan 		err = PTR_ERR(obj);
81984c86ca1SWang Nan 
82084c86ca1SWang Nan 		if (err == -ENOTSUP)
82184c86ca1SWang Nan 			snprintf(errbuf, sizeof(errbuf),
82284c86ca1SWang Nan 				 "BPF support is not compiled");
82384c86ca1SWang Nan 		else
824d3e0ce39SWang Nan 			bpf__strerror_prepare_load(bpf_file_name,
825d3e0ce39SWang Nan 						   source,
826d3e0ce39SWang Nan 						   -err, errbuf,
827d3e0ce39SWang Nan 						   sizeof(errbuf));
82884c86ca1SWang Nan 
8295d9cdc11SArnaldo Carvalho de Melo 		parse_state->error->help = strdup("(add -v to see detail)");
8305d9cdc11SArnaldo Carvalho de Melo 		parse_state->error->str = strdup(errbuf);
83184c86ca1SWang Nan 		return err;
83284c86ca1SWang Nan 	}
83384c86ca1SWang Nan 
8345d9cdc11SArnaldo Carvalho de Melo 	err = parse_events_load_bpf_obj(parse_state, list, obj, head_config);
835a34f3be7SWang Nan 	if (err)
836a34f3be7SWang Nan 		return err;
8375d9cdc11SArnaldo Carvalho de Melo 	err = parse_events_config_bpf(parse_state, obj, &obj_head_config);
83895088a59SWang Nan 
83995088a59SWang Nan 	/*
84095088a59SWang Nan 	 * Caller doesn't know anything about obj_head_config,
84195088a59SWang Nan 	 * so combine them together again before returnning.
84295088a59SWang Nan 	 */
84395088a59SWang Nan 	if (head_config)
84495088a59SWang Nan 		list_splice_tail(&obj_head_config, head_config);
84595088a59SWang Nan 	return err;
84684c86ca1SWang Nan }
84784c86ca1SWang Nan 
84889812fc8SJiri Olsa static int
84989812fc8SJiri Olsa parse_breakpoint_type(const char *type, struct perf_event_attr *attr)
8501b290d67SFrederic Weisbecker {
8511b290d67SFrederic Weisbecker 	int i;
8521b290d67SFrederic Weisbecker 
8531b290d67SFrederic Weisbecker 	for (i = 0; i < 3; i++) {
85489812fc8SJiri Olsa 		if (!type || !type[i])
8551b290d67SFrederic Weisbecker 			break;
8561b290d67SFrederic Weisbecker 
8577582732fSJiri Olsa #define CHECK_SET_TYPE(bit)		\
8587582732fSJiri Olsa do {					\
8597582732fSJiri Olsa 	if (attr->bp_type & bit)	\
8607582732fSJiri Olsa 		return -EINVAL;		\
8617582732fSJiri Olsa 	else				\
8627582732fSJiri Olsa 		attr->bp_type |= bit;	\
8637582732fSJiri Olsa } while (0)
8647582732fSJiri Olsa 
8651b290d67SFrederic Weisbecker 		switch (type[i]) {
8661b290d67SFrederic Weisbecker 		case 'r':
8677582732fSJiri Olsa 			CHECK_SET_TYPE(HW_BREAKPOINT_R);
8681b290d67SFrederic Weisbecker 			break;
8691b290d67SFrederic Weisbecker 		case 'w':
8707582732fSJiri Olsa 			CHECK_SET_TYPE(HW_BREAKPOINT_W);
8711b290d67SFrederic Weisbecker 			break;
8721b290d67SFrederic Weisbecker 		case 'x':
8737582732fSJiri Olsa 			CHECK_SET_TYPE(HW_BREAKPOINT_X);
8741b290d67SFrederic Weisbecker 			break;
8751b290d67SFrederic Weisbecker 		default:
87689812fc8SJiri Olsa 			return -EINVAL;
8771b290d67SFrederic Weisbecker 		}
8781b290d67SFrederic Weisbecker 	}
87989812fc8SJiri Olsa 
8807582732fSJiri Olsa #undef CHECK_SET_TYPE
8817582732fSJiri Olsa 
8821b290d67SFrederic Weisbecker 	if (!attr->bp_type) /* Default */
8831b290d67SFrederic Weisbecker 		attr->bp_type = HW_BREAKPOINT_R | HW_BREAKPOINT_W;
8841b290d67SFrederic Weisbecker 
88589812fc8SJiri Olsa 	return 0;
8861b290d67SFrederic Weisbecker }
8871b290d67SFrederic Weisbecker 
888c5cd8ac0SDavid Ahern int parse_events_add_breakpoint(struct list_head *list, int *idx,
8893741eb9fSJacob Shin 				void *ptr, char *type, u64 len)
8901b290d67SFrederic Weisbecker {
89189812fc8SJiri Olsa 	struct perf_event_attr attr;
8921b290d67SFrederic Weisbecker 
89389812fc8SJiri Olsa 	memset(&attr, 0, sizeof(attr));
8949fafd98fSJiri Olsa 	attr.bp_addr = (unsigned long) ptr;
8951b290d67SFrederic Weisbecker 
89689812fc8SJiri Olsa 	if (parse_breakpoint_type(type, &attr))
89789812fc8SJiri Olsa 		return -EINVAL;
8981b290d67SFrederic Weisbecker 
8993741eb9fSJacob Shin 	/* Provide some defaults if len is not specified */
9003741eb9fSJacob Shin 	if (!len) {
90189812fc8SJiri Olsa 		if (attr.bp_type == HW_BREAKPOINT_X)
9023741eb9fSJacob Shin 			len = sizeof(long);
903aa59a485SFrederic Weisbecker 		else
9043741eb9fSJacob Shin 			len = HW_BREAKPOINT_LEN_4;
9053741eb9fSJacob Shin 	}
9063741eb9fSJacob Shin 
9073741eb9fSJacob Shin 	attr.bp_len = len;
908aa59a485SFrederic Weisbecker 
90989812fc8SJiri Olsa 	attr.type = PERF_TYPE_BREAKPOINT;
9104a841d65SJovi Zhang 	attr.sample_period = 1;
9111b290d67SFrederic Weisbecker 
912930a2e29SJiri Olsa 	return add_event(list, idx, &attr, NULL, NULL);
9131b290d67SFrederic Weisbecker }
9141b290d67SFrederic Weisbecker 
9153b0e371cSJiri Olsa static int check_type_val(struct parse_events_term *term,
9163b0e371cSJiri Olsa 			  struct parse_events_error *err,
9173b0e371cSJiri Olsa 			  int type)
9183b0e371cSJiri Olsa {
9193b0e371cSJiri Olsa 	if (type == term->type_val)
9203b0e371cSJiri Olsa 		return 0;
9213b0e371cSJiri Olsa 
9223b0e371cSJiri Olsa 	if (err) {
9233b0e371cSJiri Olsa 		err->idx = term->err_val;
9243b0e371cSJiri Olsa 		if (type == PARSE_EVENTS__TERM_TYPE_NUM)
9253b0e371cSJiri Olsa 			err->str = strdup("expected numeric value");
9263b0e371cSJiri Olsa 		else
9273b0e371cSJiri Olsa 			err->str = strdup("expected string value");
9283b0e371cSJiri Olsa 	}
9293b0e371cSJiri Olsa 	return -EINVAL;
9303b0e371cSJiri Olsa }
9313b0e371cSJiri Olsa 
93217cb5f84SWang Nan /*
93317cb5f84SWang Nan  * Update according to parse-events.l
93417cb5f84SWang Nan  */
93517cb5f84SWang Nan static const char *config_term_names[__PARSE_EVENTS__TERM_TYPE_NR] = {
93617cb5f84SWang Nan 	[PARSE_EVENTS__TERM_TYPE_USER]			= "<sysfs term>",
93717cb5f84SWang Nan 	[PARSE_EVENTS__TERM_TYPE_CONFIG]		= "config",
93817cb5f84SWang Nan 	[PARSE_EVENTS__TERM_TYPE_CONFIG1]		= "config1",
93917cb5f84SWang Nan 	[PARSE_EVENTS__TERM_TYPE_CONFIG2]		= "config2",
94017cb5f84SWang Nan 	[PARSE_EVENTS__TERM_TYPE_NAME]			= "name",
94117cb5f84SWang Nan 	[PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD]		= "period",
94217cb5f84SWang Nan 	[PARSE_EVENTS__TERM_TYPE_SAMPLE_FREQ]		= "freq",
94317cb5f84SWang Nan 	[PARSE_EVENTS__TERM_TYPE_BRANCH_SAMPLE_TYPE]	= "branch_type",
94417cb5f84SWang Nan 	[PARSE_EVENTS__TERM_TYPE_TIME]			= "time",
94517cb5f84SWang Nan 	[PARSE_EVENTS__TERM_TYPE_CALLGRAPH]		= "call-graph",
94617cb5f84SWang Nan 	[PARSE_EVENTS__TERM_TYPE_STACKSIZE]		= "stack-size",
94717cb5f84SWang Nan 	[PARSE_EVENTS__TERM_TYPE_NOINHERIT]		= "no-inherit",
94817cb5f84SWang Nan 	[PARSE_EVENTS__TERM_TYPE_INHERIT]		= "inherit",
949792d48b4SArnaldo Carvalho de Melo 	[PARSE_EVENTS__TERM_TYPE_MAX_STACK]		= "max-stack",
9502fda5adaSArnaldo Carvalho de Melo 	[PARSE_EVENTS__TERM_TYPE_MAX_EVENTS]		= "nr",
951626a6b78SWang Nan 	[PARSE_EVENTS__TERM_TYPE_OVERWRITE]		= "overwrite",
952626a6b78SWang Nan 	[PARSE_EVENTS__TERM_TYPE_NOOVERWRITE]		= "no-overwrite",
953dd60fba7SMathieu Poirier 	[PARSE_EVENTS__TERM_TYPE_DRV_CFG]		= "driver-config",
954064b4e82SJin Yao 	[PARSE_EVENTS__TERM_TYPE_PERCORE]		= "percore",
95517cb5f84SWang Nan };
95617cb5f84SWang Nan 
9571669e509SWang Nan static bool config_term_shrinked;
9581669e509SWang Nan 
9591669e509SWang Nan static bool
9601669e509SWang Nan config_term_avail(int term_type, struct parse_events_error *err)
9611669e509SWang Nan {
9621669e509SWang Nan 	if (term_type < 0 || term_type >= __PARSE_EVENTS__TERM_TYPE_NR) {
9631669e509SWang Nan 		err->str = strdup("Invalid term_type");
9641669e509SWang Nan 		return false;
9651669e509SWang Nan 	}
9661669e509SWang Nan 	if (!config_term_shrinked)
9671669e509SWang Nan 		return true;
9681669e509SWang Nan 
9691669e509SWang Nan 	switch (term_type) {
9701669e509SWang Nan 	case PARSE_EVENTS__TERM_TYPE_CONFIG:
9711669e509SWang Nan 	case PARSE_EVENTS__TERM_TYPE_CONFIG1:
9721669e509SWang Nan 	case PARSE_EVENTS__TERM_TYPE_CONFIG2:
9731669e509SWang Nan 	case PARSE_EVENTS__TERM_TYPE_NAME:
97406835545SSukadev Bhattiprolu 	case PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD:
975064b4e82SJin Yao 	case PARSE_EVENTS__TERM_TYPE_PERCORE:
9761669e509SWang Nan 		return true;
9771669e509SWang Nan 	default:
9781669e509SWang Nan 		if (!err)
9791669e509SWang Nan 			return false;
9801669e509SWang Nan 
9811669e509SWang Nan 		/* term_type is validated so indexing is safe */
9821669e509SWang Nan 		if (asprintf(&err->str, "'%s' is not usable in 'perf stat'",
9831669e509SWang Nan 			     config_term_names[term_type]) < 0)
9841669e509SWang Nan 			err->str = NULL;
9851669e509SWang Nan 		return false;
9861669e509SWang Nan 	}
9871669e509SWang Nan }
9881669e509SWang Nan 
9891669e509SWang Nan void parse_events__shrink_config_terms(void)
9901669e509SWang Nan {
9911669e509SWang Nan 	config_term_shrinked = true;
9921669e509SWang Nan }
9931669e509SWang Nan 
9940b8891a8SHe Kuang static int config_term_common(struct perf_event_attr *attr,
9953b0e371cSJiri Olsa 			      struct parse_events_term *term,
9963b0e371cSJiri Olsa 			      struct parse_events_error *err)
9978f707d84SJiri Olsa {
99816fa7e82SJiri Olsa #define CHECK_TYPE_VAL(type)						   \
99916fa7e82SJiri Olsa do {									   \
10003b0e371cSJiri Olsa 	if (check_type_val(term, err, PARSE_EVENTS__TERM_TYPE_ ## type)) \
100116fa7e82SJiri Olsa 		return -EINVAL;						   \
100216fa7e82SJiri Olsa } while (0)
100316fa7e82SJiri Olsa 
100416fa7e82SJiri Olsa 	switch (term->type_term) {
10058f707d84SJiri Olsa 	case PARSE_EVENTS__TERM_TYPE_CONFIG:
100616fa7e82SJiri Olsa 		CHECK_TYPE_VAL(NUM);
10078f707d84SJiri Olsa 		attr->config = term->val.num;
10088f707d84SJiri Olsa 		break;
10098f707d84SJiri Olsa 	case PARSE_EVENTS__TERM_TYPE_CONFIG1:
101016fa7e82SJiri Olsa 		CHECK_TYPE_VAL(NUM);
10118f707d84SJiri Olsa 		attr->config1 = term->val.num;
10128f707d84SJiri Olsa 		break;
10138f707d84SJiri Olsa 	case PARSE_EVENTS__TERM_TYPE_CONFIG2:
101416fa7e82SJiri Olsa 		CHECK_TYPE_VAL(NUM);
10158f707d84SJiri Olsa 		attr->config2 = term->val.num;
10168f707d84SJiri Olsa 		break;
10178f707d84SJiri Olsa 	case PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD:
101816fa7e82SJiri Olsa 		CHECK_TYPE_VAL(NUM);
10198f707d84SJiri Olsa 		break;
102009af2a55SNamhyung Kim 	case PARSE_EVENTS__TERM_TYPE_SAMPLE_FREQ:
102109af2a55SNamhyung Kim 		CHECK_TYPE_VAL(NUM);
102209af2a55SNamhyung Kim 		break;
10238f707d84SJiri Olsa 	case PARSE_EVENTS__TERM_TYPE_BRANCH_SAMPLE_TYPE:
1024ac12f676SAndi Kleen 		CHECK_TYPE_VAL(STR);
1025ac12f676SAndi Kleen 		if (strcmp(term->val.str, "no") &&
1026ac12f676SAndi Kleen 		    parse_branch_str(term->val.str, &attr->branch_sample_type)) {
1027ac12f676SAndi Kleen 			err->str = strdup("invalid branch sample type");
1028ac12f676SAndi Kleen 			err->idx = term->err_val;
1029ac12f676SAndi Kleen 			return -EINVAL;
1030ac12f676SAndi Kleen 		}
10318f707d84SJiri Olsa 		break;
103232067712SKan Liang 	case PARSE_EVENTS__TERM_TYPE_TIME:
103332067712SKan Liang 		CHECK_TYPE_VAL(NUM);
103432067712SKan Liang 		if (term->val.num > 1) {
103532067712SKan Liang 			err->str = strdup("expected 0 or 1");
103632067712SKan Liang 			err->idx = term->err_val;
103732067712SKan Liang 			return -EINVAL;
103832067712SKan Liang 		}
103932067712SKan Liang 		break;
1040d457c963SKan Liang 	case PARSE_EVENTS__TERM_TYPE_CALLGRAPH:
1041d457c963SKan Liang 		CHECK_TYPE_VAL(STR);
1042d457c963SKan Liang 		break;
1043d457c963SKan Liang 	case PARSE_EVENTS__TERM_TYPE_STACKSIZE:
1044d457c963SKan Liang 		CHECK_TYPE_VAL(NUM);
1045d457c963SKan Liang 		break;
1046374ce938SWang Nan 	case PARSE_EVENTS__TERM_TYPE_INHERIT:
1047374ce938SWang Nan 		CHECK_TYPE_VAL(NUM);
1048374ce938SWang Nan 		break;
1049374ce938SWang Nan 	case PARSE_EVENTS__TERM_TYPE_NOINHERIT:
1050374ce938SWang Nan 		CHECK_TYPE_VAL(NUM);
1051374ce938SWang Nan 		break;
1052626a6b78SWang Nan 	case PARSE_EVENTS__TERM_TYPE_OVERWRITE:
1053626a6b78SWang Nan 		CHECK_TYPE_VAL(NUM);
1054626a6b78SWang Nan 		break;
1055626a6b78SWang Nan 	case PARSE_EVENTS__TERM_TYPE_NOOVERWRITE:
1056626a6b78SWang Nan 		CHECK_TYPE_VAL(NUM);
1057626a6b78SWang Nan 		break;
10586b5fc39bSJiri Olsa 	case PARSE_EVENTS__TERM_TYPE_NAME:
10596b5fc39bSJiri Olsa 		CHECK_TYPE_VAL(STR);
10606b5fc39bSJiri Olsa 		break;
1061792d48b4SArnaldo Carvalho de Melo 	case PARSE_EVENTS__TERM_TYPE_MAX_STACK:
1062792d48b4SArnaldo Carvalho de Melo 		CHECK_TYPE_VAL(NUM);
1063792d48b4SArnaldo Carvalho de Melo 		break;
10642fda5adaSArnaldo Carvalho de Melo 	case PARSE_EVENTS__TERM_TYPE_MAX_EVENTS:
10652fda5adaSArnaldo Carvalho de Melo 		CHECK_TYPE_VAL(NUM);
10662fda5adaSArnaldo Carvalho de Melo 		break;
1067064b4e82SJin Yao 	case PARSE_EVENTS__TERM_TYPE_PERCORE:
1068064b4e82SJin Yao 		CHECK_TYPE_VAL(NUM);
1069064b4e82SJin Yao 		if ((unsigned int)term->val.num > 1) {
1070064b4e82SJin Yao 			err->str = strdup("expected 0 or 1");
1071064b4e82SJin Yao 			err->idx = term->err_val;
1072064b4e82SJin Yao 			return -EINVAL;
1073064b4e82SJin Yao 		}
1074064b4e82SJin Yao 		break;
10758f707d84SJiri Olsa 	default:
1076ffeb883eSHe Kuang 		err->str = strdup("unknown term");
1077ffeb883eSHe Kuang 		err->idx = term->err_term;
1078ffeb883eSHe Kuang 		err->help = parse_events_formats_error_string(NULL);
10798f707d84SJiri Olsa 		return -EINVAL;
10808f707d84SJiri Olsa 	}
108116fa7e82SJiri Olsa 
10821669e509SWang Nan 	/*
10831669e509SWang Nan 	 * Check term availbility after basic checking so
10841669e509SWang Nan 	 * PARSE_EVENTS__TERM_TYPE_USER can be found and filtered.
10851669e509SWang Nan 	 *
10861669e509SWang Nan 	 * If check availbility at the entry of this function,
10871669e509SWang Nan 	 * user will see "'<sysfs term>' is not usable in 'perf stat'"
10881669e509SWang Nan 	 * if an invalid config term is provided for legacy events
10891669e509SWang Nan 	 * (for example, instructions/badterm/...), which is confusing.
10901669e509SWang Nan 	 */
10911669e509SWang Nan 	if (!config_term_avail(term->type_term, err))
10921669e509SWang Nan 		return -EINVAL;
10938f707d84SJiri Olsa 	return 0;
109416fa7e82SJiri Olsa #undef CHECK_TYPE_VAL
10958f707d84SJiri Olsa }
10968f707d84SJiri Olsa 
10970b8891a8SHe Kuang static int config_term_pmu(struct perf_event_attr *attr,
10980b8891a8SHe Kuang 			   struct parse_events_term *term,
10990b8891a8SHe Kuang 			   struct parse_events_error *err)
11000b8891a8SHe Kuang {
1101dd60fba7SMathieu Poirier 	if (term->type_term == PARSE_EVENTS__TERM_TYPE_USER ||
1102dd60fba7SMathieu Poirier 	    term->type_term == PARSE_EVENTS__TERM_TYPE_DRV_CFG)
11030b8891a8SHe Kuang 		/*
11040b8891a8SHe Kuang 		 * Always succeed for sysfs terms, as we dont know
11050b8891a8SHe Kuang 		 * at this point what type they need to have.
11060b8891a8SHe Kuang 		 */
11070b8891a8SHe Kuang 		return 0;
11080b8891a8SHe Kuang 	else
11090b8891a8SHe Kuang 		return config_term_common(attr, term, err);
11100b8891a8SHe Kuang }
11110b8891a8SHe Kuang 
1112e637d177SHe Kuang static int config_term_tracepoint(struct perf_event_attr *attr,
1113e637d177SHe Kuang 				  struct parse_events_term *term,
1114e637d177SHe Kuang 				  struct parse_events_error *err)
1115e637d177SHe Kuang {
1116e637d177SHe Kuang 	switch (term->type_term) {
1117e637d177SHe Kuang 	case PARSE_EVENTS__TERM_TYPE_CALLGRAPH:
1118e637d177SHe Kuang 	case PARSE_EVENTS__TERM_TYPE_STACKSIZE:
1119374ce938SWang Nan 	case PARSE_EVENTS__TERM_TYPE_INHERIT:
1120374ce938SWang Nan 	case PARSE_EVENTS__TERM_TYPE_NOINHERIT:
1121792d48b4SArnaldo Carvalho de Melo 	case PARSE_EVENTS__TERM_TYPE_MAX_STACK:
11222fda5adaSArnaldo Carvalho de Melo 	case PARSE_EVENTS__TERM_TYPE_MAX_EVENTS:
1123626a6b78SWang Nan 	case PARSE_EVENTS__TERM_TYPE_OVERWRITE:
1124626a6b78SWang Nan 	case PARSE_EVENTS__TERM_TYPE_NOOVERWRITE:
1125e637d177SHe Kuang 		return config_term_common(attr, term, err);
1126e637d177SHe Kuang 	default:
1127e637d177SHe Kuang 		if (err) {
1128e637d177SHe Kuang 			err->idx = term->err_term;
1129e637d177SHe Kuang 			err->str = strdup("unknown term");
1130e637d177SHe Kuang 			err->help = strdup("valid terms: call-graph,stack-size\n");
1131e637d177SHe Kuang 		}
1132e637d177SHe Kuang 		return -EINVAL;
1133e637d177SHe Kuang 	}
1134e637d177SHe Kuang 
1135e637d177SHe Kuang 	return 0;
1136e637d177SHe Kuang }
1137e637d177SHe Kuang 
11388f707d84SJiri Olsa static int config_attr(struct perf_event_attr *attr,
11393b0e371cSJiri Olsa 		       struct list_head *head,
11400b8891a8SHe Kuang 		       struct parse_events_error *err,
11410b8891a8SHe Kuang 		       config_term_func_t config_term)
11428f707d84SJiri Olsa {
11436cee6cd3SArnaldo Carvalho de Melo 	struct parse_events_term *term;
11448f707d84SJiri Olsa 
11458f707d84SJiri Olsa 	list_for_each_entry(term, head, list)
11463b0e371cSJiri Olsa 		if (config_term(attr, term, err))
11478f707d84SJiri Olsa 			return -EINVAL;
11488f707d84SJiri Olsa 
11498f707d84SJiri Olsa 	return 0;
11508f707d84SJiri Olsa }
11518f707d84SJiri Olsa 
1152930a2e29SJiri Olsa static int get_config_terms(struct list_head *head_config,
1153930a2e29SJiri Olsa 			    struct list_head *head_terms __maybe_unused)
1154930a2e29SJiri Olsa {
1155930a2e29SJiri Olsa #define ADD_CONFIG_TERM(__type, __name, __val)			\
1156930a2e29SJiri Olsa do {								\
1157930a2e29SJiri Olsa 	struct perf_evsel_config_term *__t;			\
1158930a2e29SJiri Olsa 								\
1159930a2e29SJiri Olsa 	__t = zalloc(sizeof(*__t));				\
1160930a2e29SJiri Olsa 	if (!__t)						\
1161930a2e29SJiri Olsa 		return -ENOMEM;					\
1162930a2e29SJiri Olsa 								\
1163930a2e29SJiri Olsa 	INIT_LIST_HEAD(&__t->list);				\
1164930a2e29SJiri Olsa 	__t->type       = PERF_EVSEL__CONFIG_TERM_ ## __type;	\
1165930a2e29SJiri Olsa 	__t->val.__name = __val;				\
1166c2f1ceadSAndi Kleen 	__t->weak	= term->weak;				\
1167930a2e29SJiri Olsa 	list_add_tail(&__t->list, head_terms);			\
1168930a2e29SJiri Olsa } while (0)
1169930a2e29SJiri Olsa 
1170930a2e29SJiri Olsa 	struct parse_events_term *term;
1171930a2e29SJiri Olsa 
1172930a2e29SJiri Olsa 	list_for_each_entry(term, head_config, list) {
1173930a2e29SJiri Olsa 		switch (term->type_term) {
1174ee4c7588SJiri Olsa 		case PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD:
1175ee4c7588SJiri Olsa 			ADD_CONFIG_TERM(PERIOD, period, term->val.num);
117632067712SKan Liang 			break;
117709af2a55SNamhyung Kim 		case PARSE_EVENTS__TERM_TYPE_SAMPLE_FREQ:
117809af2a55SNamhyung Kim 			ADD_CONFIG_TERM(FREQ, freq, term->val.num);
117909af2a55SNamhyung Kim 			break;
118032067712SKan Liang 		case PARSE_EVENTS__TERM_TYPE_TIME:
118132067712SKan Liang 			ADD_CONFIG_TERM(TIME, time, term->val.num);
118232067712SKan Liang 			break;
1183d457c963SKan Liang 		case PARSE_EVENTS__TERM_TYPE_CALLGRAPH:
1184d457c963SKan Liang 			ADD_CONFIG_TERM(CALLGRAPH, callgraph, term->val.str);
1185d457c963SKan Liang 			break;
1186ac12f676SAndi Kleen 		case PARSE_EVENTS__TERM_TYPE_BRANCH_SAMPLE_TYPE:
1187ac12f676SAndi Kleen 			ADD_CONFIG_TERM(BRANCH, branch, term->val.str);
1188ac12f676SAndi Kleen 			break;
1189d457c963SKan Liang 		case PARSE_EVENTS__TERM_TYPE_STACKSIZE:
1190d457c963SKan Liang 			ADD_CONFIG_TERM(STACK_USER, stack_user, term->val.num);
1191d457c963SKan Liang 			break;
1192374ce938SWang Nan 		case PARSE_EVENTS__TERM_TYPE_INHERIT:
1193374ce938SWang Nan 			ADD_CONFIG_TERM(INHERIT, inherit, term->val.num ? 1 : 0);
1194374ce938SWang Nan 			break;
1195374ce938SWang Nan 		case PARSE_EVENTS__TERM_TYPE_NOINHERIT:
1196374ce938SWang Nan 			ADD_CONFIG_TERM(INHERIT, inherit, term->val.num ? 0 : 1);
1197374ce938SWang Nan 			break;
1198792d48b4SArnaldo Carvalho de Melo 		case PARSE_EVENTS__TERM_TYPE_MAX_STACK:
1199792d48b4SArnaldo Carvalho de Melo 			ADD_CONFIG_TERM(MAX_STACK, max_stack, term->val.num);
1200792d48b4SArnaldo Carvalho de Melo 			break;
12012fda5adaSArnaldo Carvalho de Melo 		case PARSE_EVENTS__TERM_TYPE_MAX_EVENTS:
12022fda5adaSArnaldo Carvalho de Melo 			ADD_CONFIG_TERM(MAX_EVENTS, max_events, term->val.num);
12032fda5adaSArnaldo Carvalho de Melo 			break;
1204626a6b78SWang Nan 		case PARSE_EVENTS__TERM_TYPE_OVERWRITE:
1205626a6b78SWang Nan 			ADD_CONFIG_TERM(OVERWRITE, overwrite, term->val.num ? 1 : 0);
1206626a6b78SWang Nan 			break;
1207626a6b78SWang Nan 		case PARSE_EVENTS__TERM_TYPE_NOOVERWRITE:
1208626a6b78SWang Nan 			ADD_CONFIG_TERM(OVERWRITE, overwrite, term->val.num ? 0 : 1);
1209626a6b78SWang Nan 			break;
1210dd60fba7SMathieu Poirier 		case PARSE_EVENTS__TERM_TYPE_DRV_CFG:
1211dd60fba7SMathieu Poirier 			ADD_CONFIG_TERM(DRV_CFG, drv_cfg, term->val.str);
1212dd60fba7SMathieu Poirier 			break;
1213064b4e82SJin Yao 		case PARSE_EVENTS__TERM_TYPE_PERCORE:
1214064b4e82SJin Yao 			ADD_CONFIG_TERM(PERCORE, percore,
1215064b4e82SJin Yao 					term->val.num ? true : false);
1216064b4e82SJin Yao 			break;
1217930a2e29SJiri Olsa 		default:
1218930a2e29SJiri Olsa 			break;
1219930a2e29SJiri Olsa 		}
1220930a2e29SJiri Olsa 	}
1221930a2e29SJiri Olsa #undef ADD_EVSEL_CONFIG
1222930a2e29SJiri Olsa 	return 0;
1223930a2e29SJiri Olsa }
1224930a2e29SJiri Olsa 
1225e637d177SHe Kuang int parse_events_add_tracepoint(struct list_head *list, int *idx,
12268c619d6aSWang Nan 				const char *sys, const char *event,
1227272ed29aSJiri Olsa 				struct parse_events_error *err,
1228e637d177SHe Kuang 				struct list_head *head_config)
1229e637d177SHe Kuang {
1230e637d177SHe Kuang 	if (head_config) {
1231e637d177SHe Kuang 		struct perf_event_attr attr;
1232e637d177SHe Kuang 
1233272ed29aSJiri Olsa 		if (config_attr(&attr, head_config, err,
1234e637d177SHe Kuang 				config_term_tracepoint))
1235e637d177SHe Kuang 			return -EINVAL;
1236e637d177SHe Kuang 	}
1237e637d177SHe Kuang 
1238e637d177SHe Kuang 	if (strpbrk(sys, "*?"))
1239e637d177SHe Kuang 		return add_tracepoint_multi_sys(list, idx, sys, event,
1240272ed29aSJiri Olsa 						err, head_config);
1241e637d177SHe Kuang 	else
1242e637d177SHe Kuang 		return add_tracepoint_event(list, idx, sys, event,
1243272ed29aSJiri Olsa 					    err, head_config);
1244e637d177SHe Kuang }
1245e637d177SHe Kuang 
12465d9cdc11SArnaldo Carvalho de Melo int parse_events_add_numeric(struct parse_events_state *parse_state,
124787d650beSJiri Olsa 			     struct list_head *list,
1248b527bab5SRobert Richter 			     u32 type, u64 config,
12498f707d84SJiri Olsa 			     struct list_head *head_config)
125074d5b588SJaswinder Singh Rajput {
125189812fc8SJiri Olsa 	struct perf_event_attr attr;
1252930a2e29SJiri Olsa 	LIST_HEAD(config_terms);
125374d5b588SJaswinder Singh Rajput 
125489812fc8SJiri Olsa 	memset(&attr, 0, sizeof(attr));
125589812fc8SJiri Olsa 	attr.type = type;
125689812fc8SJiri Olsa 	attr.config = config;
12578f707d84SJiri Olsa 
1258930a2e29SJiri Olsa 	if (head_config) {
12595d9cdc11SArnaldo Carvalho de Melo 		if (config_attr(&attr, head_config, parse_state->error,
12600b8891a8SHe Kuang 				config_term_common))
12618f707d84SJiri Olsa 			return -EINVAL;
12628f707d84SJiri Olsa 
1263930a2e29SJiri Olsa 		if (get_config_terms(head_config, &config_terms))
1264930a2e29SJiri Olsa 			return -ENOMEM;
1265930a2e29SJiri Olsa 	}
1266930a2e29SJiri Olsa 
12675d9cdc11SArnaldo Carvalho de Melo 	return add_event(list, &parse_state->idx, &attr,
126810bf358aSWang Nan 			 get_config_name(head_config), &config_terms);
1269b908debdSIngo Molnar }
1270b908debdSIngo Molnar 
1271f0fbb114SAndi Kleen int parse_events_add_tool(struct parse_events_state *parse_state,
1272f0fbb114SAndi Kleen 			  struct list_head *list,
1273f0fbb114SAndi Kleen 			  enum perf_tool_event tool_event)
1274f0fbb114SAndi Kleen {
1275f0fbb114SAndi Kleen 	return add_event_tool(list, &parse_state->idx, tool_event);
1276f0fbb114SAndi Kleen }
1277f0fbb114SAndi Kleen 
1278064b4e82SJin Yao static bool config_term_percore(struct list_head *config_terms)
1279064b4e82SJin Yao {
1280064b4e82SJin Yao 	struct perf_evsel_config_term *term;
1281064b4e82SJin Yao 
1282064b4e82SJin Yao 	list_for_each_entry(term, config_terms, list) {
1283064b4e82SJin Yao 		if (term->type == PERF_EVSEL__CONFIG_TERM_PERCORE)
1284064b4e82SJin Yao 			return term->val.percore;
1285064b4e82SJin Yao 	}
1286064b4e82SJin Yao 
1287064b4e82SJin Yao 	return false;
1288064b4e82SJin Yao }
1289064b4e82SJin Yao 
1290c199c11dSAgustin Vega-Frias int parse_events_add_pmu(struct parse_events_state *parse_state,
129136adec85SJiri Olsa 			 struct list_head *list, char *name,
12923cdc5c2cSKan Liang 			 struct list_head *head_config,
12933cdc5c2cSKan Liang 			 bool auto_merge_stats,
12943cdc5c2cSKan Liang 			 bool use_alias)
12955f537a26SJiri Olsa {
12965f537a26SJiri Olsa 	struct perf_event_attr attr;
129746441bdcSMatt Fleming 	struct perf_pmu_info info;
12985f537a26SJiri Olsa 	struct perf_pmu *pmu;
1299410136f5SStephane Eranian 	struct perf_evsel *evsel;
1300333b5665SAndi Kleen 	struct parse_events_error *err = parse_state->error;
13013cdc5c2cSKan Liang 	bool use_uncore_alias;
1302930a2e29SJiri Olsa 	LIST_HEAD(config_terms);
13035f537a26SJiri Olsa 
13045f537a26SJiri Olsa 	pmu = perf_pmu__find(name);
1305333b5665SAndi Kleen 	if (!pmu) {
1306333b5665SAndi Kleen 		if (asprintf(&err->str,
1307333b5665SAndi Kleen 				"Cannot find PMU `%s'. Missing kernel support?",
1308333b5665SAndi Kleen 				name) < 0)
1309333b5665SAndi Kleen 			err->str = NULL;
13105f537a26SJiri Olsa 		return -EINVAL;
1311333b5665SAndi Kleen 	}
13125f537a26SJiri Olsa 
1313dc0a6202SAdrian Hunter 	if (pmu->default_config) {
1314dc0a6202SAdrian Hunter 		memcpy(&attr, pmu->default_config,
1315dc0a6202SAdrian Hunter 		       sizeof(struct perf_event_attr));
1316dc0a6202SAdrian Hunter 	} else {
13175f537a26SJiri Olsa 		memset(&attr, 0, sizeof(attr));
1318dc0a6202SAdrian Hunter 	}
13195f537a26SJiri Olsa 
13203cdc5c2cSKan Liang 	use_uncore_alias = (pmu->is_uncore && use_alias);
13213cdc5c2cSKan Liang 
1322ad962273SAdrian Hunter 	if (!head_config) {
1323ad962273SAdrian Hunter 		attr.type = pmu->type;
1324f0fbb114SAndi Kleen 		evsel = __add_event(list, &parse_state->idx, &attr, NULL, pmu, NULL,
1325f0fbb114SAndi Kleen 				    auto_merge_stats, NULL);
13268c5421c0SAgustin Vega-Frias 		if (evsel) {
13278c5421c0SAgustin Vega-Frias 			evsel->pmu_name = name;
13283cdc5c2cSKan Liang 			evsel->use_uncore_alias = use_uncore_alias;
13298c5421c0SAgustin Vega-Frias 			return 0;
13308c5421c0SAgustin Vega-Frias 		} else {
13318c5421c0SAgustin Vega-Frias 			return -ENOMEM;
13328c5421c0SAgustin Vega-Frias 		}
1333ad962273SAdrian Hunter 	}
1334ad962273SAdrian Hunter 
133546441bdcSMatt Fleming 	if (perf_pmu__check_alias(pmu, head_config, &info))
1336a6146d50SZheng Yan 		return -EINVAL;
1337a6146d50SZheng Yan 
13385f537a26SJiri Olsa 	/*
13395f537a26SJiri Olsa 	 * Configure hardcoded terms first, no need to check
13405f537a26SJiri Olsa 	 * return value when called with fail == 0 ;)
13415f537a26SJiri Olsa 	 */
13425d9cdc11SArnaldo Carvalho de Melo 	if (config_attr(&attr, head_config, parse_state->error, config_term_pmu))
1343c056ba6aSJiri Olsa 		return -EINVAL;
13445f537a26SJiri Olsa 
1345930a2e29SJiri Olsa 	if (get_config_terms(head_config, &config_terms))
1346930a2e29SJiri Olsa 		return -ENOMEM;
1347930a2e29SJiri Olsa 
13485d9cdc11SArnaldo Carvalho de Melo 	if (perf_pmu__config(pmu, &attr, head_config, parse_state->error))
13495f537a26SJiri Olsa 		return -EINVAL;
13505f537a26SJiri Olsa 
13515d9cdc11SArnaldo Carvalho de Melo 	evsel = __add_event(list, &parse_state->idx, &attr,
135266ec1191SMark Rutland 			    get_config_name(head_config), pmu,
1353f0fbb114SAndi Kleen 			    &config_terms, auto_merge_stats, NULL);
1354410136f5SStephane Eranian 	if (evsel) {
135546441bdcSMatt Fleming 		evsel->unit = info.unit;
135646441bdcSMatt Fleming 		evsel->scale = info.scale;
1357044330c1SMatt Fleming 		evsel->per_pkg = info.per_pkg;
13581d9e446bSJiri Olsa 		evsel->snapshot = info.snapshot;
135937932c18SAndi Kleen 		evsel->metric_expr = info.metric_expr;
136096284814SAndi Kleen 		evsel->metric_name = info.metric_name;
13618c5421c0SAgustin Vega-Frias 		evsel->pmu_name = name;
13623cdc5c2cSKan Liang 		evsel->use_uncore_alias = use_uncore_alias;
1363064b4e82SJin Yao 		evsel->percore = config_term_percore(&evsel->config_terms);
1364410136f5SStephane Eranian 	}
1365410136f5SStephane Eranian 
1366410136f5SStephane Eranian 	return evsel ? 0 : -ENOMEM;
13675f537a26SJiri Olsa }
13685f537a26SJiri Olsa 
13695d9cdc11SArnaldo Carvalho de Melo int parse_events_multi_pmu_add(struct parse_events_state *parse_state,
13702073ad33SAndi Kleen 			       char *str, struct list_head **listp)
13712073ad33SAndi Kleen {
13722073ad33SAndi Kleen 	struct list_head *head;
13732073ad33SAndi Kleen 	struct parse_events_term *term;
13742073ad33SAndi Kleen 	struct list_head *list;
13752073ad33SAndi Kleen 	struct perf_pmu *pmu = NULL;
13762073ad33SAndi Kleen 	int ok = 0;
13772073ad33SAndi Kleen 
13782073ad33SAndi Kleen 	*listp = NULL;
13792073ad33SAndi Kleen 	/* Add it for all PMUs that support the alias */
13802073ad33SAndi Kleen 	list = malloc(sizeof(struct list_head));
13812073ad33SAndi Kleen 	if (!list)
13822073ad33SAndi Kleen 		return -1;
13832073ad33SAndi Kleen 	INIT_LIST_HEAD(list);
13842073ad33SAndi Kleen 	while ((pmu = perf_pmu__scan(pmu)) != NULL) {
13852073ad33SAndi Kleen 		struct perf_pmu_alias *alias;
13862073ad33SAndi Kleen 
13872073ad33SAndi Kleen 		list_for_each_entry(alias, &pmu->aliases, list) {
13882073ad33SAndi Kleen 			if (!strcasecmp(alias->name, str)) {
13892073ad33SAndi Kleen 				head = malloc(sizeof(struct list_head));
13902073ad33SAndi Kleen 				if (!head)
13912073ad33SAndi Kleen 					return -1;
13922073ad33SAndi Kleen 				INIT_LIST_HEAD(head);
13932073ad33SAndi Kleen 				if (parse_events_term__num(&term, PARSE_EVENTS__TERM_TYPE_USER,
13942073ad33SAndi Kleen 							   str, 1, false, &str, NULL) < 0)
13952073ad33SAndi Kleen 					return -1;
13962073ad33SAndi Kleen 				list_add_tail(&term->list, head);
13972073ad33SAndi Kleen 
1398c199c11dSAgustin Vega-Frias 				if (!parse_events_add_pmu(parse_state, list,
13993cdc5c2cSKan Liang 							  pmu->name, head,
14003cdc5c2cSKan Liang 							  true, true)) {
14012073ad33SAndi Kleen 					pr_debug("%s -> %s/%s/\n", str,
14022073ad33SAndi Kleen 						 pmu->name, alias->str);
14032073ad33SAndi Kleen 					ok++;
14042073ad33SAndi Kleen 				}
14052073ad33SAndi Kleen 
14062073ad33SAndi Kleen 				parse_events_terms__delete(head);
14072073ad33SAndi Kleen 			}
14082073ad33SAndi Kleen 		}
14092073ad33SAndi Kleen 	}
14102073ad33SAndi Kleen 	if (!ok)
14112073ad33SAndi Kleen 		return -1;
14122073ad33SAndi Kleen 	*listp = list;
14132073ad33SAndi Kleen 	return 0;
14142073ad33SAndi Kleen }
14152073ad33SAndi Kleen 
14166a4bb04cSJiri Olsa int parse_events__modifier_group(struct list_head *list,
14176a4bb04cSJiri Olsa 				 char *event_mod)
141889efb029SJiri Olsa {
14196a4bb04cSJiri Olsa 	return parse_events__modifier_event(list, event_mod, true);
14206a4bb04cSJiri Olsa }
14216a4bb04cSJiri Olsa 
14223cdc5c2cSKan Liang /*
14233cdc5c2cSKan Liang  * Check if the two uncore PMUs are from the same uncore block
14243cdc5c2cSKan Liang  * The format of the uncore PMU name is uncore_#blockname_#pmuidx
14253cdc5c2cSKan Liang  */
14263cdc5c2cSKan Liang static bool is_same_uncore_block(const char *pmu_name_a, const char *pmu_name_b)
14273cdc5c2cSKan Liang {
14283cdc5c2cSKan Liang 	char *end_a, *end_b;
14293cdc5c2cSKan Liang 
14303cdc5c2cSKan Liang 	end_a = strrchr(pmu_name_a, '_');
14313cdc5c2cSKan Liang 	end_b = strrchr(pmu_name_b, '_');
14323cdc5c2cSKan Liang 
14333cdc5c2cSKan Liang 	if (!end_a || !end_b)
14343cdc5c2cSKan Liang 		return false;
14353cdc5c2cSKan Liang 
14363cdc5c2cSKan Liang 	if ((end_a - pmu_name_a) != (end_b - pmu_name_b))
14373cdc5c2cSKan Liang 		return false;
14383cdc5c2cSKan Liang 
14393cdc5c2cSKan Liang 	return (strncmp(pmu_name_a, pmu_name_b, end_a - pmu_name_a) == 0);
14403cdc5c2cSKan Liang }
14413cdc5c2cSKan Liang 
14423cdc5c2cSKan Liang static int
14433cdc5c2cSKan Liang parse_events__set_leader_for_uncore_aliase(char *name, struct list_head *list,
14443cdc5c2cSKan Liang 					   struct parse_events_state *parse_state)
14453cdc5c2cSKan Liang {
14463cdc5c2cSKan Liang 	struct perf_evsel *evsel, *leader;
14473cdc5c2cSKan Liang 	uintptr_t *leaders;
14483cdc5c2cSKan Liang 	bool is_leader = true;
14493cdc5c2cSKan Liang 	int i, nr_pmu = 0, total_members, ret = 0;
14503cdc5c2cSKan Liang 
14513cdc5c2cSKan Liang 	leader = list_first_entry(list, struct perf_evsel, node);
14523cdc5c2cSKan Liang 	evsel = list_last_entry(list, struct perf_evsel, node);
14533cdc5c2cSKan Liang 	total_members = evsel->idx - leader->idx + 1;
14543cdc5c2cSKan Liang 
14553cdc5c2cSKan Liang 	leaders = calloc(total_members, sizeof(uintptr_t));
14563cdc5c2cSKan Liang 	if (WARN_ON(!leaders))
14573cdc5c2cSKan Liang 		return 0;
14583cdc5c2cSKan Liang 
14593cdc5c2cSKan Liang 	/*
14603cdc5c2cSKan Liang 	 * Going through the whole group and doing sanity check.
14613cdc5c2cSKan Liang 	 * All members must use alias, and be from the same uncore block.
14623cdc5c2cSKan Liang 	 * Also, storing the leader events in an array.
14633cdc5c2cSKan Liang 	 */
14643cdc5c2cSKan Liang 	__evlist__for_each_entry(list, evsel) {
14653cdc5c2cSKan Liang 
14663cdc5c2cSKan Liang 		/* Only split the uncore group which members use alias */
14673cdc5c2cSKan Liang 		if (!evsel->use_uncore_alias)
14683cdc5c2cSKan Liang 			goto out;
14693cdc5c2cSKan Liang 
14703cdc5c2cSKan Liang 		/* The events must be from the same uncore block */
14713cdc5c2cSKan Liang 		if (!is_same_uncore_block(leader->pmu_name, evsel->pmu_name))
14723cdc5c2cSKan Liang 			goto out;
14733cdc5c2cSKan Liang 
14743cdc5c2cSKan Liang 		if (!is_leader)
14753cdc5c2cSKan Liang 			continue;
14763cdc5c2cSKan Liang 		/*
14773cdc5c2cSKan Liang 		 * If the event's PMU name starts to repeat, it must be a new
14783cdc5c2cSKan Liang 		 * event. That can be used to distinguish the leader from
14793cdc5c2cSKan Liang 		 * other members, even they have the same event name.
14803cdc5c2cSKan Liang 		 */
14813cdc5c2cSKan Liang 		if ((leader != evsel) && (leader->pmu_name == evsel->pmu_name)) {
14823cdc5c2cSKan Liang 			is_leader = false;
14833cdc5c2cSKan Liang 			continue;
14843cdc5c2cSKan Liang 		}
14853cdc5c2cSKan Liang 		/* The name is always alias name */
14863cdc5c2cSKan Liang 		WARN_ON(strcmp(leader->name, evsel->name));
14873cdc5c2cSKan Liang 
14883cdc5c2cSKan Liang 		/* Store the leader event for each PMU */
14893cdc5c2cSKan Liang 		leaders[nr_pmu++] = (uintptr_t) evsel;
14903cdc5c2cSKan Liang 	}
14913cdc5c2cSKan Liang 
14923cdc5c2cSKan Liang 	/* only one event alias */
14933cdc5c2cSKan Liang 	if (nr_pmu == total_members) {
14943cdc5c2cSKan Liang 		parse_state->nr_groups--;
14953cdc5c2cSKan Liang 		goto handled;
14963cdc5c2cSKan Liang 	}
14973cdc5c2cSKan Liang 
14983cdc5c2cSKan Liang 	/*
14993cdc5c2cSKan Liang 	 * An uncore event alias is a joint name which means the same event
15003cdc5c2cSKan Liang 	 * runs on all PMUs of a block.
15013cdc5c2cSKan Liang 	 * Perf doesn't support mixed events from different PMUs in the same
15023cdc5c2cSKan Liang 	 * group. The big group has to be split into multiple small groups
15033cdc5c2cSKan Liang 	 * which only include the events from the same PMU.
15043cdc5c2cSKan Liang 	 *
15053cdc5c2cSKan Liang 	 * Here the uncore event aliases must be from the same uncore block.
15063cdc5c2cSKan Liang 	 * The number of PMUs must be same for each alias. The number of new
15073cdc5c2cSKan Liang 	 * small groups equals to the number of PMUs.
15083cdc5c2cSKan Liang 	 * Setting the leader event for corresponding members in each group.
15093cdc5c2cSKan Liang 	 */
15103cdc5c2cSKan Liang 	i = 0;
15113cdc5c2cSKan Liang 	__evlist__for_each_entry(list, evsel) {
15123cdc5c2cSKan Liang 		if (i >= nr_pmu)
15133cdc5c2cSKan Liang 			i = 0;
15143cdc5c2cSKan Liang 		evsel->leader = (struct perf_evsel *) leaders[i++];
15153cdc5c2cSKan Liang 	}
15163cdc5c2cSKan Liang 
15173cdc5c2cSKan Liang 	/* The number of members and group name are same for each group */
15183cdc5c2cSKan Liang 	for (i = 0; i < nr_pmu; i++) {
15193cdc5c2cSKan Liang 		evsel = (struct perf_evsel *) leaders[i];
15203cdc5c2cSKan Liang 		evsel->nr_members = total_members / nr_pmu;
15213cdc5c2cSKan Liang 		evsel->group_name = name ? strdup(name) : NULL;
15223cdc5c2cSKan Liang 	}
15233cdc5c2cSKan Liang 
15243cdc5c2cSKan Liang 	/* Take the new small groups into account */
15253cdc5c2cSKan Liang 	parse_state->nr_groups += nr_pmu - 1;
15263cdc5c2cSKan Liang 
15273cdc5c2cSKan Liang handled:
15283cdc5c2cSKan Liang 	ret = 1;
15293cdc5c2cSKan Liang out:
15303cdc5c2cSKan Liang 	free(leaders);
15313cdc5c2cSKan Liang 	return ret;
15323cdc5c2cSKan Liang }
15333cdc5c2cSKan Liang 
15343cdc5c2cSKan Liang void parse_events__set_leader(char *name, struct list_head *list,
15353cdc5c2cSKan Liang 			      struct parse_events_state *parse_state)
15366a4bb04cSJiri Olsa {
15376a4bb04cSJiri Olsa 	struct perf_evsel *leader;
15386a4bb04cSJiri Olsa 
1539854f7363SWang Nan 	if (list_empty(list)) {
1540854f7363SWang Nan 		WARN_ONCE(true, "WARNING: failed to set leader: empty list");
1541854f7363SWang Nan 		return;
1542854f7363SWang Nan 	}
1543854f7363SWang Nan 
15443cdc5c2cSKan Liang 	if (parse_events__set_leader_for_uncore_aliase(name, list, parse_state))
15453cdc5c2cSKan Liang 		return;
15463cdc5c2cSKan Liang 
154763dab225SArnaldo Carvalho de Melo 	__perf_evlist__set_leader(list);
154863dab225SArnaldo Carvalho de Melo 	leader = list_entry(list->next, struct perf_evsel, node);
15496a4bb04cSJiri Olsa 	leader->group_name = name ? strdup(name) : NULL;
155089efb029SJiri Olsa }
155189efb029SJiri Olsa 
1552c5cd8ac0SDavid Ahern /* list_event is assumed to point to malloc'ed memory */
15535d7be90eSJiri Olsa void parse_events_update_lists(struct list_head *list_event,
15545d7be90eSJiri Olsa 			       struct list_head *list_all)
15555d7be90eSJiri Olsa {
15565d7be90eSJiri Olsa 	/*
15575d7be90eSJiri Olsa 	 * Called for single event definition. Update the
155889efb029SJiri Olsa 	 * 'all event' list, and reinit the 'single event'
15595d7be90eSJiri Olsa 	 * list, for next event definition.
15605d7be90eSJiri Olsa 	 */
15615d7be90eSJiri Olsa 	list_splice_tail(list_event, list_all);
1562b847cbdcSJiri Olsa 	free(list_event);
15635d7be90eSJiri Olsa }
15645d7be90eSJiri Olsa 
1565f5b1135bSJiri Olsa struct event_modifier {
1566f5b1135bSJiri Olsa 	int eu;
1567f5b1135bSJiri Olsa 	int ek;
1568f5b1135bSJiri Olsa 	int eh;
1569f5b1135bSJiri Olsa 	int eH;
1570f5b1135bSJiri Olsa 	int eG;
1571a1e12da4SJiri Olsa 	int eI;
1572f5b1135bSJiri Olsa 	int precise;
15737f94af7aSJiri Olsa 	int precise_max;
1574f5b1135bSJiri Olsa 	int exclude_GH;
15753c176311SJiri Olsa 	int sample_read;
1576e9a7c414SMichael Ellerman 	int pinned;
15775a5dfe4bSAndi Kleen 	int weak;
1578f5b1135bSJiri Olsa };
157961c45981SPaul Mackerras 
1580f5b1135bSJiri Olsa static int get_event_modifier(struct event_modifier *mod, char *str,
1581f5b1135bSJiri Olsa 			       struct perf_evsel *evsel)
1582f5b1135bSJiri Olsa {
1583f5b1135bSJiri Olsa 	int eu = evsel ? evsel->attr.exclude_user : 0;
1584f5b1135bSJiri Olsa 	int ek = evsel ? evsel->attr.exclude_kernel : 0;
1585f5b1135bSJiri Olsa 	int eh = evsel ? evsel->attr.exclude_hv : 0;
1586f5b1135bSJiri Olsa 	int eH = evsel ? evsel->attr.exclude_host : 0;
1587f5b1135bSJiri Olsa 	int eG = evsel ? evsel->attr.exclude_guest : 0;
1588a1e12da4SJiri Olsa 	int eI = evsel ? evsel->attr.exclude_idle : 0;
1589f5b1135bSJiri Olsa 	int precise = evsel ? evsel->attr.precise_ip : 0;
15907f94af7aSJiri Olsa 	int precise_max = 0;
15913c176311SJiri Olsa 	int sample_read = 0;
1592e9a7c414SMichael Ellerman 	int pinned = evsel ? evsel->attr.pinned : 0;
1593f5b1135bSJiri Olsa 
1594f5b1135bSJiri Olsa 	int exclude = eu | ek | eh;
1595f5b1135bSJiri Olsa 	int exclude_GH = evsel ? evsel->exclude_GH : 0;
15965a5dfe4bSAndi Kleen 	int weak = 0;
1597f5b1135bSJiri Olsa 
1598f5b1135bSJiri Olsa 	memset(mod, 0, sizeof(*mod));
1599ceb53fbfSIngo Molnar 
160061c45981SPaul Mackerras 	while (*str) {
1601ab608344SPeter Zijlstra 		if (*str == 'u') {
1602ab608344SPeter Zijlstra 			if (!exclude)
1603ab608344SPeter Zijlstra 				exclude = eu = ek = eh = 1;
160461c45981SPaul Mackerras 			eu = 0;
1605ab608344SPeter Zijlstra 		} else if (*str == 'k') {
1606ab608344SPeter Zijlstra 			if (!exclude)
1607ab608344SPeter Zijlstra 				exclude = eu = ek = eh = 1;
160861c45981SPaul Mackerras 			ek = 0;
1609ab608344SPeter Zijlstra 		} else if (*str == 'h') {
1610ab608344SPeter Zijlstra 			if (!exclude)
1611ab608344SPeter Zijlstra 				exclude = eu = ek = eh = 1;
161261c45981SPaul Mackerras 			eh = 0;
161399320cc8SJoerg Roedel 		} else if (*str == 'G') {
161499320cc8SJoerg Roedel 			if (!exclude_GH)
161599320cc8SJoerg Roedel 				exclude_GH = eG = eH = 1;
161699320cc8SJoerg Roedel 			eG = 0;
161799320cc8SJoerg Roedel 		} else if (*str == 'H') {
161899320cc8SJoerg Roedel 			if (!exclude_GH)
161999320cc8SJoerg Roedel 				exclude_GH = eG = eH = 1;
162099320cc8SJoerg Roedel 			eH = 0;
1621a1e12da4SJiri Olsa 		} else if (*str == 'I') {
1622a1e12da4SJiri Olsa 			eI = 1;
1623ab608344SPeter Zijlstra 		} else if (*str == 'p') {
1624ab608344SPeter Zijlstra 			precise++;
16251342798cSDavid Ahern 			/* use of precise requires exclude_guest */
16261342798cSDavid Ahern 			if (!exclude_GH)
16271342798cSDavid Ahern 				eG = 1;
16287f94af7aSJiri Olsa 		} else if (*str == 'P') {
16297f94af7aSJiri Olsa 			precise_max = 1;
16303c176311SJiri Olsa 		} else if (*str == 'S') {
16313c176311SJiri Olsa 			sample_read = 1;
1632e9a7c414SMichael Ellerman 		} else if (*str == 'D') {
1633e9a7c414SMichael Ellerman 			pinned = 1;
16345a5dfe4bSAndi Kleen 		} else if (*str == 'W') {
16355a5dfe4bSAndi Kleen 			weak = 1;
1636ab608344SPeter Zijlstra 		} else
163761c45981SPaul Mackerras 			break;
1638ab608344SPeter Zijlstra 
163961c45981SPaul Mackerras 		++str;
164061c45981SPaul Mackerras 	}
164174d5b588SJaswinder Singh Rajput 
164286470930SIngo Molnar 	/*
164389812fc8SJiri Olsa 	 * precise ip:
164489812fc8SJiri Olsa 	 *
164589812fc8SJiri Olsa 	 *  0 - SAMPLE_IP can have arbitrary skid
164689812fc8SJiri Olsa 	 *  1 - SAMPLE_IP must have constant skid
164789812fc8SJiri Olsa 	 *  2 - SAMPLE_IP requested to have 0 skid
164889812fc8SJiri Olsa 	 *  3 - SAMPLE_IP must have 0 skid
164989812fc8SJiri Olsa 	 *
165089812fc8SJiri Olsa 	 *  See also PERF_RECORD_MISC_EXACT_IP
165186470930SIngo Molnar 	 */
165289812fc8SJiri Olsa 	if (precise > 3)
165389812fc8SJiri Olsa 		return -EINVAL;
165486470930SIngo Molnar 
1655f5b1135bSJiri Olsa 	mod->eu = eu;
1656f5b1135bSJiri Olsa 	mod->ek = ek;
1657f5b1135bSJiri Olsa 	mod->eh = eh;
1658f5b1135bSJiri Olsa 	mod->eH = eH;
1659f5b1135bSJiri Olsa 	mod->eG = eG;
1660a1e12da4SJiri Olsa 	mod->eI = eI;
1661f5b1135bSJiri Olsa 	mod->precise = precise;
16627f94af7aSJiri Olsa 	mod->precise_max = precise_max;
1663f5b1135bSJiri Olsa 	mod->exclude_GH = exclude_GH;
16643c176311SJiri Olsa 	mod->sample_read = sample_read;
1665e9a7c414SMichael Ellerman 	mod->pinned = pinned;
16665a5dfe4bSAndi Kleen 	mod->weak = weak;
1667e9a7c414SMichael Ellerman 
1668f5b1135bSJiri Olsa 	return 0;
1669f5b1135bSJiri Olsa }
1670f5b1135bSJiri Olsa 
1671534123f4SJiri Olsa /*
1672534123f4SJiri Olsa  * Basic modifier sanity check to validate it contains only one
1673534123f4SJiri Olsa  * instance of any modifier (apart from 'p') present.
1674534123f4SJiri Olsa  */
1675534123f4SJiri Olsa static int check_modifier(char *str)
1676534123f4SJiri Olsa {
1677534123f4SJiri Olsa 	char *p = str;
1678534123f4SJiri Olsa 
1679534123f4SJiri Olsa 	/* The sizeof includes 0 byte as well. */
16805a5dfe4bSAndi Kleen 	if (strlen(str) > (sizeof("ukhGHpppPSDIW") - 1))
1681534123f4SJiri Olsa 		return -1;
1682534123f4SJiri Olsa 
1683534123f4SJiri Olsa 	while (*p) {
1684534123f4SJiri Olsa 		if (*p != 'p' && strchr(p + 1, *p))
1685534123f4SJiri Olsa 			return -1;
1686534123f4SJiri Olsa 		p++;
1687534123f4SJiri Olsa 	}
1688534123f4SJiri Olsa 
1689534123f4SJiri Olsa 	return 0;
1690534123f4SJiri Olsa }
1691534123f4SJiri Olsa 
1692f5b1135bSJiri Olsa int parse_events__modifier_event(struct list_head *list, char *str, bool add)
1693f5b1135bSJiri Olsa {
1694f5b1135bSJiri Olsa 	struct perf_evsel *evsel;
1695f5b1135bSJiri Olsa 	struct event_modifier mod;
1696f5b1135bSJiri Olsa 
1697f5b1135bSJiri Olsa 	if (str == NULL)
1698f5b1135bSJiri Olsa 		return 0;
1699f5b1135bSJiri Olsa 
1700534123f4SJiri Olsa 	if (check_modifier(str))
1701534123f4SJiri Olsa 		return -EINVAL;
1702534123f4SJiri Olsa 
1703f5b1135bSJiri Olsa 	if (!add && get_event_modifier(&mod, str, NULL))
1704f5b1135bSJiri Olsa 		return -EINVAL;
1705f5b1135bSJiri Olsa 
1706e5cadb93SArnaldo Carvalho de Melo 	__evlist__for_each_entry(list, evsel) {
1707f5b1135bSJiri Olsa 		if (add && get_event_modifier(&mod, str, evsel))
1708f5b1135bSJiri Olsa 			return -EINVAL;
1709f5b1135bSJiri Olsa 
1710f5b1135bSJiri Olsa 		evsel->attr.exclude_user   = mod.eu;
1711f5b1135bSJiri Olsa 		evsel->attr.exclude_kernel = mod.ek;
1712f5b1135bSJiri Olsa 		evsel->attr.exclude_hv     = mod.eh;
1713f5b1135bSJiri Olsa 		evsel->attr.precise_ip     = mod.precise;
1714f5b1135bSJiri Olsa 		evsel->attr.exclude_host   = mod.eH;
1715f5b1135bSJiri Olsa 		evsel->attr.exclude_guest  = mod.eG;
1716a1e12da4SJiri Olsa 		evsel->attr.exclude_idle   = mod.eI;
1717f5b1135bSJiri Olsa 		evsel->exclude_GH          = mod.exclude_GH;
17183c176311SJiri Olsa 		evsel->sample_read         = mod.sample_read;
17197f94af7aSJiri Olsa 		evsel->precise_max         = mod.precise_max;
17205a5dfe4bSAndi Kleen 		evsel->weak_group	   = mod.weak;
1721e9a7c414SMichael Ellerman 
1722e9a7c414SMichael Ellerman 		if (perf_evsel__is_group_leader(evsel))
1723e9a7c414SMichael Ellerman 			evsel->attr.pinned = mod.pinned;
1724ceb53fbfSIngo Molnar 	}
172586470930SIngo Molnar 
172689812fc8SJiri Olsa 	return 0;
172786470930SIngo Molnar }
172886470930SIngo Molnar 
1729ac2ba9f3SRobert Richter int parse_events_name(struct list_head *list, char *name)
1730ac2ba9f3SRobert Richter {
1731ac2ba9f3SRobert Richter 	struct perf_evsel *evsel;
1732ac2ba9f3SRobert Richter 
1733e5cadb93SArnaldo Carvalho de Melo 	__evlist__for_each_entry(list, evsel) {
1734ac2ba9f3SRobert Richter 		if (!evsel->name)
1735ac2ba9f3SRobert Richter 			evsel->name = strdup(name);
1736ac2ba9f3SRobert Richter 	}
1737ac2ba9f3SRobert Richter 
1738ac2ba9f3SRobert Richter 	return 0;
1739ac2ba9f3SRobert Richter }
1740ac2ba9f3SRobert Richter 
1741dcb4e102SKan Liang static int
1742dcb4e102SKan Liang comp_pmu(const void *p1, const void *p2)
1743dcb4e102SKan Liang {
1744dcb4e102SKan Liang 	struct perf_pmu_event_symbol *pmu1 = (struct perf_pmu_event_symbol *) p1;
1745dcb4e102SKan Liang 	struct perf_pmu_event_symbol *pmu2 = (struct perf_pmu_event_symbol *) p2;
1746dcb4e102SKan Liang 
1747e312bcf1SAndi Kleen 	return strcasecmp(pmu1->symbol, pmu2->symbol);
1748dcb4e102SKan Liang }
1749dcb4e102SKan Liang 
1750dcb4e102SKan Liang static void perf_pmu__parse_cleanup(void)
1751dcb4e102SKan Liang {
1752dcb4e102SKan Liang 	if (perf_pmu_events_list_num > 0) {
1753dcb4e102SKan Liang 		struct perf_pmu_event_symbol *p;
1754dcb4e102SKan Liang 		int i;
1755dcb4e102SKan Liang 
1756dcb4e102SKan Liang 		for (i = 0; i < perf_pmu_events_list_num; i++) {
1757dcb4e102SKan Liang 			p = perf_pmu_events_list + i;
1758360e071bSTaeung Song 			zfree(&p->symbol);
1759dcb4e102SKan Liang 		}
1760506fde11STaeung Song 		zfree(&perf_pmu_events_list);
1761dcb4e102SKan Liang 		perf_pmu_events_list_num = 0;
1762dcb4e102SKan Liang 	}
1763dcb4e102SKan Liang }
1764dcb4e102SKan Liang 
1765dcb4e102SKan Liang #define SET_SYMBOL(str, stype)		\
1766dcb4e102SKan Liang do {					\
1767dcb4e102SKan Liang 	p->symbol = str;		\
1768dcb4e102SKan Liang 	if (!p->symbol)			\
1769dcb4e102SKan Liang 		goto err;		\
1770dcb4e102SKan Liang 	p->type = stype;		\
1771dcb4e102SKan Liang } while (0)
1772dcb4e102SKan Liang 
1773dcb4e102SKan Liang /*
1774dcb4e102SKan Liang  * Read the pmu events list from sysfs
1775dcb4e102SKan Liang  * Save it into perf_pmu_events_list
1776dcb4e102SKan Liang  */
1777dcb4e102SKan Liang static void perf_pmu__parse_init(void)
1778dcb4e102SKan Liang {
1779dcb4e102SKan Liang 
1780dcb4e102SKan Liang 	struct perf_pmu *pmu = NULL;
1781dcb4e102SKan Liang 	struct perf_pmu_alias *alias;
1782dcb4e102SKan Liang 	int len = 0;
1783dcb4e102SKan Liang 
1784231bb2aaSAndi Kleen 	pmu = NULL;
1785231bb2aaSAndi Kleen 	while ((pmu = perf_pmu__scan(pmu)) != NULL) {
1786dcb4e102SKan Liang 		list_for_each_entry(alias, &pmu->aliases, list) {
1787dcb4e102SKan Liang 			if (strchr(alias->name, '-'))
1788dcb4e102SKan Liang 				len++;
1789dcb4e102SKan Liang 			len++;
1790dcb4e102SKan Liang 		}
1791231bb2aaSAndi Kleen 	}
1792231bb2aaSAndi Kleen 
1793231bb2aaSAndi Kleen 	if (len == 0) {
1794231bb2aaSAndi Kleen 		perf_pmu_events_list_num = -1;
1795231bb2aaSAndi Kleen 		return;
1796231bb2aaSAndi Kleen 	}
1797dcb4e102SKan Liang 	perf_pmu_events_list = malloc(sizeof(struct perf_pmu_event_symbol) * len);
1798dcb4e102SKan Liang 	if (!perf_pmu_events_list)
1799dcb4e102SKan Liang 		return;
1800dcb4e102SKan Liang 	perf_pmu_events_list_num = len;
1801dcb4e102SKan Liang 
1802dcb4e102SKan Liang 	len = 0;
1803231bb2aaSAndi Kleen 	pmu = NULL;
1804231bb2aaSAndi Kleen 	while ((pmu = perf_pmu__scan(pmu)) != NULL) {
1805dcb4e102SKan Liang 		list_for_each_entry(alias, &pmu->aliases, list) {
1806dcb4e102SKan Liang 			struct perf_pmu_event_symbol *p = perf_pmu_events_list + len;
1807dcb4e102SKan Liang 			char *tmp = strchr(alias->name, '-');
1808dcb4e102SKan Liang 
1809dcb4e102SKan Liang 			if (tmp != NULL) {
1810dcb4e102SKan Liang 				SET_SYMBOL(strndup(alias->name, tmp - alias->name),
1811dcb4e102SKan Liang 						PMU_EVENT_SYMBOL_PREFIX);
1812dcb4e102SKan Liang 				p++;
1813dcb4e102SKan Liang 				SET_SYMBOL(strdup(++tmp), PMU_EVENT_SYMBOL_SUFFIX);
1814dcb4e102SKan Liang 				len += 2;
1815dcb4e102SKan Liang 			} else {
1816dcb4e102SKan Liang 				SET_SYMBOL(strdup(alias->name), PMU_EVENT_SYMBOL);
1817dcb4e102SKan Liang 				len++;
1818dcb4e102SKan Liang 			}
1819dcb4e102SKan Liang 		}
1820231bb2aaSAndi Kleen 	}
1821dcb4e102SKan Liang 	qsort(perf_pmu_events_list, len,
1822dcb4e102SKan Liang 		sizeof(struct perf_pmu_event_symbol), comp_pmu);
1823dcb4e102SKan Liang 
1824dcb4e102SKan Liang 	return;
1825dcb4e102SKan Liang err:
1826dcb4e102SKan Liang 	perf_pmu__parse_cleanup();
1827dcb4e102SKan Liang }
1828dcb4e102SKan Liang 
1829dcb4e102SKan Liang enum perf_pmu_event_symbol_type
1830dcb4e102SKan Liang perf_pmu__parse_check(const char *name)
1831dcb4e102SKan Liang {
1832dcb4e102SKan Liang 	struct perf_pmu_event_symbol p, *r;
1833dcb4e102SKan Liang 
1834dcb4e102SKan Liang 	/* scan kernel pmu events from sysfs if needed */
1835dcb4e102SKan Liang 	if (perf_pmu_events_list_num == 0)
1836dcb4e102SKan Liang 		perf_pmu__parse_init();
1837dcb4e102SKan Liang 	/*
1838dcb4e102SKan Liang 	 * name "cpu" could be prefix of cpu-cycles or cpu// events.
1839dcb4e102SKan Liang 	 * cpu-cycles has been handled by hardcode.
1840dcb4e102SKan Liang 	 * So it must be cpu// events, not kernel pmu event.
1841dcb4e102SKan Liang 	 */
1842dcb4e102SKan Liang 	if ((perf_pmu_events_list_num <= 0) || !strcmp(name, "cpu"))
1843dcb4e102SKan Liang 		return PMU_EVENT_SYMBOL_ERR;
1844dcb4e102SKan Liang 
1845dcb4e102SKan Liang 	p.symbol = strdup(name);
1846dcb4e102SKan Liang 	r = bsearch(&p, perf_pmu_events_list,
1847dcb4e102SKan Liang 			(size_t) perf_pmu_events_list_num,
1848dcb4e102SKan Liang 			sizeof(struct perf_pmu_event_symbol), comp_pmu);
1849360e071bSTaeung Song 	zfree(&p.symbol);
1850dcb4e102SKan Liang 	return r ? r->type : PMU_EVENT_SYMBOL_ERR;
1851dcb4e102SKan Liang }
1852dcb4e102SKan Liang 
18535d9cdc11SArnaldo Carvalho de Melo static int parse_events__scanner(const char *str, void *parse_state, int start_token)
1854ac20de6fSZheng Yan {
1855ac20de6fSZheng Yan 	YY_BUFFER_STATE buffer;
1856ac20de6fSZheng Yan 	void *scanner;
1857ac20de6fSZheng Yan 	int ret;
1858ac20de6fSZheng Yan 
185990e2b22dSJiri Olsa 	ret = parse_events_lex_init_extra(start_token, &scanner);
1860ac20de6fSZheng Yan 	if (ret)
1861ac20de6fSZheng Yan 		return ret;
1862ac20de6fSZheng Yan 
1863ac20de6fSZheng Yan 	buffer = parse_events__scan_string(str, scanner);
1864ac20de6fSZheng Yan 
1865ac20de6fSZheng Yan #ifdef PARSER_DEBUG
1866ac20de6fSZheng Yan 	parse_events_debug = 1;
1867ac20de6fSZheng Yan #endif
18685d9cdc11SArnaldo Carvalho de Melo 	ret = parse_events_parse(parse_state, scanner);
1869ac20de6fSZheng Yan 
1870ac20de6fSZheng Yan 	parse_events__flush_buffer(buffer, scanner);
1871ac20de6fSZheng Yan 	parse_events__delete_buffer(buffer, scanner);
1872ac20de6fSZheng Yan 	parse_events_lex_destroy(scanner);
1873ac20de6fSZheng Yan 	return ret;
1874ac20de6fSZheng Yan }
1875ac20de6fSZheng Yan 
187690e2b22dSJiri Olsa /*
187790e2b22dSJiri Olsa  * parse event config string, return a list of event terms.
187890e2b22dSJiri Olsa  */
187990e2b22dSJiri Olsa int parse_events_terms(struct list_head *terms, const char *str)
188090e2b22dSJiri Olsa {
18815d9cdc11SArnaldo Carvalho de Melo 	struct parse_events_state parse_state = {
188290e2b22dSJiri Olsa 		.terms = NULL,
188390e2b22dSJiri Olsa 	};
188490e2b22dSJiri Olsa 	int ret;
188590e2b22dSJiri Olsa 
18865d9cdc11SArnaldo Carvalho de Melo 	ret = parse_events__scanner(str, &parse_state, PE_START_TERMS);
188790e2b22dSJiri Olsa 	if (!ret) {
18885d9cdc11SArnaldo Carvalho de Melo 		list_splice(parse_state.terms, terms);
18895d9cdc11SArnaldo Carvalho de Melo 		zfree(&parse_state.terms);
189090e2b22dSJiri Olsa 		return 0;
189190e2b22dSJiri Olsa 	}
189290e2b22dSJiri Olsa 
18935d9cdc11SArnaldo Carvalho de Melo 	parse_events_terms__delete(parse_state.terms);
189490e2b22dSJiri Olsa 	return ret;
189590e2b22dSJiri Olsa }
189690e2b22dSJiri Olsa 
1897b39b8393SJiri Olsa int parse_events(struct perf_evlist *evlist, const char *str,
1898b39b8393SJiri Olsa 		 struct parse_events_error *err)
189986470930SIngo Molnar {
19005d9cdc11SArnaldo Carvalho de Melo 	struct parse_events_state parse_state = {
19015d9cdc11SArnaldo Carvalho de Melo 		.list   = LIST_HEAD_INIT(parse_state.list),
190246010ab2SJiri Olsa 		.idx    = evlist->nr_entries,
1903b39b8393SJiri Olsa 		.error  = err,
19047630b3e2SWang Nan 		.evlist = evlist,
190546010ab2SJiri Olsa 	};
190646010ab2SJiri Olsa 	int ret;
190786470930SIngo Molnar 
19085d9cdc11SArnaldo Carvalho de Melo 	ret = parse_events__scanner(str, &parse_state, PE_START_EVENTS);
1909dcb4e102SKan Liang 	perf_pmu__parse_cleanup();
191089812fc8SJiri Olsa 	if (!ret) {
191115bfd2ccSWang Nan 		struct perf_evsel *last;
191215bfd2ccSWang Nan 
19135d9cdc11SArnaldo Carvalho de Melo 		if (list_empty(&parse_state.list)) {
1914c23080a6SArnaldo Carvalho de Melo 			WARN_ONCE(true, "WARNING: event parser found nothing\n");
1915854f7363SWang Nan 			return -1;
1916854f7363SWang Nan 		}
1917854f7363SWang Nan 
19185d9cdc11SArnaldo Carvalho de Melo 		perf_evlist__splice_list_tail(evlist, &parse_state.list);
19195d9cdc11SArnaldo Carvalho de Melo 		evlist->nr_groups += parse_state.nr_groups;
192015bfd2ccSWang Nan 		last = perf_evlist__last(evlist);
192115bfd2ccSWang Nan 		last->cmdline_group_boundary = true;
192215bfd2ccSWang Nan 
192386470930SIngo Molnar 		return 0;
192486470930SIngo Molnar 	}
192586470930SIngo Molnar 
19265d7be90eSJiri Olsa 	/*
19275d7be90eSJiri Olsa 	 * There are 2 users - builtin-record and builtin-test objects.
19285d7be90eSJiri Olsa 	 * Both call perf_evlist__delete in case of error, so we dont
19295d7be90eSJiri Olsa 	 * need to bother.
19305d7be90eSJiri Olsa 	 */
193189812fc8SJiri Olsa 	return ret;
193289812fc8SJiri Olsa }
193389812fc8SJiri Olsa 
1934b39b8393SJiri Olsa #define MAX_WIDTH 1000
1935b39b8393SJiri Olsa static int get_term_width(void)
1936b39b8393SJiri Olsa {
1937b39b8393SJiri Olsa 	struct winsize ws;
1938b39b8393SJiri Olsa 
1939b39b8393SJiri Olsa 	get_term_dimensions(&ws);
1940b39b8393SJiri Olsa 	return ws.ws_col > MAX_WIDTH ? MAX_WIDTH : ws.ws_col;
1941b39b8393SJiri Olsa }
1942b39b8393SJiri Olsa 
1943333b5665SAndi Kleen void parse_events_print_error(struct parse_events_error *err,
1944b39b8393SJiri Olsa 			      const char *event)
1945b39b8393SJiri Olsa {
1946b39b8393SJiri Olsa 	const char *str = "invalid or unsupported event: ";
1947b39b8393SJiri Olsa 	char _buf[MAX_WIDTH];
1948b39b8393SJiri Olsa 	char *buf = (char *) event;
1949b39b8393SJiri Olsa 	int idx = 0;
1950b39b8393SJiri Olsa 
1951b39b8393SJiri Olsa 	if (err->str) {
1952b39b8393SJiri Olsa 		/* -2 for extra '' in the final fprintf */
1953b39b8393SJiri Olsa 		int width       = get_term_width() - 2;
1954b39b8393SJiri Olsa 		int len_event   = strlen(event);
1955b39b8393SJiri Olsa 		int len_str, max_len, cut = 0;
1956b39b8393SJiri Olsa 
1957b39b8393SJiri Olsa 		/*
1958b39b8393SJiri Olsa 		 * Maximum error index indent, we will cut
1959b39b8393SJiri Olsa 		 * the event string if it's bigger.
1960b39b8393SJiri Olsa 		 */
1961141b2d31SAdrian Hunter 		int max_err_idx = 13;
1962b39b8393SJiri Olsa 
1963b39b8393SJiri Olsa 		/*
1964b39b8393SJiri Olsa 		 * Let's be specific with the message when
1965b39b8393SJiri Olsa 		 * we have the precise error.
1966b39b8393SJiri Olsa 		 */
1967b39b8393SJiri Olsa 		str     = "event syntax error: ";
1968b39b8393SJiri Olsa 		len_str = strlen(str);
1969b39b8393SJiri Olsa 		max_len = width - len_str;
1970b39b8393SJiri Olsa 
1971b39b8393SJiri Olsa 		buf = _buf;
1972b39b8393SJiri Olsa 
1973bd1a0be5SAdam Buchbinder 		/* We're cutting from the beginning. */
1974b39b8393SJiri Olsa 		if (err->idx > max_err_idx)
1975b39b8393SJiri Olsa 			cut = err->idx - max_err_idx;
1976b39b8393SJiri Olsa 
1977b39b8393SJiri Olsa 		strncpy(buf, event + cut, max_len);
1978b39b8393SJiri Olsa 
1979b39b8393SJiri Olsa 		/* Mark cut parts with '..' on both sides. */
1980b39b8393SJiri Olsa 		if (cut)
1981b39b8393SJiri Olsa 			buf[0] = buf[1] = '.';
1982b39b8393SJiri Olsa 
1983b39b8393SJiri Olsa 		if ((len_event - cut) > max_len) {
1984b39b8393SJiri Olsa 			buf[max_len - 1] = buf[max_len - 2] = '.';
1985b39b8393SJiri Olsa 			buf[max_len] = 0;
1986b39b8393SJiri Olsa 		}
1987b39b8393SJiri Olsa 
1988b39b8393SJiri Olsa 		idx = len_str + err->idx - cut;
1989b39b8393SJiri Olsa 	}
1990b39b8393SJiri Olsa 
1991b39b8393SJiri Olsa 	fprintf(stderr, "%s'%s'\n", str, buf);
1992b39b8393SJiri Olsa 	if (idx) {
1993b39b8393SJiri Olsa 		fprintf(stderr, "%*s\\___ %s\n", idx + 1, "", err->str);
1994b39b8393SJiri Olsa 		if (err->help)
1995b39b8393SJiri Olsa 			fprintf(stderr, "\n%s\n", err->help);
1996360e071bSTaeung Song 		zfree(&err->str);
1997360e071bSTaeung Song 		zfree(&err->help);
1998b39b8393SJiri Olsa 	}
1999b39b8393SJiri Olsa }
2000b39b8393SJiri Olsa 
2001b39b8393SJiri Olsa #undef MAX_WIDTH
2002b39b8393SJiri Olsa 
2003f120f9d5SJiri Olsa int parse_events_option(const struct option *opt, const char *str,
20041d037ca1SIrina Tirdea 			int unset __maybe_unused)
2005f120f9d5SJiri Olsa {
2006f120f9d5SJiri Olsa 	struct perf_evlist *evlist = *(struct perf_evlist **)opt->value;
2007b39b8393SJiri Olsa 	struct parse_events_error err = { .idx = 0, };
2008b39b8393SJiri Olsa 	int ret = parse_events(evlist, str, &err);
20099175ce1fSAndi Kleen 
2010333b5665SAndi Kleen 	if (ret) {
2011b39b8393SJiri Olsa 		parse_events_print_error(&err, str);
2012333b5665SAndi Kleen 		fprintf(stderr, "Run 'perf list' for a list of valid events\n");
2013333b5665SAndi Kleen 	}
2014b39b8393SJiri Olsa 
20159175ce1fSAndi Kleen 	return ret;
2016f120f9d5SJiri Olsa }
2017f120f9d5SJiri Olsa 
20184ba1faa1SWang Nan static int
20194ba1faa1SWang Nan foreach_evsel_in_last_glob(struct perf_evlist *evlist,
20204ba1faa1SWang Nan 			   int (*func)(struct perf_evsel *evsel,
20214ba1faa1SWang Nan 				       const void *arg),
20224ba1faa1SWang Nan 			   const void *arg)
2023c171b552SLi Zefan {
202469aad6f1SArnaldo Carvalho de Melo 	struct perf_evsel *last = NULL;
20254ba1faa1SWang Nan 	int err;
2026c171b552SLi Zefan 
2027854f7363SWang Nan 	/*
2028854f7363SWang Nan 	 * Don't return when list_empty, give func a chance to report
2029854f7363SWang Nan 	 * error when it found last == NULL.
2030854f7363SWang Nan 	 *
2031854f7363SWang Nan 	 * So no need to WARN here, let *func do this.
2032854f7363SWang Nan 	 */
2033361c99a6SArnaldo Carvalho de Melo 	if (evlist->nr_entries > 0)
20340c21f736SArnaldo Carvalho de Melo 		last = perf_evlist__last(evlist);
203569aad6f1SArnaldo Carvalho de Melo 
203615bfd2ccSWang Nan 	do {
20374ba1faa1SWang Nan 		err = (*func)(last, arg);
20384ba1faa1SWang Nan 		if (err)
2039c171b552SLi Zefan 			return -1;
20404ba1faa1SWang Nan 		if (!last)
20414ba1faa1SWang Nan 			return 0;
2042c171b552SLi Zefan 
204315bfd2ccSWang Nan 		if (last->node.prev == &evlist->entries)
204415bfd2ccSWang Nan 			return 0;
204515bfd2ccSWang Nan 		last = list_entry(last->node.prev, struct perf_evsel, node);
204615bfd2ccSWang Nan 	} while (!last->cmdline_group_boundary);
204715bfd2ccSWang Nan 
2048c171b552SLi Zefan 	return 0;
2049c171b552SLi Zefan }
2050c171b552SLi Zefan 
20514ba1faa1SWang Nan static int set_filter(struct perf_evsel *evsel, const void *arg)
20524ba1faa1SWang Nan {
20534ba1faa1SWang Nan 	const char *str = arg;
20541e857484SMathieu Poirier 	bool found = false;
20551e857484SMathieu Poirier 	int nr_addr_filters = 0;
20561e857484SMathieu Poirier 	struct perf_pmu *pmu = NULL;
20574ba1faa1SWang Nan 
205849836f78SJack Henschel 	if (evsel == NULL) {
205949836f78SJack Henschel 		fprintf(stderr,
206049836f78SJack Henschel 			"--filter option should follow a -e tracepoint or HW tracer option\n");
206149836f78SJack Henschel 		return -1;
206249836f78SJack Henschel 	}
20634ba1faa1SWang Nan 
20641e857484SMathieu Poirier 	if (evsel->attr.type == PERF_TYPE_TRACEPOINT) {
20653541c034SMathieu Poirier 		if (perf_evsel__append_tp_filter(evsel, str) < 0) {
20664ba1faa1SWang Nan 			fprintf(stderr,
20674ba1faa1SWang Nan 				"not enough memory to hold filter string\n");
20684ba1faa1SWang Nan 			return -1;
20694ba1faa1SWang Nan 		}
20704ba1faa1SWang Nan 
20714ba1faa1SWang Nan 		return 0;
20724ba1faa1SWang Nan 	}
20734ba1faa1SWang Nan 
20741e857484SMathieu Poirier 	while ((pmu = perf_pmu__scan(pmu)) != NULL)
20751e857484SMathieu Poirier 		if (pmu->type == evsel->attr.type) {
20761e857484SMathieu Poirier 			found = true;
20771e857484SMathieu Poirier 			break;
20781e857484SMathieu Poirier 		}
20791e857484SMathieu Poirier 
20801e857484SMathieu Poirier 	if (found)
20811e857484SMathieu Poirier 		perf_pmu__scan_file(pmu, "nr_addr_filters",
20821e857484SMathieu Poirier 				    "%d", &nr_addr_filters);
20831e857484SMathieu Poirier 
208449836f78SJack Henschel 	if (!nr_addr_filters) {
208549836f78SJack Henschel 		fprintf(stderr,
208649836f78SJack Henschel 			"This CPU does not support address filtering\n");
208749836f78SJack Henschel 		return -1;
208849836f78SJack Henschel 	}
20891e857484SMathieu Poirier 
20901e857484SMathieu Poirier 	if (perf_evsel__append_addr_filter(evsel, str) < 0) {
20911e857484SMathieu Poirier 		fprintf(stderr,
20921e857484SMathieu Poirier 			"not enough memory to hold filter string\n");
20931e857484SMathieu Poirier 		return -1;
20941e857484SMathieu Poirier 	}
20951e857484SMathieu Poirier 
20961e857484SMathieu Poirier 	return 0;
20971e857484SMathieu Poirier }
20981e857484SMathieu Poirier 
20994ba1faa1SWang Nan int parse_filter(const struct option *opt, const char *str,
21004ba1faa1SWang Nan 		 int unset __maybe_unused)
21014ba1faa1SWang Nan {
21024ba1faa1SWang Nan 	struct perf_evlist *evlist = *(struct perf_evlist **)opt->value;
21034ba1faa1SWang Nan 
21044ba1faa1SWang Nan 	return foreach_evsel_in_last_glob(evlist, set_filter,
21054ba1faa1SWang Nan 					  (const void *)str);
21064ba1faa1SWang Nan }
21074ba1faa1SWang Nan 
21084ba1faa1SWang Nan static int add_exclude_perf_filter(struct perf_evsel *evsel,
21094ba1faa1SWang Nan 				   const void *arg __maybe_unused)
21104ba1faa1SWang Nan {
21114ba1faa1SWang Nan 	char new_filter[64];
21124ba1faa1SWang Nan 
21134ba1faa1SWang Nan 	if (evsel == NULL || evsel->attr.type != PERF_TYPE_TRACEPOINT) {
21144ba1faa1SWang Nan 		fprintf(stderr,
21154ba1faa1SWang Nan 			"--exclude-perf option should follow a -e tracepoint option\n");
21164ba1faa1SWang Nan 		return -1;
21174ba1faa1SWang Nan 	}
21184ba1faa1SWang Nan 
21194ba1faa1SWang Nan 	snprintf(new_filter, sizeof(new_filter), "common_pid != %d", getpid());
21204ba1faa1SWang Nan 
21213541c034SMathieu Poirier 	if (perf_evsel__append_tp_filter(evsel, new_filter) < 0) {
21224ba1faa1SWang Nan 		fprintf(stderr,
21234ba1faa1SWang Nan 			"not enough memory to hold filter string\n");
21244ba1faa1SWang Nan 		return -1;
21254ba1faa1SWang Nan 	}
21264ba1faa1SWang Nan 
21274ba1faa1SWang Nan 	return 0;
21284ba1faa1SWang Nan }
21294ba1faa1SWang Nan 
21304ba1faa1SWang Nan int exclude_perf(const struct option *opt,
21314ba1faa1SWang Nan 		 const char *arg __maybe_unused,
21324ba1faa1SWang Nan 		 int unset __maybe_unused)
21334ba1faa1SWang Nan {
21344ba1faa1SWang Nan 	struct perf_evlist *evlist = *(struct perf_evlist **)opt->value;
21354ba1faa1SWang Nan 
21364ba1faa1SWang Nan 	return foreach_evsel_in_last_glob(evlist, add_exclude_perf_filter,
21374ba1faa1SWang Nan 					  NULL);
21384ba1faa1SWang Nan }
21394ba1faa1SWang Nan 
214086470930SIngo Molnar static const char * const event_type_descriptors[] = {
214186470930SIngo Molnar 	"Hardware event",
214286470930SIngo Molnar 	"Software event",
214386470930SIngo Molnar 	"Tracepoint event",
214486470930SIngo Molnar 	"Hardware cache event",
214541bdcb23SLiming Wang 	"Raw hardware event descriptor",
214641bdcb23SLiming Wang 	"Hardware breakpoint",
214786470930SIngo Molnar };
214886470930SIngo Molnar 
2149ab0e4800SYunlong Song static int cmp_string(const void *a, const void *b)
2150ab0e4800SYunlong Song {
2151ab0e4800SYunlong Song 	const char * const *as = a;
2152ab0e4800SYunlong Song 	const char * const *bs = b;
2153ab0e4800SYunlong Song 
2154ab0e4800SYunlong Song 	return strcmp(*as, *bs);
2155ab0e4800SYunlong Song }
2156ab0e4800SYunlong Song 
215786470930SIngo Molnar /*
2158f6bdafefSJason Baron  * Print the events from <debugfs_mount_point>/tracing/events
2159f6bdafefSJason Baron  */
2160f6bdafefSJason Baron 
2161a3277d2dSFrederic Weisbecker void print_tracepoint_events(const char *subsys_glob, const char *event_glob,
2162a3277d2dSFrederic Weisbecker 			     bool name_only)
2163f6bdafefSJason Baron {
2164f6bdafefSJason Baron 	DIR *sys_dir, *evt_dir;
216522a9f41bSArnaldo Carvalho de Melo 	struct dirent *sys_dirent, *evt_dirent;
2166f6bdafefSJason Baron 	char evt_path[MAXPATHLEN];
216725a7d914SArnaldo Carvalho de Melo 	char *dir_path;
2168ab0e4800SYunlong Song 	char **evt_list = NULL;
2169ab0e4800SYunlong Song 	unsigned int evt_i = 0, evt_num = 0;
2170ab0e4800SYunlong Song 	bool evt_num_known = false;
2171f6bdafefSJason Baron 
2172ab0e4800SYunlong Song restart:
21737014e0e3SArnaldo Carvalho de Melo 	sys_dir = tracing_events__opendir();
2174f6bdafefSJason Baron 	if (!sys_dir)
2175725b1368SEric Dumazet 		return;
2176f6bdafefSJason Baron 
2177ab0e4800SYunlong Song 	if (evt_num_known) {
2178ab0e4800SYunlong Song 		evt_list = zalloc(sizeof(char *) * evt_num);
2179ab0e4800SYunlong Song 		if (!evt_list)
2180ab0e4800SYunlong Song 			goto out_close_sys_dir;
2181ab0e4800SYunlong Song 	}
2182ab0e4800SYunlong Song 
218322a9f41bSArnaldo Carvalho de Melo 	for_each_subsystem(sys_dir, sys_dirent) {
2184668b8788SArnaldo Carvalho de Melo 		if (subsys_glob != NULL &&
218522a9f41bSArnaldo Carvalho de Melo 		    !strglobmatch(sys_dirent->d_name, subsys_glob))
2186668b8788SArnaldo Carvalho de Melo 			continue;
2187725b1368SEric Dumazet 
218825a7d914SArnaldo Carvalho de Melo 		dir_path = get_events_file(sys_dirent->d_name);
218925a7d914SArnaldo Carvalho de Melo 		if (!dir_path)
219025a7d914SArnaldo Carvalho de Melo 			continue;
2191725b1368SEric Dumazet 		evt_dir = opendir(dir_path);
2192725b1368SEric Dumazet 		if (!evt_dir)
219325a7d914SArnaldo Carvalho de Melo 			goto next;
2194725b1368SEric Dumazet 
2195c02cab22SArnaldo Carvalho de Melo 		for_each_event(dir_path, evt_dir, evt_dirent) {
2196668b8788SArnaldo Carvalho de Melo 			if (event_glob != NULL &&
219722a9f41bSArnaldo Carvalho de Melo 			    !strglobmatch(evt_dirent->d_name, event_glob))
2198668b8788SArnaldo Carvalho de Melo 				continue;
2199668b8788SArnaldo Carvalho de Melo 
2200ab0e4800SYunlong Song 			if (!evt_num_known) {
2201ab0e4800SYunlong Song 				evt_num++;
2202a3277d2dSFrederic Weisbecker 				continue;
2203a3277d2dSFrederic Weisbecker 			}
2204a3277d2dSFrederic Weisbecker 
2205f6bdafefSJason Baron 			snprintf(evt_path, MAXPATHLEN, "%s:%s",
220622a9f41bSArnaldo Carvalho de Melo 				 sys_dirent->d_name, evt_dirent->d_name);
2207ab0e4800SYunlong Song 
2208ab0e4800SYunlong Song 			evt_list[evt_i] = strdup(evt_path);
220925a7d914SArnaldo Carvalho de Melo 			if (evt_list[evt_i] == NULL) {
221025a7d914SArnaldo Carvalho de Melo 				put_events_file(dir_path);
2211ab0e4800SYunlong Song 				goto out_close_evt_dir;
221225a7d914SArnaldo Carvalho de Melo 			}
2213ab0e4800SYunlong Song 			evt_i++;
2214f6bdafefSJason Baron 		}
2215f6bdafefSJason Baron 		closedir(evt_dir);
221625a7d914SArnaldo Carvalho de Melo next:
221725a7d914SArnaldo Carvalho de Melo 		put_events_file(dir_path);
2218f6bdafefSJason Baron 	}
2219f6bdafefSJason Baron 	closedir(sys_dir);
2220ab0e4800SYunlong Song 
2221ab0e4800SYunlong Song 	if (!evt_num_known) {
2222ab0e4800SYunlong Song 		evt_num_known = true;
2223ab0e4800SYunlong Song 		goto restart;
2224ab0e4800SYunlong Song 	}
2225ab0e4800SYunlong Song 	qsort(evt_list, evt_num, sizeof(char *), cmp_string);
2226ab0e4800SYunlong Song 	evt_i = 0;
2227ab0e4800SYunlong Song 	while (evt_i < evt_num) {
2228ab0e4800SYunlong Song 		if (name_only) {
2229ab0e4800SYunlong Song 			printf("%s ", evt_list[evt_i++]);
2230ab0e4800SYunlong Song 			continue;
2231ab0e4800SYunlong Song 		}
2232ab0e4800SYunlong Song 		printf("  %-50s [%s]\n", evt_list[evt_i++],
2233ab0e4800SYunlong Song 				event_type_descriptors[PERF_TYPE_TRACEPOINT]);
2234ab0e4800SYunlong Song 	}
2235dfc431cbSArnaldo Carvalho de Melo 	if (evt_num && pager_in_use())
2236ab0e4800SYunlong Song 		printf("\n");
2237ab0e4800SYunlong Song 
2238ab0e4800SYunlong Song out_free:
2239ab0e4800SYunlong Song 	evt_num = evt_i;
2240ab0e4800SYunlong Song 	for (evt_i = 0; evt_i < evt_num; evt_i++)
2241ab0e4800SYunlong Song 		zfree(&evt_list[evt_i]);
2242ab0e4800SYunlong Song 	zfree(&evt_list);
2243ab0e4800SYunlong Song 	return;
2244ab0e4800SYunlong Song 
2245ab0e4800SYunlong Song out_close_evt_dir:
2246ab0e4800SYunlong Song 	closedir(evt_dir);
2247ab0e4800SYunlong Song out_close_sys_dir:
2248ab0e4800SYunlong Song 	closedir(sys_dir);
2249ab0e4800SYunlong Song 
2250ab0e4800SYunlong Song 	printf("FATAL: not enough memory to print %s\n",
2251ab0e4800SYunlong Song 			event_type_descriptors[PERF_TYPE_TRACEPOINT]);
2252ab0e4800SYunlong Song 	if (evt_list)
2253ab0e4800SYunlong Song 		goto out_free;
2254f6bdafefSJason Baron }
2255f6bdafefSJason Baron 
2256f6bdafefSJason Baron /*
225720c457b8SThomas Renninger  * Check whether event is in <debugfs_mount_point>/tracing/events
225820c457b8SThomas Renninger  */
225920c457b8SThomas Renninger 
226020c457b8SThomas Renninger int is_valid_tracepoint(const char *event_string)
226120c457b8SThomas Renninger {
226220c457b8SThomas Renninger 	DIR *sys_dir, *evt_dir;
226322a9f41bSArnaldo Carvalho de Melo 	struct dirent *sys_dirent, *evt_dirent;
226420c457b8SThomas Renninger 	char evt_path[MAXPATHLEN];
226525a7d914SArnaldo Carvalho de Melo 	char *dir_path;
226620c457b8SThomas Renninger 
22677014e0e3SArnaldo Carvalho de Melo 	sys_dir = tracing_events__opendir();
226820c457b8SThomas Renninger 	if (!sys_dir)
226920c457b8SThomas Renninger 		return 0;
227020c457b8SThomas Renninger 
227122a9f41bSArnaldo Carvalho de Melo 	for_each_subsystem(sys_dir, sys_dirent) {
227225a7d914SArnaldo Carvalho de Melo 		dir_path = get_events_file(sys_dirent->d_name);
227325a7d914SArnaldo Carvalho de Melo 		if (!dir_path)
227425a7d914SArnaldo Carvalho de Melo 			continue;
227520c457b8SThomas Renninger 		evt_dir = opendir(dir_path);
227620c457b8SThomas Renninger 		if (!evt_dir)
227725a7d914SArnaldo Carvalho de Melo 			goto next;
227820c457b8SThomas Renninger 
2279c02cab22SArnaldo Carvalho de Melo 		for_each_event(dir_path, evt_dir, evt_dirent) {
228020c457b8SThomas Renninger 			snprintf(evt_path, MAXPATHLEN, "%s:%s",
228122a9f41bSArnaldo Carvalho de Melo 				 sys_dirent->d_name, evt_dirent->d_name);
228220c457b8SThomas Renninger 			if (!strcmp(evt_path, event_string)) {
228320c457b8SThomas Renninger 				closedir(evt_dir);
228420c457b8SThomas Renninger 				closedir(sys_dir);
228520c457b8SThomas Renninger 				return 1;
228620c457b8SThomas Renninger 			}
228720c457b8SThomas Renninger 		}
228820c457b8SThomas Renninger 		closedir(evt_dir);
228925a7d914SArnaldo Carvalho de Melo next:
229025a7d914SArnaldo Carvalho de Melo 		put_events_file(dir_path);
229120c457b8SThomas Renninger 	}
229220c457b8SThomas Renninger 	closedir(sys_dir);
229320c457b8SThomas Renninger 	return 0;
229420c457b8SThomas Renninger }
229520c457b8SThomas Renninger 
2296b41f1cecSNamhyung Kim static bool is_event_supported(u8 type, unsigned config)
2297b41f1cecSNamhyung Kim {
2298b41f1cecSNamhyung Kim 	bool ret = true;
229988fee52eSVince Weaver 	int open_return;
2300b41f1cecSNamhyung Kim 	struct perf_evsel *evsel;
2301b41f1cecSNamhyung Kim 	struct perf_event_attr attr = {
2302b41f1cecSNamhyung Kim 		.type = type,
2303b41f1cecSNamhyung Kim 		.config = config,
2304b41f1cecSNamhyung Kim 		.disabled = 1,
2305b41f1cecSNamhyung Kim 	};
230689896051SArnaldo Carvalho de Melo 	struct thread_map *tmap = thread_map__new_by_tid(0);
230789896051SArnaldo Carvalho de Melo 
230889896051SArnaldo Carvalho de Melo 	if (tmap == NULL)
230989896051SArnaldo Carvalho de Melo 		return false;
2310b41f1cecSNamhyung Kim 
2311ef503831SArnaldo Carvalho de Melo 	evsel = perf_evsel__new(&attr);
2312b41f1cecSNamhyung Kim 	if (evsel) {
231389896051SArnaldo Carvalho de Melo 		open_return = perf_evsel__open(evsel, NULL, tmap);
231488fee52eSVince Weaver 		ret = open_return >= 0;
231588fee52eSVince Weaver 
231688fee52eSVince Weaver 		if (open_return == -EACCES) {
231788fee52eSVince Weaver 			/*
231888fee52eSVince Weaver 			 * This happens if the paranoid value
231988fee52eSVince Weaver 			 * /proc/sys/kernel/perf_event_paranoid is set to 2
232088fee52eSVince Weaver 			 * Re-run with exclude_kernel set; we don't do that
232188fee52eSVince Weaver 			 * by default as some ARM machines do not support it.
232288fee52eSVince Weaver 			 *
232388fee52eSVince Weaver 			 */
232488fee52eSVince Weaver 			evsel->attr.exclude_kernel = 1;
232589896051SArnaldo Carvalho de Melo 			ret = perf_evsel__open(evsel, NULL, tmap) >= 0;
232688fee52eSVince Weaver 		}
2327b41f1cecSNamhyung Kim 		perf_evsel__delete(evsel);
2328b41f1cecSNamhyung Kim 	}
2329b41f1cecSNamhyung Kim 
233039df730bSChangbin Du 	thread_map__put(tmap);
2331b41f1cecSNamhyung Kim 	return ret;
2332b41f1cecSNamhyung Kim }
2333b41f1cecSNamhyung Kim 
233440218daeSMasami Hiramatsu void print_sdt_events(const char *subsys_glob, const char *event_glob,
233540218daeSMasami Hiramatsu 		      bool name_only)
233640218daeSMasami Hiramatsu {
233740218daeSMasami Hiramatsu 	struct probe_cache *pcache;
233840218daeSMasami Hiramatsu 	struct probe_cache_entry *ent;
233940218daeSMasami Hiramatsu 	struct strlist *bidlist, *sdtlist;
234040218daeSMasami Hiramatsu 	struct strlist_config cfg = {.dont_dupstr = true};
234140218daeSMasami Hiramatsu 	struct str_node *nd, *nd2;
234240218daeSMasami Hiramatsu 	char *buf, *path, *ptr = NULL;
234340218daeSMasami Hiramatsu 	bool show_detail = false;
234440218daeSMasami Hiramatsu 	int ret;
234540218daeSMasami Hiramatsu 
234640218daeSMasami Hiramatsu 	sdtlist = strlist__new(NULL, &cfg);
234740218daeSMasami Hiramatsu 	if (!sdtlist) {
234840218daeSMasami Hiramatsu 		pr_debug("Failed to allocate new strlist for SDT\n");
234940218daeSMasami Hiramatsu 		return;
235040218daeSMasami Hiramatsu 	}
235140218daeSMasami Hiramatsu 	bidlist = build_id_cache__list_all(true);
235240218daeSMasami Hiramatsu 	if (!bidlist) {
235340218daeSMasami Hiramatsu 		pr_debug("Failed to get buildids: %d\n", errno);
235440218daeSMasami Hiramatsu 		return;
235540218daeSMasami Hiramatsu 	}
235640218daeSMasami Hiramatsu 	strlist__for_each_entry(nd, bidlist) {
2357f045b8c4SKrister Johansen 		pcache = probe_cache__new(nd->s, NULL);
235840218daeSMasami Hiramatsu 		if (!pcache)
235940218daeSMasami Hiramatsu 			continue;
236040218daeSMasami Hiramatsu 		list_for_each_entry(ent, &pcache->entries, node) {
236140218daeSMasami Hiramatsu 			if (!ent->sdt)
236240218daeSMasami Hiramatsu 				continue;
236340218daeSMasami Hiramatsu 			if (subsys_glob &&
236440218daeSMasami Hiramatsu 			    !strglobmatch(ent->pev.group, subsys_glob))
236540218daeSMasami Hiramatsu 				continue;
236640218daeSMasami Hiramatsu 			if (event_glob &&
236740218daeSMasami Hiramatsu 			    !strglobmatch(ent->pev.event, event_glob))
236840218daeSMasami Hiramatsu 				continue;
236940218daeSMasami Hiramatsu 			ret = asprintf(&buf, "%s:%s@%s", ent->pev.group,
237040218daeSMasami Hiramatsu 					ent->pev.event, nd->s);
237140218daeSMasami Hiramatsu 			if (ret > 0)
237240218daeSMasami Hiramatsu 				strlist__add(sdtlist, buf);
237340218daeSMasami Hiramatsu 		}
237440218daeSMasami Hiramatsu 		probe_cache__delete(pcache);
237540218daeSMasami Hiramatsu 	}
237640218daeSMasami Hiramatsu 	strlist__delete(bidlist);
237740218daeSMasami Hiramatsu 
237840218daeSMasami Hiramatsu 	strlist__for_each_entry(nd, sdtlist) {
237940218daeSMasami Hiramatsu 		buf = strchr(nd->s, '@');
238040218daeSMasami Hiramatsu 		if (buf)
238140218daeSMasami Hiramatsu 			*(buf++) = '\0';
238240218daeSMasami Hiramatsu 		if (name_only) {
238340218daeSMasami Hiramatsu 			printf("%s ", nd->s);
238440218daeSMasami Hiramatsu 			continue;
238540218daeSMasami Hiramatsu 		}
238640218daeSMasami Hiramatsu 		nd2 = strlist__next(nd);
238740218daeSMasami Hiramatsu 		if (nd2) {
238840218daeSMasami Hiramatsu 			ptr = strchr(nd2->s, '@');
238940218daeSMasami Hiramatsu 			if (ptr)
239040218daeSMasami Hiramatsu 				*ptr = '\0';
239140218daeSMasami Hiramatsu 			if (strcmp(nd->s, nd2->s) == 0)
239240218daeSMasami Hiramatsu 				show_detail = true;
239340218daeSMasami Hiramatsu 		}
239440218daeSMasami Hiramatsu 		if (show_detail) {
239540218daeSMasami Hiramatsu 			path = build_id_cache__origname(buf);
239640218daeSMasami Hiramatsu 			ret = asprintf(&buf, "%s@%s(%.12s)", nd->s, path, buf);
239740218daeSMasami Hiramatsu 			if (ret > 0) {
239840218daeSMasami Hiramatsu 				printf("  %-50s [%s]\n", buf, "SDT event");
239940218daeSMasami Hiramatsu 				free(buf);
240040218daeSMasami Hiramatsu 			}
24018bde8516SChangbin Du 			free(path);
240240218daeSMasami Hiramatsu 		} else
240340218daeSMasami Hiramatsu 			printf("  %-50s [%s]\n", nd->s, "SDT event");
240440218daeSMasami Hiramatsu 		if (nd2) {
240540218daeSMasami Hiramatsu 			if (strcmp(nd->s, nd2->s) != 0)
240640218daeSMasami Hiramatsu 				show_detail = false;
240740218daeSMasami Hiramatsu 			if (ptr)
240840218daeSMasami Hiramatsu 				*ptr = '@';
240940218daeSMasami Hiramatsu 		}
241040218daeSMasami Hiramatsu 	}
241140218daeSMasami Hiramatsu 	strlist__delete(sdtlist);
241240218daeSMasami Hiramatsu }
241340218daeSMasami Hiramatsu 
2414a3277d2dSFrederic Weisbecker int print_hwcache_events(const char *event_glob, bool name_only)
2415668b8788SArnaldo Carvalho de Melo {
2416ab0e4800SYunlong Song 	unsigned int type, op, i, evt_i = 0, evt_num = 0;
24170b668bc9SArnaldo Carvalho de Melo 	char name[64];
2418ab0e4800SYunlong Song 	char **evt_list = NULL;
2419ab0e4800SYunlong Song 	bool evt_num_known = false;
2420ab0e4800SYunlong Song 
2421ab0e4800SYunlong Song restart:
2422ab0e4800SYunlong Song 	if (evt_num_known) {
2423ab0e4800SYunlong Song 		evt_list = zalloc(sizeof(char *) * evt_num);
2424ab0e4800SYunlong Song 		if (!evt_list)
2425ab0e4800SYunlong Song 			goto out_enomem;
2426ab0e4800SYunlong Song 	}
2427668b8788SArnaldo Carvalho de Melo 
2428668b8788SArnaldo Carvalho de Melo 	for (type = 0; type < PERF_COUNT_HW_CACHE_MAX; type++) {
2429668b8788SArnaldo Carvalho de Melo 		for (op = 0; op < PERF_COUNT_HW_CACHE_OP_MAX; op++) {
2430668b8788SArnaldo Carvalho de Melo 			/* skip invalid cache type */
24310b668bc9SArnaldo Carvalho de Melo 			if (!perf_evsel__is_cache_op_valid(type, op))
2432668b8788SArnaldo Carvalho de Melo 				continue;
2433668b8788SArnaldo Carvalho de Melo 
2434668b8788SArnaldo Carvalho de Melo 			for (i = 0; i < PERF_COUNT_HW_CACHE_RESULT_MAX; i++) {
24350b668bc9SArnaldo Carvalho de Melo 				__perf_evsel__hw_cache_type_op_res_name(type, op, i,
24360b668bc9SArnaldo Carvalho de Melo 									name, sizeof(name));
2437947b4ad1SIngo Molnar 				if (event_glob != NULL && !strglobmatch(name, event_glob))
2438668b8788SArnaldo Carvalho de Melo 					continue;
2439668b8788SArnaldo Carvalho de Melo 
2440b41f1cecSNamhyung Kim 				if (!is_event_supported(PERF_TYPE_HW_CACHE,
2441b41f1cecSNamhyung Kim 							type | (op << 8) | (i << 16)))
2442b41f1cecSNamhyung Kim 					continue;
2443b41f1cecSNamhyung Kim 
2444ab0e4800SYunlong Song 				if (!evt_num_known) {
2445ab0e4800SYunlong Song 					evt_num++;
2446ab0e4800SYunlong Song 					continue;
2447ab0e4800SYunlong Song 				}
2448ab0e4800SYunlong Song 
2449ab0e4800SYunlong Song 				evt_list[evt_i] = strdup(name);
2450ab0e4800SYunlong Song 				if (evt_list[evt_i] == NULL)
2451ab0e4800SYunlong Song 					goto out_enomem;
2452ab0e4800SYunlong Song 				evt_i++;
2453668b8788SArnaldo Carvalho de Melo 			}
2454668b8788SArnaldo Carvalho de Melo 		}
2455668b8788SArnaldo Carvalho de Melo 	}
2456668b8788SArnaldo Carvalho de Melo 
2457ab0e4800SYunlong Song 	if (!evt_num_known) {
2458ab0e4800SYunlong Song 		evt_num_known = true;
2459ab0e4800SYunlong Song 		goto restart;
2460ab0e4800SYunlong Song 	}
2461ab0e4800SYunlong Song 	qsort(evt_list, evt_num, sizeof(char *), cmp_string);
2462ab0e4800SYunlong Song 	evt_i = 0;
2463ab0e4800SYunlong Song 	while (evt_i < evt_num) {
2464ab0e4800SYunlong Song 		if (name_only) {
2465ab0e4800SYunlong Song 			printf("%s ", evt_list[evt_i++]);
2466ab0e4800SYunlong Song 			continue;
2467ab0e4800SYunlong Song 		}
2468ab0e4800SYunlong Song 		printf("  %-50s [%s]\n", evt_list[evt_i++],
2469ab0e4800SYunlong Song 				event_type_descriptors[PERF_TYPE_HW_CACHE]);
2470ab0e4800SYunlong Song 	}
2471dfc431cbSArnaldo Carvalho de Melo 	if (evt_num && pager_in_use())
2472dc098b35SAndi Kleen 		printf("\n");
2473ab0e4800SYunlong Song 
2474ab0e4800SYunlong Song out_free:
2475ab0e4800SYunlong Song 	evt_num = evt_i;
2476ab0e4800SYunlong Song 	for (evt_i = 0; evt_i < evt_num; evt_i++)
2477ab0e4800SYunlong Song 		zfree(&evt_list[evt_i]);
2478ab0e4800SYunlong Song 	zfree(&evt_list);
2479ab0e4800SYunlong Song 	return evt_num;
2480ab0e4800SYunlong Song 
2481ab0e4800SYunlong Song out_enomem:
2482ab0e4800SYunlong Song 	printf("FATAL: not enough memory to print %s\n", event_type_descriptors[PERF_TYPE_HW_CACHE]);
2483ab0e4800SYunlong Song 	if (evt_list)
2484ab0e4800SYunlong Song 		goto out_free;
2485ab0e4800SYunlong Song 	return evt_num;
2486668b8788SArnaldo Carvalho de Melo }
2487668b8788SArnaldo Carvalho de Melo 
24885e0861baSAndi Kleen static void print_tool_event(const char *name, const char *event_glob,
24895e0861baSAndi Kleen 			     bool name_only)
24905e0861baSAndi Kleen {
24915e0861baSAndi Kleen 	if (event_glob && !strglobmatch(name, event_glob))
24925e0861baSAndi Kleen 		return;
24935e0861baSAndi Kleen 	if (name_only)
24945e0861baSAndi Kleen 		printf("%s ", name);
24955e0861baSAndi Kleen 	else
24965e0861baSAndi Kleen 		printf("  %-50s [%s]\n", name, "Tool event");
24975e0861baSAndi Kleen 
24985e0861baSAndi Kleen }
24995e0861baSAndi Kleen 
25005e0861baSAndi Kleen void print_tool_events(const char *event_glob, bool name_only)
25015e0861baSAndi Kleen {
25025e0861baSAndi Kleen 	print_tool_event("duration_time", event_glob, name_only);
25035e0861baSAndi Kleen 	if (pager_in_use())
25045e0861baSAndi Kleen 		printf("\n");
25055e0861baSAndi Kleen }
25065e0861baSAndi Kleen 
2507705750f2SYunlong Song void print_symbol_events(const char *event_glob, unsigned type,
2508a3277d2dSFrederic Weisbecker 				struct event_symbol *syms, unsigned max,
2509a3277d2dSFrederic Weisbecker 				bool name_only)
251086470930SIngo Molnar {
2511ab0e4800SYunlong Song 	unsigned int i, evt_i = 0, evt_num = 0;
2512947b4ad1SIngo Molnar 	char name[MAX_NAME_LEN];
2513ab0e4800SYunlong Song 	char **evt_list = NULL;
2514ab0e4800SYunlong Song 	bool evt_num_known = false;
2515ab0e4800SYunlong Song 
2516ab0e4800SYunlong Song restart:
2517ab0e4800SYunlong Song 	if (evt_num_known) {
2518ab0e4800SYunlong Song 		evt_list = zalloc(sizeof(char *) * evt_num);
2519ab0e4800SYunlong Song 		if (!evt_list)
2520ab0e4800SYunlong Song 			goto out_enomem;
2521ab0e4800SYunlong Song 		syms -= max;
2522ab0e4800SYunlong Song 	}
252386470930SIngo Molnar 
25241dc12760SJiri Olsa 	for (i = 0; i < max; i++, syms++) {
2525668b8788SArnaldo Carvalho de Melo 
2526e37df6c7SArnaldo Carvalho de Melo 		if (event_glob != NULL && syms->symbol != NULL &&
2527668b8788SArnaldo Carvalho de Melo 		    !(strglobmatch(syms->symbol, event_glob) ||
2528668b8788SArnaldo Carvalho de Melo 		      (syms->alias && strglobmatch(syms->alias, event_glob))))
2529668b8788SArnaldo Carvalho de Melo 			continue;
253086470930SIngo Molnar 
2531b41f1cecSNamhyung Kim 		if (!is_event_supported(type, i))
2532b41f1cecSNamhyung Kim 			continue;
2533b41f1cecSNamhyung Kim 
2534ab0e4800SYunlong Song 		if (!evt_num_known) {
2535ab0e4800SYunlong Song 			evt_num++;
2536a3277d2dSFrederic Weisbecker 			continue;
2537a3277d2dSFrederic Weisbecker 		}
2538a3277d2dSFrederic Weisbecker 
2539ab0e4800SYunlong Song 		if (!name_only && strlen(syms->alias))
2540947b4ad1SIngo Molnar 			snprintf(name, MAX_NAME_LEN, "%s OR %s", syms->symbol, syms->alias);
254174d5b588SJaswinder Singh Rajput 		else
2542bd8d57fbSArnaldo Carvalho de Melo 			strlcpy(name, syms->symbol, MAX_NAME_LEN);
254386470930SIngo Molnar 
2544ab0e4800SYunlong Song 		evt_list[evt_i] = strdup(name);
2545ab0e4800SYunlong Song 		if (evt_list[evt_i] == NULL)
2546ab0e4800SYunlong Song 			goto out_enomem;
2547ab0e4800SYunlong Song 		evt_i++;
254886470930SIngo Molnar 	}
254986470930SIngo Molnar 
2550ab0e4800SYunlong Song 	if (!evt_num_known) {
2551ab0e4800SYunlong Song 		evt_num_known = true;
2552ab0e4800SYunlong Song 		goto restart;
2553ab0e4800SYunlong Song 	}
2554ab0e4800SYunlong Song 	qsort(evt_list, evt_num, sizeof(char *), cmp_string);
2555ab0e4800SYunlong Song 	evt_i = 0;
2556ab0e4800SYunlong Song 	while (evt_i < evt_num) {
2557ab0e4800SYunlong Song 		if (name_only) {
2558ab0e4800SYunlong Song 			printf("%s ", evt_list[evt_i++]);
2559ab0e4800SYunlong Song 			continue;
2560ab0e4800SYunlong Song 		}
2561ab0e4800SYunlong Song 		printf("  %-50s [%s]\n", evt_list[evt_i++], event_type_descriptors[type]);
2562ab0e4800SYunlong Song 	}
2563dfc431cbSArnaldo Carvalho de Melo 	if (evt_num && pager_in_use())
2564689d3018SMarti Raudsepp 		printf("\n");
2565ab0e4800SYunlong Song 
2566ab0e4800SYunlong Song out_free:
2567ab0e4800SYunlong Song 	evt_num = evt_i;
2568ab0e4800SYunlong Song 	for (evt_i = 0; evt_i < evt_num; evt_i++)
2569ab0e4800SYunlong Song 		zfree(&evt_list[evt_i]);
2570ab0e4800SYunlong Song 	zfree(&evt_list);
2571ab0e4800SYunlong Song 	return;
2572ab0e4800SYunlong Song 
2573ab0e4800SYunlong Song out_enomem:
2574ab0e4800SYunlong Song 	printf("FATAL: not enough memory to print %s\n", event_type_descriptors[type]);
2575ab0e4800SYunlong Song 	if (evt_list)
2576ab0e4800SYunlong Song 		goto out_free;
2577668b8788SArnaldo Carvalho de Melo }
25781dc12760SJiri Olsa 
25791dc12760SJiri Olsa /*
25801dc12760SJiri Olsa  * Print the help text for the event symbols:
25811dc12760SJiri Olsa  */
2582c8d6828aSSukadev Bhattiprolu void print_events(const char *event_glob, bool name_only, bool quiet_flag,
2583bf874fcfSAndi Kleen 			bool long_desc, bool details_flag)
25841dc12760SJiri Olsa {
25851dc12760SJiri Olsa 	print_symbol_events(event_glob, PERF_TYPE_HARDWARE,
2586a3277d2dSFrederic Weisbecker 			    event_symbols_hw, PERF_COUNT_HW_MAX, name_only);
25871dc12760SJiri Olsa 
25881dc12760SJiri Olsa 	print_symbol_events(event_glob, PERF_TYPE_SOFTWARE,
2589a3277d2dSFrederic Weisbecker 			    event_symbols_sw, PERF_COUNT_SW_MAX, name_only);
25905e0861baSAndi Kleen 	print_tool_events(event_glob, name_only);
25911dc12760SJiri Olsa 
2592a3277d2dSFrederic Weisbecker 	print_hwcache_events(event_glob, name_only);
259373c24cb8SJaswinder Singh Rajput 
2594bf874fcfSAndi Kleen 	print_pmu_events(event_glob, name_only, quiet_flag, long_desc,
2595bf874fcfSAndi Kleen 			details_flag);
2596dc098b35SAndi Kleen 
2597668b8788SArnaldo Carvalho de Melo 	if (event_glob != NULL)
2598668b8788SArnaldo Carvalho de Melo 		return;
259973c24cb8SJaswinder Singh Rajput 
2600a3277d2dSFrederic Weisbecker 	if (!name_only) {
2601947b4ad1SIngo Molnar 		printf("  %-50s [%s]\n",
26025f537a26SJiri Olsa 		       "rNNN",
26031cf4a063SArnaldo Carvalho de Melo 		       event_type_descriptors[PERF_TYPE_RAW]);
26045f537a26SJiri Olsa 		printf("  %-50s [%s]\n",
26055f537a26SJiri Olsa 		       "cpu/t1=v1[,t2=v2,t3 ...]/modifier",
26065f537a26SJiri Olsa 		       event_type_descriptors[PERF_TYPE_RAW]);
2607dfc431cbSArnaldo Carvalho de Melo 		if (pager_in_use())
2608dfc431cbSArnaldo Carvalho de Melo 			printf("   (see 'man perf-list' on how to encode it)\n\n");
260986470930SIngo Molnar 
2610947b4ad1SIngo Molnar 		printf("  %-50s [%s]\n",
26113741eb9fSJacob Shin 		       "mem:<addr>[/len][:access]",
261241bdcb23SLiming Wang 			event_type_descriptors[PERF_TYPE_BREAKPOINT]);
2613dfc431cbSArnaldo Carvalho de Melo 		if (pager_in_use())
26141b290d67SFrederic Weisbecker 			printf("\n");
2615a3277d2dSFrederic Weisbecker 	}
26161b290d67SFrederic Weisbecker 
2617a3277d2dSFrederic Weisbecker 	print_tracepoint_events(NULL, NULL, name_only);
261840218daeSMasami Hiramatsu 
261940218daeSMasami Hiramatsu 	print_sdt_events(NULL, NULL, name_only);
262071b0acceSAndi Kleen 
262133bbc571SJiri Olsa 	metricgroup__print(true, true, NULL, name_only, details_flag);
262286470930SIngo Molnar }
26238f707d84SJiri Olsa 
26246cee6cd3SArnaldo Carvalho de Melo int parse_events__is_hardcoded_term(struct parse_events_term *term)
26258f707d84SJiri Olsa {
262616fa7e82SJiri Olsa 	return term->type_term != PARSE_EVENTS__TERM_TYPE_USER;
26278f707d84SJiri Olsa }
26288f707d84SJiri Olsa 
262967b49b38SJiri Olsa static int new_term(struct parse_events_term **_term,
263067b49b38SJiri Olsa 		    struct parse_events_term *temp,
263167b49b38SJiri Olsa 		    char *str, u64 num)
26328f707d84SJiri Olsa {
26336cee6cd3SArnaldo Carvalho de Melo 	struct parse_events_term *term;
26348f707d84SJiri Olsa 
263567b49b38SJiri Olsa 	term = malloc(sizeof(*term));
26368f707d84SJiri Olsa 	if (!term)
26378f707d84SJiri Olsa 		return -ENOMEM;
26388f707d84SJiri Olsa 
263967b49b38SJiri Olsa 	*term = *temp;
26408f707d84SJiri Olsa 	INIT_LIST_HEAD(&term->list);
2641c2f1ceadSAndi Kleen 	term->weak = false;
26428f707d84SJiri Olsa 
264367b49b38SJiri Olsa 	switch (term->type_val) {
26448f707d84SJiri Olsa 	case PARSE_EVENTS__TERM_TYPE_NUM:
26458f707d84SJiri Olsa 		term->val.num = num;
26468f707d84SJiri Olsa 		break;
26478f707d84SJiri Olsa 	case PARSE_EVENTS__TERM_TYPE_STR:
26488f707d84SJiri Olsa 		term->val.str = str;
26498f707d84SJiri Olsa 		break;
26508f707d84SJiri Olsa 	default:
26514be8be6bSAdrian Hunter 		free(term);
26528f707d84SJiri Olsa 		return -EINVAL;
26538f707d84SJiri Olsa 	}
26548f707d84SJiri Olsa 
26558f707d84SJiri Olsa 	*_term = term;
26568f707d84SJiri Olsa 	return 0;
26578f707d84SJiri Olsa }
26588f707d84SJiri Olsa 
26596cee6cd3SArnaldo Carvalho de Melo int parse_events_term__num(struct parse_events_term **term,
2660cecf3a2eSJiri Olsa 			   int type_term, char *config, u64 num,
266199e7138eSJiri Olsa 			   bool no_value,
2662bb78ce7dSAdrian Hunter 			   void *loc_term_, void *loc_val_)
266316fa7e82SJiri Olsa {
2664bb78ce7dSAdrian Hunter 	YYLTYPE *loc_term = loc_term_;
2665bb78ce7dSAdrian Hunter 	YYLTYPE *loc_val = loc_val_;
2666bb78ce7dSAdrian Hunter 
266767b49b38SJiri Olsa 	struct parse_events_term temp = {
266867b49b38SJiri Olsa 		.type_val  = PARSE_EVENTS__TERM_TYPE_NUM,
266967b49b38SJiri Olsa 		.type_term = type_term,
267067b49b38SJiri Olsa 		.config    = config,
267199e7138eSJiri Olsa 		.no_value  = no_value,
267267b49b38SJiri Olsa 		.err_term  = loc_term ? loc_term->first_column : 0,
267367b49b38SJiri Olsa 		.err_val   = loc_val  ? loc_val->first_column  : 0,
267467b49b38SJiri Olsa 	};
267567b49b38SJiri Olsa 
267667b49b38SJiri Olsa 	return new_term(term, &temp, NULL, num);
267716fa7e82SJiri Olsa }
267816fa7e82SJiri Olsa 
26796cee6cd3SArnaldo Carvalho de Melo int parse_events_term__str(struct parse_events_term **term,
2680cecf3a2eSJiri Olsa 			   int type_term, char *config, char *str,
2681bb78ce7dSAdrian Hunter 			   void *loc_term_, void *loc_val_)
268216fa7e82SJiri Olsa {
2683bb78ce7dSAdrian Hunter 	YYLTYPE *loc_term = loc_term_;
2684bb78ce7dSAdrian Hunter 	YYLTYPE *loc_val = loc_val_;
2685bb78ce7dSAdrian Hunter 
268667b49b38SJiri Olsa 	struct parse_events_term temp = {
268767b49b38SJiri Olsa 		.type_val  = PARSE_EVENTS__TERM_TYPE_STR,
268867b49b38SJiri Olsa 		.type_term = type_term,
268967b49b38SJiri Olsa 		.config    = config,
269067b49b38SJiri Olsa 		.err_term  = loc_term ? loc_term->first_column : 0,
269167b49b38SJiri Olsa 		.err_val   = loc_val  ? loc_val->first_column  : 0,
269267b49b38SJiri Olsa 	};
269367b49b38SJiri Olsa 
269467b49b38SJiri Olsa 	return new_term(term, &temp, str, 0);
269516fa7e82SJiri Olsa }
269616fa7e82SJiri Olsa 
26976cee6cd3SArnaldo Carvalho de Melo int parse_events_term__sym_hw(struct parse_events_term **term,
26981d33d6dcSJiri Olsa 			      char *config, unsigned idx)
26991d33d6dcSJiri Olsa {
27001d33d6dcSJiri Olsa 	struct event_symbol *sym;
270167b49b38SJiri Olsa 	struct parse_events_term temp = {
270267b49b38SJiri Olsa 		.type_val  = PARSE_EVENTS__TERM_TYPE_STR,
270367b49b38SJiri Olsa 		.type_term = PARSE_EVENTS__TERM_TYPE_USER,
270467b49b38SJiri Olsa 		.config    = config ?: (char *) "event",
270567b49b38SJiri Olsa 	};
27061d33d6dcSJiri Olsa 
27071d33d6dcSJiri Olsa 	BUG_ON(idx >= PERF_COUNT_HW_MAX);
27081d33d6dcSJiri Olsa 	sym = &event_symbols_hw[idx];
27091d33d6dcSJiri Olsa 
271067b49b38SJiri Olsa 	return new_term(term, &temp, (char *) sym->symbol, 0);
27111d33d6dcSJiri Olsa }
27121d33d6dcSJiri Olsa 
27136cee6cd3SArnaldo Carvalho de Melo int parse_events_term__clone(struct parse_events_term **new,
27146cee6cd3SArnaldo Carvalho de Melo 			     struct parse_events_term *term)
2715a6146d50SZheng Yan {
271667b49b38SJiri Olsa 	struct parse_events_term temp = {
271767b49b38SJiri Olsa 		.type_val  = term->type_val,
271867b49b38SJiri Olsa 		.type_term = term->type_term,
271967b49b38SJiri Olsa 		.config    = term->config,
272067b49b38SJiri Olsa 		.err_term  = term->err_term,
272167b49b38SJiri Olsa 		.err_val   = term->err_val,
272267b49b38SJiri Olsa 	};
272367b49b38SJiri Olsa 
272467b49b38SJiri Olsa 	return new_term(new, &temp, term->val.str, term->val.num);
2725a6146d50SZheng Yan }
2726a6146d50SZheng Yan 
27278255718fSAndi Kleen int parse_events_copy_term_list(struct list_head *old,
27288255718fSAndi Kleen 				 struct list_head **new)
27298255718fSAndi Kleen {
27308255718fSAndi Kleen 	struct parse_events_term *term, *n;
27318255718fSAndi Kleen 	int ret;
27328255718fSAndi Kleen 
27338255718fSAndi Kleen 	if (!old) {
27348255718fSAndi Kleen 		*new = NULL;
27358255718fSAndi Kleen 		return 0;
27368255718fSAndi Kleen 	}
27378255718fSAndi Kleen 
27388255718fSAndi Kleen 	*new = malloc(sizeof(struct list_head));
27398255718fSAndi Kleen 	if (!*new)
27408255718fSAndi Kleen 		return -ENOMEM;
27418255718fSAndi Kleen 	INIT_LIST_HEAD(*new);
27428255718fSAndi Kleen 
27438255718fSAndi Kleen 	list_for_each_entry (term, old, list) {
27448255718fSAndi Kleen 		ret = parse_events_term__clone(&n, term);
27458255718fSAndi Kleen 		if (ret)
27468255718fSAndi Kleen 			return ret;
27478255718fSAndi Kleen 		list_add_tail(&n->list, *new);
27488255718fSAndi Kleen 	}
27498255718fSAndi Kleen 	return 0;
27508255718fSAndi Kleen }
27518255718fSAndi Kleen 
2752fc0a2c1dSArnaldo Carvalho de Melo void parse_events_terms__purge(struct list_head *terms)
27538f707d84SJiri Olsa {
27546cee6cd3SArnaldo Carvalho de Melo 	struct parse_events_term *term, *h;
27558f707d84SJiri Olsa 
2756a8adfcebSWang Nan 	list_for_each_entry_safe(term, h, terms, list) {
27572d055bf2SWang Nan 		if (term->array.nr_ranges)
2758360e071bSTaeung Song 			zfree(&term->array.ranges);
2759a8adfcebSWang Nan 		list_del_init(&term->list);
27608f707d84SJiri Olsa 		free(term);
27618f707d84SJiri Olsa 	}
2762a8adfcebSWang Nan }
2763b39b8393SJiri Olsa 
27642146afc6SArnaldo Carvalho de Melo void parse_events_terms__delete(struct list_head *terms)
2765fc0a2c1dSArnaldo Carvalho de Melo {
27662146afc6SArnaldo Carvalho de Melo 	if (!terms)
27672146afc6SArnaldo Carvalho de Melo 		return;
2768fc0a2c1dSArnaldo Carvalho de Melo 	parse_events_terms__purge(terms);
2769d20a5f2bSWang Nan 	free(terms);
2770fc0a2c1dSArnaldo Carvalho de Melo }
2771fc0a2c1dSArnaldo Carvalho de Melo 
27722d055bf2SWang Nan void parse_events__clear_array(struct parse_events_array *a)
27732d055bf2SWang Nan {
2774360e071bSTaeung Song 	zfree(&a->ranges);
27752d055bf2SWang Nan }
27762d055bf2SWang Nan 
27775d9cdc11SArnaldo Carvalho de Melo void parse_events_evlist_error(struct parse_events_state *parse_state,
2778b39b8393SJiri Olsa 			       int idx, const char *str)
2779b39b8393SJiri Olsa {
27805d9cdc11SArnaldo Carvalho de Melo 	struct parse_events_error *err = parse_state->error;
2781b39b8393SJiri Olsa 
2782a6ced2beSAdrian Hunter 	if (!err)
2783a6ced2beSAdrian Hunter 		return;
2784b39b8393SJiri Olsa 	err->idx = idx;
2785b39b8393SJiri Olsa 	err->str = strdup(str);
2786b39b8393SJiri Olsa 	WARN_ONCE(!err->str, "WARNING: failed to allocate error string");
2787b39b8393SJiri Olsa }
2788ffeb883eSHe Kuang 
278917cb5f84SWang Nan static void config_terms_list(char *buf, size_t buf_sz)
279017cb5f84SWang Nan {
279117cb5f84SWang Nan 	int i;
279217cb5f84SWang Nan 	bool first = true;
279317cb5f84SWang Nan 
279417cb5f84SWang Nan 	buf[0] = '\0';
279517cb5f84SWang Nan 	for (i = 0; i < __PARSE_EVENTS__TERM_TYPE_NR; i++) {
279617cb5f84SWang Nan 		const char *name = config_term_names[i];
279717cb5f84SWang Nan 
27981669e509SWang Nan 		if (!config_term_avail(i, NULL))
27991669e509SWang Nan 			continue;
280017cb5f84SWang Nan 		if (!name)
280117cb5f84SWang Nan 			continue;
280217cb5f84SWang Nan 		if (name[0] == '<')
280317cb5f84SWang Nan 			continue;
280417cb5f84SWang Nan 
280517cb5f84SWang Nan 		if (strlen(buf) + strlen(name) + 2 >= buf_sz)
280617cb5f84SWang Nan 			return;
280717cb5f84SWang Nan 
280817cb5f84SWang Nan 		if (!first)
280917cb5f84SWang Nan 			strcat(buf, ",");
281017cb5f84SWang Nan 		else
281117cb5f84SWang Nan 			first = false;
281217cb5f84SWang Nan 		strcat(buf, name);
281317cb5f84SWang Nan 	}
281417cb5f84SWang Nan }
281517cb5f84SWang Nan 
2816ffeb883eSHe Kuang /*
2817ffeb883eSHe Kuang  * Return string contains valid config terms of an event.
2818ffeb883eSHe Kuang  * @additional_terms: For terms such as PMU sysfs terms.
2819ffeb883eSHe Kuang  */
2820ffeb883eSHe Kuang char *parse_events_formats_error_string(char *additional_terms)
2821ffeb883eSHe Kuang {
2822ffeb883eSHe Kuang 	char *str;
2823626a6b78SWang Nan 	/* "no-overwrite" is the longest name */
282417cb5f84SWang Nan 	char static_terms[__PARSE_EVENTS__TERM_TYPE_NR *
2825626a6b78SWang Nan 			  (sizeof("no-overwrite") - 1)];
2826ffeb883eSHe Kuang 
282717cb5f84SWang Nan 	config_terms_list(static_terms, sizeof(static_terms));
2828ffeb883eSHe Kuang 	/* valid terms */
2829ffeb883eSHe Kuang 	if (additional_terms) {
283026dee028SWang Nan 		if (asprintf(&str, "valid terms: %s,%s",
283126dee028SWang Nan 			     additional_terms, static_terms) < 0)
2832ffeb883eSHe Kuang 			goto fail;
2833ffeb883eSHe Kuang 	} else {
283426dee028SWang Nan 		if (asprintf(&str, "valid terms: %s", static_terms) < 0)
2835ffeb883eSHe Kuang 			goto fail;
2836ffeb883eSHe Kuang 	}
2837ffeb883eSHe Kuang 	return str;
2838ffeb883eSHe Kuang 
2839ffeb883eSHe Kuang fail:
2840ffeb883eSHe Kuang 	return NULL;
2841ffeb883eSHe Kuang }
2842