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