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