Arnaldo Carvalho de Melo | 76b31a2 | 2017-04-18 12:26:44 -0300 | [diff] [blame] | 1 | #include <dirent.h> |
Arnaldo Carvalho de Melo | a43783a | 2017-04-18 10:46:11 -0300 | [diff] [blame] | 2 | #include <errno.h> |
Arnaldo Carvalho de Melo | fd20e81 | 2017-04-17 15:23:08 -0300 | [diff] [blame] | 3 | #include <inttypes.h> |
Arnaldo Carvalho de Melo | 1eae20c | 2017-04-18 12:33:30 -0300 | [diff] [blame] | 4 | #include <regex.h> |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 5 | #include "callchain.h" |
Arnaldo Carvalho de Melo | b0a7d1a | 2012-10-06 16:26:02 -0300 | [diff] [blame] | 6 | #include "debug.h" |
| 7 | #include "event.h" |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 8 | #include "evsel.h" |
| 9 | #include "hist.h" |
Arnaldo Carvalho de Melo | 9d2f8e2 | 2012-10-06 15:43:20 -0300 | [diff] [blame] | 10 | #include "machine.h" |
| 11 | #include "map.h" |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 12 | #include "sort.h" |
Arnaldo Carvalho de Melo | 69d2591 | 2012-11-09 11:32:52 -0300 | [diff] [blame] | 13 | #include "strlist.h" |
Arnaldo Carvalho de Melo | 9d2f8e2 | 2012-10-06 15:43:20 -0300 | [diff] [blame] | 14 | #include "thread.h" |
Adrian Hunter | d027b64 | 2014-07-23 14:23:00 +0300 | [diff] [blame] | 15 | #include "vdso.h" |
Arnaldo Carvalho de Melo | 9d2f8e2 | 2012-10-06 15:43:20 -0300 | [diff] [blame] | 16 | #include <stdbool.h> |
Arnaldo Carvalho de Melo | 7a8ef4c | 2017-04-19 20:57:47 -0300 | [diff] [blame] | 17 | #include <sys/types.h> |
| 18 | #include <sys/stat.h> |
| 19 | #include <unistd.h> |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 20 | #include "unwind.h" |
Andi Kleen | 8b7bad5 | 2014-11-12 18:05:20 -0800 | [diff] [blame] | 21 | #include "linux/hash.h" |
Hari Bathini | f3b3614 | 2017-03-08 02:11:43 +0530 | [diff] [blame] | 22 | #include "asm/bug.h" |
Arnaldo Carvalho de Melo | 9d2f8e2 | 2012-10-06 15:43:20 -0300 | [diff] [blame] | 23 | |
Arnaldo Carvalho de Melo | 3d689ed | 2017-04-17 16:10:49 -0300 | [diff] [blame] | 24 | #include "sane_ctype.h" |
| 25 | #include <symbol/kallsyms.h> |
| 26 | |
Arnaldo Carvalho de Melo | b91fc39 | 2015-04-06 20:43:22 -0300 | [diff] [blame] | 27 | static void __machine__remove_thread(struct machine *machine, struct thread *th, bool lock); |
| 28 | |
Arnaldo Carvalho de Melo | e167f99 | 2014-10-14 15:07:48 -0300 | [diff] [blame] | 29 | static void dsos__init(struct dsos *dsos) |
| 30 | { |
| 31 | INIT_LIST_HEAD(&dsos->head); |
| 32 | dsos->root = RB_ROOT; |
Arnaldo Carvalho de Melo | e880784 | 2015-06-01 15:40:01 -0300 | [diff] [blame] | 33 | pthread_rwlock_init(&dsos->lock, NULL); |
Arnaldo Carvalho de Melo | e167f99 | 2014-10-14 15:07:48 -0300 | [diff] [blame] | 34 | } |
| 35 | |
Arnaldo Carvalho de Melo | 69d2591 | 2012-11-09 11:32:52 -0300 | [diff] [blame] | 36 | int machine__init(struct machine *machine, const char *root_dir, pid_t pid) |
| 37 | { |
Wang Nan | 93b0ba3 | 2015-12-08 02:25:44 +0000 | [diff] [blame] | 38 | memset(machine, 0, sizeof(*machine)); |
Arnaldo Carvalho de Melo | 11246c7 | 2014-10-21 17:29:02 -0300 | [diff] [blame] | 39 | map_groups__init(&machine->kmaps, machine); |
Arnaldo Carvalho de Melo | 69d2591 | 2012-11-09 11:32:52 -0300 | [diff] [blame] | 40 | RB_CLEAR_NODE(&machine->rb_node); |
Arnaldo Carvalho de Melo | 3d39ac5 | 2015-05-28 13:06:42 -0300 | [diff] [blame] | 41 | dsos__init(&machine->dsos); |
Arnaldo Carvalho de Melo | 69d2591 | 2012-11-09 11:32:52 -0300 | [diff] [blame] | 42 | |
| 43 | machine->threads = RB_ROOT; |
Arnaldo Carvalho de Melo | b91fc39 | 2015-04-06 20:43:22 -0300 | [diff] [blame] | 44 | pthread_rwlock_init(&machine->threads_lock, NULL); |
Arnaldo Carvalho de Melo | d2c1103 | 2016-05-04 10:09:33 -0300 | [diff] [blame] | 45 | machine->nr_threads = 0; |
Arnaldo Carvalho de Melo | 69d2591 | 2012-11-09 11:32:52 -0300 | [diff] [blame] | 46 | INIT_LIST_HEAD(&machine->dead_threads); |
| 47 | machine->last_match = NULL; |
| 48 | |
Adrian Hunter | d027b64 | 2014-07-23 14:23:00 +0300 | [diff] [blame] | 49 | machine->vdso_info = NULL; |
Arnaldo Carvalho de Melo | 4cde998 | 2015-09-09 12:25:00 -0300 | [diff] [blame] | 50 | machine->env = NULL; |
Adrian Hunter | d027b64 | 2014-07-23 14:23:00 +0300 | [diff] [blame] | 51 | |
Arnaldo Carvalho de Melo | 69d2591 | 2012-11-09 11:32:52 -0300 | [diff] [blame] | 52 | machine->pid = pid; |
| 53 | |
Jiri Olsa | 14bd6d2 | 2014-01-07 13:47:19 +0100 | [diff] [blame] | 54 | machine->id_hdr_size = 0; |
Arnaldo Carvalho de Melo | caf8a0d | 2016-05-17 11:56:24 -0300 | [diff] [blame] | 55 | machine->kptr_restrict_warned = false; |
Adrian Hunter | cfe1c41 | 2014-07-31 09:00:45 +0300 | [diff] [blame] | 56 | machine->comm_exec = false; |
Adrian Hunter | fbe2af4 | 2014-08-15 22:08:39 +0300 | [diff] [blame] | 57 | machine->kernel_start = 0; |
Adrian Hunter | 611a5ce | 2013-08-08 14:32:20 +0300 | [diff] [blame] | 58 | |
Masami Hiramatsu | cc1121a | 2015-12-09 11:11:33 +0900 | [diff] [blame] | 59 | memset(machine->vmlinux_maps, 0, sizeof(machine->vmlinux_maps)); |
| 60 | |
Arnaldo Carvalho de Melo | 69d2591 | 2012-11-09 11:32:52 -0300 | [diff] [blame] | 61 | machine->root_dir = strdup(root_dir); |
| 62 | if (machine->root_dir == NULL) |
| 63 | return -ENOMEM; |
| 64 | |
| 65 | if (pid != HOST_KERNEL_ID) { |
Adrian Hunter | 1fcb876 | 2014-07-14 13:02:25 +0300 | [diff] [blame] | 66 | struct thread *thread = machine__findnew_thread(machine, -1, |
Adrian Hunter | 314add6 | 2013-08-27 11:23:03 +0300 | [diff] [blame] | 67 | pid); |
Arnaldo Carvalho de Melo | 69d2591 | 2012-11-09 11:32:52 -0300 | [diff] [blame] | 68 | char comm[64]; |
| 69 | |
| 70 | if (thread == NULL) |
| 71 | return -ENOMEM; |
| 72 | |
| 73 | snprintf(comm, sizeof(comm), "[guest/%d]", pid); |
Frederic Weisbecker | 162f0be | 2013-09-11 16:18:24 +0200 | [diff] [blame] | 74 | thread__set_comm(thread, comm, 0); |
Arnaldo Carvalho de Melo | b91fc39 | 2015-04-06 20:43:22 -0300 | [diff] [blame] | 75 | thread__put(thread); |
Arnaldo Carvalho de Melo | 69d2591 | 2012-11-09 11:32:52 -0300 | [diff] [blame] | 76 | } |
| 77 | |
Adrian Hunter | b9d266b | 2014-07-22 16:17:25 +0300 | [diff] [blame] | 78 | machine->current_tid = NULL; |
| 79 | |
Arnaldo Carvalho de Melo | 69d2591 | 2012-11-09 11:32:52 -0300 | [diff] [blame] | 80 | return 0; |
| 81 | } |
| 82 | |
David Ahern | 8fb598e | 2013-09-28 13:13:00 -0600 | [diff] [blame] | 83 | struct machine *machine__new_host(void) |
| 84 | { |
| 85 | struct machine *machine = malloc(sizeof(*machine)); |
| 86 | |
| 87 | if (machine != NULL) { |
| 88 | machine__init(machine, "", HOST_KERNEL_ID); |
| 89 | |
| 90 | if (machine__create_kernel_maps(machine) < 0) |
| 91 | goto out_delete; |
| 92 | } |
| 93 | |
| 94 | return machine; |
| 95 | out_delete: |
| 96 | free(machine); |
| 97 | return NULL; |
| 98 | } |
| 99 | |
Arnaldo Carvalho de Melo | 7d132ca | 2017-01-05 15:31:48 -0300 | [diff] [blame] | 100 | struct machine *machine__new_kallsyms(void) |
| 101 | { |
| 102 | struct machine *machine = machine__new_host(); |
| 103 | /* |
| 104 | * FIXME: |
| 105 | * 1) MAP__FUNCTION will go away when we stop loading separate maps for |
| 106 | * functions and data objects. |
| 107 | * 2) We should switch to machine__load_kallsyms(), i.e. not explicitely |
| 108 | * ask for not using the kcore parsing code, once this one is fixed |
| 109 | * to create a map per module. |
| 110 | */ |
| 111 | if (machine && __machine__load_kallsyms(machine, "/proc/kallsyms", MAP__FUNCTION, true) <= 0) { |
| 112 | machine__delete(machine); |
| 113 | machine = NULL; |
| 114 | } |
| 115 | |
| 116 | return machine; |
| 117 | } |
| 118 | |
Arnaldo Carvalho de Melo | d3a7c48 | 2015-06-02 11:53:26 -0300 | [diff] [blame] | 119 | static void dsos__purge(struct dsos *dsos) |
Arnaldo Carvalho de Melo | 69d2591 | 2012-11-09 11:32:52 -0300 | [diff] [blame] | 120 | { |
| 121 | struct dso *pos, *n; |
| 122 | |
Arnaldo Carvalho de Melo | e880784 | 2015-06-01 15:40:01 -0300 | [diff] [blame] | 123 | pthread_rwlock_wrlock(&dsos->lock); |
| 124 | |
Waiman Long | 8fa7d87 | 2014-09-29 16:07:28 -0400 | [diff] [blame] | 125 | list_for_each_entry_safe(pos, n, &dsos->head, node) { |
Waiman Long | 4598a0a | 2014-09-30 13:36:15 -0400 | [diff] [blame] | 126 | RB_CLEAR_NODE(&pos->rb_node); |
Adrian Hunter | e266a75 | 2015-11-13 11:48:30 +0200 | [diff] [blame] | 127 | pos->root = NULL; |
Arnaldo Carvalho de Melo | d3a7c48 | 2015-06-02 11:53:26 -0300 | [diff] [blame] | 128 | list_del_init(&pos->node); |
| 129 | dso__put(pos); |
Arnaldo Carvalho de Melo | 69d2591 | 2012-11-09 11:32:52 -0300 | [diff] [blame] | 130 | } |
Arnaldo Carvalho de Melo | e880784 | 2015-06-01 15:40:01 -0300 | [diff] [blame] | 131 | |
| 132 | pthread_rwlock_unlock(&dsos->lock); |
Arnaldo Carvalho de Melo | d3a7c48 | 2015-06-02 11:53:26 -0300 | [diff] [blame] | 133 | } |
Arnaldo Carvalho de Melo | e880784 | 2015-06-01 15:40:01 -0300 | [diff] [blame] | 134 | |
Arnaldo Carvalho de Melo | d3a7c48 | 2015-06-02 11:53:26 -0300 | [diff] [blame] | 135 | static void dsos__exit(struct dsos *dsos) |
| 136 | { |
| 137 | dsos__purge(dsos); |
Arnaldo Carvalho de Melo | e880784 | 2015-06-01 15:40:01 -0300 | [diff] [blame] | 138 | pthread_rwlock_destroy(&dsos->lock); |
Arnaldo Carvalho de Melo | 69d2591 | 2012-11-09 11:32:52 -0300 | [diff] [blame] | 139 | } |
| 140 | |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 141 | void machine__delete_threads(struct machine *machine) |
| 142 | { |
Arnaldo Carvalho de Melo | b91fc39 | 2015-04-06 20:43:22 -0300 | [diff] [blame] | 143 | struct rb_node *nd; |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 144 | |
Arnaldo Carvalho de Melo | b91fc39 | 2015-04-06 20:43:22 -0300 | [diff] [blame] | 145 | pthread_rwlock_wrlock(&machine->threads_lock); |
| 146 | nd = rb_first(&machine->threads); |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 147 | while (nd) { |
| 148 | struct thread *t = rb_entry(nd, struct thread, rb_node); |
| 149 | |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 150 | nd = rb_next(nd); |
Arnaldo Carvalho de Melo | b91fc39 | 2015-04-06 20:43:22 -0300 | [diff] [blame] | 151 | __machine__remove_thread(machine, t, false); |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 152 | } |
Arnaldo Carvalho de Melo | b91fc39 | 2015-04-06 20:43:22 -0300 | [diff] [blame] | 153 | pthread_rwlock_unlock(&machine->threads_lock); |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 154 | } |
| 155 | |
Arnaldo Carvalho de Melo | 69d2591 | 2012-11-09 11:32:52 -0300 | [diff] [blame] | 156 | void machine__exit(struct machine *machine) |
| 157 | { |
Masami Hiramatsu | ebe9729 | 2015-11-18 15:40:24 +0900 | [diff] [blame] | 158 | machine__destroy_kernel_maps(machine); |
Arnaldo Carvalho de Melo | 69d2591 | 2012-11-09 11:32:52 -0300 | [diff] [blame] | 159 | map_groups__exit(&machine->kmaps); |
Arnaldo Carvalho de Melo | e880784 | 2015-06-01 15:40:01 -0300 | [diff] [blame] | 160 | dsos__exit(&machine->dsos); |
Arnaldo Carvalho de Melo | 9a4388c | 2015-05-29 11:54:08 -0300 | [diff] [blame] | 161 | machine__exit_vdso(machine); |
Arnaldo Carvalho de Melo | 0466252 | 2013-12-26 17:41:15 -0300 | [diff] [blame] | 162 | zfree(&machine->root_dir); |
Adrian Hunter | b9d266b | 2014-07-22 16:17:25 +0300 | [diff] [blame] | 163 | zfree(&machine->current_tid); |
Arnaldo Carvalho de Melo | b91fc39 | 2015-04-06 20:43:22 -0300 | [diff] [blame] | 164 | pthread_rwlock_destroy(&machine->threads_lock); |
Arnaldo Carvalho de Melo | 69d2591 | 2012-11-09 11:32:52 -0300 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | void machine__delete(struct machine *machine) |
| 168 | { |
Arnaldo Carvalho de Melo | 32ca678 | 2016-06-22 10:19:11 -0300 | [diff] [blame] | 169 | if (machine) { |
| 170 | machine__exit(machine); |
| 171 | free(machine); |
| 172 | } |
Arnaldo Carvalho de Melo | 69d2591 | 2012-11-09 11:32:52 -0300 | [diff] [blame] | 173 | } |
| 174 | |
Arnaldo Carvalho de Melo | 876650e | 2012-12-18 19:15:48 -0300 | [diff] [blame] | 175 | void machines__init(struct machines *machines) |
| 176 | { |
| 177 | machine__init(&machines->host, "", HOST_KERNEL_ID); |
| 178 | machines->guests = RB_ROOT; |
| 179 | } |
| 180 | |
| 181 | void machines__exit(struct machines *machines) |
| 182 | { |
| 183 | machine__exit(&machines->host); |
| 184 | /* XXX exit guest */ |
| 185 | } |
| 186 | |
| 187 | struct machine *machines__add(struct machines *machines, pid_t pid, |
Arnaldo Carvalho de Melo | 69d2591 | 2012-11-09 11:32:52 -0300 | [diff] [blame] | 188 | const char *root_dir) |
| 189 | { |
Arnaldo Carvalho de Melo | 876650e | 2012-12-18 19:15:48 -0300 | [diff] [blame] | 190 | struct rb_node **p = &machines->guests.rb_node; |
Arnaldo Carvalho de Melo | 69d2591 | 2012-11-09 11:32:52 -0300 | [diff] [blame] | 191 | struct rb_node *parent = NULL; |
| 192 | struct machine *pos, *machine = malloc(sizeof(*machine)); |
| 193 | |
| 194 | if (machine == NULL) |
| 195 | return NULL; |
| 196 | |
| 197 | if (machine__init(machine, root_dir, pid) != 0) { |
| 198 | free(machine); |
| 199 | return NULL; |
| 200 | } |
| 201 | |
| 202 | while (*p != NULL) { |
| 203 | parent = *p; |
| 204 | pos = rb_entry(parent, struct machine, rb_node); |
| 205 | if (pid < pos->pid) |
| 206 | p = &(*p)->rb_left; |
| 207 | else |
| 208 | p = &(*p)->rb_right; |
| 209 | } |
| 210 | |
| 211 | rb_link_node(&machine->rb_node, parent, p); |
Arnaldo Carvalho de Melo | 876650e | 2012-12-18 19:15:48 -0300 | [diff] [blame] | 212 | rb_insert_color(&machine->rb_node, &machines->guests); |
Arnaldo Carvalho de Melo | 69d2591 | 2012-11-09 11:32:52 -0300 | [diff] [blame] | 213 | |
| 214 | return machine; |
| 215 | } |
| 216 | |
Adrian Hunter | cfe1c41 | 2014-07-31 09:00:45 +0300 | [diff] [blame] | 217 | void machines__set_comm_exec(struct machines *machines, bool comm_exec) |
| 218 | { |
| 219 | struct rb_node *nd; |
| 220 | |
| 221 | machines->host.comm_exec = comm_exec; |
| 222 | |
| 223 | for (nd = rb_first(&machines->guests); nd; nd = rb_next(nd)) { |
| 224 | struct machine *machine = rb_entry(nd, struct machine, rb_node); |
| 225 | |
| 226 | machine->comm_exec = comm_exec; |
| 227 | } |
| 228 | } |
| 229 | |
Arnaldo Carvalho de Melo | 876650e | 2012-12-18 19:15:48 -0300 | [diff] [blame] | 230 | struct machine *machines__find(struct machines *machines, pid_t pid) |
Arnaldo Carvalho de Melo | 69d2591 | 2012-11-09 11:32:52 -0300 | [diff] [blame] | 231 | { |
Arnaldo Carvalho de Melo | 876650e | 2012-12-18 19:15:48 -0300 | [diff] [blame] | 232 | struct rb_node **p = &machines->guests.rb_node; |
Arnaldo Carvalho de Melo | 69d2591 | 2012-11-09 11:32:52 -0300 | [diff] [blame] | 233 | struct rb_node *parent = NULL; |
| 234 | struct machine *machine; |
| 235 | struct machine *default_machine = NULL; |
| 236 | |
Arnaldo Carvalho de Melo | 876650e | 2012-12-18 19:15:48 -0300 | [diff] [blame] | 237 | if (pid == HOST_KERNEL_ID) |
| 238 | return &machines->host; |
| 239 | |
Arnaldo Carvalho de Melo | 69d2591 | 2012-11-09 11:32:52 -0300 | [diff] [blame] | 240 | while (*p != NULL) { |
| 241 | parent = *p; |
| 242 | machine = rb_entry(parent, struct machine, rb_node); |
| 243 | if (pid < machine->pid) |
| 244 | p = &(*p)->rb_left; |
| 245 | else if (pid > machine->pid) |
| 246 | p = &(*p)->rb_right; |
| 247 | else |
| 248 | return machine; |
| 249 | if (!machine->pid) |
| 250 | default_machine = machine; |
| 251 | } |
| 252 | |
| 253 | return default_machine; |
| 254 | } |
| 255 | |
Arnaldo Carvalho de Melo | 876650e | 2012-12-18 19:15:48 -0300 | [diff] [blame] | 256 | struct machine *machines__findnew(struct machines *machines, pid_t pid) |
Arnaldo Carvalho de Melo | 69d2591 | 2012-11-09 11:32:52 -0300 | [diff] [blame] | 257 | { |
| 258 | char path[PATH_MAX]; |
| 259 | const char *root_dir = ""; |
| 260 | struct machine *machine = machines__find(machines, pid); |
| 261 | |
| 262 | if (machine && (machine->pid == pid)) |
| 263 | goto out; |
| 264 | |
| 265 | if ((pid != HOST_KERNEL_ID) && |
| 266 | (pid != DEFAULT_GUEST_KERNEL_ID) && |
| 267 | (symbol_conf.guestmount)) { |
| 268 | sprintf(path, "%s/%d", symbol_conf.guestmount, pid); |
| 269 | if (access(path, R_OK)) { |
| 270 | static struct strlist *seen; |
| 271 | |
| 272 | if (!seen) |
Arnaldo Carvalho de Melo | 4a77e21 | 2015-07-20 12:13:34 -0300 | [diff] [blame] | 273 | seen = strlist__new(NULL, NULL); |
Arnaldo Carvalho de Melo | 69d2591 | 2012-11-09 11:32:52 -0300 | [diff] [blame] | 274 | |
| 275 | if (!strlist__has_entry(seen, path)) { |
| 276 | pr_err("Can't access file %s\n", path); |
| 277 | strlist__add(seen, path); |
| 278 | } |
| 279 | machine = NULL; |
| 280 | goto out; |
| 281 | } |
| 282 | root_dir = path; |
| 283 | } |
| 284 | |
| 285 | machine = machines__add(machines, pid, root_dir); |
| 286 | out: |
| 287 | return machine; |
| 288 | } |
| 289 | |
Arnaldo Carvalho de Melo | 876650e | 2012-12-18 19:15:48 -0300 | [diff] [blame] | 290 | void machines__process_guests(struct machines *machines, |
| 291 | machine__process_t process, void *data) |
Arnaldo Carvalho de Melo | 69d2591 | 2012-11-09 11:32:52 -0300 | [diff] [blame] | 292 | { |
| 293 | struct rb_node *nd; |
| 294 | |
Arnaldo Carvalho de Melo | 876650e | 2012-12-18 19:15:48 -0300 | [diff] [blame] | 295 | for (nd = rb_first(&machines->guests); nd; nd = rb_next(nd)) { |
Arnaldo Carvalho de Melo | 69d2591 | 2012-11-09 11:32:52 -0300 | [diff] [blame] | 296 | struct machine *pos = rb_entry(nd, struct machine, rb_node); |
| 297 | process(pos, data); |
| 298 | } |
| 299 | } |
| 300 | |
| 301 | char *machine__mmap_name(struct machine *machine, char *bf, size_t size) |
| 302 | { |
| 303 | if (machine__is_host(machine)) |
| 304 | snprintf(bf, size, "[%s]", "kernel.kallsyms"); |
| 305 | else if (machine__is_default_guest(machine)) |
| 306 | snprintf(bf, size, "[%s]", "guest.kernel.kallsyms"); |
| 307 | else { |
| 308 | snprintf(bf, size, "[%s.%d]", "guest.kernel.kallsyms", |
| 309 | machine->pid); |
| 310 | } |
| 311 | |
| 312 | return bf; |
| 313 | } |
| 314 | |
Arnaldo Carvalho de Melo | 876650e | 2012-12-18 19:15:48 -0300 | [diff] [blame] | 315 | 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] | 316 | { |
| 317 | struct rb_node *node; |
| 318 | struct machine *machine; |
| 319 | |
Arnaldo Carvalho de Melo | 876650e | 2012-12-18 19:15:48 -0300 | [diff] [blame] | 320 | machines->host.id_hdr_size = id_hdr_size; |
| 321 | |
| 322 | for (node = rb_first(&machines->guests); node; node = rb_next(node)) { |
Arnaldo Carvalho de Melo | 69d2591 | 2012-11-09 11:32:52 -0300 | [diff] [blame] | 323 | machine = rb_entry(node, struct machine, rb_node); |
| 324 | machine->id_hdr_size = id_hdr_size; |
| 325 | } |
| 326 | |
| 327 | return; |
| 328 | } |
| 329 | |
Adrian Hunter | 29ce361 | 2014-07-16 11:07:13 +0300 | [diff] [blame] | 330 | static void machine__update_thread_pid(struct machine *machine, |
| 331 | struct thread *th, pid_t pid) |
| 332 | { |
| 333 | struct thread *leader; |
| 334 | |
| 335 | if (pid == th->pid_ || pid == -1 || th->pid_ != -1) |
| 336 | return; |
| 337 | |
| 338 | th->pid_ = pid; |
| 339 | |
| 340 | if (th->pid_ == th->tid) |
| 341 | return; |
| 342 | |
Arnaldo Carvalho de Melo | b91fc39 | 2015-04-06 20:43:22 -0300 | [diff] [blame] | 343 | leader = __machine__findnew_thread(machine, th->pid_, th->pid_); |
Adrian Hunter | 29ce361 | 2014-07-16 11:07:13 +0300 | [diff] [blame] | 344 | if (!leader) |
| 345 | goto out_err; |
| 346 | |
| 347 | if (!leader->mg) |
Arnaldo Carvalho de Melo | 11246c7 | 2014-10-21 17:29:02 -0300 | [diff] [blame] | 348 | leader->mg = map_groups__new(machine); |
Adrian Hunter | 29ce361 | 2014-07-16 11:07:13 +0300 | [diff] [blame] | 349 | |
| 350 | if (!leader->mg) |
| 351 | goto out_err; |
| 352 | |
| 353 | if (th->mg == leader->mg) |
| 354 | return; |
| 355 | |
| 356 | if (th->mg) { |
| 357 | /* |
| 358 | * Maps are created from MMAP events which provide the pid and |
| 359 | * tid. Consequently there never should be any maps on a thread |
| 360 | * with an unknown pid. Just print an error if there are. |
| 361 | */ |
| 362 | if (!map_groups__empty(th->mg)) |
| 363 | pr_err("Discarding thread maps for %d:%d\n", |
| 364 | th->pid_, th->tid); |
Arnaldo Carvalho de Melo | 8e160b2 | 2015-05-19 20:07:14 -0300 | [diff] [blame] | 365 | map_groups__put(th->mg); |
Adrian Hunter | 29ce361 | 2014-07-16 11:07:13 +0300 | [diff] [blame] | 366 | } |
| 367 | |
| 368 | th->mg = map_groups__get(leader->mg); |
Arnaldo Carvalho de Melo | abd8286 | 2015-12-11 19:11:23 -0300 | [diff] [blame] | 369 | out_put: |
| 370 | thread__put(leader); |
Adrian Hunter | 29ce361 | 2014-07-16 11:07:13 +0300 | [diff] [blame] | 371 | return; |
Adrian Hunter | 29ce361 | 2014-07-16 11:07:13 +0300 | [diff] [blame] | 372 | out_err: |
| 373 | 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] | 374 | goto out_put; |
Adrian Hunter | 29ce361 | 2014-07-16 11:07:13 +0300 | [diff] [blame] | 375 | } |
| 376 | |
Arnaldo Carvalho de Melo | abd8286 | 2015-12-11 19:11:23 -0300 | [diff] [blame] | 377 | /* |
Adam Buchbinder | bd1a0be | 2016-02-24 10:02:25 -0800 | [diff] [blame] | 378 | * Caller must eventually drop thread->refcnt returned with a successful |
Arnaldo Carvalho de Melo | abd8286 | 2015-12-11 19:11:23 -0300 | [diff] [blame] | 379 | * lookup/new thread inserted. |
| 380 | */ |
Arnaldo Carvalho de Melo | b91fc39 | 2015-04-06 20:43:22 -0300 | [diff] [blame] | 381 | static struct thread *____machine__findnew_thread(struct machine *machine, |
| 382 | pid_t pid, pid_t tid, |
| 383 | bool create) |
Arnaldo Carvalho de Melo | 9d2f8e2 | 2012-10-06 15:43:20 -0300 | [diff] [blame] | 384 | { |
| 385 | struct rb_node **p = &machine->threads.rb_node; |
| 386 | struct rb_node *parent = NULL; |
| 387 | struct thread *th; |
| 388 | |
| 389 | /* |
Adrian Hunter | 3805123 | 2013-07-04 16:20:31 +0300 | [diff] [blame] | 390 | * Front-end cache - TID lookups come in blocks, |
Arnaldo Carvalho de Melo | 9d2f8e2 | 2012-10-06 15:43:20 -0300 | [diff] [blame] | 391 | * so most of the time we dont have to look up |
| 392 | * the full rbtree: |
| 393 | */ |
Adrian Hunter | 29ce361 | 2014-07-16 11:07:13 +0300 | [diff] [blame] | 394 | th = machine->last_match; |
Arnaldo Carvalho de Melo | f3b623b | 2015-03-02 22:21:35 -0300 | [diff] [blame] | 395 | if (th != NULL) { |
| 396 | if (th->tid == tid) { |
| 397 | machine__update_thread_pid(machine, th, pid); |
Arnaldo Carvalho de Melo | abd8286 | 2015-12-11 19:11:23 -0300 | [diff] [blame] | 398 | return thread__get(th); |
Arnaldo Carvalho de Melo | f3b623b | 2015-03-02 22:21:35 -0300 | [diff] [blame] | 399 | } |
| 400 | |
Arnaldo Carvalho de Melo | 0ceb8f6 | 2015-05-11 18:08:12 -0300 | [diff] [blame] | 401 | machine->last_match = NULL; |
Adrian Hunter | 99d725f | 2013-08-26 16:00:19 +0300 | [diff] [blame] | 402 | } |
Arnaldo Carvalho de Melo | 9d2f8e2 | 2012-10-06 15:43:20 -0300 | [diff] [blame] | 403 | |
| 404 | while (*p != NULL) { |
| 405 | parent = *p; |
| 406 | th = rb_entry(parent, struct thread, rb_node); |
| 407 | |
Adrian Hunter | 3805123 | 2013-07-04 16:20:31 +0300 | [diff] [blame] | 408 | if (th->tid == tid) { |
Arnaldo Carvalho de Melo | 0ceb8f6 | 2015-05-11 18:08:12 -0300 | [diff] [blame] | 409 | machine->last_match = th; |
Adrian Hunter | 29ce361 | 2014-07-16 11:07:13 +0300 | [diff] [blame] | 410 | machine__update_thread_pid(machine, th, pid); |
Arnaldo Carvalho de Melo | abd8286 | 2015-12-11 19:11:23 -0300 | [diff] [blame] | 411 | return thread__get(th); |
Arnaldo Carvalho de Melo | 9d2f8e2 | 2012-10-06 15:43:20 -0300 | [diff] [blame] | 412 | } |
| 413 | |
Adrian Hunter | 3805123 | 2013-07-04 16:20:31 +0300 | [diff] [blame] | 414 | if (tid < th->tid) |
Arnaldo Carvalho de Melo | 9d2f8e2 | 2012-10-06 15:43:20 -0300 | [diff] [blame] | 415 | p = &(*p)->rb_left; |
| 416 | else |
| 417 | p = &(*p)->rb_right; |
| 418 | } |
| 419 | |
| 420 | if (!create) |
| 421 | return NULL; |
| 422 | |
Adrian Hunter | 99d725f | 2013-08-26 16:00:19 +0300 | [diff] [blame] | 423 | th = thread__new(pid, tid); |
Arnaldo Carvalho de Melo | 9d2f8e2 | 2012-10-06 15:43:20 -0300 | [diff] [blame] | 424 | if (th != NULL) { |
| 425 | rb_link_node(&th->rb_node, parent, p); |
| 426 | rb_insert_color(&th->rb_node, &machine->threads); |
Jiri Olsa | cddcef6 | 2014-04-09 20:54:29 +0200 | [diff] [blame] | 427 | |
| 428 | /* |
| 429 | * We have to initialize map_groups separately |
| 430 | * after rb tree is updated. |
| 431 | * |
| 432 | * The reason is that we call machine__findnew_thread |
| 433 | * within thread__init_map_groups to find the thread |
| 434 | * leader and that would screwed the rb tree. |
| 435 | */ |
Adrian Hunter | 418029b | 2014-07-16 10:19:44 +0300 | [diff] [blame] | 436 | if (thread__init_map_groups(th, machine)) { |
Arnaldo Carvalho de Melo | 0170b14 | 2015-05-25 15:23:05 -0300 | [diff] [blame] | 437 | rb_erase_init(&th->rb_node, &machine->threads); |
Arnaldo Carvalho de Melo | b91fc39 | 2015-04-06 20:43:22 -0300 | [diff] [blame] | 438 | RB_CLEAR_NODE(&th->rb_node); |
Arnaldo Carvalho de Melo | abd8286 | 2015-12-11 19:11:23 -0300 | [diff] [blame] | 439 | thread__put(th); |
Jiri Olsa | cddcef6 | 2014-04-09 20:54:29 +0200 | [diff] [blame] | 440 | return NULL; |
Adrian Hunter | 418029b | 2014-07-16 10:19:44 +0300 | [diff] [blame] | 441 | } |
Arnaldo Carvalho de Melo | f3b623b | 2015-03-02 22:21:35 -0300 | [diff] [blame] | 442 | /* |
| 443 | * It is now in the rbtree, get a ref |
| 444 | */ |
| 445 | thread__get(th); |
Arnaldo Carvalho de Melo | 0ceb8f6 | 2015-05-11 18:08:12 -0300 | [diff] [blame] | 446 | machine->last_match = th; |
Arnaldo Carvalho de Melo | d2c1103 | 2016-05-04 10:09:33 -0300 | [diff] [blame] | 447 | ++machine->nr_threads; |
Arnaldo Carvalho de Melo | 9d2f8e2 | 2012-10-06 15:43:20 -0300 | [diff] [blame] | 448 | } |
| 449 | |
| 450 | return th; |
| 451 | } |
| 452 | |
Arnaldo Carvalho de Melo | b91fc39 | 2015-04-06 20:43:22 -0300 | [diff] [blame] | 453 | struct thread *__machine__findnew_thread(struct machine *machine, pid_t pid, pid_t tid) |
| 454 | { |
| 455 | return ____machine__findnew_thread(machine, pid, tid, true); |
| 456 | } |
| 457 | |
Adrian Hunter | 314add6 | 2013-08-27 11:23:03 +0300 | [diff] [blame] | 458 | struct thread *machine__findnew_thread(struct machine *machine, pid_t pid, |
| 459 | pid_t tid) |
Arnaldo Carvalho de Melo | 9d2f8e2 | 2012-10-06 15:43:20 -0300 | [diff] [blame] | 460 | { |
Arnaldo Carvalho de Melo | b91fc39 | 2015-04-06 20:43:22 -0300 | [diff] [blame] | 461 | struct thread *th; |
| 462 | |
| 463 | pthread_rwlock_wrlock(&machine->threads_lock); |
Arnaldo Carvalho de Melo | abd8286 | 2015-12-11 19:11:23 -0300 | [diff] [blame] | 464 | th = __machine__findnew_thread(machine, pid, tid); |
Arnaldo Carvalho de Melo | b91fc39 | 2015-04-06 20:43:22 -0300 | [diff] [blame] | 465 | pthread_rwlock_unlock(&machine->threads_lock); |
| 466 | return th; |
Arnaldo Carvalho de Melo | 9d2f8e2 | 2012-10-06 15:43:20 -0300 | [diff] [blame] | 467 | } |
| 468 | |
Jiri Olsa | d75e609 | 2014-03-14 15:00:03 +0100 | [diff] [blame] | 469 | struct thread *machine__find_thread(struct machine *machine, pid_t pid, |
| 470 | pid_t tid) |
Arnaldo Carvalho de Melo | 9d2f8e2 | 2012-10-06 15:43:20 -0300 | [diff] [blame] | 471 | { |
Arnaldo Carvalho de Melo | b91fc39 | 2015-04-06 20:43:22 -0300 | [diff] [blame] | 472 | struct thread *th; |
| 473 | pthread_rwlock_rdlock(&machine->threads_lock); |
Arnaldo Carvalho de Melo | abd8286 | 2015-12-11 19:11:23 -0300 | [diff] [blame] | 474 | th = ____machine__findnew_thread(machine, pid, tid, false); |
Arnaldo Carvalho de Melo | b91fc39 | 2015-04-06 20:43:22 -0300 | [diff] [blame] | 475 | pthread_rwlock_unlock(&machine->threads_lock); |
| 476 | return th; |
Arnaldo Carvalho de Melo | 9d2f8e2 | 2012-10-06 15:43:20 -0300 | [diff] [blame] | 477 | } |
Arnaldo Carvalho de Melo | b0a7d1a | 2012-10-06 16:26:02 -0300 | [diff] [blame] | 478 | |
Adrian Hunter | cfe1c41 | 2014-07-31 09:00:45 +0300 | [diff] [blame] | 479 | struct comm *machine__thread_exec_comm(struct machine *machine, |
| 480 | struct thread *thread) |
| 481 | { |
| 482 | if (machine->comm_exec) |
| 483 | return thread__exec_comm(thread); |
| 484 | else |
| 485 | return thread__comm(thread); |
| 486 | } |
| 487 | |
Frederic Weisbecker | 162f0be | 2013-09-11 16:18:24 +0200 | [diff] [blame] | 488 | int machine__process_comm_event(struct machine *machine, union perf_event *event, |
| 489 | struct perf_sample *sample) |
Arnaldo Carvalho de Melo | b0a7d1a | 2012-10-06 16:26:02 -0300 | [diff] [blame] | 490 | { |
Adrian Hunter | 314add6 | 2013-08-27 11:23:03 +0300 | [diff] [blame] | 491 | struct thread *thread = machine__findnew_thread(machine, |
| 492 | event->comm.pid, |
| 493 | event->comm.tid); |
Adrian Hunter | 65de51f | 2014-07-31 09:00:44 +0300 | [diff] [blame] | 494 | bool exec = event->header.misc & PERF_RECORD_MISC_COMM_EXEC; |
Arnaldo Carvalho de Melo | b91fc39 | 2015-04-06 20:43:22 -0300 | [diff] [blame] | 495 | int err = 0; |
Arnaldo Carvalho de Melo | b0a7d1a | 2012-10-06 16:26:02 -0300 | [diff] [blame] | 496 | |
Adrian Hunter | cfe1c41 | 2014-07-31 09:00:45 +0300 | [diff] [blame] | 497 | if (exec) |
| 498 | machine->comm_exec = true; |
| 499 | |
Arnaldo Carvalho de Melo | b0a7d1a | 2012-10-06 16:26:02 -0300 | [diff] [blame] | 500 | if (dump_trace) |
| 501 | perf_event__fprintf_comm(event, stdout); |
| 502 | |
Adrian Hunter | 65de51f | 2014-07-31 09:00:44 +0300 | [diff] [blame] | 503 | if (thread == NULL || |
| 504 | __thread__set_comm(thread, event->comm.comm, sample->time, exec)) { |
Arnaldo Carvalho de Melo | b0a7d1a | 2012-10-06 16:26:02 -0300 | [diff] [blame] | 505 | dump_printf("problem processing PERF_RECORD_COMM, skipping event.\n"); |
Arnaldo Carvalho de Melo | b91fc39 | 2015-04-06 20:43:22 -0300 | [diff] [blame] | 506 | err = -1; |
Arnaldo Carvalho de Melo | b0a7d1a | 2012-10-06 16:26:02 -0300 | [diff] [blame] | 507 | } |
| 508 | |
Arnaldo Carvalho de Melo | b91fc39 | 2015-04-06 20:43:22 -0300 | [diff] [blame] | 509 | thread__put(thread); |
| 510 | |
| 511 | return err; |
Arnaldo Carvalho de Melo | b0a7d1a | 2012-10-06 16:26:02 -0300 | [diff] [blame] | 512 | } |
| 513 | |
Hari Bathini | f3b3614 | 2017-03-08 02:11:43 +0530 | [diff] [blame] | 514 | int machine__process_namespaces_event(struct machine *machine __maybe_unused, |
| 515 | union perf_event *event, |
| 516 | struct perf_sample *sample __maybe_unused) |
| 517 | { |
| 518 | struct thread *thread = machine__findnew_thread(machine, |
| 519 | event->namespaces.pid, |
| 520 | event->namespaces.tid); |
| 521 | int err = 0; |
| 522 | |
| 523 | WARN_ONCE(event->namespaces.nr_namespaces > NR_NAMESPACES, |
| 524 | "\nWARNING: kernel seems to support more namespaces than perf" |
| 525 | " tool.\nTry updating the perf tool..\n\n"); |
| 526 | |
| 527 | WARN_ONCE(event->namespaces.nr_namespaces < NR_NAMESPACES, |
| 528 | "\nWARNING: perf tool seems to support more namespaces than" |
| 529 | " the kernel.\nTry updating the kernel..\n\n"); |
| 530 | |
| 531 | if (dump_trace) |
| 532 | perf_event__fprintf_namespaces(event, stdout); |
| 533 | |
| 534 | if (thread == NULL || |
| 535 | thread__set_namespaces(thread, sample->time, &event->namespaces)) { |
| 536 | dump_printf("problem processing PERF_RECORD_NAMESPACES, skipping event.\n"); |
| 537 | err = -1; |
| 538 | } |
| 539 | |
| 540 | thread__put(thread); |
| 541 | |
| 542 | return err; |
| 543 | } |
| 544 | |
Arnaldo Carvalho de Melo | b0a7d1a | 2012-10-06 16:26:02 -0300 | [diff] [blame] | 545 | int machine__process_lost_event(struct machine *machine __maybe_unused, |
Frederic Weisbecker | 162f0be | 2013-09-11 16:18:24 +0200 | [diff] [blame] | 546 | union perf_event *event, struct perf_sample *sample __maybe_unused) |
Arnaldo Carvalho de Melo | b0a7d1a | 2012-10-06 16:26:02 -0300 | [diff] [blame] | 547 | { |
| 548 | dump_printf(": id:%" PRIu64 ": lost:%" PRIu64 "\n", |
| 549 | event->lost.id, event->lost.lost); |
| 550 | return 0; |
| 551 | } |
| 552 | |
Kan Liang | c4937a9 | 2015-05-10 15:13:15 -0400 | [diff] [blame] | 553 | int machine__process_lost_samples_event(struct machine *machine __maybe_unused, |
| 554 | union perf_event *event, struct perf_sample *sample) |
| 555 | { |
| 556 | dump_printf(": id:%" PRIu64 ": lost samples :%" PRIu64 "\n", |
| 557 | sample->id, event->lost_samples.lost); |
| 558 | return 0; |
| 559 | } |
| 560 | |
Arnaldo Carvalho de Melo | 9f2de31 | 2015-06-01 12:01:02 -0300 | [diff] [blame] | 561 | static struct dso *machine__findnew_module_dso(struct machine *machine, |
| 562 | struct kmod_path *m, |
| 563 | const char *filename) |
Jiri Olsa | da17ea3 | 2015-02-12 22:10:52 +0100 | [diff] [blame] | 564 | { |
| 565 | struct dso *dso; |
Jiri Olsa | da17ea3 | 2015-02-12 22:10:52 +0100 | [diff] [blame] | 566 | |
Arnaldo Carvalho de Melo | e880784 | 2015-06-01 15:40:01 -0300 | [diff] [blame] | 567 | pthread_rwlock_wrlock(&machine->dsos.lock); |
| 568 | |
| 569 | dso = __dsos__find(&machine->dsos, m->name, true); |
Jiri Olsa | da17ea3 | 2015-02-12 22:10:52 +0100 | [diff] [blame] | 570 | if (!dso) { |
Arnaldo Carvalho de Melo | e880784 | 2015-06-01 15:40:01 -0300 | [diff] [blame] | 571 | dso = __dsos__addnew(&machine->dsos, m->name); |
Jiri Olsa | da17ea3 | 2015-02-12 22:10:52 +0100 | [diff] [blame] | 572 | if (dso == NULL) |
Arnaldo Carvalho de Melo | e880784 | 2015-06-01 15:40:01 -0300 | [diff] [blame] | 573 | goto out_unlock; |
Jiri Olsa | da17ea3 | 2015-02-12 22:10:52 +0100 | [diff] [blame] | 574 | |
| 575 | if (machine__is_host(machine)) |
| 576 | dso->symtab_type = DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE; |
| 577 | else |
| 578 | dso->symtab_type = DSO_BINARY_TYPE__GUEST_KMODULE; |
| 579 | |
| 580 | /* _KMODULE_COMP should be next to _KMODULE */ |
Jiri Olsa | ca33380 | 2015-02-17 17:29:57 +0100 | [diff] [blame] | 581 | if (m->kmod && m->comp) |
Jiri Olsa | da17ea3 | 2015-02-12 22:10:52 +0100 | [diff] [blame] | 582 | dso->symtab_type++; |
Jiri Olsa | ca33380 | 2015-02-17 17:29:57 +0100 | [diff] [blame] | 583 | |
| 584 | dso__set_short_name(dso, strdup(m->name), true); |
| 585 | dso__set_long_name(dso, strdup(filename), true); |
Jiri Olsa | da17ea3 | 2015-02-12 22:10:52 +0100 | [diff] [blame] | 586 | } |
| 587 | |
Arnaldo Carvalho de Melo | d3a7c48 | 2015-06-02 11:53:26 -0300 | [diff] [blame] | 588 | dso__get(dso); |
Arnaldo Carvalho de Melo | e880784 | 2015-06-01 15:40:01 -0300 | [diff] [blame] | 589 | out_unlock: |
| 590 | pthread_rwlock_unlock(&machine->dsos.lock); |
Jiri Olsa | da17ea3 | 2015-02-12 22:10:52 +0100 | [diff] [blame] | 591 | return dso; |
| 592 | } |
| 593 | |
Adrian Hunter | 4a96f7a | 2015-04-30 17:37:29 +0300 | [diff] [blame] | 594 | int machine__process_aux_event(struct machine *machine __maybe_unused, |
| 595 | union perf_event *event) |
| 596 | { |
| 597 | if (dump_trace) |
| 598 | perf_event__fprintf_aux(event, stdout); |
| 599 | return 0; |
| 600 | } |
| 601 | |
Adrian Hunter | 0ad21f6 | 2015-04-30 17:37:30 +0300 | [diff] [blame] | 602 | int machine__process_itrace_start_event(struct machine *machine __maybe_unused, |
| 603 | union perf_event *event) |
| 604 | { |
| 605 | if (dump_trace) |
| 606 | perf_event__fprintf_itrace_start(event, stdout); |
| 607 | return 0; |
| 608 | } |
| 609 | |
Adrian Hunter | 0286039 | 2015-07-21 12:44:03 +0300 | [diff] [blame] | 610 | int machine__process_switch_event(struct machine *machine __maybe_unused, |
| 611 | union perf_event *event) |
| 612 | { |
| 613 | if (dump_trace) |
| 614 | perf_event__fprintf_switch(event, stdout); |
| 615 | return 0; |
| 616 | } |
| 617 | |
Wang Nan | c03d518 | 2015-11-26 03:59:57 +0000 | [diff] [blame] | 618 | static void dso__adjust_kmod_long_name(struct dso *dso, const char *filename) |
| 619 | { |
| 620 | const char *dup_filename; |
| 621 | |
| 622 | if (!filename || !dso || !dso->long_name) |
| 623 | return; |
| 624 | if (dso->long_name[0] != '[') |
| 625 | return; |
| 626 | if (!strchr(filename, '/')) |
| 627 | return; |
| 628 | |
| 629 | dup_filename = strdup(filename); |
| 630 | if (!dup_filename) |
| 631 | return; |
| 632 | |
Wang Nan | 5dcf16d | 2015-12-07 02:36:25 +0000 | [diff] [blame] | 633 | dso__set_long_name(dso, dup_filename, true); |
Wang Nan | c03d518 | 2015-11-26 03:59:57 +0000 | [diff] [blame] | 634 | } |
| 635 | |
Arnaldo Carvalho de Melo | 9f2de31 | 2015-06-01 12:01:02 -0300 | [diff] [blame] | 636 | struct map *machine__findnew_module_map(struct machine *machine, u64 start, |
| 637 | const char *filename) |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 638 | { |
Jiri Olsa | ca33380 | 2015-02-17 17:29:57 +0100 | [diff] [blame] | 639 | struct map *map = NULL; |
Masami Hiramatsu | 566c69c | 2015-11-18 15:40:35 +0900 | [diff] [blame] | 640 | struct dso *dso = NULL; |
Jiri Olsa | ca33380 | 2015-02-17 17:29:57 +0100 | [diff] [blame] | 641 | struct kmod_path m; |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 642 | |
Jiri Olsa | ca33380 | 2015-02-17 17:29:57 +0100 | [diff] [blame] | 643 | if (kmod_path__parse_name(&m, filename)) |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 644 | return NULL; |
| 645 | |
Jiri Olsa | bc84f46 | 2015-02-17 17:31:18 +0100 | [diff] [blame] | 646 | map = map_groups__find_by_name(&machine->kmaps, MAP__FUNCTION, |
| 647 | m.name); |
Wang Nan | c03d518 | 2015-11-26 03:59:57 +0000 | [diff] [blame] | 648 | if (map) { |
| 649 | /* |
| 650 | * If the map's dso is an offline module, give dso__load() |
| 651 | * a chance to find the file path of that module by fixing |
| 652 | * long_name. |
| 653 | */ |
| 654 | dso__adjust_kmod_long_name(map->dso, filename); |
Jiri Olsa | bc84f46 | 2015-02-17 17:31:18 +0100 | [diff] [blame] | 655 | goto out; |
Wang Nan | c03d518 | 2015-11-26 03:59:57 +0000 | [diff] [blame] | 656 | } |
Jiri Olsa | bc84f46 | 2015-02-17 17:31:18 +0100 | [diff] [blame] | 657 | |
Arnaldo Carvalho de Melo | 9f2de31 | 2015-06-01 12:01:02 -0300 | [diff] [blame] | 658 | dso = machine__findnew_module_dso(machine, &m, filename); |
Jiri Olsa | ca33380 | 2015-02-17 17:29:57 +0100 | [diff] [blame] | 659 | if (dso == NULL) |
| 660 | goto out; |
| 661 | |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 662 | map = map__new2(start, dso, MAP__FUNCTION); |
| 663 | if (map == NULL) |
Jiri Olsa | ca33380 | 2015-02-17 17:29:57 +0100 | [diff] [blame] | 664 | goto out; |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 665 | |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 666 | map_groups__insert(&machine->kmaps, map); |
Jiri Olsa | ca33380 | 2015-02-17 17:29:57 +0100 | [diff] [blame] | 667 | |
Masami Hiramatsu | 9afcb420 | 2015-11-18 15:40:20 +0900 | [diff] [blame] | 668 | /* Put the map here because map_groups__insert alread got it */ |
| 669 | map__put(map); |
Jiri Olsa | ca33380 | 2015-02-17 17:29:57 +0100 | [diff] [blame] | 670 | out: |
Masami Hiramatsu | 566c69c | 2015-11-18 15:40:35 +0900 | [diff] [blame] | 671 | /* put the dso here, corresponding to machine__findnew_module_dso */ |
| 672 | dso__put(dso); |
Jiri Olsa | ca33380 | 2015-02-17 17:29:57 +0100 | [diff] [blame] | 673 | free(m.name); |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 674 | return map; |
| 675 | } |
| 676 | |
Arnaldo Carvalho de Melo | 876650e | 2012-12-18 19:15:48 -0300 | [diff] [blame] | 677 | size_t machines__fprintf_dsos(struct machines *machines, FILE *fp) |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 678 | { |
| 679 | struct rb_node *nd; |
Arnaldo Carvalho de Melo | 3d39ac5 | 2015-05-28 13:06:42 -0300 | [diff] [blame] | 680 | size_t ret = __dsos__fprintf(&machines->host.dsos.head, fp); |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 681 | |
Arnaldo Carvalho de Melo | 876650e | 2012-12-18 19:15:48 -0300 | [diff] [blame] | 682 | for (nd = rb_first(&machines->guests); nd; nd = rb_next(nd)) { |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 683 | struct machine *pos = rb_entry(nd, struct machine, rb_node); |
Arnaldo Carvalho de Melo | 3d39ac5 | 2015-05-28 13:06:42 -0300 | [diff] [blame] | 684 | ret += __dsos__fprintf(&pos->dsos.head, fp); |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 685 | } |
| 686 | |
| 687 | return ret; |
| 688 | } |
| 689 | |
Waiman Long | 8fa7d87 | 2014-09-29 16:07:28 -0400 | [diff] [blame] | 690 | 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] | 691 | bool (skip)(struct dso *dso, int parm), int parm) |
| 692 | { |
Arnaldo Carvalho de Melo | 3d39ac5 | 2015-05-28 13:06:42 -0300 | [diff] [blame] | 693 | return __dsos__fprintf_buildid(&m->dsos.head, fp, skip, parm); |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 694 | } |
| 695 | |
Arnaldo Carvalho de Melo | 876650e | 2012-12-18 19:15:48 -0300 | [diff] [blame] | 696 | 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] | 697 | bool (skip)(struct dso *dso, int parm), int parm) |
| 698 | { |
| 699 | struct rb_node *nd; |
Arnaldo Carvalho de Melo | 876650e | 2012-12-18 19:15:48 -0300 | [diff] [blame] | 700 | 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] | 701 | |
Arnaldo Carvalho de Melo | 876650e | 2012-12-18 19:15:48 -0300 | [diff] [blame] | 702 | for (nd = rb_first(&machines->guests); nd; nd = rb_next(nd)) { |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 703 | struct machine *pos = rb_entry(nd, struct machine, rb_node); |
| 704 | ret += machine__fprintf_dsos_buildid(pos, fp, skip, parm); |
| 705 | } |
| 706 | return ret; |
| 707 | } |
| 708 | |
| 709 | size_t machine__fprintf_vmlinux_path(struct machine *machine, FILE *fp) |
| 710 | { |
| 711 | int i; |
| 712 | size_t printed = 0; |
Arnaldo Carvalho de Melo | a5e813c | 2015-09-30 11:54:04 -0300 | [diff] [blame] | 713 | struct dso *kdso = machine__kernel_map(machine)->dso; |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 714 | |
| 715 | if (kdso->has_build_id) { |
| 716 | char filename[PATH_MAX]; |
| 717 | if (dso__build_id_filename(kdso, filename, sizeof(filename))) |
| 718 | printed += fprintf(fp, "[0] %s\n", filename); |
| 719 | } |
| 720 | |
| 721 | for (i = 0; i < vmlinux_path__nr_entries; ++i) |
| 722 | printed += fprintf(fp, "[%d] %s\n", |
| 723 | i + kdso->has_build_id, vmlinux_path[i]); |
| 724 | |
| 725 | return printed; |
| 726 | } |
| 727 | |
| 728 | size_t machine__fprintf(struct machine *machine, FILE *fp) |
| 729 | { |
Arnaldo Carvalho de Melo | d2c1103 | 2016-05-04 10:09:33 -0300 | [diff] [blame] | 730 | size_t ret; |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 731 | struct rb_node *nd; |
| 732 | |
Arnaldo Carvalho de Melo | b91fc39 | 2015-04-06 20:43:22 -0300 | [diff] [blame] | 733 | pthread_rwlock_rdlock(&machine->threads_lock); |
| 734 | |
Arnaldo Carvalho de Melo | d2c1103 | 2016-05-04 10:09:33 -0300 | [diff] [blame] | 735 | ret = fprintf(fp, "Threads: %u\n", machine->nr_threads); |
| 736 | |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 737 | for (nd = rb_first(&machine->threads); nd; nd = rb_next(nd)) { |
| 738 | struct thread *pos = rb_entry(nd, struct thread, rb_node); |
| 739 | |
| 740 | ret += thread__fprintf(pos, fp); |
| 741 | } |
| 742 | |
Arnaldo Carvalho de Melo | b91fc39 | 2015-04-06 20:43:22 -0300 | [diff] [blame] | 743 | pthread_rwlock_unlock(&machine->threads_lock); |
| 744 | |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 745 | return ret; |
| 746 | } |
| 747 | |
| 748 | static struct dso *machine__get_kernel(struct machine *machine) |
| 749 | { |
| 750 | const char *vmlinux_name = NULL; |
| 751 | struct dso *kernel; |
| 752 | |
| 753 | if (machine__is_host(machine)) { |
| 754 | vmlinux_name = symbol_conf.vmlinux_name; |
| 755 | if (!vmlinux_name) |
Masami Hiramatsu | 0a77582 | 2016-05-15 12:19:40 +0900 | [diff] [blame] | 756 | vmlinux_name = DSO__NAME_KALLSYMS; |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 757 | |
Arnaldo Carvalho de Melo | 459ce51 | 2015-05-28 12:40:55 -0300 | [diff] [blame] | 758 | kernel = machine__findnew_kernel(machine, vmlinux_name, |
| 759 | "[kernel]", DSO_TYPE_KERNEL); |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 760 | } else { |
| 761 | char bf[PATH_MAX]; |
| 762 | |
| 763 | if (machine__is_default_guest(machine)) |
| 764 | vmlinux_name = symbol_conf.default_guest_vmlinux_name; |
| 765 | if (!vmlinux_name) |
| 766 | vmlinux_name = machine__mmap_name(machine, bf, |
| 767 | sizeof(bf)); |
| 768 | |
Arnaldo Carvalho de Melo | 459ce51 | 2015-05-28 12:40:55 -0300 | [diff] [blame] | 769 | kernel = machine__findnew_kernel(machine, vmlinux_name, |
| 770 | "[guest.kernel]", |
| 771 | DSO_TYPE_GUEST_KERNEL); |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 772 | } |
| 773 | |
| 774 | if (kernel != NULL && (!kernel->has_build_id)) |
| 775 | dso__read_running_kernel_build_id(kernel, machine); |
| 776 | |
| 777 | return kernel; |
| 778 | } |
| 779 | |
| 780 | struct process_args { |
| 781 | u64 start; |
| 782 | }; |
| 783 | |
Adrian Hunter | 15a0a87 | 2014-01-29 16:14:38 +0200 | [diff] [blame] | 784 | static void machine__get_kallsyms_filename(struct machine *machine, char *buf, |
| 785 | size_t bufsz) |
| 786 | { |
| 787 | if (machine__is_default_guest(machine)) |
| 788 | scnprintf(buf, bufsz, "%s", symbol_conf.default_guest_kallsyms); |
| 789 | else |
| 790 | scnprintf(buf, bufsz, "%s/proc/kallsyms", machine->root_dir); |
| 791 | } |
| 792 | |
Simon Que | a93f0e5 | 2014-06-16 11:32:09 -0700 | [diff] [blame] | 793 | const char *ref_reloc_sym_names[] = {"_text", "_stext", NULL}; |
| 794 | |
| 795 | /* Figure out the start address of kernel map from /proc/kallsyms. |
| 796 | * Returns the name of the start symbol in *symbol_name. Pass in NULL as |
| 797 | * symbol_name if it's not that important. |
| 798 | */ |
Arnaldo Carvalho de Melo | b843f62 | 2017-04-27 21:21:09 -0300 | [diff] [blame] | 799 | static int machine__get_running_kernel_start(struct machine *machine, |
| 800 | const char **symbol_name, u64 *start) |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 801 | { |
Adrian Hunter | 15a0a87 | 2014-01-29 16:14:38 +0200 | [diff] [blame] | 802 | char filename[PATH_MAX]; |
Arnaldo Carvalho de Melo | b843f62 | 2017-04-27 21:21:09 -0300 | [diff] [blame] | 803 | int i, err = -1; |
Simon Que | a93f0e5 | 2014-06-16 11:32:09 -0700 | [diff] [blame] | 804 | const char *name; |
| 805 | u64 addr = 0; |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 806 | |
Adrian Hunter | 15a0a87 | 2014-01-29 16:14:38 +0200 | [diff] [blame] | 807 | machine__get_kallsyms_filename(machine, filename, PATH_MAX); |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 808 | |
| 809 | if (symbol__restricted_filename(filename, "/proc/kallsyms")) |
| 810 | return 0; |
| 811 | |
Simon Que | a93f0e5 | 2014-06-16 11:32:09 -0700 | [diff] [blame] | 812 | 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] | 813 | err = kallsyms__get_function_start(filename, name, &addr); |
| 814 | if (!err) |
Simon Que | a93f0e5 | 2014-06-16 11:32:09 -0700 | [diff] [blame] | 815 | break; |
| 816 | } |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 817 | |
Arnaldo Carvalho de Melo | b843f62 | 2017-04-27 21:21:09 -0300 | [diff] [blame] | 818 | if (err) |
| 819 | return -1; |
| 820 | |
Simon Que | a93f0e5 | 2014-06-16 11:32:09 -0700 | [diff] [blame] | 821 | if (symbol_name) |
| 822 | *symbol_name = name; |
| 823 | |
Arnaldo Carvalho de Melo | b843f62 | 2017-04-27 21:21:09 -0300 | [diff] [blame] | 824 | *start = addr; |
| 825 | return 0; |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 826 | } |
| 827 | |
| 828 | int __machine__create_kernel_maps(struct machine *machine, struct dso *kernel) |
| 829 | { |
Arnaldo Carvalho de Melo | a0b2f5a | 2017-02-14 16:19:56 -0300 | [diff] [blame] | 830 | int type; |
Arnaldo Carvalho de Melo | b843f62 | 2017-04-27 21:21:09 -0300 | [diff] [blame] | 831 | u64 start = 0; |
| 832 | |
| 833 | if (machine__get_running_kernel_start(machine, NULL, &start)) |
| 834 | return -1; |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 835 | |
Masami Hiramatsu | cc1121a | 2015-12-09 11:11:33 +0900 | [diff] [blame] | 836 | /* In case of renewal the kernel map, destroy previous one */ |
| 837 | machine__destroy_kernel_maps(machine); |
| 838 | |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 839 | for (type = 0; type < MAP__NR_TYPES; ++type) { |
| 840 | struct kmap *kmap; |
Arnaldo Carvalho de Melo | 77e6597 | 2015-09-30 11:08:58 -0300 | [diff] [blame] | 841 | struct map *map; |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 842 | |
| 843 | machine->vmlinux_maps[type] = map__new2(start, kernel, type); |
| 844 | if (machine->vmlinux_maps[type] == NULL) |
| 845 | return -1; |
| 846 | |
| 847 | machine->vmlinux_maps[type]->map_ip = |
| 848 | machine->vmlinux_maps[type]->unmap_ip = |
| 849 | identity__map_ip; |
Arnaldo Carvalho de Melo | a5e813c | 2015-09-30 11:54:04 -0300 | [diff] [blame] | 850 | map = __machine__kernel_map(machine, type); |
Arnaldo Carvalho de Melo | 77e6597 | 2015-09-30 11:08:58 -0300 | [diff] [blame] | 851 | kmap = map__kmap(map); |
Wang Nan | ba92732 | 2015-04-07 08:22:45 +0000 | [diff] [blame] | 852 | if (!kmap) |
| 853 | return -1; |
| 854 | |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 855 | kmap->kmaps = &machine->kmaps; |
Arnaldo Carvalho de Melo | 77e6597 | 2015-09-30 11:08:58 -0300 | [diff] [blame] | 856 | map_groups__insert(&machine->kmaps, map); |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 857 | } |
| 858 | |
| 859 | return 0; |
| 860 | } |
| 861 | |
| 862 | void machine__destroy_kernel_maps(struct machine *machine) |
| 863 | { |
Arnaldo Carvalho de Melo | a0b2f5a | 2017-02-14 16:19:56 -0300 | [diff] [blame] | 864 | int type; |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 865 | |
| 866 | for (type = 0; type < MAP__NR_TYPES; ++type) { |
| 867 | struct kmap *kmap; |
Arnaldo Carvalho de Melo | a5e813c | 2015-09-30 11:54:04 -0300 | [diff] [blame] | 868 | struct map *map = __machine__kernel_map(machine, type); |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 869 | |
Arnaldo Carvalho de Melo | 77e6597 | 2015-09-30 11:08:58 -0300 | [diff] [blame] | 870 | if (map == NULL) |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 871 | continue; |
| 872 | |
Arnaldo Carvalho de Melo | 77e6597 | 2015-09-30 11:08:58 -0300 | [diff] [blame] | 873 | kmap = map__kmap(map); |
| 874 | map_groups__remove(&machine->kmaps, map); |
Wang Nan | ba92732 | 2015-04-07 08:22:45 +0000 | [diff] [blame] | 875 | if (kmap && kmap->ref_reloc_sym) { |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 876 | /* |
| 877 | * ref_reloc_sym is shared among all maps, so free just |
| 878 | * on one of them. |
| 879 | */ |
| 880 | if (type == MAP__FUNCTION) { |
Arnaldo Carvalho de Melo | 0466252 | 2013-12-26 17:41:15 -0300 | [diff] [blame] | 881 | zfree((char **)&kmap->ref_reloc_sym->name); |
| 882 | zfree(&kmap->ref_reloc_sym); |
| 883 | } else |
| 884 | kmap->ref_reloc_sym = NULL; |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 885 | } |
| 886 | |
Masami Hiramatsu | e96e407 | 2015-11-18 15:40:22 +0900 | [diff] [blame] | 887 | map__put(machine->vmlinux_maps[type]); |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 888 | machine->vmlinux_maps[type] = NULL; |
| 889 | } |
| 890 | } |
| 891 | |
Arnaldo Carvalho de Melo | 876650e | 2012-12-18 19:15:48 -0300 | [diff] [blame] | 892 | int machines__create_guest_kernel_maps(struct machines *machines) |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 893 | { |
| 894 | int ret = 0; |
| 895 | struct dirent **namelist = NULL; |
| 896 | int i, items = 0; |
| 897 | char path[PATH_MAX]; |
| 898 | pid_t pid; |
| 899 | char *endp; |
| 900 | |
| 901 | if (symbol_conf.default_guest_vmlinux_name || |
| 902 | symbol_conf.default_guest_modules || |
| 903 | symbol_conf.default_guest_kallsyms) { |
| 904 | machines__create_kernel_maps(machines, DEFAULT_GUEST_KERNEL_ID); |
| 905 | } |
| 906 | |
| 907 | if (symbol_conf.guestmount) { |
| 908 | items = scandir(symbol_conf.guestmount, &namelist, NULL, NULL); |
| 909 | if (items <= 0) |
| 910 | return -ENOENT; |
| 911 | for (i = 0; i < items; i++) { |
| 912 | if (!isdigit(namelist[i]->d_name[0])) { |
| 913 | /* Filter out . and .. */ |
| 914 | continue; |
| 915 | } |
| 916 | pid = (pid_t)strtol(namelist[i]->d_name, &endp, 10); |
| 917 | if ((*endp != '\0') || |
| 918 | (endp == namelist[i]->d_name) || |
| 919 | (errno == ERANGE)) { |
| 920 | pr_debug("invalid directory (%s). Skipping.\n", |
| 921 | namelist[i]->d_name); |
| 922 | continue; |
| 923 | } |
| 924 | sprintf(path, "%s/%s/proc/kallsyms", |
| 925 | symbol_conf.guestmount, |
| 926 | namelist[i]->d_name); |
| 927 | ret = access(path, R_OK); |
| 928 | if (ret) { |
| 929 | pr_debug("Can't access file %s\n", path); |
| 930 | goto failure; |
| 931 | } |
| 932 | machines__create_kernel_maps(machines, pid); |
| 933 | } |
| 934 | failure: |
| 935 | free(namelist); |
| 936 | } |
| 937 | |
| 938 | return ret; |
| 939 | } |
| 940 | |
Arnaldo Carvalho de Melo | 876650e | 2012-12-18 19:15:48 -0300 | [diff] [blame] | 941 | void machines__destroy_kernel_maps(struct machines *machines) |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 942 | { |
Arnaldo Carvalho de Melo | 876650e | 2012-12-18 19:15:48 -0300 | [diff] [blame] | 943 | struct rb_node *next = rb_first(&machines->guests); |
| 944 | |
| 945 | machine__destroy_kernel_maps(&machines->host); |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 946 | |
| 947 | while (next) { |
| 948 | struct machine *pos = rb_entry(next, struct machine, rb_node); |
| 949 | |
| 950 | next = rb_next(&pos->rb_node); |
Arnaldo Carvalho de Melo | 876650e | 2012-12-18 19:15:48 -0300 | [diff] [blame] | 951 | rb_erase(&pos->rb_node, &machines->guests); |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 952 | machine__delete(pos); |
| 953 | } |
| 954 | } |
| 955 | |
Arnaldo Carvalho de Melo | 876650e | 2012-12-18 19:15:48 -0300 | [diff] [blame] | 956 | 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] | 957 | { |
| 958 | struct machine *machine = machines__findnew(machines, pid); |
| 959 | |
| 960 | if (machine == NULL) |
| 961 | return -1; |
| 962 | |
| 963 | return machine__create_kernel_maps(machine); |
| 964 | } |
| 965 | |
Arnaldo Carvalho de Melo | e02092b | 2016-04-19 12:12:49 -0300 | [diff] [blame] | 966 | int __machine__load_kallsyms(struct machine *machine, const char *filename, |
Arnaldo Carvalho de Melo | be39db9 | 2016-09-01 19:25:52 -0300 | [diff] [blame] | 967 | enum map_type type, bool no_kcore) |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 968 | { |
Arnaldo Carvalho de Melo | a5e813c | 2015-09-30 11:54:04 -0300 | [diff] [blame] | 969 | struct map *map = machine__kernel_map(machine); |
Arnaldo Carvalho de Melo | be39db9 | 2016-09-01 19:25:52 -0300 | [diff] [blame] | 970 | int ret = __dso__load_kallsyms(map->dso, filename, map, no_kcore); |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 971 | |
| 972 | if (ret > 0) { |
| 973 | dso__set_loaded(map->dso, type); |
| 974 | /* |
| 975 | * Since /proc/kallsyms will have multiple sessions for the |
| 976 | * kernel, with modules between them, fixup the end of all |
| 977 | * sections. |
| 978 | */ |
| 979 | __map_groups__fixup_end(&machine->kmaps, type); |
| 980 | } |
| 981 | |
| 982 | return ret; |
| 983 | } |
| 984 | |
Arnaldo Carvalho de Melo | e02092b | 2016-04-19 12:12:49 -0300 | [diff] [blame] | 985 | int machine__load_kallsyms(struct machine *machine, const char *filename, |
Arnaldo Carvalho de Melo | be39db9 | 2016-09-01 19:25:52 -0300 | [diff] [blame] | 986 | enum map_type type) |
Arnaldo Carvalho de Melo | e02092b | 2016-04-19 12:12:49 -0300 | [diff] [blame] | 987 | { |
Arnaldo Carvalho de Melo | be39db9 | 2016-09-01 19:25:52 -0300 | [diff] [blame] | 988 | return __machine__load_kallsyms(machine, filename, type, false); |
Arnaldo Carvalho de Melo | e02092b | 2016-04-19 12:12:49 -0300 | [diff] [blame] | 989 | } |
| 990 | |
Arnaldo Carvalho de Melo | be39db9 | 2016-09-01 19:25:52 -0300 | [diff] [blame] | 991 | int machine__load_vmlinux_path(struct machine *machine, enum map_type type) |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 992 | { |
Arnaldo Carvalho de Melo | a5e813c | 2015-09-30 11:54:04 -0300 | [diff] [blame] | 993 | struct map *map = machine__kernel_map(machine); |
Arnaldo Carvalho de Melo | be39db9 | 2016-09-01 19:25:52 -0300 | [diff] [blame] | 994 | int ret = dso__load_vmlinux_path(map->dso, map); |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 995 | |
Adrian Hunter | 39b12f78 | 2013-08-07 14:38:47 +0300 | [diff] [blame] | 996 | if (ret > 0) |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 997 | dso__set_loaded(map->dso, type); |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 998 | |
| 999 | return ret; |
| 1000 | } |
| 1001 | |
| 1002 | static void map_groups__fixup_end(struct map_groups *mg) |
| 1003 | { |
| 1004 | int i; |
| 1005 | for (i = 0; i < MAP__NR_TYPES; ++i) |
| 1006 | __map_groups__fixup_end(mg, i); |
| 1007 | } |
| 1008 | |
| 1009 | static char *get_kernel_version(const char *root_dir) |
| 1010 | { |
| 1011 | char version[PATH_MAX]; |
| 1012 | FILE *file; |
| 1013 | char *name, *tmp; |
| 1014 | const char *prefix = "Linux version "; |
| 1015 | |
| 1016 | sprintf(version, "%s/proc/version", root_dir); |
| 1017 | file = fopen(version, "r"); |
| 1018 | if (!file) |
| 1019 | return NULL; |
| 1020 | |
| 1021 | version[0] = '\0'; |
| 1022 | tmp = fgets(version, sizeof(version), file); |
| 1023 | fclose(file); |
| 1024 | |
| 1025 | name = strstr(version, prefix); |
| 1026 | if (!name) |
| 1027 | return NULL; |
| 1028 | name += strlen(prefix); |
| 1029 | tmp = strchr(name, ' '); |
| 1030 | if (tmp) |
| 1031 | *tmp = '\0'; |
| 1032 | |
| 1033 | return strdup(name); |
| 1034 | } |
| 1035 | |
Jiri Olsa | bb58a8a | 2015-02-12 22:20:01 +0100 | [diff] [blame] | 1036 | static bool is_kmod_dso(struct dso *dso) |
| 1037 | { |
| 1038 | return dso->symtab_type == DSO_BINARY_TYPE__SYSTEM_PATH_KMODULE || |
| 1039 | dso->symtab_type == DSO_BINARY_TYPE__GUEST_KMODULE; |
| 1040 | } |
| 1041 | |
| 1042 | static int map_groups__set_module_path(struct map_groups *mg, const char *path, |
| 1043 | struct kmod_path *m) |
| 1044 | { |
| 1045 | struct map *map; |
| 1046 | char *long_name; |
| 1047 | |
| 1048 | map = map_groups__find_by_name(mg, MAP__FUNCTION, m->name); |
| 1049 | if (map == NULL) |
| 1050 | return 0; |
| 1051 | |
| 1052 | long_name = strdup(path); |
| 1053 | if (long_name == NULL) |
| 1054 | return -ENOMEM; |
| 1055 | |
| 1056 | dso__set_long_name(map->dso, long_name, true); |
| 1057 | dso__kernel_module_get_build_id(map->dso, ""); |
| 1058 | |
| 1059 | /* |
| 1060 | * Full name could reveal us kmod compression, so |
| 1061 | * we need to update the symtab_type if needed. |
| 1062 | */ |
| 1063 | if (m->comp && is_kmod_dso(map->dso)) |
| 1064 | map->dso->symtab_type++; |
| 1065 | |
| 1066 | return 0; |
| 1067 | } |
| 1068 | |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 1069 | static int map_groups__set_modules_path_dir(struct map_groups *mg, |
Richard Yao | 61d4290 | 2014-04-26 13:17:55 -0400 | [diff] [blame] | 1070 | const char *dir_name, int depth) |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 1071 | { |
| 1072 | struct dirent *dent; |
| 1073 | DIR *dir = opendir(dir_name); |
| 1074 | int ret = 0; |
| 1075 | |
| 1076 | if (!dir) { |
| 1077 | pr_debug("%s: cannot open %s dir\n", __func__, dir_name); |
| 1078 | return -1; |
| 1079 | } |
| 1080 | |
| 1081 | while ((dent = readdir(dir)) != NULL) { |
| 1082 | char path[PATH_MAX]; |
| 1083 | struct stat st; |
| 1084 | |
| 1085 | /*sshfs might return bad dent->d_type, so we have to stat*/ |
| 1086 | snprintf(path, sizeof(path), "%s/%s", dir_name, dent->d_name); |
| 1087 | if (stat(path, &st)) |
| 1088 | continue; |
| 1089 | |
| 1090 | if (S_ISDIR(st.st_mode)) { |
| 1091 | if (!strcmp(dent->d_name, ".") || |
| 1092 | !strcmp(dent->d_name, "..")) |
| 1093 | continue; |
| 1094 | |
Richard Yao | 61d4290 | 2014-04-26 13:17:55 -0400 | [diff] [blame] | 1095 | /* Do not follow top-level source and build symlinks */ |
| 1096 | if (depth == 0) { |
| 1097 | if (!strcmp(dent->d_name, "source") || |
| 1098 | !strcmp(dent->d_name, "build")) |
| 1099 | continue; |
| 1100 | } |
| 1101 | |
| 1102 | ret = map_groups__set_modules_path_dir(mg, path, |
| 1103 | depth + 1); |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 1104 | if (ret < 0) |
| 1105 | goto out; |
| 1106 | } else { |
Jiri Olsa | bb58a8a | 2015-02-12 22:20:01 +0100 | [diff] [blame] | 1107 | struct kmod_path m; |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 1108 | |
Jiri Olsa | bb58a8a | 2015-02-12 22:20:01 +0100 | [diff] [blame] | 1109 | ret = kmod_path__parse_name(&m, dent->d_name); |
| 1110 | if (ret) |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 1111 | goto out; |
Jiri Olsa | bb58a8a | 2015-02-12 22:20:01 +0100 | [diff] [blame] | 1112 | |
| 1113 | if (m.kmod) |
| 1114 | ret = map_groups__set_module_path(mg, path, &m); |
| 1115 | |
| 1116 | free(m.name); |
| 1117 | |
| 1118 | if (ret) |
| 1119 | goto out; |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 1120 | } |
| 1121 | } |
| 1122 | |
| 1123 | out: |
| 1124 | closedir(dir); |
| 1125 | return ret; |
| 1126 | } |
| 1127 | |
| 1128 | static int machine__set_modules_path(struct machine *machine) |
| 1129 | { |
| 1130 | char *version; |
| 1131 | char modules_path[PATH_MAX]; |
| 1132 | |
| 1133 | version = get_kernel_version(machine->root_dir); |
| 1134 | if (!version) |
| 1135 | return -1; |
| 1136 | |
Richard Yao | 61d4290 | 2014-04-26 13:17:55 -0400 | [diff] [blame] | 1137 | snprintf(modules_path, sizeof(modules_path), "%s/lib/modules/%s", |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 1138 | machine->root_dir, version); |
| 1139 | free(version); |
| 1140 | |
Richard Yao | 61d4290 | 2014-04-26 13:17:55 -0400 | [diff] [blame] | 1141 | 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] | 1142 | } |
Song Shan Gong | 203d8a4 | 2016-07-21 11:10:51 +0800 | [diff] [blame] | 1143 | int __weak arch__fix_module_text_start(u64 *start __maybe_unused, |
| 1144 | const char *name __maybe_unused) |
| 1145 | { |
| 1146 | return 0; |
| 1147 | } |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 1148 | |
Adrian Hunter | 316d70d | 2013-10-08 11:45:48 +0300 | [diff] [blame] | 1149 | static int machine__create_module(void *arg, const char *name, u64 start) |
| 1150 | { |
| 1151 | struct machine *machine = arg; |
| 1152 | struct map *map; |
| 1153 | |
Song Shan Gong | 203d8a4 | 2016-07-21 11:10:51 +0800 | [diff] [blame] | 1154 | if (arch__fix_module_text_start(&start, name) < 0) |
| 1155 | return -1; |
| 1156 | |
Arnaldo Carvalho de Melo | 9f2de31 | 2015-06-01 12:01:02 -0300 | [diff] [blame] | 1157 | map = machine__findnew_module_map(machine, start, name); |
Adrian Hunter | 316d70d | 2013-10-08 11:45:48 +0300 | [diff] [blame] | 1158 | if (map == NULL) |
| 1159 | return -1; |
| 1160 | |
| 1161 | dso__kernel_module_get_build_id(map->dso, machine->root_dir); |
| 1162 | |
| 1163 | return 0; |
| 1164 | } |
| 1165 | |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 1166 | static int machine__create_modules(struct machine *machine) |
| 1167 | { |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 1168 | const char *modules; |
| 1169 | char path[PATH_MAX]; |
| 1170 | |
Adrian Hunter | f4be904 | 2013-09-22 13:22:09 +0300 | [diff] [blame] | 1171 | if (machine__is_default_guest(machine)) { |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 1172 | modules = symbol_conf.default_guest_modules; |
Adrian Hunter | f4be904 | 2013-09-22 13:22:09 +0300 | [diff] [blame] | 1173 | } else { |
| 1174 | snprintf(path, PATH_MAX, "%s/proc/modules", machine->root_dir); |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 1175 | modules = path; |
| 1176 | } |
| 1177 | |
Adrian Hunter | aa7fe3b | 2013-09-22 13:22:09 +0300 | [diff] [blame] | 1178 | if (symbol__restricted_filename(modules, "/proc/modules")) |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 1179 | return -1; |
| 1180 | |
Adrian Hunter | 316d70d | 2013-10-08 11:45:48 +0300 | [diff] [blame] | 1181 | if (modules__parse(modules, machine, machine__create_module)) |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 1182 | return -1; |
| 1183 | |
Adrian Hunter | 316d70d | 2013-10-08 11:45:48 +0300 | [diff] [blame] | 1184 | if (!machine__set_modules_path(machine)) |
| 1185 | return 0; |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 1186 | |
Adrian Hunter | 316d70d | 2013-10-08 11:45:48 +0300 | [diff] [blame] | 1187 | pr_debug("Problems setting modules path maps, continuing anyway...\n"); |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 1188 | |
Jason Wessel | 8f76fcd | 2013-07-15 15:27:53 -0500 | [diff] [blame] | 1189 | return 0; |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 1190 | } |
| 1191 | |
| 1192 | int machine__create_kernel_maps(struct machine *machine) |
| 1193 | { |
| 1194 | struct dso *kernel = machine__get_kernel(machine); |
Arnaldo Carvalho de Melo | b843f62 | 2017-04-27 21:21:09 -0300 | [diff] [blame] | 1195 | const char *name = NULL; |
| 1196 | u64 addr = 0; |
Masami Hiramatsu | 1154c95 | 2015-11-18 15:40:33 +0900 | [diff] [blame] | 1197 | int ret; |
| 1198 | |
Arnaldo Carvalho de Melo | 45e9005 | 2016-05-17 11:52:26 -0300 | [diff] [blame] | 1199 | if (kernel == NULL) |
Adrian Hunter | 5512cf2 | 2014-01-29 16:14:39 +0200 | [diff] [blame] | 1200 | return -1; |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 1201 | |
Masami Hiramatsu | 1154c95 | 2015-11-18 15:40:33 +0900 | [diff] [blame] | 1202 | ret = __machine__create_kernel_maps(machine, kernel); |
| 1203 | dso__put(kernel); |
| 1204 | if (ret < 0) |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 1205 | return -1; |
| 1206 | |
| 1207 | if (symbol_conf.use_modules && machine__create_modules(machine) < 0) { |
| 1208 | if (machine__is_host(machine)) |
| 1209 | pr_debug("Problems creating module maps, " |
| 1210 | "continuing anyway...\n"); |
| 1211 | else |
| 1212 | pr_debug("Problems creating module maps for guest %d, " |
| 1213 | "continuing anyway...\n", machine->pid); |
| 1214 | } |
| 1215 | |
| 1216 | /* |
| 1217 | * Now that we have all the maps created, just set the ->end of them: |
| 1218 | */ |
| 1219 | map_groups__fixup_end(&machine->kmaps); |
Adrian Hunter | 5512cf2 | 2014-01-29 16:14:39 +0200 | [diff] [blame] | 1220 | |
Arnaldo Carvalho de Melo | b843f62 | 2017-04-27 21:21:09 -0300 | [diff] [blame] | 1221 | if (machine__get_running_kernel_start(machine, &name, &addr)) { |
Arnaldo Carvalho de Melo | 45e9005 | 2016-05-17 11:52:26 -0300 | [diff] [blame] | 1222 | } else if (maps__set_kallsyms_ref_reloc_sym(machine->vmlinux_maps, name, addr)) { |
Adrian Hunter | 5512cf2 | 2014-01-29 16:14:39 +0200 | [diff] [blame] | 1223 | machine__destroy_kernel_maps(machine); |
| 1224 | return -1; |
| 1225 | } |
| 1226 | |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 1227 | return 0; |
| 1228 | } |
| 1229 | |
Arnaldo Carvalho de Melo | b0a7d1a | 2012-10-06 16:26:02 -0300 | [diff] [blame] | 1230 | static void machine__set_kernel_mmap_len(struct machine *machine, |
| 1231 | union perf_event *event) |
| 1232 | { |
Namhyung Kim | 4552cf0 | 2012-11-07 16:27:10 +0900 | [diff] [blame] | 1233 | int i; |
| 1234 | |
| 1235 | for (i = 0; i < MAP__NR_TYPES; i++) { |
| 1236 | machine->vmlinux_maps[i]->start = event->mmap.start; |
| 1237 | machine->vmlinux_maps[i]->end = (event->mmap.start + |
| 1238 | event->mmap.len); |
| 1239 | /* |
| 1240 | * Be a bit paranoid here, some perf.data file came with |
| 1241 | * a zero sized synthesized MMAP event for the kernel. |
| 1242 | */ |
| 1243 | if (machine->vmlinux_maps[i]->end == 0) |
| 1244 | machine->vmlinux_maps[i]->end = ~0ULL; |
| 1245 | } |
Arnaldo Carvalho de Melo | b0a7d1a | 2012-10-06 16:26:02 -0300 | [diff] [blame] | 1246 | } |
| 1247 | |
Adrian Hunter | 8e0cf96 | 2013-08-07 14:38:51 +0300 | [diff] [blame] | 1248 | static bool machine__uses_kcore(struct machine *machine) |
| 1249 | { |
| 1250 | struct dso *dso; |
| 1251 | |
Arnaldo Carvalho de Melo | 3d39ac5 | 2015-05-28 13:06:42 -0300 | [diff] [blame] | 1252 | list_for_each_entry(dso, &machine->dsos.head, node) { |
Adrian Hunter | 8e0cf96 | 2013-08-07 14:38:51 +0300 | [diff] [blame] | 1253 | if (dso__is_kcore(dso)) |
| 1254 | return true; |
| 1255 | } |
| 1256 | |
| 1257 | return false; |
| 1258 | } |
| 1259 | |
Arnaldo Carvalho de Melo | b0a7d1a | 2012-10-06 16:26:02 -0300 | [diff] [blame] | 1260 | static int machine__process_kernel_mmap_event(struct machine *machine, |
| 1261 | union perf_event *event) |
| 1262 | { |
| 1263 | struct map *map; |
| 1264 | char kmmap_prefix[PATH_MAX]; |
| 1265 | enum dso_kernel_type kernel_type; |
| 1266 | bool is_kernel_mmap; |
| 1267 | |
Adrian Hunter | 8e0cf96 | 2013-08-07 14:38:51 +0300 | [diff] [blame] | 1268 | /* If we have maps from kcore then we do not need or want any others */ |
| 1269 | if (machine__uses_kcore(machine)) |
| 1270 | return 0; |
| 1271 | |
Arnaldo Carvalho de Melo | b0a7d1a | 2012-10-06 16:26:02 -0300 | [diff] [blame] | 1272 | machine__mmap_name(machine, kmmap_prefix, sizeof(kmmap_prefix)); |
| 1273 | if (machine__is_host(machine)) |
| 1274 | kernel_type = DSO_TYPE_KERNEL; |
| 1275 | else |
| 1276 | kernel_type = DSO_TYPE_GUEST_KERNEL; |
| 1277 | |
| 1278 | is_kernel_mmap = memcmp(event->mmap.filename, |
| 1279 | kmmap_prefix, |
| 1280 | strlen(kmmap_prefix) - 1) == 0; |
| 1281 | if (event->mmap.filename[0] == '/' || |
| 1282 | (!is_kernel_mmap && event->mmap.filename[0] == '[')) { |
Arnaldo Carvalho de Melo | 9f2de31 | 2015-06-01 12:01:02 -0300 | [diff] [blame] | 1283 | map = machine__findnew_module_map(machine, event->mmap.start, |
| 1284 | event->mmap.filename); |
Arnaldo Carvalho de Melo | b0a7d1a | 2012-10-06 16:26:02 -0300 | [diff] [blame] | 1285 | if (map == NULL) |
| 1286 | goto out_problem; |
| 1287 | |
Arnaldo Carvalho de Melo | b0a7d1a | 2012-10-06 16:26:02 -0300 | [diff] [blame] | 1288 | map->end = map->start + event->mmap.len; |
| 1289 | } else if (is_kernel_mmap) { |
| 1290 | const char *symbol_name = (event->mmap.filename + |
| 1291 | strlen(kmmap_prefix)); |
| 1292 | /* |
| 1293 | * Should be there already, from the build-id table in |
| 1294 | * the header. |
| 1295 | */ |
Namhyung Kim | b837a8b | 2014-11-04 10:14:33 +0900 | [diff] [blame] | 1296 | struct dso *kernel = NULL; |
| 1297 | struct dso *dso; |
| 1298 | |
Arnaldo Carvalho de Melo | e880784 | 2015-06-01 15:40:01 -0300 | [diff] [blame] | 1299 | pthread_rwlock_rdlock(&machine->dsos.lock); |
| 1300 | |
Arnaldo Carvalho de Melo | 3d39ac5 | 2015-05-28 13:06:42 -0300 | [diff] [blame] | 1301 | list_for_each_entry(dso, &machine->dsos.head, node) { |
Wang Nan | 1f121b0 | 2015-06-03 08:52:21 +0000 | [diff] [blame] | 1302 | |
| 1303 | /* |
| 1304 | * The cpumode passed to is_kernel_module is not the |
| 1305 | * cpumode of *this* event. If we insist on passing |
| 1306 | * correct cpumode to is_kernel_module, we should |
| 1307 | * record the cpumode when we adding this dso to the |
| 1308 | * linked list. |
| 1309 | * |
| 1310 | * However we don't really need passing correct |
| 1311 | * cpumode. We know the correct cpumode must be kernel |
| 1312 | * mode (if not, we should not link it onto kernel_dsos |
| 1313 | * list). |
| 1314 | * |
| 1315 | * Therefore, we pass PERF_RECORD_MISC_CPUMODE_UNKNOWN. |
| 1316 | * is_kernel_module() treats it as a kernel cpumode. |
| 1317 | */ |
| 1318 | |
| 1319 | if (!dso->kernel || |
| 1320 | is_kernel_module(dso->long_name, |
| 1321 | PERF_RECORD_MISC_CPUMODE_UNKNOWN)) |
Namhyung Kim | b837a8b | 2014-11-04 10:14:33 +0900 | [diff] [blame] | 1322 | continue; |
| 1323 | |
Wang Nan | 1f121b0 | 2015-06-03 08:52:21 +0000 | [diff] [blame] | 1324 | |
Namhyung Kim | b837a8b | 2014-11-04 10:14:33 +0900 | [diff] [blame] | 1325 | kernel = dso; |
| 1326 | break; |
| 1327 | } |
| 1328 | |
Arnaldo Carvalho de Melo | e880784 | 2015-06-01 15:40:01 -0300 | [diff] [blame] | 1329 | pthread_rwlock_unlock(&machine->dsos.lock); |
| 1330 | |
Namhyung Kim | b837a8b | 2014-11-04 10:14:33 +0900 | [diff] [blame] | 1331 | if (kernel == NULL) |
Arnaldo Carvalho de Melo | aa7cc2a | 2015-05-29 11:31:12 -0300 | [diff] [blame] | 1332 | kernel = machine__findnew_dso(machine, kmmap_prefix); |
Arnaldo Carvalho de Melo | b0a7d1a | 2012-10-06 16:26:02 -0300 | [diff] [blame] | 1333 | if (kernel == NULL) |
| 1334 | goto out_problem; |
| 1335 | |
| 1336 | kernel->kernel = kernel_type; |
Arnaldo Carvalho de Melo | d3a7c48 | 2015-06-02 11:53:26 -0300 | [diff] [blame] | 1337 | if (__machine__create_kernel_maps(machine, kernel) < 0) { |
| 1338 | dso__put(kernel); |
Arnaldo Carvalho de Melo | b0a7d1a | 2012-10-06 16:26:02 -0300 | [diff] [blame] | 1339 | goto out_problem; |
Arnaldo Carvalho de Melo | d3a7c48 | 2015-06-02 11:53:26 -0300 | [diff] [blame] | 1340 | } |
Arnaldo Carvalho de Melo | b0a7d1a | 2012-10-06 16:26:02 -0300 | [diff] [blame] | 1341 | |
Namhyung Kim | 330dfa2 | 2014-11-18 13:30:28 +0900 | [diff] [blame] | 1342 | if (strstr(kernel->long_name, "vmlinux")) |
| 1343 | dso__set_short_name(kernel, "[kernel.vmlinux]", false); |
Namhyung Kim | 96d7805 | 2014-11-04 10:14:34 +0900 | [diff] [blame] | 1344 | |
Arnaldo Carvalho de Melo | b0a7d1a | 2012-10-06 16:26:02 -0300 | [diff] [blame] | 1345 | machine__set_kernel_mmap_len(machine, event); |
| 1346 | |
| 1347 | /* |
| 1348 | * Avoid using a zero address (kptr_restrict) for the ref reloc |
| 1349 | * symbol. Effectively having zero here means that at record |
| 1350 | * time /proc/sys/kernel/kptr_restrict was non zero. |
| 1351 | */ |
| 1352 | if (event->mmap.pgoff != 0) { |
| 1353 | maps__set_kallsyms_ref_reloc_sym(machine->vmlinux_maps, |
| 1354 | symbol_name, |
| 1355 | event->mmap.pgoff); |
| 1356 | } |
| 1357 | |
| 1358 | if (machine__is_default_guest(machine)) { |
| 1359 | /* |
| 1360 | * preload dso of guest kernel and modules |
| 1361 | */ |
Arnaldo Carvalho de Melo | be39db9 | 2016-09-01 19:25:52 -0300 | [diff] [blame] | 1362 | dso__load(kernel, machine__kernel_map(machine)); |
Arnaldo Carvalho de Melo | b0a7d1a | 2012-10-06 16:26:02 -0300 | [diff] [blame] | 1363 | } |
| 1364 | } |
| 1365 | return 0; |
| 1366 | out_problem: |
| 1367 | return -1; |
| 1368 | } |
| 1369 | |
Stephane Eranian | 5c5e854 | 2013-08-21 12:10:25 +0200 | [diff] [blame] | 1370 | int machine__process_mmap2_event(struct machine *machine, |
Frederic Weisbecker | 162f0be | 2013-09-11 16:18:24 +0200 | [diff] [blame] | 1371 | union perf_event *event, |
Arnaldo Carvalho de Melo | 473398a | 2016-03-22 18:23:43 -0300 | [diff] [blame] | 1372 | struct perf_sample *sample) |
Stephane Eranian | 5c5e854 | 2013-08-21 12:10:25 +0200 | [diff] [blame] | 1373 | { |
Stephane Eranian | 5c5e854 | 2013-08-21 12:10:25 +0200 | [diff] [blame] | 1374 | struct thread *thread; |
| 1375 | struct map *map; |
| 1376 | enum map_type type; |
| 1377 | int ret = 0; |
| 1378 | |
| 1379 | if (dump_trace) |
| 1380 | perf_event__fprintf_mmap2(event, stdout); |
| 1381 | |
Arnaldo Carvalho de Melo | 473398a | 2016-03-22 18:23:43 -0300 | [diff] [blame] | 1382 | if (sample->cpumode == PERF_RECORD_MISC_GUEST_KERNEL || |
| 1383 | sample->cpumode == PERF_RECORD_MISC_KERNEL) { |
Stephane Eranian | 5c5e854 | 2013-08-21 12:10:25 +0200 | [diff] [blame] | 1384 | ret = machine__process_kernel_mmap_event(machine, event); |
| 1385 | if (ret < 0) |
| 1386 | goto out_problem; |
| 1387 | return 0; |
| 1388 | } |
| 1389 | |
| 1390 | thread = machine__findnew_thread(machine, event->mmap2.pid, |
Don Zickus | 11c9abf | 2014-02-26 10:45:27 -0500 | [diff] [blame] | 1391 | event->mmap2.tid); |
Stephane Eranian | 5c5e854 | 2013-08-21 12:10:25 +0200 | [diff] [blame] | 1392 | if (thread == NULL) |
| 1393 | goto out_problem; |
| 1394 | |
| 1395 | if (event->header.misc & PERF_RECORD_MISC_MMAP_DATA) |
| 1396 | type = MAP__VARIABLE; |
| 1397 | else |
| 1398 | type = MAP__FUNCTION; |
| 1399 | |
Adrian Hunter | 2a03068 | 2014-07-22 16:17:53 +0300 | [diff] [blame] | 1400 | map = map__new(machine, event->mmap2.start, |
Stephane Eranian | 5c5e854 | 2013-08-21 12:10:25 +0200 | [diff] [blame] | 1401 | event->mmap2.len, event->mmap2.pgoff, |
| 1402 | event->mmap2.pid, event->mmap2.maj, |
| 1403 | event->mmap2.min, event->mmap2.ino, |
| 1404 | event->mmap2.ino_generation, |
Don Zickus | 7ef8070 | 2014-05-19 15:13:49 -0400 | [diff] [blame] | 1405 | event->mmap2.prot, |
| 1406 | event->mmap2.flags, |
Adrian Hunter | 5835edd | 2014-07-22 16:18:00 +0300 | [diff] [blame] | 1407 | event->mmap2.filename, type, thread); |
Stephane Eranian | 5c5e854 | 2013-08-21 12:10:25 +0200 | [diff] [blame] | 1408 | |
| 1409 | if (map == NULL) |
Arnaldo Carvalho de Melo | b91fc39 | 2015-04-06 20:43:22 -0300 | [diff] [blame] | 1410 | goto out_problem_map; |
Stephane Eranian | 5c5e854 | 2013-08-21 12:10:25 +0200 | [diff] [blame] | 1411 | |
He Kuang | 8132a2a | 2016-06-03 03:33:13 +0000 | [diff] [blame] | 1412 | ret = thread__insert_map(thread, map); |
| 1413 | if (ret) |
| 1414 | goto out_problem_insert; |
| 1415 | |
Arnaldo Carvalho de Melo | b91fc39 | 2015-04-06 20:43:22 -0300 | [diff] [blame] | 1416 | thread__put(thread); |
Arnaldo Carvalho de Melo | 84c2caf | 2015-05-25 16:59:56 -0300 | [diff] [blame] | 1417 | map__put(map); |
Stephane Eranian | 5c5e854 | 2013-08-21 12:10:25 +0200 | [diff] [blame] | 1418 | return 0; |
| 1419 | |
He Kuang | 8132a2a | 2016-06-03 03:33:13 +0000 | [diff] [blame] | 1420 | out_problem_insert: |
| 1421 | map__put(map); |
Arnaldo Carvalho de Melo | b91fc39 | 2015-04-06 20:43:22 -0300 | [diff] [blame] | 1422 | out_problem_map: |
| 1423 | thread__put(thread); |
Stephane Eranian | 5c5e854 | 2013-08-21 12:10:25 +0200 | [diff] [blame] | 1424 | out_problem: |
| 1425 | dump_printf("problem processing PERF_RECORD_MMAP2, skipping event.\n"); |
| 1426 | return 0; |
| 1427 | } |
| 1428 | |
Frederic Weisbecker | 162f0be | 2013-09-11 16:18:24 +0200 | [diff] [blame] | 1429 | 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] | 1430 | struct perf_sample *sample) |
Arnaldo Carvalho de Melo | b0a7d1a | 2012-10-06 16:26:02 -0300 | [diff] [blame] | 1431 | { |
Arnaldo Carvalho de Melo | b0a7d1a | 2012-10-06 16:26:02 -0300 | [diff] [blame] | 1432 | struct thread *thread; |
| 1433 | struct map *map; |
Stephane Eranian | bad4091 | 2013-01-24 16:10:40 +0100 | [diff] [blame] | 1434 | enum map_type type; |
Arnaldo Carvalho de Melo | b0a7d1a | 2012-10-06 16:26:02 -0300 | [diff] [blame] | 1435 | int ret = 0; |
| 1436 | |
| 1437 | if (dump_trace) |
| 1438 | perf_event__fprintf_mmap(event, stdout); |
| 1439 | |
Arnaldo Carvalho de Melo | 473398a | 2016-03-22 18:23:43 -0300 | [diff] [blame] | 1440 | if (sample->cpumode == PERF_RECORD_MISC_GUEST_KERNEL || |
| 1441 | sample->cpumode == PERF_RECORD_MISC_KERNEL) { |
Arnaldo Carvalho de Melo | b0a7d1a | 2012-10-06 16:26:02 -0300 | [diff] [blame] | 1442 | ret = machine__process_kernel_mmap_event(machine, event); |
| 1443 | if (ret < 0) |
| 1444 | goto out_problem; |
| 1445 | return 0; |
| 1446 | } |
| 1447 | |
Adrian Hunter | 314add6 | 2013-08-27 11:23:03 +0300 | [diff] [blame] | 1448 | thread = machine__findnew_thread(machine, event->mmap.pid, |
Don Zickus | 11c9abf | 2014-02-26 10:45:27 -0500 | [diff] [blame] | 1449 | event->mmap.tid); |
Arnaldo Carvalho de Melo | b0a7d1a | 2012-10-06 16:26:02 -0300 | [diff] [blame] | 1450 | if (thread == NULL) |
| 1451 | goto out_problem; |
Stephane Eranian | bad4091 | 2013-01-24 16:10:40 +0100 | [diff] [blame] | 1452 | |
| 1453 | if (event->header.misc & PERF_RECORD_MISC_MMAP_DATA) |
| 1454 | type = MAP__VARIABLE; |
| 1455 | else |
| 1456 | type = MAP__FUNCTION; |
| 1457 | |
Adrian Hunter | 2a03068 | 2014-07-22 16:17:53 +0300 | [diff] [blame] | 1458 | map = map__new(machine, event->mmap.start, |
Arnaldo Carvalho de Melo | b0a7d1a | 2012-10-06 16:26:02 -0300 | [diff] [blame] | 1459 | event->mmap.len, event->mmap.pgoff, |
Don Zickus | 7ef8070 | 2014-05-19 15:13:49 -0400 | [diff] [blame] | 1460 | event->mmap.pid, 0, 0, 0, 0, 0, 0, |
Stephane Eranian | 5c5e854 | 2013-08-21 12:10:25 +0200 | [diff] [blame] | 1461 | event->mmap.filename, |
Adrian Hunter | 5835edd | 2014-07-22 16:18:00 +0300 | [diff] [blame] | 1462 | type, thread); |
Stephane Eranian | bad4091 | 2013-01-24 16:10:40 +0100 | [diff] [blame] | 1463 | |
Arnaldo Carvalho de Melo | b0a7d1a | 2012-10-06 16:26:02 -0300 | [diff] [blame] | 1464 | if (map == NULL) |
Arnaldo Carvalho de Melo | b91fc39 | 2015-04-06 20:43:22 -0300 | [diff] [blame] | 1465 | goto out_problem_map; |
Arnaldo Carvalho de Melo | b0a7d1a | 2012-10-06 16:26:02 -0300 | [diff] [blame] | 1466 | |
He Kuang | 8132a2a | 2016-06-03 03:33:13 +0000 | [diff] [blame] | 1467 | ret = thread__insert_map(thread, map); |
| 1468 | if (ret) |
| 1469 | goto out_problem_insert; |
| 1470 | |
Arnaldo Carvalho de Melo | b91fc39 | 2015-04-06 20:43:22 -0300 | [diff] [blame] | 1471 | thread__put(thread); |
Arnaldo Carvalho de Melo | 84c2caf | 2015-05-25 16:59:56 -0300 | [diff] [blame] | 1472 | map__put(map); |
Arnaldo Carvalho de Melo | b0a7d1a | 2012-10-06 16:26:02 -0300 | [diff] [blame] | 1473 | return 0; |
| 1474 | |
He Kuang | 8132a2a | 2016-06-03 03:33:13 +0000 | [diff] [blame] | 1475 | out_problem_insert: |
| 1476 | map__put(map); |
Arnaldo Carvalho de Melo | b91fc39 | 2015-04-06 20:43:22 -0300 | [diff] [blame] | 1477 | out_problem_map: |
| 1478 | thread__put(thread); |
Arnaldo Carvalho de Melo | b0a7d1a | 2012-10-06 16:26:02 -0300 | [diff] [blame] | 1479 | out_problem: |
| 1480 | dump_printf("problem processing PERF_RECORD_MMAP, skipping event.\n"); |
| 1481 | return 0; |
| 1482 | } |
| 1483 | |
Arnaldo Carvalho de Melo | b91fc39 | 2015-04-06 20:43:22 -0300 | [diff] [blame] | 1484 | 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] | 1485 | { |
Arnaldo Carvalho de Melo | f3b623b | 2015-03-02 22:21:35 -0300 | [diff] [blame] | 1486 | if (machine->last_match == th) |
Arnaldo Carvalho de Melo | 0ceb8f6 | 2015-05-11 18:08:12 -0300 | [diff] [blame] | 1487 | machine->last_match = NULL; |
Arnaldo Carvalho de Melo | f3b623b | 2015-03-02 22:21:35 -0300 | [diff] [blame] | 1488 | |
Elena Reshetova | e34f5b1 | 2017-02-21 17:35:02 +0200 | [diff] [blame] | 1489 | BUG_ON(refcount_read(&th->refcnt) == 0); |
Arnaldo Carvalho de Melo | b91fc39 | 2015-04-06 20:43:22 -0300 | [diff] [blame] | 1490 | if (lock) |
| 1491 | pthread_rwlock_wrlock(&machine->threads_lock); |
Arnaldo Carvalho de Melo | 0170b14 | 2015-05-25 15:23:05 -0300 | [diff] [blame] | 1492 | rb_erase_init(&th->rb_node, &machine->threads); |
Arnaldo Carvalho de Melo | b91fc39 | 2015-04-06 20:43:22 -0300 | [diff] [blame] | 1493 | RB_CLEAR_NODE(&th->rb_node); |
Arnaldo Carvalho de Melo | d2c1103 | 2016-05-04 10:09:33 -0300 | [diff] [blame] | 1494 | --machine->nr_threads; |
David Ahern | 236a3bb | 2013-08-14 08:49:27 -0600 | [diff] [blame] | 1495 | /* |
Arnaldo Carvalho de Melo | f3b623b | 2015-03-02 22:21:35 -0300 | [diff] [blame] | 1496 | * Move it first to the dead_threads list, then drop the reference, |
| 1497 | * if this is the last reference, then the thread__delete destructor |
| 1498 | * 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] | 1499 | */ |
| 1500 | list_add_tail(&th->node, &machine->dead_threads); |
Arnaldo Carvalho de Melo | b91fc39 | 2015-04-06 20:43:22 -0300 | [diff] [blame] | 1501 | if (lock) |
| 1502 | pthread_rwlock_unlock(&machine->threads_lock); |
Arnaldo Carvalho de Melo | f3b623b | 2015-03-02 22:21:35 -0300 | [diff] [blame] | 1503 | thread__put(th); |
David Ahern | 236a3bb | 2013-08-14 08:49:27 -0600 | [diff] [blame] | 1504 | } |
| 1505 | |
Arnaldo Carvalho de Melo | b91fc39 | 2015-04-06 20:43:22 -0300 | [diff] [blame] | 1506 | void machine__remove_thread(struct machine *machine, struct thread *th) |
| 1507 | { |
| 1508 | return __machine__remove_thread(machine, th, true); |
| 1509 | } |
| 1510 | |
Frederic Weisbecker | 162f0be | 2013-09-11 16:18:24 +0200 | [diff] [blame] | 1511 | int machine__process_fork_event(struct machine *machine, union perf_event *event, |
| 1512 | struct perf_sample *sample) |
Arnaldo Carvalho de Melo | b0a7d1a | 2012-10-06 16:26:02 -0300 | [diff] [blame] | 1513 | { |
Jiri Olsa | d75e609 | 2014-03-14 15:00:03 +0100 | [diff] [blame] | 1514 | struct thread *thread = machine__find_thread(machine, |
| 1515 | event->fork.pid, |
| 1516 | event->fork.tid); |
Adrian Hunter | 314add6 | 2013-08-27 11:23:03 +0300 | [diff] [blame] | 1517 | struct thread *parent = machine__findnew_thread(machine, |
| 1518 | event->fork.ppid, |
| 1519 | event->fork.ptid); |
Arnaldo Carvalho de Melo | b91fc39 | 2015-04-06 20:43:22 -0300 | [diff] [blame] | 1520 | int err = 0; |
Arnaldo Carvalho de Melo | b0a7d1a | 2012-10-06 16:26:02 -0300 | [diff] [blame] | 1521 | |
Adrian Hunter | 5cb7334 | 2015-08-19 17:29:20 +0300 | [diff] [blame] | 1522 | if (dump_trace) |
| 1523 | perf_event__fprintf_task(event, stdout); |
| 1524 | |
| 1525 | /* |
| 1526 | * There may be an existing thread that is not actually the parent, |
| 1527 | * either because we are processing events out of order, or because the |
| 1528 | * (fork) event that would have removed the thread was lost. Assume the |
| 1529 | * latter case and continue on as best we can. |
| 1530 | */ |
| 1531 | if (parent->pid_ != (pid_t)event->fork.ppid) { |
| 1532 | dump_printf("removing erroneous parent thread %d/%d\n", |
| 1533 | parent->pid_, parent->tid); |
| 1534 | machine__remove_thread(machine, parent); |
| 1535 | thread__put(parent); |
| 1536 | parent = machine__findnew_thread(machine, event->fork.ppid, |
| 1537 | event->fork.ptid); |
| 1538 | } |
| 1539 | |
David Ahern | 236a3bb | 2013-08-14 08:49:27 -0600 | [diff] [blame] | 1540 | /* 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] | 1541 | if (thread != NULL) { |
David Ahern | 236a3bb | 2013-08-14 08:49:27 -0600 | [diff] [blame] | 1542 | machine__remove_thread(machine, thread); |
Arnaldo Carvalho de Melo | b91fc39 | 2015-04-06 20:43:22 -0300 | [diff] [blame] | 1543 | thread__put(thread); |
| 1544 | } |
David Ahern | 236a3bb | 2013-08-14 08:49:27 -0600 | [diff] [blame] | 1545 | |
Adrian Hunter | 314add6 | 2013-08-27 11:23:03 +0300 | [diff] [blame] | 1546 | thread = machine__findnew_thread(machine, event->fork.pid, |
| 1547 | event->fork.tid); |
Arnaldo Carvalho de Melo | b0a7d1a | 2012-10-06 16:26:02 -0300 | [diff] [blame] | 1548 | |
| 1549 | if (thread == NULL || parent == NULL || |
Frederic Weisbecker | 162f0be | 2013-09-11 16:18:24 +0200 | [diff] [blame] | 1550 | thread__fork(thread, parent, sample->time) < 0) { |
Arnaldo Carvalho de Melo | b0a7d1a | 2012-10-06 16:26:02 -0300 | [diff] [blame] | 1551 | dump_printf("problem processing PERF_RECORD_FORK, skipping event.\n"); |
Arnaldo Carvalho de Melo | b91fc39 | 2015-04-06 20:43:22 -0300 | [diff] [blame] | 1552 | err = -1; |
Arnaldo Carvalho de Melo | b0a7d1a | 2012-10-06 16:26:02 -0300 | [diff] [blame] | 1553 | } |
Arnaldo Carvalho de Melo | b91fc39 | 2015-04-06 20:43:22 -0300 | [diff] [blame] | 1554 | thread__put(thread); |
| 1555 | thread__put(parent); |
Arnaldo Carvalho de Melo | b0a7d1a | 2012-10-06 16:26:02 -0300 | [diff] [blame] | 1556 | |
Arnaldo Carvalho de Melo | b91fc39 | 2015-04-06 20:43:22 -0300 | [diff] [blame] | 1557 | return err; |
Arnaldo Carvalho de Melo | b0a7d1a | 2012-10-06 16:26:02 -0300 | [diff] [blame] | 1558 | } |
| 1559 | |
Frederic Weisbecker | 162f0be | 2013-09-11 16:18:24 +0200 | [diff] [blame] | 1560 | int machine__process_exit_event(struct machine *machine, union perf_event *event, |
| 1561 | struct perf_sample *sample __maybe_unused) |
Arnaldo Carvalho de Melo | b0a7d1a | 2012-10-06 16:26:02 -0300 | [diff] [blame] | 1562 | { |
Jiri Olsa | d75e609 | 2014-03-14 15:00:03 +0100 | [diff] [blame] | 1563 | struct thread *thread = machine__find_thread(machine, |
| 1564 | event->fork.pid, |
| 1565 | event->fork.tid); |
Arnaldo Carvalho de Melo | b0a7d1a | 2012-10-06 16:26:02 -0300 | [diff] [blame] | 1566 | |
| 1567 | if (dump_trace) |
| 1568 | perf_event__fprintf_task(event, stdout); |
| 1569 | |
Arnaldo Carvalho de Melo | b91fc39 | 2015-04-06 20:43:22 -0300 | [diff] [blame] | 1570 | if (thread != NULL) { |
David Ahern | 236a3bb | 2013-08-14 08:49:27 -0600 | [diff] [blame] | 1571 | thread__exited(thread); |
Arnaldo Carvalho de Melo | b91fc39 | 2015-04-06 20:43:22 -0300 | [diff] [blame] | 1572 | thread__put(thread); |
| 1573 | } |
Arnaldo Carvalho de Melo | b0a7d1a | 2012-10-06 16:26:02 -0300 | [diff] [blame] | 1574 | |
| 1575 | return 0; |
| 1576 | } |
| 1577 | |
Frederic Weisbecker | 162f0be | 2013-09-11 16:18:24 +0200 | [diff] [blame] | 1578 | int machine__process_event(struct machine *machine, union perf_event *event, |
| 1579 | struct perf_sample *sample) |
Arnaldo Carvalho de Melo | b0a7d1a | 2012-10-06 16:26:02 -0300 | [diff] [blame] | 1580 | { |
| 1581 | int ret; |
| 1582 | |
| 1583 | switch (event->header.type) { |
| 1584 | case PERF_RECORD_COMM: |
Frederic Weisbecker | 162f0be | 2013-09-11 16:18:24 +0200 | [diff] [blame] | 1585 | ret = machine__process_comm_event(machine, event, sample); break; |
Arnaldo Carvalho de Melo | b0a7d1a | 2012-10-06 16:26:02 -0300 | [diff] [blame] | 1586 | case PERF_RECORD_MMAP: |
Frederic Weisbecker | 162f0be | 2013-09-11 16:18:24 +0200 | [diff] [blame] | 1587 | ret = machine__process_mmap_event(machine, event, sample); break; |
Hari Bathini | f3b3614 | 2017-03-08 02:11:43 +0530 | [diff] [blame] | 1588 | case PERF_RECORD_NAMESPACES: |
| 1589 | ret = machine__process_namespaces_event(machine, event, sample); break; |
Stephane Eranian | 5c5e854 | 2013-08-21 12:10:25 +0200 | [diff] [blame] | 1590 | case PERF_RECORD_MMAP2: |
Frederic Weisbecker | 162f0be | 2013-09-11 16:18:24 +0200 | [diff] [blame] | 1591 | ret = machine__process_mmap2_event(machine, event, sample); break; |
Arnaldo Carvalho de Melo | b0a7d1a | 2012-10-06 16:26:02 -0300 | [diff] [blame] | 1592 | case PERF_RECORD_FORK: |
Frederic Weisbecker | 162f0be | 2013-09-11 16:18:24 +0200 | [diff] [blame] | 1593 | ret = machine__process_fork_event(machine, event, sample); break; |
Arnaldo Carvalho de Melo | b0a7d1a | 2012-10-06 16:26:02 -0300 | [diff] [blame] | 1594 | case PERF_RECORD_EXIT: |
Frederic Weisbecker | 162f0be | 2013-09-11 16:18:24 +0200 | [diff] [blame] | 1595 | ret = machine__process_exit_event(machine, event, sample); break; |
Arnaldo Carvalho de Melo | b0a7d1a | 2012-10-06 16:26:02 -0300 | [diff] [blame] | 1596 | case PERF_RECORD_LOST: |
Frederic Weisbecker | 162f0be | 2013-09-11 16:18:24 +0200 | [diff] [blame] | 1597 | ret = machine__process_lost_event(machine, event, sample); break; |
Adrian Hunter | 4a96f7a | 2015-04-30 17:37:29 +0300 | [diff] [blame] | 1598 | case PERF_RECORD_AUX: |
| 1599 | ret = machine__process_aux_event(machine, event); break; |
Adrian Hunter | 0ad21f6 | 2015-04-30 17:37:30 +0300 | [diff] [blame] | 1600 | case PERF_RECORD_ITRACE_START: |
Jiri Olsa | ceb9291 | 2015-06-29 13:27:45 +0200 | [diff] [blame] | 1601 | ret = machine__process_itrace_start_event(machine, event); break; |
Kan Liang | c4937a9 | 2015-05-10 15:13:15 -0400 | [diff] [blame] | 1602 | case PERF_RECORD_LOST_SAMPLES: |
| 1603 | ret = machine__process_lost_samples_event(machine, event, sample); break; |
Adrian Hunter | 0286039 | 2015-07-21 12:44:03 +0300 | [diff] [blame] | 1604 | case PERF_RECORD_SWITCH: |
| 1605 | case PERF_RECORD_SWITCH_CPU_WIDE: |
| 1606 | ret = machine__process_switch_event(machine, event); break; |
Arnaldo Carvalho de Melo | b0a7d1a | 2012-10-06 16:26:02 -0300 | [diff] [blame] | 1607 | default: |
| 1608 | ret = -1; |
| 1609 | break; |
| 1610 | } |
| 1611 | |
| 1612 | return ret; |
| 1613 | } |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 1614 | |
Greg Price | b21484f | 2012-12-06 21:48:05 -0800 | [diff] [blame] | 1615 | 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] | 1616 | { |
Arnaldo Carvalho de Melo | a7c3899 | 2017-02-13 16:52:15 -0300 | [diff] [blame] | 1617 | if (!regexec(regex, sym->name, 0, NULL, 0)) |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 1618 | return 1; |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 1619 | return 0; |
| 1620 | } |
| 1621 | |
Arnaldo Carvalho de Melo | bb871a9 | 2014-10-23 12:50:25 -0300 | [diff] [blame] | 1622 | static void ip__resolve_ams(struct thread *thread, |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 1623 | struct addr_map_symbol *ams, |
| 1624 | u64 ip) |
| 1625 | { |
| 1626 | struct addr_location al; |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 1627 | |
| 1628 | memset(&al, 0, sizeof(al)); |
Arnaldo Carvalho de Melo | 52a3cb8 | 2014-03-11 16:16:49 -0300 | [diff] [blame] | 1629 | /* |
| 1630 | * We cannot use the header.misc hint to determine whether a |
| 1631 | * branch stack address is user, kernel, guest, hypervisor. |
| 1632 | * Branches may straddle the kernel/user/hypervisor boundaries. |
| 1633 | * Thus, we have to try consecutively until we find a match |
| 1634 | * or else, the symbol is unknown |
| 1635 | */ |
Arnaldo Carvalho de Melo | bb871a9 | 2014-10-23 12:50:25 -0300 | [diff] [blame] | 1636 | thread__find_cpumode_addr_location(thread, MAP__FUNCTION, ip, &al); |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 1637 | |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 1638 | ams->addr = ip; |
| 1639 | ams->al_addr = al.addr; |
| 1640 | ams->sym = al.sym; |
| 1641 | ams->map = al.map; |
| 1642 | } |
| 1643 | |
Arnaldo Carvalho de Melo | bb871a9 | 2014-10-23 12:50:25 -0300 | [diff] [blame] | 1644 | static void ip__resolve_data(struct thread *thread, |
Stephane Eranian | 98a3b32 | 2013-01-24 16:10:35 +0100 | [diff] [blame] | 1645 | u8 m, struct addr_map_symbol *ams, u64 addr) |
| 1646 | { |
| 1647 | struct addr_location al; |
| 1648 | |
| 1649 | memset(&al, 0, sizeof(al)); |
| 1650 | |
Arnaldo Carvalho de Melo | bb871a9 | 2014-10-23 12:50:25 -0300 | [diff] [blame] | 1651 | thread__find_addr_location(thread, m, MAP__VARIABLE, addr, &al); |
Don Zickus | 06b2afc | 2014-08-20 23:25:11 -0400 | [diff] [blame] | 1652 | if (al.map == NULL) { |
| 1653 | /* |
| 1654 | * some shared data regions have execute bit set which puts |
| 1655 | * their mapping in the MAP__FUNCTION type array. |
| 1656 | * Check there as a fallback option before dropping the sample. |
| 1657 | */ |
Arnaldo Carvalho de Melo | bb871a9 | 2014-10-23 12:50:25 -0300 | [diff] [blame] | 1658 | thread__find_addr_location(thread, m, MAP__FUNCTION, addr, &al); |
Don Zickus | 06b2afc | 2014-08-20 23:25:11 -0400 | [diff] [blame] | 1659 | } |
| 1660 | |
Stephane Eranian | 98a3b32 | 2013-01-24 16:10:35 +0100 | [diff] [blame] | 1661 | ams->addr = addr; |
| 1662 | ams->al_addr = al.addr; |
| 1663 | ams->sym = al.sym; |
| 1664 | ams->map = al.map; |
| 1665 | } |
| 1666 | |
Arnaldo Carvalho de Melo | e80faac | 2014-01-22 13:05:06 -0300 | [diff] [blame] | 1667 | struct mem_info *sample__resolve_mem(struct perf_sample *sample, |
| 1668 | struct addr_location *al) |
Stephane Eranian | 98a3b32 | 2013-01-24 16:10:35 +0100 | [diff] [blame] | 1669 | { |
| 1670 | struct mem_info *mi = zalloc(sizeof(*mi)); |
| 1671 | |
| 1672 | if (!mi) |
| 1673 | return NULL; |
| 1674 | |
Arnaldo Carvalho de Melo | bb871a9 | 2014-10-23 12:50:25 -0300 | [diff] [blame] | 1675 | ip__resolve_ams(al->thread, &mi->iaddr, sample->ip); |
| 1676 | ip__resolve_data(al->thread, al->cpumode, &mi->daddr, sample->addr); |
Stephane Eranian | 98a3b32 | 2013-01-24 16:10:35 +0100 | [diff] [blame] | 1677 | mi->data_src.val = sample->data_src; |
| 1678 | |
| 1679 | return mi; |
| 1680 | } |
| 1681 | |
Andi Kleen | 37592b8 | 2014-11-12 18:05:19 -0800 | [diff] [blame] | 1682 | static int add_callchain_ip(struct thread *thread, |
Arnaldo Carvalho de Melo | 91d7b2d | 2016-04-14 14:48:07 -0300 | [diff] [blame] | 1683 | struct callchain_cursor *cursor, |
Andi Kleen | 37592b8 | 2014-11-12 18:05:19 -0800 | [diff] [blame] | 1684 | struct symbol **parent, |
| 1685 | struct addr_location *root_al, |
David Hildenbrand | 73dbcd6 | 2015-03-30 10:11:00 +0200 | [diff] [blame] | 1686 | u8 *cpumode, |
Jin Yao | 410024d | 2016-10-31 09:19:49 +0800 | [diff] [blame] | 1687 | u64 ip, |
| 1688 | bool branch, |
| 1689 | struct branch_flags *flags, |
| 1690 | int nr_loop_iter, |
| 1691 | int samples) |
Andi Kleen | 37592b8 | 2014-11-12 18:05:19 -0800 | [diff] [blame] | 1692 | { |
| 1693 | struct addr_location al; |
| 1694 | |
| 1695 | al.filtered = 0; |
| 1696 | al.sym = NULL; |
David Hildenbrand | 73dbcd6 | 2015-03-30 10:11:00 +0200 | [diff] [blame] | 1697 | if (!cpumode) { |
Andi Kleen | 8b7bad5 | 2014-11-12 18:05:20 -0800 | [diff] [blame] | 1698 | thread__find_cpumode_addr_location(thread, MAP__FUNCTION, |
| 1699 | ip, &al); |
David Hildenbrand | 73dbcd6 | 2015-03-30 10:11:00 +0200 | [diff] [blame] | 1700 | } else { |
Kan Liang | 2e77784 | 2014-12-02 10:06:53 -0500 | [diff] [blame] | 1701 | if (ip >= PERF_CONTEXT_MAX) { |
| 1702 | switch (ip) { |
| 1703 | case PERF_CONTEXT_HV: |
David Hildenbrand | 73dbcd6 | 2015-03-30 10:11:00 +0200 | [diff] [blame] | 1704 | *cpumode = PERF_RECORD_MISC_HYPERVISOR; |
Kan Liang | 2e77784 | 2014-12-02 10:06:53 -0500 | [diff] [blame] | 1705 | break; |
| 1706 | case PERF_CONTEXT_KERNEL: |
David Hildenbrand | 73dbcd6 | 2015-03-30 10:11:00 +0200 | [diff] [blame] | 1707 | *cpumode = PERF_RECORD_MISC_KERNEL; |
Kan Liang | 2e77784 | 2014-12-02 10:06:53 -0500 | [diff] [blame] | 1708 | break; |
| 1709 | case PERF_CONTEXT_USER: |
David Hildenbrand | 73dbcd6 | 2015-03-30 10:11:00 +0200 | [diff] [blame] | 1710 | *cpumode = PERF_RECORD_MISC_USER; |
Kan Liang | 2e77784 | 2014-12-02 10:06:53 -0500 | [diff] [blame] | 1711 | break; |
| 1712 | default: |
| 1713 | pr_debug("invalid callchain context: " |
| 1714 | "%"PRId64"\n", (s64) ip); |
| 1715 | /* |
| 1716 | * It seems the callchain is corrupted. |
| 1717 | * Discard all. |
| 1718 | */ |
Arnaldo Carvalho de Melo | 91d7b2d | 2016-04-14 14:48:07 -0300 | [diff] [blame] | 1719 | callchain_cursor_reset(cursor); |
Kan Liang | 2e77784 | 2014-12-02 10:06:53 -0500 | [diff] [blame] | 1720 | return 1; |
| 1721 | } |
| 1722 | return 0; |
| 1723 | } |
David Hildenbrand | 73dbcd6 | 2015-03-30 10:11:00 +0200 | [diff] [blame] | 1724 | thread__find_addr_location(thread, *cpumode, MAP__FUNCTION, |
| 1725 | ip, &al); |
Kan Liang | 2e77784 | 2014-12-02 10:06:53 -0500 | [diff] [blame] | 1726 | } |
| 1727 | |
Andi Kleen | 37592b8 | 2014-11-12 18:05:19 -0800 | [diff] [blame] | 1728 | if (al.sym != NULL) { |
Jiri Olsa | de7e6a7 | 2016-05-03 13:54:43 +0200 | [diff] [blame] | 1729 | if (perf_hpp_list.parent && !*parent && |
Andi Kleen | 37592b8 | 2014-11-12 18:05:19 -0800 | [diff] [blame] | 1730 | symbol__match_regex(al.sym, &parent_regex)) |
| 1731 | *parent = al.sym; |
| 1732 | else if (have_ignore_callees && root_al && |
| 1733 | symbol__match_regex(al.sym, &ignore_callees_regex)) { |
| 1734 | /* Treat this symbol as the root, |
| 1735 | forgetting its callees. */ |
| 1736 | *root_al = al; |
Arnaldo Carvalho de Melo | 91d7b2d | 2016-04-14 14:48:07 -0300 | [diff] [blame] | 1737 | callchain_cursor_reset(cursor); |
Andi Kleen | 37592b8 | 2014-11-12 18:05:19 -0800 | [diff] [blame] | 1738 | } |
| 1739 | } |
| 1740 | |
Namhyung Kim | b49a8fe | 2015-11-26 16:08:20 +0900 | [diff] [blame] | 1741 | if (symbol_conf.hide_unresolved && al.sym == NULL) |
| 1742 | return 0; |
Jin Yao | 410024d | 2016-10-31 09:19:49 +0800 | [diff] [blame] | 1743 | return callchain_cursor_append(cursor, al.addr, al.map, al.sym, |
| 1744 | branch, flags, nr_loop_iter, samples); |
Andi Kleen | 37592b8 | 2014-11-12 18:05:19 -0800 | [diff] [blame] | 1745 | } |
| 1746 | |
Arnaldo Carvalho de Melo | 644f2df | 2014-01-22 13:15:36 -0300 | [diff] [blame] | 1747 | struct branch_info *sample__resolve_bstack(struct perf_sample *sample, |
| 1748 | struct addr_location *al) |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 1749 | { |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 1750 | unsigned int i; |
Arnaldo Carvalho de Melo | 644f2df | 2014-01-22 13:15:36 -0300 | [diff] [blame] | 1751 | const struct branch_stack *bs = sample->branch_stack; |
| 1752 | 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] | 1753 | |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 1754 | if (!bi) |
| 1755 | return NULL; |
| 1756 | |
| 1757 | for (i = 0; i < bs->nr; i++) { |
Arnaldo Carvalho de Melo | bb871a9 | 2014-10-23 12:50:25 -0300 | [diff] [blame] | 1758 | ip__resolve_ams(al->thread, &bi[i].to, bs->entries[i].to); |
| 1759 | 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] | 1760 | bi[i].flags = bs->entries[i].flags; |
| 1761 | } |
| 1762 | return bi; |
| 1763 | } |
| 1764 | |
Andi Kleen | 8b7bad5 | 2014-11-12 18:05:20 -0800 | [diff] [blame] | 1765 | #define CHASHSZ 127 |
| 1766 | #define CHASHBITS 7 |
| 1767 | #define NO_ENTRY 0xff |
| 1768 | |
| 1769 | #define PERF_MAX_BRANCH_DEPTH 127 |
| 1770 | |
| 1771 | /* Remove loops. */ |
| 1772 | static int remove_loops(struct branch_entry *l, int nr) |
| 1773 | { |
| 1774 | int i, j, off; |
| 1775 | unsigned char chash[CHASHSZ]; |
| 1776 | |
| 1777 | memset(chash, NO_ENTRY, sizeof(chash)); |
| 1778 | |
| 1779 | BUG_ON(PERF_MAX_BRANCH_DEPTH > 255); |
| 1780 | |
| 1781 | for (i = 0; i < nr; i++) { |
| 1782 | int h = hash_64(l[i].from, CHASHBITS) % CHASHSZ; |
| 1783 | |
| 1784 | /* no collision handling for now */ |
| 1785 | if (chash[h] == NO_ENTRY) { |
| 1786 | chash[h] = i; |
| 1787 | } else if (l[chash[h]].from == l[i].from) { |
| 1788 | bool is_loop = true; |
| 1789 | /* check if it is a real loop */ |
| 1790 | off = 0; |
| 1791 | for (j = chash[h]; j < i && i + off < nr; j++, off++) |
| 1792 | if (l[j].from != l[i + off].from) { |
| 1793 | is_loop = false; |
| 1794 | break; |
| 1795 | } |
| 1796 | if (is_loop) { |
| 1797 | memmove(l + i, l + i + off, |
| 1798 | (nr - (i + off)) * sizeof(*l)); |
| 1799 | nr -= off; |
| 1800 | } |
| 1801 | } |
| 1802 | } |
| 1803 | return nr; |
| 1804 | } |
| 1805 | |
Kan Liang | 384b605 | 2015-01-05 13:23:05 -0500 | [diff] [blame] | 1806 | /* |
| 1807 | * Recolve LBR callstack chain sample |
| 1808 | * Return: |
| 1809 | * 1 on success get LBR callchain information |
| 1810 | * 0 no available LBR callchain information, should try fp |
| 1811 | * negative error code on other errors. |
| 1812 | */ |
| 1813 | static int resolve_lbr_callchain_sample(struct thread *thread, |
Arnaldo Carvalho de Melo | 91d7b2d | 2016-04-14 14:48:07 -0300 | [diff] [blame] | 1814 | struct callchain_cursor *cursor, |
Kan Liang | 384b605 | 2015-01-05 13:23:05 -0500 | [diff] [blame] | 1815 | struct perf_sample *sample, |
| 1816 | struct symbol **parent, |
| 1817 | struct addr_location *root_al, |
| 1818 | int max_stack) |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 1819 | { |
Kan Liang | 384b605 | 2015-01-05 13:23:05 -0500 | [diff] [blame] | 1820 | struct ip_callchain *chain = sample->callchain; |
Arnaldo Carvalho de Melo | 18ef15c | 2016-10-03 11:07:24 -0300 | [diff] [blame] | 1821 | int chain_nr = min(max_stack, (int)chain->nr), i; |
David Hildenbrand | 73dbcd6 | 2015-03-30 10:11:00 +0200 | [diff] [blame] | 1822 | u8 cpumode = PERF_RECORD_MISC_USER; |
Kan Liang | 384b605 | 2015-01-05 13:23:05 -0500 | [diff] [blame] | 1823 | u64 ip; |
| 1824 | |
| 1825 | for (i = 0; i < chain_nr; i++) { |
| 1826 | if (chain->ips[i] == PERF_CONTEXT_USER) |
| 1827 | break; |
| 1828 | } |
| 1829 | |
| 1830 | /* LBR only affects the user callchain */ |
| 1831 | if (i != chain_nr) { |
| 1832 | struct branch_stack *lbr_stack = sample->branch_stack; |
Jin Yao | 410024d | 2016-10-31 09:19:49 +0800 | [diff] [blame] | 1833 | int lbr_nr = lbr_stack->nr, j, k; |
| 1834 | bool branch; |
| 1835 | struct branch_flags *flags; |
Kan Liang | 384b605 | 2015-01-05 13:23:05 -0500 | [diff] [blame] | 1836 | /* |
| 1837 | * LBR callstack can only get user call chain. |
| 1838 | * The mix_chain_nr is kernel call chain |
| 1839 | * number plus LBR user call chain number. |
| 1840 | * i is kernel call chain number, |
| 1841 | * 1 is PERF_CONTEXT_USER, |
| 1842 | * lbr_nr + 1 is the user call chain number. |
| 1843 | * For details, please refer to the comments |
| 1844 | * in callchain__printf |
| 1845 | */ |
| 1846 | int mix_chain_nr = i + 1 + lbr_nr + 1; |
| 1847 | |
Kan Liang | 384b605 | 2015-01-05 13:23:05 -0500 | [diff] [blame] | 1848 | for (j = 0; j < mix_chain_nr; j++) { |
Arnaldo Carvalho de Melo | 18ef15c | 2016-10-03 11:07:24 -0300 | [diff] [blame] | 1849 | int err; |
Jin Yao | 410024d | 2016-10-31 09:19:49 +0800 | [diff] [blame] | 1850 | branch = false; |
| 1851 | flags = NULL; |
| 1852 | |
Kan Liang | 384b605 | 2015-01-05 13:23:05 -0500 | [diff] [blame] | 1853 | if (callchain_param.order == ORDER_CALLEE) { |
| 1854 | if (j < i + 1) |
| 1855 | ip = chain->ips[j]; |
Jin Yao | 410024d | 2016-10-31 09:19:49 +0800 | [diff] [blame] | 1856 | else if (j > i + 1) { |
| 1857 | k = j - i - 2; |
| 1858 | ip = lbr_stack->entries[k].from; |
| 1859 | branch = true; |
| 1860 | flags = &lbr_stack->entries[k].flags; |
| 1861 | } else { |
Kan Liang | 384b605 | 2015-01-05 13:23:05 -0500 | [diff] [blame] | 1862 | ip = lbr_stack->entries[0].to; |
Jin Yao | 410024d | 2016-10-31 09:19:49 +0800 | [diff] [blame] | 1863 | branch = true; |
| 1864 | flags = &lbr_stack->entries[0].flags; |
| 1865 | } |
Kan Liang | 384b605 | 2015-01-05 13:23:05 -0500 | [diff] [blame] | 1866 | } else { |
Jin Yao | 410024d | 2016-10-31 09:19:49 +0800 | [diff] [blame] | 1867 | if (j < lbr_nr) { |
| 1868 | k = lbr_nr - j - 1; |
| 1869 | ip = lbr_stack->entries[k].from; |
| 1870 | branch = true; |
| 1871 | flags = &lbr_stack->entries[k].flags; |
| 1872 | } |
Kan Liang | 384b605 | 2015-01-05 13:23:05 -0500 | [diff] [blame] | 1873 | else if (j > lbr_nr) |
| 1874 | ip = chain->ips[i + 1 - (j - lbr_nr)]; |
Jin Yao | 410024d | 2016-10-31 09:19:49 +0800 | [diff] [blame] | 1875 | else { |
Kan Liang | 384b605 | 2015-01-05 13:23:05 -0500 | [diff] [blame] | 1876 | ip = lbr_stack->entries[0].to; |
Jin Yao | 410024d | 2016-10-31 09:19:49 +0800 | [diff] [blame] | 1877 | branch = true; |
| 1878 | flags = &lbr_stack->entries[0].flags; |
| 1879 | } |
Kan Liang | 384b605 | 2015-01-05 13:23:05 -0500 | [diff] [blame] | 1880 | } |
| 1881 | |
Jin Yao | 410024d | 2016-10-31 09:19:49 +0800 | [diff] [blame] | 1882 | err = add_callchain_ip(thread, cursor, parent, |
| 1883 | root_al, &cpumode, ip, |
| 1884 | branch, flags, 0, 0); |
Kan Liang | 384b605 | 2015-01-05 13:23:05 -0500 | [diff] [blame] | 1885 | if (err) |
| 1886 | return (err < 0) ? err : 0; |
| 1887 | } |
| 1888 | return 1; |
| 1889 | } |
| 1890 | |
| 1891 | return 0; |
| 1892 | } |
| 1893 | |
| 1894 | static int thread__resolve_callchain_sample(struct thread *thread, |
Arnaldo Carvalho de Melo | 91d7b2d | 2016-04-14 14:48:07 -0300 | [diff] [blame] | 1895 | struct callchain_cursor *cursor, |
Kan Liang | 384b605 | 2015-01-05 13:23:05 -0500 | [diff] [blame] | 1896 | struct perf_evsel *evsel, |
| 1897 | struct perf_sample *sample, |
| 1898 | struct symbol **parent, |
| 1899 | struct addr_location *root_al, |
| 1900 | int max_stack) |
| 1901 | { |
| 1902 | struct branch_stack *branch = sample->branch_stack; |
| 1903 | struct ip_callchain *chain = sample->callchain; |
Arnaldo Carvalho de Melo | a29d5c9 | 2016-05-16 21:16:54 -0300 | [diff] [blame] | 1904 | int chain_nr = chain->nr; |
David Hildenbrand | 73dbcd6 | 2015-03-30 10:11:00 +0200 | [diff] [blame] | 1905 | u8 cpumode = PERF_RECORD_MISC_USER; |
Arnaldo Carvalho de Melo | bf8bddb | 2016-05-19 11:14:15 -0300 | [diff] [blame] | 1906 | int i, j, err, nr_entries; |
Andi Kleen | 8b7bad5 | 2014-11-12 18:05:20 -0800 | [diff] [blame] | 1907 | int skip_idx = -1; |
| 1908 | int first_call = 0; |
Jin Yao | 410024d | 2016-10-31 09:19:49 +0800 | [diff] [blame] | 1909 | int nr_loop_iter; |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 1910 | |
Arnaldo Carvalho de Melo | acf2abb | 2016-04-18 10:35:03 -0300 | [diff] [blame] | 1911 | if (perf_evsel__has_branch_callstack(evsel)) { |
Arnaldo Carvalho de Melo | 91d7b2d | 2016-04-14 14:48:07 -0300 | [diff] [blame] | 1912 | err = resolve_lbr_callchain_sample(thread, cursor, sample, parent, |
Kan Liang | 384b605 | 2015-01-05 13:23:05 -0500 | [diff] [blame] | 1913 | root_al, max_stack); |
| 1914 | if (err) |
| 1915 | return (err < 0) ? err : 0; |
| 1916 | } |
| 1917 | |
Sukadev Bhattiprolu | a60335b | 2014-06-25 08:49:03 -0700 | [diff] [blame] | 1918 | /* |
| 1919 | * Based on DWARF debug information, some architectures skip |
| 1920 | * a callchain entry saved by the kernel. |
| 1921 | */ |
Arnaldo Carvalho de Melo | bf8bddb | 2016-05-19 11:14:15 -0300 | [diff] [blame] | 1922 | skip_idx = arch_skip_callchain_idx(thread, chain); |
Sukadev Bhattiprolu | a60335b | 2014-06-25 08:49:03 -0700 | [diff] [blame] | 1923 | |
Andi Kleen | 8b7bad5 | 2014-11-12 18:05:20 -0800 | [diff] [blame] | 1924 | /* |
| 1925 | * Add branches to call stack for easier browsing. This gives |
| 1926 | * more context for a sample than just the callers. |
| 1927 | * |
| 1928 | * This uses individual histograms of paths compared to the |
| 1929 | * aggregated histograms the normal LBR mode uses. |
| 1930 | * |
| 1931 | * Limitations for now: |
| 1932 | * - No extra filters |
| 1933 | * - No annotations (should annotate somehow) |
| 1934 | */ |
| 1935 | |
| 1936 | if (branch && callchain_param.branch_callstack) { |
| 1937 | int nr = min(max_stack, (int)branch->nr); |
| 1938 | struct branch_entry be[nr]; |
| 1939 | |
| 1940 | if (branch->nr > PERF_MAX_BRANCH_DEPTH) { |
| 1941 | pr_warning("corrupted branch chain. skipping...\n"); |
| 1942 | goto check_calls; |
| 1943 | } |
| 1944 | |
| 1945 | for (i = 0; i < nr; i++) { |
| 1946 | if (callchain_param.order == ORDER_CALLEE) { |
| 1947 | be[i] = branch->entries[i]; |
| 1948 | /* |
| 1949 | * Check for overlap into the callchain. |
| 1950 | * The return address is one off compared to |
| 1951 | * the branch entry. To adjust for this |
| 1952 | * assume the calling instruction is not longer |
| 1953 | * than 8 bytes. |
| 1954 | */ |
| 1955 | if (i == skip_idx || |
| 1956 | chain->ips[first_call] >= PERF_CONTEXT_MAX) |
| 1957 | first_call++; |
| 1958 | else if (be[i].from < chain->ips[first_call] && |
| 1959 | be[i].from >= chain->ips[first_call] - 8) |
| 1960 | first_call++; |
| 1961 | } else |
| 1962 | be[i] = branch->entries[branch->nr - i - 1]; |
| 1963 | } |
| 1964 | |
Jin Yao | 410024d | 2016-10-31 09:19:49 +0800 | [diff] [blame] | 1965 | nr_loop_iter = nr; |
Andi Kleen | 8b7bad5 | 2014-11-12 18:05:20 -0800 | [diff] [blame] | 1966 | nr = remove_loops(be, nr); |
| 1967 | |
Jin Yao | 410024d | 2016-10-31 09:19:49 +0800 | [diff] [blame] | 1968 | /* |
| 1969 | * Get the number of iterations. |
| 1970 | * It's only approximation, but good enough in practice. |
| 1971 | */ |
| 1972 | if (nr_loop_iter > nr) |
| 1973 | nr_loop_iter = nr_loop_iter - nr + 1; |
| 1974 | else |
| 1975 | nr_loop_iter = 0; |
| 1976 | |
Andi Kleen | 8b7bad5 | 2014-11-12 18:05:20 -0800 | [diff] [blame] | 1977 | for (i = 0; i < nr; i++) { |
Jin Yao | 410024d | 2016-10-31 09:19:49 +0800 | [diff] [blame] | 1978 | if (i == nr - 1) |
| 1979 | err = add_callchain_ip(thread, cursor, parent, |
| 1980 | root_al, |
| 1981 | NULL, be[i].to, |
| 1982 | true, &be[i].flags, |
| 1983 | nr_loop_iter, 1); |
| 1984 | else |
| 1985 | err = add_callchain_ip(thread, cursor, parent, |
| 1986 | root_al, |
| 1987 | NULL, be[i].to, |
| 1988 | true, &be[i].flags, |
| 1989 | 0, 0); |
| 1990 | |
Andi Kleen | 8b7bad5 | 2014-11-12 18:05:20 -0800 | [diff] [blame] | 1991 | if (!err) |
Arnaldo Carvalho de Melo | 91d7b2d | 2016-04-14 14:48:07 -0300 | [diff] [blame] | 1992 | err = add_callchain_ip(thread, cursor, parent, root_al, |
Jin Yao | 410024d | 2016-10-31 09:19:49 +0800 | [diff] [blame] | 1993 | NULL, be[i].from, |
| 1994 | true, &be[i].flags, |
| 1995 | 0, 0); |
Andi Kleen | 8b7bad5 | 2014-11-12 18:05:20 -0800 | [diff] [blame] | 1996 | if (err == -EINVAL) |
| 1997 | break; |
| 1998 | if (err) |
| 1999 | return err; |
| 2000 | } |
| 2001 | chain_nr -= nr; |
| 2002 | } |
| 2003 | |
| 2004 | check_calls: |
Arnaldo Carvalho de Melo | bf8bddb | 2016-05-19 11:14:15 -0300 | [diff] [blame] | 2005 | for (i = first_call, nr_entries = 0; |
Arnaldo Carvalho de Melo | a29d5c9 | 2016-05-16 21:16:54 -0300 | [diff] [blame] | 2006 | i < chain_nr && nr_entries < max_stack; i++) { |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 2007 | u64 ip; |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 2008 | |
| 2009 | if (callchain_param.order == ORDER_CALLEE) |
Sukadev Bhattiprolu | a60335b | 2014-06-25 08:49:03 -0700 | [diff] [blame] | 2010 | j = i; |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 2011 | else |
Sukadev Bhattiprolu | a60335b | 2014-06-25 08:49:03 -0700 | [diff] [blame] | 2012 | j = chain->nr - i - 1; |
| 2013 | |
| 2014 | #ifdef HAVE_SKIP_CALLCHAIN_IDX |
| 2015 | if (j == skip_idx) |
| 2016 | continue; |
| 2017 | #endif |
| 2018 | ip = chain->ips[j]; |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 2019 | |
Arnaldo Carvalho de Melo | bf8bddb | 2016-05-19 11:14:15 -0300 | [diff] [blame] | 2020 | if (ip < PERF_CONTEXT_MAX) |
| 2021 | ++nr_entries; |
Arnaldo Carvalho de Melo | a29d5c9 | 2016-05-16 21:16:54 -0300 | [diff] [blame] | 2022 | |
Jin Yao | 410024d | 2016-10-31 09:19:49 +0800 | [diff] [blame] | 2023 | err = add_callchain_ip(thread, cursor, parent, |
| 2024 | root_al, &cpumode, ip, |
| 2025 | false, NULL, 0, 0); |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 2026 | |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 2027 | if (err) |
Kan Liang | 2e77784 | 2014-12-02 10:06:53 -0500 | [diff] [blame] | 2028 | return (err < 0) ? err : 0; |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 2029 | } |
| 2030 | |
| 2031 | return 0; |
| 2032 | } |
| 2033 | |
| 2034 | static int unwind_entry(struct unwind_entry *entry, void *arg) |
| 2035 | { |
| 2036 | struct callchain_cursor *cursor = arg; |
Namhyung Kim | b49a8fe | 2015-11-26 16:08:20 +0900 | [diff] [blame] | 2037 | |
| 2038 | if (symbol_conf.hide_unresolved && entry->sym == NULL) |
| 2039 | return 0; |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 2040 | return callchain_cursor_append(cursor, entry->ip, |
Jin Yao | 410024d | 2016-10-31 09:19:49 +0800 | [diff] [blame] | 2041 | entry->map, entry->sym, |
| 2042 | false, NULL, 0, 0); |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 2043 | } |
| 2044 | |
Chris Phlipot | 9919a65 | 2016-04-28 01:19:06 -0700 | [diff] [blame] | 2045 | static int thread__resolve_callchain_unwind(struct thread *thread, |
| 2046 | struct callchain_cursor *cursor, |
| 2047 | struct perf_evsel *evsel, |
| 2048 | struct perf_sample *sample, |
| 2049 | int max_stack) |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 2050 | { |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 2051 | /* Can we do dwarf post unwind? */ |
| 2052 | if (!((evsel->attr.sample_type & PERF_SAMPLE_REGS_USER) && |
| 2053 | (evsel->attr.sample_type & PERF_SAMPLE_STACK_USER))) |
| 2054 | return 0; |
| 2055 | |
| 2056 | /* Bail out if nothing was captured. */ |
| 2057 | if ((!sample->user_regs.regs) || |
| 2058 | (!sample->user_stack.size)) |
| 2059 | return 0; |
| 2060 | |
Arnaldo Carvalho de Melo | 91d7b2d | 2016-04-14 14:48:07 -0300 | [diff] [blame] | 2061 | return unwind__get_entries(unwind_entry, cursor, |
Jiri Olsa | 352ea45 | 2014-01-07 13:47:25 +0100 | [diff] [blame] | 2062 | thread, sample, max_stack); |
Chris Phlipot | 9919a65 | 2016-04-28 01:19:06 -0700 | [diff] [blame] | 2063 | } |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 2064 | |
Chris Phlipot | 9919a65 | 2016-04-28 01:19:06 -0700 | [diff] [blame] | 2065 | int thread__resolve_callchain(struct thread *thread, |
| 2066 | struct callchain_cursor *cursor, |
| 2067 | struct perf_evsel *evsel, |
| 2068 | struct perf_sample *sample, |
| 2069 | struct symbol **parent, |
| 2070 | struct addr_location *root_al, |
| 2071 | int max_stack) |
| 2072 | { |
| 2073 | int ret = 0; |
| 2074 | |
| 2075 | callchain_cursor_reset(&callchain_cursor); |
| 2076 | |
| 2077 | if (callchain_param.order == ORDER_CALLEE) { |
| 2078 | ret = thread__resolve_callchain_sample(thread, cursor, |
| 2079 | evsel, sample, |
| 2080 | parent, root_al, |
| 2081 | max_stack); |
| 2082 | if (ret) |
| 2083 | return ret; |
| 2084 | ret = thread__resolve_callchain_unwind(thread, cursor, |
| 2085 | evsel, sample, |
| 2086 | max_stack); |
| 2087 | } else { |
| 2088 | ret = thread__resolve_callchain_unwind(thread, cursor, |
| 2089 | evsel, sample, |
| 2090 | max_stack); |
| 2091 | if (ret) |
| 2092 | return ret; |
| 2093 | ret = thread__resolve_callchain_sample(thread, cursor, |
| 2094 | evsel, sample, |
| 2095 | parent, root_al, |
| 2096 | max_stack); |
| 2097 | } |
| 2098 | |
| 2099 | return ret; |
Arnaldo Carvalho de Melo | 3f067dc | 2012-12-07 17:39:39 -0300 | [diff] [blame] | 2100 | } |
David Ahern | 35feee1 | 2013-09-28 13:12:58 -0600 | [diff] [blame] | 2101 | |
| 2102 | int machine__for_each_thread(struct machine *machine, |
| 2103 | int (*fn)(struct thread *thread, void *p), |
| 2104 | void *priv) |
| 2105 | { |
| 2106 | struct rb_node *nd; |
| 2107 | struct thread *thread; |
| 2108 | int rc = 0; |
| 2109 | |
| 2110 | for (nd = rb_first(&machine->threads); nd; nd = rb_next(nd)) { |
| 2111 | thread = rb_entry(nd, struct thread, rb_node); |
| 2112 | rc = fn(thread, priv); |
| 2113 | if (rc != 0) |
| 2114 | return rc; |
| 2115 | } |
| 2116 | |
| 2117 | list_for_each_entry(thread, &machine->dead_threads, node) { |
| 2118 | rc = fn(thread, priv); |
| 2119 | if (rc != 0) |
| 2120 | return rc; |
| 2121 | } |
| 2122 | return rc; |
| 2123 | } |
Arnaldo Carvalho de Melo | 58d925d | 2013-11-11 11:28:02 -0300 | [diff] [blame] | 2124 | |
Adrian Hunter | a5499b3 | 2015-05-29 16:33:30 +0300 | [diff] [blame] | 2125 | int machines__for_each_thread(struct machines *machines, |
| 2126 | int (*fn)(struct thread *thread, void *p), |
| 2127 | void *priv) |
| 2128 | { |
| 2129 | struct rb_node *nd; |
| 2130 | int rc = 0; |
| 2131 | |
| 2132 | rc = machine__for_each_thread(&machines->host, fn, priv); |
| 2133 | if (rc != 0) |
| 2134 | return rc; |
| 2135 | |
| 2136 | for (nd = rb_first(&machines->guests); nd; nd = rb_next(nd)) { |
| 2137 | struct machine *machine = rb_entry(nd, struct machine, rb_node); |
| 2138 | |
| 2139 | rc = machine__for_each_thread(machine, fn, priv); |
| 2140 | if (rc != 0) |
| 2141 | return rc; |
| 2142 | } |
| 2143 | return rc; |
| 2144 | } |
| 2145 | |
Arnaldo Carvalho de Melo | a33fbd5 | 2013-11-11 11:36:12 -0300 | [diff] [blame] | 2146 | int __machine__synthesize_threads(struct machine *machine, struct perf_tool *tool, |
Arnaldo Carvalho de Melo | 602ad87 | 2013-11-12 16:46:16 -0300 | [diff] [blame] | 2147 | struct target *target, struct thread_map *threads, |
Kan Liang | 9d9cad7 | 2015-06-17 09:51:11 -0400 | [diff] [blame] | 2148 | perf_event__handler_t process, bool data_mmap, |
| 2149 | unsigned int proc_map_timeout) |
Arnaldo Carvalho de Melo | 58d925d | 2013-11-11 11:28:02 -0300 | [diff] [blame] | 2150 | { |
Arnaldo Carvalho de Melo | 602ad87 | 2013-11-12 16:46:16 -0300 | [diff] [blame] | 2151 | if (target__has_task(target)) |
Kan Liang | 9d9cad7 | 2015-06-17 09:51:11 -0400 | [diff] [blame] | 2152 | return perf_event__synthesize_thread_map(tool, threads, process, machine, data_mmap, proc_map_timeout); |
Arnaldo Carvalho de Melo | 602ad87 | 2013-11-12 16:46:16 -0300 | [diff] [blame] | 2153 | else if (target__has_cpu(target)) |
Kan Liang | 9d9cad7 | 2015-06-17 09:51:11 -0400 | [diff] [blame] | 2154 | return perf_event__synthesize_threads(tool, process, machine, data_mmap, proc_map_timeout); |
Arnaldo Carvalho de Melo | 58d925d | 2013-11-11 11:28:02 -0300 | [diff] [blame] | 2155 | /* command specified */ |
| 2156 | return 0; |
| 2157 | } |
Adrian Hunter | b9d266b | 2014-07-22 16:17:25 +0300 | [diff] [blame] | 2158 | |
| 2159 | pid_t machine__get_current_tid(struct machine *machine, int cpu) |
| 2160 | { |
| 2161 | if (cpu < 0 || cpu >= MAX_NR_CPUS || !machine->current_tid) |
| 2162 | return -1; |
| 2163 | |
| 2164 | return machine->current_tid[cpu]; |
| 2165 | } |
| 2166 | |
| 2167 | int machine__set_current_tid(struct machine *machine, int cpu, pid_t pid, |
| 2168 | pid_t tid) |
| 2169 | { |
| 2170 | struct thread *thread; |
| 2171 | |
| 2172 | if (cpu < 0) |
| 2173 | return -EINVAL; |
| 2174 | |
| 2175 | if (!machine->current_tid) { |
| 2176 | int i; |
| 2177 | |
| 2178 | machine->current_tid = calloc(MAX_NR_CPUS, sizeof(pid_t)); |
| 2179 | if (!machine->current_tid) |
| 2180 | return -ENOMEM; |
| 2181 | for (i = 0; i < MAX_NR_CPUS; i++) |
| 2182 | machine->current_tid[i] = -1; |
| 2183 | } |
| 2184 | |
| 2185 | if (cpu >= MAX_NR_CPUS) { |
| 2186 | pr_err("Requested CPU %d too large. ", cpu); |
| 2187 | pr_err("Consider raising MAX_NR_CPUS\n"); |
| 2188 | return -EINVAL; |
| 2189 | } |
| 2190 | |
| 2191 | machine->current_tid[cpu] = tid; |
| 2192 | |
| 2193 | thread = machine__findnew_thread(machine, pid, tid); |
| 2194 | if (!thread) |
| 2195 | return -ENOMEM; |
| 2196 | |
| 2197 | thread->cpu = cpu; |
Arnaldo Carvalho de Melo | b91fc39 | 2015-04-06 20:43:22 -0300 | [diff] [blame] | 2198 | thread__put(thread); |
Adrian Hunter | b9d266b | 2014-07-22 16:17:25 +0300 | [diff] [blame] | 2199 | |
| 2200 | return 0; |
| 2201 | } |
Adrian Hunter | fbe2af4 | 2014-08-15 22:08:39 +0300 | [diff] [blame] | 2202 | |
| 2203 | int machine__get_kernel_start(struct machine *machine) |
| 2204 | { |
Arnaldo Carvalho de Melo | a5e813c | 2015-09-30 11:54:04 -0300 | [diff] [blame] | 2205 | struct map *map = machine__kernel_map(machine); |
Adrian Hunter | fbe2af4 | 2014-08-15 22:08:39 +0300 | [diff] [blame] | 2206 | int err = 0; |
| 2207 | |
| 2208 | /* |
| 2209 | * The only addresses above 2^63 are kernel addresses of a 64-bit |
| 2210 | * kernel. Note that addresses are unsigned so that on a 32-bit system |
| 2211 | * all addresses including kernel addresses are less than 2^32. In |
| 2212 | * that case (32-bit system), if the kernel mapping is unknown, all |
| 2213 | * addresses will be assumed to be in user space - see |
| 2214 | * machine__kernel_ip(). |
| 2215 | */ |
| 2216 | machine->kernel_start = 1ULL << 63; |
| 2217 | if (map) { |
Arnaldo Carvalho de Melo | be39db9 | 2016-09-01 19:25:52 -0300 | [diff] [blame] | 2218 | err = map__load(map); |
Adrian Hunter | fbe2af4 | 2014-08-15 22:08:39 +0300 | [diff] [blame] | 2219 | if (map->start) |
| 2220 | machine->kernel_start = map->start; |
| 2221 | } |
| 2222 | return err; |
| 2223 | } |
Arnaldo Carvalho de Melo | aa7cc2a | 2015-05-29 11:31:12 -0300 | [diff] [blame] | 2224 | |
| 2225 | struct dso *machine__findnew_dso(struct machine *machine, const char *filename) |
| 2226 | { |
Arnaldo Carvalho de Melo | e880784 | 2015-06-01 15:40:01 -0300 | [diff] [blame] | 2227 | return dsos__findnew(&machine->dsos, filename); |
Arnaldo Carvalho de Melo | aa7cc2a | 2015-05-29 11:31:12 -0300 | [diff] [blame] | 2228 | } |
Arnaldo Carvalho de Melo | c3168b0 | 2015-07-22 16:14:29 -0300 | [diff] [blame] | 2229 | |
| 2230 | char *machine__resolve_kernel_addr(void *vmachine, unsigned long long *addrp, char **modp) |
| 2231 | { |
| 2232 | struct machine *machine = vmachine; |
| 2233 | struct map *map; |
Arnaldo Carvalho de Melo | be39db9 | 2016-09-01 19:25:52 -0300 | [diff] [blame] | 2234 | struct symbol *sym = map_groups__find_symbol(&machine->kmaps, MAP__FUNCTION, *addrp, &map); |
Arnaldo Carvalho de Melo | c3168b0 | 2015-07-22 16:14:29 -0300 | [diff] [blame] | 2235 | |
| 2236 | if (sym == NULL) |
| 2237 | return NULL; |
| 2238 | |
| 2239 | *modp = __map__is_kmodule(map) ? (char *)map->dso->short_name : NULL; |
| 2240 | *addrp = map->unmap_ip(map, sym->start); |
| 2241 | return sym->name; |
| 2242 | } |