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