xref: /linux/tools/perf/util/parse-events.c (revision 545a96c90fbe82c4c1e214b0aada4a22e5ffd1e4)
1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
2d2709c7cSDavid Howells #include <linux/hw_breakpoint.h>
38dd2a131SJiri Olsa #include <linux/err.h>
47f7c536fSArnaldo Carvalho de Melo #include <linux/zalloc.h>
576b31a29SArnaldo Carvalho de Melo #include <dirent.h>
6a43783aeSArnaldo Carvalho de Melo #include <errno.h>
786a5e0c2SArnaldo Carvalho de Melo #include <sys/ioctl.h>
8bafae98eSArnaldo Carvalho de Melo #include <sys/types.h>
9bafae98eSArnaldo Carvalho de Melo #include <sys/stat.h>
10bafae98eSArnaldo Carvalho de Melo #include <fcntl.h>
11391e4206SArnaldo Carvalho de Melo #include <sys/param.h>
12b0742e90SArnaldo Carvalho de Melo #include "term.h"
134a3cec84SArnaldo Carvalho de Melo #include "build-id.h"
14361c99a6SArnaldo Carvalho de Melo #include "evlist.h"
1569aad6f1SArnaldo Carvalho de Melo #include "evsel.h"
16fa0d9846SArnaldo Carvalho de Melo #include <subcmd/pager.h>
174b6ab94eSJosh Poimboeuf #include <subcmd/parse-options.h>
1886470930SIngo Molnar #include "parse-events.h"
194b6ab94eSJosh Poimboeuf #include <subcmd/exec-cmd.h>
20a067558eSArnaldo Carvalho de Melo #include "string2.h"
218ec20b17SArnaldo Carvalho de Melo #include "strlist.h"
2284c86ca1SWang Nan #include "bpf-loader.h"
236e81c74cSMasami Hiramatsu #include "debug.h"
24592d5a6bSJiri Olsa #include <api/fs/tracing_path.h>
259c3516d1SJiri Olsa #include <perf/cpumap.h>
26ac20de6fSZheng Yan #include "parse-events-bison.h"
271244a327SJiri Olsa #define YY_EXTRA_TYPE void*
2889812fc8SJiri Olsa #include "parse-events-flex.h"
295f537a26SJiri Olsa #include "pmu.h"
30b41f1cecSNamhyung Kim #include "thread_map.h"
3140218daeSMasami Hiramatsu #include "probe-file.h"
32b39b8393SJiri Olsa #include "asm/bug.h"
33ac12f676SAndi Kleen #include "util/parse-branch-options.h"
3471b0acceSAndi Kleen #include "metricgroup.h"
3595be9d19SArnaldo Carvalho de Melo #include "util/evsel_config.h"
36252a2fdcSArnaldo Carvalho de Melo #include "util/event.h"
3770943490SStephane Eranian #include "util/pfm.h"
389cbfa2f6SJin Yao #include "util/parse-events-hybrid.h"
395e4edd1fSJin Yao #include "util/pmu-hybrid.h"
40943b69acSJin Yao #include "perf.h"
4189812fc8SJiri Olsa 
4289812fc8SJiri Olsa #define MAX_NAME_LEN 100
4386470930SIngo Molnar 
4482ba1f2fSJiri Olsa #ifdef PARSER_DEBUG
4582ba1f2fSJiri Olsa extern int parse_events_debug;
4682ba1f2fSJiri Olsa #endif
475d9cdc11SArnaldo Carvalho de Melo int parse_events_parse(void *parse_state, void *scanner);
48e637d177SHe Kuang static int get_config_terms(struct list_head *head_config,
49e637d177SHe Kuang 			    struct list_head *head_terms __maybe_unused);
505e4edd1fSJin Yao static int parse_events__with_hybrid_pmu(struct parse_events_state *parse_state,
515e4edd1fSJin Yao 					 const char *str, char *pmu_name,
525e4edd1fSJin Yao 					 struct list_head *list);
53bcd3279fSFrederic Weisbecker 
54dcb4e102SKan Liang static struct perf_pmu_event_symbol *perf_pmu_events_list;
55dcb4e102SKan Liang /*
56dcb4e102SKan Liang  * The variable indicates the number of supported pmu event symbols.
57dcb4e102SKan Liang  * 0 means not initialized and ready to init
58dcb4e102SKan Liang  * -1 means failed to init, don't try anymore
59dcb4e102SKan Liang  * >0 is the number of supported pmu event symbols
60dcb4e102SKan Liang  */
61dcb4e102SKan Liang static int perf_pmu_events_list_num;
62dcb4e102SKan Liang 
63705750f2SYunlong Song struct event_symbol event_symbols_hw[PERF_COUNT_HW_MAX] = {
641dc12760SJiri Olsa 	[PERF_COUNT_HW_CPU_CYCLES] = {
651dc12760SJiri Olsa 		.symbol = "cpu-cycles",
661dc12760SJiri Olsa 		.alias  = "cycles",
671dc12760SJiri Olsa 	},
681dc12760SJiri Olsa 	[PERF_COUNT_HW_INSTRUCTIONS] = {
691dc12760SJiri Olsa 		.symbol = "instructions",
701dc12760SJiri Olsa 		.alias  = "",
711dc12760SJiri Olsa 	},
721dc12760SJiri Olsa 	[PERF_COUNT_HW_CACHE_REFERENCES] = {
731dc12760SJiri Olsa 		.symbol = "cache-references",
741dc12760SJiri Olsa 		.alias  = "",
751dc12760SJiri Olsa 	},
761dc12760SJiri Olsa 	[PERF_COUNT_HW_CACHE_MISSES] = {
771dc12760SJiri Olsa 		.symbol = "cache-misses",
781dc12760SJiri Olsa 		.alias  = "",
791dc12760SJiri Olsa 	},
801dc12760SJiri Olsa 	[PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = {
811dc12760SJiri Olsa 		.symbol = "branch-instructions",
821dc12760SJiri Olsa 		.alias  = "branches",
831dc12760SJiri Olsa 	},
841dc12760SJiri Olsa 	[PERF_COUNT_HW_BRANCH_MISSES] = {
851dc12760SJiri Olsa 		.symbol = "branch-misses",
861dc12760SJiri Olsa 		.alias  = "",
871dc12760SJiri Olsa 	},
881dc12760SJiri Olsa 	[PERF_COUNT_HW_BUS_CYCLES] = {
891dc12760SJiri Olsa 		.symbol = "bus-cycles",
901dc12760SJiri Olsa 		.alias  = "",
911dc12760SJiri Olsa 	},
921dc12760SJiri Olsa 	[PERF_COUNT_HW_STALLED_CYCLES_FRONTEND] = {
931dc12760SJiri Olsa 		.symbol = "stalled-cycles-frontend",
941dc12760SJiri Olsa 		.alias  = "idle-cycles-frontend",
951dc12760SJiri Olsa 	},
961dc12760SJiri Olsa 	[PERF_COUNT_HW_STALLED_CYCLES_BACKEND] = {
971dc12760SJiri Olsa 		.symbol = "stalled-cycles-backend",
981dc12760SJiri Olsa 		.alias  = "idle-cycles-backend",
991dc12760SJiri Olsa 	},
1001dc12760SJiri Olsa 	[PERF_COUNT_HW_REF_CPU_CYCLES] = {
1011dc12760SJiri Olsa 		.symbol = "ref-cycles",
1021dc12760SJiri Olsa 		.alias  = "",
1031dc12760SJiri Olsa 	},
1041dc12760SJiri Olsa };
10586470930SIngo Molnar 
106705750f2SYunlong Song struct event_symbol event_symbols_sw[PERF_COUNT_SW_MAX] = {
1071dc12760SJiri Olsa 	[PERF_COUNT_SW_CPU_CLOCK] = {
1081dc12760SJiri Olsa 		.symbol = "cpu-clock",
1091dc12760SJiri Olsa 		.alias  = "",
1101dc12760SJiri Olsa 	},
1111dc12760SJiri Olsa 	[PERF_COUNT_SW_TASK_CLOCK] = {
1121dc12760SJiri Olsa 		.symbol = "task-clock",
1131dc12760SJiri Olsa 		.alias  = "",
1141dc12760SJiri Olsa 	},
1151dc12760SJiri Olsa 	[PERF_COUNT_SW_PAGE_FAULTS] = {
1161dc12760SJiri Olsa 		.symbol = "page-faults",
1171dc12760SJiri Olsa 		.alias  = "faults",
1181dc12760SJiri Olsa 	},
1191dc12760SJiri Olsa 	[PERF_COUNT_SW_CONTEXT_SWITCHES] = {
1201dc12760SJiri Olsa 		.symbol = "context-switches",
1211dc12760SJiri Olsa 		.alias  = "cs",
1221dc12760SJiri Olsa 	},
1231dc12760SJiri Olsa 	[PERF_COUNT_SW_CPU_MIGRATIONS] = {
1241dc12760SJiri Olsa 		.symbol = "cpu-migrations",
1251dc12760SJiri Olsa 		.alias  = "migrations",
1261dc12760SJiri Olsa 	},
1271dc12760SJiri Olsa 	[PERF_COUNT_SW_PAGE_FAULTS_MIN] = {
1281dc12760SJiri Olsa 		.symbol = "minor-faults",
1291dc12760SJiri Olsa 		.alias  = "",
1301dc12760SJiri Olsa 	},
1311dc12760SJiri Olsa 	[PERF_COUNT_SW_PAGE_FAULTS_MAJ] = {
1321dc12760SJiri Olsa 		.symbol = "major-faults",
1331dc12760SJiri Olsa 		.alias  = "",
1341dc12760SJiri Olsa 	},
1351dc12760SJiri Olsa 	[PERF_COUNT_SW_ALIGNMENT_FAULTS] = {
1361dc12760SJiri Olsa 		.symbol = "alignment-faults",
1371dc12760SJiri Olsa 		.alias  = "",
1381dc12760SJiri Olsa 	},
1391dc12760SJiri Olsa 	[PERF_COUNT_SW_EMULATION_FAULTS] = {
1401dc12760SJiri Olsa 		.symbol = "emulation-faults",
1411dc12760SJiri Olsa 		.alias  = "",
1421dc12760SJiri Olsa 	},
143d22d1a2aSAdrian Hunter 	[PERF_COUNT_SW_DUMMY] = {
144d22d1a2aSAdrian Hunter 		.symbol = "dummy",
145d22d1a2aSAdrian Hunter 		.alias  = "",
146d22d1a2aSAdrian Hunter 	},
147bae9cc41SArnaldo Carvalho de Melo 	[PERF_COUNT_SW_BPF_OUTPUT] = {
148bae9cc41SArnaldo Carvalho de Melo 		.symbol = "bpf-output",
149bae9cc41SArnaldo Carvalho de Melo 		.alias  = "",
150bae9cc41SArnaldo Carvalho de Melo 	},
151fb6c79d7SNamhyung Kim 	[PERF_COUNT_SW_CGROUP_SWITCHES] = {
152fb6c79d7SNamhyung Kim 		.symbol = "cgroup-switches",
153fb6c79d7SNamhyung Kim 		.alias  = "",
154fb6c79d7SNamhyung Kim 	},
15586470930SIngo Molnar };
15686470930SIngo Molnar 
15775eafc97SFlorian Fischer struct event_symbol event_symbols_tool[PERF_TOOL_MAX] = {
15875eafc97SFlorian Fischer 	[PERF_TOOL_DURATION_TIME] = {
15975eafc97SFlorian Fischer 		.symbol = "duration_time",
16075eafc97SFlorian Fischer 		.alias  = "",
16175eafc97SFlorian Fischer 	},
16275eafc97SFlorian Fischer 	[PERF_TOOL_USER_TIME] = {
16375eafc97SFlorian Fischer 		.symbol = "user_time",
16475eafc97SFlorian Fischer 		.alias  = "",
16575eafc97SFlorian Fischer 	},
16675eafc97SFlorian Fischer 	[PERF_TOOL_SYSTEM_TIME] = {
16775eafc97SFlorian Fischer 		.symbol = "system_time",
16875eafc97SFlorian Fischer 		.alias  = "",
16975eafc97SFlorian Fischer 	},
17075eafc97SFlorian Fischer };
17175eafc97SFlorian Fischer 
172cdd6c482SIngo Molnar #define __PERF_EVENT_FIELD(config, name) \
173cdd6c482SIngo Molnar 	((config & PERF_EVENT_##name##_MASK) >> PERF_EVENT_##name##_SHIFT)
17486470930SIngo Molnar 
175cdd6c482SIngo Molnar #define PERF_EVENT_RAW(config)		__PERF_EVENT_FIELD(config, RAW)
176cdd6c482SIngo Molnar #define PERF_EVENT_CONFIG(config)	__PERF_EVENT_FIELD(config, CONFIG)
177cdd6c482SIngo Molnar #define PERF_EVENT_TYPE(config)		__PERF_EVENT_FIELD(config, TYPE)
178cdd6c482SIngo Molnar #define PERF_EVENT_ID(config)		__PERF_EVENT_FIELD(config, EVENT)
17986470930SIngo Molnar 
18022a9f41bSArnaldo Carvalho de Melo #define for_each_subsystem(sys_dir, sys_dirent)			\
18122a9f41bSArnaldo Carvalho de Melo 	while ((sys_dirent = readdir(sys_dir)) != NULL)		\
18222a9f41bSArnaldo Carvalho de Melo 		if (sys_dirent->d_type == DT_DIR &&		\
18322a9f41bSArnaldo Carvalho de Melo 		    (strcmp(sys_dirent->d_name, ".")) &&	\
18422a9f41bSArnaldo Carvalho de Melo 		    (strcmp(sys_dirent->d_name, "..")))
185f6bdafefSJason Baron 
186c02cab22SArnaldo Carvalho de Melo static int tp_event_has_id(const char *dir_path, struct dirent *evt_dir)
187ae07b63fSPeter Zijlstra {
188ae07b63fSPeter Zijlstra 	char evt_path[MAXPATHLEN];
189ae07b63fSPeter Zijlstra 	int fd;
190ae07b63fSPeter Zijlstra 
191c02cab22SArnaldo Carvalho de Melo 	snprintf(evt_path, MAXPATHLEN, "%s/%s/id", dir_path, evt_dir->d_name);
192ae07b63fSPeter Zijlstra 	fd = open(evt_path, O_RDONLY);
193ae07b63fSPeter Zijlstra 	if (fd < 0)
194ae07b63fSPeter Zijlstra 		return -EINVAL;
195ae07b63fSPeter Zijlstra 	close(fd);
196ae07b63fSPeter Zijlstra 
197ae07b63fSPeter Zijlstra 	return 0;
198ae07b63fSPeter Zijlstra }
199ae07b63fSPeter Zijlstra 
200c02cab22SArnaldo Carvalho de Melo #define for_each_event(dir_path, evt_dir, evt_dirent)		\
20122a9f41bSArnaldo Carvalho de Melo 	while ((evt_dirent = readdir(evt_dir)) != NULL)		\
20222a9f41bSArnaldo Carvalho de Melo 		if (evt_dirent->d_type == DT_DIR &&		\
20322a9f41bSArnaldo Carvalho de Melo 		    (strcmp(evt_dirent->d_name, ".")) &&	\
20422a9f41bSArnaldo Carvalho de Melo 		    (strcmp(evt_dirent->d_name, "..")) &&	\
205c02cab22SArnaldo Carvalho de Melo 		    (!tp_event_has_id(dir_path, evt_dirent)))
206f6bdafefSJason Baron 
207270bbbe8SLi Zefan #define MAX_EVENT_LENGTH 512
208f6bdafefSJason Baron 
2091ef2ed10SFrederic Weisbecker struct tracepoint_path *tracepoint_id_to_path(u64 config)
210f6bdafefSJason Baron {
2111ef2ed10SFrederic Weisbecker 	struct tracepoint_path *path = NULL;
212f6bdafefSJason Baron 	DIR *sys_dir, *evt_dir;
21322a9f41bSArnaldo Carvalho de Melo 	struct dirent *sys_dirent, *evt_dirent;
2148aa8a7c8SStephane Eranian 	char id_buf[24];
215725b1368SEric Dumazet 	int fd;
216f6bdafefSJason Baron 	u64 id;
217f6bdafefSJason Baron 	char evt_path[MAXPATHLEN];
21825a7d914SArnaldo Carvalho de Melo 	char *dir_path;
219f6bdafefSJason Baron 
2207014e0e3SArnaldo Carvalho de Melo 	sys_dir = tracing_events__opendir();
221f6bdafefSJason Baron 	if (!sys_dir)
222725b1368SEric Dumazet 		return NULL;
223f6bdafefSJason Baron 
22422a9f41bSArnaldo Carvalho de Melo 	for_each_subsystem(sys_dir, sys_dirent) {
22525a7d914SArnaldo Carvalho de Melo 		dir_path = get_events_file(sys_dirent->d_name);
22625a7d914SArnaldo Carvalho de Melo 		if (!dir_path)
22725a7d914SArnaldo Carvalho de Melo 			continue;
228725b1368SEric Dumazet 		evt_dir = opendir(dir_path);
229725b1368SEric Dumazet 		if (!evt_dir)
23025a7d914SArnaldo Carvalho de Melo 			goto next;
231725b1368SEric Dumazet 
232c02cab22SArnaldo Carvalho de Melo 		for_each_event(dir_path, evt_dir, evt_dirent) {
233725b1368SEric Dumazet 
23477f18153SJiri Olsa 			scnprintf(evt_path, MAXPATHLEN, "%s/%s/id", dir_path,
23522a9f41bSArnaldo Carvalho de Melo 				  evt_dirent->d_name);
236725b1368SEric Dumazet 			fd = open(evt_path, O_RDONLY);
237f6bdafefSJason Baron 			if (fd < 0)
238f6bdafefSJason Baron 				continue;
239f6bdafefSJason Baron 			if (read(fd, id_buf, sizeof(id_buf)) < 0) {
240f6bdafefSJason Baron 				close(fd);
241f6bdafefSJason Baron 				continue;
242f6bdafefSJason Baron 			}
243f6bdafefSJason Baron 			close(fd);
244f6bdafefSJason Baron 			id = atoll(id_buf);
245f6bdafefSJason Baron 			if (id == config) {
24625a7d914SArnaldo Carvalho de Melo 				put_events_file(dir_path);
247f6bdafefSJason Baron 				closedir(evt_dir);
248f6bdafefSJason Baron 				closedir(sys_dir);
24959b4caebSJulia Lawall 				path = zalloc(sizeof(*path));
2505aa365f2STaeung Song 				if (!path)
2515aa365f2STaeung Song 					return NULL;
2527125f204SArnaldo Carvalho de Melo 				if (asprintf(&path->system, "%.*s", MAX_EVENT_LENGTH, sys_dirent->d_name) < 0) {
2531ef2ed10SFrederic Weisbecker 					free(path);
2541ef2ed10SFrederic Weisbecker 					return NULL;
2551ef2ed10SFrederic Weisbecker 				}
2567125f204SArnaldo Carvalho de Melo 				if (asprintf(&path->name, "%.*s", MAX_EVENT_LENGTH, evt_dirent->d_name) < 0) {
25774cf249dSArnaldo Carvalho de Melo 					zfree(&path->system);
2581ef2ed10SFrederic Weisbecker 					free(path);
2591ef2ed10SFrederic Weisbecker 					return NULL;
2601ef2ed10SFrederic Weisbecker 				}
2611ef2ed10SFrederic Weisbecker 				return path;
262f6bdafefSJason Baron 			}
263f6bdafefSJason Baron 		}
264f6bdafefSJason Baron 		closedir(evt_dir);
26525a7d914SArnaldo Carvalho de Melo next:
26625a7d914SArnaldo Carvalho de Melo 		put_events_file(dir_path);
267f6bdafefSJason Baron 	}
268f6bdafefSJason Baron 
269f6bdafefSJason Baron 	closedir(sys_dir);
2701ef2ed10SFrederic Weisbecker 	return NULL;
2711ef2ed10SFrederic Weisbecker }
2721ef2ed10SFrederic Weisbecker 
273e7c93f09SNamhyung Kim struct tracepoint_path *tracepoint_name_to_path(const char *name)
274e7c93f09SNamhyung Kim {
275e7c93f09SNamhyung Kim 	struct tracepoint_path *path = zalloc(sizeof(*path));
276e7c93f09SNamhyung Kim 	char *str = strchr(name, ':');
277e7c93f09SNamhyung Kim 
278e7c93f09SNamhyung Kim 	if (path == NULL || str == NULL) {
279e7c93f09SNamhyung Kim 		free(path);
280e7c93f09SNamhyung Kim 		return NULL;
281e7c93f09SNamhyung Kim 	}
282e7c93f09SNamhyung Kim 
283e7c93f09SNamhyung Kim 	path->system = strndup(name, str - name);
284e7c93f09SNamhyung Kim 	path->name = strdup(str+1);
285e7c93f09SNamhyung Kim 
286e7c93f09SNamhyung Kim 	if (path->system == NULL || path->name == NULL) {
28774cf249dSArnaldo Carvalho de Melo 		zfree(&path->system);
28874cf249dSArnaldo Carvalho de Melo 		zfree(&path->name);
289506fde11STaeung Song 		zfree(&path);
290e7c93f09SNamhyung Kim 	}
291e7c93f09SNamhyung Kim 
292e7c93f09SNamhyung Kim 	return path;
293e7c93f09SNamhyung Kim }
294e7c93f09SNamhyung Kim 
2951424dc96SDavid Ahern const char *event_type(int type)
2961424dc96SDavid Ahern {
2971424dc96SDavid Ahern 	switch (type) {
2981424dc96SDavid Ahern 	case PERF_TYPE_HARDWARE:
2991424dc96SDavid Ahern 		return "hardware";
3001424dc96SDavid Ahern 
3011424dc96SDavid Ahern 	case PERF_TYPE_SOFTWARE:
3021424dc96SDavid Ahern 		return "software";
3031424dc96SDavid Ahern 
3041424dc96SDavid Ahern 	case PERF_TYPE_TRACEPOINT:
3051424dc96SDavid Ahern 		return "tracepoint";
3061424dc96SDavid Ahern 
3071424dc96SDavid Ahern 	case PERF_TYPE_HW_CACHE:
3081424dc96SDavid Ahern 		return "hardware-cache";
3091424dc96SDavid Ahern 
3101424dc96SDavid Ahern 	default:
3111424dc96SDavid Ahern 		break;
3121424dc96SDavid Ahern 	}
3131424dc96SDavid Ahern 
3141424dc96SDavid Ahern 	return "unknown";
3151424dc96SDavid Ahern }
3161424dc96SDavid Ahern 
3172b62b3a6SIan Rogers static char *get_config_str(struct list_head *head_terms, int type_term)
318e814fdddSWang Nan {
319e814fdddSWang Nan 	struct parse_events_term *term;
320e814fdddSWang Nan 
321e814fdddSWang Nan 	if (!head_terms)
322e814fdddSWang Nan 		return NULL;
323e814fdddSWang Nan 
324e814fdddSWang Nan 	list_for_each_entry(term, head_terms, list)
3252b62b3a6SIan Rogers 		if (term->type_term == type_term)
326e814fdddSWang Nan 			return term->val.str;
327e814fdddSWang Nan 
328e814fdddSWang Nan 	return NULL;
329e814fdddSWang Nan }
3307ae92e74SYan, Zheng 
3312b62b3a6SIan Rogers static char *get_config_metric_id(struct list_head *head_terms)
3322b62b3a6SIan Rogers {
3332b62b3a6SIan Rogers 	return get_config_str(head_terms, PARSE_EVENTS__TERM_TYPE_METRIC_ID);
3342b62b3a6SIan Rogers }
3352b62b3a6SIan Rogers 
3362b62b3a6SIan Rogers static char *get_config_name(struct list_head *head_terms)
3372b62b3a6SIan Rogers {
3382b62b3a6SIan Rogers 	return get_config_str(head_terms, PARSE_EVENTS__TERM_TYPE_NAME);
3392b62b3a6SIan Rogers }
3402b62b3a6SIan Rogers 
34132dcd021SJiri Olsa static struct evsel *
342410136f5SStephane Eranian __add_event(struct list_head *list, int *idx,
3437ae92e74SYan, Zheng 	    struct perf_event_attr *attr,
34470943490SStephane Eranian 	    bool init_attr,
3452b62b3a6SIan Rogers 	    const char *name, const char *metric_id, struct perf_pmu *pmu,
346f0fbb114SAndi Kleen 	    struct list_head *config_terms, bool auto_merge_stats,
347f0fbb114SAndi Kleen 	    const char *cpu_list)
34889812fc8SJiri Olsa {
34932dcd021SJiri Olsa 	struct evsel *evsel;
3505d680be3SNamhyung Kim 	struct perf_cpu_map *cpus = pmu ? perf_cpu_map__get(pmu->cpus) :
3519c3516d1SJiri Olsa 			       cpu_list ? perf_cpu_map__new(cpu_list) : NULL;
35289812fc8SJiri Olsa 
353e4064776SJin Yao 	if (pmu && attr->type == PERF_TYPE_RAW)
354e4064776SJin Yao 		perf_pmu__warn_invalid_config(pmu, attr->config, name);
355e4064776SJin Yao 
35670943490SStephane Eranian 	if (init_attr)
35789812fc8SJiri Olsa 		event_attr_init(attr);
35889812fc8SJiri Olsa 
3598f6725a2SArnaldo Carvalho de Melo 	evsel = evsel__new_idx(attr, *idx);
3600f1b550eSNamhyung Kim 	if (!evsel) {
3610f1b550eSNamhyung Kim 		perf_cpu_map__put(cpus);
362410136f5SStephane Eranian 		return NULL;
3630f1b550eSNamhyung Kim 	}
36489812fc8SJiri Olsa 
36575fc5ae5STaeung Song 	(*idx)++;
3665d680be3SNamhyung Kim 	evsel->core.cpus = cpus;
367fe1f61b3SJiri Olsa 	evsel->core.own_cpus = perf_cpu_map__get(cpus);
368648b5af3SJiri Olsa 	evsel->core.system_wide = pmu ? pmu->is_uncore : false;
36963ce8449SArnaldo Carvalho de Melo 	evsel->auto_merge_stats = auto_merge_stats;
370f30a79b0SJiri Olsa 
3719db1763cSArnaldo Carvalho de Melo 	if (name)
37289812fc8SJiri Olsa 		evsel->name = strdup(name);
373930a2e29SJiri Olsa 
3742b62b3a6SIan Rogers 	if (metric_id)
3752b62b3a6SIan Rogers 		evsel->metric_id = strdup(metric_id);
3762b62b3a6SIan Rogers 
377930a2e29SJiri Olsa 	if (config_terms)
37899fc5941SAdrian Hunter 		list_splice_init(config_terms, &evsel->config_terms);
379930a2e29SJiri Olsa 
38070943490SStephane Eranian 	if (list)
381b27c4eceSJiri Olsa 		list_add_tail(&evsel->core.node, list);
38270943490SStephane Eranian 
383410136f5SStephane Eranian 	return evsel;
38489812fc8SJiri Olsa }
38589812fc8SJiri Olsa 
38670943490SStephane Eranian struct evsel *parse_events__add_event(int idx, struct perf_event_attr *attr,
3872b62b3a6SIan Rogers 				      const char *name, const char *metric_id,
3882b62b3a6SIan Rogers 				      struct perf_pmu *pmu)
38970943490SStephane Eranian {
3902b62b3a6SIan Rogers 	return __add_event(/*list=*/NULL, &idx, attr, /*init_attr=*/false, name,
3912b62b3a6SIan Rogers 			   metric_id, pmu, /*config_terms=*/NULL,
3922b62b3a6SIan Rogers 			   /*auto_merge_stats=*/false, /*cpu_list=*/NULL);
39370943490SStephane Eranian }
39470943490SStephane Eranian 
395c5cd8ac0SDavid Ahern static int add_event(struct list_head *list, int *idx,
3968e8bbfb3SIan Rogers 		     struct perf_event_attr *attr, const char *name,
3972b62b3a6SIan Rogers 		     const char *metric_id, struct list_head *config_terms)
3987ae92e74SYan, Zheng {
3992b62b3a6SIan Rogers 	return __add_event(list, idx, attr, /*init_attr*/true, name, metric_id,
4002b62b3a6SIan Rogers 			   /*pmu=*/NULL, config_terms,
4012b62b3a6SIan Rogers 			   /*auto_merge_stats=*/false, /*cpu_list=*/NULL) ? 0 : -ENOMEM;
402f0fbb114SAndi Kleen }
403f0fbb114SAndi Kleen 
404f0fbb114SAndi Kleen static int add_event_tool(struct list_head *list, int *idx,
405f0fbb114SAndi Kleen 			  enum perf_tool_event tool_event)
406f0fbb114SAndi Kleen {
40732dcd021SJiri Olsa 	struct evsel *evsel;
408f0fbb114SAndi Kleen 	struct perf_event_attr attr = {
409f0fbb114SAndi Kleen 		.type = PERF_TYPE_SOFTWARE,
410f0fbb114SAndi Kleen 		.config = PERF_COUNT_SW_DUMMY,
411f0fbb114SAndi Kleen 	};
412f0fbb114SAndi Kleen 
4132b62b3a6SIan Rogers 	evsel = __add_event(list, idx, &attr, /*init_attr=*/true, /*name=*/NULL,
4142b62b3a6SIan Rogers 			    /*metric_id=*/NULL, /*pmu=*/NULL,
4152b62b3a6SIan Rogers 			    /*config_terms=*/NULL, /*auto_merge_stats=*/false,
4162b62b3a6SIan Rogers 			    /*cpu_list=*/"0");
417f0fbb114SAndi Kleen 	if (!evsel)
418f0fbb114SAndi Kleen 		return -ENOMEM;
419f0fbb114SAndi Kleen 	evsel->tool_event = tool_event;
420b03b89b3SFlorian Fischer 	if (tool_event == PERF_TOOL_DURATION_TIME
421b03b89b3SFlorian Fischer 	    || tool_event == PERF_TOOL_USER_TIME
422b03b89b3SFlorian Fischer 	    || tool_event == PERF_TOOL_SYSTEM_TIME) {
423b194c9cdSIan Rogers 		free((char *)evsel->unit);
424b194c9cdSIan Rogers 		evsel->unit = strdup("ns");
425b194c9cdSIan Rogers 	}
426f0fbb114SAndi Kleen 	return 0;
4277ae92e74SYan, Zheng }
4287ae92e74SYan, Zheng 
429*545a96c9SIan Rogers static int parse_aliases(char *str, const char *const names[][EVSEL__MAX_ALIASES], int size)
43086470930SIngo Molnar {
43186470930SIngo Molnar 	int i, j;
43261c45981SPaul Mackerras 	int n, longest = -1;
43386470930SIngo Molnar 
43486470930SIngo Molnar 	for (i = 0; i < size; i++) {
435c64e85e1SArnaldo Carvalho de Melo 		for (j = 0; j < EVSEL__MAX_ALIASES && names[i][j]; j++) {
43661c45981SPaul Mackerras 			n = strlen(names[i][j]);
43789812fc8SJiri Olsa 			if (n > longest && !strncasecmp(str, names[i][j], n))
43861c45981SPaul Mackerras 				longest = n;
43961c45981SPaul Mackerras 		}
44089812fc8SJiri Olsa 		if (longest > 0)
44186470930SIngo Molnar 			return i;
44286470930SIngo Molnar 	}
44386470930SIngo Molnar 
4448953645fSIngo Molnar 	return -1;
44586470930SIngo Molnar }
44686470930SIngo Molnar 
44743d0b978SWang Nan typedef int config_term_func_t(struct perf_event_attr *attr,
44843d0b978SWang Nan 			       struct parse_events_term *term,
44943d0b978SWang Nan 			       struct parse_events_error *err);
45043d0b978SWang Nan static int config_term_common(struct perf_event_attr *attr,
45143d0b978SWang Nan 			      struct parse_events_term *term,
45243d0b978SWang Nan 			      struct parse_events_error *err);
45343d0b978SWang Nan static int config_attr(struct perf_event_attr *attr,
45443d0b978SWang Nan 		       struct list_head *head,
45543d0b978SWang Nan 		       struct parse_events_error *err,
45643d0b978SWang Nan 		       config_term_func_t config_term);
45743d0b978SWang Nan 
458c5cd8ac0SDavid Ahern int parse_events_add_cache(struct list_head *list, int *idx,
45943d0b978SWang Nan 			   char *type, char *op_result1, char *op_result2,
4602c97b0d4SArnaldo Carvalho de Melo 			   struct parse_events_error *err,
461c93afadcSJin Yao 			   struct list_head *head_config,
462c93afadcSJin Yao 			   struct parse_events_state *parse_state)
46386470930SIngo Molnar {
46489812fc8SJiri Olsa 	struct perf_event_attr attr;
46543d0b978SWang Nan 	LIST_HEAD(config_terms);
4668e8bbfb3SIan Rogers 	char name[MAX_NAME_LEN];
4672b62b3a6SIan Rogers 	const char *config_name, *metric_id;
46861c45981SPaul Mackerras 	int cache_type = -1, cache_op = -1, cache_result = -1;
46989812fc8SJiri Olsa 	char *op_result[2] = { op_result1, op_result2 };
47030def61fSJin Yao 	int i, n, ret;
47130def61fSJin Yao 	bool hybrid;
47286470930SIngo Molnar 
47386470930SIngo Molnar 	/*
47486470930SIngo Molnar 	 * No fallback - if we cannot get a clear cache type
47586470930SIngo Molnar 	 * then bail out:
47686470930SIngo Molnar 	 */
477c64e85e1SArnaldo Carvalho de Melo 	cache_type = parse_aliases(type, evsel__hw_cache, PERF_COUNT_HW_CACHE_MAX);
47886470930SIngo Molnar 	if (cache_type == -1)
47989812fc8SJiri Olsa 		return -EINVAL;
48086470930SIngo Molnar 
48143d0b978SWang Nan 	config_name = get_config_name(head_config);
48289812fc8SJiri Olsa 	n = snprintf(name, MAX_NAME_LEN, "%s", type);
48389812fc8SJiri Olsa 
48489812fc8SJiri Olsa 	for (i = 0; (i < 2) && (op_result[i]); i++) {
48589812fc8SJiri Olsa 		char *str = op_result[i];
48689812fc8SJiri Olsa 
487275ef387SJiri Olsa 		n += snprintf(name + n, MAX_NAME_LEN - n, "-%s", str);
48861c45981SPaul Mackerras 
48961c45981SPaul Mackerras 		if (cache_op == -1) {
490c64e85e1SArnaldo Carvalho de Melo 			cache_op = parse_aliases(str, evsel__hw_cache_op,
49161c45981SPaul Mackerras 						 PERF_COUNT_HW_CACHE_OP_MAX);
49261c45981SPaul Mackerras 			if (cache_op >= 0) {
493c754c382SArnaldo Carvalho de Melo 				if (!evsel__is_cache_op_valid(cache_type, cache_op))
49489812fc8SJiri Olsa 					return -EINVAL;
49561c45981SPaul Mackerras 				continue;
49661c45981SPaul Mackerras 			}
49761c45981SPaul Mackerras 		}
49861c45981SPaul Mackerras 
49961c45981SPaul Mackerras 		if (cache_result == -1) {
500c64e85e1SArnaldo Carvalho de Melo 			cache_result = parse_aliases(str, evsel__hw_cache_result,
50161c45981SPaul Mackerras 						     PERF_COUNT_HW_CACHE_RESULT_MAX);
50261c45981SPaul Mackerras 			if (cache_result >= 0)
50361c45981SPaul Mackerras 				continue;
50461c45981SPaul Mackerras 		}
50561c45981SPaul Mackerras 	}
50661c45981SPaul Mackerras 
50786470930SIngo Molnar 	/*
50886470930SIngo Molnar 	 * Fall back to reads:
50986470930SIngo Molnar 	 */
5108953645fSIngo Molnar 	if (cache_op == -1)
5118953645fSIngo Molnar 		cache_op = PERF_COUNT_HW_CACHE_OP_READ;
51286470930SIngo Molnar 
51386470930SIngo Molnar 	/*
51486470930SIngo Molnar 	 * Fall back to accesses:
51586470930SIngo Molnar 	 */
51686470930SIngo Molnar 	if (cache_result == -1)
51786470930SIngo Molnar 		cache_result = PERF_COUNT_HW_CACHE_RESULT_ACCESS;
51886470930SIngo Molnar 
51989812fc8SJiri Olsa 	memset(&attr, 0, sizeof(attr));
52089812fc8SJiri Olsa 	attr.config = cache_type | (cache_op << 8) | (cache_result << 16);
52189812fc8SJiri Olsa 	attr.type = PERF_TYPE_HW_CACHE;
52243d0b978SWang Nan 
52343d0b978SWang Nan 	if (head_config) {
5242c97b0d4SArnaldo Carvalho de Melo 		if (config_attr(&attr, head_config, err,
52543d0b978SWang Nan 				config_term_common))
52643d0b978SWang Nan 			return -EINVAL;
52743d0b978SWang Nan 
52843d0b978SWang Nan 		if (get_config_terms(head_config, &config_terms))
52943d0b978SWang Nan 			return -ENOMEM;
53043d0b978SWang Nan 	}
53130def61fSJin Yao 
5322b62b3a6SIan Rogers 	metric_id = get_config_metric_id(head_config);
53330def61fSJin Yao 	ret = parse_events__add_cache_hybrid(list, idx, &attr,
5342b62b3a6SIan Rogers 					     config_name ? : name,
5352b62b3a6SIan Rogers 					     metric_id,
5362b62b3a6SIan Rogers 					     &config_terms,
537c93afadcSJin Yao 					     &hybrid, parse_state);
53830def61fSJin Yao 	if (hybrid)
53999fc5941SAdrian Hunter 		goto out_free_terms;
54030def61fSJin Yao 
5412b62b3a6SIan Rogers 	ret = add_event(list, idx, &attr, config_name ? : name, metric_id,
5422b62b3a6SIan Rogers 			&config_terms);
54399fc5941SAdrian Hunter out_free_terms:
54499fc5941SAdrian Hunter 	free_config_terms(&config_terms);
54599fc5941SAdrian Hunter 	return ret;
54686470930SIngo Molnar }
54786470930SIngo Molnar 
548272ed29aSJiri Olsa static void tracepoint_error(struct parse_events_error *e, int err,
5498c619d6aSWang Nan 			     const char *sys, const char *name)
55019658171SJiri Olsa {
5514584f084SIan Rogers 	const char *str;
55219658171SJiri Olsa 	char help[BUFSIZ];
55319658171SJiri Olsa 
554ec183d22SAdrian Hunter 	if (!e)
555ec183d22SAdrian Hunter 		return;
556ec183d22SAdrian Hunter 
55719658171SJiri Olsa 	/*
55819658171SJiri Olsa 	 * We get error directly from syscall errno ( > 0),
55919658171SJiri Olsa 	 * or from encoded pointer's error ( < 0).
56019658171SJiri Olsa 	 */
56119658171SJiri Olsa 	err = abs(err);
56219658171SJiri Olsa 
56319658171SJiri Olsa 	switch (err) {
56419658171SJiri Olsa 	case EACCES:
5654584f084SIan Rogers 		str = "can't access trace events";
56619658171SJiri Olsa 		break;
56719658171SJiri Olsa 	case ENOENT:
5684584f084SIan Rogers 		str = "unknown tracepoint";
56919658171SJiri Olsa 		break;
57019658171SJiri Olsa 	default:
5714584f084SIan Rogers 		str = "failed to add tracepoint";
57219658171SJiri Olsa 		break;
57319658171SJiri Olsa 	}
57419658171SJiri Olsa 
57519658171SJiri Olsa 	tracing_path__strerror_open_tp(err, help, sizeof(help), sys, name);
5766c191289SIan Rogers 	parse_events_error__handle(e, 0, strdup(str), strdup(help));
57719658171SJiri Olsa }
57819658171SJiri Olsa 
579c5cd8ac0SDavid Ahern static int add_tracepoint(struct list_head *list, int *idx,
5808c619d6aSWang Nan 			  const char *sys_name, const char *evt_name,
581272ed29aSJiri Olsa 			  struct parse_events_error *err,
582e637d177SHe Kuang 			  struct list_head *head_config)
583bcd3279fSFrederic Weisbecker {
5848f6725a2SArnaldo Carvalho de Melo 	struct evsel *evsel = evsel__newtp_idx(sys_name, evt_name, (*idx)++);
585bcd3279fSFrederic Weisbecker 
58619658171SJiri Olsa 	if (IS_ERR(evsel)) {
587272ed29aSJiri Olsa 		tracepoint_error(err, PTR_ERR(evsel), sys_name, evt_name);
5888dd2a131SJiri Olsa 		return PTR_ERR(evsel);
58919658171SJiri Olsa 	}
590bcd3279fSFrederic Weisbecker 
591e637d177SHe Kuang 	if (head_config) {
592e637d177SHe Kuang 		LIST_HEAD(config_terms);
593e637d177SHe Kuang 
594e637d177SHe Kuang 		if (get_config_terms(head_config, &config_terms))
595e637d177SHe Kuang 			return -ENOMEM;
596e637d177SHe Kuang 		list_splice(&config_terms, &evsel->config_terms);
597e637d177SHe Kuang 	}
598e637d177SHe Kuang 
599b27c4eceSJiri Olsa 	list_add_tail(&evsel->core.node, list);
60082fe1c29SArnaldo Carvalho de Melo 	return 0;
601bcd3279fSFrederic Weisbecker }
602bcd3279fSFrederic Weisbecker 
603c5cd8ac0SDavid Ahern static int add_tracepoint_multi_event(struct list_head *list, int *idx,
6048c619d6aSWang Nan 				      const char *sys_name, const char *evt_name,
605272ed29aSJiri Olsa 				      struct parse_events_error *err,
606e637d177SHe Kuang 				      struct list_head *head_config)
607bcd3279fSFrederic Weisbecker {
60825a7d914SArnaldo Carvalho de Melo 	char *evt_path;
609bcd3279fSFrederic Weisbecker 	struct dirent *evt_ent;
610bcd3279fSFrederic Weisbecker 	DIR *evt_dir;
61127bf90bfSJiri Olsa 	int ret = 0, found = 0;
612bcd3279fSFrederic Weisbecker 
61325a7d914SArnaldo Carvalho de Melo 	evt_path = get_events_file(sys_name);
61425a7d914SArnaldo Carvalho de Melo 	if (!evt_path) {
61525a7d914SArnaldo Carvalho de Melo 		tracepoint_error(err, errno, sys_name, evt_name);
61625a7d914SArnaldo Carvalho de Melo 		return -1;
61725a7d914SArnaldo Carvalho de Melo 	}
618bcd3279fSFrederic Weisbecker 	evt_dir = opendir(evt_path);
619bcd3279fSFrederic Weisbecker 	if (!evt_dir) {
62025a7d914SArnaldo Carvalho de Melo 		put_events_file(evt_path);
621272ed29aSJiri Olsa 		tracepoint_error(err, errno, sys_name, evt_name);
62289812fc8SJiri Olsa 		return -1;
623bcd3279fSFrederic Weisbecker 	}
624bcd3279fSFrederic Weisbecker 
62589812fc8SJiri Olsa 	while (!ret && (evt_ent = readdir(evt_dir))) {
626bcd3279fSFrederic Weisbecker 		if (!strcmp(evt_ent->d_name, ".")
627bcd3279fSFrederic Weisbecker 		    || !strcmp(evt_ent->d_name, "..")
628bcd3279fSFrederic Weisbecker 		    || !strcmp(evt_ent->d_name, "enable")
629bcd3279fSFrederic Weisbecker 		    || !strcmp(evt_ent->d_name, "filter"))
630bcd3279fSFrederic Weisbecker 			continue;
631bcd3279fSFrederic Weisbecker 
63289812fc8SJiri Olsa 		if (!strglobmatch(evt_ent->d_name, evt_name))
633fb1d2edfSMasami Hiramatsu 			continue;
634fb1d2edfSMasami Hiramatsu 
63527bf90bfSJiri Olsa 		found++;
63627bf90bfSJiri Olsa 
637e637d177SHe Kuang 		ret = add_tracepoint(list, idx, sys_name, evt_ent->d_name,
638272ed29aSJiri Olsa 				     err, head_config);
639bcd3279fSFrederic Weisbecker 	}
640bcd3279fSFrederic Weisbecker 
64127bf90bfSJiri Olsa 	if (!found) {
64227bf90bfSJiri Olsa 		tracepoint_error(err, ENOENT, sys_name, evt_name);
64327bf90bfSJiri Olsa 		ret = -1;
64427bf90bfSJiri Olsa 	}
64527bf90bfSJiri Olsa 
64625a7d914SArnaldo Carvalho de Melo 	put_events_file(evt_path);
6470bd3f084SJiri Olsa 	closedir(evt_dir);
64889812fc8SJiri Olsa 	return ret;
649bcd3279fSFrederic Weisbecker }
650bcd3279fSFrederic Weisbecker 
651c5cd8ac0SDavid Ahern static int add_tracepoint_event(struct list_head *list, int *idx,
6528c619d6aSWang Nan 				const char *sys_name, const char *evt_name,
653272ed29aSJiri Olsa 				struct parse_events_error *err,
654e637d177SHe Kuang 				struct list_head *head_config)
655f35488f9SJiri Olsa {
656f35488f9SJiri Olsa 	return strpbrk(evt_name, "*?") ?
657e637d177SHe Kuang 	       add_tracepoint_multi_event(list, idx, sys_name, evt_name,
658272ed29aSJiri Olsa 					  err, head_config) :
659e637d177SHe Kuang 	       add_tracepoint(list, idx, sys_name, evt_name,
660272ed29aSJiri Olsa 			      err, head_config);
661f35488f9SJiri Olsa }
662f35488f9SJiri Olsa 
663c5cd8ac0SDavid Ahern static int add_tracepoint_multi_sys(struct list_head *list, int *idx,
6648c619d6aSWang Nan 				    const char *sys_name, const char *evt_name,
665272ed29aSJiri Olsa 				    struct parse_events_error *err,
666e637d177SHe Kuang 				    struct list_head *head_config)
667f35488f9SJiri Olsa {
668f35488f9SJiri Olsa 	struct dirent *events_ent;
669f35488f9SJiri Olsa 	DIR *events_dir;
670f35488f9SJiri Olsa 	int ret = 0;
671f35488f9SJiri Olsa 
6727014e0e3SArnaldo Carvalho de Melo 	events_dir = tracing_events__opendir();
673f35488f9SJiri Olsa 	if (!events_dir) {
674272ed29aSJiri Olsa 		tracepoint_error(err, errno, sys_name, evt_name);
675f35488f9SJiri Olsa 		return -1;
676f35488f9SJiri Olsa 	}
677f35488f9SJiri Olsa 
678f35488f9SJiri Olsa 	while (!ret && (events_ent = readdir(events_dir))) {
679f35488f9SJiri Olsa 		if (!strcmp(events_ent->d_name, ".")
680f35488f9SJiri Olsa 		    || !strcmp(events_ent->d_name, "..")
681f35488f9SJiri Olsa 		    || !strcmp(events_ent->d_name, "enable")
682f35488f9SJiri Olsa 		    || !strcmp(events_ent->d_name, "header_event")
683f35488f9SJiri Olsa 		    || !strcmp(events_ent->d_name, "header_page"))
684f35488f9SJiri Olsa 			continue;
685f35488f9SJiri Olsa 
686f35488f9SJiri Olsa 		if (!strglobmatch(events_ent->d_name, sys_name))
687f35488f9SJiri Olsa 			continue;
688f35488f9SJiri Olsa 
689f35488f9SJiri Olsa 		ret = add_tracepoint_event(list, idx, events_ent->d_name,
690272ed29aSJiri Olsa 					   evt_name, err, head_config);
691f35488f9SJiri Olsa 	}
692f35488f9SJiri Olsa 
693f35488f9SJiri Olsa 	closedir(events_dir);
694f35488f9SJiri Olsa 	return ret;
695f35488f9SJiri Olsa }
696f35488f9SJiri Olsa 
697c18cf78dSArnaldo Carvalho de Melo #ifdef HAVE_LIBBPF_SUPPORT
6984edf30e3SWang Nan struct __add_bpf_event_param {
6995d9cdc11SArnaldo Carvalho de Melo 	struct parse_events_state *parse_state;
7004edf30e3SWang Nan 	struct list_head *list;
70195088a59SWang Nan 	struct list_head *head_config;
7024edf30e3SWang Nan };
7034edf30e3SWang Nan 
704af4a0991SArnaldo Carvalho de Melo static int add_bpf_event(const char *group, const char *event, int fd, struct bpf_object *obj,
7054edf30e3SWang Nan 			 void *_param)
7064edf30e3SWang Nan {
7074edf30e3SWang Nan 	LIST_HEAD(new_evsels);
7084edf30e3SWang Nan 	struct __add_bpf_event_param *param = _param;
7095d9cdc11SArnaldo Carvalho de Melo 	struct parse_events_state *parse_state = param->parse_state;
7104edf30e3SWang Nan 	struct list_head *list = param->list;
71132dcd021SJiri Olsa 	struct evsel *pos;
7124edf30e3SWang Nan 	int err;
7132620b7e3SArnaldo Carvalho de Melo 	/*
7142620b7e3SArnaldo Carvalho de Melo 	 * Check if we should add the event, i.e. if it is a TP but starts with a '!',
7152620b7e3SArnaldo Carvalho de Melo 	 * then don't add the tracepoint, this will be used for something else, like
7162620b7e3SArnaldo Carvalho de Melo 	 * adding to a BPF_MAP_TYPE_PROG_ARRAY.
7172620b7e3SArnaldo Carvalho de Melo 	 *
7182620b7e3SArnaldo Carvalho de Melo 	 * See tools/perf/examples/bpf/augmented_raw_syscalls.c
7192620b7e3SArnaldo Carvalho de Melo 	 */
7202620b7e3SArnaldo Carvalho de Melo 	if (group[0] == '!')
7212620b7e3SArnaldo Carvalho de Melo 		return 0;
7224edf30e3SWang Nan 
7234edf30e3SWang Nan 	pr_debug("add bpf event %s:%s and attach bpf program %d\n",
724cd102d70SWang Nan 		 group, event, fd);
7254edf30e3SWang Nan 
7265d369a75SArnaldo Carvalho de Melo 	err = parse_events_add_tracepoint(&new_evsels, &parse_state->idx, group,
7275d369a75SArnaldo Carvalho de Melo 					  event, parse_state->error,
72895088a59SWang Nan 					  param->head_config);
7294edf30e3SWang Nan 	if (err) {
73032dcd021SJiri Olsa 		struct evsel *evsel, *tmp;
7314edf30e3SWang Nan 
7324edf30e3SWang Nan 		pr_debug("Failed to add BPF event %s:%s\n",
733cd102d70SWang Nan 			 group, event);
734b27c4eceSJiri Olsa 		list_for_each_entry_safe(evsel, tmp, &new_evsels, core.node) {
735b27c4eceSJiri Olsa 			list_del_init(&evsel->core.node);
7365eb2dd2aSJiri Olsa 			evsel__delete(evsel);
7374edf30e3SWang Nan 		}
7384edf30e3SWang Nan 		return err;
7394edf30e3SWang Nan 	}
740cd102d70SWang Nan 	pr_debug("adding %s:%s\n", group, event);
7414edf30e3SWang Nan 
742b27c4eceSJiri Olsa 	list_for_each_entry(pos, &new_evsels, core.node) {
7431f45b1d4SWang Nan 		pr_debug("adding %s:%s to %p\n",
744cd102d70SWang Nan 			 group, event, pos);
7451f45b1d4SWang Nan 		pos->bpf_fd = fd;
746af4a0991SArnaldo Carvalho de Melo 		pos->bpf_obj = obj;
7471f45b1d4SWang Nan 	}
7484edf30e3SWang Nan 	list_splice(&new_evsels, list);
7494edf30e3SWang Nan 	return 0;
7504edf30e3SWang Nan }
7514edf30e3SWang Nan 
7525d9cdc11SArnaldo Carvalho de Melo int parse_events_load_bpf_obj(struct parse_events_state *parse_state,
75384c86ca1SWang Nan 			      struct list_head *list,
75495088a59SWang Nan 			      struct bpf_object *obj,
75595088a59SWang Nan 			      struct list_head *head_config)
75684c86ca1SWang Nan {
75784c86ca1SWang Nan 	int err;
75884c86ca1SWang Nan 	char errbuf[BUFSIZ];
7595d9cdc11SArnaldo Carvalho de Melo 	struct __add_bpf_event_param param = {parse_state, list, head_config};
760aa3abf30SWang Nan 	static bool registered_unprobe_atexit = false;
76184c86ca1SWang Nan 
76284c86ca1SWang Nan 	if (IS_ERR(obj) || !obj) {
76384c86ca1SWang Nan 		snprintf(errbuf, sizeof(errbuf),
76484c86ca1SWang Nan 			 "Internal error: load bpf obj with NULL");
76584c86ca1SWang Nan 		err = -EINVAL;
76684c86ca1SWang Nan 		goto errout;
76784c86ca1SWang Nan 	}
76884c86ca1SWang Nan 
76984c86ca1SWang Nan 	/*
770aa3abf30SWang Nan 	 * Register atexit handler before calling bpf__probe() so
771aa3abf30SWang Nan 	 * bpf__probe() don't need to unprobe probe points its already
772aa3abf30SWang Nan 	 * created when failure.
773aa3abf30SWang Nan 	 */
774aa3abf30SWang Nan 	if (!registered_unprobe_atexit) {
775aa3abf30SWang Nan 		atexit(bpf__clear);
776aa3abf30SWang Nan 		registered_unprobe_atexit = true;
777aa3abf30SWang Nan 	}
778aa3abf30SWang Nan 
779aa3abf30SWang Nan 	err = bpf__probe(obj);
780aa3abf30SWang Nan 	if (err) {
781aa3abf30SWang Nan 		bpf__strerror_probe(obj, err, errbuf, sizeof(errbuf));
782aa3abf30SWang Nan 		goto errout;
783aa3abf30SWang Nan 	}
784aa3abf30SWang Nan 
7851e5e3ee8SWang Nan 	err = bpf__load(obj);
7861e5e3ee8SWang Nan 	if (err) {
7871e5e3ee8SWang Nan 		bpf__strerror_load(obj, err, errbuf, sizeof(errbuf));
7881e5e3ee8SWang Nan 		goto errout;
7891e5e3ee8SWang Nan 	}
7901e5e3ee8SWang Nan 
791cd102d70SWang Nan 	err = bpf__foreach_event(obj, add_bpf_event, &param);
7924edf30e3SWang Nan 	if (err) {
7934edf30e3SWang Nan 		snprintf(errbuf, sizeof(errbuf),
7944edf30e3SWang Nan 			 "Attach events in BPF object failed");
7954edf30e3SWang Nan 		goto errout;
7964edf30e3SWang Nan 	}
7974edf30e3SWang Nan 
7984edf30e3SWang Nan 	return 0;
79984c86ca1SWang Nan errout:
8006c191289SIan Rogers 	parse_events_error__handle(parse_state->error, 0,
8015f634c8eSIan Rogers 				strdup(errbuf), strdup("(add -v to see detail)"));
80284c86ca1SWang Nan 	return err;
80384c86ca1SWang Nan }
80484c86ca1SWang Nan 
805a34f3be7SWang Nan static int
8065d9cdc11SArnaldo Carvalho de Melo parse_events_config_bpf(struct parse_events_state *parse_state,
807a34f3be7SWang Nan 			struct bpf_object *obj,
808a34f3be7SWang Nan 			struct list_head *head_config)
809a34f3be7SWang Nan {
810a34f3be7SWang Nan 	struct parse_events_term *term;
811a34f3be7SWang Nan 	int error_pos;
812a34f3be7SWang Nan 
813a34f3be7SWang Nan 	if (!head_config || list_empty(head_config))
814a34f3be7SWang Nan 		return 0;
815a34f3be7SWang Nan 
816a34f3be7SWang Nan 	list_for_each_entry(term, head_config, list) {
817a34f3be7SWang Nan 		int err;
818a34f3be7SWang Nan 
819a34f3be7SWang Nan 		if (term->type_term != PARSE_EVENTS__TERM_TYPE_USER) {
8206c191289SIan Rogers 			parse_events_error__handle(parse_state->error, term->err_term,
8215f634c8eSIan Rogers 						strdup("Invalid config term for BPF object"),
8225f634c8eSIan Rogers 						NULL);
823a34f3be7SWang Nan 			return -EINVAL;
824a34f3be7SWang Nan 		}
825a34f3be7SWang Nan 
8265d9cdc11SArnaldo Carvalho de Melo 		err = bpf__config_obj(obj, term, parse_state->evlist, &error_pos);
827a34f3be7SWang Nan 		if (err) {
8285f634c8eSIan Rogers 			char errbuf[BUFSIZ];
8295f634c8eSIan Rogers 			int idx;
8305f634c8eSIan Rogers 
8315d9cdc11SArnaldo Carvalho de Melo 			bpf__strerror_config_obj(obj, term, parse_state->evlist,
832a34f3be7SWang Nan 						 &error_pos, err, errbuf,
833a34f3be7SWang Nan 						 sizeof(errbuf));
8345f634c8eSIan Rogers 
8355f634c8eSIan Rogers 			if (err == -BPF_LOADER_ERRNO__OBJCONF_MAP_VALUE)
8365f634c8eSIan Rogers 				idx = term->err_val;
8375f634c8eSIan Rogers 			else
8385f634c8eSIan Rogers 				idx = term->err_term + error_pos;
8395f634c8eSIan Rogers 
8406c191289SIan Rogers 			parse_events_error__handle(parse_state->error, idx,
8415f634c8eSIan Rogers 						strdup(errbuf),
8425f634c8eSIan Rogers 						strdup(
8437630b3e2SWang Nan "Hint:\tValid config terms:\n"
844e571e029SWang Nan "     \tmap:[<arraymap>].value<indices>=[value]\n"
845e571e029SWang Nan "     \tmap:[<eventmap>].event<indices>=[event]\n"
8467630b3e2SWang Nan "\n"
847e571e029SWang Nan "     \twhere <indices> is something like [0,3...5] or [all]\n"
8485f634c8eSIan Rogers "     \t(add -v to see detail)"));
849a34f3be7SWang Nan 			return err;
850a34f3be7SWang Nan 		}
851a34f3be7SWang Nan 	}
852a34f3be7SWang Nan 	return 0;
853a34f3be7SWang Nan }
854a34f3be7SWang Nan 
85595088a59SWang Nan /*
85695088a59SWang Nan  * Split config terms:
85795088a59SWang Nan  * perf record -e bpf.c/call-graph=fp,map:array.value[0]=1/ ...
85895088a59SWang Nan  *  'call-graph=fp' is 'evt config', should be applied to each
85995088a59SWang Nan  *  events in bpf.c.
86095088a59SWang Nan  * 'map:array.value[0]=1' is 'obj config', should be processed
86195088a59SWang Nan  * with parse_events_config_bpf.
86295088a59SWang Nan  *
86395088a59SWang Nan  * Move object config terms from the first list to obj_head_config.
86495088a59SWang Nan  */
86595088a59SWang Nan static void
86695088a59SWang Nan split_bpf_config_terms(struct list_head *evt_head_config,
86795088a59SWang Nan 		       struct list_head *obj_head_config)
86895088a59SWang Nan {
86995088a59SWang Nan 	struct parse_events_term *term, *temp;
87095088a59SWang Nan 
87195088a59SWang Nan 	/*
8724d39c89fSIngo Molnar 	 * Currently, all possible user config term
87395088a59SWang Nan 	 * belong to bpf object. parse_events__is_hardcoded_term()
8744d39c89fSIngo Molnar 	 * happens to be a good flag.
87595088a59SWang Nan 	 *
87695088a59SWang Nan 	 * See parse_events_config_bpf() and
87795088a59SWang Nan 	 * config_term_tracepoint().
87895088a59SWang Nan 	 */
87995088a59SWang Nan 	list_for_each_entry_safe(term, temp, evt_head_config, list)
88095088a59SWang Nan 		if (!parse_events__is_hardcoded_term(term))
88195088a59SWang Nan 			list_move_tail(&term->list, obj_head_config);
88295088a59SWang Nan }
88395088a59SWang Nan 
8845d9cdc11SArnaldo Carvalho de Melo int parse_events_load_bpf(struct parse_events_state *parse_state,
88584c86ca1SWang Nan 			  struct list_head *list,
886d509db04SWang Nan 			  char *bpf_file_name,
887a34f3be7SWang Nan 			  bool source,
888a34f3be7SWang Nan 			  struct list_head *head_config)
88984c86ca1SWang Nan {
890a34f3be7SWang Nan 	int err;
89195088a59SWang Nan 	struct bpf_object *obj;
89295088a59SWang Nan 	LIST_HEAD(obj_head_config);
89395088a59SWang Nan 
89495088a59SWang Nan 	if (head_config)
89595088a59SWang Nan 		split_bpf_config_terms(head_config, &obj_head_config);
89684c86ca1SWang Nan 
897d509db04SWang Nan 	obj = bpf__prepare_load(bpf_file_name, source);
8986371ca3bSWang Nan 	if (IS_ERR(obj)) {
89984c86ca1SWang Nan 		char errbuf[BUFSIZ];
90084c86ca1SWang Nan 
9016371ca3bSWang Nan 		err = PTR_ERR(obj);
90284c86ca1SWang Nan 
90384c86ca1SWang Nan 		if (err == -ENOTSUP)
90484c86ca1SWang Nan 			snprintf(errbuf, sizeof(errbuf),
90584c86ca1SWang Nan 				 "BPF support is not compiled");
90684c86ca1SWang Nan 		else
907d3e0ce39SWang Nan 			bpf__strerror_prepare_load(bpf_file_name,
908d3e0ce39SWang Nan 						   source,
909d3e0ce39SWang Nan 						   -err, errbuf,
910d3e0ce39SWang Nan 						   sizeof(errbuf));
91184c86ca1SWang Nan 
9126c191289SIan Rogers 		parse_events_error__handle(parse_state->error, 0,
9135f634c8eSIan Rogers 					strdup(errbuf), strdup("(add -v to see detail)"));
91484c86ca1SWang Nan 		return err;
91584c86ca1SWang Nan 	}
91684c86ca1SWang Nan 
9175d9cdc11SArnaldo Carvalho de Melo 	err = parse_events_load_bpf_obj(parse_state, list, obj, head_config);
918a34f3be7SWang Nan 	if (err)
919a34f3be7SWang Nan 		return err;
9205d9cdc11SArnaldo Carvalho de Melo 	err = parse_events_config_bpf(parse_state, obj, &obj_head_config);
92195088a59SWang Nan 
92295088a59SWang Nan 	/*
92395088a59SWang Nan 	 * Caller doesn't know anything about obj_head_config,
9244d39c89fSIngo Molnar 	 * so combine them together again before returning.
92595088a59SWang Nan 	 */
92695088a59SWang Nan 	if (head_config)
92795088a59SWang Nan 		list_splice_tail(&obj_head_config, head_config);
92895088a59SWang Nan 	return err;
92984c86ca1SWang Nan }
930c18cf78dSArnaldo Carvalho de Melo #else // HAVE_LIBBPF_SUPPORT
931c18cf78dSArnaldo Carvalho de Melo int parse_events_load_bpf_obj(struct parse_events_state *parse_state,
932c18cf78dSArnaldo Carvalho de Melo 			      struct list_head *list __maybe_unused,
933c18cf78dSArnaldo Carvalho de Melo 			      struct bpf_object *obj __maybe_unused,
934c18cf78dSArnaldo Carvalho de Melo 			      struct list_head *head_config __maybe_unused)
935c18cf78dSArnaldo Carvalho de Melo {
9366c191289SIan Rogers 	parse_events_error__handle(parse_state->error, 0,
937c18cf78dSArnaldo Carvalho de Melo 				   strdup("BPF support is not compiled"),
938c18cf78dSArnaldo Carvalho de Melo 				   strdup("Make sure libbpf-devel is available at build time."));
939c18cf78dSArnaldo Carvalho de Melo 	return -ENOTSUP;
940c18cf78dSArnaldo Carvalho de Melo }
941c18cf78dSArnaldo Carvalho de Melo 
942c18cf78dSArnaldo Carvalho de Melo int parse_events_load_bpf(struct parse_events_state *parse_state,
943c18cf78dSArnaldo Carvalho de Melo 			  struct list_head *list __maybe_unused,
944c18cf78dSArnaldo Carvalho de Melo 			  char *bpf_file_name __maybe_unused,
945c18cf78dSArnaldo Carvalho de Melo 			  bool source __maybe_unused,
946c18cf78dSArnaldo Carvalho de Melo 			  struct list_head *head_config __maybe_unused)
947c18cf78dSArnaldo Carvalho de Melo {
9486c191289SIan Rogers 	parse_events_error__handle(parse_state->error, 0,
949c18cf78dSArnaldo Carvalho de Melo 				   strdup("BPF support is not compiled"),
950c18cf78dSArnaldo Carvalho de Melo 				   strdup("Make sure libbpf-devel is available at build time."));
951c18cf78dSArnaldo Carvalho de Melo 	return -ENOTSUP;
952c18cf78dSArnaldo Carvalho de Melo }
953c18cf78dSArnaldo Carvalho de Melo #endif // HAVE_LIBBPF_SUPPORT
95484c86ca1SWang Nan 
95589812fc8SJiri Olsa static int
95689812fc8SJiri Olsa parse_breakpoint_type(const char *type, struct perf_event_attr *attr)
9571b290d67SFrederic Weisbecker {
9581b290d67SFrederic Weisbecker 	int i;
9591b290d67SFrederic Weisbecker 
9601b290d67SFrederic Weisbecker 	for (i = 0; i < 3; i++) {
96189812fc8SJiri Olsa 		if (!type || !type[i])
9621b290d67SFrederic Weisbecker 			break;
9631b290d67SFrederic Weisbecker 
9647582732fSJiri Olsa #define CHECK_SET_TYPE(bit)		\
9657582732fSJiri Olsa do {					\
9667582732fSJiri Olsa 	if (attr->bp_type & bit)	\
9677582732fSJiri Olsa 		return -EINVAL;		\
9687582732fSJiri Olsa 	else				\
9697582732fSJiri Olsa 		attr->bp_type |= bit;	\
9707582732fSJiri Olsa } while (0)
9717582732fSJiri Olsa 
9721b290d67SFrederic Weisbecker 		switch (type[i]) {
9731b290d67SFrederic Weisbecker 		case 'r':
9747582732fSJiri Olsa 			CHECK_SET_TYPE(HW_BREAKPOINT_R);
9751b290d67SFrederic Weisbecker 			break;
9761b290d67SFrederic Weisbecker 		case 'w':
9777582732fSJiri Olsa 			CHECK_SET_TYPE(HW_BREAKPOINT_W);
9781b290d67SFrederic Weisbecker 			break;
9791b290d67SFrederic Weisbecker 		case 'x':
9807582732fSJiri Olsa 			CHECK_SET_TYPE(HW_BREAKPOINT_X);
9811b290d67SFrederic Weisbecker 			break;
9821b290d67SFrederic Weisbecker 		default:
98389812fc8SJiri Olsa 			return -EINVAL;
9841b290d67SFrederic Weisbecker 		}
9851b290d67SFrederic Weisbecker 	}
98689812fc8SJiri Olsa 
9877582732fSJiri Olsa #undef CHECK_SET_TYPE
9887582732fSJiri Olsa 
9891b290d67SFrederic Weisbecker 	if (!attr->bp_type) /* Default */
9901b290d67SFrederic Weisbecker 		attr->bp_type = HW_BREAKPOINT_R | HW_BREAKPOINT_W;
9911b290d67SFrederic Weisbecker 
99289812fc8SJiri Olsa 	return 0;
9931b290d67SFrederic Weisbecker }
9941b290d67SFrederic Weisbecker 
995c5cd8ac0SDavid Ahern int parse_events_add_breakpoint(struct list_head *list, int *idx,
996aa98d848SIan Rogers 				u64 addr, char *type, u64 len)
9971b290d67SFrederic Weisbecker {
99889812fc8SJiri Olsa 	struct perf_event_attr attr;
9991b290d67SFrederic Weisbecker 
100089812fc8SJiri Olsa 	memset(&attr, 0, sizeof(attr));
1001aa98d848SIan Rogers 	attr.bp_addr = addr;
10021b290d67SFrederic Weisbecker 
100389812fc8SJiri Olsa 	if (parse_breakpoint_type(type, &attr))
100489812fc8SJiri Olsa 		return -EINVAL;
10051b290d67SFrederic Weisbecker 
10063741eb9fSJacob Shin 	/* Provide some defaults if len is not specified */
10073741eb9fSJacob Shin 	if (!len) {
100889812fc8SJiri Olsa 		if (attr.bp_type == HW_BREAKPOINT_X)
10093741eb9fSJacob Shin 			len = sizeof(long);
1010aa59a485SFrederic Weisbecker 		else
10113741eb9fSJacob Shin 			len = HW_BREAKPOINT_LEN_4;
10123741eb9fSJacob Shin 	}
10133741eb9fSJacob Shin 
10143741eb9fSJacob Shin 	attr.bp_len = len;
1015aa59a485SFrederic Weisbecker 
101689812fc8SJiri Olsa 	attr.type = PERF_TYPE_BREAKPOINT;
10174a841d65SJovi Zhang 	attr.sample_period = 1;
10181b290d67SFrederic Weisbecker 
10192b62b3a6SIan Rogers 	return add_event(list, idx, &attr, /*name=*/NULL, /*mertic_id=*/NULL,
10202b62b3a6SIan Rogers 			 /*config_terms=*/NULL);
10211b290d67SFrederic Weisbecker }
10221b290d67SFrederic Weisbecker 
10233b0e371cSJiri Olsa static int check_type_val(struct parse_events_term *term,
10243b0e371cSJiri Olsa 			  struct parse_events_error *err,
10253b0e371cSJiri Olsa 			  int type)
10263b0e371cSJiri Olsa {
10273b0e371cSJiri Olsa 	if (type == term->type_val)
10283b0e371cSJiri Olsa 		return 0;
10293b0e371cSJiri Olsa 
10303b0e371cSJiri Olsa 	if (err) {
10316c191289SIan Rogers 		parse_events_error__handle(err, term->err_val,
1032448d732cSIan Rogers 					type == PARSE_EVENTS__TERM_TYPE_NUM
1033448d732cSIan Rogers 					? strdup("expected numeric value")
1034448d732cSIan Rogers 					: strdup("expected string value"),
1035448d732cSIan Rogers 					NULL);
10363b0e371cSJiri Olsa 	}
10373b0e371cSJiri Olsa 	return -EINVAL;
10383b0e371cSJiri Olsa }
10393b0e371cSJiri Olsa 
104017cb5f84SWang Nan /*
104117cb5f84SWang Nan  * Update according to parse-events.l
104217cb5f84SWang Nan  */
104317cb5f84SWang Nan static const char *config_term_names[__PARSE_EVENTS__TERM_TYPE_NR] = {
104417cb5f84SWang Nan 	[PARSE_EVENTS__TERM_TYPE_USER]			= "<sysfs term>",
104517cb5f84SWang Nan 	[PARSE_EVENTS__TERM_TYPE_CONFIG]		= "config",
104617cb5f84SWang Nan 	[PARSE_EVENTS__TERM_TYPE_CONFIG1]		= "config1",
104717cb5f84SWang Nan 	[PARSE_EVENTS__TERM_TYPE_CONFIG2]		= "config2",
104817cb5f84SWang Nan 	[PARSE_EVENTS__TERM_TYPE_NAME]			= "name",
104917cb5f84SWang Nan 	[PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD]		= "period",
105017cb5f84SWang Nan 	[PARSE_EVENTS__TERM_TYPE_SAMPLE_FREQ]		= "freq",
105117cb5f84SWang Nan 	[PARSE_EVENTS__TERM_TYPE_BRANCH_SAMPLE_TYPE]	= "branch_type",
105217cb5f84SWang Nan 	[PARSE_EVENTS__TERM_TYPE_TIME]			= "time",
105317cb5f84SWang Nan 	[PARSE_EVENTS__TERM_TYPE_CALLGRAPH]		= "call-graph",
105417cb5f84SWang Nan 	[PARSE_EVENTS__TERM_TYPE_STACKSIZE]		= "stack-size",
105517cb5f84SWang Nan 	[PARSE_EVENTS__TERM_TYPE_NOINHERIT]		= "no-inherit",
105617cb5f84SWang Nan 	[PARSE_EVENTS__TERM_TYPE_INHERIT]		= "inherit",
1057792d48b4SArnaldo Carvalho de Melo 	[PARSE_EVENTS__TERM_TYPE_MAX_STACK]		= "max-stack",
10582fda5adaSArnaldo Carvalho de Melo 	[PARSE_EVENTS__TERM_TYPE_MAX_EVENTS]		= "nr",
1059626a6b78SWang Nan 	[PARSE_EVENTS__TERM_TYPE_OVERWRITE]		= "overwrite",
1060626a6b78SWang Nan 	[PARSE_EVENTS__TERM_TYPE_NOOVERWRITE]		= "no-overwrite",
1061dd60fba7SMathieu Poirier 	[PARSE_EVENTS__TERM_TYPE_DRV_CFG]		= "driver-config",
1062064b4e82SJin Yao 	[PARSE_EVENTS__TERM_TYPE_PERCORE]		= "percore",
10631b992154SAdrian Hunter 	[PARSE_EVENTS__TERM_TYPE_AUX_OUTPUT]		= "aux-output",
1064eb7a52d4SAdrian Hunter 	[PARSE_EVENTS__TERM_TYPE_AUX_SAMPLE_SIZE]	= "aux-sample-size",
10652b62b3a6SIan Rogers 	[PARSE_EVENTS__TERM_TYPE_METRIC_ID]		= "metric-id",
106617cb5f84SWang Nan };
106717cb5f84SWang Nan 
10681669e509SWang Nan static bool config_term_shrinked;
10691669e509SWang Nan 
10701669e509SWang Nan static bool
10711669e509SWang Nan config_term_avail(int term_type, struct parse_events_error *err)
10721669e509SWang Nan {
1073448d732cSIan Rogers 	char *err_str;
1074448d732cSIan Rogers 
10751669e509SWang Nan 	if (term_type < 0 || term_type >= __PARSE_EVENTS__TERM_TYPE_NR) {
10766c191289SIan Rogers 		parse_events_error__handle(err, -1,
1077448d732cSIan Rogers 					strdup("Invalid term_type"), NULL);
10781669e509SWang Nan 		return false;
10791669e509SWang Nan 	}
10801669e509SWang Nan 	if (!config_term_shrinked)
10811669e509SWang Nan 		return true;
10821669e509SWang Nan 
10831669e509SWang Nan 	switch (term_type) {
10841669e509SWang Nan 	case PARSE_EVENTS__TERM_TYPE_CONFIG:
10851669e509SWang Nan 	case PARSE_EVENTS__TERM_TYPE_CONFIG1:
10861669e509SWang Nan 	case PARSE_EVENTS__TERM_TYPE_CONFIG2:
10871669e509SWang Nan 	case PARSE_EVENTS__TERM_TYPE_NAME:
10882b62b3a6SIan Rogers 	case PARSE_EVENTS__TERM_TYPE_METRIC_ID:
108906835545SSukadev Bhattiprolu 	case PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD:
1090064b4e82SJin Yao 	case PARSE_EVENTS__TERM_TYPE_PERCORE:
10911669e509SWang Nan 		return true;
10921669e509SWang Nan 	default:
10931669e509SWang Nan 		if (!err)
10941669e509SWang Nan 			return false;
10951669e509SWang Nan 
10961669e509SWang Nan 		/* term_type is validated so indexing is safe */
1097448d732cSIan Rogers 		if (asprintf(&err_str, "'%s' is not usable in 'perf stat'",
1098448d732cSIan Rogers 				config_term_names[term_type]) >= 0)
10996c191289SIan Rogers 			parse_events_error__handle(err, -1, err_str, NULL);
11001669e509SWang Nan 		return false;
11011669e509SWang Nan 	}
11021669e509SWang Nan }
11031669e509SWang Nan 
11041669e509SWang Nan void parse_events__shrink_config_terms(void)
11051669e509SWang Nan {
11061669e509SWang Nan 	config_term_shrinked = true;
11071669e509SWang Nan }
11081669e509SWang Nan 
11090b8891a8SHe Kuang static int config_term_common(struct perf_event_attr *attr,
11103b0e371cSJiri Olsa 			      struct parse_events_term *term,
11113b0e371cSJiri Olsa 			      struct parse_events_error *err)
11128f707d84SJiri Olsa {
111316fa7e82SJiri Olsa #define CHECK_TYPE_VAL(type)						   \
111416fa7e82SJiri Olsa do {									   \
11153b0e371cSJiri Olsa 	if (check_type_val(term, err, PARSE_EVENTS__TERM_TYPE_ ## type)) \
111616fa7e82SJiri Olsa 		return -EINVAL;						   \
111716fa7e82SJiri Olsa } while (0)
111816fa7e82SJiri Olsa 
111916fa7e82SJiri Olsa 	switch (term->type_term) {
11208f707d84SJiri Olsa 	case PARSE_EVENTS__TERM_TYPE_CONFIG:
112116fa7e82SJiri Olsa 		CHECK_TYPE_VAL(NUM);
11228f707d84SJiri Olsa 		attr->config = term->val.num;
11238f707d84SJiri Olsa 		break;
11248f707d84SJiri Olsa 	case PARSE_EVENTS__TERM_TYPE_CONFIG1:
112516fa7e82SJiri Olsa 		CHECK_TYPE_VAL(NUM);
11268f707d84SJiri Olsa 		attr->config1 = term->val.num;
11278f707d84SJiri Olsa 		break;
11288f707d84SJiri Olsa 	case PARSE_EVENTS__TERM_TYPE_CONFIG2:
112916fa7e82SJiri Olsa 		CHECK_TYPE_VAL(NUM);
11308f707d84SJiri Olsa 		attr->config2 = term->val.num;
11318f707d84SJiri Olsa 		break;
11328f707d84SJiri Olsa 	case PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD:
113316fa7e82SJiri Olsa 		CHECK_TYPE_VAL(NUM);
11348f707d84SJiri Olsa 		break;
113509af2a55SNamhyung Kim 	case PARSE_EVENTS__TERM_TYPE_SAMPLE_FREQ:
113609af2a55SNamhyung Kim 		CHECK_TYPE_VAL(NUM);
113709af2a55SNamhyung Kim 		break;
11388f707d84SJiri Olsa 	case PARSE_EVENTS__TERM_TYPE_BRANCH_SAMPLE_TYPE:
1139ac12f676SAndi Kleen 		CHECK_TYPE_VAL(STR);
1140ac12f676SAndi Kleen 		if (strcmp(term->val.str, "no") &&
1141448d732cSIan Rogers 		    parse_branch_str(term->val.str,
1142448d732cSIan Rogers 				    &attr->branch_sample_type)) {
11436c191289SIan Rogers 			parse_events_error__handle(err, term->err_val,
1144448d732cSIan Rogers 					strdup("invalid branch sample type"),
1145448d732cSIan Rogers 					NULL);
1146ac12f676SAndi Kleen 			return -EINVAL;
1147ac12f676SAndi Kleen 		}
11488f707d84SJiri Olsa 		break;
114932067712SKan Liang 	case PARSE_EVENTS__TERM_TYPE_TIME:
115032067712SKan Liang 		CHECK_TYPE_VAL(NUM);
115132067712SKan Liang 		if (term->val.num > 1) {
11526c191289SIan Rogers 			parse_events_error__handle(err, term->err_val,
1153448d732cSIan Rogers 						strdup("expected 0 or 1"),
1154448d732cSIan Rogers 						NULL);
115532067712SKan Liang 			return -EINVAL;
115632067712SKan Liang 		}
115732067712SKan Liang 		break;
1158d457c963SKan Liang 	case PARSE_EVENTS__TERM_TYPE_CALLGRAPH:
1159d457c963SKan Liang 		CHECK_TYPE_VAL(STR);
1160d457c963SKan Liang 		break;
1161d457c963SKan Liang 	case PARSE_EVENTS__TERM_TYPE_STACKSIZE:
1162d457c963SKan Liang 		CHECK_TYPE_VAL(NUM);
1163d457c963SKan Liang 		break;
1164374ce938SWang Nan 	case PARSE_EVENTS__TERM_TYPE_INHERIT:
1165374ce938SWang Nan 		CHECK_TYPE_VAL(NUM);
1166374ce938SWang Nan 		break;
1167374ce938SWang Nan 	case PARSE_EVENTS__TERM_TYPE_NOINHERIT:
1168374ce938SWang Nan 		CHECK_TYPE_VAL(NUM);
1169374ce938SWang Nan 		break;
1170626a6b78SWang Nan 	case PARSE_EVENTS__TERM_TYPE_OVERWRITE:
1171626a6b78SWang Nan 		CHECK_TYPE_VAL(NUM);
1172626a6b78SWang Nan 		break;
1173626a6b78SWang Nan 	case PARSE_EVENTS__TERM_TYPE_NOOVERWRITE:
1174626a6b78SWang Nan 		CHECK_TYPE_VAL(NUM);
1175626a6b78SWang Nan 		break;
11766b5fc39bSJiri Olsa 	case PARSE_EVENTS__TERM_TYPE_NAME:
11776b5fc39bSJiri Olsa 		CHECK_TYPE_VAL(STR);
11786b5fc39bSJiri Olsa 		break;
11792b62b3a6SIan Rogers 	case PARSE_EVENTS__TERM_TYPE_METRIC_ID:
11802b62b3a6SIan Rogers 		CHECK_TYPE_VAL(STR);
11812b62b3a6SIan Rogers 		break;
1182792d48b4SArnaldo Carvalho de Melo 	case PARSE_EVENTS__TERM_TYPE_MAX_STACK:
1183792d48b4SArnaldo Carvalho de Melo 		CHECK_TYPE_VAL(NUM);
1184792d48b4SArnaldo Carvalho de Melo 		break;
11852fda5adaSArnaldo Carvalho de Melo 	case PARSE_EVENTS__TERM_TYPE_MAX_EVENTS:
11862fda5adaSArnaldo Carvalho de Melo 		CHECK_TYPE_VAL(NUM);
11872fda5adaSArnaldo Carvalho de Melo 		break;
1188064b4e82SJin Yao 	case PARSE_EVENTS__TERM_TYPE_PERCORE:
1189064b4e82SJin Yao 		CHECK_TYPE_VAL(NUM);
1190064b4e82SJin Yao 		if ((unsigned int)term->val.num > 1) {
11916c191289SIan Rogers 			parse_events_error__handle(err, term->err_val,
1192448d732cSIan Rogers 						strdup("expected 0 or 1"),
1193448d732cSIan Rogers 						NULL);
1194064b4e82SJin Yao 			return -EINVAL;
1195064b4e82SJin Yao 		}
1196064b4e82SJin Yao 		break;
11971b992154SAdrian Hunter 	case PARSE_EVENTS__TERM_TYPE_AUX_OUTPUT:
11981b992154SAdrian Hunter 		CHECK_TYPE_VAL(NUM);
11991b992154SAdrian Hunter 		break;
1200eb7a52d4SAdrian Hunter 	case PARSE_EVENTS__TERM_TYPE_AUX_SAMPLE_SIZE:
1201eb7a52d4SAdrian Hunter 		CHECK_TYPE_VAL(NUM);
1202eb7a52d4SAdrian Hunter 		if (term->val.num > UINT_MAX) {
12036c191289SIan Rogers 			parse_events_error__handle(err, term->err_val,
1204eb7a52d4SAdrian Hunter 						strdup("too big"),
1205eb7a52d4SAdrian Hunter 						NULL);
1206eb7a52d4SAdrian Hunter 			return -EINVAL;
1207eb7a52d4SAdrian Hunter 		}
1208eb7a52d4SAdrian Hunter 		break;
12098f707d84SJiri Olsa 	default:
12106c191289SIan Rogers 		parse_events_error__handle(err, term->err_term,
1211448d732cSIan Rogers 				strdup("unknown term"),
1212448d732cSIan Rogers 				parse_events_formats_error_string(NULL));
12138f707d84SJiri Olsa 		return -EINVAL;
12148f707d84SJiri Olsa 	}
121516fa7e82SJiri Olsa 
12161669e509SWang Nan 	/*
12174d39c89fSIngo Molnar 	 * Check term availability after basic checking so
12181669e509SWang Nan 	 * PARSE_EVENTS__TERM_TYPE_USER can be found and filtered.
12191669e509SWang Nan 	 *
12204d39c89fSIngo Molnar 	 * If check availability at the entry of this function,
12211669e509SWang Nan 	 * user will see "'<sysfs term>' is not usable in 'perf stat'"
12221669e509SWang Nan 	 * if an invalid config term is provided for legacy events
12231669e509SWang Nan 	 * (for example, instructions/badterm/...), which is confusing.
12241669e509SWang Nan 	 */
12251669e509SWang Nan 	if (!config_term_avail(term->type_term, err))
12261669e509SWang Nan 		return -EINVAL;
12278f707d84SJiri Olsa 	return 0;
122816fa7e82SJiri Olsa #undef CHECK_TYPE_VAL
12298f707d84SJiri Olsa }
12308f707d84SJiri Olsa 
12310b8891a8SHe Kuang static int config_term_pmu(struct perf_event_attr *attr,
12320b8891a8SHe Kuang 			   struct parse_events_term *term,
12330b8891a8SHe Kuang 			   struct parse_events_error *err)
12340b8891a8SHe Kuang {
1235dd60fba7SMathieu Poirier 	if (term->type_term == PARSE_EVENTS__TERM_TYPE_USER ||
1236dd60fba7SMathieu Poirier 	    term->type_term == PARSE_EVENTS__TERM_TYPE_DRV_CFG)
12370b8891a8SHe Kuang 		/*
12380b8891a8SHe Kuang 		 * Always succeed for sysfs terms, as we dont know
12390b8891a8SHe Kuang 		 * at this point what type they need to have.
12400b8891a8SHe Kuang 		 */
12410b8891a8SHe Kuang 		return 0;
12420b8891a8SHe Kuang 	else
12430b8891a8SHe Kuang 		return config_term_common(attr, term, err);
12440b8891a8SHe Kuang }
12450b8891a8SHe Kuang 
1246e637d177SHe Kuang static int config_term_tracepoint(struct perf_event_attr *attr,
1247e637d177SHe Kuang 				  struct parse_events_term *term,
1248e637d177SHe Kuang 				  struct parse_events_error *err)
1249e637d177SHe Kuang {
1250e637d177SHe Kuang 	switch (term->type_term) {
1251e637d177SHe Kuang 	case PARSE_EVENTS__TERM_TYPE_CALLGRAPH:
1252e637d177SHe Kuang 	case PARSE_EVENTS__TERM_TYPE_STACKSIZE:
1253374ce938SWang Nan 	case PARSE_EVENTS__TERM_TYPE_INHERIT:
1254374ce938SWang Nan 	case PARSE_EVENTS__TERM_TYPE_NOINHERIT:
1255792d48b4SArnaldo Carvalho de Melo 	case PARSE_EVENTS__TERM_TYPE_MAX_STACK:
12562fda5adaSArnaldo Carvalho de Melo 	case PARSE_EVENTS__TERM_TYPE_MAX_EVENTS:
1257626a6b78SWang Nan 	case PARSE_EVENTS__TERM_TYPE_OVERWRITE:
1258626a6b78SWang Nan 	case PARSE_EVENTS__TERM_TYPE_NOOVERWRITE:
12591b992154SAdrian Hunter 	case PARSE_EVENTS__TERM_TYPE_AUX_OUTPUT:
1260eb7a52d4SAdrian Hunter 	case PARSE_EVENTS__TERM_TYPE_AUX_SAMPLE_SIZE:
1261e637d177SHe Kuang 		return config_term_common(attr, term, err);
1262e637d177SHe Kuang 	default:
1263e637d177SHe Kuang 		if (err) {
12646c191289SIan Rogers 			parse_events_error__handle(err, term->err_term,
1265448d732cSIan Rogers 				strdup("unknown term"),
1266448d732cSIan Rogers 				strdup("valid terms: call-graph,stack-size\n"));
1267e637d177SHe Kuang 		}
1268e637d177SHe Kuang 		return -EINVAL;
1269e637d177SHe Kuang 	}
1270e637d177SHe Kuang 
1271e637d177SHe Kuang 	return 0;
1272e637d177SHe Kuang }
1273e637d177SHe Kuang 
12748f707d84SJiri Olsa static int config_attr(struct perf_event_attr *attr,
12753b0e371cSJiri Olsa 		       struct list_head *head,
12760b8891a8SHe Kuang 		       struct parse_events_error *err,
12770b8891a8SHe Kuang 		       config_term_func_t config_term)
12788f707d84SJiri Olsa {
12796cee6cd3SArnaldo Carvalho de Melo 	struct parse_events_term *term;
12808f707d84SJiri Olsa 
12818f707d84SJiri Olsa 	list_for_each_entry(term, head, list)
12823b0e371cSJiri Olsa 		if (config_term(attr, term, err))
12838f707d84SJiri Olsa 			return -EINVAL;
12848f707d84SJiri Olsa 
12858f707d84SJiri Olsa 	return 0;
12868f707d84SJiri Olsa }
12878f707d84SJiri Olsa 
1288930a2e29SJiri Olsa static int get_config_terms(struct list_head *head_config,
1289930a2e29SJiri Olsa 			    struct list_head *head_terms __maybe_unused)
1290930a2e29SJiri Olsa {
129105e54e23SIan Rogers #define ADD_CONFIG_TERM(__type, __weak)				\
129235ac0cadSArnaldo Carvalho de Melo 	struct evsel_config_term *__t;			\
1293930a2e29SJiri Olsa 								\
1294930a2e29SJiri Olsa 	__t = zalloc(sizeof(*__t));				\
1295930a2e29SJiri Olsa 	if (!__t)						\
1296930a2e29SJiri Olsa 		return -ENOMEM;					\
1297930a2e29SJiri Olsa 								\
1298930a2e29SJiri Olsa 	INIT_LIST_HEAD(&__t->list);				\
129935ac0cadSArnaldo Carvalho de Melo 	__t->type       = EVSEL__CONFIG_TERM_ ## __type;	\
130005e54e23SIan Rogers 	__t->weak	= __weak;				\
1301e884602bSLeo Yan 	list_add_tail(&__t->list, head_terms)
1302e884602bSLeo Yan 
130305e54e23SIan Rogers #define ADD_CONFIG_TERM_VAL(__type, __name, __val, __weak)	\
1304e884602bSLeo Yan do {								\
130505e54e23SIan Rogers 	ADD_CONFIG_TERM(__type, __weak);			\
1306e884602bSLeo Yan 	__t->val.__name = __val;				\
1307e884602bSLeo Yan } while (0)
1308e884602bSLeo Yan 
130905e54e23SIan Rogers #define ADD_CONFIG_TERM_STR(__type, __val, __weak)		\
1310e884602bSLeo Yan do {								\
131105e54e23SIan Rogers 	ADD_CONFIG_TERM(__type, __weak);			\
13123220fb8dSLeo Yan 	__t->val.str = strdup(__val);				\
13133220fb8dSLeo Yan 	if (!__t->val.str) {					\
13143220fb8dSLeo Yan 		zfree(&__t);					\
13153220fb8dSLeo Yan 		return -ENOMEM;					\
13163220fb8dSLeo Yan 	}							\
13173220fb8dSLeo Yan 	__t->free_str = true;					\
1318930a2e29SJiri Olsa } while (0)
1319930a2e29SJiri Olsa 
1320930a2e29SJiri Olsa 	struct parse_events_term *term;
1321930a2e29SJiri Olsa 
1322930a2e29SJiri Olsa 	list_for_each_entry(term, head_config, list) {
1323930a2e29SJiri Olsa 		switch (term->type_term) {
1324ee4c7588SJiri Olsa 		case PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD:
132505e54e23SIan Rogers 			ADD_CONFIG_TERM_VAL(PERIOD, period, term->val.num, term->weak);
132632067712SKan Liang 			break;
132709af2a55SNamhyung Kim 		case PARSE_EVENTS__TERM_TYPE_SAMPLE_FREQ:
132805e54e23SIan Rogers 			ADD_CONFIG_TERM_VAL(FREQ, freq, term->val.num, term->weak);
132909af2a55SNamhyung Kim 			break;
133032067712SKan Liang 		case PARSE_EVENTS__TERM_TYPE_TIME:
133105e54e23SIan Rogers 			ADD_CONFIG_TERM_VAL(TIME, time, term->val.num, term->weak);
133232067712SKan Liang 			break;
1333d457c963SKan Liang 		case PARSE_EVENTS__TERM_TYPE_CALLGRAPH:
133405e54e23SIan Rogers 			ADD_CONFIG_TERM_STR(CALLGRAPH, term->val.str, term->weak);
1335d457c963SKan Liang 			break;
1336ac12f676SAndi Kleen 		case PARSE_EVENTS__TERM_TYPE_BRANCH_SAMPLE_TYPE:
133705e54e23SIan Rogers 			ADD_CONFIG_TERM_STR(BRANCH, term->val.str, term->weak);
1338ac12f676SAndi Kleen 			break;
1339d457c963SKan Liang 		case PARSE_EVENTS__TERM_TYPE_STACKSIZE:
1340e884602bSLeo Yan 			ADD_CONFIG_TERM_VAL(STACK_USER, stack_user,
134105e54e23SIan Rogers 					    term->val.num, term->weak);
1342d457c963SKan Liang 			break;
1343374ce938SWang Nan 		case PARSE_EVENTS__TERM_TYPE_INHERIT:
1344e884602bSLeo Yan 			ADD_CONFIG_TERM_VAL(INHERIT, inherit,
134505e54e23SIan Rogers 					    term->val.num ? 1 : 0, term->weak);
1346374ce938SWang Nan 			break;
1347374ce938SWang Nan 		case PARSE_EVENTS__TERM_TYPE_NOINHERIT:
1348e884602bSLeo Yan 			ADD_CONFIG_TERM_VAL(INHERIT, inherit,
134905e54e23SIan Rogers 					    term->val.num ? 0 : 1, term->weak);
1350374ce938SWang Nan 			break;
1351792d48b4SArnaldo Carvalho de Melo 		case PARSE_EVENTS__TERM_TYPE_MAX_STACK:
1352e884602bSLeo Yan 			ADD_CONFIG_TERM_VAL(MAX_STACK, max_stack,
135305e54e23SIan Rogers 					    term->val.num, term->weak);
1354792d48b4SArnaldo Carvalho de Melo 			break;
13552fda5adaSArnaldo Carvalho de Melo 		case PARSE_EVENTS__TERM_TYPE_MAX_EVENTS:
1356e884602bSLeo Yan 			ADD_CONFIG_TERM_VAL(MAX_EVENTS, max_events,
135705e54e23SIan Rogers 					    term->val.num, term->weak);
13582fda5adaSArnaldo Carvalho de Melo 			break;
1359626a6b78SWang Nan 		case PARSE_EVENTS__TERM_TYPE_OVERWRITE:
1360e884602bSLeo Yan 			ADD_CONFIG_TERM_VAL(OVERWRITE, overwrite,
136105e54e23SIan Rogers 					    term->val.num ? 1 : 0, term->weak);
1362626a6b78SWang Nan 			break;
1363626a6b78SWang Nan 		case PARSE_EVENTS__TERM_TYPE_NOOVERWRITE:
1364e884602bSLeo Yan 			ADD_CONFIG_TERM_VAL(OVERWRITE, overwrite,
136505e54e23SIan Rogers 					    term->val.num ? 0 : 1, term->weak);
1366626a6b78SWang Nan 			break;
1367dd60fba7SMathieu Poirier 		case PARSE_EVENTS__TERM_TYPE_DRV_CFG:
136805e54e23SIan Rogers 			ADD_CONFIG_TERM_STR(DRV_CFG, term->val.str, term->weak);
1369dd60fba7SMathieu Poirier 			break;
1370064b4e82SJin Yao 		case PARSE_EVENTS__TERM_TYPE_PERCORE:
1371e884602bSLeo Yan 			ADD_CONFIG_TERM_VAL(PERCORE, percore,
137205e54e23SIan Rogers 					    term->val.num ? true : false, term->weak);
1373064b4e82SJin Yao 			break;
13741b992154SAdrian Hunter 		case PARSE_EVENTS__TERM_TYPE_AUX_OUTPUT:
1375e884602bSLeo Yan 			ADD_CONFIG_TERM_VAL(AUX_OUTPUT, aux_output,
137605e54e23SIan Rogers 					    term->val.num ? 1 : 0, term->weak);
13771b992154SAdrian Hunter 			break;
1378eb7a52d4SAdrian Hunter 		case PARSE_EVENTS__TERM_TYPE_AUX_SAMPLE_SIZE:
1379e884602bSLeo Yan 			ADD_CONFIG_TERM_VAL(AUX_SAMPLE_SIZE, aux_sample_size,
138005e54e23SIan Rogers 					    term->val.num, term->weak);
1381eb7a52d4SAdrian Hunter 			break;
1382930a2e29SJiri Olsa 		default:
1383930a2e29SJiri Olsa 			break;
1384930a2e29SJiri Olsa 		}
1385930a2e29SJiri Olsa 	}
1386a1ac7de6SAdrian Hunter 	return 0;
1387a1ac7de6SAdrian Hunter }
1388a1ac7de6SAdrian Hunter 
1389a1ac7de6SAdrian Hunter /*
139035ac0cadSArnaldo Carvalho de Melo  * Add EVSEL__CONFIG_TERM_CFG_CHG where cfg_chg will have a bit set for
1391a1ac7de6SAdrian Hunter  * each bit of attr->config that the user has changed.
1392a1ac7de6SAdrian Hunter  */
1393a1ac7de6SAdrian Hunter static int get_config_chgs(struct perf_pmu *pmu, struct list_head *head_config,
1394a1ac7de6SAdrian Hunter 			   struct list_head *head_terms)
1395a1ac7de6SAdrian Hunter {
1396a1ac7de6SAdrian Hunter 	struct parse_events_term *term;
1397a1ac7de6SAdrian Hunter 	u64 bits = 0;
1398a1ac7de6SAdrian Hunter 	int type;
1399a1ac7de6SAdrian Hunter 
1400a1ac7de6SAdrian Hunter 	list_for_each_entry(term, head_config, list) {
1401a1ac7de6SAdrian Hunter 		switch (term->type_term) {
1402a1ac7de6SAdrian Hunter 		case PARSE_EVENTS__TERM_TYPE_USER:
1403a1ac7de6SAdrian Hunter 			type = perf_pmu__format_type(&pmu->format, term->config);
1404a1ac7de6SAdrian Hunter 			if (type != PERF_PMU_FORMAT_VALUE_CONFIG)
1405a1ac7de6SAdrian Hunter 				continue;
1406a1ac7de6SAdrian Hunter 			bits |= perf_pmu__format_bits(&pmu->format, term->config);
1407a1ac7de6SAdrian Hunter 			break;
1408a1ac7de6SAdrian Hunter 		case PARSE_EVENTS__TERM_TYPE_CONFIG:
1409a1ac7de6SAdrian Hunter 			bits = ~(u64)0;
1410a1ac7de6SAdrian Hunter 			break;
1411a1ac7de6SAdrian Hunter 		default:
1412a1ac7de6SAdrian Hunter 			break;
1413a1ac7de6SAdrian Hunter 		}
1414a1ac7de6SAdrian Hunter 	}
1415a1ac7de6SAdrian Hunter 
1416a1ac7de6SAdrian Hunter 	if (bits)
141705e54e23SIan Rogers 		ADD_CONFIG_TERM_VAL(CFG_CHG, cfg_chg, bits, false);
1418a1ac7de6SAdrian Hunter 
1419a1ac7de6SAdrian Hunter #undef ADD_CONFIG_TERM
1420930a2e29SJiri Olsa 	return 0;
1421930a2e29SJiri Olsa }
1422930a2e29SJiri Olsa 
1423e637d177SHe Kuang int parse_events_add_tracepoint(struct list_head *list, int *idx,
14248c619d6aSWang Nan 				const char *sys, const char *event,
1425272ed29aSJiri Olsa 				struct parse_events_error *err,
1426e637d177SHe Kuang 				struct list_head *head_config)
1427e637d177SHe Kuang {
1428e637d177SHe Kuang 	if (head_config) {
1429e637d177SHe Kuang 		struct perf_event_attr attr;
1430e637d177SHe Kuang 
1431272ed29aSJiri Olsa 		if (config_attr(&attr, head_config, err,
1432e637d177SHe Kuang 				config_term_tracepoint))
1433e637d177SHe Kuang 			return -EINVAL;
1434e637d177SHe Kuang 	}
1435e637d177SHe Kuang 
1436e637d177SHe Kuang 	if (strpbrk(sys, "*?"))
1437e637d177SHe Kuang 		return add_tracepoint_multi_sys(list, idx, sys, event,
1438272ed29aSJiri Olsa 						err, head_config);
1439e637d177SHe Kuang 	else
1440e637d177SHe Kuang 		return add_tracepoint_event(list, idx, sys, event,
1441272ed29aSJiri Olsa 					    err, head_config);
1442e637d177SHe Kuang }
1443e637d177SHe Kuang 
14445d9cdc11SArnaldo Carvalho de Melo int parse_events_add_numeric(struct parse_events_state *parse_state,
144587d650beSJiri Olsa 			     struct list_head *list,
1446b527bab5SRobert Richter 			     u32 type, u64 config,
14478f707d84SJiri Olsa 			     struct list_head *head_config)
144874d5b588SJaswinder Singh Rajput {
144989812fc8SJiri Olsa 	struct perf_event_attr attr;
1450930a2e29SJiri Olsa 	LIST_HEAD(config_terms);
14512b62b3a6SIan Rogers 	const char *name, *metric_id;
14529cbfa2f6SJin Yao 	bool hybrid;
14539cbfa2f6SJin Yao 	int ret;
145474d5b588SJaswinder Singh Rajput 
145589812fc8SJiri Olsa 	memset(&attr, 0, sizeof(attr));
145689812fc8SJiri Olsa 	attr.type = type;
145789812fc8SJiri Olsa 	attr.config = config;
14588f707d84SJiri Olsa 
1459930a2e29SJiri Olsa 	if (head_config) {
14605d9cdc11SArnaldo Carvalho de Melo 		if (config_attr(&attr, head_config, parse_state->error,
14610b8891a8SHe Kuang 				config_term_common))
14628f707d84SJiri Olsa 			return -EINVAL;
14638f707d84SJiri Olsa 
1464930a2e29SJiri Olsa 		if (get_config_terms(head_config, &config_terms))
1465930a2e29SJiri Olsa 			return -ENOMEM;
1466930a2e29SJiri Olsa 	}
1467930a2e29SJiri Olsa 
14682b62b3a6SIan Rogers 	name = get_config_name(head_config);
14692b62b3a6SIan Rogers 	metric_id = get_config_metric_id(head_config);
14709cbfa2f6SJin Yao 	ret = parse_events__add_numeric_hybrid(parse_state, list, &attr,
14712b62b3a6SIan Rogers 					       name, metric_id,
14729cbfa2f6SJin Yao 					       &config_terms, &hybrid);
14739cbfa2f6SJin Yao 	if (hybrid)
147499fc5941SAdrian Hunter 		goto out_free_terms;
14759cbfa2f6SJin Yao 
14762b62b3a6SIan Rogers 	ret = add_event(list, &parse_state->idx, &attr, name, metric_id,
14772b62b3a6SIan Rogers 			&config_terms);
147899fc5941SAdrian Hunter out_free_terms:
147999fc5941SAdrian Hunter 	free_config_terms(&config_terms);
148099fc5941SAdrian Hunter 	return ret;
1481b908debdSIngo Molnar }
1482b908debdSIngo Molnar 
1483f0fbb114SAndi Kleen int parse_events_add_tool(struct parse_events_state *parse_state,
1484f0fbb114SAndi Kleen 			  struct list_head *list,
14858228e936SIan Rogers 			  int tool_event)
1486f0fbb114SAndi Kleen {
1487f0fbb114SAndi Kleen 	return add_event_tool(list, &parse_state->idx, tool_event);
1488f0fbb114SAndi Kleen }
1489f0fbb114SAndi Kleen 
1490064b4e82SJin Yao static bool config_term_percore(struct list_head *config_terms)
1491064b4e82SJin Yao {
149235ac0cadSArnaldo Carvalho de Melo 	struct evsel_config_term *term;
1493064b4e82SJin Yao 
1494064b4e82SJin Yao 	list_for_each_entry(term, config_terms, list) {
149535ac0cadSArnaldo Carvalho de Melo 		if (term->type == EVSEL__CONFIG_TERM_PERCORE)
1496064b4e82SJin Yao 			return term->val.percore;
1497064b4e82SJin Yao 	}
1498064b4e82SJin Yao 
1499064b4e82SJin Yao 	return false;
1500064b4e82SJin Yao }
1501064b4e82SJin Yao 
15025e4edd1fSJin Yao static int parse_events__inside_hybrid_pmu(struct parse_events_state *parse_state,
15035e4edd1fSJin Yao 					   struct list_head *list, char *name,
15045e4edd1fSJin Yao 					   struct list_head *head_config)
15055e4edd1fSJin Yao {
15065e4edd1fSJin Yao 	struct parse_events_term *term;
15075e4edd1fSJin Yao 	int ret = -1;
15085e4edd1fSJin Yao 
15095e4edd1fSJin Yao 	if (parse_state->fake_pmu || !head_config || list_empty(head_config) ||
15105e4edd1fSJin Yao 	    !perf_pmu__is_hybrid(name)) {
15115e4edd1fSJin Yao 		return -1;
15125e4edd1fSJin Yao 	}
15135e4edd1fSJin Yao 
15145e4edd1fSJin Yao 	/*
15155e4edd1fSJin Yao 	 * More than one term in list.
15165e4edd1fSJin Yao 	 */
15175e4edd1fSJin Yao 	if (head_config->next && head_config->next->next != head_config)
15185e4edd1fSJin Yao 		return -1;
15195e4edd1fSJin Yao 
15205e4edd1fSJin Yao 	term = list_first_entry(head_config, struct parse_events_term, list);
15215e4edd1fSJin Yao 	if (term && term->config && strcmp(term->config, "event")) {
15225e4edd1fSJin Yao 		ret = parse_events__with_hybrid_pmu(parse_state, term->config,
15235e4edd1fSJin Yao 						    name, list);
15245e4edd1fSJin Yao 	}
15255e4edd1fSJin Yao 
15265e4edd1fSJin Yao 	return ret;
15275e4edd1fSJin Yao }
15285e4edd1fSJin Yao 
1529c199c11dSAgustin Vega-Frias int parse_events_add_pmu(struct parse_events_state *parse_state,
153036adec85SJiri Olsa 			 struct list_head *list, char *name,
15313cdc5c2cSKan Liang 			 struct list_head *head_config,
15323cdc5c2cSKan Liang 			 bool auto_merge_stats,
15333cdc5c2cSKan Liang 			 bool use_alias)
15345f537a26SJiri Olsa {
15355f537a26SJiri Olsa 	struct perf_event_attr attr;
153646441bdcSMatt Fleming 	struct perf_pmu_info info;
15375f537a26SJiri Olsa 	struct perf_pmu *pmu;
153832dcd021SJiri Olsa 	struct evsel *evsel;
1539333b5665SAndi Kleen 	struct parse_events_error *err = parse_state->error;
15403cdc5c2cSKan Liang 	bool use_uncore_alias;
1541930a2e29SJiri Olsa 	LIST_HEAD(config_terms);
15425f537a26SJiri Olsa 
1543f034fc50SAdrian Hunter 	pmu = parse_state->fake_pmu ?: perf_pmu__find(name);
1544f034fc50SAdrian Hunter 
1545f034fc50SAdrian Hunter 	if (verbose > 1 && !(pmu && pmu->selectable)) {
15464ac22b48SIan Rogers 		fprintf(stderr, "Attempting to add event pmu '%s' with '",
15474ac22b48SIan Rogers 			name);
15484ac22b48SIan Rogers 		if (head_config) {
15494ac22b48SIan Rogers 			struct parse_events_term *term;
15504ac22b48SIan Rogers 
15514ac22b48SIan Rogers 			list_for_each_entry(term, head_config, list) {
15524ac22b48SIan Rogers 				fprintf(stderr, "%s,", term->config);
15534ac22b48SIan Rogers 			}
15544ac22b48SIan Rogers 		}
15554ac22b48SIan Rogers 		fprintf(stderr, "' that may result in non-fatal errors\n");
15564ac22b48SIan Rogers 	}
15574ac22b48SIan Rogers 
1558333b5665SAndi Kleen 	if (!pmu) {
1559448d732cSIan Rogers 		char *err_str;
1560448d732cSIan Rogers 
1561448d732cSIan Rogers 		if (asprintf(&err_str,
1562333b5665SAndi Kleen 				"Cannot find PMU `%s'. Missing kernel support?",
1563448d732cSIan Rogers 				name) >= 0)
15646c191289SIan Rogers 			parse_events_error__handle(err, 0, err_str, NULL);
15655f537a26SJiri Olsa 		return -EINVAL;
1566333b5665SAndi Kleen 	}
15675f537a26SJiri Olsa 
1568dc0a6202SAdrian Hunter 	if (pmu->default_config) {
1569dc0a6202SAdrian Hunter 		memcpy(&attr, pmu->default_config,
1570dc0a6202SAdrian Hunter 		       sizeof(struct perf_event_attr));
1571dc0a6202SAdrian Hunter 	} else {
15725f537a26SJiri Olsa 		memset(&attr, 0, sizeof(attr));
1573dc0a6202SAdrian Hunter 	}
15745f537a26SJiri Olsa 
15753cdc5c2cSKan Liang 	use_uncore_alias = (pmu->is_uncore && use_alias);
15763cdc5c2cSKan Liang 
1577ad962273SAdrian Hunter 	if (!head_config) {
1578ad962273SAdrian Hunter 		attr.type = pmu->type;
15792b62b3a6SIan Rogers 		evsel = __add_event(list, &parse_state->idx, &attr,
15802b62b3a6SIan Rogers 				    /*init_attr=*/true, /*name=*/NULL,
15812b62b3a6SIan Rogers 				    /*metric_id=*/NULL, pmu,
15822b62b3a6SIan Rogers 				    /*config_terms=*/NULL, auto_merge_stats,
15832b62b3a6SIan Rogers 				    /*cpu_list=*/NULL);
15848c5421c0SAgustin Vega-Frias 		if (evsel) {
1585d4953f7eSIan Rogers 			evsel->pmu_name = name ? strdup(name) : NULL;
15863cdc5c2cSKan Liang 			evsel->use_uncore_alias = use_uncore_alias;
15878c5421c0SAgustin Vega-Frias 			return 0;
15888c5421c0SAgustin Vega-Frias 		} else {
15898c5421c0SAgustin Vega-Frias 			return -ENOMEM;
15908c5421c0SAgustin Vega-Frias 		}
1591ad962273SAdrian Hunter 	}
1592ad962273SAdrian Hunter 
1593387ad33fSJiri Olsa 	if (!parse_state->fake_pmu && perf_pmu__check_alias(pmu, head_config, &info))
1594a6146d50SZheng Yan 		return -EINVAL;
1595a6146d50SZheng Yan 
15964ac22b48SIan Rogers 	if (verbose > 1) {
15974ac22b48SIan Rogers 		fprintf(stderr, "After aliases, add event pmu '%s' with '",
15984ac22b48SIan Rogers 			name);
15994ac22b48SIan Rogers 		if (head_config) {
16004ac22b48SIan Rogers 			struct parse_events_term *term;
16014ac22b48SIan Rogers 
16024ac22b48SIan Rogers 			list_for_each_entry(term, head_config, list) {
16034ac22b48SIan Rogers 				fprintf(stderr, "%s,", term->config);
16044ac22b48SIan Rogers 			}
16054ac22b48SIan Rogers 		}
16064ac22b48SIan Rogers 		fprintf(stderr, "' that may result in non-fatal errors\n");
16074ac22b48SIan Rogers 	}
16084ac22b48SIan Rogers 
16095f537a26SJiri Olsa 	/*
16105f537a26SJiri Olsa 	 * Configure hardcoded terms first, no need to check
16115f537a26SJiri Olsa 	 * return value when called with fail == 0 ;)
16125f537a26SJiri Olsa 	 */
16135d9cdc11SArnaldo Carvalho de Melo 	if (config_attr(&attr, head_config, parse_state->error, config_term_pmu))
1614c056ba6aSJiri Olsa 		return -EINVAL;
16155f537a26SJiri Olsa 
1616930a2e29SJiri Olsa 	if (get_config_terms(head_config, &config_terms))
1617930a2e29SJiri Olsa 		return -ENOMEM;
1618930a2e29SJiri Olsa 
1619a1ac7de6SAdrian Hunter 	/*
1620a1ac7de6SAdrian Hunter 	 * When using default config, record which bits of attr->config were
1621a1ac7de6SAdrian Hunter 	 * changed by the user.
1622a1ac7de6SAdrian Hunter 	 */
1623a1ac7de6SAdrian Hunter 	if (pmu->default_config && get_config_chgs(pmu, head_config, &config_terms))
1624a1ac7de6SAdrian Hunter 		return -ENOMEM;
1625a1ac7de6SAdrian Hunter 
16265e4edd1fSJin Yao 	if (!parse_events__inside_hybrid_pmu(parse_state, list, name,
16275e4edd1fSJin Yao 					     head_config)) {
16285e4edd1fSJin Yao 		return 0;
16295e4edd1fSJin Yao 	}
16305e4edd1fSJin Yao 
1631387ad33fSJiri Olsa 	if (!parse_state->fake_pmu && perf_pmu__config(pmu, &attr, head_config, parse_state->error)) {
1632a7d212fcSAdrian Hunter 		free_config_terms(&config_terms);
16335f537a26SJiri Olsa 		return -EINVAL;
163438f2c422SIan Rogers 	}
16355f537a26SJiri Olsa 
16362b62b3a6SIan Rogers 	evsel = __add_event(list, &parse_state->idx, &attr, /*init_attr=*/true,
16372b62b3a6SIan Rogers 			    get_config_name(head_config),
16382b62b3a6SIan Rogers 			    get_config_metric_id(head_config), pmu,
16392b62b3a6SIan Rogers 			    &config_terms, auto_merge_stats, /*cpu_list=*/NULL);
164033321a06SIan Rogers 	if (!evsel)
164133321a06SIan Rogers 		return -ENOMEM;
164233321a06SIan Rogers 
164312279429SJin Yao 	if (evsel->name)
164412279429SJin Yao 		evsel->use_config_name = true;
164512279429SJin Yao 
164633321a06SIan Rogers 	evsel->pmu_name = name ? strdup(name) : NULL;
164733321a06SIan Rogers 	evsel->use_uncore_alias = use_uncore_alias;
164833321a06SIan Rogers 	evsel->percore = config_term_percore(&evsel->config_terms);
164933321a06SIan Rogers 
165033321a06SIan Rogers 	if (parse_state->fake_pmu)
165133321a06SIan Rogers 		return 0;
165233321a06SIan Rogers 
1653b194c9cdSIan Rogers 	free((char *)evsel->unit);
1654b194c9cdSIan Rogers 	evsel->unit = strdup(info.unit);
165546441bdcSMatt Fleming 	evsel->scale = info.scale;
1656044330c1SMatt Fleming 	evsel->per_pkg = info.per_pkg;
16571d9e446bSJiri Olsa 	evsel->snapshot = info.snapshot;
165837932c18SAndi Kleen 	evsel->metric_expr = info.metric_expr;
165996284814SAndi Kleen 	evsel->metric_name = info.metric_name;
166033321a06SIan Rogers 	return 0;
16615f537a26SJiri Olsa }
16625f537a26SJiri Olsa 
16635d9cdc11SArnaldo Carvalho de Melo int parse_events_multi_pmu_add(struct parse_events_state *parse_state,
1664fb081153SIan Rogers 			       char *str, struct list_head *head,
1665fb081153SIan Rogers 			       struct list_head **listp)
16662073ad33SAndi Kleen {
16672073ad33SAndi Kleen 	struct parse_events_term *term;
1668fb081153SIan Rogers 	struct list_head *list = NULL;
166991c9923aSZhengjun Xing 	struct list_head *orig_head = NULL;
16702073ad33SAndi Kleen 	struct perf_pmu *pmu = NULL;
16712073ad33SAndi Kleen 	int ok = 0;
1672fb081153SIan Rogers 	char *config;
16732073ad33SAndi Kleen 
16742073ad33SAndi Kleen 	*listp = NULL;
1675fb081153SIan Rogers 
1676fb081153SIan Rogers 	if (!head) {
1677fb081153SIan Rogers 		head = malloc(sizeof(struct list_head));
1678fb081153SIan Rogers 		if (!head)
1679fb081153SIan Rogers 			goto out_err;
1680fb081153SIan Rogers 
1681fb081153SIan Rogers 		INIT_LIST_HEAD(head);
1682fb081153SIan Rogers 	}
1683fb081153SIan Rogers 	config = strdup(str);
1684fb081153SIan Rogers 	if (!config)
1685fb081153SIan Rogers 		goto out_err;
1686fb081153SIan Rogers 
1687fb081153SIan Rogers 	if (parse_events_term__num(&term,
1688fb081153SIan Rogers 				   PARSE_EVENTS__TERM_TYPE_USER,
1689fb081153SIan Rogers 				   config, 1, false, &config,
1690fb081153SIan Rogers 					NULL) < 0) {
1691fb081153SIan Rogers 		free(config);
1692fb081153SIan Rogers 		goto out_err;
1693fb081153SIan Rogers 	}
1694fb081153SIan Rogers 	list_add_tail(&term->list, head);
1695fb081153SIan Rogers 
16962073ad33SAndi Kleen 	/* Add it for all PMUs that support the alias */
16972073ad33SAndi Kleen 	list = malloc(sizeof(struct list_head));
16982073ad33SAndi Kleen 	if (!list)
1699fb081153SIan Rogers 		goto out_err;
1700fb081153SIan Rogers 
17012073ad33SAndi Kleen 	INIT_LIST_HEAD(list);
1702fb081153SIan Rogers 
17032073ad33SAndi Kleen 	while ((pmu = perf_pmu__scan(pmu)) != NULL) {
17042073ad33SAndi Kleen 		struct perf_pmu_alias *alias;
17052073ad33SAndi Kleen 
17062073ad33SAndi Kleen 		list_for_each_entry(alias, &pmu->aliases, list) {
17072073ad33SAndi Kleen 			if (!strcasecmp(alias->name, str)) {
170891c9923aSZhengjun Xing 				parse_events_copy_term_list(head, &orig_head);
1709c199c11dSAgustin Vega-Frias 				if (!parse_events_add_pmu(parse_state, list,
171091c9923aSZhengjun Xing 							  pmu->name, orig_head,
17113cdc5c2cSKan Liang 							  true, true)) {
1712d4953f7eSIan Rogers 					pr_debug("%s -> %s/%s/\n", str,
17132073ad33SAndi Kleen 						 pmu->name, alias->str);
17142073ad33SAndi Kleen 					ok++;
17152073ad33SAndi Kleen 				}
171691c9923aSZhengjun Xing 				parse_events_terms__delete(orig_head);
1717fb081153SIan Rogers 			}
1718fb081153SIan Rogers 		}
1719fb081153SIan Rogers 	}
1720b4a7276cSJohn Garry 
1721b4a7276cSJohn Garry 	if (parse_state->fake_pmu) {
1722b4a7276cSJohn Garry 		if (!parse_events_add_pmu(parse_state, list, str, head,
1723b4a7276cSJohn Garry 					  true, true)) {
1724b4a7276cSJohn Garry 			pr_debug("%s -> %s/%s/\n", str, "fake_pmu", str);
1725b4a7276cSJohn Garry 			ok++;
1726b4a7276cSJohn Garry 		}
1727b4a7276cSJohn Garry 	}
1728b4a7276cSJohn Garry 
1729fb081153SIan Rogers out_err:
1730fb081153SIan Rogers 	if (ok)
1731fb081153SIan Rogers 		*listp = list;
1732fb081153SIan Rogers 	else
1733fb081153SIan Rogers 		free(list);
17342073ad33SAndi Kleen 
17352073ad33SAndi Kleen 	parse_events_terms__delete(head);
1736fb081153SIan Rogers 	return ok ? 0 : -1;
17372073ad33SAndi Kleen }
17382073ad33SAndi Kleen 
17396a4bb04cSJiri Olsa int parse_events__modifier_group(struct list_head *list,
17406a4bb04cSJiri Olsa 				 char *event_mod)
174189efb029SJiri Olsa {
17426a4bb04cSJiri Olsa 	return parse_events__modifier_event(list, event_mod, true);
17436a4bb04cSJiri Olsa }
17446a4bb04cSJiri Olsa 
17453cdc5c2cSKan Liang /*
17463cdc5c2cSKan Liang  * Check if the two uncore PMUs are from the same uncore block
17473cdc5c2cSKan Liang  * The format of the uncore PMU name is uncore_#blockname_#pmuidx
17483cdc5c2cSKan Liang  */
17493cdc5c2cSKan Liang static bool is_same_uncore_block(const char *pmu_name_a, const char *pmu_name_b)
17503cdc5c2cSKan Liang {
17513cdc5c2cSKan Liang 	char *end_a, *end_b;
17523cdc5c2cSKan Liang 
17533cdc5c2cSKan Liang 	end_a = strrchr(pmu_name_a, '_');
17543cdc5c2cSKan Liang 	end_b = strrchr(pmu_name_b, '_');
17553cdc5c2cSKan Liang 
17563cdc5c2cSKan Liang 	if (!end_a || !end_b)
17573cdc5c2cSKan Liang 		return false;
17583cdc5c2cSKan Liang 
17593cdc5c2cSKan Liang 	if ((end_a - pmu_name_a) != (end_b - pmu_name_b))
17603cdc5c2cSKan Liang 		return false;
17613cdc5c2cSKan Liang 
17623cdc5c2cSKan Liang 	return (strncmp(pmu_name_a, pmu_name_b, end_a - pmu_name_a) == 0);
17633cdc5c2cSKan Liang }
17643cdc5c2cSKan Liang 
17653cdc5c2cSKan Liang static int
17663cdc5c2cSKan Liang parse_events__set_leader_for_uncore_aliase(char *name, struct list_head *list,
17673cdc5c2cSKan Liang 					   struct parse_events_state *parse_state)
17683cdc5c2cSKan Liang {
176932dcd021SJiri Olsa 	struct evsel *evsel, *leader;
17703cdc5c2cSKan Liang 	uintptr_t *leaders;
17713cdc5c2cSKan Liang 	bool is_leader = true;
17723cdc5c2cSKan Liang 	int i, nr_pmu = 0, total_members, ret = 0;
17733cdc5c2cSKan Liang 
1774b27c4eceSJiri Olsa 	leader = list_first_entry(list, struct evsel, core.node);
1775b27c4eceSJiri Olsa 	evsel = list_last_entry(list, struct evsel, core.node);
177638fe0e01SJiri Olsa 	total_members = evsel->core.idx - leader->core.idx + 1;
17773cdc5c2cSKan Liang 
17783cdc5c2cSKan Liang 	leaders = calloc(total_members, sizeof(uintptr_t));
17793cdc5c2cSKan Liang 	if (WARN_ON(!leaders))
17803cdc5c2cSKan Liang 		return 0;
17813cdc5c2cSKan Liang 
17823cdc5c2cSKan Liang 	/*
17833cdc5c2cSKan Liang 	 * Going through the whole group and doing sanity check.
17843cdc5c2cSKan Liang 	 * All members must use alias, and be from the same uncore block.
17853cdc5c2cSKan Liang 	 * Also, storing the leader events in an array.
17863cdc5c2cSKan Liang 	 */
17873cdc5c2cSKan Liang 	__evlist__for_each_entry(list, evsel) {
17883cdc5c2cSKan Liang 
17893cdc5c2cSKan Liang 		/* Only split the uncore group which members use alias */
17903cdc5c2cSKan Liang 		if (!evsel->use_uncore_alias)
17913cdc5c2cSKan Liang 			goto out;
17923cdc5c2cSKan Liang 
17933cdc5c2cSKan Liang 		/* The events must be from the same uncore block */
17943cdc5c2cSKan Liang 		if (!is_same_uncore_block(leader->pmu_name, evsel->pmu_name))
17953cdc5c2cSKan Liang 			goto out;
17963cdc5c2cSKan Liang 
17973cdc5c2cSKan Liang 		if (!is_leader)
17983cdc5c2cSKan Liang 			continue;
17993cdc5c2cSKan Liang 		/*
18003cdc5c2cSKan Liang 		 * If the event's PMU name starts to repeat, it must be a new
18013cdc5c2cSKan Liang 		 * event. That can be used to distinguish the leader from
18023cdc5c2cSKan Liang 		 * other members, even they have the same event name.
18033cdc5c2cSKan Liang 		 */
18048510895bSJin Yao 		if ((leader != evsel) &&
18058510895bSJin Yao 		    !strcmp(leader->pmu_name, evsel->pmu_name)) {
18063cdc5c2cSKan Liang 			is_leader = false;
18073cdc5c2cSKan Liang 			continue;
18083cdc5c2cSKan Liang 		}
18093cdc5c2cSKan Liang 
18103cdc5c2cSKan Liang 		/* Store the leader event for each PMU */
18113cdc5c2cSKan Liang 		leaders[nr_pmu++] = (uintptr_t) evsel;
18123cdc5c2cSKan Liang 	}
18133cdc5c2cSKan Liang 
18143cdc5c2cSKan Liang 	/* only one event alias */
18153cdc5c2cSKan Liang 	if (nr_pmu == total_members) {
18163cdc5c2cSKan Liang 		parse_state->nr_groups--;
18173cdc5c2cSKan Liang 		goto handled;
18183cdc5c2cSKan Liang 	}
18193cdc5c2cSKan Liang 
18203cdc5c2cSKan Liang 	/*
18213cdc5c2cSKan Liang 	 * An uncore event alias is a joint name which means the same event
18223cdc5c2cSKan Liang 	 * runs on all PMUs of a block.
18233cdc5c2cSKan Liang 	 * Perf doesn't support mixed events from different PMUs in the same
18243cdc5c2cSKan Liang 	 * group. The big group has to be split into multiple small groups
18253cdc5c2cSKan Liang 	 * which only include the events from the same PMU.
18263cdc5c2cSKan Liang 	 *
18273cdc5c2cSKan Liang 	 * Here the uncore event aliases must be from the same uncore block.
18283cdc5c2cSKan Liang 	 * The number of PMUs must be same for each alias. The number of new
18293cdc5c2cSKan Liang 	 * small groups equals to the number of PMUs.
18303cdc5c2cSKan Liang 	 * Setting the leader event for corresponding members in each group.
18313cdc5c2cSKan Liang 	 */
18323cdc5c2cSKan Liang 	i = 0;
18333cdc5c2cSKan Liang 	__evlist__for_each_entry(list, evsel) {
18343cdc5c2cSKan Liang 		if (i >= nr_pmu)
18353cdc5c2cSKan Liang 			i = 0;
1836fba7c866SJiri Olsa 		evsel__set_leader(evsel, (struct evsel *) leaders[i++]);
18373cdc5c2cSKan Liang 	}
18383cdc5c2cSKan Liang 
18393cdc5c2cSKan Liang 	/* The number of members and group name are same for each group */
18403cdc5c2cSKan Liang 	for (i = 0; i < nr_pmu; i++) {
184132dcd021SJiri Olsa 		evsel = (struct evsel *) leaders[i];
18425643b1a5SJiri Olsa 		evsel->core.nr_members = total_members / nr_pmu;
18433cdc5c2cSKan Liang 		evsel->group_name = name ? strdup(name) : NULL;
18443cdc5c2cSKan Liang 	}
18453cdc5c2cSKan Liang 
18463cdc5c2cSKan Liang 	/* Take the new small groups into account */
18473cdc5c2cSKan Liang 	parse_state->nr_groups += nr_pmu - 1;
18483cdc5c2cSKan Liang 
18493cdc5c2cSKan Liang handled:
18503cdc5c2cSKan Liang 	ret = 1;
18513cdc5c2cSKan Liang out:
18523cdc5c2cSKan Liang 	free(leaders);
18533cdc5c2cSKan Liang 	return ret;
18543cdc5c2cSKan Liang }
18553cdc5c2cSKan Liang 
185694dbfd67SIan Rogers __weak struct evsel *arch_evlist__leader(struct list_head *list)
185794dbfd67SIan Rogers {
185894dbfd67SIan Rogers 	return list_first_entry(list, struct evsel, core.node);
185994dbfd67SIan Rogers }
186094dbfd67SIan Rogers 
18613cdc5c2cSKan Liang void parse_events__set_leader(char *name, struct list_head *list,
18623cdc5c2cSKan Liang 			      struct parse_events_state *parse_state)
18636a4bb04cSJiri Olsa {
186432dcd021SJiri Olsa 	struct evsel *leader;
18656a4bb04cSJiri Olsa 
1866854f7363SWang Nan 	if (list_empty(list)) {
1867854f7363SWang Nan 		WARN_ONCE(true, "WARNING: failed to set leader: empty list");
1868854f7363SWang Nan 		return;
1869854f7363SWang Nan 	}
1870854f7363SWang Nan 
18713cdc5c2cSKan Liang 	if (parse_events__set_leader_for_uncore_aliase(name, list, parse_state))
18723cdc5c2cSKan Liang 		return;
18733cdc5c2cSKan Liang 
187494dbfd67SIan Rogers 	leader = arch_evlist__leader(list);
1875ecdcf630SIan Rogers 	__perf_evlist__set_leader(list, &leader->core);
18766a4bb04cSJiri Olsa 	leader->group_name = name ? strdup(name) : NULL;
187794dbfd67SIan Rogers 	list_move(&leader->core.node, list);
187889efb029SJiri Olsa }
187989efb029SJiri Olsa 
1880c5cd8ac0SDavid Ahern /* list_event is assumed to point to malloc'ed memory */
18815d7be90eSJiri Olsa void parse_events_update_lists(struct list_head *list_event,
18825d7be90eSJiri Olsa 			       struct list_head *list_all)
18835d7be90eSJiri Olsa {
18845d7be90eSJiri Olsa 	/*
18855d7be90eSJiri Olsa 	 * Called for single event definition. Update the
188689efb029SJiri Olsa 	 * 'all event' list, and reinit the 'single event'
18875d7be90eSJiri Olsa 	 * list, for next event definition.
18885d7be90eSJiri Olsa 	 */
18895d7be90eSJiri Olsa 	list_splice_tail(list_event, list_all);
1890b847cbdcSJiri Olsa 	free(list_event);
18915d7be90eSJiri Olsa }
18925d7be90eSJiri Olsa 
1893f5b1135bSJiri Olsa struct event_modifier {
1894f5b1135bSJiri Olsa 	int eu;
1895f5b1135bSJiri Olsa 	int ek;
1896f5b1135bSJiri Olsa 	int eh;
1897f5b1135bSJiri Olsa 	int eH;
1898f5b1135bSJiri Olsa 	int eG;
1899a1e12da4SJiri Olsa 	int eI;
1900f5b1135bSJiri Olsa 	int precise;
19017f94af7aSJiri Olsa 	int precise_max;
1902f5b1135bSJiri Olsa 	int exclude_GH;
19033c176311SJiri Olsa 	int sample_read;
1904e9a7c414SMichael Ellerman 	int pinned;
19055a5dfe4bSAndi Kleen 	int weak;
19060997a266SAndi Kleen 	int exclusive;
190701bd8efcSSong Liu 	int bpf_counter;
1908f5b1135bSJiri Olsa };
190961c45981SPaul Mackerras 
1910f5b1135bSJiri Olsa static int get_event_modifier(struct event_modifier *mod, char *str,
191132dcd021SJiri Olsa 			       struct evsel *evsel)
1912f5b1135bSJiri Olsa {
19131fc632ceSJiri Olsa 	int eu = evsel ? evsel->core.attr.exclude_user : 0;
19141fc632ceSJiri Olsa 	int ek = evsel ? evsel->core.attr.exclude_kernel : 0;
19151fc632ceSJiri Olsa 	int eh = evsel ? evsel->core.attr.exclude_hv : 0;
19161fc632ceSJiri Olsa 	int eH = evsel ? evsel->core.attr.exclude_host : 0;
19171fc632ceSJiri Olsa 	int eG = evsel ? evsel->core.attr.exclude_guest : 0;
19181fc632ceSJiri Olsa 	int eI = evsel ? evsel->core.attr.exclude_idle : 0;
19191fc632ceSJiri Olsa 	int precise = evsel ? evsel->core.attr.precise_ip : 0;
19207f94af7aSJiri Olsa 	int precise_max = 0;
19213c176311SJiri Olsa 	int sample_read = 0;
19221fc632ceSJiri Olsa 	int pinned = evsel ? evsel->core.attr.pinned : 0;
19230997a266SAndi Kleen 	int exclusive = evsel ? evsel->core.attr.exclusive : 0;
1924f5b1135bSJiri Olsa 
1925f5b1135bSJiri Olsa 	int exclude = eu | ek | eh;
1926f5b1135bSJiri Olsa 	int exclude_GH = evsel ? evsel->exclude_GH : 0;
19275a5dfe4bSAndi Kleen 	int weak = 0;
192801bd8efcSSong Liu 	int bpf_counter = 0;
1929f5b1135bSJiri Olsa 
1930f5b1135bSJiri Olsa 	memset(mod, 0, sizeof(*mod));
1931ceb53fbfSIngo Molnar 
193261c45981SPaul Mackerras 	while (*str) {
1933ab608344SPeter Zijlstra 		if (*str == 'u') {
1934ab608344SPeter Zijlstra 			if (!exclude)
1935ab608344SPeter Zijlstra 				exclude = eu = ek = eh = 1;
1936943b69acSJin Yao 			if (!exclude_GH && !perf_guest)
1937943b69acSJin Yao 				eG = 1;
193861c45981SPaul Mackerras 			eu = 0;
1939ab608344SPeter Zijlstra 		} else if (*str == 'k') {
1940ab608344SPeter Zijlstra 			if (!exclude)
1941ab608344SPeter Zijlstra 				exclude = eu = ek = eh = 1;
194261c45981SPaul Mackerras 			ek = 0;
1943ab608344SPeter Zijlstra 		} else if (*str == 'h') {
1944ab608344SPeter Zijlstra 			if (!exclude)
1945ab608344SPeter Zijlstra 				exclude = eu = ek = eh = 1;
194661c45981SPaul Mackerras 			eh = 0;
194799320cc8SJoerg Roedel 		} else if (*str == 'G') {
194899320cc8SJoerg Roedel 			if (!exclude_GH)
194999320cc8SJoerg Roedel 				exclude_GH = eG = eH = 1;
195099320cc8SJoerg Roedel 			eG = 0;
195199320cc8SJoerg Roedel 		} else if (*str == 'H') {
195299320cc8SJoerg Roedel 			if (!exclude_GH)
195399320cc8SJoerg Roedel 				exclude_GH = eG = eH = 1;
195499320cc8SJoerg Roedel 			eH = 0;
1955a1e12da4SJiri Olsa 		} else if (*str == 'I') {
1956a1e12da4SJiri Olsa 			eI = 1;
1957ab608344SPeter Zijlstra 		} else if (*str == 'p') {
1958ab608344SPeter Zijlstra 			precise++;
19591342798cSDavid Ahern 			/* use of precise requires exclude_guest */
19601342798cSDavid Ahern 			if (!exclude_GH)
19611342798cSDavid Ahern 				eG = 1;
19627f94af7aSJiri Olsa 		} else if (*str == 'P') {
19637f94af7aSJiri Olsa 			precise_max = 1;
19643c176311SJiri Olsa 		} else if (*str == 'S') {
19653c176311SJiri Olsa 			sample_read = 1;
1966e9a7c414SMichael Ellerman 		} else if (*str == 'D') {
1967e9a7c414SMichael Ellerman 			pinned = 1;
19680997a266SAndi Kleen 		} else if (*str == 'e') {
19690997a266SAndi Kleen 			exclusive = 1;
19705a5dfe4bSAndi Kleen 		} else if (*str == 'W') {
19715a5dfe4bSAndi Kleen 			weak = 1;
197201bd8efcSSong Liu 		} else if (*str == 'b') {
197301bd8efcSSong Liu 			bpf_counter = 1;
1974ab608344SPeter Zijlstra 		} else
197561c45981SPaul Mackerras 			break;
1976ab608344SPeter Zijlstra 
197761c45981SPaul Mackerras 		++str;
197861c45981SPaul Mackerras 	}
197974d5b588SJaswinder Singh Rajput 
198086470930SIngo Molnar 	/*
198189812fc8SJiri Olsa 	 * precise ip:
198289812fc8SJiri Olsa 	 *
198389812fc8SJiri Olsa 	 *  0 - SAMPLE_IP can have arbitrary skid
198489812fc8SJiri Olsa 	 *  1 - SAMPLE_IP must have constant skid
198589812fc8SJiri Olsa 	 *  2 - SAMPLE_IP requested to have 0 skid
198689812fc8SJiri Olsa 	 *  3 - SAMPLE_IP must have 0 skid
198789812fc8SJiri Olsa 	 *
198889812fc8SJiri Olsa 	 *  See also PERF_RECORD_MISC_EXACT_IP
198986470930SIngo Molnar 	 */
199089812fc8SJiri Olsa 	if (precise > 3)
199189812fc8SJiri Olsa 		return -EINVAL;
199286470930SIngo Molnar 
1993f5b1135bSJiri Olsa 	mod->eu = eu;
1994f5b1135bSJiri Olsa 	mod->ek = ek;
1995f5b1135bSJiri Olsa 	mod->eh = eh;
1996f5b1135bSJiri Olsa 	mod->eH = eH;
1997f5b1135bSJiri Olsa 	mod->eG = eG;
1998a1e12da4SJiri Olsa 	mod->eI = eI;
1999f5b1135bSJiri Olsa 	mod->precise = precise;
20007f94af7aSJiri Olsa 	mod->precise_max = precise_max;
2001f5b1135bSJiri Olsa 	mod->exclude_GH = exclude_GH;
20023c176311SJiri Olsa 	mod->sample_read = sample_read;
2003e9a7c414SMichael Ellerman 	mod->pinned = pinned;
20045a5dfe4bSAndi Kleen 	mod->weak = weak;
200501bd8efcSSong Liu 	mod->bpf_counter = bpf_counter;
20060997a266SAndi Kleen 	mod->exclusive = exclusive;
2007e9a7c414SMichael Ellerman 
2008f5b1135bSJiri Olsa 	return 0;
2009f5b1135bSJiri Olsa }
2010f5b1135bSJiri Olsa 
2011534123f4SJiri Olsa /*
2012534123f4SJiri Olsa  * Basic modifier sanity check to validate it contains only one
2013534123f4SJiri Olsa  * instance of any modifier (apart from 'p') present.
2014534123f4SJiri Olsa  */
2015534123f4SJiri Olsa static int check_modifier(char *str)
2016534123f4SJiri Olsa {
2017534123f4SJiri Olsa 	char *p = str;
2018534123f4SJiri Olsa 
2019534123f4SJiri Olsa 	/* The sizeof includes 0 byte as well. */
202001bd8efcSSong Liu 	if (strlen(str) > (sizeof("ukhGHpppPSDIWeb") - 1))
2021534123f4SJiri Olsa 		return -1;
2022534123f4SJiri Olsa 
2023534123f4SJiri Olsa 	while (*p) {
2024534123f4SJiri Olsa 		if (*p != 'p' && strchr(p + 1, *p))
2025534123f4SJiri Olsa 			return -1;
2026534123f4SJiri Olsa 		p++;
2027534123f4SJiri Olsa 	}
2028534123f4SJiri Olsa 
2029534123f4SJiri Olsa 	return 0;
2030534123f4SJiri Olsa }
2031534123f4SJiri Olsa 
2032f5b1135bSJiri Olsa int parse_events__modifier_event(struct list_head *list, char *str, bool add)
2033f5b1135bSJiri Olsa {
203432dcd021SJiri Olsa 	struct evsel *evsel;
2035f5b1135bSJiri Olsa 	struct event_modifier mod;
2036f5b1135bSJiri Olsa 
2037f5b1135bSJiri Olsa 	if (str == NULL)
2038f5b1135bSJiri Olsa 		return 0;
2039f5b1135bSJiri Olsa 
2040534123f4SJiri Olsa 	if (check_modifier(str))
2041534123f4SJiri Olsa 		return -EINVAL;
2042534123f4SJiri Olsa 
2043f5b1135bSJiri Olsa 	if (!add && get_event_modifier(&mod, str, NULL))
2044f5b1135bSJiri Olsa 		return -EINVAL;
2045f5b1135bSJiri Olsa 
2046e5cadb93SArnaldo Carvalho de Melo 	__evlist__for_each_entry(list, evsel) {
2047f5b1135bSJiri Olsa 		if (add && get_event_modifier(&mod, str, evsel))
2048f5b1135bSJiri Olsa 			return -EINVAL;
2049f5b1135bSJiri Olsa 
20501fc632ceSJiri Olsa 		evsel->core.attr.exclude_user   = mod.eu;
20511fc632ceSJiri Olsa 		evsel->core.attr.exclude_kernel = mod.ek;
20521fc632ceSJiri Olsa 		evsel->core.attr.exclude_hv     = mod.eh;
20531fc632ceSJiri Olsa 		evsel->core.attr.precise_ip     = mod.precise;
20541fc632ceSJiri Olsa 		evsel->core.attr.exclude_host   = mod.eH;
20551fc632ceSJiri Olsa 		evsel->core.attr.exclude_guest  = mod.eG;
20561fc632ceSJiri Olsa 		evsel->core.attr.exclude_idle   = mod.eI;
2057f5b1135bSJiri Olsa 		evsel->exclude_GH          = mod.exclude_GH;
20583c176311SJiri Olsa 		evsel->sample_read         = mod.sample_read;
20597f94af7aSJiri Olsa 		evsel->precise_max         = mod.precise_max;
20605a5dfe4bSAndi Kleen 		evsel->weak_group	   = mod.weak;
206101bd8efcSSong Liu 		evsel->bpf_counter	   = mod.bpf_counter;
2062e9a7c414SMichael Ellerman 
20630997a266SAndi Kleen 		if (evsel__is_group_leader(evsel)) {
20641fc632ceSJiri Olsa 			evsel->core.attr.pinned = mod.pinned;
20650997a266SAndi Kleen 			evsel->core.attr.exclusive = mod.exclusive;
20660997a266SAndi Kleen 		}
2067ceb53fbfSIngo Molnar 	}
206886470930SIngo Molnar 
206989812fc8SJiri Olsa 	return 0;
207086470930SIngo Molnar }
207186470930SIngo Molnar 
20728e8bbfb3SIan Rogers int parse_events_name(struct list_head *list, const char *name)
2073ac2ba9f3SRobert Richter {
207432dcd021SJiri Olsa 	struct evsel *evsel;
2075ac2ba9f3SRobert Richter 
2076e5cadb93SArnaldo Carvalho de Melo 	__evlist__for_each_entry(list, evsel) {
2077ac2ba9f3SRobert Richter 		if (!evsel->name)
2078ac2ba9f3SRobert Richter 			evsel->name = strdup(name);
2079ac2ba9f3SRobert Richter 	}
2080ac2ba9f3SRobert Richter 
2081ac2ba9f3SRobert Richter 	return 0;
2082ac2ba9f3SRobert Richter }
2083ac2ba9f3SRobert Richter 
2084dcb4e102SKan Liang static int
2085dcb4e102SKan Liang comp_pmu(const void *p1, const void *p2)
2086dcb4e102SKan Liang {
2087dcb4e102SKan Liang 	struct perf_pmu_event_symbol *pmu1 = (struct perf_pmu_event_symbol *) p1;
2088dcb4e102SKan Liang 	struct perf_pmu_event_symbol *pmu2 = (struct perf_pmu_event_symbol *) p2;
2089dcb4e102SKan Liang 
2090e312bcf1SAndi Kleen 	return strcasecmp(pmu1->symbol, pmu2->symbol);
2091dcb4e102SKan Liang }
2092dcb4e102SKan Liang 
2093dcb4e102SKan Liang static void perf_pmu__parse_cleanup(void)
2094dcb4e102SKan Liang {
2095dcb4e102SKan Liang 	if (perf_pmu_events_list_num > 0) {
2096dcb4e102SKan Liang 		struct perf_pmu_event_symbol *p;
2097dcb4e102SKan Liang 		int i;
2098dcb4e102SKan Liang 
2099dcb4e102SKan Liang 		for (i = 0; i < perf_pmu_events_list_num; i++) {
2100dcb4e102SKan Liang 			p = perf_pmu_events_list + i;
2101360e071bSTaeung Song 			zfree(&p->symbol);
2102dcb4e102SKan Liang 		}
2103506fde11STaeung Song 		zfree(&perf_pmu_events_list);
2104dcb4e102SKan Liang 		perf_pmu_events_list_num = 0;
2105dcb4e102SKan Liang 	}
2106dcb4e102SKan Liang }
2107dcb4e102SKan Liang 
2108dcb4e102SKan Liang #define SET_SYMBOL(str, stype)		\
2109dcb4e102SKan Liang do {					\
2110dcb4e102SKan Liang 	p->symbol = str;		\
2111dcb4e102SKan Liang 	if (!p->symbol)			\
2112dcb4e102SKan Liang 		goto err;		\
2113dcb4e102SKan Liang 	p->type = stype;		\
2114dcb4e102SKan Liang } while (0)
2115dcb4e102SKan Liang 
2116dcb4e102SKan Liang /*
2117dcb4e102SKan Liang  * Read the pmu events list from sysfs
2118dcb4e102SKan Liang  * Save it into perf_pmu_events_list
2119dcb4e102SKan Liang  */
2120dcb4e102SKan Liang static void perf_pmu__parse_init(void)
2121dcb4e102SKan Liang {
2122dcb4e102SKan Liang 
2123dcb4e102SKan Liang 	struct perf_pmu *pmu = NULL;
2124dcb4e102SKan Liang 	struct perf_pmu_alias *alias;
2125dcb4e102SKan Liang 	int len = 0;
2126dcb4e102SKan Liang 
2127231bb2aaSAndi Kleen 	pmu = NULL;
2128231bb2aaSAndi Kleen 	while ((pmu = perf_pmu__scan(pmu)) != NULL) {
2129dcb4e102SKan Liang 		list_for_each_entry(alias, &pmu->aliases, list) {
2130864bc8c9SJohn Garry 			char *tmp = strchr(alias->name, '-');
2131864bc8c9SJohn Garry 
2132864bc8c9SJohn Garry 			if (tmp) {
2133864bc8c9SJohn Garry 				char *tmp2 = NULL;
2134864bc8c9SJohn Garry 
2135864bc8c9SJohn Garry 				tmp2 = strchr(tmp + 1, '-');
2136dcb4e102SKan Liang 				len++;
2137864bc8c9SJohn Garry 				if (tmp2)
2138864bc8c9SJohn Garry 					len++;
2139864bc8c9SJohn Garry 			}
2140864bc8c9SJohn Garry 
2141dcb4e102SKan Liang 			len++;
2142dcb4e102SKan Liang 		}
2143231bb2aaSAndi Kleen 	}
2144231bb2aaSAndi Kleen 
2145231bb2aaSAndi Kleen 	if (len == 0) {
2146231bb2aaSAndi Kleen 		perf_pmu_events_list_num = -1;
2147231bb2aaSAndi Kleen 		return;
2148231bb2aaSAndi Kleen 	}
2149dcb4e102SKan Liang 	perf_pmu_events_list = malloc(sizeof(struct perf_pmu_event_symbol) * len);
2150dcb4e102SKan Liang 	if (!perf_pmu_events_list)
2151dcb4e102SKan Liang 		return;
2152dcb4e102SKan Liang 	perf_pmu_events_list_num = len;
2153dcb4e102SKan Liang 
2154dcb4e102SKan Liang 	len = 0;
2155231bb2aaSAndi Kleen 	pmu = NULL;
2156231bb2aaSAndi Kleen 	while ((pmu = perf_pmu__scan(pmu)) != NULL) {
2157dcb4e102SKan Liang 		list_for_each_entry(alias, &pmu->aliases, list) {
2158dcb4e102SKan Liang 			struct perf_pmu_event_symbol *p = perf_pmu_events_list + len;
2159dcb4e102SKan Liang 			char *tmp = strchr(alias->name, '-');
2160864bc8c9SJohn Garry 			char *tmp2 = NULL;
2161dcb4e102SKan Liang 
2162864bc8c9SJohn Garry 			if (tmp)
2163864bc8c9SJohn Garry 				tmp2 = strchr(tmp + 1, '-');
2164864bc8c9SJohn Garry 			if (tmp2) {
2165864bc8c9SJohn Garry 				SET_SYMBOL(strndup(alias->name, tmp - alias->name),
2166864bc8c9SJohn Garry 						PMU_EVENT_SYMBOL_PREFIX);
2167864bc8c9SJohn Garry 				p++;
2168864bc8c9SJohn Garry 				tmp++;
2169864bc8c9SJohn Garry 				SET_SYMBOL(strndup(tmp, tmp2 - tmp), PMU_EVENT_SYMBOL_SUFFIX);
2170864bc8c9SJohn Garry 				p++;
2171864bc8c9SJohn Garry 				SET_SYMBOL(strdup(++tmp2), PMU_EVENT_SYMBOL_SUFFIX2);
2172864bc8c9SJohn Garry 				len += 3;
2173864bc8c9SJohn Garry 			} else if (tmp) {
2174dcb4e102SKan Liang 				SET_SYMBOL(strndup(alias->name, tmp - alias->name),
2175dcb4e102SKan Liang 						PMU_EVENT_SYMBOL_PREFIX);
2176dcb4e102SKan Liang 				p++;
2177dcb4e102SKan Liang 				SET_SYMBOL(strdup(++tmp), PMU_EVENT_SYMBOL_SUFFIX);
2178dcb4e102SKan Liang 				len += 2;
2179dcb4e102SKan Liang 			} else {
2180dcb4e102SKan Liang 				SET_SYMBOL(strdup(alias->name), PMU_EVENT_SYMBOL);
2181dcb4e102SKan Liang 				len++;
2182dcb4e102SKan Liang 			}
2183dcb4e102SKan Liang 		}
2184231bb2aaSAndi Kleen 	}
2185dcb4e102SKan Liang 	qsort(perf_pmu_events_list, len,
2186dcb4e102SKan Liang 		sizeof(struct perf_pmu_event_symbol), comp_pmu);
2187dcb4e102SKan Liang 
2188dcb4e102SKan Liang 	return;
2189dcb4e102SKan Liang err:
2190dcb4e102SKan Liang 	perf_pmu__parse_cleanup();
2191dcb4e102SKan Liang }
2192dcb4e102SKan Liang 
21934929e95aSJiri Olsa /*
21944929e95aSJiri Olsa  * This function injects special term in
21954929e95aSJiri Olsa  * perf_pmu_events_list so the test code
21964929e95aSJiri Olsa  * can check on this functionality.
21974929e95aSJiri Olsa  */
21984929e95aSJiri Olsa int perf_pmu__test_parse_init(void)
21994929e95aSJiri Olsa {
2200b4a7276cSJohn Garry 	struct perf_pmu_event_symbol *list, *tmp, symbols[] = {
2201b4a7276cSJohn Garry 		{(char *)"read", PMU_EVENT_SYMBOL},
2202b4a7276cSJohn Garry 		{(char *)"event", PMU_EVENT_SYMBOL_PREFIX},
2203b4a7276cSJohn Garry 		{(char *)"two", PMU_EVENT_SYMBOL_SUFFIX},
2204b4a7276cSJohn Garry 		{(char *)"hyphen", PMU_EVENT_SYMBOL_SUFFIX},
2205b4a7276cSJohn Garry 		{(char *)"hyph", PMU_EVENT_SYMBOL_SUFFIX2},
2206b4a7276cSJohn Garry 	};
2207b4a7276cSJohn Garry 	unsigned long i, j;
22084929e95aSJiri Olsa 
2209b4a7276cSJohn Garry 	tmp = list = malloc(sizeof(*list) * ARRAY_SIZE(symbols));
22104929e95aSJiri Olsa 	if (!list)
22114929e95aSJiri Olsa 		return -ENOMEM;
22124929e95aSJiri Olsa 
2213b4a7276cSJohn Garry 	for (i = 0; i < ARRAY_SIZE(symbols); i++, tmp++) {
2214b4a7276cSJohn Garry 		tmp->type = symbols[i].type;
2215b4a7276cSJohn Garry 		tmp->symbol = strdup(symbols[i].symbol);
2216a7a72631SWeiguo Li 		if (!tmp->symbol)
2217b4a7276cSJohn Garry 			goto err_free;
22184929e95aSJiri Olsa 	}
22194929e95aSJiri Olsa 
22204929e95aSJiri Olsa 	perf_pmu_events_list = list;
2221b4a7276cSJohn Garry 	perf_pmu_events_list_num = ARRAY_SIZE(symbols);
2222b4a7276cSJohn Garry 
2223b4a7276cSJohn Garry 	qsort(perf_pmu_events_list, ARRAY_SIZE(symbols),
2224b4a7276cSJohn Garry 	      sizeof(struct perf_pmu_event_symbol), comp_pmu);
22254929e95aSJiri Olsa 	return 0;
2226b4a7276cSJohn Garry 
2227b4a7276cSJohn Garry err_free:
2228b4a7276cSJohn Garry 	for (j = 0, tmp = list; j < i; j++, tmp++)
2229b4a7276cSJohn Garry 		free(tmp->symbol);
2230b4a7276cSJohn Garry 	free(list);
2231b4a7276cSJohn Garry 	return -ENOMEM;
22324929e95aSJiri Olsa }
22334929e95aSJiri Olsa 
2234dcb4e102SKan Liang enum perf_pmu_event_symbol_type
2235dcb4e102SKan Liang perf_pmu__parse_check(const char *name)
2236dcb4e102SKan Liang {
2237dcb4e102SKan Liang 	struct perf_pmu_event_symbol p, *r;
2238dcb4e102SKan Liang 
2239dcb4e102SKan Liang 	/* scan kernel pmu events from sysfs if needed */
2240dcb4e102SKan Liang 	if (perf_pmu_events_list_num == 0)
2241dcb4e102SKan Liang 		perf_pmu__parse_init();
2242dcb4e102SKan Liang 	/*
2243dcb4e102SKan Liang 	 * name "cpu" could be prefix of cpu-cycles or cpu// events.
2244dcb4e102SKan Liang 	 * cpu-cycles has been handled by hardcode.
2245dcb4e102SKan Liang 	 * So it must be cpu// events, not kernel pmu event.
2246dcb4e102SKan Liang 	 */
2247dcb4e102SKan Liang 	if ((perf_pmu_events_list_num <= 0) || !strcmp(name, "cpu"))
2248dcb4e102SKan Liang 		return PMU_EVENT_SYMBOL_ERR;
2249dcb4e102SKan Liang 
2250dcb4e102SKan Liang 	p.symbol = strdup(name);
2251dcb4e102SKan Liang 	r = bsearch(&p, perf_pmu_events_list,
2252dcb4e102SKan Liang 			(size_t) perf_pmu_events_list_num,
2253dcb4e102SKan Liang 			sizeof(struct perf_pmu_event_symbol), comp_pmu);
2254360e071bSTaeung Song 	zfree(&p.symbol);
2255dcb4e102SKan Liang 	return r ? r->type : PMU_EVENT_SYMBOL_ERR;
2256dcb4e102SKan Liang }
2257dcb4e102SKan Liang 
22581244a327SJiri Olsa static int parse_events__scanner(const char *str,
22591244a327SJiri Olsa 				 struct parse_events_state *parse_state)
2260ac20de6fSZheng Yan {
2261ac20de6fSZheng Yan 	YY_BUFFER_STATE buffer;
2262ac20de6fSZheng Yan 	void *scanner;
2263ac20de6fSZheng Yan 	int ret;
2264ac20de6fSZheng Yan 
22651244a327SJiri Olsa 	ret = parse_events_lex_init_extra(parse_state, &scanner);
2266ac20de6fSZheng Yan 	if (ret)
2267ac20de6fSZheng Yan 		return ret;
2268ac20de6fSZheng Yan 
2269ac20de6fSZheng Yan 	buffer = parse_events__scan_string(str, scanner);
2270ac20de6fSZheng Yan 
2271ac20de6fSZheng Yan #ifdef PARSER_DEBUG
2272ac20de6fSZheng Yan 	parse_events_debug = 1;
2273ae762641SJiri Olsa 	parse_events_set_debug(1, scanner);
2274ac20de6fSZheng Yan #endif
22755d9cdc11SArnaldo Carvalho de Melo 	ret = parse_events_parse(parse_state, scanner);
2276ac20de6fSZheng Yan 
2277ac20de6fSZheng Yan 	parse_events__flush_buffer(buffer, scanner);
2278ac20de6fSZheng Yan 	parse_events__delete_buffer(buffer, scanner);
2279ac20de6fSZheng Yan 	parse_events_lex_destroy(scanner);
2280ac20de6fSZheng Yan 	return ret;
2281ac20de6fSZheng Yan }
2282ac20de6fSZheng Yan 
228390e2b22dSJiri Olsa /*
228490e2b22dSJiri Olsa  * parse event config string, return a list of event terms.
228590e2b22dSJiri Olsa  */
228690e2b22dSJiri Olsa int parse_events_terms(struct list_head *terms, const char *str)
228790e2b22dSJiri Olsa {
22885d9cdc11SArnaldo Carvalho de Melo 	struct parse_events_state parse_state = {
228990e2b22dSJiri Olsa 		.terms  = NULL,
22901244a327SJiri Olsa 		.stoken = PE_START_TERMS,
229190e2b22dSJiri Olsa 	};
229290e2b22dSJiri Olsa 	int ret;
229390e2b22dSJiri Olsa 
22941244a327SJiri Olsa 	ret = parse_events__scanner(str, &parse_state);
22954929e95aSJiri Olsa 	perf_pmu__parse_cleanup();
22964929e95aSJiri Olsa 
229790e2b22dSJiri Olsa 	if (!ret) {
22985d9cdc11SArnaldo Carvalho de Melo 		list_splice(parse_state.terms, terms);
22995d9cdc11SArnaldo Carvalho de Melo 		zfree(&parse_state.terms);
230090e2b22dSJiri Olsa 		return 0;
230190e2b22dSJiri Olsa 	}
230290e2b22dSJiri Olsa 
23035d9cdc11SArnaldo Carvalho de Melo 	parse_events_terms__delete(parse_state.terms);
230490e2b22dSJiri Olsa 	return ret;
230590e2b22dSJiri Olsa }
230690e2b22dSJiri Olsa 
23075e4edd1fSJin Yao static int parse_events__with_hybrid_pmu(struct parse_events_state *parse_state,
23085e4edd1fSJin Yao 					 const char *str, char *pmu_name,
23095e4edd1fSJin Yao 					 struct list_head *list)
23105e4edd1fSJin Yao {
23115e4edd1fSJin Yao 	struct parse_events_state ps = {
23125e4edd1fSJin Yao 		.list            = LIST_HEAD_INIT(ps.list),
23135e4edd1fSJin Yao 		.stoken          = PE_START_EVENTS,
23145e4edd1fSJin Yao 		.hybrid_pmu_name = pmu_name,
23155e4edd1fSJin Yao 		.idx             = parse_state->idx,
23165e4edd1fSJin Yao 	};
23175e4edd1fSJin Yao 	int ret;
23185e4edd1fSJin Yao 
23195e4edd1fSJin Yao 	ret = parse_events__scanner(str, &ps);
23205e4edd1fSJin Yao 	perf_pmu__parse_cleanup();
23215e4edd1fSJin Yao 
23225e4edd1fSJin Yao 	if (!ret) {
23235e4edd1fSJin Yao 		if (!list_empty(&ps.list)) {
23245e4edd1fSJin Yao 			list_splice(&ps.list, list);
23255e4edd1fSJin Yao 			parse_state->idx = ps.idx;
23265e4edd1fSJin Yao 			return 0;
23275e4edd1fSJin Yao 		} else
23285e4edd1fSJin Yao 			return -1;
23295e4edd1fSJin Yao 	}
23305e4edd1fSJin Yao 
23315e4edd1fSJin Yao 	return ret;
23325e4edd1fSJin Yao }
23335e4edd1fSJin Yao 
23343bf91aa5SArnaldo Carvalho de Melo int __parse_events(struct evlist *evlist, const char *str,
23353bf91aa5SArnaldo Carvalho de Melo 		   struct parse_events_error *err, struct perf_pmu *fake_pmu)
233686470930SIngo Molnar {
23375d9cdc11SArnaldo Carvalho de Melo 	struct parse_events_state parse_state = {
23385d9cdc11SArnaldo Carvalho de Melo 		.list	  = LIST_HEAD_INIT(parse_state.list),
23396484d2f9SJiri Olsa 		.idx	  = evlist->core.nr_entries,
2340b39b8393SJiri Olsa 		.error	  = err,
23417630b3e2SWang Nan 		.evlist	  = evlist,
23421244a327SJiri Olsa 		.stoken	  = PE_START_EVENTS,
23433bf91aa5SArnaldo Carvalho de Melo 		.fake_pmu = fake_pmu,
234446010ab2SJiri Olsa 	};
234546010ab2SJiri Olsa 	int ret;
234686470930SIngo Molnar 
23471244a327SJiri Olsa 	ret = parse_events__scanner(str, &parse_state);
2348dcb4e102SKan Liang 	perf_pmu__parse_cleanup();
234915bfd2ccSWang Nan 
23508e8714c3SIan Rogers 	if (!ret && list_empty(&parse_state.list)) {
2351c23080a6SArnaldo Carvalho de Melo 		WARN_ONCE(true, "WARNING: event parser found nothing\n");
2352854f7363SWang Nan 		return -1;
2353854f7363SWang Nan 	}
2354854f7363SWang Nan 
23558e8714c3SIan Rogers 	/*
23568e8714c3SIan Rogers 	 * Add list to the evlist even with errors to allow callers to clean up.
23578e8714c3SIan Rogers 	 */
2358e414fd1aSArnaldo Carvalho de Melo 	evlist__splice_list_tail(evlist, &parse_state.list);
23598e8714c3SIan Rogers 
23608e8714c3SIan Rogers 	if (!ret) {
23618e8714c3SIan Rogers 		struct evsel *last;
23628e8714c3SIan Rogers 
23633a683120SJiri Olsa 		evlist->core.nr_groups += parse_state.nr_groups;
2364515dbe48SJiri Olsa 		last = evlist__last(evlist);
236515bfd2ccSWang Nan 		last->cmdline_group_boundary = true;
236615bfd2ccSWang Nan 
236786470930SIngo Molnar 		return 0;
236886470930SIngo Molnar 	}
236986470930SIngo Molnar 
23705d7be90eSJiri Olsa 	/*
23715d7be90eSJiri Olsa 	 * There are 2 users - builtin-record and builtin-test objects.
2372c12995a5SJiri Olsa 	 * Both call evlist__delete in case of error, so we dont
23735d7be90eSJiri Olsa 	 * need to bother.
23745d7be90eSJiri Olsa 	 */
237589812fc8SJiri Olsa 	return ret;
237689812fc8SJiri Olsa }
237789812fc8SJiri Olsa 
237807eafd4eSIan Rogers void parse_events_error__init(struct parse_events_error *err)
237907eafd4eSIan Rogers {
238007eafd4eSIan Rogers 	bzero(err, sizeof(*err));
238107eafd4eSIan Rogers }
238207eafd4eSIan Rogers 
238307eafd4eSIan Rogers void parse_events_error__exit(struct parse_events_error *err)
238407eafd4eSIan Rogers {
238507eafd4eSIan Rogers 	zfree(&err->str);
238607eafd4eSIan Rogers 	zfree(&err->help);
238707eafd4eSIan Rogers 	zfree(&err->first_str);
238807eafd4eSIan Rogers 	zfree(&err->first_help);
238907eafd4eSIan Rogers }
239007eafd4eSIan Rogers 
23916c191289SIan Rogers void parse_events_error__handle(struct parse_events_error *err, int idx,
23926c191289SIan Rogers 				char *str, char *help)
23936c191289SIan Rogers {
23946c191289SIan Rogers 	if (WARN(!str, "WARNING: failed to provide error string\n")) {
23956c191289SIan Rogers 		free(help);
23966c191289SIan Rogers 		return;
23976c191289SIan Rogers 	}
23986c191289SIan Rogers 	switch (err->num_errors) {
23996c191289SIan Rogers 	case 0:
24006c191289SIan Rogers 		err->idx = idx;
24016c191289SIan Rogers 		err->str = str;
24026c191289SIan Rogers 		err->help = help;
24036c191289SIan Rogers 		break;
24046c191289SIan Rogers 	case 1:
24056c191289SIan Rogers 		err->first_idx = err->idx;
24066c191289SIan Rogers 		err->idx = idx;
24076c191289SIan Rogers 		err->first_str = err->str;
24086c191289SIan Rogers 		err->str = str;
24096c191289SIan Rogers 		err->first_help = err->help;
24106c191289SIan Rogers 		err->help = help;
24116c191289SIan Rogers 		break;
24126c191289SIan Rogers 	default:
24136c191289SIan Rogers 		pr_debug("Multiple errors dropping message: %s (%s)\n",
24146c191289SIan Rogers 			err->str, err->help);
24156c191289SIan Rogers 		free(err->str);
24166c191289SIan Rogers 		err->str = str;
24176c191289SIan Rogers 		free(err->help);
24186c191289SIan Rogers 		err->help = help;
24196c191289SIan Rogers 		break;
24206c191289SIan Rogers 	}
24216c191289SIan Rogers 	err->num_errors++;
24226c191289SIan Rogers }
24236c191289SIan Rogers 
2424b39b8393SJiri Olsa #define MAX_WIDTH 1000
2425b39b8393SJiri Olsa static int get_term_width(void)
2426b39b8393SJiri Olsa {
2427b39b8393SJiri Olsa 	struct winsize ws;
2428b39b8393SJiri Olsa 
2429b39b8393SJiri Olsa 	get_term_dimensions(&ws);
2430b39b8393SJiri Olsa 	return ws.ws_col > MAX_WIDTH ? MAX_WIDTH : ws.ws_col;
2431b39b8393SJiri Olsa }
2432b39b8393SJiri Olsa 
24336c191289SIan Rogers static void __parse_events_error__print(int err_idx, const char *err_str,
2434a910e466SIan Rogers 					const char *err_help, const char *event)
2435b39b8393SJiri Olsa {
2436b39b8393SJiri Olsa 	const char *str = "invalid or unsupported event: ";
2437b39b8393SJiri Olsa 	char _buf[MAX_WIDTH];
2438b39b8393SJiri Olsa 	char *buf = (char *) event;
2439b39b8393SJiri Olsa 	int idx = 0;
2440a910e466SIan Rogers 	if (err_str) {
2441b39b8393SJiri Olsa 		/* -2 for extra '' in the final fprintf */
2442b39b8393SJiri Olsa 		int width       = get_term_width() - 2;
2443b39b8393SJiri Olsa 		int len_event   = strlen(event);
2444b39b8393SJiri Olsa 		int len_str, max_len, cut = 0;
2445b39b8393SJiri Olsa 
2446b39b8393SJiri Olsa 		/*
2447b39b8393SJiri Olsa 		 * Maximum error index indent, we will cut
2448b39b8393SJiri Olsa 		 * the event string if it's bigger.
2449b39b8393SJiri Olsa 		 */
2450141b2d31SAdrian Hunter 		int max_err_idx = 13;
2451b39b8393SJiri Olsa 
2452b39b8393SJiri Olsa 		/*
2453b39b8393SJiri Olsa 		 * Let's be specific with the message when
2454b39b8393SJiri Olsa 		 * we have the precise error.
2455b39b8393SJiri Olsa 		 */
2456b39b8393SJiri Olsa 		str     = "event syntax error: ";
2457b39b8393SJiri Olsa 		len_str = strlen(str);
2458b39b8393SJiri Olsa 		max_len = width - len_str;
2459b39b8393SJiri Olsa 
2460b39b8393SJiri Olsa 		buf = _buf;
2461b39b8393SJiri Olsa 
2462bd1a0be5SAdam Buchbinder 		/* We're cutting from the beginning. */
2463a910e466SIan Rogers 		if (err_idx > max_err_idx)
2464a910e466SIan Rogers 			cut = err_idx - max_err_idx;
2465b39b8393SJiri Olsa 
2466b39b8393SJiri Olsa 		strncpy(buf, event + cut, max_len);
2467b39b8393SJiri Olsa 
2468b39b8393SJiri Olsa 		/* Mark cut parts with '..' on both sides. */
2469b39b8393SJiri Olsa 		if (cut)
2470b39b8393SJiri Olsa 			buf[0] = buf[1] = '.';
2471b39b8393SJiri Olsa 
2472b39b8393SJiri Olsa 		if ((len_event - cut) > max_len) {
2473b39b8393SJiri Olsa 			buf[max_len - 1] = buf[max_len - 2] = '.';
2474b39b8393SJiri Olsa 			buf[max_len] = 0;
2475b39b8393SJiri Olsa 		}
2476b39b8393SJiri Olsa 
2477a910e466SIan Rogers 		idx = len_str + err_idx - cut;
2478b39b8393SJiri Olsa 	}
2479b39b8393SJiri Olsa 
2480b39b8393SJiri Olsa 	fprintf(stderr, "%s'%s'\n", str, buf);
2481b39b8393SJiri Olsa 	if (idx) {
2482a910e466SIan Rogers 		fprintf(stderr, "%*s\\___ %s\n", idx + 1, "", err_str);
2483a910e466SIan Rogers 		if (err_help)
2484a910e466SIan Rogers 			fprintf(stderr, "\n%s\n", err_help);
2485a910e466SIan Rogers 	}
2486a910e466SIan Rogers }
2487a910e466SIan Rogers 
24886c191289SIan Rogers void parse_events_error__print(struct parse_events_error *err,
2489a910e466SIan Rogers 			       const char *event)
2490a910e466SIan Rogers {
2491a910e466SIan Rogers 	if (!err->num_errors)
2492a910e466SIan Rogers 		return;
2493a910e466SIan Rogers 
24946c191289SIan Rogers 	__parse_events_error__print(err->idx, err->str, err->help, event);
2495a910e466SIan Rogers 
2496a910e466SIan Rogers 	if (err->num_errors > 1) {
2497a910e466SIan Rogers 		fputs("\nInitial error:\n", stderr);
24986c191289SIan Rogers 		__parse_events_error__print(err->first_idx, err->first_str,
2499a910e466SIan Rogers 					err->first_help, event);
2500b39b8393SJiri Olsa 	}
2501b39b8393SJiri Olsa }
2502b39b8393SJiri Olsa 
2503b39b8393SJiri Olsa #undef MAX_WIDTH
2504b39b8393SJiri Olsa 
2505f120f9d5SJiri Olsa int parse_events_option(const struct option *opt, const char *str,
25061d037ca1SIrina Tirdea 			int unset __maybe_unused)
2507f120f9d5SJiri Olsa {
250863503dbaSJiri Olsa 	struct evlist *evlist = *(struct evlist **)opt->value;
2509a910e466SIan Rogers 	struct parse_events_error err;
2510a910e466SIan Rogers 	int ret;
2511a910e466SIan Rogers 
251207eafd4eSIan Rogers 	parse_events_error__init(&err);
2513a910e466SIan Rogers 	ret = parse_events(evlist, str, &err);
25149175ce1fSAndi Kleen 
2515333b5665SAndi Kleen 	if (ret) {
25166c191289SIan Rogers 		parse_events_error__print(&err, str);
2517333b5665SAndi Kleen 		fprintf(stderr, "Run 'perf list' for a list of valid events\n");
2518333b5665SAndi Kleen 	}
251907eafd4eSIan Rogers 	parse_events_error__exit(&err);
2520b39b8393SJiri Olsa 
25219175ce1fSAndi Kleen 	return ret;
2522f120f9d5SJiri Olsa }
2523f120f9d5SJiri Olsa 
2524d0abbc3cSArnaldo Carvalho de Melo int parse_events_option_new_evlist(const struct option *opt, const char *str, int unset)
2525d0abbc3cSArnaldo Carvalho de Melo {
2526d0abbc3cSArnaldo Carvalho de Melo 	struct evlist **evlistp = opt->value;
2527d0abbc3cSArnaldo Carvalho de Melo 	int ret;
2528d0abbc3cSArnaldo Carvalho de Melo 
2529d0abbc3cSArnaldo Carvalho de Melo 	if (*evlistp == NULL) {
2530d0abbc3cSArnaldo Carvalho de Melo 		*evlistp = evlist__new();
2531d0abbc3cSArnaldo Carvalho de Melo 
2532d0abbc3cSArnaldo Carvalho de Melo 		if (*evlistp == NULL) {
2533d0abbc3cSArnaldo Carvalho de Melo 			fprintf(stderr, "Not enough memory to create evlist\n");
2534d0abbc3cSArnaldo Carvalho de Melo 			return -1;
2535d0abbc3cSArnaldo Carvalho de Melo 		}
2536d0abbc3cSArnaldo Carvalho de Melo 	}
2537d0abbc3cSArnaldo Carvalho de Melo 
2538d0abbc3cSArnaldo Carvalho de Melo 	ret = parse_events_option(opt, str, unset);
2539d0abbc3cSArnaldo Carvalho de Melo 	if (ret) {
2540d0abbc3cSArnaldo Carvalho de Melo 		evlist__delete(*evlistp);
2541d0abbc3cSArnaldo Carvalho de Melo 		*evlistp = NULL;
2542d0abbc3cSArnaldo Carvalho de Melo 	}
2543d0abbc3cSArnaldo Carvalho de Melo 
2544d0abbc3cSArnaldo Carvalho de Melo 	return ret;
2545d0abbc3cSArnaldo Carvalho de Melo }
2546d0abbc3cSArnaldo Carvalho de Melo 
25474ba1faa1SWang Nan static int
254863503dbaSJiri Olsa foreach_evsel_in_last_glob(struct evlist *evlist,
254932dcd021SJiri Olsa 			   int (*func)(struct evsel *evsel,
25504ba1faa1SWang Nan 				       const void *arg),
25514ba1faa1SWang Nan 			   const void *arg)
2552c171b552SLi Zefan {
255332dcd021SJiri Olsa 	struct evsel *last = NULL;
25544ba1faa1SWang Nan 	int err;
2555c171b552SLi Zefan 
2556854f7363SWang Nan 	/*
2557854f7363SWang Nan 	 * Don't return when list_empty, give func a chance to report
2558854f7363SWang Nan 	 * error when it found last == NULL.
2559854f7363SWang Nan 	 *
2560854f7363SWang Nan 	 * So no need to WARN here, let *func do this.
2561854f7363SWang Nan 	 */
25626484d2f9SJiri Olsa 	if (evlist->core.nr_entries > 0)
2563515dbe48SJiri Olsa 		last = evlist__last(evlist);
256469aad6f1SArnaldo Carvalho de Melo 
256515bfd2ccSWang Nan 	do {
25664ba1faa1SWang Nan 		err = (*func)(last, arg);
25674ba1faa1SWang Nan 		if (err)
2568c171b552SLi Zefan 			return -1;
25694ba1faa1SWang Nan 		if (!last)
25704ba1faa1SWang Nan 			return 0;
2571c171b552SLi Zefan 
2572ce9036a6SJiri Olsa 		if (last->core.node.prev == &evlist->core.entries)
257315bfd2ccSWang Nan 			return 0;
2574b27c4eceSJiri Olsa 		last = list_entry(last->core.node.prev, struct evsel, core.node);
257515bfd2ccSWang Nan 	} while (!last->cmdline_group_boundary);
257615bfd2ccSWang Nan 
2577c171b552SLi Zefan 	return 0;
2578c171b552SLi Zefan }
2579c171b552SLi Zefan 
258032dcd021SJiri Olsa static int set_filter(struct evsel *evsel, const void *arg)
25814ba1faa1SWang Nan {
25824ba1faa1SWang Nan 	const char *str = arg;
25831e857484SMathieu Poirier 	bool found = false;
25841e857484SMathieu Poirier 	int nr_addr_filters = 0;
25851e857484SMathieu Poirier 	struct perf_pmu *pmu = NULL;
25864ba1faa1SWang Nan 
258749836f78SJack Henschel 	if (evsel == NULL) {
258849836f78SJack Henschel 		fprintf(stderr,
258949836f78SJack Henschel 			"--filter option should follow a -e tracepoint or HW tracer option\n");
259049836f78SJack Henschel 		return -1;
259149836f78SJack Henschel 	}
25924ba1faa1SWang Nan 
25931fc632ceSJiri Olsa 	if (evsel->core.attr.type == PERF_TYPE_TRACEPOINT) {
2594ad681adfSArnaldo Carvalho de Melo 		if (evsel__append_tp_filter(evsel, str) < 0) {
25954ba1faa1SWang Nan 			fprintf(stderr,
25964ba1faa1SWang Nan 				"not enough memory to hold filter string\n");
25974ba1faa1SWang Nan 			return -1;
25984ba1faa1SWang Nan 		}
25994ba1faa1SWang Nan 
26004ba1faa1SWang Nan 		return 0;
26014ba1faa1SWang Nan 	}
26024ba1faa1SWang Nan 
26031e857484SMathieu Poirier 	while ((pmu = perf_pmu__scan(pmu)) != NULL)
26041fc632ceSJiri Olsa 		if (pmu->type == evsel->core.attr.type) {
26051e857484SMathieu Poirier 			found = true;
26061e857484SMathieu Poirier 			break;
26071e857484SMathieu Poirier 		}
26081e857484SMathieu Poirier 
26091e857484SMathieu Poirier 	if (found)
26101e857484SMathieu Poirier 		perf_pmu__scan_file(pmu, "nr_addr_filters",
26111e857484SMathieu Poirier 				    "%d", &nr_addr_filters);
26121e857484SMathieu Poirier 
261349836f78SJack Henschel 	if (!nr_addr_filters) {
261449836f78SJack Henschel 		fprintf(stderr,
261549836f78SJack Henschel 			"This CPU does not support address filtering\n");
261649836f78SJack Henschel 		return -1;
261749836f78SJack Henschel 	}
26181e857484SMathieu Poirier 
2619ad681adfSArnaldo Carvalho de Melo 	if (evsel__append_addr_filter(evsel, str) < 0) {
26201e857484SMathieu Poirier 		fprintf(stderr,
26211e857484SMathieu Poirier 			"not enough memory to hold filter string\n");
26221e857484SMathieu Poirier 		return -1;
26231e857484SMathieu Poirier 	}
26241e857484SMathieu Poirier 
26251e857484SMathieu Poirier 	return 0;
26261e857484SMathieu Poirier }
26271e857484SMathieu Poirier 
26284ba1faa1SWang Nan int parse_filter(const struct option *opt, const char *str,
26294ba1faa1SWang Nan 		 int unset __maybe_unused)
26304ba1faa1SWang Nan {
263163503dbaSJiri Olsa 	struct evlist *evlist = *(struct evlist **)opt->value;
26324ba1faa1SWang Nan 
26334ba1faa1SWang Nan 	return foreach_evsel_in_last_glob(evlist, set_filter,
26344ba1faa1SWang Nan 					  (const void *)str);
26354ba1faa1SWang Nan }
26364ba1faa1SWang Nan 
263732dcd021SJiri Olsa static int add_exclude_perf_filter(struct evsel *evsel,
26384ba1faa1SWang Nan 				   const void *arg __maybe_unused)
26394ba1faa1SWang Nan {
26404ba1faa1SWang Nan 	char new_filter[64];
26414ba1faa1SWang Nan 
26421fc632ceSJiri Olsa 	if (evsel == NULL || evsel->core.attr.type != PERF_TYPE_TRACEPOINT) {
26434ba1faa1SWang Nan 		fprintf(stderr,
26444ba1faa1SWang Nan 			"--exclude-perf option should follow a -e tracepoint option\n");
26454ba1faa1SWang Nan 		return -1;
26464ba1faa1SWang Nan 	}
26474ba1faa1SWang Nan 
26484ba1faa1SWang Nan 	snprintf(new_filter, sizeof(new_filter), "common_pid != %d", getpid());
26494ba1faa1SWang Nan 
2650ad681adfSArnaldo Carvalho de Melo 	if (evsel__append_tp_filter(evsel, new_filter) < 0) {
26514ba1faa1SWang Nan 		fprintf(stderr,
26524ba1faa1SWang Nan 			"not enough memory to hold filter string\n");
26534ba1faa1SWang Nan 		return -1;
26544ba1faa1SWang Nan 	}
26554ba1faa1SWang Nan 
26564ba1faa1SWang Nan 	return 0;
26574ba1faa1SWang Nan }
26584ba1faa1SWang Nan 
26594ba1faa1SWang Nan int exclude_perf(const struct option *opt,
26604ba1faa1SWang Nan 		 const char *arg __maybe_unused,
26614ba1faa1SWang Nan 		 int unset __maybe_unused)
26624ba1faa1SWang Nan {
266363503dbaSJiri Olsa 	struct evlist *evlist = *(struct evlist **)opt->value;
26644ba1faa1SWang Nan 
26654ba1faa1SWang Nan 	return foreach_evsel_in_last_glob(evlist, add_exclude_perf_filter,
26664ba1faa1SWang Nan 					  NULL);
26674ba1faa1SWang Nan }
26684ba1faa1SWang Nan 
266986470930SIngo Molnar static const char * const event_type_descriptors[] = {
267086470930SIngo Molnar 	"Hardware event",
267186470930SIngo Molnar 	"Software event",
267286470930SIngo Molnar 	"Tracepoint event",
267386470930SIngo Molnar 	"Hardware cache event",
267441bdcb23SLiming Wang 	"Raw hardware event descriptor",
267541bdcb23SLiming Wang 	"Hardware breakpoint",
267686470930SIngo Molnar };
267786470930SIngo Molnar 
2678ab0e4800SYunlong Song static int cmp_string(const void *a, const void *b)
2679ab0e4800SYunlong Song {
2680ab0e4800SYunlong Song 	const char * const *as = a;
2681ab0e4800SYunlong Song 	const char * const *bs = b;
2682ab0e4800SYunlong Song 
2683ab0e4800SYunlong Song 	return strcmp(*as, *bs);
2684ab0e4800SYunlong Song }
2685ab0e4800SYunlong Song 
268686470930SIngo Molnar /*
2687f6bdafefSJason Baron  * Print the events from <debugfs_mount_point>/tracing/events
2688f6bdafefSJason Baron  */
2689f6bdafefSJason Baron 
2690a3277d2dSFrederic Weisbecker void print_tracepoint_events(const char *subsys_glob, const char *event_glob,
2691a3277d2dSFrederic Weisbecker 			     bool name_only)
2692f6bdafefSJason Baron {
2693f6bdafefSJason Baron 	DIR *sys_dir, *evt_dir;
269422a9f41bSArnaldo Carvalho de Melo 	struct dirent *sys_dirent, *evt_dirent;
2695f6bdafefSJason Baron 	char evt_path[MAXPATHLEN];
269625a7d914SArnaldo Carvalho de Melo 	char *dir_path;
2697ab0e4800SYunlong Song 	char **evt_list = NULL;
2698ab0e4800SYunlong Song 	unsigned int evt_i = 0, evt_num = 0;
2699ab0e4800SYunlong Song 	bool evt_num_known = false;
2700f6bdafefSJason Baron 
2701ab0e4800SYunlong Song restart:
27027014e0e3SArnaldo Carvalho de Melo 	sys_dir = tracing_events__opendir();
2703f6bdafefSJason Baron 	if (!sys_dir)
2704725b1368SEric Dumazet 		return;
2705f6bdafefSJason Baron 
2706ab0e4800SYunlong Song 	if (evt_num_known) {
2707ab0e4800SYunlong Song 		evt_list = zalloc(sizeof(char *) * evt_num);
2708ab0e4800SYunlong Song 		if (!evt_list)
2709ab0e4800SYunlong Song 			goto out_close_sys_dir;
2710ab0e4800SYunlong Song 	}
2711ab0e4800SYunlong Song 
271222a9f41bSArnaldo Carvalho de Melo 	for_each_subsystem(sys_dir, sys_dirent) {
2713668b8788SArnaldo Carvalho de Melo 		if (subsys_glob != NULL &&
271422a9f41bSArnaldo Carvalho de Melo 		    !strglobmatch(sys_dirent->d_name, subsys_glob))
2715668b8788SArnaldo Carvalho de Melo 			continue;
2716725b1368SEric Dumazet 
271725a7d914SArnaldo Carvalho de Melo 		dir_path = get_events_file(sys_dirent->d_name);
271825a7d914SArnaldo Carvalho de Melo 		if (!dir_path)
271925a7d914SArnaldo Carvalho de Melo 			continue;
2720725b1368SEric Dumazet 		evt_dir = opendir(dir_path);
2721725b1368SEric Dumazet 		if (!evt_dir)
272225a7d914SArnaldo Carvalho de Melo 			goto next;
2723725b1368SEric Dumazet 
2724c02cab22SArnaldo Carvalho de Melo 		for_each_event(dir_path, evt_dir, evt_dirent) {
2725668b8788SArnaldo Carvalho de Melo 			if (event_glob != NULL &&
272622a9f41bSArnaldo Carvalho de Melo 			    !strglobmatch(evt_dirent->d_name, event_glob))
2727668b8788SArnaldo Carvalho de Melo 				continue;
2728668b8788SArnaldo Carvalho de Melo 
2729ab0e4800SYunlong Song 			if (!evt_num_known) {
2730ab0e4800SYunlong Song 				evt_num++;
2731a3277d2dSFrederic Weisbecker 				continue;
2732a3277d2dSFrederic Weisbecker 			}
2733a3277d2dSFrederic Weisbecker 
2734f6bdafefSJason Baron 			snprintf(evt_path, MAXPATHLEN, "%s:%s",
273522a9f41bSArnaldo Carvalho de Melo 				 sys_dirent->d_name, evt_dirent->d_name);
2736ab0e4800SYunlong Song 
2737ab0e4800SYunlong Song 			evt_list[evt_i] = strdup(evt_path);
273825a7d914SArnaldo Carvalho de Melo 			if (evt_list[evt_i] == NULL) {
273925a7d914SArnaldo Carvalho de Melo 				put_events_file(dir_path);
2740ab0e4800SYunlong Song 				goto out_close_evt_dir;
274125a7d914SArnaldo Carvalho de Melo 			}
2742ab0e4800SYunlong Song 			evt_i++;
2743f6bdafefSJason Baron 		}
2744f6bdafefSJason Baron 		closedir(evt_dir);
274525a7d914SArnaldo Carvalho de Melo next:
274625a7d914SArnaldo Carvalho de Melo 		put_events_file(dir_path);
2747f6bdafefSJason Baron 	}
2748f6bdafefSJason Baron 	closedir(sys_dir);
2749ab0e4800SYunlong Song 
2750ab0e4800SYunlong Song 	if (!evt_num_known) {
2751ab0e4800SYunlong Song 		evt_num_known = true;
2752ab0e4800SYunlong Song 		goto restart;
2753ab0e4800SYunlong Song 	}
2754ab0e4800SYunlong Song 	qsort(evt_list, evt_num, sizeof(char *), cmp_string);
2755ab0e4800SYunlong Song 	evt_i = 0;
2756ab0e4800SYunlong Song 	while (evt_i < evt_num) {
2757ab0e4800SYunlong Song 		if (name_only) {
2758ab0e4800SYunlong Song 			printf("%s ", evt_list[evt_i++]);
2759ab0e4800SYunlong Song 			continue;
2760ab0e4800SYunlong Song 		}
2761ab0e4800SYunlong Song 		printf("  %-50s [%s]\n", evt_list[evt_i++],
2762ab0e4800SYunlong Song 				event_type_descriptors[PERF_TYPE_TRACEPOINT]);
2763ab0e4800SYunlong Song 	}
2764dfc431cbSArnaldo Carvalho de Melo 	if (evt_num && pager_in_use())
2765ab0e4800SYunlong Song 		printf("\n");
2766ab0e4800SYunlong Song 
2767ab0e4800SYunlong Song out_free:
2768ab0e4800SYunlong Song 	evt_num = evt_i;
2769ab0e4800SYunlong Song 	for (evt_i = 0; evt_i < evt_num; evt_i++)
2770ab0e4800SYunlong Song 		zfree(&evt_list[evt_i]);
2771ab0e4800SYunlong Song 	zfree(&evt_list);
2772ab0e4800SYunlong Song 	return;
2773ab0e4800SYunlong Song 
2774ab0e4800SYunlong Song out_close_evt_dir:
2775ab0e4800SYunlong Song 	closedir(evt_dir);
2776ab0e4800SYunlong Song out_close_sys_dir:
2777ab0e4800SYunlong Song 	closedir(sys_dir);
2778ab0e4800SYunlong Song 
2779ab0e4800SYunlong Song 	printf("FATAL: not enough memory to print %s\n",
2780ab0e4800SYunlong Song 			event_type_descriptors[PERF_TYPE_TRACEPOINT]);
2781ab0e4800SYunlong Song 	if (evt_list)
2782ab0e4800SYunlong Song 		goto out_free;
2783f6bdafefSJason Baron }
2784f6bdafefSJason Baron 
2785f6bdafefSJason Baron /*
278620c457b8SThomas Renninger  * Check whether event is in <debugfs_mount_point>/tracing/events
278720c457b8SThomas Renninger  */
278820c457b8SThomas Renninger 
278920c457b8SThomas Renninger int is_valid_tracepoint(const char *event_string)
279020c457b8SThomas Renninger {
279120c457b8SThomas Renninger 	DIR *sys_dir, *evt_dir;
279222a9f41bSArnaldo Carvalho de Melo 	struct dirent *sys_dirent, *evt_dirent;
279320c457b8SThomas Renninger 	char evt_path[MAXPATHLEN];
279425a7d914SArnaldo Carvalho de Melo 	char *dir_path;
279520c457b8SThomas Renninger 
27967014e0e3SArnaldo Carvalho de Melo 	sys_dir = tracing_events__opendir();
279720c457b8SThomas Renninger 	if (!sys_dir)
279820c457b8SThomas Renninger 		return 0;
279920c457b8SThomas Renninger 
280022a9f41bSArnaldo Carvalho de Melo 	for_each_subsystem(sys_dir, sys_dirent) {
280125a7d914SArnaldo Carvalho de Melo 		dir_path = get_events_file(sys_dirent->d_name);
280225a7d914SArnaldo Carvalho de Melo 		if (!dir_path)
280325a7d914SArnaldo Carvalho de Melo 			continue;
280420c457b8SThomas Renninger 		evt_dir = opendir(dir_path);
280520c457b8SThomas Renninger 		if (!evt_dir)
280625a7d914SArnaldo Carvalho de Melo 			goto next;
280720c457b8SThomas Renninger 
2808c02cab22SArnaldo Carvalho de Melo 		for_each_event(dir_path, evt_dir, evt_dirent) {
280920c457b8SThomas Renninger 			snprintf(evt_path, MAXPATHLEN, "%s:%s",
281022a9f41bSArnaldo Carvalho de Melo 				 sys_dirent->d_name, evt_dirent->d_name);
281120c457b8SThomas Renninger 			if (!strcmp(evt_path, event_string)) {
281220c457b8SThomas Renninger 				closedir(evt_dir);
281320c457b8SThomas Renninger 				closedir(sys_dir);
281420c457b8SThomas Renninger 				return 1;
281520c457b8SThomas Renninger 			}
281620c457b8SThomas Renninger 		}
281720c457b8SThomas Renninger 		closedir(evt_dir);
281825a7d914SArnaldo Carvalho de Melo next:
281925a7d914SArnaldo Carvalho de Melo 		put_events_file(dir_path);
282020c457b8SThomas Renninger 	}
282120c457b8SThomas Renninger 	closedir(sys_dir);
282220c457b8SThomas Renninger 	return 0;
282320c457b8SThomas Renninger }
282420c457b8SThomas Renninger 
28256c93f39fSJin Yao static bool is_event_supported(u8 type, u64 config)
2826b41f1cecSNamhyung Kim {
2827b41f1cecSNamhyung Kim 	bool ret = true;
282888fee52eSVince Weaver 	int open_return;
282932dcd021SJiri Olsa 	struct evsel *evsel;
2830b41f1cecSNamhyung Kim 	struct perf_event_attr attr = {
2831b41f1cecSNamhyung Kim 		.type = type,
2832b41f1cecSNamhyung Kim 		.config = config,
2833b41f1cecSNamhyung Kim 		.disabled = 1,
2834b41f1cecSNamhyung Kim 	};
28359749b90eSJiri Olsa 	struct perf_thread_map *tmap = thread_map__new_by_tid(0);
283689896051SArnaldo Carvalho de Melo 
283789896051SArnaldo Carvalho de Melo 	if (tmap == NULL)
283889896051SArnaldo Carvalho de Melo 		return false;
2839b41f1cecSNamhyung Kim 
2840365c3ae7SJiri Olsa 	evsel = evsel__new(&attr);
2841b41f1cecSNamhyung Kim 	if (evsel) {
28425972d1e0SJiri Olsa 		open_return = evsel__open(evsel, NULL, tmap);
284388fee52eSVince Weaver 		ret = open_return >= 0;
284488fee52eSVince Weaver 
284588fee52eSVince Weaver 		if (open_return == -EACCES) {
284688fee52eSVince Weaver 			/*
284788fee52eSVince Weaver 			 * This happens if the paranoid value
284888fee52eSVince Weaver 			 * /proc/sys/kernel/perf_event_paranoid is set to 2
284988fee52eSVince Weaver 			 * Re-run with exclude_kernel set; we don't do that
285088fee52eSVince Weaver 			 * by default as some ARM machines do not support it.
285188fee52eSVince Weaver 			 *
285288fee52eSVince Weaver 			 */
28531fc632ceSJiri Olsa 			evsel->core.attr.exclude_kernel = 1;
28545972d1e0SJiri Olsa 			ret = evsel__open(evsel, NULL, tmap) >= 0;
285588fee52eSVince Weaver 		}
28565eb2dd2aSJiri Olsa 		evsel__delete(evsel);
2857b41f1cecSNamhyung Kim 	}
2858b41f1cecSNamhyung Kim 
28597836e52eSJiri Olsa 	perf_thread_map__put(tmap);
2860b41f1cecSNamhyung Kim 	return ret;
2861b41f1cecSNamhyung Kim }
2862b41f1cecSNamhyung Kim 
286340218daeSMasami Hiramatsu void print_sdt_events(const char *subsys_glob, const char *event_glob,
286440218daeSMasami Hiramatsu 		      bool name_only)
286540218daeSMasami Hiramatsu {
286640218daeSMasami Hiramatsu 	struct probe_cache *pcache;
286740218daeSMasami Hiramatsu 	struct probe_cache_entry *ent;
286840218daeSMasami Hiramatsu 	struct strlist *bidlist, *sdtlist;
286940218daeSMasami Hiramatsu 	struct strlist_config cfg = {.dont_dupstr = true};
287040218daeSMasami Hiramatsu 	struct str_node *nd, *nd2;
287140218daeSMasami Hiramatsu 	char *buf, *path, *ptr = NULL;
287240218daeSMasami Hiramatsu 	bool show_detail = false;
287340218daeSMasami Hiramatsu 	int ret;
287440218daeSMasami Hiramatsu 
287540218daeSMasami Hiramatsu 	sdtlist = strlist__new(NULL, &cfg);
287640218daeSMasami Hiramatsu 	if (!sdtlist) {
287740218daeSMasami Hiramatsu 		pr_debug("Failed to allocate new strlist for SDT\n");
287840218daeSMasami Hiramatsu 		return;
287940218daeSMasami Hiramatsu 	}
288040218daeSMasami Hiramatsu 	bidlist = build_id_cache__list_all(true);
288140218daeSMasami Hiramatsu 	if (!bidlist) {
288240218daeSMasami Hiramatsu 		pr_debug("Failed to get buildids: %d\n", errno);
288340218daeSMasami Hiramatsu 		return;
288440218daeSMasami Hiramatsu 	}
288540218daeSMasami Hiramatsu 	strlist__for_each_entry(nd, bidlist) {
2886f045b8c4SKrister Johansen 		pcache = probe_cache__new(nd->s, NULL);
288740218daeSMasami Hiramatsu 		if (!pcache)
288840218daeSMasami Hiramatsu 			continue;
288940218daeSMasami Hiramatsu 		list_for_each_entry(ent, &pcache->entries, node) {
289040218daeSMasami Hiramatsu 			if (!ent->sdt)
289140218daeSMasami Hiramatsu 				continue;
289240218daeSMasami Hiramatsu 			if (subsys_glob &&
289340218daeSMasami Hiramatsu 			    !strglobmatch(ent->pev.group, subsys_glob))
289440218daeSMasami Hiramatsu 				continue;
289540218daeSMasami Hiramatsu 			if (event_glob &&
289640218daeSMasami Hiramatsu 			    !strglobmatch(ent->pev.event, event_glob))
289740218daeSMasami Hiramatsu 				continue;
289840218daeSMasami Hiramatsu 			ret = asprintf(&buf, "%s:%s@%s", ent->pev.group,
289940218daeSMasami Hiramatsu 					ent->pev.event, nd->s);
290040218daeSMasami Hiramatsu 			if (ret > 0)
290140218daeSMasami Hiramatsu 				strlist__add(sdtlist, buf);
290240218daeSMasami Hiramatsu 		}
290340218daeSMasami Hiramatsu 		probe_cache__delete(pcache);
290440218daeSMasami Hiramatsu 	}
290540218daeSMasami Hiramatsu 	strlist__delete(bidlist);
290640218daeSMasami Hiramatsu 
290740218daeSMasami Hiramatsu 	strlist__for_each_entry(nd, sdtlist) {
290840218daeSMasami Hiramatsu 		buf = strchr(nd->s, '@');
290940218daeSMasami Hiramatsu 		if (buf)
291040218daeSMasami Hiramatsu 			*(buf++) = '\0';
291140218daeSMasami Hiramatsu 		if (name_only) {
291240218daeSMasami Hiramatsu 			printf("%s ", nd->s);
291340218daeSMasami Hiramatsu 			continue;
291440218daeSMasami Hiramatsu 		}
291540218daeSMasami Hiramatsu 		nd2 = strlist__next(nd);
291640218daeSMasami Hiramatsu 		if (nd2) {
291740218daeSMasami Hiramatsu 			ptr = strchr(nd2->s, '@');
291840218daeSMasami Hiramatsu 			if (ptr)
291940218daeSMasami Hiramatsu 				*ptr = '\0';
292040218daeSMasami Hiramatsu 			if (strcmp(nd->s, nd2->s) == 0)
292140218daeSMasami Hiramatsu 				show_detail = true;
292240218daeSMasami Hiramatsu 		}
292340218daeSMasami Hiramatsu 		if (show_detail) {
292440218daeSMasami Hiramatsu 			path = build_id_cache__origname(buf);
292540218daeSMasami Hiramatsu 			ret = asprintf(&buf, "%s@%s(%.12s)", nd->s, path, buf);
292640218daeSMasami Hiramatsu 			if (ret > 0) {
292740218daeSMasami Hiramatsu 				printf("  %-50s [%s]\n", buf, "SDT event");
292840218daeSMasami Hiramatsu 				free(buf);
292940218daeSMasami Hiramatsu 			}
29308bde8516SChangbin Du 			free(path);
293140218daeSMasami Hiramatsu 		} else
293240218daeSMasami Hiramatsu 			printf("  %-50s [%s]\n", nd->s, "SDT event");
293340218daeSMasami Hiramatsu 		if (nd2) {
293440218daeSMasami Hiramatsu 			if (strcmp(nd->s, nd2->s) != 0)
293540218daeSMasami Hiramatsu 				show_detail = false;
293640218daeSMasami Hiramatsu 			if (ptr)
293740218daeSMasami Hiramatsu 				*ptr = '@';
293840218daeSMasami Hiramatsu 		}
293940218daeSMasami Hiramatsu 	}
294040218daeSMasami Hiramatsu 	strlist__delete(sdtlist);
294140218daeSMasami Hiramatsu }
294240218daeSMasami Hiramatsu 
2943a3277d2dSFrederic Weisbecker int print_hwcache_events(const char *event_glob, bool name_only)
2944668b8788SArnaldo Carvalho de Melo {
29456c93f39fSJin Yao 	unsigned int type, op, i, evt_i = 0, evt_num = 0, npmus = 0;
29466c93f39fSJin Yao 	char name[64], new_name[128];
29476c93f39fSJin Yao 	char **evt_list = NULL, **evt_pmus = NULL;
2948ab0e4800SYunlong Song 	bool evt_num_known = false;
29496c93f39fSJin Yao 	struct perf_pmu *pmu = NULL;
29506c93f39fSJin Yao 
29516c93f39fSJin Yao 	if (perf_pmu__has_hybrid()) {
29526c93f39fSJin Yao 		npmus = perf_pmu__hybrid_pmu_num();
29536c93f39fSJin Yao 		evt_pmus = zalloc(sizeof(char *) * npmus);
29546c93f39fSJin Yao 		if (!evt_pmus)
29556c93f39fSJin Yao 			goto out_enomem;
29566c93f39fSJin Yao 	}
2957ab0e4800SYunlong Song 
2958ab0e4800SYunlong Song restart:
2959ab0e4800SYunlong Song 	if (evt_num_known) {
2960ab0e4800SYunlong Song 		evt_list = zalloc(sizeof(char *) * evt_num);
2961ab0e4800SYunlong Song 		if (!evt_list)
2962ab0e4800SYunlong Song 			goto out_enomem;
2963ab0e4800SYunlong Song 	}
2964668b8788SArnaldo Carvalho de Melo 
2965668b8788SArnaldo Carvalho de Melo 	for (type = 0; type < PERF_COUNT_HW_CACHE_MAX; type++) {
2966668b8788SArnaldo Carvalho de Melo 		for (op = 0; op < PERF_COUNT_HW_CACHE_OP_MAX; op++) {
2967668b8788SArnaldo Carvalho de Melo 			/* skip invalid cache type */
2968c754c382SArnaldo Carvalho de Melo 			if (!evsel__is_cache_op_valid(type, op))
2969668b8788SArnaldo Carvalho de Melo 				continue;
2970668b8788SArnaldo Carvalho de Melo 
2971668b8788SArnaldo Carvalho de Melo 			for (i = 0; i < PERF_COUNT_HW_CACHE_RESULT_MAX; i++) {
29726c93f39fSJin Yao 				unsigned int hybrid_supported = 0, j;
29736c93f39fSJin Yao 				bool supported;
29746c93f39fSJin Yao 
29758ab2e96dSArnaldo Carvalho de Melo 				__evsel__hw_cache_type_op_res_name(type, op, i, name, sizeof(name));
2976947b4ad1SIngo Molnar 				if (event_glob != NULL && !strglobmatch(name, event_glob))
2977668b8788SArnaldo Carvalho de Melo 					continue;
2978668b8788SArnaldo Carvalho de Melo 
29796c93f39fSJin Yao 				if (!perf_pmu__has_hybrid()) {
2980b41f1cecSNamhyung Kim 					if (!is_event_supported(PERF_TYPE_HW_CACHE,
29816c93f39fSJin Yao 								type | (op << 8) | (i << 16))) {
2982b41f1cecSNamhyung Kim 						continue;
29836c93f39fSJin Yao 					}
29846c93f39fSJin Yao 				} else {
29856c93f39fSJin Yao 					perf_pmu__for_each_hybrid_pmu(pmu) {
29866c93f39fSJin Yao 						if (!evt_num_known) {
29876c93f39fSJin Yao 							evt_num++;
29886c93f39fSJin Yao 							continue;
29896c93f39fSJin Yao 						}
29906c93f39fSJin Yao 
29916c93f39fSJin Yao 						supported = is_event_supported(
29926c93f39fSJin Yao 									PERF_TYPE_HW_CACHE,
29936c93f39fSJin Yao 									type | (op << 8) | (i << 16) |
29946c93f39fSJin Yao 									((__u64)pmu->type << PERF_PMU_TYPE_SHIFT));
29956c93f39fSJin Yao 						if (supported) {
29966c93f39fSJin Yao 							snprintf(new_name, sizeof(new_name), "%s/%s/",
29976c93f39fSJin Yao 								 pmu->name, name);
29986c93f39fSJin Yao 							evt_pmus[hybrid_supported] = strdup(new_name);
29996c93f39fSJin Yao 							hybrid_supported++;
30006c93f39fSJin Yao 						}
30016c93f39fSJin Yao 					}
30026c93f39fSJin Yao 
30036c93f39fSJin Yao 					if (hybrid_supported == 0)
30046c93f39fSJin Yao 						continue;
30056c93f39fSJin Yao 				}
3006b41f1cecSNamhyung Kim 
3007ab0e4800SYunlong Song 				if (!evt_num_known) {
3008ab0e4800SYunlong Song 					evt_num++;
3009ab0e4800SYunlong Song 					continue;
3010ab0e4800SYunlong Song 				}
3011ab0e4800SYunlong Song 
30126c93f39fSJin Yao 				if ((hybrid_supported == 0) ||
30136c93f39fSJin Yao 				    (hybrid_supported == npmus)) {
3014ab0e4800SYunlong Song 					evt_list[evt_i] = strdup(name);
30156c93f39fSJin Yao 					if (npmus > 0) {
30166c93f39fSJin Yao 						for (j = 0; j < npmus; j++)
30176c93f39fSJin Yao 							zfree(&evt_pmus[j]);
30186c93f39fSJin Yao 					}
30196c93f39fSJin Yao 				} else {
30206c93f39fSJin Yao 					for (j = 0; j < hybrid_supported; j++) {
30216c93f39fSJin Yao 						evt_list[evt_i++] = evt_pmus[j];
30226c93f39fSJin Yao 						evt_pmus[j] = NULL;
30236c93f39fSJin Yao 					}
30246c93f39fSJin Yao 					continue;
30256c93f39fSJin Yao 				}
30266c93f39fSJin Yao 
3027ab0e4800SYunlong Song 				if (evt_list[evt_i] == NULL)
3028ab0e4800SYunlong Song 					goto out_enomem;
3029ab0e4800SYunlong Song 				evt_i++;
3030668b8788SArnaldo Carvalho de Melo 			}
3031668b8788SArnaldo Carvalho de Melo 		}
3032668b8788SArnaldo Carvalho de Melo 	}
3033668b8788SArnaldo Carvalho de Melo 
3034ab0e4800SYunlong Song 	if (!evt_num_known) {
3035ab0e4800SYunlong Song 		evt_num_known = true;
3036ab0e4800SYunlong Song 		goto restart;
3037ab0e4800SYunlong Song 	}
30386c93f39fSJin Yao 
30396c93f39fSJin Yao 	for (evt_i = 0; evt_i < evt_num; evt_i++) {
30406c93f39fSJin Yao 		if (!evt_list[evt_i])
30416c93f39fSJin Yao 			break;
30426c93f39fSJin Yao 	}
30436c93f39fSJin Yao 
30446c93f39fSJin Yao 	evt_num = evt_i;
3045ab0e4800SYunlong Song 	qsort(evt_list, evt_num, sizeof(char *), cmp_string);
3046ab0e4800SYunlong Song 	evt_i = 0;
3047ab0e4800SYunlong Song 	while (evt_i < evt_num) {
3048ab0e4800SYunlong Song 		if (name_only) {
3049ab0e4800SYunlong Song 			printf("%s ", evt_list[evt_i++]);
3050ab0e4800SYunlong Song 			continue;
3051ab0e4800SYunlong Song 		}
3052ab0e4800SYunlong Song 		printf("  %-50s [%s]\n", evt_list[evt_i++],
3053ab0e4800SYunlong Song 				event_type_descriptors[PERF_TYPE_HW_CACHE]);
3054ab0e4800SYunlong Song 	}
3055dfc431cbSArnaldo Carvalho de Melo 	if (evt_num && pager_in_use())
3056dc098b35SAndi Kleen 		printf("\n");
3057ab0e4800SYunlong Song 
3058ab0e4800SYunlong Song out_free:
3059ab0e4800SYunlong Song 	evt_num = evt_i;
3060ab0e4800SYunlong Song 	for (evt_i = 0; evt_i < evt_num; evt_i++)
3061ab0e4800SYunlong Song 		zfree(&evt_list[evt_i]);
3062ab0e4800SYunlong Song 	zfree(&evt_list);
30636c93f39fSJin Yao 
30646c93f39fSJin Yao 	for (evt_i = 0; evt_i < npmus; evt_i++)
30656c93f39fSJin Yao 		zfree(&evt_pmus[evt_i]);
30666c93f39fSJin Yao 	zfree(&evt_pmus);
3067ab0e4800SYunlong Song 	return evt_num;
3068ab0e4800SYunlong Song 
3069ab0e4800SYunlong Song out_enomem:
3070ab0e4800SYunlong Song 	printf("FATAL: not enough memory to print %s\n", event_type_descriptors[PERF_TYPE_HW_CACHE]);
3071ab0e4800SYunlong Song 	if (evt_list)
3072ab0e4800SYunlong Song 		goto out_free;
3073ab0e4800SYunlong Song 	return evt_num;
3074668b8788SArnaldo Carvalho de Melo }
3075668b8788SArnaldo Carvalho de Melo 
307675eafc97SFlorian Fischer static void print_tool_event(const struct event_symbol *syms, const char *event_glob,
30775e0861baSAndi Kleen 			     bool name_only)
30785e0861baSAndi Kleen {
307975eafc97SFlorian Fischer 	if (syms->symbol == NULL)
30805e0861baSAndi Kleen 		return;
30815e0861baSAndi Kleen 
308275eafc97SFlorian Fischer 	if (event_glob && !(strglobmatch(syms->symbol, event_glob) ||
308375eafc97SFlorian Fischer 	      (syms->alias && strglobmatch(syms->alias, event_glob))))
308475eafc97SFlorian Fischer 		return;
308575eafc97SFlorian Fischer 
308675eafc97SFlorian Fischer 	if (name_only)
308775eafc97SFlorian Fischer 		printf("%s ", syms->symbol);
308875eafc97SFlorian Fischer 	else {
308975eafc97SFlorian Fischer 		char name[MAX_NAME_LEN];
309075eafc97SFlorian Fischer 		if (syms->alias && strlen(syms->alias))
309175eafc97SFlorian Fischer 			snprintf(name, MAX_NAME_LEN, "%s OR %s", syms->symbol, syms->alias);
309275eafc97SFlorian Fischer 		else
309375eafc97SFlorian Fischer 			strlcpy(name, syms->symbol, MAX_NAME_LEN);
309475eafc97SFlorian Fischer 		printf("  %-50s [%s]\n", name, "Tool event");
309575eafc97SFlorian Fischer 	}
30965e0861baSAndi Kleen }
30975e0861baSAndi Kleen 
30985e0861baSAndi Kleen void print_tool_events(const char *event_glob, bool name_only)
30995e0861baSAndi Kleen {
310075eafc97SFlorian Fischer 	// Start at 1 because the first enum entry symbols no tool event
310175eafc97SFlorian Fischer 	for (int i = 1; i < PERF_TOOL_MAX; ++i) {
310275eafc97SFlorian Fischer 		print_tool_event(event_symbols_tool + i, event_glob, name_only);
310375eafc97SFlorian Fischer 	}
31045e0861baSAndi Kleen 	if (pager_in_use())
31055e0861baSAndi Kleen 		printf("\n");
31065e0861baSAndi Kleen }
31075e0861baSAndi Kleen 
3108705750f2SYunlong Song void print_symbol_events(const char *event_glob, unsigned type,
3109a3277d2dSFrederic Weisbecker 				struct event_symbol *syms, unsigned max,
3110a3277d2dSFrederic Weisbecker 				bool name_only)
311186470930SIngo Molnar {
3112ab0e4800SYunlong Song 	unsigned int i, evt_i = 0, evt_num = 0;
3113947b4ad1SIngo Molnar 	char name[MAX_NAME_LEN];
3114ab0e4800SYunlong Song 	char **evt_list = NULL;
3115ab0e4800SYunlong Song 	bool evt_num_known = false;
3116ab0e4800SYunlong Song 
3117ab0e4800SYunlong Song restart:
3118ab0e4800SYunlong Song 	if (evt_num_known) {
3119ab0e4800SYunlong Song 		evt_list = zalloc(sizeof(char *) * evt_num);
3120ab0e4800SYunlong Song 		if (!evt_list)
3121ab0e4800SYunlong Song 			goto out_enomem;
3122ab0e4800SYunlong Song 		syms -= max;
3123ab0e4800SYunlong Song 	}
312486470930SIngo Molnar 
31251dc12760SJiri Olsa 	for (i = 0; i < max; i++, syms++) {
31263b2f17adSArnaldo Carvalho de Melo 		/*
31273b2f17adSArnaldo Carvalho de Melo 		 * New attr.config still not supported here, the latest
31283b2f17adSArnaldo Carvalho de Melo 		 * example was PERF_COUNT_SW_CGROUP_SWITCHES
31293b2f17adSArnaldo Carvalho de Melo 		 */
31303b2f17adSArnaldo Carvalho de Melo 		if (syms->symbol == NULL)
31313b2f17adSArnaldo Carvalho de Melo 			continue;
3132668b8788SArnaldo Carvalho de Melo 
31333b2f17adSArnaldo Carvalho de Melo 		if (event_glob != NULL && !(strglobmatch(syms->symbol, event_glob) ||
3134668b8788SArnaldo Carvalho de Melo 		      (syms->alias && strglobmatch(syms->alias, event_glob))))
3135668b8788SArnaldo Carvalho de Melo 			continue;
313686470930SIngo Molnar 
3137b41f1cecSNamhyung Kim 		if (!is_event_supported(type, i))
3138b41f1cecSNamhyung Kim 			continue;
3139b41f1cecSNamhyung Kim 
3140ab0e4800SYunlong Song 		if (!evt_num_known) {
3141ab0e4800SYunlong Song 			evt_num++;
3142a3277d2dSFrederic Weisbecker 			continue;
3143a3277d2dSFrederic Weisbecker 		}
3144a3277d2dSFrederic Weisbecker 
3145ab0e4800SYunlong Song 		if (!name_only && strlen(syms->alias))
3146947b4ad1SIngo Molnar 			snprintf(name, MAX_NAME_LEN, "%s OR %s", syms->symbol, syms->alias);
314774d5b588SJaswinder Singh Rajput 		else
3148bd8d57fbSArnaldo Carvalho de Melo 			strlcpy(name, syms->symbol, MAX_NAME_LEN);
314986470930SIngo Molnar 
3150ab0e4800SYunlong Song 		evt_list[evt_i] = strdup(name);
3151ab0e4800SYunlong Song 		if (evt_list[evt_i] == NULL)
3152ab0e4800SYunlong Song 			goto out_enomem;
3153ab0e4800SYunlong Song 		evt_i++;
315486470930SIngo Molnar 	}
315586470930SIngo Molnar 
3156ab0e4800SYunlong Song 	if (!evt_num_known) {
3157ab0e4800SYunlong Song 		evt_num_known = true;
3158ab0e4800SYunlong Song 		goto restart;
3159ab0e4800SYunlong Song 	}
3160ab0e4800SYunlong Song 	qsort(evt_list, evt_num, sizeof(char *), cmp_string);
3161ab0e4800SYunlong Song 	evt_i = 0;
3162ab0e4800SYunlong Song 	while (evt_i < evt_num) {
3163ab0e4800SYunlong Song 		if (name_only) {
3164ab0e4800SYunlong Song 			printf("%s ", evt_list[evt_i++]);
3165ab0e4800SYunlong Song 			continue;
3166ab0e4800SYunlong Song 		}
3167ab0e4800SYunlong Song 		printf("  %-50s [%s]\n", evt_list[evt_i++], event_type_descriptors[type]);
3168ab0e4800SYunlong Song 	}
3169dfc431cbSArnaldo Carvalho de Melo 	if (evt_num && pager_in_use())
3170689d3018SMarti Raudsepp 		printf("\n");
3171ab0e4800SYunlong Song 
3172ab0e4800SYunlong Song out_free:
3173ab0e4800SYunlong Song 	evt_num = evt_i;
3174ab0e4800SYunlong Song 	for (evt_i = 0; evt_i < evt_num; evt_i++)
3175ab0e4800SYunlong Song 		zfree(&evt_list[evt_i]);
3176ab0e4800SYunlong Song 	zfree(&evt_list);
3177ab0e4800SYunlong Song 	return;
3178ab0e4800SYunlong Song 
3179ab0e4800SYunlong Song out_enomem:
3180ab0e4800SYunlong Song 	printf("FATAL: not enough memory to print %s\n", event_type_descriptors[type]);
3181ab0e4800SYunlong Song 	if (evt_list)
3182ab0e4800SYunlong Song 		goto out_free;
3183668b8788SArnaldo Carvalho de Melo }
31841dc12760SJiri Olsa 
31851dc12760SJiri Olsa /*
31861dc12760SJiri Olsa  * Print the help text for the event symbols:
31871dc12760SJiri Olsa  */
3188c8d6828aSSukadev Bhattiprolu void print_events(const char *event_glob, bool name_only, bool quiet_flag,
31890e0ae874SJin Yao 			bool long_desc, bool details_flag, bool deprecated,
31900e0ae874SJin Yao 			const char *pmu_name)
31911dc12760SJiri Olsa {
31921dc12760SJiri Olsa 	print_symbol_events(event_glob, PERF_TYPE_HARDWARE,
3193a3277d2dSFrederic Weisbecker 			    event_symbols_hw, PERF_COUNT_HW_MAX, name_only);
31941dc12760SJiri Olsa 
31951dc12760SJiri Olsa 	print_symbol_events(event_glob, PERF_TYPE_SOFTWARE,
3196a3277d2dSFrederic Weisbecker 			    event_symbols_sw, PERF_COUNT_SW_MAX, name_only);
31975e0861baSAndi Kleen 	print_tool_events(event_glob, name_only);
31981dc12760SJiri Olsa 
3199a3277d2dSFrederic Weisbecker 	print_hwcache_events(event_glob, name_only);
320073c24cb8SJaswinder Singh Rajput 
3201bf874fcfSAndi Kleen 	print_pmu_events(event_glob, name_only, quiet_flag, long_desc,
32020e0ae874SJin Yao 			details_flag, deprecated, pmu_name);
3203dc098b35SAndi Kleen 
3204668b8788SArnaldo Carvalho de Melo 	if (event_glob != NULL)
3205668b8788SArnaldo Carvalho de Melo 		return;
320673c24cb8SJaswinder Singh Rajput 
3207a3277d2dSFrederic Weisbecker 	if (!name_only) {
3208947b4ad1SIngo Molnar 		printf("  %-50s [%s]\n",
32095f537a26SJiri Olsa 		       "rNNN",
32101cf4a063SArnaldo Carvalho de Melo 		       event_type_descriptors[PERF_TYPE_RAW]);
32115f537a26SJiri Olsa 		printf("  %-50s [%s]\n",
32125f537a26SJiri Olsa 		       "cpu/t1=v1[,t2=v2,t3 ...]/modifier",
32135f537a26SJiri Olsa 		       event_type_descriptors[PERF_TYPE_RAW]);
3214dfc431cbSArnaldo Carvalho de Melo 		if (pager_in_use())
3215dfc431cbSArnaldo Carvalho de Melo 			printf("   (see 'man perf-list' on how to encode it)\n\n");
321686470930SIngo Molnar 
3217947b4ad1SIngo Molnar 		printf("  %-50s [%s]\n",
32183741eb9fSJacob Shin 		       "mem:<addr>[/len][:access]",
321941bdcb23SLiming Wang 			event_type_descriptors[PERF_TYPE_BREAKPOINT]);
3220dfc431cbSArnaldo Carvalho de Melo 		if (pager_in_use())
32211b290d67SFrederic Weisbecker 			printf("\n");
3222a3277d2dSFrederic Weisbecker 	}
32231b290d67SFrederic Weisbecker 
3224a3277d2dSFrederic Weisbecker 	print_tracepoint_events(NULL, NULL, name_only);
322540218daeSMasami Hiramatsu 
322640218daeSMasami Hiramatsu 	print_sdt_events(NULL, NULL, name_only);
322771b0acceSAndi Kleen 
32280e0ae874SJin Yao 	metricgroup__print(true, true, NULL, name_only, details_flag,
32290e0ae874SJin Yao 			   pmu_name);
323070943490SStephane Eranian 
323170943490SStephane Eranian 	print_libpfm_events(name_only, long_desc);
323286470930SIngo Molnar }
32338f707d84SJiri Olsa 
32346cee6cd3SArnaldo Carvalho de Melo int parse_events__is_hardcoded_term(struct parse_events_term *term)
32358f707d84SJiri Olsa {
323616fa7e82SJiri Olsa 	return term->type_term != PARSE_EVENTS__TERM_TYPE_USER;
32378f707d84SJiri Olsa }
32388f707d84SJiri Olsa 
323967b49b38SJiri Olsa static int new_term(struct parse_events_term **_term,
324067b49b38SJiri Olsa 		    struct parse_events_term *temp,
324167b49b38SJiri Olsa 		    char *str, u64 num)
32428f707d84SJiri Olsa {
32436cee6cd3SArnaldo Carvalho de Melo 	struct parse_events_term *term;
32448f707d84SJiri Olsa 
324567b49b38SJiri Olsa 	term = malloc(sizeof(*term));
32468f707d84SJiri Olsa 	if (!term)
32478f707d84SJiri Olsa 		return -ENOMEM;
32488f707d84SJiri Olsa 
324967b49b38SJiri Olsa 	*term = *temp;
32508f707d84SJiri Olsa 	INIT_LIST_HEAD(&term->list);
3251c2f1ceadSAndi Kleen 	term->weak = false;
32528f707d84SJiri Olsa 
325367b49b38SJiri Olsa 	switch (term->type_val) {
32548f707d84SJiri Olsa 	case PARSE_EVENTS__TERM_TYPE_NUM:
32558f707d84SJiri Olsa 		term->val.num = num;
32568f707d84SJiri Olsa 		break;
32578f707d84SJiri Olsa 	case PARSE_EVENTS__TERM_TYPE_STR:
32588f707d84SJiri Olsa 		term->val.str = str;
32598f707d84SJiri Olsa 		break;
32608f707d84SJiri Olsa 	default:
32614be8be6bSAdrian Hunter 		free(term);
32628f707d84SJiri Olsa 		return -EINVAL;
32638f707d84SJiri Olsa 	}
32648f707d84SJiri Olsa 
32658f707d84SJiri Olsa 	*_term = term;
32668f707d84SJiri Olsa 	return 0;
32678f707d84SJiri Olsa }
32688f707d84SJiri Olsa 
32696cee6cd3SArnaldo Carvalho de Melo int parse_events_term__num(struct parse_events_term **term,
3270cecf3a2eSJiri Olsa 			   int type_term, char *config, u64 num,
327199e7138eSJiri Olsa 			   bool no_value,
3272bb78ce7dSAdrian Hunter 			   void *loc_term_, void *loc_val_)
327316fa7e82SJiri Olsa {
3274bb78ce7dSAdrian Hunter 	YYLTYPE *loc_term = loc_term_;
3275bb78ce7dSAdrian Hunter 	YYLTYPE *loc_val = loc_val_;
3276bb78ce7dSAdrian Hunter 
327767b49b38SJiri Olsa 	struct parse_events_term temp = {
327867b49b38SJiri Olsa 		.type_val  = PARSE_EVENTS__TERM_TYPE_NUM,
327967b49b38SJiri Olsa 		.type_term = type_term,
32804f9d4f8aSJohn Garry 		.config    = config ? : strdup(config_term_names[type_term]),
328199e7138eSJiri Olsa 		.no_value  = no_value,
328267b49b38SJiri Olsa 		.err_term  = loc_term ? loc_term->first_column : 0,
328367b49b38SJiri Olsa 		.err_val   = loc_val  ? loc_val->first_column  : 0,
328467b49b38SJiri Olsa 	};
328567b49b38SJiri Olsa 
328667b49b38SJiri Olsa 	return new_term(term, &temp, NULL, num);
328716fa7e82SJiri Olsa }
328816fa7e82SJiri Olsa 
32896cee6cd3SArnaldo Carvalho de Melo int parse_events_term__str(struct parse_events_term **term,
3290cecf3a2eSJiri Olsa 			   int type_term, char *config, char *str,
3291bb78ce7dSAdrian Hunter 			   void *loc_term_, void *loc_val_)
329216fa7e82SJiri Olsa {
3293bb78ce7dSAdrian Hunter 	YYLTYPE *loc_term = loc_term_;
3294bb78ce7dSAdrian Hunter 	YYLTYPE *loc_val = loc_val_;
3295bb78ce7dSAdrian Hunter 
329667b49b38SJiri Olsa 	struct parse_events_term temp = {
329767b49b38SJiri Olsa 		.type_val  = PARSE_EVENTS__TERM_TYPE_STR,
329867b49b38SJiri Olsa 		.type_term = type_term,
329967b49b38SJiri Olsa 		.config    = config,
330067b49b38SJiri Olsa 		.err_term  = loc_term ? loc_term->first_column : 0,
330167b49b38SJiri Olsa 		.err_val   = loc_val  ? loc_val->first_column  : 0,
330267b49b38SJiri Olsa 	};
330367b49b38SJiri Olsa 
330467b49b38SJiri Olsa 	return new_term(term, &temp, str, 0);
330516fa7e82SJiri Olsa }
330616fa7e82SJiri Olsa 
33076cee6cd3SArnaldo Carvalho de Melo int parse_events_term__sym_hw(struct parse_events_term **term,
33081d33d6dcSJiri Olsa 			      char *config, unsigned idx)
33091d33d6dcSJiri Olsa {
33101d33d6dcSJiri Olsa 	struct event_symbol *sym;
3311b6645a72SIan Rogers 	char *str;
331267b49b38SJiri Olsa 	struct parse_events_term temp = {
331367b49b38SJiri Olsa 		.type_val  = PARSE_EVENTS__TERM_TYPE_STR,
331467b49b38SJiri Olsa 		.type_term = PARSE_EVENTS__TERM_TYPE_USER,
3315b6645a72SIan Rogers 		.config    = config,
331667b49b38SJiri Olsa 	};
33171d33d6dcSJiri Olsa 
3318b6645a72SIan Rogers 	if (!temp.config) {
3319b6645a72SIan Rogers 		temp.config = strdup("event");
3320b6645a72SIan Rogers 		if (!temp.config)
3321b6645a72SIan Rogers 			return -ENOMEM;
3322b6645a72SIan Rogers 	}
33231d33d6dcSJiri Olsa 	BUG_ON(idx >= PERF_COUNT_HW_MAX);
33241d33d6dcSJiri Olsa 	sym = &event_symbols_hw[idx];
33251d33d6dcSJiri Olsa 
3326b6645a72SIan Rogers 	str = strdup(sym->symbol);
3327b6645a72SIan Rogers 	if (!str)
3328b6645a72SIan Rogers 		return -ENOMEM;
3329b6645a72SIan Rogers 	return new_term(term, &temp, str, 0);
33301d33d6dcSJiri Olsa }
33311d33d6dcSJiri Olsa 
33326cee6cd3SArnaldo Carvalho de Melo int parse_events_term__clone(struct parse_events_term **new,
33336cee6cd3SArnaldo Carvalho de Melo 			     struct parse_events_term *term)
3334a6146d50SZheng Yan {
3335b6645a72SIan Rogers 	char *str;
333667b49b38SJiri Olsa 	struct parse_events_term temp = {
333767b49b38SJiri Olsa 		.type_val  = term->type_val,
333867b49b38SJiri Olsa 		.type_term = term->type_term,
3339b6645a72SIan Rogers 		.config    = NULL,
334067b49b38SJiri Olsa 		.err_term  = term->err_term,
334167b49b38SJiri Olsa 		.err_val   = term->err_val,
334267b49b38SJiri Olsa 	};
334367b49b38SJiri Olsa 
3344b6645a72SIan Rogers 	if (term->config) {
3345b6645a72SIan Rogers 		temp.config = strdup(term->config);
3346b6645a72SIan Rogers 		if (!temp.config)
3347b6645a72SIan Rogers 			return -ENOMEM;
3348b6645a72SIan Rogers 	}
3349b6645a72SIan Rogers 	if (term->type_val == PARSE_EVENTS__TERM_TYPE_NUM)
3350b6645a72SIan Rogers 		return new_term(new, &temp, NULL, term->val.num);
3351b6645a72SIan Rogers 
3352b6645a72SIan Rogers 	str = strdup(term->val.str);
3353b6645a72SIan Rogers 	if (!str)
3354b6645a72SIan Rogers 		return -ENOMEM;
3355b6645a72SIan Rogers 	return new_term(new, &temp, str, 0);
3356a6146d50SZheng Yan }
3357a6146d50SZheng Yan 
33581dc92556SIan Rogers void parse_events_term__delete(struct parse_events_term *term)
33591dc92556SIan Rogers {
33601dc92556SIan Rogers 	if (term->array.nr_ranges)
33611dc92556SIan Rogers 		zfree(&term->array.ranges);
33621dc92556SIan Rogers 
33631dc92556SIan Rogers 	if (term->type_val != PARSE_EVENTS__TERM_TYPE_NUM)
33641dc92556SIan Rogers 		zfree(&term->val.str);
33651dc92556SIan Rogers 
33661dc92556SIan Rogers 	zfree(&term->config);
33671dc92556SIan Rogers 	free(term);
33681dc92556SIan Rogers }
33691dc92556SIan Rogers 
33708255718fSAndi Kleen int parse_events_copy_term_list(struct list_head *old,
33718255718fSAndi Kleen 				 struct list_head **new)
33728255718fSAndi Kleen {
33738255718fSAndi Kleen 	struct parse_events_term *term, *n;
33748255718fSAndi Kleen 	int ret;
33758255718fSAndi Kleen 
33768255718fSAndi Kleen 	if (!old) {
33778255718fSAndi Kleen 		*new = NULL;
33788255718fSAndi Kleen 		return 0;
33798255718fSAndi Kleen 	}
33808255718fSAndi Kleen 
33818255718fSAndi Kleen 	*new = malloc(sizeof(struct list_head));
33828255718fSAndi Kleen 	if (!*new)
33838255718fSAndi Kleen 		return -ENOMEM;
33848255718fSAndi Kleen 	INIT_LIST_HEAD(*new);
33858255718fSAndi Kleen 
33868255718fSAndi Kleen 	list_for_each_entry (term, old, list) {
33878255718fSAndi Kleen 		ret = parse_events_term__clone(&n, term);
33888255718fSAndi Kleen 		if (ret)
33898255718fSAndi Kleen 			return ret;
33908255718fSAndi Kleen 		list_add_tail(&n->list, *new);
33918255718fSAndi Kleen 	}
33928255718fSAndi Kleen 	return 0;
33938255718fSAndi Kleen }
33948255718fSAndi Kleen 
3395fc0a2c1dSArnaldo Carvalho de Melo void parse_events_terms__purge(struct list_head *terms)
33968f707d84SJiri Olsa {
33976cee6cd3SArnaldo Carvalho de Melo 	struct parse_events_term *term, *h;
33988f707d84SJiri Olsa 
3399a8adfcebSWang Nan 	list_for_each_entry_safe(term, h, terms, list) {
3400a8adfcebSWang Nan 		list_del_init(&term->list);
34011dc92556SIan Rogers 		parse_events_term__delete(term);
34028f707d84SJiri Olsa 	}
3403a8adfcebSWang Nan }
3404b39b8393SJiri Olsa 
34052146afc6SArnaldo Carvalho de Melo void parse_events_terms__delete(struct list_head *terms)
3406fc0a2c1dSArnaldo Carvalho de Melo {
34072146afc6SArnaldo Carvalho de Melo 	if (!terms)
34082146afc6SArnaldo Carvalho de Melo 		return;
3409fc0a2c1dSArnaldo Carvalho de Melo 	parse_events_terms__purge(terms);
3410d20a5f2bSWang Nan 	free(terms);
3411fc0a2c1dSArnaldo Carvalho de Melo }
3412fc0a2c1dSArnaldo Carvalho de Melo 
34132d055bf2SWang Nan void parse_events__clear_array(struct parse_events_array *a)
34142d055bf2SWang Nan {
3415360e071bSTaeung Song 	zfree(&a->ranges);
34162d055bf2SWang Nan }
34172d055bf2SWang Nan 
34185d9cdc11SArnaldo Carvalho de Melo void parse_events_evlist_error(struct parse_events_state *parse_state,
3419b39b8393SJiri Olsa 			       int idx, const char *str)
3420b39b8393SJiri Olsa {
3421448d732cSIan Rogers 	if (!parse_state->error)
3422a6ced2beSAdrian Hunter 		return;
3423448d732cSIan Rogers 
34246c191289SIan Rogers 	parse_events_error__handle(parse_state->error, idx, strdup(str), NULL);
3425b39b8393SJiri Olsa }
3426ffeb883eSHe Kuang 
342717cb5f84SWang Nan static void config_terms_list(char *buf, size_t buf_sz)
342817cb5f84SWang Nan {
342917cb5f84SWang Nan 	int i;
343017cb5f84SWang Nan 	bool first = true;
343117cb5f84SWang Nan 
343217cb5f84SWang Nan 	buf[0] = '\0';
343317cb5f84SWang Nan 	for (i = 0; i < __PARSE_EVENTS__TERM_TYPE_NR; i++) {
343417cb5f84SWang Nan 		const char *name = config_term_names[i];
343517cb5f84SWang Nan 
34361669e509SWang Nan 		if (!config_term_avail(i, NULL))
34371669e509SWang Nan 			continue;
343817cb5f84SWang Nan 		if (!name)
343917cb5f84SWang Nan 			continue;
344017cb5f84SWang Nan 		if (name[0] == '<')
344117cb5f84SWang Nan 			continue;
344217cb5f84SWang Nan 
344317cb5f84SWang Nan 		if (strlen(buf) + strlen(name) + 2 >= buf_sz)
344417cb5f84SWang Nan 			return;
344517cb5f84SWang Nan 
344617cb5f84SWang Nan 		if (!first)
344717cb5f84SWang Nan 			strcat(buf, ",");
344817cb5f84SWang Nan 		else
344917cb5f84SWang Nan 			first = false;
345017cb5f84SWang Nan 		strcat(buf, name);
345117cb5f84SWang Nan 	}
345217cb5f84SWang Nan }
345317cb5f84SWang Nan 
3454ffeb883eSHe Kuang /*
3455ffeb883eSHe Kuang  * Return string contains valid config terms of an event.
3456ffeb883eSHe Kuang  * @additional_terms: For terms such as PMU sysfs terms.
3457ffeb883eSHe Kuang  */
3458ffeb883eSHe Kuang char *parse_events_formats_error_string(char *additional_terms)
3459ffeb883eSHe Kuang {
3460ffeb883eSHe Kuang 	char *str;
3461626a6b78SWang Nan 	/* "no-overwrite" is the longest name */
346217cb5f84SWang Nan 	char static_terms[__PARSE_EVENTS__TERM_TYPE_NR *
3463626a6b78SWang Nan 			  (sizeof("no-overwrite") - 1)];
3464ffeb883eSHe Kuang 
346517cb5f84SWang Nan 	config_terms_list(static_terms, sizeof(static_terms));
3466ffeb883eSHe Kuang 	/* valid terms */
3467ffeb883eSHe Kuang 	if (additional_terms) {
346826dee028SWang Nan 		if (asprintf(&str, "valid terms: %s,%s",
346926dee028SWang Nan 			     additional_terms, static_terms) < 0)
3470ffeb883eSHe Kuang 			goto fail;
3471ffeb883eSHe Kuang 	} else {
347226dee028SWang Nan 		if (asprintf(&str, "valid terms: %s", static_terms) < 0)
3473ffeb883eSHe Kuang 			goto fail;
3474ffeb883eSHe Kuang 	}
3475ffeb883eSHe Kuang 	return str;
3476ffeb883eSHe Kuang 
3477ffeb883eSHe Kuang fail:
3478ffeb883eSHe Kuang 	return NULL;
3479ffeb883eSHe Kuang }
34809cbfa2f6SJin Yao 
34819cbfa2f6SJin Yao struct evsel *parse_events__add_event_hybrid(struct list_head *list, int *idx,
34829cbfa2f6SJin Yao 					     struct perf_event_attr *attr,
34832b62b3a6SIan Rogers 					     const char *name,
34842b62b3a6SIan Rogers 					     const char *metric_id,
34852b62b3a6SIan Rogers 					     struct perf_pmu *pmu,
34869cbfa2f6SJin Yao 					     struct list_head *config_terms)
34879cbfa2f6SJin Yao {
34882b62b3a6SIan Rogers 	return __add_event(list, idx, attr, /*init_attr=*/true, name, metric_id,
34892b62b3a6SIan Rogers 			   pmu, config_terms, /*auto_merge_stats=*/false,
34902b62b3a6SIan Rogers 			   /*cpu_list=*/NULL);
34919cbfa2f6SJin Yao }
3492