1 2 %option reentrant 3 %option bison-bridge 4 %option prefix="parse_events_" 5 %option stack 6 %option bison-locations 7 %option yylineno 8 %option reject 9 10 %{ 11 #include <errno.h> 12 #include <sys/types.h> 13 #include <sys/stat.h> 14 #include <unistd.h> 15 #include "../perf.h" 16 #include "parse-events.h" 17 #include "parse-events-bison.h" 18 19 char *parse_events_get_text(yyscan_t yyscanner); 20 YYSTYPE *parse_events_get_lval(yyscan_t yyscanner); 21 22 static int __value(YYSTYPE *yylval, char *str, int base, int token) 23 { 24 u64 num; 25 26 errno = 0; 27 num = strtoull(str, NULL, base); 28 if (errno) 29 return PE_ERROR; 30 31 yylval->num = num; 32 return token; 33 } 34 35 static int value(yyscan_t scanner, int base) 36 { 37 YYSTYPE *yylval = parse_events_get_lval(scanner); 38 char *text = parse_events_get_text(scanner); 39 40 return __value(yylval, text, base, PE_VALUE); 41 } 42 43 static int raw(yyscan_t scanner) 44 { 45 YYSTYPE *yylval = parse_events_get_lval(scanner); 46 char *text = parse_events_get_text(scanner); 47 48 return __value(yylval, text + 1, 16, PE_RAW); 49 } 50 51 static int str(yyscan_t scanner, int token) 52 { 53 YYSTYPE *yylval = parse_events_get_lval(scanner); 54 char *text = parse_events_get_text(scanner); 55 56 if (text[0] != '\'') { 57 yylval->str = strdup(text); 58 } else { 59 /* 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 63 * as well, like this: 64 * name=\'CPU_CLK_UNHALTED.THREAD:cmask=1\' 65 * quotes need to be escaped to bypass shell 66 * processing. 67 */ 68 yylval->str = strndup(&text[1], strlen(text) - 2); 69 } 70 71 return token; 72 } 73 74 static bool isbpf_suffix(char *text) 75 { 76 int len = strlen(text); 77 78 if (len < 2) 79 return false; 80 if ((text[len - 1] == 'c' || text[len - 1] == 'o') && 81 text[len - 2] == '.') 82 return true; 83 if (len > 4 && !strcmp(text + len - 4, ".obj")) 84 return true; 85 return false; 86 } 87 88 static bool isbpf(yyscan_t scanner) 89 { 90 char *text = parse_events_get_text(scanner); 91 struct stat st; 92 93 if (!isbpf_suffix(text)) 94 return false; 95 96 return stat(text, &st) == 0; 97 } 98 99 /* 100 * This function is called when the parser gets two kind of input: 101 * 102 * @cfg1 or @cfg2=config 103 * 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 107 * 'config'. 108 */ 109 static int drv_str(yyscan_t scanner, int token) 110 { 111 YYSTYPE *yylval = parse_events_get_lval(scanner); 112 char *text = parse_events_get_text(scanner); 113 114 /* Strip off the '@' */ 115 yylval->str = strdup(text + 1); 116 return token; 117 } 118 119 #define REWIND(__alloc) \ 120 do { \ 121 YYSTYPE *__yylval = parse_events_get_lval(yyscanner); \ 122 char *text = parse_events_get_text(yyscanner); \ 123 \ 124 if (__alloc) \ 125 __yylval->str = strdup(text); \ 126 \ 127 yycolumn -= strlen(text); \ 128 yyless(0); \ 129 } while (0) 130 131 static int pmu_str_check(yyscan_t scanner) 132 { 133 YYSTYPE *yylval = parse_events_get_lval(scanner); 134 char *text = parse_events_get_text(scanner); 135 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; 144 default: 145 return PE_NAME; 146 } 147 } 148 149 static int sym(yyscan_t scanner, int type, int config) 150 { 151 YYSTYPE *yylval = parse_events_get_lval(scanner); 152 153 yylval->num = (type << 16) + config; 154 return type == PERF_TYPE_HARDWARE ? PE_VALUE_SYM_HW : PE_VALUE_SYM_SW; 155 } 156 157 static int term(yyscan_t scanner, int type) 158 { 159 YYSTYPE *yylval = parse_events_get_lval(scanner); 160 161 yylval->num = type; 162 return PE_TERM; 163 } 164 165 #define YY_USER_ACTION \ 166 do { \ 167 yylloc->last_column = yylloc->first_column; \ 168 yylloc->first_column = yycolumn; \ 169 yycolumn += yyleng; \ 170 } while (0); 171 172 #define USER_REJECT \ 173 yycolumn -= yyleng; \ 174 REJECT 175 176 %} 177 178 %x mem 179 %s config 180 %x event 181 %x array 182 183 group [^,{}/]*[{][^}]*[}][^,{}/]* 184 event_pmu [^,{}/]+[/][^/]*[/][^,{}/]* 185 event [^,{}/]+ 186 bpf_object [^,{}]+\.(o|bpf)[a-zA-Z0-9._]* 187 bpf_source [^,{}]+\.c[a-zA-Z0-9._]* 188 189 num_dec [0-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} 199 200 %% 201 202 %{ 203 { 204 int start_token; 205 206 start_token = parse_events_get_extra(yyscanner); 207 208 if (start_token == PE_START_TERMS) 209 BEGIN(config); 210 else if (start_token == PE_START_EVENTS) 211 BEGIN(event); 212 213 if (start_token) { 214 parse_events_set_extra(NULL, yyscanner); 215 /* 216 * The flex parser does not init locations variable 217 * via the scan_string interface, so we need do the 218 * init in here. 219 */ 220 yycolumn = 0; 221 return start_token; 222 } 223 } 224 %} 225 226 <event>{ 227 228 {group} { 229 BEGIN(INITIAL); 230 REWIND(0); 231 } 232 233 {event_pmu} | 234 {bpf_object} | 235 {bpf_source} | 236 {event} { 237 BEGIN(INITIAL); 238 REWIND(1); 239 return PE_EVENT_NAME; 240 } 241 242 <<EOF>> { 243 BEGIN(INITIAL); 244 REWIND(0); 245 } 246 247 } 248 249 <array>{ 250 "]" { BEGIN(config); return ']'; } 251 {num_dec} { return value(yyscanner, 10); } 252 {num_hex} { return value(yyscanner, 16); } 253 , { return ','; } 254 "\.\.\." { return PE_ARRAY_RANGE; } 255 } 256 257 <config>{ 258 /* 259 * Please update config_term_names when new static term is added. 260 */ 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); } 277 , { return ','; } 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); } 283 } 284 285 <mem>{ 286 {modifier_bp} { return str(yyscanner, PE_MODIFIER_BP); } 287 : { return ':'; } 288 "/" { return '/'; } 289 {num_dec} { return value(yyscanner, 10); } 290 {num_hex} { return value(yyscanner, 16); } 291 /* 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. 296 */ 297 . { unput(*yytext); BEGIN(INITIAL); } 298 /* 299 * We destroy the scanner after reaching EOF, 300 * but anyway just to be sure get back to INIT state. 301 */ 302 <<EOF>> { BEGIN(INITIAL); } 303 } 304 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); } 327 328 /* 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 332 */ 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); } 338 339 L1-dcache|l1-d|l1d|L1-data | 340 L1-icache|l1-i|l1i|L1-instruction | 341 LLC|L2 | 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); } 346 347 load|loads|read | 348 store|stores|write | 349 prefetch|prefetches | 350 speculative-read|speculative-load | 351 refs|Reference|ops|access | 352 misses|miss { return str(yyscanner, PE_NAME_CACHE_OP_RESULT); } 353 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); } 358 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 '/'; } 365 - { return '-'; } 366 , { BEGIN(event); return ','; } 367 : { return ':'; } 368 "{" { BEGIN(event); return '{'; } 369 "}" { return '}'; } 370 = { return '='; } 371 \n { } 372 . { } 373 374 %% 375 376 int parse_events_wrap(void *scanner __maybe_unused) 377 { 378 return 1; 379 } 380