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 if (text[0] != '\'') {
57 yylval->str = strdup(text);
60 * If a text tag specified on the command line
61 * contains opening single quite ' then it is
62 * expected that the tag ends with single quote
64 * name=\'CPU_CLK_UNHALTED.THREAD:cmask=1\'
65 * quotes need to be escaped to bypass shell
68 yylval->str = strndup(&text[1], strlen(text) - 2);
74 static bool isbpf_suffix(char *text)
76 int len = strlen(text);
80 if ((text[len - 1] == 'c' || text[len - 1] == 'o') &&
83 if (len > 4 && !strcmp(text + len - 4, ".obj"))
88 static bool isbpf(yyscan_t scanner)
90 char *text = parse_events_get_text(scanner);
93 if (!isbpf_suffix(text))
96 return stat(text, &st) == 0;
100 * This function is called when the parser gets two kind of input:
102 * @cfg1 or @cfg2=config
104 * The leading '@' is stripped off before 'cfg1' and 'cfg2=config' are given to
105 * bison. In the latter case it is necessary to keep the string intact so that
106 * the PMU kernel driver can determine what configurable is associated to
109 static int drv_str(yyscan_t scanner, int token)
111 YYSTYPE *yylval = parse_events_get_lval(scanner);
112 char *text = parse_events_get_text(scanner);
114 /* Strip off the '@' */
115 yylval->str = strdup(text + 1);
119 #define REWIND(__alloc) \
121 YYSTYPE *__yylval = parse_events_get_lval(yyscanner); \
122 char *text = parse_events_get_text(yyscanner); \
125 __yylval->str = strdup(text); \
127 yycolumn -= strlen(text); \
131 static int pmu_str_check(yyscan_t scanner)
133 YYSTYPE *yylval = parse_events_get_lval(scanner);
134 char *text = parse_events_get_text(scanner);
136 yylval->str = strdup(text);
137 switch (perf_pmu__parse_check(text)) {
138 case PMU_EVENT_SYMBOL_PREFIX:
139 return PE_PMU_EVENT_PRE;
140 case PMU_EVENT_SYMBOL_SUFFIX:
141 return PE_PMU_EVENT_SUF;
142 case PMU_EVENT_SYMBOL:
143 return PE_KERNEL_PMU_EVENT;
149 static int sym(yyscan_t scanner, int type, int config)
151 YYSTYPE *yylval = parse_events_get_lval(scanner);
153 yylval->num = (type << 16) + config;
154 return type == PERF_TYPE_HARDWARE ? PE_VALUE_SYM_HW : PE_VALUE_SYM_SW;
157 static int term(yyscan_t scanner, int type)
159 YYSTYPE *yylval = parse_events_get_lval(scanner);
165 #define YY_USER_ACTION \
167 yylloc->last_column = yylloc->first_column; \
168 yylloc->first_column = yycolumn; \
169 yycolumn += yyleng; \
172 #define USER_REJECT \
173 yycolumn -= yyleng; \
183 group [^,{}/]*[{][^}]*[}][^,{}/]*
184 event_pmu [^,{}/]+[/][^/]*[/][^,{}/]*
186 bpf_object [^,{}]+\.(o|bpf)[a-zA-Z0-9._]*
187 bpf_source [^,{}]+\.c[a-zA-Z0-9._]*
190 num_hex 0x[a-fA-F0-9]+
191 num_raw_hex [a-fA-F0-9]+
192 name [a-zA-Z_*?\[\]][a-zA-Z0-9_*?.\[\]]*
193 name_tag [\'][a-zA-Z_*?\[\]][a-zA-Z0-9_*?\-,\.\[\]:=]*[\']
194 name_minus [a-zA-Z_*?][a-zA-Z0-9\-_*?.:]*
195 drv_cfg_term [a-zA-Z0-9_\.]+(=[a-zA-Z0-9_*?\.:]+)?
196 /* If you add a modifier you need to update check_modifier() */
197 modifier_event [ukhpPGHSDIW]+
198 modifier_bp [rwx]{1,3}
206 start_token = parse_events_get_extra(yyscanner);
208 if (start_token == PE_START_TERMS)
210 else if (start_token == PE_START_EVENTS)
214 parse_events_set_extra(NULL, yyscanner);
216 * The flex parser does not init locations variable
217 * via the scan_string interface, so we need do the
239 return PE_EVENT_NAME;
250 "]" { BEGIN(config); return ']'; }
251 {num_dec} { return value(yyscanner, 10); }
252 {num_hex} { return value(yyscanner, 16); }
254 "\.\.\." { return PE_ARRAY_RANGE; }
259 * Please update config_term_names when new static term is added.
261 config { return term(yyscanner, PARSE_EVENTS__TERM_TYPE_CONFIG); }
262 config1 { return term(yyscanner, PARSE_EVENTS__TERM_TYPE_CONFIG1); }
263 config2 { return term(yyscanner, PARSE_EVENTS__TERM_TYPE_CONFIG2); }
264 name { return term(yyscanner, PARSE_EVENTS__TERM_TYPE_NAME); }
265 period { return term(yyscanner, PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD); }
266 freq { return term(yyscanner, PARSE_EVENTS__TERM_TYPE_SAMPLE_FREQ); }
267 branch_type { return term(yyscanner, PARSE_EVENTS__TERM_TYPE_BRANCH_SAMPLE_TYPE); }
268 time { return term(yyscanner, PARSE_EVENTS__TERM_TYPE_TIME); }
269 call-graph { return term(yyscanner, PARSE_EVENTS__TERM_TYPE_CALLGRAPH); }
270 stack-size { return term(yyscanner, PARSE_EVENTS__TERM_TYPE_STACKSIZE); }
271 max-stack { return term(yyscanner, PARSE_EVENTS__TERM_TYPE_MAX_STACK); }
272 nr { return term(yyscanner, PARSE_EVENTS__TERM_TYPE_MAX_EVENTS); }
273 inherit { return term(yyscanner, PARSE_EVENTS__TERM_TYPE_INHERIT); }
274 no-inherit { return term(yyscanner, PARSE_EVENTS__TERM_TYPE_NOINHERIT); }
275 overwrite { return term(yyscanner, PARSE_EVENTS__TERM_TYPE_OVERWRITE); }
276 no-overwrite { return term(yyscanner, PARSE_EVENTS__TERM_TYPE_NOOVERWRITE); }
278 "/" { BEGIN(INITIAL); return '/'; }
279 {name_minus} { return str(yyscanner, PE_NAME); }
280 \[all\] { return PE_ARRAY_ALL; }
281 "[" { BEGIN(array); return '['; }
282 @{drv_cfg_term} { return drv_str(yyscanner, PE_DRV_CFG_TERM); }
286 {modifier_bp} { return str(yyscanner, PE_MODIFIER_BP); }
289 {num_dec} { return value(yyscanner, 10); }
290 {num_hex} { return value(yyscanner, 16); }
292 * We need to separate 'mem:' scanner part, in order to get specific
293 * modifier bits parsed out. Otherwise we would need to handle PE_NAME
294 * and we'd need to parse it manually. During the escape from <mem>
295 * state we need to put the escaping char back, so we dont miss it.
297 . { unput(*yytext); BEGIN(INITIAL); }
299 * We destroy the scanner after reaching EOF,
300 * but anyway just to be sure get back to INIT state.
302 <<EOF>> { BEGIN(INITIAL); }
305 cpu-cycles|cycles { return sym(yyscanner, PERF_TYPE_HARDWARE, PERF_COUNT_HW_CPU_CYCLES); }
306 stalled-cycles-frontend|idle-cycles-frontend { return sym(yyscanner, PERF_TYPE_HARDWARE, PERF_COUNT_HW_STALLED_CYCLES_FRONTEND); }
307 stalled-cycles-backend|idle-cycles-backend { return sym(yyscanner, PERF_TYPE_HARDWARE, PERF_COUNT_HW_STALLED_CYCLES_BACKEND); }
308 instructions { return sym(yyscanner, PERF_TYPE_HARDWARE, PERF_COUNT_HW_INSTRUCTIONS); }
309 cache-references { return sym(yyscanner, PERF_TYPE_HARDWARE, PERF_COUNT_HW_CACHE_REFERENCES); }
310 cache-misses { return sym(yyscanner, PERF_TYPE_HARDWARE, PERF_COUNT_HW_CACHE_MISSES); }
311 branch-instructions|branches { return sym(yyscanner, PERF_TYPE_HARDWARE, PERF_COUNT_HW_BRANCH_INSTRUCTIONS); }
312 branch-misses { return sym(yyscanner, PERF_TYPE_HARDWARE, PERF_COUNT_HW_BRANCH_MISSES); }
313 bus-cycles { return sym(yyscanner, PERF_TYPE_HARDWARE, PERF_COUNT_HW_BUS_CYCLES); }
314 ref-cycles { return sym(yyscanner, PERF_TYPE_HARDWARE, PERF_COUNT_HW_REF_CPU_CYCLES); }
315 cpu-clock { return sym(yyscanner, PERF_TYPE_SOFTWARE, PERF_COUNT_SW_CPU_CLOCK); }
316 task-clock { return sym(yyscanner, PERF_TYPE_SOFTWARE, PERF_COUNT_SW_TASK_CLOCK); }
317 page-faults|faults { return sym(yyscanner, PERF_TYPE_SOFTWARE, PERF_COUNT_SW_PAGE_FAULTS); }
318 minor-faults { return sym(yyscanner, PERF_TYPE_SOFTWARE, PERF_COUNT_SW_PAGE_FAULTS_MIN); }
319 major-faults { return sym(yyscanner, PERF_TYPE_SOFTWARE, PERF_COUNT_SW_PAGE_FAULTS_MAJ); }
320 context-switches|cs { return sym(yyscanner, PERF_TYPE_SOFTWARE, PERF_COUNT_SW_CONTEXT_SWITCHES); }
321 cpu-migrations|migrations { return sym(yyscanner, PERF_TYPE_SOFTWARE, PERF_COUNT_SW_CPU_MIGRATIONS); }
322 alignment-faults { return sym(yyscanner, PERF_TYPE_SOFTWARE, PERF_COUNT_SW_ALIGNMENT_FAULTS); }
323 emulation-faults { return sym(yyscanner, PERF_TYPE_SOFTWARE, PERF_COUNT_SW_EMULATION_FAULTS); }
324 dummy { return sym(yyscanner, PERF_TYPE_SOFTWARE, PERF_COUNT_SW_DUMMY); }
325 duration_time { return sym(yyscanner, PERF_TYPE_SOFTWARE, PERF_COUNT_SW_DUMMY); }
326 bpf-output { return sym(yyscanner, PERF_TYPE_SOFTWARE, PERF_COUNT_SW_BPF_OUTPUT); }
329 * We have to handle the kernel PMU event cycles-ct/cycles-t/mem-loads/mem-stores separately.
330 * Because the prefix cycles is mixed up with cpu-cycles.
331 * loads and stores are mixed up with cache event
333 cycles-ct { return str(yyscanner, PE_KERNEL_PMU_EVENT); }
334 cycles-t { return str(yyscanner, PE_KERNEL_PMU_EVENT); }
335 mem-loads { return str(yyscanner, PE_KERNEL_PMU_EVENT); }
336 mem-stores { return str(yyscanner, PE_KERNEL_PMU_EVENT); }
337 topdown-[a-z-]+ { return str(yyscanner, PE_KERNEL_PMU_EVENT); }
339 L1-dcache|l1-d|l1d|L1-data |
340 L1-icache|l1-i|l1i|L1-instruction |
342 dTLB|d-tlb|Data-TLB |
343 iTLB|i-tlb|Instruction-TLB |
344 branch|branches|bpu|btb|bpc |
345 node { return str(yyscanner, PE_NAME_CACHE_TYPE); }
349 prefetch|prefetches |
350 speculative-read|speculative-load |
351 refs|Reference|ops|access |
352 misses|miss { return str(yyscanner, PE_NAME_CACHE_OP_RESULT); }
354 mem: { BEGIN(mem); return PE_PREFIX_MEM; }
355 r{num_raw_hex} { return raw(yyscanner); }
356 {num_dec} { return value(yyscanner, 10); }
357 {num_hex} { return value(yyscanner, 16); }
359 {modifier_event} { return str(yyscanner, PE_MODIFIER_EVENT); }
360 {bpf_object} { if (!isbpf(yyscanner)) { USER_REJECT }; return str(yyscanner, PE_BPF_OBJECT); }
361 {bpf_source} { if (!isbpf(yyscanner)) { USER_REJECT }; return str(yyscanner, PE_BPF_SOURCE); }
362 {name} { return pmu_str_check(yyscanner); }
363 {name_tag} { return str(yyscanner, PE_NAME); }
364 "/" { BEGIN(config); return '/'; }
366 , { BEGIN(event); return ','; }
368 "{" { BEGIN(event); return '{'; }
376 int parse_events_wrap(void *scanner __maybe_unused)