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