xref: /linux/tools/perf/util/parse-events.c (revision 327daf34554d20a6aa190dce5a625a555055435e)
1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
2d2709c7cSDavid Howells #include <linux/hw_breakpoint.h>
38dd2a131SJiri Olsa #include <linux/err.h>
4347c2f0aSIan Rogers #include <linux/list_sort.h>
57f7c536fSArnaldo Carvalho de Melo #include <linux/zalloc.h>
676b31a29SArnaldo Carvalho de Melo #include <dirent.h>
7a43783aeSArnaldo Carvalho de Melo #include <errno.h>
886a5e0c2SArnaldo Carvalho de Melo #include <sys/ioctl.h>
9391e4206SArnaldo Carvalho de Melo #include <sys/param.h>
10b0742e90SArnaldo Carvalho de Melo #include "term.h"
11361c99a6SArnaldo Carvalho de Melo #include "evlist.h"
1269aad6f1SArnaldo Carvalho de Melo #include "evsel.h"
134b6ab94eSJosh Poimboeuf #include <subcmd/parse-options.h>
1486470930SIngo Molnar #include "parse-events.h"
15a067558eSArnaldo Carvalho de Melo #include "string2.h"
168ec20b17SArnaldo Carvalho de Melo #include "strlist.h"
1784c86ca1SWang Nan #include "bpf-loader.h"
186e81c74cSMasami Hiramatsu #include "debug.h"
19592d5a6bSJiri Olsa #include <api/fs/tracing_path.h>
209c3516d1SJiri Olsa #include <perf/cpumap.h>
21ac20de6fSZheng Yan #include "parse-events-bison.h"
2289812fc8SJiri Olsa #include "parse-events-flex.h"
235f537a26SJiri Olsa #include "pmu.h"
24b39b8393SJiri Olsa #include "asm/bug.h"
25ac12f676SAndi Kleen #include "util/parse-branch-options.h"
2695be9d19SArnaldo Carvalho de Melo #include "util/evsel_config.h"
27252a2fdcSArnaldo Carvalho de Melo #include "util/event.h"
289cbfa2f6SJin Yao #include "util/parse-events-hybrid.h"
295e4edd1fSJin Yao #include "util/pmu-hybrid.h"
30d180aa56SNamhyung Kim #include "util/bpf-filter.h"
31ea0c5239SIan Rogers #include "util/util.h"
329b7c7728SIan Rogers #include "tracepoint.h"
3371c86cdaSZhengjun Xing #include "thread_map.h"
3489812fc8SJiri Olsa 
3589812fc8SJiri Olsa #define MAX_NAME_LEN 100
3686470930SIngo Molnar 
379b7c7728SIan Rogers struct perf_pmu_event_symbol {
389b7c7728SIan Rogers 	char	*symbol;
399b7c7728SIan Rogers 	enum perf_pmu_event_symbol_type	type;
409b7c7728SIan Rogers };
419b7c7728SIan Rogers 
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);
485e4edd1fSJin Yao static int parse_events__with_hybrid_pmu(struct parse_events_state *parse_state,
495e4edd1fSJin Yao 					 const char *str, char *pmu_name,
505e4edd1fSJin Yao 					 struct list_head *list);
51bcd3279fSFrederic Weisbecker 
52dcb4e102SKan Liang static struct perf_pmu_event_symbol *perf_pmu_events_list;
53dcb4e102SKan Liang /*
54dcb4e102SKan Liang  * The variable indicates the number of supported pmu event symbols.
55dcb4e102SKan Liang  * 0 means not initialized and ready to init
56dcb4e102SKan Liang  * -1 means failed to init, don't try anymore
57dcb4e102SKan Liang  * >0 is the number of supported pmu event symbols
58dcb4e102SKan Liang  */
59dcb4e102SKan Liang static int perf_pmu_events_list_num;
60dcb4e102SKan Liang 
61705750f2SYunlong Song struct event_symbol event_symbols_hw[PERF_COUNT_HW_MAX] = {
621dc12760SJiri Olsa 	[PERF_COUNT_HW_CPU_CYCLES] = {
631dc12760SJiri Olsa 		.symbol = "cpu-cycles",
641dc12760SJiri Olsa 		.alias  = "cycles",
651dc12760SJiri Olsa 	},
661dc12760SJiri Olsa 	[PERF_COUNT_HW_INSTRUCTIONS] = {
671dc12760SJiri Olsa 		.symbol = "instructions",
681dc12760SJiri Olsa 		.alias  = "",
691dc12760SJiri Olsa 	},
701dc12760SJiri Olsa 	[PERF_COUNT_HW_CACHE_REFERENCES] = {
711dc12760SJiri Olsa 		.symbol = "cache-references",
721dc12760SJiri Olsa 		.alias  = "",
731dc12760SJiri Olsa 	},
741dc12760SJiri Olsa 	[PERF_COUNT_HW_CACHE_MISSES] = {
751dc12760SJiri Olsa 		.symbol = "cache-misses",
761dc12760SJiri Olsa 		.alias  = "",
771dc12760SJiri Olsa 	},
781dc12760SJiri Olsa 	[PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = {
791dc12760SJiri Olsa 		.symbol = "branch-instructions",
801dc12760SJiri Olsa 		.alias  = "branches",
811dc12760SJiri Olsa 	},
821dc12760SJiri Olsa 	[PERF_COUNT_HW_BRANCH_MISSES] = {
831dc12760SJiri Olsa 		.symbol = "branch-misses",
841dc12760SJiri Olsa 		.alias  = "",
851dc12760SJiri Olsa 	},
861dc12760SJiri Olsa 	[PERF_COUNT_HW_BUS_CYCLES] = {
871dc12760SJiri Olsa 		.symbol = "bus-cycles",
881dc12760SJiri Olsa 		.alias  = "",
891dc12760SJiri Olsa 	},
901dc12760SJiri Olsa 	[PERF_COUNT_HW_STALLED_CYCLES_FRONTEND] = {
911dc12760SJiri Olsa 		.symbol = "stalled-cycles-frontend",
921dc12760SJiri Olsa 		.alias  = "idle-cycles-frontend",
931dc12760SJiri Olsa 	},
941dc12760SJiri Olsa 	[PERF_COUNT_HW_STALLED_CYCLES_BACKEND] = {
951dc12760SJiri Olsa 		.symbol = "stalled-cycles-backend",
961dc12760SJiri Olsa 		.alias  = "idle-cycles-backend",
971dc12760SJiri Olsa 	},
981dc12760SJiri Olsa 	[PERF_COUNT_HW_REF_CPU_CYCLES] = {
991dc12760SJiri Olsa 		.symbol = "ref-cycles",
1001dc12760SJiri Olsa 		.alias  = "",
1011dc12760SJiri Olsa 	},
1021dc12760SJiri Olsa };
10386470930SIngo Molnar 
104705750f2SYunlong Song struct event_symbol event_symbols_sw[PERF_COUNT_SW_MAX] = {
1051dc12760SJiri Olsa 	[PERF_COUNT_SW_CPU_CLOCK] = {
1061dc12760SJiri Olsa 		.symbol = "cpu-clock",
1071dc12760SJiri Olsa 		.alias  = "",
1081dc12760SJiri Olsa 	},
1091dc12760SJiri Olsa 	[PERF_COUNT_SW_TASK_CLOCK] = {
1101dc12760SJiri Olsa 		.symbol = "task-clock",
1111dc12760SJiri Olsa 		.alias  = "",
1121dc12760SJiri Olsa 	},
1131dc12760SJiri Olsa 	[PERF_COUNT_SW_PAGE_FAULTS] = {
1141dc12760SJiri Olsa 		.symbol = "page-faults",
1151dc12760SJiri Olsa 		.alias  = "faults",
1161dc12760SJiri Olsa 	},
1171dc12760SJiri Olsa 	[PERF_COUNT_SW_CONTEXT_SWITCHES] = {
1181dc12760SJiri Olsa 		.symbol = "context-switches",
1191dc12760SJiri Olsa 		.alias  = "cs",
1201dc12760SJiri Olsa 	},
1211dc12760SJiri Olsa 	[PERF_COUNT_SW_CPU_MIGRATIONS] = {
1221dc12760SJiri Olsa 		.symbol = "cpu-migrations",
1231dc12760SJiri Olsa 		.alias  = "migrations",
1241dc12760SJiri Olsa 	},
1251dc12760SJiri Olsa 	[PERF_COUNT_SW_PAGE_FAULTS_MIN] = {
1261dc12760SJiri Olsa 		.symbol = "minor-faults",
1271dc12760SJiri Olsa 		.alias  = "",
1281dc12760SJiri Olsa 	},
1291dc12760SJiri Olsa 	[PERF_COUNT_SW_PAGE_FAULTS_MAJ] = {
1301dc12760SJiri Olsa 		.symbol = "major-faults",
1311dc12760SJiri Olsa 		.alias  = "",
1321dc12760SJiri Olsa 	},
1331dc12760SJiri Olsa 	[PERF_COUNT_SW_ALIGNMENT_FAULTS] = {
1341dc12760SJiri Olsa 		.symbol = "alignment-faults",
1351dc12760SJiri Olsa 		.alias  = "",
1361dc12760SJiri Olsa 	},
1371dc12760SJiri Olsa 	[PERF_COUNT_SW_EMULATION_FAULTS] = {
1381dc12760SJiri Olsa 		.symbol = "emulation-faults",
1391dc12760SJiri Olsa 		.alias  = "",
1401dc12760SJiri Olsa 	},
141d22d1a2aSAdrian Hunter 	[PERF_COUNT_SW_DUMMY] = {
142d22d1a2aSAdrian Hunter 		.symbol = "dummy",
143d22d1a2aSAdrian Hunter 		.alias  = "",
144d22d1a2aSAdrian Hunter 	},
145bae9cc41SArnaldo Carvalho de Melo 	[PERF_COUNT_SW_BPF_OUTPUT] = {
146bae9cc41SArnaldo Carvalho de Melo 		.symbol = "bpf-output",
147bae9cc41SArnaldo Carvalho de Melo 		.alias  = "",
148bae9cc41SArnaldo Carvalho de Melo 	},
149fb6c79d7SNamhyung Kim 	[PERF_COUNT_SW_CGROUP_SWITCHES] = {
150fb6c79d7SNamhyung Kim 		.symbol = "cgroup-switches",
151fb6c79d7SNamhyung Kim 		.alias  = "",
152fb6c79d7SNamhyung Kim 	},
15386470930SIngo Molnar };
15486470930SIngo Molnar 
15571c86cdaSZhengjun Xing bool is_event_supported(u8 type, u64 config)
15671c86cdaSZhengjun Xing {
15771c86cdaSZhengjun Xing 	bool ret = true;
15871c86cdaSZhengjun Xing 	int open_return;
15971c86cdaSZhengjun Xing 	struct evsel *evsel;
16071c86cdaSZhengjun Xing 	struct perf_event_attr attr = {
16171c86cdaSZhengjun Xing 		.type = type,
16271c86cdaSZhengjun Xing 		.config = config,
16371c86cdaSZhengjun Xing 		.disabled = 1,
16471c86cdaSZhengjun Xing 	};
16571c86cdaSZhengjun Xing 	struct perf_thread_map *tmap = thread_map__new_by_tid(0);
16671c86cdaSZhengjun Xing 
16771c86cdaSZhengjun Xing 	if (tmap == NULL)
16871c86cdaSZhengjun Xing 		return false;
16971c86cdaSZhengjun Xing 
17071c86cdaSZhengjun Xing 	evsel = evsel__new(&attr);
17171c86cdaSZhengjun Xing 	if (evsel) {
17271c86cdaSZhengjun Xing 		open_return = evsel__open(evsel, NULL, tmap);
17371c86cdaSZhengjun Xing 		ret = open_return >= 0;
17471c86cdaSZhengjun Xing 
17571c86cdaSZhengjun Xing 		if (open_return == -EACCES) {
17671c86cdaSZhengjun Xing 			/*
17771c86cdaSZhengjun Xing 			 * This happens if the paranoid value
17871c86cdaSZhengjun Xing 			 * /proc/sys/kernel/perf_event_paranoid is set to 2
17971c86cdaSZhengjun Xing 			 * Re-run with exclude_kernel set; we don't do that
18071c86cdaSZhengjun Xing 			 * by default as some ARM machines do not support it.
18171c86cdaSZhengjun Xing 			 *
18271c86cdaSZhengjun Xing 			 */
18371c86cdaSZhengjun Xing 			evsel->core.attr.exclude_kernel = 1;
18471c86cdaSZhengjun Xing 			ret = evsel__open(evsel, NULL, tmap) >= 0;
18571c86cdaSZhengjun Xing 		}
18671c86cdaSZhengjun Xing 		evsel__delete(evsel);
18771c86cdaSZhengjun Xing 	}
18871c86cdaSZhengjun Xing 
18971c86cdaSZhengjun Xing 	perf_thread_map__put(tmap);
19071c86cdaSZhengjun Xing 	return ret;
19171c86cdaSZhengjun Xing }
19271c86cdaSZhengjun Xing 
1931424dc96SDavid Ahern const char *event_type(int type)
1941424dc96SDavid Ahern {
1951424dc96SDavid Ahern 	switch (type) {
1961424dc96SDavid Ahern 	case PERF_TYPE_HARDWARE:
1971424dc96SDavid Ahern 		return "hardware";
1981424dc96SDavid Ahern 
1991424dc96SDavid Ahern 	case PERF_TYPE_SOFTWARE:
2001424dc96SDavid Ahern 		return "software";
2011424dc96SDavid Ahern 
2021424dc96SDavid Ahern 	case PERF_TYPE_TRACEPOINT:
2031424dc96SDavid Ahern 		return "tracepoint";
2041424dc96SDavid Ahern 
2051424dc96SDavid Ahern 	case PERF_TYPE_HW_CACHE:
2061424dc96SDavid Ahern 		return "hardware-cache";
2071424dc96SDavid Ahern 
2081424dc96SDavid Ahern 	default:
2091424dc96SDavid Ahern 		break;
2101424dc96SDavid Ahern 	}
2111424dc96SDavid Ahern 
2121424dc96SDavid Ahern 	return "unknown";
2131424dc96SDavid Ahern }
2141424dc96SDavid Ahern 
2152b62b3a6SIan Rogers static char *get_config_str(struct list_head *head_terms, int type_term)
216e814fdddSWang Nan {
217e814fdddSWang Nan 	struct parse_events_term *term;
218e814fdddSWang Nan 
219e814fdddSWang Nan 	if (!head_terms)
220e814fdddSWang Nan 		return NULL;
221e814fdddSWang Nan 
222e814fdddSWang Nan 	list_for_each_entry(term, head_terms, list)
2232b62b3a6SIan Rogers 		if (term->type_term == type_term)
224e814fdddSWang Nan 			return term->val.str;
225e814fdddSWang Nan 
226e814fdddSWang Nan 	return NULL;
227e814fdddSWang Nan }
2287ae92e74SYan, Zheng 
2292b62b3a6SIan Rogers static char *get_config_metric_id(struct list_head *head_terms)
2302b62b3a6SIan Rogers {
2312b62b3a6SIan Rogers 	return get_config_str(head_terms, PARSE_EVENTS__TERM_TYPE_METRIC_ID);
2322b62b3a6SIan Rogers }
2332b62b3a6SIan Rogers 
2342b62b3a6SIan Rogers static char *get_config_name(struct list_head *head_terms)
2352b62b3a6SIan Rogers {
2362b62b3a6SIan Rogers 	return get_config_str(head_terms, PARSE_EVENTS__TERM_TYPE_NAME);
2372b62b3a6SIan Rogers }
2382b62b3a6SIan Rogers 
23932dcd021SJiri Olsa static struct evsel *
240410136f5SStephane Eranian __add_event(struct list_head *list, int *idx,
2417ae92e74SYan, Zheng 	    struct perf_event_attr *attr,
24270943490SStephane Eranian 	    bool init_attr,
2432b62b3a6SIan Rogers 	    const char *name, const char *metric_id, struct perf_pmu *pmu,
244f0fbb114SAndi Kleen 	    struct list_head *config_terms, bool auto_merge_stats,
245f0fbb114SAndi Kleen 	    const char *cpu_list)
24689812fc8SJiri Olsa {
24732dcd021SJiri Olsa 	struct evsel *evsel;
2485d680be3SNamhyung Kim 	struct perf_cpu_map *cpus = pmu ? perf_cpu_map__get(pmu->cpus) :
2499c3516d1SJiri Olsa 			       cpu_list ? perf_cpu_map__new(cpu_list) : NULL;
25089812fc8SJiri Olsa 
251e552b7beSRob Herring 	if (pmu)
252e552b7beSRob Herring 		perf_pmu__warn_invalid_formats(pmu);
253e552b7beSRob Herring 
254e4064776SJin Yao 	if (pmu && attr->type == PERF_TYPE_RAW)
255e4064776SJin Yao 		perf_pmu__warn_invalid_config(pmu, attr->config, name);
256e4064776SJin Yao 
25770943490SStephane Eranian 	if (init_attr)
25889812fc8SJiri Olsa 		event_attr_init(attr);
25989812fc8SJiri Olsa 
2608f6725a2SArnaldo Carvalho de Melo 	evsel = evsel__new_idx(attr, *idx);
2610f1b550eSNamhyung Kim 	if (!evsel) {
2620f1b550eSNamhyung Kim 		perf_cpu_map__put(cpus);
263410136f5SStephane Eranian 		return NULL;
2640f1b550eSNamhyung Kim 	}
26589812fc8SJiri Olsa 
26675fc5ae5STaeung Song 	(*idx)++;
2675d680be3SNamhyung Kim 	evsel->core.cpus = cpus;
268fe1f61b3SJiri Olsa 	evsel->core.own_cpus = perf_cpu_map__get(cpus);
269d3345fecSAdrian Hunter 	evsel->core.requires_cpu = pmu ? pmu->is_uncore : false;
27063ce8449SArnaldo Carvalho de Melo 	evsel->auto_merge_stats = auto_merge_stats;
271f7400262SNamhyung Kim 	evsel->pmu = pmu;
272f30a79b0SJiri Olsa 
2739db1763cSArnaldo Carvalho de Melo 	if (name)
27489812fc8SJiri Olsa 		evsel->name = strdup(name);
275930a2e29SJiri Olsa 
2762b62b3a6SIan Rogers 	if (metric_id)
2772b62b3a6SIan Rogers 		evsel->metric_id = strdup(metric_id);
2782b62b3a6SIan Rogers 
279930a2e29SJiri Olsa 	if (config_terms)
28099fc5941SAdrian Hunter 		list_splice_init(config_terms, &evsel->config_terms);
281930a2e29SJiri Olsa 
28270943490SStephane Eranian 	if (list)
283b27c4eceSJiri Olsa 		list_add_tail(&evsel->core.node, list);
28470943490SStephane Eranian 
285410136f5SStephane Eranian 	return evsel;
28689812fc8SJiri Olsa }
28789812fc8SJiri Olsa 
28870943490SStephane Eranian struct evsel *parse_events__add_event(int idx, struct perf_event_attr *attr,
2892b62b3a6SIan Rogers 				      const char *name, const char *metric_id,
2902b62b3a6SIan Rogers 				      struct perf_pmu *pmu)
29170943490SStephane Eranian {
2922b62b3a6SIan Rogers 	return __add_event(/*list=*/NULL, &idx, attr, /*init_attr=*/false, name,
2932b62b3a6SIan Rogers 			   metric_id, pmu, /*config_terms=*/NULL,
2942b62b3a6SIan Rogers 			   /*auto_merge_stats=*/false, /*cpu_list=*/NULL);
29570943490SStephane Eranian }
29670943490SStephane Eranian 
297c5cd8ac0SDavid Ahern static int add_event(struct list_head *list, int *idx,
2988e8bbfb3SIan Rogers 		     struct perf_event_attr *attr, const char *name,
2992b62b3a6SIan Rogers 		     const char *metric_id, struct list_head *config_terms)
3007ae92e74SYan, Zheng {
3012b62b3a6SIan Rogers 	return __add_event(list, idx, attr, /*init_attr*/true, name, metric_id,
3022b62b3a6SIan Rogers 			   /*pmu=*/NULL, config_terms,
3032b62b3a6SIan Rogers 			   /*auto_merge_stats=*/false, /*cpu_list=*/NULL) ? 0 : -ENOMEM;
304f0fbb114SAndi Kleen }
305f0fbb114SAndi Kleen 
306f0fbb114SAndi Kleen static int add_event_tool(struct list_head *list, int *idx,
307f0fbb114SAndi Kleen 			  enum perf_tool_event tool_event)
308f0fbb114SAndi Kleen {
30932dcd021SJiri Olsa 	struct evsel *evsel;
310f0fbb114SAndi Kleen 	struct perf_event_attr attr = {
311f0fbb114SAndi Kleen 		.type = PERF_TYPE_SOFTWARE,
312f0fbb114SAndi Kleen 		.config = PERF_COUNT_SW_DUMMY,
313f0fbb114SAndi Kleen 	};
314f0fbb114SAndi Kleen 
3152b62b3a6SIan Rogers 	evsel = __add_event(list, idx, &attr, /*init_attr=*/true, /*name=*/NULL,
3162b62b3a6SIan Rogers 			    /*metric_id=*/NULL, /*pmu=*/NULL,
3172b62b3a6SIan Rogers 			    /*config_terms=*/NULL, /*auto_merge_stats=*/false,
3182b62b3a6SIan Rogers 			    /*cpu_list=*/"0");
319f0fbb114SAndi Kleen 	if (!evsel)
320f0fbb114SAndi Kleen 		return -ENOMEM;
321f0fbb114SAndi Kleen 	evsel->tool_event = tool_event;
322b03b89b3SFlorian Fischer 	if (tool_event == PERF_TOOL_DURATION_TIME
323b03b89b3SFlorian Fischer 	    || tool_event == PERF_TOOL_USER_TIME
324b03b89b3SFlorian Fischer 	    || tool_event == PERF_TOOL_SYSTEM_TIME) {
325b194c9cdSIan Rogers 		free((char *)evsel->unit);
326b194c9cdSIan Rogers 		evsel->unit = strdup("ns");
327b194c9cdSIan Rogers 	}
328f0fbb114SAndi Kleen 	return 0;
3297ae92e74SYan, Zheng }
3307ae92e74SYan, Zheng 
331545a96c9SIan Rogers static int parse_aliases(char *str, const char *const names[][EVSEL__MAX_ALIASES], int size)
33286470930SIngo Molnar {
33386470930SIngo Molnar 	int i, j;
33461c45981SPaul Mackerras 	int n, longest = -1;
33586470930SIngo Molnar 
33686470930SIngo Molnar 	for (i = 0; i < size; i++) {
337c64e85e1SArnaldo Carvalho de Melo 		for (j = 0; j < EVSEL__MAX_ALIASES && names[i][j]; j++) {
33861c45981SPaul Mackerras 			n = strlen(names[i][j]);
33989812fc8SJiri Olsa 			if (n > longest && !strncasecmp(str, names[i][j], n))
34061c45981SPaul Mackerras 				longest = n;
34161c45981SPaul Mackerras 		}
34289812fc8SJiri Olsa 		if (longest > 0)
34386470930SIngo Molnar 			return i;
34486470930SIngo Molnar 	}
34586470930SIngo Molnar 
3468953645fSIngo Molnar 	return -1;
34786470930SIngo Molnar }
34886470930SIngo Molnar 
34943d0b978SWang Nan typedef int config_term_func_t(struct perf_event_attr *attr,
35043d0b978SWang Nan 			       struct parse_events_term *term,
35143d0b978SWang Nan 			       struct parse_events_error *err);
35243d0b978SWang Nan static int config_term_common(struct perf_event_attr *attr,
35343d0b978SWang Nan 			      struct parse_events_term *term,
35443d0b978SWang Nan 			      struct parse_events_error *err);
35543d0b978SWang Nan static int config_attr(struct perf_event_attr *attr,
35643d0b978SWang Nan 		       struct list_head *head,
35743d0b978SWang Nan 		       struct parse_events_error *err,
35843d0b978SWang Nan 		       config_term_func_t config_term);
35943d0b978SWang Nan 
360c5cd8ac0SDavid Ahern int parse_events_add_cache(struct list_head *list, int *idx,
36143d0b978SWang Nan 			   char *type, char *op_result1, char *op_result2,
3622c97b0d4SArnaldo Carvalho de Melo 			   struct parse_events_error *err,
363c93afadcSJin Yao 			   struct list_head *head_config,
364c93afadcSJin Yao 			   struct parse_events_state *parse_state)
36586470930SIngo Molnar {
36689812fc8SJiri Olsa 	struct perf_event_attr attr;
36743d0b978SWang Nan 	LIST_HEAD(config_terms);
3688e8bbfb3SIan Rogers 	char name[MAX_NAME_LEN];
3692b62b3a6SIan Rogers 	const char *config_name, *metric_id;
37061c45981SPaul Mackerras 	int cache_type = -1, cache_op = -1, cache_result = -1;
37189812fc8SJiri Olsa 	char *op_result[2] = { op_result1, op_result2 };
37230def61fSJin Yao 	int i, n, ret;
37330def61fSJin Yao 	bool hybrid;
37486470930SIngo Molnar 
37586470930SIngo Molnar 	/*
37686470930SIngo Molnar 	 * No fallback - if we cannot get a clear cache type
37786470930SIngo Molnar 	 * then bail out:
37886470930SIngo Molnar 	 */
379c64e85e1SArnaldo Carvalho de Melo 	cache_type = parse_aliases(type, evsel__hw_cache, PERF_COUNT_HW_CACHE_MAX);
38086470930SIngo Molnar 	if (cache_type == -1)
38189812fc8SJiri Olsa 		return -EINVAL;
38286470930SIngo Molnar 
38343d0b978SWang Nan 	config_name = get_config_name(head_config);
38489812fc8SJiri Olsa 	n = snprintf(name, MAX_NAME_LEN, "%s", type);
38589812fc8SJiri Olsa 
38689812fc8SJiri Olsa 	for (i = 0; (i < 2) && (op_result[i]); i++) {
38789812fc8SJiri Olsa 		char *str = op_result[i];
38889812fc8SJiri Olsa 
389275ef387SJiri Olsa 		n += snprintf(name + n, MAX_NAME_LEN - n, "-%s", str);
39061c45981SPaul Mackerras 
39161c45981SPaul Mackerras 		if (cache_op == -1) {
392c64e85e1SArnaldo Carvalho de Melo 			cache_op = parse_aliases(str, evsel__hw_cache_op,
39361c45981SPaul Mackerras 						 PERF_COUNT_HW_CACHE_OP_MAX);
39461c45981SPaul Mackerras 			if (cache_op >= 0) {
395c754c382SArnaldo Carvalho de Melo 				if (!evsel__is_cache_op_valid(cache_type, cache_op))
39689812fc8SJiri Olsa 					return -EINVAL;
39761c45981SPaul Mackerras 				continue;
39861c45981SPaul Mackerras 			}
39961c45981SPaul Mackerras 		}
40061c45981SPaul Mackerras 
40161c45981SPaul Mackerras 		if (cache_result == -1) {
402c64e85e1SArnaldo Carvalho de Melo 			cache_result = parse_aliases(str, evsel__hw_cache_result,
40361c45981SPaul Mackerras 						     PERF_COUNT_HW_CACHE_RESULT_MAX);
40461c45981SPaul Mackerras 			if (cache_result >= 0)
40561c45981SPaul Mackerras 				continue;
40661c45981SPaul Mackerras 		}
40761c45981SPaul Mackerras 	}
40861c45981SPaul Mackerras 
40986470930SIngo Molnar 	/*
41086470930SIngo Molnar 	 * Fall back to reads:
41186470930SIngo Molnar 	 */
4128953645fSIngo Molnar 	if (cache_op == -1)
4138953645fSIngo Molnar 		cache_op = PERF_COUNT_HW_CACHE_OP_READ;
41486470930SIngo Molnar 
41586470930SIngo Molnar 	/*
41686470930SIngo Molnar 	 * Fall back to accesses:
41786470930SIngo Molnar 	 */
41886470930SIngo Molnar 	if (cache_result == -1)
41986470930SIngo Molnar 		cache_result = PERF_COUNT_HW_CACHE_RESULT_ACCESS;
42086470930SIngo Molnar 
42189812fc8SJiri Olsa 	memset(&attr, 0, sizeof(attr));
42289812fc8SJiri Olsa 	attr.config = cache_type | (cache_op << 8) | (cache_result << 16);
42389812fc8SJiri Olsa 	attr.type = PERF_TYPE_HW_CACHE;
42443d0b978SWang Nan 
42543d0b978SWang Nan 	if (head_config) {
4262c97b0d4SArnaldo Carvalho de Melo 		if (config_attr(&attr, head_config, err,
42743d0b978SWang Nan 				config_term_common))
42843d0b978SWang Nan 			return -EINVAL;
42943d0b978SWang Nan 
43043d0b978SWang Nan 		if (get_config_terms(head_config, &config_terms))
43143d0b978SWang Nan 			return -ENOMEM;
43243d0b978SWang Nan 	}
43330def61fSJin Yao 
4342b62b3a6SIan Rogers 	metric_id = get_config_metric_id(head_config);
43530def61fSJin Yao 	ret = parse_events__add_cache_hybrid(list, idx, &attr,
4362b62b3a6SIan Rogers 					     config_name ? : name,
4372b62b3a6SIan Rogers 					     metric_id,
4382b62b3a6SIan Rogers 					     &config_terms,
439c93afadcSJin Yao 					     &hybrid, parse_state);
44030def61fSJin Yao 	if (hybrid)
44199fc5941SAdrian Hunter 		goto out_free_terms;
44230def61fSJin Yao 
4432b62b3a6SIan Rogers 	ret = add_event(list, idx, &attr, config_name ? : name, metric_id,
4442b62b3a6SIan Rogers 			&config_terms);
44599fc5941SAdrian Hunter out_free_terms:
44699fc5941SAdrian Hunter 	free_config_terms(&config_terms);
44799fc5941SAdrian Hunter 	return ret;
44886470930SIngo Molnar }
44986470930SIngo Molnar 
450378ef0f5SIan Rogers #ifdef HAVE_LIBTRACEEVENT
451272ed29aSJiri Olsa static void tracepoint_error(struct parse_events_error *e, int err,
4528c619d6aSWang Nan 			     const char *sys, const char *name)
45319658171SJiri Olsa {
4544584f084SIan Rogers 	const char *str;
45519658171SJiri Olsa 	char help[BUFSIZ];
45619658171SJiri Olsa 
457ec183d22SAdrian Hunter 	if (!e)
458ec183d22SAdrian Hunter 		return;
459ec183d22SAdrian Hunter 
46019658171SJiri Olsa 	/*
46119658171SJiri Olsa 	 * We get error directly from syscall errno ( > 0),
46219658171SJiri Olsa 	 * or from encoded pointer's error ( < 0).
46319658171SJiri Olsa 	 */
46419658171SJiri Olsa 	err = abs(err);
46519658171SJiri Olsa 
46619658171SJiri Olsa 	switch (err) {
46719658171SJiri Olsa 	case EACCES:
4684584f084SIan Rogers 		str = "can't access trace events";
46919658171SJiri Olsa 		break;
47019658171SJiri Olsa 	case ENOENT:
4714584f084SIan Rogers 		str = "unknown tracepoint";
47219658171SJiri Olsa 		break;
47319658171SJiri Olsa 	default:
4744584f084SIan Rogers 		str = "failed to add tracepoint";
47519658171SJiri Olsa 		break;
47619658171SJiri Olsa 	}
47719658171SJiri Olsa 
47819658171SJiri Olsa 	tracing_path__strerror_open_tp(err, help, sizeof(help), sys, name);
4796c191289SIan Rogers 	parse_events_error__handle(e, 0, strdup(str), strdup(help));
48019658171SJiri Olsa }
48119658171SJiri Olsa 
482c5cd8ac0SDavid Ahern static int add_tracepoint(struct list_head *list, int *idx,
4838c619d6aSWang Nan 			  const char *sys_name, const char *evt_name,
484272ed29aSJiri Olsa 			  struct parse_events_error *err,
485e637d177SHe Kuang 			  struct list_head *head_config)
486bcd3279fSFrederic Weisbecker {
4878f6725a2SArnaldo Carvalho de Melo 	struct evsel *evsel = evsel__newtp_idx(sys_name, evt_name, (*idx)++);
488bcd3279fSFrederic Weisbecker 
48919658171SJiri Olsa 	if (IS_ERR(evsel)) {
490272ed29aSJiri Olsa 		tracepoint_error(err, PTR_ERR(evsel), sys_name, evt_name);
4918dd2a131SJiri Olsa 		return PTR_ERR(evsel);
49219658171SJiri Olsa 	}
493bcd3279fSFrederic Weisbecker 
494e637d177SHe Kuang 	if (head_config) {
495e637d177SHe Kuang 		LIST_HEAD(config_terms);
496e637d177SHe Kuang 
497e637d177SHe Kuang 		if (get_config_terms(head_config, &config_terms))
498e637d177SHe Kuang 			return -ENOMEM;
499e637d177SHe Kuang 		list_splice(&config_terms, &evsel->config_terms);
500e637d177SHe Kuang 	}
501e637d177SHe Kuang 
502b27c4eceSJiri Olsa 	list_add_tail(&evsel->core.node, list);
50382fe1c29SArnaldo Carvalho de Melo 	return 0;
504bcd3279fSFrederic Weisbecker }
505bcd3279fSFrederic Weisbecker 
506c5cd8ac0SDavid Ahern static int add_tracepoint_multi_event(struct list_head *list, int *idx,
5078c619d6aSWang Nan 				      const char *sys_name, const char *evt_name,
508272ed29aSJiri Olsa 				      struct parse_events_error *err,
509e637d177SHe Kuang 				      struct list_head *head_config)
510bcd3279fSFrederic Weisbecker {
51125a7d914SArnaldo Carvalho de Melo 	char *evt_path;
512bcd3279fSFrederic Weisbecker 	struct dirent *evt_ent;
513bcd3279fSFrederic Weisbecker 	DIR *evt_dir;
51427bf90bfSJiri Olsa 	int ret = 0, found = 0;
515bcd3279fSFrederic Weisbecker 
51625a7d914SArnaldo Carvalho de Melo 	evt_path = get_events_file(sys_name);
51725a7d914SArnaldo Carvalho de Melo 	if (!evt_path) {
51825a7d914SArnaldo Carvalho de Melo 		tracepoint_error(err, errno, sys_name, evt_name);
51925a7d914SArnaldo Carvalho de Melo 		return -1;
52025a7d914SArnaldo Carvalho de Melo 	}
521bcd3279fSFrederic Weisbecker 	evt_dir = opendir(evt_path);
522bcd3279fSFrederic Weisbecker 	if (!evt_dir) {
52325a7d914SArnaldo Carvalho de Melo 		put_events_file(evt_path);
524272ed29aSJiri Olsa 		tracepoint_error(err, errno, sys_name, evt_name);
52589812fc8SJiri Olsa 		return -1;
526bcd3279fSFrederic Weisbecker 	}
527bcd3279fSFrederic Weisbecker 
52889812fc8SJiri Olsa 	while (!ret && (evt_ent = readdir(evt_dir))) {
529bcd3279fSFrederic Weisbecker 		if (!strcmp(evt_ent->d_name, ".")
530bcd3279fSFrederic Weisbecker 		    || !strcmp(evt_ent->d_name, "..")
531bcd3279fSFrederic Weisbecker 		    || !strcmp(evt_ent->d_name, "enable")
532bcd3279fSFrederic Weisbecker 		    || !strcmp(evt_ent->d_name, "filter"))
533bcd3279fSFrederic Weisbecker 			continue;
534bcd3279fSFrederic Weisbecker 
53589812fc8SJiri Olsa 		if (!strglobmatch(evt_ent->d_name, evt_name))
536fb1d2edfSMasami Hiramatsu 			continue;
537fb1d2edfSMasami Hiramatsu 
53827bf90bfSJiri Olsa 		found++;
53927bf90bfSJiri Olsa 
540e637d177SHe Kuang 		ret = add_tracepoint(list, idx, sys_name, evt_ent->d_name,
541272ed29aSJiri Olsa 				     err, head_config);
542bcd3279fSFrederic Weisbecker 	}
543bcd3279fSFrederic Weisbecker 
54427bf90bfSJiri Olsa 	if (!found) {
54527bf90bfSJiri Olsa 		tracepoint_error(err, ENOENT, sys_name, evt_name);
54627bf90bfSJiri Olsa 		ret = -1;
54727bf90bfSJiri Olsa 	}
54827bf90bfSJiri Olsa 
54925a7d914SArnaldo Carvalho de Melo 	put_events_file(evt_path);
5500bd3f084SJiri Olsa 	closedir(evt_dir);
55189812fc8SJiri Olsa 	return ret;
552bcd3279fSFrederic Weisbecker }
553bcd3279fSFrederic Weisbecker 
554c5cd8ac0SDavid Ahern static int add_tracepoint_event(struct list_head *list, int *idx,
5558c619d6aSWang Nan 				const char *sys_name, const char *evt_name,
556272ed29aSJiri Olsa 				struct parse_events_error *err,
557e637d177SHe Kuang 				struct list_head *head_config)
558f35488f9SJiri Olsa {
559f35488f9SJiri Olsa 	return strpbrk(evt_name, "*?") ?
560e637d177SHe Kuang 	       add_tracepoint_multi_event(list, idx, sys_name, evt_name,
561272ed29aSJiri Olsa 					  err, head_config) :
562e637d177SHe Kuang 	       add_tracepoint(list, idx, sys_name, evt_name,
563272ed29aSJiri Olsa 			      err, head_config);
564f35488f9SJiri Olsa }
565f35488f9SJiri Olsa 
566c5cd8ac0SDavid Ahern static int add_tracepoint_multi_sys(struct list_head *list, int *idx,
5678c619d6aSWang Nan 				    const char *sys_name, const char *evt_name,
568272ed29aSJiri Olsa 				    struct parse_events_error *err,
569e637d177SHe Kuang 				    struct list_head *head_config)
570f35488f9SJiri Olsa {
571f35488f9SJiri Olsa 	struct dirent *events_ent;
572f35488f9SJiri Olsa 	DIR *events_dir;
573f35488f9SJiri Olsa 	int ret = 0;
574f35488f9SJiri Olsa 
5757014e0e3SArnaldo Carvalho de Melo 	events_dir = tracing_events__opendir();
576f35488f9SJiri Olsa 	if (!events_dir) {
577272ed29aSJiri Olsa 		tracepoint_error(err, errno, sys_name, evt_name);
578f35488f9SJiri Olsa 		return -1;
579f35488f9SJiri Olsa 	}
580f35488f9SJiri Olsa 
581f35488f9SJiri Olsa 	while (!ret && (events_ent = readdir(events_dir))) {
582f35488f9SJiri Olsa 		if (!strcmp(events_ent->d_name, ".")
583f35488f9SJiri Olsa 		    || !strcmp(events_ent->d_name, "..")
584f35488f9SJiri Olsa 		    || !strcmp(events_ent->d_name, "enable")
585f35488f9SJiri Olsa 		    || !strcmp(events_ent->d_name, "header_event")
586f35488f9SJiri Olsa 		    || !strcmp(events_ent->d_name, "header_page"))
587f35488f9SJiri Olsa 			continue;
588f35488f9SJiri Olsa 
589f35488f9SJiri Olsa 		if (!strglobmatch(events_ent->d_name, sys_name))
590f35488f9SJiri Olsa 			continue;
591f35488f9SJiri Olsa 
592f35488f9SJiri Olsa 		ret = add_tracepoint_event(list, idx, events_ent->d_name,
593272ed29aSJiri Olsa 					   evt_name, err, head_config);
594f35488f9SJiri Olsa 	}
595f35488f9SJiri Olsa 
596f35488f9SJiri Olsa 	closedir(events_dir);
597f35488f9SJiri Olsa 	return ret;
598f35488f9SJiri Olsa }
599378ef0f5SIan Rogers #endif /* HAVE_LIBTRACEEVENT */
600f35488f9SJiri Olsa 
601c18cf78dSArnaldo Carvalho de Melo #ifdef HAVE_LIBBPF_SUPPORT
6024edf30e3SWang Nan struct __add_bpf_event_param {
6035d9cdc11SArnaldo Carvalho de Melo 	struct parse_events_state *parse_state;
6044edf30e3SWang Nan 	struct list_head *list;
60595088a59SWang Nan 	struct list_head *head_config;
6064edf30e3SWang Nan };
6074edf30e3SWang Nan 
608af4a0991SArnaldo Carvalho de Melo static int add_bpf_event(const char *group, const char *event, int fd, struct bpf_object *obj,
6094edf30e3SWang Nan 			 void *_param)
6104edf30e3SWang Nan {
6114edf30e3SWang Nan 	LIST_HEAD(new_evsels);
6124edf30e3SWang Nan 	struct __add_bpf_event_param *param = _param;
6135d9cdc11SArnaldo Carvalho de Melo 	struct parse_events_state *parse_state = param->parse_state;
6144edf30e3SWang Nan 	struct list_head *list = param->list;
61532dcd021SJiri Olsa 	struct evsel *pos;
6164edf30e3SWang Nan 	int err;
6172620b7e3SArnaldo Carvalho de Melo 	/*
6182620b7e3SArnaldo Carvalho de Melo 	 * Check if we should add the event, i.e. if it is a TP but starts with a '!',
6192620b7e3SArnaldo Carvalho de Melo 	 * then don't add the tracepoint, this will be used for something else, like
6202620b7e3SArnaldo Carvalho de Melo 	 * adding to a BPF_MAP_TYPE_PROG_ARRAY.
6212620b7e3SArnaldo Carvalho de Melo 	 *
6222620b7e3SArnaldo Carvalho de Melo 	 * See tools/perf/examples/bpf/augmented_raw_syscalls.c
6232620b7e3SArnaldo Carvalho de Melo 	 */
6242620b7e3SArnaldo Carvalho de Melo 	if (group[0] == '!')
6252620b7e3SArnaldo Carvalho de Melo 		return 0;
6264edf30e3SWang Nan 
6274edf30e3SWang Nan 	pr_debug("add bpf event %s:%s and attach bpf program %d\n",
628cd102d70SWang Nan 		 group, event, fd);
6294edf30e3SWang Nan 
6305d369a75SArnaldo Carvalho de Melo 	err = parse_events_add_tracepoint(&new_evsels, &parse_state->idx, group,
6315d369a75SArnaldo Carvalho de Melo 					  event, parse_state->error,
63295088a59SWang Nan 					  param->head_config);
6334edf30e3SWang Nan 	if (err) {
63432dcd021SJiri Olsa 		struct evsel *evsel, *tmp;
6354edf30e3SWang Nan 
6364edf30e3SWang Nan 		pr_debug("Failed to add BPF event %s:%s\n",
637cd102d70SWang Nan 			 group, event);
638b27c4eceSJiri Olsa 		list_for_each_entry_safe(evsel, tmp, &new_evsels, core.node) {
639b27c4eceSJiri Olsa 			list_del_init(&evsel->core.node);
6405eb2dd2aSJiri Olsa 			evsel__delete(evsel);
6414edf30e3SWang Nan 		}
6424edf30e3SWang Nan 		return err;
6434edf30e3SWang Nan 	}
644cd102d70SWang Nan 	pr_debug("adding %s:%s\n", group, event);
6454edf30e3SWang Nan 
646b27c4eceSJiri Olsa 	list_for_each_entry(pos, &new_evsels, core.node) {
6471f45b1d4SWang Nan 		pr_debug("adding %s:%s to %p\n",
648cd102d70SWang Nan 			 group, event, pos);
6491f45b1d4SWang Nan 		pos->bpf_fd = fd;
650af4a0991SArnaldo Carvalho de Melo 		pos->bpf_obj = obj;
6511f45b1d4SWang Nan 	}
6524edf30e3SWang Nan 	list_splice(&new_evsels, list);
6534edf30e3SWang Nan 	return 0;
6544edf30e3SWang Nan }
6554edf30e3SWang Nan 
6565d9cdc11SArnaldo Carvalho de Melo int parse_events_load_bpf_obj(struct parse_events_state *parse_state,
65784c86ca1SWang Nan 			      struct list_head *list,
65895088a59SWang Nan 			      struct bpf_object *obj,
65995088a59SWang Nan 			      struct list_head *head_config)
66084c86ca1SWang Nan {
66184c86ca1SWang Nan 	int err;
66284c86ca1SWang Nan 	char errbuf[BUFSIZ];
6635d9cdc11SArnaldo Carvalho de Melo 	struct __add_bpf_event_param param = {parse_state, list, head_config};
664aa3abf30SWang Nan 	static bool registered_unprobe_atexit = false;
66584c86ca1SWang Nan 
66684c86ca1SWang Nan 	if (IS_ERR(obj) || !obj) {
66784c86ca1SWang Nan 		snprintf(errbuf, sizeof(errbuf),
66884c86ca1SWang Nan 			 "Internal error: load bpf obj with NULL");
66984c86ca1SWang Nan 		err = -EINVAL;
67084c86ca1SWang Nan 		goto errout;
67184c86ca1SWang Nan 	}
67284c86ca1SWang Nan 
67384c86ca1SWang Nan 	/*
674aa3abf30SWang Nan 	 * Register atexit handler before calling bpf__probe() so
675aa3abf30SWang Nan 	 * bpf__probe() don't need to unprobe probe points its already
676aa3abf30SWang Nan 	 * created when failure.
677aa3abf30SWang Nan 	 */
678aa3abf30SWang Nan 	if (!registered_unprobe_atexit) {
679aa3abf30SWang Nan 		atexit(bpf__clear);
680aa3abf30SWang Nan 		registered_unprobe_atexit = true;
681aa3abf30SWang Nan 	}
682aa3abf30SWang Nan 
683aa3abf30SWang Nan 	err = bpf__probe(obj);
684aa3abf30SWang Nan 	if (err) {
685aa3abf30SWang Nan 		bpf__strerror_probe(obj, err, errbuf, sizeof(errbuf));
686aa3abf30SWang Nan 		goto errout;
687aa3abf30SWang Nan 	}
688aa3abf30SWang Nan 
6891e5e3ee8SWang Nan 	err = bpf__load(obj);
6901e5e3ee8SWang Nan 	if (err) {
6911e5e3ee8SWang Nan 		bpf__strerror_load(obj, err, errbuf, sizeof(errbuf));
6921e5e3ee8SWang Nan 		goto errout;
6931e5e3ee8SWang Nan 	}
6941e5e3ee8SWang Nan 
695cd102d70SWang Nan 	err = bpf__foreach_event(obj, add_bpf_event, &param);
6964edf30e3SWang Nan 	if (err) {
6974edf30e3SWang Nan 		snprintf(errbuf, sizeof(errbuf),
6984edf30e3SWang Nan 			 "Attach events in BPF object failed");
6994edf30e3SWang Nan 		goto errout;
7004edf30e3SWang Nan 	}
7014edf30e3SWang Nan 
7024edf30e3SWang Nan 	return 0;
70384c86ca1SWang Nan errout:
7046c191289SIan Rogers 	parse_events_error__handle(parse_state->error, 0,
7055f634c8eSIan Rogers 				strdup(errbuf), strdup("(add -v to see detail)"));
70684c86ca1SWang Nan 	return err;
70784c86ca1SWang Nan }
70884c86ca1SWang Nan 
709a34f3be7SWang Nan static int
7105d9cdc11SArnaldo Carvalho de Melo parse_events_config_bpf(struct parse_events_state *parse_state,
711a34f3be7SWang Nan 			struct bpf_object *obj,
712a34f3be7SWang Nan 			struct list_head *head_config)
713a34f3be7SWang Nan {
714a34f3be7SWang Nan 	struct parse_events_term *term;
715a34f3be7SWang Nan 	int error_pos;
716a34f3be7SWang Nan 
717a34f3be7SWang Nan 	if (!head_config || list_empty(head_config))
718a34f3be7SWang Nan 		return 0;
719a34f3be7SWang Nan 
720a34f3be7SWang Nan 	list_for_each_entry(term, head_config, list) {
721a34f3be7SWang Nan 		int err;
722a34f3be7SWang Nan 
723a34f3be7SWang Nan 		if (term->type_term != PARSE_EVENTS__TERM_TYPE_USER) {
7246c191289SIan Rogers 			parse_events_error__handle(parse_state->error, term->err_term,
7255f634c8eSIan Rogers 						strdup("Invalid config term for BPF object"),
7265f634c8eSIan Rogers 						NULL);
727a34f3be7SWang Nan 			return -EINVAL;
728a34f3be7SWang Nan 		}
729a34f3be7SWang Nan 
7305d9cdc11SArnaldo Carvalho de Melo 		err = bpf__config_obj(obj, term, parse_state->evlist, &error_pos);
731a34f3be7SWang Nan 		if (err) {
7325f634c8eSIan Rogers 			char errbuf[BUFSIZ];
7335f634c8eSIan Rogers 			int idx;
7345f634c8eSIan Rogers 
7355d9cdc11SArnaldo Carvalho de Melo 			bpf__strerror_config_obj(obj, term, parse_state->evlist,
736a34f3be7SWang Nan 						 &error_pos, err, errbuf,
737a34f3be7SWang Nan 						 sizeof(errbuf));
7385f634c8eSIan Rogers 
7395f634c8eSIan Rogers 			if (err == -BPF_LOADER_ERRNO__OBJCONF_MAP_VALUE)
7405f634c8eSIan Rogers 				idx = term->err_val;
7415f634c8eSIan Rogers 			else
7425f634c8eSIan Rogers 				idx = term->err_term + error_pos;
7435f634c8eSIan Rogers 
7446c191289SIan Rogers 			parse_events_error__handle(parse_state->error, idx,
7455f634c8eSIan Rogers 						strdup(errbuf),
7465f634c8eSIan Rogers 						strdup(
7477630b3e2SWang Nan "Hint:\tValid config terms:\n"
748e571e029SWang Nan "     \tmap:[<arraymap>].value<indices>=[value]\n"
749e571e029SWang Nan "     \tmap:[<eventmap>].event<indices>=[event]\n"
7507630b3e2SWang Nan "\n"
751e571e029SWang Nan "     \twhere <indices> is something like [0,3...5] or [all]\n"
7525f634c8eSIan Rogers "     \t(add -v to see detail)"));
753a34f3be7SWang Nan 			return err;
754a34f3be7SWang Nan 		}
755a34f3be7SWang Nan 	}
756a34f3be7SWang Nan 	return 0;
757a34f3be7SWang Nan }
758a34f3be7SWang Nan 
75995088a59SWang Nan /*
76095088a59SWang Nan  * Split config terms:
76195088a59SWang Nan  * perf record -e bpf.c/call-graph=fp,map:array.value[0]=1/ ...
76295088a59SWang Nan  *  'call-graph=fp' is 'evt config', should be applied to each
76395088a59SWang Nan  *  events in bpf.c.
76495088a59SWang Nan  * 'map:array.value[0]=1' is 'obj config', should be processed
76595088a59SWang Nan  * with parse_events_config_bpf.
76695088a59SWang Nan  *
76795088a59SWang Nan  * Move object config terms from the first list to obj_head_config.
76895088a59SWang Nan  */
76995088a59SWang Nan static void
77095088a59SWang Nan split_bpf_config_terms(struct list_head *evt_head_config,
77195088a59SWang Nan 		       struct list_head *obj_head_config)
77295088a59SWang Nan {
77395088a59SWang Nan 	struct parse_events_term *term, *temp;
77495088a59SWang Nan 
77595088a59SWang Nan 	/*
7764d39c89fSIngo Molnar 	 * Currently, all possible user config term
77795088a59SWang Nan 	 * belong to bpf object. parse_events__is_hardcoded_term()
7784d39c89fSIngo Molnar 	 * happens to be a good flag.
77995088a59SWang Nan 	 *
78095088a59SWang Nan 	 * See parse_events_config_bpf() and
78195088a59SWang Nan 	 * config_term_tracepoint().
78295088a59SWang Nan 	 */
78395088a59SWang Nan 	list_for_each_entry_safe(term, temp, evt_head_config, list)
78495088a59SWang Nan 		if (!parse_events__is_hardcoded_term(term))
78595088a59SWang Nan 			list_move_tail(&term->list, obj_head_config);
78695088a59SWang Nan }
78795088a59SWang Nan 
7885d9cdc11SArnaldo Carvalho de Melo int parse_events_load_bpf(struct parse_events_state *parse_state,
78984c86ca1SWang Nan 			  struct list_head *list,
790d509db04SWang Nan 			  char *bpf_file_name,
791a34f3be7SWang Nan 			  bool source,
792a34f3be7SWang Nan 			  struct list_head *head_config)
79384c86ca1SWang Nan {
794a34f3be7SWang Nan 	int err;
79595088a59SWang Nan 	struct bpf_object *obj;
79695088a59SWang Nan 	LIST_HEAD(obj_head_config);
79795088a59SWang Nan 
79895088a59SWang Nan 	if (head_config)
79995088a59SWang Nan 		split_bpf_config_terms(head_config, &obj_head_config);
80084c86ca1SWang Nan 
801d509db04SWang Nan 	obj = bpf__prepare_load(bpf_file_name, source);
8026371ca3bSWang Nan 	if (IS_ERR(obj)) {
80384c86ca1SWang Nan 		char errbuf[BUFSIZ];
80484c86ca1SWang Nan 
8056371ca3bSWang Nan 		err = PTR_ERR(obj);
80684c86ca1SWang Nan 
80784c86ca1SWang Nan 		if (err == -ENOTSUP)
80884c86ca1SWang Nan 			snprintf(errbuf, sizeof(errbuf),
80984c86ca1SWang Nan 				 "BPF support is not compiled");
81084c86ca1SWang Nan 		else
811d3e0ce39SWang Nan 			bpf__strerror_prepare_load(bpf_file_name,
812d3e0ce39SWang Nan 						   source,
813d3e0ce39SWang Nan 						   -err, errbuf,
814d3e0ce39SWang Nan 						   sizeof(errbuf));
81584c86ca1SWang Nan 
8166c191289SIan Rogers 		parse_events_error__handle(parse_state->error, 0,
8175f634c8eSIan Rogers 					strdup(errbuf), strdup("(add -v to see detail)"));
81884c86ca1SWang Nan 		return err;
81984c86ca1SWang Nan 	}
82084c86ca1SWang Nan 
8215d9cdc11SArnaldo Carvalho de Melo 	err = parse_events_load_bpf_obj(parse_state, list, obj, head_config);
822a34f3be7SWang Nan 	if (err)
823a34f3be7SWang Nan 		return err;
8245d9cdc11SArnaldo Carvalho de Melo 	err = parse_events_config_bpf(parse_state, obj, &obj_head_config);
82595088a59SWang Nan 
82695088a59SWang Nan 	/*
82795088a59SWang Nan 	 * Caller doesn't know anything about obj_head_config,
8284d39c89fSIngo Molnar 	 * so combine them together again before returning.
82995088a59SWang Nan 	 */
83095088a59SWang Nan 	if (head_config)
83195088a59SWang Nan 		list_splice_tail(&obj_head_config, head_config);
83295088a59SWang Nan 	return err;
83384c86ca1SWang Nan }
834c18cf78dSArnaldo Carvalho de Melo #else // HAVE_LIBBPF_SUPPORT
835c18cf78dSArnaldo Carvalho de Melo int parse_events_load_bpf_obj(struct parse_events_state *parse_state,
836c18cf78dSArnaldo Carvalho de Melo 			      struct list_head *list __maybe_unused,
837c18cf78dSArnaldo Carvalho de Melo 			      struct bpf_object *obj __maybe_unused,
838c18cf78dSArnaldo Carvalho de Melo 			      struct list_head *head_config __maybe_unused)
839c18cf78dSArnaldo Carvalho de Melo {
8406c191289SIan Rogers 	parse_events_error__handle(parse_state->error, 0,
841c18cf78dSArnaldo Carvalho de Melo 				   strdup("BPF support is not compiled"),
842c18cf78dSArnaldo Carvalho de Melo 				   strdup("Make sure libbpf-devel is available at build time."));
843c18cf78dSArnaldo Carvalho de Melo 	return -ENOTSUP;
844c18cf78dSArnaldo Carvalho de Melo }
845c18cf78dSArnaldo Carvalho de Melo 
846c18cf78dSArnaldo Carvalho de Melo int parse_events_load_bpf(struct parse_events_state *parse_state,
847c18cf78dSArnaldo Carvalho de Melo 			  struct list_head *list __maybe_unused,
848c18cf78dSArnaldo Carvalho de Melo 			  char *bpf_file_name __maybe_unused,
849c18cf78dSArnaldo Carvalho de Melo 			  bool source __maybe_unused,
850c18cf78dSArnaldo Carvalho de Melo 			  struct list_head *head_config __maybe_unused)
851c18cf78dSArnaldo Carvalho de Melo {
8526c191289SIan Rogers 	parse_events_error__handle(parse_state->error, 0,
853c18cf78dSArnaldo Carvalho de Melo 				   strdup("BPF support is not compiled"),
854c18cf78dSArnaldo Carvalho de Melo 				   strdup("Make sure libbpf-devel is available at build time."));
855c18cf78dSArnaldo Carvalho de Melo 	return -ENOTSUP;
856c18cf78dSArnaldo Carvalho de Melo }
857c18cf78dSArnaldo Carvalho de Melo #endif // HAVE_LIBBPF_SUPPORT
85884c86ca1SWang Nan 
85989812fc8SJiri Olsa static int
86089812fc8SJiri Olsa parse_breakpoint_type(const char *type, struct perf_event_attr *attr)
8611b290d67SFrederic Weisbecker {
8621b290d67SFrederic Weisbecker 	int i;
8631b290d67SFrederic Weisbecker 
8641b290d67SFrederic Weisbecker 	for (i = 0; i < 3; i++) {
86589812fc8SJiri Olsa 		if (!type || !type[i])
8661b290d67SFrederic Weisbecker 			break;
8671b290d67SFrederic Weisbecker 
8687582732fSJiri Olsa #define CHECK_SET_TYPE(bit)		\
8697582732fSJiri Olsa do {					\
8707582732fSJiri Olsa 	if (attr->bp_type & bit)	\
8717582732fSJiri Olsa 		return -EINVAL;		\
8727582732fSJiri Olsa 	else				\
8737582732fSJiri Olsa 		attr->bp_type |= bit;	\
8747582732fSJiri Olsa } while (0)
8757582732fSJiri Olsa 
8761b290d67SFrederic Weisbecker 		switch (type[i]) {
8771b290d67SFrederic Weisbecker 		case 'r':
8787582732fSJiri Olsa 			CHECK_SET_TYPE(HW_BREAKPOINT_R);
8791b290d67SFrederic Weisbecker 			break;
8801b290d67SFrederic Weisbecker 		case 'w':
8817582732fSJiri Olsa 			CHECK_SET_TYPE(HW_BREAKPOINT_W);
8821b290d67SFrederic Weisbecker 			break;
8831b290d67SFrederic Weisbecker 		case 'x':
8847582732fSJiri Olsa 			CHECK_SET_TYPE(HW_BREAKPOINT_X);
8851b290d67SFrederic Weisbecker 			break;
8861b290d67SFrederic Weisbecker 		default:
88789812fc8SJiri Olsa 			return -EINVAL;
8881b290d67SFrederic Weisbecker 		}
8891b290d67SFrederic Weisbecker 	}
89089812fc8SJiri Olsa 
8917582732fSJiri Olsa #undef CHECK_SET_TYPE
8927582732fSJiri Olsa 
8931b290d67SFrederic Weisbecker 	if (!attr->bp_type) /* Default */
8941b290d67SFrederic Weisbecker 		attr->bp_type = HW_BREAKPOINT_R | HW_BREAKPOINT_W;
8951b290d67SFrederic Weisbecker 
89689812fc8SJiri Olsa 	return 0;
8971b290d67SFrederic Weisbecker }
8981b290d67SFrederic Weisbecker 
899c5cd8ac0SDavid Ahern int parse_events_add_breakpoint(struct list_head *list, int *idx,
900aa98d848SIan Rogers 				u64 addr, char *type, u64 len)
9011b290d67SFrederic Weisbecker {
90289812fc8SJiri Olsa 	struct perf_event_attr attr;
9031b290d67SFrederic Weisbecker 
90489812fc8SJiri Olsa 	memset(&attr, 0, sizeof(attr));
905aa98d848SIan Rogers 	attr.bp_addr = addr;
9061b290d67SFrederic Weisbecker 
90789812fc8SJiri Olsa 	if (parse_breakpoint_type(type, &attr))
90889812fc8SJiri Olsa 		return -EINVAL;
9091b290d67SFrederic Weisbecker 
9103741eb9fSJacob Shin 	/* Provide some defaults if len is not specified */
9113741eb9fSJacob Shin 	if (!len) {
91289812fc8SJiri Olsa 		if (attr.bp_type == HW_BREAKPOINT_X)
9133741eb9fSJacob Shin 			len = sizeof(long);
914aa59a485SFrederic Weisbecker 		else
9153741eb9fSJacob Shin 			len = HW_BREAKPOINT_LEN_4;
9163741eb9fSJacob Shin 	}
9173741eb9fSJacob Shin 
9183741eb9fSJacob Shin 	attr.bp_len = len;
919aa59a485SFrederic Weisbecker 
92089812fc8SJiri Olsa 	attr.type = PERF_TYPE_BREAKPOINT;
9214a841d65SJovi Zhang 	attr.sample_period = 1;
9221b290d67SFrederic Weisbecker 
9232b62b3a6SIan Rogers 	return add_event(list, idx, &attr, /*name=*/NULL, /*mertic_id=*/NULL,
9242b62b3a6SIan Rogers 			 /*config_terms=*/NULL);
9251b290d67SFrederic Weisbecker }
9261b290d67SFrederic Weisbecker 
9273b0e371cSJiri Olsa static int check_type_val(struct parse_events_term *term,
9283b0e371cSJiri Olsa 			  struct parse_events_error *err,
9293b0e371cSJiri Olsa 			  int type)
9303b0e371cSJiri Olsa {
9313b0e371cSJiri Olsa 	if (type == term->type_val)
9323b0e371cSJiri Olsa 		return 0;
9333b0e371cSJiri Olsa 
9343b0e371cSJiri Olsa 	if (err) {
9356c191289SIan Rogers 		parse_events_error__handle(err, term->err_val,
936448d732cSIan Rogers 					type == PARSE_EVENTS__TERM_TYPE_NUM
937448d732cSIan Rogers 					? strdup("expected numeric value")
938448d732cSIan Rogers 					: strdup("expected string value"),
939448d732cSIan Rogers 					NULL);
9403b0e371cSJiri Olsa 	}
9413b0e371cSJiri Olsa 	return -EINVAL;
9423b0e371cSJiri Olsa }
9433b0e371cSJiri Olsa 
94417cb5f84SWang Nan /*
94517cb5f84SWang Nan  * Update according to parse-events.l
94617cb5f84SWang Nan  */
94717cb5f84SWang Nan static const char *config_term_names[__PARSE_EVENTS__TERM_TYPE_NR] = {
94817cb5f84SWang Nan 	[PARSE_EVENTS__TERM_TYPE_USER]			= "<sysfs term>",
94917cb5f84SWang Nan 	[PARSE_EVENTS__TERM_TYPE_CONFIG]		= "config",
95017cb5f84SWang Nan 	[PARSE_EVENTS__TERM_TYPE_CONFIG1]		= "config1",
95117cb5f84SWang Nan 	[PARSE_EVENTS__TERM_TYPE_CONFIG2]		= "config2",
952204e7c49SRob Herring 	[PARSE_EVENTS__TERM_TYPE_CONFIG3]		= "config3",
95317cb5f84SWang Nan 	[PARSE_EVENTS__TERM_TYPE_NAME]			= "name",
95417cb5f84SWang Nan 	[PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD]		= "period",
95517cb5f84SWang Nan 	[PARSE_EVENTS__TERM_TYPE_SAMPLE_FREQ]		= "freq",
95617cb5f84SWang Nan 	[PARSE_EVENTS__TERM_TYPE_BRANCH_SAMPLE_TYPE]	= "branch_type",
95717cb5f84SWang Nan 	[PARSE_EVENTS__TERM_TYPE_TIME]			= "time",
95817cb5f84SWang Nan 	[PARSE_EVENTS__TERM_TYPE_CALLGRAPH]		= "call-graph",
95917cb5f84SWang Nan 	[PARSE_EVENTS__TERM_TYPE_STACKSIZE]		= "stack-size",
96017cb5f84SWang Nan 	[PARSE_EVENTS__TERM_TYPE_NOINHERIT]		= "no-inherit",
96117cb5f84SWang Nan 	[PARSE_EVENTS__TERM_TYPE_INHERIT]		= "inherit",
962792d48b4SArnaldo Carvalho de Melo 	[PARSE_EVENTS__TERM_TYPE_MAX_STACK]		= "max-stack",
9632fda5adaSArnaldo Carvalho de Melo 	[PARSE_EVENTS__TERM_TYPE_MAX_EVENTS]		= "nr",
964626a6b78SWang Nan 	[PARSE_EVENTS__TERM_TYPE_OVERWRITE]		= "overwrite",
965626a6b78SWang Nan 	[PARSE_EVENTS__TERM_TYPE_NOOVERWRITE]		= "no-overwrite",
966dd60fba7SMathieu Poirier 	[PARSE_EVENTS__TERM_TYPE_DRV_CFG]		= "driver-config",
967064b4e82SJin Yao 	[PARSE_EVENTS__TERM_TYPE_PERCORE]		= "percore",
9681b992154SAdrian Hunter 	[PARSE_EVENTS__TERM_TYPE_AUX_OUTPUT]		= "aux-output",
969eb7a52d4SAdrian Hunter 	[PARSE_EVENTS__TERM_TYPE_AUX_SAMPLE_SIZE]	= "aux-sample-size",
9702b62b3a6SIan Rogers 	[PARSE_EVENTS__TERM_TYPE_METRIC_ID]		= "metric-id",
97117cb5f84SWang Nan };
97217cb5f84SWang Nan 
9731669e509SWang Nan static bool config_term_shrinked;
9741669e509SWang Nan 
9751669e509SWang Nan static bool
9761669e509SWang Nan config_term_avail(int term_type, struct parse_events_error *err)
9771669e509SWang Nan {
978448d732cSIan Rogers 	char *err_str;
979448d732cSIan Rogers 
9801669e509SWang Nan 	if (term_type < 0 || term_type >= __PARSE_EVENTS__TERM_TYPE_NR) {
9816c191289SIan Rogers 		parse_events_error__handle(err, -1,
982448d732cSIan Rogers 					strdup("Invalid term_type"), NULL);
9831669e509SWang Nan 		return false;
9841669e509SWang Nan 	}
9851669e509SWang Nan 	if (!config_term_shrinked)
9861669e509SWang Nan 		return true;
9871669e509SWang Nan 
9881669e509SWang Nan 	switch (term_type) {
9891669e509SWang Nan 	case PARSE_EVENTS__TERM_TYPE_CONFIG:
9901669e509SWang Nan 	case PARSE_EVENTS__TERM_TYPE_CONFIG1:
9911669e509SWang Nan 	case PARSE_EVENTS__TERM_TYPE_CONFIG2:
992204e7c49SRob Herring 	case PARSE_EVENTS__TERM_TYPE_CONFIG3:
9931669e509SWang Nan 	case PARSE_EVENTS__TERM_TYPE_NAME:
9942b62b3a6SIan Rogers 	case PARSE_EVENTS__TERM_TYPE_METRIC_ID:
99506835545SSukadev Bhattiprolu 	case PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD:
996064b4e82SJin Yao 	case PARSE_EVENTS__TERM_TYPE_PERCORE:
9971669e509SWang Nan 		return true;
9981669e509SWang Nan 	default:
9991669e509SWang Nan 		if (!err)
10001669e509SWang Nan 			return false;
10011669e509SWang Nan 
10021669e509SWang Nan 		/* term_type is validated so indexing is safe */
1003448d732cSIan Rogers 		if (asprintf(&err_str, "'%s' is not usable in 'perf stat'",
1004448d732cSIan Rogers 				config_term_names[term_type]) >= 0)
10056c191289SIan Rogers 			parse_events_error__handle(err, -1, err_str, NULL);
10061669e509SWang Nan 		return false;
10071669e509SWang Nan 	}
10081669e509SWang Nan }
10091669e509SWang Nan 
10101669e509SWang Nan void parse_events__shrink_config_terms(void)
10111669e509SWang Nan {
10121669e509SWang Nan 	config_term_shrinked = true;
10131669e509SWang Nan }
10141669e509SWang Nan 
10150b8891a8SHe Kuang static int config_term_common(struct perf_event_attr *attr,
10163b0e371cSJiri Olsa 			      struct parse_events_term *term,
10173b0e371cSJiri Olsa 			      struct parse_events_error *err)
10188f707d84SJiri Olsa {
101916fa7e82SJiri Olsa #define CHECK_TYPE_VAL(type)						   \
102016fa7e82SJiri Olsa do {									   \
10213b0e371cSJiri Olsa 	if (check_type_val(term, err, PARSE_EVENTS__TERM_TYPE_ ## type)) \
102216fa7e82SJiri Olsa 		return -EINVAL;						   \
102316fa7e82SJiri Olsa } while (0)
102416fa7e82SJiri Olsa 
102516fa7e82SJiri Olsa 	switch (term->type_term) {
10268f707d84SJiri Olsa 	case PARSE_EVENTS__TERM_TYPE_CONFIG:
102716fa7e82SJiri Olsa 		CHECK_TYPE_VAL(NUM);
10288f707d84SJiri Olsa 		attr->config = term->val.num;
10298f707d84SJiri Olsa 		break;
10308f707d84SJiri Olsa 	case PARSE_EVENTS__TERM_TYPE_CONFIG1:
103116fa7e82SJiri Olsa 		CHECK_TYPE_VAL(NUM);
10328f707d84SJiri Olsa 		attr->config1 = term->val.num;
10338f707d84SJiri Olsa 		break;
10348f707d84SJiri Olsa 	case PARSE_EVENTS__TERM_TYPE_CONFIG2:
103516fa7e82SJiri Olsa 		CHECK_TYPE_VAL(NUM);
10368f707d84SJiri Olsa 		attr->config2 = term->val.num;
10378f707d84SJiri Olsa 		break;
1038204e7c49SRob Herring 	case PARSE_EVENTS__TERM_TYPE_CONFIG3:
1039204e7c49SRob Herring 		CHECK_TYPE_VAL(NUM);
1040204e7c49SRob Herring 		attr->config3 = term->val.num;
1041204e7c49SRob Herring 		break;
10428f707d84SJiri Olsa 	case PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD:
104316fa7e82SJiri Olsa 		CHECK_TYPE_VAL(NUM);
10448f707d84SJiri Olsa 		break;
104509af2a55SNamhyung Kim 	case PARSE_EVENTS__TERM_TYPE_SAMPLE_FREQ:
104609af2a55SNamhyung Kim 		CHECK_TYPE_VAL(NUM);
104709af2a55SNamhyung Kim 		break;
10488f707d84SJiri Olsa 	case PARSE_EVENTS__TERM_TYPE_BRANCH_SAMPLE_TYPE:
1049ac12f676SAndi Kleen 		CHECK_TYPE_VAL(STR);
1050ac12f676SAndi Kleen 		if (strcmp(term->val.str, "no") &&
1051448d732cSIan Rogers 		    parse_branch_str(term->val.str,
1052448d732cSIan Rogers 				    &attr->branch_sample_type)) {
10536c191289SIan Rogers 			parse_events_error__handle(err, term->err_val,
1054448d732cSIan Rogers 					strdup("invalid branch sample type"),
1055448d732cSIan Rogers 					NULL);
1056ac12f676SAndi Kleen 			return -EINVAL;
1057ac12f676SAndi Kleen 		}
10588f707d84SJiri Olsa 		break;
105932067712SKan Liang 	case PARSE_EVENTS__TERM_TYPE_TIME:
106032067712SKan Liang 		CHECK_TYPE_VAL(NUM);
106132067712SKan Liang 		if (term->val.num > 1) {
10626c191289SIan Rogers 			parse_events_error__handle(err, term->err_val,
1063448d732cSIan Rogers 						strdup("expected 0 or 1"),
1064448d732cSIan Rogers 						NULL);
106532067712SKan Liang 			return -EINVAL;
106632067712SKan Liang 		}
106732067712SKan Liang 		break;
1068d457c963SKan Liang 	case PARSE_EVENTS__TERM_TYPE_CALLGRAPH:
1069d457c963SKan Liang 		CHECK_TYPE_VAL(STR);
1070d457c963SKan Liang 		break;
1071d457c963SKan Liang 	case PARSE_EVENTS__TERM_TYPE_STACKSIZE:
1072d457c963SKan Liang 		CHECK_TYPE_VAL(NUM);
1073d457c963SKan Liang 		break;
1074374ce938SWang Nan 	case PARSE_EVENTS__TERM_TYPE_INHERIT:
1075374ce938SWang Nan 		CHECK_TYPE_VAL(NUM);
1076374ce938SWang Nan 		break;
1077374ce938SWang Nan 	case PARSE_EVENTS__TERM_TYPE_NOINHERIT:
1078374ce938SWang Nan 		CHECK_TYPE_VAL(NUM);
1079374ce938SWang Nan 		break;
1080626a6b78SWang Nan 	case PARSE_EVENTS__TERM_TYPE_OVERWRITE:
1081626a6b78SWang Nan 		CHECK_TYPE_VAL(NUM);
1082626a6b78SWang Nan 		break;
1083626a6b78SWang Nan 	case PARSE_EVENTS__TERM_TYPE_NOOVERWRITE:
1084626a6b78SWang Nan 		CHECK_TYPE_VAL(NUM);
1085626a6b78SWang Nan 		break;
10866b5fc39bSJiri Olsa 	case PARSE_EVENTS__TERM_TYPE_NAME:
10876b5fc39bSJiri Olsa 		CHECK_TYPE_VAL(STR);
10886b5fc39bSJiri Olsa 		break;
10892b62b3a6SIan Rogers 	case PARSE_EVENTS__TERM_TYPE_METRIC_ID:
10902b62b3a6SIan Rogers 		CHECK_TYPE_VAL(STR);
10912b62b3a6SIan Rogers 		break;
1092792d48b4SArnaldo Carvalho de Melo 	case PARSE_EVENTS__TERM_TYPE_MAX_STACK:
1093792d48b4SArnaldo Carvalho de Melo 		CHECK_TYPE_VAL(NUM);
1094792d48b4SArnaldo Carvalho de Melo 		break;
10952fda5adaSArnaldo Carvalho de Melo 	case PARSE_EVENTS__TERM_TYPE_MAX_EVENTS:
10962fda5adaSArnaldo Carvalho de Melo 		CHECK_TYPE_VAL(NUM);
10972fda5adaSArnaldo Carvalho de Melo 		break;
1098064b4e82SJin Yao 	case PARSE_EVENTS__TERM_TYPE_PERCORE:
1099064b4e82SJin Yao 		CHECK_TYPE_VAL(NUM);
1100064b4e82SJin Yao 		if ((unsigned int)term->val.num > 1) {
11016c191289SIan Rogers 			parse_events_error__handle(err, term->err_val,
1102448d732cSIan Rogers 						strdup("expected 0 or 1"),
1103448d732cSIan Rogers 						NULL);
1104064b4e82SJin Yao 			return -EINVAL;
1105064b4e82SJin Yao 		}
1106064b4e82SJin Yao 		break;
11071b992154SAdrian Hunter 	case PARSE_EVENTS__TERM_TYPE_AUX_OUTPUT:
11081b992154SAdrian Hunter 		CHECK_TYPE_VAL(NUM);
11091b992154SAdrian Hunter 		break;
1110eb7a52d4SAdrian Hunter 	case PARSE_EVENTS__TERM_TYPE_AUX_SAMPLE_SIZE:
1111eb7a52d4SAdrian Hunter 		CHECK_TYPE_VAL(NUM);
1112eb7a52d4SAdrian Hunter 		if (term->val.num > UINT_MAX) {
11136c191289SIan Rogers 			parse_events_error__handle(err, term->err_val,
1114eb7a52d4SAdrian Hunter 						strdup("too big"),
1115eb7a52d4SAdrian Hunter 						NULL);
1116eb7a52d4SAdrian Hunter 			return -EINVAL;
1117eb7a52d4SAdrian Hunter 		}
1118eb7a52d4SAdrian Hunter 		break;
11198f707d84SJiri Olsa 	default:
11206c191289SIan Rogers 		parse_events_error__handle(err, term->err_term,
1121448d732cSIan Rogers 				strdup("unknown term"),
1122448d732cSIan Rogers 				parse_events_formats_error_string(NULL));
11238f707d84SJiri Olsa 		return -EINVAL;
11248f707d84SJiri Olsa 	}
112516fa7e82SJiri Olsa 
11261669e509SWang Nan 	/*
11274d39c89fSIngo Molnar 	 * Check term availability after basic checking so
11281669e509SWang Nan 	 * PARSE_EVENTS__TERM_TYPE_USER can be found and filtered.
11291669e509SWang Nan 	 *
11304d39c89fSIngo Molnar 	 * If check availability at the entry of this function,
11311669e509SWang Nan 	 * user will see "'<sysfs term>' is not usable in 'perf stat'"
11321669e509SWang Nan 	 * if an invalid config term is provided for legacy events
11331669e509SWang Nan 	 * (for example, instructions/badterm/...), which is confusing.
11341669e509SWang Nan 	 */
11351669e509SWang Nan 	if (!config_term_avail(term->type_term, err))
11361669e509SWang Nan 		return -EINVAL;
11378f707d84SJiri Olsa 	return 0;
113816fa7e82SJiri Olsa #undef CHECK_TYPE_VAL
11398f707d84SJiri Olsa }
11408f707d84SJiri Olsa 
11410b8891a8SHe Kuang static int config_term_pmu(struct perf_event_attr *attr,
11420b8891a8SHe Kuang 			   struct parse_events_term *term,
11430b8891a8SHe Kuang 			   struct parse_events_error *err)
11440b8891a8SHe Kuang {
1145dd60fba7SMathieu Poirier 	if (term->type_term == PARSE_EVENTS__TERM_TYPE_USER ||
1146dd60fba7SMathieu Poirier 	    term->type_term == PARSE_EVENTS__TERM_TYPE_DRV_CFG)
11470b8891a8SHe Kuang 		/*
11480b8891a8SHe Kuang 		 * Always succeed for sysfs terms, as we dont know
11490b8891a8SHe Kuang 		 * at this point what type they need to have.
11500b8891a8SHe Kuang 		 */
11510b8891a8SHe Kuang 		return 0;
11520b8891a8SHe Kuang 	else
11530b8891a8SHe Kuang 		return config_term_common(attr, term, err);
11540b8891a8SHe Kuang }
11550b8891a8SHe Kuang 
1156378ef0f5SIan Rogers #ifdef HAVE_LIBTRACEEVENT
1157e637d177SHe Kuang static int config_term_tracepoint(struct perf_event_attr *attr,
1158e637d177SHe Kuang 				  struct parse_events_term *term,
1159e637d177SHe Kuang 				  struct parse_events_error *err)
1160e637d177SHe Kuang {
1161e637d177SHe Kuang 	switch (term->type_term) {
1162e637d177SHe Kuang 	case PARSE_EVENTS__TERM_TYPE_CALLGRAPH:
1163e637d177SHe Kuang 	case PARSE_EVENTS__TERM_TYPE_STACKSIZE:
1164374ce938SWang Nan 	case PARSE_EVENTS__TERM_TYPE_INHERIT:
1165374ce938SWang Nan 	case PARSE_EVENTS__TERM_TYPE_NOINHERIT:
1166792d48b4SArnaldo Carvalho de Melo 	case PARSE_EVENTS__TERM_TYPE_MAX_STACK:
11672fda5adaSArnaldo Carvalho de Melo 	case PARSE_EVENTS__TERM_TYPE_MAX_EVENTS:
1168626a6b78SWang Nan 	case PARSE_EVENTS__TERM_TYPE_OVERWRITE:
1169626a6b78SWang Nan 	case PARSE_EVENTS__TERM_TYPE_NOOVERWRITE:
11701b992154SAdrian Hunter 	case PARSE_EVENTS__TERM_TYPE_AUX_OUTPUT:
1171eb7a52d4SAdrian Hunter 	case PARSE_EVENTS__TERM_TYPE_AUX_SAMPLE_SIZE:
1172e637d177SHe Kuang 		return config_term_common(attr, term, err);
1173e637d177SHe Kuang 	default:
1174e637d177SHe Kuang 		if (err) {
11756c191289SIan Rogers 			parse_events_error__handle(err, term->err_term,
1176448d732cSIan Rogers 				strdup("unknown term"),
1177448d732cSIan Rogers 				strdup("valid terms: call-graph,stack-size\n"));
1178e637d177SHe Kuang 		}
1179e637d177SHe Kuang 		return -EINVAL;
1180e637d177SHe Kuang 	}
1181e637d177SHe Kuang 
1182e637d177SHe Kuang 	return 0;
1183e637d177SHe Kuang }
1184378ef0f5SIan Rogers #endif
1185e637d177SHe Kuang 
11868f707d84SJiri Olsa static int config_attr(struct perf_event_attr *attr,
11873b0e371cSJiri Olsa 		       struct list_head *head,
11880b8891a8SHe Kuang 		       struct parse_events_error *err,
11890b8891a8SHe Kuang 		       config_term_func_t config_term)
11908f707d84SJiri Olsa {
11916cee6cd3SArnaldo Carvalho de Melo 	struct parse_events_term *term;
11928f707d84SJiri Olsa 
11938f707d84SJiri Olsa 	list_for_each_entry(term, head, list)
11943b0e371cSJiri Olsa 		if (config_term(attr, term, err))
11958f707d84SJiri Olsa 			return -EINVAL;
11968f707d84SJiri Olsa 
11978f707d84SJiri Olsa 	return 0;
11988f707d84SJiri Olsa }
11998f707d84SJiri Olsa 
1200930a2e29SJiri Olsa static int get_config_terms(struct list_head *head_config,
1201930a2e29SJiri Olsa 			    struct list_head *head_terms __maybe_unused)
1202930a2e29SJiri Olsa {
120305e54e23SIan Rogers #define ADD_CONFIG_TERM(__type, __weak)				\
120435ac0cadSArnaldo Carvalho de Melo 	struct evsel_config_term *__t;			\
1205930a2e29SJiri Olsa 								\
1206930a2e29SJiri Olsa 	__t = zalloc(sizeof(*__t));				\
1207930a2e29SJiri Olsa 	if (!__t)						\
1208930a2e29SJiri Olsa 		return -ENOMEM;					\
1209930a2e29SJiri Olsa 								\
1210930a2e29SJiri Olsa 	INIT_LIST_HEAD(&__t->list);				\
121135ac0cadSArnaldo Carvalho de Melo 	__t->type       = EVSEL__CONFIG_TERM_ ## __type;	\
121205e54e23SIan Rogers 	__t->weak	= __weak;				\
1213e884602bSLeo Yan 	list_add_tail(&__t->list, head_terms)
1214e884602bSLeo Yan 
121505e54e23SIan Rogers #define ADD_CONFIG_TERM_VAL(__type, __name, __val, __weak)	\
1216e884602bSLeo Yan do {								\
121705e54e23SIan Rogers 	ADD_CONFIG_TERM(__type, __weak);			\
1218e884602bSLeo Yan 	__t->val.__name = __val;				\
1219e884602bSLeo Yan } while (0)
1220e884602bSLeo Yan 
122105e54e23SIan Rogers #define ADD_CONFIG_TERM_STR(__type, __val, __weak)		\
1222e884602bSLeo Yan do {								\
122305e54e23SIan Rogers 	ADD_CONFIG_TERM(__type, __weak);			\
12243220fb8dSLeo Yan 	__t->val.str = strdup(__val);				\
12253220fb8dSLeo Yan 	if (!__t->val.str) {					\
12263220fb8dSLeo Yan 		zfree(&__t);					\
12273220fb8dSLeo Yan 		return -ENOMEM;					\
12283220fb8dSLeo Yan 	}							\
12293220fb8dSLeo Yan 	__t->free_str = true;					\
1230930a2e29SJiri Olsa } while (0)
1231930a2e29SJiri Olsa 
1232930a2e29SJiri Olsa 	struct parse_events_term *term;
1233930a2e29SJiri Olsa 
1234930a2e29SJiri Olsa 	list_for_each_entry(term, head_config, list) {
1235930a2e29SJiri Olsa 		switch (term->type_term) {
1236ee4c7588SJiri Olsa 		case PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD:
123705e54e23SIan Rogers 			ADD_CONFIG_TERM_VAL(PERIOD, period, term->val.num, term->weak);
123832067712SKan Liang 			break;
123909af2a55SNamhyung Kim 		case PARSE_EVENTS__TERM_TYPE_SAMPLE_FREQ:
124005e54e23SIan Rogers 			ADD_CONFIG_TERM_VAL(FREQ, freq, term->val.num, term->weak);
124109af2a55SNamhyung Kim 			break;
124232067712SKan Liang 		case PARSE_EVENTS__TERM_TYPE_TIME:
124305e54e23SIan Rogers 			ADD_CONFIG_TERM_VAL(TIME, time, term->val.num, term->weak);
124432067712SKan Liang 			break;
1245d457c963SKan Liang 		case PARSE_EVENTS__TERM_TYPE_CALLGRAPH:
124605e54e23SIan Rogers 			ADD_CONFIG_TERM_STR(CALLGRAPH, term->val.str, term->weak);
1247d457c963SKan Liang 			break;
1248ac12f676SAndi Kleen 		case PARSE_EVENTS__TERM_TYPE_BRANCH_SAMPLE_TYPE:
124905e54e23SIan Rogers 			ADD_CONFIG_TERM_STR(BRANCH, term->val.str, term->weak);
1250ac12f676SAndi Kleen 			break;
1251d457c963SKan Liang 		case PARSE_EVENTS__TERM_TYPE_STACKSIZE:
1252e884602bSLeo Yan 			ADD_CONFIG_TERM_VAL(STACK_USER, stack_user,
125305e54e23SIan Rogers 					    term->val.num, term->weak);
1254d457c963SKan Liang 			break;
1255374ce938SWang Nan 		case PARSE_EVENTS__TERM_TYPE_INHERIT:
1256e884602bSLeo Yan 			ADD_CONFIG_TERM_VAL(INHERIT, inherit,
125705e54e23SIan Rogers 					    term->val.num ? 1 : 0, term->weak);
1258374ce938SWang Nan 			break;
1259374ce938SWang Nan 		case PARSE_EVENTS__TERM_TYPE_NOINHERIT:
1260e884602bSLeo Yan 			ADD_CONFIG_TERM_VAL(INHERIT, inherit,
126105e54e23SIan Rogers 					    term->val.num ? 0 : 1, term->weak);
1262374ce938SWang Nan 			break;
1263792d48b4SArnaldo Carvalho de Melo 		case PARSE_EVENTS__TERM_TYPE_MAX_STACK:
1264e884602bSLeo Yan 			ADD_CONFIG_TERM_VAL(MAX_STACK, max_stack,
126505e54e23SIan Rogers 					    term->val.num, term->weak);
1266792d48b4SArnaldo Carvalho de Melo 			break;
12672fda5adaSArnaldo Carvalho de Melo 		case PARSE_EVENTS__TERM_TYPE_MAX_EVENTS:
1268e884602bSLeo Yan 			ADD_CONFIG_TERM_VAL(MAX_EVENTS, max_events,
126905e54e23SIan Rogers 					    term->val.num, term->weak);
12702fda5adaSArnaldo Carvalho de Melo 			break;
1271626a6b78SWang Nan 		case PARSE_EVENTS__TERM_TYPE_OVERWRITE:
1272e884602bSLeo Yan 			ADD_CONFIG_TERM_VAL(OVERWRITE, overwrite,
127305e54e23SIan Rogers 					    term->val.num ? 1 : 0, term->weak);
1274626a6b78SWang Nan 			break;
1275626a6b78SWang Nan 		case PARSE_EVENTS__TERM_TYPE_NOOVERWRITE:
1276e884602bSLeo Yan 			ADD_CONFIG_TERM_VAL(OVERWRITE, overwrite,
127705e54e23SIan Rogers 					    term->val.num ? 0 : 1, term->weak);
1278626a6b78SWang Nan 			break;
1279dd60fba7SMathieu Poirier 		case PARSE_EVENTS__TERM_TYPE_DRV_CFG:
128005e54e23SIan Rogers 			ADD_CONFIG_TERM_STR(DRV_CFG, term->val.str, term->weak);
1281dd60fba7SMathieu Poirier 			break;
1282064b4e82SJin Yao 		case PARSE_EVENTS__TERM_TYPE_PERCORE:
1283e884602bSLeo Yan 			ADD_CONFIG_TERM_VAL(PERCORE, percore,
128405e54e23SIan Rogers 					    term->val.num ? true : false, term->weak);
1285064b4e82SJin Yao 			break;
12861b992154SAdrian Hunter 		case PARSE_EVENTS__TERM_TYPE_AUX_OUTPUT:
1287e884602bSLeo Yan 			ADD_CONFIG_TERM_VAL(AUX_OUTPUT, aux_output,
128805e54e23SIan Rogers 					    term->val.num ? 1 : 0, term->weak);
12891b992154SAdrian Hunter 			break;
1290eb7a52d4SAdrian Hunter 		case PARSE_EVENTS__TERM_TYPE_AUX_SAMPLE_SIZE:
1291e884602bSLeo Yan 			ADD_CONFIG_TERM_VAL(AUX_SAMPLE_SIZE, aux_sample_size,
129205e54e23SIan Rogers 					    term->val.num, term->weak);
1293eb7a52d4SAdrian Hunter 			break;
1294930a2e29SJiri Olsa 		default:
1295930a2e29SJiri Olsa 			break;
1296930a2e29SJiri Olsa 		}
1297930a2e29SJiri Olsa 	}
1298a1ac7de6SAdrian Hunter 	return 0;
1299a1ac7de6SAdrian Hunter }
1300a1ac7de6SAdrian Hunter 
1301a1ac7de6SAdrian Hunter /*
130235ac0cadSArnaldo Carvalho de Melo  * Add EVSEL__CONFIG_TERM_CFG_CHG where cfg_chg will have a bit set for
1303a1ac7de6SAdrian Hunter  * each bit of attr->config that the user has changed.
1304a1ac7de6SAdrian Hunter  */
1305a1ac7de6SAdrian Hunter static int get_config_chgs(struct perf_pmu *pmu, struct list_head *head_config,
1306a1ac7de6SAdrian Hunter 			   struct list_head *head_terms)
1307a1ac7de6SAdrian Hunter {
1308a1ac7de6SAdrian Hunter 	struct parse_events_term *term;
1309a1ac7de6SAdrian Hunter 	u64 bits = 0;
1310a1ac7de6SAdrian Hunter 	int type;
1311a1ac7de6SAdrian Hunter 
1312a1ac7de6SAdrian Hunter 	list_for_each_entry(term, head_config, list) {
1313a1ac7de6SAdrian Hunter 		switch (term->type_term) {
1314a1ac7de6SAdrian Hunter 		case PARSE_EVENTS__TERM_TYPE_USER:
1315a1ac7de6SAdrian Hunter 			type = perf_pmu__format_type(&pmu->format, term->config);
1316a1ac7de6SAdrian Hunter 			if (type != PERF_PMU_FORMAT_VALUE_CONFIG)
1317a1ac7de6SAdrian Hunter 				continue;
1318a1ac7de6SAdrian Hunter 			bits |= perf_pmu__format_bits(&pmu->format, term->config);
1319a1ac7de6SAdrian Hunter 			break;
1320a1ac7de6SAdrian Hunter 		case PARSE_EVENTS__TERM_TYPE_CONFIG:
1321a1ac7de6SAdrian Hunter 			bits = ~(u64)0;
1322a1ac7de6SAdrian Hunter 			break;
1323a1ac7de6SAdrian Hunter 		default:
1324a1ac7de6SAdrian Hunter 			break;
1325a1ac7de6SAdrian Hunter 		}
1326a1ac7de6SAdrian Hunter 	}
1327a1ac7de6SAdrian Hunter 
1328a1ac7de6SAdrian Hunter 	if (bits)
132905e54e23SIan Rogers 		ADD_CONFIG_TERM_VAL(CFG_CHG, cfg_chg, bits, false);
1330a1ac7de6SAdrian Hunter 
1331a1ac7de6SAdrian Hunter #undef ADD_CONFIG_TERM
1332930a2e29SJiri Olsa 	return 0;
1333930a2e29SJiri Olsa }
1334930a2e29SJiri Olsa 
1335e637d177SHe Kuang int parse_events_add_tracepoint(struct list_head *list, int *idx,
13368c619d6aSWang Nan 				const char *sys, const char *event,
1337272ed29aSJiri Olsa 				struct parse_events_error *err,
1338e637d177SHe Kuang 				struct list_head *head_config)
1339e637d177SHe Kuang {
1340378ef0f5SIan Rogers #ifdef HAVE_LIBTRACEEVENT
1341e637d177SHe Kuang 	if (head_config) {
1342e637d177SHe Kuang 		struct perf_event_attr attr;
1343e637d177SHe Kuang 
1344272ed29aSJiri Olsa 		if (config_attr(&attr, head_config, err,
1345e637d177SHe Kuang 				config_term_tracepoint))
1346e637d177SHe Kuang 			return -EINVAL;
1347e637d177SHe Kuang 	}
1348e637d177SHe Kuang 
1349e637d177SHe Kuang 	if (strpbrk(sys, "*?"))
1350e637d177SHe Kuang 		return add_tracepoint_multi_sys(list, idx, sys, event,
1351272ed29aSJiri Olsa 						err, head_config);
1352e637d177SHe Kuang 	else
1353e637d177SHe Kuang 		return add_tracepoint_event(list, idx, sys, event,
1354272ed29aSJiri Olsa 					    err, head_config);
1355378ef0f5SIan Rogers #else
1356378ef0f5SIan Rogers 	(void)list;
1357378ef0f5SIan Rogers 	(void)idx;
1358378ef0f5SIan Rogers 	(void)sys;
1359378ef0f5SIan Rogers 	(void)event;
1360378ef0f5SIan Rogers 	(void)head_config;
1361378ef0f5SIan Rogers 	parse_events_error__handle(err, 0, strdup("unsupported tracepoint"),
1362378ef0f5SIan Rogers 				strdup("libtraceevent is necessary for tracepoint support"));
1363378ef0f5SIan Rogers 	return -1;
1364378ef0f5SIan Rogers #endif
1365e637d177SHe Kuang }
1366e637d177SHe Kuang 
13675d9cdc11SArnaldo Carvalho de Melo int parse_events_add_numeric(struct parse_events_state *parse_state,
136887d650beSJiri Olsa 			     struct list_head *list,
1369b527bab5SRobert Richter 			     u32 type, u64 config,
13708f707d84SJiri Olsa 			     struct list_head *head_config)
137174d5b588SJaswinder Singh Rajput {
137289812fc8SJiri Olsa 	struct perf_event_attr attr;
1373930a2e29SJiri Olsa 	LIST_HEAD(config_terms);
13742b62b3a6SIan Rogers 	const char *name, *metric_id;
13759cbfa2f6SJin Yao 	bool hybrid;
13769cbfa2f6SJin Yao 	int ret;
137774d5b588SJaswinder Singh Rajput 
137889812fc8SJiri Olsa 	memset(&attr, 0, sizeof(attr));
137989812fc8SJiri Olsa 	attr.type = type;
138089812fc8SJiri Olsa 	attr.config = config;
13818f707d84SJiri Olsa 
1382930a2e29SJiri Olsa 	if (head_config) {
13835d9cdc11SArnaldo Carvalho de Melo 		if (config_attr(&attr, head_config, parse_state->error,
13840b8891a8SHe Kuang 				config_term_common))
13858f707d84SJiri Olsa 			return -EINVAL;
13868f707d84SJiri Olsa 
1387930a2e29SJiri Olsa 		if (get_config_terms(head_config, &config_terms))
1388930a2e29SJiri Olsa 			return -ENOMEM;
1389930a2e29SJiri Olsa 	}
1390930a2e29SJiri Olsa 
13912b62b3a6SIan Rogers 	name = get_config_name(head_config);
13922b62b3a6SIan Rogers 	metric_id = get_config_metric_id(head_config);
13939cbfa2f6SJin Yao 	ret = parse_events__add_numeric_hybrid(parse_state, list, &attr,
13942b62b3a6SIan Rogers 					       name, metric_id,
13959cbfa2f6SJin Yao 					       &config_terms, &hybrid);
13969cbfa2f6SJin Yao 	if (hybrid)
139799fc5941SAdrian Hunter 		goto out_free_terms;
13989cbfa2f6SJin Yao 
13992b62b3a6SIan Rogers 	ret = add_event(list, &parse_state->idx, &attr, name, metric_id,
14002b62b3a6SIan Rogers 			&config_terms);
140199fc5941SAdrian Hunter out_free_terms:
140299fc5941SAdrian Hunter 	free_config_terms(&config_terms);
140399fc5941SAdrian Hunter 	return ret;
1404b908debdSIngo Molnar }
1405b908debdSIngo Molnar 
1406f0fbb114SAndi Kleen int parse_events_add_tool(struct parse_events_state *parse_state,
1407f0fbb114SAndi Kleen 			  struct list_head *list,
14088228e936SIan Rogers 			  int tool_event)
1409f0fbb114SAndi Kleen {
1410f0fbb114SAndi Kleen 	return add_event_tool(list, &parse_state->idx, tool_event);
1411f0fbb114SAndi Kleen }
1412f0fbb114SAndi Kleen 
1413064b4e82SJin Yao static bool config_term_percore(struct list_head *config_terms)
1414064b4e82SJin Yao {
141535ac0cadSArnaldo Carvalho de Melo 	struct evsel_config_term *term;
1416064b4e82SJin Yao 
1417064b4e82SJin Yao 	list_for_each_entry(term, config_terms, list) {
141835ac0cadSArnaldo Carvalho de Melo 		if (term->type == EVSEL__CONFIG_TERM_PERCORE)
1419064b4e82SJin Yao 			return term->val.percore;
1420064b4e82SJin Yao 	}
1421064b4e82SJin Yao 
1422064b4e82SJin Yao 	return false;
1423064b4e82SJin Yao }
1424064b4e82SJin Yao 
14255e4edd1fSJin Yao static int parse_events__inside_hybrid_pmu(struct parse_events_state *parse_state,
14265e4edd1fSJin Yao 					   struct list_head *list, char *name,
14275e4edd1fSJin Yao 					   struct list_head *head_config)
14285e4edd1fSJin Yao {
14295e4edd1fSJin Yao 	struct parse_events_term *term;
14305e4edd1fSJin Yao 	int ret = -1;
14315e4edd1fSJin Yao 
14325e4edd1fSJin Yao 	if (parse_state->fake_pmu || !head_config || list_empty(head_config) ||
14335e4edd1fSJin Yao 	    !perf_pmu__is_hybrid(name)) {
14345e4edd1fSJin Yao 		return -1;
14355e4edd1fSJin Yao 	}
14365e4edd1fSJin Yao 
14375e4edd1fSJin Yao 	/*
14385e4edd1fSJin Yao 	 * More than one term in list.
14395e4edd1fSJin Yao 	 */
14405e4edd1fSJin Yao 	if (head_config->next && head_config->next->next != head_config)
14415e4edd1fSJin Yao 		return -1;
14425e4edd1fSJin Yao 
14435e4edd1fSJin Yao 	term = list_first_entry(head_config, struct parse_events_term, list);
14445e4edd1fSJin Yao 	if (term && term->config && strcmp(term->config, "event")) {
14455e4edd1fSJin Yao 		ret = parse_events__with_hybrid_pmu(parse_state, term->config,
14465e4edd1fSJin Yao 						    name, list);
14475e4edd1fSJin Yao 	}
14485e4edd1fSJin Yao 
14495e4edd1fSJin Yao 	return ret;
14505e4edd1fSJin Yao }
14515e4edd1fSJin Yao 
1452c199c11dSAgustin Vega-Frias int parse_events_add_pmu(struct parse_events_state *parse_state,
145336adec85SJiri Olsa 			 struct list_head *list, char *name,
14543cdc5c2cSKan Liang 			 struct list_head *head_config,
1455e733f87eSIan Rogers 			 bool auto_merge_stats)
14565f537a26SJiri Olsa {
14575f537a26SJiri Olsa 	struct perf_event_attr attr;
145846441bdcSMatt Fleming 	struct perf_pmu_info info;
14595f537a26SJiri Olsa 	struct perf_pmu *pmu;
146032dcd021SJiri Olsa 	struct evsel *evsel;
1461333b5665SAndi Kleen 	struct parse_events_error *err = parse_state->error;
1462930a2e29SJiri Olsa 	LIST_HEAD(config_terms);
14635f537a26SJiri Olsa 
1464f034fc50SAdrian Hunter 	pmu = parse_state->fake_pmu ?: perf_pmu__find(name);
1465f034fc50SAdrian Hunter 
1466f034fc50SAdrian Hunter 	if (verbose > 1 && !(pmu && pmu->selectable)) {
14674ac22b48SIan Rogers 		fprintf(stderr, "Attempting to add event pmu '%s' with '",
14684ac22b48SIan Rogers 			name);
14694ac22b48SIan Rogers 		if (head_config) {
14704ac22b48SIan Rogers 			struct parse_events_term *term;
14714ac22b48SIan Rogers 
14724ac22b48SIan Rogers 			list_for_each_entry(term, head_config, list) {
14734ac22b48SIan Rogers 				fprintf(stderr, "%s,", term->config);
14744ac22b48SIan Rogers 			}
14754ac22b48SIan Rogers 		}
14764ac22b48SIan Rogers 		fprintf(stderr, "' that may result in non-fatal errors\n");
14774ac22b48SIan Rogers 	}
14784ac22b48SIan Rogers 
1479333b5665SAndi Kleen 	if (!pmu) {
1480448d732cSIan Rogers 		char *err_str;
1481448d732cSIan Rogers 
1482448d732cSIan Rogers 		if (asprintf(&err_str,
1483333b5665SAndi Kleen 				"Cannot find PMU `%s'. Missing kernel support?",
1484448d732cSIan Rogers 				name) >= 0)
14856c191289SIan Rogers 			parse_events_error__handle(err, 0, err_str, NULL);
14865f537a26SJiri Olsa 		return -EINVAL;
1487333b5665SAndi Kleen 	}
14885f537a26SJiri Olsa 
1489dc0a6202SAdrian Hunter 	if (pmu->default_config) {
1490dc0a6202SAdrian Hunter 		memcpy(&attr, pmu->default_config,
1491dc0a6202SAdrian Hunter 		       sizeof(struct perf_event_attr));
1492dc0a6202SAdrian Hunter 	} else {
14935f537a26SJiri Olsa 		memset(&attr, 0, sizeof(attr));
1494dc0a6202SAdrian Hunter 	}
14955f537a26SJiri Olsa 
1496ad962273SAdrian Hunter 	if (!head_config) {
1497ad962273SAdrian Hunter 		attr.type = pmu->type;
14982b62b3a6SIan Rogers 		evsel = __add_event(list, &parse_state->idx, &attr,
14992b62b3a6SIan Rogers 				    /*init_attr=*/true, /*name=*/NULL,
15002b62b3a6SIan Rogers 				    /*metric_id=*/NULL, pmu,
15012b62b3a6SIan Rogers 				    /*config_terms=*/NULL, auto_merge_stats,
15022b62b3a6SIan Rogers 				    /*cpu_list=*/NULL);
15038c5421c0SAgustin Vega-Frias 		if (evsel) {
1504d4953f7eSIan Rogers 			evsel->pmu_name = name ? strdup(name) : NULL;
15058c5421c0SAgustin Vega-Frias 			return 0;
15068c5421c0SAgustin Vega-Frias 		} else {
15078c5421c0SAgustin Vega-Frias 			return -ENOMEM;
15088c5421c0SAgustin Vega-Frias 		}
1509ad962273SAdrian Hunter 	}
1510ad962273SAdrian Hunter 
1511387ad33fSJiri Olsa 	if (!parse_state->fake_pmu && perf_pmu__check_alias(pmu, head_config, &info))
1512a6146d50SZheng Yan 		return -EINVAL;
1513a6146d50SZheng Yan 
15144ac22b48SIan Rogers 	if (verbose > 1) {
15154ac22b48SIan Rogers 		fprintf(stderr, "After aliases, add event pmu '%s' with '",
15164ac22b48SIan Rogers 			name);
15174ac22b48SIan Rogers 		if (head_config) {
15184ac22b48SIan Rogers 			struct parse_events_term *term;
15194ac22b48SIan Rogers 
15204ac22b48SIan Rogers 			list_for_each_entry(term, head_config, list) {
15214ac22b48SIan Rogers 				fprintf(stderr, "%s,", term->config);
15224ac22b48SIan Rogers 			}
15234ac22b48SIan Rogers 		}
15244ac22b48SIan Rogers 		fprintf(stderr, "' that may result in non-fatal errors\n");
15254ac22b48SIan Rogers 	}
15264ac22b48SIan Rogers 
15275f537a26SJiri Olsa 	/*
15285f537a26SJiri Olsa 	 * Configure hardcoded terms first, no need to check
15295f537a26SJiri Olsa 	 * return value when called with fail == 0 ;)
15305f537a26SJiri Olsa 	 */
15315d9cdc11SArnaldo Carvalho de Melo 	if (config_attr(&attr, head_config, parse_state->error, config_term_pmu))
1532c056ba6aSJiri Olsa 		return -EINVAL;
15335f537a26SJiri Olsa 
1534930a2e29SJiri Olsa 	if (get_config_terms(head_config, &config_terms))
1535930a2e29SJiri Olsa 		return -ENOMEM;
1536930a2e29SJiri Olsa 
1537a1ac7de6SAdrian Hunter 	/*
1538a1ac7de6SAdrian Hunter 	 * When using default config, record which bits of attr->config were
1539a1ac7de6SAdrian Hunter 	 * changed by the user.
1540a1ac7de6SAdrian Hunter 	 */
1541a1ac7de6SAdrian Hunter 	if (pmu->default_config && get_config_chgs(pmu, head_config, &config_terms))
1542a1ac7de6SAdrian Hunter 		return -ENOMEM;
1543a1ac7de6SAdrian Hunter 
15445e4edd1fSJin Yao 	if (!parse_events__inside_hybrid_pmu(parse_state, list, name,
15455e4edd1fSJin Yao 					     head_config)) {
15465e4edd1fSJin Yao 		return 0;
15475e4edd1fSJin Yao 	}
15485e4edd1fSJin Yao 
1549387ad33fSJiri Olsa 	if (!parse_state->fake_pmu && perf_pmu__config(pmu, &attr, head_config, parse_state->error)) {
1550a7d212fcSAdrian Hunter 		free_config_terms(&config_terms);
15515f537a26SJiri Olsa 		return -EINVAL;
155238f2c422SIan Rogers 	}
15535f537a26SJiri Olsa 
15542b62b3a6SIan Rogers 	evsel = __add_event(list, &parse_state->idx, &attr, /*init_attr=*/true,
15552b62b3a6SIan Rogers 			    get_config_name(head_config),
15562b62b3a6SIan Rogers 			    get_config_metric_id(head_config), pmu,
15572b62b3a6SIan Rogers 			    &config_terms, auto_merge_stats, /*cpu_list=*/NULL);
155833321a06SIan Rogers 	if (!evsel)
155933321a06SIan Rogers 		return -ENOMEM;
156033321a06SIan Rogers 
156112279429SJin Yao 	if (evsel->name)
156212279429SJin Yao 		evsel->use_config_name = true;
156312279429SJin Yao 
156433321a06SIan Rogers 	evsel->pmu_name = name ? strdup(name) : NULL;
156533321a06SIan Rogers 	evsel->percore = config_term_percore(&evsel->config_terms);
156633321a06SIan Rogers 
156733321a06SIan Rogers 	if (parse_state->fake_pmu)
156833321a06SIan Rogers 		return 0;
156933321a06SIan Rogers 
1570b194c9cdSIan Rogers 	free((char *)evsel->unit);
1571b194c9cdSIan Rogers 	evsel->unit = strdup(info.unit);
157246441bdcSMatt Fleming 	evsel->scale = info.scale;
1573044330c1SMatt Fleming 	evsel->per_pkg = info.per_pkg;
15741d9e446bSJiri Olsa 	evsel->snapshot = info.snapshot;
157533321a06SIan Rogers 	return 0;
15765f537a26SJiri Olsa }
15775f537a26SJiri Olsa 
15785d9cdc11SArnaldo Carvalho de Melo int parse_events_multi_pmu_add(struct parse_events_state *parse_state,
1579fb081153SIan Rogers 			       char *str, struct list_head *head,
1580fb081153SIan Rogers 			       struct list_head **listp)
15812073ad33SAndi Kleen {
15822073ad33SAndi Kleen 	struct parse_events_term *term;
1583fb081153SIan Rogers 	struct list_head *list = NULL;
158491c9923aSZhengjun Xing 	struct list_head *orig_head = NULL;
15852073ad33SAndi Kleen 	struct perf_pmu *pmu = NULL;
15862073ad33SAndi Kleen 	int ok = 0;
1587fb081153SIan Rogers 	char *config;
15882073ad33SAndi Kleen 
15892073ad33SAndi Kleen 	*listp = NULL;
1590fb081153SIan Rogers 
1591fb081153SIan Rogers 	if (!head) {
1592fb081153SIan Rogers 		head = malloc(sizeof(struct list_head));
1593fb081153SIan Rogers 		if (!head)
1594fb081153SIan Rogers 			goto out_err;
1595fb081153SIan Rogers 
1596fb081153SIan Rogers 		INIT_LIST_HEAD(head);
1597fb081153SIan Rogers 	}
1598fb081153SIan Rogers 	config = strdup(str);
1599fb081153SIan Rogers 	if (!config)
1600fb081153SIan Rogers 		goto out_err;
1601fb081153SIan Rogers 
1602fb081153SIan Rogers 	if (parse_events_term__num(&term,
1603fb081153SIan Rogers 				   PARSE_EVENTS__TERM_TYPE_USER,
160466c9598bSNamhyung Kim 				   config, 1, false, NULL,
1605fb081153SIan Rogers 					NULL) < 0) {
1606fb081153SIan Rogers 		free(config);
1607fb081153SIan Rogers 		goto out_err;
1608fb081153SIan Rogers 	}
1609fb081153SIan Rogers 	list_add_tail(&term->list, head);
1610fb081153SIan Rogers 
16112073ad33SAndi Kleen 	/* Add it for all PMUs that support the alias */
16122073ad33SAndi Kleen 	list = malloc(sizeof(struct list_head));
16132073ad33SAndi Kleen 	if (!list)
1614fb081153SIan Rogers 		goto out_err;
1615fb081153SIan Rogers 
16162073ad33SAndi Kleen 	INIT_LIST_HEAD(list);
1617fb081153SIan Rogers 
16182073ad33SAndi Kleen 	while ((pmu = perf_pmu__scan(pmu)) != NULL) {
16192073ad33SAndi Kleen 		struct perf_pmu_alias *alias;
16202073ad33SAndi Kleen 
16212073ad33SAndi Kleen 		list_for_each_entry(alias, &pmu->aliases, list) {
16222073ad33SAndi Kleen 			if (!strcasecmp(alias->name, str)) {
162391c9923aSZhengjun Xing 				parse_events_copy_term_list(head, &orig_head);
1624c199c11dSAgustin Vega-Frias 				if (!parse_events_add_pmu(parse_state, list,
162591c9923aSZhengjun Xing 							  pmu->name, orig_head,
1626e733f87eSIan Rogers 							  /*auto_merge_stats=*/true)) {
1627d4953f7eSIan Rogers 					pr_debug("%s -> %s/%s/\n", str,
16282073ad33SAndi Kleen 						 pmu->name, alias->str);
16292073ad33SAndi Kleen 					ok++;
16302073ad33SAndi Kleen 				}
163191c9923aSZhengjun Xing 				parse_events_terms__delete(orig_head);
1632fb081153SIan Rogers 			}
1633fb081153SIan Rogers 		}
1634fb081153SIan Rogers 	}
1635b4a7276cSJohn Garry 
1636b4a7276cSJohn Garry 	if (parse_state->fake_pmu) {
1637b4a7276cSJohn Garry 		if (!parse_events_add_pmu(parse_state, list, str, head,
1638e733f87eSIan Rogers 					  /*auto_merge_stats=*/true)) {
1639b4a7276cSJohn Garry 			pr_debug("%s -> %s/%s/\n", str, "fake_pmu", str);
1640b4a7276cSJohn Garry 			ok++;
1641b4a7276cSJohn Garry 		}
1642b4a7276cSJohn Garry 	}
1643b4a7276cSJohn Garry 
1644fb081153SIan Rogers out_err:
1645fb081153SIan Rogers 	if (ok)
1646fb081153SIan Rogers 		*listp = list;
1647fb081153SIan Rogers 	else
1648fb081153SIan Rogers 		free(list);
16492073ad33SAndi Kleen 
16502073ad33SAndi Kleen 	parse_events_terms__delete(head);
1651fb081153SIan Rogers 	return ok ? 0 : -1;
16522073ad33SAndi Kleen }
16532073ad33SAndi Kleen 
16546a4bb04cSJiri Olsa int parse_events__modifier_group(struct list_head *list,
16556a4bb04cSJiri Olsa 				 char *event_mod)
165689efb029SJiri Olsa {
16576a4bb04cSJiri Olsa 	return parse_events__modifier_event(list, event_mod, true);
16586a4bb04cSJiri Olsa }
16596a4bb04cSJiri Olsa 
1660347c2f0aSIan Rogers void parse_events__set_leader(char *name, struct list_head *list)
16616a4bb04cSJiri Olsa {
166232dcd021SJiri Olsa 	struct evsel *leader;
16636a4bb04cSJiri Olsa 
1664854f7363SWang Nan 	if (list_empty(list)) {
1665854f7363SWang Nan 		WARN_ONCE(true, "WARNING: failed to set leader: empty list");
1666854f7363SWang Nan 		return;
1667854f7363SWang Nan 	}
1668854f7363SWang Nan 
1669347c2f0aSIan Rogers 	leader = list_first_entry(list, struct evsel, core.node);
1670ecdcf630SIan Rogers 	__perf_evlist__set_leader(list, &leader->core);
16714bb311b2SIan Rogers 	leader->group_name = name;
167289efb029SJiri Olsa }
167389efb029SJiri Olsa 
1674c5cd8ac0SDavid Ahern /* list_event is assumed to point to malloc'ed memory */
16755d7be90eSJiri Olsa void parse_events_update_lists(struct list_head *list_event,
16765d7be90eSJiri Olsa 			       struct list_head *list_all)
16775d7be90eSJiri Olsa {
16785d7be90eSJiri Olsa 	/*
16795d7be90eSJiri Olsa 	 * Called for single event definition. Update the
168089efb029SJiri Olsa 	 * 'all event' list, and reinit the 'single event'
16815d7be90eSJiri Olsa 	 * list, for next event definition.
16825d7be90eSJiri Olsa 	 */
16835d7be90eSJiri Olsa 	list_splice_tail(list_event, list_all);
1684b847cbdcSJiri Olsa 	free(list_event);
16855d7be90eSJiri Olsa }
16865d7be90eSJiri Olsa 
1687f5b1135bSJiri Olsa struct event_modifier {
1688f5b1135bSJiri Olsa 	int eu;
1689f5b1135bSJiri Olsa 	int ek;
1690f5b1135bSJiri Olsa 	int eh;
1691f5b1135bSJiri Olsa 	int eH;
1692f5b1135bSJiri Olsa 	int eG;
1693a1e12da4SJiri Olsa 	int eI;
1694f5b1135bSJiri Olsa 	int precise;
16957f94af7aSJiri Olsa 	int precise_max;
1696f5b1135bSJiri Olsa 	int exclude_GH;
16973c176311SJiri Olsa 	int sample_read;
1698e9a7c414SMichael Ellerman 	int pinned;
16995a5dfe4bSAndi Kleen 	int weak;
17000997a266SAndi Kleen 	int exclusive;
170101bd8efcSSong Liu 	int bpf_counter;
1702f5b1135bSJiri Olsa };
170361c45981SPaul Mackerras 
1704f5b1135bSJiri Olsa static int get_event_modifier(struct event_modifier *mod, char *str,
170532dcd021SJiri Olsa 			       struct evsel *evsel)
1706f5b1135bSJiri Olsa {
17071fc632ceSJiri Olsa 	int eu = evsel ? evsel->core.attr.exclude_user : 0;
17081fc632ceSJiri Olsa 	int ek = evsel ? evsel->core.attr.exclude_kernel : 0;
17091fc632ceSJiri Olsa 	int eh = evsel ? evsel->core.attr.exclude_hv : 0;
17101fc632ceSJiri Olsa 	int eH = evsel ? evsel->core.attr.exclude_host : 0;
17111fc632ceSJiri Olsa 	int eG = evsel ? evsel->core.attr.exclude_guest : 0;
17121fc632ceSJiri Olsa 	int eI = evsel ? evsel->core.attr.exclude_idle : 0;
17131fc632ceSJiri Olsa 	int precise = evsel ? evsel->core.attr.precise_ip : 0;
17147f94af7aSJiri Olsa 	int precise_max = 0;
17153c176311SJiri Olsa 	int sample_read = 0;
17161fc632ceSJiri Olsa 	int pinned = evsel ? evsel->core.attr.pinned : 0;
17170997a266SAndi Kleen 	int exclusive = evsel ? evsel->core.attr.exclusive : 0;
1718f5b1135bSJiri Olsa 
1719f5b1135bSJiri Olsa 	int exclude = eu | ek | eh;
1720f5b1135bSJiri Olsa 	int exclude_GH = evsel ? evsel->exclude_GH : 0;
17215a5dfe4bSAndi Kleen 	int weak = 0;
172201bd8efcSSong Liu 	int bpf_counter = 0;
1723f5b1135bSJiri Olsa 
1724f5b1135bSJiri Olsa 	memset(mod, 0, sizeof(*mod));
1725ceb53fbfSIngo Molnar 
172661c45981SPaul Mackerras 	while (*str) {
1727ab608344SPeter Zijlstra 		if (*str == 'u') {
1728ab608344SPeter Zijlstra 			if (!exclude)
1729ab608344SPeter Zijlstra 				exclude = eu = ek = eh = 1;
1730943b69acSJin Yao 			if (!exclude_GH && !perf_guest)
1731943b69acSJin Yao 				eG = 1;
173261c45981SPaul Mackerras 			eu = 0;
1733ab608344SPeter Zijlstra 		} else if (*str == 'k') {
1734ab608344SPeter Zijlstra 			if (!exclude)
1735ab608344SPeter Zijlstra 				exclude = eu = ek = eh = 1;
173661c45981SPaul Mackerras 			ek = 0;
1737ab608344SPeter Zijlstra 		} else if (*str == 'h') {
1738ab608344SPeter Zijlstra 			if (!exclude)
1739ab608344SPeter Zijlstra 				exclude = eu = ek = eh = 1;
174061c45981SPaul Mackerras 			eh = 0;
174199320cc8SJoerg Roedel 		} else if (*str == 'G') {
174299320cc8SJoerg Roedel 			if (!exclude_GH)
174399320cc8SJoerg Roedel 				exclude_GH = eG = eH = 1;
174499320cc8SJoerg Roedel 			eG = 0;
174599320cc8SJoerg Roedel 		} else if (*str == 'H') {
174699320cc8SJoerg Roedel 			if (!exclude_GH)
174799320cc8SJoerg Roedel 				exclude_GH = eG = eH = 1;
174899320cc8SJoerg Roedel 			eH = 0;
1749a1e12da4SJiri Olsa 		} else if (*str == 'I') {
1750a1e12da4SJiri Olsa 			eI = 1;
1751ab608344SPeter Zijlstra 		} else if (*str == 'p') {
1752ab608344SPeter Zijlstra 			precise++;
17531342798cSDavid Ahern 			/* use of precise requires exclude_guest */
17541342798cSDavid Ahern 			if (!exclude_GH)
17551342798cSDavid Ahern 				eG = 1;
17567f94af7aSJiri Olsa 		} else if (*str == 'P') {
17577f94af7aSJiri Olsa 			precise_max = 1;
17583c176311SJiri Olsa 		} else if (*str == 'S') {
17593c176311SJiri Olsa 			sample_read = 1;
1760e9a7c414SMichael Ellerman 		} else if (*str == 'D') {
1761e9a7c414SMichael Ellerman 			pinned = 1;
17620997a266SAndi Kleen 		} else if (*str == 'e') {
17630997a266SAndi Kleen 			exclusive = 1;
17645a5dfe4bSAndi Kleen 		} else if (*str == 'W') {
17655a5dfe4bSAndi Kleen 			weak = 1;
176601bd8efcSSong Liu 		} else if (*str == 'b') {
176701bd8efcSSong Liu 			bpf_counter = 1;
1768ab608344SPeter Zijlstra 		} else
176961c45981SPaul Mackerras 			break;
1770ab608344SPeter Zijlstra 
177161c45981SPaul Mackerras 		++str;
177261c45981SPaul Mackerras 	}
177374d5b588SJaswinder Singh Rajput 
177486470930SIngo Molnar 	/*
177589812fc8SJiri Olsa 	 * precise ip:
177689812fc8SJiri Olsa 	 *
177789812fc8SJiri Olsa 	 *  0 - SAMPLE_IP can have arbitrary skid
177889812fc8SJiri Olsa 	 *  1 - SAMPLE_IP must have constant skid
177989812fc8SJiri Olsa 	 *  2 - SAMPLE_IP requested to have 0 skid
178089812fc8SJiri Olsa 	 *  3 - SAMPLE_IP must have 0 skid
178189812fc8SJiri Olsa 	 *
178289812fc8SJiri Olsa 	 *  See also PERF_RECORD_MISC_EXACT_IP
178386470930SIngo Molnar 	 */
178489812fc8SJiri Olsa 	if (precise > 3)
178589812fc8SJiri Olsa 		return -EINVAL;
178686470930SIngo Molnar 
1787f5b1135bSJiri Olsa 	mod->eu = eu;
1788f5b1135bSJiri Olsa 	mod->ek = ek;
1789f5b1135bSJiri Olsa 	mod->eh = eh;
1790f5b1135bSJiri Olsa 	mod->eH = eH;
1791f5b1135bSJiri Olsa 	mod->eG = eG;
1792a1e12da4SJiri Olsa 	mod->eI = eI;
1793f5b1135bSJiri Olsa 	mod->precise = precise;
17947f94af7aSJiri Olsa 	mod->precise_max = precise_max;
1795f5b1135bSJiri Olsa 	mod->exclude_GH = exclude_GH;
17963c176311SJiri Olsa 	mod->sample_read = sample_read;
1797e9a7c414SMichael Ellerman 	mod->pinned = pinned;
17985a5dfe4bSAndi Kleen 	mod->weak = weak;
179901bd8efcSSong Liu 	mod->bpf_counter = bpf_counter;
18000997a266SAndi Kleen 	mod->exclusive = exclusive;
1801e9a7c414SMichael Ellerman 
1802f5b1135bSJiri Olsa 	return 0;
1803f5b1135bSJiri Olsa }
1804f5b1135bSJiri Olsa 
1805534123f4SJiri Olsa /*
1806534123f4SJiri Olsa  * Basic modifier sanity check to validate it contains only one
1807534123f4SJiri Olsa  * instance of any modifier (apart from 'p') present.
1808534123f4SJiri Olsa  */
1809534123f4SJiri Olsa static int check_modifier(char *str)
1810534123f4SJiri Olsa {
1811534123f4SJiri Olsa 	char *p = str;
1812534123f4SJiri Olsa 
1813534123f4SJiri Olsa 	/* The sizeof includes 0 byte as well. */
181401bd8efcSSong Liu 	if (strlen(str) > (sizeof("ukhGHpppPSDIWeb") - 1))
1815534123f4SJiri Olsa 		return -1;
1816534123f4SJiri Olsa 
1817534123f4SJiri Olsa 	while (*p) {
1818534123f4SJiri Olsa 		if (*p != 'p' && strchr(p + 1, *p))
1819534123f4SJiri Olsa 			return -1;
1820534123f4SJiri Olsa 		p++;
1821534123f4SJiri Olsa 	}
1822534123f4SJiri Olsa 
1823534123f4SJiri Olsa 	return 0;
1824534123f4SJiri Olsa }
1825534123f4SJiri Olsa 
1826f5b1135bSJiri Olsa int parse_events__modifier_event(struct list_head *list, char *str, bool add)
1827f5b1135bSJiri Olsa {
182832dcd021SJiri Olsa 	struct evsel *evsel;
1829f5b1135bSJiri Olsa 	struct event_modifier mod;
1830f5b1135bSJiri Olsa 
1831f5b1135bSJiri Olsa 	if (str == NULL)
1832f5b1135bSJiri Olsa 		return 0;
1833f5b1135bSJiri Olsa 
1834534123f4SJiri Olsa 	if (check_modifier(str))
1835534123f4SJiri Olsa 		return -EINVAL;
1836534123f4SJiri Olsa 
1837f5b1135bSJiri Olsa 	if (!add && get_event_modifier(&mod, str, NULL))
1838f5b1135bSJiri Olsa 		return -EINVAL;
1839f5b1135bSJiri Olsa 
1840e5cadb93SArnaldo Carvalho de Melo 	__evlist__for_each_entry(list, evsel) {
1841f5b1135bSJiri Olsa 		if (add && get_event_modifier(&mod, str, evsel))
1842f5b1135bSJiri Olsa 			return -EINVAL;
1843f5b1135bSJiri Olsa 
18441fc632ceSJiri Olsa 		evsel->core.attr.exclude_user   = mod.eu;
18451fc632ceSJiri Olsa 		evsel->core.attr.exclude_kernel = mod.ek;
18461fc632ceSJiri Olsa 		evsel->core.attr.exclude_hv     = mod.eh;
18471fc632ceSJiri Olsa 		evsel->core.attr.precise_ip     = mod.precise;
18481fc632ceSJiri Olsa 		evsel->core.attr.exclude_host   = mod.eH;
18491fc632ceSJiri Olsa 		evsel->core.attr.exclude_guest  = mod.eG;
18501fc632ceSJiri Olsa 		evsel->core.attr.exclude_idle   = mod.eI;
1851f5b1135bSJiri Olsa 		evsel->exclude_GH          = mod.exclude_GH;
18523c176311SJiri Olsa 		evsel->sample_read         = mod.sample_read;
18537f94af7aSJiri Olsa 		evsel->precise_max         = mod.precise_max;
18545a5dfe4bSAndi Kleen 		evsel->weak_group	   = mod.weak;
185501bd8efcSSong Liu 		evsel->bpf_counter	   = mod.bpf_counter;
1856e9a7c414SMichael Ellerman 
18570997a266SAndi Kleen 		if (evsel__is_group_leader(evsel)) {
18581fc632ceSJiri Olsa 			evsel->core.attr.pinned = mod.pinned;
18590997a266SAndi Kleen 			evsel->core.attr.exclusive = mod.exclusive;
18600997a266SAndi Kleen 		}
1861ceb53fbfSIngo Molnar 	}
186286470930SIngo Molnar 
186389812fc8SJiri Olsa 	return 0;
186486470930SIngo Molnar }
186586470930SIngo Molnar 
18668e8bbfb3SIan Rogers int parse_events_name(struct list_head *list, const char *name)
1867ac2ba9f3SRobert Richter {
186832dcd021SJiri Olsa 	struct evsel *evsel;
1869ac2ba9f3SRobert Richter 
1870e5cadb93SArnaldo Carvalho de Melo 	__evlist__for_each_entry(list, evsel) {
1871ac2ba9f3SRobert Richter 		if (!evsel->name)
1872ac2ba9f3SRobert Richter 			evsel->name = strdup(name);
1873ac2ba9f3SRobert Richter 	}
1874ac2ba9f3SRobert Richter 
1875ac2ba9f3SRobert Richter 	return 0;
1876ac2ba9f3SRobert Richter }
1877ac2ba9f3SRobert Richter 
1878dcb4e102SKan Liang static int
1879dcb4e102SKan Liang comp_pmu(const void *p1, const void *p2)
1880dcb4e102SKan Liang {
1881dcb4e102SKan Liang 	struct perf_pmu_event_symbol *pmu1 = (struct perf_pmu_event_symbol *) p1;
1882dcb4e102SKan Liang 	struct perf_pmu_event_symbol *pmu2 = (struct perf_pmu_event_symbol *) p2;
1883dcb4e102SKan Liang 
1884e312bcf1SAndi Kleen 	return strcasecmp(pmu1->symbol, pmu2->symbol);
1885dcb4e102SKan Liang }
1886dcb4e102SKan Liang 
1887dcb4e102SKan Liang static void perf_pmu__parse_cleanup(void)
1888dcb4e102SKan Liang {
1889dcb4e102SKan Liang 	if (perf_pmu_events_list_num > 0) {
1890dcb4e102SKan Liang 		struct perf_pmu_event_symbol *p;
1891dcb4e102SKan Liang 		int i;
1892dcb4e102SKan Liang 
1893dcb4e102SKan Liang 		for (i = 0; i < perf_pmu_events_list_num; i++) {
1894dcb4e102SKan Liang 			p = perf_pmu_events_list + i;
1895360e071bSTaeung Song 			zfree(&p->symbol);
1896dcb4e102SKan Liang 		}
1897506fde11STaeung Song 		zfree(&perf_pmu_events_list);
1898dcb4e102SKan Liang 		perf_pmu_events_list_num = 0;
1899dcb4e102SKan Liang 	}
1900dcb4e102SKan Liang }
1901dcb4e102SKan Liang 
1902dcb4e102SKan Liang #define SET_SYMBOL(str, stype)		\
1903dcb4e102SKan Liang do {					\
1904dcb4e102SKan Liang 	p->symbol = str;		\
1905dcb4e102SKan Liang 	if (!p->symbol)			\
1906dcb4e102SKan Liang 		goto err;		\
1907dcb4e102SKan Liang 	p->type = stype;		\
1908dcb4e102SKan Liang } while (0)
1909dcb4e102SKan Liang 
1910dcb4e102SKan Liang /*
1911dcb4e102SKan Liang  * Read the pmu events list from sysfs
1912dcb4e102SKan Liang  * Save it into perf_pmu_events_list
1913dcb4e102SKan Liang  */
1914dcb4e102SKan Liang static void perf_pmu__parse_init(void)
1915dcb4e102SKan Liang {
1916dcb4e102SKan Liang 
1917dcb4e102SKan Liang 	struct perf_pmu *pmu = NULL;
1918dcb4e102SKan Liang 	struct perf_pmu_alias *alias;
1919dcb4e102SKan Liang 	int len = 0;
1920dcb4e102SKan Liang 
1921231bb2aaSAndi Kleen 	pmu = NULL;
1922231bb2aaSAndi Kleen 	while ((pmu = perf_pmu__scan(pmu)) != NULL) {
1923dcb4e102SKan Liang 		list_for_each_entry(alias, &pmu->aliases, list) {
1924864bc8c9SJohn Garry 			char *tmp = strchr(alias->name, '-');
1925864bc8c9SJohn Garry 
1926864bc8c9SJohn Garry 			if (tmp) {
1927864bc8c9SJohn Garry 				char *tmp2 = NULL;
1928864bc8c9SJohn Garry 
1929864bc8c9SJohn Garry 				tmp2 = strchr(tmp + 1, '-');
1930dcb4e102SKan Liang 				len++;
1931864bc8c9SJohn Garry 				if (tmp2)
1932864bc8c9SJohn Garry 					len++;
1933864bc8c9SJohn Garry 			}
1934864bc8c9SJohn Garry 
1935dcb4e102SKan Liang 			len++;
1936dcb4e102SKan Liang 		}
1937231bb2aaSAndi Kleen 	}
1938231bb2aaSAndi Kleen 
1939231bb2aaSAndi Kleen 	if (len == 0) {
1940231bb2aaSAndi Kleen 		perf_pmu_events_list_num = -1;
1941231bb2aaSAndi Kleen 		return;
1942231bb2aaSAndi Kleen 	}
1943dcb4e102SKan Liang 	perf_pmu_events_list = malloc(sizeof(struct perf_pmu_event_symbol) * len);
1944dcb4e102SKan Liang 	if (!perf_pmu_events_list)
1945dcb4e102SKan Liang 		return;
1946dcb4e102SKan Liang 	perf_pmu_events_list_num = len;
1947dcb4e102SKan Liang 
1948dcb4e102SKan Liang 	len = 0;
1949231bb2aaSAndi Kleen 	pmu = NULL;
1950231bb2aaSAndi Kleen 	while ((pmu = perf_pmu__scan(pmu)) != NULL) {
1951dcb4e102SKan Liang 		list_for_each_entry(alias, &pmu->aliases, list) {
1952dcb4e102SKan Liang 			struct perf_pmu_event_symbol *p = perf_pmu_events_list + len;
1953dcb4e102SKan Liang 			char *tmp = strchr(alias->name, '-');
1954864bc8c9SJohn Garry 			char *tmp2 = NULL;
1955dcb4e102SKan Liang 
1956864bc8c9SJohn Garry 			if (tmp)
1957864bc8c9SJohn Garry 				tmp2 = strchr(tmp + 1, '-');
1958864bc8c9SJohn Garry 			if (tmp2) {
1959864bc8c9SJohn Garry 				SET_SYMBOL(strndup(alias->name, tmp - alias->name),
1960864bc8c9SJohn Garry 						PMU_EVENT_SYMBOL_PREFIX);
1961864bc8c9SJohn Garry 				p++;
1962864bc8c9SJohn Garry 				tmp++;
1963864bc8c9SJohn Garry 				SET_SYMBOL(strndup(tmp, tmp2 - tmp), PMU_EVENT_SYMBOL_SUFFIX);
1964864bc8c9SJohn Garry 				p++;
1965864bc8c9SJohn Garry 				SET_SYMBOL(strdup(++tmp2), PMU_EVENT_SYMBOL_SUFFIX2);
1966864bc8c9SJohn Garry 				len += 3;
1967864bc8c9SJohn Garry 			} else if (tmp) {
1968dcb4e102SKan Liang 				SET_SYMBOL(strndup(alias->name, tmp - alias->name),
1969dcb4e102SKan Liang 						PMU_EVENT_SYMBOL_PREFIX);
1970dcb4e102SKan Liang 				p++;
1971dcb4e102SKan Liang 				SET_SYMBOL(strdup(++tmp), PMU_EVENT_SYMBOL_SUFFIX);
1972dcb4e102SKan Liang 				len += 2;
1973dcb4e102SKan Liang 			} else {
1974dcb4e102SKan Liang 				SET_SYMBOL(strdup(alias->name), PMU_EVENT_SYMBOL);
1975dcb4e102SKan Liang 				len++;
1976dcb4e102SKan Liang 			}
1977dcb4e102SKan Liang 		}
1978231bb2aaSAndi Kleen 	}
1979dcb4e102SKan Liang 	qsort(perf_pmu_events_list, len,
1980dcb4e102SKan Liang 		sizeof(struct perf_pmu_event_symbol), comp_pmu);
1981dcb4e102SKan Liang 
1982dcb4e102SKan Liang 	return;
1983dcb4e102SKan Liang err:
1984dcb4e102SKan Liang 	perf_pmu__parse_cleanup();
1985dcb4e102SKan Liang }
1986dcb4e102SKan Liang 
19874929e95aSJiri Olsa /*
19884929e95aSJiri Olsa  * This function injects special term in
19894929e95aSJiri Olsa  * perf_pmu_events_list so the test code
19904929e95aSJiri Olsa  * can check on this functionality.
19914929e95aSJiri Olsa  */
19924929e95aSJiri Olsa int perf_pmu__test_parse_init(void)
19934929e95aSJiri Olsa {
1994b4a7276cSJohn Garry 	struct perf_pmu_event_symbol *list, *tmp, symbols[] = {
1995b4a7276cSJohn Garry 		{(char *)"read", PMU_EVENT_SYMBOL},
1996b4a7276cSJohn Garry 		{(char *)"event", PMU_EVENT_SYMBOL_PREFIX},
1997b4a7276cSJohn Garry 		{(char *)"two", PMU_EVENT_SYMBOL_SUFFIX},
1998b4a7276cSJohn Garry 		{(char *)"hyphen", PMU_EVENT_SYMBOL_SUFFIX},
1999b4a7276cSJohn Garry 		{(char *)"hyph", PMU_EVENT_SYMBOL_SUFFIX2},
2000b4a7276cSJohn Garry 	};
2001b4a7276cSJohn Garry 	unsigned long i, j;
20024929e95aSJiri Olsa 
2003b4a7276cSJohn Garry 	tmp = list = malloc(sizeof(*list) * ARRAY_SIZE(symbols));
20044929e95aSJiri Olsa 	if (!list)
20054929e95aSJiri Olsa 		return -ENOMEM;
20064929e95aSJiri Olsa 
2007b4a7276cSJohn Garry 	for (i = 0; i < ARRAY_SIZE(symbols); i++, tmp++) {
2008b4a7276cSJohn Garry 		tmp->type = symbols[i].type;
2009b4a7276cSJohn Garry 		tmp->symbol = strdup(symbols[i].symbol);
2010a7a72631SWeiguo Li 		if (!tmp->symbol)
2011b4a7276cSJohn Garry 			goto err_free;
20124929e95aSJiri Olsa 	}
20134929e95aSJiri Olsa 
20144929e95aSJiri Olsa 	perf_pmu_events_list = list;
2015b4a7276cSJohn Garry 	perf_pmu_events_list_num = ARRAY_SIZE(symbols);
2016b4a7276cSJohn Garry 
2017b4a7276cSJohn Garry 	qsort(perf_pmu_events_list, ARRAY_SIZE(symbols),
2018b4a7276cSJohn Garry 	      sizeof(struct perf_pmu_event_symbol), comp_pmu);
20194929e95aSJiri Olsa 	return 0;
2020b4a7276cSJohn Garry 
2021b4a7276cSJohn Garry err_free:
2022b4a7276cSJohn Garry 	for (j = 0, tmp = list; j < i; j++, tmp++)
202325feb605SArnaldo Carvalho de Melo 		zfree(&tmp->symbol);
2024b4a7276cSJohn Garry 	free(list);
2025b4a7276cSJohn Garry 	return -ENOMEM;
20264929e95aSJiri Olsa }
20274929e95aSJiri Olsa 
2028dcb4e102SKan Liang enum perf_pmu_event_symbol_type
2029dcb4e102SKan Liang perf_pmu__parse_check(const char *name)
2030dcb4e102SKan Liang {
2031dcb4e102SKan Liang 	struct perf_pmu_event_symbol p, *r;
2032dcb4e102SKan Liang 
2033dcb4e102SKan Liang 	/* scan kernel pmu events from sysfs if needed */
2034dcb4e102SKan Liang 	if (perf_pmu_events_list_num == 0)
2035dcb4e102SKan Liang 		perf_pmu__parse_init();
2036dcb4e102SKan Liang 	/*
2037dcb4e102SKan Liang 	 * name "cpu" could be prefix of cpu-cycles or cpu// events.
2038dcb4e102SKan Liang 	 * cpu-cycles has been handled by hardcode.
2039dcb4e102SKan Liang 	 * So it must be cpu// events, not kernel pmu event.
2040dcb4e102SKan Liang 	 */
2041dcb4e102SKan Liang 	if ((perf_pmu_events_list_num <= 0) || !strcmp(name, "cpu"))
2042dcb4e102SKan Liang 		return PMU_EVENT_SYMBOL_ERR;
2043dcb4e102SKan Liang 
2044dcb4e102SKan Liang 	p.symbol = strdup(name);
2045dcb4e102SKan Liang 	r = bsearch(&p, perf_pmu_events_list,
2046dcb4e102SKan Liang 			(size_t) perf_pmu_events_list_num,
2047dcb4e102SKan Liang 			sizeof(struct perf_pmu_event_symbol), comp_pmu);
2048360e071bSTaeung Song 	zfree(&p.symbol);
2049dcb4e102SKan Liang 	return r ? r->type : PMU_EVENT_SYMBOL_ERR;
2050dcb4e102SKan Liang }
2051dcb4e102SKan Liang 
20521244a327SJiri Olsa static int parse_events__scanner(const char *str,
20531244a327SJiri Olsa 				 struct parse_events_state *parse_state)
2054ac20de6fSZheng Yan {
2055ac20de6fSZheng Yan 	YY_BUFFER_STATE buffer;
2056ac20de6fSZheng Yan 	void *scanner;
2057ac20de6fSZheng Yan 	int ret;
2058ac20de6fSZheng Yan 
20591244a327SJiri Olsa 	ret = parse_events_lex_init_extra(parse_state, &scanner);
2060ac20de6fSZheng Yan 	if (ret)
2061ac20de6fSZheng Yan 		return ret;
2062ac20de6fSZheng Yan 
2063ac20de6fSZheng Yan 	buffer = parse_events__scan_string(str, scanner);
2064ac20de6fSZheng Yan 
2065ac20de6fSZheng Yan #ifdef PARSER_DEBUG
2066ac20de6fSZheng Yan 	parse_events_debug = 1;
2067ae762641SJiri Olsa 	parse_events_set_debug(1, scanner);
2068ac20de6fSZheng Yan #endif
20695d9cdc11SArnaldo Carvalho de Melo 	ret = parse_events_parse(parse_state, scanner);
2070ac20de6fSZheng Yan 
2071ac20de6fSZheng Yan 	parse_events__flush_buffer(buffer, scanner);
2072ac20de6fSZheng Yan 	parse_events__delete_buffer(buffer, scanner);
2073ac20de6fSZheng Yan 	parse_events_lex_destroy(scanner);
2074ac20de6fSZheng Yan 	return ret;
2075ac20de6fSZheng Yan }
2076ac20de6fSZheng Yan 
207790e2b22dSJiri Olsa /*
207890e2b22dSJiri Olsa  * parse event config string, return a list of event terms.
207990e2b22dSJiri Olsa  */
208090e2b22dSJiri Olsa int parse_events_terms(struct list_head *terms, const char *str)
208190e2b22dSJiri Olsa {
20825d9cdc11SArnaldo Carvalho de Melo 	struct parse_events_state parse_state = {
208390e2b22dSJiri Olsa 		.terms  = NULL,
20841244a327SJiri Olsa 		.stoken = PE_START_TERMS,
208590e2b22dSJiri Olsa 	};
208690e2b22dSJiri Olsa 	int ret;
208790e2b22dSJiri Olsa 
20881244a327SJiri Olsa 	ret = parse_events__scanner(str, &parse_state);
20894929e95aSJiri Olsa 	perf_pmu__parse_cleanup();
20904929e95aSJiri Olsa 
209190e2b22dSJiri Olsa 	if (!ret) {
20925d9cdc11SArnaldo Carvalho de Melo 		list_splice(parse_state.terms, terms);
20935d9cdc11SArnaldo Carvalho de Melo 		zfree(&parse_state.terms);
209490e2b22dSJiri Olsa 		return 0;
209590e2b22dSJiri Olsa 	}
209690e2b22dSJiri Olsa 
20975d9cdc11SArnaldo Carvalho de Melo 	parse_events_terms__delete(parse_state.terms);
209890e2b22dSJiri Olsa 	return ret;
209990e2b22dSJiri Olsa }
210090e2b22dSJiri Olsa 
21015e4edd1fSJin Yao static int parse_events__with_hybrid_pmu(struct parse_events_state *parse_state,
21025e4edd1fSJin Yao 					 const char *str, char *pmu_name,
21035e4edd1fSJin Yao 					 struct list_head *list)
21045e4edd1fSJin Yao {
21055e4edd1fSJin Yao 	struct parse_events_state ps = {
21065e4edd1fSJin Yao 		.list            = LIST_HEAD_INIT(ps.list),
21075e4edd1fSJin Yao 		.stoken          = PE_START_EVENTS,
21085e4edd1fSJin Yao 		.hybrid_pmu_name = pmu_name,
21095e4edd1fSJin Yao 		.idx             = parse_state->idx,
21105e4edd1fSJin Yao 	};
21115e4edd1fSJin Yao 	int ret;
21125e4edd1fSJin Yao 
21135e4edd1fSJin Yao 	ret = parse_events__scanner(str, &ps);
21145e4edd1fSJin Yao 	perf_pmu__parse_cleanup();
21155e4edd1fSJin Yao 
21165e4edd1fSJin Yao 	if (!ret) {
21175e4edd1fSJin Yao 		if (!list_empty(&ps.list)) {
21185e4edd1fSJin Yao 			list_splice(&ps.list, list);
21195e4edd1fSJin Yao 			parse_state->idx = ps.idx;
21205e4edd1fSJin Yao 			return 0;
21215e4edd1fSJin Yao 		} else
21225e4edd1fSJin Yao 			return -1;
21235e4edd1fSJin Yao 	}
21245e4edd1fSJin Yao 
21255e4edd1fSJin Yao 	return ret;
21265e4edd1fSJin Yao }
21275e4edd1fSJin Yao 
2128347c2f0aSIan Rogers __weak int arch_evlist__cmp(const struct evsel *lhs, const struct evsel *rhs)
2129347c2f0aSIan Rogers {
2130347c2f0aSIan Rogers 	/* Order by insertion index. */
2131347c2f0aSIan Rogers 	return lhs->core.idx - rhs->core.idx;
2132347c2f0aSIan Rogers }
2133347c2f0aSIan Rogers 
2134347c2f0aSIan Rogers static int evlist__cmp(void *state, const struct list_head *l, const struct list_head *r)
2135347c2f0aSIan Rogers {
2136347c2f0aSIan Rogers 	const struct perf_evsel *lhs_core = container_of(l, struct perf_evsel, node);
2137347c2f0aSIan Rogers 	const struct evsel *lhs = container_of(lhs_core, struct evsel, core);
2138347c2f0aSIan Rogers 	const struct perf_evsel *rhs_core = container_of(r, struct perf_evsel, node);
2139347c2f0aSIan Rogers 	const struct evsel *rhs = container_of(rhs_core, struct evsel, core);
2140347c2f0aSIan Rogers 	int *leader_idx = state;
2141347c2f0aSIan Rogers 	int lhs_leader_idx = *leader_idx, rhs_leader_idx = *leader_idx, ret;
2142347c2f0aSIan Rogers 	const char *lhs_pmu_name, *rhs_pmu_name;
2143*327daf34SIan Rogers 	bool lhs_has_group = false, rhs_has_group = false;
2144347c2f0aSIan Rogers 
2145347c2f0aSIan Rogers 	/*
2146347c2f0aSIan Rogers 	 * First sort by grouping/leader. Read the leader idx only if the evsel
2147347c2f0aSIan Rogers 	 * is part of a group, as -1 indicates no group.
2148347c2f0aSIan Rogers 	 */
2149*327daf34SIan Rogers 	if (lhs_core->leader != lhs_core || lhs_core->nr_members > 1) {
2150*327daf34SIan Rogers 		lhs_has_group = true;
2151347c2f0aSIan Rogers 		lhs_leader_idx = lhs_core->leader->idx;
2152*327daf34SIan Rogers 	}
2153*327daf34SIan Rogers 	if (rhs_core->leader != rhs_core || rhs_core->nr_members > 1) {
2154*327daf34SIan Rogers 		rhs_has_group = true;
2155347c2f0aSIan Rogers 		rhs_leader_idx = rhs_core->leader->idx;
2156*327daf34SIan Rogers 	}
2157347c2f0aSIan Rogers 
2158347c2f0aSIan Rogers 	if (lhs_leader_idx != rhs_leader_idx)
2159347c2f0aSIan Rogers 		return lhs_leader_idx - rhs_leader_idx;
2160347c2f0aSIan Rogers 
2161*327daf34SIan Rogers 	/* Group by PMU if there is a group. Groups can't span PMUs. */
2162*327daf34SIan Rogers 	if (lhs_has_group && rhs_has_group) {
2163347c2f0aSIan Rogers 		lhs_pmu_name = evsel__group_pmu_name(lhs);
2164347c2f0aSIan Rogers 		rhs_pmu_name = evsel__group_pmu_name(rhs);
2165347c2f0aSIan Rogers 		ret = strcmp(lhs_pmu_name, rhs_pmu_name);
2166347c2f0aSIan Rogers 		if (ret)
2167347c2f0aSIan Rogers 			return ret;
2168*327daf34SIan Rogers 	}
2169347c2f0aSIan Rogers 
2170347c2f0aSIan Rogers 	/* Architecture specific sorting. */
2171347c2f0aSIan Rogers 	return arch_evlist__cmp(lhs, rhs);
2172347c2f0aSIan Rogers }
2173347c2f0aSIan Rogers 
2174a4c7d7c5SIan Rogers static bool parse_events__sort_events_and_fix_groups(struct list_head *list)
2175347c2f0aSIan Rogers {
2176a4c7d7c5SIan Rogers 	int idx = 0, unsorted_idx = -1;
2177347c2f0aSIan Rogers 	struct evsel *pos, *cur_leader = NULL;
2178347c2f0aSIan Rogers 	struct perf_evsel *cur_leaders_grp = NULL;
2179a4c7d7c5SIan Rogers 	bool idx_changed = false;
2180a4c7d7c5SIan Rogers 	int orig_num_leaders = 0, num_leaders = 0;
2181347c2f0aSIan Rogers 
2182347c2f0aSIan Rogers 	/*
2183347c2f0aSIan Rogers 	 * Compute index to insert ungrouped events at. Place them where the
2184347c2f0aSIan Rogers 	 * first ungrouped event appears.
2185347c2f0aSIan Rogers 	 */
2186347c2f0aSIan Rogers 	list_for_each_entry(pos, list, core.node) {
2187347c2f0aSIan Rogers 		const struct evsel *pos_leader = evsel__leader(pos);
2188347c2f0aSIan Rogers 
2189a4c7d7c5SIan Rogers 		if (pos == pos_leader)
2190a4c7d7c5SIan Rogers 			orig_num_leaders++;
2191347c2f0aSIan Rogers 
2192a4c7d7c5SIan Rogers 		/*
2193a4c7d7c5SIan Rogers 		 * Ensure indexes are sequential, in particular for multiple
2194a4c7d7c5SIan Rogers 		 * event lists being merged. The indexes are used to detect when
2195a4c7d7c5SIan Rogers 		 * the user order is modified.
2196a4c7d7c5SIan Rogers 		 */
2197a4c7d7c5SIan Rogers 		pos->core.idx = idx++;
2198a4c7d7c5SIan Rogers 
2199a4c7d7c5SIan Rogers 		if (unsorted_idx == -1 && pos == pos_leader && pos->core.nr_members < 2)
2200a4c7d7c5SIan Rogers 			unsorted_idx = pos->core.idx;
2201347c2f0aSIan Rogers 	}
2202347c2f0aSIan Rogers 
2203347c2f0aSIan Rogers 	/* Sort events. */
2204a4c7d7c5SIan Rogers 	list_sort(&unsorted_idx, list, evlist__cmp);
2205347c2f0aSIan Rogers 
2206347c2f0aSIan Rogers 	/*
2207347c2f0aSIan Rogers 	 * Recompute groups, splitting for PMUs and adding groups for events
2208347c2f0aSIan Rogers 	 * that require them.
2209347c2f0aSIan Rogers 	 */
2210347c2f0aSIan Rogers 	idx = 0;
2211347c2f0aSIan Rogers 	list_for_each_entry(pos, list, core.node) {
2212347c2f0aSIan Rogers 		const struct evsel *pos_leader = evsel__leader(pos);
2213347c2f0aSIan Rogers 		const char *pos_pmu_name = evsel__group_pmu_name(pos);
2214347c2f0aSIan Rogers 		const char *cur_leader_pmu_name, *pos_leader_pmu_name;
2215347c2f0aSIan Rogers 		bool force_grouped = arch_evsel__must_be_in_group(pos);
2216347c2f0aSIan Rogers 
2217347c2f0aSIan Rogers 		/* Reset index and nr_members. */
2218a4c7d7c5SIan Rogers 		if (pos->core.idx != idx)
2219a4c7d7c5SIan Rogers 			idx_changed = true;
2220347c2f0aSIan Rogers 		pos->core.idx = idx++;
2221347c2f0aSIan Rogers 		pos->core.nr_members = 0;
2222347c2f0aSIan Rogers 
2223347c2f0aSIan Rogers 		/*
2224347c2f0aSIan Rogers 		 * Set the group leader respecting the given groupings and that
2225347c2f0aSIan Rogers 		 * groups can't span PMUs.
2226347c2f0aSIan Rogers 		 */
2227347c2f0aSIan Rogers 		if (!cur_leader)
2228347c2f0aSIan Rogers 			cur_leader = pos;
2229347c2f0aSIan Rogers 
2230347c2f0aSIan Rogers 		cur_leader_pmu_name = evsel__group_pmu_name(cur_leader);
2231347c2f0aSIan Rogers 		if ((cur_leaders_grp != pos->core.leader && !force_grouped) ||
2232347c2f0aSIan Rogers 		    strcmp(cur_leader_pmu_name, pos_pmu_name)) {
2233347c2f0aSIan Rogers 			/* Event is for a different group/PMU than last. */
2234347c2f0aSIan Rogers 			cur_leader = pos;
2235347c2f0aSIan Rogers 			/*
2236347c2f0aSIan Rogers 			 * Remember the leader's group before it is overwritten,
2237347c2f0aSIan Rogers 			 * so that later events match as being in the same
2238347c2f0aSIan Rogers 			 * group.
2239347c2f0aSIan Rogers 			 */
2240347c2f0aSIan Rogers 			cur_leaders_grp = pos->core.leader;
2241347c2f0aSIan Rogers 		}
2242347c2f0aSIan Rogers 		pos_leader_pmu_name = evsel__group_pmu_name(pos_leader);
2243347c2f0aSIan Rogers 		if (strcmp(pos_leader_pmu_name, pos_pmu_name) || force_grouped) {
2244347c2f0aSIan Rogers 			/*
2245347c2f0aSIan Rogers 			 * Event's PMU differs from its leader's. Groups can't
2246347c2f0aSIan Rogers 			 * span PMUs, so update leader from the group/PMU
2247347c2f0aSIan Rogers 			 * tracker.
2248347c2f0aSIan Rogers 			 */
2249347c2f0aSIan Rogers 			evsel__set_leader(pos, cur_leader);
2250347c2f0aSIan Rogers 		}
2251347c2f0aSIan Rogers 	}
2252347c2f0aSIan Rogers 	list_for_each_entry(pos, list, core.node) {
2253a4c7d7c5SIan Rogers 		struct evsel *pos_leader = evsel__leader(pos);
2254a4c7d7c5SIan Rogers 
2255a4c7d7c5SIan Rogers 		if (pos == pos_leader)
2256a4c7d7c5SIan Rogers 			num_leaders++;
2257a4c7d7c5SIan Rogers 		pos_leader->core.nr_members++;
2258347c2f0aSIan Rogers 	}
2259a4c7d7c5SIan Rogers 	return idx_changed || num_leaders != orig_num_leaders;
2260347c2f0aSIan Rogers }
2261347c2f0aSIan Rogers 
22623bf91aa5SArnaldo Carvalho de Melo int __parse_events(struct evlist *evlist, const char *str,
2263a4c7d7c5SIan Rogers 		   struct parse_events_error *err, struct perf_pmu *fake_pmu,
2264a4c7d7c5SIan Rogers 		   bool warn_if_reordered)
226586470930SIngo Molnar {
22665d9cdc11SArnaldo Carvalho de Melo 	struct parse_events_state parse_state = {
22675d9cdc11SArnaldo Carvalho de Melo 		.list	  = LIST_HEAD_INIT(parse_state.list),
22686484d2f9SJiri Olsa 		.idx	  = evlist->core.nr_entries,
2269b39b8393SJiri Olsa 		.error	  = err,
22707630b3e2SWang Nan 		.evlist	  = evlist,
22711244a327SJiri Olsa 		.stoken	  = PE_START_EVENTS,
22723bf91aa5SArnaldo Carvalho de Melo 		.fake_pmu = fake_pmu,
227346010ab2SJiri Olsa 	};
227446010ab2SJiri Olsa 	int ret;
227586470930SIngo Molnar 
22761244a327SJiri Olsa 	ret = parse_events__scanner(str, &parse_state);
2277dcb4e102SKan Liang 	perf_pmu__parse_cleanup();
227815bfd2ccSWang Nan 
22798e8714c3SIan Rogers 	if (!ret && list_empty(&parse_state.list)) {
2280c23080a6SArnaldo Carvalho de Melo 		WARN_ONCE(true, "WARNING: event parser found nothing\n");
2281854f7363SWang Nan 		return -1;
2282854f7363SWang Nan 	}
2283854f7363SWang Nan 
2284a4c7d7c5SIan Rogers 	if (parse_events__sort_events_and_fix_groups(&parse_state.list) &&
2285a4c7d7c5SIan Rogers 	    warn_if_reordered && !parse_state.wild_card_pmus)
2286a4c7d7c5SIan Rogers 		pr_warning("WARNING: events were regrouped to match PMUs\n");
2287347c2f0aSIan Rogers 
22888e8714c3SIan Rogers 	/*
22898e8714c3SIan Rogers 	 * Add list to the evlist even with errors to allow callers to clean up.
22908e8714c3SIan Rogers 	 */
2291e414fd1aSArnaldo Carvalho de Melo 	evlist__splice_list_tail(evlist, &parse_state.list);
22928e8714c3SIan Rogers 
22938e8714c3SIan Rogers 	if (!ret) {
22948e8714c3SIan Rogers 		struct evsel *last;
22958e8714c3SIan Rogers 
2296515dbe48SJiri Olsa 		last = evlist__last(evlist);
229715bfd2ccSWang Nan 		last->cmdline_group_boundary = true;
229815bfd2ccSWang Nan 
229986470930SIngo Molnar 		return 0;
230086470930SIngo Molnar 	}
230186470930SIngo Molnar 
23025d7be90eSJiri Olsa 	/*
23035d7be90eSJiri Olsa 	 * There are 2 users - builtin-record and builtin-test objects.
2304c12995a5SJiri Olsa 	 * Both call evlist__delete in case of error, so we dont
23055d7be90eSJiri Olsa 	 * need to bother.
23065d7be90eSJiri Olsa 	 */
230789812fc8SJiri Olsa 	return ret;
230889812fc8SJiri Olsa }
230989812fc8SJiri Olsa 
2310806731a9SAdrian Hunter int parse_event(struct evlist *evlist, const char *str)
2311806731a9SAdrian Hunter {
2312806731a9SAdrian Hunter 	struct parse_events_error err;
2313806731a9SAdrian Hunter 	int ret;
2314806731a9SAdrian Hunter 
2315806731a9SAdrian Hunter 	parse_events_error__init(&err);
2316806731a9SAdrian Hunter 	ret = parse_events(evlist, str, &err);
2317806731a9SAdrian Hunter 	parse_events_error__exit(&err);
2318806731a9SAdrian Hunter 	return ret;
2319806731a9SAdrian Hunter }
2320806731a9SAdrian Hunter 
232107eafd4eSIan Rogers void parse_events_error__init(struct parse_events_error *err)
232207eafd4eSIan Rogers {
232307eafd4eSIan Rogers 	bzero(err, sizeof(*err));
232407eafd4eSIan Rogers }
232507eafd4eSIan Rogers 
232607eafd4eSIan Rogers void parse_events_error__exit(struct parse_events_error *err)
232707eafd4eSIan Rogers {
232807eafd4eSIan Rogers 	zfree(&err->str);
232907eafd4eSIan Rogers 	zfree(&err->help);
233007eafd4eSIan Rogers 	zfree(&err->first_str);
233107eafd4eSIan Rogers 	zfree(&err->first_help);
233207eafd4eSIan Rogers }
233307eafd4eSIan Rogers 
23346c191289SIan Rogers void parse_events_error__handle(struct parse_events_error *err, int idx,
23356c191289SIan Rogers 				char *str, char *help)
23366c191289SIan Rogers {
2337806731a9SAdrian Hunter 	if (WARN(!str || !err, "WARNING: failed to provide error string or struct\n"))
23382e828582SAdrian Hunter 		goto out_free;
23396c191289SIan Rogers 	switch (err->num_errors) {
23406c191289SIan Rogers 	case 0:
23416c191289SIan Rogers 		err->idx = idx;
23426c191289SIan Rogers 		err->str = str;
23436c191289SIan Rogers 		err->help = help;
23446c191289SIan Rogers 		break;
23456c191289SIan Rogers 	case 1:
23466c191289SIan Rogers 		err->first_idx = err->idx;
23476c191289SIan Rogers 		err->idx = idx;
23486c191289SIan Rogers 		err->first_str = err->str;
23496c191289SIan Rogers 		err->str = str;
23506c191289SIan Rogers 		err->first_help = err->help;
23516c191289SIan Rogers 		err->help = help;
23526c191289SIan Rogers 		break;
23536c191289SIan Rogers 	default:
23546c191289SIan Rogers 		pr_debug("Multiple errors dropping message: %s (%s)\n",
23556c191289SIan Rogers 			err->str, err->help);
23566c191289SIan Rogers 		free(err->str);
23576c191289SIan Rogers 		err->str = str;
23586c191289SIan Rogers 		free(err->help);
23596c191289SIan Rogers 		err->help = help;
23606c191289SIan Rogers 		break;
23616c191289SIan Rogers 	}
23626c191289SIan Rogers 	err->num_errors++;
23632e828582SAdrian Hunter 	return;
23642e828582SAdrian Hunter 
23652e828582SAdrian Hunter out_free:
23662e828582SAdrian Hunter 	free(str);
23672e828582SAdrian Hunter 	free(help);
23686c191289SIan Rogers }
23696c191289SIan Rogers 
2370b39b8393SJiri Olsa #define MAX_WIDTH 1000
2371b39b8393SJiri Olsa static int get_term_width(void)
2372b39b8393SJiri Olsa {
2373b39b8393SJiri Olsa 	struct winsize ws;
2374b39b8393SJiri Olsa 
2375b39b8393SJiri Olsa 	get_term_dimensions(&ws);
2376b39b8393SJiri Olsa 	return ws.ws_col > MAX_WIDTH ? MAX_WIDTH : ws.ws_col;
2377b39b8393SJiri Olsa }
2378b39b8393SJiri Olsa 
23796c191289SIan Rogers static void __parse_events_error__print(int err_idx, const char *err_str,
2380a910e466SIan Rogers 					const char *err_help, const char *event)
2381b39b8393SJiri Olsa {
2382b39b8393SJiri Olsa 	const char *str = "invalid or unsupported event: ";
2383b39b8393SJiri Olsa 	char _buf[MAX_WIDTH];
2384b39b8393SJiri Olsa 	char *buf = (char *) event;
2385b39b8393SJiri Olsa 	int idx = 0;
2386a910e466SIan Rogers 	if (err_str) {
2387b39b8393SJiri Olsa 		/* -2 for extra '' in the final fprintf */
2388b39b8393SJiri Olsa 		int width       = get_term_width() - 2;
2389b39b8393SJiri Olsa 		int len_event   = strlen(event);
2390b39b8393SJiri Olsa 		int len_str, max_len, cut = 0;
2391b39b8393SJiri Olsa 
2392b39b8393SJiri Olsa 		/*
2393b39b8393SJiri Olsa 		 * Maximum error index indent, we will cut
2394b39b8393SJiri Olsa 		 * the event string if it's bigger.
2395b39b8393SJiri Olsa 		 */
2396141b2d31SAdrian Hunter 		int max_err_idx = 13;
2397b39b8393SJiri Olsa 
2398b39b8393SJiri Olsa 		/*
2399b39b8393SJiri Olsa 		 * Let's be specific with the message when
2400b39b8393SJiri Olsa 		 * we have the precise error.
2401b39b8393SJiri Olsa 		 */
2402b39b8393SJiri Olsa 		str     = "event syntax error: ";
2403b39b8393SJiri Olsa 		len_str = strlen(str);
2404b39b8393SJiri Olsa 		max_len = width - len_str;
2405b39b8393SJiri Olsa 
2406b39b8393SJiri Olsa 		buf = _buf;
2407b39b8393SJiri Olsa 
2408bd1a0be5SAdam Buchbinder 		/* We're cutting from the beginning. */
2409a910e466SIan Rogers 		if (err_idx > max_err_idx)
2410a910e466SIan Rogers 			cut = err_idx - max_err_idx;
2411b39b8393SJiri Olsa 
2412b39b8393SJiri Olsa 		strncpy(buf, event + cut, max_len);
2413b39b8393SJiri Olsa 
2414b39b8393SJiri Olsa 		/* Mark cut parts with '..' on both sides. */
2415b39b8393SJiri Olsa 		if (cut)
2416b39b8393SJiri Olsa 			buf[0] = buf[1] = '.';
2417b39b8393SJiri Olsa 
2418b39b8393SJiri Olsa 		if ((len_event - cut) > max_len) {
2419b39b8393SJiri Olsa 			buf[max_len - 1] = buf[max_len - 2] = '.';
2420b39b8393SJiri Olsa 			buf[max_len] = 0;
2421b39b8393SJiri Olsa 		}
2422b39b8393SJiri Olsa 
2423a910e466SIan Rogers 		idx = len_str + err_idx - cut;
2424b39b8393SJiri Olsa 	}
2425b39b8393SJiri Olsa 
2426b39b8393SJiri Olsa 	fprintf(stderr, "%s'%s'\n", str, buf);
2427b39b8393SJiri Olsa 	if (idx) {
2428a910e466SIan Rogers 		fprintf(stderr, "%*s\\___ %s\n", idx + 1, "", err_str);
2429a910e466SIan Rogers 		if (err_help)
2430a910e466SIan Rogers 			fprintf(stderr, "\n%s\n", err_help);
2431a910e466SIan Rogers 	}
2432a910e466SIan Rogers }
2433a910e466SIan Rogers 
24346c191289SIan Rogers void parse_events_error__print(struct parse_events_error *err,
2435a910e466SIan Rogers 			       const char *event)
2436a910e466SIan Rogers {
2437a910e466SIan Rogers 	if (!err->num_errors)
2438a910e466SIan Rogers 		return;
2439a910e466SIan Rogers 
24406c191289SIan Rogers 	__parse_events_error__print(err->idx, err->str, err->help, event);
2441a910e466SIan Rogers 
2442a910e466SIan Rogers 	if (err->num_errors > 1) {
2443a910e466SIan Rogers 		fputs("\nInitial error:\n", stderr);
24446c191289SIan Rogers 		__parse_events_error__print(err->first_idx, err->first_str,
2445a910e466SIan Rogers 					err->first_help, event);
2446b39b8393SJiri Olsa 	}
2447b39b8393SJiri Olsa }
2448b39b8393SJiri Olsa 
2449b39b8393SJiri Olsa #undef MAX_WIDTH
2450b39b8393SJiri Olsa 
2451f120f9d5SJiri Olsa int parse_events_option(const struct option *opt, const char *str,
24521d037ca1SIrina Tirdea 			int unset __maybe_unused)
2453f120f9d5SJiri Olsa {
245463503dbaSJiri Olsa 	struct evlist *evlist = *(struct evlist **)opt->value;
2455a910e466SIan Rogers 	struct parse_events_error err;
2456a910e466SIan Rogers 	int ret;
2457a910e466SIan Rogers 
245807eafd4eSIan Rogers 	parse_events_error__init(&err);
2459a910e466SIan Rogers 	ret = parse_events(evlist, str, &err);
24609175ce1fSAndi Kleen 
2461333b5665SAndi Kleen 	if (ret) {
24626c191289SIan Rogers 		parse_events_error__print(&err, str);
2463333b5665SAndi Kleen 		fprintf(stderr, "Run 'perf list' for a list of valid events\n");
2464333b5665SAndi Kleen 	}
246507eafd4eSIan Rogers 	parse_events_error__exit(&err);
2466b39b8393SJiri Olsa 
24679175ce1fSAndi Kleen 	return ret;
2468f120f9d5SJiri Olsa }
2469f120f9d5SJiri Olsa 
2470d0abbc3cSArnaldo Carvalho de Melo int parse_events_option_new_evlist(const struct option *opt, const char *str, int unset)
2471d0abbc3cSArnaldo Carvalho de Melo {
2472d0abbc3cSArnaldo Carvalho de Melo 	struct evlist **evlistp = opt->value;
2473d0abbc3cSArnaldo Carvalho de Melo 	int ret;
2474d0abbc3cSArnaldo Carvalho de Melo 
2475d0abbc3cSArnaldo Carvalho de Melo 	if (*evlistp == NULL) {
2476d0abbc3cSArnaldo Carvalho de Melo 		*evlistp = evlist__new();
2477d0abbc3cSArnaldo Carvalho de Melo 
2478d0abbc3cSArnaldo Carvalho de Melo 		if (*evlistp == NULL) {
2479d0abbc3cSArnaldo Carvalho de Melo 			fprintf(stderr, "Not enough memory to create evlist\n");
2480d0abbc3cSArnaldo Carvalho de Melo 			return -1;
2481d0abbc3cSArnaldo Carvalho de Melo 		}
2482d0abbc3cSArnaldo Carvalho de Melo 	}
2483d0abbc3cSArnaldo Carvalho de Melo 
2484d0abbc3cSArnaldo Carvalho de Melo 	ret = parse_events_option(opt, str, unset);
2485d0abbc3cSArnaldo Carvalho de Melo 	if (ret) {
2486d0abbc3cSArnaldo Carvalho de Melo 		evlist__delete(*evlistp);
2487d0abbc3cSArnaldo Carvalho de Melo 		*evlistp = NULL;
2488d0abbc3cSArnaldo Carvalho de Melo 	}
2489d0abbc3cSArnaldo Carvalho de Melo 
2490d0abbc3cSArnaldo Carvalho de Melo 	return ret;
2491d0abbc3cSArnaldo Carvalho de Melo }
2492d0abbc3cSArnaldo Carvalho de Melo 
24934ba1faa1SWang Nan static int
249463503dbaSJiri Olsa foreach_evsel_in_last_glob(struct evlist *evlist,
249532dcd021SJiri Olsa 			   int (*func)(struct evsel *evsel,
24964ba1faa1SWang Nan 				       const void *arg),
24974ba1faa1SWang Nan 			   const void *arg)
2498c171b552SLi Zefan {
249932dcd021SJiri Olsa 	struct evsel *last = NULL;
25004ba1faa1SWang Nan 	int err;
2501c171b552SLi Zefan 
2502854f7363SWang Nan 	/*
2503854f7363SWang Nan 	 * Don't return when list_empty, give func a chance to report
2504854f7363SWang Nan 	 * error when it found last == NULL.
2505854f7363SWang Nan 	 *
2506854f7363SWang Nan 	 * So no need to WARN here, let *func do this.
2507854f7363SWang Nan 	 */
25086484d2f9SJiri Olsa 	if (evlist->core.nr_entries > 0)
2509515dbe48SJiri Olsa 		last = evlist__last(evlist);
251069aad6f1SArnaldo Carvalho de Melo 
251115bfd2ccSWang Nan 	do {
25124ba1faa1SWang Nan 		err = (*func)(last, arg);
25134ba1faa1SWang Nan 		if (err)
2514c171b552SLi Zefan 			return -1;
25154ba1faa1SWang Nan 		if (!last)
25164ba1faa1SWang Nan 			return 0;
2517c171b552SLi Zefan 
2518ce9036a6SJiri Olsa 		if (last->core.node.prev == &evlist->core.entries)
251915bfd2ccSWang Nan 			return 0;
2520b27c4eceSJiri Olsa 		last = list_entry(last->core.node.prev, struct evsel, core.node);
252115bfd2ccSWang Nan 	} while (!last->cmdline_group_boundary);
252215bfd2ccSWang Nan 
2523c171b552SLi Zefan 	return 0;
2524c171b552SLi Zefan }
2525c171b552SLi Zefan 
252632dcd021SJiri Olsa static int set_filter(struct evsel *evsel, const void *arg)
25274ba1faa1SWang Nan {
25284ba1faa1SWang Nan 	const char *str = arg;
25291e857484SMathieu Poirier 	bool found = false;
25301e857484SMathieu Poirier 	int nr_addr_filters = 0;
25311e857484SMathieu Poirier 	struct perf_pmu *pmu = NULL;
25324ba1faa1SWang Nan 
253349836f78SJack Henschel 	if (evsel == NULL) {
253449836f78SJack Henschel 		fprintf(stderr,
253549836f78SJack Henschel 			"--filter option should follow a -e tracepoint or HW tracer option\n");
253649836f78SJack Henschel 		return -1;
253749836f78SJack Henschel 	}
25384ba1faa1SWang Nan 
25391fc632ceSJiri Olsa 	if (evsel->core.attr.type == PERF_TYPE_TRACEPOINT) {
2540ad681adfSArnaldo Carvalho de Melo 		if (evsel__append_tp_filter(evsel, str) < 0) {
25414ba1faa1SWang Nan 			fprintf(stderr,
25424ba1faa1SWang Nan 				"not enough memory to hold filter string\n");
25434ba1faa1SWang Nan 			return -1;
25444ba1faa1SWang Nan 		}
25454ba1faa1SWang Nan 
25464ba1faa1SWang Nan 		return 0;
25474ba1faa1SWang Nan 	}
25484ba1faa1SWang Nan 
25491e857484SMathieu Poirier 	while ((pmu = perf_pmu__scan(pmu)) != NULL)
25501fc632ceSJiri Olsa 		if (pmu->type == evsel->core.attr.type) {
25511e857484SMathieu Poirier 			found = true;
25521e857484SMathieu Poirier 			break;
25531e857484SMathieu Poirier 		}
25541e857484SMathieu Poirier 
25551e857484SMathieu Poirier 	if (found)
25561e857484SMathieu Poirier 		perf_pmu__scan_file(pmu, "nr_addr_filters",
25571e857484SMathieu Poirier 				    "%d", &nr_addr_filters);
25581e857484SMathieu Poirier 
2559d180aa56SNamhyung Kim 	if (!nr_addr_filters)
2560d180aa56SNamhyung Kim 		return perf_bpf_filter__parse(&evsel->bpf_filters, str);
25611e857484SMathieu Poirier 
2562ad681adfSArnaldo Carvalho de Melo 	if (evsel__append_addr_filter(evsel, str) < 0) {
25631e857484SMathieu Poirier 		fprintf(stderr,
25641e857484SMathieu Poirier 			"not enough memory to hold filter string\n");
25651e857484SMathieu Poirier 		return -1;
25661e857484SMathieu Poirier 	}
25671e857484SMathieu Poirier 
25681e857484SMathieu Poirier 	return 0;
25691e857484SMathieu Poirier }
25701e857484SMathieu Poirier 
25714ba1faa1SWang Nan int parse_filter(const struct option *opt, const char *str,
25724ba1faa1SWang Nan 		 int unset __maybe_unused)
25734ba1faa1SWang Nan {
257463503dbaSJiri Olsa 	struct evlist *evlist = *(struct evlist **)opt->value;
25754ba1faa1SWang Nan 
25764ba1faa1SWang Nan 	return foreach_evsel_in_last_glob(evlist, set_filter,
25774ba1faa1SWang Nan 					  (const void *)str);
25784ba1faa1SWang Nan }
25794ba1faa1SWang Nan 
258032dcd021SJiri Olsa static int add_exclude_perf_filter(struct evsel *evsel,
25814ba1faa1SWang Nan 				   const void *arg __maybe_unused)
25824ba1faa1SWang Nan {
25834ba1faa1SWang Nan 	char new_filter[64];
25844ba1faa1SWang Nan 
25851fc632ceSJiri Olsa 	if (evsel == NULL || evsel->core.attr.type != PERF_TYPE_TRACEPOINT) {
25864ba1faa1SWang Nan 		fprintf(stderr,
25874ba1faa1SWang Nan 			"--exclude-perf option should follow a -e tracepoint option\n");
25884ba1faa1SWang Nan 		return -1;
25894ba1faa1SWang Nan 	}
25904ba1faa1SWang Nan 
25914ba1faa1SWang Nan 	snprintf(new_filter, sizeof(new_filter), "common_pid != %d", getpid());
25924ba1faa1SWang Nan 
2593ad681adfSArnaldo Carvalho de Melo 	if (evsel__append_tp_filter(evsel, new_filter) < 0) {
25944ba1faa1SWang Nan 		fprintf(stderr,
25954ba1faa1SWang Nan 			"not enough memory to hold filter string\n");
25964ba1faa1SWang Nan 		return -1;
25974ba1faa1SWang Nan 	}
25984ba1faa1SWang Nan 
25994ba1faa1SWang Nan 	return 0;
26004ba1faa1SWang Nan }
26014ba1faa1SWang Nan 
26024ba1faa1SWang Nan int exclude_perf(const struct option *opt,
26034ba1faa1SWang Nan 		 const char *arg __maybe_unused,
26044ba1faa1SWang Nan 		 int unset __maybe_unused)
26054ba1faa1SWang Nan {
260663503dbaSJiri Olsa 	struct evlist *evlist = *(struct evlist **)opt->value;
26074ba1faa1SWang Nan 
26084ba1faa1SWang Nan 	return foreach_evsel_in_last_glob(evlist, add_exclude_perf_filter,
26094ba1faa1SWang Nan 					  NULL);
26104ba1faa1SWang Nan }
26114ba1faa1SWang Nan 
26126cee6cd3SArnaldo Carvalho de Melo int parse_events__is_hardcoded_term(struct parse_events_term *term)
26138f707d84SJiri Olsa {
261416fa7e82SJiri Olsa 	return term->type_term != PARSE_EVENTS__TERM_TYPE_USER;
26158f707d84SJiri Olsa }
26168f707d84SJiri Olsa 
261767b49b38SJiri Olsa static int new_term(struct parse_events_term **_term,
261867b49b38SJiri Olsa 		    struct parse_events_term *temp,
261967b49b38SJiri Olsa 		    char *str, u64 num)
26208f707d84SJiri Olsa {
26216cee6cd3SArnaldo Carvalho de Melo 	struct parse_events_term *term;
26228f707d84SJiri Olsa 
262367b49b38SJiri Olsa 	term = malloc(sizeof(*term));
26248f707d84SJiri Olsa 	if (!term)
26258f707d84SJiri Olsa 		return -ENOMEM;
26268f707d84SJiri Olsa 
262767b49b38SJiri Olsa 	*term = *temp;
26288f707d84SJiri Olsa 	INIT_LIST_HEAD(&term->list);
2629c2f1ceadSAndi Kleen 	term->weak = false;
26308f707d84SJiri Olsa 
263167b49b38SJiri Olsa 	switch (term->type_val) {
26328f707d84SJiri Olsa 	case PARSE_EVENTS__TERM_TYPE_NUM:
26338f707d84SJiri Olsa 		term->val.num = num;
26348f707d84SJiri Olsa 		break;
26358f707d84SJiri Olsa 	case PARSE_EVENTS__TERM_TYPE_STR:
26368f707d84SJiri Olsa 		term->val.str = str;
26378f707d84SJiri Olsa 		break;
26388f707d84SJiri Olsa 	default:
26394be8be6bSAdrian Hunter 		free(term);
26408f707d84SJiri Olsa 		return -EINVAL;
26418f707d84SJiri Olsa 	}
26428f707d84SJiri Olsa 
26438f707d84SJiri Olsa 	*_term = term;
26448f707d84SJiri Olsa 	return 0;
26458f707d84SJiri Olsa }
26468f707d84SJiri Olsa 
26476cee6cd3SArnaldo Carvalho de Melo int parse_events_term__num(struct parse_events_term **term,
2648cecf3a2eSJiri Olsa 			   int type_term, char *config, u64 num,
264999e7138eSJiri Olsa 			   bool no_value,
2650bb78ce7dSAdrian Hunter 			   void *loc_term_, void *loc_val_)
265116fa7e82SJiri Olsa {
2652bb78ce7dSAdrian Hunter 	YYLTYPE *loc_term = loc_term_;
2653bb78ce7dSAdrian Hunter 	YYLTYPE *loc_val = loc_val_;
2654bb78ce7dSAdrian Hunter 
265567b49b38SJiri Olsa 	struct parse_events_term temp = {
265667b49b38SJiri Olsa 		.type_val  = PARSE_EVENTS__TERM_TYPE_NUM,
265767b49b38SJiri Olsa 		.type_term = type_term,
26584f9d4f8aSJohn Garry 		.config    = config ? : strdup(config_term_names[type_term]),
265999e7138eSJiri Olsa 		.no_value  = no_value,
266067b49b38SJiri Olsa 		.err_term  = loc_term ? loc_term->first_column : 0,
266167b49b38SJiri Olsa 		.err_val   = loc_val  ? loc_val->first_column  : 0,
266267b49b38SJiri Olsa 	};
266367b49b38SJiri Olsa 
266467b49b38SJiri Olsa 	return new_term(term, &temp, NULL, num);
266516fa7e82SJiri Olsa }
266616fa7e82SJiri Olsa 
26676cee6cd3SArnaldo Carvalho de Melo int parse_events_term__str(struct parse_events_term **term,
2668cecf3a2eSJiri Olsa 			   int type_term, char *config, char *str,
2669bb78ce7dSAdrian Hunter 			   void *loc_term_, void *loc_val_)
267016fa7e82SJiri Olsa {
2671bb78ce7dSAdrian Hunter 	YYLTYPE *loc_term = loc_term_;
2672bb78ce7dSAdrian Hunter 	YYLTYPE *loc_val = loc_val_;
2673bb78ce7dSAdrian Hunter 
267467b49b38SJiri Olsa 	struct parse_events_term temp = {
267567b49b38SJiri Olsa 		.type_val  = PARSE_EVENTS__TERM_TYPE_STR,
267667b49b38SJiri Olsa 		.type_term = type_term,
267767b49b38SJiri Olsa 		.config    = config,
267867b49b38SJiri Olsa 		.err_term  = loc_term ? loc_term->first_column : 0,
267967b49b38SJiri Olsa 		.err_val   = loc_val  ? loc_val->first_column  : 0,
268067b49b38SJiri Olsa 	};
268167b49b38SJiri Olsa 
268267b49b38SJiri Olsa 	return new_term(term, &temp, str, 0);
268316fa7e82SJiri Olsa }
268416fa7e82SJiri Olsa 
26856cee6cd3SArnaldo Carvalho de Melo int parse_events_term__sym_hw(struct parse_events_term **term,
26861d33d6dcSJiri Olsa 			      char *config, unsigned idx)
26871d33d6dcSJiri Olsa {
26881d33d6dcSJiri Olsa 	struct event_symbol *sym;
2689b6645a72SIan Rogers 	char *str;
269067b49b38SJiri Olsa 	struct parse_events_term temp = {
269167b49b38SJiri Olsa 		.type_val  = PARSE_EVENTS__TERM_TYPE_STR,
269267b49b38SJiri Olsa 		.type_term = PARSE_EVENTS__TERM_TYPE_USER,
2693b6645a72SIan Rogers 		.config    = config,
269467b49b38SJiri Olsa 	};
26951d33d6dcSJiri Olsa 
2696b6645a72SIan Rogers 	if (!temp.config) {
2697b6645a72SIan Rogers 		temp.config = strdup("event");
2698b6645a72SIan Rogers 		if (!temp.config)
2699b6645a72SIan Rogers 			return -ENOMEM;
2700b6645a72SIan Rogers 	}
27011d33d6dcSJiri Olsa 	BUG_ON(idx >= PERF_COUNT_HW_MAX);
27021d33d6dcSJiri Olsa 	sym = &event_symbols_hw[idx];
27031d33d6dcSJiri Olsa 
2704b6645a72SIan Rogers 	str = strdup(sym->symbol);
2705b6645a72SIan Rogers 	if (!str)
2706b6645a72SIan Rogers 		return -ENOMEM;
2707b6645a72SIan Rogers 	return new_term(term, &temp, str, 0);
27081d33d6dcSJiri Olsa }
27091d33d6dcSJiri Olsa 
27106cee6cd3SArnaldo Carvalho de Melo int parse_events_term__clone(struct parse_events_term **new,
27116cee6cd3SArnaldo Carvalho de Melo 			     struct parse_events_term *term)
2712a6146d50SZheng Yan {
2713b6645a72SIan Rogers 	char *str;
271467b49b38SJiri Olsa 	struct parse_events_term temp = {
271567b49b38SJiri Olsa 		.type_val  = term->type_val,
271667b49b38SJiri Olsa 		.type_term = term->type_term,
2717b6645a72SIan Rogers 		.config    = NULL,
271867b49b38SJiri Olsa 		.err_term  = term->err_term,
271967b49b38SJiri Olsa 		.err_val   = term->err_val,
272067b49b38SJiri Olsa 	};
272167b49b38SJiri Olsa 
2722b6645a72SIan Rogers 	if (term->config) {
2723b6645a72SIan Rogers 		temp.config = strdup(term->config);
2724b6645a72SIan Rogers 		if (!temp.config)
2725b6645a72SIan Rogers 			return -ENOMEM;
2726b6645a72SIan Rogers 	}
2727b6645a72SIan Rogers 	if (term->type_val == PARSE_EVENTS__TERM_TYPE_NUM)
2728b6645a72SIan Rogers 		return new_term(new, &temp, NULL, term->val.num);
2729b6645a72SIan Rogers 
2730b6645a72SIan Rogers 	str = strdup(term->val.str);
2731b6645a72SIan Rogers 	if (!str)
2732b6645a72SIan Rogers 		return -ENOMEM;
2733b6645a72SIan Rogers 	return new_term(new, &temp, str, 0);
2734a6146d50SZheng Yan }
2735a6146d50SZheng Yan 
27361dc92556SIan Rogers void parse_events_term__delete(struct parse_events_term *term)
27371dc92556SIan Rogers {
27381dc92556SIan Rogers 	if (term->array.nr_ranges)
27391dc92556SIan Rogers 		zfree(&term->array.ranges);
27401dc92556SIan Rogers 
27411dc92556SIan Rogers 	if (term->type_val != PARSE_EVENTS__TERM_TYPE_NUM)
27421dc92556SIan Rogers 		zfree(&term->val.str);
27431dc92556SIan Rogers 
27441dc92556SIan Rogers 	zfree(&term->config);
27451dc92556SIan Rogers 	free(term);
27461dc92556SIan Rogers }
27471dc92556SIan Rogers 
27488255718fSAndi Kleen int parse_events_copy_term_list(struct list_head *old,
27498255718fSAndi Kleen 				 struct list_head **new)
27508255718fSAndi Kleen {
27518255718fSAndi Kleen 	struct parse_events_term *term, *n;
27528255718fSAndi Kleen 	int ret;
27538255718fSAndi Kleen 
27548255718fSAndi Kleen 	if (!old) {
27558255718fSAndi Kleen 		*new = NULL;
27568255718fSAndi Kleen 		return 0;
27578255718fSAndi Kleen 	}
27588255718fSAndi Kleen 
27598255718fSAndi Kleen 	*new = malloc(sizeof(struct list_head));
27608255718fSAndi Kleen 	if (!*new)
27618255718fSAndi Kleen 		return -ENOMEM;
27628255718fSAndi Kleen 	INIT_LIST_HEAD(*new);
27638255718fSAndi Kleen 
27648255718fSAndi Kleen 	list_for_each_entry (term, old, list) {
27658255718fSAndi Kleen 		ret = parse_events_term__clone(&n, term);
27668255718fSAndi Kleen 		if (ret)
27678255718fSAndi Kleen 			return ret;
27688255718fSAndi Kleen 		list_add_tail(&n->list, *new);
27698255718fSAndi Kleen 	}
27708255718fSAndi Kleen 	return 0;
27718255718fSAndi Kleen }
27728255718fSAndi Kleen 
2773fc0a2c1dSArnaldo Carvalho de Melo void parse_events_terms__purge(struct list_head *terms)
27748f707d84SJiri Olsa {
27756cee6cd3SArnaldo Carvalho de Melo 	struct parse_events_term *term, *h;
27768f707d84SJiri Olsa 
2777a8adfcebSWang Nan 	list_for_each_entry_safe(term, h, terms, list) {
2778a8adfcebSWang Nan 		list_del_init(&term->list);
27791dc92556SIan Rogers 		parse_events_term__delete(term);
27808f707d84SJiri Olsa 	}
2781a8adfcebSWang Nan }
2782b39b8393SJiri Olsa 
27832146afc6SArnaldo Carvalho de Melo void parse_events_terms__delete(struct list_head *terms)
2784fc0a2c1dSArnaldo Carvalho de Melo {
27852146afc6SArnaldo Carvalho de Melo 	if (!terms)
27862146afc6SArnaldo Carvalho de Melo 		return;
2787fc0a2c1dSArnaldo Carvalho de Melo 	parse_events_terms__purge(terms);
2788d20a5f2bSWang Nan 	free(terms);
2789fc0a2c1dSArnaldo Carvalho de Melo }
2790fc0a2c1dSArnaldo Carvalho de Melo 
27912d055bf2SWang Nan void parse_events__clear_array(struct parse_events_array *a)
27922d055bf2SWang Nan {
2793360e071bSTaeung Song 	zfree(&a->ranges);
27942d055bf2SWang Nan }
27952d055bf2SWang Nan 
27965d9cdc11SArnaldo Carvalho de Melo void parse_events_evlist_error(struct parse_events_state *parse_state,
2797b39b8393SJiri Olsa 			       int idx, const char *str)
2798b39b8393SJiri Olsa {
2799448d732cSIan Rogers 	if (!parse_state->error)
2800a6ced2beSAdrian Hunter 		return;
2801448d732cSIan Rogers 
28026c191289SIan Rogers 	parse_events_error__handle(parse_state->error, idx, strdup(str), NULL);
2803b39b8393SJiri Olsa }
2804ffeb883eSHe Kuang 
280517cb5f84SWang Nan static void config_terms_list(char *buf, size_t buf_sz)
280617cb5f84SWang Nan {
280717cb5f84SWang Nan 	int i;
280817cb5f84SWang Nan 	bool first = true;
280917cb5f84SWang Nan 
281017cb5f84SWang Nan 	buf[0] = '\0';
281117cb5f84SWang Nan 	for (i = 0; i < __PARSE_EVENTS__TERM_TYPE_NR; i++) {
281217cb5f84SWang Nan 		const char *name = config_term_names[i];
281317cb5f84SWang Nan 
28141669e509SWang Nan 		if (!config_term_avail(i, NULL))
28151669e509SWang Nan 			continue;
281617cb5f84SWang Nan 		if (!name)
281717cb5f84SWang Nan 			continue;
281817cb5f84SWang Nan 		if (name[0] == '<')
281917cb5f84SWang Nan 			continue;
282017cb5f84SWang Nan 
282117cb5f84SWang Nan 		if (strlen(buf) + strlen(name) + 2 >= buf_sz)
282217cb5f84SWang Nan 			return;
282317cb5f84SWang Nan 
282417cb5f84SWang Nan 		if (!first)
282517cb5f84SWang Nan 			strcat(buf, ",");
282617cb5f84SWang Nan 		else
282717cb5f84SWang Nan 			first = false;
282817cb5f84SWang Nan 		strcat(buf, name);
282917cb5f84SWang Nan 	}
283017cb5f84SWang Nan }
283117cb5f84SWang Nan 
2832ffeb883eSHe Kuang /*
2833ffeb883eSHe Kuang  * Return string contains valid config terms of an event.
2834ffeb883eSHe Kuang  * @additional_terms: For terms such as PMU sysfs terms.
2835ffeb883eSHe Kuang  */
2836ffeb883eSHe Kuang char *parse_events_formats_error_string(char *additional_terms)
2837ffeb883eSHe Kuang {
2838ffeb883eSHe Kuang 	char *str;
2839626a6b78SWang Nan 	/* "no-overwrite" is the longest name */
284017cb5f84SWang Nan 	char static_terms[__PARSE_EVENTS__TERM_TYPE_NR *
2841626a6b78SWang Nan 			  (sizeof("no-overwrite") - 1)];
2842ffeb883eSHe Kuang 
284317cb5f84SWang Nan 	config_terms_list(static_terms, sizeof(static_terms));
2844ffeb883eSHe Kuang 	/* valid terms */
2845ffeb883eSHe Kuang 	if (additional_terms) {
284626dee028SWang Nan 		if (asprintf(&str, "valid terms: %s,%s",
284726dee028SWang Nan 			     additional_terms, static_terms) < 0)
2848ffeb883eSHe Kuang 			goto fail;
2849ffeb883eSHe Kuang 	} else {
285026dee028SWang Nan 		if (asprintf(&str, "valid terms: %s", static_terms) < 0)
2851ffeb883eSHe Kuang 			goto fail;
2852ffeb883eSHe Kuang 	}
2853ffeb883eSHe Kuang 	return str;
2854ffeb883eSHe Kuang 
2855ffeb883eSHe Kuang fail:
2856ffeb883eSHe Kuang 	return NULL;
2857ffeb883eSHe Kuang }
28589cbfa2f6SJin Yao 
28599cbfa2f6SJin Yao struct evsel *parse_events__add_event_hybrid(struct list_head *list, int *idx,
28609cbfa2f6SJin Yao 					     struct perf_event_attr *attr,
28612b62b3a6SIan Rogers 					     const char *name,
28622b62b3a6SIan Rogers 					     const char *metric_id,
28632b62b3a6SIan Rogers 					     struct perf_pmu *pmu,
28649cbfa2f6SJin Yao 					     struct list_head *config_terms)
28659cbfa2f6SJin Yao {
28662b62b3a6SIan Rogers 	return __add_event(list, idx, attr, /*init_attr=*/true, name, metric_id,
28672b62b3a6SIan Rogers 			   pmu, config_terms, /*auto_merge_stats=*/false,
28682b62b3a6SIan Rogers 			   /*cpu_list=*/NULL);
28699cbfa2f6SJin Yao }
2870