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