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