1 2 %option reentrant 3 %option bison-bridge 4 %option prefix="parse_events_" 5 %option stack 6 %option bison-locations 7 %option yylineno 8 9 %{ 10 #include <errno.h> 11 #include "../perf.h" 12 #include "parse-events.h" 13 #include "parse-events-bison.h" 14 15 char *parse_events_get_text(yyscan_t yyscanner); 16 YYSTYPE *parse_events_get_lval(yyscan_t yyscanner); 17 18 static int __value(YYSTYPE *yylval, char *str, int base, int token) 19 { 20 u64 num; 21 22 errno = 0; 23 num = strtoull(str, NULL, base); 24 if (errno) 25 return PE_ERROR; 26 27 yylval->num = num; 28 return token; 29 } 30 31 static int value(yyscan_t scanner, int base) 32 { 33 YYSTYPE *yylval = parse_events_get_lval(scanner); 34 char *text = parse_events_get_text(scanner); 35 36 return __value(yylval, text, base, PE_VALUE); 37 } 38 39 static int raw(yyscan_t scanner) 40 { 41 YYSTYPE *yylval = parse_events_get_lval(scanner); 42 char *text = parse_events_get_text(scanner); 43 44 return __value(yylval, text + 1, 16, PE_RAW); 45 } 46 47 static int str(yyscan_t scanner, int token) 48 { 49 YYSTYPE *yylval = parse_events_get_lval(scanner); 50 char *text = parse_events_get_text(scanner); 51 52 yylval->str = strdup(text); 53 return token; 54 } 55 56 #define REWIND(__alloc) \ 57 do { \ 58 YYSTYPE *__yylval = parse_events_get_lval(yyscanner); \ 59 char *text = parse_events_get_text(yyscanner); \ 60 \ 61 if (__alloc) \ 62 __yylval->str = strdup(text); \ 63 \ 64 yycolumn -= strlen(text); \ 65 yyless(0); \ 66 } while (0) 67 68 static int pmu_str_check(yyscan_t scanner) 69 { 70 YYSTYPE *yylval = parse_events_get_lval(scanner); 71 char *text = parse_events_get_text(scanner); 72 73 yylval->str = strdup(text); 74 switch (perf_pmu__parse_check(text)) { 75 case PMU_EVENT_SYMBOL_PREFIX: 76 return PE_PMU_EVENT_PRE; 77 case PMU_EVENT_SYMBOL_SUFFIX: 78 return PE_PMU_EVENT_SUF; 79 case PMU_EVENT_SYMBOL: 80 return PE_KERNEL_PMU_EVENT; 81 default: 82 return PE_NAME; 83 } 84 } 85 86 static int sym(yyscan_t scanner, int type, int config) 87 { 88 YYSTYPE *yylval = parse_events_get_lval(scanner); 89 90 yylval->num = (type << 16) + config; 91 return type == PERF_TYPE_HARDWARE ? PE_VALUE_SYM_HW : PE_VALUE_SYM_SW; 92 } 93 94 static int term(yyscan_t scanner, int type) 95 { 96 YYSTYPE *yylval = parse_events_get_lval(scanner); 97 98 yylval->num = type; 99 return PE_TERM; 100 } 101 102 #define YY_USER_ACTION \ 103 do { \ 104 yylloc->last_column = yylloc->first_column; \ 105 yylloc->first_column = yycolumn; \ 106 yycolumn += yyleng; \ 107 } while (0); 108 109 %} 110 111 %x mem 112 %s config 113 %x event 114 %x array 115 116 group [^,{}/]*[{][^}]*[}][^,{}/]* 117 event_pmu [^,{}/]+[/][^/]*[/][^,{}/]* 118 event [^,{}/]+ 119 bpf_object .*\.(o|bpf) 120 bpf_source .*\.c 121 122 num_dec [0-9]+ 123 num_hex 0x[a-fA-F0-9]+ 124 num_raw_hex [a-fA-F0-9]+ 125 name [a-zA-Z_*?][a-zA-Z0-9_*?.]* 126 name_minus [a-zA-Z_*?][a-zA-Z0-9\-_*?.:]* 127 /* If you add a modifier you need to update check_modifier() */ 128 modifier_event [ukhpPGHSDI]+ 129 modifier_bp [rwx]{1,3} 130 131 %% 132 133 %{ 134 { 135 int start_token; 136 137 start_token = parse_events_get_extra(yyscanner); 138 139 if (start_token == PE_START_TERMS) 140 BEGIN(config); 141 else if (start_token == PE_START_EVENTS) 142 BEGIN(event); 143 144 if (start_token) { 145 parse_events_set_extra(NULL, yyscanner); 146 /* 147 * The flex parser does not init locations variable 148 * via the scan_string interface, so we need do the 149 * init in here. 150 */ 151 yycolumn = 0; 152 return start_token; 153 } 154 } 155 %} 156 157 <event>{ 158 159 {group} { 160 BEGIN(INITIAL); 161 REWIND(0); 162 } 163 164 {event_pmu} | 165 {bpf_object} | 166 {bpf_source} | 167 {event} { 168 BEGIN(INITIAL); 169 REWIND(1); 170 return PE_EVENT_NAME; 171 } 172 173 <<EOF>> { 174 BEGIN(INITIAL); 175 REWIND(0); 176 } 177 178 } 179 180 <array>{ 181 "]" { BEGIN(config); return ']'; } 182 {num_dec} { return value(yyscanner, 10); } 183 {num_hex} { return value(yyscanner, 16); } 184 , { return ','; } 185 "\.\.\." { return PE_ARRAY_RANGE; } 186 } 187 188 <config>{ 189 /* 190 * Please update config_term_names when new static term is added. 191 */ 192 config { return term(yyscanner, PARSE_EVENTS__TERM_TYPE_CONFIG); } 193 config1 { return term(yyscanner, PARSE_EVENTS__TERM_TYPE_CONFIG1); } 194 config2 { return term(yyscanner, PARSE_EVENTS__TERM_TYPE_CONFIG2); } 195 name { return term(yyscanner, PARSE_EVENTS__TERM_TYPE_NAME); } 196 period { return term(yyscanner, PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD); } 197 freq { return term(yyscanner, PARSE_EVENTS__TERM_TYPE_SAMPLE_FREQ); } 198 branch_type { return term(yyscanner, PARSE_EVENTS__TERM_TYPE_BRANCH_SAMPLE_TYPE); } 199 time { return term(yyscanner, PARSE_EVENTS__TERM_TYPE_TIME); } 200 call-graph { return term(yyscanner, PARSE_EVENTS__TERM_TYPE_CALLGRAPH); } 201 stack-size { return term(yyscanner, PARSE_EVENTS__TERM_TYPE_STACKSIZE); } 202 inherit { return term(yyscanner, PARSE_EVENTS__TERM_TYPE_INHERIT); } 203 no-inherit { return term(yyscanner, PARSE_EVENTS__TERM_TYPE_NOINHERIT); } 204 , { return ','; } 205 "/" { BEGIN(INITIAL); return '/'; } 206 {name_minus} { return str(yyscanner, PE_NAME); } 207 \[all\] { return PE_ARRAY_ALL; } 208 "[" { BEGIN(array); return '['; } 209 } 210 211 <mem>{ 212 {modifier_bp} { return str(yyscanner, PE_MODIFIER_BP); } 213 : { return ':'; } 214 "/" { return '/'; } 215 {num_dec} { return value(yyscanner, 10); } 216 {num_hex} { return value(yyscanner, 16); } 217 /* 218 * We need to separate 'mem:' scanner part, in order to get specific 219 * modifier bits parsed out. Otherwise we would need to handle PE_NAME 220 * and we'd need to parse it manually. During the escape from <mem> 221 * state we need to put the escaping char back, so we dont miss it. 222 */ 223 . { unput(*yytext); BEGIN(INITIAL); } 224 /* 225 * We destroy the scanner after reaching EOF, 226 * but anyway just to be sure get back to INIT state. 227 */ 228 <<EOF>> { BEGIN(INITIAL); } 229 } 230 231 cpu-cycles|cycles { return sym(yyscanner, PERF_TYPE_HARDWARE, PERF_COUNT_HW_CPU_CYCLES); } 232 stalled-cycles-frontend|idle-cycles-frontend { return sym(yyscanner, PERF_TYPE_HARDWARE, PERF_COUNT_HW_STALLED_CYCLES_FRONTEND); } 233 stalled-cycles-backend|idle-cycles-backend { return sym(yyscanner, PERF_TYPE_HARDWARE, PERF_COUNT_HW_STALLED_CYCLES_BACKEND); } 234 instructions { return sym(yyscanner, PERF_TYPE_HARDWARE, PERF_COUNT_HW_INSTRUCTIONS); } 235 cache-references { return sym(yyscanner, PERF_TYPE_HARDWARE, PERF_COUNT_HW_CACHE_REFERENCES); } 236 cache-misses { return sym(yyscanner, PERF_TYPE_HARDWARE, PERF_COUNT_HW_CACHE_MISSES); } 237 branch-instructions|branches { return sym(yyscanner, PERF_TYPE_HARDWARE, PERF_COUNT_HW_BRANCH_INSTRUCTIONS); } 238 branch-misses { return sym(yyscanner, PERF_TYPE_HARDWARE, PERF_COUNT_HW_BRANCH_MISSES); } 239 bus-cycles { return sym(yyscanner, PERF_TYPE_HARDWARE, PERF_COUNT_HW_BUS_CYCLES); } 240 ref-cycles { return sym(yyscanner, PERF_TYPE_HARDWARE, PERF_COUNT_HW_REF_CPU_CYCLES); } 241 cpu-clock { return sym(yyscanner, PERF_TYPE_SOFTWARE, PERF_COUNT_SW_CPU_CLOCK); } 242 task-clock { return sym(yyscanner, PERF_TYPE_SOFTWARE, PERF_COUNT_SW_TASK_CLOCK); } 243 page-faults|faults { return sym(yyscanner, PERF_TYPE_SOFTWARE, PERF_COUNT_SW_PAGE_FAULTS); } 244 minor-faults { return sym(yyscanner, PERF_TYPE_SOFTWARE, PERF_COUNT_SW_PAGE_FAULTS_MIN); } 245 major-faults { return sym(yyscanner, PERF_TYPE_SOFTWARE, PERF_COUNT_SW_PAGE_FAULTS_MAJ); } 246 context-switches|cs { return sym(yyscanner, PERF_TYPE_SOFTWARE, PERF_COUNT_SW_CONTEXT_SWITCHES); } 247 cpu-migrations|migrations { return sym(yyscanner, PERF_TYPE_SOFTWARE, PERF_COUNT_SW_CPU_MIGRATIONS); } 248 alignment-faults { return sym(yyscanner, PERF_TYPE_SOFTWARE, PERF_COUNT_SW_ALIGNMENT_FAULTS); } 249 emulation-faults { return sym(yyscanner, PERF_TYPE_SOFTWARE, PERF_COUNT_SW_EMULATION_FAULTS); } 250 dummy { return sym(yyscanner, PERF_TYPE_SOFTWARE, PERF_COUNT_SW_DUMMY); } 251 bpf-output { return sym(yyscanner, PERF_TYPE_SOFTWARE, PERF_COUNT_SW_BPF_OUTPUT); } 252 253 /* 254 * We have to handle the kernel PMU event cycles-ct/cycles-t/mem-loads/mem-stores separately. 255 * Because the prefix cycles is mixed up with cpu-cycles. 256 * loads and stores are mixed up with cache event 257 */ 258 cycles-ct { return str(yyscanner, PE_KERNEL_PMU_EVENT); } 259 cycles-t { return str(yyscanner, PE_KERNEL_PMU_EVENT); } 260 mem-loads { return str(yyscanner, PE_KERNEL_PMU_EVENT); } 261 mem-stores { return str(yyscanner, PE_KERNEL_PMU_EVENT); } 262 263 L1-dcache|l1-d|l1d|L1-data | 264 L1-icache|l1-i|l1i|L1-instruction | 265 LLC|L2 | 266 dTLB|d-tlb|Data-TLB | 267 iTLB|i-tlb|Instruction-TLB | 268 branch|branches|bpu|btb|bpc | 269 node { return str(yyscanner, PE_NAME_CACHE_TYPE); } 270 271 load|loads|read | 272 store|stores|write | 273 prefetch|prefetches | 274 speculative-read|speculative-load | 275 refs|Reference|ops|access | 276 misses|miss { return str(yyscanner, PE_NAME_CACHE_OP_RESULT); } 277 278 mem: { BEGIN(mem); return PE_PREFIX_MEM; } 279 r{num_raw_hex} { return raw(yyscanner); } 280 {num_dec} { return value(yyscanner, 10); } 281 {num_hex} { return value(yyscanner, 16); } 282 283 {modifier_event} { return str(yyscanner, PE_MODIFIER_EVENT); } 284 {bpf_object} { return str(yyscanner, PE_BPF_OBJECT); } 285 {bpf_source} { return str(yyscanner, PE_BPF_SOURCE); } 286 {name} { return pmu_str_check(yyscanner); } 287 "/" { BEGIN(config); return '/'; } 288 - { return '-'; } 289 , { BEGIN(event); return ','; } 290 : { return ':'; } 291 "{" { BEGIN(event); return '{'; } 292 "}" { return '}'; } 293 = { return '='; } 294 \n { } 295 . { } 296 297 %% 298 299 int parse_events_wrap(void *scanner __maybe_unused) 300 { 301 return 1; 302 } 303