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