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