xref: /linux/tools/perf/util/parse-events.c (revision 71b0acce78d12e99eeda6fd6642ba89cc2b2b49c)
1d2709c7cSDavid Howells #include <linux/hw_breakpoint.h>
28dd2a131SJiri Olsa #include <linux/err.h>
376b31a29SArnaldo Carvalho de Melo #include <dirent.h>
4a43783aeSArnaldo Carvalho de Melo #include <errno.h>
586a5e0c2SArnaldo Carvalho de Melo #include <sys/ioctl.h>
6391e4206SArnaldo Carvalho de Melo #include <sys/param.h>
7b0742e90SArnaldo Carvalho de Melo #include "term.h"
86b58e7f1SUlrich Drepper #include "../perf.h"
9361c99a6SArnaldo Carvalho de Melo #include "evlist.h"
1069aad6f1SArnaldo Carvalho de Melo #include "evsel.h"
114b6ab94eSJosh Poimboeuf #include <subcmd/parse-options.h>
1286470930SIngo Molnar #include "parse-events.h"
134b6ab94eSJosh Poimboeuf #include <subcmd/exec-cmd.h>
14a067558eSArnaldo Carvalho de Melo #include "string2.h"
158ec20b17SArnaldo Carvalho de Melo #include "strlist.h"
165aab621bSArnaldo Carvalho de Melo #include "symbol.h"
175beeded1SJason Baron #include "cache.h"
188755a8f2SArjan van de Ven #include "header.h"
1984c86ca1SWang Nan #include "bpf-loader.h"
206e81c74cSMasami Hiramatsu #include "debug.h"
21592d5a6bSJiri Olsa #include <api/fs/tracing_path.h>
22ac20de6fSZheng Yan #include "parse-events-bison.h"
2390e2b22dSJiri Olsa #define YY_EXTRA_TYPE int
2489812fc8SJiri Olsa #include "parse-events-flex.h"
255f537a26SJiri Olsa #include "pmu.h"
26b41f1cecSNamhyung Kim #include "thread_map.h"
27f30a79b0SJiri Olsa #include "cpumap.h"
2840218daeSMasami Hiramatsu #include "probe-file.h"
29b39b8393SJiri Olsa #include "asm/bug.h"
30ac12f676SAndi Kleen #include "util/parse-branch-options.h"
31*71b0acceSAndi Kleen #include "metricgroup.h"
3289812fc8SJiri Olsa 
3389812fc8SJiri Olsa #define MAX_NAME_LEN 100
3486470930SIngo Molnar 
3582ba1f2fSJiri Olsa #ifdef PARSER_DEBUG
3682ba1f2fSJiri Olsa extern int parse_events_debug;
3782ba1f2fSJiri Olsa #endif
385d9cdc11SArnaldo Carvalho de Melo int parse_events_parse(void *parse_state, void *scanner);
39e637d177SHe Kuang static int get_config_terms(struct list_head *head_config,
40e637d177SHe Kuang 			    struct list_head *head_terms __maybe_unused);
41bcd3279fSFrederic Weisbecker 
42dcb4e102SKan Liang static struct perf_pmu_event_symbol *perf_pmu_events_list;
43dcb4e102SKan Liang /*
44dcb4e102SKan Liang  * The variable indicates the number of supported pmu event symbols.
45dcb4e102SKan Liang  * 0 means not initialized and ready to init
46dcb4e102SKan Liang  * -1 means failed to init, don't try anymore
47dcb4e102SKan Liang  * >0 is the number of supported pmu event symbols
48dcb4e102SKan Liang  */
49dcb4e102SKan Liang static int perf_pmu_events_list_num;
50dcb4e102SKan Liang 
51705750f2SYunlong Song struct event_symbol event_symbols_hw[PERF_COUNT_HW_MAX] = {
521dc12760SJiri Olsa 	[PERF_COUNT_HW_CPU_CYCLES] = {
531dc12760SJiri Olsa 		.symbol = "cpu-cycles",
541dc12760SJiri Olsa 		.alias  = "cycles",
551dc12760SJiri Olsa 	},
561dc12760SJiri Olsa 	[PERF_COUNT_HW_INSTRUCTIONS] = {
571dc12760SJiri Olsa 		.symbol = "instructions",
581dc12760SJiri Olsa 		.alias  = "",
591dc12760SJiri Olsa 	},
601dc12760SJiri Olsa 	[PERF_COUNT_HW_CACHE_REFERENCES] = {
611dc12760SJiri Olsa 		.symbol = "cache-references",
621dc12760SJiri Olsa 		.alias  = "",
631dc12760SJiri Olsa 	},
641dc12760SJiri Olsa 	[PERF_COUNT_HW_CACHE_MISSES] = {
651dc12760SJiri Olsa 		.symbol = "cache-misses",
661dc12760SJiri Olsa 		.alias  = "",
671dc12760SJiri Olsa 	},
681dc12760SJiri Olsa 	[PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = {
691dc12760SJiri Olsa 		.symbol = "branch-instructions",
701dc12760SJiri Olsa 		.alias  = "branches",
711dc12760SJiri Olsa 	},
721dc12760SJiri Olsa 	[PERF_COUNT_HW_BRANCH_MISSES] = {
731dc12760SJiri Olsa 		.symbol = "branch-misses",
741dc12760SJiri Olsa 		.alias  = "",
751dc12760SJiri Olsa 	},
761dc12760SJiri Olsa 	[PERF_COUNT_HW_BUS_CYCLES] = {
771dc12760SJiri Olsa 		.symbol = "bus-cycles",
781dc12760SJiri Olsa 		.alias  = "",
791dc12760SJiri Olsa 	},
801dc12760SJiri Olsa 	[PERF_COUNT_HW_STALLED_CYCLES_FRONTEND] = {
811dc12760SJiri Olsa 		.symbol = "stalled-cycles-frontend",
821dc12760SJiri Olsa 		.alias  = "idle-cycles-frontend",
831dc12760SJiri Olsa 	},
841dc12760SJiri Olsa 	[PERF_COUNT_HW_STALLED_CYCLES_BACKEND] = {
851dc12760SJiri Olsa 		.symbol = "stalled-cycles-backend",
861dc12760SJiri Olsa 		.alias  = "idle-cycles-backend",
871dc12760SJiri Olsa 	},
881dc12760SJiri Olsa 	[PERF_COUNT_HW_REF_CPU_CYCLES] = {
891dc12760SJiri Olsa 		.symbol = "ref-cycles",
901dc12760SJiri Olsa 		.alias  = "",
911dc12760SJiri Olsa 	},
921dc12760SJiri Olsa };
9386470930SIngo Molnar 
94705750f2SYunlong Song struct event_symbol event_symbols_sw[PERF_COUNT_SW_MAX] = {
951dc12760SJiri Olsa 	[PERF_COUNT_SW_CPU_CLOCK] = {
961dc12760SJiri Olsa 		.symbol = "cpu-clock",
971dc12760SJiri Olsa 		.alias  = "",
981dc12760SJiri Olsa 	},
991dc12760SJiri Olsa 	[PERF_COUNT_SW_TASK_CLOCK] = {
1001dc12760SJiri Olsa 		.symbol = "task-clock",
1011dc12760SJiri Olsa 		.alias  = "",
1021dc12760SJiri Olsa 	},
1031dc12760SJiri Olsa 	[PERF_COUNT_SW_PAGE_FAULTS] = {
1041dc12760SJiri Olsa 		.symbol = "page-faults",
1051dc12760SJiri Olsa 		.alias  = "faults",
1061dc12760SJiri Olsa 	},
1071dc12760SJiri Olsa 	[PERF_COUNT_SW_CONTEXT_SWITCHES] = {
1081dc12760SJiri Olsa 		.symbol = "context-switches",
1091dc12760SJiri Olsa 		.alias  = "cs",
1101dc12760SJiri Olsa 	},
1111dc12760SJiri Olsa 	[PERF_COUNT_SW_CPU_MIGRATIONS] = {
1121dc12760SJiri Olsa 		.symbol = "cpu-migrations",
1131dc12760SJiri Olsa 		.alias  = "migrations",
1141dc12760SJiri Olsa 	},
1151dc12760SJiri Olsa 	[PERF_COUNT_SW_PAGE_FAULTS_MIN] = {
1161dc12760SJiri Olsa 		.symbol = "minor-faults",
1171dc12760SJiri Olsa 		.alias  = "",
1181dc12760SJiri Olsa 	},
1191dc12760SJiri Olsa 	[PERF_COUNT_SW_PAGE_FAULTS_MAJ] = {
1201dc12760SJiri Olsa 		.symbol = "major-faults",
1211dc12760SJiri Olsa 		.alias  = "",
1221dc12760SJiri Olsa 	},
1231dc12760SJiri Olsa 	[PERF_COUNT_SW_ALIGNMENT_FAULTS] = {
1241dc12760SJiri Olsa 		.symbol = "alignment-faults",
1251dc12760SJiri Olsa 		.alias  = "",
1261dc12760SJiri Olsa 	},
1271dc12760SJiri Olsa 	[PERF_COUNT_SW_EMULATION_FAULTS] = {
1281dc12760SJiri Olsa 		.symbol = "emulation-faults",
1291dc12760SJiri Olsa 		.alias  = "",
1301dc12760SJiri Olsa 	},
131d22d1a2aSAdrian Hunter 	[PERF_COUNT_SW_DUMMY] = {
132d22d1a2aSAdrian Hunter 		.symbol = "dummy",
133d22d1a2aSAdrian Hunter 		.alias  = "",
134d22d1a2aSAdrian Hunter 	},
135bae9cc41SArnaldo Carvalho de Melo 	[PERF_COUNT_SW_BPF_OUTPUT] = {
136bae9cc41SArnaldo Carvalho de Melo 		.symbol = "bpf-output",
137bae9cc41SArnaldo Carvalho de Melo 		.alias  = "",
138bae9cc41SArnaldo Carvalho de Melo 	},
13986470930SIngo Molnar };
14086470930SIngo Molnar 
141cdd6c482SIngo Molnar #define __PERF_EVENT_FIELD(config, name) \
142cdd6c482SIngo Molnar 	((config & PERF_EVENT_##name##_MASK) >> PERF_EVENT_##name##_SHIFT)
14386470930SIngo Molnar 
144cdd6c482SIngo Molnar #define PERF_EVENT_RAW(config)		__PERF_EVENT_FIELD(config, RAW)
145cdd6c482SIngo Molnar #define PERF_EVENT_CONFIG(config)	__PERF_EVENT_FIELD(config, CONFIG)
146cdd6c482SIngo Molnar #define PERF_EVENT_TYPE(config)		__PERF_EVENT_FIELD(config, TYPE)
147cdd6c482SIngo Molnar #define PERF_EVENT_ID(config)		__PERF_EVENT_FIELD(config, EVENT)
14886470930SIngo Molnar 
14922a9f41bSArnaldo Carvalho de Melo #define for_each_subsystem(sys_dir, sys_dirent)			\
15022a9f41bSArnaldo Carvalho de Melo 	while ((sys_dirent = readdir(sys_dir)) != NULL)		\
15122a9f41bSArnaldo Carvalho de Melo 		if (sys_dirent->d_type == DT_DIR &&		\
15222a9f41bSArnaldo Carvalho de Melo 		    (strcmp(sys_dirent->d_name, ".")) &&	\
15322a9f41bSArnaldo Carvalho de Melo 		    (strcmp(sys_dirent->d_name, "..")))
154f6bdafefSJason Baron 
155ae07b63fSPeter Zijlstra static int tp_event_has_id(struct dirent *sys_dir, struct dirent *evt_dir)
156ae07b63fSPeter Zijlstra {
157ae07b63fSPeter Zijlstra 	char evt_path[MAXPATHLEN];
158ae07b63fSPeter Zijlstra 	int fd;
159ae07b63fSPeter Zijlstra 
160ebf294bfSArnaldo Carvalho de Melo 	snprintf(evt_path, MAXPATHLEN, "%s/%s/%s/id", tracing_events_path,
161ae07b63fSPeter Zijlstra 			sys_dir->d_name, evt_dir->d_name);
162ae07b63fSPeter Zijlstra 	fd = open(evt_path, O_RDONLY);
163ae07b63fSPeter Zijlstra 	if (fd < 0)
164ae07b63fSPeter Zijlstra 		return -EINVAL;
165ae07b63fSPeter Zijlstra 	close(fd);
166ae07b63fSPeter Zijlstra 
167ae07b63fSPeter Zijlstra 	return 0;
168ae07b63fSPeter Zijlstra }
169ae07b63fSPeter Zijlstra 
17022a9f41bSArnaldo Carvalho de Melo #define for_each_event(sys_dirent, evt_dir, evt_dirent)		\
17122a9f41bSArnaldo Carvalho de Melo 	while ((evt_dirent = readdir(evt_dir)) != NULL)		\
17222a9f41bSArnaldo Carvalho de Melo 		if (evt_dirent->d_type == DT_DIR &&		\
17322a9f41bSArnaldo Carvalho de Melo 		    (strcmp(evt_dirent->d_name, ".")) &&	\
17422a9f41bSArnaldo Carvalho de Melo 		    (strcmp(evt_dirent->d_name, "..")) &&	\
17522a9f41bSArnaldo Carvalho de Melo 		    (!tp_event_has_id(sys_dirent, evt_dirent)))
176f6bdafefSJason Baron 
177270bbbe8SLi Zefan #define MAX_EVENT_LENGTH 512
178f6bdafefSJason Baron 
179f6bdafefSJason Baron 
1801ef2ed10SFrederic Weisbecker struct tracepoint_path *tracepoint_id_to_path(u64 config)
181f6bdafefSJason Baron {
1821ef2ed10SFrederic Weisbecker 	struct tracepoint_path *path = NULL;
183f6bdafefSJason Baron 	DIR *sys_dir, *evt_dir;
18422a9f41bSArnaldo Carvalho de Melo 	struct dirent *sys_dirent, *evt_dirent;
1858aa8a7c8SStephane Eranian 	char id_buf[24];
186725b1368SEric Dumazet 	int fd;
187f6bdafefSJason Baron 	u64 id;
188f6bdafefSJason Baron 	char evt_path[MAXPATHLEN];
189725b1368SEric Dumazet 	char dir_path[MAXPATHLEN];
190f6bdafefSJason Baron 
191ebf294bfSArnaldo Carvalho de Melo 	sys_dir = opendir(tracing_events_path);
192f6bdafefSJason Baron 	if (!sys_dir)
193725b1368SEric Dumazet 		return NULL;
194f6bdafefSJason Baron 
19522a9f41bSArnaldo Carvalho de Melo 	for_each_subsystem(sys_dir, sys_dirent) {
196725b1368SEric Dumazet 
197ebf294bfSArnaldo Carvalho de Melo 		snprintf(dir_path, MAXPATHLEN, "%s/%s", tracing_events_path,
19822a9f41bSArnaldo Carvalho de Melo 			 sys_dirent->d_name);
199725b1368SEric Dumazet 		evt_dir = opendir(dir_path);
200725b1368SEric Dumazet 		if (!evt_dir)
2016b58e7f1SUlrich Drepper 			continue;
202725b1368SEric Dumazet 
20322a9f41bSArnaldo Carvalho de Melo 		for_each_event(sys_dirent, evt_dir, evt_dirent) {
204725b1368SEric Dumazet 
205725b1368SEric Dumazet 			snprintf(evt_path, MAXPATHLEN, "%s/%s/id", dir_path,
20622a9f41bSArnaldo Carvalho de Melo 				 evt_dirent->d_name);
207725b1368SEric Dumazet 			fd = open(evt_path, O_RDONLY);
208f6bdafefSJason Baron 			if (fd < 0)
209f6bdafefSJason Baron 				continue;
210f6bdafefSJason Baron 			if (read(fd, id_buf, sizeof(id_buf)) < 0) {
211f6bdafefSJason Baron 				close(fd);
212f6bdafefSJason Baron 				continue;
213f6bdafefSJason Baron 			}
214f6bdafefSJason Baron 			close(fd);
215f6bdafefSJason Baron 			id = atoll(id_buf);
216f6bdafefSJason Baron 			if (id == config) {
217f6bdafefSJason Baron 				closedir(evt_dir);
218f6bdafefSJason Baron 				closedir(sys_dir);
21959b4caebSJulia Lawall 				path = zalloc(sizeof(*path));
2205aa365f2STaeung Song 				if (!path)
2215aa365f2STaeung Song 					return NULL;
2221ef2ed10SFrederic Weisbecker 				path->system = malloc(MAX_EVENT_LENGTH);
2231ef2ed10SFrederic Weisbecker 				if (!path->system) {
2241ef2ed10SFrederic Weisbecker 					free(path);
2251ef2ed10SFrederic Weisbecker 					return NULL;
2261ef2ed10SFrederic Weisbecker 				}
2271ef2ed10SFrederic Weisbecker 				path->name = malloc(MAX_EVENT_LENGTH);
2281ef2ed10SFrederic Weisbecker 				if (!path->name) {
22974cf249dSArnaldo Carvalho de Melo 					zfree(&path->system);
2301ef2ed10SFrederic Weisbecker 					free(path);
2311ef2ed10SFrederic Weisbecker 					return NULL;
2321ef2ed10SFrederic Weisbecker 				}
23322a9f41bSArnaldo Carvalho de Melo 				strncpy(path->system, sys_dirent->d_name,
2341ef2ed10SFrederic Weisbecker 					MAX_EVENT_LENGTH);
23522a9f41bSArnaldo Carvalho de Melo 				strncpy(path->name, evt_dirent->d_name,
2361ef2ed10SFrederic Weisbecker 					MAX_EVENT_LENGTH);
2371ef2ed10SFrederic Weisbecker 				return path;
238f6bdafefSJason Baron 			}
239f6bdafefSJason Baron 		}
240f6bdafefSJason Baron 		closedir(evt_dir);
241f6bdafefSJason Baron 	}
242f6bdafefSJason Baron 
243f6bdafefSJason Baron 	closedir(sys_dir);
2441ef2ed10SFrederic Weisbecker 	return NULL;
2451ef2ed10SFrederic Weisbecker }
2461ef2ed10SFrederic Weisbecker 
247e7c93f09SNamhyung Kim struct tracepoint_path *tracepoint_name_to_path(const char *name)
248e7c93f09SNamhyung Kim {
249e7c93f09SNamhyung Kim 	struct tracepoint_path *path = zalloc(sizeof(*path));
250e7c93f09SNamhyung Kim 	char *str = strchr(name, ':');
251e7c93f09SNamhyung Kim 
252e7c93f09SNamhyung Kim 	if (path == NULL || str == NULL) {
253e7c93f09SNamhyung Kim 		free(path);
254e7c93f09SNamhyung Kim 		return NULL;
255e7c93f09SNamhyung Kim 	}
256e7c93f09SNamhyung Kim 
257e7c93f09SNamhyung Kim 	path->system = strndup(name, str - name);
258e7c93f09SNamhyung Kim 	path->name = strdup(str+1);
259e7c93f09SNamhyung Kim 
260e7c93f09SNamhyung Kim 	if (path->system == NULL || path->name == NULL) {
26174cf249dSArnaldo Carvalho de Melo 		zfree(&path->system);
26274cf249dSArnaldo Carvalho de Melo 		zfree(&path->name);
263506fde11STaeung Song 		zfree(&path);
264e7c93f09SNamhyung Kim 	}
265e7c93f09SNamhyung Kim 
266e7c93f09SNamhyung Kim 	return path;
267e7c93f09SNamhyung Kim }
268e7c93f09SNamhyung Kim 
2691424dc96SDavid Ahern const char *event_type(int type)
2701424dc96SDavid Ahern {
2711424dc96SDavid Ahern 	switch (type) {
2721424dc96SDavid Ahern 	case PERF_TYPE_HARDWARE:
2731424dc96SDavid Ahern 		return "hardware";
2741424dc96SDavid Ahern 
2751424dc96SDavid Ahern 	case PERF_TYPE_SOFTWARE:
2761424dc96SDavid Ahern 		return "software";
2771424dc96SDavid Ahern 
2781424dc96SDavid Ahern 	case PERF_TYPE_TRACEPOINT:
2791424dc96SDavid Ahern 		return "tracepoint";
2801424dc96SDavid Ahern 
2811424dc96SDavid Ahern 	case PERF_TYPE_HW_CACHE:
2821424dc96SDavid Ahern 		return "hardware-cache";
2831424dc96SDavid Ahern 
2841424dc96SDavid Ahern 	default:
2851424dc96SDavid Ahern 		break;
2861424dc96SDavid Ahern 	}
2871424dc96SDavid Ahern 
2881424dc96SDavid Ahern 	return "unknown";
2891424dc96SDavid Ahern }
2901424dc96SDavid Ahern 
291e814fdddSWang Nan static int parse_events__is_name_term(struct parse_events_term *term)
292e814fdddSWang Nan {
293e814fdddSWang Nan 	return term->type_term == PARSE_EVENTS__TERM_TYPE_NAME;
294e814fdddSWang Nan }
2957ae92e74SYan, Zheng 
296e814fdddSWang Nan static char *get_config_name(struct list_head *head_terms)
297e814fdddSWang Nan {
298e814fdddSWang Nan 	struct parse_events_term *term;
299e814fdddSWang Nan 
300e814fdddSWang Nan 	if (!head_terms)
301e814fdddSWang Nan 		return NULL;
302e814fdddSWang Nan 
303e814fdddSWang Nan 	list_for_each_entry(term, head_terms, list)
304e814fdddSWang Nan 		if (parse_events__is_name_term(term))
305e814fdddSWang Nan 			return term->val.str;
306e814fdddSWang Nan 
307e814fdddSWang Nan 	return NULL;
308e814fdddSWang Nan }
3097ae92e74SYan, Zheng 
310410136f5SStephane Eranian static struct perf_evsel *
311410136f5SStephane Eranian __add_event(struct list_head *list, int *idx,
3127ae92e74SYan, Zheng 	    struct perf_event_attr *attr,
313930a2e29SJiri Olsa 	    char *name, struct cpu_map *cpus,
31463ce8449SArnaldo Carvalho de Melo 	    struct list_head *config_terms, bool auto_merge_stats)
31589812fc8SJiri Olsa {
31689812fc8SJiri Olsa 	struct perf_evsel *evsel;
31789812fc8SJiri Olsa 
31889812fc8SJiri Olsa 	event_attr_init(attr);
31989812fc8SJiri Olsa 
32075fc5ae5STaeung Song 	evsel = perf_evsel__new_idx(attr, *idx);
321c5cd8ac0SDavid Ahern 	if (!evsel)
322410136f5SStephane Eranian 		return NULL;
32389812fc8SJiri Olsa 
32475fc5ae5STaeung Song 	(*idx)++;
325f30a79b0SJiri Olsa 	evsel->cpus        = cpu_map__get(cpus);
326fce4d296SAdrian Hunter 	evsel->own_cpus    = cpu_map__get(cpus);
327e3ba76deSJiri Olsa 	evsel->system_wide = !!cpus;
32863ce8449SArnaldo Carvalho de Melo 	evsel->auto_merge_stats = auto_merge_stats;
329f30a79b0SJiri Olsa 
3309db1763cSArnaldo Carvalho de Melo 	if (name)
33189812fc8SJiri Olsa 		evsel->name = strdup(name);
332930a2e29SJiri Olsa 
333930a2e29SJiri Olsa 	if (config_terms)
334930a2e29SJiri Olsa 		list_splice(config_terms, &evsel->config_terms);
335930a2e29SJiri Olsa 
336b847cbdcSJiri Olsa 	list_add_tail(&evsel->node, list);
337410136f5SStephane Eranian 	return evsel;
33889812fc8SJiri Olsa }
33989812fc8SJiri Olsa 
340c5cd8ac0SDavid Ahern static int add_event(struct list_head *list, int *idx,
341930a2e29SJiri Olsa 		     struct perf_event_attr *attr, char *name,
342930a2e29SJiri Olsa 		     struct list_head *config_terms)
3437ae92e74SYan, Zheng {
34463ce8449SArnaldo Carvalho de Melo 	return __add_event(list, idx, attr, name, NULL, config_terms, false) ? 0 : -ENOMEM;
3457ae92e74SYan, Zheng }
3467ae92e74SYan, Zheng 
3470b668bc9SArnaldo Carvalho de Melo static int parse_aliases(char *str, const char *names[][PERF_EVSEL__MAX_ALIASES], int size)
34886470930SIngo Molnar {
34986470930SIngo Molnar 	int i, j;
35061c45981SPaul Mackerras 	int n, longest = -1;
35186470930SIngo Molnar 
35286470930SIngo Molnar 	for (i = 0; i < size; i++) {
3530b668bc9SArnaldo Carvalho de Melo 		for (j = 0; j < PERF_EVSEL__MAX_ALIASES && names[i][j]; j++) {
35461c45981SPaul Mackerras 			n = strlen(names[i][j]);
35589812fc8SJiri Olsa 			if (n > longest && !strncasecmp(str, names[i][j], n))
35661c45981SPaul Mackerras 				longest = n;
35761c45981SPaul Mackerras 		}
35889812fc8SJiri Olsa 		if (longest > 0)
35986470930SIngo Molnar 			return i;
36086470930SIngo Molnar 	}
36186470930SIngo Molnar 
3628953645fSIngo Molnar 	return -1;
36386470930SIngo Molnar }
36486470930SIngo Molnar 
36543d0b978SWang Nan typedef int config_term_func_t(struct perf_event_attr *attr,
36643d0b978SWang Nan 			       struct parse_events_term *term,
36743d0b978SWang Nan 			       struct parse_events_error *err);
36843d0b978SWang Nan static int config_term_common(struct perf_event_attr *attr,
36943d0b978SWang Nan 			      struct parse_events_term *term,
37043d0b978SWang Nan 			      struct parse_events_error *err);
37143d0b978SWang Nan static int config_attr(struct perf_event_attr *attr,
37243d0b978SWang Nan 		       struct list_head *head,
37343d0b978SWang Nan 		       struct parse_events_error *err,
37443d0b978SWang Nan 		       config_term_func_t config_term);
37543d0b978SWang Nan 
376c5cd8ac0SDavid Ahern int parse_events_add_cache(struct list_head *list, int *idx,
37743d0b978SWang Nan 			   char *type, char *op_result1, char *op_result2,
3782c97b0d4SArnaldo Carvalho de Melo 			   struct parse_events_error *err,
37943d0b978SWang Nan 			   struct list_head *head_config)
38086470930SIngo Molnar {
38189812fc8SJiri Olsa 	struct perf_event_attr attr;
38243d0b978SWang Nan 	LIST_HEAD(config_terms);
38343d0b978SWang Nan 	char name[MAX_NAME_LEN], *config_name;
38461c45981SPaul Mackerras 	int cache_type = -1, cache_op = -1, cache_result = -1;
38589812fc8SJiri Olsa 	char *op_result[2] = { op_result1, op_result2 };
38689812fc8SJiri Olsa 	int i, n;
38786470930SIngo Molnar 
38886470930SIngo Molnar 	/*
38986470930SIngo Molnar 	 * No fallback - if we cannot get a clear cache type
39086470930SIngo Molnar 	 * then bail out:
39186470930SIngo Molnar 	 */
3920b668bc9SArnaldo Carvalho de Melo 	cache_type = parse_aliases(type, perf_evsel__hw_cache,
39389812fc8SJiri Olsa 				   PERF_COUNT_HW_CACHE_MAX);
39486470930SIngo Molnar 	if (cache_type == -1)
39589812fc8SJiri Olsa 		return -EINVAL;
39686470930SIngo Molnar 
39743d0b978SWang Nan 	config_name = get_config_name(head_config);
39889812fc8SJiri Olsa 	n = snprintf(name, MAX_NAME_LEN, "%s", type);
39989812fc8SJiri Olsa 
40089812fc8SJiri Olsa 	for (i = 0; (i < 2) && (op_result[i]); i++) {
40189812fc8SJiri Olsa 		char *str = op_result[i];
40289812fc8SJiri Olsa 
403275ef387SJiri Olsa 		n += snprintf(name + n, MAX_NAME_LEN - n, "-%s", str);
40461c45981SPaul Mackerras 
40561c45981SPaul Mackerras 		if (cache_op == -1) {
4060b668bc9SArnaldo Carvalho de Melo 			cache_op = parse_aliases(str, perf_evsel__hw_cache_op,
40761c45981SPaul Mackerras 						 PERF_COUNT_HW_CACHE_OP_MAX);
40861c45981SPaul Mackerras 			if (cache_op >= 0) {
4090b668bc9SArnaldo Carvalho de Melo 				if (!perf_evsel__is_cache_op_valid(cache_type, cache_op))
41089812fc8SJiri Olsa 					return -EINVAL;
41161c45981SPaul Mackerras 				continue;
41261c45981SPaul Mackerras 			}
41361c45981SPaul Mackerras 		}
41461c45981SPaul Mackerras 
41561c45981SPaul Mackerras 		if (cache_result == -1) {
4160b668bc9SArnaldo Carvalho de Melo 			cache_result = parse_aliases(str, perf_evsel__hw_cache_result,
41761c45981SPaul Mackerras 						     PERF_COUNT_HW_CACHE_RESULT_MAX);
41861c45981SPaul Mackerras 			if (cache_result >= 0)
41961c45981SPaul Mackerras 				continue;
42061c45981SPaul Mackerras 		}
42161c45981SPaul Mackerras 	}
42261c45981SPaul Mackerras 
42386470930SIngo Molnar 	/*
42486470930SIngo Molnar 	 * Fall back to reads:
42586470930SIngo Molnar 	 */
4268953645fSIngo Molnar 	if (cache_op == -1)
4278953645fSIngo Molnar 		cache_op = PERF_COUNT_HW_CACHE_OP_READ;
42886470930SIngo Molnar 
42986470930SIngo Molnar 	/*
43086470930SIngo Molnar 	 * Fall back to accesses:
43186470930SIngo Molnar 	 */
43286470930SIngo Molnar 	if (cache_result == -1)
43386470930SIngo Molnar 		cache_result = PERF_COUNT_HW_CACHE_RESULT_ACCESS;
43486470930SIngo Molnar 
43589812fc8SJiri Olsa 	memset(&attr, 0, sizeof(attr));
43689812fc8SJiri Olsa 	attr.config = cache_type | (cache_op << 8) | (cache_result << 16);
43789812fc8SJiri Olsa 	attr.type = PERF_TYPE_HW_CACHE;
43843d0b978SWang Nan 
43943d0b978SWang Nan 	if (head_config) {
4402c97b0d4SArnaldo Carvalho de Melo 		if (config_attr(&attr, head_config, err,
44143d0b978SWang Nan 				config_term_common))
44243d0b978SWang Nan 			return -EINVAL;
44343d0b978SWang Nan 
44443d0b978SWang Nan 		if (get_config_terms(head_config, &config_terms))
44543d0b978SWang Nan 			return -ENOMEM;
44643d0b978SWang Nan 	}
44743d0b978SWang Nan 	return add_event(list, idx, &attr, config_name ? : name, &config_terms);
44886470930SIngo Molnar }
44986470930SIngo Molnar 
450272ed29aSJiri Olsa static void tracepoint_error(struct parse_events_error *e, int err,
4518c619d6aSWang Nan 			     const char *sys, const char *name)
45219658171SJiri Olsa {
45319658171SJiri Olsa 	char help[BUFSIZ];
45419658171SJiri Olsa 
455ec183d22SAdrian Hunter 	if (!e)
456ec183d22SAdrian Hunter 		return;
457ec183d22SAdrian Hunter 
45819658171SJiri Olsa 	/*
45919658171SJiri Olsa 	 * We get error directly from syscall errno ( > 0),
46019658171SJiri Olsa 	 * or from encoded pointer's error ( < 0).
46119658171SJiri Olsa 	 */
46219658171SJiri Olsa 	err = abs(err);
46319658171SJiri Olsa 
46419658171SJiri Olsa 	switch (err) {
46519658171SJiri Olsa 	case EACCES:
466272ed29aSJiri Olsa 		e->str = strdup("can't access trace events");
46719658171SJiri Olsa 		break;
46819658171SJiri Olsa 	case ENOENT:
469272ed29aSJiri Olsa 		e->str = strdup("unknown tracepoint");
47019658171SJiri Olsa 		break;
47119658171SJiri Olsa 	default:
472272ed29aSJiri Olsa 		e->str = strdup("failed to add tracepoint");
47319658171SJiri Olsa 		break;
47419658171SJiri Olsa 	}
47519658171SJiri Olsa 
47619658171SJiri Olsa 	tracing_path__strerror_open_tp(err, help, sizeof(help), sys, name);
477272ed29aSJiri Olsa 	e->help = strdup(help);
47819658171SJiri Olsa }
47919658171SJiri Olsa 
480c5cd8ac0SDavid Ahern static int add_tracepoint(struct list_head *list, int *idx,
4818c619d6aSWang Nan 			  const char *sys_name, const char *evt_name,
482272ed29aSJiri Olsa 			  struct parse_events_error *err,
483e637d177SHe Kuang 			  struct list_head *head_config)
484bcd3279fSFrederic Weisbecker {
48582fe1c29SArnaldo Carvalho de Melo 	struct perf_evsel *evsel;
486bcd3279fSFrederic Weisbecker 
487ef503831SArnaldo Carvalho de Melo 	evsel = perf_evsel__newtp_idx(sys_name, evt_name, (*idx)++);
48819658171SJiri Olsa 	if (IS_ERR(evsel)) {
489272ed29aSJiri Olsa 		tracepoint_error(err, PTR_ERR(evsel), sys_name, evt_name);
4908dd2a131SJiri Olsa 		return PTR_ERR(evsel);
49119658171SJiri Olsa 	}
492bcd3279fSFrederic Weisbecker 
493e637d177SHe Kuang 	if (head_config) {
494e637d177SHe Kuang 		LIST_HEAD(config_terms);
495e637d177SHe Kuang 
496e637d177SHe Kuang 		if (get_config_terms(head_config, &config_terms))
497e637d177SHe Kuang 			return -ENOMEM;
498e637d177SHe Kuang 		list_splice(&config_terms, &evsel->config_terms);
499e637d177SHe Kuang 	}
500e637d177SHe Kuang 
50182fe1c29SArnaldo Carvalho de Melo 	list_add_tail(&evsel->node, list);
50282fe1c29SArnaldo Carvalho de Melo 	return 0;
503bcd3279fSFrederic Weisbecker }
504bcd3279fSFrederic Weisbecker 
505c5cd8ac0SDavid Ahern static int add_tracepoint_multi_event(struct list_head *list, int *idx,
5068c619d6aSWang Nan 				      const char *sys_name, const char *evt_name,
507272ed29aSJiri Olsa 				      struct parse_events_error *err,
508e637d177SHe Kuang 				      struct list_head *head_config)
509bcd3279fSFrederic Weisbecker {
510bcd3279fSFrederic Weisbecker 	char evt_path[MAXPATHLEN];
511bcd3279fSFrederic Weisbecker 	struct dirent *evt_ent;
512bcd3279fSFrederic Weisbecker 	DIR *evt_dir;
51327bf90bfSJiri Olsa 	int ret = 0, found = 0;
514bcd3279fSFrederic Weisbecker 
515ebf294bfSArnaldo Carvalho de Melo 	snprintf(evt_path, MAXPATHLEN, "%s/%s", tracing_events_path, sys_name);
516bcd3279fSFrederic Weisbecker 	evt_dir = opendir(evt_path);
517bcd3279fSFrederic Weisbecker 	if (!evt_dir) {
518272ed29aSJiri Olsa 		tracepoint_error(err, errno, sys_name, evt_name);
51989812fc8SJiri Olsa 		return -1;
520bcd3279fSFrederic Weisbecker 	}
521bcd3279fSFrederic Weisbecker 
52289812fc8SJiri Olsa 	while (!ret && (evt_ent = readdir(evt_dir))) {
523bcd3279fSFrederic Weisbecker 		if (!strcmp(evt_ent->d_name, ".")
524bcd3279fSFrederic Weisbecker 		    || !strcmp(evt_ent->d_name, "..")
525bcd3279fSFrederic Weisbecker 		    || !strcmp(evt_ent->d_name, "enable")
526bcd3279fSFrederic Weisbecker 		    || !strcmp(evt_ent->d_name, "filter"))
527bcd3279fSFrederic Weisbecker 			continue;
528bcd3279fSFrederic Weisbecker 
52989812fc8SJiri Olsa 		if (!strglobmatch(evt_ent->d_name, evt_name))
530fb1d2edfSMasami Hiramatsu 			continue;
531fb1d2edfSMasami Hiramatsu 
53227bf90bfSJiri Olsa 		found++;
53327bf90bfSJiri Olsa 
534e637d177SHe Kuang 		ret = add_tracepoint(list, idx, sys_name, evt_ent->d_name,
535272ed29aSJiri Olsa 				     err, head_config);
536bcd3279fSFrederic Weisbecker 	}
537bcd3279fSFrederic Weisbecker 
53827bf90bfSJiri Olsa 	if (!found) {
53927bf90bfSJiri Olsa 		tracepoint_error(err, ENOENT, sys_name, evt_name);
54027bf90bfSJiri Olsa 		ret = -1;
54127bf90bfSJiri Olsa 	}
54227bf90bfSJiri Olsa 
5430bd3f084SJiri Olsa 	closedir(evt_dir);
54489812fc8SJiri Olsa 	return ret;
545bcd3279fSFrederic Weisbecker }
546bcd3279fSFrederic Weisbecker 
547c5cd8ac0SDavid Ahern static int add_tracepoint_event(struct list_head *list, int *idx,
5488c619d6aSWang Nan 				const char *sys_name, const char *evt_name,
549272ed29aSJiri Olsa 				struct parse_events_error *err,
550e637d177SHe Kuang 				struct list_head *head_config)
551f35488f9SJiri Olsa {
552f35488f9SJiri Olsa 	return strpbrk(evt_name, "*?") ?
553e637d177SHe Kuang 	       add_tracepoint_multi_event(list, idx, sys_name, evt_name,
554272ed29aSJiri Olsa 					  err, head_config) :
555e637d177SHe Kuang 	       add_tracepoint(list, idx, sys_name, evt_name,
556272ed29aSJiri Olsa 			      err, head_config);
557f35488f9SJiri Olsa }
558f35488f9SJiri Olsa 
559c5cd8ac0SDavid Ahern static int add_tracepoint_multi_sys(struct list_head *list, int *idx,
5608c619d6aSWang Nan 				    const char *sys_name, const char *evt_name,
561272ed29aSJiri Olsa 				    struct parse_events_error *err,
562e637d177SHe Kuang 				    struct list_head *head_config)
563f35488f9SJiri Olsa {
564f35488f9SJiri Olsa 	struct dirent *events_ent;
565f35488f9SJiri Olsa 	DIR *events_dir;
566f35488f9SJiri Olsa 	int ret = 0;
567f35488f9SJiri Olsa 
568f35488f9SJiri Olsa 	events_dir = opendir(tracing_events_path);
569f35488f9SJiri Olsa 	if (!events_dir) {
570272ed29aSJiri Olsa 		tracepoint_error(err, errno, sys_name, evt_name);
571f35488f9SJiri Olsa 		return -1;
572f35488f9SJiri Olsa 	}
573f35488f9SJiri Olsa 
574f35488f9SJiri Olsa 	while (!ret && (events_ent = readdir(events_dir))) {
575f35488f9SJiri Olsa 		if (!strcmp(events_ent->d_name, ".")
576f35488f9SJiri Olsa 		    || !strcmp(events_ent->d_name, "..")
577f35488f9SJiri Olsa 		    || !strcmp(events_ent->d_name, "enable")
578f35488f9SJiri Olsa 		    || !strcmp(events_ent->d_name, "header_event")
579f35488f9SJiri Olsa 		    || !strcmp(events_ent->d_name, "header_page"))
580f35488f9SJiri Olsa 			continue;
581f35488f9SJiri Olsa 
582f35488f9SJiri Olsa 		if (!strglobmatch(events_ent->d_name, sys_name))
583f35488f9SJiri Olsa 			continue;
584f35488f9SJiri Olsa 
585f35488f9SJiri Olsa 		ret = add_tracepoint_event(list, idx, events_ent->d_name,
586272ed29aSJiri Olsa 					   evt_name, err, head_config);
587f35488f9SJiri Olsa 	}
588f35488f9SJiri Olsa 
589f35488f9SJiri Olsa 	closedir(events_dir);
590f35488f9SJiri Olsa 	return ret;
591f35488f9SJiri Olsa }
592f35488f9SJiri Olsa 
5934edf30e3SWang Nan struct __add_bpf_event_param {
5945d9cdc11SArnaldo Carvalho de Melo 	struct parse_events_state *parse_state;
5954edf30e3SWang Nan 	struct list_head *list;
59695088a59SWang Nan 	struct list_head *head_config;
5974edf30e3SWang Nan };
5984edf30e3SWang Nan 
599cd102d70SWang Nan static int add_bpf_event(const char *group, const char *event, int fd,
6004edf30e3SWang Nan 			 void *_param)
6014edf30e3SWang Nan {
6024edf30e3SWang Nan 	LIST_HEAD(new_evsels);
6034edf30e3SWang Nan 	struct __add_bpf_event_param *param = _param;
6045d9cdc11SArnaldo Carvalho de Melo 	struct parse_events_state *parse_state = param->parse_state;
6054edf30e3SWang Nan 	struct list_head *list = param->list;
6061f45b1d4SWang Nan 	struct perf_evsel *pos;
6074edf30e3SWang Nan 	int err;
6084edf30e3SWang Nan 
6094edf30e3SWang Nan 	pr_debug("add bpf event %s:%s and attach bpf program %d\n",
610cd102d70SWang Nan 		 group, event, fd);
6114edf30e3SWang Nan 
6125d369a75SArnaldo Carvalho de Melo 	err = parse_events_add_tracepoint(&new_evsels, &parse_state->idx, group,
6135d369a75SArnaldo Carvalho de Melo 					  event, parse_state->error,
61495088a59SWang Nan 					  param->head_config);
6154edf30e3SWang Nan 	if (err) {
6164edf30e3SWang Nan 		struct perf_evsel *evsel, *tmp;
6174edf30e3SWang Nan 
6184edf30e3SWang Nan 		pr_debug("Failed to add BPF event %s:%s\n",
619cd102d70SWang Nan 			 group, event);
6204edf30e3SWang Nan 		list_for_each_entry_safe(evsel, tmp, &new_evsels, node) {
6214edf30e3SWang Nan 			list_del(&evsel->node);
6224edf30e3SWang Nan 			perf_evsel__delete(evsel);
6234edf30e3SWang Nan 		}
6244edf30e3SWang Nan 		return err;
6254edf30e3SWang Nan 	}
626cd102d70SWang Nan 	pr_debug("adding %s:%s\n", group, event);
6274edf30e3SWang Nan 
6281f45b1d4SWang Nan 	list_for_each_entry(pos, &new_evsels, node) {
6291f45b1d4SWang Nan 		pr_debug("adding %s:%s to %p\n",
630cd102d70SWang Nan 			 group, event, pos);
6311f45b1d4SWang Nan 		pos->bpf_fd = fd;
6321f45b1d4SWang Nan 	}
6334edf30e3SWang Nan 	list_splice(&new_evsels, list);
6344edf30e3SWang Nan 	return 0;
6354edf30e3SWang Nan }
6364edf30e3SWang Nan 
6375d9cdc11SArnaldo Carvalho de Melo int parse_events_load_bpf_obj(struct parse_events_state *parse_state,
63884c86ca1SWang Nan 			      struct list_head *list,
63995088a59SWang Nan 			      struct bpf_object *obj,
64095088a59SWang Nan 			      struct list_head *head_config)
64184c86ca1SWang Nan {
64284c86ca1SWang Nan 	int err;
64384c86ca1SWang Nan 	char errbuf[BUFSIZ];
6445d9cdc11SArnaldo Carvalho de Melo 	struct __add_bpf_event_param param = {parse_state, list, head_config};
645aa3abf30SWang Nan 	static bool registered_unprobe_atexit = false;
64684c86ca1SWang Nan 
64784c86ca1SWang Nan 	if (IS_ERR(obj) || !obj) {
64884c86ca1SWang Nan 		snprintf(errbuf, sizeof(errbuf),
64984c86ca1SWang Nan 			 "Internal error: load bpf obj with NULL");
65084c86ca1SWang Nan 		err = -EINVAL;
65184c86ca1SWang Nan 		goto errout;
65284c86ca1SWang Nan 	}
65384c86ca1SWang Nan 
65484c86ca1SWang Nan 	/*
655aa3abf30SWang Nan 	 * Register atexit handler before calling bpf__probe() so
656aa3abf30SWang Nan 	 * bpf__probe() don't need to unprobe probe points its already
657aa3abf30SWang Nan 	 * created when failure.
658aa3abf30SWang Nan 	 */
659aa3abf30SWang Nan 	if (!registered_unprobe_atexit) {
660aa3abf30SWang Nan 		atexit(bpf__clear);
661aa3abf30SWang Nan 		registered_unprobe_atexit = true;
662aa3abf30SWang Nan 	}
663aa3abf30SWang Nan 
664aa3abf30SWang Nan 	err = bpf__probe(obj);
665aa3abf30SWang Nan 	if (err) {
666aa3abf30SWang Nan 		bpf__strerror_probe(obj, err, errbuf, sizeof(errbuf));
667aa3abf30SWang Nan 		goto errout;
668aa3abf30SWang Nan 	}
669aa3abf30SWang Nan 
6701e5e3ee8SWang Nan 	err = bpf__load(obj);
6711e5e3ee8SWang Nan 	if (err) {
6721e5e3ee8SWang Nan 		bpf__strerror_load(obj, err, errbuf, sizeof(errbuf));
6731e5e3ee8SWang Nan 		goto errout;
6741e5e3ee8SWang Nan 	}
6751e5e3ee8SWang Nan 
676cd102d70SWang Nan 	err = bpf__foreach_event(obj, add_bpf_event, &param);
6774edf30e3SWang Nan 	if (err) {
6784edf30e3SWang Nan 		snprintf(errbuf, sizeof(errbuf),
6794edf30e3SWang Nan 			 "Attach events in BPF object failed");
6804edf30e3SWang Nan 		goto errout;
6814edf30e3SWang Nan 	}
6824edf30e3SWang Nan 
6834edf30e3SWang Nan 	return 0;
68484c86ca1SWang Nan errout:
6855d9cdc11SArnaldo Carvalho de Melo 	parse_state->error->help = strdup("(add -v to see detail)");
6865d9cdc11SArnaldo Carvalho de Melo 	parse_state->error->str = strdup(errbuf);
68784c86ca1SWang Nan 	return err;
68884c86ca1SWang Nan }
68984c86ca1SWang Nan 
690a34f3be7SWang Nan static int
6915d9cdc11SArnaldo Carvalho de Melo parse_events_config_bpf(struct parse_events_state *parse_state,
692a34f3be7SWang Nan 			struct bpf_object *obj,
693a34f3be7SWang Nan 			struct list_head *head_config)
694a34f3be7SWang Nan {
695a34f3be7SWang Nan 	struct parse_events_term *term;
696a34f3be7SWang Nan 	int error_pos;
697a34f3be7SWang Nan 
698a34f3be7SWang Nan 	if (!head_config || list_empty(head_config))
699a34f3be7SWang Nan 		return 0;
700a34f3be7SWang Nan 
701a34f3be7SWang Nan 	list_for_each_entry(term, head_config, list) {
702a34f3be7SWang Nan 		char errbuf[BUFSIZ];
703a34f3be7SWang Nan 		int err;
704a34f3be7SWang Nan 
705a34f3be7SWang Nan 		if (term->type_term != PARSE_EVENTS__TERM_TYPE_USER) {
706a34f3be7SWang Nan 			snprintf(errbuf, sizeof(errbuf),
707a34f3be7SWang Nan 				 "Invalid config term for BPF object");
708a34f3be7SWang Nan 			errbuf[BUFSIZ - 1] = '\0';
709a34f3be7SWang Nan 
7105d9cdc11SArnaldo Carvalho de Melo 			parse_state->error->idx = term->err_term;
7115d9cdc11SArnaldo Carvalho de Melo 			parse_state->error->str = strdup(errbuf);
712a34f3be7SWang Nan 			return -EINVAL;
713a34f3be7SWang Nan 		}
714a34f3be7SWang Nan 
7155d9cdc11SArnaldo Carvalho de Melo 		err = bpf__config_obj(obj, term, parse_state->evlist, &error_pos);
716a34f3be7SWang Nan 		if (err) {
7175d9cdc11SArnaldo Carvalho de Melo 			bpf__strerror_config_obj(obj, term, parse_state->evlist,
718a34f3be7SWang Nan 						 &error_pos, err, errbuf,
719a34f3be7SWang Nan 						 sizeof(errbuf));
7205d9cdc11SArnaldo Carvalho de Melo 			parse_state->error->help = strdup(
7217630b3e2SWang Nan "Hint:\tValid config terms:\n"
722e571e029SWang Nan "     \tmap:[<arraymap>].value<indices>=[value]\n"
723e571e029SWang Nan "     \tmap:[<eventmap>].event<indices>=[event]\n"
7247630b3e2SWang Nan "\n"
725e571e029SWang Nan "     \twhere <indices> is something like [0,3...5] or [all]\n"
726a34f3be7SWang Nan "     \t(add -v to see detail)");
7275d9cdc11SArnaldo Carvalho de Melo 			parse_state->error->str = strdup(errbuf);
728a34f3be7SWang Nan 			if (err == -BPF_LOADER_ERRNO__OBJCONF_MAP_VALUE)
7295d9cdc11SArnaldo Carvalho de Melo 				parse_state->error->idx = term->err_val;
730a34f3be7SWang Nan 			else
7315d9cdc11SArnaldo Carvalho de Melo 				parse_state->error->idx = term->err_term + error_pos;
732a34f3be7SWang Nan 			return err;
733a34f3be7SWang Nan 		}
734a34f3be7SWang Nan 	}
735a34f3be7SWang Nan 	return 0;
736a34f3be7SWang Nan }
737a34f3be7SWang Nan 
73895088a59SWang Nan /*
73995088a59SWang Nan  * Split config terms:
74095088a59SWang Nan  * perf record -e bpf.c/call-graph=fp,map:array.value[0]=1/ ...
74195088a59SWang Nan  *  'call-graph=fp' is 'evt config', should be applied to each
74295088a59SWang Nan  *  events in bpf.c.
74395088a59SWang Nan  * 'map:array.value[0]=1' is 'obj config', should be processed
74495088a59SWang Nan  * with parse_events_config_bpf.
74595088a59SWang Nan  *
74695088a59SWang Nan  * Move object config terms from the first list to obj_head_config.
74795088a59SWang Nan  */
74895088a59SWang Nan static void
74995088a59SWang Nan split_bpf_config_terms(struct list_head *evt_head_config,
75095088a59SWang Nan 		       struct list_head *obj_head_config)
75195088a59SWang Nan {
75295088a59SWang Nan 	struct parse_events_term *term, *temp;
75395088a59SWang Nan 
75495088a59SWang Nan 	/*
75595088a59SWang Nan 	 * Currectly, all possible user config term
75695088a59SWang Nan 	 * belong to bpf object. parse_events__is_hardcoded_term()
75795088a59SWang Nan 	 * happends to be a good flag.
75895088a59SWang Nan 	 *
75995088a59SWang Nan 	 * See parse_events_config_bpf() and
76095088a59SWang Nan 	 * config_term_tracepoint().
76195088a59SWang Nan 	 */
76295088a59SWang Nan 	list_for_each_entry_safe(term, temp, evt_head_config, list)
76395088a59SWang Nan 		if (!parse_events__is_hardcoded_term(term))
76495088a59SWang Nan 			list_move_tail(&term->list, obj_head_config);
76595088a59SWang Nan }
76695088a59SWang Nan 
7675d9cdc11SArnaldo Carvalho de Melo int parse_events_load_bpf(struct parse_events_state *parse_state,
76884c86ca1SWang Nan 			  struct list_head *list,
769d509db04SWang Nan 			  char *bpf_file_name,
770a34f3be7SWang Nan 			  bool source,
771a34f3be7SWang Nan 			  struct list_head *head_config)
77284c86ca1SWang Nan {
773a34f3be7SWang Nan 	int err;
77495088a59SWang Nan 	struct bpf_object *obj;
77595088a59SWang Nan 	LIST_HEAD(obj_head_config);
77695088a59SWang Nan 
77795088a59SWang Nan 	if (head_config)
77895088a59SWang Nan 		split_bpf_config_terms(head_config, &obj_head_config);
77984c86ca1SWang Nan 
780d509db04SWang Nan 	obj = bpf__prepare_load(bpf_file_name, source);
7816371ca3bSWang Nan 	if (IS_ERR(obj)) {
78284c86ca1SWang Nan 		char errbuf[BUFSIZ];
78384c86ca1SWang Nan 
7846371ca3bSWang Nan 		err = PTR_ERR(obj);
78584c86ca1SWang Nan 
78684c86ca1SWang Nan 		if (err == -ENOTSUP)
78784c86ca1SWang Nan 			snprintf(errbuf, sizeof(errbuf),
78884c86ca1SWang Nan 				 "BPF support is not compiled");
78984c86ca1SWang Nan 		else
790d3e0ce39SWang Nan 			bpf__strerror_prepare_load(bpf_file_name,
791d3e0ce39SWang Nan 						   source,
792d3e0ce39SWang Nan 						   -err, errbuf,
793d3e0ce39SWang Nan 						   sizeof(errbuf));
79484c86ca1SWang Nan 
7955d9cdc11SArnaldo Carvalho de Melo 		parse_state->error->help = strdup("(add -v to see detail)");
7965d9cdc11SArnaldo Carvalho de Melo 		parse_state->error->str = strdup(errbuf);
79784c86ca1SWang Nan 		return err;
79884c86ca1SWang Nan 	}
79984c86ca1SWang Nan 
8005d9cdc11SArnaldo Carvalho de Melo 	err = parse_events_load_bpf_obj(parse_state, list, obj, head_config);
801a34f3be7SWang Nan 	if (err)
802a34f3be7SWang Nan 		return err;
8035d9cdc11SArnaldo Carvalho de Melo 	err = parse_events_config_bpf(parse_state, obj, &obj_head_config);
80495088a59SWang Nan 
80595088a59SWang Nan 	/*
80695088a59SWang Nan 	 * Caller doesn't know anything about obj_head_config,
80795088a59SWang Nan 	 * so combine them together again before returnning.
80895088a59SWang Nan 	 */
80995088a59SWang Nan 	if (head_config)
81095088a59SWang Nan 		list_splice_tail(&obj_head_config, head_config);
81195088a59SWang Nan 	return err;
81284c86ca1SWang Nan }
81384c86ca1SWang Nan 
81489812fc8SJiri Olsa static int
81589812fc8SJiri Olsa parse_breakpoint_type(const char *type, struct perf_event_attr *attr)
8161b290d67SFrederic Weisbecker {
8171b290d67SFrederic Weisbecker 	int i;
8181b290d67SFrederic Weisbecker 
8191b290d67SFrederic Weisbecker 	for (i = 0; i < 3; i++) {
82089812fc8SJiri Olsa 		if (!type || !type[i])
8211b290d67SFrederic Weisbecker 			break;
8221b290d67SFrederic Weisbecker 
8237582732fSJiri Olsa #define CHECK_SET_TYPE(bit)		\
8247582732fSJiri Olsa do {					\
8257582732fSJiri Olsa 	if (attr->bp_type & bit)	\
8267582732fSJiri Olsa 		return -EINVAL;		\
8277582732fSJiri Olsa 	else				\
8287582732fSJiri Olsa 		attr->bp_type |= bit;	\
8297582732fSJiri Olsa } while (0)
8307582732fSJiri Olsa 
8311b290d67SFrederic Weisbecker 		switch (type[i]) {
8321b290d67SFrederic Weisbecker 		case 'r':
8337582732fSJiri Olsa 			CHECK_SET_TYPE(HW_BREAKPOINT_R);
8341b290d67SFrederic Weisbecker 			break;
8351b290d67SFrederic Weisbecker 		case 'w':
8367582732fSJiri Olsa 			CHECK_SET_TYPE(HW_BREAKPOINT_W);
8371b290d67SFrederic Weisbecker 			break;
8381b290d67SFrederic Weisbecker 		case 'x':
8397582732fSJiri Olsa 			CHECK_SET_TYPE(HW_BREAKPOINT_X);
8401b290d67SFrederic Weisbecker 			break;
8411b290d67SFrederic Weisbecker 		default:
84289812fc8SJiri Olsa 			return -EINVAL;
8431b290d67SFrederic Weisbecker 		}
8441b290d67SFrederic Weisbecker 	}
84589812fc8SJiri Olsa 
8467582732fSJiri Olsa #undef CHECK_SET_TYPE
8477582732fSJiri Olsa 
8481b290d67SFrederic Weisbecker 	if (!attr->bp_type) /* Default */
8491b290d67SFrederic Weisbecker 		attr->bp_type = HW_BREAKPOINT_R | HW_BREAKPOINT_W;
8501b290d67SFrederic Weisbecker 
85189812fc8SJiri Olsa 	return 0;
8521b290d67SFrederic Weisbecker }
8531b290d67SFrederic Weisbecker 
854c5cd8ac0SDavid Ahern int parse_events_add_breakpoint(struct list_head *list, int *idx,
8553741eb9fSJacob Shin 				void *ptr, char *type, u64 len)
8561b290d67SFrederic Weisbecker {
85789812fc8SJiri Olsa 	struct perf_event_attr attr;
8581b290d67SFrederic Weisbecker 
85989812fc8SJiri Olsa 	memset(&attr, 0, sizeof(attr));
8609fafd98fSJiri Olsa 	attr.bp_addr = (unsigned long) ptr;
8611b290d67SFrederic Weisbecker 
86289812fc8SJiri Olsa 	if (parse_breakpoint_type(type, &attr))
86389812fc8SJiri Olsa 		return -EINVAL;
8641b290d67SFrederic Weisbecker 
8653741eb9fSJacob Shin 	/* Provide some defaults if len is not specified */
8663741eb9fSJacob Shin 	if (!len) {
86789812fc8SJiri Olsa 		if (attr.bp_type == HW_BREAKPOINT_X)
8683741eb9fSJacob Shin 			len = sizeof(long);
869aa59a485SFrederic Weisbecker 		else
8703741eb9fSJacob Shin 			len = HW_BREAKPOINT_LEN_4;
8713741eb9fSJacob Shin 	}
8723741eb9fSJacob Shin 
8733741eb9fSJacob Shin 	attr.bp_len = len;
874aa59a485SFrederic Weisbecker 
87589812fc8SJiri Olsa 	attr.type = PERF_TYPE_BREAKPOINT;
8764a841d65SJovi Zhang 	attr.sample_period = 1;
8771b290d67SFrederic Weisbecker 
878930a2e29SJiri Olsa 	return add_event(list, idx, &attr, NULL, NULL);
8791b290d67SFrederic Weisbecker }
8801b290d67SFrederic Weisbecker 
8813b0e371cSJiri Olsa static int check_type_val(struct parse_events_term *term,
8823b0e371cSJiri Olsa 			  struct parse_events_error *err,
8833b0e371cSJiri Olsa 			  int type)
8843b0e371cSJiri Olsa {
8853b0e371cSJiri Olsa 	if (type == term->type_val)
8863b0e371cSJiri Olsa 		return 0;
8873b0e371cSJiri Olsa 
8883b0e371cSJiri Olsa 	if (err) {
8893b0e371cSJiri Olsa 		err->idx = term->err_val;
8903b0e371cSJiri Olsa 		if (type == PARSE_EVENTS__TERM_TYPE_NUM)
8913b0e371cSJiri Olsa 			err->str = strdup("expected numeric value");
8923b0e371cSJiri Olsa 		else
8933b0e371cSJiri Olsa 			err->str = strdup("expected string value");
8943b0e371cSJiri Olsa 	}
8953b0e371cSJiri Olsa 	return -EINVAL;
8963b0e371cSJiri Olsa }
8973b0e371cSJiri Olsa 
89817cb5f84SWang Nan /*
89917cb5f84SWang Nan  * Update according to parse-events.l
90017cb5f84SWang Nan  */
90117cb5f84SWang Nan static const char *config_term_names[__PARSE_EVENTS__TERM_TYPE_NR] = {
90217cb5f84SWang Nan 	[PARSE_EVENTS__TERM_TYPE_USER]			= "<sysfs term>",
90317cb5f84SWang Nan 	[PARSE_EVENTS__TERM_TYPE_CONFIG]		= "config",
90417cb5f84SWang Nan 	[PARSE_EVENTS__TERM_TYPE_CONFIG1]		= "config1",
90517cb5f84SWang Nan 	[PARSE_EVENTS__TERM_TYPE_CONFIG2]		= "config2",
90617cb5f84SWang Nan 	[PARSE_EVENTS__TERM_TYPE_NAME]			= "name",
90717cb5f84SWang Nan 	[PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD]		= "period",
90817cb5f84SWang Nan 	[PARSE_EVENTS__TERM_TYPE_SAMPLE_FREQ]		= "freq",
90917cb5f84SWang Nan 	[PARSE_EVENTS__TERM_TYPE_BRANCH_SAMPLE_TYPE]	= "branch_type",
91017cb5f84SWang Nan 	[PARSE_EVENTS__TERM_TYPE_TIME]			= "time",
91117cb5f84SWang Nan 	[PARSE_EVENTS__TERM_TYPE_CALLGRAPH]		= "call-graph",
91217cb5f84SWang Nan 	[PARSE_EVENTS__TERM_TYPE_STACKSIZE]		= "stack-size",
91317cb5f84SWang Nan 	[PARSE_EVENTS__TERM_TYPE_NOINHERIT]		= "no-inherit",
91417cb5f84SWang Nan 	[PARSE_EVENTS__TERM_TYPE_INHERIT]		= "inherit",
915792d48b4SArnaldo Carvalho de Melo 	[PARSE_EVENTS__TERM_TYPE_MAX_STACK]		= "max-stack",
916626a6b78SWang Nan 	[PARSE_EVENTS__TERM_TYPE_OVERWRITE]		= "overwrite",
917626a6b78SWang Nan 	[PARSE_EVENTS__TERM_TYPE_NOOVERWRITE]		= "no-overwrite",
918dd60fba7SMathieu Poirier 	[PARSE_EVENTS__TERM_TYPE_DRV_CFG]		= "driver-config",
91917cb5f84SWang Nan };
92017cb5f84SWang Nan 
9211669e509SWang Nan static bool config_term_shrinked;
9221669e509SWang Nan 
9231669e509SWang Nan static bool
9241669e509SWang Nan config_term_avail(int term_type, struct parse_events_error *err)
9251669e509SWang Nan {
9261669e509SWang Nan 	if (term_type < 0 || term_type >= __PARSE_EVENTS__TERM_TYPE_NR) {
9271669e509SWang Nan 		err->str = strdup("Invalid term_type");
9281669e509SWang Nan 		return false;
9291669e509SWang Nan 	}
9301669e509SWang Nan 	if (!config_term_shrinked)
9311669e509SWang Nan 		return true;
9321669e509SWang Nan 
9331669e509SWang Nan 	switch (term_type) {
9341669e509SWang Nan 	case PARSE_EVENTS__TERM_TYPE_CONFIG:
9351669e509SWang Nan 	case PARSE_EVENTS__TERM_TYPE_CONFIG1:
9361669e509SWang Nan 	case PARSE_EVENTS__TERM_TYPE_CONFIG2:
9371669e509SWang Nan 	case PARSE_EVENTS__TERM_TYPE_NAME:
93806835545SSukadev Bhattiprolu 	case PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD:
9391669e509SWang Nan 		return true;
9401669e509SWang Nan 	default:
9411669e509SWang Nan 		if (!err)
9421669e509SWang Nan 			return false;
9431669e509SWang Nan 
9441669e509SWang Nan 		/* term_type is validated so indexing is safe */
9451669e509SWang Nan 		if (asprintf(&err->str, "'%s' is not usable in 'perf stat'",
9461669e509SWang Nan 			     config_term_names[term_type]) < 0)
9471669e509SWang Nan 			err->str = NULL;
9481669e509SWang Nan 		return false;
9491669e509SWang Nan 	}
9501669e509SWang Nan }
9511669e509SWang Nan 
9521669e509SWang Nan void parse_events__shrink_config_terms(void)
9531669e509SWang Nan {
9541669e509SWang Nan 	config_term_shrinked = true;
9551669e509SWang Nan }
9561669e509SWang Nan 
9570b8891a8SHe Kuang static int config_term_common(struct perf_event_attr *attr,
9583b0e371cSJiri Olsa 			      struct parse_events_term *term,
9593b0e371cSJiri Olsa 			      struct parse_events_error *err)
9608f707d84SJiri Olsa {
96116fa7e82SJiri Olsa #define CHECK_TYPE_VAL(type)						   \
96216fa7e82SJiri Olsa do {									   \
9633b0e371cSJiri Olsa 	if (check_type_val(term, err, PARSE_EVENTS__TERM_TYPE_ ## type)) \
96416fa7e82SJiri Olsa 		return -EINVAL;						   \
96516fa7e82SJiri Olsa } while (0)
96616fa7e82SJiri Olsa 
96716fa7e82SJiri Olsa 	switch (term->type_term) {
9688f707d84SJiri Olsa 	case PARSE_EVENTS__TERM_TYPE_CONFIG:
96916fa7e82SJiri Olsa 		CHECK_TYPE_VAL(NUM);
9708f707d84SJiri Olsa 		attr->config = term->val.num;
9718f707d84SJiri Olsa 		break;
9728f707d84SJiri Olsa 	case PARSE_EVENTS__TERM_TYPE_CONFIG1:
97316fa7e82SJiri Olsa 		CHECK_TYPE_VAL(NUM);
9748f707d84SJiri Olsa 		attr->config1 = term->val.num;
9758f707d84SJiri Olsa 		break;
9768f707d84SJiri Olsa 	case PARSE_EVENTS__TERM_TYPE_CONFIG2:
97716fa7e82SJiri Olsa 		CHECK_TYPE_VAL(NUM);
9788f707d84SJiri Olsa 		attr->config2 = term->val.num;
9798f707d84SJiri Olsa 		break;
9808f707d84SJiri Olsa 	case PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD:
98116fa7e82SJiri Olsa 		CHECK_TYPE_VAL(NUM);
9828f707d84SJiri Olsa 		break;
98309af2a55SNamhyung Kim 	case PARSE_EVENTS__TERM_TYPE_SAMPLE_FREQ:
98409af2a55SNamhyung Kim 		CHECK_TYPE_VAL(NUM);
98509af2a55SNamhyung Kim 		break;
9868f707d84SJiri Olsa 	case PARSE_EVENTS__TERM_TYPE_BRANCH_SAMPLE_TYPE:
987ac12f676SAndi Kleen 		CHECK_TYPE_VAL(STR);
988ac12f676SAndi Kleen 		if (strcmp(term->val.str, "no") &&
989ac12f676SAndi Kleen 		    parse_branch_str(term->val.str, &attr->branch_sample_type)) {
990ac12f676SAndi Kleen 			err->str = strdup("invalid branch sample type");
991ac12f676SAndi Kleen 			err->idx = term->err_val;
992ac12f676SAndi Kleen 			return -EINVAL;
993ac12f676SAndi Kleen 		}
9948f707d84SJiri Olsa 		break;
99532067712SKan Liang 	case PARSE_EVENTS__TERM_TYPE_TIME:
99632067712SKan Liang 		CHECK_TYPE_VAL(NUM);
99732067712SKan Liang 		if (term->val.num > 1) {
99832067712SKan Liang 			err->str = strdup("expected 0 or 1");
99932067712SKan Liang 			err->idx = term->err_val;
100032067712SKan Liang 			return -EINVAL;
100132067712SKan Liang 		}
100232067712SKan Liang 		break;
1003d457c963SKan Liang 	case PARSE_EVENTS__TERM_TYPE_CALLGRAPH:
1004d457c963SKan Liang 		CHECK_TYPE_VAL(STR);
1005d457c963SKan Liang 		break;
1006d457c963SKan Liang 	case PARSE_EVENTS__TERM_TYPE_STACKSIZE:
1007d457c963SKan Liang 		CHECK_TYPE_VAL(NUM);
1008d457c963SKan Liang 		break;
1009374ce938SWang Nan 	case PARSE_EVENTS__TERM_TYPE_INHERIT:
1010374ce938SWang Nan 		CHECK_TYPE_VAL(NUM);
1011374ce938SWang Nan 		break;
1012374ce938SWang Nan 	case PARSE_EVENTS__TERM_TYPE_NOINHERIT:
1013374ce938SWang Nan 		CHECK_TYPE_VAL(NUM);
1014374ce938SWang Nan 		break;
1015626a6b78SWang Nan 	case PARSE_EVENTS__TERM_TYPE_OVERWRITE:
1016626a6b78SWang Nan 		CHECK_TYPE_VAL(NUM);
1017626a6b78SWang Nan 		break;
1018626a6b78SWang Nan 	case PARSE_EVENTS__TERM_TYPE_NOOVERWRITE:
1019626a6b78SWang Nan 		CHECK_TYPE_VAL(NUM);
1020626a6b78SWang Nan 		break;
10216b5fc39bSJiri Olsa 	case PARSE_EVENTS__TERM_TYPE_NAME:
10226b5fc39bSJiri Olsa 		CHECK_TYPE_VAL(STR);
10236b5fc39bSJiri Olsa 		break;
1024792d48b4SArnaldo Carvalho de Melo 	case PARSE_EVENTS__TERM_TYPE_MAX_STACK:
1025792d48b4SArnaldo Carvalho de Melo 		CHECK_TYPE_VAL(NUM);
1026792d48b4SArnaldo Carvalho de Melo 		break;
10278f707d84SJiri Olsa 	default:
1028ffeb883eSHe Kuang 		err->str = strdup("unknown term");
1029ffeb883eSHe Kuang 		err->idx = term->err_term;
1030ffeb883eSHe Kuang 		err->help = parse_events_formats_error_string(NULL);
10318f707d84SJiri Olsa 		return -EINVAL;
10328f707d84SJiri Olsa 	}
103316fa7e82SJiri Olsa 
10341669e509SWang Nan 	/*
10351669e509SWang Nan 	 * Check term availbility after basic checking so
10361669e509SWang Nan 	 * PARSE_EVENTS__TERM_TYPE_USER can be found and filtered.
10371669e509SWang Nan 	 *
10381669e509SWang Nan 	 * If check availbility at the entry of this function,
10391669e509SWang Nan 	 * user will see "'<sysfs term>' is not usable in 'perf stat'"
10401669e509SWang Nan 	 * if an invalid config term is provided for legacy events
10411669e509SWang Nan 	 * (for example, instructions/badterm/...), which is confusing.
10421669e509SWang Nan 	 */
10431669e509SWang Nan 	if (!config_term_avail(term->type_term, err))
10441669e509SWang Nan 		return -EINVAL;
10458f707d84SJiri Olsa 	return 0;
104616fa7e82SJiri Olsa #undef CHECK_TYPE_VAL
10478f707d84SJiri Olsa }
10488f707d84SJiri Olsa 
10490b8891a8SHe Kuang static int config_term_pmu(struct perf_event_attr *attr,
10500b8891a8SHe Kuang 			   struct parse_events_term *term,
10510b8891a8SHe Kuang 			   struct parse_events_error *err)
10520b8891a8SHe Kuang {
1053dd60fba7SMathieu Poirier 	if (term->type_term == PARSE_EVENTS__TERM_TYPE_USER ||
1054dd60fba7SMathieu Poirier 	    term->type_term == PARSE_EVENTS__TERM_TYPE_DRV_CFG)
10550b8891a8SHe Kuang 		/*
10560b8891a8SHe Kuang 		 * Always succeed for sysfs terms, as we dont know
10570b8891a8SHe Kuang 		 * at this point what type they need to have.
10580b8891a8SHe Kuang 		 */
10590b8891a8SHe Kuang 		return 0;
10600b8891a8SHe Kuang 	else
10610b8891a8SHe Kuang 		return config_term_common(attr, term, err);
10620b8891a8SHe Kuang }
10630b8891a8SHe Kuang 
1064e637d177SHe Kuang static int config_term_tracepoint(struct perf_event_attr *attr,
1065e637d177SHe Kuang 				  struct parse_events_term *term,
1066e637d177SHe Kuang 				  struct parse_events_error *err)
1067e637d177SHe Kuang {
1068e637d177SHe Kuang 	switch (term->type_term) {
1069e637d177SHe Kuang 	case PARSE_EVENTS__TERM_TYPE_CALLGRAPH:
1070e637d177SHe Kuang 	case PARSE_EVENTS__TERM_TYPE_STACKSIZE:
1071374ce938SWang Nan 	case PARSE_EVENTS__TERM_TYPE_INHERIT:
1072374ce938SWang Nan 	case PARSE_EVENTS__TERM_TYPE_NOINHERIT:
1073792d48b4SArnaldo Carvalho de Melo 	case PARSE_EVENTS__TERM_TYPE_MAX_STACK:
1074626a6b78SWang Nan 	case PARSE_EVENTS__TERM_TYPE_OVERWRITE:
1075626a6b78SWang Nan 	case PARSE_EVENTS__TERM_TYPE_NOOVERWRITE:
1076e637d177SHe Kuang 		return config_term_common(attr, term, err);
1077e637d177SHe Kuang 	default:
1078e637d177SHe Kuang 		if (err) {
1079e637d177SHe Kuang 			err->idx = term->err_term;
1080e637d177SHe Kuang 			err->str = strdup("unknown term");
1081e637d177SHe Kuang 			err->help = strdup("valid terms: call-graph,stack-size\n");
1082e637d177SHe Kuang 		}
1083e637d177SHe Kuang 		return -EINVAL;
1084e637d177SHe Kuang 	}
1085e637d177SHe Kuang 
1086e637d177SHe Kuang 	return 0;
1087e637d177SHe Kuang }
1088e637d177SHe Kuang 
10898f707d84SJiri Olsa static int config_attr(struct perf_event_attr *attr,
10903b0e371cSJiri Olsa 		       struct list_head *head,
10910b8891a8SHe Kuang 		       struct parse_events_error *err,
10920b8891a8SHe Kuang 		       config_term_func_t config_term)
10938f707d84SJiri Olsa {
10946cee6cd3SArnaldo Carvalho de Melo 	struct parse_events_term *term;
10958f707d84SJiri Olsa 
10968f707d84SJiri Olsa 	list_for_each_entry(term, head, list)
10973b0e371cSJiri Olsa 		if (config_term(attr, term, err))
10988f707d84SJiri Olsa 			return -EINVAL;
10998f707d84SJiri Olsa 
11008f707d84SJiri Olsa 	return 0;
11018f707d84SJiri Olsa }
11028f707d84SJiri Olsa 
1103930a2e29SJiri Olsa static int get_config_terms(struct list_head *head_config,
1104930a2e29SJiri Olsa 			    struct list_head *head_terms __maybe_unused)
1105930a2e29SJiri Olsa {
1106930a2e29SJiri Olsa #define ADD_CONFIG_TERM(__type, __name, __val)			\
1107930a2e29SJiri Olsa do {								\
1108930a2e29SJiri Olsa 	struct perf_evsel_config_term *__t;			\
1109930a2e29SJiri Olsa 								\
1110930a2e29SJiri Olsa 	__t = zalloc(sizeof(*__t));				\
1111930a2e29SJiri Olsa 	if (!__t)						\
1112930a2e29SJiri Olsa 		return -ENOMEM;					\
1113930a2e29SJiri Olsa 								\
1114930a2e29SJiri Olsa 	INIT_LIST_HEAD(&__t->list);				\
1115930a2e29SJiri Olsa 	__t->type       = PERF_EVSEL__CONFIG_TERM_ ## __type;	\
1116930a2e29SJiri Olsa 	__t->val.__name = __val;				\
1117930a2e29SJiri Olsa 	list_add_tail(&__t->list, head_terms);			\
1118930a2e29SJiri Olsa } while (0)
1119930a2e29SJiri Olsa 
1120930a2e29SJiri Olsa 	struct parse_events_term *term;
1121930a2e29SJiri Olsa 
1122930a2e29SJiri Olsa 	list_for_each_entry(term, head_config, list) {
1123930a2e29SJiri Olsa 		switch (term->type_term) {
1124ee4c7588SJiri Olsa 		case PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD:
1125ee4c7588SJiri Olsa 			ADD_CONFIG_TERM(PERIOD, period, term->val.num);
112632067712SKan Liang 			break;
112709af2a55SNamhyung Kim 		case PARSE_EVENTS__TERM_TYPE_SAMPLE_FREQ:
112809af2a55SNamhyung Kim 			ADD_CONFIG_TERM(FREQ, freq, term->val.num);
112909af2a55SNamhyung Kim 			break;
113032067712SKan Liang 		case PARSE_EVENTS__TERM_TYPE_TIME:
113132067712SKan Liang 			ADD_CONFIG_TERM(TIME, time, term->val.num);
113232067712SKan Liang 			break;
1133d457c963SKan Liang 		case PARSE_EVENTS__TERM_TYPE_CALLGRAPH:
1134d457c963SKan Liang 			ADD_CONFIG_TERM(CALLGRAPH, callgraph, term->val.str);
1135d457c963SKan Liang 			break;
1136ac12f676SAndi Kleen 		case PARSE_EVENTS__TERM_TYPE_BRANCH_SAMPLE_TYPE:
1137ac12f676SAndi Kleen 			ADD_CONFIG_TERM(BRANCH, branch, term->val.str);
1138ac12f676SAndi Kleen 			break;
1139d457c963SKan Liang 		case PARSE_EVENTS__TERM_TYPE_STACKSIZE:
1140d457c963SKan Liang 			ADD_CONFIG_TERM(STACK_USER, stack_user, term->val.num);
1141d457c963SKan Liang 			break;
1142374ce938SWang Nan 		case PARSE_EVENTS__TERM_TYPE_INHERIT:
1143374ce938SWang Nan 			ADD_CONFIG_TERM(INHERIT, inherit, term->val.num ? 1 : 0);
1144374ce938SWang Nan 			break;
1145374ce938SWang Nan 		case PARSE_EVENTS__TERM_TYPE_NOINHERIT:
1146374ce938SWang Nan 			ADD_CONFIG_TERM(INHERIT, inherit, term->val.num ? 0 : 1);
1147374ce938SWang Nan 			break;
1148792d48b4SArnaldo Carvalho de Melo 		case PARSE_EVENTS__TERM_TYPE_MAX_STACK:
1149792d48b4SArnaldo Carvalho de Melo 			ADD_CONFIG_TERM(MAX_STACK, max_stack, term->val.num);
1150792d48b4SArnaldo Carvalho de Melo 			break;
1151626a6b78SWang Nan 		case PARSE_EVENTS__TERM_TYPE_OVERWRITE:
1152626a6b78SWang Nan 			ADD_CONFIG_TERM(OVERWRITE, overwrite, term->val.num ? 1 : 0);
1153626a6b78SWang Nan 			break;
1154626a6b78SWang Nan 		case PARSE_EVENTS__TERM_TYPE_NOOVERWRITE:
1155626a6b78SWang Nan 			ADD_CONFIG_TERM(OVERWRITE, overwrite, term->val.num ? 0 : 1);
1156626a6b78SWang Nan 			break;
1157dd60fba7SMathieu Poirier 		case PARSE_EVENTS__TERM_TYPE_DRV_CFG:
1158dd60fba7SMathieu Poirier 			ADD_CONFIG_TERM(DRV_CFG, drv_cfg, term->val.str);
1159dd60fba7SMathieu Poirier 			break;
1160930a2e29SJiri Olsa 		default:
1161930a2e29SJiri Olsa 			break;
1162930a2e29SJiri Olsa 		}
1163930a2e29SJiri Olsa 	}
1164930a2e29SJiri Olsa #undef ADD_EVSEL_CONFIG
1165930a2e29SJiri Olsa 	return 0;
1166930a2e29SJiri Olsa }
1167930a2e29SJiri Olsa 
1168e637d177SHe Kuang int parse_events_add_tracepoint(struct list_head *list, int *idx,
11698c619d6aSWang Nan 				const char *sys, const char *event,
1170272ed29aSJiri Olsa 				struct parse_events_error *err,
1171e637d177SHe Kuang 				struct list_head *head_config)
1172e637d177SHe Kuang {
1173e637d177SHe Kuang 	if (head_config) {
1174e637d177SHe Kuang 		struct perf_event_attr attr;
1175e637d177SHe Kuang 
1176272ed29aSJiri Olsa 		if (config_attr(&attr, head_config, err,
1177e637d177SHe Kuang 				config_term_tracepoint))
1178e637d177SHe Kuang 			return -EINVAL;
1179e637d177SHe Kuang 	}
1180e637d177SHe Kuang 
1181e637d177SHe Kuang 	if (strpbrk(sys, "*?"))
1182e637d177SHe Kuang 		return add_tracepoint_multi_sys(list, idx, sys, event,
1183272ed29aSJiri Olsa 						err, head_config);
1184e637d177SHe Kuang 	else
1185e637d177SHe Kuang 		return add_tracepoint_event(list, idx, sys, event,
1186272ed29aSJiri Olsa 					    err, head_config);
1187e637d177SHe Kuang }
1188e637d177SHe Kuang 
11895d9cdc11SArnaldo Carvalho de Melo int parse_events_add_numeric(struct parse_events_state *parse_state,
119087d650beSJiri Olsa 			     struct list_head *list,
1191b527bab5SRobert Richter 			     u32 type, u64 config,
11928f707d84SJiri Olsa 			     struct list_head *head_config)
119374d5b588SJaswinder Singh Rajput {
119489812fc8SJiri Olsa 	struct perf_event_attr attr;
1195930a2e29SJiri Olsa 	LIST_HEAD(config_terms);
119674d5b588SJaswinder Singh Rajput 
119789812fc8SJiri Olsa 	memset(&attr, 0, sizeof(attr));
119889812fc8SJiri Olsa 	attr.type = type;
119989812fc8SJiri Olsa 	attr.config = config;
12008f707d84SJiri Olsa 
1201930a2e29SJiri Olsa 	if (head_config) {
12025d9cdc11SArnaldo Carvalho de Melo 		if (config_attr(&attr, head_config, parse_state->error,
12030b8891a8SHe Kuang 				config_term_common))
12048f707d84SJiri Olsa 			return -EINVAL;
12058f707d84SJiri Olsa 
1206930a2e29SJiri Olsa 		if (get_config_terms(head_config, &config_terms))
1207930a2e29SJiri Olsa 			return -ENOMEM;
1208930a2e29SJiri Olsa 	}
1209930a2e29SJiri Olsa 
12105d9cdc11SArnaldo Carvalho de Melo 	return add_event(list, &parse_state->idx, &attr,
121110bf358aSWang Nan 			 get_config_name(head_config), &config_terms);
1212b908debdSIngo Molnar }
1213b908debdSIngo Molnar 
121463ce8449SArnaldo Carvalho de Melo static int __parse_events_add_pmu(struct parse_events_state *parse_state,
121536adec85SJiri Olsa 			 struct list_head *list, char *name,
121663ce8449SArnaldo Carvalho de Melo 			 struct list_head *head_config, bool auto_merge_stats)
12175f537a26SJiri Olsa {
12185f537a26SJiri Olsa 	struct perf_event_attr attr;
121946441bdcSMatt Fleming 	struct perf_pmu_info info;
12205f537a26SJiri Olsa 	struct perf_pmu *pmu;
1221410136f5SStephane Eranian 	struct perf_evsel *evsel;
1222930a2e29SJiri Olsa 	LIST_HEAD(config_terms);
12235f537a26SJiri Olsa 
12245f537a26SJiri Olsa 	pmu = perf_pmu__find(name);
12255f537a26SJiri Olsa 	if (!pmu)
12265f537a26SJiri Olsa 		return -EINVAL;
12275f537a26SJiri Olsa 
1228dc0a6202SAdrian Hunter 	if (pmu->default_config) {
1229dc0a6202SAdrian Hunter 		memcpy(&attr, pmu->default_config,
1230dc0a6202SAdrian Hunter 		       sizeof(struct perf_event_attr));
1231dc0a6202SAdrian Hunter 	} else {
12325f537a26SJiri Olsa 		memset(&attr, 0, sizeof(attr));
1233dc0a6202SAdrian Hunter 	}
12345f537a26SJiri Olsa 
1235ad962273SAdrian Hunter 	if (!head_config) {
1236ad962273SAdrian Hunter 		attr.type = pmu->type;
123763ce8449SArnaldo Carvalho de Melo 		evsel = __add_event(list, &parse_state->idx, &attr, NULL, pmu->cpus, NULL, auto_merge_stats);
1238ad962273SAdrian Hunter 		return evsel ? 0 : -ENOMEM;
1239ad962273SAdrian Hunter 	}
1240ad962273SAdrian Hunter 
124146441bdcSMatt Fleming 	if (perf_pmu__check_alias(pmu, head_config, &info))
1242a6146d50SZheng Yan 		return -EINVAL;
1243a6146d50SZheng Yan 
12445f537a26SJiri Olsa 	/*
12455f537a26SJiri Olsa 	 * Configure hardcoded terms first, no need to check
12465f537a26SJiri Olsa 	 * return value when called with fail == 0 ;)
12475f537a26SJiri Olsa 	 */
12485d9cdc11SArnaldo Carvalho de Melo 	if (config_attr(&attr, head_config, parse_state->error, config_term_pmu))
1249c056ba6aSJiri Olsa 		return -EINVAL;
12505f537a26SJiri Olsa 
1251930a2e29SJiri Olsa 	if (get_config_terms(head_config, &config_terms))
1252930a2e29SJiri Olsa 		return -ENOMEM;
1253930a2e29SJiri Olsa 
12545d9cdc11SArnaldo Carvalho de Melo 	if (perf_pmu__config(pmu, &attr, head_config, parse_state->error))
12555f537a26SJiri Olsa 		return -EINVAL;
12565f537a26SJiri Olsa 
12575d9cdc11SArnaldo Carvalho de Melo 	evsel = __add_event(list, &parse_state->idx, &attr,
1258e814fdddSWang Nan 			    get_config_name(head_config), pmu->cpus,
125963ce8449SArnaldo Carvalho de Melo 			    &config_terms, auto_merge_stats);
1260410136f5SStephane Eranian 	if (evsel) {
126146441bdcSMatt Fleming 		evsel->unit = info.unit;
126246441bdcSMatt Fleming 		evsel->scale = info.scale;
1263044330c1SMatt Fleming 		evsel->per_pkg = info.per_pkg;
12641d9e446bSJiri Olsa 		evsel->snapshot = info.snapshot;
126537932c18SAndi Kleen 		evsel->metric_expr = info.metric_expr;
126696284814SAndi Kleen 		evsel->metric_name = info.metric_name;
1267410136f5SStephane Eranian 	}
1268410136f5SStephane Eranian 
1269410136f5SStephane Eranian 	return evsel ? 0 : -ENOMEM;
12705f537a26SJiri Olsa }
12715f537a26SJiri Olsa 
127263ce8449SArnaldo Carvalho de Melo int parse_events_add_pmu(struct parse_events_state *parse_state,
127363ce8449SArnaldo Carvalho de Melo 			 struct list_head *list, char *name,
127463ce8449SArnaldo Carvalho de Melo 			 struct list_head *head_config)
127563ce8449SArnaldo Carvalho de Melo {
127663ce8449SArnaldo Carvalho de Melo 	return __parse_events_add_pmu(parse_state, list, name, head_config, false);
127763ce8449SArnaldo Carvalho de Melo }
127863ce8449SArnaldo Carvalho de Melo 
12795d9cdc11SArnaldo Carvalho de Melo int parse_events_multi_pmu_add(struct parse_events_state *parse_state,
12802073ad33SAndi Kleen 			       char *str, struct list_head **listp)
12812073ad33SAndi Kleen {
12822073ad33SAndi Kleen 	struct list_head *head;
12832073ad33SAndi Kleen 	struct parse_events_term *term;
12842073ad33SAndi Kleen 	struct list_head *list;
12852073ad33SAndi Kleen 	struct perf_pmu *pmu = NULL;
12862073ad33SAndi Kleen 	int ok = 0;
12872073ad33SAndi Kleen 
12882073ad33SAndi Kleen 	*listp = NULL;
12892073ad33SAndi Kleen 	/* Add it for all PMUs that support the alias */
12902073ad33SAndi Kleen 	list = malloc(sizeof(struct list_head));
12912073ad33SAndi Kleen 	if (!list)
12922073ad33SAndi Kleen 		return -1;
12932073ad33SAndi Kleen 	INIT_LIST_HEAD(list);
12942073ad33SAndi Kleen 	while ((pmu = perf_pmu__scan(pmu)) != NULL) {
12952073ad33SAndi Kleen 		struct perf_pmu_alias *alias;
12962073ad33SAndi Kleen 
12972073ad33SAndi Kleen 		list_for_each_entry(alias, &pmu->aliases, list) {
12982073ad33SAndi Kleen 			if (!strcasecmp(alias->name, str)) {
12992073ad33SAndi Kleen 				head = malloc(sizeof(struct list_head));
13002073ad33SAndi Kleen 				if (!head)
13012073ad33SAndi Kleen 					return -1;
13022073ad33SAndi Kleen 				INIT_LIST_HEAD(head);
13032073ad33SAndi Kleen 				if (parse_events_term__num(&term, PARSE_EVENTS__TERM_TYPE_USER,
13042073ad33SAndi Kleen 							   str, 1, false, &str, NULL) < 0)
13052073ad33SAndi Kleen 					return -1;
13062073ad33SAndi Kleen 				list_add_tail(&term->list, head);
13072073ad33SAndi Kleen 
130863ce8449SArnaldo Carvalho de Melo 				if (!__parse_events_add_pmu(parse_state, list,
130963ce8449SArnaldo Carvalho de Melo 							    pmu->name, head, true)) {
13102073ad33SAndi Kleen 					pr_debug("%s -> %s/%s/\n", str,
13112073ad33SAndi Kleen 						 pmu->name, alias->str);
13122073ad33SAndi Kleen 					ok++;
13132073ad33SAndi Kleen 				}
13142073ad33SAndi Kleen 
13152073ad33SAndi Kleen 				parse_events_terms__delete(head);
13162073ad33SAndi Kleen 			}
13172073ad33SAndi Kleen 		}
13182073ad33SAndi Kleen 	}
13192073ad33SAndi Kleen 	if (!ok)
13202073ad33SAndi Kleen 		return -1;
13212073ad33SAndi Kleen 	*listp = list;
13222073ad33SAndi Kleen 	return 0;
13232073ad33SAndi Kleen }
13242073ad33SAndi Kleen 
13256a4bb04cSJiri Olsa int parse_events__modifier_group(struct list_head *list,
13266a4bb04cSJiri Olsa 				 char *event_mod)
132789efb029SJiri Olsa {
13286a4bb04cSJiri Olsa 	return parse_events__modifier_event(list, event_mod, true);
13296a4bb04cSJiri Olsa }
13306a4bb04cSJiri Olsa 
133163dab225SArnaldo Carvalho de Melo void parse_events__set_leader(char *name, struct list_head *list)
13326a4bb04cSJiri Olsa {
13336a4bb04cSJiri Olsa 	struct perf_evsel *leader;
13346a4bb04cSJiri Olsa 
1335854f7363SWang Nan 	if (list_empty(list)) {
1336854f7363SWang Nan 		WARN_ONCE(true, "WARNING: failed to set leader: empty list");
1337854f7363SWang Nan 		return;
1338854f7363SWang Nan 	}
1339854f7363SWang Nan 
134063dab225SArnaldo Carvalho de Melo 	__perf_evlist__set_leader(list);
134163dab225SArnaldo Carvalho de Melo 	leader = list_entry(list->next, struct perf_evsel, node);
13426a4bb04cSJiri Olsa 	leader->group_name = name ? strdup(name) : NULL;
134389efb029SJiri Olsa }
134489efb029SJiri Olsa 
1345c5cd8ac0SDavid Ahern /* list_event is assumed to point to malloc'ed memory */
13465d7be90eSJiri Olsa void parse_events_update_lists(struct list_head *list_event,
13475d7be90eSJiri Olsa 			       struct list_head *list_all)
13485d7be90eSJiri Olsa {
13495d7be90eSJiri Olsa 	/*
13505d7be90eSJiri Olsa 	 * Called for single event definition. Update the
135189efb029SJiri Olsa 	 * 'all event' list, and reinit the 'single event'
13525d7be90eSJiri Olsa 	 * list, for next event definition.
13535d7be90eSJiri Olsa 	 */
13545d7be90eSJiri Olsa 	list_splice_tail(list_event, list_all);
1355b847cbdcSJiri Olsa 	free(list_event);
13565d7be90eSJiri Olsa }
13575d7be90eSJiri Olsa 
1358f5b1135bSJiri Olsa struct event_modifier {
1359f5b1135bSJiri Olsa 	int eu;
1360f5b1135bSJiri Olsa 	int ek;
1361f5b1135bSJiri Olsa 	int eh;
1362f5b1135bSJiri Olsa 	int eH;
1363f5b1135bSJiri Olsa 	int eG;
1364a1e12da4SJiri Olsa 	int eI;
1365f5b1135bSJiri Olsa 	int precise;
13667f94af7aSJiri Olsa 	int precise_max;
1367f5b1135bSJiri Olsa 	int exclude_GH;
13683c176311SJiri Olsa 	int sample_read;
1369e9a7c414SMichael Ellerman 	int pinned;
13705a5dfe4bSAndi Kleen 	int weak;
1371f5b1135bSJiri Olsa };
137261c45981SPaul Mackerras 
1373f5b1135bSJiri Olsa static int get_event_modifier(struct event_modifier *mod, char *str,
1374f5b1135bSJiri Olsa 			       struct perf_evsel *evsel)
1375f5b1135bSJiri Olsa {
1376f5b1135bSJiri Olsa 	int eu = evsel ? evsel->attr.exclude_user : 0;
1377f5b1135bSJiri Olsa 	int ek = evsel ? evsel->attr.exclude_kernel : 0;
1378f5b1135bSJiri Olsa 	int eh = evsel ? evsel->attr.exclude_hv : 0;
1379f5b1135bSJiri Olsa 	int eH = evsel ? evsel->attr.exclude_host : 0;
1380f5b1135bSJiri Olsa 	int eG = evsel ? evsel->attr.exclude_guest : 0;
1381a1e12da4SJiri Olsa 	int eI = evsel ? evsel->attr.exclude_idle : 0;
1382f5b1135bSJiri Olsa 	int precise = evsel ? evsel->attr.precise_ip : 0;
13837f94af7aSJiri Olsa 	int precise_max = 0;
13843c176311SJiri Olsa 	int sample_read = 0;
1385e9a7c414SMichael Ellerman 	int pinned = evsel ? evsel->attr.pinned : 0;
1386f5b1135bSJiri Olsa 
1387f5b1135bSJiri Olsa 	int exclude = eu | ek | eh;
1388f5b1135bSJiri Olsa 	int exclude_GH = evsel ? evsel->exclude_GH : 0;
13895a5dfe4bSAndi Kleen 	int weak = 0;
1390f5b1135bSJiri Olsa 
1391f5b1135bSJiri Olsa 	memset(mod, 0, sizeof(*mod));
1392ceb53fbfSIngo Molnar 
139361c45981SPaul Mackerras 	while (*str) {
1394ab608344SPeter Zijlstra 		if (*str == 'u') {
1395ab608344SPeter Zijlstra 			if (!exclude)
1396ab608344SPeter Zijlstra 				exclude = eu = ek = eh = 1;
139761c45981SPaul Mackerras 			eu = 0;
1398ab608344SPeter Zijlstra 		} else if (*str == 'k') {
1399ab608344SPeter Zijlstra 			if (!exclude)
1400ab608344SPeter Zijlstra 				exclude = eu = ek = eh = 1;
140161c45981SPaul Mackerras 			ek = 0;
1402ab608344SPeter Zijlstra 		} else if (*str == 'h') {
1403ab608344SPeter Zijlstra 			if (!exclude)
1404ab608344SPeter Zijlstra 				exclude = eu = ek = eh = 1;
140561c45981SPaul Mackerras 			eh = 0;
140699320cc8SJoerg Roedel 		} else if (*str == 'G') {
140799320cc8SJoerg Roedel 			if (!exclude_GH)
140899320cc8SJoerg Roedel 				exclude_GH = eG = eH = 1;
140999320cc8SJoerg Roedel 			eG = 0;
141099320cc8SJoerg Roedel 		} else if (*str == 'H') {
141199320cc8SJoerg Roedel 			if (!exclude_GH)
141299320cc8SJoerg Roedel 				exclude_GH = eG = eH = 1;
141399320cc8SJoerg Roedel 			eH = 0;
1414a1e12da4SJiri Olsa 		} else if (*str == 'I') {
1415a1e12da4SJiri Olsa 			eI = 1;
1416ab608344SPeter Zijlstra 		} else if (*str == 'p') {
1417ab608344SPeter Zijlstra 			precise++;
14181342798cSDavid Ahern 			/* use of precise requires exclude_guest */
14191342798cSDavid Ahern 			if (!exclude_GH)
14201342798cSDavid Ahern 				eG = 1;
14217f94af7aSJiri Olsa 		} else if (*str == 'P') {
14227f94af7aSJiri Olsa 			precise_max = 1;
14233c176311SJiri Olsa 		} else if (*str == 'S') {
14243c176311SJiri Olsa 			sample_read = 1;
1425e9a7c414SMichael Ellerman 		} else if (*str == 'D') {
1426e9a7c414SMichael Ellerman 			pinned = 1;
14275a5dfe4bSAndi Kleen 		} else if (*str == 'W') {
14285a5dfe4bSAndi Kleen 			weak = 1;
1429ab608344SPeter Zijlstra 		} else
143061c45981SPaul Mackerras 			break;
1431ab608344SPeter Zijlstra 
143261c45981SPaul Mackerras 		++str;
143361c45981SPaul Mackerras 	}
143474d5b588SJaswinder Singh Rajput 
143586470930SIngo Molnar 	/*
143689812fc8SJiri Olsa 	 * precise ip:
143789812fc8SJiri Olsa 	 *
143889812fc8SJiri Olsa 	 *  0 - SAMPLE_IP can have arbitrary skid
143989812fc8SJiri Olsa 	 *  1 - SAMPLE_IP must have constant skid
144089812fc8SJiri Olsa 	 *  2 - SAMPLE_IP requested to have 0 skid
144189812fc8SJiri Olsa 	 *  3 - SAMPLE_IP must have 0 skid
144289812fc8SJiri Olsa 	 *
144389812fc8SJiri Olsa 	 *  See also PERF_RECORD_MISC_EXACT_IP
144486470930SIngo Molnar 	 */
144589812fc8SJiri Olsa 	if (precise > 3)
144689812fc8SJiri Olsa 		return -EINVAL;
144786470930SIngo Molnar 
1448f5b1135bSJiri Olsa 	mod->eu = eu;
1449f5b1135bSJiri Olsa 	mod->ek = ek;
1450f5b1135bSJiri Olsa 	mod->eh = eh;
1451f5b1135bSJiri Olsa 	mod->eH = eH;
1452f5b1135bSJiri Olsa 	mod->eG = eG;
1453a1e12da4SJiri Olsa 	mod->eI = eI;
1454f5b1135bSJiri Olsa 	mod->precise = precise;
14557f94af7aSJiri Olsa 	mod->precise_max = precise_max;
1456f5b1135bSJiri Olsa 	mod->exclude_GH = exclude_GH;
14573c176311SJiri Olsa 	mod->sample_read = sample_read;
1458e9a7c414SMichael Ellerman 	mod->pinned = pinned;
14595a5dfe4bSAndi Kleen 	mod->weak = weak;
1460e9a7c414SMichael Ellerman 
1461f5b1135bSJiri Olsa 	return 0;
1462f5b1135bSJiri Olsa }
1463f5b1135bSJiri Olsa 
1464534123f4SJiri Olsa /*
1465534123f4SJiri Olsa  * Basic modifier sanity check to validate it contains only one
1466534123f4SJiri Olsa  * instance of any modifier (apart from 'p') present.
1467534123f4SJiri Olsa  */
1468534123f4SJiri Olsa static int check_modifier(char *str)
1469534123f4SJiri Olsa {
1470534123f4SJiri Olsa 	char *p = str;
1471534123f4SJiri Olsa 
1472534123f4SJiri Olsa 	/* The sizeof includes 0 byte as well. */
14735a5dfe4bSAndi Kleen 	if (strlen(str) > (sizeof("ukhGHpppPSDIW") - 1))
1474534123f4SJiri Olsa 		return -1;
1475534123f4SJiri Olsa 
1476534123f4SJiri Olsa 	while (*p) {
1477534123f4SJiri Olsa 		if (*p != 'p' && strchr(p + 1, *p))
1478534123f4SJiri Olsa 			return -1;
1479534123f4SJiri Olsa 		p++;
1480534123f4SJiri Olsa 	}
1481534123f4SJiri Olsa 
1482534123f4SJiri Olsa 	return 0;
1483534123f4SJiri Olsa }
1484534123f4SJiri Olsa 
1485f5b1135bSJiri Olsa int parse_events__modifier_event(struct list_head *list, char *str, bool add)
1486f5b1135bSJiri Olsa {
1487f5b1135bSJiri Olsa 	struct perf_evsel *evsel;
1488f5b1135bSJiri Olsa 	struct event_modifier mod;
1489f5b1135bSJiri Olsa 
1490f5b1135bSJiri Olsa 	if (str == NULL)
1491f5b1135bSJiri Olsa 		return 0;
1492f5b1135bSJiri Olsa 
1493534123f4SJiri Olsa 	if (check_modifier(str))
1494534123f4SJiri Olsa 		return -EINVAL;
1495534123f4SJiri Olsa 
1496f5b1135bSJiri Olsa 	if (!add && get_event_modifier(&mod, str, NULL))
1497f5b1135bSJiri Olsa 		return -EINVAL;
1498f5b1135bSJiri Olsa 
1499e5cadb93SArnaldo Carvalho de Melo 	__evlist__for_each_entry(list, evsel) {
1500f5b1135bSJiri Olsa 		if (add && get_event_modifier(&mod, str, evsel))
1501f5b1135bSJiri Olsa 			return -EINVAL;
1502f5b1135bSJiri Olsa 
1503f5b1135bSJiri Olsa 		evsel->attr.exclude_user   = mod.eu;
1504f5b1135bSJiri Olsa 		evsel->attr.exclude_kernel = mod.ek;
1505f5b1135bSJiri Olsa 		evsel->attr.exclude_hv     = mod.eh;
1506f5b1135bSJiri Olsa 		evsel->attr.precise_ip     = mod.precise;
1507f5b1135bSJiri Olsa 		evsel->attr.exclude_host   = mod.eH;
1508f5b1135bSJiri Olsa 		evsel->attr.exclude_guest  = mod.eG;
1509a1e12da4SJiri Olsa 		evsel->attr.exclude_idle   = mod.eI;
1510f5b1135bSJiri Olsa 		evsel->exclude_GH          = mod.exclude_GH;
15113c176311SJiri Olsa 		evsel->sample_read         = mod.sample_read;
15127f94af7aSJiri Olsa 		evsel->precise_max         = mod.precise_max;
15135a5dfe4bSAndi Kleen 		evsel->weak_group	   = mod.weak;
1514e9a7c414SMichael Ellerman 
1515e9a7c414SMichael Ellerman 		if (perf_evsel__is_group_leader(evsel))
1516e9a7c414SMichael Ellerman 			evsel->attr.pinned = mod.pinned;
1517ceb53fbfSIngo Molnar 	}
151886470930SIngo Molnar 
151989812fc8SJiri Olsa 	return 0;
152086470930SIngo Molnar }
152186470930SIngo Molnar 
1522ac2ba9f3SRobert Richter int parse_events_name(struct list_head *list, char *name)
1523ac2ba9f3SRobert Richter {
1524ac2ba9f3SRobert Richter 	struct perf_evsel *evsel;
1525ac2ba9f3SRobert Richter 
1526e5cadb93SArnaldo Carvalho de Melo 	__evlist__for_each_entry(list, evsel) {
1527ac2ba9f3SRobert Richter 		if (!evsel->name)
1528ac2ba9f3SRobert Richter 			evsel->name = strdup(name);
1529ac2ba9f3SRobert Richter 	}
1530ac2ba9f3SRobert Richter 
1531ac2ba9f3SRobert Richter 	return 0;
1532ac2ba9f3SRobert Richter }
1533ac2ba9f3SRobert Richter 
1534dcb4e102SKan Liang static int
1535dcb4e102SKan Liang comp_pmu(const void *p1, const void *p2)
1536dcb4e102SKan Liang {
1537dcb4e102SKan Liang 	struct perf_pmu_event_symbol *pmu1 = (struct perf_pmu_event_symbol *) p1;
1538dcb4e102SKan Liang 	struct perf_pmu_event_symbol *pmu2 = (struct perf_pmu_event_symbol *) p2;
1539dcb4e102SKan Liang 
1540e312bcf1SAndi Kleen 	return strcasecmp(pmu1->symbol, pmu2->symbol);
1541dcb4e102SKan Liang }
1542dcb4e102SKan Liang 
1543dcb4e102SKan Liang static void perf_pmu__parse_cleanup(void)
1544dcb4e102SKan Liang {
1545dcb4e102SKan Liang 	if (perf_pmu_events_list_num > 0) {
1546dcb4e102SKan Liang 		struct perf_pmu_event_symbol *p;
1547dcb4e102SKan Liang 		int i;
1548dcb4e102SKan Liang 
1549dcb4e102SKan Liang 		for (i = 0; i < perf_pmu_events_list_num; i++) {
1550dcb4e102SKan Liang 			p = perf_pmu_events_list + i;
1551360e071bSTaeung Song 			zfree(&p->symbol);
1552dcb4e102SKan Liang 		}
1553506fde11STaeung Song 		zfree(&perf_pmu_events_list);
1554dcb4e102SKan Liang 		perf_pmu_events_list_num = 0;
1555dcb4e102SKan Liang 	}
1556dcb4e102SKan Liang }
1557dcb4e102SKan Liang 
1558dcb4e102SKan Liang #define SET_SYMBOL(str, stype)		\
1559dcb4e102SKan Liang do {					\
1560dcb4e102SKan Liang 	p->symbol = str;		\
1561dcb4e102SKan Liang 	if (!p->symbol)			\
1562dcb4e102SKan Liang 		goto err;		\
1563dcb4e102SKan Liang 	p->type = stype;		\
1564dcb4e102SKan Liang } while (0)
1565dcb4e102SKan Liang 
1566dcb4e102SKan Liang /*
1567dcb4e102SKan Liang  * Read the pmu events list from sysfs
1568dcb4e102SKan Liang  * Save it into perf_pmu_events_list
1569dcb4e102SKan Liang  */
1570dcb4e102SKan Liang static void perf_pmu__parse_init(void)
1571dcb4e102SKan Liang {
1572dcb4e102SKan Liang 
1573dcb4e102SKan Liang 	struct perf_pmu *pmu = NULL;
1574dcb4e102SKan Liang 	struct perf_pmu_alias *alias;
1575dcb4e102SKan Liang 	int len = 0;
1576dcb4e102SKan Liang 
1577231bb2aaSAndi Kleen 	pmu = NULL;
1578231bb2aaSAndi Kleen 	while ((pmu = perf_pmu__scan(pmu)) != NULL) {
1579dcb4e102SKan Liang 		list_for_each_entry(alias, &pmu->aliases, list) {
1580dcb4e102SKan Liang 			if (strchr(alias->name, '-'))
1581dcb4e102SKan Liang 				len++;
1582dcb4e102SKan Liang 			len++;
1583dcb4e102SKan Liang 		}
1584231bb2aaSAndi Kleen 	}
1585231bb2aaSAndi Kleen 
1586231bb2aaSAndi Kleen 	if (len == 0) {
1587231bb2aaSAndi Kleen 		perf_pmu_events_list_num = -1;
1588231bb2aaSAndi Kleen 		return;
1589231bb2aaSAndi Kleen 	}
1590dcb4e102SKan Liang 	perf_pmu_events_list = malloc(sizeof(struct perf_pmu_event_symbol) * len);
1591dcb4e102SKan Liang 	if (!perf_pmu_events_list)
1592dcb4e102SKan Liang 		return;
1593dcb4e102SKan Liang 	perf_pmu_events_list_num = len;
1594dcb4e102SKan Liang 
1595dcb4e102SKan Liang 	len = 0;
1596231bb2aaSAndi Kleen 	pmu = NULL;
1597231bb2aaSAndi Kleen 	while ((pmu = perf_pmu__scan(pmu)) != NULL) {
1598dcb4e102SKan Liang 		list_for_each_entry(alias, &pmu->aliases, list) {
1599dcb4e102SKan Liang 			struct perf_pmu_event_symbol *p = perf_pmu_events_list + len;
1600dcb4e102SKan Liang 			char *tmp = strchr(alias->name, '-');
1601dcb4e102SKan Liang 
1602dcb4e102SKan Liang 			if (tmp != NULL) {
1603dcb4e102SKan Liang 				SET_SYMBOL(strndup(alias->name, tmp - alias->name),
1604dcb4e102SKan Liang 						PMU_EVENT_SYMBOL_PREFIX);
1605dcb4e102SKan Liang 				p++;
1606dcb4e102SKan Liang 				SET_SYMBOL(strdup(++tmp), PMU_EVENT_SYMBOL_SUFFIX);
1607dcb4e102SKan Liang 				len += 2;
1608dcb4e102SKan Liang 			} else {
1609dcb4e102SKan Liang 				SET_SYMBOL(strdup(alias->name), PMU_EVENT_SYMBOL);
1610dcb4e102SKan Liang 				len++;
1611dcb4e102SKan Liang 			}
1612dcb4e102SKan Liang 		}
1613231bb2aaSAndi Kleen 	}
1614dcb4e102SKan Liang 	qsort(perf_pmu_events_list, len,
1615dcb4e102SKan Liang 		sizeof(struct perf_pmu_event_symbol), comp_pmu);
1616dcb4e102SKan Liang 
1617dcb4e102SKan Liang 	return;
1618dcb4e102SKan Liang err:
1619dcb4e102SKan Liang 	perf_pmu__parse_cleanup();
1620dcb4e102SKan Liang }
1621dcb4e102SKan Liang 
1622dcb4e102SKan Liang enum perf_pmu_event_symbol_type
1623dcb4e102SKan Liang perf_pmu__parse_check(const char *name)
1624dcb4e102SKan Liang {
1625dcb4e102SKan Liang 	struct perf_pmu_event_symbol p, *r;
1626dcb4e102SKan Liang 
1627dcb4e102SKan Liang 	/* scan kernel pmu events from sysfs if needed */
1628dcb4e102SKan Liang 	if (perf_pmu_events_list_num == 0)
1629dcb4e102SKan Liang 		perf_pmu__parse_init();
1630dcb4e102SKan Liang 	/*
1631dcb4e102SKan Liang 	 * name "cpu" could be prefix of cpu-cycles or cpu// events.
1632dcb4e102SKan Liang 	 * cpu-cycles has been handled by hardcode.
1633dcb4e102SKan Liang 	 * So it must be cpu// events, not kernel pmu event.
1634dcb4e102SKan Liang 	 */
1635dcb4e102SKan Liang 	if ((perf_pmu_events_list_num <= 0) || !strcmp(name, "cpu"))
1636dcb4e102SKan Liang 		return PMU_EVENT_SYMBOL_ERR;
1637dcb4e102SKan Liang 
1638dcb4e102SKan Liang 	p.symbol = strdup(name);
1639dcb4e102SKan Liang 	r = bsearch(&p, perf_pmu_events_list,
1640dcb4e102SKan Liang 			(size_t) perf_pmu_events_list_num,
1641dcb4e102SKan Liang 			sizeof(struct perf_pmu_event_symbol), comp_pmu);
1642360e071bSTaeung Song 	zfree(&p.symbol);
1643dcb4e102SKan Liang 	return r ? r->type : PMU_EVENT_SYMBOL_ERR;
1644dcb4e102SKan Liang }
1645dcb4e102SKan Liang 
16465d9cdc11SArnaldo Carvalho de Melo static int parse_events__scanner(const char *str, void *parse_state, int start_token)
1647ac20de6fSZheng Yan {
1648ac20de6fSZheng Yan 	YY_BUFFER_STATE buffer;
1649ac20de6fSZheng Yan 	void *scanner;
1650ac20de6fSZheng Yan 	int ret;
1651ac20de6fSZheng Yan 
165290e2b22dSJiri Olsa 	ret = parse_events_lex_init_extra(start_token, &scanner);
1653ac20de6fSZheng Yan 	if (ret)
1654ac20de6fSZheng Yan 		return ret;
1655ac20de6fSZheng Yan 
1656ac20de6fSZheng Yan 	buffer = parse_events__scan_string(str, scanner);
1657ac20de6fSZheng Yan 
1658ac20de6fSZheng Yan #ifdef PARSER_DEBUG
1659ac20de6fSZheng Yan 	parse_events_debug = 1;
1660ac20de6fSZheng Yan #endif
16615d9cdc11SArnaldo Carvalho de Melo 	ret = parse_events_parse(parse_state, scanner);
1662ac20de6fSZheng Yan 
1663ac20de6fSZheng Yan 	parse_events__flush_buffer(buffer, scanner);
1664ac20de6fSZheng Yan 	parse_events__delete_buffer(buffer, scanner);
1665ac20de6fSZheng Yan 	parse_events_lex_destroy(scanner);
1666ac20de6fSZheng Yan 	return ret;
1667ac20de6fSZheng Yan }
1668ac20de6fSZheng Yan 
166990e2b22dSJiri Olsa /*
167090e2b22dSJiri Olsa  * parse event config string, return a list of event terms.
167190e2b22dSJiri Olsa  */
167290e2b22dSJiri Olsa int parse_events_terms(struct list_head *terms, const char *str)
167390e2b22dSJiri Olsa {
16745d9cdc11SArnaldo Carvalho de Melo 	struct parse_events_state parse_state = {
167590e2b22dSJiri Olsa 		.terms = NULL,
167690e2b22dSJiri Olsa 	};
167790e2b22dSJiri Olsa 	int ret;
167890e2b22dSJiri Olsa 
16795d9cdc11SArnaldo Carvalho de Melo 	ret = parse_events__scanner(str, &parse_state, PE_START_TERMS);
168090e2b22dSJiri Olsa 	if (!ret) {
16815d9cdc11SArnaldo Carvalho de Melo 		list_splice(parse_state.terms, terms);
16825d9cdc11SArnaldo Carvalho de Melo 		zfree(&parse_state.terms);
168390e2b22dSJiri Olsa 		return 0;
168490e2b22dSJiri Olsa 	}
168590e2b22dSJiri Olsa 
16865d9cdc11SArnaldo Carvalho de Melo 	parse_events_terms__delete(parse_state.terms);
168790e2b22dSJiri Olsa 	return ret;
168890e2b22dSJiri Olsa }
168990e2b22dSJiri Olsa 
1690b39b8393SJiri Olsa int parse_events(struct perf_evlist *evlist, const char *str,
1691b39b8393SJiri Olsa 		 struct parse_events_error *err)
169286470930SIngo Molnar {
16935d9cdc11SArnaldo Carvalho de Melo 	struct parse_events_state parse_state = {
16945d9cdc11SArnaldo Carvalho de Melo 		.list   = LIST_HEAD_INIT(parse_state.list),
169546010ab2SJiri Olsa 		.idx    = evlist->nr_entries,
1696b39b8393SJiri Olsa 		.error  = err,
16977630b3e2SWang Nan 		.evlist = evlist,
169846010ab2SJiri Olsa 	};
169946010ab2SJiri Olsa 	int ret;
170086470930SIngo Molnar 
17015d9cdc11SArnaldo Carvalho de Melo 	ret = parse_events__scanner(str, &parse_state, PE_START_EVENTS);
1702dcb4e102SKan Liang 	perf_pmu__parse_cleanup();
170389812fc8SJiri Olsa 	if (!ret) {
170415bfd2ccSWang Nan 		struct perf_evsel *last;
170515bfd2ccSWang Nan 
17065d9cdc11SArnaldo Carvalho de Melo 		if (list_empty(&parse_state.list)) {
1707854f7363SWang Nan 			WARN_ONCE(true, "WARNING: event parser found nothing");
1708854f7363SWang Nan 			return -1;
1709854f7363SWang Nan 		}
1710854f7363SWang Nan 
17115d9cdc11SArnaldo Carvalho de Melo 		perf_evlist__splice_list_tail(evlist, &parse_state.list);
17125d9cdc11SArnaldo Carvalho de Melo 		evlist->nr_groups += parse_state.nr_groups;
171315bfd2ccSWang Nan 		last = perf_evlist__last(evlist);
171415bfd2ccSWang Nan 		last->cmdline_group_boundary = true;
171515bfd2ccSWang Nan 
171686470930SIngo Molnar 		return 0;
171786470930SIngo Molnar 	}
171886470930SIngo Molnar 
17195d7be90eSJiri Olsa 	/*
17205d7be90eSJiri Olsa 	 * There are 2 users - builtin-record and builtin-test objects.
17215d7be90eSJiri Olsa 	 * Both call perf_evlist__delete in case of error, so we dont
17225d7be90eSJiri Olsa 	 * need to bother.
17235d7be90eSJiri Olsa 	 */
172489812fc8SJiri Olsa 	return ret;
172589812fc8SJiri Olsa }
172689812fc8SJiri Olsa 
1727b39b8393SJiri Olsa #define MAX_WIDTH 1000
1728b39b8393SJiri Olsa static int get_term_width(void)
1729b39b8393SJiri Olsa {
1730b39b8393SJiri Olsa 	struct winsize ws;
1731b39b8393SJiri Olsa 
1732b39b8393SJiri Olsa 	get_term_dimensions(&ws);
1733b39b8393SJiri Olsa 	return ws.ws_col > MAX_WIDTH ? MAX_WIDTH : ws.ws_col;
1734b39b8393SJiri Olsa }
1735b39b8393SJiri Olsa 
1736b39b8393SJiri Olsa static void parse_events_print_error(struct parse_events_error *err,
1737b39b8393SJiri Olsa 				     const char *event)
1738b39b8393SJiri Olsa {
1739b39b8393SJiri Olsa 	const char *str = "invalid or unsupported event: ";
1740b39b8393SJiri Olsa 	char _buf[MAX_WIDTH];
1741b39b8393SJiri Olsa 	char *buf = (char *) event;
1742b39b8393SJiri Olsa 	int idx = 0;
1743b39b8393SJiri Olsa 
1744b39b8393SJiri Olsa 	if (err->str) {
1745b39b8393SJiri Olsa 		/* -2 for extra '' in the final fprintf */
1746b39b8393SJiri Olsa 		int width       = get_term_width() - 2;
1747b39b8393SJiri Olsa 		int len_event   = strlen(event);
1748b39b8393SJiri Olsa 		int len_str, max_len, cut = 0;
1749b39b8393SJiri Olsa 
1750b39b8393SJiri Olsa 		/*
1751b39b8393SJiri Olsa 		 * Maximum error index indent, we will cut
1752b39b8393SJiri Olsa 		 * the event string if it's bigger.
1753b39b8393SJiri Olsa 		 */
1754141b2d31SAdrian Hunter 		int max_err_idx = 13;
1755b39b8393SJiri Olsa 
1756b39b8393SJiri Olsa 		/*
1757b39b8393SJiri Olsa 		 * Let's be specific with the message when
1758b39b8393SJiri Olsa 		 * we have the precise error.
1759b39b8393SJiri Olsa 		 */
1760b39b8393SJiri Olsa 		str     = "event syntax error: ";
1761b39b8393SJiri Olsa 		len_str = strlen(str);
1762b39b8393SJiri Olsa 		max_len = width - len_str;
1763b39b8393SJiri Olsa 
1764b39b8393SJiri Olsa 		buf = _buf;
1765b39b8393SJiri Olsa 
1766bd1a0be5SAdam Buchbinder 		/* We're cutting from the beginning. */
1767b39b8393SJiri Olsa 		if (err->idx > max_err_idx)
1768b39b8393SJiri Olsa 			cut = err->idx - max_err_idx;
1769b39b8393SJiri Olsa 
1770b39b8393SJiri Olsa 		strncpy(buf, event + cut, max_len);
1771b39b8393SJiri Olsa 
1772b39b8393SJiri Olsa 		/* Mark cut parts with '..' on both sides. */
1773b39b8393SJiri Olsa 		if (cut)
1774b39b8393SJiri Olsa 			buf[0] = buf[1] = '.';
1775b39b8393SJiri Olsa 
1776b39b8393SJiri Olsa 		if ((len_event - cut) > max_len) {
1777b39b8393SJiri Olsa 			buf[max_len - 1] = buf[max_len - 2] = '.';
1778b39b8393SJiri Olsa 			buf[max_len] = 0;
1779b39b8393SJiri Olsa 		}
1780b39b8393SJiri Olsa 
1781b39b8393SJiri Olsa 		idx = len_str + err->idx - cut;
1782b39b8393SJiri Olsa 	}
1783b39b8393SJiri Olsa 
1784b39b8393SJiri Olsa 	fprintf(stderr, "%s'%s'\n", str, buf);
1785b39b8393SJiri Olsa 	if (idx) {
1786b39b8393SJiri Olsa 		fprintf(stderr, "%*s\\___ %s\n", idx + 1, "", err->str);
1787b39b8393SJiri Olsa 		if (err->help)
1788b39b8393SJiri Olsa 			fprintf(stderr, "\n%s\n", err->help);
1789360e071bSTaeung Song 		zfree(&err->str);
1790360e071bSTaeung Song 		zfree(&err->help);
1791b39b8393SJiri Olsa 	}
1792b39b8393SJiri Olsa 
1793b39b8393SJiri Olsa 	fprintf(stderr, "Run 'perf list' for a list of valid events\n");
1794b39b8393SJiri Olsa }
1795b39b8393SJiri Olsa 
1796b39b8393SJiri Olsa #undef MAX_WIDTH
1797b39b8393SJiri Olsa 
1798f120f9d5SJiri Olsa int parse_events_option(const struct option *opt, const char *str,
17991d037ca1SIrina Tirdea 			int unset __maybe_unused)
1800f120f9d5SJiri Olsa {
1801f120f9d5SJiri Olsa 	struct perf_evlist *evlist = *(struct perf_evlist **)opt->value;
1802b39b8393SJiri Olsa 	struct parse_events_error err = { .idx = 0, };
1803b39b8393SJiri Olsa 	int ret = parse_events(evlist, str, &err);
18049175ce1fSAndi Kleen 
1805b39b8393SJiri Olsa 	if (ret)
1806b39b8393SJiri Olsa 		parse_events_print_error(&err, str);
1807b39b8393SJiri Olsa 
18089175ce1fSAndi Kleen 	return ret;
1809f120f9d5SJiri Olsa }
1810f120f9d5SJiri Olsa 
18114ba1faa1SWang Nan static int
18124ba1faa1SWang Nan foreach_evsel_in_last_glob(struct perf_evlist *evlist,
18134ba1faa1SWang Nan 			   int (*func)(struct perf_evsel *evsel,
18144ba1faa1SWang Nan 				       const void *arg),
18154ba1faa1SWang Nan 			   const void *arg)
1816c171b552SLi Zefan {
181769aad6f1SArnaldo Carvalho de Melo 	struct perf_evsel *last = NULL;
18184ba1faa1SWang Nan 	int err;
1819c171b552SLi Zefan 
1820854f7363SWang Nan 	/*
1821854f7363SWang Nan 	 * Don't return when list_empty, give func a chance to report
1822854f7363SWang Nan 	 * error when it found last == NULL.
1823854f7363SWang Nan 	 *
1824854f7363SWang Nan 	 * So no need to WARN here, let *func do this.
1825854f7363SWang Nan 	 */
1826361c99a6SArnaldo Carvalho de Melo 	if (evlist->nr_entries > 0)
18270c21f736SArnaldo Carvalho de Melo 		last = perf_evlist__last(evlist);
182869aad6f1SArnaldo Carvalho de Melo 
182915bfd2ccSWang Nan 	do {
18304ba1faa1SWang Nan 		err = (*func)(last, arg);
18314ba1faa1SWang Nan 		if (err)
1832c171b552SLi Zefan 			return -1;
18334ba1faa1SWang Nan 		if (!last)
18344ba1faa1SWang Nan 			return 0;
1835c171b552SLi Zefan 
183615bfd2ccSWang Nan 		if (last->node.prev == &evlist->entries)
183715bfd2ccSWang Nan 			return 0;
183815bfd2ccSWang Nan 		last = list_entry(last->node.prev, struct perf_evsel, node);
183915bfd2ccSWang Nan 	} while (!last->cmdline_group_boundary);
184015bfd2ccSWang Nan 
1841c171b552SLi Zefan 	return 0;
1842c171b552SLi Zefan }
1843c171b552SLi Zefan 
18444ba1faa1SWang Nan static int set_filter(struct perf_evsel *evsel, const void *arg)
18454ba1faa1SWang Nan {
18464ba1faa1SWang Nan 	const char *str = arg;
18471e857484SMathieu Poirier 	bool found = false;
18481e857484SMathieu Poirier 	int nr_addr_filters = 0;
18491e857484SMathieu Poirier 	struct perf_pmu *pmu = NULL;
18504ba1faa1SWang Nan 
18511e857484SMathieu Poirier 	if (evsel == NULL)
18521e857484SMathieu Poirier 		goto err;
18534ba1faa1SWang Nan 
18541e857484SMathieu Poirier 	if (evsel->attr.type == PERF_TYPE_TRACEPOINT) {
18553541c034SMathieu Poirier 		if (perf_evsel__append_tp_filter(evsel, str) < 0) {
18564ba1faa1SWang Nan 			fprintf(stderr,
18574ba1faa1SWang Nan 				"not enough memory to hold filter string\n");
18584ba1faa1SWang Nan 			return -1;
18594ba1faa1SWang Nan 		}
18604ba1faa1SWang Nan 
18614ba1faa1SWang Nan 		return 0;
18624ba1faa1SWang Nan 	}
18634ba1faa1SWang Nan 
18641e857484SMathieu Poirier 	while ((pmu = perf_pmu__scan(pmu)) != NULL)
18651e857484SMathieu Poirier 		if (pmu->type == evsel->attr.type) {
18661e857484SMathieu Poirier 			found = true;
18671e857484SMathieu Poirier 			break;
18681e857484SMathieu Poirier 		}
18691e857484SMathieu Poirier 
18701e857484SMathieu Poirier 	if (found)
18711e857484SMathieu Poirier 		perf_pmu__scan_file(pmu, "nr_addr_filters",
18721e857484SMathieu Poirier 				    "%d", &nr_addr_filters);
18731e857484SMathieu Poirier 
18741e857484SMathieu Poirier 	if (!nr_addr_filters)
18751e857484SMathieu Poirier 		goto err;
18761e857484SMathieu Poirier 
18771e857484SMathieu Poirier 	if (perf_evsel__append_addr_filter(evsel, str) < 0) {
18781e857484SMathieu Poirier 		fprintf(stderr,
18791e857484SMathieu Poirier 			"not enough memory to hold filter string\n");
18801e857484SMathieu Poirier 		return -1;
18811e857484SMathieu Poirier 	}
18821e857484SMathieu Poirier 
18831e857484SMathieu Poirier 	return 0;
18841e857484SMathieu Poirier 
18851e857484SMathieu Poirier err:
18861e857484SMathieu Poirier 	fprintf(stderr,
18871e857484SMathieu Poirier 		"--filter option should follow a -e tracepoint or HW tracer option\n");
18881e857484SMathieu Poirier 
18891e857484SMathieu Poirier 	return -1;
18901e857484SMathieu Poirier }
18911e857484SMathieu Poirier 
18924ba1faa1SWang Nan int parse_filter(const struct option *opt, const char *str,
18934ba1faa1SWang Nan 		 int unset __maybe_unused)
18944ba1faa1SWang Nan {
18954ba1faa1SWang Nan 	struct perf_evlist *evlist = *(struct perf_evlist **)opt->value;
18964ba1faa1SWang Nan 
18974ba1faa1SWang Nan 	return foreach_evsel_in_last_glob(evlist, set_filter,
18984ba1faa1SWang Nan 					  (const void *)str);
18994ba1faa1SWang Nan }
19004ba1faa1SWang Nan 
19014ba1faa1SWang Nan static int add_exclude_perf_filter(struct perf_evsel *evsel,
19024ba1faa1SWang Nan 				   const void *arg __maybe_unused)
19034ba1faa1SWang Nan {
19044ba1faa1SWang Nan 	char new_filter[64];
19054ba1faa1SWang Nan 
19064ba1faa1SWang Nan 	if (evsel == NULL || evsel->attr.type != PERF_TYPE_TRACEPOINT) {
19074ba1faa1SWang Nan 		fprintf(stderr,
19084ba1faa1SWang Nan 			"--exclude-perf option should follow a -e tracepoint option\n");
19094ba1faa1SWang Nan 		return -1;
19104ba1faa1SWang Nan 	}
19114ba1faa1SWang Nan 
19124ba1faa1SWang Nan 	snprintf(new_filter, sizeof(new_filter), "common_pid != %d", getpid());
19134ba1faa1SWang Nan 
19143541c034SMathieu Poirier 	if (perf_evsel__append_tp_filter(evsel, new_filter) < 0) {
19154ba1faa1SWang Nan 		fprintf(stderr,
19164ba1faa1SWang Nan 			"not enough memory to hold filter string\n");
19174ba1faa1SWang Nan 		return -1;
19184ba1faa1SWang Nan 	}
19194ba1faa1SWang Nan 
19204ba1faa1SWang Nan 	return 0;
19214ba1faa1SWang Nan }
19224ba1faa1SWang Nan 
19234ba1faa1SWang Nan int exclude_perf(const struct option *opt,
19244ba1faa1SWang Nan 		 const char *arg __maybe_unused,
19254ba1faa1SWang Nan 		 int unset __maybe_unused)
19264ba1faa1SWang Nan {
19274ba1faa1SWang Nan 	struct perf_evlist *evlist = *(struct perf_evlist **)opt->value;
19284ba1faa1SWang Nan 
19294ba1faa1SWang Nan 	return foreach_evsel_in_last_glob(evlist, add_exclude_perf_filter,
19304ba1faa1SWang Nan 					  NULL);
19314ba1faa1SWang Nan }
19324ba1faa1SWang Nan 
193386470930SIngo Molnar static const char * const event_type_descriptors[] = {
193486470930SIngo Molnar 	"Hardware event",
193586470930SIngo Molnar 	"Software event",
193686470930SIngo Molnar 	"Tracepoint event",
193786470930SIngo Molnar 	"Hardware cache event",
193841bdcb23SLiming Wang 	"Raw hardware event descriptor",
193941bdcb23SLiming Wang 	"Hardware breakpoint",
194086470930SIngo Molnar };
194186470930SIngo Molnar 
1942ab0e4800SYunlong Song static int cmp_string(const void *a, const void *b)
1943ab0e4800SYunlong Song {
1944ab0e4800SYunlong Song 	const char * const *as = a;
1945ab0e4800SYunlong Song 	const char * const *bs = b;
1946ab0e4800SYunlong Song 
1947ab0e4800SYunlong Song 	return strcmp(*as, *bs);
1948ab0e4800SYunlong Song }
1949ab0e4800SYunlong Song 
195086470930SIngo Molnar /*
1951f6bdafefSJason Baron  * Print the events from <debugfs_mount_point>/tracing/events
1952f6bdafefSJason Baron  */
1953f6bdafefSJason Baron 
1954a3277d2dSFrederic Weisbecker void print_tracepoint_events(const char *subsys_glob, const char *event_glob,
1955a3277d2dSFrederic Weisbecker 			     bool name_only)
1956f6bdafefSJason Baron {
1957f6bdafefSJason Baron 	DIR *sys_dir, *evt_dir;
195822a9f41bSArnaldo Carvalho de Melo 	struct dirent *sys_dirent, *evt_dirent;
1959f6bdafefSJason Baron 	char evt_path[MAXPATHLEN];
1960725b1368SEric Dumazet 	char dir_path[MAXPATHLEN];
1961ab0e4800SYunlong Song 	char **evt_list = NULL;
1962ab0e4800SYunlong Song 	unsigned int evt_i = 0, evt_num = 0;
1963ab0e4800SYunlong Song 	bool evt_num_known = false;
1964f6bdafefSJason Baron 
1965ab0e4800SYunlong Song restart:
1966ebf294bfSArnaldo Carvalho de Melo 	sys_dir = opendir(tracing_events_path);
1967f6bdafefSJason Baron 	if (!sys_dir)
1968725b1368SEric Dumazet 		return;
1969f6bdafefSJason Baron 
1970ab0e4800SYunlong Song 	if (evt_num_known) {
1971ab0e4800SYunlong Song 		evt_list = zalloc(sizeof(char *) * evt_num);
1972ab0e4800SYunlong Song 		if (!evt_list)
1973ab0e4800SYunlong Song 			goto out_close_sys_dir;
1974ab0e4800SYunlong Song 	}
1975ab0e4800SYunlong Song 
197622a9f41bSArnaldo Carvalho de Melo 	for_each_subsystem(sys_dir, sys_dirent) {
1977668b8788SArnaldo Carvalho de Melo 		if (subsys_glob != NULL &&
197822a9f41bSArnaldo Carvalho de Melo 		    !strglobmatch(sys_dirent->d_name, subsys_glob))
1979668b8788SArnaldo Carvalho de Melo 			continue;
1980725b1368SEric Dumazet 
1981ebf294bfSArnaldo Carvalho de Melo 		snprintf(dir_path, MAXPATHLEN, "%s/%s", tracing_events_path,
198222a9f41bSArnaldo Carvalho de Melo 			 sys_dirent->d_name);
1983725b1368SEric Dumazet 		evt_dir = opendir(dir_path);
1984725b1368SEric Dumazet 		if (!evt_dir)
19856b58e7f1SUlrich Drepper 			continue;
1986725b1368SEric Dumazet 
198722a9f41bSArnaldo Carvalho de Melo 		for_each_event(sys_dirent, evt_dir, evt_dirent) {
1988668b8788SArnaldo Carvalho de Melo 			if (event_glob != NULL &&
198922a9f41bSArnaldo Carvalho de Melo 			    !strglobmatch(evt_dirent->d_name, event_glob))
1990668b8788SArnaldo Carvalho de Melo 				continue;
1991668b8788SArnaldo Carvalho de Melo 
1992ab0e4800SYunlong Song 			if (!evt_num_known) {
1993ab0e4800SYunlong Song 				evt_num++;
1994a3277d2dSFrederic Weisbecker 				continue;
1995a3277d2dSFrederic Weisbecker 			}
1996a3277d2dSFrederic Weisbecker 
1997f6bdafefSJason Baron 			snprintf(evt_path, MAXPATHLEN, "%s:%s",
199822a9f41bSArnaldo Carvalho de Melo 				 sys_dirent->d_name, evt_dirent->d_name);
1999ab0e4800SYunlong Song 
2000ab0e4800SYunlong Song 			evt_list[evt_i] = strdup(evt_path);
2001ab0e4800SYunlong Song 			if (evt_list[evt_i] == NULL)
2002ab0e4800SYunlong Song 				goto out_close_evt_dir;
2003ab0e4800SYunlong Song 			evt_i++;
2004f6bdafefSJason Baron 		}
2005f6bdafefSJason Baron 		closedir(evt_dir);
2006f6bdafefSJason Baron 	}
2007f6bdafefSJason Baron 	closedir(sys_dir);
2008ab0e4800SYunlong Song 
2009ab0e4800SYunlong Song 	if (!evt_num_known) {
2010ab0e4800SYunlong Song 		evt_num_known = true;
2011ab0e4800SYunlong Song 		goto restart;
2012ab0e4800SYunlong Song 	}
2013ab0e4800SYunlong Song 	qsort(evt_list, evt_num, sizeof(char *), cmp_string);
2014ab0e4800SYunlong Song 	evt_i = 0;
2015ab0e4800SYunlong Song 	while (evt_i < evt_num) {
2016ab0e4800SYunlong Song 		if (name_only) {
2017ab0e4800SYunlong Song 			printf("%s ", evt_list[evt_i++]);
2018ab0e4800SYunlong Song 			continue;
2019ab0e4800SYunlong Song 		}
2020ab0e4800SYunlong Song 		printf("  %-50s [%s]\n", evt_list[evt_i++],
2021ab0e4800SYunlong Song 				event_type_descriptors[PERF_TYPE_TRACEPOINT]);
2022ab0e4800SYunlong Song 	}
2023dfc431cbSArnaldo Carvalho de Melo 	if (evt_num && pager_in_use())
2024ab0e4800SYunlong Song 		printf("\n");
2025ab0e4800SYunlong Song 
2026ab0e4800SYunlong Song out_free:
2027ab0e4800SYunlong Song 	evt_num = evt_i;
2028ab0e4800SYunlong Song 	for (evt_i = 0; evt_i < evt_num; evt_i++)
2029ab0e4800SYunlong Song 		zfree(&evt_list[evt_i]);
2030ab0e4800SYunlong Song 	zfree(&evt_list);
2031ab0e4800SYunlong Song 	return;
2032ab0e4800SYunlong Song 
2033ab0e4800SYunlong Song out_close_evt_dir:
2034ab0e4800SYunlong Song 	closedir(evt_dir);
2035ab0e4800SYunlong Song out_close_sys_dir:
2036ab0e4800SYunlong Song 	closedir(sys_dir);
2037ab0e4800SYunlong Song 
2038ab0e4800SYunlong Song 	printf("FATAL: not enough memory to print %s\n",
2039ab0e4800SYunlong Song 			event_type_descriptors[PERF_TYPE_TRACEPOINT]);
2040ab0e4800SYunlong Song 	if (evt_list)
2041ab0e4800SYunlong Song 		goto out_free;
2042f6bdafefSJason Baron }
2043f6bdafefSJason Baron 
2044f6bdafefSJason Baron /*
204520c457b8SThomas Renninger  * Check whether event is in <debugfs_mount_point>/tracing/events
204620c457b8SThomas Renninger  */
204720c457b8SThomas Renninger 
204820c457b8SThomas Renninger int is_valid_tracepoint(const char *event_string)
204920c457b8SThomas Renninger {
205020c457b8SThomas Renninger 	DIR *sys_dir, *evt_dir;
205122a9f41bSArnaldo Carvalho de Melo 	struct dirent *sys_dirent, *evt_dirent;
205220c457b8SThomas Renninger 	char evt_path[MAXPATHLEN];
205320c457b8SThomas Renninger 	char dir_path[MAXPATHLEN];
205420c457b8SThomas Renninger 
2055ebf294bfSArnaldo Carvalho de Melo 	sys_dir = opendir(tracing_events_path);
205620c457b8SThomas Renninger 	if (!sys_dir)
205720c457b8SThomas Renninger 		return 0;
205820c457b8SThomas Renninger 
205922a9f41bSArnaldo Carvalho de Melo 	for_each_subsystem(sys_dir, sys_dirent) {
206020c457b8SThomas Renninger 
2061ebf294bfSArnaldo Carvalho de Melo 		snprintf(dir_path, MAXPATHLEN, "%s/%s", tracing_events_path,
206222a9f41bSArnaldo Carvalho de Melo 			 sys_dirent->d_name);
206320c457b8SThomas Renninger 		evt_dir = opendir(dir_path);
206420c457b8SThomas Renninger 		if (!evt_dir)
206520c457b8SThomas Renninger 			continue;
206620c457b8SThomas Renninger 
206722a9f41bSArnaldo Carvalho de Melo 		for_each_event(sys_dirent, evt_dir, evt_dirent) {
206820c457b8SThomas Renninger 			snprintf(evt_path, MAXPATHLEN, "%s:%s",
206922a9f41bSArnaldo Carvalho de Melo 				 sys_dirent->d_name, evt_dirent->d_name);
207020c457b8SThomas Renninger 			if (!strcmp(evt_path, event_string)) {
207120c457b8SThomas Renninger 				closedir(evt_dir);
207220c457b8SThomas Renninger 				closedir(sys_dir);
207320c457b8SThomas Renninger 				return 1;
207420c457b8SThomas Renninger 			}
207520c457b8SThomas Renninger 		}
207620c457b8SThomas Renninger 		closedir(evt_dir);
207720c457b8SThomas Renninger 	}
207820c457b8SThomas Renninger 	closedir(sys_dir);
207920c457b8SThomas Renninger 	return 0;
208020c457b8SThomas Renninger }
208120c457b8SThomas Renninger 
2082b41f1cecSNamhyung Kim static bool is_event_supported(u8 type, unsigned config)
2083b41f1cecSNamhyung Kim {
2084b41f1cecSNamhyung Kim 	bool ret = true;
208588fee52eSVince Weaver 	int open_return;
2086b41f1cecSNamhyung Kim 	struct perf_evsel *evsel;
2087b41f1cecSNamhyung Kim 	struct perf_event_attr attr = {
2088b41f1cecSNamhyung Kim 		.type = type,
2089b41f1cecSNamhyung Kim 		.config = config,
2090b41f1cecSNamhyung Kim 		.disabled = 1,
2091b41f1cecSNamhyung Kim 	};
209289896051SArnaldo Carvalho de Melo 	struct thread_map *tmap = thread_map__new_by_tid(0);
209389896051SArnaldo Carvalho de Melo 
209489896051SArnaldo Carvalho de Melo 	if (tmap == NULL)
209589896051SArnaldo Carvalho de Melo 		return false;
2096b41f1cecSNamhyung Kim 
2097ef503831SArnaldo Carvalho de Melo 	evsel = perf_evsel__new(&attr);
2098b41f1cecSNamhyung Kim 	if (evsel) {
209989896051SArnaldo Carvalho de Melo 		open_return = perf_evsel__open(evsel, NULL, tmap);
210088fee52eSVince Weaver 		ret = open_return >= 0;
210188fee52eSVince Weaver 
210288fee52eSVince Weaver 		if (open_return == -EACCES) {
210388fee52eSVince Weaver 			/*
210488fee52eSVince Weaver 			 * This happens if the paranoid value
210588fee52eSVince Weaver 			 * /proc/sys/kernel/perf_event_paranoid is set to 2
210688fee52eSVince Weaver 			 * Re-run with exclude_kernel set; we don't do that
210788fee52eSVince Weaver 			 * by default as some ARM machines do not support it.
210888fee52eSVince Weaver 			 *
210988fee52eSVince Weaver 			 */
211088fee52eSVince Weaver 			evsel->attr.exclude_kernel = 1;
211189896051SArnaldo Carvalho de Melo 			ret = perf_evsel__open(evsel, NULL, tmap) >= 0;
211288fee52eSVince Weaver 		}
2113b41f1cecSNamhyung Kim 		perf_evsel__delete(evsel);
2114b41f1cecSNamhyung Kim 	}
2115b41f1cecSNamhyung Kim 
2116b41f1cecSNamhyung Kim 	return ret;
2117b41f1cecSNamhyung Kim }
2118b41f1cecSNamhyung Kim 
211940218daeSMasami Hiramatsu void print_sdt_events(const char *subsys_glob, const char *event_glob,
212040218daeSMasami Hiramatsu 		      bool name_only)
212140218daeSMasami Hiramatsu {
212240218daeSMasami Hiramatsu 	struct probe_cache *pcache;
212340218daeSMasami Hiramatsu 	struct probe_cache_entry *ent;
212440218daeSMasami Hiramatsu 	struct strlist *bidlist, *sdtlist;
212540218daeSMasami Hiramatsu 	struct strlist_config cfg = {.dont_dupstr = true};
212640218daeSMasami Hiramatsu 	struct str_node *nd, *nd2;
212740218daeSMasami Hiramatsu 	char *buf, *path, *ptr = NULL;
212840218daeSMasami Hiramatsu 	bool show_detail = false;
212940218daeSMasami Hiramatsu 	int ret;
213040218daeSMasami Hiramatsu 
213140218daeSMasami Hiramatsu 	sdtlist = strlist__new(NULL, &cfg);
213240218daeSMasami Hiramatsu 	if (!sdtlist) {
213340218daeSMasami Hiramatsu 		pr_debug("Failed to allocate new strlist for SDT\n");
213440218daeSMasami Hiramatsu 		return;
213540218daeSMasami Hiramatsu 	}
213640218daeSMasami Hiramatsu 	bidlist = build_id_cache__list_all(true);
213740218daeSMasami Hiramatsu 	if (!bidlist) {
213840218daeSMasami Hiramatsu 		pr_debug("Failed to get buildids: %d\n", errno);
213940218daeSMasami Hiramatsu 		return;
214040218daeSMasami Hiramatsu 	}
214140218daeSMasami Hiramatsu 	strlist__for_each_entry(nd, bidlist) {
2142f045b8c4SKrister Johansen 		pcache = probe_cache__new(nd->s, NULL);
214340218daeSMasami Hiramatsu 		if (!pcache)
214440218daeSMasami Hiramatsu 			continue;
214540218daeSMasami Hiramatsu 		list_for_each_entry(ent, &pcache->entries, node) {
214640218daeSMasami Hiramatsu 			if (!ent->sdt)
214740218daeSMasami Hiramatsu 				continue;
214840218daeSMasami Hiramatsu 			if (subsys_glob &&
214940218daeSMasami Hiramatsu 			    !strglobmatch(ent->pev.group, subsys_glob))
215040218daeSMasami Hiramatsu 				continue;
215140218daeSMasami Hiramatsu 			if (event_glob &&
215240218daeSMasami Hiramatsu 			    !strglobmatch(ent->pev.event, event_glob))
215340218daeSMasami Hiramatsu 				continue;
215440218daeSMasami Hiramatsu 			ret = asprintf(&buf, "%s:%s@%s", ent->pev.group,
215540218daeSMasami Hiramatsu 					ent->pev.event, nd->s);
215640218daeSMasami Hiramatsu 			if (ret > 0)
215740218daeSMasami Hiramatsu 				strlist__add(sdtlist, buf);
215840218daeSMasami Hiramatsu 		}
215940218daeSMasami Hiramatsu 		probe_cache__delete(pcache);
216040218daeSMasami Hiramatsu 	}
216140218daeSMasami Hiramatsu 	strlist__delete(bidlist);
216240218daeSMasami Hiramatsu 
216340218daeSMasami Hiramatsu 	strlist__for_each_entry(nd, sdtlist) {
216440218daeSMasami Hiramatsu 		buf = strchr(nd->s, '@');
216540218daeSMasami Hiramatsu 		if (buf)
216640218daeSMasami Hiramatsu 			*(buf++) = '\0';
216740218daeSMasami Hiramatsu 		if (name_only) {
216840218daeSMasami Hiramatsu 			printf("%s ", nd->s);
216940218daeSMasami Hiramatsu 			continue;
217040218daeSMasami Hiramatsu 		}
217140218daeSMasami Hiramatsu 		nd2 = strlist__next(nd);
217240218daeSMasami Hiramatsu 		if (nd2) {
217340218daeSMasami Hiramatsu 			ptr = strchr(nd2->s, '@');
217440218daeSMasami Hiramatsu 			if (ptr)
217540218daeSMasami Hiramatsu 				*ptr = '\0';
217640218daeSMasami Hiramatsu 			if (strcmp(nd->s, nd2->s) == 0)
217740218daeSMasami Hiramatsu 				show_detail = true;
217840218daeSMasami Hiramatsu 		}
217940218daeSMasami Hiramatsu 		if (show_detail) {
218040218daeSMasami Hiramatsu 			path = build_id_cache__origname(buf);
218140218daeSMasami Hiramatsu 			ret = asprintf(&buf, "%s@%s(%.12s)", nd->s, path, buf);
218240218daeSMasami Hiramatsu 			if (ret > 0) {
218340218daeSMasami Hiramatsu 				printf("  %-50s [%s]\n", buf, "SDT event");
218440218daeSMasami Hiramatsu 				free(buf);
218540218daeSMasami Hiramatsu 			}
218640218daeSMasami Hiramatsu 		} else
218740218daeSMasami Hiramatsu 			printf("  %-50s [%s]\n", nd->s, "SDT event");
218840218daeSMasami Hiramatsu 		if (nd2) {
218940218daeSMasami Hiramatsu 			if (strcmp(nd->s, nd2->s) != 0)
219040218daeSMasami Hiramatsu 				show_detail = false;
219140218daeSMasami Hiramatsu 			if (ptr)
219240218daeSMasami Hiramatsu 				*ptr = '@';
219340218daeSMasami Hiramatsu 		}
219440218daeSMasami Hiramatsu 	}
219540218daeSMasami Hiramatsu 	strlist__delete(sdtlist);
219640218daeSMasami Hiramatsu }
219740218daeSMasami Hiramatsu 
2198a3277d2dSFrederic Weisbecker int print_hwcache_events(const char *event_glob, bool name_only)
2199668b8788SArnaldo Carvalho de Melo {
2200ab0e4800SYunlong Song 	unsigned int type, op, i, evt_i = 0, evt_num = 0;
22010b668bc9SArnaldo Carvalho de Melo 	char name[64];
2202ab0e4800SYunlong Song 	char **evt_list = NULL;
2203ab0e4800SYunlong Song 	bool evt_num_known = false;
2204ab0e4800SYunlong Song 
2205ab0e4800SYunlong Song restart:
2206ab0e4800SYunlong Song 	if (evt_num_known) {
2207ab0e4800SYunlong Song 		evt_list = zalloc(sizeof(char *) * evt_num);
2208ab0e4800SYunlong Song 		if (!evt_list)
2209ab0e4800SYunlong Song 			goto out_enomem;
2210ab0e4800SYunlong Song 	}
2211668b8788SArnaldo Carvalho de Melo 
2212668b8788SArnaldo Carvalho de Melo 	for (type = 0; type < PERF_COUNT_HW_CACHE_MAX; type++) {
2213668b8788SArnaldo Carvalho de Melo 		for (op = 0; op < PERF_COUNT_HW_CACHE_OP_MAX; op++) {
2214668b8788SArnaldo Carvalho de Melo 			/* skip invalid cache type */
22150b668bc9SArnaldo Carvalho de Melo 			if (!perf_evsel__is_cache_op_valid(type, op))
2216668b8788SArnaldo Carvalho de Melo 				continue;
2217668b8788SArnaldo Carvalho de Melo 
2218668b8788SArnaldo Carvalho de Melo 			for (i = 0; i < PERF_COUNT_HW_CACHE_RESULT_MAX; i++) {
22190b668bc9SArnaldo Carvalho de Melo 				__perf_evsel__hw_cache_type_op_res_name(type, op, i,
22200b668bc9SArnaldo Carvalho de Melo 									name, sizeof(name));
2221947b4ad1SIngo Molnar 				if (event_glob != NULL && !strglobmatch(name, event_glob))
2222668b8788SArnaldo Carvalho de Melo 					continue;
2223668b8788SArnaldo Carvalho de Melo 
2224b41f1cecSNamhyung Kim 				if (!is_event_supported(PERF_TYPE_HW_CACHE,
2225b41f1cecSNamhyung Kim 							type | (op << 8) | (i << 16)))
2226b41f1cecSNamhyung Kim 					continue;
2227b41f1cecSNamhyung Kim 
2228ab0e4800SYunlong Song 				if (!evt_num_known) {
2229ab0e4800SYunlong Song 					evt_num++;
2230ab0e4800SYunlong Song 					continue;
2231ab0e4800SYunlong Song 				}
2232ab0e4800SYunlong Song 
2233ab0e4800SYunlong Song 				evt_list[evt_i] = strdup(name);
2234ab0e4800SYunlong Song 				if (evt_list[evt_i] == NULL)
2235ab0e4800SYunlong Song 					goto out_enomem;
2236ab0e4800SYunlong Song 				evt_i++;
2237668b8788SArnaldo Carvalho de Melo 			}
2238668b8788SArnaldo Carvalho de Melo 		}
2239668b8788SArnaldo Carvalho de Melo 	}
2240668b8788SArnaldo Carvalho de Melo 
2241ab0e4800SYunlong Song 	if (!evt_num_known) {
2242ab0e4800SYunlong Song 		evt_num_known = true;
2243ab0e4800SYunlong Song 		goto restart;
2244ab0e4800SYunlong Song 	}
2245ab0e4800SYunlong Song 	qsort(evt_list, evt_num, sizeof(char *), cmp_string);
2246ab0e4800SYunlong Song 	evt_i = 0;
2247ab0e4800SYunlong Song 	while (evt_i < evt_num) {
2248ab0e4800SYunlong Song 		if (name_only) {
2249ab0e4800SYunlong Song 			printf("%s ", evt_list[evt_i++]);
2250ab0e4800SYunlong Song 			continue;
2251ab0e4800SYunlong Song 		}
2252ab0e4800SYunlong Song 		printf("  %-50s [%s]\n", evt_list[evt_i++],
2253ab0e4800SYunlong Song 				event_type_descriptors[PERF_TYPE_HW_CACHE]);
2254ab0e4800SYunlong Song 	}
2255dfc431cbSArnaldo Carvalho de Melo 	if (evt_num && pager_in_use())
2256dc098b35SAndi Kleen 		printf("\n");
2257ab0e4800SYunlong Song 
2258ab0e4800SYunlong Song out_free:
2259ab0e4800SYunlong Song 	evt_num = evt_i;
2260ab0e4800SYunlong Song 	for (evt_i = 0; evt_i < evt_num; evt_i++)
2261ab0e4800SYunlong Song 		zfree(&evt_list[evt_i]);
2262ab0e4800SYunlong Song 	zfree(&evt_list);
2263ab0e4800SYunlong Song 	return evt_num;
2264ab0e4800SYunlong Song 
2265ab0e4800SYunlong Song out_enomem:
2266ab0e4800SYunlong Song 	printf("FATAL: not enough memory to print %s\n", event_type_descriptors[PERF_TYPE_HW_CACHE]);
2267ab0e4800SYunlong Song 	if (evt_list)
2268ab0e4800SYunlong Song 		goto out_free;
2269ab0e4800SYunlong Song 	return evt_num;
2270668b8788SArnaldo Carvalho de Melo }
2271668b8788SArnaldo Carvalho de Melo 
2272705750f2SYunlong Song void print_symbol_events(const char *event_glob, unsigned type,
2273a3277d2dSFrederic Weisbecker 				struct event_symbol *syms, unsigned max,
2274a3277d2dSFrederic Weisbecker 				bool name_only)
227586470930SIngo Molnar {
2276ab0e4800SYunlong Song 	unsigned int i, evt_i = 0, evt_num = 0;
2277947b4ad1SIngo Molnar 	char name[MAX_NAME_LEN];
2278ab0e4800SYunlong Song 	char **evt_list = NULL;
2279ab0e4800SYunlong Song 	bool evt_num_known = false;
2280ab0e4800SYunlong Song 
2281ab0e4800SYunlong Song restart:
2282ab0e4800SYunlong Song 	if (evt_num_known) {
2283ab0e4800SYunlong Song 		evt_list = zalloc(sizeof(char *) * evt_num);
2284ab0e4800SYunlong Song 		if (!evt_list)
2285ab0e4800SYunlong Song 			goto out_enomem;
2286ab0e4800SYunlong Song 		syms -= max;
2287ab0e4800SYunlong Song 	}
228886470930SIngo Molnar 
22891dc12760SJiri Olsa 	for (i = 0; i < max; i++, syms++) {
2290668b8788SArnaldo Carvalho de Melo 
2291e37df6c7SArnaldo Carvalho de Melo 		if (event_glob != NULL && syms->symbol != NULL &&
2292668b8788SArnaldo Carvalho de Melo 		    !(strglobmatch(syms->symbol, event_glob) ||
2293668b8788SArnaldo Carvalho de Melo 		      (syms->alias && strglobmatch(syms->alias, event_glob))))
2294668b8788SArnaldo Carvalho de Melo 			continue;
229586470930SIngo Molnar 
2296b41f1cecSNamhyung Kim 		if (!is_event_supported(type, i))
2297b41f1cecSNamhyung Kim 			continue;
2298b41f1cecSNamhyung Kim 
2299ab0e4800SYunlong Song 		if (!evt_num_known) {
2300ab0e4800SYunlong Song 			evt_num++;
2301a3277d2dSFrederic Weisbecker 			continue;
2302a3277d2dSFrederic Weisbecker 		}
2303a3277d2dSFrederic Weisbecker 
2304ab0e4800SYunlong Song 		if (!name_only && strlen(syms->alias))
2305947b4ad1SIngo Molnar 			snprintf(name, MAX_NAME_LEN, "%s OR %s", syms->symbol, syms->alias);
230674d5b588SJaswinder Singh Rajput 		else
2307947b4ad1SIngo Molnar 			strncpy(name, syms->symbol, MAX_NAME_LEN);
230886470930SIngo Molnar 
2309ab0e4800SYunlong Song 		evt_list[evt_i] = strdup(name);
2310ab0e4800SYunlong Song 		if (evt_list[evt_i] == NULL)
2311ab0e4800SYunlong Song 			goto out_enomem;
2312ab0e4800SYunlong Song 		evt_i++;
231386470930SIngo Molnar 	}
231486470930SIngo Molnar 
2315ab0e4800SYunlong Song 	if (!evt_num_known) {
2316ab0e4800SYunlong Song 		evt_num_known = true;
2317ab0e4800SYunlong Song 		goto restart;
2318ab0e4800SYunlong Song 	}
2319ab0e4800SYunlong Song 	qsort(evt_list, evt_num, sizeof(char *), cmp_string);
2320ab0e4800SYunlong Song 	evt_i = 0;
2321ab0e4800SYunlong Song 	while (evt_i < evt_num) {
2322ab0e4800SYunlong Song 		if (name_only) {
2323ab0e4800SYunlong Song 			printf("%s ", evt_list[evt_i++]);
2324ab0e4800SYunlong Song 			continue;
2325ab0e4800SYunlong Song 		}
2326ab0e4800SYunlong Song 		printf("  %-50s [%s]\n", evt_list[evt_i++], event_type_descriptors[type]);
2327ab0e4800SYunlong Song 	}
2328dfc431cbSArnaldo Carvalho de Melo 	if (evt_num && pager_in_use())
2329689d3018SMarti Raudsepp 		printf("\n");
2330ab0e4800SYunlong Song 
2331ab0e4800SYunlong Song out_free:
2332ab0e4800SYunlong Song 	evt_num = evt_i;
2333ab0e4800SYunlong Song 	for (evt_i = 0; evt_i < evt_num; evt_i++)
2334ab0e4800SYunlong Song 		zfree(&evt_list[evt_i]);
2335ab0e4800SYunlong Song 	zfree(&evt_list);
2336ab0e4800SYunlong Song 	return;
2337ab0e4800SYunlong Song 
2338ab0e4800SYunlong Song out_enomem:
2339ab0e4800SYunlong Song 	printf("FATAL: not enough memory to print %s\n", event_type_descriptors[type]);
2340ab0e4800SYunlong Song 	if (evt_list)
2341ab0e4800SYunlong Song 		goto out_free;
2342668b8788SArnaldo Carvalho de Melo }
23431dc12760SJiri Olsa 
23441dc12760SJiri Olsa /*
23451dc12760SJiri Olsa  * Print the help text for the event symbols:
23461dc12760SJiri Olsa  */
2347c8d6828aSSukadev Bhattiprolu void print_events(const char *event_glob, bool name_only, bool quiet_flag,
2348bf874fcfSAndi Kleen 			bool long_desc, bool details_flag)
23491dc12760SJiri Olsa {
23501dc12760SJiri Olsa 	print_symbol_events(event_glob, PERF_TYPE_HARDWARE,
2351a3277d2dSFrederic Weisbecker 			    event_symbols_hw, PERF_COUNT_HW_MAX, name_only);
23521dc12760SJiri Olsa 
23531dc12760SJiri Olsa 	print_symbol_events(event_glob, PERF_TYPE_SOFTWARE,
2354a3277d2dSFrederic Weisbecker 			    event_symbols_sw, PERF_COUNT_SW_MAX, name_only);
23551dc12760SJiri Olsa 
2356a3277d2dSFrederic Weisbecker 	print_hwcache_events(event_glob, name_only);
235773c24cb8SJaswinder Singh Rajput 
2358bf874fcfSAndi Kleen 	print_pmu_events(event_glob, name_only, quiet_flag, long_desc,
2359bf874fcfSAndi Kleen 			details_flag);
2360dc098b35SAndi Kleen 
2361668b8788SArnaldo Carvalho de Melo 	if (event_glob != NULL)
2362668b8788SArnaldo Carvalho de Melo 		return;
236373c24cb8SJaswinder Singh Rajput 
2364a3277d2dSFrederic Weisbecker 	if (!name_only) {
2365947b4ad1SIngo Molnar 		printf("  %-50s [%s]\n",
23665f537a26SJiri Olsa 		       "rNNN",
23671cf4a063SArnaldo Carvalho de Melo 		       event_type_descriptors[PERF_TYPE_RAW]);
23685f537a26SJiri Olsa 		printf("  %-50s [%s]\n",
23695f537a26SJiri Olsa 		       "cpu/t1=v1[,t2=v2,t3 ...]/modifier",
23705f537a26SJiri Olsa 		       event_type_descriptors[PERF_TYPE_RAW]);
2371dfc431cbSArnaldo Carvalho de Melo 		if (pager_in_use())
2372dfc431cbSArnaldo Carvalho de Melo 			printf("   (see 'man perf-list' on how to encode it)\n\n");
237386470930SIngo Molnar 
2374947b4ad1SIngo Molnar 		printf("  %-50s [%s]\n",
23753741eb9fSJacob Shin 		       "mem:<addr>[/len][:access]",
237641bdcb23SLiming Wang 			event_type_descriptors[PERF_TYPE_BREAKPOINT]);
2377dfc431cbSArnaldo Carvalho de Melo 		if (pager_in_use())
23781b290d67SFrederic Weisbecker 			printf("\n");
2379a3277d2dSFrederic Weisbecker 	}
23801b290d67SFrederic Weisbecker 
2381a3277d2dSFrederic Weisbecker 	print_tracepoint_events(NULL, NULL, name_only);
238240218daeSMasami Hiramatsu 
238340218daeSMasami Hiramatsu 	print_sdt_events(NULL, NULL, name_only);
2384*71b0acceSAndi Kleen 
2385*71b0acceSAndi Kleen 	metricgroup__print(true, true, NULL, name_only);
238686470930SIngo Molnar }
23878f707d84SJiri Olsa 
23886cee6cd3SArnaldo Carvalho de Melo int parse_events__is_hardcoded_term(struct parse_events_term *term)
23898f707d84SJiri Olsa {
239016fa7e82SJiri Olsa 	return term->type_term != PARSE_EVENTS__TERM_TYPE_USER;
23918f707d84SJiri Olsa }
23928f707d84SJiri Olsa 
239367b49b38SJiri Olsa static int new_term(struct parse_events_term **_term,
239467b49b38SJiri Olsa 		    struct parse_events_term *temp,
239567b49b38SJiri Olsa 		    char *str, u64 num)
23968f707d84SJiri Olsa {
23976cee6cd3SArnaldo Carvalho de Melo 	struct parse_events_term *term;
23988f707d84SJiri Olsa 
239967b49b38SJiri Olsa 	term = malloc(sizeof(*term));
24008f707d84SJiri Olsa 	if (!term)
24018f707d84SJiri Olsa 		return -ENOMEM;
24028f707d84SJiri Olsa 
240367b49b38SJiri Olsa 	*term = *temp;
24048f707d84SJiri Olsa 	INIT_LIST_HEAD(&term->list);
24058f707d84SJiri Olsa 
240667b49b38SJiri Olsa 	switch (term->type_val) {
24078f707d84SJiri Olsa 	case PARSE_EVENTS__TERM_TYPE_NUM:
24088f707d84SJiri Olsa 		term->val.num = num;
24098f707d84SJiri Olsa 		break;
24108f707d84SJiri Olsa 	case PARSE_EVENTS__TERM_TYPE_STR:
24118f707d84SJiri Olsa 		term->val.str = str;
24128f707d84SJiri Olsa 		break;
24138f707d84SJiri Olsa 	default:
24144be8be6bSAdrian Hunter 		free(term);
24158f707d84SJiri Olsa 		return -EINVAL;
24168f707d84SJiri Olsa 	}
24178f707d84SJiri Olsa 
24188f707d84SJiri Olsa 	*_term = term;
24198f707d84SJiri Olsa 	return 0;
24208f707d84SJiri Olsa }
24218f707d84SJiri Olsa 
24226cee6cd3SArnaldo Carvalho de Melo int parse_events_term__num(struct parse_events_term **term,
2423cecf3a2eSJiri Olsa 			   int type_term, char *config, u64 num,
242499e7138eSJiri Olsa 			   bool no_value,
2425bb78ce7dSAdrian Hunter 			   void *loc_term_, void *loc_val_)
242616fa7e82SJiri Olsa {
2427bb78ce7dSAdrian Hunter 	YYLTYPE *loc_term = loc_term_;
2428bb78ce7dSAdrian Hunter 	YYLTYPE *loc_val = loc_val_;
2429bb78ce7dSAdrian Hunter 
243067b49b38SJiri Olsa 	struct parse_events_term temp = {
243167b49b38SJiri Olsa 		.type_val  = PARSE_EVENTS__TERM_TYPE_NUM,
243267b49b38SJiri Olsa 		.type_term = type_term,
243367b49b38SJiri Olsa 		.config    = config,
243499e7138eSJiri Olsa 		.no_value  = no_value,
243567b49b38SJiri Olsa 		.err_term  = loc_term ? loc_term->first_column : 0,
243667b49b38SJiri Olsa 		.err_val   = loc_val  ? loc_val->first_column  : 0,
243767b49b38SJiri Olsa 	};
243867b49b38SJiri Olsa 
243967b49b38SJiri Olsa 	return new_term(term, &temp, NULL, num);
244016fa7e82SJiri Olsa }
244116fa7e82SJiri Olsa 
24426cee6cd3SArnaldo Carvalho de Melo int parse_events_term__str(struct parse_events_term **term,
2443cecf3a2eSJiri Olsa 			   int type_term, char *config, char *str,
2444bb78ce7dSAdrian Hunter 			   void *loc_term_, void *loc_val_)
244516fa7e82SJiri Olsa {
2446bb78ce7dSAdrian Hunter 	YYLTYPE *loc_term = loc_term_;
2447bb78ce7dSAdrian Hunter 	YYLTYPE *loc_val = loc_val_;
2448bb78ce7dSAdrian Hunter 
244967b49b38SJiri Olsa 	struct parse_events_term temp = {
245067b49b38SJiri Olsa 		.type_val  = PARSE_EVENTS__TERM_TYPE_STR,
245167b49b38SJiri Olsa 		.type_term = type_term,
245267b49b38SJiri Olsa 		.config    = config,
245367b49b38SJiri Olsa 		.err_term  = loc_term ? loc_term->first_column : 0,
245467b49b38SJiri Olsa 		.err_val   = loc_val  ? loc_val->first_column  : 0,
245567b49b38SJiri Olsa 	};
245667b49b38SJiri Olsa 
245767b49b38SJiri Olsa 	return new_term(term, &temp, str, 0);
245816fa7e82SJiri Olsa }
245916fa7e82SJiri Olsa 
24606cee6cd3SArnaldo Carvalho de Melo int parse_events_term__sym_hw(struct parse_events_term **term,
24611d33d6dcSJiri Olsa 			      char *config, unsigned idx)
24621d33d6dcSJiri Olsa {
24631d33d6dcSJiri Olsa 	struct event_symbol *sym;
246467b49b38SJiri Olsa 	struct parse_events_term temp = {
246567b49b38SJiri Olsa 		.type_val  = PARSE_EVENTS__TERM_TYPE_STR,
246667b49b38SJiri Olsa 		.type_term = PARSE_EVENTS__TERM_TYPE_USER,
246767b49b38SJiri Olsa 		.config    = config ?: (char *) "event",
246867b49b38SJiri Olsa 	};
24691d33d6dcSJiri Olsa 
24701d33d6dcSJiri Olsa 	BUG_ON(idx >= PERF_COUNT_HW_MAX);
24711d33d6dcSJiri Olsa 	sym = &event_symbols_hw[idx];
24721d33d6dcSJiri Olsa 
247367b49b38SJiri Olsa 	return new_term(term, &temp, (char *) sym->symbol, 0);
24741d33d6dcSJiri Olsa }
24751d33d6dcSJiri Olsa 
24766cee6cd3SArnaldo Carvalho de Melo int parse_events_term__clone(struct parse_events_term **new,
24776cee6cd3SArnaldo Carvalho de Melo 			     struct parse_events_term *term)
2478a6146d50SZheng Yan {
247967b49b38SJiri Olsa 	struct parse_events_term temp = {
248067b49b38SJiri Olsa 		.type_val  = term->type_val,
248167b49b38SJiri Olsa 		.type_term = term->type_term,
248267b49b38SJiri Olsa 		.config    = term->config,
248367b49b38SJiri Olsa 		.err_term  = term->err_term,
248467b49b38SJiri Olsa 		.err_val   = term->err_val,
248567b49b38SJiri Olsa 	};
248667b49b38SJiri Olsa 
248767b49b38SJiri Olsa 	return new_term(new, &temp, term->val.str, term->val.num);
2488a6146d50SZheng Yan }
2489a6146d50SZheng Yan 
24908255718fSAndi Kleen int parse_events_copy_term_list(struct list_head *old,
24918255718fSAndi Kleen 				 struct list_head **new)
24928255718fSAndi Kleen {
24938255718fSAndi Kleen 	struct parse_events_term *term, *n;
24948255718fSAndi Kleen 	int ret;
24958255718fSAndi Kleen 
24968255718fSAndi Kleen 	if (!old) {
24978255718fSAndi Kleen 		*new = NULL;
24988255718fSAndi Kleen 		return 0;
24998255718fSAndi Kleen 	}
25008255718fSAndi Kleen 
25018255718fSAndi Kleen 	*new = malloc(sizeof(struct list_head));
25028255718fSAndi Kleen 	if (!*new)
25038255718fSAndi Kleen 		return -ENOMEM;
25048255718fSAndi Kleen 	INIT_LIST_HEAD(*new);
25058255718fSAndi Kleen 
25068255718fSAndi Kleen 	list_for_each_entry (term, old, list) {
25078255718fSAndi Kleen 		ret = parse_events_term__clone(&n, term);
25088255718fSAndi Kleen 		if (ret)
25098255718fSAndi Kleen 			return ret;
25108255718fSAndi Kleen 		list_add_tail(&n->list, *new);
25118255718fSAndi Kleen 	}
25128255718fSAndi Kleen 	return 0;
25138255718fSAndi Kleen }
25148255718fSAndi Kleen 
2515fc0a2c1dSArnaldo Carvalho de Melo void parse_events_terms__purge(struct list_head *terms)
25168f707d84SJiri Olsa {
25176cee6cd3SArnaldo Carvalho de Melo 	struct parse_events_term *term, *h;
25188f707d84SJiri Olsa 
2519a8adfcebSWang Nan 	list_for_each_entry_safe(term, h, terms, list) {
25202d055bf2SWang Nan 		if (term->array.nr_ranges)
2521360e071bSTaeung Song 			zfree(&term->array.ranges);
2522a8adfcebSWang Nan 		list_del_init(&term->list);
25238f707d84SJiri Olsa 		free(term);
25248f707d84SJiri Olsa 	}
2525a8adfcebSWang Nan }
2526b39b8393SJiri Olsa 
25272146afc6SArnaldo Carvalho de Melo void parse_events_terms__delete(struct list_head *terms)
2528fc0a2c1dSArnaldo Carvalho de Melo {
25292146afc6SArnaldo Carvalho de Melo 	if (!terms)
25302146afc6SArnaldo Carvalho de Melo 		return;
2531fc0a2c1dSArnaldo Carvalho de Melo 	parse_events_terms__purge(terms);
2532d20a5f2bSWang Nan 	free(terms);
2533fc0a2c1dSArnaldo Carvalho de Melo }
2534fc0a2c1dSArnaldo Carvalho de Melo 
25352d055bf2SWang Nan void parse_events__clear_array(struct parse_events_array *a)
25362d055bf2SWang Nan {
2537360e071bSTaeung Song 	zfree(&a->ranges);
25382d055bf2SWang Nan }
25392d055bf2SWang Nan 
25405d9cdc11SArnaldo Carvalho de Melo void parse_events_evlist_error(struct parse_events_state *parse_state,
2541b39b8393SJiri Olsa 			       int idx, const char *str)
2542b39b8393SJiri Olsa {
25435d9cdc11SArnaldo Carvalho de Melo 	struct parse_events_error *err = parse_state->error;
2544b39b8393SJiri Olsa 
2545a6ced2beSAdrian Hunter 	if (!err)
2546a6ced2beSAdrian Hunter 		return;
2547b39b8393SJiri Olsa 	err->idx = idx;
2548b39b8393SJiri Olsa 	err->str = strdup(str);
2549b39b8393SJiri Olsa 	WARN_ONCE(!err->str, "WARNING: failed to allocate error string");
2550b39b8393SJiri Olsa }
2551ffeb883eSHe Kuang 
255217cb5f84SWang Nan static void config_terms_list(char *buf, size_t buf_sz)
255317cb5f84SWang Nan {
255417cb5f84SWang Nan 	int i;
255517cb5f84SWang Nan 	bool first = true;
255617cb5f84SWang Nan 
255717cb5f84SWang Nan 	buf[0] = '\0';
255817cb5f84SWang Nan 	for (i = 0; i < __PARSE_EVENTS__TERM_TYPE_NR; i++) {
255917cb5f84SWang Nan 		const char *name = config_term_names[i];
256017cb5f84SWang Nan 
25611669e509SWang Nan 		if (!config_term_avail(i, NULL))
25621669e509SWang Nan 			continue;
256317cb5f84SWang Nan 		if (!name)
256417cb5f84SWang Nan 			continue;
256517cb5f84SWang Nan 		if (name[0] == '<')
256617cb5f84SWang Nan 			continue;
256717cb5f84SWang Nan 
256817cb5f84SWang Nan 		if (strlen(buf) + strlen(name) + 2 >= buf_sz)
256917cb5f84SWang Nan 			return;
257017cb5f84SWang Nan 
257117cb5f84SWang Nan 		if (!first)
257217cb5f84SWang Nan 			strcat(buf, ",");
257317cb5f84SWang Nan 		else
257417cb5f84SWang Nan 			first = false;
257517cb5f84SWang Nan 		strcat(buf, name);
257617cb5f84SWang Nan 	}
257717cb5f84SWang Nan }
257817cb5f84SWang Nan 
2579ffeb883eSHe Kuang /*
2580ffeb883eSHe Kuang  * Return string contains valid config terms of an event.
2581ffeb883eSHe Kuang  * @additional_terms: For terms such as PMU sysfs terms.
2582ffeb883eSHe Kuang  */
2583ffeb883eSHe Kuang char *parse_events_formats_error_string(char *additional_terms)
2584ffeb883eSHe Kuang {
2585ffeb883eSHe Kuang 	char *str;
2586626a6b78SWang Nan 	/* "no-overwrite" is the longest name */
258717cb5f84SWang Nan 	char static_terms[__PARSE_EVENTS__TERM_TYPE_NR *
2588626a6b78SWang Nan 			  (sizeof("no-overwrite") - 1)];
2589ffeb883eSHe Kuang 
259017cb5f84SWang Nan 	config_terms_list(static_terms, sizeof(static_terms));
2591ffeb883eSHe Kuang 	/* valid terms */
2592ffeb883eSHe Kuang 	if (additional_terms) {
259326dee028SWang Nan 		if (asprintf(&str, "valid terms: %s,%s",
259426dee028SWang Nan 			     additional_terms, static_terms) < 0)
2595ffeb883eSHe Kuang 			goto fail;
2596ffeb883eSHe Kuang 	} else {
259726dee028SWang Nan 		if (asprintf(&str, "valid terms: %s", static_terms) < 0)
2598ffeb883eSHe Kuang 			goto fail;
2599ffeb883eSHe Kuang 	}
2600ffeb883eSHe Kuang 	return str;
2601ffeb883eSHe Kuang 
2602ffeb883eSHe Kuang fail:
2603ffeb883eSHe Kuang 	return NULL;
2604ffeb883eSHe Kuang }
2605