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