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