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