1 %pure-parser 2 %parse-param {void *_parse_state} 3 %parse-param {void *scanner} 4 %lex-param {void* scanner} 5 %locations 6 7 %{ 8 9 #define YYDEBUG 1 10 11 #include <fnmatch.h> 12 #include <linux/compiler.h> 13 #include <linux/list.h> 14 #include <linux/types.h> 15 #include "util.h" 16 #include "pmu.h" 17 #include "evsel.h" 18 #include "debug.h" 19 #include "parse-events.h" 20 #include "parse-events-bison.h" 21 22 void parse_events_error(YYLTYPE *loc, void *parse_state, void *scanner, char const *msg); 23 24 #define ABORT_ON(val) \ 25 do { \ 26 if (val) \ 27 YYABORT; \ 28 } while (0) 29 30 #define ALLOC_LIST(list) \ 31 do { \ 32 list = malloc(sizeof(*list)); \ 33 ABORT_ON(!list); \ 34 INIT_LIST_HEAD(list); \ 35 } while (0) 36 37 static void inc_group_count(struct list_head *list, 38 struct parse_events_state *parse_state) 39 { 40 /* Count groups only have more than 1 members */ 41 if (!list_is_last(list->next, list)) 42 parse_state->nr_groups++; 43 } 44 45 %} 46 47 %token PE_START_EVENTS PE_START_TERMS 48 %token PE_VALUE PE_VALUE_SYM_HW PE_VALUE_SYM_SW PE_RAW PE_TERM 49 %token PE_VALUE_SYM_TOOL 50 %token PE_EVENT_NAME 51 %token PE_NAME 52 %token PE_BPF_OBJECT PE_BPF_SOURCE 53 %token PE_MODIFIER_EVENT PE_MODIFIER_BP 54 %token PE_NAME_CACHE_TYPE PE_NAME_CACHE_OP_RESULT 55 %token PE_PREFIX_MEM PE_PREFIX_RAW PE_PREFIX_GROUP 56 %token PE_ERROR 57 %token PE_PMU_EVENT_PRE PE_PMU_EVENT_SUF PE_KERNEL_PMU_EVENT 58 %token PE_ARRAY_ALL PE_ARRAY_RANGE 59 %token PE_DRV_CFG_TERM 60 %type <num> PE_VALUE 61 %type <num> PE_VALUE_SYM_HW 62 %type <num> PE_VALUE_SYM_SW 63 %type <num> PE_VALUE_SYM_TOOL 64 %type <num> PE_RAW 65 %type <num> PE_TERM 66 %type <str> PE_NAME 67 %type <str> PE_BPF_OBJECT 68 %type <str> PE_BPF_SOURCE 69 %type <str> PE_NAME_CACHE_TYPE 70 %type <str> PE_NAME_CACHE_OP_RESULT 71 %type <str> PE_MODIFIER_EVENT 72 %type <str> PE_MODIFIER_BP 73 %type <str> PE_EVENT_NAME 74 %type <str> PE_PMU_EVENT_PRE PE_PMU_EVENT_SUF PE_KERNEL_PMU_EVENT 75 %type <str> PE_DRV_CFG_TERM 76 %type <num> value_sym 77 %type <head> event_config 78 %type <head> opt_event_config 79 %type <head> opt_pmu_config 80 %type <term> event_term 81 %type <head> event_pmu 82 %type <head> event_legacy_symbol 83 %type <head> event_legacy_cache 84 %type <head> event_legacy_mem 85 %type <head> event_legacy_tracepoint 86 %type <tracepoint_name> tracepoint_name 87 %type <head> event_legacy_numeric 88 %type <head> event_legacy_raw 89 %type <head> event_bpf_file 90 %type <head> event_def 91 %type <head> event_mod 92 %type <head> event_name 93 %type <head> event 94 %type <head> events 95 %type <head> group_def 96 %type <head> group 97 %type <head> groups 98 %type <array> array 99 %type <array> array_term 100 %type <array> array_terms 101 102 %union 103 { 104 char *str; 105 u64 num; 106 struct list_head *head; 107 struct parse_events_term *term; 108 struct tracepoint_name { 109 char *sys; 110 char *event; 111 } tracepoint_name; 112 struct parse_events_array array; 113 } 114 %% 115 116 start: 117 PE_START_EVENTS start_events 118 | 119 PE_START_TERMS start_terms 120 121 start_events: groups 122 { 123 struct parse_events_state *parse_state = _parse_state; 124 125 parse_events_update_lists($1, &parse_state->list); 126 } 127 128 groups: 129 groups ',' group 130 { 131 struct list_head *list = $1; 132 struct list_head *group = $3; 133 134 parse_events_update_lists(group, list); 135 $$ = list; 136 } 137 | 138 groups ',' event 139 { 140 struct list_head *list = $1; 141 struct list_head *event = $3; 142 143 parse_events_update_lists(event, list); 144 $$ = list; 145 } 146 | 147 group 148 | 149 event 150 151 group: 152 group_def ':' PE_MODIFIER_EVENT 153 { 154 struct list_head *list = $1; 155 156 ABORT_ON(parse_events__modifier_group(list, $3)); 157 $$ = list; 158 } 159 | 160 group_def 161 162 group_def: 163 PE_NAME '{' events '}' 164 { 165 struct list_head *list = $3; 166 167 inc_group_count(list, _parse_state); 168 parse_events__set_leader($1, list, _parse_state); 169 $$ = list; 170 } 171 | 172 '{' events '}' 173 { 174 struct list_head *list = $2; 175 176 inc_group_count(list, _parse_state); 177 parse_events__set_leader(NULL, list, _parse_state); 178 $$ = list; 179 } 180 181 events: 182 events ',' event 183 { 184 struct list_head *event = $3; 185 struct list_head *list = $1; 186 187 parse_events_update_lists(event, list); 188 $$ = list; 189 } 190 | 191 event 192 193 event: event_mod 194 195 event_mod: 196 event_name PE_MODIFIER_EVENT 197 { 198 struct list_head *list = $1; 199 200 /* 201 * Apply modifier on all events added by single event definition 202 * (there could be more events added for multiple tracepoint 203 * definitions via '*?'. 204 */ 205 ABORT_ON(parse_events__modifier_event(list, $2, false)); 206 $$ = list; 207 } 208 | 209 event_name 210 211 event_name: 212 PE_EVENT_NAME event_def 213 { 214 ABORT_ON(parse_events_name($2, $1)); 215 free($1); 216 $$ = $2; 217 } 218 | 219 event_def 220 221 event_def: event_pmu | 222 event_legacy_symbol | 223 event_legacy_cache sep_dc | 224 event_legacy_mem | 225 event_legacy_tracepoint sep_dc | 226 event_legacy_numeric sep_dc | 227 event_legacy_raw sep_dc | 228 event_bpf_file 229 230 event_pmu: 231 PE_NAME opt_pmu_config 232 { 233 struct parse_events_state *parse_state = _parse_state; 234 struct parse_events_error *error = parse_state->error; 235 struct list_head *list, *orig_terms, *terms; 236 237 if (parse_events_copy_term_list($2, &orig_terms)) 238 YYABORT; 239 240 if (error) 241 error->idx = @1.first_column; 242 243 ALLOC_LIST(list); 244 if (parse_events_add_pmu(_parse_state, list, $1, $2, false, false)) { 245 struct perf_pmu *pmu = NULL; 246 int ok = 0; 247 char *pattern; 248 249 if (asprintf(&pattern, "%s*", $1) < 0) 250 YYABORT; 251 252 while ((pmu = perf_pmu__scan(pmu)) != NULL) { 253 char *name = pmu->name; 254 255 if (!strncmp(name, "uncore_", 7) && 256 strncmp($1, "uncore_", 7)) 257 name += 7; 258 if (!fnmatch(pattern, name, 0)) { 259 if (parse_events_copy_term_list(orig_terms, &terms)) { 260 free(pattern); 261 YYABORT; 262 } 263 if (!parse_events_add_pmu(_parse_state, list, pmu->name, terms, true, false)) 264 ok++; 265 parse_events_terms__delete(terms); 266 } 267 } 268 269 free(pattern); 270 271 if (!ok) 272 YYABORT; 273 } 274 parse_events_terms__delete($2); 275 parse_events_terms__delete(orig_terms); 276 $$ = list; 277 } 278 | 279 PE_KERNEL_PMU_EVENT sep_dc 280 { 281 struct list_head *list; 282 283 if (parse_events_multi_pmu_add(_parse_state, $1, &list) < 0) 284 YYABORT; 285 $$ = list; 286 } 287 | 288 PE_PMU_EVENT_PRE '-' PE_PMU_EVENT_SUF sep_dc 289 { 290 struct list_head *list; 291 char pmu_name[128]; 292 293 snprintf(&pmu_name, 128, "%s-%s", $1, $3); 294 if (parse_events_multi_pmu_add(_parse_state, pmu_name, &list) < 0) 295 YYABORT; 296 $$ = list; 297 } 298 299 value_sym: 300 PE_VALUE_SYM_HW 301 | 302 PE_VALUE_SYM_SW 303 304 event_legacy_symbol: 305 value_sym '/' event_config '/' 306 { 307 struct list_head *list; 308 int type = $1 >> 16; 309 int config = $1 & 255; 310 311 ALLOC_LIST(list); 312 ABORT_ON(parse_events_add_numeric(_parse_state, list, type, config, $3)); 313 parse_events_terms__delete($3); 314 $$ = list; 315 } 316 | 317 value_sym sep_slash_slash_dc 318 { 319 struct list_head *list; 320 int type = $1 >> 16; 321 int config = $1 & 255; 322 323 ALLOC_LIST(list); 324 ABORT_ON(parse_events_add_numeric(_parse_state, list, type, config, NULL)); 325 $$ = list; 326 } 327 | 328 PE_VALUE_SYM_TOOL sep_slash_slash_dc 329 { 330 struct list_head *list; 331 332 ALLOC_LIST(list); 333 ABORT_ON(parse_events_add_tool(_parse_state, list, $1)); 334 $$ = list; 335 } 336 337 event_legacy_cache: 338 PE_NAME_CACHE_TYPE '-' PE_NAME_CACHE_OP_RESULT '-' PE_NAME_CACHE_OP_RESULT opt_event_config 339 { 340 struct parse_events_state *parse_state = _parse_state; 341 struct parse_events_error *error = parse_state->error; 342 struct list_head *list; 343 344 ALLOC_LIST(list); 345 ABORT_ON(parse_events_add_cache(list, &parse_state->idx, $1, $3, $5, error, $6)); 346 parse_events_terms__delete($6); 347 $$ = list; 348 } 349 | 350 PE_NAME_CACHE_TYPE '-' PE_NAME_CACHE_OP_RESULT opt_event_config 351 { 352 struct parse_events_state *parse_state = _parse_state; 353 struct parse_events_error *error = parse_state->error; 354 struct list_head *list; 355 356 ALLOC_LIST(list); 357 ABORT_ON(parse_events_add_cache(list, &parse_state->idx, $1, $3, NULL, error, $4)); 358 parse_events_terms__delete($4); 359 $$ = list; 360 } 361 | 362 PE_NAME_CACHE_TYPE opt_event_config 363 { 364 struct parse_events_state *parse_state = _parse_state; 365 struct parse_events_error *error = parse_state->error; 366 struct list_head *list; 367 368 ALLOC_LIST(list); 369 ABORT_ON(parse_events_add_cache(list, &parse_state->idx, $1, NULL, NULL, error, $2)); 370 parse_events_terms__delete($2); 371 $$ = list; 372 } 373 374 event_legacy_mem: 375 PE_PREFIX_MEM PE_VALUE '/' PE_VALUE ':' PE_MODIFIER_BP sep_dc 376 { 377 struct parse_events_state *parse_state = _parse_state; 378 struct list_head *list; 379 380 ALLOC_LIST(list); 381 ABORT_ON(parse_events_add_breakpoint(list, &parse_state->idx, 382 (void *) $2, $6, $4)); 383 $$ = list; 384 } 385 | 386 PE_PREFIX_MEM PE_VALUE '/' PE_VALUE sep_dc 387 { 388 struct parse_events_state *parse_state = _parse_state; 389 struct list_head *list; 390 391 ALLOC_LIST(list); 392 ABORT_ON(parse_events_add_breakpoint(list, &parse_state->idx, 393 (void *) $2, NULL, $4)); 394 $$ = list; 395 } 396 | 397 PE_PREFIX_MEM PE_VALUE ':' PE_MODIFIER_BP sep_dc 398 { 399 struct parse_events_state *parse_state = _parse_state; 400 struct list_head *list; 401 402 ALLOC_LIST(list); 403 ABORT_ON(parse_events_add_breakpoint(list, &parse_state->idx, 404 (void *) $2, $4, 0)); 405 $$ = list; 406 } 407 | 408 PE_PREFIX_MEM PE_VALUE sep_dc 409 { 410 struct parse_events_state *parse_state = _parse_state; 411 struct list_head *list; 412 413 ALLOC_LIST(list); 414 ABORT_ON(parse_events_add_breakpoint(list, &parse_state->idx, 415 (void *) $2, NULL, 0)); 416 $$ = list; 417 } 418 419 event_legacy_tracepoint: 420 tracepoint_name opt_event_config 421 { 422 struct parse_events_state *parse_state = _parse_state; 423 struct parse_events_error *error = parse_state->error; 424 struct list_head *list; 425 426 ALLOC_LIST(list); 427 if (error) 428 error->idx = @1.first_column; 429 430 if (parse_events_add_tracepoint(list, &parse_state->idx, $1.sys, $1.event, 431 error, $2)) 432 return -1; 433 434 $$ = list; 435 } 436 437 tracepoint_name: 438 PE_NAME '-' PE_NAME ':' PE_NAME 439 { 440 char sys_name[128]; 441 struct tracepoint_name tracepoint; 442 443 snprintf(&sys_name, 128, "%s-%s", $1, $3); 444 tracepoint.sys = &sys_name; 445 tracepoint.event = $5; 446 447 $$ = tracepoint; 448 } 449 | 450 PE_NAME ':' PE_NAME 451 { 452 struct tracepoint_name tracepoint = {$1, $3}; 453 454 $$ = tracepoint; 455 } 456 457 event_legacy_numeric: 458 PE_VALUE ':' PE_VALUE opt_event_config 459 { 460 struct list_head *list; 461 462 ALLOC_LIST(list); 463 ABORT_ON(parse_events_add_numeric(_parse_state, list, (u32)$1, $3, $4)); 464 parse_events_terms__delete($4); 465 $$ = list; 466 } 467 468 event_legacy_raw: 469 PE_RAW opt_event_config 470 { 471 struct list_head *list; 472 473 ALLOC_LIST(list); 474 ABORT_ON(parse_events_add_numeric(_parse_state, list, PERF_TYPE_RAW, $1, $2)); 475 parse_events_terms__delete($2); 476 $$ = list; 477 } 478 479 event_bpf_file: 480 PE_BPF_OBJECT opt_event_config 481 { 482 struct parse_events_state *parse_state = _parse_state; 483 struct parse_events_error *error = parse_state->error; 484 struct list_head *list; 485 486 ALLOC_LIST(list); 487 ABORT_ON(parse_events_load_bpf(parse_state, list, $1, false, $2)); 488 parse_events_terms__delete($2); 489 $$ = list; 490 } 491 | 492 PE_BPF_SOURCE opt_event_config 493 { 494 struct list_head *list; 495 496 ALLOC_LIST(list); 497 ABORT_ON(parse_events_load_bpf(_parse_state, list, $1, true, $2)); 498 parse_events_terms__delete($2); 499 $$ = list; 500 } 501 502 opt_event_config: 503 '/' event_config '/' 504 { 505 $$ = $2; 506 } 507 | 508 '/' '/' 509 { 510 $$ = NULL; 511 } 512 | 513 { 514 $$ = NULL; 515 } 516 517 opt_pmu_config: 518 '/' event_config '/' 519 { 520 $$ = $2; 521 } 522 | 523 '/' '/' 524 { 525 $$ = NULL; 526 } 527 528 start_terms: event_config 529 { 530 struct parse_events_state *parse_state = _parse_state; 531 parse_state->terms = $1; 532 } 533 534 event_config: 535 event_config ',' event_term 536 { 537 struct list_head *head = $1; 538 struct parse_events_term *term = $3; 539 540 ABORT_ON(!head); 541 list_add_tail(&term->list, head); 542 $$ = $1; 543 } 544 | 545 event_term 546 { 547 struct list_head *head = malloc(sizeof(*head)); 548 struct parse_events_term *term = $1; 549 550 ABORT_ON(!head); 551 INIT_LIST_HEAD(head); 552 list_add_tail(&term->list, head); 553 $$ = head; 554 } 555 556 event_term: 557 PE_NAME '=' PE_NAME 558 { 559 struct parse_events_term *term; 560 561 ABORT_ON(parse_events_term__str(&term, PARSE_EVENTS__TERM_TYPE_USER, 562 $1, $3, &@1, &@3)); 563 $$ = term; 564 } 565 | 566 PE_NAME '=' PE_VALUE 567 { 568 struct parse_events_term *term; 569 570 ABORT_ON(parse_events_term__num(&term, PARSE_EVENTS__TERM_TYPE_USER, 571 $1, $3, false, &@1, &@3)); 572 $$ = term; 573 } 574 | 575 PE_NAME '=' PE_VALUE_SYM_HW 576 { 577 struct parse_events_term *term; 578 int config = $3 & 255; 579 580 ABORT_ON(parse_events_term__sym_hw(&term, $1, config)); 581 $$ = term; 582 } 583 | 584 PE_NAME 585 { 586 struct parse_events_term *term; 587 588 ABORT_ON(parse_events_term__num(&term, PARSE_EVENTS__TERM_TYPE_USER, 589 $1, 1, true, &@1, NULL)); 590 $$ = term; 591 } 592 | 593 PE_VALUE_SYM_HW 594 { 595 struct parse_events_term *term; 596 int config = $1 & 255; 597 598 ABORT_ON(parse_events_term__sym_hw(&term, NULL, config)); 599 $$ = term; 600 } 601 | 602 PE_TERM '=' PE_NAME 603 { 604 struct parse_events_term *term; 605 606 ABORT_ON(parse_events_term__str(&term, (int)$1, NULL, $3, &@1, &@3)); 607 $$ = term; 608 } 609 | 610 PE_TERM '=' PE_VALUE 611 { 612 struct parse_events_term *term; 613 614 ABORT_ON(parse_events_term__num(&term, (int)$1, NULL, $3, false, &@1, &@3)); 615 $$ = term; 616 } 617 | 618 PE_TERM 619 { 620 struct parse_events_term *term; 621 622 ABORT_ON(parse_events_term__num(&term, (int)$1, NULL, 1, true, &@1, NULL)); 623 $$ = term; 624 } 625 | 626 PE_NAME array '=' PE_NAME 627 { 628 struct parse_events_term *term; 629 int i; 630 631 ABORT_ON(parse_events_term__str(&term, PARSE_EVENTS__TERM_TYPE_USER, 632 $1, $4, &@1, &@4)); 633 634 term->array = $2; 635 $$ = term; 636 } 637 | 638 PE_NAME array '=' PE_VALUE 639 { 640 struct parse_events_term *term; 641 642 ABORT_ON(parse_events_term__num(&term, PARSE_EVENTS__TERM_TYPE_USER, 643 $1, $4, false, &@1, &@4)); 644 term->array = $2; 645 $$ = term; 646 } 647 | 648 PE_DRV_CFG_TERM 649 { 650 struct parse_events_term *term; 651 652 ABORT_ON(parse_events_term__str(&term, PARSE_EVENTS__TERM_TYPE_DRV_CFG, 653 $1, $1, &@1, NULL)); 654 $$ = term; 655 } 656 657 array: 658 '[' array_terms ']' 659 { 660 $$ = $2; 661 } 662 | 663 PE_ARRAY_ALL 664 { 665 $$.nr_ranges = 0; 666 $$.ranges = NULL; 667 } 668 669 array_terms: 670 array_terms ',' array_term 671 { 672 struct parse_events_array new_array; 673 674 new_array.nr_ranges = $1.nr_ranges + $3.nr_ranges; 675 new_array.ranges = malloc(sizeof(new_array.ranges[0]) * 676 new_array.nr_ranges); 677 ABORT_ON(!new_array.ranges); 678 memcpy(&new_array.ranges[0], $1.ranges, 679 $1.nr_ranges * sizeof(new_array.ranges[0])); 680 memcpy(&new_array.ranges[$1.nr_ranges], $3.ranges, 681 $3.nr_ranges * sizeof(new_array.ranges[0])); 682 free($1.ranges); 683 free($3.ranges); 684 $$ = new_array; 685 } 686 | 687 array_term 688 689 array_term: 690 PE_VALUE 691 { 692 struct parse_events_array array; 693 694 array.nr_ranges = 1; 695 array.ranges = malloc(sizeof(array.ranges[0])); 696 ABORT_ON(!array.ranges); 697 array.ranges[0].start = $1; 698 array.ranges[0].length = 1; 699 $$ = array; 700 } 701 | 702 PE_VALUE PE_ARRAY_RANGE PE_VALUE 703 { 704 struct parse_events_array array; 705 706 ABORT_ON($3 < $1); 707 array.nr_ranges = 1; 708 array.ranges = malloc(sizeof(array.ranges[0])); 709 ABORT_ON(!array.ranges); 710 array.ranges[0].start = $1; 711 array.ranges[0].length = $3 - $1 + 1; 712 $$ = array; 713 } 714 715 sep_dc: ':' | 716 717 sep_slash_slash_dc: '/' '/' | ':' | 718 719 %% 720 721 void parse_events_error(YYLTYPE *loc, void *parse_state, 722 void *scanner __maybe_unused, 723 char const *msg __maybe_unused) 724 { 725 parse_events_evlist_error(parse_state, loc->last_column, "parser error"); 726 } 727