1 // SPDX-License-Identifier: GPL-2.0 2 #include <linux/hw_breakpoint.h> 3 #include <linux/err.h> 4 #include <linux/list_sort.h> 5 #include <linux/zalloc.h> 6 #include <dirent.h> 7 #include <errno.h> 8 #include <sys/ioctl.h> 9 #include <sys/param.h> 10 #include "term.h" 11 #include "evlist.h" 12 #include "evsel.h" 13 #include <subcmd/parse-options.h> 14 #include "parse-events.h" 15 #include "string2.h" 16 #include "strlist.h" 17 #include "debug.h" 18 #include <api/fs/tracing_path.h> 19 #include <perf/cpumap.h> 20 #include <util/parse-events-bison.h> 21 #include <util/parse-events-flex.h> 22 #include "pmu.h" 23 #include "pmus.h" 24 #include "asm/bug.h" 25 #include "util/parse-branch-options.h" 26 #include "util/evsel_config.h" 27 #include "util/event.h" 28 #include "util/bpf-filter.h" 29 #include "util/util.h" 30 #include "tracepoint.h" 31 32 #define MAX_NAME_LEN 100 33 34 #ifdef PARSER_DEBUG 35 extern int parse_events_debug; 36 #endif 37 static int get_config_terms(struct list_head *head_config, 38 struct list_head *head_terms __maybe_unused); 39 40 struct event_symbol event_symbols_hw[PERF_COUNT_HW_MAX] = { 41 [PERF_COUNT_HW_CPU_CYCLES] = { 42 .symbol = "cpu-cycles", 43 .alias = "cycles", 44 }, 45 [PERF_COUNT_HW_INSTRUCTIONS] = { 46 .symbol = "instructions", 47 .alias = "", 48 }, 49 [PERF_COUNT_HW_CACHE_REFERENCES] = { 50 .symbol = "cache-references", 51 .alias = "", 52 }, 53 [PERF_COUNT_HW_CACHE_MISSES] = { 54 .symbol = "cache-misses", 55 .alias = "", 56 }, 57 [PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = { 58 .symbol = "branch-instructions", 59 .alias = "branches", 60 }, 61 [PERF_COUNT_HW_BRANCH_MISSES] = { 62 .symbol = "branch-misses", 63 .alias = "", 64 }, 65 [PERF_COUNT_HW_BUS_CYCLES] = { 66 .symbol = "bus-cycles", 67 .alias = "", 68 }, 69 [PERF_COUNT_HW_STALLED_CYCLES_FRONTEND] = { 70 .symbol = "stalled-cycles-frontend", 71 .alias = "idle-cycles-frontend", 72 }, 73 [PERF_COUNT_HW_STALLED_CYCLES_BACKEND] = { 74 .symbol = "stalled-cycles-backend", 75 .alias = "idle-cycles-backend", 76 }, 77 [PERF_COUNT_HW_REF_CPU_CYCLES] = { 78 .symbol = "ref-cycles", 79 .alias = "", 80 }, 81 }; 82 83 struct event_symbol event_symbols_sw[PERF_COUNT_SW_MAX] = { 84 [PERF_COUNT_SW_CPU_CLOCK] = { 85 .symbol = "cpu-clock", 86 .alias = "", 87 }, 88 [PERF_COUNT_SW_TASK_CLOCK] = { 89 .symbol = "task-clock", 90 .alias = "", 91 }, 92 [PERF_COUNT_SW_PAGE_FAULTS] = { 93 .symbol = "page-faults", 94 .alias = "faults", 95 }, 96 [PERF_COUNT_SW_CONTEXT_SWITCHES] = { 97 .symbol = "context-switches", 98 .alias = "cs", 99 }, 100 [PERF_COUNT_SW_CPU_MIGRATIONS] = { 101 .symbol = "cpu-migrations", 102 .alias = "migrations", 103 }, 104 [PERF_COUNT_SW_PAGE_FAULTS_MIN] = { 105 .symbol = "minor-faults", 106 .alias = "", 107 }, 108 [PERF_COUNT_SW_PAGE_FAULTS_MAJ] = { 109 .symbol = "major-faults", 110 .alias = "", 111 }, 112 [PERF_COUNT_SW_ALIGNMENT_FAULTS] = { 113 .symbol = "alignment-faults", 114 .alias = "", 115 }, 116 [PERF_COUNT_SW_EMULATION_FAULTS] = { 117 .symbol = "emulation-faults", 118 .alias = "", 119 }, 120 [PERF_COUNT_SW_DUMMY] = { 121 .symbol = "dummy", 122 .alias = "", 123 }, 124 [PERF_COUNT_SW_BPF_OUTPUT] = { 125 .symbol = "bpf-output", 126 .alias = "", 127 }, 128 [PERF_COUNT_SW_CGROUP_SWITCHES] = { 129 .symbol = "cgroup-switches", 130 .alias = "", 131 }, 132 }; 133 134 const char *event_type(int type) 135 { 136 switch (type) { 137 case PERF_TYPE_HARDWARE: 138 return "hardware"; 139 140 case PERF_TYPE_SOFTWARE: 141 return "software"; 142 143 case PERF_TYPE_TRACEPOINT: 144 return "tracepoint"; 145 146 case PERF_TYPE_HW_CACHE: 147 return "hardware-cache"; 148 149 default: 150 break; 151 } 152 153 return "unknown"; 154 } 155 156 static char *get_config_str(struct list_head *head_terms, int type_term) 157 { 158 struct parse_events_term *term; 159 160 if (!head_terms) 161 return NULL; 162 163 list_for_each_entry(term, head_terms, list) 164 if (term->type_term == type_term) 165 return term->val.str; 166 167 return NULL; 168 } 169 170 static char *get_config_metric_id(struct list_head *head_terms) 171 { 172 return get_config_str(head_terms, PARSE_EVENTS__TERM_TYPE_METRIC_ID); 173 } 174 175 static char *get_config_name(struct list_head *head_terms) 176 { 177 return get_config_str(head_terms, PARSE_EVENTS__TERM_TYPE_NAME); 178 } 179 180 /** 181 * fix_raw - For each raw term see if there is an event (aka alias) in pmu that 182 * matches the raw's string value. If the string value matches an 183 * event then change the term to be an event, if not then change it to 184 * be a config term. For example, "read" may be an event of the PMU or 185 * a raw hex encoding of 0xead. The fix-up is done late so the PMU of 186 * the event can be determined and we don't need to scan all PMUs 187 * ahead-of-time. 188 * @config_terms: the list of terms that may contain a raw term. 189 * @pmu: the PMU to scan for events from. 190 */ 191 static void fix_raw(struct list_head *config_terms, struct perf_pmu *pmu) 192 { 193 struct parse_events_term *term; 194 195 list_for_each_entry(term, config_terms, list) { 196 struct perf_pmu_alias *alias; 197 bool matched = false; 198 199 if (term->type_term != PARSE_EVENTS__TERM_TYPE_RAW) 200 continue; 201 202 list_for_each_entry(alias, &pmu->aliases, list) { 203 if (!strcmp(alias->name, term->val.str)) { 204 free(term->config); 205 term->config = term->val.str; 206 term->type_val = PARSE_EVENTS__TERM_TYPE_NUM; 207 term->type_term = PARSE_EVENTS__TERM_TYPE_USER; 208 term->val.num = 1; 209 term->no_value = true; 210 matched = true; 211 break; 212 } 213 } 214 if (!matched) { 215 u64 num; 216 217 free(term->config); 218 term->config = strdup("config"); 219 errno = 0; 220 num = strtoull(term->val.str + 1, NULL, 16); 221 assert(errno == 0); 222 free(term->val.str); 223 term->type_val = PARSE_EVENTS__TERM_TYPE_NUM; 224 term->type_term = PARSE_EVENTS__TERM_TYPE_CONFIG; 225 term->val.num = num; 226 term->no_value = false; 227 } 228 } 229 } 230 231 static struct evsel * 232 __add_event(struct list_head *list, int *idx, 233 struct perf_event_attr *attr, 234 bool init_attr, 235 const char *name, const char *metric_id, struct perf_pmu *pmu, 236 struct list_head *config_terms, bool auto_merge_stats, 237 const char *cpu_list) 238 { 239 struct evsel *evsel; 240 struct perf_cpu_map *cpus = pmu ? perf_cpu_map__get(pmu->cpus) : 241 cpu_list ? perf_cpu_map__new(cpu_list) : NULL; 242 243 if (pmu) 244 perf_pmu__warn_invalid_formats(pmu); 245 246 if (pmu && (attr->type == PERF_TYPE_RAW || attr->type >= PERF_TYPE_MAX)) { 247 perf_pmu__warn_invalid_config(pmu, attr->config, name, 248 PERF_PMU_FORMAT_VALUE_CONFIG, "config"); 249 perf_pmu__warn_invalid_config(pmu, attr->config1, name, 250 PERF_PMU_FORMAT_VALUE_CONFIG1, "config1"); 251 perf_pmu__warn_invalid_config(pmu, attr->config2, name, 252 PERF_PMU_FORMAT_VALUE_CONFIG2, "config2"); 253 perf_pmu__warn_invalid_config(pmu, attr->config3, name, 254 PERF_PMU_FORMAT_VALUE_CONFIG3, "config3"); 255 } 256 if (init_attr) 257 event_attr_init(attr); 258 259 evsel = evsel__new_idx(attr, *idx); 260 if (!evsel) { 261 perf_cpu_map__put(cpus); 262 return NULL; 263 } 264 265 (*idx)++; 266 evsel->core.cpus = cpus; 267 evsel->core.own_cpus = perf_cpu_map__get(cpus); 268 evsel->core.requires_cpu = pmu ? pmu->is_uncore : false; 269 evsel->core.is_pmu_core = pmu ? pmu->is_core : false; 270 evsel->auto_merge_stats = auto_merge_stats; 271 evsel->pmu = pmu; 272 evsel->pmu_name = pmu && pmu->name ? strdup(pmu->name) : NULL; 273 274 if (name) 275 evsel->name = strdup(name); 276 277 if (metric_id) 278 evsel->metric_id = strdup(metric_id); 279 280 if (config_terms) 281 list_splice_init(config_terms, &evsel->config_terms); 282 283 if (list) 284 list_add_tail(&evsel->core.node, list); 285 286 return evsel; 287 } 288 289 struct evsel *parse_events__add_event(int idx, struct perf_event_attr *attr, 290 const char *name, const char *metric_id, 291 struct perf_pmu *pmu) 292 { 293 return __add_event(/*list=*/NULL, &idx, attr, /*init_attr=*/false, name, 294 metric_id, pmu, /*config_terms=*/NULL, 295 /*auto_merge_stats=*/false, /*cpu_list=*/NULL); 296 } 297 298 static int add_event(struct list_head *list, int *idx, 299 struct perf_event_attr *attr, const char *name, 300 const char *metric_id, struct list_head *config_terms) 301 { 302 return __add_event(list, idx, attr, /*init_attr*/true, name, metric_id, 303 /*pmu=*/NULL, config_terms, 304 /*auto_merge_stats=*/false, /*cpu_list=*/NULL) ? 0 : -ENOMEM; 305 } 306 307 static int add_event_tool(struct list_head *list, int *idx, 308 enum perf_tool_event tool_event) 309 { 310 struct evsel *evsel; 311 struct perf_event_attr attr = { 312 .type = PERF_TYPE_SOFTWARE, 313 .config = PERF_COUNT_SW_DUMMY, 314 }; 315 316 evsel = __add_event(list, idx, &attr, /*init_attr=*/true, /*name=*/NULL, 317 /*metric_id=*/NULL, /*pmu=*/NULL, 318 /*config_terms=*/NULL, /*auto_merge_stats=*/false, 319 /*cpu_list=*/"0"); 320 if (!evsel) 321 return -ENOMEM; 322 evsel->tool_event = tool_event; 323 if (tool_event == PERF_TOOL_DURATION_TIME 324 || tool_event == PERF_TOOL_USER_TIME 325 || tool_event == PERF_TOOL_SYSTEM_TIME) { 326 free((char *)evsel->unit); 327 evsel->unit = strdup("ns"); 328 } 329 return 0; 330 } 331 332 /** 333 * parse_aliases - search names for entries beginning or equalling str ignoring 334 * case. If mutliple entries in names match str then the longest 335 * is chosen. 336 * @str: The needle to look for. 337 * @names: The haystack to search. 338 * @size: The size of the haystack. 339 * @longest: Out argument giving the length of the matching entry. 340 */ 341 static int parse_aliases(const char *str, const char *const names[][EVSEL__MAX_ALIASES], int size, 342 int *longest) 343 { 344 *longest = -1; 345 for (int i = 0; i < size; i++) { 346 for (int j = 0; j < EVSEL__MAX_ALIASES && names[i][j]; j++) { 347 int n = strlen(names[i][j]); 348 349 if (n > *longest && !strncasecmp(str, names[i][j], n)) 350 *longest = n; 351 } 352 if (*longest > 0) 353 return i; 354 } 355 356 return -1; 357 } 358 359 typedef int config_term_func_t(struct perf_event_attr *attr, 360 struct parse_events_term *term, 361 struct parse_events_error *err); 362 static int config_term_common(struct perf_event_attr *attr, 363 struct parse_events_term *term, 364 struct parse_events_error *err); 365 static int config_attr(struct perf_event_attr *attr, 366 struct list_head *head, 367 struct parse_events_error *err, 368 config_term_func_t config_term); 369 370 /** 371 * parse_events__decode_legacy_cache - Search name for the legacy cache event 372 * name composed of 1, 2 or 3 hyphen 373 * separated sections. The first section is 374 * the cache type while the others are the 375 * optional op and optional result. To make 376 * life hard the names in the table also 377 * contain hyphens and the longest name 378 * should always be selected. 379 */ 380 int parse_events__decode_legacy_cache(const char *name, int extended_pmu_type, __u64 *config) 381 { 382 int len, cache_type = -1, cache_op = -1, cache_result = -1; 383 const char *name_end = &name[strlen(name) + 1]; 384 const char *str = name; 385 386 cache_type = parse_aliases(str, evsel__hw_cache, PERF_COUNT_HW_CACHE_MAX, &len); 387 if (cache_type == -1) 388 return -EINVAL; 389 str += len + 1; 390 391 if (str < name_end) { 392 cache_op = parse_aliases(str, evsel__hw_cache_op, 393 PERF_COUNT_HW_CACHE_OP_MAX, &len); 394 if (cache_op >= 0) { 395 if (!evsel__is_cache_op_valid(cache_type, cache_op)) 396 return -EINVAL; 397 str += len + 1; 398 } else { 399 cache_result = parse_aliases(str, evsel__hw_cache_result, 400 PERF_COUNT_HW_CACHE_RESULT_MAX, &len); 401 if (cache_result >= 0) 402 str += len + 1; 403 } 404 } 405 if (str < name_end) { 406 if (cache_op < 0) { 407 cache_op = parse_aliases(str, evsel__hw_cache_op, 408 PERF_COUNT_HW_CACHE_OP_MAX, &len); 409 if (cache_op >= 0) { 410 if (!evsel__is_cache_op_valid(cache_type, cache_op)) 411 return -EINVAL; 412 } 413 } else if (cache_result < 0) { 414 cache_result = parse_aliases(str, evsel__hw_cache_result, 415 PERF_COUNT_HW_CACHE_RESULT_MAX, &len); 416 } 417 } 418 419 /* 420 * Fall back to reads: 421 */ 422 if (cache_op == -1) 423 cache_op = PERF_COUNT_HW_CACHE_OP_READ; 424 425 /* 426 * Fall back to accesses: 427 */ 428 if (cache_result == -1) 429 cache_result = PERF_COUNT_HW_CACHE_RESULT_ACCESS; 430 431 *config = cache_type | (cache_op << 8) | (cache_result << 16); 432 if (perf_pmus__supports_extended_type()) 433 *config |= (__u64)extended_pmu_type << PERF_PMU_TYPE_SHIFT; 434 return 0; 435 } 436 437 /** 438 * parse_events__filter_pmu - returns false if a wildcard PMU should be 439 * considered, true if it should be filtered. 440 */ 441 bool parse_events__filter_pmu(const struct parse_events_state *parse_state, 442 const struct perf_pmu *pmu) 443 { 444 if (parse_state->pmu_filter == NULL) 445 return false; 446 447 if (pmu->name == NULL) 448 return true; 449 450 return strcmp(parse_state->pmu_filter, pmu->name) != 0; 451 } 452 453 int parse_events_add_cache(struct list_head *list, int *idx, const char *name, 454 struct parse_events_state *parse_state, 455 struct list_head *head_config) 456 { 457 struct perf_pmu *pmu = NULL; 458 bool found_supported = false; 459 const char *config_name = get_config_name(head_config); 460 const char *metric_id = get_config_metric_id(head_config); 461 462 /* Legacy cache events are only supported by core PMUs. */ 463 while ((pmu = perf_pmus__scan_core(pmu)) != NULL) { 464 LIST_HEAD(config_terms); 465 struct perf_event_attr attr; 466 int ret; 467 468 if (parse_events__filter_pmu(parse_state, pmu)) 469 continue; 470 471 memset(&attr, 0, sizeof(attr)); 472 attr.type = PERF_TYPE_HW_CACHE; 473 474 ret = parse_events__decode_legacy_cache(name, pmu->type, &attr.config); 475 if (ret) 476 return ret; 477 478 found_supported = true; 479 480 if (head_config) { 481 if (config_attr(&attr, head_config, parse_state->error, config_term_common)) 482 return -EINVAL; 483 484 if (get_config_terms(head_config, &config_terms)) 485 return -ENOMEM; 486 } 487 488 if (__add_event(list, idx, &attr, /*init_attr*/true, config_name ?: name, 489 metric_id, pmu, &config_terms, /*auto_merge_stats=*/false, 490 /*cpu_list=*/NULL) == NULL) 491 return -ENOMEM; 492 493 free_config_terms(&config_terms); 494 } 495 return found_supported ? 0 : -EINVAL; 496 } 497 498 #ifdef HAVE_LIBTRACEEVENT 499 static void tracepoint_error(struct parse_events_error *e, int err, 500 const char *sys, const char *name, int column) 501 { 502 const char *str; 503 char help[BUFSIZ]; 504 505 if (!e) 506 return; 507 508 /* 509 * We get error directly from syscall errno ( > 0), 510 * or from encoded pointer's error ( < 0). 511 */ 512 err = abs(err); 513 514 switch (err) { 515 case EACCES: 516 str = "can't access trace events"; 517 break; 518 case ENOENT: 519 str = "unknown tracepoint"; 520 break; 521 default: 522 str = "failed to add tracepoint"; 523 break; 524 } 525 526 tracing_path__strerror_open_tp(err, help, sizeof(help), sys, name); 527 parse_events_error__handle(e, column, strdup(str), strdup(help)); 528 } 529 530 static int add_tracepoint(struct list_head *list, int *idx, 531 const char *sys_name, const char *evt_name, 532 struct parse_events_error *err, 533 struct list_head *head_config, void *loc_) 534 { 535 YYLTYPE *loc = loc_; 536 struct evsel *evsel = evsel__newtp_idx(sys_name, evt_name, (*idx)++); 537 538 if (IS_ERR(evsel)) { 539 tracepoint_error(err, PTR_ERR(evsel), sys_name, evt_name, loc->first_column); 540 return PTR_ERR(evsel); 541 } 542 543 if (head_config) { 544 LIST_HEAD(config_terms); 545 546 if (get_config_terms(head_config, &config_terms)) 547 return -ENOMEM; 548 list_splice(&config_terms, &evsel->config_terms); 549 } 550 551 list_add_tail(&evsel->core.node, list); 552 return 0; 553 } 554 555 static int add_tracepoint_multi_event(struct list_head *list, int *idx, 556 const char *sys_name, const char *evt_name, 557 struct parse_events_error *err, 558 struct list_head *head_config, YYLTYPE *loc) 559 { 560 char *evt_path; 561 struct dirent *evt_ent; 562 DIR *evt_dir; 563 int ret = 0, found = 0; 564 565 evt_path = get_events_file(sys_name); 566 if (!evt_path) { 567 tracepoint_error(err, errno, sys_name, evt_name, loc->first_column); 568 return -1; 569 } 570 evt_dir = opendir(evt_path); 571 if (!evt_dir) { 572 put_events_file(evt_path); 573 tracepoint_error(err, errno, sys_name, evt_name, loc->first_column); 574 return -1; 575 } 576 577 while (!ret && (evt_ent = readdir(evt_dir))) { 578 if (!strcmp(evt_ent->d_name, ".") 579 || !strcmp(evt_ent->d_name, "..") 580 || !strcmp(evt_ent->d_name, "enable") 581 || !strcmp(evt_ent->d_name, "filter")) 582 continue; 583 584 if (!strglobmatch(evt_ent->d_name, evt_name)) 585 continue; 586 587 found++; 588 589 ret = add_tracepoint(list, idx, sys_name, evt_ent->d_name, 590 err, head_config, loc); 591 } 592 593 if (!found) { 594 tracepoint_error(err, ENOENT, sys_name, evt_name, loc->first_column); 595 ret = -1; 596 } 597 598 put_events_file(evt_path); 599 closedir(evt_dir); 600 return ret; 601 } 602 603 static int add_tracepoint_event(struct list_head *list, int *idx, 604 const char *sys_name, const char *evt_name, 605 struct parse_events_error *err, 606 struct list_head *head_config, YYLTYPE *loc) 607 { 608 return strpbrk(evt_name, "*?") ? 609 add_tracepoint_multi_event(list, idx, sys_name, evt_name, 610 err, head_config, loc) : 611 add_tracepoint(list, idx, sys_name, evt_name, 612 err, head_config, loc); 613 } 614 615 static int add_tracepoint_multi_sys(struct list_head *list, int *idx, 616 const char *sys_name, const char *evt_name, 617 struct parse_events_error *err, 618 struct list_head *head_config, YYLTYPE *loc) 619 { 620 struct dirent *events_ent; 621 DIR *events_dir; 622 int ret = 0; 623 624 events_dir = tracing_events__opendir(); 625 if (!events_dir) { 626 tracepoint_error(err, errno, sys_name, evt_name, loc->first_column); 627 return -1; 628 } 629 630 while (!ret && (events_ent = readdir(events_dir))) { 631 if (!strcmp(events_ent->d_name, ".") 632 || !strcmp(events_ent->d_name, "..") 633 || !strcmp(events_ent->d_name, "enable") 634 || !strcmp(events_ent->d_name, "header_event") 635 || !strcmp(events_ent->d_name, "header_page")) 636 continue; 637 638 if (!strglobmatch(events_ent->d_name, sys_name)) 639 continue; 640 641 ret = add_tracepoint_event(list, idx, events_ent->d_name, 642 evt_name, err, head_config, loc); 643 } 644 645 closedir(events_dir); 646 return ret; 647 } 648 #endif /* HAVE_LIBTRACEEVENT */ 649 650 static int 651 parse_breakpoint_type(const char *type, struct perf_event_attr *attr) 652 { 653 int i; 654 655 for (i = 0; i < 3; i++) { 656 if (!type || !type[i]) 657 break; 658 659 #define CHECK_SET_TYPE(bit) \ 660 do { \ 661 if (attr->bp_type & bit) \ 662 return -EINVAL; \ 663 else \ 664 attr->bp_type |= bit; \ 665 } while (0) 666 667 switch (type[i]) { 668 case 'r': 669 CHECK_SET_TYPE(HW_BREAKPOINT_R); 670 break; 671 case 'w': 672 CHECK_SET_TYPE(HW_BREAKPOINT_W); 673 break; 674 case 'x': 675 CHECK_SET_TYPE(HW_BREAKPOINT_X); 676 break; 677 default: 678 return -EINVAL; 679 } 680 } 681 682 #undef CHECK_SET_TYPE 683 684 if (!attr->bp_type) /* Default */ 685 attr->bp_type = HW_BREAKPOINT_R | HW_BREAKPOINT_W; 686 687 return 0; 688 } 689 690 int parse_events_add_breakpoint(struct parse_events_state *parse_state, 691 struct list_head *list, 692 u64 addr, char *type, u64 len, 693 struct list_head *head_config __maybe_unused) 694 { 695 struct perf_event_attr attr; 696 LIST_HEAD(config_terms); 697 const char *name; 698 699 memset(&attr, 0, sizeof(attr)); 700 attr.bp_addr = addr; 701 702 if (parse_breakpoint_type(type, &attr)) 703 return -EINVAL; 704 705 /* Provide some defaults if len is not specified */ 706 if (!len) { 707 if (attr.bp_type == HW_BREAKPOINT_X) 708 len = sizeof(long); 709 else 710 len = HW_BREAKPOINT_LEN_4; 711 } 712 713 attr.bp_len = len; 714 715 attr.type = PERF_TYPE_BREAKPOINT; 716 attr.sample_period = 1; 717 718 if (head_config) { 719 if (config_attr(&attr, head_config, parse_state->error, 720 config_term_common)) 721 return -EINVAL; 722 723 if (get_config_terms(head_config, &config_terms)) 724 return -ENOMEM; 725 } 726 727 name = get_config_name(head_config); 728 729 return add_event(list, &parse_state->idx, &attr, name, /*mertic_id=*/NULL, 730 &config_terms); 731 } 732 733 static int check_type_val(struct parse_events_term *term, 734 struct parse_events_error *err, 735 int type) 736 { 737 if (type == term->type_val) 738 return 0; 739 740 if (err) { 741 parse_events_error__handle(err, term->err_val, 742 type == PARSE_EVENTS__TERM_TYPE_NUM 743 ? strdup("expected numeric value") 744 : strdup("expected string value"), 745 NULL); 746 } 747 return -EINVAL; 748 } 749 750 /* 751 * Update according to parse-events.l 752 */ 753 static const char *config_term_names[__PARSE_EVENTS__TERM_TYPE_NR] = { 754 [PARSE_EVENTS__TERM_TYPE_USER] = "<sysfs term>", 755 [PARSE_EVENTS__TERM_TYPE_CONFIG] = "config", 756 [PARSE_EVENTS__TERM_TYPE_CONFIG1] = "config1", 757 [PARSE_EVENTS__TERM_TYPE_CONFIG2] = "config2", 758 [PARSE_EVENTS__TERM_TYPE_CONFIG3] = "config3", 759 [PARSE_EVENTS__TERM_TYPE_NAME] = "name", 760 [PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD] = "period", 761 [PARSE_EVENTS__TERM_TYPE_SAMPLE_FREQ] = "freq", 762 [PARSE_EVENTS__TERM_TYPE_BRANCH_SAMPLE_TYPE] = "branch_type", 763 [PARSE_EVENTS__TERM_TYPE_TIME] = "time", 764 [PARSE_EVENTS__TERM_TYPE_CALLGRAPH] = "call-graph", 765 [PARSE_EVENTS__TERM_TYPE_STACKSIZE] = "stack-size", 766 [PARSE_EVENTS__TERM_TYPE_NOINHERIT] = "no-inherit", 767 [PARSE_EVENTS__TERM_TYPE_INHERIT] = "inherit", 768 [PARSE_EVENTS__TERM_TYPE_MAX_STACK] = "max-stack", 769 [PARSE_EVENTS__TERM_TYPE_MAX_EVENTS] = "nr", 770 [PARSE_EVENTS__TERM_TYPE_OVERWRITE] = "overwrite", 771 [PARSE_EVENTS__TERM_TYPE_NOOVERWRITE] = "no-overwrite", 772 [PARSE_EVENTS__TERM_TYPE_DRV_CFG] = "driver-config", 773 [PARSE_EVENTS__TERM_TYPE_PERCORE] = "percore", 774 [PARSE_EVENTS__TERM_TYPE_AUX_OUTPUT] = "aux-output", 775 [PARSE_EVENTS__TERM_TYPE_AUX_SAMPLE_SIZE] = "aux-sample-size", 776 [PARSE_EVENTS__TERM_TYPE_METRIC_ID] = "metric-id", 777 [PARSE_EVENTS__TERM_TYPE_RAW] = "raw", 778 [PARSE_EVENTS__TERM_TYPE_LEGACY_CACHE] = "legacy-cache", 779 [PARSE_EVENTS__TERM_TYPE_HARDWARE] = "hardware", 780 }; 781 782 static bool config_term_shrinked; 783 784 static bool 785 config_term_avail(int term_type, struct parse_events_error *err) 786 { 787 char *err_str; 788 789 if (term_type < 0 || term_type >= __PARSE_EVENTS__TERM_TYPE_NR) { 790 parse_events_error__handle(err, -1, 791 strdup("Invalid term_type"), NULL); 792 return false; 793 } 794 if (!config_term_shrinked) 795 return true; 796 797 switch (term_type) { 798 case PARSE_EVENTS__TERM_TYPE_CONFIG: 799 case PARSE_EVENTS__TERM_TYPE_CONFIG1: 800 case PARSE_EVENTS__TERM_TYPE_CONFIG2: 801 case PARSE_EVENTS__TERM_TYPE_CONFIG3: 802 case PARSE_EVENTS__TERM_TYPE_NAME: 803 case PARSE_EVENTS__TERM_TYPE_METRIC_ID: 804 case PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD: 805 case PARSE_EVENTS__TERM_TYPE_PERCORE: 806 return true; 807 default: 808 if (!err) 809 return false; 810 811 /* term_type is validated so indexing is safe */ 812 if (asprintf(&err_str, "'%s' is not usable in 'perf stat'", 813 config_term_names[term_type]) >= 0) 814 parse_events_error__handle(err, -1, err_str, NULL); 815 return false; 816 } 817 } 818 819 void parse_events__shrink_config_terms(void) 820 { 821 config_term_shrinked = true; 822 } 823 824 static int config_term_common(struct perf_event_attr *attr, 825 struct parse_events_term *term, 826 struct parse_events_error *err) 827 { 828 #define CHECK_TYPE_VAL(type) \ 829 do { \ 830 if (check_type_val(term, err, PARSE_EVENTS__TERM_TYPE_ ## type)) \ 831 return -EINVAL; \ 832 } while (0) 833 834 switch (term->type_term) { 835 case PARSE_EVENTS__TERM_TYPE_CONFIG: 836 CHECK_TYPE_VAL(NUM); 837 attr->config = term->val.num; 838 break; 839 case PARSE_EVENTS__TERM_TYPE_CONFIG1: 840 CHECK_TYPE_VAL(NUM); 841 attr->config1 = term->val.num; 842 break; 843 case PARSE_EVENTS__TERM_TYPE_CONFIG2: 844 CHECK_TYPE_VAL(NUM); 845 attr->config2 = term->val.num; 846 break; 847 case PARSE_EVENTS__TERM_TYPE_CONFIG3: 848 CHECK_TYPE_VAL(NUM); 849 attr->config3 = term->val.num; 850 break; 851 case PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD: 852 CHECK_TYPE_VAL(NUM); 853 break; 854 case PARSE_EVENTS__TERM_TYPE_SAMPLE_FREQ: 855 CHECK_TYPE_VAL(NUM); 856 break; 857 case PARSE_EVENTS__TERM_TYPE_BRANCH_SAMPLE_TYPE: 858 CHECK_TYPE_VAL(STR); 859 if (strcmp(term->val.str, "no") && 860 parse_branch_str(term->val.str, 861 &attr->branch_sample_type)) { 862 parse_events_error__handle(err, term->err_val, 863 strdup("invalid branch sample type"), 864 NULL); 865 return -EINVAL; 866 } 867 break; 868 case PARSE_EVENTS__TERM_TYPE_TIME: 869 CHECK_TYPE_VAL(NUM); 870 if (term->val.num > 1) { 871 parse_events_error__handle(err, term->err_val, 872 strdup("expected 0 or 1"), 873 NULL); 874 return -EINVAL; 875 } 876 break; 877 case PARSE_EVENTS__TERM_TYPE_CALLGRAPH: 878 CHECK_TYPE_VAL(STR); 879 break; 880 case PARSE_EVENTS__TERM_TYPE_STACKSIZE: 881 CHECK_TYPE_VAL(NUM); 882 break; 883 case PARSE_EVENTS__TERM_TYPE_INHERIT: 884 CHECK_TYPE_VAL(NUM); 885 break; 886 case PARSE_EVENTS__TERM_TYPE_NOINHERIT: 887 CHECK_TYPE_VAL(NUM); 888 break; 889 case PARSE_EVENTS__TERM_TYPE_OVERWRITE: 890 CHECK_TYPE_VAL(NUM); 891 break; 892 case PARSE_EVENTS__TERM_TYPE_NOOVERWRITE: 893 CHECK_TYPE_VAL(NUM); 894 break; 895 case PARSE_EVENTS__TERM_TYPE_NAME: 896 CHECK_TYPE_VAL(STR); 897 break; 898 case PARSE_EVENTS__TERM_TYPE_METRIC_ID: 899 CHECK_TYPE_VAL(STR); 900 break; 901 case PARSE_EVENTS__TERM_TYPE_RAW: 902 CHECK_TYPE_VAL(STR); 903 break; 904 case PARSE_EVENTS__TERM_TYPE_MAX_STACK: 905 CHECK_TYPE_VAL(NUM); 906 break; 907 case PARSE_EVENTS__TERM_TYPE_MAX_EVENTS: 908 CHECK_TYPE_VAL(NUM); 909 break; 910 case PARSE_EVENTS__TERM_TYPE_PERCORE: 911 CHECK_TYPE_VAL(NUM); 912 if ((unsigned int)term->val.num > 1) { 913 parse_events_error__handle(err, term->err_val, 914 strdup("expected 0 or 1"), 915 NULL); 916 return -EINVAL; 917 } 918 break; 919 case PARSE_EVENTS__TERM_TYPE_AUX_OUTPUT: 920 CHECK_TYPE_VAL(NUM); 921 break; 922 case PARSE_EVENTS__TERM_TYPE_AUX_SAMPLE_SIZE: 923 CHECK_TYPE_VAL(NUM); 924 if (term->val.num > UINT_MAX) { 925 parse_events_error__handle(err, term->err_val, 926 strdup("too big"), 927 NULL); 928 return -EINVAL; 929 } 930 break; 931 default: 932 parse_events_error__handle(err, term->err_term, 933 strdup("unknown term"), 934 parse_events_formats_error_string(NULL)); 935 return -EINVAL; 936 } 937 938 /* 939 * Check term availability after basic checking so 940 * PARSE_EVENTS__TERM_TYPE_USER can be found and filtered. 941 * 942 * If check availability at the entry of this function, 943 * user will see "'<sysfs term>' is not usable in 'perf stat'" 944 * if an invalid config term is provided for legacy events 945 * (for example, instructions/badterm/...), which is confusing. 946 */ 947 if (!config_term_avail(term->type_term, err)) 948 return -EINVAL; 949 return 0; 950 #undef CHECK_TYPE_VAL 951 } 952 953 static int config_term_pmu(struct perf_event_attr *attr, 954 struct parse_events_term *term, 955 struct parse_events_error *err) 956 { 957 if (term->type_term == PARSE_EVENTS__TERM_TYPE_LEGACY_CACHE) { 958 const struct perf_pmu *pmu = perf_pmus__find_by_type(attr->type); 959 960 if (!pmu) { 961 char *err_str; 962 963 if (asprintf(&err_str, "Failed to find PMU for type %d", attr->type) >= 0) 964 parse_events_error__handle(err, term->err_term, 965 err_str, /*help=*/NULL); 966 return -EINVAL; 967 } 968 if (perf_pmu__supports_legacy_cache(pmu)) { 969 attr->type = PERF_TYPE_HW_CACHE; 970 return parse_events__decode_legacy_cache(term->config, pmu->type, 971 &attr->config); 972 } else 973 term->type_term = PARSE_EVENTS__TERM_TYPE_USER; 974 } 975 if (term->type_term == PARSE_EVENTS__TERM_TYPE_HARDWARE) { 976 const struct perf_pmu *pmu = perf_pmus__find_by_type(attr->type); 977 978 if (!pmu) { 979 char *err_str; 980 981 if (asprintf(&err_str, "Failed to find PMU for type %d", attr->type) >= 0) 982 parse_events_error__handle(err, term->err_term, 983 err_str, /*help=*/NULL); 984 return -EINVAL; 985 } 986 attr->type = PERF_TYPE_HARDWARE; 987 attr->config = term->val.num; 988 if (perf_pmus__supports_extended_type()) 989 attr->config |= (__u64)pmu->type << PERF_PMU_TYPE_SHIFT; 990 return 0; 991 } 992 if (term->type_term == PARSE_EVENTS__TERM_TYPE_USER || 993 term->type_term == PARSE_EVENTS__TERM_TYPE_DRV_CFG) { 994 /* 995 * Always succeed for sysfs terms, as we dont know 996 * at this point what type they need to have. 997 */ 998 return 0; 999 } 1000 return config_term_common(attr, term, err); 1001 } 1002 1003 #ifdef HAVE_LIBTRACEEVENT 1004 static int config_term_tracepoint(struct perf_event_attr *attr, 1005 struct parse_events_term *term, 1006 struct parse_events_error *err) 1007 { 1008 switch (term->type_term) { 1009 case PARSE_EVENTS__TERM_TYPE_CALLGRAPH: 1010 case PARSE_EVENTS__TERM_TYPE_STACKSIZE: 1011 case PARSE_EVENTS__TERM_TYPE_INHERIT: 1012 case PARSE_EVENTS__TERM_TYPE_NOINHERIT: 1013 case PARSE_EVENTS__TERM_TYPE_MAX_STACK: 1014 case PARSE_EVENTS__TERM_TYPE_MAX_EVENTS: 1015 case PARSE_EVENTS__TERM_TYPE_OVERWRITE: 1016 case PARSE_EVENTS__TERM_TYPE_NOOVERWRITE: 1017 case PARSE_EVENTS__TERM_TYPE_AUX_OUTPUT: 1018 case PARSE_EVENTS__TERM_TYPE_AUX_SAMPLE_SIZE: 1019 return config_term_common(attr, term, err); 1020 default: 1021 if (err) { 1022 parse_events_error__handle(err, term->err_term, 1023 strdup("unknown term"), 1024 strdup("valid terms: call-graph,stack-size\n")); 1025 } 1026 return -EINVAL; 1027 } 1028 1029 return 0; 1030 } 1031 #endif 1032 1033 static int config_attr(struct perf_event_attr *attr, 1034 struct list_head *head, 1035 struct parse_events_error *err, 1036 config_term_func_t config_term) 1037 { 1038 struct parse_events_term *term; 1039 1040 list_for_each_entry(term, head, list) 1041 if (config_term(attr, term, err)) 1042 return -EINVAL; 1043 1044 return 0; 1045 } 1046 1047 static int get_config_terms(struct list_head *head_config, 1048 struct list_head *head_terms __maybe_unused) 1049 { 1050 #define ADD_CONFIG_TERM(__type, __weak) \ 1051 struct evsel_config_term *__t; \ 1052 \ 1053 __t = zalloc(sizeof(*__t)); \ 1054 if (!__t) \ 1055 return -ENOMEM; \ 1056 \ 1057 INIT_LIST_HEAD(&__t->list); \ 1058 __t->type = EVSEL__CONFIG_TERM_ ## __type; \ 1059 __t->weak = __weak; \ 1060 list_add_tail(&__t->list, head_terms) 1061 1062 #define ADD_CONFIG_TERM_VAL(__type, __name, __val, __weak) \ 1063 do { \ 1064 ADD_CONFIG_TERM(__type, __weak); \ 1065 __t->val.__name = __val; \ 1066 } while (0) 1067 1068 #define ADD_CONFIG_TERM_STR(__type, __val, __weak) \ 1069 do { \ 1070 ADD_CONFIG_TERM(__type, __weak); \ 1071 __t->val.str = strdup(__val); \ 1072 if (!__t->val.str) { \ 1073 zfree(&__t); \ 1074 return -ENOMEM; \ 1075 } \ 1076 __t->free_str = true; \ 1077 } while (0) 1078 1079 struct parse_events_term *term; 1080 1081 list_for_each_entry(term, head_config, list) { 1082 switch (term->type_term) { 1083 case PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD: 1084 ADD_CONFIG_TERM_VAL(PERIOD, period, term->val.num, term->weak); 1085 break; 1086 case PARSE_EVENTS__TERM_TYPE_SAMPLE_FREQ: 1087 ADD_CONFIG_TERM_VAL(FREQ, freq, term->val.num, term->weak); 1088 break; 1089 case PARSE_EVENTS__TERM_TYPE_TIME: 1090 ADD_CONFIG_TERM_VAL(TIME, time, term->val.num, term->weak); 1091 break; 1092 case PARSE_EVENTS__TERM_TYPE_CALLGRAPH: 1093 ADD_CONFIG_TERM_STR(CALLGRAPH, term->val.str, term->weak); 1094 break; 1095 case PARSE_EVENTS__TERM_TYPE_BRANCH_SAMPLE_TYPE: 1096 ADD_CONFIG_TERM_STR(BRANCH, term->val.str, term->weak); 1097 break; 1098 case PARSE_EVENTS__TERM_TYPE_STACKSIZE: 1099 ADD_CONFIG_TERM_VAL(STACK_USER, stack_user, 1100 term->val.num, term->weak); 1101 break; 1102 case PARSE_EVENTS__TERM_TYPE_INHERIT: 1103 ADD_CONFIG_TERM_VAL(INHERIT, inherit, 1104 term->val.num ? 1 : 0, term->weak); 1105 break; 1106 case PARSE_EVENTS__TERM_TYPE_NOINHERIT: 1107 ADD_CONFIG_TERM_VAL(INHERIT, inherit, 1108 term->val.num ? 0 : 1, term->weak); 1109 break; 1110 case PARSE_EVENTS__TERM_TYPE_MAX_STACK: 1111 ADD_CONFIG_TERM_VAL(MAX_STACK, max_stack, 1112 term->val.num, term->weak); 1113 break; 1114 case PARSE_EVENTS__TERM_TYPE_MAX_EVENTS: 1115 ADD_CONFIG_TERM_VAL(MAX_EVENTS, max_events, 1116 term->val.num, term->weak); 1117 break; 1118 case PARSE_EVENTS__TERM_TYPE_OVERWRITE: 1119 ADD_CONFIG_TERM_VAL(OVERWRITE, overwrite, 1120 term->val.num ? 1 : 0, term->weak); 1121 break; 1122 case PARSE_EVENTS__TERM_TYPE_NOOVERWRITE: 1123 ADD_CONFIG_TERM_VAL(OVERWRITE, overwrite, 1124 term->val.num ? 0 : 1, term->weak); 1125 break; 1126 case PARSE_EVENTS__TERM_TYPE_DRV_CFG: 1127 ADD_CONFIG_TERM_STR(DRV_CFG, term->val.str, term->weak); 1128 break; 1129 case PARSE_EVENTS__TERM_TYPE_PERCORE: 1130 ADD_CONFIG_TERM_VAL(PERCORE, percore, 1131 term->val.num ? true : false, term->weak); 1132 break; 1133 case PARSE_EVENTS__TERM_TYPE_AUX_OUTPUT: 1134 ADD_CONFIG_TERM_VAL(AUX_OUTPUT, aux_output, 1135 term->val.num ? 1 : 0, term->weak); 1136 break; 1137 case PARSE_EVENTS__TERM_TYPE_AUX_SAMPLE_SIZE: 1138 ADD_CONFIG_TERM_VAL(AUX_SAMPLE_SIZE, aux_sample_size, 1139 term->val.num, term->weak); 1140 break; 1141 default: 1142 break; 1143 } 1144 } 1145 return 0; 1146 } 1147 1148 /* 1149 * Add EVSEL__CONFIG_TERM_CFG_CHG where cfg_chg will have a bit set for 1150 * each bit of attr->config that the user has changed. 1151 */ 1152 static int get_config_chgs(struct perf_pmu *pmu, struct list_head *head_config, 1153 struct list_head *head_terms) 1154 { 1155 struct parse_events_term *term; 1156 u64 bits = 0; 1157 int type; 1158 1159 list_for_each_entry(term, head_config, list) { 1160 switch (term->type_term) { 1161 case PARSE_EVENTS__TERM_TYPE_USER: 1162 type = perf_pmu__format_type(&pmu->format, term->config); 1163 if (type != PERF_PMU_FORMAT_VALUE_CONFIG) 1164 continue; 1165 bits |= perf_pmu__format_bits(&pmu->format, term->config); 1166 break; 1167 case PARSE_EVENTS__TERM_TYPE_CONFIG: 1168 bits = ~(u64)0; 1169 break; 1170 default: 1171 break; 1172 } 1173 } 1174 1175 if (bits) 1176 ADD_CONFIG_TERM_VAL(CFG_CHG, cfg_chg, bits, false); 1177 1178 #undef ADD_CONFIG_TERM 1179 return 0; 1180 } 1181 1182 int parse_events_add_tracepoint(struct list_head *list, int *idx, 1183 const char *sys, const char *event, 1184 struct parse_events_error *err, 1185 struct list_head *head_config, void *loc_) 1186 { 1187 YYLTYPE *loc = loc_; 1188 #ifdef HAVE_LIBTRACEEVENT 1189 if (head_config) { 1190 struct perf_event_attr attr; 1191 1192 if (config_attr(&attr, head_config, err, 1193 config_term_tracepoint)) 1194 return -EINVAL; 1195 } 1196 1197 if (strpbrk(sys, "*?")) 1198 return add_tracepoint_multi_sys(list, idx, sys, event, 1199 err, head_config, loc); 1200 else 1201 return add_tracepoint_event(list, idx, sys, event, 1202 err, head_config, loc); 1203 #else 1204 (void)list; 1205 (void)idx; 1206 (void)sys; 1207 (void)event; 1208 (void)head_config; 1209 parse_events_error__handle(err, loc->first_column, strdup("unsupported tracepoint"), 1210 strdup("libtraceevent is necessary for tracepoint support")); 1211 return -1; 1212 #endif 1213 } 1214 1215 static int __parse_events_add_numeric(struct parse_events_state *parse_state, 1216 struct list_head *list, 1217 struct perf_pmu *pmu, u32 type, u32 extended_type, 1218 u64 config, struct list_head *head_config) 1219 { 1220 struct perf_event_attr attr; 1221 LIST_HEAD(config_terms); 1222 const char *name, *metric_id; 1223 int ret; 1224 1225 memset(&attr, 0, sizeof(attr)); 1226 attr.type = type; 1227 attr.config = config; 1228 if (extended_type && (type == PERF_TYPE_HARDWARE || type == PERF_TYPE_HW_CACHE)) { 1229 assert(perf_pmus__supports_extended_type()); 1230 attr.config |= (u64)extended_type << PERF_PMU_TYPE_SHIFT; 1231 } 1232 1233 if (head_config) { 1234 if (config_attr(&attr, head_config, parse_state->error, 1235 config_term_common)) 1236 return -EINVAL; 1237 1238 if (get_config_terms(head_config, &config_terms)) 1239 return -ENOMEM; 1240 } 1241 1242 name = get_config_name(head_config); 1243 metric_id = get_config_metric_id(head_config); 1244 ret = __add_event(list, &parse_state->idx, &attr, /*init_attr*/true, name, 1245 metric_id, pmu, &config_terms, /*auto_merge_stats=*/false, 1246 /*cpu_list=*/NULL) ? 0 : -ENOMEM; 1247 free_config_terms(&config_terms); 1248 return ret; 1249 } 1250 1251 int parse_events_add_numeric(struct parse_events_state *parse_state, 1252 struct list_head *list, 1253 u32 type, u64 config, 1254 struct list_head *head_config, 1255 bool wildcard) 1256 { 1257 struct perf_pmu *pmu = NULL; 1258 bool found_supported = false; 1259 1260 /* Wildcards on numeric values are only supported by core PMUs. */ 1261 if (wildcard && perf_pmus__supports_extended_type()) { 1262 while ((pmu = perf_pmus__scan_core(pmu)) != NULL) { 1263 int ret; 1264 1265 found_supported = true; 1266 if (parse_events__filter_pmu(parse_state, pmu)) 1267 continue; 1268 1269 ret = __parse_events_add_numeric(parse_state, list, pmu, 1270 type, pmu->type, 1271 config, head_config); 1272 if (ret) 1273 return ret; 1274 } 1275 if (found_supported) 1276 return 0; 1277 } 1278 return __parse_events_add_numeric(parse_state, list, perf_pmus__find_by_type(type), 1279 type, /*extended_type=*/0, config, head_config); 1280 } 1281 1282 int parse_events_add_tool(struct parse_events_state *parse_state, 1283 struct list_head *list, 1284 int tool_event) 1285 { 1286 return add_event_tool(list, &parse_state->idx, tool_event); 1287 } 1288 1289 static bool config_term_percore(struct list_head *config_terms) 1290 { 1291 struct evsel_config_term *term; 1292 1293 list_for_each_entry(term, config_terms, list) { 1294 if (term->type == EVSEL__CONFIG_TERM_PERCORE) 1295 return term->val.percore; 1296 } 1297 1298 return false; 1299 } 1300 1301 int parse_events_add_pmu(struct parse_events_state *parse_state, 1302 struct list_head *list, char *name, 1303 struct list_head *head_config, 1304 bool auto_merge_stats, void *loc_) 1305 { 1306 struct perf_event_attr attr; 1307 struct perf_pmu_info info; 1308 struct perf_pmu *pmu; 1309 struct evsel *evsel; 1310 struct parse_events_error *err = parse_state->error; 1311 YYLTYPE *loc = loc_; 1312 LIST_HEAD(config_terms); 1313 1314 pmu = parse_state->fake_pmu ?: perf_pmus__find(name); 1315 1316 if (verbose > 1 && !(pmu && pmu->selectable)) { 1317 fprintf(stderr, "Attempting to add event pmu '%s' with '", 1318 name); 1319 if (head_config) { 1320 struct parse_events_term *term; 1321 1322 list_for_each_entry(term, head_config, list) { 1323 fprintf(stderr, "%s,", term->config); 1324 } 1325 } 1326 fprintf(stderr, "' that may result in non-fatal errors\n"); 1327 } 1328 1329 if (!pmu) { 1330 char *err_str; 1331 1332 if (asprintf(&err_str, 1333 "Cannot find PMU `%s'. Missing kernel support?", 1334 name) >= 0) 1335 parse_events_error__handle(err, loc->first_column, err_str, NULL); 1336 return -EINVAL; 1337 } 1338 if (head_config) 1339 fix_raw(head_config, pmu); 1340 1341 if (pmu->default_config) { 1342 memcpy(&attr, pmu->default_config, 1343 sizeof(struct perf_event_attr)); 1344 } else { 1345 memset(&attr, 0, sizeof(attr)); 1346 } 1347 attr.type = pmu->type; 1348 1349 if (!head_config) { 1350 evsel = __add_event(list, &parse_state->idx, &attr, 1351 /*init_attr=*/true, /*name=*/NULL, 1352 /*metric_id=*/NULL, pmu, 1353 /*config_terms=*/NULL, auto_merge_stats, 1354 /*cpu_list=*/NULL); 1355 return evsel ? 0 : -ENOMEM; 1356 } 1357 1358 if (!parse_state->fake_pmu && perf_pmu__check_alias(pmu, head_config, &info)) 1359 return -EINVAL; 1360 1361 if (verbose > 1) { 1362 fprintf(stderr, "After aliases, add event pmu '%s' with '", 1363 name); 1364 if (head_config) { 1365 struct parse_events_term *term; 1366 1367 list_for_each_entry(term, head_config, list) { 1368 fprintf(stderr, "%s,", term->config); 1369 } 1370 } 1371 fprintf(stderr, "' that may result in non-fatal errors\n"); 1372 } 1373 1374 /* 1375 * Configure hardcoded terms first, no need to check 1376 * return value when called with fail == 0 ;) 1377 */ 1378 if (config_attr(&attr, head_config, parse_state->error, config_term_pmu)) 1379 return -EINVAL; 1380 1381 if (get_config_terms(head_config, &config_terms)) 1382 return -ENOMEM; 1383 1384 /* 1385 * When using default config, record which bits of attr->config were 1386 * changed by the user. 1387 */ 1388 if (pmu->default_config && get_config_chgs(pmu, head_config, &config_terms)) 1389 return -ENOMEM; 1390 1391 if (!parse_state->fake_pmu && perf_pmu__config(pmu, &attr, head_config, parse_state->error)) { 1392 free_config_terms(&config_terms); 1393 return -EINVAL; 1394 } 1395 1396 evsel = __add_event(list, &parse_state->idx, &attr, /*init_attr=*/true, 1397 get_config_name(head_config), 1398 get_config_metric_id(head_config), pmu, 1399 &config_terms, auto_merge_stats, /*cpu_list=*/NULL); 1400 if (!evsel) 1401 return -ENOMEM; 1402 1403 if (evsel->name) 1404 evsel->use_config_name = true; 1405 1406 evsel->percore = config_term_percore(&evsel->config_terms); 1407 1408 if (parse_state->fake_pmu) 1409 return 0; 1410 1411 free((char *)evsel->unit); 1412 evsel->unit = strdup(info.unit); 1413 evsel->scale = info.scale; 1414 evsel->per_pkg = info.per_pkg; 1415 evsel->snapshot = info.snapshot; 1416 return 0; 1417 } 1418 1419 int parse_events_multi_pmu_add(struct parse_events_state *parse_state, 1420 char *str, struct list_head *head, 1421 struct list_head **listp, void *loc_) 1422 { 1423 struct parse_events_term *term; 1424 struct list_head *list = NULL; 1425 struct list_head *orig_head = NULL; 1426 struct perf_pmu *pmu = NULL; 1427 YYLTYPE *loc = loc_; 1428 int ok = 0; 1429 char *config; 1430 1431 *listp = NULL; 1432 1433 if (!head) { 1434 head = malloc(sizeof(struct list_head)); 1435 if (!head) 1436 goto out_err; 1437 1438 INIT_LIST_HEAD(head); 1439 } 1440 config = strdup(str); 1441 if (!config) 1442 goto out_err; 1443 1444 if (parse_events_term__num(&term, 1445 PARSE_EVENTS__TERM_TYPE_USER, 1446 config, 1, false, NULL, 1447 NULL) < 0) { 1448 free(config); 1449 goto out_err; 1450 } 1451 list_add_tail(&term->list, head); 1452 1453 /* Add it for all PMUs that support the alias */ 1454 list = malloc(sizeof(struct list_head)); 1455 if (!list) 1456 goto out_err; 1457 1458 INIT_LIST_HEAD(list); 1459 1460 while ((pmu = perf_pmus__scan(pmu)) != NULL) { 1461 struct perf_pmu_alias *alias; 1462 bool auto_merge_stats; 1463 1464 if (parse_events__filter_pmu(parse_state, pmu)) 1465 continue; 1466 1467 auto_merge_stats = perf_pmu__auto_merge_stats(pmu); 1468 1469 list_for_each_entry(alias, &pmu->aliases, list) { 1470 if (!strcasecmp(alias->name, str)) { 1471 parse_events_copy_term_list(head, &orig_head); 1472 if (!parse_events_add_pmu(parse_state, list, 1473 pmu->name, orig_head, 1474 auto_merge_stats, loc)) { 1475 pr_debug("%s -> %s/%s/\n", str, 1476 pmu->name, alias->str); 1477 parse_state->wild_card_pmus = true; 1478 ok++; 1479 } 1480 parse_events_terms__delete(orig_head); 1481 } 1482 } 1483 } 1484 1485 if (parse_state->fake_pmu) { 1486 if (!parse_events_add_pmu(parse_state, list, str, head, 1487 /*auto_merge_stats=*/true, loc)) { 1488 pr_debug("%s -> %s/%s/\n", str, "fake_pmu", str); 1489 ok++; 1490 } 1491 } 1492 1493 out_err: 1494 if (ok) 1495 *listp = list; 1496 else 1497 free(list); 1498 1499 parse_events_terms__delete(head); 1500 return ok ? 0 : -1; 1501 } 1502 1503 int parse_events__modifier_group(struct list_head *list, 1504 char *event_mod) 1505 { 1506 return parse_events__modifier_event(list, event_mod, true); 1507 } 1508 1509 void parse_events__set_leader(char *name, struct list_head *list) 1510 { 1511 struct evsel *leader; 1512 1513 if (list_empty(list)) { 1514 WARN_ONCE(true, "WARNING: failed to set leader: empty list"); 1515 return; 1516 } 1517 1518 leader = list_first_entry(list, struct evsel, core.node); 1519 __perf_evlist__set_leader(list, &leader->core); 1520 leader->group_name = name; 1521 } 1522 1523 /* list_event is assumed to point to malloc'ed memory */ 1524 void parse_events_update_lists(struct list_head *list_event, 1525 struct list_head *list_all) 1526 { 1527 /* 1528 * Called for single event definition. Update the 1529 * 'all event' list, and reinit the 'single event' 1530 * list, for next event definition. 1531 */ 1532 list_splice_tail(list_event, list_all); 1533 free(list_event); 1534 } 1535 1536 struct event_modifier { 1537 int eu; 1538 int ek; 1539 int eh; 1540 int eH; 1541 int eG; 1542 int eI; 1543 int precise; 1544 int precise_max; 1545 int exclude_GH; 1546 int sample_read; 1547 int pinned; 1548 int weak; 1549 int exclusive; 1550 int bpf_counter; 1551 }; 1552 1553 static int get_event_modifier(struct event_modifier *mod, char *str, 1554 struct evsel *evsel) 1555 { 1556 int eu = evsel ? evsel->core.attr.exclude_user : 0; 1557 int ek = evsel ? evsel->core.attr.exclude_kernel : 0; 1558 int eh = evsel ? evsel->core.attr.exclude_hv : 0; 1559 int eH = evsel ? evsel->core.attr.exclude_host : 0; 1560 int eG = evsel ? evsel->core.attr.exclude_guest : 0; 1561 int eI = evsel ? evsel->core.attr.exclude_idle : 0; 1562 int precise = evsel ? evsel->core.attr.precise_ip : 0; 1563 int precise_max = 0; 1564 int sample_read = 0; 1565 int pinned = evsel ? evsel->core.attr.pinned : 0; 1566 int exclusive = evsel ? evsel->core.attr.exclusive : 0; 1567 1568 int exclude = eu | ek | eh; 1569 int exclude_GH = evsel ? evsel->exclude_GH : 0; 1570 int weak = 0; 1571 int bpf_counter = 0; 1572 1573 memset(mod, 0, sizeof(*mod)); 1574 1575 while (*str) { 1576 if (*str == 'u') { 1577 if (!exclude) 1578 exclude = eu = ek = eh = 1; 1579 if (!exclude_GH && !perf_guest) 1580 eG = 1; 1581 eu = 0; 1582 } else if (*str == 'k') { 1583 if (!exclude) 1584 exclude = eu = ek = eh = 1; 1585 ek = 0; 1586 } else if (*str == 'h') { 1587 if (!exclude) 1588 exclude = eu = ek = eh = 1; 1589 eh = 0; 1590 } else if (*str == 'G') { 1591 if (!exclude_GH) 1592 exclude_GH = eG = eH = 1; 1593 eG = 0; 1594 } else if (*str == 'H') { 1595 if (!exclude_GH) 1596 exclude_GH = eG = eH = 1; 1597 eH = 0; 1598 } else if (*str == 'I') { 1599 eI = 1; 1600 } else if (*str == 'p') { 1601 precise++; 1602 /* use of precise requires exclude_guest */ 1603 if (!exclude_GH) 1604 eG = 1; 1605 } else if (*str == 'P') { 1606 precise_max = 1; 1607 } else if (*str == 'S') { 1608 sample_read = 1; 1609 } else if (*str == 'D') { 1610 pinned = 1; 1611 } else if (*str == 'e') { 1612 exclusive = 1; 1613 } else if (*str == 'W') { 1614 weak = 1; 1615 } else if (*str == 'b') { 1616 bpf_counter = 1; 1617 } else 1618 break; 1619 1620 ++str; 1621 } 1622 1623 /* 1624 * precise ip: 1625 * 1626 * 0 - SAMPLE_IP can have arbitrary skid 1627 * 1 - SAMPLE_IP must have constant skid 1628 * 2 - SAMPLE_IP requested to have 0 skid 1629 * 3 - SAMPLE_IP must have 0 skid 1630 * 1631 * See also PERF_RECORD_MISC_EXACT_IP 1632 */ 1633 if (precise > 3) 1634 return -EINVAL; 1635 1636 mod->eu = eu; 1637 mod->ek = ek; 1638 mod->eh = eh; 1639 mod->eH = eH; 1640 mod->eG = eG; 1641 mod->eI = eI; 1642 mod->precise = precise; 1643 mod->precise_max = precise_max; 1644 mod->exclude_GH = exclude_GH; 1645 mod->sample_read = sample_read; 1646 mod->pinned = pinned; 1647 mod->weak = weak; 1648 mod->bpf_counter = bpf_counter; 1649 mod->exclusive = exclusive; 1650 1651 return 0; 1652 } 1653 1654 /* 1655 * Basic modifier sanity check to validate it contains only one 1656 * instance of any modifier (apart from 'p') present. 1657 */ 1658 static int check_modifier(char *str) 1659 { 1660 char *p = str; 1661 1662 /* The sizeof includes 0 byte as well. */ 1663 if (strlen(str) > (sizeof("ukhGHpppPSDIWeb") - 1)) 1664 return -1; 1665 1666 while (*p) { 1667 if (*p != 'p' && strchr(p + 1, *p)) 1668 return -1; 1669 p++; 1670 } 1671 1672 return 0; 1673 } 1674 1675 int parse_events__modifier_event(struct list_head *list, char *str, bool add) 1676 { 1677 struct evsel *evsel; 1678 struct event_modifier mod; 1679 1680 if (str == NULL) 1681 return 0; 1682 1683 if (check_modifier(str)) 1684 return -EINVAL; 1685 1686 if (!add && get_event_modifier(&mod, str, NULL)) 1687 return -EINVAL; 1688 1689 __evlist__for_each_entry(list, evsel) { 1690 if (add && get_event_modifier(&mod, str, evsel)) 1691 return -EINVAL; 1692 1693 evsel->core.attr.exclude_user = mod.eu; 1694 evsel->core.attr.exclude_kernel = mod.ek; 1695 evsel->core.attr.exclude_hv = mod.eh; 1696 evsel->core.attr.precise_ip = mod.precise; 1697 evsel->core.attr.exclude_host = mod.eH; 1698 evsel->core.attr.exclude_guest = mod.eG; 1699 evsel->core.attr.exclude_idle = mod.eI; 1700 evsel->exclude_GH = mod.exclude_GH; 1701 evsel->sample_read = mod.sample_read; 1702 evsel->precise_max = mod.precise_max; 1703 evsel->weak_group = mod.weak; 1704 evsel->bpf_counter = mod.bpf_counter; 1705 1706 if (evsel__is_group_leader(evsel)) { 1707 evsel->core.attr.pinned = mod.pinned; 1708 evsel->core.attr.exclusive = mod.exclusive; 1709 } 1710 } 1711 1712 return 0; 1713 } 1714 1715 int parse_events_name(struct list_head *list, const char *name) 1716 { 1717 struct evsel *evsel; 1718 1719 __evlist__for_each_entry(list, evsel) { 1720 if (!evsel->name) { 1721 evsel->name = strdup(name); 1722 if (!evsel->name) 1723 return -ENOMEM; 1724 } 1725 } 1726 1727 return 0; 1728 } 1729 1730 static int parse_events__scanner(const char *str, 1731 struct parse_events_state *parse_state) 1732 { 1733 YY_BUFFER_STATE buffer; 1734 void *scanner; 1735 int ret; 1736 1737 ret = parse_events_lex_init_extra(parse_state, &scanner); 1738 if (ret) 1739 return ret; 1740 1741 buffer = parse_events__scan_string(str, scanner); 1742 1743 #ifdef PARSER_DEBUG 1744 parse_events_debug = 1; 1745 parse_events_set_debug(1, scanner); 1746 #endif 1747 ret = parse_events_parse(parse_state, scanner); 1748 1749 parse_events__flush_buffer(buffer, scanner); 1750 parse_events__delete_buffer(buffer, scanner); 1751 parse_events_lex_destroy(scanner); 1752 return ret; 1753 } 1754 1755 /* 1756 * parse event config string, return a list of event terms. 1757 */ 1758 int parse_events_terms(struct list_head *terms, const char *str) 1759 { 1760 struct parse_events_state parse_state = { 1761 .terms = NULL, 1762 .stoken = PE_START_TERMS, 1763 }; 1764 int ret; 1765 1766 ret = parse_events__scanner(str, &parse_state); 1767 1768 if (!ret) { 1769 list_splice(parse_state.terms, terms); 1770 zfree(&parse_state.terms); 1771 return 0; 1772 } 1773 1774 parse_events_terms__delete(parse_state.terms); 1775 return ret; 1776 } 1777 1778 static int evsel__compute_group_pmu_name(struct evsel *evsel, 1779 const struct list_head *head) 1780 { 1781 struct evsel *leader = evsel__leader(evsel); 1782 struct evsel *pos; 1783 const char *group_pmu_name; 1784 struct perf_pmu *pmu = evsel__find_pmu(evsel); 1785 1786 if (!pmu) { 1787 /* 1788 * For PERF_TYPE_HARDWARE and PERF_TYPE_HW_CACHE types the PMU 1789 * is a core PMU, but in heterogeneous systems this is 1790 * unknown. For now pick the first core PMU. 1791 */ 1792 pmu = perf_pmus__scan_core(NULL); 1793 } 1794 if (!pmu) { 1795 pr_debug("No PMU found for '%s'\n", evsel__name(evsel)); 1796 return -EINVAL; 1797 } 1798 group_pmu_name = pmu->name; 1799 /* 1800 * Software events may be in a group with other uncore PMU events. Use 1801 * the pmu_name of the first non-software event to avoid breaking the 1802 * software event out of the group. 1803 * 1804 * Aux event leaders, like intel_pt, expect a group with events from 1805 * other PMUs, so substitute the AUX event's PMU in this case. 1806 */ 1807 if (perf_pmu__is_software(pmu) || evsel__is_aux_event(leader)) { 1808 struct perf_pmu *leader_pmu = evsel__find_pmu(leader); 1809 1810 if (!leader_pmu) { 1811 /* As with determining pmu above. */ 1812 leader_pmu = perf_pmus__scan_core(NULL); 1813 } 1814 /* 1815 * Starting with the leader, find the first event with a named 1816 * non-software PMU. for_each_group_(member|evsel) isn't used as 1817 * the list isn't yet sorted putting evsel's in the same group 1818 * together. 1819 */ 1820 if (leader_pmu && !perf_pmu__is_software(leader_pmu)) { 1821 group_pmu_name = leader_pmu->name; 1822 } else if (leader->core.nr_members > 1) { 1823 list_for_each_entry(pos, head, core.node) { 1824 struct perf_pmu *pos_pmu; 1825 1826 if (pos == leader || evsel__leader(pos) != leader) 1827 continue; 1828 pos_pmu = evsel__find_pmu(pos); 1829 if (!pos_pmu) { 1830 /* As with determining pmu above. */ 1831 pos_pmu = perf_pmus__scan_core(NULL); 1832 } 1833 if (pos_pmu && !perf_pmu__is_software(pos_pmu)) { 1834 group_pmu_name = pos_pmu->name; 1835 break; 1836 } 1837 } 1838 } 1839 } 1840 /* Assign the actual name taking care that the fake PMU lacks a name. */ 1841 evsel->group_pmu_name = strdup(group_pmu_name ?: "fake"); 1842 return evsel->group_pmu_name ? 0 : -ENOMEM; 1843 } 1844 1845 __weak int arch_evlist__cmp(const struct evsel *lhs, const struct evsel *rhs) 1846 { 1847 /* Order by insertion index. */ 1848 return lhs->core.idx - rhs->core.idx; 1849 } 1850 1851 static int evlist__cmp(void *_fg_idx, const struct list_head *l, const struct list_head *r) 1852 { 1853 const struct perf_evsel *lhs_core = container_of(l, struct perf_evsel, node); 1854 const struct evsel *lhs = container_of(lhs_core, struct evsel, core); 1855 const struct perf_evsel *rhs_core = container_of(r, struct perf_evsel, node); 1856 const struct evsel *rhs = container_of(rhs_core, struct evsel, core); 1857 int *force_grouped_idx = _fg_idx; 1858 int lhs_sort_idx, rhs_sort_idx, ret; 1859 const char *lhs_pmu_name, *rhs_pmu_name; 1860 bool lhs_has_group, rhs_has_group; 1861 1862 /* 1863 * First sort by grouping/leader. Read the leader idx only if the evsel 1864 * is part of a group, by default ungrouped events will be sorted 1865 * relative to grouped events based on where the first ungrouped event 1866 * occurs. If both events don't have a group we want to fall-through to 1867 * the arch specific sorting, that can reorder and fix things like 1868 * Intel's topdown events. 1869 */ 1870 if (lhs_core->leader != lhs_core || lhs_core->nr_members > 1) { 1871 lhs_has_group = true; 1872 lhs_sort_idx = lhs_core->leader->idx; 1873 } else { 1874 lhs_has_group = false; 1875 lhs_sort_idx = *force_grouped_idx != -1 && arch_evsel__must_be_in_group(lhs) 1876 ? *force_grouped_idx 1877 : lhs_core->idx; 1878 } 1879 if (rhs_core->leader != rhs_core || rhs_core->nr_members > 1) { 1880 rhs_has_group = true; 1881 rhs_sort_idx = rhs_core->leader->idx; 1882 } else { 1883 rhs_has_group = false; 1884 rhs_sort_idx = *force_grouped_idx != -1 && arch_evsel__must_be_in_group(rhs) 1885 ? *force_grouped_idx 1886 : rhs_core->idx; 1887 } 1888 1889 if (lhs_sort_idx != rhs_sort_idx) 1890 return lhs_sort_idx - rhs_sort_idx; 1891 1892 /* Group by PMU if there is a group. Groups can't span PMUs. */ 1893 if (lhs_has_group && rhs_has_group) { 1894 lhs_pmu_name = lhs->group_pmu_name; 1895 rhs_pmu_name = rhs->group_pmu_name; 1896 ret = strcmp(lhs_pmu_name, rhs_pmu_name); 1897 if (ret) 1898 return ret; 1899 } 1900 1901 /* Architecture specific sorting. */ 1902 return arch_evlist__cmp(lhs, rhs); 1903 } 1904 1905 static int parse_events__sort_events_and_fix_groups(struct list_head *list) 1906 { 1907 int idx = 0, force_grouped_idx = -1; 1908 struct evsel *pos, *cur_leader = NULL; 1909 struct perf_evsel *cur_leaders_grp = NULL; 1910 bool idx_changed = false, cur_leader_force_grouped = false; 1911 int orig_num_leaders = 0, num_leaders = 0; 1912 int ret; 1913 1914 /* 1915 * Compute index to insert ungrouped events at. Place them where the 1916 * first ungrouped event appears. 1917 */ 1918 list_for_each_entry(pos, list, core.node) { 1919 const struct evsel *pos_leader = evsel__leader(pos); 1920 1921 ret = evsel__compute_group_pmu_name(pos, list); 1922 if (ret) 1923 return ret; 1924 1925 if (pos == pos_leader) 1926 orig_num_leaders++; 1927 1928 /* 1929 * Ensure indexes are sequential, in particular for multiple 1930 * event lists being merged. The indexes are used to detect when 1931 * the user order is modified. 1932 */ 1933 pos->core.idx = idx++; 1934 1935 /* Remember an index to sort all forced grouped events together to. */ 1936 if (force_grouped_idx == -1 && pos == pos_leader && pos->core.nr_members < 2 && 1937 arch_evsel__must_be_in_group(pos)) 1938 force_grouped_idx = pos->core.idx; 1939 } 1940 1941 /* Sort events. */ 1942 list_sort(&force_grouped_idx, list, evlist__cmp); 1943 1944 /* 1945 * Recompute groups, splitting for PMUs and adding groups for events 1946 * that require them. 1947 */ 1948 idx = 0; 1949 list_for_each_entry(pos, list, core.node) { 1950 const struct evsel *pos_leader = evsel__leader(pos); 1951 const char *pos_pmu_name = pos->group_pmu_name; 1952 const char *cur_leader_pmu_name; 1953 bool pos_force_grouped = force_grouped_idx != -1 && 1954 arch_evsel__must_be_in_group(pos); 1955 1956 /* Reset index and nr_members. */ 1957 if (pos->core.idx != idx) 1958 idx_changed = true; 1959 pos->core.idx = idx++; 1960 pos->core.nr_members = 0; 1961 1962 /* 1963 * Set the group leader respecting the given groupings and that 1964 * groups can't span PMUs. 1965 */ 1966 if (!cur_leader) 1967 cur_leader = pos; 1968 1969 cur_leader_pmu_name = cur_leader->group_pmu_name; 1970 if ((cur_leaders_grp != pos->core.leader && 1971 (!pos_force_grouped || !cur_leader_force_grouped)) || 1972 strcmp(cur_leader_pmu_name, pos_pmu_name)) { 1973 /* Event is for a different group/PMU than last. */ 1974 cur_leader = pos; 1975 /* 1976 * Remember the leader's group before it is overwritten, 1977 * so that later events match as being in the same 1978 * group. 1979 */ 1980 cur_leaders_grp = pos->core.leader; 1981 /* 1982 * Avoid forcing events into groups with events that 1983 * don't need to be in the group. 1984 */ 1985 cur_leader_force_grouped = pos_force_grouped; 1986 } 1987 if (pos_leader != cur_leader) { 1988 /* The leader changed so update it. */ 1989 evsel__set_leader(pos, cur_leader); 1990 } 1991 } 1992 list_for_each_entry(pos, list, core.node) { 1993 struct evsel *pos_leader = evsel__leader(pos); 1994 1995 if (pos == pos_leader) 1996 num_leaders++; 1997 pos_leader->core.nr_members++; 1998 } 1999 return (idx_changed || num_leaders != orig_num_leaders) ? 1 : 0; 2000 } 2001 2002 int __parse_events(struct evlist *evlist, const char *str, const char *pmu_filter, 2003 struct parse_events_error *err, struct perf_pmu *fake_pmu, 2004 bool warn_if_reordered) 2005 { 2006 struct parse_events_state parse_state = { 2007 .list = LIST_HEAD_INIT(parse_state.list), 2008 .idx = evlist->core.nr_entries, 2009 .error = err, 2010 .stoken = PE_START_EVENTS, 2011 .fake_pmu = fake_pmu, 2012 .pmu_filter = pmu_filter, 2013 .match_legacy_cache_terms = true, 2014 }; 2015 int ret, ret2; 2016 2017 ret = parse_events__scanner(str, &parse_state); 2018 2019 if (!ret && list_empty(&parse_state.list)) { 2020 WARN_ONCE(true, "WARNING: event parser found nothing\n"); 2021 return -1; 2022 } 2023 2024 ret2 = parse_events__sort_events_and_fix_groups(&parse_state.list); 2025 if (ret2 < 0) 2026 return ret; 2027 2028 if (ret2 && warn_if_reordered && !parse_state.wild_card_pmus) 2029 pr_warning("WARNING: events were regrouped to match PMUs\n"); 2030 2031 /* 2032 * Add list to the evlist even with errors to allow callers to clean up. 2033 */ 2034 evlist__splice_list_tail(evlist, &parse_state.list); 2035 2036 if (!ret) { 2037 struct evsel *last; 2038 2039 last = evlist__last(evlist); 2040 last->cmdline_group_boundary = true; 2041 2042 return 0; 2043 } 2044 2045 /* 2046 * There are 2 users - builtin-record and builtin-test objects. 2047 * Both call evlist__delete in case of error, so we dont 2048 * need to bother. 2049 */ 2050 return ret; 2051 } 2052 2053 int parse_event(struct evlist *evlist, const char *str) 2054 { 2055 struct parse_events_error err; 2056 int ret; 2057 2058 parse_events_error__init(&err); 2059 ret = parse_events(evlist, str, &err); 2060 parse_events_error__exit(&err); 2061 return ret; 2062 } 2063 2064 void parse_events_error__init(struct parse_events_error *err) 2065 { 2066 bzero(err, sizeof(*err)); 2067 } 2068 2069 void parse_events_error__exit(struct parse_events_error *err) 2070 { 2071 zfree(&err->str); 2072 zfree(&err->help); 2073 zfree(&err->first_str); 2074 zfree(&err->first_help); 2075 } 2076 2077 void parse_events_error__handle(struct parse_events_error *err, int idx, 2078 char *str, char *help) 2079 { 2080 if (WARN(!str || !err, "WARNING: failed to provide error string or struct\n")) 2081 goto out_free; 2082 switch (err->num_errors) { 2083 case 0: 2084 err->idx = idx; 2085 err->str = str; 2086 err->help = help; 2087 break; 2088 case 1: 2089 err->first_idx = err->idx; 2090 err->idx = idx; 2091 err->first_str = err->str; 2092 err->str = str; 2093 err->first_help = err->help; 2094 err->help = help; 2095 break; 2096 default: 2097 pr_debug("Multiple errors dropping message: %s (%s)\n", 2098 err->str, err->help); 2099 free(err->str); 2100 err->str = str; 2101 free(err->help); 2102 err->help = help; 2103 break; 2104 } 2105 err->num_errors++; 2106 return; 2107 2108 out_free: 2109 free(str); 2110 free(help); 2111 } 2112 2113 #define MAX_WIDTH 1000 2114 static int get_term_width(void) 2115 { 2116 struct winsize ws; 2117 2118 get_term_dimensions(&ws); 2119 return ws.ws_col > MAX_WIDTH ? MAX_WIDTH : ws.ws_col; 2120 } 2121 2122 static void __parse_events_error__print(int err_idx, const char *err_str, 2123 const char *err_help, const char *event) 2124 { 2125 const char *str = "invalid or unsupported event: "; 2126 char _buf[MAX_WIDTH]; 2127 char *buf = (char *) event; 2128 int idx = 0; 2129 if (err_str) { 2130 /* -2 for extra '' in the final fprintf */ 2131 int width = get_term_width() - 2; 2132 int len_event = strlen(event); 2133 int len_str, max_len, cut = 0; 2134 2135 /* 2136 * Maximum error index indent, we will cut 2137 * the event string if it's bigger. 2138 */ 2139 int max_err_idx = 13; 2140 2141 /* 2142 * Let's be specific with the message when 2143 * we have the precise error. 2144 */ 2145 str = "event syntax error: "; 2146 len_str = strlen(str); 2147 max_len = width - len_str; 2148 2149 buf = _buf; 2150 2151 /* We're cutting from the beginning. */ 2152 if (err_idx > max_err_idx) 2153 cut = err_idx - max_err_idx; 2154 2155 strncpy(buf, event + cut, max_len); 2156 2157 /* Mark cut parts with '..' on both sides. */ 2158 if (cut) 2159 buf[0] = buf[1] = '.'; 2160 2161 if ((len_event - cut) > max_len) { 2162 buf[max_len - 1] = buf[max_len - 2] = '.'; 2163 buf[max_len] = 0; 2164 } 2165 2166 idx = len_str + err_idx - cut; 2167 } 2168 2169 fprintf(stderr, "%s'%s'\n", str, buf); 2170 if (idx) { 2171 fprintf(stderr, "%*s\\___ %s\n", idx + 1, "", err_str); 2172 if (err_help) 2173 fprintf(stderr, "\n%s\n", err_help); 2174 } 2175 } 2176 2177 void parse_events_error__print(struct parse_events_error *err, 2178 const char *event) 2179 { 2180 if (!err->num_errors) 2181 return; 2182 2183 __parse_events_error__print(err->idx, err->str, err->help, event); 2184 2185 if (err->num_errors > 1) { 2186 fputs("\nInitial error:\n", stderr); 2187 __parse_events_error__print(err->first_idx, err->first_str, 2188 err->first_help, event); 2189 } 2190 } 2191 2192 #undef MAX_WIDTH 2193 2194 int parse_events_option(const struct option *opt, const char *str, 2195 int unset __maybe_unused) 2196 { 2197 struct parse_events_option_args *args = opt->value; 2198 struct parse_events_error err; 2199 int ret; 2200 2201 parse_events_error__init(&err); 2202 ret = __parse_events(*args->evlistp, str, args->pmu_filter, &err, 2203 /*fake_pmu=*/NULL, /*warn_if_reordered=*/true); 2204 2205 if (ret) { 2206 parse_events_error__print(&err, str); 2207 fprintf(stderr, "Run 'perf list' for a list of valid events\n"); 2208 } 2209 parse_events_error__exit(&err); 2210 2211 return ret; 2212 } 2213 2214 int parse_events_option_new_evlist(const struct option *opt, const char *str, int unset) 2215 { 2216 struct parse_events_option_args *args = opt->value; 2217 int ret; 2218 2219 if (*args->evlistp == NULL) { 2220 *args->evlistp = evlist__new(); 2221 2222 if (*args->evlistp == NULL) { 2223 fprintf(stderr, "Not enough memory to create evlist\n"); 2224 return -1; 2225 } 2226 } 2227 ret = parse_events_option(opt, str, unset); 2228 if (ret) { 2229 evlist__delete(*args->evlistp); 2230 *args->evlistp = NULL; 2231 } 2232 2233 return ret; 2234 } 2235 2236 static int 2237 foreach_evsel_in_last_glob(struct evlist *evlist, 2238 int (*func)(struct evsel *evsel, 2239 const void *arg), 2240 const void *arg) 2241 { 2242 struct evsel *last = NULL; 2243 int err; 2244 2245 /* 2246 * Don't return when list_empty, give func a chance to report 2247 * error when it found last == NULL. 2248 * 2249 * So no need to WARN here, let *func do this. 2250 */ 2251 if (evlist->core.nr_entries > 0) 2252 last = evlist__last(evlist); 2253 2254 do { 2255 err = (*func)(last, arg); 2256 if (err) 2257 return -1; 2258 if (!last) 2259 return 0; 2260 2261 if (last->core.node.prev == &evlist->core.entries) 2262 return 0; 2263 last = list_entry(last->core.node.prev, struct evsel, core.node); 2264 } while (!last->cmdline_group_boundary); 2265 2266 return 0; 2267 } 2268 2269 static int set_filter(struct evsel *evsel, const void *arg) 2270 { 2271 const char *str = arg; 2272 bool found = false; 2273 int nr_addr_filters = 0; 2274 struct perf_pmu *pmu = NULL; 2275 2276 if (evsel == NULL) { 2277 fprintf(stderr, 2278 "--filter option should follow a -e tracepoint or HW tracer option\n"); 2279 return -1; 2280 } 2281 2282 if (evsel->core.attr.type == PERF_TYPE_TRACEPOINT) { 2283 if (evsel__append_tp_filter(evsel, str) < 0) { 2284 fprintf(stderr, 2285 "not enough memory to hold filter string\n"); 2286 return -1; 2287 } 2288 2289 return 0; 2290 } 2291 2292 while ((pmu = perf_pmus__scan(pmu)) != NULL) 2293 if (pmu->type == evsel->core.attr.type) { 2294 found = true; 2295 break; 2296 } 2297 2298 if (found) 2299 perf_pmu__scan_file(pmu, "nr_addr_filters", 2300 "%d", &nr_addr_filters); 2301 2302 if (!nr_addr_filters) 2303 return perf_bpf_filter__parse(&evsel->bpf_filters, str); 2304 2305 if (evsel__append_addr_filter(evsel, str) < 0) { 2306 fprintf(stderr, 2307 "not enough memory to hold filter string\n"); 2308 return -1; 2309 } 2310 2311 return 0; 2312 } 2313 2314 int parse_filter(const struct option *opt, const char *str, 2315 int unset __maybe_unused) 2316 { 2317 struct evlist *evlist = *(struct evlist **)opt->value; 2318 2319 return foreach_evsel_in_last_glob(evlist, set_filter, 2320 (const void *)str); 2321 } 2322 2323 static int add_exclude_perf_filter(struct evsel *evsel, 2324 const void *arg __maybe_unused) 2325 { 2326 char new_filter[64]; 2327 2328 if (evsel == NULL || evsel->core.attr.type != PERF_TYPE_TRACEPOINT) { 2329 fprintf(stderr, 2330 "--exclude-perf option should follow a -e tracepoint option\n"); 2331 return -1; 2332 } 2333 2334 snprintf(new_filter, sizeof(new_filter), "common_pid != %d", getpid()); 2335 2336 if (evsel__append_tp_filter(evsel, new_filter) < 0) { 2337 fprintf(stderr, 2338 "not enough memory to hold filter string\n"); 2339 return -1; 2340 } 2341 2342 return 0; 2343 } 2344 2345 int exclude_perf(const struct option *opt, 2346 const char *arg __maybe_unused, 2347 int unset __maybe_unused) 2348 { 2349 struct evlist *evlist = *(struct evlist **)opt->value; 2350 2351 return foreach_evsel_in_last_glob(evlist, add_exclude_perf_filter, 2352 NULL); 2353 } 2354 2355 int parse_events__is_hardcoded_term(struct parse_events_term *term) 2356 { 2357 return term->type_term != PARSE_EVENTS__TERM_TYPE_USER; 2358 } 2359 2360 static int new_term(struct parse_events_term **_term, 2361 struct parse_events_term *temp, 2362 char *str, u64 num) 2363 { 2364 struct parse_events_term *term; 2365 2366 term = malloc(sizeof(*term)); 2367 if (!term) 2368 return -ENOMEM; 2369 2370 *term = *temp; 2371 INIT_LIST_HEAD(&term->list); 2372 term->weak = false; 2373 2374 switch (term->type_val) { 2375 case PARSE_EVENTS__TERM_TYPE_NUM: 2376 term->val.num = num; 2377 break; 2378 case PARSE_EVENTS__TERM_TYPE_STR: 2379 term->val.str = str; 2380 break; 2381 default: 2382 free(term); 2383 return -EINVAL; 2384 } 2385 2386 *_term = term; 2387 return 0; 2388 } 2389 2390 int parse_events_term__num(struct parse_events_term **term, 2391 int type_term, char *config, u64 num, 2392 bool no_value, 2393 void *loc_term_, void *loc_val_) 2394 { 2395 YYLTYPE *loc_term = loc_term_; 2396 YYLTYPE *loc_val = loc_val_; 2397 2398 struct parse_events_term temp = { 2399 .type_val = PARSE_EVENTS__TERM_TYPE_NUM, 2400 .type_term = type_term, 2401 .config = config ? : strdup(config_term_names[type_term]), 2402 .no_value = no_value, 2403 .err_term = loc_term ? loc_term->first_column : 0, 2404 .err_val = loc_val ? loc_val->first_column : 0, 2405 }; 2406 2407 return new_term(term, &temp, NULL, num); 2408 } 2409 2410 int parse_events_term__str(struct parse_events_term **term, 2411 int type_term, char *config, char *str, 2412 void *loc_term_, void *loc_val_) 2413 { 2414 YYLTYPE *loc_term = loc_term_; 2415 YYLTYPE *loc_val = loc_val_; 2416 2417 struct parse_events_term temp = { 2418 .type_val = PARSE_EVENTS__TERM_TYPE_STR, 2419 .type_term = type_term, 2420 .config = config, 2421 .err_term = loc_term ? loc_term->first_column : 0, 2422 .err_val = loc_val ? loc_val->first_column : 0, 2423 }; 2424 2425 return new_term(term, &temp, str, 0); 2426 } 2427 2428 int parse_events_term__term(struct parse_events_term **term, 2429 int term_lhs, int term_rhs, 2430 void *loc_term, void *loc_val) 2431 { 2432 return parse_events_term__str(term, term_lhs, NULL, 2433 strdup(config_term_names[term_rhs]), 2434 loc_term, loc_val); 2435 } 2436 2437 int parse_events_term__clone(struct parse_events_term **new, 2438 struct parse_events_term *term) 2439 { 2440 char *str; 2441 struct parse_events_term temp = { 2442 .type_val = term->type_val, 2443 .type_term = term->type_term, 2444 .config = NULL, 2445 .err_term = term->err_term, 2446 .err_val = term->err_val, 2447 }; 2448 2449 if (term->config) { 2450 temp.config = strdup(term->config); 2451 if (!temp.config) 2452 return -ENOMEM; 2453 } 2454 if (term->type_val == PARSE_EVENTS__TERM_TYPE_NUM) 2455 return new_term(new, &temp, NULL, term->val.num); 2456 2457 str = strdup(term->val.str); 2458 if (!str) 2459 return -ENOMEM; 2460 return new_term(new, &temp, str, 0); 2461 } 2462 2463 void parse_events_term__delete(struct parse_events_term *term) 2464 { 2465 if (term->type_val != PARSE_EVENTS__TERM_TYPE_NUM) 2466 zfree(&term->val.str); 2467 2468 zfree(&term->config); 2469 free(term); 2470 } 2471 2472 int parse_events_copy_term_list(struct list_head *old, 2473 struct list_head **new) 2474 { 2475 struct parse_events_term *term, *n; 2476 int ret; 2477 2478 if (!old) { 2479 *new = NULL; 2480 return 0; 2481 } 2482 2483 *new = malloc(sizeof(struct list_head)); 2484 if (!*new) 2485 return -ENOMEM; 2486 INIT_LIST_HEAD(*new); 2487 2488 list_for_each_entry (term, old, list) { 2489 ret = parse_events_term__clone(&n, term); 2490 if (ret) 2491 return ret; 2492 list_add_tail(&n->list, *new); 2493 } 2494 return 0; 2495 } 2496 2497 void parse_events_terms__purge(struct list_head *terms) 2498 { 2499 struct parse_events_term *term, *h; 2500 2501 list_for_each_entry_safe(term, h, terms, list) { 2502 list_del_init(&term->list); 2503 parse_events_term__delete(term); 2504 } 2505 } 2506 2507 void parse_events_terms__delete(struct list_head *terms) 2508 { 2509 if (!terms) 2510 return; 2511 parse_events_terms__purge(terms); 2512 free(terms); 2513 } 2514 2515 void parse_events_evlist_error(struct parse_events_state *parse_state, 2516 int idx, const char *str) 2517 { 2518 if (!parse_state->error) 2519 return; 2520 2521 parse_events_error__handle(parse_state->error, idx, strdup(str), NULL); 2522 } 2523 2524 static void config_terms_list(char *buf, size_t buf_sz) 2525 { 2526 int i; 2527 bool first = true; 2528 2529 buf[0] = '\0'; 2530 for (i = 0; i < __PARSE_EVENTS__TERM_TYPE_NR; i++) { 2531 const char *name = config_term_names[i]; 2532 2533 if (!config_term_avail(i, NULL)) 2534 continue; 2535 if (!name) 2536 continue; 2537 if (name[0] == '<') 2538 continue; 2539 2540 if (strlen(buf) + strlen(name) + 2 >= buf_sz) 2541 return; 2542 2543 if (!first) 2544 strcat(buf, ","); 2545 else 2546 first = false; 2547 strcat(buf, name); 2548 } 2549 } 2550 2551 /* 2552 * Return string contains valid config terms of an event. 2553 * @additional_terms: For terms such as PMU sysfs terms. 2554 */ 2555 char *parse_events_formats_error_string(char *additional_terms) 2556 { 2557 char *str; 2558 /* "no-overwrite" is the longest name */ 2559 char static_terms[__PARSE_EVENTS__TERM_TYPE_NR * 2560 (sizeof("no-overwrite") - 1)]; 2561 2562 config_terms_list(static_terms, sizeof(static_terms)); 2563 /* valid terms */ 2564 if (additional_terms) { 2565 if (asprintf(&str, "valid terms: %s,%s", 2566 additional_terms, static_terms) < 0) 2567 goto fail; 2568 } else { 2569 if (asprintf(&str, "valid terms: %s", static_terms) < 0) 2570 goto fail; 2571 } 2572 return str; 2573 2574 fail: 2575 return NULL; 2576 } 2577