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