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