1
2#include <dirent.h>
3#include <errno.h>
4#include <inttypes.h>
5#include <regex.h>
6#include <stdlib.h>
7#include "callchain.h"
8#include "debug.h"
9#include "dso.h"
10#include "env.h"
11#include "event.h"
12#include "evsel.h"
13#include "hist.h"
14#include "machine.h"
15#include "map.h"
16#include "map_symbol.h"
17#include "branch.h"
18#include "mem-events.h"
19#include "srcline.h"
20#include "symbol.h"
21#include "sort.h"
22#include "strlist.h"
23#include "target.h"
24#include "thread.h"
25#include "util.h"
26#include "vdso.h"
27#include <stdbool.h>
28#include <sys/types.h>
29#include <sys/stat.h>
30#include <unistd.h>
31#include "unwind.h"
32#include "linux/hash.h"
33#include "asm/bug.h"
34#include "bpf-event.h"
35#include <internal/lib.h>
36
37#include <linux/ctype.h>
38#include <symbol/kallsyms.h>
39#include <linux/mman.h>
40#include <linux/string.h>
41#include <linux/zalloc.h>
42
43static void __machine__remove_thread(struct machine *machine, struct thread *th, bool lock);
44
45static struct dso *machine__kernel_dso(struct machine *machine)
46{
47 return machine->vmlinux_map->dso;
48}
49
50static void dsos__init(struct dsos *dsos)
51{
52 INIT_LIST_HEAD(&dsos->head);
53 dsos->root = RB_ROOT;
54 init_rwsem(&dsos->lock);
55}
56
57static void machine__threads_init(struct machine *machine)
58{
59 int i;
60
61 for (i = 0; i < THREADS__TABLE_SIZE; i++) {
62 struct threads *threads = &machine->threads[i];
63 threads->entries = RB_ROOT_CACHED;
64 init_rwsem(&threads->lock);
65 threads->nr = 0;
66 INIT_LIST_HEAD(&threads->dead);
67 threads->last_match = NULL;
68 }
69}
70
71static int machine__set_mmap_name(struct machine *machine)
72{
73 if (machine__is_host(machine))
74 machine->mmap_name = strdup("[kernel.kallsyms]");
75 else if (machine__is_default_guest(machine))
76 machine->mmap_name = strdup("[guest.kernel.kallsyms]");
77 else if (asprintf(&machine->mmap_name, "[guest.kernel.kallsyms.%d]",
78 machine->pid) < 0)
79 machine->mmap_name = NULL;
80
81 return machine->mmap_name ? 0 : -ENOMEM;
82}
83
84int machine__init(struct machine *machine, const char *root_dir, pid_t pid)
85{
86 int err = -ENOMEM;
87
88 memset(machine, 0, sizeof(*machine));
89 maps__init(&machine->kmaps, machine);
90 RB_CLEAR_NODE(&machine->rb_node);
91 dsos__init(&machine->dsos);
92
93 machine__threads_init(machine);
94
95 machine->vdso_info = NULL;
96 machine->env = NULL;
97
98 machine->pid = pid;
99
100 machine->id_hdr_size = 0;
101 machine->kptr_restrict_warned = false;
102 machine->comm_exec = false;
103 machine->kernel_start = 0;
104 machine->vmlinux_map = NULL;
105
106 machine->root_dir = strdup(root_dir);
107 if (machine->root_dir == NULL)
108 return -ENOMEM;
109
110 if (machine__set_mmap_name(machine))
111 goto out;
112
113 if (pid != HOST_KERNEL_ID) {
114 struct thread *thread = machine__findnew_thread(machine, -1,
115 pid);
116 char comm[64];
117
118 if (thread == NULL)
119 goto out;
120
121 snprintf(comm, sizeof(comm), "[guest/%d]", pid);
122 thread__set_comm(thread, comm, 0);
123 thread__put(thread);
124 }
125
126 machine->current_tid = NULL;
127 err = 0;
128
129out:
130 if (err) {
131 zfree(&machine->root_dir);
132 zfree(&machine->mmap_name);
133 }
134 return 0;
135}
136
137struct machine *machine__new_host(void)
138{
139 struct machine *machine = malloc(sizeof(*machine));
140
141 if (machine != NULL) {
142 machine__init(machine, "", HOST_KERNEL_ID);
143
144 if (machine__create_kernel_maps(machine) < 0)
145 goto out_delete;
146 }
147
148 return machine;
149out_delete:
150 free(machine);
151 return NULL;
152}
153
154struct machine *machine__new_kallsyms(void)
155{
156 struct machine *machine = machine__new_host();
157
158
159
160
161
162
163 if (machine && machine__load_kallsyms(machine, "/proc/kallsyms") <= 0) {
164 machine__delete(machine);
165 machine = NULL;
166 }
167
168 return machine;
169}
170
171static void dsos__purge(struct dsos *dsos)
172{
173 struct dso *pos, *n;
174
175 down_write(&dsos->lock);
176
177 list_for_each_entry_safe(pos, n, &dsos->head, node) {
178 RB_CLEAR_NODE(&pos->rb_node);
179 pos->root = NULL;
180 list_del_init(&pos->node);
181 dso__put(pos);
182 }
183
184 up_write(&dsos->lock);
185}
186
187static void dsos__exit(struct dsos *dsos)
188{
189 dsos__purge(dsos);
190 exit_rwsem(&dsos->lock);
191}
192
193void machine__delete_threads(struct machine *machine)
194{
195 struct rb_node *nd;
196 int i;
197
198 for (i = 0; i < THREADS__TABLE_SIZE; i++) {
199 struct threads *threads = &machine->threads[i];
200 down_write(&threads->lock);
201 nd = rb_first_cached(&threads->entries);
202 while (nd) {
203 struct thread *t = rb_entry(nd, struct thread, rb_node);
204
205 nd = rb_next(nd);
206 __machine__remove_thread(machine, t, false);
207 }
208 up_write(&threads->lock);
209 }
210}
211
212void machine__exit(struct machine *machine)
213{
214 int i;
215
216 if (machine == NULL)
217 return;
218
219 machine__destroy_kernel_maps(machine);
220 maps__exit(&machine->kmaps);
221 dsos__exit(&machine->dsos);
222 machine__exit_vdso(machine);
223 zfree(&machine->root_dir);
224 zfree(&machine->mmap_name);
225 zfree(&machine->current_tid);
226
227 for (i = 0; i < THREADS__TABLE_SIZE; i++) {
228 struct threads *threads = &machine->threads[i];
229 struct thread *thread, *n;
230
231
232
233
234
235
236
237
238 list_for_each_entry_safe(thread, n, &threads->dead, node)
239 list_del_init(&thread->node);
240
241 exit_rwsem(&threads->lock);
242 }
243}
244
245void machine__delete(struct machine *machine)
246{
247 if (machine) {
248 machine__exit(machine);
249 free(machine);
250 }
251}
252
253void machines__init(struct machines *machines)
254{
255 machine__init(&machines->host, "", HOST_KERNEL_ID);
256 machines->guests = RB_ROOT_CACHED;
257}
258
259void machines__exit(struct machines *machines)
260{
261 machine__exit(&machines->host);
262
263}
264
265struct machine *machines__add(struct machines *machines, pid_t pid,
266 const char *root_dir)
267{
268 struct rb_node **p = &machines->guests.rb_root.rb_node;
269 struct rb_node *parent = NULL;
270 struct machine *pos, *machine = malloc(sizeof(*machine));
271 bool leftmost = true;
272
273 if (machine == NULL)
274 return NULL;
275
276 if (machine__init(machine, root_dir, pid) != 0) {
277 free(machine);
278 return NULL;
279 }
280
281 while (*p != NULL) {
282 parent = *p;
283 pos = rb_entry(parent, struct machine, rb_node);
284 if (pid < pos->pid)
285 p = &(*p)->rb_left;
286 else {
287 p = &(*p)->rb_right;
288 leftmost = false;
289 }
290 }
291
292 rb_link_node(&machine->rb_node, parent, p);
293 rb_insert_color_cached(&machine->rb_node, &machines->guests, leftmost);
294
295 return machine;
296}
297
298void machines__set_comm_exec(struct machines *machines, bool comm_exec)
299{
300 struct rb_node *nd;
301
302 machines->host.comm_exec = comm_exec;
303
304 for (nd = rb_first_cached(&machines->guests); nd; nd = rb_next(nd)) {
305 struct machine *machine = rb_entry(nd, struct machine, rb_node);
306
307 machine->comm_exec = comm_exec;
308 }
309}
310
311struct machine *machines__find(struct machines *machines, pid_t pid)
312{
313 struct rb_node **p = &machines->guests.rb_root.rb_node;
314 struct rb_node *parent = NULL;
315 struct machine *machine;
316 struct machine *default_machine = NULL;
317
318 if (pid == HOST_KERNEL_ID)
319 return &machines->host;
320
321 while (*p != NULL) {
322 parent = *p;
323 machine = rb_entry(parent, struct machine, rb_node);
324 if (pid < machine->pid)
325 p = &(*p)->rb_left;
326 else if (pid > machine->pid)
327 p = &(*p)->rb_right;
328 else
329 return machine;
330 if (!machine->pid)
331 default_machine = machine;
332 }
333
334 return default_machine;
335}
336
337struct machine *machines__findnew(struct machines *machines, pid_t pid)
338{
339 char path[PATH_MAX];
340 const char *root_dir = "";
341 struct machine *machine = machines__find(machines, pid);
342
343 if (machine && (machine->pid == pid))
344 goto out;
345
346 if ((pid != HOST_KERNEL_ID) &&
347 (pid != DEFAULT_GUEST_KERNEL_ID) &&
348 (symbol_conf.guestmount)) {
349 sprintf(path, "%s/%d", symbol_conf.guestmount, pid);
350 if (access(path, R_OK)) {
351 static struct strlist *seen;
352
353 if (!seen)
354 seen = strlist__new(NULL, NULL);
355
356 if (!strlist__has_entry(seen, path)) {
357 pr_err("Can't access file %s\n", path);
358 strlist__add(seen, path);
359 }
360 machine = NULL;
361 goto out;
362 }
363 root_dir = path;
364 }
365
366 machine = machines__add(machines, pid, root_dir);
367out:
368 return machine;
369}
370
371void machines__process_guests(struct machines *machines,
372 machine__process_t process, void *data)
373{
374 struct rb_node *nd;
375
376 for (nd = rb_first_cached(&machines->guests); nd; nd = rb_next(nd)) {
377 struct machine *pos = rb_entry(nd, struct machine, rb_node);
378 process(pos, data);
379 }
380}
381
382void machines__set_id_hdr_size(struct machines *machines, u16 id_hdr_size)
383{
384 struct rb_node *node;
385 struct machine *machine;
386
387 machines->host.id_hdr_size = id_hdr_size;
388
389 for (node = rb_first_cached(&machines->guests); node;
390 node = rb_next(node)) {
391 machine = rb_entry(node, struct machine, rb_node);
392 machine->id_hdr_size = id_hdr_size;
393 }
394
395 return;
396}
397
398static void machine__update_thread_pid(struct machine *machine,
399 struct thread *th, pid_t pid)
400{
401 struct thread *leader;
402
403 if (pid == th->pid_ || pid == -1 || th->pid_ != -1)
404 return;
405
406 th->pid_ = pid;
407
408 if (th->pid_ == th->tid)
409 return;
410
411 leader = __machine__findnew_thread(machine, th->pid_, th->pid_);
412 if (!leader)
413 goto out_err;
414
415 if (!leader->maps)
416 leader->maps = maps__new(machine);
417
418 if (!leader->maps)
419 goto out_err;
420
421 if (th->maps == leader->maps)
422 return;
423
424 if (th->maps) {
425
426
427
428
429
430 if (!maps__empty(th->maps))
431 pr_err("Discarding thread maps for %d:%d\n",
432 th->pid_, th->tid);
433 maps__put(th->maps);
434 }
435
436 th->maps = maps__get(leader->maps);
437out_put:
438 thread__put(leader);
439 return;
440out_err:
441 pr_err("Failed to join map groups for %d:%d\n", th->pid_, th->tid);
442 goto out_put;
443}
444
445
446
447
448
449
450static struct thread*
451__threads__get_last_match(struct threads *threads, struct machine *machine,
452 int pid, int tid)
453{
454 struct thread *th;
455
456 th = threads->last_match;
457 if (th != NULL) {
458 if (th->tid == tid) {
459 machine__update_thread_pid(machine, th, pid);
460 return thread__get(th);
461 }
462
463 threads->last_match = NULL;
464 }
465
466 return NULL;
467}
468
469static struct thread*
470threads__get_last_match(struct threads *threads, struct machine *machine,
471 int pid, int tid)
472{
473 struct thread *th = NULL;
474
475 if (perf_singlethreaded)
476 th = __threads__get_last_match(threads, machine, pid, tid);
477
478 return th;
479}
480
481static void
482__threads__set_last_match(struct threads *threads, struct thread *th)
483{
484 threads->last_match = th;
485}
486
487static void
488threads__set_last_match(struct threads *threads, struct thread *th)
489{
490 if (perf_singlethreaded)
491 __threads__set_last_match(threads, th);
492}
493
494
495
496
497
498static struct thread *____machine__findnew_thread(struct machine *machine,
499 struct threads *threads,
500 pid_t pid, pid_t tid,
501 bool create)
502{
503 struct rb_node **p = &threads->entries.rb_root.rb_node;
504 struct rb_node *parent = NULL;
505 struct thread *th;
506 bool leftmost = true;
507
508 th = threads__get_last_match(threads, machine, pid, tid);
509 if (th)
510 return th;
511
512 while (*p != NULL) {
513 parent = *p;
514 th = rb_entry(parent, struct thread, rb_node);
515
516 if (th->tid == tid) {
517 threads__set_last_match(threads, th);
518 machine__update_thread_pid(machine, th, pid);
519 return thread__get(th);
520 }
521
522 if (tid < th->tid)
523 p = &(*p)->rb_left;
524 else {
525 p = &(*p)->rb_right;
526 leftmost = false;
527 }
528 }
529
530 if (!create)
531 return NULL;
532
533 th = thread__new(pid, tid);
534 if (th != NULL) {
535 rb_link_node(&th->rb_node, parent, p);
536 rb_insert_color_cached(&th->rb_node, &threads->entries, leftmost);
537
538
539
540
541
542
543
544
545 if (thread__init_maps(th, machine)) {
546 rb_erase_cached(&th->rb_node, &threads->entries);
547 RB_CLEAR_NODE(&th->rb_node);
548 thread__put(th);
549 return NULL;
550 }
551
552
553
554 thread__get(th);
555 threads__set_last_match(threads, th);
556 ++threads->nr;
557 }
558
559 return th;
560}
561
562struct thread *__machine__findnew_thread(struct machine *machine, pid_t pid, pid_t tid)
563{
564 return ____machine__findnew_thread(machine, machine__threads(machine, tid), pid, tid, true);
565}
566
567struct thread *machine__findnew_thread(struct machine *machine, pid_t pid,
568 pid_t tid)
569{
570 struct threads *threads = machine__threads(machine, tid);
571 struct thread *th;
572
573 down_write(&threads->lock);
574 th = __machine__findnew_thread(machine, pid, tid);
575 up_write(&threads->lock);
576 return th;
577}
578
579struct thread *machine__find_thread(struct machine *machine, pid_t pid,
580 pid_t tid)
581{
582 struct threads *threads = machine__threads(machine, tid);
583 struct thread *th;
584
585 down_read(&threads->lock);
586 th = ____machine__findnew_thread(machine, threads, pid, tid, false);
587 up_read(&threads->lock);
588 return th;
589}
590
591struct comm *machine__thread_exec_comm(struct machine *machine,
592 struct thread *thread)
593{
594 if (machine->comm_exec)
595 return thread__exec_comm(thread);
596 else
597 return thread__comm(thread);
598}
599
600int machine__process_comm_event(struct machine *machine, union perf_event *event,
601 struct perf_sample *sample)
602{
603 struct thread *thread = machine__findnew_thread(machine,
604 event->comm.pid,
605 event->comm.tid);
606 bool exec = event->header.misc & PERF_RECORD_MISC_COMM_EXEC;
607 int err = 0;
608
609 if (exec)
610 machine->comm_exec = true;
611
612 if (dump_trace)
613 perf_event__fprintf_comm(event, stdout);
614
615 if (thread == NULL ||
616 __thread__set_comm(thread, event->comm.comm, sample->time, exec)) {
617 dump_printf("problem processing PERF_RECORD_COMM, skipping event.\n");
618 err = -1;
619 }
620
621 thread__put(thread);
622
623 return err;
624}
625
626int machine__process_namespaces_event(struct machine *machine __maybe_unused,
627 union perf_event *event,
628 struct perf_sample *sample __maybe_unused)
629{
630 struct thread *thread = machine__findnew_thread(machine,
631 event->namespaces.pid,
632 event->namespaces.tid);
633 int err = 0;
634
635 WARN_ONCE(event->namespaces.nr_namespaces > NR_NAMESPACES,
636 "\nWARNING: kernel seems to support more namespaces than perf"
637 " tool.\nTry updating the perf tool..\n\n");
638
639 WARN_ONCE(event->namespaces.nr_namespaces < NR_NAMESPACES,
640 "\nWARNING: perf tool seems to support more namespaces than"
641 " the kernel.\nTry updating the kernel..\n\n");
642
643 if (dump_trace)
644 perf_event__fprintf_namespaces(event, stdout);
645
646 if (thread == NULL ||
647 thread__set_namespaces(thread, sample->time, &event->namespaces)) {
648 dump_printf("problem processing PERF_RECORD_NAMESPACES, skipping event.\n");
649 err = -1;
650 }
651
652 thread__put(thread);
653
654 return err;
655}
656
657int machine__process_lost_event(struct machine *machine __maybe_unused,
658 union perf_event *event, struct perf_sample *sample __maybe_unused)
659{
660 dump_printf(": id:%" PRI_lu64 ": lost:%" PRI_lu64 "\n",
661 event->lost.id, event->lost.lost);
662 return 0;
663}
664
665int machine__process_lost_samples_event(struct machine *machine __maybe_unused,
666 union perf_event *event, struct perf_sample *sample)
667{
668 dump_printf(": id:%" PRIu64 ": lost samples :%" PRI_lu64 "\n",
669 sample->id, event->lost_samples.lost);
670 return 0;
671}
672
673static struct dso *machine__findnew_module_dso(struct machine *machine,
674 struct kmod_path *m,
675 const char *filename)
676{
677 struct dso *dso;
678
679 down_write(&machine->dsos.lock);
680
681 dso = __dsos__find(&machine->dsos, m->name, true);
682 if (!dso) {
683 dso = __dsos__addnew(&machine->dsos, m->name);
684 if (dso == NULL)
685 goto out_unlock;
686
687 dso__set_module_info(dso, m, machine);
688 dso__set_long_name(dso, strdup(filename), true);
689 dso->kernel = DSO_TYPE_KERNEL;
690 }
691
692 dso__get(dso);
693out_unlock:
694 up_write(&machine->dsos.lock);
695 return dso;
696}
697
698int machine__process_aux_event(struct machine *machine __maybe_unused,
699 union perf_event *event)
700{
701 if (dump_trace)
702 perf_event__fprintf_aux(event, stdout);
703 return 0;
704}
705
706int machine__process_itrace_start_event(struct machine *machine __maybe_unused,
707 union perf_event *event)
708{
709 if (dump_trace)
710 perf_event__fprintf_itrace_start(event, stdout);
711 return 0;
712}
713
714int machine__process_switch_event(struct machine *machine __maybe_unused,
715 union perf_event *event)
716{
717 if (dump_trace)
718 perf_event__fprintf_switch(event, stdout);
719 return 0;
720}
721
722static int machine__process_ksymbol_register(struct machine *machine,
723 union perf_event *event,
724 struct perf_sample *sample __maybe_unused)
725{
726 struct symbol *sym;
727 struct map *map = maps__find(&machine->kmaps, event->ksymbol.addr);
728
729 if (!map) {
730 struct dso *dso = dso__new(event->ksymbol.name);
731
732 if (dso) {
733 dso->kernel = DSO_TYPE_KERNEL;
734 map = map__new2(0, dso);
735 }
736
737 if (!dso || !map) {
738 dso__put(dso);
739 return -ENOMEM;
740 }
741
742 map->start = event->ksymbol.addr;
743 map->end = map->start + event->ksymbol.len;
744 maps__insert(&machine->kmaps, map);
745 }
746
747 sym = symbol__new(map->map_ip(map, map->start),
748 event->ksymbol.len,
749 0, 0, event->ksymbol.name);
750 if (!sym)
751 return -ENOMEM;
752 dso__insert_symbol(map->dso, sym);
753 return 0;
754}
755
756static int machine__process_ksymbol_unregister(struct machine *machine,
757 union perf_event *event,
758 struct perf_sample *sample __maybe_unused)
759{
760 struct map *map;
761
762 map = maps__find(&machine->kmaps, event->ksymbol.addr);
763 if (map)
764 maps__remove(&machine->kmaps, map);
765
766 return 0;
767}
768
769int machine__process_ksymbol(struct machine *machine __maybe_unused,
770 union perf_event *event,
771 struct perf_sample *sample)
772{
773 if (dump_trace)
774 perf_event__fprintf_ksymbol(event, stdout);
775
776 if (event->ksymbol.flags & PERF_RECORD_KSYMBOL_FLAGS_UNREGISTER)
777 return machine__process_ksymbol_unregister(machine, event,
778 sample);
779 return machine__process_ksymbol_register(machine, event, sample);
780}
781
782static struct map *machine__addnew_module_map(struct machine *machine, u64 start,
783 const char *filename)
784{
785 struct map *map = NULL;
786 struct kmod_path m;
787 struct dso *dso;
788
789 if (kmod_path__parse_name(&m, filename))
790 return NULL;
791
792 dso = machine__findnew_module_dso(machine, &m, filename);
793 if (dso == NULL)
794 goto out;
795
796 map = map__new2(start, dso);
797 if (map == NULL)
798 goto out;
799
800 maps__insert(&machine->kmaps, map);
801
802
803 map__put(map);
804out:
805
806 dso__put(dso);
807 zfree(&m.name);
808 return map;
809}
810
811size_t machines__fprintf_dsos(struct machines *machines, FILE *fp)
812{
813 struct rb_node *nd;
814 size_t ret = __dsos__fprintf(&machines->host.dsos.head, fp);
815
816 for (nd = rb_first_cached(&machines->guests); nd; nd = rb_next(nd)) {
817 struct machine *pos = rb_entry(nd, struct machine, rb_node);
818 ret += __dsos__fprintf(&pos->dsos.head, fp);
819 }
820
821 return ret;
822}
823
824size_t machine__fprintf_dsos_buildid(struct machine *m, FILE *fp,
825 bool (skip)(struct dso *dso, int parm), int parm)
826{
827 return __dsos__fprintf_buildid(&m->dsos.head, fp, skip, parm);
828}
829
830size_t machines__fprintf_dsos_buildid(struct machines *machines, FILE *fp,
831 bool (skip)(struct dso *dso, int parm), int parm)
832{
833 struct rb_node *nd;
834 size_t ret = machine__fprintf_dsos_buildid(&machines->host, fp, skip, parm);
835
836 for (nd = rb_first_cached(&machines->guests); nd; nd = rb_next(nd)) {
837 struct machine *pos = rb_entry(nd, struct machine, rb_node);
838 ret += machine__fprintf_dsos_buildid(pos, fp, skip, parm);
839 }
840 return ret;
841}
842
843size_t machine__fprintf_vmlinux_path(struct machine *machine, FILE *fp)
844{
845 int i;
846 size_t printed = 0;
847 struct dso *kdso = machine__kernel_dso(machine);
848
849 if (kdso->has_build_id) {
850 char filename[PATH_MAX];
851 if (dso__build_id_filename(kdso, filename, sizeof(filename),
852 false))
853 printed += fprintf(fp, "[0] %s\n", filename);
854 }
855
856 for (i = 0; i < vmlinux_path__nr_entries; ++i)
857 printed += fprintf(fp, "[%d] %s\n",
858 i + kdso->has_build_id, vmlinux_path[i]);
859
860 return printed;
861}
862
863size_t machine__fprintf(struct machine *machine, FILE *fp)
864{
865 struct rb_node *nd;
866 size_t ret;
867 int i;
868
869 for (i = 0; i < THREADS__TABLE_SIZE; i++) {
870 struct threads *threads = &machine->threads[i];
871
872 down_read(&threads->lock);
873
874 ret = fprintf(fp, "Threads: %u\n", threads->nr);
875
876 for (nd = rb_first_cached(&threads->entries); nd;
877 nd = rb_next(nd)) {
878 struct thread *pos = rb_entry(nd, struct thread, rb_node);
879
880 ret += thread__fprintf(pos, fp);
881 }
882
883 up_read(&threads->lock);
884 }
885 return ret;
886}
887
888static struct dso *machine__get_kernel(struct machine *machine)
889{
890 const char *vmlinux_name = machine->mmap_name;
891 struct dso *kernel;
892
893 if (machine__is_host(machine)) {
894 if (symbol_conf.vmlinux_name)
895 vmlinux_name = symbol_conf.vmlinux_name;
896
897 kernel = machine__findnew_kernel(machine, vmlinux_name,
898 "[kernel]", DSO_TYPE_KERNEL);
899 } else {
900 if (symbol_conf.default_guest_vmlinux_name)
901 vmlinux_name = symbol_conf.default_guest_vmlinux_name;
902
903 kernel = machine__findnew_kernel(machine, vmlinux_name,
904 "[guest.kernel]",
905 DSO_TYPE_GUEST_KERNEL);
906 }
907
908 if (kernel != NULL && (!kernel->has_build_id))
909 dso__read_running_kernel_build_id(kernel, machine);
910
911 return kernel;
912}
913
914struct process_args {
915 u64 start;
916};
917
918void machine__get_kallsyms_filename(struct machine *machine, char *buf,
919 size_t bufsz)
920{
921 if (machine__is_default_guest(machine))
922 scnprintf(buf, bufsz, "%s", symbol_conf.default_guest_kallsyms);
923 else
924 scnprintf(buf, bufsz, "%s/proc/kallsyms", machine->root_dir);
925}
926
927const char *ref_reloc_sym_names[] = {"_text", "_stext", NULL};
928
929
930
931
932
933static int machine__get_running_kernel_start(struct machine *machine,
934 const char **symbol_name,
935 u64 *start, u64 *end)
936{
937 char filename[PATH_MAX];
938 int i, err = -1;
939 const char *name;
940 u64 addr = 0;
941
942 machine__get_kallsyms_filename(machine, filename, PATH_MAX);
943
944 if (symbol__restricted_filename(filename, "/proc/kallsyms"))
945 return 0;
946
947 for (i = 0; (name = ref_reloc_sym_names[i]) != NULL; i++) {
948 err = kallsyms__get_function_start(filename, name, &addr);
949 if (!err)
950 break;
951 }
952
953 if (err)
954 return -1;
955
956 if (symbol_name)
957 *symbol_name = name;
958
959 *start = addr;
960
961 err = kallsyms__get_function_start(filename, "_etext", &addr);
962 if (!err)
963 *end = addr;
964
965 return 0;
966}
967
968int machine__create_extra_kernel_map(struct machine *machine,
969 struct dso *kernel,
970 struct extra_kernel_map *xm)
971{
972 struct kmap *kmap;
973 struct map *map;
974
975 map = map__new2(xm->start, kernel);
976 if (!map)
977 return -1;
978
979 map->end = xm->end;
980 map->pgoff = xm->pgoff;
981
982 kmap = map__kmap(map);
983
984 strlcpy(kmap->name, xm->name, KMAP_NAME_LEN);
985
986 maps__insert(&machine->kmaps, map);
987
988 pr_debug2("Added extra kernel map %s %" PRIx64 "-%" PRIx64 "\n",
989 kmap->name, map->start, map->end);
990
991 map__put(map);
992
993 return 0;
994}
995
996static u64 find_entry_trampoline(struct dso *dso)
997{
998
999 const char *syms[] = {
1000 "_entry_trampoline",
1001 "__entry_trampoline_start",
1002 "entry_SYSCALL_64_trampoline",
1003 };
1004 struct symbol *sym = dso__first_symbol(dso);
1005 unsigned int i;
1006
1007 for (; sym; sym = dso__next_symbol(sym)) {
1008 if (sym->binding != STB_GLOBAL)
1009 continue;
1010 for (i = 0; i < ARRAY_SIZE(syms); i++) {
1011 if (!strcmp(sym->name, syms[i]))
1012 return sym->start;
1013 }
1014 }
1015
1016 return 0;
1017}
1018
1019
1020
1021
1022
1023#define X86_64_CPU_ENTRY_AREA_PER_CPU 0xfffffe0000000000ULL
1024#define X86_64_CPU_ENTRY_AREA_SIZE 0x2c000
1025#define X86_64_ENTRY_TRAMPOLINE 0x6000
1026
1027
1028int machine__map_x86_64_entry_trampolines(struct machine *machine,
1029 struct dso *kernel)
1030{
1031 struct maps *kmaps = &machine->kmaps;
1032 int nr_cpus_avail, cpu;
1033 bool found = false;
1034 struct map *map;
1035 u64 pgoff;
1036
1037
1038
1039
1040
1041 maps__for_each_entry(kmaps, map) {
1042 struct kmap *kmap = __map__kmap(map);
1043 struct map *dest_map;
1044
1045 if (!kmap || !is_entry_trampoline(kmap->name))
1046 continue;
1047
1048 dest_map = maps__find(kmaps, map->pgoff);
1049 if (dest_map != map)
1050 map->pgoff = dest_map->map_ip(dest_map, map->pgoff);
1051 found = true;
1052 }
1053 if (found || machine->trampolines_mapped)
1054 return 0;
1055
1056 pgoff = find_entry_trampoline(kernel);
1057 if (!pgoff)
1058 return 0;
1059
1060 nr_cpus_avail = machine__nr_cpus_avail(machine);
1061
1062
1063 for (cpu = 0; cpu < nr_cpus_avail; cpu++) {
1064 u64 va = X86_64_CPU_ENTRY_AREA_PER_CPU +
1065 cpu * X86_64_CPU_ENTRY_AREA_SIZE +
1066 X86_64_ENTRY_TRAMPOLINE;
1067 struct extra_kernel_map xm = {
1068 .start = va,
1069 .end = va + page_size,
1070 .pgoff = pgoff,
1071 };
1072
1073 strlcpy(xm.name, ENTRY_TRAMPOLINE_NAME, KMAP_NAME_LEN);
1074
1075 if (machine__create_extra_kernel_map(machine, kernel, &xm) < 0)
1076 return -1;
1077 }
1078
1079 machine->trampolines_mapped = nr_cpus_avail;
1080
1081 return 0;
1082}
1083
1084int __weak machine__create_extra_kernel_maps(struct machine *machine __maybe_unused,
1085 struct dso *kernel __maybe_unused)
1086{
1087 return 0;
1088}
1089
1090static int
1091__machine__create_kernel_maps(struct machine *machine, struct dso *kernel)
1092{
1093
1094 machine__destroy_kernel_maps(machine);
1095
1096 machine->vmlinux_map = map__new2(0, kernel);
1097 if (machine->vmlinux_map == NULL)
1098 return -1;
1099
1100 machine->vmlinux_map->map_ip = machine->vmlinux_map->unmap_ip = identity__map_ip;
1101 maps__insert(&machine->kmaps, machine->vmlinux_map);
1102 return 0;
1103}
1104
1105void machine__destroy_kernel_maps(struct machine *machine)
1106{
1107 struct kmap *kmap;
1108 struct map *map = machine__kernel_map(machine);
1109
1110 if (map == NULL)
1111 return;
1112
1113 kmap = map__kmap(map);
1114 maps__remove(&machine->kmaps, map);
1115 if (kmap && kmap->ref_reloc_sym) {
1116 zfree((char **)&kmap->ref_reloc_sym->name);
1117 zfree(&kmap->ref_reloc_sym);
1118 }
1119
1120 map__zput(machine->vmlinux_map);
1121}
1122
1123int machines__create_guest_kernel_maps(struct machines *machines)
1124{
1125 int ret = 0;
1126 struct dirent **namelist = NULL;
1127 int i, items = 0;
1128 char path[PATH_MAX];
1129 pid_t pid;
1130 char *endp;
1131
1132 if (symbol_conf.default_guest_vmlinux_name ||
1133 symbol_conf.default_guest_modules ||
1134 symbol_conf.default_guest_kallsyms) {
1135 machines__create_kernel_maps(machines, DEFAULT_GUEST_KERNEL_ID);
1136 }
1137
1138 if (symbol_conf.guestmount) {
1139 items = scandir(symbol_conf.guestmount, &namelist, NULL, NULL);
1140 if (items <= 0)
1141 return -ENOENT;
1142 for (i = 0; i < items; i++) {
1143 if (!isdigit(namelist[i]->d_name[0])) {
1144
1145 continue;
1146 }
1147 pid = (pid_t)strtol(namelist[i]->d_name, &endp, 10);
1148 if ((*endp != '\0') ||
1149 (endp == namelist[i]->d_name) ||
1150 (errno == ERANGE)) {
1151 pr_debug("invalid directory (%s). Skipping.\n",
1152 namelist[i]->d_name);
1153 continue;
1154 }
1155 sprintf(path, "%s/%s/proc/kallsyms",
1156 symbol_conf.guestmount,
1157 namelist[i]->d_name);
1158 ret = access(path, R_OK);
1159 if (ret) {
1160 pr_debug("Can't access file %s\n", path);
1161 goto failure;
1162 }
1163 machines__create_kernel_maps(machines, pid);
1164 }
1165failure:
1166 free(namelist);
1167 }
1168
1169 return ret;
1170}
1171
1172void machines__destroy_kernel_maps(struct machines *machines)
1173{
1174 struct rb_node *next = rb_first_cached(&machines->guests);
1175
1176 machine__destroy_kernel_maps(&machines->host);
1177
1178 while (next) {
1179 struct machine *pos = rb_entry(next, struct machine, rb_node);
1180
1181 next = rb_next(&pos->rb_node);
1182 rb_erase_cached(&pos->rb_node, &machines->guests);
1183 machine__delete(pos);
1184 }
1185}
1186
1187int machines__create_kernel_maps(struct machines *machines, pid_t pid)
1188{
1189 struct machine *machine = machines__findnew(machines, pid);
1190
1191 if (machine == NULL)
1192 return -1;
1193
1194 return machine__create_kernel_maps(machine);
1195}
1196
1197int machine__load_kallsyms(struct machine *machine, const char *filename)
1198{
1199 struct map *map = machine__kernel_map(machine);
1200 int ret = __dso__load_kallsyms(map->dso, filename, map, true);
1201
1202 if (ret > 0) {
1203 dso__set_loaded(map->dso);
1204
1205
1206
1207
1208
1209 maps__fixup_end(&machine->kmaps);
1210 }
1211
1212 return ret;
1213}
1214
1215int machine__load_vmlinux_path(struct machine *machine)
1216{
1217 struct map *map = machine__kernel_map(machine);
1218 int ret = dso__load_vmlinux_path(map->dso, map);
1219
1220 if (ret > 0)
1221 dso__set_loaded(map->dso);
1222
1223 return ret;
1224}
1225
1226static char *get_kernel_version(const char *root_dir)
1227{
1228 char version[PATH_MAX];
1229 FILE *file;
1230 char *name, *tmp;
1231 const char *prefix = "Linux version ";
1232
1233 sprintf(version, "%s/proc/version", root_dir);
1234 file = fopen(version, "r");
1235 if (!file)
1236 return NULL;
1237
1238 tmp = fgets(version, sizeof(version), file);
1239 fclose(file);
1240 if (!tmp)
1241 return NULL;
1242
1243 name = strstr(version, prefix);
1244 if (!name)
1245 return NULL;
1246 name += strlen(prefix);
1247 tmp = strchr(name, ' ');
1248 if (tmp)
1249 *tmp = '\0';
1250
1251 return strdup(name);
1252}
1253
1254static bool is_kmod_dso(struct dso *dso)
1255{
1256 return dso->symtab_type == DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE ||
1257 dso->symtab_type == DSO_BINARY_TYPE__GUEST_KMODULE;
1258}
1259
1260static int maps__set_module_path(struct maps *maps, const char *path, struct kmod_path *m)
1261{
1262 char *long_name;
1263 struct map *map = maps__find_by_name(maps, m->name);
1264
1265 if (map == NULL)
1266 return 0;
1267
1268 long_name = strdup(path);
1269 if (long_name == NULL)
1270 return -ENOMEM;
1271
1272 dso__set_long_name(map->dso, long_name, true);
1273 dso__kernel_module_get_build_id(map->dso, "");
1274
1275
1276
1277
1278
1279 if (m->comp && is_kmod_dso(map->dso)) {
1280 map->dso->symtab_type++;
1281 map->dso->comp = m->comp;
1282 }
1283
1284 return 0;
1285}
1286
1287static int maps__set_modules_path_dir(struct maps *maps, const char *dir_name, int depth)
1288{
1289 struct dirent *dent;
1290 DIR *dir = opendir(dir_name);
1291 int ret = 0;
1292
1293 if (!dir) {
1294 pr_debug("%s: cannot open %s dir\n", __func__, dir_name);
1295 return -1;
1296 }
1297
1298 while ((dent = readdir(dir)) != NULL) {
1299 char path[PATH_MAX];
1300 struct stat st;
1301
1302
1303 snprintf(path, sizeof(path), "%s/%s", dir_name, dent->d_name);
1304 if (stat(path, &st))
1305 continue;
1306
1307 if (S_ISDIR(st.st_mode)) {
1308 if (!strcmp(dent->d_name, ".") ||
1309 !strcmp(dent->d_name, ".."))
1310 continue;
1311
1312
1313 if (depth == 0) {
1314 if (!strcmp(dent->d_name, "source") ||
1315 !strcmp(dent->d_name, "build"))
1316 continue;
1317 }
1318
1319 ret = maps__set_modules_path_dir(maps, path, depth + 1);
1320 if (ret < 0)
1321 goto out;
1322 } else {
1323 struct kmod_path m;
1324
1325 ret = kmod_path__parse_name(&m, dent->d_name);
1326 if (ret)
1327 goto out;
1328
1329 if (m.kmod)
1330 ret = maps__set_module_path(maps, path, &m);
1331
1332 zfree(&m.name);
1333
1334 if (ret)
1335 goto out;
1336 }
1337 }
1338
1339out:
1340 closedir(dir);
1341 return ret;
1342}
1343
1344static int machine__set_modules_path(struct machine *machine)
1345{
1346 char *version;
1347 char modules_path[PATH_MAX];
1348
1349 version = get_kernel_version(machine->root_dir);
1350 if (!version)
1351 return -1;
1352
1353 snprintf(modules_path, sizeof(modules_path), "%s/lib/modules/%s",
1354 machine->root_dir, version);
1355 free(version);
1356
1357 return maps__set_modules_path_dir(&machine->kmaps, modules_path, 0);
1358}
1359int __weak arch__fix_module_text_start(u64 *start __maybe_unused,
1360 u64 *size __maybe_unused,
1361 const char *name __maybe_unused)
1362{
1363 return 0;
1364}
1365
1366static int machine__create_module(void *arg, const char *name, u64 start,
1367 u64 size)
1368{
1369 struct machine *machine = arg;
1370 struct map *map;
1371
1372 if (arch__fix_module_text_start(&start, &size, name) < 0)
1373 return -1;
1374
1375 map = machine__addnew_module_map(machine, start, name);
1376 if (map == NULL)
1377 return -1;
1378 map->end = start + size;
1379
1380 dso__kernel_module_get_build_id(map->dso, machine->root_dir);
1381
1382 return 0;
1383}
1384
1385static int machine__create_modules(struct machine *machine)
1386{
1387 const char *modules;
1388 char path[PATH_MAX];
1389
1390 if (machine__is_default_guest(machine)) {
1391 modules = symbol_conf.default_guest_modules;
1392 } else {
1393 snprintf(path, PATH_MAX, "%s/proc/modules", machine->root_dir);
1394 modules = path;
1395 }
1396
1397 if (symbol__restricted_filename(modules, "/proc/modules"))
1398 return -1;
1399
1400 if (modules__parse(modules, machine, machine__create_module))
1401 return -1;
1402
1403 if (!machine__set_modules_path(machine))
1404 return 0;
1405
1406 pr_debug("Problems setting modules path maps, continuing anyway...\n");
1407
1408 return 0;
1409}
1410
1411static void machine__set_kernel_mmap(struct machine *machine,
1412 u64 start, u64 end)
1413{
1414 machine->vmlinux_map->start = start;
1415 machine->vmlinux_map->end = end;
1416
1417
1418
1419
1420 if (start == 0 && end == 0)
1421 machine->vmlinux_map->end = ~0ULL;
1422}
1423
1424static void machine__update_kernel_mmap(struct machine *machine,
1425 u64 start, u64 end)
1426{
1427 struct map *map = machine__kernel_map(machine);
1428
1429 map__get(map);
1430 maps__remove(&machine->kmaps, map);
1431
1432 machine__set_kernel_mmap(machine, start, end);
1433
1434 maps__insert(&machine->kmaps, map);
1435 map__put(map);
1436}
1437
1438int machine__create_kernel_maps(struct machine *machine)
1439{
1440 struct dso *kernel = machine__get_kernel(machine);
1441 const char *name = NULL;
1442 struct map *map;
1443 u64 start = 0, end = ~0ULL;
1444 int ret;
1445
1446 if (kernel == NULL)
1447 return -1;
1448
1449 ret = __machine__create_kernel_maps(machine, kernel);
1450 if (ret < 0)
1451 goto out_put;
1452
1453 if (symbol_conf.use_modules && machine__create_modules(machine) < 0) {
1454 if (machine__is_host(machine))
1455 pr_debug("Problems creating module maps, "
1456 "continuing anyway...\n");
1457 else
1458 pr_debug("Problems creating module maps for guest %d, "
1459 "continuing anyway...\n", machine->pid);
1460 }
1461
1462 if (!machine__get_running_kernel_start(machine, &name, &start, &end)) {
1463 if (name &&
1464 map__set_kallsyms_ref_reloc_sym(machine->vmlinux_map, name, start)) {
1465 machine__destroy_kernel_maps(machine);
1466 ret = -1;
1467 goto out_put;
1468 }
1469
1470
1471
1472
1473
1474 machine__update_kernel_mmap(machine, start, end);
1475 }
1476
1477 if (machine__create_extra_kernel_maps(machine, kernel))
1478 pr_debug("Problems creating extra kernel maps, continuing anyway...\n");
1479
1480 if (end == ~0ULL) {
1481
1482 map = map__next(machine__kernel_map(machine));
1483 if (map)
1484 machine__set_kernel_mmap(machine, start, map->start);
1485 }
1486
1487out_put:
1488 dso__put(kernel);
1489 return ret;
1490}
1491
1492static bool machine__uses_kcore(struct machine *machine)
1493{
1494 struct dso *dso;
1495
1496 list_for_each_entry(dso, &machine->dsos.head, node) {
1497 if (dso__is_kcore(dso))
1498 return true;
1499 }
1500
1501 return false;
1502}
1503
1504static bool perf_event__is_extra_kernel_mmap(struct machine *machine,
1505 union perf_event *event)
1506{
1507 return machine__is(machine, "x86_64") &&
1508 is_entry_trampoline(event->mmap.filename);
1509}
1510
1511static int machine__process_extra_kernel_map(struct machine *machine,
1512 union perf_event *event)
1513{
1514 struct dso *kernel = machine__kernel_dso(machine);
1515 struct extra_kernel_map xm = {
1516 .start = event->mmap.start,
1517 .end = event->mmap.start + event->mmap.len,
1518 .pgoff = event->mmap.pgoff,
1519 };
1520
1521 if (kernel == NULL)
1522 return -1;
1523
1524 strlcpy(xm.name, event->mmap.filename, KMAP_NAME_LEN);
1525
1526 return machine__create_extra_kernel_map(machine, kernel, &xm);
1527}
1528
1529static int machine__process_kernel_mmap_event(struct machine *machine,
1530 union perf_event *event)
1531{
1532 struct map *map;
1533 enum dso_kernel_type kernel_type;
1534 bool is_kernel_mmap;
1535
1536
1537 if (machine__uses_kcore(machine))
1538 return 0;
1539
1540 if (machine__is_host(machine))
1541 kernel_type = DSO_TYPE_KERNEL;
1542 else
1543 kernel_type = DSO_TYPE_GUEST_KERNEL;
1544
1545 is_kernel_mmap = memcmp(event->mmap.filename,
1546 machine->mmap_name,
1547 strlen(machine->mmap_name) - 1) == 0;
1548 if (event->mmap.filename[0] == '/' ||
1549 (!is_kernel_mmap && event->mmap.filename[0] == '[')) {
1550 map = machine__addnew_module_map(machine, event->mmap.start,
1551 event->mmap.filename);
1552 if (map == NULL)
1553 goto out_problem;
1554
1555 map->end = map->start + event->mmap.len;
1556 } else if (is_kernel_mmap) {
1557 const char *symbol_name = (event->mmap.filename +
1558 strlen(machine->mmap_name));
1559
1560
1561
1562
1563 struct dso *kernel = NULL;
1564 struct dso *dso;
1565
1566 down_read(&machine->dsos.lock);
1567
1568 list_for_each_entry(dso, &machine->dsos.head, node) {
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586 if (!dso->kernel ||
1587 is_kernel_module(dso->long_name,
1588 PERF_RECORD_MISC_CPUMODE_UNKNOWN))
1589 continue;
1590
1591
1592 kernel = dso;
1593 break;
1594 }
1595
1596 up_read(&machine->dsos.lock);
1597
1598 if (kernel == NULL)
1599 kernel = machine__findnew_dso(machine, machine->mmap_name);
1600 if (kernel == NULL)
1601 goto out_problem;
1602
1603 kernel->kernel = kernel_type;
1604 if (__machine__create_kernel_maps(machine, kernel) < 0) {
1605 dso__put(kernel);
1606 goto out_problem;
1607 }
1608
1609 if (strstr(kernel->long_name, "vmlinux"))
1610 dso__set_short_name(kernel, "[kernel.vmlinux]", false);
1611
1612 machine__update_kernel_mmap(machine, event->mmap.start,
1613 event->mmap.start + event->mmap.len);
1614
1615
1616
1617
1618
1619
1620 if (event->mmap.pgoff != 0) {
1621 map__set_kallsyms_ref_reloc_sym(machine->vmlinux_map,
1622 symbol_name,
1623 event->mmap.pgoff);
1624 }
1625
1626 if (machine__is_default_guest(machine)) {
1627
1628
1629
1630 dso__load(kernel, machine__kernel_map(machine));
1631 }
1632 } else if (perf_event__is_extra_kernel_mmap(machine, event)) {
1633 return machine__process_extra_kernel_map(machine, event);
1634 }
1635 return 0;
1636out_problem:
1637 return -1;
1638}
1639
1640int machine__process_mmap2_event(struct machine *machine,
1641 union perf_event *event,
1642 struct perf_sample *sample)
1643{
1644 struct thread *thread;
1645 struct map *map;
1646 struct dso_id dso_id = {
1647 .maj = event->mmap2.maj,
1648 .min = event->mmap2.min,
1649 .ino = event->mmap2.ino,
1650 .ino_generation = event->mmap2.ino_generation,
1651 };
1652 int ret = 0;
1653
1654 if (dump_trace)
1655 perf_event__fprintf_mmap2(event, stdout);
1656
1657 if (sample->cpumode == PERF_RECORD_MISC_GUEST_KERNEL ||
1658 sample->cpumode == PERF_RECORD_MISC_KERNEL) {
1659 ret = machine__process_kernel_mmap_event(machine, event);
1660 if (ret < 0)
1661 goto out_problem;
1662 return 0;
1663 }
1664
1665 thread = machine__findnew_thread(machine, event->mmap2.pid,
1666 event->mmap2.tid);
1667 if (thread == NULL)
1668 goto out_problem;
1669
1670 map = map__new(machine, event->mmap2.start,
1671 event->mmap2.len, event->mmap2.pgoff,
1672 &dso_id, event->mmap2.prot,
1673 event->mmap2.flags,
1674 event->mmap2.filename, thread);
1675
1676 if (map == NULL)
1677 goto out_problem_map;
1678
1679 ret = thread__insert_map(thread, map);
1680 if (ret)
1681 goto out_problem_insert;
1682
1683 thread__put(thread);
1684 map__put(map);
1685 return 0;
1686
1687out_problem_insert:
1688 map__put(map);
1689out_problem_map:
1690 thread__put(thread);
1691out_problem:
1692 dump_printf("problem processing PERF_RECORD_MMAP2, skipping event.\n");
1693 return 0;
1694}
1695
1696int machine__process_mmap_event(struct machine *machine, union perf_event *event,
1697 struct perf_sample *sample)
1698{
1699 struct thread *thread;
1700 struct map *map;
1701 u32 prot = 0;
1702 int ret = 0;
1703
1704 if (dump_trace)
1705 perf_event__fprintf_mmap(event, stdout);
1706
1707 if (sample->cpumode == PERF_RECORD_MISC_GUEST_KERNEL ||
1708 sample->cpumode == PERF_RECORD_MISC_KERNEL) {
1709 ret = machine__process_kernel_mmap_event(machine, event);
1710 if (ret < 0)
1711 goto out_problem;
1712 return 0;
1713 }
1714
1715 thread = machine__findnew_thread(machine, event->mmap.pid,
1716 event->mmap.tid);
1717 if (thread == NULL)
1718 goto out_problem;
1719
1720 if (!(event->header.misc & PERF_RECORD_MISC_MMAP_DATA))
1721 prot = PROT_EXEC;
1722
1723 map = map__new(machine, event->mmap.start,
1724 event->mmap.len, event->mmap.pgoff,
1725 NULL, prot, 0, event->mmap.filename, thread);
1726
1727 if (map == NULL)
1728 goto out_problem_map;
1729
1730 ret = thread__insert_map(thread, map);
1731 if (ret)
1732 goto out_problem_insert;
1733
1734 thread__put(thread);
1735 map__put(map);
1736 return 0;
1737
1738out_problem_insert:
1739 map__put(map);
1740out_problem_map:
1741 thread__put(thread);
1742out_problem:
1743 dump_printf("problem processing PERF_RECORD_MMAP, skipping event.\n");
1744 return 0;
1745}
1746
1747static void __machine__remove_thread(struct machine *machine, struct thread *th, bool lock)
1748{
1749 struct threads *threads = machine__threads(machine, th->tid);
1750
1751 if (threads->last_match == th)
1752 threads__set_last_match(threads, NULL);
1753
1754 if (lock)
1755 down_write(&threads->lock);
1756
1757 BUG_ON(refcount_read(&th->refcnt) == 0);
1758
1759 rb_erase_cached(&th->rb_node, &threads->entries);
1760 RB_CLEAR_NODE(&th->rb_node);
1761 --threads->nr;
1762
1763
1764
1765
1766
1767 list_add_tail(&th->node, &threads->dead);
1768
1769
1770
1771
1772
1773
1774 thread__put(th);
1775
1776 if (lock)
1777 up_write(&threads->lock);
1778}
1779
1780void machine__remove_thread(struct machine *machine, struct thread *th)
1781{
1782 return __machine__remove_thread(machine, th, true);
1783}
1784
1785int machine__process_fork_event(struct machine *machine, union perf_event *event,
1786 struct perf_sample *sample)
1787{
1788 struct thread *thread = machine__find_thread(machine,
1789 event->fork.pid,
1790 event->fork.tid);
1791 struct thread *parent = machine__findnew_thread(machine,
1792 event->fork.ppid,
1793 event->fork.ptid);
1794 bool do_maps_clone = true;
1795 int err = 0;
1796
1797 if (dump_trace)
1798 perf_event__fprintf_task(event, stdout);
1799
1800
1801
1802
1803
1804
1805
1806 if (parent->pid_ != (pid_t)event->fork.ppid) {
1807 dump_printf("removing erroneous parent thread %d/%d\n",
1808 parent->pid_, parent->tid);
1809 machine__remove_thread(machine, parent);
1810 thread__put(parent);
1811 parent = machine__findnew_thread(machine, event->fork.ppid,
1812 event->fork.ptid);
1813 }
1814
1815
1816 if (thread != NULL) {
1817 machine__remove_thread(machine, thread);
1818 thread__put(thread);
1819 }
1820
1821 thread = machine__findnew_thread(machine, event->fork.pid,
1822 event->fork.tid);
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837 if (event->fork.header.misc & PERF_RECORD_MISC_FORK_EXEC)
1838 do_maps_clone = false;
1839
1840 if (thread == NULL || parent == NULL ||
1841 thread__fork(thread, parent, sample->time, do_maps_clone) < 0) {
1842 dump_printf("problem processing PERF_RECORD_FORK, skipping event.\n");
1843 err = -1;
1844 }
1845 thread__put(thread);
1846 thread__put(parent);
1847
1848 return err;
1849}
1850
1851int machine__process_exit_event(struct machine *machine, union perf_event *event,
1852 struct perf_sample *sample __maybe_unused)
1853{
1854 struct thread *thread = machine__find_thread(machine,
1855 event->fork.pid,
1856 event->fork.tid);
1857
1858 if (dump_trace)
1859 perf_event__fprintf_task(event, stdout);
1860
1861 if (thread != NULL) {
1862 thread__exited(thread);
1863 thread__put(thread);
1864 }
1865
1866 return 0;
1867}
1868
1869int machine__process_event(struct machine *machine, union perf_event *event,
1870 struct perf_sample *sample)
1871{
1872 int ret;
1873
1874 switch (event->header.type) {
1875 case PERF_RECORD_COMM:
1876 ret = machine__process_comm_event(machine, event, sample); break;
1877 case PERF_RECORD_MMAP:
1878 ret = machine__process_mmap_event(machine, event, sample); break;
1879 case PERF_RECORD_NAMESPACES:
1880 ret = machine__process_namespaces_event(machine, event, sample); break;
1881 case PERF_RECORD_MMAP2:
1882 ret = machine__process_mmap2_event(machine, event, sample); break;
1883 case PERF_RECORD_FORK:
1884 ret = machine__process_fork_event(machine, event, sample); break;
1885 case PERF_RECORD_EXIT:
1886 ret = machine__process_exit_event(machine, event, sample); break;
1887 case PERF_RECORD_LOST:
1888 ret = machine__process_lost_event(machine, event, sample); break;
1889 case PERF_RECORD_AUX:
1890 ret = machine__process_aux_event(machine, event); break;
1891 case PERF_RECORD_ITRACE_START:
1892 ret = machine__process_itrace_start_event(machine, event); break;
1893 case PERF_RECORD_LOST_SAMPLES:
1894 ret = machine__process_lost_samples_event(machine, event, sample); break;
1895 case PERF_RECORD_SWITCH:
1896 case PERF_RECORD_SWITCH_CPU_WIDE:
1897 ret = machine__process_switch_event(machine, event); break;
1898 case PERF_RECORD_KSYMBOL:
1899 ret = machine__process_ksymbol(machine, event, sample); break;
1900 case PERF_RECORD_BPF_EVENT:
1901 ret = machine__process_bpf(machine, event, sample); break;
1902 default:
1903 ret = -1;
1904 break;
1905 }
1906
1907 return ret;
1908}
1909
1910static bool symbol__match_regex(struct symbol *sym, regex_t *regex)
1911{
1912 if (!regexec(regex, sym->name, 0, NULL, 0))
1913 return 1;
1914 return 0;
1915}
1916
1917static void ip__resolve_ams(struct thread *thread,
1918 struct addr_map_symbol *ams,
1919 u64 ip)
1920{
1921 struct addr_location al;
1922
1923 memset(&al, 0, sizeof(al));
1924
1925
1926
1927
1928
1929
1930
1931 thread__find_cpumode_addr_location(thread, ip, &al);
1932
1933 ams->addr = ip;
1934 ams->al_addr = al.addr;
1935 ams->ms.maps = al.maps;
1936 ams->ms.sym = al.sym;
1937 ams->ms.map = al.map;
1938 ams->phys_addr = 0;
1939}
1940
1941static void ip__resolve_data(struct thread *thread,
1942 u8 m, struct addr_map_symbol *ams,
1943 u64 addr, u64 phys_addr)
1944{
1945 struct addr_location al;
1946
1947 memset(&al, 0, sizeof(al));
1948
1949 thread__find_symbol(thread, m, addr, &al);
1950
1951 ams->addr = addr;
1952 ams->al_addr = al.addr;
1953 ams->ms.maps = al.maps;
1954 ams->ms.sym = al.sym;
1955 ams->ms.map = al.map;
1956 ams->phys_addr = phys_addr;
1957}
1958
1959struct mem_info *sample__resolve_mem(struct perf_sample *sample,
1960 struct addr_location *al)
1961{
1962 struct mem_info *mi = mem_info__new();
1963
1964 if (!mi)
1965 return NULL;
1966
1967 ip__resolve_ams(al->thread, &mi->iaddr, sample->ip);
1968 ip__resolve_data(al->thread, al->cpumode, &mi->daddr,
1969 sample->addr, sample->phys_addr);
1970 mi->data_src.val = sample->data_src;
1971
1972 return mi;
1973}
1974
1975static char *callchain_srcline(struct map_symbol *ms, u64 ip)
1976{
1977 struct map *map = ms->map;
1978 char *srcline = NULL;
1979
1980 if (!map || callchain_param.key == CCKEY_FUNCTION)
1981 return srcline;
1982
1983 srcline = srcline__tree_find(&map->dso->srclines, ip);
1984 if (!srcline) {
1985 bool show_sym = false;
1986 bool show_addr = callchain_param.key == CCKEY_ADDRESS;
1987
1988 srcline = get_srcline(map->dso, map__rip_2objdump(map, ip),
1989 ms->sym, show_sym, show_addr, ip);
1990 srcline__tree_insert(&map->dso->srclines, ip, srcline);
1991 }
1992
1993 return srcline;
1994}
1995
1996struct iterations {
1997 int nr_loop_iter;
1998 u64 cycles;
1999};
2000
2001static int add_callchain_ip(struct thread *thread,
2002 struct callchain_cursor *cursor,
2003 struct symbol **parent,
2004 struct addr_location *root_al,
2005 u8 *cpumode,
2006 u64 ip,
2007 bool branch,
2008 struct branch_flags *flags,
2009 struct iterations *iter,
2010 u64 branch_from)
2011{
2012 struct map_symbol ms;
2013 struct addr_location al;
2014 int nr_loop_iter = 0;
2015 u64 iter_cycles = 0;
2016 const char *srcline = NULL;
2017
2018 al.filtered = 0;
2019 al.sym = NULL;
2020 if (!cpumode) {
2021 thread__find_cpumode_addr_location(thread, ip, &al);
2022 } else {
2023 if (ip >= PERF_CONTEXT_MAX) {
2024 switch (ip) {
2025 case PERF_CONTEXT_HV:
2026 *cpumode = PERF_RECORD_MISC_HYPERVISOR;
2027 break;
2028 case PERF_CONTEXT_KERNEL:
2029 *cpumode = PERF_RECORD_MISC_KERNEL;
2030 break;
2031 case PERF_CONTEXT_USER:
2032 *cpumode = PERF_RECORD_MISC_USER;
2033 break;
2034 default:
2035 pr_debug("invalid callchain context: "
2036 "%"PRId64"\n", (s64) ip);
2037
2038
2039
2040
2041 callchain_cursor_reset(cursor);
2042 return 1;
2043 }
2044 return 0;
2045 }
2046 thread__find_symbol(thread, *cpumode, ip, &al);
2047 }
2048
2049 if (al.sym != NULL) {
2050 if (perf_hpp_list.parent && !*parent &&
2051 symbol__match_regex(al.sym, &parent_regex))
2052 *parent = al.sym;
2053 else if (have_ignore_callees && root_al &&
2054 symbol__match_regex(al.sym, &ignore_callees_regex)) {
2055
2056
2057 *root_al = al;
2058 callchain_cursor_reset(cursor);
2059 }
2060 }
2061
2062 if (symbol_conf.hide_unresolved && al.sym == NULL)
2063 return 0;
2064
2065 if (iter) {
2066 nr_loop_iter = iter->nr_loop_iter;
2067 iter_cycles = iter->cycles;
2068 }
2069
2070 ms.maps = al.maps;
2071 ms.map = al.map;
2072 ms.sym = al.sym;
2073 srcline = callchain_srcline(&ms, al.addr);
2074 return callchain_cursor_append(cursor, ip, &ms,
2075 branch, flags, nr_loop_iter,
2076 iter_cycles, branch_from, srcline);
2077}
2078
2079struct branch_info *sample__resolve_bstack(struct perf_sample *sample,
2080 struct addr_location *al)
2081{
2082 unsigned int i;
2083 const struct branch_stack *bs = sample->branch_stack;
2084 struct branch_info *bi = calloc(bs->nr, sizeof(struct branch_info));
2085
2086 if (!bi)
2087 return NULL;
2088
2089 for (i = 0; i < bs->nr; i++) {
2090 ip__resolve_ams(al->thread, &bi[i].to, bs->entries[i].to);
2091 ip__resolve_ams(al->thread, &bi[i].from, bs->entries[i].from);
2092 bi[i].flags = bs->entries[i].flags;
2093 }
2094 return bi;
2095}
2096
2097static void save_iterations(struct iterations *iter,
2098 struct branch_entry *be, int nr)
2099{
2100 int i;
2101
2102 iter->nr_loop_iter++;
2103 iter->cycles = 0;
2104
2105 for (i = 0; i < nr; i++)
2106 iter->cycles += be[i].flags.cycles;
2107}
2108
2109#define CHASHSZ 127
2110#define CHASHBITS 7
2111#define NO_ENTRY 0xff
2112
2113#define PERF_MAX_BRANCH_DEPTH 127
2114
2115
2116static int remove_loops(struct branch_entry *l, int nr,
2117 struct iterations *iter)
2118{
2119 int i, j, off;
2120 unsigned char chash[CHASHSZ];
2121
2122 memset(chash, NO_ENTRY, sizeof(chash));
2123
2124 BUG_ON(PERF_MAX_BRANCH_DEPTH > 255);
2125
2126 for (i = 0; i < nr; i++) {
2127 int h = hash_64(l[i].from, CHASHBITS) % CHASHSZ;
2128
2129
2130 if (chash[h] == NO_ENTRY) {
2131 chash[h] = i;
2132 } else if (l[chash[h]].from == l[i].from) {
2133 bool is_loop = true;
2134
2135 off = 0;
2136 for (j = chash[h]; j < i && i + off < nr; j++, off++)
2137 if (l[j].from != l[i + off].from) {
2138 is_loop = false;
2139 break;
2140 }
2141 if (is_loop) {
2142 j = nr - (i + off);
2143 if (j > 0) {
2144 save_iterations(iter + i + off,
2145 l + i, off);
2146
2147 memmove(iter + i, iter + i + off,
2148 j * sizeof(*iter));
2149
2150 memmove(l + i, l + i + off,
2151 j * sizeof(*l));
2152 }
2153
2154 nr -= off;
2155 }
2156 }
2157 }
2158 return nr;
2159}
2160
2161
2162
2163
2164
2165
2166
2167
2168static int resolve_lbr_callchain_sample(struct thread *thread,
2169 struct callchain_cursor *cursor,
2170 struct perf_sample *sample,
2171 struct symbol **parent,
2172 struct addr_location *root_al,
2173 int max_stack)
2174{
2175 struct ip_callchain *chain = sample->callchain;
2176 int chain_nr = min(max_stack, (int)chain->nr), i;
2177 u8 cpumode = PERF_RECORD_MISC_USER;
2178 u64 ip, branch_from = 0;
2179
2180 for (i = 0; i < chain_nr; i++) {
2181 if (chain->ips[i] == PERF_CONTEXT_USER)
2182 break;
2183 }
2184
2185
2186 if (i != chain_nr) {
2187 struct branch_stack *lbr_stack = sample->branch_stack;
2188 int lbr_nr = lbr_stack->nr, j, k;
2189 bool branch;
2190 struct branch_flags *flags;
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201 int mix_chain_nr = i + 1 + lbr_nr + 1;
2202
2203 for (j = 0; j < mix_chain_nr; j++) {
2204 int err;
2205 branch = false;
2206 flags = NULL;
2207
2208 if (callchain_param.order == ORDER_CALLEE) {
2209 if (j < i + 1)
2210 ip = chain->ips[j];
2211 else if (j > i + 1) {
2212 k = j - i - 2;
2213 ip = lbr_stack->entries[k].from;
2214 branch = true;
2215 flags = &lbr_stack->entries[k].flags;
2216 } else {
2217 ip = lbr_stack->entries[0].to;
2218 branch = true;
2219 flags = &lbr_stack->entries[0].flags;
2220 branch_from =
2221 lbr_stack->entries[0].from;
2222 }
2223 } else {
2224 if (j < lbr_nr) {
2225 k = lbr_nr - j - 1;
2226 ip = lbr_stack->entries[k].from;
2227 branch = true;
2228 flags = &lbr_stack->entries[k].flags;
2229 }
2230 else if (j > lbr_nr)
2231 ip = chain->ips[i + 1 - (j - lbr_nr)];
2232 else {
2233 ip = lbr_stack->entries[0].to;
2234 branch = true;
2235 flags = &lbr_stack->entries[0].flags;
2236 branch_from =
2237 lbr_stack->entries[0].from;
2238 }
2239 }
2240
2241 err = add_callchain_ip(thread, cursor, parent,
2242 root_al, &cpumode, ip,
2243 branch, flags, NULL,
2244 branch_from);
2245 if (err)
2246 return (err < 0) ? err : 0;
2247 }
2248 return 1;
2249 }
2250
2251 return 0;
2252}
2253
2254static int find_prev_cpumode(struct ip_callchain *chain, struct thread *thread,
2255 struct callchain_cursor *cursor,
2256 struct symbol **parent,
2257 struct addr_location *root_al,
2258 u8 *cpumode, int ent)
2259{
2260 int err = 0;
2261
2262 while (--ent >= 0) {
2263 u64 ip = chain->ips[ent];
2264
2265 if (ip >= PERF_CONTEXT_MAX) {
2266 err = add_callchain_ip(thread, cursor, parent,
2267 root_al, cpumode, ip,
2268 false, NULL, NULL, 0);
2269 break;
2270 }
2271 }
2272 return err;
2273}
2274
2275static int thread__resolve_callchain_sample(struct thread *thread,
2276 struct callchain_cursor *cursor,
2277 struct evsel *evsel,
2278 struct perf_sample *sample,
2279 struct symbol **parent,
2280 struct addr_location *root_al,
2281 int max_stack)
2282{
2283 struct branch_stack *branch = sample->branch_stack;
2284 struct ip_callchain *chain = sample->callchain;
2285 int chain_nr = 0;
2286 u8 cpumode = PERF_RECORD_MISC_USER;
2287 int i, j, err, nr_entries;
2288 int skip_idx = -1;
2289 int first_call = 0;
2290
2291 if (chain)
2292 chain_nr = chain->nr;
2293
2294 if (perf_evsel__has_branch_callstack(evsel)) {
2295 err = resolve_lbr_callchain_sample(thread, cursor, sample, parent,
2296 root_al, max_stack);
2297 if (err)
2298 return (err < 0) ? err : 0;
2299 }
2300
2301
2302
2303
2304
2305 skip_idx = arch_skip_callchain_idx(thread, chain);
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319 if (branch && callchain_param.branch_callstack) {
2320 int nr = min(max_stack, (int)branch->nr);
2321 struct branch_entry be[nr];
2322 struct iterations iter[nr];
2323
2324 if (branch->nr > PERF_MAX_BRANCH_DEPTH) {
2325 pr_warning("corrupted branch chain. skipping...\n");
2326 goto check_calls;
2327 }
2328
2329 for (i = 0; i < nr; i++) {
2330 if (callchain_param.order == ORDER_CALLEE) {
2331 be[i] = branch->entries[i];
2332
2333 if (chain == NULL)
2334 continue;
2335
2336
2337
2338
2339
2340
2341
2342
2343 if (i == skip_idx ||
2344 chain->ips[first_call] >= PERF_CONTEXT_MAX)
2345 first_call++;
2346 else if (be[i].from < chain->ips[first_call] &&
2347 be[i].from >= chain->ips[first_call] - 8)
2348 first_call++;
2349 } else
2350 be[i] = branch->entries[branch->nr - i - 1];
2351 }
2352
2353 memset(iter, 0, sizeof(struct iterations) * nr);
2354 nr = remove_loops(be, nr, iter);
2355
2356 for (i = 0; i < nr; i++) {
2357 err = add_callchain_ip(thread, cursor, parent,
2358 root_al,
2359 NULL, be[i].to,
2360 true, &be[i].flags,
2361 NULL, be[i].from);
2362
2363 if (!err)
2364 err = add_callchain_ip(thread, cursor, parent, root_al,
2365 NULL, be[i].from,
2366 true, &be[i].flags,
2367 &iter[i], 0);
2368 if (err == -EINVAL)
2369 break;
2370 if (err)
2371 return err;
2372 }
2373
2374 if (chain_nr == 0)
2375 return 0;
2376
2377 chain_nr -= nr;
2378 }
2379
2380check_calls:
2381 if (chain && callchain_param.order != ORDER_CALLEE) {
2382 err = find_prev_cpumode(chain, thread, cursor, parent, root_al,
2383 &cpumode, chain->nr - first_call);
2384 if (err)
2385 return (err < 0) ? err : 0;
2386 }
2387 for (i = first_call, nr_entries = 0;
2388 i < chain_nr && nr_entries < max_stack; i++) {
2389 u64 ip;
2390
2391 if (callchain_param.order == ORDER_CALLEE)
2392 j = i;
2393 else
2394 j = chain->nr - i - 1;
2395
2396#ifdef HAVE_SKIP_CALLCHAIN_IDX
2397 if (j == skip_idx)
2398 continue;
2399#endif
2400 ip = chain->ips[j];
2401 if (ip < PERF_CONTEXT_MAX)
2402 ++nr_entries;
2403 else if (callchain_param.order != ORDER_CALLEE) {
2404 err = find_prev_cpumode(chain, thread, cursor, parent,
2405 root_al, &cpumode, j);
2406 if (err)
2407 return (err < 0) ? err : 0;
2408 continue;
2409 }
2410
2411 err = add_callchain_ip(thread, cursor, parent,
2412 root_al, &cpumode, ip,
2413 false, NULL, NULL, 0);
2414
2415 if (err)
2416 return (err < 0) ? err : 0;
2417 }
2418
2419 return 0;
2420}
2421
2422static int append_inlines(struct callchain_cursor *cursor, struct map_symbol *ms, u64 ip)
2423{
2424 struct symbol *sym = ms->sym;
2425 struct map *map = ms->map;
2426 struct inline_node *inline_node;
2427 struct inline_list *ilist;
2428 u64 addr;
2429 int ret = 1;
2430
2431 if (!symbol_conf.inline_name || !map || !sym)
2432 return ret;
2433
2434 addr = map__map_ip(map, ip);
2435 addr = map__rip_2objdump(map, addr);
2436
2437 inline_node = inlines__tree_find(&map->dso->inlined_nodes, addr);
2438 if (!inline_node) {
2439 inline_node = dso__parse_addr_inlines(map->dso, addr, sym);
2440 if (!inline_node)
2441 return ret;
2442 inlines__tree_insert(&map->dso->inlined_nodes, inline_node);
2443 }
2444
2445 list_for_each_entry(ilist, &inline_node->val, list) {
2446 struct map_symbol ilist_ms = {
2447 .maps = ms->maps,
2448 .map = map,
2449 .sym = ilist->symbol,
2450 };
2451 ret = callchain_cursor_append(cursor, ip, &ilist_ms, false,
2452 NULL, 0, 0, 0, ilist->srcline);
2453
2454 if (ret != 0)
2455 return ret;
2456 }
2457
2458 return ret;
2459}
2460
2461static int unwind_entry(struct unwind_entry *entry, void *arg)
2462{
2463 struct callchain_cursor *cursor = arg;
2464 const char *srcline = NULL;
2465 u64 addr = entry->ip;
2466
2467 if (symbol_conf.hide_unresolved && entry->ms.sym == NULL)
2468 return 0;
2469
2470 if (append_inlines(cursor, &entry->ms, entry->ip) == 0)
2471 return 0;
2472
2473
2474
2475
2476
2477 if (entry->ms.map)
2478 addr = map__map_ip(entry->ms.map, entry->ip);
2479
2480 srcline = callchain_srcline(&entry->ms, addr);
2481 return callchain_cursor_append(cursor, entry->ip, &entry->ms,
2482 false, NULL, 0, 0, 0, srcline);
2483}
2484
2485static int thread__resolve_callchain_unwind(struct thread *thread,
2486 struct callchain_cursor *cursor,
2487 struct evsel *evsel,
2488 struct perf_sample *sample,
2489 int max_stack)
2490{
2491
2492 if (!((evsel->core.attr.sample_type & PERF_SAMPLE_REGS_USER) &&
2493 (evsel->core.attr.sample_type & PERF_SAMPLE_STACK_USER)))
2494 return 0;
2495
2496
2497 if ((!sample->user_regs.regs) ||
2498 (!sample->user_stack.size))
2499 return 0;
2500
2501 return unwind__get_entries(unwind_entry, cursor,
2502 thread, sample, max_stack);
2503}
2504
2505int thread__resolve_callchain(struct thread *thread,
2506 struct callchain_cursor *cursor,
2507 struct evsel *evsel,
2508 struct perf_sample *sample,
2509 struct symbol **parent,
2510 struct addr_location *root_al,
2511 int max_stack)
2512{
2513 int ret = 0;
2514
2515 callchain_cursor_reset(cursor);
2516
2517 if (callchain_param.order == ORDER_CALLEE) {
2518 ret = thread__resolve_callchain_sample(thread, cursor,
2519 evsel, sample,
2520 parent, root_al,
2521 max_stack);
2522 if (ret)
2523 return ret;
2524 ret = thread__resolve_callchain_unwind(thread, cursor,
2525 evsel, sample,
2526 max_stack);
2527 } else {
2528 ret = thread__resolve_callchain_unwind(thread, cursor,
2529 evsel, sample,
2530 max_stack);
2531 if (ret)
2532 return ret;
2533 ret = thread__resolve_callchain_sample(thread, cursor,
2534 evsel, sample,
2535 parent, root_al,
2536 max_stack);
2537 }
2538
2539 return ret;
2540}
2541
2542int machine__for_each_thread(struct machine *machine,
2543 int (*fn)(struct thread *thread, void *p),
2544 void *priv)
2545{
2546 struct threads *threads;
2547 struct rb_node *nd;
2548 struct thread *thread;
2549 int rc = 0;
2550 int i;
2551
2552 for (i = 0; i < THREADS__TABLE_SIZE; i++) {
2553 threads = &machine->threads[i];
2554 for (nd = rb_first_cached(&threads->entries); nd;
2555 nd = rb_next(nd)) {
2556 thread = rb_entry(nd, struct thread, rb_node);
2557 rc = fn(thread, priv);
2558 if (rc != 0)
2559 return rc;
2560 }
2561
2562 list_for_each_entry(thread, &threads->dead, node) {
2563 rc = fn(thread, priv);
2564 if (rc != 0)
2565 return rc;
2566 }
2567 }
2568 return rc;
2569}
2570
2571int machines__for_each_thread(struct machines *machines,
2572 int (*fn)(struct thread *thread, void *p),
2573 void *priv)
2574{
2575 struct rb_node *nd;
2576 int rc = 0;
2577
2578 rc = machine__for_each_thread(&machines->host, fn, priv);
2579 if (rc != 0)
2580 return rc;
2581
2582 for (nd = rb_first_cached(&machines->guests); nd; nd = rb_next(nd)) {
2583 struct machine *machine = rb_entry(nd, struct machine, rb_node);
2584
2585 rc = machine__for_each_thread(machine, fn, priv);
2586 if (rc != 0)
2587 return rc;
2588 }
2589 return rc;
2590}
2591
2592pid_t machine__get_current_tid(struct machine *machine, int cpu)
2593{
2594 int nr_cpus = min(machine->env->nr_cpus_online, MAX_NR_CPUS);
2595
2596 if (cpu < 0 || cpu >= nr_cpus || !machine->current_tid)
2597 return -1;
2598
2599 return machine->current_tid[cpu];
2600}
2601
2602int machine__set_current_tid(struct machine *machine, int cpu, pid_t pid,
2603 pid_t tid)
2604{
2605 struct thread *thread;
2606 int nr_cpus = min(machine->env->nr_cpus_online, MAX_NR_CPUS);
2607
2608 if (cpu < 0)
2609 return -EINVAL;
2610
2611 if (!machine->current_tid) {
2612 int i;
2613
2614 machine->current_tid = calloc(nr_cpus, sizeof(pid_t));
2615 if (!machine->current_tid)
2616 return -ENOMEM;
2617 for (i = 0; i < nr_cpus; i++)
2618 machine->current_tid[i] = -1;
2619 }
2620
2621 if (cpu >= nr_cpus) {
2622 pr_err("Requested CPU %d too large. ", cpu);
2623 pr_err("Consider raising MAX_NR_CPUS\n");
2624 return -EINVAL;
2625 }
2626
2627 machine->current_tid[cpu] = tid;
2628
2629 thread = machine__findnew_thread(machine, pid, tid);
2630 if (!thread)
2631 return -ENOMEM;
2632
2633 thread->cpu = cpu;
2634 thread__put(thread);
2635
2636 return 0;
2637}
2638
2639
2640
2641
2642
2643bool machine__is(struct machine *machine, const char *arch)
2644{
2645 return machine && !strcmp(perf_env__raw_arch(machine->env), arch);
2646}
2647
2648int machine__nr_cpus_avail(struct machine *machine)
2649{
2650 return machine ? perf_env__nr_cpus_avail(machine->env) : 0;
2651}
2652
2653int machine__get_kernel_start(struct machine *machine)
2654{
2655 struct map *map = machine__kernel_map(machine);
2656 int err = 0;
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666 machine->kernel_start = 1ULL << 63;
2667 if (map) {
2668 err = map__load(map);
2669
2670
2671
2672
2673
2674 if (!err && !machine__is(machine, "x86_64"))
2675 machine->kernel_start = map->start;
2676 }
2677 return err;
2678}
2679
2680u8 machine__addr_cpumode(struct machine *machine, u8 cpumode, u64 addr)
2681{
2682 u8 addr_cpumode = cpumode;
2683 bool kernel_ip;
2684
2685 if (!machine->single_address_space)
2686 goto out;
2687
2688 kernel_ip = machine__kernel_ip(machine, addr);
2689 switch (cpumode) {
2690 case PERF_RECORD_MISC_KERNEL:
2691 case PERF_RECORD_MISC_USER:
2692 addr_cpumode = kernel_ip ? PERF_RECORD_MISC_KERNEL :
2693 PERF_RECORD_MISC_USER;
2694 break;
2695 case PERF_RECORD_MISC_GUEST_KERNEL:
2696 case PERF_RECORD_MISC_GUEST_USER:
2697 addr_cpumode = kernel_ip ? PERF_RECORD_MISC_GUEST_KERNEL :
2698 PERF_RECORD_MISC_GUEST_USER;
2699 break;
2700 default:
2701 break;
2702 }
2703out:
2704 return addr_cpumode;
2705}
2706
2707struct dso *machine__findnew_dso_id(struct machine *machine, const char *filename, struct dso_id *id)
2708{
2709 return dsos__findnew_id(&machine->dsos, filename, id);
2710}
2711
2712struct dso *machine__findnew_dso(struct machine *machine, const char *filename)
2713{
2714 return machine__findnew_dso_id(machine, filename, NULL);
2715}
2716
2717char *machine__resolve_kernel_addr(void *vmachine, unsigned long long *addrp, char **modp)
2718{
2719 struct machine *machine = vmachine;
2720 struct map *map;
2721 struct symbol *sym = machine__find_kernel_symbol(machine, *addrp, &map);
2722
2723 if (sym == NULL)
2724 return NULL;
2725
2726 *modp = __map__is_kmodule(map) ? (char *)map->dso->short_name : NULL;
2727 *addrp = map->unmap_ip(map, sym->start);
2728 return sym->name;
2729}
2730