xref: /linux/tools/perf/util/parse-events.c (revision d4953f7ef1a2e87ef732823af35361404d13fea8)
1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
2d2709c7cSDavid Howells #include <linux/hw_breakpoint.h>
38dd2a131SJiri Olsa #include <linux/err.h>
47f7c536fSArnaldo Carvalho de Melo #include <linux/zalloc.h>
576b31a29SArnaldo Carvalho de Melo #include <dirent.h>
6a43783aeSArnaldo Carvalho de Melo #include <errno.h>
786a5e0c2SArnaldo Carvalho de Melo #include <sys/ioctl.h>
8bafae98eSArnaldo Carvalho de Melo #include <sys/types.h>
9bafae98eSArnaldo Carvalho de Melo #include <sys/stat.h>
10bafae98eSArnaldo Carvalho de Melo #include <fcntl.h>
11391e4206SArnaldo Carvalho de Melo #include <sys/param.h>
12b0742e90SArnaldo Carvalho de Melo #include "term.h"
134a3cec84SArnaldo Carvalho de Melo #include "build-id.h"
14361c99a6SArnaldo Carvalho de Melo #include "evlist.h"
1569aad6f1SArnaldo Carvalho de Melo #include "evsel.h"
16fa0d9846SArnaldo Carvalho de Melo #include <subcmd/pager.h>
174b6ab94eSJosh Poimboeuf #include <subcmd/parse-options.h>
1886470930SIngo Molnar #include "parse-events.h"
194b6ab94eSJosh Poimboeuf #include <subcmd/exec-cmd.h>
20a067558eSArnaldo Carvalho de Melo #include "string2.h"
218ec20b17SArnaldo Carvalho de Melo #include "strlist.h"
225aab621bSArnaldo Carvalho de Melo #include "symbol.h"
238755a8f2SArjan van de Ven #include "header.h"
2484c86ca1SWang Nan #include "bpf-loader.h"
256e81c74cSMasami Hiramatsu #include "debug.h"
26592d5a6bSJiri Olsa #include <api/fs/tracing_path.h>
279c3516d1SJiri Olsa #include <perf/cpumap.h>
28ac20de6fSZheng Yan #include "parse-events-bison.h"
2990e2b22dSJiri Olsa #define YY_EXTRA_TYPE int
3089812fc8SJiri Olsa #include "parse-events-flex.h"
315f537a26SJiri Olsa #include "pmu.h"
32b41f1cecSNamhyung Kim #include "thread_map.h"
3340218daeSMasami Hiramatsu #include "probe-file.h"
34b39b8393SJiri Olsa #include "asm/bug.h"
35ac12f676SAndi Kleen #include "util/parse-branch-options.h"
3671b0acceSAndi Kleen #include "metricgroup.h"
3795be9d19SArnaldo Carvalho de Melo #include "util/evsel_config.h"
38252a2fdcSArnaldo Carvalho de Melo #include "util/event.h"
3989812fc8SJiri Olsa 
4089812fc8SJiri Olsa #define MAX_NAME_LEN 100
4186470930SIngo Molnar 
4282ba1f2fSJiri Olsa #ifdef PARSER_DEBUG
4382ba1f2fSJiri Olsa extern int parse_events_debug;
4482ba1f2fSJiri Olsa #endif
455d9cdc11SArnaldo Carvalho de Melo int parse_events_parse(void *parse_state, void *scanner);
46e637d177SHe Kuang static int get_config_terms(struct list_head *head_config,
47e637d177SHe Kuang 			    struct list_head *head_terms __maybe_unused);
48bcd3279fSFrederic Weisbecker 
49dcb4e102SKan Liang static struct perf_pmu_event_symbol *perf_pmu_events_list;
50dcb4e102SKan Liang /*
51dcb4e102SKan Liang  * The variable indicates the number of supported pmu event symbols.
52dcb4e102SKan Liang  * 0 means not initialized and ready to init
53dcb4e102SKan Liang  * -1 means failed to init, don't try anymore
54dcb4e102SKan Liang  * >0 is the number of supported pmu event symbols
55dcb4e102SKan Liang  */
56dcb4e102SKan Liang static int perf_pmu_events_list_num;
57dcb4e102SKan Liang 
58705750f2SYunlong Song struct event_symbol event_symbols_hw[PERF_COUNT_HW_MAX] = {
591dc12760SJiri Olsa 	[PERF_COUNT_HW_CPU_CYCLES] = {
601dc12760SJiri Olsa 		.symbol = "cpu-cycles",
611dc12760SJiri Olsa 		.alias  = "cycles",
621dc12760SJiri Olsa 	},
631dc12760SJiri Olsa 	[PERF_COUNT_HW_INSTRUCTIONS] = {
641dc12760SJiri Olsa 		.symbol = "instructions",
651dc12760SJiri Olsa 		.alias  = "",
661dc12760SJiri Olsa 	},
671dc12760SJiri Olsa 	[PERF_COUNT_HW_CACHE_REFERENCES] = {
681dc12760SJiri Olsa 		.symbol = "cache-references",
691dc12760SJiri Olsa 		.alias  = "",
701dc12760SJiri Olsa 	},
711dc12760SJiri Olsa 	[PERF_COUNT_HW_CACHE_MISSES] = {
721dc12760SJiri Olsa 		.symbol = "cache-misses",
731dc12760SJiri Olsa 		.alias  = "",
741dc12760SJiri Olsa 	},
751dc12760SJiri Olsa 	[PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = {
761dc12760SJiri Olsa 		.symbol = "branch-instructions",
771dc12760SJiri Olsa 		.alias  = "branches",
781dc12760SJiri Olsa 	},
791dc12760SJiri Olsa 	[PERF_COUNT_HW_BRANCH_MISSES] = {
801dc12760SJiri Olsa 		.symbol = "branch-misses",
811dc12760SJiri Olsa 		.alias  = "",
821dc12760SJiri Olsa 	},
831dc12760SJiri Olsa 	[PERF_COUNT_HW_BUS_CYCLES] = {
841dc12760SJiri Olsa 		.symbol = "bus-cycles",
851dc12760SJiri Olsa 		.alias  = "",
861dc12760SJiri Olsa 	},
871dc12760SJiri Olsa 	[PERF_COUNT_HW_STALLED_CYCLES_FRONTEND] = {
881dc12760SJiri Olsa 		.symbol = "stalled-cycles-frontend",
891dc12760SJiri Olsa 		.alias  = "idle-cycles-frontend",
901dc12760SJiri Olsa 	},
911dc12760SJiri Olsa 	[PERF_COUNT_HW_STALLED_CYCLES_BACKEND] = {
921dc12760SJiri Olsa 		.symbol = "stalled-cycles-backend",
931dc12760SJiri Olsa 		.alias  = "idle-cycles-backend",
941dc12760SJiri Olsa 	},
951dc12760SJiri Olsa 	[PERF_COUNT_HW_REF_CPU_CYCLES] = {
961dc12760SJiri Olsa 		.symbol = "ref-cycles",
971dc12760SJiri Olsa 		.alias  = "",
981dc12760SJiri Olsa 	},
991dc12760SJiri Olsa };
10086470930SIngo Molnar 
101705750f2SYunlong Song struct event_symbol event_symbols_sw[PERF_COUNT_SW_MAX] = {
1021dc12760SJiri Olsa 	[PERF_COUNT_SW_CPU_CLOCK] = {
1031dc12760SJiri Olsa 		.symbol = "cpu-clock",
1041dc12760SJiri Olsa 		.alias  = "",
1051dc12760SJiri Olsa 	},
1061dc12760SJiri Olsa 	[PERF_COUNT_SW_TASK_CLOCK] = {
1071dc12760SJiri Olsa 		.symbol = "task-clock",
1081dc12760SJiri Olsa 		.alias  = "",
1091dc12760SJiri Olsa 	},
1101dc12760SJiri Olsa 	[PERF_COUNT_SW_PAGE_FAULTS] = {
1111dc12760SJiri Olsa 		.symbol = "page-faults",
1121dc12760SJiri Olsa 		.alias  = "faults",
1131dc12760SJiri Olsa 	},
1141dc12760SJiri Olsa 	[PERF_COUNT_SW_CONTEXT_SWITCHES] = {
1151dc12760SJiri Olsa 		.symbol = "context-switches",
1161dc12760SJiri Olsa 		.alias  = "cs",
1171dc12760SJiri Olsa 	},
1181dc12760SJiri Olsa 	[PERF_COUNT_SW_CPU_MIGRATIONS] = {
1191dc12760SJiri Olsa 		.symbol = "cpu-migrations",
1201dc12760SJiri Olsa 		.alias  = "migrations",
1211dc12760SJiri Olsa 	},
1221dc12760SJiri Olsa 	[PERF_COUNT_SW_PAGE_FAULTS_MIN] = {
1231dc12760SJiri Olsa 		.symbol = "minor-faults",
1241dc12760SJiri Olsa 		.alias  = "",
1251dc12760SJiri Olsa 	},
1261dc12760SJiri Olsa 	[PERF_COUNT_SW_PAGE_FAULTS_MAJ] = {
1271dc12760SJiri Olsa 		.symbol = "major-faults",
1281dc12760SJiri Olsa 		.alias  = "",
1291dc12760SJiri Olsa 	},
1301dc12760SJiri Olsa 	[PERF_COUNT_SW_ALIGNMENT_FAULTS] = {
1311dc12760SJiri Olsa 		.symbol = "alignment-faults",
1321dc12760SJiri Olsa 		.alias  = "",
1331dc12760SJiri Olsa 	},
1341dc12760SJiri Olsa 	[PERF_COUNT_SW_EMULATION_FAULTS] = {
1351dc12760SJiri Olsa 		.symbol = "emulation-faults",
1361dc12760SJiri Olsa 		.alias  = "",
1371dc12760SJiri Olsa 	},
138d22d1a2aSAdrian Hunter 	[PERF_COUNT_SW_DUMMY] = {
139d22d1a2aSAdrian Hunter 		.symbol = "dummy",
140d22d1a2aSAdrian Hunter 		.alias  = "",
141d22d1a2aSAdrian Hunter 	},
142bae9cc41SArnaldo Carvalho de Melo 	[PERF_COUNT_SW_BPF_OUTPUT] = {
143bae9cc41SArnaldo Carvalho de Melo 		.symbol = "bpf-output",
144bae9cc41SArnaldo Carvalho de Melo 		.alias  = "",
145bae9cc41SArnaldo Carvalho de Melo 	},
14686470930SIngo Molnar };
14786470930SIngo Molnar 
148cdd6c482SIngo Molnar #define __PERF_EVENT_FIELD(config, name) \
149cdd6c482SIngo Molnar 	((config & PERF_EVENT_##name##_MASK) >> PERF_EVENT_##name##_SHIFT)
15086470930SIngo Molnar 
151cdd6c482SIngo Molnar #define PERF_EVENT_RAW(config)		__PERF_EVENT_FIELD(config, RAW)
152cdd6c482SIngo Molnar #define PERF_EVENT_CONFIG(config)	__PERF_EVENT_FIELD(config, CONFIG)
153cdd6c482SIngo Molnar #define PERF_EVENT_TYPE(config)		__PERF_EVENT_FIELD(config, TYPE)
154cdd6c482SIngo Molnar #define PERF_EVENT_ID(config)		__PERF_EVENT_FIELD(config, EVENT)
15586470930SIngo Molnar 
15622a9f41bSArnaldo Carvalho de Melo #define for_each_subsystem(sys_dir, sys_dirent)			\
15722a9f41bSArnaldo Carvalho de Melo 	while ((sys_dirent = readdir(sys_dir)) != NULL)		\
15822a9f41bSArnaldo Carvalho de Melo 		if (sys_dirent->d_type == DT_DIR &&		\
15922a9f41bSArnaldo Carvalho de Melo 		    (strcmp(sys_dirent->d_name, ".")) &&	\
16022a9f41bSArnaldo Carvalho de Melo 		    (strcmp(sys_dirent->d_name, "..")))
161f6bdafefSJason Baron 
162c02cab22SArnaldo Carvalho de Melo static int tp_event_has_id(const char *dir_path, struct dirent *evt_dir)
163ae07b63fSPeter Zijlstra {
164ae07b63fSPeter Zijlstra 	char evt_path[MAXPATHLEN];
165ae07b63fSPeter Zijlstra 	int fd;
166ae07b63fSPeter Zijlstra 
167c02cab22SArnaldo Carvalho de Melo 	snprintf(evt_path, MAXPATHLEN, "%s/%s/id", dir_path, evt_dir->d_name);
168ae07b63fSPeter Zijlstra 	fd = open(evt_path, O_RDONLY);
169ae07b63fSPeter Zijlstra 	if (fd < 0)
170ae07b63fSPeter Zijlstra 		return -EINVAL;
171ae07b63fSPeter Zijlstra 	close(fd);
172ae07b63fSPeter Zijlstra 
173ae07b63fSPeter Zijlstra 	return 0;
174ae07b63fSPeter Zijlstra }
175ae07b63fSPeter Zijlstra 
176c02cab22SArnaldo Carvalho de Melo #define for_each_event(dir_path, evt_dir, evt_dirent)		\
17722a9f41bSArnaldo Carvalho de Melo 	while ((evt_dirent = readdir(evt_dir)) != NULL)		\
17822a9f41bSArnaldo Carvalho de Melo 		if (evt_dirent->d_type == DT_DIR &&		\
17922a9f41bSArnaldo Carvalho de Melo 		    (strcmp(evt_dirent->d_name, ".")) &&	\
18022a9f41bSArnaldo Carvalho de Melo 		    (strcmp(evt_dirent->d_name, "..")) &&	\
181c02cab22SArnaldo Carvalho de Melo 		    (!tp_event_has_id(dir_path, evt_dirent)))
182f6bdafefSJason Baron 
183270bbbe8SLi Zefan #define MAX_EVENT_LENGTH 512
184f6bdafefSJason Baron 
185448d732cSIan Rogers void parse_events__handle_error(struct parse_events_error *err, int idx,
186448d732cSIan Rogers 				char *str, char *help)
187448d732cSIan Rogers {
188448d732cSIan Rogers 	if (WARN(!str, "WARNING: failed to provide error string\n")) {
189448d732cSIan Rogers 		free(help);
190448d732cSIan Rogers 		return;
191448d732cSIan Rogers 	}
192a910e466SIan Rogers 	switch (err->num_errors) {
193a910e466SIan Rogers 	case 0:
194448d732cSIan Rogers 		err->idx = idx;
195a910e466SIan Rogers 		err->str = str;
196a910e466SIan Rogers 		err->help = help;
197a910e466SIan Rogers 		break;
198a910e466SIan Rogers 	case 1:
199a910e466SIan Rogers 		err->first_idx = err->idx;
200a910e466SIan Rogers 		err->idx = idx;
201a910e466SIan Rogers 		err->first_str = err->str;
202a910e466SIan Rogers 		err->str = str;
203a910e466SIan Rogers 		err->first_help = err->help;
204a910e466SIan Rogers 		err->help = help;
205a910e466SIan Rogers 		break;
206a910e466SIan Rogers 	default:
207a910e466SIan Rogers 		WARN_ONCE(1, "WARNING: multiple event parsing errors\n");
208448d732cSIan Rogers 		free(err->str);
209448d732cSIan Rogers 		err->str = str;
210448d732cSIan Rogers 		free(err->help);
211448d732cSIan Rogers 		err->help = help;
212a910e466SIan Rogers 		break;
213a910e466SIan Rogers 	}
214a910e466SIan Rogers 	err->num_errors++;
215448d732cSIan Rogers }
216f6bdafefSJason Baron 
2171ef2ed10SFrederic Weisbecker struct tracepoint_path *tracepoint_id_to_path(u64 config)
218f6bdafefSJason Baron {
2191ef2ed10SFrederic Weisbecker 	struct tracepoint_path *path = NULL;
220f6bdafefSJason Baron 	DIR *sys_dir, *evt_dir;
22122a9f41bSArnaldo Carvalho de Melo 	struct dirent *sys_dirent, *evt_dirent;
2228aa8a7c8SStephane Eranian 	char id_buf[24];
223725b1368SEric Dumazet 	int fd;
224f6bdafefSJason Baron 	u64 id;
225f6bdafefSJason Baron 	char evt_path[MAXPATHLEN];
22625a7d914SArnaldo Carvalho de Melo 	char *dir_path;
227f6bdafefSJason Baron 
2287014e0e3SArnaldo Carvalho de Melo 	sys_dir = tracing_events__opendir();
229f6bdafefSJason Baron 	if (!sys_dir)
230725b1368SEric Dumazet 		return NULL;
231f6bdafefSJason Baron 
23222a9f41bSArnaldo Carvalho de Melo 	for_each_subsystem(sys_dir, sys_dirent) {
23325a7d914SArnaldo Carvalho de Melo 		dir_path = get_events_file(sys_dirent->d_name);
23425a7d914SArnaldo Carvalho de Melo 		if (!dir_path)
23525a7d914SArnaldo Carvalho de Melo 			continue;
236725b1368SEric Dumazet 		evt_dir = opendir(dir_path);
237725b1368SEric Dumazet 		if (!evt_dir)
23825a7d914SArnaldo Carvalho de Melo 			goto next;
239725b1368SEric Dumazet 
240c02cab22SArnaldo Carvalho de Melo 		for_each_event(dir_path, evt_dir, evt_dirent) {
241725b1368SEric Dumazet 
24277f18153SJiri Olsa 			scnprintf(evt_path, MAXPATHLEN, "%s/%s/id", dir_path,
24322a9f41bSArnaldo Carvalho de Melo 				  evt_dirent->d_name);
244725b1368SEric Dumazet 			fd = open(evt_path, O_RDONLY);
245f6bdafefSJason Baron 			if (fd < 0)
246f6bdafefSJason Baron 				continue;
247f6bdafefSJason Baron 			if (read(fd, id_buf, sizeof(id_buf)) < 0) {
248f6bdafefSJason Baron 				close(fd);
249f6bdafefSJason Baron 				continue;
250f6bdafefSJason Baron 			}
251f6bdafefSJason Baron 			close(fd);
252f6bdafefSJason Baron 			id = atoll(id_buf);
253f6bdafefSJason Baron 			if (id == config) {
25425a7d914SArnaldo Carvalho de Melo 				put_events_file(dir_path);
255f6bdafefSJason Baron 				closedir(evt_dir);
256f6bdafefSJason Baron 				closedir(sys_dir);
25759b4caebSJulia Lawall 				path = zalloc(sizeof(*path));
2585aa365f2STaeung Song 				if (!path)
2595aa365f2STaeung Song 					return NULL;
2607125f204SArnaldo Carvalho de Melo 				if (asprintf(&path->system, "%.*s", MAX_EVENT_LENGTH, sys_dirent->d_name) < 0) {
2611ef2ed10SFrederic Weisbecker 					free(path);
2621ef2ed10SFrederic Weisbecker 					return NULL;
2631ef2ed10SFrederic Weisbecker 				}
2647125f204SArnaldo Carvalho de Melo 				if (asprintf(&path->name, "%.*s", MAX_EVENT_LENGTH, evt_dirent->d_name) < 0) {
26574cf249dSArnaldo Carvalho de Melo 					zfree(&path->system);
2661ef2ed10SFrederic Weisbecker 					free(path);
2671ef2ed10SFrederic Weisbecker 					return NULL;
2681ef2ed10SFrederic Weisbecker 				}
2691ef2ed10SFrederic Weisbecker 				return path;
270f6bdafefSJason Baron 			}
271f6bdafefSJason Baron 		}
272f6bdafefSJason Baron 		closedir(evt_dir);
27325a7d914SArnaldo Carvalho de Melo next:
27425a7d914SArnaldo Carvalho de Melo 		put_events_file(dir_path);
275f6bdafefSJason Baron 	}
276f6bdafefSJason Baron 
277f6bdafefSJason Baron 	closedir(sys_dir);
2781ef2ed10SFrederic Weisbecker 	return NULL;
2791ef2ed10SFrederic Weisbecker }
2801ef2ed10SFrederic Weisbecker 
281e7c93f09SNamhyung Kim struct tracepoint_path *tracepoint_name_to_path(const char *name)
282e7c93f09SNamhyung Kim {
283e7c93f09SNamhyung Kim 	struct tracepoint_path *path = zalloc(sizeof(*path));
284e7c93f09SNamhyung Kim 	char *str = strchr(name, ':');
285e7c93f09SNamhyung Kim 
286e7c93f09SNamhyung Kim 	if (path == NULL || str == NULL) {
287e7c93f09SNamhyung Kim 		free(path);
288e7c93f09SNamhyung Kim 		return NULL;
289e7c93f09SNamhyung Kim 	}
290e7c93f09SNamhyung Kim 
291e7c93f09SNamhyung Kim 	path->system = strndup(name, str - name);
292e7c93f09SNamhyung Kim 	path->name = strdup(str+1);
293e7c93f09SNamhyung Kim 
294e7c93f09SNamhyung Kim 	if (path->system == NULL || path->name == NULL) {
29574cf249dSArnaldo Carvalho de Melo 		zfree(&path->system);
29674cf249dSArnaldo Carvalho de Melo 		zfree(&path->name);
297506fde11STaeung Song 		zfree(&path);
298e7c93f09SNamhyung Kim 	}
299e7c93f09SNamhyung Kim 
300e7c93f09SNamhyung Kim 	return path;
301e7c93f09SNamhyung Kim }
302e7c93f09SNamhyung Kim 
3031424dc96SDavid Ahern const char *event_type(int type)
3041424dc96SDavid Ahern {
3051424dc96SDavid Ahern 	switch (type) {
3061424dc96SDavid Ahern 	case PERF_TYPE_HARDWARE:
3071424dc96SDavid Ahern 		return "hardware";
3081424dc96SDavid Ahern 
3091424dc96SDavid Ahern 	case PERF_TYPE_SOFTWARE:
3101424dc96SDavid Ahern 		return "software";
3111424dc96SDavid Ahern 
3121424dc96SDavid Ahern 	case PERF_TYPE_TRACEPOINT:
3131424dc96SDavid Ahern 		return "tracepoint";
3141424dc96SDavid Ahern 
3151424dc96SDavid Ahern 	case PERF_TYPE_HW_CACHE:
3161424dc96SDavid Ahern 		return "hardware-cache";
3171424dc96SDavid Ahern 
3181424dc96SDavid Ahern 	default:
3191424dc96SDavid Ahern 		break;
3201424dc96SDavid Ahern 	}
3211424dc96SDavid Ahern 
3221424dc96SDavid Ahern 	return "unknown";
3231424dc96SDavid Ahern }
3241424dc96SDavid Ahern 
325e814fdddSWang Nan static int parse_events__is_name_term(struct parse_events_term *term)
326e814fdddSWang Nan {
327e814fdddSWang Nan 	return term->type_term == PARSE_EVENTS__TERM_TYPE_NAME;
328e814fdddSWang Nan }
3297ae92e74SYan, Zheng 
330e814fdddSWang Nan static char *get_config_name(struct list_head *head_terms)
331e814fdddSWang Nan {
332e814fdddSWang Nan 	struct parse_events_term *term;
333e814fdddSWang Nan 
334e814fdddSWang Nan 	if (!head_terms)
335e814fdddSWang Nan 		return NULL;
336e814fdddSWang Nan 
337e814fdddSWang Nan 	list_for_each_entry(term, head_terms, list)
338e814fdddSWang Nan 		if (parse_events__is_name_term(term))
339e814fdddSWang Nan 			return term->val.str;
340e814fdddSWang Nan 
341e814fdddSWang Nan 	return NULL;
342e814fdddSWang Nan }
3437ae92e74SYan, Zheng 
34432dcd021SJiri Olsa static struct evsel *
345410136f5SStephane Eranian __add_event(struct list_head *list, int *idx,
3467ae92e74SYan, Zheng 	    struct perf_event_attr *attr,
34766ec1191SMark Rutland 	    char *name, struct perf_pmu *pmu,
348f0fbb114SAndi Kleen 	    struct list_head *config_terms, bool auto_merge_stats,
349f0fbb114SAndi Kleen 	    const char *cpu_list)
35089812fc8SJiri Olsa {
35132dcd021SJiri Olsa 	struct evsel *evsel;
352f854839bSJiri Olsa 	struct perf_cpu_map *cpus = pmu ? pmu->cpus :
3539c3516d1SJiri Olsa 			       cpu_list ? perf_cpu_map__new(cpu_list) : NULL;
35489812fc8SJiri Olsa 
35589812fc8SJiri Olsa 	event_attr_init(attr);
35689812fc8SJiri Olsa 
35775fc5ae5STaeung Song 	evsel = perf_evsel__new_idx(attr, *idx);
358c5cd8ac0SDavid Ahern 	if (!evsel)
359410136f5SStephane Eranian 		return NULL;
36089812fc8SJiri Olsa 
36175fc5ae5STaeung Song 	(*idx)++;
362d400bd3aSJiri Olsa 	evsel->core.cpus   = perf_cpu_map__get(cpus);
363fe1f61b3SJiri Olsa 	evsel->core.own_cpus = perf_cpu_map__get(cpus);
364648b5af3SJiri Olsa 	evsel->core.system_wide = pmu ? pmu->is_uncore : false;
36563ce8449SArnaldo Carvalho de Melo 	evsel->auto_merge_stats = auto_merge_stats;
366f30a79b0SJiri Olsa 
3679db1763cSArnaldo Carvalho de Melo 	if (name)
36889812fc8SJiri Olsa 		evsel->name = strdup(name);
369930a2e29SJiri Olsa 
370930a2e29SJiri Olsa 	if (config_terms)
371930a2e29SJiri Olsa 		list_splice(config_terms, &evsel->config_terms);
372930a2e29SJiri Olsa 
373b27c4eceSJiri Olsa 	list_add_tail(&evsel->core.node, list);
374410136f5SStephane Eranian 	return evsel;
37589812fc8SJiri Olsa }
37689812fc8SJiri Olsa 
377c5cd8ac0SDavid Ahern static int add_event(struct list_head *list, int *idx,
378930a2e29SJiri Olsa 		     struct perf_event_attr *attr, char *name,
379930a2e29SJiri Olsa 		     struct list_head *config_terms)
3807ae92e74SYan, Zheng {
381f0fbb114SAndi Kleen 	return __add_event(list, idx, attr, name, NULL, config_terms, false, NULL) ? 0 : -ENOMEM;
382f0fbb114SAndi Kleen }
383f0fbb114SAndi Kleen 
384f0fbb114SAndi Kleen static int add_event_tool(struct list_head *list, int *idx,
385f0fbb114SAndi Kleen 			  enum perf_tool_event tool_event)
386f0fbb114SAndi Kleen {
38732dcd021SJiri Olsa 	struct evsel *evsel;
388f0fbb114SAndi Kleen 	struct perf_event_attr attr = {
389f0fbb114SAndi Kleen 		.type = PERF_TYPE_SOFTWARE,
390f0fbb114SAndi Kleen 		.config = PERF_COUNT_SW_DUMMY,
391f0fbb114SAndi Kleen 	};
392f0fbb114SAndi Kleen 
393f0fbb114SAndi Kleen 	evsel = __add_event(list, idx, &attr, NULL, NULL, NULL, false, "0");
394f0fbb114SAndi Kleen 	if (!evsel)
395f0fbb114SAndi Kleen 		return -ENOMEM;
396f0fbb114SAndi Kleen 	evsel->tool_event = tool_event;
397f0fbb114SAndi Kleen 	if (tool_event == PERF_TOOL_DURATION_TIME)
398f0fbb114SAndi Kleen 		evsel->unit = strdup("ns");
399f0fbb114SAndi Kleen 	return 0;
4007ae92e74SYan, Zheng }
4017ae92e74SYan, Zheng 
4020b668bc9SArnaldo Carvalho de Melo static int parse_aliases(char *str, const char *names[][PERF_EVSEL__MAX_ALIASES], int size)
40386470930SIngo Molnar {
40486470930SIngo Molnar 	int i, j;
40561c45981SPaul Mackerras 	int n, longest = -1;
40686470930SIngo Molnar 
40786470930SIngo Molnar 	for (i = 0; i < size; i++) {
4080b668bc9SArnaldo Carvalho de Melo 		for (j = 0; j < PERF_EVSEL__MAX_ALIASES && names[i][j]; j++) {
40961c45981SPaul Mackerras 			n = strlen(names[i][j]);
41089812fc8SJiri Olsa 			if (n > longest && !strncasecmp(str, names[i][j], n))
41161c45981SPaul Mackerras 				longest = n;
41261c45981SPaul Mackerras 		}
41389812fc8SJiri Olsa 		if (longest > 0)
41486470930SIngo Molnar 			return i;
41586470930SIngo Molnar 	}
41686470930SIngo Molnar 
4178953645fSIngo Molnar 	return -1;
41886470930SIngo Molnar }
41986470930SIngo Molnar 
42043d0b978SWang Nan typedef int config_term_func_t(struct perf_event_attr *attr,
42143d0b978SWang Nan 			       struct parse_events_term *term,
42243d0b978SWang Nan 			       struct parse_events_error *err);
42343d0b978SWang Nan static int config_term_common(struct perf_event_attr *attr,
42443d0b978SWang Nan 			      struct parse_events_term *term,
42543d0b978SWang Nan 			      struct parse_events_error *err);
42643d0b978SWang Nan static int config_attr(struct perf_event_attr *attr,
42743d0b978SWang Nan 		       struct list_head *head,
42843d0b978SWang Nan 		       struct parse_events_error *err,
42943d0b978SWang Nan 		       config_term_func_t config_term);
43043d0b978SWang Nan 
431c5cd8ac0SDavid Ahern int parse_events_add_cache(struct list_head *list, int *idx,
43243d0b978SWang Nan 			   char *type, char *op_result1, char *op_result2,
4332c97b0d4SArnaldo Carvalho de Melo 			   struct parse_events_error *err,
43443d0b978SWang Nan 			   struct list_head *head_config)
43586470930SIngo Molnar {
43689812fc8SJiri Olsa 	struct perf_event_attr attr;
43743d0b978SWang Nan 	LIST_HEAD(config_terms);
43843d0b978SWang Nan 	char name[MAX_NAME_LEN], *config_name;
43961c45981SPaul Mackerras 	int cache_type = -1, cache_op = -1, cache_result = -1;
44089812fc8SJiri Olsa 	char *op_result[2] = { op_result1, op_result2 };
44189812fc8SJiri Olsa 	int i, n;
44286470930SIngo Molnar 
44386470930SIngo Molnar 	/*
44486470930SIngo Molnar 	 * No fallback - if we cannot get a clear cache type
44586470930SIngo Molnar 	 * then bail out:
44686470930SIngo Molnar 	 */
4470b668bc9SArnaldo Carvalho de Melo 	cache_type = parse_aliases(type, perf_evsel__hw_cache,
44889812fc8SJiri Olsa 				   PERF_COUNT_HW_CACHE_MAX);
44986470930SIngo Molnar 	if (cache_type == -1)
45089812fc8SJiri Olsa 		return -EINVAL;
45186470930SIngo Molnar 
45243d0b978SWang Nan 	config_name = get_config_name(head_config);
45389812fc8SJiri Olsa 	n = snprintf(name, MAX_NAME_LEN, "%s", type);
45489812fc8SJiri Olsa 
45589812fc8SJiri Olsa 	for (i = 0; (i < 2) && (op_result[i]); i++) {
45689812fc8SJiri Olsa 		char *str = op_result[i];
45789812fc8SJiri Olsa 
458275ef387SJiri Olsa 		n += snprintf(name + n, MAX_NAME_LEN - n, "-%s", str);
45961c45981SPaul Mackerras 
46061c45981SPaul Mackerras 		if (cache_op == -1) {
4610b668bc9SArnaldo Carvalho de Melo 			cache_op = parse_aliases(str, perf_evsel__hw_cache_op,
46261c45981SPaul Mackerras 						 PERF_COUNT_HW_CACHE_OP_MAX);
46361c45981SPaul Mackerras 			if (cache_op >= 0) {
4640b668bc9SArnaldo Carvalho de Melo 				if (!perf_evsel__is_cache_op_valid(cache_type, cache_op))
46589812fc8SJiri Olsa 					return -EINVAL;
46661c45981SPaul Mackerras 				continue;
46761c45981SPaul Mackerras 			}
46861c45981SPaul Mackerras 		}
46961c45981SPaul Mackerras 
47061c45981SPaul Mackerras 		if (cache_result == -1) {
4710b668bc9SArnaldo Carvalho de Melo 			cache_result = parse_aliases(str, perf_evsel__hw_cache_result,
47261c45981SPaul Mackerras 						     PERF_COUNT_HW_CACHE_RESULT_MAX);
47361c45981SPaul Mackerras 			if (cache_result >= 0)
47461c45981SPaul Mackerras 				continue;
47561c45981SPaul Mackerras 		}
47661c45981SPaul Mackerras 	}
47761c45981SPaul Mackerras 
47886470930SIngo Molnar 	/*
47986470930SIngo Molnar 	 * Fall back to reads:
48086470930SIngo Molnar 	 */
4818953645fSIngo Molnar 	if (cache_op == -1)
4828953645fSIngo Molnar 		cache_op = PERF_COUNT_HW_CACHE_OP_READ;
48386470930SIngo Molnar 
48486470930SIngo Molnar 	/*
48586470930SIngo Molnar 	 * Fall back to accesses:
48686470930SIngo Molnar 	 */
48786470930SIngo Molnar 	if (cache_result == -1)
48886470930SIngo Molnar 		cache_result = PERF_COUNT_HW_CACHE_RESULT_ACCESS;
48986470930SIngo Molnar 
49089812fc8SJiri Olsa 	memset(&attr, 0, sizeof(attr));
49189812fc8SJiri Olsa 	attr.config = cache_type | (cache_op << 8) | (cache_result << 16);
49289812fc8SJiri Olsa 	attr.type = PERF_TYPE_HW_CACHE;
49343d0b978SWang Nan 
49443d0b978SWang Nan 	if (head_config) {
4952c97b0d4SArnaldo Carvalho de Melo 		if (config_attr(&attr, head_config, err,
49643d0b978SWang Nan 				config_term_common))
49743d0b978SWang Nan 			return -EINVAL;
49843d0b978SWang Nan 
49943d0b978SWang Nan 		if (get_config_terms(head_config, &config_terms))
50043d0b978SWang Nan 			return -ENOMEM;
50143d0b978SWang Nan 	}
50243d0b978SWang Nan 	return add_event(list, idx, &attr, config_name ? : name, &config_terms);
50386470930SIngo Molnar }
50486470930SIngo Molnar 
505272ed29aSJiri Olsa static void tracepoint_error(struct parse_events_error *e, int err,
5068c619d6aSWang Nan 			     const char *sys, const char *name)
50719658171SJiri Olsa {
5084584f084SIan Rogers 	const char *str;
50919658171SJiri Olsa 	char help[BUFSIZ];
51019658171SJiri Olsa 
511ec183d22SAdrian Hunter 	if (!e)
512ec183d22SAdrian Hunter 		return;
513ec183d22SAdrian Hunter 
51419658171SJiri Olsa 	/*
51519658171SJiri Olsa 	 * We get error directly from syscall errno ( > 0),
51619658171SJiri Olsa 	 * or from encoded pointer's error ( < 0).
51719658171SJiri Olsa 	 */
51819658171SJiri Olsa 	err = abs(err);
51919658171SJiri Olsa 
52019658171SJiri Olsa 	switch (err) {
52119658171SJiri Olsa 	case EACCES:
5224584f084SIan Rogers 		str = "can't access trace events";
52319658171SJiri Olsa 		break;
52419658171SJiri Olsa 	case ENOENT:
5254584f084SIan Rogers 		str = "unknown tracepoint";
52619658171SJiri Olsa 		break;
52719658171SJiri Olsa 	default:
5284584f084SIan Rogers 		str = "failed to add tracepoint";
52919658171SJiri Olsa 		break;
53019658171SJiri Olsa 	}
53119658171SJiri Olsa 
53219658171SJiri Olsa 	tracing_path__strerror_open_tp(err, help, sizeof(help), sys, name);
5334584f084SIan Rogers 	parse_events__handle_error(e, 0, strdup(str), strdup(help));
53419658171SJiri Olsa }
53519658171SJiri Olsa 
536c5cd8ac0SDavid Ahern static int add_tracepoint(struct list_head *list, int *idx,
5378c619d6aSWang Nan 			  const char *sys_name, const char *evt_name,
538272ed29aSJiri Olsa 			  struct parse_events_error *err,
539e637d177SHe Kuang 			  struct list_head *head_config)
540bcd3279fSFrederic Weisbecker {
54132dcd021SJiri Olsa 	struct evsel *evsel;
542bcd3279fSFrederic Weisbecker 
543ef503831SArnaldo Carvalho de Melo 	evsel = perf_evsel__newtp_idx(sys_name, evt_name, (*idx)++);
54419658171SJiri Olsa 	if (IS_ERR(evsel)) {
545272ed29aSJiri Olsa 		tracepoint_error(err, PTR_ERR(evsel), sys_name, evt_name);
5468dd2a131SJiri Olsa 		return PTR_ERR(evsel);
54719658171SJiri Olsa 	}
548bcd3279fSFrederic Weisbecker 
549e637d177SHe Kuang 	if (head_config) {
550e637d177SHe Kuang 		LIST_HEAD(config_terms);
551e637d177SHe Kuang 
552e637d177SHe Kuang 		if (get_config_terms(head_config, &config_terms))
553e637d177SHe Kuang 			return -ENOMEM;
554e637d177SHe Kuang 		list_splice(&config_terms, &evsel->config_terms);
555e637d177SHe Kuang 	}
556e637d177SHe Kuang 
557b27c4eceSJiri Olsa 	list_add_tail(&evsel->core.node, list);
55882fe1c29SArnaldo Carvalho de Melo 	return 0;
559bcd3279fSFrederic Weisbecker }
560bcd3279fSFrederic Weisbecker 
561c5cd8ac0SDavid Ahern static int add_tracepoint_multi_event(struct list_head *list, int *idx,
5628c619d6aSWang Nan 				      const char *sys_name, const char *evt_name,
563272ed29aSJiri Olsa 				      struct parse_events_error *err,
564e637d177SHe Kuang 				      struct list_head *head_config)
565bcd3279fSFrederic Weisbecker {
56625a7d914SArnaldo Carvalho de Melo 	char *evt_path;
567bcd3279fSFrederic Weisbecker 	struct dirent *evt_ent;
568bcd3279fSFrederic Weisbecker 	DIR *evt_dir;
56927bf90bfSJiri Olsa 	int ret = 0, found = 0;
570bcd3279fSFrederic Weisbecker 
57125a7d914SArnaldo Carvalho de Melo 	evt_path = get_events_file(sys_name);
57225a7d914SArnaldo Carvalho de Melo 	if (!evt_path) {
57325a7d914SArnaldo Carvalho de Melo 		tracepoint_error(err, errno, sys_name, evt_name);
57425a7d914SArnaldo Carvalho de Melo 		return -1;
57525a7d914SArnaldo Carvalho de Melo 	}
576bcd3279fSFrederic Weisbecker 	evt_dir = opendir(evt_path);
577bcd3279fSFrederic Weisbecker 	if (!evt_dir) {
57825a7d914SArnaldo Carvalho de Melo 		put_events_file(evt_path);
579272ed29aSJiri Olsa 		tracepoint_error(err, errno, sys_name, evt_name);
58089812fc8SJiri Olsa 		return -1;
581bcd3279fSFrederic Weisbecker 	}
582bcd3279fSFrederic Weisbecker 
58389812fc8SJiri Olsa 	while (!ret && (evt_ent = readdir(evt_dir))) {
584bcd3279fSFrederic Weisbecker 		if (!strcmp(evt_ent->d_name, ".")
585bcd3279fSFrederic Weisbecker 		    || !strcmp(evt_ent->d_name, "..")
586bcd3279fSFrederic Weisbecker 		    || !strcmp(evt_ent->d_name, "enable")
587bcd3279fSFrederic Weisbecker 		    || !strcmp(evt_ent->d_name, "filter"))
588bcd3279fSFrederic Weisbecker 			continue;
589bcd3279fSFrederic Weisbecker 
59089812fc8SJiri Olsa 		if (!strglobmatch(evt_ent->d_name, evt_name))
591fb1d2edfSMasami Hiramatsu 			continue;
592fb1d2edfSMasami Hiramatsu 
59327bf90bfSJiri Olsa 		found++;
59427bf90bfSJiri Olsa 
595e637d177SHe Kuang 		ret = add_tracepoint(list, idx, sys_name, evt_ent->d_name,
596272ed29aSJiri Olsa 				     err, head_config);
597bcd3279fSFrederic Weisbecker 	}
598bcd3279fSFrederic Weisbecker 
59927bf90bfSJiri Olsa 	if (!found) {
60027bf90bfSJiri Olsa 		tracepoint_error(err, ENOENT, sys_name, evt_name);
60127bf90bfSJiri Olsa 		ret = -1;
60227bf90bfSJiri Olsa 	}
60327bf90bfSJiri Olsa 
60425a7d914SArnaldo Carvalho de Melo 	put_events_file(evt_path);
6050bd3f084SJiri Olsa 	closedir(evt_dir);
60689812fc8SJiri Olsa 	return ret;
607bcd3279fSFrederic Weisbecker }
608bcd3279fSFrederic Weisbecker 
609c5cd8ac0SDavid Ahern static int add_tracepoint_event(struct list_head *list, int *idx,
6108c619d6aSWang Nan 				const char *sys_name, const char *evt_name,
611272ed29aSJiri Olsa 				struct parse_events_error *err,
612e637d177SHe Kuang 				struct list_head *head_config)
613f35488f9SJiri Olsa {
614f35488f9SJiri Olsa 	return strpbrk(evt_name, "*?") ?
615e637d177SHe Kuang 	       add_tracepoint_multi_event(list, idx, sys_name, evt_name,
616272ed29aSJiri Olsa 					  err, head_config) :
617e637d177SHe Kuang 	       add_tracepoint(list, idx, sys_name, evt_name,
618272ed29aSJiri Olsa 			      err, head_config);
619f35488f9SJiri Olsa }
620f35488f9SJiri Olsa 
621c5cd8ac0SDavid Ahern static int add_tracepoint_multi_sys(struct list_head *list, int *idx,
6228c619d6aSWang Nan 				    const char *sys_name, const char *evt_name,
623272ed29aSJiri Olsa 				    struct parse_events_error *err,
624e637d177SHe Kuang 				    struct list_head *head_config)
625f35488f9SJiri Olsa {
626f35488f9SJiri Olsa 	struct dirent *events_ent;
627f35488f9SJiri Olsa 	DIR *events_dir;
628f35488f9SJiri Olsa 	int ret = 0;
629f35488f9SJiri Olsa 
6307014e0e3SArnaldo Carvalho de Melo 	events_dir = tracing_events__opendir();
631f35488f9SJiri Olsa 	if (!events_dir) {
632272ed29aSJiri Olsa 		tracepoint_error(err, errno, sys_name, evt_name);
633f35488f9SJiri Olsa 		return -1;
634f35488f9SJiri Olsa 	}
635f35488f9SJiri Olsa 
636f35488f9SJiri Olsa 	while (!ret && (events_ent = readdir(events_dir))) {
637f35488f9SJiri Olsa 		if (!strcmp(events_ent->d_name, ".")
638f35488f9SJiri Olsa 		    || !strcmp(events_ent->d_name, "..")
639f35488f9SJiri Olsa 		    || !strcmp(events_ent->d_name, "enable")
640f35488f9SJiri Olsa 		    || !strcmp(events_ent->d_name, "header_event")
641f35488f9SJiri Olsa 		    || !strcmp(events_ent->d_name, "header_page"))
642f35488f9SJiri Olsa 			continue;
643f35488f9SJiri Olsa 
644f35488f9SJiri Olsa 		if (!strglobmatch(events_ent->d_name, sys_name))
645f35488f9SJiri Olsa 			continue;
646f35488f9SJiri Olsa 
647f35488f9SJiri Olsa 		ret = add_tracepoint_event(list, idx, events_ent->d_name,
648272ed29aSJiri Olsa 					   evt_name, err, head_config);
649f35488f9SJiri Olsa 	}
650f35488f9SJiri Olsa 
651f35488f9SJiri Olsa 	closedir(events_dir);
652f35488f9SJiri Olsa 	return ret;
653f35488f9SJiri Olsa }
654f35488f9SJiri Olsa 
6554edf30e3SWang Nan struct __add_bpf_event_param {
6565d9cdc11SArnaldo Carvalho de Melo 	struct parse_events_state *parse_state;
6574edf30e3SWang Nan 	struct list_head *list;
65895088a59SWang Nan 	struct list_head *head_config;
6594edf30e3SWang Nan };
6604edf30e3SWang Nan 
661af4a0991SArnaldo Carvalho de Melo static int add_bpf_event(const char *group, const char *event, int fd, struct bpf_object *obj,
6624edf30e3SWang Nan 			 void *_param)
6634edf30e3SWang Nan {
6644edf30e3SWang Nan 	LIST_HEAD(new_evsels);
6654edf30e3SWang Nan 	struct __add_bpf_event_param *param = _param;
6665d9cdc11SArnaldo Carvalho de Melo 	struct parse_events_state *parse_state = param->parse_state;
6674edf30e3SWang Nan 	struct list_head *list = param->list;
66832dcd021SJiri Olsa 	struct evsel *pos;
6694edf30e3SWang Nan 	int err;
6702620b7e3SArnaldo Carvalho de Melo 	/*
6712620b7e3SArnaldo Carvalho de Melo 	 * Check if we should add the event, i.e. if it is a TP but starts with a '!',
6722620b7e3SArnaldo Carvalho de Melo 	 * then don't add the tracepoint, this will be used for something else, like
6732620b7e3SArnaldo Carvalho de Melo 	 * adding to a BPF_MAP_TYPE_PROG_ARRAY.
6742620b7e3SArnaldo Carvalho de Melo 	 *
6752620b7e3SArnaldo Carvalho de Melo 	 * See tools/perf/examples/bpf/augmented_raw_syscalls.c
6762620b7e3SArnaldo Carvalho de Melo 	 */
6772620b7e3SArnaldo Carvalho de Melo 	if (group[0] == '!')
6782620b7e3SArnaldo Carvalho de Melo 		return 0;
6794edf30e3SWang Nan 
6804edf30e3SWang Nan 	pr_debug("add bpf event %s:%s and attach bpf program %d\n",
681cd102d70SWang Nan 		 group, event, fd);
6824edf30e3SWang Nan 
6835d369a75SArnaldo Carvalho de Melo 	err = parse_events_add_tracepoint(&new_evsels, &parse_state->idx, group,
6845d369a75SArnaldo Carvalho de Melo 					  event, parse_state->error,
68595088a59SWang Nan 					  param->head_config);
6864edf30e3SWang Nan 	if (err) {
68732dcd021SJiri Olsa 		struct evsel *evsel, *tmp;
6884edf30e3SWang Nan 
6894edf30e3SWang Nan 		pr_debug("Failed to add BPF event %s:%s\n",
690cd102d70SWang Nan 			 group, event);
691b27c4eceSJiri Olsa 		list_for_each_entry_safe(evsel, tmp, &new_evsels, core.node) {
692b27c4eceSJiri Olsa 			list_del_init(&evsel->core.node);
6935eb2dd2aSJiri Olsa 			evsel__delete(evsel);
6944edf30e3SWang Nan 		}
6954edf30e3SWang Nan 		return err;
6964edf30e3SWang Nan 	}
697cd102d70SWang Nan 	pr_debug("adding %s:%s\n", group, event);
6984edf30e3SWang Nan 
699b27c4eceSJiri Olsa 	list_for_each_entry(pos, &new_evsels, core.node) {
7001f45b1d4SWang Nan 		pr_debug("adding %s:%s to %p\n",
701cd102d70SWang Nan 			 group, event, pos);
7021f45b1d4SWang Nan 		pos->bpf_fd = fd;
703af4a0991SArnaldo Carvalho de Melo 		pos->bpf_obj = obj;
7041f45b1d4SWang Nan 	}
7054edf30e3SWang Nan 	list_splice(&new_evsels, list);
7064edf30e3SWang Nan 	return 0;
7074edf30e3SWang Nan }
7084edf30e3SWang Nan 
7095d9cdc11SArnaldo Carvalho de Melo int parse_events_load_bpf_obj(struct parse_events_state *parse_state,
71084c86ca1SWang Nan 			      struct list_head *list,
71195088a59SWang Nan 			      struct bpf_object *obj,
71295088a59SWang Nan 			      struct list_head *head_config)
71384c86ca1SWang Nan {
71484c86ca1SWang Nan 	int err;
71584c86ca1SWang Nan 	char errbuf[BUFSIZ];
7165d9cdc11SArnaldo Carvalho de Melo 	struct __add_bpf_event_param param = {parse_state, list, head_config};
717aa3abf30SWang Nan 	static bool registered_unprobe_atexit = false;
71884c86ca1SWang Nan 
71984c86ca1SWang Nan 	if (IS_ERR(obj) || !obj) {
72084c86ca1SWang Nan 		snprintf(errbuf, sizeof(errbuf),
72184c86ca1SWang Nan 			 "Internal error: load bpf obj with NULL");
72284c86ca1SWang Nan 		err = -EINVAL;
72384c86ca1SWang Nan 		goto errout;
72484c86ca1SWang Nan 	}
72584c86ca1SWang Nan 
72684c86ca1SWang Nan 	/*
727aa3abf30SWang Nan 	 * Register atexit handler before calling bpf__probe() so
728aa3abf30SWang Nan 	 * bpf__probe() don't need to unprobe probe points its already
729aa3abf30SWang Nan 	 * created when failure.
730aa3abf30SWang Nan 	 */
731aa3abf30SWang Nan 	if (!registered_unprobe_atexit) {
732aa3abf30SWang Nan 		atexit(bpf__clear);
733aa3abf30SWang Nan 		registered_unprobe_atexit = true;
734aa3abf30SWang Nan 	}
735aa3abf30SWang Nan 
736aa3abf30SWang Nan 	err = bpf__probe(obj);
737aa3abf30SWang Nan 	if (err) {
738aa3abf30SWang Nan 		bpf__strerror_probe(obj, err, errbuf, sizeof(errbuf));
739aa3abf30SWang Nan 		goto errout;
740aa3abf30SWang Nan 	}
741aa3abf30SWang Nan 
7421e5e3ee8SWang Nan 	err = bpf__load(obj);
7431e5e3ee8SWang Nan 	if (err) {
7441e5e3ee8SWang Nan 		bpf__strerror_load(obj, err, errbuf, sizeof(errbuf));
7451e5e3ee8SWang Nan 		goto errout;
7461e5e3ee8SWang Nan 	}
7471e5e3ee8SWang Nan 
748cd102d70SWang Nan 	err = bpf__foreach_event(obj, add_bpf_event, &param);
7494edf30e3SWang Nan 	if (err) {
7504edf30e3SWang Nan 		snprintf(errbuf, sizeof(errbuf),
7514edf30e3SWang Nan 			 "Attach events in BPF object failed");
7524edf30e3SWang Nan 		goto errout;
7534edf30e3SWang Nan 	}
7544edf30e3SWang Nan 
7554edf30e3SWang Nan 	return 0;
75684c86ca1SWang Nan errout:
7575d9cdc11SArnaldo Carvalho de Melo 	parse_state->error->help = strdup("(add -v to see detail)");
7585d9cdc11SArnaldo Carvalho de Melo 	parse_state->error->str = strdup(errbuf);
75984c86ca1SWang Nan 	return err;
76084c86ca1SWang Nan }
76184c86ca1SWang Nan 
762a34f3be7SWang Nan static int
7635d9cdc11SArnaldo Carvalho de Melo parse_events_config_bpf(struct parse_events_state *parse_state,
764a34f3be7SWang Nan 			struct bpf_object *obj,
765a34f3be7SWang Nan 			struct list_head *head_config)
766a34f3be7SWang Nan {
767a34f3be7SWang Nan 	struct parse_events_term *term;
768a34f3be7SWang Nan 	int error_pos;
769a34f3be7SWang Nan 
770a34f3be7SWang Nan 	if (!head_config || list_empty(head_config))
771a34f3be7SWang Nan 		return 0;
772a34f3be7SWang Nan 
773a34f3be7SWang Nan 	list_for_each_entry(term, head_config, list) {
774a34f3be7SWang Nan 		char errbuf[BUFSIZ];
775a34f3be7SWang Nan 		int err;
776a34f3be7SWang Nan 
777a34f3be7SWang Nan 		if (term->type_term != PARSE_EVENTS__TERM_TYPE_USER) {
778a34f3be7SWang Nan 			snprintf(errbuf, sizeof(errbuf),
779a34f3be7SWang Nan 				 "Invalid config term for BPF object");
780a34f3be7SWang Nan 			errbuf[BUFSIZ - 1] = '\0';
781a34f3be7SWang Nan 
7825d9cdc11SArnaldo Carvalho de Melo 			parse_state->error->idx = term->err_term;
7835d9cdc11SArnaldo Carvalho de Melo 			parse_state->error->str = strdup(errbuf);
784a34f3be7SWang Nan 			return -EINVAL;
785a34f3be7SWang Nan 		}
786a34f3be7SWang Nan 
7875d9cdc11SArnaldo Carvalho de Melo 		err = bpf__config_obj(obj, term, parse_state->evlist, &error_pos);
788a34f3be7SWang Nan 		if (err) {
7895d9cdc11SArnaldo Carvalho de Melo 			bpf__strerror_config_obj(obj, term, parse_state->evlist,
790a34f3be7SWang Nan 						 &error_pos, err, errbuf,
791a34f3be7SWang Nan 						 sizeof(errbuf));
7925d9cdc11SArnaldo Carvalho de Melo 			parse_state->error->help = strdup(
7937630b3e2SWang Nan "Hint:\tValid config terms:\n"
794e571e029SWang Nan "     \tmap:[<arraymap>].value<indices>=[value]\n"
795e571e029SWang Nan "     \tmap:[<eventmap>].event<indices>=[event]\n"
7967630b3e2SWang Nan "\n"
797e571e029SWang Nan "     \twhere <indices> is something like [0,3...5] or [all]\n"
798a34f3be7SWang Nan "     \t(add -v to see detail)");
7995d9cdc11SArnaldo Carvalho de Melo 			parse_state->error->str = strdup(errbuf);
800a34f3be7SWang Nan 			if (err == -BPF_LOADER_ERRNO__OBJCONF_MAP_VALUE)
8015d9cdc11SArnaldo Carvalho de Melo 				parse_state->error->idx = term->err_val;
802a34f3be7SWang Nan 			else
8035d9cdc11SArnaldo Carvalho de Melo 				parse_state->error->idx = term->err_term + error_pos;
804a34f3be7SWang Nan 			return err;
805a34f3be7SWang Nan 		}
806a34f3be7SWang Nan 	}
807a34f3be7SWang Nan 	return 0;
808a34f3be7SWang Nan }
809a34f3be7SWang Nan 
81095088a59SWang Nan /*
81195088a59SWang Nan  * Split config terms:
81295088a59SWang Nan  * perf record -e bpf.c/call-graph=fp,map:array.value[0]=1/ ...
81395088a59SWang Nan  *  'call-graph=fp' is 'evt config', should be applied to each
81495088a59SWang Nan  *  events in bpf.c.
81595088a59SWang Nan  * 'map:array.value[0]=1' is 'obj config', should be processed
81695088a59SWang Nan  * with parse_events_config_bpf.
81795088a59SWang Nan  *
81895088a59SWang Nan  * Move object config terms from the first list to obj_head_config.
81995088a59SWang Nan  */
82095088a59SWang Nan static void
82195088a59SWang Nan split_bpf_config_terms(struct list_head *evt_head_config,
82295088a59SWang Nan 		       struct list_head *obj_head_config)
82395088a59SWang Nan {
82495088a59SWang Nan 	struct parse_events_term *term, *temp;
82595088a59SWang Nan 
82695088a59SWang Nan 	/*
82795088a59SWang Nan 	 * Currectly, all possible user config term
82895088a59SWang Nan 	 * belong to bpf object. parse_events__is_hardcoded_term()
82995088a59SWang Nan 	 * happends to be a good flag.
83095088a59SWang Nan 	 *
83195088a59SWang Nan 	 * See parse_events_config_bpf() and
83295088a59SWang Nan 	 * config_term_tracepoint().
83395088a59SWang Nan 	 */
83495088a59SWang Nan 	list_for_each_entry_safe(term, temp, evt_head_config, list)
83595088a59SWang Nan 		if (!parse_events__is_hardcoded_term(term))
83695088a59SWang Nan 			list_move_tail(&term->list, obj_head_config);
83795088a59SWang Nan }
83895088a59SWang Nan 
8395d9cdc11SArnaldo Carvalho de Melo int parse_events_load_bpf(struct parse_events_state *parse_state,
84084c86ca1SWang Nan 			  struct list_head *list,
841d509db04SWang Nan 			  char *bpf_file_name,
842a34f3be7SWang Nan 			  bool source,
843a34f3be7SWang Nan 			  struct list_head *head_config)
84484c86ca1SWang Nan {
845a34f3be7SWang Nan 	int err;
84695088a59SWang Nan 	struct bpf_object *obj;
84795088a59SWang Nan 	LIST_HEAD(obj_head_config);
84895088a59SWang Nan 
84995088a59SWang Nan 	if (head_config)
85095088a59SWang Nan 		split_bpf_config_terms(head_config, &obj_head_config);
85184c86ca1SWang Nan 
852d509db04SWang Nan 	obj = bpf__prepare_load(bpf_file_name, source);
8536371ca3bSWang Nan 	if (IS_ERR(obj)) {
85484c86ca1SWang Nan 		char errbuf[BUFSIZ];
85584c86ca1SWang Nan 
8566371ca3bSWang Nan 		err = PTR_ERR(obj);
85784c86ca1SWang Nan 
85884c86ca1SWang Nan 		if (err == -ENOTSUP)
85984c86ca1SWang Nan 			snprintf(errbuf, sizeof(errbuf),
86084c86ca1SWang Nan 				 "BPF support is not compiled");
86184c86ca1SWang Nan 		else
862d3e0ce39SWang Nan 			bpf__strerror_prepare_load(bpf_file_name,
863d3e0ce39SWang Nan 						   source,
864d3e0ce39SWang Nan 						   -err, errbuf,
865d3e0ce39SWang Nan 						   sizeof(errbuf));
86684c86ca1SWang Nan 
8675d9cdc11SArnaldo Carvalho de Melo 		parse_state->error->help = strdup("(add -v to see detail)");
8685d9cdc11SArnaldo Carvalho de Melo 		parse_state->error->str = strdup(errbuf);
86984c86ca1SWang Nan 		return err;
87084c86ca1SWang Nan 	}
87184c86ca1SWang Nan 
8725d9cdc11SArnaldo Carvalho de Melo 	err = parse_events_load_bpf_obj(parse_state, list, obj, head_config);
873a34f3be7SWang Nan 	if (err)
874a34f3be7SWang Nan 		return err;
8755d9cdc11SArnaldo Carvalho de Melo 	err = parse_events_config_bpf(parse_state, obj, &obj_head_config);
87695088a59SWang Nan 
87795088a59SWang Nan 	/*
87895088a59SWang Nan 	 * Caller doesn't know anything about obj_head_config,
87995088a59SWang Nan 	 * so combine them together again before returnning.
88095088a59SWang Nan 	 */
88195088a59SWang Nan 	if (head_config)
88295088a59SWang Nan 		list_splice_tail(&obj_head_config, head_config);
88395088a59SWang Nan 	return err;
88484c86ca1SWang Nan }
88584c86ca1SWang Nan 
88689812fc8SJiri Olsa static int
88789812fc8SJiri Olsa parse_breakpoint_type(const char *type, struct perf_event_attr *attr)
8881b290d67SFrederic Weisbecker {
8891b290d67SFrederic Weisbecker 	int i;
8901b290d67SFrederic Weisbecker 
8911b290d67SFrederic Weisbecker 	for (i = 0; i < 3; i++) {
89289812fc8SJiri Olsa 		if (!type || !type[i])
8931b290d67SFrederic Weisbecker 			break;
8941b290d67SFrederic Weisbecker 
8957582732fSJiri Olsa #define CHECK_SET_TYPE(bit)		\
8967582732fSJiri Olsa do {					\
8977582732fSJiri Olsa 	if (attr->bp_type & bit)	\
8987582732fSJiri Olsa 		return -EINVAL;		\
8997582732fSJiri Olsa 	else				\
9007582732fSJiri Olsa 		attr->bp_type |= bit;	\
9017582732fSJiri Olsa } while (0)
9027582732fSJiri Olsa 
9031b290d67SFrederic Weisbecker 		switch (type[i]) {
9041b290d67SFrederic Weisbecker 		case 'r':
9057582732fSJiri Olsa 			CHECK_SET_TYPE(HW_BREAKPOINT_R);
9061b290d67SFrederic Weisbecker 			break;
9071b290d67SFrederic Weisbecker 		case 'w':
9087582732fSJiri Olsa 			CHECK_SET_TYPE(HW_BREAKPOINT_W);
9091b290d67SFrederic Weisbecker 			break;
9101b290d67SFrederic Weisbecker 		case 'x':
9117582732fSJiri Olsa 			CHECK_SET_TYPE(HW_BREAKPOINT_X);
9121b290d67SFrederic Weisbecker 			break;
9131b290d67SFrederic Weisbecker 		default:
91489812fc8SJiri Olsa 			return -EINVAL;
9151b290d67SFrederic Weisbecker 		}
9161b290d67SFrederic Weisbecker 	}
91789812fc8SJiri Olsa 
9187582732fSJiri Olsa #undef CHECK_SET_TYPE
9197582732fSJiri Olsa 
9201b290d67SFrederic Weisbecker 	if (!attr->bp_type) /* Default */
9211b290d67SFrederic Weisbecker 		attr->bp_type = HW_BREAKPOINT_R | HW_BREAKPOINT_W;
9221b290d67SFrederic Weisbecker 
92389812fc8SJiri Olsa 	return 0;
9241b290d67SFrederic Weisbecker }
9251b290d67SFrederic Weisbecker 
926c5cd8ac0SDavid Ahern int parse_events_add_breakpoint(struct list_head *list, int *idx,
9273741eb9fSJacob Shin 				void *ptr, char *type, u64 len)
9281b290d67SFrederic Weisbecker {
92989812fc8SJiri Olsa 	struct perf_event_attr attr;
9301b290d67SFrederic Weisbecker 
93189812fc8SJiri Olsa 	memset(&attr, 0, sizeof(attr));
9329fafd98fSJiri Olsa 	attr.bp_addr = (unsigned long) ptr;
9331b290d67SFrederic Weisbecker 
93489812fc8SJiri Olsa 	if (parse_breakpoint_type(type, &attr))
93589812fc8SJiri Olsa 		return -EINVAL;
9361b290d67SFrederic Weisbecker 
9373741eb9fSJacob Shin 	/* Provide some defaults if len is not specified */
9383741eb9fSJacob Shin 	if (!len) {
93989812fc8SJiri Olsa 		if (attr.bp_type == HW_BREAKPOINT_X)
9403741eb9fSJacob Shin 			len = sizeof(long);
941aa59a485SFrederic Weisbecker 		else
9423741eb9fSJacob Shin 			len = HW_BREAKPOINT_LEN_4;
9433741eb9fSJacob Shin 	}
9443741eb9fSJacob Shin 
9453741eb9fSJacob Shin 	attr.bp_len = len;
946aa59a485SFrederic Weisbecker 
94789812fc8SJiri Olsa 	attr.type = PERF_TYPE_BREAKPOINT;
9484a841d65SJovi Zhang 	attr.sample_period = 1;
9491b290d67SFrederic Weisbecker 
950930a2e29SJiri Olsa 	return add_event(list, idx, &attr, NULL, NULL);
9511b290d67SFrederic Weisbecker }
9521b290d67SFrederic Weisbecker 
9533b0e371cSJiri Olsa static int check_type_val(struct parse_events_term *term,
9543b0e371cSJiri Olsa 			  struct parse_events_error *err,
9553b0e371cSJiri Olsa 			  int type)
9563b0e371cSJiri Olsa {
9573b0e371cSJiri Olsa 	if (type == term->type_val)
9583b0e371cSJiri Olsa 		return 0;
9593b0e371cSJiri Olsa 
9603b0e371cSJiri Olsa 	if (err) {
961448d732cSIan Rogers 		parse_events__handle_error(err, term->err_val,
962448d732cSIan Rogers 					type == PARSE_EVENTS__TERM_TYPE_NUM
963448d732cSIan Rogers 					? strdup("expected numeric value")
964448d732cSIan Rogers 					: strdup("expected string value"),
965448d732cSIan Rogers 					NULL);
9663b0e371cSJiri Olsa 	}
9673b0e371cSJiri Olsa 	return -EINVAL;
9683b0e371cSJiri Olsa }
9693b0e371cSJiri Olsa 
97017cb5f84SWang Nan /*
97117cb5f84SWang Nan  * Update according to parse-events.l
97217cb5f84SWang Nan  */
97317cb5f84SWang Nan static const char *config_term_names[__PARSE_EVENTS__TERM_TYPE_NR] = {
97417cb5f84SWang Nan 	[PARSE_EVENTS__TERM_TYPE_USER]			= "<sysfs term>",
97517cb5f84SWang Nan 	[PARSE_EVENTS__TERM_TYPE_CONFIG]		= "config",
97617cb5f84SWang Nan 	[PARSE_EVENTS__TERM_TYPE_CONFIG1]		= "config1",
97717cb5f84SWang Nan 	[PARSE_EVENTS__TERM_TYPE_CONFIG2]		= "config2",
97817cb5f84SWang Nan 	[PARSE_EVENTS__TERM_TYPE_NAME]			= "name",
97917cb5f84SWang Nan 	[PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD]		= "period",
98017cb5f84SWang Nan 	[PARSE_EVENTS__TERM_TYPE_SAMPLE_FREQ]		= "freq",
98117cb5f84SWang Nan 	[PARSE_EVENTS__TERM_TYPE_BRANCH_SAMPLE_TYPE]	= "branch_type",
98217cb5f84SWang Nan 	[PARSE_EVENTS__TERM_TYPE_TIME]			= "time",
98317cb5f84SWang Nan 	[PARSE_EVENTS__TERM_TYPE_CALLGRAPH]		= "call-graph",
98417cb5f84SWang Nan 	[PARSE_EVENTS__TERM_TYPE_STACKSIZE]		= "stack-size",
98517cb5f84SWang Nan 	[PARSE_EVENTS__TERM_TYPE_NOINHERIT]		= "no-inherit",
98617cb5f84SWang Nan 	[PARSE_EVENTS__TERM_TYPE_INHERIT]		= "inherit",
987792d48b4SArnaldo Carvalho de Melo 	[PARSE_EVENTS__TERM_TYPE_MAX_STACK]		= "max-stack",
9882fda5adaSArnaldo Carvalho de Melo 	[PARSE_EVENTS__TERM_TYPE_MAX_EVENTS]		= "nr",
989626a6b78SWang Nan 	[PARSE_EVENTS__TERM_TYPE_OVERWRITE]		= "overwrite",
990626a6b78SWang Nan 	[PARSE_EVENTS__TERM_TYPE_NOOVERWRITE]		= "no-overwrite",
991dd60fba7SMathieu Poirier 	[PARSE_EVENTS__TERM_TYPE_DRV_CFG]		= "driver-config",
992064b4e82SJin Yao 	[PARSE_EVENTS__TERM_TYPE_PERCORE]		= "percore",
9931b992154SAdrian Hunter 	[PARSE_EVENTS__TERM_TYPE_AUX_OUTPUT]		= "aux-output",
994eb7a52d4SAdrian Hunter 	[PARSE_EVENTS__TERM_TYPE_AUX_SAMPLE_SIZE]	= "aux-sample-size",
99517cb5f84SWang Nan };
99617cb5f84SWang Nan 
9971669e509SWang Nan static bool config_term_shrinked;
9981669e509SWang Nan 
9991669e509SWang Nan static bool
10001669e509SWang Nan config_term_avail(int term_type, struct parse_events_error *err)
10011669e509SWang Nan {
1002448d732cSIan Rogers 	char *err_str;
1003448d732cSIan Rogers 
10041669e509SWang Nan 	if (term_type < 0 || term_type >= __PARSE_EVENTS__TERM_TYPE_NR) {
1005448d732cSIan Rogers 		parse_events__handle_error(err, -1,
1006448d732cSIan Rogers 					strdup("Invalid term_type"), NULL);
10071669e509SWang Nan 		return false;
10081669e509SWang Nan 	}
10091669e509SWang Nan 	if (!config_term_shrinked)
10101669e509SWang Nan 		return true;
10111669e509SWang Nan 
10121669e509SWang Nan 	switch (term_type) {
10131669e509SWang Nan 	case PARSE_EVENTS__TERM_TYPE_CONFIG:
10141669e509SWang Nan 	case PARSE_EVENTS__TERM_TYPE_CONFIG1:
10151669e509SWang Nan 	case PARSE_EVENTS__TERM_TYPE_CONFIG2:
10161669e509SWang Nan 	case PARSE_EVENTS__TERM_TYPE_NAME:
101706835545SSukadev Bhattiprolu 	case PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD:
1018064b4e82SJin Yao 	case PARSE_EVENTS__TERM_TYPE_PERCORE:
10191669e509SWang Nan 		return true;
10201669e509SWang Nan 	default:
10211669e509SWang Nan 		if (!err)
10221669e509SWang Nan 			return false;
10231669e509SWang Nan 
10241669e509SWang Nan 		/* term_type is validated so indexing is safe */
1025448d732cSIan Rogers 		if (asprintf(&err_str, "'%s' is not usable in 'perf stat'",
1026448d732cSIan Rogers 				config_term_names[term_type]) >= 0)
1027448d732cSIan Rogers 			parse_events__handle_error(err, -1, err_str, NULL);
10281669e509SWang Nan 		return false;
10291669e509SWang Nan 	}
10301669e509SWang Nan }
10311669e509SWang Nan 
10321669e509SWang Nan void parse_events__shrink_config_terms(void)
10331669e509SWang Nan {
10341669e509SWang Nan 	config_term_shrinked = true;
10351669e509SWang Nan }
10361669e509SWang Nan 
10370b8891a8SHe Kuang static int config_term_common(struct perf_event_attr *attr,
10383b0e371cSJiri Olsa 			      struct parse_events_term *term,
10393b0e371cSJiri Olsa 			      struct parse_events_error *err)
10408f707d84SJiri Olsa {
104116fa7e82SJiri Olsa #define CHECK_TYPE_VAL(type)						   \
104216fa7e82SJiri Olsa do {									   \
10433b0e371cSJiri Olsa 	if (check_type_val(term, err, PARSE_EVENTS__TERM_TYPE_ ## type)) \
104416fa7e82SJiri Olsa 		return -EINVAL;						   \
104516fa7e82SJiri Olsa } while (0)
104616fa7e82SJiri Olsa 
104716fa7e82SJiri Olsa 	switch (term->type_term) {
10488f707d84SJiri Olsa 	case PARSE_EVENTS__TERM_TYPE_CONFIG:
104916fa7e82SJiri Olsa 		CHECK_TYPE_VAL(NUM);
10508f707d84SJiri Olsa 		attr->config = term->val.num;
10518f707d84SJiri Olsa 		break;
10528f707d84SJiri Olsa 	case PARSE_EVENTS__TERM_TYPE_CONFIG1:
105316fa7e82SJiri Olsa 		CHECK_TYPE_VAL(NUM);
10548f707d84SJiri Olsa 		attr->config1 = term->val.num;
10558f707d84SJiri Olsa 		break;
10568f707d84SJiri Olsa 	case PARSE_EVENTS__TERM_TYPE_CONFIG2:
105716fa7e82SJiri Olsa 		CHECK_TYPE_VAL(NUM);
10588f707d84SJiri Olsa 		attr->config2 = term->val.num;
10598f707d84SJiri Olsa 		break;
10608f707d84SJiri Olsa 	case PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD:
106116fa7e82SJiri Olsa 		CHECK_TYPE_VAL(NUM);
10628f707d84SJiri Olsa 		break;
106309af2a55SNamhyung Kim 	case PARSE_EVENTS__TERM_TYPE_SAMPLE_FREQ:
106409af2a55SNamhyung Kim 		CHECK_TYPE_VAL(NUM);
106509af2a55SNamhyung Kim 		break;
10668f707d84SJiri Olsa 	case PARSE_EVENTS__TERM_TYPE_BRANCH_SAMPLE_TYPE:
1067ac12f676SAndi Kleen 		CHECK_TYPE_VAL(STR);
1068ac12f676SAndi Kleen 		if (strcmp(term->val.str, "no") &&
1069448d732cSIan Rogers 		    parse_branch_str(term->val.str,
1070448d732cSIan Rogers 				    &attr->branch_sample_type)) {
1071448d732cSIan Rogers 			parse_events__handle_error(err, term->err_val,
1072448d732cSIan Rogers 					strdup("invalid branch sample type"),
1073448d732cSIan Rogers 					NULL);
1074ac12f676SAndi Kleen 			return -EINVAL;
1075ac12f676SAndi Kleen 		}
10768f707d84SJiri Olsa 		break;
107732067712SKan Liang 	case PARSE_EVENTS__TERM_TYPE_TIME:
107832067712SKan Liang 		CHECK_TYPE_VAL(NUM);
107932067712SKan Liang 		if (term->val.num > 1) {
1080448d732cSIan Rogers 			parse_events__handle_error(err, term->err_val,
1081448d732cSIan Rogers 						strdup("expected 0 or 1"),
1082448d732cSIan Rogers 						NULL);
108332067712SKan Liang 			return -EINVAL;
108432067712SKan Liang 		}
108532067712SKan Liang 		break;
1086d457c963SKan Liang 	case PARSE_EVENTS__TERM_TYPE_CALLGRAPH:
1087d457c963SKan Liang 		CHECK_TYPE_VAL(STR);
1088d457c963SKan Liang 		break;
1089d457c963SKan Liang 	case PARSE_EVENTS__TERM_TYPE_STACKSIZE:
1090d457c963SKan Liang 		CHECK_TYPE_VAL(NUM);
1091d457c963SKan Liang 		break;
1092374ce938SWang Nan 	case PARSE_EVENTS__TERM_TYPE_INHERIT:
1093374ce938SWang Nan 		CHECK_TYPE_VAL(NUM);
1094374ce938SWang Nan 		break;
1095374ce938SWang Nan 	case PARSE_EVENTS__TERM_TYPE_NOINHERIT:
1096374ce938SWang Nan 		CHECK_TYPE_VAL(NUM);
1097374ce938SWang Nan 		break;
1098626a6b78SWang Nan 	case PARSE_EVENTS__TERM_TYPE_OVERWRITE:
1099626a6b78SWang Nan 		CHECK_TYPE_VAL(NUM);
1100626a6b78SWang Nan 		break;
1101626a6b78SWang Nan 	case PARSE_EVENTS__TERM_TYPE_NOOVERWRITE:
1102626a6b78SWang Nan 		CHECK_TYPE_VAL(NUM);
1103626a6b78SWang Nan 		break;
11046b5fc39bSJiri Olsa 	case PARSE_EVENTS__TERM_TYPE_NAME:
11056b5fc39bSJiri Olsa 		CHECK_TYPE_VAL(STR);
11066b5fc39bSJiri Olsa 		break;
1107792d48b4SArnaldo Carvalho de Melo 	case PARSE_EVENTS__TERM_TYPE_MAX_STACK:
1108792d48b4SArnaldo Carvalho de Melo 		CHECK_TYPE_VAL(NUM);
1109792d48b4SArnaldo Carvalho de Melo 		break;
11102fda5adaSArnaldo Carvalho de Melo 	case PARSE_EVENTS__TERM_TYPE_MAX_EVENTS:
11112fda5adaSArnaldo Carvalho de Melo 		CHECK_TYPE_VAL(NUM);
11122fda5adaSArnaldo Carvalho de Melo 		break;
1113064b4e82SJin Yao 	case PARSE_EVENTS__TERM_TYPE_PERCORE:
1114064b4e82SJin Yao 		CHECK_TYPE_VAL(NUM);
1115064b4e82SJin Yao 		if ((unsigned int)term->val.num > 1) {
1116448d732cSIan Rogers 			parse_events__handle_error(err, term->err_val,
1117448d732cSIan Rogers 						strdup("expected 0 or 1"),
1118448d732cSIan Rogers 						NULL);
1119064b4e82SJin Yao 			return -EINVAL;
1120064b4e82SJin Yao 		}
1121064b4e82SJin Yao 		break;
11221b992154SAdrian Hunter 	case PARSE_EVENTS__TERM_TYPE_AUX_OUTPUT:
11231b992154SAdrian Hunter 		CHECK_TYPE_VAL(NUM);
11241b992154SAdrian Hunter 		break;
1125eb7a52d4SAdrian Hunter 	case PARSE_EVENTS__TERM_TYPE_AUX_SAMPLE_SIZE:
1126eb7a52d4SAdrian Hunter 		CHECK_TYPE_VAL(NUM);
1127eb7a52d4SAdrian Hunter 		if (term->val.num > UINT_MAX) {
1128eb7a52d4SAdrian Hunter 			parse_events__handle_error(err, term->err_val,
1129eb7a52d4SAdrian Hunter 						strdup("too big"),
1130eb7a52d4SAdrian Hunter 						NULL);
1131eb7a52d4SAdrian Hunter 			return -EINVAL;
1132eb7a52d4SAdrian Hunter 		}
1133eb7a52d4SAdrian Hunter 		break;
11348f707d84SJiri Olsa 	default:
1135448d732cSIan Rogers 		parse_events__handle_error(err, term->err_term,
1136448d732cSIan Rogers 				strdup("unknown term"),
1137448d732cSIan Rogers 				parse_events_formats_error_string(NULL));
11388f707d84SJiri Olsa 		return -EINVAL;
11398f707d84SJiri Olsa 	}
114016fa7e82SJiri Olsa 
11411669e509SWang Nan 	/*
11421669e509SWang Nan 	 * Check term availbility after basic checking so
11431669e509SWang Nan 	 * PARSE_EVENTS__TERM_TYPE_USER can be found and filtered.
11441669e509SWang Nan 	 *
11451669e509SWang Nan 	 * If check availbility at the entry of this function,
11461669e509SWang Nan 	 * user will see "'<sysfs term>' is not usable in 'perf stat'"
11471669e509SWang Nan 	 * if an invalid config term is provided for legacy events
11481669e509SWang Nan 	 * (for example, instructions/badterm/...), which is confusing.
11491669e509SWang Nan 	 */
11501669e509SWang Nan 	if (!config_term_avail(term->type_term, err))
11511669e509SWang Nan 		return -EINVAL;
11528f707d84SJiri Olsa 	return 0;
115316fa7e82SJiri Olsa #undef CHECK_TYPE_VAL
11548f707d84SJiri Olsa }
11558f707d84SJiri Olsa 
11560b8891a8SHe Kuang static int config_term_pmu(struct perf_event_attr *attr,
11570b8891a8SHe Kuang 			   struct parse_events_term *term,
11580b8891a8SHe Kuang 			   struct parse_events_error *err)
11590b8891a8SHe Kuang {
1160dd60fba7SMathieu Poirier 	if (term->type_term == PARSE_EVENTS__TERM_TYPE_USER ||
1161dd60fba7SMathieu Poirier 	    term->type_term == PARSE_EVENTS__TERM_TYPE_DRV_CFG)
11620b8891a8SHe Kuang 		/*
11630b8891a8SHe Kuang 		 * Always succeed for sysfs terms, as we dont know
11640b8891a8SHe Kuang 		 * at this point what type they need to have.
11650b8891a8SHe Kuang 		 */
11660b8891a8SHe Kuang 		return 0;
11670b8891a8SHe Kuang 	else
11680b8891a8SHe Kuang 		return config_term_common(attr, term, err);
11690b8891a8SHe Kuang }
11700b8891a8SHe Kuang 
1171e637d177SHe Kuang static int config_term_tracepoint(struct perf_event_attr *attr,
1172e637d177SHe Kuang 				  struct parse_events_term *term,
1173e637d177SHe Kuang 				  struct parse_events_error *err)
1174e637d177SHe Kuang {
1175e637d177SHe Kuang 	switch (term->type_term) {
1176e637d177SHe Kuang 	case PARSE_EVENTS__TERM_TYPE_CALLGRAPH:
1177e637d177SHe Kuang 	case PARSE_EVENTS__TERM_TYPE_STACKSIZE:
1178374ce938SWang Nan 	case PARSE_EVENTS__TERM_TYPE_INHERIT:
1179374ce938SWang Nan 	case PARSE_EVENTS__TERM_TYPE_NOINHERIT:
1180792d48b4SArnaldo Carvalho de Melo 	case PARSE_EVENTS__TERM_TYPE_MAX_STACK:
11812fda5adaSArnaldo Carvalho de Melo 	case PARSE_EVENTS__TERM_TYPE_MAX_EVENTS:
1182626a6b78SWang Nan 	case PARSE_EVENTS__TERM_TYPE_OVERWRITE:
1183626a6b78SWang Nan 	case PARSE_EVENTS__TERM_TYPE_NOOVERWRITE:
11841b992154SAdrian Hunter 	case PARSE_EVENTS__TERM_TYPE_AUX_OUTPUT:
1185eb7a52d4SAdrian Hunter 	case PARSE_EVENTS__TERM_TYPE_AUX_SAMPLE_SIZE:
1186e637d177SHe Kuang 		return config_term_common(attr, term, err);
1187e637d177SHe Kuang 	default:
1188e637d177SHe Kuang 		if (err) {
1189448d732cSIan Rogers 			parse_events__handle_error(err, term->err_term,
1190448d732cSIan Rogers 				strdup("unknown term"),
1191448d732cSIan Rogers 				strdup("valid terms: call-graph,stack-size\n"));
1192e637d177SHe Kuang 		}
1193e637d177SHe Kuang 		return -EINVAL;
1194e637d177SHe Kuang 	}
1195e637d177SHe Kuang 
1196e637d177SHe Kuang 	return 0;
1197e637d177SHe Kuang }
1198e637d177SHe Kuang 
11998f707d84SJiri Olsa static int config_attr(struct perf_event_attr *attr,
12003b0e371cSJiri Olsa 		       struct list_head *head,
12010b8891a8SHe Kuang 		       struct parse_events_error *err,
12020b8891a8SHe Kuang 		       config_term_func_t config_term)
12038f707d84SJiri Olsa {
12046cee6cd3SArnaldo Carvalho de Melo 	struct parse_events_term *term;
12058f707d84SJiri Olsa 
12068f707d84SJiri Olsa 	list_for_each_entry(term, head, list)
12073b0e371cSJiri Olsa 		if (config_term(attr, term, err))
12088f707d84SJiri Olsa 			return -EINVAL;
12098f707d84SJiri Olsa 
12108f707d84SJiri Olsa 	return 0;
12118f707d84SJiri Olsa }
12128f707d84SJiri Olsa 
1213930a2e29SJiri Olsa static int get_config_terms(struct list_head *head_config,
1214930a2e29SJiri Olsa 			    struct list_head *head_terms __maybe_unused)
1215930a2e29SJiri Olsa {
121605e54e23SIan Rogers #define ADD_CONFIG_TERM(__type, __weak)				\
1217930a2e29SJiri Olsa 	struct perf_evsel_config_term *__t;			\
1218930a2e29SJiri Olsa 								\
1219930a2e29SJiri Olsa 	__t = zalloc(sizeof(*__t));				\
1220930a2e29SJiri Olsa 	if (!__t)						\
1221930a2e29SJiri Olsa 		return -ENOMEM;					\
1222930a2e29SJiri Olsa 								\
1223930a2e29SJiri Olsa 	INIT_LIST_HEAD(&__t->list);				\
1224930a2e29SJiri Olsa 	__t->type       = PERF_EVSEL__CONFIG_TERM_ ## __type;	\
122505e54e23SIan Rogers 	__t->weak	= __weak;				\
1226e884602bSLeo Yan 	list_add_tail(&__t->list, head_terms)
1227e884602bSLeo Yan 
122805e54e23SIan Rogers #define ADD_CONFIG_TERM_VAL(__type, __name, __val, __weak)	\
1229e884602bSLeo Yan do {								\
123005e54e23SIan Rogers 	ADD_CONFIG_TERM(__type, __weak);			\
1231e884602bSLeo Yan 	__t->val.__name = __val;				\
1232e884602bSLeo Yan } while (0)
1233e884602bSLeo Yan 
123405e54e23SIan Rogers #define ADD_CONFIG_TERM_STR(__type, __val, __weak)		\
1235e884602bSLeo Yan do {								\
123605e54e23SIan Rogers 	ADD_CONFIG_TERM(__type, __weak);			\
12373220fb8dSLeo Yan 	__t->val.str = strdup(__val);				\
12383220fb8dSLeo Yan 	if (!__t->val.str) {					\
12393220fb8dSLeo Yan 		zfree(&__t);					\
12403220fb8dSLeo Yan 		return -ENOMEM;					\
12413220fb8dSLeo Yan 	}							\
12423220fb8dSLeo Yan 	__t->free_str = true;					\
1243930a2e29SJiri Olsa } while (0)
1244930a2e29SJiri Olsa 
1245930a2e29SJiri Olsa 	struct parse_events_term *term;
1246930a2e29SJiri Olsa 
1247930a2e29SJiri Olsa 	list_for_each_entry(term, head_config, list) {
1248930a2e29SJiri Olsa 		switch (term->type_term) {
1249ee4c7588SJiri Olsa 		case PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD:
125005e54e23SIan Rogers 			ADD_CONFIG_TERM_VAL(PERIOD, period, term->val.num, term->weak);
125132067712SKan Liang 			break;
125209af2a55SNamhyung Kim 		case PARSE_EVENTS__TERM_TYPE_SAMPLE_FREQ:
125305e54e23SIan Rogers 			ADD_CONFIG_TERM_VAL(FREQ, freq, term->val.num, term->weak);
125409af2a55SNamhyung Kim 			break;
125532067712SKan Liang 		case PARSE_EVENTS__TERM_TYPE_TIME:
125605e54e23SIan Rogers 			ADD_CONFIG_TERM_VAL(TIME, time, term->val.num, term->weak);
125732067712SKan Liang 			break;
1258d457c963SKan Liang 		case PARSE_EVENTS__TERM_TYPE_CALLGRAPH:
125905e54e23SIan Rogers 			ADD_CONFIG_TERM_STR(CALLGRAPH, term->val.str, term->weak);
1260d457c963SKan Liang 			break;
1261ac12f676SAndi Kleen 		case PARSE_EVENTS__TERM_TYPE_BRANCH_SAMPLE_TYPE:
126205e54e23SIan Rogers 			ADD_CONFIG_TERM_STR(BRANCH, term->val.str, term->weak);
1263ac12f676SAndi Kleen 			break;
1264d457c963SKan Liang 		case PARSE_EVENTS__TERM_TYPE_STACKSIZE:
1265e884602bSLeo Yan 			ADD_CONFIG_TERM_VAL(STACK_USER, stack_user,
126605e54e23SIan Rogers 					    term->val.num, term->weak);
1267d457c963SKan Liang 			break;
1268374ce938SWang Nan 		case PARSE_EVENTS__TERM_TYPE_INHERIT:
1269e884602bSLeo Yan 			ADD_CONFIG_TERM_VAL(INHERIT, inherit,
127005e54e23SIan Rogers 					    term->val.num ? 1 : 0, term->weak);
1271374ce938SWang Nan 			break;
1272374ce938SWang Nan 		case PARSE_EVENTS__TERM_TYPE_NOINHERIT:
1273e884602bSLeo Yan 			ADD_CONFIG_TERM_VAL(INHERIT, inherit,
127405e54e23SIan Rogers 					    term->val.num ? 0 : 1, term->weak);
1275374ce938SWang Nan 			break;
1276792d48b4SArnaldo Carvalho de Melo 		case PARSE_EVENTS__TERM_TYPE_MAX_STACK:
1277e884602bSLeo Yan 			ADD_CONFIG_TERM_VAL(MAX_STACK, max_stack,
127805e54e23SIan Rogers 					    term->val.num, term->weak);
1279792d48b4SArnaldo Carvalho de Melo 			break;
12802fda5adaSArnaldo Carvalho de Melo 		case PARSE_EVENTS__TERM_TYPE_MAX_EVENTS:
1281e884602bSLeo Yan 			ADD_CONFIG_TERM_VAL(MAX_EVENTS, max_events,
128205e54e23SIan Rogers 					    term->val.num, term->weak);
12832fda5adaSArnaldo Carvalho de Melo 			break;
1284626a6b78SWang Nan 		case PARSE_EVENTS__TERM_TYPE_OVERWRITE:
1285e884602bSLeo Yan 			ADD_CONFIG_TERM_VAL(OVERWRITE, overwrite,
128605e54e23SIan Rogers 					    term->val.num ? 1 : 0, term->weak);
1287626a6b78SWang Nan 			break;
1288626a6b78SWang Nan 		case PARSE_EVENTS__TERM_TYPE_NOOVERWRITE:
1289e884602bSLeo Yan 			ADD_CONFIG_TERM_VAL(OVERWRITE, overwrite,
129005e54e23SIan Rogers 					    term->val.num ? 0 : 1, term->weak);
1291626a6b78SWang Nan 			break;
1292dd60fba7SMathieu Poirier 		case PARSE_EVENTS__TERM_TYPE_DRV_CFG:
129305e54e23SIan Rogers 			ADD_CONFIG_TERM_STR(DRV_CFG, term->val.str, term->weak);
1294dd60fba7SMathieu Poirier 			break;
1295064b4e82SJin Yao 		case PARSE_EVENTS__TERM_TYPE_PERCORE:
1296e884602bSLeo Yan 			ADD_CONFIG_TERM_VAL(PERCORE, percore,
129705e54e23SIan Rogers 					    term->val.num ? true : false, term->weak);
1298064b4e82SJin Yao 			break;
12991b992154SAdrian Hunter 		case PARSE_EVENTS__TERM_TYPE_AUX_OUTPUT:
1300e884602bSLeo Yan 			ADD_CONFIG_TERM_VAL(AUX_OUTPUT, aux_output,
130105e54e23SIan Rogers 					    term->val.num ? 1 : 0, term->weak);
13021b992154SAdrian Hunter 			break;
1303eb7a52d4SAdrian Hunter 		case PARSE_EVENTS__TERM_TYPE_AUX_SAMPLE_SIZE:
1304e884602bSLeo Yan 			ADD_CONFIG_TERM_VAL(AUX_SAMPLE_SIZE, aux_sample_size,
130505e54e23SIan Rogers 					    term->val.num, term->weak);
1306eb7a52d4SAdrian Hunter 			break;
1307930a2e29SJiri Olsa 		default:
1308930a2e29SJiri Olsa 			break;
1309930a2e29SJiri Olsa 		}
1310930a2e29SJiri Olsa 	}
1311a1ac7de6SAdrian Hunter 	return 0;
1312a1ac7de6SAdrian Hunter }
1313a1ac7de6SAdrian Hunter 
1314a1ac7de6SAdrian Hunter /*
1315a1ac7de6SAdrian Hunter  * Add PERF_EVSEL__CONFIG_TERM_CFG_CHG where cfg_chg will have a bit set for
1316a1ac7de6SAdrian Hunter  * each bit of attr->config that the user has changed.
1317a1ac7de6SAdrian Hunter  */
1318a1ac7de6SAdrian Hunter static int get_config_chgs(struct perf_pmu *pmu, struct list_head *head_config,
1319a1ac7de6SAdrian Hunter 			   struct list_head *head_terms)
1320a1ac7de6SAdrian Hunter {
1321a1ac7de6SAdrian Hunter 	struct parse_events_term *term;
1322a1ac7de6SAdrian Hunter 	u64 bits = 0;
1323a1ac7de6SAdrian Hunter 	int type;
1324a1ac7de6SAdrian Hunter 
1325a1ac7de6SAdrian Hunter 	list_for_each_entry(term, head_config, list) {
1326a1ac7de6SAdrian Hunter 		switch (term->type_term) {
1327a1ac7de6SAdrian Hunter 		case PARSE_EVENTS__TERM_TYPE_USER:
1328a1ac7de6SAdrian Hunter 			type = perf_pmu__format_type(&pmu->format, term->config);
1329a1ac7de6SAdrian Hunter 			if (type != PERF_PMU_FORMAT_VALUE_CONFIG)
1330a1ac7de6SAdrian Hunter 				continue;
1331a1ac7de6SAdrian Hunter 			bits |= perf_pmu__format_bits(&pmu->format, term->config);
1332a1ac7de6SAdrian Hunter 			break;
1333a1ac7de6SAdrian Hunter 		case PARSE_EVENTS__TERM_TYPE_CONFIG:
1334a1ac7de6SAdrian Hunter 			bits = ~(u64)0;
1335a1ac7de6SAdrian Hunter 			break;
1336a1ac7de6SAdrian Hunter 		default:
1337a1ac7de6SAdrian Hunter 			break;
1338a1ac7de6SAdrian Hunter 		}
1339a1ac7de6SAdrian Hunter 	}
1340a1ac7de6SAdrian Hunter 
1341a1ac7de6SAdrian Hunter 	if (bits)
134205e54e23SIan Rogers 		ADD_CONFIG_TERM_VAL(CFG_CHG, cfg_chg, bits, false);
1343a1ac7de6SAdrian Hunter 
1344a1ac7de6SAdrian Hunter #undef ADD_CONFIG_TERM
1345930a2e29SJiri Olsa 	return 0;
1346930a2e29SJiri Olsa }
1347930a2e29SJiri Olsa 
1348e637d177SHe Kuang int parse_events_add_tracepoint(struct list_head *list, int *idx,
13498c619d6aSWang Nan 				const char *sys, const char *event,
1350272ed29aSJiri Olsa 				struct parse_events_error *err,
1351e637d177SHe Kuang 				struct list_head *head_config)
1352e637d177SHe Kuang {
1353e637d177SHe Kuang 	if (head_config) {
1354e637d177SHe Kuang 		struct perf_event_attr attr;
1355e637d177SHe Kuang 
1356272ed29aSJiri Olsa 		if (config_attr(&attr, head_config, err,
1357e637d177SHe Kuang 				config_term_tracepoint))
1358e637d177SHe Kuang 			return -EINVAL;
1359e637d177SHe Kuang 	}
1360e637d177SHe Kuang 
1361e637d177SHe Kuang 	if (strpbrk(sys, "*?"))
1362e637d177SHe Kuang 		return add_tracepoint_multi_sys(list, idx, sys, event,
1363272ed29aSJiri Olsa 						err, head_config);
1364e637d177SHe Kuang 	else
1365e637d177SHe Kuang 		return add_tracepoint_event(list, idx, sys, event,
1366272ed29aSJiri Olsa 					    err, head_config);
1367e637d177SHe Kuang }
1368e637d177SHe Kuang 
13695d9cdc11SArnaldo Carvalho de Melo int parse_events_add_numeric(struct parse_events_state *parse_state,
137087d650beSJiri Olsa 			     struct list_head *list,
1371b527bab5SRobert Richter 			     u32 type, u64 config,
13728f707d84SJiri Olsa 			     struct list_head *head_config)
137374d5b588SJaswinder Singh Rajput {
137489812fc8SJiri Olsa 	struct perf_event_attr attr;
1375930a2e29SJiri Olsa 	LIST_HEAD(config_terms);
137674d5b588SJaswinder Singh Rajput 
137789812fc8SJiri Olsa 	memset(&attr, 0, sizeof(attr));
137889812fc8SJiri Olsa 	attr.type = type;
137989812fc8SJiri Olsa 	attr.config = config;
13808f707d84SJiri Olsa 
1381930a2e29SJiri Olsa 	if (head_config) {
13825d9cdc11SArnaldo Carvalho de Melo 		if (config_attr(&attr, head_config, parse_state->error,
13830b8891a8SHe Kuang 				config_term_common))
13848f707d84SJiri Olsa 			return -EINVAL;
13858f707d84SJiri Olsa 
1386930a2e29SJiri Olsa 		if (get_config_terms(head_config, &config_terms))
1387930a2e29SJiri Olsa 			return -ENOMEM;
1388930a2e29SJiri Olsa 	}
1389930a2e29SJiri Olsa 
13905d9cdc11SArnaldo Carvalho de Melo 	return add_event(list, &parse_state->idx, &attr,
139110bf358aSWang Nan 			 get_config_name(head_config), &config_terms);
1392b908debdSIngo Molnar }
1393b908debdSIngo Molnar 
1394f0fbb114SAndi Kleen int parse_events_add_tool(struct parse_events_state *parse_state,
1395f0fbb114SAndi Kleen 			  struct list_head *list,
1396f0fbb114SAndi Kleen 			  enum perf_tool_event tool_event)
1397f0fbb114SAndi Kleen {
1398f0fbb114SAndi Kleen 	return add_event_tool(list, &parse_state->idx, tool_event);
1399f0fbb114SAndi Kleen }
1400f0fbb114SAndi Kleen 
1401064b4e82SJin Yao static bool config_term_percore(struct list_head *config_terms)
1402064b4e82SJin Yao {
1403064b4e82SJin Yao 	struct perf_evsel_config_term *term;
1404064b4e82SJin Yao 
1405064b4e82SJin Yao 	list_for_each_entry(term, config_terms, list) {
1406064b4e82SJin Yao 		if (term->type == PERF_EVSEL__CONFIG_TERM_PERCORE)
1407064b4e82SJin Yao 			return term->val.percore;
1408064b4e82SJin Yao 	}
1409064b4e82SJin Yao 
1410064b4e82SJin Yao 	return false;
1411064b4e82SJin Yao }
1412064b4e82SJin Yao 
1413c199c11dSAgustin Vega-Frias int parse_events_add_pmu(struct parse_events_state *parse_state,
141436adec85SJiri Olsa 			 struct list_head *list, char *name,
14153cdc5c2cSKan Liang 			 struct list_head *head_config,
14163cdc5c2cSKan Liang 			 bool auto_merge_stats,
14173cdc5c2cSKan Liang 			 bool use_alias)
14185f537a26SJiri Olsa {
14195f537a26SJiri Olsa 	struct perf_event_attr attr;
142046441bdcSMatt Fleming 	struct perf_pmu_info info;
14215f537a26SJiri Olsa 	struct perf_pmu *pmu;
142232dcd021SJiri Olsa 	struct evsel *evsel;
1423333b5665SAndi Kleen 	struct parse_events_error *err = parse_state->error;
14243cdc5c2cSKan Liang 	bool use_uncore_alias;
1425930a2e29SJiri Olsa 	LIST_HEAD(config_terms);
14265f537a26SJiri Olsa 
14275f537a26SJiri Olsa 	pmu = perf_pmu__find(name);
1428333b5665SAndi Kleen 	if (!pmu) {
1429448d732cSIan Rogers 		char *err_str;
1430448d732cSIan Rogers 
1431448d732cSIan Rogers 		if (asprintf(&err_str,
1432333b5665SAndi Kleen 				"Cannot find PMU `%s'. Missing kernel support?",
1433448d732cSIan Rogers 				name) >= 0)
1434a910e466SIan Rogers 			parse_events__handle_error(err, 0, err_str, NULL);
14355f537a26SJiri Olsa 		return -EINVAL;
1436333b5665SAndi Kleen 	}
14375f537a26SJiri Olsa 
1438dc0a6202SAdrian Hunter 	if (pmu->default_config) {
1439dc0a6202SAdrian Hunter 		memcpy(&attr, pmu->default_config,
1440dc0a6202SAdrian Hunter 		       sizeof(struct perf_event_attr));
1441dc0a6202SAdrian Hunter 	} else {
14425f537a26SJiri Olsa 		memset(&attr, 0, sizeof(attr));
1443dc0a6202SAdrian Hunter 	}
14445f537a26SJiri Olsa 
14453cdc5c2cSKan Liang 	use_uncore_alias = (pmu->is_uncore && use_alias);
14463cdc5c2cSKan Liang 
1447ad962273SAdrian Hunter 	if (!head_config) {
1448ad962273SAdrian Hunter 		attr.type = pmu->type;
1449f0fbb114SAndi Kleen 		evsel = __add_event(list, &parse_state->idx, &attr, NULL, pmu, NULL,
1450f0fbb114SAndi Kleen 				    auto_merge_stats, NULL);
14518c5421c0SAgustin Vega-Frias 		if (evsel) {
1452*d4953f7eSIan Rogers 			evsel->pmu_name = name ? strdup(name) : NULL;
14533cdc5c2cSKan Liang 			evsel->use_uncore_alias = use_uncore_alias;
14548c5421c0SAgustin Vega-Frias 			return 0;
14558c5421c0SAgustin Vega-Frias 		} else {
14568c5421c0SAgustin Vega-Frias 			return -ENOMEM;
14578c5421c0SAgustin Vega-Frias 		}
1458ad962273SAdrian Hunter 	}
1459ad962273SAdrian Hunter 
146046441bdcSMatt Fleming 	if (perf_pmu__check_alias(pmu, head_config, &info))
1461a6146d50SZheng Yan 		return -EINVAL;
1462a6146d50SZheng Yan 
14635f537a26SJiri Olsa 	/*
14645f537a26SJiri Olsa 	 * Configure hardcoded terms first, no need to check
14655f537a26SJiri Olsa 	 * return value when called with fail == 0 ;)
14665f537a26SJiri Olsa 	 */
14675d9cdc11SArnaldo Carvalho de Melo 	if (config_attr(&attr, head_config, parse_state->error, config_term_pmu))
1468c056ba6aSJiri Olsa 		return -EINVAL;
14695f537a26SJiri Olsa 
1470930a2e29SJiri Olsa 	if (get_config_terms(head_config, &config_terms))
1471930a2e29SJiri Olsa 		return -ENOMEM;
1472930a2e29SJiri Olsa 
1473a1ac7de6SAdrian Hunter 	/*
1474a1ac7de6SAdrian Hunter 	 * When using default config, record which bits of attr->config were
1475a1ac7de6SAdrian Hunter 	 * changed by the user.
1476a1ac7de6SAdrian Hunter 	 */
1477a1ac7de6SAdrian Hunter 	if (pmu->default_config && get_config_chgs(pmu, head_config, &config_terms))
1478a1ac7de6SAdrian Hunter 		return -ENOMEM;
1479a1ac7de6SAdrian Hunter 
148038f2c422SIan Rogers 	if (perf_pmu__config(pmu, &attr, head_config, parse_state->error)) {
148138f2c422SIan Rogers 		struct perf_evsel_config_term *pos, *tmp;
148238f2c422SIan Rogers 
148338f2c422SIan Rogers 		list_for_each_entry_safe(pos, tmp, &config_terms, list) {
148438f2c422SIan Rogers 			list_del_init(&pos->list);
148538f2c422SIan Rogers 			free(pos);
148638f2c422SIan Rogers 		}
14875f537a26SJiri Olsa 		return -EINVAL;
148838f2c422SIan Rogers 	}
14895f537a26SJiri Olsa 
14905d9cdc11SArnaldo Carvalho de Melo 	evsel = __add_event(list, &parse_state->idx, &attr,
149166ec1191SMark Rutland 			    get_config_name(head_config), pmu,
1492f0fbb114SAndi Kleen 			    &config_terms, auto_merge_stats, NULL);
1493410136f5SStephane Eranian 	if (evsel) {
149446441bdcSMatt Fleming 		evsel->unit = info.unit;
149546441bdcSMatt Fleming 		evsel->scale = info.scale;
1496044330c1SMatt Fleming 		evsel->per_pkg = info.per_pkg;
14971d9e446bSJiri Olsa 		evsel->snapshot = info.snapshot;
149837932c18SAndi Kleen 		evsel->metric_expr = info.metric_expr;
149996284814SAndi Kleen 		evsel->metric_name = info.metric_name;
1500*d4953f7eSIan Rogers 		evsel->pmu_name = name ? strdup(name) : NULL;
15013cdc5c2cSKan Liang 		evsel->use_uncore_alias = use_uncore_alias;
1502064b4e82SJin Yao 		evsel->percore = config_term_percore(&evsel->config_terms);
1503410136f5SStephane Eranian 	}
1504410136f5SStephane Eranian 
1505410136f5SStephane Eranian 	return evsel ? 0 : -ENOMEM;
15065f537a26SJiri Olsa }
15075f537a26SJiri Olsa 
15085d9cdc11SArnaldo Carvalho de Melo int parse_events_multi_pmu_add(struct parse_events_state *parse_state,
15092073ad33SAndi Kleen 			       char *str, struct list_head **listp)
15102073ad33SAndi Kleen {
15112073ad33SAndi Kleen 	struct parse_events_term *term;
15122073ad33SAndi Kleen 	struct list_head *list;
15132073ad33SAndi Kleen 	struct perf_pmu *pmu = NULL;
15142073ad33SAndi Kleen 	int ok = 0;
15152073ad33SAndi Kleen 
15162073ad33SAndi Kleen 	*listp = NULL;
15172073ad33SAndi Kleen 	/* Add it for all PMUs that support the alias */
15182073ad33SAndi Kleen 	list = malloc(sizeof(struct list_head));
15192073ad33SAndi Kleen 	if (!list)
15202073ad33SAndi Kleen 		return -1;
15212073ad33SAndi Kleen 	INIT_LIST_HEAD(list);
15222073ad33SAndi Kleen 	while ((pmu = perf_pmu__scan(pmu)) != NULL) {
15232073ad33SAndi Kleen 		struct perf_pmu_alias *alias;
15242073ad33SAndi Kleen 
15252073ad33SAndi Kleen 		list_for_each_entry(alias, &pmu->aliases, list) {
15262073ad33SAndi Kleen 			if (!strcasecmp(alias->name, str)) {
1527b6645a72SIan Rogers 				struct list_head *head;
1528b6645a72SIan Rogers 				char *config;
1529b6645a72SIan Rogers 
15302073ad33SAndi Kleen 				head = malloc(sizeof(struct list_head));
15312073ad33SAndi Kleen 				if (!head)
15322073ad33SAndi Kleen 					return -1;
15332073ad33SAndi Kleen 				INIT_LIST_HEAD(head);
1534b6645a72SIan Rogers 				config = strdup(str);
1535b6645a72SIan Rogers 				if (!config)
15362073ad33SAndi Kleen 					return -1;
1537b6645a72SIan Rogers 				if (parse_events_term__num(&term,
1538b6645a72SIan Rogers 						   PARSE_EVENTS__TERM_TYPE_USER,
1539b6645a72SIan Rogers 						   config, 1, false, &config,
1540b6645a72SIan Rogers 						   NULL) < 0) {
1541b6645a72SIan Rogers 					free(list);
1542b6645a72SIan Rogers 					free(config);
1543b6645a72SIan Rogers 					return -1;
1544b6645a72SIan Rogers 				}
15452073ad33SAndi Kleen 				list_add_tail(&term->list, head);
15462073ad33SAndi Kleen 
1547c199c11dSAgustin Vega-Frias 				if (!parse_events_add_pmu(parse_state, list,
15483cdc5c2cSKan Liang 							  pmu->name, head,
15493cdc5c2cSKan Liang 							  true, true)) {
1550*d4953f7eSIan Rogers 					pr_debug("%s -> %s/%s/\n", str,
15512073ad33SAndi Kleen 						 pmu->name, alias->str);
15522073ad33SAndi Kleen 					ok++;
15532073ad33SAndi Kleen 				}
15542073ad33SAndi Kleen 
15552073ad33SAndi Kleen 				parse_events_terms__delete(head);
15562073ad33SAndi Kleen 			}
15572073ad33SAndi Kleen 		}
15582073ad33SAndi Kleen 	}
1559b6645a72SIan Rogers 	if (!ok) {
1560b6645a72SIan Rogers 		free(list);
15612073ad33SAndi Kleen 		return -1;
1562b6645a72SIan Rogers 	}
15632073ad33SAndi Kleen 	*listp = list;
15642073ad33SAndi Kleen 	return 0;
15652073ad33SAndi Kleen }
15662073ad33SAndi Kleen 
15676a4bb04cSJiri Olsa int parse_events__modifier_group(struct list_head *list,
15686a4bb04cSJiri Olsa 				 char *event_mod)
156989efb029SJiri Olsa {
15706a4bb04cSJiri Olsa 	return parse_events__modifier_event(list, event_mod, true);
15716a4bb04cSJiri Olsa }
15726a4bb04cSJiri Olsa 
15733cdc5c2cSKan Liang /*
15743cdc5c2cSKan Liang  * Check if the two uncore PMUs are from the same uncore block
15753cdc5c2cSKan Liang  * The format of the uncore PMU name is uncore_#blockname_#pmuidx
15763cdc5c2cSKan Liang  */
15773cdc5c2cSKan Liang static bool is_same_uncore_block(const char *pmu_name_a, const char *pmu_name_b)
15783cdc5c2cSKan Liang {
15793cdc5c2cSKan Liang 	char *end_a, *end_b;
15803cdc5c2cSKan Liang 
15813cdc5c2cSKan Liang 	end_a = strrchr(pmu_name_a, '_');
15823cdc5c2cSKan Liang 	end_b = strrchr(pmu_name_b, '_');
15833cdc5c2cSKan Liang 
15843cdc5c2cSKan Liang 	if (!end_a || !end_b)
15853cdc5c2cSKan Liang 		return false;
15863cdc5c2cSKan Liang 
15873cdc5c2cSKan Liang 	if ((end_a - pmu_name_a) != (end_b - pmu_name_b))
15883cdc5c2cSKan Liang 		return false;
15893cdc5c2cSKan Liang 
15903cdc5c2cSKan Liang 	return (strncmp(pmu_name_a, pmu_name_b, end_a - pmu_name_a) == 0);
15913cdc5c2cSKan Liang }
15923cdc5c2cSKan Liang 
15933cdc5c2cSKan Liang static int
15943cdc5c2cSKan Liang parse_events__set_leader_for_uncore_aliase(char *name, struct list_head *list,
15953cdc5c2cSKan Liang 					   struct parse_events_state *parse_state)
15963cdc5c2cSKan Liang {
159732dcd021SJiri Olsa 	struct evsel *evsel, *leader;
15983cdc5c2cSKan Liang 	uintptr_t *leaders;
15993cdc5c2cSKan Liang 	bool is_leader = true;
16003cdc5c2cSKan Liang 	int i, nr_pmu = 0, total_members, ret = 0;
16013cdc5c2cSKan Liang 
1602b27c4eceSJiri Olsa 	leader = list_first_entry(list, struct evsel, core.node);
1603b27c4eceSJiri Olsa 	evsel = list_last_entry(list, struct evsel, core.node);
16043cdc5c2cSKan Liang 	total_members = evsel->idx - leader->idx + 1;
16053cdc5c2cSKan Liang 
16063cdc5c2cSKan Liang 	leaders = calloc(total_members, sizeof(uintptr_t));
16073cdc5c2cSKan Liang 	if (WARN_ON(!leaders))
16083cdc5c2cSKan Liang 		return 0;
16093cdc5c2cSKan Liang 
16103cdc5c2cSKan Liang 	/*
16113cdc5c2cSKan Liang 	 * Going through the whole group and doing sanity check.
16123cdc5c2cSKan Liang 	 * All members must use alias, and be from the same uncore block.
16133cdc5c2cSKan Liang 	 * Also, storing the leader events in an array.
16143cdc5c2cSKan Liang 	 */
16153cdc5c2cSKan Liang 	__evlist__for_each_entry(list, evsel) {
16163cdc5c2cSKan Liang 
16173cdc5c2cSKan Liang 		/* Only split the uncore group which members use alias */
16183cdc5c2cSKan Liang 		if (!evsel->use_uncore_alias)
16193cdc5c2cSKan Liang 			goto out;
16203cdc5c2cSKan Liang 
16213cdc5c2cSKan Liang 		/* The events must be from the same uncore block */
16223cdc5c2cSKan Liang 		if (!is_same_uncore_block(leader->pmu_name, evsel->pmu_name))
16233cdc5c2cSKan Liang 			goto out;
16243cdc5c2cSKan Liang 
16253cdc5c2cSKan Liang 		if (!is_leader)
16263cdc5c2cSKan Liang 			continue;
16273cdc5c2cSKan Liang 		/*
16283cdc5c2cSKan Liang 		 * If the event's PMU name starts to repeat, it must be a new
16293cdc5c2cSKan Liang 		 * event. That can be used to distinguish the leader from
16303cdc5c2cSKan Liang 		 * other members, even they have the same event name.
16313cdc5c2cSKan Liang 		 */
16323cdc5c2cSKan Liang 		if ((leader != evsel) && (leader->pmu_name == evsel->pmu_name)) {
16333cdc5c2cSKan Liang 			is_leader = false;
16343cdc5c2cSKan Liang 			continue;
16353cdc5c2cSKan Liang 		}
16363cdc5c2cSKan Liang 		/* The name is always alias name */
16373cdc5c2cSKan Liang 		WARN_ON(strcmp(leader->name, evsel->name));
16383cdc5c2cSKan Liang 
16393cdc5c2cSKan Liang 		/* Store the leader event for each PMU */
16403cdc5c2cSKan Liang 		leaders[nr_pmu++] = (uintptr_t) evsel;
16413cdc5c2cSKan Liang 	}
16423cdc5c2cSKan Liang 
16433cdc5c2cSKan Liang 	/* only one event alias */
16443cdc5c2cSKan Liang 	if (nr_pmu == total_members) {
16453cdc5c2cSKan Liang 		parse_state->nr_groups--;
16463cdc5c2cSKan Liang 		goto handled;
16473cdc5c2cSKan Liang 	}
16483cdc5c2cSKan Liang 
16493cdc5c2cSKan Liang 	/*
16503cdc5c2cSKan Liang 	 * An uncore event alias is a joint name which means the same event
16513cdc5c2cSKan Liang 	 * runs on all PMUs of a block.
16523cdc5c2cSKan Liang 	 * Perf doesn't support mixed events from different PMUs in the same
16533cdc5c2cSKan Liang 	 * group. The big group has to be split into multiple small groups
16543cdc5c2cSKan Liang 	 * which only include the events from the same PMU.
16553cdc5c2cSKan Liang 	 *
16563cdc5c2cSKan Liang 	 * Here the uncore event aliases must be from the same uncore block.
16573cdc5c2cSKan Liang 	 * The number of PMUs must be same for each alias. The number of new
16583cdc5c2cSKan Liang 	 * small groups equals to the number of PMUs.
16593cdc5c2cSKan Liang 	 * Setting the leader event for corresponding members in each group.
16603cdc5c2cSKan Liang 	 */
16613cdc5c2cSKan Liang 	i = 0;
16623cdc5c2cSKan Liang 	__evlist__for_each_entry(list, evsel) {
16633cdc5c2cSKan Liang 		if (i >= nr_pmu)
16643cdc5c2cSKan Liang 			i = 0;
166532dcd021SJiri Olsa 		evsel->leader = (struct evsel *) leaders[i++];
16663cdc5c2cSKan Liang 	}
16673cdc5c2cSKan Liang 
16683cdc5c2cSKan Liang 	/* The number of members and group name are same for each group */
16693cdc5c2cSKan Liang 	for (i = 0; i < nr_pmu; i++) {
167032dcd021SJiri Olsa 		evsel = (struct evsel *) leaders[i];
16715643b1a5SJiri Olsa 		evsel->core.nr_members = total_members / nr_pmu;
16723cdc5c2cSKan Liang 		evsel->group_name = name ? strdup(name) : NULL;
16733cdc5c2cSKan Liang 	}
16743cdc5c2cSKan Liang 
16753cdc5c2cSKan Liang 	/* Take the new small groups into account */
16763cdc5c2cSKan Liang 	parse_state->nr_groups += nr_pmu - 1;
16773cdc5c2cSKan Liang 
16783cdc5c2cSKan Liang handled:
16793cdc5c2cSKan Liang 	ret = 1;
16803cdc5c2cSKan Liang out:
16813cdc5c2cSKan Liang 	free(leaders);
16823cdc5c2cSKan Liang 	return ret;
16833cdc5c2cSKan Liang }
16843cdc5c2cSKan Liang 
16853cdc5c2cSKan Liang void parse_events__set_leader(char *name, struct list_head *list,
16863cdc5c2cSKan Liang 			      struct parse_events_state *parse_state)
16876a4bb04cSJiri Olsa {
168832dcd021SJiri Olsa 	struct evsel *leader;
16896a4bb04cSJiri Olsa 
1690854f7363SWang Nan 	if (list_empty(list)) {
1691854f7363SWang Nan 		WARN_ONCE(true, "WARNING: failed to set leader: empty list");
1692854f7363SWang Nan 		return;
1693854f7363SWang Nan 	}
1694854f7363SWang Nan 
16953cdc5c2cSKan Liang 	if (parse_events__set_leader_for_uncore_aliase(name, list, parse_state))
16963cdc5c2cSKan Liang 		return;
16973cdc5c2cSKan Liang 
169863dab225SArnaldo Carvalho de Melo 	__perf_evlist__set_leader(list);
1699b27c4eceSJiri Olsa 	leader = list_entry(list->next, struct evsel, core.node);
17006a4bb04cSJiri Olsa 	leader->group_name = name ? strdup(name) : NULL;
170189efb029SJiri Olsa }
170289efb029SJiri Olsa 
1703c5cd8ac0SDavid Ahern /* list_event is assumed to point to malloc'ed memory */
17045d7be90eSJiri Olsa void parse_events_update_lists(struct list_head *list_event,
17055d7be90eSJiri Olsa 			       struct list_head *list_all)
17065d7be90eSJiri Olsa {
17075d7be90eSJiri Olsa 	/*
17085d7be90eSJiri Olsa 	 * Called for single event definition. Update the
170989efb029SJiri Olsa 	 * 'all event' list, and reinit the 'single event'
17105d7be90eSJiri Olsa 	 * list, for next event definition.
17115d7be90eSJiri Olsa 	 */
17125d7be90eSJiri Olsa 	list_splice_tail(list_event, list_all);
1713b847cbdcSJiri Olsa 	free(list_event);
17145d7be90eSJiri Olsa }
17155d7be90eSJiri Olsa 
1716f5b1135bSJiri Olsa struct event_modifier {
1717f5b1135bSJiri Olsa 	int eu;
1718f5b1135bSJiri Olsa 	int ek;
1719f5b1135bSJiri Olsa 	int eh;
1720f5b1135bSJiri Olsa 	int eH;
1721f5b1135bSJiri Olsa 	int eG;
1722a1e12da4SJiri Olsa 	int eI;
1723f5b1135bSJiri Olsa 	int precise;
17247f94af7aSJiri Olsa 	int precise_max;
1725f5b1135bSJiri Olsa 	int exclude_GH;
17263c176311SJiri Olsa 	int sample_read;
1727e9a7c414SMichael Ellerman 	int pinned;
17285a5dfe4bSAndi Kleen 	int weak;
1729f5b1135bSJiri Olsa };
173061c45981SPaul Mackerras 
1731f5b1135bSJiri Olsa static int get_event_modifier(struct event_modifier *mod, char *str,
173232dcd021SJiri Olsa 			       struct evsel *evsel)
1733f5b1135bSJiri Olsa {
17341fc632ceSJiri Olsa 	int eu = evsel ? evsel->core.attr.exclude_user : 0;
17351fc632ceSJiri Olsa 	int ek = evsel ? evsel->core.attr.exclude_kernel : 0;
17361fc632ceSJiri Olsa 	int eh = evsel ? evsel->core.attr.exclude_hv : 0;
17371fc632ceSJiri Olsa 	int eH = evsel ? evsel->core.attr.exclude_host : 0;
17381fc632ceSJiri Olsa 	int eG = evsel ? evsel->core.attr.exclude_guest : 0;
17391fc632ceSJiri Olsa 	int eI = evsel ? evsel->core.attr.exclude_idle : 0;
17401fc632ceSJiri Olsa 	int precise = evsel ? evsel->core.attr.precise_ip : 0;
17417f94af7aSJiri Olsa 	int precise_max = 0;
17423c176311SJiri Olsa 	int sample_read = 0;
17431fc632ceSJiri Olsa 	int pinned = evsel ? evsel->core.attr.pinned : 0;
1744f5b1135bSJiri Olsa 
1745f5b1135bSJiri Olsa 	int exclude = eu | ek | eh;
1746f5b1135bSJiri Olsa 	int exclude_GH = evsel ? evsel->exclude_GH : 0;
17475a5dfe4bSAndi Kleen 	int weak = 0;
1748f5b1135bSJiri Olsa 
1749f5b1135bSJiri Olsa 	memset(mod, 0, sizeof(*mod));
1750ceb53fbfSIngo Molnar 
175161c45981SPaul Mackerras 	while (*str) {
1752ab608344SPeter Zijlstra 		if (*str == 'u') {
1753ab608344SPeter Zijlstra 			if (!exclude)
1754ab608344SPeter Zijlstra 				exclude = eu = ek = eh = 1;
175561c45981SPaul Mackerras 			eu = 0;
1756ab608344SPeter Zijlstra 		} else if (*str == 'k') {
1757ab608344SPeter Zijlstra 			if (!exclude)
1758ab608344SPeter Zijlstra 				exclude = eu = ek = eh = 1;
175961c45981SPaul Mackerras 			ek = 0;
1760ab608344SPeter Zijlstra 		} else if (*str == 'h') {
1761ab608344SPeter Zijlstra 			if (!exclude)
1762ab608344SPeter Zijlstra 				exclude = eu = ek = eh = 1;
176361c45981SPaul Mackerras 			eh = 0;
176499320cc8SJoerg Roedel 		} else if (*str == 'G') {
176599320cc8SJoerg Roedel 			if (!exclude_GH)
176699320cc8SJoerg Roedel 				exclude_GH = eG = eH = 1;
176799320cc8SJoerg Roedel 			eG = 0;
176899320cc8SJoerg Roedel 		} else if (*str == 'H') {
176999320cc8SJoerg Roedel 			if (!exclude_GH)
177099320cc8SJoerg Roedel 				exclude_GH = eG = eH = 1;
177199320cc8SJoerg Roedel 			eH = 0;
1772a1e12da4SJiri Olsa 		} else if (*str == 'I') {
1773a1e12da4SJiri Olsa 			eI = 1;
1774ab608344SPeter Zijlstra 		} else if (*str == 'p') {
1775ab608344SPeter Zijlstra 			precise++;
17761342798cSDavid Ahern 			/* use of precise requires exclude_guest */
17771342798cSDavid Ahern 			if (!exclude_GH)
17781342798cSDavid Ahern 				eG = 1;
17797f94af7aSJiri Olsa 		} else if (*str == 'P') {
17807f94af7aSJiri Olsa 			precise_max = 1;
17813c176311SJiri Olsa 		} else if (*str == 'S') {
17823c176311SJiri Olsa 			sample_read = 1;
1783e9a7c414SMichael Ellerman 		} else if (*str == 'D') {
1784e9a7c414SMichael Ellerman 			pinned = 1;
17855a5dfe4bSAndi Kleen 		} else if (*str == 'W') {
17865a5dfe4bSAndi Kleen 			weak = 1;
1787ab608344SPeter Zijlstra 		} else
178861c45981SPaul Mackerras 			break;
1789ab608344SPeter Zijlstra 
179061c45981SPaul Mackerras 		++str;
179161c45981SPaul Mackerras 	}
179274d5b588SJaswinder Singh Rajput 
179386470930SIngo Molnar 	/*
179489812fc8SJiri Olsa 	 * precise ip:
179589812fc8SJiri Olsa 	 *
179689812fc8SJiri Olsa 	 *  0 - SAMPLE_IP can have arbitrary skid
179789812fc8SJiri Olsa 	 *  1 - SAMPLE_IP must have constant skid
179889812fc8SJiri Olsa 	 *  2 - SAMPLE_IP requested to have 0 skid
179989812fc8SJiri Olsa 	 *  3 - SAMPLE_IP must have 0 skid
180089812fc8SJiri Olsa 	 *
180189812fc8SJiri Olsa 	 *  See also PERF_RECORD_MISC_EXACT_IP
180286470930SIngo Molnar 	 */
180389812fc8SJiri Olsa 	if (precise > 3)
180489812fc8SJiri Olsa 		return -EINVAL;
180586470930SIngo Molnar 
1806f5b1135bSJiri Olsa 	mod->eu = eu;
1807f5b1135bSJiri Olsa 	mod->ek = ek;
1808f5b1135bSJiri Olsa 	mod->eh = eh;
1809f5b1135bSJiri Olsa 	mod->eH = eH;
1810f5b1135bSJiri Olsa 	mod->eG = eG;
1811a1e12da4SJiri Olsa 	mod->eI = eI;
1812f5b1135bSJiri Olsa 	mod->precise = precise;
18137f94af7aSJiri Olsa 	mod->precise_max = precise_max;
1814f5b1135bSJiri Olsa 	mod->exclude_GH = exclude_GH;
18153c176311SJiri Olsa 	mod->sample_read = sample_read;
1816e9a7c414SMichael Ellerman 	mod->pinned = pinned;
18175a5dfe4bSAndi Kleen 	mod->weak = weak;
1818e9a7c414SMichael Ellerman 
1819f5b1135bSJiri Olsa 	return 0;
1820f5b1135bSJiri Olsa }
1821f5b1135bSJiri Olsa 
1822534123f4SJiri Olsa /*
1823534123f4SJiri Olsa  * Basic modifier sanity check to validate it contains only one
1824534123f4SJiri Olsa  * instance of any modifier (apart from 'p') present.
1825534123f4SJiri Olsa  */
1826534123f4SJiri Olsa static int check_modifier(char *str)
1827534123f4SJiri Olsa {
1828534123f4SJiri Olsa 	char *p = str;
1829534123f4SJiri Olsa 
1830534123f4SJiri Olsa 	/* The sizeof includes 0 byte as well. */
18315a5dfe4bSAndi Kleen 	if (strlen(str) > (sizeof("ukhGHpppPSDIW") - 1))
1832534123f4SJiri Olsa 		return -1;
1833534123f4SJiri Olsa 
1834534123f4SJiri Olsa 	while (*p) {
1835534123f4SJiri Olsa 		if (*p != 'p' && strchr(p + 1, *p))
1836534123f4SJiri Olsa 			return -1;
1837534123f4SJiri Olsa 		p++;
1838534123f4SJiri Olsa 	}
1839534123f4SJiri Olsa 
1840534123f4SJiri Olsa 	return 0;
1841534123f4SJiri Olsa }
1842534123f4SJiri Olsa 
1843f5b1135bSJiri Olsa int parse_events__modifier_event(struct list_head *list, char *str, bool add)
1844f5b1135bSJiri Olsa {
184532dcd021SJiri Olsa 	struct evsel *evsel;
1846f5b1135bSJiri Olsa 	struct event_modifier mod;
1847f5b1135bSJiri Olsa 
1848f5b1135bSJiri Olsa 	if (str == NULL)
1849f5b1135bSJiri Olsa 		return 0;
1850f5b1135bSJiri Olsa 
1851534123f4SJiri Olsa 	if (check_modifier(str))
1852534123f4SJiri Olsa 		return -EINVAL;
1853534123f4SJiri Olsa 
1854f5b1135bSJiri Olsa 	if (!add && get_event_modifier(&mod, str, NULL))
1855f5b1135bSJiri Olsa 		return -EINVAL;
1856f5b1135bSJiri Olsa 
1857e5cadb93SArnaldo Carvalho de Melo 	__evlist__for_each_entry(list, evsel) {
1858f5b1135bSJiri Olsa 		if (add && get_event_modifier(&mod, str, evsel))
1859f5b1135bSJiri Olsa 			return -EINVAL;
1860f5b1135bSJiri Olsa 
18611fc632ceSJiri Olsa 		evsel->core.attr.exclude_user   = mod.eu;
18621fc632ceSJiri Olsa 		evsel->core.attr.exclude_kernel = mod.ek;
18631fc632ceSJiri Olsa 		evsel->core.attr.exclude_hv     = mod.eh;
18641fc632ceSJiri Olsa 		evsel->core.attr.precise_ip     = mod.precise;
18651fc632ceSJiri Olsa 		evsel->core.attr.exclude_host   = mod.eH;
18661fc632ceSJiri Olsa 		evsel->core.attr.exclude_guest  = mod.eG;
18671fc632ceSJiri Olsa 		evsel->core.attr.exclude_idle   = mod.eI;
1868f5b1135bSJiri Olsa 		evsel->exclude_GH          = mod.exclude_GH;
18693c176311SJiri Olsa 		evsel->sample_read         = mod.sample_read;
18707f94af7aSJiri Olsa 		evsel->precise_max         = mod.precise_max;
18715a5dfe4bSAndi Kleen 		evsel->weak_group	   = mod.weak;
1872e9a7c414SMichael Ellerman 
1873e9a7c414SMichael Ellerman 		if (perf_evsel__is_group_leader(evsel))
18741fc632ceSJiri Olsa 			evsel->core.attr.pinned = mod.pinned;
1875ceb53fbfSIngo Molnar 	}
187686470930SIngo Molnar 
187789812fc8SJiri Olsa 	return 0;
187886470930SIngo Molnar }
187986470930SIngo Molnar 
1880ac2ba9f3SRobert Richter int parse_events_name(struct list_head *list, char *name)
1881ac2ba9f3SRobert Richter {
188232dcd021SJiri Olsa 	struct evsel *evsel;
1883ac2ba9f3SRobert Richter 
1884e5cadb93SArnaldo Carvalho de Melo 	__evlist__for_each_entry(list, evsel) {
1885ac2ba9f3SRobert Richter 		if (!evsel->name)
1886ac2ba9f3SRobert Richter 			evsel->name = strdup(name);
1887ac2ba9f3SRobert Richter 	}
1888ac2ba9f3SRobert Richter 
1889ac2ba9f3SRobert Richter 	return 0;
1890ac2ba9f3SRobert Richter }
1891ac2ba9f3SRobert Richter 
1892dcb4e102SKan Liang static int
1893dcb4e102SKan Liang comp_pmu(const void *p1, const void *p2)
1894dcb4e102SKan Liang {
1895dcb4e102SKan Liang 	struct perf_pmu_event_symbol *pmu1 = (struct perf_pmu_event_symbol *) p1;
1896dcb4e102SKan Liang 	struct perf_pmu_event_symbol *pmu2 = (struct perf_pmu_event_symbol *) p2;
1897dcb4e102SKan Liang 
1898e312bcf1SAndi Kleen 	return strcasecmp(pmu1->symbol, pmu2->symbol);
1899dcb4e102SKan Liang }
1900dcb4e102SKan Liang 
1901dcb4e102SKan Liang static void perf_pmu__parse_cleanup(void)
1902dcb4e102SKan Liang {
1903dcb4e102SKan Liang 	if (perf_pmu_events_list_num > 0) {
1904dcb4e102SKan Liang 		struct perf_pmu_event_symbol *p;
1905dcb4e102SKan Liang 		int i;
1906dcb4e102SKan Liang 
1907dcb4e102SKan Liang 		for (i = 0; i < perf_pmu_events_list_num; i++) {
1908dcb4e102SKan Liang 			p = perf_pmu_events_list + i;
1909360e071bSTaeung Song 			zfree(&p->symbol);
1910dcb4e102SKan Liang 		}
1911506fde11STaeung Song 		zfree(&perf_pmu_events_list);
1912dcb4e102SKan Liang 		perf_pmu_events_list_num = 0;
1913dcb4e102SKan Liang 	}
1914dcb4e102SKan Liang }
1915dcb4e102SKan Liang 
1916dcb4e102SKan Liang #define SET_SYMBOL(str, stype)		\
1917dcb4e102SKan Liang do {					\
1918dcb4e102SKan Liang 	p->symbol = str;		\
1919dcb4e102SKan Liang 	if (!p->symbol)			\
1920dcb4e102SKan Liang 		goto err;		\
1921dcb4e102SKan Liang 	p->type = stype;		\
1922dcb4e102SKan Liang } while (0)
1923dcb4e102SKan Liang 
1924dcb4e102SKan Liang /*
1925dcb4e102SKan Liang  * Read the pmu events list from sysfs
1926dcb4e102SKan Liang  * Save it into perf_pmu_events_list
1927dcb4e102SKan Liang  */
1928dcb4e102SKan Liang static void perf_pmu__parse_init(void)
1929dcb4e102SKan Liang {
1930dcb4e102SKan Liang 
1931dcb4e102SKan Liang 	struct perf_pmu *pmu = NULL;
1932dcb4e102SKan Liang 	struct perf_pmu_alias *alias;
1933dcb4e102SKan Liang 	int len = 0;
1934dcb4e102SKan Liang 
1935231bb2aaSAndi Kleen 	pmu = NULL;
1936231bb2aaSAndi Kleen 	while ((pmu = perf_pmu__scan(pmu)) != NULL) {
1937dcb4e102SKan Liang 		list_for_each_entry(alias, &pmu->aliases, list) {
1938dcb4e102SKan Liang 			if (strchr(alias->name, '-'))
1939dcb4e102SKan Liang 				len++;
1940dcb4e102SKan Liang 			len++;
1941dcb4e102SKan Liang 		}
1942231bb2aaSAndi Kleen 	}
1943231bb2aaSAndi Kleen 
1944231bb2aaSAndi Kleen 	if (len == 0) {
1945231bb2aaSAndi Kleen 		perf_pmu_events_list_num = -1;
1946231bb2aaSAndi Kleen 		return;
1947231bb2aaSAndi Kleen 	}
1948dcb4e102SKan Liang 	perf_pmu_events_list = malloc(sizeof(struct perf_pmu_event_symbol) * len);
1949dcb4e102SKan Liang 	if (!perf_pmu_events_list)
1950dcb4e102SKan Liang 		return;
1951dcb4e102SKan Liang 	perf_pmu_events_list_num = len;
1952dcb4e102SKan Liang 
1953dcb4e102SKan Liang 	len = 0;
1954231bb2aaSAndi Kleen 	pmu = NULL;
1955231bb2aaSAndi Kleen 	while ((pmu = perf_pmu__scan(pmu)) != NULL) {
1956dcb4e102SKan Liang 		list_for_each_entry(alias, &pmu->aliases, list) {
1957dcb4e102SKan Liang 			struct perf_pmu_event_symbol *p = perf_pmu_events_list + len;
1958dcb4e102SKan Liang 			char *tmp = strchr(alias->name, '-');
1959dcb4e102SKan Liang 
1960dcb4e102SKan Liang 			if (tmp != NULL) {
1961dcb4e102SKan Liang 				SET_SYMBOL(strndup(alias->name, tmp - alias->name),
1962dcb4e102SKan Liang 						PMU_EVENT_SYMBOL_PREFIX);
1963dcb4e102SKan Liang 				p++;
1964dcb4e102SKan Liang 				SET_SYMBOL(strdup(++tmp), PMU_EVENT_SYMBOL_SUFFIX);
1965dcb4e102SKan Liang 				len += 2;
1966dcb4e102SKan Liang 			} else {
1967dcb4e102SKan Liang 				SET_SYMBOL(strdup(alias->name), PMU_EVENT_SYMBOL);
1968dcb4e102SKan Liang 				len++;
1969dcb4e102SKan Liang 			}
1970dcb4e102SKan Liang 		}
1971231bb2aaSAndi Kleen 	}
1972dcb4e102SKan Liang 	qsort(perf_pmu_events_list, len,
1973dcb4e102SKan Liang 		sizeof(struct perf_pmu_event_symbol), comp_pmu);
1974dcb4e102SKan Liang 
1975dcb4e102SKan Liang 	return;
1976dcb4e102SKan Liang err:
1977dcb4e102SKan Liang 	perf_pmu__parse_cleanup();
1978dcb4e102SKan Liang }
1979dcb4e102SKan Liang 
1980dcb4e102SKan Liang enum perf_pmu_event_symbol_type
1981dcb4e102SKan Liang perf_pmu__parse_check(const char *name)
1982dcb4e102SKan Liang {
1983dcb4e102SKan Liang 	struct perf_pmu_event_symbol p, *r;
1984dcb4e102SKan Liang 
1985dcb4e102SKan Liang 	/* scan kernel pmu events from sysfs if needed */
1986dcb4e102SKan Liang 	if (perf_pmu_events_list_num == 0)
1987dcb4e102SKan Liang 		perf_pmu__parse_init();
1988dcb4e102SKan Liang 	/*
1989dcb4e102SKan Liang 	 * name "cpu" could be prefix of cpu-cycles or cpu// events.
1990dcb4e102SKan Liang 	 * cpu-cycles has been handled by hardcode.
1991dcb4e102SKan Liang 	 * So it must be cpu// events, not kernel pmu event.
1992dcb4e102SKan Liang 	 */
1993dcb4e102SKan Liang 	if ((perf_pmu_events_list_num <= 0) || !strcmp(name, "cpu"))
1994dcb4e102SKan Liang 		return PMU_EVENT_SYMBOL_ERR;
1995dcb4e102SKan Liang 
1996dcb4e102SKan Liang 	p.symbol = strdup(name);
1997dcb4e102SKan Liang 	r = bsearch(&p, perf_pmu_events_list,
1998dcb4e102SKan Liang 			(size_t) perf_pmu_events_list_num,
1999dcb4e102SKan Liang 			sizeof(struct perf_pmu_event_symbol), comp_pmu);
2000360e071bSTaeung Song 	zfree(&p.symbol);
2001dcb4e102SKan Liang 	return r ? r->type : PMU_EVENT_SYMBOL_ERR;
2002dcb4e102SKan Liang }
2003dcb4e102SKan Liang 
20045d9cdc11SArnaldo Carvalho de Melo static int parse_events__scanner(const char *str, void *parse_state, int start_token)
2005ac20de6fSZheng Yan {
2006ac20de6fSZheng Yan 	YY_BUFFER_STATE buffer;
2007ac20de6fSZheng Yan 	void *scanner;
2008ac20de6fSZheng Yan 	int ret;
2009ac20de6fSZheng Yan 
201090e2b22dSJiri Olsa 	ret = parse_events_lex_init_extra(start_token, &scanner);
2011ac20de6fSZheng Yan 	if (ret)
2012ac20de6fSZheng Yan 		return ret;
2013ac20de6fSZheng Yan 
2014ac20de6fSZheng Yan 	buffer = parse_events__scan_string(str, scanner);
2015ac20de6fSZheng Yan 
2016ac20de6fSZheng Yan #ifdef PARSER_DEBUG
2017ac20de6fSZheng Yan 	parse_events_debug = 1;
2018ac20de6fSZheng Yan #endif
20195d9cdc11SArnaldo Carvalho de Melo 	ret = parse_events_parse(parse_state, scanner);
2020ac20de6fSZheng Yan 
2021ac20de6fSZheng Yan 	parse_events__flush_buffer(buffer, scanner);
2022ac20de6fSZheng Yan 	parse_events__delete_buffer(buffer, scanner);
2023ac20de6fSZheng Yan 	parse_events_lex_destroy(scanner);
2024ac20de6fSZheng Yan 	return ret;
2025ac20de6fSZheng Yan }
2026ac20de6fSZheng Yan 
202790e2b22dSJiri Olsa /*
202890e2b22dSJiri Olsa  * parse event config string, return a list of event terms.
202990e2b22dSJiri Olsa  */
203090e2b22dSJiri Olsa int parse_events_terms(struct list_head *terms, const char *str)
203190e2b22dSJiri Olsa {
20325d9cdc11SArnaldo Carvalho de Melo 	struct parse_events_state parse_state = {
203390e2b22dSJiri Olsa 		.terms = NULL,
203490e2b22dSJiri Olsa 	};
203590e2b22dSJiri Olsa 	int ret;
203690e2b22dSJiri Olsa 
20375d9cdc11SArnaldo Carvalho de Melo 	ret = parse_events__scanner(str, &parse_state, PE_START_TERMS);
203890e2b22dSJiri Olsa 	if (!ret) {
20395d9cdc11SArnaldo Carvalho de Melo 		list_splice(parse_state.terms, terms);
20405d9cdc11SArnaldo Carvalho de Melo 		zfree(&parse_state.terms);
204190e2b22dSJiri Olsa 		return 0;
204290e2b22dSJiri Olsa 	}
204390e2b22dSJiri Olsa 
20445d9cdc11SArnaldo Carvalho de Melo 	parse_events_terms__delete(parse_state.terms);
204590e2b22dSJiri Olsa 	return ret;
204690e2b22dSJiri Olsa }
204790e2b22dSJiri Olsa 
204863503dbaSJiri Olsa int parse_events(struct evlist *evlist, const char *str,
2049b39b8393SJiri Olsa 		 struct parse_events_error *err)
205086470930SIngo Molnar {
20515d9cdc11SArnaldo Carvalho de Melo 	struct parse_events_state parse_state = {
20525d9cdc11SArnaldo Carvalho de Melo 		.list   = LIST_HEAD_INIT(parse_state.list),
20536484d2f9SJiri Olsa 		.idx    = evlist->core.nr_entries,
2054b39b8393SJiri Olsa 		.error  = err,
20557630b3e2SWang Nan 		.evlist = evlist,
205646010ab2SJiri Olsa 	};
205746010ab2SJiri Olsa 	int ret;
205886470930SIngo Molnar 
20595d9cdc11SArnaldo Carvalho de Melo 	ret = parse_events__scanner(str, &parse_state, PE_START_EVENTS);
2060dcb4e102SKan Liang 	perf_pmu__parse_cleanup();
206115bfd2ccSWang Nan 
20628e8714c3SIan Rogers 	if (!ret && list_empty(&parse_state.list)) {
2063c23080a6SArnaldo Carvalho de Melo 		WARN_ONCE(true, "WARNING: event parser found nothing\n");
2064854f7363SWang Nan 		return -1;
2065854f7363SWang Nan 	}
2066854f7363SWang Nan 
20678e8714c3SIan Rogers 	/*
20688e8714c3SIan Rogers 	 * Add list to the evlist even with errors to allow callers to clean up.
20698e8714c3SIan Rogers 	 */
20705d9cdc11SArnaldo Carvalho de Melo 	perf_evlist__splice_list_tail(evlist, &parse_state.list);
20718e8714c3SIan Rogers 
20728e8714c3SIan Rogers 	if (!ret) {
20738e8714c3SIan Rogers 		struct evsel *last;
20748e8714c3SIan Rogers 
20755d9cdc11SArnaldo Carvalho de Melo 		evlist->nr_groups += parse_state.nr_groups;
2076515dbe48SJiri Olsa 		last = evlist__last(evlist);
207715bfd2ccSWang Nan 		last->cmdline_group_boundary = true;
207815bfd2ccSWang Nan 
207986470930SIngo Molnar 		return 0;
208086470930SIngo Molnar 	}
208186470930SIngo Molnar 
20825d7be90eSJiri Olsa 	/*
20835d7be90eSJiri Olsa 	 * There are 2 users - builtin-record and builtin-test objects.
2084c12995a5SJiri Olsa 	 * Both call evlist__delete in case of error, so we dont
20855d7be90eSJiri Olsa 	 * need to bother.
20865d7be90eSJiri Olsa 	 */
208789812fc8SJiri Olsa 	return ret;
208889812fc8SJiri Olsa }
208989812fc8SJiri Olsa 
2090b39b8393SJiri Olsa #define MAX_WIDTH 1000
2091b39b8393SJiri Olsa static int get_term_width(void)
2092b39b8393SJiri Olsa {
2093b39b8393SJiri Olsa 	struct winsize ws;
2094b39b8393SJiri Olsa 
2095b39b8393SJiri Olsa 	get_term_dimensions(&ws);
2096b39b8393SJiri Olsa 	return ws.ws_col > MAX_WIDTH ? MAX_WIDTH : ws.ws_col;
2097b39b8393SJiri Olsa }
2098b39b8393SJiri Olsa 
2099a910e466SIan Rogers static void __parse_events_print_error(int err_idx, const char *err_str,
2100a910e466SIan Rogers 				const char *err_help, const char *event)
2101b39b8393SJiri Olsa {
2102b39b8393SJiri Olsa 	const char *str = "invalid or unsupported event: ";
2103b39b8393SJiri Olsa 	char _buf[MAX_WIDTH];
2104b39b8393SJiri Olsa 	char *buf = (char *) event;
2105b39b8393SJiri Olsa 	int idx = 0;
2106a910e466SIan Rogers 	if (err_str) {
2107b39b8393SJiri Olsa 		/* -2 for extra '' in the final fprintf */
2108b39b8393SJiri Olsa 		int width       = get_term_width() - 2;
2109b39b8393SJiri Olsa 		int len_event   = strlen(event);
2110b39b8393SJiri Olsa 		int len_str, max_len, cut = 0;
2111b39b8393SJiri Olsa 
2112b39b8393SJiri Olsa 		/*
2113b39b8393SJiri Olsa 		 * Maximum error index indent, we will cut
2114b39b8393SJiri Olsa 		 * the event string if it's bigger.
2115b39b8393SJiri Olsa 		 */
2116141b2d31SAdrian Hunter 		int max_err_idx = 13;
2117b39b8393SJiri Olsa 
2118b39b8393SJiri Olsa 		/*
2119b39b8393SJiri Olsa 		 * Let's be specific with the message when
2120b39b8393SJiri Olsa 		 * we have the precise error.
2121b39b8393SJiri Olsa 		 */
2122b39b8393SJiri Olsa 		str     = "event syntax error: ";
2123b39b8393SJiri Olsa 		len_str = strlen(str);
2124b39b8393SJiri Olsa 		max_len = width - len_str;
2125b39b8393SJiri Olsa 
2126b39b8393SJiri Olsa 		buf = _buf;
2127b39b8393SJiri Olsa 
2128bd1a0be5SAdam Buchbinder 		/* We're cutting from the beginning. */
2129a910e466SIan Rogers 		if (err_idx > max_err_idx)
2130a910e466SIan Rogers 			cut = err_idx - max_err_idx;
2131b39b8393SJiri Olsa 
2132b39b8393SJiri Olsa 		strncpy(buf, event + cut, max_len);
2133b39b8393SJiri Olsa 
2134b39b8393SJiri Olsa 		/* Mark cut parts with '..' on both sides. */
2135b39b8393SJiri Olsa 		if (cut)
2136b39b8393SJiri Olsa 			buf[0] = buf[1] = '.';
2137b39b8393SJiri Olsa 
2138b39b8393SJiri Olsa 		if ((len_event - cut) > max_len) {
2139b39b8393SJiri Olsa 			buf[max_len - 1] = buf[max_len - 2] = '.';
2140b39b8393SJiri Olsa 			buf[max_len] = 0;
2141b39b8393SJiri Olsa 		}
2142b39b8393SJiri Olsa 
2143a910e466SIan Rogers 		idx = len_str + err_idx - cut;
2144b39b8393SJiri Olsa 	}
2145b39b8393SJiri Olsa 
2146b39b8393SJiri Olsa 	fprintf(stderr, "%s'%s'\n", str, buf);
2147b39b8393SJiri Olsa 	if (idx) {
2148a910e466SIan Rogers 		fprintf(stderr, "%*s\\___ %s\n", idx + 1, "", err_str);
2149a910e466SIan Rogers 		if (err_help)
2150a910e466SIan Rogers 			fprintf(stderr, "\n%s\n", err_help);
2151a910e466SIan Rogers 	}
2152a910e466SIan Rogers }
2153a910e466SIan Rogers 
2154a910e466SIan Rogers void parse_events_print_error(struct parse_events_error *err,
2155a910e466SIan Rogers 			      const char *event)
2156a910e466SIan Rogers {
2157a910e466SIan Rogers 	if (!err->num_errors)
2158a910e466SIan Rogers 		return;
2159a910e466SIan Rogers 
2160a910e466SIan Rogers 	__parse_events_print_error(err->idx, err->str, err->help, event);
2161360e071bSTaeung Song 	zfree(&err->str);
2162360e071bSTaeung Song 	zfree(&err->help);
2163a910e466SIan Rogers 
2164a910e466SIan Rogers 	if (err->num_errors > 1) {
2165a910e466SIan Rogers 		fputs("\nInitial error:\n", stderr);
2166a910e466SIan Rogers 		__parse_events_print_error(err->first_idx, err->first_str,
2167a910e466SIan Rogers 					err->first_help, event);
2168a910e466SIan Rogers 		zfree(&err->first_str);
2169a910e466SIan Rogers 		zfree(&err->first_help);
2170b39b8393SJiri Olsa 	}
2171b39b8393SJiri Olsa }
2172b39b8393SJiri Olsa 
2173b39b8393SJiri Olsa #undef MAX_WIDTH
2174b39b8393SJiri Olsa 
2175f120f9d5SJiri Olsa int parse_events_option(const struct option *opt, const char *str,
21761d037ca1SIrina Tirdea 			int unset __maybe_unused)
2177f120f9d5SJiri Olsa {
217863503dbaSJiri Olsa 	struct evlist *evlist = *(struct evlist **)opt->value;
2179a910e466SIan Rogers 	struct parse_events_error err;
2180a910e466SIan Rogers 	int ret;
2181a910e466SIan Rogers 
2182a910e466SIan Rogers 	bzero(&err, sizeof(err));
2183a910e466SIan Rogers 	ret = parse_events(evlist, str, &err);
21849175ce1fSAndi Kleen 
2185333b5665SAndi Kleen 	if (ret) {
2186b39b8393SJiri Olsa 		parse_events_print_error(&err, str);
2187333b5665SAndi Kleen 		fprintf(stderr, "Run 'perf list' for a list of valid events\n");
2188333b5665SAndi Kleen 	}
2189b39b8393SJiri Olsa 
21909175ce1fSAndi Kleen 	return ret;
2191f120f9d5SJiri Olsa }
2192f120f9d5SJiri Olsa 
21934ba1faa1SWang Nan static int
219463503dbaSJiri Olsa foreach_evsel_in_last_glob(struct evlist *evlist,
219532dcd021SJiri Olsa 			   int (*func)(struct evsel *evsel,
21964ba1faa1SWang Nan 				       const void *arg),
21974ba1faa1SWang Nan 			   const void *arg)
2198c171b552SLi Zefan {
219932dcd021SJiri Olsa 	struct evsel *last = NULL;
22004ba1faa1SWang Nan 	int err;
2201c171b552SLi Zefan 
2202854f7363SWang Nan 	/*
2203854f7363SWang Nan 	 * Don't return when list_empty, give func a chance to report
2204854f7363SWang Nan 	 * error when it found last == NULL.
2205854f7363SWang Nan 	 *
2206854f7363SWang Nan 	 * So no need to WARN here, let *func do this.
2207854f7363SWang Nan 	 */
22086484d2f9SJiri Olsa 	if (evlist->core.nr_entries > 0)
2209515dbe48SJiri Olsa 		last = evlist__last(evlist);
221069aad6f1SArnaldo Carvalho de Melo 
221115bfd2ccSWang Nan 	do {
22124ba1faa1SWang Nan 		err = (*func)(last, arg);
22134ba1faa1SWang Nan 		if (err)
2214c171b552SLi Zefan 			return -1;
22154ba1faa1SWang Nan 		if (!last)
22164ba1faa1SWang Nan 			return 0;
2217c171b552SLi Zefan 
2218ce9036a6SJiri Olsa 		if (last->core.node.prev == &evlist->core.entries)
221915bfd2ccSWang Nan 			return 0;
2220b27c4eceSJiri Olsa 		last = list_entry(last->core.node.prev, struct evsel, core.node);
222115bfd2ccSWang Nan 	} while (!last->cmdline_group_boundary);
222215bfd2ccSWang Nan 
2223c171b552SLi Zefan 	return 0;
2224c171b552SLi Zefan }
2225c171b552SLi Zefan 
222632dcd021SJiri Olsa static int set_filter(struct evsel *evsel, const void *arg)
22274ba1faa1SWang Nan {
22284ba1faa1SWang Nan 	const char *str = arg;
22291e857484SMathieu Poirier 	bool found = false;
22301e857484SMathieu Poirier 	int nr_addr_filters = 0;
22311e857484SMathieu Poirier 	struct perf_pmu *pmu = NULL;
22324ba1faa1SWang Nan 
223349836f78SJack Henschel 	if (evsel == NULL) {
223449836f78SJack Henschel 		fprintf(stderr,
223549836f78SJack Henschel 			"--filter option should follow a -e tracepoint or HW tracer option\n");
223649836f78SJack Henschel 		return -1;
223749836f78SJack Henschel 	}
22384ba1faa1SWang Nan 
22391fc632ceSJiri Olsa 	if (evsel->core.attr.type == PERF_TYPE_TRACEPOINT) {
22403541c034SMathieu Poirier 		if (perf_evsel__append_tp_filter(evsel, str) < 0) {
22414ba1faa1SWang Nan 			fprintf(stderr,
22424ba1faa1SWang Nan 				"not enough memory to hold filter string\n");
22434ba1faa1SWang Nan 			return -1;
22444ba1faa1SWang Nan 		}
22454ba1faa1SWang Nan 
22464ba1faa1SWang Nan 		return 0;
22474ba1faa1SWang Nan 	}
22484ba1faa1SWang Nan 
22491e857484SMathieu Poirier 	while ((pmu = perf_pmu__scan(pmu)) != NULL)
22501fc632ceSJiri Olsa 		if (pmu->type == evsel->core.attr.type) {
22511e857484SMathieu Poirier 			found = true;
22521e857484SMathieu Poirier 			break;
22531e857484SMathieu Poirier 		}
22541e857484SMathieu Poirier 
22551e857484SMathieu Poirier 	if (found)
22561e857484SMathieu Poirier 		perf_pmu__scan_file(pmu, "nr_addr_filters",
22571e857484SMathieu Poirier 				    "%d", &nr_addr_filters);
22581e857484SMathieu Poirier 
225949836f78SJack Henschel 	if (!nr_addr_filters) {
226049836f78SJack Henschel 		fprintf(stderr,
226149836f78SJack Henschel 			"This CPU does not support address filtering\n");
226249836f78SJack Henschel 		return -1;
226349836f78SJack Henschel 	}
22641e857484SMathieu Poirier 
22651e857484SMathieu Poirier 	if (perf_evsel__append_addr_filter(evsel, str) < 0) {
22661e857484SMathieu Poirier 		fprintf(stderr,
22671e857484SMathieu Poirier 			"not enough memory to hold filter string\n");
22681e857484SMathieu Poirier 		return -1;
22691e857484SMathieu Poirier 	}
22701e857484SMathieu Poirier 
22711e857484SMathieu Poirier 	return 0;
22721e857484SMathieu Poirier }
22731e857484SMathieu Poirier 
22744ba1faa1SWang Nan int parse_filter(const struct option *opt, const char *str,
22754ba1faa1SWang Nan 		 int unset __maybe_unused)
22764ba1faa1SWang Nan {
227763503dbaSJiri Olsa 	struct evlist *evlist = *(struct evlist **)opt->value;
22784ba1faa1SWang Nan 
22794ba1faa1SWang Nan 	return foreach_evsel_in_last_glob(evlist, set_filter,
22804ba1faa1SWang Nan 					  (const void *)str);
22814ba1faa1SWang Nan }
22824ba1faa1SWang Nan 
228332dcd021SJiri Olsa static int add_exclude_perf_filter(struct evsel *evsel,
22844ba1faa1SWang Nan 				   const void *arg __maybe_unused)
22854ba1faa1SWang Nan {
22864ba1faa1SWang Nan 	char new_filter[64];
22874ba1faa1SWang Nan 
22881fc632ceSJiri Olsa 	if (evsel == NULL || evsel->core.attr.type != PERF_TYPE_TRACEPOINT) {
22894ba1faa1SWang Nan 		fprintf(stderr,
22904ba1faa1SWang Nan 			"--exclude-perf option should follow a -e tracepoint option\n");
22914ba1faa1SWang Nan 		return -1;
22924ba1faa1SWang Nan 	}
22934ba1faa1SWang Nan 
22944ba1faa1SWang Nan 	snprintf(new_filter, sizeof(new_filter), "common_pid != %d", getpid());
22954ba1faa1SWang Nan 
22963541c034SMathieu Poirier 	if (perf_evsel__append_tp_filter(evsel, new_filter) < 0) {
22974ba1faa1SWang Nan 		fprintf(stderr,
22984ba1faa1SWang Nan 			"not enough memory to hold filter string\n");
22994ba1faa1SWang Nan 		return -1;
23004ba1faa1SWang Nan 	}
23014ba1faa1SWang Nan 
23024ba1faa1SWang Nan 	return 0;
23034ba1faa1SWang Nan }
23044ba1faa1SWang Nan 
23054ba1faa1SWang Nan int exclude_perf(const struct option *opt,
23064ba1faa1SWang Nan 		 const char *arg __maybe_unused,
23074ba1faa1SWang Nan 		 int unset __maybe_unused)
23084ba1faa1SWang Nan {
230963503dbaSJiri Olsa 	struct evlist *evlist = *(struct evlist **)opt->value;
23104ba1faa1SWang Nan 
23114ba1faa1SWang Nan 	return foreach_evsel_in_last_glob(evlist, add_exclude_perf_filter,
23124ba1faa1SWang Nan 					  NULL);
23134ba1faa1SWang Nan }
23144ba1faa1SWang Nan 
231586470930SIngo Molnar static const char * const event_type_descriptors[] = {
231686470930SIngo Molnar 	"Hardware event",
231786470930SIngo Molnar 	"Software event",
231886470930SIngo Molnar 	"Tracepoint event",
231986470930SIngo Molnar 	"Hardware cache event",
232041bdcb23SLiming Wang 	"Raw hardware event descriptor",
232141bdcb23SLiming Wang 	"Hardware breakpoint",
232286470930SIngo Molnar };
232386470930SIngo Molnar 
2324ab0e4800SYunlong Song static int cmp_string(const void *a, const void *b)
2325ab0e4800SYunlong Song {
2326ab0e4800SYunlong Song 	const char * const *as = a;
2327ab0e4800SYunlong Song 	const char * const *bs = b;
2328ab0e4800SYunlong Song 
2329ab0e4800SYunlong Song 	return strcmp(*as, *bs);
2330ab0e4800SYunlong Song }
2331ab0e4800SYunlong Song 
233286470930SIngo Molnar /*
2333f6bdafefSJason Baron  * Print the events from <debugfs_mount_point>/tracing/events
2334f6bdafefSJason Baron  */
2335f6bdafefSJason Baron 
2336a3277d2dSFrederic Weisbecker void print_tracepoint_events(const char *subsys_glob, const char *event_glob,
2337a3277d2dSFrederic Weisbecker 			     bool name_only)
2338f6bdafefSJason Baron {
2339f6bdafefSJason Baron 	DIR *sys_dir, *evt_dir;
234022a9f41bSArnaldo Carvalho de Melo 	struct dirent *sys_dirent, *evt_dirent;
2341f6bdafefSJason Baron 	char evt_path[MAXPATHLEN];
234225a7d914SArnaldo Carvalho de Melo 	char *dir_path;
2343ab0e4800SYunlong Song 	char **evt_list = NULL;
2344ab0e4800SYunlong Song 	unsigned int evt_i = 0, evt_num = 0;
2345ab0e4800SYunlong Song 	bool evt_num_known = false;
2346f6bdafefSJason Baron 
2347ab0e4800SYunlong Song restart:
23487014e0e3SArnaldo Carvalho de Melo 	sys_dir = tracing_events__opendir();
2349f6bdafefSJason Baron 	if (!sys_dir)
2350725b1368SEric Dumazet 		return;
2351f6bdafefSJason Baron 
2352ab0e4800SYunlong Song 	if (evt_num_known) {
2353ab0e4800SYunlong Song 		evt_list = zalloc(sizeof(char *) * evt_num);
2354ab0e4800SYunlong Song 		if (!evt_list)
2355ab0e4800SYunlong Song 			goto out_close_sys_dir;
2356ab0e4800SYunlong Song 	}
2357ab0e4800SYunlong Song 
235822a9f41bSArnaldo Carvalho de Melo 	for_each_subsystem(sys_dir, sys_dirent) {
2359668b8788SArnaldo Carvalho de Melo 		if (subsys_glob != NULL &&
236022a9f41bSArnaldo Carvalho de Melo 		    !strglobmatch(sys_dirent->d_name, subsys_glob))
2361668b8788SArnaldo Carvalho de Melo 			continue;
2362725b1368SEric Dumazet 
236325a7d914SArnaldo Carvalho de Melo 		dir_path = get_events_file(sys_dirent->d_name);
236425a7d914SArnaldo Carvalho de Melo 		if (!dir_path)
236525a7d914SArnaldo Carvalho de Melo 			continue;
2366725b1368SEric Dumazet 		evt_dir = opendir(dir_path);
2367725b1368SEric Dumazet 		if (!evt_dir)
236825a7d914SArnaldo Carvalho de Melo 			goto next;
2369725b1368SEric Dumazet 
2370c02cab22SArnaldo Carvalho de Melo 		for_each_event(dir_path, evt_dir, evt_dirent) {
2371668b8788SArnaldo Carvalho de Melo 			if (event_glob != NULL &&
237222a9f41bSArnaldo Carvalho de Melo 			    !strglobmatch(evt_dirent->d_name, event_glob))
2373668b8788SArnaldo Carvalho de Melo 				continue;
2374668b8788SArnaldo Carvalho de Melo 
2375ab0e4800SYunlong Song 			if (!evt_num_known) {
2376ab0e4800SYunlong Song 				evt_num++;
2377a3277d2dSFrederic Weisbecker 				continue;
2378a3277d2dSFrederic Weisbecker 			}
2379a3277d2dSFrederic Weisbecker 
2380f6bdafefSJason Baron 			snprintf(evt_path, MAXPATHLEN, "%s:%s",
238122a9f41bSArnaldo Carvalho de Melo 				 sys_dirent->d_name, evt_dirent->d_name);
2382ab0e4800SYunlong Song 
2383ab0e4800SYunlong Song 			evt_list[evt_i] = strdup(evt_path);
238425a7d914SArnaldo Carvalho de Melo 			if (evt_list[evt_i] == NULL) {
238525a7d914SArnaldo Carvalho de Melo 				put_events_file(dir_path);
2386ab0e4800SYunlong Song 				goto out_close_evt_dir;
238725a7d914SArnaldo Carvalho de Melo 			}
2388ab0e4800SYunlong Song 			evt_i++;
2389f6bdafefSJason Baron 		}
2390f6bdafefSJason Baron 		closedir(evt_dir);
239125a7d914SArnaldo Carvalho de Melo next:
239225a7d914SArnaldo Carvalho de Melo 		put_events_file(dir_path);
2393f6bdafefSJason Baron 	}
2394f6bdafefSJason Baron 	closedir(sys_dir);
2395ab0e4800SYunlong Song 
2396ab0e4800SYunlong Song 	if (!evt_num_known) {
2397ab0e4800SYunlong Song 		evt_num_known = true;
2398ab0e4800SYunlong Song 		goto restart;
2399ab0e4800SYunlong Song 	}
2400ab0e4800SYunlong Song 	qsort(evt_list, evt_num, sizeof(char *), cmp_string);
2401ab0e4800SYunlong Song 	evt_i = 0;
2402ab0e4800SYunlong Song 	while (evt_i < evt_num) {
2403ab0e4800SYunlong Song 		if (name_only) {
2404ab0e4800SYunlong Song 			printf("%s ", evt_list[evt_i++]);
2405ab0e4800SYunlong Song 			continue;
2406ab0e4800SYunlong Song 		}
2407ab0e4800SYunlong Song 		printf("  %-50s [%s]\n", evt_list[evt_i++],
2408ab0e4800SYunlong Song 				event_type_descriptors[PERF_TYPE_TRACEPOINT]);
2409ab0e4800SYunlong Song 	}
2410dfc431cbSArnaldo Carvalho de Melo 	if (evt_num && pager_in_use())
2411ab0e4800SYunlong Song 		printf("\n");
2412ab0e4800SYunlong Song 
2413ab0e4800SYunlong Song out_free:
2414ab0e4800SYunlong Song 	evt_num = evt_i;
2415ab0e4800SYunlong Song 	for (evt_i = 0; evt_i < evt_num; evt_i++)
2416ab0e4800SYunlong Song 		zfree(&evt_list[evt_i]);
2417ab0e4800SYunlong Song 	zfree(&evt_list);
2418ab0e4800SYunlong Song 	return;
2419ab0e4800SYunlong Song 
2420ab0e4800SYunlong Song out_close_evt_dir:
2421ab0e4800SYunlong Song 	closedir(evt_dir);
2422ab0e4800SYunlong Song out_close_sys_dir:
2423ab0e4800SYunlong Song 	closedir(sys_dir);
2424ab0e4800SYunlong Song 
2425ab0e4800SYunlong Song 	printf("FATAL: not enough memory to print %s\n",
2426ab0e4800SYunlong Song 			event_type_descriptors[PERF_TYPE_TRACEPOINT]);
2427ab0e4800SYunlong Song 	if (evt_list)
2428ab0e4800SYunlong Song 		goto out_free;
2429f6bdafefSJason Baron }
2430f6bdafefSJason Baron 
2431f6bdafefSJason Baron /*
243220c457b8SThomas Renninger  * Check whether event is in <debugfs_mount_point>/tracing/events
243320c457b8SThomas Renninger  */
243420c457b8SThomas Renninger 
243520c457b8SThomas Renninger int is_valid_tracepoint(const char *event_string)
243620c457b8SThomas Renninger {
243720c457b8SThomas Renninger 	DIR *sys_dir, *evt_dir;
243822a9f41bSArnaldo Carvalho de Melo 	struct dirent *sys_dirent, *evt_dirent;
243920c457b8SThomas Renninger 	char evt_path[MAXPATHLEN];
244025a7d914SArnaldo Carvalho de Melo 	char *dir_path;
244120c457b8SThomas Renninger 
24427014e0e3SArnaldo Carvalho de Melo 	sys_dir = tracing_events__opendir();
244320c457b8SThomas Renninger 	if (!sys_dir)
244420c457b8SThomas Renninger 		return 0;
244520c457b8SThomas Renninger 
244622a9f41bSArnaldo Carvalho de Melo 	for_each_subsystem(sys_dir, sys_dirent) {
244725a7d914SArnaldo Carvalho de Melo 		dir_path = get_events_file(sys_dirent->d_name);
244825a7d914SArnaldo Carvalho de Melo 		if (!dir_path)
244925a7d914SArnaldo Carvalho de Melo 			continue;
245020c457b8SThomas Renninger 		evt_dir = opendir(dir_path);
245120c457b8SThomas Renninger 		if (!evt_dir)
245225a7d914SArnaldo Carvalho de Melo 			goto next;
245320c457b8SThomas Renninger 
2454c02cab22SArnaldo Carvalho de Melo 		for_each_event(dir_path, evt_dir, evt_dirent) {
245520c457b8SThomas Renninger 			snprintf(evt_path, MAXPATHLEN, "%s:%s",
245622a9f41bSArnaldo Carvalho de Melo 				 sys_dirent->d_name, evt_dirent->d_name);
245720c457b8SThomas Renninger 			if (!strcmp(evt_path, event_string)) {
245820c457b8SThomas Renninger 				closedir(evt_dir);
245920c457b8SThomas Renninger 				closedir(sys_dir);
246020c457b8SThomas Renninger 				return 1;
246120c457b8SThomas Renninger 			}
246220c457b8SThomas Renninger 		}
246320c457b8SThomas Renninger 		closedir(evt_dir);
246425a7d914SArnaldo Carvalho de Melo next:
246525a7d914SArnaldo Carvalho de Melo 		put_events_file(dir_path);
246620c457b8SThomas Renninger 	}
246720c457b8SThomas Renninger 	closedir(sys_dir);
246820c457b8SThomas Renninger 	return 0;
246920c457b8SThomas Renninger }
247020c457b8SThomas Renninger 
2471b41f1cecSNamhyung Kim static bool is_event_supported(u8 type, unsigned config)
2472b41f1cecSNamhyung Kim {
2473b41f1cecSNamhyung Kim 	bool ret = true;
247488fee52eSVince Weaver 	int open_return;
247532dcd021SJiri Olsa 	struct evsel *evsel;
2476b41f1cecSNamhyung Kim 	struct perf_event_attr attr = {
2477b41f1cecSNamhyung Kim 		.type = type,
2478b41f1cecSNamhyung Kim 		.config = config,
2479b41f1cecSNamhyung Kim 		.disabled = 1,
2480b41f1cecSNamhyung Kim 	};
24819749b90eSJiri Olsa 	struct perf_thread_map *tmap = thread_map__new_by_tid(0);
248289896051SArnaldo Carvalho de Melo 
248389896051SArnaldo Carvalho de Melo 	if (tmap == NULL)
248489896051SArnaldo Carvalho de Melo 		return false;
2485b41f1cecSNamhyung Kim 
2486365c3ae7SJiri Olsa 	evsel = evsel__new(&attr);
2487b41f1cecSNamhyung Kim 	if (evsel) {
24885972d1e0SJiri Olsa 		open_return = evsel__open(evsel, NULL, tmap);
248988fee52eSVince Weaver 		ret = open_return >= 0;
249088fee52eSVince Weaver 
249188fee52eSVince Weaver 		if (open_return == -EACCES) {
249288fee52eSVince Weaver 			/*
249388fee52eSVince Weaver 			 * This happens if the paranoid value
249488fee52eSVince Weaver 			 * /proc/sys/kernel/perf_event_paranoid is set to 2
249588fee52eSVince Weaver 			 * Re-run with exclude_kernel set; we don't do that
249688fee52eSVince Weaver 			 * by default as some ARM machines do not support it.
249788fee52eSVince Weaver 			 *
249888fee52eSVince Weaver 			 */
24991fc632ceSJiri Olsa 			evsel->core.attr.exclude_kernel = 1;
25005972d1e0SJiri Olsa 			ret = evsel__open(evsel, NULL, tmap) >= 0;
250188fee52eSVince Weaver 		}
25025eb2dd2aSJiri Olsa 		evsel__delete(evsel);
2503b41f1cecSNamhyung Kim 	}
2504b41f1cecSNamhyung Kim 
25057836e52eSJiri Olsa 	perf_thread_map__put(tmap);
2506b41f1cecSNamhyung Kim 	return ret;
2507b41f1cecSNamhyung Kim }
2508b41f1cecSNamhyung Kim 
250940218daeSMasami Hiramatsu void print_sdt_events(const char *subsys_glob, const char *event_glob,
251040218daeSMasami Hiramatsu 		      bool name_only)
251140218daeSMasami Hiramatsu {
251240218daeSMasami Hiramatsu 	struct probe_cache *pcache;
251340218daeSMasami Hiramatsu 	struct probe_cache_entry *ent;
251440218daeSMasami Hiramatsu 	struct strlist *bidlist, *sdtlist;
251540218daeSMasami Hiramatsu 	struct strlist_config cfg = {.dont_dupstr = true};
251640218daeSMasami Hiramatsu 	struct str_node *nd, *nd2;
251740218daeSMasami Hiramatsu 	char *buf, *path, *ptr = NULL;
251840218daeSMasami Hiramatsu 	bool show_detail = false;
251940218daeSMasami Hiramatsu 	int ret;
252040218daeSMasami Hiramatsu 
252140218daeSMasami Hiramatsu 	sdtlist = strlist__new(NULL, &cfg);
252240218daeSMasami Hiramatsu 	if (!sdtlist) {
252340218daeSMasami Hiramatsu 		pr_debug("Failed to allocate new strlist for SDT\n");
252440218daeSMasami Hiramatsu 		return;
252540218daeSMasami Hiramatsu 	}
252640218daeSMasami Hiramatsu 	bidlist = build_id_cache__list_all(true);
252740218daeSMasami Hiramatsu 	if (!bidlist) {
252840218daeSMasami Hiramatsu 		pr_debug("Failed to get buildids: %d\n", errno);
252940218daeSMasami Hiramatsu 		return;
253040218daeSMasami Hiramatsu 	}
253140218daeSMasami Hiramatsu 	strlist__for_each_entry(nd, bidlist) {
2532f045b8c4SKrister Johansen 		pcache = probe_cache__new(nd->s, NULL);
253340218daeSMasami Hiramatsu 		if (!pcache)
253440218daeSMasami Hiramatsu 			continue;
253540218daeSMasami Hiramatsu 		list_for_each_entry(ent, &pcache->entries, node) {
253640218daeSMasami Hiramatsu 			if (!ent->sdt)
253740218daeSMasami Hiramatsu 				continue;
253840218daeSMasami Hiramatsu 			if (subsys_glob &&
253940218daeSMasami Hiramatsu 			    !strglobmatch(ent->pev.group, subsys_glob))
254040218daeSMasami Hiramatsu 				continue;
254140218daeSMasami Hiramatsu 			if (event_glob &&
254240218daeSMasami Hiramatsu 			    !strglobmatch(ent->pev.event, event_glob))
254340218daeSMasami Hiramatsu 				continue;
254440218daeSMasami Hiramatsu 			ret = asprintf(&buf, "%s:%s@%s", ent->pev.group,
254540218daeSMasami Hiramatsu 					ent->pev.event, nd->s);
254640218daeSMasami Hiramatsu 			if (ret > 0)
254740218daeSMasami Hiramatsu 				strlist__add(sdtlist, buf);
254840218daeSMasami Hiramatsu 		}
254940218daeSMasami Hiramatsu 		probe_cache__delete(pcache);
255040218daeSMasami Hiramatsu 	}
255140218daeSMasami Hiramatsu 	strlist__delete(bidlist);
255240218daeSMasami Hiramatsu 
255340218daeSMasami Hiramatsu 	strlist__for_each_entry(nd, sdtlist) {
255440218daeSMasami Hiramatsu 		buf = strchr(nd->s, '@');
255540218daeSMasami Hiramatsu 		if (buf)
255640218daeSMasami Hiramatsu 			*(buf++) = '\0';
255740218daeSMasami Hiramatsu 		if (name_only) {
255840218daeSMasami Hiramatsu 			printf("%s ", nd->s);
255940218daeSMasami Hiramatsu 			continue;
256040218daeSMasami Hiramatsu 		}
256140218daeSMasami Hiramatsu 		nd2 = strlist__next(nd);
256240218daeSMasami Hiramatsu 		if (nd2) {
256340218daeSMasami Hiramatsu 			ptr = strchr(nd2->s, '@');
256440218daeSMasami Hiramatsu 			if (ptr)
256540218daeSMasami Hiramatsu 				*ptr = '\0';
256640218daeSMasami Hiramatsu 			if (strcmp(nd->s, nd2->s) == 0)
256740218daeSMasami Hiramatsu 				show_detail = true;
256840218daeSMasami Hiramatsu 		}
256940218daeSMasami Hiramatsu 		if (show_detail) {
257040218daeSMasami Hiramatsu 			path = build_id_cache__origname(buf);
257140218daeSMasami Hiramatsu 			ret = asprintf(&buf, "%s@%s(%.12s)", nd->s, path, buf);
257240218daeSMasami Hiramatsu 			if (ret > 0) {
257340218daeSMasami Hiramatsu 				printf("  %-50s [%s]\n", buf, "SDT event");
257440218daeSMasami Hiramatsu 				free(buf);
257540218daeSMasami Hiramatsu 			}
25768bde8516SChangbin Du 			free(path);
257740218daeSMasami Hiramatsu 		} else
257840218daeSMasami Hiramatsu 			printf("  %-50s [%s]\n", nd->s, "SDT event");
257940218daeSMasami Hiramatsu 		if (nd2) {
258040218daeSMasami Hiramatsu 			if (strcmp(nd->s, nd2->s) != 0)
258140218daeSMasami Hiramatsu 				show_detail = false;
258240218daeSMasami Hiramatsu 			if (ptr)
258340218daeSMasami Hiramatsu 				*ptr = '@';
258440218daeSMasami Hiramatsu 		}
258540218daeSMasami Hiramatsu 	}
258640218daeSMasami Hiramatsu 	strlist__delete(sdtlist);
258740218daeSMasami Hiramatsu }
258840218daeSMasami Hiramatsu 
2589a3277d2dSFrederic Weisbecker int print_hwcache_events(const char *event_glob, bool name_only)
2590668b8788SArnaldo Carvalho de Melo {
2591ab0e4800SYunlong Song 	unsigned int type, op, i, evt_i = 0, evt_num = 0;
25920b668bc9SArnaldo Carvalho de Melo 	char name[64];
2593ab0e4800SYunlong Song 	char **evt_list = NULL;
2594ab0e4800SYunlong Song 	bool evt_num_known = false;
2595ab0e4800SYunlong Song 
2596ab0e4800SYunlong Song restart:
2597ab0e4800SYunlong Song 	if (evt_num_known) {
2598ab0e4800SYunlong Song 		evt_list = zalloc(sizeof(char *) * evt_num);
2599ab0e4800SYunlong Song 		if (!evt_list)
2600ab0e4800SYunlong Song 			goto out_enomem;
2601ab0e4800SYunlong Song 	}
2602668b8788SArnaldo Carvalho de Melo 
2603668b8788SArnaldo Carvalho de Melo 	for (type = 0; type < PERF_COUNT_HW_CACHE_MAX; type++) {
2604668b8788SArnaldo Carvalho de Melo 		for (op = 0; op < PERF_COUNT_HW_CACHE_OP_MAX; op++) {
2605668b8788SArnaldo Carvalho de Melo 			/* skip invalid cache type */
26060b668bc9SArnaldo Carvalho de Melo 			if (!perf_evsel__is_cache_op_valid(type, op))
2607668b8788SArnaldo Carvalho de Melo 				continue;
2608668b8788SArnaldo Carvalho de Melo 
2609668b8788SArnaldo Carvalho de Melo 			for (i = 0; i < PERF_COUNT_HW_CACHE_RESULT_MAX; i++) {
26100b668bc9SArnaldo Carvalho de Melo 				__perf_evsel__hw_cache_type_op_res_name(type, op, i,
26110b668bc9SArnaldo Carvalho de Melo 									name, sizeof(name));
2612947b4ad1SIngo Molnar 				if (event_glob != NULL && !strglobmatch(name, event_glob))
2613668b8788SArnaldo Carvalho de Melo 					continue;
2614668b8788SArnaldo Carvalho de Melo 
2615b41f1cecSNamhyung Kim 				if (!is_event_supported(PERF_TYPE_HW_CACHE,
2616b41f1cecSNamhyung Kim 							type | (op << 8) | (i << 16)))
2617b41f1cecSNamhyung Kim 					continue;
2618b41f1cecSNamhyung Kim 
2619ab0e4800SYunlong Song 				if (!evt_num_known) {
2620ab0e4800SYunlong Song 					evt_num++;
2621ab0e4800SYunlong Song 					continue;
2622ab0e4800SYunlong Song 				}
2623ab0e4800SYunlong Song 
2624ab0e4800SYunlong Song 				evt_list[evt_i] = strdup(name);
2625ab0e4800SYunlong Song 				if (evt_list[evt_i] == NULL)
2626ab0e4800SYunlong Song 					goto out_enomem;
2627ab0e4800SYunlong Song 				evt_i++;
2628668b8788SArnaldo Carvalho de Melo 			}
2629668b8788SArnaldo Carvalho de Melo 		}
2630668b8788SArnaldo Carvalho de Melo 	}
2631668b8788SArnaldo Carvalho de Melo 
2632ab0e4800SYunlong Song 	if (!evt_num_known) {
2633ab0e4800SYunlong Song 		evt_num_known = true;
2634ab0e4800SYunlong Song 		goto restart;
2635ab0e4800SYunlong Song 	}
2636ab0e4800SYunlong Song 	qsort(evt_list, evt_num, sizeof(char *), cmp_string);
2637ab0e4800SYunlong Song 	evt_i = 0;
2638ab0e4800SYunlong Song 	while (evt_i < evt_num) {
2639ab0e4800SYunlong Song 		if (name_only) {
2640ab0e4800SYunlong Song 			printf("%s ", evt_list[evt_i++]);
2641ab0e4800SYunlong Song 			continue;
2642ab0e4800SYunlong Song 		}
2643ab0e4800SYunlong Song 		printf("  %-50s [%s]\n", evt_list[evt_i++],
2644ab0e4800SYunlong Song 				event_type_descriptors[PERF_TYPE_HW_CACHE]);
2645ab0e4800SYunlong Song 	}
2646dfc431cbSArnaldo Carvalho de Melo 	if (evt_num && pager_in_use())
2647dc098b35SAndi Kleen 		printf("\n");
2648ab0e4800SYunlong Song 
2649ab0e4800SYunlong Song out_free:
2650ab0e4800SYunlong Song 	evt_num = evt_i;
2651ab0e4800SYunlong Song 	for (evt_i = 0; evt_i < evt_num; evt_i++)
2652ab0e4800SYunlong Song 		zfree(&evt_list[evt_i]);
2653ab0e4800SYunlong Song 	zfree(&evt_list);
2654ab0e4800SYunlong Song 	return evt_num;
2655ab0e4800SYunlong Song 
2656ab0e4800SYunlong Song out_enomem:
2657ab0e4800SYunlong Song 	printf("FATAL: not enough memory to print %s\n", event_type_descriptors[PERF_TYPE_HW_CACHE]);
2658ab0e4800SYunlong Song 	if (evt_list)
2659ab0e4800SYunlong Song 		goto out_free;
2660ab0e4800SYunlong Song 	return evt_num;
2661668b8788SArnaldo Carvalho de Melo }
2662668b8788SArnaldo Carvalho de Melo 
26635e0861baSAndi Kleen static void print_tool_event(const char *name, const char *event_glob,
26645e0861baSAndi Kleen 			     bool name_only)
26655e0861baSAndi Kleen {
26665e0861baSAndi Kleen 	if (event_glob && !strglobmatch(name, event_glob))
26675e0861baSAndi Kleen 		return;
26685e0861baSAndi Kleen 	if (name_only)
26695e0861baSAndi Kleen 		printf("%s ", name);
26705e0861baSAndi Kleen 	else
26715e0861baSAndi Kleen 		printf("  %-50s [%s]\n", name, "Tool event");
26725e0861baSAndi Kleen 
26735e0861baSAndi Kleen }
26745e0861baSAndi Kleen 
26755e0861baSAndi Kleen void print_tool_events(const char *event_glob, bool name_only)
26765e0861baSAndi Kleen {
26775e0861baSAndi Kleen 	print_tool_event("duration_time", event_glob, name_only);
26785e0861baSAndi Kleen 	if (pager_in_use())
26795e0861baSAndi Kleen 		printf("\n");
26805e0861baSAndi Kleen }
26815e0861baSAndi Kleen 
2682705750f2SYunlong Song void print_symbol_events(const char *event_glob, unsigned type,
2683a3277d2dSFrederic Weisbecker 				struct event_symbol *syms, unsigned max,
2684a3277d2dSFrederic Weisbecker 				bool name_only)
268586470930SIngo Molnar {
2686ab0e4800SYunlong Song 	unsigned int i, evt_i = 0, evt_num = 0;
2687947b4ad1SIngo Molnar 	char name[MAX_NAME_LEN];
2688ab0e4800SYunlong Song 	char **evt_list = NULL;
2689ab0e4800SYunlong Song 	bool evt_num_known = false;
2690ab0e4800SYunlong Song 
2691ab0e4800SYunlong Song restart:
2692ab0e4800SYunlong Song 	if (evt_num_known) {
2693ab0e4800SYunlong Song 		evt_list = zalloc(sizeof(char *) * evt_num);
2694ab0e4800SYunlong Song 		if (!evt_list)
2695ab0e4800SYunlong Song 			goto out_enomem;
2696ab0e4800SYunlong Song 		syms -= max;
2697ab0e4800SYunlong Song 	}
269886470930SIngo Molnar 
26991dc12760SJiri Olsa 	for (i = 0; i < max; i++, syms++) {
2700668b8788SArnaldo Carvalho de Melo 
2701e37df6c7SArnaldo Carvalho de Melo 		if (event_glob != NULL && syms->symbol != NULL &&
2702668b8788SArnaldo Carvalho de Melo 		    !(strglobmatch(syms->symbol, event_glob) ||
2703668b8788SArnaldo Carvalho de Melo 		      (syms->alias && strglobmatch(syms->alias, event_glob))))
2704668b8788SArnaldo Carvalho de Melo 			continue;
270586470930SIngo Molnar 
2706b41f1cecSNamhyung Kim 		if (!is_event_supported(type, i))
2707b41f1cecSNamhyung Kim 			continue;
2708b41f1cecSNamhyung Kim 
2709ab0e4800SYunlong Song 		if (!evt_num_known) {
2710ab0e4800SYunlong Song 			evt_num++;
2711a3277d2dSFrederic Weisbecker 			continue;
2712a3277d2dSFrederic Weisbecker 		}
2713a3277d2dSFrederic Weisbecker 
2714ab0e4800SYunlong Song 		if (!name_only && strlen(syms->alias))
2715947b4ad1SIngo Molnar 			snprintf(name, MAX_NAME_LEN, "%s OR %s", syms->symbol, syms->alias);
271674d5b588SJaswinder Singh Rajput 		else
2717bd8d57fbSArnaldo Carvalho de Melo 			strlcpy(name, syms->symbol, MAX_NAME_LEN);
271886470930SIngo Molnar 
2719ab0e4800SYunlong Song 		evt_list[evt_i] = strdup(name);
2720ab0e4800SYunlong Song 		if (evt_list[evt_i] == NULL)
2721ab0e4800SYunlong Song 			goto out_enomem;
2722ab0e4800SYunlong Song 		evt_i++;
272386470930SIngo Molnar 	}
272486470930SIngo Molnar 
2725ab0e4800SYunlong Song 	if (!evt_num_known) {
2726ab0e4800SYunlong Song 		evt_num_known = true;
2727ab0e4800SYunlong Song 		goto restart;
2728ab0e4800SYunlong Song 	}
2729ab0e4800SYunlong Song 	qsort(evt_list, evt_num, sizeof(char *), cmp_string);
2730ab0e4800SYunlong Song 	evt_i = 0;
2731ab0e4800SYunlong Song 	while (evt_i < evt_num) {
2732ab0e4800SYunlong Song 		if (name_only) {
2733ab0e4800SYunlong Song 			printf("%s ", evt_list[evt_i++]);
2734ab0e4800SYunlong Song 			continue;
2735ab0e4800SYunlong Song 		}
2736ab0e4800SYunlong Song 		printf("  %-50s [%s]\n", evt_list[evt_i++], event_type_descriptors[type]);
2737ab0e4800SYunlong Song 	}
2738dfc431cbSArnaldo Carvalho de Melo 	if (evt_num && pager_in_use())
2739689d3018SMarti Raudsepp 		printf("\n");
2740ab0e4800SYunlong Song 
2741ab0e4800SYunlong Song out_free:
2742ab0e4800SYunlong Song 	evt_num = evt_i;
2743ab0e4800SYunlong Song 	for (evt_i = 0; evt_i < evt_num; evt_i++)
2744ab0e4800SYunlong Song 		zfree(&evt_list[evt_i]);
2745ab0e4800SYunlong Song 	zfree(&evt_list);
2746ab0e4800SYunlong Song 	return;
2747ab0e4800SYunlong Song 
2748ab0e4800SYunlong Song out_enomem:
2749ab0e4800SYunlong Song 	printf("FATAL: not enough memory to print %s\n", event_type_descriptors[type]);
2750ab0e4800SYunlong Song 	if (evt_list)
2751ab0e4800SYunlong Song 		goto out_free;
2752668b8788SArnaldo Carvalho de Melo }
27531dc12760SJiri Olsa 
27541dc12760SJiri Olsa /*
27551dc12760SJiri Olsa  * Print the help text for the event symbols:
27561dc12760SJiri Olsa  */
2757c8d6828aSSukadev Bhattiprolu void print_events(const char *event_glob, bool name_only, bool quiet_flag,
2758a7f6c8c8SJin Yao 			bool long_desc, bool details_flag, bool deprecated)
27591dc12760SJiri Olsa {
27601dc12760SJiri Olsa 	print_symbol_events(event_glob, PERF_TYPE_HARDWARE,
2761a3277d2dSFrederic Weisbecker 			    event_symbols_hw, PERF_COUNT_HW_MAX, name_only);
27621dc12760SJiri Olsa 
27631dc12760SJiri Olsa 	print_symbol_events(event_glob, PERF_TYPE_SOFTWARE,
2764a3277d2dSFrederic Weisbecker 			    event_symbols_sw, PERF_COUNT_SW_MAX, name_only);
27655e0861baSAndi Kleen 	print_tool_events(event_glob, name_only);
27661dc12760SJiri Olsa 
2767a3277d2dSFrederic Weisbecker 	print_hwcache_events(event_glob, name_only);
276873c24cb8SJaswinder Singh Rajput 
2769bf874fcfSAndi Kleen 	print_pmu_events(event_glob, name_only, quiet_flag, long_desc,
2770a7f6c8c8SJin Yao 			details_flag, deprecated);
2771dc098b35SAndi Kleen 
2772668b8788SArnaldo Carvalho de Melo 	if (event_glob != NULL)
2773668b8788SArnaldo Carvalho de Melo 		return;
277473c24cb8SJaswinder Singh Rajput 
2775a3277d2dSFrederic Weisbecker 	if (!name_only) {
2776947b4ad1SIngo Molnar 		printf("  %-50s [%s]\n",
27775f537a26SJiri Olsa 		       "rNNN",
27781cf4a063SArnaldo Carvalho de Melo 		       event_type_descriptors[PERF_TYPE_RAW]);
27795f537a26SJiri Olsa 		printf("  %-50s [%s]\n",
27805f537a26SJiri Olsa 		       "cpu/t1=v1[,t2=v2,t3 ...]/modifier",
27815f537a26SJiri Olsa 		       event_type_descriptors[PERF_TYPE_RAW]);
2782dfc431cbSArnaldo Carvalho de Melo 		if (pager_in_use())
2783dfc431cbSArnaldo Carvalho de Melo 			printf("   (see 'man perf-list' on how to encode it)\n\n");
278486470930SIngo Molnar 
2785947b4ad1SIngo Molnar 		printf("  %-50s [%s]\n",
27863741eb9fSJacob Shin 		       "mem:<addr>[/len][:access]",
278741bdcb23SLiming Wang 			event_type_descriptors[PERF_TYPE_BREAKPOINT]);
2788dfc431cbSArnaldo Carvalho de Melo 		if (pager_in_use())
27891b290d67SFrederic Weisbecker 			printf("\n");
2790a3277d2dSFrederic Weisbecker 	}
27911b290d67SFrederic Weisbecker 
2792a3277d2dSFrederic Weisbecker 	print_tracepoint_events(NULL, NULL, name_only);
279340218daeSMasami Hiramatsu 
279440218daeSMasami Hiramatsu 	print_sdt_events(NULL, NULL, name_only);
279571b0acceSAndi Kleen 
279633bbc571SJiri Olsa 	metricgroup__print(true, true, NULL, name_only, details_flag);
279786470930SIngo Molnar }
27988f707d84SJiri Olsa 
27996cee6cd3SArnaldo Carvalho de Melo int parse_events__is_hardcoded_term(struct parse_events_term *term)
28008f707d84SJiri Olsa {
280116fa7e82SJiri Olsa 	return term->type_term != PARSE_EVENTS__TERM_TYPE_USER;
28028f707d84SJiri Olsa }
28038f707d84SJiri Olsa 
280467b49b38SJiri Olsa static int new_term(struct parse_events_term **_term,
280567b49b38SJiri Olsa 		    struct parse_events_term *temp,
280667b49b38SJiri Olsa 		    char *str, u64 num)
28078f707d84SJiri Olsa {
28086cee6cd3SArnaldo Carvalho de Melo 	struct parse_events_term *term;
28098f707d84SJiri Olsa 
281067b49b38SJiri Olsa 	term = malloc(sizeof(*term));
28118f707d84SJiri Olsa 	if (!term)
28128f707d84SJiri Olsa 		return -ENOMEM;
28138f707d84SJiri Olsa 
281467b49b38SJiri Olsa 	*term = *temp;
28158f707d84SJiri Olsa 	INIT_LIST_HEAD(&term->list);
2816c2f1ceadSAndi Kleen 	term->weak = false;
28178f707d84SJiri Olsa 
281867b49b38SJiri Olsa 	switch (term->type_val) {
28198f707d84SJiri Olsa 	case PARSE_EVENTS__TERM_TYPE_NUM:
28208f707d84SJiri Olsa 		term->val.num = num;
28218f707d84SJiri Olsa 		break;
28228f707d84SJiri Olsa 	case PARSE_EVENTS__TERM_TYPE_STR:
28238f707d84SJiri Olsa 		term->val.str = str;
28248f707d84SJiri Olsa 		break;
28258f707d84SJiri Olsa 	default:
28264be8be6bSAdrian Hunter 		free(term);
28278f707d84SJiri Olsa 		return -EINVAL;
28288f707d84SJiri Olsa 	}
28298f707d84SJiri Olsa 
28308f707d84SJiri Olsa 	*_term = term;
28318f707d84SJiri Olsa 	return 0;
28328f707d84SJiri Olsa }
28338f707d84SJiri Olsa 
28346cee6cd3SArnaldo Carvalho de Melo int parse_events_term__num(struct parse_events_term **term,
2835cecf3a2eSJiri Olsa 			   int type_term, char *config, u64 num,
283699e7138eSJiri Olsa 			   bool no_value,
2837bb78ce7dSAdrian Hunter 			   void *loc_term_, void *loc_val_)
283816fa7e82SJiri Olsa {
2839bb78ce7dSAdrian Hunter 	YYLTYPE *loc_term = loc_term_;
2840bb78ce7dSAdrian Hunter 	YYLTYPE *loc_val = loc_val_;
2841bb78ce7dSAdrian Hunter 
284267b49b38SJiri Olsa 	struct parse_events_term temp = {
284367b49b38SJiri Olsa 		.type_val  = PARSE_EVENTS__TERM_TYPE_NUM,
284467b49b38SJiri Olsa 		.type_term = type_term,
284567b49b38SJiri Olsa 		.config    = config,
284699e7138eSJiri Olsa 		.no_value  = no_value,
284767b49b38SJiri Olsa 		.err_term  = loc_term ? loc_term->first_column : 0,
284867b49b38SJiri Olsa 		.err_val   = loc_val  ? loc_val->first_column  : 0,
284967b49b38SJiri Olsa 	};
285067b49b38SJiri Olsa 
285167b49b38SJiri Olsa 	return new_term(term, &temp, NULL, num);
285216fa7e82SJiri Olsa }
285316fa7e82SJiri Olsa 
28546cee6cd3SArnaldo Carvalho de Melo int parse_events_term__str(struct parse_events_term **term,
2855cecf3a2eSJiri Olsa 			   int type_term, char *config, char *str,
2856bb78ce7dSAdrian Hunter 			   void *loc_term_, void *loc_val_)
285716fa7e82SJiri Olsa {
2858bb78ce7dSAdrian Hunter 	YYLTYPE *loc_term = loc_term_;
2859bb78ce7dSAdrian Hunter 	YYLTYPE *loc_val = loc_val_;
2860bb78ce7dSAdrian Hunter 
286167b49b38SJiri Olsa 	struct parse_events_term temp = {
286267b49b38SJiri Olsa 		.type_val  = PARSE_EVENTS__TERM_TYPE_STR,
286367b49b38SJiri Olsa 		.type_term = type_term,
286467b49b38SJiri Olsa 		.config    = config,
286567b49b38SJiri Olsa 		.err_term  = loc_term ? loc_term->first_column : 0,
286667b49b38SJiri Olsa 		.err_val   = loc_val  ? loc_val->first_column  : 0,
286767b49b38SJiri Olsa 	};
286867b49b38SJiri Olsa 
286967b49b38SJiri Olsa 	return new_term(term, &temp, str, 0);
287016fa7e82SJiri Olsa }
287116fa7e82SJiri Olsa 
28726cee6cd3SArnaldo Carvalho de Melo int parse_events_term__sym_hw(struct parse_events_term **term,
28731d33d6dcSJiri Olsa 			      char *config, unsigned idx)
28741d33d6dcSJiri Olsa {
28751d33d6dcSJiri Olsa 	struct event_symbol *sym;
2876b6645a72SIan Rogers 	char *str;
287767b49b38SJiri Olsa 	struct parse_events_term temp = {
287867b49b38SJiri Olsa 		.type_val  = PARSE_EVENTS__TERM_TYPE_STR,
287967b49b38SJiri Olsa 		.type_term = PARSE_EVENTS__TERM_TYPE_USER,
2880b6645a72SIan Rogers 		.config    = config,
288167b49b38SJiri Olsa 	};
28821d33d6dcSJiri Olsa 
2883b6645a72SIan Rogers 	if (!temp.config) {
2884b6645a72SIan Rogers 		temp.config = strdup("event");
2885b6645a72SIan Rogers 		if (!temp.config)
2886b6645a72SIan Rogers 			return -ENOMEM;
2887b6645a72SIan Rogers 	}
28881d33d6dcSJiri Olsa 	BUG_ON(idx >= PERF_COUNT_HW_MAX);
28891d33d6dcSJiri Olsa 	sym = &event_symbols_hw[idx];
28901d33d6dcSJiri Olsa 
2891b6645a72SIan Rogers 	str = strdup(sym->symbol);
2892b6645a72SIan Rogers 	if (!str)
2893b6645a72SIan Rogers 		return -ENOMEM;
2894b6645a72SIan Rogers 	return new_term(term, &temp, str, 0);
28951d33d6dcSJiri Olsa }
28961d33d6dcSJiri Olsa 
28976cee6cd3SArnaldo Carvalho de Melo int parse_events_term__clone(struct parse_events_term **new,
28986cee6cd3SArnaldo Carvalho de Melo 			     struct parse_events_term *term)
2899a6146d50SZheng Yan {
2900b6645a72SIan Rogers 	char *str;
290167b49b38SJiri Olsa 	struct parse_events_term temp = {
290267b49b38SJiri Olsa 		.type_val  = term->type_val,
290367b49b38SJiri Olsa 		.type_term = term->type_term,
2904b6645a72SIan Rogers 		.config    = NULL,
290567b49b38SJiri Olsa 		.err_term  = term->err_term,
290667b49b38SJiri Olsa 		.err_val   = term->err_val,
290767b49b38SJiri Olsa 	};
290867b49b38SJiri Olsa 
2909b6645a72SIan Rogers 	if (term->config) {
2910b6645a72SIan Rogers 		temp.config = strdup(term->config);
2911b6645a72SIan Rogers 		if (!temp.config)
2912b6645a72SIan Rogers 			return -ENOMEM;
2913b6645a72SIan Rogers 	}
2914b6645a72SIan Rogers 	if (term->type_val == PARSE_EVENTS__TERM_TYPE_NUM)
2915b6645a72SIan Rogers 		return new_term(new, &temp, NULL, term->val.num);
2916b6645a72SIan Rogers 
2917b6645a72SIan Rogers 	str = strdup(term->val.str);
2918b6645a72SIan Rogers 	if (!str)
2919b6645a72SIan Rogers 		return -ENOMEM;
2920b6645a72SIan Rogers 	return new_term(new, &temp, str, 0);
2921a6146d50SZheng Yan }
2922a6146d50SZheng Yan 
29231dc92556SIan Rogers void parse_events_term__delete(struct parse_events_term *term)
29241dc92556SIan Rogers {
29251dc92556SIan Rogers 	if (term->array.nr_ranges)
29261dc92556SIan Rogers 		zfree(&term->array.ranges);
29271dc92556SIan Rogers 
29281dc92556SIan Rogers 	if (term->type_val != PARSE_EVENTS__TERM_TYPE_NUM)
29291dc92556SIan Rogers 		zfree(&term->val.str);
29301dc92556SIan Rogers 
29311dc92556SIan Rogers 	zfree(&term->config);
29321dc92556SIan Rogers 	free(term);
29331dc92556SIan Rogers }
29341dc92556SIan Rogers 
29358255718fSAndi Kleen int parse_events_copy_term_list(struct list_head *old,
29368255718fSAndi Kleen 				 struct list_head **new)
29378255718fSAndi Kleen {
29388255718fSAndi Kleen 	struct parse_events_term *term, *n;
29398255718fSAndi Kleen 	int ret;
29408255718fSAndi Kleen 
29418255718fSAndi Kleen 	if (!old) {
29428255718fSAndi Kleen 		*new = NULL;
29438255718fSAndi Kleen 		return 0;
29448255718fSAndi Kleen 	}
29458255718fSAndi Kleen 
29468255718fSAndi Kleen 	*new = malloc(sizeof(struct list_head));
29478255718fSAndi Kleen 	if (!*new)
29488255718fSAndi Kleen 		return -ENOMEM;
29498255718fSAndi Kleen 	INIT_LIST_HEAD(*new);
29508255718fSAndi Kleen 
29518255718fSAndi Kleen 	list_for_each_entry (term, old, list) {
29528255718fSAndi Kleen 		ret = parse_events_term__clone(&n, term);
29538255718fSAndi Kleen 		if (ret)
29548255718fSAndi Kleen 			return ret;
29558255718fSAndi Kleen 		list_add_tail(&n->list, *new);
29568255718fSAndi Kleen 	}
29578255718fSAndi Kleen 	return 0;
29588255718fSAndi Kleen }
29598255718fSAndi Kleen 
2960fc0a2c1dSArnaldo Carvalho de Melo void parse_events_terms__purge(struct list_head *terms)
29618f707d84SJiri Olsa {
29626cee6cd3SArnaldo Carvalho de Melo 	struct parse_events_term *term, *h;
29638f707d84SJiri Olsa 
2964a8adfcebSWang Nan 	list_for_each_entry_safe(term, h, terms, list) {
2965a8adfcebSWang Nan 		list_del_init(&term->list);
29661dc92556SIan Rogers 		parse_events_term__delete(term);
29678f707d84SJiri Olsa 	}
2968a8adfcebSWang Nan }
2969b39b8393SJiri Olsa 
29702146afc6SArnaldo Carvalho de Melo void parse_events_terms__delete(struct list_head *terms)
2971fc0a2c1dSArnaldo Carvalho de Melo {
29722146afc6SArnaldo Carvalho de Melo 	if (!terms)
29732146afc6SArnaldo Carvalho de Melo 		return;
2974fc0a2c1dSArnaldo Carvalho de Melo 	parse_events_terms__purge(terms);
2975d20a5f2bSWang Nan 	free(terms);
2976fc0a2c1dSArnaldo Carvalho de Melo }
2977fc0a2c1dSArnaldo Carvalho de Melo 
29782d055bf2SWang Nan void parse_events__clear_array(struct parse_events_array *a)
29792d055bf2SWang Nan {
2980360e071bSTaeung Song 	zfree(&a->ranges);
29812d055bf2SWang Nan }
29822d055bf2SWang Nan 
29835d9cdc11SArnaldo Carvalho de Melo void parse_events_evlist_error(struct parse_events_state *parse_state,
2984b39b8393SJiri Olsa 			       int idx, const char *str)
2985b39b8393SJiri Olsa {
2986448d732cSIan Rogers 	if (!parse_state->error)
2987a6ced2beSAdrian Hunter 		return;
2988448d732cSIan Rogers 
2989448d732cSIan Rogers 	parse_events__handle_error(parse_state->error, idx, strdup(str), NULL);
2990b39b8393SJiri Olsa }
2991ffeb883eSHe Kuang 
299217cb5f84SWang Nan static void config_terms_list(char *buf, size_t buf_sz)
299317cb5f84SWang Nan {
299417cb5f84SWang Nan 	int i;
299517cb5f84SWang Nan 	bool first = true;
299617cb5f84SWang Nan 
299717cb5f84SWang Nan 	buf[0] = '\0';
299817cb5f84SWang Nan 	for (i = 0; i < __PARSE_EVENTS__TERM_TYPE_NR; i++) {
299917cb5f84SWang Nan 		const char *name = config_term_names[i];
300017cb5f84SWang Nan 
30011669e509SWang Nan 		if (!config_term_avail(i, NULL))
30021669e509SWang Nan 			continue;
300317cb5f84SWang Nan 		if (!name)
300417cb5f84SWang Nan 			continue;
300517cb5f84SWang Nan 		if (name[0] == '<')
300617cb5f84SWang Nan 			continue;
300717cb5f84SWang Nan 
300817cb5f84SWang Nan 		if (strlen(buf) + strlen(name) + 2 >= buf_sz)
300917cb5f84SWang Nan 			return;
301017cb5f84SWang Nan 
301117cb5f84SWang Nan 		if (!first)
301217cb5f84SWang Nan 			strcat(buf, ",");
301317cb5f84SWang Nan 		else
301417cb5f84SWang Nan 			first = false;
301517cb5f84SWang Nan 		strcat(buf, name);
301617cb5f84SWang Nan 	}
301717cb5f84SWang Nan }
301817cb5f84SWang Nan 
3019ffeb883eSHe Kuang /*
3020ffeb883eSHe Kuang  * Return string contains valid config terms of an event.
3021ffeb883eSHe Kuang  * @additional_terms: For terms such as PMU sysfs terms.
3022ffeb883eSHe Kuang  */
3023ffeb883eSHe Kuang char *parse_events_formats_error_string(char *additional_terms)
3024ffeb883eSHe Kuang {
3025ffeb883eSHe Kuang 	char *str;
3026626a6b78SWang Nan 	/* "no-overwrite" is the longest name */
302717cb5f84SWang Nan 	char static_terms[__PARSE_EVENTS__TERM_TYPE_NR *
3028626a6b78SWang Nan 			  (sizeof("no-overwrite") - 1)];
3029ffeb883eSHe Kuang 
303017cb5f84SWang Nan 	config_terms_list(static_terms, sizeof(static_terms));
3031ffeb883eSHe Kuang 	/* valid terms */
3032ffeb883eSHe Kuang 	if (additional_terms) {
303326dee028SWang Nan 		if (asprintf(&str, "valid terms: %s,%s",
303426dee028SWang Nan 			     additional_terms, static_terms) < 0)
3035ffeb883eSHe Kuang 			goto fail;
3036ffeb883eSHe Kuang 	} else {
303726dee028SWang Nan 		if (asprintf(&str, "valid terms: %s", static_terms) < 0)
3038ffeb883eSHe Kuang 			goto fail;
3039ffeb883eSHe Kuang 	}
3040ffeb883eSHe Kuang 	return str;
3041ffeb883eSHe Kuang 
3042ffeb883eSHe Kuang fail:
3043ffeb883eSHe Kuang 	return NULL;
3044ffeb883eSHe Kuang }
3045