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