perf machine: Workaround missing maps for x86 PTI entry trampolines
[GitHub/moto-9609/android_kernel_motorola_exynos9610.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 /* Kernel-space maps for symbols that are outside the main kernel map and module maps */
822 struct extra_kernel_map {
823 u64 start;
824 u64 end;
825 u64 pgoff;
826 };
827
828 static int machine__create_extra_kernel_map(struct machine *machine,
829 struct dso *kernel,
830 struct extra_kernel_map *xm)
831 {
832 struct kmap *kmap;
833 struct map *map;
834
835 map = map__new2(xm->start, kernel, MAP__FUNCTION);
836 if (!map)
837 return -1;
838
839 map->end = xm->end;
840 map->pgoff = xm->pgoff;
841
842 kmap = map__kmap(map);
843
844 kmap->kmaps = &machine->kmaps;
845
846 map_groups__insert(&machine->kmaps, map);
847
848 pr_debug2("Added extra kernel map %" PRIx64 "-%" PRIx64 "\n",
849 map->start, map->end);
850
851 map__put(map);
852
853 return 0;
854 }
855
856 static u64 find_entry_trampoline(struct dso *dso)
857 {
858 /* Duplicates are removed so lookup all aliases */
859 const char *syms[] = {
860 "_entry_trampoline",
861 "__entry_trampoline_start",
862 "entry_SYSCALL_64_trampoline",
863 };
864 struct symbol *sym = dso__first_symbol(dso, MAP__FUNCTION);
865 unsigned int i;
866
867 for (; sym; sym = dso__next_symbol(sym)) {
868 if (sym->binding != STB_GLOBAL)
869 continue;
870 for (i = 0; i < ARRAY_SIZE(syms); i++) {
871 if (!strcmp(sym->name, syms[i]))
872 return sym->start;
873 }
874 }
875
876 return 0;
877 }
878
879 /*
880 * These values can be used for kernels that do not have symbols for the entry
881 * trampolines in kallsyms.
882 */
883 #define X86_64_CPU_ENTRY_AREA_PER_CPU 0xfffffe0000000000ULL
884 #define X86_64_CPU_ENTRY_AREA_SIZE 0x2c000
885 #define X86_64_ENTRY_TRAMPOLINE 0x6000
886
887 /* Map x86_64 PTI entry trampolines */
888 int machine__map_x86_64_entry_trampolines(struct machine *machine,
889 struct dso *kernel)
890 {
891 u64 pgoff = find_entry_trampoline(kernel);
892 int nr_cpus_avail, cpu;
893
894 if (!pgoff)
895 return 0;
896
897 nr_cpus_avail = machine__nr_cpus_avail(machine);
898
899 /* Add a 1 page map for each CPU's entry trampoline */
900 for (cpu = 0; cpu < nr_cpus_avail; cpu++) {
901 u64 va = X86_64_CPU_ENTRY_AREA_PER_CPU +
902 cpu * X86_64_CPU_ENTRY_AREA_SIZE +
903 X86_64_ENTRY_TRAMPOLINE;
904 struct extra_kernel_map xm = {
905 .start = va,
906 .end = va + page_size,
907 .pgoff = pgoff,
908 };
909
910 if (machine__create_extra_kernel_map(machine, kernel, &xm) < 0)
911 return -1;
912 }
913
914 return 0;
915 }
916
917 int __machine__create_kernel_maps(struct machine *machine, struct dso *kernel)
918 {
919 int type;
920 u64 start = 0;
921
922 if (machine__get_running_kernel_start(machine, NULL, &start))
923 return -1;
924
925 /* In case of renewal the kernel map, destroy previous one */
926 machine__destroy_kernel_maps(machine);
927
928 for (type = 0; type < MAP__NR_TYPES; ++type) {
929 struct kmap *kmap;
930 struct map *map;
931
932 machine->vmlinux_maps[type] = map__new2(start, kernel, type);
933 if (machine->vmlinux_maps[type] == NULL)
934 return -1;
935
936 machine->vmlinux_maps[type]->map_ip =
937 machine->vmlinux_maps[type]->unmap_ip =
938 identity__map_ip;
939 map = __machine__kernel_map(machine, type);
940 kmap = map__kmap(map);
941 if (!kmap)
942 return -1;
943
944 kmap->kmaps = &machine->kmaps;
945 map_groups__insert(&machine->kmaps, map);
946 }
947
948 return 0;
949 }
950
951 void machine__destroy_kernel_maps(struct machine *machine)
952 {
953 int type;
954
955 for (type = 0; type < MAP__NR_TYPES; ++type) {
956 struct kmap *kmap;
957 struct map *map = __machine__kernel_map(machine, type);
958
959 if (map == NULL)
960 continue;
961
962 kmap = map__kmap(map);
963 map_groups__remove(&machine->kmaps, map);
964 if (kmap && kmap->ref_reloc_sym) {
965 /*
966 * ref_reloc_sym is shared among all maps, so free just
967 * on one of them.
968 */
969 if (type == MAP__FUNCTION) {
970 zfree((char **)&kmap->ref_reloc_sym->name);
971 zfree(&kmap->ref_reloc_sym);
972 } else
973 kmap->ref_reloc_sym = NULL;
974 }
975
976 map__put(machine->vmlinux_maps[type]);
977 machine->vmlinux_maps[type] = NULL;
978 }
979 }
980
981 int machines__create_guest_kernel_maps(struct machines *machines)
982 {
983 int ret = 0;
984 struct dirent **namelist = NULL;
985 int i, items = 0;
986 char path[PATH_MAX];
987 pid_t pid;
988 char *endp;
989
990 if (symbol_conf.default_guest_vmlinux_name ||
991 symbol_conf.default_guest_modules ||
992 symbol_conf.default_guest_kallsyms) {
993 machines__create_kernel_maps(machines, DEFAULT_GUEST_KERNEL_ID);
994 }
995
996 if (symbol_conf.guestmount) {
997 items = scandir(symbol_conf.guestmount, &namelist, NULL, NULL);
998 if (items <= 0)
999 return -ENOENT;
1000 for (i = 0; i < items; i++) {
1001 if (!isdigit(namelist[i]->d_name[0])) {
1002 /* Filter out . and .. */
1003 continue;
1004 }
1005 pid = (pid_t)strtol(namelist[i]->d_name, &endp, 10);
1006 if ((*endp != '\0') ||
1007 (endp == namelist[i]->d_name) ||
1008 (errno == ERANGE)) {
1009 pr_debug("invalid directory (%s). Skipping.\n",
1010 namelist[i]->d_name);
1011 continue;
1012 }
1013 sprintf(path, "%s/%s/proc/kallsyms",
1014 symbol_conf.guestmount,
1015 namelist[i]->d_name);
1016 ret = access(path, R_OK);
1017 if (ret) {
1018 pr_debug("Can't access file %s\n", path);
1019 goto failure;
1020 }
1021 machines__create_kernel_maps(machines, pid);
1022 }
1023 failure:
1024 free(namelist);
1025 }
1026
1027 return ret;
1028 }
1029
1030 void machines__destroy_kernel_maps(struct machines *machines)
1031 {
1032 struct rb_node *next = rb_first(&machines->guests);
1033
1034 machine__destroy_kernel_maps(&machines->host);
1035
1036 while (next) {
1037 struct machine *pos = rb_entry(next, struct machine, rb_node);
1038
1039 next = rb_next(&pos->rb_node);
1040 rb_erase(&pos->rb_node, &machines->guests);
1041 machine__delete(pos);
1042 }
1043 }
1044
1045 int machines__create_kernel_maps(struct machines *machines, pid_t pid)
1046 {
1047 struct machine *machine = machines__findnew(machines, pid);
1048
1049 if (machine == NULL)
1050 return -1;
1051
1052 return machine__create_kernel_maps(machine);
1053 }
1054
1055 int __machine__load_kallsyms(struct machine *machine, const char *filename,
1056 enum map_type type, bool no_kcore)
1057 {
1058 struct map *map = machine__kernel_map(machine);
1059 int ret = __dso__load_kallsyms(map->dso, filename, map, no_kcore);
1060
1061 if (ret > 0) {
1062 dso__set_loaded(map->dso, type);
1063 /*
1064 * Since /proc/kallsyms will have multiple sessions for the
1065 * kernel, with modules between them, fixup the end of all
1066 * sections.
1067 */
1068 __map_groups__fixup_end(&machine->kmaps, type);
1069 }
1070
1071 return ret;
1072 }
1073
1074 int machine__load_kallsyms(struct machine *machine, const char *filename,
1075 enum map_type type)
1076 {
1077 return __machine__load_kallsyms(machine, filename, type, false);
1078 }
1079
1080 int machine__load_vmlinux_path(struct machine *machine, enum map_type type)
1081 {
1082 struct map *map = machine__kernel_map(machine);
1083 int ret = dso__load_vmlinux_path(map->dso, map);
1084
1085 if (ret > 0)
1086 dso__set_loaded(map->dso, type);
1087
1088 return ret;
1089 }
1090
1091 static void map_groups__fixup_end(struct map_groups *mg)
1092 {
1093 int i;
1094 for (i = 0; i < MAP__NR_TYPES; ++i)
1095 __map_groups__fixup_end(mg, i);
1096 }
1097
1098 static char *get_kernel_version(const char *root_dir)
1099 {
1100 char version[PATH_MAX];
1101 FILE *file;
1102 char *name, *tmp;
1103 const char *prefix = "Linux version ";
1104
1105 sprintf(version, "%s/proc/version", root_dir);
1106 file = fopen(version, "r");
1107 if (!file)
1108 return NULL;
1109
1110 version[0] = '\0';
1111 tmp = fgets(version, sizeof(version), file);
1112 fclose(file);
1113
1114 name = strstr(version, prefix);
1115 if (!name)
1116 return NULL;
1117 name += strlen(prefix);
1118 tmp = strchr(name, ' ');
1119 if (tmp)
1120 *tmp = '\0';
1121
1122 return strdup(name);
1123 }
1124
1125 static bool is_kmod_dso(struct dso *dso)
1126 {
1127 return dso->symtab_type == DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE ||
1128 dso->symtab_type == DSO_BINARY_TYPE__GUEST_KMODULE;
1129 }
1130
1131 static int map_groups__set_module_path(struct map_groups *mg, const char *path,
1132 struct kmod_path *m)
1133 {
1134 struct map *map;
1135 char *long_name;
1136
1137 map = map_groups__find_by_name(mg, MAP__FUNCTION, m->name);
1138 if (map == NULL)
1139 return 0;
1140
1141 long_name = strdup(path);
1142 if (long_name == NULL)
1143 return -ENOMEM;
1144
1145 dso__set_long_name(map->dso, long_name, true);
1146 dso__kernel_module_get_build_id(map->dso, "");
1147
1148 /*
1149 * Full name could reveal us kmod compression, so
1150 * we need to update the symtab_type if needed.
1151 */
1152 if (m->comp && is_kmod_dso(map->dso))
1153 map->dso->symtab_type++;
1154
1155 return 0;
1156 }
1157
1158 static int map_groups__set_modules_path_dir(struct map_groups *mg,
1159 const char *dir_name, int depth)
1160 {
1161 struct dirent *dent;
1162 DIR *dir = opendir(dir_name);
1163 int ret = 0;
1164
1165 if (!dir) {
1166 pr_debug("%s: cannot open %s dir\n", __func__, dir_name);
1167 return -1;
1168 }
1169
1170 while ((dent = readdir(dir)) != NULL) {
1171 char path[PATH_MAX];
1172 struct stat st;
1173
1174 /*sshfs might return bad dent->d_type, so we have to stat*/
1175 snprintf(path, sizeof(path), "%s/%s", dir_name, dent->d_name);
1176 if (stat(path, &st))
1177 continue;
1178
1179 if (S_ISDIR(st.st_mode)) {
1180 if (!strcmp(dent->d_name, ".") ||
1181 !strcmp(dent->d_name, ".."))
1182 continue;
1183
1184 /* Do not follow top-level source and build symlinks */
1185 if (depth == 0) {
1186 if (!strcmp(dent->d_name, "source") ||
1187 !strcmp(dent->d_name, "build"))
1188 continue;
1189 }
1190
1191 ret = map_groups__set_modules_path_dir(mg, path,
1192 depth + 1);
1193 if (ret < 0)
1194 goto out;
1195 } else {
1196 struct kmod_path m;
1197
1198 ret = kmod_path__parse_name(&m, dent->d_name);
1199 if (ret)
1200 goto out;
1201
1202 if (m.kmod)
1203 ret = map_groups__set_module_path(mg, path, &m);
1204
1205 free(m.name);
1206
1207 if (ret)
1208 goto out;
1209 }
1210 }
1211
1212 out:
1213 closedir(dir);
1214 return ret;
1215 }
1216
1217 static int machine__set_modules_path(struct machine *machine)
1218 {
1219 char *version;
1220 char modules_path[PATH_MAX];
1221
1222 version = get_kernel_version(machine->root_dir);
1223 if (!version)
1224 return -1;
1225
1226 snprintf(modules_path, sizeof(modules_path), "%s/lib/modules/%s",
1227 machine->root_dir, version);
1228 free(version);
1229
1230 return map_groups__set_modules_path_dir(&machine->kmaps, modules_path, 0);
1231 }
1232 int __weak arch__fix_module_text_start(u64 *start __maybe_unused,
1233 const char *name __maybe_unused)
1234 {
1235 return 0;
1236 }
1237
1238 static int machine__create_module(void *arg, const char *name, u64 start,
1239 u64 size)
1240 {
1241 struct machine *machine = arg;
1242 struct map *map;
1243
1244 if (arch__fix_module_text_start(&start, name) < 0)
1245 return -1;
1246
1247 map = machine__findnew_module_map(machine, start, name);
1248 if (map == NULL)
1249 return -1;
1250 map->end = start + size;
1251
1252 dso__kernel_module_get_build_id(map->dso, machine->root_dir);
1253
1254 return 0;
1255 }
1256
1257 static int machine__create_modules(struct machine *machine)
1258 {
1259 const char *modules;
1260 char path[PATH_MAX];
1261
1262 if (machine__is_default_guest(machine)) {
1263 modules = symbol_conf.default_guest_modules;
1264 } else {
1265 snprintf(path, PATH_MAX, "%s/proc/modules", machine->root_dir);
1266 modules = path;
1267 }
1268
1269 if (symbol__restricted_filename(modules, "/proc/modules"))
1270 return -1;
1271
1272 if (modules__parse(modules, machine, machine__create_module))
1273 return -1;
1274
1275 if (!machine__set_modules_path(machine))
1276 return 0;
1277
1278 pr_debug("Problems setting modules path maps, continuing anyway...\n");
1279
1280 return 0;
1281 }
1282
1283 int machine__create_kernel_maps(struct machine *machine)
1284 {
1285 struct dso *kernel = machine__get_kernel(machine);
1286 const char *name = NULL;
1287 u64 addr = 0;
1288 int ret;
1289
1290 if (kernel == NULL)
1291 return -1;
1292
1293 ret = __machine__create_kernel_maps(machine, kernel);
1294 dso__put(kernel);
1295 if (ret < 0)
1296 return -1;
1297
1298 if (symbol_conf.use_modules && machine__create_modules(machine) < 0) {
1299 if (machine__is_host(machine))
1300 pr_debug("Problems creating module maps, "
1301 "continuing anyway...\n");
1302 else
1303 pr_debug("Problems creating module maps for guest %d, "
1304 "continuing anyway...\n", machine->pid);
1305 }
1306
1307 /*
1308 * Now that we have all the maps created, just set the ->end of them:
1309 */
1310 map_groups__fixup_end(&machine->kmaps);
1311
1312 if (!machine__get_running_kernel_start(machine, &name, &addr)) {
1313 if (name &&
1314 maps__set_kallsyms_ref_reloc_sym(machine->vmlinux_maps, name, addr)) {
1315 machine__destroy_kernel_maps(machine);
1316 return -1;
1317 }
1318 }
1319
1320 return 0;
1321 }
1322
1323 static void machine__set_kernel_mmap_len(struct machine *machine,
1324 union perf_event *event)
1325 {
1326 int i;
1327
1328 for (i = 0; i < MAP__NR_TYPES; i++) {
1329 machine->vmlinux_maps[i]->start = event->mmap.start;
1330 machine->vmlinux_maps[i]->end = (event->mmap.start +
1331 event->mmap.len);
1332 /*
1333 * Be a bit paranoid here, some perf.data file came with
1334 * a zero sized synthesized MMAP event for the kernel.
1335 */
1336 if (machine->vmlinux_maps[i]->end == 0)
1337 machine->vmlinux_maps[i]->end = ~0ULL;
1338 }
1339 }
1340
1341 static bool machine__uses_kcore(struct machine *machine)
1342 {
1343 struct dso *dso;
1344
1345 list_for_each_entry(dso, &machine->dsos.head, node) {
1346 if (dso__is_kcore(dso))
1347 return true;
1348 }
1349
1350 return false;
1351 }
1352
1353 static int machine__process_kernel_mmap_event(struct machine *machine,
1354 union perf_event *event)
1355 {
1356 struct map *map;
1357 char kmmap_prefix[PATH_MAX];
1358 enum dso_kernel_type kernel_type;
1359 bool is_kernel_mmap;
1360
1361 /* If we have maps from kcore then we do not need or want any others */
1362 if (machine__uses_kcore(machine))
1363 return 0;
1364
1365 machine__mmap_name(machine, kmmap_prefix, sizeof(kmmap_prefix));
1366 if (machine__is_host(machine))
1367 kernel_type = DSO_TYPE_KERNEL;
1368 else
1369 kernel_type = DSO_TYPE_GUEST_KERNEL;
1370
1371 is_kernel_mmap = memcmp(event->mmap.filename,
1372 kmmap_prefix,
1373 strlen(kmmap_prefix) - 1) == 0;
1374 if (event->mmap.filename[0] == '/' ||
1375 (!is_kernel_mmap && event->mmap.filename[0] == '[')) {
1376 map = machine__findnew_module_map(machine, event->mmap.start,
1377 event->mmap.filename);
1378 if (map == NULL)
1379 goto out_problem;
1380
1381 map->end = map->start + event->mmap.len;
1382 } else if (is_kernel_mmap) {
1383 const char *symbol_name = (event->mmap.filename +
1384 strlen(kmmap_prefix));
1385 /*
1386 * Should be there already, from the build-id table in
1387 * the header.
1388 */
1389 struct dso *kernel = NULL;
1390 struct dso *dso;
1391
1392 pthread_rwlock_rdlock(&machine->dsos.lock);
1393
1394 list_for_each_entry(dso, &machine->dsos.head, node) {
1395
1396 /*
1397 * The cpumode passed to is_kernel_module is not the
1398 * cpumode of *this* event. If we insist on passing
1399 * correct cpumode to is_kernel_module, we should
1400 * record the cpumode when we adding this dso to the
1401 * linked list.
1402 *
1403 * However we don't really need passing correct
1404 * cpumode. We know the correct cpumode must be kernel
1405 * mode (if not, we should not link it onto kernel_dsos
1406 * list).
1407 *
1408 * Therefore, we pass PERF_RECORD_MISC_CPUMODE_UNKNOWN.
1409 * is_kernel_module() treats it as a kernel cpumode.
1410 */
1411
1412 if (!dso->kernel ||
1413 is_kernel_module(dso->long_name,
1414 PERF_RECORD_MISC_CPUMODE_UNKNOWN))
1415 continue;
1416
1417
1418 kernel = dso;
1419 break;
1420 }
1421
1422 pthread_rwlock_unlock(&machine->dsos.lock);
1423
1424 if (kernel == NULL)
1425 kernel = machine__findnew_dso(machine, kmmap_prefix);
1426 if (kernel == NULL)
1427 goto out_problem;
1428
1429 kernel->kernel = kernel_type;
1430 if (__machine__create_kernel_maps(machine, kernel) < 0) {
1431 dso__put(kernel);
1432 goto out_problem;
1433 }
1434
1435 if (strstr(kernel->long_name, "vmlinux"))
1436 dso__set_short_name(kernel, "[kernel.vmlinux]", false);
1437
1438 machine__set_kernel_mmap_len(machine, event);
1439
1440 /*
1441 * Avoid using a zero address (kptr_restrict) for the ref reloc
1442 * symbol. Effectively having zero here means that at record
1443 * time /proc/sys/kernel/kptr_restrict was non zero.
1444 */
1445 if (event->mmap.pgoff != 0) {
1446 maps__set_kallsyms_ref_reloc_sym(machine->vmlinux_maps,
1447 symbol_name,
1448 event->mmap.pgoff);
1449 }
1450
1451 if (machine__is_default_guest(machine)) {
1452 /*
1453 * preload dso of guest kernel and modules
1454 */
1455 dso__load(kernel, machine__kernel_map(machine));
1456 }
1457 }
1458 return 0;
1459 out_problem:
1460 return -1;
1461 }
1462
1463 int machine__process_mmap2_event(struct machine *machine,
1464 union perf_event *event,
1465 struct perf_sample *sample)
1466 {
1467 struct thread *thread;
1468 struct map *map;
1469 enum map_type type;
1470 int ret = 0;
1471
1472 if (dump_trace)
1473 perf_event__fprintf_mmap2(event, stdout);
1474
1475 if (sample->cpumode == PERF_RECORD_MISC_GUEST_KERNEL ||
1476 sample->cpumode == PERF_RECORD_MISC_KERNEL) {
1477 ret = machine__process_kernel_mmap_event(machine, event);
1478 if (ret < 0)
1479 goto out_problem;
1480 return 0;
1481 }
1482
1483 thread = machine__findnew_thread(machine, event->mmap2.pid,
1484 event->mmap2.tid);
1485 if (thread == NULL)
1486 goto out_problem;
1487
1488 if (event->header.misc & PERF_RECORD_MISC_MMAP_DATA)
1489 type = MAP__VARIABLE;
1490 else
1491 type = MAP__FUNCTION;
1492
1493 map = map__new(machine, event->mmap2.start,
1494 event->mmap2.len, event->mmap2.pgoff,
1495 event->mmap2.maj,
1496 event->mmap2.min, event->mmap2.ino,
1497 event->mmap2.ino_generation,
1498 event->mmap2.prot,
1499 event->mmap2.flags,
1500 event->mmap2.filename, type, thread);
1501
1502 if (map == NULL)
1503 goto out_problem_map;
1504
1505 ret = thread__insert_map(thread, map);
1506 if (ret)
1507 goto out_problem_insert;
1508
1509 thread__put(thread);
1510 map__put(map);
1511 return 0;
1512
1513 out_problem_insert:
1514 map__put(map);
1515 out_problem_map:
1516 thread__put(thread);
1517 out_problem:
1518 dump_printf("problem processing PERF_RECORD_MMAP2, skipping event.\n");
1519 return 0;
1520 }
1521
1522 int machine__process_mmap_event(struct machine *machine, union perf_event *event,
1523 struct perf_sample *sample)
1524 {
1525 struct thread *thread;
1526 struct map *map;
1527 enum map_type type;
1528 int ret = 0;
1529
1530 if (dump_trace)
1531 perf_event__fprintf_mmap(event, stdout);
1532
1533 if (sample->cpumode == PERF_RECORD_MISC_GUEST_KERNEL ||
1534 sample->cpumode == PERF_RECORD_MISC_KERNEL) {
1535 ret = machine__process_kernel_mmap_event(machine, event);
1536 if (ret < 0)
1537 goto out_problem;
1538 return 0;
1539 }
1540
1541 thread = machine__findnew_thread(machine, event->mmap.pid,
1542 event->mmap.tid);
1543 if (thread == NULL)
1544 goto out_problem;
1545
1546 if (event->header.misc & PERF_RECORD_MISC_MMAP_DATA)
1547 type = MAP__VARIABLE;
1548 else
1549 type = MAP__FUNCTION;
1550
1551 map = map__new(machine, event->mmap.start,
1552 event->mmap.len, event->mmap.pgoff,
1553 0, 0, 0, 0, 0, 0,
1554 event->mmap.filename,
1555 type, thread);
1556
1557 if (map == NULL)
1558 goto out_problem_map;
1559
1560 ret = thread__insert_map(thread, map);
1561 if (ret)
1562 goto out_problem_insert;
1563
1564 thread__put(thread);
1565 map__put(map);
1566 return 0;
1567
1568 out_problem_insert:
1569 map__put(map);
1570 out_problem_map:
1571 thread__put(thread);
1572 out_problem:
1573 dump_printf("problem processing PERF_RECORD_MMAP, skipping event.\n");
1574 return 0;
1575 }
1576
1577 static void __machine__remove_thread(struct machine *machine, struct thread *th, bool lock)
1578 {
1579 if (machine->last_match == th)
1580 machine->last_match = NULL;
1581
1582 BUG_ON(refcount_read(&th->refcnt) == 0);
1583 if (lock)
1584 pthread_rwlock_wrlock(&machine->threads_lock);
1585 rb_erase_init(&th->rb_node, &machine->threads);
1586 RB_CLEAR_NODE(&th->rb_node);
1587 --machine->nr_threads;
1588 /*
1589 * Move it first to the dead_threads list, then drop the reference,
1590 * if this is the last reference, then the thread__delete destructor
1591 * will be called and we will remove it from the dead_threads list.
1592 */
1593 list_add_tail(&th->node, &machine->dead_threads);
1594 if (lock)
1595 pthread_rwlock_unlock(&machine->threads_lock);
1596 thread__put(th);
1597 }
1598
1599 void machine__remove_thread(struct machine *machine, struct thread *th)
1600 {
1601 return __machine__remove_thread(machine, th, true);
1602 }
1603
1604 int machine__process_fork_event(struct machine *machine, union perf_event *event,
1605 struct perf_sample *sample)
1606 {
1607 struct thread *thread = machine__find_thread(machine,
1608 event->fork.pid,
1609 event->fork.tid);
1610 struct thread *parent = machine__findnew_thread(machine,
1611 event->fork.ppid,
1612 event->fork.ptid);
1613 int err = 0;
1614
1615 if (dump_trace)
1616 perf_event__fprintf_task(event, stdout);
1617
1618 /*
1619 * There may be an existing thread that is not actually the parent,
1620 * either because we are processing events out of order, or because the
1621 * (fork) event that would have removed the thread was lost. Assume the
1622 * latter case and continue on as best we can.
1623 */
1624 if (parent->pid_ != (pid_t)event->fork.ppid) {
1625 dump_printf("removing erroneous parent thread %d/%d\n",
1626 parent->pid_, parent->tid);
1627 machine__remove_thread(machine, parent);
1628 thread__put(parent);
1629 parent = machine__findnew_thread(machine, event->fork.ppid,
1630 event->fork.ptid);
1631 }
1632
1633 /* if a thread currently exists for the thread id remove it */
1634 if (thread != NULL) {
1635 machine__remove_thread(machine, thread);
1636 thread__put(thread);
1637 }
1638
1639 thread = machine__findnew_thread(machine, event->fork.pid,
1640 event->fork.tid);
1641
1642 if (thread == NULL || parent == NULL ||
1643 thread__fork(thread, parent, sample->time) < 0) {
1644 dump_printf("problem processing PERF_RECORD_FORK, skipping event.\n");
1645 err = -1;
1646 }
1647 thread__put(thread);
1648 thread__put(parent);
1649
1650 return err;
1651 }
1652
1653 int machine__process_exit_event(struct machine *machine, union perf_event *event,
1654 struct perf_sample *sample __maybe_unused)
1655 {
1656 struct thread *thread = machine__find_thread(machine,
1657 event->fork.pid,
1658 event->fork.tid);
1659
1660 if (dump_trace)
1661 perf_event__fprintf_task(event, stdout);
1662
1663 if (thread != NULL) {
1664 thread__exited(thread);
1665 thread__put(thread);
1666 }
1667
1668 return 0;
1669 }
1670
1671 int machine__process_event(struct machine *machine, union perf_event *event,
1672 struct perf_sample *sample)
1673 {
1674 int ret;
1675
1676 switch (event->header.type) {
1677 case PERF_RECORD_COMM:
1678 ret = machine__process_comm_event(machine, event, sample); break;
1679 case PERF_RECORD_MMAP:
1680 ret = machine__process_mmap_event(machine, event, sample); break;
1681 case PERF_RECORD_NAMESPACES:
1682 ret = machine__process_namespaces_event(machine, event, sample); break;
1683 case PERF_RECORD_MMAP2:
1684 ret = machine__process_mmap2_event(machine, event, sample); break;
1685 case PERF_RECORD_FORK:
1686 ret = machine__process_fork_event(machine, event, sample); break;
1687 case PERF_RECORD_EXIT:
1688 ret = machine__process_exit_event(machine, event, sample); break;
1689 case PERF_RECORD_LOST:
1690 ret = machine__process_lost_event(machine, event, sample); break;
1691 case PERF_RECORD_AUX:
1692 ret = machine__process_aux_event(machine, event); break;
1693 case PERF_RECORD_ITRACE_START:
1694 ret = machine__process_itrace_start_event(machine, event); break;
1695 case PERF_RECORD_LOST_SAMPLES:
1696 ret = machine__process_lost_samples_event(machine, event, sample); break;
1697 case PERF_RECORD_SWITCH:
1698 case PERF_RECORD_SWITCH_CPU_WIDE:
1699 ret = machine__process_switch_event(machine, event); break;
1700 default:
1701 ret = -1;
1702 break;
1703 }
1704
1705 return ret;
1706 }
1707
1708 static bool symbol__match_regex(struct symbol *sym, regex_t *regex)
1709 {
1710 if (!regexec(regex, sym->name, 0, NULL, 0))
1711 return 1;
1712 return 0;
1713 }
1714
1715 static void ip__resolve_ams(struct thread *thread,
1716 struct addr_map_symbol *ams,
1717 u64 ip)
1718 {
1719 struct addr_location al;
1720
1721 memset(&al, 0, sizeof(al));
1722 /*
1723 * We cannot use the header.misc hint to determine whether a
1724 * branch stack address is user, kernel, guest, hypervisor.
1725 * Branches may straddle the kernel/user/hypervisor boundaries.
1726 * Thus, we have to try consecutively until we find a match
1727 * or else, the symbol is unknown
1728 */
1729 thread__find_cpumode_addr_location(thread, MAP__FUNCTION, ip, &al);
1730
1731 ams->addr = ip;
1732 ams->al_addr = al.addr;
1733 ams->sym = al.sym;
1734 ams->map = al.map;
1735 ams->phys_addr = 0;
1736 }
1737
1738 static void ip__resolve_data(struct thread *thread,
1739 u8 m, struct addr_map_symbol *ams,
1740 u64 addr, u64 phys_addr)
1741 {
1742 struct addr_location al;
1743
1744 memset(&al, 0, sizeof(al));
1745
1746 thread__find_addr_location(thread, m, MAP__VARIABLE, addr, &al);
1747 if (al.map == NULL) {
1748 /*
1749 * some shared data regions have execute bit set which puts
1750 * their mapping in the MAP__FUNCTION type array.
1751 * Check there as a fallback option before dropping the sample.
1752 */
1753 thread__find_addr_location(thread, m, MAP__FUNCTION, addr, &al);
1754 }
1755
1756 ams->addr = addr;
1757 ams->al_addr = al.addr;
1758 ams->sym = al.sym;
1759 ams->map = al.map;
1760 ams->phys_addr = phys_addr;
1761 }
1762
1763 struct mem_info *sample__resolve_mem(struct perf_sample *sample,
1764 struct addr_location *al)
1765 {
1766 struct mem_info *mi = zalloc(sizeof(*mi));
1767
1768 if (!mi)
1769 return NULL;
1770
1771 ip__resolve_ams(al->thread, &mi->iaddr, sample->ip);
1772 ip__resolve_data(al->thread, al->cpumode, &mi->daddr,
1773 sample->addr, sample->phys_addr);
1774 mi->data_src.val = sample->data_src;
1775
1776 return mi;
1777 }
1778
1779 struct iterations {
1780 int nr_loop_iter;
1781 u64 cycles;
1782 };
1783
1784 static int add_callchain_ip(struct thread *thread,
1785 struct callchain_cursor *cursor,
1786 struct symbol **parent,
1787 struct addr_location *root_al,
1788 u8 *cpumode,
1789 u64 ip,
1790 bool branch,
1791 struct branch_flags *flags,
1792 struct iterations *iter,
1793 u64 branch_from)
1794 {
1795 struct addr_location al;
1796 int nr_loop_iter = 0;
1797 u64 iter_cycles = 0;
1798
1799 al.filtered = 0;
1800 al.sym = NULL;
1801 if (!cpumode) {
1802 thread__find_cpumode_addr_location(thread, MAP__FUNCTION,
1803 ip, &al);
1804 } else {
1805 if (ip >= PERF_CONTEXT_MAX) {
1806 switch (ip) {
1807 case PERF_CONTEXT_HV:
1808 *cpumode = PERF_RECORD_MISC_HYPERVISOR;
1809 break;
1810 case PERF_CONTEXT_KERNEL:
1811 *cpumode = PERF_RECORD_MISC_KERNEL;
1812 break;
1813 case PERF_CONTEXT_USER:
1814 *cpumode = PERF_RECORD_MISC_USER;
1815 break;
1816 default:
1817 pr_debug("invalid callchain context: "
1818 "%"PRId64"\n", (s64) ip);
1819 /*
1820 * It seems the callchain is corrupted.
1821 * Discard all.
1822 */
1823 callchain_cursor_reset(cursor);
1824 return 1;
1825 }
1826 return 0;
1827 }
1828 thread__find_addr_location(thread, *cpumode, MAP__FUNCTION,
1829 ip, &al);
1830 }
1831
1832 if (al.sym != NULL) {
1833 if (perf_hpp_list.parent && !*parent &&
1834 symbol__match_regex(al.sym, &parent_regex))
1835 *parent = al.sym;
1836 else if (have_ignore_callees && root_al &&
1837 symbol__match_regex(al.sym, &ignore_callees_regex)) {
1838 /* Treat this symbol as the root,
1839 forgetting its callees. */
1840 *root_al = al;
1841 callchain_cursor_reset(cursor);
1842 }
1843 }
1844
1845 if (symbol_conf.hide_unresolved && al.sym == NULL)
1846 return 0;
1847
1848 if (iter) {
1849 nr_loop_iter = iter->nr_loop_iter;
1850 iter_cycles = iter->cycles;
1851 }
1852
1853 return callchain_cursor_append(cursor, al.addr, al.map, al.sym,
1854 branch, flags, nr_loop_iter,
1855 iter_cycles, branch_from);
1856 }
1857
1858 struct branch_info *sample__resolve_bstack(struct perf_sample *sample,
1859 struct addr_location *al)
1860 {
1861 unsigned int i;
1862 const struct branch_stack *bs = sample->branch_stack;
1863 struct branch_info *bi = calloc(bs->nr, sizeof(struct branch_info));
1864
1865 if (!bi)
1866 return NULL;
1867
1868 for (i = 0; i < bs->nr; i++) {
1869 ip__resolve_ams(al->thread, &bi[i].to, bs->entries[i].to);
1870 ip__resolve_ams(al->thread, &bi[i].from, bs->entries[i].from);
1871 bi[i].flags = bs->entries[i].flags;
1872 }
1873 return bi;
1874 }
1875
1876 static void save_iterations(struct iterations *iter,
1877 struct branch_entry *be, int nr)
1878 {
1879 int i;
1880
1881 iter->nr_loop_iter = nr;
1882 iter->cycles = 0;
1883
1884 for (i = 0; i < nr; i++)
1885 iter->cycles += be[i].flags.cycles;
1886 }
1887
1888 #define CHASHSZ 127
1889 #define CHASHBITS 7
1890 #define NO_ENTRY 0xff
1891
1892 #define PERF_MAX_BRANCH_DEPTH 127
1893
1894 /* Remove loops. */
1895 static int remove_loops(struct branch_entry *l, int nr,
1896 struct iterations *iter)
1897 {
1898 int i, j, off;
1899 unsigned char chash[CHASHSZ];
1900
1901 memset(chash, NO_ENTRY, sizeof(chash));
1902
1903 BUG_ON(PERF_MAX_BRANCH_DEPTH > 255);
1904
1905 for (i = 0; i < nr; i++) {
1906 int h = hash_64(l[i].from, CHASHBITS) % CHASHSZ;
1907
1908 /* no collision handling for now */
1909 if (chash[h] == NO_ENTRY) {
1910 chash[h] = i;
1911 } else if (l[chash[h]].from == l[i].from) {
1912 bool is_loop = true;
1913 /* check if it is a real loop */
1914 off = 0;
1915 for (j = chash[h]; j < i && i + off < nr; j++, off++)
1916 if (l[j].from != l[i + off].from) {
1917 is_loop = false;
1918 break;
1919 }
1920 if (is_loop) {
1921 j = nr - (i + off);
1922 if (j > 0) {
1923 save_iterations(iter + i + off,
1924 l + i, off);
1925
1926 memmove(iter + i, iter + i + off,
1927 j * sizeof(*iter));
1928
1929 memmove(l + i, l + i + off,
1930 j * sizeof(*l));
1931 }
1932
1933 nr -= off;
1934 }
1935 }
1936 }
1937 return nr;
1938 }
1939
1940 /*
1941 * Recolve LBR callstack chain sample
1942 * Return:
1943 * 1 on success get LBR callchain information
1944 * 0 no available LBR callchain information, should try fp
1945 * negative error code on other errors.
1946 */
1947 static int resolve_lbr_callchain_sample(struct thread *thread,
1948 struct callchain_cursor *cursor,
1949 struct perf_sample *sample,
1950 struct symbol **parent,
1951 struct addr_location *root_al,
1952 int max_stack)
1953 {
1954 struct ip_callchain *chain = sample->callchain;
1955 int chain_nr = min(max_stack, (int)chain->nr), i;
1956 u8 cpumode = PERF_RECORD_MISC_USER;
1957 u64 ip, branch_from = 0;
1958
1959 for (i = 0; i < chain_nr; i++) {
1960 if (chain->ips[i] == PERF_CONTEXT_USER)
1961 break;
1962 }
1963
1964 /* LBR only affects the user callchain */
1965 if (i != chain_nr) {
1966 struct branch_stack *lbr_stack = sample->branch_stack;
1967 int lbr_nr = lbr_stack->nr, j, k;
1968 bool branch;
1969 struct branch_flags *flags;
1970 /*
1971 * LBR callstack can only get user call chain.
1972 * The mix_chain_nr is kernel call chain
1973 * number plus LBR user call chain number.
1974 * i is kernel call chain number,
1975 * 1 is PERF_CONTEXT_USER,
1976 * lbr_nr + 1 is the user call chain number.
1977 * For details, please refer to the comments
1978 * in callchain__printf
1979 */
1980 int mix_chain_nr = i + 1 + lbr_nr + 1;
1981
1982 for (j = 0; j < mix_chain_nr; j++) {
1983 int err;
1984 branch = false;
1985 flags = NULL;
1986
1987 if (callchain_param.order == ORDER_CALLEE) {
1988 if (j < i + 1)
1989 ip = chain->ips[j];
1990 else if (j > i + 1) {
1991 k = j - i - 2;
1992 ip = lbr_stack->entries[k].from;
1993 branch = true;
1994 flags = &lbr_stack->entries[k].flags;
1995 } else {
1996 ip = lbr_stack->entries[0].to;
1997 branch = true;
1998 flags = &lbr_stack->entries[0].flags;
1999 branch_from =
2000 lbr_stack->entries[0].from;
2001 }
2002 } else {
2003 if (j < lbr_nr) {
2004 k = lbr_nr - j - 1;
2005 ip = lbr_stack->entries[k].from;
2006 branch = true;
2007 flags = &lbr_stack->entries[k].flags;
2008 }
2009 else if (j > lbr_nr)
2010 ip = chain->ips[i + 1 - (j - lbr_nr)];
2011 else {
2012 ip = lbr_stack->entries[0].to;
2013 branch = true;
2014 flags = &lbr_stack->entries[0].flags;
2015 branch_from =
2016 lbr_stack->entries[0].from;
2017 }
2018 }
2019
2020 err = add_callchain_ip(thread, cursor, parent,
2021 root_al, &cpumode, ip,
2022 branch, flags, NULL,
2023 branch_from);
2024 if (err)
2025 return (err < 0) ? err : 0;
2026 }
2027 return 1;
2028 }
2029
2030 return 0;
2031 }
2032
2033 static int thread__resolve_callchain_sample(struct thread *thread,
2034 struct callchain_cursor *cursor,
2035 struct perf_evsel *evsel,
2036 struct perf_sample *sample,
2037 struct symbol **parent,
2038 struct addr_location *root_al,
2039 int max_stack)
2040 {
2041 struct branch_stack *branch = sample->branch_stack;
2042 struct ip_callchain *chain = sample->callchain;
2043 int chain_nr = 0;
2044 u8 cpumode = PERF_RECORD_MISC_USER;
2045 int i, j, err, nr_entries;
2046 int skip_idx = -1;
2047 int first_call = 0;
2048
2049 if (chain)
2050 chain_nr = chain->nr;
2051
2052 if (perf_evsel__has_branch_callstack(evsel)) {
2053 err = resolve_lbr_callchain_sample(thread, cursor, sample, parent,
2054 root_al, max_stack);
2055 if (err)
2056 return (err < 0) ? err : 0;
2057 }
2058
2059 /*
2060 * Based on DWARF debug information, some architectures skip
2061 * a callchain entry saved by the kernel.
2062 */
2063 skip_idx = arch_skip_callchain_idx(thread, chain);
2064
2065 /*
2066 * Add branches to call stack for easier browsing. This gives
2067 * more context for a sample than just the callers.
2068 *
2069 * This uses individual histograms of paths compared to the
2070 * aggregated histograms the normal LBR mode uses.
2071 *
2072 * Limitations for now:
2073 * - No extra filters
2074 * - No annotations (should annotate somehow)
2075 */
2076
2077 if (branch && callchain_param.branch_callstack) {
2078 int nr = min(max_stack, (int)branch->nr);
2079 struct branch_entry be[nr];
2080 struct iterations iter[nr];
2081
2082 if (branch->nr > PERF_MAX_BRANCH_DEPTH) {
2083 pr_warning("corrupted branch chain. skipping...\n");
2084 goto check_calls;
2085 }
2086
2087 for (i = 0; i < nr; i++) {
2088 if (callchain_param.order == ORDER_CALLEE) {
2089 be[i] = branch->entries[i];
2090
2091 if (chain == NULL)
2092 continue;
2093
2094 /*
2095 * Check for overlap into the callchain.
2096 * The return address is one off compared to
2097 * the branch entry. To adjust for this
2098 * assume the calling instruction is not longer
2099 * than 8 bytes.
2100 */
2101 if (i == skip_idx ||
2102 chain->ips[first_call] >= PERF_CONTEXT_MAX)
2103 first_call++;
2104 else if (be[i].from < chain->ips[first_call] &&
2105 be[i].from >= chain->ips[first_call] - 8)
2106 first_call++;
2107 } else
2108 be[i] = branch->entries[branch->nr - i - 1];
2109 }
2110
2111 memset(iter, 0, sizeof(struct iterations) * nr);
2112 nr = remove_loops(be, nr, iter);
2113
2114 for (i = 0; i < nr; i++) {
2115 err = add_callchain_ip(thread, cursor, parent,
2116 root_al,
2117 NULL, be[i].to,
2118 true, &be[i].flags,
2119 NULL, be[i].from);
2120
2121 if (!err)
2122 err = add_callchain_ip(thread, cursor, parent, root_al,
2123 NULL, be[i].from,
2124 true, &be[i].flags,
2125 &iter[i], 0);
2126 if (err == -EINVAL)
2127 break;
2128 if (err)
2129 return err;
2130 }
2131
2132 if (chain_nr == 0)
2133 return 0;
2134
2135 chain_nr -= nr;
2136 }
2137
2138 check_calls:
2139 for (i = first_call, nr_entries = 0;
2140 i < chain_nr && nr_entries < max_stack; i++) {
2141 u64 ip;
2142
2143 if (callchain_param.order == ORDER_CALLEE)
2144 j = i;
2145 else
2146 j = chain->nr - i - 1;
2147
2148 #ifdef HAVE_SKIP_CALLCHAIN_IDX
2149 if (j == skip_idx)
2150 continue;
2151 #endif
2152 ip = chain->ips[j];
2153
2154 if (ip < PERF_CONTEXT_MAX)
2155 ++nr_entries;
2156
2157 err = add_callchain_ip(thread, cursor, parent,
2158 root_al, &cpumode, ip,
2159 false, NULL, NULL, 0);
2160
2161 if (err)
2162 return (err < 0) ? err : 0;
2163 }
2164
2165 return 0;
2166 }
2167
2168 static int unwind_entry(struct unwind_entry *entry, void *arg)
2169 {
2170 struct callchain_cursor *cursor = arg;
2171
2172 if (symbol_conf.hide_unresolved && entry->sym == NULL)
2173 return 0;
2174 return callchain_cursor_append(cursor, entry->ip,
2175 entry->map, entry->sym,
2176 false, NULL, 0, 0, 0);
2177 }
2178
2179 static int thread__resolve_callchain_unwind(struct thread *thread,
2180 struct callchain_cursor *cursor,
2181 struct perf_evsel *evsel,
2182 struct perf_sample *sample,
2183 int max_stack)
2184 {
2185 /* Can we do dwarf post unwind? */
2186 if (!((evsel->attr.sample_type & PERF_SAMPLE_REGS_USER) &&
2187 (evsel->attr.sample_type & PERF_SAMPLE_STACK_USER)))
2188 return 0;
2189
2190 /* Bail out if nothing was captured. */
2191 if ((!sample->user_regs.regs) ||
2192 (!sample->user_stack.size))
2193 return 0;
2194
2195 return unwind__get_entries(unwind_entry, cursor,
2196 thread, sample, max_stack);
2197 }
2198
2199 int thread__resolve_callchain(struct thread *thread,
2200 struct callchain_cursor *cursor,
2201 struct perf_evsel *evsel,
2202 struct perf_sample *sample,
2203 struct symbol **parent,
2204 struct addr_location *root_al,
2205 int max_stack)
2206 {
2207 int ret = 0;
2208
2209 callchain_cursor_reset(&callchain_cursor);
2210
2211 if (callchain_param.order == ORDER_CALLEE) {
2212 ret = thread__resolve_callchain_sample(thread, cursor,
2213 evsel, sample,
2214 parent, root_al,
2215 max_stack);
2216 if (ret)
2217 return ret;
2218 ret = thread__resolve_callchain_unwind(thread, cursor,
2219 evsel, sample,
2220 max_stack);
2221 } else {
2222 ret = thread__resolve_callchain_unwind(thread, cursor,
2223 evsel, sample,
2224 max_stack);
2225 if (ret)
2226 return ret;
2227 ret = thread__resolve_callchain_sample(thread, cursor,
2228 evsel, sample,
2229 parent, root_al,
2230 max_stack);
2231 }
2232
2233 return ret;
2234 }
2235
2236 int machine__for_each_thread(struct machine *machine,
2237 int (*fn)(struct thread *thread, void *p),
2238 void *priv)
2239 {
2240 struct rb_node *nd;
2241 struct thread *thread;
2242 int rc = 0;
2243
2244 for (nd = rb_first(&machine->threads); nd; nd = rb_next(nd)) {
2245 thread = rb_entry(nd, struct thread, rb_node);
2246 rc = fn(thread, priv);
2247 if (rc != 0)
2248 return rc;
2249 }
2250
2251 list_for_each_entry(thread, &machine->dead_threads, node) {
2252 rc = fn(thread, priv);
2253 if (rc != 0)
2254 return rc;
2255 }
2256 return rc;
2257 }
2258
2259 int machines__for_each_thread(struct machines *machines,
2260 int (*fn)(struct thread *thread, void *p),
2261 void *priv)
2262 {
2263 struct rb_node *nd;
2264 int rc = 0;
2265
2266 rc = machine__for_each_thread(&machines->host, fn, priv);
2267 if (rc != 0)
2268 return rc;
2269
2270 for (nd = rb_first(&machines->guests); nd; nd = rb_next(nd)) {
2271 struct machine *machine = rb_entry(nd, struct machine, rb_node);
2272
2273 rc = machine__for_each_thread(machine, fn, priv);
2274 if (rc != 0)
2275 return rc;
2276 }
2277 return rc;
2278 }
2279
2280 int __machine__synthesize_threads(struct machine *machine, struct perf_tool *tool,
2281 struct target *target, struct thread_map *threads,
2282 perf_event__handler_t process, bool data_mmap,
2283 unsigned int proc_map_timeout)
2284 {
2285 if (target__has_task(target))
2286 return perf_event__synthesize_thread_map(tool, threads, process, machine, data_mmap, proc_map_timeout);
2287 else if (target__has_cpu(target))
2288 return perf_event__synthesize_threads(tool, process, machine, data_mmap, proc_map_timeout);
2289 /* command specified */
2290 return 0;
2291 }
2292
2293 pid_t machine__get_current_tid(struct machine *machine, int cpu)
2294 {
2295 if (cpu < 0 || cpu >= MAX_NR_CPUS || !machine->current_tid)
2296 return -1;
2297
2298 return machine->current_tid[cpu];
2299 }
2300
2301 int machine__set_current_tid(struct machine *machine, int cpu, pid_t pid,
2302 pid_t tid)
2303 {
2304 struct thread *thread;
2305
2306 if (cpu < 0)
2307 return -EINVAL;
2308
2309 if (!machine->current_tid) {
2310 int i;
2311
2312 machine->current_tid = calloc(MAX_NR_CPUS, sizeof(pid_t));
2313 if (!machine->current_tid)
2314 return -ENOMEM;
2315 for (i = 0; i < MAX_NR_CPUS; i++)
2316 machine->current_tid[i] = -1;
2317 }
2318
2319 if (cpu >= MAX_NR_CPUS) {
2320 pr_err("Requested CPU %d too large. ", cpu);
2321 pr_err("Consider raising MAX_NR_CPUS\n");
2322 return -EINVAL;
2323 }
2324
2325 machine->current_tid[cpu] = tid;
2326
2327 thread = machine__findnew_thread(machine, pid, tid);
2328 if (!thread)
2329 return -ENOMEM;
2330
2331 thread->cpu = cpu;
2332 thread__put(thread);
2333
2334 return 0;
2335 }
2336
2337 /*
2338 * Compares the raw arch string. N.B. see instead perf_env__arch() if a
2339 * normalized arch is needed.
2340 */
2341 bool machine__is(struct machine *machine, const char *arch)
2342 {
2343 return machine && !strcmp(perf_env__raw_arch(machine->env), arch);
2344 }
2345
2346 int machine__nr_cpus_avail(struct machine *machine)
2347 {
2348 return machine ? perf_env__nr_cpus_avail(machine->env) : 0;
2349 }
2350
2351 int machine__get_kernel_start(struct machine *machine)
2352 {
2353 struct map *map = machine__kernel_map(machine);
2354 int err = 0;
2355
2356 /*
2357 * The only addresses above 2^63 are kernel addresses of a 64-bit
2358 * kernel. Note that addresses are unsigned so that on a 32-bit system
2359 * all addresses including kernel addresses are less than 2^32. In
2360 * that case (32-bit system), if the kernel mapping is unknown, all
2361 * addresses will be assumed to be in user space - see
2362 * machine__kernel_ip().
2363 */
2364 machine->kernel_start = 1ULL << 63;
2365 if (map) {
2366 err = map__load(map);
2367 /*
2368 * On x86_64, PTI entry trampolines are less than the
2369 * start of kernel text, but still above 2^63. So leave
2370 * kernel_start = 1ULL << 63 for x86_64.
2371 */
2372 if (!err && !machine__is(machine, "x86_64"))
2373 machine->kernel_start = map->start;
2374 }
2375 return err;
2376 }
2377
2378 struct dso *machine__findnew_dso(struct machine *machine, const char *filename)
2379 {
2380 return dsos__findnew(&machine->dsos, filename);
2381 }
2382
2383 char *machine__resolve_kernel_addr(void *vmachine, unsigned long long *addrp, char **modp)
2384 {
2385 struct machine *machine = vmachine;
2386 struct map *map;
2387 struct symbol *sym = map_groups__find_symbol(&machine->kmaps, MAP__FUNCTION, *addrp, &map);
2388
2389 if (sym == NULL)
2390 return NULL;
2391
2392 *modp = __map__is_kmodule(map) ? (char *)map->dso->short_name : NULL;
2393 *addrp = map->unmap_ip(map, sym->start);
2394 return sym->name;
2395 }