Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Arnaldo Carvalho de Melo | 76b31a2 | 2017-04-18 12:26:44 -0300 | [diff] [blame] | 2 | #include <dirent.h> |
Arnaldo Carvalho de Melo | a43783a | 2017-04-18 10:46:11 -0300 | [diff] [blame] | 3 | #include <errno.h> |
Arnaldo Carvalho de Melo | fd20e81 | 2017-04-17 15:23:08 -0300 | [diff] [blame] | 4 | #include <inttypes.h> |
Arnaldo Carvalho de Melo | 1eae20c | 2017-04-18 12:33:30 -0300 | [diff] [blame] | 5 | #include <regex.h> |
Arnaldo Carvalho de Melo | f2a39fe | 2019-08-30 14:45:20 -0300 | [diff] [blame] | 6 | #include <stdlib.h> |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 7 | #include "callchain.h" |
Arnaldo Carvalho de Melo | b0a7d1a | 2012-10-06 16:26:02 -0300 | [diff] [blame] | 8 | #include "debug.h" |
Arnaldo Carvalho de Melo | 4a3cec8 | 2019-08-30 11:11:01 -0300 | [diff] [blame] | 9 | #include "dso.h" |
Arnaldo Carvalho de Melo | f2a39fe | 2019-08-30 14:45:20 -0300 | [diff] [blame] | 10 | #include "env.h" |
Arnaldo Carvalho de Melo | b0a7d1a | 2012-10-06 16:26:02 -0300 | [diff] [blame] | 11 | #include "event.h" |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 12 | #include "evsel.h" |
| 13 | #include "hist.h" |
Arnaldo Carvalho de Melo | 9d2f8e2 | 2012-10-06 15:43:20 -0300 | [diff] [blame] | 14 | #include "machine.h" |
| 15 | #include "map.h" |
Arnaldo Carvalho de Melo | d3300a3 | 2019-08-30 15:09:54 -0300 | [diff] [blame] | 16 | #include "map_symbol.h" |
| 17 | #include "branch.h" |
| 18 | #include "mem-events.h" |
Arnaldo Carvalho de Melo | 9d5f0c3 | 2022-01-18 09:02:43 -0300 | [diff] [blame] | 19 | #include "path.h" |
Arnaldo Carvalho de Melo | 97b9d86 | 2019-08-22 17:10:08 -0300 | [diff] [blame] | 20 | #include "srcline.h" |
Arnaldo Carvalho de Melo | daecf9e | 2019-01-28 00:03:34 +0100 | [diff] [blame] | 21 | #include "symbol.h" |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 22 | #include "sort.h" |
Arnaldo Carvalho de Melo | 69d2591 | 2012-11-09 11:32:52 -0300 | [diff] [blame] | 23 | #include "strlist.h" |
Arnaldo Carvalho de Melo | aeb00b1 | 2019-08-22 15:40:29 -0300 | [diff] [blame] | 24 | #include "target.h" |
Arnaldo Carvalho de Melo | 9d2f8e2 | 2012-10-06 15:43:20 -0300 | [diff] [blame] | 25 | #include "thread.h" |
Arnaldo Carvalho de Melo | 97b9d86 | 2019-08-22 17:10:08 -0300 | [diff] [blame] | 26 | #include "util.h" |
Adrian Hunter | d027b640 | 2014-07-23 14:23:00 +0300 | [diff] [blame] | 27 | #include "vdso.h" |
Arnaldo Carvalho de Melo | 9d2f8e2 | 2012-10-06 15:43:20 -0300 | [diff] [blame] | 28 | #include <stdbool.h> |
Arnaldo Carvalho de Melo | 7a8ef4c | 2017-04-19 20:57:47 -0300 | [diff] [blame] | 29 | #include <sys/types.h> |
| 30 | #include <sys/stat.h> |
| 31 | #include <unistd.h> |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 32 | #include "unwind.h" |
Andi Kleen | 8b7bad5 | 2014-11-12 18:05:20 -0800 | [diff] [blame] | 33 | #include "linux/hash.h" |
Hari Bathini | f3b3614 | 2017-03-08 02:11:43 +0530 | [diff] [blame] | 34 | #include "asm/bug.h" |
Song Liu | 45178a9 | 2019-01-17 08:15:18 -0800 | [diff] [blame] | 35 | #include "bpf-event.h" |
Jiri Olsa | 20f2be1 | 2019-08-06 15:25:25 +0200 | [diff] [blame] | 36 | #include <internal/lib.h> // page_size |
Namhyung Kim | d1277aa | 2020-03-25 21:45:31 +0900 | [diff] [blame] | 37 | #include "cgroup.h" |
Alexandre Truong | b9f6fbb | 2021-12-17 15:45:20 +0000 | [diff] [blame] | 38 | #include "arm64-frame-pointer-unwind-support.h" |
Arnaldo Carvalho de Melo | 9d2f8e2 | 2012-10-06 15:43:20 -0300 | [diff] [blame] | 39 | |
Arnaldo Carvalho de Melo | 3052ba5 | 2019-06-25 17:27:31 -0300 | [diff] [blame] | 40 | #include <linux/ctype.h> |
Arnaldo Carvalho de Melo | 3d689ed | 2017-04-17 16:10:49 -0300 | [diff] [blame] | 41 | #include <symbol/kallsyms.h> |
Arnaldo Carvalho de Melo | 0f476f2 | 2018-04-26 11:30:50 -0300 | [diff] [blame] | 42 | #include <linux/mman.h> |
Arnaldo Carvalho de Melo | 8520a98 | 2019-08-29 16:18:59 -0300 | [diff] [blame] | 43 | #include <linux/string.h> |
Arnaldo Carvalho de Melo | 7f7c536 | 2019-07-04 11:32:27 -0300 | [diff] [blame] | 44 | #include <linux/zalloc.h> |
Arnaldo Carvalho de Melo | 3d689ed | 2017-04-17 16:10:49 -0300 | [diff] [blame] | 45 | |
Arnaldo Carvalho de Melo | b91fc39 | 2015-04-06 20:43:22 -0300 | [diff] [blame] | 46 | static void __machine__remove_thread(struct machine *machine, struct thread *th, bool lock); |
| 47 | |
Arnaldo Carvalho de Melo | 93730f8 | 2019-10-31 15:22:24 -0300 | [diff] [blame] | 48 | static struct dso *machine__kernel_dso(struct machine *machine) |
| 49 | { |
| 50 | return machine->vmlinux_map->dso; |
| 51 | } |
| 52 | |
Arnaldo Carvalho de Melo | e167f99 | 2014-10-14 15:07:48 -0300 | [diff] [blame] | 53 | static void dsos__init(struct dsos *dsos) |
| 54 | { |
| 55 | INIT_LIST_HEAD(&dsos->head); |
| 56 | dsos->root = RB_ROOT; |
Arnaldo Carvalho de Melo | 0a7c74e | 2017-04-04 13:15:04 -0300 | [diff] [blame] | 57 | init_rwsem(&dsos->lock); |
Arnaldo Carvalho de Melo | e167f99 | 2014-10-14 15:07:48 -0300 | [diff] [blame] | 58 | } |
| 59 | |
Kan Liang | 91e467b | 2017-09-10 19:23:14 -0700 | [diff] [blame] | 60 | static void machine__threads_init(struct machine *machine) |
| 61 | { |
| 62 | int i; |
| 63 | |
| 64 | for (i = 0; i < THREADS__TABLE_SIZE; i++) { |
| 65 | struct threads *threads = &machine->threads[i]; |
Davidlohr Bueso | f3acb3a | 2018-12-06 11:18:14 -0800 | [diff] [blame] | 66 | threads->entries = RB_ROOT_CACHED; |
Arnaldo Carvalho de Melo | 0a7c74e | 2017-04-04 13:15:04 -0300 | [diff] [blame] | 67 | init_rwsem(&threads->lock); |
Kan Liang | 91e467b | 2017-09-10 19:23:14 -0700 | [diff] [blame] | 68 | threads->nr = 0; |
| 69 | INIT_LIST_HEAD(&threads->dead); |
| 70 | threads->last_match = NULL; |
| 71 | } |
| 72 | } |
| 73 | |
Jiri Olsa | 8c7f1bb | 2018-02-15 13:26:30 +0100 | [diff] [blame] | 74 | static int machine__set_mmap_name(struct machine *machine) |
| 75 | { |
Jiri Olsa | c192524 | 2018-03-12 16:24:06 +0100 | [diff] [blame] | 76 | if (machine__is_host(machine)) |
| 77 | machine->mmap_name = strdup("[kernel.kallsyms]"); |
| 78 | else if (machine__is_default_guest(machine)) |
| 79 | machine->mmap_name = strdup("[guest.kernel.kallsyms]"); |
| 80 | else if (asprintf(&machine->mmap_name, "[guest.kernel.kallsyms.%d]", |
| 81 | machine->pid) < 0) |
| 82 | machine->mmap_name = NULL; |
Jiri Olsa | 8c7f1bb | 2018-02-15 13:26:30 +0100 | [diff] [blame] | 83 | |
| 84 | return machine->mmap_name ? 0 : -ENOMEM; |
| 85 | } |
| 86 | |
Arnaldo Carvalho de Melo | 69d2591 | 2012-11-09 11:32:52 -0300 | [diff] [blame] | 87 | int machine__init(struct machine *machine, const char *root_dir, pid_t pid) |
| 88 | { |
Jiri Olsa | 81f981d | 2018-02-15 13:26:29 +0100 | [diff] [blame] | 89 | int err = -ENOMEM; |
| 90 | |
Wang Nan | 93b0ba3 | 2015-12-08 02:25:44 +0000 | [diff] [blame] | 91 | memset(machine, 0, sizeof(*machine)); |
Arnaldo Carvalho de Melo | 79b6bb7 | 2019-11-25 21:58:33 -0300 | [diff] [blame] | 92 | maps__init(&machine->kmaps, machine); |
Arnaldo Carvalho de Melo | 69d2591 | 2012-11-09 11:32:52 -0300 | [diff] [blame] | 93 | RB_CLEAR_NODE(&machine->rb_node); |
Arnaldo Carvalho de Melo | 3d39ac5 | 2015-05-28 13:06:42 -0300 | [diff] [blame] | 94 | dsos__init(&machine->dsos); |
Arnaldo Carvalho de Melo | 69d2591 | 2012-11-09 11:32:52 -0300 | [diff] [blame] | 95 | |
Kan Liang | 91e467b | 2017-09-10 19:23:14 -0700 | [diff] [blame] | 96 | machine__threads_init(machine); |
Arnaldo Carvalho de Melo | 69d2591 | 2012-11-09 11:32:52 -0300 | [diff] [blame] | 97 | |
Adrian Hunter | d027b640 | 2014-07-23 14:23:00 +0300 | [diff] [blame] | 98 | machine->vdso_info = NULL; |
Arnaldo Carvalho de Melo | 4cde998 | 2015-09-09 12:25:00 -0300 | [diff] [blame] | 99 | machine->env = NULL; |
Adrian Hunter | d027b640 | 2014-07-23 14:23:00 +0300 | [diff] [blame] | 100 | |
Arnaldo Carvalho de Melo | 69d2591 | 2012-11-09 11:32:52 -0300 | [diff] [blame] | 101 | machine->pid = pid; |
| 102 | |
Jiri Olsa | 14bd6d2 | 2014-01-07 13:47:19 +0100 | [diff] [blame] | 103 | machine->id_hdr_size = 0; |
Arnaldo Carvalho de Melo | caf8a0d | 2016-05-17 11:56:24 -0300 | [diff] [blame] | 104 | machine->kptr_restrict_warned = false; |
Adrian Hunter | cfe1c41 | 2014-07-31 09:00:45 +0300 | [diff] [blame] | 105 | machine->comm_exec = false; |
Adrian Hunter | fbe2af4 | 2014-08-15 22:08:39 +0300 | [diff] [blame] | 106 | machine->kernel_start = 0; |
Arnaldo Carvalho de Melo | 3183f8c | 2018-04-26 16:52:34 -0300 | [diff] [blame] | 107 | machine->vmlinux_map = NULL; |
Masami Hiramatsu | cc1121a | 2015-12-09 11:11:33 +0900 | [diff] [blame] | 108 | |
Arnaldo Carvalho de Melo | 69d2591 | 2012-11-09 11:32:52 -0300 | [diff] [blame] | 109 | machine->root_dir = strdup(root_dir); |
| 110 | if (machine->root_dir == NULL) |
| 111 | return -ENOMEM; |
| 112 | |
Jiri Olsa | 8c7f1bb | 2018-02-15 13:26:30 +0100 | [diff] [blame] | 113 | if (machine__set_mmap_name(machine)) |
| 114 | goto out; |
| 115 | |
Arnaldo Carvalho de Melo | 69d2591 | 2012-11-09 11:32:52 -0300 | [diff] [blame] | 116 | if (pid != HOST_KERNEL_ID) { |
Adrian Hunter | 1fcb876 | 2014-07-14 13:02:25 +0300 | [diff] [blame] | 117 | struct thread *thread = machine__findnew_thread(machine, -1, |
Adrian Hunter | 314add6 | 2013-08-27 11:23:03 +0300 | [diff] [blame] | 118 | pid); |
Arnaldo Carvalho de Melo | 69d2591 | 2012-11-09 11:32:52 -0300 | [diff] [blame] | 119 | char comm[64]; |
| 120 | |
| 121 | if (thread == NULL) |
Jiri Olsa | 81f981d | 2018-02-15 13:26:29 +0100 | [diff] [blame] | 122 | goto out; |
Arnaldo Carvalho de Melo | 69d2591 | 2012-11-09 11:32:52 -0300 | [diff] [blame] | 123 | |
| 124 | snprintf(comm, sizeof(comm), "[guest/%d]", pid); |
Frederic Weisbecker | 162f0be | 2013-09-11 16:18:24 +0200 | [diff] [blame] | 125 | thread__set_comm(thread, comm, 0); |
Arnaldo Carvalho de Melo | b91fc39 | 2015-04-06 20:43:22 -0300 | [diff] [blame] | 126 | thread__put(thread); |
Arnaldo Carvalho de Melo | 69d2591 | 2012-11-09 11:32:52 -0300 | [diff] [blame] | 127 | } |
| 128 | |
Adrian Hunter | b9d266b | 2014-07-22 16:17:25 +0300 | [diff] [blame] | 129 | machine->current_tid = NULL; |
Jiri Olsa | 81f981d | 2018-02-15 13:26:29 +0100 | [diff] [blame] | 130 | err = 0; |
Adrian Hunter | b9d266b | 2014-07-22 16:17:25 +0300 | [diff] [blame] | 131 | |
Jiri Olsa | 81f981d | 2018-02-15 13:26:29 +0100 | [diff] [blame] | 132 | out: |
Jiri Olsa | 8c7f1bb | 2018-02-15 13:26:30 +0100 | [diff] [blame] | 133 | if (err) { |
Jiri Olsa | 81f981d | 2018-02-15 13:26:29 +0100 | [diff] [blame] | 134 | zfree(&machine->root_dir); |
Jiri Olsa | 8c7f1bb | 2018-02-15 13:26:30 +0100 | [diff] [blame] | 135 | zfree(&machine->mmap_name); |
| 136 | } |
Arnaldo Carvalho de Melo | 69d2591 | 2012-11-09 11:32:52 -0300 | [diff] [blame] | 137 | return 0; |
| 138 | } |
| 139 | |
David Ahern | 8fb598e | 2013-09-28 13:13:00 -0600 | [diff] [blame] | 140 | struct machine *machine__new_host(void) |
| 141 | { |
| 142 | struct machine *machine = malloc(sizeof(*machine)); |
| 143 | |
| 144 | if (machine != NULL) { |
| 145 | machine__init(machine, "", HOST_KERNEL_ID); |
| 146 | |
| 147 | if (machine__create_kernel_maps(machine) < 0) |
| 148 | goto out_delete; |
| 149 | } |
| 150 | |
| 151 | return machine; |
| 152 | out_delete: |
| 153 | free(machine); |
| 154 | return NULL; |
| 155 | } |
| 156 | |
Arnaldo Carvalho de Melo | 7d132ca | 2017-01-05 15:31:48 -0300 | [diff] [blame] | 157 | struct machine *machine__new_kallsyms(void) |
| 158 | { |
| 159 | struct machine *machine = machine__new_host(); |
| 160 | /* |
| 161 | * FIXME: |
Ingo Molnar | adba163 | 2018-12-03 11:22:00 +0100 | [diff] [blame] | 162 | * 1) We should switch to machine__load_kallsyms(), i.e. not explicitly |
Arnaldo Carvalho de Melo | 7d132ca | 2017-01-05 15:31:48 -0300 | [diff] [blame] | 163 | * ask for not using the kcore parsing code, once this one is fixed |
| 164 | * to create a map per module. |
| 165 | */ |
Arnaldo Carvalho de Melo | 329f0ad | 2018-04-25 11:40:32 -0300 | [diff] [blame] | 166 | if (machine && machine__load_kallsyms(machine, "/proc/kallsyms") <= 0) { |
Arnaldo Carvalho de Melo | 7d132ca | 2017-01-05 15:31:48 -0300 | [diff] [blame] | 167 | machine__delete(machine); |
| 168 | machine = NULL; |
| 169 | } |
| 170 | |
| 171 | return machine; |
| 172 | } |
| 173 | |
Arnaldo Carvalho de Melo | d3a7c48 | 2015-06-02 11:53:26 -0300 | [diff] [blame] | 174 | static void dsos__purge(struct dsos *dsos) |
Arnaldo Carvalho de Melo | 69d2591 | 2012-11-09 11:32:52 -0300 | [diff] [blame] | 175 | { |
| 176 | struct dso *pos, *n; |
| 177 | |
Arnaldo Carvalho de Melo | 0a7c74e | 2017-04-04 13:15:04 -0300 | [diff] [blame] | 178 | down_write(&dsos->lock); |
Arnaldo Carvalho de Melo | e880784 | 2015-06-01 15:40:01 -0300 | [diff] [blame] | 179 | |
Waiman Long | 8fa7d87 | 2014-09-29 16:07:28 -0400 | [diff] [blame] | 180 | list_for_each_entry_safe(pos, n, &dsos->head, node) { |
Waiman Long | 4598a0a | 2014-09-30 13:36:15 -0400 | [diff] [blame] | 181 | RB_CLEAR_NODE(&pos->rb_node); |
Adrian Hunter | e266a75 | 2015-11-13 11:48:30 +0200 | [diff] [blame] | 182 | pos->root = NULL; |
Arnaldo Carvalho de Melo | d3a7c48 | 2015-06-02 11:53:26 -0300 | [diff] [blame] | 183 | list_del_init(&pos->node); |
| 184 | dso__put(pos); |
Arnaldo Carvalho de Melo | 69d2591 | 2012-11-09 11:32:52 -0300 | [diff] [blame] | 185 | } |
Arnaldo Carvalho de Melo | e880784 | 2015-06-01 15:40:01 -0300 | [diff] [blame] | 186 | |
Arnaldo Carvalho de Melo | 0a7c74e | 2017-04-04 13:15:04 -0300 | [diff] [blame] | 187 | up_write(&dsos->lock); |
Arnaldo Carvalho de Melo | d3a7c48 | 2015-06-02 11:53:26 -0300 | [diff] [blame] | 188 | } |
Arnaldo Carvalho de Melo | e880784 | 2015-06-01 15:40:01 -0300 | [diff] [blame] | 189 | |
Arnaldo Carvalho de Melo | d3a7c48 | 2015-06-02 11:53:26 -0300 | [diff] [blame] | 190 | static void dsos__exit(struct dsos *dsos) |
| 191 | { |
| 192 | dsos__purge(dsos); |
Arnaldo Carvalho de Melo | 0a7c74e | 2017-04-04 13:15:04 -0300 | [diff] [blame] | 193 | exit_rwsem(&dsos->lock); |
Arnaldo Carvalho de Melo | 69d2591 | 2012-11-09 11:32:52 -0300 | [diff] [blame] | 194 | } |
| 195 | |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 196 | void machine__delete_threads(struct machine *machine) |
| 197 | { |
Arnaldo Carvalho de Melo | b91fc39 | 2015-04-06 20:43:22 -0300 | [diff] [blame] | 198 | struct rb_node *nd; |
Kan Liang | 91e467b | 2017-09-10 19:23:14 -0700 | [diff] [blame] | 199 | int i; |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 200 | |
Kan Liang | 91e467b | 2017-09-10 19:23:14 -0700 | [diff] [blame] | 201 | for (i = 0; i < THREADS__TABLE_SIZE; i++) { |
| 202 | struct threads *threads = &machine->threads[i]; |
Arnaldo Carvalho de Melo | 0a7c74e | 2017-04-04 13:15:04 -0300 | [diff] [blame] | 203 | down_write(&threads->lock); |
Davidlohr Bueso | f3acb3a | 2018-12-06 11:18:14 -0800 | [diff] [blame] | 204 | nd = rb_first_cached(&threads->entries); |
Kan Liang | 91e467b | 2017-09-10 19:23:14 -0700 | [diff] [blame] | 205 | while (nd) { |
| 206 | struct thread *t = rb_entry(nd, struct thread, rb_node); |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 207 | |
Kan Liang | 91e467b | 2017-09-10 19:23:14 -0700 | [diff] [blame] | 208 | nd = rb_next(nd); |
| 209 | __machine__remove_thread(machine, t, false); |
| 210 | } |
Arnaldo Carvalho de Melo | 0a7c74e | 2017-04-04 13:15:04 -0300 | [diff] [blame] | 211 | up_write(&threads->lock); |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 212 | } |
| 213 | } |
| 214 | |
Arnaldo Carvalho de Melo | 69d2591 | 2012-11-09 11:32:52 -0300 | [diff] [blame] | 215 | void machine__exit(struct machine *machine) |
| 216 | { |
Kan Liang | 91e467b | 2017-09-10 19:23:14 -0700 | [diff] [blame] | 217 | int i; |
| 218 | |
Arnaldo Carvalho de Melo | 19993b8 | 2017-11-13 16:06:29 -0300 | [diff] [blame] | 219 | if (machine == NULL) |
| 220 | return; |
| 221 | |
Masami Hiramatsu | ebe9729 | 2015-11-18 15:40:24 +0900 | [diff] [blame] | 222 | machine__destroy_kernel_maps(machine); |
Arnaldo Carvalho de Melo | 79b6bb7 | 2019-11-25 21:58:33 -0300 | [diff] [blame] | 223 | maps__exit(&machine->kmaps); |
Arnaldo Carvalho de Melo | e880784 | 2015-06-01 15:40:01 -0300 | [diff] [blame] | 224 | dsos__exit(&machine->dsos); |
Arnaldo Carvalho de Melo | 9a4388c | 2015-05-29 11:54:08 -0300 | [diff] [blame] | 225 | machine__exit_vdso(machine); |
Arnaldo Carvalho de Melo | 0466252 | 2013-12-26 17:41:15 -0300 | [diff] [blame] | 226 | zfree(&machine->root_dir); |
Jiri Olsa | 8c7f1bb | 2018-02-15 13:26:30 +0100 | [diff] [blame] | 227 | zfree(&machine->mmap_name); |
Adrian Hunter | b9d266b | 2014-07-22 16:17:25 +0300 | [diff] [blame] | 228 | zfree(&machine->current_tid); |
Kan Liang | 91e467b | 2017-09-10 19:23:14 -0700 | [diff] [blame] | 229 | |
| 230 | for (i = 0; i < THREADS__TABLE_SIZE; i++) { |
| 231 | struct threads *threads = &machine->threads[i]; |
Arnaldo Carvalho de Melo | 4c00af0 | 2019-07-05 12:11:35 -0300 | [diff] [blame] | 232 | struct thread *thread, *n; |
| 233 | /* |
| 234 | * Forget about the dead, at this point whatever threads were |
| 235 | * left in the dead lists better have a reference count taken |
| 236 | * by who is using them, and then, when they drop those references |
| 237 | * and it finally hits zero, thread__put() will check and see that |
| 238 | * its not in the dead threads list and will not try to remove it |
| 239 | * from there, just calling thread__delete() straight away. |
| 240 | */ |
| 241 | list_for_each_entry_safe(thread, n, &threads->dead, node) |
| 242 | list_del_init(&thread->node); |
| 243 | |
Arnaldo Carvalho de Melo | 0a7c74e | 2017-04-04 13:15:04 -0300 | [diff] [blame] | 244 | exit_rwsem(&threads->lock); |
Kan Liang | 91e467b | 2017-09-10 19:23:14 -0700 | [diff] [blame] | 245 | } |
Arnaldo Carvalho de Melo | 69d2591 | 2012-11-09 11:32:52 -0300 | [diff] [blame] | 246 | } |
| 247 | |
| 248 | void machine__delete(struct machine *machine) |
| 249 | { |
Arnaldo Carvalho de Melo | 32ca678 | 2016-06-22 10:19:11 -0300 | [diff] [blame] | 250 | if (machine) { |
| 251 | machine__exit(machine); |
| 252 | free(machine); |
| 253 | } |
Arnaldo Carvalho de Melo | 69d2591 | 2012-11-09 11:32:52 -0300 | [diff] [blame] | 254 | } |
| 255 | |
Arnaldo Carvalho de Melo | 876650e6 | 2012-12-18 19:15:48 -0300 | [diff] [blame] | 256 | void machines__init(struct machines *machines) |
| 257 | { |
| 258 | machine__init(&machines->host, "", HOST_KERNEL_ID); |
Davidlohr Bueso | f3acb3a | 2018-12-06 11:18:14 -0800 | [diff] [blame] | 259 | machines->guests = RB_ROOT_CACHED; |
Arnaldo Carvalho de Melo | 876650e6 | 2012-12-18 19:15:48 -0300 | [diff] [blame] | 260 | } |
| 261 | |
| 262 | void machines__exit(struct machines *machines) |
| 263 | { |
| 264 | machine__exit(&machines->host); |
| 265 | /* XXX exit guest */ |
| 266 | } |
| 267 | |
| 268 | struct machine *machines__add(struct machines *machines, pid_t pid, |
Arnaldo Carvalho de Melo | 69d2591 | 2012-11-09 11:32:52 -0300 | [diff] [blame] | 269 | const char *root_dir) |
| 270 | { |
Davidlohr Bueso | f3acb3a | 2018-12-06 11:18:14 -0800 | [diff] [blame] | 271 | struct rb_node **p = &machines->guests.rb_root.rb_node; |
Arnaldo Carvalho de Melo | 69d2591 | 2012-11-09 11:32:52 -0300 | [diff] [blame] | 272 | struct rb_node *parent = NULL; |
| 273 | struct machine *pos, *machine = malloc(sizeof(*machine)); |
Davidlohr Bueso | f3acb3a | 2018-12-06 11:18:14 -0800 | [diff] [blame] | 274 | bool leftmost = true; |
Arnaldo Carvalho de Melo | 69d2591 | 2012-11-09 11:32:52 -0300 | [diff] [blame] | 275 | |
| 276 | if (machine == NULL) |
| 277 | return NULL; |
| 278 | |
| 279 | if (machine__init(machine, root_dir, pid) != 0) { |
| 280 | free(machine); |
| 281 | return NULL; |
| 282 | } |
| 283 | |
| 284 | while (*p != NULL) { |
| 285 | parent = *p; |
| 286 | pos = rb_entry(parent, struct machine, rb_node); |
| 287 | if (pid < pos->pid) |
| 288 | p = &(*p)->rb_left; |
Davidlohr Bueso | f3acb3a | 2018-12-06 11:18:14 -0800 | [diff] [blame] | 289 | else { |
Arnaldo Carvalho de Melo | 69d2591 | 2012-11-09 11:32:52 -0300 | [diff] [blame] | 290 | p = &(*p)->rb_right; |
Davidlohr Bueso | f3acb3a | 2018-12-06 11:18:14 -0800 | [diff] [blame] | 291 | leftmost = false; |
| 292 | } |
Arnaldo Carvalho de Melo | 69d2591 | 2012-11-09 11:32:52 -0300 | [diff] [blame] | 293 | } |
| 294 | |
| 295 | rb_link_node(&machine->rb_node, parent, p); |
Davidlohr Bueso | f3acb3a | 2018-12-06 11:18:14 -0800 | [diff] [blame] | 296 | rb_insert_color_cached(&machine->rb_node, &machines->guests, leftmost); |
Arnaldo Carvalho de Melo | 69d2591 | 2012-11-09 11:32:52 -0300 | [diff] [blame] | 297 | |
| 298 | return machine; |
| 299 | } |
| 300 | |
Adrian Hunter | cfe1c41 | 2014-07-31 09:00:45 +0300 | [diff] [blame] | 301 | void machines__set_comm_exec(struct machines *machines, bool comm_exec) |
| 302 | { |
| 303 | struct rb_node *nd; |
| 304 | |
| 305 | machines->host.comm_exec = comm_exec; |
| 306 | |
Davidlohr Bueso | f3acb3a | 2018-12-06 11:18:14 -0800 | [diff] [blame] | 307 | for (nd = rb_first_cached(&machines->guests); nd; nd = rb_next(nd)) { |
Adrian Hunter | cfe1c41 | 2014-07-31 09:00:45 +0300 | [diff] [blame] | 308 | struct machine *machine = rb_entry(nd, struct machine, rb_node); |
| 309 | |
| 310 | machine->comm_exec = comm_exec; |
| 311 | } |
| 312 | } |
| 313 | |
Arnaldo Carvalho de Melo | 876650e6 | 2012-12-18 19:15:48 -0300 | [diff] [blame] | 314 | struct machine *machines__find(struct machines *machines, pid_t pid) |
Arnaldo Carvalho de Melo | 69d2591 | 2012-11-09 11:32:52 -0300 | [diff] [blame] | 315 | { |
Davidlohr Bueso | f3acb3a | 2018-12-06 11:18:14 -0800 | [diff] [blame] | 316 | struct rb_node **p = &machines->guests.rb_root.rb_node; |
Arnaldo Carvalho de Melo | 69d2591 | 2012-11-09 11:32:52 -0300 | [diff] [blame] | 317 | struct rb_node *parent = NULL; |
| 318 | struct machine *machine; |
| 319 | struct machine *default_machine = NULL; |
| 320 | |
Arnaldo Carvalho de Melo | 876650e6 | 2012-12-18 19:15:48 -0300 | [diff] [blame] | 321 | if (pid == HOST_KERNEL_ID) |
| 322 | return &machines->host; |
| 323 | |
Arnaldo Carvalho de Melo | 69d2591 | 2012-11-09 11:32:52 -0300 | [diff] [blame] | 324 | while (*p != NULL) { |
| 325 | parent = *p; |
| 326 | machine = rb_entry(parent, struct machine, rb_node); |
| 327 | if (pid < machine->pid) |
| 328 | p = &(*p)->rb_left; |
| 329 | else if (pid > machine->pid) |
| 330 | p = &(*p)->rb_right; |
| 331 | else |
| 332 | return machine; |
| 333 | if (!machine->pid) |
| 334 | default_machine = machine; |
| 335 | } |
| 336 | |
| 337 | return default_machine; |
| 338 | } |
| 339 | |
Arnaldo Carvalho de Melo | 876650e6 | 2012-12-18 19:15:48 -0300 | [diff] [blame] | 340 | struct machine *machines__findnew(struct machines *machines, pid_t pid) |
Arnaldo Carvalho de Melo | 69d2591 | 2012-11-09 11:32:52 -0300 | [diff] [blame] | 341 | { |
| 342 | char path[PATH_MAX]; |
| 343 | const char *root_dir = ""; |
| 344 | struct machine *machine = machines__find(machines, pid); |
| 345 | |
| 346 | if (machine && (machine->pid == pid)) |
| 347 | goto out; |
| 348 | |
| 349 | if ((pid != HOST_KERNEL_ID) && |
| 350 | (pid != DEFAULT_GUEST_KERNEL_ID) && |
| 351 | (symbol_conf.guestmount)) { |
| 352 | sprintf(path, "%s/%d", symbol_conf.guestmount, pid); |
| 353 | if (access(path, R_OK)) { |
| 354 | static struct strlist *seen; |
| 355 | |
| 356 | if (!seen) |
Arnaldo Carvalho de Melo | 4a77e21 | 2015-07-20 12:13:34 -0300 | [diff] [blame] | 357 | seen = strlist__new(NULL, NULL); |
Arnaldo Carvalho de Melo | 69d2591 | 2012-11-09 11:32:52 -0300 | [diff] [blame] | 358 | |
| 359 | if (!strlist__has_entry(seen, path)) { |
| 360 | pr_err("Can't access file %s\n", path); |
| 361 | strlist__add(seen, path); |
| 362 | } |
| 363 | machine = NULL; |
| 364 | goto out; |
| 365 | } |
| 366 | root_dir = path; |
| 367 | } |
| 368 | |
| 369 | machine = machines__add(machines, pid, root_dir); |
| 370 | out: |
| 371 | return machine; |
| 372 | } |
| 373 | |
Adrian Hunter | fcda5ff | 2021-02-18 11:57:55 +0200 | [diff] [blame] | 374 | struct machine *machines__find_guest(struct machines *machines, pid_t pid) |
| 375 | { |
| 376 | struct machine *machine = machines__find(machines, pid); |
| 377 | |
| 378 | if (!machine) |
| 379 | machine = machines__findnew(machines, DEFAULT_GUEST_KERNEL_ID); |
| 380 | return machine; |
| 381 | } |
| 382 | |
Arnaldo Carvalho de Melo | 876650e6 | 2012-12-18 19:15:48 -0300 | [diff] [blame] | 383 | void machines__process_guests(struct machines *machines, |
| 384 | machine__process_t process, void *data) |
Arnaldo Carvalho de Melo | 69d2591 | 2012-11-09 11:32:52 -0300 | [diff] [blame] | 385 | { |
| 386 | struct rb_node *nd; |
| 387 | |
Davidlohr Bueso | f3acb3a | 2018-12-06 11:18:14 -0800 | [diff] [blame] | 388 | for (nd = rb_first_cached(&machines->guests); nd; nd = rb_next(nd)) { |
Arnaldo Carvalho de Melo | 69d2591 | 2012-11-09 11:32:52 -0300 | [diff] [blame] | 389 | struct machine *pos = rb_entry(nd, struct machine, rb_node); |
| 390 | process(pos, data); |
| 391 | } |
| 392 | } |
| 393 | |
Arnaldo Carvalho de Melo | 876650e6 | 2012-12-18 19:15:48 -0300 | [diff] [blame] | 394 | void machines__set_id_hdr_size(struct machines *machines, u16 id_hdr_size) |
Arnaldo Carvalho de Melo | 69d2591 | 2012-11-09 11:32:52 -0300 | [diff] [blame] | 395 | { |
| 396 | struct rb_node *node; |
| 397 | struct machine *machine; |
| 398 | |
Arnaldo Carvalho de Melo | 876650e6 | 2012-12-18 19:15:48 -0300 | [diff] [blame] | 399 | machines->host.id_hdr_size = id_hdr_size; |
| 400 | |
Davidlohr Bueso | f3acb3a | 2018-12-06 11:18:14 -0800 | [diff] [blame] | 401 | for (node = rb_first_cached(&machines->guests); node; |
| 402 | node = rb_next(node)) { |
Arnaldo Carvalho de Melo | 69d2591 | 2012-11-09 11:32:52 -0300 | [diff] [blame] | 403 | machine = rb_entry(node, struct machine, rb_node); |
| 404 | machine->id_hdr_size = id_hdr_size; |
| 405 | } |
| 406 | |
| 407 | return; |
| 408 | } |
| 409 | |
Adrian Hunter | 29ce361 | 2014-07-16 11:07:13 +0300 | [diff] [blame] | 410 | static void machine__update_thread_pid(struct machine *machine, |
| 411 | struct thread *th, pid_t pid) |
| 412 | { |
| 413 | struct thread *leader; |
| 414 | |
| 415 | if (pid == th->pid_ || pid == -1 || th->pid_ != -1) |
| 416 | return; |
| 417 | |
| 418 | th->pid_ = pid; |
| 419 | |
| 420 | if (th->pid_ == th->tid) |
| 421 | return; |
| 422 | |
Arnaldo Carvalho de Melo | b91fc39 | 2015-04-06 20:43:22 -0300 | [diff] [blame] | 423 | leader = __machine__findnew_thread(machine, th->pid_, th->pid_); |
Adrian Hunter | 29ce361 | 2014-07-16 11:07:13 +0300 | [diff] [blame] | 424 | if (!leader) |
| 425 | goto out_err; |
| 426 | |
Arnaldo Carvalho de Melo | fe87797d | 2019-11-25 22:07:43 -0300 | [diff] [blame] | 427 | if (!leader->maps) |
| 428 | leader->maps = maps__new(machine); |
Adrian Hunter | 29ce361 | 2014-07-16 11:07:13 +0300 | [diff] [blame] | 429 | |
Arnaldo Carvalho de Melo | fe87797d | 2019-11-25 22:07:43 -0300 | [diff] [blame] | 430 | if (!leader->maps) |
Adrian Hunter | 29ce361 | 2014-07-16 11:07:13 +0300 | [diff] [blame] | 431 | goto out_err; |
| 432 | |
Arnaldo Carvalho de Melo | fe87797d | 2019-11-25 22:07:43 -0300 | [diff] [blame] | 433 | if (th->maps == leader->maps) |
Adrian Hunter | 29ce361 | 2014-07-16 11:07:13 +0300 | [diff] [blame] | 434 | return; |
| 435 | |
Arnaldo Carvalho de Melo | fe87797d | 2019-11-25 22:07:43 -0300 | [diff] [blame] | 436 | if (th->maps) { |
Adrian Hunter | 29ce361 | 2014-07-16 11:07:13 +0300 | [diff] [blame] | 437 | /* |
| 438 | * Maps are created from MMAP events which provide the pid and |
| 439 | * tid. Consequently there never should be any maps on a thread |
| 440 | * with an unknown pid. Just print an error if there are. |
| 441 | */ |
Arnaldo Carvalho de Melo | fe87797d | 2019-11-25 22:07:43 -0300 | [diff] [blame] | 442 | if (!maps__empty(th->maps)) |
Adrian Hunter | 29ce361 | 2014-07-16 11:07:13 +0300 | [diff] [blame] | 443 | pr_err("Discarding thread maps for %d:%d\n", |
| 444 | th->pid_, th->tid); |
Arnaldo Carvalho de Melo | fe87797d | 2019-11-25 22:07:43 -0300 | [diff] [blame] | 445 | maps__put(th->maps); |
Adrian Hunter | 29ce361 | 2014-07-16 11:07:13 +0300 | [diff] [blame] | 446 | } |
| 447 | |
Arnaldo Carvalho de Melo | fe87797d | 2019-11-25 22:07:43 -0300 | [diff] [blame] | 448 | th->maps = maps__get(leader->maps); |
Arnaldo Carvalho de Melo | abd8286 | 2015-12-11 19:11:23 -0300 | [diff] [blame] | 449 | out_put: |
| 450 | thread__put(leader); |
Adrian Hunter | 29ce361 | 2014-07-16 11:07:13 +0300 | [diff] [blame] | 451 | return; |
Adrian Hunter | 29ce361 | 2014-07-16 11:07:13 +0300 | [diff] [blame] | 452 | out_err: |
| 453 | pr_err("Failed to join map groups for %d:%d\n", th->pid_, th->tid); |
Arnaldo Carvalho de Melo | abd8286 | 2015-12-11 19:11:23 -0300 | [diff] [blame] | 454 | goto out_put; |
Adrian Hunter | 29ce361 | 2014-07-16 11:07:13 +0300 | [diff] [blame] | 455 | } |
| 456 | |
Arnaldo Carvalho de Melo | abd8286 | 2015-12-11 19:11:23 -0300 | [diff] [blame] | 457 | /* |
Jiri Olsa | f8b2ebb | 2018-07-19 16:33:42 +0200 | [diff] [blame] | 458 | * Front-end cache - TID lookups come in blocks, |
| 459 | * so most of the time we dont have to look up |
| 460 | * the full rbtree: |
| 461 | */ |
| 462 | static struct thread* |
Jiri Olsa | b57334b | 2018-07-19 16:33:44 +0200 | [diff] [blame] | 463 | __threads__get_last_match(struct threads *threads, struct machine *machine, |
| 464 | int pid, int tid) |
Jiri Olsa | f8b2ebb | 2018-07-19 16:33:42 +0200 | [diff] [blame] | 465 | { |
| 466 | struct thread *th; |
| 467 | |
| 468 | th = threads->last_match; |
| 469 | if (th != NULL) { |
| 470 | if (th->tid == tid) { |
| 471 | machine__update_thread_pid(machine, th, pid); |
| 472 | return thread__get(th); |
| 473 | } |
| 474 | |
| 475 | threads->last_match = NULL; |
| 476 | } |
| 477 | |
| 478 | return NULL; |
| 479 | } |
| 480 | |
Jiri Olsa | b57334b | 2018-07-19 16:33:44 +0200 | [diff] [blame] | 481 | static struct thread* |
| 482 | threads__get_last_match(struct threads *threads, struct machine *machine, |
| 483 | int pid, int tid) |
| 484 | { |
| 485 | struct thread *th = NULL; |
| 486 | |
| 487 | if (perf_singlethreaded) |
| 488 | th = __threads__get_last_match(threads, machine, pid, tid); |
| 489 | |
| 490 | return th; |
| 491 | } |
| 492 | |
| 493 | static void |
| 494 | __threads__set_last_match(struct threads *threads, struct thread *th) |
| 495 | { |
| 496 | threads->last_match = th; |
| 497 | } |
| 498 | |
Jiri Olsa | 67fda0f | 2018-07-19 16:33:43 +0200 | [diff] [blame] | 499 | static void |
| 500 | threads__set_last_match(struct threads *threads, struct thread *th) |
| 501 | { |
Jiri Olsa | b57334b | 2018-07-19 16:33:44 +0200 | [diff] [blame] | 502 | if (perf_singlethreaded) |
| 503 | __threads__set_last_match(threads, th); |
Jiri Olsa | 67fda0f | 2018-07-19 16:33:43 +0200 | [diff] [blame] | 504 | } |
| 505 | |
Jiri Olsa | f8b2ebb | 2018-07-19 16:33:42 +0200 | [diff] [blame] | 506 | /* |
Adam Buchbinder | bd1a0be5 | 2016-02-24 10:02:25 -0800 | [diff] [blame] | 507 | * Caller must eventually drop thread->refcnt returned with a successful |
Arnaldo Carvalho de Melo | abd8286 | 2015-12-11 19:11:23 -0300 | [diff] [blame] | 508 | * lookup/new thread inserted. |
| 509 | */ |
Arnaldo Carvalho de Melo | b91fc39 | 2015-04-06 20:43:22 -0300 | [diff] [blame] | 510 | static struct thread *____machine__findnew_thread(struct machine *machine, |
Arnaldo Carvalho de Melo | 75e45e4 | 2017-09-14 16:16:34 -0300 | [diff] [blame] | 511 | struct threads *threads, |
Arnaldo Carvalho de Melo | b91fc39 | 2015-04-06 20:43:22 -0300 | [diff] [blame] | 512 | pid_t pid, pid_t tid, |
| 513 | bool create) |
Arnaldo Carvalho de Melo | 9d2f8e2 | 2012-10-06 15:43:20 -0300 | [diff] [blame] | 514 | { |
Davidlohr Bueso | f3acb3a | 2018-12-06 11:18:14 -0800 | [diff] [blame] | 515 | struct rb_node **p = &threads->entries.rb_root.rb_node; |
Arnaldo Carvalho de Melo | 9d2f8e2 | 2012-10-06 15:43:20 -0300 | [diff] [blame] | 516 | struct rb_node *parent = NULL; |
| 517 | struct thread *th; |
Davidlohr Bueso | f3acb3a | 2018-12-06 11:18:14 -0800 | [diff] [blame] | 518 | bool leftmost = true; |
Arnaldo Carvalho de Melo | 9d2f8e2 | 2012-10-06 15:43:20 -0300 | [diff] [blame] | 519 | |
Jiri Olsa | f8b2ebb | 2018-07-19 16:33:42 +0200 | [diff] [blame] | 520 | th = threads__get_last_match(threads, machine, pid, tid); |
| 521 | if (th) |
| 522 | return th; |
Arnaldo Carvalho de Melo | 9d2f8e2 | 2012-10-06 15:43:20 -0300 | [diff] [blame] | 523 | |
| 524 | while (*p != NULL) { |
| 525 | parent = *p; |
| 526 | th = rb_entry(parent, struct thread, rb_node); |
| 527 | |
Adrian Hunter | 3805123 | 2013-07-04 16:20:31 +0300 | [diff] [blame] | 528 | if (th->tid == tid) { |
Jiri Olsa | 67fda0f | 2018-07-19 16:33:43 +0200 | [diff] [blame] | 529 | threads__set_last_match(threads, th); |
Adrian Hunter | 29ce361 | 2014-07-16 11:07:13 +0300 | [diff] [blame] | 530 | machine__update_thread_pid(machine, th, pid); |
Arnaldo Carvalho de Melo | abd8286 | 2015-12-11 19:11:23 -0300 | [diff] [blame] | 531 | return thread__get(th); |
Arnaldo Carvalho de Melo | 9d2f8e2 | 2012-10-06 15:43:20 -0300 | [diff] [blame] | 532 | } |
| 533 | |
Adrian Hunter | 3805123 | 2013-07-04 16:20:31 +0300 | [diff] [blame] | 534 | if (tid < th->tid) |
Arnaldo Carvalho de Melo | 9d2f8e2 | 2012-10-06 15:43:20 -0300 | [diff] [blame] | 535 | p = &(*p)->rb_left; |
Davidlohr Bueso | f3acb3a | 2018-12-06 11:18:14 -0800 | [diff] [blame] | 536 | else { |
Arnaldo Carvalho de Melo | 9d2f8e2 | 2012-10-06 15:43:20 -0300 | [diff] [blame] | 537 | p = &(*p)->rb_right; |
Davidlohr Bueso | f3acb3a | 2018-12-06 11:18:14 -0800 | [diff] [blame] | 538 | leftmost = false; |
| 539 | } |
Arnaldo Carvalho de Melo | 9d2f8e2 | 2012-10-06 15:43:20 -0300 | [diff] [blame] | 540 | } |
| 541 | |
| 542 | if (!create) |
| 543 | return NULL; |
| 544 | |
Adrian Hunter | 99d725f | 2013-08-26 16:00:19 +0300 | [diff] [blame] | 545 | th = thread__new(pid, tid); |
Arnaldo Carvalho de Melo | 9d2f8e2 | 2012-10-06 15:43:20 -0300 | [diff] [blame] | 546 | if (th != NULL) { |
| 547 | rb_link_node(&th->rb_node, parent, p); |
Davidlohr Bueso | f3acb3a | 2018-12-06 11:18:14 -0800 | [diff] [blame] | 548 | rb_insert_color_cached(&th->rb_node, &threads->entries, leftmost); |
Jiri Olsa | cddcef6 | 2014-04-09 20:54:29 +0200 | [diff] [blame] | 549 | |
| 550 | /* |
Arnaldo Carvalho de Melo | 79b6bb7 | 2019-11-25 21:58:33 -0300 | [diff] [blame] | 551 | * We have to initialize maps separately after rb tree is updated. |
Jiri Olsa | cddcef6 | 2014-04-09 20:54:29 +0200 | [diff] [blame] | 552 | * |
| 553 | * The reason is that we call machine__findnew_thread |
Arnaldo Carvalho de Melo | 79b6bb7 | 2019-11-25 21:58:33 -0300 | [diff] [blame] | 554 | * within thread__init_maps to find the thread |
Jiri Olsa | cddcef6 | 2014-04-09 20:54:29 +0200 | [diff] [blame] | 555 | * leader and that would screwed the rb tree. |
| 556 | */ |
Arnaldo Carvalho de Melo | 79b6bb7 | 2019-11-25 21:58:33 -0300 | [diff] [blame] | 557 | if (thread__init_maps(th, machine)) { |
Davidlohr Bueso | f3acb3a | 2018-12-06 11:18:14 -0800 | [diff] [blame] | 558 | rb_erase_cached(&th->rb_node, &threads->entries); |
Arnaldo Carvalho de Melo | b91fc39 | 2015-04-06 20:43:22 -0300 | [diff] [blame] | 559 | RB_CLEAR_NODE(&th->rb_node); |
Arnaldo Carvalho de Melo | abd8286 | 2015-12-11 19:11:23 -0300 | [diff] [blame] | 560 | thread__put(th); |
Jiri Olsa | cddcef6 | 2014-04-09 20:54:29 +0200 | [diff] [blame] | 561 | return NULL; |
Adrian Hunter | 418029b | 2014-07-16 10:19:44 +0300 | [diff] [blame] | 562 | } |
Arnaldo Carvalho de Melo | f3b623b | 2015-03-02 22:21:35 -0300 | [diff] [blame] | 563 | /* |
| 564 | * It is now in the rbtree, get a ref |
| 565 | */ |
| 566 | thread__get(th); |
Jiri Olsa | 67fda0f | 2018-07-19 16:33:43 +0200 | [diff] [blame] | 567 | threads__set_last_match(threads, th); |
Kan Liang | 91e467b | 2017-09-10 19:23:14 -0700 | [diff] [blame] | 568 | ++threads->nr; |
Arnaldo Carvalho de Melo | 9d2f8e2 | 2012-10-06 15:43:20 -0300 | [diff] [blame] | 569 | } |
| 570 | |
| 571 | return th; |
| 572 | } |
| 573 | |
Arnaldo Carvalho de Melo | b91fc39 | 2015-04-06 20:43:22 -0300 | [diff] [blame] | 574 | struct thread *__machine__findnew_thread(struct machine *machine, pid_t pid, pid_t tid) |
| 575 | { |
Arnaldo Carvalho de Melo | 75e45e4 | 2017-09-14 16:16:34 -0300 | [diff] [blame] | 576 | return ____machine__findnew_thread(machine, machine__threads(machine, tid), pid, tid, true); |
Arnaldo Carvalho de Melo | b91fc39 | 2015-04-06 20:43:22 -0300 | [diff] [blame] | 577 | } |
| 578 | |
Adrian Hunter | 314add6 | 2013-08-27 11:23:03 +0300 | [diff] [blame] | 579 | struct thread *machine__findnew_thread(struct machine *machine, pid_t pid, |
| 580 | pid_t tid) |
Arnaldo Carvalho de Melo | 9d2f8e2 | 2012-10-06 15:43:20 -0300 | [diff] [blame] | 581 | { |
Kan Liang | 91e467b | 2017-09-10 19:23:14 -0700 | [diff] [blame] | 582 | struct threads *threads = machine__threads(machine, tid); |
Arnaldo Carvalho de Melo | b91fc39 | 2015-04-06 20:43:22 -0300 | [diff] [blame] | 583 | struct thread *th; |
| 584 | |
Arnaldo Carvalho de Melo | 0a7c74e | 2017-04-04 13:15:04 -0300 | [diff] [blame] | 585 | down_write(&threads->lock); |
Arnaldo Carvalho de Melo | abd8286 | 2015-12-11 19:11:23 -0300 | [diff] [blame] | 586 | th = __machine__findnew_thread(machine, pid, tid); |
Arnaldo Carvalho de Melo | 0a7c74e | 2017-04-04 13:15:04 -0300 | [diff] [blame] | 587 | up_write(&threads->lock); |
Arnaldo Carvalho de Melo | b91fc39 | 2015-04-06 20:43:22 -0300 | [diff] [blame] | 588 | return th; |
Arnaldo Carvalho de Melo | 9d2f8e2 | 2012-10-06 15:43:20 -0300 | [diff] [blame] | 589 | } |
| 590 | |
Jiri Olsa | d75e609 | 2014-03-14 15:00:03 +0100 | [diff] [blame] | 591 | struct thread *machine__find_thread(struct machine *machine, pid_t pid, |
| 592 | pid_t tid) |
Arnaldo Carvalho de Melo | 9d2f8e2 | 2012-10-06 15:43:20 -0300 | [diff] [blame] | 593 | { |
Kan Liang | 91e467b | 2017-09-10 19:23:14 -0700 | [diff] [blame] | 594 | struct threads *threads = machine__threads(machine, tid); |
Arnaldo Carvalho de Melo | b91fc39 | 2015-04-06 20:43:22 -0300 | [diff] [blame] | 595 | struct thread *th; |
Kan Liang | 91e467b | 2017-09-10 19:23:14 -0700 | [diff] [blame] | 596 | |
Arnaldo Carvalho de Melo | 0a7c74e | 2017-04-04 13:15:04 -0300 | [diff] [blame] | 597 | down_read(&threads->lock); |
Arnaldo Carvalho de Melo | 75e45e4 | 2017-09-14 16:16:34 -0300 | [diff] [blame] | 598 | th = ____machine__findnew_thread(machine, threads, pid, tid, false); |
Arnaldo Carvalho de Melo | 0a7c74e | 2017-04-04 13:15:04 -0300 | [diff] [blame] | 599 | up_read(&threads->lock); |
Arnaldo Carvalho de Melo | b91fc39 | 2015-04-06 20:43:22 -0300 | [diff] [blame] | 600 | return th; |
Arnaldo Carvalho de Melo | 9d2f8e2 | 2012-10-06 15:43:20 -0300 | [diff] [blame] | 601 | } |
Arnaldo Carvalho de Melo | b0a7d1a | 2012-10-06 16:26:02 -0300 | [diff] [blame] | 602 | |
Adrian Hunter | 3035cb6 | 2021-02-18 11:57:56 +0200 | [diff] [blame] | 603 | /* |
| 604 | * Threads are identified by pid and tid, and the idle task has pid == tid == 0. |
| 605 | * So here a single thread is created for that, but actually there is a separate |
| 606 | * idle task per cpu, so there should be one 'struct thread' per cpu, but there |
| 607 | * is only 1. That causes problems for some tools, requiring workarounds. For |
| 608 | * example get_idle_thread() in builtin-sched.c, or thread_stack__per_cpu(). |
| 609 | */ |
| 610 | struct thread *machine__idle_thread(struct machine *machine) |
| 611 | { |
| 612 | struct thread *thread = machine__findnew_thread(machine, 0, 0); |
| 613 | |
| 614 | if (!thread || thread__set_comm(thread, "swapper", 0) || |
| 615 | thread__set_namespaces(thread, 0, NULL)) |
| 616 | pr_err("problem inserting idle task for machine pid %d\n", machine->pid); |
| 617 | |
| 618 | return thread; |
| 619 | } |
| 620 | |
Adrian Hunter | cfe1c41 | 2014-07-31 09:00:45 +0300 | [diff] [blame] | 621 | struct comm *machine__thread_exec_comm(struct machine *machine, |
| 622 | struct thread *thread) |
| 623 | { |
| 624 | if (machine->comm_exec) |
| 625 | return thread__exec_comm(thread); |
| 626 | else |
| 627 | return thread__comm(thread); |
| 628 | } |
| 629 | |
Frederic Weisbecker | 162f0be | 2013-09-11 16:18:24 +0200 | [diff] [blame] | 630 | int machine__process_comm_event(struct machine *machine, union perf_event *event, |
| 631 | struct perf_sample *sample) |
Arnaldo Carvalho de Melo | b0a7d1a | 2012-10-06 16:26:02 -0300 | [diff] [blame] | 632 | { |
Adrian Hunter | 314add6 | 2013-08-27 11:23:03 +0300 | [diff] [blame] | 633 | struct thread *thread = machine__findnew_thread(machine, |
| 634 | event->comm.pid, |
| 635 | event->comm.tid); |
Adrian Hunter | 65de51f | 2014-07-31 09:00:44 +0300 | [diff] [blame] | 636 | bool exec = event->header.misc & PERF_RECORD_MISC_COMM_EXEC; |
Arnaldo Carvalho de Melo | b91fc39 | 2015-04-06 20:43:22 -0300 | [diff] [blame] | 637 | int err = 0; |
Arnaldo Carvalho de Melo | b0a7d1a | 2012-10-06 16:26:02 -0300 | [diff] [blame] | 638 | |
Adrian Hunter | cfe1c41 | 2014-07-31 09:00:45 +0300 | [diff] [blame] | 639 | if (exec) |
| 640 | machine->comm_exec = true; |
| 641 | |
Arnaldo Carvalho de Melo | b0a7d1a | 2012-10-06 16:26:02 -0300 | [diff] [blame] | 642 | if (dump_trace) |
| 643 | perf_event__fprintf_comm(event, stdout); |
| 644 | |
Adrian Hunter | 65de51f | 2014-07-31 09:00:44 +0300 | [diff] [blame] | 645 | if (thread == NULL || |
| 646 | __thread__set_comm(thread, event->comm.comm, sample->time, exec)) { |
Arnaldo Carvalho de Melo | b0a7d1a | 2012-10-06 16:26:02 -0300 | [diff] [blame] | 647 | dump_printf("problem processing PERF_RECORD_COMM, skipping event.\n"); |
Arnaldo Carvalho de Melo | b91fc39 | 2015-04-06 20:43:22 -0300 | [diff] [blame] | 648 | err = -1; |
Arnaldo Carvalho de Melo | b0a7d1a | 2012-10-06 16:26:02 -0300 | [diff] [blame] | 649 | } |
| 650 | |
Arnaldo Carvalho de Melo | b91fc39 | 2015-04-06 20:43:22 -0300 | [diff] [blame] | 651 | thread__put(thread); |
| 652 | |
| 653 | return err; |
Arnaldo Carvalho de Melo | b0a7d1a | 2012-10-06 16:26:02 -0300 | [diff] [blame] | 654 | } |
| 655 | |
Hari Bathini | f3b3614 | 2017-03-08 02:11:43 +0530 | [diff] [blame] | 656 | int machine__process_namespaces_event(struct machine *machine __maybe_unused, |
| 657 | union perf_event *event, |
| 658 | struct perf_sample *sample __maybe_unused) |
| 659 | { |
| 660 | struct thread *thread = machine__findnew_thread(machine, |
| 661 | event->namespaces.pid, |
| 662 | event->namespaces.tid); |
| 663 | int err = 0; |
| 664 | |
| 665 | WARN_ONCE(event->namespaces.nr_namespaces > NR_NAMESPACES, |
| 666 | "\nWARNING: kernel seems to support more namespaces than perf" |
| 667 | " tool.\nTry updating the perf tool..\n\n"); |
| 668 | |
| 669 | WARN_ONCE(event->namespaces.nr_namespaces < NR_NAMESPACES, |
| 670 | "\nWARNING: perf tool seems to support more namespaces than" |
| 671 | " the kernel.\nTry updating the kernel..\n\n"); |
| 672 | |
| 673 | if (dump_trace) |
| 674 | perf_event__fprintf_namespaces(event, stdout); |
| 675 | |
| 676 | if (thread == NULL || |
| 677 | thread__set_namespaces(thread, sample->time, &event->namespaces)) { |
| 678 | dump_printf("problem processing PERF_RECORD_NAMESPACES, skipping event.\n"); |
| 679 | err = -1; |
| 680 | } |
| 681 | |
| 682 | thread__put(thread); |
| 683 | |
| 684 | return err; |
| 685 | } |
| 686 | |
Namhyung Kim | d1277aa | 2020-03-25 21:45:31 +0900 | [diff] [blame] | 687 | int machine__process_cgroup_event(struct machine *machine, |
Namhyung Kim | ba78c1c | 2020-03-25 21:45:30 +0900 | [diff] [blame] | 688 | union perf_event *event, |
| 689 | struct perf_sample *sample __maybe_unused) |
| 690 | { |
Namhyung Kim | d1277aa | 2020-03-25 21:45:31 +0900 | [diff] [blame] | 691 | struct cgroup *cgrp; |
| 692 | |
Namhyung Kim | ba78c1c | 2020-03-25 21:45:30 +0900 | [diff] [blame] | 693 | if (dump_trace) |
| 694 | perf_event__fprintf_cgroup(event, stdout); |
| 695 | |
Namhyung Kim | d1277aa | 2020-03-25 21:45:31 +0900 | [diff] [blame] | 696 | cgrp = cgroup__findnew(machine->env, event->cgroup.id, event->cgroup.path); |
| 697 | if (cgrp == NULL) |
| 698 | return -ENOMEM; |
| 699 | |
Namhyung Kim | ba78c1c | 2020-03-25 21:45:30 +0900 | [diff] [blame] | 700 | return 0; |
| 701 | } |
| 702 | |
Arnaldo Carvalho de Melo | b0a7d1a | 2012-10-06 16:26:02 -0300 | [diff] [blame] | 703 | int machine__process_lost_event(struct machine *machine __maybe_unused, |
Frederic Weisbecker | 162f0be | 2013-09-11 16:18:24 +0200 | [diff] [blame] | 704 | union perf_event *event, struct perf_sample *sample __maybe_unused) |
Arnaldo Carvalho de Melo | b0a7d1a | 2012-10-06 16:26:02 -0300 | [diff] [blame] | 705 | { |
Jiri Olsa | 5290ed6 | 2019-08-25 20:17:46 +0200 | [diff] [blame] | 706 | dump_printf(": id:%" PRI_lu64 ": lost:%" PRI_lu64 "\n", |
Arnaldo Carvalho de Melo | b0a7d1a | 2012-10-06 16:26:02 -0300 | [diff] [blame] | 707 | event->lost.id, event->lost.lost); |
| 708 | return 0; |
| 709 | } |
| 710 | |
Kan Liang | c4937a9 | 2015-05-10 15:13:15 -0400 | [diff] [blame] | 711 | int machine__process_lost_samples_event(struct machine *machine __maybe_unused, |
| 712 | union perf_event *event, struct perf_sample *sample) |
| 713 | { |
Jiri Olsa | a2e254d | 2019-08-25 20:17:47 +0200 | [diff] [blame] | 714 | dump_printf(": id:%" PRIu64 ": lost samples :%" PRI_lu64 "\n", |
Kan Liang | c4937a9 | 2015-05-10 15:13:15 -0400 | [diff] [blame] | 715 | sample->id, event->lost_samples.lost); |
| 716 | return 0; |
| 717 | } |
| 718 | |
Arnaldo Carvalho de Melo | 9f2de31 | 2015-06-01 12:01:02 -0300 | [diff] [blame] | 719 | static struct dso *machine__findnew_module_dso(struct machine *machine, |
| 720 | struct kmod_path *m, |
| 721 | const char *filename) |
Jiri Olsa | da17ea3 | 2015-02-12 22:10:52 +0100 | [diff] [blame] | 722 | { |
| 723 | struct dso *dso; |
Jiri Olsa | da17ea3 | 2015-02-12 22:10:52 +0100 | [diff] [blame] | 724 | |
Arnaldo Carvalho de Melo | 0a7c74e | 2017-04-04 13:15:04 -0300 | [diff] [blame] | 725 | down_write(&machine->dsos.lock); |
Arnaldo Carvalho de Melo | e880784 | 2015-06-01 15:40:01 -0300 | [diff] [blame] | 726 | |
| 727 | dso = __dsos__find(&machine->dsos, m->name, true); |
Jiri Olsa | da17ea3 | 2015-02-12 22:10:52 +0100 | [diff] [blame] | 728 | if (!dso) { |
Arnaldo Carvalho de Melo | e880784 | 2015-06-01 15:40:01 -0300 | [diff] [blame] | 729 | dso = __dsos__addnew(&machine->dsos, m->name); |
Jiri Olsa | da17ea3 | 2015-02-12 22:10:52 +0100 | [diff] [blame] | 730 | if (dso == NULL) |
Arnaldo Carvalho de Melo | e880784 | 2015-06-01 15:40:01 -0300 | [diff] [blame] | 731 | goto out_unlock; |
Jiri Olsa | da17ea3 | 2015-02-12 22:10:52 +0100 | [diff] [blame] | 732 | |
Namhyung Kim | 6b335e8 | 2017-05-31 21:01:04 +0900 | [diff] [blame] | 733 | dso__set_module_info(dso, m, machine); |
Jiri Olsa | ca33380 | 2015-02-17 17:29:57 +0100 | [diff] [blame] | 734 | dso__set_long_name(dso, strdup(filename), true); |
Jiri Olsa | 1c695c8 | 2020-08-08 14:21:54 +0200 | [diff] [blame] | 735 | dso->kernel = DSO_SPACE__KERNEL; |
Jiri Olsa | da17ea3 | 2015-02-12 22:10:52 +0100 | [diff] [blame] | 736 | } |
| 737 | |
Arnaldo Carvalho de Melo | d3a7c48 | 2015-06-02 11:53:26 -0300 | [diff] [blame] | 738 | dso__get(dso); |
Arnaldo Carvalho de Melo | e880784 | 2015-06-01 15:40:01 -0300 | [diff] [blame] | 739 | out_unlock: |
Arnaldo Carvalho de Melo | 0a7c74e | 2017-04-04 13:15:04 -0300 | [diff] [blame] | 740 | up_write(&machine->dsos.lock); |
Jiri Olsa | da17ea3 | 2015-02-12 22:10:52 +0100 | [diff] [blame] | 741 | return dso; |
| 742 | } |
| 743 | |
Adrian Hunter | 4a96f7a | 2015-04-30 17:37:29 +0300 | [diff] [blame] | 744 | int machine__process_aux_event(struct machine *machine __maybe_unused, |
| 745 | union perf_event *event) |
| 746 | { |
| 747 | if (dump_trace) |
| 748 | perf_event__fprintf_aux(event, stdout); |
| 749 | return 0; |
| 750 | } |
| 751 | |
Adrian Hunter | 0ad21f6 | 2015-04-30 17:37:30 +0300 | [diff] [blame] | 752 | int machine__process_itrace_start_event(struct machine *machine __maybe_unused, |
| 753 | union perf_event *event) |
| 754 | { |
| 755 | if (dump_trace) |
| 756 | perf_event__fprintf_itrace_start(event, stdout); |
| 757 | return 0; |
| 758 | } |
| 759 | |
Adrian Hunter | 6175047 | 2021-09-07 19:39:02 +0300 | [diff] [blame] | 760 | int machine__process_aux_output_hw_id_event(struct machine *machine __maybe_unused, |
| 761 | union perf_event *event) |
| 762 | { |
| 763 | if (dump_trace) |
| 764 | perf_event__fprintf_aux_output_hw_id(event, stdout); |
| 765 | return 0; |
| 766 | } |
| 767 | |
Adrian Hunter | 0286039 | 2015-07-21 12:44:03 +0300 | [diff] [blame] | 768 | int machine__process_switch_event(struct machine *machine __maybe_unused, |
| 769 | union perf_event *event) |
| 770 | { |
| 771 | if (dump_trace) |
| 772 | perf_event__fprintf_switch(event, stdout); |
| 773 | return 0; |
| 774 | } |
| 775 | |
Song Liu | 9aa0bfa | 2019-01-17 08:15:17 -0800 | [diff] [blame] | 776 | static int machine__process_ksymbol_register(struct machine *machine, |
| 777 | union perf_event *event, |
| 778 | struct perf_sample *sample __maybe_unused) |
| 779 | { |
| 780 | struct symbol *sym; |
Arnaldo Carvalho de Melo | 79b6bb7 | 2019-11-25 21:58:33 -0300 | [diff] [blame] | 781 | struct map *map = maps__find(&machine->kmaps, event->ksymbol.addr); |
Song Liu | 9aa0bfa | 2019-01-17 08:15:17 -0800 | [diff] [blame] | 782 | |
Song Liu | 9aa0bfa | 2019-01-17 08:15:17 -0800 | [diff] [blame] | 783 | if (!map) { |
Jiri Olsa | 4a4eb61 | 2020-02-10 21:08:47 +0100 | [diff] [blame] | 784 | struct dso *dso = dso__new(event->ksymbol.name); |
| 785 | |
| 786 | if (dso) { |
Jiri Olsa | 1c695c8 | 2020-08-08 14:21:54 +0200 | [diff] [blame] | 787 | dso->kernel = DSO_SPACE__KERNEL; |
Jiri Olsa | 4a4eb61 | 2020-02-10 21:08:47 +0100 | [diff] [blame] | 788 | map = map__new2(0, dso); |
Riccardo Mancini | c087e94 | 2021-06-12 19:37:48 +0200 | [diff] [blame] | 789 | dso__put(dso); |
Jiri Olsa | 4a4eb61 | 2020-02-10 21:08:47 +0100 | [diff] [blame] | 790 | } |
| 791 | |
| 792 | if (!dso || !map) { |
Song Liu | 9aa0bfa | 2019-01-17 08:15:17 -0800 | [diff] [blame] | 793 | return -ENOMEM; |
Jiri Olsa | 4a4eb61 | 2020-02-10 21:08:47 +0100 | [diff] [blame] | 794 | } |
Song Liu | 9aa0bfa | 2019-01-17 08:15:17 -0800 | [diff] [blame] | 795 | |
Adrian Hunter | 789e241 | 2020-05-12 15:19:19 +0300 | [diff] [blame] | 796 | if (event->ksymbol.ksym_type == PERF_RECORD_KSYMBOL_TYPE_OOL) { |
| 797 | map->dso->binary_type = DSO_BINARY_TYPE__OOL; |
| 798 | map->dso->data.file_size = event->ksymbol.len; |
| 799 | dso__set_loaded(map->dso); |
| 800 | } |
| 801 | |
Arnaldo Carvalho de Melo | ebdba16 | 2019-08-26 19:15:18 -0300 | [diff] [blame] | 802 | map->start = event->ksymbol.addr; |
| 803 | map->end = map->start + event->ksymbol.len; |
Arnaldo Carvalho de Melo | 79b6bb7 | 2019-11-25 21:58:33 -0300 | [diff] [blame] | 804 | maps__insert(&machine->kmaps, map); |
Riccardo Mancini | c087e94 | 2021-06-12 19:37:48 +0200 | [diff] [blame] | 805 | map__put(map); |
Jiri Olsa | 7eddf7e | 2020-03-12 20:56:09 +0100 | [diff] [blame] | 806 | dso__set_loaded(dso); |
Jiri Olsa | 3c29d44 | 2020-03-12 20:56:10 +0100 | [diff] [blame] | 807 | |
| 808 | if (is_bpf_image(event->ksymbol.name)) { |
| 809 | dso->binary_type = DSO_BINARY_TYPE__BPF_IMAGE; |
| 810 | dso__set_long_name(dso, "", false); |
| 811 | } |
Song Liu | 9aa0bfa | 2019-01-17 08:15:17 -0800 | [diff] [blame] | 812 | } |
| 813 | |
Jiri Olsa | 8529f2e | 2019-05-08 15:20:04 +0200 | [diff] [blame] | 814 | sym = symbol__new(map->map_ip(map, map->start), |
Arnaldo Carvalho de Melo | ebdba16 | 2019-08-26 19:15:18 -0300 | [diff] [blame] | 815 | event->ksymbol.len, |
| 816 | 0, 0, event->ksymbol.name); |
Song Liu | 9aa0bfa | 2019-01-17 08:15:17 -0800 | [diff] [blame] | 817 | if (!sym) |
| 818 | return -ENOMEM; |
| 819 | dso__insert_symbol(map->dso, sym); |
| 820 | return 0; |
| 821 | } |
| 822 | |
| 823 | static int machine__process_ksymbol_unregister(struct machine *machine, |
| 824 | union perf_event *event, |
| 825 | struct perf_sample *sample __maybe_unused) |
| 826 | { |
Tommi Rantala | ab8bf5f | 2020-10-16 14:47:18 +0300 | [diff] [blame] | 827 | struct symbol *sym; |
Song Liu | 9aa0bfa | 2019-01-17 08:15:17 -0800 | [diff] [blame] | 828 | struct map *map; |
| 829 | |
Arnaldo Carvalho de Melo | 79b6bb7 | 2019-11-25 21:58:33 -0300 | [diff] [blame] | 830 | map = maps__find(&machine->kmaps, event->ksymbol.addr); |
Tommi Rantala | ab8bf5f | 2020-10-16 14:47:18 +0300 | [diff] [blame] | 831 | if (!map) |
| 832 | return 0; |
| 833 | |
| 834 | if (map != machine->vmlinux_map) |
Arnaldo Carvalho de Melo | 79b6bb7 | 2019-11-25 21:58:33 -0300 | [diff] [blame] | 835 | maps__remove(&machine->kmaps, map); |
Tommi Rantala | ab8bf5f | 2020-10-16 14:47:18 +0300 | [diff] [blame] | 836 | else { |
| 837 | sym = dso__find_symbol(map->dso, map->map_ip(map, map->start)); |
| 838 | if (sym) |
| 839 | dso__delete_symbol(map->dso, sym); |
| 840 | } |
Song Liu | 9aa0bfa | 2019-01-17 08:15:17 -0800 | [diff] [blame] | 841 | |
| 842 | return 0; |
| 843 | } |
| 844 | |
| 845 | int machine__process_ksymbol(struct machine *machine __maybe_unused, |
| 846 | union perf_event *event, |
| 847 | struct perf_sample *sample) |
| 848 | { |
| 849 | if (dump_trace) |
| 850 | perf_event__fprintf_ksymbol(event, stdout); |
| 851 | |
Arnaldo Carvalho de Melo | ebdba16 | 2019-08-26 19:15:18 -0300 | [diff] [blame] | 852 | if (event->ksymbol.flags & PERF_RECORD_KSYMBOL_FLAGS_UNREGISTER) |
Song Liu | 9aa0bfa | 2019-01-17 08:15:17 -0800 | [diff] [blame] | 853 | return machine__process_ksymbol_unregister(machine, event, |
| 854 | sample); |
| 855 | return machine__process_ksymbol_register(machine, event, sample); |
| 856 | } |
| 857 | |
Adrian Hunter | 246eba8 | 2020-05-12 15:19:18 +0300 | [diff] [blame] | 858 | int machine__process_text_poke(struct machine *machine, union perf_event *event, |
| 859 | struct perf_sample *sample __maybe_unused) |
| 860 | { |
| 861 | struct map *map = maps__find(&machine->kmaps, event->text_poke.addr); |
| 862 | u8 cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK; |
| 863 | |
| 864 | if (dump_trace) |
Adrian Hunter | 7eeb985 | 2020-05-12 15:19:22 +0300 | [diff] [blame] | 865 | perf_event__fprintf_text_poke(event, machine, stdout); |
Adrian Hunter | 246eba8 | 2020-05-12 15:19:18 +0300 | [diff] [blame] | 866 | |
| 867 | if (!event->text_poke.new_len) |
| 868 | return 0; |
| 869 | |
| 870 | if (cpumode != PERF_RECORD_MISC_KERNEL) { |
| 871 | pr_debug("%s: unsupported cpumode - ignoring\n", __func__); |
| 872 | return 0; |
| 873 | } |
| 874 | |
| 875 | if (map && map->dso) { |
| 876 | u8 *new_bytes = event->text_poke.bytes + event->text_poke.old_len; |
| 877 | int ret; |
| 878 | |
| 879 | /* |
| 880 | * Kernel maps might be changed when loading symbols so loading |
| 881 | * must be done prior to using kernel maps. |
| 882 | */ |
| 883 | map__load(map); |
| 884 | ret = dso__data_write_cache_addr(map->dso, map, machine, |
| 885 | event->text_poke.addr, |
| 886 | new_bytes, |
| 887 | event->text_poke.new_len); |
| 888 | if (ret != event->text_poke.new_len) |
| 889 | pr_debug("Failed to write kernel text poke at %#" PRI_lx64 "\n", |
| 890 | event->text_poke.addr); |
| 891 | } else { |
| 892 | pr_debug("Failed to find kernel text poke address map for %#" PRI_lx64 "\n", |
| 893 | event->text_poke.addr); |
| 894 | } |
| 895 | |
| 896 | return 0; |
| 897 | } |
| 898 | |
Arnaldo Carvalho de Melo | a94ab91 | 2019-11-14 12:28:41 -0300 | [diff] [blame] | 899 | static struct map *machine__addnew_module_map(struct machine *machine, u64 start, |
| 900 | const char *filename) |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 901 | { |
Jiri Olsa | ca33380 | 2015-02-17 17:29:57 +0100 | [diff] [blame] | 902 | struct map *map = NULL; |
Jiri Olsa | ca33380 | 2015-02-17 17:29:57 +0100 | [diff] [blame] | 903 | struct kmod_path m; |
Arnaldo Carvalho de Melo | a94ab91 | 2019-11-14 12:28:41 -0300 | [diff] [blame] | 904 | struct dso *dso; |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 905 | |
Jiri Olsa | ca33380 | 2015-02-17 17:29:57 +0100 | [diff] [blame] | 906 | if (kmod_path__parse_name(&m, filename)) |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 907 | return NULL; |
| 908 | |
Arnaldo Carvalho de Melo | 9f2de31 | 2015-06-01 12:01:02 -0300 | [diff] [blame] | 909 | dso = machine__findnew_module_dso(machine, &m, filename); |
Jiri Olsa | ca33380 | 2015-02-17 17:29:57 +0100 | [diff] [blame] | 910 | if (dso == NULL) |
| 911 | goto out; |
| 912 | |
Arnaldo Carvalho de Melo | 3183f8c | 2018-04-26 16:52:34 -0300 | [diff] [blame] | 913 | map = map__new2(start, dso); |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 914 | if (map == NULL) |
Jiri Olsa | ca33380 | 2015-02-17 17:29:57 +0100 | [diff] [blame] | 915 | goto out; |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 916 | |
Arnaldo Carvalho de Melo | 79b6bb7 | 2019-11-25 21:58:33 -0300 | [diff] [blame] | 917 | maps__insert(&machine->kmaps, map); |
Jiri Olsa | ca33380 | 2015-02-17 17:29:57 +0100 | [diff] [blame] | 918 | |
Ingo Molnar | 4d39c89 | 2021-03-23 17:09:15 +0100 | [diff] [blame] | 919 | /* Put the map here because maps__insert already got it */ |
Masami Hiramatsu | 9afcb420 | 2015-11-18 15:40:20 +0900 | [diff] [blame] | 920 | map__put(map); |
Jiri Olsa | ca33380 | 2015-02-17 17:29:57 +0100 | [diff] [blame] | 921 | out: |
Masami Hiramatsu | 566c69c | 2015-11-18 15:40:35 +0900 | [diff] [blame] | 922 | /* put the dso here, corresponding to machine__findnew_module_dso */ |
| 923 | dso__put(dso); |
Arnaldo Carvalho de Melo | d8f9da2 | 2019-07-04 12:06:20 -0300 | [diff] [blame] | 924 | zfree(&m.name); |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 925 | return map; |
| 926 | } |
| 927 | |
Arnaldo Carvalho de Melo | 876650e6 | 2012-12-18 19:15:48 -0300 | [diff] [blame] | 928 | size_t machines__fprintf_dsos(struct machines *machines, FILE *fp) |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 929 | { |
| 930 | struct rb_node *nd; |
Arnaldo Carvalho de Melo | 3d39ac5 | 2015-05-28 13:06:42 -0300 | [diff] [blame] | 931 | size_t ret = __dsos__fprintf(&machines->host.dsos.head, fp); |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 932 | |
Davidlohr Bueso | f3acb3a | 2018-12-06 11:18:14 -0800 | [diff] [blame] | 933 | for (nd = rb_first_cached(&machines->guests); nd; nd = rb_next(nd)) { |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 934 | struct machine *pos = rb_entry(nd, struct machine, rb_node); |
Arnaldo Carvalho de Melo | 3d39ac5 | 2015-05-28 13:06:42 -0300 | [diff] [blame] | 935 | ret += __dsos__fprintf(&pos->dsos.head, fp); |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 936 | } |
| 937 | |
| 938 | return ret; |
| 939 | } |
| 940 | |
Waiman Long | 8fa7d87 | 2014-09-29 16:07:28 -0400 | [diff] [blame] | 941 | size_t machine__fprintf_dsos_buildid(struct machine *m, FILE *fp, |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 942 | bool (skip)(struct dso *dso, int parm), int parm) |
| 943 | { |
Arnaldo Carvalho de Melo | 3d39ac5 | 2015-05-28 13:06:42 -0300 | [diff] [blame] | 944 | return __dsos__fprintf_buildid(&m->dsos.head, fp, skip, parm); |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 945 | } |
| 946 | |
Arnaldo Carvalho de Melo | 876650e6 | 2012-12-18 19:15:48 -0300 | [diff] [blame] | 947 | size_t machines__fprintf_dsos_buildid(struct machines *machines, FILE *fp, |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 948 | bool (skip)(struct dso *dso, int parm), int parm) |
| 949 | { |
| 950 | struct rb_node *nd; |
Arnaldo Carvalho de Melo | 876650e6 | 2012-12-18 19:15:48 -0300 | [diff] [blame] | 951 | size_t ret = machine__fprintf_dsos_buildid(&machines->host, fp, skip, parm); |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 952 | |
Davidlohr Bueso | f3acb3a | 2018-12-06 11:18:14 -0800 | [diff] [blame] | 953 | for (nd = rb_first_cached(&machines->guests); nd; nd = rb_next(nd)) { |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 954 | struct machine *pos = rb_entry(nd, struct machine, rb_node); |
| 955 | ret += machine__fprintf_dsos_buildid(pos, fp, skip, parm); |
| 956 | } |
| 957 | return ret; |
| 958 | } |
| 959 | |
| 960 | size_t machine__fprintf_vmlinux_path(struct machine *machine, FILE *fp) |
| 961 | { |
| 962 | int i; |
| 963 | size_t printed = 0; |
Arnaldo Carvalho de Melo | 93730f8 | 2019-10-31 15:22:24 -0300 | [diff] [blame] | 964 | struct dso *kdso = machine__kernel_dso(machine); |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 965 | |
| 966 | if (kdso->has_build_id) { |
| 967 | char filename[PATH_MAX]; |
Krister Johansen | d239699 | 2017-07-05 18:48:13 -0700 | [diff] [blame] | 968 | if (dso__build_id_filename(kdso, filename, sizeof(filename), |
| 969 | false)) |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 970 | printed += fprintf(fp, "[0] %s\n", filename); |
| 971 | } |
| 972 | |
| 973 | for (i = 0; i < vmlinux_path__nr_entries; ++i) |
| 974 | printed += fprintf(fp, "[%d] %s\n", |
| 975 | i + kdso->has_build_id, vmlinux_path[i]); |
| 976 | |
| 977 | return printed; |
| 978 | } |
| 979 | |
| 980 | size_t machine__fprintf(struct machine *machine, FILE *fp) |
| 981 | { |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 982 | struct rb_node *nd; |
Kan Liang | 91e467b | 2017-09-10 19:23:14 -0700 | [diff] [blame] | 983 | size_t ret; |
| 984 | int i; |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 985 | |
Kan Liang | 91e467b | 2017-09-10 19:23:14 -0700 | [diff] [blame] | 986 | for (i = 0; i < THREADS__TABLE_SIZE; i++) { |
| 987 | struct threads *threads = &machine->threads[i]; |
Arnaldo Carvalho de Melo | 0a7c74e | 2017-04-04 13:15:04 -0300 | [diff] [blame] | 988 | |
| 989 | down_read(&threads->lock); |
Arnaldo Carvalho de Melo | b91fc39 | 2015-04-06 20:43:22 -0300 | [diff] [blame] | 990 | |
Kan Liang | 91e467b | 2017-09-10 19:23:14 -0700 | [diff] [blame] | 991 | ret = fprintf(fp, "Threads: %u\n", threads->nr); |
Arnaldo Carvalho de Melo | d2c1103 | 2016-05-04 10:09:33 -0300 | [diff] [blame] | 992 | |
Davidlohr Bueso | f3acb3a | 2018-12-06 11:18:14 -0800 | [diff] [blame] | 993 | for (nd = rb_first_cached(&threads->entries); nd; |
| 994 | nd = rb_next(nd)) { |
Kan Liang | 91e467b | 2017-09-10 19:23:14 -0700 | [diff] [blame] | 995 | struct thread *pos = rb_entry(nd, struct thread, rb_node); |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 996 | |
Kan Liang | 91e467b | 2017-09-10 19:23:14 -0700 | [diff] [blame] | 997 | ret += thread__fprintf(pos, fp); |
| 998 | } |
| 999 | |
Arnaldo Carvalho de Melo | 0a7c74e | 2017-04-04 13:15:04 -0300 | [diff] [blame] | 1000 | up_read(&threads->lock); |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 1001 | } |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 1002 | return ret; |
| 1003 | } |
| 1004 | |
| 1005 | static struct dso *machine__get_kernel(struct machine *machine) |
| 1006 | { |
Jiri Olsa | 8c7f1bb | 2018-02-15 13:26:30 +0100 | [diff] [blame] | 1007 | const char *vmlinux_name = machine->mmap_name; |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 1008 | struct dso *kernel; |
| 1009 | |
| 1010 | if (machine__is_host(machine)) { |
Jiri Olsa | c192524 | 2018-03-12 16:24:06 +0100 | [diff] [blame] | 1011 | if (symbol_conf.vmlinux_name) |
| 1012 | vmlinux_name = symbol_conf.vmlinux_name; |
| 1013 | |
Arnaldo Carvalho de Melo | 459ce51 | 2015-05-28 12:40:55 -0300 | [diff] [blame] | 1014 | kernel = machine__findnew_kernel(machine, vmlinux_name, |
Jiri Olsa | 1c695c8 | 2020-08-08 14:21:54 +0200 | [diff] [blame] | 1015 | "[kernel]", DSO_SPACE__KERNEL); |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 1016 | } else { |
Jiri Olsa | c192524 | 2018-03-12 16:24:06 +0100 | [diff] [blame] | 1017 | if (symbol_conf.default_guest_vmlinux_name) |
| 1018 | vmlinux_name = symbol_conf.default_guest_vmlinux_name; |
| 1019 | |
Arnaldo Carvalho de Melo | 459ce51 | 2015-05-28 12:40:55 -0300 | [diff] [blame] | 1020 | kernel = machine__findnew_kernel(machine, vmlinux_name, |
| 1021 | "[guest.kernel]", |
Jiri Olsa | 1c695c8 | 2020-08-08 14:21:54 +0200 | [diff] [blame] | 1022 | DSO_SPACE__KERNEL_GUEST); |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 1023 | } |
| 1024 | |
| 1025 | if (kernel != NULL && (!kernel->has_build_id)) |
| 1026 | dso__read_running_kernel_build_id(kernel, machine); |
| 1027 | |
| 1028 | return kernel; |
| 1029 | } |
| 1030 | |
| 1031 | struct process_args { |
| 1032 | u64 start; |
| 1033 | }; |
| 1034 | |
Adrian Hunter | 1c5aae7 | 2018-05-22 13:54:36 +0300 | [diff] [blame] | 1035 | void machine__get_kallsyms_filename(struct machine *machine, char *buf, |
| 1036 | size_t bufsz) |
Adrian Hunter | 15a0a87 | 2014-01-29 16:14:38 +0200 | [diff] [blame] | 1037 | { |
| 1038 | if (machine__is_default_guest(machine)) |
| 1039 | scnprintf(buf, bufsz, "%s", symbol_conf.default_guest_kallsyms); |
| 1040 | else |
| 1041 | scnprintf(buf, bufsz, "%s/proc/kallsyms", machine->root_dir); |
| 1042 | } |
| 1043 | |
Simon Que | a93f0e5 | 2014-06-16 11:32:09 -0700 | [diff] [blame] | 1044 | const char *ref_reloc_sym_names[] = {"_text", "_stext", NULL}; |
| 1045 | |
| 1046 | /* Figure out the start address of kernel map from /proc/kallsyms. |
| 1047 | * Returns the name of the start symbol in *symbol_name. Pass in NULL as |
| 1048 | * symbol_name if it's not that important. |
| 1049 | */ |
Arnaldo Carvalho de Melo | b843f62 | 2017-04-27 21:21:09 -0300 | [diff] [blame] | 1050 | static int machine__get_running_kernel_start(struct machine *machine, |
Jiri Olsa | ed9adb2 | 2019-05-08 15:20:03 +0200 | [diff] [blame] | 1051 | const char **symbol_name, |
| 1052 | u64 *start, u64 *end) |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 1053 | { |
Adrian Hunter | 15a0a87 | 2014-01-29 16:14:38 +0200 | [diff] [blame] | 1054 | char filename[PATH_MAX]; |
Arnaldo Carvalho de Melo | b843f62 | 2017-04-27 21:21:09 -0300 | [diff] [blame] | 1055 | int i, err = -1; |
Simon Que | a93f0e5 | 2014-06-16 11:32:09 -0700 | [diff] [blame] | 1056 | const char *name; |
| 1057 | u64 addr = 0; |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 1058 | |
Adrian Hunter | 15a0a87 | 2014-01-29 16:14:38 +0200 | [diff] [blame] | 1059 | machine__get_kallsyms_filename(machine, filename, PATH_MAX); |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 1060 | |
| 1061 | if (symbol__restricted_filename(filename, "/proc/kallsyms")) |
| 1062 | return 0; |
| 1063 | |
Simon Que | a93f0e5 | 2014-06-16 11:32:09 -0700 | [diff] [blame] | 1064 | for (i = 0; (name = ref_reloc_sym_names[i]) != NULL; i++) { |
Arnaldo Carvalho de Melo | b843f62 | 2017-04-27 21:21:09 -0300 | [diff] [blame] | 1065 | err = kallsyms__get_function_start(filename, name, &addr); |
| 1066 | if (!err) |
Simon Que | a93f0e5 | 2014-06-16 11:32:09 -0700 | [diff] [blame] | 1067 | break; |
| 1068 | } |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 1069 | |
Arnaldo Carvalho de Melo | b843f62 | 2017-04-27 21:21:09 -0300 | [diff] [blame] | 1070 | if (err) |
| 1071 | return -1; |
| 1072 | |
Simon Que | a93f0e5 | 2014-06-16 11:32:09 -0700 | [diff] [blame] | 1073 | if (symbol_name) |
| 1074 | *symbol_name = name; |
| 1075 | |
Arnaldo Carvalho de Melo | b843f62 | 2017-04-27 21:21:09 -0300 | [diff] [blame] | 1076 | *start = addr; |
Jiri Olsa | ed9adb2 | 2019-05-08 15:20:03 +0200 | [diff] [blame] | 1077 | |
| 1078 | err = kallsyms__get_function_start(filename, "_etext", &addr); |
| 1079 | if (!err) |
| 1080 | *end = addr; |
| 1081 | |
Arnaldo Carvalho de Melo | b843f62 | 2017-04-27 21:21:09 -0300 | [diff] [blame] | 1082 | return 0; |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 1083 | } |
| 1084 | |
Adrian Hunter | 1c5aae7 | 2018-05-22 13:54:36 +0300 | [diff] [blame] | 1085 | int machine__create_extra_kernel_map(struct machine *machine, |
| 1086 | struct dso *kernel, |
| 1087 | struct extra_kernel_map *xm) |
Adrian Hunter | 4d99e41 | 2018-05-22 13:54:33 +0300 | [diff] [blame] | 1088 | { |
| 1089 | struct kmap *kmap; |
| 1090 | struct map *map; |
| 1091 | |
| 1092 | map = map__new2(xm->start, kernel); |
| 1093 | if (!map) |
| 1094 | return -1; |
| 1095 | |
| 1096 | map->end = xm->end; |
| 1097 | map->pgoff = xm->pgoff; |
| 1098 | |
| 1099 | kmap = map__kmap(map); |
| 1100 | |
Adrian Hunter | 5759a68 | 2018-05-22 13:54:35 +0300 | [diff] [blame] | 1101 | strlcpy(kmap->name, xm->name, KMAP_NAME_LEN); |
Adrian Hunter | 4d99e41 | 2018-05-22 13:54:33 +0300 | [diff] [blame] | 1102 | |
Arnaldo Carvalho de Melo | 79b6bb7 | 2019-11-25 21:58:33 -0300 | [diff] [blame] | 1103 | maps__insert(&machine->kmaps, map); |
Adrian Hunter | 4d99e41 | 2018-05-22 13:54:33 +0300 | [diff] [blame] | 1104 | |
Adrian Hunter | 5759a68 | 2018-05-22 13:54:35 +0300 | [diff] [blame] | 1105 | pr_debug2("Added extra kernel map %s %" PRIx64 "-%" PRIx64 "\n", |
| 1106 | kmap->name, map->start, map->end); |
Adrian Hunter | 4d99e41 | 2018-05-22 13:54:33 +0300 | [diff] [blame] | 1107 | |
| 1108 | map__put(map); |
| 1109 | |
| 1110 | return 0; |
| 1111 | } |
| 1112 | |
| 1113 | static u64 find_entry_trampoline(struct dso *dso) |
| 1114 | { |
| 1115 | /* Duplicates are removed so lookup all aliases */ |
| 1116 | const char *syms[] = { |
| 1117 | "_entry_trampoline", |
| 1118 | "__entry_trampoline_start", |
| 1119 | "entry_SYSCALL_64_trampoline", |
| 1120 | }; |
| 1121 | struct symbol *sym = dso__first_symbol(dso); |
| 1122 | unsigned int i; |
| 1123 | |
| 1124 | for (; sym; sym = dso__next_symbol(sym)) { |
| 1125 | if (sym->binding != STB_GLOBAL) |
| 1126 | continue; |
| 1127 | for (i = 0; i < ARRAY_SIZE(syms); i++) { |
| 1128 | if (!strcmp(sym->name, syms[i])) |
| 1129 | return sym->start; |
| 1130 | } |
| 1131 | } |
| 1132 | |
| 1133 | return 0; |
| 1134 | } |
| 1135 | |
| 1136 | /* |
| 1137 | * These values can be used for kernels that do not have symbols for the entry |
| 1138 | * trampolines in kallsyms. |
| 1139 | */ |
| 1140 | #define X86_64_CPU_ENTRY_AREA_PER_CPU 0xfffffe0000000000ULL |
| 1141 | #define X86_64_CPU_ENTRY_AREA_SIZE 0x2c000 |
| 1142 | #define X86_64_ENTRY_TRAMPOLINE 0x6000 |
| 1143 | |
| 1144 | /* Map x86_64 PTI entry trampolines */ |
| 1145 | int machine__map_x86_64_entry_trampolines(struct machine *machine, |
| 1146 | struct dso *kernel) |
| 1147 | { |
Arnaldo Carvalho de Melo | 79b6bb7 | 2019-11-25 21:58:33 -0300 | [diff] [blame] | 1148 | struct maps *kmaps = &machine->kmaps; |
Adrian Hunter | 4d99e41 | 2018-05-22 13:54:33 +0300 | [diff] [blame] | 1149 | int nr_cpus_avail, cpu; |
Adrian Hunter | 1c5aae7 | 2018-05-22 13:54:36 +0300 | [diff] [blame] | 1150 | bool found = false; |
| 1151 | struct map *map; |
| 1152 | u64 pgoff; |
Adrian Hunter | 4d99e41 | 2018-05-22 13:54:33 +0300 | [diff] [blame] | 1153 | |
Adrian Hunter | 1c5aae7 | 2018-05-22 13:54:36 +0300 | [diff] [blame] | 1154 | /* |
| 1155 | * In the vmlinux case, pgoff is a virtual address which must now be |
| 1156 | * mapped to a vmlinux offset. |
| 1157 | */ |
Arnaldo Carvalho de Melo | 79b6bb7 | 2019-11-25 21:58:33 -0300 | [diff] [blame] | 1158 | maps__for_each_entry(kmaps, map) { |
Adrian Hunter | 1c5aae7 | 2018-05-22 13:54:36 +0300 | [diff] [blame] | 1159 | struct kmap *kmap = __map__kmap(map); |
| 1160 | struct map *dest_map; |
| 1161 | |
| 1162 | if (!kmap || !is_entry_trampoline(kmap->name)) |
| 1163 | continue; |
| 1164 | |
Arnaldo Carvalho de Melo | 79b6bb7 | 2019-11-25 21:58:33 -0300 | [diff] [blame] | 1165 | dest_map = maps__find(kmaps, map->pgoff); |
Adrian Hunter | 1c5aae7 | 2018-05-22 13:54:36 +0300 | [diff] [blame] | 1166 | if (dest_map != map) |
| 1167 | map->pgoff = dest_map->map_ip(dest_map, map->pgoff); |
| 1168 | found = true; |
| 1169 | } |
| 1170 | if (found || machine->trampolines_mapped) |
| 1171 | return 0; |
| 1172 | |
| 1173 | pgoff = find_entry_trampoline(kernel); |
Adrian Hunter | 4d99e41 | 2018-05-22 13:54:33 +0300 | [diff] [blame] | 1174 | if (!pgoff) |
| 1175 | return 0; |
| 1176 | |
| 1177 | nr_cpus_avail = machine__nr_cpus_avail(machine); |
| 1178 | |
| 1179 | /* Add a 1 page map for each CPU's entry trampoline */ |
| 1180 | for (cpu = 0; cpu < nr_cpus_avail; cpu++) { |
| 1181 | u64 va = X86_64_CPU_ENTRY_AREA_PER_CPU + |
| 1182 | cpu * X86_64_CPU_ENTRY_AREA_SIZE + |
| 1183 | X86_64_ENTRY_TRAMPOLINE; |
| 1184 | struct extra_kernel_map xm = { |
| 1185 | .start = va, |
| 1186 | .end = va + page_size, |
| 1187 | .pgoff = pgoff, |
| 1188 | }; |
| 1189 | |
Adrian Hunter | 5759a68 | 2018-05-22 13:54:35 +0300 | [diff] [blame] | 1190 | strlcpy(xm.name, ENTRY_TRAMPOLINE_NAME, KMAP_NAME_LEN); |
| 1191 | |
Adrian Hunter | 4d99e41 | 2018-05-22 13:54:33 +0300 | [diff] [blame] | 1192 | if (machine__create_extra_kernel_map(machine, kernel, &xm) < 0) |
| 1193 | return -1; |
| 1194 | } |
| 1195 | |
Adrian Hunter | 1c5aae7 | 2018-05-22 13:54:36 +0300 | [diff] [blame] | 1196 | machine->trampolines_mapped = nr_cpus_avail; |
| 1197 | |
| 1198 | return 0; |
| 1199 | } |
| 1200 | |
| 1201 | int __weak machine__create_extra_kernel_maps(struct machine *machine __maybe_unused, |
| 1202 | struct dso *kernel __maybe_unused) |
| 1203 | { |
Adrian Hunter | 4d99e41 | 2018-05-22 13:54:33 +0300 | [diff] [blame] | 1204 | return 0; |
| 1205 | } |
| 1206 | |
Jiri Olsa | 1fb87b8 | 2018-02-15 13:26:32 +0100 | [diff] [blame] | 1207 | static int |
| 1208 | __machine__create_kernel_maps(struct machine *machine, struct dso *kernel) |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 1209 | { |
Masami Hiramatsu | cc1121a | 2015-12-09 11:11:33 +0900 | [diff] [blame] | 1210 | /* In case of renewal the kernel map, destroy previous one */ |
| 1211 | machine__destroy_kernel_maps(machine); |
| 1212 | |
Arnaldo Carvalho de Melo | 3183f8c | 2018-04-26 16:52:34 -0300 | [diff] [blame] | 1213 | machine->vmlinux_map = map__new2(0, kernel); |
| 1214 | if (machine->vmlinux_map == NULL) |
| 1215 | return -1; |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 1216 | |
Arnaldo Carvalho de Melo | 3183f8c | 2018-04-26 16:52:34 -0300 | [diff] [blame] | 1217 | machine->vmlinux_map->map_ip = machine->vmlinux_map->unmap_ip = identity__map_ip; |
Jiri Olsa | 484214f | 2020-02-10 15:32:18 +0100 | [diff] [blame] | 1218 | maps__insert(&machine->kmaps, machine->vmlinux_map); |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 1219 | return 0; |
| 1220 | } |
| 1221 | |
| 1222 | void machine__destroy_kernel_maps(struct machine *machine) |
| 1223 | { |
Arnaldo Carvalho de Melo | 3183f8c | 2018-04-26 16:52:34 -0300 | [diff] [blame] | 1224 | struct kmap *kmap; |
| 1225 | struct map *map = machine__kernel_map(machine); |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 1226 | |
Arnaldo Carvalho de Melo | 3183f8c | 2018-04-26 16:52:34 -0300 | [diff] [blame] | 1227 | if (map == NULL) |
| 1228 | return; |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 1229 | |
Arnaldo Carvalho de Melo | 3183f8c | 2018-04-26 16:52:34 -0300 | [diff] [blame] | 1230 | kmap = map__kmap(map); |
Arnaldo Carvalho de Melo | 79b6bb7 | 2019-11-25 21:58:33 -0300 | [diff] [blame] | 1231 | maps__remove(&machine->kmaps, map); |
Arnaldo Carvalho de Melo | 3183f8c | 2018-04-26 16:52:34 -0300 | [diff] [blame] | 1232 | if (kmap && kmap->ref_reloc_sym) { |
| 1233 | zfree((char **)&kmap->ref_reloc_sym->name); |
| 1234 | zfree(&kmap->ref_reloc_sym); |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 1235 | } |
Arnaldo Carvalho de Melo | 3183f8c | 2018-04-26 16:52:34 -0300 | [diff] [blame] | 1236 | |
| 1237 | map__zput(machine->vmlinux_map); |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 1238 | } |
| 1239 | |
Arnaldo Carvalho de Melo | 876650e6 | 2012-12-18 19:15:48 -0300 | [diff] [blame] | 1240 | int machines__create_guest_kernel_maps(struct machines *machines) |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 1241 | { |
| 1242 | int ret = 0; |
| 1243 | struct dirent **namelist = NULL; |
| 1244 | int i, items = 0; |
| 1245 | char path[PATH_MAX]; |
| 1246 | pid_t pid; |
| 1247 | char *endp; |
| 1248 | |
| 1249 | if (symbol_conf.default_guest_vmlinux_name || |
| 1250 | symbol_conf.default_guest_modules || |
| 1251 | symbol_conf.default_guest_kallsyms) { |
| 1252 | machines__create_kernel_maps(machines, DEFAULT_GUEST_KERNEL_ID); |
| 1253 | } |
| 1254 | |
| 1255 | if (symbol_conf.guestmount) { |
| 1256 | items = scandir(symbol_conf.guestmount, &namelist, NULL, NULL); |
| 1257 | if (items <= 0) |
| 1258 | return -ENOENT; |
| 1259 | for (i = 0; i < items; i++) { |
| 1260 | if (!isdigit(namelist[i]->d_name[0])) { |
| 1261 | /* Filter out . and .. */ |
| 1262 | continue; |
| 1263 | } |
| 1264 | pid = (pid_t)strtol(namelist[i]->d_name, &endp, 10); |
| 1265 | if ((*endp != '\0') || |
| 1266 | (endp == namelist[i]->d_name) || |
| 1267 | (errno == ERANGE)) { |
| 1268 | pr_debug("invalid directory (%s). Skipping.\n", |
| 1269 | namelist[i]->d_name); |
| 1270 | continue; |
| 1271 | } |
| 1272 | sprintf(path, "%s/%s/proc/kallsyms", |
| 1273 | symbol_conf.guestmount, |
| 1274 | namelist[i]->d_name); |
| 1275 | ret = access(path, R_OK); |
| 1276 | if (ret) { |
| 1277 | pr_debug("Can't access file %s\n", path); |
| 1278 | goto failure; |
| 1279 | } |
| 1280 | machines__create_kernel_maps(machines, pid); |
| 1281 | } |
| 1282 | failure: |
| 1283 | free(namelist); |
| 1284 | } |
| 1285 | |
| 1286 | return ret; |
| 1287 | } |
| 1288 | |
Arnaldo Carvalho de Melo | 876650e6 | 2012-12-18 19:15:48 -0300 | [diff] [blame] | 1289 | void machines__destroy_kernel_maps(struct machines *machines) |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 1290 | { |
Davidlohr Bueso | f3acb3a | 2018-12-06 11:18:14 -0800 | [diff] [blame] | 1291 | struct rb_node *next = rb_first_cached(&machines->guests); |
Arnaldo Carvalho de Melo | 876650e6 | 2012-12-18 19:15:48 -0300 | [diff] [blame] | 1292 | |
| 1293 | machine__destroy_kernel_maps(&machines->host); |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 1294 | |
| 1295 | while (next) { |
| 1296 | struct machine *pos = rb_entry(next, struct machine, rb_node); |
| 1297 | |
| 1298 | next = rb_next(&pos->rb_node); |
Davidlohr Bueso | f3acb3a | 2018-12-06 11:18:14 -0800 | [diff] [blame] | 1299 | rb_erase_cached(&pos->rb_node, &machines->guests); |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 1300 | machine__delete(pos); |
| 1301 | } |
| 1302 | } |
| 1303 | |
Arnaldo Carvalho de Melo | 876650e6 | 2012-12-18 19:15:48 -0300 | [diff] [blame] | 1304 | int machines__create_kernel_maps(struct machines *machines, pid_t pid) |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 1305 | { |
| 1306 | struct machine *machine = machines__findnew(machines, pid); |
| 1307 | |
| 1308 | if (machine == NULL) |
| 1309 | return -1; |
| 1310 | |
| 1311 | return machine__create_kernel_maps(machine); |
| 1312 | } |
| 1313 | |
Arnaldo Carvalho de Melo | 3183f8c | 2018-04-26 16:52:34 -0300 | [diff] [blame] | 1314 | int machine__load_kallsyms(struct machine *machine, const char *filename) |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 1315 | { |
Arnaldo Carvalho de Melo | a5e813c | 2015-09-30 11:54:04 -0300 | [diff] [blame] | 1316 | struct map *map = machine__kernel_map(machine); |
Jiri Olsa | e8f3879 | 2018-02-15 13:26:33 +0100 | [diff] [blame] | 1317 | int ret = __dso__load_kallsyms(map->dso, filename, map, true); |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 1318 | |
| 1319 | if (ret > 0) { |
Arnaldo Carvalho de Melo | 3183f8c | 2018-04-26 16:52:34 -0300 | [diff] [blame] | 1320 | dso__set_loaded(map->dso); |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 1321 | /* |
| 1322 | * Since /proc/kallsyms will have multiple sessions for the |
| 1323 | * kernel, with modules between them, fixup the end of all |
| 1324 | * sections. |
| 1325 | */ |
Arnaldo Carvalho de Melo | 79b6bb7 | 2019-11-25 21:58:33 -0300 | [diff] [blame] | 1326 | maps__fixup_end(&machine->kmaps); |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 1327 | } |
| 1328 | |
| 1329 | return ret; |
| 1330 | } |
| 1331 | |
Arnaldo Carvalho de Melo | 1d1a265 | 2018-04-25 12:18:11 -0300 | [diff] [blame] | 1332 | int machine__load_vmlinux_path(struct machine *machine) |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 1333 | { |
Arnaldo Carvalho de Melo | a5e813c | 2015-09-30 11:54:04 -0300 | [diff] [blame] | 1334 | struct map *map = machine__kernel_map(machine); |
Arnaldo Carvalho de Melo | be39db9 | 2016-09-01 19:25:52 -0300 | [diff] [blame] | 1335 | int ret = dso__load_vmlinux_path(map->dso, map); |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 1336 | |
Adrian Hunter | 39b12f78 | 2013-08-07 14:38:47 +0300 | [diff] [blame] | 1337 | if (ret > 0) |
Arnaldo Carvalho de Melo | 3183f8c | 2018-04-26 16:52:34 -0300 | [diff] [blame] | 1338 | dso__set_loaded(map->dso); |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 1339 | |
| 1340 | return ret; |
| 1341 | } |
| 1342 | |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 1343 | static char *get_kernel_version(const char *root_dir) |
| 1344 | { |
| 1345 | char version[PATH_MAX]; |
| 1346 | FILE *file; |
| 1347 | char *name, *tmp; |
| 1348 | const char *prefix = "Linux version "; |
| 1349 | |
| 1350 | sprintf(version, "%s/proc/version", root_dir); |
| 1351 | file = fopen(version, "r"); |
| 1352 | if (!file) |
| 1353 | return NULL; |
| 1354 | |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 1355 | tmp = fgets(version, sizeof(version), file); |
| 1356 | fclose(file); |
Donald Yandt | 34b65af | 2019-05-28 09:41:28 -0400 | [diff] [blame] | 1357 | if (!tmp) |
| 1358 | return NULL; |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 1359 | |
| 1360 | name = strstr(version, prefix); |
| 1361 | if (!name) |
| 1362 | return NULL; |
| 1363 | name += strlen(prefix); |
| 1364 | tmp = strchr(name, ' '); |
| 1365 | if (tmp) |
| 1366 | *tmp = '\0'; |
| 1367 | |
| 1368 | return strdup(name); |
| 1369 | } |
| 1370 | |
Jiri Olsa | bb58a8a | 2015-02-12 22:20:01 +0100 | [diff] [blame] | 1371 | static bool is_kmod_dso(struct dso *dso) |
| 1372 | { |
| 1373 | return dso->symtab_type == DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE || |
| 1374 | dso->symtab_type == DSO_BINARY_TYPE__GUEST_KMODULE; |
| 1375 | } |
| 1376 | |
Arnaldo Carvalho de Melo | 9a29cee | 2019-11-25 22:21:28 -0300 | [diff] [blame] | 1377 | static int maps__set_module_path(struct maps *maps, const char *path, struct kmod_path *m) |
Jiri Olsa | bb58a8a | 2015-02-12 22:20:01 +0100 | [diff] [blame] | 1378 | { |
Jiri Olsa | bb58a8a | 2015-02-12 22:20:01 +0100 | [diff] [blame] | 1379 | char *long_name; |
Arnaldo Carvalho de Melo | 9a29cee | 2019-11-25 22:21:28 -0300 | [diff] [blame] | 1380 | struct map *map = maps__find_by_name(maps, m->name); |
Jiri Olsa | bb58a8a | 2015-02-12 22:20:01 +0100 | [diff] [blame] | 1381 | |
Jiri Olsa | bb58a8a | 2015-02-12 22:20:01 +0100 | [diff] [blame] | 1382 | if (map == NULL) |
| 1383 | return 0; |
| 1384 | |
| 1385 | long_name = strdup(path); |
| 1386 | if (long_name == NULL) |
| 1387 | return -ENOMEM; |
| 1388 | |
| 1389 | dso__set_long_name(map->dso, long_name, true); |
| 1390 | dso__kernel_module_get_build_id(map->dso, ""); |
| 1391 | |
| 1392 | /* |
| 1393 | * Full name could reveal us kmod compression, so |
| 1394 | * we need to update the symtab_type if needed. |
| 1395 | */ |
Jiri Olsa | 2af5247 | 2018-08-17 11:48:07 +0200 | [diff] [blame] | 1396 | if (m->comp && is_kmod_dso(map->dso)) { |
Jiri Olsa | bb58a8a | 2015-02-12 22:20:01 +0100 | [diff] [blame] | 1397 | map->dso->symtab_type++; |
Jiri Olsa | 2af5247 | 2018-08-17 11:48:07 +0200 | [diff] [blame] | 1398 | map->dso->comp = m->comp; |
| 1399 | } |
Jiri Olsa | bb58a8a | 2015-02-12 22:20:01 +0100 | [diff] [blame] | 1400 | |
| 1401 | return 0; |
| 1402 | } |
| 1403 | |
Arnaldo Carvalho de Melo | 9a29cee | 2019-11-25 22:21:28 -0300 | [diff] [blame] | 1404 | static int maps__set_modules_path_dir(struct maps *maps, const char *dir_name, int depth) |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 1405 | { |
| 1406 | struct dirent *dent; |
| 1407 | DIR *dir = opendir(dir_name); |
| 1408 | int ret = 0; |
| 1409 | |
| 1410 | if (!dir) { |
| 1411 | pr_debug("%s: cannot open %s dir\n", __func__, dir_name); |
| 1412 | return -1; |
| 1413 | } |
| 1414 | |
| 1415 | while ((dent = readdir(dir)) != NULL) { |
| 1416 | char path[PATH_MAX]; |
| 1417 | struct stat st; |
| 1418 | |
| 1419 | /*sshfs might return bad dent->d_type, so we have to stat*/ |
Arnaldo Carvalho de Melo | 9d5f0c3 | 2022-01-18 09:02:43 -0300 | [diff] [blame] | 1420 | path__join(path, sizeof(path), dir_name, dent->d_name); |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 1421 | if (stat(path, &st)) |
| 1422 | continue; |
| 1423 | |
| 1424 | if (S_ISDIR(st.st_mode)) { |
| 1425 | if (!strcmp(dent->d_name, ".") || |
| 1426 | !strcmp(dent->d_name, "..")) |
| 1427 | continue; |
| 1428 | |
Richard Yao | 61d4290 | 2014-04-26 13:17:55 -0400 | [diff] [blame] | 1429 | /* Do not follow top-level source and build symlinks */ |
| 1430 | if (depth == 0) { |
| 1431 | if (!strcmp(dent->d_name, "source") || |
| 1432 | !strcmp(dent->d_name, "build")) |
| 1433 | continue; |
| 1434 | } |
| 1435 | |
Arnaldo Carvalho de Melo | 9a29cee | 2019-11-25 22:21:28 -0300 | [diff] [blame] | 1436 | ret = maps__set_modules_path_dir(maps, path, depth + 1); |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 1437 | if (ret < 0) |
| 1438 | goto out; |
| 1439 | } else { |
Jiri Olsa | bb58a8a | 2015-02-12 22:20:01 +0100 | [diff] [blame] | 1440 | struct kmod_path m; |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 1441 | |
Jiri Olsa | bb58a8a | 2015-02-12 22:20:01 +0100 | [diff] [blame] | 1442 | ret = kmod_path__parse_name(&m, dent->d_name); |
| 1443 | if (ret) |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 1444 | goto out; |
Jiri Olsa | bb58a8a | 2015-02-12 22:20:01 +0100 | [diff] [blame] | 1445 | |
| 1446 | if (m.kmod) |
Arnaldo Carvalho de Melo | 9a29cee | 2019-11-25 22:21:28 -0300 | [diff] [blame] | 1447 | ret = maps__set_module_path(maps, path, &m); |
Jiri Olsa | bb58a8a | 2015-02-12 22:20:01 +0100 | [diff] [blame] | 1448 | |
Arnaldo Carvalho de Melo | d8f9da2 | 2019-07-04 12:06:20 -0300 | [diff] [blame] | 1449 | zfree(&m.name); |
Jiri Olsa | bb58a8a | 2015-02-12 22:20:01 +0100 | [diff] [blame] | 1450 | |
| 1451 | if (ret) |
| 1452 | goto out; |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 1453 | } |
| 1454 | } |
| 1455 | |
| 1456 | out: |
| 1457 | closedir(dir); |
| 1458 | return ret; |
| 1459 | } |
| 1460 | |
| 1461 | static int machine__set_modules_path(struct machine *machine) |
| 1462 | { |
| 1463 | char *version; |
| 1464 | char modules_path[PATH_MAX]; |
| 1465 | |
| 1466 | version = get_kernel_version(machine->root_dir); |
| 1467 | if (!version) |
| 1468 | return -1; |
| 1469 | |
Richard Yao | 61d4290 | 2014-04-26 13:17:55 -0400 | [diff] [blame] | 1470 | snprintf(modules_path, sizeof(modules_path), "%s/lib/modules/%s", |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 1471 | machine->root_dir, version); |
| 1472 | free(version); |
| 1473 | |
Arnaldo Carvalho de Melo | 79b6bb7 | 2019-11-25 21:58:33 -0300 | [diff] [blame] | 1474 | return maps__set_modules_path_dir(&machine->kmaps, modules_path, 0); |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 1475 | } |
Song Shan Gong | 203d8a4 | 2016-07-21 11:10:51 +0800 | [diff] [blame] | 1476 | int __weak arch__fix_module_text_start(u64 *start __maybe_unused, |
Thomas Richter | 12a6d29 | 2019-07-24 14:27:02 +0200 | [diff] [blame] | 1477 | u64 *size __maybe_unused, |
Song Shan Gong | 203d8a4 | 2016-07-21 11:10:51 +0800 | [diff] [blame] | 1478 | const char *name __maybe_unused) |
| 1479 | { |
| 1480 | return 0; |
| 1481 | } |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 1482 | |
Thomas Richter | 9ad4652 | 2017-08-03 15:49:02 +0200 | [diff] [blame] | 1483 | static int machine__create_module(void *arg, const char *name, u64 start, |
| 1484 | u64 size) |
Adrian Hunter | 316d70d | 2013-10-08 11:45:48 +0300 | [diff] [blame] | 1485 | { |
| 1486 | struct machine *machine = arg; |
| 1487 | struct map *map; |
| 1488 | |
Thomas Richter | 12a6d29 | 2019-07-24 14:27:02 +0200 | [diff] [blame] | 1489 | if (arch__fix_module_text_start(&start, &size, name) < 0) |
Song Shan Gong | 203d8a4 | 2016-07-21 11:10:51 +0800 | [diff] [blame] | 1490 | return -1; |
| 1491 | |
Arnaldo Carvalho de Melo | a94ab91 | 2019-11-14 12:28:41 -0300 | [diff] [blame] | 1492 | map = machine__addnew_module_map(machine, start, name); |
Adrian Hunter | 316d70d | 2013-10-08 11:45:48 +0300 | [diff] [blame] | 1493 | if (map == NULL) |
| 1494 | return -1; |
Thomas Richter | 9ad4652 | 2017-08-03 15:49:02 +0200 | [diff] [blame] | 1495 | map->end = start + size; |
Adrian Hunter | 316d70d | 2013-10-08 11:45:48 +0300 | [diff] [blame] | 1496 | |
| 1497 | dso__kernel_module_get_build_id(map->dso, machine->root_dir); |
| 1498 | |
| 1499 | return 0; |
| 1500 | } |
| 1501 | |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 1502 | static int machine__create_modules(struct machine *machine) |
| 1503 | { |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 1504 | const char *modules; |
| 1505 | char path[PATH_MAX]; |
| 1506 | |
Adrian Hunter | f4be904 | 2013-09-22 13:22:09 +0300 | [diff] [blame] | 1507 | if (machine__is_default_guest(machine)) { |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 1508 | modules = symbol_conf.default_guest_modules; |
Adrian Hunter | f4be904 | 2013-09-22 13:22:09 +0300 | [diff] [blame] | 1509 | } else { |
| 1510 | snprintf(path, PATH_MAX, "%s/proc/modules", machine->root_dir); |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 1511 | modules = path; |
| 1512 | } |
| 1513 | |
Adrian Hunter | aa7fe3b | 2013-09-22 13:22:09 +0300 | [diff] [blame] | 1514 | if (symbol__restricted_filename(modules, "/proc/modules")) |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 1515 | return -1; |
| 1516 | |
Adrian Hunter | 316d70d | 2013-10-08 11:45:48 +0300 | [diff] [blame] | 1517 | if (modules__parse(modules, machine, machine__create_module)) |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 1518 | return -1; |
| 1519 | |
Adrian Hunter | 316d70d | 2013-10-08 11:45:48 +0300 | [diff] [blame] | 1520 | if (!machine__set_modules_path(machine)) |
| 1521 | return 0; |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 1522 | |
Adrian Hunter | 316d70d | 2013-10-08 11:45:48 +0300 | [diff] [blame] | 1523 | pr_debug("Problems setting modules path maps, continuing anyway...\n"); |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 1524 | |
Jason Wessel | 8f76fcd | 2013-07-15 15:27:53 -0500 | [diff] [blame] | 1525 | return 0; |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 1526 | } |
| 1527 | |
Jiri Olsa | 1fb87b8 | 2018-02-15 13:26:32 +0100 | [diff] [blame] | 1528 | static void machine__set_kernel_mmap(struct machine *machine, |
| 1529 | u64 start, u64 end) |
| 1530 | { |
Arnaldo Carvalho de Melo | 3183f8c | 2018-04-26 16:52:34 -0300 | [diff] [blame] | 1531 | machine->vmlinux_map->start = start; |
| 1532 | machine->vmlinux_map->end = end; |
| 1533 | /* |
| 1534 | * Be a bit paranoid here, some perf.data file came with |
| 1535 | * a zero sized synthesized MMAP event for the kernel. |
| 1536 | */ |
| 1537 | if (start == 0 && end == 0) |
| 1538 | machine->vmlinux_map->end = ~0ULL; |
Jiri Olsa | 1fb87b8 | 2018-02-15 13:26:32 +0100 | [diff] [blame] | 1539 | } |
| 1540 | |
Wei Li | 977c7a6 | 2019-02-28 17:20:03 +0800 | [diff] [blame] | 1541 | static void machine__update_kernel_mmap(struct machine *machine, |
| 1542 | u64 start, u64 end) |
| 1543 | { |
| 1544 | struct map *map = machine__kernel_map(machine); |
| 1545 | |
| 1546 | map__get(map); |
Arnaldo Carvalho de Melo | 79b6bb7 | 2019-11-25 21:58:33 -0300 | [diff] [blame] | 1547 | maps__remove(&machine->kmaps, map); |
Wei Li | 977c7a6 | 2019-02-28 17:20:03 +0800 | [diff] [blame] | 1548 | |
| 1549 | machine__set_kernel_mmap(machine, start, end); |
| 1550 | |
Arnaldo Carvalho de Melo | 79b6bb7 | 2019-11-25 21:58:33 -0300 | [diff] [blame] | 1551 | maps__insert(&machine->kmaps, map); |
Wei Li | 977c7a6 | 2019-02-28 17:20:03 +0800 | [diff] [blame] | 1552 | map__put(map); |
| 1553 | } |
| 1554 | |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 1555 | int machine__create_kernel_maps(struct machine *machine) |
| 1556 | { |
| 1557 | struct dso *kernel = machine__get_kernel(machine); |
Arnaldo Carvalho de Melo | b843f62 | 2017-04-27 21:21:09 -0300 | [diff] [blame] | 1558 | const char *name = NULL; |
Namhyung Kim | ee05d2179 | 2018-02-19 19:05:45 +0900 | [diff] [blame] | 1559 | struct map *map; |
Jiri Olsa | ed9adb2 | 2019-05-08 15:20:03 +0200 | [diff] [blame] | 1560 | u64 start = 0, end = ~0ULL; |
Masami Hiramatsu | 1154c95 | 2015-11-18 15:40:33 +0900 | [diff] [blame] | 1561 | int ret; |
| 1562 | |
Arnaldo Carvalho de Melo | 45e9005 | 2016-05-17 11:52:26 -0300 | [diff] [blame] | 1563 | if (kernel == NULL) |
Adrian Hunter | 5512cf2 | 2014-01-29 16:14:39 +0200 | [diff] [blame] | 1564 | return -1; |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 1565 | |
Masami Hiramatsu | 1154c95 | 2015-11-18 15:40:33 +0900 | [diff] [blame] | 1566 | ret = __machine__create_kernel_maps(machine, kernel); |
Masami Hiramatsu | 1154c95 | 2015-11-18 15:40:33 +0900 | [diff] [blame] | 1567 | if (ret < 0) |
Adrian Hunter | 1c5aae7 | 2018-05-22 13:54:36 +0300 | [diff] [blame] | 1568 | goto out_put; |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 1569 | |
| 1570 | if (symbol_conf.use_modules && machine__create_modules(machine) < 0) { |
| 1571 | if (machine__is_host(machine)) |
| 1572 | pr_debug("Problems creating module maps, " |
| 1573 | "continuing anyway...\n"); |
| 1574 | else |
| 1575 | pr_debug("Problems creating module maps for guest %d, " |
| 1576 | "continuing anyway...\n", machine->pid); |
| 1577 | } |
| 1578 | |
Jiri Olsa | ed9adb2 | 2019-05-08 15:20:03 +0200 | [diff] [blame] | 1579 | if (!machine__get_running_kernel_start(machine, &name, &start, &end)) { |
Jiri Olsa | 3f938ee | 2017-06-26 11:51:53 +0200 | [diff] [blame] | 1580 | if (name && |
Jiri Olsa | ed9adb2 | 2019-05-08 15:20:03 +0200 | [diff] [blame] | 1581 | map__set_kallsyms_ref_reloc_sym(machine->vmlinux_map, name, start)) { |
Jiri Olsa | 3f938ee | 2017-06-26 11:51:53 +0200 | [diff] [blame] | 1582 | machine__destroy_kernel_maps(machine); |
Adrian Hunter | 1c5aae7 | 2018-05-22 13:54:36 +0300 | [diff] [blame] | 1583 | ret = -1; |
| 1584 | goto out_put; |
Jiri Olsa | 3f938ee | 2017-06-26 11:51:53 +0200 | [diff] [blame] | 1585 | } |
Namhyung Kim | ee05d2179 | 2018-02-19 19:05:45 +0900 | [diff] [blame] | 1586 | |
Wei Li | 977c7a6 | 2019-02-28 17:20:03 +0800 | [diff] [blame] | 1587 | /* |
| 1588 | * we have a real start address now, so re-order the kmaps |
| 1589 | * assume it's the last in the kmaps |
| 1590 | */ |
Jiri Olsa | ed9adb2 | 2019-05-08 15:20:03 +0200 | [diff] [blame] | 1591 | machine__update_kernel_mmap(machine, start, end); |
Adrian Hunter | 5512cf2 | 2014-01-29 16:14:39 +0200 | [diff] [blame] | 1592 | } |
| 1593 | |
Adrian Hunter | 1c5aae7 | 2018-05-22 13:54:36 +0300 | [diff] [blame] | 1594 | if (machine__create_extra_kernel_maps(machine, kernel)) |
| 1595 | pr_debug("Problems creating extra kernel maps, continuing anyway...\n"); |
| 1596 | |
Jiri Olsa | ed9adb2 | 2019-05-08 15:20:03 +0200 | [diff] [blame] | 1597 | if (end == ~0ULL) { |
| 1598 | /* update end address of the kernel map using adjacent module address */ |
| 1599 | map = map__next(machine__kernel_map(machine)); |
| 1600 | if (map) |
| 1601 | machine__set_kernel_mmap(machine, start, map->start); |
| 1602 | } |
| 1603 | |
Adrian Hunter | 1c5aae7 | 2018-05-22 13:54:36 +0300 | [diff] [blame] | 1604 | out_put: |
| 1605 | dso__put(kernel); |
| 1606 | return ret; |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 1607 | } |
| 1608 | |
Adrian Hunter | 8e0cf96 | 2013-08-07 14:38:51 +0300 | [diff] [blame] | 1609 | static bool machine__uses_kcore(struct machine *machine) |
| 1610 | { |
| 1611 | struct dso *dso; |
| 1612 | |
Arnaldo Carvalho de Melo | 3d39ac5 | 2015-05-28 13:06:42 -0300 | [diff] [blame] | 1613 | list_for_each_entry(dso, &machine->dsos.head, node) { |
Adrian Hunter | 8e0cf96 | 2013-08-07 14:38:51 +0300 | [diff] [blame] | 1614 | if (dso__is_kcore(dso)) |
| 1615 | return true; |
| 1616 | } |
| 1617 | |
| 1618 | return false; |
| 1619 | } |
| 1620 | |
Adrian Hunter | a8ce99b | 2018-05-22 13:54:37 +0300 | [diff] [blame] | 1621 | static bool perf_event__is_extra_kernel_mmap(struct machine *machine, |
Jiri Olsa | 031f112 | 2020-11-26 18:00:12 +0100 | [diff] [blame] | 1622 | struct extra_kernel_map *xm) |
Adrian Hunter | a8ce99b | 2018-05-22 13:54:37 +0300 | [diff] [blame] | 1623 | { |
| 1624 | return machine__is(machine, "x86_64") && |
Jiri Olsa | 031f112 | 2020-11-26 18:00:12 +0100 | [diff] [blame] | 1625 | is_entry_trampoline(xm->name); |
Adrian Hunter | a8ce99b | 2018-05-22 13:54:37 +0300 | [diff] [blame] | 1626 | } |
| 1627 | |
| 1628 | static int machine__process_extra_kernel_map(struct machine *machine, |
Jiri Olsa | 031f112 | 2020-11-26 18:00:12 +0100 | [diff] [blame] | 1629 | struct extra_kernel_map *xm) |
Adrian Hunter | a8ce99b | 2018-05-22 13:54:37 +0300 | [diff] [blame] | 1630 | { |
Arnaldo Carvalho de Melo | 93730f8 | 2019-10-31 15:22:24 -0300 | [diff] [blame] | 1631 | struct dso *kernel = machine__kernel_dso(machine); |
Adrian Hunter | a8ce99b | 2018-05-22 13:54:37 +0300 | [diff] [blame] | 1632 | |
| 1633 | if (kernel == NULL) |
| 1634 | return -1; |
| 1635 | |
Jiri Olsa | 031f112 | 2020-11-26 18:00:12 +0100 | [diff] [blame] | 1636 | return machine__create_extra_kernel_map(machine, kernel, xm); |
Adrian Hunter | a8ce99b | 2018-05-22 13:54:37 +0300 | [diff] [blame] | 1637 | } |
| 1638 | |
Arnaldo Carvalho de Melo | b0a7d1a | 2012-10-06 16:26:02 -0300 | [diff] [blame] | 1639 | static int machine__process_kernel_mmap_event(struct machine *machine, |
Jiri Olsa | 1ca6e80 | 2020-12-14 11:54:49 +0100 | [diff] [blame] | 1640 | struct extra_kernel_map *xm, |
| 1641 | struct build_id *bid) |
Arnaldo Carvalho de Melo | b0a7d1a | 2012-10-06 16:26:02 -0300 | [diff] [blame] | 1642 | { |
| 1643 | struct map *map; |
Jiri Olsa | 1c695c8 | 2020-08-08 14:21:54 +0200 | [diff] [blame] | 1644 | enum dso_space_type dso_space; |
Arnaldo Carvalho de Melo | b0a7d1a | 2012-10-06 16:26:02 -0300 | [diff] [blame] | 1645 | bool is_kernel_mmap; |
| 1646 | |
Adrian Hunter | 8e0cf96 | 2013-08-07 14:38:51 +0300 | [diff] [blame] | 1647 | /* If we have maps from kcore then we do not need or want any others */ |
| 1648 | if (machine__uses_kcore(machine)) |
| 1649 | return 0; |
| 1650 | |
Arnaldo Carvalho de Melo | b0a7d1a | 2012-10-06 16:26:02 -0300 | [diff] [blame] | 1651 | if (machine__is_host(machine)) |
Jiri Olsa | 1c695c8 | 2020-08-08 14:21:54 +0200 | [diff] [blame] | 1652 | dso_space = DSO_SPACE__KERNEL; |
Arnaldo Carvalho de Melo | b0a7d1a | 2012-10-06 16:26:02 -0300 | [diff] [blame] | 1653 | else |
Jiri Olsa | 1c695c8 | 2020-08-08 14:21:54 +0200 | [diff] [blame] | 1654 | dso_space = DSO_SPACE__KERNEL_GUEST; |
Arnaldo Carvalho de Melo | b0a7d1a | 2012-10-06 16:26:02 -0300 | [diff] [blame] | 1655 | |
Jiri Olsa | 031f112 | 2020-11-26 18:00:12 +0100 | [diff] [blame] | 1656 | is_kernel_mmap = memcmp(xm->name, machine->mmap_name, |
Jiri Olsa | 8c7f1bb | 2018-02-15 13:26:30 +0100 | [diff] [blame] | 1657 | strlen(machine->mmap_name) - 1) == 0; |
Jiri Olsa | 031f112 | 2020-11-26 18:00:12 +0100 | [diff] [blame] | 1658 | if (xm->name[0] == '/' || |
| 1659 | (!is_kernel_mmap && xm->name[0] == '[')) { |
| 1660 | map = machine__addnew_module_map(machine, xm->start, |
| 1661 | xm->name); |
Arnaldo Carvalho de Melo | b0a7d1a | 2012-10-06 16:26:02 -0300 | [diff] [blame] | 1662 | if (map == NULL) |
| 1663 | goto out_problem; |
| 1664 | |
Jiri Olsa | 031f112 | 2020-11-26 18:00:12 +0100 | [diff] [blame] | 1665 | map->end = map->start + xm->end - xm->start; |
Jiri Olsa | 1ca6e80 | 2020-12-14 11:54:49 +0100 | [diff] [blame] | 1666 | |
| 1667 | if (build_id__is_defined(bid)) |
| 1668 | dso__set_build_id(map->dso, bid); |
| 1669 | |
Arnaldo Carvalho de Melo | b0a7d1a | 2012-10-06 16:26:02 -0300 | [diff] [blame] | 1670 | } else if (is_kernel_mmap) { |
Jiri Olsa | 031f112 | 2020-11-26 18:00:12 +0100 | [diff] [blame] | 1671 | const char *symbol_name = (xm->name + strlen(machine->mmap_name)); |
Arnaldo Carvalho de Melo | b0a7d1a | 2012-10-06 16:26:02 -0300 | [diff] [blame] | 1672 | /* |
| 1673 | * Should be there already, from the build-id table in |
| 1674 | * the header. |
| 1675 | */ |
Namhyung Kim | b837a8b | 2014-11-04 10:14:33 +0900 | [diff] [blame] | 1676 | struct dso *kernel = NULL; |
| 1677 | struct dso *dso; |
| 1678 | |
Arnaldo Carvalho de Melo | 0a7c74e | 2017-04-04 13:15:04 -0300 | [diff] [blame] | 1679 | down_read(&machine->dsos.lock); |
Arnaldo Carvalho de Melo | e880784 | 2015-06-01 15:40:01 -0300 | [diff] [blame] | 1680 | |
Arnaldo Carvalho de Melo | 3d39ac5 | 2015-05-28 13:06:42 -0300 | [diff] [blame] | 1681 | list_for_each_entry(dso, &machine->dsos.head, node) { |
Wang Nan | 1f121b0 | 2015-06-03 08:52:21 +0000 | [diff] [blame] | 1682 | |
| 1683 | /* |
| 1684 | * The cpumode passed to is_kernel_module is not the |
| 1685 | * cpumode of *this* event. If we insist on passing |
| 1686 | * correct cpumode to is_kernel_module, we should |
| 1687 | * record the cpumode when we adding this dso to the |
| 1688 | * linked list. |
| 1689 | * |
| 1690 | * However we don't really need passing correct |
| 1691 | * cpumode. We know the correct cpumode must be kernel |
| 1692 | * mode (if not, we should not link it onto kernel_dsos |
| 1693 | * list). |
| 1694 | * |
| 1695 | * Therefore, we pass PERF_RECORD_MISC_CPUMODE_UNKNOWN. |
| 1696 | * is_kernel_module() treats it as a kernel cpumode. |
| 1697 | */ |
| 1698 | |
| 1699 | if (!dso->kernel || |
| 1700 | is_kernel_module(dso->long_name, |
| 1701 | PERF_RECORD_MISC_CPUMODE_UNKNOWN)) |
Namhyung Kim | b837a8b | 2014-11-04 10:14:33 +0900 | [diff] [blame] | 1702 | continue; |
| 1703 | |
Wang Nan | 1f121b0 | 2015-06-03 08:52:21 +0000 | [diff] [blame] | 1704 | |
Namhyung Kim | b837a8b | 2014-11-04 10:14:33 +0900 | [diff] [blame] | 1705 | kernel = dso; |
| 1706 | break; |
| 1707 | } |
| 1708 | |
Arnaldo Carvalho de Melo | 0a7c74e | 2017-04-04 13:15:04 -0300 | [diff] [blame] | 1709 | up_read(&machine->dsos.lock); |
Arnaldo Carvalho de Melo | e880784 | 2015-06-01 15:40:01 -0300 | [diff] [blame] | 1710 | |
Namhyung Kim | b837a8b | 2014-11-04 10:14:33 +0900 | [diff] [blame] | 1711 | if (kernel == NULL) |
Jiri Olsa | 8c7f1bb | 2018-02-15 13:26:30 +0100 | [diff] [blame] | 1712 | kernel = machine__findnew_dso(machine, machine->mmap_name); |
Arnaldo Carvalho de Melo | b0a7d1a | 2012-10-06 16:26:02 -0300 | [diff] [blame] | 1713 | if (kernel == NULL) |
| 1714 | goto out_problem; |
| 1715 | |
Jiri Olsa | 1c695c8 | 2020-08-08 14:21:54 +0200 | [diff] [blame] | 1716 | kernel->kernel = dso_space; |
Arnaldo Carvalho de Melo | d3a7c48 | 2015-06-02 11:53:26 -0300 | [diff] [blame] | 1717 | if (__machine__create_kernel_maps(machine, kernel) < 0) { |
| 1718 | dso__put(kernel); |
Arnaldo Carvalho de Melo | b0a7d1a | 2012-10-06 16:26:02 -0300 | [diff] [blame] | 1719 | goto out_problem; |
Arnaldo Carvalho de Melo | d3a7c48 | 2015-06-02 11:53:26 -0300 | [diff] [blame] | 1720 | } |
Arnaldo Carvalho de Melo | b0a7d1a | 2012-10-06 16:26:02 -0300 | [diff] [blame] | 1721 | |
Namhyung Kim | 330dfa2 | 2014-11-18 13:30:28 +0900 | [diff] [blame] | 1722 | if (strstr(kernel->long_name, "vmlinux")) |
| 1723 | dso__set_short_name(kernel, "[kernel.vmlinux]", false); |
Namhyung Kim | 96d7805 | 2014-11-04 10:14:34 +0900 | [diff] [blame] | 1724 | |
Jiri Olsa | 031f112 | 2020-11-26 18:00:12 +0100 | [diff] [blame] | 1725 | machine__update_kernel_mmap(machine, xm->start, xm->end); |
Arnaldo Carvalho de Melo | b0a7d1a | 2012-10-06 16:26:02 -0300 | [diff] [blame] | 1726 | |
Jiri Olsa | 1ca6e80 | 2020-12-14 11:54:49 +0100 | [diff] [blame] | 1727 | if (build_id__is_defined(bid)) |
| 1728 | dso__set_build_id(kernel, bid); |
| 1729 | |
Arnaldo Carvalho de Melo | b0a7d1a | 2012-10-06 16:26:02 -0300 | [diff] [blame] | 1730 | /* |
| 1731 | * Avoid using a zero address (kptr_restrict) for the ref reloc |
| 1732 | * symbol. Effectively having zero here means that at record |
| 1733 | * time /proc/sys/kernel/kptr_restrict was non zero. |
| 1734 | */ |
Jiri Olsa | 031f112 | 2020-11-26 18:00:12 +0100 | [diff] [blame] | 1735 | if (xm->pgoff != 0) { |
Arnaldo Carvalho de Melo | 3183f8c | 2018-04-26 16:52:34 -0300 | [diff] [blame] | 1736 | map__set_kallsyms_ref_reloc_sym(machine->vmlinux_map, |
| 1737 | symbol_name, |
Jiri Olsa | 031f112 | 2020-11-26 18:00:12 +0100 | [diff] [blame] | 1738 | xm->pgoff); |
Arnaldo Carvalho de Melo | b0a7d1a | 2012-10-06 16:26:02 -0300 | [diff] [blame] | 1739 | } |
| 1740 | |
| 1741 | if (machine__is_default_guest(machine)) { |
| 1742 | /* |
| 1743 | * preload dso of guest kernel and modules |
| 1744 | */ |
Arnaldo Carvalho de Melo | be39db9 | 2016-09-01 19:25:52 -0300 | [diff] [blame] | 1745 | dso__load(kernel, machine__kernel_map(machine)); |
Arnaldo Carvalho de Melo | b0a7d1a | 2012-10-06 16:26:02 -0300 | [diff] [blame] | 1746 | } |
Jiri Olsa | 031f112 | 2020-11-26 18:00:12 +0100 | [diff] [blame] | 1747 | } else if (perf_event__is_extra_kernel_mmap(machine, xm)) { |
| 1748 | return machine__process_extra_kernel_map(machine, xm); |
Arnaldo Carvalho de Melo | b0a7d1a | 2012-10-06 16:26:02 -0300 | [diff] [blame] | 1749 | } |
| 1750 | return 0; |
| 1751 | out_problem: |
| 1752 | return -1; |
| 1753 | } |
| 1754 | |
Stephane Eranian | 5c5e854 | 2013-08-21 12:10:25 +0200 | [diff] [blame] | 1755 | int machine__process_mmap2_event(struct machine *machine, |
Frederic Weisbecker | 162f0be | 2013-09-11 16:18:24 +0200 | [diff] [blame] | 1756 | union perf_event *event, |
Arnaldo Carvalho de Melo | 473398a | 2016-03-22 18:23:43 -0300 | [diff] [blame] | 1757 | struct perf_sample *sample) |
Stephane Eranian | 5c5e854 | 2013-08-21 12:10:25 +0200 | [diff] [blame] | 1758 | { |
Stephane Eranian | 5c5e854 | 2013-08-21 12:10:25 +0200 | [diff] [blame] | 1759 | struct thread *thread; |
| 1760 | struct map *map; |
Arnaldo Carvalho de Melo | 4a7380a | 2019-11-19 12:40:29 -0300 | [diff] [blame] | 1761 | struct dso_id dso_id = { |
| 1762 | .maj = event->mmap2.maj, |
| 1763 | .min = event->mmap2.min, |
| 1764 | .ino = event->mmap2.ino, |
| 1765 | .ino_generation = event->mmap2.ino_generation, |
| 1766 | }; |
Jiri Olsa | 1ca6e80 | 2020-12-14 11:54:49 +0100 | [diff] [blame] | 1767 | struct build_id __bid, *bid = NULL; |
Stephane Eranian | 5c5e854 | 2013-08-21 12:10:25 +0200 | [diff] [blame] | 1768 | int ret = 0; |
| 1769 | |
| 1770 | if (dump_trace) |
| 1771 | perf_event__fprintf_mmap2(event, stdout); |
| 1772 | |
Jiri Olsa | 1ca6e80 | 2020-12-14 11:54:49 +0100 | [diff] [blame] | 1773 | if (event->header.misc & PERF_RECORD_MISC_MMAP_BUILD_ID) { |
| 1774 | bid = &__bid; |
| 1775 | build_id__init(bid, event->mmap2.build_id, event->mmap2.build_id_size); |
| 1776 | } |
| 1777 | |
Arnaldo Carvalho de Melo | 473398a | 2016-03-22 18:23:43 -0300 | [diff] [blame] | 1778 | if (sample->cpumode == PERF_RECORD_MISC_GUEST_KERNEL || |
| 1779 | sample->cpumode == PERF_RECORD_MISC_KERNEL) { |
Jiri Olsa | 031f112 | 2020-11-26 18:00:12 +0100 | [diff] [blame] | 1780 | struct extra_kernel_map xm = { |
| 1781 | .start = event->mmap2.start, |
| 1782 | .end = event->mmap2.start + event->mmap2.len, |
| 1783 | .pgoff = event->mmap2.pgoff, |
| 1784 | }; |
| 1785 | |
| 1786 | strlcpy(xm.name, event->mmap2.filename, KMAP_NAME_LEN); |
Jiri Olsa | 1ca6e80 | 2020-12-14 11:54:49 +0100 | [diff] [blame] | 1787 | ret = machine__process_kernel_mmap_event(machine, &xm, bid); |
Stephane Eranian | 5c5e854 | 2013-08-21 12:10:25 +0200 | [diff] [blame] | 1788 | if (ret < 0) |
| 1789 | goto out_problem; |
| 1790 | return 0; |
| 1791 | } |
| 1792 | |
| 1793 | thread = machine__findnew_thread(machine, event->mmap2.pid, |
Don Zickus | 11c9abf | 2014-02-26 10:45:27 -0500 | [diff] [blame] | 1794 | event->mmap2.tid); |
Stephane Eranian | 5c5e854 | 2013-08-21 12:10:25 +0200 | [diff] [blame] | 1795 | if (thread == NULL) |
| 1796 | goto out_problem; |
| 1797 | |
Adrian Hunter | 2a03068 | 2014-07-22 16:17:53 +0300 | [diff] [blame] | 1798 | map = map__new(machine, event->mmap2.start, |
Stephane Eranian | 5c5e854 | 2013-08-21 12:10:25 +0200 | [diff] [blame] | 1799 | event->mmap2.len, event->mmap2.pgoff, |
Arnaldo Carvalho de Melo | 4a7380a | 2019-11-19 12:40:29 -0300 | [diff] [blame] | 1800 | &dso_id, event->mmap2.prot, |
Jiri Olsa | 1ca6e80 | 2020-12-14 11:54:49 +0100 | [diff] [blame] | 1801 | event->mmap2.flags, bid, |
Arnaldo Carvalho de Melo | 3183f8c | 2018-04-26 16:52:34 -0300 | [diff] [blame] | 1802 | event->mmap2.filename, thread); |
Stephane Eranian | 5c5e854 | 2013-08-21 12:10:25 +0200 | [diff] [blame] | 1803 | |
| 1804 | if (map == NULL) |
Arnaldo Carvalho de Melo | b91fc39 | 2015-04-06 20:43:22 -0300 | [diff] [blame] | 1805 | goto out_problem_map; |
Stephane Eranian | 5c5e854 | 2013-08-21 12:10:25 +0200 | [diff] [blame] | 1806 | |
He Kuang | 8132a2a | 2016-06-03 03:33:13 +0000 | [diff] [blame] | 1807 | ret = thread__insert_map(thread, map); |
| 1808 | if (ret) |
| 1809 | goto out_problem_insert; |
| 1810 | |
Arnaldo Carvalho de Melo | b91fc39 | 2015-04-06 20:43:22 -0300 | [diff] [blame] | 1811 | thread__put(thread); |
Arnaldo Carvalho de Melo | 84c2caf | 2015-05-25 16:59:56 -0300 | [diff] [blame] | 1812 | map__put(map); |
Stephane Eranian | 5c5e854 | 2013-08-21 12:10:25 +0200 | [diff] [blame] | 1813 | return 0; |
| 1814 | |
He Kuang | 8132a2a | 2016-06-03 03:33:13 +0000 | [diff] [blame] | 1815 | out_problem_insert: |
| 1816 | map__put(map); |
Arnaldo Carvalho de Melo | b91fc39 | 2015-04-06 20:43:22 -0300 | [diff] [blame] | 1817 | out_problem_map: |
| 1818 | thread__put(thread); |
Stephane Eranian | 5c5e854 | 2013-08-21 12:10:25 +0200 | [diff] [blame] | 1819 | out_problem: |
| 1820 | dump_printf("problem processing PERF_RECORD_MMAP2, skipping event.\n"); |
| 1821 | return 0; |
| 1822 | } |
| 1823 | |
Frederic Weisbecker | 162f0be | 2013-09-11 16:18:24 +0200 | [diff] [blame] | 1824 | int machine__process_mmap_event(struct machine *machine, union perf_event *event, |
Arnaldo Carvalho de Melo | 473398a | 2016-03-22 18:23:43 -0300 | [diff] [blame] | 1825 | struct perf_sample *sample) |
Arnaldo Carvalho de Melo | b0a7d1a | 2012-10-06 16:26:02 -0300 | [diff] [blame] | 1826 | { |
Arnaldo Carvalho de Melo | b0a7d1a | 2012-10-06 16:26:02 -0300 | [diff] [blame] | 1827 | struct thread *thread; |
| 1828 | struct map *map; |
Arnaldo Carvalho de Melo | 0f476f2 | 2018-04-26 11:30:50 -0300 | [diff] [blame] | 1829 | u32 prot = 0; |
Arnaldo Carvalho de Melo | b0a7d1a | 2012-10-06 16:26:02 -0300 | [diff] [blame] | 1830 | int ret = 0; |
| 1831 | |
| 1832 | if (dump_trace) |
| 1833 | perf_event__fprintf_mmap(event, stdout); |
| 1834 | |
Arnaldo Carvalho de Melo | 473398a | 2016-03-22 18:23:43 -0300 | [diff] [blame] | 1835 | if (sample->cpumode == PERF_RECORD_MISC_GUEST_KERNEL || |
| 1836 | sample->cpumode == PERF_RECORD_MISC_KERNEL) { |
Jiri Olsa | 031f112 | 2020-11-26 18:00:12 +0100 | [diff] [blame] | 1837 | struct extra_kernel_map xm = { |
| 1838 | .start = event->mmap.start, |
| 1839 | .end = event->mmap.start + event->mmap.len, |
| 1840 | .pgoff = event->mmap.pgoff, |
| 1841 | }; |
| 1842 | |
| 1843 | strlcpy(xm.name, event->mmap.filename, KMAP_NAME_LEN); |
Jiri Olsa | 1ca6e80 | 2020-12-14 11:54:49 +0100 | [diff] [blame] | 1844 | ret = machine__process_kernel_mmap_event(machine, &xm, NULL); |
Arnaldo Carvalho de Melo | b0a7d1a | 2012-10-06 16:26:02 -0300 | [diff] [blame] | 1845 | if (ret < 0) |
| 1846 | goto out_problem; |
| 1847 | return 0; |
| 1848 | } |
| 1849 | |
Adrian Hunter | 314add6 | 2013-08-27 11:23:03 +0300 | [diff] [blame] | 1850 | thread = machine__findnew_thread(machine, event->mmap.pid, |
Don Zickus | 11c9abf | 2014-02-26 10:45:27 -0500 | [diff] [blame] | 1851 | event->mmap.tid); |
Arnaldo Carvalho de Melo | b0a7d1a | 2012-10-06 16:26:02 -0300 | [diff] [blame] | 1852 | if (thread == NULL) |
| 1853 | goto out_problem; |
Stephane Eranian | bad4091 | 2013-01-24 16:10:40 +0100 | [diff] [blame] | 1854 | |
Arnaldo Carvalho de Melo | 3183f8c | 2018-04-26 16:52:34 -0300 | [diff] [blame] | 1855 | if (!(event->header.misc & PERF_RECORD_MISC_MMAP_DATA)) |
Arnaldo Carvalho de Melo | 0f476f2 | 2018-04-26 11:30:50 -0300 | [diff] [blame] | 1856 | prot = PROT_EXEC; |
Stephane Eranian | bad4091 | 2013-01-24 16:10:40 +0100 | [diff] [blame] | 1857 | |
Adrian Hunter | 2a03068 | 2014-07-22 16:17:53 +0300 | [diff] [blame] | 1858 | map = map__new(machine, event->mmap.start, |
Arnaldo Carvalho de Melo | b0a7d1a | 2012-10-06 16:26:02 -0300 | [diff] [blame] | 1859 | event->mmap.len, event->mmap.pgoff, |
Jiri Olsa | 1ca6e80 | 2020-12-14 11:54:49 +0100 | [diff] [blame] | 1860 | NULL, prot, 0, NULL, event->mmap.filename, thread); |
Stephane Eranian | bad4091 | 2013-01-24 16:10:40 +0100 | [diff] [blame] | 1861 | |
Arnaldo Carvalho de Melo | b0a7d1a | 2012-10-06 16:26:02 -0300 | [diff] [blame] | 1862 | if (map == NULL) |
Arnaldo Carvalho de Melo | b91fc39 | 2015-04-06 20:43:22 -0300 | [diff] [blame] | 1863 | goto out_problem_map; |
Arnaldo Carvalho de Melo | b0a7d1a | 2012-10-06 16:26:02 -0300 | [diff] [blame] | 1864 | |
He Kuang | 8132a2a | 2016-06-03 03:33:13 +0000 | [diff] [blame] | 1865 | ret = thread__insert_map(thread, map); |
| 1866 | if (ret) |
| 1867 | goto out_problem_insert; |
| 1868 | |
Arnaldo Carvalho de Melo | b91fc39 | 2015-04-06 20:43:22 -0300 | [diff] [blame] | 1869 | thread__put(thread); |
Arnaldo Carvalho de Melo | 84c2caf | 2015-05-25 16:59:56 -0300 | [diff] [blame] | 1870 | map__put(map); |
Arnaldo Carvalho de Melo | b0a7d1a | 2012-10-06 16:26:02 -0300 | [diff] [blame] | 1871 | return 0; |
| 1872 | |
He Kuang | 8132a2a | 2016-06-03 03:33:13 +0000 | [diff] [blame] | 1873 | out_problem_insert: |
| 1874 | map__put(map); |
Arnaldo Carvalho de Melo | b91fc39 | 2015-04-06 20:43:22 -0300 | [diff] [blame] | 1875 | out_problem_map: |
| 1876 | thread__put(thread); |
Arnaldo Carvalho de Melo | b0a7d1a | 2012-10-06 16:26:02 -0300 | [diff] [blame] | 1877 | out_problem: |
| 1878 | dump_printf("problem processing PERF_RECORD_MMAP, skipping event.\n"); |
| 1879 | return 0; |
| 1880 | } |
| 1881 | |
Arnaldo Carvalho de Melo | b91fc39 | 2015-04-06 20:43:22 -0300 | [diff] [blame] | 1882 | static void __machine__remove_thread(struct machine *machine, struct thread *th, bool lock) |
David Ahern | 236a3bb | 2013-08-14 08:49:27 -0600 | [diff] [blame] | 1883 | { |
Kan Liang | 91e467b | 2017-09-10 19:23:14 -0700 | [diff] [blame] | 1884 | struct threads *threads = machine__threads(machine, th->tid); |
| 1885 | |
| 1886 | if (threads->last_match == th) |
Jiri Olsa | 67fda0f | 2018-07-19 16:33:43 +0200 | [diff] [blame] | 1887 | threads__set_last_match(threads, NULL); |
Arnaldo Carvalho de Melo | f3b623b | 2015-03-02 22:21:35 -0300 | [diff] [blame] | 1888 | |
Arnaldo Carvalho de Melo | b91fc39 | 2015-04-06 20:43:22 -0300 | [diff] [blame] | 1889 | if (lock) |
Arnaldo Carvalho de Melo | 0a7c74e | 2017-04-04 13:15:04 -0300 | [diff] [blame] | 1890 | down_write(&threads->lock); |
Arnaldo Carvalho de Melo | 4c00af0 | 2019-07-05 12:11:35 -0300 | [diff] [blame] | 1891 | |
| 1892 | BUG_ON(refcount_read(&th->refcnt) == 0); |
| 1893 | |
Davidlohr Bueso | f3acb3a | 2018-12-06 11:18:14 -0800 | [diff] [blame] | 1894 | rb_erase_cached(&th->rb_node, &threads->entries); |
Arnaldo Carvalho de Melo | b91fc39 | 2015-04-06 20:43:22 -0300 | [diff] [blame] | 1895 | RB_CLEAR_NODE(&th->rb_node); |
Kan Liang | 91e467b | 2017-09-10 19:23:14 -0700 | [diff] [blame] | 1896 | --threads->nr; |
David Ahern | 236a3bb | 2013-08-14 08:49:27 -0600 | [diff] [blame] | 1897 | /* |
Arnaldo Carvalho de Melo | f3b623b | 2015-03-02 22:21:35 -0300 | [diff] [blame] | 1898 | * Move it first to the dead_threads list, then drop the reference, |
| 1899 | * if this is the last reference, then the thread__delete destructor |
| 1900 | * will be called and we will remove it from the dead_threads list. |
David Ahern | 236a3bb | 2013-08-14 08:49:27 -0600 | [diff] [blame] | 1901 | */ |
Kan Liang | 91e467b | 2017-09-10 19:23:14 -0700 | [diff] [blame] | 1902 | list_add_tail(&th->node, &threads->dead); |
Arnaldo Carvalho de Melo | 4c00af0 | 2019-07-05 12:11:35 -0300 | [diff] [blame] | 1903 | |
| 1904 | /* |
| 1905 | * We need to do the put here because if this is the last refcount, |
| 1906 | * then we will be touching the threads->dead head when removing the |
| 1907 | * thread. |
| 1908 | */ |
| 1909 | thread__put(th); |
| 1910 | |
Arnaldo Carvalho de Melo | b91fc39 | 2015-04-06 20:43:22 -0300 | [diff] [blame] | 1911 | if (lock) |
Arnaldo Carvalho de Melo | 0a7c74e | 2017-04-04 13:15:04 -0300 | [diff] [blame] | 1912 | up_write(&threads->lock); |
David Ahern | 236a3bb | 2013-08-14 08:49:27 -0600 | [diff] [blame] | 1913 | } |
| 1914 | |
Arnaldo Carvalho de Melo | b91fc39 | 2015-04-06 20:43:22 -0300 | [diff] [blame] | 1915 | void machine__remove_thread(struct machine *machine, struct thread *th) |
| 1916 | { |
| 1917 | return __machine__remove_thread(machine, th, true); |
| 1918 | } |
| 1919 | |
Frederic Weisbecker | 162f0be | 2013-09-11 16:18:24 +0200 | [diff] [blame] | 1920 | int machine__process_fork_event(struct machine *machine, union perf_event *event, |
| 1921 | struct perf_sample *sample) |
Arnaldo Carvalho de Melo | b0a7d1a | 2012-10-06 16:26:02 -0300 | [diff] [blame] | 1922 | { |
Jiri Olsa | d75e609 | 2014-03-14 15:00:03 +0100 | [diff] [blame] | 1923 | struct thread *thread = machine__find_thread(machine, |
| 1924 | event->fork.pid, |
| 1925 | event->fork.tid); |
Adrian Hunter | 314add6 | 2013-08-27 11:23:03 +0300 | [diff] [blame] | 1926 | struct thread *parent = machine__findnew_thread(machine, |
| 1927 | event->fork.ppid, |
| 1928 | event->fork.ptid); |
David Miller | 4f8f382 | 2018-10-30 22:24:04 -0700 | [diff] [blame] | 1929 | bool do_maps_clone = true; |
Arnaldo Carvalho de Melo | b91fc39 | 2015-04-06 20:43:22 -0300 | [diff] [blame] | 1930 | int err = 0; |
Arnaldo Carvalho de Melo | b0a7d1a | 2012-10-06 16:26:02 -0300 | [diff] [blame] | 1931 | |
Adrian Hunter | 5cb7334 | 2015-08-19 17:29:20 +0300 | [diff] [blame] | 1932 | if (dump_trace) |
| 1933 | perf_event__fprintf_task(event, stdout); |
| 1934 | |
| 1935 | /* |
| 1936 | * There may be an existing thread that is not actually the parent, |
| 1937 | * either because we are processing events out of order, or because the |
| 1938 | * (fork) event that would have removed the thread was lost. Assume the |
| 1939 | * latter case and continue on as best we can. |
| 1940 | */ |
| 1941 | if (parent->pid_ != (pid_t)event->fork.ppid) { |
| 1942 | dump_printf("removing erroneous parent thread %d/%d\n", |
| 1943 | parent->pid_, parent->tid); |
| 1944 | machine__remove_thread(machine, parent); |
| 1945 | thread__put(parent); |
| 1946 | parent = machine__findnew_thread(machine, event->fork.ppid, |
| 1947 | event->fork.ptid); |
| 1948 | } |
| 1949 | |
David Ahern | 236a3bb | 2013-08-14 08:49:27 -0600 | [diff] [blame] | 1950 | /* if a thread currently exists for the thread id remove it */ |
Arnaldo Carvalho de Melo | b91fc39 | 2015-04-06 20:43:22 -0300 | [diff] [blame] | 1951 | if (thread != NULL) { |
David Ahern | 236a3bb | 2013-08-14 08:49:27 -0600 | [diff] [blame] | 1952 | machine__remove_thread(machine, thread); |
Arnaldo Carvalho de Melo | b91fc39 | 2015-04-06 20:43:22 -0300 | [diff] [blame] | 1953 | thread__put(thread); |
| 1954 | } |
David Ahern | 236a3bb | 2013-08-14 08:49:27 -0600 | [diff] [blame] | 1955 | |
Adrian Hunter | 314add6 | 2013-08-27 11:23:03 +0300 | [diff] [blame] | 1956 | thread = machine__findnew_thread(machine, event->fork.pid, |
| 1957 | event->fork.tid); |
David Miller | 4f8f382 | 2018-10-30 22:24:04 -0700 | [diff] [blame] | 1958 | /* |
| 1959 | * When synthesizing FORK events, we are trying to create thread |
| 1960 | * objects for the already running tasks on the machine. |
| 1961 | * |
| 1962 | * Normally, for a kernel FORK event, we want to clone the parent's |
| 1963 | * maps because that is what the kernel just did. |
| 1964 | * |
| 1965 | * But when synthesizing, this should not be done. If we do, we end up |
Ingo Molnar | 4d39c89 | 2021-03-23 17:09:15 +0100 | [diff] [blame] | 1966 | * with overlapping maps as we process the synthesized MMAP2 events that |
David Miller | 4f8f382 | 2018-10-30 22:24:04 -0700 | [diff] [blame] | 1967 | * get delivered shortly thereafter. |
| 1968 | * |
| 1969 | * Use the FORK event misc flags in an internal way to signal this |
| 1970 | * situation, so we can elide the map clone when appropriate. |
| 1971 | */ |
| 1972 | if (event->fork.header.misc & PERF_RECORD_MISC_FORK_EXEC) |
| 1973 | do_maps_clone = false; |
Arnaldo Carvalho de Melo | b0a7d1a | 2012-10-06 16:26:02 -0300 | [diff] [blame] | 1974 | |
| 1975 | if (thread == NULL || parent == NULL || |
David Miller | 4f8f382 | 2018-10-30 22:24:04 -0700 | [diff] [blame] | 1976 | thread__fork(thread, parent, sample->time, do_maps_clone) < 0) { |
Arnaldo Carvalho de Melo | b0a7d1a | 2012-10-06 16:26:02 -0300 | [diff] [blame] | 1977 | dump_printf("problem processing PERF_RECORD_FORK, skipping event.\n"); |
Arnaldo Carvalho de Melo | b91fc39 | 2015-04-06 20:43:22 -0300 | [diff] [blame] | 1978 | err = -1; |
Arnaldo Carvalho de Melo | b0a7d1a | 2012-10-06 16:26:02 -0300 | [diff] [blame] | 1979 | } |
Arnaldo Carvalho de Melo | b91fc39 | 2015-04-06 20:43:22 -0300 | [diff] [blame] | 1980 | thread__put(thread); |
| 1981 | thread__put(parent); |
Arnaldo Carvalho de Melo | b0a7d1a | 2012-10-06 16:26:02 -0300 | [diff] [blame] | 1982 | |
Arnaldo Carvalho de Melo | b91fc39 | 2015-04-06 20:43:22 -0300 | [diff] [blame] | 1983 | return err; |
Arnaldo Carvalho de Melo | b0a7d1a | 2012-10-06 16:26:02 -0300 | [diff] [blame] | 1984 | } |
| 1985 | |
Frederic Weisbecker | 162f0be | 2013-09-11 16:18:24 +0200 | [diff] [blame] | 1986 | int machine__process_exit_event(struct machine *machine, union perf_event *event, |
| 1987 | struct perf_sample *sample __maybe_unused) |
Arnaldo Carvalho de Melo | b0a7d1a | 2012-10-06 16:26:02 -0300 | [diff] [blame] | 1988 | { |
Jiri Olsa | d75e609 | 2014-03-14 15:00:03 +0100 | [diff] [blame] | 1989 | struct thread *thread = machine__find_thread(machine, |
| 1990 | event->fork.pid, |
| 1991 | event->fork.tid); |
Arnaldo Carvalho de Melo | b0a7d1a | 2012-10-06 16:26:02 -0300 | [diff] [blame] | 1992 | |
| 1993 | if (dump_trace) |
| 1994 | perf_event__fprintf_task(event, stdout); |
| 1995 | |
Arnaldo Carvalho de Melo | b91fc39 | 2015-04-06 20:43:22 -0300 | [diff] [blame] | 1996 | if (thread != NULL) { |
David Ahern | 236a3bb | 2013-08-14 08:49:27 -0600 | [diff] [blame] | 1997 | thread__exited(thread); |
Arnaldo Carvalho de Melo | b91fc39 | 2015-04-06 20:43:22 -0300 | [diff] [blame] | 1998 | thread__put(thread); |
| 1999 | } |
Arnaldo Carvalho de Melo | b0a7d1a | 2012-10-06 16:26:02 -0300 | [diff] [blame] | 2000 | |
| 2001 | return 0; |
| 2002 | } |
| 2003 | |
Frederic Weisbecker | 162f0be | 2013-09-11 16:18:24 +0200 | [diff] [blame] | 2004 | int machine__process_event(struct machine *machine, union perf_event *event, |
| 2005 | struct perf_sample *sample) |
Arnaldo Carvalho de Melo | b0a7d1a | 2012-10-06 16:26:02 -0300 | [diff] [blame] | 2006 | { |
| 2007 | int ret; |
| 2008 | |
| 2009 | switch (event->header.type) { |
| 2010 | case PERF_RECORD_COMM: |
Frederic Weisbecker | 162f0be | 2013-09-11 16:18:24 +0200 | [diff] [blame] | 2011 | ret = machine__process_comm_event(machine, event, sample); break; |
Arnaldo Carvalho de Melo | b0a7d1a | 2012-10-06 16:26:02 -0300 | [diff] [blame] | 2012 | case PERF_RECORD_MMAP: |
Frederic Weisbecker | 162f0be | 2013-09-11 16:18:24 +0200 | [diff] [blame] | 2013 | ret = machine__process_mmap_event(machine, event, sample); break; |
Hari Bathini | f3b3614 | 2017-03-08 02:11:43 +0530 | [diff] [blame] | 2014 | case PERF_RECORD_NAMESPACES: |
| 2015 | ret = machine__process_namespaces_event(machine, event, sample); break; |
Namhyung Kim | ba78c1c | 2020-03-25 21:45:30 +0900 | [diff] [blame] | 2016 | case PERF_RECORD_CGROUP: |
| 2017 | ret = machine__process_cgroup_event(machine, event, sample); break; |
Stephane Eranian | 5c5e854 | 2013-08-21 12:10:25 +0200 | [diff] [blame] | 2018 | case PERF_RECORD_MMAP2: |
Frederic Weisbecker | 162f0be | 2013-09-11 16:18:24 +0200 | [diff] [blame] | 2019 | ret = machine__process_mmap2_event(machine, event, sample); break; |
Arnaldo Carvalho de Melo | b0a7d1a | 2012-10-06 16:26:02 -0300 | [diff] [blame] | 2020 | case PERF_RECORD_FORK: |
Frederic Weisbecker | 162f0be | 2013-09-11 16:18:24 +0200 | [diff] [blame] | 2021 | ret = machine__process_fork_event(machine, event, sample); break; |
Arnaldo Carvalho de Melo | b0a7d1a | 2012-10-06 16:26:02 -0300 | [diff] [blame] | 2022 | case PERF_RECORD_EXIT: |
Frederic Weisbecker | 162f0be | 2013-09-11 16:18:24 +0200 | [diff] [blame] | 2023 | ret = machine__process_exit_event(machine, event, sample); break; |
Arnaldo Carvalho de Melo | b0a7d1a | 2012-10-06 16:26:02 -0300 | [diff] [blame] | 2024 | case PERF_RECORD_LOST: |
Frederic Weisbecker | 162f0be | 2013-09-11 16:18:24 +0200 | [diff] [blame] | 2025 | ret = machine__process_lost_event(machine, event, sample); break; |
Adrian Hunter | 4a96f7a | 2015-04-30 17:37:29 +0300 | [diff] [blame] | 2026 | case PERF_RECORD_AUX: |
| 2027 | ret = machine__process_aux_event(machine, event); break; |
Adrian Hunter | 0ad21f6 | 2015-04-30 17:37:30 +0300 | [diff] [blame] | 2028 | case PERF_RECORD_ITRACE_START: |
Jiri Olsa | ceb9291 | 2015-06-29 13:27:45 +0200 | [diff] [blame] | 2029 | ret = machine__process_itrace_start_event(machine, event); break; |
Kan Liang | c4937a9 | 2015-05-10 15:13:15 -0400 | [diff] [blame] | 2030 | case PERF_RECORD_LOST_SAMPLES: |
| 2031 | ret = machine__process_lost_samples_event(machine, event, sample); break; |
Adrian Hunter | 0286039 | 2015-07-21 12:44:03 +0300 | [diff] [blame] | 2032 | case PERF_RECORD_SWITCH: |
| 2033 | case PERF_RECORD_SWITCH_CPU_WIDE: |
| 2034 | ret = machine__process_switch_event(machine, event); break; |
Song Liu | 9aa0bfa | 2019-01-17 08:15:17 -0800 | [diff] [blame] | 2035 | case PERF_RECORD_KSYMBOL: |
| 2036 | ret = machine__process_ksymbol(machine, event, sample); break; |
Song Liu | 45178a9 | 2019-01-17 08:15:18 -0800 | [diff] [blame] | 2037 | case PERF_RECORD_BPF_EVENT: |
Arnaldo Carvalho de Melo | 3f604b5 | 2019-08-26 19:28:13 -0300 | [diff] [blame] | 2038 | ret = machine__process_bpf(machine, event, sample); break; |
Adrian Hunter | 246eba8 | 2020-05-12 15:19:18 +0300 | [diff] [blame] | 2039 | case PERF_RECORD_TEXT_POKE: |
| 2040 | ret = machine__process_text_poke(machine, event, sample); break; |
Adrian Hunter | 6175047 | 2021-09-07 19:39:02 +0300 | [diff] [blame] | 2041 | case PERF_RECORD_AUX_OUTPUT_HW_ID: |
| 2042 | ret = machine__process_aux_output_hw_id_event(machine, event); break; |
Arnaldo Carvalho de Melo | b0a7d1a | 2012-10-06 16:26:02 -0300 | [diff] [blame] | 2043 | default: |
| 2044 | ret = -1; |
| 2045 | break; |
| 2046 | } |
| 2047 | |
| 2048 | return ret; |
| 2049 | } |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 2050 | |
Greg Price | b21484f | 2012-12-06 21:48:05 -0800 | [diff] [blame] | 2051 | static bool symbol__match_regex(struct symbol *sym, regex_t *regex) |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 2052 | { |
Arnaldo Carvalho de Melo | a7c3899 | 2017-02-13 16:52:15 -0300 | [diff] [blame] | 2053 | if (!regexec(regex, sym->name, 0, NULL, 0)) |
Jiapeng Chong | 83ff0f9 | 2021-03-09 18:11:09 +0800 | [diff] [blame] | 2054 | return true; |
| 2055 | return false; |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 2056 | } |
| 2057 | |
Arnaldo Carvalho de Melo | bb871a9 | 2014-10-23 12:50:25 -0300 | [diff] [blame] | 2058 | static void ip__resolve_ams(struct thread *thread, |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 2059 | struct addr_map_symbol *ams, |
| 2060 | u64 ip) |
| 2061 | { |
| 2062 | struct addr_location al; |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 2063 | |
| 2064 | memset(&al, 0, sizeof(al)); |
Arnaldo Carvalho de Melo | 52a3cb8 | 2014-03-11 16:16:49 -0300 | [diff] [blame] | 2065 | /* |
| 2066 | * We cannot use the header.misc hint to determine whether a |
| 2067 | * branch stack address is user, kernel, guest, hypervisor. |
| 2068 | * Branches may straddle the kernel/user/hypervisor boundaries. |
| 2069 | * Thus, we have to try consecutively until we find a match |
| 2070 | * or else, the symbol is unknown |
| 2071 | */ |
Arnaldo Carvalho de Melo | 26bd933 | 2018-04-25 17:58:03 -0300 | [diff] [blame] | 2072 | thread__find_cpumode_addr_location(thread, ip, &al); |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 2073 | |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 2074 | ams->addr = ip; |
| 2075 | ams->al_addr = al.addr; |
Arnaldo Carvalho de Melo | f2eaea0 | 2019-11-25 22:15:35 -0300 | [diff] [blame] | 2076 | ams->ms.maps = al.maps; |
Arnaldo Carvalho de Melo | d46a4cd | 2019-11-04 15:57:38 -0300 | [diff] [blame] | 2077 | ams->ms.sym = al.sym; |
| 2078 | ams->ms.map = al.map; |
Kan Liang | 8780fb2 | 2017-08-29 13:11:09 -0400 | [diff] [blame] | 2079 | ams->phys_addr = 0; |
Kan Liang | a50d03e | 2020-12-16 10:57:58 -0800 | [diff] [blame] | 2080 | ams->data_page_size = 0; |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 2081 | } |
| 2082 | |
Arnaldo Carvalho de Melo | bb871a9 | 2014-10-23 12:50:25 -0300 | [diff] [blame] | 2083 | static void ip__resolve_data(struct thread *thread, |
Kan Liang | 8780fb2 | 2017-08-29 13:11:09 -0400 | [diff] [blame] | 2084 | u8 m, struct addr_map_symbol *ams, |
Kan Liang | a50d03e | 2020-12-16 10:57:58 -0800 | [diff] [blame] | 2085 | u64 addr, u64 phys_addr, u64 daddr_page_size) |
Stephane Eranian | 98a3b32 | 2013-01-24 16:10:35 +0100 | [diff] [blame] | 2086 | { |
| 2087 | struct addr_location al; |
| 2088 | |
| 2089 | memset(&al, 0, sizeof(al)); |
| 2090 | |
Arnaldo Carvalho de Melo | 117d3c2 | 2018-04-25 18:16:53 -0300 | [diff] [blame] | 2091 | thread__find_symbol(thread, m, addr, &al); |
Don Zickus | 06b2afc | 2014-08-20 23:25:11 -0400 | [diff] [blame] | 2092 | |
Stephane Eranian | 98a3b32 | 2013-01-24 16:10:35 +0100 | [diff] [blame] | 2093 | ams->addr = addr; |
| 2094 | ams->al_addr = al.addr; |
Arnaldo Carvalho de Melo | f2eaea0 | 2019-11-25 22:15:35 -0300 | [diff] [blame] | 2095 | ams->ms.maps = al.maps; |
Arnaldo Carvalho de Melo | d46a4cd | 2019-11-04 15:57:38 -0300 | [diff] [blame] | 2096 | ams->ms.sym = al.sym; |
| 2097 | ams->ms.map = al.map; |
Kan Liang | 8780fb2 | 2017-08-29 13:11:09 -0400 | [diff] [blame] | 2098 | ams->phys_addr = phys_addr; |
Kan Liang | a50d03e | 2020-12-16 10:57:58 -0800 | [diff] [blame] | 2099 | ams->data_page_size = daddr_page_size; |
Stephane Eranian | 98a3b32 | 2013-01-24 16:10:35 +0100 | [diff] [blame] | 2100 | } |
| 2101 | |
Arnaldo Carvalho de Melo | e80faac | 2014-01-22 13:05:06 -0300 | [diff] [blame] | 2102 | struct mem_info *sample__resolve_mem(struct perf_sample *sample, |
| 2103 | struct addr_location *al) |
Stephane Eranian | 98a3b32 | 2013-01-24 16:10:35 +0100 | [diff] [blame] | 2104 | { |
Jiri Olsa | 9f87498 | 2018-03-07 16:50:06 +0100 | [diff] [blame] | 2105 | struct mem_info *mi = mem_info__new(); |
Stephane Eranian | 98a3b32 | 2013-01-24 16:10:35 +0100 | [diff] [blame] | 2106 | |
| 2107 | if (!mi) |
| 2108 | return NULL; |
| 2109 | |
Arnaldo Carvalho de Melo | bb871a9 | 2014-10-23 12:50:25 -0300 | [diff] [blame] | 2110 | ip__resolve_ams(al->thread, &mi->iaddr, sample->ip); |
Kan Liang | 8780fb2 | 2017-08-29 13:11:09 -0400 | [diff] [blame] | 2111 | ip__resolve_data(al->thread, al->cpumode, &mi->daddr, |
Kan Liang | a50d03e | 2020-12-16 10:57:58 -0800 | [diff] [blame] | 2112 | sample->addr, sample->phys_addr, |
| 2113 | sample->data_page_size); |
Stephane Eranian | 98a3b32 | 2013-01-24 16:10:35 +0100 | [diff] [blame] | 2114 | mi->data_src.val = sample->data_src; |
| 2115 | |
| 2116 | return mi; |
| 2117 | } |
| 2118 | |
Arnaldo Carvalho de Melo | 5f0fef8 | 2019-11-04 12:14:32 -0300 | [diff] [blame] | 2119 | static char *callchain_srcline(struct map_symbol *ms, u64 ip) |
Milian Wolff | 40a342c | 2017-10-09 22:32:56 +0200 | [diff] [blame] | 2120 | { |
Arnaldo Carvalho de Melo | 5f0fef8 | 2019-11-04 12:14:32 -0300 | [diff] [blame] | 2121 | struct map *map = ms->map; |
Milian Wolff | 21ac9d5 | 2017-10-19 13:38:34 +0200 | [diff] [blame] | 2122 | char *srcline = NULL; |
Milian Wolff | 40a342c | 2017-10-09 22:32:56 +0200 | [diff] [blame] | 2123 | |
Milian Wolff | 21ac9d5 | 2017-10-19 13:38:34 +0200 | [diff] [blame] | 2124 | if (!map || callchain_param.key == CCKEY_FUNCTION) |
| 2125 | return srcline; |
| 2126 | |
| 2127 | srcline = srcline__tree_find(&map->dso->srclines, ip); |
| 2128 | if (!srcline) { |
| 2129 | bool show_sym = false; |
| 2130 | bool show_addr = callchain_param.key == CCKEY_ADDRESS; |
| 2131 | |
| 2132 | srcline = get_srcline(map->dso, map__rip_2objdump(map, ip), |
Arnaldo Carvalho de Melo | 5f0fef8 | 2019-11-04 12:14:32 -0300 | [diff] [blame] | 2133 | ms->sym, show_sym, show_addr, ip); |
Milian Wolff | 21ac9d5 | 2017-10-19 13:38:34 +0200 | [diff] [blame] | 2134 | srcline__tree_insert(&map->dso->srclines, ip, srcline); |
| 2135 | } |
| 2136 | |
| 2137 | return srcline; |
Milian Wolff | 40a342c | 2017-10-09 22:32:56 +0200 | [diff] [blame] | 2138 | } |
| 2139 | |
Jin Yao | c4ee062 | 2017-08-07 21:05:15 +0800 | [diff] [blame] | 2140 | struct iterations { |
| 2141 | int nr_loop_iter; |
| 2142 | u64 cycles; |
| 2143 | }; |
| 2144 | |
Andi Kleen | 37592b8 | 2014-11-12 18:05:19 -0800 | [diff] [blame] | 2145 | static int add_callchain_ip(struct thread *thread, |
Arnaldo Carvalho de Melo | 91d7b2d | 2016-04-14 14:48:07 -0300 | [diff] [blame] | 2146 | struct callchain_cursor *cursor, |
Andi Kleen | 37592b8 | 2014-11-12 18:05:19 -0800 | [diff] [blame] | 2147 | struct symbol **parent, |
| 2148 | struct addr_location *root_al, |
David Hildenbrand | 73dbcd6 | 2015-03-30 10:11:00 +0200 | [diff] [blame] | 2149 | u8 *cpumode, |
Jin Yao | 410024d | 2016-10-31 09:19:49 +0800 | [diff] [blame] | 2150 | u64 ip, |
| 2151 | bool branch, |
| 2152 | struct branch_flags *flags, |
Jin Yao | c4ee062 | 2017-08-07 21:05:15 +0800 | [diff] [blame] | 2153 | struct iterations *iter, |
Jin Yao | b851dd4 | 2017-07-18 20:13:15 +0800 | [diff] [blame] | 2154 | u64 branch_from) |
Andi Kleen | 37592b8 | 2014-11-12 18:05:19 -0800 | [diff] [blame] | 2155 | { |
Arnaldo Carvalho de Melo | 5f0fef8 | 2019-11-04 12:14:32 -0300 | [diff] [blame] | 2156 | struct map_symbol ms; |
Andi Kleen | 37592b8 | 2014-11-12 18:05:19 -0800 | [diff] [blame] | 2157 | struct addr_location al; |
Jin Yao | c4ee062 | 2017-08-07 21:05:15 +0800 | [diff] [blame] | 2158 | int nr_loop_iter = 0; |
| 2159 | u64 iter_cycles = 0; |
Milian Wolff | 40a342c | 2017-10-09 22:32:56 +0200 | [diff] [blame] | 2160 | const char *srcline = NULL; |
Andi Kleen | 37592b8 | 2014-11-12 18:05:19 -0800 | [diff] [blame] | 2161 | |
| 2162 | al.filtered = 0; |
| 2163 | al.sym = NULL; |
Michael Petlan | 57f0ff0 | 2021-07-19 16:53:32 +0200 | [diff] [blame] | 2164 | al.srcline = NULL; |
David Hildenbrand | 73dbcd6 | 2015-03-30 10:11:00 +0200 | [diff] [blame] | 2165 | if (!cpumode) { |
Arnaldo Carvalho de Melo | 26bd933 | 2018-04-25 17:58:03 -0300 | [diff] [blame] | 2166 | thread__find_cpumode_addr_location(thread, ip, &al); |
David Hildenbrand | 73dbcd6 | 2015-03-30 10:11:00 +0200 | [diff] [blame] | 2167 | } else { |
Kan Liang | 2e77784 | 2014-12-02 10:06:53 -0500 | [diff] [blame] | 2168 | if (ip >= PERF_CONTEXT_MAX) { |
| 2169 | switch (ip) { |
| 2170 | case PERF_CONTEXT_HV: |
David Hildenbrand | 73dbcd6 | 2015-03-30 10:11:00 +0200 | [diff] [blame] | 2171 | *cpumode = PERF_RECORD_MISC_HYPERVISOR; |
Kan Liang | 2e77784 | 2014-12-02 10:06:53 -0500 | [diff] [blame] | 2172 | break; |
| 2173 | case PERF_CONTEXT_KERNEL: |
David Hildenbrand | 73dbcd6 | 2015-03-30 10:11:00 +0200 | [diff] [blame] | 2174 | *cpumode = PERF_RECORD_MISC_KERNEL; |
Kan Liang | 2e77784 | 2014-12-02 10:06:53 -0500 | [diff] [blame] | 2175 | break; |
| 2176 | case PERF_CONTEXT_USER: |
David Hildenbrand | 73dbcd6 | 2015-03-30 10:11:00 +0200 | [diff] [blame] | 2177 | *cpumode = PERF_RECORD_MISC_USER; |
Kan Liang | 2e77784 | 2014-12-02 10:06:53 -0500 | [diff] [blame] | 2178 | break; |
| 2179 | default: |
| 2180 | pr_debug("invalid callchain context: " |
| 2181 | "%"PRId64"\n", (s64) ip); |
| 2182 | /* |
| 2183 | * It seems the callchain is corrupted. |
| 2184 | * Discard all. |
| 2185 | */ |
Arnaldo Carvalho de Melo | 91d7b2d | 2016-04-14 14:48:07 -0300 | [diff] [blame] | 2186 | callchain_cursor_reset(cursor); |
Kan Liang | 2e77784 | 2014-12-02 10:06:53 -0500 | [diff] [blame] | 2187 | return 1; |
| 2188 | } |
| 2189 | return 0; |
| 2190 | } |
Arnaldo Carvalho de Melo | 4546263 | 2018-04-24 11:24:49 -0300 | [diff] [blame] | 2191 | thread__find_symbol(thread, *cpumode, ip, &al); |
Kan Liang | 2e77784 | 2014-12-02 10:06:53 -0500 | [diff] [blame] | 2192 | } |
| 2193 | |
Andi Kleen | 37592b8 | 2014-11-12 18:05:19 -0800 | [diff] [blame] | 2194 | if (al.sym != NULL) { |
Jiri Olsa | de7e6a7 | 2016-05-03 13:54:43 +0200 | [diff] [blame] | 2195 | if (perf_hpp_list.parent && !*parent && |
Andi Kleen | 37592b8 | 2014-11-12 18:05:19 -0800 | [diff] [blame] | 2196 | symbol__match_regex(al.sym, &parent_regex)) |
| 2197 | *parent = al.sym; |
| 2198 | else if (have_ignore_callees && root_al && |
| 2199 | symbol__match_regex(al.sym, &ignore_callees_regex)) { |
| 2200 | /* Treat this symbol as the root, |
| 2201 | forgetting its callees. */ |
| 2202 | *root_al = al; |
Arnaldo Carvalho de Melo | 91d7b2d | 2016-04-14 14:48:07 -0300 | [diff] [blame] | 2203 | callchain_cursor_reset(cursor); |
Andi Kleen | 37592b8 | 2014-11-12 18:05:19 -0800 | [diff] [blame] | 2204 | } |
| 2205 | } |
| 2206 | |
Namhyung Kim | b49a8fe | 2015-11-26 16:08:20 +0900 | [diff] [blame] | 2207 | if (symbol_conf.hide_unresolved && al.sym == NULL) |
| 2208 | return 0; |
Jin Yao | c4ee062 | 2017-08-07 21:05:15 +0800 | [diff] [blame] | 2209 | |
| 2210 | if (iter) { |
| 2211 | nr_loop_iter = iter->nr_loop_iter; |
| 2212 | iter_cycles = iter->cycles; |
| 2213 | } |
| 2214 | |
Arnaldo Carvalho de Melo | f2eaea0 | 2019-11-25 22:15:35 -0300 | [diff] [blame] | 2215 | ms.maps = al.maps; |
Arnaldo Carvalho de Melo | 5f0fef8 | 2019-11-04 12:14:32 -0300 | [diff] [blame] | 2216 | ms.map = al.map; |
| 2217 | ms.sym = al.sym; |
| 2218 | srcline = callchain_srcline(&ms, al.addr); |
| 2219 | return callchain_cursor_append(cursor, ip, &ms, |
Jin Yao | c4ee062 | 2017-08-07 21:05:15 +0800 | [diff] [blame] | 2220 | branch, flags, nr_loop_iter, |
Milian Wolff | 40a342c | 2017-10-09 22:32:56 +0200 | [diff] [blame] | 2221 | iter_cycles, branch_from, srcline); |
Andi Kleen | 37592b8 | 2014-11-12 18:05:19 -0800 | [diff] [blame] | 2222 | } |
| 2223 | |
Arnaldo Carvalho de Melo | 644f2df | 2014-01-22 13:15:36 -0300 | [diff] [blame] | 2224 | struct branch_info *sample__resolve_bstack(struct perf_sample *sample, |
| 2225 | struct addr_location *al) |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 2226 | { |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 2227 | unsigned int i; |
Arnaldo Carvalho de Melo | 644f2df | 2014-01-22 13:15:36 -0300 | [diff] [blame] | 2228 | const struct branch_stack *bs = sample->branch_stack; |
Kan Liang | 42bbabe | 2020-02-28 08:30:00 -0800 | [diff] [blame] | 2229 | struct branch_entry *entries = perf_sample__branch_entries(sample); |
Arnaldo Carvalho de Melo | 644f2df | 2014-01-22 13:15:36 -0300 | [diff] [blame] | 2230 | struct branch_info *bi = calloc(bs->nr, sizeof(struct branch_info)); |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 2231 | |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 2232 | if (!bi) |
| 2233 | return NULL; |
| 2234 | |
| 2235 | for (i = 0; i < bs->nr; i++) { |
Kan Liang | 42bbabe | 2020-02-28 08:30:00 -0800 | [diff] [blame] | 2236 | ip__resolve_ams(al->thread, &bi[i].to, entries[i].to); |
| 2237 | ip__resolve_ams(al->thread, &bi[i].from, entries[i].from); |
| 2238 | bi[i].flags = entries[i].flags; |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 2239 | } |
| 2240 | return bi; |
| 2241 | } |
| 2242 | |
Jin Yao | c4ee062 | 2017-08-07 21:05:15 +0800 | [diff] [blame] | 2243 | static void save_iterations(struct iterations *iter, |
| 2244 | struct branch_entry *be, int nr) |
| 2245 | { |
| 2246 | int i; |
| 2247 | |
Jin Yao | a3366db0 | 2019-01-04 14:10:30 +0800 | [diff] [blame] | 2248 | iter->nr_loop_iter++; |
Jin Yao | c4ee062 | 2017-08-07 21:05:15 +0800 | [diff] [blame] | 2249 | iter->cycles = 0; |
| 2250 | |
| 2251 | for (i = 0; i < nr; i++) |
| 2252 | iter->cycles += be[i].flags.cycles; |
| 2253 | } |
| 2254 | |
Andi Kleen | 8b7bad5 | 2014-11-12 18:05:20 -0800 | [diff] [blame] | 2255 | #define CHASHSZ 127 |
| 2256 | #define CHASHBITS 7 |
| 2257 | #define NO_ENTRY 0xff |
| 2258 | |
| 2259 | #define PERF_MAX_BRANCH_DEPTH 127 |
| 2260 | |
| 2261 | /* Remove loops. */ |
Jin Yao | c4ee062 | 2017-08-07 21:05:15 +0800 | [diff] [blame] | 2262 | static int remove_loops(struct branch_entry *l, int nr, |
| 2263 | struct iterations *iter) |
Andi Kleen | 8b7bad5 | 2014-11-12 18:05:20 -0800 | [diff] [blame] | 2264 | { |
| 2265 | int i, j, off; |
| 2266 | unsigned char chash[CHASHSZ]; |
| 2267 | |
| 2268 | memset(chash, NO_ENTRY, sizeof(chash)); |
| 2269 | |
| 2270 | BUG_ON(PERF_MAX_BRANCH_DEPTH > 255); |
| 2271 | |
| 2272 | for (i = 0; i < nr; i++) { |
| 2273 | int h = hash_64(l[i].from, CHASHBITS) % CHASHSZ; |
| 2274 | |
| 2275 | /* no collision handling for now */ |
| 2276 | if (chash[h] == NO_ENTRY) { |
| 2277 | chash[h] = i; |
| 2278 | } else if (l[chash[h]].from == l[i].from) { |
| 2279 | bool is_loop = true; |
| 2280 | /* check if it is a real loop */ |
| 2281 | off = 0; |
| 2282 | for (j = chash[h]; j < i && i + off < nr; j++, off++) |
| 2283 | if (l[j].from != l[i + off].from) { |
| 2284 | is_loop = false; |
| 2285 | break; |
| 2286 | } |
| 2287 | if (is_loop) { |
Jin Yao | c4ee062 | 2017-08-07 21:05:15 +0800 | [diff] [blame] | 2288 | j = nr - (i + off); |
| 2289 | if (j > 0) { |
| 2290 | save_iterations(iter + i + off, |
| 2291 | l + i, off); |
| 2292 | |
| 2293 | memmove(iter + i, iter + i + off, |
| 2294 | j * sizeof(*iter)); |
| 2295 | |
| 2296 | memmove(l + i, l + i + off, |
| 2297 | j * sizeof(*l)); |
| 2298 | } |
| 2299 | |
Andi Kleen | 8b7bad5 | 2014-11-12 18:05:20 -0800 | [diff] [blame] | 2300 | nr -= off; |
| 2301 | } |
| 2302 | } |
| 2303 | } |
| 2304 | return nr; |
| 2305 | } |
| 2306 | |
Kan Liang | dd3e249 | 2020-03-19 13:25:07 -0700 | [diff] [blame] | 2307 | static int lbr_callchain_add_kernel_ip(struct thread *thread, |
| 2308 | struct callchain_cursor *cursor, |
| 2309 | struct perf_sample *sample, |
| 2310 | struct symbol **parent, |
| 2311 | struct addr_location *root_al, |
| 2312 | u64 branch_from, |
| 2313 | bool callee, int end) |
| 2314 | { |
| 2315 | struct ip_callchain *chain = sample->callchain; |
| 2316 | u8 cpumode = PERF_RECORD_MISC_USER; |
| 2317 | int err, i; |
| 2318 | |
| 2319 | if (callee) { |
| 2320 | for (i = 0; i < end + 1; i++) { |
| 2321 | err = add_callchain_ip(thread, cursor, parent, |
| 2322 | root_al, &cpumode, chain->ips[i], |
| 2323 | false, NULL, NULL, branch_from); |
| 2324 | if (err) |
| 2325 | return err; |
| 2326 | } |
| 2327 | return 0; |
| 2328 | } |
| 2329 | |
| 2330 | for (i = end; i >= 0; i--) { |
| 2331 | err = add_callchain_ip(thread, cursor, parent, |
| 2332 | root_al, &cpumode, chain->ips[i], |
| 2333 | false, NULL, NULL, branch_from); |
| 2334 | if (err) |
| 2335 | return err; |
| 2336 | } |
| 2337 | |
| 2338 | return 0; |
| 2339 | } |
| 2340 | |
Kan Liang | 7f1d393 | 2020-03-19 13:25:11 -0700 | [diff] [blame] | 2341 | static void save_lbr_cursor_node(struct thread *thread, |
| 2342 | struct callchain_cursor *cursor, |
| 2343 | int idx) |
| 2344 | { |
| 2345 | struct lbr_stitch *lbr_stitch = thread->lbr_stitch; |
| 2346 | |
| 2347 | if (!lbr_stitch) |
| 2348 | return; |
| 2349 | |
| 2350 | if (cursor->pos == cursor->nr) { |
| 2351 | lbr_stitch->prev_lbr_cursor[idx].valid = false; |
| 2352 | return; |
| 2353 | } |
| 2354 | |
| 2355 | if (!cursor->curr) |
| 2356 | cursor->curr = cursor->first; |
| 2357 | else |
| 2358 | cursor->curr = cursor->curr->next; |
| 2359 | memcpy(&lbr_stitch->prev_lbr_cursor[idx], cursor->curr, |
| 2360 | sizeof(struct callchain_cursor_node)); |
| 2361 | |
| 2362 | lbr_stitch->prev_lbr_cursor[idx].valid = true; |
| 2363 | cursor->pos++; |
| 2364 | } |
| 2365 | |
Kan Liang | e2b23483 | 2020-03-19 13:25:08 -0700 | [diff] [blame] | 2366 | static int lbr_callchain_add_lbr_ip(struct thread *thread, |
| 2367 | struct callchain_cursor *cursor, |
| 2368 | struct perf_sample *sample, |
| 2369 | struct symbol **parent, |
| 2370 | struct addr_location *root_al, |
| 2371 | u64 *branch_from, |
| 2372 | bool callee) |
| 2373 | { |
| 2374 | struct branch_stack *lbr_stack = sample->branch_stack; |
| 2375 | struct branch_entry *entries = perf_sample__branch_entries(sample); |
| 2376 | u8 cpumode = PERF_RECORD_MISC_USER; |
| 2377 | int lbr_nr = lbr_stack->nr; |
| 2378 | struct branch_flags *flags; |
| 2379 | int err, i; |
| 2380 | u64 ip; |
| 2381 | |
Kan Liang | 7f1d393 | 2020-03-19 13:25:11 -0700 | [diff] [blame] | 2382 | /* |
| 2383 | * The curr and pos are not used in writing session. They are cleared |
| 2384 | * in callchain_cursor_commit() when the writing session is closed. |
| 2385 | * Using curr and pos to track the current cursor node. |
| 2386 | */ |
| 2387 | if (thread->lbr_stitch) { |
| 2388 | cursor->curr = NULL; |
| 2389 | cursor->pos = cursor->nr; |
| 2390 | if (cursor->nr) { |
| 2391 | cursor->curr = cursor->first; |
| 2392 | for (i = 0; i < (int)(cursor->nr - 1); i++) |
| 2393 | cursor->curr = cursor->curr->next; |
| 2394 | } |
| 2395 | } |
| 2396 | |
Kan Liang | e2b23483 | 2020-03-19 13:25:08 -0700 | [diff] [blame] | 2397 | if (callee) { |
| 2398 | /* Add LBR ip from first entries.to */ |
| 2399 | ip = entries[0].to; |
| 2400 | flags = &entries[0].flags; |
| 2401 | *branch_from = entries[0].from; |
| 2402 | err = add_callchain_ip(thread, cursor, parent, |
| 2403 | root_al, &cpumode, ip, |
| 2404 | true, flags, NULL, |
| 2405 | *branch_from); |
| 2406 | if (err) |
| 2407 | return err; |
| 2408 | |
Kan Liang | 7f1d393 | 2020-03-19 13:25:11 -0700 | [diff] [blame] | 2409 | /* |
| 2410 | * The number of cursor node increases. |
| 2411 | * Move the current cursor node. |
| 2412 | * But does not need to save current cursor node for entry 0. |
| 2413 | * It's impossible to stitch the whole LBRs of previous sample. |
| 2414 | */ |
| 2415 | if (thread->lbr_stitch && (cursor->pos != cursor->nr)) { |
| 2416 | if (!cursor->curr) |
| 2417 | cursor->curr = cursor->first; |
| 2418 | else |
| 2419 | cursor->curr = cursor->curr->next; |
| 2420 | cursor->pos++; |
| 2421 | } |
| 2422 | |
Kan Liang | e2b23483 | 2020-03-19 13:25:08 -0700 | [diff] [blame] | 2423 | /* Add LBR ip from entries.from one by one. */ |
| 2424 | for (i = 0; i < lbr_nr; i++) { |
| 2425 | ip = entries[i].from; |
| 2426 | flags = &entries[i].flags; |
| 2427 | err = add_callchain_ip(thread, cursor, parent, |
| 2428 | root_al, &cpumode, ip, |
| 2429 | true, flags, NULL, |
| 2430 | *branch_from); |
| 2431 | if (err) |
| 2432 | return err; |
Kan Liang | 7f1d393 | 2020-03-19 13:25:11 -0700 | [diff] [blame] | 2433 | save_lbr_cursor_node(thread, cursor, i); |
Kan Liang | e2b23483 | 2020-03-19 13:25:08 -0700 | [diff] [blame] | 2434 | } |
| 2435 | return 0; |
| 2436 | } |
| 2437 | |
| 2438 | /* Add LBR ip from entries.from one by one. */ |
| 2439 | for (i = lbr_nr - 1; i >= 0; i--) { |
| 2440 | ip = entries[i].from; |
| 2441 | flags = &entries[i].flags; |
| 2442 | err = add_callchain_ip(thread, cursor, parent, |
| 2443 | root_al, &cpumode, ip, |
| 2444 | true, flags, NULL, |
| 2445 | *branch_from); |
| 2446 | if (err) |
| 2447 | return err; |
Kan Liang | 7f1d393 | 2020-03-19 13:25:11 -0700 | [diff] [blame] | 2448 | save_lbr_cursor_node(thread, cursor, i); |
Kan Liang | e2b23483 | 2020-03-19 13:25:08 -0700 | [diff] [blame] | 2449 | } |
| 2450 | |
| 2451 | /* Add LBR ip from first entries.to */ |
| 2452 | ip = entries[0].to; |
| 2453 | flags = &entries[0].flags; |
| 2454 | *branch_from = entries[0].from; |
| 2455 | err = add_callchain_ip(thread, cursor, parent, |
| 2456 | root_al, &cpumode, ip, |
| 2457 | true, flags, NULL, |
| 2458 | *branch_from); |
| 2459 | if (err) |
| 2460 | return err; |
| 2461 | |
| 2462 | return 0; |
| 2463 | } |
| 2464 | |
Kan Liang | ff16562 | 2020-03-19 13:25:12 -0700 | [diff] [blame] | 2465 | static int lbr_callchain_add_stitched_lbr_ip(struct thread *thread, |
| 2466 | struct callchain_cursor *cursor) |
| 2467 | { |
| 2468 | struct lbr_stitch *lbr_stitch = thread->lbr_stitch; |
| 2469 | struct callchain_cursor_node *cnode; |
| 2470 | struct stitch_list *stitch_node; |
| 2471 | int err; |
| 2472 | |
| 2473 | list_for_each_entry(stitch_node, &lbr_stitch->lists, node) { |
| 2474 | cnode = &stitch_node->cursor; |
| 2475 | |
| 2476 | err = callchain_cursor_append(cursor, cnode->ip, |
| 2477 | &cnode->ms, |
| 2478 | cnode->branch, |
| 2479 | &cnode->branch_flags, |
| 2480 | cnode->nr_loop_iter, |
| 2481 | cnode->iter_cycles, |
| 2482 | cnode->branch_from, |
| 2483 | cnode->srcline); |
| 2484 | if (err) |
| 2485 | return err; |
| 2486 | } |
| 2487 | return 0; |
| 2488 | } |
| 2489 | |
| 2490 | static struct stitch_list *get_stitch_node(struct thread *thread) |
| 2491 | { |
| 2492 | struct lbr_stitch *lbr_stitch = thread->lbr_stitch; |
| 2493 | struct stitch_list *stitch_node; |
| 2494 | |
| 2495 | if (!list_empty(&lbr_stitch->free_lists)) { |
| 2496 | stitch_node = list_first_entry(&lbr_stitch->free_lists, |
| 2497 | struct stitch_list, node); |
| 2498 | list_del(&stitch_node->node); |
| 2499 | |
| 2500 | return stitch_node; |
| 2501 | } |
| 2502 | |
| 2503 | return malloc(sizeof(struct stitch_list)); |
| 2504 | } |
| 2505 | |
| 2506 | static bool has_stitched_lbr(struct thread *thread, |
| 2507 | struct perf_sample *cur, |
| 2508 | struct perf_sample *prev, |
| 2509 | unsigned int max_lbr, |
| 2510 | bool callee) |
| 2511 | { |
| 2512 | struct branch_stack *cur_stack = cur->branch_stack; |
| 2513 | struct branch_entry *cur_entries = perf_sample__branch_entries(cur); |
| 2514 | struct branch_stack *prev_stack = prev->branch_stack; |
| 2515 | struct branch_entry *prev_entries = perf_sample__branch_entries(prev); |
| 2516 | struct lbr_stitch *lbr_stitch = thread->lbr_stitch; |
| 2517 | int i, j, nr_identical_branches = 0; |
| 2518 | struct stitch_list *stitch_node; |
| 2519 | u64 cur_base, distance; |
| 2520 | |
| 2521 | if (!cur_stack || !prev_stack) |
| 2522 | return false; |
| 2523 | |
| 2524 | /* Find the physical index of the base-of-stack for current sample. */ |
| 2525 | cur_base = max_lbr - cur_stack->nr + cur_stack->hw_idx + 1; |
| 2526 | |
| 2527 | distance = (prev_stack->hw_idx > cur_base) ? (prev_stack->hw_idx - cur_base) : |
| 2528 | (max_lbr + prev_stack->hw_idx - cur_base); |
| 2529 | /* Previous sample has shorter stack. Nothing can be stitched. */ |
| 2530 | if (distance + 1 > prev_stack->nr) |
| 2531 | return false; |
| 2532 | |
| 2533 | /* |
| 2534 | * Check if there are identical LBRs between two samples. |
Ingo Molnar | 4d39c89 | 2021-03-23 17:09:15 +0100 | [diff] [blame] | 2535 | * Identical LBRs must have same from, to and flags values. Also, |
Kan Liang | ff16562 | 2020-03-19 13:25:12 -0700 | [diff] [blame] | 2536 | * they have to be saved in the same LBR registers (same physical |
| 2537 | * index). |
| 2538 | * |
| 2539 | * Starts from the base-of-stack of current sample. |
| 2540 | */ |
| 2541 | for (i = distance, j = cur_stack->nr - 1; (i >= 0) && (j >= 0); i--, j--) { |
| 2542 | if ((prev_entries[i].from != cur_entries[j].from) || |
| 2543 | (prev_entries[i].to != cur_entries[j].to) || |
| 2544 | (prev_entries[i].flags.value != cur_entries[j].flags.value)) |
| 2545 | break; |
| 2546 | nr_identical_branches++; |
| 2547 | } |
| 2548 | |
| 2549 | if (!nr_identical_branches) |
| 2550 | return false; |
| 2551 | |
| 2552 | /* |
| 2553 | * Save the LBRs between the base-of-stack of previous sample |
| 2554 | * and the base-of-stack of current sample into lbr_stitch->lists. |
| 2555 | * These LBRs will be stitched later. |
| 2556 | */ |
| 2557 | for (i = prev_stack->nr - 1; i > (int)distance; i--) { |
| 2558 | |
| 2559 | if (!lbr_stitch->prev_lbr_cursor[i].valid) |
| 2560 | continue; |
| 2561 | |
| 2562 | stitch_node = get_stitch_node(thread); |
| 2563 | if (!stitch_node) |
| 2564 | return false; |
| 2565 | |
| 2566 | memcpy(&stitch_node->cursor, &lbr_stitch->prev_lbr_cursor[i], |
| 2567 | sizeof(struct callchain_cursor_node)); |
| 2568 | |
| 2569 | if (callee) |
| 2570 | list_add(&stitch_node->node, &lbr_stitch->lists); |
| 2571 | else |
| 2572 | list_add_tail(&stitch_node->node, &lbr_stitch->lists); |
| 2573 | } |
| 2574 | |
| 2575 | return true; |
| 2576 | } |
| 2577 | |
Kan Liang | 7f1d393 | 2020-03-19 13:25:11 -0700 | [diff] [blame] | 2578 | static bool alloc_lbr_stitch(struct thread *thread, unsigned int max_lbr) |
Kan Liang | 9c6c3f4 | 2020-03-19 13:25:10 -0700 | [diff] [blame] | 2579 | { |
| 2580 | if (thread->lbr_stitch) |
| 2581 | return true; |
| 2582 | |
| 2583 | thread->lbr_stitch = zalloc(sizeof(*thread->lbr_stitch)); |
| 2584 | if (!thread->lbr_stitch) |
| 2585 | goto err; |
| 2586 | |
Kan Liang | 7f1d393 | 2020-03-19 13:25:11 -0700 | [diff] [blame] | 2587 | thread->lbr_stitch->prev_lbr_cursor = calloc(max_lbr + 1, sizeof(struct callchain_cursor_node)); |
| 2588 | if (!thread->lbr_stitch->prev_lbr_cursor) |
| 2589 | goto free_lbr_stitch; |
| 2590 | |
Kan Liang | ff16562 | 2020-03-19 13:25:12 -0700 | [diff] [blame] | 2591 | INIT_LIST_HEAD(&thread->lbr_stitch->lists); |
| 2592 | INIT_LIST_HEAD(&thread->lbr_stitch->free_lists); |
| 2593 | |
Kan Liang | 7f1d393 | 2020-03-19 13:25:11 -0700 | [diff] [blame] | 2594 | return true; |
| 2595 | |
| 2596 | free_lbr_stitch: |
| 2597 | zfree(&thread->lbr_stitch); |
Kan Liang | 9c6c3f4 | 2020-03-19 13:25:10 -0700 | [diff] [blame] | 2598 | err: |
| 2599 | pr_warning("Failed to allocate space for stitched LBRs. Disable LBR stitch\n"); |
| 2600 | thread->lbr_stitch_enable = false; |
| 2601 | return false; |
| 2602 | } |
| 2603 | |
Kan Liang | 384b605 | 2015-01-05 13:23:05 -0500 | [diff] [blame] | 2604 | /* |
Ingo Molnar | 4d39c89 | 2021-03-23 17:09:15 +0100 | [diff] [blame] | 2605 | * Resolve LBR callstack chain sample |
Kan Liang | 384b605 | 2015-01-05 13:23:05 -0500 | [diff] [blame] | 2606 | * Return: |
| 2607 | * 1 on success get LBR callchain information |
| 2608 | * 0 no available LBR callchain information, should try fp |
| 2609 | * negative error code on other errors. |
| 2610 | */ |
| 2611 | static int resolve_lbr_callchain_sample(struct thread *thread, |
Arnaldo Carvalho de Melo | 91d7b2d | 2016-04-14 14:48:07 -0300 | [diff] [blame] | 2612 | struct callchain_cursor *cursor, |
Kan Liang | 384b605 | 2015-01-05 13:23:05 -0500 | [diff] [blame] | 2613 | struct perf_sample *sample, |
| 2614 | struct symbol **parent, |
| 2615 | struct addr_location *root_al, |
Kan Liang | 7f1d393 | 2020-03-19 13:25:11 -0700 | [diff] [blame] | 2616 | int max_stack, |
| 2617 | unsigned int max_lbr) |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 2618 | { |
Kan Liang | ff16562 | 2020-03-19 13:25:12 -0700 | [diff] [blame] | 2619 | bool callee = (callchain_param.order == ORDER_CALLEE); |
Kan Liang | 384b605 | 2015-01-05 13:23:05 -0500 | [diff] [blame] | 2620 | struct ip_callchain *chain = sample->callchain; |
Arnaldo Carvalho de Melo | 18ef15c | 2016-10-03 11:07:24 -0300 | [diff] [blame] | 2621 | int chain_nr = min(max_stack, (int)chain->nr), i; |
Kan Liang | 9c6c3f4 | 2020-03-19 13:25:10 -0700 | [diff] [blame] | 2622 | struct lbr_stitch *lbr_stitch; |
Kan Liang | ff16562 | 2020-03-19 13:25:12 -0700 | [diff] [blame] | 2623 | bool stitched_lbr = false; |
Kan Liang | e2b23483 | 2020-03-19 13:25:08 -0700 | [diff] [blame] | 2624 | u64 branch_from = 0; |
Kan Liang | e48b831 | 2020-03-19 13:25:06 -0700 | [diff] [blame] | 2625 | int err; |
Kan Liang | 384b605 | 2015-01-05 13:23:05 -0500 | [diff] [blame] | 2626 | |
| 2627 | for (i = 0; i < chain_nr; i++) { |
| 2628 | if (chain->ips[i] == PERF_CONTEXT_USER) |
| 2629 | break; |
| 2630 | } |
| 2631 | |
| 2632 | /* LBR only affects the user callchain */ |
Kan Liang | f860326 | 2020-03-19 13:25:05 -0700 | [diff] [blame] | 2633 | if (i == chain_nr) |
| 2634 | return 0; |
Kan Liang | 384b605 | 2015-01-05 13:23:05 -0500 | [diff] [blame] | 2635 | |
Kan Liang | 9c6c3f4 | 2020-03-19 13:25:10 -0700 | [diff] [blame] | 2636 | if (thread->lbr_stitch_enable && !sample->no_hw_idx && |
Kan Liang | 7f1d393 | 2020-03-19 13:25:11 -0700 | [diff] [blame] | 2637 | (max_lbr > 0) && alloc_lbr_stitch(thread, max_lbr)) { |
Kan Liang | 9c6c3f4 | 2020-03-19 13:25:10 -0700 | [diff] [blame] | 2638 | lbr_stitch = thread->lbr_stitch; |
| 2639 | |
Kan Liang | ff16562 | 2020-03-19 13:25:12 -0700 | [diff] [blame] | 2640 | stitched_lbr = has_stitched_lbr(thread, sample, |
| 2641 | &lbr_stitch->prev_sample, |
| 2642 | max_lbr, callee); |
| 2643 | |
| 2644 | if (!stitched_lbr && !list_empty(&lbr_stitch->lists)) { |
| 2645 | list_replace_init(&lbr_stitch->lists, |
| 2646 | &lbr_stitch->free_lists); |
| 2647 | } |
Kan Liang | 9c6c3f4 | 2020-03-19 13:25:10 -0700 | [diff] [blame] | 2648 | memcpy(&lbr_stitch->prev_sample, sample, sizeof(*sample)); |
| 2649 | } |
| 2650 | |
Kan Liang | ff16562 | 2020-03-19 13:25:12 -0700 | [diff] [blame] | 2651 | if (callee) { |
Kan Liang | e48b831 | 2020-03-19 13:25:06 -0700 | [diff] [blame] | 2652 | /* Add kernel ip */ |
Kan Liang | dd3e249 | 2020-03-19 13:25:07 -0700 | [diff] [blame] | 2653 | err = lbr_callchain_add_kernel_ip(thread, cursor, sample, |
| 2654 | parent, root_al, branch_from, |
| 2655 | true, i); |
| 2656 | if (err) |
| 2657 | goto error; |
| 2658 | |
Kan Liang | e2b23483 | 2020-03-19 13:25:08 -0700 | [diff] [blame] | 2659 | err = lbr_callchain_add_lbr_ip(thread, cursor, sample, parent, |
| 2660 | root_al, &branch_from, true); |
Kan Liang | f860326 | 2020-03-19 13:25:05 -0700 | [diff] [blame] | 2661 | if (err) |
Kan Liang | e48b831 | 2020-03-19 13:25:06 -0700 | [diff] [blame] | 2662 | goto error; |
| 2663 | |
Kan Liang | ff16562 | 2020-03-19 13:25:12 -0700 | [diff] [blame] | 2664 | if (stitched_lbr) { |
| 2665 | err = lbr_callchain_add_stitched_lbr_ip(thread, cursor); |
| 2666 | if (err) |
| 2667 | goto error; |
| 2668 | } |
| 2669 | |
Kan Liang | e48b831 | 2020-03-19 13:25:06 -0700 | [diff] [blame] | 2670 | } else { |
Kan Liang | ff16562 | 2020-03-19 13:25:12 -0700 | [diff] [blame] | 2671 | if (stitched_lbr) { |
| 2672 | err = lbr_callchain_add_stitched_lbr_ip(thread, cursor); |
| 2673 | if (err) |
| 2674 | goto error; |
| 2675 | } |
Kan Liang | e2b23483 | 2020-03-19 13:25:08 -0700 | [diff] [blame] | 2676 | err = lbr_callchain_add_lbr_ip(thread, cursor, sample, parent, |
| 2677 | root_al, &branch_from, false); |
Kan Liang | e48b831 | 2020-03-19 13:25:06 -0700 | [diff] [blame] | 2678 | if (err) |
| 2679 | goto error; |
| 2680 | |
| 2681 | /* Add kernel ip */ |
Kan Liang | dd3e249 | 2020-03-19 13:25:07 -0700 | [diff] [blame] | 2682 | err = lbr_callchain_add_kernel_ip(thread, cursor, sample, |
| 2683 | parent, root_al, branch_from, |
| 2684 | false, i); |
| 2685 | if (err) |
| 2686 | goto error; |
Kan Liang | f860326 | 2020-03-19 13:25:05 -0700 | [diff] [blame] | 2687 | } |
| 2688 | return 1; |
Kan Liang | e48b831 | 2020-03-19 13:25:06 -0700 | [diff] [blame] | 2689 | |
| 2690 | error: |
| 2691 | return (err < 0) ? err : 0; |
Kan Liang | 384b605 | 2015-01-05 13:23:05 -0500 | [diff] [blame] | 2692 | } |
| 2693 | |
David S. Miller | e9024d5 | 2018-10-30 12:12:26 -0300 | [diff] [blame] | 2694 | static int find_prev_cpumode(struct ip_callchain *chain, struct thread *thread, |
| 2695 | struct callchain_cursor *cursor, |
| 2696 | struct symbol **parent, |
| 2697 | struct addr_location *root_al, |
| 2698 | u8 *cpumode, int ent) |
| 2699 | { |
| 2700 | int err = 0; |
| 2701 | |
| 2702 | while (--ent >= 0) { |
| 2703 | u64 ip = chain->ips[ent]; |
| 2704 | |
| 2705 | if (ip >= PERF_CONTEXT_MAX) { |
| 2706 | err = add_callchain_ip(thread, cursor, parent, |
| 2707 | root_al, cpumode, ip, |
| 2708 | false, NULL, NULL, 0); |
| 2709 | break; |
| 2710 | } |
| 2711 | } |
| 2712 | return err; |
| 2713 | } |
| 2714 | |
Alexandre Truong | b9f6fbb | 2021-12-17 15:45:20 +0000 | [diff] [blame] | 2715 | static u64 get_leaf_frame_caller(struct perf_sample *sample, |
| 2716 | struct thread *thread, int usr_idx) |
Alexandre Truong | 32bfa5b | 2021-12-17 15:45:16 +0000 | [diff] [blame] | 2717 | { |
Alexandre Truong | b9f6fbb | 2021-12-17 15:45:20 +0000 | [diff] [blame] | 2718 | if (machine__normalized_is(thread->maps->machine, "arm64")) |
| 2719 | return get_leaf_frame_caller_aarch64(sample, thread, usr_idx); |
| 2720 | else |
| 2721 | return 0; |
Alexandre Truong | 32bfa5b | 2021-12-17 15:45:16 +0000 | [diff] [blame] | 2722 | } |
| 2723 | |
Kan Liang | 384b605 | 2015-01-05 13:23:05 -0500 | [diff] [blame] | 2724 | static int thread__resolve_callchain_sample(struct thread *thread, |
Arnaldo Carvalho de Melo | 91d7b2d | 2016-04-14 14:48:07 -0300 | [diff] [blame] | 2725 | struct callchain_cursor *cursor, |
Jiri Olsa | 32dcd02 | 2019-07-21 13:23:51 +0200 | [diff] [blame] | 2726 | struct evsel *evsel, |
Kan Liang | 384b605 | 2015-01-05 13:23:05 -0500 | [diff] [blame] | 2727 | struct perf_sample *sample, |
| 2728 | struct symbol **parent, |
| 2729 | struct addr_location *root_al, |
| 2730 | int max_stack) |
| 2731 | { |
| 2732 | struct branch_stack *branch = sample->branch_stack; |
Kan Liang | 42bbabe | 2020-02-28 08:30:00 -0800 | [diff] [blame] | 2733 | struct branch_entry *entries = perf_sample__branch_entries(sample); |
Kan Liang | 384b605 | 2015-01-05 13:23:05 -0500 | [diff] [blame] | 2734 | struct ip_callchain *chain = sample->callchain; |
Jin Yao | b49a821 | 2017-05-08 18:43:02 +0800 | [diff] [blame] | 2735 | int chain_nr = 0; |
David Hildenbrand | 73dbcd6 | 2015-03-30 10:11:00 +0200 | [diff] [blame] | 2736 | u8 cpumode = PERF_RECORD_MISC_USER; |
Alexandre Truong | 32bfa5b | 2021-12-17 15:45:16 +0000 | [diff] [blame] | 2737 | int i, j, err, nr_entries, usr_idx; |
Andi Kleen | 8b7bad5 | 2014-11-12 18:05:20 -0800 | [diff] [blame] | 2738 | int skip_idx = -1; |
| 2739 | int first_call = 0; |
Alexandre Truong | 32bfa5b | 2021-12-17 15:45:16 +0000 | [diff] [blame] | 2740 | u64 leaf_frame_caller; |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 2741 | |
Jin Yao | b49a821 | 2017-05-08 18:43:02 +0800 | [diff] [blame] | 2742 | if (chain) |
| 2743 | chain_nr = chain->nr; |
| 2744 | |
Arnaldo Carvalho de Melo | 4f138a9 | 2020-04-30 11:19:45 -0300 | [diff] [blame] | 2745 | if (evsel__has_branch_callstack(evsel)) { |
Arnaldo Carvalho de Melo | 6e6d1d6 | 2020-05-04 13:44:03 -0300 | [diff] [blame] | 2746 | struct perf_env *env = evsel__env(evsel); |
Kan Liang | 7f1d393 | 2020-03-19 13:25:11 -0700 | [diff] [blame] | 2747 | |
Arnaldo Carvalho de Melo | 91d7b2d | 2016-04-14 14:48:07 -0300 | [diff] [blame] | 2748 | err = resolve_lbr_callchain_sample(thread, cursor, sample, parent, |
Kan Liang | 7f1d393 | 2020-03-19 13:25:11 -0700 | [diff] [blame] | 2749 | root_al, max_stack, |
| 2750 | !env ? 0 : env->max_branches); |
Kan Liang | 384b605 | 2015-01-05 13:23:05 -0500 | [diff] [blame] | 2751 | if (err) |
| 2752 | return (err < 0) ? err : 0; |
| 2753 | } |
| 2754 | |
Sukadev Bhattiprolu | a60335b | 2014-06-25 08:49:03 -0700 | [diff] [blame] | 2755 | /* |
| 2756 | * Based on DWARF debug information, some architectures skip |
| 2757 | * a callchain entry saved by the kernel. |
| 2758 | */ |
Arnaldo Carvalho de Melo | bf8bddb | 2016-05-19 11:14:15 -0300 | [diff] [blame] | 2759 | skip_idx = arch_skip_callchain_idx(thread, chain); |
Sukadev Bhattiprolu | a60335b | 2014-06-25 08:49:03 -0700 | [diff] [blame] | 2760 | |
Andi Kleen | 8b7bad5 | 2014-11-12 18:05:20 -0800 | [diff] [blame] | 2761 | /* |
| 2762 | * Add branches to call stack for easier browsing. This gives |
| 2763 | * more context for a sample than just the callers. |
| 2764 | * |
| 2765 | * This uses individual histograms of paths compared to the |
| 2766 | * aggregated histograms the normal LBR mode uses. |
| 2767 | * |
| 2768 | * Limitations for now: |
| 2769 | * - No extra filters |
| 2770 | * - No annotations (should annotate somehow) |
| 2771 | */ |
| 2772 | |
| 2773 | if (branch && callchain_param.branch_callstack) { |
| 2774 | int nr = min(max_stack, (int)branch->nr); |
| 2775 | struct branch_entry be[nr]; |
Jin Yao | c4ee062 | 2017-08-07 21:05:15 +0800 | [diff] [blame] | 2776 | struct iterations iter[nr]; |
Andi Kleen | 8b7bad5 | 2014-11-12 18:05:20 -0800 | [diff] [blame] | 2777 | |
| 2778 | if (branch->nr > PERF_MAX_BRANCH_DEPTH) { |
| 2779 | pr_warning("corrupted branch chain. skipping...\n"); |
| 2780 | goto check_calls; |
| 2781 | } |
| 2782 | |
| 2783 | for (i = 0; i < nr; i++) { |
| 2784 | if (callchain_param.order == ORDER_CALLEE) { |
Kan Liang | 42bbabe | 2020-02-28 08:30:00 -0800 | [diff] [blame] | 2785 | be[i] = entries[i]; |
Jin Yao | b49a821 | 2017-05-08 18:43:02 +0800 | [diff] [blame] | 2786 | |
| 2787 | if (chain == NULL) |
| 2788 | continue; |
| 2789 | |
Andi Kleen | 8b7bad5 | 2014-11-12 18:05:20 -0800 | [diff] [blame] | 2790 | /* |
| 2791 | * Check for overlap into the callchain. |
| 2792 | * The return address is one off compared to |
| 2793 | * the branch entry. To adjust for this |
| 2794 | * assume the calling instruction is not longer |
| 2795 | * than 8 bytes. |
| 2796 | */ |
| 2797 | if (i == skip_idx || |
| 2798 | chain->ips[first_call] >= PERF_CONTEXT_MAX) |
| 2799 | first_call++; |
| 2800 | else if (be[i].from < chain->ips[first_call] && |
| 2801 | be[i].from >= chain->ips[first_call] - 8) |
| 2802 | first_call++; |
| 2803 | } else |
Kan Liang | 42bbabe | 2020-02-28 08:30:00 -0800 | [diff] [blame] | 2804 | be[i] = entries[branch->nr - i - 1]; |
Andi Kleen | 8b7bad5 | 2014-11-12 18:05:20 -0800 | [diff] [blame] | 2805 | } |
| 2806 | |
Jin Yao | c4ee062 | 2017-08-07 21:05:15 +0800 | [diff] [blame] | 2807 | memset(iter, 0, sizeof(struct iterations) * nr); |
| 2808 | nr = remove_loops(be, nr, iter); |
Jin Yao | 410024d | 2016-10-31 09:19:49 +0800 | [diff] [blame] | 2809 | |
Andi Kleen | 8b7bad5 | 2014-11-12 18:05:20 -0800 | [diff] [blame] | 2810 | for (i = 0; i < nr; i++) { |
Jin Yao | c4ee062 | 2017-08-07 21:05:15 +0800 | [diff] [blame] | 2811 | err = add_callchain_ip(thread, cursor, parent, |
| 2812 | root_al, |
| 2813 | NULL, be[i].to, |
| 2814 | true, &be[i].flags, |
| 2815 | NULL, be[i].from); |
Jin Yao | 410024d | 2016-10-31 09:19:49 +0800 | [diff] [blame] | 2816 | |
Andi Kleen | 8b7bad5 | 2014-11-12 18:05:20 -0800 | [diff] [blame] | 2817 | if (!err) |
Arnaldo Carvalho de Melo | 91d7b2d | 2016-04-14 14:48:07 -0300 | [diff] [blame] | 2818 | err = add_callchain_ip(thread, cursor, parent, root_al, |
Jin Yao | 410024d | 2016-10-31 09:19:49 +0800 | [diff] [blame] | 2819 | NULL, be[i].from, |
| 2820 | true, &be[i].flags, |
Jin Yao | c4ee062 | 2017-08-07 21:05:15 +0800 | [diff] [blame] | 2821 | &iter[i], 0); |
Andi Kleen | 8b7bad5 | 2014-11-12 18:05:20 -0800 | [diff] [blame] | 2822 | if (err == -EINVAL) |
| 2823 | break; |
| 2824 | if (err) |
| 2825 | return err; |
| 2826 | } |
Jin Yao | b49a821 | 2017-05-08 18:43:02 +0800 | [diff] [blame] | 2827 | |
| 2828 | if (chain_nr == 0) |
| 2829 | return 0; |
| 2830 | |
Andi Kleen | 8b7bad5 | 2014-11-12 18:05:20 -0800 | [diff] [blame] | 2831 | chain_nr -= nr; |
| 2832 | } |
| 2833 | |
| 2834 | check_calls: |
Adrian Hunter | aceb982 | 2019-11-14 16:25:38 +0200 | [diff] [blame] | 2835 | if (chain && callchain_param.order != ORDER_CALLEE) { |
David S. Miller | e9024d5 | 2018-10-30 12:12:26 -0300 | [diff] [blame] | 2836 | err = find_prev_cpumode(chain, thread, cursor, parent, root_al, |
| 2837 | &cpumode, chain->nr - first_call); |
| 2838 | if (err) |
| 2839 | return (err < 0) ? err : 0; |
| 2840 | } |
Arnaldo Carvalho de Melo | bf8bddb | 2016-05-19 11:14:15 -0300 | [diff] [blame] | 2841 | for (i = first_call, nr_entries = 0; |
Arnaldo Carvalho de Melo | a29d5c9 | 2016-05-16 21:16:54 -0300 | [diff] [blame] | 2842 | i < chain_nr && nr_entries < max_stack; i++) { |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 2843 | u64 ip; |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 2844 | |
| 2845 | if (callchain_param.order == ORDER_CALLEE) |
Sukadev Bhattiprolu | a60335b | 2014-06-25 08:49:03 -0700 | [diff] [blame] | 2846 | j = i; |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 2847 | else |
Sukadev Bhattiprolu | a60335b | 2014-06-25 08:49:03 -0700 | [diff] [blame] | 2848 | j = chain->nr - i - 1; |
| 2849 | |
| 2850 | #ifdef HAVE_SKIP_CALLCHAIN_IDX |
| 2851 | if (j == skip_idx) |
| 2852 | continue; |
| 2853 | #endif |
| 2854 | ip = chain->ips[j]; |
Arnaldo Carvalho de Melo | bf8bddb | 2016-05-19 11:14:15 -0300 | [diff] [blame] | 2855 | if (ip < PERF_CONTEXT_MAX) |
| 2856 | ++nr_entries; |
David S. Miller | e9024d5 | 2018-10-30 12:12:26 -0300 | [diff] [blame] | 2857 | else if (callchain_param.order != ORDER_CALLEE) { |
| 2858 | err = find_prev_cpumode(chain, thread, cursor, parent, |
| 2859 | root_al, &cpumode, j); |
| 2860 | if (err) |
| 2861 | return (err < 0) ? err : 0; |
| 2862 | continue; |
| 2863 | } |
Arnaldo Carvalho de Melo | a29d5c9 | 2016-05-16 21:16:54 -0300 | [diff] [blame] | 2864 | |
Alexandre Truong | 32bfa5b | 2021-12-17 15:45:16 +0000 | [diff] [blame] | 2865 | /* |
| 2866 | * PERF_CONTEXT_USER allows us to locate where the user stack ends. |
| 2867 | * Depending on callchain_param.order and the position of PERF_CONTEXT_USER, |
| 2868 | * the index will be different in order to add the missing frame |
| 2869 | * at the right place. |
| 2870 | */ |
| 2871 | |
| 2872 | usr_idx = callchain_param.order == ORDER_CALLEE ? j-2 : j-1; |
| 2873 | |
| 2874 | if (usr_idx >= 0 && chain->ips[usr_idx] == PERF_CONTEXT_USER) { |
| 2875 | |
| 2876 | leaf_frame_caller = get_leaf_frame_caller(sample, thread, usr_idx); |
| 2877 | |
| 2878 | /* |
| 2879 | * check if leaf_frame_Caller != ip to not add the same |
| 2880 | * value twice. |
| 2881 | */ |
| 2882 | |
| 2883 | if (leaf_frame_caller && leaf_frame_caller != ip) { |
| 2884 | |
| 2885 | err = add_callchain_ip(thread, cursor, parent, |
| 2886 | root_al, &cpumode, leaf_frame_caller, |
| 2887 | false, NULL, NULL, 0); |
| 2888 | if (err) |
| 2889 | return (err < 0) ? err : 0; |
| 2890 | } |
| 2891 | } |
| 2892 | |
Jin Yao | 410024d | 2016-10-31 09:19:49 +0800 | [diff] [blame] | 2893 | err = add_callchain_ip(thread, cursor, parent, |
| 2894 | root_al, &cpumode, ip, |
Jin Yao | c4ee062 | 2017-08-07 21:05:15 +0800 | [diff] [blame] | 2895 | false, NULL, NULL, 0); |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 2896 | |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 2897 | if (err) |
Kan Liang | 2e77784 | 2014-12-02 10:06:53 -0500 | [diff] [blame] | 2898 | return (err < 0) ? err : 0; |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 2899 | } |
| 2900 | |
| 2901 | return 0; |
| 2902 | } |
| 2903 | |
Arnaldo Carvalho de Melo | c152973 | 2019-11-04 11:58:21 -0300 | [diff] [blame] | 2904 | static int append_inlines(struct callchain_cursor *cursor, struct map_symbol *ms, u64 ip) |
Milian Wolff | 11ea251 | 2017-10-09 22:32:59 +0200 | [diff] [blame] | 2905 | { |
Arnaldo Carvalho de Melo | c152973 | 2019-11-04 11:58:21 -0300 | [diff] [blame] | 2906 | struct symbol *sym = ms->sym; |
| 2907 | struct map *map = ms->map; |
Milian Wolff | 11ea251 | 2017-10-09 22:32:59 +0200 | [diff] [blame] | 2908 | struct inline_node *inline_node; |
| 2909 | struct inline_list *ilist; |
| 2910 | u64 addr; |
Milian Wolff | b38775c | 2017-10-19 13:38:33 +0200 | [diff] [blame] | 2911 | int ret = 1; |
Milian Wolff | 11ea251 | 2017-10-09 22:32:59 +0200 | [diff] [blame] | 2912 | |
| 2913 | if (!symbol_conf.inline_name || !map || !sym) |
Milian Wolff | b38775c | 2017-10-19 13:38:33 +0200 | [diff] [blame] | 2914 | return ret; |
Milian Wolff | 11ea251 | 2017-10-09 22:32:59 +0200 | [diff] [blame] | 2915 | |
Milian Wolff | 7a8a8fc | 2018-09-26 15:52:06 +0200 | [diff] [blame] | 2916 | addr = map__map_ip(map, ip); |
| 2917 | addr = map__rip_2objdump(map, addr); |
Milian Wolff | 11ea251 | 2017-10-09 22:32:59 +0200 | [diff] [blame] | 2918 | |
| 2919 | inline_node = inlines__tree_find(&map->dso->inlined_nodes, addr); |
| 2920 | if (!inline_node) { |
| 2921 | inline_node = dso__parse_addr_inlines(map->dso, addr, sym); |
| 2922 | if (!inline_node) |
Milian Wolff | b38775c | 2017-10-19 13:38:33 +0200 | [diff] [blame] | 2923 | return ret; |
Milian Wolff | 11ea251 | 2017-10-09 22:32:59 +0200 | [diff] [blame] | 2924 | inlines__tree_insert(&map->dso->inlined_nodes, inline_node); |
| 2925 | } |
| 2926 | |
| 2927 | list_for_each_entry(ilist, &inline_node->val, list) { |
Arnaldo Carvalho de Melo | 5f0fef8 | 2019-11-04 12:14:32 -0300 | [diff] [blame] | 2928 | struct map_symbol ilist_ms = { |
Arnaldo Carvalho de Melo | 77b91c1 | 2019-11-29 15:47:51 -0300 | [diff] [blame] | 2929 | .maps = ms->maps, |
Arnaldo Carvalho de Melo | 5f0fef8 | 2019-11-04 12:14:32 -0300 | [diff] [blame] | 2930 | .map = map, |
| 2931 | .sym = ilist->symbol, |
| 2932 | }; |
| 2933 | ret = callchain_cursor_append(cursor, ip, &ilist_ms, false, |
Milian Wolff | b38775c | 2017-10-19 13:38:33 +0200 | [diff] [blame] | 2934 | NULL, 0, 0, 0, ilist->srcline); |
Milian Wolff | 11ea251 | 2017-10-09 22:32:59 +0200 | [diff] [blame] | 2935 | |
| 2936 | if (ret != 0) |
| 2937 | return ret; |
| 2938 | } |
| 2939 | |
Milian Wolff | b38775c | 2017-10-19 13:38:33 +0200 | [diff] [blame] | 2940 | return ret; |
Milian Wolff | 11ea251 | 2017-10-09 22:32:59 +0200 | [diff] [blame] | 2941 | } |
| 2942 | |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 2943 | static int unwind_entry(struct unwind_entry *entry, void *arg) |
| 2944 | { |
| 2945 | struct callchain_cursor *cursor = arg; |
Milian Wolff | 40a342c | 2017-10-09 22:32:56 +0200 | [diff] [blame] | 2946 | const char *srcline = NULL; |
Milian Wolff | ff4ce28 | 2018-09-26 15:52:05 +0200 | [diff] [blame] | 2947 | u64 addr = entry->ip; |
Namhyung Kim | b49a8fe | 2015-11-26 16:08:20 +0900 | [diff] [blame] | 2948 | |
Arnaldo Carvalho de Melo | c152973 | 2019-11-04 11:58:21 -0300 | [diff] [blame] | 2949 | if (symbol_conf.hide_unresolved && entry->ms.sym == NULL) |
Namhyung Kim | b49a8fe | 2015-11-26 16:08:20 +0900 | [diff] [blame] | 2950 | return 0; |
Milian Wolff | 40a342c | 2017-10-09 22:32:56 +0200 | [diff] [blame] | 2951 | |
Arnaldo Carvalho de Melo | c152973 | 2019-11-04 11:58:21 -0300 | [diff] [blame] | 2952 | if (append_inlines(cursor, &entry->ms, entry->ip) == 0) |
Milian Wolff | 11ea251 | 2017-10-09 22:32:59 +0200 | [diff] [blame] | 2953 | return 0; |
| 2954 | |
Sandipan Das | 2a9d505 | 2018-07-03 17:35:55 +0530 | [diff] [blame] | 2955 | /* |
| 2956 | * Convert entry->ip from a virtual address to an offset in |
| 2957 | * its corresponding binary. |
| 2958 | */ |
Arnaldo Carvalho de Melo | c152973 | 2019-11-04 11:58:21 -0300 | [diff] [blame] | 2959 | if (entry->ms.map) |
| 2960 | addr = map__map_ip(entry->ms.map, entry->ip); |
Sandipan Das | 2a9d505 | 2018-07-03 17:35:55 +0530 | [diff] [blame] | 2961 | |
Arnaldo Carvalho de Melo | 5f0fef8 | 2019-11-04 12:14:32 -0300 | [diff] [blame] | 2962 | srcline = callchain_srcline(&entry->ms, addr); |
| 2963 | return callchain_cursor_append(cursor, entry->ip, &entry->ms, |
Milian Wolff | 40a342c | 2017-10-09 22:32:56 +0200 | [diff] [blame] | 2964 | false, NULL, 0, 0, 0, srcline); |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 2965 | } |
| 2966 | |
Chris Phlipot | 9919a65 | 2016-04-28 01:19:06 -0700 | [diff] [blame] | 2967 | static int thread__resolve_callchain_unwind(struct thread *thread, |
| 2968 | struct callchain_cursor *cursor, |
Jiri Olsa | 32dcd02 | 2019-07-21 13:23:51 +0200 | [diff] [blame] | 2969 | struct evsel *evsel, |
Chris Phlipot | 9919a65 | 2016-04-28 01:19:06 -0700 | [diff] [blame] | 2970 | struct perf_sample *sample, |
| 2971 | int max_stack) |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 2972 | { |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 2973 | /* Can we do dwarf post unwind? */ |
Jiri Olsa | 1fc632c | 2019-07-21 13:24:29 +0200 | [diff] [blame] | 2974 | if (!((evsel->core.attr.sample_type & PERF_SAMPLE_REGS_USER) && |
| 2975 | (evsel->core.attr.sample_type & PERF_SAMPLE_STACK_USER))) |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 2976 | return 0; |
| 2977 | |
| 2978 | /* Bail out if nothing was captured. */ |
| 2979 | if ((!sample->user_regs.regs) || |
| 2980 | (!sample->user_stack.size)) |
| 2981 | return 0; |
| 2982 | |
Arnaldo Carvalho de Melo | 91d7b2d | 2016-04-14 14:48:07 -0300 | [diff] [blame] | 2983 | return unwind__get_entries(unwind_entry, cursor, |
Jiri Olsa | 352ea45 | 2014-01-07 13:47:25 +0100 | [diff] [blame] | 2984 | thread, sample, max_stack); |
Chris Phlipot | 9919a65 | 2016-04-28 01:19:06 -0700 | [diff] [blame] | 2985 | } |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 2986 | |
Chris Phlipot | 9919a65 | 2016-04-28 01:19:06 -0700 | [diff] [blame] | 2987 | int thread__resolve_callchain(struct thread *thread, |
| 2988 | struct callchain_cursor *cursor, |
Jiri Olsa | 32dcd02 | 2019-07-21 13:23:51 +0200 | [diff] [blame] | 2989 | struct evsel *evsel, |
Chris Phlipot | 9919a65 | 2016-04-28 01:19:06 -0700 | [diff] [blame] | 2990 | struct perf_sample *sample, |
| 2991 | struct symbol **parent, |
| 2992 | struct addr_location *root_al, |
| 2993 | int max_stack) |
| 2994 | { |
| 2995 | int ret = 0; |
| 2996 | |
Jiri Olsa | 914eb9c | 2017-08-06 16:39:39 +0200 | [diff] [blame] | 2997 | callchain_cursor_reset(cursor); |
Chris Phlipot | 9919a65 | 2016-04-28 01:19:06 -0700 | [diff] [blame] | 2998 | |
| 2999 | if (callchain_param.order == ORDER_CALLEE) { |
| 3000 | ret = thread__resolve_callchain_sample(thread, cursor, |
| 3001 | evsel, sample, |
| 3002 | parent, root_al, |
| 3003 | max_stack); |
| 3004 | if (ret) |
| 3005 | return ret; |
| 3006 | ret = thread__resolve_callchain_unwind(thread, cursor, |
| 3007 | evsel, sample, |
| 3008 | max_stack); |
| 3009 | } else { |
| 3010 | ret = thread__resolve_callchain_unwind(thread, cursor, |
| 3011 | evsel, sample, |
| 3012 | max_stack); |
| 3013 | if (ret) |
| 3014 | return ret; |
| 3015 | ret = thread__resolve_callchain_sample(thread, cursor, |
| 3016 | evsel, sample, |
| 3017 | parent, root_al, |
| 3018 | max_stack); |
| 3019 | } |
| 3020 | |
| 3021 | return ret; |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 3022 | } |
David Ahern | 35feee1 | 2013-09-28 13:12:58 -0600 | [diff] [blame] | 3023 | |
| 3024 | int machine__for_each_thread(struct machine *machine, |
| 3025 | int (*fn)(struct thread *thread, void *p), |
| 3026 | void *priv) |
| 3027 | { |
Kan Liang | 91e467b | 2017-09-10 19:23:14 -0700 | [diff] [blame] | 3028 | struct threads *threads; |
David Ahern | 35feee1 | 2013-09-28 13:12:58 -0600 | [diff] [blame] | 3029 | struct rb_node *nd; |
| 3030 | struct thread *thread; |
| 3031 | int rc = 0; |
Kan Liang | 91e467b | 2017-09-10 19:23:14 -0700 | [diff] [blame] | 3032 | int i; |
David Ahern | 35feee1 | 2013-09-28 13:12:58 -0600 | [diff] [blame] | 3033 | |
Kan Liang | 91e467b | 2017-09-10 19:23:14 -0700 | [diff] [blame] | 3034 | for (i = 0; i < THREADS__TABLE_SIZE; i++) { |
| 3035 | threads = &machine->threads[i]; |
Davidlohr Bueso | f3acb3a | 2018-12-06 11:18:14 -0800 | [diff] [blame] | 3036 | for (nd = rb_first_cached(&threads->entries); nd; |
| 3037 | nd = rb_next(nd)) { |
Kan Liang | 91e467b | 2017-09-10 19:23:14 -0700 | [diff] [blame] | 3038 | thread = rb_entry(nd, struct thread, rb_node); |
| 3039 | rc = fn(thread, priv); |
| 3040 | if (rc != 0) |
| 3041 | return rc; |
| 3042 | } |
David Ahern | 35feee1 | 2013-09-28 13:12:58 -0600 | [diff] [blame] | 3043 | |
Kan Liang | 91e467b | 2017-09-10 19:23:14 -0700 | [diff] [blame] | 3044 | list_for_each_entry(thread, &threads->dead, node) { |
| 3045 | rc = fn(thread, priv); |
| 3046 | if (rc != 0) |
| 3047 | return rc; |
| 3048 | } |
David Ahern | 35feee1 | 2013-09-28 13:12:58 -0600 | [diff] [blame] | 3049 | } |
| 3050 | return rc; |
| 3051 | } |
Arnaldo Carvalho de Melo | 58d925d | 2013-11-11 11:28:02 -0300 | [diff] [blame] | 3052 | |
Adrian Hunter | a5499b3 | 2015-05-29 16:33:30 +0300 | [diff] [blame] | 3053 | int machines__for_each_thread(struct machines *machines, |
| 3054 | int (*fn)(struct thread *thread, void *p), |
| 3055 | void *priv) |
| 3056 | { |
| 3057 | struct rb_node *nd; |
| 3058 | int rc = 0; |
| 3059 | |
| 3060 | rc = machine__for_each_thread(&machines->host, fn, priv); |
| 3061 | if (rc != 0) |
| 3062 | return rc; |
| 3063 | |
Davidlohr Bueso | f3acb3a | 2018-12-06 11:18:14 -0800 | [diff] [blame] | 3064 | for (nd = rb_first_cached(&machines->guests); nd; nd = rb_next(nd)) { |
Adrian Hunter | a5499b3 | 2015-05-29 16:33:30 +0300 | [diff] [blame] | 3065 | struct machine *machine = rb_entry(nd, struct machine, rb_node); |
| 3066 | |
| 3067 | rc = machine__for_each_thread(machine, fn, priv); |
| 3068 | if (rc != 0) |
| 3069 | return rc; |
| 3070 | } |
| 3071 | return rc; |
| 3072 | } |
| 3073 | |
Adrian Hunter | b9d266b | 2014-07-22 16:17:25 +0300 | [diff] [blame] | 3074 | pid_t machine__get_current_tid(struct machine *machine, int cpu) |
| 3075 | { |
Adrian Hunter | 5501e92 | 2021-01-07 19:41:59 +0200 | [diff] [blame] | 3076 | int nr_cpus = min(machine->env->nr_cpus_avail, MAX_NR_CPUS); |
Kyle Meyer | 8c72746 | 2019-08-27 16:43:50 -0500 | [diff] [blame] | 3077 | |
| 3078 | if (cpu < 0 || cpu >= nr_cpus || !machine->current_tid) |
Adrian Hunter | b9d266b | 2014-07-22 16:17:25 +0300 | [diff] [blame] | 3079 | return -1; |
| 3080 | |
| 3081 | return machine->current_tid[cpu]; |
| 3082 | } |
| 3083 | |
| 3084 | int machine__set_current_tid(struct machine *machine, int cpu, pid_t pid, |
| 3085 | pid_t tid) |
| 3086 | { |
| 3087 | struct thread *thread; |
Adrian Hunter | 5501e92 | 2021-01-07 19:41:59 +0200 | [diff] [blame] | 3088 | int nr_cpus = min(machine->env->nr_cpus_avail, MAX_NR_CPUS); |
Adrian Hunter | b9d266b | 2014-07-22 16:17:25 +0300 | [diff] [blame] | 3089 | |
| 3090 | if (cpu < 0) |
| 3091 | return -EINVAL; |
| 3092 | |
| 3093 | if (!machine->current_tid) { |
| 3094 | int i; |
| 3095 | |
Kyle Meyer | 8c72746 | 2019-08-27 16:43:50 -0500 | [diff] [blame] | 3096 | machine->current_tid = calloc(nr_cpus, sizeof(pid_t)); |
Adrian Hunter | b9d266b | 2014-07-22 16:17:25 +0300 | [diff] [blame] | 3097 | if (!machine->current_tid) |
| 3098 | return -ENOMEM; |
Kyle Meyer | 8c72746 | 2019-08-27 16:43:50 -0500 | [diff] [blame] | 3099 | for (i = 0; i < nr_cpus; i++) |
Adrian Hunter | b9d266b | 2014-07-22 16:17:25 +0300 | [diff] [blame] | 3100 | machine->current_tid[i] = -1; |
| 3101 | } |
| 3102 | |
Kyle Meyer | 8c72746 | 2019-08-27 16:43:50 -0500 | [diff] [blame] | 3103 | if (cpu >= nr_cpus) { |
Adrian Hunter | b9d266b | 2014-07-22 16:17:25 +0300 | [diff] [blame] | 3104 | pr_err("Requested CPU %d too large. ", cpu); |
| 3105 | pr_err("Consider raising MAX_NR_CPUS\n"); |
| 3106 | return -EINVAL; |
| 3107 | } |
| 3108 | |
| 3109 | machine->current_tid[cpu] = tid; |
| 3110 | |
| 3111 | thread = machine__findnew_thread(machine, pid, tid); |
| 3112 | if (!thread) |
| 3113 | return -ENOMEM; |
| 3114 | |
| 3115 | thread->cpu = cpu; |
Arnaldo Carvalho de Melo | b91fc39 | 2015-04-06 20:43:22 -0300 | [diff] [blame] | 3116 | thread__put(thread); |
Adrian Hunter | b9d266b | 2014-07-22 16:17:25 +0300 | [diff] [blame] | 3117 | |
| 3118 | return 0; |
| 3119 | } |
Adrian Hunter | fbe2af4 | 2014-08-15 22:08:39 +0300 | [diff] [blame] | 3120 | |
Adrian Hunter | dbbd34a | 2018-05-17 12:21:53 +0300 | [diff] [blame] | 3121 | /* |
Alexandre Truong | b9f6fbb | 2021-12-17 15:45:20 +0000 | [diff] [blame] | 3122 | * Compares the raw arch string. N.B. see instead perf_env__arch() or |
| 3123 | * machine__normalized_is() if a normalized arch is needed. |
Adrian Hunter | dbbd34a | 2018-05-17 12:21:53 +0300 | [diff] [blame] | 3124 | */ |
| 3125 | bool machine__is(struct machine *machine, const char *arch) |
| 3126 | { |
| 3127 | return machine && !strcmp(perf_env__raw_arch(machine->env), arch); |
| 3128 | } |
| 3129 | |
Alexandre Truong | b9f6fbb | 2021-12-17 15:45:20 +0000 | [diff] [blame] | 3130 | bool machine__normalized_is(struct machine *machine, const char *arch) |
| 3131 | { |
| 3132 | return machine && !strcmp(perf_env__arch(machine->env), arch); |
| 3133 | } |
| 3134 | |
Adrian Hunter | 9cecca3 | 2018-05-22 13:54:32 +0300 | [diff] [blame] | 3135 | int machine__nr_cpus_avail(struct machine *machine) |
| 3136 | { |
| 3137 | return machine ? perf_env__nr_cpus_avail(machine->env) : 0; |
| 3138 | } |
| 3139 | |
Adrian Hunter | fbe2af4 | 2014-08-15 22:08:39 +0300 | [diff] [blame] | 3140 | int machine__get_kernel_start(struct machine *machine) |
| 3141 | { |
Arnaldo Carvalho de Melo | a5e813c | 2015-09-30 11:54:04 -0300 | [diff] [blame] | 3142 | struct map *map = machine__kernel_map(machine); |
Adrian Hunter | fbe2af4 | 2014-08-15 22:08:39 +0300 | [diff] [blame] | 3143 | int err = 0; |
| 3144 | |
| 3145 | /* |
| 3146 | * The only addresses above 2^63 are kernel addresses of a 64-bit |
| 3147 | * kernel. Note that addresses are unsigned so that on a 32-bit system |
| 3148 | * all addresses including kernel addresses are less than 2^32. In |
| 3149 | * that case (32-bit system), if the kernel mapping is unknown, all |
| 3150 | * addresses will be assumed to be in user space - see |
| 3151 | * machine__kernel_ip(). |
| 3152 | */ |
| 3153 | machine->kernel_start = 1ULL << 63; |
| 3154 | if (map) { |
Arnaldo Carvalho de Melo | be39db9 | 2016-09-01 19:25:52 -0300 | [diff] [blame] | 3155 | err = map__load(map); |
Adrian Hunter | 19422a9 | 2018-05-17 12:21:54 +0300 | [diff] [blame] | 3156 | /* |
| 3157 | * On x86_64, PTI entry trampolines are less than the |
| 3158 | * start of kernel text, but still above 2^63. So leave |
| 3159 | * kernel_start = 1ULL << 63 for x86_64. |
| 3160 | */ |
| 3161 | if (!err && !machine__is(machine, "x86_64")) |
Adrian Hunter | fbe2af4 | 2014-08-15 22:08:39 +0300 | [diff] [blame] | 3162 | machine->kernel_start = map->start; |
| 3163 | } |
| 3164 | return err; |
| 3165 | } |
Arnaldo Carvalho de Melo | aa7cc2a | 2015-05-29 11:31:12 -0300 | [diff] [blame] | 3166 | |
Adrian Hunter | 8e80ad9 | 2018-11-06 23:07:10 +0200 | [diff] [blame] | 3167 | u8 machine__addr_cpumode(struct machine *machine, u8 cpumode, u64 addr) |
| 3168 | { |
| 3169 | u8 addr_cpumode = cpumode; |
| 3170 | bool kernel_ip; |
| 3171 | |
| 3172 | if (!machine->single_address_space) |
| 3173 | goto out; |
| 3174 | |
| 3175 | kernel_ip = machine__kernel_ip(machine, addr); |
| 3176 | switch (cpumode) { |
| 3177 | case PERF_RECORD_MISC_KERNEL: |
| 3178 | case PERF_RECORD_MISC_USER: |
| 3179 | addr_cpumode = kernel_ip ? PERF_RECORD_MISC_KERNEL : |
| 3180 | PERF_RECORD_MISC_USER; |
| 3181 | break; |
| 3182 | case PERF_RECORD_MISC_GUEST_KERNEL: |
| 3183 | case PERF_RECORD_MISC_GUEST_USER: |
| 3184 | addr_cpumode = kernel_ip ? PERF_RECORD_MISC_GUEST_KERNEL : |
| 3185 | PERF_RECORD_MISC_GUEST_USER; |
| 3186 | break; |
| 3187 | default: |
| 3188 | break; |
| 3189 | } |
| 3190 | out: |
| 3191 | return addr_cpumode; |
| 3192 | } |
| 3193 | |
Arnaldo Carvalho de Melo | 0e3149f | 2019-11-19 18:44:22 -0300 | [diff] [blame] | 3194 | struct dso *machine__findnew_dso_id(struct machine *machine, const char *filename, struct dso_id *id) |
| 3195 | { |
| 3196 | return dsos__findnew_id(&machine->dsos, filename, id); |
| 3197 | } |
| 3198 | |
Arnaldo Carvalho de Melo | aa7cc2a | 2015-05-29 11:31:12 -0300 | [diff] [blame] | 3199 | struct dso *machine__findnew_dso(struct machine *machine, const char *filename) |
| 3200 | { |
Arnaldo Carvalho de Melo | 0e3149f | 2019-11-19 18:44:22 -0300 | [diff] [blame] | 3201 | return machine__findnew_dso_id(machine, filename, NULL); |
Arnaldo Carvalho de Melo | aa7cc2a | 2015-05-29 11:31:12 -0300 | [diff] [blame] | 3202 | } |
Arnaldo Carvalho de Melo | c3168b0 | 2015-07-22 16:14:29 -0300 | [diff] [blame] | 3203 | |
| 3204 | char *machine__resolve_kernel_addr(void *vmachine, unsigned long long *addrp, char **modp) |
| 3205 | { |
| 3206 | struct machine *machine = vmachine; |
| 3207 | struct map *map; |
Arnaldo Carvalho de Melo | 107cad9 | 2018-04-30 12:20:54 -0300 | [diff] [blame] | 3208 | struct symbol *sym = machine__find_kernel_symbol(machine, *addrp, &map); |
Arnaldo Carvalho de Melo | c3168b0 | 2015-07-22 16:14:29 -0300 | [diff] [blame] | 3209 | |
| 3210 | if (sym == NULL) |
| 3211 | return NULL; |
| 3212 | |
| 3213 | *modp = __map__is_kmodule(map) ? (char *)map->dso->short_name : NULL; |
| 3214 | *addrp = map->unmap_ip(map, sym->start); |
| 3215 | return sym->name; |
| 3216 | } |
Jiri Olsa | c57f5ea | 2020-09-13 23:03:08 +0200 | [diff] [blame] | 3217 | |
| 3218 | int machine__for_each_dso(struct machine *machine, machine__dso_t fn, void *priv) |
| 3219 | { |
| 3220 | struct dso *pos; |
| 3221 | int err = 0; |
| 3222 | |
| 3223 | list_for_each_entry(pos, &machine->dsos.head, node) { |
| 3224 | if (fn(pos, machine, priv)) |
| 3225 | err = -1; |
| 3226 | } |
| 3227 | return err; |
| 3228 | } |