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