xref: /openbmc/linux/tools/perf/util/parse-events.l (revision c4c3c32d)
1 
2 %option reentrant
3 %option bison-bridge
4 %option prefix="parse_events_"
5 %option stack
6 %option bison-locations
7 %option yylineno
8 %option reject
9 
10 %{
11 #include <errno.h>
12 #include <sys/types.h>
13 #include <sys/stat.h>
14 #include <unistd.h>
15 #include "parse-events.h"
16 #include "parse-events-bison.h"
17 #include "evsel.h"
18 
19 char *parse_events_get_text(yyscan_t yyscanner);
20 YYSTYPE *parse_events_get_lval(yyscan_t yyscanner);
21 
22 static int __value(YYSTYPE *yylval, char *str, int base, int token)
23 {
24 	u64 num;
25 
26 	errno = 0;
27 	num = strtoull(str, NULL, base);
28 	if (errno)
29 		return PE_ERROR;
30 
31 	yylval->num = num;
32 	return token;
33 }
34 
35 static int value(yyscan_t scanner, int base)
36 {
37 	YYSTYPE *yylval = parse_events_get_lval(scanner);
38 	char *text = parse_events_get_text(scanner);
39 
40 	return __value(yylval, text, base, PE_VALUE);
41 }
42 
43 static int str(yyscan_t scanner, int token)
44 {
45 	YYSTYPE *yylval = parse_events_get_lval(scanner);
46 	char *text = parse_events_get_text(scanner);
47 
48 	if (text[0] != '\'') {
49 		yylval->str = strdup(text);
50 	} else {
51 		/*
52 		 * If a text tag specified on the command line
53 		 * contains opening single quite ' then it is
54 		 * expected that the tag ends with single quote
55 		 * as well, like this:
56 		 *     name=\'CPU_CLK_UNHALTED.THREAD:cmask=1\'
57 		 * quotes need to be escaped to bypass shell
58 		 * processing.
59 		 */
60 		yylval->str = strndup(&text[1], strlen(text) - 2);
61 	}
62 
63 	return token;
64 }
65 
66 static int lc_str(yyscan_t scanner, const struct parse_events_state *state)
67 {
68 	return str(scanner, state->match_legacy_cache_terms ? PE_LEGACY_CACHE : PE_NAME);
69 }
70 
71 static bool isbpf_suffix(char *text)
72 {
73 	int len = strlen(text);
74 
75 	if (len < 2)
76 		return false;
77 	if ((text[len - 1] == 'c' || text[len - 1] == 'o') &&
78 	    text[len - 2] == '.')
79 		return true;
80 	if (len > 4 && !strcmp(text + len - 4, ".obj"))
81 		return true;
82 	return false;
83 }
84 
85 static bool isbpf(yyscan_t scanner)
86 {
87 	char *text = parse_events_get_text(scanner);
88 	struct stat st;
89 
90 	if (!isbpf_suffix(text))
91 		return false;
92 
93 	return stat(text, &st) == 0;
94 }
95 
96 /*
97  * This function is called when the parser gets two kind of input:
98  *
99  * 	@cfg1 or @cfg2=config
100  *
101  * The leading '@' is stripped off before 'cfg1' and 'cfg2=config' are given to
102  * bison.  In the latter case it is necessary to keep the string intact so that
103  * the PMU kernel driver can determine what configurable is associated to
104  * 'config'.
105  */
106 static int drv_str(yyscan_t scanner, int token)
107 {
108 	YYSTYPE *yylval = parse_events_get_lval(scanner);
109 	char *text = parse_events_get_text(scanner);
110 
111 	/* Strip off the '@' */
112 	yylval->str = strdup(text + 1);
113 	return token;
114 }
115 
116 #define REWIND(__alloc)				\
117 do {								\
118 	YYSTYPE *__yylval = parse_events_get_lval(yyscanner);	\
119 	char *text = parse_events_get_text(yyscanner);		\
120 								\
121 	if (__alloc)						\
122 		__yylval->str = strdup(text);			\
123 								\
124 	yycolumn -= strlen(text);				\
125 	yyless(0);						\
126 } while (0)
127 
128 static int sym(yyscan_t scanner, int type, int config)
129 {
130 	YYSTYPE *yylval = parse_events_get_lval(scanner);
131 
132 	yylval->num = (type << 16) + config;
133 	return type == PERF_TYPE_HARDWARE ? PE_VALUE_SYM_HW : PE_VALUE_SYM_SW;
134 }
135 
136 static int tool(yyscan_t scanner, enum perf_tool_event event)
137 {
138 	YYSTYPE *yylval = parse_events_get_lval(scanner);
139 
140 	yylval->num = event;
141 	return PE_VALUE_SYM_TOOL;
142 }
143 
144 static int term(yyscan_t scanner, int type)
145 {
146 	YYSTYPE *yylval = parse_events_get_lval(scanner);
147 
148 	yylval->num = type;
149 	return PE_TERM;
150 }
151 
152 static int hw_term(yyscan_t scanner, int config)
153 {
154 	YYSTYPE *yylval = parse_events_get_lval(scanner);
155 	char *text = parse_events_get_text(scanner);
156 
157 	yylval->hardware_term.str = strdup(text);
158 	yylval->hardware_term.num = PERF_TYPE_HARDWARE + config;
159 	return PE_TERM_HW;
160 }
161 
162 #define YY_USER_ACTION					\
163 do {							\
164 	yylloc->last_column  = yylloc->first_column;	\
165 	yylloc->first_column = yycolumn;		\
166 	yycolumn += yyleng;				\
167 } while (0);
168 
169 #define USER_REJECT		\
170 	yycolumn -= yyleng;	\
171 	REJECT
172 
173 %}
174 
175 %x mem
176 %s config
177 %x event
178 %x array
179 
180 group		[^,{}/]*[{][^}]*[}][^,{}/]*
181 event_pmu	[^,{}/]+[/][^/]*[/][^,{}/]*
182 event		[^,{}/]+
183 bpf_object	[^,{}]+\.(o|bpf)[a-zA-Z0-9._]*
184 bpf_source	[^,{}]+\.c[a-zA-Z0-9._]*
185 
186 num_dec		[0-9]+
187 num_hex		0x[a-fA-F0-9]+
188 num_raw_hex	[a-fA-F0-9]+
189 name		[a-zA-Z_*?\[\]][a-zA-Z0-9_*?.\[\]!\-]*
190 name_tag	[\'][a-zA-Z_*?\[\]][a-zA-Z0-9_*?\-,\.\[\]:=]*[\']
191 name_minus	[a-zA-Z_*?][a-zA-Z0-9\-_*?.:]*
192 drv_cfg_term	[a-zA-Z0-9_\.]+(=[a-zA-Z0-9_*?\.:]+)?
193 /*
194  * If you add a modifier you need to update check_modifier().
195  * Also, the letters in modifier_event must not be in modifier_bp.
196  */
197 modifier_event	[ukhpPGHSDIWeb]+
198 modifier_bp	[rwx]{1,3}
199 lc_type 	(L1-dcache|l1-d|l1d|L1-data|L1-icache|l1-i|l1i|L1-instruction|LLC|L2|dTLB|d-tlb|Data-TLB|iTLB|i-tlb|Instruction-TLB|branch|branches|bpu|btb|bpc|node)
200 lc_op_result	(load|loads|read|store|stores|write|prefetch|prefetches|speculative-read|speculative-load|refs|Reference|ops|access|misses|miss)
201 digit		[0-9]
202 non_digit	[^0-9]
203 
204 %%
205 
206 %{
207 	struct parse_events_state *_parse_state = parse_events_get_extra(yyscanner);
208 	{
209 		int start_token = _parse_state->stoken;
210 
211 		if (start_token == PE_START_TERMS)
212 			BEGIN(config);
213 		else if (start_token == PE_START_EVENTS)
214 			BEGIN(event);
215 
216 		if (start_token) {
217 			_parse_state->stoken = 0;
218 			/*
219 			 * The flex parser does not init locations variable
220 			 * via the scan_string interface, so we need do the
221 			 * init in here.
222 			 */
223 			yycolumn = 0;
224 			return start_token;
225 		}
226          }
227 %}
228 
229 <event>{
230 
231 {group}		{
232 			BEGIN(INITIAL);
233 			REWIND(0);
234 		}
235 
236 {event_pmu}	|
237 {bpf_object}	|
238 {bpf_source}	|
239 {event}		{
240 			BEGIN(INITIAL);
241 			REWIND(1);
242 			return PE_EVENT_NAME;
243 		}
244 
245 <<EOF>>		{
246 			BEGIN(INITIAL);
247 			REWIND(0);
248 		}
249 ,		{
250 			return ',';
251 		}
252 }
253 
254 <array>{
255 "]"			{ BEGIN(config); return ']'; }
256 {num_dec}		{ return value(yyscanner, 10); }
257 {num_hex}		{ return value(yyscanner, 16); }
258 ,			{ return ','; }
259 "\.\.\."		{ return PE_ARRAY_RANGE; }
260 }
261 
262 <config>{
263 	/*
264 	 * Please update config_term_names when new static term is added.
265 	 */
266 config			{ return term(yyscanner, PARSE_EVENTS__TERM_TYPE_CONFIG); }
267 config1			{ return term(yyscanner, PARSE_EVENTS__TERM_TYPE_CONFIG1); }
268 config2			{ return term(yyscanner, PARSE_EVENTS__TERM_TYPE_CONFIG2); }
269 config3			{ return term(yyscanner, PARSE_EVENTS__TERM_TYPE_CONFIG3); }
270 name			{ return term(yyscanner, PARSE_EVENTS__TERM_TYPE_NAME); }
271 period			{ return term(yyscanner, PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD); }
272 freq			{ return term(yyscanner, PARSE_EVENTS__TERM_TYPE_SAMPLE_FREQ); }
273 branch_type		{ return term(yyscanner, PARSE_EVENTS__TERM_TYPE_BRANCH_SAMPLE_TYPE); }
274 time			{ return term(yyscanner, PARSE_EVENTS__TERM_TYPE_TIME); }
275 call-graph		{ return term(yyscanner, PARSE_EVENTS__TERM_TYPE_CALLGRAPH); }
276 stack-size		{ return term(yyscanner, PARSE_EVENTS__TERM_TYPE_STACKSIZE); }
277 max-stack		{ return term(yyscanner, PARSE_EVENTS__TERM_TYPE_MAX_STACK); }
278 nr			{ return term(yyscanner, PARSE_EVENTS__TERM_TYPE_MAX_EVENTS); }
279 inherit			{ return term(yyscanner, PARSE_EVENTS__TERM_TYPE_INHERIT); }
280 no-inherit		{ return term(yyscanner, PARSE_EVENTS__TERM_TYPE_NOINHERIT); }
281 overwrite		{ return term(yyscanner, PARSE_EVENTS__TERM_TYPE_OVERWRITE); }
282 no-overwrite		{ return term(yyscanner, PARSE_EVENTS__TERM_TYPE_NOOVERWRITE); }
283 percore			{ return term(yyscanner, PARSE_EVENTS__TERM_TYPE_PERCORE); }
284 aux-output		{ return term(yyscanner, PARSE_EVENTS__TERM_TYPE_AUX_OUTPUT); }
285 aux-sample-size		{ return term(yyscanner, PARSE_EVENTS__TERM_TYPE_AUX_SAMPLE_SIZE); }
286 metric-id		{ return term(yyscanner, PARSE_EVENTS__TERM_TYPE_METRIC_ID); }
287 cpu-cycles|cycles				{ return hw_term(yyscanner, PERF_COUNT_HW_CPU_CYCLES); }
288 stalled-cycles-frontend|idle-cycles-frontend	{ return hw_term(yyscanner, PERF_COUNT_HW_STALLED_CYCLES_FRONTEND); }
289 stalled-cycles-backend|idle-cycles-backend	{ return hw_term(yyscanner, PERF_COUNT_HW_STALLED_CYCLES_BACKEND); }
290 instructions					{ return hw_term(yyscanner, PERF_COUNT_HW_INSTRUCTIONS); }
291 cache-references				{ return hw_term(yyscanner, PERF_COUNT_HW_CACHE_REFERENCES); }
292 cache-misses					{ return hw_term(yyscanner, PERF_COUNT_HW_CACHE_MISSES); }
293 branch-instructions|branches			{ return hw_term(yyscanner, PERF_COUNT_HW_BRANCH_INSTRUCTIONS); }
294 branch-misses					{ return hw_term(yyscanner, PERF_COUNT_HW_BRANCH_MISSES); }
295 bus-cycles					{ return hw_term(yyscanner, PERF_COUNT_HW_BUS_CYCLES); }
296 ref-cycles					{ return hw_term(yyscanner, PERF_COUNT_HW_REF_CPU_CYCLES); }
297 r{num_raw_hex}		{ return str(yyscanner, PE_RAW); }
298 r0x{num_raw_hex}	{ return str(yyscanner, PE_RAW); }
299 ,			{ return ','; }
300 "/"			{ BEGIN(INITIAL); return '/'; }
301 {lc_type}			{ return lc_str(yyscanner, _parse_state); }
302 {lc_type}-{lc_op_result}	{ return lc_str(yyscanner, _parse_state); }
303 {lc_type}-{lc_op_result}-{lc_op_result}	{ return lc_str(yyscanner, _parse_state); }
304 {name_minus}		{ return str(yyscanner, PE_NAME); }
305 \[all\]			{ return PE_ARRAY_ALL; }
306 "["			{ BEGIN(array); return '['; }
307 @{drv_cfg_term}		{ return drv_str(yyscanner, PE_DRV_CFG_TERM); }
308 }
309 
310 <mem>{
311 {modifier_bp}		{ return str(yyscanner, PE_MODIFIER_BP); }
312 	/*
313 	 * The colon before memory access modifiers can get mixed up with the
314 	 * colon before event modifiers. Fortunately none of the option letters
315 	 * are the same, so trailing context can be used disambiguate the two
316 	 * cases.
317 	 */
318 ":"/{modifier_bp}	{ return PE_BP_COLON; }
319 	/*
320 	 * The slash before memory length can get mixed up with the slash before
321 	 * config terms. Fortunately config terms do not start with a numeric
322 	 * digit, so trailing context can be used disambiguate the two cases.
323 	 */
324 "/"/{digit}		{ return PE_BP_SLASH; }
325 "/"/{non_digit}		{ BEGIN(config); return '/'; }
326 {num_dec}		{ return value(yyscanner, 10); }
327 {num_hex}		{ return value(yyscanner, 16); }
328 	/*
329 	 * We need to separate 'mem:' scanner part, in order to get specific
330 	 * modifier bits parsed out. Otherwise we would need to handle PE_NAME
331 	 * and we'd need to parse it manually. During the escape from <mem>
332 	 * state we need to put the escaping char back, so we dont miss it.
333 	 */
334 .			{ unput(*yytext); BEGIN(INITIAL); }
335 	/*
336 	 * We destroy the scanner after reaching EOF,
337 	 * but anyway just to be sure get back to INIT state.
338 	 */
339 <<EOF>>			{ BEGIN(INITIAL); }
340 }
341 
342 cpu-cycles|cycles				{ return sym(yyscanner, PERF_TYPE_HARDWARE, PERF_COUNT_HW_CPU_CYCLES); }
343 stalled-cycles-frontend|idle-cycles-frontend	{ return sym(yyscanner, PERF_TYPE_HARDWARE, PERF_COUNT_HW_STALLED_CYCLES_FRONTEND); }
344 stalled-cycles-backend|idle-cycles-backend	{ return sym(yyscanner, PERF_TYPE_HARDWARE, PERF_COUNT_HW_STALLED_CYCLES_BACKEND); }
345 instructions					{ return sym(yyscanner, PERF_TYPE_HARDWARE, PERF_COUNT_HW_INSTRUCTIONS); }
346 cache-references				{ return sym(yyscanner, PERF_TYPE_HARDWARE, PERF_COUNT_HW_CACHE_REFERENCES); }
347 cache-misses					{ return sym(yyscanner, PERF_TYPE_HARDWARE, PERF_COUNT_HW_CACHE_MISSES); }
348 branch-instructions|branches			{ return sym(yyscanner, PERF_TYPE_HARDWARE, PERF_COUNT_HW_BRANCH_INSTRUCTIONS); }
349 branch-misses					{ return sym(yyscanner, PERF_TYPE_HARDWARE, PERF_COUNT_HW_BRANCH_MISSES); }
350 bus-cycles					{ return sym(yyscanner, PERF_TYPE_HARDWARE, PERF_COUNT_HW_BUS_CYCLES); }
351 ref-cycles					{ return sym(yyscanner, PERF_TYPE_HARDWARE, PERF_COUNT_HW_REF_CPU_CYCLES); }
352 cpu-clock					{ return sym(yyscanner, PERF_TYPE_SOFTWARE, PERF_COUNT_SW_CPU_CLOCK); }
353 task-clock					{ return sym(yyscanner, PERF_TYPE_SOFTWARE, PERF_COUNT_SW_TASK_CLOCK); }
354 page-faults|faults				{ return sym(yyscanner, PERF_TYPE_SOFTWARE, PERF_COUNT_SW_PAGE_FAULTS); }
355 minor-faults					{ return sym(yyscanner, PERF_TYPE_SOFTWARE, PERF_COUNT_SW_PAGE_FAULTS_MIN); }
356 major-faults					{ return sym(yyscanner, PERF_TYPE_SOFTWARE, PERF_COUNT_SW_PAGE_FAULTS_MAJ); }
357 context-switches|cs				{ return sym(yyscanner, PERF_TYPE_SOFTWARE, PERF_COUNT_SW_CONTEXT_SWITCHES); }
358 cpu-migrations|migrations			{ return sym(yyscanner, PERF_TYPE_SOFTWARE, PERF_COUNT_SW_CPU_MIGRATIONS); }
359 alignment-faults				{ return sym(yyscanner, PERF_TYPE_SOFTWARE, PERF_COUNT_SW_ALIGNMENT_FAULTS); }
360 emulation-faults				{ return sym(yyscanner, PERF_TYPE_SOFTWARE, PERF_COUNT_SW_EMULATION_FAULTS); }
361 dummy						{ return sym(yyscanner, PERF_TYPE_SOFTWARE, PERF_COUNT_SW_DUMMY); }
362 duration_time					{ return tool(yyscanner, PERF_TOOL_DURATION_TIME); }
363 user_time						{ return tool(yyscanner, PERF_TOOL_USER_TIME); }
364 system_time						{ return tool(yyscanner, PERF_TOOL_SYSTEM_TIME); }
365 bpf-output					{ return sym(yyscanner, PERF_TYPE_SOFTWARE, PERF_COUNT_SW_BPF_OUTPUT); }
366 cgroup-switches					{ return sym(yyscanner, PERF_TYPE_SOFTWARE, PERF_COUNT_SW_CGROUP_SWITCHES); }
367 
368 {lc_type}			{ return str(yyscanner, PE_LEGACY_CACHE); }
369 {lc_type}-{lc_op_result}	{ return str(yyscanner, PE_LEGACY_CACHE); }
370 {lc_type}-{lc_op_result}-{lc_op_result}	{ return str(yyscanner, PE_LEGACY_CACHE); }
371 mem:			{ BEGIN(mem); return PE_PREFIX_MEM; }
372 r{num_raw_hex}		{ return str(yyscanner, PE_RAW); }
373 {num_dec}		{ return value(yyscanner, 10); }
374 {num_hex}		{ return value(yyscanner, 16); }
375 
376 {modifier_event}	{ return str(yyscanner, PE_MODIFIER_EVENT); }
377 {bpf_object}		{ if (!isbpf(yyscanner)) { USER_REJECT }; return str(yyscanner, PE_BPF_OBJECT); }
378 {bpf_source}		{ if (!isbpf(yyscanner)) { USER_REJECT }; return str(yyscanner, PE_BPF_SOURCE); }
379 {name}			{ return str(yyscanner, PE_NAME); }
380 {name_tag}		{ return str(yyscanner, PE_NAME); }
381 "/"			{ BEGIN(config); return '/'; }
382 ,			{ BEGIN(event); return ','; }
383 :			{ return ':'; }
384 "{"			{ BEGIN(event); return '{'; }
385 "}"			{ return '}'; }
386 =			{ return '='; }
387 \n			{ }
388 .			{ }
389 
390 %%
391 
392 int parse_events_wrap(void *scanner __maybe_unused)
393 {
394 	return 1;
395 }
396