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