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 raw(yyscan_t scanner)
46 YYSTYPE *yylval = parse_events_get_lval(scanner);
47 char *text = parse_events_get_text(scanner);
49 return __value(yylval, text + 1, 16, PE_RAW);
52 static int str(yyscan_t scanner, int token)
54 YYSTYPE *yylval = parse_events_get_lval(scanner);
55 char *text = parse_events_get_text(scanner);
57 if (text[0] != '\'') {
58 yylval->str = strdup(text);
61 * If a text tag specified on the command line
62 * contains opening single quite ' then it is
63 * expected that the tag ends with single quote
65 * name=\'CPU_CLK_UNHALTED.THREAD:cmask=1\'
66 * quotes need to be escaped to bypass shell
69 yylval->str = strndup(&text[1], strlen(text) - 2);
75 static bool isbpf_suffix(char *text)
77 int len = strlen(text);
81 if ((text[len - 1] == 'c' || text[len - 1] == 'o') &&
84 if (len > 4 && !strcmp(text + len - 4, ".obj"))
89 static bool isbpf(yyscan_t scanner)
91 char *text = parse_events_get_text(scanner);
94 if (!isbpf_suffix(text))
97 return stat(text, &st) == 0;
101 * This function is called when the parser gets two kind of input:
103 * @cfg1 or @cfg2=config
105 * The leading '@' is stripped off before 'cfg1' and 'cfg2=config' are given to
106 * bison. In the latter case it is necessary to keep the string intact so that
107 * the PMU kernel driver can determine what configurable is associated to
110 static int drv_str(yyscan_t scanner, int token)
112 YYSTYPE *yylval = parse_events_get_lval(scanner);
113 char *text = parse_events_get_text(scanner);
115 /* Strip off the '@' */
116 yylval->str = strdup(text + 1);
120 #define REWIND(__alloc) \
122 YYSTYPE *__yylval = parse_events_get_lval(yyscanner); \
123 char *text = parse_events_get_text(yyscanner); \
126 __yylval->str = strdup(text); \
128 yycolumn -= strlen(text); \
132 static int pmu_str_check(yyscan_t scanner)
134 YYSTYPE *yylval = parse_events_get_lval(scanner);
135 char *text = parse_events_get_text(scanner);
137 yylval->str = strdup(text);
138 switch (perf_pmu__parse_check(text)) {
139 case PMU_EVENT_SYMBOL_PREFIX:
140 return PE_PMU_EVENT_PRE;
141 case PMU_EVENT_SYMBOL_SUFFIX:
142 return PE_PMU_EVENT_SUF;
143 case PMU_EVENT_SYMBOL:
144 return PE_KERNEL_PMU_EVENT;
150 static int sym(yyscan_t scanner, int type, int config)
152 YYSTYPE *yylval = parse_events_get_lval(scanner);
154 yylval->num = (type << 16) + config;
155 return type == PERF_TYPE_HARDWARE ? PE_VALUE_SYM_HW : PE_VALUE_SYM_SW;
158 static int tool(yyscan_t scanner, enum perf_tool_event event)
160 YYSTYPE *yylval = parse_events_get_lval(scanner);
163 return PE_VALUE_SYM_TOOL;
166 static int term(yyscan_t scanner, int type)
168 YYSTYPE *yylval = parse_events_get_lval(scanner);
174 #define YY_USER_ACTION \
176 yylloc->last_column = yylloc->first_column; \
177 yylloc->first_column = yycolumn; \
178 yycolumn += yyleng; \
181 #define USER_REJECT \
182 yycolumn -= yyleng; \
192 group [^,{}/]*[{][^}]*[}][^,{}/]*
193 event_pmu [^,{}/]+[/][^/]*[/][^,{}/]*
195 bpf_object [^,{}]+\.(o|bpf)[a-zA-Z0-9._]*
196 bpf_source [^,{}]+\.c[a-zA-Z0-9._]*
199 num_hex 0x[a-fA-F0-9]+
200 num_raw_hex [a-fA-F0-9]+
201 name [a-zA-Z_*?\[\]][a-zA-Z0-9_*?.\[\]]*
202 name_tag [\'][a-zA-Z_*?\[\]][a-zA-Z0-9_*?\-,\.\[\]:=]*[\']
203 name_minus [a-zA-Z_*?][a-zA-Z0-9\-_*?.:]*
204 drv_cfg_term [a-zA-Z0-9_\.]+(=[a-zA-Z0-9_*?\.:]+)?
205 /* If you add a modifier you need to update check_modifier() */
206 modifier_event [ukhpPGHSDIW]+
207 modifier_bp [rwx]{1,3}
215 start_token = parse_events_get_extra(yyscanner);
217 if (start_token == PE_START_TERMS)
219 else if (start_token == PE_START_EVENTS)
223 parse_events_set_extra(NULL, yyscanner);
225 * The flex parser does not init locations variable
226 * via the scan_string interface, so we need do the
248 return PE_EVENT_NAME;
259 "]" { BEGIN(config); return ']'; }
260 {num_dec} { return value(yyscanner, 10); }
261 {num_hex} { return value(yyscanner, 16); }
263 "\.\.\." { return PE_ARRAY_RANGE; }
268 * Please update config_term_names when new static term is added.
270 config { return term(yyscanner, PARSE_EVENTS__TERM_TYPE_CONFIG); }
271 config1 { return term(yyscanner, PARSE_EVENTS__TERM_TYPE_CONFIG1); }
272 config2 { return term(yyscanner, PARSE_EVENTS__TERM_TYPE_CONFIG2); }
273 name { return term(yyscanner, PARSE_EVENTS__TERM_TYPE_NAME); }
274 period { return term(yyscanner, PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD); }
275 freq { return term(yyscanner, PARSE_EVENTS__TERM_TYPE_SAMPLE_FREQ); }
276 branch_type { return term(yyscanner, PARSE_EVENTS__TERM_TYPE_BRANCH_SAMPLE_TYPE); }
277 time { return term(yyscanner, PARSE_EVENTS__TERM_TYPE_TIME); }
278 call-graph { return term(yyscanner, PARSE_EVENTS__TERM_TYPE_CALLGRAPH); }
279 stack-size { return term(yyscanner, PARSE_EVENTS__TERM_TYPE_STACKSIZE); }
280 max-stack { return term(yyscanner, PARSE_EVENTS__TERM_TYPE_MAX_STACK); }
281 nr { return term(yyscanner, PARSE_EVENTS__TERM_TYPE_MAX_EVENTS); }
282 inherit { return term(yyscanner, PARSE_EVENTS__TERM_TYPE_INHERIT); }
283 no-inherit { return term(yyscanner, PARSE_EVENTS__TERM_TYPE_NOINHERIT); }
284 overwrite { return term(yyscanner, PARSE_EVENTS__TERM_TYPE_OVERWRITE); }
285 no-overwrite { return term(yyscanner, PARSE_EVENTS__TERM_TYPE_NOOVERWRITE); }
286 percore { return term(yyscanner, PARSE_EVENTS__TERM_TYPE_PERCORE); }
287 aux-output { return term(yyscanner, PARSE_EVENTS__TERM_TYPE_AUX_OUTPUT); }
288 aux-sample-size { return term(yyscanner, PARSE_EVENTS__TERM_TYPE_AUX_SAMPLE_SIZE); }
290 "/" { BEGIN(INITIAL); return '/'; }
291 {name_minus} { return str(yyscanner, PE_NAME); }
292 \[all\] { return PE_ARRAY_ALL; }
293 "[" { BEGIN(array); return '['; }
294 @{drv_cfg_term} { return drv_str(yyscanner, PE_DRV_CFG_TERM); }
298 {modifier_bp} { return str(yyscanner, PE_MODIFIER_BP); }
301 {num_dec} { return value(yyscanner, 10); }
302 {num_hex} { return value(yyscanner, 16); }
304 * We need to separate 'mem:' scanner part, in order to get specific
305 * modifier bits parsed out. Otherwise we would need to handle PE_NAME
306 * and we'd need to parse it manually. During the escape from <mem>
307 * state we need to put the escaping char back, so we dont miss it.
309 . { unput(*yytext); BEGIN(INITIAL); }
311 * We destroy the scanner after reaching EOF,
312 * but anyway just to be sure get back to INIT state.
314 <<EOF>> { BEGIN(INITIAL); }
317 cpu-cycles|cycles { return sym(yyscanner, PERF_TYPE_HARDWARE, PERF_COUNT_HW_CPU_CYCLES); }
318 stalled-cycles-frontend|idle-cycles-frontend { return sym(yyscanner, PERF_TYPE_HARDWARE, PERF_COUNT_HW_STALLED_CYCLES_FRONTEND); }
319 stalled-cycles-backend|idle-cycles-backend { return sym(yyscanner, PERF_TYPE_HARDWARE, PERF_COUNT_HW_STALLED_CYCLES_BACKEND); }
320 instructions { return sym(yyscanner, PERF_TYPE_HARDWARE, PERF_COUNT_HW_INSTRUCTIONS); }
321 cache-references { return sym(yyscanner, PERF_TYPE_HARDWARE, PERF_COUNT_HW_CACHE_REFERENCES); }
322 cache-misses { return sym(yyscanner, PERF_TYPE_HARDWARE, PERF_COUNT_HW_CACHE_MISSES); }
323 branch-instructions|branches { return sym(yyscanner, PERF_TYPE_HARDWARE, PERF_COUNT_HW_BRANCH_INSTRUCTIONS); }
324 branch-misses { return sym(yyscanner, PERF_TYPE_HARDWARE, PERF_COUNT_HW_BRANCH_MISSES); }
325 bus-cycles { return sym(yyscanner, PERF_TYPE_HARDWARE, PERF_COUNT_HW_BUS_CYCLES); }
326 ref-cycles { return sym(yyscanner, PERF_TYPE_HARDWARE, PERF_COUNT_HW_REF_CPU_CYCLES); }
327 cpu-clock { return sym(yyscanner, PERF_TYPE_SOFTWARE, PERF_COUNT_SW_CPU_CLOCK); }
328 task-clock { return sym(yyscanner, PERF_TYPE_SOFTWARE, PERF_COUNT_SW_TASK_CLOCK); }
329 page-faults|faults { return sym(yyscanner, PERF_TYPE_SOFTWARE, PERF_COUNT_SW_PAGE_FAULTS); }
330 minor-faults { return sym(yyscanner, PERF_TYPE_SOFTWARE, PERF_COUNT_SW_PAGE_FAULTS_MIN); }
331 major-faults { return sym(yyscanner, PERF_TYPE_SOFTWARE, PERF_COUNT_SW_PAGE_FAULTS_MAJ); }
332 context-switches|cs { return sym(yyscanner, PERF_TYPE_SOFTWARE, PERF_COUNT_SW_CONTEXT_SWITCHES); }
333 cpu-migrations|migrations { return sym(yyscanner, PERF_TYPE_SOFTWARE, PERF_COUNT_SW_CPU_MIGRATIONS); }
334 alignment-faults { return sym(yyscanner, PERF_TYPE_SOFTWARE, PERF_COUNT_SW_ALIGNMENT_FAULTS); }
335 emulation-faults { return sym(yyscanner, PERF_TYPE_SOFTWARE, PERF_COUNT_SW_EMULATION_FAULTS); }
336 dummy { return sym(yyscanner, PERF_TYPE_SOFTWARE, PERF_COUNT_SW_DUMMY); }
337 duration_time { return tool(yyscanner, PERF_TOOL_DURATION_TIME); }
338 bpf-output { return sym(yyscanner, PERF_TYPE_SOFTWARE, PERF_COUNT_SW_BPF_OUTPUT); }
341 * We have to handle the kernel PMU event cycles-ct/cycles-t/mem-loads/mem-stores separately.
342 * Because the prefix cycles is mixed up with cpu-cycles.
343 * loads and stores are mixed up with cache event
345 cycles-ct { return str(yyscanner, PE_KERNEL_PMU_EVENT); }
346 cycles-t { return str(yyscanner, PE_KERNEL_PMU_EVENT); }
347 mem-loads { return str(yyscanner, PE_KERNEL_PMU_EVENT); }
348 mem-stores { return str(yyscanner, PE_KERNEL_PMU_EVENT); }
349 topdown-[a-z-]+ { return str(yyscanner, PE_KERNEL_PMU_EVENT); }
351 L1-dcache|l1-d|l1d|L1-data |
352 L1-icache|l1-i|l1i|L1-instruction |
354 dTLB|d-tlb|Data-TLB |
355 iTLB|i-tlb|Instruction-TLB |
356 branch|branches|bpu|btb|bpc |
357 node { return str(yyscanner, PE_NAME_CACHE_TYPE); }
361 prefetch|prefetches |
362 speculative-read|speculative-load |
363 refs|Reference|ops|access |
364 misses|miss { return str(yyscanner, PE_NAME_CACHE_OP_RESULT); }
366 mem: { BEGIN(mem); return PE_PREFIX_MEM; }
367 r{num_raw_hex} { return raw(yyscanner); }
368 {num_dec} { return value(yyscanner, 10); }
369 {num_hex} { return value(yyscanner, 16); }
371 {modifier_event} { return str(yyscanner, PE_MODIFIER_EVENT); }
372 {bpf_object} { if (!isbpf(yyscanner)) { USER_REJECT }; return str(yyscanner, PE_BPF_OBJECT); }
373 {bpf_source} { if (!isbpf(yyscanner)) { USER_REJECT }; return str(yyscanner, PE_BPF_SOURCE); }
374 {name} { return pmu_str_check(yyscanner); }
375 {name_tag} { return str(yyscanner, PE_NAME); }
376 "/" { BEGIN(config); return '/'; }
378 , { BEGIN(event); return ','; }
380 "{" { BEGIN(event); return '{'; }
388 int parse_events_wrap(void *scanner __maybe_unused)