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