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 #include "s390-cpumsf.h" 60 61 #include "sane_ctype.h" 62 #include "symbol/kallsyms.h" 63 64 static bool auxtrace__dont_decode(struct perf_session *session) 65 { 66 return !session->itrace_synth_opts || 67 session->itrace_synth_opts->dont_decode; 68 } 69 70 int auxtrace_mmap__mmap(struct auxtrace_mmap *mm, 71 struct auxtrace_mmap_params *mp, 72 void *userpg, int fd) 73 { 74 struct perf_event_mmap_page *pc = userpg; 75 76 WARN_ONCE(mm->base, "Uninitialized auxtrace_mmap\n"); 77 78 mm->userpg = userpg; 79 mm->mask = mp->mask; 80 mm->len = mp->len; 81 mm->prev = 0; 82 mm->idx = mp->idx; 83 mm->tid = mp->tid; 84 mm->cpu = mp->cpu; 85 86 if (!mp->len) { 87 mm->base = NULL; 88 return 0; 89 } 90 91 #if BITS_PER_LONG != 64 && !defined(HAVE_SYNC_COMPARE_AND_SWAP_SUPPORT) 92 pr_err("Cannot use AUX area tracing mmaps\n"); 93 return -1; 94 #endif 95 96 pc->aux_offset = mp->offset; 97 pc->aux_size = mp->len; 98 99 mm->base = mmap(NULL, mp->len, mp->prot, MAP_SHARED, fd, mp->offset); 100 if (mm->base == MAP_FAILED) { 101 pr_debug2("failed to mmap AUX area\n"); 102 mm->base = NULL; 103 return -1; 104 } 105 106 return 0; 107 } 108 109 void auxtrace_mmap__munmap(struct auxtrace_mmap *mm) 110 { 111 if (mm->base) { 112 munmap(mm->base, mm->len); 113 mm->base = NULL; 114 } 115 } 116 117 void auxtrace_mmap_params__init(struct auxtrace_mmap_params *mp, 118 off_t auxtrace_offset, 119 unsigned int auxtrace_pages, 120 bool auxtrace_overwrite) 121 { 122 if (auxtrace_pages) { 123 mp->offset = auxtrace_offset; 124 mp->len = auxtrace_pages * (size_t)page_size; 125 mp->mask = is_power_of_2(mp->len) ? mp->len - 1 : 0; 126 mp->prot = PROT_READ | (auxtrace_overwrite ? 0 : PROT_WRITE); 127 pr_debug2("AUX area mmap length %zu\n", mp->len); 128 } else { 129 mp->len = 0; 130 } 131 } 132 133 void auxtrace_mmap_params__set_idx(struct auxtrace_mmap_params *mp, 134 struct perf_evlist *evlist, int idx, 135 bool per_cpu) 136 { 137 mp->idx = idx; 138 139 if (per_cpu) { 140 mp->cpu = evlist->cpus->map[idx]; 141 if (evlist->threads) 142 mp->tid = thread_map__pid(evlist->threads, 0); 143 else 144 mp->tid = -1; 145 } else { 146 mp->cpu = -1; 147 mp->tid = thread_map__pid(evlist->threads, idx); 148 } 149 } 150 151 #define AUXTRACE_INIT_NR_QUEUES 32 152 153 static struct auxtrace_queue *auxtrace_alloc_queue_array(unsigned int nr_queues) 154 { 155 struct auxtrace_queue *queue_array; 156 unsigned int max_nr_queues, i; 157 158 max_nr_queues = UINT_MAX / sizeof(struct auxtrace_queue); 159 if (nr_queues > max_nr_queues) 160 return NULL; 161 162 queue_array = calloc(nr_queues, sizeof(struct auxtrace_queue)); 163 if (!queue_array) 164 return NULL; 165 166 for (i = 0; i < nr_queues; i++) { 167 INIT_LIST_HEAD(&queue_array[i].head); 168 queue_array[i].priv = NULL; 169 } 170 171 return queue_array; 172 } 173 174 int auxtrace_queues__init(struct auxtrace_queues *queues) 175 { 176 queues->nr_queues = AUXTRACE_INIT_NR_QUEUES; 177 queues->queue_array = auxtrace_alloc_queue_array(queues->nr_queues); 178 if (!queues->queue_array) 179 return -ENOMEM; 180 return 0; 181 } 182 183 static int auxtrace_queues__grow(struct auxtrace_queues *queues, 184 unsigned int new_nr_queues) 185 { 186 unsigned int nr_queues = queues->nr_queues; 187 struct auxtrace_queue *queue_array; 188 unsigned int i; 189 190 if (!nr_queues) 191 nr_queues = AUXTRACE_INIT_NR_QUEUES; 192 193 while (nr_queues && nr_queues < new_nr_queues) 194 nr_queues <<= 1; 195 196 if (nr_queues < queues->nr_queues || nr_queues < new_nr_queues) 197 return -EINVAL; 198 199 queue_array = auxtrace_alloc_queue_array(nr_queues); 200 if (!queue_array) 201 return -ENOMEM; 202 203 for (i = 0; i < queues->nr_queues; i++) { 204 list_splice_tail(&queues->queue_array[i].head, 205 &queue_array[i].head); 206 queue_array[i].tid = queues->queue_array[i].tid; 207 queue_array[i].cpu = queues->queue_array[i].cpu; 208 queue_array[i].set = queues->queue_array[i].set; 209 queue_array[i].priv = queues->queue_array[i].priv; 210 } 211 212 queues->nr_queues = nr_queues; 213 queues->queue_array = queue_array; 214 215 return 0; 216 } 217 218 static void *auxtrace_copy_data(u64 size, struct perf_session *session) 219 { 220 int fd = perf_data__fd(session->data); 221 void *p; 222 ssize_t ret; 223 224 if (size > SSIZE_MAX) 225 return NULL; 226 227 p = malloc(size); 228 if (!p) 229 return NULL; 230 231 ret = readn(fd, p, size); 232 if (ret != (ssize_t)size) { 233 free(p); 234 return NULL; 235 } 236 237 return p; 238 } 239 240 static int auxtrace_queues__queue_buffer(struct auxtrace_queues *queues, 241 unsigned int idx, 242 struct auxtrace_buffer *buffer) 243 { 244 struct auxtrace_queue *queue; 245 int err; 246 247 if (idx >= queues->nr_queues) { 248 err = auxtrace_queues__grow(queues, idx + 1); 249 if (err) 250 return err; 251 } 252 253 queue = &queues->queue_array[idx]; 254 255 if (!queue->set) { 256 queue->set = true; 257 queue->tid = buffer->tid; 258 queue->cpu = buffer->cpu; 259 } else if (buffer->cpu != queue->cpu || buffer->tid != queue->tid) { 260 pr_err("auxtrace queue conflict: cpu %d, tid %d vs cpu %d, tid %d\n", 261 queue->cpu, queue->tid, buffer->cpu, buffer->tid); 262 return -EINVAL; 263 } 264 265 buffer->buffer_nr = queues->next_buffer_nr++; 266 267 list_add_tail(&buffer->list, &queue->head); 268 269 queues->new_data = true; 270 queues->populated = true; 271 272 return 0; 273 } 274 275 /* Limit buffers to 32MiB on 32-bit */ 276 #define BUFFER_LIMIT_FOR_32_BIT (32 * 1024 * 1024) 277 278 static int auxtrace_queues__split_buffer(struct auxtrace_queues *queues, 279 unsigned int idx, 280 struct auxtrace_buffer *buffer) 281 { 282 u64 sz = buffer->size; 283 bool consecutive = false; 284 struct auxtrace_buffer *b; 285 int err; 286 287 while (sz > BUFFER_LIMIT_FOR_32_BIT) { 288 b = memdup(buffer, sizeof(struct auxtrace_buffer)); 289 if (!b) 290 return -ENOMEM; 291 b->size = BUFFER_LIMIT_FOR_32_BIT; 292 b->consecutive = consecutive; 293 err = auxtrace_queues__queue_buffer(queues, idx, b); 294 if (err) { 295 auxtrace_buffer__free(b); 296 return err; 297 } 298 buffer->data_offset += BUFFER_LIMIT_FOR_32_BIT; 299 sz -= BUFFER_LIMIT_FOR_32_BIT; 300 consecutive = true; 301 } 302 303 buffer->size = sz; 304 buffer->consecutive = consecutive; 305 306 return 0; 307 } 308 309 static bool filter_cpu(struct perf_session *session, int cpu) 310 { 311 unsigned long *cpu_bitmap = session->itrace_synth_opts->cpu_bitmap; 312 313 return cpu_bitmap && cpu != -1 && !test_bit(cpu, cpu_bitmap); 314 } 315 316 static int auxtrace_queues__add_buffer(struct auxtrace_queues *queues, 317 struct perf_session *session, 318 unsigned int idx, 319 struct auxtrace_buffer *buffer, 320 struct auxtrace_buffer **buffer_ptr) 321 { 322 int err = -ENOMEM; 323 324 if (filter_cpu(session, buffer->cpu)) 325 return 0; 326 327 buffer = memdup(buffer, sizeof(*buffer)); 328 if (!buffer) 329 return -ENOMEM; 330 331 if (session->one_mmap) { 332 buffer->data = buffer->data_offset - session->one_mmap_offset + 333 session->one_mmap_addr; 334 } else if (perf_data__is_pipe(session->data)) { 335 buffer->data = auxtrace_copy_data(buffer->size, session); 336 if (!buffer->data) 337 goto out_free; 338 buffer->data_needs_freeing = true; 339 } else if (BITS_PER_LONG == 32 && 340 buffer->size > BUFFER_LIMIT_FOR_32_BIT) { 341 err = auxtrace_queues__split_buffer(queues, idx, buffer); 342 if (err) 343 goto out_free; 344 } 345 346 err = auxtrace_queues__queue_buffer(queues, idx, buffer); 347 if (err) 348 goto out_free; 349 350 /* FIXME: Doesn't work for split buffer */ 351 if (buffer_ptr) 352 *buffer_ptr = buffer; 353 354 return 0; 355 356 out_free: 357 auxtrace_buffer__free(buffer); 358 return err; 359 } 360 361 int auxtrace_queues__add_event(struct auxtrace_queues *queues, 362 struct perf_session *session, 363 union perf_event *event, off_t data_offset, 364 struct auxtrace_buffer **buffer_ptr) 365 { 366 struct auxtrace_buffer buffer = { 367 .pid = -1, 368 .tid = event->auxtrace.tid, 369 .cpu = event->auxtrace.cpu, 370 .data_offset = data_offset, 371 .offset = event->auxtrace.offset, 372 .reference = event->auxtrace.reference, 373 .size = event->auxtrace.size, 374 }; 375 unsigned int idx = event->auxtrace.idx; 376 377 return auxtrace_queues__add_buffer(queues, session, idx, &buffer, 378 buffer_ptr); 379 } 380 381 static int auxtrace_queues__add_indexed_event(struct auxtrace_queues *queues, 382 struct perf_session *session, 383 off_t file_offset, size_t sz) 384 { 385 union perf_event *event; 386 int err; 387 char buf[PERF_SAMPLE_MAX_SIZE]; 388 389 err = perf_session__peek_event(session, file_offset, buf, 390 PERF_SAMPLE_MAX_SIZE, &event, NULL); 391 if (err) 392 return err; 393 394 if (event->header.type == PERF_RECORD_AUXTRACE) { 395 if (event->header.size < sizeof(struct auxtrace_event) || 396 event->header.size != sz) { 397 err = -EINVAL; 398 goto out; 399 } 400 file_offset += event->header.size; 401 err = auxtrace_queues__add_event(queues, session, event, 402 file_offset, NULL); 403 } 404 out: 405 return err; 406 } 407 408 void auxtrace_queues__free(struct auxtrace_queues *queues) 409 { 410 unsigned int i; 411 412 for (i = 0; i < queues->nr_queues; i++) { 413 while (!list_empty(&queues->queue_array[i].head)) { 414 struct auxtrace_buffer *buffer; 415 416 buffer = list_entry(queues->queue_array[i].head.next, 417 struct auxtrace_buffer, list); 418 list_del(&buffer->list); 419 auxtrace_buffer__free(buffer); 420 } 421 } 422 423 zfree(&queues->queue_array); 424 queues->nr_queues = 0; 425 } 426 427 static void auxtrace_heapify(struct auxtrace_heap_item *heap_array, 428 unsigned int pos, unsigned int queue_nr, 429 u64 ordinal) 430 { 431 unsigned int parent; 432 433 while (pos) { 434 parent = (pos - 1) >> 1; 435 if (heap_array[parent].ordinal <= ordinal) 436 break; 437 heap_array[pos] = heap_array[parent]; 438 pos = parent; 439 } 440 heap_array[pos].queue_nr = queue_nr; 441 heap_array[pos].ordinal = ordinal; 442 } 443 444 int auxtrace_heap__add(struct auxtrace_heap *heap, unsigned int queue_nr, 445 u64 ordinal) 446 { 447 struct auxtrace_heap_item *heap_array; 448 449 if (queue_nr >= heap->heap_sz) { 450 unsigned int heap_sz = AUXTRACE_INIT_NR_QUEUES; 451 452 while (heap_sz <= queue_nr) 453 heap_sz <<= 1; 454 heap_array = realloc(heap->heap_array, 455 heap_sz * sizeof(struct auxtrace_heap_item)); 456 if (!heap_array) 457 return -ENOMEM; 458 heap->heap_array = heap_array; 459 heap->heap_sz = heap_sz; 460 } 461 462 auxtrace_heapify(heap->heap_array, heap->heap_cnt++, queue_nr, ordinal); 463 464 return 0; 465 } 466 467 void auxtrace_heap__free(struct auxtrace_heap *heap) 468 { 469 zfree(&heap->heap_array); 470 heap->heap_cnt = 0; 471 heap->heap_sz = 0; 472 } 473 474 void auxtrace_heap__pop(struct auxtrace_heap *heap) 475 { 476 unsigned int pos, last, heap_cnt = heap->heap_cnt; 477 struct auxtrace_heap_item *heap_array; 478 479 if (!heap_cnt) 480 return; 481 482 heap->heap_cnt -= 1; 483 484 heap_array = heap->heap_array; 485 486 pos = 0; 487 while (1) { 488 unsigned int left, right; 489 490 left = (pos << 1) + 1; 491 if (left >= heap_cnt) 492 break; 493 right = left + 1; 494 if (right >= heap_cnt) { 495 heap_array[pos] = heap_array[left]; 496 return; 497 } 498 if (heap_array[left].ordinal < heap_array[right].ordinal) { 499 heap_array[pos] = heap_array[left]; 500 pos = left; 501 } else { 502 heap_array[pos] = heap_array[right]; 503 pos = right; 504 } 505 } 506 507 last = heap_cnt - 1; 508 auxtrace_heapify(heap_array, pos, heap_array[last].queue_nr, 509 heap_array[last].ordinal); 510 } 511 512 size_t auxtrace_record__info_priv_size(struct auxtrace_record *itr, 513 struct perf_evlist *evlist) 514 { 515 if (itr) 516 return itr->info_priv_size(itr, evlist); 517 return 0; 518 } 519 520 static int auxtrace_not_supported(void) 521 { 522 pr_err("AUX area tracing is not supported on this architecture\n"); 523 return -EINVAL; 524 } 525 526 int auxtrace_record__info_fill(struct auxtrace_record *itr, 527 struct perf_session *session, 528 struct auxtrace_info_event *auxtrace_info, 529 size_t priv_size) 530 { 531 if (itr) 532 return itr->info_fill(itr, session, auxtrace_info, priv_size); 533 return auxtrace_not_supported(); 534 } 535 536 void auxtrace_record__free(struct auxtrace_record *itr) 537 { 538 if (itr) 539 itr->free(itr); 540 } 541 542 int auxtrace_record__snapshot_start(struct auxtrace_record *itr) 543 { 544 if (itr && itr->snapshot_start) 545 return itr->snapshot_start(itr); 546 return 0; 547 } 548 549 int auxtrace_record__snapshot_finish(struct auxtrace_record *itr) 550 { 551 if (itr && itr->snapshot_finish) 552 return itr->snapshot_finish(itr); 553 return 0; 554 } 555 556 int auxtrace_record__find_snapshot(struct auxtrace_record *itr, int idx, 557 struct auxtrace_mmap *mm, 558 unsigned char *data, u64 *head, u64 *old) 559 { 560 if (itr && itr->find_snapshot) 561 return itr->find_snapshot(itr, idx, mm, data, head, old); 562 return 0; 563 } 564 565 int auxtrace_record__options(struct auxtrace_record *itr, 566 struct perf_evlist *evlist, 567 struct record_opts *opts) 568 { 569 if (itr) 570 return itr->recording_options(itr, evlist, opts); 571 return 0; 572 } 573 574 u64 auxtrace_record__reference(struct auxtrace_record *itr) 575 { 576 if (itr) 577 return itr->reference(itr); 578 return 0; 579 } 580 581 int auxtrace_parse_snapshot_options(struct auxtrace_record *itr, 582 struct record_opts *opts, const char *str) 583 { 584 if (!str) 585 return 0; 586 587 if (itr) 588 return itr->parse_snapshot_options(itr, opts, str); 589 590 pr_err("No AUX area tracing to snapshot\n"); 591 return -EINVAL; 592 } 593 594 struct auxtrace_record *__weak 595 auxtrace_record__init(struct perf_evlist *evlist __maybe_unused, int *err) 596 { 597 *err = 0; 598 return NULL; 599 } 600 601 static int auxtrace_index__alloc(struct list_head *head) 602 { 603 struct auxtrace_index *auxtrace_index; 604 605 auxtrace_index = malloc(sizeof(struct auxtrace_index)); 606 if (!auxtrace_index) 607 return -ENOMEM; 608 609 auxtrace_index->nr = 0; 610 INIT_LIST_HEAD(&auxtrace_index->list); 611 612 list_add_tail(&auxtrace_index->list, head); 613 614 return 0; 615 } 616 617 void auxtrace_index__free(struct list_head *head) 618 { 619 struct auxtrace_index *auxtrace_index, *n; 620 621 list_for_each_entry_safe(auxtrace_index, n, head, list) { 622 list_del(&auxtrace_index->list); 623 free(auxtrace_index); 624 } 625 } 626 627 static struct auxtrace_index *auxtrace_index__last(struct list_head *head) 628 { 629 struct auxtrace_index *auxtrace_index; 630 int err; 631 632 if (list_empty(head)) { 633 err = auxtrace_index__alloc(head); 634 if (err) 635 return NULL; 636 } 637 638 auxtrace_index = list_entry(head->prev, struct auxtrace_index, list); 639 640 if (auxtrace_index->nr >= PERF_AUXTRACE_INDEX_ENTRY_COUNT) { 641 err = auxtrace_index__alloc(head); 642 if (err) 643 return NULL; 644 auxtrace_index = list_entry(head->prev, struct auxtrace_index, 645 list); 646 } 647 648 return auxtrace_index; 649 } 650 651 int auxtrace_index__auxtrace_event(struct list_head *head, 652 union perf_event *event, off_t file_offset) 653 { 654 struct auxtrace_index *auxtrace_index; 655 size_t nr; 656 657 auxtrace_index = auxtrace_index__last(head); 658 if (!auxtrace_index) 659 return -ENOMEM; 660 661 nr = auxtrace_index->nr; 662 auxtrace_index->entries[nr].file_offset = file_offset; 663 auxtrace_index->entries[nr].sz = event->header.size; 664 auxtrace_index->nr += 1; 665 666 return 0; 667 } 668 669 static int auxtrace_index__do_write(int fd, 670 struct auxtrace_index *auxtrace_index) 671 { 672 struct auxtrace_index_entry ent; 673 size_t i; 674 675 for (i = 0; i < auxtrace_index->nr; i++) { 676 ent.file_offset = auxtrace_index->entries[i].file_offset; 677 ent.sz = auxtrace_index->entries[i].sz; 678 if (writen(fd, &ent, sizeof(ent)) != sizeof(ent)) 679 return -errno; 680 } 681 return 0; 682 } 683 684 int auxtrace_index__write(int fd, struct list_head *head) 685 { 686 struct auxtrace_index *auxtrace_index; 687 u64 total = 0; 688 int err; 689 690 list_for_each_entry(auxtrace_index, head, list) 691 total += auxtrace_index->nr; 692 693 if (writen(fd, &total, sizeof(total)) != sizeof(total)) 694 return -errno; 695 696 list_for_each_entry(auxtrace_index, head, list) { 697 err = auxtrace_index__do_write(fd, auxtrace_index); 698 if (err) 699 return err; 700 } 701 702 return 0; 703 } 704 705 static int auxtrace_index__process_entry(int fd, struct list_head *head, 706 bool needs_swap) 707 { 708 struct auxtrace_index *auxtrace_index; 709 struct auxtrace_index_entry ent; 710 size_t nr; 711 712 if (readn(fd, &ent, sizeof(ent)) != sizeof(ent)) 713 return -1; 714 715 auxtrace_index = auxtrace_index__last(head); 716 if (!auxtrace_index) 717 return -1; 718 719 nr = auxtrace_index->nr; 720 if (needs_swap) { 721 auxtrace_index->entries[nr].file_offset = 722 bswap_64(ent.file_offset); 723 auxtrace_index->entries[nr].sz = bswap_64(ent.sz); 724 } else { 725 auxtrace_index->entries[nr].file_offset = ent.file_offset; 726 auxtrace_index->entries[nr].sz = ent.sz; 727 } 728 729 auxtrace_index->nr = nr + 1; 730 731 return 0; 732 } 733 734 int auxtrace_index__process(int fd, u64 size, struct perf_session *session, 735 bool needs_swap) 736 { 737 struct list_head *head = &session->auxtrace_index; 738 u64 nr; 739 740 if (readn(fd, &nr, sizeof(u64)) != sizeof(u64)) 741 return -1; 742 743 if (needs_swap) 744 nr = bswap_64(nr); 745 746 if (sizeof(u64) + nr * sizeof(struct auxtrace_index_entry) > size) 747 return -1; 748 749 while (nr--) { 750 int err; 751 752 err = auxtrace_index__process_entry(fd, head, needs_swap); 753 if (err) 754 return -1; 755 } 756 757 return 0; 758 } 759 760 static int auxtrace_queues__process_index_entry(struct auxtrace_queues *queues, 761 struct perf_session *session, 762 struct auxtrace_index_entry *ent) 763 { 764 return auxtrace_queues__add_indexed_event(queues, session, 765 ent->file_offset, ent->sz); 766 } 767 768 int auxtrace_queues__process_index(struct auxtrace_queues *queues, 769 struct perf_session *session) 770 { 771 struct auxtrace_index *auxtrace_index; 772 struct auxtrace_index_entry *ent; 773 size_t i; 774 int err; 775 776 if (auxtrace__dont_decode(session)) 777 return 0; 778 779 list_for_each_entry(auxtrace_index, &session->auxtrace_index, list) { 780 for (i = 0; i < auxtrace_index->nr; i++) { 781 ent = &auxtrace_index->entries[i]; 782 err = auxtrace_queues__process_index_entry(queues, 783 session, 784 ent); 785 if (err) 786 return err; 787 } 788 } 789 return 0; 790 } 791 792 struct auxtrace_buffer *auxtrace_buffer__next(struct auxtrace_queue *queue, 793 struct auxtrace_buffer *buffer) 794 { 795 if (buffer) { 796 if (list_is_last(&buffer->list, &queue->head)) 797 return NULL; 798 return list_entry(buffer->list.next, struct auxtrace_buffer, 799 list); 800 } else { 801 if (list_empty(&queue->head)) 802 return NULL; 803 return list_entry(queue->head.next, struct auxtrace_buffer, 804 list); 805 } 806 } 807 808 void *auxtrace_buffer__get_data(struct auxtrace_buffer *buffer, int fd) 809 { 810 size_t adj = buffer->data_offset & (page_size - 1); 811 size_t size = buffer->size + adj; 812 off_t file_offset = buffer->data_offset - adj; 813 void *addr; 814 815 if (buffer->data) 816 return buffer->data; 817 818 addr = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, file_offset); 819 if (addr == MAP_FAILED) 820 return NULL; 821 822 buffer->mmap_addr = addr; 823 buffer->mmap_size = size; 824 825 buffer->data = addr + adj; 826 827 return buffer->data; 828 } 829 830 void auxtrace_buffer__put_data(struct auxtrace_buffer *buffer) 831 { 832 if (!buffer->data || !buffer->mmap_addr) 833 return; 834 munmap(buffer->mmap_addr, buffer->mmap_size); 835 buffer->mmap_addr = NULL; 836 buffer->mmap_size = 0; 837 buffer->data = NULL; 838 buffer->use_data = NULL; 839 } 840 841 void auxtrace_buffer__drop_data(struct auxtrace_buffer *buffer) 842 { 843 auxtrace_buffer__put_data(buffer); 844 if (buffer->data_needs_freeing) { 845 buffer->data_needs_freeing = false; 846 zfree(&buffer->data); 847 buffer->use_data = NULL; 848 buffer->size = 0; 849 } 850 } 851 852 void auxtrace_buffer__free(struct auxtrace_buffer *buffer) 853 { 854 auxtrace_buffer__drop_data(buffer); 855 free(buffer); 856 } 857 858 void auxtrace_synth_error(struct auxtrace_error_event *auxtrace_error, int type, 859 int code, int cpu, pid_t pid, pid_t tid, u64 ip, 860 const char *msg) 861 { 862 size_t size; 863 864 memset(auxtrace_error, 0, sizeof(struct auxtrace_error_event)); 865 866 auxtrace_error->header.type = PERF_RECORD_AUXTRACE_ERROR; 867 auxtrace_error->type = type; 868 auxtrace_error->code = code; 869 auxtrace_error->cpu = cpu; 870 auxtrace_error->pid = pid; 871 auxtrace_error->tid = tid; 872 auxtrace_error->ip = ip; 873 strlcpy(auxtrace_error->msg, msg, MAX_AUXTRACE_ERROR_MSG); 874 875 size = (void *)auxtrace_error->msg - (void *)auxtrace_error + 876 strlen(auxtrace_error->msg) + 1; 877 auxtrace_error->header.size = PERF_ALIGN(size, sizeof(u64)); 878 } 879 880 int perf_event__synthesize_auxtrace_info(struct auxtrace_record *itr, 881 struct perf_tool *tool, 882 struct perf_session *session, 883 perf_event__handler_t process) 884 { 885 union perf_event *ev; 886 size_t priv_size; 887 int err; 888 889 pr_debug2("Synthesizing auxtrace information\n"); 890 priv_size = auxtrace_record__info_priv_size(itr, session->evlist); 891 ev = zalloc(sizeof(struct auxtrace_info_event) + priv_size); 892 if (!ev) 893 return -ENOMEM; 894 895 ev->auxtrace_info.header.type = PERF_RECORD_AUXTRACE_INFO; 896 ev->auxtrace_info.header.size = sizeof(struct auxtrace_info_event) + 897 priv_size; 898 err = auxtrace_record__info_fill(itr, session, &ev->auxtrace_info, 899 priv_size); 900 if (err) 901 goto out_free; 902 903 err = process(tool, ev, NULL, NULL); 904 out_free: 905 free(ev); 906 return err; 907 } 908 909 int perf_event__process_auxtrace_info(struct perf_tool *tool __maybe_unused, 910 union perf_event *event, 911 struct perf_session *session) 912 { 913 enum auxtrace_type type = event->auxtrace_info.type; 914 915 if (dump_trace) 916 fprintf(stdout, " type: %u\n", type); 917 918 switch (type) { 919 case PERF_AUXTRACE_INTEL_PT: 920 return intel_pt_process_auxtrace_info(event, session); 921 case PERF_AUXTRACE_INTEL_BTS: 922 return intel_bts_process_auxtrace_info(event, session); 923 case PERF_AUXTRACE_ARM_SPE: 924 return arm_spe_process_auxtrace_info(event, session); 925 case PERF_AUXTRACE_CS_ETM: 926 return cs_etm__process_auxtrace_info(event, session); 927 case PERF_AUXTRACE_S390_CPUMSF: 928 return s390_cpumsf_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 kallsyms__is_function(type) && 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 (!kallsyms__is_function(type)) 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); 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); 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); 1982 struct symbol *last_sym = dso__last_symbol(dso); 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