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