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_session *session, 910 union perf_event *event) 911 { 912 enum auxtrace_type type = event->auxtrace_info.type; 913 914 if (dump_trace) 915 fprintf(stdout, " type: %u\n", type); 916 917 switch (type) { 918 case PERF_AUXTRACE_INTEL_PT: 919 return intel_pt_process_auxtrace_info(event, session); 920 case PERF_AUXTRACE_INTEL_BTS: 921 return intel_bts_process_auxtrace_info(event, session); 922 case PERF_AUXTRACE_ARM_SPE: 923 return arm_spe_process_auxtrace_info(event, session); 924 case PERF_AUXTRACE_CS_ETM: 925 return cs_etm__process_auxtrace_info(event, session); 926 case PERF_AUXTRACE_S390_CPUMSF: 927 return s390_cpumsf_process_auxtrace_info(event, session); 928 case PERF_AUXTRACE_UNKNOWN: 929 default: 930 return -EINVAL; 931 } 932 } 933 934 s64 perf_event__process_auxtrace(struct perf_session *session, 935 union perf_event *event) 936 { 937 s64 err; 938 939 if (dump_trace) 940 fprintf(stdout, " size: %#"PRIx64" offset: %#"PRIx64" ref: %#"PRIx64" idx: %u tid: %d cpu: %d\n", 941 event->auxtrace.size, event->auxtrace.offset, 942 event->auxtrace.reference, event->auxtrace.idx, 943 event->auxtrace.tid, event->auxtrace.cpu); 944 945 if (auxtrace__dont_decode(session)) 946 return event->auxtrace.size; 947 948 if (!session->auxtrace || event->header.type != PERF_RECORD_AUXTRACE) 949 return -EINVAL; 950 951 err = session->auxtrace->process_auxtrace_event(session, event, session->tool); 952 if (err < 0) 953 return err; 954 955 return event->auxtrace.size; 956 } 957 958 #define PERF_ITRACE_DEFAULT_PERIOD_TYPE PERF_ITRACE_PERIOD_NANOSECS 959 #define PERF_ITRACE_DEFAULT_PERIOD 100000 960 #define PERF_ITRACE_DEFAULT_CALLCHAIN_SZ 16 961 #define PERF_ITRACE_MAX_CALLCHAIN_SZ 1024 962 #define PERF_ITRACE_DEFAULT_LAST_BRANCH_SZ 64 963 #define PERF_ITRACE_MAX_LAST_BRANCH_SZ 1024 964 965 void itrace_synth_opts__set_default(struct itrace_synth_opts *synth_opts, 966 bool no_sample) 967 { 968 synth_opts->branches = true; 969 synth_opts->transactions = true; 970 synth_opts->ptwrites = true; 971 synth_opts->pwr_events = true; 972 synth_opts->errors = true; 973 if (no_sample) { 974 synth_opts->period_type = PERF_ITRACE_PERIOD_INSTRUCTIONS; 975 synth_opts->period = 1; 976 synth_opts->calls = true; 977 } else { 978 synth_opts->instructions = true; 979 synth_opts->period_type = PERF_ITRACE_DEFAULT_PERIOD_TYPE; 980 synth_opts->period = PERF_ITRACE_DEFAULT_PERIOD; 981 } 982 synth_opts->callchain_sz = PERF_ITRACE_DEFAULT_CALLCHAIN_SZ; 983 synth_opts->last_branch_sz = PERF_ITRACE_DEFAULT_LAST_BRANCH_SZ; 984 synth_opts->initial_skip = 0; 985 } 986 987 /* 988 * Please check tools/perf/Documentation/perf-script.txt for information 989 * about the options parsed here, which is introduced after this cset, 990 * when support in 'perf script' for these options is introduced. 991 */ 992 int itrace_parse_synth_opts(const struct option *opt, const char *str, 993 int unset) 994 { 995 struct itrace_synth_opts *synth_opts = opt->value; 996 const char *p; 997 char *endptr; 998 bool period_type_set = false; 999 bool period_set = false; 1000 1001 synth_opts->set = true; 1002 1003 if (unset) { 1004 synth_opts->dont_decode = true; 1005 return 0; 1006 } 1007 1008 if (!str) { 1009 itrace_synth_opts__set_default(synth_opts, false); 1010 return 0; 1011 } 1012 1013 for (p = str; *p;) { 1014 switch (*p++) { 1015 case 'i': 1016 synth_opts->instructions = true; 1017 while (*p == ' ' || *p == ',') 1018 p += 1; 1019 if (isdigit(*p)) { 1020 synth_opts->period = strtoull(p, &endptr, 10); 1021 period_set = true; 1022 p = endptr; 1023 while (*p == ' ' || *p == ',') 1024 p += 1; 1025 switch (*p++) { 1026 case 'i': 1027 synth_opts->period_type = 1028 PERF_ITRACE_PERIOD_INSTRUCTIONS; 1029 period_type_set = true; 1030 break; 1031 case 't': 1032 synth_opts->period_type = 1033 PERF_ITRACE_PERIOD_TICKS; 1034 period_type_set = true; 1035 break; 1036 case 'm': 1037 synth_opts->period *= 1000; 1038 /* Fall through */ 1039 case 'u': 1040 synth_opts->period *= 1000; 1041 /* Fall through */ 1042 case 'n': 1043 if (*p++ != 's') 1044 goto out_err; 1045 synth_opts->period_type = 1046 PERF_ITRACE_PERIOD_NANOSECS; 1047 period_type_set = true; 1048 break; 1049 case '\0': 1050 goto out; 1051 default: 1052 goto out_err; 1053 } 1054 } 1055 break; 1056 case 'b': 1057 synth_opts->branches = true; 1058 break; 1059 case 'x': 1060 synth_opts->transactions = true; 1061 break; 1062 case 'w': 1063 synth_opts->ptwrites = true; 1064 break; 1065 case 'p': 1066 synth_opts->pwr_events = true; 1067 break; 1068 case 'e': 1069 synth_opts->errors = true; 1070 break; 1071 case 'd': 1072 synth_opts->log = true; 1073 break; 1074 case 'c': 1075 synth_opts->branches = true; 1076 synth_opts->calls = true; 1077 break; 1078 case 'r': 1079 synth_opts->branches = true; 1080 synth_opts->returns = true; 1081 break; 1082 case 'g': 1083 synth_opts->callchain = true; 1084 synth_opts->callchain_sz = 1085 PERF_ITRACE_DEFAULT_CALLCHAIN_SZ; 1086 while (*p == ' ' || *p == ',') 1087 p += 1; 1088 if (isdigit(*p)) { 1089 unsigned int val; 1090 1091 val = strtoul(p, &endptr, 10); 1092 p = endptr; 1093 if (!val || val > PERF_ITRACE_MAX_CALLCHAIN_SZ) 1094 goto out_err; 1095 synth_opts->callchain_sz = val; 1096 } 1097 break; 1098 case 'l': 1099 synth_opts->last_branch = true; 1100 synth_opts->last_branch_sz = 1101 PERF_ITRACE_DEFAULT_LAST_BRANCH_SZ; 1102 while (*p == ' ' || *p == ',') 1103 p += 1; 1104 if (isdigit(*p)) { 1105 unsigned int val; 1106 1107 val = strtoul(p, &endptr, 10); 1108 p = endptr; 1109 if (!val || 1110 val > PERF_ITRACE_MAX_LAST_BRANCH_SZ) 1111 goto out_err; 1112 synth_opts->last_branch_sz = val; 1113 } 1114 break; 1115 case 's': 1116 synth_opts->initial_skip = strtoul(p, &endptr, 10); 1117 if (p == endptr) 1118 goto out_err; 1119 p = endptr; 1120 break; 1121 case ' ': 1122 case ',': 1123 break; 1124 default: 1125 goto out_err; 1126 } 1127 } 1128 out: 1129 if (synth_opts->instructions) { 1130 if (!period_type_set) 1131 synth_opts->period_type = 1132 PERF_ITRACE_DEFAULT_PERIOD_TYPE; 1133 if (!period_set) 1134 synth_opts->period = PERF_ITRACE_DEFAULT_PERIOD; 1135 } 1136 1137 return 0; 1138 1139 out_err: 1140 pr_err("Bad Instruction Tracing options '%s'\n", str); 1141 return -EINVAL; 1142 } 1143 1144 static const char * const auxtrace_error_type_name[] = { 1145 [PERF_AUXTRACE_ERROR_ITRACE] = "instruction trace", 1146 }; 1147 1148 static const char *auxtrace_error_name(int type) 1149 { 1150 const char *error_type_name = NULL; 1151 1152 if (type < PERF_AUXTRACE_ERROR_MAX) 1153 error_type_name = auxtrace_error_type_name[type]; 1154 if (!error_type_name) 1155 error_type_name = "unknown AUX"; 1156 return error_type_name; 1157 } 1158 1159 size_t perf_event__fprintf_auxtrace_error(union perf_event *event, FILE *fp) 1160 { 1161 struct auxtrace_error_event *e = &event->auxtrace_error; 1162 int ret; 1163 1164 ret = fprintf(fp, " %s error type %u", 1165 auxtrace_error_name(e->type), e->type); 1166 ret += fprintf(fp, " cpu %d pid %d tid %d ip %#"PRIx64" code %u: %s\n", 1167 e->cpu, e->pid, e->tid, e->ip, e->code, e->msg); 1168 return ret; 1169 } 1170 1171 void perf_session__auxtrace_error_inc(struct perf_session *session, 1172 union perf_event *event) 1173 { 1174 struct auxtrace_error_event *e = &event->auxtrace_error; 1175 1176 if (e->type < PERF_AUXTRACE_ERROR_MAX) 1177 session->evlist->stats.nr_auxtrace_errors[e->type] += 1; 1178 } 1179 1180 void events_stats__auxtrace_error_warn(const struct events_stats *stats) 1181 { 1182 int i; 1183 1184 for (i = 0; i < PERF_AUXTRACE_ERROR_MAX; i++) { 1185 if (!stats->nr_auxtrace_errors[i]) 1186 continue; 1187 ui__warning("%u %s errors\n", 1188 stats->nr_auxtrace_errors[i], 1189 auxtrace_error_name(i)); 1190 } 1191 } 1192 1193 int perf_event__process_auxtrace_error(struct perf_session *session, 1194 union perf_event *event) 1195 { 1196 if (auxtrace__dont_decode(session)) 1197 return 0; 1198 1199 perf_event__fprintf_auxtrace_error(event, stdout); 1200 return 0; 1201 } 1202 1203 static int __auxtrace_mmap__read(struct perf_mmap *map, 1204 struct auxtrace_record *itr, 1205 struct perf_tool *tool, process_auxtrace_t fn, 1206 bool snapshot, size_t snapshot_size) 1207 { 1208 struct auxtrace_mmap *mm = &map->auxtrace_mmap; 1209 u64 head, old = mm->prev, offset, ref; 1210 unsigned char *data = mm->base; 1211 size_t size, head_off, old_off, len1, len2, padding; 1212 union perf_event ev; 1213 void *data1, *data2; 1214 1215 if (snapshot) { 1216 head = auxtrace_mmap__read_snapshot_head(mm); 1217 if (auxtrace_record__find_snapshot(itr, mm->idx, mm, data, 1218 &head, &old)) 1219 return -1; 1220 } else { 1221 head = auxtrace_mmap__read_head(mm); 1222 } 1223 1224 if (old == head) 1225 return 0; 1226 1227 pr_debug3("auxtrace idx %d old %#"PRIx64" head %#"PRIx64" diff %#"PRIx64"\n", 1228 mm->idx, old, head, head - old); 1229 1230 if (mm->mask) { 1231 head_off = head & mm->mask; 1232 old_off = old & mm->mask; 1233 } else { 1234 head_off = head % mm->len; 1235 old_off = old % mm->len; 1236 } 1237 1238 if (head_off > old_off) 1239 size = head_off - old_off; 1240 else 1241 size = mm->len - (old_off - head_off); 1242 1243 if (snapshot && size > snapshot_size) 1244 size = snapshot_size; 1245 1246 ref = auxtrace_record__reference(itr); 1247 1248 if (head > old || size <= head || mm->mask) { 1249 offset = head - size; 1250 } else { 1251 /* 1252 * When the buffer size is not a power of 2, 'head' wraps at the 1253 * highest multiple of the buffer size, so we have to subtract 1254 * the remainder here. 1255 */ 1256 u64 rem = (0ULL - mm->len) % mm->len; 1257 1258 offset = head - size - rem; 1259 } 1260 1261 if (size > head_off) { 1262 len1 = size - head_off; 1263 data1 = &data[mm->len - len1]; 1264 len2 = head_off; 1265 data2 = &data[0]; 1266 } else { 1267 len1 = size; 1268 data1 = &data[head_off - len1]; 1269 len2 = 0; 1270 data2 = NULL; 1271 } 1272 1273 if (itr->alignment) { 1274 unsigned int unwanted = len1 % itr->alignment; 1275 1276 len1 -= unwanted; 1277 size -= unwanted; 1278 } 1279 1280 /* padding must be written by fn() e.g. record__process_auxtrace() */ 1281 padding = size & 7; 1282 if (padding) 1283 padding = 8 - padding; 1284 1285 memset(&ev, 0, sizeof(ev)); 1286 ev.auxtrace.header.type = PERF_RECORD_AUXTRACE; 1287 ev.auxtrace.header.size = sizeof(ev.auxtrace); 1288 ev.auxtrace.size = size + padding; 1289 ev.auxtrace.offset = offset; 1290 ev.auxtrace.reference = ref; 1291 ev.auxtrace.idx = mm->idx; 1292 ev.auxtrace.tid = mm->tid; 1293 ev.auxtrace.cpu = mm->cpu; 1294 1295 if (fn(tool, map, &ev, data1, len1, data2, len2)) 1296 return -1; 1297 1298 mm->prev = head; 1299 1300 if (!snapshot) { 1301 auxtrace_mmap__write_tail(mm, head); 1302 if (itr->read_finish) { 1303 int err; 1304 1305 err = itr->read_finish(itr, mm->idx); 1306 if (err < 0) 1307 return err; 1308 } 1309 } 1310 1311 return 1; 1312 } 1313 1314 int auxtrace_mmap__read(struct perf_mmap *map, struct auxtrace_record *itr, 1315 struct perf_tool *tool, process_auxtrace_t fn) 1316 { 1317 return __auxtrace_mmap__read(map, itr, tool, fn, false, 0); 1318 } 1319 1320 int auxtrace_mmap__read_snapshot(struct perf_mmap *map, 1321 struct auxtrace_record *itr, 1322 struct perf_tool *tool, process_auxtrace_t fn, 1323 size_t snapshot_size) 1324 { 1325 return __auxtrace_mmap__read(map, itr, tool, fn, true, snapshot_size); 1326 } 1327 1328 /** 1329 * struct auxtrace_cache - hash table to implement a cache 1330 * @hashtable: the hashtable 1331 * @sz: hashtable size (number of hlists) 1332 * @entry_size: size of an entry 1333 * @limit: limit the number of entries to this maximum, when reached the cache 1334 * is dropped and caching begins again with an empty cache 1335 * @cnt: current number of entries 1336 * @bits: hashtable size (@sz = 2^@bits) 1337 */ 1338 struct auxtrace_cache { 1339 struct hlist_head *hashtable; 1340 size_t sz; 1341 size_t entry_size; 1342 size_t limit; 1343 size_t cnt; 1344 unsigned int bits; 1345 }; 1346 1347 struct auxtrace_cache *auxtrace_cache__new(unsigned int bits, size_t entry_size, 1348 unsigned int limit_percent) 1349 { 1350 struct auxtrace_cache *c; 1351 struct hlist_head *ht; 1352 size_t sz, i; 1353 1354 c = zalloc(sizeof(struct auxtrace_cache)); 1355 if (!c) 1356 return NULL; 1357 1358 sz = 1UL << bits; 1359 1360 ht = calloc(sz, sizeof(struct hlist_head)); 1361 if (!ht) 1362 goto out_free; 1363 1364 for (i = 0; i < sz; i++) 1365 INIT_HLIST_HEAD(&ht[i]); 1366 1367 c->hashtable = ht; 1368 c->sz = sz; 1369 c->entry_size = entry_size; 1370 c->limit = (c->sz * limit_percent) / 100; 1371 c->bits = bits; 1372 1373 return c; 1374 1375 out_free: 1376 free(c); 1377 return NULL; 1378 } 1379 1380 static void auxtrace_cache__drop(struct auxtrace_cache *c) 1381 { 1382 struct auxtrace_cache_entry *entry; 1383 struct hlist_node *tmp; 1384 size_t i; 1385 1386 if (!c) 1387 return; 1388 1389 for (i = 0; i < c->sz; i++) { 1390 hlist_for_each_entry_safe(entry, tmp, &c->hashtable[i], hash) { 1391 hlist_del(&entry->hash); 1392 auxtrace_cache__free_entry(c, entry); 1393 } 1394 } 1395 1396 c->cnt = 0; 1397 } 1398 1399 void auxtrace_cache__free(struct auxtrace_cache *c) 1400 { 1401 if (!c) 1402 return; 1403 1404 auxtrace_cache__drop(c); 1405 free(c->hashtable); 1406 free(c); 1407 } 1408 1409 void *auxtrace_cache__alloc_entry(struct auxtrace_cache *c) 1410 { 1411 return malloc(c->entry_size); 1412 } 1413 1414 void auxtrace_cache__free_entry(struct auxtrace_cache *c __maybe_unused, 1415 void *entry) 1416 { 1417 free(entry); 1418 } 1419 1420 int auxtrace_cache__add(struct auxtrace_cache *c, u32 key, 1421 struct auxtrace_cache_entry *entry) 1422 { 1423 if (c->limit && ++c->cnt > c->limit) 1424 auxtrace_cache__drop(c); 1425 1426 entry->key = key; 1427 hlist_add_head(&entry->hash, &c->hashtable[hash_32(key, c->bits)]); 1428 1429 return 0; 1430 } 1431 1432 void *auxtrace_cache__lookup(struct auxtrace_cache *c, u32 key) 1433 { 1434 struct auxtrace_cache_entry *entry; 1435 struct hlist_head *hlist; 1436 1437 if (!c) 1438 return NULL; 1439 1440 hlist = &c->hashtable[hash_32(key, c->bits)]; 1441 hlist_for_each_entry(entry, hlist, hash) { 1442 if (entry->key == key) 1443 return entry; 1444 } 1445 1446 return NULL; 1447 } 1448 1449 static void addr_filter__free_str(struct addr_filter *filt) 1450 { 1451 free(filt->str); 1452 filt->action = NULL; 1453 filt->sym_from = NULL; 1454 filt->sym_to = NULL; 1455 filt->filename = NULL; 1456 filt->str = NULL; 1457 } 1458 1459 static struct addr_filter *addr_filter__new(void) 1460 { 1461 struct addr_filter *filt = zalloc(sizeof(*filt)); 1462 1463 if (filt) 1464 INIT_LIST_HEAD(&filt->list); 1465 1466 return filt; 1467 } 1468 1469 static void addr_filter__free(struct addr_filter *filt) 1470 { 1471 if (filt) 1472 addr_filter__free_str(filt); 1473 free(filt); 1474 } 1475 1476 static void addr_filters__add(struct addr_filters *filts, 1477 struct addr_filter *filt) 1478 { 1479 list_add_tail(&filt->list, &filts->head); 1480 filts->cnt += 1; 1481 } 1482 1483 static void addr_filters__del(struct addr_filters *filts, 1484 struct addr_filter *filt) 1485 { 1486 list_del_init(&filt->list); 1487 filts->cnt -= 1; 1488 } 1489 1490 void addr_filters__init(struct addr_filters *filts) 1491 { 1492 INIT_LIST_HEAD(&filts->head); 1493 filts->cnt = 0; 1494 } 1495 1496 void addr_filters__exit(struct addr_filters *filts) 1497 { 1498 struct addr_filter *filt, *n; 1499 1500 list_for_each_entry_safe(filt, n, &filts->head, list) { 1501 addr_filters__del(filts, filt); 1502 addr_filter__free(filt); 1503 } 1504 } 1505 1506 static int parse_num_or_str(char **inp, u64 *num, const char **str, 1507 const char *str_delim) 1508 { 1509 *inp += strspn(*inp, " "); 1510 1511 if (isdigit(**inp)) { 1512 char *endptr; 1513 1514 if (!num) 1515 return -EINVAL; 1516 errno = 0; 1517 *num = strtoull(*inp, &endptr, 0); 1518 if (errno) 1519 return -errno; 1520 if (endptr == *inp) 1521 return -EINVAL; 1522 *inp = endptr; 1523 } else { 1524 size_t n; 1525 1526 if (!str) 1527 return -EINVAL; 1528 *inp += strspn(*inp, " "); 1529 *str = *inp; 1530 n = strcspn(*inp, str_delim); 1531 if (!n) 1532 return -EINVAL; 1533 *inp += n; 1534 if (**inp) { 1535 **inp = '\0'; 1536 *inp += 1; 1537 } 1538 } 1539 return 0; 1540 } 1541 1542 static int parse_action(struct addr_filter *filt) 1543 { 1544 if (!strcmp(filt->action, "filter")) { 1545 filt->start = true; 1546 filt->range = true; 1547 } else if (!strcmp(filt->action, "start")) { 1548 filt->start = true; 1549 } else if (!strcmp(filt->action, "stop")) { 1550 filt->start = false; 1551 } else if (!strcmp(filt->action, "tracestop")) { 1552 filt->start = false; 1553 filt->range = true; 1554 filt->action += 5; /* Change 'tracestop' to 'stop' */ 1555 } else { 1556 return -EINVAL; 1557 } 1558 return 0; 1559 } 1560 1561 static int parse_sym_idx(char **inp, int *idx) 1562 { 1563 *idx = -1; 1564 1565 *inp += strspn(*inp, " "); 1566 1567 if (**inp != '#') 1568 return 0; 1569 1570 *inp += 1; 1571 1572 if (**inp == 'g' || **inp == 'G') { 1573 *inp += 1; 1574 *idx = 0; 1575 } else { 1576 unsigned long num; 1577 char *endptr; 1578 1579 errno = 0; 1580 num = strtoul(*inp, &endptr, 0); 1581 if (errno) 1582 return -errno; 1583 if (endptr == *inp || num > INT_MAX) 1584 return -EINVAL; 1585 *inp = endptr; 1586 *idx = num; 1587 } 1588 1589 return 0; 1590 } 1591 1592 static int parse_addr_size(char **inp, u64 *num, const char **str, int *idx) 1593 { 1594 int err = parse_num_or_str(inp, num, str, " "); 1595 1596 if (!err && *str) 1597 err = parse_sym_idx(inp, idx); 1598 1599 return err; 1600 } 1601 1602 static int parse_one_filter(struct addr_filter *filt, const char **filter_inp) 1603 { 1604 char *fstr; 1605 int err; 1606 1607 filt->str = fstr = strdup(*filter_inp); 1608 if (!fstr) 1609 return -ENOMEM; 1610 1611 err = parse_num_or_str(&fstr, NULL, &filt->action, " "); 1612 if (err) 1613 goto out_err; 1614 1615 err = parse_action(filt); 1616 if (err) 1617 goto out_err; 1618 1619 err = parse_addr_size(&fstr, &filt->addr, &filt->sym_from, 1620 &filt->sym_from_idx); 1621 if (err) 1622 goto out_err; 1623 1624 fstr += strspn(fstr, " "); 1625 1626 if (*fstr == '/') { 1627 fstr += 1; 1628 err = parse_addr_size(&fstr, &filt->size, &filt->sym_to, 1629 &filt->sym_to_idx); 1630 if (err) 1631 goto out_err; 1632 filt->range = true; 1633 } 1634 1635 fstr += strspn(fstr, " "); 1636 1637 if (*fstr == '@') { 1638 fstr += 1; 1639 err = parse_num_or_str(&fstr, NULL, &filt->filename, " ,"); 1640 if (err) 1641 goto out_err; 1642 } 1643 1644 fstr += strspn(fstr, " ,"); 1645 1646 *filter_inp += fstr - filt->str; 1647 1648 return 0; 1649 1650 out_err: 1651 addr_filter__free_str(filt); 1652 1653 return err; 1654 } 1655 1656 int addr_filters__parse_bare_filter(struct addr_filters *filts, 1657 const char *filter) 1658 { 1659 struct addr_filter *filt; 1660 const char *fstr = filter; 1661 int err; 1662 1663 while (*fstr) { 1664 filt = addr_filter__new(); 1665 err = parse_one_filter(filt, &fstr); 1666 if (err) { 1667 addr_filter__free(filt); 1668 addr_filters__exit(filts); 1669 return err; 1670 } 1671 addr_filters__add(filts, filt); 1672 } 1673 1674 return 0; 1675 } 1676 1677 struct sym_args { 1678 const char *name; 1679 u64 start; 1680 u64 size; 1681 int idx; 1682 int cnt; 1683 bool started; 1684 bool global; 1685 bool selected; 1686 bool duplicate; 1687 bool near; 1688 }; 1689 1690 static bool kern_sym_match(struct sym_args *args, const char *name, char type) 1691 { 1692 /* A function with the same name, and global or the n'th found or any */ 1693 return kallsyms__is_function(type) && 1694 !strcmp(name, args->name) && 1695 ((args->global && isupper(type)) || 1696 (args->selected && ++(args->cnt) == args->idx) || 1697 (!args->global && !args->selected)); 1698 } 1699 1700 static int find_kern_sym_cb(void *arg, const char *name, char type, u64 start) 1701 { 1702 struct sym_args *args = arg; 1703 1704 if (args->started) { 1705 if (!args->size) 1706 args->size = start - args->start; 1707 if (args->selected) { 1708 if (args->size) 1709 return 1; 1710 } else if (kern_sym_match(args, name, type)) { 1711 args->duplicate = true; 1712 return 1; 1713 } 1714 } else if (kern_sym_match(args, name, type)) { 1715 args->started = true; 1716 args->start = start; 1717 } 1718 1719 return 0; 1720 } 1721 1722 static int print_kern_sym_cb(void *arg, const char *name, char type, u64 start) 1723 { 1724 struct sym_args *args = arg; 1725 1726 if (kern_sym_match(args, name, type)) { 1727 pr_err("#%d\t0x%"PRIx64"\t%c\t%s\n", 1728 ++args->cnt, start, type, name); 1729 args->near = true; 1730 } else if (args->near) { 1731 args->near = false; 1732 pr_err("\t\twhich is near\t\t%s\n", name); 1733 } 1734 1735 return 0; 1736 } 1737 1738 static int sym_not_found_error(const char *sym_name, int idx) 1739 { 1740 if (idx > 0) { 1741 pr_err("N'th occurrence (N=%d) of symbol '%s' not found.\n", 1742 idx, sym_name); 1743 } else if (!idx) { 1744 pr_err("Global symbol '%s' not found.\n", sym_name); 1745 } else { 1746 pr_err("Symbol '%s' not found.\n", sym_name); 1747 } 1748 pr_err("Note that symbols must be functions.\n"); 1749 1750 return -EINVAL; 1751 } 1752 1753 static int find_kern_sym(const char *sym_name, u64 *start, u64 *size, int idx) 1754 { 1755 struct sym_args args = { 1756 .name = sym_name, 1757 .idx = idx, 1758 .global = !idx, 1759 .selected = idx > 0, 1760 }; 1761 int err; 1762 1763 *start = 0; 1764 *size = 0; 1765 1766 err = kallsyms__parse("/proc/kallsyms", &args, find_kern_sym_cb); 1767 if (err < 0) { 1768 pr_err("Failed to parse /proc/kallsyms\n"); 1769 return err; 1770 } 1771 1772 if (args.duplicate) { 1773 pr_err("Multiple kernel symbols with name '%s'\n", sym_name); 1774 args.cnt = 0; 1775 kallsyms__parse("/proc/kallsyms", &args, print_kern_sym_cb); 1776 pr_err("Disambiguate symbol name by inserting #n after the name e.g. %s #2\n", 1777 sym_name); 1778 pr_err("Or select a global symbol by inserting #0 or #g or #G\n"); 1779 return -EINVAL; 1780 } 1781 1782 if (!args.started) { 1783 pr_err("Kernel symbol lookup: "); 1784 return sym_not_found_error(sym_name, idx); 1785 } 1786 1787 *start = args.start; 1788 *size = args.size; 1789 1790 return 0; 1791 } 1792 1793 static int find_entire_kern_cb(void *arg, const char *name __maybe_unused, 1794 char type, u64 start) 1795 { 1796 struct sym_args *args = arg; 1797 1798 if (!kallsyms__is_function(type)) 1799 return 0; 1800 1801 if (!args->started) { 1802 args->started = true; 1803 args->start = start; 1804 } 1805 /* Don't know exactly where the kernel ends, so we add a page */ 1806 args->size = round_up(start, page_size) + page_size - args->start; 1807 1808 return 0; 1809 } 1810 1811 static int addr_filter__entire_kernel(struct addr_filter *filt) 1812 { 1813 struct sym_args args = { .started = false }; 1814 int err; 1815 1816 err = kallsyms__parse("/proc/kallsyms", &args, find_entire_kern_cb); 1817 if (err < 0 || !args.started) { 1818 pr_err("Failed to parse /proc/kallsyms\n"); 1819 return err; 1820 } 1821 1822 filt->addr = args.start; 1823 filt->size = args.size; 1824 1825 return 0; 1826 } 1827 1828 static int check_end_after_start(struct addr_filter *filt, u64 start, u64 size) 1829 { 1830 if (start + size >= filt->addr) 1831 return 0; 1832 1833 if (filt->sym_from) { 1834 pr_err("Symbol '%s' (0x%"PRIx64") comes before '%s' (0x%"PRIx64")\n", 1835 filt->sym_to, start, filt->sym_from, filt->addr); 1836 } else { 1837 pr_err("Symbol '%s' (0x%"PRIx64") comes before address 0x%"PRIx64")\n", 1838 filt->sym_to, start, filt->addr); 1839 } 1840 1841 return -EINVAL; 1842 } 1843 1844 static int addr_filter__resolve_kernel_syms(struct addr_filter *filt) 1845 { 1846 bool no_size = false; 1847 u64 start, size; 1848 int err; 1849 1850 if (symbol_conf.kptr_restrict) { 1851 pr_err("Kernel addresses are restricted. Unable to resolve kernel symbols.\n"); 1852 return -EINVAL; 1853 } 1854 1855 if (filt->sym_from && !strcmp(filt->sym_from, "*")) 1856 return addr_filter__entire_kernel(filt); 1857 1858 if (filt->sym_from) { 1859 err = find_kern_sym(filt->sym_from, &start, &size, 1860 filt->sym_from_idx); 1861 if (err) 1862 return err; 1863 filt->addr = start; 1864 if (filt->range && !filt->size && !filt->sym_to) { 1865 filt->size = size; 1866 no_size = !size; 1867 } 1868 } 1869 1870 if (filt->sym_to) { 1871 err = find_kern_sym(filt->sym_to, &start, &size, 1872 filt->sym_to_idx); 1873 if (err) 1874 return err; 1875 1876 err = check_end_after_start(filt, start, size); 1877 if (err) 1878 return err; 1879 filt->size = start + size - filt->addr; 1880 no_size = !size; 1881 } 1882 1883 /* The very last symbol in kallsyms does not imply a particular size */ 1884 if (no_size) { 1885 pr_err("Cannot determine size of symbol '%s'\n", 1886 filt->sym_to ? filt->sym_to : filt->sym_from); 1887 return -EINVAL; 1888 } 1889 1890 return 0; 1891 } 1892 1893 static struct dso *load_dso(const char *name) 1894 { 1895 struct map *map; 1896 struct dso *dso; 1897 1898 map = dso__new_map(name); 1899 if (!map) 1900 return NULL; 1901 1902 map__load(map); 1903 1904 dso = dso__get(map->dso); 1905 1906 map__put(map); 1907 1908 return dso; 1909 } 1910 1911 static bool dso_sym_match(struct symbol *sym, const char *name, int *cnt, 1912 int idx) 1913 { 1914 /* Same name, and global or the n'th found or any */ 1915 return !arch__compare_symbol_names(name, sym->name) && 1916 ((!idx && sym->binding == STB_GLOBAL) || 1917 (idx > 0 && ++*cnt == idx) || 1918 idx < 0); 1919 } 1920 1921 static void print_duplicate_syms(struct dso *dso, const char *sym_name) 1922 { 1923 struct symbol *sym; 1924 bool near = false; 1925 int cnt = 0; 1926 1927 pr_err("Multiple symbols with name '%s'\n", sym_name); 1928 1929 sym = dso__first_symbol(dso); 1930 while (sym) { 1931 if (dso_sym_match(sym, sym_name, &cnt, -1)) { 1932 pr_err("#%d\t0x%"PRIx64"\t%c\t%s\n", 1933 ++cnt, sym->start, 1934 sym->binding == STB_GLOBAL ? 'g' : 1935 sym->binding == STB_LOCAL ? 'l' : 'w', 1936 sym->name); 1937 near = true; 1938 } else if (near) { 1939 near = false; 1940 pr_err("\t\twhich is near\t\t%s\n", sym->name); 1941 } 1942 sym = dso__next_symbol(sym); 1943 } 1944 1945 pr_err("Disambiguate symbol name by inserting #n after the name e.g. %s #2\n", 1946 sym_name); 1947 pr_err("Or select a global symbol by inserting #0 or #g or #G\n"); 1948 } 1949 1950 static int find_dso_sym(struct dso *dso, const char *sym_name, u64 *start, 1951 u64 *size, int idx) 1952 { 1953 struct symbol *sym; 1954 int cnt = 0; 1955 1956 *start = 0; 1957 *size = 0; 1958 1959 sym = dso__first_symbol(dso); 1960 while (sym) { 1961 if (*start) { 1962 if (!*size) 1963 *size = sym->start - *start; 1964 if (idx > 0) { 1965 if (*size) 1966 return 1; 1967 } else if (dso_sym_match(sym, sym_name, &cnt, idx)) { 1968 print_duplicate_syms(dso, sym_name); 1969 return -EINVAL; 1970 } 1971 } else if (dso_sym_match(sym, sym_name, &cnt, idx)) { 1972 *start = sym->start; 1973 *size = sym->end - sym->start; 1974 } 1975 sym = dso__next_symbol(sym); 1976 } 1977 1978 if (!*start) 1979 return sym_not_found_error(sym_name, idx); 1980 1981 return 0; 1982 } 1983 1984 static int addr_filter__entire_dso(struct addr_filter *filt, struct dso *dso) 1985 { 1986 if (dso__data_file_size(dso, NULL)) { 1987 pr_err("Failed to determine filter for %s\nCannot determine file size.\n", 1988 filt->filename); 1989 return -EINVAL; 1990 } 1991 1992 filt->addr = 0; 1993 filt->size = dso->data.file_size; 1994 1995 return 0; 1996 } 1997 1998 static int addr_filter__resolve_syms(struct addr_filter *filt) 1999 { 2000 u64 start, size; 2001 struct dso *dso; 2002 int err = 0; 2003 2004 if (!filt->sym_from && !filt->sym_to) 2005 return 0; 2006 2007 if (!filt->filename) 2008 return addr_filter__resolve_kernel_syms(filt); 2009 2010 dso = load_dso(filt->filename); 2011 if (!dso) { 2012 pr_err("Failed to load symbols from: %s\n", filt->filename); 2013 return -EINVAL; 2014 } 2015 2016 if (filt->sym_from && !strcmp(filt->sym_from, "*")) { 2017 err = addr_filter__entire_dso(filt, dso); 2018 goto put_dso; 2019 } 2020 2021 if (filt->sym_from) { 2022 err = find_dso_sym(dso, filt->sym_from, &start, &size, 2023 filt->sym_from_idx); 2024 if (err) 2025 goto put_dso; 2026 filt->addr = start; 2027 if (filt->range && !filt->size && !filt->sym_to) 2028 filt->size = size; 2029 } 2030 2031 if (filt->sym_to) { 2032 err = find_dso_sym(dso, filt->sym_to, &start, &size, 2033 filt->sym_to_idx); 2034 if (err) 2035 goto put_dso; 2036 2037 err = check_end_after_start(filt, start, size); 2038 if (err) 2039 return err; 2040 2041 filt->size = start + size - filt->addr; 2042 } 2043 2044 put_dso: 2045 dso__put(dso); 2046 2047 return err; 2048 } 2049 2050 static char *addr_filter__to_str(struct addr_filter *filt) 2051 { 2052 char filename_buf[PATH_MAX]; 2053 const char *at = ""; 2054 const char *fn = ""; 2055 char *filter; 2056 int err; 2057 2058 if (filt->filename) { 2059 at = "@"; 2060 fn = realpath(filt->filename, filename_buf); 2061 if (!fn) 2062 return NULL; 2063 } 2064 2065 if (filt->range) { 2066 err = asprintf(&filter, "%s 0x%"PRIx64"/0x%"PRIx64"%s%s", 2067 filt->action, filt->addr, filt->size, at, fn); 2068 } else { 2069 err = asprintf(&filter, "%s 0x%"PRIx64"%s%s", 2070 filt->action, filt->addr, at, fn); 2071 } 2072 2073 return err < 0 ? NULL : filter; 2074 } 2075 2076 static int parse_addr_filter(struct perf_evsel *evsel, const char *filter, 2077 int max_nr) 2078 { 2079 struct addr_filters filts; 2080 struct addr_filter *filt; 2081 int err; 2082 2083 addr_filters__init(&filts); 2084 2085 err = addr_filters__parse_bare_filter(&filts, filter); 2086 if (err) 2087 goto out_exit; 2088 2089 if (filts.cnt > max_nr) { 2090 pr_err("Error: number of address filters (%d) exceeds maximum (%d)\n", 2091 filts.cnt, max_nr); 2092 err = -EINVAL; 2093 goto out_exit; 2094 } 2095 2096 list_for_each_entry(filt, &filts.head, list) { 2097 char *new_filter; 2098 2099 err = addr_filter__resolve_syms(filt); 2100 if (err) 2101 goto out_exit; 2102 2103 new_filter = addr_filter__to_str(filt); 2104 if (!new_filter) { 2105 err = -ENOMEM; 2106 goto out_exit; 2107 } 2108 2109 if (perf_evsel__append_addr_filter(evsel, new_filter)) { 2110 err = -ENOMEM; 2111 goto out_exit; 2112 } 2113 } 2114 2115 out_exit: 2116 addr_filters__exit(&filts); 2117 2118 if (err) { 2119 pr_err("Failed to parse address filter: '%s'\n", filter); 2120 pr_err("Filter format is: filter|start|stop|tracestop <start symbol or address> [/ <end symbol or size>] [@<file name>]\n"); 2121 pr_err("Where multiple filters are separated by space or comma.\n"); 2122 } 2123 2124 return err; 2125 } 2126 2127 static struct perf_pmu *perf_evsel__find_pmu(struct perf_evsel *evsel) 2128 { 2129 struct perf_pmu *pmu = NULL; 2130 2131 while ((pmu = perf_pmu__scan(pmu)) != NULL) { 2132 if (pmu->type == evsel->attr.type) 2133 break; 2134 } 2135 2136 return pmu; 2137 } 2138 2139 static int perf_evsel__nr_addr_filter(struct perf_evsel *evsel) 2140 { 2141 struct perf_pmu *pmu = perf_evsel__find_pmu(evsel); 2142 int nr_addr_filters = 0; 2143 2144 if (!pmu) 2145 return 0; 2146 2147 perf_pmu__scan_file(pmu, "nr_addr_filters", "%d", &nr_addr_filters); 2148 2149 return nr_addr_filters; 2150 } 2151 2152 int auxtrace_parse_filters(struct perf_evlist *evlist) 2153 { 2154 struct perf_evsel *evsel; 2155 char *filter; 2156 int err, max_nr; 2157 2158 evlist__for_each_entry(evlist, evsel) { 2159 filter = evsel->filter; 2160 max_nr = perf_evsel__nr_addr_filter(evsel); 2161 if (!filter || !max_nr) 2162 continue; 2163 evsel->filter = NULL; 2164 err = parse_addr_filter(evsel, filter, max_nr); 2165 free(filter); 2166 if (err) 2167 return err; 2168 pr_debug("Address filter: %s\n", evsel->filter); 2169 } 2170 2171 return 0; 2172 } 2173