Merge tag 'nfsd-4.7' of git://linux-nfs.org/~bfields/linux
[deliverable/linux.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 364/*
bd1a0be5 365 * Caller must eventually drop thread->refcnt returned with a successful
abd82868
ACM
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)
712 vmlinux_name = "[kernel.kallsyms]";
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;
4b99375b 1138 u64 addr = machine__get_running_kernel_start(machine, &name);
1154c957
MH
1139 int ret;
1140
1141 if (!addr || 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
AH
1162
1163 if (maps__set_kallsyms_ref_reloc_sym(machine->vmlinux_maps, name,
1164 addr)) {
1165 machine__destroy_kernel_maps(machine);
1166 return -1;
1167 }
1168
3f067dca
ACM
1169 return 0;
1170}
1171
b0a7d1a0
ACM
1172static void machine__set_kernel_mmap_len(struct machine *machine,
1173 union perf_event *event)
1174{
4552cf0f
NK
1175 int i;
1176
1177 for (i = 0; i < MAP__NR_TYPES; i++) {
1178 machine->vmlinux_maps[i]->start = event->mmap.start;
1179 machine->vmlinux_maps[i]->end = (event->mmap.start +
1180 event->mmap.len);
1181 /*
1182 * Be a bit paranoid here, some perf.data file came with
1183 * a zero sized synthesized MMAP event for the kernel.
1184 */
1185 if (machine->vmlinux_maps[i]->end == 0)
1186 machine->vmlinux_maps[i]->end = ~0ULL;
1187 }
b0a7d1a0
ACM
1188}
1189
8e0cf965
AH
1190static bool machine__uses_kcore(struct machine *machine)
1191{
1192 struct dso *dso;
1193
3d39ac53 1194 list_for_each_entry(dso, &machine->dsos.head, node) {
8e0cf965
AH
1195 if (dso__is_kcore(dso))
1196 return true;
1197 }
1198
1199 return false;
1200}
1201
b0a7d1a0
ACM
1202static int machine__process_kernel_mmap_event(struct machine *machine,
1203 union perf_event *event)
1204{
1205 struct map *map;
1206 char kmmap_prefix[PATH_MAX];
1207 enum dso_kernel_type kernel_type;
1208 bool is_kernel_mmap;
1209
8e0cf965
AH
1210 /* If we have maps from kcore then we do not need or want any others */
1211 if (machine__uses_kcore(machine))
1212 return 0;
1213
b0a7d1a0
ACM
1214 machine__mmap_name(machine, kmmap_prefix, sizeof(kmmap_prefix));
1215 if (machine__is_host(machine))
1216 kernel_type = DSO_TYPE_KERNEL;
1217 else
1218 kernel_type = DSO_TYPE_GUEST_KERNEL;
1219
1220 is_kernel_mmap = memcmp(event->mmap.filename,
1221 kmmap_prefix,
1222 strlen(kmmap_prefix) - 1) == 0;
1223 if (event->mmap.filename[0] == '/' ||
1224 (!is_kernel_mmap && event->mmap.filename[0] == '[')) {
9f2de315
ACM
1225 map = machine__findnew_module_map(machine, event->mmap.start,
1226 event->mmap.filename);
b0a7d1a0
ACM
1227 if (map == NULL)
1228 goto out_problem;
1229
b0a7d1a0
ACM
1230 map->end = map->start + event->mmap.len;
1231 } else if (is_kernel_mmap) {
1232 const char *symbol_name = (event->mmap.filename +
1233 strlen(kmmap_prefix));
1234 /*
1235 * Should be there already, from the build-id table in
1236 * the header.
1237 */
b837a8bd
NK
1238 struct dso *kernel = NULL;
1239 struct dso *dso;
1240
e8807844
ACM
1241 pthread_rwlock_rdlock(&machine->dsos.lock);
1242
3d39ac53 1243 list_for_each_entry(dso, &machine->dsos.head, node) {
1f121b03
WN
1244
1245 /*
1246 * The cpumode passed to is_kernel_module is not the
1247 * cpumode of *this* event. If we insist on passing
1248 * correct cpumode to is_kernel_module, we should
1249 * record the cpumode when we adding this dso to the
1250 * linked list.
1251 *
1252 * However we don't really need passing correct
1253 * cpumode. We know the correct cpumode must be kernel
1254 * mode (if not, we should not link it onto kernel_dsos
1255 * list).
1256 *
1257 * Therefore, we pass PERF_RECORD_MISC_CPUMODE_UNKNOWN.
1258 * is_kernel_module() treats it as a kernel cpumode.
1259 */
1260
1261 if (!dso->kernel ||
1262 is_kernel_module(dso->long_name,
1263 PERF_RECORD_MISC_CPUMODE_UNKNOWN))
b837a8bd
NK
1264 continue;
1265
1f121b03 1266
b837a8bd
NK
1267 kernel = dso;
1268 break;
1269 }
1270
e8807844
ACM
1271 pthread_rwlock_unlock(&machine->dsos.lock);
1272
b837a8bd 1273 if (kernel == NULL)
aa7cc2ae 1274 kernel = machine__findnew_dso(machine, kmmap_prefix);
b0a7d1a0
ACM
1275 if (kernel == NULL)
1276 goto out_problem;
1277
1278 kernel->kernel = kernel_type;
d3a7c489
ACM
1279 if (__machine__create_kernel_maps(machine, kernel) < 0) {
1280 dso__put(kernel);
b0a7d1a0 1281 goto out_problem;
d3a7c489 1282 }
b0a7d1a0 1283
330dfa22
NK
1284 if (strstr(kernel->long_name, "vmlinux"))
1285 dso__set_short_name(kernel, "[kernel.vmlinux]", false);
96d78059 1286
b0a7d1a0
ACM
1287 machine__set_kernel_mmap_len(machine, event);
1288
1289 /*
1290 * Avoid using a zero address (kptr_restrict) for the ref reloc
1291 * symbol. Effectively having zero here means that at record
1292 * time /proc/sys/kernel/kptr_restrict was non zero.
1293 */
1294 if (event->mmap.pgoff != 0) {
1295 maps__set_kallsyms_ref_reloc_sym(machine->vmlinux_maps,
1296 symbol_name,
1297 event->mmap.pgoff);
1298 }
1299
1300 if (machine__is_default_guest(machine)) {
1301 /*
1302 * preload dso of guest kernel and modules
1303 */
a5e813c6 1304 dso__load(kernel, machine__kernel_map(machine), NULL);
b0a7d1a0
ACM
1305 }
1306 }
1307 return 0;
1308out_problem:
1309 return -1;
1310}
1311
5c5e854b 1312int machine__process_mmap2_event(struct machine *machine,
162f0bef 1313 union perf_event *event,
473398a2 1314 struct perf_sample *sample)
5c5e854b 1315{
5c5e854b
SE
1316 struct thread *thread;
1317 struct map *map;
1318 enum map_type type;
1319 int ret = 0;
1320
1321 if (dump_trace)
1322 perf_event__fprintf_mmap2(event, stdout);
1323
473398a2
ACM
1324 if (sample->cpumode == PERF_RECORD_MISC_GUEST_KERNEL ||
1325 sample->cpumode == PERF_RECORD_MISC_KERNEL) {
5c5e854b
SE
1326 ret = machine__process_kernel_mmap_event(machine, event);
1327 if (ret < 0)
1328 goto out_problem;
1329 return 0;
1330 }
1331
1332 thread = machine__findnew_thread(machine, event->mmap2.pid,
11c9abf2 1333 event->mmap2.tid);
5c5e854b
SE
1334 if (thread == NULL)
1335 goto out_problem;
1336
1337 if (event->header.misc & PERF_RECORD_MISC_MMAP_DATA)
1338 type = MAP__VARIABLE;
1339 else
1340 type = MAP__FUNCTION;
1341
2a03068c 1342 map = map__new(machine, event->mmap2.start,
5c5e854b
SE
1343 event->mmap2.len, event->mmap2.pgoff,
1344 event->mmap2.pid, event->mmap2.maj,
1345 event->mmap2.min, event->mmap2.ino,
1346 event->mmap2.ino_generation,
7ef80703
DZ
1347 event->mmap2.prot,
1348 event->mmap2.flags,
5835edda 1349 event->mmap2.filename, type, thread);
5c5e854b
SE
1350
1351 if (map == NULL)
b91fc39f 1352 goto out_problem_map;
5c5e854b
SE
1353
1354 thread__insert_map(thread, map);
b91fc39f 1355 thread__put(thread);
84c2cafa 1356 map__put(map);
5c5e854b
SE
1357 return 0;
1358
b91fc39f
ACM
1359out_problem_map:
1360 thread__put(thread);
5c5e854b
SE
1361out_problem:
1362 dump_printf("problem processing PERF_RECORD_MMAP2, skipping event.\n");
1363 return 0;
1364}
1365
162f0bef 1366int machine__process_mmap_event(struct machine *machine, union perf_event *event,
473398a2 1367 struct perf_sample *sample)
b0a7d1a0 1368{
b0a7d1a0
ACM
1369 struct thread *thread;
1370 struct map *map;
bad40917 1371 enum map_type type;
b0a7d1a0
ACM
1372 int ret = 0;
1373
1374 if (dump_trace)
1375 perf_event__fprintf_mmap(event, stdout);
1376
473398a2
ACM
1377 if (sample->cpumode == PERF_RECORD_MISC_GUEST_KERNEL ||
1378 sample->cpumode == PERF_RECORD_MISC_KERNEL) {
b0a7d1a0
ACM
1379 ret = machine__process_kernel_mmap_event(machine, event);
1380 if (ret < 0)
1381 goto out_problem;
1382 return 0;
1383 }
1384
314add6b 1385 thread = machine__findnew_thread(machine, event->mmap.pid,
11c9abf2 1386 event->mmap.tid);
b0a7d1a0
ACM
1387 if (thread == NULL)
1388 goto out_problem;
bad40917
SE
1389
1390 if (event->header.misc & PERF_RECORD_MISC_MMAP_DATA)
1391 type = MAP__VARIABLE;
1392 else
1393 type = MAP__FUNCTION;
1394
2a03068c 1395 map = map__new(machine, event->mmap.start,
b0a7d1a0 1396 event->mmap.len, event->mmap.pgoff,
7ef80703 1397 event->mmap.pid, 0, 0, 0, 0, 0, 0,
5c5e854b 1398 event->mmap.filename,
5835edda 1399 type, thread);
bad40917 1400
b0a7d1a0 1401 if (map == NULL)
b91fc39f 1402 goto out_problem_map;
b0a7d1a0
ACM
1403
1404 thread__insert_map(thread, map);
b91fc39f 1405 thread__put(thread);
84c2cafa 1406 map__put(map);
b0a7d1a0
ACM
1407 return 0;
1408
b91fc39f
ACM
1409out_problem_map:
1410 thread__put(thread);
b0a7d1a0
ACM
1411out_problem:
1412 dump_printf("problem processing PERF_RECORD_MMAP, skipping event.\n");
1413 return 0;
1414}
1415
b91fc39f 1416static void __machine__remove_thread(struct machine *machine, struct thread *th, bool lock)
236a3bbd 1417{
f3b623b8 1418 if (machine->last_match == th)
0ceb8f6e 1419 machine->last_match = NULL;
f3b623b8 1420
59a51c1d 1421 BUG_ON(atomic_read(&th->refcnt) == 0);
b91fc39f
ACM
1422 if (lock)
1423 pthread_rwlock_wrlock(&machine->threads_lock);
0170b14f 1424 rb_erase_init(&th->rb_node, &machine->threads);
b91fc39f 1425 RB_CLEAR_NODE(&th->rb_node);
d2c11034 1426 --machine->nr_threads;
236a3bbd 1427 /*
f3b623b8
ACM
1428 * Move it first to the dead_threads list, then drop the reference,
1429 * if this is the last reference, then the thread__delete destructor
1430 * will be called and we will remove it from the dead_threads list.
236a3bbd
DA
1431 */
1432 list_add_tail(&th->node, &machine->dead_threads);
b91fc39f
ACM
1433 if (lock)
1434 pthread_rwlock_unlock(&machine->threads_lock);
f3b623b8 1435 thread__put(th);
236a3bbd
DA
1436}
1437
b91fc39f
ACM
1438void machine__remove_thread(struct machine *machine, struct thread *th)
1439{
1440 return __machine__remove_thread(machine, th, true);
1441}
1442
162f0bef
FW
1443int machine__process_fork_event(struct machine *machine, union perf_event *event,
1444 struct perf_sample *sample)
b0a7d1a0 1445{
d75e6097
JO
1446 struct thread *thread = machine__find_thread(machine,
1447 event->fork.pid,
1448 event->fork.tid);
314add6b
AH
1449 struct thread *parent = machine__findnew_thread(machine,
1450 event->fork.ppid,
1451 event->fork.ptid);
b91fc39f 1452 int err = 0;
b0a7d1a0 1453
5cb73340
AH
1454 if (dump_trace)
1455 perf_event__fprintf_task(event, stdout);
1456
1457 /*
1458 * There may be an existing thread that is not actually the parent,
1459 * either because we are processing events out of order, or because the
1460 * (fork) event that would have removed the thread was lost. Assume the
1461 * latter case and continue on as best we can.
1462 */
1463 if (parent->pid_ != (pid_t)event->fork.ppid) {
1464 dump_printf("removing erroneous parent thread %d/%d\n",
1465 parent->pid_, parent->tid);
1466 machine__remove_thread(machine, parent);
1467 thread__put(parent);
1468 parent = machine__findnew_thread(machine, event->fork.ppid,
1469 event->fork.ptid);
1470 }
1471
236a3bbd 1472 /* if a thread currently exists for the thread id remove it */
b91fc39f 1473 if (thread != NULL) {
236a3bbd 1474 machine__remove_thread(machine, thread);
b91fc39f
ACM
1475 thread__put(thread);
1476 }
236a3bbd 1477
314add6b
AH
1478 thread = machine__findnew_thread(machine, event->fork.pid,
1479 event->fork.tid);
b0a7d1a0
ACM
1480
1481 if (thread == NULL || parent == NULL ||
162f0bef 1482 thread__fork(thread, parent, sample->time) < 0) {
b0a7d1a0 1483 dump_printf("problem processing PERF_RECORD_FORK, skipping event.\n");
b91fc39f 1484 err = -1;
b0a7d1a0 1485 }
b91fc39f
ACM
1486 thread__put(thread);
1487 thread__put(parent);
b0a7d1a0 1488
b91fc39f 1489 return err;
b0a7d1a0
ACM
1490}
1491
162f0bef
FW
1492int machine__process_exit_event(struct machine *machine, union perf_event *event,
1493 struct perf_sample *sample __maybe_unused)
b0a7d1a0 1494{
d75e6097
JO
1495 struct thread *thread = machine__find_thread(machine,
1496 event->fork.pid,
1497 event->fork.tid);
b0a7d1a0
ACM
1498
1499 if (dump_trace)
1500 perf_event__fprintf_task(event, stdout);
1501
b91fc39f 1502 if (thread != NULL) {
236a3bbd 1503 thread__exited(thread);
b91fc39f
ACM
1504 thread__put(thread);
1505 }
b0a7d1a0
ACM
1506
1507 return 0;
1508}
1509
162f0bef
FW
1510int machine__process_event(struct machine *machine, union perf_event *event,
1511 struct perf_sample *sample)
b0a7d1a0
ACM
1512{
1513 int ret;
1514
1515 switch (event->header.type) {
1516 case PERF_RECORD_COMM:
162f0bef 1517 ret = machine__process_comm_event(machine, event, sample); break;
b0a7d1a0 1518 case PERF_RECORD_MMAP:
162f0bef 1519 ret = machine__process_mmap_event(machine, event, sample); break;
5c5e854b 1520 case PERF_RECORD_MMAP2:
162f0bef 1521 ret = machine__process_mmap2_event(machine, event, sample); break;
b0a7d1a0 1522 case PERF_RECORD_FORK:
162f0bef 1523 ret = machine__process_fork_event(machine, event, sample); break;
b0a7d1a0 1524 case PERF_RECORD_EXIT:
162f0bef 1525 ret = machine__process_exit_event(machine, event, sample); break;
b0a7d1a0 1526 case PERF_RECORD_LOST:
162f0bef 1527 ret = machine__process_lost_event(machine, event, sample); break;
4a96f7a0
AH
1528 case PERF_RECORD_AUX:
1529 ret = machine__process_aux_event(machine, event); break;
0ad21f68 1530 case PERF_RECORD_ITRACE_START:
ceb92913 1531 ret = machine__process_itrace_start_event(machine, event); break;
c4937a91
KL
1532 case PERF_RECORD_LOST_SAMPLES:
1533 ret = machine__process_lost_samples_event(machine, event, sample); break;
0286039f
AH
1534 case PERF_RECORD_SWITCH:
1535 case PERF_RECORD_SWITCH_CPU_WIDE:
1536 ret = machine__process_switch_event(machine, event); break;
b0a7d1a0
ACM
1537 default:
1538 ret = -1;
1539 break;
1540 }
1541
1542 return ret;
1543}
3f067dca 1544
b21484f1 1545static bool symbol__match_regex(struct symbol *sym, regex_t *regex)
3f067dca 1546{
b21484f1 1547 if (sym->name && !regexec(regex, sym->name, 0, NULL, 0))
3f067dca 1548 return 1;
3f067dca
ACM
1549 return 0;
1550}
1551
bb871a9c 1552static void ip__resolve_ams(struct thread *thread,
3f067dca
ACM
1553 struct addr_map_symbol *ams,
1554 u64 ip)
1555{
1556 struct addr_location al;
3f067dca
ACM
1557
1558 memset(&al, 0, sizeof(al));
52a3cb8c
ACM
1559 /*
1560 * We cannot use the header.misc hint to determine whether a
1561 * branch stack address is user, kernel, guest, hypervisor.
1562 * Branches may straddle the kernel/user/hypervisor boundaries.
1563 * Thus, we have to try consecutively until we find a match
1564 * or else, the symbol is unknown
1565 */
bb871a9c 1566 thread__find_cpumode_addr_location(thread, MAP__FUNCTION, ip, &al);
3f067dca 1567
3f067dca
ACM
1568 ams->addr = ip;
1569 ams->al_addr = al.addr;
1570 ams->sym = al.sym;
1571 ams->map = al.map;
1572}
1573
bb871a9c 1574static void ip__resolve_data(struct thread *thread,
98a3b32c
SE
1575 u8 m, struct addr_map_symbol *ams, u64 addr)
1576{
1577 struct addr_location al;
1578
1579 memset(&al, 0, sizeof(al));
1580
bb871a9c 1581 thread__find_addr_location(thread, m, MAP__VARIABLE, addr, &al);
06b2afc0
DZ
1582 if (al.map == NULL) {
1583 /*
1584 * some shared data regions have execute bit set which puts
1585 * their mapping in the MAP__FUNCTION type array.
1586 * Check there as a fallback option before dropping the sample.
1587 */
bb871a9c 1588 thread__find_addr_location(thread, m, MAP__FUNCTION, addr, &al);
06b2afc0
DZ
1589 }
1590
98a3b32c
SE
1591 ams->addr = addr;
1592 ams->al_addr = al.addr;
1593 ams->sym = al.sym;
1594 ams->map = al.map;
1595}
1596
e80faac0
ACM
1597struct mem_info *sample__resolve_mem(struct perf_sample *sample,
1598 struct addr_location *al)
98a3b32c
SE
1599{
1600 struct mem_info *mi = zalloc(sizeof(*mi));
1601
1602 if (!mi)
1603 return NULL;
1604
bb871a9c
ACM
1605 ip__resolve_ams(al->thread, &mi->iaddr, sample->ip);
1606 ip__resolve_data(al->thread, al->cpumode, &mi->daddr, sample->addr);
98a3b32c
SE
1607 mi->data_src.val = sample->data_src;
1608
1609 return mi;
1610}
1611
37592b8a 1612static int add_callchain_ip(struct thread *thread,
91d7b2de 1613 struct callchain_cursor *cursor,
37592b8a
AK
1614 struct symbol **parent,
1615 struct addr_location *root_al,
73dbcd65 1616 u8 *cpumode,
37592b8a
AK
1617 u64 ip)
1618{
1619 struct addr_location al;
1620
1621 al.filtered = 0;
1622 al.sym = NULL;
73dbcd65 1623 if (!cpumode) {
8b7bad58
AK
1624 thread__find_cpumode_addr_location(thread, MAP__FUNCTION,
1625 ip, &al);
73dbcd65 1626 } else {
2e77784b
KL
1627 if (ip >= PERF_CONTEXT_MAX) {
1628 switch (ip) {
1629 case PERF_CONTEXT_HV:
73dbcd65 1630 *cpumode = PERF_RECORD_MISC_HYPERVISOR;
2e77784b
KL
1631 break;
1632 case PERF_CONTEXT_KERNEL:
73dbcd65 1633 *cpumode = PERF_RECORD_MISC_KERNEL;
2e77784b
KL
1634 break;
1635 case PERF_CONTEXT_USER:
73dbcd65 1636 *cpumode = PERF_RECORD_MISC_USER;
2e77784b
KL
1637 break;
1638 default:
1639 pr_debug("invalid callchain context: "
1640 "%"PRId64"\n", (s64) ip);
1641 /*
1642 * It seems the callchain is corrupted.
1643 * Discard all.
1644 */
91d7b2de 1645 callchain_cursor_reset(cursor);
2e77784b
KL
1646 return 1;
1647 }
1648 return 0;
1649 }
73dbcd65
DH
1650 thread__find_addr_location(thread, *cpumode, MAP__FUNCTION,
1651 ip, &al);
2e77784b
KL
1652 }
1653
37592b8a 1654 if (al.sym != NULL) {
de7e6a7c 1655 if (perf_hpp_list.parent && !*parent &&
37592b8a
AK
1656 symbol__match_regex(al.sym, &parent_regex))
1657 *parent = al.sym;
1658 else if (have_ignore_callees && root_al &&
1659 symbol__match_regex(al.sym, &ignore_callees_regex)) {
1660 /* Treat this symbol as the root,
1661 forgetting its callees. */
1662 *root_al = al;
91d7b2de 1663 callchain_cursor_reset(cursor);
37592b8a
AK
1664 }
1665 }
1666
b49a8fe5
NK
1667 if (symbol_conf.hide_unresolved && al.sym == NULL)
1668 return 0;
91d7b2de 1669 return callchain_cursor_append(cursor, al.addr, al.map, al.sym);
37592b8a
AK
1670}
1671
644f2df2
ACM
1672struct branch_info *sample__resolve_bstack(struct perf_sample *sample,
1673 struct addr_location *al)
3f067dca 1674{
3f067dca 1675 unsigned int i;
644f2df2
ACM
1676 const struct branch_stack *bs = sample->branch_stack;
1677 struct branch_info *bi = calloc(bs->nr, sizeof(struct branch_info));
3f067dca 1678
3f067dca
ACM
1679 if (!bi)
1680 return NULL;
1681
1682 for (i = 0; i < bs->nr; i++) {
bb871a9c
ACM
1683 ip__resolve_ams(al->thread, &bi[i].to, bs->entries[i].to);
1684 ip__resolve_ams(al->thread, &bi[i].from, bs->entries[i].from);
3f067dca
ACM
1685 bi[i].flags = bs->entries[i].flags;
1686 }
1687 return bi;
1688}
1689
8b7bad58
AK
1690#define CHASHSZ 127
1691#define CHASHBITS 7
1692#define NO_ENTRY 0xff
1693
1694#define PERF_MAX_BRANCH_DEPTH 127
1695
1696/* Remove loops. */
1697static int remove_loops(struct branch_entry *l, int nr)
1698{
1699 int i, j, off;
1700 unsigned char chash[CHASHSZ];
1701
1702 memset(chash, NO_ENTRY, sizeof(chash));
1703
1704 BUG_ON(PERF_MAX_BRANCH_DEPTH > 255);
1705
1706 for (i = 0; i < nr; i++) {
1707 int h = hash_64(l[i].from, CHASHBITS) % CHASHSZ;
1708
1709 /* no collision handling for now */
1710 if (chash[h] == NO_ENTRY) {
1711 chash[h] = i;
1712 } else if (l[chash[h]].from == l[i].from) {
1713 bool is_loop = true;
1714 /* check if it is a real loop */
1715 off = 0;
1716 for (j = chash[h]; j < i && i + off < nr; j++, off++)
1717 if (l[j].from != l[i + off].from) {
1718 is_loop = false;
1719 break;
1720 }
1721 if (is_loop) {
1722 memmove(l + i, l + i + off,
1723 (nr - (i + off)) * sizeof(*l));
1724 nr -= off;
1725 }
1726 }
1727 }
1728 return nr;
1729}
1730
384b6055
KL
1731/*
1732 * Recolve LBR callstack chain sample
1733 * Return:
1734 * 1 on success get LBR callchain information
1735 * 0 no available LBR callchain information, should try fp
1736 * negative error code on other errors.
1737 */
1738static int resolve_lbr_callchain_sample(struct thread *thread,
91d7b2de 1739 struct callchain_cursor *cursor,
384b6055
KL
1740 struct perf_sample *sample,
1741 struct symbol **parent,
1742 struct addr_location *root_al,
1743 int max_stack)
3f067dca 1744{
384b6055
KL
1745 struct ip_callchain *chain = sample->callchain;
1746 int chain_nr = min(max_stack, (int)chain->nr);
73dbcd65 1747 u8 cpumode = PERF_RECORD_MISC_USER;
384b6055
KL
1748 int i, j, err;
1749 u64 ip;
1750
1751 for (i = 0; i < chain_nr; i++) {
1752 if (chain->ips[i] == PERF_CONTEXT_USER)
1753 break;
1754 }
1755
1756 /* LBR only affects the user callchain */
1757 if (i != chain_nr) {
1758 struct branch_stack *lbr_stack = sample->branch_stack;
1759 int lbr_nr = lbr_stack->nr;
1760 /*
1761 * LBR callstack can only get user call chain.
1762 * The mix_chain_nr is kernel call chain
1763 * number plus LBR user call chain number.
1764 * i is kernel call chain number,
1765 * 1 is PERF_CONTEXT_USER,
1766 * lbr_nr + 1 is the user call chain number.
1767 * For details, please refer to the comments
1768 * in callchain__printf
1769 */
1770 int mix_chain_nr = i + 1 + lbr_nr + 1;
1771
4cb93446 1772 if (mix_chain_nr > (int)sysctl_perf_event_max_stack + PERF_MAX_BRANCH_DEPTH) {
384b6055
KL
1773 pr_warning("corrupted callchain. skipping...\n");
1774 return 0;
1775 }
1776
1777 for (j = 0; j < mix_chain_nr; j++) {
1778 if (callchain_param.order == ORDER_CALLEE) {
1779 if (j < i + 1)
1780 ip = chain->ips[j];
1781 else if (j > i + 1)
1782 ip = lbr_stack->entries[j - i - 2].from;
1783 else
1784 ip = lbr_stack->entries[0].to;
1785 } else {
1786 if (j < lbr_nr)
1787 ip = lbr_stack->entries[lbr_nr - j - 1].from;
1788 else if (j > lbr_nr)
1789 ip = chain->ips[i + 1 - (j - lbr_nr)];
1790 else
1791 ip = lbr_stack->entries[0].to;
1792 }
1793
91d7b2de 1794 err = add_callchain_ip(thread, cursor, parent, root_al, &cpumode, ip);
384b6055
KL
1795 if (err)
1796 return (err < 0) ? err : 0;
1797 }
1798 return 1;
1799 }
1800
1801 return 0;
1802}
1803
1804static int thread__resolve_callchain_sample(struct thread *thread,
91d7b2de 1805 struct callchain_cursor *cursor,
384b6055
KL
1806 struct perf_evsel *evsel,
1807 struct perf_sample *sample,
1808 struct symbol **parent,
1809 struct addr_location *root_al,
1810 int max_stack)
1811{
1812 struct branch_stack *branch = sample->branch_stack;
1813 struct ip_callchain *chain = sample->callchain;
91e95617 1814 int chain_nr = min(max_stack, (int)chain->nr);
73dbcd65 1815 u8 cpumode = PERF_RECORD_MISC_USER;
2e77784b 1816 int i, j, err;
8b7bad58
AK
1817 int skip_idx = -1;
1818 int first_call = 0;
1819
acf2abbd 1820 if (perf_evsel__has_branch_callstack(evsel)) {
91d7b2de 1821 err = resolve_lbr_callchain_sample(thread, cursor, sample, parent,
384b6055
KL
1822 root_al, max_stack);
1823 if (err)
1824 return (err < 0) ? err : 0;
1825 }
1826
8b7bad58
AK
1827 /*
1828 * Based on DWARF debug information, some architectures skip
1829 * a callchain entry saved by the kernel.
1830 */
4cb93446 1831 if (chain->nr < sysctl_perf_event_max_stack)
8b7bad58 1832 skip_idx = arch_skip_callchain_idx(thread, chain);
3f067dca 1833
8b7bad58
AK
1834 /*
1835 * Add branches to call stack for easier browsing. This gives
1836 * more context for a sample than just the callers.
1837 *
1838 * This uses individual histograms of paths compared to the
1839 * aggregated histograms the normal LBR mode uses.
1840 *
1841 * Limitations for now:
1842 * - No extra filters
1843 * - No annotations (should annotate somehow)
1844 */
1845
1846 if (branch && callchain_param.branch_callstack) {
1847 int nr = min(max_stack, (int)branch->nr);
1848 struct branch_entry be[nr];
1849
1850 if (branch->nr > PERF_MAX_BRANCH_DEPTH) {
1851 pr_warning("corrupted branch chain. skipping...\n");
1852 goto check_calls;
1853 }
1854
1855 for (i = 0; i < nr; i++) {
1856 if (callchain_param.order == ORDER_CALLEE) {
1857 be[i] = branch->entries[i];
1858 /*
1859 * Check for overlap into the callchain.
1860 * The return address is one off compared to
1861 * the branch entry. To adjust for this
1862 * assume the calling instruction is not longer
1863 * than 8 bytes.
1864 */
1865 if (i == skip_idx ||
1866 chain->ips[first_call] >= PERF_CONTEXT_MAX)
1867 first_call++;
1868 else if (be[i].from < chain->ips[first_call] &&
1869 be[i].from >= chain->ips[first_call] - 8)
1870 first_call++;
1871 } else
1872 be[i] = branch->entries[branch->nr - i - 1];
1873 }
1874
1875 nr = remove_loops(be, nr);
1876
1877 for (i = 0; i < nr; i++) {
91d7b2de 1878 err = add_callchain_ip(thread, cursor, parent, root_al,
73dbcd65 1879 NULL, be[i].to);
8b7bad58 1880 if (!err)
91d7b2de 1881 err = add_callchain_ip(thread, cursor, parent, root_al,
73dbcd65 1882 NULL, be[i].from);
8b7bad58
AK
1883 if (err == -EINVAL)
1884 break;
1885 if (err)
1886 return err;
1887 }
1888 chain_nr -= nr;
1889 }
1890
1891check_calls:
4cb93446 1892 if (chain->nr > sysctl_perf_event_max_stack && (int)chain->nr > max_stack) {
3f067dca
ACM
1893 pr_warning("corrupted callchain. skipping...\n");
1894 return 0;
1895 }
1896
8b7bad58 1897 for (i = first_call; i < chain_nr; i++) {
3f067dca 1898 u64 ip;
3f067dca
ACM
1899
1900 if (callchain_param.order == ORDER_CALLEE)
a60335ba 1901 j = i;
3f067dca 1902 else
a60335ba
SB
1903 j = chain->nr - i - 1;
1904
1905#ifdef HAVE_SKIP_CALLCHAIN_IDX
1906 if (j == skip_idx)
1907 continue;
1908#endif
1909 ip = chain->ips[j];
3f067dca 1910
91d7b2de 1911 err = add_callchain_ip(thread, cursor, parent, root_al, &cpumode, ip);
3f067dca 1912
3f067dca 1913 if (err)
2e77784b 1914 return (err < 0) ? err : 0;
3f067dca
ACM
1915 }
1916
1917 return 0;
1918}
1919
1920static int unwind_entry(struct unwind_entry *entry, void *arg)
1921{
1922 struct callchain_cursor *cursor = arg;
b49a8fe5
NK
1923
1924 if (symbol_conf.hide_unresolved && entry->sym == NULL)
1925 return 0;
3f067dca
ACM
1926 return callchain_cursor_append(cursor, entry->ip,
1927 entry->map, entry->sym);
1928}
1929
9919a65e
CP
1930static int thread__resolve_callchain_unwind(struct thread *thread,
1931 struct callchain_cursor *cursor,
1932 struct perf_evsel *evsel,
1933 struct perf_sample *sample,
1934 int max_stack)
3f067dca 1935{
3f067dca
ACM
1936 /* Can we do dwarf post unwind? */
1937 if (!((evsel->attr.sample_type & PERF_SAMPLE_REGS_USER) &&
1938 (evsel->attr.sample_type & PERF_SAMPLE_STACK_USER)))
1939 return 0;
1940
1941 /* Bail out if nothing was captured. */
1942 if ((!sample->user_regs.regs) ||
1943 (!sample->user_stack.size))
1944 return 0;
1945
91d7b2de 1946 return unwind__get_entries(unwind_entry, cursor,
352ea45a 1947 thread, sample, max_stack);
9919a65e 1948}
3f067dca 1949
9919a65e
CP
1950int thread__resolve_callchain(struct thread *thread,
1951 struct callchain_cursor *cursor,
1952 struct perf_evsel *evsel,
1953 struct perf_sample *sample,
1954 struct symbol **parent,
1955 struct addr_location *root_al,
1956 int max_stack)
1957{
1958 int ret = 0;
1959
1960 callchain_cursor_reset(&callchain_cursor);
1961
1962 if (callchain_param.order == ORDER_CALLEE) {
1963 ret = thread__resolve_callchain_sample(thread, cursor,
1964 evsel, sample,
1965 parent, root_al,
1966 max_stack);
1967 if (ret)
1968 return ret;
1969 ret = thread__resolve_callchain_unwind(thread, cursor,
1970 evsel, sample,
1971 max_stack);
1972 } else {
1973 ret = thread__resolve_callchain_unwind(thread, cursor,
1974 evsel, sample,
1975 max_stack);
1976 if (ret)
1977 return ret;
1978 ret = thread__resolve_callchain_sample(thread, cursor,
1979 evsel, sample,
1980 parent, root_al,
1981 max_stack);
1982 }
1983
1984 return ret;
3f067dca 1985}
35feee19
DA
1986
1987int machine__for_each_thread(struct machine *machine,
1988 int (*fn)(struct thread *thread, void *p),
1989 void *priv)
1990{
1991 struct rb_node *nd;
1992 struct thread *thread;
1993 int rc = 0;
1994
1995 for (nd = rb_first(&machine->threads); nd; nd = rb_next(nd)) {
1996 thread = rb_entry(nd, struct thread, rb_node);
1997 rc = fn(thread, priv);
1998 if (rc != 0)
1999 return rc;
2000 }
2001
2002 list_for_each_entry(thread, &machine->dead_threads, node) {
2003 rc = fn(thread, priv);
2004 if (rc != 0)
2005 return rc;
2006 }
2007 return rc;
2008}
58d925dc 2009
a5499b37
AH
2010int machines__for_each_thread(struct machines *machines,
2011 int (*fn)(struct thread *thread, void *p),
2012 void *priv)
2013{
2014 struct rb_node *nd;
2015 int rc = 0;
2016
2017 rc = machine__for_each_thread(&machines->host, fn, priv);
2018 if (rc != 0)
2019 return rc;
2020
2021 for (nd = rb_first(&machines->guests); nd; nd = rb_next(nd)) {
2022 struct machine *machine = rb_entry(nd, struct machine, rb_node);
2023
2024 rc = machine__for_each_thread(machine, fn, priv);
2025 if (rc != 0)
2026 return rc;
2027 }
2028 return rc;
2029}
2030
a33fbd56 2031int __machine__synthesize_threads(struct machine *machine, struct perf_tool *tool,
602ad878 2032 struct target *target, struct thread_map *threads,
9d9cad76
KL
2033 perf_event__handler_t process, bool data_mmap,
2034 unsigned int proc_map_timeout)
58d925dc 2035{
602ad878 2036 if (target__has_task(target))
9d9cad76 2037 return perf_event__synthesize_thread_map(tool, threads, process, machine, data_mmap, proc_map_timeout);
602ad878 2038 else if (target__has_cpu(target))
9d9cad76 2039 return perf_event__synthesize_threads(tool, process, machine, data_mmap, proc_map_timeout);
58d925dc
ACM
2040 /* command specified */
2041 return 0;
2042}
b9d266ba
AH
2043
2044pid_t machine__get_current_tid(struct machine *machine, int cpu)
2045{
2046 if (cpu < 0 || cpu >= MAX_NR_CPUS || !machine->current_tid)
2047 return -1;
2048
2049 return machine->current_tid[cpu];
2050}
2051
2052int machine__set_current_tid(struct machine *machine, int cpu, pid_t pid,
2053 pid_t tid)
2054{
2055 struct thread *thread;
2056
2057 if (cpu < 0)
2058 return -EINVAL;
2059
2060 if (!machine->current_tid) {
2061 int i;
2062
2063 machine->current_tid = calloc(MAX_NR_CPUS, sizeof(pid_t));
2064 if (!machine->current_tid)
2065 return -ENOMEM;
2066 for (i = 0; i < MAX_NR_CPUS; i++)
2067 machine->current_tid[i] = -1;
2068 }
2069
2070 if (cpu >= MAX_NR_CPUS) {
2071 pr_err("Requested CPU %d too large. ", cpu);
2072 pr_err("Consider raising MAX_NR_CPUS\n");
2073 return -EINVAL;
2074 }
2075
2076 machine->current_tid[cpu] = tid;
2077
2078 thread = machine__findnew_thread(machine, pid, tid);
2079 if (!thread)
2080 return -ENOMEM;
2081
2082 thread->cpu = cpu;
b91fc39f 2083 thread__put(thread);
b9d266ba
AH
2084
2085 return 0;
2086}
fbe2af45
AH
2087
2088int machine__get_kernel_start(struct machine *machine)
2089{
a5e813c6 2090 struct map *map = machine__kernel_map(machine);
fbe2af45
AH
2091 int err = 0;
2092
2093 /*
2094 * The only addresses above 2^63 are kernel addresses of a 64-bit
2095 * kernel. Note that addresses are unsigned so that on a 32-bit system
2096 * all addresses including kernel addresses are less than 2^32. In
2097 * that case (32-bit system), if the kernel mapping is unknown, all
2098 * addresses will be assumed to be in user space - see
2099 * machine__kernel_ip().
2100 */
2101 machine->kernel_start = 1ULL << 63;
2102 if (map) {
2103 err = map__load(map, machine->symbol_filter);
2104 if (map->start)
2105 machine->kernel_start = map->start;
2106 }
2107 return err;
2108}
aa7cc2ae
ACM
2109
2110struct dso *machine__findnew_dso(struct machine *machine, const char *filename)
2111{
e8807844 2112 return dsos__findnew(&machine->dsos, filename);
aa7cc2ae 2113}
c3168b0d
ACM
2114
2115char *machine__resolve_kernel_addr(void *vmachine, unsigned long long *addrp, char **modp)
2116{
2117 struct machine *machine = vmachine;
2118 struct map *map;
2119 struct symbol *sym = map_groups__find_symbol(&machine->kmaps, MAP__FUNCTION, *addrp, &map, NULL);
2120
2121 if (sym == NULL)
2122 return NULL;
2123
2124 *modp = __map__is_kmodule(map) ? (char *)map->dso->short_name : NULL;
2125 *addrp = map->unmap_ip(map, sym->start);
2126 return sym->name;
2127}
This page took 0.251151 seconds and 5 git commands to generate.