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