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