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 #include "evsel.h" 19 20 char *parse_events_get_text(yyscan_t yyscanner); 21 YYSTYPE *parse_events_get_lval(yyscan_t yyscanner); 22 23 static int __value(YYSTYPE *yylval, char *str, int base, int token) 24 { 25 u64 num; 26 27 errno = 0; 28 num = strtoull(str, NULL, base); 29 if (errno) 30 return PE_ERROR; 31 32 yylval->num = num; 33 return token; 34 } 35 36 static int value(yyscan_t scanner, int base) 37 { 38 YYSTYPE *yylval = parse_events_get_lval(scanner); 39 char *text = parse_events_get_text(scanner); 40 41 return __value(yylval, text, base, PE_VALUE); 42 } 43 44 static int raw(yyscan_t scanner) 45 { 46 YYSTYPE *yylval = parse_events_get_lval(scanner); 47 char *text = parse_events_get_text(scanner); 48 49 return __value(yylval, text + 1, 16, PE_RAW); 50 } 51 52 static int str(yyscan_t scanner, int token) 53 { 54 YYSTYPE *yylval = parse_events_get_lval(scanner); 55 char *text = parse_events_get_text(scanner); 56 57 if (text[0] != '\'') { 58 yylval->str = strdup(text); 59 } else { 60 /* 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 64 * as well, like this: 65 * name=\'CPU_CLK_UNHALTED.THREAD:cmask=1\' 66 * quotes need to be escaped to bypass shell 67 * processing. 68 */ 69 yylval->str = strndup(&text[1], strlen(text) - 2); 70 } 71 72 return token; 73 } 74 75 static bool isbpf_suffix(char *text) 76 { 77 int len = strlen(text); 78 79 if (len < 2) 80 return false; 81 if ((text[len - 1] == 'c' || text[len - 1] == 'o') && 82 text[len - 2] == '.') 83 return true; 84 if (len > 4 && !strcmp(text + len - 4, ".obj")) 85 return true; 86 return false; 87 } 88 89 static bool isbpf(yyscan_t scanner) 90 { 91 char *text = parse_events_get_text(scanner); 92 struct stat st; 93 94 if (!isbpf_suffix(text)) 95 return false; 96 97 return stat(text, &st) == 0; 98 } 99 100 /* 101 * This function is called when the parser gets two kind of input: 102 * 103 * @cfg1 or @cfg2=config 104 * 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 108 * 'config'. 109 */ 110 static int drv_str(yyscan_t scanner, int token) 111 { 112 YYSTYPE *yylval = parse_events_get_lval(scanner); 113 char *text = parse_events_get_text(scanner); 114 115 /* Strip off the '@' */ 116 yylval->str = strdup(text + 1); 117 return token; 118 } 119 120 #define REWIND(__alloc) \ 121 do { \ 122 YYSTYPE *__yylval = parse_events_get_lval(yyscanner); \ 123 char *text = parse_events_get_text(yyscanner); \ 124 \ 125 if (__alloc) \ 126 __yylval->str = strdup(text); \ 127 \ 128 yycolumn -= strlen(text); \ 129 yyless(0); \ 130 } while (0) 131 132 static int pmu_str_check(yyscan_t scanner) 133 { 134 YYSTYPE *yylval = parse_events_get_lval(scanner); 135 char *text = parse_events_get_text(scanner); 136 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; 145 default: 146 return PE_NAME; 147 } 148 } 149 150 static int sym(yyscan_t scanner, int type, int config) 151 { 152 YYSTYPE *yylval = parse_events_get_lval(scanner); 153 154 yylval->num = (type << 16) + config; 155 return type == PERF_TYPE_HARDWARE ? PE_VALUE_SYM_HW : PE_VALUE_SYM_SW; 156 } 157 158 static int tool(yyscan_t scanner, enum perf_tool_event event) 159 { 160 YYSTYPE *yylval = parse_events_get_lval(scanner); 161 162 yylval->num = event; 163 return PE_VALUE_SYM_TOOL; 164 } 165 166 static int term(yyscan_t scanner, int type) 167 { 168 YYSTYPE *yylval = parse_events_get_lval(scanner); 169 170 yylval->num = type; 171 return PE_TERM; 172 } 173 174 #define YY_USER_ACTION \ 175 do { \ 176 yylloc->last_column = yylloc->first_column; \ 177 yylloc->first_column = yycolumn; \ 178 yycolumn += yyleng; \ 179 } while (0); 180 181 #define USER_REJECT \ 182 yycolumn -= yyleng; \ 183 REJECT 184 185 %} 186 187 %x mem 188 %s config 189 %x event 190 %x array 191 192 group [^,{}/]*[{][^}]*[}][^,{}/]* 193 event_pmu [^,{}/]+[/][^/]*[/][^,{}/]* 194 event [^,{}/]+ 195 bpf_object [^,{}]+\.(o|bpf)[a-zA-Z0-9._]* 196 bpf_source [^,{}]+\.c[a-zA-Z0-9._]* 197 198 num_dec [0-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} 208 209 %% 210 211 %{ 212 { 213 int start_token; 214 215 start_token = parse_events_get_extra(yyscanner); 216 217 if (start_token == PE_START_TERMS) 218 BEGIN(config); 219 else if (start_token == PE_START_EVENTS) 220 BEGIN(event); 221 222 if (start_token) { 223 parse_events_set_extra(NULL, yyscanner); 224 /* 225 * The flex parser does not init locations variable 226 * via the scan_string interface, so we need do the 227 * init in here. 228 */ 229 yycolumn = 0; 230 return start_token; 231 } 232 } 233 %} 234 235 <event>{ 236 237 {group} { 238 BEGIN(INITIAL); 239 REWIND(0); 240 } 241 242 {event_pmu} | 243 {bpf_object} | 244 {bpf_source} | 245 {event} { 246 BEGIN(INITIAL); 247 REWIND(1); 248 return PE_EVENT_NAME; 249 } 250 251 <<EOF>> { 252 BEGIN(INITIAL); 253 REWIND(0); 254 } 255 256 } 257 258 <array>{ 259 "]" { BEGIN(config); return ']'; } 260 {num_dec} { return value(yyscanner, 10); } 261 {num_hex} { return value(yyscanner, 16); } 262 , { return ','; } 263 "\.\.\." { return PE_ARRAY_RANGE; } 264 } 265 266 <config>{ 267 /* 268 * Please update config_term_names when new static term is added. 269 */ 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); } 289 , { return ','; } 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); } 295 } 296 297 <mem>{ 298 {modifier_bp} { return str(yyscanner, PE_MODIFIER_BP); } 299 : { return ':'; } 300 "/" { return '/'; } 301 {num_dec} { return value(yyscanner, 10); } 302 {num_hex} { return value(yyscanner, 16); } 303 /* 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. 308 */ 309 . { unput(*yytext); BEGIN(INITIAL); } 310 /* 311 * We destroy the scanner after reaching EOF, 312 * but anyway just to be sure get back to INIT state. 313 */ 314 <<EOF>> { BEGIN(INITIAL); } 315 } 316 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); } 339 340 /* 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 344 */ 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); } 350 351 L1-dcache|l1-d|l1d|L1-data | 352 L1-icache|l1-i|l1i|L1-instruction | 353 LLC|L2 | 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); } 358 359 load|loads|read | 360 store|stores|write | 361 prefetch|prefetches | 362 speculative-read|speculative-load | 363 refs|Reference|ops|access | 364 misses|miss { return str(yyscanner, PE_NAME_CACHE_OP_RESULT); } 365 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); } 370 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 '/'; } 377 - { return '-'; } 378 , { BEGIN(event); return ','; } 379 : { return ':'; } 380 "{" { BEGIN(event); return '{'; } 381 "}" { return '}'; } 382 = { return '='; } 383 \n { } 384 . { } 385 386 %% 387 388 int parse_events_wrap(void *scanner __maybe_unused) 389 { 390 return 1; 391 } 392