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