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