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