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