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