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
machine__init(struct machine * machine,const char * root_dir,pid_t pid)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
dsos__delete(struct list_head * dsos)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
machine__delete_dead_threads(struct machine * machine)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
machine__delete_threads(struct machine * machine)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
machine__exit(struct machine * machine)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
machine__delete(struct machine * machine)91 void machine__delete(struct machine *machine)
92 {
93 machine__exit(machine);
94 free(machine);
95 }
96
machines__init(struct machines * machines)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
machines__exit(struct machines * machines)104 void machines__exit(struct machines *machines)
105 {
106 machine__exit(&machines->host);
107 /* XXX exit guest */
108 }
109
machines__add(struct machines * machines,pid_t pid,const char * root_dir)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
machines__set_symbol_filter(struct machines * machines,symbol_filter_t symbol_filter)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
machines__find(struct machines * machines,pid_t pid)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
machines__findnew(struct machines * machines,pid_t pid)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
machines__process_guests(struct machines * machines,machine__process_t process,void * data)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
machine__mmap_name(struct machine * machine,char * bf,size_t size)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
machines__set_id_hdr_size(struct machines * machines,u16 id_hdr_size)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
__machine__findnew_thread(struct machine * machine,pid_t pid,pid_t tid,bool create)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
machine__findnew_thread(struct machine * machine,pid_t pid,pid_t tid)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
machine__find_thread(struct machine * machine,pid_t tid)312 struct thread *machine__find_thread(struct machine *machine, pid_t tid)
313 {
314 return __machine__findnew_thread(machine, 0, tid, false);
315 }
316
machine__process_comm_event(struct machine * machine,union perf_event * event)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
machine__process_lost_event(struct machine * machine __maybe_unused,union perf_event * event)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
machine__new_module(struct machine * machine,u64 start,const char * filename)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
machines__fprintf_dsos(struct machines * machines,FILE * fp)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
machine__fprintf_dsos_buildid(struct machine * machine,FILE * fp,bool (skip)(struct dso * dso,int parm),int parm)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
machines__fprintf_dsos_buildid(struct machines * machines,FILE * fp,bool (skip)(struct dso * dso,int parm),int parm)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
machine__fprintf_vmlinux_path(struct machine * machine,FILE * fp)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
machine__fprintf(struct machine * machine,FILE * fp)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
machine__get_kernel(struct machine * machine)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
symbol__in_kernel(void * arg,const char * name,char type __maybe_unused,u64 start)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 */
machine__get_kernel_start_addr(struct machine * machine)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
__machine__create_kernel_maps(struct machine * machine,struct dso * kernel)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
machine__destroy_kernel_maps(struct machine * machine)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
machines__create_guest_kernel_maps(struct machines * machines)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
machines__destroy_kernel_maps(struct machines * machines)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
machines__create_kernel_maps(struct machines * machines,pid_t pid)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
machine__load_kallsyms(struct machine * machine,const char * filename,enum map_type type,symbol_filter_t filter)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
machine__load_vmlinux_path(struct machine * machine,enum map_type type,symbol_filter_t filter)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
map_groups__fixup_end(struct map_groups * mg)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
get_kernel_version(const char * root_dir)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
map_groups__set_modules_path_dir(struct map_groups * mg,const char * dir_name)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
machine__set_modules_path(struct machine * machine)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
machine__create_modules(struct machine * machine)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(modules, "/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
machine__create_kernel_maps(struct machine * machine)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
machine__set_kernel_mmap_len(struct machine * machine,union perf_event * event)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
machine__uses_kcore(struct machine * machine)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
machine__process_kernel_mmap_event(struct machine * machine,union perf_event * event)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
machine__process_mmap2_event(struct machine * machine,union perf_event * event)1000 int machine__process_mmap2_event(struct machine *machine,
1001 union perf_event *event)
1002 {
1003 u8 cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
1004 struct thread *thread;
1005 struct map *map;
1006 enum map_type type;
1007 int ret = 0;
1008
1009 if (dump_trace)
1010 perf_event__fprintf_mmap2(event, stdout);
1011
1012 if (cpumode == PERF_RECORD_MISC_GUEST_KERNEL ||
1013 cpumode == PERF_RECORD_MISC_KERNEL) {
1014 ret = machine__process_kernel_mmap_event(machine, event);
1015 if (ret < 0)
1016 goto out_problem;
1017 return 0;
1018 }
1019
1020 thread = machine__findnew_thread(machine, event->mmap2.pid,
1021 event->mmap2.pid);
1022 if (thread == NULL)
1023 goto out_problem;
1024
1025 if (event->header.misc & PERF_RECORD_MISC_MMAP_DATA)
1026 type = MAP__VARIABLE;
1027 else
1028 type = MAP__FUNCTION;
1029
1030 map = map__new(&machine->user_dsos, event->mmap2.start,
1031 event->mmap2.len, event->mmap2.pgoff,
1032 event->mmap2.pid, event->mmap2.maj,
1033 event->mmap2.min, event->mmap2.ino,
1034 event->mmap2.ino_generation,
1035 event->mmap2.filename, type);
1036
1037 if (map == NULL)
1038 goto out_problem;
1039
1040 thread__insert_map(thread, map);
1041 return 0;
1042
1043 out_problem:
1044 dump_printf("problem processing PERF_RECORD_MMAP2, skipping event.\n");
1045 return 0;
1046 }
1047
machine__process_mmap_event(struct machine * machine,union perf_event * event)1048 int machine__process_mmap_event(struct machine *machine, union perf_event *event)
1049 {
1050 u8 cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
1051 struct thread *thread;
1052 struct map *map;
1053 enum map_type type;
1054 int ret = 0;
1055
1056 if (dump_trace)
1057 perf_event__fprintf_mmap(event, stdout);
1058
1059 if (cpumode == PERF_RECORD_MISC_GUEST_KERNEL ||
1060 cpumode == PERF_RECORD_MISC_KERNEL) {
1061 ret = machine__process_kernel_mmap_event(machine, event);
1062 if (ret < 0)
1063 goto out_problem;
1064 return 0;
1065 }
1066
1067 thread = machine__findnew_thread(machine, event->mmap.pid,
1068 event->mmap.pid);
1069 if (thread == NULL)
1070 goto out_problem;
1071
1072 if (event->header.misc & PERF_RECORD_MISC_MMAP_DATA)
1073 type = MAP__VARIABLE;
1074 else
1075 type = MAP__FUNCTION;
1076
1077 map = map__new(&machine->user_dsos, event->mmap.start,
1078 event->mmap.len, event->mmap.pgoff,
1079 event->mmap.pid, 0, 0, 0, 0,
1080 event->mmap.filename,
1081 type);
1082
1083 if (map == NULL)
1084 goto out_problem;
1085
1086 thread__insert_map(thread, map);
1087 return 0;
1088
1089 out_problem:
1090 dump_printf("problem processing PERF_RECORD_MMAP, skipping event.\n");
1091 return 0;
1092 }
1093
machine__remove_thread(struct machine * machine,struct thread * th)1094 static void machine__remove_thread(struct machine *machine, struct thread *th)
1095 {
1096 machine->last_match = NULL;
1097 rb_erase(&th->rb_node, &machine->threads);
1098 /*
1099 * We may have references to this thread, for instance in some hist_entry
1100 * instances, so just move them to a separate list.
1101 */
1102 list_add_tail(&th->node, &machine->dead_threads);
1103 }
1104
machine__process_fork_event(struct machine * machine,union perf_event * event)1105 int machine__process_fork_event(struct machine *machine, union perf_event *event)
1106 {
1107 struct thread *thread = machine__find_thread(machine, event->fork.tid);
1108 struct thread *parent = machine__findnew_thread(machine,
1109 event->fork.ppid,
1110 event->fork.ptid);
1111
1112 /* if a thread currently exists for the thread id remove it */
1113 if (thread != NULL)
1114 machine__remove_thread(machine, thread);
1115
1116 thread = machine__findnew_thread(machine, event->fork.pid,
1117 event->fork.tid);
1118 if (dump_trace)
1119 perf_event__fprintf_task(event, stdout);
1120
1121 if (thread == NULL || parent == NULL ||
1122 thread__fork(thread, parent) < 0) {
1123 dump_printf("problem processing PERF_RECORD_FORK, skipping event.\n");
1124 return -1;
1125 }
1126
1127 return 0;
1128 }
1129
machine__process_exit_event(struct machine * machine __maybe_unused,union perf_event * event)1130 int machine__process_exit_event(struct machine *machine __maybe_unused,
1131 union perf_event *event)
1132 {
1133 struct thread *thread = machine__find_thread(machine, event->fork.tid);
1134
1135 if (dump_trace)
1136 perf_event__fprintf_task(event, stdout);
1137
1138 if (thread != NULL)
1139 thread__exited(thread);
1140
1141 return 0;
1142 }
1143
machine__process_event(struct machine * machine,union perf_event * event)1144 int machine__process_event(struct machine *machine, union perf_event *event)
1145 {
1146 int ret;
1147
1148 switch (event->header.type) {
1149 case PERF_RECORD_COMM:
1150 ret = machine__process_comm_event(machine, event); break;
1151 case PERF_RECORD_MMAP:
1152 ret = machine__process_mmap_event(machine, event); break;
1153 case PERF_RECORD_MMAP2:
1154 ret = machine__process_mmap2_event(machine, event); break;
1155 case PERF_RECORD_FORK:
1156 ret = machine__process_fork_event(machine, event); break;
1157 case PERF_RECORD_EXIT:
1158 ret = machine__process_exit_event(machine, event); break;
1159 case PERF_RECORD_LOST:
1160 ret = machine__process_lost_event(machine, event); break;
1161 default:
1162 ret = -1;
1163 break;
1164 }
1165
1166 return ret;
1167 }
1168
symbol__match_regex(struct symbol * sym,regex_t * regex)1169 static bool symbol__match_regex(struct symbol *sym, regex_t *regex)
1170 {
1171 if (sym->name && !regexec(regex, sym->name, 0, NULL, 0))
1172 return 1;
1173 return 0;
1174 }
1175
1176 static const u8 cpumodes[] = {
1177 PERF_RECORD_MISC_USER,
1178 PERF_RECORD_MISC_KERNEL,
1179 PERF_RECORD_MISC_GUEST_USER,
1180 PERF_RECORD_MISC_GUEST_KERNEL
1181 };
1182 #define NCPUMODES (sizeof(cpumodes)/sizeof(u8))
1183
ip__resolve_ams(struct machine * machine,struct thread * thread,struct addr_map_symbol * ams,u64 ip)1184 static void ip__resolve_ams(struct machine *machine, struct thread *thread,
1185 struct addr_map_symbol *ams,
1186 u64 ip)
1187 {
1188 struct addr_location al;
1189 size_t i;
1190 u8 m;
1191
1192 memset(&al, 0, sizeof(al));
1193
1194 for (i = 0; i < NCPUMODES; i++) {
1195 m = cpumodes[i];
1196 /*
1197 * We cannot use the header.misc hint to determine whether a
1198 * branch stack address is user, kernel, guest, hypervisor.
1199 * Branches may straddle the kernel/user/hypervisor boundaries.
1200 * Thus, we have to try consecutively until we find a match
1201 * or else, the symbol is unknown
1202 */
1203 thread__find_addr_location(thread, machine, m, MAP__FUNCTION,
1204 ip, &al);
1205 if (al.sym)
1206 goto found;
1207 }
1208 found:
1209 ams->addr = ip;
1210 ams->al_addr = al.addr;
1211 ams->sym = al.sym;
1212 ams->map = al.map;
1213 }
1214
ip__resolve_data(struct machine * machine,struct thread * thread,u8 m,struct addr_map_symbol * ams,u64 addr)1215 static void ip__resolve_data(struct machine *machine, struct thread *thread,
1216 u8 m, struct addr_map_symbol *ams, u64 addr)
1217 {
1218 struct addr_location al;
1219
1220 memset(&al, 0, sizeof(al));
1221
1222 thread__find_addr_location(thread, machine, m, MAP__VARIABLE, addr,
1223 &al);
1224 ams->addr = addr;
1225 ams->al_addr = al.addr;
1226 ams->sym = al.sym;
1227 ams->map = al.map;
1228 }
1229
machine__resolve_mem(struct machine * machine,struct thread * thr,struct perf_sample * sample,u8 cpumode)1230 struct mem_info *machine__resolve_mem(struct machine *machine,
1231 struct thread *thr,
1232 struct perf_sample *sample,
1233 u8 cpumode)
1234 {
1235 struct mem_info *mi = zalloc(sizeof(*mi));
1236
1237 if (!mi)
1238 return NULL;
1239
1240 ip__resolve_ams(machine, thr, &mi->iaddr, sample->ip);
1241 ip__resolve_data(machine, thr, cpumode, &mi->daddr, sample->addr);
1242 mi->data_src.val = sample->data_src;
1243
1244 return mi;
1245 }
1246
machine__resolve_bstack(struct machine * machine,struct thread * thr,struct branch_stack * bs)1247 struct branch_info *machine__resolve_bstack(struct machine *machine,
1248 struct thread *thr,
1249 struct branch_stack *bs)
1250 {
1251 struct branch_info *bi;
1252 unsigned int i;
1253
1254 bi = calloc(bs->nr, sizeof(struct branch_info));
1255 if (!bi)
1256 return NULL;
1257
1258 for (i = 0; i < bs->nr; i++) {
1259 ip__resolve_ams(machine, thr, &bi[i].to, bs->entries[i].to);
1260 ip__resolve_ams(machine, thr, &bi[i].from, bs->entries[i].from);
1261 bi[i].flags = bs->entries[i].flags;
1262 }
1263 return bi;
1264 }
1265
machine__resolve_callchain_sample(struct machine * machine,struct thread * thread,struct ip_callchain * chain,struct symbol ** parent,struct addr_location * root_al)1266 static int machine__resolve_callchain_sample(struct machine *machine,
1267 struct thread *thread,
1268 struct ip_callchain *chain,
1269 struct symbol **parent,
1270 struct addr_location *root_al)
1271 {
1272 u8 cpumode = PERF_RECORD_MISC_USER;
1273 unsigned int i;
1274 int err;
1275
1276 callchain_cursor_reset(&callchain_cursor);
1277
1278 if (chain->nr > PERF_MAX_STACK_DEPTH) {
1279 pr_warning("corrupted callchain. skipping...\n");
1280 return 0;
1281 }
1282
1283 for (i = 0; i < chain->nr; i++) {
1284 u64 ip;
1285 struct addr_location al;
1286
1287 if (callchain_param.order == ORDER_CALLEE)
1288 ip = chain->ips[i];
1289 else
1290 ip = chain->ips[chain->nr - i - 1];
1291
1292 if (ip >= PERF_CONTEXT_MAX) {
1293 switch (ip) {
1294 case PERF_CONTEXT_HV:
1295 cpumode = PERF_RECORD_MISC_HYPERVISOR;
1296 break;
1297 case PERF_CONTEXT_KERNEL:
1298 cpumode = PERF_RECORD_MISC_KERNEL;
1299 break;
1300 case PERF_CONTEXT_USER:
1301 cpumode = PERF_RECORD_MISC_USER;
1302 break;
1303 default:
1304 pr_debug("invalid callchain context: "
1305 "%"PRId64"\n", (s64) ip);
1306 /*
1307 * It seems the callchain is corrupted.
1308 * Discard all.
1309 */
1310 callchain_cursor_reset(&callchain_cursor);
1311 return 0;
1312 }
1313 continue;
1314 }
1315
1316 al.filtered = false;
1317 thread__find_addr_location(thread, machine, cpumode,
1318 MAP__FUNCTION, ip, &al);
1319 if (al.sym != NULL) {
1320 if (sort__has_parent && !*parent &&
1321 symbol__match_regex(al.sym, &parent_regex))
1322 *parent = al.sym;
1323 else if (have_ignore_callees && root_al &&
1324 symbol__match_regex(al.sym, &ignore_callees_regex)) {
1325 /* Treat this symbol as the root,
1326 forgetting its callees. */
1327 *root_al = al;
1328 callchain_cursor_reset(&callchain_cursor);
1329 }
1330 if (!symbol_conf.use_callchain)
1331 break;
1332 }
1333
1334 err = callchain_cursor_append(&callchain_cursor,
1335 ip, al.map, al.sym);
1336 if (err)
1337 return err;
1338 }
1339
1340 return 0;
1341 }
1342
unwind_entry(struct unwind_entry * entry,void * arg)1343 static int unwind_entry(struct unwind_entry *entry, void *arg)
1344 {
1345 struct callchain_cursor *cursor = arg;
1346 return callchain_cursor_append(cursor, entry->ip,
1347 entry->map, entry->sym);
1348 }
1349
machine__resolve_callchain(struct machine * machine,struct perf_evsel * evsel,struct thread * thread,struct perf_sample * sample,struct symbol ** parent,struct addr_location * root_al)1350 int machine__resolve_callchain(struct machine *machine,
1351 struct perf_evsel *evsel,
1352 struct thread *thread,
1353 struct perf_sample *sample,
1354 struct symbol **parent,
1355 struct addr_location *root_al)
1356 {
1357 int ret;
1358
1359 ret = machine__resolve_callchain_sample(machine, thread,
1360 sample->callchain, parent, root_al);
1361 if (ret)
1362 return ret;
1363
1364 /* Can we do dwarf post unwind? */
1365 if (!((evsel->attr.sample_type & PERF_SAMPLE_REGS_USER) &&
1366 (evsel->attr.sample_type & PERF_SAMPLE_STACK_USER)))
1367 return 0;
1368
1369 /* Bail out if nothing was captured. */
1370 if ((!sample->user_regs.regs) ||
1371 (!sample->user_stack.size))
1372 return 0;
1373
1374 return unwind__get_entries(unwind_entry, &callchain_cursor, machine,
1375 thread, evsel->attr.sample_regs_user,
1376 sample);
1377
1378 }
1379