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