GNU Linux-libre 5.19-rc6-gnu
[releases.git] / tools / perf / util / machine.c
1 // SPDX-License-Identifier: GPL-2.0
2 #include <dirent.h>
3 #include <errno.h>
4 #include <inttypes.h>
5 #include <regex.h>
6 #include <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 "path.h"
20 #include "srcline.h"
21 #include "symbol.h"
22 #include "sort.h"
23 #include "strlist.h"
24 #include "target.h"
25 #include "thread.h"
26 #include "util.h"
27 #include "vdso.h"
28 #include <stdbool.h>
29 #include <sys/types.h>
30 #include <sys/stat.h>
31 #include <unistd.h>
32 #include "unwind.h"
33 #include "linux/hash.h"
34 #include "asm/bug.h"
35 #include "bpf-event.h"
36 #include <internal/lib.h> // page_size
37 #include "cgroup.h"
38 #include "arm64-frame-pointer-unwind-support.h"
39
40 #include <linux/ctype.h>
41 #include <symbol/kallsyms.h>
42 #include <linux/mman.h>
43 #include <linux/string.h>
44 #include <linux/zalloc.h>
45
46 static void __machine__remove_thread(struct machine *machine, struct thread *th, bool lock);
47
48 static struct dso *machine__kernel_dso(struct machine *machine)
49 {
50         return machine->vmlinux_map->dso;
51 }
52
53 static void dsos__init(struct dsos *dsos)
54 {
55         INIT_LIST_HEAD(&dsos->head);
56         dsos->root = RB_ROOT;
57         init_rwsem(&dsos->lock);
58 }
59
60 static void machine__threads_init(struct machine *machine)
61 {
62         int i;
63
64         for (i = 0; i < THREADS__TABLE_SIZE; i++) {
65                 struct threads *threads = &machine->threads[i];
66                 threads->entries = RB_ROOT_CACHED;
67                 init_rwsem(&threads->lock);
68                 threads->nr = 0;
69                 INIT_LIST_HEAD(&threads->dead);
70                 threads->last_match = NULL;
71         }
72 }
73
74 static int machine__set_mmap_name(struct machine *machine)
75 {
76         if (machine__is_host(machine))
77                 machine->mmap_name = strdup("[kernel.kallsyms]");
78         else if (machine__is_default_guest(machine))
79                 machine->mmap_name = strdup("[guest.kernel.kallsyms]");
80         else if (asprintf(&machine->mmap_name, "[guest.kernel.kallsyms.%d]",
81                           machine->pid) < 0)
82                 machine->mmap_name = NULL;
83
84         return machine->mmap_name ? 0 : -ENOMEM;
85 }
86
87 static void thread__set_guest_comm(struct thread *thread, pid_t pid)
88 {
89         char comm[64];
90
91         snprintf(comm, sizeof(comm), "[guest/%d]", pid);
92         thread__set_comm(thread, comm, 0);
93 }
94
95 int machine__init(struct machine *machine, const char *root_dir, pid_t pid)
96 {
97         int err = -ENOMEM;
98
99         memset(machine, 0, sizeof(*machine));
100         machine->kmaps = maps__new(machine);
101         if (machine->kmaps == NULL)
102                 return -ENOMEM;
103
104         RB_CLEAR_NODE(&machine->rb_node);
105         dsos__init(&machine->dsos);
106
107         machine__threads_init(machine);
108
109         machine->vdso_info = NULL;
110         machine->env = NULL;
111
112         machine->pid = pid;
113
114         machine->id_hdr_size = 0;
115         machine->kptr_restrict_warned = false;
116         machine->comm_exec = false;
117         machine->kernel_start = 0;
118         machine->vmlinux_map = NULL;
119
120         machine->root_dir = strdup(root_dir);
121         if (machine->root_dir == NULL)
122                 goto out;
123
124         if (machine__set_mmap_name(machine))
125                 goto out;
126
127         if (pid != HOST_KERNEL_ID) {
128                 struct thread *thread = machine__findnew_thread(machine, -1,
129                                                                 pid);
130
131                 if (thread == NULL)
132                         goto out;
133
134                 thread__set_guest_comm(thread, pid);
135                 thread__put(thread);
136         }
137
138         machine->current_tid = NULL;
139         err = 0;
140
141 out:
142         if (err) {
143                 zfree(&machine->kmaps);
144                 zfree(&machine->root_dir);
145                 zfree(&machine->mmap_name);
146         }
147         return 0;
148 }
149
150 struct machine *machine__new_host(void)
151 {
152         struct machine *machine = malloc(sizeof(*machine));
153
154         if (machine != NULL) {
155                 machine__init(machine, "", HOST_KERNEL_ID);
156
157                 if (machine__create_kernel_maps(machine) < 0)
158                         goto out_delete;
159         }
160
161         return machine;
162 out_delete:
163         free(machine);
164         return NULL;
165 }
166
167 struct machine *machine__new_kallsyms(void)
168 {
169         struct machine *machine = machine__new_host();
170         /*
171          * FIXME:
172          * 1) We should switch to machine__load_kallsyms(), i.e. not explicitly
173          *    ask for not using the kcore parsing code, once this one is fixed
174          *    to create a map per module.
175          */
176         if (machine && machine__load_kallsyms(machine, "/proc/kallsyms") <= 0) {
177                 machine__delete(machine);
178                 machine = NULL;
179         }
180
181         return machine;
182 }
183
184 static void dsos__purge(struct dsos *dsos)
185 {
186         struct dso *pos, *n;
187
188         down_write(&dsos->lock);
189
190         list_for_each_entry_safe(pos, n, &dsos->head, node) {
191                 RB_CLEAR_NODE(&pos->rb_node);
192                 pos->root = NULL;
193                 list_del_init(&pos->node);
194                 dso__put(pos);
195         }
196
197         up_write(&dsos->lock);
198 }
199
200 static void dsos__exit(struct dsos *dsos)
201 {
202         dsos__purge(dsos);
203         exit_rwsem(&dsos->lock);
204 }
205
206 void machine__delete_threads(struct machine *machine)
207 {
208         struct rb_node *nd;
209         int i;
210
211         for (i = 0; i < THREADS__TABLE_SIZE; i++) {
212                 struct threads *threads = &machine->threads[i];
213                 down_write(&threads->lock);
214                 nd = rb_first_cached(&threads->entries);
215                 while (nd) {
216                         struct thread *t = rb_entry(nd, struct thread, rb_node);
217
218                         nd = rb_next(nd);
219                         __machine__remove_thread(machine, t, false);
220                 }
221                 up_write(&threads->lock);
222         }
223 }
224
225 void machine__exit(struct machine *machine)
226 {
227         int i;
228
229         if (machine == NULL)
230                 return;
231
232         machine__destroy_kernel_maps(machine);
233         maps__delete(machine->kmaps);
234         dsos__exit(&machine->dsos);
235         machine__exit_vdso(machine);
236         zfree(&machine->root_dir);
237         zfree(&machine->mmap_name);
238         zfree(&machine->current_tid);
239
240         for (i = 0; i < THREADS__TABLE_SIZE; i++) {
241                 struct threads *threads = &machine->threads[i];
242                 struct thread *thread, *n;
243                 /*
244                  * Forget about the dead, at this point whatever threads were
245                  * left in the dead lists better have a reference count taken
246                  * by who is using them, and then, when they drop those references
247                  * and it finally hits zero, thread__put() will check and see that
248                  * its not in the dead threads list and will not try to remove it
249                  * from there, just calling thread__delete() straight away.
250                  */
251                 list_for_each_entry_safe(thread, n, &threads->dead, node)
252                         list_del_init(&thread->node);
253
254                 exit_rwsem(&threads->lock);
255         }
256 }
257
258 void machine__delete(struct machine *machine)
259 {
260         if (machine) {
261                 machine__exit(machine);
262                 free(machine);
263         }
264 }
265
266 void machines__init(struct machines *machines)
267 {
268         machine__init(&machines->host, "", HOST_KERNEL_ID);
269         machines->guests = RB_ROOT_CACHED;
270 }
271
272 void machines__exit(struct machines *machines)
273 {
274         machine__exit(&machines->host);
275         /* XXX exit guest */
276 }
277
278 struct machine *machines__add(struct machines *machines, pid_t pid,
279                               const char *root_dir)
280 {
281         struct rb_node **p = &machines->guests.rb_root.rb_node;
282         struct rb_node *parent = NULL;
283         struct machine *pos, *machine = malloc(sizeof(*machine));
284         bool leftmost = true;
285
286         if (machine == NULL)
287                 return NULL;
288
289         if (machine__init(machine, root_dir, pid) != 0) {
290                 free(machine);
291                 return NULL;
292         }
293
294         while (*p != NULL) {
295                 parent = *p;
296                 pos = rb_entry(parent, struct machine, rb_node);
297                 if (pid < pos->pid)
298                         p = &(*p)->rb_left;
299                 else {
300                         p = &(*p)->rb_right;
301                         leftmost = false;
302                 }
303         }
304
305         rb_link_node(&machine->rb_node, parent, p);
306         rb_insert_color_cached(&machine->rb_node, &machines->guests, leftmost);
307
308         machine->machines = machines;
309
310         return machine;
311 }
312
313 void machines__set_comm_exec(struct machines *machines, bool comm_exec)
314 {
315         struct rb_node *nd;
316
317         machines->host.comm_exec = comm_exec;
318
319         for (nd = rb_first_cached(&machines->guests); nd; nd = rb_next(nd)) {
320                 struct machine *machine = rb_entry(nd, struct machine, rb_node);
321
322                 machine->comm_exec = comm_exec;
323         }
324 }
325
326 struct machine *machines__find(struct machines *machines, pid_t pid)
327 {
328         struct rb_node **p = &machines->guests.rb_root.rb_node;
329         struct rb_node *parent = NULL;
330         struct machine *machine;
331         struct machine *default_machine = NULL;
332
333         if (pid == HOST_KERNEL_ID)
334                 return &machines->host;
335
336         while (*p != NULL) {
337                 parent = *p;
338                 machine = rb_entry(parent, struct machine, rb_node);
339                 if (pid < machine->pid)
340                         p = &(*p)->rb_left;
341                 else if (pid > machine->pid)
342                         p = &(*p)->rb_right;
343                 else
344                         return machine;
345                 if (!machine->pid)
346                         default_machine = machine;
347         }
348
349         return default_machine;
350 }
351
352 struct machine *machines__findnew(struct machines *machines, pid_t pid)
353 {
354         char path[PATH_MAX];
355         const char *root_dir = "";
356         struct machine *machine = machines__find(machines, pid);
357
358         if (machine && (machine->pid == pid))
359                 goto out;
360
361         if ((pid != HOST_KERNEL_ID) &&
362             (pid != DEFAULT_GUEST_KERNEL_ID) &&
363             (symbol_conf.guestmount)) {
364                 sprintf(path, "%s/%d", symbol_conf.guestmount, pid);
365                 if (access(path, R_OK)) {
366                         static struct strlist *seen;
367
368                         if (!seen)
369                                 seen = strlist__new(NULL, NULL);
370
371                         if (!strlist__has_entry(seen, path)) {
372                                 pr_err("Can't access file %s\n", path);
373                                 strlist__add(seen, path);
374                         }
375                         machine = NULL;
376                         goto out;
377                 }
378                 root_dir = path;
379         }
380
381         machine = machines__add(machines, pid, root_dir);
382 out:
383         return machine;
384 }
385
386 struct machine *machines__find_guest(struct machines *machines, pid_t pid)
387 {
388         struct machine *machine = machines__find(machines, pid);
389
390         if (!machine)
391                 machine = machines__findnew(machines, DEFAULT_GUEST_KERNEL_ID);
392         return machine;
393 }
394
395 /*
396  * A common case for KVM test programs is that the test program acts as the
397  * hypervisor, creating, running and destroying the virtual machine, and
398  * providing the guest object code from its own object code. In this case,
399  * the VM is not running an OS, but only the functions loaded into it by the
400  * hypervisor test program, and conveniently, loaded at the same virtual
401  * addresses.
402  *
403  * Normally to resolve addresses, MMAP events are needed to map addresses
404  * back to the object code and debug symbols for that object code.
405  *
406  * Currently, there is no way to get such mapping information from guests
407  * but, in the scenario described above, the guest has the same mappings
408  * as the hypervisor, so support for that scenario can be achieved.
409  *
410  * To support that, copy the host thread's maps to the guest thread's maps.
411  * Note, we do not discover the guest until we encounter a guest event,
412  * which works well because it is not until then that we know that the host
413  * thread's maps have been set up.
414  *
415  * This function returns the guest thread. Apart from keeping the data
416  * structures sane, using a thread belonging to the guest machine, instead
417  * of the host thread, allows it to have its own comm (refer
418  * thread__set_guest_comm()).
419  */
420 static struct thread *findnew_guest_code(struct machine *machine,
421                                          struct machine *host_machine,
422                                          pid_t pid)
423 {
424         struct thread *host_thread;
425         struct thread *thread;
426         int err;
427
428         if (!machine)
429                 return NULL;
430
431         thread = machine__findnew_thread(machine, -1, pid);
432         if (!thread)
433                 return NULL;
434
435         /* Assume maps are set up if there are any */
436         if (thread->maps->nr_maps)
437                 return thread;
438
439         host_thread = machine__find_thread(host_machine, -1, pid);
440         if (!host_thread)
441                 goto out_err;
442
443         thread__set_guest_comm(thread, pid);
444
445         /*
446          * Guest code can be found in hypervisor process at the same address
447          * so copy host maps.
448          */
449         err = maps__clone(thread, host_thread->maps);
450         thread__put(host_thread);
451         if (err)
452                 goto out_err;
453
454         return thread;
455
456 out_err:
457         thread__zput(thread);
458         return NULL;
459 }
460
461 struct thread *machines__findnew_guest_code(struct machines *machines, pid_t pid)
462 {
463         struct machine *host_machine = machines__find(machines, HOST_KERNEL_ID);
464         struct machine *machine = machines__findnew(machines, pid);
465
466         return findnew_guest_code(machine, host_machine, pid);
467 }
468
469 struct thread *machine__findnew_guest_code(struct machine *machine, pid_t pid)
470 {
471         struct machines *machines = machine->machines;
472         struct machine *host_machine;
473
474         if (!machines)
475                 return NULL;
476
477         host_machine = machines__find(machines, HOST_KERNEL_ID);
478
479         return findnew_guest_code(machine, host_machine, pid);
480 }
481
482 void machines__process_guests(struct machines *machines,
483                               machine__process_t process, void *data)
484 {
485         struct rb_node *nd;
486
487         for (nd = rb_first_cached(&machines->guests); nd; nd = rb_next(nd)) {
488                 struct machine *pos = rb_entry(nd, struct machine, rb_node);
489                 process(pos, data);
490         }
491 }
492
493 void machines__set_id_hdr_size(struct machines *machines, u16 id_hdr_size)
494 {
495         struct rb_node *node;
496         struct machine *machine;
497
498         machines->host.id_hdr_size = id_hdr_size;
499
500         for (node = rb_first_cached(&machines->guests); node;
501              node = rb_next(node)) {
502                 machine = rb_entry(node, struct machine, rb_node);
503                 machine->id_hdr_size = id_hdr_size;
504         }
505
506         return;
507 }
508
509 static void machine__update_thread_pid(struct machine *machine,
510                                        struct thread *th, pid_t pid)
511 {
512         struct thread *leader;
513
514         if (pid == th->pid_ || pid == -1 || th->pid_ != -1)
515                 return;
516
517         th->pid_ = pid;
518
519         if (th->pid_ == th->tid)
520                 return;
521
522         leader = __machine__findnew_thread(machine, th->pid_, th->pid_);
523         if (!leader)
524                 goto out_err;
525
526         if (!leader->maps)
527                 leader->maps = maps__new(machine);
528
529         if (!leader->maps)
530                 goto out_err;
531
532         if (th->maps == leader->maps)
533                 return;
534
535         if (th->maps) {
536                 /*
537                  * Maps are created from MMAP events which provide the pid and
538                  * tid.  Consequently there never should be any maps on a thread
539                  * with an unknown pid.  Just print an error if there are.
540                  */
541                 if (!maps__empty(th->maps))
542                         pr_err("Discarding thread maps for %d:%d\n",
543                                th->pid_, th->tid);
544                 maps__put(th->maps);
545         }
546
547         th->maps = maps__get(leader->maps);
548 out_put:
549         thread__put(leader);
550         return;
551 out_err:
552         pr_err("Failed to join map groups for %d:%d\n", th->pid_, th->tid);
553         goto out_put;
554 }
555
556 /*
557  * Front-end cache - TID lookups come in blocks,
558  * so most of the time we dont have to look up
559  * the full rbtree:
560  */
561 static struct thread*
562 __threads__get_last_match(struct threads *threads, struct machine *machine,
563                           int pid, int tid)
564 {
565         struct thread *th;
566
567         th = threads->last_match;
568         if (th != NULL) {
569                 if (th->tid == tid) {
570                         machine__update_thread_pid(machine, th, pid);
571                         return thread__get(th);
572                 }
573
574                 threads->last_match = NULL;
575         }
576
577         return NULL;
578 }
579
580 static struct thread*
581 threads__get_last_match(struct threads *threads, struct machine *machine,
582                         int pid, int tid)
583 {
584         struct thread *th = NULL;
585
586         if (perf_singlethreaded)
587                 th = __threads__get_last_match(threads, machine, pid, tid);
588
589         return th;
590 }
591
592 static void
593 __threads__set_last_match(struct threads *threads, struct thread *th)
594 {
595         threads->last_match = th;
596 }
597
598 static void
599 threads__set_last_match(struct threads *threads, struct thread *th)
600 {
601         if (perf_singlethreaded)
602                 __threads__set_last_match(threads, th);
603 }
604
605 /*
606  * Caller must eventually drop thread->refcnt returned with a successful
607  * lookup/new thread inserted.
608  */
609 static struct thread *____machine__findnew_thread(struct machine *machine,
610                                                   struct threads *threads,
611                                                   pid_t pid, pid_t tid,
612                                                   bool create)
613 {
614         struct rb_node **p = &threads->entries.rb_root.rb_node;
615         struct rb_node *parent = NULL;
616         struct thread *th;
617         bool leftmost = true;
618
619         th = threads__get_last_match(threads, machine, pid, tid);
620         if (th)
621                 return th;
622
623         while (*p != NULL) {
624                 parent = *p;
625                 th = rb_entry(parent, struct thread, rb_node);
626
627                 if (th->tid == tid) {
628                         threads__set_last_match(threads, th);
629                         machine__update_thread_pid(machine, th, pid);
630                         return thread__get(th);
631                 }
632
633                 if (tid < th->tid)
634                         p = &(*p)->rb_left;
635                 else {
636                         p = &(*p)->rb_right;
637                         leftmost = false;
638                 }
639         }
640
641         if (!create)
642                 return NULL;
643
644         th = thread__new(pid, tid);
645         if (th != NULL) {
646                 rb_link_node(&th->rb_node, parent, p);
647                 rb_insert_color_cached(&th->rb_node, &threads->entries, leftmost);
648
649                 /*
650                  * We have to initialize maps separately after rb tree is updated.
651                  *
652                  * The reason is that we call machine__findnew_thread
653                  * within thread__init_maps to find the thread
654                  * leader and that would screwed the rb tree.
655                  */
656                 if (thread__init_maps(th, machine)) {
657                         rb_erase_cached(&th->rb_node, &threads->entries);
658                         RB_CLEAR_NODE(&th->rb_node);
659                         thread__put(th);
660                         return NULL;
661                 }
662                 /*
663                  * It is now in the rbtree, get a ref
664                  */
665                 thread__get(th);
666                 threads__set_last_match(threads, th);
667                 ++threads->nr;
668         }
669
670         return th;
671 }
672
673 struct thread *__machine__findnew_thread(struct machine *machine, pid_t pid, pid_t tid)
674 {
675         return ____machine__findnew_thread(machine, machine__threads(machine, tid), pid, tid, true);
676 }
677
678 struct thread *machine__findnew_thread(struct machine *machine, pid_t pid,
679                                        pid_t tid)
680 {
681         struct threads *threads = machine__threads(machine, tid);
682         struct thread *th;
683
684         down_write(&threads->lock);
685         th = __machine__findnew_thread(machine, pid, tid);
686         up_write(&threads->lock);
687         return th;
688 }
689
690 struct thread *machine__find_thread(struct machine *machine, pid_t pid,
691                                     pid_t tid)
692 {
693         struct threads *threads = machine__threads(machine, tid);
694         struct thread *th;
695
696         down_read(&threads->lock);
697         th =  ____machine__findnew_thread(machine, threads, pid, tid, false);
698         up_read(&threads->lock);
699         return th;
700 }
701
702 /*
703  * Threads are identified by pid and tid, and the idle task has pid == tid == 0.
704  * So here a single thread is created for that, but actually there is a separate
705  * idle task per cpu, so there should be one 'struct thread' per cpu, but there
706  * is only 1. That causes problems for some tools, requiring workarounds. For
707  * example get_idle_thread() in builtin-sched.c, or thread_stack__per_cpu().
708  */
709 struct thread *machine__idle_thread(struct machine *machine)
710 {
711         struct thread *thread = machine__findnew_thread(machine, 0, 0);
712
713         if (!thread || thread__set_comm(thread, "swapper", 0) ||
714             thread__set_namespaces(thread, 0, NULL))
715                 pr_err("problem inserting idle task for machine pid %d\n", machine->pid);
716
717         return thread;
718 }
719
720 struct comm *machine__thread_exec_comm(struct machine *machine,
721                                        struct thread *thread)
722 {
723         if (machine->comm_exec)
724                 return thread__exec_comm(thread);
725         else
726                 return thread__comm(thread);
727 }
728
729 int machine__process_comm_event(struct machine *machine, union perf_event *event,
730                                 struct perf_sample *sample)
731 {
732         struct thread *thread = machine__findnew_thread(machine,
733                                                         event->comm.pid,
734                                                         event->comm.tid);
735         bool exec = event->header.misc & PERF_RECORD_MISC_COMM_EXEC;
736         int err = 0;
737
738         if (exec)
739                 machine->comm_exec = true;
740
741         if (dump_trace)
742                 perf_event__fprintf_comm(event, stdout);
743
744         if (thread == NULL ||
745             __thread__set_comm(thread, event->comm.comm, sample->time, exec)) {
746                 dump_printf("problem processing PERF_RECORD_COMM, skipping event.\n");
747                 err = -1;
748         }
749
750         thread__put(thread);
751
752         return err;
753 }
754
755 int machine__process_namespaces_event(struct machine *machine __maybe_unused,
756                                       union perf_event *event,
757                                       struct perf_sample *sample __maybe_unused)
758 {
759         struct thread *thread = machine__findnew_thread(machine,
760                                                         event->namespaces.pid,
761                                                         event->namespaces.tid);
762         int err = 0;
763
764         WARN_ONCE(event->namespaces.nr_namespaces > NR_NAMESPACES,
765                   "\nWARNING: kernel seems to support more namespaces than perf"
766                   " tool.\nTry updating the perf tool..\n\n");
767
768         WARN_ONCE(event->namespaces.nr_namespaces < NR_NAMESPACES,
769                   "\nWARNING: perf tool seems to support more namespaces than"
770                   " the kernel.\nTry updating the kernel..\n\n");
771
772         if (dump_trace)
773                 perf_event__fprintf_namespaces(event, stdout);
774
775         if (thread == NULL ||
776             thread__set_namespaces(thread, sample->time, &event->namespaces)) {
777                 dump_printf("problem processing PERF_RECORD_NAMESPACES, skipping event.\n");
778                 err = -1;
779         }
780
781         thread__put(thread);
782
783         return err;
784 }
785
786 int machine__process_cgroup_event(struct machine *machine,
787                                   union perf_event *event,
788                                   struct perf_sample *sample __maybe_unused)
789 {
790         struct cgroup *cgrp;
791
792         if (dump_trace)
793                 perf_event__fprintf_cgroup(event, stdout);
794
795         cgrp = cgroup__findnew(machine->env, event->cgroup.id, event->cgroup.path);
796         if (cgrp == NULL)
797                 return -ENOMEM;
798
799         return 0;
800 }
801
802 int machine__process_lost_event(struct machine *machine __maybe_unused,
803                                 union perf_event *event, struct perf_sample *sample __maybe_unused)
804 {
805         dump_printf(": id:%" PRI_lu64 ": lost:%" PRI_lu64 "\n",
806                     event->lost.id, event->lost.lost);
807         return 0;
808 }
809
810 int machine__process_lost_samples_event(struct machine *machine __maybe_unused,
811                                         union perf_event *event, struct perf_sample *sample)
812 {
813         dump_printf(": id:%" PRIu64 ": lost samples :%" PRI_lu64 "\n",
814                     sample->id, event->lost_samples.lost);
815         return 0;
816 }
817
818 static struct dso *machine__findnew_module_dso(struct machine *machine,
819                                                struct kmod_path *m,
820                                                const char *filename)
821 {
822         struct dso *dso;
823
824         down_write(&machine->dsos.lock);
825
826         dso = __dsos__find(&machine->dsos, m->name, true);
827         if (!dso) {
828                 dso = __dsos__addnew(&machine->dsos, m->name);
829                 if (dso == NULL)
830                         goto out_unlock;
831
832                 dso__set_module_info(dso, m, machine);
833                 dso__set_long_name(dso, strdup(filename), true);
834                 dso->kernel = DSO_SPACE__KERNEL;
835         }
836
837         dso__get(dso);
838 out_unlock:
839         up_write(&machine->dsos.lock);
840         return dso;
841 }
842
843 int machine__process_aux_event(struct machine *machine __maybe_unused,
844                                union perf_event *event)
845 {
846         if (dump_trace)
847                 perf_event__fprintf_aux(event, stdout);
848         return 0;
849 }
850
851 int machine__process_itrace_start_event(struct machine *machine __maybe_unused,
852                                         union perf_event *event)
853 {
854         if (dump_trace)
855                 perf_event__fprintf_itrace_start(event, stdout);
856         return 0;
857 }
858
859 int machine__process_aux_output_hw_id_event(struct machine *machine __maybe_unused,
860                                             union perf_event *event)
861 {
862         if (dump_trace)
863                 perf_event__fprintf_aux_output_hw_id(event, stdout);
864         return 0;
865 }
866
867 int machine__process_switch_event(struct machine *machine __maybe_unused,
868                                   union perf_event *event)
869 {
870         if (dump_trace)
871                 perf_event__fprintf_switch(event, stdout);
872         return 0;
873 }
874
875 static int machine__process_ksymbol_register(struct machine *machine,
876                                              union perf_event *event,
877                                              struct perf_sample *sample __maybe_unused)
878 {
879         struct symbol *sym;
880         struct map *map = maps__find(machine__kernel_maps(machine), event->ksymbol.addr);
881
882         if (!map) {
883                 struct dso *dso = dso__new(event->ksymbol.name);
884
885                 if (dso) {
886                         dso->kernel = DSO_SPACE__KERNEL;
887                         map = map__new2(0, dso);
888                         dso__put(dso);
889                 }
890
891                 if (!dso || !map) {
892                         return -ENOMEM;
893                 }
894
895                 if (event->ksymbol.ksym_type == PERF_RECORD_KSYMBOL_TYPE_OOL) {
896                         map->dso->binary_type = DSO_BINARY_TYPE__OOL;
897                         map->dso->data.file_size = event->ksymbol.len;
898                         dso__set_loaded(map->dso);
899                 }
900
901                 map->start = event->ksymbol.addr;
902                 map->end = map->start + event->ksymbol.len;
903                 maps__insert(machine__kernel_maps(machine), map);
904                 map__put(map);
905                 dso__set_loaded(dso);
906
907                 if (is_bpf_image(event->ksymbol.name)) {
908                         dso->binary_type = DSO_BINARY_TYPE__BPF_IMAGE;
909                         dso__set_long_name(dso, "", false);
910                 }
911         }
912
913         sym = symbol__new(map->map_ip(map, map->start),
914                           event->ksymbol.len,
915                           0, 0, event->ksymbol.name);
916         if (!sym)
917                 return -ENOMEM;
918         dso__insert_symbol(map->dso, sym);
919         return 0;
920 }
921
922 static int machine__process_ksymbol_unregister(struct machine *machine,
923                                                union perf_event *event,
924                                                struct perf_sample *sample __maybe_unused)
925 {
926         struct symbol *sym;
927         struct map *map;
928
929         map = maps__find(machine__kernel_maps(machine), event->ksymbol.addr);
930         if (!map)
931                 return 0;
932
933         if (map != machine->vmlinux_map)
934                 maps__remove(machine__kernel_maps(machine), map);
935         else {
936                 sym = dso__find_symbol(map->dso, map->map_ip(map, map->start));
937                 if (sym)
938                         dso__delete_symbol(map->dso, sym);
939         }
940
941         return 0;
942 }
943
944 int machine__process_ksymbol(struct machine *machine __maybe_unused,
945                              union perf_event *event,
946                              struct perf_sample *sample)
947 {
948         if (dump_trace)
949                 perf_event__fprintf_ksymbol(event, stdout);
950
951         if (event->ksymbol.flags & PERF_RECORD_KSYMBOL_FLAGS_UNREGISTER)
952                 return machine__process_ksymbol_unregister(machine, event,
953                                                            sample);
954         return machine__process_ksymbol_register(machine, event, sample);
955 }
956
957 int machine__process_text_poke(struct machine *machine, union perf_event *event,
958                                struct perf_sample *sample __maybe_unused)
959 {
960         struct map *map = maps__find(machine__kernel_maps(machine), event->text_poke.addr);
961         u8 cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
962
963         if (dump_trace)
964                 perf_event__fprintf_text_poke(event, machine, stdout);
965
966         if (!event->text_poke.new_len)
967                 return 0;
968
969         if (cpumode != PERF_RECORD_MISC_KERNEL) {
970                 pr_debug("%s: unsupported cpumode - ignoring\n", __func__);
971                 return 0;
972         }
973
974         if (map && map->dso) {
975                 u8 *new_bytes = event->text_poke.bytes + event->text_poke.old_len;
976                 int ret;
977
978                 /*
979                  * Kernel maps might be changed when loading symbols so loading
980                  * must be done prior to using kernel maps.
981                  */
982                 map__load(map);
983                 ret = dso__data_write_cache_addr(map->dso, map, machine,
984                                                  event->text_poke.addr,
985                                                  new_bytes,
986                                                  event->text_poke.new_len);
987                 if (ret != event->text_poke.new_len)
988                         pr_debug("Failed to write kernel text poke at %#" PRI_lx64 "\n",
989                                  event->text_poke.addr);
990         } else {
991                 pr_debug("Failed to find kernel text poke address map for %#" PRI_lx64 "\n",
992                          event->text_poke.addr);
993         }
994
995         return 0;
996 }
997
998 static struct map *machine__addnew_module_map(struct machine *machine, u64 start,
999                                               const char *filename)
1000 {
1001         struct map *map = NULL;
1002         struct kmod_path m;
1003         struct dso *dso;
1004
1005         if (kmod_path__parse_name(&m, filename))
1006                 return NULL;
1007
1008         dso = machine__findnew_module_dso(machine, &m, filename);
1009         if (dso == NULL)
1010                 goto out;
1011
1012         map = map__new2(start, dso);
1013         if (map == NULL)
1014                 goto out;
1015
1016         maps__insert(machine__kernel_maps(machine), map);
1017
1018         /* Put the map here because maps__insert already got it */
1019         map__put(map);
1020 out:
1021         /* put the dso here, corresponding to  machine__findnew_module_dso */
1022         dso__put(dso);
1023         zfree(&m.name);
1024         return map;
1025 }
1026
1027 size_t machines__fprintf_dsos(struct machines *machines, FILE *fp)
1028 {
1029         struct rb_node *nd;
1030         size_t ret = __dsos__fprintf(&machines->host.dsos.head, fp);
1031
1032         for (nd = rb_first_cached(&machines->guests); nd; nd = rb_next(nd)) {
1033                 struct machine *pos = rb_entry(nd, struct machine, rb_node);
1034                 ret += __dsos__fprintf(&pos->dsos.head, fp);
1035         }
1036
1037         return ret;
1038 }
1039
1040 size_t machine__fprintf_dsos_buildid(struct machine *m, FILE *fp,
1041                                      bool (skip)(struct dso *dso, int parm), int parm)
1042 {
1043         return __dsos__fprintf_buildid(&m->dsos.head, fp, skip, parm);
1044 }
1045
1046 size_t machines__fprintf_dsos_buildid(struct machines *machines, FILE *fp,
1047                                      bool (skip)(struct dso *dso, int parm), int parm)
1048 {
1049         struct rb_node *nd;
1050         size_t ret = machine__fprintf_dsos_buildid(&machines->host, fp, skip, parm);
1051
1052         for (nd = rb_first_cached(&machines->guests); nd; nd = rb_next(nd)) {
1053                 struct machine *pos = rb_entry(nd, struct machine, rb_node);
1054                 ret += machine__fprintf_dsos_buildid(pos, fp, skip, parm);
1055         }
1056         return ret;
1057 }
1058
1059 size_t machine__fprintf_vmlinux_path(struct machine *machine, FILE *fp)
1060 {
1061         int i;
1062         size_t printed = 0;
1063         struct dso *kdso = machine__kernel_dso(machine);
1064
1065         if (kdso->has_build_id) {
1066                 char filename[PATH_MAX];
1067                 if (dso__build_id_filename(kdso, filename, sizeof(filename),
1068                                            false))
1069                         printed += fprintf(fp, "[0] %s\n", filename);
1070         }
1071
1072         for (i = 0; i < vmlinux_path__nr_entries; ++i)
1073                 printed += fprintf(fp, "[%d] %s\n",
1074                                    i + kdso->has_build_id, vmlinux_path[i]);
1075
1076         return printed;
1077 }
1078
1079 size_t machine__fprintf(struct machine *machine, FILE *fp)
1080 {
1081         struct rb_node *nd;
1082         size_t ret;
1083         int i;
1084
1085         for (i = 0; i < THREADS__TABLE_SIZE; i++) {
1086                 struct threads *threads = &machine->threads[i];
1087
1088                 down_read(&threads->lock);
1089
1090                 ret = fprintf(fp, "Threads: %u\n", threads->nr);
1091
1092                 for (nd = rb_first_cached(&threads->entries); nd;
1093                      nd = rb_next(nd)) {
1094                         struct thread *pos = rb_entry(nd, struct thread, rb_node);
1095
1096                         ret += thread__fprintf(pos, fp);
1097                 }
1098
1099                 up_read(&threads->lock);
1100         }
1101         return ret;
1102 }
1103
1104 static struct dso *machine__get_kernel(struct machine *machine)
1105 {
1106         const char *vmlinux_name = machine->mmap_name;
1107         struct dso *kernel;
1108
1109         if (machine__is_host(machine)) {
1110                 if (symbol_conf.vmlinux_name)
1111                         vmlinux_name = symbol_conf.vmlinux_name;
1112
1113                 kernel = machine__findnew_kernel(machine, vmlinux_name,
1114                                                  "[kernel]", DSO_SPACE__KERNEL);
1115         } else {
1116                 if (symbol_conf.default_guest_vmlinux_name)
1117                         vmlinux_name = symbol_conf.default_guest_vmlinux_name;
1118
1119                 kernel = machine__findnew_kernel(machine, vmlinux_name,
1120                                                  "[guest.kernel]",
1121                                                  DSO_SPACE__KERNEL_GUEST);
1122         }
1123
1124         if (kernel != NULL && (!kernel->has_build_id))
1125                 dso__read_running_kernel_build_id(kernel, machine);
1126
1127         return kernel;
1128 }
1129
1130 struct process_args {
1131         u64 start;
1132 };
1133
1134 void machine__get_kallsyms_filename(struct machine *machine, char *buf,
1135                                     size_t bufsz)
1136 {
1137         if (machine__is_default_guest(machine))
1138                 scnprintf(buf, bufsz, "%s", symbol_conf.default_guest_kallsyms);
1139         else
1140                 scnprintf(buf, bufsz, "%s/proc/kallsyms", machine->root_dir);
1141 }
1142
1143 const char *ref_reloc_sym_names[] = {"_text", "_stext", NULL};
1144
1145 /* Figure out the start address of kernel map from /proc/kallsyms.
1146  * Returns the name of the start symbol in *symbol_name. Pass in NULL as
1147  * symbol_name if it's not that important.
1148  */
1149 static int machine__get_running_kernel_start(struct machine *machine,
1150                                              const char **symbol_name,
1151                                              u64 *start, u64 *end)
1152 {
1153         char filename[PATH_MAX];
1154         int i, err = -1;
1155         const char *name;
1156         u64 addr = 0;
1157
1158         machine__get_kallsyms_filename(machine, filename, PATH_MAX);
1159
1160         if (symbol__restricted_filename(filename, "/proc/kallsyms"))
1161                 return 0;
1162
1163         for (i = 0; (name = ref_reloc_sym_names[i]) != NULL; i++) {
1164                 err = kallsyms__get_function_start(filename, name, &addr);
1165                 if (!err)
1166                         break;
1167         }
1168
1169         if (err)
1170                 return -1;
1171
1172         if (symbol_name)
1173                 *symbol_name = name;
1174
1175         *start = addr;
1176
1177         err = kallsyms__get_function_start(filename, "_etext", &addr);
1178         if (!err)
1179                 *end = addr;
1180
1181         return 0;
1182 }
1183
1184 int machine__create_extra_kernel_map(struct machine *machine,
1185                                      struct dso *kernel,
1186                                      struct extra_kernel_map *xm)
1187 {
1188         struct kmap *kmap;
1189         struct map *map;
1190
1191         map = map__new2(xm->start, kernel);
1192         if (!map)
1193                 return -1;
1194
1195         map->end   = xm->end;
1196         map->pgoff = xm->pgoff;
1197
1198         kmap = map__kmap(map);
1199
1200         strlcpy(kmap->name, xm->name, KMAP_NAME_LEN);
1201
1202         maps__insert(machine__kernel_maps(machine), map);
1203
1204         pr_debug2("Added extra kernel map %s %" PRIx64 "-%" PRIx64 "\n",
1205                   kmap->name, map->start, map->end);
1206
1207         map__put(map);
1208
1209         return 0;
1210 }
1211
1212 static u64 find_entry_trampoline(struct dso *dso)
1213 {
1214         /* Duplicates are removed so lookup all aliases */
1215         const char *syms[] = {
1216                 "_entry_trampoline",
1217                 "__entry_trampoline_start",
1218                 "entry_SYSCALL_64_trampoline",
1219         };
1220         struct symbol *sym = dso__first_symbol(dso);
1221         unsigned int i;
1222
1223         for (; sym; sym = dso__next_symbol(sym)) {
1224                 if (sym->binding != STB_GLOBAL)
1225                         continue;
1226                 for (i = 0; i < ARRAY_SIZE(syms); i++) {
1227                         if (!strcmp(sym->name, syms[i]))
1228                                 return sym->start;
1229                 }
1230         }
1231
1232         return 0;
1233 }
1234
1235 /*
1236  * These values can be used for kernels that do not have symbols for the entry
1237  * trampolines in kallsyms.
1238  */
1239 #define X86_64_CPU_ENTRY_AREA_PER_CPU   0xfffffe0000000000ULL
1240 #define X86_64_CPU_ENTRY_AREA_SIZE      0x2c000
1241 #define X86_64_ENTRY_TRAMPOLINE         0x6000
1242
1243 /* Map x86_64 PTI entry trampolines */
1244 int machine__map_x86_64_entry_trampolines(struct machine *machine,
1245                                           struct dso *kernel)
1246 {
1247         struct maps *kmaps = machine__kernel_maps(machine);
1248         int nr_cpus_avail, cpu;
1249         bool found = false;
1250         struct map *map;
1251         u64 pgoff;
1252
1253         /*
1254          * In the vmlinux case, pgoff is a virtual address which must now be
1255          * mapped to a vmlinux offset.
1256          */
1257         maps__for_each_entry(kmaps, map) {
1258                 struct kmap *kmap = __map__kmap(map);
1259                 struct map *dest_map;
1260
1261                 if (!kmap || !is_entry_trampoline(kmap->name))
1262                         continue;
1263
1264                 dest_map = maps__find(kmaps, map->pgoff);
1265                 if (dest_map != map)
1266                         map->pgoff = dest_map->map_ip(dest_map, map->pgoff);
1267                 found = true;
1268         }
1269         if (found || machine->trampolines_mapped)
1270                 return 0;
1271
1272         pgoff = find_entry_trampoline(kernel);
1273         if (!pgoff)
1274                 return 0;
1275
1276         nr_cpus_avail = machine__nr_cpus_avail(machine);
1277
1278         /* Add a 1 page map for each CPU's entry trampoline */
1279         for (cpu = 0; cpu < nr_cpus_avail; cpu++) {
1280                 u64 va = X86_64_CPU_ENTRY_AREA_PER_CPU +
1281                          cpu * X86_64_CPU_ENTRY_AREA_SIZE +
1282                          X86_64_ENTRY_TRAMPOLINE;
1283                 struct extra_kernel_map xm = {
1284                         .start = va,
1285                         .end   = va + page_size,
1286                         .pgoff = pgoff,
1287                 };
1288
1289                 strlcpy(xm.name, ENTRY_TRAMPOLINE_NAME, KMAP_NAME_LEN);
1290
1291                 if (machine__create_extra_kernel_map(machine, kernel, &xm) < 0)
1292                         return -1;
1293         }
1294
1295         machine->trampolines_mapped = nr_cpus_avail;
1296
1297         return 0;
1298 }
1299
1300 int __weak machine__create_extra_kernel_maps(struct machine *machine __maybe_unused,
1301                                              struct dso *kernel __maybe_unused)
1302 {
1303         return 0;
1304 }
1305
1306 static int
1307 __machine__create_kernel_maps(struct machine *machine, struct dso *kernel)
1308 {
1309         /* In case of renewal the kernel map, destroy previous one */
1310         machine__destroy_kernel_maps(machine);
1311
1312         machine->vmlinux_map = map__new2(0, kernel);
1313         if (machine->vmlinux_map == NULL)
1314                 return -1;
1315
1316         machine->vmlinux_map->map_ip = machine->vmlinux_map->unmap_ip = identity__map_ip;
1317         maps__insert(machine__kernel_maps(machine), machine->vmlinux_map);
1318         return 0;
1319 }
1320
1321 void machine__destroy_kernel_maps(struct machine *machine)
1322 {
1323         struct kmap *kmap;
1324         struct map *map = machine__kernel_map(machine);
1325
1326         if (map == NULL)
1327                 return;
1328
1329         kmap = map__kmap(map);
1330         maps__remove(machine__kernel_maps(machine), map);
1331         if (kmap && kmap->ref_reloc_sym) {
1332                 zfree((char **)&kmap->ref_reloc_sym->name);
1333                 zfree(&kmap->ref_reloc_sym);
1334         }
1335
1336         map__zput(machine->vmlinux_map);
1337 }
1338
1339 int machines__create_guest_kernel_maps(struct machines *machines)
1340 {
1341         int ret = 0;
1342         struct dirent **namelist = NULL;
1343         int i, items = 0;
1344         char path[PATH_MAX];
1345         pid_t pid;
1346         char *endp;
1347
1348         if (symbol_conf.default_guest_vmlinux_name ||
1349             symbol_conf.default_guest_modules ||
1350             symbol_conf.default_guest_kallsyms) {
1351                 machines__create_kernel_maps(machines, DEFAULT_GUEST_KERNEL_ID);
1352         }
1353
1354         if (symbol_conf.guestmount) {
1355                 items = scandir(symbol_conf.guestmount, &namelist, NULL, NULL);
1356                 if (items <= 0)
1357                         return -ENOENT;
1358                 for (i = 0; i < items; i++) {
1359                         if (!isdigit(namelist[i]->d_name[0])) {
1360                                 /* Filter out . and .. */
1361                                 continue;
1362                         }
1363                         pid = (pid_t)strtol(namelist[i]->d_name, &endp, 10);
1364                         if ((*endp != '\0') ||
1365                             (endp == namelist[i]->d_name) ||
1366                             (errno == ERANGE)) {
1367                                 pr_debug("invalid directory (%s). Skipping.\n",
1368                                          namelist[i]->d_name);
1369                                 continue;
1370                         }
1371                         sprintf(path, "%s/%s/proc/kallsyms",
1372                                 symbol_conf.guestmount,
1373                                 namelist[i]->d_name);
1374                         ret = access(path, R_OK);
1375                         if (ret) {
1376                                 pr_debug("Can't access file %s\n", path);
1377                                 goto failure;
1378                         }
1379                         machines__create_kernel_maps(machines, pid);
1380                 }
1381 failure:
1382                 free(namelist);
1383         }
1384
1385         return ret;
1386 }
1387
1388 void machines__destroy_kernel_maps(struct machines *machines)
1389 {
1390         struct rb_node *next = rb_first_cached(&machines->guests);
1391
1392         machine__destroy_kernel_maps(&machines->host);
1393
1394         while (next) {
1395                 struct machine *pos = rb_entry(next, struct machine, rb_node);
1396
1397                 next = rb_next(&pos->rb_node);
1398                 rb_erase_cached(&pos->rb_node, &machines->guests);
1399                 machine__delete(pos);
1400         }
1401 }
1402
1403 int machines__create_kernel_maps(struct machines *machines, pid_t pid)
1404 {
1405         struct machine *machine = machines__findnew(machines, pid);
1406
1407         if (machine == NULL)
1408                 return -1;
1409
1410         return machine__create_kernel_maps(machine);
1411 }
1412
1413 int machine__load_kallsyms(struct machine *machine, const char *filename)
1414 {
1415         struct map *map = machine__kernel_map(machine);
1416         int ret = __dso__load_kallsyms(map->dso, filename, map, true);
1417
1418         if (ret > 0) {
1419                 dso__set_loaded(map->dso);
1420                 /*
1421                  * Since /proc/kallsyms will have multiple sessions for the
1422                  * kernel, with modules between them, fixup the end of all
1423                  * sections.
1424                  */
1425                 maps__fixup_end(machine__kernel_maps(machine));
1426         }
1427
1428         return ret;
1429 }
1430
1431 int machine__load_vmlinux_path(struct machine *machine)
1432 {
1433         struct map *map = machine__kernel_map(machine);
1434         int ret = dso__load_vmlinux_path(map->dso, map);
1435
1436         if (ret > 0)
1437                 dso__set_loaded(map->dso);
1438
1439         return ret;
1440 }
1441
1442 static char *get_kernel_version(const char *root_dir)
1443 {
1444         char version[PATH_MAX];
1445         FILE *file;
1446         char *name, *tmp;
1447         const char *prefix = "Linux version ";
1448
1449         sprintf(version, "%s/proc/version", root_dir);
1450         file = fopen(version, "r");
1451         if (!file)
1452                 return NULL;
1453
1454         tmp = fgets(version, sizeof(version), file);
1455         fclose(file);
1456         if (!tmp)
1457                 return NULL;
1458
1459         name = strstr(version, prefix);
1460         if (!name)
1461                 return NULL;
1462         name += strlen(prefix);
1463         tmp = strchr(name, ' ');
1464         if (tmp)
1465                 *tmp = '\0';
1466
1467         return strdup(name);
1468 }
1469
1470 static bool is_kmod_dso(struct dso *dso)
1471 {
1472         return dso->symtab_type == DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE ||
1473                dso->symtab_type == DSO_BINARY_TYPE__GUEST_KMODULE;
1474 }
1475
1476 static int maps__set_module_path(struct maps *maps, const char *path, struct kmod_path *m)
1477 {
1478         char *long_name;
1479         struct map *map = maps__find_by_name(maps, m->name);
1480
1481         if (map == NULL)
1482                 return 0;
1483
1484         long_name = strdup(path);
1485         if (long_name == NULL)
1486                 return -ENOMEM;
1487
1488         dso__set_long_name(map->dso, long_name, true);
1489         dso__kernel_module_get_build_id(map->dso, "");
1490
1491         /*
1492          * Full name could reveal us kmod compression, so
1493          * we need to update the symtab_type if needed.
1494          */
1495         if (m->comp && is_kmod_dso(map->dso)) {
1496                 map->dso->symtab_type++;
1497                 map->dso->comp = m->comp;
1498         }
1499
1500         return 0;
1501 }
1502
1503 static int maps__set_modules_path_dir(struct maps *maps, const char *dir_name, int depth)
1504 {
1505         struct dirent *dent;
1506         DIR *dir = opendir(dir_name);
1507         int ret = 0;
1508
1509         if (!dir) {
1510                 pr_debug("%s: cannot open %s dir\n", __func__, dir_name);
1511                 return -1;
1512         }
1513
1514         while ((dent = readdir(dir)) != NULL) {
1515                 char path[PATH_MAX];
1516                 struct stat st;
1517
1518                 /*sshfs might return bad dent->d_type, so we have to stat*/
1519                 path__join(path, sizeof(path), dir_name, dent->d_name);
1520                 if (stat(path, &st))
1521                         continue;
1522
1523                 if (S_ISDIR(st.st_mode)) {
1524                         if (!strcmp(dent->d_name, ".") ||
1525                             !strcmp(dent->d_name, ".."))
1526                                 continue;
1527
1528                         /* Do not follow top-level source and build symlinks */
1529                         if (depth == 0) {
1530                                 if (!strcmp(dent->d_name, "source") ||
1531                                     !strcmp(dent->d_name, "build"))
1532                                         continue;
1533                         }
1534
1535                         ret = maps__set_modules_path_dir(maps, path, depth + 1);
1536                         if (ret < 0)
1537                                 goto out;
1538                 } else {
1539                         struct kmod_path m;
1540
1541                         ret = kmod_path__parse_name(&m, dent->d_name);
1542                         if (ret)
1543                                 goto out;
1544
1545                         if (m.kmod)
1546                                 ret = maps__set_module_path(maps, path, &m);
1547
1548                         zfree(&m.name);
1549
1550                         if (ret)
1551                                 goto out;
1552                 }
1553         }
1554
1555 out:
1556         closedir(dir);
1557         return ret;
1558 }
1559
1560 static int machine__set_modules_path(struct machine *machine)
1561 {
1562         char *version;
1563         char modules_path[PATH_MAX];
1564
1565         version = get_kernel_version(machine->root_dir);
1566         if (!version)
1567                 return -1;
1568
1569         snprintf(modules_path, sizeof(modules_path), "%s/lib/modules/%s",
1570                  machine->root_dir, version);
1571         free(version);
1572
1573         return maps__set_modules_path_dir(machine__kernel_maps(machine), modules_path, 0);
1574 }
1575 int __weak arch__fix_module_text_start(u64 *start __maybe_unused,
1576                                 u64 *size __maybe_unused,
1577                                 const char *name __maybe_unused)
1578 {
1579         return 0;
1580 }
1581
1582 static int machine__create_module(void *arg, const char *name, u64 start,
1583                                   u64 size)
1584 {
1585         struct machine *machine = arg;
1586         struct map *map;
1587
1588         if (arch__fix_module_text_start(&start, &size, name) < 0)
1589                 return -1;
1590
1591         map = machine__addnew_module_map(machine, start, name);
1592         if (map == NULL)
1593                 return -1;
1594         map->end = start + size;
1595
1596         dso__kernel_module_get_build_id(map->dso, machine->root_dir);
1597
1598         return 0;
1599 }
1600
1601 static int machine__create_modules(struct machine *machine)
1602 {
1603         const char *modules;
1604         char path[PATH_MAX];
1605
1606         if (machine__is_default_guest(machine)) {
1607                 modules = symbol_conf.default_guest_modules;
1608         } else {
1609                 snprintf(path, PATH_MAX, "%s/proc/modules", machine->root_dir);
1610                 modules = path;
1611         }
1612
1613         if (symbol__restricted_filename(modules, "/proc/modules"))
1614                 return -1;
1615
1616         if (modules__parse(modules, machine, machine__create_module))
1617                 return -1;
1618
1619         if (!machine__set_modules_path(machine))
1620                 return 0;
1621
1622         pr_debug("Problems setting modules path maps, continuing anyway...\n");
1623
1624         return 0;
1625 }
1626
1627 static void machine__set_kernel_mmap(struct machine *machine,
1628                                      u64 start, u64 end)
1629 {
1630         machine->vmlinux_map->start = start;
1631         machine->vmlinux_map->end   = end;
1632         /*
1633          * Be a bit paranoid here, some perf.data file came with
1634          * a zero sized synthesized MMAP event for the kernel.
1635          */
1636         if (start == 0 && end == 0)
1637                 machine->vmlinux_map->end = ~0ULL;
1638 }
1639
1640 static void machine__update_kernel_mmap(struct machine *machine,
1641                                      u64 start, u64 end)
1642 {
1643         struct map *map = machine__kernel_map(machine);
1644
1645         map__get(map);
1646         maps__remove(machine__kernel_maps(machine), map);
1647
1648         machine__set_kernel_mmap(machine, start, end);
1649
1650         maps__insert(machine__kernel_maps(machine), map);
1651         map__put(map);
1652 }
1653
1654 int machine__create_kernel_maps(struct machine *machine)
1655 {
1656         struct dso *kernel = machine__get_kernel(machine);
1657         const char *name = NULL;
1658         struct map *map;
1659         u64 start = 0, end = ~0ULL;
1660         int ret;
1661
1662         if (kernel == NULL)
1663                 return -1;
1664
1665         ret = __machine__create_kernel_maps(machine, kernel);
1666         if (ret < 0)
1667                 goto out_put;
1668
1669         if (symbol_conf.use_modules && machine__create_modules(machine) < 0) {
1670                 if (machine__is_host(machine))
1671                         pr_debug("Problems creating module maps, "
1672                                  "continuing anyway...\n");
1673                 else
1674                         pr_debug("Problems creating module maps for guest %d, "
1675                                  "continuing anyway...\n", machine->pid);
1676         }
1677
1678         if (!machine__get_running_kernel_start(machine, &name, &start, &end)) {
1679                 if (name &&
1680                     map__set_kallsyms_ref_reloc_sym(machine->vmlinux_map, name, start)) {
1681                         machine__destroy_kernel_maps(machine);
1682                         ret = -1;
1683                         goto out_put;
1684                 }
1685
1686                 /*
1687                  * we have a real start address now, so re-order the kmaps
1688                  * assume it's the last in the kmaps
1689                  */
1690                 machine__update_kernel_mmap(machine, start, end);
1691         }
1692
1693         if (machine__create_extra_kernel_maps(machine, kernel))
1694                 pr_debug("Problems creating extra kernel maps, continuing anyway...\n");
1695
1696         if (end == ~0ULL) {
1697                 /* update end address of the kernel map using adjacent module address */
1698                 map = map__next(machine__kernel_map(machine));
1699                 if (map)
1700                         machine__set_kernel_mmap(machine, start, map->start);
1701         }
1702
1703 out_put:
1704         dso__put(kernel);
1705         return ret;
1706 }
1707
1708 static bool machine__uses_kcore(struct machine *machine)
1709 {
1710         struct dso *dso;
1711
1712         list_for_each_entry(dso, &machine->dsos.head, node) {
1713                 if (dso__is_kcore(dso))
1714                         return true;
1715         }
1716
1717         return false;
1718 }
1719
1720 static bool perf_event__is_extra_kernel_mmap(struct machine *machine,
1721                                              struct extra_kernel_map *xm)
1722 {
1723         return machine__is(machine, "x86_64") &&
1724                is_entry_trampoline(xm->name);
1725 }
1726
1727 static int machine__process_extra_kernel_map(struct machine *machine,
1728                                              struct extra_kernel_map *xm)
1729 {
1730         struct dso *kernel = machine__kernel_dso(machine);
1731
1732         if (kernel == NULL)
1733                 return -1;
1734
1735         return machine__create_extra_kernel_map(machine, kernel, xm);
1736 }
1737
1738 static int machine__process_kernel_mmap_event(struct machine *machine,
1739                                               struct extra_kernel_map *xm,
1740                                               struct build_id *bid)
1741 {
1742         struct map *map;
1743         enum dso_space_type dso_space;
1744         bool is_kernel_mmap;
1745
1746         /* If we have maps from kcore then we do not need or want any others */
1747         if (machine__uses_kcore(machine))
1748                 return 0;
1749
1750         if (machine__is_host(machine))
1751                 dso_space = DSO_SPACE__KERNEL;
1752         else
1753                 dso_space = DSO_SPACE__KERNEL_GUEST;
1754
1755         is_kernel_mmap = memcmp(xm->name, machine->mmap_name,
1756                                 strlen(machine->mmap_name) - 1) == 0;
1757         if (xm->name[0] == '/' ||
1758             (!is_kernel_mmap && xm->name[0] == '[')) {
1759                 map = machine__addnew_module_map(machine, xm->start,
1760                                                  xm->name);
1761                 if (map == NULL)
1762                         goto out_problem;
1763
1764                 map->end = map->start + xm->end - xm->start;
1765
1766                 if (build_id__is_defined(bid))
1767                         dso__set_build_id(map->dso, bid);
1768
1769         } else if (is_kernel_mmap) {
1770                 const char *symbol_name = (xm->name + strlen(machine->mmap_name));
1771                 /*
1772                  * Should be there already, from the build-id table in
1773                  * the header.
1774                  */
1775                 struct dso *kernel = NULL;
1776                 struct dso *dso;
1777
1778                 down_read(&machine->dsos.lock);
1779
1780                 list_for_each_entry(dso, &machine->dsos.head, node) {
1781
1782                         /*
1783                          * The cpumode passed to is_kernel_module is not the
1784                          * cpumode of *this* event. If we insist on passing
1785                          * correct cpumode to is_kernel_module, we should
1786                          * record the cpumode when we adding this dso to the
1787                          * linked list.
1788                          *
1789                          * However we don't really need passing correct
1790                          * cpumode.  We know the correct cpumode must be kernel
1791                          * mode (if not, we should not link it onto kernel_dsos
1792                          * list).
1793                          *
1794                          * Therefore, we pass PERF_RECORD_MISC_CPUMODE_UNKNOWN.
1795                          * is_kernel_module() treats it as a kernel cpumode.
1796                          */
1797
1798                         if (!dso->kernel ||
1799                             is_kernel_module(dso->long_name,
1800                                              PERF_RECORD_MISC_CPUMODE_UNKNOWN))
1801                                 continue;
1802
1803
1804                         kernel = dso;
1805                         break;
1806                 }
1807
1808                 up_read(&machine->dsos.lock);
1809
1810                 if (kernel == NULL)
1811                         kernel = machine__findnew_dso(machine, machine->mmap_name);
1812                 if (kernel == NULL)
1813                         goto out_problem;
1814
1815                 kernel->kernel = dso_space;
1816                 if (__machine__create_kernel_maps(machine, kernel) < 0) {
1817                         dso__put(kernel);
1818                         goto out_problem;
1819                 }
1820
1821                 if (strstr(kernel->long_name, "vmlinux"))
1822                         dso__set_short_name(kernel, "[kernel.vmlinux]", false);
1823
1824                 machine__update_kernel_mmap(machine, xm->start, xm->end);
1825
1826                 if (build_id__is_defined(bid))
1827                         dso__set_build_id(kernel, bid);
1828
1829                 /*
1830                  * Avoid using a zero address (kptr_restrict) for the ref reloc
1831                  * symbol. Effectively having zero here means that at record
1832                  * time /proc/sys/kernel/kptr_restrict was non zero.
1833                  */
1834                 if (xm->pgoff != 0) {
1835                         map__set_kallsyms_ref_reloc_sym(machine->vmlinux_map,
1836                                                         symbol_name,
1837                                                         xm->pgoff);
1838                 }
1839
1840                 if (machine__is_default_guest(machine)) {
1841                         /*
1842                          * preload dso of guest kernel and modules
1843                          */
1844                         dso__load(kernel, machine__kernel_map(machine));
1845                 }
1846         } else if (perf_event__is_extra_kernel_mmap(machine, xm)) {
1847                 return machine__process_extra_kernel_map(machine, xm);
1848         }
1849         return 0;
1850 out_problem:
1851         return -1;
1852 }
1853
1854 int machine__process_mmap2_event(struct machine *machine,
1855                                  union perf_event *event,
1856                                  struct perf_sample *sample)
1857 {
1858         struct thread *thread;
1859         struct map *map;
1860         struct dso_id dso_id = {
1861                 .maj = event->mmap2.maj,
1862                 .min = event->mmap2.min,
1863                 .ino = event->mmap2.ino,
1864                 .ino_generation = event->mmap2.ino_generation,
1865         };
1866         struct build_id __bid, *bid = NULL;
1867         int ret = 0;
1868
1869         if (dump_trace)
1870                 perf_event__fprintf_mmap2(event, stdout);
1871
1872         if (event->header.misc & PERF_RECORD_MISC_MMAP_BUILD_ID) {
1873                 bid = &__bid;
1874                 build_id__init(bid, event->mmap2.build_id, event->mmap2.build_id_size);
1875         }
1876
1877         if (sample->cpumode == PERF_RECORD_MISC_GUEST_KERNEL ||
1878             sample->cpumode == PERF_RECORD_MISC_KERNEL) {
1879                 struct extra_kernel_map xm = {
1880                         .start = event->mmap2.start,
1881                         .end   = event->mmap2.start + event->mmap2.len,
1882                         .pgoff = event->mmap2.pgoff,
1883                 };
1884
1885                 strlcpy(xm.name, event->mmap2.filename, KMAP_NAME_LEN);
1886                 ret = machine__process_kernel_mmap_event(machine, &xm, bid);
1887                 if (ret < 0)
1888                         goto out_problem;
1889                 return 0;
1890         }
1891
1892         thread = machine__findnew_thread(machine, event->mmap2.pid,
1893                                         event->mmap2.tid);
1894         if (thread == NULL)
1895                 goto out_problem;
1896
1897         map = map__new(machine, event->mmap2.start,
1898                         event->mmap2.len, event->mmap2.pgoff,
1899                         &dso_id, event->mmap2.prot,
1900                         event->mmap2.flags, bid,
1901                         event->mmap2.filename, thread);
1902
1903         if (map == NULL)
1904                 goto out_problem_map;
1905
1906         ret = thread__insert_map(thread, map);
1907         if (ret)
1908                 goto out_problem_insert;
1909
1910         thread__put(thread);
1911         map__put(map);
1912         return 0;
1913
1914 out_problem_insert:
1915         map__put(map);
1916 out_problem_map:
1917         thread__put(thread);
1918 out_problem:
1919         dump_printf("problem processing PERF_RECORD_MMAP2, skipping event.\n");
1920         return 0;
1921 }
1922
1923 int machine__process_mmap_event(struct machine *machine, union perf_event *event,
1924                                 struct perf_sample *sample)
1925 {
1926         struct thread *thread;
1927         struct map *map;
1928         u32 prot = 0;
1929         int ret = 0;
1930
1931         if (dump_trace)
1932                 perf_event__fprintf_mmap(event, stdout);
1933
1934         if (sample->cpumode == PERF_RECORD_MISC_GUEST_KERNEL ||
1935             sample->cpumode == PERF_RECORD_MISC_KERNEL) {
1936                 struct extra_kernel_map xm = {
1937                         .start = event->mmap.start,
1938                         .end   = event->mmap.start + event->mmap.len,
1939                         .pgoff = event->mmap.pgoff,
1940                 };
1941
1942                 strlcpy(xm.name, event->mmap.filename, KMAP_NAME_LEN);
1943                 ret = machine__process_kernel_mmap_event(machine, &xm, NULL);
1944                 if (ret < 0)
1945                         goto out_problem;
1946                 return 0;
1947         }
1948
1949         thread = machine__findnew_thread(machine, event->mmap.pid,
1950                                          event->mmap.tid);
1951         if (thread == NULL)
1952                 goto out_problem;
1953
1954         if (!(event->header.misc & PERF_RECORD_MISC_MMAP_DATA))
1955                 prot = PROT_EXEC;
1956
1957         map = map__new(machine, event->mmap.start,
1958                         event->mmap.len, event->mmap.pgoff,
1959                         NULL, prot, 0, NULL, event->mmap.filename, thread);
1960
1961         if (map == NULL)
1962                 goto out_problem_map;
1963
1964         ret = thread__insert_map(thread, map);
1965         if (ret)
1966                 goto out_problem_insert;
1967
1968         thread__put(thread);
1969         map__put(map);
1970         return 0;
1971
1972 out_problem_insert:
1973         map__put(map);
1974 out_problem_map:
1975         thread__put(thread);
1976 out_problem:
1977         dump_printf("problem processing PERF_RECORD_MMAP, skipping event.\n");
1978         return 0;
1979 }
1980
1981 static void __machine__remove_thread(struct machine *machine, struct thread *th, bool lock)
1982 {
1983         struct threads *threads = machine__threads(machine, th->tid);
1984
1985         if (threads->last_match == th)
1986                 threads__set_last_match(threads, NULL);
1987
1988         if (lock)
1989                 down_write(&threads->lock);
1990
1991         BUG_ON(refcount_read(&th->refcnt) == 0);
1992
1993         rb_erase_cached(&th->rb_node, &threads->entries);
1994         RB_CLEAR_NODE(&th->rb_node);
1995         --threads->nr;
1996         /*
1997          * Move it first to the dead_threads list, then drop the reference,
1998          * if this is the last reference, then the thread__delete destructor
1999          * will be called and we will remove it from the dead_threads list.
2000          */
2001         list_add_tail(&th->node, &threads->dead);
2002
2003         /*
2004          * We need to do the put here because if this is the last refcount,
2005          * then we will be touching the threads->dead head when removing the
2006          * thread.
2007          */
2008         thread__put(th);
2009
2010         if (lock)
2011                 up_write(&threads->lock);
2012 }
2013
2014 void machine__remove_thread(struct machine *machine, struct thread *th)
2015 {
2016         return __machine__remove_thread(machine, th, true);
2017 }
2018
2019 int machine__process_fork_event(struct machine *machine, union perf_event *event,
2020                                 struct perf_sample *sample)
2021 {
2022         struct thread *thread = machine__find_thread(machine,
2023                                                      event->fork.pid,
2024                                                      event->fork.tid);
2025         struct thread *parent = machine__findnew_thread(machine,
2026                                                         event->fork.ppid,
2027                                                         event->fork.ptid);
2028         bool do_maps_clone = true;
2029         int err = 0;
2030
2031         if (dump_trace)
2032                 perf_event__fprintf_task(event, stdout);
2033
2034         /*
2035          * There may be an existing thread that is not actually the parent,
2036          * either because we are processing events out of order, or because the
2037          * (fork) event that would have removed the thread was lost. Assume the
2038          * latter case and continue on as best we can.
2039          */
2040         if (parent->pid_ != (pid_t)event->fork.ppid) {
2041                 dump_printf("removing erroneous parent thread %d/%d\n",
2042                             parent->pid_, parent->tid);
2043                 machine__remove_thread(machine, parent);
2044                 thread__put(parent);
2045                 parent = machine__findnew_thread(machine, event->fork.ppid,
2046                                                  event->fork.ptid);
2047         }
2048
2049         /* if a thread currently exists for the thread id remove it */
2050         if (thread != NULL) {
2051                 machine__remove_thread(machine, thread);
2052                 thread__put(thread);
2053         }
2054
2055         thread = machine__findnew_thread(machine, event->fork.pid,
2056                                          event->fork.tid);
2057         /*
2058          * When synthesizing FORK events, we are trying to create thread
2059          * objects for the already running tasks on the machine.
2060          *
2061          * Normally, for a kernel FORK event, we want to clone the parent's
2062          * maps because that is what the kernel just did.
2063          *
2064          * But when synthesizing, this should not be done.  If we do, we end up
2065          * with overlapping maps as we process the synthesized MMAP2 events that
2066          * get delivered shortly thereafter.
2067          *
2068          * Use the FORK event misc flags in an internal way to signal this
2069          * situation, so we can elide the map clone when appropriate.
2070          */
2071         if (event->fork.header.misc & PERF_RECORD_MISC_FORK_EXEC)
2072                 do_maps_clone = false;
2073
2074         if (thread == NULL || parent == NULL ||
2075             thread__fork(thread, parent, sample->time, do_maps_clone) < 0) {
2076                 dump_printf("problem processing PERF_RECORD_FORK, skipping event.\n");
2077                 err = -1;
2078         }
2079         thread__put(thread);
2080         thread__put(parent);
2081
2082         return err;
2083 }
2084
2085 int machine__process_exit_event(struct machine *machine, union perf_event *event,
2086                                 struct perf_sample *sample __maybe_unused)
2087 {
2088         struct thread *thread = machine__find_thread(machine,
2089                                                      event->fork.pid,
2090                                                      event->fork.tid);
2091
2092         if (dump_trace)
2093                 perf_event__fprintf_task(event, stdout);
2094
2095         if (thread != NULL) {
2096                 thread__exited(thread);
2097                 thread__put(thread);
2098         }
2099
2100         return 0;
2101 }
2102
2103 int machine__process_event(struct machine *machine, union perf_event *event,
2104                            struct perf_sample *sample)
2105 {
2106         int ret;
2107
2108         switch (event->header.type) {
2109         case PERF_RECORD_COMM:
2110                 ret = machine__process_comm_event(machine, event, sample); break;
2111         case PERF_RECORD_MMAP:
2112                 ret = machine__process_mmap_event(machine, event, sample); break;
2113         case PERF_RECORD_NAMESPACES:
2114                 ret = machine__process_namespaces_event(machine, event, sample); break;
2115         case PERF_RECORD_CGROUP:
2116                 ret = machine__process_cgroup_event(machine, event, sample); break;
2117         case PERF_RECORD_MMAP2:
2118                 ret = machine__process_mmap2_event(machine, event, sample); break;
2119         case PERF_RECORD_FORK:
2120                 ret = machine__process_fork_event(machine, event, sample); break;
2121         case PERF_RECORD_EXIT:
2122                 ret = machine__process_exit_event(machine, event, sample); break;
2123         case PERF_RECORD_LOST:
2124                 ret = machine__process_lost_event(machine, event, sample); break;
2125         case PERF_RECORD_AUX:
2126                 ret = machine__process_aux_event(machine, event); break;
2127         case PERF_RECORD_ITRACE_START:
2128                 ret = machine__process_itrace_start_event(machine, event); break;
2129         case PERF_RECORD_LOST_SAMPLES:
2130                 ret = machine__process_lost_samples_event(machine, event, sample); break;
2131         case PERF_RECORD_SWITCH:
2132         case PERF_RECORD_SWITCH_CPU_WIDE:
2133                 ret = machine__process_switch_event(machine, event); break;
2134         case PERF_RECORD_KSYMBOL:
2135                 ret = machine__process_ksymbol(machine, event, sample); break;
2136         case PERF_RECORD_BPF_EVENT:
2137                 ret = machine__process_bpf(machine, event, sample); break;
2138         case PERF_RECORD_TEXT_POKE:
2139                 ret = machine__process_text_poke(machine, event, sample); break;
2140         case PERF_RECORD_AUX_OUTPUT_HW_ID:
2141                 ret = machine__process_aux_output_hw_id_event(machine, event); break;
2142         default:
2143                 ret = -1;
2144                 break;
2145         }
2146
2147         return ret;
2148 }
2149
2150 static bool symbol__match_regex(struct symbol *sym, regex_t *regex)
2151 {
2152         if (!regexec(regex, sym->name, 0, NULL, 0))
2153                 return true;
2154         return false;
2155 }
2156
2157 static void ip__resolve_ams(struct thread *thread,
2158                             struct addr_map_symbol *ams,
2159                             u64 ip)
2160 {
2161         struct addr_location al;
2162
2163         memset(&al, 0, sizeof(al));
2164         /*
2165          * We cannot use the header.misc hint to determine whether a
2166          * branch stack address is user, kernel, guest, hypervisor.
2167          * Branches may straddle the kernel/user/hypervisor boundaries.
2168          * Thus, we have to try consecutively until we find a match
2169          * or else, the symbol is unknown
2170          */
2171         thread__find_cpumode_addr_location(thread, ip, &al);
2172
2173         ams->addr = ip;
2174         ams->al_addr = al.addr;
2175         ams->al_level = al.level;
2176         ams->ms.maps = al.maps;
2177         ams->ms.sym = al.sym;
2178         ams->ms.map = al.map;
2179         ams->phys_addr = 0;
2180         ams->data_page_size = 0;
2181 }
2182
2183 static void ip__resolve_data(struct thread *thread,
2184                              u8 m, struct addr_map_symbol *ams,
2185                              u64 addr, u64 phys_addr, u64 daddr_page_size)
2186 {
2187         struct addr_location al;
2188
2189         memset(&al, 0, sizeof(al));
2190
2191         thread__find_symbol(thread, m, addr, &al);
2192
2193         ams->addr = addr;
2194         ams->al_addr = al.addr;
2195         ams->al_level = al.level;
2196         ams->ms.maps = al.maps;
2197         ams->ms.sym = al.sym;
2198         ams->ms.map = al.map;
2199         ams->phys_addr = phys_addr;
2200         ams->data_page_size = daddr_page_size;
2201 }
2202
2203 struct mem_info *sample__resolve_mem(struct perf_sample *sample,
2204                                      struct addr_location *al)
2205 {
2206         struct mem_info *mi = mem_info__new();
2207
2208         if (!mi)
2209                 return NULL;
2210
2211         ip__resolve_ams(al->thread, &mi->iaddr, sample->ip);
2212         ip__resolve_data(al->thread, al->cpumode, &mi->daddr,
2213                          sample->addr, sample->phys_addr,
2214                          sample->data_page_size);
2215         mi->data_src.val = sample->data_src;
2216
2217         return mi;
2218 }
2219
2220 static char *callchain_srcline(struct map_symbol *ms, u64 ip)
2221 {
2222         struct map *map = ms->map;
2223         char *srcline = NULL;
2224
2225         if (!map || callchain_param.key == CCKEY_FUNCTION)
2226                 return srcline;
2227
2228         srcline = srcline__tree_find(&map->dso->srclines, ip);
2229         if (!srcline) {
2230                 bool show_sym = false;
2231                 bool show_addr = callchain_param.key == CCKEY_ADDRESS;
2232
2233                 srcline = get_srcline(map->dso, map__rip_2objdump(map, ip),
2234                                       ms->sym, show_sym, show_addr, ip);
2235                 srcline__tree_insert(&map->dso->srclines, ip, srcline);
2236         }
2237
2238         return srcline;
2239 }
2240
2241 struct iterations {
2242         int nr_loop_iter;
2243         u64 cycles;
2244 };
2245
2246 static int add_callchain_ip(struct thread *thread,
2247                             struct callchain_cursor *cursor,
2248                             struct symbol **parent,
2249                             struct addr_location *root_al,
2250                             u8 *cpumode,
2251                             u64 ip,
2252                             bool branch,
2253                             struct branch_flags *flags,
2254                             struct iterations *iter,
2255                             u64 branch_from)
2256 {
2257         struct map_symbol ms;
2258         struct addr_location al;
2259         int nr_loop_iter = 0;
2260         u64 iter_cycles = 0;
2261         const char *srcline = NULL;
2262
2263         al.filtered = 0;
2264         al.sym = NULL;
2265         al.srcline = NULL;
2266         if (!cpumode) {
2267                 thread__find_cpumode_addr_location(thread, ip, &al);
2268         } else {
2269                 if (ip >= PERF_CONTEXT_MAX) {
2270                         switch (ip) {
2271                         case PERF_CONTEXT_HV:
2272                                 *cpumode = PERF_RECORD_MISC_HYPERVISOR;
2273                                 break;
2274                         case PERF_CONTEXT_KERNEL:
2275                                 *cpumode = PERF_RECORD_MISC_KERNEL;
2276                                 break;
2277                         case PERF_CONTEXT_USER:
2278                                 *cpumode = PERF_RECORD_MISC_USER;
2279                                 break;
2280                         default:
2281                                 pr_debug("invalid callchain context: "
2282                                          "%"PRId64"\n", (s64) ip);
2283                                 /*
2284                                  * It seems the callchain is corrupted.
2285                                  * Discard all.
2286                                  */
2287                                 callchain_cursor_reset(cursor);
2288                                 return 1;
2289                         }
2290                         return 0;
2291                 }
2292                 thread__find_symbol(thread, *cpumode, ip, &al);
2293         }
2294
2295         if (al.sym != NULL) {
2296                 if (perf_hpp_list.parent && !*parent &&
2297                     symbol__match_regex(al.sym, &parent_regex))
2298                         *parent = al.sym;
2299                 else if (have_ignore_callees && root_al &&
2300                   symbol__match_regex(al.sym, &ignore_callees_regex)) {
2301                         /* Treat this symbol as the root,
2302                            forgetting its callees. */
2303                         *root_al = al;
2304                         callchain_cursor_reset(cursor);
2305                 }
2306         }
2307
2308         if (symbol_conf.hide_unresolved && al.sym == NULL)
2309                 return 0;
2310
2311         if (iter) {
2312                 nr_loop_iter = iter->nr_loop_iter;
2313                 iter_cycles = iter->cycles;
2314         }
2315
2316         ms.maps = al.maps;
2317         ms.map = al.map;
2318         ms.sym = al.sym;
2319         srcline = callchain_srcline(&ms, al.addr);
2320         return callchain_cursor_append(cursor, ip, &ms,
2321                                        branch, flags, nr_loop_iter,
2322                                        iter_cycles, branch_from, srcline);
2323 }
2324
2325 struct branch_info *sample__resolve_bstack(struct perf_sample *sample,
2326                                            struct addr_location *al)
2327 {
2328         unsigned int i;
2329         const struct branch_stack *bs = sample->branch_stack;
2330         struct branch_entry *entries = perf_sample__branch_entries(sample);
2331         struct branch_info *bi = calloc(bs->nr, sizeof(struct branch_info));
2332
2333         if (!bi)
2334                 return NULL;
2335
2336         for (i = 0; i < bs->nr; i++) {
2337                 ip__resolve_ams(al->thread, &bi[i].to, entries[i].to);
2338                 ip__resolve_ams(al->thread, &bi[i].from, entries[i].from);
2339                 bi[i].flags = entries[i].flags;
2340         }
2341         return bi;
2342 }
2343
2344 static void save_iterations(struct iterations *iter,
2345                             struct branch_entry *be, int nr)
2346 {
2347         int i;
2348
2349         iter->nr_loop_iter++;
2350         iter->cycles = 0;
2351
2352         for (i = 0; i < nr; i++)
2353                 iter->cycles += be[i].flags.cycles;
2354 }
2355
2356 #define CHASHSZ 127
2357 #define CHASHBITS 7
2358 #define NO_ENTRY 0xff
2359
2360 #define PERF_MAX_BRANCH_DEPTH 127
2361
2362 /* Remove loops. */
2363 static int remove_loops(struct branch_entry *l, int nr,
2364                         struct iterations *iter)
2365 {
2366         int i, j, off;
2367         unsigned char chash[CHASHSZ];
2368
2369         memset(chash, NO_ENTRY, sizeof(chash));
2370
2371         BUG_ON(PERF_MAX_BRANCH_DEPTH > 255);
2372
2373         for (i = 0; i < nr; i++) {
2374                 int h = hash_64(l[i].from, CHASHBITS) % CHASHSZ;
2375
2376                 /* no collision handling for now */
2377                 if (chash[h] == NO_ENTRY) {
2378                         chash[h] = i;
2379                 } else if (l[chash[h]].from == l[i].from) {
2380                         bool is_loop = true;
2381                         /* check if it is a real loop */
2382                         off = 0;
2383                         for (j = chash[h]; j < i && i + off < nr; j++, off++)
2384                                 if (l[j].from != l[i + off].from) {
2385                                         is_loop = false;
2386                                         break;
2387                                 }
2388                         if (is_loop) {
2389                                 j = nr - (i + off);
2390                                 if (j > 0) {
2391                                         save_iterations(iter + i + off,
2392                                                 l + i, off);
2393
2394                                         memmove(iter + i, iter + i + off,
2395                                                 j * sizeof(*iter));
2396
2397                                         memmove(l + i, l + i + off,
2398                                                 j * sizeof(*l));
2399                                 }
2400
2401                                 nr -= off;
2402                         }
2403                 }
2404         }
2405         return nr;
2406 }
2407
2408 static int lbr_callchain_add_kernel_ip(struct thread *thread,
2409                                        struct callchain_cursor *cursor,
2410                                        struct perf_sample *sample,
2411                                        struct symbol **parent,
2412                                        struct addr_location *root_al,
2413                                        u64 branch_from,
2414                                        bool callee, int end)
2415 {
2416         struct ip_callchain *chain = sample->callchain;
2417         u8 cpumode = PERF_RECORD_MISC_USER;
2418         int err, i;
2419
2420         if (callee) {
2421                 for (i = 0; i < end + 1; i++) {
2422                         err = add_callchain_ip(thread, cursor, parent,
2423                                                root_al, &cpumode, chain->ips[i],
2424                                                false, NULL, NULL, branch_from);
2425                         if (err)
2426                                 return err;
2427                 }
2428                 return 0;
2429         }
2430
2431         for (i = end; i >= 0; i--) {
2432                 err = add_callchain_ip(thread, cursor, parent,
2433                                        root_al, &cpumode, chain->ips[i],
2434                                        false, NULL, NULL, branch_from);
2435                 if (err)
2436                         return err;
2437         }
2438
2439         return 0;
2440 }
2441
2442 static void save_lbr_cursor_node(struct thread *thread,
2443                                  struct callchain_cursor *cursor,
2444                                  int idx)
2445 {
2446         struct lbr_stitch *lbr_stitch = thread->lbr_stitch;
2447
2448         if (!lbr_stitch)
2449                 return;
2450
2451         if (cursor->pos == cursor->nr) {
2452                 lbr_stitch->prev_lbr_cursor[idx].valid = false;
2453                 return;
2454         }
2455
2456         if (!cursor->curr)
2457                 cursor->curr = cursor->first;
2458         else
2459                 cursor->curr = cursor->curr->next;
2460         memcpy(&lbr_stitch->prev_lbr_cursor[idx], cursor->curr,
2461                sizeof(struct callchain_cursor_node));
2462
2463         lbr_stitch->prev_lbr_cursor[idx].valid = true;
2464         cursor->pos++;
2465 }
2466
2467 static int lbr_callchain_add_lbr_ip(struct thread *thread,
2468                                     struct callchain_cursor *cursor,
2469                                     struct perf_sample *sample,
2470                                     struct symbol **parent,
2471                                     struct addr_location *root_al,
2472                                     u64 *branch_from,
2473                                     bool callee)
2474 {
2475         struct branch_stack *lbr_stack = sample->branch_stack;
2476         struct branch_entry *entries = perf_sample__branch_entries(sample);
2477         u8 cpumode = PERF_RECORD_MISC_USER;
2478         int lbr_nr = lbr_stack->nr;
2479         struct branch_flags *flags;
2480         int err, i;
2481         u64 ip;
2482
2483         /*
2484          * The curr and pos are not used in writing session. They are cleared
2485          * in callchain_cursor_commit() when the writing session is closed.
2486          * Using curr and pos to track the current cursor node.
2487          */
2488         if (thread->lbr_stitch) {
2489                 cursor->curr = NULL;
2490                 cursor->pos = cursor->nr;
2491                 if (cursor->nr) {
2492                         cursor->curr = cursor->first;
2493                         for (i = 0; i < (int)(cursor->nr - 1); i++)
2494                                 cursor->curr = cursor->curr->next;
2495                 }
2496         }
2497
2498         if (callee) {
2499                 /* Add LBR ip from first entries.to */
2500                 ip = entries[0].to;
2501                 flags = &entries[0].flags;
2502                 *branch_from = entries[0].from;
2503                 err = add_callchain_ip(thread, cursor, parent,
2504                                        root_al, &cpumode, ip,
2505                                        true, flags, NULL,
2506                                        *branch_from);
2507                 if (err)
2508                         return err;
2509
2510                 /*
2511                  * The number of cursor node increases.
2512                  * Move the current cursor node.
2513                  * But does not need to save current cursor node for entry 0.
2514                  * It's impossible to stitch the whole LBRs of previous sample.
2515                  */
2516                 if (thread->lbr_stitch && (cursor->pos != cursor->nr)) {
2517                         if (!cursor->curr)
2518                                 cursor->curr = cursor->first;
2519                         else
2520                                 cursor->curr = cursor->curr->next;
2521                         cursor->pos++;
2522                 }
2523
2524                 /* Add LBR ip from entries.from one by one. */
2525                 for (i = 0; i < lbr_nr; i++) {
2526                         ip = entries[i].from;
2527                         flags = &entries[i].flags;
2528                         err = add_callchain_ip(thread, cursor, parent,
2529                                                root_al, &cpumode, ip,
2530                                                true, flags, NULL,
2531                                                *branch_from);
2532                         if (err)
2533                                 return err;
2534                         save_lbr_cursor_node(thread, cursor, i);
2535                 }
2536                 return 0;
2537         }
2538
2539         /* Add LBR ip from entries.from one by one. */
2540         for (i = lbr_nr - 1; i >= 0; i--) {
2541                 ip = entries[i].from;
2542                 flags = &entries[i].flags;
2543                 err = add_callchain_ip(thread, cursor, parent,
2544                                        root_al, &cpumode, ip,
2545                                        true, flags, NULL,
2546                                        *branch_from);
2547                 if (err)
2548                         return err;
2549                 save_lbr_cursor_node(thread, cursor, i);
2550         }
2551
2552         /* Add LBR ip from first entries.to */
2553         ip = entries[0].to;
2554         flags = &entries[0].flags;
2555         *branch_from = entries[0].from;
2556         err = add_callchain_ip(thread, cursor, parent,
2557                                root_al, &cpumode, ip,
2558                                true, flags, NULL,
2559                                *branch_from);
2560         if (err)
2561                 return err;
2562
2563         return 0;
2564 }
2565
2566 static int lbr_callchain_add_stitched_lbr_ip(struct thread *thread,
2567                                              struct callchain_cursor *cursor)
2568 {
2569         struct lbr_stitch *lbr_stitch = thread->lbr_stitch;
2570         struct callchain_cursor_node *cnode;
2571         struct stitch_list *stitch_node;
2572         int err;
2573
2574         list_for_each_entry(stitch_node, &lbr_stitch->lists, node) {
2575                 cnode = &stitch_node->cursor;
2576
2577                 err = callchain_cursor_append(cursor, cnode->ip,
2578                                               &cnode->ms,
2579                                               cnode->branch,
2580                                               &cnode->branch_flags,
2581                                               cnode->nr_loop_iter,
2582                                               cnode->iter_cycles,
2583                                               cnode->branch_from,
2584                                               cnode->srcline);
2585                 if (err)
2586                         return err;
2587         }
2588         return 0;
2589 }
2590
2591 static struct stitch_list *get_stitch_node(struct thread *thread)
2592 {
2593         struct lbr_stitch *lbr_stitch = thread->lbr_stitch;
2594         struct stitch_list *stitch_node;
2595
2596         if (!list_empty(&lbr_stitch->free_lists)) {
2597                 stitch_node = list_first_entry(&lbr_stitch->free_lists,
2598                                                struct stitch_list, node);
2599                 list_del(&stitch_node->node);
2600
2601                 return stitch_node;
2602         }
2603
2604         return malloc(sizeof(struct stitch_list));
2605 }
2606
2607 static bool has_stitched_lbr(struct thread *thread,
2608                              struct perf_sample *cur,
2609                              struct perf_sample *prev,
2610                              unsigned int max_lbr,
2611                              bool callee)
2612 {
2613         struct branch_stack *cur_stack = cur->branch_stack;
2614         struct branch_entry *cur_entries = perf_sample__branch_entries(cur);
2615         struct branch_stack *prev_stack = prev->branch_stack;
2616         struct branch_entry *prev_entries = perf_sample__branch_entries(prev);
2617         struct lbr_stitch *lbr_stitch = thread->lbr_stitch;
2618         int i, j, nr_identical_branches = 0;
2619         struct stitch_list *stitch_node;
2620         u64 cur_base, distance;
2621
2622         if (!cur_stack || !prev_stack)
2623                 return false;
2624
2625         /* Find the physical index of the base-of-stack for current sample. */
2626         cur_base = max_lbr - cur_stack->nr + cur_stack->hw_idx + 1;
2627
2628         distance = (prev_stack->hw_idx > cur_base) ? (prev_stack->hw_idx - cur_base) :
2629                                                      (max_lbr + prev_stack->hw_idx - cur_base);
2630         /* Previous sample has shorter stack. Nothing can be stitched. */
2631         if (distance + 1 > prev_stack->nr)
2632                 return false;
2633
2634         /*
2635          * Check if there are identical LBRs between two samples.
2636          * Identical LBRs must have same from, to and flags values. Also,
2637          * they have to be saved in the same LBR registers (same physical
2638          * index).
2639          *
2640          * Starts from the base-of-stack of current sample.
2641          */
2642         for (i = distance, j = cur_stack->nr - 1; (i >= 0) && (j >= 0); i--, j--) {
2643                 if ((prev_entries[i].from != cur_entries[j].from) ||
2644                     (prev_entries[i].to != cur_entries[j].to) ||
2645                     (prev_entries[i].flags.value != cur_entries[j].flags.value))
2646                         break;
2647                 nr_identical_branches++;
2648         }
2649
2650         if (!nr_identical_branches)
2651                 return false;
2652
2653         /*
2654          * Save the LBRs between the base-of-stack of previous sample
2655          * and the base-of-stack of current sample into lbr_stitch->lists.
2656          * These LBRs will be stitched later.
2657          */
2658         for (i = prev_stack->nr - 1; i > (int)distance; i--) {
2659
2660                 if (!lbr_stitch->prev_lbr_cursor[i].valid)
2661                         continue;
2662
2663                 stitch_node = get_stitch_node(thread);
2664                 if (!stitch_node)
2665                         return false;
2666
2667                 memcpy(&stitch_node->cursor, &lbr_stitch->prev_lbr_cursor[i],
2668                        sizeof(struct callchain_cursor_node));
2669
2670                 if (callee)
2671                         list_add(&stitch_node->node, &lbr_stitch->lists);
2672                 else
2673                         list_add_tail(&stitch_node->node, &lbr_stitch->lists);
2674         }
2675
2676         return true;
2677 }
2678
2679 static bool alloc_lbr_stitch(struct thread *thread, unsigned int max_lbr)
2680 {
2681         if (thread->lbr_stitch)
2682                 return true;
2683
2684         thread->lbr_stitch = zalloc(sizeof(*thread->lbr_stitch));
2685         if (!thread->lbr_stitch)
2686                 goto err;
2687
2688         thread->lbr_stitch->prev_lbr_cursor = calloc(max_lbr + 1, sizeof(struct callchain_cursor_node));
2689         if (!thread->lbr_stitch->prev_lbr_cursor)
2690                 goto free_lbr_stitch;
2691
2692         INIT_LIST_HEAD(&thread->lbr_stitch->lists);
2693         INIT_LIST_HEAD(&thread->lbr_stitch->free_lists);
2694
2695         return true;
2696
2697 free_lbr_stitch:
2698         zfree(&thread->lbr_stitch);
2699 err:
2700         pr_warning("Failed to allocate space for stitched LBRs. Disable LBR stitch\n");
2701         thread->lbr_stitch_enable = false;
2702         return false;
2703 }
2704
2705 /*
2706  * Resolve LBR callstack chain sample
2707  * Return:
2708  * 1 on success get LBR callchain information
2709  * 0 no available LBR callchain information, should try fp
2710  * negative error code on other errors.
2711  */
2712 static int resolve_lbr_callchain_sample(struct thread *thread,
2713                                         struct callchain_cursor *cursor,
2714                                         struct perf_sample *sample,
2715                                         struct symbol **parent,
2716                                         struct addr_location *root_al,
2717                                         int max_stack,
2718                                         unsigned int max_lbr)
2719 {
2720         bool callee = (callchain_param.order == ORDER_CALLEE);
2721         struct ip_callchain *chain = sample->callchain;
2722         int chain_nr = min(max_stack, (int)chain->nr), i;
2723         struct lbr_stitch *lbr_stitch;
2724         bool stitched_lbr = false;
2725         u64 branch_from = 0;
2726         int err;
2727
2728         for (i = 0; i < chain_nr; i++) {
2729                 if (chain->ips[i] == PERF_CONTEXT_USER)
2730                         break;
2731         }
2732
2733         /* LBR only affects the user callchain */
2734         if (i == chain_nr)
2735                 return 0;
2736
2737         if (thread->lbr_stitch_enable && !sample->no_hw_idx &&
2738             (max_lbr > 0) && alloc_lbr_stitch(thread, max_lbr)) {
2739                 lbr_stitch = thread->lbr_stitch;
2740
2741                 stitched_lbr = has_stitched_lbr(thread, sample,
2742                                                 &lbr_stitch->prev_sample,
2743                                                 max_lbr, callee);
2744
2745                 if (!stitched_lbr && !list_empty(&lbr_stitch->lists)) {
2746                         list_replace_init(&lbr_stitch->lists,
2747                                           &lbr_stitch->free_lists);
2748                 }
2749                 memcpy(&lbr_stitch->prev_sample, sample, sizeof(*sample));
2750         }
2751
2752         if (callee) {
2753                 /* Add kernel ip */
2754                 err = lbr_callchain_add_kernel_ip(thread, cursor, sample,
2755                                                   parent, root_al, branch_from,
2756                                                   true, i);
2757                 if (err)
2758                         goto error;
2759
2760                 err = lbr_callchain_add_lbr_ip(thread, cursor, sample, parent,
2761                                                root_al, &branch_from, true);
2762                 if (err)
2763                         goto error;
2764
2765                 if (stitched_lbr) {
2766                         err = lbr_callchain_add_stitched_lbr_ip(thread, cursor);
2767                         if (err)
2768                                 goto error;
2769                 }
2770
2771         } else {
2772                 if (stitched_lbr) {
2773                         err = lbr_callchain_add_stitched_lbr_ip(thread, cursor);
2774                         if (err)
2775                                 goto error;
2776                 }
2777                 err = lbr_callchain_add_lbr_ip(thread, cursor, sample, parent,
2778                                                root_al, &branch_from, false);
2779                 if (err)
2780                         goto error;
2781
2782                 /* Add kernel ip */
2783                 err = lbr_callchain_add_kernel_ip(thread, cursor, sample,
2784                                                   parent, root_al, branch_from,
2785                                                   false, i);
2786                 if (err)
2787                         goto error;
2788         }
2789         return 1;
2790
2791 error:
2792         return (err < 0) ? err : 0;
2793 }
2794
2795 static int find_prev_cpumode(struct ip_callchain *chain, struct thread *thread,
2796                              struct callchain_cursor *cursor,
2797                              struct symbol **parent,
2798                              struct addr_location *root_al,
2799                              u8 *cpumode, int ent)
2800 {
2801         int err = 0;
2802
2803         while (--ent >= 0) {
2804                 u64 ip = chain->ips[ent];
2805
2806                 if (ip >= PERF_CONTEXT_MAX) {
2807                         err = add_callchain_ip(thread, cursor, parent,
2808                                                root_al, cpumode, ip,
2809                                                false, NULL, NULL, 0);
2810                         break;
2811                 }
2812         }
2813         return err;
2814 }
2815
2816 static u64 get_leaf_frame_caller(struct perf_sample *sample,
2817                 struct thread *thread, int usr_idx)
2818 {
2819         if (machine__normalized_is(thread->maps->machine, "arm64"))
2820                 return get_leaf_frame_caller_aarch64(sample, thread, usr_idx);
2821         else
2822                 return 0;
2823 }
2824
2825 static int thread__resolve_callchain_sample(struct thread *thread,
2826                                             struct callchain_cursor *cursor,
2827                                             struct evsel *evsel,
2828                                             struct perf_sample *sample,
2829                                             struct symbol **parent,
2830                                             struct addr_location *root_al,
2831                                             int max_stack)
2832 {
2833         struct branch_stack *branch = sample->branch_stack;
2834         struct branch_entry *entries = perf_sample__branch_entries(sample);
2835         struct ip_callchain *chain = sample->callchain;
2836         int chain_nr = 0;
2837         u8 cpumode = PERF_RECORD_MISC_USER;
2838         int i, j, err, nr_entries, usr_idx;
2839         int skip_idx = -1;
2840         int first_call = 0;
2841         u64 leaf_frame_caller;
2842
2843         if (chain)
2844                 chain_nr = chain->nr;
2845
2846         if (evsel__has_branch_callstack(evsel)) {
2847                 struct perf_env *env = evsel__env(evsel);
2848
2849                 err = resolve_lbr_callchain_sample(thread, cursor, sample, parent,
2850                                                    root_al, max_stack,
2851                                                    !env ? 0 : env->max_branches);
2852                 if (err)
2853                         return (err < 0) ? err : 0;
2854         }
2855
2856         /*
2857          * Based on DWARF debug information, some architectures skip
2858          * a callchain entry saved by the kernel.
2859          */
2860         skip_idx = arch_skip_callchain_idx(thread, chain);
2861
2862         /*
2863          * Add branches to call stack for easier browsing. This gives
2864          * more context for a sample than just the callers.
2865          *
2866          * This uses individual histograms of paths compared to the
2867          * aggregated histograms the normal LBR mode uses.
2868          *
2869          * Limitations for now:
2870          * - No extra filters
2871          * - No annotations (should annotate somehow)
2872          */
2873
2874         if (branch && callchain_param.branch_callstack) {
2875                 int nr = min(max_stack, (int)branch->nr);
2876                 struct branch_entry be[nr];
2877                 struct iterations iter[nr];
2878
2879                 if (branch->nr > PERF_MAX_BRANCH_DEPTH) {
2880                         pr_warning("corrupted branch chain. skipping...\n");
2881                         goto check_calls;
2882                 }
2883
2884                 for (i = 0; i < nr; i++) {
2885                         if (callchain_param.order == ORDER_CALLEE) {
2886                                 be[i] = entries[i];
2887
2888                                 if (chain == NULL)
2889                                         continue;
2890
2891                                 /*
2892                                  * Check for overlap into the callchain.
2893                                  * The return address is one off compared to
2894                                  * the branch entry. To adjust for this
2895                                  * assume the calling instruction is not longer
2896                                  * than 8 bytes.
2897                                  */
2898                                 if (i == skip_idx ||
2899                                     chain->ips[first_call] >= PERF_CONTEXT_MAX)
2900                                         first_call++;
2901                                 else if (be[i].from < chain->ips[first_call] &&
2902                                     be[i].from >= chain->ips[first_call] - 8)
2903                                         first_call++;
2904                         } else
2905                                 be[i] = entries[branch->nr - i - 1];
2906                 }
2907
2908                 memset(iter, 0, sizeof(struct iterations) * nr);
2909                 nr = remove_loops(be, nr, iter);
2910
2911                 for (i = 0; i < nr; i++) {
2912                         err = add_callchain_ip(thread, cursor, parent,
2913                                                root_al,
2914                                                NULL, be[i].to,
2915                                                true, &be[i].flags,
2916                                                NULL, be[i].from);
2917
2918                         if (!err)
2919                                 err = add_callchain_ip(thread, cursor, parent, root_al,
2920                                                        NULL, be[i].from,
2921                                                        true, &be[i].flags,
2922                                                        &iter[i], 0);
2923                         if (err == -EINVAL)
2924                                 break;
2925                         if (err)
2926                                 return err;
2927                 }
2928
2929                 if (chain_nr == 0)
2930                         return 0;
2931
2932                 chain_nr -= nr;
2933         }
2934
2935 check_calls:
2936         if (chain && callchain_param.order != ORDER_CALLEE) {
2937                 err = find_prev_cpumode(chain, thread, cursor, parent, root_al,
2938                                         &cpumode, chain->nr - first_call);
2939                 if (err)
2940                         return (err < 0) ? err : 0;
2941         }
2942         for (i = first_call, nr_entries = 0;
2943              i < chain_nr && nr_entries < max_stack; i++) {
2944                 u64 ip;
2945
2946                 if (callchain_param.order == ORDER_CALLEE)
2947                         j = i;
2948                 else
2949                         j = chain->nr - i - 1;
2950
2951 #ifdef HAVE_SKIP_CALLCHAIN_IDX
2952                 if (j == skip_idx)
2953                         continue;
2954 #endif
2955                 ip = chain->ips[j];
2956                 if (ip < PERF_CONTEXT_MAX)
2957                        ++nr_entries;
2958                 else if (callchain_param.order != ORDER_CALLEE) {
2959                         err = find_prev_cpumode(chain, thread, cursor, parent,
2960                                                 root_al, &cpumode, j);
2961                         if (err)
2962                                 return (err < 0) ? err : 0;
2963                         continue;
2964                 }
2965
2966                 /*
2967                  * PERF_CONTEXT_USER allows us to locate where the user stack ends.
2968                  * Depending on callchain_param.order and the position of PERF_CONTEXT_USER,
2969                  * the index will be different in order to add the missing frame
2970                  * at the right place.
2971                  */
2972
2973                 usr_idx = callchain_param.order == ORDER_CALLEE ? j-2 : j-1;
2974
2975                 if (usr_idx >= 0 && chain->ips[usr_idx] == PERF_CONTEXT_USER) {
2976
2977                         leaf_frame_caller = get_leaf_frame_caller(sample, thread, usr_idx);
2978
2979                         /*
2980                          * check if leaf_frame_Caller != ip to not add the same
2981                          * value twice.
2982                          */
2983
2984                         if (leaf_frame_caller && leaf_frame_caller != ip) {
2985
2986                                 err = add_callchain_ip(thread, cursor, parent,
2987                                                root_al, &cpumode, leaf_frame_caller,
2988                                                false, NULL, NULL, 0);
2989                                 if (err)
2990                                         return (err < 0) ? err : 0;
2991                         }
2992                 }
2993
2994                 err = add_callchain_ip(thread, cursor, parent,
2995                                        root_al, &cpumode, ip,
2996                                        false, NULL, NULL, 0);
2997
2998                 if (err)
2999                         return (err < 0) ? err : 0;
3000         }
3001
3002         return 0;
3003 }
3004
3005 static int append_inlines(struct callchain_cursor *cursor, struct map_symbol *ms, u64 ip)
3006 {
3007         struct symbol *sym = ms->sym;
3008         struct map *map = ms->map;
3009         struct inline_node *inline_node;
3010         struct inline_list *ilist;
3011         u64 addr;
3012         int ret = 1;
3013
3014         if (!symbol_conf.inline_name || !map || !sym)
3015                 return ret;
3016
3017         addr = map__map_ip(map, ip);
3018         addr = map__rip_2objdump(map, addr);
3019
3020         inline_node = inlines__tree_find(&map->dso->inlined_nodes, addr);
3021         if (!inline_node) {
3022                 inline_node = dso__parse_addr_inlines(map->dso, addr, sym);
3023                 if (!inline_node)
3024                         return ret;
3025                 inlines__tree_insert(&map->dso->inlined_nodes, inline_node);
3026         }
3027
3028         list_for_each_entry(ilist, &inline_node->val, list) {
3029                 struct map_symbol ilist_ms = {
3030                         .maps = ms->maps,
3031                         .map = map,
3032                         .sym = ilist->symbol,
3033                 };
3034                 ret = callchain_cursor_append(cursor, ip, &ilist_ms, false,
3035                                               NULL, 0, 0, 0, ilist->srcline);
3036
3037                 if (ret != 0)
3038                         return ret;
3039         }
3040
3041         return ret;
3042 }
3043
3044 static int unwind_entry(struct unwind_entry *entry, void *arg)
3045 {
3046         struct callchain_cursor *cursor = arg;
3047         const char *srcline = NULL;
3048         u64 addr = entry->ip;
3049
3050         if (symbol_conf.hide_unresolved && entry->ms.sym == NULL)
3051                 return 0;
3052
3053         if (append_inlines(cursor, &entry->ms, entry->ip) == 0)
3054                 return 0;
3055
3056         /*
3057          * Convert entry->ip from a virtual address to an offset in
3058          * its corresponding binary.
3059          */
3060         if (entry->ms.map)
3061                 addr = map__map_ip(entry->ms.map, entry->ip);
3062
3063         srcline = callchain_srcline(&entry->ms, addr);
3064         return callchain_cursor_append(cursor, entry->ip, &entry->ms,
3065                                        false, NULL, 0, 0, 0, srcline);
3066 }
3067
3068 static int thread__resolve_callchain_unwind(struct thread *thread,
3069                                             struct callchain_cursor *cursor,
3070                                             struct evsel *evsel,
3071                                             struct perf_sample *sample,
3072                                             int max_stack)
3073 {
3074         /* Can we do dwarf post unwind? */
3075         if (!((evsel->core.attr.sample_type & PERF_SAMPLE_REGS_USER) &&
3076               (evsel->core.attr.sample_type & PERF_SAMPLE_STACK_USER)))
3077                 return 0;
3078
3079         /* Bail out if nothing was captured. */
3080         if ((!sample->user_regs.regs) ||
3081             (!sample->user_stack.size))
3082                 return 0;
3083
3084         return unwind__get_entries(unwind_entry, cursor,
3085                                    thread, sample, max_stack, false);
3086 }
3087
3088 int thread__resolve_callchain(struct thread *thread,
3089                               struct callchain_cursor *cursor,
3090                               struct evsel *evsel,
3091                               struct perf_sample *sample,
3092                               struct symbol **parent,
3093                               struct addr_location *root_al,
3094                               int max_stack)
3095 {
3096         int ret = 0;
3097
3098         callchain_cursor_reset(cursor);
3099
3100         if (callchain_param.order == ORDER_CALLEE) {
3101                 ret = thread__resolve_callchain_sample(thread, cursor,
3102                                                        evsel, sample,
3103                                                        parent, root_al,
3104                                                        max_stack);
3105                 if (ret)
3106                         return ret;
3107                 ret = thread__resolve_callchain_unwind(thread, cursor,
3108                                                        evsel, sample,
3109                                                        max_stack);
3110         } else {
3111                 ret = thread__resolve_callchain_unwind(thread, cursor,
3112                                                        evsel, sample,
3113                                                        max_stack);
3114                 if (ret)
3115                         return ret;
3116                 ret = thread__resolve_callchain_sample(thread, cursor,
3117                                                        evsel, sample,
3118                                                        parent, root_al,
3119                                                        max_stack);
3120         }
3121
3122         return ret;
3123 }
3124
3125 int machine__for_each_thread(struct machine *machine,
3126                              int (*fn)(struct thread *thread, void *p),
3127                              void *priv)
3128 {
3129         struct threads *threads;
3130         struct rb_node *nd;
3131         struct thread *thread;
3132         int rc = 0;
3133         int i;
3134
3135         for (i = 0; i < THREADS__TABLE_SIZE; i++) {
3136                 threads = &machine->threads[i];
3137                 for (nd = rb_first_cached(&threads->entries); nd;
3138                      nd = rb_next(nd)) {
3139                         thread = rb_entry(nd, struct thread, rb_node);
3140                         rc = fn(thread, priv);
3141                         if (rc != 0)
3142                                 return rc;
3143                 }
3144
3145                 list_for_each_entry(thread, &threads->dead, node) {
3146                         rc = fn(thread, priv);
3147                         if (rc != 0)
3148                                 return rc;
3149                 }
3150         }
3151         return rc;
3152 }
3153
3154 int machines__for_each_thread(struct machines *machines,
3155                               int (*fn)(struct thread *thread, void *p),
3156                               void *priv)
3157 {
3158         struct rb_node *nd;
3159         int rc = 0;
3160
3161         rc = machine__for_each_thread(&machines->host, fn, priv);
3162         if (rc != 0)
3163                 return rc;
3164
3165         for (nd = rb_first_cached(&machines->guests); nd; nd = rb_next(nd)) {
3166                 struct machine *machine = rb_entry(nd, struct machine, rb_node);
3167
3168                 rc = machine__for_each_thread(machine, fn, priv);
3169                 if (rc != 0)
3170                         return rc;
3171         }
3172         return rc;
3173 }
3174
3175 pid_t machine__get_current_tid(struct machine *machine, int cpu)
3176 {
3177         int nr_cpus = min(machine->env->nr_cpus_avail, MAX_NR_CPUS);
3178
3179         if (cpu < 0 || cpu >= nr_cpus || !machine->current_tid)
3180                 return -1;
3181
3182         return machine->current_tid[cpu];
3183 }
3184
3185 int machine__set_current_tid(struct machine *machine, int cpu, pid_t pid,
3186                              pid_t tid)
3187 {
3188         struct thread *thread;
3189         int nr_cpus = min(machine->env->nr_cpus_avail, MAX_NR_CPUS);
3190
3191         if (cpu < 0)
3192                 return -EINVAL;
3193
3194         if (!machine->current_tid) {
3195                 int i;
3196
3197                 machine->current_tid = calloc(nr_cpus, sizeof(pid_t));
3198                 if (!machine->current_tid)
3199                         return -ENOMEM;
3200                 for (i = 0; i < nr_cpus; i++)
3201                         machine->current_tid[i] = -1;
3202         }
3203
3204         if (cpu >= nr_cpus) {
3205                 pr_err("Requested CPU %d too large. ", cpu);
3206                 pr_err("Consider raising MAX_NR_CPUS\n");
3207                 return -EINVAL;
3208         }
3209
3210         machine->current_tid[cpu] = tid;
3211
3212         thread = machine__findnew_thread(machine, pid, tid);
3213         if (!thread)
3214                 return -ENOMEM;
3215
3216         thread->cpu = cpu;
3217         thread__put(thread);
3218
3219         return 0;
3220 }
3221
3222 /*
3223  * Compares the raw arch string. N.B. see instead perf_env__arch() or
3224  * machine__normalized_is() if a normalized arch is needed.
3225  */
3226 bool machine__is(struct machine *machine, const char *arch)
3227 {
3228         return machine && !strcmp(perf_env__raw_arch(machine->env), arch);
3229 }
3230
3231 bool machine__normalized_is(struct machine *machine, const char *arch)
3232 {
3233         return machine && !strcmp(perf_env__arch(machine->env), arch);
3234 }
3235
3236 int machine__nr_cpus_avail(struct machine *machine)
3237 {
3238         return machine ? perf_env__nr_cpus_avail(machine->env) : 0;
3239 }
3240
3241 int machine__get_kernel_start(struct machine *machine)
3242 {
3243         struct map *map = machine__kernel_map(machine);
3244         int err = 0;
3245
3246         /*
3247          * The only addresses above 2^63 are kernel addresses of a 64-bit
3248          * kernel.  Note that addresses are unsigned so that on a 32-bit system
3249          * all addresses including kernel addresses are less than 2^32.  In
3250          * that case (32-bit system), if the kernel mapping is unknown, all
3251          * addresses will be assumed to be in user space - see
3252          * machine__kernel_ip().
3253          */
3254         machine->kernel_start = 1ULL << 63;
3255         if (map) {
3256                 err = map__load(map);
3257                 /*
3258                  * On x86_64, PTI entry trampolines are less than the
3259                  * start of kernel text, but still above 2^63. So leave
3260                  * kernel_start = 1ULL << 63 for x86_64.
3261                  */
3262                 if (!err && !machine__is(machine, "x86_64"))
3263                         machine->kernel_start = map->start;
3264         }
3265         return err;
3266 }
3267
3268 u8 machine__addr_cpumode(struct machine *machine, u8 cpumode, u64 addr)
3269 {
3270         u8 addr_cpumode = cpumode;
3271         bool kernel_ip;
3272
3273         if (!machine->single_address_space)
3274                 goto out;
3275
3276         kernel_ip = machine__kernel_ip(machine, addr);
3277         switch (cpumode) {
3278         case PERF_RECORD_MISC_KERNEL:
3279         case PERF_RECORD_MISC_USER:
3280                 addr_cpumode = kernel_ip ? PERF_RECORD_MISC_KERNEL :
3281                                            PERF_RECORD_MISC_USER;
3282                 break;
3283         case PERF_RECORD_MISC_GUEST_KERNEL:
3284         case PERF_RECORD_MISC_GUEST_USER:
3285                 addr_cpumode = kernel_ip ? PERF_RECORD_MISC_GUEST_KERNEL :
3286                                            PERF_RECORD_MISC_GUEST_USER;
3287                 break;
3288         default:
3289                 break;
3290         }
3291 out:
3292         return addr_cpumode;
3293 }
3294
3295 struct dso *machine__findnew_dso_id(struct machine *machine, const char *filename, struct dso_id *id)
3296 {
3297         return dsos__findnew_id(&machine->dsos, filename, id);
3298 }
3299
3300 struct dso *machine__findnew_dso(struct machine *machine, const char *filename)
3301 {
3302         return machine__findnew_dso_id(machine, filename, NULL);
3303 }
3304
3305 char *machine__resolve_kernel_addr(void *vmachine, unsigned long long *addrp, char **modp)
3306 {
3307         struct machine *machine = vmachine;
3308         struct map *map;
3309         struct symbol *sym = machine__find_kernel_symbol(machine, *addrp, &map);
3310
3311         if (sym == NULL)
3312                 return NULL;
3313
3314         *modp = __map__is_kmodule(map) ? (char *)map->dso->short_name : NULL;
3315         *addrp = map->unmap_ip(map, sym->start);
3316         return sym->name;
3317 }
3318
3319 int machine__for_each_dso(struct machine *machine, machine__dso_t fn, void *priv)
3320 {
3321         struct dso *pos;
3322         int err = 0;
3323
3324         list_for_each_entry(pos, &machine->dsos.head, node) {
3325                 if (fn(pos, machine, priv))
3326                         err = -1;
3327         }
3328         return err;
3329 }