4 %option prefix="parse_events_"
6 %option bison-locations
12 #include <sys/types.h>
15 #include "parse-events.h"
16 #include "parse-events-bison.h"
19 char *parse_events_get_text(yyscan_t yyscanner);
20 YYSTYPE *parse_events_get_lval(yyscan_t yyscanner);
22 static int __value(YYSTYPE *yylval, char *str, int base, int token)
27 num = strtoull(str, NULL, base);
35 static int value(yyscan_t scanner, int base)
37 YYSTYPE *yylval = parse_events_get_lval(scanner);
38 char *text = parse_events_get_text(scanner);
40 return __value(yylval, text, base, PE_VALUE);
43 static int str(yyscan_t scanner, int token)
45 YYSTYPE *yylval = parse_events_get_lval(scanner);
46 char *text = parse_events_get_text(scanner);
48 if (text[0] != '\'') {
49 yylval->str = strdup(text);
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
56 * name=\'CPU_CLK_UNHALTED.THREAD:cmask=1\'
57 * quotes need to be escaped to bypass shell
60 yylval->str = strndup(&text[1], strlen(text) - 2);
66 static int lc_str(yyscan_t scanner, const struct parse_events_state *state)
68 return str(scanner, state->match_legacy_cache_terms ? PE_LEGACY_CACHE : PE_NAME);
71 static bool isbpf_suffix(char *text)
73 int len = strlen(text);
77 if ((text[len - 1] == 'c' || text[len - 1] == 'o') &&
80 if (len > 4 && !strcmp(text + len - 4, ".obj"))
85 static bool isbpf(yyscan_t scanner)
87 char *text = parse_events_get_text(scanner);
90 if (!isbpf_suffix(text))
93 return stat(text, &st) == 0;
97 * This function is called when the parser gets two kind of input:
99 * @cfg1 or @cfg2=config
101 * The leading '@' is stripped off before 'cfg1' and 'cfg2=config' are given to
102 * bison. In the latter case it is necessary to keep the string intact so that
103 * the PMU kernel driver can determine what configurable is associated to
106 static int drv_str(yyscan_t scanner, int token)
108 YYSTYPE *yylval = parse_events_get_lval(scanner);
109 char *text = parse_events_get_text(scanner);
111 /* Strip off the '@' */
112 yylval->str = strdup(text + 1);
116 #define REWIND(__alloc) \
118 YYSTYPE *__yylval = parse_events_get_lval(yyscanner); \
119 char *text = parse_events_get_text(yyscanner); \
122 __yylval->str = strdup(text); \
124 yycolumn -= strlen(text); \
128 static int sym(yyscan_t scanner, int type, int config)
130 YYSTYPE *yylval = parse_events_get_lval(scanner);
132 yylval->num = (type << 16) + config;
133 return type == PERF_TYPE_HARDWARE ? PE_VALUE_SYM_HW : PE_VALUE_SYM_SW;
136 static int tool(yyscan_t scanner, enum perf_tool_event event)
138 YYSTYPE *yylval = parse_events_get_lval(scanner);
141 return PE_VALUE_SYM_TOOL;
144 static int term(yyscan_t scanner, int type)
146 YYSTYPE *yylval = parse_events_get_lval(scanner);
152 static int hw_term(yyscan_t scanner, int config)
154 YYSTYPE *yylval = parse_events_get_lval(scanner);
155 char *text = parse_events_get_text(scanner);
157 yylval->hardware_term.str = strdup(text);
158 yylval->hardware_term.num = PERF_TYPE_HARDWARE + config;
162 #define YY_USER_ACTION \
164 yylloc->last_column = yylloc->first_column; \
165 yylloc->first_column = yycolumn; \
166 yycolumn += yyleng; \
169 #define USER_REJECT \
170 yycolumn -= yyleng; \
180 group [^,{}/]*[{][^}]*[}][^,{}/]*
181 event_pmu [^,{}/]+[/][^/]*[/][^,{}/]*
183 bpf_object [^,{}]+\.(o|bpf)[a-zA-Z0-9._]*
184 bpf_source [^,{}]+\.c[a-zA-Z0-9._]*
187 num_hex 0x[a-fA-F0-9]+
188 num_raw_hex [a-fA-F0-9]+
189 name [a-zA-Z_*?\[\]][a-zA-Z0-9_*?.\[\]!\-]*
190 name_tag [\'][a-zA-Z_*?\[\]][a-zA-Z0-9_*?\-,\.\[\]:=]*[\']
191 name_minus [a-zA-Z_*?][a-zA-Z0-9\-_*?.:]*
192 drv_cfg_term [a-zA-Z0-9_\.]+(=[a-zA-Z0-9_*?\.:]+)?
194 * If you add a modifier you need to update check_modifier().
195 * Also, the letters in modifier_event must not be in modifier_bp.
197 modifier_event [ukhpPGHSDIWeb]+
198 modifier_bp [rwx]{1,3}
199 lc_type (L1-dcache|l1-d|l1d|L1-data|L1-icache|l1-i|l1i|L1-instruction|LLC|L2|dTLB|d-tlb|Data-TLB|iTLB|i-tlb|Instruction-TLB|branch|branches|bpu|btb|bpc|node)
200 lc_op_result (load|loads|read|store|stores|write|prefetch|prefetches|speculative-read|speculative-load|refs|Reference|ops|access|misses|miss)
207 struct parse_events_state *_parse_state = parse_events_get_extra(yyscanner);
209 int start_token = _parse_state->stoken;
211 if (start_token == PE_START_TERMS)
213 else if (start_token == PE_START_EVENTS)
217 _parse_state->stoken = 0;
219 * The flex parser does not init locations variable
220 * via the scan_string interface, so we need do the
242 return PE_EVENT_NAME;
255 "]" { BEGIN(config); return ']'; }
256 {num_dec} { return value(yyscanner, 10); }
257 {num_hex} { return value(yyscanner, 16); }
259 "\.\.\." { return PE_ARRAY_RANGE; }
264 * Please update config_term_names when new static term is added.
266 config { return term(yyscanner, PARSE_EVENTS__TERM_TYPE_CONFIG); }
267 config1 { return term(yyscanner, PARSE_EVENTS__TERM_TYPE_CONFIG1); }
268 config2 { return term(yyscanner, PARSE_EVENTS__TERM_TYPE_CONFIG2); }
269 config3 { return term(yyscanner, PARSE_EVENTS__TERM_TYPE_CONFIG3); }
270 name { return term(yyscanner, PARSE_EVENTS__TERM_TYPE_NAME); }
271 period { return term(yyscanner, PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD); }
272 freq { return term(yyscanner, PARSE_EVENTS__TERM_TYPE_SAMPLE_FREQ); }
273 branch_type { return term(yyscanner, PARSE_EVENTS__TERM_TYPE_BRANCH_SAMPLE_TYPE); }
274 time { return term(yyscanner, PARSE_EVENTS__TERM_TYPE_TIME); }
275 call-graph { return term(yyscanner, PARSE_EVENTS__TERM_TYPE_CALLGRAPH); }
276 stack-size { return term(yyscanner, PARSE_EVENTS__TERM_TYPE_STACKSIZE); }
277 max-stack { return term(yyscanner, PARSE_EVENTS__TERM_TYPE_MAX_STACK); }
278 nr { return term(yyscanner, PARSE_EVENTS__TERM_TYPE_MAX_EVENTS); }
279 inherit { return term(yyscanner, PARSE_EVENTS__TERM_TYPE_INHERIT); }
280 no-inherit { return term(yyscanner, PARSE_EVENTS__TERM_TYPE_NOINHERIT); }
281 overwrite { return term(yyscanner, PARSE_EVENTS__TERM_TYPE_OVERWRITE); }
282 no-overwrite { return term(yyscanner, PARSE_EVENTS__TERM_TYPE_NOOVERWRITE); }
283 percore { return term(yyscanner, PARSE_EVENTS__TERM_TYPE_PERCORE); }
284 aux-output { return term(yyscanner, PARSE_EVENTS__TERM_TYPE_AUX_OUTPUT); }
285 aux-sample-size { return term(yyscanner, PARSE_EVENTS__TERM_TYPE_AUX_SAMPLE_SIZE); }
286 metric-id { return term(yyscanner, PARSE_EVENTS__TERM_TYPE_METRIC_ID); }
287 cpu-cycles|cycles { return hw_term(yyscanner, PERF_COUNT_HW_CPU_CYCLES); }
288 stalled-cycles-frontend|idle-cycles-frontend { return hw_term(yyscanner, PERF_COUNT_HW_STALLED_CYCLES_FRONTEND); }
289 stalled-cycles-backend|idle-cycles-backend { return hw_term(yyscanner, PERF_COUNT_HW_STALLED_CYCLES_BACKEND); }
290 instructions { return hw_term(yyscanner, PERF_COUNT_HW_INSTRUCTIONS); }
291 cache-references { return hw_term(yyscanner, PERF_COUNT_HW_CACHE_REFERENCES); }
292 cache-misses { return hw_term(yyscanner, PERF_COUNT_HW_CACHE_MISSES); }
293 branch-instructions|branches { return hw_term(yyscanner, PERF_COUNT_HW_BRANCH_INSTRUCTIONS); }
294 branch-misses { return hw_term(yyscanner, PERF_COUNT_HW_BRANCH_MISSES); }
295 bus-cycles { return hw_term(yyscanner, PERF_COUNT_HW_BUS_CYCLES); }
296 ref-cycles { return hw_term(yyscanner, PERF_COUNT_HW_REF_CPU_CYCLES); }
297 r{num_raw_hex} { return str(yyscanner, PE_RAW); }
298 r0x{num_raw_hex} { return str(yyscanner, PE_RAW); }
300 "/" { BEGIN(INITIAL); return '/'; }
301 {lc_type} { return lc_str(yyscanner, _parse_state); }
302 {lc_type}-{lc_op_result} { return lc_str(yyscanner, _parse_state); }
303 {lc_type}-{lc_op_result}-{lc_op_result} { return lc_str(yyscanner, _parse_state); }
304 {name_minus} { return str(yyscanner, PE_NAME); }
305 \[all\] { return PE_ARRAY_ALL; }
306 "[" { BEGIN(array); return '['; }
307 @{drv_cfg_term} { return drv_str(yyscanner, PE_DRV_CFG_TERM); }
311 {modifier_bp} { return str(yyscanner, PE_MODIFIER_BP); }
313 * The colon before memory access modifiers can get mixed up with the
314 * colon before event modifiers. Fortunately none of the option letters
315 * are the same, so trailing context can be used disambiguate the two
318 ":"/{modifier_bp} { return PE_BP_COLON; }
320 * The slash before memory length can get mixed up with the slash before
321 * config terms. Fortunately config terms do not start with a numeric
322 * digit, so trailing context can be used disambiguate the two cases.
324 "/"/{digit} { return PE_BP_SLASH; }
325 "/"/{non_digit} { BEGIN(config); return '/'; }
326 {num_dec} { return value(yyscanner, 10); }
327 {num_hex} { return value(yyscanner, 16); }
329 * We need to separate 'mem:' scanner part, in order to get specific
330 * modifier bits parsed out. Otherwise we would need to handle PE_NAME
331 * and we'd need to parse it manually. During the escape from <mem>
332 * state we need to put the escaping char back, so we dont miss it.
334 . { unput(*yytext); BEGIN(INITIAL); }
336 * We destroy the scanner after reaching EOF,
337 * but anyway just to be sure get back to INIT state.
339 <<EOF>> { BEGIN(INITIAL); }
342 cpu-cycles|cycles { return sym(yyscanner, PERF_TYPE_HARDWARE, PERF_COUNT_HW_CPU_CYCLES); }
343 stalled-cycles-frontend|idle-cycles-frontend { return sym(yyscanner, PERF_TYPE_HARDWARE, PERF_COUNT_HW_STALLED_CYCLES_FRONTEND); }
344 stalled-cycles-backend|idle-cycles-backend { return sym(yyscanner, PERF_TYPE_HARDWARE, PERF_COUNT_HW_STALLED_CYCLES_BACKEND); }
345 instructions { return sym(yyscanner, PERF_TYPE_HARDWARE, PERF_COUNT_HW_INSTRUCTIONS); }
346 cache-references { return sym(yyscanner, PERF_TYPE_HARDWARE, PERF_COUNT_HW_CACHE_REFERENCES); }
347 cache-misses { return sym(yyscanner, PERF_TYPE_HARDWARE, PERF_COUNT_HW_CACHE_MISSES); }
348 branch-instructions|branches { return sym(yyscanner, PERF_TYPE_HARDWARE, PERF_COUNT_HW_BRANCH_INSTRUCTIONS); }
349 branch-misses { return sym(yyscanner, PERF_TYPE_HARDWARE, PERF_COUNT_HW_BRANCH_MISSES); }
350 bus-cycles { return sym(yyscanner, PERF_TYPE_HARDWARE, PERF_COUNT_HW_BUS_CYCLES); }
351 ref-cycles { return sym(yyscanner, PERF_TYPE_HARDWARE, PERF_COUNT_HW_REF_CPU_CYCLES); }
352 cpu-clock { return sym(yyscanner, PERF_TYPE_SOFTWARE, PERF_COUNT_SW_CPU_CLOCK); }
353 task-clock { return sym(yyscanner, PERF_TYPE_SOFTWARE, PERF_COUNT_SW_TASK_CLOCK); }
354 page-faults|faults { return sym(yyscanner, PERF_TYPE_SOFTWARE, PERF_COUNT_SW_PAGE_FAULTS); }
355 minor-faults { return sym(yyscanner, PERF_TYPE_SOFTWARE, PERF_COUNT_SW_PAGE_FAULTS_MIN); }
356 major-faults { return sym(yyscanner, PERF_TYPE_SOFTWARE, PERF_COUNT_SW_PAGE_FAULTS_MAJ); }
357 context-switches|cs { return sym(yyscanner, PERF_TYPE_SOFTWARE, PERF_COUNT_SW_CONTEXT_SWITCHES); }
358 cpu-migrations|migrations { return sym(yyscanner, PERF_TYPE_SOFTWARE, PERF_COUNT_SW_CPU_MIGRATIONS); }
359 alignment-faults { return sym(yyscanner, PERF_TYPE_SOFTWARE, PERF_COUNT_SW_ALIGNMENT_FAULTS); }
360 emulation-faults { return sym(yyscanner, PERF_TYPE_SOFTWARE, PERF_COUNT_SW_EMULATION_FAULTS); }
361 dummy { return sym(yyscanner, PERF_TYPE_SOFTWARE, PERF_COUNT_SW_DUMMY); }
362 duration_time { return tool(yyscanner, PERF_TOOL_DURATION_TIME); }
363 user_time { return tool(yyscanner, PERF_TOOL_USER_TIME); }
364 system_time { return tool(yyscanner, PERF_TOOL_SYSTEM_TIME); }
365 bpf-output { return sym(yyscanner, PERF_TYPE_SOFTWARE, PERF_COUNT_SW_BPF_OUTPUT); }
366 cgroup-switches { return sym(yyscanner, PERF_TYPE_SOFTWARE, PERF_COUNT_SW_CGROUP_SWITCHES); }
368 {lc_type} { return str(yyscanner, PE_LEGACY_CACHE); }
369 {lc_type}-{lc_op_result} { return str(yyscanner, PE_LEGACY_CACHE); }
370 {lc_type}-{lc_op_result}-{lc_op_result} { return str(yyscanner, PE_LEGACY_CACHE); }
371 mem: { BEGIN(mem); return PE_PREFIX_MEM; }
372 r{num_raw_hex} { return str(yyscanner, PE_RAW); }
373 {num_dec} { return value(yyscanner, 10); }
374 {num_hex} { return value(yyscanner, 16); }
376 {modifier_event} { return str(yyscanner, PE_MODIFIER_EVENT); }
377 {bpf_object} { if (!isbpf(yyscanner)) { USER_REJECT }; return str(yyscanner, PE_BPF_OBJECT); }
378 {bpf_source} { if (!isbpf(yyscanner)) { USER_REJECT }; return str(yyscanner, PE_BPF_SOURCE); }
379 {name} { return str(yyscanner, PE_NAME); }
380 {name_tag} { return str(yyscanner, PE_NAME); }
381 "/" { BEGIN(config); return '/'; }
382 , { BEGIN(event); return ','; }
384 "{" { BEGIN(event); return '{'; }
392 int parse_events_wrap(void *scanner __maybe_unused)