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