xref: /openbmc/linux/tools/perf/util/parse-events.l (revision 20e2fc42)
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)
133 {
134 	YYSTYPE *yylval = parse_events_get_lval(scanner);
135 	char *text = parse_events_get_text(scanner);
136 
137 	yylval->str = strdup(text);
138 	switch (perf_pmu__parse_check(text)) {
139 		case PMU_EVENT_SYMBOL_PREFIX:
140 			return PE_PMU_EVENT_PRE;
141 		case PMU_EVENT_SYMBOL_SUFFIX:
142 			return PE_PMU_EVENT_SUF;
143 		case PMU_EVENT_SYMBOL:
144 			return PE_KERNEL_PMU_EVENT;
145 		default:
146 			return PE_NAME;
147 	}
148 }
149 
150 static int sym(yyscan_t scanner, int type, int config)
151 {
152 	YYSTYPE *yylval = parse_events_get_lval(scanner);
153 
154 	yylval->num = (type << 16) + config;
155 	return type == PERF_TYPE_HARDWARE ? PE_VALUE_SYM_HW : PE_VALUE_SYM_SW;
156 }
157 
158 static int tool(yyscan_t scanner, enum perf_tool_event event)
159 {
160 	YYSTYPE *yylval = parse_events_get_lval(scanner);
161 
162 	yylval->num = event;
163 	return PE_VALUE_SYM_TOOL;
164 }
165 
166 static int term(yyscan_t scanner, int type)
167 {
168 	YYSTYPE *yylval = parse_events_get_lval(scanner);
169 
170 	yylval->num = type;
171 	return PE_TERM;
172 }
173 
174 #define YY_USER_ACTION					\
175 do {							\
176 	yylloc->last_column  = yylloc->first_column;	\
177 	yylloc->first_column = yycolumn;		\
178 	yycolumn += yyleng;				\
179 } while (0);
180 
181 #define USER_REJECT		\
182 	yycolumn -= yyleng;	\
183 	REJECT
184 
185 %}
186 
187 %x mem
188 %s config
189 %x event
190 %x array
191 
192 group		[^,{}/]*[{][^}]*[}][^,{}/]*
193 event_pmu	[^,{}/]+[/][^/]*[/][^,{}/]*
194 event		[^,{}/]+
195 bpf_object	[^,{}]+\.(o|bpf)[a-zA-Z0-9._]*
196 bpf_source	[^,{}]+\.c[a-zA-Z0-9._]*
197 
198 num_dec		[0-9]+
199 num_hex		0x[a-fA-F0-9]+
200 num_raw_hex	[a-fA-F0-9]+
201 name		[a-zA-Z_*?\[\]][a-zA-Z0-9_*?.\[\]]*
202 name_tag	[\'][a-zA-Z_*?\[\]][a-zA-Z0-9_*?\-,\.\[\]:=]*[\']
203 name_minus	[a-zA-Z_*?][a-zA-Z0-9\-_*?.:]*
204 drv_cfg_term	[a-zA-Z0-9_\.]+(=[a-zA-Z0-9_*?\.:]+)?
205 /* If you add a modifier you need to update check_modifier() */
206 modifier_event	[ukhpPGHSDIW]+
207 modifier_bp	[rwx]{1,3}
208 
209 %%
210 
211 %{
212 	{
213 		int start_token;
214 
215 		start_token = parse_events_get_extra(yyscanner);
216 
217 		if (start_token == PE_START_TERMS)
218 			BEGIN(config);
219 		else if (start_token == PE_START_EVENTS)
220 			BEGIN(event);
221 
222 		if (start_token) {
223 			parse_events_set_extra(NULL, yyscanner);
224 			/*
225 			 * The flex parser does not init locations variable
226 			 * via the scan_string interface, so we need do the
227 			 * init in here.
228 			 */
229 			yycolumn = 0;
230 			return start_token;
231 		}
232          }
233 %}
234 
235 <event>{
236 
237 {group}		{
238 			BEGIN(INITIAL);
239 			REWIND(0);
240 		}
241 
242 {event_pmu}	|
243 {bpf_object}	|
244 {bpf_source}	|
245 {event}		{
246 			BEGIN(INITIAL);
247 			REWIND(1);
248 			return PE_EVENT_NAME;
249 		}
250 
251 <<EOF>>		{
252 			BEGIN(INITIAL);
253 			REWIND(0);
254 		}
255 
256 }
257 
258 <array>{
259 "]"			{ BEGIN(config); return ']'; }
260 {num_dec}		{ return value(yyscanner, 10); }
261 {num_hex}		{ return value(yyscanner, 16); }
262 ,			{ return ','; }
263 "\.\.\."		{ return PE_ARRAY_RANGE; }
264 }
265 
266 <config>{
267 	/*
268 	 * Please update config_term_names when new static term is added.
269 	 */
270 config			{ return term(yyscanner, PARSE_EVENTS__TERM_TYPE_CONFIG); }
271 config1			{ return term(yyscanner, PARSE_EVENTS__TERM_TYPE_CONFIG1); }
272 config2			{ return term(yyscanner, PARSE_EVENTS__TERM_TYPE_CONFIG2); }
273 name			{ return term(yyscanner, PARSE_EVENTS__TERM_TYPE_NAME); }
274 period			{ return term(yyscanner, PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD); }
275 freq			{ return term(yyscanner, PARSE_EVENTS__TERM_TYPE_SAMPLE_FREQ); }
276 branch_type		{ return term(yyscanner, PARSE_EVENTS__TERM_TYPE_BRANCH_SAMPLE_TYPE); }
277 time			{ return term(yyscanner, PARSE_EVENTS__TERM_TYPE_TIME); }
278 call-graph		{ return term(yyscanner, PARSE_EVENTS__TERM_TYPE_CALLGRAPH); }
279 stack-size		{ return term(yyscanner, PARSE_EVENTS__TERM_TYPE_STACKSIZE); }
280 max-stack		{ return term(yyscanner, PARSE_EVENTS__TERM_TYPE_MAX_STACK); }
281 nr			{ return term(yyscanner, PARSE_EVENTS__TERM_TYPE_MAX_EVENTS); }
282 inherit			{ return term(yyscanner, PARSE_EVENTS__TERM_TYPE_INHERIT); }
283 no-inherit		{ return term(yyscanner, PARSE_EVENTS__TERM_TYPE_NOINHERIT); }
284 overwrite		{ return term(yyscanner, PARSE_EVENTS__TERM_TYPE_OVERWRITE); }
285 no-overwrite		{ return term(yyscanner, PARSE_EVENTS__TERM_TYPE_NOOVERWRITE); }
286 percore			{ return term(yyscanner, PARSE_EVENTS__TERM_TYPE_PERCORE); }
287 aux-output		{ return term(yyscanner, PARSE_EVENTS__TERM_TYPE_AUX_OUTPUT); }
288 ,			{ return ','; }
289 "/"			{ BEGIN(INITIAL); return '/'; }
290 {name_minus}		{ return str(yyscanner, PE_NAME); }
291 \[all\]			{ return PE_ARRAY_ALL; }
292 "["			{ BEGIN(array); return '['; }
293 @{drv_cfg_term}		{ return drv_str(yyscanner, PE_DRV_CFG_TERM); }
294 }
295 
296 <mem>{
297 {modifier_bp}		{ return str(yyscanner, PE_MODIFIER_BP); }
298 :			{ return ':'; }
299 "/"			{ return '/'; }
300 {num_dec}		{ return value(yyscanner, 10); }
301 {num_hex}		{ return value(yyscanner, 16); }
302 	/*
303 	 * We need to separate 'mem:' scanner part, in order to get specific
304 	 * modifier bits parsed out. Otherwise we would need to handle PE_NAME
305 	 * and we'd need to parse it manually. During the escape from <mem>
306 	 * state we need to put the escaping char back, so we dont miss it.
307 	 */
308 .			{ unput(*yytext); BEGIN(INITIAL); }
309 	/*
310 	 * We destroy the scanner after reaching EOF,
311 	 * but anyway just to be sure get back to INIT state.
312 	 */
313 <<EOF>>			{ BEGIN(INITIAL); }
314 }
315 
316 cpu-cycles|cycles				{ return sym(yyscanner, PERF_TYPE_HARDWARE, PERF_COUNT_HW_CPU_CYCLES); }
317 stalled-cycles-frontend|idle-cycles-frontend	{ return sym(yyscanner, PERF_TYPE_HARDWARE, PERF_COUNT_HW_STALLED_CYCLES_FRONTEND); }
318 stalled-cycles-backend|idle-cycles-backend	{ return sym(yyscanner, PERF_TYPE_HARDWARE, PERF_COUNT_HW_STALLED_CYCLES_BACKEND); }
319 instructions					{ return sym(yyscanner, PERF_TYPE_HARDWARE, PERF_COUNT_HW_INSTRUCTIONS); }
320 cache-references				{ return sym(yyscanner, PERF_TYPE_HARDWARE, PERF_COUNT_HW_CACHE_REFERENCES); }
321 cache-misses					{ return sym(yyscanner, PERF_TYPE_HARDWARE, PERF_COUNT_HW_CACHE_MISSES); }
322 branch-instructions|branches			{ return sym(yyscanner, PERF_TYPE_HARDWARE, PERF_COUNT_HW_BRANCH_INSTRUCTIONS); }
323 branch-misses					{ return sym(yyscanner, PERF_TYPE_HARDWARE, PERF_COUNT_HW_BRANCH_MISSES); }
324 bus-cycles					{ return sym(yyscanner, PERF_TYPE_HARDWARE, PERF_COUNT_HW_BUS_CYCLES); }
325 ref-cycles					{ return sym(yyscanner, PERF_TYPE_HARDWARE, PERF_COUNT_HW_REF_CPU_CYCLES); }
326 cpu-clock					{ return sym(yyscanner, PERF_TYPE_SOFTWARE, PERF_COUNT_SW_CPU_CLOCK); }
327 task-clock					{ return sym(yyscanner, PERF_TYPE_SOFTWARE, PERF_COUNT_SW_TASK_CLOCK); }
328 page-faults|faults				{ return sym(yyscanner, PERF_TYPE_SOFTWARE, PERF_COUNT_SW_PAGE_FAULTS); }
329 minor-faults					{ return sym(yyscanner, PERF_TYPE_SOFTWARE, PERF_COUNT_SW_PAGE_FAULTS_MIN); }
330 major-faults					{ return sym(yyscanner, PERF_TYPE_SOFTWARE, PERF_COUNT_SW_PAGE_FAULTS_MAJ); }
331 context-switches|cs				{ return sym(yyscanner, PERF_TYPE_SOFTWARE, PERF_COUNT_SW_CONTEXT_SWITCHES); }
332 cpu-migrations|migrations			{ return sym(yyscanner, PERF_TYPE_SOFTWARE, PERF_COUNT_SW_CPU_MIGRATIONS); }
333 alignment-faults				{ return sym(yyscanner, PERF_TYPE_SOFTWARE, PERF_COUNT_SW_ALIGNMENT_FAULTS); }
334 emulation-faults				{ return sym(yyscanner, PERF_TYPE_SOFTWARE, PERF_COUNT_SW_EMULATION_FAULTS); }
335 dummy						{ return sym(yyscanner, PERF_TYPE_SOFTWARE, PERF_COUNT_SW_DUMMY); }
336 duration_time					{ return tool(yyscanner, PERF_TOOL_DURATION_TIME); }
337 bpf-output					{ return sym(yyscanner, PERF_TYPE_SOFTWARE, PERF_COUNT_SW_BPF_OUTPUT); }
338 
339 	/*
340 	 * We have to handle the kernel PMU event cycles-ct/cycles-t/mem-loads/mem-stores separately.
341 	 * Because the prefix cycles is mixed up with cpu-cycles.
342 	 * loads and stores are mixed up with cache event
343 	 */
344 cycles-ct					{ return str(yyscanner, PE_KERNEL_PMU_EVENT); }
345 cycles-t					{ return str(yyscanner, PE_KERNEL_PMU_EVENT); }
346 mem-loads					{ return str(yyscanner, PE_KERNEL_PMU_EVENT); }
347 mem-stores					{ return str(yyscanner, PE_KERNEL_PMU_EVENT); }
348 topdown-[a-z-]+					{ return str(yyscanner, PE_KERNEL_PMU_EVENT); }
349 
350 L1-dcache|l1-d|l1d|L1-data		|
351 L1-icache|l1-i|l1i|L1-instruction	|
352 LLC|L2					|
353 dTLB|d-tlb|Data-TLB			|
354 iTLB|i-tlb|Instruction-TLB		|
355 branch|branches|bpu|btb|bpc		|
356 node					{ return str(yyscanner, PE_NAME_CACHE_TYPE); }
357 
358 load|loads|read				|
359 store|stores|write			|
360 prefetch|prefetches			|
361 speculative-read|speculative-load	|
362 refs|Reference|ops|access		|
363 misses|miss				{ return str(yyscanner, PE_NAME_CACHE_OP_RESULT); }
364 
365 mem:			{ BEGIN(mem); return PE_PREFIX_MEM; }
366 r{num_raw_hex}		{ return raw(yyscanner); }
367 {num_dec}		{ return value(yyscanner, 10); }
368 {num_hex}		{ return value(yyscanner, 16); }
369 
370 {modifier_event}	{ return str(yyscanner, PE_MODIFIER_EVENT); }
371 {bpf_object}		{ if (!isbpf(yyscanner)) { USER_REJECT }; return str(yyscanner, PE_BPF_OBJECT); }
372 {bpf_source}		{ if (!isbpf(yyscanner)) { USER_REJECT }; return str(yyscanner, PE_BPF_SOURCE); }
373 {name}			{ return pmu_str_check(yyscanner); }
374 {name_tag}		{ return str(yyscanner, PE_NAME); }
375 "/"			{ BEGIN(config); return '/'; }
376 -			{ return '-'; }
377 ,			{ BEGIN(event); return ','; }
378 :			{ return ':'; }
379 "{"			{ BEGIN(event); return '{'; }
380 "}"			{ return '}'; }
381 =			{ return '='; }
382 \n			{ }
383 .			{ }
384 
385 %%
386 
387 int parse_events_wrap(void *scanner __maybe_unused)
388 {
389 	return 1;
390 }
391