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