1 // SPDX-License-Identifier: GPL-2.0 2 #include <dirent.h> 3 #include <errno.h> 4 #include <inttypes.h> 5 #include <regex.h> 6 #include <stdlib.h> 7 #include <string.h> 8 #include "callchain.h" 9 #include "debug.h" 10 #include "dso.h" 11 #include "env.h" 12 #include "dwarf-regs.h" 13 #include "event.h" 14 #include "evsel.h" 15 #include "hist.h" 16 #include "machine.h" 17 #include "map.h" 18 #include "map_symbol.h" 19 #include "branch.h" 20 #include "mem-events.h" 21 #include "mem-info.h" 22 #include "path.h" 23 #include "srcline.h" 24 #include "symbol.h" 25 #include "synthetic-events.h" 26 #include "sort.h" 27 #include "strlist.h" 28 #include "target.h" 29 #include "thread.h" 30 #include "util.h" 31 #include "vdso.h" 32 #include <stdbool.h> 33 #include <sys/types.h> 34 #include <sys/stat.h> 35 #include <unistd.h> 36 #include "unwind.h" 37 #include "linux/hash.h" 38 #include "asm/bug.h" 39 #include "bpf-event.h" 40 #include <internal/lib.h> // page_size 41 #include "cgroup.h" 42 #include "arm64-frame-pointer-unwind-support.h" 43 #include <api/io_dir.h> 44 45 #include <linux/ctype.h> 46 #include <symbol/kallsyms.h> 47 #include <linux/mman.h> 48 #include <linux/string.h> 49 #include <linux/zalloc.h> 50 51 static struct dso *machine__kernel_dso(struct machine *machine) 52 { 53 return map__dso(machine->vmlinux_map); 54 } 55 56 static int machine__set_mmap_name(struct machine *machine) 57 { 58 if (machine__is_host(machine)) 59 machine->mmap_name = strdup("[kernel.kallsyms]"); 60 else if (machine__is_default_guest(machine)) 61 machine->mmap_name = strdup("[guest.kernel.kallsyms]"); 62 else if (asprintf(&machine->mmap_name, "[guest.kernel.kallsyms.%d]", 63 machine->pid) < 0) 64 machine->mmap_name = NULL; 65 66 return machine->mmap_name ? 0 : -ENOMEM; 67 } 68 69 static void thread__set_guest_comm(struct thread *thread, pid_t pid) 70 { 71 char comm[64]; 72 73 snprintf(comm, sizeof(comm), "[guest/%d]", pid); 74 thread__set_comm(thread, comm, 0); 75 } 76 77 int machine__init(struct machine *machine, const char *root_dir, pid_t pid) 78 { 79 int err = -ENOMEM; 80 81 memset(machine, 0, sizeof(*machine)); 82 machine->kmaps = maps__new(machine); 83 if (machine->kmaps == NULL) 84 return -ENOMEM; 85 86 RB_CLEAR_NODE(&machine->rb_node); 87 dsos__init(&machine->dsos); 88 89 threads__init(&machine->threads); 90 91 machine->vdso_info = NULL; 92 machine->env = NULL; 93 94 machine->pid = pid; 95 96 machine->id_hdr_size = 0; 97 machine->kptr_restrict_warned = false; 98 machine->comm_exec = false; 99 machine->kernel_start = 0; 100 machine->vmlinux_map = NULL; 101 /* There is no initial context switch in, so we start at 1. */ 102 machine->parallelism = 1; 103 104 machine->root_dir = strdup(root_dir); 105 if (machine->root_dir == NULL) 106 goto out; 107 108 if (machine__set_mmap_name(machine)) 109 goto out; 110 111 if (pid != HOST_KERNEL_ID) { 112 struct thread *thread = machine__findnew_thread(machine, -1, 113 pid); 114 115 if (thread == NULL) 116 goto out; 117 118 thread__set_guest_comm(thread, pid); 119 thread__put(thread); 120 } 121 122 machine->current_tid = NULL; 123 err = 0; 124 125 out: 126 if (err) { 127 zfree(&machine->kmaps); 128 zfree(&machine->root_dir); 129 zfree(&machine->mmap_name); 130 } 131 return 0; 132 } 133 134 static struct machine *__machine__new_host(struct perf_env *host_env, bool kernel_maps) 135 { 136 struct machine *machine = malloc(sizeof(*machine)); 137 138 if (!machine) 139 return NULL; 140 141 machine__init(machine, "", HOST_KERNEL_ID); 142 143 if (kernel_maps && machine__create_kernel_maps(machine) < 0) { 144 free(machine); 145 return NULL; 146 } 147 machine->env = host_env; 148 return machine; 149 } 150 151 struct machine *machine__new_host(struct perf_env *host_env) 152 { 153 return __machine__new_host(host_env, /*kernel_maps=*/true); 154 } 155 156 static int mmap_handler(const struct perf_tool *tool __maybe_unused, 157 union perf_event *event, 158 struct perf_sample *sample, 159 struct machine *machine) 160 { 161 return machine__process_mmap2_event(machine, event, sample); 162 } 163 164 static int machine__init_live(struct machine *machine, pid_t pid) 165 { 166 union perf_event event; 167 168 memset(&event, 0, sizeof(event)); 169 return perf_event__synthesize_mmap_events(NULL, &event, pid, pid, 170 mmap_handler, machine, true); 171 } 172 173 struct machine *machine__new_live(struct perf_env *host_env, bool kernel_maps, pid_t pid) 174 { 175 struct machine *machine = __machine__new_host(host_env, kernel_maps); 176 177 if (!machine) 178 return NULL; 179 180 if (machine__init_live(machine, pid)) { 181 machine__delete(machine); 182 return NULL; 183 } 184 return machine; 185 } 186 187 struct machine *machine__new_kallsyms(struct perf_env *host_env) 188 { 189 struct machine *machine = machine__new_host(host_env); 190 /* 191 * FIXME: 192 * 1) We should switch to machine__load_kallsyms(), i.e. not explicitly 193 * ask for not using the kcore parsing code, once this one is fixed 194 * to create a map per module. 195 */ 196 if (machine && machine__load_kallsyms(machine, "/proc/kallsyms") <= 0) { 197 machine__delete(machine); 198 machine = NULL; 199 } 200 201 return machine; 202 } 203 204 void machine__delete_threads(struct machine *machine) 205 { 206 threads__remove_all_threads(&machine->threads); 207 } 208 209 void machine__exit(struct machine *machine) 210 { 211 if (machine == NULL) 212 return; 213 214 machine__destroy_kernel_maps(machine); 215 maps__zput(machine->kmaps); 216 dsos__exit(&machine->dsos); 217 machine__exit_vdso(machine); 218 zfree(&machine->root_dir); 219 zfree(&machine->mmap_name); 220 zfree(&machine->current_tid); 221 zfree(&machine->kallsyms_filename); 222 223 threads__exit(&machine->threads); 224 } 225 226 void machine__delete(struct machine *machine) 227 { 228 if (machine) { 229 machine__exit(machine); 230 free(machine); 231 } 232 } 233 234 void machines__init(struct machines *machines) 235 { 236 machine__init(&machines->host, "", HOST_KERNEL_ID); 237 machines->guests = RB_ROOT_CACHED; 238 } 239 240 void machines__exit(struct machines *machines) 241 { 242 machine__exit(&machines->host); 243 /* XXX exit guest */ 244 } 245 246 struct machine *machines__add(struct machines *machines, pid_t pid, 247 const char *root_dir) 248 { 249 struct rb_node **p = &machines->guests.rb_root.rb_node; 250 struct rb_node *parent = NULL; 251 struct machine *pos, *machine = malloc(sizeof(*machine)); 252 bool leftmost = true; 253 254 if (machine == NULL) 255 return NULL; 256 257 if (machine__init(machine, root_dir, pid) != 0) { 258 free(machine); 259 return NULL; 260 } 261 262 while (*p != NULL) { 263 parent = *p; 264 pos = rb_entry(parent, struct machine, rb_node); 265 if (pid < pos->pid) 266 p = &(*p)->rb_left; 267 else { 268 p = &(*p)->rb_right; 269 leftmost = false; 270 } 271 } 272 273 rb_link_node(&machine->rb_node, parent, p); 274 rb_insert_color_cached(&machine->rb_node, &machines->guests, leftmost); 275 276 machine->machines = machines; 277 278 return machine; 279 } 280 281 void machines__set_comm_exec(struct machines *machines, bool comm_exec) 282 { 283 struct rb_node *nd; 284 285 machines->host.comm_exec = comm_exec; 286 287 for (nd = rb_first_cached(&machines->guests); nd; nd = rb_next(nd)) { 288 struct machine *machine = rb_entry(nd, struct machine, rb_node); 289 290 machine->comm_exec = comm_exec; 291 } 292 } 293 294 struct machine *machines__find(struct machines *machines, pid_t pid) 295 { 296 struct rb_node **p = &machines->guests.rb_root.rb_node; 297 struct rb_node *parent = NULL; 298 struct machine *machine; 299 struct machine *default_machine = NULL; 300 301 if (pid == HOST_KERNEL_ID) 302 return &machines->host; 303 304 while (*p != NULL) { 305 parent = *p; 306 machine = rb_entry(parent, struct machine, rb_node); 307 if (pid < machine->pid) 308 p = &(*p)->rb_left; 309 else if (pid > machine->pid) 310 p = &(*p)->rb_right; 311 else 312 return machine; 313 if (!machine->pid) 314 default_machine = machine; 315 } 316 317 return default_machine; 318 } 319 320 struct machine *machines__findnew(struct machines *machines, pid_t pid) 321 { 322 char path[PATH_MAX]; 323 const char *root_dir = ""; 324 struct machine *machine = machines__find(machines, pid); 325 326 if (machine && (machine->pid == pid)) 327 goto out; 328 329 if ((pid != HOST_KERNEL_ID) && 330 (pid != DEFAULT_GUEST_KERNEL_ID) && 331 (symbol_conf.guestmount)) { 332 sprintf(path, "%s/%d", symbol_conf.guestmount, pid); 333 if (access(path, R_OK)) { 334 static struct strlist *seen; 335 336 if (!seen) 337 seen = strlist__new(NULL, NULL); 338 339 if (!strlist__has_entry(seen, path)) { 340 pr_err("Can't access file %s\n", path); 341 strlist__add(seen, path); 342 } 343 machine = NULL; 344 goto out; 345 } 346 root_dir = path; 347 } 348 349 machine = machines__add(machines, pid, root_dir); 350 out: 351 return machine; 352 } 353 354 struct machine *machines__find_guest(struct machines *machines, pid_t pid) 355 { 356 struct machine *machine = machines__find(machines, pid); 357 358 if (!machine) 359 machine = machines__findnew(machines, DEFAULT_GUEST_KERNEL_ID); 360 return machine; 361 } 362 363 /* 364 * A common case for KVM test programs is that the test program acts as the 365 * hypervisor, creating, running and destroying the virtual machine, and 366 * providing the guest object code from its own object code. In this case, 367 * the VM is not running an OS, but only the functions loaded into it by the 368 * hypervisor test program, and conveniently, loaded at the same virtual 369 * addresses. 370 * 371 * Normally to resolve addresses, MMAP events are needed to map addresses 372 * back to the object code and debug symbols for that object code. 373 * 374 * Currently, there is no way to get such mapping information from guests 375 * but, in the scenario described above, the guest has the same mappings 376 * as the hypervisor, so support for that scenario can be achieved. 377 * 378 * To support that, copy the host thread's maps to the guest thread's maps. 379 * Note, we do not discover the guest until we encounter a guest event, 380 * which works well because it is not until then that we know that the host 381 * thread's maps have been set up. 382 * 383 * This function returns the guest thread. Apart from keeping the data 384 * structures sane, using a thread belonging to the guest machine, instead 385 * of the host thread, allows it to have its own comm (refer 386 * thread__set_guest_comm()). 387 */ 388 static struct thread *findnew_guest_code(struct machine *machine, 389 struct machine *host_machine, 390 pid_t pid) 391 { 392 struct thread *host_thread; 393 struct thread *thread; 394 int err; 395 396 if (!machine) 397 return NULL; 398 399 thread = machine__findnew_thread(machine, -1, pid); 400 if (!thread) 401 return NULL; 402 403 /* Assume maps are set up if there are any */ 404 if (!maps__empty(thread__maps(thread))) 405 return thread; 406 407 host_thread = machine__find_thread(host_machine, -1, pid); 408 if (!host_thread) 409 goto out_err; 410 411 thread__set_guest_comm(thread, pid); 412 413 /* 414 * Guest code can be found in hypervisor process at the same address 415 * so copy host maps. 416 */ 417 err = maps__copy_from(thread__maps(thread), thread__maps(host_thread)); 418 thread__put(host_thread); 419 if (err) 420 goto out_err; 421 422 return thread; 423 424 out_err: 425 thread__zput(thread); 426 return NULL; 427 } 428 429 struct thread *machines__findnew_guest_code(struct machines *machines, pid_t pid) 430 { 431 struct machine *host_machine = machines__find(machines, HOST_KERNEL_ID); 432 struct machine *machine = machines__findnew(machines, pid); 433 434 return findnew_guest_code(machine, host_machine, pid); 435 } 436 437 struct thread *machine__findnew_guest_code(struct machine *machine, pid_t pid) 438 { 439 struct machines *machines = machine->machines; 440 struct machine *host_machine; 441 442 if (!machines) 443 return NULL; 444 445 host_machine = machines__find(machines, HOST_KERNEL_ID); 446 447 return findnew_guest_code(machine, host_machine, pid); 448 } 449 450 void machines__process_guests(struct machines *machines, 451 machine__process_t process, void *data) 452 { 453 struct rb_node *nd; 454 455 for (nd = rb_first_cached(&machines->guests); nd; nd = rb_next(nd)) { 456 struct machine *pos = rb_entry(nd, struct machine, rb_node); 457 process(pos, data); 458 } 459 } 460 461 void machines__set_id_hdr_size(struct machines *machines, u16 id_hdr_size) 462 { 463 struct rb_node *node; 464 struct machine *machine; 465 466 machines->host.id_hdr_size = id_hdr_size; 467 468 for (node = rb_first_cached(&machines->guests); node; 469 node = rb_next(node)) { 470 machine = rb_entry(node, struct machine, rb_node); 471 machine->id_hdr_size = id_hdr_size; 472 } 473 474 return; 475 } 476 477 static void machine__update_thread_pid(struct machine *machine, 478 struct thread *th, pid_t pid) 479 { 480 struct thread *leader; 481 482 if (pid == thread__pid(th) || pid == -1 || thread__pid(th) != -1) 483 return; 484 485 thread__set_pid(th, pid); 486 487 if (thread__pid(th) == thread__tid(th)) 488 return; 489 490 leader = machine__findnew_thread(machine, thread__pid(th), thread__pid(th)); 491 if (!leader) 492 goto out_err; 493 494 if (!thread__maps(leader)) 495 thread__set_maps(leader, maps__new(machine)); 496 497 if (!thread__maps(leader)) 498 goto out_err; 499 500 if (thread__maps(th) == thread__maps(leader)) 501 goto out_put; 502 503 if (thread__maps(th)) { 504 /* 505 * Maps are created from MMAP events which provide the pid and 506 * tid. Consequently there never should be any maps on a thread 507 * with an unknown pid. Just print an error if there are. 508 */ 509 if (!maps__empty(thread__maps(th))) 510 pr_err("Discarding thread maps for %d:%d\n", 511 thread__pid(th), thread__tid(th)); 512 maps__put(thread__maps(th)); 513 } 514 515 thread__set_maps(th, maps__get(thread__maps(leader))); 516 out_put: 517 thread__put(leader); 518 return; 519 out_err: 520 pr_err("Failed to join map groups for %d:%d\n", thread__pid(th), thread__tid(th)); 521 goto out_put; 522 } 523 524 /* 525 * Caller must eventually drop thread->refcnt returned with a successful 526 * lookup/new thread inserted. 527 */ 528 static struct thread *__machine__findnew_thread(struct machine *machine, 529 pid_t pid, 530 pid_t tid, 531 bool create) 532 { 533 struct thread *th = threads__find(&machine->threads, tid); 534 bool created; 535 536 if (th) { 537 machine__update_thread_pid(machine, th, pid); 538 return th; 539 } 540 if (!create) 541 return NULL; 542 543 th = threads__findnew(&machine->threads, pid, tid, &created); 544 if (created) { 545 /* 546 * We have to initialize maps separately after rb tree is 547 * updated. 548 * 549 * The reason is that we call machine__findnew_thread within 550 * thread__init_maps to find the thread leader and that would 551 * screwed the rb tree. 552 */ 553 if (thread__init_maps(th, machine)) { 554 pr_err("Thread init failed thread %d\n", pid); 555 threads__remove(&machine->threads, th); 556 thread__put(th); 557 return NULL; 558 } 559 } else 560 machine__update_thread_pid(machine, th, pid); 561 562 return th; 563 } 564 565 struct thread *machine__findnew_thread(struct machine *machine, pid_t pid, pid_t tid) 566 { 567 return __machine__findnew_thread(machine, pid, tid, /*create=*/true); 568 } 569 570 struct thread *machine__find_thread(struct machine *machine, pid_t pid, 571 pid_t tid) 572 { 573 return __machine__findnew_thread(machine, pid, tid, /*create=*/false); 574 } 575 576 /* 577 * Threads are identified by pid and tid, and the idle task has pid == tid == 0. 578 * So here a single thread is created for that, but actually there is a separate 579 * idle task per cpu, so there should be one 'struct thread' per cpu, but there 580 * is only 1. That causes problems for some tools, requiring workarounds. For 581 * example get_idle_thread() in builtin-sched.c, or thread_stack__per_cpu(). 582 */ 583 struct thread *machine__idle_thread(struct machine *machine) 584 { 585 struct thread *thread = machine__findnew_thread(machine, 0, 0); 586 587 if (!thread || thread__set_comm(thread, "swapper", 0) || 588 thread__set_namespaces(thread, 0, NULL)) 589 pr_err("problem inserting idle task for machine pid %d\n", machine->pid); 590 591 return thread; 592 } 593 594 struct comm *machine__thread_exec_comm(struct machine *machine, 595 struct thread *thread) 596 { 597 if (machine->comm_exec) 598 return thread__exec_comm(thread); 599 else 600 return thread__comm(thread); 601 } 602 603 int machine__process_comm_event(struct machine *machine, union perf_event *event, 604 struct perf_sample *sample) 605 { 606 struct thread *thread = machine__findnew_thread(machine, 607 event->comm.pid, 608 event->comm.tid); 609 bool exec = event->header.misc & PERF_RECORD_MISC_COMM_EXEC; 610 int err = 0; 611 612 if (exec) 613 machine->comm_exec = true; 614 615 if (dump_trace) 616 perf_event__fprintf_comm(event, stdout); 617 618 if (thread == NULL || 619 __thread__set_comm(thread, event->comm.comm, sample->time, exec)) { 620 dump_printf("problem processing PERF_RECORD_COMM, skipping event.\n"); 621 err = -1; 622 } 623 624 thread__put(thread); 625 626 return err; 627 } 628 629 int machine__process_namespaces_event(struct machine *machine __maybe_unused, 630 union perf_event *event, 631 struct perf_sample *sample __maybe_unused) 632 { 633 struct thread *thread = machine__findnew_thread(machine, 634 event->namespaces.pid, 635 event->namespaces.tid); 636 int err = 0; 637 638 WARN_ONCE(event->namespaces.nr_namespaces > NR_NAMESPACES, 639 "\nWARNING: kernel seems to support more namespaces than perf" 640 " tool.\nTry updating the perf tool..\n\n"); 641 642 WARN_ONCE(event->namespaces.nr_namespaces < NR_NAMESPACES, 643 "\nWARNING: perf tool seems to support more namespaces than" 644 " the kernel.\nTry updating the kernel..\n\n"); 645 646 if (dump_trace) 647 perf_event__fprintf_namespaces(event, stdout); 648 649 if (thread == NULL || 650 thread__set_namespaces(thread, sample->time, &event->namespaces)) { 651 dump_printf("problem processing PERF_RECORD_NAMESPACES, skipping event.\n"); 652 err = -1; 653 } 654 655 thread__put(thread); 656 657 return err; 658 } 659 660 int machine__process_cgroup_event(struct machine *machine, 661 union perf_event *event, 662 struct perf_sample *sample __maybe_unused) 663 { 664 struct cgroup *cgrp; 665 666 if (dump_trace) 667 perf_event__fprintf_cgroup(event, stdout); 668 669 cgrp = cgroup__findnew(machine->env, event->cgroup.id, event->cgroup.path); 670 if (cgrp == NULL) 671 return -ENOMEM; 672 673 return 0; 674 } 675 676 int machine__process_lost_event(struct machine *machine __maybe_unused, 677 union perf_event *event, struct perf_sample *sample __maybe_unused) 678 { 679 dump_printf(": id:%" PRI_lu64 ": lost:%" PRI_lu64 "\n", 680 event->lost.id, event->lost.lost); 681 return 0; 682 } 683 684 int machine__process_lost_samples_event(struct machine *machine __maybe_unused, 685 union perf_event *event, struct perf_sample *sample) 686 { 687 dump_printf(": id:%" PRIu64 ": lost samples :%" PRI_lu64 "%s\n", 688 sample->id, event->lost_samples.lost, 689 event->header.misc & PERF_RECORD_MISC_LOST_SAMPLES_BPF ? " (BPF)" : ""); 690 return 0; 691 } 692 693 int machine__process_aux_event(struct machine *machine __maybe_unused, 694 union perf_event *event) 695 { 696 if (dump_trace) 697 perf_event__fprintf_aux(event, stdout); 698 return 0; 699 } 700 701 int machine__process_itrace_start_event(struct machine *machine __maybe_unused, 702 union perf_event *event) 703 { 704 if (dump_trace) 705 perf_event__fprintf_itrace_start(event, stdout); 706 return 0; 707 } 708 709 int machine__process_aux_output_hw_id_event(struct machine *machine __maybe_unused, 710 union perf_event *event) 711 { 712 if (dump_trace) 713 perf_event__fprintf_aux_output_hw_id(event, stdout); 714 return 0; 715 } 716 717 int machine__process_switch_event(struct machine *machine __maybe_unused, 718 union perf_event *event) 719 { 720 bool out = event->header.misc & PERF_RECORD_MISC_SWITCH_OUT; 721 722 if (dump_trace) 723 perf_event__fprintf_switch(event, stdout); 724 machine->parallelism += out ? -1 : 1; 725 return 0; 726 } 727 728 static int machine__process_ksymbol_register(struct machine *machine, 729 union perf_event *event, 730 struct perf_sample *sample __maybe_unused) 731 { 732 struct symbol *sym; 733 struct dso *dso = NULL; 734 struct map *map; 735 int err = 0; 736 737 /* Ignore mapping symbols in ksymbol events - check early before any state mutation */ 738 if (is_ignored_kernel_symbol(event->ksymbol.name)) 739 return 0; 740 741 map = maps__find(machine__kernel_maps(machine), event->ksymbol.addr); 742 743 if (!map) { 744 dso = dso__new(event->ksymbol.name); 745 746 if (!dso) { 747 err = -ENOMEM; 748 goto out; 749 } 750 dso__set_kernel(dso, DSO_SPACE__KERNEL); 751 map = map__new2(0, dso); 752 if (!map) { 753 err = -ENOMEM; 754 goto out; 755 } 756 if (event->ksymbol.ksym_type == PERF_RECORD_KSYMBOL_TYPE_OOL) { 757 dso__set_binary_type(dso, DSO_BINARY_TYPE__OOL); 758 dso__data(dso)->file_size = event->ksymbol.len; 759 dso__set_loaded(dso); 760 } 761 762 map__set_start(map, event->ksymbol.addr); 763 map__set_end(map, map__start(map) + event->ksymbol.len); 764 err = maps__fixup_overlap_and_insert(machine__kernel_maps(machine), map); 765 if (err) { 766 err = -ENOMEM; 767 goto out; 768 } 769 770 dso__set_loaded(dso); 771 772 if (is_bpf_image(event->ksymbol.name)) { 773 dso__set_binary_type(dso, DSO_BINARY_TYPE__BPF_IMAGE); 774 dso__set_long_name(dso, "", false); 775 } 776 } else { 777 dso = dso__get(map__dso(map)); 778 } 779 780 sym = symbol__new(map__map_ip(map, map__start(map)), 781 event->ksymbol.len, 782 0, 0, event->ksymbol.name); 783 if (!sym) { 784 err = -ENOMEM; 785 goto out; 786 } 787 dso__insert_symbol(dso, sym); 788 out: 789 map__put(map); 790 dso__put(dso); 791 return err; 792 } 793 794 static int machine__process_ksymbol_unregister(struct machine *machine, 795 union perf_event *event, 796 struct perf_sample *sample __maybe_unused) 797 { 798 struct symbol *sym; 799 struct map *map; 800 801 /* Ignore mapping symbols in ksymbol events */ 802 if (is_ignored_kernel_symbol(event->ksymbol.name)) 803 return 0; 804 805 map = maps__find(machine__kernel_maps(machine), event->ksymbol.addr); 806 if (!map) 807 return 0; 808 809 if (!RC_CHK_EQUAL(map, machine->vmlinux_map)) 810 maps__remove(machine__kernel_maps(machine), map); 811 else { 812 struct dso *dso = map__dso(map); 813 814 sym = dso__find_symbol(dso, map__map_ip(map, map__start(map))); 815 if (sym) 816 dso__delete_symbol(dso, sym); 817 } 818 map__put(map); 819 return 0; 820 } 821 822 int machine__process_ksymbol(struct machine *machine __maybe_unused, 823 union perf_event *event, 824 struct perf_sample *sample) 825 { 826 if (dump_trace) 827 perf_event__fprintf_ksymbol(event, stdout); 828 829 if (event->header.size < offsetof(struct perf_record_ksymbol, name) + 2 || 830 !memchr(event->ksymbol.name, '\0', 831 event->header.size - offsetof(struct perf_record_ksymbol, name))) 832 return -EINVAL; 833 834 /* no need to process non-JIT BPF as it cannot get samples */ 835 if (event->ksymbol.len == 0) 836 return 0; 837 838 if (event->ksymbol.flags & PERF_RECORD_KSYMBOL_FLAGS_UNREGISTER) 839 return machine__process_ksymbol_unregister(machine, event, 840 sample); 841 return machine__process_ksymbol_register(machine, event, sample); 842 } 843 844 int machine__process_text_poke(struct machine *machine, union perf_event *event, 845 struct perf_sample *sample __maybe_unused) 846 { 847 struct map *map = maps__find(machine__kernel_maps(machine), event->text_poke.addr); 848 u8 cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK; 849 struct dso *dso = map ? map__dso(map) : NULL; 850 851 if (dump_trace) 852 perf_event__fprintf_text_poke(event, machine, stdout); 853 854 if (!event->text_poke.new_len) 855 goto out; 856 857 if (cpumode != PERF_RECORD_MISC_KERNEL) { 858 pr_debug("%s: unsupported cpumode - ignoring\n", __func__); 859 goto out; 860 } 861 862 if (dso) { 863 u8 *new_bytes = event->text_poke.bytes + event->text_poke.old_len; 864 int ret; 865 866 /* 867 * Kernel maps might be changed when loading symbols so loading 868 * must be done prior to using kernel maps. 869 */ 870 map__load(map); 871 ret = dso__data_write_cache_addr(dso, map, machine, 872 event->text_poke.addr, 873 new_bytes, 874 event->text_poke.new_len); 875 if (ret != event->text_poke.new_len) 876 pr_debug("Failed to write kernel text poke at %#" PRI_lx64 "\n", 877 event->text_poke.addr); 878 } else { 879 pr_debug("Failed to find kernel text poke address map for %#" PRI_lx64 "\n", 880 event->text_poke.addr); 881 } 882 out: 883 map__put(map); 884 return 0; 885 } 886 887 static struct map *machine__addnew_module_map(struct machine *machine, u64 start, 888 const char *filename) 889 { 890 struct map *map = NULL; 891 struct kmod_path m; 892 struct dso *dso; 893 int err; 894 895 if (kmod_path__parse_name(&m, filename)) 896 return NULL; 897 898 dso = dsos__findnew_module_dso(&machine->dsos, machine, &m, filename); 899 if (dso == NULL) 900 goto out; 901 902 map = map__new2(start, dso); 903 if (map == NULL) 904 goto out; 905 906 err = maps__insert(machine__kernel_maps(machine), map); 907 /* If maps__insert failed, return NULL. */ 908 if (err) { 909 map__put(map); 910 map = NULL; 911 } 912 out: 913 /* put the dso here, corresponding to machine__findnew_module_dso */ 914 dso__put(dso); 915 zfree(&m.name); 916 return map; 917 } 918 919 size_t machines__fprintf_dsos(struct machines *machines, FILE *fp) 920 { 921 struct rb_node *nd; 922 size_t ret = dsos__fprintf(&machines->host.dsos, fp); 923 924 for (nd = rb_first_cached(&machines->guests); nd; nd = rb_next(nd)) { 925 struct machine *pos = rb_entry(nd, struct machine, rb_node); 926 ret += dsos__fprintf(&pos->dsos, fp); 927 } 928 929 return ret; 930 } 931 932 size_t machine__fprintf_dsos_buildid(struct machine *m, FILE *fp, 933 bool (skip)(struct dso *dso, int parm), int parm) 934 { 935 return dsos__fprintf_buildid(&m->dsos, fp, skip, parm); 936 } 937 938 size_t machines__fprintf_dsos_buildid(struct machines *machines, FILE *fp, 939 bool (skip)(struct dso *dso, int parm), int parm) 940 { 941 struct rb_node *nd; 942 size_t ret = machine__fprintf_dsos_buildid(&machines->host, fp, skip, parm); 943 944 for (nd = rb_first_cached(&machines->guests); nd; nd = rb_next(nd)) { 945 struct machine *pos = rb_entry(nd, struct machine, rb_node); 946 ret += machine__fprintf_dsos_buildid(pos, fp, skip, parm); 947 } 948 return ret; 949 } 950 951 struct machine_fprintf_cb_args { 952 FILE *fp; 953 size_t printed; 954 }; 955 956 static int machine_fprintf_cb(struct thread *thread, void *data) 957 { 958 struct machine_fprintf_cb_args *args = data; 959 960 /* TODO: handle fprintf errors. */ 961 args->printed += thread__fprintf(thread, args->fp); 962 return 0; 963 } 964 965 size_t machine__fprintf(struct machine *machine, FILE *fp) 966 { 967 struct machine_fprintf_cb_args args = { 968 .fp = fp, 969 .printed = 0, 970 }; 971 size_t ret = fprintf(fp, "Threads: %zu\n", threads__nr(&machine->threads)); 972 973 machine__for_each_thread(machine, machine_fprintf_cb, &args); 974 return ret + args.printed; 975 } 976 977 static struct dso *machine__get_kernel(struct machine *machine) 978 { 979 const char *vmlinux_name = machine->mmap_name; 980 struct dso *kernel; 981 982 if (machine__is_host(machine)) { 983 if (symbol_conf.vmlinux_name) 984 vmlinux_name = symbol_conf.vmlinux_name; 985 986 kernel = machine__findnew_kernel(machine, vmlinux_name, 987 "[kernel]", DSO_SPACE__KERNEL); 988 } else { 989 if (symbol_conf.default_guest_vmlinux_name) 990 vmlinux_name = symbol_conf.default_guest_vmlinux_name; 991 992 kernel = machine__findnew_kernel(machine, vmlinux_name, 993 "[guest.kernel]", 994 DSO_SPACE__KERNEL_GUEST); 995 } 996 997 if (kernel != NULL && (!dso__has_build_id(kernel))) 998 dso__read_running_kernel_build_id(kernel, machine); 999 1000 return kernel; 1001 } 1002 1003 void machine__get_kallsyms_filename(struct machine *machine, char *buf, 1004 size_t bufsz) 1005 { 1006 if (machine__is_default_guest(machine)) 1007 scnprintf(buf, bufsz, "%s", symbol_conf.default_guest_kallsyms); 1008 else 1009 scnprintf(buf, bufsz, "%s/proc/kallsyms", machine->root_dir); 1010 } 1011 1012 const char *ref_reloc_sym_names[] = {"_text", "_stext", NULL}; 1013 1014 /* Figure out the start address of kernel map from /proc/kallsyms. 1015 * Returns the name of the start symbol in *symbol_name. Pass in NULL as 1016 * symbol_name if it's not that important. 1017 */ 1018 static int machine__get_running_kernel_start(struct machine *machine, 1019 const char **symbol_name, 1020 u64 *start, u64 *end) 1021 { 1022 char filename[PATH_MAX]; 1023 int i, err = -1; 1024 const char *name; 1025 u64 addr = 0; 1026 1027 machine__get_kallsyms_filename(machine, filename, PATH_MAX); 1028 1029 if (symbol__restricted_filename(filename, "/proc/kallsyms")) 1030 return 0; 1031 1032 for (i = 0; (name = ref_reloc_sym_names[i]) != NULL; i++) { 1033 err = kallsyms__get_function_start(filename, name, &addr); 1034 if (!err) 1035 break; 1036 } 1037 1038 if (err) 1039 return -1; 1040 1041 if (symbol_name) 1042 *symbol_name = name; 1043 1044 *start = addr; 1045 1046 err = kallsyms__get_symbol_start(filename, "_edata", &addr); 1047 if (err) 1048 err = kallsyms__get_symbol_start(filename, "_etext", &addr); 1049 if (!err) 1050 *end = addr; 1051 1052 return 0; 1053 } 1054 1055 int machine__create_extra_kernel_map(struct machine *machine, 1056 struct dso *kernel, 1057 struct extra_kernel_map *xm) 1058 { 1059 struct kmap *kmap; 1060 struct map *map; 1061 int err; 1062 1063 map = map__new2(xm->start, kernel); 1064 if (!map) 1065 return -ENOMEM; 1066 1067 map__set_end(map, xm->end); 1068 map__set_pgoff(map, xm->pgoff); 1069 1070 kmap = map__kmap(map); 1071 1072 strlcpy(kmap->name, xm->name, KMAP_NAME_LEN); 1073 1074 err = maps__insert(machine__kernel_maps(machine), map); 1075 1076 if (!err) { 1077 pr_debug2("Added extra kernel map %s %" PRIx64 "-%" PRIx64 "\n", 1078 kmap->name, map__start(map), map__end(map)); 1079 } 1080 1081 map__put(map); 1082 1083 return err; 1084 } 1085 1086 static u64 find_entry_trampoline(struct dso *dso) 1087 { 1088 /* Duplicates are removed so lookup all aliases */ 1089 const char *syms[] = { 1090 "_entry_trampoline", 1091 "__entry_trampoline_start", 1092 "entry_SYSCALL_64_trampoline", 1093 }; 1094 struct symbol *sym = dso__first_symbol(dso); 1095 unsigned int i; 1096 1097 for (; sym; sym = dso__next_symbol(sym)) { 1098 if (symbol__binding(sym) != STB_GLOBAL) 1099 continue; 1100 for (i = 0; i < ARRAY_SIZE(syms); i++) { 1101 if (!strcmp(sym->name, syms[i])) 1102 return sym->start; 1103 } 1104 } 1105 1106 return 0; 1107 } 1108 1109 /* 1110 * These values can be used for kernels that do not have symbols for the entry 1111 * trampolines in kallsyms. 1112 */ 1113 #define X86_64_CPU_ENTRY_AREA_PER_CPU 0xfffffe0000000000ULL 1114 #define X86_64_CPU_ENTRY_AREA_SIZE 0x2c000 1115 #define X86_64_ENTRY_TRAMPOLINE 0x6000 1116 1117 struct machine__map_x86_64_entry_trampolines_args { 1118 struct maps *kmaps; 1119 bool found; 1120 }; 1121 1122 static int machine__map_x86_64_entry_trampolines_cb(struct map *map, void *data) 1123 { 1124 struct machine__map_x86_64_entry_trampolines_args *args = data; 1125 struct map *dest_map; 1126 struct kmap *kmap = __map__kmap(map); 1127 1128 if (!kmap || !is_entry_trampoline(kmap->name)) 1129 return 0; 1130 1131 dest_map = maps__find(args->kmaps, map__pgoff(map)); 1132 if (RC_CHK_ACCESS(dest_map) != RC_CHK_ACCESS(map)) 1133 map__set_pgoff(map, map__map_ip(dest_map, map__pgoff(map))); 1134 1135 map__put(dest_map); 1136 args->found = true; 1137 return 0; 1138 } 1139 1140 /* Map x86_64 PTI entry trampolines */ 1141 int machine__map_x86_64_entry_trampolines(struct machine *machine, 1142 struct dso *kernel) 1143 { 1144 struct machine__map_x86_64_entry_trampolines_args args = { 1145 .kmaps = machine__kernel_maps(machine), 1146 .found = false, 1147 }; 1148 int nr_cpus_avail, cpu; 1149 u64 pgoff; 1150 1151 /* 1152 * In the vmlinux case, pgoff is a virtual address which must now be 1153 * mapped to a vmlinux offset. 1154 */ 1155 maps__for_each_map(args.kmaps, machine__map_x86_64_entry_trampolines_cb, &args); 1156 1157 if (args.found || machine->trampolines_mapped) 1158 return 0; 1159 1160 pgoff = find_entry_trampoline(kernel); 1161 if (!pgoff) 1162 return 0; 1163 1164 nr_cpus_avail = machine__nr_cpus_avail(machine); 1165 1166 /* Add a 1 page map for each CPU's entry trampoline */ 1167 for (cpu = 0; cpu < nr_cpus_avail; cpu++) { 1168 u64 va = X86_64_CPU_ENTRY_AREA_PER_CPU + 1169 cpu * X86_64_CPU_ENTRY_AREA_SIZE + 1170 X86_64_ENTRY_TRAMPOLINE; 1171 struct extra_kernel_map xm = { 1172 .start = va, 1173 .end = va + page_size, 1174 .pgoff = pgoff, 1175 }; 1176 1177 strlcpy(xm.name, ENTRY_TRAMPOLINE_NAME, KMAP_NAME_LEN); 1178 1179 if (machine__create_extra_kernel_map(machine, kernel, &xm) < 0) 1180 return -1; 1181 } 1182 1183 machine->trampolines_mapped = nr_cpus_avail; 1184 1185 return 0; 1186 } 1187 1188 int __weak machine__create_extra_kernel_maps(struct machine *machine __maybe_unused, 1189 struct dso *kernel __maybe_unused) 1190 { 1191 return 0; 1192 } 1193 1194 static int 1195 __machine__create_kernel_maps(struct machine *machine, struct dso *kernel) 1196 { 1197 /* In case of renewal the kernel map, destroy previous one */ 1198 machine__destroy_kernel_maps(machine); 1199 1200 map__put(machine->vmlinux_map); 1201 machine->vmlinux_map = map__new2(0, kernel); 1202 if (machine->vmlinux_map == NULL) 1203 return -ENOMEM; 1204 1205 map__set_mapping_type(machine->vmlinux_map, MAPPING_TYPE__IDENTITY); 1206 return maps__insert(machine__kernel_maps(machine), machine->vmlinux_map); 1207 } 1208 1209 void machine__destroy_kernel_maps(struct machine *machine) 1210 { 1211 struct kmap *kmap; 1212 struct map *map = machine__kernel_map(machine); 1213 1214 if (map == NULL) 1215 return; 1216 1217 kmap = map__kmap(map); 1218 maps__remove(machine__kernel_maps(machine), map); 1219 if (kmap && kmap->ref_reloc_sym) { 1220 zfree((char **)&kmap->ref_reloc_sym->name); 1221 zfree(&kmap->ref_reloc_sym); 1222 } 1223 1224 map__zput(machine->vmlinux_map); 1225 } 1226 1227 int machines__create_guest_kernel_maps(struct machines *machines) 1228 { 1229 int ret = 0; 1230 struct dirent **namelist = NULL; 1231 int i, items = 0; 1232 char path[PATH_MAX]; 1233 pid_t pid; 1234 char *endp; 1235 1236 if (symbol_conf.default_guest_vmlinux_name || 1237 symbol_conf.default_guest_modules || 1238 symbol_conf.default_guest_kallsyms) { 1239 machines__create_kernel_maps(machines, DEFAULT_GUEST_KERNEL_ID); 1240 } 1241 1242 if (symbol_conf.guestmount) { 1243 items = scandir(symbol_conf.guestmount, &namelist, NULL, NULL); 1244 if (items <= 0) 1245 return -ENOENT; 1246 for (i = 0; i < items; i++) { 1247 if (!isdigit(namelist[i]->d_name[0])) { 1248 /* Filter out . and .. */ 1249 continue; 1250 } 1251 pid = (pid_t)strtol(namelist[i]->d_name, &endp, 10); 1252 if ((*endp != '\0') || 1253 (endp == namelist[i]->d_name) || 1254 (errno == ERANGE)) { 1255 pr_debug("invalid directory (%s). Skipping.\n", 1256 namelist[i]->d_name); 1257 continue; 1258 } 1259 sprintf(path, "%s/%s/proc/kallsyms", 1260 symbol_conf.guestmount, 1261 namelist[i]->d_name); 1262 ret = access(path, R_OK); 1263 if (ret) { 1264 pr_debug("Can't access file %s\n", path); 1265 goto failure; 1266 } 1267 machines__create_kernel_maps(machines, pid); 1268 } 1269 failure: 1270 free(namelist); 1271 } 1272 1273 return ret; 1274 } 1275 1276 void machines__destroy_kernel_maps(struct machines *machines) 1277 { 1278 struct rb_node *next = rb_first_cached(&machines->guests); 1279 1280 machine__destroy_kernel_maps(&machines->host); 1281 1282 while (next) { 1283 struct machine *pos = rb_entry(next, struct machine, rb_node); 1284 1285 next = rb_next(&pos->rb_node); 1286 rb_erase_cached(&pos->rb_node, &machines->guests); 1287 machine__delete(pos); 1288 } 1289 } 1290 1291 int machines__create_kernel_maps(struct machines *machines, pid_t pid) 1292 { 1293 struct machine *machine = machines__findnew(machines, pid); 1294 1295 if (machine == NULL) 1296 return -1; 1297 1298 return machine__create_kernel_maps(machine); 1299 } 1300 1301 int machine__load_kallsyms(struct machine *machine, const char *filename) 1302 { 1303 struct map *map = machine__kernel_map(machine); 1304 struct dso *dso = map__dso(map); 1305 int ret = __dso__load_kallsyms(dso, filename, map, true); 1306 1307 if (ret > 0) { 1308 dso__set_loaded(dso); 1309 /* 1310 * Since /proc/kallsyms will have multiple sessions for the 1311 * kernel, with modules between them, fixup the end of all 1312 * sections. 1313 */ 1314 maps__fixup_end(machine__kernel_maps(machine)); 1315 } 1316 1317 return ret; 1318 } 1319 1320 int machine__load_vmlinux_path(struct machine *machine) 1321 { 1322 struct map *map = machine__kernel_map(machine); 1323 struct dso *dso = map__dso(map); 1324 int ret = dso__load_vmlinux_path(dso, map); 1325 1326 if (ret > 0) 1327 dso__set_loaded(dso); 1328 1329 return ret; 1330 } 1331 1332 static char *get_kernel_version(const char *root_dir) 1333 { 1334 char version[PATH_MAX]; 1335 FILE *file; 1336 char *name, *tmp; 1337 const char *prefix = "Linux version "; 1338 1339 sprintf(version, "%s/proc/version", root_dir); 1340 file = fopen(version, "r"); 1341 if (!file) 1342 return NULL; 1343 1344 tmp = fgets(version, sizeof(version), file); 1345 fclose(file); 1346 if (!tmp) 1347 return NULL; 1348 1349 name = strstr(version, prefix); 1350 if (!name) 1351 return NULL; 1352 name += strlen(prefix); 1353 tmp = strchr(name, ' '); 1354 if (tmp) 1355 *tmp = '\0'; 1356 1357 return strdup(name); 1358 } 1359 1360 static bool is_kmod_dso(struct dso *dso) 1361 { 1362 return dso__symtab_type(dso) == DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE || 1363 dso__symtab_type(dso) == DSO_BINARY_TYPE__GUEST_KMODULE; 1364 } 1365 1366 static int maps__set_module_path(struct maps *maps, const char *path, struct kmod_path *m) 1367 { 1368 char *long_name; 1369 struct dso *dso; 1370 struct map *map = maps__find_by_name(maps, m->name); 1371 1372 if (map == NULL) 1373 return 0; 1374 1375 long_name = strdup(path); 1376 if (long_name == NULL) { 1377 map__put(map); 1378 return -ENOMEM; 1379 } 1380 1381 dso = map__dso(map); 1382 dso__set_long_name(dso, long_name, true); 1383 dso__kernel_module_get_build_id(dso, ""); 1384 1385 /* 1386 * Full name could reveal us kmod compression, so 1387 * we need to update the symtab_type if needed. 1388 */ 1389 if (m->comp && is_kmod_dso(dso)) { 1390 dso__set_symtab_type(dso, dso__symtab_type(dso)+1); 1391 dso__set_comp(dso, m->comp); 1392 } 1393 map__put(map); 1394 return 0; 1395 } 1396 1397 static int maps__set_modules_path_dir(struct maps *maps, char *path, size_t path_size, int depth) 1398 { 1399 struct io_dirent64 *dent; 1400 struct io_dir iod; 1401 size_t root_len = strlen(path); 1402 int ret = 0; 1403 1404 io_dir__init(&iod, open(path, O_CLOEXEC | O_DIRECTORY | O_RDONLY)); 1405 if (iod.dirfd < 0) { 1406 pr_debug("%s: cannot open %s dir\n", __func__, path); 1407 return -1; 1408 } 1409 /* Bounds check, should never happen. */ 1410 if (root_len >= path_size) 1411 return -1; 1412 path[root_len++] = '/'; 1413 while ((dent = io_dir__readdir(&iod)) != NULL) { 1414 if (io_dir__is_dir(&iod, dent)) { 1415 if (!strcmp(dent->d_name, ".") || 1416 !strcmp(dent->d_name, "..")) 1417 continue; 1418 1419 /* Do not follow top-level source and build symlinks */ 1420 if (depth == 0) { 1421 if (!strcmp(dent->d_name, "source") || 1422 !strcmp(dent->d_name, "build")) 1423 continue; 1424 } 1425 1426 /* Bounds check, should never happen. */ 1427 if (root_len + strlen(dent->d_name) >= path_size) 1428 continue; 1429 1430 strcpy(path + root_len, dent->d_name); 1431 ret = maps__set_modules_path_dir(maps, path, path_size, depth + 1); 1432 if (ret < 0) 1433 goto out; 1434 } else { 1435 struct kmod_path m; 1436 1437 ret = kmod_path__parse_name(&m, dent->d_name); 1438 if (ret) 1439 goto out; 1440 1441 if (m.kmod) { 1442 /* Bounds check, should never happen. */ 1443 if (root_len + strlen(dent->d_name) < path_size) { 1444 strcpy(path + root_len, dent->d_name); 1445 ret = maps__set_module_path(maps, path, &m); 1446 1447 } 1448 } 1449 zfree(&m.name); 1450 1451 if (ret) 1452 goto out; 1453 } 1454 } 1455 1456 out: 1457 close(iod.dirfd); 1458 return ret; 1459 } 1460 1461 static int machine__set_modules_path(struct machine *machine) 1462 { 1463 char *version; 1464 char modules_path[PATH_MAX]; 1465 1466 version = get_kernel_version(machine->root_dir); 1467 if (!version) 1468 return -1; 1469 1470 snprintf(modules_path, sizeof(modules_path), "%s/lib/modules/%s", 1471 machine->root_dir, version); 1472 free(version); 1473 1474 return maps__set_modules_path_dir(machine__kernel_maps(machine), 1475 modules_path, sizeof(modules_path), 0); 1476 } 1477 int __weak arch__fix_module_text_start(u64 *start __maybe_unused, 1478 u64 *size __maybe_unused, 1479 const char *name __maybe_unused) 1480 { 1481 return 0; 1482 } 1483 1484 static int machine__create_module(void *arg, const char *name, u64 start, 1485 u64 size) 1486 { 1487 struct machine *machine = arg; 1488 struct map *map; 1489 1490 if (arch__fix_module_text_start(&start, &size, name) < 0) 1491 return -1; 1492 1493 map = machine__addnew_module_map(machine, start, name); 1494 if (map == NULL) 1495 return -1; 1496 map__set_end(map, start + size); 1497 1498 dso__kernel_module_get_build_id(map__dso(map), machine->root_dir); 1499 map__put(map); 1500 return 0; 1501 } 1502 1503 static int machine__create_modules(struct machine *machine) 1504 { 1505 const char *modules; 1506 char path[PATH_MAX]; 1507 1508 if (machine__is_default_guest(machine)) { 1509 modules = symbol_conf.default_guest_modules; 1510 } else { 1511 snprintf(path, PATH_MAX, "%s/proc/modules", machine->root_dir); 1512 modules = path; 1513 } 1514 1515 if (symbol__restricted_filename(modules, "/proc/modules")) 1516 return -1; 1517 1518 if (modules__parse(modules, machine, machine__create_module)) 1519 return -1; 1520 1521 if (!machine__set_modules_path(machine)) 1522 return 0; 1523 1524 pr_debug("Problems setting modules path maps, continuing anyway...\n"); 1525 1526 return 0; 1527 } 1528 1529 static void machine__set_kernel_mmap(struct machine *machine, 1530 u64 start, u64 end) 1531 { 1532 map__set_start(machine->vmlinux_map, start); 1533 map__set_end(machine->vmlinux_map, end); 1534 /* 1535 * Be a bit paranoid here, some perf.data file came with 1536 * a zero sized synthesized MMAP event for the kernel. 1537 */ 1538 if (start == 0 && end == 0) 1539 map__set_end(machine->vmlinux_map, ~0ULL); 1540 } 1541 1542 static int machine__update_kernel_mmap(struct machine *machine, 1543 u64 start, u64 end) 1544 { 1545 struct map *orig, *updated; 1546 int err; 1547 1548 orig = machine->vmlinux_map; 1549 updated = map__get(orig); 1550 1551 machine->vmlinux_map = updated; 1552 maps__remove(machine__kernel_maps(machine), orig); 1553 machine__set_kernel_mmap(machine, start, end); 1554 err = maps__insert(machine__kernel_maps(machine), updated); 1555 map__put(orig); 1556 1557 return err; 1558 } 1559 1560 int machine__create_kernel_maps(struct machine *machine) 1561 { 1562 struct dso *kernel = machine__get_kernel(machine); 1563 const char *name = NULL; 1564 u64 start = 0, end = ~0ULL; 1565 int ret; 1566 1567 if (kernel == NULL) 1568 return -1; 1569 1570 ret = __machine__create_kernel_maps(machine, kernel); 1571 if (ret < 0) 1572 goto out_put; 1573 1574 if (symbol_conf.use_modules && machine__create_modules(machine) < 0) { 1575 if (machine__is_host(machine)) 1576 pr_debug("Problems creating module maps, " 1577 "continuing anyway...\n"); 1578 else 1579 pr_debug("Problems creating module maps for guest %d, " 1580 "continuing anyway...\n", machine->pid); 1581 } 1582 1583 if (!machine__get_running_kernel_start(machine, &name, &start, &end)) { 1584 if (name && 1585 map__set_kallsyms_ref_reloc_sym(machine->vmlinux_map, name, start)) { 1586 machine__destroy_kernel_maps(machine); 1587 ret = -1; 1588 goto out_put; 1589 } 1590 1591 /* 1592 * we have a real start address now, so re-order the kmaps 1593 * assume it's the last in the kmaps 1594 */ 1595 ret = machine__update_kernel_mmap(machine, start, end); 1596 if (ret < 0) 1597 goto out_put; 1598 } 1599 1600 if (machine__create_extra_kernel_maps(machine, kernel)) 1601 pr_debug("Problems creating extra kernel maps, continuing anyway...\n"); 1602 1603 if (end == ~0ULL) { 1604 /* update end address of the kernel map using adjacent module address */ 1605 struct map *next = maps__find_next_entry(machine__kernel_maps(machine), 1606 machine__kernel_map(machine)); 1607 1608 if (next) { 1609 machine__set_kernel_mmap(machine, start, map__start(next)); 1610 map__put(next); 1611 } 1612 } 1613 1614 maps__fixup_end(machine__kernel_maps(machine)); 1615 1616 out_put: 1617 dso__put(kernel); 1618 return ret; 1619 } 1620 1621 static int machine__uses_kcore_cb(struct dso *dso, void *data __maybe_unused) 1622 { 1623 return dso__is_kcore(dso) ? 1 : 0; 1624 } 1625 1626 static bool machine__uses_kcore(struct machine *machine) 1627 { 1628 return dsos__for_each_dso(&machine->dsos, machine__uses_kcore_cb, NULL) != 0 ? true : false; 1629 } 1630 1631 static bool machine__is(struct machine *machine, uint16_t e_machine) 1632 { 1633 if (!machine) 1634 return false; 1635 1636 if (!machine->env) { 1637 if (machine__is_host(machine)) 1638 return e_machine == EM_HOST; 1639 return false; 1640 } 1641 1642 return perf_env__e_machine(machine->env, NULL) == e_machine; 1643 } 1644 1645 static bool perf_event__is_extra_kernel_mmap(struct machine *machine, 1646 struct extra_kernel_map *xm) 1647 { 1648 return machine__is(machine, EM_X86_64) && 1649 is_entry_trampoline(xm->name); 1650 } 1651 1652 static int machine__process_extra_kernel_map(struct machine *machine, 1653 struct extra_kernel_map *xm) 1654 { 1655 struct dso *kernel = machine__kernel_dso(machine); 1656 1657 if (kernel == NULL) 1658 return -1; 1659 1660 return machine__create_extra_kernel_map(machine, kernel, xm); 1661 } 1662 1663 static int machine__process_kernel_mmap_event(struct machine *machine, 1664 struct extra_kernel_map *xm, 1665 struct build_id *bid) 1666 { 1667 enum dso_space_type dso_space; 1668 bool is_kernel_mmap; 1669 const char *mmap_name = machine->mmap_name; 1670 1671 /* If we have maps from kcore then we do not need or want any others */ 1672 if (machine__uses_kcore(machine)) 1673 return 0; 1674 1675 if (machine__is_host(machine)) 1676 dso_space = DSO_SPACE__KERNEL; 1677 else 1678 dso_space = DSO_SPACE__KERNEL_GUEST; 1679 1680 is_kernel_mmap = memcmp(xm->name, mmap_name, strlen(mmap_name) - 1) == 0; 1681 if (!is_kernel_mmap && !machine__is_host(machine)) { 1682 /* 1683 * If the event was recorded inside the guest and injected into 1684 * the host perf.data file, then it will match a host mmap_name, 1685 * so try that - see machine__set_mmap_name(). 1686 */ 1687 mmap_name = "[kernel.kallsyms]"; 1688 is_kernel_mmap = memcmp(xm->name, mmap_name, strlen(mmap_name) - 1) == 0; 1689 } 1690 if (xm->name[0] == '/' || 1691 (!is_kernel_mmap && xm->name[0] == '[')) { 1692 struct map *map = machine__addnew_module_map(machine, xm->start, xm->name); 1693 1694 if (map == NULL) 1695 goto out_problem; 1696 1697 map__set_end(map, map__start(map) + xm->end - xm->start); 1698 1699 if (build_id__is_defined(bid)) 1700 dso__set_build_id(map__dso(map), bid); 1701 1702 map__put(map); 1703 } else if (is_kernel_mmap) { 1704 const char *symbol_name = xm->name + strlen(mmap_name); 1705 /* 1706 * Should be there already, from the build-id table in 1707 * the header. 1708 */ 1709 struct dso *kernel = dsos__find_kernel_dso(&machine->dsos); 1710 1711 if (kernel == NULL) 1712 kernel = machine__findnew_dso(machine, machine->mmap_name); 1713 if (kernel == NULL) 1714 goto out_problem; 1715 1716 dso__set_kernel(kernel, dso_space); 1717 if (__machine__create_kernel_maps(machine, kernel) < 0) { 1718 dso__put(kernel); 1719 goto out_problem; 1720 } 1721 1722 if (strstr(dso__long_name(kernel), "vmlinux")) 1723 dso__set_short_name(kernel, "[kernel.vmlinux]", false); 1724 1725 if (machine__update_kernel_mmap(machine, xm->start, xm->end) < 0) { 1726 dso__put(kernel); 1727 goto out_problem; 1728 } 1729 1730 if (build_id__is_defined(bid)) 1731 dso__set_build_id(kernel, bid); 1732 1733 /* 1734 * Avoid using a zero address (kptr_restrict) for the ref reloc 1735 * symbol. Effectively having zero here means that at record 1736 * time /proc/sys/kernel/kptr_restrict was non zero. 1737 */ 1738 if (xm->pgoff != 0) { 1739 map__set_kallsyms_ref_reloc_sym(machine->vmlinux_map, 1740 symbol_name, 1741 xm->pgoff); 1742 } 1743 1744 if (machine__is_default_guest(machine)) { 1745 /* 1746 * preload dso of guest kernel and modules 1747 */ 1748 dso__load(kernel, machine__kernel_map(machine)); 1749 } 1750 dso__put(kernel); 1751 } else if (perf_event__is_extra_kernel_mmap(machine, xm)) { 1752 return machine__process_extra_kernel_map(machine, xm); 1753 } 1754 return 0; 1755 out_problem: 1756 return -1; 1757 } 1758 1759 int machine__process_mmap2_event(struct machine *machine, 1760 union perf_event *event, 1761 struct perf_sample *sample) 1762 { 1763 struct thread *thread; 1764 struct map *map; 1765 struct dso_id dso_id = dso_id_empty; 1766 int ret = 0; 1767 1768 if (dump_trace) 1769 perf_event__fprintf_mmap2(event, stdout); 1770 1771 if (event->header.misc & PERF_RECORD_MISC_MMAP_BUILD_ID) { 1772 build_id__init(&dso_id.build_id, event->mmap2.build_id, event->mmap2.build_id_size); 1773 } else { 1774 dso_id.maj = event->mmap2.maj; 1775 dso_id.min = event->mmap2.min; 1776 dso_id.ino = event->mmap2.ino; 1777 dso_id.ino_generation = event->mmap2.ino_generation; 1778 dso_id.mmap2_valid = true; 1779 dso_id.mmap2_ino_generation_valid = true; 1780 } 1781 1782 if (sample->cpumode == PERF_RECORD_MISC_GUEST_KERNEL || 1783 sample->cpumode == PERF_RECORD_MISC_KERNEL) { 1784 struct extra_kernel_map xm = { 1785 .start = event->mmap2.start, 1786 .end = event->mmap2.start + event->mmap2.len, 1787 .pgoff = event->mmap2.pgoff, 1788 }; 1789 1790 strlcpy(xm.name, event->mmap2.filename, KMAP_NAME_LEN); 1791 ret = machine__process_kernel_mmap_event(machine, &xm, &dso_id.build_id); 1792 if (ret < 0) 1793 goto out_problem; 1794 return 0; 1795 } 1796 1797 thread = machine__findnew_thread(machine, event->mmap2.pid, 1798 event->mmap2.tid); 1799 if (thread == NULL) 1800 goto out_problem; 1801 1802 map = map__new(machine, event->mmap2.start, 1803 event->mmap2.len, event->mmap2.pgoff, 1804 &dso_id, event->mmap2.prot, 1805 event->mmap2.flags, 1806 event->mmap2.filename, thread); 1807 1808 if (map == NULL) 1809 goto out_problem_map; 1810 1811 ret = thread__insert_map(thread, map); 1812 if (ret) 1813 goto out_problem_insert; 1814 1815 thread__put(thread); 1816 map__put(map); 1817 return 0; 1818 1819 out_problem_insert: 1820 map__put(map); 1821 out_problem_map: 1822 thread__put(thread); 1823 out_problem: 1824 dump_printf("problem processing PERF_RECORD_MMAP2, skipping event.\n"); 1825 return 0; 1826 } 1827 1828 int machine__process_mmap_event(struct machine *machine, union perf_event *event, 1829 struct perf_sample *sample) 1830 { 1831 struct thread *thread; 1832 struct map *map; 1833 u32 prot = 0; 1834 int ret = 0; 1835 1836 if (dump_trace) 1837 perf_event__fprintf_mmap(event, stdout); 1838 1839 if (sample->cpumode == PERF_RECORD_MISC_GUEST_KERNEL || 1840 sample->cpumode == PERF_RECORD_MISC_KERNEL) { 1841 struct extra_kernel_map xm = { 1842 .start = event->mmap.start, 1843 .end = event->mmap.start + event->mmap.len, 1844 .pgoff = event->mmap.pgoff, 1845 }; 1846 1847 strlcpy(xm.name, event->mmap.filename, KMAP_NAME_LEN); 1848 ret = machine__process_kernel_mmap_event(machine, &xm, NULL); 1849 if (ret < 0) 1850 goto out_problem; 1851 return 0; 1852 } 1853 1854 thread = machine__findnew_thread(machine, event->mmap.pid, 1855 event->mmap.tid); 1856 if (thread == NULL) 1857 goto out_problem; 1858 1859 if (!(event->header.misc & PERF_RECORD_MISC_MMAP_DATA)) 1860 prot = PROT_EXEC; 1861 1862 map = map__new(machine, event->mmap.start, 1863 event->mmap.len, event->mmap.pgoff, 1864 &dso_id_empty, prot, /*flags=*/0, event->mmap.filename, thread); 1865 1866 if (map == NULL) 1867 goto out_problem_map; 1868 1869 ret = thread__insert_map(thread, map); 1870 if (ret) 1871 goto out_problem_insert; 1872 1873 thread__put(thread); 1874 map__put(map); 1875 return 0; 1876 1877 out_problem_insert: 1878 map__put(map); 1879 out_problem_map: 1880 thread__put(thread); 1881 out_problem: 1882 dump_printf("problem processing PERF_RECORD_MMAP, skipping event.\n"); 1883 return 0; 1884 } 1885 1886 void machine__remove_thread(struct machine *machine, struct thread *th) 1887 { 1888 return threads__remove(&machine->threads, th); 1889 } 1890 1891 int machine__process_fork_event(struct machine *machine, union perf_event *event, 1892 struct perf_sample *sample) 1893 { 1894 struct thread *thread = machine__find_thread(machine, 1895 event->fork.pid, 1896 event->fork.tid); 1897 struct thread *parent = machine__findnew_thread(machine, 1898 event->fork.ppid, 1899 event->fork.ptid); 1900 bool do_maps_clone = true; 1901 int err = 0; 1902 1903 if (dump_trace) 1904 perf_event__fprintf_task(event, stdout); 1905 1906 /* 1907 * There may be an existing thread that is not actually the parent, 1908 * either because we are processing events out of order, or because the 1909 * (fork) event that would have removed the thread was lost. Assume the 1910 * latter case and continue on as best we can. 1911 */ 1912 if (thread__pid(parent) != (pid_t)event->fork.ppid) { 1913 dump_printf("removing erroneous parent thread %d/%d\n", 1914 thread__pid(parent), thread__tid(parent)); 1915 machine__remove_thread(machine, parent); 1916 thread__put(parent); 1917 parent = machine__findnew_thread(machine, event->fork.ppid, 1918 event->fork.ptid); 1919 } 1920 1921 /* if a thread currently exists for the thread id remove it */ 1922 if (thread != NULL) { 1923 machine__remove_thread(machine, thread); 1924 thread__put(thread); 1925 } 1926 1927 thread = machine__findnew_thread(machine, event->fork.pid, 1928 event->fork.tid); 1929 /* 1930 * When synthesizing FORK events, we are trying to create thread 1931 * objects for the already running tasks on the machine. 1932 * 1933 * Normally, for a kernel FORK event, we want to clone the parent's 1934 * maps because that is what the kernel just did. 1935 * 1936 * But when synthesizing, this should not be done. If we do, we end up 1937 * with overlapping maps as we process the synthesized MMAP2 events that 1938 * get delivered shortly thereafter. 1939 * 1940 * Use the FORK event misc flags in an internal way to signal this 1941 * situation, so we can elide the map clone when appropriate. 1942 */ 1943 if (event->fork.header.misc & PERF_RECORD_MISC_FORK_EXEC) 1944 do_maps_clone = false; 1945 1946 if (thread == NULL || parent == NULL || 1947 thread__fork(thread, parent, sample->time, do_maps_clone) < 0) { 1948 dump_printf("problem processing PERF_RECORD_FORK, skipping event.\n"); 1949 err = -1; 1950 } 1951 thread__put(thread); 1952 thread__put(parent); 1953 1954 return err; 1955 } 1956 1957 int machine__process_exit_event(struct machine *machine, union perf_event *event, 1958 struct perf_sample *sample __maybe_unused) 1959 { 1960 struct thread *thread = machine__find_thread(machine, 1961 event->fork.pid, 1962 event->fork.tid); 1963 1964 if (dump_trace) 1965 perf_event__fprintf_task(event, stdout); 1966 1967 /* There is no context switch out before exit, so we decrement here. */ 1968 machine->parallelism--; 1969 if (thread != NULL) { 1970 if (symbol_conf.keep_exited_threads) 1971 thread__set_exited(thread, /*exited=*/true); 1972 else 1973 machine__remove_thread(machine, thread); 1974 } 1975 thread__put(thread); 1976 return 0; 1977 } 1978 1979 int machine__process_event(struct machine *machine, union perf_event *event, 1980 struct perf_sample *sample) 1981 { 1982 int ret; 1983 1984 switch (event->header.type) { 1985 case PERF_RECORD_COMM: 1986 ret = machine__process_comm_event(machine, event, sample); break; 1987 case PERF_RECORD_MMAP: 1988 ret = machine__process_mmap_event(machine, event, sample); break; 1989 case PERF_RECORD_NAMESPACES: 1990 ret = machine__process_namespaces_event(machine, event, sample); break; 1991 case PERF_RECORD_CGROUP: 1992 ret = machine__process_cgroup_event(machine, event, sample); break; 1993 case PERF_RECORD_MMAP2: 1994 ret = machine__process_mmap2_event(machine, event, sample); break; 1995 case PERF_RECORD_FORK: 1996 ret = machine__process_fork_event(machine, event, sample); break; 1997 case PERF_RECORD_EXIT: 1998 ret = machine__process_exit_event(machine, event, sample); break; 1999 case PERF_RECORD_LOST: 2000 ret = machine__process_lost_event(machine, event, sample); break; 2001 case PERF_RECORD_AUX: 2002 ret = machine__process_aux_event(machine, event); break; 2003 case PERF_RECORD_ITRACE_START: 2004 ret = machine__process_itrace_start_event(machine, event); break; 2005 case PERF_RECORD_LOST_SAMPLES: 2006 ret = machine__process_lost_samples_event(machine, event, sample); break; 2007 case PERF_RECORD_SWITCH: 2008 case PERF_RECORD_SWITCH_CPU_WIDE: 2009 ret = machine__process_switch_event(machine, event); break; 2010 case PERF_RECORD_KSYMBOL: 2011 ret = machine__process_ksymbol(machine, event, sample); break; 2012 case PERF_RECORD_BPF_EVENT: 2013 ret = machine__process_bpf(machine, event, sample); break; 2014 case PERF_RECORD_TEXT_POKE: 2015 ret = machine__process_text_poke(machine, event, sample); break; 2016 case PERF_RECORD_AUX_OUTPUT_HW_ID: 2017 ret = machine__process_aux_output_hw_id_event(machine, event); break; 2018 default: 2019 ret = -1; 2020 break; 2021 } 2022 2023 return ret; 2024 } 2025 2026 static bool symbol__match_regex(struct symbol *sym, regex_t *regex) 2027 { 2028 return regexec(regex, sym->name, 0, NULL, 0) == 0; 2029 } 2030 2031 static void ip__resolve_ams(struct thread *thread, 2032 struct addr_map_symbol *ams, 2033 u64 ip) 2034 { 2035 struct addr_location al; 2036 2037 addr_location__init(&al); 2038 /* 2039 * We cannot use the header.misc hint to determine whether a 2040 * branch stack address is user, kernel, guest, hypervisor. 2041 * Branches may straddle the kernel/user/hypervisor boundaries. 2042 * Thus, we have to try consecutively until we find a match 2043 * or else, the symbol is unknown 2044 */ 2045 thread__find_cpumode_addr_location(thread, ip, /*symbols=*/true, &al); 2046 2047 ams->addr = ip; 2048 ams->al_addr = al.addr; 2049 ams->al_level = al.level; 2050 ams->ms.thread = thread__get(al.thread); 2051 ams->ms.sym = al.sym; 2052 ams->ms.map = map__get(al.map); 2053 ams->phys_addr = 0; 2054 ams->data_page_size = 0; 2055 addr_location__exit(&al); 2056 } 2057 2058 static void ip__resolve_data(struct thread *thread, 2059 u8 m, struct addr_map_symbol *ams, 2060 u64 addr, u64 phys_addr, u64 daddr_page_size) 2061 { 2062 struct addr_location al; 2063 2064 addr_location__init(&al); 2065 2066 thread__find_symbol(thread, m, addr, &al); 2067 2068 ams->addr = addr; 2069 ams->al_addr = al.addr; 2070 ams->al_level = al.level; 2071 ams->ms.thread = thread__get(al.thread); 2072 ams->ms.sym = al.sym; 2073 ams->ms.map = map__get(al.map); 2074 ams->phys_addr = phys_addr; 2075 ams->data_page_size = daddr_page_size; 2076 addr_location__exit(&al); 2077 } 2078 2079 struct mem_info *sample__resolve_mem(struct perf_sample *sample, 2080 struct addr_location *al) 2081 { 2082 struct mem_info *mi = mem_info__new(); 2083 2084 if (!mi) 2085 return NULL; 2086 2087 ip__resolve_ams(al->thread, mem_info__iaddr(mi), sample->ip); 2088 ip__resolve_data(al->thread, al->cpumode, mem_info__daddr(mi), 2089 sample->addr, sample->phys_addr, 2090 sample->data_page_size); 2091 mem_info__data_src(mi)->val = sample->data_src; 2092 2093 return mi; 2094 } 2095 2096 static char *callchain_srcline(struct map_symbol *ms, u64 ip) 2097 { 2098 struct map *map = ms->map; 2099 char *srcline = NULL; 2100 struct dso *dso; 2101 2102 if (!map || callchain_param.key == CCKEY_FUNCTION) 2103 return srcline; 2104 2105 dso = map__dso(map); 2106 srcline = srcline__tree_find(dso__srclines(dso), ip); 2107 if (!srcline) { 2108 bool show_sym = false; 2109 bool show_addr = callchain_param.key == CCKEY_ADDRESS; 2110 2111 srcline = get_srcline(dso, map__rip_2objdump(map, ip), 2112 ms->sym, show_sym, show_addr, ip); 2113 srcline__tree_insert(dso__srclines(dso), ip, srcline); 2114 } 2115 2116 return srcline; 2117 } 2118 2119 struct iterations { 2120 int nr_loop_iter; 2121 u64 cycles; 2122 }; 2123 2124 static int append_inlines(struct callchain_cursor *cursor, struct map_symbol *ms, u64 ip, 2125 bool branch, struct branch_flags *flags, int nr_loop_iter, 2126 u64 iter_cycles, u64 branch_from) 2127 { 2128 struct symbol *sym = ms->sym; 2129 struct map *map = ms->map; 2130 struct inline_node *inline_node; 2131 struct inline_list *ilist; 2132 struct dso *dso; 2133 u64 addr; 2134 int ret = 1; 2135 struct map_symbol ilist_ms; 2136 bool first = true; 2137 2138 if (!symbol_conf.inline_name || !map || !sym) 2139 return ret; 2140 2141 addr = map__dso_map_ip(map, ip); 2142 addr = map__rip_2objdump(map, addr); 2143 dso = map__dso(map); 2144 2145 inline_node = inlines__tree_find(dso__inlined_nodes(dso), addr); 2146 if (!inline_node) { 2147 inline_node = dso__parse_addr_inlines(dso, addr, sym); 2148 if (!inline_node) 2149 return ret; 2150 inlines__tree_insert(dso__inlined_nodes(dso), inline_node); 2151 } 2152 2153 ilist_ms = (struct map_symbol) { 2154 .thread = thread__get(ms->thread), 2155 .map = map__get(map), 2156 }; 2157 list_for_each_entry(ilist, &inline_node->val, list) { 2158 ilist_ms.sym = ilist->symbol; 2159 if (first) { 2160 ret = callchain_cursor_append(cursor, ip, &ilist_ms, 2161 branch, flags, nr_loop_iter, 2162 iter_cycles, branch_from, ilist->srcline); 2163 } else { 2164 ret = callchain_cursor_append(cursor, ip, &ilist_ms, false, 2165 NULL, 0, 0, 0, ilist->srcline); 2166 } 2167 first = false; 2168 2169 if (ret != 0) 2170 return ret; 2171 } 2172 map_symbol__exit(&ilist_ms); 2173 2174 return ret; 2175 } 2176 2177 static int add_callchain_ip(struct thread *thread, 2178 struct callchain_cursor *cursor, 2179 struct symbol **parent, 2180 struct addr_location *root_al, 2181 u8 *cpumode, 2182 u64 ip, 2183 bool branch, 2184 struct branch_flags *flags, 2185 struct iterations *iter, 2186 u64 branch_from, 2187 bool symbols) 2188 { 2189 struct map_symbol ms = {}; 2190 struct addr_location al; 2191 int nr_loop_iter = 0, err = 0; 2192 u64 iter_cycles = 0; 2193 const char *srcline = NULL; 2194 2195 addr_location__init(&al); 2196 al.filtered = 0; 2197 al.sym = NULL; 2198 al.srcline = NULL; 2199 if (!cpumode) { 2200 thread__find_cpumode_addr_location(thread, ip, symbols, &al); 2201 } else { 2202 if (ip >= PERF_CONTEXT_MAX) { 2203 switch (ip) { 2204 case PERF_CONTEXT_HV: 2205 *cpumode = PERF_RECORD_MISC_HYPERVISOR; 2206 break; 2207 case PERF_CONTEXT_KERNEL: 2208 *cpumode = PERF_RECORD_MISC_KERNEL; 2209 break; 2210 case PERF_CONTEXT_USER: 2211 case PERF_CONTEXT_USER_DEFERRED: 2212 *cpumode = PERF_RECORD_MISC_USER; 2213 break; 2214 default: 2215 pr_debug("invalid callchain context: " 2216 "%"PRId64"\n", (s64) ip); 2217 /* 2218 * It seems the callchain is corrupted. 2219 * Discard all. 2220 */ 2221 callchain_cursor_reset(cursor); 2222 err = 1; 2223 goto out; 2224 } 2225 goto out; 2226 } 2227 if (symbols) 2228 thread__find_symbol(thread, *cpumode, ip, &al); 2229 else 2230 thread__find_map(thread, *cpumode, ip, &al); 2231 } 2232 2233 if (al.sym != NULL) { 2234 if (perf_hpp_list.parent && !*parent && 2235 symbol__match_regex(al.sym, &parent_regex)) 2236 *parent = al.sym; 2237 else if (have_ignore_callees && root_al && 2238 symbol__match_regex(al.sym, &ignore_callees_regex)) { 2239 /* Treat this symbol as the root, 2240 forgetting its callees. */ 2241 addr_location__copy(root_al, &al); 2242 callchain_cursor_reset(cursor); 2243 } 2244 } 2245 2246 if (symbol_conf.hide_unresolved && al.sym == NULL) 2247 goto out; 2248 2249 if (iter) { 2250 nr_loop_iter = iter->nr_loop_iter; 2251 iter_cycles = iter->cycles; 2252 } 2253 2254 ms.thread = thread__get(al.thread); 2255 ms.map = map__get(al.map); 2256 ms.sym = al.sym; 2257 2258 if (append_inlines(cursor, &ms, ip, branch, flags, nr_loop_iter, 2259 iter_cycles, branch_from) == 0) 2260 goto out; 2261 2262 srcline = callchain_srcline(&ms, al.addr); 2263 err = callchain_cursor_append(cursor, ip, &ms, 2264 branch, flags, nr_loop_iter, 2265 iter_cycles, branch_from, srcline); 2266 out: 2267 addr_location__exit(&al); 2268 map_symbol__exit(&ms); 2269 return err; 2270 } 2271 2272 struct branch_info *sample__resolve_bstack(struct perf_sample *sample, 2273 struct addr_location *al) 2274 { 2275 unsigned int i; 2276 const struct branch_stack *bs = sample->branch_stack; 2277 struct branch_entry *entries = perf_sample__branch_entries(sample); 2278 u64 *branch_stack_cntr = sample->branch_stack_cntr; 2279 struct branch_info *bi = calloc(bs->nr, sizeof(struct branch_info)); 2280 2281 if (!bi) 2282 return NULL; 2283 2284 for (i = 0; i < bs->nr; i++) { 2285 ip__resolve_ams(al->thread, &bi[i].to, entries[i].to); 2286 ip__resolve_ams(al->thread, &bi[i].from, entries[i].from); 2287 bi[i].flags = entries[i].flags; 2288 if (branch_stack_cntr) 2289 bi[i].branch_stack_cntr = branch_stack_cntr[i]; 2290 } 2291 return bi; 2292 } 2293 2294 static void save_iterations(struct iterations *iter, 2295 struct branch_entry *be, int nr) 2296 { 2297 int i; 2298 2299 iter->nr_loop_iter++; 2300 iter->cycles = 0; 2301 2302 for (i = 0; i < nr; i++) 2303 iter->cycles += be[i].flags.cycles; 2304 } 2305 2306 #define CHASHSZ 127 2307 #define CHASHBITS 7 2308 #define NO_ENTRY 0xff 2309 2310 #define PERF_MAX_BRANCH_DEPTH 127 2311 2312 /* Remove loops. */ 2313 static int remove_loops(struct branch_entry *l, int nr, 2314 struct iterations *iter) 2315 { 2316 int i, j, off; 2317 unsigned char chash[CHASHSZ]; 2318 2319 memset(chash, NO_ENTRY, sizeof(chash)); 2320 2321 BUG_ON(PERF_MAX_BRANCH_DEPTH > 255); 2322 2323 for (i = 0; i < nr; i++) { 2324 int h = hash_64(l[i].from, CHASHBITS) % CHASHSZ; 2325 2326 /* no collision handling for now */ 2327 if (chash[h] == NO_ENTRY) { 2328 chash[h] = i; 2329 } else if (l[chash[h]].from == l[i].from) { 2330 bool is_loop = true; 2331 /* check if it is a real loop */ 2332 off = 0; 2333 for (j = chash[h]; j < i && i + off < nr; j++, off++) 2334 if (l[j].from != l[i + off].from) { 2335 is_loop = false; 2336 break; 2337 } 2338 if (is_loop) { 2339 j = nr - (i + off); 2340 if (j > 0) { 2341 save_iterations(iter + i + off, 2342 l + i, off); 2343 2344 memmove(iter + i, iter + i + off, 2345 j * sizeof(*iter)); 2346 2347 memmove(l + i, l + i + off, 2348 j * sizeof(*l)); 2349 } 2350 2351 nr -= off; 2352 } 2353 } 2354 } 2355 return nr; 2356 } 2357 2358 static int lbr_callchain_add_kernel_ip(struct thread *thread, 2359 struct callchain_cursor *cursor, 2360 struct perf_sample *sample, 2361 struct symbol **parent, 2362 struct addr_location *root_al, 2363 u64 branch_from, 2364 bool callee, int end, 2365 bool symbols) 2366 { 2367 struct ip_callchain *chain = sample->callchain; 2368 u8 cpumode = PERF_RECORD_MISC_USER; 2369 int err, i; 2370 2371 if (callee) { 2372 for (i = 0; i < end + 1; i++) { 2373 err = add_callchain_ip(thread, cursor, parent, 2374 root_al, &cpumode, chain->ips[i], 2375 false, NULL, NULL, branch_from, 2376 symbols); 2377 if (err) 2378 return err; 2379 } 2380 return 0; 2381 } 2382 2383 for (i = end; i >= 0; i--) { 2384 err = add_callchain_ip(thread, cursor, parent, 2385 root_al, &cpumode, chain->ips[i], 2386 false, NULL, NULL, branch_from, 2387 symbols); 2388 if (err) 2389 return err; 2390 } 2391 2392 return 0; 2393 } 2394 2395 static void save_lbr_cursor_node(struct thread *thread, 2396 struct callchain_cursor *cursor, 2397 int idx) 2398 { 2399 struct lbr_stitch *lbr_stitch = thread__lbr_stitch(thread); 2400 2401 if (!lbr_stitch) 2402 return; 2403 2404 if (cursor->pos == cursor->nr) { 2405 lbr_stitch->prev_lbr_cursor[idx].valid = false; 2406 return; 2407 } 2408 2409 if (!cursor->curr) 2410 cursor->curr = cursor->first; 2411 else 2412 cursor->curr = cursor->curr->next; 2413 2414 map_symbol__exit(&lbr_stitch->prev_lbr_cursor[idx].ms); 2415 memcpy(&lbr_stitch->prev_lbr_cursor[idx], cursor->curr, 2416 sizeof(struct callchain_cursor_node)); 2417 lbr_stitch->prev_lbr_cursor[idx].ms.thread = thread__get(cursor->curr->ms.thread); 2418 lbr_stitch->prev_lbr_cursor[idx].ms.map = map__get(cursor->curr->ms.map); 2419 2420 lbr_stitch->prev_lbr_cursor[idx].valid = true; 2421 cursor->pos++; 2422 } 2423 2424 static int lbr_callchain_add_lbr_ip(struct thread *thread, 2425 struct callchain_cursor *cursor, 2426 struct perf_sample *sample, 2427 struct symbol **parent, 2428 struct addr_location *root_al, 2429 u64 *branch_from, 2430 bool callee, 2431 bool symbols) 2432 { 2433 struct branch_stack *lbr_stack = sample->branch_stack; 2434 struct branch_entry *entries = perf_sample__branch_entries(sample); 2435 u8 cpumode = PERF_RECORD_MISC_USER; 2436 int lbr_nr = lbr_stack->nr; 2437 struct branch_flags *flags; 2438 int err, i; 2439 u64 ip; 2440 2441 /* 2442 * The curr and pos are not used in writing session. They are cleared 2443 * in callchain_cursor_commit() when the writing session is closed. 2444 * Using curr and pos to track the current cursor node. 2445 */ 2446 if (thread__lbr_stitch(thread)) { 2447 cursor->curr = NULL; 2448 cursor->pos = cursor->nr; 2449 if (cursor->nr) { 2450 cursor->curr = cursor->first; 2451 for (i = 0; i < (int)(cursor->nr - 1); i++) 2452 cursor->curr = cursor->curr->next; 2453 } 2454 } 2455 2456 if (callee) { 2457 /* 2458 * Set the (first) leaf function's IP to sample->ip (the 2459 * location of the sample) but if not recorded use entries.to 2460 */ 2461 if (sample->ip) 2462 ip = sample->ip; 2463 else 2464 ip = entries[0].to; 2465 flags = &entries[0].flags; 2466 *branch_from = entries[0].from; 2467 err = add_callchain_ip(thread, cursor, parent, 2468 root_al, &cpumode, ip, 2469 true, flags, NULL, 2470 *branch_from, symbols); 2471 if (err) 2472 return err; 2473 2474 /* 2475 * The number of cursor node increases. 2476 * Move the current cursor node. 2477 * But does not need to save current cursor node for entry 0. 2478 * It's impossible to stitch the whole LBRs of previous sample. 2479 */ 2480 if (thread__lbr_stitch(thread) && (cursor->pos != cursor->nr)) { 2481 if (!cursor->curr) 2482 cursor->curr = cursor->first; 2483 else 2484 cursor->curr = cursor->curr->next; 2485 cursor->pos++; 2486 } 2487 2488 /* Add LBR ip from entries.from one by one. */ 2489 for (i = 0; i < lbr_nr; i++) { 2490 ip = entries[i].from; 2491 flags = &entries[i].flags; 2492 err = add_callchain_ip(thread, cursor, parent, 2493 root_al, &cpumode, ip, 2494 true, flags, NULL, 2495 *branch_from, symbols); 2496 if (err) 2497 return err; 2498 save_lbr_cursor_node(thread, cursor, i); 2499 } 2500 return 0; 2501 } 2502 2503 /* Add LBR ip from entries.from one by one. */ 2504 for (i = lbr_nr - 1; i >= 0; i--) { 2505 ip = entries[i].from; 2506 flags = &entries[i].flags; 2507 err = add_callchain_ip(thread, cursor, parent, 2508 root_al, &cpumode, ip, 2509 true, flags, NULL, 2510 *branch_from, symbols); 2511 if (err) 2512 return err; 2513 save_lbr_cursor_node(thread, cursor, i); 2514 } 2515 2516 if (lbr_nr > 0) { 2517 /* 2518 * Set the (first) leaf function's IP to sample->ip (the 2519 * location of the sample) but if not recorded use entries.to 2520 */ 2521 if (sample->ip) 2522 ip = sample->ip; 2523 else 2524 ip = entries[0].to; 2525 flags = &entries[0].flags; 2526 *branch_from = entries[0].from; 2527 err = add_callchain_ip(thread, cursor, parent, 2528 root_al, &cpumode, ip, 2529 true, flags, NULL, 2530 *branch_from, symbols); 2531 if (err) 2532 return err; 2533 } 2534 2535 return 0; 2536 } 2537 2538 static int lbr_callchain_add_stitched_lbr_ip(struct thread *thread, 2539 struct callchain_cursor *cursor) 2540 { 2541 struct lbr_stitch *lbr_stitch = thread__lbr_stitch(thread); 2542 struct callchain_cursor_node *cnode; 2543 struct stitch_list *stitch_node; 2544 int err; 2545 2546 list_for_each_entry(stitch_node, &lbr_stitch->lists, node) { 2547 cnode = &stitch_node->cursor; 2548 2549 err = callchain_cursor_append(cursor, cnode->ip, 2550 &cnode->ms, 2551 cnode->branch, 2552 &cnode->branch_flags, 2553 cnode->nr_loop_iter, 2554 cnode->iter_cycles, 2555 cnode->branch_from, 2556 cnode->srcline); 2557 if (err) 2558 return err; 2559 } 2560 return 0; 2561 } 2562 2563 static struct stitch_list *get_stitch_node(struct thread *thread) 2564 { 2565 struct lbr_stitch *lbr_stitch = thread__lbr_stitch(thread); 2566 struct stitch_list *stitch_node; 2567 2568 if (!list_empty(&lbr_stitch->free_lists)) { 2569 stitch_node = list_first_entry(&lbr_stitch->free_lists, 2570 struct stitch_list, node); 2571 list_del(&stitch_node->node); 2572 2573 return stitch_node; 2574 } 2575 2576 return malloc(sizeof(struct stitch_list)); 2577 } 2578 2579 static bool has_stitched_lbr(struct thread *thread, 2580 struct perf_sample *cur, 2581 struct perf_sample *prev, 2582 unsigned int max_lbr, 2583 bool callee) 2584 { 2585 struct branch_stack *cur_stack = cur->branch_stack; 2586 struct branch_entry *cur_entries = perf_sample__branch_entries(cur); 2587 struct branch_stack *prev_stack = prev->branch_stack; 2588 struct branch_entry *prev_entries = perf_sample__branch_entries(prev); 2589 struct lbr_stitch *lbr_stitch = thread__lbr_stitch(thread); 2590 int i, j, nr_identical_branches = 0; 2591 struct stitch_list *stitch_node; 2592 u64 cur_base, distance; 2593 2594 if (!cur_stack || !prev_stack) 2595 return false; 2596 2597 /* Find the physical index of the base-of-stack for current sample. */ 2598 cur_base = max_lbr - cur_stack->nr + cur_stack->hw_idx + 1; 2599 2600 distance = (prev_stack->hw_idx > cur_base) ? (prev_stack->hw_idx - cur_base) : 2601 (max_lbr + prev_stack->hw_idx - cur_base); 2602 /* Previous sample has shorter stack. Nothing can be stitched. */ 2603 if (distance + 1 > prev_stack->nr) 2604 return false; 2605 2606 /* 2607 * Check if there are identical LBRs between two samples. 2608 * Identical LBRs must have same from, to and flags values. Also, 2609 * they have to be saved in the same LBR registers (same physical 2610 * index). 2611 * 2612 * Starts from the base-of-stack of current sample. 2613 */ 2614 for (i = distance, j = cur_stack->nr - 1; (i >= 0) && (j >= 0); i--, j--) { 2615 if ((prev_entries[i].from != cur_entries[j].from) || 2616 (prev_entries[i].to != cur_entries[j].to) || 2617 (prev_entries[i].flags.value != cur_entries[j].flags.value)) 2618 break; 2619 nr_identical_branches++; 2620 } 2621 2622 if (!nr_identical_branches) 2623 return false; 2624 2625 /* 2626 * Save the LBRs between the base-of-stack of previous sample 2627 * and the base-of-stack of current sample into lbr_stitch->lists. 2628 * These LBRs will be stitched later. 2629 */ 2630 for (i = prev_stack->nr - 1; i > (int)distance; i--) { 2631 2632 if (!lbr_stitch->prev_lbr_cursor[i].valid) 2633 continue; 2634 2635 stitch_node = get_stitch_node(thread); 2636 if (!stitch_node) 2637 return false; 2638 2639 memcpy(&stitch_node->cursor, &lbr_stitch->prev_lbr_cursor[i], 2640 sizeof(struct callchain_cursor_node)); 2641 2642 stitch_node->cursor.ms.thread = 2643 thread__get(lbr_stitch->prev_lbr_cursor[i].ms.thread); 2644 stitch_node->cursor.ms.map = map__get(lbr_stitch->prev_lbr_cursor[i].ms.map); 2645 2646 if (callee) 2647 list_add(&stitch_node->node, &lbr_stitch->lists); 2648 else 2649 list_add_tail(&stitch_node->node, &lbr_stitch->lists); 2650 } 2651 2652 return true; 2653 } 2654 2655 static bool alloc_lbr_stitch(struct thread *thread, unsigned int max_lbr) 2656 { 2657 if (thread__lbr_stitch(thread)) 2658 return true; 2659 2660 thread__set_lbr_stitch(thread, zalloc(sizeof(struct lbr_stitch))); 2661 if (!thread__lbr_stitch(thread)) 2662 goto err; 2663 2664 thread__lbr_stitch(thread)->prev_lbr_cursor = 2665 calloc(max_lbr + 1, sizeof(struct callchain_cursor_node)); 2666 if (!thread__lbr_stitch(thread)->prev_lbr_cursor) 2667 goto free_lbr_stitch; 2668 2669 thread__lbr_stitch(thread)->prev_lbr_cursor_size = max_lbr + 1; 2670 2671 INIT_LIST_HEAD(&thread__lbr_stitch(thread)->lists); 2672 INIT_LIST_HEAD(&thread__lbr_stitch(thread)->free_lists); 2673 2674 return true; 2675 2676 free_lbr_stitch: 2677 free(thread__lbr_stitch(thread)); 2678 thread__set_lbr_stitch(thread, NULL); 2679 err: 2680 pr_warning("Failed to allocate space for stitched LBRs. Disable LBR stitch\n"); 2681 thread__set_lbr_stitch_enable(thread, false); 2682 return false; 2683 } 2684 2685 /* 2686 * Resolve LBR callstack chain sample 2687 * Return: 2688 * 1 on success get LBR callchain information 2689 * 0 no available LBR callchain information, should try fp 2690 * negative error code on other errors. 2691 */ 2692 static int resolve_lbr_callchain_sample(struct thread *thread, 2693 struct callchain_cursor *cursor, 2694 struct perf_sample *sample, 2695 struct symbol **parent, 2696 struct addr_location *root_al, 2697 int max_stack, 2698 unsigned int max_lbr, 2699 bool symbols) 2700 { 2701 bool callee = (callchain_param.order == ORDER_CALLEE); 2702 struct ip_callchain *chain = sample->callchain; 2703 int chain_nr = min(max_stack, (int)chain->nr), i; 2704 struct lbr_stitch *lbr_stitch; 2705 bool stitched_lbr = false; 2706 u64 branch_from = 0; 2707 int err; 2708 2709 for (i = 0; i < chain_nr; i++) { 2710 if (chain->ips[i] == PERF_CONTEXT_USER) 2711 break; 2712 } 2713 2714 /* LBR only affects the user callchain */ 2715 if (i == chain_nr) 2716 return 0; 2717 2718 if (thread__lbr_stitch_enable(thread) && !sample->no_hw_idx && 2719 (max_lbr > 0) && alloc_lbr_stitch(thread, max_lbr)) { 2720 lbr_stitch = thread__lbr_stitch(thread); 2721 2722 stitched_lbr = has_stitched_lbr(thread, sample, 2723 &lbr_stitch->prev_sample, 2724 max_lbr, callee); 2725 2726 if (!stitched_lbr && !list_empty(&lbr_stitch->lists)) { 2727 struct stitch_list *stitch_node; 2728 2729 list_for_each_entry(stitch_node, &lbr_stitch->lists, node) 2730 map_symbol__exit(&stitch_node->cursor.ms); 2731 2732 list_splice_init(&lbr_stitch->lists, &lbr_stitch->free_lists); 2733 } 2734 memcpy(&lbr_stitch->prev_sample, sample, sizeof(*sample)); 2735 } 2736 2737 if (callee) { 2738 /* Add kernel ip */ 2739 err = lbr_callchain_add_kernel_ip(thread, cursor, sample, 2740 parent, root_al, branch_from, 2741 true, i, symbols); 2742 if (err) 2743 goto error; 2744 2745 err = lbr_callchain_add_lbr_ip(thread, cursor, sample, parent, 2746 root_al, &branch_from, true, symbols); 2747 if (err) 2748 goto error; 2749 2750 if (stitched_lbr) { 2751 err = lbr_callchain_add_stitched_lbr_ip(thread, cursor); 2752 if (err) 2753 goto error; 2754 } 2755 2756 } else { 2757 if (stitched_lbr) { 2758 err = lbr_callchain_add_stitched_lbr_ip(thread, cursor); 2759 if (err) 2760 goto error; 2761 } 2762 err = lbr_callchain_add_lbr_ip(thread, cursor, sample, parent, 2763 root_al, &branch_from, false, symbols); 2764 if (err) 2765 goto error; 2766 2767 /* Add kernel ip */ 2768 err = lbr_callchain_add_kernel_ip(thread, cursor, sample, 2769 parent, root_al, branch_from, 2770 false, i, symbols); 2771 if (err) 2772 goto error; 2773 } 2774 return 1; 2775 2776 error: 2777 return (err < 0) ? err : 0; 2778 } 2779 2780 static int find_prev_cpumode(struct ip_callchain *chain, struct thread *thread, 2781 struct callchain_cursor *cursor, 2782 struct symbol **parent, 2783 struct addr_location *root_al, 2784 u8 *cpumode, int ent, bool symbols) 2785 { 2786 int err = 0; 2787 2788 while (--ent >= 0) { 2789 u64 ip = chain->ips[ent]; 2790 2791 if (ip >= PERF_CONTEXT_MAX) { 2792 err = add_callchain_ip(thread, cursor, parent, 2793 root_al, cpumode, ip, 2794 false, NULL, NULL, 0, symbols); 2795 break; 2796 } 2797 } 2798 return err; 2799 } 2800 2801 static u64 get_leaf_frame_caller(struct perf_sample *sample, 2802 struct thread *thread, int usr_idx) 2803 { 2804 if (thread__e_machine(thread, /*machine=*/NULL, /*e_flags=*/NULL) == EM_AARCH64) 2805 return get_leaf_frame_caller_aarch64(sample, thread, usr_idx); 2806 else 2807 return 0; 2808 } 2809 2810 static int thread__resolve_callchain_sample(struct thread *thread, 2811 struct callchain_cursor *cursor, 2812 struct perf_sample *sample, 2813 struct symbol **parent, 2814 struct addr_location *root_al, 2815 int max_stack, 2816 bool symbols) 2817 { 2818 struct evsel *evsel = sample->evsel; 2819 struct branch_stack *branch = sample->branch_stack; 2820 struct branch_entry *entries = perf_sample__branch_entries(sample); 2821 struct ip_callchain *chain = sample->callchain; 2822 int chain_nr = 0; 2823 u8 cpumode = PERF_RECORD_MISC_USER; 2824 int i, j, err, nr_entries, usr_idx; 2825 int skip_idx = -1; 2826 int first_call = 0; 2827 u64 leaf_frame_caller; 2828 2829 if (chain) 2830 chain_nr = chain->nr; 2831 2832 if (evsel__has_branch_callstack(evsel)) { 2833 struct perf_env *env = evsel__env(evsel); 2834 2835 err = resolve_lbr_callchain_sample(thread, cursor, sample, parent, 2836 root_al, max_stack, 2837 !env ? 0 : env->max_branches, 2838 symbols); 2839 if (err) 2840 return (err < 0) ? err : 0; 2841 } 2842 2843 /* 2844 * Based on DWARF debug information, some architectures skip 2845 * a callchain entry saved by the kernel. 2846 */ 2847 skip_idx = arch_skip_callchain_idx(thread, chain); 2848 2849 /* 2850 * Add branches to call stack for easier browsing. This gives 2851 * more context for a sample than just the callers. 2852 * 2853 * This uses individual histograms of paths compared to the 2854 * aggregated histograms the normal LBR mode uses. 2855 * 2856 * Limitations for now: 2857 * - No extra filters 2858 * - No annotations (should annotate somehow) 2859 */ 2860 2861 if (branch && callchain_param.branch_callstack) { 2862 int nr = min(max_stack, (int)branch->nr); 2863 struct branch_entry be[nr]; 2864 struct iterations iter[nr]; 2865 2866 if (branch->nr > PERF_MAX_BRANCH_DEPTH) { 2867 pr_warning("corrupted branch chain. skipping...\n"); 2868 goto check_calls; 2869 } 2870 2871 for (i = 0; i < nr; i++) { 2872 if (callchain_param.order == ORDER_CALLEE) { 2873 be[i] = entries[i]; 2874 2875 if (chain == NULL) 2876 continue; 2877 2878 /* 2879 * Check for overlap into the callchain. 2880 * The return address is one off compared to 2881 * the branch entry. To adjust for this 2882 * assume the calling instruction is not longer 2883 * than 8 bytes. 2884 */ 2885 if (i == skip_idx || 2886 chain->ips[first_call] >= PERF_CONTEXT_MAX) 2887 first_call++; 2888 else if (be[i].from < chain->ips[first_call] && 2889 be[i].from >= chain->ips[first_call] - 8) 2890 first_call++; 2891 } else 2892 be[i] = entries[branch->nr - i - 1]; 2893 } 2894 2895 memset(iter, 0, sizeof(struct iterations) * nr); 2896 nr = remove_loops(be, nr, iter); 2897 2898 for (i = 0; i < nr; i++) { 2899 err = add_callchain_ip(thread, cursor, parent, 2900 root_al, 2901 NULL, be[i].to, 2902 true, &be[i].flags, 2903 NULL, be[i].from, symbols); 2904 2905 if (!err) { 2906 err = add_callchain_ip(thread, cursor, parent, root_al, 2907 NULL, be[i].from, 2908 true, &be[i].flags, 2909 &iter[i], 0, symbols); 2910 } 2911 if (err == -EINVAL) 2912 break; 2913 if (err) 2914 return err; 2915 } 2916 2917 if (chain_nr == 0) 2918 return 0; 2919 2920 chain_nr -= nr; 2921 } 2922 2923 check_calls: 2924 if (chain && callchain_param.order != ORDER_CALLEE) { 2925 err = find_prev_cpumode(chain, thread, cursor, parent, root_al, 2926 &cpumode, chain->nr - first_call, symbols); 2927 if (err) 2928 return (err < 0) ? err : 0; 2929 } 2930 for (i = first_call, nr_entries = 0; 2931 i < chain_nr && nr_entries < max_stack; i++) { 2932 u64 ip; 2933 2934 if (callchain_param.order == ORDER_CALLEE) 2935 j = i; 2936 else 2937 j = chain->nr - i - 1; 2938 2939 #ifdef HAVE_SKIP_CALLCHAIN_IDX 2940 if (j == skip_idx) 2941 continue; 2942 #endif 2943 ip = chain->ips[j]; 2944 if (ip < PERF_CONTEXT_MAX) 2945 ++nr_entries; 2946 else if (callchain_param.order != ORDER_CALLEE) { 2947 err = find_prev_cpumode(chain, thread, cursor, parent, 2948 root_al, &cpumode, j, symbols); 2949 if (err) 2950 return (err < 0) ? err : 0; 2951 continue; 2952 } 2953 2954 /* 2955 * PERF_CONTEXT_USER allows us to locate where the user stack ends. 2956 * Depending on callchain_param.order and the position of PERF_CONTEXT_USER, 2957 * the index will be different in order to add the missing frame 2958 * at the right place. 2959 */ 2960 2961 usr_idx = callchain_param.order == ORDER_CALLEE ? j-2 : j-1; 2962 2963 if (usr_idx >= 0 && chain->ips[usr_idx] == PERF_CONTEXT_USER) { 2964 2965 leaf_frame_caller = get_leaf_frame_caller(sample, thread, usr_idx); 2966 2967 /* 2968 * check if leaf_frame_Caller != ip to not add the same 2969 * value twice. 2970 */ 2971 2972 if (leaf_frame_caller && leaf_frame_caller != ip) { 2973 2974 err = add_callchain_ip(thread, cursor, parent, 2975 root_al, &cpumode, leaf_frame_caller, 2976 false, NULL, NULL, 0, symbols); 2977 if (err) 2978 return (err < 0) ? err : 0; 2979 } 2980 } 2981 2982 err = add_callchain_ip(thread, cursor, parent, 2983 root_al, &cpumode, ip, 2984 false, NULL, NULL, 0, symbols); 2985 2986 if (err) 2987 return (err < 0) ? err : 0; 2988 } 2989 2990 return 0; 2991 } 2992 2993 static int unwind_entry(struct unwind_entry *entry, void *arg) 2994 { 2995 struct callchain_cursor *cursor = arg; 2996 const char *srcline = NULL; 2997 u64 addr = entry->ip; 2998 2999 if (symbol_conf.hide_unresolved && entry->ms.sym == NULL) 3000 return 0; 3001 3002 if (append_inlines(cursor, &entry->ms, entry->ip, /*branch=*/false, /*branch_flags=*/NULL, 3003 /*nr_loop_iter=*/0, /*iter_cycles=*/0, /*branch_from=*/0) == 0) 3004 return 0; 3005 3006 /* 3007 * Convert entry->ip from a virtual address to an offset in 3008 * its corresponding binary. 3009 */ 3010 if (entry->ms.map) 3011 addr = map__dso_map_ip(entry->ms.map, entry->ip); 3012 3013 srcline = callchain_srcline(&entry->ms, addr); 3014 return callchain_cursor_append(cursor, entry->ip, &entry->ms, 3015 false, NULL, 0, 0, 0, srcline); 3016 } 3017 3018 static int thread__resolve_callchain_unwind(struct thread *thread, 3019 struct callchain_cursor *cursor, 3020 struct perf_sample *sample, 3021 int max_stack, bool symbols) 3022 { 3023 struct evsel *evsel = sample->evsel; 3024 3025 /* Can we do dwarf post unwind? */ 3026 if (!((evsel->core.attr.sample_type & PERF_SAMPLE_REGS_USER) && 3027 (evsel->core.attr.sample_type & PERF_SAMPLE_STACK_USER))) 3028 return 0; 3029 3030 /* Bail out if nothing was captured. */ 3031 if (!sample->user_regs || !sample->user_regs->regs || 3032 !sample->user_stack.size) 3033 return 0; 3034 3035 if (!symbols) 3036 pr_debug("Not resolving symbols with an unwinder isn't currently supported\n"); 3037 3038 return unwind__get_entries(unwind_entry, cursor, 3039 thread, sample, max_stack, false); 3040 } 3041 3042 int __thread__resolve_callchain(struct thread *thread, 3043 struct callchain_cursor *cursor, 3044 struct perf_sample *sample, 3045 struct symbol **parent, 3046 struct addr_location *root_al, 3047 int max_stack, 3048 bool symbols) 3049 { 3050 int ret = 0; 3051 3052 if (cursor == NULL) 3053 return -ENOMEM; 3054 3055 callchain_cursor_reset(cursor); 3056 3057 if (callchain_param.order == ORDER_CALLEE) { 3058 ret = thread__resolve_callchain_sample(thread, cursor, 3059 sample, 3060 parent, root_al, 3061 max_stack, symbols); 3062 if (ret) 3063 return ret; 3064 ret = thread__resolve_callchain_unwind(thread, cursor, 3065 sample, 3066 max_stack, symbols); 3067 } else { 3068 ret = thread__resolve_callchain_unwind(thread, cursor, 3069 sample, 3070 max_stack, symbols); 3071 if (ret) 3072 return ret; 3073 ret = thread__resolve_callchain_sample(thread, cursor, 3074 sample, 3075 parent, root_al, 3076 max_stack, symbols); 3077 } 3078 3079 return ret; 3080 } 3081 3082 int machine__for_each_thread(struct machine *machine, 3083 int (*fn)(struct thread *thread, void *p), 3084 void *priv) 3085 { 3086 return threads__for_each_thread(&machine->threads, fn, priv); 3087 } 3088 3089 int machines__for_each_thread(struct machines *machines, 3090 int (*fn)(struct thread *thread, void *p), 3091 void *priv) 3092 { 3093 struct rb_node *nd; 3094 int rc = 0; 3095 3096 rc = machine__for_each_thread(&machines->host, fn, priv); 3097 if (rc != 0) 3098 return rc; 3099 3100 for (nd = rb_first_cached(&machines->guests); nd; nd = rb_next(nd)) { 3101 struct machine *machine = rb_entry(nd, struct machine, rb_node); 3102 3103 rc = machine__for_each_thread(machine, fn, priv); 3104 if (rc != 0) 3105 return rc; 3106 } 3107 return rc; 3108 } 3109 3110 3111 static int thread_list_cb(struct thread *thread, void *data) 3112 { 3113 struct list_head *list = data; 3114 struct thread_list *entry = malloc(sizeof(*entry)); 3115 3116 if (!entry) 3117 return -ENOMEM; 3118 3119 entry->thread = thread__get(thread); 3120 list_add_tail(&entry->list, list); 3121 return 0; 3122 } 3123 3124 int machine__thread_list(struct machine *machine, struct list_head *list) 3125 { 3126 return machine__for_each_thread(machine, thread_list_cb, list); 3127 } 3128 3129 void thread_list__delete(struct list_head *list) 3130 { 3131 struct thread_list *pos, *next; 3132 3133 list_for_each_entry_safe(pos, next, list, list) { 3134 thread__zput(pos->thread); 3135 list_del(&pos->list); 3136 free(pos); 3137 } 3138 } 3139 3140 pid_t machine__get_current_tid(struct machine *machine, int cpu) 3141 { 3142 if (cpu < 0 || (size_t)cpu >= machine->current_tid_sz) 3143 return -1; 3144 3145 return machine->current_tid[cpu]; 3146 } 3147 3148 int machine__set_current_tid(struct machine *machine, int cpu, pid_t pid, 3149 pid_t tid) 3150 { 3151 struct thread *thread; 3152 const pid_t init_val = -1; 3153 3154 if (cpu < 0) 3155 return -EINVAL; 3156 3157 if (realloc_array_as_needed(machine->current_tid, 3158 machine->current_tid_sz, 3159 (unsigned int)cpu, 3160 &init_val)) 3161 return -ENOMEM; 3162 3163 machine->current_tid[cpu] = tid; 3164 3165 thread = machine__findnew_thread(machine, pid, tid); 3166 if (!thread) 3167 return -ENOMEM; 3168 3169 thread__set_cpu(thread, cpu); 3170 thread__put(thread); 3171 3172 return 0; 3173 } 3174 3175 int machine__nr_cpus_avail(struct machine *machine) 3176 { 3177 return machine ? perf_env__nr_cpus_avail(machine->env) : 0; 3178 } 3179 3180 int machine__get_kernel_start(struct machine *machine) 3181 { 3182 struct map *map = machine__kernel_map(machine); 3183 int err = 0; 3184 3185 /* 3186 * The only addresses above 2^63 are kernel addresses of a 64-bit 3187 * kernel. Note that addresses are unsigned so that on a 32-bit system 3188 * all addresses including kernel addresses are less than 2^32. In 3189 * that case (32-bit system), if the kernel mapping is unknown, all 3190 * addresses will be assumed to be in user space - see 3191 * machine__kernel_ip(). 3192 */ 3193 machine->kernel_start = 1ULL << 63; 3194 if (map) { 3195 err = map__load(map); 3196 /* 3197 * On x86_64, PTI entry trampolines are less than the 3198 * start of kernel text, but still above 2^63. So leave 3199 * kernel_start = 1ULL << 63 for x86_64. 3200 */ 3201 if (!err && !machine__is(machine, EM_X86_64)) 3202 machine->kernel_start = map__start(map); 3203 } 3204 return err; 3205 } 3206 3207 u8 machine__addr_cpumode(struct machine *machine, u8 cpumode, u64 addr) 3208 { 3209 u8 addr_cpumode = cpumode; 3210 bool kernel_ip; 3211 3212 if (!machine->single_address_space) 3213 goto out; 3214 3215 kernel_ip = machine__kernel_ip(machine, addr); 3216 switch (cpumode) { 3217 case PERF_RECORD_MISC_KERNEL: 3218 case PERF_RECORD_MISC_USER: 3219 addr_cpumode = kernel_ip ? PERF_RECORD_MISC_KERNEL : 3220 PERF_RECORD_MISC_USER; 3221 break; 3222 case PERF_RECORD_MISC_GUEST_KERNEL: 3223 case PERF_RECORD_MISC_GUEST_USER: 3224 addr_cpumode = kernel_ip ? PERF_RECORD_MISC_GUEST_KERNEL : 3225 PERF_RECORD_MISC_GUEST_USER; 3226 break; 3227 default: 3228 break; 3229 } 3230 out: 3231 return addr_cpumode; 3232 } 3233 3234 struct dso *machine__findnew_dso_id(struct machine *machine, const char *filename, 3235 const struct dso_id *id) 3236 { 3237 return dsos__findnew_id(&machine->dsos, filename, id); 3238 } 3239 3240 struct dso *machine__findnew_dso(struct machine *machine, const char *filename) 3241 { 3242 return machine__findnew_dso_id(machine, filename, &dso_id_empty); 3243 } 3244 3245 char *machine__resolve_kernel_addr(void *vmachine, unsigned long long *addrp, char **modp) 3246 { 3247 struct machine *machine = vmachine; 3248 struct map *map; 3249 struct symbol *sym = machine__find_kernel_symbol(machine, *addrp, &map); 3250 3251 if (sym == NULL) 3252 return NULL; 3253 3254 *modp = __map__is_kmodule(map) ? (char *)dso__short_name(map__dso(map)) : NULL; 3255 *addrp = map__unmap_ip(map, sym->start); 3256 return sym->name; 3257 } 3258 3259 struct machine__for_each_dso_cb_args { 3260 struct machine *machine; 3261 machine__dso_t fn; 3262 void *priv; 3263 }; 3264 3265 static int machine__for_each_dso_cb(struct dso *dso, void *data) 3266 { 3267 struct machine__for_each_dso_cb_args *args = data; 3268 3269 return args->fn(dso, args->machine, args->priv); 3270 } 3271 3272 int machine__for_each_dso(struct machine *machine, machine__dso_t fn, void *priv) 3273 { 3274 struct machine__for_each_dso_cb_args args = { 3275 .machine = machine, 3276 .fn = fn, 3277 .priv = priv, 3278 }; 3279 3280 return dsos__for_each_dso(&machine->dsos, machine__for_each_dso_cb, &args); 3281 } 3282 3283 int machine__for_each_kernel_map(struct machine *machine, machine__map_t fn, void *priv) 3284 { 3285 struct maps *maps = machine__kernel_maps(machine); 3286 3287 return maps__for_each_map(maps, fn, priv); 3288 } 3289 3290 bool machine__is_lock_function(struct machine *machine, u64 addr) 3291 { 3292 if (!machine->sched.text_start) { 3293 struct map *kmap; 3294 struct symbol *sym = machine__find_kernel_symbol_by_name(machine, "__sched_text_start", &kmap); 3295 3296 if (!sym) { 3297 /* to avoid retry */ 3298 machine->sched.text_start = 1; 3299 return false; 3300 } 3301 3302 machine->sched.text_start = map__unmap_ip(kmap, sym->start); 3303 3304 /* should not fail from here */ 3305 sym = machine__find_kernel_symbol_by_name(machine, "__sched_text_end", &kmap); 3306 machine->sched.text_end = map__unmap_ip(kmap, sym->start); 3307 3308 sym = machine__find_kernel_symbol_by_name(machine, "__lock_text_start", &kmap); 3309 machine->lock.text_start = map__unmap_ip(kmap, sym->start); 3310 3311 sym = machine__find_kernel_symbol_by_name(machine, "__lock_text_end", &kmap); 3312 machine->lock.text_end = map__unmap_ip(kmap, sym->start); 3313 3314 sym = machine__find_kernel_symbol_by_name(machine, "__traceiter_contention_begin", &kmap); 3315 if (sym) { 3316 machine->traceiter.text_start = map__unmap_ip(kmap, sym->start); 3317 machine->traceiter.text_end = map__unmap_ip(kmap, sym->end); 3318 } 3319 sym = machine__find_kernel_symbol_by_name(machine, "trace_contention_begin", &kmap); 3320 if (sym) { 3321 machine->trace.text_start = map__unmap_ip(kmap, sym->start); 3322 machine->trace.text_end = map__unmap_ip(kmap, sym->end); 3323 } 3324 } 3325 3326 /* failed to get kernel symbols */ 3327 if (machine->sched.text_start == 1) 3328 return false; 3329 3330 /* mutex and rwsem functions are in sched text section */ 3331 if (machine->sched.text_start <= addr && addr < machine->sched.text_end) 3332 return true; 3333 3334 /* spinlock functions are in lock text section */ 3335 if (machine->lock.text_start <= addr && addr < machine->lock.text_end) 3336 return true; 3337 3338 /* traceiter functions currently don't have their own section 3339 * but we consider them lock functions 3340 */ 3341 if (machine->traceiter.text_start != 0) { 3342 if (machine->traceiter.text_start <= addr && addr < machine->traceiter.text_end) 3343 return true; 3344 } 3345 3346 if (machine->trace.text_start != 0) { 3347 if (machine->trace.text_start <= addr && addr < machine->trace.text_end) 3348 return true; 3349 } 3350 3351 return false; 3352 } 3353 3354 int machine__hit_all_dsos(struct machine *machine) 3355 { 3356 return dsos__hit_all(&machine->dsos); 3357 } 3358