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