1 #include <linux/hw_breakpoint.h> 2 #include <linux/err.h> 3 #include "util.h" 4 #include "../perf.h" 5 #include "evlist.h" 6 #include "evsel.h" 7 #include "parse-options.h" 8 #include "parse-events.h" 9 #include "exec_cmd.h" 10 #include "string.h" 11 #include "symbol.h" 12 #include "cache.h" 13 #include "header.h" 14 #include "debug.h" 15 #include <api/fs/tracing_path.h> 16 #include "parse-events-bison.h" 17 #define YY_EXTRA_TYPE int 18 #include "parse-events-flex.h" 19 #include "pmu.h" 20 #include "thread_map.h" 21 #include "cpumap.h" 22 #include "asm/bug.h" 23 24 #define MAX_NAME_LEN 100 25 26 #ifdef PARSER_DEBUG 27 extern int parse_events_debug; 28 #endif 29 int parse_events_parse(void *data, void *scanner); 30 static int get_config_terms(struct list_head *head_config, 31 struct list_head *head_terms __maybe_unused); 32 33 static struct perf_pmu_event_symbol *perf_pmu_events_list; 34 /* 35 * The variable indicates the number of supported pmu event symbols. 36 * 0 means not initialized and ready to init 37 * -1 means failed to init, don't try anymore 38 * >0 is the number of supported pmu event symbols 39 */ 40 static int perf_pmu_events_list_num; 41 42 struct event_symbol event_symbols_hw[PERF_COUNT_HW_MAX] = { 43 [PERF_COUNT_HW_CPU_CYCLES] = { 44 .symbol = "cpu-cycles", 45 .alias = "cycles", 46 }, 47 [PERF_COUNT_HW_INSTRUCTIONS] = { 48 .symbol = "instructions", 49 .alias = "", 50 }, 51 [PERF_COUNT_HW_CACHE_REFERENCES] = { 52 .symbol = "cache-references", 53 .alias = "", 54 }, 55 [PERF_COUNT_HW_CACHE_MISSES] = { 56 .symbol = "cache-misses", 57 .alias = "", 58 }, 59 [PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = { 60 .symbol = "branch-instructions", 61 .alias = "branches", 62 }, 63 [PERF_COUNT_HW_BRANCH_MISSES] = { 64 .symbol = "branch-misses", 65 .alias = "", 66 }, 67 [PERF_COUNT_HW_BUS_CYCLES] = { 68 .symbol = "bus-cycles", 69 .alias = "", 70 }, 71 [PERF_COUNT_HW_STALLED_CYCLES_FRONTEND] = { 72 .symbol = "stalled-cycles-frontend", 73 .alias = "idle-cycles-frontend", 74 }, 75 [PERF_COUNT_HW_STALLED_CYCLES_BACKEND] = { 76 .symbol = "stalled-cycles-backend", 77 .alias = "idle-cycles-backend", 78 }, 79 [PERF_COUNT_HW_REF_CPU_CYCLES] = { 80 .symbol = "ref-cycles", 81 .alias = "", 82 }, 83 }; 84 85 struct event_symbol event_symbols_sw[PERF_COUNT_SW_MAX] = { 86 [PERF_COUNT_SW_CPU_CLOCK] = { 87 .symbol = "cpu-clock", 88 .alias = "", 89 }, 90 [PERF_COUNT_SW_TASK_CLOCK] = { 91 .symbol = "task-clock", 92 .alias = "", 93 }, 94 [PERF_COUNT_SW_PAGE_FAULTS] = { 95 .symbol = "page-faults", 96 .alias = "faults", 97 }, 98 [PERF_COUNT_SW_CONTEXT_SWITCHES] = { 99 .symbol = "context-switches", 100 .alias = "cs", 101 }, 102 [PERF_COUNT_SW_CPU_MIGRATIONS] = { 103 .symbol = "cpu-migrations", 104 .alias = "migrations", 105 }, 106 [PERF_COUNT_SW_PAGE_FAULTS_MIN] = { 107 .symbol = "minor-faults", 108 .alias = "", 109 }, 110 [PERF_COUNT_SW_PAGE_FAULTS_MAJ] = { 111 .symbol = "major-faults", 112 .alias = "", 113 }, 114 [PERF_COUNT_SW_ALIGNMENT_FAULTS] = { 115 .symbol = "alignment-faults", 116 .alias = "", 117 }, 118 [PERF_COUNT_SW_EMULATION_FAULTS] = { 119 .symbol = "emulation-faults", 120 .alias = "", 121 }, 122 [PERF_COUNT_SW_DUMMY] = { 123 .symbol = "dummy", 124 .alias = "", 125 }, 126 }; 127 128 #define __PERF_EVENT_FIELD(config, name) \ 129 ((config & PERF_EVENT_##name##_MASK) >> PERF_EVENT_##name##_SHIFT) 130 131 #define PERF_EVENT_RAW(config) __PERF_EVENT_FIELD(config, RAW) 132 #define PERF_EVENT_CONFIG(config) __PERF_EVENT_FIELD(config, CONFIG) 133 #define PERF_EVENT_TYPE(config) __PERF_EVENT_FIELD(config, TYPE) 134 #define PERF_EVENT_ID(config) __PERF_EVENT_FIELD(config, EVENT) 135 136 #define for_each_subsystem(sys_dir, sys_dirent, sys_next) \ 137 while (!readdir_r(sys_dir, &sys_dirent, &sys_next) && sys_next) \ 138 if (sys_dirent.d_type == DT_DIR && \ 139 (strcmp(sys_dirent.d_name, ".")) && \ 140 (strcmp(sys_dirent.d_name, ".."))) 141 142 static int tp_event_has_id(struct dirent *sys_dir, struct dirent *evt_dir) 143 { 144 char evt_path[MAXPATHLEN]; 145 int fd; 146 147 snprintf(evt_path, MAXPATHLEN, "%s/%s/%s/id", tracing_events_path, 148 sys_dir->d_name, evt_dir->d_name); 149 fd = open(evt_path, O_RDONLY); 150 if (fd < 0) 151 return -EINVAL; 152 close(fd); 153 154 return 0; 155 } 156 157 #define for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next) \ 158 while (!readdir_r(evt_dir, &evt_dirent, &evt_next) && evt_next) \ 159 if (evt_dirent.d_type == DT_DIR && \ 160 (strcmp(evt_dirent.d_name, ".")) && \ 161 (strcmp(evt_dirent.d_name, "..")) && \ 162 (!tp_event_has_id(&sys_dirent, &evt_dirent))) 163 164 #define MAX_EVENT_LENGTH 512 165 166 167 struct tracepoint_path *tracepoint_id_to_path(u64 config) 168 { 169 struct tracepoint_path *path = NULL; 170 DIR *sys_dir, *evt_dir; 171 struct dirent *sys_next, *evt_next, sys_dirent, evt_dirent; 172 char id_buf[24]; 173 int fd; 174 u64 id; 175 char evt_path[MAXPATHLEN]; 176 char dir_path[MAXPATHLEN]; 177 178 sys_dir = opendir(tracing_events_path); 179 if (!sys_dir) 180 return NULL; 181 182 for_each_subsystem(sys_dir, sys_dirent, sys_next) { 183 184 snprintf(dir_path, MAXPATHLEN, "%s/%s", tracing_events_path, 185 sys_dirent.d_name); 186 evt_dir = opendir(dir_path); 187 if (!evt_dir) 188 continue; 189 190 for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next) { 191 192 snprintf(evt_path, MAXPATHLEN, "%s/%s/id", dir_path, 193 evt_dirent.d_name); 194 fd = open(evt_path, O_RDONLY); 195 if (fd < 0) 196 continue; 197 if (read(fd, id_buf, sizeof(id_buf)) < 0) { 198 close(fd); 199 continue; 200 } 201 close(fd); 202 id = atoll(id_buf); 203 if (id == config) { 204 closedir(evt_dir); 205 closedir(sys_dir); 206 path = zalloc(sizeof(*path)); 207 path->system = malloc(MAX_EVENT_LENGTH); 208 if (!path->system) { 209 free(path); 210 return NULL; 211 } 212 path->name = malloc(MAX_EVENT_LENGTH); 213 if (!path->name) { 214 zfree(&path->system); 215 free(path); 216 return NULL; 217 } 218 strncpy(path->system, sys_dirent.d_name, 219 MAX_EVENT_LENGTH); 220 strncpy(path->name, evt_dirent.d_name, 221 MAX_EVENT_LENGTH); 222 return path; 223 } 224 } 225 closedir(evt_dir); 226 } 227 228 closedir(sys_dir); 229 return NULL; 230 } 231 232 struct tracepoint_path *tracepoint_name_to_path(const char *name) 233 { 234 struct tracepoint_path *path = zalloc(sizeof(*path)); 235 char *str = strchr(name, ':'); 236 237 if (path == NULL || str == NULL) { 238 free(path); 239 return NULL; 240 } 241 242 path->system = strndup(name, str - name); 243 path->name = strdup(str+1); 244 245 if (path->system == NULL || path->name == NULL) { 246 zfree(&path->system); 247 zfree(&path->name); 248 free(path); 249 path = NULL; 250 } 251 252 return path; 253 } 254 255 const char *event_type(int type) 256 { 257 switch (type) { 258 case PERF_TYPE_HARDWARE: 259 return "hardware"; 260 261 case PERF_TYPE_SOFTWARE: 262 return "software"; 263 264 case PERF_TYPE_TRACEPOINT: 265 return "tracepoint"; 266 267 case PERF_TYPE_HW_CACHE: 268 return "hardware-cache"; 269 270 default: 271 break; 272 } 273 274 return "unknown"; 275 } 276 277 278 279 static struct perf_evsel * 280 __add_event(struct list_head *list, int *idx, 281 struct perf_event_attr *attr, 282 char *name, struct cpu_map *cpus, 283 struct list_head *config_terms) 284 { 285 struct perf_evsel *evsel; 286 287 event_attr_init(attr); 288 289 evsel = perf_evsel__new_idx(attr, (*idx)++); 290 if (!evsel) 291 return NULL; 292 293 evsel->cpus = cpu_map__get(cpus); 294 evsel->own_cpus = cpu_map__get(cpus); 295 296 if (name) 297 evsel->name = strdup(name); 298 299 if (config_terms) 300 list_splice(config_terms, &evsel->config_terms); 301 302 list_add_tail(&evsel->node, list); 303 return evsel; 304 } 305 306 static int add_event(struct list_head *list, int *idx, 307 struct perf_event_attr *attr, char *name, 308 struct list_head *config_terms) 309 { 310 return __add_event(list, idx, attr, name, NULL, config_terms) ? 0 : -ENOMEM; 311 } 312 313 static int parse_aliases(char *str, const char *names[][PERF_EVSEL__MAX_ALIASES], int size) 314 { 315 int i, j; 316 int n, longest = -1; 317 318 for (i = 0; i < size; i++) { 319 for (j = 0; j < PERF_EVSEL__MAX_ALIASES && names[i][j]; j++) { 320 n = strlen(names[i][j]); 321 if (n > longest && !strncasecmp(str, names[i][j], n)) 322 longest = n; 323 } 324 if (longest > 0) 325 return i; 326 } 327 328 return -1; 329 } 330 331 int parse_events_add_cache(struct list_head *list, int *idx, 332 char *type, char *op_result1, char *op_result2) 333 { 334 struct perf_event_attr attr; 335 char name[MAX_NAME_LEN]; 336 int cache_type = -1, cache_op = -1, cache_result = -1; 337 char *op_result[2] = { op_result1, op_result2 }; 338 int i, n; 339 340 /* 341 * No fallback - if we cannot get a clear cache type 342 * then bail out: 343 */ 344 cache_type = parse_aliases(type, perf_evsel__hw_cache, 345 PERF_COUNT_HW_CACHE_MAX); 346 if (cache_type == -1) 347 return -EINVAL; 348 349 n = snprintf(name, MAX_NAME_LEN, "%s", type); 350 351 for (i = 0; (i < 2) && (op_result[i]); i++) { 352 char *str = op_result[i]; 353 354 n += snprintf(name + n, MAX_NAME_LEN - n, "-%s", str); 355 356 if (cache_op == -1) { 357 cache_op = parse_aliases(str, perf_evsel__hw_cache_op, 358 PERF_COUNT_HW_CACHE_OP_MAX); 359 if (cache_op >= 0) { 360 if (!perf_evsel__is_cache_op_valid(cache_type, cache_op)) 361 return -EINVAL; 362 continue; 363 } 364 } 365 366 if (cache_result == -1) { 367 cache_result = parse_aliases(str, perf_evsel__hw_cache_result, 368 PERF_COUNT_HW_CACHE_RESULT_MAX); 369 if (cache_result >= 0) 370 continue; 371 } 372 } 373 374 /* 375 * Fall back to reads: 376 */ 377 if (cache_op == -1) 378 cache_op = PERF_COUNT_HW_CACHE_OP_READ; 379 380 /* 381 * Fall back to accesses: 382 */ 383 if (cache_result == -1) 384 cache_result = PERF_COUNT_HW_CACHE_RESULT_ACCESS; 385 386 memset(&attr, 0, sizeof(attr)); 387 attr.config = cache_type | (cache_op << 8) | (cache_result << 16); 388 attr.type = PERF_TYPE_HW_CACHE; 389 return add_event(list, idx, &attr, name, NULL); 390 } 391 392 static void tracepoint_error(struct parse_events_error *e, int err, 393 char *sys, char *name) 394 { 395 char help[BUFSIZ]; 396 397 /* 398 * We get error directly from syscall errno ( > 0), 399 * or from encoded pointer's error ( < 0). 400 */ 401 err = abs(err); 402 403 switch (err) { 404 case EACCES: 405 e->str = strdup("can't access trace events"); 406 break; 407 case ENOENT: 408 e->str = strdup("unknown tracepoint"); 409 break; 410 default: 411 e->str = strdup("failed to add tracepoint"); 412 break; 413 } 414 415 tracing_path__strerror_open_tp(err, help, sizeof(help), sys, name); 416 e->help = strdup(help); 417 } 418 419 static int add_tracepoint(struct list_head *list, int *idx, 420 char *sys_name, char *evt_name, 421 struct parse_events_error *err, 422 struct list_head *head_config) 423 { 424 struct perf_evsel *evsel; 425 426 evsel = perf_evsel__newtp_idx(sys_name, evt_name, (*idx)++); 427 if (IS_ERR(evsel)) { 428 tracepoint_error(err, PTR_ERR(evsel), sys_name, evt_name); 429 return PTR_ERR(evsel); 430 } 431 432 if (head_config) { 433 LIST_HEAD(config_terms); 434 435 if (get_config_terms(head_config, &config_terms)) 436 return -ENOMEM; 437 list_splice(&config_terms, &evsel->config_terms); 438 } 439 440 list_add_tail(&evsel->node, list); 441 return 0; 442 } 443 444 static int add_tracepoint_multi_event(struct list_head *list, int *idx, 445 char *sys_name, char *evt_name, 446 struct parse_events_error *err, 447 struct list_head *head_config) 448 { 449 char evt_path[MAXPATHLEN]; 450 struct dirent *evt_ent; 451 DIR *evt_dir; 452 int ret = 0; 453 454 snprintf(evt_path, MAXPATHLEN, "%s/%s", tracing_events_path, sys_name); 455 evt_dir = opendir(evt_path); 456 if (!evt_dir) { 457 tracepoint_error(err, errno, sys_name, evt_name); 458 return -1; 459 } 460 461 while (!ret && (evt_ent = readdir(evt_dir))) { 462 if (!strcmp(evt_ent->d_name, ".") 463 || !strcmp(evt_ent->d_name, "..") 464 || !strcmp(evt_ent->d_name, "enable") 465 || !strcmp(evt_ent->d_name, "filter")) 466 continue; 467 468 if (!strglobmatch(evt_ent->d_name, evt_name)) 469 continue; 470 471 ret = add_tracepoint(list, idx, sys_name, evt_ent->d_name, 472 err, head_config); 473 } 474 475 closedir(evt_dir); 476 return ret; 477 } 478 479 static int add_tracepoint_event(struct list_head *list, int *idx, 480 char *sys_name, char *evt_name, 481 struct parse_events_error *err, 482 struct list_head *head_config) 483 { 484 return strpbrk(evt_name, "*?") ? 485 add_tracepoint_multi_event(list, idx, sys_name, evt_name, 486 err, head_config) : 487 add_tracepoint(list, idx, sys_name, evt_name, 488 err, head_config); 489 } 490 491 static int add_tracepoint_multi_sys(struct list_head *list, int *idx, 492 char *sys_name, char *evt_name, 493 struct parse_events_error *err, 494 struct list_head *head_config) 495 { 496 struct dirent *events_ent; 497 DIR *events_dir; 498 int ret = 0; 499 500 events_dir = opendir(tracing_events_path); 501 if (!events_dir) { 502 tracepoint_error(err, errno, sys_name, evt_name); 503 return -1; 504 } 505 506 while (!ret && (events_ent = readdir(events_dir))) { 507 if (!strcmp(events_ent->d_name, ".") 508 || !strcmp(events_ent->d_name, "..") 509 || !strcmp(events_ent->d_name, "enable") 510 || !strcmp(events_ent->d_name, "header_event") 511 || !strcmp(events_ent->d_name, "header_page")) 512 continue; 513 514 if (!strglobmatch(events_ent->d_name, sys_name)) 515 continue; 516 517 ret = add_tracepoint_event(list, idx, events_ent->d_name, 518 evt_name, err, head_config); 519 } 520 521 closedir(events_dir); 522 return ret; 523 } 524 525 static int 526 parse_breakpoint_type(const char *type, struct perf_event_attr *attr) 527 { 528 int i; 529 530 for (i = 0; i < 3; i++) { 531 if (!type || !type[i]) 532 break; 533 534 #define CHECK_SET_TYPE(bit) \ 535 do { \ 536 if (attr->bp_type & bit) \ 537 return -EINVAL; \ 538 else \ 539 attr->bp_type |= bit; \ 540 } while (0) 541 542 switch (type[i]) { 543 case 'r': 544 CHECK_SET_TYPE(HW_BREAKPOINT_R); 545 break; 546 case 'w': 547 CHECK_SET_TYPE(HW_BREAKPOINT_W); 548 break; 549 case 'x': 550 CHECK_SET_TYPE(HW_BREAKPOINT_X); 551 break; 552 default: 553 return -EINVAL; 554 } 555 } 556 557 #undef CHECK_SET_TYPE 558 559 if (!attr->bp_type) /* Default */ 560 attr->bp_type = HW_BREAKPOINT_R | HW_BREAKPOINT_W; 561 562 return 0; 563 } 564 565 int parse_events_add_breakpoint(struct list_head *list, int *idx, 566 void *ptr, char *type, u64 len) 567 { 568 struct perf_event_attr attr; 569 570 memset(&attr, 0, sizeof(attr)); 571 attr.bp_addr = (unsigned long) ptr; 572 573 if (parse_breakpoint_type(type, &attr)) 574 return -EINVAL; 575 576 /* Provide some defaults if len is not specified */ 577 if (!len) { 578 if (attr.bp_type == HW_BREAKPOINT_X) 579 len = sizeof(long); 580 else 581 len = HW_BREAKPOINT_LEN_4; 582 } 583 584 attr.bp_len = len; 585 586 attr.type = PERF_TYPE_BREAKPOINT; 587 attr.sample_period = 1; 588 589 return add_event(list, idx, &attr, NULL, NULL); 590 } 591 592 static int check_type_val(struct parse_events_term *term, 593 struct parse_events_error *err, 594 int type) 595 { 596 if (type == term->type_val) 597 return 0; 598 599 if (err) { 600 err->idx = term->err_val; 601 if (type == PARSE_EVENTS__TERM_TYPE_NUM) 602 err->str = strdup("expected numeric value"); 603 else 604 err->str = strdup("expected string value"); 605 } 606 return -EINVAL; 607 } 608 609 typedef int config_term_func_t(struct perf_event_attr *attr, 610 struct parse_events_term *term, 611 struct parse_events_error *err); 612 613 static int config_term_common(struct perf_event_attr *attr, 614 struct parse_events_term *term, 615 struct parse_events_error *err) 616 { 617 #define CHECK_TYPE_VAL(type) \ 618 do { \ 619 if (check_type_val(term, err, PARSE_EVENTS__TERM_TYPE_ ## type)) \ 620 return -EINVAL; \ 621 } while (0) 622 623 switch (term->type_term) { 624 case PARSE_EVENTS__TERM_TYPE_CONFIG: 625 CHECK_TYPE_VAL(NUM); 626 attr->config = term->val.num; 627 break; 628 case PARSE_EVENTS__TERM_TYPE_CONFIG1: 629 CHECK_TYPE_VAL(NUM); 630 attr->config1 = term->val.num; 631 break; 632 case PARSE_EVENTS__TERM_TYPE_CONFIG2: 633 CHECK_TYPE_VAL(NUM); 634 attr->config2 = term->val.num; 635 break; 636 case PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD: 637 CHECK_TYPE_VAL(NUM); 638 break; 639 case PARSE_EVENTS__TERM_TYPE_SAMPLE_FREQ: 640 CHECK_TYPE_VAL(NUM); 641 break; 642 case PARSE_EVENTS__TERM_TYPE_BRANCH_SAMPLE_TYPE: 643 /* 644 * TODO uncomment when the field is available 645 * attr->branch_sample_type = term->val.num; 646 */ 647 break; 648 case PARSE_EVENTS__TERM_TYPE_TIME: 649 CHECK_TYPE_VAL(NUM); 650 if (term->val.num > 1) { 651 err->str = strdup("expected 0 or 1"); 652 err->idx = term->err_val; 653 return -EINVAL; 654 } 655 break; 656 case PARSE_EVENTS__TERM_TYPE_CALLGRAPH: 657 CHECK_TYPE_VAL(STR); 658 break; 659 case PARSE_EVENTS__TERM_TYPE_STACKSIZE: 660 CHECK_TYPE_VAL(NUM); 661 break; 662 case PARSE_EVENTS__TERM_TYPE_NAME: 663 CHECK_TYPE_VAL(STR); 664 break; 665 default: 666 err->str = strdup("unknown term"); 667 err->idx = term->err_term; 668 err->help = parse_events_formats_error_string(NULL); 669 return -EINVAL; 670 } 671 672 return 0; 673 #undef CHECK_TYPE_VAL 674 } 675 676 static int config_term_pmu(struct perf_event_attr *attr, 677 struct parse_events_term *term, 678 struct parse_events_error *err) 679 { 680 if (term->type_term == PARSE_EVENTS__TERM_TYPE_USER) 681 /* 682 * Always succeed for sysfs terms, as we dont know 683 * at this point what type they need to have. 684 */ 685 return 0; 686 else 687 return config_term_common(attr, term, err); 688 } 689 690 static int config_term_tracepoint(struct perf_event_attr *attr, 691 struct parse_events_term *term, 692 struct parse_events_error *err) 693 { 694 switch (term->type_term) { 695 case PARSE_EVENTS__TERM_TYPE_CALLGRAPH: 696 case PARSE_EVENTS__TERM_TYPE_STACKSIZE: 697 return config_term_common(attr, term, err); 698 default: 699 if (err) { 700 err->idx = term->err_term; 701 err->str = strdup("unknown term"); 702 err->help = strdup("valid terms: call-graph,stack-size\n"); 703 } 704 return -EINVAL; 705 } 706 707 return 0; 708 } 709 710 static int config_attr(struct perf_event_attr *attr, 711 struct list_head *head, 712 struct parse_events_error *err, 713 config_term_func_t config_term) 714 { 715 struct parse_events_term *term; 716 717 list_for_each_entry(term, head, list) 718 if (config_term(attr, term, err)) 719 return -EINVAL; 720 721 return 0; 722 } 723 724 static int get_config_terms(struct list_head *head_config, 725 struct list_head *head_terms __maybe_unused) 726 { 727 #define ADD_CONFIG_TERM(__type, __name, __val) \ 728 do { \ 729 struct perf_evsel_config_term *__t; \ 730 \ 731 __t = zalloc(sizeof(*__t)); \ 732 if (!__t) \ 733 return -ENOMEM; \ 734 \ 735 INIT_LIST_HEAD(&__t->list); \ 736 __t->type = PERF_EVSEL__CONFIG_TERM_ ## __type; \ 737 __t->val.__name = __val; \ 738 list_add_tail(&__t->list, head_terms); \ 739 } while (0) 740 741 struct parse_events_term *term; 742 743 list_for_each_entry(term, head_config, list) { 744 switch (term->type_term) { 745 case PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD: 746 ADD_CONFIG_TERM(PERIOD, period, term->val.num); 747 break; 748 case PARSE_EVENTS__TERM_TYPE_SAMPLE_FREQ: 749 ADD_CONFIG_TERM(FREQ, freq, term->val.num); 750 break; 751 case PARSE_EVENTS__TERM_TYPE_TIME: 752 ADD_CONFIG_TERM(TIME, time, term->val.num); 753 break; 754 case PARSE_EVENTS__TERM_TYPE_CALLGRAPH: 755 ADD_CONFIG_TERM(CALLGRAPH, callgraph, term->val.str); 756 break; 757 case PARSE_EVENTS__TERM_TYPE_STACKSIZE: 758 ADD_CONFIG_TERM(STACK_USER, stack_user, term->val.num); 759 break; 760 default: 761 break; 762 } 763 } 764 #undef ADD_EVSEL_CONFIG 765 return 0; 766 } 767 768 int parse_events_add_tracepoint(struct list_head *list, int *idx, 769 char *sys, char *event, 770 struct parse_events_error *err, 771 struct list_head *head_config) 772 { 773 if (head_config) { 774 struct perf_event_attr attr; 775 776 if (config_attr(&attr, head_config, err, 777 config_term_tracepoint)) 778 return -EINVAL; 779 } 780 781 if (strpbrk(sys, "*?")) 782 return add_tracepoint_multi_sys(list, idx, sys, event, 783 err, head_config); 784 else 785 return add_tracepoint_event(list, idx, sys, event, 786 err, head_config); 787 } 788 789 int parse_events_add_numeric(struct parse_events_evlist *data, 790 struct list_head *list, 791 u32 type, u64 config, 792 struct list_head *head_config) 793 { 794 struct perf_event_attr attr; 795 LIST_HEAD(config_terms); 796 797 memset(&attr, 0, sizeof(attr)); 798 attr.type = type; 799 attr.config = config; 800 801 if (head_config) { 802 if (config_attr(&attr, head_config, data->error, 803 config_term_common)) 804 return -EINVAL; 805 806 if (get_config_terms(head_config, &config_terms)) 807 return -ENOMEM; 808 } 809 810 return add_event(list, &data->idx, &attr, NULL, &config_terms); 811 } 812 813 static int parse_events__is_name_term(struct parse_events_term *term) 814 { 815 return term->type_term == PARSE_EVENTS__TERM_TYPE_NAME; 816 } 817 818 static char *pmu_event_name(struct list_head *head_terms) 819 { 820 struct parse_events_term *term; 821 822 list_for_each_entry(term, head_terms, list) 823 if (parse_events__is_name_term(term)) 824 return term->val.str; 825 826 return NULL; 827 } 828 829 int parse_events_add_pmu(struct parse_events_evlist *data, 830 struct list_head *list, char *name, 831 struct list_head *head_config) 832 { 833 struct perf_event_attr attr; 834 struct perf_pmu_info info; 835 struct perf_pmu *pmu; 836 struct perf_evsel *evsel; 837 LIST_HEAD(config_terms); 838 839 pmu = perf_pmu__find(name); 840 if (!pmu) 841 return -EINVAL; 842 843 if (pmu->default_config) { 844 memcpy(&attr, pmu->default_config, 845 sizeof(struct perf_event_attr)); 846 } else { 847 memset(&attr, 0, sizeof(attr)); 848 } 849 850 if (!head_config) { 851 attr.type = pmu->type; 852 evsel = __add_event(list, &data->idx, &attr, NULL, pmu->cpus, NULL); 853 return evsel ? 0 : -ENOMEM; 854 } 855 856 if (perf_pmu__check_alias(pmu, head_config, &info)) 857 return -EINVAL; 858 859 /* 860 * Configure hardcoded terms first, no need to check 861 * return value when called with fail == 0 ;) 862 */ 863 if (config_attr(&attr, head_config, data->error, config_term_pmu)) 864 return -EINVAL; 865 866 if (get_config_terms(head_config, &config_terms)) 867 return -ENOMEM; 868 869 if (perf_pmu__config(pmu, &attr, head_config, data->error)) 870 return -EINVAL; 871 872 evsel = __add_event(list, &data->idx, &attr, 873 pmu_event_name(head_config), pmu->cpus, 874 &config_terms); 875 if (evsel) { 876 evsel->unit = info.unit; 877 evsel->scale = info.scale; 878 evsel->per_pkg = info.per_pkg; 879 evsel->snapshot = info.snapshot; 880 } 881 882 return evsel ? 0 : -ENOMEM; 883 } 884 885 int parse_events__modifier_group(struct list_head *list, 886 char *event_mod) 887 { 888 return parse_events__modifier_event(list, event_mod, true); 889 } 890 891 void parse_events__set_leader(char *name, struct list_head *list) 892 { 893 struct perf_evsel *leader; 894 895 if (list_empty(list)) { 896 WARN_ONCE(true, "WARNING: failed to set leader: empty list"); 897 return; 898 } 899 900 __perf_evlist__set_leader(list); 901 leader = list_entry(list->next, struct perf_evsel, node); 902 leader->group_name = name ? strdup(name) : NULL; 903 } 904 905 /* list_event is assumed to point to malloc'ed memory */ 906 void parse_events_update_lists(struct list_head *list_event, 907 struct list_head *list_all) 908 { 909 /* 910 * Called for single event definition. Update the 911 * 'all event' list, and reinit the 'single event' 912 * list, for next event definition. 913 */ 914 list_splice_tail(list_event, list_all); 915 free(list_event); 916 } 917 918 struct event_modifier { 919 int eu; 920 int ek; 921 int eh; 922 int eH; 923 int eG; 924 int eI; 925 int precise; 926 int exclude_GH; 927 int sample_read; 928 int pinned; 929 }; 930 931 static int get_event_modifier(struct event_modifier *mod, char *str, 932 struct perf_evsel *evsel) 933 { 934 int eu = evsel ? evsel->attr.exclude_user : 0; 935 int ek = evsel ? evsel->attr.exclude_kernel : 0; 936 int eh = evsel ? evsel->attr.exclude_hv : 0; 937 int eH = evsel ? evsel->attr.exclude_host : 0; 938 int eG = evsel ? evsel->attr.exclude_guest : 0; 939 int eI = evsel ? evsel->attr.exclude_idle : 0; 940 int precise = evsel ? evsel->attr.precise_ip : 0; 941 int sample_read = 0; 942 int pinned = evsel ? evsel->attr.pinned : 0; 943 944 int exclude = eu | ek | eh; 945 int exclude_GH = evsel ? evsel->exclude_GH : 0; 946 947 memset(mod, 0, sizeof(*mod)); 948 949 while (*str) { 950 if (*str == 'u') { 951 if (!exclude) 952 exclude = eu = ek = eh = 1; 953 eu = 0; 954 } else if (*str == 'k') { 955 if (!exclude) 956 exclude = eu = ek = eh = 1; 957 ek = 0; 958 } else if (*str == 'h') { 959 if (!exclude) 960 exclude = eu = ek = eh = 1; 961 eh = 0; 962 } else if (*str == 'G') { 963 if (!exclude_GH) 964 exclude_GH = eG = eH = 1; 965 eG = 0; 966 } else if (*str == 'H') { 967 if (!exclude_GH) 968 exclude_GH = eG = eH = 1; 969 eH = 0; 970 } else if (*str == 'I') { 971 eI = 1; 972 } else if (*str == 'p') { 973 precise++; 974 /* use of precise requires exclude_guest */ 975 if (!exclude_GH) 976 eG = 1; 977 } else if (*str == 'S') { 978 sample_read = 1; 979 } else if (*str == 'D') { 980 pinned = 1; 981 } else 982 break; 983 984 ++str; 985 } 986 987 /* 988 * precise ip: 989 * 990 * 0 - SAMPLE_IP can have arbitrary skid 991 * 1 - SAMPLE_IP must have constant skid 992 * 2 - SAMPLE_IP requested to have 0 skid 993 * 3 - SAMPLE_IP must have 0 skid 994 * 995 * See also PERF_RECORD_MISC_EXACT_IP 996 */ 997 if (precise > 3) 998 return -EINVAL; 999 1000 mod->eu = eu; 1001 mod->ek = ek; 1002 mod->eh = eh; 1003 mod->eH = eH; 1004 mod->eG = eG; 1005 mod->eI = eI; 1006 mod->precise = precise; 1007 mod->exclude_GH = exclude_GH; 1008 mod->sample_read = sample_read; 1009 mod->pinned = pinned; 1010 1011 return 0; 1012 } 1013 1014 /* 1015 * Basic modifier sanity check to validate it contains only one 1016 * instance of any modifier (apart from 'p') present. 1017 */ 1018 static int check_modifier(char *str) 1019 { 1020 char *p = str; 1021 1022 /* The sizeof includes 0 byte as well. */ 1023 if (strlen(str) > (sizeof("ukhGHpppSDI") - 1)) 1024 return -1; 1025 1026 while (*p) { 1027 if (*p != 'p' && strchr(p + 1, *p)) 1028 return -1; 1029 p++; 1030 } 1031 1032 return 0; 1033 } 1034 1035 int parse_events__modifier_event(struct list_head *list, char *str, bool add) 1036 { 1037 struct perf_evsel *evsel; 1038 struct event_modifier mod; 1039 1040 if (str == NULL) 1041 return 0; 1042 1043 if (check_modifier(str)) 1044 return -EINVAL; 1045 1046 if (!add && get_event_modifier(&mod, str, NULL)) 1047 return -EINVAL; 1048 1049 __evlist__for_each(list, evsel) { 1050 if (add && get_event_modifier(&mod, str, evsel)) 1051 return -EINVAL; 1052 1053 evsel->attr.exclude_user = mod.eu; 1054 evsel->attr.exclude_kernel = mod.ek; 1055 evsel->attr.exclude_hv = mod.eh; 1056 evsel->attr.precise_ip = mod.precise; 1057 evsel->attr.exclude_host = mod.eH; 1058 evsel->attr.exclude_guest = mod.eG; 1059 evsel->attr.exclude_idle = mod.eI; 1060 evsel->exclude_GH = mod.exclude_GH; 1061 evsel->sample_read = mod.sample_read; 1062 1063 if (perf_evsel__is_group_leader(evsel)) 1064 evsel->attr.pinned = mod.pinned; 1065 } 1066 1067 return 0; 1068 } 1069 1070 int parse_events_name(struct list_head *list, char *name) 1071 { 1072 struct perf_evsel *evsel; 1073 1074 __evlist__for_each(list, evsel) { 1075 if (!evsel->name) 1076 evsel->name = strdup(name); 1077 } 1078 1079 return 0; 1080 } 1081 1082 static int 1083 comp_pmu(const void *p1, const void *p2) 1084 { 1085 struct perf_pmu_event_symbol *pmu1 = (struct perf_pmu_event_symbol *) p1; 1086 struct perf_pmu_event_symbol *pmu2 = (struct perf_pmu_event_symbol *) p2; 1087 1088 return strcmp(pmu1->symbol, pmu2->symbol); 1089 } 1090 1091 static void perf_pmu__parse_cleanup(void) 1092 { 1093 if (perf_pmu_events_list_num > 0) { 1094 struct perf_pmu_event_symbol *p; 1095 int i; 1096 1097 for (i = 0; i < perf_pmu_events_list_num; i++) { 1098 p = perf_pmu_events_list + i; 1099 free(p->symbol); 1100 } 1101 free(perf_pmu_events_list); 1102 perf_pmu_events_list = NULL; 1103 perf_pmu_events_list_num = 0; 1104 } 1105 } 1106 1107 #define SET_SYMBOL(str, stype) \ 1108 do { \ 1109 p->symbol = str; \ 1110 if (!p->symbol) \ 1111 goto err; \ 1112 p->type = stype; \ 1113 } while (0) 1114 1115 /* 1116 * Read the pmu events list from sysfs 1117 * Save it into perf_pmu_events_list 1118 */ 1119 static void perf_pmu__parse_init(void) 1120 { 1121 1122 struct perf_pmu *pmu = NULL; 1123 struct perf_pmu_alias *alias; 1124 int len = 0; 1125 1126 pmu = perf_pmu__find("cpu"); 1127 if ((pmu == NULL) || list_empty(&pmu->aliases)) { 1128 perf_pmu_events_list_num = -1; 1129 return; 1130 } 1131 list_for_each_entry(alias, &pmu->aliases, list) { 1132 if (strchr(alias->name, '-')) 1133 len++; 1134 len++; 1135 } 1136 perf_pmu_events_list = malloc(sizeof(struct perf_pmu_event_symbol) * len); 1137 if (!perf_pmu_events_list) 1138 return; 1139 perf_pmu_events_list_num = len; 1140 1141 len = 0; 1142 list_for_each_entry(alias, &pmu->aliases, list) { 1143 struct perf_pmu_event_symbol *p = perf_pmu_events_list + len; 1144 char *tmp = strchr(alias->name, '-'); 1145 1146 if (tmp != NULL) { 1147 SET_SYMBOL(strndup(alias->name, tmp - alias->name), 1148 PMU_EVENT_SYMBOL_PREFIX); 1149 p++; 1150 SET_SYMBOL(strdup(++tmp), PMU_EVENT_SYMBOL_SUFFIX); 1151 len += 2; 1152 } else { 1153 SET_SYMBOL(strdup(alias->name), PMU_EVENT_SYMBOL); 1154 len++; 1155 } 1156 } 1157 qsort(perf_pmu_events_list, len, 1158 sizeof(struct perf_pmu_event_symbol), comp_pmu); 1159 1160 return; 1161 err: 1162 perf_pmu__parse_cleanup(); 1163 } 1164 1165 enum perf_pmu_event_symbol_type 1166 perf_pmu__parse_check(const char *name) 1167 { 1168 struct perf_pmu_event_symbol p, *r; 1169 1170 /* scan kernel pmu events from sysfs if needed */ 1171 if (perf_pmu_events_list_num == 0) 1172 perf_pmu__parse_init(); 1173 /* 1174 * name "cpu" could be prefix of cpu-cycles or cpu// events. 1175 * cpu-cycles has been handled by hardcode. 1176 * So it must be cpu// events, not kernel pmu event. 1177 */ 1178 if ((perf_pmu_events_list_num <= 0) || !strcmp(name, "cpu")) 1179 return PMU_EVENT_SYMBOL_ERR; 1180 1181 p.symbol = strdup(name); 1182 r = bsearch(&p, perf_pmu_events_list, 1183 (size_t) perf_pmu_events_list_num, 1184 sizeof(struct perf_pmu_event_symbol), comp_pmu); 1185 free(p.symbol); 1186 return r ? r->type : PMU_EVENT_SYMBOL_ERR; 1187 } 1188 1189 static int parse_events__scanner(const char *str, void *data, int start_token) 1190 { 1191 YY_BUFFER_STATE buffer; 1192 void *scanner; 1193 int ret; 1194 1195 ret = parse_events_lex_init_extra(start_token, &scanner); 1196 if (ret) 1197 return ret; 1198 1199 buffer = parse_events__scan_string(str, scanner); 1200 1201 #ifdef PARSER_DEBUG 1202 parse_events_debug = 1; 1203 #endif 1204 ret = parse_events_parse(data, scanner); 1205 1206 parse_events__flush_buffer(buffer, scanner); 1207 parse_events__delete_buffer(buffer, scanner); 1208 parse_events_lex_destroy(scanner); 1209 return ret; 1210 } 1211 1212 /* 1213 * parse event config string, return a list of event terms. 1214 */ 1215 int parse_events_terms(struct list_head *terms, const char *str) 1216 { 1217 struct parse_events_terms data = { 1218 .terms = NULL, 1219 }; 1220 int ret; 1221 1222 ret = parse_events__scanner(str, &data, PE_START_TERMS); 1223 if (!ret) { 1224 list_splice(data.terms, terms); 1225 zfree(&data.terms); 1226 return 0; 1227 } 1228 1229 if (data.terms) 1230 parse_events__free_terms(data.terms); 1231 return ret; 1232 } 1233 1234 int parse_events(struct perf_evlist *evlist, const char *str, 1235 struct parse_events_error *err) 1236 { 1237 struct parse_events_evlist data = { 1238 .list = LIST_HEAD_INIT(data.list), 1239 .idx = evlist->nr_entries, 1240 .error = err, 1241 }; 1242 int ret; 1243 1244 ret = parse_events__scanner(str, &data, PE_START_EVENTS); 1245 perf_pmu__parse_cleanup(); 1246 if (!ret) { 1247 struct perf_evsel *last; 1248 1249 if (list_empty(&data.list)) { 1250 WARN_ONCE(true, "WARNING: event parser found nothing"); 1251 return -1; 1252 } 1253 1254 perf_evlist__splice_list_tail(evlist, &data.list); 1255 evlist->nr_groups += data.nr_groups; 1256 last = perf_evlist__last(evlist); 1257 last->cmdline_group_boundary = true; 1258 1259 return 0; 1260 } 1261 1262 /* 1263 * There are 2 users - builtin-record and builtin-test objects. 1264 * Both call perf_evlist__delete in case of error, so we dont 1265 * need to bother. 1266 */ 1267 return ret; 1268 } 1269 1270 #define MAX_WIDTH 1000 1271 static int get_term_width(void) 1272 { 1273 struct winsize ws; 1274 1275 get_term_dimensions(&ws); 1276 return ws.ws_col > MAX_WIDTH ? MAX_WIDTH : ws.ws_col; 1277 } 1278 1279 static void parse_events_print_error(struct parse_events_error *err, 1280 const char *event) 1281 { 1282 const char *str = "invalid or unsupported event: "; 1283 char _buf[MAX_WIDTH]; 1284 char *buf = (char *) event; 1285 int idx = 0; 1286 1287 if (err->str) { 1288 /* -2 for extra '' in the final fprintf */ 1289 int width = get_term_width() - 2; 1290 int len_event = strlen(event); 1291 int len_str, max_len, cut = 0; 1292 1293 /* 1294 * Maximum error index indent, we will cut 1295 * the event string if it's bigger. 1296 */ 1297 int max_err_idx = 13; 1298 1299 /* 1300 * Let's be specific with the message when 1301 * we have the precise error. 1302 */ 1303 str = "event syntax error: "; 1304 len_str = strlen(str); 1305 max_len = width - len_str; 1306 1307 buf = _buf; 1308 1309 /* We're cutting from the beggining. */ 1310 if (err->idx > max_err_idx) 1311 cut = err->idx - max_err_idx; 1312 1313 strncpy(buf, event + cut, max_len); 1314 1315 /* Mark cut parts with '..' on both sides. */ 1316 if (cut) 1317 buf[0] = buf[1] = '.'; 1318 1319 if ((len_event - cut) > max_len) { 1320 buf[max_len - 1] = buf[max_len - 2] = '.'; 1321 buf[max_len] = 0; 1322 } 1323 1324 idx = len_str + err->idx - cut; 1325 } 1326 1327 fprintf(stderr, "%s'%s'\n", str, buf); 1328 if (idx) { 1329 fprintf(stderr, "%*s\\___ %s\n", idx + 1, "", err->str); 1330 if (err->help) 1331 fprintf(stderr, "\n%s\n", err->help); 1332 free(err->str); 1333 free(err->help); 1334 } 1335 1336 fprintf(stderr, "Run 'perf list' for a list of valid events\n"); 1337 } 1338 1339 #undef MAX_WIDTH 1340 1341 int parse_events_option(const struct option *opt, const char *str, 1342 int unset __maybe_unused) 1343 { 1344 struct perf_evlist *evlist = *(struct perf_evlist **)opt->value; 1345 struct parse_events_error err = { .idx = 0, }; 1346 int ret = parse_events(evlist, str, &err); 1347 1348 if (ret) 1349 parse_events_print_error(&err, str); 1350 1351 return ret; 1352 } 1353 1354 static int 1355 foreach_evsel_in_last_glob(struct perf_evlist *evlist, 1356 int (*func)(struct perf_evsel *evsel, 1357 const void *arg), 1358 const void *arg) 1359 { 1360 struct perf_evsel *last = NULL; 1361 int err; 1362 1363 /* 1364 * Don't return when list_empty, give func a chance to report 1365 * error when it found last == NULL. 1366 * 1367 * So no need to WARN here, let *func do this. 1368 */ 1369 if (evlist->nr_entries > 0) 1370 last = perf_evlist__last(evlist); 1371 1372 do { 1373 err = (*func)(last, arg); 1374 if (err) 1375 return -1; 1376 if (!last) 1377 return 0; 1378 1379 if (last->node.prev == &evlist->entries) 1380 return 0; 1381 last = list_entry(last->node.prev, struct perf_evsel, node); 1382 } while (!last->cmdline_group_boundary); 1383 1384 return 0; 1385 } 1386 1387 static int set_filter(struct perf_evsel *evsel, const void *arg) 1388 { 1389 const char *str = arg; 1390 1391 if (evsel == NULL || evsel->attr.type != PERF_TYPE_TRACEPOINT) { 1392 fprintf(stderr, 1393 "--filter option should follow a -e tracepoint option\n"); 1394 return -1; 1395 } 1396 1397 if (perf_evsel__append_filter(evsel, "&&", str) < 0) { 1398 fprintf(stderr, 1399 "not enough memory to hold filter string\n"); 1400 return -1; 1401 } 1402 1403 return 0; 1404 } 1405 1406 int parse_filter(const struct option *opt, const char *str, 1407 int unset __maybe_unused) 1408 { 1409 struct perf_evlist *evlist = *(struct perf_evlist **)opt->value; 1410 1411 return foreach_evsel_in_last_glob(evlist, set_filter, 1412 (const void *)str); 1413 } 1414 1415 static int add_exclude_perf_filter(struct perf_evsel *evsel, 1416 const void *arg __maybe_unused) 1417 { 1418 char new_filter[64]; 1419 1420 if (evsel == NULL || evsel->attr.type != PERF_TYPE_TRACEPOINT) { 1421 fprintf(stderr, 1422 "--exclude-perf option should follow a -e tracepoint option\n"); 1423 return -1; 1424 } 1425 1426 snprintf(new_filter, sizeof(new_filter), "common_pid != %d", getpid()); 1427 1428 if (perf_evsel__append_filter(evsel, "&&", new_filter) < 0) { 1429 fprintf(stderr, 1430 "not enough memory to hold filter string\n"); 1431 return -1; 1432 } 1433 1434 return 0; 1435 } 1436 1437 int exclude_perf(const struct option *opt, 1438 const char *arg __maybe_unused, 1439 int unset __maybe_unused) 1440 { 1441 struct perf_evlist *evlist = *(struct perf_evlist **)opt->value; 1442 1443 return foreach_evsel_in_last_glob(evlist, add_exclude_perf_filter, 1444 NULL); 1445 } 1446 1447 static const char * const event_type_descriptors[] = { 1448 "Hardware event", 1449 "Software event", 1450 "Tracepoint event", 1451 "Hardware cache event", 1452 "Raw hardware event descriptor", 1453 "Hardware breakpoint", 1454 }; 1455 1456 static int cmp_string(const void *a, const void *b) 1457 { 1458 const char * const *as = a; 1459 const char * const *bs = b; 1460 1461 return strcmp(*as, *bs); 1462 } 1463 1464 /* 1465 * Print the events from <debugfs_mount_point>/tracing/events 1466 */ 1467 1468 void print_tracepoint_events(const char *subsys_glob, const char *event_glob, 1469 bool name_only) 1470 { 1471 DIR *sys_dir, *evt_dir; 1472 struct dirent *sys_next, *evt_next, sys_dirent, evt_dirent; 1473 char evt_path[MAXPATHLEN]; 1474 char dir_path[MAXPATHLEN]; 1475 char **evt_list = NULL; 1476 unsigned int evt_i = 0, evt_num = 0; 1477 bool evt_num_known = false; 1478 1479 restart: 1480 sys_dir = opendir(tracing_events_path); 1481 if (!sys_dir) 1482 return; 1483 1484 if (evt_num_known) { 1485 evt_list = zalloc(sizeof(char *) * evt_num); 1486 if (!evt_list) 1487 goto out_close_sys_dir; 1488 } 1489 1490 for_each_subsystem(sys_dir, sys_dirent, sys_next) { 1491 if (subsys_glob != NULL && 1492 !strglobmatch(sys_dirent.d_name, subsys_glob)) 1493 continue; 1494 1495 snprintf(dir_path, MAXPATHLEN, "%s/%s", tracing_events_path, 1496 sys_dirent.d_name); 1497 evt_dir = opendir(dir_path); 1498 if (!evt_dir) 1499 continue; 1500 1501 for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next) { 1502 if (event_glob != NULL && 1503 !strglobmatch(evt_dirent.d_name, event_glob)) 1504 continue; 1505 1506 if (!evt_num_known) { 1507 evt_num++; 1508 continue; 1509 } 1510 1511 snprintf(evt_path, MAXPATHLEN, "%s:%s", 1512 sys_dirent.d_name, evt_dirent.d_name); 1513 1514 evt_list[evt_i] = strdup(evt_path); 1515 if (evt_list[evt_i] == NULL) 1516 goto out_close_evt_dir; 1517 evt_i++; 1518 } 1519 closedir(evt_dir); 1520 } 1521 closedir(sys_dir); 1522 1523 if (!evt_num_known) { 1524 evt_num_known = true; 1525 goto restart; 1526 } 1527 qsort(evt_list, evt_num, sizeof(char *), cmp_string); 1528 evt_i = 0; 1529 while (evt_i < evt_num) { 1530 if (name_only) { 1531 printf("%s ", evt_list[evt_i++]); 1532 continue; 1533 } 1534 printf(" %-50s [%s]\n", evt_list[evt_i++], 1535 event_type_descriptors[PERF_TYPE_TRACEPOINT]); 1536 } 1537 if (evt_num) 1538 printf("\n"); 1539 1540 out_free: 1541 evt_num = evt_i; 1542 for (evt_i = 0; evt_i < evt_num; evt_i++) 1543 zfree(&evt_list[evt_i]); 1544 zfree(&evt_list); 1545 return; 1546 1547 out_close_evt_dir: 1548 closedir(evt_dir); 1549 out_close_sys_dir: 1550 closedir(sys_dir); 1551 1552 printf("FATAL: not enough memory to print %s\n", 1553 event_type_descriptors[PERF_TYPE_TRACEPOINT]); 1554 if (evt_list) 1555 goto out_free; 1556 } 1557 1558 /* 1559 * Check whether event is in <debugfs_mount_point>/tracing/events 1560 */ 1561 1562 int is_valid_tracepoint(const char *event_string) 1563 { 1564 DIR *sys_dir, *evt_dir; 1565 struct dirent *sys_next, *evt_next, sys_dirent, evt_dirent; 1566 char evt_path[MAXPATHLEN]; 1567 char dir_path[MAXPATHLEN]; 1568 1569 sys_dir = opendir(tracing_events_path); 1570 if (!sys_dir) 1571 return 0; 1572 1573 for_each_subsystem(sys_dir, sys_dirent, sys_next) { 1574 1575 snprintf(dir_path, MAXPATHLEN, "%s/%s", tracing_events_path, 1576 sys_dirent.d_name); 1577 evt_dir = opendir(dir_path); 1578 if (!evt_dir) 1579 continue; 1580 1581 for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next) { 1582 snprintf(evt_path, MAXPATHLEN, "%s:%s", 1583 sys_dirent.d_name, evt_dirent.d_name); 1584 if (!strcmp(evt_path, event_string)) { 1585 closedir(evt_dir); 1586 closedir(sys_dir); 1587 return 1; 1588 } 1589 } 1590 closedir(evt_dir); 1591 } 1592 closedir(sys_dir); 1593 return 0; 1594 } 1595 1596 static bool is_event_supported(u8 type, unsigned config) 1597 { 1598 bool ret = true; 1599 int open_return; 1600 struct perf_evsel *evsel; 1601 struct perf_event_attr attr = { 1602 .type = type, 1603 .config = config, 1604 .disabled = 1, 1605 }; 1606 struct { 1607 struct thread_map map; 1608 int threads[1]; 1609 } tmap = { 1610 .map.nr = 1, 1611 .threads = { 0 }, 1612 }; 1613 1614 evsel = perf_evsel__new(&attr); 1615 if (evsel) { 1616 open_return = perf_evsel__open(evsel, NULL, &tmap.map); 1617 ret = open_return >= 0; 1618 1619 if (open_return == -EACCES) { 1620 /* 1621 * This happens if the paranoid value 1622 * /proc/sys/kernel/perf_event_paranoid is set to 2 1623 * Re-run with exclude_kernel set; we don't do that 1624 * by default as some ARM machines do not support it. 1625 * 1626 */ 1627 evsel->attr.exclude_kernel = 1; 1628 ret = perf_evsel__open(evsel, NULL, &tmap.map) >= 0; 1629 } 1630 perf_evsel__delete(evsel); 1631 } 1632 1633 return ret; 1634 } 1635 1636 int print_hwcache_events(const char *event_glob, bool name_only) 1637 { 1638 unsigned int type, op, i, evt_i = 0, evt_num = 0; 1639 char name[64]; 1640 char **evt_list = NULL; 1641 bool evt_num_known = false; 1642 1643 restart: 1644 if (evt_num_known) { 1645 evt_list = zalloc(sizeof(char *) * evt_num); 1646 if (!evt_list) 1647 goto out_enomem; 1648 } 1649 1650 for (type = 0; type < PERF_COUNT_HW_CACHE_MAX; type++) { 1651 for (op = 0; op < PERF_COUNT_HW_CACHE_OP_MAX; op++) { 1652 /* skip invalid cache type */ 1653 if (!perf_evsel__is_cache_op_valid(type, op)) 1654 continue; 1655 1656 for (i = 0; i < PERF_COUNT_HW_CACHE_RESULT_MAX; i++) { 1657 __perf_evsel__hw_cache_type_op_res_name(type, op, i, 1658 name, sizeof(name)); 1659 if (event_glob != NULL && !strglobmatch(name, event_glob)) 1660 continue; 1661 1662 if (!is_event_supported(PERF_TYPE_HW_CACHE, 1663 type | (op << 8) | (i << 16))) 1664 continue; 1665 1666 if (!evt_num_known) { 1667 evt_num++; 1668 continue; 1669 } 1670 1671 evt_list[evt_i] = strdup(name); 1672 if (evt_list[evt_i] == NULL) 1673 goto out_enomem; 1674 evt_i++; 1675 } 1676 } 1677 } 1678 1679 if (!evt_num_known) { 1680 evt_num_known = true; 1681 goto restart; 1682 } 1683 qsort(evt_list, evt_num, sizeof(char *), cmp_string); 1684 evt_i = 0; 1685 while (evt_i < evt_num) { 1686 if (name_only) { 1687 printf("%s ", evt_list[evt_i++]); 1688 continue; 1689 } 1690 printf(" %-50s [%s]\n", evt_list[evt_i++], 1691 event_type_descriptors[PERF_TYPE_HW_CACHE]); 1692 } 1693 if (evt_num) 1694 printf("\n"); 1695 1696 out_free: 1697 evt_num = evt_i; 1698 for (evt_i = 0; evt_i < evt_num; evt_i++) 1699 zfree(&evt_list[evt_i]); 1700 zfree(&evt_list); 1701 return evt_num; 1702 1703 out_enomem: 1704 printf("FATAL: not enough memory to print %s\n", event_type_descriptors[PERF_TYPE_HW_CACHE]); 1705 if (evt_list) 1706 goto out_free; 1707 return evt_num; 1708 } 1709 1710 void print_symbol_events(const char *event_glob, unsigned type, 1711 struct event_symbol *syms, unsigned max, 1712 bool name_only) 1713 { 1714 unsigned int i, evt_i = 0, evt_num = 0; 1715 char name[MAX_NAME_LEN]; 1716 char **evt_list = NULL; 1717 bool evt_num_known = false; 1718 1719 restart: 1720 if (evt_num_known) { 1721 evt_list = zalloc(sizeof(char *) * evt_num); 1722 if (!evt_list) 1723 goto out_enomem; 1724 syms -= max; 1725 } 1726 1727 for (i = 0; i < max; i++, syms++) { 1728 1729 if (event_glob != NULL && 1730 !(strglobmatch(syms->symbol, event_glob) || 1731 (syms->alias && strglobmatch(syms->alias, event_glob)))) 1732 continue; 1733 1734 if (!is_event_supported(type, i)) 1735 continue; 1736 1737 if (!evt_num_known) { 1738 evt_num++; 1739 continue; 1740 } 1741 1742 if (!name_only && strlen(syms->alias)) 1743 snprintf(name, MAX_NAME_LEN, "%s OR %s", syms->symbol, syms->alias); 1744 else 1745 strncpy(name, syms->symbol, MAX_NAME_LEN); 1746 1747 evt_list[evt_i] = strdup(name); 1748 if (evt_list[evt_i] == NULL) 1749 goto out_enomem; 1750 evt_i++; 1751 } 1752 1753 if (!evt_num_known) { 1754 evt_num_known = true; 1755 goto restart; 1756 } 1757 qsort(evt_list, evt_num, sizeof(char *), cmp_string); 1758 evt_i = 0; 1759 while (evt_i < evt_num) { 1760 if (name_only) { 1761 printf("%s ", evt_list[evt_i++]); 1762 continue; 1763 } 1764 printf(" %-50s [%s]\n", evt_list[evt_i++], event_type_descriptors[type]); 1765 } 1766 if (evt_num) 1767 printf("\n"); 1768 1769 out_free: 1770 evt_num = evt_i; 1771 for (evt_i = 0; evt_i < evt_num; evt_i++) 1772 zfree(&evt_list[evt_i]); 1773 zfree(&evt_list); 1774 return; 1775 1776 out_enomem: 1777 printf("FATAL: not enough memory to print %s\n", event_type_descriptors[type]); 1778 if (evt_list) 1779 goto out_free; 1780 } 1781 1782 /* 1783 * Print the help text for the event symbols: 1784 */ 1785 void print_events(const char *event_glob, bool name_only) 1786 { 1787 print_symbol_events(event_glob, PERF_TYPE_HARDWARE, 1788 event_symbols_hw, PERF_COUNT_HW_MAX, name_only); 1789 1790 print_symbol_events(event_glob, PERF_TYPE_SOFTWARE, 1791 event_symbols_sw, PERF_COUNT_SW_MAX, name_only); 1792 1793 print_hwcache_events(event_glob, name_only); 1794 1795 print_pmu_events(event_glob, name_only); 1796 1797 if (event_glob != NULL) 1798 return; 1799 1800 if (!name_only) { 1801 printf(" %-50s [%s]\n", 1802 "rNNN", 1803 event_type_descriptors[PERF_TYPE_RAW]); 1804 printf(" %-50s [%s]\n", 1805 "cpu/t1=v1[,t2=v2,t3 ...]/modifier", 1806 event_type_descriptors[PERF_TYPE_RAW]); 1807 printf(" (see 'man perf-list' on how to encode it)\n"); 1808 printf("\n"); 1809 1810 printf(" %-50s [%s]\n", 1811 "mem:<addr>[/len][:access]", 1812 event_type_descriptors[PERF_TYPE_BREAKPOINT]); 1813 printf("\n"); 1814 } 1815 1816 print_tracepoint_events(NULL, NULL, name_only); 1817 } 1818 1819 int parse_events__is_hardcoded_term(struct parse_events_term *term) 1820 { 1821 return term->type_term != PARSE_EVENTS__TERM_TYPE_USER; 1822 } 1823 1824 static int new_term(struct parse_events_term **_term, int type_val, 1825 int type_term, char *config, 1826 char *str, u64 num, int err_term, int err_val) 1827 { 1828 struct parse_events_term *term; 1829 1830 term = zalloc(sizeof(*term)); 1831 if (!term) 1832 return -ENOMEM; 1833 1834 INIT_LIST_HEAD(&term->list); 1835 term->type_val = type_val; 1836 term->type_term = type_term; 1837 term->config = config; 1838 term->err_term = err_term; 1839 term->err_val = err_val; 1840 1841 switch (type_val) { 1842 case PARSE_EVENTS__TERM_TYPE_NUM: 1843 term->val.num = num; 1844 break; 1845 case PARSE_EVENTS__TERM_TYPE_STR: 1846 term->val.str = str; 1847 break; 1848 default: 1849 free(term); 1850 return -EINVAL; 1851 } 1852 1853 *_term = term; 1854 return 0; 1855 } 1856 1857 int parse_events_term__num(struct parse_events_term **term, 1858 int type_term, char *config, u64 num, 1859 void *loc_term_, void *loc_val_) 1860 { 1861 YYLTYPE *loc_term = loc_term_; 1862 YYLTYPE *loc_val = loc_val_; 1863 1864 return new_term(term, PARSE_EVENTS__TERM_TYPE_NUM, type_term, 1865 config, NULL, num, 1866 loc_term ? loc_term->first_column : 0, 1867 loc_val ? loc_val->first_column : 0); 1868 } 1869 1870 int parse_events_term__str(struct parse_events_term **term, 1871 int type_term, char *config, char *str, 1872 void *loc_term_, void *loc_val_) 1873 { 1874 YYLTYPE *loc_term = loc_term_; 1875 YYLTYPE *loc_val = loc_val_; 1876 1877 return new_term(term, PARSE_EVENTS__TERM_TYPE_STR, type_term, 1878 config, str, 0, 1879 loc_term ? loc_term->first_column : 0, 1880 loc_val ? loc_val->first_column : 0); 1881 } 1882 1883 int parse_events_term__sym_hw(struct parse_events_term **term, 1884 char *config, unsigned idx) 1885 { 1886 struct event_symbol *sym; 1887 1888 BUG_ON(idx >= PERF_COUNT_HW_MAX); 1889 sym = &event_symbols_hw[idx]; 1890 1891 if (config) 1892 return new_term(term, PARSE_EVENTS__TERM_TYPE_STR, 1893 PARSE_EVENTS__TERM_TYPE_USER, config, 1894 (char *) sym->symbol, 0, 0, 0); 1895 else 1896 return new_term(term, PARSE_EVENTS__TERM_TYPE_STR, 1897 PARSE_EVENTS__TERM_TYPE_USER, 1898 (char *) "event", (char *) sym->symbol, 1899 0, 0, 0); 1900 } 1901 1902 int parse_events_term__clone(struct parse_events_term **new, 1903 struct parse_events_term *term) 1904 { 1905 return new_term(new, term->type_val, term->type_term, term->config, 1906 term->val.str, term->val.num, 1907 term->err_term, term->err_val); 1908 } 1909 1910 void parse_events__free_terms(struct list_head *terms) 1911 { 1912 struct parse_events_term *term, *h; 1913 1914 list_for_each_entry_safe(term, h, terms, list) 1915 free(term); 1916 } 1917 1918 void parse_events_evlist_error(struct parse_events_evlist *data, 1919 int idx, const char *str) 1920 { 1921 struct parse_events_error *err = data->error; 1922 1923 if (!err) 1924 return; 1925 err->idx = idx; 1926 err->str = strdup(str); 1927 WARN_ONCE(!err->str, "WARNING: failed to allocate error string"); 1928 } 1929 1930 /* 1931 * Return string contains valid config terms of an event. 1932 * @additional_terms: For terms such as PMU sysfs terms. 1933 */ 1934 char *parse_events_formats_error_string(char *additional_terms) 1935 { 1936 char *str; 1937 static const char *static_terms = "config,config1,config2,name," 1938 "period,freq,branch_type,time," 1939 "call-graph,stack-size\n"; 1940 1941 /* valid terms */ 1942 if (additional_terms) { 1943 if (!asprintf(&str, "valid terms: %s,%s", 1944 additional_terms, static_terms)) 1945 goto fail; 1946 } else { 1947 if (!asprintf(&str, "valid terms: %s", static_terms)) 1948 goto fail; 1949 } 1950 return str; 1951 1952 fail: 1953 return NULL; 1954 } 1955