1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * auxtrace.c: AUX area trace support 4 * Copyright (c) 2013-2015, Intel Corporation. 5 */ 6 7 #include <inttypes.h> 8 #include <sys/types.h> 9 #include <sys/mman.h> 10 #include <stdbool.h> 11 #include <string.h> 12 #include <limits.h> 13 #include <errno.h> 14 15 #include <linux/kernel.h> 16 #include <linux/perf_event.h> 17 #include <linux/types.h> 18 #include <linux/bitops.h> 19 #include <linux/log2.h> 20 #include <linux/string.h> 21 #include <linux/time64.h> 22 23 #include <sys/param.h> 24 #include <stdlib.h> 25 #include <stdio.h> 26 #include <linux/list.h> 27 #include <linux/zalloc.h> 28 29 #include "evlist.h" 30 #include "dso.h" 31 #include "map.h" 32 #include "pmu.h" 33 #include "evsel.h" 34 #include "symbol.h" 35 #include "util/synthetic-events.h" 36 #include "thread_map.h" 37 #include "asm/bug.h" 38 #include "auxtrace.h" 39 40 #include <linux/hash.h> 41 42 #include "event.h" 43 #include "record.h" 44 #include "session.h" 45 #include "debug.h" 46 #include <subcmd/parse-options.h> 47 48 #include "cs-etm.h" 49 #include "intel-pt.h" 50 #include "intel-bts.h" 51 #include "arm-spe.h" 52 #include "s390-cpumsf.h" 53 #include "util/mmap.h" 54 55 #include <linux/ctype.h> 56 #include <linux/kernel.h> 57 #include "symbol/kallsyms.h" 58 #include <internal/lib.h> 59 60 static bool auxtrace__dont_decode(struct perf_session *session) 61 { 62 return !session->itrace_synth_opts || 63 session->itrace_synth_opts->dont_decode; 64 } 65 66 int auxtrace_mmap__mmap(struct auxtrace_mmap *mm, 67 struct auxtrace_mmap_params *mp, 68 void *userpg, int fd) 69 { 70 struct perf_event_mmap_page *pc = userpg; 71 72 WARN_ONCE(mm->base, "Uninitialized auxtrace_mmap\n"); 73 74 mm->userpg = userpg; 75 mm->mask = mp->mask; 76 mm->len = mp->len; 77 mm->prev = 0; 78 mm->idx = mp->idx; 79 mm->tid = mp->tid; 80 mm->cpu = mp->cpu; 81 82 if (!mp->len) { 83 mm->base = NULL; 84 return 0; 85 } 86 87 #if BITS_PER_LONG != 64 && !defined(HAVE_SYNC_COMPARE_AND_SWAP_SUPPORT) 88 pr_err("Cannot use AUX area tracing mmaps\n"); 89 return -1; 90 #endif 91 92 pc->aux_offset = mp->offset; 93 pc->aux_size = mp->len; 94 95 mm->base = mmap(NULL, mp->len, mp->prot, MAP_SHARED, fd, mp->offset); 96 if (mm->base == MAP_FAILED) { 97 pr_debug2("failed to mmap AUX area\n"); 98 mm->base = NULL; 99 return -1; 100 } 101 102 return 0; 103 } 104 105 void auxtrace_mmap__munmap(struct auxtrace_mmap *mm) 106 { 107 if (mm->base) { 108 munmap(mm->base, mm->len); 109 mm->base = NULL; 110 } 111 } 112 113 void auxtrace_mmap_params__init(struct auxtrace_mmap_params *mp, 114 off_t auxtrace_offset, 115 unsigned int auxtrace_pages, 116 bool auxtrace_overwrite) 117 { 118 if (auxtrace_pages) { 119 mp->offset = auxtrace_offset; 120 mp->len = auxtrace_pages * (size_t)page_size; 121 mp->mask = is_power_of_2(mp->len) ? mp->len - 1 : 0; 122 mp->prot = PROT_READ | (auxtrace_overwrite ? 0 : PROT_WRITE); 123 pr_debug2("AUX area mmap length %zu\n", mp->len); 124 } else { 125 mp->len = 0; 126 } 127 } 128 129 void auxtrace_mmap_params__set_idx(struct auxtrace_mmap_params *mp, 130 struct evlist *evlist, int idx, 131 bool per_cpu) 132 { 133 mp->idx = idx; 134 135 if (per_cpu) { 136 mp->cpu = evlist->core.cpus->map[idx]; 137 if (evlist->core.threads) 138 mp->tid = perf_thread_map__pid(evlist->core.threads, 0); 139 else 140 mp->tid = -1; 141 } else { 142 mp->cpu = -1; 143 mp->tid = perf_thread_map__pid(evlist->core.threads, idx); 144 } 145 } 146 147 #define AUXTRACE_INIT_NR_QUEUES 32 148 149 static struct auxtrace_queue *auxtrace_alloc_queue_array(unsigned int nr_queues) 150 { 151 struct auxtrace_queue *queue_array; 152 unsigned int max_nr_queues, i; 153 154 max_nr_queues = UINT_MAX / sizeof(struct auxtrace_queue); 155 if (nr_queues > max_nr_queues) 156 return NULL; 157 158 queue_array = calloc(nr_queues, sizeof(struct auxtrace_queue)); 159 if (!queue_array) 160 return NULL; 161 162 for (i = 0; i < nr_queues; i++) { 163 INIT_LIST_HEAD(&queue_array[i].head); 164 queue_array[i].priv = NULL; 165 } 166 167 return queue_array; 168 } 169 170 int auxtrace_queues__init(struct auxtrace_queues *queues) 171 { 172 queues->nr_queues = AUXTRACE_INIT_NR_QUEUES; 173 queues->queue_array = auxtrace_alloc_queue_array(queues->nr_queues); 174 if (!queues->queue_array) 175 return -ENOMEM; 176 return 0; 177 } 178 179 static int auxtrace_queues__grow(struct auxtrace_queues *queues, 180 unsigned int new_nr_queues) 181 { 182 unsigned int nr_queues = queues->nr_queues; 183 struct auxtrace_queue *queue_array; 184 unsigned int i; 185 186 if (!nr_queues) 187 nr_queues = AUXTRACE_INIT_NR_QUEUES; 188 189 while (nr_queues && nr_queues < new_nr_queues) 190 nr_queues <<= 1; 191 192 if (nr_queues < queues->nr_queues || nr_queues < new_nr_queues) 193 return -EINVAL; 194 195 queue_array = auxtrace_alloc_queue_array(nr_queues); 196 if (!queue_array) 197 return -ENOMEM; 198 199 for (i = 0; i < queues->nr_queues; i++) { 200 list_splice_tail(&queues->queue_array[i].head, 201 &queue_array[i].head); 202 queue_array[i].tid = queues->queue_array[i].tid; 203 queue_array[i].cpu = queues->queue_array[i].cpu; 204 queue_array[i].set = queues->queue_array[i].set; 205 queue_array[i].priv = queues->queue_array[i].priv; 206 } 207 208 queues->nr_queues = nr_queues; 209 queues->queue_array = queue_array; 210 211 return 0; 212 } 213 214 static void *auxtrace_copy_data(u64 size, struct perf_session *session) 215 { 216 int fd = perf_data__fd(session->data); 217 void *p; 218 ssize_t ret; 219 220 if (size > SSIZE_MAX) 221 return NULL; 222 223 p = malloc(size); 224 if (!p) 225 return NULL; 226 227 ret = readn(fd, p, size); 228 if (ret != (ssize_t)size) { 229 free(p); 230 return NULL; 231 } 232 233 return p; 234 } 235 236 static int auxtrace_queues__queue_buffer(struct auxtrace_queues *queues, 237 unsigned int idx, 238 struct auxtrace_buffer *buffer) 239 { 240 struct auxtrace_queue *queue; 241 int err; 242 243 if (idx >= queues->nr_queues) { 244 err = auxtrace_queues__grow(queues, idx + 1); 245 if (err) 246 return err; 247 } 248 249 queue = &queues->queue_array[idx]; 250 251 if (!queue->set) { 252 queue->set = true; 253 queue->tid = buffer->tid; 254 queue->cpu = buffer->cpu; 255 } else if (buffer->cpu != queue->cpu || buffer->tid != queue->tid) { 256 pr_err("auxtrace queue conflict: cpu %d, tid %d vs cpu %d, tid %d\n", 257 queue->cpu, queue->tid, buffer->cpu, buffer->tid); 258 return -EINVAL; 259 } 260 261 buffer->buffer_nr = queues->next_buffer_nr++; 262 263 list_add_tail(&buffer->list, &queue->head); 264 265 queues->new_data = true; 266 queues->populated = true; 267 268 return 0; 269 } 270 271 /* Limit buffers to 32MiB on 32-bit */ 272 #define BUFFER_LIMIT_FOR_32_BIT (32 * 1024 * 1024) 273 274 static int auxtrace_queues__split_buffer(struct auxtrace_queues *queues, 275 unsigned int idx, 276 struct auxtrace_buffer *buffer) 277 { 278 u64 sz = buffer->size; 279 bool consecutive = false; 280 struct auxtrace_buffer *b; 281 int err; 282 283 while (sz > BUFFER_LIMIT_FOR_32_BIT) { 284 b = memdup(buffer, sizeof(struct auxtrace_buffer)); 285 if (!b) 286 return -ENOMEM; 287 b->size = BUFFER_LIMIT_FOR_32_BIT; 288 b->consecutive = consecutive; 289 err = auxtrace_queues__queue_buffer(queues, idx, b); 290 if (err) { 291 auxtrace_buffer__free(b); 292 return err; 293 } 294 buffer->data_offset += BUFFER_LIMIT_FOR_32_BIT; 295 sz -= BUFFER_LIMIT_FOR_32_BIT; 296 consecutive = true; 297 } 298 299 buffer->size = sz; 300 buffer->consecutive = consecutive; 301 302 return 0; 303 } 304 305 static bool filter_cpu(struct perf_session *session, int cpu) 306 { 307 unsigned long *cpu_bitmap = session->itrace_synth_opts->cpu_bitmap; 308 309 return cpu_bitmap && cpu != -1 && !test_bit(cpu, cpu_bitmap); 310 } 311 312 static int auxtrace_queues__add_buffer(struct auxtrace_queues *queues, 313 struct perf_session *session, 314 unsigned int idx, 315 struct auxtrace_buffer *buffer, 316 struct auxtrace_buffer **buffer_ptr) 317 { 318 int err = -ENOMEM; 319 320 if (filter_cpu(session, buffer->cpu)) 321 return 0; 322 323 buffer = memdup(buffer, sizeof(*buffer)); 324 if (!buffer) 325 return -ENOMEM; 326 327 if (session->one_mmap) { 328 buffer->data = buffer->data_offset - session->one_mmap_offset + 329 session->one_mmap_addr; 330 } else if (perf_data__is_pipe(session->data)) { 331 buffer->data = auxtrace_copy_data(buffer->size, session); 332 if (!buffer->data) 333 goto out_free; 334 buffer->data_needs_freeing = true; 335 } else if (BITS_PER_LONG == 32 && 336 buffer->size > BUFFER_LIMIT_FOR_32_BIT) { 337 err = auxtrace_queues__split_buffer(queues, idx, buffer); 338 if (err) 339 goto out_free; 340 } 341 342 err = auxtrace_queues__queue_buffer(queues, idx, buffer); 343 if (err) 344 goto out_free; 345 346 /* FIXME: Doesn't work for split buffer */ 347 if (buffer_ptr) 348 *buffer_ptr = buffer; 349 350 return 0; 351 352 out_free: 353 auxtrace_buffer__free(buffer); 354 return err; 355 } 356 357 int auxtrace_queues__add_event(struct auxtrace_queues *queues, 358 struct perf_session *session, 359 union perf_event *event, off_t data_offset, 360 struct auxtrace_buffer **buffer_ptr) 361 { 362 struct auxtrace_buffer buffer = { 363 .pid = -1, 364 .tid = event->auxtrace.tid, 365 .cpu = event->auxtrace.cpu, 366 .data_offset = data_offset, 367 .offset = event->auxtrace.offset, 368 .reference = event->auxtrace.reference, 369 .size = event->auxtrace.size, 370 }; 371 unsigned int idx = event->auxtrace.idx; 372 373 return auxtrace_queues__add_buffer(queues, session, idx, &buffer, 374 buffer_ptr); 375 } 376 377 static int auxtrace_queues__add_indexed_event(struct auxtrace_queues *queues, 378 struct perf_session *session, 379 off_t file_offset, size_t sz) 380 { 381 union perf_event *event; 382 int err; 383 char buf[PERF_SAMPLE_MAX_SIZE]; 384 385 err = perf_session__peek_event(session, file_offset, buf, 386 PERF_SAMPLE_MAX_SIZE, &event, NULL); 387 if (err) 388 return err; 389 390 if (event->header.type == PERF_RECORD_AUXTRACE) { 391 if (event->header.size < sizeof(struct perf_record_auxtrace) || 392 event->header.size != sz) { 393 err = -EINVAL; 394 goto out; 395 } 396 file_offset += event->header.size; 397 err = auxtrace_queues__add_event(queues, session, event, 398 file_offset, NULL); 399 } 400 out: 401 return err; 402 } 403 404 void auxtrace_queues__free(struct auxtrace_queues *queues) 405 { 406 unsigned int i; 407 408 for (i = 0; i < queues->nr_queues; i++) { 409 while (!list_empty(&queues->queue_array[i].head)) { 410 struct auxtrace_buffer *buffer; 411 412 buffer = list_entry(queues->queue_array[i].head.next, 413 struct auxtrace_buffer, list); 414 list_del_init(&buffer->list); 415 auxtrace_buffer__free(buffer); 416 } 417 } 418 419 zfree(&queues->queue_array); 420 queues->nr_queues = 0; 421 } 422 423 static void auxtrace_heapify(struct auxtrace_heap_item *heap_array, 424 unsigned int pos, unsigned int queue_nr, 425 u64 ordinal) 426 { 427 unsigned int parent; 428 429 while (pos) { 430 parent = (pos - 1) >> 1; 431 if (heap_array[parent].ordinal <= ordinal) 432 break; 433 heap_array[pos] = heap_array[parent]; 434 pos = parent; 435 } 436 heap_array[pos].queue_nr = queue_nr; 437 heap_array[pos].ordinal = ordinal; 438 } 439 440 int auxtrace_heap__add(struct auxtrace_heap *heap, unsigned int queue_nr, 441 u64 ordinal) 442 { 443 struct auxtrace_heap_item *heap_array; 444 445 if (queue_nr >= heap->heap_sz) { 446 unsigned int heap_sz = AUXTRACE_INIT_NR_QUEUES; 447 448 while (heap_sz <= queue_nr) 449 heap_sz <<= 1; 450 heap_array = realloc(heap->heap_array, 451 heap_sz * sizeof(struct auxtrace_heap_item)); 452 if (!heap_array) 453 return -ENOMEM; 454 heap->heap_array = heap_array; 455 heap->heap_sz = heap_sz; 456 } 457 458 auxtrace_heapify(heap->heap_array, heap->heap_cnt++, queue_nr, ordinal); 459 460 return 0; 461 } 462 463 void auxtrace_heap__free(struct auxtrace_heap *heap) 464 { 465 zfree(&heap->heap_array); 466 heap->heap_cnt = 0; 467 heap->heap_sz = 0; 468 } 469 470 void auxtrace_heap__pop(struct auxtrace_heap *heap) 471 { 472 unsigned int pos, last, heap_cnt = heap->heap_cnt; 473 struct auxtrace_heap_item *heap_array; 474 475 if (!heap_cnt) 476 return; 477 478 heap->heap_cnt -= 1; 479 480 heap_array = heap->heap_array; 481 482 pos = 0; 483 while (1) { 484 unsigned int left, right; 485 486 left = (pos << 1) + 1; 487 if (left >= heap_cnt) 488 break; 489 right = left + 1; 490 if (right >= heap_cnt) { 491 heap_array[pos] = heap_array[left]; 492 return; 493 } 494 if (heap_array[left].ordinal < heap_array[right].ordinal) { 495 heap_array[pos] = heap_array[left]; 496 pos = left; 497 } else { 498 heap_array[pos] = heap_array[right]; 499 pos = right; 500 } 501 } 502 503 last = heap_cnt - 1; 504 auxtrace_heapify(heap_array, pos, heap_array[last].queue_nr, 505 heap_array[last].ordinal); 506 } 507 508 size_t auxtrace_record__info_priv_size(struct auxtrace_record *itr, 509 struct evlist *evlist) 510 { 511 if (itr) 512 return itr->info_priv_size(itr, evlist); 513 return 0; 514 } 515 516 static int auxtrace_not_supported(void) 517 { 518 pr_err("AUX area tracing is not supported on this architecture\n"); 519 return -EINVAL; 520 } 521 522 int auxtrace_record__info_fill(struct auxtrace_record *itr, 523 struct perf_session *session, 524 struct perf_record_auxtrace_info *auxtrace_info, 525 size_t priv_size) 526 { 527 if (itr) 528 return itr->info_fill(itr, session, auxtrace_info, priv_size); 529 return auxtrace_not_supported(); 530 } 531 532 void auxtrace_record__free(struct auxtrace_record *itr) 533 { 534 if (itr) 535 itr->free(itr); 536 } 537 538 int auxtrace_record__snapshot_start(struct auxtrace_record *itr) 539 { 540 if (itr && itr->snapshot_start) 541 return itr->snapshot_start(itr); 542 return 0; 543 } 544 545 int auxtrace_record__snapshot_finish(struct auxtrace_record *itr, bool on_exit) 546 { 547 if (!on_exit && itr && itr->snapshot_finish) 548 return itr->snapshot_finish(itr); 549 return 0; 550 } 551 552 int auxtrace_record__find_snapshot(struct auxtrace_record *itr, int idx, 553 struct auxtrace_mmap *mm, 554 unsigned char *data, u64 *head, u64 *old) 555 { 556 if (itr && itr->find_snapshot) 557 return itr->find_snapshot(itr, idx, mm, data, head, old); 558 return 0; 559 } 560 561 int auxtrace_record__options(struct auxtrace_record *itr, 562 struct evlist *evlist, 563 struct record_opts *opts) 564 { 565 if (itr) 566 return itr->recording_options(itr, evlist, opts); 567 return 0; 568 } 569 570 u64 auxtrace_record__reference(struct auxtrace_record *itr) 571 { 572 if (itr) 573 return itr->reference(itr); 574 return 0; 575 } 576 577 int auxtrace_parse_snapshot_options(struct auxtrace_record *itr, 578 struct record_opts *opts, const char *str) 579 { 580 if (!str) 581 return 0; 582 583 /* PMU-agnostic options */ 584 switch (*str) { 585 case 'e': 586 opts->auxtrace_snapshot_on_exit = true; 587 str++; 588 break; 589 default: 590 break; 591 } 592 593 if (itr) 594 return itr->parse_snapshot_options(itr, opts, str); 595 596 pr_err("No AUX area tracing to snapshot\n"); 597 return -EINVAL; 598 } 599 600 struct auxtrace_record *__weak 601 auxtrace_record__init(struct evlist *evlist __maybe_unused, int *err) 602 { 603 *err = 0; 604 return NULL; 605 } 606 607 static int auxtrace_index__alloc(struct list_head *head) 608 { 609 struct auxtrace_index *auxtrace_index; 610 611 auxtrace_index = malloc(sizeof(struct auxtrace_index)); 612 if (!auxtrace_index) 613 return -ENOMEM; 614 615 auxtrace_index->nr = 0; 616 INIT_LIST_HEAD(&auxtrace_index->list); 617 618 list_add_tail(&auxtrace_index->list, head); 619 620 return 0; 621 } 622 623 void auxtrace_index__free(struct list_head *head) 624 { 625 struct auxtrace_index *auxtrace_index, *n; 626 627 list_for_each_entry_safe(auxtrace_index, n, head, list) { 628 list_del_init(&auxtrace_index->list); 629 free(auxtrace_index); 630 } 631 } 632 633 static struct auxtrace_index *auxtrace_index__last(struct list_head *head) 634 { 635 struct auxtrace_index *auxtrace_index; 636 int err; 637 638 if (list_empty(head)) { 639 err = auxtrace_index__alloc(head); 640 if (err) 641 return NULL; 642 } 643 644 auxtrace_index = list_entry(head->prev, struct auxtrace_index, list); 645 646 if (auxtrace_index->nr >= PERF_AUXTRACE_INDEX_ENTRY_COUNT) { 647 err = auxtrace_index__alloc(head); 648 if (err) 649 return NULL; 650 auxtrace_index = list_entry(head->prev, struct auxtrace_index, 651 list); 652 } 653 654 return auxtrace_index; 655 } 656 657 int auxtrace_index__auxtrace_event(struct list_head *head, 658 union perf_event *event, off_t file_offset) 659 { 660 struct auxtrace_index *auxtrace_index; 661 size_t nr; 662 663 auxtrace_index = auxtrace_index__last(head); 664 if (!auxtrace_index) 665 return -ENOMEM; 666 667 nr = auxtrace_index->nr; 668 auxtrace_index->entries[nr].file_offset = file_offset; 669 auxtrace_index->entries[nr].sz = event->header.size; 670 auxtrace_index->nr += 1; 671 672 return 0; 673 } 674 675 static int auxtrace_index__do_write(int fd, 676 struct auxtrace_index *auxtrace_index) 677 { 678 struct auxtrace_index_entry ent; 679 size_t i; 680 681 for (i = 0; i < auxtrace_index->nr; i++) { 682 ent.file_offset = auxtrace_index->entries[i].file_offset; 683 ent.sz = auxtrace_index->entries[i].sz; 684 if (writen(fd, &ent, sizeof(ent)) != sizeof(ent)) 685 return -errno; 686 } 687 return 0; 688 } 689 690 int auxtrace_index__write(int fd, struct list_head *head) 691 { 692 struct auxtrace_index *auxtrace_index; 693 u64 total = 0; 694 int err; 695 696 list_for_each_entry(auxtrace_index, head, list) 697 total += auxtrace_index->nr; 698 699 if (writen(fd, &total, sizeof(total)) != sizeof(total)) 700 return -errno; 701 702 list_for_each_entry(auxtrace_index, head, list) { 703 err = auxtrace_index__do_write(fd, auxtrace_index); 704 if (err) 705 return err; 706 } 707 708 return 0; 709 } 710 711 static int auxtrace_index__process_entry(int fd, struct list_head *head, 712 bool needs_swap) 713 { 714 struct auxtrace_index *auxtrace_index; 715 struct auxtrace_index_entry ent; 716 size_t nr; 717 718 if (readn(fd, &ent, sizeof(ent)) != sizeof(ent)) 719 return -1; 720 721 auxtrace_index = auxtrace_index__last(head); 722 if (!auxtrace_index) 723 return -1; 724 725 nr = auxtrace_index->nr; 726 if (needs_swap) { 727 auxtrace_index->entries[nr].file_offset = 728 bswap_64(ent.file_offset); 729 auxtrace_index->entries[nr].sz = bswap_64(ent.sz); 730 } else { 731 auxtrace_index->entries[nr].file_offset = ent.file_offset; 732 auxtrace_index->entries[nr].sz = ent.sz; 733 } 734 735 auxtrace_index->nr = nr + 1; 736 737 return 0; 738 } 739 740 int auxtrace_index__process(int fd, u64 size, struct perf_session *session, 741 bool needs_swap) 742 { 743 struct list_head *head = &session->auxtrace_index; 744 u64 nr; 745 746 if (readn(fd, &nr, sizeof(u64)) != sizeof(u64)) 747 return -1; 748 749 if (needs_swap) 750 nr = bswap_64(nr); 751 752 if (sizeof(u64) + nr * sizeof(struct auxtrace_index_entry) > size) 753 return -1; 754 755 while (nr--) { 756 int err; 757 758 err = auxtrace_index__process_entry(fd, head, needs_swap); 759 if (err) 760 return -1; 761 } 762 763 return 0; 764 } 765 766 static int auxtrace_queues__process_index_entry(struct auxtrace_queues *queues, 767 struct perf_session *session, 768 struct auxtrace_index_entry *ent) 769 { 770 return auxtrace_queues__add_indexed_event(queues, session, 771 ent->file_offset, ent->sz); 772 } 773 774 int auxtrace_queues__process_index(struct auxtrace_queues *queues, 775 struct perf_session *session) 776 { 777 struct auxtrace_index *auxtrace_index; 778 struct auxtrace_index_entry *ent; 779 size_t i; 780 int err; 781 782 if (auxtrace__dont_decode(session)) 783 return 0; 784 785 list_for_each_entry(auxtrace_index, &session->auxtrace_index, list) { 786 for (i = 0; i < auxtrace_index->nr; i++) { 787 ent = &auxtrace_index->entries[i]; 788 err = auxtrace_queues__process_index_entry(queues, 789 session, 790 ent); 791 if (err) 792 return err; 793 } 794 } 795 return 0; 796 } 797 798 struct auxtrace_buffer *auxtrace_buffer__next(struct auxtrace_queue *queue, 799 struct auxtrace_buffer *buffer) 800 { 801 if (buffer) { 802 if (list_is_last(&buffer->list, &queue->head)) 803 return NULL; 804 return list_entry(buffer->list.next, struct auxtrace_buffer, 805 list); 806 } else { 807 if (list_empty(&queue->head)) 808 return NULL; 809 return list_entry(queue->head.next, struct auxtrace_buffer, 810 list); 811 } 812 } 813 814 void *auxtrace_buffer__get_data(struct auxtrace_buffer *buffer, int fd) 815 { 816 size_t adj = buffer->data_offset & (page_size - 1); 817 size_t size = buffer->size + adj; 818 off_t file_offset = buffer->data_offset - adj; 819 void *addr; 820 821 if (buffer->data) 822 return buffer->data; 823 824 addr = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, file_offset); 825 if (addr == MAP_FAILED) 826 return NULL; 827 828 buffer->mmap_addr = addr; 829 buffer->mmap_size = size; 830 831 buffer->data = addr + adj; 832 833 return buffer->data; 834 } 835 836 void auxtrace_buffer__put_data(struct auxtrace_buffer *buffer) 837 { 838 if (!buffer->data || !buffer->mmap_addr) 839 return; 840 munmap(buffer->mmap_addr, buffer->mmap_size); 841 buffer->mmap_addr = NULL; 842 buffer->mmap_size = 0; 843 buffer->data = NULL; 844 buffer->use_data = NULL; 845 } 846 847 void auxtrace_buffer__drop_data(struct auxtrace_buffer *buffer) 848 { 849 auxtrace_buffer__put_data(buffer); 850 if (buffer->data_needs_freeing) { 851 buffer->data_needs_freeing = false; 852 zfree(&buffer->data); 853 buffer->use_data = NULL; 854 buffer->size = 0; 855 } 856 } 857 858 void auxtrace_buffer__free(struct auxtrace_buffer *buffer) 859 { 860 auxtrace_buffer__drop_data(buffer); 861 free(buffer); 862 } 863 864 void auxtrace_synth_error(struct perf_record_auxtrace_error *auxtrace_error, int type, 865 int code, int cpu, pid_t pid, pid_t tid, u64 ip, 866 const char *msg, u64 timestamp) 867 { 868 size_t size; 869 870 memset(auxtrace_error, 0, sizeof(struct perf_record_auxtrace_error)); 871 872 auxtrace_error->header.type = PERF_RECORD_AUXTRACE_ERROR; 873 auxtrace_error->type = type; 874 auxtrace_error->code = code; 875 auxtrace_error->cpu = cpu; 876 auxtrace_error->pid = pid; 877 auxtrace_error->tid = tid; 878 auxtrace_error->fmt = 1; 879 auxtrace_error->ip = ip; 880 auxtrace_error->time = timestamp; 881 strlcpy(auxtrace_error->msg, msg, MAX_AUXTRACE_ERROR_MSG); 882 883 size = (void *)auxtrace_error->msg - (void *)auxtrace_error + 884 strlen(auxtrace_error->msg) + 1; 885 auxtrace_error->header.size = PERF_ALIGN(size, sizeof(u64)); 886 } 887 888 int perf_event__synthesize_auxtrace_info(struct auxtrace_record *itr, 889 struct perf_tool *tool, 890 struct perf_session *session, 891 perf_event__handler_t process) 892 { 893 union perf_event *ev; 894 size_t priv_size; 895 int err; 896 897 pr_debug2("Synthesizing auxtrace information\n"); 898 priv_size = auxtrace_record__info_priv_size(itr, session->evlist); 899 ev = zalloc(sizeof(struct perf_record_auxtrace_info) + priv_size); 900 if (!ev) 901 return -ENOMEM; 902 903 ev->auxtrace_info.header.type = PERF_RECORD_AUXTRACE_INFO; 904 ev->auxtrace_info.header.size = sizeof(struct perf_record_auxtrace_info) + 905 priv_size; 906 err = auxtrace_record__info_fill(itr, session, &ev->auxtrace_info, 907 priv_size); 908 if (err) 909 goto out_free; 910 911 err = process(tool, ev, NULL, NULL); 912 out_free: 913 free(ev); 914 return err; 915 } 916 917 int perf_event__process_auxtrace_info(struct perf_session *session, 918 union perf_event *event) 919 { 920 enum auxtrace_type type = event->auxtrace_info.type; 921 922 if (dump_trace) 923 fprintf(stdout, " type: %u\n", type); 924 925 switch (type) { 926 case PERF_AUXTRACE_INTEL_PT: 927 return intel_pt_process_auxtrace_info(event, session); 928 case PERF_AUXTRACE_INTEL_BTS: 929 return intel_bts_process_auxtrace_info(event, session); 930 case PERF_AUXTRACE_ARM_SPE: 931 return arm_spe_process_auxtrace_info(event, session); 932 case PERF_AUXTRACE_CS_ETM: 933 return cs_etm__process_auxtrace_info(event, session); 934 case PERF_AUXTRACE_S390_CPUMSF: 935 return s390_cpumsf_process_auxtrace_info(event, session); 936 case PERF_AUXTRACE_UNKNOWN: 937 default: 938 return -EINVAL; 939 } 940 } 941 942 s64 perf_event__process_auxtrace(struct perf_session *session, 943 union perf_event *event) 944 { 945 s64 err; 946 947 if (dump_trace) 948 fprintf(stdout, " size: %#"PRI_lx64" offset: %#"PRI_lx64" ref: %#"PRI_lx64" idx: %u tid: %d cpu: %d\n", 949 event->auxtrace.size, event->auxtrace.offset, 950 event->auxtrace.reference, event->auxtrace.idx, 951 event->auxtrace.tid, event->auxtrace.cpu); 952 953 if (auxtrace__dont_decode(session)) 954 return event->auxtrace.size; 955 956 if (!session->auxtrace || event->header.type != PERF_RECORD_AUXTRACE) 957 return -EINVAL; 958 959 err = session->auxtrace->process_auxtrace_event(session, event, session->tool); 960 if (err < 0) 961 return err; 962 963 return event->auxtrace.size; 964 } 965 966 #define PERF_ITRACE_DEFAULT_PERIOD_TYPE PERF_ITRACE_PERIOD_NANOSECS 967 #define PERF_ITRACE_DEFAULT_PERIOD 100000 968 #define PERF_ITRACE_DEFAULT_CALLCHAIN_SZ 16 969 #define PERF_ITRACE_MAX_CALLCHAIN_SZ 1024 970 #define PERF_ITRACE_DEFAULT_LAST_BRANCH_SZ 64 971 #define PERF_ITRACE_MAX_LAST_BRANCH_SZ 1024 972 973 void itrace_synth_opts__set_default(struct itrace_synth_opts *synth_opts, 974 bool no_sample) 975 { 976 synth_opts->branches = true; 977 synth_opts->transactions = true; 978 synth_opts->ptwrites = true; 979 synth_opts->pwr_events = true; 980 synth_opts->other_events = true; 981 synth_opts->errors = true; 982 if (no_sample) { 983 synth_opts->period_type = PERF_ITRACE_PERIOD_INSTRUCTIONS; 984 synth_opts->period = 1; 985 synth_opts->calls = true; 986 } else { 987 synth_opts->instructions = true; 988 synth_opts->period_type = PERF_ITRACE_DEFAULT_PERIOD_TYPE; 989 synth_opts->period = PERF_ITRACE_DEFAULT_PERIOD; 990 } 991 synth_opts->callchain_sz = PERF_ITRACE_DEFAULT_CALLCHAIN_SZ; 992 synth_opts->last_branch_sz = PERF_ITRACE_DEFAULT_LAST_BRANCH_SZ; 993 synth_opts->initial_skip = 0; 994 } 995 996 /* 997 * Please check tools/perf/Documentation/perf-script.txt for information 998 * about the options parsed here, which is introduced after this cset, 999 * when support in 'perf script' for these options is introduced. 1000 */ 1001 int itrace_parse_synth_opts(const struct option *opt, const char *str, 1002 int unset) 1003 { 1004 struct itrace_synth_opts *synth_opts = opt->value; 1005 const char *p; 1006 char *endptr; 1007 bool period_type_set = false; 1008 bool period_set = false; 1009 1010 synth_opts->set = true; 1011 1012 if (unset) { 1013 synth_opts->dont_decode = true; 1014 return 0; 1015 } 1016 1017 if (!str) { 1018 itrace_synth_opts__set_default(synth_opts, 1019 synth_opts->default_no_sample); 1020 return 0; 1021 } 1022 1023 for (p = str; *p;) { 1024 switch (*p++) { 1025 case 'i': 1026 synth_opts->instructions = true; 1027 while (*p == ' ' || *p == ',') 1028 p += 1; 1029 if (isdigit(*p)) { 1030 synth_opts->period = strtoull(p, &endptr, 10); 1031 period_set = true; 1032 p = endptr; 1033 while (*p == ' ' || *p == ',') 1034 p += 1; 1035 switch (*p++) { 1036 case 'i': 1037 synth_opts->period_type = 1038 PERF_ITRACE_PERIOD_INSTRUCTIONS; 1039 period_type_set = true; 1040 break; 1041 case 't': 1042 synth_opts->period_type = 1043 PERF_ITRACE_PERIOD_TICKS; 1044 period_type_set = true; 1045 break; 1046 case 'm': 1047 synth_opts->period *= 1000; 1048 /* Fall through */ 1049 case 'u': 1050 synth_opts->period *= 1000; 1051 /* Fall through */ 1052 case 'n': 1053 if (*p++ != 's') 1054 goto out_err; 1055 synth_opts->period_type = 1056 PERF_ITRACE_PERIOD_NANOSECS; 1057 period_type_set = true; 1058 break; 1059 case '\0': 1060 goto out; 1061 default: 1062 goto out_err; 1063 } 1064 } 1065 break; 1066 case 'b': 1067 synth_opts->branches = true; 1068 break; 1069 case 'x': 1070 synth_opts->transactions = true; 1071 break; 1072 case 'w': 1073 synth_opts->ptwrites = true; 1074 break; 1075 case 'p': 1076 synth_opts->pwr_events = true; 1077 break; 1078 case 'o': 1079 synth_opts->other_events = true; 1080 break; 1081 case 'e': 1082 synth_opts->errors = true; 1083 break; 1084 case 'd': 1085 synth_opts->log = true; 1086 break; 1087 case 'c': 1088 synth_opts->branches = true; 1089 synth_opts->calls = true; 1090 break; 1091 case 'r': 1092 synth_opts->branches = true; 1093 synth_opts->returns = true; 1094 break; 1095 case 'g': 1096 synth_opts->callchain = true; 1097 synth_opts->callchain_sz = 1098 PERF_ITRACE_DEFAULT_CALLCHAIN_SZ; 1099 while (*p == ' ' || *p == ',') 1100 p += 1; 1101 if (isdigit(*p)) { 1102 unsigned int val; 1103 1104 val = strtoul(p, &endptr, 10); 1105 p = endptr; 1106 if (!val || val > PERF_ITRACE_MAX_CALLCHAIN_SZ) 1107 goto out_err; 1108 synth_opts->callchain_sz = val; 1109 } 1110 break; 1111 case 'l': 1112 synth_opts->last_branch = true; 1113 synth_opts->last_branch_sz = 1114 PERF_ITRACE_DEFAULT_LAST_BRANCH_SZ; 1115 while (*p == ' ' || *p == ',') 1116 p += 1; 1117 if (isdigit(*p)) { 1118 unsigned int val; 1119 1120 val = strtoul(p, &endptr, 10); 1121 p = endptr; 1122 if (!val || 1123 val > PERF_ITRACE_MAX_LAST_BRANCH_SZ) 1124 goto out_err; 1125 synth_opts->last_branch_sz = val; 1126 } 1127 break; 1128 case 's': 1129 synth_opts->initial_skip = strtoul(p, &endptr, 10); 1130 if (p == endptr) 1131 goto out_err; 1132 p = endptr; 1133 break; 1134 case ' ': 1135 case ',': 1136 break; 1137 default: 1138 goto out_err; 1139 } 1140 } 1141 out: 1142 if (synth_opts->instructions) { 1143 if (!period_type_set) 1144 synth_opts->period_type = 1145 PERF_ITRACE_DEFAULT_PERIOD_TYPE; 1146 if (!period_set) 1147 synth_opts->period = PERF_ITRACE_DEFAULT_PERIOD; 1148 } 1149 1150 return 0; 1151 1152 out_err: 1153 pr_err("Bad Instruction Tracing options '%s'\n", str); 1154 return -EINVAL; 1155 } 1156 1157 static const char * const auxtrace_error_type_name[] = { 1158 [PERF_AUXTRACE_ERROR_ITRACE] = "instruction trace", 1159 }; 1160 1161 static const char *auxtrace_error_name(int type) 1162 { 1163 const char *error_type_name = NULL; 1164 1165 if (type < PERF_AUXTRACE_ERROR_MAX) 1166 error_type_name = auxtrace_error_type_name[type]; 1167 if (!error_type_name) 1168 error_type_name = "unknown AUX"; 1169 return error_type_name; 1170 } 1171 1172 size_t perf_event__fprintf_auxtrace_error(union perf_event *event, FILE *fp) 1173 { 1174 struct perf_record_auxtrace_error *e = &event->auxtrace_error; 1175 unsigned long long nsecs = e->time; 1176 const char *msg = e->msg; 1177 int ret; 1178 1179 ret = fprintf(fp, " %s error type %u", 1180 auxtrace_error_name(e->type), e->type); 1181 1182 if (e->fmt && nsecs) { 1183 unsigned long secs = nsecs / NSEC_PER_SEC; 1184 1185 nsecs -= secs * NSEC_PER_SEC; 1186 ret += fprintf(fp, " time %lu.%09llu", secs, nsecs); 1187 } else { 1188 ret += fprintf(fp, " time 0"); 1189 } 1190 1191 if (!e->fmt) 1192 msg = (const char *)&e->time; 1193 1194 ret += fprintf(fp, " cpu %d pid %d tid %d ip %#"PRI_lx64" code %u: %s\n", 1195 e->cpu, e->pid, e->tid, e->ip, e->code, msg); 1196 return ret; 1197 } 1198 1199 void perf_session__auxtrace_error_inc(struct perf_session *session, 1200 union perf_event *event) 1201 { 1202 struct perf_record_auxtrace_error *e = &event->auxtrace_error; 1203 1204 if (e->type < PERF_AUXTRACE_ERROR_MAX) 1205 session->evlist->stats.nr_auxtrace_errors[e->type] += 1; 1206 } 1207 1208 void events_stats__auxtrace_error_warn(const struct events_stats *stats) 1209 { 1210 int i; 1211 1212 for (i = 0; i < PERF_AUXTRACE_ERROR_MAX; i++) { 1213 if (!stats->nr_auxtrace_errors[i]) 1214 continue; 1215 ui__warning("%u %s errors\n", 1216 stats->nr_auxtrace_errors[i], 1217 auxtrace_error_name(i)); 1218 } 1219 } 1220 1221 int perf_event__process_auxtrace_error(struct perf_session *session, 1222 union perf_event *event) 1223 { 1224 if (auxtrace__dont_decode(session)) 1225 return 0; 1226 1227 perf_event__fprintf_auxtrace_error(event, stdout); 1228 return 0; 1229 } 1230 1231 static int __auxtrace_mmap__read(struct mmap *map, 1232 struct auxtrace_record *itr, 1233 struct perf_tool *tool, process_auxtrace_t fn, 1234 bool snapshot, size_t snapshot_size) 1235 { 1236 struct auxtrace_mmap *mm = &map->auxtrace_mmap; 1237 u64 head, old = mm->prev, offset, ref; 1238 unsigned char *data = mm->base; 1239 size_t size, head_off, old_off, len1, len2, padding; 1240 union perf_event ev; 1241 void *data1, *data2; 1242 1243 if (snapshot) { 1244 head = auxtrace_mmap__read_snapshot_head(mm); 1245 if (auxtrace_record__find_snapshot(itr, mm->idx, mm, data, 1246 &head, &old)) 1247 return -1; 1248 } else { 1249 head = auxtrace_mmap__read_head(mm); 1250 } 1251 1252 if (old == head) 1253 return 0; 1254 1255 pr_debug3("auxtrace idx %d old %#"PRIx64" head %#"PRIx64" diff %#"PRIx64"\n", 1256 mm->idx, old, head, head - old); 1257 1258 if (mm->mask) { 1259 head_off = head & mm->mask; 1260 old_off = old & mm->mask; 1261 } else { 1262 head_off = head % mm->len; 1263 old_off = old % mm->len; 1264 } 1265 1266 if (head_off > old_off) 1267 size = head_off - old_off; 1268 else 1269 size = mm->len - (old_off - head_off); 1270 1271 if (snapshot && size > snapshot_size) 1272 size = snapshot_size; 1273 1274 ref = auxtrace_record__reference(itr); 1275 1276 if (head > old || size <= head || mm->mask) { 1277 offset = head - size; 1278 } else { 1279 /* 1280 * When the buffer size is not a power of 2, 'head' wraps at the 1281 * highest multiple of the buffer size, so we have to subtract 1282 * the remainder here. 1283 */ 1284 u64 rem = (0ULL - mm->len) % mm->len; 1285 1286 offset = head - size - rem; 1287 } 1288 1289 if (size > head_off) { 1290 len1 = size - head_off; 1291 data1 = &data[mm->len - len1]; 1292 len2 = head_off; 1293 data2 = &data[0]; 1294 } else { 1295 len1 = size; 1296 data1 = &data[head_off - len1]; 1297 len2 = 0; 1298 data2 = NULL; 1299 } 1300 1301 if (itr->alignment) { 1302 unsigned int unwanted = len1 % itr->alignment; 1303 1304 len1 -= unwanted; 1305 size -= unwanted; 1306 } 1307 1308 /* padding must be written by fn() e.g. record__process_auxtrace() */ 1309 padding = size & (PERF_AUXTRACE_RECORD_ALIGNMENT - 1); 1310 if (padding) 1311 padding = PERF_AUXTRACE_RECORD_ALIGNMENT - padding; 1312 1313 memset(&ev, 0, sizeof(ev)); 1314 ev.auxtrace.header.type = PERF_RECORD_AUXTRACE; 1315 ev.auxtrace.header.size = sizeof(ev.auxtrace); 1316 ev.auxtrace.size = size + padding; 1317 ev.auxtrace.offset = offset; 1318 ev.auxtrace.reference = ref; 1319 ev.auxtrace.idx = mm->idx; 1320 ev.auxtrace.tid = mm->tid; 1321 ev.auxtrace.cpu = mm->cpu; 1322 1323 if (fn(tool, map, &ev, data1, len1, data2, len2)) 1324 return -1; 1325 1326 mm->prev = head; 1327 1328 if (!snapshot) { 1329 auxtrace_mmap__write_tail(mm, head); 1330 if (itr->read_finish) { 1331 int err; 1332 1333 err = itr->read_finish(itr, mm->idx); 1334 if (err < 0) 1335 return err; 1336 } 1337 } 1338 1339 return 1; 1340 } 1341 1342 int auxtrace_mmap__read(struct mmap *map, struct auxtrace_record *itr, 1343 struct perf_tool *tool, process_auxtrace_t fn) 1344 { 1345 return __auxtrace_mmap__read(map, itr, tool, fn, false, 0); 1346 } 1347 1348 int auxtrace_mmap__read_snapshot(struct mmap *map, 1349 struct auxtrace_record *itr, 1350 struct perf_tool *tool, process_auxtrace_t fn, 1351 size_t snapshot_size) 1352 { 1353 return __auxtrace_mmap__read(map, itr, tool, fn, true, snapshot_size); 1354 } 1355 1356 /** 1357 * struct auxtrace_cache - hash table to implement a cache 1358 * @hashtable: the hashtable 1359 * @sz: hashtable size (number of hlists) 1360 * @entry_size: size of an entry 1361 * @limit: limit the number of entries to this maximum, when reached the cache 1362 * is dropped and caching begins again with an empty cache 1363 * @cnt: current number of entries 1364 * @bits: hashtable size (@sz = 2^@bits) 1365 */ 1366 struct auxtrace_cache { 1367 struct hlist_head *hashtable; 1368 size_t sz; 1369 size_t entry_size; 1370 size_t limit; 1371 size_t cnt; 1372 unsigned int bits; 1373 }; 1374 1375 struct auxtrace_cache *auxtrace_cache__new(unsigned int bits, size_t entry_size, 1376 unsigned int limit_percent) 1377 { 1378 struct auxtrace_cache *c; 1379 struct hlist_head *ht; 1380 size_t sz, i; 1381 1382 c = zalloc(sizeof(struct auxtrace_cache)); 1383 if (!c) 1384 return NULL; 1385 1386 sz = 1UL << bits; 1387 1388 ht = calloc(sz, sizeof(struct hlist_head)); 1389 if (!ht) 1390 goto out_free; 1391 1392 for (i = 0; i < sz; i++) 1393 INIT_HLIST_HEAD(&ht[i]); 1394 1395 c->hashtable = ht; 1396 c->sz = sz; 1397 c->entry_size = entry_size; 1398 c->limit = (c->sz * limit_percent) / 100; 1399 c->bits = bits; 1400 1401 return c; 1402 1403 out_free: 1404 free(c); 1405 return NULL; 1406 } 1407 1408 static void auxtrace_cache__drop(struct auxtrace_cache *c) 1409 { 1410 struct auxtrace_cache_entry *entry; 1411 struct hlist_node *tmp; 1412 size_t i; 1413 1414 if (!c) 1415 return; 1416 1417 for (i = 0; i < c->sz; i++) { 1418 hlist_for_each_entry_safe(entry, tmp, &c->hashtable[i], hash) { 1419 hlist_del(&entry->hash); 1420 auxtrace_cache__free_entry(c, entry); 1421 } 1422 } 1423 1424 c->cnt = 0; 1425 } 1426 1427 void auxtrace_cache__free(struct auxtrace_cache *c) 1428 { 1429 if (!c) 1430 return; 1431 1432 auxtrace_cache__drop(c); 1433 zfree(&c->hashtable); 1434 free(c); 1435 } 1436 1437 void *auxtrace_cache__alloc_entry(struct auxtrace_cache *c) 1438 { 1439 return malloc(c->entry_size); 1440 } 1441 1442 void auxtrace_cache__free_entry(struct auxtrace_cache *c __maybe_unused, 1443 void *entry) 1444 { 1445 free(entry); 1446 } 1447 1448 int auxtrace_cache__add(struct auxtrace_cache *c, u32 key, 1449 struct auxtrace_cache_entry *entry) 1450 { 1451 if (c->limit && ++c->cnt > c->limit) 1452 auxtrace_cache__drop(c); 1453 1454 entry->key = key; 1455 hlist_add_head(&entry->hash, &c->hashtable[hash_32(key, c->bits)]); 1456 1457 return 0; 1458 } 1459 1460 static struct auxtrace_cache_entry *auxtrace_cache__rm(struct auxtrace_cache *c, 1461 u32 key) 1462 { 1463 struct auxtrace_cache_entry *entry; 1464 struct hlist_head *hlist; 1465 struct hlist_node *n; 1466 1467 if (!c) 1468 return NULL; 1469 1470 hlist = &c->hashtable[hash_32(key, c->bits)]; 1471 hlist_for_each_entry_safe(entry, n, hlist, hash) { 1472 if (entry->key == key) { 1473 hlist_del(&entry->hash); 1474 return entry; 1475 } 1476 } 1477 1478 return NULL; 1479 } 1480 1481 void auxtrace_cache__remove(struct auxtrace_cache *c, u32 key) 1482 { 1483 struct auxtrace_cache_entry *entry = auxtrace_cache__rm(c, key); 1484 1485 auxtrace_cache__free_entry(c, entry); 1486 } 1487 1488 void *auxtrace_cache__lookup(struct auxtrace_cache *c, u32 key) 1489 { 1490 struct auxtrace_cache_entry *entry; 1491 struct hlist_head *hlist; 1492 1493 if (!c) 1494 return NULL; 1495 1496 hlist = &c->hashtable[hash_32(key, c->bits)]; 1497 hlist_for_each_entry(entry, hlist, hash) { 1498 if (entry->key == key) 1499 return entry; 1500 } 1501 1502 return NULL; 1503 } 1504 1505 static void addr_filter__free_str(struct addr_filter *filt) 1506 { 1507 zfree(&filt->str); 1508 filt->action = NULL; 1509 filt->sym_from = NULL; 1510 filt->sym_to = NULL; 1511 filt->filename = NULL; 1512 } 1513 1514 static struct addr_filter *addr_filter__new(void) 1515 { 1516 struct addr_filter *filt = zalloc(sizeof(*filt)); 1517 1518 if (filt) 1519 INIT_LIST_HEAD(&filt->list); 1520 1521 return filt; 1522 } 1523 1524 static void addr_filter__free(struct addr_filter *filt) 1525 { 1526 if (filt) 1527 addr_filter__free_str(filt); 1528 free(filt); 1529 } 1530 1531 static void addr_filters__add(struct addr_filters *filts, 1532 struct addr_filter *filt) 1533 { 1534 list_add_tail(&filt->list, &filts->head); 1535 filts->cnt += 1; 1536 } 1537 1538 static void addr_filters__del(struct addr_filters *filts, 1539 struct addr_filter *filt) 1540 { 1541 list_del_init(&filt->list); 1542 filts->cnt -= 1; 1543 } 1544 1545 void addr_filters__init(struct addr_filters *filts) 1546 { 1547 INIT_LIST_HEAD(&filts->head); 1548 filts->cnt = 0; 1549 } 1550 1551 void addr_filters__exit(struct addr_filters *filts) 1552 { 1553 struct addr_filter *filt, *n; 1554 1555 list_for_each_entry_safe(filt, n, &filts->head, list) { 1556 addr_filters__del(filts, filt); 1557 addr_filter__free(filt); 1558 } 1559 } 1560 1561 static int parse_num_or_str(char **inp, u64 *num, const char **str, 1562 const char *str_delim) 1563 { 1564 *inp += strspn(*inp, " "); 1565 1566 if (isdigit(**inp)) { 1567 char *endptr; 1568 1569 if (!num) 1570 return -EINVAL; 1571 errno = 0; 1572 *num = strtoull(*inp, &endptr, 0); 1573 if (errno) 1574 return -errno; 1575 if (endptr == *inp) 1576 return -EINVAL; 1577 *inp = endptr; 1578 } else { 1579 size_t n; 1580 1581 if (!str) 1582 return -EINVAL; 1583 *inp += strspn(*inp, " "); 1584 *str = *inp; 1585 n = strcspn(*inp, str_delim); 1586 if (!n) 1587 return -EINVAL; 1588 *inp += n; 1589 if (**inp) { 1590 **inp = '\0'; 1591 *inp += 1; 1592 } 1593 } 1594 return 0; 1595 } 1596 1597 static int parse_action(struct addr_filter *filt) 1598 { 1599 if (!strcmp(filt->action, "filter")) { 1600 filt->start = true; 1601 filt->range = true; 1602 } else if (!strcmp(filt->action, "start")) { 1603 filt->start = true; 1604 } else if (!strcmp(filt->action, "stop")) { 1605 filt->start = false; 1606 } else if (!strcmp(filt->action, "tracestop")) { 1607 filt->start = false; 1608 filt->range = true; 1609 filt->action += 5; /* Change 'tracestop' to 'stop' */ 1610 } else { 1611 return -EINVAL; 1612 } 1613 return 0; 1614 } 1615 1616 static int parse_sym_idx(char **inp, int *idx) 1617 { 1618 *idx = -1; 1619 1620 *inp += strspn(*inp, " "); 1621 1622 if (**inp != '#') 1623 return 0; 1624 1625 *inp += 1; 1626 1627 if (**inp == 'g' || **inp == 'G') { 1628 *inp += 1; 1629 *idx = 0; 1630 } else { 1631 unsigned long num; 1632 char *endptr; 1633 1634 errno = 0; 1635 num = strtoul(*inp, &endptr, 0); 1636 if (errno) 1637 return -errno; 1638 if (endptr == *inp || num > INT_MAX) 1639 return -EINVAL; 1640 *inp = endptr; 1641 *idx = num; 1642 } 1643 1644 return 0; 1645 } 1646 1647 static int parse_addr_size(char **inp, u64 *num, const char **str, int *idx) 1648 { 1649 int err = parse_num_or_str(inp, num, str, " "); 1650 1651 if (!err && *str) 1652 err = parse_sym_idx(inp, idx); 1653 1654 return err; 1655 } 1656 1657 static int parse_one_filter(struct addr_filter *filt, const char **filter_inp) 1658 { 1659 char *fstr; 1660 int err; 1661 1662 filt->str = fstr = strdup(*filter_inp); 1663 if (!fstr) 1664 return -ENOMEM; 1665 1666 err = parse_num_or_str(&fstr, NULL, &filt->action, " "); 1667 if (err) 1668 goto out_err; 1669 1670 err = parse_action(filt); 1671 if (err) 1672 goto out_err; 1673 1674 err = parse_addr_size(&fstr, &filt->addr, &filt->sym_from, 1675 &filt->sym_from_idx); 1676 if (err) 1677 goto out_err; 1678 1679 fstr += strspn(fstr, " "); 1680 1681 if (*fstr == '/') { 1682 fstr += 1; 1683 err = parse_addr_size(&fstr, &filt->size, &filt->sym_to, 1684 &filt->sym_to_idx); 1685 if (err) 1686 goto out_err; 1687 filt->range = true; 1688 } 1689 1690 fstr += strspn(fstr, " "); 1691 1692 if (*fstr == '@') { 1693 fstr += 1; 1694 err = parse_num_or_str(&fstr, NULL, &filt->filename, " ,"); 1695 if (err) 1696 goto out_err; 1697 } 1698 1699 fstr += strspn(fstr, " ,"); 1700 1701 *filter_inp += fstr - filt->str; 1702 1703 return 0; 1704 1705 out_err: 1706 addr_filter__free_str(filt); 1707 1708 return err; 1709 } 1710 1711 int addr_filters__parse_bare_filter(struct addr_filters *filts, 1712 const char *filter) 1713 { 1714 struct addr_filter *filt; 1715 const char *fstr = filter; 1716 int err; 1717 1718 while (*fstr) { 1719 filt = addr_filter__new(); 1720 err = parse_one_filter(filt, &fstr); 1721 if (err) { 1722 addr_filter__free(filt); 1723 addr_filters__exit(filts); 1724 return err; 1725 } 1726 addr_filters__add(filts, filt); 1727 } 1728 1729 return 0; 1730 } 1731 1732 struct sym_args { 1733 const char *name; 1734 u64 start; 1735 u64 size; 1736 int idx; 1737 int cnt; 1738 bool started; 1739 bool global; 1740 bool selected; 1741 bool duplicate; 1742 bool near; 1743 }; 1744 1745 static bool kern_sym_match(struct sym_args *args, const char *name, char type) 1746 { 1747 /* A function with the same name, and global or the n'th found or any */ 1748 return kallsyms__is_function(type) && 1749 !strcmp(name, args->name) && 1750 ((args->global && isupper(type)) || 1751 (args->selected && ++(args->cnt) == args->idx) || 1752 (!args->global && !args->selected)); 1753 } 1754 1755 static int find_kern_sym_cb(void *arg, const char *name, char type, u64 start) 1756 { 1757 struct sym_args *args = arg; 1758 1759 if (args->started) { 1760 if (!args->size) 1761 args->size = start - args->start; 1762 if (args->selected) { 1763 if (args->size) 1764 return 1; 1765 } else if (kern_sym_match(args, name, type)) { 1766 args->duplicate = true; 1767 return 1; 1768 } 1769 } else if (kern_sym_match(args, name, type)) { 1770 args->started = true; 1771 args->start = start; 1772 } 1773 1774 return 0; 1775 } 1776 1777 static int print_kern_sym_cb(void *arg, const char *name, char type, u64 start) 1778 { 1779 struct sym_args *args = arg; 1780 1781 if (kern_sym_match(args, name, type)) { 1782 pr_err("#%d\t0x%"PRIx64"\t%c\t%s\n", 1783 ++args->cnt, start, type, name); 1784 args->near = true; 1785 } else if (args->near) { 1786 args->near = false; 1787 pr_err("\t\twhich is near\t\t%s\n", name); 1788 } 1789 1790 return 0; 1791 } 1792 1793 static int sym_not_found_error(const char *sym_name, int idx) 1794 { 1795 if (idx > 0) { 1796 pr_err("N'th occurrence (N=%d) of symbol '%s' not found.\n", 1797 idx, sym_name); 1798 } else if (!idx) { 1799 pr_err("Global symbol '%s' not found.\n", sym_name); 1800 } else { 1801 pr_err("Symbol '%s' not found.\n", sym_name); 1802 } 1803 pr_err("Note that symbols must be functions.\n"); 1804 1805 return -EINVAL; 1806 } 1807 1808 static int find_kern_sym(const char *sym_name, u64 *start, u64 *size, int idx) 1809 { 1810 struct sym_args args = { 1811 .name = sym_name, 1812 .idx = idx, 1813 .global = !idx, 1814 .selected = idx > 0, 1815 }; 1816 int err; 1817 1818 *start = 0; 1819 *size = 0; 1820 1821 err = kallsyms__parse("/proc/kallsyms", &args, find_kern_sym_cb); 1822 if (err < 0) { 1823 pr_err("Failed to parse /proc/kallsyms\n"); 1824 return err; 1825 } 1826 1827 if (args.duplicate) { 1828 pr_err("Multiple kernel symbols with name '%s'\n", sym_name); 1829 args.cnt = 0; 1830 kallsyms__parse("/proc/kallsyms", &args, print_kern_sym_cb); 1831 pr_err("Disambiguate symbol name by inserting #n after the name e.g. %s #2\n", 1832 sym_name); 1833 pr_err("Or select a global symbol by inserting #0 or #g or #G\n"); 1834 return -EINVAL; 1835 } 1836 1837 if (!args.started) { 1838 pr_err("Kernel symbol lookup: "); 1839 return sym_not_found_error(sym_name, idx); 1840 } 1841 1842 *start = args.start; 1843 *size = args.size; 1844 1845 return 0; 1846 } 1847 1848 static int find_entire_kern_cb(void *arg, const char *name __maybe_unused, 1849 char type, u64 start) 1850 { 1851 struct sym_args *args = arg; 1852 1853 if (!kallsyms__is_function(type)) 1854 return 0; 1855 1856 if (!args->started) { 1857 args->started = true; 1858 args->start = start; 1859 } 1860 /* Don't know exactly where the kernel ends, so we add a page */ 1861 args->size = round_up(start, page_size) + page_size - args->start; 1862 1863 return 0; 1864 } 1865 1866 static int addr_filter__entire_kernel(struct addr_filter *filt) 1867 { 1868 struct sym_args args = { .started = false }; 1869 int err; 1870 1871 err = kallsyms__parse("/proc/kallsyms", &args, find_entire_kern_cb); 1872 if (err < 0 || !args.started) { 1873 pr_err("Failed to parse /proc/kallsyms\n"); 1874 return err; 1875 } 1876 1877 filt->addr = args.start; 1878 filt->size = args.size; 1879 1880 return 0; 1881 } 1882 1883 static int check_end_after_start(struct addr_filter *filt, u64 start, u64 size) 1884 { 1885 if (start + size >= filt->addr) 1886 return 0; 1887 1888 if (filt->sym_from) { 1889 pr_err("Symbol '%s' (0x%"PRIx64") comes before '%s' (0x%"PRIx64")\n", 1890 filt->sym_to, start, filt->sym_from, filt->addr); 1891 } else { 1892 pr_err("Symbol '%s' (0x%"PRIx64") comes before address 0x%"PRIx64")\n", 1893 filt->sym_to, start, filt->addr); 1894 } 1895 1896 return -EINVAL; 1897 } 1898 1899 static int addr_filter__resolve_kernel_syms(struct addr_filter *filt) 1900 { 1901 bool no_size = false; 1902 u64 start, size; 1903 int err; 1904 1905 if (symbol_conf.kptr_restrict) { 1906 pr_err("Kernel addresses are restricted. Unable to resolve kernel symbols.\n"); 1907 return -EINVAL; 1908 } 1909 1910 if (filt->sym_from && !strcmp(filt->sym_from, "*")) 1911 return addr_filter__entire_kernel(filt); 1912 1913 if (filt->sym_from) { 1914 err = find_kern_sym(filt->sym_from, &start, &size, 1915 filt->sym_from_idx); 1916 if (err) 1917 return err; 1918 filt->addr = start; 1919 if (filt->range && !filt->size && !filt->sym_to) { 1920 filt->size = size; 1921 no_size = !size; 1922 } 1923 } 1924 1925 if (filt->sym_to) { 1926 err = find_kern_sym(filt->sym_to, &start, &size, 1927 filt->sym_to_idx); 1928 if (err) 1929 return err; 1930 1931 err = check_end_after_start(filt, start, size); 1932 if (err) 1933 return err; 1934 filt->size = start + size - filt->addr; 1935 no_size = !size; 1936 } 1937 1938 /* The very last symbol in kallsyms does not imply a particular size */ 1939 if (no_size) { 1940 pr_err("Cannot determine size of symbol '%s'\n", 1941 filt->sym_to ? filt->sym_to : filt->sym_from); 1942 return -EINVAL; 1943 } 1944 1945 return 0; 1946 } 1947 1948 static struct dso *load_dso(const char *name) 1949 { 1950 struct map *map; 1951 struct dso *dso; 1952 1953 map = dso__new_map(name); 1954 if (!map) 1955 return NULL; 1956 1957 if (map__load(map) < 0) 1958 pr_err("File '%s' not found or has no symbols.\n", name); 1959 1960 dso = dso__get(map->dso); 1961 1962 map__put(map); 1963 1964 return dso; 1965 } 1966 1967 static bool dso_sym_match(struct symbol *sym, const char *name, int *cnt, 1968 int idx) 1969 { 1970 /* Same name, and global or the n'th found or any */ 1971 return !arch__compare_symbol_names(name, sym->name) && 1972 ((!idx && sym->binding == STB_GLOBAL) || 1973 (idx > 0 && ++*cnt == idx) || 1974 idx < 0); 1975 } 1976 1977 static void print_duplicate_syms(struct dso *dso, const char *sym_name) 1978 { 1979 struct symbol *sym; 1980 bool near = false; 1981 int cnt = 0; 1982 1983 pr_err("Multiple symbols with name '%s'\n", sym_name); 1984 1985 sym = dso__first_symbol(dso); 1986 while (sym) { 1987 if (dso_sym_match(sym, sym_name, &cnt, -1)) { 1988 pr_err("#%d\t0x%"PRIx64"\t%c\t%s\n", 1989 ++cnt, sym->start, 1990 sym->binding == STB_GLOBAL ? 'g' : 1991 sym->binding == STB_LOCAL ? 'l' : 'w', 1992 sym->name); 1993 near = true; 1994 } else if (near) { 1995 near = false; 1996 pr_err("\t\twhich is near\t\t%s\n", sym->name); 1997 } 1998 sym = dso__next_symbol(sym); 1999 } 2000 2001 pr_err("Disambiguate symbol name by inserting #n after the name e.g. %s #2\n", 2002 sym_name); 2003 pr_err("Or select a global symbol by inserting #0 or #g or #G\n"); 2004 } 2005 2006 static int find_dso_sym(struct dso *dso, const char *sym_name, u64 *start, 2007 u64 *size, int idx) 2008 { 2009 struct symbol *sym; 2010 int cnt = 0; 2011 2012 *start = 0; 2013 *size = 0; 2014 2015 sym = dso__first_symbol(dso); 2016 while (sym) { 2017 if (*start) { 2018 if (!*size) 2019 *size = sym->start - *start; 2020 if (idx > 0) { 2021 if (*size) 2022 return 1; 2023 } else if (dso_sym_match(sym, sym_name, &cnt, idx)) { 2024 print_duplicate_syms(dso, sym_name); 2025 return -EINVAL; 2026 } 2027 } else if (dso_sym_match(sym, sym_name, &cnt, idx)) { 2028 *start = sym->start; 2029 *size = sym->end - sym->start; 2030 } 2031 sym = dso__next_symbol(sym); 2032 } 2033 2034 if (!*start) 2035 return sym_not_found_error(sym_name, idx); 2036 2037 return 0; 2038 } 2039 2040 static int addr_filter__entire_dso(struct addr_filter *filt, struct dso *dso) 2041 { 2042 if (dso__data_file_size(dso, NULL)) { 2043 pr_err("Failed to determine filter for %s\nCannot determine file size.\n", 2044 filt->filename); 2045 return -EINVAL; 2046 } 2047 2048 filt->addr = 0; 2049 filt->size = dso->data.file_size; 2050 2051 return 0; 2052 } 2053 2054 static int addr_filter__resolve_syms(struct addr_filter *filt) 2055 { 2056 u64 start, size; 2057 struct dso *dso; 2058 int err = 0; 2059 2060 if (!filt->sym_from && !filt->sym_to) 2061 return 0; 2062 2063 if (!filt->filename) 2064 return addr_filter__resolve_kernel_syms(filt); 2065 2066 dso = load_dso(filt->filename); 2067 if (!dso) { 2068 pr_err("Failed to load symbols from: %s\n", filt->filename); 2069 return -EINVAL; 2070 } 2071 2072 if (filt->sym_from && !strcmp(filt->sym_from, "*")) { 2073 err = addr_filter__entire_dso(filt, dso); 2074 goto put_dso; 2075 } 2076 2077 if (filt->sym_from) { 2078 err = find_dso_sym(dso, filt->sym_from, &start, &size, 2079 filt->sym_from_idx); 2080 if (err) 2081 goto put_dso; 2082 filt->addr = start; 2083 if (filt->range && !filt->size && !filt->sym_to) 2084 filt->size = size; 2085 } 2086 2087 if (filt->sym_to) { 2088 err = find_dso_sym(dso, filt->sym_to, &start, &size, 2089 filt->sym_to_idx); 2090 if (err) 2091 goto put_dso; 2092 2093 err = check_end_after_start(filt, start, size); 2094 if (err) 2095 return err; 2096 2097 filt->size = start + size - filt->addr; 2098 } 2099 2100 put_dso: 2101 dso__put(dso); 2102 2103 return err; 2104 } 2105 2106 static char *addr_filter__to_str(struct addr_filter *filt) 2107 { 2108 char filename_buf[PATH_MAX]; 2109 const char *at = ""; 2110 const char *fn = ""; 2111 char *filter; 2112 int err; 2113 2114 if (filt->filename) { 2115 at = "@"; 2116 fn = realpath(filt->filename, filename_buf); 2117 if (!fn) 2118 return NULL; 2119 } 2120 2121 if (filt->range) { 2122 err = asprintf(&filter, "%s 0x%"PRIx64"/0x%"PRIx64"%s%s", 2123 filt->action, filt->addr, filt->size, at, fn); 2124 } else { 2125 err = asprintf(&filter, "%s 0x%"PRIx64"%s%s", 2126 filt->action, filt->addr, at, fn); 2127 } 2128 2129 return err < 0 ? NULL : filter; 2130 } 2131 2132 static int parse_addr_filter(struct evsel *evsel, const char *filter, 2133 int max_nr) 2134 { 2135 struct addr_filters filts; 2136 struct addr_filter *filt; 2137 int err; 2138 2139 addr_filters__init(&filts); 2140 2141 err = addr_filters__parse_bare_filter(&filts, filter); 2142 if (err) 2143 goto out_exit; 2144 2145 if (filts.cnt > max_nr) { 2146 pr_err("Error: number of address filters (%d) exceeds maximum (%d)\n", 2147 filts.cnt, max_nr); 2148 err = -EINVAL; 2149 goto out_exit; 2150 } 2151 2152 list_for_each_entry(filt, &filts.head, list) { 2153 char *new_filter; 2154 2155 err = addr_filter__resolve_syms(filt); 2156 if (err) 2157 goto out_exit; 2158 2159 new_filter = addr_filter__to_str(filt); 2160 if (!new_filter) { 2161 err = -ENOMEM; 2162 goto out_exit; 2163 } 2164 2165 if (perf_evsel__append_addr_filter(evsel, new_filter)) { 2166 err = -ENOMEM; 2167 goto out_exit; 2168 } 2169 } 2170 2171 out_exit: 2172 addr_filters__exit(&filts); 2173 2174 if (err) { 2175 pr_err("Failed to parse address filter: '%s'\n", filter); 2176 pr_err("Filter format is: filter|start|stop|tracestop <start symbol or address> [/ <end symbol or size>] [@<file name>]\n"); 2177 pr_err("Where multiple filters are separated by space or comma.\n"); 2178 } 2179 2180 return err; 2181 } 2182 2183 static struct perf_pmu *perf_evsel__find_pmu(struct evsel *evsel) 2184 { 2185 struct perf_pmu *pmu = NULL; 2186 2187 while ((pmu = perf_pmu__scan(pmu)) != NULL) { 2188 if (pmu->type == evsel->core.attr.type) 2189 break; 2190 } 2191 2192 return pmu; 2193 } 2194 2195 static int perf_evsel__nr_addr_filter(struct evsel *evsel) 2196 { 2197 struct perf_pmu *pmu = perf_evsel__find_pmu(evsel); 2198 int nr_addr_filters = 0; 2199 2200 if (!pmu) 2201 return 0; 2202 2203 perf_pmu__scan_file(pmu, "nr_addr_filters", "%d", &nr_addr_filters); 2204 2205 return nr_addr_filters; 2206 } 2207 2208 int auxtrace_parse_filters(struct evlist *evlist) 2209 { 2210 struct evsel *evsel; 2211 char *filter; 2212 int err, max_nr; 2213 2214 evlist__for_each_entry(evlist, evsel) { 2215 filter = evsel->filter; 2216 max_nr = perf_evsel__nr_addr_filter(evsel); 2217 if (!filter || !max_nr) 2218 continue; 2219 evsel->filter = NULL; 2220 err = parse_addr_filter(evsel, filter, max_nr); 2221 free(filter); 2222 if (err) 2223 return err; 2224 pr_debug("Address filter: %s\n", evsel->filter); 2225 } 2226 2227 return 0; 2228 } 2229 2230 int auxtrace__process_event(struct perf_session *session, union perf_event *event, 2231 struct perf_sample *sample, struct perf_tool *tool) 2232 { 2233 if (!session->auxtrace) 2234 return 0; 2235 2236 return session->auxtrace->process_event(session, event, sample, tool); 2237 } 2238 2239 int auxtrace__flush_events(struct perf_session *session, struct perf_tool *tool) 2240 { 2241 if (!session->auxtrace) 2242 return 0; 2243 2244 return session->auxtrace->flush_events(session, tool); 2245 } 2246 2247 void auxtrace__free_events(struct perf_session *session) 2248 { 2249 if (!session->auxtrace) 2250 return; 2251 2252 return session->auxtrace->free_events(session); 2253 } 2254 2255 void auxtrace__free(struct perf_session *session) 2256 { 2257 if (!session->auxtrace) 2258 return; 2259 2260 return session->auxtrace->free(session); 2261 } 2262