xref: /linux/tools/perf/util/parse-events.c (revision 9d6a1df9b2eef52ad03a594b1237a16dbbe34e83)
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"
241eaf496eSIan Rogers #include "pmus.h"
25b39b8393SJiri Olsa #include "asm/bug.h"
26ac12f676SAndi Kleen #include "util/parse-branch-options.h"
2795be9d19SArnaldo Carvalho de Melo #include "util/evsel_config.h"
28252a2fdcSArnaldo Carvalho de Melo #include "util/event.h"
29d180aa56SNamhyung Kim #include "util/bpf-filter.h"
30ea0c5239SIan Rogers #include "util/util.h"
319b7c7728SIan Rogers #include "tracepoint.h"
3289812fc8SJiri Olsa 
3389812fc8SJiri Olsa #define MAX_NAME_LEN 100
3486470930SIngo Molnar 
3582ba1f2fSJiri Olsa #ifdef PARSER_DEBUG
3682ba1f2fSJiri Olsa extern int parse_events_debug;
3782ba1f2fSJiri Olsa #endif
385d9cdc11SArnaldo Carvalho de Melo int parse_events_parse(void *parse_state, void *scanner);
39e637d177SHe Kuang static int get_config_terms(struct list_head *head_config,
40e637d177SHe Kuang 			    struct list_head *head_terms __maybe_unused);
41bcd3279fSFrederic Weisbecker 
42705750f2SYunlong Song struct event_symbol event_symbols_hw[PERF_COUNT_HW_MAX] = {
431dc12760SJiri Olsa 	[PERF_COUNT_HW_CPU_CYCLES] = {
441dc12760SJiri Olsa 		.symbol = "cpu-cycles",
451dc12760SJiri Olsa 		.alias  = "cycles",
461dc12760SJiri Olsa 	},
471dc12760SJiri Olsa 	[PERF_COUNT_HW_INSTRUCTIONS] = {
481dc12760SJiri Olsa 		.symbol = "instructions",
491dc12760SJiri Olsa 		.alias  = "",
501dc12760SJiri Olsa 	},
511dc12760SJiri Olsa 	[PERF_COUNT_HW_CACHE_REFERENCES] = {
521dc12760SJiri Olsa 		.symbol = "cache-references",
531dc12760SJiri Olsa 		.alias  = "",
541dc12760SJiri Olsa 	},
551dc12760SJiri Olsa 	[PERF_COUNT_HW_CACHE_MISSES] = {
561dc12760SJiri Olsa 		.symbol = "cache-misses",
571dc12760SJiri Olsa 		.alias  = "",
581dc12760SJiri Olsa 	},
591dc12760SJiri Olsa 	[PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = {
601dc12760SJiri Olsa 		.symbol = "branch-instructions",
611dc12760SJiri Olsa 		.alias  = "branches",
621dc12760SJiri Olsa 	},
631dc12760SJiri Olsa 	[PERF_COUNT_HW_BRANCH_MISSES] = {
641dc12760SJiri Olsa 		.symbol = "branch-misses",
651dc12760SJiri Olsa 		.alias  = "",
661dc12760SJiri Olsa 	},
671dc12760SJiri Olsa 	[PERF_COUNT_HW_BUS_CYCLES] = {
681dc12760SJiri Olsa 		.symbol = "bus-cycles",
691dc12760SJiri Olsa 		.alias  = "",
701dc12760SJiri Olsa 	},
711dc12760SJiri Olsa 	[PERF_COUNT_HW_STALLED_CYCLES_FRONTEND] = {
721dc12760SJiri Olsa 		.symbol = "stalled-cycles-frontend",
731dc12760SJiri Olsa 		.alias  = "idle-cycles-frontend",
741dc12760SJiri Olsa 	},
751dc12760SJiri Olsa 	[PERF_COUNT_HW_STALLED_CYCLES_BACKEND] = {
761dc12760SJiri Olsa 		.symbol = "stalled-cycles-backend",
771dc12760SJiri Olsa 		.alias  = "idle-cycles-backend",
781dc12760SJiri Olsa 	},
791dc12760SJiri Olsa 	[PERF_COUNT_HW_REF_CPU_CYCLES] = {
801dc12760SJiri Olsa 		.symbol = "ref-cycles",
811dc12760SJiri Olsa 		.alias  = "",
821dc12760SJiri Olsa 	},
831dc12760SJiri Olsa };
8486470930SIngo Molnar 
85705750f2SYunlong Song struct event_symbol event_symbols_sw[PERF_COUNT_SW_MAX] = {
861dc12760SJiri Olsa 	[PERF_COUNT_SW_CPU_CLOCK] = {
871dc12760SJiri Olsa 		.symbol = "cpu-clock",
881dc12760SJiri Olsa 		.alias  = "",
891dc12760SJiri Olsa 	},
901dc12760SJiri Olsa 	[PERF_COUNT_SW_TASK_CLOCK] = {
911dc12760SJiri Olsa 		.symbol = "task-clock",
921dc12760SJiri Olsa 		.alias  = "",
931dc12760SJiri Olsa 	},
941dc12760SJiri Olsa 	[PERF_COUNT_SW_PAGE_FAULTS] = {
951dc12760SJiri Olsa 		.symbol = "page-faults",
961dc12760SJiri Olsa 		.alias  = "faults",
971dc12760SJiri Olsa 	},
981dc12760SJiri Olsa 	[PERF_COUNT_SW_CONTEXT_SWITCHES] = {
991dc12760SJiri Olsa 		.symbol = "context-switches",
1001dc12760SJiri Olsa 		.alias  = "cs",
1011dc12760SJiri Olsa 	},
1021dc12760SJiri Olsa 	[PERF_COUNT_SW_CPU_MIGRATIONS] = {
1031dc12760SJiri Olsa 		.symbol = "cpu-migrations",
1041dc12760SJiri Olsa 		.alias  = "migrations",
1051dc12760SJiri Olsa 	},
1061dc12760SJiri Olsa 	[PERF_COUNT_SW_PAGE_FAULTS_MIN] = {
1071dc12760SJiri Olsa 		.symbol = "minor-faults",
1081dc12760SJiri Olsa 		.alias  = "",
1091dc12760SJiri Olsa 	},
1101dc12760SJiri Olsa 	[PERF_COUNT_SW_PAGE_FAULTS_MAJ] = {
1111dc12760SJiri Olsa 		.symbol = "major-faults",
1121dc12760SJiri Olsa 		.alias  = "",
1131dc12760SJiri Olsa 	},
1141dc12760SJiri Olsa 	[PERF_COUNT_SW_ALIGNMENT_FAULTS] = {
1151dc12760SJiri Olsa 		.symbol = "alignment-faults",
1161dc12760SJiri Olsa 		.alias  = "",
1171dc12760SJiri Olsa 	},
1181dc12760SJiri Olsa 	[PERF_COUNT_SW_EMULATION_FAULTS] = {
1191dc12760SJiri Olsa 		.symbol = "emulation-faults",
1201dc12760SJiri Olsa 		.alias  = "",
1211dc12760SJiri Olsa 	},
122d22d1a2aSAdrian Hunter 	[PERF_COUNT_SW_DUMMY] = {
123d22d1a2aSAdrian Hunter 		.symbol = "dummy",
124d22d1a2aSAdrian Hunter 		.alias  = "",
125d22d1a2aSAdrian Hunter 	},
126bae9cc41SArnaldo Carvalho de Melo 	[PERF_COUNT_SW_BPF_OUTPUT] = {
127bae9cc41SArnaldo Carvalho de Melo 		.symbol = "bpf-output",
128bae9cc41SArnaldo Carvalho de Melo 		.alias  = "",
129bae9cc41SArnaldo Carvalho de Melo 	},
130fb6c79d7SNamhyung Kim 	[PERF_COUNT_SW_CGROUP_SWITCHES] = {
131fb6c79d7SNamhyung Kim 		.symbol = "cgroup-switches",
132fb6c79d7SNamhyung Kim 		.alias  = "",
133fb6c79d7SNamhyung Kim 	},
13486470930SIngo Molnar };
13586470930SIngo Molnar 
1361424dc96SDavid Ahern const char *event_type(int type)
1371424dc96SDavid Ahern {
1381424dc96SDavid Ahern 	switch (type) {
1391424dc96SDavid Ahern 	case PERF_TYPE_HARDWARE:
1401424dc96SDavid Ahern 		return "hardware";
1411424dc96SDavid Ahern 
1421424dc96SDavid Ahern 	case PERF_TYPE_SOFTWARE:
1431424dc96SDavid Ahern 		return "software";
1441424dc96SDavid Ahern 
1451424dc96SDavid Ahern 	case PERF_TYPE_TRACEPOINT:
1461424dc96SDavid Ahern 		return "tracepoint";
1471424dc96SDavid Ahern 
1481424dc96SDavid Ahern 	case PERF_TYPE_HW_CACHE:
1491424dc96SDavid Ahern 		return "hardware-cache";
1501424dc96SDavid Ahern 
1511424dc96SDavid Ahern 	default:
1521424dc96SDavid Ahern 		break;
1531424dc96SDavid Ahern 	}
1541424dc96SDavid Ahern 
1551424dc96SDavid Ahern 	return "unknown";
1561424dc96SDavid Ahern }
1571424dc96SDavid Ahern 
1582b62b3a6SIan Rogers static char *get_config_str(struct list_head *head_terms, int type_term)
159e814fdddSWang Nan {
160e814fdddSWang Nan 	struct parse_events_term *term;
161e814fdddSWang Nan 
162e814fdddSWang Nan 	if (!head_terms)
163e814fdddSWang Nan 		return NULL;
164e814fdddSWang Nan 
165e814fdddSWang Nan 	list_for_each_entry(term, head_terms, list)
1662b62b3a6SIan Rogers 		if (term->type_term == type_term)
167e814fdddSWang Nan 			return term->val.str;
168e814fdddSWang Nan 
169e814fdddSWang Nan 	return NULL;
170e814fdddSWang Nan }
1717ae92e74SYan, Zheng 
1722b62b3a6SIan Rogers static char *get_config_metric_id(struct list_head *head_terms)
1732b62b3a6SIan Rogers {
1742b62b3a6SIan Rogers 	return get_config_str(head_terms, PARSE_EVENTS__TERM_TYPE_METRIC_ID);
1752b62b3a6SIan Rogers }
1762b62b3a6SIan Rogers 
1772b62b3a6SIan Rogers static char *get_config_name(struct list_head *head_terms)
1782b62b3a6SIan Rogers {
1792b62b3a6SIan Rogers 	return get_config_str(head_terms, PARSE_EVENTS__TERM_TYPE_NAME);
1802b62b3a6SIan Rogers }
1812b62b3a6SIan Rogers 
18270c90e4aSIan Rogers /**
18370c90e4aSIan Rogers  * fix_raw - For each raw term see if there is an event (aka alias) in pmu that
18470c90e4aSIan Rogers  *           matches the raw's string value. If the string value matches an
18570c90e4aSIan Rogers  *           event then change the term to be an event, if not then change it to
18670c90e4aSIan Rogers  *           be a config term. For example, "read" may be an event of the PMU or
18770c90e4aSIan Rogers  *           a raw hex encoding of 0xead. The fix-up is done late so the PMU of
18870c90e4aSIan Rogers  *           the event can be determined and we don't need to scan all PMUs
18970c90e4aSIan Rogers  *           ahead-of-time.
19070c90e4aSIan Rogers  * @config_terms: the list of terms that may contain a raw term.
19170c90e4aSIan Rogers  * @pmu: the PMU to scan for events from.
19270c90e4aSIan Rogers  */
19370c90e4aSIan Rogers static void fix_raw(struct list_head *config_terms, struct perf_pmu *pmu)
19470c90e4aSIan Rogers {
19570c90e4aSIan Rogers 	struct parse_events_term *term;
19670c90e4aSIan Rogers 
19770c90e4aSIan Rogers 	list_for_each_entry(term, config_terms, list) {
19870c90e4aSIan Rogers 		struct perf_pmu_alias *alias;
19970c90e4aSIan Rogers 		bool matched = false;
20070c90e4aSIan Rogers 
20170c90e4aSIan Rogers 		if (term->type_term != PARSE_EVENTS__TERM_TYPE_RAW)
20270c90e4aSIan Rogers 			continue;
20370c90e4aSIan Rogers 
20470c90e4aSIan Rogers 		list_for_each_entry(alias, &pmu->aliases, list) {
20570c90e4aSIan Rogers 			if (!strcmp(alias->name, term->val.str)) {
20670c90e4aSIan Rogers 				free(term->config);
20770c90e4aSIan Rogers 				term->config = term->val.str;
20870c90e4aSIan Rogers 				term->type_val = PARSE_EVENTS__TERM_TYPE_NUM;
20970c90e4aSIan Rogers 				term->type_term = PARSE_EVENTS__TERM_TYPE_USER;
21070c90e4aSIan Rogers 				term->val.num = 1;
21170c90e4aSIan Rogers 				term->no_value = true;
21270c90e4aSIan Rogers 				matched = true;
21370c90e4aSIan Rogers 				break;
21470c90e4aSIan Rogers 			}
21570c90e4aSIan Rogers 		}
21670c90e4aSIan Rogers 		if (!matched) {
21770c90e4aSIan Rogers 			u64 num;
21870c90e4aSIan Rogers 
21970c90e4aSIan Rogers 			free(term->config);
22070c90e4aSIan Rogers 			term->config = strdup("config");
22170c90e4aSIan Rogers 			errno = 0;
22270c90e4aSIan Rogers 			num = strtoull(term->val.str + 1, NULL, 16);
22370c90e4aSIan Rogers 			assert(errno == 0);
22470c90e4aSIan Rogers 			free(term->val.str);
22570c90e4aSIan Rogers 			term->type_val = PARSE_EVENTS__TERM_TYPE_NUM;
22670c90e4aSIan Rogers 			term->type_term = PARSE_EVENTS__TERM_TYPE_CONFIG;
22770c90e4aSIan Rogers 			term->val.num = num;
22870c90e4aSIan Rogers 			term->no_value = false;
22970c90e4aSIan Rogers 		}
23070c90e4aSIan Rogers 	}
23170c90e4aSIan Rogers }
23270c90e4aSIan Rogers 
23332dcd021SJiri Olsa static struct evsel *
234410136f5SStephane Eranian __add_event(struct list_head *list, int *idx,
2357ae92e74SYan, Zheng 	    struct perf_event_attr *attr,
23670943490SStephane Eranian 	    bool init_attr,
2372b62b3a6SIan Rogers 	    const char *name, const char *metric_id, struct perf_pmu *pmu,
238f0fbb114SAndi Kleen 	    struct list_head *config_terms, bool auto_merge_stats,
239f0fbb114SAndi Kleen 	    const char *cpu_list)
24089812fc8SJiri Olsa {
24132dcd021SJiri Olsa 	struct evsel *evsel;
2425d680be3SNamhyung Kim 	struct perf_cpu_map *cpus = pmu ? perf_cpu_map__get(pmu->cpus) :
2439c3516d1SJiri Olsa 			       cpu_list ? perf_cpu_map__new(cpu_list) : NULL;
24489812fc8SJiri Olsa 
245e552b7beSRob Herring 	if (pmu)
246e552b7beSRob Herring 		perf_pmu__warn_invalid_formats(pmu);
247e552b7beSRob Herring 
248e4064776SJin Yao 	if (pmu && attr->type == PERF_TYPE_RAW)
249e4064776SJin Yao 		perf_pmu__warn_invalid_config(pmu, attr->config, name);
250e4064776SJin Yao 
25170943490SStephane Eranian 	if (init_attr)
25289812fc8SJiri Olsa 		event_attr_init(attr);
25389812fc8SJiri Olsa 
2548f6725a2SArnaldo Carvalho de Melo 	evsel = evsel__new_idx(attr, *idx);
2550f1b550eSNamhyung Kim 	if (!evsel) {
2560f1b550eSNamhyung Kim 		perf_cpu_map__put(cpus);
257410136f5SStephane Eranian 		return NULL;
2580f1b550eSNamhyung Kim 	}
25989812fc8SJiri Olsa 
26075fc5ae5STaeung Song 	(*idx)++;
2615d680be3SNamhyung Kim 	evsel->core.cpus = cpus;
262fe1f61b3SJiri Olsa 	evsel->core.own_cpus = perf_cpu_map__get(cpus);
263d3345fecSAdrian Hunter 	evsel->core.requires_cpu = pmu ? pmu->is_uncore : false;
2641578e63dSIan Rogers 	evsel->core.is_pmu_core = pmu ? pmu->is_core : false;
26563ce8449SArnaldo Carvalho de Melo 	evsel->auto_merge_stats = auto_merge_stats;
266f7400262SNamhyung Kim 	evsel->pmu = pmu;
267cae256aeSIan Rogers 	evsel->pmu_name = pmu && pmu->name ? strdup(pmu->name) : NULL;
268f30a79b0SJiri Olsa 
2699db1763cSArnaldo Carvalho de Melo 	if (name)
27089812fc8SJiri Olsa 		evsel->name = strdup(name);
271930a2e29SJiri Olsa 
2722b62b3a6SIan Rogers 	if (metric_id)
2732b62b3a6SIan Rogers 		evsel->metric_id = strdup(metric_id);
2742b62b3a6SIan Rogers 
275930a2e29SJiri Olsa 	if (config_terms)
27699fc5941SAdrian Hunter 		list_splice_init(config_terms, &evsel->config_terms);
277930a2e29SJiri Olsa 
27870943490SStephane Eranian 	if (list)
279b27c4eceSJiri Olsa 		list_add_tail(&evsel->core.node, list);
28070943490SStephane Eranian 
281410136f5SStephane Eranian 	return evsel;
28289812fc8SJiri Olsa }
28389812fc8SJiri Olsa 
28470943490SStephane Eranian struct evsel *parse_events__add_event(int idx, struct perf_event_attr *attr,
2852b62b3a6SIan Rogers 				      const char *name, const char *metric_id,
2862b62b3a6SIan Rogers 				      struct perf_pmu *pmu)
28770943490SStephane Eranian {
2882b62b3a6SIan Rogers 	return __add_event(/*list=*/NULL, &idx, attr, /*init_attr=*/false, name,
2892b62b3a6SIan Rogers 			   metric_id, pmu, /*config_terms=*/NULL,
2902b62b3a6SIan Rogers 			   /*auto_merge_stats=*/false, /*cpu_list=*/NULL);
29170943490SStephane Eranian }
29270943490SStephane Eranian 
293c5cd8ac0SDavid Ahern static int add_event(struct list_head *list, int *idx,
2948e8bbfb3SIan Rogers 		     struct perf_event_attr *attr, const char *name,
2952b62b3a6SIan Rogers 		     const char *metric_id, struct list_head *config_terms)
2967ae92e74SYan, Zheng {
2972b62b3a6SIan Rogers 	return __add_event(list, idx, attr, /*init_attr*/true, name, metric_id,
2982b62b3a6SIan Rogers 			   /*pmu=*/NULL, config_terms,
2992b62b3a6SIan Rogers 			   /*auto_merge_stats=*/false, /*cpu_list=*/NULL) ? 0 : -ENOMEM;
300f0fbb114SAndi Kleen }
301f0fbb114SAndi Kleen 
302f0fbb114SAndi Kleen static int add_event_tool(struct list_head *list, int *idx,
303f0fbb114SAndi Kleen 			  enum perf_tool_event tool_event)
304f0fbb114SAndi Kleen {
30532dcd021SJiri Olsa 	struct evsel *evsel;
306f0fbb114SAndi Kleen 	struct perf_event_attr attr = {
307f0fbb114SAndi Kleen 		.type = PERF_TYPE_SOFTWARE,
308f0fbb114SAndi Kleen 		.config = PERF_COUNT_SW_DUMMY,
309f0fbb114SAndi Kleen 	};
310f0fbb114SAndi Kleen 
3112b62b3a6SIan Rogers 	evsel = __add_event(list, idx, &attr, /*init_attr=*/true, /*name=*/NULL,
3122b62b3a6SIan Rogers 			    /*metric_id=*/NULL, /*pmu=*/NULL,
3132b62b3a6SIan Rogers 			    /*config_terms=*/NULL, /*auto_merge_stats=*/false,
3142b62b3a6SIan Rogers 			    /*cpu_list=*/"0");
315f0fbb114SAndi Kleen 	if (!evsel)
316f0fbb114SAndi Kleen 		return -ENOMEM;
317f0fbb114SAndi Kleen 	evsel->tool_event = tool_event;
318b03b89b3SFlorian Fischer 	if (tool_event == PERF_TOOL_DURATION_TIME
319b03b89b3SFlorian Fischer 	    || tool_event == PERF_TOOL_USER_TIME
320b03b89b3SFlorian Fischer 	    || tool_event == PERF_TOOL_SYSTEM_TIME) {
321b194c9cdSIan Rogers 		free((char *)evsel->unit);
322b194c9cdSIan Rogers 		evsel->unit = strdup("ns");
323b194c9cdSIan Rogers 	}
324f0fbb114SAndi Kleen 	return 0;
3257ae92e74SYan, Zheng }
3267ae92e74SYan, Zheng 
32770c90e4aSIan Rogers /**
32870c90e4aSIan Rogers  * parse_aliases - search names for entries beginning or equalling str ignoring
32970c90e4aSIan Rogers  *                 case. If mutliple entries in names match str then the longest
33070c90e4aSIan Rogers  *                 is chosen.
33170c90e4aSIan Rogers  * @str: The needle to look for.
33270c90e4aSIan Rogers  * @names: The haystack to search.
33370c90e4aSIan Rogers  * @size: The size of the haystack.
33470c90e4aSIan Rogers  * @longest: Out argument giving the length of the matching entry.
33570c90e4aSIan Rogers  */
33670c90e4aSIan Rogers static int parse_aliases(const char *str, const char *const names[][EVSEL__MAX_ALIASES], int size,
33770c90e4aSIan Rogers 			 int *longest)
33886470930SIngo Molnar {
33970c90e4aSIan Rogers 	*longest = -1;
34070c90e4aSIan Rogers 	for (int i = 0; i < size; i++) {
34170c90e4aSIan Rogers 		for (int j = 0; j < EVSEL__MAX_ALIASES && names[i][j]; j++) {
34270c90e4aSIan Rogers 			int n = strlen(names[i][j]);
34386470930SIngo Molnar 
34470c90e4aSIan Rogers 			if (n > *longest && !strncasecmp(str, names[i][j], n))
34570c90e4aSIan Rogers 				*longest = n;
34661c45981SPaul Mackerras 		}
34770c90e4aSIan Rogers 		if (*longest > 0)
34886470930SIngo Molnar 			return i;
34986470930SIngo Molnar 	}
35086470930SIngo Molnar 
3518953645fSIngo Molnar 	return -1;
35286470930SIngo Molnar }
35386470930SIngo Molnar 
35443d0b978SWang Nan typedef int config_term_func_t(struct perf_event_attr *attr,
35543d0b978SWang Nan 			       struct parse_events_term *term,
35643d0b978SWang Nan 			       struct parse_events_error *err);
35743d0b978SWang Nan static int config_term_common(struct perf_event_attr *attr,
35843d0b978SWang Nan 			      struct parse_events_term *term,
35943d0b978SWang Nan 			      struct parse_events_error *err);
36043d0b978SWang Nan static int config_attr(struct perf_event_attr *attr,
36143d0b978SWang Nan 		       struct list_head *head,
36243d0b978SWang Nan 		       struct parse_events_error *err,
36343d0b978SWang Nan 		       config_term_func_t config_term);
36443d0b978SWang Nan 
3656fd1e519SIan Rogers /**
3666fd1e519SIan Rogers  * parse_events__decode_legacy_cache - Search name for the legacy cache event
3676fd1e519SIan Rogers  *                                     name composed of 1, 2 or 3 hyphen
3686fd1e519SIan Rogers  *                                     separated sections. The first section is
3696fd1e519SIan Rogers  *                                     the cache type while the others are the
3706fd1e519SIan Rogers  *                                     optional op and optional result. To make
3716fd1e519SIan Rogers  *                                     life hard the names in the table also
3726fd1e519SIan Rogers  *                                     contain hyphens and the longest name
37370c90e4aSIan Rogers  *                                     should always be selected.
37486470930SIngo Molnar  */
375d7f21df0SIan Rogers int parse_events__decode_legacy_cache(const char *name, int pmu_type, __u64 *config)
3766fd1e519SIan Rogers {
3776fd1e519SIan Rogers 	int len, cache_type = -1, cache_op = -1, cache_result = -1;
3786fd1e519SIan Rogers 	const char *name_end = &name[strlen(name) + 1];
3796fd1e519SIan Rogers 	const char *str = name;
3806fd1e519SIan Rogers 
38170c90e4aSIan Rogers 	cache_type = parse_aliases(str, evsel__hw_cache, PERF_COUNT_HW_CACHE_MAX, &len);
38286470930SIngo Molnar 	if (cache_type == -1)
38389812fc8SJiri Olsa 		return -EINVAL;
38470c90e4aSIan Rogers 	str += len + 1;
38586470930SIngo Molnar 
38670c90e4aSIan Rogers 	if (str < name_end) {
387c64e85e1SArnaldo Carvalho de Melo 		cache_op = parse_aliases(str, evsel__hw_cache_op,
38870c90e4aSIan Rogers 					PERF_COUNT_HW_CACHE_OP_MAX, &len);
38961c45981SPaul Mackerras 		if (cache_op >= 0) {
390c754c382SArnaldo Carvalho de Melo 			if (!evsel__is_cache_op_valid(cache_type, cache_op))
39189812fc8SJiri Olsa 				return -EINVAL;
39270c90e4aSIan Rogers 			str += len + 1;
39370c90e4aSIan Rogers 		} else {
394c64e85e1SArnaldo Carvalho de Melo 			cache_result = parse_aliases(str, evsel__hw_cache_result,
39570c90e4aSIan Rogers 						PERF_COUNT_HW_CACHE_RESULT_MAX, &len);
39661c45981SPaul Mackerras 			if (cache_result >= 0)
39770c90e4aSIan Rogers 				str += len + 1;
39870c90e4aSIan Rogers 		}
39970c90e4aSIan Rogers 	}
40070c90e4aSIan Rogers 	if (str < name_end) {
40170c90e4aSIan Rogers 		if (cache_op < 0) {
40270c90e4aSIan Rogers 			cache_op = parse_aliases(str, evsel__hw_cache_op,
40370c90e4aSIan Rogers 						PERF_COUNT_HW_CACHE_OP_MAX, &len);
40470c90e4aSIan Rogers 			if (cache_op >= 0) {
40570c90e4aSIan Rogers 				if (!evsel__is_cache_op_valid(cache_type, cache_op))
40670c90e4aSIan Rogers 					return -EINVAL;
40770c90e4aSIan Rogers 			}
40870c90e4aSIan Rogers 		} else if (cache_result < 0) {
40970c90e4aSIan Rogers 			cache_result = parse_aliases(str, evsel__hw_cache_result,
41070c90e4aSIan Rogers 						PERF_COUNT_HW_CACHE_RESULT_MAX, &len);
41161c45981SPaul Mackerras 		}
41261c45981SPaul Mackerras 	}
41361c45981SPaul Mackerras 
41486470930SIngo Molnar 	/*
41586470930SIngo Molnar 	 * Fall back to reads:
41686470930SIngo Molnar 	 */
4178953645fSIngo Molnar 	if (cache_op == -1)
4188953645fSIngo Molnar 		cache_op = PERF_COUNT_HW_CACHE_OP_READ;
41986470930SIngo Molnar 
42086470930SIngo Molnar 	/*
42186470930SIngo Molnar 	 * Fall back to accesses:
42286470930SIngo Molnar 	 */
42386470930SIngo Molnar 	if (cache_result == -1)
42486470930SIngo Molnar 		cache_result = PERF_COUNT_HW_CACHE_RESULT_ACCESS;
42586470930SIngo Molnar 
4266fd1e519SIan Rogers 	*config = ((__u64)pmu_type << PERF_PMU_TYPE_SHIFT) |
4276fd1e519SIan Rogers 		cache_type | (cache_op << 8) | (cache_result << 16);
4286fd1e519SIan Rogers 	return 0;
4296fd1e519SIan Rogers }
4306fd1e519SIan Rogers 
431411ad22eSIan Rogers /**
432411ad22eSIan Rogers  * parse_events__filter_pmu - returns false if a wildcard PMU should be
433411ad22eSIan Rogers  *                            considered, true if it should be filtered.
434411ad22eSIan Rogers  */
435411ad22eSIan Rogers bool parse_events__filter_pmu(const struct parse_events_state *parse_state,
436411ad22eSIan Rogers 			      const struct perf_pmu *pmu)
437411ad22eSIan Rogers {
438411ad22eSIan Rogers 	if (parse_state->pmu_filter == NULL)
439411ad22eSIan Rogers 		return false;
440411ad22eSIan Rogers 
441411ad22eSIan Rogers 	if (pmu->name == NULL)
442411ad22eSIan Rogers 		return true;
443411ad22eSIan Rogers 
444411ad22eSIan Rogers 	return strcmp(parse_state->pmu_filter, pmu->name) != 0;
445411ad22eSIan Rogers }
446411ad22eSIan Rogers 
4476fd1e519SIan Rogers int parse_events_add_cache(struct list_head *list, int *idx, const char *name,
448411ad22eSIan Rogers 			   struct parse_events_state *parse_state,
4492bdf4d7eSIan Rogers 			   struct list_head *head_config)
4506fd1e519SIan Rogers {
4512bdf4d7eSIan Rogers 	struct perf_pmu *pmu = NULL;
4522bdf4d7eSIan Rogers 	bool found_supported = false;
4532bdf4d7eSIan Rogers 	const char *config_name = get_config_name(head_config);
4542bdf4d7eSIan Rogers 	const char *metric_id = get_config_metric_id(head_config);
4556fd1e519SIan Rogers 
456*9d6a1df9SIan Rogers 	/* Legacy cache events are only supported by core PMUs. */
457*9d6a1df9SIan Rogers 	while ((pmu = perf_pmus__scan_core(pmu)) != NULL) {
4582bdf4d7eSIan Rogers 		LIST_HEAD(config_terms);
4592bdf4d7eSIan Rogers 		struct perf_event_attr attr;
4602bdf4d7eSIan Rogers 		int ret;
4612bdf4d7eSIan Rogers 
462411ad22eSIan Rogers 		if (parse_events__filter_pmu(parse_state, pmu))
463411ad22eSIan Rogers 			continue;
464411ad22eSIan Rogers 
46589812fc8SJiri Olsa 		memset(&attr, 0, sizeof(attr));
46689812fc8SJiri Olsa 		attr.type = PERF_TYPE_HW_CACHE;
4672bdf4d7eSIan Rogers 
4682bdf4d7eSIan Rogers 		ret = parse_events__decode_legacy_cache(name, pmu->type, &attr.config);
4696fd1e519SIan Rogers 		if (ret)
4706fd1e519SIan Rogers 			return ret;
47143d0b978SWang Nan 
4722bdf4d7eSIan Rogers 		found_supported = true;
4732bdf4d7eSIan Rogers 
47443d0b978SWang Nan 		if (head_config) {
475411ad22eSIan Rogers 			if (config_attr(&attr, head_config, parse_state->error, config_term_common))
47643d0b978SWang Nan 				return -EINVAL;
47743d0b978SWang Nan 
47843d0b978SWang Nan 			if (get_config_terms(head_config, &config_terms))
47943d0b978SWang Nan 				return -ENOMEM;
48043d0b978SWang Nan 		}
48130def61fSJin Yao 
4822bdf4d7eSIan Rogers 		if (__add_event(list, idx, &attr, /*init_attr*/true, config_name ?: name,
4832bdf4d7eSIan Rogers 				metric_id, pmu, &config_terms, /*auto_merge_stats=*/false,
4842bdf4d7eSIan Rogers 				/*cpu_list=*/NULL) == NULL)
4852bdf4d7eSIan Rogers 			return -ENOMEM;
48630def61fSJin Yao 
48799fc5941SAdrian Hunter 		free_config_terms(&config_terms);
4882bdf4d7eSIan Rogers 	}
4892bdf4d7eSIan Rogers 	return found_supported ? 0 : -EINVAL;
49086470930SIngo Molnar }
49186470930SIngo Molnar 
492378ef0f5SIan Rogers #ifdef HAVE_LIBTRACEEVENT
493272ed29aSJiri Olsa static void tracepoint_error(struct parse_events_error *e, int err,
4948c619d6aSWang Nan 			     const char *sys, const char *name)
49519658171SJiri Olsa {
4964584f084SIan Rogers 	const char *str;
49719658171SJiri Olsa 	char help[BUFSIZ];
49819658171SJiri Olsa 
499ec183d22SAdrian Hunter 	if (!e)
500ec183d22SAdrian Hunter 		return;
501ec183d22SAdrian Hunter 
50219658171SJiri Olsa 	/*
50319658171SJiri Olsa 	 * We get error directly from syscall errno ( > 0),
50419658171SJiri Olsa 	 * or from encoded pointer's error ( < 0).
50519658171SJiri Olsa 	 */
50619658171SJiri Olsa 	err = abs(err);
50719658171SJiri Olsa 
50819658171SJiri Olsa 	switch (err) {
50919658171SJiri Olsa 	case EACCES:
5104584f084SIan Rogers 		str = "can't access trace events";
51119658171SJiri Olsa 		break;
51219658171SJiri Olsa 	case ENOENT:
5134584f084SIan Rogers 		str = "unknown tracepoint";
51419658171SJiri Olsa 		break;
51519658171SJiri Olsa 	default:
5164584f084SIan Rogers 		str = "failed to add tracepoint";
51719658171SJiri Olsa 		break;
51819658171SJiri Olsa 	}
51919658171SJiri Olsa 
52019658171SJiri Olsa 	tracing_path__strerror_open_tp(err, help, sizeof(help), sys, name);
5216c191289SIan Rogers 	parse_events_error__handle(e, 0, strdup(str), strdup(help));
52219658171SJiri Olsa }
52319658171SJiri Olsa 
524c5cd8ac0SDavid Ahern static int add_tracepoint(struct list_head *list, int *idx,
5258c619d6aSWang Nan 			  const char *sys_name, const char *evt_name,
526272ed29aSJiri Olsa 			  struct parse_events_error *err,
527e637d177SHe Kuang 			  struct list_head *head_config)
528bcd3279fSFrederic Weisbecker {
5298f6725a2SArnaldo Carvalho de Melo 	struct evsel *evsel = evsel__newtp_idx(sys_name, evt_name, (*idx)++);
530bcd3279fSFrederic Weisbecker 
53119658171SJiri Olsa 	if (IS_ERR(evsel)) {
532272ed29aSJiri Olsa 		tracepoint_error(err, PTR_ERR(evsel), sys_name, evt_name);
5338dd2a131SJiri Olsa 		return PTR_ERR(evsel);
53419658171SJiri Olsa 	}
535bcd3279fSFrederic Weisbecker 
536e637d177SHe Kuang 	if (head_config) {
537e637d177SHe Kuang 		LIST_HEAD(config_terms);
538e637d177SHe Kuang 
539e637d177SHe Kuang 		if (get_config_terms(head_config, &config_terms))
540e637d177SHe Kuang 			return -ENOMEM;
541e637d177SHe Kuang 		list_splice(&config_terms, &evsel->config_terms);
542e637d177SHe Kuang 	}
543e637d177SHe Kuang 
544b27c4eceSJiri Olsa 	list_add_tail(&evsel->core.node, list);
54582fe1c29SArnaldo Carvalho de Melo 	return 0;
546bcd3279fSFrederic Weisbecker }
547bcd3279fSFrederic Weisbecker 
548c5cd8ac0SDavid Ahern static int add_tracepoint_multi_event(struct list_head *list, int *idx,
5498c619d6aSWang Nan 				      const char *sys_name, const char *evt_name,
550272ed29aSJiri Olsa 				      struct parse_events_error *err,
551e637d177SHe Kuang 				      struct list_head *head_config)
552bcd3279fSFrederic Weisbecker {
55325a7d914SArnaldo Carvalho de Melo 	char *evt_path;
554bcd3279fSFrederic Weisbecker 	struct dirent *evt_ent;
555bcd3279fSFrederic Weisbecker 	DIR *evt_dir;
55627bf90bfSJiri Olsa 	int ret = 0, found = 0;
557bcd3279fSFrederic Weisbecker 
55825a7d914SArnaldo Carvalho de Melo 	evt_path = get_events_file(sys_name);
55925a7d914SArnaldo Carvalho de Melo 	if (!evt_path) {
56025a7d914SArnaldo Carvalho de Melo 		tracepoint_error(err, errno, sys_name, evt_name);
56125a7d914SArnaldo Carvalho de Melo 		return -1;
56225a7d914SArnaldo Carvalho de Melo 	}
563bcd3279fSFrederic Weisbecker 	evt_dir = opendir(evt_path);
564bcd3279fSFrederic Weisbecker 	if (!evt_dir) {
56525a7d914SArnaldo Carvalho de Melo 		put_events_file(evt_path);
566272ed29aSJiri Olsa 		tracepoint_error(err, errno, sys_name, evt_name);
56789812fc8SJiri Olsa 		return -1;
568bcd3279fSFrederic Weisbecker 	}
569bcd3279fSFrederic Weisbecker 
57089812fc8SJiri Olsa 	while (!ret && (evt_ent = readdir(evt_dir))) {
571bcd3279fSFrederic Weisbecker 		if (!strcmp(evt_ent->d_name, ".")
572bcd3279fSFrederic Weisbecker 		    || !strcmp(evt_ent->d_name, "..")
573bcd3279fSFrederic Weisbecker 		    || !strcmp(evt_ent->d_name, "enable")
574bcd3279fSFrederic Weisbecker 		    || !strcmp(evt_ent->d_name, "filter"))
575bcd3279fSFrederic Weisbecker 			continue;
576bcd3279fSFrederic Weisbecker 
57789812fc8SJiri Olsa 		if (!strglobmatch(evt_ent->d_name, evt_name))
578fb1d2edfSMasami Hiramatsu 			continue;
579fb1d2edfSMasami Hiramatsu 
58027bf90bfSJiri Olsa 		found++;
58127bf90bfSJiri Olsa 
582e637d177SHe Kuang 		ret = add_tracepoint(list, idx, sys_name, evt_ent->d_name,
583272ed29aSJiri Olsa 				     err, head_config);
584bcd3279fSFrederic Weisbecker 	}
585bcd3279fSFrederic Weisbecker 
58627bf90bfSJiri Olsa 	if (!found) {
58727bf90bfSJiri Olsa 		tracepoint_error(err, ENOENT, sys_name, evt_name);
58827bf90bfSJiri Olsa 		ret = -1;
58927bf90bfSJiri Olsa 	}
59027bf90bfSJiri Olsa 
59125a7d914SArnaldo Carvalho de Melo 	put_events_file(evt_path);
5920bd3f084SJiri Olsa 	closedir(evt_dir);
59389812fc8SJiri Olsa 	return ret;
594bcd3279fSFrederic Weisbecker }
595bcd3279fSFrederic Weisbecker 
596c5cd8ac0SDavid Ahern static int add_tracepoint_event(struct list_head *list, int *idx,
5978c619d6aSWang Nan 				const char *sys_name, const char *evt_name,
598272ed29aSJiri Olsa 				struct parse_events_error *err,
599e637d177SHe Kuang 				struct list_head *head_config)
600f35488f9SJiri Olsa {
601f35488f9SJiri Olsa 	return strpbrk(evt_name, "*?") ?
602e637d177SHe Kuang 	       add_tracepoint_multi_event(list, idx, sys_name, evt_name,
603272ed29aSJiri Olsa 					  err, head_config) :
604e637d177SHe Kuang 	       add_tracepoint(list, idx, sys_name, evt_name,
605272ed29aSJiri Olsa 			      err, head_config);
606f35488f9SJiri Olsa }
607f35488f9SJiri Olsa 
608c5cd8ac0SDavid Ahern static int add_tracepoint_multi_sys(struct list_head *list, int *idx,
6098c619d6aSWang Nan 				    const char *sys_name, const char *evt_name,
610272ed29aSJiri Olsa 				    struct parse_events_error *err,
611e637d177SHe Kuang 				    struct list_head *head_config)
612f35488f9SJiri Olsa {
613f35488f9SJiri Olsa 	struct dirent *events_ent;
614f35488f9SJiri Olsa 	DIR *events_dir;
615f35488f9SJiri Olsa 	int ret = 0;
616f35488f9SJiri Olsa 
6177014e0e3SArnaldo Carvalho de Melo 	events_dir = tracing_events__opendir();
618f35488f9SJiri Olsa 	if (!events_dir) {
619272ed29aSJiri Olsa 		tracepoint_error(err, errno, sys_name, evt_name);
620f35488f9SJiri Olsa 		return -1;
621f35488f9SJiri Olsa 	}
622f35488f9SJiri Olsa 
623f35488f9SJiri Olsa 	while (!ret && (events_ent = readdir(events_dir))) {
624f35488f9SJiri Olsa 		if (!strcmp(events_ent->d_name, ".")
625f35488f9SJiri Olsa 		    || !strcmp(events_ent->d_name, "..")
626f35488f9SJiri Olsa 		    || !strcmp(events_ent->d_name, "enable")
627f35488f9SJiri Olsa 		    || !strcmp(events_ent->d_name, "header_event")
628f35488f9SJiri Olsa 		    || !strcmp(events_ent->d_name, "header_page"))
629f35488f9SJiri Olsa 			continue;
630f35488f9SJiri Olsa 
631f35488f9SJiri Olsa 		if (!strglobmatch(events_ent->d_name, sys_name))
632f35488f9SJiri Olsa 			continue;
633f35488f9SJiri Olsa 
634f35488f9SJiri Olsa 		ret = add_tracepoint_event(list, idx, events_ent->d_name,
635272ed29aSJiri Olsa 					   evt_name, err, head_config);
636f35488f9SJiri Olsa 	}
637f35488f9SJiri Olsa 
638f35488f9SJiri Olsa 	closedir(events_dir);
639f35488f9SJiri Olsa 	return ret;
640f35488f9SJiri Olsa }
641378ef0f5SIan Rogers #endif /* HAVE_LIBTRACEEVENT */
642f35488f9SJiri Olsa 
643c18cf78dSArnaldo Carvalho de Melo #ifdef HAVE_LIBBPF_SUPPORT
6444edf30e3SWang Nan struct __add_bpf_event_param {
6455d9cdc11SArnaldo Carvalho de Melo 	struct parse_events_state *parse_state;
6464edf30e3SWang Nan 	struct list_head *list;
64795088a59SWang Nan 	struct list_head *head_config;
6484edf30e3SWang Nan };
6494edf30e3SWang Nan 
650af4a0991SArnaldo Carvalho de Melo static int add_bpf_event(const char *group, const char *event, int fd, struct bpf_object *obj,
6514edf30e3SWang Nan 			 void *_param)
6524edf30e3SWang Nan {
6534edf30e3SWang Nan 	LIST_HEAD(new_evsels);
6544edf30e3SWang Nan 	struct __add_bpf_event_param *param = _param;
6555d9cdc11SArnaldo Carvalho de Melo 	struct parse_events_state *parse_state = param->parse_state;
6564edf30e3SWang Nan 	struct list_head *list = param->list;
65732dcd021SJiri Olsa 	struct evsel *pos;
6584edf30e3SWang Nan 	int err;
6592620b7e3SArnaldo Carvalho de Melo 	/*
6602620b7e3SArnaldo Carvalho de Melo 	 * Check if we should add the event, i.e. if it is a TP but starts with a '!',
6612620b7e3SArnaldo Carvalho de Melo 	 * then don't add the tracepoint, this will be used for something else, like
6622620b7e3SArnaldo Carvalho de Melo 	 * adding to a BPF_MAP_TYPE_PROG_ARRAY.
6632620b7e3SArnaldo Carvalho de Melo 	 *
6642620b7e3SArnaldo Carvalho de Melo 	 * See tools/perf/examples/bpf/augmented_raw_syscalls.c
6652620b7e3SArnaldo Carvalho de Melo 	 */
6662620b7e3SArnaldo Carvalho de Melo 	if (group[0] == '!')
6672620b7e3SArnaldo Carvalho de Melo 		return 0;
6684edf30e3SWang Nan 
6694edf30e3SWang Nan 	pr_debug("add bpf event %s:%s and attach bpf program %d\n",
670cd102d70SWang Nan 		 group, event, fd);
6714edf30e3SWang Nan 
6725d369a75SArnaldo Carvalho de Melo 	err = parse_events_add_tracepoint(&new_evsels, &parse_state->idx, group,
6735d369a75SArnaldo Carvalho de Melo 					  event, parse_state->error,
67495088a59SWang Nan 					  param->head_config);
6754edf30e3SWang Nan 	if (err) {
67632dcd021SJiri Olsa 		struct evsel *evsel, *tmp;
6774edf30e3SWang Nan 
6784edf30e3SWang Nan 		pr_debug("Failed to add BPF event %s:%s\n",
679cd102d70SWang Nan 			 group, event);
680b27c4eceSJiri Olsa 		list_for_each_entry_safe(evsel, tmp, &new_evsels, core.node) {
681b27c4eceSJiri Olsa 			list_del_init(&evsel->core.node);
6825eb2dd2aSJiri Olsa 			evsel__delete(evsel);
6834edf30e3SWang Nan 		}
6844edf30e3SWang Nan 		return err;
6854edf30e3SWang Nan 	}
686cd102d70SWang Nan 	pr_debug("adding %s:%s\n", group, event);
6874edf30e3SWang Nan 
688b27c4eceSJiri Olsa 	list_for_each_entry(pos, &new_evsels, core.node) {
6891f45b1d4SWang Nan 		pr_debug("adding %s:%s to %p\n",
690cd102d70SWang Nan 			 group, event, pos);
6911f45b1d4SWang Nan 		pos->bpf_fd = fd;
692af4a0991SArnaldo Carvalho de Melo 		pos->bpf_obj = obj;
6931f45b1d4SWang Nan 	}
6944edf30e3SWang Nan 	list_splice(&new_evsels, list);
6954edf30e3SWang Nan 	return 0;
6964edf30e3SWang Nan }
6974edf30e3SWang Nan 
6985d9cdc11SArnaldo Carvalho de Melo int parse_events_load_bpf_obj(struct parse_events_state *parse_state,
69984c86ca1SWang Nan 			      struct list_head *list,
70095088a59SWang Nan 			      struct bpf_object *obj,
70195088a59SWang Nan 			      struct list_head *head_config)
70284c86ca1SWang Nan {
70384c86ca1SWang Nan 	int err;
70484c86ca1SWang Nan 	char errbuf[BUFSIZ];
7055d9cdc11SArnaldo Carvalho de Melo 	struct __add_bpf_event_param param = {parse_state, list, head_config};
706aa3abf30SWang Nan 	static bool registered_unprobe_atexit = false;
70784c86ca1SWang Nan 
70884c86ca1SWang Nan 	if (IS_ERR(obj) || !obj) {
70984c86ca1SWang Nan 		snprintf(errbuf, sizeof(errbuf),
71084c86ca1SWang Nan 			 "Internal error: load bpf obj with NULL");
71184c86ca1SWang Nan 		err = -EINVAL;
71284c86ca1SWang Nan 		goto errout;
71384c86ca1SWang Nan 	}
71484c86ca1SWang Nan 
71584c86ca1SWang Nan 	/*
716aa3abf30SWang Nan 	 * Register atexit handler before calling bpf__probe() so
717aa3abf30SWang Nan 	 * bpf__probe() don't need to unprobe probe points its already
718aa3abf30SWang Nan 	 * created when failure.
719aa3abf30SWang Nan 	 */
720aa3abf30SWang Nan 	if (!registered_unprobe_atexit) {
721aa3abf30SWang Nan 		atexit(bpf__clear);
722aa3abf30SWang Nan 		registered_unprobe_atexit = true;
723aa3abf30SWang Nan 	}
724aa3abf30SWang Nan 
725aa3abf30SWang Nan 	err = bpf__probe(obj);
726aa3abf30SWang Nan 	if (err) {
727aa3abf30SWang Nan 		bpf__strerror_probe(obj, err, errbuf, sizeof(errbuf));
728aa3abf30SWang Nan 		goto errout;
729aa3abf30SWang Nan 	}
730aa3abf30SWang Nan 
7311e5e3ee8SWang Nan 	err = bpf__load(obj);
7321e5e3ee8SWang Nan 	if (err) {
7331e5e3ee8SWang Nan 		bpf__strerror_load(obj, err, errbuf, sizeof(errbuf));
7341e5e3ee8SWang Nan 		goto errout;
7351e5e3ee8SWang Nan 	}
7361e5e3ee8SWang Nan 
737cd102d70SWang Nan 	err = bpf__foreach_event(obj, add_bpf_event, &param);
7384edf30e3SWang Nan 	if (err) {
7394edf30e3SWang Nan 		snprintf(errbuf, sizeof(errbuf),
7404edf30e3SWang Nan 			 "Attach events in BPF object failed");
7414edf30e3SWang Nan 		goto errout;
7424edf30e3SWang Nan 	}
7434edf30e3SWang Nan 
7444edf30e3SWang Nan 	return 0;
74584c86ca1SWang Nan errout:
7466c191289SIan Rogers 	parse_events_error__handle(parse_state->error, 0,
7475f634c8eSIan Rogers 				strdup(errbuf), strdup("(add -v to see detail)"));
74884c86ca1SWang Nan 	return err;
74984c86ca1SWang Nan }
75084c86ca1SWang Nan 
751a34f3be7SWang Nan static int
7525d9cdc11SArnaldo Carvalho de Melo parse_events_config_bpf(struct parse_events_state *parse_state,
753a34f3be7SWang Nan 			struct bpf_object *obj,
754a34f3be7SWang Nan 			struct list_head *head_config)
755a34f3be7SWang Nan {
756a34f3be7SWang Nan 	struct parse_events_term *term;
757a34f3be7SWang Nan 	int error_pos;
758a34f3be7SWang Nan 
759a34f3be7SWang Nan 	if (!head_config || list_empty(head_config))
760a34f3be7SWang Nan 		return 0;
761a34f3be7SWang Nan 
762a34f3be7SWang Nan 	list_for_each_entry(term, head_config, list) {
763a34f3be7SWang Nan 		int err;
764a34f3be7SWang Nan 
765a34f3be7SWang Nan 		if (term->type_term != PARSE_EVENTS__TERM_TYPE_USER) {
7666c191289SIan Rogers 			parse_events_error__handle(parse_state->error, term->err_term,
7675f634c8eSIan Rogers 						strdup("Invalid config term for BPF object"),
7685f634c8eSIan Rogers 						NULL);
769a34f3be7SWang Nan 			return -EINVAL;
770a34f3be7SWang Nan 		}
771a34f3be7SWang Nan 
7725d9cdc11SArnaldo Carvalho de Melo 		err = bpf__config_obj(obj, term, parse_state->evlist, &error_pos);
773a34f3be7SWang Nan 		if (err) {
7745f634c8eSIan Rogers 			char errbuf[BUFSIZ];
7755f634c8eSIan Rogers 			int idx;
7765f634c8eSIan Rogers 
7775d9cdc11SArnaldo Carvalho de Melo 			bpf__strerror_config_obj(obj, term, parse_state->evlist,
778a34f3be7SWang Nan 						 &error_pos, err, errbuf,
779a34f3be7SWang Nan 						 sizeof(errbuf));
7805f634c8eSIan Rogers 
7815f634c8eSIan Rogers 			if (err == -BPF_LOADER_ERRNO__OBJCONF_MAP_VALUE)
7825f634c8eSIan Rogers 				idx = term->err_val;
7835f634c8eSIan Rogers 			else
7845f634c8eSIan Rogers 				idx = term->err_term + error_pos;
7855f634c8eSIan Rogers 
7866c191289SIan Rogers 			parse_events_error__handle(parse_state->error, idx,
7875f634c8eSIan Rogers 						strdup(errbuf),
7885f634c8eSIan Rogers 						strdup(
7897630b3e2SWang Nan "Hint:\tValid config terms:\n"
790e571e029SWang Nan "     \tmap:[<arraymap>].value<indices>=[value]\n"
791e571e029SWang Nan "     \tmap:[<eventmap>].event<indices>=[event]\n"
7927630b3e2SWang Nan "\n"
793e571e029SWang Nan "     \twhere <indices> is something like [0,3...5] or [all]\n"
7945f634c8eSIan Rogers "     \t(add -v to see detail)"));
795a34f3be7SWang Nan 			return err;
796a34f3be7SWang Nan 		}
797a34f3be7SWang Nan 	}
798a34f3be7SWang Nan 	return 0;
799a34f3be7SWang Nan }
800a34f3be7SWang Nan 
80195088a59SWang Nan /*
80295088a59SWang Nan  * Split config terms:
80395088a59SWang Nan  * perf record -e bpf.c/call-graph=fp,map:array.value[0]=1/ ...
80495088a59SWang Nan  *  'call-graph=fp' is 'evt config', should be applied to each
80595088a59SWang Nan  *  events in bpf.c.
80695088a59SWang Nan  * 'map:array.value[0]=1' is 'obj config', should be processed
80795088a59SWang Nan  * with parse_events_config_bpf.
80895088a59SWang Nan  *
80995088a59SWang Nan  * Move object config terms from the first list to obj_head_config.
81095088a59SWang Nan  */
81195088a59SWang Nan static void
81295088a59SWang Nan split_bpf_config_terms(struct list_head *evt_head_config,
81395088a59SWang Nan 		       struct list_head *obj_head_config)
81495088a59SWang Nan {
81595088a59SWang Nan 	struct parse_events_term *term, *temp;
81695088a59SWang Nan 
81795088a59SWang Nan 	/*
8184d39c89fSIngo Molnar 	 * Currently, all possible user config term
81995088a59SWang Nan 	 * belong to bpf object. parse_events__is_hardcoded_term()
8204d39c89fSIngo Molnar 	 * happens to be a good flag.
82195088a59SWang Nan 	 *
82295088a59SWang Nan 	 * See parse_events_config_bpf() and
82395088a59SWang Nan 	 * config_term_tracepoint().
82495088a59SWang Nan 	 */
82595088a59SWang Nan 	list_for_each_entry_safe(term, temp, evt_head_config, list)
82695088a59SWang Nan 		if (!parse_events__is_hardcoded_term(term))
82795088a59SWang Nan 			list_move_tail(&term->list, obj_head_config);
82895088a59SWang Nan }
82995088a59SWang Nan 
8305d9cdc11SArnaldo Carvalho de Melo int parse_events_load_bpf(struct parse_events_state *parse_state,
83184c86ca1SWang Nan 			  struct list_head *list,
832d509db04SWang Nan 			  char *bpf_file_name,
833a34f3be7SWang Nan 			  bool source,
834a34f3be7SWang Nan 			  struct list_head *head_config)
83584c86ca1SWang Nan {
836a34f3be7SWang Nan 	int err;
83795088a59SWang Nan 	struct bpf_object *obj;
83895088a59SWang Nan 	LIST_HEAD(obj_head_config);
83995088a59SWang Nan 
84095088a59SWang Nan 	if (head_config)
84195088a59SWang Nan 		split_bpf_config_terms(head_config, &obj_head_config);
84284c86ca1SWang Nan 
843d509db04SWang Nan 	obj = bpf__prepare_load(bpf_file_name, source);
8446371ca3bSWang Nan 	if (IS_ERR(obj)) {
84584c86ca1SWang Nan 		char errbuf[BUFSIZ];
84684c86ca1SWang Nan 
8476371ca3bSWang Nan 		err = PTR_ERR(obj);
84884c86ca1SWang Nan 
84984c86ca1SWang Nan 		if (err == -ENOTSUP)
85084c86ca1SWang Nan 			snprintf(errbuf, sizeof(errbuf),
85184c86ca1SWang Nan 				 "BPF support is not compiled");
85284c86ca1SWang Nan 		else
853d3e0ce39SWang Nan 			bpf__strerror_prepare_load(bpf_file_name,
854d3e0ce39SWang Nan 						   source,
855d3e0ce39SWang Nan 						   -err, errbuf,
856d3e0ce39SWang Nan 						   sizeof(errbuf));
85784c86ca1SWang Nan 
8586c191289SIan Rogers 		parse_events_error__handle(parse_state->error, 0,
8595f634c8eSIan Rogers 					strdup(errbuf), strdup("(add -v to see detail)"));
86084c86ca1SWang Nan 		return err;
86184c86ca1SWang Nan 	}
86284c86ca1SWang Nan 
8635d9cdc11SArnaldo Carvalho de Melo 	err = parse_events_load_bpf_obj(parse_state, list, obj, head_config);
864a34f3be7SWang Nan 	if (err)
865a34f3be7SWang Nan 		return err;
8665d9cdc11SArnaldo Carvalho de Melo 	err = parse_events_config_bpf(parse_state, obj, &obj_head_config);
86795088a59SWang Nan 
86895088a59SWang Nan 	/*
86995088a59SWang Nan 	 * Caller doesn't know anything about obj_head_config,
8704d39c89fSIngo Molnar 	 * so combine them together again before returning.
87195088a59SWang Nan 	 */
87295088a59SWang Nan 	if (head_config)
87395088a59SWang Nan 		list_splice_tail(&obj_head_config, head_config);
87495088a59SWang Nan 	return err;
87584c86ca1SWang Nan }
876c18cf78dSArnaldo Carvalho de Melo #else // HAVE_LIBBPF_SUPPORT
877c18cf78dSArnaldo Carvalho de Melo int parse_events_load_bpf_obj(struct parse_events_state *parse_state,
878c18cf78dSArnaldo Carvalho de Melo 			      struct list_head *list __maybe_unused,
879c18cf78dSArnaldo Carvalho de Melo 			      struct bpf_object *obj __maybe_unused,
880c18cf78dSArnaldo Carvalho de Melo 			      struct list_head *head_config __maybe_unused)
881c18cf78dSArnaldo Carvalho de Melo {
8826c191289SIan Rogers 	parse_events_error__handle(parse_state->error, 0,
883c18cf78dSArnaldo Carvalho de Melo 				   strdup("BPF support is not compiled"),
884c18cf78dSArnaldo Carvalho de Melo 				   strdup("Make sure libbpf-devel is available at build time."));
885c18cf78dSArnaldo Carvalho de Melo 	return -ENOTSUP;
886c18cf78dSArnaldo Carvalho de Melo }
887c18cf78dSArnaldo Carvalho de Melo 
888c18cf78dSArnaldo Carvalho de Melo int parse_events_load_bpf(struct parse_events_state *parse_state,
889c18cf78dSArnaldo Carvalho de Melo 			  struct list_head *list __maybe_unused,
890c18cf78dSArnaldo Carvalho de Melo 			  char *bpf_file_name __maybe_unused,
891c18cf78dSArnaldo Carvalho de Melo 			  bool source __maybe_unused,
892c18cf78dSArnaldo Carvalho de Melo 			  struct list_head *head_config __maybe_unused)
893c18cf78dSArnaldo Carvalho de Melo {
8946c191289SIan Rogers 	parse_events_error__handle(parse_state->error, 0,
895c18cf78dSArnaldo Carvalho de Melo 				   strdup("BPF support is not compiled"),
896c18cf78dSArnaldo Carvalho de Melo 				   strdup("Make sure libbpf-devel is available at build time."));
897c18cf78dSArnaldo Carvalho de Melo 	return -ENOTSUP;
898c18cf78dSArnaldo Carvalho de Melo }
899c18cf78dSArnaldo Carvalho de Melo #endif // HAVE_LIBBPF_SUPPORT
90084c86ca1SWang Nan 
90189812fc8SJiri Olsa static int
90289812fc8SJiri Olsa parse_breakpoint_type(const char *type, struct perf_event_attr *attr)
9031b290d67SFrederic Weisbecker {
9041b290d67SFrederic Weisbecker 	int i;
9051b290d67SFrederic Weisbecker 
9061b290d67SFrederic Weisbecker 	for (i = 0; i < 3; i++) {
90789812fc8SJiri Olsa 		if (!type || !type[i])
9081b290d67SFrederic Weisbecker 			break;
9091b290d67SFrederic Weisbecker 
9107582732fSJiri Olsa #define CHECK_SET_TYPE(bit)		\
9117582732fSJiri Olsa do {					\
9127582732fSJiri Olsa 	if (attr->bp_type & bit)	\
9137582732fSJiri Olsa 		return -EINVAL;		\
9147582732fSJiri Olsa 	else				\
9157582732fSJiri Olsa 		attr->bp_type |= bit;	\
9167582732fSJiri Olsa } while (0)
9177582732fSJiri Olsa 
9181b290d67SFrederic Weisbecker 		switch (type[i]) {
9191b290d67SFrederic Weisbecker 		case 'r':
9207582732fSJiri Olsa 			CHECK_SET_TYPE(HW_BREAKPOINT_R);
9211b290d67SFrederic Weisbecker 			break;
9221b290d67SFrederic Weisbecker 		case 'w':
9237582732fSJiri Olsa 			CHECK_SET_TYPE(HW_BREAKPOINT_W);
9241b290d67SFrederic Weisbecker 			break;
9251b290d67SFrederic Weisbecker 		case 'x':
9267582732fSJiri Olsa 			CHECK_SET_TYPE(HW_BREAKPOINT_X);
9271b290d67SFrederic Weisbecker 			break;
9281b290d67SFrederic Weisbecker 		default:
92989812fc8SJiri Olsa 			return -EINVAL;
9301b290d67SFrederic Weisbecker 		}
9311b290d67SFrederic Weisbecker 	}
93289812fc8SJiri Olsa 
9337582732fSJiri Olsa #undef CHECK_SET_TYPE
9347582732fSJiri Olsa 
9351b290d67SFrederic Weisbecker 	if (!attr->bp_type) /* Default */
9361b290d67SFrederic Weisbecker 		attr->bp_type = HW_BREAKPOINT_R | HW_BREAKPOINT_W;
9371b290d67SFrederic Weisbecker 
93889812fc8SJiri Olsa 	return 0;
9391b290d67SFrederic Weisbecker }
9401b290d67SFrederic Weisbecker 
941c5cd8ac0SDavid Ahern int parse_events_add_breakpoint(struct list_head *list, int *idx,
942aa98d848SIan Rogers 				u64 addr, char *type, u64 len)
9431b290d67SFrederic Weisbecker {
94489812fc8SJiri Olsa 	struct perf_event_attr attr;
9451b290d67SFrederic Weisbecker 
94689812fc8SJiri Olsa 	memset(&attr, 0, sizeof(attr));
947aa98d848SIan Rogers 	attr.bp_addr = addr;
9481b290d67SFrederic Weisbecker 
94989812fc8SJiri Olsa 	if (parse_breakpoint_type(type, &attr))
95089812fc8SJiri Olsa 		return -EINVAL;
9511b290d67SFrederic Weisbecker 
9523741eb9fSJacob Shin 	/* Provide some defaults if len is not specified */
9533741eb9fSJacob Shin 	if (!len) {
95489812fc8SJiri Olsa 		if (attr.bp_type == HW_BREAKPOINT_X)
9553741eb9fSJacob Shin 			len = sizeof(long);
956aa59a485SFrederic Weisbecker 		else
9573741eb9fSJacob Shin 			len = HW_BREAKPOINT_LEN_4;
9583741eb9fSJacob Shin 	}
9593741eb9fSJacob Shin 
9603741eb9fSJacob Shin 	attr.bp_len = len;
961aa59a485SFrederic Weisbecker 
96289812fc8SJiri Olsa 	attr.type = PERF_TYPE_BREAKPOINT;
9634a841d65SJovi Zhang 	attr.sample_period = 1;
9641b290d67SFrederic Weisbecker 
9652b62b3a6SIan Rogers 	return add_event(list, idx, &attr, /*name=*/NULL, /*mertic_id=*/NULL,
9662b62b3a6SIan Rogers 			 /*config_terms=*/NULL);
9671b290d67SFrederic Weisbecker }
9681b290d67SFrederic Weisbecker 
9693b0e371cSJiri Olsa static int check_type_val(struct parse_events_term *term,
9703b0e371cSJiri Olsa 			  struct parse_events_error *err,
9713b0e371cSJiri Olsa 			  int type)
9723b0e371cSJiri Olsa {
9733b0e371cSJiri Olsa 	if (type == term->type_val)
9743b0e371cSJiri Olsa 		return 0;
9753b0e371cSJiri Olsa 
9763b0e371cSJiri Olsa 	if (err) {
9776c191289SIan Rogers 		parse_events_error__handle(err, term->err_val,
978448d732cSIan Rogers 					type == PARSE_EVENTS__TERM_TYPE_NUM
979448d732cSIan Rogers 					? strdup("expected numeric value")
980448d732cSIan Rogers 					: strdup("expected string value"),
981448d732cSIan Rogers 					NULL);
9823b0e371cSJiri Olsa 	}
9833b0e371cSJiri Olsa 	return -EINVAL;
9843b0e371cSJiri Olsa }
9853b0e371cSJiri Olsa 
98617cb5f84SWang Nan /*
98717cb5f84SWang Nan  * Update according to parse-events.l
98817cb5f84SWang Nan  */
98917cb5f84SWang Nan static const char *config_term_names[__PARSE_EVENTS__TERM_TYPE_NR] = {
99017cb5f84SWang Nan 	[PARSE_EVENTS__TERM_TYPE_USER]			= "<sysfs term>",
99117cb5f84SWang Nan 	[PARSE_EVENTS__TERM_TYPE_CONFIG]		= "config",
99217cb5f84SWang Nan 	[PARSE_EVENTS__TERM_TYPE_CONFIG1]		= "config1",
99317cb5f84SWang Nan 	[PARSE_EVENTS__TERM_TYPE_CONFIG2]		= "config2",
994204e7c49SRob Herring 	[PARSE_EVENTS__TERM_TYPE_CONFIG3]		= "config3",
99517cb5f84SWang Nan 	[PARSE_EVENTS__TERM_TYPE_NAME]			= "name",
99617cb5f84SWang Nan 	[PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD]		= "period",
99717cb5f84SWang Nan 	[PARSE_EVENTS__TERM_TYPE_SAMPLE_FREQ]		= "freq",
99817cb5f84SWang Nan 	[PARSE_EVENTS__TERM_TYPE_BRANCH_SAMPLE_TYPE]	= "branch_type",
99917cb5f84SWang Nan 	[PARSE_EVENTS__TERM_TYPE_TIME]			= "time",
100017cb5f84SWang Nan 	[PARSE_EVENTS__TERM_TYPE_CALLGRAPH]		= "call-graph",
100117cb5f84SWang Nan 	[PARSE_EVENTS__TERM_TYPE_STACKSIZE]		= "stack-size",
100217cb5f84SWang Nan 	[PARSE_EVENTS__TERM_TYPE_NOINHERIT]		= "no-inherit",
100317cb5f84SWang Nan 	[PARSE_EVENTS__TERM_TYPE_INHERIT]		= "inherit",
1004792d48b4SArnaldo Carvalho de Melo 	[PARSE_EVENTS__TERM_TYPE_MAX_STACK]		= "max-stack",
10052fda5adaSArnaldo Carvalho de Melo 	[PARSE_EVENTS__TERM_TYPE_MAX_EVENTS]		= "nr",
1006626a6b78SWang Nan 	[PARSE_EVENTS__TERM_TYPE_OVERWRITE]		= "overwrite",
1007626a6b78SWang Nan 	[PARSE_EVENTS__TERM_TYPE_NOOVERWRITE]		= "no-overwrite",
1008dd60fba7SMathieu Poirier 	[PARSE_EVENTS__TERM_TYPE_DRV_CFG]		= "driver-config",
1009064b4e82SJin Yao 	[PARSE_EVENTS__TERM_TYPE_PERCORE]		= "percore",
10101b992154SAdrian Hunter 	[PARSE_EVENTS__TERM_TYPE_AUX_OUTPUT]		= "aux-output",
1011eb7a52d4SAdrian Hunter 	[PARSE_EVENTS__TERM_TYPE_AUX_SAMPLE_SIZE]	= "aux-sample-size",
10122b62b3a6SIan Rogers 	[PARSE_EVENTS__TERM_TYPE_METRIC_ID]		= "metric-id",
101370c90e4aSIan Rogers 	[PARSE_EVENTS__TERM_TYPE_RAW]                   = "raw",
10146fd1e519SIan Rogers 	[PARSE_EVENTS__TERM_TYPE_LEGACY_CACHE]          = "legacy-cache",
10155ea8f2ccSIan Rogers 	[PARSE_EVENTS__TERM_TYPE_HARDWARE]              = "hardware",
101617cb5f84SWang Nan };
101717cb5f84SWang Nan 
10181669e509SWang Nan static bool config_term_shrinked;
10191669e509SWang Nan 
10201669e509SWang Nan static bool
10211669e509SWang Nan config_term_avail(int term_type, struct parse_events_error *err)
10221669e509SWang Nan {
1023448d732cSIan Rogers 	char *err_str;
1024448d732cSIan Rogers 
10251669e509SWang Nan 	if (term_type < 0 || term_type >= __PARSE_EVENTS__TERM_TYPE_NR) {
10266c191289SIan Rogers 		parse_events_error__handle(err, -1,
1027448d732cSIan Rogers 					strdup("Invalid term_type"), NULL);
10281669e509SWang Nan 		return false;
10291669e509SWang Nan 	}
10301669e509SWang Nan 	if (!config_term_shrinked)
10311669e509SWang Nan 		return true;
10321669e509SWang Nan 
10331669e509SWang Nan 	switch (term_type) {
10341669e509SWang Nan 	case PARSE_EVENTS__TERM_TYPE_CONFIG:
10351669e509SWang Nan 	case PARSE_EVENTS__TERM_TYPE_CONFIG1:
10361669e509SWang Nan 	case PARSE_EVENTS__TERM_TYPE_CONFIG2:
1037204e7c49SRob Herring 	case PARSE_EVENTS__TERM_TYPE_CONFIG3:
10381669e509SWang Nan 	case PARSE_EVENTS__TERM_TYPE_NAME:
10392b62b3a6SIan Rogers 	case PARSE_EVENTS__TERM_TYPE_METRIC_ID:
104006835545SSukadev Bhattiprolu 	case PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD:
1041064b4e82SJin Yao 	case PARSE_EVENTS__TERM_TYPE_PERCORE:
10421669e509SWang Nan 		return true;
10431669e509SWang Nan 	default:
10441669e509SWang Nan 		if (!err)
10451669e509SWang Nan 			return false;
10461669e509SWang Nan 
10471669e509SWang Nan 		/* term_type is validated so indexing is safe */
1048448d732cSIan Rogers 		if (asprintf(&err_str, "'%s' is not usable in 'perf stat'",
1049448d732cSIan Rogers 				config_term_names[term_type]) >= 0)
10506c191289SIan Rogers 			parse_events_error__handle(err, -1, err_str, NULL);
10511669e509SWang Nan 		return false;
10521669e509SWang Nan 	}
10531669e509SWang Nan }
10541669e509SWang Nan 
10551669e509SWang Nan void parse_events__shrink_config_terms(void)
10561669e509SWang Nan {
10571669e509SWang Nan 	config_term_shrinked = true;
10581669e509SWang Nan }
10591669e509SWang Nan 
10600b8891a8SHe Kuang static int config_term_common(struct perf_event_attr *attr,
10613b0e371cSJiri Olsa 			      struct parse_events_term *term,
10623b0e371cSJiri Olsa 			      struct parse_events_error *err)
10638f707d84SJiri Olsa {
106416fa7e82SJiri Olsa #define CHECK_TYPE_VAL(type)						   \
106516fa7e82SJiri Olsa do {									   \
10663b0e371cSJiri Olsa 	if (check_type_val(term, err, PARSE_EVENTS__TERM_TYPE_ ## type)) \
106716fa7e82SJiri Olsa 		return -EINVAL;						   \
106816fa7e82SJiri Olsa } while (0)
106916fa7e82SJiri Olsa 
107016fa7e82SJiri Olsa 	switch (term->type_term) {
10718f707d84SJiri Olsa 	case PARSE_EVENTS__TERM_TYPE_CONFIG:
107216fa7e82SJiri Olsa 		CHECK_TYPE_VAL(NUM);
10738f707d84SJiri Olsa 		attr->config = term->val.num;
10748f707d84SJiri Olsa 		break;
10758f707d84SJiri Olsa 	case PARSE_EVENTS__TERM_TYPE_CONFIG1:
107616fa7e82SJiri Olsa 		CHECK_TYPE_VAL(NUM);
10778f707d84SJiri Olsa 		attr->config1 = term->val.num;
10788f707d84SJiri Olsa 		break;
10798f707d84SJiri Olsa 	case PARSE_EVENTS__TERM_TYPE_CONFIG2:
108016fa7e82SJiri Olsa 		CHECK_TYPE_VAL(NUM);
10818f707d84SJiri Olsa 		attr->config2 = term->val.num;
10828f707d84SJiri Olsa 		break;
1083204e7c49SRob Herring 	case PARSE_EVENTS__TERM_TYPE_CONFIG3:
1084204e7c49SRob Herring 		CHECK_TYPE_VAL(NUM);
1085204e7c49SRob Herring 		attr->config3 = term->val.num;
1086204e7c49SRob Herring 		break;
10878f707d84SJiri Olsa 	case PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD:
108816fa7e82SJiri Olsa 		CHECK_TYPE_VAL(NUM);
10898f707d84SJiri Olsa 		break;
109009af2a55SNamhyung Kim 	case PARSE_EVENTS__TERM_TYPE_SAMPLE_FREQ:
109109af2a55SNamhyung Kim 		CHECK_TYPE_VAL(NUM);
109209af2a55SNamhyung Kim 		break;
10938f707d84SJiri Olsa 	case PARSE_EVENTS__TERM_TYPE_BRANCH_SAMPLE_TYPE:
1094ac12f676SAndi Kleen 		CHECK_TYPE_VAL(STR);
1095ac12f676SAndi Kleen 		if (strcmp(term->val.str, "no") &&
1096448d732cSIan Rogers 		    parse_branch_str(term->val.str,
1097448d732cSIan Rogers 				    &attr->branch_sample_type)) {
10986c191289SIan Rogers 			parse_events_error__handle(err, term->err_val,
1099448d732cSIan Rogers 					strdup("invalid branch sample type"),
1100448d732cSIan Rogers 					NULL);
1101ac12f676SAndi Kleen 			return -EINVAL;
1102ac12f676SAndi Kleen 		}
11038f707d84SJiri Olsa 		break;
110432067712SKan Liang 	case PARSE_EVENTS__TERM_TYPE_TIME:
110532067712SKan Liang 		CHECK_TYPE_VAL(NUM);
110632067712SKan Liang 		if (term->val.num > 1) {
11076c191289SIan Rogers 			parse_events_error__handle(err, term->err_val,
1108448d732cSIan Rogers 						strdup("expected 0 or 1"),
1109448d732cSIan Rogers 						NULL);
111032067712SKan Liang 			return -EINVAL;
111132067712SKan Liang 		}
111232067712SKan Liang 		break;
1113d457c963SKan Liang 	case PARSE_EVENTS__TERM_TYPE_CALLGRAPH:
1114d457c963SKan Liang 		CHECK_TYPE_VAL(STR);
1115d457c963SKan Liang 		break;
1116d457c963SKan Liang 	case PARSE_EVENTS__TERM_TYPE_STACKSIZE:
1117d457c963SKan Liang 		CHECK_TYPE_VAL(NUM);
1118d457c963SKan Liang 		break;
1119374ce938SWang Nan 	case PARSE_EVENTS__TERM_TYPE_INHERIT:
1120374ce938SWang Nan 		CHECK_TYPE_VAL(NUM);
1121374ce938SWang Nan 		break;
1122374ce938SWang Nan 	case PARSE_EVENTS__TERM_TYPE_NOINHERIT:
1123374ce938SWang Nan 		CHECK_TYPE_VAL(NUM);
1124374ce938SWang Nan 		break;
1125626a6b78SWang Nan 	case PARSE_EVENTS__TERM_TYPE_OVERWRITE:
1126626a6b78SWang Nan 		CHECK_TYPE_VAL(NUM);
1127626a6b78SWang Nan 		break;
1128626a6b78SWang Nan 	case PARSE_EVENTS__TERM_TYPE_NOOVERWRITE:
1129626a6b78SWang Nan 		CHECK_TYPE_VAL(NUM);
1130626a6b78SWang Nan 		break;
11316b5fc39bSJiri Olsa 	case PARSE_EVENTS__TERM_TYPE_NAME:
11326b5fc39bSJiri Olsa 		CHECK_TYPE_VAL(STR);
11336b5fc39bSJiri Olsa 		break;
11342b62b3a6SIan Rogers 	case PARSE_EVENTS__TERM_TYPE_METRIC_ID:
11352b62b3a6SIan Rogers 		CHECK_TYPE_VAL(STR);
11362b62b3a6SIan Rogers 		break;
113770c90e4aSIan Rogers 	case PARSE_EVENTS__TERM_TYPE_RAW:
113870c90e4aSIan Rogers 		CHECK_TYPE_VAL(STR);
113970c90e4aSIan Rogers 		break;
1140792d48b4SArnaldo Carvalho de Melo 	case PARSE_EVENTS__TERM_TYPE_MAX_STACK:
1141792d48b4SArnaldo Carvalho de Melo 		CHECK_TYPE_VAL(NUM);
1142792d48b4SArnaldo Carvalho de Melo 		break;
11432fda5adaSArnaldo Carvalho de Melo 	case PARSE_EVENTS__TERM_TYPE_MAX_EVENTS:
11442fda5adaSArnaldo Carvalho de Melo 		CHECK_TYPE_VAL(NUM);
11452fda5adaSArnaldo Carvalho de Melo 		break;
1146064b4e82SJin Yao 	case PARSE_EVENTS__TERM_TYPE_PERCORE:
1147064b4e82SJin Yao 		CHECK_TYPE_VAL(NUM);
1148064b4e82SJin Yao 		if ((unsigned int)term->val.num > 1) {
11496c191289SIan Rogers 			parse_events_error__handle(err, term->err_val,
1150448d732cSIan Rogers 						strdup("expected 0 or 1"),
1151448d732cSIan Rogers 						NULL);
1152064b4e82SJin Yao 			return -EINVAL;
1153064b4e82SJin Yao 		}
1154064b4e82SJin Yao 		break;
11551b992154SAdrian Hunter 	case PARSE_EVENTS__TERM_TYPE_AUX_OUTPUT:
11561b992154SAdrian Hunter 		CHECK_TYPE_VAL(NUM);
11571b992154SAdrian Hunter 		break;
1158eb7a52d4SAdrian Hunter 	case PARSE_EVENTS__TERM_TYPE_AUX_SAMPLE_SIZE:
1159eb7a52d4SAdrian Hunter 		CHECK_TYPE_VAL(NUM);
1160eb7a52d4SAdrian Hunter 		if (term->val.num > UINT_MAX) {
11616c191289SIan Rogers 			parse_events_error__handle(err, term->err_val,
1162eb7a52d4SAdrian Hunter 						strdup("too big"),
1163eb7a52d4SAdrian Hunter 						NULL);
1164eb7a52d4SAdrian Hunter 			return -EINVAL;
1165eb7a52d4SAdrian Hunter 		}
1166eb7a52d4SAdrian Hunter 		break;
11678f707d84SJiri Olsa 	default:
11686c191289SIan Rogers 		parse_events_error__handle(err, term->err_term,
1169448d732cSIan Rogers 				strdup("unknown term"),
1170448d732cSIan Rogers 				parse_events_formats_error_string(NULL));
11718f707d84SJiri Olsa 		return -EINVAL;
11728f707d84SJiri Olsa 	}
117316fa7e82SJiri Olsa 
11741669e509SWang Nan 	/*
11754d39c89fSIngo Molnar 	 * Check term availability after basic checking so
11761669e509SWang Nan 	 * PARSE_EVENTS__TERM_TYPE_USER can be found and filtered.
11771669e509SWang Nan 	 *
11784d39c89fSIngo Molnar 	 * If check availability at the entry of this function,
11791669e509SWang Nan 	 * user will see "'<sysfs term>' is not usable in 'perf stat'"
11801669e509SWang Nan 	 * if an invalid config term is provided for legacy events
11811669e509SWang Nan 	 * (for example, instructions/badterm/...), which is confusing.
11821669e509SWang Nan 	 */
11831669e509SWang Nan 	if (!config_term_avail(term->type_term, err))
11841669e509SWang Nan 		return -EINVAL;
11858f707d84SJiri Olsa 	return 0;
118616fa7e82SJiri Olsa #undef CHECK_TYPE_VAL
11878f707d84SJiri Olsa }
11888f707d84SJiri Olsa 
11890b8891a8SHe Kuang static int config_term_pmu(struct perf_event_attr *attr,
11900b8891a8SHe Kuang 			   struct parse_events_term *term,
11910b8891a8SHe Kuang 			   struct parse_events_error *err)
11920b8891a8SHe Kuang {
11936fd1e519SIan Rogers 	if (term->type_term == PARSE_EVENTS__TERM_TYPE_LEGACY_CACHE) {
11941eaf496eSIan Rogers 		const struct perf_pmu *pmu = perf_pmus__find_by_type(attr->type);
11956fd1e519SIan Rogers 
11966fd1e519SIan Rogers 		if (perf_pmu__supports_legacy_cache(pmu)) {
11976fd1e519SIan Rogers 			attr->type = PERF_TYPE_HW_CACHE;
11986fd1e519SIan Rogers 			return parse_events__decode_legacy_cache(term->config, pmu->type,
11996fd1e519SIan Rogers 								 &attr->config);
12006fd1e519SIan Rogers 		} else
12016fd1e519SIan Rogers 			term->type_term = PARSE_EVENTS__TERM_TYPE_USER;
12026fd1e519SIan Rogers 	}
12035ea8f2ccSIan Rogers 	if (term->type_term == PARSE_EVENTS__TERM_TYPE_HARDWARE) {
12041eaf496eSIan Rogers 		const struct perf_pmu *pmu = perf_pmus__find_by_type(attr->type);
12055ea8f2ccSIan Rogers 
12065ea8f2ccSIan Rogers 		if (!pmu) {
12075ea8f2ccSIan Rogers 			pr_debug("Failed to find PMU for type %d", attr->type);
12085ea8f2ccSIan Rogers 			return -EINVAL;
12095ea8f2ccSIan Rogers 		}
12105ea8f2ccSIan Rogers 		attr->type = PERF_TYPE_HARDWARE;
12115ea8f2ccSIan Rogers 		attr->config = ((__u64)pmu->type << PERF_PMU_TYPE_SHIFT) | term->val.num;
12125ea8f2ccSIan Rogers 		return 0;
12135ea8f2ccSIan Rogers 	}
1214dd60fba7SMathieu Poirier 	if (term->type_term == PARSE_EVENTS__TERM_TYPE_USER ||
12156fd1e519SIan Rogers 	    term->type_term == PARSE_EVENTS__TERM_TYPE_DRV_CFG) {
12160b8891a8SHe Kuang 		/*
12170b8891a8SHe Kuang 		 * Always succeed for sysfs terms, as we dont know
12180b8891a8SHe Kuang 		 * at this point what type they need to have.
12190b8891a8SHe Kuang 		 */
12200b8891a8SHe Kuang 		return 0;
12216fd1e519SIan Rogers 	}
12220b8891a8SHe Kuang 	return config_term_common(attr, term, err);
12230b8891a8SHe Kuang }
12240b8891a8SHe Kuang 
1225378ef0f5SIan Rogers #ifdef HAVE_LIBTRACEEVENT
1226e637d177SHe Kuang static int config_term_tracepoint(struct perf_event_attr *attr,
1227e637d177SHe Kuang 				  struct parse_events_term *term,
1228e637d177SHe Kuang 				  struct parse_events_error *err)
1229e637d177SHe Kuang {
1230e637d177SHe Kuang 	switch (term->type_term) {
1231e637d177SHe Kuang 	case PARSE_EVENTS__TERM_TYPE_CALLGRAPH:
1232e637d177SHe Kuang 	case PARSE_EVENTS__TERM_TYPE_STACKSIZE:
1233374ce938SWang Nan 	case PARSE_EVENTS__TERM_TYPE_INHERIT:
1234374ce938SWang Nan 	case PARSE_EVENTS__TERM_TYPE_NOINHERIT:
1235792d48b4SArnaldo Carvalho de Melo 	case PARSE_EVENTS__TERM_TYPE_MAX_STACK:
12362fda5adaSArnaldo Carvalho de Melo 	case PARSE_EVENTS__TERM_TYPE_MAX_EVENTS:
1237626a6b78SWang Nan 	case PARSE_EVENTS__TERM_TYPE_OVERWRITE:
1238626a6b78SWang Nan 	case PARSE_EVENTS__TERM_TYPE_NOOVERWRITE:
12391b992154SAdrian Hunter 	case PARSE_EVENTS__TERM_TYPE_AUX_OUTPUT:
1240eb7a52d4SAdrian Hunter 	case PARSE_EVENTS__TERM_TYPE_AUX_SAMPLE_SIZE:
1241e637d177SHe Kuang 		return config_term_common(attr, term, err);
1242e637d177SHe Kuang 	default:
1243e637d177SHe Kuang 		if (err) {
12446c191289SIan Rogers 			parse_events_error__handle(err, term->err_term,
1245448d732cSIan Rogers 				strdup("unknown term"),
1246448d732cSIan Rogers 				strdup("valid terms: call-graph,stack-size\n"));
1247e637d177SHe Kuang 		}
1248e637d177SHe Kuang 		return -EINVAL;
1249e637d177SHe Kuang 	}
1250e637d177SHe Kuang 
1251e637d177SHe Kuang 	return 0;
1252e637d177SHe Kuang }
1253378ef0f5SIan Rogers #endif
1254e637d177SHe Kuang 
12558f707d84SJiri Olsa static int config_attr(struct perf_event_attr *attr,
12563b0e371cSJiri Olsa 		       struct list_head *head,
12570b8891a8SHe Kuang 		       struct parse_events_error *err,
12580b8891a8SHe Kuang 		       config_term_func_t config_term)
12598f707d84SJiri Olsa {
12606cee6cd3SArnaldo Carvalho de Melo 	struct parse_events_term *term;
12618f707d84SJiri Olsa 
12628f707d84SJiri Olsa 	list_for_each_entry(term, head, list)
12633b0e371cSJiri Olsa 		if (config_term(attr, term, err))
12648f707d84SJiri Olsa 			return -EINVAL;
12658f707d84SJiri Olsa 
12668f707d84SJiri Olsa 	return 0;
12678f707d84SJiri Olsa }
12688f707d84SJiri Olsa 
1269930a2e29SJiri Olsa static int get_config_terms(struct list_head *head_config,
1270930a2e29SJiri Olsa 			    struct list_head *head_terms __maybe_unused)
1271930a2e29SJiri Olsa {
127205e54e23SIan Rogers #define ADD_CONFIG_TERM(__type, __weak)				\
127335ac0cadSArnaldo Carvalho de Melo 	struct evsel_config_term *__t;			\
1274930a2e29SJiri Olsa 								\
1275930a2e29SJiri Olsa 	__t = zalloc(sizeof(*__t));				\
1276930a2e29SJiri Olsa 	if (!__t)						\
1277930a2e29SJiri Olsa 		return -ENOMEM;					\
1278930a2e29SJiri Olsa 								\
1279930a2e29SJiri Olsa 	INIT_LIST_HEAD(&__t->list);				\
128035ac0cadSArnaldo Carvalho de Melo 	__t->type       = EVSEL__CONFIG_TERM_ ## __type;	\
128105e54e23SIan Rogers 	__t->weak	= __weak;				\
1282e884602bSLeo Yan 	list_add_tail(&__t->list, head_terms)
1283e884602bSLeo Yan 
128405e54e23SIan Rogers #define ADD_CONFIG_TERM_VAL(__type, __name, __val, __weak)	\
1285e884602bSLeo Yan do {								\
128605e54e23SIan Rogers 	ADD_CONFIG_TERM(__type, __weak);			\
1287e884602bSLeo Yan 	__t->val.__name = __val;				\
1288e884602bSLeo Yan } while (0)
1289e884602bSLeo Yan 
129005e54e23SIan Rogers #define ADD_CONFIG_TERM_STR(__type, __val, __weak)		\
1291e884602bSLeo Yan do {								\
129205e54e23SIan Rogers 	ADD_CONFIG_TERM(__type, __weak);			\
12933220fb8dSLeo Yan 	__t->val.str = strdup(__val);				\
12943220fb8dSLeo Yan 	if (!__t->val.str) {					\
12953220fb8dSLeo Yan 		zfree(&__t);					\
12963220fb8dSLeo Yan 		return -ENOMEM;					\
12973220fb8dSLeo Yan 	}							\
12983220fb8dSLeo Yan 	__t->free_str = true;					\
1299930a2e29SJiri Olsa } while (0)
1300930a2e29SJiri Olsa 
1301930a2e29SJiri Olsa 	struct parse_events_term *term;
1302930a2e29SJiri Olsa 
1303930a2e29SJiri Olsa 	list_for_each_entry(term, head_config, list) {
1304930a2e29SJiri Olsa 		switch (term->type_term) {
1305ee4c7588SJiri Olsa 		case PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD:
130605e54e23SIan Rogers 			ADD_CONFIG_TERM_VAL(PERIOD, period, term->val.num, term->weak);
130732067712SKan Liang 			break;
130809af2a55SNamhyung Kim 		case PARSE_EVENTS__TERM_TYPE_SAMPLE_FREQ:
130905e54e23SIan Rogers 			ADD_CONFIG_TERM_VAL(FREQ, freq, term->val.num, term->weak);
131009af2a55SNamhyung Kim 			break;
131132067712SKan Liang 		case PARSE_EVENTS__TERM_TYPE_TIME:
131205e54e23SIan Rogers 			ADD_CONFIG_TERM_VAL(TIME, time, term->val.num, term->weak);
131332067712SKan Liang 			break;
1314d457c963SKan Liang 		case PARSE_EVENTS__TERM_TYPE_CALLGRAPH:
131505e54e23SIan Rogers 			ADD_CONFIG_TERM_STR(CALLGRAPH, term->val.str, term->weak);
1316d457c963SKan Liang 			break;
1317ac12f676SAndi Kleen 		case PARSE_EVENTS__TERM_TYPE_BRANCH_SAMPLE_TYPE:
131805e54e23SIan Rogers 			ADD_CONFIG_TERM_STR(BRANCH, term->val.str, term->weak);
1319ac12f676SAndi Kleen 			break;
1320d457c963SKan Liang 		case PARSE_EVENTS__TERM_TYPE_STACKSIZE:
1321e884602bSLeo Yan 			ADD_CONFIG_TERM_VAL(STACK_USER, stack_user,
132205e54e23SIan Rogers 					    term->val.num, term->weak);
1323d457c963SKan Liang 			break;
1324374ce938SWang Nan 		case PARSE_EVENTS__TERM_TYPE_INHERIT:
1325e884602bSLeo Yan 			ADD_CONFIG_TERM_VAL(INHERIT, inherit,
132605e54e23SIan Rogers 					    term->val.num ? 1 : 0, term->weak);
1327374ce938SWang Nan 			break;
1328374ce938SWang Nan 		case PARSE_EVENTS__TERM_TYPE_NOINHERIT:
1329e884602bSLeo Yan 			ADD_CONFIG_TERM_VAL(INHERIT, inherit,
133005e54e23SIan Rogers 					    term->val.num ? 0 : 1, term->weak);
1331374ce938SWang Nan 			break;
1332792d48b4SArnaldo Carvalho de Melo 		case PARSE_EVENTS__TERM_TYPE_MAX_STACK:
1333e884602bSLeo Yan 			ADD_CONFIG_TERM_VAL(MAX_STACK, max_stack,
133405e54e23SIan Rogers 					    term->val.num, term->weak);
1335792d48b4SArnaldo Carvalho de Melo 			break;
13362fda5adaSArnaldo Carvalho de Melo 		case PARSE_EVENTS__TERM_TYPE_MAX_EVENTS:
1337e884602bSLeo Yan 			ADD_CONFIG_TERM_VAL(MAX_EVENTS, max_events,
133805e54e23SIan Rogers 					    term->val.num, term->weak);
13392fda5adaSArnaldo Carvalho de Melo 			break;
1340626a6b78SWang Nan 		case PARSE_EVENTS__TERM_TYPE_OVERWRITE:
1341e884602bSLeo Yan 			ADD_CONFIG_TERM_VAL(OVERWRITE, overwrite,
134205e54e23SIan Rogers 					    term->val.num ? 1 : 0, term->weak);
1343626a6b78SWang Nan 			break;
1344626a6b78SWang Nan 		case PARSE_EVENTS__TERM_TYPE_NOOVERWRITE:
1345e884602bSLeo Yan 			ADD_CONFIG_TERM_VAL(OVERWRITE, overwrite,
134605e54e23SIan Rogers 					    term->val.num ? 0 : 1, term->weak);
1347626a6b78SWang Nan 			break;
1348dd60fba7SMathieu Poirier 		case PARSE_EVENTS__TERM_TYPE_DRV_CFG:
134905e54e23SIan Rogers 			ADD_CONFIG_TERM_STR(DRV_CFG, term->val.str, term->weak);
1350dd60fba7SMathieu Poirier 			break;
1351064b4e82SJin Yao 		case PARSE_EVENTS__TERM_TYPE_PERCORE:
1352e884602bSLeo Yan 			ADD_CONFIG_TERM_VAL(PERCORE, percore,
135305e54e23SIan Rogers 					    term->val.num ? true : false, term->weak);
1354064b4e82SJin Yao 			break;
13551b992154SAdrian Hunter 		case PARSE_EVENTS__TERM_TYPE_AUX_OUTPUT:
1356e884602bSLeo Yan 			ADD_CONFIG_TERM_VAL(AUX_OUTPUT, aux_output,
135705e54e23SIan Rogers 					    term->val.num ? 1 : 0, term->weak);
13581b992154SAdrian Hunter 			break;
1359eb7a52d4SAdrian Hunter 		case PARSE_EVENTS__TERM_TYPE_AUX_SAMPLE_SIZE:
1360e884602bSLeo Yan 			ADD_CONFIG_TERM_VAL(AUX_SAMPLE_SIZE, aux_sample_size,
136105e54e23SIan Rogers 					    term->val.num, term->weak);
1362eb7a52d4SAdrian Hunter 			break;
1363930a2e29SJiri Olsa 		default:
1364930a2e29SJiri Olsa 			break;
1365930a2e29SJiri Olsa 		}
1366930a2e29SJiri Olsa 	}
1367a1ac7de6SAdrian Hunter 	return 0;
1368a1ac7de6SAdrian Hunter }
1369a1ac7de6SAdrian Hunter 
1370a1ac7de6SAdrian Hunter /*
137135ac0cadSArnaldo Carvalho de Melo  * Add EVSEL__CONFIG_TERM_CFG_CHG where cfg_chg will have a bit set for
1372a1ac7de6SAdrian Hunter  * each bit of attr->config that the user has changed.
1373a1ac7de6SAdrian Hunter  */
1374a1ac7de6SAdrian Hunter static int get_config_chgs(struct perf_pmu *pmu, struct list_head *head_config,
1375a1ac7de6SAdrian Hunter 			   struct list_head *head_terms)
1376a1ac7de6SAdrian Hunter {
1377a1ac7de6SAdrian Hunter 	struct parse_events_term *term;
1378a1ac7de6SAdrian Hunter 	u64 bits = 0;
1379a1ac7de6SAdrian Hunter 	int type;
1380a1ac7de6SAdrian Hunter 
1381a1ac7de6SAdrian Hunter 	list_for_each_entry(term, head_config, list) {
1382a1ac7de6SAdrian Hunter 		switch (term->type_term) {
1383a1ac7de6SAdrian Hunter 		case PARSE_EVENTS__TERM_TYPE_USER:
1384a1ac7de6SAdrian Hunter 			type = perf_pmu__format_type(&pmu->format, term->config);
1385a1ac7de6SAdrian Hunter 			if (type != PERF_PMU_FORMAT_VALUE_CONFIG)
1386a1ac7de6SAdrian Hunter 				continue;
1387a1ac7de6SAdrian Hunter 			bits |= perf_pmu__format_bits(&pmu->format, term->config);
1388a1ac7de6SAdrian Hunter 			break;
1389a1ac7de6SAdrian Hunter 		case PARSE_EVENTS__TERM_TYPE_CONFIG:
1390a1ac7de6SAdrian Hunter 			bits = ~(u64)0;
1391a1ac7de6SAdrian Hunter 			break;
1392a1ac7de6SAdrian Hunter 		default:
1393a1ac7de6SAdrian Hunter 			break;
1394a1ac7de6SAdrian Hunter 		}
1395a1ac7de6SAdrian Hunter 	}
1396a1ac7de6SAdrian Hunter 
1397a1ac7de6SAdrian Hunter 	if (bits)
139805e54e23SIan Rogers 		ADD_CONFIG_TERM_VAL(CFG_CHG, cfg_chg, bits, false);
1399a1ac7de6SAdrian Hunter 
1400a1ac7de6SAdrian Hunter #undef ADD_CONFIG_TERM
1401930a2e29SJiri Olsa 	return 0;
1402930a2e29SJiri Olsa }
1403930a2e29SJiri Olsa 
1404e637d177SHe Kuang int parse_events_add_tracepoint(struct list_head *list, int *idx,
14058c619d6aSWang Nan 				const char *sys, const char *event,
1406272ed29aSJiri Olsa 				struct parse_events_error *err,
1407e637d177SHe Kuang 				struct list_head *head_config)
1408e637d177SHe Kuang {
1409378ef0f5SIan Rogers #ifdef HAVE_LIBTRACEEVENT
1410e637d177SHe Kuang 	if (head_config) {
1411e637d177SHe Kuang 		struct perf_event_attr attr;
1412e637d177SHe Kuang 
1413272ed29aSJiri Olsa 		if (config_attr(&attr, head_config, err,
1414e637d177SHe Kuang 				config_term_tracepoint))
1415e637d177SHe Kuang 			return -EINVAL;
1416e637d177SHe Kuang 	}
1417e637d177SHe Kuang 
1418e637d177SHe Kuang 	if (strpbrk(sys, "*?"))
1419e637d177SHe Kuang 		return add_tracepoint_multi_sys(list, idx, sys, event,
1420272ed29aSJiri Olsa 						err, head_config);
1421e637d177SHe Kuang 	else
1422e637d177SHe Kuang 		return add_tracepoint_event(list, idx, sys, event,
1423272ed29aSJiri Olsa 					    err, head_config);
1424378ef0f5SIan Rogers #else
1425378ef0f5SIan Rogers 	(void)list;
1426378ef0f5SIan Rogers 	(void)idx;
1427378ef0f5SIan Rogers 	(void)sys;
1428378ef0f5SIan Rogers 	(void)event;
1429378ef0f5SIan Rogers 	(void)head_config;
1430378ef0f5SIan Rogers 	parse_events_error__handle(err, 0, strdup("unsupported tracepoint"),
1431378ef0f5SIan Rogers 				strdup("libtraceevent is necessary for tracepoint support"));
1432378ef0f5SIan Rogers 	return -1;
1433378ef0f5SIan Rogers #endif
1434e637d177SHe Kuang }
1435e637d177SHe Kuang 
14368bc75f69SIan Rogers static int __parse_events_add_numeric(struct parse_events_state *parse_state,
143787d650beSJiri Olsa 				struct list_head *list,
14388bc75f69SIan Rogers 				struct perf_pmu *pmu, u32 type, u64 config,
14398f707d84SJiri Olsa 				struct list_head *head_config)
144074d5b588SJaswinder Singh Rajput {
144189812fc8SJiri Olsa 	struct perf_event_attr attr;
1442930a2e29SJiri Olsa 	LIST_HEAD(config_terms);
14432b62b3a6SIan Rogers 	const char *name, *metric_id;
14449cbfa2f6SJin Yao 	int ret;
144574d5b588SJaswinder Singh Rajput 
144689812fc8SJiri Olsa 	memset(&attr, 0, sizeof(attr));
144789812fc8SJiri Olsa 	attr.type = type;
144889812fc8SJiri Olsa 	attr.config = config;
14498f707d84SJiri Olsa 
1450930a2e29SJiri Olsa 	if (head_config) {
14515d9cdc11SArnaldo Carvalho de Melo 		if (config_attr(&attr, head_config, parse_state->error,
14520b8891a8SHe Kuang 				config_term_common))
14538f707d84SJiri Olsa 			return -EINVAL;
14548f707d84SJiri Olsa 
1455930a2e29SJiri Olsa 		if (get_config_terms(head_config, &config_terms))
1456930a2e29SJiri Olsa 			return -ENOMEM;
1457930a2e29SJiri Olsa 	}
1458930a2e29SJiri Olsa 
14592b62b3a6SIan Rogers 	name = get_config_name(head_config);
14602b62b3a6SIan Rogers 	metric_id = get_config_metric_id(head_config);
14618bc75f69SIan Rogers 	ret = __add_event(list, &parse_state->idx, &attr, /*init_attr*/true, name,
14628bc75f69SIan Rogers 			metric_id, pmu, &config_terms, /*auto_merge_stats=*/false,
14638bc75f69SIan Rogers 			/*cpu_list=*/NULL) ? 0 : -ENOMEM;
146499fc5941SAdrian Hunter 	free_config_terms(&config_terms);
146599fc5941SAdrian Hunter 	return ret;
1466b908debdSIngo Molnar }
1467b908debdSIngo Molnar 
14688bc75f69SIan Rogers int parse_events_add_numeric(struct parse_events_state *parse_state,
14698bc75f69SIan Rogers 			     struct list_head *list,
14708bc75f69SIan Rogers 			     u32 type, u64 config,
14718bc75f69SIan Rogers 			     struct list_head *head_config,
14728bc75f69SIan Rogers 			     bool wildcard)
14738bc75f69SIan Rogers {
14748bc75f69SIan Rogers 	struct perf_pmu *pmu = NULL;
14758bc75f69SIan Rogers 	bool found_supported = false;
14768bc75f69SIan Rogers 
14778bc75f69SIan Rogers 	if (!wildcard)
14788bc75f69SIan Rogers 		return __parse_events_add_numeric(parse_state, list, /*pmu=*/NULL,
14798bc75f69SIan Rogers 						  type, config, head_config);
14808bc75f69SIan Rogers 
1481*9d6a1df9SIan Rogers 	/* Wildcards on numeric values are only supported by core PMUs. */
1482*9d6a1df9SIan Rogers 	while ((pmu = perf_pmus__scan_core(pmu)) != NULL) {
14838bc75f69SIan Rogers 		int ret;
14848bc75f69SIan Rogers 
1485411ad22eSIan Rogers 		if (parse_events__filter_pmu(parse_state, pmu))
1486411ad22eSIan Rogers 			continue;
1487411ad22eSIan Rogers 
14888bc75f69SIan Rogers 		found_supported = true;
14898bc75f69SIan Rogers 		ret = __parse_events_add_numeric(parse_state, list, pmu, pmu->type,
14908bc75f69SIan Rogers 						 config, head_config);
14918bc75f69SIan Rogers 		if (ret)
14928bc75f69SIan Rogers 			return ret;
14938bc75f69SIan Rogers 	}
14948bc75f69SIan Rogers 	return found_supported ? 0 : -EINVAL;
14958bc75f69SIan Rogers }
14968bc75f69SIan Rogers 
1497f0fbb114SAndi Kleen int parse_events_add_tool(struct parse_events_state *parse_state,
1498f0fbb114SAndi Kleen 			  struct list_head *list,
14998228e936SIan Rogers 			  int tool_event)
1500f0fbb114SAndi Kleen {
1501f0fbb114SAndi Kleen 	return add_event_tool(list, &parse_state->idx, tool_event);
1502f0fbb114SAndi Kleen }
1503f0fbb114SAndi Kleen 
1504064b4e82SJin Yao static bool config_term_percore(struct list_head *config_terms)
1505064b4e82SJin Yao {
150635ac0cadSArnaldo Carvalho de Melo 	struct evsel_config_term *term;
1507064b4e82SJin Yao 
1508064b4e82SJin Yao 	list_for_each_entry(term, config_terms, list) {
150935ac0cadSArnaldo Carvalho de Melo 		if (term->type == EVSEL__CONFIG_TERM_PERCORE)
1510064b4e82SJin Yao 			return term->val.percore;
1511064b4e82SJin Yao 	}
1512064b4e82SJin Yao 
1513064b4e82SJin Yao 	return false;
1514064b4e82SJin Yao }
1515064b4e82SJin Yao 
1516c199c11dSAgustin Vega-Frias int parse_events_add_pmu(struct parse_events_state *parse_state,
151736adec85SJiri Olsa 			 struct list_head *list, char *name,
15183cdc5c2cSKan Liang 			 struct list_head *head_config,
1519e733f87eSIan Rogers 			 bool auto_merge_stats)
15205f537a26SJiri Olsa {
15215f537a26SJiri Olsa 	struct perf_event_attr attr;
152246441bdcSMatt Fleming 	struct perf_pmu_info info;
15235f537a26SJiri Olsa 	struct perf_pmu *pmu;
152432dcd021SJiri Olsa 	struct evsel *evsel;
1525333b5665SAndi Kleen 	struct parse_events_error *err = parse_state->error;
1526930a2e29SJiri Olsa 	LIST_HEAD(config_terms);
15275f537a26SJiri Olsa 
15281eaf496eSIan Rogers 	pmu = parse_state->fake_pmu ?: perf_pmus__find(name);
1529f034fc50SAdrian Hunter 
1530f034fc50SAdrian Hunter 	if (verbose > 1 && !(pmu && pmu->selectable)) {
15314ac22b48SIan Rogers 		fprintf(stderr, "Attempting to add event pmu '%s' with '",
15324ac22b48SIan Rogers 			name);
15334ac22b48SIan Rogers 		if (head_config) {
15344ac22b48SIan Rogers 			struct parse_events_term *term;
15354ac22b48SIan Rogers 
15364ac22b48SIan Rogers 			list_for_each_entry(term, head_config, list) {
15374ac22b48SIan Rogers 				fprintf(stderr, "%s,", term->config);
15384ac22b48SIan Rogers 			}
15394ac22b48SIan Rogers 		}
15404ac22b48SIan Rogers 		fprintf(stderr, "' that may result in non-fatal errors\n");
15414ac22b48SIan Rogers 	}
15424ac22b48SIan Rogers 
1543333b5665SAndi Kleen 	if (!pmu) {
1544448d732cSIan Rogers 		char *err_str;
1545448d732cSIan Rogers 
1546448d732cSIan Rogers 		if (asprintf(&err_str,
1547333b5665SAndi Kleen 				"Cannot find PMU `%s'. Missing kernel support?",
1548448d732cSIan Rogers 				name) >= 0)
15496c191289SIan Rogers 			parse_events_error__handle(err, 0, err_str, NULL);
15505f537a26SJiri Olsa 		return -EINVAL;
1551333b5665SAndi Kleen 	}
155270c90e4aSIan Rogers 	if (head_config)
155370c90e4aSIan Rogers 		fix_raw(head_config, pmu);
15545f537a26SJiri Olsa 
1555dc0a6202SAdrian Hunter 	if (pmu->default_config) {
1556dc0a6202SAdrian Hunter 		memcpy(&attr, pmu->default_config,
1557dc0a6202SAdrian Hunter 		       sizeof(struct perf_event_attr));
1558dc0a6202SAdrian Hunter 	} else {
15595f537a26SJiri Olsa 		memset(&attr, 0, sizeof(attr));
1560dc0a6202SAdrian Hunter 	}
1561c9aeb2e9SIan Rogers 	attr.type = pmu->type;
15625f537a26SJiri Olsa 
1563ad962273SAdrian Hunter 	if (!head_config) {
15642b62b3a6SIan Rogers 		evsel = __add_event(list, &parse_state->idx, &attr,
15652b62b3a6SIan Rogers 				    /*init_attr=*/true, /*name=*/NULL,
15662b62b3a6SIan Rogers 				    /*metric_id=*/NULL, pmu,
15672b62b3a6SIan Rogers 				    /*config_terms=*/NULL, auto_merge_stats,
15682b62b3a6SIan Rogers 				    /*cpu_list=*/NULL);
1569cae256aeSIan Rogers 		return evsel ? 0 : -ENOMEM;
1570ad962273SAdrian Hunter 	}
1571ad962273SAdrian Hunter 
1572387ad33fSJiri Olsa 	if (!parse_state->fake_pmu && perf_pmu__check_alias(pmu, head_config, &info))
1573a6146d50SZheng Yan 		return -EINVAL;
1574a6146d50SZheng Yan 
15754ac22b48SIan Rogers 	if (verbose > 1) {
15764ac22b48SIan Rogers 		fprintf(stderr, "After aliases, add event pmu '%s' with '",
15774ac22b48SIan Rogers 			name);
15784ac22b48SIan Rogers 		if (head_config) {
15794ac22b48SIan Rogers 			struct parse_events_term *term;
15804ac22b48SIan Rogers 
15814ac22b48SIan Rogers 			list_for_each_entry(term, head_config, list) {
15824ac22b48SIan Rogers 				fprintf(stderr, "%s,", term->config);
15834ac22b48SIan Rogers 			}
15844ac22b48SIan Rogers 		}
15854ac22b48SIan Rogers 		fprintf(stderr, "' that may result in non-fatal errors\n");
15864ac22b48SIan Rogers 	}
15874ac22b48SIan Rogers 
15885f537a26SJiri Olsa 	/*
15895f537a26SJiri Olsa 	 * Configure hardcoded terms first, no need to check
15905f537a26SJiri Olsa 	 * return value when called with fail == 0 ;)
15915f537a26SJiri Olsa 	 */
15925d9cdc11SArnaldo Carvalho de Melo 	if (config_attr(&attr, head_config, parse_state->error, config_term_pmu))
1593c056ba6aSJiri Olsa 		return -EINVAL;
15945f537a26SJiri Olsa 
1595930a2e29SJiri Olsa 	if (get_config_terms(head_config, &config_terms))
1596930a2e29SJiri Olsa 		return -ENOMEM;
1597930a2e29SJiri Olsa 
1598a1ac7de6SAdrian Hunter 	/*
1599a1ac7de6SAdrian Hunter 	 * When using default config, record which bits of attr->config were
1600a1ac7de6SAdrian Hunter 	 * changed by the user.
1601a1ac7de6SAdrian Hunter 	 */
1602a1ac7de6SAdrian Hunter 	if (pmu->default_config && get_config_chgs(pmu, head_config, &config_terms))
1603a1ac7de6SAdrian Hunter 		return -ENOMEM;
1604a1ac7de6SAdrian Hunter 
1605387ad33fSJiri Olsa 	if (!parse_state->fake_pmu && perf_pmu__config(pmu, &attr, head_config, parse_state->error)) {
1606a7d212fcSAdrian Hunter 		free_config_terms(&config_terms);
16075f537a26SJiri Olsa 		return -EINVAL;
160838f2c422SIan Rogers 	}
16095f537a26SJiri Olsa 
16102b62b3a6SIan Rogers 	evsel = __add_event(list, &parse_state->idx, &attr, /*init_attr=*/true,
16112b62b3a6SIan Rogers 			    get_config_name(head_config),
16122b62b3a6SIan Rogers 			    get_config_metric_id(head_config), pmu,
16132b62b3a6SIan Rogers 			    &config_terms, auto_merge_stats, /*cpu_list=*/NULL);
161433321a06SIan Rogers 	if (!evsel)
161533321a06SIan Rogers 		return -ENOMEM;
161633321a06SIan Rogers 
161712279429SJin Yao 	if (evsel->name)
161812279429SJin Yao 		evsel->use_config_name = true;
161912279429SJin Yao 
162033321a06SIan Rogers 	evsel->percore = config_term_percore(&evsel->config_terms);
162133321a06SIan Rogers 
162233321a06SIan Rogers 	if (parse_state->fake_pmu)
162333321a06SIan Rogers 		return 0;
162433321a06SIan Rogers 
1625b194c9cdSIan Rogers 	free((char *)evsel->unit);
1626b194c9cdSIan Rogers 	evsel->unit = strdup(info.unit);
162746441bdcSMatt Fleming 	evsel->scale = info.scale;
1628044330c1SMatt Fleming 	evsel->per_pkg = info.per_pkg;
16291d9e446bSJiri Olsa 	evsel->snapshot = info.snapshot;
163033321a06SIan Rogers 	return 0;
16315f537a26SJiri Olsa }
16325f537a26SJiri Olsa 
16335d9cdc11SArnaldo Carvalho de Melo int parse_events_multi_pmu_add(struct parse_events_state *parse_state,
1634fb081153SIan Rogers 			       char *str, struct list_head *head,
1635fb081153SIan Rogers 			       struct list_head **listp)
16362073ad33SAndi Kleen {
16372073ad33SAndi Kleen 	struct parse_events_term *term;
1638fb081153SIan Rogers 	struct list_head *list = NULL;
163991c9923aSZhengjun Xing 	struct list_head *orig_head = NULL;
16402073ad33SAndi Kleen 	struct perf_pmu *pmu = NULL;
16412073ad33SAndi Kleen 	int ok = 0;
1642fb081153SIan Rogers 	char *config;
16432073ad33SAndi Kleen 
16442073ad33SAndi Kleen 	*listp = NULL;
1645fb081153SIan Rogers 
1646fb081153SIan Rogers 	if (!head) {
1647fb081153SIan Rogers 		head = malloc(sizeof(struct list_head));
1648fb081153SIan Rogers 		if (!head)
1649fb081153SIan Rogers 			goto out_err;
1650fb081153SIan Rogers 
1651fb081153SIan Rogers 		INIT_LIST_HEAD(head);
1652fb081153SIan Rogers 	}
1653fb081153SIan Rogers 	config = strdup(str);
1654fb081153SIan Rogers 	if (!config)
1655fb081153SIan Rogers 		goto out_err;
1656fb081153SIan Rogers 
1657fb081153SIan Rogers 	if (parse_events_term__num(&term,
1658fb081153SIan Rogers 				   PARSE_EVENTS__TERM_TYPE_USER,
165966c9598bSNamhyung Kim 				   config, 1, false, NULL,
1660fb081153SIan Rogers 					NULL) < 0) {
1661fb081153SIan Rogers 		free(config);
1662fb081153SIan Rogers 		goto out_err;
1663fb081153SIan Rogers 	}
1664fb081153SIan Rogers 	list_add_tail(&term->list, head);
1665fb081153SIan Rogers 
16662073ad33SAndi Kleen 	/* Add it for all PMUs that support the alias */
16672073ad33SAndi Kleen 	list = malloc(sizeof(struct list_head));
16682073ad33SAndi Kleen 	if (!list)
1669fb081153SIan Rogers 		goto out_err;
1670fb081153SIan Rogers 
16712073ad33SAndi Kleen 	INIT_LIST_HEAD(list);
1672fb081153SIan Rogers 
16731eaf496eSIan Rogers 	while ((pmu = perf_pmus__scan(pmu)) != NULL) {
16742073ad33SAndi Kleen 		struct perf_pmu_alias *alias;
167552c7b4d3SIan Rogers 		bool auto_merge_stats;
16762073ad33SAndi Kleen 
1677411ad22eSIan Rogers 		if (parse_events__filter_pmu(parse_state, pmu))
1678411ad22eSIan Rogers 			continue;
1679411ad22eSIan Rogers 
168052c7b4d3SIan Rogers 		auto_merge_stats = perf_pmu__auto_merge_stats(pmu);
168152c7b4d3SIan Rogers 
16822073ad33SAndi Kleen 		list_for_each_entry(alias, &pmu->aliases, list) {
16832073ad33SAndi Kleen 			if (!strcasecmp(alias->name, str)) {
168491c9923aSZhengjun Xing 				parse_events_copy_term_list(head, &orig_head);
1685c199c11dSAgustin Vega-Frias 				if (!parse_events_add_pmu(parse_state, list,
168691c9923aSZhengjun Xing 							  pmu->name, orig_head,
168752c7b4d3SIan Rogers 							  auto_merge_stats)) {
1688d4953f7eSIan Rogers 					pr_debug("%s -> %s/%s/\n", str,
16892073ad33SAndi Kleen 						 pmu->name, alias->str);
16902073ad33SAndi Kleen 					ok++;
16912073ad33SAndi Kleen 				}
169291c9923aSZhengjun Xing 				parse_events_terms__delete(orig_head);
1693fb081153SIan Rogers 			}
1694fb081153SIan Rogers 		}
1695fb081153SIan Rogers 	}
1696b4a7276cSJohn Garry 
1697b4a7276cSJohn Garry 	if (parse_state->fake_pmu) {
1698b4a7276cSJohn Garry 		if (!parse_events_add_pmu(parse_state, list, str, head,
1699e733f87eSIan Rogers 					  /*auto_merge_stats=*/true)) {
1700b4a7276cSJohn Garry 			pr_debug("%s -> %s/%s/\n", str, "fake_pmu", str);
1701b4a7276cSJohn Garry 			ok++;
1702b4a7276cSJohn Garry 		}
1703b4a7276cSJohn Garry 	}
1704b4a7276cSJohn Garry 
1705fb081153SIan Rogers out_err:
1706fb081153SIan Rogers 	if (ok)
1707fb081153SIan Rogers 		*listp = list;
1708fb081153SIan Rogers 	else
1709fb081153SIan Rogers 		free(list);
17102073ad33SAndi Kleen 
17112073ad33SAndi Kleen 	parse_events_terms__delete(head);
1712fb081153SIan Rogers 	return ok ? 0 : -1;
17132073ad33SAndi Kleen }
17142073ad33SAndi Kleen 
17156a4bb04cSJiri Olsa int parse_events__modifier_group(struct list_head *list,
17166a4bb04cSJiri Olsa 				 char *event_mod)
171789efb029SJiri Olsa {
17186a4bb04cSJiri Olsa 	return parse_events__modifier_event(list, event_mod, true);
17196a4bb04cSJiri Olsa }
17206a4bb04cSJiri Olsa 
1721347c2f0aSIan Rogers void parse_events__set_leader(char *name, struct list_head *list)
17226a4bb04cSJiri Olsa {
172332dcd021SJiri Olsa 	struct evsel *leader;
17246a4bb04cSJiri Olsa 
1725854f7363SWang Nan 	if (list_empty(list)) {
1726854f7363SWang Nan 		WARN_ONCE(true, "WARNING: failed to set leader: empty list");
1727854f7363SWang Nan 		return;
1728854f7363SWang Nan 	}
1729854f7363SWang Nan 
1730347c2f0aSIan Rogers 	leader = list_first_entry(list, struct evsel, core.node);
1731ecdcf630SIan Rogers 	__perf_evlist__set_leader(list, &leader->core);
17324bb311b2SIan Rogers 	leader->group_name = name;
173389efb029SJiri Olsa }
173489efb029SJiri Olsa 
1735c5cd8ac0SDavid Ahern /* list_event is assumed to point to malloc'ed memory */
17365d7be90eSJiri Olsa void parse_events_update_lists(struct list_head *list_event,
17375d7be90eSJiri Olsa 			       struct list_head *list_all)
17385d7be90eSJiri Olsa {
17395d7be90eSJiri Olsa 	/*
17405d7be90eSJiri Olsa 	 * Called for single event definition. Update the
174189efb029SJiri Olsa 	 * 'all event' list, and reinit the 'single event'
17425d7be90eSJiri Olsa 	 * list, for next event definition.
17435d7be90eSJiri Olsa 	 */
17445d7be90eSJiri Olsa 	list_splice_tail(list_event, list_all);
1745b847cbdcSJiri Olsa 	free(list_event);
17465d7be90eSJiri Olsa }
17475d7be90eSJiri Olsa 
1748f5b1135bSJiri Olsa struct event_modifier {
1749f5b1135bSJiri Olsa 	int eu;
1750f5b1135bSJiri Olsa 	int ek;
1751f5b1135bSJiri Olsa 	int eh;
1752f5b1135bSJiri Olsa 	int eH;
1753f5b1135bSJiri Olsa 	int eG;
1754a1e12da4SJiri Olsa 	int eI;
1755f5b1135bSJiri Olsa 	int precise;
17567f94af7aSJiri Olsa 	int precise_max;
1757f5b1135bSJiri Olsa 	int exclude_GH;
17583c176311SJiri Olsa 	int sample_read;
1759e9a7c414SMichael Ellerman 	int pinned;
17605a5dfe4bSAndi Kleen 	int weak;
17610997a266SAndi Kleen 	int exclusive;
176201bd8efcSSong Liu 	int bpf_counter;
1763f5b1135bSJiri Olsa };
176461c45981SPaul Mackerras 
1765f5b1135bSJiri Olsa static int get_event_modifier(struct event_modifier *mod, char *str,
176632dcd021SJiri Olsa 			       struct evsel *evsel)
1767f5b1135bSJiri Olsa {
17681fc632ceSJiri Olsa 	int eu = evsel ? evsel->core.attr.exclude_user : 0;
17691fc632ceSJiri Olsa 	int ek = evsel ? evsel->core.attr.exclude_kernel : 0;
17701fc632ceSJiri Olsa 	int eh = evsel ? evsel->core.attr.exclude_hv : 0;
17711fc632ceSJiri Olsa 	int eH = evsel ? evsel->core.attr.exclude_host : 0;
17721fc632ceSJiri Olsa 	int eG = evsel ? evsel->core.attr.exclude_guest : 0;
17731fc632ceSJiri Olsa 	int eI = evsel ? evsel->core.attr.exclude_idle : 0;
17741fc632ceSJiri Olsa 	int precise = evsel ? evsel->core.attr.precise_ip : 0;
17757f94af7aSJiri Olsa 	int precise_max = 0;
17763c176311SJiri Olsa 	int sample_read = 0;
17771fc632ceSJiri Olsa 	int pinned = evsel ? evsel->core.attr.pinned : 0;
17780997a266SAndi Kleen 	int exclusive = evsel ? evsel->core.attr.exclusive : 0;
1779f5b1135bSJiri Olsa 
1780f5b1135bSJiri Olsa 	int exclude = eu | ek | eh;
1781f5b1135bSJiri Olsa 	int exclude_GH = evsel ? evsel->exclude_GH : 0;
17825a5dfe4bSAndi Kleen 	int weak = 0;
178301bd8efcSSong Liu 	int bpf_counter = 0;
1784f5b1135bSJiri Olsa 
1785f5b1135bSJiri Olsa 	memset(mod, 0, sizeof(*mod));
1786ceb53fbfSIngo Molnar 
178761c45981SPaul Mackerras 	while (*str) {
1788ab608344SPeter Zijlstra 		if (*str == 'u') {
1789ab608344SPeter Zijlstra 			if (!exclude)
1790ab608344SPeter Zijlstra 				exclude = eu = ek = eh = 1;
1791943b69acSJin Yao 			if (!exclude_GH && !perf_guest)
1792943b69acSJin Yao 				eG = 1;
179361c45981SPaul Mackerras 			eu = 0;
1794ab608344SPeter Zijlstra 		} else if (*str == 'k') {
1795ab608344SPeter Zijlstra 			if (!exclude)
1796ab608344SPeter Zijlstra 				exclude = eu = ek = eh = 1;
179761c45981SPaul Mackerras 			ek = 0;
1798ab608344SPeter Zijlstra 		} else if (*str == 'h') {
1799ab608344SPeter Zijlstra 			if (!exclude)
1800ab608344SPeter Zijlstra 				exclude = eu = ek = eh = 1;
180161c45981SPaul Mackerras 			eh = 0;
180299320cc8SJoerg Roedel 		} else if (*str == 'G') {
180399320cc8SJoerg Roedel 			if (!exclude_GH)
180499320cc8SJoerg Roedel 				exclude_GH = eG = eH = 1;
180599320cc8SJoerg Roedel 			eG = 0;
180699320cc8SJoerg Roedel 		} else if (*str == 'H') {
180799320cc8SJoerg Roedel 			if (!exclude_GH)
180899320cc8SJoerg Roedel 				exclude_GH = eG = eH = 1;
180999320cc8SJoerg Roedel 			eH = 0;
1810a1e12da4SJiri Olsa 		} else if (*str == 'I') {
1811a1e12da4SJiri Olsa 			eI = 1;
1812ab608344SPeter Zijlstra 		} else if (*str == 'p') {
1813ab608344SPeter Zijlstra 			precise++;
18141342798cSDavid Ahern 			/* use of precise requires exclude_guest */
18151342798cSDavid Ahern 			if (!exclude_GH)
18161342798cSDavid Ahern 				eG = 1;
18177f94af7aSJiri Olsa 		} else if (*str == 'P') {
18187f94af7aSJiri Olsa 			precise_max = 1;
18193c176311SJiri Olsa 		} else if (*str == 'S') {
18203c176311SJiri Olsa 			sample_read = 1;
1821e9a7c414SMichael Ellerman 		} else if (*str == 'D') {
1822e9a7c414SMichael Ellerman 			pinned = 1;
18230997a266SAndi Kleen 		} else if (*str == 'e') {
18240997a266SAndi Kleen 			exclusive = 1;
18255a5dfe4bSAndi Kleen 		} else if (*str == 'W') {
18265a5dfe4bSAndi Kleen 			weak = 1;
182701bd8efcSSong Liu 		} else if (*str == 'b') {
182801bd8efcSSong Liu 			bpf_counter = 1;
1829ab608344SPeter Zijlstra 		} else
183061c45981SPaul Mackerras 			break;
1831ab608344SPeter Zijlstra 
183261c45981SPaul Mackerras 		++str;
183361c45981SPaul Mackerras 	}
183474d5b588SJaswinder Singh Rajput 
183586470930SIngo Molnar 	/*
183689812fc8SJiri Olsa 	 * precise ip:
183789812fc8SJiri Olsa 	 *
183889812fc8SJiri Olsa 	 *  0 - SAMPLE_IP can have arbitrary skid
183989812fc8SJiri Olsa 	 *  1 - SAMPLE_IP must have constant skid
184089812fc8SJiri Olsa 	 *  2 - SAMPLE_IP requested to have 0 skid
184189812fc8SJiri Olsa 	 *  3 - SAMPLE_IP must have 0 skid
184289812fc8SJiri Olsa 	 *
184389812fc8SJiri Olsa 	 *  See also PERF_RECORD_MISC_EXACT_IP
184486470930SIngo Molnar 	 */
184589812fc8SJiri Olsa 	if (precise > 3)
184689812fc8SJiri Olsa 		return -EINVAL;
184786470930SIngo Molnar 
1848f5b1135bSJiri Olsa 	mod->eu = eu;
1849f5b1135bSJiri Olsa 	mod->ek = ek;
1850f5b1135bSJiri Olsa 	mod->eh = eh;
1851f5b1135bSJiri Olsa 	mod->eH = eH;
1852f5b1135bSJiri Olsa 	mod->eG = eG;
1853a1e12da4SJiri Olsa 	mod->eI = eI;
1854f5b1135bSJiri Olsa 	mod->precise = precise;
18557f94af7aSJiri Olsa 	mod->precise_max = precise_max;
1856f5b1135bSJiri Olsa 	mod->exclude_GH = exclude_GH;
18573c176311SJiri Olsa 	mod->sample_read = sample_read;
1858e9a7c414SMichael Ellerman 	mod->pinned = pinned;
18595a5dfe4bSAndi Kleen 	mod->weak = weak;
186001bd8efcSSong Liu 	mod->bpf_counter = bpf_counter;
18610997a266SAndi Kleen 	mod->exclusive = exclusive;
1862e9a7c414SMichael Ellerman 
1863f5b1135bSJiri Olsa 	return 0;
1864f5b1135bSJiri Olsa }
1865f5b1135bSJiri Olsa 
1866534123f4SJiri Olsa /*
1867534123f4SJiri Olsa  * Basic modifier sanity check to validate it contains only one
1868534123f4SJiri Olsa  * instance of any modifier (apart from 'p') present.
1869534123f4SJiri Olsa  */
1870534123f4SJiri Olsa static int check_modifier(char *str)
1871534123f4SJiri Olsa {
1872534123f4SJiri Olsa 	char *p = str;
1873534123f4SJiri Olsa 
1874534123f4SJiri Olsa 	/* The sizeof includes 0 byte as well. */
187501bd8efcSSong Liu 	if (strlen(str) > (sizeof("ukhGHpppPSDIWeb") - 1))
1876534123f4SJiri Olsa 		return -1;
1877534123f4SJiri Olsa 
1878534123f4SJiri Olsa 	while (*p) {
1879534123f4SJiri Olsa 		if (*p != 'p' && strchr(p + 1, *p))
1880534123f4SJiri Olsa 			return -1;
1881534123f4SJiri Olsa 		p++;
1882534123f4SJiri Olsa 	}
1883534123f4SJiri Olsa 
1884534123f4SJiri Olsa 	return 0;
1885534123f4SJiri Olsa }
1886534123f4SJiri Olsa 
1887f5b1135bSJiri Olsa int parse_events__modifier_event(struct list_head *list, char *str, bool add)
1888f5b1135bSJiri Olsa {
188932dcd021SJiri Olsa 	struct evsel *evsel;
1890f5b1135bSJiri Olsa 	struct event_modifier mod;
1891f5b1135bSJiri Olsa 
1892f5b1135bSJiri Olsa 	if (str == NULL)
1893f5b1135bSJiri Olsa 		return 0;
1894f5b1135bSJiri Olsa 
1895534123f4SJiri Olsa 	if (check_modifier(str))
1896534123f4SJiri Olsa 		return -EINVAL;
1897534123f4SJiri Olsa 
1898f5b1135bSJiri Olsa 	if (!add && get_event_modifier(&mod, str, NULL))
1899f5b1135bSJiri Olsa 		return -EINVAL;
1900f5b1135bSJiri Olsa 
1901e5cadb93SArnaldo Carvalho de Melo 	__evlist__for_each_entry(list, evsel) {
1902f5b1135bSJiri Olsa 		if (add && get_event_modifier(&mod, str, evsel))
1903f5b1135bSJiri Olsa 			return -EINVAL;
1904f5b1135bSJiri Olsa 
19051fc632ceSJiri Olsa 		evsel->core.attr.exclude_user   = mod.eu;
19061fc632ceSJiri Olsa 		evsel->core.attr.exclude_kernel = mod.ek;
19071fc632ceSJiri Olsa 		evsel->core.attr.exclude_hv     = mod.eh;
19081fc632ceSJiri Olsa 		evsel->core.attr.precise_ip     = mod.precise;
19091fc632ceSJiri Olsa 		evsel->core.attr.exclude_host   = mod.eH;
19101fc632ceSJiri Olsa 		evsel->core.attr.exclude_guest  = mod.eG;
19111fc632ceSJiri Olsa 		evsel->core.attr.exclude_idle   = mod.eI;
1912f5b1135bSJiri Olsa 		evsel->exclude_GH          = mod.exclude_GH;
19133c176311SJiri Olsa 		evsel->sample_read         = mod.sample_read;
19147f94af7aSJiri Olsa 		evsel->precise_max         = mod.precise_max;
19155a5dfe4bSAndi Kleen 		evsel->weak_group	   = mod.weak;
191601bd8efcSSong Liu 		evsel->bpf_counter	   = mod.bpf_counter;
1917e9a7c414SMichael Ellerman 
19180997a266SAndi Kleen 		if (evsel__is_group_leader(evsel)) {
19191fc632ceSJiri Olsa 			evsel->core.attr.pinned = mod.pinned;
19200997a266SAndi Kleen 			evsel->core.attr.exclusive = mod.exclusive;
19210997a266SAndi Kleen 		}
1922ceb53fbfSIngo Molnar 	}
192386470930SIngo Molnar 
192489812fc8SJiri Olsa 	return 0;
192586470930SIngo Molnar }
192686470930SIngo Molnar 
19278e8bbfb3SIan Rogers int parse_events_name(struct list_head *list, const char *name)
1928ac2ba9f3SRobert Richter {
192932dcd021SJiri Olsa 	struct evsel *evsel;
1930ac2ba9f3SRobert Richter 
1931e5cadb93SArnaldo Carvalho de Melo 	__evlist__for_each_entry(list, evsel) {
1932ac2ba9f3SRobert Richter 		if (!evsel->name)
1933ac2ba9f3SRobert Richter 			evsel->name = strdup(name);
1934ac2ba9f3SRobert Richter 	}
1935ac2ba9f3SRobert Richter 
1936ac2ba9f3SRobert Richter 	return 0;
1937ac2ba9f3SRobert Richter }
1938ac2ba9f3SRobert Richter 
19391244a327SJiri Olsa static int parse_events__scanner(const char *str,
19401244a327SJiri Olsa 				 struct parse_events_state *parse_state)
1941ac20de6fSZheng Yan {
1942ac20de6fSZheng Yan 	YY_BUFFER_STATE buffer;
1943ac20de6fSZheng Yan 	void *scanner;
1944ac20de6fSZheng Yan 	int ret;
1945ac20de6fSZheng Yan 
19461244a327SJiri Olsa 	ret = parse_events_lex_init_extra(parse_state, &scanner);
1947ac20de6fSZheng Yan 	if (ret)
1948ac20de6fSZheng Yan 		return ret;
1949ac20de6fSZheng Yan 
1950ac20de6fSZheng Yan 	buffer = parse_events__scan_string(str, scanner);
1951ac20de6fSZheng Yan 
1952ac20de6fSZheng Yan #ifdef PARSER_DEBUG
1953ac20de6fSZheng Yan 	parse_events_debug = 1;
1954ae762641SJiri Olsa 	parse_events_set_debug(1, scanner);
1955ac20de6fSZheng Yan #endif
19565d9cdc11SArnaldo Carvalho de Melo 	ret = parse_events_parse(parse_state, scanner);
1957ac20de6fSZheng Yan 
1958ac20de6fSZheng Yan 	parse_events__flush_buffer(buffer, scanner);
1959ac20de6fSZheng Yan 	parse_events__delete_buffer(buffer, scanner);
1960ac20de6fSZheng Yan 	parse_events_lex_destroy(scanner);
1961ac20de6fSZheng Yan 	return ret;
1962ac20de6fSZheng Yan }
1963ac20de6fSZheng Yan 
196490e2b22dSJiri Olsa /*
196590e2b22dSJiri Olsa  * parse event config string, return a list of event terms.
196690e2b22dSJiri Olsa  */
196790e2b22dSJiri Olsa int parse_events_terms(struct list_head *terms, const char *str)
196890e2b22dSJiri Olsa {
19695d9cdc11SArnaldo Carvalho de Melo 	struct parse_events_state parse_state = {
197090e2b22dSJiri Olsa 		.terms  = NULL,
19711244a327SJiri Olsa 		.stoken = PE_START_TERMS,
197290e2b22dSJiri Olsa 	};
197390e2b22dSJiri Olsa 	int ret;
197490e2b22dSJiri Olsa 
19751244a327SJiri Olsa 	ret = parse_events__scanner(str, &parse_state);
19764929e95aSJiri Olsa 
197790e2b22dSJiri Olsa 	if (!ret) {
19785d9cdc11SArnaldo Carvalho de Melo 		list_splice(parse_state.terms, terms);
19795d9cdc11SArnaldo Carvalho de Melo 		zfree(&parse_state.terms);
198090e2b22dSJiri Olsa 		return 0;
198190e2b22dSJiri Olsa 	}
198290e2b22dSJiri Olsa 
19835d9cdc11SArnaldo Carvalho de Melo 	parse_events_terms__delete(parse_state.terms);
198490e2b22dSJiri Olsa 	return ret;
198590e2b22dSJiri Olsa }
198690e2b22dSJiri Olsa 
1987347c2f0aSIan Rogers __weak int arch_evlist__cmp(const struct evsel *lhs, const struct evsel *rhs)
1988347c2f0aSIan Rogers {
1989347c2f0aSIan Rogers 	/* Order by insertion index. */
1990347c2f0aSIan Rogers 	return lhs->core.idx - rhs->core.idx;
1991347c2f0aSIan Rogers }
1992347c2f0aSIan Rogers 
1993347c2f0aSIan Rogers static int evlist__cmp(void *state, const struct list_head *l, const struct list_head *r)
1994347c2f0aSIan Rogers {
1995347c2f0aSIan Rogers 	const struct perf_evsel *lhs_core = container_of(l, struct perf_evsel, node);
1996347c2f0aSIan Rogers 	const struct evsel *lhs = container_of(lhs_core, struct evsel, core);
1997347c2f0aSIan Rogers 	const struct perf_evsel *rhs_core = container_of(r, struct perf_evsel, node);
1998347c2f0aSIan Rogers 	const struct evsel *rhs = container_of(rhs_core, struct evsel, core);
1999347c2f0aSIan Rogers 	int *leader_idx = state;
2000347c2f0aSIan Rogers 	int lhs_leader_idx = *leader_idx, rhs_leader_idx = *leader_idx, ret;
2001347c2f0aSIan Rogers 	const char *lhs_pmu_name, *rhs_pmu_name;
2002327daf34SIan Rogers 	bool lhs_has_group = false, rhs_has_group = false;
2003347c2f0aSIan Rogers 
2004347c2f0aSIan Rogers 	/*
2005347c2f0aSIan Rogers 	 * First sort by grouping/leader. Read the leader idx only if the evsel
2006347c2f0aSIan Rogers 	 * is part of a group, as -1 indicates no group.
2007347c2f0aSIan Rogers 	 */
2008327daf34SIan Rogers 	if (lhs_core->leader != lhs_core || lhs_core->nr_members > 1) {
2009327daf34SIan Rogers 		lhs_has_group = true;
2010347c2f0aSIan Rogers 		lhs_leader_idx = lhs_core->leader->idx;
2011327daf34SIan Rogers 	}
2012327daf34SIan Rogers 	if (rhs_core->leader != rhs_core || rhs_core->nr_members > 1) {
2013327daf34SIan Rogers 		rhs_has_group = true;
2014347c2f0aSIan Rogers 		rhs_leader_idx = rhs_core->leader->idx;
2015327daf34SIan Rogers 	}
2016347c2f0aSIan Rogers 
2017347c2f0aSIan Rogers 	if (lhs_leader_idx != rhs_leader_idx)
2018347c2f0aSIan Rogers 		return lhs_leader_idx - rhs_leader_idx;
2019347c2f0aSIan Rogers 
2020327daf34SIan Rogers 	/* Group by PMU if there is a group. Groups can't span PMUs. */
2021327daf34SIan Rogers 	if (lhs_has_group && rhs_has_group) {
2022347c2f0aSIan Rogers 		lhs_pmu_name = evsel__group_pmu_name(lhs);
2023347c2f0aSIan Rogers 		rhs_pmu_name = evsel__group_pmu_name(rhs);
2024347c2f0aSIan Rogers 		ret = strcmp(lhs_pmu_name, rhs_pmu_name);
2025347c2f0aSIan Rogers 		if (ret)
2026347c2f0aSIan Rogers 			return ret;
2027327daf34SIan Rogers 	}
2028347c2f0aSIan Rogers 
2029347c2f0aSIan Rogers 	/* Architecture specific sorting. */
2030347c2f0aSIan Rogers 	return arch_evlist__cmp(lhs, rhs);
2031347c2f0aSIan Rogers }
2032347c2f0aSIan Rogers 
2033a4c7d7c5SIan Rogers static bool parse_events__sort_events_and_fix_groups(struct list_head *list)
2034347c2f0aSIan Rogers {
2035a4c7d7c5SIan Rogers 	int idx = 0, unsorted_idx = -1;
2036347c2f0aSIan Rogers 	struct evsel *pos, *cur_leader = NULL;
2037347c2f0aSIan Rogers 	struct perf_evsel *cur_leaders_grp = NULL;
2038a4c7d7c5SIan Rogers 	bool idx_changed = false;
2039a4c7d7c5SIan Rogers 	int orig_num_leaders = 0, num_leaders = 0;
2040347c2f0aSIan Rogers 
2041347c2f0aSIan Rogers 	/*
2042347c2f0aSIan Rogers 	 * Compute index to insert ungrouped events at. Place them where the
2043347c2f0aSIan Rogers 	 * first ungrouped event appears.
2044347c2f0aSIan Rogers 	 */
2045347c2f0aSIan Rogers 	list_for_each_entry(pos, list, core.node) {
2046347c2f0aSIan Rogers 		const struct evsel *pos_leader = evsel__leader(pos);
2047347c2f0aSIan Rogers 
2048a4c7d7c5SIan Rogers 		if (pos == pos_leader)
2049a4c7d7c5SIan Rogers 			orig_num_leaders++;
2050347c2f0aSIan Rogers 
2051a4c7d7c5SIan Rogers 		/*
2052a4c7d7c5SIan Rogers 		 * Ensure indexes are sequential, in particular for multiple
2053a4c7d7c5SIan Rogers 		 * event lists being merged. The indexes are used to detect when
2054a4c7d7c5SIan Rogers 		 * the user order is modified.
2055a4c7d7c5SIan Rogers 		 */
2056a4c7d7c5SIan Rogers 		pos->core.idx = idx++;
2057a4c7d7c5SIan Rogers 
2058a4c7d7c5SIan Rogers 		if (unsorted_idx == -1 && pos == pos_leader && pos->core.nr_members < 2)
2059a4c7d7c5SIan Rogers 			unsorted_idx = pos->core.idx;
2060347c2f0aSIan Rogers 	}
2061347c2f0aSIan Rogers 
2062347c2f0aSIan Rogers 	/* Sort events. */
2063a4c7d7c5SIan Rogers 	list_sort(&unsorted_idx, list, evlist__cmp);
2064347c2f0aSIan Rogers 
2065347c2f0aSIan Rogers 	/*
2066347c2f0aSIan Rogers 	 * Recompute groups, splitting for PMUs and adding groups for events
2067347c2f0aSIan Rogers 	 * that require them.
2068347c2f0aSIan Rogers 	 */
2069347c2f0aSIan Rogers 	idx = 0;
2070347c2f0aSIan Rogers 	list_for_each_entry(pos, list, core.node) {
2071347c2f0aSIan Rogers 		const struct evsel *pos_leader = evsel__leader(pos);
2072347c2f0aSIan Rogers 		const char *pos_pmu_name = evsel__group_pmu_name(pos);
2073347c2f0aSIan Rogers 		const char *cur_leader_pmu_name, *pos_leader_pmu_name;
2074347c2f0aSIan Rogers 		bool force_grouped = arch_evsel__must_be_in_group(pos);
2075347c2f0aSIan Rogers 
2076347c2f0aSIan Rogers 		/* Reset index and nr_members. */
2077a4c7d7c5SIan Rogers 		if (pos->core.idx != idx)
2078a4c7d7c5SIan Rogers 			idx_changed = true;
2079347c2f0aSIan Rogers 		pos->core.idx = idx++;
2080347c2f0aSIan Rogers 		pos->core.nr_members = 0;
2081347c2f0aSIan Rogers 
2082347c2f0aSIan Rogers 		/*
2083347c2f0aSIan Rogers 		 * Set the group leader respecting the given groupings and that
2084347c2f0aSIan Rogers 		 * groups can't span PMUs.
2085347c2f0aSIan Rogers 		 */
2086347c2f0aSIan Rogers 		if (!cur_leader)
2087347c2f0aSIan Rogers 			cur_leader = pos;
2088347c2f0aSIan Rogers 
2089347c2f0aSIan Rogers 		cur_leader_pmu_name = evsel__group_pmu_name(cur_leader);
2090347c2f0aSIan Rogers 		if ((cur_leaders_grp != pos->core.leader && !force_grouped) ||
2091347c2f0aSIan Rogers 		    strcmp(cur_leader_pmu_name, pos_pmu_name)) {
2092347c2f0aSIan Rogers 			/* Event is for a different group/PMU than last. */
2093347c2f0aSIan Rogers 			cur_leader = pos;
2094347c2f0aSIan Rogers 			/*
2095347c2f0aSIan Rogers 			 * Remember the leader's group before it is overwritten,
2096347c2f0aSIan Rogers 			 * so that later events match as being in the same
2097347c2f0aSIan Rogers 			 * group.
2098347c2f0aSIan Rogers 			 */
2099347c2f0aSIan Rogers 			cur_leaders_grp = pos->core.leader;
2100347c2f0aSIan Rogers 		}
2101347c2f0aSIan Rogers 		pos_leader_pmu_name = evsel__group_pmu_name(pos_leader);
2102347c2f0aSIan Rogers 		if (strcmp(pos_leader_pmu_name, pos_pmu_name) || force_grouped) {
2103347c2f0aSIan Rogers 			/*
2104347c2f0aSIan Rogers 			 * Event's PMU differs from its leader's. Groups can't
2105347c2f0aSIan Rogers 			 * span PMUs, so update leader from the group/PMU
2106347c2f0aSIan Rogers 			 * tracker.
2107347c2f0aSIan Rogers 			 */
2108347c2f0aSIan Rogers 			evsel__set_leader(pos, cur_leader);
2109347c2f0aSIan Rogers 		}
2110347c2f0aSIan Rogers 	}
2111347c2f0aSIan Rogers 	list_for_each_entry(pos, list, core.node) {
2112a4c7d7c5SIan Rogers 		struct evsel *pos_leader = evsel__leader(pos);
2113a4c7d7c5SIan Rogers 
2114a4c7d7c5SIan Rogers 		if (pos == pos_leader)
2115a4c7d7c5SIan Rogers 			num_leaders++;
2116a4c7d7c5SIan Rogers 		pos_leader->core.nr_members++;
2117347c2f0aSIan Rogers 	}
2118a4c7d7c5SIan Rogers 	return idx_changed || num_leaders != orig_num_leaders;
2119347c2f0aSIan Rogers }
2120347c2f0aSIan Rogers 
2121411ad22eSIan Rogers int __parse_events(struct evlist *evlist, const char *str, const char *pmu_filter,
2122a4c7d7c5SIan Rogers 		   struct parse_events_error *err, struct perf_pmu *fake_pmu,
2123a4c7d7c5SIan Rogers 		   bool warn_if_reordered)
212486470930SIngo Molnar {
21255d9cdc11SArnaldo Carvalho de Melo 	struct parse_events_state parse_state = {
21265d9cdc11SArnaldo Carvalho de Melo 		.list	  = LIST_HEAD_INIT(parse_state.list),
21276484d2f9SJiri Olsa 		.idx	  = evlist->core.nr_entries,
2128b39b8393SJiri Olsa 		.error	  = err,
21297630b3e2SWang Nan 		.evlist	  = evlist,
21301244a327SJiri Olsa 		.stoken	  = PE_START_EVENTS,
21313bf91aa5SArnaldo Carvalho de Melo 		.fake_pmu = fake_pmu,
2132411ad22eSIan Rogers 		.pmu_filter = pmu_filter,
21336fd1e519SIan Rogers 		.match_legacy_cache_terms = true,
213446010ab2SJiri Olsa 	};
213546010ab2SJiri Olsa 	int ret;
213686470930SIngo Molnar 
21371244a327SJiri Olsa 	ret = parse_events__scanner(str, &parse_state);
213815bfd2ccSWang Nan 
21398e8714c3SIan Rogers 	if (!ret && list_empty(&parse_state.list)) {
2140c23080a6SArnaldo Carvalho de Melo 		WARN_ONCE(true, "WARNING: event parser found nothing\n");
2141854f7363SWang Nan 		return -1;
2142854f7363SWang Nan 	}
2143854f7363SWang Nan 
2144a4c7d7c5SIan Rogers 	if (parse_events__sort_events_and_fix_groups(&parse_state.list) &&
2145a4c7d7c5SIan Rogers 	    warn_if_reordered && !parse_state.wild_card_pmus)
2146a4c7d7c5SIan Rogers 		pr_warning("WARNING: events were regrouped to match PMUs\n");
2147347c2f0aSIan Rogers 
21488e8714c3SIan Rogers 	/*
21498e8714c3SIan Rogers 	 * Add list to the evlist even with errors to allow callers to clean up.
21508e8714c3SIan Rogers 	 */
2151e414fd1aSArnaldo Carvalho de Melo 	evlist__splice_list_tail(evlist, &parse_state.list);
21528e8714c3SIan Rogers 
21538e8714c3SIan Rogers 	if (!ret) {
21548e8714c3SIan Rogers 		struct evsel *last;
21558e8714c3SIan Rogers 
2156515dbe48SJiri Olsa 		last = evlist__last(evlist);
215715bfd2ccSWang Nan 		last->cmdline_group_boundary = true;
215815bfd2ccSWang Nan 
215986470930SIngo Molnar 		return 0;
216086470930SIngo Molnar 	}
216186470930SIngo Molnar 
21625d7be90eSJiri Olsa 	/*
21635d7be90eSJiri Olsa 	 * There are 2 users - builtin-record and builtin-test objects.
2164c12995a5SJiri Olsa 	 * Both call evlist__delete in case of error, so we dont
21655d7be90eSJiri Olsa 	 * need to bother.
21665d7be90eSJiri Olsa 	 */
216789812fc8SJiri Olsa 	return ret;
216889812fc8SJiri Olsa }
216989812fc8SJiri Olsa 
2170806731a9SAdrian Hunter int parse_event(struct evlist *evlist, const char *str)
2171806731a9SAdrian Hunter {
2172806731a9SAdrian Hunter 	struct parse_events_error err;
2173806731a9SAdrian Hunter 	int ret;
2174806731a9SAdrian Hunter 
2175806731a9SAdrian Hunter 	parse_events_error__init(&err);
2176806731a9SAdrian Hunter 	ret = parse_events(evlist, str, &err);
2177806731a9SAdrian Hunter 	parse_events_error__exit(&err);
2178806731a9SAdrian Hunter 	return ret;
2179806731a9SAdrian Hunter }
2180806731a9SAdrian Hunter 
218107eafd4eSIan Rogers void parse_events_error__init(struct parse_events_error *err)
218207eafd4eSIan Rogers {
218307eafd4eSIan Rogers 	bzero(err, sizeof(*err));
218407eafd4eSIan Rogers }
218507eafd4eSIan Rogers 
218607eafd4eSIan Rogers void parse_events_error__exit(struct parse_events_error *err)
218707eafd4eSIan Rogers {
218807eafd4eSIan Rogers 	zfree(&err->str);
218907eafd4eSIan Rogers 	zfree(&err->help);
219007eafd4eSIan Rogers 	zfree(&err->first_str);
219107eafd4eSIan Rogers 	zfree(&err->first_help);
219207eafd4eSIan Rogers }
219307eafd4eSIan Rogers 
21946c191289SIan Rogers void parse_events_error__handle(struct parse_events_error *err, int idx,
21956c191289SIan Rogers 				char *str, char *help)
21966c191289SIan Rogers {
2197806731a9SAdrian Hunter 	if (WARN(!str || !err, "WARNING: failed to provide error string or struct\n"))
21982e828582SAdrian Hunter 		goto out_free;
21996c191289SIan Rogers 	switch (err->num_errors) {
22006c191289SIan Rogers 	case 0:
22016c191289SIan Rogers 		err->idx = idx;
22026c191289SIan Rogers 		err->str = str;
22036c191289SIan Rogers 		err->help = help;
22046c191289SIan Rogers 		break;
22056c191289SIan Rogers 	case 1:
22066c191289SIan Rogers 		err->first_idx = err->idx;
22076c191289SIan Rogers 		err->idx = idx;
22086c191289SIan Rogers 		err->first_str = err->str;
22096c191289SIan Rogers 		err->str = str;
22106c191289SIan Rogers 		err->first_help = err->help;
22116c191289SIan Rogers 		err->help = help;
22126c191289SIan Rogers 		break;
22136c191289SIan Rogers 	default:
22146c191289SIan Rogers 		pr_debug("Multiple errors dropping message: %s (%s)\n",
22156c191289SIan Rogers 			err->str, err->help);
22166c191289SIan Rogers 		free(err->str);
22176c191289SIan Rogers 		err->str = str;
22186c191289SIan Rogers 		free(err->help);
22196c191289SIan Rogers 		err->help = help;
22206c191289SIan Rogers 		break;
22216c191289SIan Rogers 	}
22226c191289SIan Rogers 	err->num_errors++;
22232e828582SAdrian Hunter 	return;
22242e828582SAdrian Hunter 
22252e828582SAdrian Hunter out_free:
22262e828582SAdrian Hunter 	free(str);
22272e828582SAdrian Hunter 	free(help);
22286c191289SIan Rogers }
22296c191289SIan Rogers 
2230b39b8393SJiri Olsa #define MAX_WIDTH 1000
2231b39b8393SJiri Olsa static int get_term_width(void)
2232b39b8393SJiri Olsa {
2233b39b8393SJiri Olsa 	struct winsize ws;
2234b39b8393SJiri Olsa 
2235b39b8393SJiri Olsa 	get_term_dimensions(&ws);
2236b39b8393SJiri Olsa 	return ws.ws_col > MAX_WIDTH ? MAX_WIDTH : ws.ws_col;
2237b39b8393SJiri Olsa }
2238b39b8393SJiri Olsa 
22396c191289SIan Rogers static void __parse_events_error__print(int err_idx, const char *err_str,
2240a910e466SIan Rogers 					const char *err_help, const char *event)
2241b39b8393SJiri Olsa {
2242b39b8393SJiri Olsa 	const char *str = "invalid or unsupported event: ";
2243b39b8393SJiri Olsa 	char _buf[MAX_WIDTH];
2244b39b8393SJiri Olsa 	char *buf = (char *) event;
2245b39b8393SJiri Olsa 	int idx = 0;
2246a910e466SIan Rogers 	if (err_str) {
2247b39b8393SJiri Olsa 		/* -2 for extra '' in the final fprintf */
2248b39b8393SJiri Olsa 		int width       = get_term_width() - 2;
2249b39b8393SJiri Olsa 		int len_event   = strlen(event);
2250b39b8393SJiri Olsa 		int len_str, max_len, cut = 0;
2251b39b8393SJiri Olsa 
2252b39b8393SJiri Olsa 		/*
2253b39b8393SJiri Olsa 		 * Maximum error index indent, we will cut
2254b39b8393SJiri Olsa 		 * the event string if it's bigger.
2255b39b8393SJiri Olsa 		 */
2256141b2d31SAdrian Hunter 		int max_err_idx = 13;
2257b39b8393SJiri Olsa 
2258b39b8393SJiri Olsa 		/*
2259b39b8393SJiri Olsa 		 * Let's be specific with the message when
2260b39b8393SJiri Olsa 		 * we have the precise error.
2261b39b8393SJiri Olsa 		 */
2262b39b8393SJiri Olsa 		str     = "event syntax error: ";
2263b39b8393SJiri Olsa 		len_str = strlen(str);
2264b39b8393SJiri Olsa 		max_len = width - len_str;
2265b39b8393SJiri Olsa 
2266b39b8393SJiri Olsa 		buf = _buf;
2267b39b8393SJiri Olsa 
2268bd1a0be5SAdam Buchbinder 		/* We're cutting from the beginning. */
2269a910e466SIan Rogers 		if (err_idx > max_err_idx)
2270a910e466SIan Rogers 			cut = err_idx - max_err_idx;
2271b39b8393SJiri Olsa 
2272b39b8393SJiri Olsa 		strncpy(buf, event + cut, max_len);
2273b39b8393SJiri Olsa 
2274b39b8393SJiri Olsa 		/* Mark cut parts with '..' on both sides. */
2275b39b8393SJiri Olsa 		if (cut)
2276b39b8393SJiri Olsa 			buf[0] = buf[1] = '.';
2277b39b8393SJiri Olsa 
2278b39b8393SJiri Olsa 		if ((len_event - cut) > max_len) {
2279b39b8393SJiri Olsa 			buf[max_len - 1] = buf[max_len - 2] = '.';
2280b39b8393SJiri Olsa 			buf[max_len] = 0;
2281b39b8393SJiri Olsa 		}
2282b39b8393SJiri Olsa 
2283a910e466SIan Rogers 		idx = len_str + err_idx - cut;
2284b39b8393SJiri Olsa 	}
2285b39b8393SJiri Olsa 
2286b39b8393SJiri Olsa 	fprintf(stderr, "%s'%s'\n", str, buf);
2287b39b8393SJiri Olsa 	if (idx) {
2288a910e466SIan Rogers 		fprintf(stderr, "%*s\\___ %s\n", idx + 1, "", err_str);
2289a910e466SIan Rogers 		if (err_help)
2290a910e466SIan Rogers 			fprintf(stderr, "\n%s\n", err_help);
2291a910e466SIan Rogers 	}
2292a910e466SIan Rogers }
2293a910e466SIan Rogers 
22946c191289SIan Rogers void parse_events_error__print(struct parse_events_error *err,
2295a910e466SIan Rogers 			       const char *event)
2296a910e466SIan Rogers {
2297a910e466SIan Rogers 	if (!err->num_errors)
2298a910e466SIan Rogers 		return;
2299a910e466SIan Rogers 
23006c191289SIan Rogers 	__parse_events_error__print(err->idx, err->str, err->help, event);
2301a910e466SIan Rogers 
2302a910e466SIan Rogers 	if (err->num_errors > 1) {
2303a910e466SIan Rogers 		fputs("\nInitial error:\n", stderr);
23046c191289SIan Rogers 		__parse_events_error__print(err->first_idx, err->first_str,
2305a910e466SIan Rogers 					err->first_help, event);
2306b39b8393SJiri Olsa 	}
2307b39b8393SJiri Olsa }
2308b39b8393SJiri Olsa 
2309b39b8393SJiri Olsa #undef MAX_WIDTH
2310b39b8393SJiri Olsa 
2311f120f9d5SJiri Olsa int parse_events_option(const struct option *opt, const char *str,
23121d037ca1SIrina Tirdea 			int unset __maybe_unused)
2313f120f9d5SJiri Olsa {
2314411ad22eSIan Rogers 	struct parse_events_option_args *args = opt->value;
2315a910e466SIan Rogers 	struct parse_events_error err;
2316a910e466SIan Rogers 	int ret;
2317a910e466SIan Rogers 
231807eafd4eSIan Rogers 	parse_events_error__init(&err);
2319411ad22eSIan Rogers 	ret = __parse_events(*args->evlistp, str, args->pmu_filter, &err,
2320411ad22eSIan Rogers 			     /*fake_pmu=*/NULL, /*warn_if_reordered=*/true);
23219175ce1fSAndi Kleen 
2322333b5665SAndi Kleen 	if (ret) {
23236c191289SIan Rogers 		parse_events_error__print(&err, str);
2324333b5665SAndi Kleen 		fprintf(stderr, "Run 'perf list' for a list of valid events\n");
2325333b5665SAndi Kleen 	}
232607eafd4eSIan Rogers 	parse_events_error__exit(&err);
2327b39b8393SJiri Olsa 
23289175ce1fSAndi Kleen 	return ret;
2329f120f9d5SJiri Olsa }
2330f120f9d5SJiri Olsa 
2331d0abbc3cSArnaldo Carvalho de Melo int parse_events_option_new_evlist(const struct option *opt, const char *str, int unset)
2332d0abbc3cSArnaldo Carvalho de Melo {
2333411ad22eSIan Rogers 	struct parse_events_option_args *args = opt->value;
2334d0abbc3cSArnaldo Carvalho de Melo 	int ret;
2335d0abbc3cSArnaldo Carvalho de Melo 
2336411ad22eSIan Rogers 	if (*args->evlistp == NULL) {
2337411ad22eSIan Rogers 		*args->evlistp = evlist__new();
2338d0abbc3cSArnaldo Carvalho de Melo 
2339411ad22eSIan Rogers 		if (*args->evlistp == NULL) {
2340d0abbc3cSArnaldo Carvalho de Melo 			fprintf(stderr, "Not enough memory to create evlist\n");
2341d0abbc3cSArnaldo Carvalho de Melo 			return -1;
2342d0abbc3cSArnaldo Carvalho de Melo 		}
2343d0abbc3cSArnaldo Carvalho de Melo 	}
2344d0abbc3cSArnaldo Carvalho de Melo 	ret = parse_events_option(opt, str, unset);
2345d0abbc3cSArnaldo Carvalho de Melo 	if (ret) {
2346411ad22eSIan Rogers 		evlist__delete(*args->evlistp);
2347411ad22eSIan Rogers 		*args->evlistp = NULL;
2348d0abbc3cSArnaldo Carvalho de Melo 	}
2349d0abbc3cSArnaldo Carvalho de Melo 
2350d0abbc3cSArnaldo Carvalho de Melo 	return ret;
2351d0abbc3cSArnaldo Carvalho de Melo }
2352d0abbc3cSArnaldo Carvalho de Melo 
23534ba1faa1SWang Nan static int
235463503dbaSJiri Olsa foreach_evsel_in_last_glob(struct evlist *evlist,
235532dcd021SJiri Olsa 			   int (*func)(struct evsel *evsel,
23564ba1faa1SWang Nan 				       const void *arg),
23574ba1faa1SWang Nan 			   const void *arg)
2358c171b552SLi Zefan {
235932dcd021SJiri Olsa 	struct evsel *last = NULL;
23604ba1faa1SWang Nan 	int err;
2361c171b552SLi Zefan 
2362854f7363SWang Nan 	/*
2363854f7363SWang Nan 	 * Don't return when list_empty, give func a chance to report
2364854f7363SWang Nan 	 * error when it found last == NULL.
2365854f7363SWang Nan 	 *
2366854f7363SWang Nan 	 * So no need to WARN here, let *func do this.
2367854f7363SWang Nan 	 */
23686484d2f9SJiri Olsa 	if (evlist->core.nr_entries > 0)
2369515dbe48SJiri Olsa 		last = evlist__last(evlist);
237069aad6f1SArnaldo Carvalho de Melo 
237115bfd2ccSWang Nan 	do {
23724ba1faa1SWang Nan 		err = (*func)(last, arg);
23734ba1faa1SWang Nan 		if (err)
2374c171b552SLi Zefan 			return -1;
23754ba1faa1SWang Nan 		if (!last)
23764ba1faa1SWang Nan 			return 0;
2377c171b552SLi Zefan 
2378ce9036a6SJiri Olsa 		if (last->core.node.prev == &evlist->core.entries)
237915bfd2ccSWang Nan 			return 0;
2380b27c4eceSJiri Olsa 		last = list_entry(last->core.node.prev, struct evsel, core.node);
238115bfd2ccSWang Nan 	} while (!last->cmdline_group_boundary);
238215bfd2ccSWang Nan 
2383c171b552SLi Zefan 	return 0;
2384c171b552SLi Zefan }
2385c171b552SLi Zefan 
238632dcd021SJiri Olsa static int set_filter(struct evsel *evsel, const void *arg)
23874ba1faa1SWang Nan {
23884ba1faa1SWang Nan 	const char *str = arg;
23891e857484SMathieu Poirier 	bool found = false;
23901e857484SMathieu Poirier 	int nr_addr_filters = 0;
23911e857484SMathieu Poirier 	struct perf_pmu *pmu = NULL;
23924ba1faa1SWang Nan 
239349836f78SJack Henschel 	if (evsel == NULL) {
239449836f78SJack Henschel 		fprintf(stderr,
239549836f78SJack Henschel 			"--filter option should follow a -e tracepoint or HW tracer option\n");
239649836f78SJack Henschel 		return -1;
239749836f78SJack Henschel 	}
23984ba1faa1SWang Nan 
23991fc632ceSJiri Olsa 	if (evsel->core.attr.type == PERF_TYPE_TRACEPOINT) {
2400ad681adfSArnaldo Carvalho de Melo 		if (evsel__append_tp_filter(evsel, str) < 0) {
24014ba1faa1SWang Nan 			fprintf(stderr,
24024ba1faa1SWang Nan 				"not enough memory to hold filter string\n");
24034ba1faa1SWang Nan 			return -1;
24044ba1faa1SWang Nan 		}
24054ba1faa1SWang Nan 
24064ba1faa1SWang Nan 		return 0;
24074ba1faa1SWang Nan 	}
24084ba1faa1SWang Nan 
24091eaf496eSIan Rogers 	while ((pmu = perf_pmus__scan(pmu)) != NULL)
24101fc632ceSJiri Olsa 		if (pmu->type == evsel->core.attr.type) {
24111e857484SMathieu Poirier 			found = true;
24121e857484SMathieu Poirier 			break;
24131e857484SMathieu Poirier 		}
24141e857484SMathieu Poirier 
24151e857484SMathieu Poirier 	if (found)
24161e857484SMathieu Poirier 		perf_pmu__scan_file(pmu, "nr_addr_filters",
24171e857484SMathieu Poirier 				    "%d", &nr_addr_filters);
24181e857484SMathieu Poirier 
2419d180aa56SNamhyung Kim 	if (!nr_addr_filters)
2420d180aa56SNamhyung Kim 		return perf_bpf_filter__parse(&evsel->bpf_filters, str);
24211e857484SMathieu Poirier 
2422ad681adfSArnaldo Carvalho de Melo 	if (evsel__append_addr_filter(evsel, str) < 0) {
24231e857484SMathieu Poirier 		fprintf(stderr,
24241e857484SMathieu Poirier 			"not enough memory to hold filter string\n");
24251e857484SMathieu Poirier 		return -1;
24261e857484SMathieu Poirier 	}
24271e857484SMathieu Poirier 
24281e857484SMathieu Poirier 	return 0;
24291e857484SMathieu Poirier }
24301e857484SMathieu Poirier 
24314ba1faa1SWang Nan int parse_filter(const struct option *opt, const char *str,
24324ba1faa1SWang Nan 		 int unset __maybe_unused)
24334ba1faa1SWang Nan {
243463503dbaSJiri Olsa 	struct evlist *evlist = *(struct evlist **)opt->value;
24354ba1faa1SWang Nan 
24364ba1faa1SWang Nan 	return foreach_evsel_in_last_glob(evlist, set_filter,
24374ba1faa1SWang Nan 					  (const void *)str);
24384ba1faa1SWang Nan }
24394ba1faa1SWang Nan 
244032dcd021SJiri Olsa static int add_exclude_perf_filter(struct evsel *evsel,
24414ba1faa1SWang Nan 				   const void *arg __maybe_unused)
24424ba1faa1SWang Nan {
24434ba1faa1SWang Nan 	char new_filter[64];
24444ba1faa1SWang Nan 
24451fc632ceSJiri Olsa 	if (evsel == NULL || evsel->core.attr.type != PERF_TYPE_TRACEPOINT) {
24464ba1faa1SWang Nan 		fprintf(stderr,
24474ba1faa1SWang Nan 			"--exclude-perf option should follow a -e tracepoint option\n");
24484ba1faa1SWang Nan 		return -1;
24494ba1faa1SWang Nan 	}
24504ba1faa1SWang Nan 
24514ba1faa1SWang Nan 	snprintf(new_filter, sizeof(new_filter), "common_pid != %d", getpid());
24524ba1faa1SWang Nan 
2453ad681adfSArnaldo Carvalho de Melo 	if (evsel__append_tp_filter(evsel, new_filter) < 0) {
24544ba1faa1SWang Nan 		fprintf(stderr,
24554ba1faa1SWang Nan 			"not enough memory to hold filter string\n");
24564ba1faa1SWang Nan 		return -1;
24574ba1faa1SWang Nan 	}
24584ba1faa1SWang Nan 
24594ba1faa1SWang Nan 	return 0;
24604ba1faa1SWang Nan }
24614ba1faa1SWang Nan 
24624ba1faa1SWang Nan int exclude_perf(const struct option *opt,
24634ba1faa1SWang Nan 		 const char *arg __maybe_unused,
24644ba1faa1SWang Nan 		 int unset __maybe_unused)
24654ba1faa1SWang Nan {
246663503dbaSJiri Olsa 	struct evlist *evlist = *(struct evlist **)opt->value;
24674ba1faa1SWang Nan 
24684ba1faa1SWang Nan 	return foreach_evsel_in_last_glob(evlist, add_exclude_perf_filter,
24694ba1faa1SWang Nan 					  NULL);
24704ba1faa1SWang Nan }
24714ba1faa1SWang Nan 
24726cee6cd3SArnaldo Carvalho de Melo int parse_events__is_hardcoded_term(struct parse_events_term *term)
24738f707d84SJiri Olsa {
247416fa7e82SJiri Olsa 	return term->type_term != PARSE_EVENTS__TERM_TYPE_USER;
24758f707d84SJiri Olsa }
24768f707d84SJiri Olsa 
247767b49b38SJiri Olsa static int new_term(struct parse_events_term **_term,
247867b49b38SJiri Olsa 		    struct parse_events_term *temp,
247967b49b38SJiri Olsa 		    char *str, u64 num)
24808f707d84SJiri Olsa {
24816cee6cd3SArnaldo Carvalho de Melo 	struct parse_events_term *term;
24828f707d84SJiri Olsa 
248367b49b38SJiri Olsa 	term = malloc(sizeof(*term));
24848f707d84SJiri Olsa 	if (!term)
24858f707d84SJiri Olsa 		return -ENOMEM;
24868f707d84SJiri Olsa 
248767b49b38SJiri Olsa 	*term = *temp;
24888f707d84SJiri Olsa 	INIT_LIST_HEAD(&term->list);
2489c2f1ceadSAndi Kleen 	term->weak = false;
24908f707d84SJiri Olsa 
249167b49b38SJiri Olsa 	switch (term->type_val) {
24928f707d84SJiri Olsa 	case PARSE_EVENTS__TERM_TYPE_NUM:
24938f707d84SJiri Olsa 		term->val.num = num;
24948f707d84SJiri Olsa 		break;
24958f707d84SJiri Olsa 	case PARSE_EVENTS__TERM_TYPE_STR:
24968f707d84SJiri Olsa 		term->val.str = str;
24978f707d84SJiri Olsa 		break;
24988f707d84SJiri Olsa 	default:
24994be8be6bSAdrian Hunter 		free(term);
25008f707d84SJiri Olsa 		return -EINVAL;
25018f707d84SJiri Olsa 	}
25028f707d84SJiri Olsa 
25038f707d84SJiri Olsa 	*_term = term;
25048f707d84SJiri Olsa 	return 0;
25058f707d84SJiri Olsa }
25068f707d84SJiri Olsa 
25076cee6cd3SArnaldo Carvalho de Melo int parse_events_term__num(struct parse_events_term **term,
2508cecf3a2eSJiri Olsa 			   int type_term, char *config, u64 num,
250999e7138eSJiri Olsa 			   bool no_value,
2510bb78ce7dSAdrian Hunter 			   void *loc_term_, void *loc_val_)
251116fa7e82SJiri Olsa {
2512bb78ce7dSAdrian Hunter 	YYLTYPE *loc_term = loc_term_;
2513bb78ce7dSAdrian Hunter 	YYLTYPE *loc_val = loc_val_;
2514bb78ce7dSAdrian Hunter 
251567b49b38SJiri Olsa 	struct parse_events_term temp = {
251667b49b38SJiri Olsa 		.type_val  = PARSE_EVENTS__TERM_TYPE_NUM,
251767b49b38SJiri Olsa 		.type_term = type_term,
25184f9d4f8aSJohn Garry 		.config    = config ? : strdup(config_term_names[type_term]),
251999e7138eSJiri Olsa 		.no_value  = no_value,
252067b49b38SJiri Olsa 		.err_term  = loc_term ? loc_term->first_column : 0,
252167b49b38SJiri Olsa 		.err_val   = loc_val  ? loc_val->first_column  : 0,
252267b49b38SJiri Olsa 	};
252367b49b38SJiri Olsa 
252467b49b38SJiri Olsa 	return new_term(term, &temp, NULL, num);
252516fa7e82SJiri Olsa }
252616fa7e82SJiri Olsa 
25276cee6cd3SArnaldo Carvalho de Melo int parse_events_term__str(struct parse_events_term **term,
2528cecf3a2eSJiri Olsa 			   int type_term, char *config, char *str,
2529bb78ce7dSAdrian Hunter 			   void *loc_term_, void *loc_val_)
253016fa7e82SJiri Olsa {
2531bb78ce7dSAdrian Hunter 	YYLTYPE *loc_term = loc_term_;
2532bb78ce7dSAdrian Hunter 	YYLTYPE *loc_val = loc_val_;
2533bb78ce7dSAdrian Hunter 
253467b49b38SJiri Olsa 	struct parse_events_term temp = {
253567b49b38SJiri Olsa 		.type_val  = PARSE_EVENTS__TERM_TYPE_STR,
253667b49b38SJiri Olsa 		.type_term = type_term,
253767b49b38SJiri Olsa 		.config    = config,
253867b49b38SJiri Olsa 		.err_term  = loc_term ? loc_term->first_column : 0,
253967b49b38SJiri Olsa 		.err_val   = loc_val  ? loc_val->first_column  : 0,
254067b49b38SJiri Olsa 	};
254167b49b38SJiri Olsa 
254267b49b38SJiri Olsa 	return new_term(term, &temp, str, 0);
254316fa7e82SJiri Olsa }
254416fa7e82SJiri Olsa 
2545e831f3ccSIan Rogers int parse_events_term__term(struct parse_events_term **term,
2546e831f3ccSIan Rogers 			    int term_lhs, int term_rhs,
2547e831f3ccSIan Rogers 			    void *loc_term, void *loc_val)
2548e831f3ccSIan Rogers {
2549e831f3ccSIan Rogers 	return parse_events_term__str(term, term_lhs, NULL,
2550e831f3ccSIan Rogers 				      strdup(config_term_names[term_rhs]),
2551e831f3ccSIan Rogers 				      loc_term, loc_val);
2552e831f3ccSIan Rogers }
2553e831f3ccSIan Rogers 
25546cee6cd3SArnaldo Carvalho de Melo int parse_events_term__clone(struct parse_events_term **new,
25556cee6cd3SArnaldo Carvalho de Melo 			     struct parse_events_term *term)
2556a6146d50SZheng Yan {
2557b6645a72SIan Rogers 	char *str;
255867b49b38SJiri Olsa 	struct parse_events_term temp = {
255967b49b38SJiri Olsa 		.type_val  = term->type_val,
256067b49b38SJiri Olsa 		.type_term = term->type_term,
2561b6645a72SIan Rogers 		.config    = NULL,
256267b49b38SJiri Olsa 		.err_term  = term->err_term,
256367b49b38SJiri Olsa 		.err_val   = term->err_val,
256467b49b38SJiri Olsa 	};
256567b49b38SJiri Olsa 
2566b6645a72SIan Rogers 	if (term->config) {
2567b6645a72SIan Rogers 		temp.config = strdup(term->config);
2568b6645a72SIan Rogers 		if (!temp.config)
2569b6645a72SIan Rogers 			return -ENOMEM;
2570b6645a72SIan Rogers 	}
2571b6645a72SIan Rogers 	if (term->type_val == PARSE_EVENTS__TERM_TYPE_NUM)
2572b6645a72SIan Rogers 		return new_term(new, &temp, NULL, term->val.num);
2573b6645a72SIan Rogers 
2574b6645a72SIan Rogers 	str = strdup(term->val.str);
2575b6645a72SIan Rogers 	if (!str)
2576b6645a72SIan Rogers 		return -ENOMEM;
2577b6645a72SIan Rogers 	return new_term(new, &temp, str, 0);
2578a6146d50SZheng Yan }
2579a6146d50SZheng Yan 
25801dc92556SIan Rogers void parse_events_term__delete(struct parse_events_term *term)
25811dc92556SIan Rogers {
25821dc92556SIan Rogers 	if (term->array.nr_ranges)
25831dc92556SIan Rogers 		zfree(&term->array.ranges);
25841dc92556SIan Rogers 
25851dc92556SIan Rogers 	if (term->type_val != PARSE_EVENTS__TERM_TYPE_NUM)
25861dc92556SIan Rogers 		zfree(&term->val.str);
25871dc92556SIan Rogers 
25881dc92556SIan Rogers 	zfree(&term->config);
25891dc92556SIan Rogers 	free(term);
25901dc92556SIan Rogers }
25911dc92556SIan Rogers 
25928255718fSAndi Kleen int parse_events_copy_term_list(struct list_head *old,
25938255718fSAndi Kleen 				 struct list_head **new)
25948255718fSAndi Kleen {
25958255718fSAndi Kleen 	struct parse_events_term *term, *n;
25968255718fSAndi Kleen 	int ret;
25978255718fSAndi Kleen 
25988255718fSAndi Kleen 	if (!old) {
25998255718fSAndi Kleen 		*new = NULL;
26008255718fSAndi Kleen 		return 0;
26018255718fSAndi Kleen 	}
26028255718fSAndi Kleen 
26038255718fSAndi Kleen 	*new = malloc(sizeof(struct list_head));
26048255718fSAndi Kleen 	if (!*new)
26058255718fSAndi Kleen 		return -ENOMEM;
26068255718fSAndi Kleen 	INIT_LIST_HEAD(*new);
26078255718fSAndi Kleen 
26088255718fSAndi Kleen 	list_for_each_entry (term, old, list) {
26098255718fSAndi Kleen 		ret = parse_events_term__clone(&n, term);
26108255718fSAndi Kleen 		if (ret)
26118255718fSAndi Kleen 			return ret;
26128255718fSAndi Kleen 		list_add_tail(&n->list, *new);
26138255718fSAndi Kleen 	}
26148255718fSAndi Kleen 	return 0;
26158255718fSAndi Kleen }
26168255718fSAndi Kleen 
2617fc0a2c1dSArnaldo Carvalho de Melo void parse_events_terms__purge(struct list_head *terms)
26188f707d84SJiri Olsa {
26196cee6cd3SArnaldo Carvalho de Melo 	struct parse_events_term *term, *h;
26208f707d84SJiri Olsa 
2621a8adfcebSWang Nan 	list_for_each_entry_safe(term, h, terms, list) {
2622a8adfcebSWang Nan 		list_del_init(&term->list);
26231dc92556SIan Rogers 		parse_events_term__delete(term);
26248f707d84SJiri Olsa 	}
2625a8adfcebSWang Nan }
2626b39b8393SJiri Olsa 
26272146afc6SArnaldo Carvalho de Melo void parse_events_terms__delete(struct list_head *terms)
2628fc0a2c1dSArnaldo Carvalho de Melo {
26292146afc6SArnaldo Carvalho de Melo 	if (!terms)
26302146afc6SArnaldo Carvalho de Melo 		return;
2631fc0a2c1dSArnaldo Carvalho de Melo 	parse_events_terms__purge(terms);
2632d20a5f2bSWang Nan 	free(terms);
2633fc0a2c1dSArnaldo Carvalho de Melo }
2634fc0a2c1dSArnaldo Carvalho de Melo 
26352d055bf2SWang Nan void parse_events__clear_array(struct parse_events_array *a)
26362d055bf2SWang Nan {
2637360e071bSTaeung Song 	zfree(&a->ranges);
26382d055bf2SWang Nan }
26392d055bf2SWang Nan 
26405d9cdc11SArnaldo Carvalho de Melo void parse_events_evlist_error(struct parse_events_state *parse_state,
2641b39b8393SJiri Olsa 			       int idx, const char *str)
2642b39b8393SJiri Olsa {
2643448d732cSIan Rogers 	if (!parse_state->error)
2644a6ced2beSAdrian Hunter 		return;
2645448d732cSIan Rogers 
26466c191289SIan Rogers 	parse_events_error__handle(parse_state->error, idx, strdup(str), NULL);
2647b39b8393SJiri Olsa }
2648ffeb883eSHe Kuang 
264917cb5f84SWang Nan static void config_terms_list(char *buf, size_t buf_sz)
265017cb5f84SWang Nan {
265117cb5f84SWang Nan 	int i;
265217cb5f84SWang Nan 	bool first = true;
265317cb5f84SWang Nan 
265417cb5f84SWang Nan 	buf[0] = '\0';
265517cb5f84SWang Nan 	for (i = 0; i < __PARSE_EVENTS__TERM_TYPE_NR; i++) {
265617cb5f84SWang Nan 		const char *name = config_term_names[i];
265717cb5f84SWang Nan 
26581669e509SWang Nan 		if (!config_term_avail(i, NULL))
26591669e509SWang Nan 			continue;
266017cb5f84SWang Nan 		if (!name)
266117cb5f84SWang Nan 			continue;
266217cb5f84SWang Nan 		if (name[0] == '<')
266317cb5f84SWang Nan 			continue;
266417cb5f84SWang Nan 
266517cb5f84SWang Nan 		if (strlen(buf) + strlen(name) + 2 >= buf_sz)
266617cb5f84SWang Nan 			return;
266717cb5f84SWang Nan 
266817cb5f84SWang Nan 		if (!first)
266917cb5f84SWang Nan 			strcat(buf, ",");
267017cb5f84SWang Nan 		else
267117cb5f84SWang Nan 			first = false;
267217cb5f84SWang Nan 		strcat(buf, name);
267317cb5f84SWang Nan 	}
267417cb5f84SWang Nan }
267517cb5f84SWang Nan 
2676ffeb883eSHe Kuang /*
2677ffeb883eSHe Kuang  * Return string contains valid config terms of an event.
2678ffeb883eSHe Kuang  * @additional_terms: For terms such as PMU sysfs terms.
2679ffeb883eSHe Kuang  */
2680ffeb883eSHe Kuang char *parse_events_formats_error_string(char *additional_terms)
2681ffeb883eSHe Kuang {
2682ffeb883eSHe Kuang 	char *str;
2683626a6b78SWang Nan 	/* "no-overwrite" is the longest name */
268417cb5f84SWang Nan 	char static_terms[__PARSE_EVENTS__TERM_TYPE_NR *
2685626a6b78SWang Nan 			  (sizeof("no-overwrite") - 1)];
2686ffeb883eSHe Kuang 
268717cb5f84SWang Nan 	config_terms_list(static_terms, sizeof(static_terms));
2688ffeb883eSHe Kuang 	/* valid terms */
2689ffeb883eSHe Kuang 	if (additional_terms) {
269026dee028SWang Nan 		if (asprintf(&str, "valid terms: %s,%s",
269126dee028SWang Nan 			     additional_terms, static_terms) < 0)
2692ffeb883eSHe Kuang 			goto fail;
2693ffeb883eSHe Kuang 	} else {
269426dee028SWang Nan 		if (asprintf(&str, "valid terms: %s", static_terms) < 0)
2695ffeb883eSHe Kuang 			goto fail;
2696ffeb883eSHe Kuang 	}
2697ffeb883eSHe Kuang 	return str;
2698ffeb883eSHe Kuang 
2699ffeb883eSHe Kuang fail:
2700ffeb883eSHe Kuang 	return NULL;
2701ffeb883eSHe Kuang }
2702