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