1 #include "callchain.h" 2 #include "debug.h" 3 #include "event.h" 4 #include "evsel.h" 5 #include "hist.h" 6 #include "machine.h" 7 #include "map.h" 8 #include "sort.h" 9 #include "strlist.h" 10 #include "thread.h" 11 #include "vdso.h" 12 #include <stdbool.h> 13 #include <symbol/kallsyms.h> 14 #include "unwind.h" 15 #include "linux/hash.h" 16 17 static void dsos__init(struct dsos *dsos) 18 { 19 INIT_LIST_HEAD(&dsos->head); 20 dsos->root = RB_ROOT; 21 } 22 23 int machine__init(struct machine *machine, const char *root_dir, pid_t pid) 24 { 25 map_groups__init(&machine->kmaps, machine); 26 RB_CLEAR_NODE(&machine->rb_node); 27 dsos__init(&machine->user_dsos); 28 dsos__init(&machine->kernel_dsos); 29 30 machine->threads = RB_ROOT; 31 INIT_LIST_HEAD(&machine->dead_threads); 32 machine->last_match = NULL; 33 34 machine->vdso_info = NULL; 35 36 machine->pid = pid; 37 38 machine->symbol_filter = NULL; 39 machine->id_hdr_size = 0; 40 machine->comm_exec = false; 41 machine->kernel_start = 0; 42 43 machine->root_dir = strdup(root_dir); 44 if (machine->root_dir == NULL) 45 return -ENOMEM; 46 47 if (pid != HOST_KERNEL_ID) { 48 struct thread *thread = machine__findnew_thread(machine, -1, 49 pid); 50 char comm[64]; 51 52 if (thread == NULL) 53 return -ENOMEM; 54 55 snprintf(comm, sizeof(comm), "[guest/%d]", pid); 56 thread__set_comm(thread, comm, 0); 57 } 58 59 machine->current_tid = NULL; 60 61 return 0; 62 } 63 64 struct machine *machine__new_host(void) 65 { 66 struct machine *machine = malloc(sizeof(*machine)); 67 68 if (machine != NULL) { 69 machine__init(machine, "", HOST_KERNEL_ID); 70 71 if (machine__create_kernel_maps(machine) < 0) 72 goto out_delete; 73 } 74 75 return machine; 76 out_delete: 77 free(machine); 78 return NULL; 79 } 80 81 static void dsos__delete(struct dsos *dsos) 82 { 83 struct dso *pos, *n; 84 85 list_for_each_entry_safe(pos, n, &dsos->head, node) { 86 RB_CLEAR_NODE(&pos->rb_node); 87 list_del(&pos->node); 88 dso__delete(pos); 89 } 90 } 91 92 void machine__delete_threads(struct machine *machine) 93 { 94 struct rb_node *nd = rb_first(&machine->threads); 95 96 while (nd) { 97 struct thread *t = rb_entry(nd, struct thread, rb_node); 98 99 nd = rb_next(nd); 100 machine__remove_thread(machine, t); 101 } 102 } 103 104 void machine__exit(struct machine *machine) 105 { 106 map_groups__exit(&machine->kmaps); 107 dsos__delete(&machine->user_dsos); 108 dsos__delete(&machine->kernel_dsos); 109 vdso__exit(machine); 110 zfree(&machine->root_dir); 111 zfree(&machine->current_tid); 112 } 113 114 void machine__delete(struct machine *machine) 115 { 116 machine__exit(machine); 117 free(machine); 118 } 119 120 void machines__init(struct machines *machines) 121 { 122 machine__init(&machines->host, "", HOST_KERNEL_ID); 123 machines->guests = RB_ROOT; 124 machines->symbol_filter = NULL; 125 } 126 127 void machines__exit(struct machines *machines) 128 { 129 machine__exit(&machines->host); 130 /* XXX exit guest */ 131 } 132 133 struct machine *machines__add(struct machines *machines, pid_t pid, 134 const char *root_dir) 135 { 136 struct rb_node **p = &machines->guests.rb_node; 137 struct rb_node *parent = NULL; 138 struct machine *pos, *machine = malloc(sizeof(*machine)); 139 140 if (machine == NULL) 141 return NULL; 142 143 if (machine__init(machine, root_dir, pid) != 0) { 144 free(machine); 145 return NULL; 146 } 147 148 machine->symbol_filter = machines->symbol_filter; 149 150 while (*p != NULL) { 151 parent = *p; 152 pos = rb_entry(parent, struct machine, rb_node); 153 if (pid < pos->pid) 154 p = &(*p)->rb_left; 155 else 156 p = &(*p)->rb_right; 157 } 158 159 rb_link_node(&machine->rb_node, parent, p); 160 rb_insert_color(&machine->rb_node, &machines->guests); 161 162 return machine; 163 } 164 165 void machines__set_symbol_filter(struct machines *machines, 166 symbol_filter_t symbol_filter) 167 { 168 struct rb_node *nd; 169 170 machines->symbol_filter = symbol_filter; 171 machines->host.symbol_filter = symbol_filter; 172 173 for (nd = rb_first(&machines->guests); nd; nd = rb_next(nd)) { 174 struct machine *machine = rb_entry(nd, struct machine, rb_node); 175 176 machine->symbol_filter = symbol_filter; 177 } 178 } 179 180 void machines__set_comm_exec(struct machines *machines, bool comm_exec) 181 { 182 struct rb_node *nd; 183 184 machines->host.comm_exec = comm_exec; 185 186 for (nd = rb_first(&machines->guests); nd; nd = rb_next(nd)) { 187 struct machine *machine = rb_entry(nd, struct machine, rb_node); 188 189 machine->comm_exec = comm_exec; 190 } 191 } 192 193 struct machine *machines__find(struct machines *machines, pid_t pid) 194 { 195 struct rb_node **p = &machines->guests.rb_node; 196 struct rb_node *parent = NULL; 197 struct machine *machine; 198 struct machine *default_machine = NULL; 199 200 if (pid == HOST_KERNEL_ID) 201 return &machines->host; 202 203 while (*p != NULL) { 204 parent = *p; 205 machine = rb_entry(parent, struct machine, rb_node); 206 if (pid < machine->pid) 207 p = &(*p)->rb_left; 208 else if (pid > machine->pid) 209 p = &(*p)->rb_right; 210 else 211 return machine; 212 if (!machine->pid) 213 default_machine = machine; 214 } 215 216 return default_machine; 217 } 218 219 struct machine *machines__findnew(struct machines *machines, pid_t pid) 220 { 221 char path[PATH_MAX]; 222 const char *root_dir = ""; 223 struct machine *machine = machines__find(machines, pid); 224 225 if (machine && (machine->pid == pid)) 226 goto out; 227 228 if ((pid != HOST_KERNEL_ID) && 229 (pid != DEFAULT_GUEST_KERNEL_ID) && 230 (symbol_conf.guestmount)) { 231 sprintf(path, "%s/%d", symbol_conf.guestmount, pid); 232 if (access(path, R_OK)) { 233 static struct strlist *seen; 234 235 if (!seen) 236 seen = strlist__new(true, NULL); 237 238 if (!strlist__has_entry(seen, path)) { 239 pr_err("Can't access file %s\n", path); 240 strlist__add(seen, path); 241 } 242 machine = NULL; 243 goto out; 244 } 245 root_dir = path; 246 } 247 248 machine = machines__add(machines, pid, root_dir); 249 out: 250 return machine; 251 } 252 253 void machines__process_guests(struct machines *machines, 254 machine__process_t process, void *data) 255 { 256 struct rb_node *nd; 257 258 for (nd = rb_first(&machines->guests); nd; nd = rb_next(nd)) { 259 struct machine *pos = rb_entry(nd, struct machine, rb_node); 260 process(pos, data); 261 } 262 } 263 264 char *machine__mmap_name(struct machine *machine, char *bf, size_t size) 265 { 266 if (machine__is_host(machine)) 267 snprintf(bf, size, "[%s]", "kernel.kallsyms"); 268 else if (machine__is_default_guest(machine)) 269 snprintf(bf, size, "[%s]", "guest.kernel.kallsyms"); 270 else { 271 snprintf(bf, size, "[%s.%d]", "guest.kernel.kallsyms", 272 machine->pid); 273 } 274 275 return bf; 276 } 277 278 void machines__set_id_hdr_size(struct machines *machines, u16 id_hdr_size) 279 { 280 struct rb_node *node; 281 struct machine *machine; 282 283 machines->host.id_hdr_size = id_hdr_size; 284 285 for (node = rb_first(&machines->guests); node; node = rb_next(node)) { 286 machine = rb_entry(node, struct machine, rb_node); 287 machine->id_hdr_size = id_hdr_size; 288 } 289 290 return; 291 } 292 293 static void machine__update_thread_pid(struct machine *machine, 294 struct thread *th, pid_t pid) 295 { 296 struct thread *leader; 297 298 if (pid == th->pid_ || pid == -1 || th->pid_ != -1) 299 return; 300 301 th->pid_ = pid; 302 303 if (th->pid_ == th->tid) 304 return; 305 306 leader = machine__findnew_thread(machine, th->pid_, th->pid_); 307 if (!leader) 308 goto out_err; 309 310 if (!leader->mg) 311 leader->mg = map_groups__new(machine); 312 313 if (!leader->mg) 314 goto out_err; 315 316 if (th->mg == leader->mg) 317 return; 318 319 if (th->mg) { 320 /* 321 * Maps are created from MMAP events which provide the pid and 322 * tid. Consequently there never should be any maps on a thread 323 * with an unknown pid. Just print an error if there are. 324 */ 325 if (!map_groups__empty(th->mg)) 326 pr_err("Discarding thread maps for %d:%d\n", 327 th->pid_, th->tid); 328 map_groups__delete(th->mg); 329 } 330 331 th->mg = map_groups__get(leader->mg); 332 333 return; 334 335 out_err: 336 pr_err("Failed to join map groups for %d:%d\n", th->pid_, th->tid); 337 } 338 339 static struct thread *__machine__findnew_thread(struct machine *machine, 340 pid_t pid, pid_t tid, 341 bool create) 342 { 343 struct rb_node **p = &machine->threads.rb_node; 344 struct rb_node *parent = NULL; 345 struct thread *th; 346 347 /* 348 * Front-end cache - TID lookups come in blocks, 349 * so most of the time we dont have to look up 350 * the full rbtree: 351 */ 352 th = machine->last_match; 353 if (th != NULL) { 354 if (th->tid == tid) { 355 machine__update_thread_pid(machine, th, pid); 356 return th; 357 } 358 359 thread__zput(machine->last_match); 360 } 361 362 while (*p != NULL) { 363 parent = *p; 364 th = rb_entry(parent, struct thread, rb_node); 365 366 if (th->tid == tid) { 367 machine->last_match = thread__get(th); 368 machine__update_thread_pid(machine, th, pid); 369 return th; 370 } 371 372 if (tid < th->tid) 373 p = &(*p)->rb_left; 374 else 375 p = &(*p)->rb_right; 376 } 377 378 if (!create) 379 return NULL; 380 381 th = thread__new(pid, tid); 382 if (th != NULL) { 383 rb_link_node(&th->rb_node, parent, p); 384 rb_insert_color(&th->rb_node, &machine->threads); 385 386 /* 387 * We have to initialize map_groups separately 388 * after rb tree is updated. 389 * 390 * The reason is that we call machine__findnew_thread 391 * within thread__init_map_groups to find the thread 392 * leader and that would screwed the rb tree. 393 */ 394 if (thread__init_map_groups(th, machine)) { 395 rb_erase(&th->rb_node, &machine->threads); 396 thread__delete(th); 397 return NULL; 398 } 399 /* 400 * It is now in the rbtree, get a ref 401 */ 402 thread__get(th); 403 machine->last_match = thread__get(th); 404 } 405 406 return th; 407 } 408 409 struct thread *machine__findnew_thread(struct machine *machine, pid_t pid, 410 pid_t tid) 411 { 412 return __machine__findnew_thread(machine, pid, tid, true); 413 } 414 415 struct thread *machine__find_thread(struct machine *machine, pid_t pid, 416 pid_t tid) 417 { 418 return __machine__findnew_thread(machine, pid, tid, false); 419 } 420 421 struct comm *machine__thread_exec_comm(struct machine *machine, 422 struct thread *thread) 423 { 424 if (machine->comm_exec) 425 return thread__exec_comm(thread); 426 else 427 return thread__comm(thread); 428 } 429 430 int machine__process_comm_event(struct machine *machine, union perf_event *event, 431 struct perf_sample *sample) 432 { 433 struct thread *thread = machine__findnew_thread(machine, 434 event->comm.pid, 435 event->comm.tid); 436 bool exec = event->header.misc & PERF_RECORD_MISC_COMM_EXEC; 437 438 if (exec) 439 machine->comm_exec = true; 440 441 if (dump_trace) 442 perf_event__fprintf_comm(event, stdout); 443 444 if (thread == NULL || 445 __thread__set_comm(thread, event->comm.comm, sample->time, exec)) { 446 dump_printf("problem processing PERF_RECORD_COMM, skipping event.\n"); 447 return -1; 448 } 449 450 return 0; 451 } 452 453 int machine__process_lost_event(struct machine *machine __maybe_unused, 454 union perf_event *event, struct perf_sample *sample __maybe_unused) 455 { 456 dump_printf(": id:%" PRIu64 ": lost:%" PRIu64 "\n", 457 event->lost.id, event->lost.lost); 458 return 0; 459 } 460 461 static struct dso* 462 machine__module_dso(struct machine *machine, struct kmod_path *m, 463 const char *filename) 464 { 465 struct dso *dso; 466 467 dso = dsos__find(&machine->kernel_dsos, m->name, true); 468 if (!dso) { 469 dso = dsos__addnew(&machine->kernel_dsos, m->name); 470 if (dso == NULL) 471 return NULL; 472 473 if (machine__is_host(machine)) 474 dso->symtab_type = DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE; 475 else 476 dso->symtab_type = DSO_BINARY_TYPE__GUEST_KMODULE; 477 478 /* _KMODULE_COMP should be next to _KMODULE */ 479 if (m->kmod && m->comp) 480 dso->symtab_type++; 481 482 dso__set_short_name(dso, strdup(m->name), true); 483 dso__set_long_name(dso, strdup(filename), true); 484 } 485 486 return dso; 487 } 488 489 int machine__process_aux_event(struct machine *machine __maybe_unused, 490 union perf_event *event) 491 { 492 if (dump_trace) 493 perf_event__fprintf_aux(event, stdout); 494 return 0; 495 } 496 497 int machine__process_itrace_start_event(struct machine *machine __maybe_unused, 498 union perf_event *event) 499 { 500 if (dump_trace) 501 perf_event__fprintf_itrace_start(event, stdout); 502 return 0; 503 } 504 505 struct map *machine__new_module(struct machine *machine, u64 start, 506 const char *filename) 507 { 508 struct map *map = NULL; 509 struct dso *dso; 510 struct kmod_path m; 511 512 if (kmod_path__parse_name(&m, filename)) 513 return NULL; 514 515 map = map_groups__find_by_name(&machine->kmaps, MAP__FUNCTION, 516 m.name); 517 if (map) 518 goto out; 519 520 dso = machine__module_dso(machine, &m, filename); 521 if (dso == NULL) 522 goto out; 523 524 map = map__new2(start, dso, MAP__FUNCTION); 525 if (map == NULL) 526 goto out; 527 528 map_groups__insert(&machine->kmaps, map); 529 530 out: 531 free(m.name); 532 return map; 533 } 534 535 size_t machines__fprintf_dsos(struct machines *machines, FILE *fp) 536 { 537 struct rb_node *nd; 538 size_t ret = __dsos__fprintf(&machines->host.kernel_dsos.head, fp) + 539 __dsos__fprintf(&machines->host.user_dsos.head, fp); 540 541 for (nd = rb_first(&machines->guests); nd; nd = rb_next(nd)) { 542 struct machine *pos = rb_entry(nd, struct machine, rb_node); 543 ret += __dsos__fprintf(&pos->kernel_dsos.head, fp); 544 ret += __dsos__fprintf(&pos->user_dsos.head, fp); 545 } 546 547 return ret; 548 } 549 550 size_t machine__fprintf_dsos_buildid(struct machine *m, FILE *fp, 551 bool (skip)(struct dso *dso, int parm), int parm) 552 { 553 return __dsos__fprintf_buildid(&m->kernel_dsos.head, fp, skip, parm) + 554 __dsos__fprintf_buildid(&m->user_dsos.head, fp, skip, parm); 555 } 556 557 size_t machines__fprintf_dsos_buildid(struct machines *machines, FILE *fp, 558 bool (skip)(struct dso *dso, int parm), int parm) 559 { 560 struct rb_node *nd; 561 size_t ret = machine__fprintf_dsos_buildid(&machines->host, fp, skip, parm); 562 563 for (nd = rb_first(&machines->guests); nd; nd = rb_next(nd)) { 564 struct machine *pos = rb_entry(nd, struct machine, rb_node); 565 ret += machine__fprintf_dsos_buildid(pos, fp, skip, parm); 566 } 567 return ret; 568 } 569 570 size_t machine__fprintf_vmlinux_path(struct machine *machine, FILE *fp) 571 { 572 int i; 573 size_t printed = 0; 574 struct dso *kdso = machine->vmlinux_maps[MAP__FUNCTION]->dso; 575 576 if (kdso->has_build_id) { 577 char filename[PATH_MAX]; 578 if (dso__build_id_filename(kdso, filename, sizeof(filename))) 579 printed += fprintf(fp, "[0] %s\n", filename); 580 } 581 582 for (i = 0; i < vmlinux_path__nr_entries; ++i) 583 printed += fprintf(fp, "[%d] %s\n", 584 i + kdso->has_build_id, vmlinux_path[i]); 585 586 return printed; 587 } 588 589 size_t machine__fprintf(struct machine *machine, FILE *fp) 590 { 591 size_t ret = 0; 592 struct rb_node *nd; 593 594 for (nd = rb_first(&machine->threads); nd; nd = rb_next(nd)) { 595 struct thread *pos = rb_entry(nd, struct thread, rb_node); 596 597 ret += thread__fprintf(pos, fp); 598 } 599 600 return ret; 601 } 602 603 static struct dso *machine__get_kernel(struct machine *machine) 604 { 605 const char *vmlinux_name = NULL; 606 struct dso *kernel; 607 608 if (machine__is_host(machine)) { 609 vmlinux_name = symbol_conf.vmlinux_name; 610 if (!vmlinux_name) 611 vmlinux_name = "[kernel.kallsyms]"; 612 613 kernel = dso__kernel_findnew(machine, vmlinux_name, 614 "[kernel]", 615 DSO_TYPE_KERNEL); 616 } else { 617 char bf[PATH_MAX]; 618 619 if (machine__is_default_guest(machine)) 620 vmlinux_name = symbol_conf.default_guest_vmlinux_name; 621 if (!vmlinux_name) 622 vmlinux_name = machine__mmap_name(machine, bf, 623 sizeof(bf)); 624 625 kernel = dso__kernel_findnew(machine, vmlinux_name, 626 "[guest.kernel]", 627 DSO_TYPE_GUEST_KERNEL); 628 } 629 630 if (kernel != NULL && (!kernel->has_build_id)) 631 dso__read_running_kernel_build_id(kernel, machine); 632 633 return kernel; 634 } 635 636 struct process_args { 637 u64 start; 638 }; 639 640 static void machine__get_kallsyms_filename(struct machine *machine, char *buf, 641 size_t bufsz) 642 { 643 if (machine__is_default_guest(machine)) 644 scnprintf(buf, bufsz, "%s", symbol_conf.default_guest_kallsyms); 645 else 646 scnprintf(buf, bufsz, "%s/proc/kallsyms", machine->root_dir); 647 } 648 649 const char *ref_reloc_sym_names[] = {"_text", "_stext", NULL}; 650 651 /* Figure out the start address of kernel map from /proc/kallsyms. 652 * Returns the name of the start symbol in *symbol_name. Pass in NULL as 653 * symbol_name if it's not that important. 654 */ 655 static u64 machine__get_running_kernel_start(struct machine *machine, 656 const char **symbol_name) 657 { 658 char filename[PATH_MAX]; 659 int i; 660 const char *name; 661 u64 addr = 0; 662 663 machine__get_kallsyms_filename(machine, filename, PATH_MAX); 664 665 if (symbol__restricted_filename(filename, "/proc/kallsyms")) 666 return 0; 667 668 for (i = 0; (name = ref_reloc_sym_names[i]) != NULL; i++) { 669 addr = kallsyms__get_function_start(filename, name); 670 if (addr) 671 break; 672 } 673 674 if (symbol_name) 675 *symbol_name = name; 676 677 return addr; 678 } 679 680 int __machine__create_kernel_maps(struct machine *machine, struct dso *kernel) 681 { 682 enum map_type type; 683 u64 start = machine__get_running_kernel_start(machine, NULL); 684 685 for (type = 0; type < MAP__NR_TYPES; ++type) { 686 struct kmap *kmap; 687 688 machine->vmlinux_maps[type] = map__new2(start, kernel, type); 689 if (machine->vmlinux_maps[type] == NULL) 690 return -1; 691 692 machine->vmlinux_maps[type]->map_ip = 693 machine->vmlinux_maps[type]->unmap_ip = 694 identity__map_ip; 695 kmap = map__kmap(machine->vmlinux_maps[type]); 696 if (!kmap) 697 return -1; 698 699 kmap->kmaps = &machine->kmaps; 700 map_groups__insert(&machine->kmaps, 701 machine->vmlinux_maps[type]); 702 } 703 704 return 0; 705 } 706 707 void machine__destroy_kernel_maps(struct machine *machine) 708 { 709 enum map_type type; 710 711 for (type = 0; type < MAP__NR_TYPES; ++type) { 712 struct kmap *kmap; 713 714 if (machine->vmlinux_maps[type] == NULL) 715 continue; 716 717 kmap = map__kmap(machine->vmlinux_maps[type]); 718 map_groups__remove(&machine->kmaps, 719 machine->vmlinux_maps[type]); 720 if (kmap && kmap->ref_reloc_sym) { 721 /* 722 * ref_reloc_sym is shared among all maps, so free just 723 * on one of them. 724 */ 725 if (type == MAP__FUNCTION) { 726 zfree((char **)&kmap->ref_reloc_sym->name); 727 zfree(&kmap->ref_reloc_sym); 728 } else 729 kmap->ref_reloc_sym = NULL; 730 } 731 732 map__delete(machine->vmlinux_maps[type]); 733 machine->vmlinux_maps[type] = NULL; 734 } 735 } 736 737 int machines__create_guest_kernel_maps(struct machines *machines) 738 { 739 int ret = 0; 740 struct dirent **namelist = NULL; 741 int i, items = 0; 742 char path[PATH_MAX]; 743 pid_t pid; 744 char *endp; 745 746 if (symbol_conf.default_guest_vmlinux_name || 747 symbol_conf.default_guest_modules || 748 symbol_conf.default_guest_kallsyms) { 749 machines__create_kernel_maps(machines, DEFAULT_GUEST_KERNEL_ID); 750 } 751 752 if (symbol_conf.guestmount) { 753 items = scandir(symbol_conf.guestmount, &namelist, NULL, NULL); 754 if (items <= 0) 755 return -ENOENT; 756 for (i = 0; i < items; i++) { 757 if (!isdigit(namelist[i]->d_name[0])) { 758 /* Filter out . and .. */ 759 continue; 760 } 761 pid = (pid_t)strtol(namelist[i]->d_name, &endp, 10); 762 if ((*endp != '\0') || 763 (endp == namelist[i]->d_name) || 764 (errno == ERANGE)) { 765 pr_debug("invalid directory (%s). Skipping.\n", 766 namelist[i]->d_name); 767 continue; 768 } 769 sprintf(path, "%s/%s/proc/kallsyms", 770 symbol_conf.guestmount, 771 namelist[i]->d_name); 772 ret = access(path, R_OK); 773 if (ret) { 774 pr_debug("Can't access file %s\n", path); 775 goto failure; 776 } 777 machines__create_kernel_maps(machines, pid); 778 } 779 failure: 780 free(namelist); 781 } 782 783 return ret; 784 } 785 786 void machines__destroy_kernel_maps(struct machines *machines) 787 { 788 struct rb_node *next = rb_first(&machines->guests); 789 790 machine__destroy_kernel_maps(&machines->host); 791 792 while (next) { 793 struct machine *pos = rb_entry(next, struct machine, rb_node); 794 795 next = rb_next(&pos->rb_node); 796 rb_erase(&pos->rb_node, &machines->guests); 797 machine__delete(pos); 798 } 799 } 800 801 int machines__create_kernel_maps(struct machines *machines, pid_t pid) 802 { 803 struct machine *machine = machines__findnew(machines, pid); 804 805 if (machine == NULL) 806 return -1; 807 808 return machine__create_kernel_maps(machine); 809 } 810 811 int machine__load_kallsyms(struct machine *machine, const char *filename, 812 enum map_type type, symbol_filter_t filter) 813 { 814 struct map *map = machine->vmlinux_maps[type]; 815 int ret = dso__load_kallsyms(map->dso, filename, map, filter); 816 817 if (ret > 0) { 818 dso__set_loaded(map->dso, type); 819 /* 820 * Since /proc/kallsyms will have multiple sessions for the 821 * kernel, with modules between them, fixup the end of all 822 * sections. 823 */ 824 __map_groups__fixup_end(&machine->kmaps, type); 825 } 826 827 return ret; 828 } 829 830 int machine__load_vmlinux_path(struct machine *machine, enum map_type type, 831 symbol_filter_t filter) 832 { 833 struct map *map = machine->vmlinux_maps[type]; 834 int ret = dso__load_vmlinux_path(map->dso, map, filter); 835 836 if (ret > 0) 837 dso__set_loaded(map->dso, type); 838 839 return ret; 840 } 841 842 static void map_groups__fixup_end(struct map_groups *mg) 843 { 844 int i; 845 for (i = 0; i < MAP__NR_TYPES; ++i) 846 __map_groups__fixup_end(mg, i); 847 } 848 849 static char *get_kernel_version(const char *root_dir) 850 { 851 char version[PATH_MAX]; 852 FILE *file; 853 char *name, *tmp; 854 const char *prefix = "Linux version "; 855 856 sprintf(version, "%s/proc/version", root_dir); 857 file = fopen(version, "r"); 858 if (!file) 859 return NULL; 860 861 version[0] = '\0'; 862 tmp = fgets(version, sizeof(version), file); 863 fclose(file); 864 865 name = strstr(version, prefix); 866 if (!name) 867 return NULL; 868 name += strlen(prefix); 869 tmp = strchr(name, ' '); 870 if (tmp) 871 *tmp = '\0'; 872 873 return strdup(name); 874 } 875 876 static bool is_kmod_dso(struct dso *dso) 877 { 878 return dso->symtab_type == DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE || 879 dso->symtab_type == DSO_BINARY_TYPE__GUEST_KMODULE; 880 } 881 882 static int map_groups__set_module_path(struct map_groups *mg, const char *path, 883 struct kmod_path *m) 884 { 885 struct map *map; 886 char *long_name; 887 888 map = map_groups__find_by_name(mg, MAP__FUNCTION, m->name); 889 if (map == NULL) 890 return 0; 891 892 long_name = strdup(path); 893 if (long_name == NULL) 894 return -ENOMEM; 895 896 dso__set_long_name(map->dso, long_name, true); 897 dso__kernel_module_get_build_id(map->dso, ""); 898 899 /* 900 * Full name could reveal us kmod compression, so 901 * we need to update the symtab_type if needed. 902 */ 903 if (m->comp && is_kmod_dso(map->dso)) 904 map->dso->symtab_type++; 905 906 return 0; 907 } 908 909 static int map_groups__set_modules_path_dir(struct map_groups *mg, 910 const char *dir_name, int depth) 911 { 912 struct dirent *dent; 913 DIR *dir = opendir(dir_name); 914 int ret = 0; 915 916 if (!dir) { 917 pr_debug("%s: cannot open %s dir\n", __func__, dir_name); 918 return -1; 919 } 920 921 while ((dent = readdir(dir)) != NULL) { 922 char path[PATH_MAX]; 923 struct stat st; 924 925 /*sshfs might return bad dent->d_type, so we have to stat*/ 926 snprintf(path, sizeof(path), "%s/%s", dir_name, dent->d_name); 927 if (stat(path, &st)) 928 continue; 929 930 if (S_ISDIR(st.st_mode)) { 931 if (!strcmp(dent->d_name, ".") || 932 !strcmp(dent->d_name, "..")) 933 continue; 934 935 /* Do not follow top-level source and build symlinks */ 936 if (depth == 0) { 937 if (!strcmp(dent->d_name, "source") || 938 !strcmp(dent->d_name, "build")) 939 continue; 940 } 941 942 ret = map_groups__set_modules_path_dir(mg, path, 943 depth + 1); 944 if (ret < 0) 945 goto out; 946 } else { 947 struct kmod_path m; 948 949 ret = kmod_path__parse_name(&m, dent->d_name); 950 if (ret) 951 goto out; 952 953 if (m.kmod) 954 ret = map_groups__set_module_path(mg, path, &m); 955 956 free(m.name); 957 958 if (ret) 959 goto out; 960 } 961 } 962 963 out: 964 closedir(dir); 965 return ret; 966 } 967 968 static int machine__set_modules_path(struct machine *machine) 969 { 970 char *version; 971 char modules_path[PATH_MAX]; 972 973 version = get_kernel_version(machine->root_dir); 974 if (!version) 975 return -1; 976 977 snprintf(modules_path, sizeof(modules_path), "%s/lib/modules/%s", 978 machine->root_dir, version); 979 free(version); 980 981 return map_groups__set_modules_path_dir(&machine->kmaps, modules_path, 0); 982 } 983 984 static int machine__create_module(void *arg, const char *name, u64 start) 985 { 986 struct machine *machine = arg; 987 struct map *map; 988 989 map = machine__new_module(machine, start, name); 990 if (map == NULL) 991 return -1; 992 993 dso__kernel_module_get_build_id(map->dso, machine->root_dir); 994 995 return 0; 996 } 997 998 static int machine__create_modules(struct machine *machine) 999 { 1000 const char *modules; 1001 char path[PATH_MAX]; 1002 1003 if (machine__is_default_guest(machine)) { 1004 modules = symbol_conf.default_guest_modules; 1005 } else { 1006 snprintf(path, PATH_MAX, "%s/proc/modules", machine->root_dir); 1007 modules = path; 1008 } 1009 1010 if (symbol__restricted_filename(modules, "/proc/modules")) 1011 return -1; 1012 1013 if (modules__parse(modules, machine, machine__create_module)) 1014 return -1; 1015 1016 if (!machine__set_modules_path(machine)) 1017 return 0; 1018 1019 pr_debug("Problems setting modules path maps, continuing anyway...\n"); 1020 1021 return 0; 1022 } 1023 1024 int machine__create_kernel_maps(struct machine *machine) 1025 { 1026 struct dso *kernel = machine__get_kernel(machine); 1027 const char *name; 1028 u64 addr = machine__get_running_kernel_start(machine, &name); 1029 if (!addr) 1030 return -1; 1031 1032 if (kernel == NULL || 1033 __machine__create_kernel_maps(machine, kernel) < 0) 1034 return -1; 1035 1036 if (symbol_conf.use_modules && machine__create_modules(machine) < 0) { 1037 if (machine__is_host(machine)) 1038 pr_debug("Problems creating module maps, " 1039 "continuing anyway...\n"); 1040 else 1041 pr_debug("Problems creating module maps for guest %d, " 1042 "continuing anyway...\n", machine->pid); 1043 } 1044 1045 /* 1046 * Now that we have all the maps created, just set the ->end of them: 1047 */ 1048 map_groups__fixup_end(&machine->kmaps); 1049 1050 if (maps__set_kallsyms_ref_reloc_sym(machine->vmlinux_maps, name, 1051 addr)) { 1052 machine__destroy_kernel_maps(machine); 1053 return -1; 1054 } 1055 1056 return 0; 1057 } 1058 1059 static void machine__set_kernel_mmap_len(struct machine *machine, 1060 union perf_event *event) 1061 { 1062 int i; 1063 1064 for (i = 0; i < MAP__NR_TYPES; i++) { 1065 machine->vmlinux_maps[i]->start = event->mmap.start; 1066 machine->vmlinux_maps[i]->end = (event->mmap.start + 1067 event->mmap.len); 1068 /* 1069 * Be a bit paranoid here, some perf.data file came with 1070 * a zero sized synthesized MMAP event for the kernel. 1071 */ 1072 if (machine->vmlinux_maps[i]->end == 0) 1073 machine->vmlinux_maps[i]->end = ~0ULL; 1074 } 1075 } 1076 1077 static bool machine__uses_kcore(struct machine *machine) 1078 { 1079 struct dso *dso; 1080 1081 list_for_each_entry(dso, &machine->kernel_dsos.head, node) { 1082 if (dso__is_kcore(dso)) 1083 return true; 1084 } 1085 1086 return false; 1087 } 1088 1089 static int machine__process_kernel_mmap_event(struct machine *machine, 1090 union perf_event *event) 1091 { 1092 struct map *map; 1093 char kmmap_prefix[PATH_MAX]; 1094 enum dso_kernel_type kernel_type; 1095 bool is_kernel_mmap; 1096 1097 /* If we have maps from kcore then we do not need or want any others */ 1098 if (machine__uses_kcore(machine)) 1099 return 0; 1100 1101 machine__mmap_name(machine, kmmap_prefix, sizeof(kmmap_prefix)); 1102 if (machine__is_host(machine)) 1103 kernel_type = DSO_TYPE_KERNEL; 1104 else 1105 kernel_type = DSO_TYPE_GUEST_KERNEL; 1106 1107 is_kernel_mmap = memcmp(event->mmap.filename, 1108 kmmap_prefix, 1109 strlen(kmmap_prefix) - 1) == 0; 1110 if (event->mmap.filename[0] == '/' || 1111 (!is_kernel_mmap && event->mmap.filename[0] == '[')) { 1112 map = machine__new_module(machine, event->mmap.start, 1113 event->mmap.filename); 1114 if (map == NULL) 1115 goto out_problem; 1116 1117 map->end = map->start + event->mmap.len; 1118 } else if (is_kernel_mmap) { 1119 const char *symbol_name = (event->mmap.filename + 1120 strlen(kmmap_prefix)); 1121 /* 1122 * Should be there already, from the build-id table in 1123 * the header. 1124 */ 1125 struct dso *kernel = NULL; 1126 struct dso *dso; 1127 1128 list_for_each_entry(dso, &machine->kernel_dsos.head, node) { 1129 if (is_kernel_module(dso->long_name)) 1130 continue; 1131 1132 kernel = dso; 1133 break; 1134 } 1135 1136 if (kernel == NULL) 1137 kernel = __dsos__findnew(&machine->kernel_dsos, 1138 kmmap_prefix); 1139 if (kernel == NULL) 1140 goto out_problem; 1141 1142 kernel->kernel = kernel_type; 1143 if (__machine__create_kernel_maps(machine, kernel) < 0) 1144 goto out_problem; 1145 1146 if (strstr(kernel->long_name, "vmlinux")) 1147 dso__set_short_name(kernel, "[kernel.vmlinux]", false); 1148 1149 machine__set_kernel_mmap_len(machine, event); 1150 1151 /* 1152 * Avoid using a zero address (kptr_restrict) for the ref reloc 1153 * symbol. Effectively having zero here means that at record 1154 * time /proc/sys/kernel/kptr_restrict was non zero. 1155 */ 1156 if (event->mmap.pgoff != 0) { 1157 maps__set_kallsyms_ref_reloc_sym(machine->vmlinux_maps, 1158 symbol_name, 1159 event->mmap.pgoff); 1160 } 1161 1162 if (machine__is_default_guest(machine)) { 1163 /* 1164 * preload dso of guest kernel and modules 1165 */ 1166 dso__load(kernel, machine->vmlinux_maps[MAP__FUNCTION], 1167 NULL); 1168 } 1169 } 1170 return 0; 1171 out_problem: 1172 return -1; 1173 } 1174 1175 int machine__process_mmap2_event(struct machine *machine, 1176 union perf_event *event, 1177 struct perf_sample *sample __maybe_unused) 1178 { 1179 u8 cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK; 1180 struct thread *thread; 1181 struct map *map; 1182 enum map_type type; 1183 int ret = 0; 1184 1185 if (dump_trace) 1186 perf_event__fprintf_mmap2(event, stdout); 1187 1188 if (cpumode == PERF_RECORD_MISC_GUEST_KERNEL || 1189 cpumode == PERF_RECORD_MISC_KERNEL) { 1190 ret = machine__process_kernel_mmap_event(machine, event); 1191 if (ret < 0) 1192 goto out_problem; 1193 return 0; 1194 } 1195 1196 thread = machine__findnew_thread(machine, event->mmap2.pid, 1197 event->mmap2.tid); 1198 if (thread == NULL) 1199 goto out_problem; 1200 1201 if (event->header.misc & PERF_RECORD_MISC_MMAP_DATA) 1202 type = MAP__VARIABLE; 1203 else 1204 type = MAP__FUNCTION; 1205 1206 map = map__new(machine, event->mmap2.start, 1207 event->mmap2.len, event->mmap2.pgoff, 1208 event->mmap2.pid, event->mmap2.maj, 1209 event->mmap2.min, event->mmap2.ino, 1210 event->mmap2.ino_generation, 1211 event->mmap2.prot, 1212 event->mmap2.flags, 1213 event->mmap2.filename, type, thread); 1214 1215 if (map == NULL) 1216 goto out_problem; 1217 1218 thread__insert_map(thread, map); 1219 return 0; 1220 1221 out_problem: 1222 dump_printf("problem processing PERF_RECORD_MMAP2, skipping event.\n"); 1223 return 0; 1224 } 1225 1226 int machine__process_mmap_event(struct machine *machine, union perf_event *event, 1227 struct perf_sample *sample __maybe_unused) 1228 { 1229 u8 cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK; 1230 struct thread *thread; 1231 struct map *map; 1232 enum map_type type; 1233 int ret = 0; 1234 1235 if (dump_trace) 1236 perf_event__fprintf_mmap(event, stdout); 1237 1238 if (cpumode == PERF_RECORD_MISC_GUEST_KERNEL || 1239 cpumode == PERF_RECORD_MISC_KERNEL) { 1240 ret = machine__process_kernel_mmap_event(machine, event); 1241 if (ret < 0) 1242 goto out_problem; 1243 return 0; 1244 } 1245 1246 thread = machine__findnew_thread(machine, event->mmap.pid, 1247 event->mmap.tid); 1248 if (thread == NULL) 1249 goto out_problem; 1250 1251 if (event->header.misc & PERF_RECORD_MISC_MMAP_DATA) 1252 type = MAP__VARIABLE; 1253 else 1254 type = MAP__FUNCTION; 1255 1256 map = map__new(machine, event->mmap.start, 1257 event->mmap.len, event->mmap.pgoff, 1258 event->mmap.pid, 0, 0, 0, 0, 0, 0, 1259 event->mmap.filename, 1260 type, thread); 1261 1262 if (map == NULL) 1263 goto out_problem; 1264 1265 thread__insert_map(thread, map); 1266 return 0; 1267 1268 out_problem: 1269 dump_printf("problem processing PERF_RECORD_MMAP, skipping event.\n"); 1270 return 0; 1271 } 1272 1273 void machine__remove_thread(struct machine *machine, struct thread *th) 1274 { 1275 if (machine->last_match == th) 1276 thread__zput(machine->last_match); 1277 1278 rb_erase(&th->rb_node, &machine->threads); 1279 /* 1280 * Move it first to the dead_threads list, then drop the reference, 1281 * if this is the last reference, then the thread__delete destructor 1282 * will be called and we will remove it from the dead_threads list. 1283 */ 1284 list_add_tail(&th->node, &machine->dead_threads); 1285 thread__put(th); 1286 } 1287 1288 int machine__process_fork_event(struct machine *machine, union perf_event *event, 1289 struct perf_sample *sample) 1290 { 1291 struct thread *thread = machine__find_thread(machine, 1292 event->fork.pid, 1293 event->fork.tid); 1294 struct thread *parent = machine__findnew_thread(machine, 1295 event->fork.ppid, 1296 event->fork.ptid); 1297 1298 /* if a thread currently exists for the thread id remove it */ 1299 if (thread != NULL) 1300 machine__remove_thread(machine, thread); 1301 1302 thread = machine__findnew_thread(machine, event->fork.pid, 1303 event->fork.tid); 1304 if (dump_trace) 1305 perf_event__fprintf_task(event, stdout); 1306 1307 if (thread == NULL || parent == NULL || 1308 thread__fork(thread, parent, sample->time) < 0) { 1309 dump_printf("problem processing PERF_RECORD_FORK, skipping event.\n"); 1310 return -1; 1311 } 1312 1313 return 0; 1314 } 1315 1316 int machine__process_exit_event(struct machine *machine, union perf_event *event, 1317 struct perf_sample *sample __maybe_unused) 1318 { 1319 struct thread *thread = machine__find_thread(machine, 1320 event->fork.pid, 1321 event->fork.tid); 1322 1323 if (dump_trace) 1324 perf_event__fprintf_task(event, stdout); 1325 1326 if (thread != NULL) 1327 thread__exited(thread); 1328 1329 return 0; 1330 } 1331 1332 int machine__process_event(struct machine *machine, union perf_event *event, 1333 struct perf_sample *sample) 1334 { 1335 int ret; 1336 1337 switch (event->header.type) { 1338 case PERF_RECORD_COMM: 1339 ret = machine__process_comm_event(machine, event, sample); break; 1340 case PERF_RECORD_MMAP: 1341 ret = machine__process_mmap_event(machine, event, sample); break; 1342 case PERF_RECORD_MMAP2: 1343 ret = machine__process_mmap2_event(machine, event, sample); break; 1344 case PERF_RECORD_FORK: 1345 ret = machine__process_fork_event(machine, event, sample); break; 1346 case PERF_RECORD_EXIT: 1347 ret = machine__process_exit_event(machine, event, sample); break; 1348 case PERF_RECORD_LOST: 1349 ret = machine__process_lost_event(machine, event, sample); break; 1350 case PERF_RECORD_AUX: 1351 ret = machine__process_aux_event(machine, event); break; 1352 case PERF_RECORD_ITRACE_START: 1353 ret = machine__process_itrace_start_event(machine, event); 1354 break; 1355 default: 1356 ret = -1; 1357 break; 1358 } 1359 1360 return ret; 1361 } 1362 1363 static bool symbol__match_regex(struct symbol *sym, regex_t *regex) 1364 { 1365 if (sym->name && !regexec(regex, sym->name, 0, NULL, 0)) 1366 return 1; 1367 return 0; 1368 } 1369 1370 static void ip__resolve_ams(struct thread *thread, 1371 struct addr_map_symbol *ams, 1372 u64 ip) 1373 { 1374 struct addr_location al; 1375 1376 memset(&al, 0, sizeof(al)); 1377 /* 1378 * We cannot use the header.misc hint to determine whether a 1379 * branch stack address is user, kernel, guest, hypervisor. 1380 * Branches may straddle the kernel/user/hypervisor boundaries. 1381 * Thus, we have to try consecutively until we find a match 1382 * or else, the symbol is unknown 1383 */ 1384 thread__find_cpumode_addr_location(thread, MAP__FUNCTION, ip, &al); 1385 1386 ams->addr = ip; 1387 ams->al_addr = al.addr; 1388 ams->sym = al.sym; 1389 ams->map = al.map; 1390 } 1391 1392 static void ip__resolve_data(struct thread *thread, 1393 u8 m, struct addr_map_symbol *ams, u64 addr) 1394 { 1395 struct addr_location al; 1396 1397 memset(&al, 0, sizeof(al)); 1398 1399 thread__find_addr_location(thread, m, MAP__VARIABLE, addr, &al); 1400 if (al.map == NULL) { 1401 /* 1402 * some shared data regions have execute bit set which puts 1403 * their mapping in the MAP__FUNCTION type array. 1404 * Check there as a fallback option before dropping the sample. 1405 */ 1406 thread__find_addr_location(thread, m, MAP__FUNCTION, addr, &al); 1407 } 1408 1409 ams->addr = addr; 1410 ams->al_addr = al.addr; 1411 ams->sym = al.sym; 1412 ams->map = al.map; 1413 } 1414 1415 struct mem_info *sample__resolve_mem(struct perf_sample *sample, 1416 struct addr_location *al) 1417 { 1418 struct mem_info *mi = zalloc(sizeof(*mi)); 1419 1420 if (!mi) 1421 return NULL; 1422 1423 ip__resolve_ams(al->thread, &mi->iaddr, sample->ip); 1424 ip__resolve_data(al->thread, al->cpumode, &mi->daddr, sample->addr); 1425 mi->data_src.val = sample->data_src; 1426 1427 return mi; 1428 } 1429 1430 static int add_callchain_ip(struct thread *thread, 1431 struct symbol **parent, 1432 struct addr_location *root_al, 1433 u8 *cpumode, 1434 u64 ip) 1435 { 1436 struct addr_location al; 1437 1438 al.filtered = 0; 1439 al.sym = NULL; 1440 if (!cpumode) { 1441 thread__find_cpumode_addr_location(thread, MAP__FUNCTION, 1442 ip, &al); 1443 } else { 1444 if (ip >= PERF_CONTEXT_MAX) { 1445 switch (ip) { 1446 case PERF_CONTEXT_HV: 1447 *cpumode = PERF_RECORD_MISC_HYPERVISOR; 1448 break; 1449 case PERF_CONTEXT_KERNEL: 1450 *cpumode = PERF_RECORD_MISC_KERNEL; 1451 break; 1452 case PERF_CONTEXT_USER: 1453 *cpumode = PERF_RECORD_MISC_USER; 1454 break; 1455 default: 1456 pr_debug("invalid callchain context: " 1457 "%"PRId64"\n", (s64) ip); 1458 /* 1459 * It seems the callchain is corrupted. 1460 * Discard all. 1461 */ 1462 callchain_cursor_reset(&callchain_cursor); 1463 return 1; 1464 } 1465 return 0; 1466 } 1467 thread__find_addr_location(thread, *cpumode, MAP__FUNCTION, 1468 ip, &al); 1469 } 1470 1471 if (al.sym != NULL) { 1472 if (sort__has_parent && !*parent && 1473 symbol__match_regex(al.sym, &parent_regex)) 1474 *parent = al.sym; 1475 else if (have_ignore_callees && root_al && 1476 symbol__match_regex(al.sym, &ignore_callees_regex)) { 1477 /* Treat this symbol as the root, 1478 forgetting its callees. */ 1479 *root_al = al; 1480 callchain_cursor_reset(&callchain_cursor); 1481 } 1482 } 1483 1484 return callchain_cursor_append(&callchain_cursor, al.addr, al.map, al.sym); 1485 } 1486 1487 struct branch_info *sample__resolve_bstack(struct perf_sample *sample, 1488 struct addr_location *al) 1489 { 1490 unsigned int i; 1491 const struct branch_stack *bs = sample->branch_stack; 1492 struct branch_info *bi = calloc(bs->nr, sizeof(struct branch_info)); 1493 1494 if (!bi) 1495 return NULL; 1496 1497 for (i = 0; i < bs->nr; i++) { 1498 ip__resolve_ams(al->thread, &bi[i].to, bs->entries[i].to); 1499 ip__resolve_ams(al->thread, &bi[i].from, bs->entries[i].from); 1500 bi[i].flags = bs->entries[i].flags; 1501 } 1502 return bi; 1503 } 1504 1505 #define CHASHSZ 127 1506 #define CHASHBITS 7 1507 #define NO_ENTRY 0xff 1508 1509 #define PERF_MAX_BRANCH_DEPTH 127 1510 1511 /* Remove loops. */ 1512 static int remove_loops(struct branch_entry *l, int nr) 1513 { 1514 int i, j, off; 1515 unsigned char chash[CHASHSZ]; 1516 1517 memset(chash, NO_ENTRY, sizeof(chash)); 1518 1519 BUG_ON(PERF_MAX_BRANCH_DEPTH > 255); 1520 1521 for (i = 0; i < nr; i++) { 1522 int h = hash_64(l[i].from, CHASHBITS) % CHASHSZ; 1523 1524 /* no collision handling for now */ 1525 if (chash[h] == NO_ENTRY) { 1526 chash[h] = i; 1527 } else if (l[chash[h]].from == l[i].from) { 1528 bool is_loop = true; 1529 /* check if it is a real loop */ 1530 off = 0; 1531 for (j = chash[h]; j < i && i + off < nr; j++, off++) 1532 if (l[j].from != l[i + off].from) { 1533 is_loop = false; 1534 break; 1535 } 1536 if (is_loop) { 1537 memmove(l + i, l + i + off, 1538 (nr - (i + off)) * sizeof(*l)); 1539 nr -= off; 1540 } 1541 } 1542 } 1543 return nr; 1544 } 1545 1546 /* 1547 * Recolve LBR callstack chain sample 1548 * Return: 1549 * 1 on success get LBR callchain information 1550 * 0 no available LBR callchain information, should try fp 1551 * negative error code on other errors. 1552 */ 1553 static int resolve_lbr_callchain_sample(struct thread *thread, 1554 struct perf_sample *sample, 1555 struct symbol **parent, 1556 struct addr_location *root_al, 1557 int max_stack) 1558 { 1559 struct ip_callchain *chain = sample->callchain; 1560 int chain_nr = min(max_stack, (int)chain->nr); 1561 u8 cpumode = PERF_RECORD_MISC_USER; 1562 int i, j, err; 1563 u64 ip; 1564 1565 for (i = 0; i < chain_nr; i++) { 1566 if (chain->ips[i] == PERF_CONTEXT_USER) 1567 break; 1568 } 1569 1570 /* LBR only affects the user callchain */ 1571 if (i != chain_nr) { 1572 struct branch_stack *lbr_stack = sample->branch_stack; 1573 int lbr_nr = lbr_stack->nr; 1574 /* 1575 * LBR callstack can only get user call chain. 1576 * The mix_chain_nr is kernel call chain 1577 * number plus LBR user call chain number. 1578 * i is kernel call chain number, 1579 * 1 is PERF_CONTEXT_USER, 1580 * lbr_nr + 1 is the user call chain number. 1581 * For details, please refer to the comments 1582 * in callchain__printf 1583 */ 1584 int mix_chain_nr = i + 1 + lbr_nr + 1; 1585 1586 if (mix_chain_nr > PERF_MAX_STACK_DEPTH + PERF_MAX_BRANCH_DEPTH) { 1587 pr_warning("corrupted callchain. skipping...\n"); 1588 return 0; 1589 } 1590 1591 for (j = 0; j < mix_chain_nr; j++) { 1592 if (callchain_param.order == ORDER_CALLEE) { 1593 if (j < i + 1) 1594 ip = chain->ips[j]; 1595 else if (j > i + 1) 1596 ip = lbr_stack->entries[j - i - 2].from; 1597 else 1598 ip = lbr_stack->entries[0].to; 1599 } else { 1600 if (j < lbr_nr) 1601 ip = lbr_stack->entries[lbr_nr - j - 1].from; 1602 else if (j > lbr_nr) 1603 ip = chain->ips[i + 1 - (j - lbr_nr)]; 1604 else 1605 ip = lbr_stack->entries[0].to; 1606 } 1607 1608 err = add_callchain_ip(thread, parent, root_al, &cpumode, ip); 1609 if (err) 1610 return (err < 0) ? err : 0; 1611 } 1612 return 1; 1613 } 1614 1615 return 0; 1616 } 1617 1618 static int thread__resolve_callchain_sample(struct thread *thread, 1619 struct perf_evsel *evsel, 1620 struct perf_sample *sample, 1621 struct symbol **parent, 1622 struct addr_location *root_al, 1623 int max_stack) 1624 { 1625 struct branch_stack *branch = sample->branch_stack; 1626 struct ip_callchain *chain = sample->callchain; 1627 int chain_nr = min(max_stack, (int)chain->nr); 1628 u8 cpumode = PERF_RECORD_MISC_USER; 1629 int i, j, err; 1630 int skip_idx = -1; 1631 int first_call = 0; 1632 1633 callchain_cursor_reset(&callchain_cursor); 1634 1635 if (has_branch_callstack(evsel)) { 1636 err = resolve_lbr_callchain_sample(thread, sample, parent, 1637 root_al, max_stack); 1638 if (err) 1639 return (err < 0) ? err : 0; 1640 } 1641 1642 /* 1643 * Based on DWARF debug information, some architectures skip 1644 * a callchain entry saved by the kernel. 1645 */ 1646 if (chain->nr < PERF_MAX_STACK_DEPTH) 1647 skip_idx = arch_skip_callchain_idx(thread, chain); 1648 1649 /* 1650 * Add branches to call stack for easier browsing. This gives 1651 * more context for a sample than just the callers. 1652 * 1653 * This uses individual histograms of paths compared to the 1654 * aggregated histograms the normal LBR mode uses. 1655 * 1656 * Limitations for now: 1657 * - No extra filters 1658 * - No annotations (should annotate somehow) 1659 */ 1660 1661 if (branch && callchain_param.branch_callstack) { 1662 int nr = min(max_stack, (int)branch->nr); 1663 struct branch_entry be[nr]; 1664 1665 if (branch->nr > PERF_MAX_BRANCH_DEPTH) { 1666 pr_warning("corrupted branch chain. skipping...\n"); 1667 goto check_calls; 1668 } 1669 1670 for (i = 0; i < nr; i++) { 1671 if (callchain_param.order == ORDER_CALLEE) { 1672 be[i] = branch->entries[i]; 1673 /* 1674 * Check for overlap into the callchain. 1675 * The return address is one off compared to 1676 * the branch entry. To adjust for this 1677 * assume the calling instruction is not longer 1678 * than 8 bytes. 1679 */ 1680 if (i == skip_idx || 1681 chain->ips[first_call] >= PERF_CONTEXT_MAX) 1682 first_call++; 1683 else if (be[i].from < chain->ips[first_call] && 1684 be[i].from >= chain->ips[first_call] - 8) 1685 first_call++; 1686 } else 1687 be[i] = branch->entries[branch->nr - i - 1]; 1688 } 1689 1690 nr = remove_loops(be, nr); 1691 1692 for (i = 0; i < nr; i++) { 1693 err = add_callchain_ip(thread, parent, root_al, 1694 NULL, be[i].to); 1695 if (!err) 1696 err = add_callchain_ip(thread, parent, root_al, 1697 NULL, be[i].from); 1698 if (err == -EINVAL) 1699 break; 1700 if (err) 1701 return err; 1702 } 1703 chain_nr -= nr; 1704 } 1705 1706 check_calls: 1707 if (chain->nr > PERF_MAX_STACK_DEPTH) { 1708 pr_warning("corrupted callchain. skipping...\n"); 1709 return 0; 1710 } 1711 1712 for (i = first_call; i < chain_nr; i++) { 1713 u64 ip; 1714 1715 if (callchain_param.order == ORDER_CALLEE) 1716 j = i; 1717 else 1718 j = chain->nr - i - 1; 1719 1720 #ifdef HAVE_SKIP_CALLCHAIN_IDX 1721 if (j == skip_idx) 1722 continue; 1723 #endif 1724 ip = chain->ips[j]; 1725 1726 err = add_callchain_ip(thread, parent, root_al, &cpumode, ip); 1727 1728 if (err) 1729 return (err < 0) ? err : 0; 1730 } 1731 1732 return 0; 1733 } 1734 1735 static int unwind_entry(struct unwind_entry *entry, void *arg) 1736 { 1737 struct callchain_cursor *cursor = arg; 1738 return callchain_cursor_append(cursor, entry->ip, 1739 entry->map, entry->sym); 1740 } 1741 1742 int thread__resolve_callchain(struct thread *thread, 1743 struct perf_evsel *evsel, 1744 struct perf_sample *sample, 1745 struct symbol **parent, 1746 struct addr_location *root_al, 1747 int max_stack) 1748 { 1749 int ret = thread__resolve_callchain_sample(thread, evsel, 1750 sample, parent, 1751 root_al, max_stack); 1752 if (ret) 1753 return ret; 1754 1755 /* Can we do dwarf post unwind? */ 1756 if (!((evsel->attr.sample_type & PERF_SAMPLE_REGS_USER) && 1757 (evsel->attr.sample_type & PERF_SAMPLE_STACK_USER))) 1758 return 0; 1759 1760 /* Bail out if nothing was captured. */ 1761 if ((!sample->user_regs.regs) || 1762 (!sample->user_stack.size)) 1763 return 0; 1764 1765 return unwind__get_entries(unwind_entry, &callchain_cursor, 1766 thread, sample, max_stack); 1767 1768 } 1769 1770 int machine__for_each_thread(struct machine *machine, 1771 int (*fn)(struct thread *thread, void *p), 1772 void *priv) 1773 { 1774 struct rb_node *nd; 1775 struct thread *thread; 1776 int rc = 0; 1777 1778 for (nd = rb_first(&machine->threads); nd; nd = rb_next(nd)) { 1779 thread = rb_entry(nd, struct thread, rb_node); 1780 rc = fn(thread, priv); 1781 if (rc != 0) 1782 return rc; 1783 } 1784 1785 list_for_each_entry(thread, &machine->dead_threads, node) { 1786 rc = fn(thread, priv); 1787 if (rc != 0) 1788 return rc; 1789 } 1790 return rc; 1791 } 1792 1793 int __machine__synthesize_threads(struct machine *machine, struct perf_tool *tool, 1794 struct target *target, struct thread_map *threads, 1795 perf_event__handler_t process, bool data_mmap) 1796 { 1797 if (target__has_task(target)) 1798 return perf_event__synthesize_thread_map(tool, threads, process, machine, data_mmap); 1799 else if (target__has_cpu(target)) 1800 return perf_event__synthesize_threads(tool, process, machine, data_mmap); 1801 /* command specified */ 1802 return 0; 1803 } 1804 1805 pid_t machine__get_current_tid(struct machine *machine, int cpu) 1806 { 1807 if (cpu < 0 || cpu >= MAX_NR_CPUS || !machine->current_tid) 1808 return -1; 1809 1810 return machine->current_tid[cpu]; 1811 } 1812 1813 int machine__set_current_tid(struct machine *machine, int cpu, pid_t pid, 1814 pid_t tid) 1815 { 1816 struct thread *thread; 1817 1818 if (cpu < 0) 1819 return -EINVAL; 1820 1821 if (!machine->current_tid) { 1822 int i; 1823 1824 machine->current_tid = calloc(MAX_NR_CPUS, sizeof(pid_t)); 1825 if (!machine->current_tid) 1826 return -ENOMEM; 1827 for (i = 0; i < MAX_NR_CPUS; i++) 1828 machine->current_tid[i] = -1; 1829 } 1830 1831 if (cpu >= MAX_NR_CPUS) { 1832 pr_err("Requested CPU %d too large. ", cpu); 1833 pr_err("Consider raising MAX_NR_CPUS\n"); 1834 return -EINVAL; 1835 } 1836 1837 machine->current_tid[cpu] = tid; 1838 1839 thread = machine__findnew_thread(machine, pid, tid); 1840 if (!thread) 1841 return -ENOMEM; 1842 1843 thread->cpu = cpu; 1844 1845 return 0; 1846 } 1847 1848 int machine__get_kernel_start(struct machine *machine) 1849 { 1850 struct map *map = machine__kernel_map(machine, MAP__FUNCTION); 1851 int err = 0; 1852 1853 /* 1854 * The only addresses above 2^63 are kernel addresses of a 64-bit 1855 * kernel. Note that addresses are unsigned so that on a 32-bit system 1856 * all addresses including kernel addresses are less than 2^32. In 1857 * that case (32-bit system), if the kernel mapping is unknown, all 1858 * addresses will be assumed to be in user space - see 1859 * machine__kernel_ip(). 1860 */ 1861 machine->kernel_start = 1ULL << 63; 1862 if (map) { 1863 err = map__load(map, machine->symbol_filter); 1864 if (map->start) 1865 machine->kernel_start = map->start; 1866 } 1867 return err; 1868 } 1869