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