4 %option prefix="parse_events_"
6 %option bison-locations
12 #include <sys/types.h>
16 #include "parse-events.h"
17 #include "parse-events-bison.h"
20 char *parse_events_get_text(yyscan_t yyscanner);
21 YYSTYPE *parse_events_get_lval(yyscan_t yyscanner);
23 static int __value(YYSTYPE *yylval, char *str, int base, int token)
28 num = strtoull(str, NULL, base);
36 static int value(yyscan_t scanner, int base)
38 YYSTYPE *yylval = parse_events_get_lval(scanner);
39 char *text = parse_events_get_text(scanner);
41 return __value(yylval, text, base, PE_VALUE);
44 static int str(yyscan_t scanner, int token)
46 YYSTYPE *yylval = parse_events_get_lval(scanner);
47 char *text = parse_events_get_text(scanner);
49 if (text[0] != '\'') {
50 yylval->str = strdup(text);
53 * If a text tag specified on the command line
54 * contains opening single quite ' then it is
55 * expected that the tag ends with single quote
57 * name=\'CPU_CLK_UNHALTED.THREAD:cmask=1\'
58 * quotes need to be escaped to bypass shell
61 yylval->str = strndup(&text[1], strlen(text) - 2);
67 static int raw(yyscan_t scanner)
69 YYSTYPE *yylval = parse_events_get_lval(scanner);
70 char *text = parse_events_get_text(scanner);
72 if (perf_pmu__parse_check(text) == PMU_EVENT_SYMBOL)
73 return str(scanner, PE_NAME);
75 return __value(yylval, text + 1, 16, PE_RAW);
78 static bool isbpf_suffix(char *text)
80 int len = strlen(text);
84 if ((text[len - 1] == 'c' || text[len - 1] == 'o') &&
87 if (len > 4 && !strcmp(text + len - 4, ".obj"))
92 static bool isbpf(yyscan_t scanner)
94 char *text = parse_events_get_text(scanner);
97 if (!isbpf_suffix(text))
100 return stat(text, &st) == 0;
104 * This function is called when the parser gets two kind of input:
106 * @cfg1 or @cfg2=config
108 * The leading '@' is stripped off before 'cfg1' and 'cfg2=config' are given to
109 * bison. In the latter case it is necessary to keep the string intact so that
110 * the PMU kernel driver can determine what configurable is associated to
113 static int drv_str(yyscan_t scanner, int token)
115 YYSTYPE *yylval = parse_events_get_lval(scanner);
116 char *text = parse_events_get_text(scanner);
118 /* Strip off the '@' */
119 yylval->str = strdup(text + 1);
123 #define REWIND(__alloc) \
125 YYSTYPE *__yylval = parse_events_get_lval(yyscanner); \
126 char *text = parse_events_get_text(yyscanner); \
129 __yylval->str = strdup(text); \
131 yycolumn -= strlen(text); \
135 static int pmu_str_check(yyscan_t scanner, struct parse_events_state *parse_state)
137 YYSTYPE *yylval = parse_events_get_lval(scanner);
138 char *text = parse_events_get_text(scanner);
140 yylval->str = strdup(text);
142 if (parse_state->fake_pmu)
143 return PE_PMU_EVENT_FAKE;
145 switch (perf_pmu__parse_check(text)) {
146 case PMU_EVENT_SYMBOL_PREFIX:
147 return PE_PMU_EVENT_PRE;
148 case PMU_EVENT_SYMBOL_SUFFIX:
149 return PE_PMU_EVENT_SUF;
150 case PMU_EVENT_SYMBOL:
151 return PE_KERNEL_PMU_EVENT;
157 static int sym(yyscan_t scanner, int type, int config)
159 YYSTYPE *yylval = parse_events_get_lval(scanner);
161 yylval->num = (type << 16) + config;
162 return type == PERF_TYPE_HARDWARE ? PE_VALUE_SYM_HW : PE_VALUE_SYM_SW;
165 static int tool(yyscan_t scanner, enum perf_tool_event event)
167 YYSTYPE *yylval = parse_events_get_lval(scanner);
170 return PE_VALUE_SYM_TOOL;
173 static int term(yyscan_t scanner, int type)
175 YYSTYPE *yylval = parse_events_get_lval(scanner);
181 #define YY_USER_ACTION \
183 yylloc->last_column = yylloc->first_column; \
184 yylloc->first_column = yycolumn; \
185 yycolumn += yyleng; \
188 #define USER_REJECT \
189 yycolumn -= yyleng; \
199 group [^,{}/]*[{][^}]*[}][^,{}/]*
200 event_pmu [^,{}/]+[/][^/]*[/][^,{}/]*
202 bpf_object [^,{}]+\.(o|bpf)[a-zA-Z0-9._]*
203 bpf_source [^,{}]+\.c[a-zA-Z0-9._]*
206 num_hex 0x[a-fA-F0-9]+
207 num_raw_hex [a-fA-F0-9]+
208 name [a-zA-Z_*?\[\]][a-zA-Z0-9_*?.\[\]]*
209 name_tag [\'][a-zA-Z_*?\[\]][a-zA-Z0-9_*?\-,\.\[\]:=]*[\']
210 name_minus [a-zA-Z_*?][a-zA-Z0-9\-_*?.:]*
211 drv_cfg_term [a-zA-Z0-9_\.]+(=[a-zA-Z0-9_*?\.:]+)?
212 /* If you add a modifier you need to update check_modifier() */
213 modifier_event [ukhpPGHSDIW]+
214 modifier_bp [rwx]{1,3}
219 struct parse_events_state *_parse_state = parse_events_get_extra(yyscanner);
222 int start_token = _parse_state->stoken;
224 if (start_token == PE_START_TERMS)
226 else if (start_token == PE_START_EVENTS)
230 _parse_state->stoken = 0;
232 * The flex parser does not init locations variable
233 * via the scan_string interface, so we need do the
255 return PE_EVENT_NAME;
268 "]" { BEGIN(config); return ']'; }
269 {num_dec} { return value(yyscanner, 10); }
270 {num_hex} { return value(yyscanner, 16); }
272 "\.\.\." { return PE_ARRAY_RANGE; }
277 * Please update config_term_names when new static term is added.
279 config { return term(yyscanner, PARSE_EVENTS__TERM_TYPE_CONFIG); }
280 config1 { return term(yyscanner, PARSE_EVENTS__TERM_TYPE_CONFIG1); }
281 config2 { return term(yyscanner, PARSE_EVENTS__TERM_TYPE_CONFIG2); }
282 name { return term(yyscanner, PARSE_EVENTS__TERM_TYPE_NAME); }
283 period { return term(yyscanner, PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD); }
284 freq { return term(yyscanner, PARSE_EVENTS__TERM_TYPE_SAMPLE_FREQ); }
285 branch_type { return term(yyscanner, PARSE_EVENTS__TERM_TYPE_BRANCH_SAMPLE_TYPE); }
286 time { return term(yyscanner, PARSE_EVENTS__TERM_TYPE_TIME); }
287 call-graph { return term(yyscanner, PARSE_EVENTS__TERM_TYPE_CALLGRAPH); }
288 stack-size { return term(yyscanner, PARSE_EVENTS__TERM_TYPE_STACKSIZE); }
289 max-stack { return term(yyscanner, PARSE_EVENTS__TERM_TYPE_MAX_STACK); }
290 nr { return term(yyscanner, PARSE_EVENTS__TERM_TYPE_MAX_EVENTS); }
291 inherit { return term(yyscanner, PARSE_EVENTS__TERM_TYPE_INHERIT); }
292 no-inherit { return term(yyscanner, PARSE_EVENTS__TERM_TYPE_NOINHERIT); }
293 overwrite { return term(yyscanner, PARSE_EVENTS__TERM_TYPE_OVERWRITE); }
294 no-overwrite { return term(yyscanner, PARSE_EVENTS__TERM_TYPE_NOOVERWRITE); }
295 percore { return term(yyscanner, PARSE_EVENTS__TERM_TYPE_PERCORE); }
296 aux-output { return term(yyscanner, PARSE_EVENTS__TERM_TYPE_AUX_OUTPUT); }
297 aux-sample-size { return term(yyscanner, PARSE_EVENTS__TERM_TYPE_AUX_SAMPLE_SIZE); }
298 r{num_raw_hex} { return raw(yyscanner); }
299 r0x{num_raw_hex} { return raw(yyscanner); }
301 "/" { BEGIN(INITIAL); return '/'; }
302 {name_minus} { return str(yyscanner, PE_NAME); }
303 \[all\] { return PE_ARRAY_ALL; }
304 "[" { BEGIN(array); return '['; }
305 @{drv_cfg_term} { return drv_str(yyscanner, PE_DRV_CFG_TERM); }
309 {modifier_bp} { return str(yyscanner, PE_MODIFIER_BP); }
312 {num_dec} { return value(yyscanner, 10); }
313 {num_hex} { return value(yyscanner, 16); }
315 * We need to separate 'mem:' scanner part, in order to get specific
316 * modifier bits parsed out. Otherwise we would need to handle PE_NAME
317 * and we'd need to parse it manually. During the escape from <mem>
318 * state we need to put the escaping char back, so we dont miss it.
320 . { unput(*yytext); BEGIN(INITIAL); }
322 * We destroy the scanner after reaching EOF,
323 * but anyway just to be sure get back to INIT state.
325 <<EOF>> { BEGIN(INITIAL); }
328 cpu-cycles|cycles { return sym(yyscanner, PERF_TYPE_HARDWARE, PERF_COUNT_HW_CPU_CYCLES); }
329 stalled-cycles-frontend|idle-cycles-frontend { return sym(yyscanner, PERF_TYPE_HARDWARE, PERF_COUNT_HW_STALLED_CYCLES_FRONTEND); }
330 stalled-cycles-backend|idle-cycles-backend { return sym(yyscanner, PERF_TYPE_HARDWARE, PERF_COUNT_HW_STALLED_CYCLES_BACKEND); }
331 instructions { return sym(yyscanner, PERF_TYPE_HARDWARE, PERF_COUNT_HW_INSTRUCTIONS); }
332 cache-references { return sym(yyscanner, PERF_TYPE_HARDWARE, PERF_COUNT_HW_CACHE_REFERENCES); }
333 cache-misses { return sym(yyscanner, PERF_TYPE_HARDWARE, PERF_COUNT_HW_CACHE_MISSES); }
334 branch-instructions|branches { return sym(yyscanner, PERF_TYPE_HARDWARE, PERF_COUNT_HW_BRANCH_INSTRUCTIONS); }
335 branch-misses { return sym(yyscanner, PERF_TYPE_HARDWARE, PERF_COUNT_HW_BRANCH_MISSES); }
336 bus-cycles { return sym(yyscanner, PERF_TYPE_HARDWARE, PERF_COUNT_HW_BUS_CYCLES); }
337 ref-cycles { return sym(yyscanner, PERF_TYPE_HARDWARE, PERF_COUNT_HW_REF_CPU_CYCLES); }
338 cpu-clock { return sym(yyscanner, PERF_TYPE_SOFTWARE, PERF_COUNT_SW_CPU_CLOCK); }
339 task-clock { return sym(yyscanner, PERF_TYPE_SOFTWARE, PERF_COUNT_SW_TASK_CLOCK); }
340 page-faults|faults { return sym(yyscanner, PERF_TYPE_SOFTWARE, PERF_COUNT_SW_PAGE_FAULTS); }
341 minor-faults { return sym(yyscanner, PERF_TYPE_SOFTWARE, PERF_COUNT_SW_PAGE_FAULTS_MIN); }
342 major-faults { return sym(yyscanner, PERF_TYPE_SOFTWARE, PERF_COUNT_SW_PAGE_FAULTS_MAJ); }
343 context-switches|cs { return sym(yyscanner, PERF_TYPE_SOFTWARE, PERF_COUNT_SW_CONTEXT_SWITCHES); }
344 cpu-migrations|migrations { return sym(yyscanner, PERF_TYPE_SOFTWARE, PERF_COUNT_SW_CPU_MIGRATIONS); }
345 alignment-faults { return sym(yyscanner, PERF_TYPE_SOFTWARE, PERF_COUNT_SW_ALIGNMENT_FAULTS); }
346 emulation-faults { return sym(yyscanner, PERF_TYPE_SOFTWARE, PERF_COUNT_SW_EMULATION_FAULTS); }
347 dummy { return sym(yyscanner, PERF_TYPE_SOFTWARE, PERF_COUNT_SW_DUMMY); }
348 duration_time { return tool(yyscanner, PERF_TOOL_DURATION_TIME); }
349 bpf-output { return sym(yyscanner, PERF_TYPE_SOFTWARE, PERF_COUNT_SW_BPF_OUTPUT); }
352 * We have to handle the kernel PMU event cycles-ct/cycles-t/mem-loads/mem-stores separately.
353 * Because the prefix cycles is mixed up with cpu-cycles.
354 * loads and stores are mixed up with cache event
361 tx-capacity-[a-z-]+ |
362 el-capacity-[a-z-]+ { return str(yyscanner, PE_KERNEL_PMU_EVENT); }
364 L1-dcache|l1-d|l1d|L1-data |
365 L1-icache|l1-i|l1i|L1-instruction |
367 dTLB|d-tlb|Data-TLB |
368 iTLB|i-tlb|Instruction-TLB |
369 branch|branches|bpu|btb|bpc |
370 node { return str(yyscanner, PE_NAME_CACHE_TYPE); }
374 prefetch|prefetches |
375 speculative-read|speculative-load |
376 refs|Reference|ops|access |
377 misses|miss { return str(yyscanner, PE_NAME_CACHE_OP_RESULT); }
379 mem: { BEGIN(mem); return PE_PREFIX_MEM; }
380 r{num_raw_hex} { return raw(yyscanner); }
381 {num_dec} { return value(yyscanner, 10); }
382 {num_hex} { return value(yyscanner, 16); }
384 {modifier_event} { return str(yyscanner, PE_MODIFIER_EVENT); }
385 {bpf_object} { if (!isbpf(yyscanner)) { USER_REJECT }; return str(yyscanner, PE_BPF_OBJECT); }
386 {bpf_source} { if (!isbpf(yyscanner)) { USER_REJECT }; return str(yyscanner, PE_BPF_SOURCE); }
387 {name} { return pmu_str_check(yyscanner, _parse_state); }
388 {name_tag} { return str(yyscanner, PE_NAME); }
389 "/" { BEGIN(config); return '/'; }
391 , { BEGIN(event); return ','; }
393 "{" { BEGIN(event); return '{'; }
401 int parse_events_wrap(void *scanner __maybe_unused)