perf hists browser: Cleanup callchain print functions
[linux-2.6-block.git] / tools / perf / util / machine.c
CommitLineData
3f067dca 1#include "callchain.h"
b0a7d1a0
ACM
2#include "debug.h"
3#include "event.h"
3f067dca
ACM
4#include "evsel.h"
5#include "hist.h"
9d2f8e22
ACM
6#include "machine.h"
7#include "map.h"
3f067dca 8#include "sort.h"
69d2591a 9#include "strlist.h"
9d2f8e22 10#include "thread.h"
d027b640 11#include "vdso.h"
9d2f8e22 12#include <stdbool.h>
c506c96b 13#include <symbol/kallsyms.h>
3f067dca 14#include "unwind.h"
9d2f8e22 15
69d2591a
ACM
16int machine__init(struct machine *machine, const char *root_dir, pid_t pid)
17{
18 map_groups__init(&machine->kmaps);
19 RB_CLEAR_NODE(&machine->rb_node);
20 INIT_LIST_HEAD(&machine->user_dsos);
21 INIT_LIST_HEAD(&machine->kernel_dsos);
22
23 machine->threads = RB_ROOT;
24 INIT_LIST_HEAD(&machine->dead_threads);
25 machine->last_match = NULL;
26
d027b640
AH
27 machine->vdso_info = NULL;
28
69d2591a
ACM
29 machine->kmaps.machine = machine;
30 machine->pid = pid;
31
611a5ce8 32 machine->symbol_filter = NULL;
14bd6d20 33 machine->id_hdr_size = 0;
cfe1c414 34 machine->comm_exec = false;
fbe2af45 35 machine->kernel_start = 0;
611a5ce8 36
69d2591a
ACM
37 machine->root_dir = strdup(root_dir);
38 if (machine->root_dir == NULL)
39 return -ENOMEM;
40
41 if (pid != HOST_KERNEL_ID) {
1fcb8768 42 struct thread *thread = machine__findnew_thread(machine, -1,
314add6b 43 pid);
69d2591a
ACM
44 char comm[64];
45
46 if (thread == NULL)
47 return -ENOMEM;
48
49 snprintf(comm, sizeof(comm), "[guest/%d]", pid);
162f0bef 50 thread__set_comm(thread, comm, 0);
69d2591a
ACM
51 }
52
b9d266ba
AH
53 machine->current_tid = NULL;
54
69d2591a
ACM
55 return 0;
56}
57
8fb598e5
DA
58struct machine *machine__new_host(void)
59{
60 struct machine *machine = malloc(sizeof(*machine));
61
62 if (machine != NULL) {
63 machine__init(machine, "", HOST_KERNEL_ID);
64
65 if (machine__create_kernel_maps(machine) < 0)
66 goto out_delete;
67 }
68
69 return machine;
70out_delete:
71 free(machine);
72 return NULL;
73}
74
69d2591a
ACM
75static void dsos__delete(struct list_head *dsos)
76{
77 struct dso *pos, *n;
78
79 list_for_each_entry_safe(pos, n, dsos, node) {
80 list_del(&pos->node);
81 dso__delete(pos);
82 }
83}
84
3f067dca
ACM
85void machine__delete_dead_threads(struct machine *machine)
86{
87 struct thread *n, *t;
88
89 list_for_each_entry_safe(t, n, &machine->dead_threads, node) {
90 list_del(&t->node);
91 thread__delete(t);
92 }
93}
94
95void machine__delete_threads(struct machine *machine)
96{
97 struct rb_node *nd = rb_first(&machine->threads);
98
99 while (nd) {
100 struct thread *t = rb_entry(nd, struct thread, rb_node);
101
102 rb_erase(&t->rb_node, &machine->threads);
103 nd = rb_next(nd);
104 thread__delete(t);
105 }
106}
107
69d2591a
ACM
108void machine__exit(struct machine *machine)
109{
110 map_groups__exit(&machine->kmaps);
111 dsos__delete(&machine->user_dsos);
112 dsos__delete(&machine->kernel_dsos);
d027b640 113 vdso__exit(machine);
04662523 114 zfree(&machine->root_dir);
b9d266ba 115 zfree(&machine->current_tid);
69d2591a
ACM
116}
117
118void machine__delete(struct machine *machine)
119{
120 machine__exit(machine);
121 free(machine);
122}
123
876650e6
ACM
124void machines__init(struct machines *machines)
125{
126 machine__init(&machines->host, "", HOST_KERNEL_ID);
127 machines->guests = RB_ROOT;
611a5ce8 128 machines->symbol_filter = NULL;
876650e6
ACM
129}
130
131void machines__exit(struct machines *machines)
132{
133 machine__exit(&machines->host);
134 /* XXX exit guest */
135}
136
137struct machine *machines__add(struct machines *machines, pid_t pid,
69d2591a
ACM
138 const char *root_dir)
139{
876650e6 140 struct rb_node **p = &machines->guests.rb_node;
69d2591a
ACM
141 struct rb_node *parent = NULL;
142 struct machine *pos, *machine = malloc(sizeof(*machine));
143
144 if (machine == NULL)
145 return NULL;
146
147 if (machine__init(machine, root_dir, pid) != 0) {
148 free(machine);
149 return NULL;
150 }
151
611a5ce8
AH
152 machine->symbol_filter = machines->symbol_filter;
153
69d2591a
ACM
154 while (*p != NULL) {
155 parent = *p;
156 pos = rb_entry(parent, struct machine, rb_node);
157 if (pid < pos->pid)
158 p = &(*p)->rb_left;
159 else
160 p = &(*p)->rb_right;
161 }
162
163 rb_link_node(&machine->rb_node, parent, p);
876650e6 164 rb_insert_color(&machine->rb_node, &machines->guests);
69d2591a
ACM
165
166 return machine;
167}
168
611a5ce8
AH
169void machines__set_symbol_filter(struct machines *machines,
170 symbol_filter_t symbol_filter)
171{
172 struct rb_node *nd;
173
174 machines->symbol_filter = symbol_filter;
175 machines->host.symbol_filter = symbol_filter;
176
177 for (nd = rb_first(&machines->guests); nd; nd = rb_next(nd)) {
178 struct machine *machine = rb_entry(nd, struct machine, rb_node);
179
180 machine->symbol_filter = symbol_filter;
181 }
182}
183
cfe1c414
AH
184void machines__set_comm_exec(struct machines *machines, bool comm_exec)
185{
186 struct rb_node *nd;
187
188 machines->host.comm_exec = comm_exec;
189
190 for (nd = rb_first(&machines->guests); nd; nd = rb_next(nd)) {
191 struct machine *machine = rb_entry(nd, struct machine, rb_node);
192
193 machine->comm_exec = comm_exec;
194 }
195}
196
876650e6 197struct machine *machines__find(struct machines *machines, pid_t pid)
69d2591a 198{
876650e6 199 struct rb_node **p = &machines->guests.rb_node;
69d2591a
ACM
200 struct rb_node *parent = NULL;
201 struct machine *machine;
202 struct machine *default_machine = NULL;
203
876650e6
ACM
204 if (pid == HOST_KERNEL_ID)
205 return &machines->host;
206
69d2591a
ACM
207 while (*p != NULL) {
208 parent = *p;
209 machine = rb_entry(parent, struct machine, rb_node);
210 if (pid < machine->pid)
211 p = &(*p)->rb_left;
212 else if (pid > machine->pid)
213 p = &(*p)->rb_right;
214 else
215 return machine;
216 if (!machine->pid)
217 default_machine = machine;
218 }
219
220 return default_machine;
221}
222
876650e6 223struct machine *machines__findnew(struct machines *machines, pid_t pid)
69d2591a
ACM
224{
225 char path[PATH_MAX];
226 const char *root_dir = "";
227 struct machine *machine = machines__find(machines, pid);
228
229 if (machine && (machine->pid == pid))
230 goto out;
231
232 if ((pid != HOST_KERNEL_ID) &&
233 (pid != DEFAULT_GUEST_KERNEL_ID) &&
234 (symbol_conf.guestmount)) {
235 sprintf(path, "%s/%d", symbol_conf.guestmount, pid);
236 if (access(path, R_OK)) {
237 static struct strlist *seen;
238
239 if (!seen)
240 seen = strlist__new(true, NULL);
241
242 if (!strlist__has_entry(seen, path)) {
243 pr_err("Can't access file %s\n", path);
244 strlist__add(seen, path);
245 }
246 machine = NULL;
247 goto out;
248 }
249 root_dir = path;
250 }
251
252 machine = machines__add(machines, pid, root_dir);
253out:
254 return machine;
255}
256
876650e6
ACM
257void machines__process_guests(struct machines *machines,
258 machine__process_t process, void *data)
69d2591a
ACM
259{
260 struct rb_node *nd;
261
876650e6 262 for (nd = rb_first(&machines->guests); nd; nd = rb_next(nd)) {
69d2591a
ACM
263 struct machine *pos = rb_entry(nd, struct machine, rb_node);
264 process(pos, data);
265 }
266}
267
268char *machine__mmap_name(struct machine *machine, char *bf, size_t size)
269{
270 if (machine__is_host(machine))
271 snprintf(bf, size, "[%s]", "kernel.kallsyms");
272 else if (machine__is_default_guest(machine))
273 snprintf(bf, size, "[%s]", "guest.kernel.kallsyms");
274 else {
275 snprintf(bf, size, "[%s.%d]", "guest.kernel.kallsyms",
276 machine->pid);
277 }
278
279 return bf;
280}
281
876650e6 282void machines__set_id_hdr_size(struct machines *machines, u16 id_hdr_size)
69d2591a
ACM
283{
284 struct rb_node *node;
285 struct machine *machine;
286
876650e6
ACM
287 machines->host.id_hdr_size = id_hdr_size;
288
289 for (node = rb_first(&machines->guests); node; node = rb_next(node)) {
69d2591a
ACM
290 machine = rb_entry(node, struct machine, rb_node);
291 machine->id_hdr_size = id_hdr_size;
292 }
293
294 return;
295}
296
29ce3612
AH
297static void machine__update_thread_pid(struct machine *machine,
298 struct thread *th, pid_t pid)
299{
300 struct thread *leader;
301
302 if (pid == th->pid_ || pid == -1 || th->pid_ != -1)
303 return;
304
305 th->pid_ = pid;
306
307 if (th->pid_ == th->tid)
308 return;
309
310 leader = machine__findnew_thread(machine, th->pid_, th->pid_);
311 if (!leader)
312 goto out_err;
313
314 if (!leader->mg)
315 leader->mg = map_groups__new();
316
317 if (!leader->mg)
318 goto out_err;
319
320 if (th->mg == leader->mg)
321 return;
322
323 if (th->mg) {
324 /*
325 * Maps are created from MMAP events which provide the pid and
326 * tid. Consequently there never should be any maps on a thread
327 * with an unknown pid. Just print an error if there are.
328 */
329 if (!map_groups__empty(th->mg))
330 pr_err("Discarding thread maps for %d:%d\n",
331 th->pid_, th->tid);
332 map_groups__delete(th->mg);
333 }
334
335 th->mg = map_groups__get(leader->mg);
336
337 return;
338
339out_err:
340 pr_err("Failed to join map groups for %d:%d\n", th->pid_, th->tid);
341}
342
99d725fc
AH
343static struct thread *__machine__findnew_thread(struct machine *machine,
344 pid_t pid, pid_t tid,
9d2f8e22
ACM
345 bool create)
346{
347 struct rb_node **p = &machine->threads.rb_node;
348 struct rb_node *parent = NULL;
349 struct thread *th;
350
351 /*
38051234 352 * Front-end cache - TID lookups come in blocks,
9d2f8e22
ACM
353 * so most of the time we dont have to look up
354 * the full rbtree:
355 */
29ce3612
AH
356 th = machine->last_match;
357 if (th && th->tid == tid) {
358 machine__update_thread_pid(machine, th, pid);
359 return th;
99d725fc 360 }
9d2f8e22
ACM
361
362 while (*p != NULL) {
363 parent = *p;
364 th = rb_entry(parent, struct thread, rb_node);
365
38051234 366 if (th->tid == tid) {
9d2f8e22 367 machine->last_match = th;
29ce3612 368 machine__update_thread_pid(machine, th, pid);
9d2f8e22
ACM
369 return th;
370 }
371
38051234 372 if (tid < th->tid)
9d2f8e22
ACM
373 p = &(*p)->rb_left;
374 else
375 p = &(*p)->rb_right;
376 }
377
378 if (!create)
379 return NULL;
380
99d725fc 381 th = thread__new(pid, tid);
9d2f8e22
ACM
382 if (th != NULL) {
383 rb_link_node(&th->rb_node, parent, p);
384 rb_insert_color(&th->rb_node, &machine->threads);
385 machine->last_match = th;
cddcef60
JO
386
387 /*
388 * We have to initialize map_groups separately
389 * after rb tree is updated.
390 *
391 * The reason is that we call machine__findnew_thread
392 * within thread__init_map_groups to find the thread
393 * leader and that would screwed the rb tree.
394 */
418029b7
AH
395 if (thread__init_map_groups(th, machine)) {
396 thread__delete(th);
cddcef60 397 return NULL;
418029b7 398 }
9d2f8e22
ACM
399 }
400
401 return th;
402}
403
314add6b
AH
404struct thread *machine__findnew_thread(struct machine *machine, pid_t pid,
405 pid_t tid)
9d2f8e22 406{
314add6b 407 return __machine__findnew_thread(machine, pid, tid, true);
9d2f8e22
ACM
408}
409
d75e6097
JO
410struct thread *machine__find_thread(struct machine *machine, pid_t pid,
411 pid_t tid)
9d2f8e22 412{
d75e6097 413 return __machine__findnew_thread(machine, pid, tid, false);
9d2f8e22 414}
b0a7d1a0 415
cfe1c414
AH
416struct comm *machine__thread_exec_comm(struct machine *machine,
417 struct thread *thread)
418{
419 if (machine->comm_exec)
420 return thread__exec_comm(thread);
421 else
422 return thread__comm(thread);
423}
424
162f0bef
FW
425int machine__process_comm_event(struct machine *machine, union perf_event *event,
426 struct perf_sample *sample)
b0a7d1a0 427{
314add6b
AH
428 struct thread *thread = machine__findnew_thread(machine,
429 event->comm.pid,
430 event->comm.tid);
65de51f9 431 bool exec = event->header.misc & PERF_RECORD_MISC_COMM_EXEC;
b0a7d1a0 432
cfe1c414
AH
433 if (exec)
434 machine->comm_exec = true;
435
b0a7d1a0
ACM
436 if (dump_trace)
437 perf_event__fprintf_comm(event, stdout);
438
65de51f9
AH
439 if (thread == NULL ||
440 __thread__set_comm(thread, event->comm.comm, sample->time, exec)) {
b0a7d1a0
ACM
441 dump_printf("problem processing PERF_RECORD_COMM, skipping event.\n");
442 return -1;
443 }
444
445 return 0;
446}
447
448int machine__process_lost_event(struct machine *machine __maybe_unused,
162f0bef 449 union perf_event *event, struct perf_sample *sample __maybe_unused)
b0a7d1a0
ACM
450{
451 dump_printf(": id:%" PRIu64 ": lost:%" PRIu64 "\n",
452 event->lost.id, event->lost.lost);
453 return 0;
454}
455
3f067dca
ACM
456struct map *machine__new_module(struct machine *machine, u64 start,
457 const char *filename)
458{
459 struct map *map;
460 struct dso *dso = __dsos__findnew(&machine->kernel_dsos, filename);
461
462 if (dso == NULL)
463 return NULL;
464
465 map = map__new2(start, dso, MAP__FUNCTION);
466 if (map == NULL)
467 return NULL;
468
469 if (machine__is_host(machine))
470 dso->symtab_type = DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE;
471 else
472 dso->symtab_type = DSO_BINARY_TYPE__GUEST_KMODULE;
473 map_groups__insert(&machine->kmaps, map);
474 return map;
475}
476
876650e6 477size_t machines__fprintf_dsos(struct machines *machines, FILE *fp)
3f067dca
ACM
478{
479 struct rb_node *nd;
876650e6
ACM
480 size_t ret = __dsos__fprintf(&machines->host.kernel_dsos, fp) +
481 __dsos__fprintf(&machines->host.user_dsos, fp);
3f067dca 482
876650e6 483 for (nd = rb_first(&machines->guests); nd; nd = rb_next(nd)) {
3f067dca
ACM
484 struct machine *pos = rb_entry(nd, struct machine, rb_node);
485 ret += __dsos__fprintf(&pos->kernel_dsos, fp);
486 ret += __dsos__fprintf(&pos->user_dsos, fp);
487 }
488
489 return ret;
490}
491
492size_t machine__fprintf_dsos_buildid(struct machine *machine, FILE *fp,
493 bool (skip)(struct dso *dso, int parm), int parm)
494{
495 return __dsos__fprintf_buildid(&machine->kernel_dsos, fp, skip, parm) +
496 __dsos__fprintf_buildid(&machine->user_dsos, fp, skip, parm);
497}
498
876650e6 499size_t machines__fprintf_dsos_buildid(struct machines *machines, FILE *fp,
3f067dca
ACM
500 bool (skip)(struct dso *dso, int parm), int parm)
501{
502 struct rb_node *nd;
876650e6 503 size_t ret = machine__fprintf_dsos_buildid(&machines->host, fp, skip, parm);
3f067dca 504
876650e6 505 for (nd = rb_first(&machines->guests); nd; nd = rb_next(nd)) {
3f067dca
ACM
506 struct machine *pos = rb_entry(nd, struct machine, rb_node);
507 ret += machine__fprintf_dsos_buildid(pos, fp, skip, parm);
508 }
509 return ret;
510}
511
512size_t machine__fprintf_vmlinux_path(struct machine *machine, FILE *fp)
513{
514 int i;
515 size_t printed = 0;
516 struct dso *kdso = machine->vmlinux_maps[MAP__FUNCTION]->dso;
517
518 if (kdso->has_build_id) {
519 char filename[PATH_MAX];
520 if (dso__build_id_filename(kdso, filename, sizeof(filename)))
521 printed += fprintf(fp, "[0] %s\n", filename);
522 }
523
524 for (i = 0; i < vmlinux_path__nr_entries; ++i)
525 printed += fprintf(fp, "[%d] %s\n",
526 i + kdso->has_build_id, vmlinux_path[i]);
527
528 return printed;
529}
530
531size_t machine__fprintf(struct machine *machine, FILE *fp)
532{
533 size_t ret = 0;
534 struct rb_node *nd;
535
536 for (nd = rb_first(&machine->threads); nd; nd = rb_next(nd)) {
537 struct thread *pos = rb_entry(nd, struct thread, rb_node);
538
539 ret += thread__fprintf(pos, fp);
540 }
541
542 return ret;
543}
544
545static struct dso *machine__get_kernel(struct machine *machine)
546{
547 const char *vmlinux_name = NULL;
548 struct dso *kernel;
549
550 if (machine__is_host(machine)) {
551 vmlinux_name = symbol_conf.vmlinux_name;
552 if (!vmlinux_name)
553 vmlinux_name = "[kernel.kallsyms]";
554
555 kernel = dso__kernel_findnew(machine, vmlinux_name,
556 "[kernel]",
557 DSO_TYPE_KERNEL);
558 } else {
559 char bf[PATH_MAX];
560
561 if (machine__is_default_guest(machine))
562 vmlinux_name = symbol_conf.default_guest_vmlinux_name;
563 if (!vmlinux_name)
564 vmlinux_name = machine__mmap_name(machine, bf,
565 sizeof(bf));
566
567 kernel = dso__kernel_findnew(machine, vmlinux_name,
568 "[guest.kernel]",
569 DSO_TYPE_GUEST_KERNEL);
570 }
571
572 if (kernel != NULL && (!kernel->has_build_id))
573 dso__read_running_kernel_build_id(kernel, machine);
574
575 return kernel;
576}
577
578struct process_args {
579 u64 start;
580};
581
15a0a870
AH
582static void machine__get_kallsyms_filename(struct machine *machine, char *buf,
583 size_t bufsz)
584{
585 if (machine__is_default_guest(machine))
586 scnprintf(buf, bufsz, "%s", symbol_conf.default_guest_kallsyms);
587 else
588 scnprintf(buf, bufsz, "%s/proc/kallsyms", machine->root_dir);
589}
590
a93f0e55
SQ
591const char *ref_reloc_sym_names[] = {"_text", "_stext", NULL};
592
593/* Figure out the start address of kernel map from /proc/kallsyms.
594 * Returns the name of the start symbol in *symbol_name. Pass in NULL as
595 * symbol_name if it's not that important.
596 */
4b99375b
AH
597static u64 machine__get_running_kernel_start(struct machine *machine,
598 const char **symbol_name)
3f067dca 599{
15a0a870 600 char filename[PATH_MAX];
a93f0e55
SQ
601 int i;
602 const char *name;
603 u64 addr = 0;
3f067dca 604
15a0a870 605 machine__get_kallsyms_filename(machine, filename, PATH_MAX);
3f067dca
ACM
606
607 if (symbol__restricted_filename(filename, "/proc/kallsyms"))
608 return 0;
609
a93f0e55
SQ
610 for (i = 0; (name = ref_reloc_sym_names[i]) != NULL; i++) {
611 addr = kallsyms__get_function_start(filename, name);
612 if (addr)
613 break;
614 }
615
616 if (symbol_name)
617 *symbol_name = name;
3f067dca 618
a93f0e55 619 return addr;
3f067dca
ACM
620}
621
622int __machine__create_kernel_maps(struct machine *machine, struct dso *kernel)
623{
624 enum map_type type;
4b99375b 625 u64 start = machine__get_running_kernel_start(machine, NULL);
3f067dca
ACM
626
627 for (type = 0; type < MAP__NR_TYPES; ++type) {
628 struct kmap *kmap;
629
630 machine->vmlinux_maps[type] = map__new2(start, kernel, type);
631 if (machine->vmlinux_maps[type] == NULL)
632 return -1;
633
634 machine->vmlinux_maps[type]->map_ip =
635 machine->vmlinux_maps[type]->unmap_ip =
636 identity__map_ip;
637 kmap = map__kmap(machine->vmlinux_maps[type]);
638 kmap->kmaps = &machine->kmaps;
639 map_groups__insert(&machine->kmaps,
640 machine->vmlinux_maps[type]);
641 }
642
643 return 0;
644}
645
646void machine__destroy_kernel_maps(struct machine *machine)
647{
648 enum map_type type;
649
650 for (type = 0; type < MAP__NR_TYPES; ++type) {
651 struct kmap *kmap;
652
653 if (machine->vmlinux_maps[type] == NULL)
654 continue;
655
656 kmap = map__kmap(machine->vmlinux_maps[type]);
657 map_groups__remove(&machine->kmaps,
658 machine->vmlinux_maps[type]);
659 if (kmap->ref_reloc_sym) {
660 /*
661 * ref_reloc_sym is shared among all maps, so free just
662 * on one of them.
663 */
664 if (type == MAP__FUNCTION) {
04662523
ACM
665 zfree((char **)&kmap->ref_reloc_sym->name);
666 zfree(&kmap->ref_reloc_sym);
667 } else
668 kmap->ref_reloc_sym = NULL;
3f067dca
ACM
669 }
670
671 map__delete(machine->vmlinux_maps[type]);
672 machine->vmlinux_maps[type] = NULL;
673 }
674}
675
876650e6 676int machines__create_guest_kernel_maps(struct machines *machines)
3f067dca
ACM
677{
678 int ret = 0;
679 struct dirent **namelist = NULL;
680 int i, items = 0;
681 char path[PATH_MAX];
682 pid_t pid;
683 char *endp;
684
685 if (symbol_conf.default_guest_vmlinux_name ||
686 symbol_conf.default_guest_modules ||
687 symbol_conf.default_guest_kallsyms) {
688 machines__create_kernel_maps(machines, DEFAULT_GUEST_KERNEL_ID);
689 }
690
691 if (symbol_conf.guestmount) {
692 items = scandir(symbol_conf.guestmount, &namelist, NULL, NULL);
693 if (items <= 0)
694 return -ENOENT;
695 for (i = 0; i < items; i++) {
696 if (!isdigit(namelist[i]->d_name[0])) {
697 /* Filter out . and .. */
698 continue;
699 }
700 pid = (pid_t)strtol(namelist[i]->d_name, &endp, 10);
701 if ((*endp != '\0') ||
702 (endp == namelist[i]->d_name) ||
703 (errno == ERANGE)) {
704 pr_debug("invalid directory (%s). Skipping.\n",
705 namelist[i]->d_name);
706 continue;
707 }
708 sprintf(path, "%s/%s/proc/kallsyms",
709 symbol_conf.guestmount,
710 namelist[i]->d_name);
711 ret = access(path, R_OK);
712 if (ret) {
713 pr_debug("Can't access file %s\n", path);
714 goto failure;
715 }
716 machines__create_kernel_maps(machines, pid);
717 }
718failure:
719 free(namelist);
720 }
721
722 return ret;
723}
724
876650e6 725void machines__destroy_kernel_maps(struct machines *machines)
3f067dca 726{
876650e6
ACM
727 struct rb_node *next = rb_first(&machines->guests);
728
729 machine__destroy_kernel_maps(&machines->host);
3f067dca
ACM
730
731 while (next) {
732 struct machine *pos = rb_entry(next, struct machine, rb_node);
733
734 next = rb_next(&pos->rb_node);
876650e6 735 rb_erase(&pos->rb_node, &machines->guests);
3f067dca
ACM
736 machine__delete(pos);
737 }
738}
739
876650e6 740int machines__create_kernel_maps(struct machines *machines, pid_t pid)
3f067dca
ACM
741{
742 struct machine *machine = machines__findnew(machines, pid);
743
744 if (machine == NULL)
745 return -1;
746
747 return machine__create_kernel_maps(machine);
748}
749
750int machine__load_kallsyms(struct machine *machine, const char *filename,
751 enum map_type type, symbol_filter_t filter)
752{
753 struct map *map = machine->vmlinux_maps[type];
754 int ret = dso__load_kallsyms(map->dso, filename, map, filter);
755
756 if (ret > 0) {
757 dso__set_loaded(map->dso, type);
758 /*
759 * Since /proc/kallsyms will have multiple sessions for the
760 * kernel, with modules between them, fixup the end of all
761 * sections.
762 */
763 __map_groups__fixup_end(&machine->kmaps, type);
764 }
765
766 return ret;
767}
768
769int machine__load_vmlinux_path(struct machine *machine, enum map_type type,
770 symbol_filter_t filter)
771{
772 struct map *map = machine->vmlinux_maps[type];
773 int ret = dso__load_vmlinux_path(map->dso, map, filter);
774
39b12f78 775 if (ret > 0)
3f067dca 776 dso__set_loaded(map->dso, type);
3f067dca
ACM
777
778 return ret;
779}
780
781static void map_groups__fixup_end(struct map_groups *mg)
782{
783 int i;
784 for (i = 0; i < MAP__NR_TYPES; ++i)
785 __map_groups__fixup_end(mg, i);
786}
787
788static char *get_kernel_version(const char *root_dir)
789{
790 char version[PATH_MAX];
791 FILE *file;
792 char *name, *tmp;
793 const char *prefix = "Linux version ";
794
795 sprintf(version, "%s/proc/version", root_dir);
796 file = fopen(version, "r");
797 if (!file)
798 return NULL;
799
800 version[0] = '\0';
801 tmp = fgets(version, sizeof(version), file);
802 fclose(file);
803
804 name = strstr(version, prefix);
805 if (!name)
806 return NULL;
807 name += strlen(prefix);
808 tmp = strchr(name, ' ');
809 if (tmp)
810 *tmp = '\0';
811
812 return strdup(name);
813}
814
815static int map_groups__set_modules_path_dir(struct map_groups *mg,
61d4290c 816 const char *dir_name, int depth)
3f067dca
ACM
817{
818 struct dirent *dent;
819 DIR *dir = opendir(dir_name);
820 int ret = 0;
821
822 if (!dir) {
823 pr_debug("%s: cannot open %s dir\n", __func__, dir_name);
824 return -1;
825 }
826
827 while ((dent = readdir(dir)) != NULL) {
828 char path[PATH_MAX];
829 struct stat st;
830
831 /*sshfs might return bad dent->d_type, so we have to stat*/
832 snprintf(path, sizeof(path), "%s/%s", dir_name, dent->d_name);
833 if (stat(path, &st))
834 continue;
835
836 if (S_ISDIR(st.st_mode)) {
837 if (!strcmp(dent->d_name, ".") ||
838 !strcmp(dent->d_name, ".."))
839 continue;
840
61d4290c
RY
841 /* Do not follow top-level source and build symlinks */
842 if (depth == 0) {
843 if (!strcmp(dent->d_name, "source") ||
844 !strcmp(dent->d_name, "build"))
845 continue;
846 }
847
848 ret = map_groups__set_modules_path_dir(mg, path,
849 depth + 1);
3f067dca
ACM
850 if (ret < 0)
851 goto out;
852 } else {
853 char *dot = strrchr(dent->d_name, '.'),
854 dso_name[PATH_MAX];
855 struct map *map;
856 char *long_name;
857
858 if (dot == NULL || strcmp(dot, ".ko"))
859 continue;
860 snprintf(dso_name, sizeof(dso_name), "[%.*s]",
861 (int)(dot - dent->d_name), dent->d_name);
862
863 strxfrchar(dso_name, '-', '_');
864 map = map_groups__find_by_name(mg, MAP__FUNCTION,
865 dso_name);
866 if (map == NULL)
867 continue;
868
869 long_name = strdup(path);
870 if (long_name == NULL) {
871 ret = -1;
872 goto out;
873 }
7e155d4d 874 dso__set_long_name(map->dso, long_name, true);
3f067dca
ACM
875 dso__kernel_module_get_build_id(map->dso, "");
876 }
877 }
878
879out:
880 closedir(dir);
881 return ret;
882}
883
884static int machine__set_modules_path(struct machine *machine)
885{
886 char *version;
887 char modules_path[PATH_MAX];
888
889 version = get_kernel_version(machine->root_dir);
890 if (!version)
891 return -1;
892
61d4290c 893 snprintf(modules_path, sizeof(modules_path), "%s/lib/modules/%s",
3f067dca
ACM
894 machine->root_dir, version);
895 free(version);
896
61d4290c 897 return map_groups__set_modules_path_dir(&machine->kmaps, modules_path, 0);
3f067dca
ACM
898}
899
316d70d6 900static int machine__create_module(void *arg, const char *name, u64 start)
3f067dca 901{
316d70d6 902 struct machine *machine = arg;
3f067dca 903 struct map *map;
316d70d6
AH
904
905 map = machine__new_module(machine, start, name);
906 if (map == NULL)
907 return -1;
908
909 dso__kernel_module_get_build_id(map->dso, machine->root_dir);
910
911 return 0;
912}
913
914static int machine__create_modules(struct machine *machine)
915{
3f067dca
ACM
916 const char *modules;
917 char path[PATH_MAX];
918
f4be904d 919 if (machine__is_default_guest(machine)) {
3f067dca 920 modules = symbol_conf.default_guest_modules;
f4be904d
AH
921 } else {
922 snprintf(path, PATH_MAX, "%s/proc/modules", machine->root_dir);
3f067dca
ACM
923 modules = path;
924 }
925
aa7fe3b0 926 if (symbol__restricted_filename(modules, "/proc/modules"))
3f067dca
ACM
927 return -1;
928
316d70d6 929 if (modules__parse(modules, machine, machine__create_module))
3f067dca
ACM
930 return -1;
931
316d70d6
AH
932 if (!machine__set_modules_path(machine))
933 return 0;
3f067dca 934
316d70d6 935 pr_debug("Problems setting modules path maps, continuing anyway...\n");
3f067dca 936
8f76fcd9 937 return 0;
3f067dca
ACM
938}
939
940int machine__create_kernel_maps(struct machine *machine)
941{
942 struct dso *kernel = machine__get_kernel(machine);
5512cf24 943 const char *name;
4b99375b 944 u64 addr = machine__get_running_kernel_start(machine, &name);
5512cf24
AH
945 if (!addr)
946 return -1;
3f067dca
ACM
947
948 if (kernel == NULL ||
949 __machine__create_kernel_maps(machine, kernel) < 0)
950 return -1;
951
952 if (symbol_conf.use_modules && machine__create_modules(machine) < 0) {
953 if (machine__is_host(machine))
954 pr_debug("Problems creating module maps, "
955 "continuing anyway...\n");
956 else
957 pr_debug("Problems creating module maps for guest %d, "
958 "continuing anyway...\n", machine->pid);
959 }
960
961 /*
962 * Now that we have all the maps created, just set the ->end of them:
963 */
964 map_groups__fixup_end(&machine->kmaps);
5512cf24
AH
965
966 if (maps__set_kallsyms_ref_reloc_sym(machine->vmlinux_maps, name,
967 addr)) {
968 machine__destroy_kernel_maps(machine);
969 return -1;
970 }
971
3f067dca
ACM
972 return 0;
973}
974
b0a7d1a0
ACM
975static void machine__set_kernel_mmap_len(struct machine *machine,
976 union perf_event *event)
977{
4552cf0f
NK
978 int i;
979
980 for (i = 0; i < MAP__NR_TYPES; i++) {
981 machine->vmlinux_maps[i]->start = event->mmap.start;
982 machine->vmlinux_maps[i]->end = (event->mmap.start +
983 event->mmap.len);
984 /*
985 * Be a bit paranoid here, some perf.data file came with
986 * a zero sized synthesized MMAP event for the kernel.
987 */
988 if (machine->vmlinux_maps[i]->end == 0)
989 machine->vmlinux_maps[i]->end = ~0ULL;
990 }
b0a7d1a0
ACM
991}
992
8e0cf965
AH
993static bool machine__uses_kcore(struct machine *machine)
994{
995 struct dso *dso;
996
997 list_for_each_entry(dso, &machine->kernel_dsos, node) {
998 if (dso__is_kcore(dso))
999 return true;
1000 }
1001
1002 return false;
1003}
1004
b0a7d1a0
ACM
1005static int machine__process_kernel_mmap_event(struct machine *machine,
1006 union perf_event *event)
1007{
1008 struct map *map;
1009 char kmmap_prefix[PATH_MAX];
1010 enum dso_kernel_type kernel_type;
1011 bool is_kernel_mmap;
1012
8e0cf965
AH
1013 /* If we have maps from kcore then we do not need or want any others */
1014 if (machine__uses_kcore(machine))
1015 return 0;
1016
b0a7d1a0
ACM
1017 machine__mmap_name(machine, kmmap_prefix, sizeof(kmmap_prefix));
1018 if (machine__is_host(machine))
1019 kernel_type = DSO_TYPE_KERNEL;
1020 else
1021 kernel_type = DSO_TYPE_GUEST_KERNEL;
1022
1023 is_kernel_mmap = memcmp(event->mmap.filename,
1024 kmmap_prefix,
1025 strlen(kmmap_prefix) - 1) == 0;
1026 if (event->mmap.filename[0] == '/' ||
1027 (!is_kernel_mmap && event->mmap.filename[0] == '[')) {
1028
1029 char short_module_name[1024];
1030 char *name, *dot;
1031
1032 if (event->mmap.filename[0] == '/') {
1033 name = strrchr(event->mmap.filename, '/');
1034 if (name == NULL)
1035 goto out_problem;
1036
1037 ++name; /* skip / */
1038 dot = strrchr(name, '.');
1039 if (dot == NULL)
1040 goto out_problem;
1041 snprintf(short_module_name, sizeof(short_module_name),
1042 "[%.*s]", (int)(dot - name), name);
1043 strxfrchar(short_module_name, '-', '_');
1044 } else
1045 strcpy(short_module_name, event->mmap.filename);
1046
1047 map = machine__new_module(machine, event->mmap.start,
1048 event->mmap.filename);
1049 if (map == NULL)
1050 goto out_problem;
1051
1052 name = strdup(short_module_name);
1053 if (name == NULL)
1054 goto out_problem;
1055
58a98c9c 1056 dso__set_short_name(map->dso, name, true);
b0a7d1a0
ACM
1057 map->end = map->start + event->mmap.len;
1058 } else if (is_kernel_mmap) {
1059 const char *symbol_name = (event->mmap.filename +
1060 strlen(kmmap_prefix));
1061 /*
1062 * Should be there already, from the build-id table in
1063 * the header.
1064 */
1065 struct dso *kernel = __dsos__findnew(&machine->kernel_dsos,
1066 kmmap_prefix);
1067 if (kernel == NULL)
1068 goto out_problem;
1069
1070 kernel->kernel = kernel_type;
1071 if (__machine__create_kernel_maps(machine, kernel) < 0)
1072 goto out_problem;
1073
1074 machine__set_kernel_mmap_len(machine, event);
1075
1076 /*
1077 * Avoid using a zero address (kptr_restrict) for the ref reloc
1078 * symbol. Effectively having zero here means that at record
1079 * time /proc/sys/kernel/kptr_restrict was non zero.
1080 */
1081 if (event->mmap.pgoff != 0) {
1082 maps__set_kallsyms_ref_reloc_sym(machine->vmlinux_maps,
1083 symbol_name,
1084 event->mmap.pgoff);
1085 }
1086
1087 if (machine__is_default_guest(machine)) {
1088 /*
1089 * preload dso of guest kernel and modules
1090 */
1091 dso__load(kernel, machine->vmlinux_maps[MAP__FUNCTION],
1092 NULL);
1093 }
1094 }
1095 return 0;
1096out_problem:
1097 return -1;
1098}
1099
5c5e854b 1100int machine__process_mmap2_event(struct machine *machine,
162f0bef
FW
1101 union perf_event *event,
1102 struct perf_sample *sample __maybe_unused)
5c5e854b
SE
1103{
1104 u8 cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
1105 struct thread *thread;
1106 struct map *map;
1107 enum map_type type;
1108 int ret = 0;
1109
1110 if (dump_trace)
1111 perf_event__fprintf_mmap2(event, stdout);
1112
1113 if (cpumode == PERF_RECORD_MISC_GUEST_KERNEL ||
1114 cpumode == PERF_RECORD_MISC_KERNEL) {
1115 ret = machine__process_kernel_mmap_event(machine, event);
1116 if (ret < 0)
1117 goto out_problem;
1118 return 0;
1119 }
1120
1121 thread = machine__findnew_thread(machine, event->mmap2.pid,
11c9abf2 1122 event->mmap2.tid);
5c5e854b
SE
1123 if (thread == NULL)
1124 goto out_problem;
1125
1126 if (event->header.misc & PERF_RECORD_MISC_MMAP_DATA)
1127 type = MAP__VARIABLE;
1128 else
1129 type = MAP__FUNCTION;
1130
2a03068c 1131 map = map__new(machine, event->mmap2.start,
5c5e854b
SE
1132 event->mmap2.len, event->mmap2.pgoff,
1133 event->mmap2.pid, event->mmap2.maj,
1134 event->mmap2.min, event->mmap2.ino,
1135 event->mmap2.ino_generation,
7ef80703
DZ
1136 event->mmap2.prot,
1137 event->mmap2.flags,
5835edda 1138 event->mmap2.filename, type, thread);
5c5e854b
SE
1139
1140 if (map == NULL)
1141 goto out_problem;
1142
1143 thread__insert_map(thread, map);
1144 return 0;
1145
1146out_problem:
1147 dump_printf("problem processing PERF_RECORD_MMAP2, skipping event.\n");
1148 return 0;
1149}
1150
162f0bef
FW
1151int machine__process_mmap_event(struct machine *machine, union perf_event *event,
1152 struct perf_sample *sample __maybe_unused)
b0a7d1a0
ACM
1153{
1154 u8 cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
1155 struct thread *thread;
1156 struct map *map;
bad40917 1157 enum map_type type;
b0a7d1a0
ACM
1158 int ret = 0;
1159
1160 if (dump_trace)
1161 perf_event__fprintf_mmap(event, stdout);
1162
1163 if (cpumode == PERF_RECORD_MISC_GUEST_KERNEL ||
1164 cpumode == PERF_RECORD_MISC_KERNEL) {
1165 ret = machine__process_kernel_mmap_event(machine, event);
1166 if (ret < 0)
1167 goto out_problem;
1168 return 0;
1169 }
1170
314add6b 1171 thread = machine__findnew_thread(machine, event->mmap.pid,
11c9abf2 1172 event->mmap.tid);
b0a7d1a0
ACM
1173 if (thread == NULL)
1174 goto out_problem;
bad40917
SE
1175
1176 if (event->header.misc & PERF_RECORD_MISC_MMAP_DATA)
1177 type = MAP__VARIABLE;
1178 else
1179 type = MAP__FUNCTION;
1180
2a03068c 1181 map = map__new(machine, event->mmap.start,
b0a7d1a0 1182 event->mmap.len, event->mmap.pgoff,
7ef80703 1183 event->mmap.pid, 0, 0, 0, 0, 0, 0,
5c5e854b 1184 event->mmap.filename,
5835edda 1185 type, thread);
bad40917 1186
b0a7d1a0
ACM
1187 if (map == NULL)
1188 goto out_problem;
1189
1190 thread__insert_map(thread, map);
1191 return 0;
1192
1193out_problem:
1194 dump_printf("problem processing PERF_RECORD_MMAP, skipping event.\n");
1195 return 0;
1196}
1197
236a3bbd
DA
1198static void machine__remove_thread(struct machine *machine, struct thread *th)
1199{
1200 machine->last_match = NULL;
1201 rb_erase(&th->rb_node, &machine->threads);
1202 /*
1203 * We may have references to this thread, for instance in some hist_entry
1204 * instances, so just move them to a separate list.
1205 */
1206 list_add_tail(&th->node, &machine->dead_threads);
1207}
1208
162f0bef
FW
1209int machine__process_fork_event(struct machine *machine, union perf_event *event,
1210 struct perf_sample *sample)
b0a7d1a0 1211{
d75e6097
JO
1212 struct thread *thread = machine__find_thread(machine,
1213 event->fork.pid,
1214 event->fork.tid);
314add6b
AH
1215 struct thread *parent = machine__findnew_thread(machine,
1216 event->fork.ppid,
1217 event->fork.ptid);
b0a7d1a0 1218
236a3bbd
DA
1219 /* if a thread currently exists for the thread id remove it */
1220 if (thread != NULL)
1221 machine__remove_thread(machine, thread);
1222
314add6b
AH
1223 thread = machine__findnew_thread(machine, event->fork.pid,
1224 event->fork.tid);
b0a7d1a0
ACM
1225 if (dump_trace)
1226 perf_event__fprintf_task(event, stdout);
1227
1228 if (thread == NULL || parent == NULL ||
162f0bef 1229 thread__fork(thread, parent, sample->time) < 0) {
b0a7d1a0
ACM
1230 dump_printf("problem processing PERF_RECORD_FORK, skipping event.\n");
1231 return -1;
1232 }
1233
1234 return 0;
1235}
1236
162f0bef
FW
1237int machine__process_exit_event(struct machine *machine, union perf_event *event,
1238 struct perf_sample *sample __maybe_unused)
b0a7d1a0 1239{
d75e6097
JO
1240 struct thread *thread = machine__find_thread(machine,
1241 event->fork.pid,
1242 event->fork.tid);
b0a7d1a0
ACM
1243
1244 if (dump_trace)
1245 perf_event__fprintf_task(event, stdout);
1246
1247 if (thread != NULL)
236a3bbd 1248 thread__exited(thread);
b0a7d1a0
ACM
1249
1250 return 0;
1251}
1252
162f0bef
FW
1253int machine__process_event(struct machine *machine, union perf_event *event,
1254 struct perf_sample *sample)
b0a7d1a0
ACM
1255{
1256 int ret;
1257
1258 switch (event->header.type) {
1259 case PERF_RECORD_COMM:
162f0bef 1260 ret = machine__process_comm_event(machine, event, sample); break;
b0a7d1a0 1261 case PERF_RECORD_MMAP:
162f0bef 1262 ret = machine__process_mmap_event(machine, event, sample); break;
5c5e854b 1263 case PERF_RECORD_MMAP2:
162f0bef 1264 ret = machine__process_mmap2_event(machine, event, sample); break;
b0a7d1a0 1265 case PERF_RECORD_FORK:
162f0bef 1266 ret = machine__process_fork_event(machine, event, sample); break;
b0a7d1a0 1267 case PERF_RECORD_EXIT:
162f0bef 1268 ret = machine__process_exit_event(machine, event, sample); break;
b0a7d1a0 1269 case PERF_RECORD_LOST:
162f0bef 1270 ret = machine__process_lost_event(machine, event, sample); break;
b0a7d1a0
ACM
1271 default:
1272 ret = -1;
1273 break;
1274 }
1275
1276 return ret;
1277}
3f067dca 1278
b21484f1 1279static bool symbol__match_regex(struct symbol *sym, regex_t *regex)
3f067dca 1280{
b21484f1 1281 if (sym->name && !regexec(regex, sym->name, 0, NULL, 0))
3f067dca 1282 return 1;
3f067dca
ACM
1283 return 0;
1284}
1285
3f067dca
ACM
1286static void ip__resolve_ams(struct machine *machine, struct thread *thread,
1287 struct addr_map_symbol *ams,
1288 u64 ip)
1289{
1290 struct addr_location al;
3f067dca
ACM
1291
1292 memset(&al, 0, sizeof(al));
52a3cb8c
ACM
1293 /*
1294 * We cannot use the header.misc hint to determine whether a
1295 * branch stack address is user, kernel, guest, hypervisor.
1296 * Branches may straddle the kernel/user/hypervisor boundaries.
1297 * Thus, we have to try consecutively until we find a match
1298 * or else, the symbol is unknown
1299 */
1300 thread__find_cpumode_addr_location(thread, machine, MAP__FUNCTION, ip, &al);
3f067dca 1301
3f067dca
ACM
1302 ams->addr = ip;
1303 ams->al_addr = al.addr;
1304 ams->sym = al.sym;
1305 ams->map = al.map;
1306}
1307
98a3b32c
SE
1308static void ip__resolve_data(struct machine *machine, struct thread *thread,
1309 u8 m, struct addr_map_symbol *ams, u64 addr)
1310{
1311 struct addr_location al;
1312
1313 memset(&al, 0, sizeof(al));
1314
61710bde
AH
1315 thread__find_addr_location(thread, machine, m, MAP__VARIABLE, addr,
1316 &al);
98a3b32c
SE
1317 ams->addr = addr;
1318 ams->al_addr = al.addr;
1319 ams->sym = al.sym;
1320 ams->map = al.map;
1321}
1322
e80faac0
ACM
1323struct mem_info *sample__resolve_mem(struct perf_sample *sample,
1324 struct addr_location *al)
98a3b32c
SE
1325{
1326 struct mem_info *mi = zalloc(sizeof(*mi));
1327
1328 if (!mi)
1329 return NULL;
1330
e80faac0
ACM
1331 ip__resolve_ams(al->machine, al->thread, &mi->iaddr, sample->ip);
1332 ip__resolve_data(al->machine, al->thread, al->cpumode,
1333 &mi->daddr, sample->addr);
98a3b32c
SE
1334 mi->data_src.val = sample->data_src;
1335
1336 return mi;
1337}
1338
644f2df2
ACM
1339struct branch_info *sample__resolve_bstack(struct perf_sample *sample,
1340 struct addr_location *al)
3f067dca 1341{
3f067dca 1342 unsigned int i;
644f2df2
ACM
1343 const struct branch_stack *bs = sample->branch_stack;
1344 struct branch_info *bi = calloc(bs->nr, sizeof(struct branch_info));
3f067dca 1345
3f067dca
ACM
1346 if (!bi)
1347 return NULL;
1348
1349 for (i = 0; i < bs->nr; i++) {
644f2df2
ACM
1350 ip__resolve_ams(al->machine, al->thread, &bi[i].to, bs->entries[i].to);
1351 ip__resolve_ams(al->machine, al->thread, &bi[i].from, bs->entries[i].from);
3f067dca
ACM
1352 bi[i].flags = bs->entries[i].flags;
1353 }
1354 return bi;
1355}
1356
1357static int machine__resolve_callchain_sample(struct machine *machine,
1358 struct thread *thread,
1359 struct ip_callchain *chain,
b21484f1 1360 struct symbol **parent,
91e95617
WL
1361 struct addr_location *root_al,
1362 int max_stack)
3f067dca
ACM
1363{
1364 u8 cpumode = PERF_RECORD_MISC_USER;
91e95617
WL
1365 int chain_nr = min(max_stack, (int)chain->nr);
1366 int i;
a60335ba 1367 int j;
3f067dca 1368 int err;
a60335ba 1369 int skip_idx __maybe_unused;
3f067dca
ACM
1370
1371 callchain_cursor_reset(&callchain_cursor);
1372
1373 if (chain->nr > PERF_MAX_STACK_DEPTH) {
1374 pr_warning("corrupted callchain. skipping...\n");
1375 return 0;
1376 }
1377
a60335ba
SB
1378 /*
1379 * Based on DWARF debug information, some architectures skip
1380 * a callchain entry saved by the kernel.
1381 */
1382 skip_idx = arch_skip_callchain_idx(machine, thread, chain);
1383
91e95617 1384 for (i = 0; i < chain_nr; i++) {
3f067dca
ACM
1385 u64 ip;
1386 struct addr_location al;
1387
1388 if (callchain_param.order == ORDER_CALLEE)
a60335ba 1389 j = i;
3f067dca 1390 else
a60335ba
SB
1391 j = chain->nr - i - 1;
1392
1393#ifdef HAVE_SKIP_CALLCHAIN_IDX
1394 if (j == skip_idx)
1395 continue;
1396#endif
1397 ip = chain->ips[j];
3f067dca
ACM
1398
1399 if (ip >= PERF_CONTEXT_MAX) {
1400 switch (ip) {
1401 case PERF_CONTEXT_HV:
1402 cpumode = PERF_RECORD_MISC_HYPERVISOR;
1403 break;
1404 case PERF_CONTEXT_KERNEL:
1405 cpumode = PERF_RECORD_MISC_KERNEL;
1406 break;
1407 case PERF_CONTEXT_USER:
1408 cpumode = PERF_RECORD_MISC_USER;
1409 break;
1410 default:
1411 pr_debug("invalid callchain context: "
1412 "%"PRId64"\n", (s64) ip);
1413 /*
1414 * It seems the callchain is corrupted.
1415 * Discard all.
1416 */
1417 callchain_cursor_reset(&callchain_cursor);
1418 return 0;
1419 }
1420 continue;
1421 }
1422
b3cef7f6 1423 al.filtered = 0;
3f067dca 1424 thread__find_addr_location(thread, machine, cpumode,
61710bde 1425 MAP__FUNCTION, ip, &al);
3f067dca
ACM
1426 if (al.sym != NULL) {
1427 if (sort__has_parent && !*parent &&
b21484f1 1428 symbol__match_regex(al.sym, &parent_regex))
3f067dca 1429 *parent = al.sym;
b21484f1
GP
1430 else if (have_ignore_callees && root_al &&
1431 symbol__match_regex(al.sym, &ignore_callees_regex)) {
1432 /* Treat this symbol as the root,
1433 forgetting its callees. */
1434 *root_al = al;
1435 callchain_cursor_reset(&callchain_cursor);
1436 }
3f067dca
ACM
1437 }
1438
1439 err = callchain_cursor_append(&callchain_cursor,
1440 ip, al.map, al.sym);
1441 if (err)
1442 return err;
1443 }
1444
1445 return 0;
1446}
1447
1448static int unwind_entry(struct unwind_entry *entry, void *arg)
1449{
1450 struct callchain_cursor *cursor = arg;
1451 return callchain_cursor_append(cursor, entry->ip,
1452 entry->map, entry->sym);
1453}
1454
1455int machine__resolve_callchain(struct machine *machine,
1456 struct perf_evsel *evsel,
1457 struct thread *thread,
1458 struct perf_sample *sample,
b21484f1 1459 struct symbol **parent,
91e95617
WL
1460 struct addr_location *root_al,
1461 int max_stack)
3f067dca
ACM
1462{
1463 int ret;
1464
3f067dca 1465 ret = machine__resolve_callchain_sample(machine, thread,
91e95617
WL
1466 sample->callchain, parent,
1467 root_al, max_stack);
3f067dca
ACM
1468 if (ret)
1469 return ret;
1470
1471 /* Can we do dwarf post unwind? */
1472 if (!((evsel->attr.sample_type & PERF_SAMPLE_REGS_USER) &&
1473 (evsel->attr.sample_type & PERF_SAMPLE_STACK_USER)))
1474 return 0;
1475
1476 /* Bail out if nothing was captured. */
1477 if ((!sample->user_regs.regs) ||
1478 (!sample->user_stack.size))
1479 return 0;
1480
1481 return unwind__get_entries(unwind_entry, &callchain_cursor, machine,
352ea45a 1482 thread, sample, max_stack);
3f067dca
ACM
1483
1484}
35feee19
DA
1485
1486int machine__for_each_thread(struct machine *machine,
1487 int (*fn)(struct thread *thread, void *p),
1488 void *priv)
1489{
1490 struct rb_node *nd;
1491 struct thread *thread;
1492 int rc = 0;
1493
1494 for (nd = rb_first(&machine->threads); nd; nd = rb_next(nd)) {
1495 thread = rb_entry(nd, struct thread, rb_node);
1496 rc = fn(thread, priv);
1497 if (rc != 0)
1498 return rc;
1499 }
1500
1501 list_for_each_entry(thread, &machine->dead_threads, node) {
1502 rc = fn(thread, priv);
1503 if (rc != 0)
1504 return rc;
1505 }
1506 return rc;
1507}
58d925dc 1508
a33fbd56 1509int __machine__synthesize_threads(struct machine *machine, struct perf_tool *tool,
602ad878 1510 struct target *target, struct thread_map *threads,
a33fbd56 1511 perf_event__handler_t process, bool data_mmap)
58d925dc 1512{
602ad878 1513 if (target__has_task(target))
58d925dc 1514 return perf_event__synthesize_thread_map(tool, threads, process, machine, data_mmap);
602ad878 1515 else if (target__has_cpu(target))
58d925dc
ACM
1516 return perf_event__synthesize_threads(tool, process, machine, data_mmap);
1517 /* command specified */
1518 return 0;
1519}
b9d266ba
AH
1520
1521pid_t machine__get_current_tid(struct machine *machine, int cpu)
1522{
1523 if (cpu < 0 || cpu >= MAX_NR_CPUS || !machine->current_tid)
1524 return -1;
1525
1526 return machine->current_tid[cpu];
1527}
1528
1529int machine__set_current_tid(struct machine *machine, int cpu, pid_t pid,
1530 pid_t tid)
1531{
1532 struct thread *thread;
1533
1534 if (cpu < 0)
1535 return -EINVAL;
1536
1537 if (!machine->current_tid) {
1538 int i;
1539
1540 machine->current_tid = calloc(MAX_NR_CPUS, sizeof(pid_t));
1541 if (!machine->current_tid)
1542 return -ENOMEM;
1543 for (i = 0; i < MAX_NR_CPUS; i++)
1544 machine->current_tid[i] = -1;
1545 }
1546
1547 if (cpu >= MAX_NR_CPUS) {
1548 pr_err("Requested CPU %d too large. ", cpu);
1549 pr_err("Consider raising MAX_NR_CPUS\n");
1550 return -EINVAL;
1551 }
1552
1553 machine->current_tid[cpu] = tid;
1554
1555 thread = machine__findnew_thread(machine, pid, tid);
1556 if (!thread)
1557 return -ENOMEM;
1558
1559 thread->cpu = cpu;
1560
1561 return 0;
1562}
fbe2af45
AH
1563
1564int machine__get_kernel_start(struct machine *machine)
1565{
1566 struct map *map = machine__kernel_map(machine, MAP__FUNCTION);
1567 int err = 0;
1568
1569 /*
1570 * The only addresses above 2^63 are kernel addresses of a 64-bit
1571 * kernel. Note that addresses are unsigned so that on a 32-bit system
1572 * all addresses including kernel addresses are less than 2^32. In
1573 * that case (32-bit system), if the kernel mapping is unknown, all
1574 * addresses will be assumed to be in user space - see
1575 * machine__kernel_ip().
1576 */
1577 machine->kernel_start = 1ULL << 63;
1578 if (map) {
1579 err = map__load(map, machine->symbol_filter);
1580 if (map->start)
1581 machine->kernel_start = map->start;
1582 }
1583 return err;
1584}