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