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