1 #include <linux/hw_breakpoint.h> 2 #include "util.h" 3 #include "../perf.h" 4 #include "evlist.h" 5 #include "evsel.h" 6 #include "parse-options.h" 7 #include "parse-events.h" 8 #include "exec_cmd.h" 9 #include "string.h" 10 #include "symbol.h" 11 #include "cache.h" 12 #include "header.h" 13 #include "debug.h" 14 #include <api/fs/debugfs.h> 15 #include "parse-events-bison.h" 16 #define YY_EXTRA_TYPE int 17 #include "parse-events-flex.h" 18 #include "pmu.h" 19 #include "thread_map.h" 20 21 #define MAX_NAME_LEN 100 22 23 struct event_symbol { 24 const char *symbol; 25 const char *alias; 26 }; 27 28 #ifdef PARSER_DEBUG 29 extern int parse_events_debug; 30 #endif 31 int parse_events_parse(void *data, void *scanner); 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 static 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 static 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 { 284 struct perf_evsel *evsel; 285 286 event_attr_init(attr); 287 288 evsel = perf_evsel__new_idx(attr, (*idx)++); 289 if (!evsel) 290 return NULL; 291 292 evsel->cpus = cpus; 293 if (name) 294 evsel->name = strdup(name); 295 list_add_tail(&evsel->node, list); 296 return evsel; 297 } 298 299 static int add_event(struct list_head *list, int *idx, 300 struct perf_event_attr *attr, char *name) 301 { 302 return __add_event(list, idx, attr, name, NULL) ? 0 : -ENOMEM; 303 } 304 305 static int parse_aliases(char *str, const char *names[][PERF_EVSEL__MAX_ALIASES], int size) 306 { 307 int i, j; 308 int n, longest = -1; 309 310 for (i = 0; i < size; i++) { 311 for (j = 0; j < PERF_EVSEL__MAX_ALIASES && names[i][j]; j++) { 312 n = strlen(names[i][j]); 313 if (n > longest && !strncasecmp(str, names[i][j], n)) 314 longest = n; 315 } 316 if (longest > 0) 317 return i; 318 } 319 320 return -1; 321 } 322 323 int parse_events_add_cache(struct list_head *list, int *idx, 324 char *type, char *op_result1, char *op_result2) 325 { 326 struct perf_event_attr attr; 327 char name[MAX_NAME_LEN]; 328 int cache_type = -1, cache_op = -1, cache_result = -1; 329 char *op_result[2] = { op_result1, op_result2 }; 330 int i, n; 331 332 /* 333 * No fallback - if we cannot get a clear cache type 334 * then bail out: 335 */ 336 cache_type = parse_aliases(type, perf_evsel__hw_cache, 337 PERF_COUNT_HW_CACHE_MAX); 338 if (cache_type == -1) 339 return -EINVAL; 340 341 n = snprintf(name, MAX_NAME_LEN, "%s", type); 342 343 for (i = 0; (i < 2) && (op_result[i]); i++) { 344 char *str = op_result[i]; 345 346 n += snprintf(name + n, MAX_NAME_LEN - n, "-%s", str); 347 348 if (cache_op == -1) { 349 cache_op = parse_aliases(str, perf_evsel__hw_cache_op, 350 PERF_COUNT_HW_CACHE_OP_MAX); 351 if (cache_op >= 0) { 352 if (!perf_evsel__is_cache_op_valid(cache_type, cache_op)) 353 return -EINVAL; 354 continue; 355 } 356 } 357 358 if (cache_result == -1) { 359 cache_result = parse_aliases(str, perf_evsel__hw_cache_result, 360 PERF_COUNT_HW_CACHE_RESULT_MAX); 361 if (cache_result >= 0) 362 continue; 363 } 364 } 365 366 /* 367 * Fall back to reads: 368 */ 369 if (cache_op == -1) 370 cache_op = PERF_COUNT_HW_CACHE_OP_READ; 371 372 /* 373 * Fall back to accesses: 374 */ 375 if (cache_result == -1) 376 cache_result = PERF_COUNT_HW_CACHE_RESULT_ACCESS; 377 378 memset(&attr, 0, sizeof(attr)); 379 attr.config = cache_type | (cache_op << 8) | (cache_result << 16); 380 attr.type = PERF_TYPE_HW_CACHE; 381 return add_event(list, idx, &attr, name); 382 } 383 384 static int add_tracepoint(struct list_head *list, int *idx, 385 char *sys_name, char *evt_name) 386 { 387 struct perf_evsel *evsel; 388 389 evsel = perf_evsel__newtp_idx(sys_name, evt_name, (*idx)++); 390 if (!evsel) 391 return -ENOMEM; 392 393 list_add_tail(&evsel->node, list); 394 395 return 0; 396 } 397 398 static int add_tracepoint_multi_event(struct list_head *list, int *idx, 399 char *sys_name, char *evt_name) 400 { 401 char evt_path[MAXPATHLEN]; 402 struct dirent *evt_ent; 403 DIR *evt_dir; 404 int ret = 0; 405 406 snprintf(evt_path, MAXPATHLEN, "%s/%s", tracing_events_path, sys_name); 407 evt_dir = opendir(evt_path); 408 if (!evt_dir) { 409 perror("Can't open event dir"); 410 return -1; 411 } 412 413 while (!ret && (evt_ent = readdir(evt_dir))) { 414 if (!strcmp(evt_ent->d_name, ".") 415 || !strcmp(evt_ent->d_name, "..") 416 || !strcmp(evt_ent->d_name, "enable") 417 || !strcmp(evt_ent->d_name, "filter")) 418 continue; 419 420 if (!strglobmatch(evt_ent->d_name, evt_name)) 421 continue; 422 423 ret = add_tracepoint(list, idx, sys_name, evt_ent->d_name); 424 } 425 426 closedir(evt_dir); 427 return ret; 428 } 429 430 static int add_tracepoint_event(struct list_head *list, int *idx, 431 char *sys_name, char *evt_name) 432 { 433 return strpbrk(evt_name, "*?") ? 434 add_tracepoint_multi_event(list, idx, sys_name, evt_name) : 435 add_tracepoint(list, idx, sys_name, evt_name); 436 } 437 438 static int add_tracepoint_multi_sys(struct list_head *list, int *idx, 439 char *sys_name, char *evt_name) 440 { 441 struct dirent *events_ent; 442 DIR *events_dir; 443 int ret = 0; 444 445 events_dir = opendir(tracing_events_path); 446 if (!events_dir) { 447 perror("Can't open event dir"); 448 return -1; 449 } 450 451 while (!ret && (events_ent = readdir(events_dir))) { 452 if (!strcmp(events_ent->d_name, ".") 453 || !strcmp(events_ent->d_name, "..") 454 || !strcmp(events_ent->d_name, "enable") 455 || !strcmp(events_ent->d_name, "header_event") 456 || !strcmp(events_ent->d_name, "header_page")) 457 continue; 458 459 if (!strglobmatch(events_ent->d_name, sys_name)) 460 continue; 461 462 ret = add_tracepoint_event(list, idx, events_ent->d_name, 463 evt_name); 464 } 465 466 closedir(events_dir); 467 return ret; 468 } 469 470 int parse_events_add_tracepoint(struct list_head *list, int *idx, 471 char *sys, char *event) 472 { 473 if (strpbrk(sys, "*?")) 474 return add_tracepoint_multi_sys(list, idx, sys, event); 475 else 476 return add_tracepoint_event(list, idx, sys, event); 477 } 478 479 static int 480 parse_breakpoint_type(const char *type, struct perf_event_attr *attr) 481 { 482 int i; 483 484 for (i = 0; i < 3; i++) { 485 if (!type || !type[i]) 486 break; 487 488 #define CHECK_SET_TYPE(bit) \ 489 do { \ 490 if (attr->bp_type & bit) \ 491 return -EINVAL; \ 492 else \ 493 attr->bp_type |= bit; \ 494 } while (0) 495 496 switch (type[i]) { 497 case 'r': 498 CHECK_SET_TYPE(HW_BREAKPOINT_R); 499 break; 500 case 'w': 501 CHECK_SET_TYPE(HW_BREAKPOINT_W); 502 break; 503 case 'x': 504 CHECK_SET_TYPE(HW_BREAKPOINT_X); 505 break; 506 default: 507 return -EINVAL; 508 } 509 } 510 511 #undef CHECK_SET_TYPE 512 513 if (!attr->bp_type) /* Default */ 514 attr->bp_type = HW_BREAKPOINT_R | HW_BREAKPOINT_W; 515 516 return 0; 517 } 518 519 int parse_events_add_breakpoint(struct list_head *list, int *idx, 520 void *ptr, char *type, u64 len) 521 { 522 struct perf_event_attr attr; 523 524 memset(&attr, 0, sizeof(attr)); 525 attr.bp_addr = (unsigned long) ptr; 526 527 if (parse_breakpoint_type(type, &attr)) 528 return -EINVAL; 529 530 /* Provide some defaults if len is not specified */ 531 if (!len) { 532 if (attr.bp_type == HW_BREAKPOINT_X) 533 len = sizeof(long); 534 else 535 len = HW_BREAKPOINT_LEN_4; 536 } 537 538 attr.bp_len = len; 539 540 attr.type = PERF_TYPE_BREAKPOINT; 541 attr.sample_period = 1; 542 543 return add_event(list, idx, &attr, NULL); 544 } 545 546 static int config_term(struct perf_event_attr *attr, 547 struct parse_events_term *term) 548 { 549 #define CHECK_TYPE_VAL(type) \ 550 do { \ 551 if (PARSE_EVENTS__TERM_TYPE_ ## type != term->type_val) \ 552 return -EINVAL; \ 553 } while (0) 554 555 switch (term->type_term) { 556 case PARSE_EVENTS__TERM_TYPE_CONFIG: 557 CHECK_TYPE_VAL(NUM); 558 attr->config = term->val.num; 559 break; 560 case PARSE_EVENTS__TERM_TYPE_CONFIG1: 561 CHECK_TYPE_VAL(NUM); 562 attr->config1 = term->val.num; 563 break; 564 case PARSE_EVENTS__TERM_TYPE_CONFIG2: 565 CHECK_TYPE_VAL(NUM); 566 attr->config2 = term->val.num; 567 break; 568 case PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD: 569 CHECK_TYPE_VAL(NUM); 570 attr->sample_period = term->val.num; 571 break; 572 case PARSE_EVENTS__TERM_TYPE_BRANCH_SAMPLE_TYPE: 573 /* 574 * TODO uncomment when the field is available 575 * attr->branch_sample_type = term->val.num; 576 */ 577 break; 578 case PARSE_EVENTS__TERM_TYPE_NAME: 579 CHECK_TYPE_VAL(STR); 580 break; 581 default: 582 return -EINVAL; 583 } 584 585 return 0; 586 #undef CHECK_TYPE_VAL 587 } 588 589 static int config_attr(struct perf_event_attr *attr, 590 struct list_head *head, int fail) 591 { 592 struct parse_events_term *term; 593 594 list_for_each_entry(term, head, list) 595 if (config_term(attr, term) && fail) 596 return -EINVAL; 597 598 return 0; 599 } 600 601 int parse_events_add_numeric(struct list_head *list, int *idx, 602 u32 type, u64 config, 603 struct list_head *head_config) 604 { 605 struct perf_event_attr attr; 606 607 memset(&attr, 0, sizeof(attr)); 608 attr.type = type; 609 attr.config = config; 610 611 if (head_config && 612 config_attr(&attr, head_config, 1)) 613 return -EINVAL; 614 615 return add_event(list, idx, &attr, NULL); 616 } 617 618 static int parse_events__is_name_term(struct parse_events_term *term) 619 { 620 return term->type_term == PARSE_EVENTS__TERM_TYPE_NAME; 621 } 622 623 static char *pmu_event_name(struct list_head *head_terms) 624 { 625 struct parse_events_term *term; 626 627 list_for_each_entry(term, head_terms, list) 628 if (parse_events__is_name_term(term)) 629 return term->val.str; 630 631 return NULL; 632 } 633 634 int parse_events_add_pmu(struct list_head *list, int *idx, 635 char *name, struct list_head *head_config) 636 { 637 struct perf_event_attr attr; 638 struct perf_pmu_info info; 639 struct perf_pmu *pmu; 640 struct perf_evsel *evsel; 641 642 pmu = perf_pmu__find(name); 643 if (!pmu) 644 return -EINVAL; 645 646 if (pmu->default_config) { 647 memcpy(&attr, pmu->default_config, 648 sizeof(struct perf_event_attr)); 649 } else { 650 memset(&attr, 0, sizeof(attr)); 651 } 652 653 if (!head_config) { 654 attr.type = pmu->type; 655 evsel = __add_event(list, idx, &attr, NULL, pmu->cpus); 656 return evsel ? 0 : -ENOMEM; 657 } 658 659 if (perf_pmu__check_alias(pmu, head_config, &info)) 660 return -EINVAL; 661 662 /* 663 * Configure hardcoded terms first, no need to check 664 * return value when called with fail == 0 ;) 665 */ 666 config_attr(&attr, head_config, 0); 667 668 if (perf_pmu__config(pmu, &attr, head_config)) 669 return -EINVAL; 670 671 evsel = __add_event(list, idx, &attr, pmu_event_name(head_config), 672 pmu->cpus); 673 if (evsel) { 674 evsel->unit = info.unit; 675 evsel->scale = info.scale; 676 evsel->per_pkg = info.per_pkg; 677 evsel->snapshot = info.snapshot; 678 } 679 680 return evsel ? 0 : -ENOMEM; 681 } 682 683 int parse_events__modifier_group(struct list_head *list, 684 char *event_mod) 685 { 686 return parse_events__modifier_event(list, event_mod, true); 687 } 688 689 void parse_events__set_leader(char *name, struct list_head *list) 690 { 691 struct perf_evsel *leader; 692 693 __perf_evlist__set_leader(list); 694 leader = list_entry(list->next, struct perf_evsel, node); 695 leader->group_name = name ? strdup(name) : NULL; 696 } 697 698 /* list_event is assumed to point to malloc'ed memory */ 699 void parse_events_update_lists(struct list_head *list_event, 700 struct list_head *list_all) 701 { 702 /* 703 * Called for single event definition. Update the 704 * 'all event' list, and reinit the 'single event' 705 * list, for next event definition. 706 */ 707 list_splice_tail(list_event, list_all); 708 free(list_event); 709 } 710 711 struct event_modifier { 712 int eu; 713 int ek; 714 int eh; 715 int eH; 716 int eG; 717 int precise; 718 int exclude_GH; 719 int sample_read; 720 int pinned; 721 }; 722 723 static int get_event_modifier(struct event_modifier *mod, char *str, 724 struct perf_evsel *evsel) 725 { 726 int eu = evsel ? evsel->attr.exclude_user : 0; 727 int ek = evsel ? evsel->attr.exclude_kernel : 0; 728 int eh = evsel ? evsel->attr.exclude_hv : 0; 729 int eH = evsel ? evsel->attr.exclude_host : 0; 730 int eG = evsel ? evsel->attr.exclude_guest : 0; 731 int precise = evsel ? evsel->attr.precise_ip : 0; 732 int sample_read = 0; 733 int pinned = evsel ? evsel->attr.pinned : 0; 734 735 int exclude = eu | ek | eh; 736 int exclude_GH = evsel ? evsel->exclude_GH : 0; 737 738 memset(mod, 0, sizeof(*mod)); 739 740 while (*str) { 741 if (*str == 'u') { 742 if (!exclude) 743 exclude = eu = ek = eh = 1; 744 eu = 0; 745 } else if (*str == 'k') { 746 if (!exclude) 747 exclude = eu = ek = eh = 1; 748 ek = 0; 749 } else if (*str == 'h') { 750 if (!exclude) 751 exclude = eu = ek = eh = 1; 752 eh = 0; 753 } else if (*str == 'G') { 754 if (!exclude_GH) 755 exclude_GH = eG = eH = 1; 756 eG = 0; 757 } else if (*str == 'H') { 758 if (!exclude_GH) 759 exclude_GH = eG = eH = 1; 760 eH = 0; 761 } else if (*str == 'p') { 762 precise++; 763 /* use of precise requires exclude_guest */ 764 if (!exclude_GH) 765 eG = 1; 766 } else if (*str == 'S') { 767 sample_read = 1; 768 } else if (*str == 'D') { 769 pinned = 1; 770 } else 771 break; 772 773 ++str; 774 } 775 776 /* 777 * precise ip: 778 * 779 * 0 - SAMPLE_IP can have arbitrary skid 780 * 1 - SAMPLE_IP must have constant skid 781 * 2 - SAMPLE_IP requested to have 0 skid 782 * 3 - SAMPLE_IP must have 0 skid 783 * 784 * See also PERF_RECORD_MISC_EXACT_IP 785 */ 786 if (precise > 3) 787 return -EINVAL; 788 789 mod->eu = eu; 790 mod->ek = ek; 791 mod->eh = eh; 792 mod->eH = eH; 793 mod->eG = eG; 794 mod->precise = precise; 795 mod->exclude_GH = exclude_GH; 796 mod->sample_read = sample_read; 797 mod->pinned = pinned; 798 799 return 0; 800 } 801 802 /* 803 * Basic modifier sanity check to validate it contains only one 804 * instance of any modifier (apart from 'p') present. 805 */ 806 static int check_modifier(char *str) 807 { 808 char *p = str; 809 810 /* The sizeof includes 0 byte as well. */ 811 if (strlen(str) > (sizeof("ukhGHpppSD") - 1)) 812 return -1; 813 814 while (*p) { 815 if (*p != 'p' && strchr(p + 1, *p)) 816 return -1; 817 p++; 818 } 819 820 return 0; 821 } 822 823 int parse_events__modifier_event(struct list_head *list, char *str, bool add) 824 { 825 struct perf_evsel *evsel; 826 struct event_modifier mod; 827 828 if (str == NULL) 829 return 0; 830 831 if (check_modifier(str)) 832 return -EINVAL; 833 834 if (!add && get_event_modifier(&mod, str, NULL)) 835 return -EINVAL; 836 837 __evlist__for_each(list, evsel) { 838 if (add && get_event_modifier(&mod, str, evsel)) 839 return -EINVAL; 840 841 evsel->attr.exclude_user = mod.eu; 842 evsel->attr.exclude_kernel = mod.ek; 843 evsel->attr.exclude_hv = mod.eh; 844 evsel->attr.precise_ip = mod.precise; 845 evsel->attr.exclude_host = mod.eH; 846 evsel->attr.exclude_guest = mod.eG; 847 evsel->exclude_GH = mod.exclude_GH; 848 evsel->sample_read = mod.sample_read; 849 850 if (perf_evsel__is_group_leader(evsel)) 851 evsel->attr.pinned = mod.pinned; 852 } 853 854 return 0; 855 } 856 857 int parse_events_name(struct list_head *list, char *name) 858 { 859 struct perf_evsel *evsel; 860 861 __evlist__for_each(list, evsel) { 862 if (!evsel->name) 863 evsel->name = strdup(name); 864 } 865 866 return 0; 867 } 868 869 static int 870 comp_pmu(const void *p1, const void *p2) 871 { 872 struct perf_pmu_event_symbol *pmu1 = (struct perf_pmu_event_symbol *) p1; 873 struct perf_pmu_event_symbol *pmu2 = (struct perf_pmu_event_symbol *) p2; 874 875 return strcmp(pmu1->symbol, pmu2->symbol); 876 } 877 878 static void perf_pmu__parse_cleanup(void) 879 { 880 if (perf_pmu_events_list_num > 0) { 881 struct perf_pmu_event_symbol *p; 882 int i; 883 884 for (i = 0; i < perf_pmu_events_list_num; i++) { 885 p = perf_pmu_events_list + i; 886 free(p->symbol); 887 } 888 free(perf_pmu_events_list); 889 perf_pmu_events_list = NULL; 890 perf_pmu_events_list_num = 0; 891 } 892 } 893 894 #define SET_SYMBOL(str, stype) \ 895 do { \ 896 p->symbol = str; \ 897 if (!p->symbol) \ 898 goto err; \ 899 p->type = stype; \ 900 } while (0) 901 902 /* 903 * Read the pmu events list from sysfs 904 * Save it into perf_pmu_events_list 905 */ 906 static void perf_pmu__parse_init(void) 907 { 908 909 struct perf_pmu *pmu = NULL; 910 struct perf_pmu_alias *alias; 911 int len = 0; 912 913 pmu = perf_pmu__find("cpu"); 914 if ((pmu == NULL) || list_empty(&pmu->aliases)) { 915 perf_pmu_events_list_num = -1; 916 return; 917 } 918 list_for_each_entry(alias, &pmu->aliases, list) { 919 if (strchr(alias->name, '-')) 920 len++; 921 len++; 922 } 923 perf_pmu_events_list = malloc(sizeof(struct perf_pmu_event_symbol) * len); 924 if (!perf_pmu_events_list) 925 return; 926 perf_pmu_events_list_num = len; 927 928 len = 0; 929 list_for_each_entry(alias, &pmu->aliases, list) { 930 struct perf_pmu_event_symbol *p = perf_pmu_events_list + len; 931 char *tmp = strchr(alias->name, '-'); 932 933 if (tmp != NULL) { 934 SET_SYMBOL(strndup(alias->name, tmp - alias->name), 935 PMU_EVENT_SYMBOL_PREFIX); 936 p++; 937 SET_SYMBOL(strdup(++tmp), PMU_EVENT_SYMBOL_SUFFIX); 938 len += 2; 939 } else { 940 SET_SYMBOL(strdup(alias->name), PMU_EVENT_SYMBOL); 941 len++; 942 } 943 } 944 qsort(perf_pmu_events_list, len, 945 sizeof(struct perf_pmu_event_symbol), comp_pmu); 946 947 return; 948 err: 949 perf_pmu__parse_cleanup(); 950 } 951 952 enum perf_pmu_event_symbol_type 953 perf_pmu__parse_check(const char *name) 954 { 955 struct perf_pmu_event_symbol p, *r; 956 957 /* scan kernel pmu events from sysfs if needed */ 958 if (perf_pmu_events_list_num == 0) 959 perf_pmu__parse_init(); 960 /* 961 * name "cpu" could be prefix of cpu-cycles or cpu// events. 962 * cpu-cycles has been handled by hardcode. 963 * So it must be cpu// events, not kernel pmu event. 964 */ 965 if ((perf_pmu_events_list_num <= 0) || !strcmp(name, "cpu")) 966 return PMU_EVENT_SYMBOL_ERR; 967 968 p.symbol = strdup(name); 969 r = bsearch(&p, perf_pmu_events_list, 970 (size_t) perf_pmu_events_list_num, 971 sizeof(struct perf_pmu_event_symbol), comp_pmu); 972 free(p.symbol); 973 return r ? r->type : PMU_EVENT_SYMBOL_ERR; 974 } 975 976 static int parse_events__scanner(const char *str, void *data, int start_token) 977 { 978 YY_BUFFER_STATE buffer; 979 void *scanner; 980 int ret; 981 982 ret = parse_events_lex_init_extra(start_token, &scanner); 983 if (ret) 984 return ret; 985 986 buffer = parse_events__scan_string(str, scanner); 987 988 #ifdef PARSER_DEBUG 989 parse_events_debug = 1; 990 #endif 991 ret = parse_events_parse(data, scanner); 992 993 parse_events__flush_buffer(buffer, scanner); 994 parse_events__delete_buffer(buffer, scanner); 995 parse_events_lex_destroy(scanner); 996 return ret; 997 } 998 999 /* 1000 * parse event config string, return a list of event terms. 1001 */ 1002 int parse_events_terms(struct list_head *terms, const char *str) 1003 { 1004 struct parse_events_terms data = { 1005 .terms = NULL, 1006 }; 1007 int ret; 1008 1009 ret = parse_events__scanner(str, &data, PE_START_TERMS); 1010 if (!ret) { 1011 list_splice(data.terms, terms); 1012 zfree(&data.terms); 1013 return 0; 1014 } 1015 1016 if (data.terms) 1017 parse_events__free_terms(data.terms); 1018 return ret; 1019 } 1020 1021 int parse_events(struct perf_evlist *evlist, const char *str) 1022 { 1023 struct parse_events_evlist data = { 1024 .list = LIST_HEAD_INIT(data.list), 1025 .idx = evlist->nr_entries, 1026 }; 1027 int ret; 1028 1029 ret = parse_events__scanner(str, &data, PE_START_EVENTS); 1030 perf_pmu__parse_cleanup(); 1031 if (!ret) { 1032 int entries = data.idx - evlist->nr_entries; 1033 perf_evlist__splice_list_tail(evlist, &data.list, entries); 1034 evlist->nr_groups += data.nr_groups; 1035 return 0; 1036 } 1037 1038 /* 1039 * There are 2 users - builtin-record and builtin-test objects. 1040 * Both call perf_evlist__delete in case of error, so we dont 1041 * need to bother. 1042 */ 1043 return ret; 1044 } 1045 1046 int parse_events_option(const struct option *opt, const char *str, 1047 int unset __maybe_unused) 1048 { 1049 struct perf_evlist *evlist = *(struct perf_evlist **)opt->value; 1050 int ret = parse_events(evlist, str); 1051 1052 if (ret) { 1053 fprintf(stderr, "invalid or unsupported event: '%s'\n", str); 1054 fprintf(stderr, "Run 'perf list' for a list of valid events\n"); 1055 } 1056 return ret; 1057 } 1058 1059 int parse_filter(const struct option *opt, const char *str, 1060 int unset __maybe_unused) 1061 { 1062 struct perf_evlist *evlist = *(struct perf_evlist **)opt->value; 1063 struct perf_evsel *last = NULL; 1064 1065 if (evlist->nr_entries > 0) 1066 last = perf_evlist__last(evlist); 1067 1068 if (last == NULL || last->attr.type != PERF_TYPE_TRACEPOINT) { 1069 fprintf(stderr, 1070 "--filter option should follow a -e tracepoint option\n"); 1071 return -1; 1072 } 1073 1074 last->filter = strdup(str); 1075 if (last->filter == NULL) { 1076 fprintf(stderr, "not enough memory to hold filter string\n"); 1077 return -1; 1078 } 1079 1080 return 0; 1081 } 1082 1083 static const char * const event_type_descriptors[] = { 1084 "Hardware event", 1085 "Software event", 1086 "Tracepoint event", 1087 "Hardware cache event", 1088 "Raw hardware event descriptor", 1089 "Hardware breakpoint", 1090 }; 1091 1092 /* 1093 * Print the events from <debugfs_mount_point>/tracing/events 1094 */ 1095 1096 void print_tracepoint_events(const char *subsys_glob, const char *event_glob, 1097 bool name_only) 1098 { 1099 DIR *sys_dir, *evt_dir; 1100 struct dirent *sys_next, *evt_next, sys_dirent, evt_dirent; 1101 char evt_path[MAXPATHLEN]; 1102 char dir_path[MAXPATHLEN]; 1103 1104 sys_dir = opendir(tracing_events_path); 1105 if (!sys_dir) 1106 return; 1107 1108 for_each_subsystem(sys_dir, sys_dirent, sys_next) { 1109 if (subsys_glob != NULL && 1110 !strglobmatch(sys_dirent.d_name, subsys_glob)) 1111 continue; 1112 1113 snprintf(dir_path, MAXPATHLEN, "%s/%s", tracing_events_path, 1114 sys_dirent.d_name); 1115 evt_dir = opendir(dir_path); 1116 if (!evt_dir) 1117 continue; 1118 1119 for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next) { 1120 if (event_glob != NULL && 1121 !strglobmatch(evt_dirent.d_name, event_glob)) 1122 continue; 1123 1124 if (name_only) { 1125 printf("%s:%s ", sys_dirent.d_name, evt_dirent.d_name); 1126 continue; 1127 } 1128 1129 snprintf(evt_path, MAXPATHLEN, "%s:%s", 1130 sys_dirent.d_name, evt_dirent.d_name); 1131 printf(" %-50s [%s]\n", evt_path, 1132 event_type_descriptors[PERF_TYPE_TRACEPOINT]); 1133 } 1134 closedir(evt_dir); 1135 } 1136 closedir(sys_dir); 1137 } 1138 1139 /* 1140 * Check whether event is in <debugfs_mount_point>/tracing/events 1141 */ 1142 1143 int is_valid_tracepoint(const char *event_string) 1144 { 1145 DIR *sys_dir, *evt_dir; 1146 struct dirent *sys_next, *evt_next, sys_dirent, evt_dirent; 1147 char evt_path[MAXPATHLEN]; 1148 char dir_path[MAXPATHLEN]; 1149 1150 sys_dir = opendir(tracing_events_path); 1151 if (!sys_dir) 1152 return 0; 1153 1154 for_each_subsystem(sys_dir, sys_dirent, sys_next) { 1155 1156 snprintf(dir_path, MAXPATHLEN, "%s/%s", tracing_events_path, 1157 sys_dirent.d_name); 1158 evt_dir = opendir(dir_path); 1159 if (!evt_dir) 1160 continue; 1161 1162 for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next) { 1163 snprintf(evt_path, MAXPATHLEN, "%s:%s", 1164 sys_dirent.d_name, evt_dirent.d_name); 1165 if (!strcmp(evt_path, event_string)) { 1166 closedir(evt_dir); 1167 closedir(sys_dir); 1168 return 1; 1169 } 1170 } 1171 closedir(evt_dir); 1172 } 1173 closedir(sys_dir); 1174 return 0; 1175 } 1176 1177 static bool is_event_supported(u8 type, unsigned config) 1178 { 1179 bool ret = true; 1180 int open_return; 1181 struct perf_evsel *evsel; 1182 struct perf_event_attr attr = { 1183 .type = type, 1184 .config = config, 1185 .disabled = 1, 1186 }; 1187 struct { 1188 struct thread_map map; 1189 int threads[1]; 1190 } tmap = { 1191 .map.nr = 1, 1192 .threads = { 0 }, 1193 }; 1194 1195 evsel = perf_evsel__new(&attr); 1196 if (evsel) { 1197 open_return = perf_evsel__open(evsel, NULL, &tmap.map); 1198 ret = open_return >= 0; 1199 1200 if (open_return == -EACCES) { 1201 /* 1202 * This happens if the paranoid value 1203 * /proc/sys/kernel/perf_event_paranoid is set to 2 1204 * Re-run with exclude_kernel set; we don't do that 1205 * by default as some ARM machines do not support it. 1206 * 1207 */ 1208 evsel->attr.exclude_kernel = 1; 1209 ret = perf_evsel__open(evsel, NULL, &tmap.map) >= 0; 1210 } 1211 perf_evsel__delete(evsel); 1212 } 1213 1214 return ret; 1215 } 1216 1217 static void __print_events_type(u8 type, struct event_symbol *syms, 1218 unsigned max) 1219 { 1220 char name[64]; 1221 unsigned i; 1222 1223 for (i = 0; i < max ; i++, syms++) { 1224 if (!is_event_supported(type, i)) 1225 continue; 1226 1227 if (strlen(syms->alias)) 1228 snprintf(name, sizeof(name), "%s OR %s", 1229 syms->symbol, syms->alias); 1230 else 1231 snprintf(name, sizeof(name), "%s", syms->symbol); 1232 1233 printf(" %-50s [%s]\n", name, event_type_descriptors[type]); 1234 } 1235 } 1236 1237 void print_events_type(u8 type) 1238 { 1239 if (type == PERF_TYPE_SOFTWARE) 1240 __print_events_type(type, event_symbols_sw, PERF_COUNT_SW_MAX); 1241 else 1242 __print_events_type(type, event_symbols_hw, PERF_COUNT_HW_MAX); 1243 } 1244 1245 int print_hwcache_events(const char *event_glob, bool name_only) 1246 { 1247 unsigned int type, op, i, printed = 0; 1248 char name[64]; 1249 1250 for (type = 0; type < PERF_COUNT_HW_CACHE_MAX; type++) { 1251 for (op = 0; op < PERF_COUNT_HW_CACHE_OP_MAX; op++) { 1252 /* skip invalid cache type */ 1253 if (!perf_evsel__is_cache_op_valid(type, op)) 1254 continue; 1255 1256 for (i = 0; i < PERF_COUNT_HW_CACHE_RESULT_MAX; i++) { 1257 __perf_evsel__hw_cache_type_op_res_name(type, op, i, 1258 name, sizeof(name)); 1259 if (event_glob != NULL && !strglobmatch(name, event_glob)) 1260 continue; 1261 1262 if (!is_event_supported(PERF_TYPE_HW_CACHE, 1263 type | (op << 8) | (i << 16))) 1264 continue; 1265 1266 if (name_only) 1267 printf("%s ", name); 1268 else 1269 printf(" %-50s [%s]\n", name, 1270 event_type_descriptors[PERF_TYPE_HW_CACHE]); 1271 ++printed; 1272 } 1273 } 1274 } 1275 1276 if (printed) 1277 printf("\n"); 1278 return printed; 1279 } 1280 1281 static void print_symbol_events(const char *event_glob, unsigned type, 1282 struct event_symbol *syms, unsigned max, 1283 bool name_only) 1284 { 1285 unsigned i, printed = 0; 1286 char name[MAX_NAME_LEN]; 1287 1288 for (i = 0; i < max; i++, syms++) { 1289 1290 if (event_glob != NULL && 1291 !(strglobmatch(syms->symbol, event_glob) || 1292 (syms->alias && strglobmatch(syms->alias, event_glob)))) 1293 continue; 1294 1295 if (!is_event_supported(type, i)) 1296 continue; 1297 1298 if (name_only) { 1299 printf("%s ", syms->symbol); 1300 continue; 1301 } 1302 1303 if (strlen(syms->alias)) 1304 snprintf(name, MAX_NAME_LEN, "%s OR %s", syms->symbol, syms->alias); 1305 else 1306 strncpy(name, syms->symbol, MAX_NAME_LEN); 1307 1308 printf(" %-50s [%s]\n", name, event_type_descriptors[type]); 1309 1310 printed++; 1311 } 1312 1313 if (printed) 1314 printf("\n"); 1315 } 1316 1317 /* 1318 * Print the help text for the event symbols: 1319 */ 1320 void print_events(const char *event_glob, bool name_only) 1321 { 1322 if (!name_only) { 1323 printf("\n"); 1324 printf("List of pre-defined events (to be used in -e):\n"); 1325 } 1326 1327 print_symbol_events(event_glob, PERF_TYPE_HARDWARE, 1328 event_symbols_hw, PERF_COUNT_HW_MAX, name_only); 1329 1330 print_symbol_events(event_glob, PERF_TYPE_SOFTWARE, 1331 event_symbols_sw, PERF_COUNT_SW_MAX, name_only); 1332 1333 print_hwcache_events(event_glob, name_only); 1334 1335 print_pmu_events(event_glob, name_only); 1336 1337 if (event_glob != NULL) 1338 return; 1339 1340 if (!name_only) { 1341 printf(" %-50s [%s]\n", 1342 "rNNN", 1343 event_type_descriptors[PERF_TYPE_RAW]); 1344 printf(" %-50s [%s]\n", 1345 "cpu/t1=v1[,t2=v2,t3 ...]/modifier", 1346 event_type_descriptors[PERF_TYPE_RAW]); 1347 printf(" (see 'man perf-list' on how to encode it)\n"); 1348 printf("\n"); 1349 1350 printf(" %-50s [%s]\n", 1351 "mem:<addr>[/len][:access]", 1352 event_type_descriptors[PERF_TYPE_BREAKPOINT]); 1353 printf("\n"); 1354 } 1355 1356 print_tracepoint_events(NULL, NULL, name_only); 1357 } 1358 1359 int parse_events__is_hardcoded_term(struct parse_events_term *term) 1360 { 1361 return term->type_term != PARSE_EVENTS__TERM_TYPE_USER; 1362 } 1363 1364 static int new_term(struct parse_events_term **_term, int type_val, 1365 int type_term, char *config, 1366 char *str, u64 num) 1367 { 1368 struct parse_events_term *term; 1369 1370 term = zalloc(sizeof(*term)); 1371 if (!term) 1372 return -ENOMEM; 1373 1374 INIT_LIST_HEAD(&term->list); 1375 term->type_val = type_val; 1376 term->type_term = type_term; 1377 term->config = config; 1378 1379 switch (type_val) { 1380 case PARSE_EVENTS__TERM_TYPE_NUM: 1381 term->val.num = num; 1382 break; 1383 case PARSE_EVENTS__TERM_TYPE_STR: 1384 term->val.str = str; 1385 break; 1386 default: 1387 free(term); 1388 return -EINVAL; 1389 } 1390 1391 *_term = term; 1392 return 0; 1393 } 1394 1395 int parse_events_term__num(struct parse_events_term **term, 1396 int type_term, char *config, u64 num) 1397 { 1398 return new_term(term, PARSE_EVENTS__TERM_TYPE_NUM, type_term, 1399 config, NULL, num); 1400 } 1401 1402 int parse_events_term__str(struct parse_events_term **term, 1403 int type_term, char *config, char *str) 1404 { 1405 return new_term(term, PARSE_EVENTS__TERM_TYPE_STR, type_term, 1406 config, str, 0); 1407 } 1408 1409 int parse_events_term__sym_hw(struct parse_events_term **term, 1410 char *config, unsigned idx) 1411 { 1412 struct event_symbol *sym; 1413 1414 BUG_ON(idx >= PERF_COUNT_HW_MAX); 1415 sym = &event_symbols_hw[idx]; 1416 1417 if (config) 1418 return new_term(term, PARSE_EVENTS__TERM_TYPE_STR, 1419 PARSE_EVENTS__TERM_TYPE_USER, config, 1420 (char *) sym->symbol, 0); 1421 else 1422 return new_term(term, PARSE_EVENTS__TERM_TYPE_STR, 1423 PARSE_EVENTS__TERM_TYPE_USER, 1424 (char *) "event", (char *) sym->symbol, 0); 1425 } 1426 1427 int parse_events_term__clone(struct parse_events_term **new, 1428 struct parse_events_term *term) 1429 { 1430 return new_term(new, term->type_val, term->type_term, term->config, 1431 term->val.str, term->val.num); 1432 } 1433 1434 void parse_events__free_terms(struct list_head *terms) 1435 { 1436 struct parse_events_term *term, *h; 1437 1438 list_for_each_entry_safe(term, h, terms, list) 1439 free(term); 1440 } 1441