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