1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef __PERF_PARSE_EVENTS_H
3 #define __PERF_PARSE_EVENTS_H
4 /*
5  * Parse symbolic events/counts passed in as options:
6  */
7 
8 #include <linux/list.h>
9 #include <stdbool.h>
10 #include <linux/types.h>
11 #include <linux/perf_event.h>
12 #include <stdio.h>
13 #include <string.h>
14 
15 struct evsel;
16 struct evlist;
17 struct parse_events_error;
18 
19 struct option;
20 struct perf_pmu;
21 struct strbuf;
22 
23 const char *event_type(int type);
24 
25 /* Arguments encoded in opt->value. */
26 struct parse_events_option_args {
27 	struct evlist **evlistp;
28 	const char *pmu_filter;
29 };
30 int parse_events_option(const struct option *opt, const char *str, int unset);
31 int parse_events_option_new_evlist(const struct option *opt, const char *str, int unset);
32 __attribute__((nonnull(1, 2, 4)))
33 int __parse_events(struct evlist *evlist, const char *str, const char *pmu_filter,
34 		   struct parse_events_error *error, struct perf_pmu *fake_pmu,
35 		   bool warn_if_reordered);
36 
37 __attribute__((nonnull(1, 2, 3)))
parse_events(struct evlist * evlist,const char * str,struct parse_events_error * err)38 static inline int parse_events(struct evlist *evlist, const char *str,
39 			       struct parse_events_error *err)
40 {
41 	return __parse_events(evlist, str, /*pmu_filter=*/NULL, err, /*fake_pmu=*/NULL,
42 			      /*warn_if_reordered=*/true);
43 }
44 
45 int parse_event(struct evlist *evlist, const char *str);
46 
47 int parse_events_terms(struct list_head *terms, const char *str, FILE *input);
48 int parse_filter(const struct option *opt, const char *str, int unset);
49 int exclude_perf(const struct option *opt, const char *arg, int unset);
50 
51 enum parse_events__term_val_type {
52 	PARSE_EVENTS__TERM_TYPE_NUM,
53 	PARSE_EVENTS__TERM_TYPE_STR,
54 };
55 
56 enum parse_events__term_type {
57 	PARSE_EVENTS__TERM_TYPE_USER,
58 	PARSE_EVENTS__TERM_TYPE_CONFIG,
59 	PARSE_EVENTS__TERM_TYPE_CONFIG1,
60 	PARSE_EVENTS__TERM_TYPE_CONFIG2,
61 	PARSE_EVENTS__TERM_TYPE_CONFIG3,
62 	PARSE_EVENTS__TERM_TYPE_NAME,
63 	PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD,
64 	PARSE_EVENTS__TERM_TYPE_SAMPLE_FREQ,
65 	PARSE_EVENTS__TERM_TYPE_BRANCH_SAMPLE_TYPE,
66 	PARSE_EVENTS__TERM_TYPE_TIME,
67 	PARSE_EVENTS__TERM_TYPE_CALLGRAPH,
68 	PARSE_EVENTS__TERM_TYPE_STACKSIZE,
69 	PARSE_EVENTS__TERM_TYPE_NOINHERIT,
70 	PARSE_EVENTS__TERM_TYPE_INHERIT,
71 	PARSE_EVENTS__TERM_TYPE_MAX_STACK,
72 	PARSE_EVENTS__TERM_TYPE_MAX_EVENTS,
73 	PARSE_EVENTS__TERM_TYPE_NOOVERWRITE,
74 	PARSE_EVENTS__TERM_TYPE_OVERWRITE,
75 	PARSE_EVENTS__TERM_TYPE_DRV_CFG,
76 	PARSE_EVENTS__TERM_TYPE_PERCORE,
77 	PARSE_EVENTS__TERM_TYPE_AUX_OUTPUT,
78 	PARSE_EVENTS__TERM_TYPE_AUX_SAMPLE_SIZE,
79 	PARSE_EVENTS__TERM_TYPE_METRIC_ID,
80 	PARSE_EVENTS__TERM_TYPE_RAW,
81 	PARSE_EVENTS__TERM_TYPE_LEGACY_CACHE,
82 	PARSE_EVENTS__TERM_TYPE_HARDWARE,
83 #define	__PARSE_EVENTS__TERM_TYPE_NR (PARSE_EVENTS__TERM_TYPE_HARDWARE + 1)
84 };
85 
86 struct parse_events_term {
87 	/** @list: The term list the term is a part of. */
88 	struct list_head list;
89 	/**
90 	 * @config: The left-hand side of a term assignment, so the term
91 	 * "event=8" would have the config be "event"
92 	 */
93 	const char *config;
94 	/**
95 	 * @val: The right-hand side of a term assignment that can either be a
96 	 * string or a number depending on type_val.
97 	 */
98 	union {
99 		char *str;
100 		u64  num;
101 	} val;
102 	/** @type_val: The union variable in val to be used for the term. */
103 	enum parse_events__term_val_type type_val;
104 	/**
105 	 * @type_term: A predefined term type or PARSE_EVENTS__TERM_TYPE_USER
106 	 * when not inbuilt.
107 	 */
108 	enum parse_events__term_type type_term;
109 	/**
110 	 * @err_term: The column index of the term from parsing, used during
111 	 * error output.
112 	 */
113 	int err_term;
114 	/**
115 	 * @err_val: The column index of the val from parsing, used during error
116 	 * output.
117 	 */
118 	int err_val;
119 	/** @used: Was the term used during parameterized-eval. */
120 	bool used;
121 	/**
122 	 * @weak: A term from the sysfs or json encoding of an event that
123 	 * shouldn't override terms coming from the command line.
124 	 */
125 	bool weak;
126 	/**
127 	 * @no_value: Is there no value. If a numeric term has no value then the
128 	 * value is assumed to be 1. An event name also has no value.
129 	 */
130 	bool no_value;
131 };
132 
133 struct parse_events_error {
134 	int   num_errors;       /* number of errors encountered */
135 	int   idx;	/* index in the parsed string */
136 	char *str;      /* string to display at the index */
137 	char *help;	/* optional help string */
138 	int   first_idx;/* as above, but for the first encountered error */
139 	char *first_str;
140 	char *first_help;
141 };
142 
143 struct parse_events_state {
144 	/* The list parsed events are placed on. */
145 	struct list_head	   list;
146 	/* The updated index used by entries as they are added. */
147 	int			   idx;
148 	/* Error information. */
149 	struct parse_events_error *error;
150 	/* Holds returned terms for term parsing. */
151 	struct list_head	  *terms;
152 	/* Start token. */
153 	int			   stoken;
154 	/* Special fake PMU marker for testing. */
155 	struct perf_pmu		  *fake_pmu;
156 	/* If non-null, when wildcard matching only match the given PMU. */
157 	const char		  *pmu_filter;
158 	/* Should PE_LEGACY_NAME tokens be generated for config terms? */
159 	bool			   match_legacy_cache_terms;
160 	/* Were multiple PMUs scanned to find events? */
161 	bool			   wild_card_pmus;
162 };
163 
164 bool parse_events__filter_pmu(const struct parse_events_state *parse_state,
165 			      const struct perf_pmu *pmu);
166 void parse_events__shrink_config_terms(void);
167 int parse_events__is_hardcoded_term(struct parse_events_term *term);
168 int parse_events_term__num(struct parse_events_term **term,
169 			   enum parse_events__term_type type_term,
170 			   const char *config, u64 num,
171 			   bool novalue,
172 			   void *loc_term, void *loc_val);
173 int parse_events_term__str(struct parse_events_term **term,
174 			   enum parse_events__term_type type_term,
175 			   char *config, char *str,
176 			   void *loc_term, void *loc_val);
177 int parse_events_term__term(struct parse_events_term **term,
178 			    enum parse_events__term_type term_lhs,
179 			    enum parse_events__term_type term_rhs,
180 			    void *loc_term, void *loc_val);
181 int parse_events_term__clone(struct parse_events_term **new,
182 			     struct parse_events_term *term);
183 void parse_events_term__delete(struct parse_events_term *term);
184 void parse_events_terms__delete(struct list_head *terms);
185 void parse_events_terms__purge(struct list_head *terms);
186 int parse_events_term__to_strbuf(struct list_head *term_list, struct strbuf *sb);
187 int parse_events__modifier_event(struct list_head *list, char *str, bool add);
188 int parse_events__modifier_group(struct list_head *list, char *event_mod);
189 int parse_events_name(struct list_head *list, const char *name);
190 int parse_events_add_tracepoint(struct list_head *list, int *idx,
191 				const char *sys, const char *event,
192 				struct parse_events_error *error,
193 				struct list_head *head_config, void *loc);
194 int parse_events_add_numeric(struct parse_events_state *parse_state,
195 			     struct list_head *list,
196 			     u32 type, u64 config,
197 			     struct list_head *head_config,
198 			     bool wildcard);
199 int parse_events_add_tool(struct parse_events_state *parse_state,
200 			  struct list_head *list,
201 			  int tool_event);
202 int parse_events_add_cache(struct list_head *list, int *idx, const char *name,
203 			   struct parse_events_state *parse_state,
204 			   struct list_head *head_config);
205 int parse_events__decode_legacy_cache(const char *name, int pmu_type, __u64 *config);
206 int parse_events_add_breakpoint(struct parse_events_state *parse_state,
207 				struct list_head *list,
208 				u64 addr, char *type, u64 len,
209 				struct list_head *head_config);
210 int parse_events_add_pmu(struct parse_events_state *parse_state,
211 			 struct list_head *list, const char *name,
212 			 struct list_head *head_config,
213 			bool auto_merge_stats, void *loc);
214 
215 struct evsel *parse_events__add_event(int idx, struct perf_event_attr *attr,
216 				      const char *name, const char *metric_id,
217 				      struct perf_pmu *pmu);
218 
219 int parse_events_multi_pmu_add(struct parse_events_state *parse_state,
220 			       char *str,
221 			       struct list_head *head_config,
222 			       struct list_head **listp, void *loc);
223 
224 int parse_events_copy_term_list(struct list_head *old,
225 				 struct list_head **new);
226 
227 void parse_events__set_leader(char *name, struct list_head *list);
228 void parse_events_update_lists(struct list_head *list_event,
229 			       struct list_head *list_all);
230 void parse_events_evlist_error(struct parse_events_state *parse_state,
231 			       int idx, const char *str);
232 
233 struct event_symbol {
234 	const char	*symbol;
235 	const char	*alias;
236 };
237 extern struct event_symbol event_symbols_hw[];
238 extern struct event_symbol event_symbols_sw[];
239 
240 char *parse_events_formats_error_string(char *additional_terms);
241 
242 void parse_events_error__init(struct parse_events_error *err);
243 void parse_events_error__exit(struct parse_events_error *err);
244 void parse_events_error__handle(struct parse_events_error *err, int idx,
245 				char *str, char *help);
246 void parse_events_error__print(struct parse_events_error *err,
247 			       const char *event);
248 
249 #ifdef HAVE_LIBELF_SUPPORT
250 /*
251  * If the probe point starts with '%',
252  * or starts with "sdt_" and has a ':' but no '=',
253  * then it should be a SDT/cached probe point.
254  */
is_sdt_event(char * str)255 static inline bool is_sdt_event(char *str)
256 {
257 	return (str[0] == '%' ||
258 		(!strncmp(str, "sdt_", 4) &&
259 		 !!strchr(str, ':') && !strchr(str, '=')));
260 }
261 #else
is_sdt_event(char * str __maybe_unused)262 static inline bool is_sdt_event(char *str __maybe_unused)
263 {
264 	return false;
265 }
266 #endif /* HAVE_LIBELF_SUPPORT */
267 
268 #endif /* __PERF_PARSE_EVENTS_H */
269