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"
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 raw(yyscan_t scanner)
45 YYSTYPE *yylval = parse_events_get_lval(scanner);
46 char *text = parse_events_get_text(scanner);
48 return __value(yylval, text + 1, 16, PE_RAW);
51 static int str(yyscan_t scanner, int token)
53 YYSTYPE *yylval = parse_events_get_lval(scanner);
54 char *text = parse_events_get_text(scanner);
56 yylval->str = strdup(text);
60 static bool isbpf_suffix(char *text)
62 int len = strlen(text);
66 if ((text[len - 1] == 'c' || text[len - 1] == 'o') &&
69 if (len > 4 && !strcmp(text + len - 4, ".obj"))
74 static bool isbpf(yyscan_t scanner)
76 char *text = parse_events_get_text(scanner);
79 if (!isbpf_suffix(text))
82 return stat(text, &st) == 0;
86 * This function is called when the parser gets two kind of input:
88 * @cfg1 or @cfg2=config
90 * The leading '@' is stripped off before 'cfg1' and 'cfg2=config' are given to
91 * bison. In the latter case it is necessary to keep the string intact so that
92 * the PMU kernel driver can determine what configurable is associated to
95 static int drv_str(yyscan_t scanner, int token)
97 YYSTYPE *yylval = parse_events_get_lval(scanner);
98 char *text = parse_events_get_text(scanner);
100 /* Strip off the '@' */
101 yylval->str = strdup(text + 1);
105 #define REWIND(__alloc) \
107 YYSTYPE *__yylval = parse_events_get_lval(yyscanner); \
108 char *text = parse_events_get_text(yyscanner); \
111 __yylval->str = strdup(text); \
113 yycolumn -= strlen(text); \
117 static int pmu_str_check(yyscan_t scanner)
119 YYSTYPE *yylval = parse_events_get_lval(scanner);
120 char *text = parse_events_get_text(scanner);
122 yylval->str = strdup(text);
123 switch (perf_pmu__parse_check(text)) {
124 case PMU_EVENT_SYMBOL_PREFIX:
125 return PE_PMU_EVENT_PRE;
126 case PMU_EVENT_SYMBOL_SUFFIX:
127 return PE_PMU_EVENT_SUF;
128 case PMU_EVENT_SYMBOL:
129 return PE_KERNEL_PMU_EVENT;
135 static int sym(yyscan_t scanner, int type, int config)
137 YYSTYPE *yylval = parse_events_get_lval(scanner);
139 yylval->num = (type << 16) + config;
140 return type == PERF_TYPE_HARDWARE ? PE_VALUE_SYM_HW : PE_VALUE_SYM_SW;
143 static int term(yyscan_t scanner, int type)
145 YYSTYPE *yylval = parse_events_get_lval(scanner);
151 #define YY_USER_ACTION \
153 yylloc->last_column = yylloc->first_column; \
154 yylloc->first_column = yycolumn; \
155 yycolumn += yyleng; \
158 #define USER_REJECT \
159 yycolumn -= yyleng; \
169 group [^,{}/]*[{][^}]*[}][^,{}/]*
170 event_pmu [^,{}/]+[/][^/]*[/][^,{}/]*
172 bpf_object [^,{}]+\.(o|bpf)[a-zA-Z0-9._]*
173 bpf_source [^,{}]+\.c[a-zA-Z0-9._]*
176 num_hex 0x[a-fA-F0-9]+
177 num_raw_hex [a-fA-F0-9]+
178 name [a-zA-Z_*?\[\]][a-zA-Z0-9_*?.\[\]]*
179 name_minus [a-zA-Z_*?][a-zA-Z0-9\-_*?.:]*
180 drv_cfg_term [a-zA-Z0-9_\.]+(=[a-zA-Z0-9_*?\.:]+)?
181 /* If you add a modifier you need to update check_modifier() */
182 modifier_event [ukhpPGHSDIW]+
183 modifier_bp [rwx]{1,3}
191 start_token = parse_events_get_extra(yyscanner);
193 if (start_token == PE_START_TERMS)
195 else if (start_token == PE_START_EVENTS)
199 parse_events_set_extra(NULL, yyscanner);
201 * The flex parser does not init locations variable
202 * via the scan_string interface, so we need do the
224 return PE_EVENT_NAME;
235 "]" { BEGIN(config); return ']'; }
236 {num_dec} { return value(yyscanner, 10); }
237 {num_hex} { return value(yyscanner, 16); }
239 "\.\.\." { return PE_ARRAY_RANGE; }
244 * Please update config_term_names when new static term is added.
246 config { return term(yyscanner, PARSE_EVENTS__TERM_TYPE_CONFIG); }
247 config1 { return term(yyscanner, PARSE_EVENTS__TERM_TYPE_CONFIG1); }
248 config2 { return term(yyscanner, PARSE_EVENTS__TERM_TYPE_CONFIG2); }
249 name { return term(yyscanner, PARSE_EVENTS__TERM_TYPE_NAME); }
250 period { return term(yyscanner, PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD); }
251 freq { return term(yyscanner, PARSE_EVENTS__TERM_TYPE_SAMPLE_FREQ); }
252 branch_type { return term(yyscanner, PARSE_EVENTS__TERM_TYPE_BRANCH_SAMPLE_TYPE); }
253 time { return term(yyscanner, PARSE_EVENTS__TERM_TYPE_TIME); }
254 call-graph { return term(yyscanner, PARSE_EVENTS__TERM_TYPE_CALLGRAPH); }
255 stack-size { return term(yyscanner, PARSE_EVENTS__TERM_TYPE_STACKSIZE); }
256 max-stack { return term(yyscanner, PARSE_EVENTS__TERM_TYPE_MAX_STACK); }
257 inherit { return term(yyscanner, PARSE_EVENTS__TERM_TYPE_INHERIT); }
258 no-inherit { return term(yyscanner, PARSE_EVENTS__TERM_TYPE_NOINHERIT); }
259 overwrite { return term(yyscanner, PARSE_EVENTS__TERM_TYPE_OVERWRITE); }
260 no-overwrite { return term(yyscanner, PARSE_EVENTS__TERM_TYPE_NOOVERWRITE); }
262 "/" { BEGIN(INITIAL); return '/'; }
263 {name_minus} { return str(yyscanner, PE_NAME); }
264 \[all\] { return PE_ARRAY_ALL; }
265 "[" { BEGIN(array); return '['; }
266 @{drv_cfg_term} { return drv_str(yyscanner, PE_DRV_CFG_TERM); }
270 {modifier_bp} { return str(yyscanner, PE_MODIFIER_BP); }
273 {num_dec} { return value(yyscanner, 10); }
274 {num_hex} { return value(yyscanner, 16); }
276 * We need to separate 'mem:' scanner part, in order to get specific
277 * modifier bits parsed out. Otherwise we would need to handle PE_NAME
278 * and we'd need to parse it manually. During the escape from <mem>
279 * state we need to put the escaping char back, so we dont miss it.
281 . { unput(*yytext); BEGIN(INITIAL); }
283 * We destroy the scanner after reaching EOF,
284 * but anyway just to be sure get back to INIT state.
286 <<EOF>> { BEGIN(INITIAL); }
289 cpu-cycles|cycles { return sym(yyscanner, PERF_TYPE_HARDWARE, PERF_COUNT_HW_CPU_CYCLES); }
290 stalled-cycles-frontend|idle-cycles-frontend { return sym(yyscanner, PERF_TYPE_HARDWARE, PERF_COUNT_HW_STALLED_CYCLES_FRONTEND); }
291 stalled-cycles-backend|idle-cycles-backend { return sym(yyscanner, PERF_TYPE_HARDWARE, PERF_COUNT_HW_STALLED_CYCLES_BACKEND); }
292 instructions { return sym(yyscanner, PERF_TYPE_HARDWARE, PERF_COUNT_HW_INSTRUCTIONS); }
293 cache-references { return sym(yyscanner, PERF_TYPE_HARDWARE, PERF_COUNT_HW_CACHE_REFERENCES); }
294 cache-misses { return sym(yyscanner, PERF_TYPE_HARDWARE, PERF_COUNT_HW_CACHE_MISSES); }
295 branch-instructions|branches { return sym(yyscanner, PERF_TYPE_HARDWARE, PERF_COUNT_HW_BRANCH_INSTRUCTIONS); }
296 branch-misses { return sym(yyscanner, PERF_TYPE_HARDWARE, PERF_COUNT_HW_BRANCH_MISSES); }
297 bus-cycles { return sym(yyscanner, PERF_TYPE_HARDWARE, PERF_COUNT_HW_BUS_CYCLES); }
298 ref-cycles { return sym(yyscanner, PERF_TYPE_HARDWARE, PERF_COUNT_HW_REF_CPU_CYCLES); }
299 cpu-clock { return sym(yyscanner, PERF_TYPE_SOFTWARE, PERF_COUNT_SW_CPU_CLOCK); }
300 task-clock { return sym(yyscanner, PERF_TYPE_SOFTWARE, PERF_COUNT_SW_TASK_CLOCK); }
301 page-faults|faults { return sym(yyscanner, PERF_TYPE_SOFTWARE, PERF_COUNT_SW_PAGE_FAULTS); }
302 minor-faults { return sym(yyscanner, PERF_TYPE_SOFTWARE, PERF_COUNT_SW_PAGE_FAULTS_MIN); }
303 major-faults { return sym(yyscanner, PERF_TYPE_SOFTWARE, PERF_COUNT_SW_PAGE_FAULTS_MAJ); }
304 context-switches|cs { return sym(yyscanner, PERF_TYPE_SOFTWARE, PERF_COUNT_SW_CONTEXT_SWITCHES); }
305 cpu-migrations|migrations { return sym(yyscanner, PERF_TYPE_SOFTWARE, PERF_COUNT_SW_CPU_MIGRATIONS); }
306 alignment-faults { return sym(yyscanner, PERF_TYPE_SOFTWARE, PERF_COUNT_SW_ALIGNMENT_FAULTS); }
307 emulation-faults { return sym(yyscanner, PERF_TYPE_SOFTWARE, PERF_COUNT_SW_EMULATION_FAULTS); }
308 dummy { return sym(yyscanner, PERF_TYPE_SOFTWARE, PERF_COUNT_SW_DUMMY); }
309 duration_time { return sym(yyscanner, PERF_TYPE_SOFTWARE, PERF_COUNT_SW_DUMMY); }
310 bpf-output { return sym(yyscanner, PERF_TYPE_SOFTWARE, PERF_COUNT_SW_BPF_OUTPUT); }
313 * We have to handle the kernel PMU event cycles-ct/cycles-t/mem-loads/mem-stores separately.
314 * Because the prefix cycles is mixed up with cpu-cycles.
315 * loads and stores are mixed up with cache event
317 cycles-ct { return str(yyscanner, PE_KERNEL_PMU_EVENT); }
318 cycles-t { return str(yyscanner, PE_KERNEL_PMU_EVENT); }
319 mem-loads { return str(yyscanner, PE_KERNEL_PMU_EVENT); }
320 mem-stores { return str(yyscanner, PE_KERNEL_PMU_EVENT); }
321 topdown-[a-z-]+ { return str(yyscanner, PE_KERNEL_PMU_EVENT); }
323 L1-dcache|l1-d|l1d|L1-data |
324 L1-icache|l1-i|l1i|L1-instruction |
326 dTLB|d-tlb|Data-TLB |
327 iTLB|i-tlb|Instruction-TLB |
328 branch|branches|bpu|btb|bpc |
329 node { return str(yyscanner, PE_NAME_CACHE_TYPE); }
333 prefetch|prefetches |
334 speculative-read|speculative-load |
335 refs|Reference|ops|access |
336 misses|miss { return str(yyscanner, PE_NAME_CACHE_OP_RESULT); }
338 mem: { BEGIN(mem); return PE_PREFIX_MEM; }
339 r{num_raw_hex} { return raw(yyscanner); }
340 {num_dec} { return value(yyscanner, 10); }
341 {num_hex} { return value(yyscanner, 16); }
343 {modifier_event} { return str(yyscanner, PE_MODIFIER_EVENT); }
344 {bpf_object} { if (!isbpf(yyscanner)) { USER_REJECT }; return str(yyscanner, PE_BPF_OBJECT); }
345 {bpf_source} { if (!isbpf(yyscanner)) { USER_REJECT }; return str(yyscanner, PE_BPF_SOURCE); }
346 {name} { return pmu_str_check(yyscanner); }
347 "/" { BEGIN(config); return '/'; }
349 , { BEGIN(event); return ','; }
351 "{" { BEGIN(event); return '{'; }
359 int parse_events_wrap(void *scanner __maybe_unused)