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 inherit { return term(yyscanner, PARSE_EVENTS__TERM_TYPE_INHERIT); }
273 no-inherit { return term(yyscanner, PARSE_EVENTS__TERM_TYPE_NOINHERIT); }
274 overwrite { return term(yyscanner, PARSE_EVENTS__TERM_TYPE_OVERWRITE); }
275 no-overwrite { return term(yyscanner, PARSE_EVENTS__TERM_TYPE_NOOVERWRITE); }
277 "/" { BEGIN(INITIAL); return '/'; }
278 {name_minus} { return str(yyscanner, PE_NAME); }
279 \[all\] { return PE_ARRAY_ALL; }
280 "[" { BEGIN(array); return '['; }
281 @{drv_cfg_term} { return drv_str(yyscanner, PE_DRV_CFG_TERM); }
285 {modifier_bp} { return str(yyscanner, PE_MODIFIER_BP); }
288 {num_dec} { return value(yyscanner, 10); }
289 {num_hex} { return value(yyscanner, 16); }
291 * We need to separate 'mem:' scanner part, in order to get specific
292 * modifier bits parsed out. Otherwise we would need to handle PE_NAME
293 * and we'd need to parse it manually. During the escape from <mem>
294 * state we need to put the escaping char back, so we dont miss it.
296 . { unput(*yytext); BEGIN(INITIAL); }
298 * We destroy the scanner after reaching EOF,
299 * but anyway just to be sure get back to INIT state.
301 <<EOF>> { BEGIN(INITIAL); }
304 cpu-cycles|cycles { return sym(yyscanner, PERF_TYPE_HARDWARE, PERF_COUNT_HW_CPU_CYCLES); }
305 stalled-cycles-frontend|idle-cycles-frontend { return sym(yyscanner, PERF_TYPE_HARDWARE, PERF_COUNT_HW_STALLED_CYCLES_FRONTEND); }
306 stalled-cycles-backend|idle-cycles-backend { return sym(yyscanner, PERF_TYPE_HARDWARE, PERF_COUNT_HW_STALLED_CYCLES_BACKEND); }
307 instructions { return sym(yyscanner, PERF_TYPE_HARDWARE, PERF_COUNT_HW_INSTRUCTIONS); }
308 cache-references { return sym(yyscanner, PERF_TYPE_HARDWARE, PERF_COUNT_HW_CACHE_REFERENCES); }
309 cache-misses { return sym(yyscanner, PERF_TYPE_HARDWARE, PERF_COUNT_HW_CACHE_MISSES); }
310 branch-instructions|branches { return sym(yyscanner, PERF_TYPE_HARDWARE, PERF_COUNT_HW_BRANCH_INSTRUCTIONS); }
311 branch-misses { return sym(yyscanner, PERF_TYPE_HARDWARE, PERF_COUNT_HW_BRANCH_MISSES); }
312 bus-cycles { return sym(yyscanner, PERF_TYPE_HARDWARE, PERF_COUNT_HW_BUS_CYCLES); }
313 ref-cycles { return sym(yyscanner, PERF_TYPE_HARDWARE, PERF_COUNT_HW_REF_CPU_CYCLES); }
314 cpu-clock { return sym(yyscanner, PERF_TYPE_SOFTWARE, PERF_COUNT_SW_CPU_CLOCK); }
315 task-clock { return sym(yyscanner, PERF_TYPE_SOFTWARE, PERF_COUNT_SW_TASK_CLOCK); }
316 page-faults|faults { return sym(yyscanner, PERF_TYPE_SOFTWARE, PERF_COUNT_SW_PAGE_FAULTS); }
317 minor-faults { return sym(yyscanner, PERF_TYPE_SOFTWARE, PERF_COUNT_SW_PAGE_FAULTS_MIN); }
318 major-faults { return sym(yyscanner, PERF_TYPE_SOFTWARE, PERF_COUNT_SW_PAGE_FAULTS_MAJ); }
319 context-switches|cs { return sym(yyscanner, PERF_TYPE_SOFTWARE, PERF_COUNT_SW_CONTEXT_SWITCHES); }
320 cpu-migrations|migrations { return sym(yyscanner, PERF_TYPE_SOFTWARE, PERF_COUNT_SW_CPU_MIGRATIONS); }
321 alignment-faults { return sym(yyscanner, PERF_TYPE_SOFTWARE, PERF_COUNT_SW_ALIGNMENT_FAULTS); }
322 emulation-faults { return sym(yyscanner, PERF_TYPE_SOFTWARE, PERF_COUNT_SW_EMULATION_FAULTS); }
323 dummy { return sym(yyscanner, PERF_TYPE_SOFTWARE, PERF_COUNT_SW_DUMMY); }
324 duration_time { return sym(yyscanner, PERF_TYPE_SOFTWARE, PERF_COUNT_SW_DUMMY); }
325 bpf-output { return sym(yyscanner, PERF_TYPE_SOFTWARE, PERF_COUNT_SW_BPF_OUTPUT); }
328 * We have to handle the kernel PMU event cycles-ct/cycles-t/mem-loads/mem-stores separately.
329 * Because the prefix cycles is mixed up with cpu-cycles.
330 * loads and stores are mixed up with cache event
332 cycles-ct { return str(yyscanner, PE_KERNEL_PMU_EVENT); }
333 cycles-t { return str(yyscanner, PE_KERNEL_PMU_EVENT); }
334 mem-loads { return str(yyscanner, PE_KERNEL_PMU_EVENT); }
335 mem-stores { return str(yyscanner, PE_KERNEL_PMU_EVENT); }
336 topdown-[a-z-]+ { return str(yyscanner, PE_KERNEL_PMU_EVENT); }
338 L1-dcache|l1-d|l1d|L1-data |
339 L1-icache|l1-i|l1i|L1-instruction |
341 dTLB|d-tlb|Data-TLB |
342 iTLB|i-tlb|Instruction-TLB |
343 branch|branches|bpu|btb|bpc |
344 node { return str(yyscanner, PE_NAME_CACHE_TYPE); }
348 prefetch|prefetches |
349 speculative-read|speculative-load |
350 refs|Reference|ops|access |
351 misses|miss { return str(yyscanner, PE_NAME_CACHE_OP_RESULT); }
353 mem: { BEGIN(mem); return PE_PREFIX_MEM; }
354 r{num_raw_hex} { return raw(yyscanner); }
355 {num_dec} { return value(yyscanner, 10); }
356 {num_hex} { return value(yyscanner, 16); }
358 {modifier_event} { return str(yyscanner, PE_MODIFIER_EVENT); }
359 {bpf_object} { if (!isbpf(yyscanner)) { USER_REJECT }; return str(yyscanner, PE_BPF_OBJECT); }
360 {bpf_source} { if (!isbpf(yyscanner)) { USER_REJECT }; return str(yyscanner, PE_BPF_SOURCE); }
361 {name} { return pmu_str_check(yyscanner); }
362 {name_tag} { return str(yyscanner, PE_NAME); }
363 "/" { BEGIN(config); return '/'; }
365 , { BEGIN(event); return ','; }
367 "{" { BEGIN(event); return '{'; }
375 int parse_events_wrap(void *scanner __maybe_unused)