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