]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blame - tools/perf/util/machine.c
perf tools: Remove filter parameter of thread__find_addr_location()
[mirror_ubuntu-zesty-kernel.git] / tools / perf / util / machine.c
CommitLineData
3f067dca 1#include "callchain.h"
b0a7d1a0
ACM
2#include "debug.h"
3#include "event.h"
3f067dca
ACM
4#include "evsel.h"
5#include "hist.h"
9d2f8e22
ACM
6#include "machine.h"
7#include "map.h"
3f067dca 8#include "sort.h"
69d2591a 9#include "strlist.h"
9d2f8e22
ACM
10#include "thread.h"
11#include <stdbool.h>
3f067dca 12#include "unwind.h"
9d2f8e22 13
69d2591a
ACM
14int machine__init(struct machine *machine, const char *root_dir, pid_t pid)
15{
16 map_groups__init(&machine->kmaps);
17 RB_CLEAR_NODE(&machine->rb_node);
18 INIT_LIST_HEAD(&machine->user_dsos);
19 INIT_LIST_HEAD(&machine->kernel_dsos);
20
21 machine->threads = RB_ROOT;
22 INIT_LIST_HEAD(&machine->dead_threads);
23 machine->last_match = NULL;
24
25 machine->kmaps.machine = machine;
26 machine->pid = pid;
27
611a5ce8
AH
28 machine->symbol_filter = NULL;
29
69d2591a
ACM
30 machine->root_dir = strdup(root_dir);
31 if (machine->root_dir == NULL)
32 return -ENOMEM;
33
34 if (pid != HOST_KERNEL_ID) {
35 struct thread *thread = machine__findnew_thread(machine, pid);
36 char comm[64];
37
38 if (thread == NULL)
39 return -ENOMEM;
40
41 snprintf(comm, sizeof(comm), "[guest/%d]", pid);
42 thread__set_comm(thread, comm);
43 }
44
45 return 0;
46}
47
48static void dsos__delete(struct list_head *dsos)
49{
50 struct dso *pos, *n;
51
52 list_for_each_entry_safe(pos, n, dsos, node) {
53 list_del(&pos->node);
54 dso__delete(pos);
55 }
56}
57
3f067dca
ACM
58void machine__delete_dead_threads(struct machine *machine)
59{
60 struct thread *n, *t;
61
62 list_for_each_entry_safe(t, n, &machine->dead_threads, node) {
63 list_del(&t->node);
64 thread__delete(t);
65 }
66}
67
68void machine__delete_threads(struct machine *machine)
69{
70 struct rb_node *nd = rb_first(&machine->threads);
71
72 while (nd) {
73 struct thread *t = rb_entry(nd, struct thread, rb_node);
74
75 rb_erase(&t->rb_node, &machine->threads);
76 nd = rb_next(nd);
77 thread__delete(t);
78 }
79}
80
69d2591a
ACM
81void machine__exit(struct machine *machine)
82{
83 map_groups__exit(&machine->kmaps);
84 dsos__delete(&machine->user_dsos);
85 dsos__delete(&machine->kernel_dsos);
86 free(machine->root_dir);
87 machine->root_dir = NULL;
88}
89
90void machine__delete(struct machine *machine)
91{
92 machine__exit(machine);
93 free(machine);
94}
95
876650e6
ACM
96void machines__init(struct machines *machines)
97{
98 machine__init(&machines->host, "", HOST_KERNEL_ID);
99 machines->guests = RB_ROOT;
611a5ce8 100 machines->symbol_filter = NULL;
876650e6
ACM
101}
102
103void machines__exit(struct machines *machines)
104{
105 machine__exit(&machines->host);
106 /* XXX exit guest */
107}
108
109struct machine *machines__add(struct machines *machines, pid_t pid,
69d2591a
ACM
110 const char *root_dir)
111{
876650e6 112 struct rb_node **p = &machines->guests.rb_node;
69d2591a
ACM
113 struct rb_node *parent = NULL;
114 struct machine *pos, *machine = malloc(sizeof(*machine));
115
116 if (machine == NULL)
117 return NULL;
118
119 if (machine__init(machine, root_dir, pid) != 0) {
120 free(machine);
121 return NULL;
122 }
123
611a5ce8
AH
124 machine->symbol_filter = machines->symbol_filter;
125
69d2591a
ACM
126 while (*p != NULL) {
127 parent = *p;
128 pos = rb_entry(parent, struct machine, rb_node);
129 if (pid < pos->pid)
130 p = &(*p)->rb_left;
131 else
132 p = &(*p)->rb_right;
133 }
134
135 rb_link_node(&machine->rb_node, parent, p);
876650e6 136 rb_insert_color(&machine->rb_node, &machines->guests);
69d2591a
ACM
137
138 return machine;
139}
140
611a5ce8
AH
141void machines__set_symbol_filter(struct machines *machines,
142 symbol_filter_t symbol_filter)
143{
144 struct rb_node *nd;
145
146 machines->symbol_filter = symbol_filter;
147 machines->host.symbol_filter = symbol_filter;
148
149 for (nd = rb_first(&machines->guests); nd; nd = rb_next(nd)) {
150 struct machine *machine = rb_entry(nd, struct machine, rb_node);
151
152 machine->symbol_filter = symbol_filter;
153 }
154}
155
876650e6 156struct machine *machines__find(struct machines *machines, pid_t pid)
69d2591a 157{
876650e6 158 struct rb_node **p = &machines->guests.rb_node;
69d2591a
ACM
159 struct rb_node *parent = NULL;
160 struct machine *machine;
161 struct machine *default_machine = NULL;
162
876650e6
ACM
163 if (pid == HOST_KERNEL_ID)
164 return &machines->host;
165
69d2591a
ACM
166 while (*p != NULL) {
167 parent = *p;
168 machine = rb_entry(parent, struct machine, rb_node);
169 if (pid < machine->pid)
170 p = &(*p)->rb_left;
171 else if (pid > machine->pid)
172 p = &(*p)->rb_right;
173 else
174 return machine;
175 if (!machine->pid)
176 default_machine = machine;
177 }
178
179 return default_machine;
180}
181
876650e6 182struct machine *machines__findnew(struct machines *machines, pid_t pid)
69d2591a
ACM
183{
184 char path[PATH_MAX];
185 const char *root_dir = "";
186 struct machine *machine = machines__find(machines, pid);
187
188 if (machine && (machine->pid == pid))
189 goto out;
190
191 if ((pid != HOST_KERNEL_ID) &&
192 (pid != DEFAULT_GUEST_KERNEL_ID) &&
193 (symbol_conf.guestmount)) {
194 sprintf(path, "%s/%d", symbol_conf.guestmount, pid);
195 if (access(path, R_OK)) {
196 static struct strlist *seen;
197
198 if (!seen)
199 seen = strlist__new(true, NULL);
200
201 if (!strlist__has_entry(seen, path)) {
202 pr_err("Can't access file %s\n", path);
203 strlist__add(seen, path);
204 }
205 machine = NULL;
206 goto out;
207 }
208 root_dir = path;
209 }
210
211 machine = machines__add(machines, pid, root_dir);
212out:
213 return machine;
214}
215
876650e6
ACM
216void machines__process_guests(struct machines *machines,
217 machine__process_t process, void *data)
69d2591a
ACM
218{
219 struct rb_node *nd;
220
876650e6 221 for (nd = rb_first(&machines->guests); nd; nd = rb_next(nd)) {
69d2591a
ACM
222 struct machine *pos = rb_entry(nd, struct machine, rb_node);
223 process(pos, data);
224 }
225}
226
227char *machine__mmap_name(struct machine *machine, char *bf, size_t size)
228{
229 if (machine__is_host(machine))
230 snprintf(bf, size, "[%s]", "kernel.kallsyms");
231 else if (machine__is_default_guest(machine))
232 snprintf(bf, size, "[%s]", "guest.kernel.kallsyms");
233 else {
234 snprintf(bf, size, "[%s.%d]", "guest.kernel.kallsyms",
235 machine->pid);
236 }
237
238 return bf;
239}
240
876650e6 241void machines__set_id_hdr_size(struct machines *machines, u16 id_hdr_size)
69d2591a
ACM
242{
243 struct rb_node *node;
244 struct machine *machine;
245
876650e6
ACM
246 machines->host.id_hdr_size = id_hdr_size;
247
248 for (node = rb_first(&machines->guests); node; node = rb_next(node)) {
69d2591a
ACM
249 machine = rb_entry(node, struct machine, rb_node);
250 machine->id_hdr_size = id_hdr_size;
251 }
252
253 return;
254}
255
38051234 256static struct thread *__machine__findnew_thread(struct machine *machine, pid_t tid,
9d2f8e22
ACM
257 bool create)
258{
259 struct rb_node **p = &machine->threads.rb_node;
260 struct rb_node *parent = NULL;
261 struct thread *th;
262
263 /*
38051234 264 * Front-end cache - TID lookups come in blocks,
9d2f8e22
ACM
265 * so most of the time we dont have to look up
266 * the full rbtree:
267 */
38051234 268 if (machine->last_match && machine->last_match->tid == tid)
9d2f8e22
ACM
269 return machine->last_match;
270
271 while (*p != NULL) {
272 parent = *p;
273 th = rb_entry(parent, struct thread, rb_node);
274
38051234 275 if (th->tid == tid) {
9d2f8e22
ACM
276 machine->last_match = th;
277 return th;
278 }
279
38051234 280 if (tid < th->tid)
9d2f8e22
ACM
281 p = &(*p)->rb_left;
282 else
283 p = &(*p)->rb_right;
284 }
285
286 if (!create)
287 return NULL;
288
38051234 289 th = thread__new(tid);
9d2f8e22
ACM
290 if (th != NULL) {
291 rb_link_node(&th->rb_node, parent, p);
292 rb_insert_color(&th->rb_node, &machine->threads);
293 machine->last_match = th;
294 }
295
296 return th;
297}
298
38051234 299struct thread *machine__findnew_thread(struct machine *machine, pid_t tid)
9d2f8e22 300{
38051234 301 return __machine__findnew_thread(machine, tid, true);
9d2f8e22
ACM
302}
303
38051234 304struct thread *machine__find_thread(struct machine *machine, pid_t tid)
9d2f8e22 305{
38051234 306 return __machine__findnew_thread(machine, tid, false);
9d2f8e22 307}
b0a7d1a0
ACM
308
309int machine__process_comm_event(struct machine *machine, union perf_event *event)
310{
311 struct thread *thread = machine__findnew_thread(machine, event->comm.tid);
312
313 if (dump_trace)
314 perf_event__fprintf_comm(event, stdout);
315
316 if (thread == NULL || thread__set_comm(thread, event->comm.comm)) {
317 dump_printf("problem processing PERF_RECORD_COMM, skipping event.\n");
318 return -1;
319 }
320
321 return 0;
322}
323
324int machine__process_lost_event(struct machine *machine __maybe_unused,
325 union perf_event *event)
326{
327 dump_printf(": id:%" PRIu64 ": lost:%" PRIu64 "\n",
328 event->lost.id, event->lost.lost);
329 return 0;
330}
331
3f067dca
ACM
332struct map *machine__new_module(struct machine *machine, u64 start,
333 const char *filename)
334{
335 struct map *map;
336 struct dso *dso = __dsos__findnew(&machine->kernel_dsos, filename);
337
338 if (dso == NULL)
339 return NULL;
340
341 map = map__new2(start, dso, MAP__FUNCTION);
342 if (map == NULL)
343 return NULL;
344
345 if (machine__is_host(machine))
346 dso->symtab_type = DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE;
347 else
348 dso->symtab_type = DSO_BINARY_TYPE__GUEST_KMODULE;
349 map_groups__insert(&machine->kmaps, map);
350 return map;
351}
352
876650e6 353size_t machines__fprintf_dsos(struct machines *machines, FILE *fp)
3f067dca
ACM
354{
355 struct rb_node *nd;
876650e6
ACM
356 size_t ret = __dsos__fprintf(&machines->host.kernel_dsos, fp) +
357 __dsos__fprintf(&machines->host.user_dsos, fp);
3f067dca 358
876650e6 359 for (nd = rb_first(&machines->guests); nd; nd = rb_next(nd)) {
3f067dca
ACM
360 struct machine *pos = rb_entry(nd, struct machine, rb_node);
361 ret += __dsos__fprintf(&pos->kernel_dsos, fp);
362 ret += __dsos__fprintf(&pos->user_dsos, fp);
363 }
364
365 return ret;
366}
367
368size_t machine__fprintf_dsos_buildid(struct machine *machine, FILE *fp,
369 bool (skip)(struct dso *dso, int parm), int parm)
370{
371 return __dsos__fprintf_buildid(&machine->kernel_dsos, fp, skip, parm) +
372 __dsos__fprintf_buildid(&machine->user_dsos, fp, skip, parm);
373}
374
876650e6 375size_t machines__fprintf_dsos_buildid(struct machines *machines, FILE *fp,
3f067dca
ACM
376 bool (skip)(struct dso *dso, int parm), int parm)
377{
378 struct rb_node *nd;
876650e6 379 size_t ret = machine__fprintf_dsos_buildid(&machines->host, fp, skip, parm);
3f067dca 380
876650e6 381 for (nd = rb_first(&machines->guests); nd; nd = rb_next(nd)) {
3f067dca
ACM
382 struct machine *pos = rb_entry(nd, struct machine, rb_node);
383 ret += machine__fprintf_dsos_buildid(pos, fp, skip, parm);
384 }
385 return ret;
386}
387
388size_t machine__fprintf_vmlinux_path(struct machine *machine, FILE *fp)
389{
390 int i;
391 size_t printed = 0;
392 struct dso *kdso = machine->vmlinux_maps[MAP__FUNCTION]->dso;
393
394 if (kdso->has_build_id) {
395 char filename[PATH_MAX];
396 if (dso__build_id_filename(kdso, filename, sizeof(filename)))
397 printed += fprintf(fp, "[0] %s\n", filename);
398 }
399
400 for (i = 0; i < vmlinux_path__nr_entries; ++i)
401 printed += fprintf(fp, "[%d] %s\n",
402 i + kdso->has_build_id, vmlinux_path[i]);
403
404 return printed;
405}
406
407size_t machine__fprintf(struct machine *machine, FILE *fp)
408{
409 size_t ret = 0;
410 struct rb_node *nd;
411
412 for (nd = rb_first(&machine->threads); nd; nd = rb_next(nd)) {
413 struct thread *pos = rb_entry(nd, struct thread, rb_node);
414
415 ret += thread__fprintf(pos, fp);
416 }
417
418 return ret;
419}
420
421static struct dso *machine__get_kernel(struct machine *machine)
422{
423 const char *vmlinux_name = NULL;
424 struct dso *kernel;
425
426 if (machine__is_host(machine)) {
427 vmlinux_name = symbol_conf.vmlinux_name;
428 if (!vmlinux_name)
429 vmlinux_name = "[kernel.kallsyms]";
430
431 kernel = dso__kernel_findnew(machine, vmlinux_name,
432 "[kernel]",
433 DSO_TYPE_KERNEL);
434 } else {
435 char bf[PATH_MAX];
436
437 if (machine__is_default_guest(machine))
438 vmlinux_name = symbol_conf.default_guest_vmlinux_name;
439 if (!vmlinux_name)
440 vmlinux_name = machine__mmap_name(machine, bf,
441 sizeof(bf));
442
443 kernel = dso__kernel_findnew(machine, vmlinux_name,
444 "[guest.kernel]",
445 DSO_TYPE_GUEST_KERNEL);
446 }
447
448 if (kernel != NULL && (!kernel->has_build_id))
449 dso__read_running_kernel_build_id(kernel, machine);
450
451 return kernel;
452}
453
454struct process_args {
455 u64 start;
456};
457
458static int symbol__in_kernel(void *arg, const char *name,
459 char type __maybe_unused, u64 start)
460{
461 struct process_args *args = arg;
462
463 if (strchr(name, '['))
464 return 0;
465
466 args->start = start;
467 return 1;
468}
469
470/* Figure out the start address of kernel map from /proc/kallsyms */
471static u64 machine__get_kernel_start_addr(struct machine *machine)
472{
473 const char *filename;
474 char path[PATH_MAX];
475 struct process_args args;
476
477 if (machine__is_host(machine)) {
478 filename = "/proc/kallsyms";
479 } else {
480 if (machine__is_default_guest(machine))
481 filename = (char *)symbol_conf.default_guest_kallsyms;
482 else {
483 sprintf(path, "%s/proc/kallsyms", machine->root_dir);
484 filename = path;
485 }
486 }
487
488 if (symbol__restricted_filename(filename, "/proc/kallsyms"))
489 return 0;
490
491 if (kallsyms__parse(filename, &args, symbol__in_kernel) <= 0)
492 return 0;
493
494 return args.start;
495}
496
497int __machine__create_kernel_maps(struct machine *machine, struct dso *kernel)
498{
499 enum map_type type;
500 u64 start = machine__get_kernel_start_addr(machine);
501
502 for (type = 0; type < MAP__NR_TYPES; ++type) {
503 struct kmap *kmap;
504
505 machine->vmlinux_maps[type] = map__new2(start, kernel, type);
506 if (machine->vmlinux_maps[type] == NULL)
507 return -1;
508
509 machine->vmlinux_maps[type]->map_ip =
510 machine->vmlinux_maps[type]->unmap_ip =
511 identity__map_ip;
512 kmap = map__kmap(machine->vmlinux_maps[type]);
513 kmap->kmaps = &machine->kmaps;
514 map_groups__insert(&machine->kmaps,
515 machine->vmlinux_maps[type]);
516 }
517
518 return 0;
519}
520
521void machine__destroy_kernel_maps(struct machine *machine)
522{
523 enum map_type type;
524
525 for (type = 0; type < MAP__NR_TYPES; ++type) {
526 struct kmap *kmap;
527
528 if (machine->vmlinux_maps[type] == NULL)
529 continue;
530
531 kmap = map__kmap(machine->vmlinux_maps[type]);
532 map_groups__remove(&machine->kmaps,
533 machine->vmlinux_maps[type]);
534 if (kmap->ref_reloc_sym) {
535 /*
536 * ref_reloc_sym is shared among all maps, so free just
537 * on one of them.
538 */
539 if (type == MAP__FUNCTION) {
540 free((char *)kmap->ref_reloc_sym->name);
541 kmap->ref_reloc_sym->name = NULL;
542 free(kmap->ref_reloc_sym);
543 }
544 kmap->ref_reloc_sym = NULL;
545 }
546
547 map__delete(machine->vmlinux_maps[type]);
548 machine->vmlinux_maps[type] = NULL;
549 }
550}
551
876650e6 552int machines__create_guest_kernel_maps(struct machines *machines)
3f067dca
ACM
553{
554 int ret = 0;
555 struct dirent **namelist = NULL;
556 int i, items = 0;
557 char path[PATH_MAX];
558 pid_t pid;
559 char *endp;
560
561 if (symbol_conf.default_guest_vmlinux_name ||
562 symbol_conf.default_guest_modules ||
563 symbol_conf.default_guest_kallsyms) {
564 machines__create_kernel_maps(machines, DEFAULT_GUEST_KERNEL_ID);
565 }
566
567 if (symbol_conf.guestmount) {
568 items = scandir(symbol_conf.guestmount, &namelist, NULL, NULL);
569 if (items <= 0)
570 return -ENOENT;
571 for (i = 0; i < items; i++) {
572 if (!isdigit(namelist[i]->d_name[0])) {
573 /* Filter out . and .. */
574 continue;
575 }
576 pid = (pid_t)strtol(namelist[i]->d_name, &endp, 10);
577 if ((*endp != '\0') ||
578 (endp == namelist[i]->d_name) ||
579 (errno == ERANGE)) {
580 pr_debug("invalid directory (%s). Skipping.\n",
581 namelist[i]->d_name);
582 continue;
583 }
584 sprintf(path, "%s/%s/proc/kallsyms",
585 symbol_conf.guestmount,
586 namelist[i]->d_name);
587 ret = access(path, R_OK);
588 if (ret) {
589 pr_debug("Can't access file %s\n", path);
590 goto failure;
591 }
592 machines__create_kernel_maps(machines, pid);
593 }
594failure:
595 free(namelist);
596 }
597
598 return ret;
599}
600
876650e6 601void machines__destroy_kernel_maps(struct machines *machines)
3f067dca 602{
876650e6
ACM
603 struct rb_node *next = rb_first(&machines->guests);
604
605 machine__destroy_kernel_maps(&machines->host);
3f067dca
ACM
606
607 while (next) {
608 struct machine *pos = rb_entry(next, struct machine, rb_node);
609
610 next = rb_next(&pos->rb_node);
876650e6 611 rb_erase(&pos->rb_node, &machines->guests);
3f067dca
ACM
612 machine__delete(pos);
613 }
614}
615
876650e6 616int machines__create_kernel_maps(struct machines *machines, pid_t pid)
3f067dca
ACM
617{
618 struct machine *machine = machines__findnew(machines, pid);
619
620 if (machine == NULL)
621 return -1;
622
623 return machine__create_kernel_maps(machine);
624}
625
626int machine__load_kallsyms(struct machine *machine, const char *filename,
627 enum map_type type, symbol_filter_t filter)
628{
629 struct map *map = machine->vmlinux_maps[type];
630 int ret = dso__load_kallsyms(map->dso, filename, map, filter);
631
632 if (ret > 0) {
633 dso__set_loaded(map->dso, type);
634 /*
635 * Since /proc/kallsyms will have multiple sessions for the
636 * kernel, with modules between them, fixup the end of all
637 * sections.
638 */
639 __map_groups__fixup_end(&machine->kmaps, type);
640 }
641
642 return ret;
643}
644
645int machine__load_vmlinux_path(struct machine *machine, enum map_type type,
646 symbol_filter_t filter)
647{
648 struct map *map = machine->vmlinux_maps[type];
649 int ret = dso__load_vmlinux_path(map->dso, map, filter);
650
39b12f78 651 if (ret > 0)
3f067dca 652 dso__set_loaded(map->dso, type);
3f067dca
ACM
653
654 return ret;
655}
656
657static void map_groups__fixup_end(struct map_groups *mg)
658{
659 int i;
660 for (i = 0; i < MAP__NR_TYPES; ++i)
661 __map_groups__fixup_end(mg, i);
662}
663
664static char *get_kernel_version(const char *root_dir)
665{
666 char version[PATH_MAX];
667 FILE *file;
668 char *name, *tmp;
669 const char *prefix = "Linux version ";
670
671 sprintf(version, "%s/proc/version", root_dir);
672 file = fopen(version, "r");
673 if (!file)
674 return NULL;
675
676 version[0] = '\0';
677 tmp = fgets(version, sizeof(version), file);
678 fclose(file);
679
680 name = strstr(version, prefix);
681 if (!name)
682 return NULL;
683 name += strlen(prefix);
684 tmp = strchr(name, ' ');
685 if (tmp)
686 *tmp = '\0';
687
688 return strdup(name);
689}
690
691static int map_groups__set_modules_path_dir(struct map_groups *mg,
692 const char *dir_name)
693{
694 struct dirent *dent;
695 DIR *dir = opendir(dir_name);
696 int ret = 0;
697
698 if (!dir) {
699 pr_debug("%s: cannot open %s dir\n", __func__, dir_name);
700 return -1;
701 }
702
703 while ((dent = readdir(dir)) != NULL) {
704 char path[PATH_MAX];
705 struct stat st;
706
707 /*sshfs might return bad dent->d_type, so we have to stat*/
708 snprintf(path, sizeof(path), "%s/%s", dir_name, dent->d_name);
709 if (stat(path, &st))
710 continue;
711
712 if (S_ISDIR(st.st_mode)) {
713 if (!strcmp(dent->d_name, ".") ||
714 !strcmp(dent->d_name, ".."))
715 continue;
716
717 ret = map_groups__set_modules_path_dir(mg, path);
718 if (ret < 0)
719 goto out;
720 } else {
721 char *dot = strrchr(dent->d_name, '.'),
722 dso_name[PATH_MAX];
723 struct map *map;
724 char *long_name;
725
726 if (dot == NULL || strcmp(dot, ".ko"))
727 continue;
728 snprintf(dso_name, sizeof(dso_name), "[%.*s]",
729 (int)(dot - dent->d_name), dent->d_name);
730
731 strxfrchar(dso_name, '-', '_');
732 map = map_groups__find_by_name(mg, MAP__FUNCTION,
733 dso_name);
734 if (map == NULL)
735 continue;
736
737 long_name = strdup(path);
738 if (long_name == NULL) {
739 ret = -1;
740 goto out;
741 }
742 dso__set_long_name(map->dso, long_name);
743 map->dso->lname_alloc = 1;
744 dso__kernel_module_get_build_id(map->dso, "");
745 }
746 }
747
748out:
749 closedir(dir);
750 return ret;
751}
752
753static int machine__set_modules_path(struct machine *machine)
754{
755 char *version;
756 char modules_path[PATH_MAX];
757
758 version = get_kernel_version(machine->root_dir);
759 if (!version)
760 return -1;
761
762 snprintf(modules_path, sizeof(modules_path), "%s/lib/modules/%s/kernel",
763 machine->root_dir, version);
764 free(version);
765
766 return map_groups__set_modules_path_dir(&machine->kmaps, modules_path);
767}
768
769static int machine__create_modules(struct machine *machine)
770{
771 char *line = NULL;
772 size_t n;
773 FILE *file;
774 struct map *map;
775 const char *modules;
776 char path[PATH_MAX];
777
778 if (machine__is_default_guest(machine))
779 modules = symbol_conf.default_guest_modules;
780 else {
781 sprintf(path, "%s/proc/modules", machine->root_dir);
782 modules = path;
783 }
784
785 if (symbol__restricted_filename(path, "/proc/modules"))
786 return -1;
787
788 file = fopen(modules, "r");
789 if (file == NULL)
790 return -1;
791
792 while (!feof(file)) {
793 char name[PATH_MAX];
794 u64 start;
795 char *sep;
796 int line_len;
797
798 line_len = getline(&line, &n, file);
799 if (line_len < 0)
800 break;
801
802 if (!line)
803 goto out_failure;
804
805 line[--line_len] = '\0'; /* \n */
806
807 sep = strrchr(line, 'x');
808 if (sep == NULL)
809 continue;
810
811 hex2u64(sep + 1, &start);
812
813 sep = strchr(line, ' ');
814 if (sep == NULL)
815 continue;
816
817 *sep = '\0';
818
819 snprintf(name, sizeof(name), "[%s]", line);
820 map = machine__new_module(machine, start, name);
821 if (map == NULL)
822 goto out_delete_line;
823 dso__kernel_module_get_build_id(map->dso, machine->root_dir);
824 }
825
826 free(line);
827 fclose(file);
828
8f76fcd9
JW
829 if (machine__set_modules_path(machine) < 0) {
830 pr_debug("Problems setting modules path maps, continuing anyway...\n");
831 }
832 return 0;
3f067dca
ACM
833
834out_delete_line:
835 free(line);
836out_failure:
837 return -1;
838}
839
840int machine__create_kernel_maps(struct machine *machine)
841{
842 struct dso *kernel = machine__get_kernel(machine);
843
844 if (kernel == NULL ||
845 __machine__create_kernel_maps(machine, kernel) < 0)
846 return -1;
847
848 if (symbol_conf.use_modules && machine__create_modules(machine) < 0) {
849 if (machine__is_host(machine))
850 pr_debug("Problems creating module maps, "
851 "continuing anyway...\n");
852 else
853 pr_debug("Problems creating module maps for guest %d, "
854 "continuing anyway...\n", machine->pid);
855 }
856
857 /*
858 * Now that we have all the maps created, just set the ->end of them:
859 */
860 map_groups__fixup_end(&machine->kmaps);
861 return 0;
862}
863
b0a7d1a0
ACM
864static void machine__set_kernel_mmap_len(struct machine *machine,
865 union perf_event *event)
866{
4552cf0f
NK
867 int i;
868
869 for (i = 0; i < MAP__NR_TYPES; i++) {
870 machine->vmlinux_maps[i]->start = event->mmap.start;
871 machine->vmlinux_maps[i]->end = (event->mmap.start +
872 event->mmap.len);
873 /*
874 * Be a bit paranoid here, some perf.data file came with
875 * a zero sized synthesized MMAP event for the kernel.
876 */
877 if (machine->vmlinux_maps[i]->end == 0)
878 machine->vmlinux_maps[i]->end = ~0ULL;
879 }
b0a7d1a0
ACM
880}
881
8e0cf965
AH
882static bool machine__uses_kcore(struct machine *machine)
883{
884 struct dso *dso;
885
886 list_for_each_entry(dso, &machine->kernel_dsos, node) {
887 if (dso__is_kcore(dso))
888 return true;
889 }
890
891 return false;
892}
893
b0a7d1a0
ACM
894static int machine__process_kernel_mmap_event(struct machine *machine,
895 union perf_event *event)
896{
897 struct map *map;
898 char kmmap_prefix[PATH_MAX];
899 enum dso_kernel_type kernel_type;
900 bool is_kernel_mmap;
901
8e0cf965
AH
902 /* If we have maps from kcore then we do not need or want any others */
903 if (machine__uses_kcore(machine))
904 return 0;
905
b0a7d1a0
ACM
906 machine__mmap_name(machine, kmmap_prefix, sizeof(kmmap_prefix));
907 if (machine__is_host(machine))
908 kernel_type = DSO_TYPE_KERNEL;
909 else
910 kernel_type = DSO_TYPE_GUEST_KERNEL;
911
912 is_kernel_mmap = memcmp(event->mmap.filename,
913 kmmap_prefix,
914 strlen(kmmap_prefix) - 1) == 0;
915 if (event->mmap.filename[0] == '/' ||
916 (!is_kernel_mmap && event->mmap.filename[0] == '[')) {
917
918 char short_module_name[1024];
919 char *name, *dot;
920
921 if (event->mmap.filename[0] == '/') {
922 name = strrchr(event->mmap.filename, '/');
923 if (name == NULL)
924 goto out_problem;
925
926 ++name; /* skip / */
927 dot = strrchr(name, '.');
928 if (dot == NULL)
929 goto out_problem;
930 snprintf(short_module_name, sizeof(short_module_name),
931 "[%.*s]", (int)(dot - name), name);
932 strxfrchar(short_module_name, '-', '_');
933 } else
934 strcpy(short_module_name, event->mmap.filename);
935
936 map = machine__new_module(machine, event->mmap.start,
937 event->mmap.filename);
938 if (map == NULL)
939 goto out_problem;
940
941 name = strdup(short_module_name);
942 if (name == NULL)
943 goto out_problem;
944
945 map->dso->short_name = name;
946 map->dso->sname_alloc = 1;
947 map->end = map->start + event->mmap.len;
948 } else if (is_kernel_mmap) {
949 const char *symbol_name = (event->mmap.filename +
950 strlen(kmmap_prefix));
951 /*
952 * Should be there already, from the build-id table in
953 * the header.
954 */
955 struct dso *kernel = __dsos__findnew(&machine->kernel_dsos,
956 kmmap_prefix);
957 if (kernel == NULL)
958 goto out_problem;
959
960 kernel->kernel = kernel_type;
961 if (__machine__create_kernel_maps(machine, kernel) < 0)
962 goto out_problem;
963
964 machine__set_kernel_mmap_len(machine, event);
965
966 /*
967 * Avoid using a zero address (kptr_restrict) for the ref reloc
968 * symbol. Effectively having zero here means that at record
969 * time /proc/sys/kernel/kptr_restrict was non zero.
970 */
971 if (event->mmap.pgoff != 0) {
972 maps__set_kallsyms_ref_reloc_sym(machine->vmlinux_maps,
973 symbol_name,
974 event->mmap.pgoff);
975 }
976
977 if (machine__is_default_guest(machine)) {
978 /*
979 * preload dso of guest kernel and modules
980 */
981 dso__load(kernel, machine->vmlinux_maps[MAP__FUNCTION],
982 NULL);
983 }
984 }
985 return 0;
986out_problem:
987 return -1;
988}
989
990int machine__process_mmap_event(struct machine *machine, union perf_event *event)
991{
992 u8 cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
993 struct thread *thread;
994 struct map *map;
bad40917 995 enum map_type type;
b0a7d1a0
ACM
996 int ret = 0;
997
998 if (dump_trace)
999 perf_event__fprintf_mmap(event, stdout);
1000
1001 if (cpumode == PERF_RECORD_MISC_GUEST_KERNEL ||
1002 cpumode == PERF_RECORD_MISC_KERNEL) {
1003 ret = machine__process_kernel_mmap_event(machine, event);
1004 if (ret < 0)
1005 goto out_problem;
1006 return 0;
1007 }
1008
1009 thread = machine__findnew_thread(machine, event->mmap.pid);
1010 if (thread == NULL)
1011 goto out_problem;
bad40917
SE
1012
1013 if (event->header.misc & PERF_RECORD_MISC_MMAP_DATA)
1014 type = MAP__VARIABLE;
1015 else
1016 type = MAP__FUNCTION;
1017
b0a7d1a0
ACM
1018 map = map__new(&machine->user_dsos, event->mmap.start,
1019 event->mmap.len, event->mmap.pgoff,
1020 event->mmap.pid, event->mmap.filename,
bad40917
SE
1021 type);
1022
b0a7d1a0
ACM
1023 if (map == NULL)
1024 goto out_problem;
1025
1026 thread__insert_map(thread, map);
1027 return 0;
1028
1029out_problem:
1030 dump_printf("problem processing PERF_RECORD_MMAP, skipping event.\n");
1031 return 0;
1032}
1033
1034int machine__process_fork_event(struct machine *machine, union perf_event *event)
1035{
1036 struct thread *thread = machine__findnew_thread(machine, event->fork.tid);
1037 struct thread *parent = machine__findnew_thread(machine, event->fork.ptid);
1038
1039 if (dump_trace)
1040 perf_event__fprintf_task(event, stdout);
1041
1042 if (thread == NULL || parent == NULL ||
1043 thread__fork(thread, parent) < 0) {
1044 dump_printf("problem processing PERF_RECORD_FORK, skipping event.\n");
1045 return -1;
1046 }
1047
1048 return 0;
1049}
1050
ed8996a6
DA
1051static void machine__remove_thread(struct machine *machine, struct thread *th)
1052{
1053 machine->last_match = NULL;
1054 rb_erase(&th->rb_node, &machine->threads);
1055 /*
1056 * We may have references to this thread, for instance in some hist_entry
1057 * instances, so just move them to a separate list.
1058 */
1059 list_add_tail(&th->node, &machine->dead_threads);
1060}
1061
b0a7d1a0
ACM
1062int machine__process_exit_event(struct machine *machine, union perf_event *event)
1063{
1064 struct thread *thread = machine__find_thread(machine, event->fork.tid);
1065
1066 if (dump_trace)
1067 perf_event__fprintf_task(event, stdout);
1068
1069 if (thread != NULL)
1070 machine__remove_thread(machine, thread);
1071
1072 return 0;
1073}
1074
1075int machine__process_event(struct machine *machine, union perf_event *event)
1076{
1077 int ret;
1078
1079 switch (event->header.type) {
1080 case PERF_RECORD_COMM:
1081 ret = machine__process_comm_event(machine, event); break;
1082 case PERF_RECORD_MMAP:
1083 ret = machine__process_mmap_event(machine, event); break;
1084 case PERF_RECORD_FORK:
1085 ret = machine__process_fork_event(machine, event); break;
1086 case PERF_RECORD_EXIT:
1087 ret = machine__process_exit_event(machine, event); break;
1088 case PERF_RECORD_LOST:
1089 ret = machine__process_lost_event(machine, event); break;
1090 default:
1091 ret = -1;
1092 break;
1093 }
1094
1095 return ret;
1096}
3f067dca 1097
b21484f1 1098static bool symbol__match_regex(struct symbol *sym, regex_t *regex)
3f067dca 1099{
b21484f1 1100 if (sym->name && !regexec(regex, sym->name, 0, NULL, 0))
3f067dca 1101 return 1;
3f067dca
ACM
1102 return 0;
1103}
1104
1105static const u8 cpumodes[] = {
1106 PERF_RECORD_MISC_USER,
1107 PERF_RECORD_MISC_KERNEL,
1108 PERF_RECORD_MISC_GUEST_USER,
1109 PERF_RECORD_MISC_GUEST_KERNEL
1110};
1111#define NCPUMODES (sizeof(cpumodes)/sizeof(u8))
1112
1113static void ip__resolve_ams(struct machine *machine, struct thread *thread,
1114 struct addr_map_symbol *ams,
1115 u64 ip)
1116{
1117 struct addr_location al;
1118 size_t i;
1119 u8 m;
1120
1121 memset(&al, 0, sizeof(al));
1122
1123 for (i = 0; i < NCPUMODES; i++) {
1124 m = cpumodes[i];
1125 /*
1126 * We cannot use the header.misc hint to determine whether a
1127 * branch stack address is user, kernel, guest, hypervisor.
1128 * Branches may straddle the kernel/user/hypervisor boundaries.
1129 * Thus, we have to try consecutively until we find a match
1130 * or else, the symbol is unknown
1131 */
1132 thread__find_addr_location(thread, machine, m, MAP__FUNCTION,
61710bde 1133 ip, &al);
3f067dca
ACM
1134 if (al.sym)
1135 goto found;
1136 }
1137found:
1138 ams->addr = ip;
1139 ams->al_addr = al.addr;
1140 ams->sym = al.sym;
1141 ams->map = al.map;
1142}
1143
98a3b32c
SE
1144static void ip__resolve_data(struct machine *machine, struct thread *thread,
1145 u8 m, struct addr_map_symbol *ams, u64 addr)
1146{
1147 struct addr_location al;
1148
1149 memset(&al, 0, sizeof(al));
1150
61710bde
AH
1151 thread__find_addr_location(thread, machine, m, MAP__VARIABLE, addr,
1152 &al);
98a3b32c
SE
1153 ams->addr = addr;
1154 ams->al_addr = al.addr;
1155 ams->sym = al.sym;
1156 ams->map = al.map;
1157}
1158
1159struct mem_info *machine__resolve_mem(struct machine *machine,
1160 struct thread *thr,
1161 struct perf_sample *sample,
1162 u8 cpumode)
1163{
1164 struct mem_info *mi = zalloc(sizeof(*mi));
1165
1166 if (!mi)
1167 return NULL;
1168
1169 ip__resolve_ams(machine, thr, &mi->iaddr, sample->ip);
1170 ip__resolve_data(machine, thr, cpumode, &mi->daddr, sample->addr);
1171 mi->data_src.val = sample->data_src;
1172
1173 return mi;
1174}
1175
3f067dca
ACM
1176struct branch_info *machine__resolve_bstack(struct machine *machine,
1177 struct thread *thr,
1178 struct branch_stack *bs)
1179{
1180 struct branch_info *bi;
1181 unsigned int i;
1182
1183 bi = calloc(bs->nr, sizeof(struct branch_info));
1184 if (!bi)
1185 return NULL;
1186
1187 for (i = 0; i < bs->nr; i++) {
1188 ip__resolve_ams(machine, thr, &bi[i].to, bs->entries[i].to);
1189 ip__resolve_ams(machine, thr, &bi[i].from, bs->entries[i].from);
1190 bi[i].flags = bs->entries[i].flags;
1191 }
1192 return bi;
1193}
1194
1195static int machine__resolve_callchain_sample(struct machine *machine,
1196 struct thread *thread,
1197 struct ip_callchain *chain,
b21484f1
GP
1198 struct symbol **parent,
1199 struct addr_location *root_al)
3f067dca
ACM
1200{
1201 u8 cpumode = PERF_RECORD_MISC_USER;
1202 unsigned int i;
1203 int err;
1204
1205 callchain_cursor_reset(&callchain_cursor);
1206
1207 if (chain->nr > PERF_MAX_STACK_DEPTH) {
1208 pr_warning("corrupted callchain. skipping...\n");
1209 return 0;
1210 }
1211
1212 for (i = 0; i < chain->nr; i++) {
1213 u64 ip;
1214 struct addr_location al;
1215
1216 if (callchain_param.order == ORDER_CALLEE)
1217 ip = chain->ips[i];
1218 else
1219 ip = chain->ips[chain->nr - i - 1];
1220
1221 if (ip >= PERF_CONTEXT_MAX) {
1222 switch (ip) {
1223 case PERF_CONTEXT_HV:
1224 cpumode = PERF_RECORD_MISC_HYPERVISOR;
1225 break;
1226 case PERF_CONTEXT_KERNEL:
1227 cpumode = PERF_RECORD_MISC_KERNEL;
1228 break;
1229 case PERF_CONTEXT_USER:
1230 cpumode = PERF_RECORD_MISC_USER;
1231 break;
1232 default:
1233 pr_debug("invalid callchain context: "
1234 "%"PRId64"\n", (s64) ip);
1235 /*
1236 * It seems the callchain is corrupted.
1237 * Discard all.
1238 */
1239 callchain_cursor_reset(&callchain_cursor);
1240 return 0;
1241 }
1242 continue;
1243 }
1244
1245 al.filtered = false;
1246 thread__find_addr_location(thread, machine, cpumode,
61710bde 1247 MAP__FUNCTION, ip, &al);
3f067dca
ACM
1248 if (al.sym != NULL) {
1249 if (sort__has_parent && !*parent &&
b21484f1 1250 symbol__match_regex(al.sym, &parent_regex))
3f067dca 1251 *parent = al.sym;
b21484f1
GP
1252 else if (have_ignore_callees && root_al &&
1253 symbol__match_regex(al.sym, &ignore_callees_regex)) {
1254 /* Treat this symbol as the root,
1255 forgetting its callees. */
1256 *root_al = al;
1257 callchain_cursor_reset(&callchain_cursor);
1258 }
3f067dca
ACM
1259 if (!symbol_conf.use_callchain)
1260 break;
1261 }
1262
1263 err = callchain_cursor_append(&callchain_cursor,
1264 ip, al.map, al.sym);
1265 if (err)
1266 return err;
1267 }
1268
1269 return 0;
1270}
1271
1272static int unwind_entry(struct unwind_entry *entry, void *arg)
1273{
1274 struct callchain_cursor *cursor = arg;
1275 return callchain_cursor_append(cursor, entry->ip,
1276 entry->map, entry->sym);
1277}
1278
1279int machine__resolve_callchain(struct machine *machine,
1280 struct perf_evsel *evsel,
1281 struct thread *thread,
1282 struct perf_sample *sample,
b21484f1
GP
1283 struct symbol **parent,
1284 struct addr_location *root_al)
3f067dca
ACM
1285{
1286 int ret;
1287
3f067dca 1288 ret = machine__resolve_callchain_sample(machine, thread,
b21484f1 1289 sample->callchain, parent, root_al);
3f067dca
ACM
1290 if (ret)
1291 return ret;
1292
1293 /* Can we do dwarf post unwind? */
1294 if (!((evsel->attr.sample_type & PERF_SAMPLE_REGS_USER) &&
1295 (evsel->attr.sample_type & PERF_SAMPLE_STACK_USER)))
1296 return 0;
1297
1298 /* Bail out if nothing was captured. */
1299 if ((!sample->user_regs.regs) ||
1300 (!sample->user_stack.size))
1301 return 0;
1302
1303 return unwind__get_entries(unwind_entry, &callchain_cursor, machine,
1304 thread, evsel->attr.sample_regs_user,
1305 sample);
1306
1307}