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