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