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