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