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