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