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